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.

1987 lines
47 KiB

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