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.

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