Another copy of my dotfiles. Because I don't completely trust GitHub.
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.

2768 lines
70 KiB

4 years ago
  1. /* See LICENSE file for copyright and license details.
  2. *
  3. * dynamic window manager is designed like any other X client as well. It is
  4. * driven through handling X events. In contrast to other X clients, a window
  5. * manager selects for SubstructureRedirectMask on the root window, to receive
  6. * events about window (dis-)appearance. Only one X connection at a time is
  7. * allowed to select for this event mask.
  8. *
  9. * The event handlers of dwm are organized in an array which is accessed
  10. * whenever a new event has been fetched. This allows event dispatching
  11. * in O(1) time.
  12. *
  13. * Each child of the root window is called a client, except windows which have
  14. * set the override_redirect flag. Clients are organized in a linked client
  15. * list on each monitor, the focus history is remembered through a stack list
  16. * on each monitor. Each client contains a bit array to indicate the tags of a
  17. * client.
  18. *
  19. * Keys and tagging rules are organized as arrays and defined in config.h.
  20. *
  21. * To understand everything else, start reading main().
  22. */
  23. #include <errno.h>
  24. #include <locale.h>
  25. #include <signal.h>
  26. #include <stdarg.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include <sys/types.h>
  32. #include <sys/wait.h>
  33. #include <X11/cursorfont.h>
  34. #include <X11/keysym.h>
  35. #include <X11/Xatom.h>
  36. #include <X11/Xlib.h>
  37. #include <X11/Xproto.h>
  38. #include <X11/Xutil.h>
  39. #ifdef XINERAMA
  40. #include <X11/extensions/Xinerama.h>
  41. #endif /* XINERAMA */
  42. #include <X11/Xft/Xft.h>
  43. #include <fcntl.h>
  44. #include <sys/prctl.h>
  45. #include "drw.h"
  46. #include "util.h"
  47. /* macros */
  48. #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
  49. #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
  50. #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
  51. * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
  52. #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
  53. #define LENGTH(X) (sizeof X / sizeof X[0])
  54. #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
  55. #define WIDTH(X) ((X)->w + 2 * (X)->bw + gappx)
  56. #define HEIGHT(X) ((X)->h + 2 * (X)->bw + gappx)
  57. #define TAGMASK ((1 << LENGTH(tags)) - 1)
  58. #define TAGSLENGTH (LENGTH(tags))
  59. #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
  60. #define TTEXTW(X) drw_fontset_getwidth(drw, (X))
  61. #define DWMBLOCKSLOCKFILE "/tmp/dwmblocks.pid"
  62. /* enums */
  63. enum { CurNormal, CurHand, CurResize, CurMove, CurLast }; /* cursor */
  64. enum { SchemeNorm, SchemeSel, SchemeStatus, SchemeTagsSel, SchemeTagsNorm, SchemeInfoSel, SchemeInfoNorm, SchemeCol1, SchemeCol2, SchemeCol3,
  65. SchemeCol4, SchemeCol5, SchemeCol6, SchemeCol7, SchemeCol8,
  66. SchemeCol9, SchemeCol10, SchemeCol11, SchemeCol12 }; /* color schemes */
  67. enum { NetSupported, NetWMName, NetWMState, NetWMCheck,
  68. NetWMFullscreen, NetActiveWindow, NetWMWindowType,
  69. NetWMWindowTypeDialog, NetClientList, NetClientListStacking, NetDesktopNames, NetDesktopViewport, NetNumberOfDesktops, NetCurrentDesktop, NetLast }; /* EWMH atoms */
  70. enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
  71. enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
  72. ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
  73. typedef union {
  74. int i;
  75. unsigned int ui;
  76. float f;
  77. const void *v;
  78. } Arg;
  79. typedef struct {
  80. unsigned int click;
  81. unsigned int mask;
  82. unsigned int button;
  83. void (*func)(const Arg *arg);
  84. const Arg arg;
  85. } Button;
  86. typedef struct Monitor Monitor;
  87. typedef struct Client Client;
  88. struct Client {
  89. char name[256];
  90. float mina, maxa;
  91. float cfact;
  92. int x, y, w, h;
  93. int oldx, oldy, oldw, oldh;
  94. int basew, baseh, incw, inch, maxw, maxh, minw, minh;
  95. int bw, oldbw;
  96. unsigned int tags;
  97. int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
  98. Client *next;
  99. Client *snext;
  100. Monitor *mon;
  101. Window win;
  102. };
  103. typedef struct {
  104. unsigned int mod;
  105. KeySym keysym;
  106. void (*func)(const Arg *);
  107. const Arg arg;
  108. } Key;
  109. typedef struct {
  110. const char *symbol;
  111. void (*arrange)(Monitor *);
  112. } Layout;
  113. typedef struct Pertag Pertag;
  114. struct Monitor {
  115. char ltsymbol[16];
  116. float mfact;
  117. int nmaster;
  118. int num;
  119. int by; /* bar geometry */
  120. int mx, my, mw, mh; /* screen size */
  121. int wx, wy, ww, wh; /* window area */
  122. unsigned int seltags;
  123. unsigned int sellt;
  124. unsigned int tagset[2];
  125. int rmaster;
  126. int showbar;
  127. int topbar;
  128. Client *clients;
  129. Client *sel;
  130. Client *stack;
  131. Monitor *next;
  132. Window barwin;
  133. const Layout *lt[2];
  134. Pertag *pertag;
  135. };
  136. typedef struct {
  137. const char *class;
  138. const char *instance;
  139. const char *title;
  140. unsigned int tags;
  141. int isfloating;
  142. int monitor;
  143. } Rule;
  144. /* function declarations */
  145. static void applyrules(Client *c);
  146. static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact);
  147. static void arrange(Monitor *m);
  148. static void arrangemon(Monitor *m);
  149. static void attach(Client *c);
  150. static void attachstack(Client *c);
  151. static void buttonpress(XEvent *e);
  152. static void checkotherwm(void);
  153. static void cleanup(void);
  154. static void cleanupmon(Monitor *mon);
  155. static void clientmessage(XEvent *e);
  156. static void configure(Client *c);
  157. static void configurenotify(XEvent *e);
  158. static void configurerequest(XEvent *e);
  159. static Monitor *createmon(void);
  160. static void cyclelayout(const Arg *arg);
  161. static void deck(Monitor *m);
  162. static void destroynotify(XEvent *e);
  163. static void detach(Client *c);
  164. static void detachstack(Client *c);
  165. static Monitor *dirtomon(int dir);
  166. static void drawbar(Monitor *m);
  167. static void drawbars(void);
  168. static void enternotify(XEvent *e);
  169. static void expose(XEvent *e);
  170. static void focus(Client *c);
  171. static void focusin(XEvent *e);
  172. static void focusmon(const Arg *arg);
  173. static void focusstack(const Arg *arg);
  174. static Atom getatomprop(Client *c, Atom prop);
  175. static int getrootptr(int *x, int *y);
  176. static long getstate(Window w);
  177. static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
  178. static void grabbuttons(Client *c, int focused);
  179. static void grabkeys(void);
  180. static void incnmaster(const Arg *arg);
  181. static void inplacerotate(const Arg *arg);
  182. static void keypress(XEvent *e);
  183. static void killclient(const Arg *arg);
  184. static void manage(Window w, XWindowAttributes *wa);
  185. static void mappingnotify(XEvent *e);
  186. static void maprequest(XEvent *e);
  187. static void monocle(Monitor *m);
  188. static void motionnotify(XEvent *e);
  189. static void movemouse(const Arg *arg);
  190. static Client *nexttiled(Client *c);
  191. static void pop(Client *);
  192. static void propertynotify(XEvent *e);
  193. static void quit(const Arg *arg);
  194. static Monitor *recttomon(int x, int y, int w, int h);
  195. static void resize(Client *c, int x, int y, int w, int h, int interact);
  196. static void resizeclient(Client *c, int x, int y, int w, int h);
  197. static void resizemouse(const Arg *arg);
  198. static void restack(Monitor *m);
  199. static void run(void);
  200. static void scan(void);
  201. static int sendevent(Client *c, Atom proto);
  202. static void sendmon(Client *c, Monitor *m);
  203. static void setclientstate(Client *c, long state);
  204. static void setcurrentdesktop(void);
  205. static void setdesktopnames(void);
  206. static void setfocus(Client *c);
  207. static void setfullscreen(Client *c, int fullscreen);
  208. static void setlayout(const Arg *arg);
  209. static void setcfact(const Arg *arg);
  210. static void setmfact(const Arg *arg);
  211. static void setnumdesktops(void);
  212. static void setup(void);
  213. static void setviewport(void);
  214. static void seturgent(Client *c, int urg);
  215. static void showhide(Client *c);
  216. static void sigchld(int unused);
  217. static void sigdwmblocks(const Arg *arg);
  218. static void spawn(const Arg *arg);
  219. static void switchcol(const Arg *arg);
  220. static void tag(const Arg *arg);
  221. static void tagmon(const Arg *arg);
  222. static void tile(Monitor *);
  223. static void togglebar(const Arg *arg);
  224. static void togglefloating(const Arg *arg);
  225. static void togglermaster(const Arg *arg);
  226. static void togglefullscr(const Arg *arg);
  227. static void toggletag(const Arg *arg);
  228. static void toggleview(const Arg *arg);
  229. static void unfocus(Client *c, int setfocus);
  230. static void unmanage(Client *c, int destroyed);
  231. static void unmapnotify(XEvent *e);
  232. static void updatecurrentdesktop(void);
  233. static void updatebarcursor(int cursorpos);
  234. static void updatebarpos(Monitor *m);
  235. static void updatebars(void);
  236. static void updateclientlist(void);
  237. static void updatedwmblockssig(int x, int e);
  238. static int updategeom(void);
  239. static void updatenumlockmask(void);
  240. static void updatesizehints(Client *c);
  241. static void updatestatus(void);
  242. static void updatetitle(Client *c);
  243. static void updatewindowtype(Client *c);
  244. static void updatewmhints(Client *c);
  245. static void view(const Arg *arg);
  246. static Client *wintoclient(Window w);
  247. static Monitor *wintomon(Window w);
  248. static void winview(const Arg* arg);
  249. static int xerror(Display *dpy, XErrorEvent *ee);
  250. static int xerrordummy(Display *dpy, XErrorEvent *ee);
  251. static int xerrorstart(Display *dpy, XErrorEvent *ee);
  252. static void zoom(const Arg *arg);
  253. static void centeredmaster(Monitor *m);
  254. static void centeredfloatingmaster(Monitor *m);
  255. /* variables */
  256. static const char broken[] = "broken";
  257. static char stextc[256];
  258. static char stexts[256];
  259. static int wstext;
  260. static int dwmblockssig;
  261. static int screen;
  262. static int sw, sh; /* X display screen geometry width, height */
  263. static int bh, blw = 0; /* bar geometry */
  264. static int lrpad; /* sum of left and right padding for text */
  265. static int vp; /* vertical padding for bar */
  266. static int sp; /* side padding for bar */
  267. static int (*xerrorxlib)(Display *, XErrorEvent *);
  268. static unsigned int numlockmask = 0;
  269. static void (*handler[LASTEvent]) (XEvent *) = {
  270. [ButtonPress] = buttonpress,
  271. [ClientMessage] = clientmessage,
  272. [ConfigureRequest] = configurerequest,
  273. [ConfigureNotify] = configurenotify,
  274. [DestroyNotify] = destroynotify,
  275. [Expose] = expose,
  276. [FocusIn] = focusin,
  277. [KeyPress] = keypress,
  278. [MappingNotify] = mappingnotify,
  279. [MapRequest] = maprequest,
  280. [MotionNotify] = motionnotify,
  281. [PropertyNotify] = propertynotify,
  282. [UnmapNotify] = unmapnotify
  283. };
  284. static Atom wmatom[WMLast], netatom[NetLast];
  285. static int running = 1;
  286. static Cur *cursor[CurLast];
  287. static Clr **scheme;
  288. static Display *dpy;
  289. static Drw *drw;
  290. static Monitor *mons, *selmon;
  291. static Window root, wmcheckwin;
  292. /* configuration, allows nested code to access above variables */
  293. #include "config.h"
  294. struct Pertag {
  295. unsigned int curtag, prevtag; /* current and previous tag */
  296. int nmasters[LENGTH(tags) + 1]; /* number of windows in master area */
  297. float mfacts[LENGTH(tags) + 1]; /* mfacts per tag */
  298. unsigned int sellts[LENGTH(tags) + 1]; /* selected layouts */
  299. const Layout *ltidxs[LENGTH(tags) + 1][2]; /* matrix of tags and layouts indexes */
  300. int showbars[LENGTH(tags) + 1]; /* display bar for the current tag */
  301. };
  302. /* compile-time check if all tags fit into an unsigned int bit array. */
  303. struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
  304. /* function implementations */
  305. void
  306. applyrules(Client *c)
  307. {
  308. const char *class, *instance;
  309. unsigned int i;
  310. const Rule *r;
  311. Monitor *m;
  312. XClassHint ch = { NULL, NULL };
  313. /* rule matching */
  314. c->isfloating = 0;
  315. c->tags = 0;
  316. XGetClassHint(dpy, c->win, &ch);
  317. class = ch.res_class ? ch.res_class : broken;
  318. instance = ch.res_name ? ch.res_name : broken;
  319. for (i = 0; i < LENGTH(rules); i++) {
  320. r = &rules[i];
  321. if ((!r->title || strstr(c->name, r->title))
  322. && (!r->class || strstr(class, r->class))
  323. && (!r->instance || strstr(instance, r->instance)))
  324. {
  325. c->isfloating = r->isfloating;
  326. c->tags |= r->tags;
  327. for (m = mons; m && m->num != r->monitor; m = m->next);
  328. if (m)
  329. c->mon = m;
  330. }
  331. }
  332. if (ch.res_class)
  333. XFree(ch.res_class);
  334. if (ch.res_name)
  335. XFree(ch.res_name);
  336. c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
  337. }
  338. int
  339. applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact)
  340. {
  341. int baseismin;
  342. Monitor *m = c->mon;
  343. /* set minimum possible */
  344. *w = MAX(1, *w);
  345. *h = MAX(1, *h);
  346. if (interact) {
  347. if (*x > sw)
  348. *x = sw - WIDTH(c);
  349. if (*y > sh)
  350. *y = sh - HEIGHT(c);
  351. if (*x + *w + 2 * c->bw < 0)
  352. *x = 0;
  353. if (*y + *h + 2 * c->bw < 0)
  354. *y = 0;
  355. } else {
  356. if (*x >= m->wx + m->ww)
  357. *x = m->wx + m->ww - WIDTH(c);
  358. if (*y >= m->wy + m->wh)
  359. *y = m->wy + m->wh - HEIGHT(c);
  360. if (*x + *w + 2 * c->bw <= m->wx)
  361. *x = m->wx;
  362. if (*y + *h + 2 * c->bw <= m->wy)
  363. *y = m->wy;
  364. }
  365. if (*h < bh)
  366. *h = bh;
  367. if (*w < bh)
  368. *w = bh;
  369. if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
  370. /* see last two sentences in ICCCM 4.1.2.3 */
  371. baseismin = c->basew == c->minw && c->baseh == c->minh;
  372. if (!baseismin) { /* temporarily remove base dimensions */
  373. *w -= c->basew;
  374. *h -= c->baseh;
  375. }
  376. /* adjust for aspect limits */
  377. if (c->mina > 0 && c->maxa > 0) {
  378. if (c->maxa < (float)*w / *h)
  379. *w = *h * c->maxa + 0.5;
  380. else if (c->mina < (float)*h / *w)
  381. *h = *w * c->mina + 0.5;
  382. }
  383. if (baseismin) { /* increment calculation requires this */
  384. *w -= c->basew;
  385. *h -= c->baseh;
  386. }
  387. /* adjust for increment value */
  388. if (c->incw)
  389. *w -= *w % c->incw;
  390. if (c->inch)
  391. *h -= *h % c->inch;
  392. /* restore base dimensions */
  393. *w = MAX(*w + c->basew, c->minw);
  394. *h = MAX(*h + c->baseh, c->minh);
  395. if (c->maxw)
  396. *w = MIN(*w, c->maxw);
  397. if (c->maxh)
  398. *h = MIN(*h, c->maxh);
  399. }
  400. return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
  401. }
  402. void
  403. arrange(Monitor *m)
  404. {
  405. if (m)
  406. showhide(m->stack);
  407. else for (m = mons; m; m = m->next)
  408. showhide(m->stack);
  409. if (m) {
  410. arrangemon(m);
  411. restack(m);
  412. } else for (m = mons; m; m = m->next)
  413. arrangemon(m);
  414. }
  415. void
  416. arrangemon(Monitor *m)
  417. {
  418. strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
  419. if (m->lt[m->sellt]->arrange)
  420. m->lt[m->sellt]->arrange(m);
  421. }
  422. void
  423. attach(Client *c)
  424. {
  425. c->next = c->mon->clients;
  426. c->mon->clients = c;
  427. }
  428. void
  429. attachstack(Client *c)
  430. {
  431. c->snext = c->mon->stack;
  432. c->mon->stack = c;
  433. }
  434. void
  435. buttonpress(XEvent *e)
  436. {
  437. unsigned int i, x, click;
  438. Arg arg = {0};
  439. Client *c;
  440. Monitor *m;
  441. XButtonPressedEvent *ev = &e->xbutton;
  442. click = ClkRootWin;
  443. /* focus monitor if necessary */
  444. if ((m = wintomon(ev->window)) && m != selmon) {
  445. unfocus(selmon->sel, 1);
  446. selmon = m;
  447. focus(NULL);
  448. }
  449. if (ev->window == selmon->barwin) {
  450. i = x = 0;
  451. do
  452. x += TEXTW(tags[i]);
  453. while (ev->x >= x && ++i < LENGTH(tags));
  454. if (i < LENGTH(tags)) {
  455. click = ClkTagBar;
  456. arg.ui = 1 << i;
  457. } else if (ev->x < x + blw)
  458. click = ClkLtSymbol;
  459. else if (ev->x < selmon->ww - wstext)
  460. click = ClkWinTitle;
  461. // else if (ev->x < selmon->ww - lrpad / 2 && ev->x >= (x = selmon->ww - wstext + lrpad / 2)) {
  462. else if (ev->x < selmon->ww - lrpad / 2 && ev->x >= selmon->ww - wstext + lrpad / 2) {
  463. click = ClkStatusText;
  464. // updatedwmblockssig(x, ev->x);
  465. } else
  466. return;
  467. } else if ((c = wintoclient(ev->window))) {
  468. focus(c);
  469. restack(selmon);
  470. XAllowEvents(dpy, ReplayPointer, CurrentTime);
  471. click = ClkClientWin;
  472. }
  473. for (i = 0; i < LENGTH(buttons); i++)
  474. if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
  475. && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
  476. buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
  477. }
  478. void
  479. checkotherwm(void)
  480. {
  481. xerrorxlib = XSetErrorHandler(xerrorstart);
  482. /* this causes an error if some other window manager is running */
  483. XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
  484. XSync(dpy, False);
  485. XSetErrorHandler(xerror);
  486. XSync(dpy, False);
  487. }
  488. void
  489. cleanup(void)
  490. {
  491. Arg a = {.ui = ~0};
  492. Layout foo = { "", NULL };
  493. Monitor *m;
  494. size_t i;
  495. view(&a);
  496. selmon->lt[selmon->sellt] = &foo;
  497. for (m = mons; m; m = m->next)
  498. while (m->stack)
  499. unmanage(m->stack, 0);
  500. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  501. while (mons)
  502. cleanupmon(mons);
  503. for (i = 0; i < CurLast; i++)
  504. drw_cur_free(drw, cursor[i]);
  505. for (i = 0; i < LENGTH(colors); i++)
  506. free(scheme[i]);
  507. XDestroyWindow(dpy, wmcheckwin);
  508. drw_free(drw);
  509. XSync(dpy, False);
  510. XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  511. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  512. }
  513. void
  514. cleanupmon(Monitor *mon)
  515. {
  516. Monitor *m;
  517. if (mon == mons)
  518. mons = mons->next;
  519. else {
  520. for (m = mons; m && m->next != mon; m = m->next);
  521. m->next = mon->next;
  522. }
  523. XUnmapWindow(dpy, mon->barwin);
  524. XDestroyWindow(dpy, mon->barwin);
  525. free(mon);
  526. }
  527. void
  528. clientmessage(XEvent *e)
  529. {
  530. XClientMessageEvent *cme = &e->xclient;
  531. Client *c = wintoclient(cme->window);
  532. if (!c)
  533. return;
  534. if (cme->message_type == netatom[NetWMState]) {
  535. if (cme->data.l[1] == netatom[NetWMFullscreen]
  536. || cme->data.l[2] == netatom[NetWMFullscreen])
  537. setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
  538. || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
  539. } else if (cme->message_type == netatom[NetActiveWindow]) {
  540. if (c != selmon->sel && !c->isurgent)
  541. seturgent(c, 1);
  542. }
  543. }
  544. void
  545. configure(Client *c)
  546. {
  547. XConfigureEvent ce;
  548. ce.type = ConfigureNotify;
  549. ce.display = dpy;
  550. ce.event = c->win;
  551. ce.window = c->win;
  552. ce.x = c->x;
  553. ce.y = c->y;
  554. ce.width = c->w;
  555. ce.height = c->h;
  556. ce.border_width = c->bw;
  557. ce.above = None;
  558. ce.override_redirect = False;
  559. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
  560. }
  561. void
  562. configurenotify(XEvent *e)
  563. {
  564. Monitor *m;
  565. Client *c;
  566. XConfigureEvent *ev = &e->xconfigure;
  567. int dirty;
  568. /* TODO: updategeom handling sucks, needs to be simplified */
  569. if (ev->window == root) {
  570. dirty = (sw != ev->width || sh != ev->height);
  571. sw = ev->width;
  572. sh = ev->height;
  573. if (updategeom() || dirty) {
  574. drw_resize(drw, sw, bh);
  575. updatebars();
  576. for (m = mons; m; m = m->next) {
  577. for (c = m->clients; c; c = c->next)
  578. if (c->isfullscreen)
  579. resizeclient(c, m->mx, m->my, m->mw, m->mh);
  580. XMoveResizeWindow(dpy, m->barwin, m->wx + sp, m->by + vp, m->ww - 2 * sp, bh);
  581. }
  582. focus(NULL);
  583. arrange(NULL);
  584. }
  585. }
  586. }
  587. void
  588. configurerequest(XEvent *e)
  589. {
  590. Client *c;
  591. Monitor *m;
  592. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  593. XWindowChanges wc;
  594. if ((c = wintoclient(ev->window))) {
  595. if (ev->value_mask & CWBorderWidth)
  596. c->bw = ev->border_width;
  597. else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
  598. m = c->mon;
  599. if (ev->value_mask & CWX) {
  600. c->oldx = c->x;
  601. c->x = m->mx + ev->x;
  602. }
  603. if (ev->value_mask & CWY) {
  604. c->oldy = c->y;
  605. c->y = m->my + ev->y;
  606. }
  607. if (ev->value_mask & CWWidth) {
  608. c->oldw = c->w;
  609. c->w = ev->width;
  610. }
  611. if (ev->value_mask & CWHeight) {
  612. c->oldh = c->h;
  613. c->h = ev->height;
  614. }
  615. if ((c->x + c->w) > m->mx + m->mw && c->isfloating)
  616. c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
  617. if ((c->y + c->h) > m->my + m->mh && c->isfloating)
  618. c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
  619. if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
  620. configure(c);
  621. if (ISVISIBLE(c))
  622. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  623. } else
  624. configure(c);
  625. } else {
  626. wc.x = ev->x;
  627. wc.y = ev->y;
  628. wc.width = ev->width;
  629. wc.height = ev->height;
  630. wc.border_width = ev->border_width;
  631. wc.sibling = ev->above;
  632. wc.stack_mode = ev->detail;
  633. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  634. }
  635. XSync(dpy, False);
  636. }
  637. Monitor *
  638. createmon(void)
  639. {
  640. Monitor *m;
  641. unsigned int i;
  642. m = ecalloc(1, sizeof(Monitor));
  643. m->tagset[0] = m->tagset[1] = 1;
  644. m->mfact = mfact;
  645. m->nmaster = nmaster;
  646. m->rmaster = rmaster;
  647. m->showbar = showbar;
  648. m->topbar = topbar;
  649. m->lt[0] = &layouts[0];
  650. m->lt[1] = &layouts[1 % LENGTH(layouts)];
  651. strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
  652. m->pertag = ecalloc(1, sizeof(Pertag));
  653. m->pertag->curtag = m->pertag->prevtag = 1;
  654. for (i = 0; i <= LENGTH(tags); i++) {
  655. m->pertag->nmasters[i] = m->nmaster;
  656. m->pertag->mfacts[i] = m->mfact;
  657. m->pertag->ltidxs[i][0] = m->lt[0];
  658. m->pertag->ltidxs[i][1] = m->lt[1];
  659. m->pertag->sellts[i] = m->sellt;
  660. m->pertag->showbars[i] = m->showbar;
  661. }
  662. return m;
  663. }
  664. void
  665. cyclelayout(const Arg *arg) {
  666. Layout *l;
  667. for(l = (Layout *)layouts; l != selmon->lt[selmon->sellt]; l++);
  668. if(arg->i > 0) {
  669. if(l->symbol && (l + 1)->symbol)
  670. setlayout(&((Arg) { .v = (l + 1) }));
  671. else
  672. setlayout(&((Arg) { .v = layouts }));
  673. } else {
  674. if(l != layouts && (l - 1)->symbol)
  675. setlayout(&((Arg) { .v = (l - 1) }));
  676. else
  677. setlayout(&((Arg) { .v = &layouts[LENGTH(layouts) - 2] }));
  678. }
  679. }
  680. void
  681. deck(Monitor *m) {
  682. unsigned int i, n, h, mw, my;
  683. float mfacts = 0;
  684. Client *c;
  685. for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++) {
  686. if (n < m->nmaster)
  687. mfacts += c->cfact;
  688. }
  689. if(n == 0)
  690. return;
  691. if(n > m->nmaster) {
  692. mw = m->nmaster ? m->ww * m->mfact : 0;
  693. snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n - m->nmaster);
  694. }
  695. else
  696. mw = m->ww;
  697. for(i = my = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  698. if(i < m->nmaster) {
  699. h = (m->wh - my) * (c->cfact / mfacts);
  700. resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False);
  701. if (my + HEIGHT(c) < m->wh)
  702. my += HEIGHT(c);
  703. mfacts -= c->cfact;
  704. }
  705. else
  706. resize(c, m->wx + mw, m->wy, m->ww - mw - (2*c->bw), m->wh - (2*c->bw), False);
  707. }
  708. void
  709. destroynotify(XEvent *e)
  710. {
  711. Client *c;
  712. XDestroyWindowEvent *ev = &e->xdestroywindow;
  713. if ((c = wintoclient(ev->window)))
  714. unmanage(c, 1);
  715. }
  716. void
  717. detach(Client *c)
  718. {
  719. Client **tc;
  720. for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
  721. *tc = c->next;
  722. }
  723. void
  724. detachstack(Client *c)
  725. {
  726. Client **tc, *t;
  727. for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
  728. *tc = c->snext;
  729. if (c == c->mon->sel) {
  730. for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
  731. c->mon->sel = t;
  732. }
  733. }
  734. Monitor *
  735. dirtomon(int dir)
  736. {
  737. Monitor *m = NULL;
  738. if (dir > 0) {
  739. if (!(m = selmon->next))
  740. m = mons;
  741. } else if (selmon == mons)
  742. for (m = mons; m->next; m = m->next);
  743. else
  744. for (m = mons; m->next != selmon; m = m->next);
  745. return m;
  746. }
  747. void
  748. drawbar(Monitor *m)
  749. {
  750. int x, w;
  751. int boxs = drw->fonts->h / 9;
  752. int boxw = drw->fonts->h / 6 + 2;
  753. unsigned int i, occ = 0, urg = 0;
  754. Client *c;
  755. /* draw status first so it can be overdrawn by tags later */
  756. if (m == selmon) { /* status is only drawn on selected monitor */
  757. char *ts = stextc;
  758. char *tp = stextc;
  759. char ctmp;
  760. drw_setscheme(drw, scheme[SchemeStatus]);
  761. x = drw_text(drw, m->ww - wstext -2 * sp, 0, lrpad / 2, bh, 0, "", 0); /* to keep left padding clean */
  762. for (;;) {
  763. if ((unsigned char)*ts > LENGTH(colors) + 10) {
  764. ts++;
  765. continue;
  766. }
  767. ctmp = *ts;
  768. *ts = '\0';
  769. if (*tp != '\0')
  770. x = drw_text(drw, x, 0, TTEXTW(tp), bh, 0, tp, 0);
  771. if (ctmp == '\0')
  772. break;
  773. /* - 11 to compensate for + 10 above */
  774. drw_setscheme(drw, scheme[(unsigned char)ctmp - 11]);
  775. *ts = ctmp;
  776. tp = ++ts;
  777. }
  778. drw_setscheme(drw, scheme[SchemeNorm]);
  779. drw_text(drw, x, 0, m->ww - x, bh, 0, "", 0); /* to keep right padding clean */
  780. }
  781. for (c = m->clients; c; c = c->next) {
  782. occ |= c->tags;
  783. if (c->isurgent)
  784. urg |= c->tags;
  785. }
  786. x = 0;
  787. for (i = 0; i < LENGTH(tags); i++) {
  788. w = TEXTW(tags[i]);
  789. drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeTagsSel : SchemeTagsNorm]);
  790. drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i);
  791. if (occ & 1 << i)
  792. drw_rect(drw, x + boxs, boxs, boxw, boxw,
  793. m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
  794. urg & 1 << i);
  795. x += w;
  796. }
  797. w = blw = TEXTW(m->ltsymbol);
  798. drw_setscheme(drw, scheme[SchemeTagsNorm]);
  799. x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0);
  800. if ((w = m->ww - wstext - x) > bh) {
  801. if (m->sel) {
  802. drw_setscheme(drw, scheme[m == selmon ? SchemeInfoSel : SchemeInfoNorm]);
  803. drw_text(drw, x, 0, w -2 * sp, bh, lrpad / 2, m->sel->name, 0);
  804. if (m->sel->isfloating)
  805. drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
  806. } else {
  807. drw_setscheme(drw, scheme[SchemeInfoNorm]);
  808. drw_rect(drw, x, 0, w -2 * sp, bh, 1, 1);
  809. }
  810. }
  811. drw_map(drw, m->barwin, 0, 0, m->ww, bh);
  812. }
  813. void
  814. drawbars(void)
  815. {
  816. Monitor *m;
  817. for (m = mons; m; m = m->next)
  818. drawbar(m);
  819. }
  820. void
  821. enternotify(XEvent *e)
  822. {
  823. Client *c;
  824. Monitor *m;
  825. XCrossingEvent *ev = &e->xcrossing;
  826. if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
  827. return;
  828. c = wintoclient(ev->window);
  829. m = c ? c->mon : wintomon(ev->window);
  830. if (m != selmon) {
  831. unfocus(selmon->sel, 1);
  832. selmon = m;
  833. } else if (!c || c == selmon->sel)
  834. return;
  835. focus(c);
  836. }
  837. void
  838. expose(XEvent *e)
  839. {
  840. Monitor *m;
  841. XExposeEvent *ev = &e->xexpose;
  842. if (ev->count == 0 && (m = wintomon(ev->window)))
  843. drawbar(m);
  844. }
  845. void
  846. focus(Client *c)
  847. {
  848. if (!c || !ISVISIBLE(c))
  849. for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
  850. if (selmon->sel && selmon->sel != c)
  851. unfocus(selmon->sel, 0);
  852. if (c) {
  853. if (c->mon != selmon)
  854. selmon = c->mon;
  855. if (c->isurgent)
  856. seturgent(c, 0);
  857. detachstack(c);
  858. attachstack(c);
  859. grabbuttons(c, 1);
  860. XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel);
  861. setfocus(c);
  862. } else {
  863. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  864. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  865. }
  866. selmon->sel = c;
  867. drawbars();
  868. }
  869. /* there are some broken focus acquiring clients needing extra handling */
  870. void
  871. focusin(XEvent *e)
  872. {
  873. XFocusChangeEvent *ev = &e->xfocus;
  874. if (selmon->sel && ev->window != selmon->sel->win)
  875. setfocus(selmon->sel);
  876. }
  877. void
  878. focusmon(const Arg *arg)
  879. {
  880. Monitor *m;
  881. if (!mons->next)
  882. return;
  883. if ((m = dirtomon(arg->i)) == selmon)
  884. return;
  885. unfocus(selmon->sel, 0);
  886. selmon = m;
  887. focus(NULL);
  888. }
  889. void
  890. focusstack(const Arg *arg)
  891. {
  892. Client *c = NULL, *i;
  893. if (!selmon->sel)
  894. return;
  895. if (arg->i > 0) {
  896. for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
  897. if (!c)
  898. for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
  899. } else {
  900. for (i = selmon->clients; i != selmon->sel; i = i->next)
  901. if (ISVISIBLE(i))
  902. c = i;
  903. if (!c)
  904. for (; i; i = i->next)
  905. if (ISVISIBLE(i))
  906. c = i;
  907. }
  908. if (c) {
  909. focus(c);
  910. restack(selmon);
  911. }
  912. }
  913. Atom
  914. getatomprop(Client *c, Atom prop)
  915. {
  916. int di;
  917. unsigned long dl;
  918. unsigned char *p = NULL;
  919. Atom da, atom = None;
  920. if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
  921. &da, &di, &dl, &dl, &p) == Success && p) {
  922. atom = *(Atom *)p;
  923. XFree(p);
  924. }
  925. return atom;
  926. }
  927. int
  928. getrootptr(int *x, int *y)
  929. {
  930. int di;
  931. unsigned int dui;
  932. Window dummy;
  933. return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
  934. }
  935. long
  936. getstate(Window w)
  937. {
  938. int format;
  939. long result = -1;
  940. unsigned char *p = NULL;
  941. unsigned long n, extra;
  942. Atom real;
  943. if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
  944. &real, &format, &n, &extra, (unsigned char **)&p) != Success)
  945. return -1;
  946. if (n != 0)
  947. result = *p;
  948. XFree(p);
  949. return result;
  950. }
  951. int
  952. gettextprop(Window w, Atom atom, char *text, unsigned int size)
  953. {
  954. char **list = NULL;
  955. int n;
  956. XTextProperty name;
  957. if (!text || size == 0)
  958. return 0;
  959. text[0] = '\0';
  960. if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems)
  961. return 0;
  962. if (name.encoding == XA_STRING)
  963. strncpy(text, (char *)name.value, size - 1);
  964. else {
  965. if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
  966. strncpy(text, *list, size - 1);
  967. XFreeStringList(list);
  968. }
  969. }
  970. text[size - 1] = '\0';
  971. XFree(name.value);
  972. return 1;
  973. }
  974. void
  975. grabbuttons(Client *c, int focused)
  976. {
  977. updatenumlockmask();
  978. {
  979. unsigned int i, j;
  980. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  981. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  982. if (!focused)
  983. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  984. BUTTONMASK, GrabModeSync, GrabModeSync, None, None);
  985. for (i = 0; i < LENGTH(buttons); i++)
  986. if (buttons[i].click == ClkClientWin)
  987. for (j = 0; j < LENGTH(modifiers); j++)
  988. XGrabButton(dpy, buttons[i].button,
  989. buttons[i].mask | modifiers[j],
  990. c->win, False, BUTTONMASK,
  991. GrabModeAsync, GrabModeSync, None, None);
  992. }
  993. }
  994. void
  995. grabkeys(void)
  996. {
  997. updatenumlockmask();
  998. {
  999. unsigned int i, j;
  1000. unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
  1001. KeyCode code;
  1002. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  1003. for (i = 0; i < LENGTH(keys); i++)
  1004. if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
  1005. for (j = 0; j < LENGTH(modifiers); j++)
  1006. XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
  1007. True, GrabModeAsync, GrabModeAsync);
  1008. }
  1009. }
  1010. void
  1011. incnmaster(const Arg *arg)
  1012. {
  1013. selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag] = MAX(selmon->nmaster + arg->i, 0);
  1014. arrange(selmon);
  1015. }
  1016. #ifdef XINERAMA
  1017. static int
  1018. isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
  1019. {
  1020. while (n--)
  1021. if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
  1022. && unique[n].width == info->width && unique[n].height == info->height)
  1023. return 0;
  1024. return 1;
  1025. }
  1026. #endif /* XINERAMA */
  1027. void
  1028. keypress(XEvent *e)
  1029. {
  1030. unsigned int i;
  1031. KeySym keysym;
  1032. XKeyEvent *ev;
  1033. ev = &e->xkey;
  1034. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  1035. for (i = 0; i < LENGTH(keys); i++)
  1036. if (keysym == keys[i].keysym
  1037. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  1038. && keys[i].func)
  1039. keys[i].func(&(keys[i].arg));
  1040. }
  1041. void
  1042. killclient(const Arg *arg)
  1043. {
  1044. if (!selmon->sel)
  1045. return;
  1046. if (!sendevent(selmon->sel, wmatom[WMDelete])) {
  1047. XGrabServer(dpy);
  1048. XSetErrorHandler(xerrordummy);
  1049. XSetCloseDownMode(dpy, DestroyAll);
  1050. XKillClient(dpy, selmon->sel->win);
  1051. XSync(dpy, False);
  1052. XSetErrorHandler(xerror);
  1053. XUngrabServer(dpy);
  1054. }
  1055. }
  1056. void
  1057. manage(Window w, XWindowAttributes *wa)
  1058. {
  1059. Client *c, *t = NULL;
  1060. Window trans = None;
  1061. XWindowChanges wc;
  1062. c = ecalloc(1, sizeof(Client));
  1063. c->win = w;
  1064. /* geometry */
  1065. c->x = c->oldx = wa->x;
  1066. c->y = c->oldy = wa->y;
  1067. c->w = c->oldw = wa->width;
  1068. c->h = c->oldh = wa->height;
  1069. c->oldbw = wa->border_width;
  1070. c->cfact = 1.0;
  1071. updatetitle(c);
  1072. if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
  1073. c->mon = t->mon;
  1074. c->tags = t->tags;
  1075. } else {
  1076. c->mon = selmon;
  1077. applyrules(c);
  1078. }
  1079. if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
  1080. c->x = c->mon->mx + c->mon->mw - WIDTH(c);
  1081. if (c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
  1082. c->y = c->mon->my + c->mon->mh - HEIGHT(c);
  1083. c->x = MAX(c->x, c->mon->mx);
  1084. /* only fix client y-offset, if the client center might cover the bar */
  1085. c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
  1086. && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
  1087. c->bw = borderpx;
  1088. wc.border_width = c->bw;
  1089. XConfigureWindow(dpy, w, CWBorderWidth, &wc);
  1090. XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel);
  1091. configure(c); /* propagates border_width, if size doesn't change */
  1092. updatewindowtype(c);
  1093. updatesizehints(c);
  1094. updatewmhints(c);
  1095. c->x = c->mon->mx + (c->mon->mw - WIDTH(c)) / 2;
  1096. c->y = c->mon->my + (c->mon->mh - HEIGHT(c)) / 2;
  1097. XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
  1098. grabbuttons(c, 0);
  1099. if (!c->isfloating)
  1100. c->isfloating = c->oldstate = trans != None || c->isfixed;
  1101. if (c->isfloating)
  1102. XRaiseWindow(dpy, c->win);
  1103. attach(c);
  1104. attachstack(c);
  1105. XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
  1106. (unsigned char *) &(c->win), 1);
  1107. XChangeProperty(dpy, root, netatom[NetClientListStacking], XA_WINDOW, 32, PropModePrepend,
  1108. (unsigned char *) &(c->win), 1);
  1109. XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
  1110. setclientstate(c, NormalState);
  1111. if (c->mon == selmon)
  1112. unfocus(selmon->sel, 0);
  1113. c->mon->sel = c;
  1114. arrange(c->mon);
  1115. XMapWindow(dpy, c->win);
  1116. focus(NULL);
  1117. }
  1118. void
  1119. mappingnotify(XEvent *e)
  1120. {
  1121. XMappingEvent *ev = &e->xmapping;
  1122. XRefreshKeyboardMapping(ev);
  1123. if (ev->request == MappingKeyboard)
  1124. grabkeys();
  1125. }
  1126. void
  1127. maprequest(XEvent *e)
  1128. {
  1129. static XWindowAttributes wa;
  1130. XMapRequestEvent *ev = &e->xmaprequest;
  1131. if (!XGetWindowAttributes(dpy, ev->window, &wa))
  1132. return;
  1133. if (wa.override_redirect)
  1134. return;
  1135. if (!wintoclient(ev->window))
  1136. manage(ev->window, &wa);
  1137. }
  1138. void
  1139. monocle(Monitor *m)
  1140. {
  1141. unsigned int n = 0;
  1142. Client *c;
  1143. for (c = m->clients; c; c = c->next)
  1144. if (ISVISIBLE(c))
  1145. n++;
  1146. if (n > 0) /* override layout symbol */
  1147. snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
  1148. for (c = nexttiled(m->clients); c; c = nexttiled(c->next))
  1149. resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
  1150. }
  1151. void
  1152. motionnotify(XEvent *e)
  1153. {
  1154. static Monitor *mon = NULL;
  1155. Monitor *m;
  1156. XMotionEvent *ev = &e->xmotion;
  1157. if (ev->window != root) {
  1158. if (ev->window == selmon->barwin)
  1159. updatebarcursor(ev->x);
  1160. return;
  1161. }
  1162. if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
  1163. unfocus(selmon->sel, 1);
  1164. selmon = m;
  1165. focus(NULL);
  1166. }
  1167. mon = m;
  1168. }
  1169. void
  1170. movemouse(const Arg *arg)
  1171. {
  1172. int x, y, ocx, ocy, nx, ny;
  1173. Client *c;
  1174. Monitor *m;
  1175. XEvent ev;
  1176. Time lasttime = 0;
  1177. if (!(c = selmon->sel))
  1178. return;
  1179. if (c->isfullscreen) /* no support moving fullscreen windows by mouse */
  1180. return;
  1181. restack(selmon);
  1182. ocx = c->x;
  1183. ocy = c->y;
  1184. if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1185. None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
  1186. return;
  1187. if (!getrootptr(&x, &y))
  1188. return;
  1189. do {
  1190. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1191. switch(ev.type) {
  1192. case ConfigureRequest:
  1193. case Expose:
  1194. case MapRequest:
  1195. handler[ev.type](&ev);
  1196. break;
  1197. case MotionNotify:
  1198. if ((ev.xmotion.time - lasttime) <= (1000 / 60))
  1199. continue;
  1200. lasttime = ev.xmotion.time;
  1201. nx = ocx + (ev.xmotion.x - x);
  1202. ny = ocy + (ev.xmotion.y - y);
  1203. if (abs(selmon->wx - nx) < snap)
  1204. nx = selmon->wx;
  1205. else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
  1206. nx = selmon->wx + selmon->ww - WIDTH(c);
  1207. if (abs(selmon->wy - ny) < snap)
  1208. ny = selmon->wy;
  1209. else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
  1210. ny = selmon->wy + selmon->wh - HEIGHT(c);
  1211. if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1212. && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
  1213. togglefloating(NULL);
  1214. if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1215. resize(c, nx, ny, c->w, c->h, 1);
  1216. break;
  1217. }
  1218. } while (ev.type != ButtonRelease);
  1219. XUngrabPointer(dpy, CurrentTime);
  1220. if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  1221. sendmon(c, m);
  1222. selmon = m;
  1223. focus(NULL);
  1224. }
  1225. }
  1226. Client *
  1227. nexttiled(Client *c)
  1228. {
  1229. for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
  1230. return c;
  1231. }
  1232. void
  1233. pop(Client *c)
  1234. {
  1235. detach(c);
  1236. attach(c);
  1237. focus(c);
  1238. arrange(c->mon);
  1239. }
  1240. void
  1241. propertynotify(XEvent *e)
  1242. {
  1243. Client *c;
  1244. Window trans;
  1245. XPropertyEvent *ev = &e->xproperty;
  1246. if ((ev->window == root) && (ev->atom == XA_WM_NAME))
  1247. updatestatus();
  1248. else if (ev->state == PropertyDelete)
  1249. return; /* ignore */
  1250. else if ((c = wintoclient(ev->window))) {
  1251. switch(ev->atom) {
  1252. default: break;
  1253. case XA_WM_TRANSIENT_FOR:
  1254. if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
  1255. (c->isfloating = (wintoclient(trans)) != NULL))
  1256. arrange(c->mon);
  1257. break;
  1258. case XA_WM_NORMAL_HINTS:
  1259. updatesizehints(c);
  1260. break;
  1261. case XA_WM_HINTS:
  1262. updatewmhints(c);
  1263. drawbars();
  1264. break;
  1265. }
  1266. if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  1267. updatetitle(c);
  1268. if (c == c->mon->sel)
  1269. drawbar(c->mon);
  1270. }
  1271. if (ev->atom == netatom[NetWMWindowType])
  1272. updatewindowtype(c);
  1273. }
  1274. }
  1275. void
  1276. quit(const Arg *arg)
  1277. {
  1278. running = 0;
  1279. }
  1280. Monitor *
  1281. recttomon(int x, int y, int w, int h)
  1282. {
  1283. Monitor *m, *r = selmon;
  1284. int a, area = 0;
  1285. for (m = mons; m; m = m->next)
  1286. if ((a = INTERSECT(x, y, w, h, m)) > area) {
  1287. area = a;
  1288. r = m;
  1289. }
  1290. return r;
  1291. }
  1292. void
  1293. resize(Client *c, int x, int y, int w, int h, int interact)
  1294. {
  1295. if (applysizehints(c, &x, &y, &w, &h, interact))
  1296. resizeclient(c, x, y, w, h);
  1297. }
  1298. void
  1299. resizeclient(Client *c, int x, int y, int w, int h)
  1300. {
  1301. XWindowChanges wc;
  1302. unsigned int n;
  1303. unsigned int gapoffset;
  1304. unsigned int gapincr;
  1305. Client *nbc;
  1306. wc.border_width = c->bw;
  1307. /* Get number of clients for the selected monitor */
  1308. for (n = 0, nbc = nexttiled(selmon->clients); nbc; nbc = nexttiled(nbc->next), n++);
  1309. /* Do nothing if layout is floating */
  1310. if (c->isfloating || selmon->lt[selmon->sellt]->arrange == NULL) {
  1311. gapincr = gapoffset = 0;
  1312. } else {
  1313. /* Remove border and gap if layout is monocle or only one client */
  1314. if (selmon->lt[selmon->sellt]->arrange == monocle) {
  1315. gapoffset = gappx;
  1316. gapincr = 2 * gappx;
  1317. // wc.border_width = 0;
  1318. } else {
  1319. gapoffset = gappx;
  1320. gapincr = 2 * gappx;
  1321. }
  1322. }
  1323. c->oldx = c->x; c->x = wc.x = x + gapoffset;
  1324. c->oldy = c->y; c->y = wc.y = y + gapoffset;
  1325. c->oldw = c->w; c->w = wc.width = w - gapincr;
  1326. c->oldh = c->h; c->h = wc.height = h - gapincr;
  1327. XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  1328. configure(c);
  1329. XSync(dpy, False);
  1330. }
  1331. void
  1332. resizemouse(const Arg *arg)
  1333. {
  1334. int ocx, ocy, nw, nh;
  1335. Client *c;
  1336. Monitor *m;
  1337. XEvent ev;
  1338. Time lasttime = 0;
  1339. if (!(c = selmon->sel))
  1340. return;
  1341. if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
  1342. return;
  1343. restack(selmon);
  1344. ocx = c->x;
  1345. ocy = c->y;
  1346. if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  1347. None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
  1348. return;
  1349. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1350. do {
  1351. XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
  1352. switch(ev.type) {
  1353. case ConfigureRequest:
  1354. case Expose:
  1355. case MapRequest:
  1356. handler[ev.type](&ev);
  1357. break;
  1358. case MotionNotify:
  1359. if ((ev.xmotion.time - lasttime) <= (1000 / 60))
  1360. continue;
  1361. lasttime = ev.xmotion.time;
  1362. nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
  1363. nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
  1364. if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
  1365. && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
  1366. {
  1367. if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
  1368. && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
  1369. togglefloating(NULL);
  1370. }
  1371. if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
  1372. resize(c, c->x, c->y, nw, nh, 1);
  1373. break;
  1374. }
  1375. } while (ev.type != ButtonRelease);
  1376. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
  1377. XUngrabPointer(dpy, CurrentTime);
  1378. while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1379. if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
  1380. sendmon(c, m);
  1381. selmon = m;
  1382. focus(NULL);
  1383. }
  1384. }
  1385. void
  1386. restack(Monitor *m)
  1387. {
  1388. Client *c;
  1389. XEvent ev;
  1390. XWindowChanges wc;
  1391. drawbar(m);
  1392. if (!m->sel)
  1393. return;
  1394. if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
  1395. XRaiseWindow(dpy, m->sel->win);
  1396. if (m->lt[m->sellt]->arrange) {
  1397. wc.stack_mode = Below;
  1398. wc.sibling = m->barwin;
  1399. for (c = m->stack; c; c = c->snext)
  1400. if (!c->isfloating && ISVISIBLE(c)) {
  1401. XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
  1402. wc.sibling = c->win;
  1403. }
  1404. }
  1405. XSync(dpy, False);
  1406. while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  1407. }
  1408. void
  1409. run(void)
  1410. {
  1411. XEvent ev;
  1412. /* main event loop */
  1413. XSync(dpy, False);
  1414. while (running && !XNextEvent(dpy, &ev))
  1415. if (handler[ev.type])
  1416. handler[ev.type](&ev); /* call handler */
  1417. }
  1418. void
  1419. scan(void)
  1420. {
  1421. unsigned int i, num;
  1422. Window d1, d2, *wins = NULL;
  1423. XWindowAttributes wa;
  1424. if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
  1425. for (i = 0; i < num; i++) {
  1426. if (!XGetWindowAttributes(dpy, wins[i], &wa)
  1427. || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
  1428. continue;
  1429. if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
  1430. manage(wins[i], &wa);
  1431. }
  1432. for (i = 0; i < num; i++) { /* now the transients */
  1433. if (!XGetWindowAttributes(dpy, wins[i], &wa))
  1434. continue;
  1435. if (XGetTransientForHint(dpy, wins[i], &d1)
  1436. && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
  1437. manage(wins[i], &wa);
  1438. }
  1439. if (wins)
  1440. XFree(wins);
  1441. }
  1442. }
  1443. void
  1444. sendmon(Client *c, Monitor *m)
  1445. {
  1446. if (c->mon == m)
  1447. return;
  1448. unfocus(c, 1);
  1449. detach(c);
  1450. detachstack(c);
  1451. c->mon = m;
  1452. c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
  1453. attach(c);
  1454. attachstack(c);
  1455. focus(NULL);
  1456. arrange(NULL);
  1457. }
  1458. void
  1459. setclientstate(Client *c, long state)
  1460. {
  1461. long data[] = { state, None };
  1462. XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
  1463. PropModeReplace, (unsigned char *)data, 2);
  1464. }
  1465. void
  1466. setcurrentdesktop(void){
  1467. long data[] = { 0 };
  1468. XChangeProperty(dpy, root, netatom[NetCurrentDesktop], XA_CARDINAL, 32, PropModeReplace, (unsigned char *)data, 1);
  1469. }
  1470. void setdesktopnames(void){
  1471. XTextProperty text;
  1472. Xutf8TextListToTextProperty(dpy, tags, TAGSLENGTH, XUTF8StringStyle, &text);
  1473. XSetTextProperty(dpy, root, &text, netatom[NetDesktopNames]);
  1474. }
  1475. int
  1476. sendevent(Client *c, Atom proto)
  1477. {
  1478. int n;
  1479. Atom *protocols;
  1480. int exists = 0;
  1481. XEvent ev;
  1482. if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
  1483. while (!exists && n--)
  1484. exists = protocols[n] == proto;
  1485. XFree(protocols);
  1486. }
  1487. if (exists) {
  1488. ev.type = ClientMessage;
  1489. ev.xclient.window = c->win;
  1490. ev.xclient.message_type = wmatom[WMProtocols];
  1491. ev.xclient.format = 32;
  1492. ev.xclient.data.l[0] = proto;
  1493. ev.xclient.data.l[1] = CurrentTime;
  1494. XSendEvent(dpy, c->win, False, NoEventMask, &ev);
  1495. }
  1496. return exists;
  1497. }
  1498. void
  1499. setnumdesktops(void){
  1500. long data[] = { TAGSLENGTH };
  1501. XChangeProperty(dpy, root, netatom[NetNumberOfDesktops], XA_CARDINAL, 32, PropModeReplace, (unsigned char *)data, 1);
  1502. }
  1503. void
  1504. setfocus(Client *c)
  1505. {
  1506. if (!c->neverfocus) {
  1507. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  1508. XChangeProperty(dpy, root, netatom[NetActiveWindow],
  1509. XA_WINDOW, 32, PropModeReplace,
  1510. (unsigned char *) &(c->win), 1);
  1511. }
  1512. sendevent(c, wmatom[WMTakeFocus]);
  1513. }
  1514. void
  1515. setfullscreen(Client *c, int fullscreen)
  1516. {
  1517. if (fullscreen && !c->isfullscreen) {
  1518. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1519. PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
  1520. c->isfullscreen = 1;
  1521. c->oldstate = c->isfloating;
  1522. c->oldbw = c->bw;
  1523. c->bw = 0;
  1524. c->isfloating = 1;
  1525. resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
  1526. XRaiseWindow(dpy, c->win);
  1527. } else if (!fullscreen && c->isfullscreen){
  1528. XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
  1529. PropModeReplace, (unsigned char*)0, 0);
  1530. c->isfullscreen = 0;
  1531. c->isfloating = c->oldstate;
  1532. c->bw = c->oldbw;
  1533. c->x = c->oldx;
  1534. c->y = c->oldy;
  1535. c->w = c->oldw;
  1536. c->h = c->oldh;
  1537. resizeclient(c, c->x, c->y, c->w, c->h);
  1538. arrange(c->mon);
  1539. }
  1540. }
  1541. void
  1542. setlayout(const Arg *arg)
  1543. {
  1544. if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
  1545. selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag] ^= 1;
  1546. if (arg && arg->v)
  1547. selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt] = (Layout *)arg->v;
  1548. strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
  1549. if (selmon->sel)
  1550. arrange(selmon);
  1551. else
  1552. drawbar(selmon);
  1553. }
  1554. void setcfact(const Arg *arg) {
  1555. float f;
  1556. Client *c;
  1557. c = selmon->sel;
  1558. if(!arg || !c || !selmon->lt[selmon->sellt]->arrange)
  1559. return;
  1560. f = arg->f + c->cfact;
  1561. if(arg->f == 0.0)
  1562. f = 1.0;
  1563. else if(f < 0.25 || f > 4.0)
  1564. return;
  1565. c->cfact = f;
  1566. arrange(selmon);
  1567. }
  1568. /* arg > 1.0 will set mfact absolutely */
  1569. void
  1570. setmfact(const Arg *arg)
  1571. {
  1572. float f;
  1573. if (!arg || !selmon->lt[selmon->sellt]->arrange)
  1574. return;
  1575. f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
  1576. if (f < 0.05 || f > 0.95)
  1577. return;
  1578. selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag] = f;
  1579. arrange(selmon);
  1580. }
  1581. void
  1582. setup(void)
  1583. {
  1584. int i;
  1585. XSetWindowAttributes wa;
  1586. Atom utf8string;
  1587. /* clean up any zombies immediately */
  1588. sigchld(0);
  1589. /* init screen */
  1590. screen = DefaultScreen(dpy);
  1591. sw = DisplayWidth(dpy, screen);
  1592. sh = DisplayHeight(dpy, screen);
  1593. root = RootWindow(dpy, screen);
  1594. drw = drw_create(dpy, screen, root, sw, sh);
  1595. if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
  1596. die("no fonts could be loaded.");
  1597. lrpad = tag_padding ? tag_padding : drw->fonts->h;
  1598. bh = user_bh ? user_bh : drw->fonts->h + 2;
  1599. updategeom();
  1600. sp = sidepad;
  1601. vp = (topbar == 1) ? vertpad : - vertpad;
  1602. /* init atoms */
  1603. utf8string = XInternAtom(dpy, "UTF8_STRING", False);
  1604. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  1605. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  1606. wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
  1607. wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
  1608. netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
  1609. netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
  1610. netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  1611. netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
  1612. netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
  1613. netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
  1614. netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
  1615. netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
  1616. netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
  1617. netatom[NetClientListStacking] = XInternAtom(dpy, "_NET_CLIENT_LIST_STACKING", False);
  1618. netatom[NetDesktopViewport] = XInternAtom(dpy, "_NET_DESKTOP_VIEWPORT", False);
  1619. netatom[NetNumberOfDesktops] = XInternAtom(dpy, "_NET_NUMBER_OF_DESKTOPS", False);
  1620. netatom[NetCurrentDesktop] = XInternAtom(dpy, "_NET_CURRENT_DESKTOP", False);
  1621. netatom[NetDesktopNames] = XInternAtom(dpy, "_NET_DESKTOP_NAMES", False);
  1622. /* init cursors */
  1623. cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
  1624. cursor[CurHand] = drw_cur_create(drw, XC_hand2);
  1625. cursor[CurResize] = drw_cur_create(drw, XC_sizing);
  1626. cursor[CurMove] = drw_cur_create(drw, XC_fleur);
  1627. /* init appearance */
  1628. scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
  1629. for (i = 0; i < LENGTH(colors); i++)
  1630. scheme[i] = drw_scm_create(drw, colors[i], 3);
  1631. /* init bars */
  1632. updatebars();
  1633. updatestatus();
  1634. updatebarpos(selmon);
  1635. /* supporting window for NetWMCheck */
  1636. wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
  1637. XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
  1638. PropModeReplace, (unsigned char *) &wmcheckwin, 1);
  1639. XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
  1640. PropModeReplace, (unsigned char *) "dwm", 3);
  1641. XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
  1642. PropModeReplace, (unsigned char *) &wmcheckwin, 1);
  1643. /* EWMH support per view */
  1644. XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
  1645. PropModeReplace, (unsigned char *) netatom, NetLast);
  1646. setnumdesktops();
  1647. setcurrentdesktop();
  1648. setdesktopnames();
  1649. setviewport();
  1650. XDeleteProperty(dpy, root, netatom[NetClientList]);
  1651. XDeleteProperty(dpy, root, netatom[NetClientListStacking]);
  1652. /* select events */
  1653. wa.cursor = cursor[CurNormal]->cursor;
  1654. wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
  1655. |ButtonPressMask|PointerMotionMask|EnterWindowMask
  1656. |LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
  1657. XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
  1658. XSelectInput(dpy, root, wa.event_mask);
  1659. grabkeys();
  1660. focus(NULL);
  1661. }
  1662. void
  1663. setviewport(void){
  1664. long data[] = { 0, 0 };
  1665. XChangeProperty(dpy, root, netatom[NetDesktopViewport], XA_CARDINAL, 32, PropModeReplace, (unsigned char *)data, 2);
  1666. }
  1667. void
  1668. seturgent(Client *c, int urg)
  1669. {
  1670. XWMHints *wmh;
  1671. c->isurgent = urg;
  1672. if (!(wmh = XGetWMHints(dpy, c->win)))
  1673. return;
  1674. wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
  1675. XSetWMHints(dpy, c->win, wmh);
  1676. XFree(wmh);
  1677. }
  1678. void
  1679. showhide(Client *c)
  1680. {
  1681. if (!c)
  1682. return;
  1683. if (ISVISIBLE(c)) {
  1684. /* show clients top down */
  1685. XMoveWindow(dpy, c->win, c->x, c->y);
  1686. if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
  1687. resize(c, c->x, c->y, c->w, c->h, 0);
  1688. showhide(c->snext);
  1689. } else {
  1690. /* hide clients bottom up */
  1691. showhide(c->snext);
  1692. XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
  1693. }
  1694. }
  1695. void
  1696. sigchld(int unused)
  1697. {
  1698. if (signal(SIGCHLD, sigchld) == SIG_ERR)
  1699. die("can't install SIGCHLD handler:");
  1700. while (0 < waitpid(-1, NULL, WNOHANG));
  1701. }
  1702. void
  1703. sigdwmblocks(const Arg *arg)
  1704. {
  1705. int fd;
  1706. struct flock fl;
  1707. union sigval sv;
  1708. if (dwmblockssig <= 0 || dwmblockssig >= 10)
  1709. return;
  1710. sv.sival_int = (dwmblockssig << 8) | arg->i;
  1711. fd = open(DWMBLOCKSLOCKFILE, O_RDONLY);
  1712. if (fd == -1)
  1713. return;
  1714. fl.l_type = F_WRLCK;
  1715. fl.l_start = 0;
  1716. fl.l_whence = SEEK_SET;
  1717. fl.l_len = 0;
  1718. if (fcntl(fd, F_GETLK, &fl) == -1 || fl.l_type == F_UNLCK)
  1719. return;
  1720. sigqueue(fl.l_pid, SIGRTMIN, sv);
  1721. }
  1722. void
  1723. spawn(const Arg *arg)
  1724. {
  1725. if (fork() == 0) {
  1726. if (dpy)
  1727. close(ConnectionNumber(dpy));
  1728. setsid();
  1729. execvp(((char **)arg->v)[0], (char **)arg->v);
  1730. fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
  1731. perror(" failed");
  1732. exit(EXIT_SUCCESS);
  1733. }
  1734. }
  1735. void
  1736. switchcol(const Arg *arg)
  1737. {
  1738. Client *c, *t;
  1739. int col = 0;
  1740. int i;
  1741. if (!selmon->sel)
  1742. return;
  1743. for (i = 0, c = nexttiled(selmon->clients); c ;
  1744. c = nexttiled(c->next), i++) {
  1745. if (c == selmon->sel)
  1746. col = (i + 1) > selmon->nmaster;
  1747. }
  1748. if (i <= selmon->nmaster)
  1749. return;
  1750. for (c = selmon->stack; c; c = c->snext) {
  1751. if (!ISVISIBLE(c))
  1752. continue;
  1753. for (i = 0, t = nexttiled(selmon->clients); t && t != c;
  1754. t = nexttiled(t->next), i++);
  1755. if (t && (i + 1 > selmon->nmaster) != col) {
  1756. focus(c);
  1757. restack(selmon);
  1758. break;
  1759. }
  1760. }
  1761. }
  1762. void
  1763. tag(const Arg *arg)
  1764. {
  1765. if (selmon->sel && arg->ui & TAGMASK) {
  1766. selmon->sel->tags = arg->ui & TAGMASK;
  1767. focus(NULL);
  1768. arrange(selmon);
  1769. }
  1770. updatecurrentdesktop();
  1771. }
  1772. void
  1773. tagmon(const Arg *arg)
  1774. {
  1775. if (!selmon->sel || !mons->next)
  1776. return;
  1777. sendmon(selmon->sel, dirtomon(arg->i));
  1778. }
  1779. void
  1780. tile(Monitor *m)
  1781. {
  1782. unsigned int i, n, h, mw, my, ty;
  1783. float mfacts = 0, sfacts = 0;
  1784. Client *c;
  1785. for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++) {
  1786. if (n < m->nmaster)
  1787. mfacts += c->cfact;
  1788. else
  1789. sfacts += c->cfact;
  1790. }
  1791. if (n == 0)
  1792. return;
  1793. if (n > m->nmaster)
  1794. mw = m->nmaster
  1795. ? m->ww * (m->rmaster ? 1.0 - m->mfact : m->mfact)
  1796. : 0;
  1797. else
  1798. mw = m->ww;
  1799. for (i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  1800. if (i < m->nmaster) {
  1801. h = (m->wh - my) * (c->cfact / mfacts);
  1802. resize(c, m->rmaster ? m->wx + m->ww - mw : m->wx,
  1803. m->wy + my, mw - (2*c->bw), h - (2*c->bw), 0);
  1804. if (my + HEIGHT(c) < m->wh)
  1805. my += HEIGHT(c);
  1806. mfacts -= c->cfact;
  1807. } else {
  1808. h = (m->wh - ty) * (c->cfact / sfacts);
  1809. resize(c, m->rmaster ? m->wx : m->wx + mw, m->wy + ty,
  1810. m->ww - mw - (2*c->bw), h - (2*c->bw), 0);
  1811. if (ty + HEIGHT(c) < m->wh)
  1812. ty += HEIGHT(c);
  1813. sfacts -= c->cfact;
  1814. }
  1815. }
  1816. void
  1817. togglebar(const Arg *arg)
  1818. {
  1819. selmon->showbar = selmon->pertag->showbars[selmon->pertag->curtag] = !selmon->showbar;
  1820. updatebarpos(selmon);
  1821. XMoveResizeWindow(dpy, selmon->barwin, selmon->wx + sp, selmon->by + vp, selmon->ww - 2 * sp, bh);
  1822. arrange(selmon);
  1823. }
  1824. void
  1825. togglefloating(const Arg *arg)
  1826. {
  1827. if (!selmon->sel)
  1828. return;
  1829. if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
  1830. return;
  1831. selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
  1832. if (selmon->sel->isfloating)
  1833. resize(selmon->sel, selmon->sel->x, selmon->sel->y,
  1834. selmon->sel->w, selmon->sel->h, 0);
  1835. arrange(selmon);
  1836. }
  1837. void
  1838. togglefullscr(const Arg *arg)
  1839. {
  1840. if(selmon->sel)
  1841. setfullscreen(selmon->sel, !selmon->sel->isfullscreen);
  1842. }
  1843. void
  1844. togglermaster(const Arg *arg)
  1845. {
  1846. selmon->rmaster = !selmon->rmaster;
  1847. /* now mfact represents the left factor */
  1848. selmon->mfact = 1.0 - selmon->mfact;
  1849. if (selmon->lt[selmon->sellt]->arrange)
  1850. arrange(selmon);
  1851. }
  1852. void
  1853. toggletag(const Arg *arg)
  1854. {
  1855. unsigned int newtags;
  1856. if (!selmon->sel)
  1857. return;
  1858. newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
  1859. if (newtags) {
  1860. selmon->sel->tags = newtags;
  1861. focus(NULL);
  1862. arrange(selmon);
  1863. }
  1864. updatecurrentdesktop();
  1865. }
  1866. void
  1867. toggleview(const Arg *arg)
  1868. {
  1869. unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
  1870. int i;
  1871. if (newtagset) {
  1872. selmon->tagset[selmon->seltags] = newtagset;
  1873. if (newtagset == ~0) {
  1874. selmon->pertag->prevtag = selmon->pertag->curtag;
  1875. selmon->pertag->curtag = 0;
  1876. }
  1877. /* test if the user did not select the same tag */
  1878. if (!(newtagset & 1 << (selmon->pertag->curtag - 1))) {
  1879. selmon->pertag->prevtag = selmon->pertag->curtag;
  1880. for (i = 0; !(newtagset & 1 << i); i++) ;
  1881. selmon->pertag->curtag = i + 1;
  1882. }
  1883. /* apply settings for this view */
  1884. selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
  1885. selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
  1886. selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
  1887. selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
  1888. selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
  1889. if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
  1890. togglebar(NULL);
  1891. focus(NULL);
  1892. arrange(selmon);
  1893. }
  1894. }
  1895. void
  1896. unfocus(Client *c, int setfocus)
  1897. {
  1898. if (!c)
  1899. return;
  1900. grabbuttons(c, 0);
  1901. XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
  1902. if (setfocus) {
  1903. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  1904. XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
  1905. }
  1906. }
  1907. void
  1908. unmanage(Client *c, int destroyed)
  1909. {
  1910. Monitor *m = c->mon;
  1911. XWindowChanges wc;
  1912. detach(c);
  1913. detachstack(c);
  1914. if (!destroyed) {
  1915. wc.border_width = c->oldbw;
  1916. XGrabServer(dpy); /* avoid race conditions */
  1917. XSetErrorHandler(xerrordummy);
  1918. XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
  1919. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  1920. setclientstate(c, WithdrawnState);
  1921. XSync(dpy, False);
  1922. XSetErrorHandler(xerror);
  1923. XUngrabServer(dpy);
  1924. }
  1925. free(c);
  1926. focus(NULL);
  1927. updateclientlist();
  1928. arrange(m);
  1929. }
  1930. void
  1931. unmapnotify(XEvent *e)
  1932. {
  1933. Client *c;
  1934. XUnmapEvent *ev = &e->xunmap;
  1935. if ((c = wintoclient(ev->window))) {
  1936. if (ev->send_event)
  1937. setclientstate(c, WithdrawnState);
  1938. else
  1939. unmanage(c, 0);
  1940. }
  1941. }
  1942. void
  1943. updatebars(void)
  1944. {
  1945. Monitor *m;
  1946. XSetWindowAttributes wa = {
  1947. .override_redirect = True,
  1948. .background_pixmap = ParentRelative,
  1949. .event_mask = ButtonPressMask|ExposureMask|PointerMotionMask
  1950. };
  1951. XClassHint ch = {"dwm", "dwm"};
  1952. for (m = mons; m; m = m->next) {
  1953. if (m->barwin)
  1954. continue;
  1955. m->barwin = XCreateWindow(dpy, root, m->wx + sp, m->by + vp, m->ww - 2 * sp, bh, 0, DefaultDepth(dpy, screen),
  1956. CopyFromParent, DefaultVisual(dpy, screen),
  1957. CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
  1958. XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
  1959. XMapRaised(dpy, m->barwin);
  1960. XSetClassHint(dpy, m->barwin, &ch);
  1961. }
  1962. }
  1963. void updatecurrentdesktop(void){
  1964. long rawdata[] = { selmon->tagset[selmon->seltags] };
  1965. int i=0;
  1966. while(*rawdata >> i+1){
  1967. i++;
  1968. }
  1969. long data[] = { i };
  1970. XChangeProperty(dpy, root, netatom[NetCurrentDesktop], XA_CARDINAL, 32, PropModeReplace, (unsigned char *)data, 1);
  1971. }
  1972. void
  1973. updatebarcursor(int cursorpos)
  1974. {
  1975. static int currentcursor = 0;
  1976. int x;
  1977. if (BETWEEN(cursorpos, (x = selmon->ww - wstext + lrpad / 2), x + wstext - lrpad)) {
  1978. updatedwmblockssig(x, cursorpos);
  1979. if (currentcursor) {
  1980. if (dwmblockssig <= 0 || dwmblockssig >= 10) {
  1981. currentcursor = 0;
  1982. XDefineCursor(dpy, selmon->barwin, cursor[CurNormal]->cursor);
  1983. }
  1984. } else {
  1985. if (dwmblockssig > 0 && dwmblockssig < 10) {
  1986. currentcursor = 1;
  1987. XDefineCursor(dpy, selmon->barwin, cursor[CurHand]->cursor);
  1988. }
  1989. }
  1990. } else {
  1991. if (currentcursor) {
  1992. currentcursor = 0;
  1993. XDefineCursor(dpy, selmon->barwin, cursor[CurNormal]->cursor);
  1994. }
  1995. }
  1996. }
  1997. void
  1998. updatebarpos(Monitor *m)
  1999. {
  2000. m->wy = m->my;
  2001. m->wh = m->mh;
  2002. if (m->showbar) {
  2003. m->wh = m->wh - vertpad - bh;
  2004. m->by = m->topbar ? m->wy : m->wy + m->wh + vertpad;
  2005. m->wy = m->topbar ? m->wy + bh + vp : m->wy;
  2006. } else
  2007. m->by = -bh - vp;
  2008. }
  2009. void
  2010. updateclientlist()
  2011. {
  2012. Client *c;
  2013. Monitor *m;
  2014. XDeleteProperty(dpy, root, netatom[NetClientList]);
  2015. for (m = mons; m; m = m->next)
  2016. for (c = m->clients; c; c = c->next)
  2017. XChangeProperty(dpy, root, netatom[NetClientList],
  2018. XA_WINDOW, 32, PropModeAppend,
  2019. (unsigned char *) &(c->win), 1);
  2020. XDeleteProperty(dpy, root, netatom[NetClientListStacking]);
  2021. for (m = mons; m; m = m->next)
  2022. for (c = m->stack; c; c = c->snext)
  2023. XChangeProperty(dpy, root, netatom[NetClientListStacking],
  2024. XA_WINDOW, 32, PropModeAppend,
  2025. (unsigned char *) &(c->win), 1);
  2026. }
  2027. void
  2028. updatedwmblockssig(int x, int e)
  2029. {
  2030. char *ts = stexts;
  2031. char *tp = stexts;
  2032. char ctmp;
  2033. while (*ts != '\0') {
  2034. if ((unsigned char)*ts > 10) {
  2035. ts++;
  2036. continue;
  2037. }
  2038. ctmp = *ts;
  2039. *ts = '\0';
  2040. x += TTEXTW(tp);
  2041. *ts = ctmp;
  2042. if (x >= e) {
  2043. dwmblockssig = (unsigned char)ctmp;
  2044. return;
  2045. }
  2046. tp = ++ts;
  2047. }
  2048. dwmblockssig = 0;
  2049. }
  2050. int
  2051. updategeom(void)
  2052. {
  2053. int dirty = 0;
  2054. #ifdef XINERAMA
  2055. if (XineramaIsActive(dpy)) {
  2056. int i, j, n, nn;
  2057. Client *c;
  2058. Monitor *m;
  2059. XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
  2060. XineramaScreenInfo *unique = NULL;
  2061. for (n = 0, m = mons; m; m = m->next, n++);
  2062. /* only consider unique geometries as separate screens */
  2063. unique = ecalloc(nn, sizeof(XineramaScreenInfo));
  2064. for (i = 0, j = 0; i < nn; i++)
  2065. if (isuniquegeom(unique, j, &info[i]))
  2066. memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
  2067. XFree(info);
  2068. nn = j;
  2069. if (n <= nn) { /* new monitors available */
  2070. for (i = 0; i < (nn - n); i++) {
  2071. for (m = mons; m && m->next; m = m->next);
  2072. if (m)
  2073. m->next = createmon();
  2074. else
  2075. mons = createmon();
  2076. }
  2077. for (i = 0, m = mons; i < nn && m; m = m->next, i++)
  2078. if (i >= n
  2079. || unique[i].x_org != m->mx || unique[i].y_org != m->my
  2080. || unique[i].width != m->mw || unique[i].height != m->mh)
  2081. {
  2082. dirty = 1;
  2083. m->num = i;
  2084. m->mx = m->wx = unique[i].x_org;
  2085. m->my = m->wy = unique[i].y_org;
  2086. m->mw = m->ww = unique[i].width;
  2087. m->mh = m->wh = unique[i].height;
  2088. updatebarpos(m);
  2089. }
  2090. } else { /* less monitors available nn < n */
  2091. for (i = nn; i < n; i++) {
  2092. for (m = mons; m && m->next; m = m->next);
  2093. while ((c = m->clients)) {
  2094. dirty = 1;
  2095. m->clients = c->next;
  2096. detachstack(c);
  2097. c->mon = mons;
  2098. attach(c);
  2099. attachstack(c);
  2100. }
  2101. if (m == selmon)
  2102. selmon = mons;
  2103. cleanupmon(m);
  2104. }
  2105. }
  2106. free(unique);
  2107. } else
  2108. #endif /* XINERAMA */
  2109. { /* default monitor setup */
  2110. if (!mons)
  2111. mons = createmon();
  2112. if (mons->mw != sw || mons->mh != sh) {
  2113. dirty = 1;
  2114. mons->mw = mons->ww = sw;
  2115. mons->mh = mons->wh = sh;
  2116. updatebarpos(mons);
  2117. }
  2118. }
  2119. if (dirty) {
  2120. selmon = mons;
  2121. selmon = wintomon(root);
  2122. }
  2123. return dirty;
  2124. }
  2125. void
  2126. updatenumlockmask(void)
  2127. {
  2128. unsigned int i, j;
  2129. XModifierKeymap *modmap;
  2130. numlockmask = 0;
  2131. modmap = XGetModifierMapping(dpy);
  2132. for (i = 0; i < 8; i++)
  2133. for (j = 0; j < modmap->max_keypermod; j++)
  2134. if (modmap->modifiermap[i * modmap->max_keypermod + j]
  2135. == XKeysymToKeycode(dpy, XK_Num_Lock))
  2136. numlockmask = (1 << i);
  2137. XFreeModifiermap(modmap);
  2138. }
  2139. void
  2140. updatesizehints(Client *c)
  2141. {
  2142. long msize;
  2143. XSizeHints size;
  2144. if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
  2145. /* size is uninitialized, ensure that size.flags aren't used */
  2146. size.flags = PSize;
  2147. if (size.flags & PBaseSize) {
  2148. c->basew = size.base_width;
  2149. c->baseh = size.base_height;
  2150. } else if (size.flags & PMinSize) {
  2151. c->basew = size.min_width;
  2152. c->baseh = size.min_height;
  2153. } else
  2154. c->basew = c->baseh = 0;
  2155. if (size.flags & PResizeInc) {
  2156. c->incw = size.width_inc;
  2157. c->inch = size.height_inc;
  2158. } else
  2159. c->incw = c->inch = 0;
  2160. if (size.flags & PMaxSize) {
  2161. c->maxw = size.max_width;
  2162. c->maxh = size.max_height;
  2163. } else
  2164. c->maxw = c->maxh = 0;
  2165. if (size.flags & PMinSize) {
  2166. c->minw = size.min_width;
  2167. c->minh = size.min_height;
  2168. } else if (size.flags & PBaseSize) {
  2169. c->minw = size.base_width;
  2170. c->minh = size.base_height;
  2171. } else
  2172. c->minw = c->minh = 0;
  2173. if (size.flags & PAspect) {
  2174. c->mina = (float)size.min_aspect.y / size.min_aspect.x;
  2175. c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
  2176. } else
  2177. c->maxa = c->mina = 0.0;
  2178. c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
  2179. }
  2180. void
  2181. updatestatus(void)
  2182. {
  2183. char rawstext[256];
  2184. if (gettextprop(root, XA_WM_NAME, rawstext, sizeof rawstext)) {
  2185. char stextt[256];
  2186. char *stc = stextc, *sts = stexts, *stt = stextt;
  2187. for (char *rt = rawstext; *rt != '\0'; rt++)
  2188. if ((unsigned char)*rt >= ' ')
  2189. *(stc++) = *(sts++) = *(stt++) = *rt;
  2190. else if ((unsigned char)*rt > 10)
  2191. *(stc++) = *rt;
  2192. else
  2193. *(sts++) = *rt;
  2194. *stc = *sts = *stt = '\0';
  2195. wstext = TEXTW(stextt);
  2196. } else {
  2197. strcpy(stextc, "dwm-"VERSION);
  2198. strcpy(stexts, stextc);
  2199. wstext = TEXTW(stextc);
  2200. }
  2201. drawbar(selmon);
  2202. }
  2203. void
  2204. updatetitle(Client *c)
  2205. {
  2206. if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
  2207. gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
  2208. if (c->name[0] == '\0') /* hack to mark broken clients */
  2209. strcpy(c->name, broken);
  2210. }
  2211. void
  2212. updatewindowtype(Client *c)
  2213. {
  2214. Atom state = getatomprop(c, netatom[NetWMState]);
  2215. Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
  2216. if (state == netatom[NetWMFullscreen])
  2217. setfullscreen(c, 1);
  2218. if (wtype == netatom[NetWMWindowTypeDialog])
  2219. c->isfloating = 1;
  2220. }
  2221. void
  2222. updatewmhints(Client *c)
  2223. {
  2224. XWMHints *wmh;
  2225. if ((wmh = XGetWMHints(dpy, c->win))) {
  2226. if (c == selmon->sel && wmh->flags & XUrgencyHint) {
  2227. wmh->flags &= ~XUrgencyHint;
  2228. XSetWMHints(dpy, c->win, wmh);
  2229. } else
  2230. c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
  2231. if (wmh->flags & InputHint)
  2232. c->neverfocus = !wmh->input;
  2233. else
  2234. c->neverfocus = 0;
  2235. XFree(wmh);
  2236. }
  2237. }
  2238. void
  2239. view(const Arg *arg)
  2240. {
  2241. int i;
  2242. unsigned int tmptag;
  2243. if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
  2244. return;
  2245. selmon->seltags ^= 1; /* toggle sel tagset */
  2246. if (arg->ui & TAGMASK) {
  2247. selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
  2248. selmon->pertag->prevtag = selmon->pertag->curtag;
  2249. if (arg->ui == ~0)
  2250. selmon->pertag->curtag = 0;
  2251. else {
  2252. for (i = 0; !(arg->ui & 1 << i); i++) ;
  2253. selmon->pertag->curtag = i + 1;
  2254. }
  2255. } else {
  2256. tmptag = selmon->pertag->prevtag;
  2257. selmon->pertag->prevtag = selmon->pertag->curtag;
  2258. selmon->pertag->curtag = tmptag;
  2259. }
  2260. selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
  2261. selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
  2262. selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
  2263. selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
  2264. selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
  2265. if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
  2266. togglebar(NULL);
  2267. focus(NULL);
  2268. arrange(selmon);
  2269. updatecurrentdesktop();
  2270. }
  2271. Client *
  2272. wintoclient(Window w)
  2273. {
  2274. Client *c;
  2275. Monitor *m;
  2276. for (m = mons; m; m = m->next)
  2277. for (c = m->clients; c; c = c->next)
  2278. if (c->win == w)
  2279. return c;
  2280. return NULL;
  2281. }
  2282. Monitor *
  2283. wintomon(Window w)
  2284. {
  2285. int x, y;
  2286. Client *c;
  2287. Monitor *m;
  2288. if (w == root && getrootptr(&x, &y))
  2289. return recttomon(x, y, 1, 1);
  2290. for (m = mons; m; m = m->next)
  2291. if (w == m->barwin)
  2292. return m;
  2293. if ((c = wintoclient(w)))
  2294. return c->mon;
  2295. return selmon;
  2296. }
  2297. /* Selects for the view of the focused window. The list of tags */
  2298. /* to be displayed is matched to the focused window tag list. */
  2299. void
  2300. winview(const Arg* arg){
  2301. Window win, win_r, win_p, *win_c;
  2302. unsigned nc;
  2303. int unused;
  2304. Client* c;
  2305. Arg a;
  2306. if (!XGetInputFocus(dpy, &win, &unused)) return;
  2307. while(XQueryTree(dpy, win, &win_r, &win_p, &win_c, &nc)
  2308. && win_p != win_r) win = win_p;
  2309. if (!(c = wintoclient(win))) return;
  2310. a.ui = c->tags;
  2311. view(&a);
  2312. }
  2313. /* There's no way to check accesses to destroyed windows, thus those cases are
  2314. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  2315. * default error handler, which may call exit. */
  2316. int
  2317. xerror(Display *dpy, XErrorEvent *ee)
  2318. {
  2319. if (ee->error_code == BadWindow
  2320. || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
  2321. || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
  2322. || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
  2323. || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
  2324. || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
  2325. || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
  2326. || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
  2327. || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
  2328. return 0;
  2329. fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
  2330. ee->request_code, ee->error_code);
  2331. return xerrorxlib(dpy, ee); /* may call exit */
  2332. }
  2333. int
  2334. xerrordummy(Display *dpy, XErrorEvent *ee)
  2335. {
  2336. return 0;
  2337. }
  2338. /* Startup Error handler to check if another window manager
  2339. * is already running. */
  2340. int
  2341. xerrorstart(Display *dpy, XErrorEvent *ee)
  2342. {
  2343. die("dwm: another window manager is already running");
  2344. return -1;
  2345. }
  2346. void
  2347. zoom(const Arg *arg)
  2348. {
  2349. Client *c = selmon->sel;
  2350. if (!selmon->lt[selmon->sellt]->arrange
  2351. || (selmon->sel && selmon->sel->isfloating))
  2352. return;
  2353. if (c == nexttiled(selmon->clients))
  2354. if (!c || !(c = nexttiled(c->next)))
  2355. return;
  2356. pop(c);
  2357. }
  2358. int
  2359. main(int argc, char *argv[])
  2360. {
  2361. if (argc == 2 && !strcmp("-v", argv[1]))
  2362. die("dwm-"VERSION);
  2363. else if (argc != 1)
  2364. die("usage: dwm [-v]");
  2365. if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  2366. fputs("warning: no locale support\n", stderr);
  2367. if (!(dpy = XOpenDisplay(NULL)))
  2368. die("dwm: cannot open display");
  2369. checkotherwm();
  2370. setup();
  2371. #ifdef __OpenBSD__
  2372. if (pledge("stdio rpath proc exec", NULL) == -1)
  2373. die("pledge");
  2374. #endif /* __OpenBSD__ */
  2375. scan();
  2376. run();
  2377. cleanup();
  2378. XCloseDisplay(dpy);
  2379. return EXIT_SUCCESS;
  2380. }
  2381. void
  2382. centeredmaster(Monitor *m)
  2383. {
  2384. unsigned int i, n, h, mw, mx, my, oty, ety, tw;
  2385. Client *c;
  2386. /* count number of clients in the selected monitor */
  2387. for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  2388. if (n == 0)
  2389. return;
  2390. /* initialize areas */
  2391. mw = m->ww;
  2392. mx = 0;
  2393. my = 0;
  2394. tw = mw;
  2395. if (n > m->nmaster) {
  2396. /* go mfact box in the center if more than nmaster clients */
  2397. mw = m->nmaster ? m->ww * m->mfact : 0;
  2398. tw = m->ww - mw;
  2399. if (n - m->nmaster > 1) {
  2400. /* only one client */
  2401. mx = (m->ww - mw) / 2;
  2402. tw = (m->ww - mw) / 2;
  2403. }
  2404. }
  2405. oty = 0;
  2406. ety = 0;
  2407. for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  2408. if (i < m->nmaster) {
  2409. /* nmaster clients are stacked vertically, in the center
  2410. * of the screen */
  2411. h = (m->wh - my) / (MIN(n, m->nmaster) - i);
  2412. resize(c, m->wx + mx, m->wy + my, mw - (2*c->bw),
  2413. h - (2*c->bw), 0);
  2414. my += HEIGHT(c);
  2415. } else {
  2416. /* stack clients are stacked vertically */
  2417. if ((i - m->nmaster) % 2 ) {
  2418. h = (m->wh - ety) / ( (1 + n - i) / 2);
  2419. resize(c, m->wx, m->wy + ety, tw - (2*c->bw),
  2420. h - (2*c->bw), 0);
  2421. ety += HEIGHT(c);
  2422. } else {
  2423. h = (m->wh - oty) / ((1 + n - i) / 2);
  2424. resize(c, m->wx + mx + mw, m->wy + oty,
  2425. tw - (2*c->bw), h - (2*c->bw), 0);
  2426. oty += HEIGHT(c);
  2427. }
  2428. }
  2429. }
  2430. void
  2431. centeredfloatingmaster(Monitor *m)
  2432. {
  2433. unsigned int i, n, w, mh, mw, mx, mxo, my, myo, tx;
  2434. Client *c;
  2435. /* count number of clients in the selected monitor */
  2436. for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
  2437. if (n == 0)
  2438. return;
  2439. /* initialize nmaster area */
  2440. if (n > m->nmaster) {
  2441. /* go mfact box in the center if more than nmaster clients */
  2442. if (m->ww > m->wh) {
  2443. mw = m->nmaster ? m->ww * m->mfact : 0;
  2444. mh = m->nmaster ? m->wh * 0.9 : 0;
  2445. } else {
  2446. mh = m->nmaster ? m->wh * m->mfact : 0;
  2447. mw = m->nmaster ? m->ww * 0.9 : 0;
  2448. }
  2449. mx = mxo = (m->ww - mw) / 2;
  2450. my = myo = (m->wh - mh) / 2;
  2451. } else {
  2452. /* go fullscreen if all clients are in the master area */
  2453. mh = m->wh;
  2454. mw = m->ww;
  2455. mx = mxo = 0;
  2456. my = myo = 0;
  2457. }
  2458. for(i = tx = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
  2459. if (i < m->nmaster) {
  2460. /* nmaster clients are stacked horizontally, in the center
  2461. * of the screen */
  2462. w = (mw + mxo - mx) / (MIN(n, m->nmaster) - i);
  2463. resize(c, m->wx + mx, m->wy + my, w - (2*c->bw),
  2464. mh - (2*c->bw), 0);
  2465. mx += WIDTH(c);
  2466. } else {
  2467. /* stack clients are stacked horizontally */
  2468. w = (m->ww - tx) / (n - i);
  2469. resize(c, m->wx + tx, m->wy, w - (2*c->bw),
  2470. m->wh - (2*c->bw), 0);
  2471. tx += WIDTH(c);
  2472. }
  2473. }
  2474. void
  2475. insertclient(Client *item, Client *insertItem, int after) {
  2476. Client *c;
  2477. if (item == NULL || insertItem == NULL || item == insertItem) return;
  2478. detach(insertItem);
  2479. if (!after && selmon->clients == item) {
  2480. attach(insertItem);
  2481. return;
  2482. }
  2483. if (after) {
  2484. c = item;
  2485. } else {
  2486. for (c = selmon->clients; c; c = c->next) { if (c->next == item) break; }
  2487. }
  2488. insertItem->next = c->next;
  2489. c->next = insertItem;
  2490. }
  2491. void
  2492. inplacerotate(const Arg *arg)
  2493. {
  2494. if(!selmon->sel || (selmon->sel->isfloating && !arg->f)) return;
  2495. unsigned int selidx = 0, i = 0;
  2496. Client *c = NULL, *stail = NULL, *mhead = NULL, *mtail = NULL, *shead = NULL;
  2497. // Determine positionings for insertclient
  2498. for (c = selmon->clients; c; c = c->next) {
  2499. if (ISVISIBLE(c) && !(c->isfloating)) {
  2500. if (selmon->sel == c) { selidx = i; }
  2501. if (i == selmon->nmaster - 1) { mtail = c; }
  2502. if (i == selmon->nmaster) { shead = c; }
  2503. if (mhead == NULL) { mhead = c; }
  2504. stail = c;
  2505. i++;
  2506. }
  2507. }
  2508. // All clients rotate
  2509. if (arg->i == 2) insertclient(selmon->clients, stail, 0);
  2510. if (arg->i == -2) insertclient(stail, selmon->clients, 1);
  2511. // Stack xor master rotate
  2512. if (arg->i == -1 && selidx >= selmon->nmaster) insertclient(stail, shead, 1);
  2513. if (arg->i == 1 && selidx >= selmon->nmaster) insertclient(shead, stail, 0);
  2514. if (arg->i == -1 && selidx < selmon->nmaster) insertclient(mtail, mhead, 1);
  2515. if (arg->i == 1 && selidx < selmon->nmaster) insertclient(mhead, mtail, 0);
  2516. // Restore focus position
  2517. i = 0;
  2518. for (c = selmon->clients; c; c = c->next) {
  2519. if (!ISVISIBLE(c) || (c->isfloating)) continue;
  2520. if (i == selidx) { focus(c); break; }
  2521. i++;
  2522. }
  2523. arrange(selmon);
  2524. focus(c);
  2525. }