My patched version of suckless' dwm.
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.

2275 lines
56 KiB

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