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.

2035 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
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
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. if((m = dirtomon(arg->i)) == selmon)
  785. return;
  786. unfocus(selmon->sel);
  787. selmon = m;
  788. focus(NULL);
  789. }
  790. void
  791. focusstack(const Arg *arg) {
  792. Client *c = NULL, *i;
  793. if(!selmon->sel)
  794. return;
  795. if(arg->i > 0) {
  796. for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
  797. if(!c)
  798. for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
  799. }
  800. else {
  801. for(i = selmon->clients; i != selmon->sel; i = i->next)
  802. if(ISVISIBLE(i))
  803. c = i;
  804. if(!c)
  805. for(; i; i = i->next)
  806. if(ISVISIBLE(i))
  807. c = i;
  808. }
  809. if(c) {
  810. focus(c);
  811. restack(selmon);
  812. }
  813. }
  814. unsigned long
  815. getcolor(const char *colstr) {
  816. Colormap cmap = DefaultColormap(dpy, screen);
  817. XColor color;
  818. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  819. die("error, cannot allocate color '%s'\n", colstr);
  820. return color.pixel;
  821. }
  822. Bool
  823. getrootptr(int *x, int *y) {
  824. int di;
  825. unsigned int dui;
  826. Window dummy;
  827. return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
  828. }
  829. long
  830. getstate(Window w) {
  831. int format, status;
  832. long result = -1;
  833. unsigned char *p = NULL;
  834. unsigned long n, extra;
  835. Atom real;
  836. status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  837. &real, &format, &n, &extra, (unsigned char **)&p);
  838. if(status != Success)
  839. return -1;
  840. if(n != 0)
  841. result = *p;
  842. XFree(p);
  843. return result;
  844. }
  845. Bool
  846. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  847. char **list = NULL;
  848. int n;
  849. XTextProperty name;
  850. if(!text || size == 0)
  851. return False;
  852. text[0] = '\0';
  853. XGetTextProperty(dpy, w, &name, atom);
  854. if(!name.nitems)
  855. return False;
  856. if(name.encoding == XA_STRING)
  857. strncpy(text, (char *)name.value, size - 1);
  858. else {
  859. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
  860. strncpy(text, *list, size - 1);
  861. XFreeStringList(list);
  862. }
  863. }
  864. text[size - 1] = '\0';
  865. XFree(name.value);
  866. return True;
  867. }
  868. void
  869. grabbuttons(Client *c, Bool focused) {
  870. updatenumlockmask();
  871. {
  872. unsigned int i, j;
  873. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  874. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  875. if(focused) {
  876. for(i = 0; i < LENGTH(buttons); i++)
  877. if(buttons[i].click == ClkClientWin)
  878. for(j = 0; j < LENGTH(modifiers); j++)
  879. XGrabButton(dpy, buttons[i].button,
  880. buttons[i].mask | modifiers[j],
  881. c->win, False, BUTTONMASK,
  882. GrabModeAsync, GrabModeSync, None, None);
  883. }
  884. else
  885. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  886. BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
  887. }
  888. }
  889. void
  890. grabkeys(void) {
  891. updatenumlockmask();
  892. {
  893. unsigned int i, j;
  894. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  895. KeyCode code;
  896. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  897. for(i = 0; i < LENGTH(keys); i++) {
  898. if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
  899. for(j = 0; j < LENGTH(modifiers); j++)
  900. XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
  901. True, GrabModeAsync, GrabModeAsync);
  902. }
  903. }
  904. }
  905. void
  906. initfont(const char *fontstr) {
  907. char *def, **missing;
  908. int i, n;
  909. missing = NULL;
  910. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  911. if(missing) {
  912. while(n--)
  913. fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
  914. XFreeStringList(missing);
  915. }
  916. if(dc.font.set) {
  917. XFontSetExtents *font_extents;
  918. XFontStruct **xfonts;
  919. char **font_names;
  920. dc.font.ascent = dc.font.descent = 0;
  921. font_extents = XExtentsOfFontSet(dc.font.set);
  922. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  923. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  924. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  925. dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
  926. xfonts++;
  927. }
  928. }
  929. else {
  930. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  931. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
  932. die("error, cannot load font: '%s'\n", fontstr);
  933. dc.font.ascent = dc.font.xfont->ascent;
  934. dc.font.descent = dc.font.xfont->descent;
  935. }
  936. dc.font.height = dc.font.ascent + dc.font.descent;
  937. }
  938. Bool
  939. isprotodel(Client *c) {
  940. int i, n;
  941. Atom *protocols;
  942. Bool ret = False;
  943. if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  944. for(i = 0; !ret && i < n; i++)
  945. if(protocols[i] == wmatom[WMDelete])
  946. ret = True;
  947. XFree(protocols);
  948. }
  949. return ret;
  950. }
  951. #ifdef XINERAMA
  952. static Bool
  953. isuniquegeom(XineramaScreenInfo *unique, size_t len, XineramaScreenInfo *info) {
  954. unsigned int i;
  955. for(i = 0; i < len; i++)
  956. if(unique[i].x_org == info->x_org && unique[i].y_org == info->y_org
  957. && unique[i].width == info->width && unique[i].height == info->height)
  958. return False;
  959. return True;
  960. }
  961. #endif /* XINERAMA */
  962. void
  963. keypress(XEvent *e) {
  964. unsigned int i;
  965. KeySym keysym;
  966. XKeyEvent *ev;
  967. ev = &e->xkey;
  968. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  969. for(i = 0; i < LENGTH(keys); i++)
  970. if(keysym == keys[i].keysym
  971. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  972. && keys[i].func)
  973. keys[i].func(&(keys[i].arg));
  974. }
  975. void
  976. killclient(const Arg *arg) {
  977. XEvent ev;
  978. if(!selmon->sel)
  979. return;
  980. if(isprotodel(selmon->sel)) {
  981. ev.type = ClientMessage;
  982. ev.xclient.window = selmon->sel->win;
  983. ev.xclient.message_type = wmatom[WMProtocols];
  984. ev.xclient.format = 32;
  985. ev.xclient.data.l[0] = wmatom[WMDelete];
  986. ev.xclient.data.l[1] = CurrentTime;
  987. XSendEvent(dpy, selmon->sel->win, False, NoEventMask, &ev);
  988. }
  989. else {
  990. XGrabServer(dpy);
  991. XSetErrorHandler(xerrordummy);
  992. XSetCloseDownMode(dpy, DestroyAll);
  993. XKillClient(dpy, selmon->sel->win);
  994. XSync(dpy, False);
  995. XSetErrorHandler(xerror);
  996. XUngrabServer(dpy);
  997. }
  998. }
  999. void
  1000. manage(Window w, XWindowAttributes *wa) {
  1001. static Client cz;
  1002. Client *c, *t = NULL;
  1003. Window trans = None;
  1004. XWindowChanges wc;
  1005. if(!(c = malloc(sizeof(Client))))
  1006. die("fatal: could not malloc() %u bytes\n", sizeof(Client));
  1007. *c = cz;
  1008. c->win = w;
  1009. updatetitle(c);
  1010. if(XGetTransientForHint(dpy, w, &trans))
  1011. t = wintoclient(trans);
  1012. if(t) {
  1013. c->mon = t->mon;
  1014. c->tags = t->tags;
  1015. }
  1016. else {
  1017. c->mon = selmon;
  1018. applyrules(c);
  1019. }
  1020. /* geometry */
  1021. c->x = wa->x + c->mon->wx;
  1022. c->y = wa->y + c->mon->wy;
  1023. c->w = wa->width;
  1024. c->h = wa->height;
  1025. c->oldbw = wa->border_width;
  1026. if(c->w == c->mon->mw && c->h == c->mon->mh) {
  1027. c->x = c->mon->mx;
  1028. c->y = c->mon->my;
  1029. c->bw = 0;
  1030. }
  1031. else {
  1032. if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
  1033. c->x = c->mon->mx + c->mon->mw - WIDTH(c);
  1034. if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
  1035. c->y = c->mon->my + c->mon->mh - HEIGHT(c);
  1036. c->x = MAX(c->x, c->mon->mx);
  1037. /* only fix client y-offset, if the client center might cover the bar */
  1038. c->y = MAX(c->y, ((c->mon->by == 0) && (c->x + (c->w / 2) >= c->mon->wx)
  1039. && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
  1040. c->bw = borderpx;
  1041. }
  1042. wc.border_width = c->bw;
  1043. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  1044. XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
  1045. configure(c); /* propagates border_width, if size doesn't change */
  1046. updatesizehints(c);
  1047. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  1048. grabbuttons(c, False);
  1049. if(!c->isfloating)
  1050. c->isfloating = trans != None || c->isfixed;
  1051. if(c->isfloating)
  1052. XRaiseWindow(dpy, c->win);
  1053. attach(c);
  1054. attachstack(c);
  1055. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  1056. XMapWindow(dpy, c->win);
  1057. setclientstate(c, NormalState);
  1058. arrange(c->mon);
  1059. }
  1060. void
  1061. mappingnotify(XEvent *e) {
  1062. XMappingEvent *ev = &e->xmapping;
  1063. XRefreshKeyboardMapping(ev);
  1064. if(ev->request == MappingKeyboard)
  1065. grabkeys();
  1066. }
  1067. void
  1068. maprequest(XEvent *e) {
  1069. static XWindowAttributes wa;
  1070. XMapRequestEvent *ev = &e->xmaprequest;
  1071. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  1072. return;
  1073. if(wa.override_redirect)
  1074. return;
  1075. if(!wintoclient(ev->window))
  1076. manage(ev->window, &wa);
  1077. }
  1078. void
  1079. monocle(Monitor *m) {
  1080. unsigned int n = 0;
  1081. Client *c;
  1082. for(c = m->clients; c; c = c->next)
  1083. if(ISVISIBLE(c))
  1084. n++;
  1085. if(n > 0) /* override layout symbol */
  1086. snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
  1087. for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
  1088. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False);
  1089. }
  1090. void
  1091. movemouse(const Arg *arg) {
  1092. int x, y, ocx, ocy, nx, ny;
  1093. Client *c;
  1094. Monitor *m;
  1095. XEvent ev;
  1096. if(!(c = selmon->sel))
  1097. return;
  1098. restack(selmon);
  1099. ocx = c->x;
  1100. ocy = c->y;
  1101. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1102. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  1103. return;
  1104. if(!getrootptr(&x, &y))
  1105. return;
  1106. do {
  1107. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1108. switch (ev.type) {
  1109. case ConfigureRequest:
  1110. case Expose:
  1111. case MapRequest:
  1112. handler[ev.type](&ev);
  1113. break;
  1114. case MotionNotify:
  1115. nx = ocx + (ev.xmotion.x - x);
  1116. ny = ocy + (ev.xmotion.y - y);
  1117. if(snap && nx >= selmon->wx && nx <= selmon->wx + selmon->ww
  1118. && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
  1119. if(abs(selmon->wx - nx) < snap)
  1120. nx = selmon->wx;
  1121. else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1122. nx = selmon->wx + selmon->ww - WIDTH(c);
  1123. if(abs(selmon->wy - ny) < snap)
  1124. ny = selmon->wy;
  1125. else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1126. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1127. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1128. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1129. togglefloating(NULL);
  1130. }
  1131. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1132. resize(c, nx, ny, c->w, c->h, True);
  1133. break;
  1134. }
  1135. } while(ev.type != ButtonRelease);
  1136. XUngrabPointer(dpy, CurrentTime);
  1137. if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
  1138. sendmon(c, m);
  1139. selmon = m;
  1140. focus(NULL);
  1141. }
  1142. }
  1143. Client *
  1144. nexttiled(Client *c) {
  1145. for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  1146. return c;
  1147. }
  1148. Monitor *
  1149. ptrtomon(int x, int y) {
  1150. Monitor *m;
  1151. for(m = mons; m; m = m->next)
  1152. if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh))
  1153. return m;
  1154. return selmon;
  1155. }
  1156. void
  1157. propertynotify(XEvent *e) {
  1158. Client *c;
  1159. Window trans;
  1160. XPropertyEvent *ev = &e->xproperty;
  1161. if((ev->window == root) && (ev->atom == XA_WM_NAME))
  1162. updatestatus();
  1163. else if(ev->state == PropertyDelete)
  1164. return; /* ignore */
  1165. else if((c = wintoclient(ev->window))) {
  1166. switch (ev->atom) {
  1167. default: break;
  1168. case XA_WM_TRANSIENT_FOR:
  1169. XGetTransientForHint(dpy, c->win, &trans);
  1170. if(!c->isfloating && (c->isfloating = (wintoclient(trans) != NULL)))
  1171. arrange(c->mon);
  1172. break;
  1173. case XA_WM_NORMAL_HINTS:
  1174. updatesizehints(c);
  1175. break;
  1176. case XA_WM_HINTS:
  1177. updatewmhints(c);
  1178. drawbars();
  1179. break;
  1180. }
  1181. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1182. updatetitle(c);
  1183. if(c == c->mon->sel)
  1184. drawbar(c->mon);
  1185. }
  1186. }
  1187. }
  1188. void
  1189. quit(const Arg *arg) {
  1190. running = False;
  1191. }
  1192. void
  1193. resize(Client *c, int x, int y, int w, int h, Bool interact) {
  1194. XWindowChanges wc;
  1195. if(applysizehints(c, &x, &y, &w, &h, interact)) {
  1196. c->x = wc.x = x;
  1197. c->y = wc.y = y;
  1198. c->w = wc.width = w;
  1199. c->h = wc.height = h;
  1200. wc.border_width = c->bw;
  1201. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1202. configure(c);
  1203. XSync(dpy, False);
  1204. }
  1205. }
  1206. void
  1207. resizemouse(const Arg *arg) {
  1208. int ocx, ocy;
  1209. int nw, nh;
  1210. Client *c;
  1211. Monitor *m;
  1212. XEvent ev;
  1213. if(!(c = selmon->sel))
  1214. return;
  1215. restack(selmon);
  1216. ocx = c->x;
  1217. ocy = c->y;
  1218. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1219. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  1220. return;
  1221. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1222. do {
  1223. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1224. switch(ev.type) {
  1225. case ConfigureRequest:
  1226. case Expose:
  1227. case MapRequest:
  1228. handler[ev.type](&ev);
  1229. break;
  1230. case MotionNotify:
  1231. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1232. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1233. if(snap && nw >= selmon->wx && nw <= selmon->wx + selmon->ww
  1234. && nh >= selmon->wy && nh <= selmon->wy + selmon->wh)
  1235. {
  1236. if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1237. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1238. togglefloating(NULL);
  1239. }
  1240. if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1241. resize(c, c->x, c->y, nw, nh, True);
  1242. break;
  1243. }
  1244. } while(ev.type != ButtonRelease);
  1245. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1246. XUngrabPointer(dpy, CurrentTime);
  1247. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1248. if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
  1249. sendmon(c, m);
  1250. selmon = m;
  1251. focus(NULL);
  1252. }
  1253. }
  1254. void
  1255. restack(Monitor *m) {
  1256. Client *c;
  1257. XEvent ev;
  1258. XWindowChanges wc;
  1259. drawbar(m);
  1260. if(!m->sel)
  1261. return;
  1262. if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
  1263. XRaiseWindow(dpy, m->sel->win);
  1264. if(m->lt[m->sellt]->arrange) {
  1265. wc.stack_mode = Below;
  1266. wc.sibling = m->barwin;
  1267. for(c = m->stack; c; c = c->snext)
  1268. if(!c->isfloating && ISVISIBLE(c)) {
  1269. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1270. wc.sibling = c->win;
  1271. }
  1272. }
  1273. XSync(dpy, False);
  1274. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1275. }
  1276. void
  1277. run(void) {
  1278. XEvent ev;
  1279. static const char *evname[LASTEvent] = {
  1280. [ButtonPress] = "buttonpress",
  1281. [ConfigureRequest] = "configurerequest",
  1282. [ConfigureNotify] = "configurenotify",
  1283. [DestroyNotify] = "destroynotify",
  1284. [EnterNotify] = "enternotify",
  1285. [Expose] = "expose",
  1286. [FocusIn] = "focusin",
  1287. [KeyPress] = "keypress",
  1288. [MappingNotify] = "mappingnotify",
  1289. [MapRequest] = "maprequest",
  1290. [PropertyNotify] = "propertynotify",
  1291. [UnmapNotify] = "unmapnotify"
  1292. };
  1293. /* main event loop */
  1294. XSync(dpy, False);
  1295. while(running && !XNextEvent(dpy, &ev)) {
  1296. D fprintf(stderr, "run event %s %ld\n", evname[ev.type], ev.xany.window);
  1297. if(handler[ev.type])
  1298. handler[ev.type](&ev); /* call handler */
  1299. }
  1300. }
  1301. void
  1302. scan(void) {
  1303. unsigned int i, num;
  1304. Window d1, d2, *wins = NULL;
  1305. XWindowAttributes wa;
  1306. if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1307. for(i = 0; i < num; i++) {
  1308. if(!XGetWindowAttributes(dpy, wins[i], &wa)
  1309. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1310. continue;
  1311. if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1312. manage(wins[i], &wa);
  1313. }
  1314. for(i = 0; i < num; i++) { /* now the transients */
  1315. if(!XGetWindowAttributes(dpy, wins[i], &wa))
  1316. continue;
  1317. if(XGetTransientForHint(dpy, wins[i], &d1)
  1318. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1319. manage(wins[i], &wa);
  1320. }
  1321. if(wins)
  1322. XFree(wins);
  1323. }
  1324. }
  1325. void
  1326. sendmon(Client *c, Monitor *m) {
  1327. if(c->mon == m)
  1328. return;
  1329. unfocus(c);
  1330. detach(c);
  1331. detachstack(c);
  1332. c->mon = m;
  1333. c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
  1334. attach(c);
  1335. attachstack(c);
  1336. focus(NULL);
  1337. arrange(NULL);
  1338. }
  1339. void
  1340. setclientstate(Client *c, long state) {
  1341. long data[] = { state, None };
  1342. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1343. PropModeReplace, (unsigned char *)data, 2);
  1344. }
  1345. void
  1346. setlayout(const Arg *arg) {
  1347. if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
  1348. selmon->sellt ^= 1;
  1349. if(arg && arg->v)
  1350. selmon->lt[selmon->sellt] = (Layout *)arg->v;
  1351. strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
  1352. if(selmon->sel)
  1353. arrange(selmon);
  1354. else
  1355. drawbar(selmon);
  1356. }
  1357. /* arg > 1.0 will set mfact absolutly */
  1358. void
  1359. setmfact(const Arg *arg) {
  1360. float f;
  1361. if(!arg || !selmon->lt[selmon->sellt]->arrange)
  1362. return;
  1363. f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  1364. if(f < 0.1 || f > 0.9)
  1365. return;
  1366. selmon->mfact = f;
  1367. arrange(selmon);
  1368. }
  1369. void
  1370. setup(void) {
  1371. XSetWindowAttributes wa;
  1372. /* clean up any zombies immediately */
  1373. sigchld(0);
  1374. /* init screen */
  1375. screen = DefaultScreen(dpy);
  1376. root = RootWindow(dpy, screen);
  1377. initfont(font);
  1378. sw = DisplayWidth(dpy, screen);
  1379. sh = DisplayHeight(dpy, screen);
  1380. bh = dc.h = dc.font.height + 2;
  1381. updategeom();
  1382. /* init atoms */
  1383. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1384. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1385. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1386. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1387. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1388. /* init cursors */
  1389. cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
  1390. cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
  1391. cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
  1392. /* init appearance */
  1393. dc.norm[ColBorder] = getcolor(normbordercolor);
  1394. dc.norm[ColBG] = getcolor(normbgcolor);
  1395. dc.norm[ColFG] = getcolor(normfgcolor);
  1396. dc.sel[ColBorder] = getcolor(selbordercolor);
  1397. dc.sel[ColBG] = getcolor(selbgcolor);
  1398. dc.sel[ColFG] = getcolor(selfgcolor);
  1399. dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
  1400. dc.gc = XCreateGC(dpy, root, 0, NULL);
  1401. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  1402. if(!dc.font.set)
  1403. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  1404. /* init bars */
  1405. updatebars();
  1406. updatestatus();
  1407. /* EWMH support per view */
  1408. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1409. PropModeReplace, (unsigned char *) netatom, NetLast);
  1410. /* select for events */
  1411. wa.cursor = cursor[CurNormal];
  1412. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
  1413. |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
  1414. |PropertyChangeMask;
  1415. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1416. XSelectInput(dpy, root, wa.event_mask);
  1417. grabkeys();
  1418. }
  1419. void
  1420. showhide(Client *c) {
  1421. if(!c)
  1422. return;
  1423. if(ISVISIBLE(c)) { /* show clients top down */
  1424. XMoveWindow(dpy, c->win, c->x, c->y);
  1425. if(!c->mon->lt[c->mon->sellt]->arrange || c->isfloating)
  1426. resize(c, c->x, c->y, c->w, c->h, False);
  1427. showhide(c->snext);
  1428. }
  1429. else { /* hide clients bottom up */
  1430. showhide(c->snext);
  1431. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  1432. }
  1433. }
  1434. void
  1435. sigchld(int unused) {
  1436. if(signal(SIGCHLD, sigchld) == SIG_ERR)
  1437. die("Can't install SIGCHLD handler");
  1438. while(0 < waitpid(-1, NULL, WNOHANG));
  1439. }
  1440. void
  1441. spawn(const Arg *arg) {
  1442. if(fork() == 0) {
  1443. if(dpy)
  1444. close(ConnectionNumber(dpy));
  1445. setsid();
  1446. execvp(((char **)arg->v)[0], (char **)arg->v);
  1447. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1448. perror(" failed");
  1449. exit(0);
  1450. }
  1451. }
  1452. void
  1453. tag(const Arg *arg) {
  1454. if(selmon->sel && arg->ui & TAGMASK) {
  1455. selmon->sel->tags = arg->ui & TAGMASK;
  1456. arrange(selmon);
  1457. }
  1458. }
  1459. void
  1460. tagmon(const Arg *arg) {
  1461. if(!selmon->sel || !mons->next)
  1462. return;
  1463. sendmon(selmon->sel, dirtomon(arg->i));
  1464. }
  1465. int
  1466. textnw(const char *text, unsigned int len) {
  1467. XRectangle r;
  1468. if(dc.font.set) {
  1469. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  1470. return r.width;
  1471. }
  1472. return XTextWidth(dc.font.xfont, text, len);
  1473. }
  1474. void
  1475. tile(Monitor *m) {
  1476. int x, y, h, w, mw;
  1477. unsigned int i, n;
  1478. Client *c;
  1479. for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  1480. if(n == 0)
  1481. return;
  1482. /* master */
  1483. c = nexttiled(m->clients);
  1484. mw = m->mfact * m->ww;
  1485. resize(c, m->wx, m->wy, (n == 1 ? m->ww : mw) - 2 * c->bw, m->wh - 2 * c->bw, False);
  1486. if(--n == 0)
  1487. return;
  1488. /* tile stack */
  1489. x = (m->wx + mw > c->x + c->w) ? c->x + c->w + 2 * c->bw : m->wx + mw;
  1490. y = m->wy;
  1491. w = (m->wx + mw > c->x + c->w) ? m->wx + m->ww - x : m->ww - mw;
  1492. h = m->wh / n;
  1493. if(h < bh)
  1494. h = m->wh;
  1495. for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
  1496. resize(c, x, y, w - 2 * c->bw, /* remainder */ ((i + 1 == n)
  1497. ? m->wy + m->wh - y - 2 * c->bw : h - 2 * c->bw), False);
  1498. if(h != m->wh)
  1499. y = c->y + HEIGHT(c);
  1500. }
  1501. }
  1502. void
  1503. togglebar(const Arg *arg) {
  1504. selmon->showbar = !selmon->showbar;
  1505. updatebarpos(selmon);
  1506. XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
  1507. arrange(selmon);
  1508. }
  1509. void
  1510. togglefloating(const Arg *arg) {
  1511. if(!selmon->sel)
  1512. return;
  1513. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  1514. if(selmon->sel->isfloating)
  1515. resize(selmon->sel, selmon->sel->x, selmon->sel->y,
  1516. selmon->sel->w, selmon->sel->h, False);
  1517. arrange(selmon);
  1518. }
  1519. void
  1520. toggletag(const Arg *arg) {
  1521. unsigned int newtags;
  1522. if(!selmon->sel)
  1523. return;
  1524. newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
  1525. if(newtags) {
  1526. selmon->sel->tags = newtags;
  1527. arrange(selmon);
  1528. }
  1529. }
  1530. void
  1531. toggleview(const Arg *arg) {
  1532. unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  1533. if(newtagset) {
  1534. selmon->tagset[selmon->seltags] = newtagset;
  1535. arrange(selmon);
  1536. }
  1537. }
  1538. void
  1539. unfocus(Client *c) {
  1540. if(!c)
  1541. return;
  1542. grabbuttons(c, False);
  1543. XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
  1544. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  1545. }
  1546. void
  1547. unmanage(Client *c, Bool destroyed) {
  1548. Monitor *m = c->mon;
  1549. XWindowChanges wc;
  1550. /* The server grab construct avoids race conditions. */
  1551. detach(c);
  1552. detachstack(c);
  1553. if(!destroyed) {
  1554. wc.border_width = c->oldbw;
  1555. XGrabServer(dpy);
  1556. XSetErrorHandler(xerrordummy);
  1557. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1558. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1559. setclientstate(c, WithdrawnState);
  1560. XSync(dpy, False);
  1561. XSetErrorHandler(xerror);
  1562. XUngrabServer(dpy);
  1563. }
  1564. free(c);
  1565. focus(NULL);
  1566. arrange(m);
  1567. }
  1568. void
  1569. unmapnotify(XEvent *e) {
  1570. Client *c;
  1571. XUnmapEvent *ev = &e->xunmap;
  1572. if((c = wintoclient(ev->window)))
  1573. unmanage(c, False);
  1574. }
  1575. void
  1576. updatebars(void) {
  1577. Monitor *m;
  1578. XSetWindowAttributes wa;
  1579. wa.override_redirect = True;
  1580. wa.background_pixmap = ParentRelative;
  1581. wa.event_mask = ButtonPressMask|ExposureMask;
  1582. for(m = mons; m; m = m->next) {
  1583. m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
  1584. CopyFromParent, DefaultVisual(dpy, screen),
  1585. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1586. XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
  1587. XMapRaised(dpy, m->barwin);
  1588. }
  1589. }
  1590. void
  1591. updatebarpos(Monitor *m) {
  1592. m->wy = m->my;
  1593. m->wh = m->mh;
  1594. if(m->showbar) {
  1595. m->wh -= bh;
  1596. m->by = m->topbar ? m->wy : m->wy + m->wh;
  1597. m->wy = m->topbar ? m->wy + bh : m->wy;
  1598. }
  1599. else
  1600. m->by = -bh;
  1601. }
  1602. Bool
  1603. updategeom(void) {
  1604. Bool dirty = False;
  1605. #ifdef XINERAMA
  1606. if(XineramaIsActive(dpy)) {
  1607. int i, j, n, nn;
  1608. Client *c;
  1609. Monitor *m;
  1610. XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
  1611. XineramaScreenInfo *unique = NULL;
  1612. info = XineramaQueryScreens(dpy, &nn);
  1613. for(n = 0, m = mons; m; m = m->next, n++);
  1614. /* only consider unique geometries as separate screens */
  1615. if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
  1616. die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
  1617. for(i = 0, j = 0; i < nn; i++)
  1618. if(isuniquegeom(unique, j, &info[i]))
  1619. memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
  1620. XFree(info);
  1621. nn = j;
  1622. if(n <= nn) {
  1623. for(i = 0; i < (nn - n); i++) { /* new monitors available */
  1624. for(m = mons; m && m->next; m = m->next);
  1625. if(m)
  1626. m->next = createmon();
  1627. else
  1628. mons = createmon();
  1629. }
  1630. for(i = 0, m = mons; i < nn && m; m = m->next, i++)
  1631. if(i >= n
  1632. || (unique[i].x_org != m->mx || unique[i].y_org != m->my
  1633. || unique[i].width != m->mw || unique[i].height != m->mh))
  1634. {
  1635. dirty = True;
  1636. m->num = i;
  1637. m->mx = m->wx = unique[i].x_org;
  1638. m->my = m->wy = unique[i].y_org;
  1639. m->mw = m->ww = unique[i].width;
  1640. m->mh = m->wh = unique[i].height;
  1641. updatebarpos(m);
  1642. }
  1643. }
  1644. else { /* less monitors available nn < n */
  1645. for(i = nn; i < n; i++) {
  1646. for(m = mons; m && m->next; m = m->next);
  1647. while(m->clients) {
  1648. dirty = True;
  1649. c = m->clients;
  1650. m->clients = c->next;
  1651. detachstack(c);
  1652. c->mon = mons;
  1653. attach(c);
  1654. attachstack(c);
  1655. }
  1656. if(m == selmon)
  1657. selmon = mons;
  1658. cleanupmon(m);
  1659. }
  1660. }
  1661. free(unique);
  1662. }
  1663. else
  1664. #endif /* XINERAMA */
  1665. /* default monitor setup */
  1666. {
  1667. if(!mons)
  1668. mons = createmon();
  1669. if(mons->mw != sw || mons->mh != sh) {
  1670. dirty = True;
  1671. mons->mw = mons->ww = sw;
  1672. mons->mh = mons->wh = sh;
  1673. updatebarpos(mons);
  1674. }
  1675. }
  1676. if(dirty) {
  1677. selmon = mons;
  1678. selmon = wintomon(root);
  1679. }
  1680. return dirty;
  1681. }
  1682. void
  1683. updatenumlockmask(void) {
  1684. unsigned int i, j;
  1685. XModifierKeymap *modmap;
  1686. numlockmask = 0;
  1687. modmap = XGetModifierMapping(dpy);
  1688. for(i = 0; i < 8; i++)
  1689. for(j = 0; j < modmap->max_keypermod; j++)
  1690. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  1691. == XKeysymToKeycode(dpy, XK_Num_Lock))
  1692. numlockmask = (1 << i);
  1693. XFreeModifiermap(modmap);
  1694. }
  1695. void
  1696. updatesizehints(Client *c) {
  1697. long msize;
  1698. XSizeHints size;
  1699. if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
  1700. /* size is uninitialized, ensure that size.flags aren't used */
  1701. size.flags = PSize;
  1702. if(size.flags & PBaseSize) {
  1703. c->basew = size.base_width;
  1704. c->baseh = size.base_height;
  1705. }
  1706. else if(size.flags & PMinSize) {
  1707. c->basew = size.min_width;
  1708. c->baseh = size.min_height;
  1709. }
  1710. else
  1711. c->basew = c->baseh = 0;
  1712. if(size.flags & PResizeInc) {
  1713. c->incw = size.width_inc;
  1714. c->inch = size.height_inc;
  1715. }
  1716. else
  1717. c->incw = c->inch = 0;
  1718. if(size.flags & PMaxSize) {
  1719. c->maxw = size.max_width;
  1720. c->maxh = size.max_height;
  1721. }
  1722. else
  1723. c->maxw = c->maxh = 0;
  1724. if(size.flags & PMinSize) {
  1725. c->minw = size.min_width;
  1726. c->minh = size.min_height;
  1727. }
  1728. else if(size.flags & PBaseSize) {
  1729. c->minw = size.base_width;
  1730. c->minh = size.base_height;
  1731. }
  1732. else
  1733. c->minw = c->minh = 0;
  1734. if(size.flags & PAspect) {
  1735. c->mina = (float)size.min_aspect.y / size.min_aspect.x;
  1736. c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
  1737. }
  1738. else
  1739. c->maxa = c->mina = 0.0;
  1740. c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
  1741. && c->maxw == c->minw && c->maxh == c->minh);
  1742. }
  1743. void
  1744. updatetitle(Client *c) {
  1745. if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  1746. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  1747. if(c->name[0] == '\0') /* hack to mark broken clients */
  1748. strcpy(c->name, broken);
  1749. }
  1750. void
  1751. updatestatus(void) {
  1752. if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
  1753. strcpy(stext, "dwm-"VERSION);
  1754. drawbar(selmon);
  1755. }
  1756. void
  1757. updatewmhints(Client *c) {
  1758. XWMHints *wmh;
  1759. if((wmh = XGetWMHints(dpy, c->win))) {
  1760. if(c == selmon->sel && wmh->flags & XUrgencyHint) {
  1761. wmh->flags &= ~XUrgencyHint;
  1762. XSetWMHints(dpy, c->win, wmh);
  1763. }
  1764. else
  1765. c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
  1766. XFree(wmh);
  1767. }
  1768. }
  1769. void
  1770. view(const Arg *arg) {
  1771. if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  1772. return;
  1773. selmon->seltags ^= 1; /* toggle sel tagset */
  1774. if(arg->ui & TAGMASK)
  1775. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  1776. arrange(selmon);
  1777. }
  1778. Client *
  1779. wintoclient(Window w) {
  1780. Client *c;
  1781. Monitor *m;
  1782. for(m = mons; m; m = m->next)
  1783. for(c = m->clients; c; c = c->next)
  1784. if(c->win == w)
  1785. return c;
  1786. return NULL;
  1787. }
  1788. Monitor *
  1789. wintomon(Window w) {
  1790. int x, y;
  1791. Client *c;
  1792. Monitor *m;
  1793. if(w == root && getrootptr(&x, &y))
  1794. return ptrtomon(x, y);
  1795. for(m = mons; m; m = m->next)
  1796. if(w == m->barwin)
  1797. return m;
  1798. if((c = wintoclient(w)))
  1799. return c->mon;
  1800. return selmon;
  1801. }
  1802. /* There's no way to check accesses to destroyed windows, thus those cases are
  1803. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  1804. * default error handler, which may call exit. */
  1805. int
  1806. xerror(Display *dpy, XErrorEvent *ee) {
  1807. if(ee->error_code == BadWindow
  1808. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  1809. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  1810. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  1811. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  1812. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  1813. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  1814. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  1815. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  1816. return 0;
  1817. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  1818. ee->request_code, ee->error_code);
  1819. return xerrorxlib(dpy, ee); /* may call exit */
  1820. }
  1821. int
  1822. xerrordummy(Display *dpy, XErrorEvent *ee) {
  1823. return 0;
  1824. }
  1825. /* Startup Error handler to check if another window manager
  1826. * is already running. */
  1827. int
  1828. xerrorstart(Display *dpy, XErrorEvent *ee) {
  1829. otherwm = True;
  1830. return -1;
  1831. }
  1832. void
  1833. zoom(const Arg *arg) {
  1834. Client *c = selmon->sel;
  1835. if(!selmon->lt[selmon->sellt]->arrange
  1836. || selmon->lt[selmon->sellt]->arrange == monocle
  1837. || (selmon->sel && selmon->sel->isfloating))
  1838. return;
  1839. if(c == nexttiled(selmon->clients))
  1840. if(!c || !(c = nexttiled(c->next)))
  1841. return;
  1842. detach(c);
  1843. attach(c);
  1844. focus(c);
  1845. arrange(c->mon);
  1846. }
  1847. int
  1848. main(int argc, char *argv[]) {
  1849. if(argc == 2 && !strcmp("-v", argv[1]))
  1850. die("dwm-"VERSION", © 2006-2009 dwm engineers, see LICENSE for details\n");
  1851. else if(argc != 1)
  1852. die("usage: dwm [-v]\n");
  1853. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1854. fputs("warning: no locale support\n", stderr);
  1855. if(!(dpy = XOpenDisplay(NULL)))
  1856. die("dwm: cannot open display\n");
  1857. checkotherwm();
  1858. setup();
  1859. scan();
  1860. run();
  1861. cleanup();
  1862. XCloseDisplay(dpy);
  1863. return 0;
  1864. }