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.

2059 lines
50 KiB

17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
17 years ago
17 years ago
17 years ago
16 years ago
17 years ago
17 years ago
17 years ago
17 years ago
16 years ago
17 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
17 years ago
17 years ago
16 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
16 years ago
16 years ago
17 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
16 years ago
16 years ago
16 years ago
16 years ago
17 years ago
17 years ago
16 years ago
16 years ago
17 years ago
17 years ago
16 years ago
17 years ago
  1. /* See LICENSE file for copyright and license details.
  2. *
  3. * dynamic window manager is designed like any other X client as well. It is
  4. * driven through handling X events. In contrast to other X clients, a window
  5. * manager selects for SubstructureRedirectMask on the root window, to receive
  6. * events about window (dis-)appearance. Only one X connection at a time is
  7. * allowed to select for this event mask.
  8. *
  9. * The event handlers of dwm are organized in an array which is accessed
  10. * whenever a new event has been fetched. This allows event dispatching
  11. * in O(1) time.
  12. *
  13. * Each child of the root window is called a client, except windows which have
  14. * set the override_redirect flag. Clients are organized in a linked client
  15. * list on each monitor, the focus history is remembered through a stack list
  16. * on each monitor. Each client contains a bit array to indicate the tags of a
  17. * client.
  18. *
  19. * Keys and tagging rules are organized as arrays and defined in config.h.
  20. *
  21. * To understand everything else, start reading main().
  22. */
  23. #include <errno.h>
  24. #include <locale.h>
  25. #include <stdarg.h>
  26. #include <signal.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include <sys/types.h>
  32. #include <sys/wait.h>
  33. #include <X11/cursorfont.h>
  34. #include <X11/keysym.h>
  35. #include <X11/Xatom.h>
  36. #include <X11/Xlib.h>
  37. #include <X11/Xproto.h>
  38. #include <X11/Xutil.h>
  39. #ifdef XINERAMA
  40. #include <X11/extensions/Xinerama.h>
  41. #endif /* XINERAMA */
  42. /* macros */
  43. #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
  44. #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask))
  45. #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
  46. #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
  47. #define LENGTH(X) (sizeof X / sizeof X[0])
  48. #define MAX(A, B) ((A) > (B) ? (A) : (B))
  49. #define MIN(A, B) ((A) < (B) ? (A) : (B))
  50. #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
  51. #define WIDTH(X) ((X)->w + 2 * (X)->bw)
  52. #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
  53. #define TAGMASK ((1 << LENGTH(tags)) - 1)
  54. #define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
  55. /* enums */
  56. enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
  57. enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
  58. enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
  59. enum { WMProtocols, WMDelete, WMState, WMLast }; /* default atoms */
  60. enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
  61. ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
  62. typedef union {
  63. int i;
  64. unsigned int ui;
  65. float f;
  66. const void *v;
  67. } Arg;
  68. typedef struct {
  69. unsigned int click;
  70. unsigned int mask;
  71. unsigned int button;
  72. void (*func)(const Arg *arg);
  73. const Arg arg;
  74. } Button;
  75. typedef struct Monitor Monitor;
  76. typedef struct Client Client;
  77. struct Client {
  78. char name[256];
  79. float mina, maxa;
  80. int x, y, w, h;
  81. int basew, baseh, incw, inch, maxw, maxh, minw, minh;
  82. int bw, oldbw;
  83. unsigned int tags;
  84. Bool isfixed, isfloating, isurgent;
  85. Client *next;
  86. Client *snext;
  87. Monitor *mon;
  88. Window win;
  89. };
  90. typedef struct {
  91. int x, y, w, h;
  92. unsigned long norm[ColLast];
  93. unsigned long sel[ColLast];
  94. Drawable drawable;
  95. GC gc;
  96. struct {
  97. int ascent;
  98. int descent;
  99. int height;
  100. XFontSet set;
  101. XFontStruct *xfont;
  102. } font;
  103. } DC; /* draw context */
  104. typedef struct {
  105. unsigned int mod;
  106. KeySym keysym;
  107. void (*func)(const Arg *);
  108. const Arg arg;
  109. } Key;
  110. typedef struct {
  111. const char *symbol;
  112. void (*arrange)(Monitor *);
  113. } Layout;
  114. struct Monitor {
  115. 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 Bool 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. if(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. }
  514. void
  515. configurerequest(XEvent *e) {
  516. Client *c;
  517. Monitor *m;
  518. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  519. XWindowChanges wc;
  520. if((c = wintoclient(ev->window))) {
  521. if(ev->value_mask & CWBorderWidth)
  522. c->bw = ev->border_width;
  523. else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
  524. m = c->mon;
  525. if(ev->value_mask & CWX)
  526. c->x = m->mx + ev->x;
  527. if(ev->value_mask & CWY)
  528. c->y = m->my + ev->y;
  529. if(ev->value_mask & CWWidth)
  530. c->w = ev->width;
  531. if(ev->value_mask & CWHeight)
  532. c->h = ev->height;
  533. if((c->x + c->w) > m->mx + m->mw && c->isfloating)
  534. c->x = m->mx + (m->mw / 2 - c->w / 2); /* center in x direction */
  535. if((c->y + c->h) > m->my + m->mh && c->isfloating)
  536. c->y = m->my + (m->mh / 2 - c->h / 2); /* center in y direction */
  537. if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
  538. configure(c);
  539. if(ISVISIBLE(c))
  540. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  541. }
  542. else
  543. configure(c);
  544. }
  545. else {
  546. wc.x = ev->x;
  547. wc.y = ev->y;
  548. wc.width = ev->width;
  549. wc.height = ev->height;
  550. wc.border_width = ev->border_width;
  551. wc.sibling = ev->above;
  552. wc.stack_mode = ev->detail;
  553. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  554. }
  555. XSync(dpy, False);
  556. }
  557. void
  558. destroynotify(XEvent *e) {
  559. Client *c;
  560. XDestroyWindowEvent *ev = &e->xdestroywindow;
  561. if((c = wintoclient(ev->window)))
  562. unmanage(c, True);
  563. }
  564. void
  565. detach(Client *c) {
  566. Client **tc;
  567. for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
  568. *tc = c->next;
  569. }
  570. void
  571. detachstack(Client *c) {
  572. Client **tc, *t;
  573. for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
  574. *tc = c->snext;
  575. if(c == c->mon->sel) {
  576. for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
  577. c->mon->sel = t;
  578. }
  579. }
  580. void
  581. die(const char *errstr, ...) {
  582. va_list ap;
  583. va_start(ap, errstr);
  584. vfprintf(stderr, errstr, ap);
  585. va_end(ap);
  586. exit(EXIT_FAILURE);
  587. }
  588. Monitor *
  589. dirtomon(int dir) {
  590. Monitor *m = NULL;
  591. if(dir > 0) {
  592. if(!(m = selmon->next))
  593. m = mons;
  594. }
  595. else {
  596. if(selmon == mons)
  597. for(m = mons; m->next; m = m->next);
  598. else
  599. for(m = mons; m->next != selmon; m = m->next);
  600. }
  601. return m;
  602. }
  603. void
  604. drawbar(Monitor *m) {
  605. int x;
  606. unsigned int i, occ = 0, urg = 0;
  607. unsigned long *col;
  608. Client *c;
  609. for(c = m->clients; c; c = c->next) {
  610. occ |= c->tags;
  611. if(c->isurgent)
  612. urg |= c->tags;
  613. }
  614. dc.x = 0;
  615. for(i = 0; i < LENGTH(tags); i++) {
  616. dc.w = TEXTW(tags[i]);
  617. col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
  618. drawtext(tags[i], col, urg & 1 << i);
  619. drawsquare(m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
  620. occ & 1 << i, urg & 1 << i, col);
  621. dc.x += dc.w;
  622. }
  623. dc.w = blw = TEXTW(m->ltsymbol);
  624. drawtext(m->ltsymbol, dc.norm, False);
  625. dc.x += dc.w;
  626. x = dc.x;
  627. if(m == selmon) { /* status is only drawn on selected monitor */
  628. dc.w = TEXTW(stext);
  629. dc.x = m->ww - dc.w;
  630. if(dc.x < x) {
  631. dc.x = x;
  632. dc.w = m->ww - x;
  633. }
  634. drawtext(stext, dc.norm, False);
  635. }
  636. else
  637. dc.x = m->ww;
  638. if((dc.w = dc.x - x) > bh) {
  639. dc.x = x;
  640. if(m->sel) {
  641. col = m == selmon ? dc.sel : dc.norm;
  642. drawtext(m->sel->name, col, False);
  643. drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
  644. }
  645. else
  646. drawtext(NULL, dc.norm, False);
  647. }
  648. XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
  649. XSync(dpy, False);
  650. }
  651. void
  652. drawbars(void) {
  653. Monitor *m;
  654. for(m = mons; m; m = m->next)
  655. drawbar(m);
  656. }
  657. void
  658. drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]) {
  659. int x;
  660. XGCValues gcv;
  661. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  662. gcv.foreground = col[invert ? ColBG : ColFG];
  663. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  664. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  665. r.x = dc.x + 1;
  666. r.y = dc.y + 1;
  667. if(filled) {
  668. r.width = r.height = x + 1;
  669. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  670. }
  671. else if(empty) {
  672. r.width = r.height = x;
  673. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  674. }
  675. }
  676. void
  677. drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
  678. char buf[256];
  679. int i, x, y, h, len, olen;
  680. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  681. XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
  682. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  683. if(!text)
  684. return;
  685. olen = strlen(text);
  686. h = dc.font.ascent + dc.font.descent;
  687. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  688. x = dc.x + (h / 2);
  689. /* shorten text if necessary */
  690. for(len = MIN(olen, sizeof buf); len && textnw(text, len) > dc.w - h; len--);
  691. if(!len)
  692. return;
  693. memcpy(buf, text, len);
  694. if(len < olen)
  695. for(i = len; i && i > len - 3; buf[--i] = '.');
  696. XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
  697. if(dc.font.set)
  698. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  699. else
  700. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  701. }
  702. void
  703. enternotify(XEvent *e) {
  704. Client *c;
  705. Monitor *m;
  706. XCrossingEvent *ev = &e->xcrossing;
  707. if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  708. return;
  709. if((m = wintomon(ev->window)) && m != selmon) {
  710. unfocus(selmon->sel);
  711. selmon = m;
  712. }
  713. if((c = wintoclient(ev->window)))
  714. focus(c);
  715. else
  716. focus(NULL);
  717. }
  718. void
  719. expose(XEvent *e) {
  720. Monitor *m;
  721. XExposeEvent *ev = &e->xexpose;
  722. if(ev->count == 0 && (m = wintomon(ev->window)))
  723. drawbar(m);
  724. }
  725. void
  726. focus(Client *c) {
  727. if(!c || !ISVISIBLE(c))
  728. for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
  729. if(selmon->sel)
  730. unfocus(selmon->sel);
  731. if(c) {
  732. if(c->mon != selmon)
  733. selmon = c->mon;
  734. if(c->isurgent)
  735. clearurgent(c);
  736. detachstack(c);
  737. attachstack(c);
  738. grabbuttons(c, True);
  739. XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
  740. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  741. }
  742. else
  743. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  744. selmon->sel = c;
  745. drawbars();
  746. }
  747. void
  748. focusin(XEvent *e) { /* there are some broken focus acquiring clients */
  749. XFocusChangeEvent *ev = &e->xfocus;
  750. if(selmon->sel && ev->window != selmon->sel->win)
  751. XSetInputFocus(dpy, selmon->sel->win, RevertToPointerRoot, CurrentTime);
  752. }
  753. void
  754. focusmon(const Arg *arg) {
  755. Monitor *m = NULL;
  756. if(!mons->next)
  757. return;
  758. m = dirtomon(arg->i);
  759. unfocus(selmon->sel);
  760. selmon = m;
  761. focus(NULL);
  762. }
  763. void
  764. focusstack(const Arg *arg) {
  765. Client *c = NULL, *i;
  766. if(!selmon->sel)
  767. return;
  768. if(arg->i > 0) {
  769. for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
  770. if(!c)
  771. for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
  772. }
  773. else {
  774. for(i = selmon->clients; i != selmon->sel; i = i->next)
  775. if(ISVISIBLE(i))
  776. c = i;
  777. if(!c)
  778. for(; i; i = i->next)
  779. if(ISVISIBLE(i))
  780. c = i;
  781. }
  782. if(c) {
  783. focus(c);
  784. restack(selmon);
  785. }
  786. }
  787. unsigned long
  788. getcolor(const char *colstr) {
  789. Colormap cmap = DefaultColormap(dpy, screen);
  790. XColor color;
  791. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  792. die("error, cannot allocate color '%s'\n", colstr);
  793. return color.pixel;
  794. }
  795. Bool
  796. getrootptr(int *x, int *y) {
  797. int di;
  798. unsigned int dui;
  799. Window dummy;
  800. return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
  801. }
  802. long
  803. getstate(Window w) {
  804. int format, status;
  805. long result = -1;
  806. unsigned char *p = NULL;
  807. unsigned long n, extra;
  808. Atom real;
  809. status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  810. &real, &format, &n, &extra, (unsigned char **)&p);
  811. if(status != Success)
  812. return -1;
  813. if(n != 0)
  814. result = *p;
  815. XFree(p);
  816. return result;
  817. }
  818. Bool
  819. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  820. char **list = NULL;
  821. int n;
  822. XTextProperty name;
  823. if(!text || size == 0)
  824. return False;
  825. text[0] = '\0';
  826. XGetTextProperty(dpy, w, &name, atom);
  827. if(!name.nitems)
  828. return False;
  829. if(name.encoding == XA_STRING)
  830. strncpy(text, (char *)name.value, size - 1);
  831. else {
  832. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
  833. strncpy(text, *list, size - 1);
  834. XFreeStringList(list);
  835. }
  836. }
  837. text[size - 1] = '\0';
  838. XFree(name.value);
  839. return True;
  840. }
  841. void
  842. grabbuttons(Client *c, Bool focused) {
  843. updatenumlockmask();
  844. {
  845. unsigned int i, j;
  846. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  847. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  848. if(focused) {
  849. for(i = 0; i < LENGTH(buttons); i++)
  850. if(buttons[i].click == ClkClientWin)
  851. for(j = 0; j < LENGTH(modifiers); j++)
  852. XGrabButton(dpy, buttons[i].button,
  853. buttons[i].mask | modifiers[j],
  854. c->win, False, BUTTONMASK,
  855. GrabModeAsync, GrabModeSync, None, None);
  856. }
  857. else
  858. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  859. BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
  860. }
  861. }
  862. void
  863. grabkeys(void) {
  864. updatenumlockmask();
  865. {
  866. unsigned int i, j;
  867. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  868. KeyCode code;
  869. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  870. for(i = 0; i < LENGTH(keys); i++) {
  871. if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
  872. for(j = 0; j < LENGTH(modifiers); j++)
  873. XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
  874. True, GrabModeAsync, GrabModeAsync);
  875. }
  876. }
  877. }
  878. void
  879. initfont(const char *fontstr) {
  880. char *def, **missing;
  881. int i, n;
  882. missing = NULL;
  883. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  884. if(missing) {
  885. while(n--)
  886. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  887. XFreeStringList(missing);
  888. }
  889. if(dc.font.set) {
  890. XFontSetExtents *font_extents;
  891. XFontStruct **xfonts;
  892. char **font_names;
  893. dc.font.ascent = dc.font.descent = 0;
  894. font_extents = XExtentsOfFontSet(dc.font.set);
  895. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  896. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  897. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  898. dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
  899. xfonts++;
  900. }
  901. }
  902. else {
  903. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  904. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  905. die("error, cannot load font: '%s'\n", fontstr);
  906. dc.font.ascent = dc.font.xfont->ascent;
  907. dc.font.descent = dc.font.xfont->descent;
  908. }
  909. dc.font.height = dc.font.ascent + dc.font.descent;
  910. }
  911. Bool
  912. isprotodel(Client *c) {
  913. int i, n;
  914. Atom *protocols;
  915. Bool ret = False;
  916. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  917. for(i = 0; !ret && i < n; i++)
  918. if(protocols[i] == wmatom[WMDelete])
  919. ret = True;
  920. XFree(protocols);
  921. }
  922. return ret;
  923. }
  924. void
  925. keypress(XEvent *e) {
  926. unsigned int i;
  927. KeySym keysym;
  928. XKeyEvent *ev;
  929. ev = &e->xkey;
  930. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  931. for(i = 0; i < LENGTH(keys); i++)
  932. if(keysym == keys[i].keysym
  933. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  934. && keys[i].func)
  935. keys[i].func(&(keys[i].arg));
  936. }
  937. void
  938. killclient(const Arg *arg) {
  939. XEvent ev;
  940. if(!selmon->sel)
  941. return;
  942. if(isprotodel(selmon->sel)) {
  943. ev.type = ClientMessage;
  944. ev.xclient.window = selmon->sel->win;
  945. ev.xclient.message_type = wmatom[WMProtocols];
  946. ev.xclient.format = 32;
  947. ev.xclient.data.l[0] = wmatom[WMDelete];
  948. ev.xclient.data.l[1] = CurrentTime;
  949. XSendEvent(dpy, selmon->sel->win, False, NoEventMask, &ev);
  950. }
  951. else {
  952. XGrabServer(dpy);
  953. XSetErrorHandler(xerrordummy);
  954. XSetCloseDownMode(dpy, DestroyAll);
  955. XKillClient(dpy, selmon->sel->win);
  956. XSync(dpy, False);
  957. XSetErrorHandler(xerror);
  958. XUngrabServer(dpy);
  959. }
  960. }
  961. void
  962. manage(Window w, XWindowAttributes *wa) {
  963. static Client cz;
  964. Client *c, *t = NULL;
  965. Window trans = None;
  966. XWindowChanges wc;
  967. if(!(c = malloc(sizeof(Client))))
  968. die("fatal: could not malloc() %u bytes\n", sizeof(Client));
  969. *c = cz;
  970. c->win = w;
  971. updatetitle(c);
  972. if(XGetTransientForHint(dpy, w, &trans))
  973. t = wintoclient(trans);
  974. if(t) {
  975. c->mon = t->mon;
  976. c->tags = t->tags;
  977. }
  978. else {
  979. c->mon = selmon;
  980. applyrules(c);
  981. }
  982. /* geometry */
  983. c->x = wa->x + c->mon->wx;
  984. c->y = wa->y + c->mon->wy;
  985. c->w = wa->width;
  986. c->h = wa->height;
  987. c->oldbw = wa->border_width;
  988. if(c->w == c->mon->mw && c->h == c->mon->mh) {
  989. c->x = c->mon->mx;
  990. c->y = c->mon->my;
  991. c->bw = 0;
  992. }
  993. else {
  994. if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
  995. c->x = c->mon->mx + c->mon->mw - WIDTH(c);
  996. if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
  997. c->y = c->mon->my + c->mon->mh - HEIGHT(c);
  998. c->x = MAX(c->x, c->mon->mx);
  999. /* only fix client y-offset, if the client center might cover the bar */
  1000. c->y = MAX(c->y, ((c->mon->by == 0) && (c->x + (c->w / 2) >= c->mon->wx)
  1001. && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
  1002. c->bw = borderpx;
  1003. }
  1004. wc.border_width = c->bw;
  1005. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  1006. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  1007. configure(c); /* propagates border_width, if size doesn't change */
  1008. updatesizehints(c);
  1009. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  1010. grabbuttons(c, False);
  1011. if(!c->isfloating)
  1012. c->isfloating = trans != None || c->isfixed;
  1013. if(c->isfloating)
  1014. XRaiseWindow(dpy, c->win);
  1015. attach(c);
  1016. attachstack(c);
  1017. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  1018. XMapWindow(dpy, c->win);
  1019. setclientstate(c, NormalState);
  1020. arrange();
  1021. }
  1022. void
  1023. mappingnotify(XEvent *e) {
  1024. XMappingEvent *ev = &e->xmapping;
  1025. XRefreshKeyboardMapping(ev);
  1026. if(ev->request == MappingKeyboard)
  1027. grabkeys();
  1028. }
  1029. void
  1030. maprequest(XEvent *e) {
  1031. static XWindowAttributes wa;
  1032. XMapRequestEvent *ev = &e->xmaprequest;
  1033. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  1034. return;
  1035. if(wa.override_redirect)
  1036. return;
  1037. if(!wintoclient(ev->window))
  1038. manage(ev->window, &wa);
  1039. }
  1040. void
  1041. monocle(Monitor *m) {
  1042. static char ntext[8];
  1043. unsigned int n = 0;
  1044. Client *c;
  1045. for(c = m->clients; c; c = c->next)
  1046. if(ISVISIBLE(c))
  1047. n++;
  1048. if(n > 0) { /* override layout symbol */
  1049. snprintf(ntext, sizeof ntext, "[%d]", n);
  1050. m->ltsymbol = ntext;
  1051. }
  1052. for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
  1053. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
  1054. }
  1055. void
  1056. movemouse(const Arg *arg) {
  1057. int x, y, ocx, ocy, nx, ny;
  1058. Client *c;
  1059. Monitor *m;
  1060. XEvent ev;
  1061. if(!(c = selmon->sel))
  1062. return;
  1063. restack(selmon);
  1064. ocx = c->x;
  1065. ocy = c->y;
  1066. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1067. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1068. return;
  1069. if(!getrootptr(&x, &y))
  1070. return;
  1071. do {
  1072. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1073. switch (ev.type) {
  1074. case ConfigureRequest:
  1075. case Expose:
  1076. case MapRequest:
  1077. handler[ev.type](&ev);
  1078. break;
  1079. case MotionNotify:
  1080. nx = ocx + (ev.xmotion.x - x);
  1081. ny = ocy + (ev.xmotion.y - y);
  1082. if(snap && nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1083. && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1084. if(abs(selmon->wx - nx) < snap)
  1085. nx = selmon->wx;
  1086. else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1087. nx = selmon->wx + selmon->ww - WIDTH(c);
  1088. if(abs(selmon->wy - ny) < snap)
  1089. ny = selmon->wy;
  1090. else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1091. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1092. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1093. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1094. togglefloating(NULL);
  1095. }
  1096. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1097. resize(c, nx, ny, c->w, c->h, True);
  1098. break;
  1099. }
  1100. } while(ev.type != ButtonRelease);
  1101. XUngrabPointer(dpy, CurrentTime);
  1102. if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
  1103. sendmon(c, m);
  1104. selmon = m;
  1105. focus(NULL);
  1106. }
  1107. }
  1108. Client *
  1109. nexttiled(Client *c) {
  1110. for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  1111. return c;
  1112. }
  1113. Monitor *
  1114. ptrtomon(int x, int y) {
  1115. Monitor *m;
  1116. for(m = mons; m; m = m->next)
  1117. if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh))
  1118. return m;
  1119. return selmon;
  1120. }
  1121. void
  1122. propertynotify(XEvent *e) {
  1123. Client *c;
  1124. Window trans;
  1125. XPropertyEvent *ev = &e->xproperty;
  1126. if((ev->window == root) && (ev->atom == XA_WM_NAME))
  1127. updatestatus();
  1128. else if(ev->state == PropertyDelete)
  1129. return; /* ignore */
  1130. else if((c = wintoclient(ev->window))) {
  1131. switch (ev->atom) {
  1132. default: break;
  1133. case XA_WM_TRANSIENT_FOR:
  1134. XGetTransientForHint(dpy, c->win, &trans);
  1135. if(!c->isfloating && (c->isfloating = (wintoclient(trans) != NULL)))
  1136. arrange();
  1137. break;
  1138. case XA_WM_NORMAL_HINTS:
  1139. updatesizehints(c);
  1140. break;
  1141. case XA_WM_HINTS:
  1142. updatewmhints(c);
  1143. drawbars();
  1144. break;
  1145. }
  1146. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1147. updatetitle(c);
  1148. if(c == c->mon->sel)
  1149. drawbar(c->mon);
  1150. }
  1151. }
  1152. }
  1153. void
  1154. quit(const Arg *arg) {
  1155. running = False;
  1156. }
  1157. void
  1158. resize(Client *c, int x, int y, int w, int h, Bool interact) {
  1159. XWindowChanges wc;
  1160. if(applysizehints(c, &x, &y, &w, &h, interact)) {
  1161. c->x = wc.x = x;
  1162. c->y = wc.y = y;
  1163. c->w = wc.width = w;
  1164. c->h = wc.height = h;
  1165. wc.border_width = c->bw;
  1166. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1167. configure(c);
  1168. XSync(dpy, False);
  1169. }
  1170. }
  1171. void
  1172. resizemouse(const Arg *arg) {
  1173. int ocx, ocy;
  1174. int nw, nh;
  1175. Client *c;
  1176. Monitor *m;
  1177. XEvent ev;
  1178. if(!(c = selmon->sel))
  1179. return;
  1180. restack(selmon);
  1181. ocx = c->x;
  1182. ocy = c->y;
  1183. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1184. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1185. return;
  1186. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1187. do {
  1188. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1189. switch(ev.type) {
  1190. case ConfigureRequest:
  1191. case Expose:
  1192. case MapRequest:
  1193. handler[ev.type](&ev);
  1194. break;
  1195. case MotionNotify:
  1196. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1197. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1198. if(snap && nw >= selmon->wx && nw <= selmon->wx + selmon->ww
  1199. && nh >= selmon->wy && nh <= selmon->wy + selmon->wh)
  1200. {
  1201. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1202. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1203. togglefloating(NULL);
  1204. }
  1205. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1206. resize(c, c->x, c->y, nw, nh, True);
  1207. break;
  1208. }
  1209. } while(ev.type != ButtonRelease);
  1210. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1211. XUngrabPointer(dpy, CurrentTime);
  1212. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1213. if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
  1214. sendmon(c, m);
  1215. selmon = m;
  1216. focus(NULL);
  1217. }
  1218. }
  1219. void
  1220. restack(Monitor *m) {
  1221. Client *c;
  1222. XEvent ev;
  1223. XWindowChanges wc;
  1224. drawbars();
  1225. if(!m->sel)
  1226. return;
  1227. if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
  1228. XRaiseWindow(dpy, m->sel->win);
  1229. if(m->lt[m->sellt]->arrange) {
  1230. wc.stack_mode = Below;
  1231. wc.sibling = m->barwin;
  1232. for(c = m->stack; c; c = c->snext)
  1233. if(!c->isfloating && ISVISIBLE(c)) {
  1234. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1235. wc.sibling = c->win;
  1236. }
  1237. }
  1238. XSync(dpy, False);
  1239. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1240. }
  1241. void
  1242. run(void) {
  1243. XEvent ev;
  1244. /* main event loop */
  1245. XSync(dpy, False);
  1246. while(running && !XNextEvent(dpy, &ev))
  1247. if(handler[ev.type])
  1248. handler[ev.type](&ev); /* call handler */
  1249. }
  1250. void
  1251. scan(void) {
  1252. unsigned int i, num;
  1253. Window d1, d2, *wins = NULL;
  1254. XWindowAttributes wa;
  1255. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1256. for(i = 0; i < num; i++) {
  1257. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1258. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1259. continue;
  1260. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1261. manage(wins[i], &wa);
  1262. }
  1263. for(i = 0; i < num; i++) { /* now the transients */
  1264. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1265. continue;
  1266. if(XGetTransientForHint(dpy, wins[i], &d1)
  1267. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1268. manage(wins[i], &wa);
  1269. }
  1270. if(wins)
  1271. XFree(wins);
  1272. }
  1273. }
  1274. void
  1275. sendmon(Client *c, Monitor *m) {
  1276. if(c->mon == m)
  1277. return;
  1278. unfocus(c);
  1279. detach(c);
  1280. detachstack(c);
  1281. c->mon = m;
  1282. c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
  1283. attach(c);
  1284. attachstack(c);
  1285. focus(NULL);
  1286. arrange();
  1287. }
  1288. void
  1289. setclientstate(Client *c, long state) {
  1290. long data[] = { state, None };
  1291. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1292. PropModeReplace, (unsigned char *)data, 2);
  1293. }
  1294. void
  1295. setlayout(const Arg *arg) {
  1296. if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
  1297. selmon->sellt ^= 1;
  1298. if(arg && arg->v)
  1299. selmon->lt[selmon->sellt] = (Layout *)arg->v;
  1300. if(selmon->sel)
  1301. arrange();
  1302. else
  1303. drawbars();
  1304. }
  1305. /* arg > 1.0 will set mfact absolutly */
  1306. void
  1307. setmfact(const Arg *arg) {
  1308. float f;
  1309. if(!arg || !selmon->lt[selmon->sellt]->arrange)
  1310. return;
  1311. f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  1312. if(f < 0.1 || f > 0.9)
  1313. return;
  1314. selmon->mfact = f;
  1315. arrange();
  1316. }
  1317. void
  1318. setup(void) {
  1319. XSetWindowAttributes wa;
  1320. /* clean up any zombies immediately */
  1321. sigchld(0);
  1322. /* init screen */
  1323. screen = DefaultScreen(dpy);
  1324. root = RootWindow(dpy, screen);
  1325. initfont(font);
  1326. sw = DisplayWidth(dpy, screen);
  1327. sh = DisplayHeight(dpy, screen);
  1328. bh = dc.h = dc.font.height + 2;
  1329. updategeom();
  1330. /* init atoms */
  1331. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1332. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1333. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1334. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1335. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1336. /* init cursors */
  1337. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1338. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1339. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1340. /* init appearance */
  1341. dc.norm[ColBorder] = getcolor(normbordercolor);
  1342. dc.norm[ColBG] = getcolor(normbgcolor);
  1343. dc.norm[ColFG] = getcolor(normfgcolor);
  1344. dc.sel[ColBorder] = getcolor(selbordercolor);
  1345. dc.sel[ColBG] = getcolor(selbgcolor);
  1346. dc.sel[ColFG] = getcolor(selfgcolor);
  1347. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1348. dc.gc = XCreateGC(dpy, root, 0, NULL);
  1349. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1350. if(!dc.font.set)
  1351. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1352. /* init bars */
  1353. updatebars();
  1354. updatestatus();
  1355. /* EWMH support per view */
  1356. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1357. PropModeReplace, (unsigned char *) netatom, NetLast);
  1358. /* select for events */
  1359. wa.cursor = cursor[CurNormal];
  1360. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
  1361. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
  1362. |PropertyChangeMask;
  1363. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1364. XSelectInput(dpy, root, wa.event_mask);
  1365. grabkeys();
  1366. }
  1367. void
  1368. showhide(Client *c) {
  1369. if(!c)
  1370. return;
  1371. if(ISVISIBLE(c)) { /* show clients top down */
  1372. XMoveWindow(dpy, c->win, c->x, c->y);
  1373. if(!c->mon->lt[c->mon->sellt]->arrange || c->isfloating)
  1374. resize(c, c->x, c->y, c->w, c->h, False);
  1375. showhide(c->snext);
  1376. }
  1377. else { /* hide clients bottom up */
  1378. showhide(c->snext);
  1379. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  1380. }
  1381. }
  1382. void
  1383. sigchld(int unused) {
  1384. if(signal(SIGCHLD, sigchld) == SIG_ERR)
  1385. die("Can't install SIGCHLD handler");
  1386. while(0 < waitpid(-1, NULL, WNOHANG));
  1387. }
  1388. void
  1389. spawn(const Arg *arg) {
  1390. if(fork() == 0) {
  1391. if(dpy)
  1392. close(ConnectionNumber(dpy));
  1393. setsid();
  1394. execvp(((char **)arg->v)[0], (char **)arg->v);
  1395. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1396. perror(" failed");
  1397. exit(0);
  1398. }
  1399. }
  1400. void
  1401. tag(const Arg *arg) {
  1402. if(selmon->sel && arg->ui & TAGMASK) {
  1403. selmon->sel->tags = arg->ui & TAGMASK;
  1404. arrange();
  1405. }
  1406. }
  1407. void
  1408. tagmon(const Arg *arg) {
  1409. if(!selmon->sel || !mons->next)
  1410. return;
  1411. sendmon(selmon->sel, dirtomon(arg->i));
  1412. }
  1413. int
  1414. textnw(const char *text, unsigned int len) {
  1415. XRectangle r;
  1416. if(dc.font.set) {
  1417. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1418. return r.width;
  1419. }
  1420. return XTextWidth(dc.font.xfont, text, len);
  1421. }
  1422. void
  1423. tile(Monitor *m) {
  1424. int x, y, h, w, mw;
  1425. unsigned int i, n;
  1426. Client *c;
  1427. for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  1428. if(n == 0)
  1429. return;
  1430. /* master */
  1431. c = nexttiled(m->clients);
  1432. mw = m->mfact * m->ww;
  1433. resize(c, m->wx, m->wy, (n == 1 ? m->ww : mw) - 2 * c->bw, m->wh - 2 * c->bw, False);
  1434. if(--n == 0)
  1435. return;
  1436. /* tile stack */
  1437. x = (m->wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : m->wx + mw;
  1438. y = m->wy;
  1439. w = (m->wx + mw > c->x + c->w) ? m->wx + m->ww - x : m->ww - mw;
  1440. h = m->wh / n;
  1441. if(h < bh)
  1442. h = m->wh;
  1443. for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
  1444. resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
  1445. ? m->wy + m->wh - y - 2 * c->bw : h - 2 * c->bw), False);
  1446. if(h != m->wh)
  1447. y = c->y + HEIGHT(c);
  1448. }
  1449. }
  1450. void
  1451. togglebar(const Arg *arg) {
  1452. selmon->showbar = !selmon->showbar;
  1453. updatebarpos(selmon);
  1454. XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  1455. arrange();
  1456. }
  1457. void
  1458. togglefloating(const Arg *arg) {
  1459. if(!selmon->sel)
  1460. return;
  1461. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  1462. if(selmon->sel->isfloating)
  1463. resize(selmon->sel, selmon->sel->x, selmon->sel->y,
  1464. selmon->sel->w, selmon->sel->h, False);
  1465. arrange();
  1466. }
  1467. void
  1468. toggletag(const Arg *arg) {
  1469. unsigned int newtags;
  1470. if(!selmon->sel)
  1471. return;
  1472. newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
  1473. if(newtags) {
  1474. selmon->sel->tags = newtags;
  1475. arrange();
  1476. }
  1477. }
  1478. void
  1479. toggleview(const Arg *arg) {
  1480. unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  1481. if(newtagset) {
  1482. selmon->tagset[selmon->seltags] = newtagset;
  1483. arrange();
  1484. }
  1485. }
  1486. void
  1487. unfocus(Client *c) {
  1488. if(!c)
  1489. return;
  1490. grabbuttons(c, False);
  1491. XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
  1492. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  1493. }
  1494. void
  1495. unmanage(Client *c, Bool destroyed) {
  1496. XWindowChanges wc;
  1497. /* The server grab construct avoids race conditions. */
  1498. detach(c);
  1499. detachstack(c);
  1500. if(!destroyed) {
  1501. wc.border_width = c->oldbw;
  1502. XGrabServer(dpy);
  1503. XSetErrorHandler(xerrordummy);
  1504. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1505. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1506. setclientstate(c, WithdrawnState);
  1507. XSync(dpy, False);
  1508. XSetErrorHandler(xerror);
  1509. XUngrabServer(dpy);
  1510. }
  1511. free(c);
  1512. focus(NULL);
  1513. arrange();
  1514. }
  1515. void
  1516. unmapnotify(XEvent *e) {
  1517. Client *c;
  1518. XUnmapEvent *ev = &e->xunmap;
  1519. if((c = wintoclient(ev->window)))
  1520. unmanage(c, False);
  1521. }
  1522. void
  1523. updatebars(void) {
  1524. Monitor *m;
  1525. XSetWindowAttributes wa;
  1526. wa.override_redirect = True;
  1527. wa.background_pixmap = ParentRelative;
  1528. wa.event_mask = ButtonPressMask|ExposureMask;
  1529. for(m = mons; m; m = m->next) {
  1530. m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
  1531. CopyFromParent, DefaultVisual(dpy, screen),
  1532. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1533. XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
  1534. XMapRaised(dpy, m->barwin);
  1535. }
  1536. }
  1537. void
  1538. updatebarpos(Monitor *m) {
  1539. m->wy = m->my;
  1540. m->wh = m->mh;
  1541. if(m->showbar) {
  1542. m->wh -= bh;
  1543. m->by = m->topbar ? m->wy : m->wy + m->wh;
  1544. m->wy = m->topbar ? m->wy + bh : m->wy;
  1545. }
  1546. else
  1547. m->by = -bh;
  1548. }
  1549. Bool
  1550. updategeom(void) {
  1551. int i, j, nn = 1, n = 1;
  1552. Client *c;
  1553. Monitor *newmons = NULL, *m = NULL, *tm;
  1554. /* TODO:
  1555. * This function needs to be seriously re-designed:
  1556. *
  1557. * #ifdef XINERAMA
  1558. * 1. Determine number of already existing monitors n
  1559. * 2. Determine number of monitors Xinerama reports nn
  1560. * 3. if(n <= nn) {
  1561. * if(n < nn) {
  1562. * append nn-n monitors to current struct
  1563. * flag dirty
  1564. * }
  1565. * for(i = 0; i < nn; i++) {
  1566. * if(oldgeom != newgeom) {
  1567. * apply newgeom;
  1568. * flag dirty;
  1569. * }
  1570. * }
  1571. * }
  1572. * else {
  1573. * detach all clients
  1574. * destroy current monitor struct
  1575. * create new monitor struct
  1576. * attach all clients to first monitor
  1577. * flag dirty;
  1578. * }
  1579. * return dirty flag to caller
  1580. * if dirty is seen by caller:
  1581. * re-arrange bars/pixmaps
  1582. * arrange()
  1583. * #else
  1584. * don't share between XINERAMA and non-XINERAMA handling if it gets
  1585. * too ugly
  1586. * #endif
  1587. */
  1588. #ifdef XINERAMA
  1589. XineramaScreenInfo *info = NULL;
  1590. Bool *flags = NULL;
  1591. if(XineramaIsActive(dpy))
  1592. info = XineramaQueryScreens(dpy, &n);
  1593. flags = (Bool *)malloc(sizeof(Bool) * n);
  1594. for(i = 0; i < n; i++)
  1595. flags[i] = False;
  1596. /* next double-loop seeks any combination of retrieved Xinerama info
  1597. * with existing monitors, this is used to avoid unnecessary
  1598. * re-allocations of monitor structs */
  1599. for(i = 0, nn = n; i < n; i++)
  1600. for(j = 0, m = mons; m; m = m->next, j++)
  1601. if(!flags[j]) {
  1602. if((flags[j] = (
  1603. info[i].x_org == m->mx
  1604. && info[i].y_org == m->my
  1605. && info[i].width == m->mw
  1606. && info[i].height == m->mh)
  1607. ))
  1608. --nn;
  1609. }
  1610. if(nn == 0) { /* no need to re-allocate monitors */
  1611. j = 0;
  1612. for(i = 0, m = mons; m; m = m->next, i++) {
  1613. m->num = info[i].screen_number;
  1614. if(info[i].x_org != m->mx
  1615. || info[i].y_org != m->my
  1616. || info[i].width != m->mw
  1617. || info[i].height != m->mh)
  1618. {
  1619. m->mx = m->wx = info[i].x_org;
  1620. m->my = m->wy = info[i].y_org;
  1621. m->mw = m->ww = info[i].width;
  1622. m->mh = m->wh = info[i].height;
  1623. updatebarpos(m);
  1624. j++;
  1625. }
  1626. }
  1627. XFree(info);
  1628. free(flags);
  1629. return j > 0;
  1630. }
  1631. /* next algorithm only considers unique geometries as separate screens */
  1632. for(i = 0; i < n; i++)
  1633. flags[i] = False; /* used for ignoring certain monitors */
  1634. for(i = 0, nn = n; i < n; i++)
  1635. for(j = 0; j < n; j++)
  1636. if(i != j && !flags[i]) {
  1637. if((flags[i] = (
  1638. info[i].x_org == info[j].x_org
  1639. && info[i].y_org == info[j].y_org
  1640. && info[i].width == info[j].width
  1641. && info[i].height == info[j].height)
  1642. ))
  1643. --nn;
  1644. }
  1645. #endif /* XINERAMA */
  1646. /* allocate monitor(s) for the new geometry setup */
  1647. for(i = 0; i < nn; i++) {
  1648. if(!(m = (Monitor *)malloc(sizeof(Monitor))))
  1649. die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
  1650. m->next = newmons;
  1651. newmons = m;
  1652. }
  1653. /* initialise monitor(s) */
  1654. #ifdef XINERAMA
  1655. if(XineramaIsActive(dpy)) {
  1656. for(i = 0, m = newmons; m && i < n; i++) {
  1657. if(!flags[i]) { /* only use screens that aren't dublettes */
  1658. m->num = info[i].screen_number;
  1659. m->mx = m->wx = info[i].x_org;
  1660. m->my = m->wy = info[i].y_org;
  1661. m->mw = m->ww = info[i].width;
  1662. m->mh = m->wh = info[i].height;
  1663. m = m->next;
  1664. }
  1665. }
  1666. XFree(info);
  1667. free(flags);
  1668. }
  1669. else
  1670. #endif /* XINERAMA */
  1671. /* default monitor setup */
  1672. {
  1673. m->num = 0;
  1674. m->mx = m->wx = 0;
  1675. m->my = m->wy = 0;
  1676. m->mw = m->ww = sw;
  1677. m->mh = m->wh = sh;
  1678. }
  1679. /* bar geometry setup */
  1680. for(m = newmons; m; m = m->next) {
  1681. m->sel = m->stack = m->clients = NULL;
  1682. m->seltags = 0;
  1683. m->sellt = 0;
  1684. m->tagset[0] = m->tagset[1] = 1;
  1685. m->mfact = mfact;
  1686. m->showbar = showbar;
  1687. m->topbar = topbar;
  1688. m->lt[0] = &layouts[0];
  1689. m->lt[1] = &layouts[1 % LENGTH(layouts)];
  1690. m->ltsymbol = layouts[0].symbol;
  1691. updatebarpos(m);
  1692. }
  1693. /* reassign left over clients of disappeared monitors */
  1694. for(tm = mons; tm; tm = tm->next)
  1695. while(tm->clients) {
  1696. c = tm->clients;
  1697. tm->clients = c->next;
  1698. detachstack(c);
  1699. c->mon = newmons;
  1700. attach(c);
  1701. attachstack(c);
  1702. }
  1703. /* select focused monitor */
  1704. cleanupmons();
  1705. selmon = mons = newmons;
  1706. selmon = wintomon(root);
  1707. return True;
  1708. }
  1709. void
  1710. updatenumlockmask(void) {
  1711. unsigned int i, j;
  1712. XModifierKeymap *modmap;
  1713. numlockmask = 0;
  1714. modmap = XGetModifierMapping(dpy);
  1715. for(i = 0; i < 8; i++)
  1716. for(j = 0; j < modmap->max_keypermod; j++)
  1717. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1718. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1719. numlockmask = (1 << i);
  1720. XFreeModifiermap(modmap);
  1721. }
  1722. void
  1723. updatesizehints(Client *c) {
  1724. long msize;
  1725. XSizeHints size;
  1726. if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
  1727. /* size is uninitialized, ensure that size.flags aren't used */
  1728. size.flags = PSize;
  1729. if(size.flags & PBaseSize) {
  1730. c->basew = size.base_width;
  1731. c->baseh = size.base_height;
  1732. }
  1733. else if(size.flags & PMinSize) {
  1734. c->basew = size.min_width;
  1735. c->baseh = size.min_height;
  1736. }
  1737. else
  1738. c->basew = c->baseh = 0;
  1739. if(size.flags & PResizeInc) {
  1740. c->incw = size.width_inc;
  1741. c->inch = size.height_inc;
  1742. }
  1743. else
  1744. c->incw = c->inch = 0;
  1745. if(size.flags & PMaxSize) {
  1746. c->maxw = size.max_width;
  1747. c->maxh = size.max_height;
  1748. }
  1749. else
  1750. c->maxw = c->maxh = 0;
  1751. if(size.flags & PMinSize) {
  1752. c->minw = size.min_width;
  1753. c->minh = size.min_height;
  1754. }
  1755. else if(size.flags & PBaseSize) {
  1756. c->minw = size.base_width;
  1757. c->minh = size.base_height;
  1758. }
  1759. else
  1760. c->minw = c->minh = 0;
  1761. if(size.flags & PAspect) {
  1762. c->mina = (float)size.min_aspect.y / size.min_aspect.x;
  1763. c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
  1764. }
  1765. else
  1766. c->maxa = c->mina = 0.0;
  1767. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1768. && c->maxw == c->minw && c->maxh == c->minh);
  1769. }
  1770. void
  1771. updatetitle(Client *c) {
  1772. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1773. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  1774. if(c->name[0] == '\0') /* hack to mark broken clients */
  1775. strcpy(c->name, broken);
  1776. }
  1777. void
  1778. updatestatus(void) {
  1779. if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  1780. strcpy(stext, "dwm-"VERSION);
  1781. drawbar(selmon);
  1782. }
  1783. void
  1784. updatewmhints(Client *c) {
  1785. XWMHints *wmh;
  1786. if((wmh = XGetWMHints(dpy, c->win))) {
  1787. if(c == selmon->sel && wmh->flags & XUrgencyHint) {
  1788. wmh->flags &= ~XUrgencyHint;
  1789. XSetWMHints(dpy, c->win, wmh);
  1790. }
  1791. else
  1792. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1793. XFree(wmh);
  1794. }
  1795. }
  1796. void
  1797. view(const Arg *arg) {
  1798. if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  1799. return;
  1800. selmon->seltags ^= 1; /* toggle sel tagset */
  1801. if(arg->ui & TAGMASK)
  1802. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  1803. arrange();
  1804. }
  1805. Client *
  1806. wintoclient(Window w) {
  1807. Client *c;
  1808. Monitor *m;
  1809. for(m = mons; m; m = m->next)
  1810. for(c = m->clients; c; c = c->next)
  1811. if(c->win == w)
  1812. return c;
  1813. return NULL;
  1814. }
  1815. Monitor *
  1816. wintomon(Window w) {
  1817. int x, y;
  1818. Client *c;
  1819. Monitor *m;
  1820. if(w == root && getrootptr(&x, &y))
  1821. return ptrtomon(x, y);
  1822. for(m = mons; m; m = m->next)
  1823. if(w == m->barwin)
  1824. return m;
  1825. if((c = wintoclient(w)))
  1826. return c->mon;
  1827. return selmon;
  1828. }
  1829. /* There's no way to check accesses to destroyed windows, thus those cases are
  1830. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1831. * default error handler, which may call exit. */
  1832. int
  1833. xerror(Display *dpy, XErrorEvent *ee) {
  1834. if(ee->error_code == BadWindow
  1835. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1836. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1837. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1838. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1839. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1840. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1841. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1842. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1843. return 0;
  1844. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1845. ee->request_code, ee->error_code);
  1846. return xerrorxlib(dpy, ee); /* may call exit */
  1847. }
  1848. int
  1849. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1850. return 0;
  1851. }
  1852. /* Startup Error handler to check if another window manager
  1853. * is already running. */
  1854. int
  1855. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1856. otherwm = True;
  1857. return -1;
  1858. }
  1859. void
  1860. zoom(const Arg *arg) {
  1861. Client *c = selmon->sel;
  1862. if(!selmon->lt[selmon->sellt]->arrange
  1863. || selmon->lt[selmon->sellt]->arrange == monocle
  1864. || (selmon->sel && selmon->sel->isfloating))
  1865. return;
  1866. if(c == nexttiled(selmon->clients))
  1867. if(!c || !(c = nexttiled(c->next)))
  1868. return;
  1869. detach(c);
  1870. attach(c);
  1871. focus(c);
  1872. arrange();
  1873. }
  1874. int
  1875. main(int argc, char *argv[]) {
  1876. if(argc == 2 && !strcmp("-v", argv[1]))
  1877. die("dwm-"VERSION", © 2006-2009 dwm engineers, see LICENSE for details\n");
  1878. else if(argc != 1)
  1879. die("usage: dwm [-v]\n");
  1880. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1881. fputs("warning: no locale support\n", stderr);
  1882. if(!(dpy = XOpenDisplay(NULL)))
  1883. die("dwm: cannot open display\n");
  1884. checkotherwm();
  1885. setup();
  1886. scan();
  1887. run();
  1888. cleanup();
  1889. XCloseDisplay(dpy);
  1890. return 0;
  1891. }