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.

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