My build of dwm
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 

2408 Zeilen
57 KiB

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