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.

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