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.

1842 lines
43 KiB

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