Another copy of my dotfiles. Because I don't completely trust GitHub.
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.

2691 lines
65 KiB

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