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.

2165 lines
52 KiB

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