aewl

view aewl.c @ 784:f259785bac44

Start up with the not-sel view.
author markus schnalke <meillo@marmaro.de>
date Sat, 17 Mar 2012 19:07:44 +0100
parents f1e806b34697
children e65be4ffdbdc
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 * The event handlers of dwm are organized in an array which is accessed
12 * whenever a new event has been fetched. This allows event dispatching in O(1)
13 * time.
14 *
15 * Each child of the root window is called a client, except windows which have
16 * set the override_redirect flag. Clients are organized in a global
17 * doubly-linked client list, the focus history is remembered through a global
18 * stack list. [...]
19 *
20 * Keys and tagging rules are organized as arrays and defined in the config.h
21 * file. [...] The current mode is represented by the arrange() function
22 * pointer, which wether points to [domax()] or dotile().
23 *
24 * To understand everything else, start reading main.c:main().
25 *
26 * -- and now about aewl --
27 *
28 * aewl is a stripped down dwm. It stated as a patchset, but finally forked off
29 * completely. The reason for this was the increasing gap between my wish to
30 * stay where dwm was, and dwm direction to go further. Further more did I
31 * always use only a small subset of dwm's features, so condencing dwm had been
32 * my wish for a long time.
33 *
34 * In aewl clients are either tagged or not (only one tag). Visible are either
35 * all tagged clients or all without the tag.
36 */
39 #include <errno.h>
40 #include <locale.h>
41 #include <stdio.h>
42 #include <stdarg.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <sys/signal.h>
47 #include <sys/types.h>
48 #include <sys/wait.h>
49 #include <X11/cursorfont.h>
50 #include <X11/keysym.h>
51 #include <X11/Xatom.h>
52 #include <X11/Xlib.h>
53 #include <X11/Xproto.h>
54 #include <X11/Xutil.h>
56 #include "config.h"
59 /* mask shorthands, used in event.c and client.c */
60 #define BUTTONMASK (ButtonPressMask | ButtonReleaseMask)
62 enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
63 enum { WMProtocols, WMDelete, WMState, WMLast }; /* default atoms */
64 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
65 enum { ColFG, ColBG, ColLast }; /* color */
67 typedef struct {
68 int ascent;
69 int descent;
70 int height;
71 XFontSet set;
72 XFontStruct *xfont;
73 } Fnt;
75 typedef struct {
76 int x, y, w, h;
77 unsigned long norm[ColLast];
78 unsigned long sel[ColLast];
79 Drawable drawable;
80 Fnt font;
81 GC gc;
82 } DC; /* draw context */
84 typedef struct Client Client;
85 struct Client {
86 char name[256];
87 int x, y, w, h;
88 int rx, ry, rw, rh; /* revert geometry */
89 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
90 int minax, minay, maxax, maxay;
91 long flags;
92 unsigned int border;
93 Bool isfixed, isfloat, ismax;
94 Bool tag;
95 Client *next;
96 Client *prev;
97 Client *snext;
98 Window win;
99 };
101 typedef struct {
102 const char* class;
103 const char* instance;
104 const char* title;
105 int tag;
106 Bool isfloat;
107 } Rule;
110 typedef struct {
111 unsigned long mod;
112 KeySym keysym;
113 void (*func)(const char* cmd);
114 const char* cmd;
115 } Key;
118 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
119 #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
123 char stext[256]; /* status text */
124 int bh; /* bar height */
125 int screen, sx, sy, sw, sh; /* screen geometry */
126 int wax, way, wah, waw; /* windowarea geometry */
127 unsigned int nmaster; /* number of master clients */
128 unsigned int numlockmask; /* dynamic lock mask */
129 void (*handler[LASTEvent])(XEvent *); /* event handler */
130 void arrange(void); /* arrange */
131 void (*arrange1)(void); /* arrange function, indicates mode */
132 void (*arrange2)(void); /* arrange function, indicates mode */
133 Atom wmatom[WMLast], netatom[NetLast];
134 Bool running = True;
135 Bool selscreen = True;
136 Bool seltag;
137 Bool viewfloats;
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;
148 static unsigned int len = 0;
152 void configure(Client *c); /* send synthetic configure event */
153 void focus(Client *c); /* focus c, c may be NULL */
154 Client *getclient(Window w); /* return client of w */
155 Bool isprotodel(Client *c); /* returns True if c->win supports wmatom[WMDelete] */
156 void manage(Window w, XWindowAttributes *wa); /* manage new client */
157 void resize(Client *c, Bool sizehints); /* resize c*/
158 void updatesizehints(Client *c); /* update the size hint variables of c */
159 void updatetitle(Client *c); /* update the name of c */
160 void unmanage(Client *c); /* destroy c */
162 void drawbar(void); /* draw the bar */
163 unsigned long getcolor(const char *colstr); /* return color of colstr */
164 void setfont(const char *fontstr); /* set the font for DC */
165 unsigned int textw(const char *text); /* return the width of text in px*/
167 void grabkeys(void); /* grab all keys defined in config.h */
169 void sendevent(Window w, Atom a, long value); /* send synthetic event to w */
170 int xerror(Display *dsply, XErrorEvent *ee); /* X error handler */
172 Client *getnext(Client *c); /* returns next visible client */
173 void settag(Client *c, Client *trans); /* sets tag of c */
175 void *emallocz(unsigned int size); /* allocates zero-initialized memory, exits on error */
176 void die(const char *errstr, ...); /* prints errstr and exits with 1 */
178 void detach(Client *c); /* detaches c from global client list */
179 void dotile(void); /* arranges all windows tiled */
180 void domax(void); /* arranges all windows fullscreen */
181 Bool isvisible(Client *c); /* returns True if client is visible */
182 void restack(void); /* restores z layers of all clients */
184 void toggleview(void); /* toggle the view */
185 void floattoggle(); /* toggle floatsview */
186 void focusnext(void); /* focuses next visible client */
187 void zoom(void); /* zooms the focused client to master area */
188 void killclient(void); /* kill c nicely */
189 void quit(void); /* quit nicely */
190 void togglemode(void); /* toggles arrange function (dotile/domax) */
191 void togglefloat(void); /* toggles focusesd client between floating/non-floating state */
192 void incnmaster(void); /* increments nmaster */
193 void decnmaster(void); /* decrements nmaster */
194 void toggletag(void); /* toggles tag of c */
195 void spawn(const char* cmd); /* forks a new subprocess with cmd */
197 void updatestatus(void); /* update the status text */
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 if (c->isfloat) {
370 return viewfloats;
371 }
372 return (!viewfloats && c->tag == seltag);
373 }
375 void
376 restack(void) {
377 Client *c;
378 XEvent ev;
380 drawbar();
381 if(!sel)
382 return;
383 /*if(sel->isfloat)*/
384 XRaiseWindow(dpy, sel->win);
386 /*
387 if(!sel->isfloat)
388 XLowerWindow(dpy, sel->win);
389 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
390 if(c == sel)
391 continue;
392 XLowerWindow(dpy, c->win);
393 }
394 */
396 XSync(dpy, False);
397 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
398 }
400 void
401 togglefloat() {
402 if (!sel)
403 return;
404 sel->isfloat = !sel->isfloat;
405 floattoggle();
406 }
408 void
409 togglemode() {
410 /* toggle between tile and max */
411 if (viewfloats) {
412 return;
413 }
414 if (seltag) {
415 arrange1 = (arrange1 == dotile) ? domax : dotile;
416 } else {
417 arrange2 = (arrange2 == dotile) ? domax : dotile;
418 }
419 if(sel)
420 arrange();
421 zoom();
422 }
424 void
425 arrange() {
426 if (seltag) {
427 arrange1();
428 } else {
429 arrange2();
430 }
431 }
433 void
434 toggleview() {
435 if (viewfloats) {
436 return;
437 }
438 seltag = !seltag;
439 arrange();
440 }
442 void
443 floattoggle() {
444 viewfloats = !viewfloats;
445 arrange();
446 }
448 void
449 zoom() {
450 unsigned int n;
451 Client *c;
453 if(!sel)
454 return;
455 if(sel->isfloat) {
456 togglemax(sel);
457 return;
458 }
459 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
460 n++;
462 if((c = sel) == nexttiled(clients))
463 if(!(c = nexttiled(c->next)))
464 return;
465 detach(c);
466 if(clients)
467 clients->prev = c;
468 c->next = clients;
469 clients = c;
470 focus(c);
471 arrange();
472 }
474 /* from util.c */
476 void *
477 emallocz(unsigned int size) {
478 void *res = calloc(1, size);
480 if(!res)
481 die("fatal: could not malloc() %u bytes\n", size);
482 return res;
483 }
485 void
486 die(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 }
519 /* from tag.c */
521 Client *
522 getnext(Client *c) {
523 while(c && !isvisible(c)) {
524 c = c->next;
525 }
526 return c;
527 }
529 void
530 settag(Client *c, Client *trans) {
531 unsigned int i;
532 XClassHint ch = { 0 };
534 if(trans) {
535 c->tag = trans->tag;
536 return;
537 }
538 c->tag = seltag; /* default */
539 XGetClassHint(dpy, c->win, &ch);
540 len = sizeof rule / sizeof rule[0];
541 for(i = 0; i < len; i++) {
542 if((rule[i].title && strstr(c->name, rule[i].title))
543 || (ch.res_class && rule[i].class && strstr(ch.res_class, rule[i].class))
544 || (ch.res_name && rule[i].instance && strstr(ch.res_name, rule[i].instance))) {
545 c->isfloat = rule[i].isfloat;
546 if (rule[i].tag < 0) {
547 c->tag = seltag;
548 } else if (rule[i].tag) {
549 c->tag = True;
550 } else {
551 c->tag = False;
552 }
553 break;
554 }
555 }
556 if(ch.res_class)
557 XFree(ch.res_class);
558 if(ch.res_name)
559 XFree(ch.res_name);
560 }
562 void
563 toggletag() {
564 if(!sel)
565 return;
566 sel->tag = !sel->tag;
567 toggleview();
568 }
570 /* from event.c */
572 void
573 movemouse(Client *c) {
574 int x1, y1, ocx, ocy, di;
575 unsigned int dui;
576 Window dummy;
577 XEvent ev;
579 ocx = c->x;
580 ocy = c->y;
581 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
582 None, cursor[CurMove], CurrentTime) != GrabSuccess)
583 return;
584 c->ismax = False;
585 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
586 for(;;) {
587 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
588 switch (ev.type) {
589 case ButtonRelease:
590 resize(c, True);
591 XUngrabPointer(dpy, CurrentTime);
592 return;
593 case ConfigureRequest:
594 case Expose:
595 case MapRequest:
596 handler[ev.type](&ev);
597 break;
598 case MotionNotify:
599 XSync(dpy, False);
600 c->x = ocx + (ev.xmotion.x - x1);
601 c->y = ocy + (ev.xmotion.y - y1);
602 if(abs(wax + c->x) < SNAP)
603 c->x = wax;
604 else if(abs((wax + waw) - (c->x + c->w + 2 * c->border)) < SNAP)
605 c->x = wax + waw - c->w - 2 * c->border;
606 if(abs(way - c->y) < SNAP)
607 c->y = way;
608 else if(abs((way + wah) - (c->y + c->h + 2 * c->border)) < SNAP)
609 c->y = way + wah - c->h - 2 * c->border;
610 resize(c, False);
611 break;
612 }
613 }
614 }
616 void
617 resizemouse(Client *c) {
618 int ocx, ocy;
619 int nw, nh;
620 XEvent ev;
622 ocx = c->x;
623 ocy = c->y;
624 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
625 None, cursor[CurResize], CurrentTime) != GrabSuccess)
626 return;
627 c->ismax = False;
628 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
629 for(;;) {
630 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
631 switch(ev.type) {
632 case ButtonRelease:
633 resize(c, True);
634 XUngrabPointer(dpy, CurrentTime);
635 return;
636 case ConfigureRequest:
637 case Expose:
638 case MapRequest:
639 handler[ev.type](&ev);
640 break;
641 case MotionNotify:
642 XSync(dpy, False);
643 nw = ev.xmotion.x - ocx - 2 * c->border + 1;
644 c->w = nw > 0 ? nw : 1;
645 nh = ev.xmotion.y - ocy - 2 * c->border + 1;
646 c->h = nh > 0 ? nh : 1;
647 resize(c, True);
648 break;
649 }
650 }
651 }
653 void
654 buttonpress(XEvent *e) {
655 Client *c;
656 XButtonPressedEvent *ev = &e->xbutton;
658 if(barwin == ev->window) {
659 return;
660 }
661 if((c = getclient(ev->window))) {
662 focus(c);
663 if(CLEANMASK(ev->state) != MODKEY)
664 return;
665 if(ev->button == Button1 && c->isfloat) {
666 restack();
667 movemouse(c);
668 } else if(ev->button == Button3 && c->isfloat && !c->isfixed) {
669 restack();
670 resizemouse(c);
671 }
672 }
673 }
675 void
676 configurerequest(XEvent *e) {
677 unsigned long newmask;
678 Client *c;
679 XConfigureRequestEvent *ev = &e->xconfigurerequest;
680 XWindowChanges wc;
682 if((c = getclient(ev->window))) {
683 c->ismax = False;
684 if(ev->value_mask & CWX)
685 c->x = ev->x;
686 if(ev->value_mask & CWY)
687 c->y = ev->y;
688 if(ev->value_mask & CWWidth)
689 c->w = ev->width;
690 if(ev->value_mask & CWHeight)
691 c->h = ev->height;
692 if(ev->value_mask & CWBorderWidth)
693 c->border = ev->border_width;
694 wc.x = c->x;
695 wc.y = c->y;
696 wc.width = c->w;
697 wc.height = c->h;
698 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
699 if(newmask)
700 XConfigureWindow(dpy, c->win, newmask, &wc);
701 else
702 configure(c);
703 XSync(dpy, False);
704 if(c->isfloat) {
705 resize(c, False);
706 if(!isvisible(c))
707 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
708 }
709 else
710 arrange();
711 } else {
712 wc.x = ev->x;
713 wc.y = ev->y;
714 wc.width = ev->width;
715 wc.height = ev->height;
716 wc.border_width = ev->border_width;
717 wc.sibling = ev->above;
718 wc.stack_mode = ev->detail;
719 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
720 XSync(dpy, False);
721 }
722 }
724 void
725 destroynotify(XEvent *e) {
726 Client *c;
727 XDestroyWindowEvent *ev = &e->xdestroywindow;
729 if((c = getclient(ev->window)))
730 unmanage(c);
731 }
733 void
734 enternotify(XEvent *e) {
735 Client *c;
736 XCrossingEvent *ev = &e->xcrossing;
738 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
739 return;
740 if((c = getclient(ev->window)) && isvisible(c))
741 focus(c);
742 else if(ev->window == root) {
743 selscreen = True;
744 for(c = stack; c && !isvisible(c); c = c->snext);
745 focus(c);
746 }
747 }
749 void
750 expose(XEvent *e) {
751 XExposeEvent *ev = &e->xexpose;
753 if(ev->count == 0) {
754 if(barwin == ev->window)
755 drawbar();
756 }
757 }
759 void
760 keypress(XEvent *e) {
761 static unsigned int len = sizeof key / sizeof key[0];
762 unsigned int i;
763 KeySym keysym;
764 XKeyEvent *ev = &e->xkey;
766 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
767 for(i = 0; i < len; i++) {
768 if(keysym == key[i].keysym && CLEANMASK(key[i].mod) == CLEANMASK(ev->state)) {
769 if(key[i].func)
770 key[i].func(key[i].cmd);
771 }
772 }
773 }
775 void
776 leavenotify(XEvent *e) {
777 XCrossingEvent *ev = &e->xcrossing;
779 if((ev->window == root) && !ev->same_screen) {
780 selscreen = False;
781 focus(NULL);
782 }
783 }
785 void
786 mappingnotify(XEvent *e) {
787 XMappingEvent *ev = &e->xmapping;
789 XRefreshKeyboardMapping(ev);
790 if(ev->request == MappingKeyboard)
791 grabkeys();
792 }
794 void
795 maprequest(XEvent *e) {
796 static XWindowAttributes wa;
797 XMapRequestEvent *ev = &e->xmaprequest;
799 if(!XGetWindowAttributes(dpy, ev->window, &wa))
800 return;
801 if(wa.override_redirect) {
802 XSelectInput(dpy, ev->window,
803 (StructureNotifyMask | PropertyChangeMask));
804 return;
805 }
806 if(!getclient(ev->window))
807 manage(ev->window, &wa);
808 }
810 void
811 propertynotify(XEvent *e) {
812 Client *c;
813 Window trans;
814 XPropertyEvent *ev = &e->xproperty;
816 if((ev->window == root) && (ev->atom = XA_WM_NAME))
817 updatestatus();
818 else if(ev->state == PropertyDelete)
819 return; /* ignore */
820 else if((c = getclient(ev->window))) {
821 switch (ev->atom) {
822 default: break;
823 case XA_WM_TRANSIENT_FOR:
824 XGetTransientForHint(dpy, c->win, &trans);
825 if(!c->isfloat && (c->isfloat = (trans != 0)))
826 arrange();
827 break;
828 case XA_WM_NORMAL_HINTS:
829 updatesizehints(c);
830 break;
831 }
832 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
833 updatetitle(c);
834 if(c == sel)
835 drawbar();
836 }
837 }
838 }
840 void
841 unmapnotify(XEvent *e) {
842 Client *c;
843 XUnmapEvent *ev = &e->xunmap;
845 if((c = getclient(ev->window)))
846 unmanage(c);
847 }
851 void (*handler[LASTEvent]) (XEvent *) = {
852 [ButtonPress] = buttonpress,
853 [ConfigureRequest] = configurerequest,
854 [DestroyNotify] = destroynotify,
855 [EnterNotify] = enternotify,
856 [LeaveNotify] = leavenotify,
857 [Expose] = expose,
858 [KeyPress] = keypress,
859 [MappingNotify] = mappingnotify,
860 [MapRequest] = maprequest,
861 [PropertyNotify] = propertynotify,
862 [UnmapNotify] = unmapnotify
863 };
865 void
866 grabkeys(void) {
867 static unsigned int len = sizeof key / sizeof key[0];
868 unsigned int i;
869 KeyCode code;
871 XUngrabKey(dpy, AnyKey, AnyModifier, root);
872 for(i = 0; i < len; i++) {
873 code = XKeysymToKeycode(dpy, key[i].keysym);
874 XGrabKey(dpy, code, key[i].mod, root, True,
875 GrabModeAsync, GrabModeAsync);
876 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
877 GrabModeAsync, GrabModeAsync);
878 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
879 GrabModeAsync, GrabModeAsync);
880 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
881 GrabModeAsync, GrabModeAsync);
882 }
883 }
885 /* from draw.c */
887 unsigned int
888 textnw(const char *text, unsigned int len) {
889 XRectangle r;
891 if(dc.font.set) {
892 XmbTextExtents(dc.font.set, text, len, NULL, &r);
893 return r.width;
894 }
895 return XTextWidth(dc.font.xfont, text, len);
896 }
898 void
899 drawtext(const char *text, unsigned long col[ColLast]) {
900 int x, y, w, h;
901 static char buf[256];
902 unsigned int len, olen;
903 XGCValues gcv;
904 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
906 XSetForeground(dpy, dc.gc, col[ColBG]);
907 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
908 if(!text)
909 return;
910 w = 0;
911 olen = len = strlen(text);
912 if(len >= sizeof buf)
913 len = sizeof buf - 1;
914 memcpy(buf, text, len);
915 buf[len] = 0;
916 h = dc.font.ascent + dc.font.descent;
917 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
918 x = dc.x + (h / 2);
919 /* shorten text if necessary */
920 while(len && (w = textnw(buf, len)) > dc.w - h)
921 buf[--len] = 0;
922 if(len < olen) {
923 if(len > 1)
924 buf[len - 1] = '.';
925 if(len > 2)
926 buf[len - 2] = '.';
927 if(len > 3)
928 buf[len - 3] = '.';
929 }
930 if(w > dc.w)
931 return; /* too long */
932 gcv.foreground = col[ColFG];
933 if(dc.font.set) {
934 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
935 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
936 } else {
937 gcv.font = dc.font.xfont->fid;
938 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
939 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
940 }
941 }
943 void
944 drawbar(void) {
945 int x;
946 unsigned long black[ColLast];
947 black[ColBG] = getcolor("#000000");
948 black[ColFG] = getcolor("#ffffff");
950 /* views */
951 dc.x = dc.y = 0;
952 dc.w = textw(NAMETAGGED);
953 drawtext(NAMETAGGED, ( (seltag&&!viewfloats) ? dc.sel : dc.norm ));
955 dc.x += dc.w;
956 dc.w = BORDERPX;
957 drawtext(NULL, black);
959 dc.x += dc.w;
960 dc.w = textw(NAMEUNTAGGED);
961 drawtext(NAMEUNTAGGED, ( (!seltag&&!viewfloats) ? dc.sel : dc.norm ));
963 dc.x += dc.w;
964 dc.w = BORDERPX;
965 drawtext(NULL, black);
967 /* status text */
968 x = dc.x + dc.w;
969 dc.w = textw(stext);
970 dc.x = sw - dc.w;
971 if(dc.x < x) {
972 dc.x = x;
973 dc.w = sw - x;
974 }
975 drawtext(stext, dc.norm);
977 /* client title */
978 if((dc.w = dc.x - x) > bh) {
979 dc.x = x;
980 drawtext(sel ? sel->name : NULL, dc.norm);
982 dc.x += dc.w;
983 dc.w = BORDERPX;
984 drawtext(NULL, black);
985 }
987 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
988 XSync(dpy, False);
989 }
991 unsigned long
992 getcolor(const char *colstr) {
993 Colormap cmap = DefaultColormap(dpy, screen);
994 XColor color;
996 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
997 die("error, cannot allocate color '%s'\n", colstr);
998 return color.pixel;
999 }
1001 void
1002 setfont(const char *fontstr) {
1003 char *def, **missing;
1004 int i, n;
1006 missing = NULL;
1007 if(dc.font.set)
1008 XFreeFontSet(dpy, dc.font.set);
1009 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
1010 if(missing) {
1011 while(n--)
1012 fprintf(stderr, "missing fontset: %s\n", missing[n]);
1013 XFreeStringList(missing);
1015 if(dc.font.set) {
1016 XFontSetExtents *font_extents;
1017 XFontStruct **xfonts;
1018 char **font_names;
1019 dc.font.ascent = dc.font.descent = 0;
1020 font_extents = XExtentsOfFontSet(dc.font.set);
1021 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
1022 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
1023 if(dc.font.ascent < (*xfonts)->ascent)
1024 dc.font.ascent = (*xfonts)->ascent;
1025 if(dc.font.descent < (*xfonts)->descent)
1026 dc.font.descent = (*xfonts)->descent;
1027 xfonts++;
1029 } else {
1030 if(dc.font.xfont)
1031 XFreeFont(dpy, dc.font.xfont);
1032 dc.font.xfont = NULL;
1033 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
1034 die("error, cannot load font: '%s'\n", fontstr);
1035 dc.font.ascent = dc.font.xfont->ascent;
1036 dc.font.descent = dc.font.xfont->descent;
1038 dc.font.height = dc.font.ascent + dc.font.descent;
1041 unsigned int
1042 textw(const char *text) {
1043 return textnw(text, strlen(text)) + dc.font.height;
1046 /* from client.c */
1048 void
1049 detachstack(Client *c) {
1050 Client **tc;
1051 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
1052 *tc = c->snext;
1055 void
1056 grabbuttons(Client *c, Bool focused) {
1057 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1059 if(focused) {
1060 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
1061 GrabModeAsync, GrabModeSync, None, None);
1062 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
1063 GrabModeAsync, GrabModeSync, None, None);
1064 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
1065 GrabModeAsync, GrabModeSync, None, None);
1066 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
1067 GrabModeAsync, GrabModeSync, None, None);
1069 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
1070 GrabModeAsync, GrabModeSync, None, None);
1071 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
1072 GrabModeAsync, GrabModeSync, None, None);
1073 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
1074 GrabModeAsync, GrabModeSync, None, None);
1075 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
1076 GrabModeAsync, GrabModeSync, None, None);
1078 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
1079 GrabModeAsync, GrabModeSync, None, None);
1080 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
1081 GrabModeAsync, GrabModeSync, None, None);
1082 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
1083 GrabModeAsync, GrabModeSync, None, None);
1084 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
1085 GrabModeAsync, GrabModeSync, None, None);
1086 } else {
1087 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
1088 GrabModeAsync, GrabModeSync, None, None);
1092 void
1093 setclientstate(Client *c, long state) {
1094 long data[] = {state, None};
1095 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1096 PropModeReplace, (unsigned char *)data, 2);
1099 int
1100 xerrordummy(Display *dsply, XErrorEvent *ee) {
1101 return 0;
1104 void
1105 configure(Client *c) {
1106 XEvent synev;
1108 synev.type = ConfigureNotify;
1109 synev.xconfigure.display = dpy;
1110 synev.xconfigure.event = c->win;
1111 synev.xconfigure.window = c->win;
1112 synev.xconfigure.x = c->x;
1113 synev.xconfigure.y = c->y;
1114 synev.xconfigure.width = c->w;
1115 synev.xconfigure.height = c->h;
1116 synev.xconfigure.border_width = c->border;
1117 synev.xconfigure.above = None;
1118 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
1121 void
1122 focus(Client *c) {
1123 if(c && !isvisible(c))
1124 return;
1125 if(sel && sel != c) {
1126 grabbuttons(sel, False);
1127 XSetWindowBorder(dpy, sel->win, dc.norm[ColBG]);
1129 if(c) {
1130 detachstack(c);
1131 c->snext = stack;
1132 stack = c;
1133 grabbuttons(c, True);
1135 sel = c;
1136 drawbar();
1137 if(!selscreen)
1138 return;
1139 if(c) {
1140 XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
1141 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1142 } else {
1143 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1147 Client *
1148 getclient(Window w) {
1149 Client *c;
1151 for(c = clients; c; c = c->next) {
1152 if(c->win == w) {
1153 return c;
1156 return NULL;
1159 Bool
1160 isprotodel(Client *c) {
1161 int i, n;
1162 Atom *protocols;
1163 Bool ret = False;
1165 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1166 for(i = 0; !ret && i < n; i++)
1167 if(protocols[i] == wmatom[WMDelete])
1168 ret = True;
1169 XFree(protocols);
1171 return ret;
1174 void
1175 killclient() {
1176 if(!sel)
1177 return;
1178 if(isprotodel(sel))
1179 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
1180 else
1181 XKillClient(dpy, sel->win);
1184 void
1185 manage(Window w, XWindowAttributes *wa) {
1186 Client *c;
1187 Window trans;
1189 c = emallocz(sizeof(Client));
1190 c->tag = True;
1191 c->win = w;
1192 c->x = wa->x;
1193 c->y = wa->y;
1194 c->w = wa->width;
1195 c->h = wa->height;
1196 if(c->w == sw && c->h == sh) {
1197 c->border = 0;
1198 c->x = sx;
1199 c->y = sy;
1200 } else {
1201 c->border = BORDERPX;
1202 if(c->x + c->w + 2 * c->border > wax + waw)
1203 c->x = wax + waw - c->w - 2 * c->border;
1204 if(c->y + c->h + 2 * c->border > way + wah)
1205 c->y = way + wah - c->h - 2 * c->border;
1206 if(c->x < wax)
1207 c->x = wax;
1208 if(c->y < way)
1209 c->y = way;
1211 updatesizehints(c);
1212 XSelectInput(dpy, c->win,
1213 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
1214 XGetTransientForHint(dpy, c->win, &trans);
1215 grabbuttons(c, False);
1216 XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
1217 updatetitle(c);
1218 settag(c, getclient(trans));
1219 if(!c->isfloat)
1220 c->isfloat = trans || c->isfixed;
1221 if(clients)
1222 clients->prev = c;
1223 c->next = clients;
1224 c->snext = stack;
1225 stack = clients = c;
1226 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
1227 XMapWindow(dpy, c->win);
1228 setclientstate(c, NormalState);
1229 if(isvisible(c))
1230 focus(c);
1231 arrange();
1234 void
1235 resize(Client *c, Bool sizehints) {
1236 float actual, dx, dy, max, min;
1237 XWindowChanges wc;
1239 if(c->w <= 0 || c->h <= 0)
1240 return;
1241 if(sizehints) {
1242 if(c->minw && c->w < c->minw)
1243 c->w = c->minw;
1244 if(c->minh && c->h < c->minh)
1245 c->h = c->minh;
1246 if(c->maxw && c->w > c->maxw)
1247 c->w = c->maxw;
1248 if(c->maxh && c->h > c->maxh)
1249 c->h = c->maxh;
1250 /* inspired by algorithm from fluxbox */
1251 if(c->minay > 0 && c->maxay && (c->h - c->baseh) > 0) {
1252 dx = (float)(c->w - c->basew);
1253 dy = (float)(c->h - c->baseh);
1254 min = (float)(c->minax) / (float)(c->minay);
1255 max = (float)(c->maxax) / (float)(c->maxay);
1256 actual = dx / dy;
1257 if(max > 0 && min > 0 && actual > 0) {
1258 if(actual < min) {
1259 dy = (dx * min + dy) / (min * min + 1);
1260 dx = dy * min;
1261 c->w = (int)dx + c->basew;
1262 c->h = (int)dy + c->baseh;
1264 else if(actual > max) {
1265 dy = (dx * min + dy) / (max * max + 1);
1266 dx = dy * min;
1267 c->w = (int)dx + c->basew;
1268 c->h = (int)dy + c->baseh;
1272 if(c->incw)
1273 c->w -= (c->w - c->basew) % c->incw;
1274 if(c->inch)
1275 c->h -= (c->h - c->baseh) % c->inch;
1277 if(c->w == sw && c->h == sh)
1278 c->border = 0;
1279 else
1280 c->border = BORDERPX;
1281 /* offscreen appearance fixes */
1282 if(c->x > sw)
1283 c->x = sw - c->w - 2 * c->border;
1284 if(c->y > sh)
1285 c->y = sh - c->h - 2 * c->border;
1286 if(c->x + c->w + 2 * c->border < sx)
1287 c->x = sx;
1288 if(c->y + c->h + 2 * c->border < sy)
1289 c->y = sy;
1290 wc.x = c->x;
1291 wc.y = c->y;
1292 wc.width = c->w;
1293 wc.height = c->h;
1294 wc.border_width = c->border;
1295 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
1296 configure(c);
1297 XSync(dpy, False);
1300 void
1301 updatesizehints(Client *c) {
1302 long msize;
1303 XSizeHints size;
1305 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
1306 size.flags = PSize;
1307 c->flags = size.flags;
1308 if(c->flags & PBaseSize) {
1309 c->basew = size.base_width;
1310 c->baseh = size.base_height;
1311 } else {
1312 c->basew = c->baseh = 0;
1314 if(c->flags & PResizeInc) {
1315 c->incw = size.width_inc;
1316 c->inch = size.height_inc;
1317 } else {
1318 c->incw = c->inch = 0;
1320 if(c->flags & PMaxSize) {
1321 c->maxw = size.max_width;
1322 c->maxh = size.max_height;
1323 } else {
1324 c->maxw = c->maxh = 0;
1326 if(c->flags & PMinSize) {
1327 c->minw = size.min_width;
1328 c->minh = size.min_height;
1329 } else {
1330 c->minw = c->minh = 0;
1332 if(c->flags & PAspect) {
1333 c->minax = size.min_aspect.x;
1334 c->minay = size.min_aspect.y;
1335 c->maxax = size.max_aspect.x;
1336 c->maxay = size.max_aspect.y;
1337 } else {
1338 c->minax = c->minay = c->maxax = c->maxay = 0;
1340 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
1341 c->maxw == c->minw && c->maxh == c->minh);
1344 void
1345 updatetitle(Client *c) {
1346 if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name)) {
1347 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
1352 Bool
1353 gettextprop(Window w, Atom atom, char* text, unsigned int size) {
1354 char **list = NULL;
1355 int n;
1356 XTextProperty name;
1358 if (!text || size == 0) {
1359 return False;
1361 text[0] = '\0';
1362 XGetTextProperty(dpy, w, &name, atom);
1363 if(!name.nitems)
1364 return False;
1365 if(name.encoding == XA_STRING)
1366 strncpy(text, (char *)name.value, size - 1);
1367 else {
1368 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
1369 strncpy(text, *list, size - 1);
1370 XFreeStringList(list);
1373 text[size - 1] = '\0';
1374 XFree(name.value);
1375 return True;
1378 void
1379 unmanage(Client *c) {
1380 Client *nc;
1382 /* The server grab construct avoids race conditions. */
1383 XGrabServer(dpy);
1384 XSetErrorHandler(xerrordummy);
1385 detach(c);
1386 detachstack(c);
1387 if(sel == c) {
1388 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
1389 focus(nc);
1391 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1392 setclientstate(c, WithdrawnState);
1393 free(c);
1394 XSync(dpy, False);
1395 XSetErrorHandler(xerror);
1396 XUngrabServer(dpy);
1397 arrange();
1400 /* from main.c */
1402 void
1403 cleanup(void) {
1404 while(stack) {
1405 resize(stack, True);
1406 unmanage(stack);
1408 if(dc.font.set)
1409 XFreeFontSet(dpy, dc.font.set);
1410 else
1411 XFreeFont(dpy, dc.font.xfont);
1412 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1413 XFreePixmap(dpy, dc.drawable);
1414 XFreeGC(dpy, dc.gc);
1415 XDestroyWindow(dpy, barwin);
1416 XFreeCursor(dpy, cursor[CurNormal]);
1417 XFreeCursor(dpy, cursor[CurResize]);
1418 XFreeCursor(dpy, cursor[CurMove]);
1419 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
1420 XSync(dpy, False);
1423 void
1424 scan(void) {
1425 unsigned int i, num;
1426 Window *wins, d1, d2;
1427 XWindowAttributes wa;
1429 wins = NULL;
1430 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1431 for(i = 0; i < num; i++) {
1432 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1433 continue;
1434 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1435 continue;
1436 if(wa.map_state == IsViewable)
1437 manage(wins[i], &wa);
1440 if(wins)
1441 XFree(wins);
1444 void
1445 setup(void) {
1446 int i, j;
1447 unsigned int mask;
1448 Window w;
1449 XModifierKeymap *modmap;
1450 XSetWindowAttributes wa;
1452 screen = DefaultScreen(dpy);
1453 root = RootWindow(dpy, screen);
1455 /* init atoms */
1456 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1457 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1458 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1459 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1460 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1461 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1462 PropModeReplace, (unsigned char *) netatom, NetLast);
1463 /* init cursors */
1464 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1465 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1466 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1467 /* init modifier map */
1468 numlockmask = 0;
1469 modmap = XGetModifierMapping(dpy);
1470 for (i = 0; i < 8; i++) {
1471 for (j = 0; j < modmap->max_keypermod; j++) {
1472 if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
1473 numlockmask = (1 << i);
1476 XFreeModifiermap(modmap);
1477 /* select for events */
1478 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
1479 | EnterWindowMask | LeaveWindowMask | PropertyChangeMask;
1480 wa.cursor = cursor[CurNormal];
1481 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
1482 grabkeys();
1483 seltag = False;
1484 viewfloats = False;
1485 /* style */
1486 dc.norm[ColBG] = getcolor(NORMBGCOLOR);
1487 dc.norm[ColFG] = getcolor(NORMFGCOLOR);
1488 dc.sel[ColBG] = getcolor(SELBGCOLOR);
1489 dc.sel[ColFG] = getcolor(SELFGCOLOR);
1490 setfont(FONT);
1491 /* geometry */
1492 sx = sy = 0;
1493 sw = DisplayWidth(dpy, screen);
1494 sh = DisplayHeight(dpy, screen);
1495 nmaster = NMASTER;
1496 arrange1 = DEFMODE;
1497 arrange2 = DEFMODE;
1498 /* bar */
1499 dc.h = bh = dc.font.height + 2;
1500 wa.override_redirect = 1;
1501 wa.background_pixmap = ParentRelative;
1502 wa.event_mask = ButtonPressMask | ExposureMask;
1503 barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0,
1504 DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
1505 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
1506 XDefineCursor(dpy, barwin, cursor[CurNormal]);
1507 XMapRaised(dpy, barwin);
1508 /* windowarea */
1509 wax = sx;
1510 way = sy + bh;
1511 wah = sh - bh;
1512 waw = sw;
1513 /* pixmap for everything */
1514 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
1515 dc.gc = XCreateGC(dpy, root, 0, 0);
1516 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1517 /* multihead support */
1518 selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
1520 updatestatus();
1523 /*
1524 * Startup Error handler to check if another window manager
1525 * is already running.
1526 */
1527 int
1528 xerrorstart(Display *dsply, XErrorEvent *ee) {
1529 otherwm = True;
1530 return -1;
1533 void
1534 sendevent(Window w, Atom a, long value) {
1535 XEvent e;
1537 e.type = ClientMessage;
1538 e.xclient.window = w;
1539 e.xclient.message_type = a;
1540 e.xclient.format = 32;
1541 e.xclient.data.l[0] = value;
1542 e.xclient.data.l[1] = CurrentTime;
1543 XSendEvent(dpy, w, False, NoEventMask, &e);
1544 XSync(dpy, False);
1547 void
1548 quit() {
1549 running = False;
1552 /* There's no way to check accesses to destroyed windows, thus those cases are
1553 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1554 * default error handler, which may call exit.
1555 */
1556 int
1557 xerror(Display *dpy, XErrorEvent *ee) {
1558 if(ee->error_code == BadWindow
1559 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
1560 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
1561 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
1562 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
1563 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
1564 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
1565 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
1566 return 0;
1567 fprintf(stderr, "aewl: fatal error: request code=%d, error code=%d\n",
1568 ee->request_code, ee->error_code);
1569 return xerrorxlib(dpy, ee); /* may call exit */
1572 void
1573 checkotherwm(void) {
1574 otherwm = False;
1575 xerrorxlib = XSetErrorHandler(xerrorstart);
1576 /* this causes an error if some other window manager is running */
1577 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
1578 XSync(dpy, False);
1579 if(otherwm) {
1580 die("aewl: another window manager is already running\n");
1583 XSetErrorHandler(xerror);
1584 XSync(dpy, False);
1587 void
1588 run(void) {
1589 XEvent ev;
1591 XSync(dpy, False);
1592 /* main event loop */
1593 while(running && !XNextEvent(dpy, &ev)) {
1594 if(handler[ev.type]) {
1595 (handler[ev.type])(&ev); /* call handler */
1600 void
1601 updatestatus(void) {
1602 if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext))) {
1603 strcpy(stext, "aewl-"VERSION);
1605 drawbar();
1608 int
1609 main(int argc, char *argv[]) {
1611 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
1612 fputs("aewl-"VERSION", Copyright 2008 markus schnalke <meillo@marmaro.de>\n", stdout);
1613 fputs("forked off dwm-3.4, (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout);
1614 exit(EXIT_SUCCESS);
1615 } else if(argc != 1) {
1616 die("usage: aewl [-v]\n");
1618 setlocale(LC_CTYPE, "");
1619 dpy = XOpenDisplay(0);
1620 if(!dpy) {
1621 die("aewl: cannot open display\n");
1624 checkotherwm();
1625 setup();
1626 scan();
1627 run();
1628 cleanup();
1630 XCloseDisplay(dpy);
1631 return 0;