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