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.

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