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.

1993 lines
48 KiB

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