Mercurial > aewl
comparison dwm.c @ 754:4c12dccc288d
this is the (second) begin of aewl - my personal stipped down dwm
author | meillo@marmaro.de |
---|---|
date | Thu, 29 May 2008 23:12:30 +0200 |
parents | main.c@628c5bac7f3b |
children | cdd895c163bd |
comparison
equal
deleted
inserted
replaced
753:c459e05a61a8 | 754:4c12dccc288d |
---|---|
1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com> | |
2 * See LICENSE file for license details. | |
3 * | |
4 * dynamic window manager is designed like any other X client as well. It is | |
5 * driven through handling X events. In contrast to other X clients, a window | |
6 * manager selects for SubstructureRedirectMask on the root window, to receive | |
7 * events about window (dis-)appearance. Only one X connection at a time is | |
8 * allowed to select for this event mask. | |
9 * | |
10 * Calls to fetch an X event from the event queue are blocking. Due reading | |
11 * status text from standard input, a select()-driven main loop has been | |
12 * implemented which selects for reads on the X connection and STDIN_FILENO to | |
13 * handle all data smoothly. The event handlers of dwm are organized in an | |
14 * array which is accessed whenever a new event has been fetched. This allows | |
15 * event dispatching in O(1) time. | |
16 * | |
17 * Each child of the root window is called a client, except windows which have | |
18 * set the override_redirect flag. Clients are organized in a global | |
19 * doubly-linked client list, the focus history is remembered through a global | |
20 * stack list. Each client contains an array of Bools of the same size as the | |
21 * global tags array to indicate the tags of a client. For each client dwm | |
22 * creates a small title window, which is resized whenever the (_NET_)WM_NAME | |
23 * properties are updated or the client is moved/resized. | |
24 * | |
25 * Keys and tagging rules are organized as arrays and defined in the config.h | |
26 * file. These arrays are kept static in event.o and tag.o respectively, | |
27 * because no other part of dwm needs access to them. The current mode is | |
28 * represented by the arrange() function pointer, which wether points to | |
29 * dofloat() or dotile(). | |
30 * | |
31 * To understand everything else, start reading main.c:main(). | |
32 */ | |
33 | |
34 #include "config.h" | |
35 #include <errno.h> | |
36 #include <locale.h> | |
37 #include <regex.h> | |
38 #include <stdio.h> | |
39 #include <stdarg.h> | |
40 #include <stdlib.h> | |
41 #include <string.h> | |
42 #include <unistd.h> | |
43 #include <sys/select.h> | |
44 #include <sys/types.h> | |
45 #include <sys/wait.h> | |
46 #include <X11/cursorfont.h> | |
47 #include <X11/keysym.h> | |
48 #include <X11/Xatom.h> | |
49 #include <X11/Xlib.h> | |
50 #include <X11/Xproto.h> | |
51 #include <X11/Xutil.h> | |
52 | |
53 /* mask shorthands, used in event.c and client.c */ | |
54 #define BUTTONMASK (ButtonPressMask | ButtonReleaseMask) | |
55 | |
56 enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */ | |
57 enum { WMProtocols, WMDelete, WMState, WMLast }; /* default atoms */ | |
58 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */ | |
59 enum { ColBorder, ColFG, ColBG, ColLast }; /* color */ | |
60 | |
61 typedef union { | |
62 const char *cmd; | |
63 int i; | |
64 } Arg; /* argument type */ | |
65 | |
66 typedef struct { | |
67 int ascent; | |
68 int descent; | |
69 int height; | |
70 XFontSet set; | |
71 XFontStruct *xfont; | |
72 } Fnt; | |
73 | |
74 typedef struct { | |
75 int x, y, w, h; | |
76 unsigned long norm[ColLast]; | |
77 unsigned long sel[ColLast]; | |
78 Drawable drawable; | |
79 Fnt font; | |
80 GC gc; | |
81 } DC; /* draw context */ | |
82 | |
83 typedef struct Client Client; | |
84 struct Client { | |
85 char name[256]; | |
86 int x, y, w, h; | |
87 int rx, ry, rw, rh; /* revert geometry */ | |
88 int basew, baseh, incw, inch, maxw, maxh, minw, minh; | |
89 int minax, minay, maxax, maxay; | |
90 long flags; | |
91 unsigned int border; | |
92 Bool isfixed, isfloat, ismax; | |
93 Bool *tags; | |
94 Client *next; | |
95 Client *prev; | |
96 Client *snext; | |
97 Window win; | |
98 }; | |
99 | |
100 typedef struct { | |
101 const char *clpattern; | |
102 const char *tpattern; | |
103 Bool isfloat; | |
104 } Rule; | |
105 | |
106 typedef struct { | |
107 regex_t *clregex; | |
108 regex_t *tregex; | |
109 } RReg; | |
110 | |
111 | |
112 typedef struct { | |
113 unsigned long mod; | |
114 KeySym keysym; | |
115 void (*func)(Arg *arg); | |
116 Arg arg; | |
117 } Key; | |
118 | |
119 | |
120 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask)) | |
121 #define MOUSEMASK (BUTTONMASK | PointerMotionMask) | |
122 | |
123 | |
124 | |
125 const char *tags[]; /* all tags */ | |
126 char stext[256]; /* status text */ | |
127 int bh, bmw; /* bar height, bar mode label width */ | |
128 int screen, sx, sy, sw, sh; /* screen geometry */ | |
129 int wax, way, wah, waw; /* windowarea geometry */ | |
130 unsigned int nmaster; /* number of master clients */ | |
131 unsigned int ntags, numlockmask; /* number of tags, dynamic lock mask */ | |
132 void (*handler[LASTEvent])(XEvent *); /* event handler */ | |
133 void (*arrange)(void); /* arrange function, indicates mode */ | |
134 Atom wmatom[WMLast], netatom[NetLast]; | |
135 Bool running, selscreen, *seltag; /* seltag is array of Bool */ | |
136 Client *clients, *sel, *stack; /* global client list and stack */ | |
137 Cursor cursor[CurLast]; | |
138 DC dc; /* global draw context */ | |
139 Display *dpy; | |
140 Window root, barwin; | |
141 | |
142 Bool running = True; | |
143 Bool selscreen = True; | |
144 Client *clients = NULL; | |
145 Client *sel = NULL; | |
146 Client *stack = NULL; | |
147 DC dc = {0}; | |
148 | |
149 static int (*xerrorxlib)(Display *, XErrorEvent *); | |
150 static Bool otherwm, readin; | |
151 static RReg *rreg = NULL; | |
152 static unsigned int len = 0; | |
153 | |
154 | |
155 TAGS | |
156 RULES | |
157 | |
158 | |
159 /* client.c */ | |
160 void configure(Client *c); /* send synthetic configure event */ | |
161 void focus(Client *c); /* focus c, c may be NULL */ | |
162 Client *getclient(Window w); /* return client of w */ | |
163 Bool isprotodel(Client *c); /* returns True if c->win supports wmatom[WMDelete] */ | |
164 void killclient(Arg *arg); /* kill c nicely */ | |
165 void manage(Window w, XWindowAttributes *wa); /* manage new client */ | |
166 void resize(Client *c, Bool sizehints); /* resize c*/ | |
167 void updatesizehints(Client *c); /* update the size hint variables of c */ | |
168 void updatetitle(Client *c); /* update the name of c */ | |
169 void unmanage(Client *c); /* destroy c */ | |
170 | |
171 /* draw.c */ | |
172 void drawstatus(void); /* draw the bar */ | |
173 unsigned long getcolor(const char *colstr); /* return color of colstr */ | |
174 void setfont(const char *fontstr); /* set the font for DC */ | |
175 unsigned int textw(const char *text); /* return the width of text in px*/ | |
176 | |
177 /* event.c */ | |
178 void grabkeys(void); /* grab all keys defined in config.h */ | |
179 void procevent(void); /* process pending X events */ | |
180 | |
181 /* main.c */ | |
182 void quit(Arg *arg); /* quit dwm nicely */ | |
183 void sendevent(Window w, Atom a, long value); /* send synthetic event to w */ | |
184 int xerror(Display *dsply, XErrorEvent *ee); /* dwm's X error handler */ | |
185 | |
186 /* tag.c */ | |
187 void initrregs(void); /* initialize regexps of rules defined in config.h */ | |
188 Client *getnext(Client *c); /* returns next visible client */ | |
189 Client *getprev(Client *c); /* returns previous visible client */ | |
190 void settags(Client *c, Client *trans); /* sets tags of c */ | |
191 void tag(Arg *arg); /* tags c with arg's index */ | |
192 void toggletag(Arg *arg); /* toggles c tags with arg's index */ | |
193 void viewnext(Arg *arg); /* view next tag(s) */ | |
194 | |
195 /* util.c */ | |
196 void *emallocz(unsigned int size); /* allocates zero-initialized memory, exits on error */ | |
197 void eprint(const char *errstr, ...); /* prints errstr and exits with 1 */ | |
198 void spawn(Arg *arg); /* forks a new subprocess with to arg's cmd */ | |
199 | |
200 /* view.c */ | |
201 void detach(Client *c); /* detaches c from global client list */ | |
202 void dofloat(void); /* arranges all windows floating */ | |
203 void dotile(void); /* arranges all windows tiled */ | |
204 void domax(void); /* arranges all windows fullscreen */ | |
205 void focusnext(Arg *arg); /* focuses next visible client, arg is ignored */ | |
206 void incnmaster(Arg *arg); /* increments nmaster with arg's index value */ | |
207 Bool isvisible(Client *c); /* returns True if client is visible */ | |
208 void restack(void); /* restores z layers of all clients */ | |
209 void togglefloat(Arg *arg); /* toggles focusesd client between floating/non-floating state */ | |
210 void togglemode(Arg *arg); /* toggles global arrange function (dotile/dofloat) */ | |
211 void view(Arg *arg); /* views the tag with arg's index */ | |
212 void zoom(Arg *arg); /* zooms the focused client to master area, arg is ignored */ | |
213 | |
214 | |
215 | |
216 | |
217 | |
218 | |
219 | |
220 | |
221 | |
222 | |
223 /* from view.c */ | |
224 /* static */ | |
225 | |
226 static Client * | |
227 nexttiled(Client *c) { | |
228 for(c = getnext(c); c && c->isfloat; c = getnext(c->next)); | |
229 return c; | |
230 } | |
231 | |
232 static void | |
233 togglemax(Client *c) { | |
234 XEvent ev; | |
235 | |
236 if(c->isfixed) | |
237 return; | |
238 | |
239 if((c->ismax = !c->ismax)) { | |
240 c->rx = c->x; c->x = wax; | |
241 c->ry = c->y; c->y = way; | |
242 c->rw = c->w; c->w = waw - 2 * BORDERPX; | |
243 c->rh = c->h; c->h = wah - 2 * BORDERPX; | |
244 } | |
245 else { | |
246 c->x = c->rx; | |
247 c->y = c->ry; | |
248 c->w = c->rw; | |
249 c->h = c->rh; | |
250 } | |
251 resize(c, True); | |
252 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev)); | |
253 } | |
254 | |
255 | |
256 | |
257 void (*arrange)(void) = DEFMODE; | |
258 | |
259 void | |
260 detach(Client *c) { | |
261 if(c->prev) | |
262 c->prev->next = c->next; | |
263 if(c->next) | |
264 c->next->prev = c->prev; | |
265 if(c == clients) | |
266 clients = c->next; | |
267 c->next = c->prev = NULL; | |
268 } | |
269 | |
270 void | |
271 dofloat(void) { | |
272 Client *c; | |
273 | |
274 for(c = clients; c; c = c->next) { | |
275 if(isvisible(c)) { | |
276 resize(c, True); | |
277 } | |
278 else | |
279 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y); | |
280 } | |
281 if(!sel || !isvisible(sel)) { | |
282 for(c = stack; c && !isvisible(c); c = c->snext); | |
283 focus(c); | |
284 } | |
285 restack(); | |
286 } | |
287 | |
288 void | |
289 dotile(void) { | |
290 unsigned int i, n, mw, mh, tw, th; | |
291 Client *c; | |
292 | |
293 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next)) | |
294 n++; | |
295 /* window geoms */ | |
296 mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1); | |
297 mw = (n > nmaster) ? waw / 2 : waw; | |
298 th = (n > nmaster) ? wah / (n - nmaster) : 0; | |
299 tw = waw - mw; | |
300 | |
301 for(i = 0, c = clients; c; c = c->next) | |
302 if(isvisible(c)) { | |
303 if(c->isfloat) { | |
304 resize(c, True); | |
305 continue; | |
306 } | |
307 c->ismax = False; | |
308 c->x = wax; | |
309 c->y = way; | |
310 if(i < nmaster) { | |
311 c->y += i * mh; | |
312 c->w = mw - 2 * BORDERPX; | |
313 c->h = mh - 2 * BORDERPX; | |
314 } | |
315 else { /* tile window */ | |
316 c->x += mw; | |
317 c->w = tw - 2 * BORDERPX; | |
318 if(th > 2 * BORDERPX) { | |
319 c->y += (i - nmaster) * th; | |
320 c->h = th - 2 * BORDERPX; | |
321 } | |
322 else /* fallback if th <= 2 * BORDERPX */ | |
323 c->h = wah - 2 * BORDERPX; | |
324 } | |
325 resize(c, False); | |
326 i++; | |
327 } | |
328 else | |
329 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y); | |
330 if(!sel || !isvisible(sel)) { | |
331 for(c = stack; c && !isvisible(c); c = c->snext); | |
332 focus(c); | |
333 } | |
334 restack(); | |
335 } | |
336 | |
337 /* begin code by mitch */ | |
338 void | |
339 arrangemax(Client *c) { | |
340 if(c == sel) { | |
341 c->ismax = True; | |
342 c->x = sx; | |
343 c->y = bh; | |
344 c->w = sw - 2 * BORDERPX; | |
345 c->h = sh - bh - 2 * BORDERPX; | |
346 XRaiseWindow(dpy, c->win); | |
347 } else { | |
348 c->ismax = False; | |
349 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y); | |
350 XLowerWindow(dpy, c->win); | |
351 } | |
352 } | |
353 | |
354 void | |
355 domax(void) { | |
356 Client *c; | |
357 | |
358 for(c = clients; c; c = c->next) { | |
359 if(isvisible(c)) { | |
360 if(c->isfloat) { | |
361 resize(c, True); | |
362 continue; | |
363 } | |
364 arrangemax(c); | |
365 resize(c, False); | |
366 } else { | |
367 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y); | |
368 } | |
369 | |
370 } | |
371 if(!sel || !isvisible(sel)) { | |
372 for(c = stack; c && !isvisible(c); c = c->snext); | |
373 focus(c); | |
374 } | |
375 restack(); | |
376 } | |
377 /* end code by mitch */ | |
378 | |
379 void | |
380 focusnext(Arg *arg) { | |
381 Client *c; | |
382 | |
383 if(!sel) | |
384 return; | |
385 if(!(c = getnext(sel->next))) | |
386 c = getnext(clients); | |
387 if(c) { | |
388 focus(c); | |
389 restack(); | |
390 } | |
391 } | |
392 | |
393 void | |
394 incnmaster(Arg *arg) { | |
395 if((arrange == dofloat) || (nmaster + arg->i < 1) | |
396 || (wah / (nmaster + arg->i) <= 2 * BORDERPX)) | |
397 return; | |
398 nmaster += arg->i; | |
399 if(sel) | |
400 arrange(); | |
401 else | |
402 drawstatus(); | |
403 } | |
404 | |
405 Bool | |
406 isvisible(Client *c) { | |
407 unsigned int i; | |
408 | |
409 for(i = 0; i < ntags; i++) | |
410 if(c->tags[i] && seltag[i]) | |
411 return True; | |
412 return False; | |
413 } | |
414 | |
415 void | |
416 restack(void) { | |
417 Client *c; | |
418 XEvent ev; | |
419 | |
420 drawstatus(); | |
421 if(!sel) | |
422 return; | |
423 if(sel->isfloat || arrange == dofloat) | |
424 XRaiseWindow(dpy, sel->win); | |
425 | |
426 /* begin code by mitch */ | |
427 if(arrange == domax) { | |
428 for(c = nexttiled(clients); c; c = nexttiled(c->next)) { | |
429 arrangemax(c); | |
430 resize(c, False); | |
431 } | |
432 | |
433 } else if (arrange == dotile) { | |
434 /* end code by mitch */ | |
435 | |
436 if(!sel->isfloat) | |
437 XLowerWindow(dpy, sel->win); | |
438 for(c = nexttiled(clients); c; c = nexttiled(c->next)) { | |
439 if(c == sel) | |
440 continue; | |
441 XLowerWindow(dpy, c->win); | |
442 } | |
443 } | |
444 XSync(dpy, False); | |
445 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev)); | |
446 } | |
447 | |
448 void | |
449 togglefloat(Arg *arg) { | |
450 if (!sel || arrange == dofloat) | |
451 return; | |
452 sel->isfloat = !sel->isfloat; | |
453 arrange(); | |
454 } | |
455 | |
456 void | |
457 togglemode(Arg *arg) { | |
458 /* only toggle between tile and max - float is just available through togglefloat */ | |
459 arrange = (arrange == dotile) ? domax : dotile; | |
460 if(sel) | |
461 arrange(); | |
462 else | |
463 drawstatus(); | |
464 } | |
465 | |
466 void | |
467 view(Arg *arg) { | |
468 unsigned int i; | |
469 | |
470 for(i = 0; i < ntags; i++) | |
471 seltag[i] = (arg->i == -1) ? True : False; | |
472 if(arg->i >= 0 && arg->i < ntags) | |
473 seltag[arg->i] = True; | |
474 arrange(); | |
475 } | |
476 | |
477 void | |
478 zoom(Arg *arg) { | |
479 unsigned int n; | |
480 Client *c; | |
481 | |
482 if(!sel) | |
483 return; | |
484 if(sel->isfloat || (arrange == dofloat)) { | |
485 togglemax(sel); | |
486 return; | |
487 } | |
488 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next)) | |
489 n++; | |
490 | |
491 if((c = sel) == nexttiled(clients)) | |
492 if(!(c = nexttiled(c->next))) | |
493 return; | |
494 detach(c); | |
495 if(clients) | |
496 clients->prev = c; | |
497 c->next = clients; | |
498 clients = c; | |
499 focus(c); | |
500 arrange(); | |
501 } | |
502 | |
503 | |
504 | |
505 | |
506 | |
507 | |
508 | |
509 | |
510 | |
511 | |
512 | |
513 | |
514 | |
515 | |
516 | |
517 | |
518 /* from util.c */ | |
519 | |
520 | |
521 void * | |
522 emallocz(unsigned int size) { | |
523 void *res = calloc(1, size); | |
524 | |
525 if(!res) | |
526 eprint("fatal: could not malloc() %u bytes\n", size); | |
527 return res; | |
528 } | |
529 | |
530 void | |
531 eprint(const char *errstr, ...) { | |
532 va_list ap; | |
533 | |
534 va_start(ap, errstr); | |
535 vfprintf(stderr, errstr, ap); | |
536 va_end(ap); | |
537 exit(EXIT_FAILURE); | |
538 } | |
539 | |
540 void | |
541 spawn(Arg *arg) { | |
542 static char *shell = NULL; | |
543 | |
544 if(!shell && !(shell = getenv("SHELL"))) | |
545 shell = "/bin/sh"; | |
546 if(!arg->cmd) | |
547 return; | |
548 /* The double-fork construct avoids zombie processes and keeps the code | |
549 * clean from stupid signal handlers. */ | |
550 if(fork() == 0) { | |
551 if(fork() == 0) { | |
552 if(dpy) | |
553 close(ConnectionNumber(dpy)); | |
554 setsid(); | |
555 execl(shell, shell, "-c", arg->cmd, (char *)NULL); | |
556 fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg->cmd); | |
557 perror(" failed"); | |
558 } | |
559 exit(0); | |
560 } | |
561 wait(0); | |
562 } | |
563 | |
564 | |
565 | |
566 | |
567 | |
568 | |
569 | |
570 | |
571 | |
572 | |
573 | |
574 | |
575 | |
576 /* from tag.c */ | |
577 | |
578 /* static */ | |
579 | |
580 Client * | |
581 getnext(Client *c) { | |
582 for(; c && !isvisible(c); c = c->next); | |
583 return c; | |
584 } | |
585 | |
586 Client * | |
587 getprev(Client *c) { | |
588 for(; c && !isvisible(c); c = c->prev); | |
589 return c; | |
590 } | |
591 | |
592 void | |
593 initrregs(void) { | |
594 unsigned int i; | |
595 regex_t *reg; | |
596 | |
597 if(rreg) | |
598 return; | |
599 len = sizeof rule / sizeof rule[0]; | |
600 rreg = emallocz(len * sizeof(RReg)); | |
601 for(i = 0; i < len; i++) { | |
602 if(rule[i].clpattern) { | |
603 reg = emallocz(sizeof(regex_t)); | |
604 if(regcomp(reg, rule[i].clpattern, REG_EXTENDED)) | |
605 free(reg); | |
606 else | |
607 rreg[i].clregex = reg; | |
608 } | |
609 if(rule[i].tpattern) { | |
610 reg = emallocz(sizeof(regex_t)); | |
611 if(regcomp(reg, rule[i].tpattern, REG_EXTENDED)) | |
612 free(reg); | |
613 else | |
614 rreg[i].tregex = reg; | |
615 } | |
616 } | |
617 } | |
618 | |
619 void | |
620 settags(Client *c, Client *trans) { | |
621 char prop[512]; | |
622 unsigned int i, j; | |
623 regmatch_t tmp; | |
624 Bool matched = trans != NULL; | |
625 XClassHint ch = { 0 }; | |
626 | |
627 if(matched) { | |
628 for(i = 0; i < ntags; i++) | |
629 c->tags[i] = trans->tags[i]; | |
630 } | |
631 else { | |
632 XGetClassHint(dpy, c->win, &ch); | |
633 snprintf(prop, sizeof prop, "%s:%s:%s", | |
634 ch.res_class ? ch.res_class : "", | |
635 ch.res_name ? ch.res_name : "", c->name); | |
636 for(i = 0; i < len; i++) | |
637 if(rreg[i].clregex && !regexec(rreg[i].clregex, prop, 1, &tmp, 0)) { | |
638 c->isfloat = rule[i].isfloat; | |
639 for(j = 0; rreg[i].tregex && j < ntags; j++) { | |
640 if(!regexec(rreg[i].tregex, tags[j], 1, &tmp, 0)) { | |
641 matched = True; | |
642 c->tags[j] = True; | |
643 } | |
644 } | |
645 break; /* perform only the first rule matching */ | |
646 } | |
647 if(ch.res_class) | |
648 XFree(ch.res_class); | |
649 if(ch.res_name) | |
650 XFree(ch.res_name); | |
651 } | |
652 if(!matched) | |
653 for(i = 0; i < ntags; i++) | |
654 c->tags[i] = seltag[i]; | |
655 } | |
656 | |
657 void | |
658 tag(Arg *arg) { | |
659 unsigned int i; | |
660 | |
661 if(!sel) | |
662 return; | |
663 for(i = 0; i < ntags; i++) | |
664 sel->tags[i] = (arg->i == -1) ? True : False; | |
665 if(arg->i >= 0 && arg->i < ntags) | |
666 sel->tags[arg->i] = True; | |
667 arrange(); | |
668 } | |
669 | |
670 void | |
671 toggletag(Arg *arg) { | |
672 unsigned int i; | |
673 | |
674 if(!sel) | |
675 return; | |
676 sel->tags[arg->i] = !sel->tags[arg->i]; | |
677 for(i = 0; i < ntags && !sel->tags[i]; i++); | |
678 if(i == ntags) | |
679 sel->tags[arg->i] = True; | |
680 arrange(); | |
681 } | |
682 | |
683 /* begin code by jukka */ | |
684 void | |
685 viewnext(Arg *arg) { | |
686 unsigned int i; | |
687 Bool last = seltag[ntags-1]; | |
688 | |
689 for (i=ntags-1; i>0; --i) | |
690 seltag[i] = seltag[i-1]; | |
691 seltag[0] = last; | |
692 arrange(); | |
693 } | |
694 /* end code by jukka */ | |
695 | |
696 | |
697 | |
698 | |
699 | |
700 | |
701 | |
702 | |
703 | |
704 | |
705 | |
706 | |
707 | |
708 | |
709 | |
710 | |
711 /* from event.c */ | |
712 /* static */ | |
713 | |
714 KEYS | |
715 | |
716 | |
717 | |
718 static void | |
719 movemouse(Client *c) { | |
720 int x1, y1, ocx, ocy, di; | |
721 unsigned int dui; | |
722 Window dummy; | |
723 XEvent ev; | |
724 | |
725 ocx = c->x; | |
726 ocy = c->y; | |
727 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, | |
728 None, cursor[CurMove], CurrentTime) != GrabSuccess) | |
729 return; | |
730 c->ismax = False; | |
731 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui); | |
732 for(;;) { | |
733 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev); | |
734 switch (ev.type) { | |
735 case ButtonRelease: | |
736 resize(c, True); | |
737 XUngrabPointer(dpy, CurrentTime); | |
738 return; | |
739 case ConfigureRequest: | |
740 case Expose: | |
741 case MapRequest: | |
742 handler[ev.type](&ev); | |
743 break; | |
744 case MotionNotify: | |
745 XSync(dpy, False); | |
746 c->x = ocx + (ev.xmotion.x - x1); | |
747 c->y = ocy + (ev.xmotion.y - y1); | |
748 if(abs(wax + c->x) < SNAP) | |
749 c->x = wax; | |
750 else if(abs((wax + waw) - (c->x + c->w + 2 * c->border)) < SNAP) | |
751 c->x = wax + waw - c->w - 2 * c->border; | |
752 if(abs(way - c->y) < SNAP) | |
753 c->y = way; | |
754 else if(abs((way + wah) - (c->y + c->h + 2 * c->border)) < SNAP) | |
755 c->y = way + wah - c->h - 2 * c->border; | |
756 resize(c, False); | |
757 break; | |
758 } | |
759 } | |
760 } | |
761 | |
762 static void | |
763 resizemouse(Client *c) { | |
764 int ocx, ocy; | |
765 int nw, nh; | |
766 XEvent ev; | |
767 | |
768 ocx = c->x; | |
769 ocy = c->y; | |
770 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, | |
771 None, cursor[CurResize], CurrentTime) != GrabSuccess) | |
772 return; | |
773 c->ismax = False; | |
774 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1); | |
775 for(;;) { | |
776 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev); | |
777 switch(ev.type) { | |
778 case ButtonRelease: | |
779 resize(c, True); | |
780 XUngrabPointer(dpy, CurrentTime); | |
781 return; | |
782 case ConfigureRequest: | |
783 case Expose: | |
784 case MapRequest: | |
785 handler[ev.type](&ev); | |
786 break; | |
787 case MotionNotify: | |
788 XSync(dpy, False); | |
789 nw = ev.xmotion.x - ocx - 2 * c->border + 1; | |
790 c->w = nw > 0 ? nw : 1; | |
791 nh = ev.xmotion.y - ocy - 2 * c->border + 1; | |
792 c->h = nh > 0 ? nh : 1; | |
793 resize(c, True); | |
794 break; | |
795 } | |
796 } | |
797 } | |
798 | |
799 static void | |
800 buttonpress(XEvent *e) { | |
801 int x; | |
802 Arg a; | |
803 Client *c; | |
804 XButtonPressedEvent *ev = &e->xbutton; | |
805 | |
806 if(barwin == ev->window) { | |
807 x = 0; | |
808 for(a.i = 0; a.i < ntags; a.i++) { | |
809 x += textw(tags[a.i]); | |
810 if(ev->x < x) { | |
811 if(ev->button == Button1) { | |
812 view(&a); | |
813 } | |
814 return; | |
815 } | |
816 } | |
817 if(ev->x < x + bmw) | |
818 if (ev->button == Button1) { | |
819 togglemode(NULL); | |
820 } | |
821 } | |
822 else if((c = getclient(ev->window))) { | |
823 focus(c); | |
824 if(CLEANMASK(ev->state) != MODKEY) | |
825 return; | |
826 if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) { | |
827 restack(); | |
828 movemouse(c); | |
829 } | |
830 else if(ev->button == Button2) | |
831 zoom(NULL); | |
832 else if(ev->button == Button3 && (arrange == dofloat || c->isfloat) && | |
833 !c->isfixed) { | |
834 restack(); | |
835 resizemouse(c); | |
836 } | |
837 } | |
838 } | |
839 | |
840 static void | |
841 configurerequest(XEvent *e) { | |
842 unsigned long newmask; | |
843 Client *c; | |
844 XConfigureRequestEvent *ev = &e->xconfigurerequest; | |
845 XWindowChanges wc; | |
846 | |
847 if((c = getclient(ev->window))) { | |
848 c->ismax = False; | |
849 if(ev->value_mask & CWX) | |
850 c->x = ev->x; | |
851 if(ev->value_mask & CWY) | |
852 c->y = ev->y; | |
853 if(ev->value_mask & CWWidth) | |
854 c->w = ev->width; | |
855 if(ev->value_mask & CWHeight) | |
856 c->h = ev->height; | |
857 if(ev->value_mask & CWBorderWidth) | |
858 c->border = ev->border_width; | |
859 wc.x = c->x; | |
860 wc.y = c->y; | |
861 wc.width = c->w; | |
862 wc.height = c->h; | |
863 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth)); | |
864 if(newmask) | |
865 XConfigureWindow(dpy, c->win, newmask, &wc); | |
866 else | |
867 configure(c); | |
868 XSync(dpy, False); | |
869 if(c->isfloat) { | |
870 resize(c, False); | |
871 if(!isvisible(c)) | |
872 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y); | |
873 } | |
874 else | |
875 arrange(); | |
876 } | |
877 else { | |
878 wc.x = ev->x; | |
879 wc.y = ev->y; | |
880 wc.width = ev->width; | |
881 wc.height = ev->height; | |
882 wc.border_width = ev->border_width; | |
883 wc.sibling = ev->above; | |
884 wc.stack_mode = ev->detail; | |
885 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc); | |
886 XSync(dpy, False); | |
887 } | |
888 } | |
889 | |
890 static void | |
891 destroynotify(XEvent *e) { | |
892 Client *c; | |
893 XDestroyWindowEvent *ev = &e->xdestroywindow; | |
894 | |
895 if((c = getclient(ev->window))) | |
896 unmanage(c); | |
897 } | |
898 | |
899 static void | |
900 enternotify(XEvent *e) { | |
901 Client *c; | |
902 XCrossingEvent *ev = &e->xcrossing; | |
903 | |
904 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior) | |
905 return; | |
906 if((c = getclient(ev->window)) && isvisible(c)) | |
907 focus(c); | |
908 else if(ev->window == root) { | |
909 selscreen = True; | |
910 for(c = stack; c && !isvisible(c); c = c->snext); | |
911 focus(c); | |
912 } | |
913 } | |
914 | |
915 static void | |
916 expose(XEvent *e) { | |
917 XExposeEvent *ev = &e->xexpose; | |
918 | |
919 if(ev->count == 0) { | |
920 if(barwin == ev->window) | |
921 drawstatus(); | |
922 } | |
923 } | |
924 | |
925 static void | |
926 keypress(XEvent *e) { | |
927 static unsigned int len = sizeof key / sizeof key[0]; | |
928 unsigned int i; | |
929 KeySym keysym; | |
930 XKeyEvent *ev = &e->xkey; | |
931 | |
932 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0); | |
933 for(i = 0; i < len; i++) { | |
934 if(keysym == key[i].keysym | |
935 && CLEANMASK(key[i].mod) == CLEANMASK(ev->state)) | |
936 { | |
937 if(key[i].func) | |
938 key[i].func(&key[i].arg); | |
939 } | |
940 } | |
941 } | |
942 | |
943 static void | |
944 leavenotify(XEvent *e) { | |
945 XCrossingEvent *ev = &e->xcrossing; | |
946 | |
947 if((ev->window == root) && !ev->same_screen) { | |
948 selscreen = False; | |
949 focus(NULL); | |
950 } | |
951 } | |
952 | |
953 static void | |
954 mappingnotify(XEvent *e) { | |
955 XMappingEvent *ev = &e->xmapping; | |
956 | |
957 XRefreshKeyboardMapping(ev); | |
958 if(ev->request == MappingKeyboard) | |
959 grabkeys(); | |
960 } | |
961 | |
962 static void | |
963 maprequest(XEvent *e) { | |
964 static XWindowAttributes wa; | |
965 XMapRequestEvent *ev = &e->xmaprequest; | |
966 | |
967 if(!XGetWindowAttributes(dpy, ev->window, &wa)) | |
968 return; | |
969 if(wa.override_redirect) { | |
970 XSelectInput(dpy, ev->window, | |
971 (StructureNotifyMask | PropertyChangeMask)); | |
972 return; | |
973 } | |
974 if(!getclient(ev->window)) | |
975 manage(ev->window, &wa); | |
976 } | |
977 | |
978 static void | |
979 propertynotify(XEvent *e) { | |
980 Client *c; | |
981 Window trans; | |
982 XPropertyEvent *ev = &e->xproperty; | |
983 | |
984 if(ev->state == PropertyDelete) | |
985 return; /* ignore */ | |
986 if((c = getclient(ev->window))) { | |
987 switch (ev->atom) { | |
988 default: break; | |
989 case XA_WM_TRANSIENT_FOR: | |
990 XGetTransientForHint(dpy, c->win, &trans); | |
991 if(!c->isfloat && (c->isfloat = (trans != 0))) | |
992 arrange(); | |
993 break; | |
994 case XA_WM_NORMAL_HINTS: | |
995 updatesizehints(c); | |
996 break; | |
997 } | |
998 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) { | |
999 updatetitle(c); | |
1000 if(c == sel) | |
1001 drawstatus(); | |
1002 } | |
1003 } | |
1004 } | |
1005 | |
1006 static void | |
1007 unmapnotify(XEvent *e) { | |
1008 Client *c; | |
1009 XUnmapEvent *ev = &e->xunmap; | |
1010 | |
1011 if((c = getclient(ev->window))) | |
1012 unmanage(c); | |
1013 } | |
1014 | |
1015 | |
1016 | |
1017 void (*handler[LASTEvent]) (XEvent *) = { | |
1018 [ButtonPress] = buttonpress, | |
1019 [ConfigureRequest] = configurerequest, | |
1020 [DestroyNotify] = destroynotify, | |
1021 [EnterNotify] = enternotify, | |
1022 [LeaveNotify] = leavenotify, | |
1023 [Expose] = expose, | |
1024 [KeyPress] = keypress, | |
1025 [MappingNotify] = mappingnotify, | |
1026 [MapRequest] = maprequest, | |
1027 [PropertyNotify] = propertynotify, | |
1028 [UnmapNotify] = unmapnotify | |
1029 }; | |
1030 | |
1031 void | |
1032 grabkeys(void) { | |
1033 static unsigned int len = sizeof key / sizeof key[0]; | |
1034 unsigned int i; | |
1035 KeyCode code; | |
1036 | |
1037 XUngrabKey(dpy, AnyKey, AnyModifier, root); | |
1038 for(i = 0; i < len; i++) { | |
1039 code = XKeysymToKeycode(dpy, key[i].keysym); | |
1040 XGrabKey(dpy, code, key[i].mod, root, True, | |
1041 GrabModeAsync, GrabModeAsync); | |
1042 XGrabKey(dpy, code, key[i].mod | LockMask, root, True, | |
1043 GrabModeAsync, GrabModeAsync); | |
1044 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True, | |
1045 GrabModeAsync, GrabModeAsync); | |
1046 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True, | |
1047 GrabModeAsync, GrabModeAsync); | |
1048 } | |
1049 } | |
1050 | |
1051 void | |
1052 procevent(void) { | |
1053 XEvent ev; | |
1054 | |
1055 while(XPending(dpy)) { | |
1056 XNextEvent(dpy, &ev); | |
1057 if(handler[ev.type]) | |
1058 (handler[ev.type])(&ev); /* call handler */ | |
1059 } | |
1060 } | |
1061 | |
1062 | |
1063 | |
1064 | |
1065 | |
1066 | |
1067 | |
1068 | |
1069 | |
1070 | |
1071 | |
1072 | |
1073 | |
1074 | |
1075 | |
1076 /* from draw.c */ | |
1077 /* static */ | |
1078 | |
1079 static unsigned int | |
1080 textnw(const char *text, unsigned int len) { | |
1081 XRectangle r; | |
1082 | |
1083 if(dc.font.set) { | |
1084 XmbTextExtents(dc.font.set, text, len, NULL, &r); | |
1085 return r.width; | |
1086 } | |
1087 return XTextWidth(dc.font.xfont, text, len); | |
1088 } | |
1089 | |
1090 static void | |
1091 drawtext(const char *text, unsigned long col[ColLast]) { | |
1092 int x, y, w, h; | |
1093 static char buf[256]; | |
1094 unsigned int len, olen; | |
1095 XGCValues gcv; | |
1096 XRectangle r = { dc.x, dc.y, dc.w, dc.h }; | |
1097 | |
1098 XSetForeground(dpy, dc.gc, col[ColBG]); | |
1099 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1); | |
1100 if(!text) | |
1101 return; | |
1102 w = 0; | |
1103 olen = len = strlen(text); | |
1104 if(len >= sizeof buf) | |
1105 len = sizeof buf - 1; | |
1106 memcpy(buf, text, len); | |
1107 buf[len] = 0; | |
1108 h = dc.font.ascent + dc.font.descent; | |
1109 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent; | |
1110 x = dc.x + (h / 2); | |
1111 /* shorten text if necessary */ | |
1112 while(len && (w = textnw(buf, len)) > dc.w - h) | |
1113 buf[--len] = 0; | |
1114 if(len < olen) { | |
1115 if(len > 1) | |
1116 buf[len - 1] = '.'; | |
1117 if(len > 2) | |
1118 buf[len - 2] = '.'; | |
1119 if(len > 3) | |
1120 buf[len - 3] = '.'; | |
1121 } | |
1122 if(w > dc.w) | |
1123 return; /* too long */ | |
1124 gcv.foreground = col[ColFG]; | |
1125 if(dc.font.set) { | |
1126 XChangeGC(dpy, dc.gc, GCForeground, &gcv); | |
1127 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len); | |
1128 } else { | |
1129 gcv.font = dc.font.xfont->fid; | |
1130 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv); | |
1131 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len); | |
1132 } | |
1133 } | |
1134 | |
1135 | |
1136 | |
1137 void | |
1138 drawstatus(void) { | |
1139 int i, x; | |
1140 | |
1141 dc.x = dc.y = 0; | |
1142 for(i = 0; i < ntags; i++) { | |
1143 dc.w = textw(tags[i]); | |
1144 drawtext(tags[i], (seltag[i] ? dc.sel : dc.norm)); | |
1145 dc.x += dc.w + 1; | |
1146 } | |
1147 dc.w = bmw; | |
1148 drawtext("", dc.norm); | |
1149 x = dc.x + dc.w; | |
1150 dc.w = textw(stext); | |
1151 dc.x = sw - dc.w; | |
1152 if(dc.x < x) { | |
1153 dc.x = x; | |
1154 dc.w = sw - x; | |
1155 } | |
1156 drawtext(stext, dc.norm); | |
1157 if((dc.w = dc.x - x) > bh) { | |
1158 dc.x = x; | |
1159 drawtext(sel ? sel->name : NULL, dc.norm); | |
1160 } | |
1161 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0); | |
1162 XSync(dpy, False); | |
1163 } | |
1164 | |
1165 unsigned long | |
1166 getcolor(const char *colstr) { | |
1167 Colormap cmap = DefaultColormap(dpy, screen); | |
1168 XColor color; | |
1169 | |
1170 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color)) | |
1171 eprint("error, cannot allocate color '%s'\n", colstr); | |
1172 return color.pixel; | |
1173 } | |
1174 | |
1175 void | |
1176 setfont(const char *fontstr) { | |
1177 char *def, **missing; | |
1178 int i, n; | |
1179 | |
1180 missing = NULL; | |
1181 if(dc.font.set) | |
1182 XFreeFontSet(dpy, dc.font.set); | |
1183 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def); | |
1184 if(missing) { | |
1185 while(n--) | |
1186 fprintf(stderr, "missing fontset: %s\n", missing[n]); | |
1187 XFreeStringList(missing); | |
1188 } | |
1189 if(dc.font.set) { | |
1190 XFontSetExtents *font_extents; | |
1191 XFontStruct **xfonts; | |
1192 char **font_names; | |
1193 dc.font.ascent = dc.font.descent = 0; | |
1194 font_extents = XExtentsOfFontSet(dc.font.set); | |
1195 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names); | |
1196 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) { | |
1197 if(dc.font.ascent < (*xfonts)->ascent) | |
1198 dc.font.ascent = (*xfonts)->ascent; | |
1199 if(dc.font.descent < (*xfonts)->descent) | |
1200 dc.font.descent = (*xfonts)->descent; | |
1201 xfonts++; | |
1202 } | |
1203 } else { | |
1204 if(dc.font.xfont) | |
1205 XFreeFont(dpy, dc.font.xfont); | |
1206 dc.font.xfont = NULL; | |
1207 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))) | |
1208 eprint("error, cannot load font: '%s'\n", fontstr); | |
1209 dc.font.ascent = dc.font.xfont->ascent; | |
1210 dc.font.descent = dc.font.xfont->descent; | |
1211 } | |
1212 dc.font.height = dc.font.ascent + dc.font.descent; | |
1213 } | |
1214 | |
1215 unsigned int | |
1216 textw(const char *text) { | |
1217 return textnw(text, strlen(text)) + dc.font.height; | |
1218 } | |
1219 | |
1220 | |
1221 | |
1222 | |
1223 | |
1224 | |
1225 | |
1226 | |
1227 | |
1228 | |
1229 | |
1230 /* from client.c */ | |
1231 /* static */ | |
1232 | |
1233 static void | |
1234 detachstack(Client *c) { | |
1235 Client **tc; | |
1236 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext); | |
1237 *tc = c->snext; | |
1238 } | |
1239 | |
1240 static void | |
1241 grabbuttons(Client *c, Bool focused) { | |
1242 XUngrabButton(dpy, AnyButton, AnyModifier, c->win); | |
1243 | |
1244 if(focused) { | |
1245 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK, | |
1246 GrabModeAsync, GrabModeSync, None, None); | |
1247 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK, | |
1248 GrabModeAsync, GrabModeSync, None, None); | |
1249 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK, | |
1250 GrabModeAsync, GrabModeSync, None, None); | |
1251 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK, | |
1252 GrabModeAsync, GrabModeSync, None, None); | |
1253 | |
1254 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK, | |
1255 GrabModeAsync, GrabModeSync, None, None); | |
1256 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK, | |
1257 GrabModeAsync, GrabModeSync, None, None); | |
1258 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK, | |
1259 GrabModeAsync, GrabModeSync, None, None); | |
1260 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK, | |
1261 GrabModeAsync, GrabModeSync, None, None); | |
1262 | |
1263 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK, | |
1264 GrabModeAsync, GrabModeSync, None, None); | |
1265 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK, | |
1266 GrabModeAsync, GrabModeSync, None, None); | |
1267 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK, | |
1268 GrabModeAsync, GrabModeSync, None, None); | |
1269 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK, | |
1270 GrabModeAsync, GrabModeSync, None, None); | |
1271 } else { | |
1272 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK, | |
1273 GrabModeAsync, GrabModeSync, None, None); | |
1274 } | |
1275 } | |
1276 | |
1277 static void | |
1278 setclientstate(Client *c, long state) { | |
1279 long data[] = {state, None}; | |
1280 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32, | |
1281 PropModeReplace, (unsigned char *)data, 2); | |
1282 } | |
1283 | |
1284 static int | |
1285 xerrordummy(Display *dsply, XErrorEvent *ee) { | |
1286 return 0; | |
1287 } | |
1288 | |
1289 | |
1290 | |
1291 void | |
1292 configure(Client *c) { | |
1293 XEvent synev; | |
1294 | |
1295 synev.type = ConfigureNotify; | |
1296 synev.xconfigure.display = dpy; | |
1297 synev.xconfigure.event = c->win; | |
1298 synev.xconfigure.window = c->win; | |
1299 synev.xconfigure.x = c->x; | |
1300 synev.xconfigure.y = c->y; | |
1301 synev.xconfigure.width = c->w; | |
1302 synev.xconfigure.height = c->h; | |
1303 synev.xconfigure.border_width = c->border; | |
1304 synev.xconfigure.above = None; | |
1305 XSendEvent(dpy, c->win, True, NoEventMask, &synev); | |
1306 } | |
1307 | |
1308 void | |
1309 focus(Client *c) { | |
1310 if(c && !isvisible(c)) | |
1311 return; | |
1312 if(sel && sel != c) { | |
1313 grabbuttons(sel, False); | |
1314 XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]); | |
1315 } | |
1316 if(c) { | |
1317 detachstack(c); | |
1318 c->snext = stack; | |
1319 stack = c; | |
1320 grabbuttons(c, True); | |
1321 } | |
1322 sel = c; | |
1323 drawstatus(); | |
1324 if(!selscreen) | |
1325 return; | |
1326 if(c) { | |
1327 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]); | |
1328 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime); | |
1329 } else { | |
1330 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); | |
1331 } | |
1332 } | |
1333 | |
1334 Client * | |
1335 getclient(Window w) { | |
1336 Client *c; | |
1337 | |
1338 for(c = clients; c; c = c->next) { | |
1339 if(c->win == w) { | |
1340 return c; | |
1341 } | |
1342 } | |
1343 return NULL; | |
1344 } | |
1345 | |
1346 Bool | |
1347 isprotodel(Client *c) { | |
1348 int i, n; | |
1349 Atom *protocols; | |
1350 Bool ret = False; | |
1351 | |
1352 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) { | |
1353 for(i = 0; !ret && i < n; i++) | |
1354 if(protocols[i] == wmatom[WMDelete]) | |
1355 ret = True; | |
1356 XFree(protocols); | |
1357 } | |
1358 return ret; | |
1359 } | |
1360 | |
1361 void | |
1362 killclient(Arg *arg) { | |
1363 if(!sel) | |
1364 return; | |
1365 if(isprotodel(sel)) | |
1366 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]); | |
1367 else | |
1368 XKillClient(dpy, sel->win); | |
1369 } | |
1370 | |
1371 void | |
1372 manage(Window w, XWindowAttributes *wa) { | |
1373 Client *c; | |
1374 Window trans; | |
1375 | |
1376 c = emallocz(sizeof(Client)); | |
1377 c->tags = emallocz(ntags * sizeof(Bool)); | |
1378 c->win = w; | |
1379 c->x = wa->x; | |
1380 c->y = wa->y; | |
1381 c->w = wa->width; | |
1382 c->h = wa->height; | |
1383 if(c->w == sw && c->h == sh) { | |
1384 c->border = 0; | |
1385 c->x = sx; | |
1386 c->y = sy; | |
1387 } else { | |
1388 c->border = BORDERPX; | |
1389 if(c->x + c->w + 2 * c->border > wax + waw) | |
1390 c->x = wax + waw - c->w - 2 * c->border; | |
1391 if(c->y + c->h + 2 * c->border > way + wah) | |
1392 c->y = way + wah - c->h - 2 * c->border; | |
1393 if(c->x < wax) | |
1394 c->x = wax; | |
1395 if(c->y < way) | |
1396 c->y = way; | |
1397 } | |
1398 updatesizehints(c); | |
1399 XSelectInput(dpy, c->win, | |
1400 StructureNotifyMask | PropertyChangeMask | EnterWindowMask); | |
1401 XGetTransientForHint(dpy, c->win, &trans); | |
1402 grabbuttons(c, False); | |
1403 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]); | |
1404 updatetitle(c); | |
1405 settags(c, getclient(trans)); | |
1406 if(!c->isfloat) | |
1407 c->isfloat = trans || c->isfixed; | |
1408 if(clients) | |
1409 clients->prev = c; | |
1410 c->next = clients; | |
1411 c->snext = stack; | |
1412 stack = clients = c; | |
1413 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y); | |
1414 XMapWindow(dpy, c->win); | |
1415 setclientstate(c, NormalState); | |
1416 if(isvisible(c)) | |
1417 focus(c); | |
1418 arrange(); | |
1419 } | |
1420 | |
1421 void | |
1422 resize(Client *c, Bool sizehints) { | |
1423 float actual, dx, dy, max, min; | |
1424 XWindowChanges wc; | |
1425 | |
1426 if(c->w <= 0 || c->h <= 0) | |
1427 return; | |
1428 if(sizehints) { | |
1429 if(c->minw && c->w < c->minw) | |
1430 c->w = c->minw; | |
1431 if(c->minh && c->h < c->minh) | |
1432 c->h = c->minh; | |
1433 if(c->maxw && c->w > c->maxw) | |
1434 c->w = c->maxw; | |
1435 if(c->maxh && c->h > c->maxh) | |
1436 c->h = c->maxh; | |
1437 /* inspired by algorithm from fluxbox */ | |
1438 if(c->minay > 0 && c->maxay && (c->h - c->baseh) > 0) { | |
1439 dx = (float)(c->w - c->basew); | |
1440 dy = (float)(c->h - c->baseh); | |
1441 min = (float)(c->minax) / (float)(c->minay); | |
1442 max = (float)(c->maxax) / (float)(c->maxay); | |
1443 actual = dx / dy; | |
1444 if(max > 0 && min > 0 && actual > 0) { | |
1445 if(actual < min) { | |
1446 dy = (dx * min + dy) / (min * min + 1); | |
1447 dx = dy * min; | |
1448 c->w = (int)dx + c->basew; | |
1449 c->h = (int)dy + c->baseh; | |
1450 } | |
1451 else if(actual > max) { | |
1452 dy = (dx * min + dy) / (max * max + 1); | |
1453 dx = dy * min; | |
1454 c->w = (int)dx + c->basew; | |
1455 c->h = (int)dy + c->baseh; | |
1456 } | |
1457 } | |
1458 } | |
1459 if(c->incw) | |
1460 c->w -= (c->w - c->basew) % c->incw; | |
1461 if(c->inch) | |
1462 c->h -= (c->h - c->baseh) % c->inch; | |
1463 } | |
1464 if(c->w == sw && c->h == sh) | |
1465 c->border = 0; | |
1466 else | |
1467 c->border = BORDERPX; | |
1468 /* offscreen appearance fixes */ | |
1469 if(c->x > sw) | |
1470 c->x = sw - c->w - 2 * c->border; | |
1471 if(c->y > sh) | |
1472 c->y = sh - c->h - 2 * c->border; | |
1473 if(c->x + c->w + 2 * c->border < sx) | |
1474 c->x = sx; | |
1475 if(c->y + c->h + 2 * c->border < sy) | |
1476 c->y = sy; | |
1477 wc.x = c->x; | |
1478 wc.y = c->y; | |
1479 wc.width = c->w; | |
1480 wc.height = c->h; | |
1481 wc.border_width = c->border; | |
1482 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc); | |
1483 configure(c); | |
1484 XSync(dpy, False); | |
1485 } | |
1486 | |
1487 void | |
1488 updatesizehints(Client *c) { | |
1489 long msize; | |
1490 XSizeHints size; | |
1491 | |
1492 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags) | |
1493 size.flags = PSize; | |
1494 c->flags = size.flags; | |
1495 if(c->flags & PBaseSize) { | |
1496 c->basew = size.base_width; | |
1497 c->baseh = size.base_height; | |
1498 } else { | |
1499 c->basew = c->baseh = 0; | |
1500 } | |
1501 if(c->flags & PResizeInc) { | |
1502 c->incw = size.width_inc; | |
1503 c->inch = size.height_inc; | |
1504 } else { | |
1505 c->incw = c->inch = 0; | |
1506 } | |
1507 if(c->flags & PMaxSize) { | |
1508 c->maxw = size.max_width; | |
1509 c->maxh = size.max_height; | |
1510 } else { | |
1511 c->maxw = c->maxh = 0; | |
1512 } | |
1513 if(c->flags & PMinSize) { | |
1514 c->minw = size.min_width; | |
1515 c->minh = size.min_height; | |
1516 } else { | |
1517 c->minw = c->minh = 0; | |
1518 } | |
1519 if(c->flags & PAspect) { | |
1520 c->minax = size.min_aspect.x; | |
1521 c->minay = size.min_aspect.y; | |
1522 c->maxax = size.max_aspect.x; | |
1523 c->maxay = size.max_aspect.y; | |
1524 } else { | |
1525 c->minax = c->minay = c->maxax = c->maxay = 0; | |
1526 } | |
1527 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh && | |
1528 c->maxw == c->minw && c->maxh == c->minh); | |
1529 } | |
1530 | |
1531 void | |
1532 updatetitle(Client *c) { | |
1533 char **list = NULL; | |
1534 int n; | |
1535 XTextProperty name; | |
1536 | |
1537 name.nitems = 0; | |
1538 c->name[0] = 0; | |
1539 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]); | |
1540 if(!name.nitems) | |
1541 XGetWMName(dpy, c->win, &name); | |
1542 if(!name.nitems) | |
1543 return; | |
1544 if(name.encoding == XA_STRING) | |
1545 strncpy(c->name, (char *)name.value, sizeof c->name); | |
1546 else { | |
1547 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) { | |
1548 strncpy(c->name, *list, sizeof c->name); | |
1549 XFreeStringList(list); | |
1550 } | |
1551 } | |
1552 XFree(name.value); | |
1553 } | |
1554 | |
1555 void | |
1556 unmanage(Client *c) { | |
1557 Client *nc; | |
1558 | |
1559 /* The server grab construct avoids race conditions. */ | |
1560 XGrabServer(dpy); | |
1561 XSetErrorHandler(xerrordummy); | |
1562 detach(c); | |
1563 detachstack(c); | |
1564 if(sel == c) { | |
1565 for(nc = stack; nc && !isvisible(nc); nc = nc->snext); | |
1566 focus(nc); | |
1567 } | |
1568 XUngrabButton(dpy, AnyButton, AnyModifier, c->win); | |
1569 setclientstate(c, WithdrawnState); | |
1570 free(c->tags); | |
1571 free(c); | |
1572 XSync(dpy, False); | |
1573 XSetErrorHandler(xerror); | |
1574 XUngrabServer(dpy); | |
1575 arrange(); | |
1576 } | |
1577 | |
1578 | |
1579 | |
1580 | |
1581 | |
1582 | |
1583 | |
1584 | |
1585 | |
1586 | |
1587 | |
1588 | |
1589 | |
1590 | |
1591 | |
1592 | |
1593 | |
1594 | |
1595 | |
1596 /* static */ | |
1597 | |
1598 | |
1599 static void | |
1600 cleanup(void) { | |
1601 close(STDIN_FILENO); | |
1602 while(stack) { | |
1603 resize(stack, True); | |
1604 unmanage(stack); | |
1605 } | |
1606 if(dc.font.set) | |
1607 XFreeFontSet(dpy, dc.font.set); | |
1608 else | |
1609 XFreeFont(dpy, dc.font.xfont); | |
1610 XUngrabKey(dpy, AnyKey, AnyModifier, root); | |
1611 XFreePixmap(dpy, dc.drawable); | |
1612 XFreeGC(dpy, dc.gc); | |
1613 XDestroyWindow(dpy, barwin); | |
1614 XFreeCursor(dpy, cursor[CurNormal]); | |
1615 XFreeCursor(dpy, cursor[CurResize]); | |
1616 XFreeCursor(dpy, cursor[CurMove]); | |
1617 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); | |
1618 XSync(dpy, False); | |
1619 free(seltag); | |
1620 } | |
1621 | |
1622 static void | |
1623 scan(void) { | |
1624 unsigned int i, num; | |
1625 Window *wins, d1, d2; | |
1626 XWindowAttributes wa; | |
1627 | |
1628 wins = NULL; | |
1629 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) { | |
1630 for(i = 0; i < num; i++) { | |
1631 if(!XGetWindowAttributes(dpy, wins[i], &wa)) | |
1632 continue; | |
1633 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1)) | |
1634 continue; | |
1635 if(wa.map_state == IsViewable) | |
1636 manage(wins[i], &wa); | |
1637 } | |
1638 } | |
1639 if(wins) | |
1640 XFree(wins); | |
1641 } | |
1642 | |
1643 static void | |
1644 setup(void) { | |
1645 int i, j; | |
1646 unsigned int mask; | |
1647 Window w; | |
1648 XModifierKeymap *modmap; | |
1649 XSetWindowAttributes wa; | |
1650 | |
1651 /* init atoms */ | |
1652 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False); | |
1653 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False); | |
1654 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False); | |
1655 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False); | |
1656 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False); | |
1657 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32, | |
1658 PropModeReplace, (unsigned char *) netatom, NetLast); | |
1659 /* init cursors */ | |
1660 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr); | |
1661 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing); | |
1662 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur); | |
1663 /* init modifier map */ | |
1664 numlockmask = 0; | |
1665 modmap = XGetModifierMapping(dpy); | |
1666 for (i = 0; i < 8; i++) { | |
1667 for (j = 0; j < modmap->max_keypermod; j++) { | |
1668 if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock)) | |
1669 numlockmask = (1 << i); | |
1670 } | |
1671 } | |
1672 XFreeModifiermap(modmap); | |
1673 /* select for events */ | |
1674 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask | |
1675 | EnterWindowMask | LeaveWindowMask; | |
1676 wa.cursor = cursor[CurNormal]; | |
1677 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa); | |
1678 grabkeys(); | |
1679 initrregs(); | |
1680 for(ntags = 0; tags[ntags]; ntags++); | |
1681 seltag = emallocz(sizeof(Bool) * ntags); | |
1682 seltag[0] = True; | |
1683 /* style */ | |
1684 dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR); | |
1685 dc.norm[ColBG] = getcolor(NORMBGCOLOR); | |
1686 dc.norm[ColFG] = getcolor(NORMFGCOLOR); | |
1687 dc.sel[ColBorder] = getcolor(SELBORDERCOLOR); | |
1688 dc.sel[ColBG] = getcolor(SELBGCOLOR); | |
1689 dc.sel[ColFG] = getcolor(SELFGCOLOR); | |
1690 setfont(FONT); | |
1691 /* geometry */ | |
1692 sx = sy = 0; | |
1693 sw = DisplayWidth(dpy, screen); | |
1694 sh = DisplayHeight(dpy, screen); | |
1695 nmaster = NMASTER; | |
1696 bmw = 1; | |
1697 /* bar */ | |
1698 dc.h = bh = dc.font.height + 2; | |
1699 wa.override_redirect = 1; | |
1700 wa.background_pixmap = ParentRelative; | |
1701 wa.event_mask = ButtonPressMask | ExposureMask; | |
1702 barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0, | |
1703 DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen), | |
1704 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); | |
1705 XDefineCursor(dpy, barwin, cursor[CurNormal]); | |
1706 XMapRaised(dpy, barwin); | |
1707 strcpy(stext, "dwm-"VERSION); | |
1708 /* windowarea */ | |
1709 wax = sx; | |
1710 way = sy + bh; | |
1711 wah = sh - bh; | |
1712 waw = sw; | |
1713 /* pixmap for everything */ | |
1714 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen)); | |
1715 dc.gc = XCreateGC(dpy, root, 0, 0); | |
1716 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter); | |
1717 /* multihead support */ | |
1718 selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask); | |
1719 } | |
1720 | |
1721 /* | |
1722 * Startup Error handler to check if another window manager | |
1723 * is already running. | |
1724 */ | |
1725 static int | |
1726 xerrorstart(Display *dsply, XErrorEvent *ee) { | |
1727 otherwm = True; | |
1728 return -1; | |
1729 } | |
1730 | |
1731 | |
1732 | |
1733 void | |
1734 sendevent(Window w, Atom a, long value) { | |
1735 XEvent e; | |
1736 | |
1737 e.type = ClientMessage; | |
1738 e.xclient.window = w; | |
1739 e.xclient.message_type = a; | |
1740 e.xclient.format = 32; | |
1741 e.xclient.data.l[0] = value; | |
1742 e.xclient.data.l[1] = CurrentTime; | |
1743 XSendEvent(dpy, w, False, NoEventMask, &e); | |
1744 XSync(dpy, False); | |
1745 } | |
1746 | |
1747 void | |
1748 quit(Arg *arg) { | |
1749 readin = running = False; | |
1750 } | |
1751 | |
1752 /* There's no way to check accesses to destroyed windows, thus those cases are | |
1753 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs | |
1754 * default error handler, which may call exit. | |
1755 */ | |
1756 int | |
1757 xerror(Display *dpy, XErrorEvent *ee) { | |
1758 if(ee->error_code == BadWindow | |
1759 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch) | |
1760 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable) | |
1761 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable) | |
1762 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable) | |
1763 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch) | |
1764 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess) | |
1765 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable)) | |
1766 return 0; | |
1767 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n", | |
1768 ee->request_code, ee->error_code); | |
1769 return xerrorxlib(dpy, ee); /* may call exit */ | |
1770 } | |
1771 | |
1772 int | |
1773 main(int argc, char *argv[]) { | |
1774 char *p; | |
1775 int r, xfd; | |
1776 fd_set rd; | |
1777 | |
1778 if(argc == 2 && !strncmp("-v", argv[1], 3)) { | |
1779 fputs("dwm-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout); | |
1780 exit(EXIT_SUCCESS); | |
1781 } | |
1782 else if(argc != 1) | |
1783 eprint("usage: dwm [-v]\n"); | |
1784 setlocale(LC_CTYPE, ""); | |
1785 dpy = XOpenDisplay(0); | |
1786 if(!dpy) | |
1787 eprint("dwm: cannot open display\n"); | |
1788 xfd = ConnectionNumber(dpy); | |
1789 screen = DefaultScreen(dpy); | |
1790 root = RootWindow(dpy, screen); | |
1791 otherwm = False; | |
1792 XSetErrorHandler(xerrorstart); | |
1793 /* this causes an error if some other window manager is running */ | |
1794 XSelectInput(dpy, root, SubstructureRedirectMask); | |
1795 XSync(dpy, False); | |
1796 if(otherwm) | |
1797 eprint("dwm: another window manager is already running\n"); | |
1798 | |
1799 XSync(dpy, False); | |
1800 XSetErrorHandler(NULL); | |
1801 xerrorxlib = XSetErrorHandler(xerror); | |
1802 XSync(dpy, False); | |
1803 setup(); | |
1804 drawstatus(); | |
1805 scan(); | |
1806 | |
1807 /* main event loop, also reads status text from stdin */ | |
1808 XSync(dpy, False); | |
1809 procevent(); | |
1810 readin = True; | |
1811 while(running) { | |
1812 FD_ZERO(&rd); | |
1813 if(readin) | |
1814 FD_SET(STDIN_FILENO, &rd); | |
1815 FD_SET(xfd, &rd); | |
1816 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) { | |
1817 if(errno == EINTR) | |
1818 continue; | |
1819 eprint("select failed\n"); | |
1820 } | |
1821 if(FD_ISSET(STDIN_FILENO, &rd)) { | |
1822 switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) { | |
1823 case -1: | |
1824 strncpy(stext, strerror(errno), sizeof stext - 1); | |
1825 stext[sizeof stext - 1] = '\0'; | |
1826 readin = False; | |
1827 break; | |
1828 case 0: | |
1829 strncpy(stext, "EOF", 4); | |
1830 readin = False; | |
1831 break; | |
1832 default: | |
1833 for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0'); | |
1834 for(; p >= stext && *p != '\n'; --p); | |
1835 if(p > stext) | |
1836 strncpy(stext, p + 1, sizeof stext); | |
1837 } | |
1838 drawstatus(); | |
1839 } | |
1840 if(FD_ISSET(xfd, &rd)) | |
1841 procevent(); | |
1842 } | |
1843 cleanup(); | |
1844 XCloseDisplay(dpy); | |
1845 return 0; | |
1846 } |