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.

2039 lines
50 KiB

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