Configuration file for DWM on MacBook Air
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.

1876 lines
44 KiB

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