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.

2085 lines
51 KiB

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