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

2156 lines
52 KiB

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