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.

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