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.
 
 
 
 
 

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