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.

1914 lines
44 KiB

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