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