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.

1988 lines
47 KiB

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