dwm.c 42 KB

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