My dwm build
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.
 
 
 
 
 

2492 lines
58 KiB

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