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.

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