dwm.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  1. /* See LICENSE file for copyright and license details.
  2. *
  3. * dynamic window manager is designed like any other X client as well. It is
  4. * driven through handling X events. In contrast to other X clients, a window
  5. * manager selects for SubstructureRedirectMask on the root window, to receive
  6. * events about window (dis-)appearance. Only one X connection at a time is
  7. * allowed to select for this event mask.
  8. *
  9. * Calls to fetch an X event from the event queue are blocking. Due reading
  10. * status text from standard input, a select()-driven main loop has been
  11. * implemented which selects for reads on the X connection and STDIN_FILENO to
  12. * handle all data smoothly. The event handlers of dwm are organized in an
  13. * array which is accessed whenever a new event has been fetched. This allows
  14. * event dispatching in O(1) time.
  15. *
  16. * Each child of the root window is called a client, except windows which have
  17. * set the override_redirect flag. Clients are organized in a global
  18. * doubly-linked client list, the focus history is remembered through a global
  19. * stack list. Each client contains an array of Bools of the same size as the
  20. * global tags array to indicate the tags of a client.
  21. *
  22. * Keys and tagging rules are organized as arrays and defined in config.h.
  23. *
  24. * To understand everything else, start reading main().
  25. */
  26. #include <errno.h>
  27. #include <locale.h>
  28. #include <stdarg.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <sys/select.h>
  34. #include <sys/types.h>
  35. #include <sys/wait.h>
  36. #include <X11/cursorfont.h>
  37. #include <X11/keysym.h>
  38. #include <X11/Xatom.h>
  39. #include <X11/Xlib.h>
  40. #include <X11/Xproto.h>
  41. #include <X11/Xutil.h>
  42. /* macros */
  43. #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
  44. #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask))
  45. #define LENGTH(x) (sizeof x / sizeof x[0])
  46. #define MAXTAGLEN 16
  47. #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
  48. /* enums */
  49. enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
  50. enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
  51. enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
  52. enum { WMProtocols, WMDelete, WMName, WMState, WMLast };/* default atoms */
  53. /* typedefs */
  54. typedef struct Client Client;
  55. struct Client {
  56. char name[256];
  57. int x, y, w, h;
  58. int basew, baseh, incw, inch, maxw, maxh, minw, minh;
  59. int minax, maxax, minay, maxay;
  60. long flags;
  61. unsigned int border, oldborder;
  62. Bool isbanned, isfixed, isfloating, isurgent;
  63. Bool *tags;
  64. Client *next;
  65. Client *prev;
  66. Client *snext;
  67. Window win;
  68. };
  69. typedef struct {
  70. int x, y, w, h;
  71. unsigned long norm[ColLast];
  72. unsigned long sel[ColLast];
  73. Drawable drawable;
  74. GC gc;
  75. struct {
  76. int ascent;
  77. int descent;
  78. int height;
  79. XFontSet set;
  80. XFontStruct *xfont;
  81. } font;
  82. } DC; /* draw context */
  83. typedef struct {
  84. unsigned long mod;
  85. KeySym keysym;
  86. void (*func)(const char *arg);
  87. const char *arg;
  88. } Key;
  89. typedef struct {
  90. const char *symbol;
  91. void (*arrange)(void);
  92. Bool isfloating;
  93. } Layout;
  94. typedef struct {
  95. const char *prop;
  96. const char *tag;
  97. Bool isfloating;
  98. } Rule;
  99. /* function declarations */
  100. void applyrules(Client *c);
  101. void arrange(void);
  102. void attach(Client *c);
  103. void attachstack(Client *c);
  104. void ban(Client *c);
  105. void buttonpress(XEvent *e);
  106. void checkotherwm(void);
  107. void cleanup(void);
  108. void configure(Client *c);
  109. void configurenotify(XEvent *e);
  110. void configurerequest(XEvent *e);
  111. void destroynotify(XEvent *e);
  112. void detach(Client *c);
  113. void detachstack(Client *c);
  114. void drawbar(void);
  115. void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
  116. void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
  117. void *emallocz(unsigned int size);
  118. void enternotify(XEvent *e);
  119. void eprint(const char *errstr, ...);
  120. void expose(XEvent *e);
  121. void floating(void); /* default floating layout */
  122. void focus(Client *c);
  123. void focusin(XEvent *e);
  124. void focusnext(const char *arg);
  125. void focusprev(const char *arg);
  126. Client *getclient(Window w);
  127. unsigned long getcolor(const char *colstr);
  128. long getstate(Window w);
  129. Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
  130. void grabbuttons(Client *c, Bool focused);
  131. void grabkeys(void);
  132. unsigned int idxoftag(const char *t);
  133. void initfont(const char *fontstr);
  134. Bool isoccupied(unsigned int t);
  135. Bool isprotodel(Client *c);
  136. Bool isurgent(unsigned int t);
  137. Bool isvisible(Client *c);
  138. void keypress(XEvent *e);
  139. void killclient(const char *arg);
  140. void manage(Window w, XWindowAttributes *wa);
  141. void mappingnotify(XEvent *e);
  142. void maprequest(XEvent *e);
  143. void monocle(void);
  144. void movemouse(Client *c);
  145. Client *nexttiled(Client *c);
  146. void propertynotify(XEvent *e);
  147. void quit(const char *arg);
  148. void reapply(const char *arg);
  149. void resize(Client *c, int x, int y, int w, int h, Bool sizehints);
  150. void resizemouse(Client *c);
  151. void restack(void);
  152. void run(void);
  153. void scan(void);
  154. void setclientstate(Client *c, long state);
  155. void setlayout(const char *arg);
  156. void setup(void);
  157. void spawn(const char *arg);
  158. void tag(const char *arg);
  159. unsigned int textnw(const char *text, unsigned int len);
  160. unsigned int textw(const char *text);
  161. void tileh(void);
  162. void tilehstack(unsigned int n);
  163. unsigned int tilemaster(void);
  164. void tilev(void);
  165. void tilevstack(unsigned int n);
  166. void togglefloating(const char *arg);
  167. void toggletag(const char *arg);
  168. void toggleview(const char *arg);
  169. void unban(Client *c);
  170. void unmanage(Client *c);
  171. void unmapnotify(XEvent *e);
  172. void updatesizehints(Client *c);
  173. void updatetitle(Client *c);
  174. void updatewmhints(Client *c);
  175. void view(const char *arg);
  176. void viewprevtag(const char *arg); /* views previous selected tags */
  177. int xerror(Display *dpy, XErrorEvent *ee);
  178. int xerrordummy(Display *dpy, XErrorEvent *ee);
  179. int xerrorstart(Display *dpy, XErrorEvent *ee);
  180. void zoom(const char *arg);
  181. /* variables */
  182. char stext[256], buf[256];
  183. int screen, sx, sy, sw, sh;
  184. int (*xerrorxlib)(Display *, XErrorEvent *);
  185. unsigned int bh, blw = 0;
  186. unsigned int numlockmask = 0;
  187. void (*handler[LASTEvent]) (XEvent *) = {
  188. [ButtonPress] = buttonpress,
  189. [ConfigureRequest] = configurerequest,
  190. [ConfigureNotify] = configurenotify,
  191. [DestroyNotify] = destroynotify,
  192. [EnterNotify] = enternotify,
  193. [Expose] = expose,
  194. [FocusIn] = focusin,
  195. [KeyPress] = keypress,
  196. [MappingNotify] = mappingnotify,
  197. [MapRequest] = maprequest,
  198. [PropertyNotify] = propertynotify,
  199. [UnmapNotify] = unmapnotify
  200. };
  201. Atom wmatom[WMLast], netatom[NetLast];
  202. Bool otherwm, readin;
  203. Bool running = True;
  204. Bool *prevtags;
  205. Bool *seltags;
  206. Client *clients = NULL;
  207. Client *sel = NULL;
  208. Client *stack = NULL;
  209. Cursor cursor[CurLast];
  210. Display *dpy;
  211. DC dc = {0};
  212. Layout *lt = NULL;
  213. Window root, barwin;
  214. /* configuration, allows nested code to access above variables */
  215. #include "config.h"
  216. #define TAGSZ (LENGTH(tags) * sizeof(Bool))
  217. static Bool tmp[LENGTH(tags)];
  218. /* function implementations */
  219. void
  220. applyrules(Client *c) {
  221. unsigned int i;
  222. Bool matched = False;
  223. Rule *r;
  224. XClassHint ch = { 0 };
  225. /* rule matching */
  226. XGetClassHint(dpy, c->win, &ch);
  227. for(i = 0; i < LENGTH(rules); i++) {
  228. r = &rules[i];
  229. if(strstr(c->name, r->prop)
  230. || (ch.res_class && strstr(ch.res_class, r->prop))
  231. || (ch.res_name && strstr(ch.res_name, r->prop)))
  232. {
  233. c->isfloating = r->isfloating;
  234. if(r->tag) {
  235. c->tags[idxoftag(r->tag)] = True;
  236. matched = True;
  237. }
  238. }
  239. }
  240. if(ch.res_class)
  241. XFree(ch.res_class);
  242. if(ch.res_name)
  243. XFree(ch.res_name);
  244. if(!matched)
  245. memcpy(c->tags, seltags, TAGSZ);
  246. }
  247. void
  248. arrange(void) {
  249. Client *c;
  250. for(c = clients; c; c = c->next)
  251. if(isvisible(c))
  252. unban(c);
  253. else
  254. ban(c);
  255. focus(NULL);
  256. lt->arrange();
  257. restack();
  258. }
  259. void
  260. attach(Client *c) {
  261. if(clients)
  262. clients->prev = c;
  263. c->next = clients;
  264. clients = c;
  265. }
  266. void
  267. attachstack(Client *c) {
  268. c->snext = stack;
  269. stack = c;
  270. }
  271. void
  272. ban(Client *c) {
  273. if(c->isbanned)
  274. return;
  275. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  276. c->isbanned = True;
  277. }
  278. void
  279. buttonpress(XEvent *e) {
  280. unsigned int i, x;
  281. Client *c;
  282. XButtonPressedEvent *ev = &e->xbutton;
  283. if(ev->window == barwin) {
  284. x = 0;
  285. for(i = 0; i < LENGTH(tags); i++) {
  286. x += textw(tags[i]);
  287. if(ev->x < x) {
  288. if(ev->button == Button1) {
  289. if(ev->state & MODKEY)
  290. tag(tags[i]);
  291. else
  292. view(tags[i]);
  293. }
  294. else if(ev->button == Button3) {
  295. if(ev->state & MODKEY)
  296. toggletag(tags[i]);
  297. else
  298. toggleview(tags[i]);
  299. }
  300. return;
  301. }
  302. }
  303. }
  304. else if((c = getclient(ev->window))) {
  305. focus(c);
  306. if(CLEANMASK(ev->state) != MODKEY)
  307. return;
  308. if(ev->button == Button1) {
  309. restack();
  310. movemouse(c);
  311. }
  312. else if(ev->button == Button2) {
  313. if((floating != lt->arrange) && c->isfloating)
  314. togglefloating(NULL);
  315. else
  316. zoom(NULL);
  317. }
  318. else if(ev->button == Button3 && !c->isfixed) {
  319. restack();
  320. resizemouse(c);
  321. }
  322. }
  323. }
  324. void
  325. checkotherwm(void) {
  326. otherwm = False;
  327. XSetErrorHandler(xerrorstart);
  328. /* this causes an error if some other window manager is running */
  329. XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  330. XSync(dpy, False);
  331. if(otherwm)
  332. eprint("dwm: another window manager is already running\n");
  333. XSync(dpy, False);
  334. XSetErrorHandler(NULL);
  335. xerrorxlib = XSetErrorHandler(xerror);
  336. XSync(dpy, False);
  337. }
  338. void
  339. cleanup(void) {
  340. close(STDIN_FILENO);
  341. while(stack) {
  342. unban(stack);
  343. unmanage(stack);
  344. }
  345. if(dc.font.set)
  346. XFreeFontSet(dpy, dc.font.set);
  347. else
  348. XFreeFont(dpy, dc.font.xfont);
  349. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  350. XFreePixmap(dpy, dc.drawable);
  351. XFreeGC(dpy, dc.gc);
  352. XFreeCursor(dpy, cursor[CurNormal]);
  353. XFreeCursor(dpy, cursor[CurResize]);
  354. XFreeCursor(dpy, cursor[CurMove]);
  355. XDestroyWindow(dpy, barwin);
  356. XSync(dpy, False);
  357. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  358. }
  359. void
  360. configure(Client *c) {
  361. XConfigureEvent ce;
  362. ce.type = ConfigureNotify;
  363. ce.display = dpy;
  364. ce.event = c->win;
  365. ce.window = c->win;
  366. ce.x = c->x;
  367. ce.y = c->y;
  368. ce.width = c->w;
  369. ce.height = c->h;
  370. ce.border_width = c->border;
  371. ce.above = None;
  372. ce.override_redirect = False;
  373. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  374. }
  375. void
  376. configurenotify(XEvent *e) {
  377. XConfigureEvent *ev = &e->xconfigure;
  378. if(ev->window == root && (ev->width != sw || ev->height != sh)) {
  379. sw = ev->width;
  380. sh = ev->height;
  381. XFreePixmap(dpy, dc.drawable);
  382. dc.drawable = XCreatePixmap(dpy, root, BW, bh, DefaultDepth(dpy, screen));
  383. XMoveResizeWindow(dpy, barwin, BX, BY, BW, bh);
  384. arrange();
  385. }
  386. }
  387. void
  388. configurerequest(XEvent *e) {
  389. Client *c;
  390. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  391. XWindowChanges wc;
  392. if((c = getclient(ev->window))) {
  393. if(ev->value_mask & CWBorderWidth)
  394. c->border = ev->border_width;
  395. if(c->isfixed || c->isfloating || lt->isfloating) {
  396. if(ev->value_mask & CWX)
  397. c->x = sx + ev->x;
  398. if(ev->value_mask & CWY)
  399. c->y = sy + ev->y;
  400. if(ev->value_mask & CWWidth)
  401. c->w = ev->width;
  402. if(ev->value_mask & CWHeight)
  403. c->h = ev->height;
  404. if((c->x - sx + c->w) > sw && c->isfloating)
  405. c->x = sx + (sw / 2 - c->w / 2); /* center in x direction */
  406. if((c->y - sy + c->h) > sh && c->isfloating)
  407. c->y = sy + (sh / 2 - c->h / 2); /* center in y direction */
  408. if((ev->value_mask & (CWX|CWY))
  409. && !(ev->value_mask & (CWWidth|CWHeight)))
  410. configure(c);
  411. if(isvisible(c))
  412. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  413. }
  414. else
  415. configure(c);
  416. }
  417. else {
  418. wc.x = ev->x;
  419. wc.y = ev->y;
  420. wc.width = ev->width;
  421. wc.height = ev->height;
  422. wc.border_width = ev->border_width;
  423. wc.sibling = ev->above;
  424. wc.stack_mode = ev->detail;
  425. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  426. }
  427. XSync(dpy, False);
  428. }
  429. void
  430. destroynotify(XEvent *e) {
  431. Client *c;
  432. XDestroyWindowEvent *ev = &e->xdestroywindow;
  433. if((c = getclient(ev->window)))
  434. unmanage(c);
  435. }
  436. void
  437. detach(Client *c) {
  438. if(c->prev)
  439. c->prev->next = c->next;
  440. if(c->next)
  441. c->next->prev = c->prev;
  442. if(c == clients)
  443. clients = c->next;
  444. c->next = c->prev = NULL;
  445. }
  446. void
  447. detachstack(Client *c) {
  448. Client **tc;
  449. for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
  450. *tc = c->snext;
  451. }
  452. void
  453. drawbar(void) {
  454. int i, x;
  455. Client *c;
  456. dc.x = 0;
  457. for(c = stack; c && !isvisible(c); c = c->snext);
  458. for(i = 0; i < LENGTH(tags); i++) {
  459. dc.w = textw(tags[i]);
  460. if(seltags[i]) {
  461. drawtext(tags[i], dc.sel, isurgent(i));
  462. drawsquare(c && c->tags[i], isoccupied(i), isurgent(i), dc.sel);
  463. }
  464. else {
  465. drawtext(tags[i], dc.norm, isurgent(i));
  466. drawsquare(c && c->tags[i], isoccupied(i), isurgent(i), dc.norm);
  467. }
  468. dc.x += dc.w;
  469. }
  470. dc.w = blw;
  471. drawtext(lt->symbol, dc.norm, False);
  472. x = dc.x + dc.w;
  473. dc.w = textw(stext);
  474. dc.x = BW - dc.w;
  475. if(dc.x < x) {
  476. dc.x = x;
  477. dc.w = BW - x;
  478. }
  479. drawtext(stext, dc.norm, False);
  480. if((dc.w = dc.x - x) > bh) {
  481. dc.x = x;
  482. if(c) {
  483. drawtext(c->name, dc.sel, False);
  484. drawsquare(False, c->isfloating, False, dc.sel);
  485. }
  486. else
  487. drawtext(NULL, dc.norm, False);
  488. }
  489. XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, BW, bh, 0, 0);
  490. XSync(dpy, False);
  491. }
  492. void
  493. drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
  494. int x;
  495. XGCValues gcv;
  496. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  497. gcv.foreground = col[invert ? ColBG : ColFG];
  498. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  499. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  500. r.x = dc.x + 1;
  501. r.y = dc.y + 1;
  502. if(filled) {
  503. r.width = r.height = x + 1;
  504. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  505. }
  506. else if(empty) {
  507. r.width = r.height = x;
  508. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  509. }
  510. }
  511. void
  512. drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
  513. int x, y, w, h;
  514. unsigned int len, olen;
  515. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  516. XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
  517. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  518. if(!text)
  519. return;
  520. w = 0;
  521. olen = len = strlen(text);
  522. if(len >= sizeof buf)
  523. len = sizeof buf - 1;
  524. memcpy(buf, text, len);
  525. buf[len] = 0;
  526. h = dc.font.ascent + dc.font.descent;
  527. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  528. x = dc.x + (h / 2);
  529. /* shorten text if necessary */
  530. while(len && (w = textnw(buf, len)) > dc.w - h)
  531. buf[--len] = 0;
  532. if(len < olen) {
  533. if(len > 1)
  534. buf[len - 1] = '.';
  535. if(len > 2)
  536. buf[len - 2] = '.';
  537. if(len > 3)
  538. buf[len - 3] = '.';
  539. }
  540. if(w > dc.w)
  541. return; /* too long */
  542. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  543. if(dc.font.set)
  544. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  545. else
  546. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  547. }
  548. void *
  549. emallocz(unsigned int size) {
  550. void *res = calloc(1, size);
  551. if(!res)
  552. eprint("fatal: could not malloc() %u bytes\n", size);
  553. return res;
  554. }
  555. void
  556. enternotify(XEvent *e) {
  557. Client *c;
  558. XCrossingEvent *ev = &e->xcrossing;
  559. if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  560. return;
  561. if((c = getclient(ev->window)))
  562. focus(c);
  563. else
  564. focus(NULL);
  565. }
  566. void
  567. eprint(const char *errstr, ...) {
  568. va_list ap;
  569. va_start(ap, errstr);
  570. vfprintf(stderr, errstr, ap);
  571. va_end(ap);
  572. exit(EXIT_FAILURE);
  573. }
  574. void
  575. expose(XEvent *e) {
  576. XExposeEvent *ev = &e->xexpose;
  577. if(ev->count == 0 && (ev->window == barwin))
  578. drawbar();
  579. }
  580. void
  581. floating(void) { /* default floating layout */
  582. Client *c;
  583. for(c = clients; c; c = c->next)
  584. if(isvisible(c))
  585. resize(c, c->x, c->y, c->w, c->h, True);
  586. }
  587. void
  588. focus(Client *c) {
  589. if(!c || (c && !isvisible(c)))
  590. for(c = stack; c && !isvisible(c); c = c->snext);
  591. if(sel && sel != c) {
  592. grabbuttons(sel, False);
  593. XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
  594. }
  595. if(c) {
  596. detachstack(c);
  597. attachstack(c);
  598. grabbuttons(c, True);
  599. }
  600. sel = c;
  601. if(c) {
  602. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  603. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  604. }
  605. else
  606. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  607. drawbar();
  608. }
  609. void
  610. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  611. XFocusChangeEvent *ev = &e->xfocus;
  612. if(sel && ev->window != sel->win)
  613. XSetInputFocus(dpy, sel->win, RevertToPointerRoot, CurrentTime);
  614. }
  615. void
  616. focusnext(const char *arg) {
  617. Client *c;
  618. if(!sel)
  619. return;
  620. for(c = sel->next; c && !isvisible(c); c = c->next);
  621. if(!c)
  622. for(c = clients; c && !isvisible(c); c = c->next);
  623. if(c) {
  624. focus(c);
  625. restack();
  626. }
  627. }
  628. void
  629. focusprev(const char *arg) {
  630. Client *c;
  631. if(!sel)
  632. return;
  633. for(c = sel->prev; c && !isvisible(c); c = c->prev);
  634. if(!c) {
  635. for(c = clients; c && c->next; c = c->next);
  636. for(; c && !isvisible(c); c = c->prev);
  637. }
  638. if(c) {
  639. focus(c);
  640. restack();
  641. }
  642. }
  643. Client *
  644. getclient(Window w) {
  645. Client *c;
  646. for(c = clients; c && c->win != w; c = c->next);
  647. return c;
  648. }
  649. unsigned long
  650. getcolor(const char *colstr) {
  651. Colormap cmap = DefaultColormap(dpy, screen);
  652. XColor color;
  653. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  654. eprint("error, cannot allocate color '%s'\n", colstr);
  655. return color.pixel;
  656. }
  657. long
  658. getstate(Window w) {
  659. int format, status;
  660. long result = -1;
  661. unsigned char *p = NULL;
  662. unsigned long n, extra;
  663. Atom real;
  664. status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  665. &real, &format, &n, &extra, (unsigned char **)&p);
  666. if(status != Success)
  667. return -1;
  668. if(n != 0)
  669. result = *p;
  670. XFree(p);
  671. return result;
  672. }
  673. Bool
  674. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  675. char **list = NULL;
  676. int n;
  677. XTextProperty name;
  678. if(!text || size == 0)
  679. return False;
  680. text[0] = '\0';
  681. XGetTextProperty(dpy, w, &name, atom);
  682. if(!name.nitems)
  683. return False;
  684. if(name.encoding == XA_STRING)
  685. strncpy(text, (char *)name.value, size - 1);
  686. else {
  687. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  688. && n > 0 && *list) {
  689. strncpy(text, *list, size - 1);
  690. XFreeStringList(list);
  691. }
  692. }
  693. text[size - 1] = '\0';
  694. XFree(name.value);
  695. return True;
  696. }
  697. void
  698. grabbuttons(Client *c, Bool focused) {
  699. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  700. if(focused) {
  701. XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
  702. GrabModeAsync, GrabModeSync, None, None);
  703. XGrabButton(dpy, Button1, MODKEY|LockMask, c->win, False, BUTTONMASK,
  704. GrabModeAsync, GrabModeSync, None, None);
  705. XGrabButton(dpy, Button1, MODKEY|numlockmask, c->win, False, BUTTONMASK,
  706. GrabModeAsync, GrabModeSync, None, None);
  707. XGrabButton(dpy, Button1, MODKEY|numlockmask|LockMask, c->win, False, BUTTONMASK,
  708. GrabModeAsync, GrabModeSync, None, None);
  709. XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
  710. GrabModeAsync, GrabModeSync, None, None);
  711. XGrabButton(dpy, Button2, MODKEY|LockMask, c->win, False, BUTTONMASK,
  712. GrabModeAsync, GrabModeSync, None, None);
  713. XGrabButton(dpy, Button2, MODKEY|numlockmask, c->win, False, BUTTONMASK,
  714. GrabModeAsync, GrabModeSync, None, None);
  715. XGrabButton(dpy, Button2, MODKEY|numlockmask|LockMask, c->win, False, BUTTONMASK,
  716. GrabModeAsync, GrabModeSync, None, None);
  717. XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
  718. GrabModeAsync, GrabModeSync, None, None);
  719. XGrabButton(dpy, Button3, MODKEY|LockMask, c->win, False, BUTTONMASK,
  720. GrabModeAsync, GrabModeSync, None, None);
  721. XGrabButton(dpy, Button3, MODKEY|numlockmask, c->win, False, BUTTONMASK,
  722. GrabModeAsync, GrabModeSync, None, None);
  723. XGrabButton(dpy, Button3, MODKEY|numlockmask|LockMask, c->win, False, BUTTONMASK,
  724. GrabModeAsync, GrabModeSync, None, None);
  725. }
  726. else
  727. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
  728. GrabModeAsync, GrabModeSync, None, None);
  729. }
  730. void
  731. grabkeys(void) {
  732. unsigned int i, j;
  733. KeyCode code;
  734. XModifierKeymap *modmap;
  735. /* init modifier map */
  736. modmap = XGetModifierMapping(dpy);
  737. for(i = 0; i < 8; i++)
  738. for(j = 0; j < modmap->max_keypermod; j++) {
  739. if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
  740. numlockmask = (1 << i);
  741. }
  742. XFreeModifiermap(modmap);
  743. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  744. for(i = 0; i < LENGTH(keys); i++) {
  745. code = XKeysymToKeycode(dpy, keys[i].keysym);
  746. XGrabKey(dpy, code, keys[i].mod, root, True,
  747. GrabModeAsync, GrabModeAsync);
  748. XGrabKey(dpy, code, keys[i].mod|LockMask, root, True,
  749. GrabModeAsync, GrabModeAsync);
  750. XGrabKey(dpy, code, keys[i].mod|numlockmask, root, True,
  751. GrabModeAsync, GrabModeAsync);
  752. XGrabKey(dpy, code, keys[i].mod|numlockmask|LockMask, root, True,
  753. GrabModeAsync, GrabModeAsync);
  754. }
  755. }
  756. unsigned int
  757. idxoftag(const char *t) {
  758. unsigned int i;
  759. for(i = 0; (i < LENGTH(tags)) && (tags[i] != t); i++);
  760. return (i < LENGTH(tags)) ? i : 0;
  761. }
  762. void
  763. initfont(const char *fontstr) {
  764. char *def, **missing;
  765. int i, n;
  766. missing = NULL;
  767. if(dc.font.set)
  768. XFreeFontSet(dpy, dc.font.set);
  769. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  770. if(missing) {
  771. while(n--)
  772. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  773. XFreeStringList(missing);
  774. }
  775. if(dc.font.set) {
  776. XFontSetExtents *font_extents;
  777. XFontStruct **xfonts;
  778. char **font_names;
  779. dc.font.ascent = dc.font.descent = 0;
  780. font_extents = XExtentsOfFontSet(dc.font.set);
  781. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  782. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  783. if(dc.font.ascent < (*xfonts)->ascent)
  784. dc.font.ascent = (*xfonts)->ascent;
  785. if(dc.font.descent < (*xfonts)->descent)
  786. dc.font.descent = (*xfonts)->descent;
  787. xfonts++;
  788. }
  789. }
  790. else {
  791. if(dc.font.xfont)
  792. XFreeFont(dpy, dc.font.xfont);
  793. dc.font.xfont = NULL;
  794. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  795. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  796. eprint("error, cannot load font: '%s'\n", fontstr);
  797. dc.font.ascent = dc.font.xfont->ascent;
  798. dc.font.descent = dc.font.xfont->descent;
  799. }
  800. dc.font.height = dc.font.ascent + dc.font.descent;
  801. }
  802. Bool
  803. isoccupied(unsigned int t) {
  804. Client *c;
  805. for(c = clients; c; c = c->next)
  806. if(c->tags[t])
  807. return True;
  808. return False;
  809. }
  810. Bool
  811. isprotodel(Client *c) {
  812. int i, n;
  813. Atom *protocols;
  814. Bool ret = False;
  815. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  816. for(i = 0; !ret && i < n; i++)
  817. if(protocols[i] == wmatom[WMDelete])
  818. ret = True;
  819. XFree(protocols);
  820. }
  821. return ret;
  822. }
  823. Bool
  824. isurgent(unsigned int t) {
  825. Client *c;
  826. for(c = clients; c; c = c->next)
  827. if(c->isurgent && c->tags[t])
  828. return True;
  829. return False;
  830. }
  831. Bool
  832. isvisible(Client *c) {
  833. unsigned int i;
  834. for(i = 0; i < LENGTH(tags); i++)
  835. if(c->tags[i] && seltags[i])
  836. return True;
  837. return False;
  838. }
  839. void
  840. keypress(XEvent *e) {
  841. unsigned int i;
  842. KeySym keysym;
  843. XKeyEvent *ev;
  844. ev = &e->xkey;
  845. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  846. for(i = 0; i < LENGTH(keys); i++)
  847. if(keysym == keys[i].keysym
  848. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state))
  849. {
  850. if(keys[i].func)
  851. keys[i].func(keys[i].arg);
  852. }
  853. }
  854. void
  855. killclient(const char *arg) {
  856. XEvent ev;
  857. if(!sel)
  858. return;
  859. if(isprotodel(sel)) {
  860. ev.type = ClientMessage;
  861. ev.xclient.window = sel->win;
  862. ev.xclient.message_type = wmatom[WMProtocols];
  863. ev.xclient.format = 32;
  864. ev.xclient.data.l[0] = wmatom[WMDelete];
  865. ev.xclient.data.l[1] = CurrentTime;
  866. XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
  867. }
  868. else
  869. XKillClient(dpy, sel->win);
  870. }
  871. void
  872. manage(Window w, XWindowAttributes *wa) {
  873. Client *c, *t = NULL;
  874. Status rettrans;
  875. Window trans;
  876. XWindowChanges wc;
  877. c = emallocz(sizeof(Client));
  878. c->tags = emallocz(TAGSZ);
  879. c->win = w;
  880. /* geometry */
  881. c->x = wa->x;
  882. c->y = wa->y;
  883. c->w = wa->width;
  884. c->h = wa->height;
  885. c->oldborder = wa->border_width;
  886. if(c->w == sw && c->h == sh) {
  887. c->x = sx;
  888. c->y = sy;
  889. c->border = wa->border_width;
  890. }
  891. else {
  892. if(c->x + c->w + 2 * c->border > WX + WW)
  893. c->x = WX + WW - c->w - 2 * c->border;
  894. if(c->y + c->h + 2 * c->border > WY + WH)
  895. c->y = WY + WH - c->h - 2 * c->border;
  896. if(c->x < WX)
  897. c->x = WX;
  898. if(c->y < WY)
  899. c->y = WY;
  900. c->border = BORDERPX;
  901. }
  902. wc.border_width = c->border;
  903. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  904. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  905. configure(c); /* propagates border_width, if size doesn't change */
  906. updatesizehints(c);
  907. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  908. grabbuttons(c, False);
  909. updatetitle(c);
  910. if((rettrans = XGetTransientForHint(dpy, w, &trans) == Success))
  911. for(t = clients; t && t->win != trans; t = t->next);
  912. if(t)
  913. memcpy(c->tags, t->tags, TAGSZ);
  914. else
  915. applyrules(c);
  916. if(!c->isfloating)
  917. c->isfloating = (rettrans == Success) || c->isfixed;
  918. attach(c);
  919. attachstack(c);
  920. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); /* some windows require this */
  921. ban(c);
  922. XMapWindow(dpy, c->win);
  923. setclientstate(c, NormalState);
  924. arrange();
  925. }
  926. void
  927. mappingnotify(XEvent *e) {
  928. XMappingEvent *ev = &e->xmapping;
  929. XRefreshKeyboardMapping(ev);
  930. if(ev->request == MappingKeyboard)
  931. grabkeys();
  932. }
  933. void
  934. maprequest(XEvent *e) {
  935. static XWindowAttributes wa;
  936. XMapRequestEvent *ev = &e->xmaprequest;
  937. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  938. return;
  939. if(wa.override_redirect)
  940. return;
  941. if(!getclient(ev->window))
  942. manage(ev->window, &wa);
  943. }
  944. void
  945. monocle(void) {
  946. Client *c;
  947. for(c = clients; c; c = c->next)
  948. if(isvisible(c))
  949. resize(c, MOX, MOY, MOW, MOH, RESIZEHINTS);
  950. }
  951. void
  952. movemouse(Client *c) {
  953. int x1, y1, ocx, ocy, di, nx, ny;
  954. unsigned int dui;
  955. Window dummy;
  956. XEvent ev;
  957. ocx = nx = c->x;
  958. ocy = ny = c->y;
  959. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  960. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  961. return;
  962. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  963. for(;;) {
  964. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  965. switch (ev.type) {
  966. case ButtonRelease:
  967. XUngrabPointer(dpy, CurrentTime);
  968. return;
  969. case ConfigureRequest:
  970. case Expose:
  971. case MapRequest:
  972. handler[ev.type](&ev);
  973. break;
  974. case MotionNotify:
  975. XSync(dpy, False);
  976. nx = ocx + (ev.xmotion.x - x1);
  977. ny = ocy + (ev.xmotion.y - y1);
  978. if(abs(WX - nx) < SNAP)
  979. nx = WX;
  980. else if(abs((WX + WW) - (nx + c->w + 2 * c->border)) < SNAP)
  981. nx = WX + WW - c->w - 2 * c->border;
  982. if(abs(WY - ny) < SNAP)
  983. ny = WY;
  984. else if(abs((WY + WH) - (ny + c->h + 2 * c->border)) < SNAP)
  985. ny = WY + WH - c->h - 2 * c->border;
  986. if(!c->isfloating && !lt->isfloating && (abs(nx - c->x) > SNAP || abs(ny - c->y) > SNAP))
  987. togglefloating(NULL);
  988. if((lt->isfloating) || c->isfloating)
  989. resize(c, nx, ny, c->w, c->h, False);
  990. break;
  991. }
  992. }
  993. }
  994. Client *
  995. nexttiled(Client *c) {
  996. for(; c && (c->isfloating || !isvisible(c)); c = c->next);
  997. return c;
  998. }
  999. void
  1000. propertynotify(XEvent *e) {
  1001. Client *c;
  1002. Window trans;
  1003. XPropertyEvent *ev = &e->xproperty;
  1004. if(ev->state == PropertyDelete)
  1005. return; /* ignore */
  1006. if((c = getclient(ev->window))) {
  1007. switch (ev->atom) {
  1008. default: break;
  1009. case XA_WM_TRANSIENT_FOR:
  1010. XGetTransientForHint(dpy, c->win, &trans);
  1011. if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
  1012. arrange();
  1013. break;
  1014. case XA_WM_NORMAL_HINTS:
  1015. updatesizehints(c);
  1016. break;
  1017. case XA_WM_HINTS:
  1018. updatewmhints(c);
  1019. drawbar();
  1020. break;
  1021. }
  1022. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1023. updatetitle(c);
  1024. if(c == sel)
  1025. drawbar();
  1026. }
  1027. }
  1028. }
  1029. void
  1030. quit(const char *arg) {
  1031. readin = running = False;
  1032. }
  1033. void
  1034. reapply(const char *arg) {
  1035. static Bool zerotags[LENGTH(tags)] = { 0 };
  1036. Client *c;
  1037. for(c = clients; c; c = c->next) {
  1038. memcpy(c->tags, zerotags, sizeof zerotags);
  1039. applyrules(c);
  1040. }
  1041. arrange();
  1042. }
  1043. void
  1044. resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
  1045. XWindowChanges wc;
  1046. if(sizehints) {
  1047. /* set minimum possible */
  1048. if (w < 1)
  1049. w = 1;
  1050. if (h < 1)
  1051. h = 1;
  1052. /* temporarily remove base dimensions */
  1053. w -= c->basew;
  1054. h -= c->baseh;
  1055. /* adjust for aspect limits */
  1056. if (c->minay > 0 && c->maxay > 0 && c->minax > 0 && c->maxax > 0) {
  1057. if (w * c->maxay > h * c->maxax)
  1058. w = h * c->maxax / c->maxay;
  1059. else if (w * c->minay < h * c->minax)
  1060. h = w * c->minay / c->minax;
  1061. }
  1062. /* adjust for increment value */
  1063. if(c->incw)
  1064. w -= w % c->incw;
  1065. if(c->inch)
  1066. h -= h % c->inch;
  1067. /* restore base dimensions */
  1068. w += c->basew;
  1069. h += c->baseh;
  1070. if(c->minw > 0 && w < c->minw)
  1071. w = c->minw;
  1072. if(c->minh > 0 && h < c->minh)
  1073. h = c->minh;
  1074. if(c->maxw > 0 && w > c->maxw)
  1075. w = c->maxw;
  1076. if(c->maxh > 0 && h > c->maxh)
  1077. h = c->maxh;
  1078. }
  1079. if(w <= 0 || h <= 0)
  1080. return;
  1081. if(x > sx + sw)
  1082. x = sw - w - 2 * c->border;
  1083. if(y > sy + sh)
  1084. y = sh - h - 2 * c->border;
  1085. if(x + w + 2 * c->border < sx)
  1086. x = sx;
  1087. if(y + h + 2 * c->border < sy)
  1088. y = sy;
  1089. if(c->x != x || c->y != y || c->w != w || c->h != h) {
  1090. c->x = wc.x = x;
  1091. c->y = wc.y = y;
  1092. c->w = wc.width = w;
  1093. c->h = wc.height = h;
  1094. wc.border_width = c->border;
  1095. XConfigureWindow(dpy, c->win,
  1096. CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1097. configure(c);
  1098. XSync(dpy, False);
  1099. }
  1100. }
  1101. void
  1102. resizemouse(Client *c) {
  1103. int ocx, ocy;
  1104. int nw, nh;
  1105. XEvent ev;
  1106. ocx = c->x;
  1107. ocy = c->y;
  1108. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1109. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1110. return;
  1111. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
  1112. for(;;) {
  1113. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask , &ev);
  1114. switch(ev.type) {
  1115. case ButtonRelease:
  1116. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
  1117. c->w + c->border - 1, c->h + c->border - 1);
  1118. XUngrabPointer(dpy, CurrentTime);
  1119. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1120. return;
  1121. case ConfigureRequest:
  1122. case Expose:
  1123. case MapRequest:
  1124. handler[ev.type](&ev);
  1125. break;
  1126. case MotionNotify:
  1127. XSync(dpy, False);
  1128. if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
  1129. nw = 1;
  1130. if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
  1131. nh = 1;
  1132. if(!c->isfloating && !lt->isfloating && (abs(nw - c->w) > SNAP || abs(nh - c->h) > SNAP))
  1133. togglefloating(NULL);
  1134. if((lt->isfloating) || c->isfloating)
  1135. resize(c, c->x, c->y, nw, nh, True);
  1136. break;
  1137. }
  1138. }
  1139. }
  1140. void
  1141. restack(void) {
  1142. Client *c;
  1143. XEvent ev;
  1144. XWindowChanges wc;
  1145. drawbar();
  1146. if(!sel)
  1147. return;
  1148. if(sel->isfloating || lt->isfloating)
  1149. XRaiseWindow(dpy, sel->win);
  1150. if(!lt->isfloating) {
  1151. wc.stack_mode = Below;
  1152. wc.sibling = barwin;
  1153. if(!sel->isfloating) {
  1154. XConfigureWindow(dpy, sel->win, CWSibling|CWStackMode, &wc);
  1155. wc.sibling = sel->win;
  1156. }
  1157. for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
  1158. if(c == sel)
  1159. continue;
  1160. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1161. wc.sibling = c->win;
  1162. }
  1163. }
  1164. XSync(dpy, False);
  1165. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1166. }
  1167. void
  1168. run(void) {
  1169. char *p;
  1170. char sbuf[sizeof stext];
  1171. fd_set rd;
  1172. int r, xfd;
  1173. unsigned int len, offset;
  1174. XEvent ev;
  1175. /* main event loop, also reads status text from stdin */
  1176. XSync(dpy, False);
  1177. xfd = ConnectionNumber(dpy);
  1178. readin = True;
  1179. offset = 0;
  1180. len = sizeof stext - 1;
  1181. sbuf[len] = stext[len] = '\0'; /* 0-terminator is never touched */
  1182. while(running) {
  1183. FD_ZERO(&rd);
  1184. if(readin)
  1185. FD_SET(STDIN_FILENO, &rd);
  1186. FD_SET(xfd, &rd);
  1187. if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
  1188. if(errno == EINTR)
  1189. continue;
  1190. eprint("select failed\n");
  1191. }
  1192. if(FD_ISSET(STDIN_FILENO, &rd)) {
  1193. switch((r = read(STDIN_FILENO, sbuf + offset, len - offset))) {
  1194. case -1:
  1195. strncpy(stext, strerror(errno), len);
  1196. readin = False;
  1197. break;
  1198. case 0:
  1199. strncpy(stext, "EOF", 4);
  1200. readin = False;
  1201. break;
  1202. default:
  1203. for(p = sbuf + offset; r > 0; p++, r--, offset++)
  1204. if(*p == '\n' || *p == '\0') {
  1205. *p = '\0';
  1206. strncpy(stext, sbuf, len);
  1207. p += r - 1; /* p is sbuf + offset + r - 1 */
  1208. for(r = 0; *(p - r) && *(p - r) != '\n'; r++);
  1209. offset = r;
  1210. if(r)
  1211. memmove(sbuf, p - r + 1, r);
  1212. break;
  1213. }
  1214. break;
  1215. }
  1216. drawbar();
  1217. }
  1218. while(XPending(dpy)) {
  1219. XNextEvent(dpy, &ev);
  1220. if(handler[ev.type])
  1221. (handler[ev.type])(&ev); /* call handler */
  1222. }
  1223. }
  1224. }
  1225. void
  1226. scan(void) {
  1227. unsigned int i, num;
  1228. Window *wins, d1, d2;
  1229. XWindowAttributes wa;
  1230. wins = NULL;
  1231. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1232. for(i = 0; i < num; i++) {
  1233. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1234. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1235. continue;
  1236. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1237. manage(wins[i], &wa);
  1238. }
  1239. for(i = 0; i < num; i++) { /* now the transients */
  1240. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1241. continue;
  1242. if(XGetTransientForHint(dpy, wins[i], &d1)
  1243. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1244. manage(wins[i], &wa);
  1245. }
  1246. }
  1247. if(wins)
  1248. XFree(wins);
  1249. }
  1250. void
  1251. setclientstate(Client *c, long state) {
  1252. long data[] = {state, None};
  1253. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1254. PropModeReplace, (unsigned char *)data, 2);
  1255. }
  1256. void
  1257. setlayout(const char *arg) {
  1258. static Layout *revert = 0;
  1259. unsigned int i;
  1260. if(!arg)
  1261. return;
  1262. for(i = 0; i < LENGTH(layouts); i++)
  1263. if(!strcmp(arg, layouts[i].symbol))
  1264. break;
  1265. if(i == LENGTH(layouts))
  1266. return;
  1267. if(revert && &layouts[i] == lt)
  1268. lt = revert;
  1269. else {
  1270. revert = lt;
  1271. lt = &layouts[i];
  1272. }
  1273. if(sel)
  1274. arrange();
  1275. else
  1276. drawbar();
  1277. }
  1278. void
  1279. setup(void) {
  1280. unsigned int i;
  1281. XSetWindowAttributes wa;
  1282. /* init screen */
  1283. screen = DefaultScreen(dpy);
  1284. root = RootWindow(dpy, screen);
  1285. sx = 0;
  1286. sy = 0;
  1287. sw = DisplayWidth(dpy, screen);
  1288. sh = DisplayHeight(dpy, screen);
  1289. /* init atoms */
  1290. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1291. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1292. wmatom[WMName] = XInternAtom(dpy, "WM_NAME", False);
  1293. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1294. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1295. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1296. /* init cursors */
  1297. wa.cursor = cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1298. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1299. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1300. /* init appearance */
  1301. dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
  1302. dc.norm[ColBG] = getcolor(NORMBGCOLOR);
  1303. dc.norm[ColFG] = getcolor(NORMFGCOLOR);
  1304. dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
  1305. dc.sel[ColBG] = getcolor(SELBGCOLOR);
  1306. dc.sel[ColFG] = getcolor(SELFGCOLOR);
  1307. initfont(FONT);
  1308. dc.h = bh = dc.font.height + 2;
  1309. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1310. dc.gc = XCreateGC(dpy, root, 0, 0);
  1311. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1312. if(!dc.font.set)
  1313. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1314. /* init tags */
  1315. seltags = emallocz(TAGSZ);
  1316. prevtags = emallocz(TAGSZ);
  1317. seltags[0] = prevtags[0] = True;
  1318. /* init layouts */
  1319. lt = &layouts[0];
  1320. /* init bar */
  1321. for(blw = i = 0; i < LENGTH(layouts); i++) {
  1322. i = textw(layouts[i].symbol);
  1323. if(i > blw)
  1324. blw = i;
  1325. }
  1326. wa.override_redirect = 1;
  1327. wa.background_pixmap = ParentRelative;
  1328. wa.event_mask = ButtonPressMask|ExposureMask;
  1329. barwin = XCreateWindow(dpy, root, BX, BY, BW, bh, 0, DefaultDepth(dpy, screen),
  1330. CopyFromParent, DefaultVisual(dpy, screen),
  1331. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1332. XDefineCursor(dpy, barwin, cursor[CurNormal]);
  1333. XMapRaised(dpy, barwin);
  1334. strcpy(stext, "dwm-"VERSION);
  1335. drawbar();
  1336. /* EWMH support per view */
  1337. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1338. PropModeReplace, (unsigned char *) netatom, NetLast);
  1339. /* select for events */
  1340. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
  1341. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask;
  1342. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1343. XSelectInput(dpy, root, wa.event_mask);
  1344. /* grab keys */
  1345. grabkeys();
  1346. }
  1347. void
  1348. spawn(const char *arg) {
  1349. static char *shell = NULL;
  1350. if(!shell && !(shell = getenv("SHELL")))
  1351. shell = "/bin/sh";
  1352. if(!arg)
  1353. return;
  1354. /* The double-fork construct avoids zombie processes and keeps the code
  1355. * clean from stupid signal handlers. */
  1356. if(fork() == 0) {
  1357. if(fork() == 0) {
  1358. if(dpy)
  1359. close(ConnectionNumber(dpy));
  1360. setsid();
  1361. execl(shell, shell, "-c", arg, (char *)NULL);
  1362. fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
  1363. perror(" failed");
  1364. }
  1365. exit(0);
  1366. }
  1367. wait(0);
  1368. }
  1369. void
  1370. tag(const char *arg) {
  1371. unsigned int i;
  1372. if(!sel)
  1373. return;
  1374. for(i = 0; i < LENGTH(tags); i++)
  1375. sel->tags[i] = (NULL == arg);
  1376. sel->tags[idxoftag(arg)] = True;
  1377. arrange();
  1378. }
  1379. unsigned int
  1380. textnw(const char *text, unsigned int len) {
  1381. XRectangle r;
  1382. if(dc.font.set) {
  1383. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1384. return r.width;
  1385. }
  1386. return XTextWidth(dc.font.xfont, text, len);
  1387. }
  1388. unsigned int
  1389. textw(const char *text) {
  1390. return textnw(text, strlen(text)) + dc.font.height;
  1391. }
  1392. void
  1393. tileresize(Client *c, int x, int y, int w, int h) {
  1394. resize(c, x, y, w, h, RESIZEHINTS);
  1395. if((RESIZEHINTS) && ((c->h < bh) || (c->h > h) || (c->w < bh) || (c->w > w)))
  1396. /* client doesn't accept size constraints */
  1397. resize(c, x, y, w, h, False);
  1398. }
  1399. void
  1400. tileh(void) {
  1401. tilehstack(tilemaster());
  1402. }
  1403. void
  1404. tilehstack(unsigned int n) {
  1405. int i, x, w;
  1406. Client *c;
  1407. if(n == 0)
  1408. return;
  1409. x = TX;
  1410. w = (TW) / n;
  1411. if(w < bh)
  1412. w = TW;
  1413. for(i = 0, c = nexttiled(clients); c; c = nexttiled(c->next), i++)
  1414. if(i > 0) {
  1415. if(i > 1 && i == n) /* remainder */
  1416. tileresize(c, x, TY, ((TX) + (TW)) - x - 2 * c->border,
  1417. TH - 2 * c->border);
  1418. else
  1419. tileresize(c, x, TY, w - 2 * c->border,
  1420. TH - 2 * c->border);
  1421. if(w != TW)
  1422. x = c->x + c->w + 2 * c->border;
  1423. }
  1424. }
  1425. unsigned int
  1426. tilemaster(void) {
  1427. unsigned int n;
  1428. Client *c, *mc;
  1429. for(n = 0, mc = c = nexttiled(clients); c; c = nexttiled(c->next))
  1430. n++;
  1431. if(n == 0)
  1432. return 0;
  1433. if(n == 1)
  1434. tileresize(mc, MOX, MOY, (MOW) - 2 * mc->border, (MOH) - 2 * mc->border);
  1435. else
  1436. tileresize(mc, MX, MY, (MW) - 2 * mc->border, (MH) - 2 * mc->border);
  1437. return n - 1;
  1438. }
  1439. void
  1440. tilev(void) {
  1441. tilevstack(tilemaster());
  1442. }
  1443. void
  1444. tilevstack(unsigned int n) {
  1445. int i, y, h;
  1446. Client *c;
  1447. if(n == 0)
  1448. return;
  1449. y = TY;
  1450. h = (TH) / n;
  1451. if(h < bh)
  1452. h = TH;
  1453. for(i = 0, c = nexttiled(clients); c; c = nexttiled(c->next), i++)
  1454. if(i > 0) {
  1455. if(i > 1 && i == n) /* remainder */
  1456. tileresize(c, TX, y, (TW) - 2 * c->border,
  1457. ((TY) + (TH)) - y - 2 * c->border);
  1458. else
  1459. tileresize(c, TX, y, (TW) - 2 * c->border,
  1460. h - 2 * c->border);
  1461. if(h != TH)
  1462. y = c->y + c->h + 2 * c->border;
  1463. }
  1464. }
  1465. void
  1466. togglefloating(const char *arg) {
  1467. if(!sel)
  1468. return;
  1469. sel->isfloating = !sel->isfloating;
  1470. if(sel->isfloating)
  1471. resize(sel, sel->x, sel->y, sel->w, sel->h, True);
  1472. arrange();
  1473. }
  1474. void
  1475. toggletag(const char *arg) {
  1476. unsigned int i, j;
  1477. if(!sel)
  1478. return;
  1479. i = idxoftag(arg);
  1480. sel->tags[i] = !sel->tags[i];
  1481. for(j = 0; j < LENGTH(tags) && !sel->tags[j]; j++);
  1482. if(j == LENGTH(tags))
  1483. sel->tags[i] = True; /* at least one tag must be enabled */
  1484. arrange();
  1485. }
  1486. void
  1487. toggleview(const char *arg) {
  1488. unsigned int i, j;
  1489. i = idxoftag(arg);
  1490. seltags[i] = !seltags[i];
  1491. for(j = 0; j < LENGTH(tags) && !seltags[j]; j++);
  1492. if(j == LENGTH(tags))
  1493. seltags[i] = True; /* at least one tag must be viewed */
  1494. arrange();
  1495. }
  1496. void
  1497. unban(Client *c) {
  1498. if(!c->isbanned)
  1499. return;
  1500. XMoveWindow(dpy, c->win, c->x, c->y);
  1501. c->isbanned = False;
  1502. }
  1503. void
  1504. unmanage(Client *c) {
  1505. XWindowChanges wc;
  1506. wc.border_width = c->oldborder;
  1507. /* The server grab construct avoids race conditions. */
  1508. XGrabServer(dpy);
  1509. XSetErrorHandler(xerrordummy);
  1510. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1511. detach(c);
  1512. detachstack(c);
  1513. if(sel == c)
  1514. focus(NULL);
  1515. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1516. setclientstate(c, WithdrawnState);
  1517. free(c->tags);
  1518. free(c);
  1519. XSync(dpy, False);
  1520. XSetErrorHandler(xerror);
  1521. XUngrabServer(dpy);
  1522. arrange();
  1523. }
  1524. void
  1525. unmapnotify(XEvent *e) {
  1526. Client *c;
  1527. XUnmapEvent *ev = &e->xunmap;
  1528. if((c = getclient(ev->window)))
  1529. unmanage(c);
  1530. }
  1531. void
  1532. updatesizehints(Client *c) {
  1533. long msize;
  1534. XSizeHints size;
  1535. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  1536. size.flags = PSize;
  1537. c->flags = size.flags;
  1538. if(c->flags & PBaseSize) {
  1539. c->basew = size.base_width;
  1540. c->baseh = size.base_height;
  1541. }
  1542. else if(c->flags & PMinSize) {
  1543. c->basew = size.min_width;
  1544. c->baseh = size.min_height;
  1545. }
  1546. else
  1547. c->basew = c->baseh = 0;
  1548. if(c->flags & PResizeInc) {
  1549. c->incw = size.width_inc;
  1550. c->inch = size.height_inc;
  1551. }
  1552. else
  1553. c->incw = c->inch = 0;
  1554. if(c->flags & PMaxSize) {
  1555. c->maxw = size.max_width;
  1556. c->maxh = size.max_height;
  1557. }
  1558. else
  1559. c->maxw = c->maxh = 0;
  1560. if(c->flags & PMinSize) {
  1561. c->minw = size.min_width;
  1562. c->minh = size.min_height;
  1563. }
  1564. else if(c->flags & PBaseSize) {
  1565. c->minw = size.base_width;
  1566. c->minh = size.base_height;
  1567. }
  1568. else
  1569. c->minw = c->minh = 0;
  1570. if(c->flags & PAspect) {
  1571. c->minax = size.min_aspect.x;
  1572. c->maxax = size.max_aspect.x;
  1573. c->minay = size.min_aspect.y;
  1574. c->maxay = size.max_aspect.y;
  1575. }
  1576. else
  1577. c->minax = c->maxax = c->minay = c->maxay = 0;
  1578. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1579. && c->maxw == c->minw && c->maxh == c->minh);
  1580. }
  1581. void
  1582. updatetitle(Client *c) {
  1583. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1584. gettextprop(c->win, wmatom[WMName], c->name, sizeof c->name);
  1585. }
  1586. void
  1587. updatewmhints(Client *c) {
  1588. XWMHints *wmh;
  1589. if((wmh = XGetWMHints(dpy, c->win))) {
  1590. if(c == sel)
  1591. sel->isurgent = False;
  1592. else
  1593. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1594. XFree(wmh);
  1595. }
  1596. }
  1597. void
  1598. view(const char *arg) {
  1599. unsigned int i;
  1600. for(i = 0; i < LENGTH(tags); i++)
  1601. tmp[i] = (NULL == arg);
  1602. tmp[idxoftag(arg)] = True;
  1603. if(memcmp(seltags, tmp, TAGSZ) != 0) {
  1604. memcpy(prevtags, seltags, TAGSZ);
  1605. memcpy(seltags, tmp, TAGSZ);
  1606. arrange();
  1607. }
  1608. }
  1609. void
  1610. viewprevtag(const char *arg) {
  1611. memcpy(tmp, seltags, TAGSZ);
  1612. memcpy(seltags, prevtags, TAGSZ);
  1613. memcpy(prevtags, tmp, TAGSZ);
  1614. arrange();
  1615. }
  1616. /* There's no way to check accesses to destroyed windows, thus those cases are
  1617. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1618. * default error handler, which may call exit. */
  1619. int
  1620. xerror(Display *dpy, XErrorEvent *ee) {
  1621. if(ee->error_code == BadWindow
  1622. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1623. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1624. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1625. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1626. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1627. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1628. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1629. return 0;
  1630. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1631. ee->request_code, ee->error_code);
  1632. return xerrorxlib(dpy, ee); /* may call exit */
  1633. }
  1634. int
  1635. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1636. return 0;
  1637. }
  1638. /* Startup Error handler to check if another window manager
  1639. * is already running. */
  1640. int
  1641. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1642. otherwm = True;
  1643. return -1;
  1644. }
  1645. void
  1646. zoom(const char *arg) {
  1647. Client *c = sel;
  1648. if(!sel || lt->isfloating || sel->isfloating)
  1649. return;
  1650. if(c == nexttiled(clients))
  1651. if(!(c = nexttiled(c->next)))
  1652. return;
  1653. detach(c);
  1654. attach(c);
  1655. focus(c);
  1656. arrange();
  1657. }
  1658. int
  1659. main(int argc, char *argv[]) {
  1660. if(argc == 2 && !strcmp("-v", argv[1]))
  1661. eprint("dwm-"VERSION", © 2006-2008 dwm engineers, see LICENSE for details\n");
  1662. else if(argc != 1)
  1663. eprint("usage: dwm [-v]\n");
  1664. setlocale(LC_CTYPE, "");
  1665. if(!(dpy = XOpenDisplay(0)))
  1666. eprint("dwm: cannot open display\n");
  1667. checkotherwm();
  1668. setup();
  1669. scan();
  1670. run();
  1671. cleanup();
  1672. XCloseDisplay(dpy);
  1673. return 0;
  1674. }