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.

1939 lines
47 KiB

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