Configuration file for DWM on MacBook Air
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.

2141 lines
51 KiB

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