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.

2216 lines
53 KiB

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