aewl

view aewl.c @ 772:0c1e9952a278

naming change from dwm to aewl; cleanups
author meillo@marmaro.de
date Sat, 06 Dec 2008 11:15:51 +0100
parents 59a6f0ba5478
children d2e56ce18f5b
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/types.h>
51 #include <sys/wait.h>
52 #include <X11/cursorfont.h>
53 #include <X11/keysym.h>
54 #include <X11/Xatom.h>
55 #include <X11/Xlib.h>
56 #include <X11/Xproto.h>
57 #include <X11/Xutil.h>
59 #include "config.h"
62 /* mask shorthands, used in event.c and client.c */
63 #define BUTTONMASK (ButtonPressMask | ButtonReleaseMask)
65 enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
66 enum { WMProtocols, WMDelete, WMState, WMLast }; /* default atoms */
67 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
68 enum { ColFG, ColBG, ColLast }; /* color */
70 typedef struct {
71 int ascent;
72 int descent;
73 int height;
74 XFontSet set;
75 XFontStruct *xfont;
76 } Fnt;
78 typedef struct {
79 int x, y, w, h;
80 unsigned long norm[ColLast];
81 unsigned long sel[ColLast];
82 Drawable drawable;
83 Fnt font;
84 GC gc;
85 } DC; /* draw context */
87 typedef struct Client Client;
88 struct Client {
89 char name[256];
90 int x, y, w, h;
91 int rx, ry, rw, rh; /* revert geometry */
92 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
93 int minax, minay, maxax, maxay;
94 long flags;
95 unsigned int border;
96 Bool isfixed, isfloat, ismax;
97 Bool tag;
98 Client *next;
99 Client *prev;
100 Client *snext;
101 Window win;
102 };
104 typedef struct {
105 const char* class;
106 const char* instance;
107 const char* title;
108 int tag;
109 Bool isfloat;
110 } Rule;
113 typedef struct {
114 unsigned long mod;
115 KeySym keysym;
116 void (*func)(const char* cmd);
117 const char* cmd;
118 } Key;
121 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
122 #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
126 char stext[256]; /* status text */
127 int bh; /* bar height */
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 numlockmask; /* 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 = True;
136 Bool selscreen = True;
137 Bool seltag;
138 Client* clients = NULL; /* global client list */
139 Client* stack = NULL; /* global client stack */
140 Client* sel = NULL; /* selected client */
141 Cursor cursor[CurLast];
142 DC dc = {0}; /* global draw context */
143 Display *dpy;
144 Window root, barwin;
146 static int (*xerrorxlib)(Display *, XErrorEvent *);
147 static Bool otherwm, readin;
148 static unsigned int len = 0;
151 RULES
152 KEYS
156 /* client.c */
157 void configure(Client *c); /* send synthetic configure event */
158 void focus(Client *c); /* focus c, c may be NULL */
159 Client *getclient(Window w); /* return client of w */
160 Bool isprotodel(Client *c); /* returns True if c->win supports wmatom[WMDelete] */
161 void manage(Window w, XWindowAttributes *wa); /* manage new client */
162 void resize(Client *c, Bool sizehints); /* resize c*/
163 void updatesizehints(Client *c); /* update the size hint variables of c */
164 void updatetitle(Client *c); /* update the name of c */
165 void unmanage(Client *c); /* destroy c */
167 /* draw.c */
168 void drawstatus(void); /* draw the bar */
169 unsigned long getcolor(const char *colstr); /* return color of colstr */
170 void setfont(const char *fontstr); /* set the font for DC */
171 unsigned int textw(const char *text); /* return the width of text in px*/
173 /* event.c */
174 void grabkeys(void); /* grab all keys defined in config.h */
175 void procevent(void); /* process pending X events */
177 /* main.c */
178 void sendevent(Window w, Atom a, long value); /* send synthetic event to w */
179 int xerror(Display *dsply, XErrorEvent *ee); /* X error handler */
181 /* tag.c */
182 Client *getnext(Client *c); /* returns next visible client */
183 void settag(Client *c, Client *trans); /* sets tag of c */
185 /* util.c */
186 void *emallocz(unsigned int size); /* allocates zero-initialized memory, exits on error */
187 void eprint(const char *errstr, ...); /* prints errstr and exits with 1 */
189 /* view.c */
190 void detach(Client *c); /* detaches c from global client list */
191 void dotile(void); /* arranges all windows tiled */
192 void domax(void); /* arranges all windows fullscreen */
193 Bool isvisible(Client *c); /* returns True if client is visible */
194 void restack(void); /* restores z layers of all clients */
197 void toggleview(void); /* toggle the view */
198 void focusnext(void); /* focuses next visible client */
199 void zoom(void); /* zooms the focused client to master area */
200 void killclient(void); /* kill c nicely */
201 void quit(void); /* quit nicely */
202 void togglemode(void); /* toggles global arrange function (dotile/domax) */
203 void togglefloat(void); /* toggles focusesd client between floating/non-floating state */
204 void incnmaster(void); /* increments nmaster */
205 void decnmaster(void); /* decrements nmaster */
206 void toggletag(void); /* toggles tag of c */
207 void spawn(const char* cmd); /* forks a new subprocess with cmd */
218 /* from view.c */
219 /* static */
221 static Client *
222 nexttiled(Client *c) {
223 for(c = getnext(c); c && c->isfloat; c = getnext(c->next));
224 return c;
225 }
227 static void
228 togglemax(Client *c) {
229 XEvent ev;
231 if(c->isfixed)
232 return;
234 if((c->ismax = !c->ismax)) {
235 c->rx = c->x;
236 c->ry = c->y;
237 c->rw = c->w;
238 c->rh = c->h;
239 c->x = wax;
240 c->y = way;
241 c->w = waw - 2 * BORDERPX;
242 c->h = wah - 2 * BORDERPX;
243 } else {
244 c->x = c->rx;
245 c->y = c->ry;
246 c->w = c->rw;
247 c->h = c->rh;
248 }
249 resize(c, False);
250 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
251 }
255 void (*arrange)(void) = DEFMODE;
257 void
258 detach(Client *c) {
259 if(c->prev)
260 c->prev->next = c->next;
261 if(c->next)
262 c->next->prev = c->prev;
263 if(c == clients)
264 clients = c->next;
265 c->next = c->prev = NULL;
266 }
268 void
269 dotile(void) {
270 unsigned int i, n, mw, mh, tw, th;
271 Client *c;
273 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
274 n++;
275 /* window geoms */
276 mh = (n > nmaster) ? wah / nmaster : wah / (n > 0 ? n : 1);
277 mw = (n > nmaster) ? waw / 2 : waw;
278 th = (n > nmaster) ? wah / (n - nmaster) : 0;
279 tw = waw - mw;
281 for(i = 0, c = clients; c; c = c->next)
282 if(isvisible(c)) {
283 if(c->isfloat) {
284 resize(c, True);
285 continue;
286 }
287 c->ismax = False;
288 c->x = wax;
289 c->y = way;
290 if(i < nmaster) {
291 c->y += i * mh;
292 c->w = mw - 2 * BORDERPX;
293 c->h = mh - 2 * BORDERPX;
294 }
295 else { /* tile window */
296 c->x += mw;
297 c->w = tw - 2 * BORDERPX;
298 if(th > 2 * BORDERPX) {
299 c->y += (i - nmaster) * th;
300 c->h = th - 2 * BORDERPX;
301 }
302 else /* fallback if th <= 2 * BORDERPX */
303 c->h = wah - 2 * BORDERPX;
304 }
305 resize(c, False);
306 i++;
307 }
308 else
309 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
310 if(!sel || !isvisible(sel)) {
311 for(c = stack; c && !isvisible(c); c = c->snext);
312 focus(c);
313 }
314 restack();
315 }
317 void
318 domax(void) {
319 Client *c;
321 for(c = clients; c; c = c->next) {
322 if(isvisible(c)) {
323 if(c->isfloat) {
324 resize(c, True);
325 continue;
326 }
327 c->ismax = True;
328 c->x = wax;
329 c->y = way;
330 c->w = waw - 2 * BORDERPX;
331 c->h = wah - 2 * BORDERPX;
332 resize(c, False);
333 } else {
334 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
335 }
336 }
337 if(!sel || !isvisible(sel)) {
338 for(c = stack; c && !isvisible(c); c = c->snext);
339 focus(c);
340 }
341 restack();
342 }
344 void
345 focusnext() {
346 Client *c;
348 if(!sel)
349 return;
350 if(!(c = getnext(sel->next)))
351 c = getnext(clients);
352 if(c) {
353 focus(c);
354 restack();
355 }
356 }
358 void
359 incnmaster() {
360 if(wah / (nmaster + 1) <= 2 * BORDERPX)
361 return;
362 nmaster++;
363 if(sel)
364 arrange();
365 else
366 drawstatus();
367 }
369 void
370 decnmaster() {
371 if(nmaster <= 1)
372 return;
373 nmaster--;
374 if(sel)
375 arrange();
376 else
377 drawstatus();
378 }
380 Bool
381 isvisible(Client *c) {
382 return (c->tag == seltag);
383 }
385 void
386 restack(void) {
387 Client *c;
388 XEvent ev;
390 drawstatus();
391 if(!sel)
392 return;
393 if(sel->isfloat)
394 XRaiseWindow(dpy, sel->win);
396 if(!sel->isfloat)
397 XLowerWindow(dpy, sel->win);
398 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
399 if(c == sel)
400 continue;
401 XLowerWindow(dpy, c->win);
402 }
404 XSync(dpy, False);
405 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
406 }
408 void
409 togglefloat() {
410 if (!sel)
411 return;
412 sel->isfloat = !sel->isfloat;
413 arrange();
414 }
416 void
417 togglemode() {
418 /* only toggle between tile and max - float is just available through togglefloat */
419 arrange = (arrange == dotile) ? domax : dotile;
420 if(sel)
421 arrange();
422 else
423 drawstatus();
424 }
426 void
427 toggleview() {
428 seltag = !seltag;
429 arrange();
430 }
432 void
433 zoom() {
434 unsigned int n;
435 Client *c;
437 if(!sel)
438 return;
439 if(sel->isfloat) {
440 togglemax(sel);
441 return;
442 }
443 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
444 n++;
446 if((c = sel) == nexttiled(clients))
447 if(!(c = nexttiled(c->next)))
448 return;
449 detach(c);
450 if(clients)
451 clients->prev = c;
452 c->next = clients;
453 clients = c;
454 focus(c);
455 arrange();
456 }
473 /* from util.c */
476 void *
477 emallocz(unsigned int size) {
478 void *res = calloc(1, size);
480 if(!res)
481 eprint("fatal: could not malloc() %u bytes\n", size);
482 return res;
483 }
485 void
486 eprint(const char *errstr, ...) {
487 va_list ap;
489 va_start(ap, errstr);
490 vfprintf(stderr, errstr, ap);
491 va_end(ap);
492 exit(EXIT_FAILURE);
493 }
495 void
496 spawn(const char* cmd) {
497 static char *shell = NULL;
499 if(!cmd)
500 return;
501 if(!(shell = getenv("SHELL")))
502 shell = "/bin/sh";
503 /* The double-fork construct avoids zombie processes and keeps the code
504 * clean from stupid signal handlers. */
505 if(fork() == 0) {
506 if(fork() == 0) {
507 if(dpy)
508 close(ConnectionNumber(dpy));
509 setsid();
510 execl(shell, shell, "-c", cmd, (char *)NULL);
511 fprintf(stderr, "aewl: execl '%s -c %s'", shell, cmd);
512 perror(" failed");
513 }
514 exit(0);
515 }
516 wait(0);
517 }
531 /* from tag.c */
533 /* static */
535 Client *
536 getnext(Client *c) {
537 while(c && !isvisible(c)) {
538 c = c->next;
539 }
540 return c;
541 }
543 void
544 settag(Client *c, Client *trans) {
545 unsigned int i;
546 XClassHint ch = { 0 };
548 if(trans) {
549 c->tag = trans->tag;
550 return;
551 }
552 c->tag = seltag; /* default */
553 XGetClassHint(dpy, c->win, &ch);
554 len = sizeof rule / sizeof rule[0];
555 for(i = 0; i < len; i++) {
556 if((rule[i].title && strstr(c->name, rule[i].title))
557 || (ch.res_class && rule[i].class && strstr(ch.res_class, rule[i].class))
558 || (ch.res_name && rule[i].instance && strstr(ch.res_name, rule[i].instance))) {
559 c->isfloat = rule[i].isfloat;
560 if (rule[i].tag < 0) {
561 c->tag = seltag;
562 } else if (rule[i].tag) {
563 c->tag = True;
564 } else {
565 c->tag = False;
566 }
567 break;
568 }
569 }
570 if(ch.res_class)
571 XFree(ch.res_class);
572 if(ch.res_name)
573 XFree(ch.res_name);
574 }
576 void
577 toggletag() {
578 if(!sel)
579 return;
580 sel->tag = !sel->tag;
581 toggleview();
582 }
594 /* from event.c */
595 /* static */
599 static void
600 movemouse(Client *c) {
601 int x1, y1, ocx, ocy, di;
602 unsigned int dui;
603 Window dummy;
604 XEvent ev;
606 ocx = c->x;
607 ocy = c->y;
608 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
609 None, cursor[CurMove], CurrentTime) != GrabSuccess)
610 return;
611 c->ismax = False;
612 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
613 for(;;) {
614 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
615 switch (ev.type) {
616 case ButtonRelease:
617 resize(c, True);
618 XUngrabPointer(dpy, CurrentTime);
619 return;
620 case ConfigureRequest:
621 case Expose:
622 case MapRequest:
623 handler[ev.type](&ev);
624 break;
625 case MotionNotify:
626 XSync(dpy, False);
627 c->x = ocx + (ev.xmotion.x - x1);
628 c->y = ocy + (ev.xmotion.y - y1);
629 if(abs(wax + c->x) < SNAP)
630 c->x = wax;
631 else if(abs((wax + waw) - (c->x + c->w + 2 * c->border)) < SNAP)
632 c->x = wax + waw - c->w - 2 * c->border;
633 if(abs(way - c->y) < SNAP)
634 c->y = way;
635 else if(abs((way + wah) - (c->y + c->h + 2 * c->border)) < SNAP)
636 c->y = way + wah - c->h - 2 * c->border;
637 resize(c, False);
638 break;
639 }
640 }
641 }
643 static void
644 resizemouse(Client *c) {
645 int ocx, ocy;
646 int nw, nh;
647 XEvent ev;
649 ocx = c->x;
650 ocy = c->y;
651 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
652 None, cursor[CurResize], CurrentTime) != GrabSuccess)
653 return;
654 c->ismax = False;
655 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
656 for(;;) {
657 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
658 switch(ev.type) {
659 case ButtonRelease:
660 resize(c, True);
661 XUngrabPointer(dpy, CurrentTime);
662 return;
663 case ConfigureRequest:
664 case Expose:
665 case MapRequest:
666 handler[ev.type](&ev);
667 break;
668 case MotionNotify:
669 XSync(dpy, False);
670 nw = ev.xmotion.x - ocx - 2 * c->border + 1;
671 c->w = nw > 0 ? nw : 1;
672 nh = ev.xmotion.y - ocy - 2 * c->border + 1;
673 c->h = nh > 0 ? nh : 1;
674 resize(c, True);
675 break;
676 }
677 }
678 }
680 static void
681 buttonpress(XEvent *e) {
682 Client *c;
683 XButtonPressedEvent *ev = &e->xbutton;
685 if(barwin == ev->window) {
686 return;
687 }
688 if((c = getclient(ev->window))) {
689 focus(c);
690 if(CLEANMASK(ev->state) != MODKEY)
691 return;
692 if(ev->button == Button1 && c->isfloat) {
693 restack();
694 movemouse(c);
695 } else if(ev->button == Button3 && c->isfloat && !c->isfixed) {
696 restack();
697 resizemouse(c);
698 }
699 }
700 }
702 static void
703 configurerequest(XEvent *e) {
704 unsigned long newmask;
705 Client *c;
706 XConfigureRequestEvent *ev = &e->xconfigurerequest;
707 XWindowChanges wc;
709 if((c = getclient(ev->window))) {
710 c->ismax = False;
711 if(ev->value_mask & CWX)
712 c->x = ev->x;
713 if(ev->value_mask & CWY)
714 c->y = ev->y;
715 if(ev->value_mask & CWWidth)
716 c->w = ev->width;
717 if(ev->value_mask & CWHeight)
718 c->h = ev->height;
719 if(ev->value_mask & CWBorderWidth)
720 c->border = ev->border_width;
721 wc.x = c->x;
722 wc.y = c->y;
723 wc.width = c->w;
724 wc.height = c->h;
725 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
726 if(newmask)
727 XConfigureWindow(dpy, c->win, newmask, &wc);
728 else
729 configure(c);
730 XSync(dpy, False);
731 if(c->isfloat) {
732 resize(c, False);
733 if(!isvisible(c))
734 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
735 }
736 else
737 arrange();
738 } else {
739 wc.x = ev->x;
740 wc.y = ev->y;
741 wc.width = ev->width;
742 wc.height = ev->height;
743 wc.border_width = ev->border_width;
744 wc.sibling = ev->above;
745 wc.stack_mode = ev->detail;
746 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
747 XSync(dpy, False);
748 }
749 }
751 static void
752 destroynotify(XEvent *e) {
753 Client *c;
754 XDestroyWindowEvent *ev = &e->xdestroywindow;
756 if((c = getclient(ev->window)))
757 unmanage(c);
758 }
760 static void
761 enternotify(XEvent *e) {
762 Client *c;
763 XCrossingEvent *ev = &e->xcrossing;
765 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
766 return;
767 if((c = getclient(ev->window)) && isvisible(c))
768 focus(c);
769 else if(ev->window == root) {
770 selscreen = True;
771 for(c = stack; c && !isvisible(c); c = c->snext);
772 focus(c);
773 }
774 }
776 static void
777 expose(XEvent *e) {
778 XExposeEvent *ev = &e->xexpose;
780 if(ev->count == 0) {
781 if(barwin == ev->window)
782 drawstatus();
783 }
784 }
786 static void
787 keypress(XEvent *e) {
788 static unsigned int len = sizeof key / sizeof key[0];
789 unsigned int i;
790 KeySym keysym;
791 XKeyEvent *ev = &e->xkey;
793 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
794 for(i = 0; i < len; i++) {
795 if(keysym == key[i].keysym && CLEANMASK(key[i].mod) == CLEANMASK(ev->state)) {
796 if(key[i].func)
797 key[i].func(key[i].cmd);
798 }
799 }
800 }
802 static void
803 leavenotify(XEvent *e) {
804 XCrossingEvent *ev = &e->xcrossing;
806 if((ev->window == root) && !ev->same_screen) {
807 selscreen = False;
808 focus(NULL);
809 }
810 }
812 static void
813 mappingnotify(XEvent *e) {
814 XMappingEvent *ev = &e->xmapping;
816 XRefreshKeyboardMapping(ev);
817 if(ev->request == MappingKeyboard)
818 grabkeys();
819 }
821 static void
822 maprequest(XEvent *e) {
823 static XWindowAttributes wa;
824 XMapRequestEvent *ev = &e->xmaprequest;
826 if(!XGetWindowAttributes(dpy, ev->window, &wa))
827 return;
828 if(wa.override_redirect) {
829 XSelectInput(dpy, ev->window,
830 (StructureNotifyMask | PropertyChangeMask));
831 return;
832 }
833 if(!getclient(ev->window))
834 manage(ev->window, &wa);
835 }
837 static void
838 propertynotify(XEvent *e) {
839 Client *c;
840 Window trans;
841 XPropertyEvent *ev = &e->xproperty;
843 if(ev->state == PropertyDelete)
844 return; /* ignore */
845 if((c = getclient(ev->window))) {
846 switch (ev->atom) {
847 default: break;
848 case XA_WM_TRANSIENT_FOR:
849 XGetTransientForHint(dpy, c->win, &trans);
850 if(!c->isfloat && (c->isfloat = (trans != 0)))
851 arrange();
852 break;
853 case XA_WM_NORMAL_HINTS:
854 updatesizehints(c);
855 break;
856 }
857 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
858 updatetitle(c);
859 if(c == sel)
860 drawstatus();
861 }
862 }
863 }
865 static void
866 unmapnotify(XEvent *e) {
867 Client *c;
868 XUnmapEvent *ev = &e->xunmap;
870 if((c = getclient(ev->window)))
871 unmanage(c);
872 }
876 void (*handler[LASTEvent]) (XEvent *) = {
877 [ButtonPress] = buttonpress,
878 [ConfigureRequest] = configurerequest,
879 [DestroyNotify] = destroynotify,
880 [EnterNotify] = enternotify,
881 [LeaveNotify] = leavenotify,
882 [Expose] = expose,
883 [KeyPress] = keypress,
884 [MappingNotify] = mappingnotify,
885 [MapRequest] = maprequest,
886 [PropertyNotify] = propertynotify,
887 [UnmapNotify] = unmapnotify
888 };
890 void
891 grabkeys(void) {
892 static unsigned int len = sizeof key / sizeof key[0];
893 unsigned int i;
894 KeyCode code;
896 XUngrabKey(dpy, AnyKey, AnyModifier, root);
897 for(i = 0; i < len; i++) {
898 code = XKeysymToKeycode(dpy, key[i].keysym);
899 XGrabKey(dpy, code, key[i].mod, root, True,
900 GrabModeAsync, GrabModeAsync);
901 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
902 GrabModeAsync, GrabModeAsync);
903 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
904 GrabModeAsync, GrabModeAsync);
905 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
906 GrabModeAsync, GrabModeAsync);
907 }
908 }
910 void
911 procevent(void) {
912 XEvent ev;
914 while(XPending(dpy)) {
915 XNextEvent(dpy, &ev);
916 if(handler[ev.type])
917 (handler[ev.type])(&ev); /* call handler */
918 }
919 }
935 /* from draw.c */
936 /* static */
938 static unsigned int
939 textnw(const char *text, unsigned int len) {
940 XRectangle r;
942 if(dc.font.set) {
943 XmbTextExtents(dc.font.set, text, len, NULL, &r);
944 return r.width;
945 }
946 return XTextWidth(dc.font.xfont, text, len);
947 }
949 static void
950 drawtext(const char *text, unsigned long col[ColLast]) {
951 int x, y, w, h;
952 static char buf[256];
953 unsigned int len, olen;
954 XGCValues gcv;
955 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
957 XSetForeground(dpy, dc.gc, col[ColBG]);
958 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
959 if(!text)
960 return;
961 w = 0;
962 olen = len = strlen(text);
963 if(len >= sizeof buf)
964 len = sizeof buf - 1;
965 memcpy(buf, text, len);
966 buf[len] = 0;
967 h = dc.font.ascent + dc.font.descent;
968 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
969 x = dc.x + (h / 2);
970 /* shorten text if necessary */
971 while(len && (w = textnw(buf, len)) > dc.w - h)
972 buf[--len] = 0;
973 if(len < olen) {
974 if(len > 1)
975 buf[len - 1] = '.';
976 if(len > 2)
977 buf[len - 2] = '.';
978 if(len > 3)
979 buf[len - 3] = '.';
980 }
981 if(w > dc.w)
982 return; /* too long */
983 gcv.foreground = col[ColFG];
984 if(dc.font.set) {
985 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
986 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
987 } else {
988 gcv.font = dc.font.xfont->fid;
989 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
990 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
991 }
992 }
996 void
997 drawstatus(void) {
998 int x;
999 unsigned long black[ColLast];
1000 black[ColBG] = getcolor("#000000");
1001 black[ColFG] = getcolor("#ffffff");
1003 /* views */
1004 dc.x = dc.y = 0;
1005 dc.w = textw(NAMETAGGED);
1006 drawtext(NAMETAGGED, ( seltag ? dc.sel : dc.norm ));
1008 dc.x += dc.w;
1009 dc.w = BORDERPX;
1010 drawtext(NULL, black);
1012 dc.x += dc.w;
1013 dc.w = textw(NAMEUNTAGGED);
1014 drawtext(NAMEUNTAGGED, ( seltag ? dc.norm : dc.sel ));
1016 dc.x += dc.w;
1017 dc.w = BORDERPX;
1018 drawtext(NULL, black);
1020 /* status text */
1021 x = dc.x + dc.w;
1022 dc.w = textw(stext);
1023 dc.x = sw - dc.w;
1024 if(dc.x < x) {
1025 dc.x = x;
1026 dc.w = sw - x;
1028 drawtext(stext, dc.norm);
1030 /* client title */
1031 if((dc.w = dc.x - x) > bh) {
1032 dc.x = x;
1033 drawtext(sel ? sel->name : NULL, dc.norm);
1035 dc.x += dc.w;
1036 dc.w = BORDERPX;
1037 drawtext(NULL, black);
1040 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
1041 XSync(dpy, False);
1044 unsigned long
1045 getcolor(const char *colstr) {
1046 Colormap cmap = DefaultColormap(dpy, screen);
1047 XColor color;
1049 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
1050 eprint("error, cannot allocate color '%s'\n", colstr);
1051 return color.pixel;
1054 void
1055 setfont(const char *fontstr) {
1056 char *def, **missing;
1057 int i, n;
1059 missing = NULL;
1060 if(dc.font.set)
1061 XFreeFontSet(dpy, dc.font.set);
1062 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
1063 if(missing) {
1064 while(n--)
1065 fprintf(stderr, "missing fontset: %s\n", missing[n]);
1066 XFreeStringList(missing);
1068 if(dc.font.set) {
1069 XFontSetExtents *font_extents;
1070 XFontStruct **xfonts;
1071 char **font_names;
1072 dc.font.ascent = dc.font.descent = 0;
1073 font_extents = XExtentsOfFontSet(dc.font.set);
1074 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
1075 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
1076 if(dc.font.ascent < (*xfonts)->ascent)
1077 dc.font.ascent = (*xfonts)->ascent;
1078 if(dc.font.descent < (*xfonts)->descent)
1079 dc.font.descent = (*xfonts)->descent;
1080 xfonts++;
1082 } else {
1083 if(dc.font.xfont)
1084 XFreeFont(dpy, dc.font.xfont);
1085 dc.font.xfont = NULL;
1086 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
1087 eprint("error, cannot load font: '%s'\n", fontstr);
1088 dc.font.ascent = dc.font.xfont->ascent;
1089 dc.font.descent = dc.font.xfont->descent;
1091 dc.font.height = dc.font.ascent + dc.font.descent;
1094 unsigned int
1095 textw(const char *text) {
1096 return textnw(text, strlen(text)) + dc.font.height;
1109 /* from client.c */
1110 /* static */
1112 static void
1113 detachstack(Client *c) {
1114 Client **tc;
1115 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
1116 *tc = c->snext;
1119 static void
1120 grabbuttons(Client *c, Bool focused) {
1121 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1123 if(focused) {
1124 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
1125 GrabModeAsync, GrabModeSync, None, None);
1126 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
1127 GrabModeAsync, GrabModeSync, None, None);
1128 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
1129 GrabModeAsync, GrabModeSync, None, None);
1130 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
1131 GrabModeAsync, GrabModeSync, None, None);
1133 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
1134 GrabModeAsync, GrabModeSync, None, None);
1135 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
1136 GrabModeAsync, GrabModeSync, None, None);
1137 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
1138 GrabModeAsync, GrabModeSync, None, None);
1139 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
1140 GrabModeAsync, GrabModeSync, None, None);
1142 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
1143 GrabModeAsync, GrabModeSync, None, None);
1144 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
1145 GrabModeAsync, GrabModeSync, None, None);
1146 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
1147 GrabModeAsync, GrabModeSync, None, None);
1148 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
1149 GrabModeAsync, GrabModeSync, None, None);
1150 } else {
1151 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
1152 GrabModeAsync, GrabModeSync, None, None);
1156 static void
1157 setclientstate(Client *c, long state) {
1158 long data[] = {state, None};
1159 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1160 PropModeReplace, (unsigned char *)data, 2);
1163 static int
1164 xerrordummy(Display *dsply, XErrorEvent *ee) {
1165 return 0;
1170 void
1171 configure(Client *c) {
1172 XEvent synev;
1174 synev.type = ConfigureNotify;
1175 synev.xconfigure.display = dpy;
1176 synev.xconfigure.event = c->win;
1177 synev.xconfigure.window = c->win;
1178 synev.xconfigure.x = c->x;
1179 synev.xconfigure.y = c->y;
1180 synev.xconfigure.width = c->w;
1181 synev.xconfigure.height = c->h;
1182 synev.xconfigure.border_width = c->border;
1183 synev.xconfigure.above = None;
1184 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
1187 void
1188 focus(Client *c) {
1189 if(c && !isvisible(c))
1190 return;
1191 if(sel && sel != c) {
1192 grabbuttons(sel, False);
1193 XSetWindowBorder(dpy, sel->win, dc.norm[ColBG]);
1195 if(c) {
1196 detachstack(c);
1197 c->snext = stack;
1198 stack = c;
1199 grabbuttons(c, True);
1201 sel = c;
1202 drawstatus();
1203 if(!selscreen)
1204 return;
1205 if(c) {
1206 XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
1207 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1208 } else {
1209 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1213 Client *
1214 getclient(Window w) {
1215 Client *c;
1217 for(c = clients; c; c = c->next) {
1218 if(c->win == w) {
1219 return c;
1222 return NULL;
1225 Bool
1226 isprotodel(Client *c) {
1227 int i, n;
1228 Atom *protocols;
1229 Bool ret = False;
1231 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1232 for(i = 0; !ret && i < n; i++)
1233 if(protocols[i] == wmatom[WMDelete])
1234 ret = True;
1235 XFree(protocols);
1237 return ret;
1240 void
1241 killclient() {
1242 if(!sel)
1243 return;
1244 if(isprotodel(sel))
1245 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
1246 else
1247 XKillClient(dpy, sel->win);
1250 void
1251 manage(Window w, XWindowAttributes *wa) {
1252 Client *c;
1253 Window trans;
1255 c = emallocz(sizeof(Client));
1256 c->tag = True;
1257 c->win = w;
1258 c->x = wa->x;
1259 c->y = wa->y;
1260 c->w = wa->width;
1261 c->h = wa->height;
1262 if(c->w == sw && c->h == sh) {
1263 c->border = 0;
1264 c->x = sx;
1265 c->y = sy;
1266 } else {
1267 c->border = BORDERPX;
1268 if(c->x + c->w + 2 * c->border > wax + waw)
1269 c->x = wax + waw - c->w - 2 * c->border;
1270 if(c->y + c->h + 2 * c->border > way + wah)
1271 c->y = way + wah - c->h - 2 * c->border;
1272 if(c->x < wax)
1273 c->x = wax;
1274 if(c->y < way)
1275 c->y = way;
1277 updatesizehints(c);
1278 XSelectInput(dpy, c->win,
1279 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
1280 XGetTransientForHint(dpy, c->win, &trans);
1281 grabbuttons(c, False);
1282 XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
1283 updatetitle(c);
1284 settag(c, getclient(trans));
1285 if(!c->isfloat)
1286 c->isfloat = trans || c->isfixed;
1287 if(clients)
1288 clients->prev = c;
1289 c->next = clients;
1290 c->snext = stack;
1291 stack = clients = c;
1292 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
1293 XMapWindow(dpy, c->win);
1294 setclientstate(c, NormalState);
1295 if(isvisible(c))
1296 focus(c);
1297 arrange();
1300 void
1301 resize(Client *c, Bool sizehints) {
1302 float actual, dx, dy, max, min;
1303 XWindowChanges wc;
1305 if(c->w <= 0 || c->h <= 0)
1306 return;
1307 if(sizehints) {
1308 if(c->minw && c->w < c->minw)
1309 c->w = c->minw;
1310 if(c->minh && c->h < c->minh)
1311 c->h = c->minh;
1312 if(c->maxw && c->w > c->maxw)
1313 c->w = c->maxw;
1314 if(c->maxh && c->h > c->maxh)
1315 c->h = c->maxh;
1316 /* inspired by algorithm from fluxbox */
1317 if(c->minay > 0 && c->maxay && (c->h - c->baseh) > 0) {
1318 dx = (float)(c->w - c->basew);
1319 dy = (float)(c->h - c->baseh);
1320 min = (float)(c->minax) / (float)(c->minay);
1321 max = (float)(c->maxax) / (float)(c->maxay);
1322 actual = dx / dy;
1323 if(max > 0 && min > 0 && actual > 0) {
1324 if(actual < min) {
1325 dy = (dx * min + dy) / (min * min + 1);
1326 dx = dy * min;
1327 c->w = (int)dx + c->basew;
1328 c->h = (int)dy + c->baseh;
1330 else if(actual > max) {
1331 dy = (dx * min + dy) / (max * max + 1);
1332 dx = dy * min;
1333 c->w = (int)dx + c->basew;
1334 c->h = (int)dy + c->baseh;
1338 if(c->incw)
1339 c->w -= (c->w - c->basew) % c->incw;
1340 if(c->inch)
1341 c->h -= (c->h - c->baseh) % c->inch;
1343 if(c->w == sw && c->h == sh)
1344 c->border = 0;
1345 else
1346 c->border = BORDERPX;
1347 /* offscreen appearance fixes */
1348 if(c->x > sw)
1349 c->x = sw - c->w - 2 * c->border;
1350 if(c->y > sh)
1351 c->y = sh - c->h - 2 * c->border;
1352 if(c->x + c->w + 2 * c->border < sx)
1353 c->x = sx;
1354 if(c->y + c->h + 2 * c->border < sy)
1355 c->y = sy;
1356 wc.x = c->x;
1357 wc.y = c->y;
1358 wc.width = c->w;
1359 wc.height = c->h;
1360 wc.border_width = c->border;
1361 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
1362 configure(c);
1363 XSync(dpy, False);
1366 void
1367 updatesizehints(Client *c) {
1368 long msize;
1369 XSizeHints size;
1371 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
1372 size.flags = PSize;
1373 c->flags = size.flags;
1374 if(c->flags & PBaseSize) {
1375 c->basew = size.base_width;
1376 c->baseh = size.base_height;
1377 } else {
1378 c->basew = c->baseh = 0;
1380 if(c->flags & PResizeInc) {
1381 c->incw = size.width_inc;
1382 c->inch = size.height_inc;
1383 } else {
1384 c->incw = c->inch = 0;
1386 if(c->flags & PMaxSize) {
1387 c->maxw = size.max_width;
1388 c->maxh = size.max_height;
1389 } else {
1390 c->maxw = c->maxh = 0;
1392 if(c->flags & PMinSize) {
1393 c->minw = size.min_width;
1394 c->minh = size.min_height;
1395 } else {
1396 c->minw = c->minh = 0;
1398 if(c->flags & PAspect) {
1399 c->minax = size.min_aspect.x;
1400 c->minay = size.min_aspect.y;
1401 c->maxax = size.max_aspect.x;
1402 c->maxay = size.max_aspect.y;
1403 } else {
1404 c->minax = c->minay = c->maxax = c->maxay = 0;
1406 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
1407 c->maxw == c->minw && c->maxh == c->minh);
1410 void
1411 updatetitle(Client *c) {
1412 char **list = NULL;
1413 int n;
1414 XTextProperty name;
1416 name.nitems = 0;
1417 c->name[0] = 0;
1418 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
1419 if(!name.nitems)
1420 XGetWMName(dpy, c->win, &name);
1421 if(!name.nitems)
1422 return;
1423 if(name.encoding == XA_STRING)
1424 strncpy(c->name, (char *)name.value, sizeof c->name);
1425 else {
1426 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
1427 strncpy(c->name, *list, sizeof c->name);
1428 XFreeStringList(list);
1431 XFree(name.value);
1434 void
1435 unmanage(Client *c) {
1436 Client *nc;
1438 /* The server grab construct avoids race conditions. */
1439 XGrabServer(dpy);
1440 XSetErrorHandler(xerrordummy);
1441 detach(c);
1442 detachstack(c);
1443 if(sel == c) {
1444 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
1445 focus(nc);
1447 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1448 setclientstate(c, WithdrawnState);
1449 free(c);
1450 XSync(dpy, False);
1451 XSetErrorHandler(xerror);
1452 XUngrabServer(dpy);
1453 arrange();
1474 /* static */
1477 static void
1478 cleanup(void) {
1479 close(STDIN_FILENO);
1480 while(stack) {
1481 resize(stack, True);
1482 unmanage(stack);
1484 if(dc.font.set)
1485 XFreeFontSet(dpy, dc.font.set);
1486 else
1487 XFreeFont(dpy, dc.font.xfont);
1488 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1489 XFreePixmap(dpy, dc.drawable);
1490 XFreeGC(dpy, dc.gc);
1491 XDestroyWindow(dpy, barwin);
1492 XFreeCursor(dpy, cursor[CurNormal]);
1493 XFreeCursor(dpy, cursor[CurResize]);
1494 XFreeCursor(dpy, cursor[CurMove]);
1495 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
1496 XSync(dpy, False);
1499 static void
1500 scan(void) {
1501 unsigned int i, num;
1502 Window *wins, d1, d2;
1503 XWindowAttributes wa;
1505 wins = NULL;
1506 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1507 for(i = 0; i < num; i++) {
1508 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1509 continue;
1510 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1511 continue;
1512 if(wa.map_state == IsViewable)
1513 manage(wins[i], &wa);
1516 if(wins)
1517 XFree(wins);
1520 static void
1521 setup(void) {
1522 int i, j;
1523 unsigned int mask;
1524 Window w;
1525 XModifierKeymap *modmap;
1526 XSetWindowAttributes wa;
1528 /* init atoms */
1529 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1530 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1531 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1532 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1533 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1534 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1535 PropModeReplace, (unsigned char *) netatom, NetLast);
1536 /* init cursors */
1537 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1538 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1539 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1540 /* init modifier map */
1541 numlockmask = 0;
1542 modmap = XGetModifierMapping(dpy);
1543 for (i = 0; i < 8; i++) {
1544 for (j = 0; j < modmap->max_keypermod; j++) {
1545 if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
1546 numlockmask = (1 << i);
1549 XFreeModifiermap(modmap);
1550 /* select for events */
1551 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
1552 | EnterWindowMask | LeaveWindowMask;
1553 wa.cursor = cursor[CurNormal];
1554 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
1555 grabkeys();
1556 seltag = True;
1557 /* style */
1558 dc.norm[ColBG] = getcolor(NORMBGCOLOR);
1559 dc.norm[ColFG] = getcolor(NORMFGCOLOR);
1560 dc.sel[ColBG] = getcolor(SELBGCOLOR);
1561 dc.sel[ColFG] = getcolor(SELFGCOLOR);
1562 setfont(FONT);
1563 /* geometry */
1564 sx = sy = 0;
1565 sw = DisplayWidth(dpy, screen);
1566 sh = DisplayHeight(dpy, screen);
1567 nmaster = NMASTER;
1568 /* bar */
1569 dc.h = bh = dc.font.height + 2;
1570 wa.override_redirect = 1;
1571 wa.background_pixmap = ParentRelative;
1572 wa.event_mask = ButtonPressMask | ExposureMask;
1573 barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0,
1574 DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
1575 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
1576 XDefineCursor(dpy, barwin, cursor[CurNormal]);
1577 XMapRaised(dpy, barwin);
1578 strcpy(stext, "aewl-"VERSION);
1579 /* windowarea */
1580 wax = sx;
1581 way = sy + bh;
1582 wah = sh - bh;
1583 waw = sw;
1584 /* pixmap for everything */
1585 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
1586 dc.gc = XCreateGC(dpy, root, 0, 0);
1587 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1588 /* multihead support */
1589 selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
1592 /*
1593 * Startup Error handler to check if another window manager
1594 * is already running.
1595 */
1596 static int
1597 xerrorstart(Display *dsply, XErrorEvent *ee) {
1598 otherwm = True;
1599 return -1;
1604 void
1605 sendevent(Window w, Atom a, long value) {
1606 XEvent e;
1608 e.type = ClientMessage;
1609 e.xclient.window = w;
1610 e.xclient.message_type = a;
1611 e.xclient.format = 32;
1612 e.xclient.data.l[0] = value;
1613 e.xclient.data.l[1] = CurrentTime;
1614 XSendEvent(dpy, w, False, NoEventMask, &e);
1615 XSync(dpy, False);
1618 void
1619 quit() {
1620 readin = running = False;
1623 /* There's no way to check accesses to destroyed windows, thus those cases are
1624 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1625 * default error handler, which may call exit.
1626 */
1627 int
1628 xerror(Display *dpy, XErrorEvent *ee) {
1629 if(ee->error_code == BadWindow
1630 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
1631 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
1632 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
1633 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
1634 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
1635 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
1636 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
1637 return 0;
1638 fprintf(stderr, "aewl: fatal error: request code=%d, error code=%d\n",
1639 ee->request_code, ee->error_code);
1640 return xerrorxlib(dpy, ee); /* may call exit */
1643 int
1644 main(int argc, char *argv[]) {
1645 char *p;
1646 int r, xfd;
1647 fd_set rd;
1649 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
1650 fputs("aewl-"VERSION", Copyright 2008 markus schnalke <meillo@marmaro.de>\n", stdout);
1651 fputs("forked off dwm-3.4, (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout);
1652 exit(EXIT_SUCCESS);
1653 } else if(argc != 1) {
1654 eprint("usage: aewl [-v]\n");
1656 setlocale(LC_CTYPE, "");
1657 dpy = XOpenDisplay(0);
1658 if(!dpy) {
1659 eprint("aewl: cannot open display\n");
1661 xfd = ConnectionNumber(dpy);
1662 screen = DefaultScreen(dpy);
1663 root = RootWindow(dpy, screen);
1664 otherwm = False;
1665 XSetErrorHandler(xerrorstart);
1666 /* this causes an error if some other window manager is running */
1667 XSelectInput(dpy, root, SubstructureRedirectMask);
1668 XSync(dpy, False);
1669 if(otherwm) {
1670 eprint("aewl: another window manager is already running\n");
1673 XSync(dpy, False);
1674 XSetErrorHandler(NULL);
1675 xerrorxlib = XSetErrorHandler(xerror);
1676 XSync(dpy, False);
1677 setup();
1678 drawstatus();
1679 scan();
1681 /* main event loop, also reads status text from stdin */
1682 XSync(dpy, False);
1683 procevent();
1684 readin = True;
1685 while(running) {
1686 FD_ZERO(&rd);
1687 if(readin)
1688 FD_SET(STDIN_FILENO, &rd);
1689 FD_SET(xfd, &rd);
1690 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
1691 if(errno == EINTR)
1692 continue;
1693 eprint("select failed\n");
1695 if(FD_ISSET(STDIN_FILENO, &rd)) {
1696 switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
1697 case -1:
1698 strncpy(stext, strerror(errno), sizeof stext - 1);
1699 stext[sizeof stext - 1] = '\0';
1700 readin = False;
1701 break;
1702 case 0:
1703 strncpy(stext, "EOF", 4);
1704 readin = False;
1705 break;
1706 default:
1707 for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
1708 for(; p >= stext && *p != '\n'; --p);
1709 if(p > stext)
1710 strncpy(stext, p + 1, sizeof stext);
1712 drawstatus();
1714 if(FD_ISSET(xfd, &rd))
1715 procevent();
1717 cleanup();
1718 XCloseDisplay(dpy);
1719 return 0;