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.

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