You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1911 lines
43 KiB

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