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.

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