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.

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