My patched version of suckless' dwm.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2156 lines
52 KiB

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