Configuration of dwm for Mac Computers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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