Configuration file for DWM on MacBook Air
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.

1973 lines
48 KiB

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