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.

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