Configuration of dwm for Mac Computers
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.

1976 lines
47 KiB

17 years ago
16 years ago
15 years ago
15 years ago
16 years ago
15 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
16 years ago
15 years ago
16 years ago
16 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
15 years ago
16 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
16 years ago
16 years ago
16 years ago
15 years ago
16 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. }
  622. if((dc.w = dc.x - x) > bh) {
  623. dc.x = x;
  624. if(m->sel) {
  625. col = m == selmon ? dc.sel : dc.norm;
  626. drawtext(m->sel->name, col, False);
  627. drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
  628. }
  629. else
  630. drawtext(NULL, dc.norm, False);
  631. }
  632. XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
  633. XSync(dpy, False);
  634. }
  635. void
  636. drawbars(void) {
  637. Monitor *m;
  638. for(m = mons; m; m = m->next)
  639. drawbar(m);
  640. }
  641. void
  642. drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
  643. int x;
  644. XGCValues gcv;
  645. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  646. gcv.foreground = col[invert ? ColBG : ColFG];
  647. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  648. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  649. r.x = dc.x + 1;
  650. r.y = dc.y + 1;
  651. if(filled) {
  652. r.width = r.height = x + 1;
  653. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  654. }
  655. else if(empty) {
  656. r.width = r.height = x;
  657. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  658. }
  659. }
  660. void
  661. drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
  662. char buf[256];
  663. int i, x, y, h, len, olen;
  664. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  665. XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
  666. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  667. if(!text)
  668. return;
  669. olen = strlen(text);
  670. h = dc.font.ascent + dc.font.descent;
  671. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  672. x = dc.x + (h / 2);
  673. /* shorten text if necessary */
  674. for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
  675. if(!len)
  676. return;
  677. memcpy(buf, text, len);
  678. if(len < olen)
  679. for(i = len; i && i > len - 3; buf[--i] = '.');
  680. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  681. if(dc.font.set)
  682. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  683. else
  684. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  685. }
  686. void
  687. enternotify(XEvent *e) {
  688. Client *c;
  689. Monitor *m;
  690. XCrossingEvent *ev = &e->xcrossing;
  691. if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  692. return;
  693. if((m = wintomon(ev->window)) && m != selmon) {
  694. unfocus(selmon->sel);
  695. selmon = m;
  696. }
  697. if((c = wintoclient(ev->window)))
  698. focus(c);
  699. else
  700. focus(NULL);
  701. }
  702. void
  703. expose(XEvent *e) {
  704. Monitor *m;
  705. XExposeEvent *ev = &e->xexpose;
  706. if(ev->count == 0 && (m = wintomon(ev->window)))
  707. drawbar(m);
  708. }
  709. void
  710. focus(Client *c) {
  711. if(!c || !ISVISIBLE(c))
  712. for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
  713. if(selmon->sel)
  714. unfocus(selmon->sel);
  715. if(c) {
  716. if(c->mon != selmon)
  717. selmon = c->mon;
  718. if(c->isurgent)
  719. clearurgent(c);
  720. detachstack(c);
  721. attachstack(c);
  722. grabbuttons(c, True);
  723. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  724. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  725. }
  726. else
  727. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  728. selmon->sel = c;
  729. drawbars();
  730. }
  731. void
  732. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  733. XFocusChangeEvent *ev = &e->xfocus;
  734. if(selmon->sel && ev->window != selmon->sel->win)
  735. XSetInputFocus(dpy, selmon->sel->win, RevertToPointerRoot, CurrentTime);
  736. }
  737. void
  738. focusmon(const Arg *arg) {
  739. Monitor *m;
  740. if(!(m = idxtomon(arg->ui)) || m == selmon)
  741. return;
  742. unfocus(selmon->sel);
  743. selmon = m;
  744. focus(NULL);
  745. }
  746. void
  747. focusstack(const Arg *arg) {
  748. Client *c = NULL, *i;
  749. if(!selmon->sel)
  750. return;
  751. if(arg->i > 0) {
  752. for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
  753. if(!c)
  754. for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
  755. }
  756. else {
  757. for(i = selmon->clients; i != selmon->sel; i = i->next)
  758. if(ISVISIBLE(i))
  759. c = i;
  760. if(!c)
  761. for(; i; i = i->next)
  762. if(ISVISIBLE(i))
  763. c = i;
  764. }
  765. if(c) {
  766. focus(c);
  767. restack(selmon);
  768. }
  769. }
  770. unsigned long
  771. getcolor(const char *colstr) {
  772. Colormap cmap = DefaultColormap(dpy, screen);
  773. XColor color;
  774. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  775. die("error, cannot allocate color '%s'\n", colstr);
  776. return color.pixel;
  777. }
  778. Bool
  779. getrootpointer(int *x, int *y) {
  780. int di;
  781. unsigned int dui;
  782. Window dummy;
  783. return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
  784. }
  785. long
  786. getstate(Window w) {
  787. int format, status;
  788. long result = -1;
  789. unsigned char *p = NULL;
  790. unsigned long n, extra;
  791. Atom real;
  792. status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  793. &real, &format, &n, &extra, (unsigned char **)&p);
  794. if(status != Success)
  795. return -1;
  796. if(n != 0)
  797. result = *p;
  798. XFree(p);
  799. return result;
  800. }
  801. Bool
  802. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  803. char **list = NULL;
  804. int n;
  805. XTextProperty name;
  806. if(!text || size == 0)
  807. return False;
  808. text[0] = '\0';
  809. XGetTextProperty(dpy, w, &name, atom);
  810. if(!name.nitems)
  811. return False;
  812. if(name.encoding == XA_STRING)
  813. strncpy(text, (char *)name.value, size - 1);
  814. else {
  815. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  816. && n > 0 && *list) {
  817. strncpy(text, *list, size - 1);
  818. XFreeStringList(list);
  819. }
  820. }
  821. text[size - 1] = '\0';
  822. XFree(name.value);
  823. return True;
  824. }
  825. void
  826. grabbuttons(Client *c, Bool focused) {
  827. updatenumlockmask();
  828. {
  829. unsigned int i, j;
  830. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  831. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  832. if(focused) {
  833. for(i = 0; i < LENGTH(buttons); i++)
  834. if(buttons[i].click == ClkClientWin)
  835. for(j = 0; j < LENGTH(modifiers); j++)
  836. XGrabButton(dpy, buttons[i].button,
  837. buttons[i].mask | modifiers[j],
  838. c->win, False, BUTTONMASK,
  839. GrabModeAsync, GrabModeSync, None, None);
  840. } else
  841. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  842. BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
  843. }
  844. }
  845. void
  846. grabkeys(void) {
  847. updatenumlockmask();
  848. { /* grab keys */
  849. unsigned int i, j;
  850. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  851. KeyCode code;
  852. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  853. for(i = 0; i < LENGTH(keys); i++) {
  854. if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
  855. for(j = 0; j < LENGTH(modifiers); j++)
  856. XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
  857. True, GrabModeAsync, GrabModeAsync);
  858. }
  859. }
  860. }
  861. Monitor *
  862. idxtomon(unsigned int n) {
  863. unsigned int i;
  864. Monitor *m;
  865. for(m = mons, i = 0; m && i != n; m = m->next, i++);
  866. return m;
  867. }
  868. void
  869. initfont(const char *fontstr) {
  870. char *def, **missing;
  871. int i, n;
  872. missing = NULL;
  873. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  874. if(missing) {
  875. while(n--)
  876. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  877. XFreeStringList(missing);
  878. }
  879. if(dc.font.set) {
  880. XFontSetExtents *font_extents;
  881. XFontStruct **xfonts;
  882. char **font_names;
  883. dc.font.ascent = dc.font.descent = 0;
  884. font_extents = XExtentsOfFontSet(dc.font.set);
  885. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  886. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  887. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  888. dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
  889. xfonts++;
  890. }
  891. }
  892. else {
  893. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  894. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  895. die("error, cannot load font: '%s'\n", fontstr);
  896. dc.font.ascent = dc.font.xfont->ascent;
  897. dc.font.descent = dc.font.xfont->descent;
  898. }
  899. dc.font.height = dc.font.ascent + dc.font.descent;
  900. }
  901. Bool
  902. isprotodel(Client *c) {
  903. int i, n;
  904. Atom *protocols;
  905. Bool ret = False;
  906. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  907. for(i = 0; !ret && i < n; i++)
  908. if(protocols[i] == wmatom[WMDelete])
  909. ret = True;
  910. XFree(protocols);
  911. }
  912. return ret;
  913. }
  914. void
  915. keypress(XEvent *e) {
  916. unsigned int i;
  917. KeySym keysym;
  918. XKeyEvent *ev;
  919. ev = &e->xkey;
  920. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  921. for(i = 0; i < LENGTH(keys); i++)
  922. if(keysym == keys[i].keysym
  923. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  924. && keys[i].func)
  925. keys[i].func(&(keys[i].arg));
  926. }
  927. void
  928. killclient(const Arg *arg) {
  929. XEvent ev;
  930. if(!selmon->sel)
  931. return;
  932. if(isprotodel(selmon->sel)) {
  933. ev.type = ClientMessage;
  934. ev.xclient.window = selmon->sel->win;
  935. ev.xclient.message_type = wmatom[WMProtocols];
  936. ev.xclient.format = 32;
  937. ev.xclient.data.l[0] = wmatom[WMDelete];
  938. ev.xclient.data.l[1] = CurrentTime;
  939. XSendEvent(dpy, selmon->sel->win, False, NoEventMask, &ev);
  940. }
  941. else
  942. XKillClient(dpy, selmon->sel->win);
  943. }
  944. void
  945. manage(Window w, XWindowAttributes *wa) {
  946. static Client cz;
  947. Client *c, *t = NULL;
  948. Window trans = None;
  949. XWindowChanges wc;
  950. if(!(c = malloc(sizeof(Client))))
  951. die("fatal: could not malloc() %u bytes\n", sizeof(Client));
  952. *c = cz;
  953. c->win = w;
  954. if(XGetTransientForHint(dpy, w, &trans))
  955. t = wintoclient(trans);
  956. if(t) {
  957. c->mon = t->mon;
  958. c->tags = t->tags;
  959. }
  960. else {
  961. c->mon = selmon;
  962. applyrules(c);
  963. }
  964. /* geometry */
  965. c->x = wa->x + c->mon->wx;
  966. c->y = wa->y + c->mon->wy;
  967. c->w = wa->width;
  968. c->h = wa->height;
  969. c->oldbw = wa->border_width;
  970. if(c->w == c->mon->mw && c->h == c->mon->mh) {
  971. c->x = c->mon->mx;
  972. c->y = c->mon->my;
  973. c->bw = 0;
  974. }
  975. else {
  976. if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
  977. c->x = c->mon->mx + c->mon->mw - WIDTH(c);
  978. if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
  979. c->y = c->mon->my + c->mon->mh - HEIGHT(c);
  980. c->x = MAX(c->x, c->mon->mx);
  981. /* only fix client y-offset, if the client center might cover the bar */
  982. c->y = MAX(c->y, ((c->mon->by == 0) && (c->x + (c->w / 2) >= c->mon->wx)
  983. && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
  984. c->bw = borderpx;
  985. }
  986. wc.border_width = c->bw;
  987. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  988. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  989. configure(c); /* propagates border_width, if size doesn't change */
  990. updatesizehints(c);
  991. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  992. grabbuttons(c, False);
  993. updatetitle(c);
  994. if(!c->isfloating)
  995. c->isfloating = trans != None || c->isfixed;
  996. if(c->isfloating)
  997. XRaiseWindow(dpy, c->win);
  998. attach(c);
  999. attachstack(c);
  1000. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  1001. XMapWindow(dpy, c->win);
  1002. setclientstate(c, NormalState);
  1003. arrange();
  1004. }
  1005. void
  1006. mappingnotify(XEvent *e) {
  1007. XMappingEvent *ev = &e->xmapping;
  1008. XRefreshKeyboardMapping(ev);
  1009. if(ev->request == MappingKeyboard)
  1010. grabkeys();
  1011. }
  1012. void
  1013. maprequest(XEvent *e) {
  1014. static XWindowAttributes wa;
  1015. XMapRequestEvent *ev = &e->xmaprequest;
  1016. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  1017. return;
  1018. if(wa.override_redirect)
  1019. return;
  1020. if(!wintoclient(ev->window))
  1021. manage(ev->window, &wa);
  1022. }
  1023. void
  1024. monocle(Monitor *m) {
  1025. Client *c;
  1026. for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
  1027. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
  1028. }
  1029. void
  1030. movemouse(const Arg *arg) {
  1031. int x, y, ocx, ocy, nx, ny;
  1032. Client *c;
  1033. Monitor *m;
  1034. XEvent ev;
  1035. if(!(c = selmon->sel))
  1036. return;
  1037. restack(selmon);
  1038. ocx = c->x;
  1039. ocy = c->y;
  1040. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1041. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1042. return;
  1043. if(!getrootpointer(&x, &y))
  1044. return;
  1045. do {
  1046. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1047. switch (ev.type) {
  1048. case ConfigureRequest:
  1049. case Expose:
  1050. case MapRequest:
  1051. handler[ev.type](&ev);
  1052. break;
  1053. case MotionNotify:
  1054. nx = ocx + (ev.xmotion.x - x);
  1055. ny = ocy + (ev.xmotion.y - y);
  1056. if(snap && nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1057. && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1058. if(abs(selmon->wx - nx) < snap)
  1059. nx = selmon->wx;
  1060. else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1061. nx = selmon->wx + selmon->ww - WIDTH(c);
  1062. if(abs(selmon->wy - ny) < snap)
  1063. ny = selmon->wy;
  1064. else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1065. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1066. if(!c->isfloating && lt[selmon->sellt]->arrange
  1067. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1068. togglefloating(NULL);
  1069. }
  1070. if(!lt[selmon->sellt]->arrange || c->isfloating)
  1071. resize(c, nx, ny, c->w, c->h, True);
  1072. break;
  1073. }
  1074. }
  1075. while(ev.type != ButtonRelease);
  1076. XUngrabPointer(dpy, CurrentTime);
  1077. if((m = pointertomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
  1078. sendmon(c, m);
  1079. selmon = m;
  1080. focus(NULL);
  1081. }
  1082. }
  1083. Client *
  1084. nexttiled(Client *c) {
  1085. for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  1086. return c;
  1087. }
  1088. Monitor *
  1089. pointertomon(int x, int y) {
  1090. Monitor *m;
  1091. for(m = mons; m; m = m->next)
  1092. if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh))
  1093. return m;
  1094. return selmon;
  1095. }
  1096. void
  1097. propertynotify(XEvent *e) {
  1098. Client *c;
  1099. Window trans;
  1100. XPropertyEvent *ev = &e->xproperty;
  1101. if((ev->window == root) && (ev->atom == XA_WM_NAME))
  1102. updatestatus();
  1103. else if(ev->state == PropertyDelete)
  1104. return; /* ignore */
  1105. else if((c = wintoclient(ev->window))) {
  1106. switch (ev->atom) {
  1107. default: break;
  1108. case XA_WM_TRANSIENT_FOR:
  1109. XGetTransientForHint(dpy, c->win, &trans);
  1110. if(!c->isfloating && (c->isfloating = (wintoclient(trans) != NULL)))
  1111. arrange();
  1112. break;
  1113. case XA_WM_NORMAL_HINTS:
  1114. updatesizehints(c);
  1115. break;
  1116. case XA_WM_HINTS:
  1117. updatewmhints(c);
  1118. drawbars();
  1119. break;
  1120. }
  1121. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1122. updatetitle(c);
  1123. if(c == selmon->sel)
  1124. drawbars();
  1125. }
  1126. }
  1127. }
  1128. void
  1129. quit(const Arg *arg) {
  1130. running = False;
  1131. }
  1132. void
  1133. resize(Client *c, int x, int y, int w, int h, Bool interact) {
  1134. XWindowChanges wc;
  1135. if(applysizehints(c, &x, &y, &w, &h, interact)) {
  1136. c->x = wc.x = x;
  1137. c->y = wc.y = y;
  1138. c->w = wc.width = w;
  1139. c->h = wc.height = h;
  1140. wc.border_width = c->bw;
  1141. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1142. configure(c);
  1143. XSync(dpy, False);
  1144. }
  1145. }
  1146. void
  1147. resizemouse(const Arg *arg) {
  1148. int ocx, ocy;
  1149. int nw, nh;
  1150. Client *c;
  1151. Monitor *m;
  1152. XEvent ev;
  1153. if(!(c = selmon->sel))
  1154. return;
  1155. restack(selmon);
  1156. ocx = c->x;
  1157. ocy = c->y;
  1158. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1159. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1160. return;
  1161. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1162. do {
  1163. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1164. switch(ev.type) {
  1165. case ConfigureRequest:
  1166. case Expose:
  1167. case MapRequest:
  1168. handler[ev.type](&ev);
  1169. break;
  1170. case MotionNotify:
  1171. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1172. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1173. if(snap && nw >= selmon->wx && nw <= selmon->wx + selmon->ww
  1174. && nh >= selmon->wy && nh <= selmon->wy + selmon->wh) {
  1175. if(!c->isfloating && lt[selmon->sellt]->arrange
  1176. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1177. togglefloating(NULL);
  1178. }
  1179. if(!lt[selmon->sellt]->arrange || c->isfloating)
  1180. resize(c, c->x, c->y, nw, nh, True);
  1181. break;
  1182. }
  1183. }
  1184. while(ev.type != ButtonRelease);
  1185. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1186. XUngrabPointer(dpy, CurrentTime);
  1187. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1188. if((m = pointertomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
  1189. sendmon(c, m);
  1190. selmon = m;
  1191. focus(NULL);
  1192. }
  1193. }
  1194. void
  1195. restack(Monitor *m) {
  1196. Client *c;
  1197. XEvent ev;
  1198. XWindowChanges wc;
  1199. drawbars();
  1200. if(!m->sel)
  1201. return;
  1202. if(m->sel->isfloating || !lt[m->sellt]->arrange)
  1203. XRaiseWindow(dpy, m->sel->win);
  1204. if(lt[m->sellt]->arrange) {
  1205. wc.stack_mode = Below;
  1206. wc.sibling = m->barwin;
  1207. for(c = m->stack; c; c = c->snext)
  1208. if(!c->isfloating && ISVISIBLE(c)) {
  1209. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1210. wc.sibling = c->win;
  1211. }
  1212. }
  1213. XSync(dpy, False);
  1214. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1215. }
  1216. void
  1217. run(void) {
  1218. XEvent ev;
  1219. /* main event loop */
  1220. XSync(dpy, False);
  1221. while(running && !XNextEvent(dpy, &ev)) {
  1222. if(handler[ev.type])
  1223. (handler[ev.type])(&ev); /* call handler */
  1224. }
  1225. }
  1226. void
  1227. scan(void) {
  1228. unsigned int i, num;
  1229. Window d1, d2, *wins = NULL;
  1230. XWindowAttributes wa;
  1231. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1232. for(i = 0; i < num; i++) {
  1233. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1234. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1235. continue;
  1236. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1237. manage(wins[i], &wa);
  1238. }
  1239. for(i = 0; i < num; i++) { /* now the transients */
  1240. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1241. continue;
  1242. if(XGetTransientForHint(dpy, wins[i], &d1)
  1243. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1244. manage(wins[i], &wa);
  1245. }
  1246. if(wins)
  1247. XFree(wins);
  1248. }
  1249. }
  1250. void
  1251. sendmon(Client *c, Monitor *m) {
  1252. if(c->mon == m)
  1253. return;
  1254. unfocus(c);
  1255. detach(c);
  1256. detachstack(c);
  1257. c->mon = m;
  1258. c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
  1259. attach(c);
  1260. attachstack(c);
  1261. focus(NULL);
  1262. arrange();
  1263. }
  1264. void
  1265. setclientstate(Client *c, long state) {
  1266. long data[] = {state, None};
  1267. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1268. PropModeReplace, (unsigned char *)data, 2);
  1269. }
  1270. void
  1271. setlayout(const Arg *arg) {
  1272. if(!arg || !arg->v || arg->v != lt[selmon->sellt])
  1273. selmon->sellt ^= 1;
  1274. if(arg && arg->v)
  1275. lt[selmon->sellt] = (Layout *)arg->v;
  1276. if(selmon->sel)
  1277. arrange();
  1278. else
  1279. drawbars();
  1280. }
  1281. /* arg > 1.0 will set mfact absolutly */
  1282. void
  1283. setmfact(const Arg *arg) {
  1284. float f;
  1285. if(!arg || !lt[selmon->sellt]->arrange)
  1286. return;
  1287. f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  1288. if(f < 0.1 || f > 0.9)
  1289. return;
  1290. selmon->mfact = f;
  1291. arrange();
  1292. }
  1293. void
  1294. setup(void) {
  1295. unsigned int i;
  1296. int w;
  1297. XSetWindowAttributes wa;
  1298. /* init screen */
  1299. screen = DefaultScreen(dpy);
  1300. root = RootWindow(dpy, screen);
  1301. initfont(font);
  1302. sw = DisplayWidth(dpy, screen);
  1303. sh = DisplayHeight(dpy, screen);
  1304. bh = dc.h = dc.font.height + 2;
  1305. lt[0] = &layouts[0];
  1306. lt[1] = &layouts[1 % LENGTH(layouts)];
  1307. updategeom();
  1308. /* init atoms */
  1309. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1310. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1311. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1312. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1313. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1314. /* init cursors */
  1315. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1316. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1317. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1318. /* init appearance */
  1319. dc.norm[ColBorder] = getcolor(normbordercolor);
  1320. dc.norm[ColBG] = getcolor(normbgcolor);
  1321. dc.norm[ColFG] = getcolor(normfgcolor);
  1322. dc.sel[ColBorder] = getcolor(selbordercolor);
  1323. dc.sel[ColBG] = getcolor(selbgcolor);
  1324. dc.sel[ColFG] = getcolor(selfgcolor);
  1325. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1326. dc.gc = XCreateGC(dpy, root, 0, NULL);
  1327. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1328. if(!dc.font.set)
  1329. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1330. /* init bars */
  1331. for(blw = i = 0; LENGTH(layouts) > 1 && i < LENGTH(layouts); i++) {
  1332. w = TEXTW(layouts[i].symbol);
  1333. blw = MAX(blw, w);
  1334. }
  1335. updatebars();
  1336. updatestatus();
  1337. /* EWMH support per view */
  1338. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1339. PropModeReplace, (unsigned char *) netatom, NetLast);
  1340. /* select for events */
  1341. wa.cursor = cursor[CurNormal];
  1342. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
  1343. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
  1344. |PropertyChangeMask;
  1345. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1346. XSelectInput(dpy, root, wa.event_mask);
  1347. grabkeys();
  1348. }
  1349. void
  1350. showhide(Client *c) {
  1351. if(!c)
  1352. return;
  1353. if(ISVISIBLE(c)) { /* show clients top down */
  1354. XMoveWindow(dpy, c->win, c->x, c->y);
  1355. if(!lt[c->mon->sellt]->arrange || c->isfloating)
  1356. resize(c, c->x, c->y, c->w, c->h, False);
  1357. showhide(c->snext);
  1358. }
  1359. else { /* hide clients bottom up */
  1360. showhide(c->snext);
  1361. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  1362. }
  1363. }
  1364. void
  1365. sigchld(int signal) {
  1366. while(0 < waitpid(-1, NULL, WNOHANG));
  1367. }
  1368. void
  1369. spawn(const Arg *arg) {
  1370. signal(SIGCHLD, sigchld);
  1371. if(fork() == 0) {
  1372. if(dpy)
  1373. close(ConnectionNumber(dpy));
  1374. setsid();
  1375. execvp(((char **)arg->v)[0], (char **)arg->v);
  1376. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1377. perror(" failed");
  1378. exit(0);
  1379. }
  1380. }
  1381. void
  1382. tag(const Arg *arg) {
  1383. if(selmon->sel && arg->ui & TAGMASK) {
  1384. selmon->sel->tags = arg->ui & TAGMASK;
  1385. arrange();
  1386. }
  1387. }
  1388. void
  1389. tagmon(const Arg *arg) {
  1390. Monitor *m;
  1391. if(!selmon->sel || !(m = idxtomon(arg->ui)))
  1392. return;
  1393. sendmon(selmon->sel, m);
  1394. }
  1395. int
  1396. textnw(const char *text, unsigned int len) {
  1397. XRectangle r;
  1398. if(dc.font.set) {
  1399. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1400. return r.width;
  1401. }
  1402. return XTextWidth(dc.font.xfont, text, len);
  1403. }
  1404. void
  1405. tile(Monitor *m) {
  1406. int x, y, h, w, mw;
  1407. unsigned int i, n;
  1408. Client *c;
  1409. for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  1410. if(n == 0)
  1411. return;
  1412. /* master */
  1413. c = nexttiled(m->clients);
  1414. mw = m->mfact * m->ww;
  1415. resize(c, m->wx, m->wy, (n == 1 ? m->ww : mw) - 2 * c->bw, m->wh - 2 * c->bw, False);
  1416. if(--n == 0)
  1417. return;
  1418. /* tile stack */
  1419. x = (m->wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : m->wx + mw;
  1420. y = m->wy;
  1421. w = (m->wx + mw > c->x + c->w) ? m->wx + m->ww - x : m->ww - mw;
  1422. h = m->wh / n;
  1423. if(h < bh)
  1424. h = m->wh;
  1425. for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
  1426. resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
  1427. ? m->wy + m->wh - y - 2 * c->bw : h - 2 * c->bw), False);
  1428. if(h != m->wh)
  1429. y = c->y + HEIGHT(c);
  1430. }
  1431. }
  1432. void
  1433. togglebar(const Arg *arg) {
  1434. selmon->showbar = !selmon->showbar;
  1435. updatebarpos(selmon);
  1436. XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  1437. arrange();
  1438. }
  1439. void
  1440. togglefloating(const Arg *arg) {
  1441. if(!selmon->sel)
  1442. return;
  1443. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  1444. if(selmon->sel->isfloating)
  1445. resize(selmon->sel, selmon->sel->x, selmon->sel->y,
  1446. selmon->sel->w, selmon->sel->h, False);
  1447. arrange();
  1448. }
  1449. void
  1450. toggletag(const Arg *arg) {
  1451. unsigned int mask;
  1452. if(!selmon->sel)
  1453. return;
  1454. mask = selmon->sel->tags ^ (arg->ui & TAGMASK);
  1455. if(mask) {
  1456. selmon->sel->tags = mask;
  1457. arrange();
  1458. }
  1459. }
  1460. void
  1461. toggleview(const Arg *arg) {
  1462. unsigned int mask = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  1463. if(mask) {
  1464. selmon->tagset[selmon->seltags] = mask;
  1465. arrange();
  1466. }
  1467. }
  1468. void
  1469. unfocus(Client *c) {
  1470. if(!c)
  1471. return;
  1472. grabbuttons(c, False);
  1473. XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
  1474. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  1475. }
  1476. void
  1477. unmanage(Client *c) {
  1478. XWindowChanges wc;
  1479. wc.border_width = c->oldbw;
  1480. /* The server grab construct avoids race conditions. */
  1481. XGrabServer(dpy);
  1482. XSetErrorHandler(xerrordummy);
  1483. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1484. detach(c);
  1485. detachstack(c);
  1486. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1487. setclientstate(c, WithdrawnState);
  1488. free(c);
  1489. XSync(dpy, False);
  1490. XSetErrorHandler(xerror);
  1491. XUngrabServer(dpy);
  1492. focus(NULL);
  1493. arrange();
  1494. }
  1495. void
  1496. unmapnotify(XEvent *e) {
  1497. Client *c;
  1498. XUnmapEvent *ev = &e->xunmap;
  1499. if((c = wintoclient(ev->window)))
  1500. unmanage(c);
  1501. }
  1502. void
  1503. updatebars(void) {
  1504. Monitor *m;
  1505. XSetWindowAttributes wa;
  1506. wa.override_redirect = True;
  1507. wa.background_pixmap = ParentRelative;
  1508. wa.event_mask = ButtonPressMask|ExposureMask;
  1509. for(m = mons; m; m = m->next) {
  1510. m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
  1511. CopyFromParent, DefaultVisual(dpy, screen),
  1512. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1513. XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
  1514. XMapRaised(dpy, m->barwin);
  1515. }
  1516. }
  1517. void
  1518. updatebarpos(Monitor *m) {
  1519. m->wy = m->my;
  1520. m->wh = m->mh;
  1521. if(m->showbar) {
  1522. m->wh -= bh;
  1523. m->by = m->topbar ? m->wy : m->wy + m->wh;
  1524. m->wy = m->topbar ? m->wy + bh : m->wy;
  1525. }
  1526. else
  1527. m->by = -bh;
  1528. }
  1529. void
  1530. updategeom(void) {
  1531. int i, n = 1;
  1532. Client *c;
  1533. Monitor *newmons = NULL, *m, *tm;
  1534. #ifdef XINERAMA
  1535. XineramaScreenInfo *info = NULL;
  1536. if(XineramaIsActive(dpy))
  1537. info = XineramaQueryScreens(dpy, &n);
  1538. #endif /* XINERAMA */
  1539. /* allocate monitor(s) for the new geometry setup */
  1540. for(i = 0; i < n; i++) {
  1541. m = (Monitor *)malloc(sizeof(Monitor));
  1542. m->next = newmons;
  1543. newmons = m;
  1544. }
  1545. /* initialise monitor(s) */
  1546. #ifdef XINERAMA
  1547. if(XineramaIsActive(dpy)) {
  1548. for(i = 0, m = newmons; m; m = m->next, i++) {
  1549. m->screen_number = info[i].screen_number;
  1550. m->mx = m->wx = info[i].x_org;
  1551. m->my = m->wy = info[i].y_org;
  1552. m->mw = m->ww = info[i].width;
  1553. m->mh = m->wh = info[i].height;
  1554. }
  1555. XFree(info);
  1556. }
  1557. else
  1558. #endif /* XINERAMA */
  1559. /* default monitor setup */
  1560. {
  1561. m->screen_number = 0;
  1562. m->mx = m->wx = 0;
  1563. m->my = m->wy = 0;
  1564. m->mw = m->ww = sw;
  1565. m->mh = m->wh = sh;
  1566. }
  1567. /* bar geometry setup */
  1568. for(m = newmons; m; m = m->next) {
  1569. m->sel = m->stack = m->clients = NULL;
  1570. m->seltags = 0;
  1571. m->sellt = 0;
  1572. m->tagset[0] = m->tagset[1] = 1;
  1573. m->mfact = mfact;
  1574. m->showbar = SHOWBAR;
  1575. m->topbar = TOPBAR;
  1576. updatebarpos(m);
  1577. }
  1578. /* reassign left over clients of disappeared monitors */
  1579. for(tm = mons; tm; tm = tm->next)
  1580. while(tm->clients) {
  1581. c = tm->clients;
  1582. tm->clients = c->next;
  1583. detachstack(c);
  1584. c->mon = newmons;
  1585. attach(c);
  1586. attachstack(c);
  1587. }
  1588. /* select focused monitor */
  1589. cleanupmons();
  1590. selmon = mons = newmons;
  1591. selmon = wintomon(root);
  1592. }
  1593. void
  1594. updatenumlockmask(void) {
  1595. unsigned int i, j;
  1596. XModifierKeymap *modmap;
  1597. numlockmask = 0;
  1598. modmap = XGetModifierMapping(dpy);
  1599. for(i = 0; i < 8; i++)
  1600. for(j = 0; j < modmap->max_keypermod; j++)
  1601. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1602. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1603. numlockmask = (1 << i);
  1604. XFreeModifiermap(modmap);
  1605. }
  1606. void
  1607. updatesizehints(Client *c) {
  1608. long msize;
  1609. XSizeHints size;
  1610. if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
  1611. /* size is uninitialized, ensure that size.flags aren't used */
  1612. size.flags = PSize;
  1613. if(size.flags & PBaseSize) {
  1614. c->basew = size.base_width;
  1615. c->baseh = size.base_height;
  1616. }
  1617. else if(size.flags & PMinSize) {
  1618. c->basew = size.min_width;
  1619. c->baseh = size.min_height;
  1620. }
  1621. else
  1622. c->basew = c->baseh = 0;
  1623. if(size.flags & PResizeInc) {
  1624. c->incw = size.width_inc;
  1625. c->inch = size.height_inc;
  1626. }
  1627. else
  1628. c->incw = c->inch = 0;
  1629. if(size.flags & PMaxSize) {
  1630. c->maxw = size.max_width;
  1631. c->maxh = size.max_height;
  1632. }
  1633. else
  1634. c->maxw = c->maxh = 0;
  1635. if(size.flags & PMinSize) {
  1636. c->minw = size.min_width;
  1637. c->minh = size.min_height;
  1638. }
  1639. else if(size.flags & PBaseSize) {
  1640. c->minw = size.base_width;
  1641. c->minh = size.base_height;
  1642. }
  1643. else
  1644. c->minw = c->minh = 0;
  1645. if(size.flags & PAspect) {
  1646. c->mina = (float)size.min_aspect.y / (float)size.min_aspect.x;
  1647. c->maxa = (float)size.max_aspect.x / (float)size.max_aspect.y;
  1648. }
  1649. else
  1650. c->maxa = c->mina = 0.0;
  1651. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1652. && c->maxw == c->minw && c->maxh == c->minh);
  1653. }
  1654. void
  1655. updatetitle(Client *c) {
  1656. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1657. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  1658. }
  1659. void
  1660. updatestatus(void) {
  1661. if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  1662. strcpy(stext, "dwm-"VERSION);
  1663. drawbar(selmon);
  1664. }
  1665. void
  1666. updatewmhints(Client *c) {
  1667. XWMHints *wmh;
  1668. if((wmh = XGetWMHints(dpy, c->win))) {
  1669. if(c == selmon->sel && wmh->flags & XUrgencyHint) {
  1670. wmh->flags &= ~XUrgencyHint;
  1671. XSetWMHints(dpy, c->win, wmh);
  1672. }
  1673. else
  1674. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1675. XFree(wmh);
  1676. }
  1677. }
  1678. void
  1679. view(const Arg *arg) {
  1680. if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  1681. return;
  1682. selmon->seltags ^= 1; /* toggle sel tagset */
  1683. if(arg->ui & TAGMASK)
  1684. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  1685. arrange();
  1686. }
  1687. Client *
  1688. wintoclient(Window w) {
  1689. Client *c;
  1690. Monitor *m;
  1691. for(m = mons; m; m = m->next)
  1692. for(c = m->clients; c; c = c->next)
  1693. if(c->win == w)
  1694. return c;
  1695. return NULL;
  1696. }
  1697. Monitor *
  1698. wintomon(Window w) {
  1699. int x, y;
  1700. Client *c;
  1701. Monitor *m;
  1702. if(w == root && getrootpointer(&x, &y))
  1703. return pointertomon(x, y);
  1704. for(m = mons; m; m = m->next)
  1705. if(w == m->barwin)
  1706. return m;
  1707. if((c = wintoclient(w)))
  1708. return c->mon;
  1709. return selmon;
  1710. }
  1711. /* There's no way to check accesses to destroyed windows, thus those cases are
  1712. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1713. * default error handler, which may call exit. */
  1714. int
  1715. xerror(Display *dpy, XErrorEvent *ee) {
  1716. if(ee->error_code == BadWindow
  1717. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1718. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1719. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1720. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1721. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1722. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1723. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1724. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1725. return 0;
  1726. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1727. ee->request_code, ee->error_code);
  1728. return xerrorxlib(dpy, ee); /* may call exit */
  1729. }
  1730. int
  1731. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1732. return 0;
  1733. }
  1734. /* Startup Error handler to check if another window manager
  1735. * is already running. */
  1736. int
  1737. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1738. otherwm = True;
  1739. return -1;
  1740. }
  1741. void
  1742. zoom(const Arg *arg) {
  1743. Client *c = selmon->sel;
  1744. if(!lt[selmon->sellt]->arrange || lt[selmon->sellt]->arrange == monocle || (selmon->sel && selmon->sel->isfloating))
  1745. return;
  1746. if(c == nexttiled(selmon->clients))
  1747. if(!c || !(c = nexttiled(c->next)))
  1748. return;
  1749. detach(c);
  1750. attach(c);
  1751. focus(c);
  1752. arrange();
  1753. }
  1754. int
  1755. main(int argc, char *argv[]) {
  1756. if(argc == 2 && !strcmp("-v", argv[1]))
  1757. die("dwm-"VERSION", © 2006-2009 dwm engineers, see LICENSE for details\n");
  1758. else if(argc != 1)
  1759. die("usage: dwm [-v]\n");
  1760. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1761. fputs("warning: no locale support\n", stderr);
  1762. if(!(dpy = XOpenDisplay(NULL)))
  1763. die("dwm: cannot open display\n");
  1764. checkotherwm();
  1765. setup();
  1766. scan();
  1767. run();
  1768. cleanup();
  1769. XCloseDisplay(dpy);
  1770. return 0;
  1771. }