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.

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