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.

2038 lines
50 KiB

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