aewl

view aewl.c @ 786:e65be4ffdbdc

Add handling for popup windows
author markus schnalke <meillo@marmaro.de>
date Thu, 14 Jun 2012 00:00:00 +0200
parents f259785bac44
children eecb13b34511
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, ispopup;
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->ispopup); 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 || c->ispopup) {
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 || c->ispopup) {
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 || sel->ispopup)
384 XRaiseWindow(dpy, sel->win);
385 else
386 XLowerWindow(dpy, sel->win);
387 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
388 if(c == sel)
389 continue;
390 XLowerWindow(dpy, c->win);
391 }
393 XSync(dpy, False);
394 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
395 }
397 void
398 togglefloat() {
399 if (!sel)
400 return;
401 sel->isfloat = !sel->isfloat;
402 floattoggle();
403 }
405 void
406 togglemode() {
407 /* toggle between tile and max */
408 if (viewfloats) {
409 return;
410 }
411 if (seltag) {
412 arrange1 = (arrange1 == dotile) ? domax : dotile;
413 } else {
414 arrange2 = (arrange2 == dotile) ? domax : dotile;
415 }
416 if(sel)
417 arrange();
418 zoom();
419 }
421 void
422 arrange() {
423 if (seltag) {
424 arrange1();
425 } else {
426 arrange2();
427 }
428 }
430 void
431 toggleview() {
432 if (viewfloats) {
433 floattoggle();
434 return;
435 }
436 seltag = !seltag;
437 arrange();
438 }
440 void
441 floattoggle() {
442 viewfloats = !viewfloats;
443 arrange();
444 }
446 void
447 zoom() {
448 unsigned int n;
449 Client *c;
451 if(!sel)
452 return;
453 if(sel->isfloat || sel->ispopup) {
454 togglemax(sel);
455 return;
456 }
457 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
458 n++;
460 if((c = sel) == nexttiled(clients))
461 if(!(c = nexttiled(c->next)))
462 return;
463 detach(c);
464 if(clients)
465 clients->prev = c;
466 c->next = clients;
467 clients = c;
468 focus(c);
469 arrange();
470 }
472 /* from util.c */
474 void *
475 emallocz(unsigned int size) {
476 void *res = calloc(1, size);
478 if(!res)
479 die("fatal: could not malloc() %u bytes\n", size);
480 return res;
481 }
483 void
484 die(const char *errstr, ...) {
485 va_list ap;
487 va_start(ap, errstr);
488 vfprintf(stderr, errstr, ap);
489 va_end(ap);
490 exit(EXIT_FAILURE);
491 }
493 void
494 spawn(const char* cmd) {
495 static char *shell = NULL;
497 if(!cmd)
498 return;
499 if(!(shell = getenv("SHELL")))
500 shell = "/bin/sh";
501 /* The double-fork construct avoids zombie processes and keeps the code
502 * clean from stupid signal handlers. */
503 if(fork() == 0) {
504 if(fork() == 0) {
505 if(dpy)
506 close(ConnectionNumber(dpy));
507 setsid();
508 execl(shell, shell, "-c", cmd, (char *)NULL);
509 fprintf(stderr, "aewl: execl '%s -c %s'", shell, cmd);
510 perror(" failed");
511 }
512 exit(0);
513 }
514 wait(0);
515 }
517 /* from tag.c */
519 Client *
520 getnext(Client *c) {
521 while(c && !isvisible(c)) {
522 c = c->next;
523 }
524 return c;
525 }
527 void
528 settag(Client *c, Client *trans) {
529 unsigned int i;
530 XClassHint ch = { 0 };
532 if(trans) {
533 c->tag = trans->tag;
534 c->isfloat = trans->isfloat;
535 return;
536 }
537 c->tag = seltag; /* default */
538 XGetClassHint(dpy, c->win, &ch);
539 len = sizeof rule / sizeof rule[0];
540 for(i = 0; i < len; i++) {
541 if((rule[i].title && strstr(c->name, rule[i].title))
542 || (ch.res_class && rule[i].class && strstr(ch.res_class, rule[i].class))
543 || (ch.res_name && rule[i].instance && strstr(ch.res_name, rule[i].instance))) {
544 c->isfloat = rule[i].isfloat;
545 if (rule[i].tag < 0) {
546 c->tag = seltag;
547 } else if (rule[i].tag) {
548 c->tag = True;
549 } else {
550 c->tag = False;
551 }
552 break;
553 }
554 }
555 if(ch.res_class)
556 XFree(ch.res_class);
557 if(ch.res_name)
558 XFree(ch.res_name);
559 }
561 void
562 toggletag() {
563 if(!sel)
564 return;
565 sel->tag = !sel->tag;
566 toggleview();
567 }
569 /* from event.c */
571 void
572 movemouse(Client *c) {
573 int x1, y1, ocx, ocy, di;
574 unsigned int dui;
575 Window dummy;
576 XEvent ev;
578 ocx = c->x;
579 ocy = c->y;
580 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
581 None, cursor[CurMove], CurrentTime) != GrabSuccess)
582 return;
583 c->ismax = False;
584 XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
585 for(;;) {
586 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
587 switch (ev.type) {
588 case ButtonRelease:
589 resize(c, True);
590 XUngrabPointer(dpy, CurrentTime);
591 return;
592 case ConfigureRequest:
593 case Expose:
594 case MapRequest:
595 handler[ev.type](&ev);
596 break;
597 case MotionNotify:
598 XSync(dpy, False);
599 c->x = ocx + (ev.xmotion.x - x1);
600 c->y = ocy + (ev.xmotion.y - y1);
601 if(abs(wax + c->x) < SNAP)
602 c->x = wax;
603 else if(abs((wax + waw) - (c->x + c->w + 2 * c->border)) < SNAP)
604 c->x = wax + waw - c->w - 2 * c->border;
605 if(abs(way - c->y) < SNAP)
606 c->y = way;
607 else if(abs((way + wah) - (c->y + c->h + 2 * c->border)) < SNAP)
608 c->y = way + wah - c->h - 2 * c->border;
609 resize(c, False);
610 break;
611 }
612 }
613 }
615 void
616 resizemouse(Client *c) {
617 int ocx, ocy;
618 int nw, nh;
619 XEvent ev;
621 ocx = c->x;
622 ocy = c->y;
623 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
624 None, cursor[CurResize], CurrentTime) != GrabSuccess)
625 return;
626 c->ismax = False;
627 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
628 for(;;) {
629 XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
630 switch(ev.type) {
631 case ButtonRelease:
632 resize(c, True);
633 XUngrabPointer(dpy, CurrentTime);
634 return;
635 case ConfigureRequest:
636 case Expose:
637 case MapRequest:
638 handler[ev.type](&ev);
639 break;
640 case MotionNotify:
641 XSync(dpy, False);
642 nw = ev.xmotion.x - ocx - 2 * c->border + 1;
643 c->w = nw > 0 ? nw : 1;
644 nh = ev.xmotion.y - ocy - 2 * c->border + 1;
645 c->h = nh > 0 ? nh : 1;
646 resize(c, True);
647 break;
648 }
649 }
650 }
652 void
653 buttonpress(XEvent *e) {
654 Client *c;
655 XButtonPressedEvent *ev = &e->xbutton;
657 if(barwin == ev->window) {
658 return;
659 }
660 if((c = getclient(ev->window))) {
661 focus(c);
662 if(CLEANMASK(ev->state) != MODKEY)
663 return;
664 if(ev->button == Button1 && (c->isfloat || c->ispopup)) {
665 restack();
666 movemouse(c);
667 } else if(ev->button == Button3 && (c->isfloat || c->ispopup) && !c->isfixed) {
668 restack();
669 resizemouse(c);
670 }
671 }
672 }
674 void
675 configurerequest(XEvent *e) {
676 unsigned long newmask;
677 Client *c;
678 XConfigureRequestEvent *ev = &e->xconfigurerequest;
679 XWindowChanges wc;
681 if((c = getclient(ev->window))) {
682 c->ismax = False;
683 if(ev->value_mask & CWX)
684 c->x = ev->x;
685 if(ev->value_mask & CWY)
686 c->y = ev->y;
687 if(ev->value_mask & CWWidth)
688 c->w = ev->width;
689 if(ev->value_mask & CWHeight)
690 c->h = ev->height;
691 if(ev->value_mask & CWBorderWidth)
692 c->border = ev->border_width;
693 wc.x = c->x;
694 wc.y = c->y;
695 wc.width = c->w;
696 wc.height = c->h;
697 newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
698 if(newmask)
699 XConfigureWindow(dpy, c->win, newmask, &wc);
700 else
701 configure(c);
702 XSync(dpy, False);
703 if(c->isfloat || c->ispopup) {
704 resize(c, False);
705 if(!isvisible(c))
706 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
707 }
708 else
709 arrange();
710 } else {
711 wc.x = ev->x;
712 wc.y = ev->y;
713 wc.width = ev->width;
714 wc.height = ev->height;
715 wc.border_width = ev->border_width;
716 wc.sibling = ev->above;
717 wc.stack_mode = ev->detail;
718 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
719 XSync(dpy, False);
720 }
721 }
723 void
724 destroynotify(XEvent *e) {
725 Client *c;
726 XDestroyWindowEvent *ev = &e->xdestroywindow;
728 if((c = getclient(ev->window)))
729 unmanage(c);
730 }
732 void
733 enternotify(XEvent *e) {
734 Client *c;
735 XCrossingEvent *ev = &e->xcrossing;
737 if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
738 return;
739 if((c = getclient(ev->window)) && isvisible(c))
740 focus(c);
741 else if(ev->window == root) {
742 selscreen = True;
743 for(c = stack; c && !isvisible(c); c = c->snext);
744 focus(c);
745 }
746 }
748 void
749 expose(XEvent *e) {
750 XExposeEvent *ev = &e->xexpose;
752 if(ev->count == 0) {
753 if(barwin == ev->window)
754 drawbar();
755 }
756 }
758 void
759 keypress(XEvent *e) {
760 static unsigned int len = sizeof key / sizeof key[0];
761 unsigned int i;
762 KeySym keysym;
763 XKeyEvent *ev = &e->xkey;
765 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
766 for(i = 0; i < len; i++) {
767 if(keysym == key[i].keysym && CLEANMASK(key[i].mod) == CLEANMASK(ev->state)) {
768 if(key[i].func)
769 key[i].func(key[i].cmd);
770 }
771 }
772 }
774 void
775 leavenotify(XEvent *e) {
776 XCrossingEvent *ev = &e->xcrossing;
778 if((ev->window == root) && !ev->same_screen) {
779 selscreen = False;
780 focus(NULL);
781 }
782 }
784 void
785 mappingnotify(XEvent *e) {
786 XMappingEvent *ev = &e->xmapping;
788 XRefreshKeyboardMapping(ev);
789 if(ev->request == MappingKeyboard)
790 grabkeys();
791 }
793 void
794 maprequest(XEvent *e) {
795 static XWindowAttributes wa;
796 XMapRequestEvent *ev = &e->xmaprequest;
798 if(!XGetWindowAttributes(dpy, ev->window, &wa))
799 return;
800 if(wa.override_redirect) {
801 XSelectInput(dpy, ev->window,
802 (StructureNotifyMask | PropertyChangeMask));
803 return;
804 }
805 if(!getclient(ev->window))
806 manage(ev->window, &wa);
807 }
809 void
810 propertynotify(XEvent *e) {
811 Client *c;
812 Window trans;
813 XPropertyEvent *ev = &e->xproperty;
815 if((ev->window == root) && (ev->atom = XA_WM_NAME))
816 updatestatus();
817 else if(ev->state == PropertyDelete)
818 return; /* ignore */
819 else if((c = getclient(ev->window))) {
820 switch (ev->atom) {
821 default: break;
822 case XA_WM_TRANSIENT_FOR:
823 XGetTransientForHint(dpy, c->win, &trans);
824 if(!c->ispopup && (c->ispopup = (trans != 0)))
825 arrange();
826 break;
827 case XA_WM_NORMAL_HINTS:
828 updatesizehints(c);
829 break;
830 }
831 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
832 updatetitle(c);
833 if(c == sel)
834 drawbar();
835 }
836 }
837 }
839 void
840 unmapnotify(XEvent *e) {
841 Client *c;
842 XUnmapEvent *ev = &e->xunmap;
844 if((c = getclient(ev->window)))
845 unmanage(c);
846 }
850 void (*handler[LASTEvent]) (XEvent *) = {
851 [ButtonPress] = buttonpress,
852 [ConfigureRequest] = configurerequest,
853 [DestroyNotify] = destroynotify,
854 [EnterNotify] = enternotify,
855 [LeaveNotify] = leavenotify,
856 [Expose] = expose,
857 [KeyPress] = keypress,
858 [MappingNotify] = mappingnotify,
859 [MapRequest] = maprequest,
860 [PropertyNotify] = propertynotify,
861 [UnmapNotify] = unmapnotify
862 };
864 void
865 grabkeys(void) {
866 static unsigned int len = sizeof key / sizeof key[0];
867 unsigned int i;
868 KeyCode code;
870 XUngrabKey(dpy, AnyKey, AnyModifier, root);
871 for(i = 0; i < len; i++) {
872 code = XKeysymToKeycode(dpy, key[i].keysym);
873 XGrabKey(dpy, code, key[i].mod, root, True,
874 GrabModeAsync, GrabModeAsync);
875 XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
876 GrabModeAsync, GrabModeAsync);
877 XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
878 GrabModeAsync, GrabModeAsync);
879 XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
880 GrabModeAsync, GrabModeAsync);
881 }
882 }
884 /* from draw.c */
886 unsigned int
887 textnw(const char *text, unsigned int len) {
888 XRectangle r;
890 if(dc.font.set) {
891 XmbTextExtents(dc.font.set, text, len, NULL, &r);
892 return r.width;
893 }
894 return XTextWidth(dc.font.xfont, text, len);
895 }
897 void
898 drawtext(const char *text, unsigned long col[ColLast]) {
899 int x, y, w, h;
900 static char buf[256];
901 unsigned int len, olen;
902 XGCValues gcv;
903 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
905 XSetForeground(dpy, dc.gc, col[ColBG]);
906 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
907 if(!text)
908 return;
909 w = 0;
910 olen = len = strlen(text);
911 if(len >= sizeof buf)
912 len = sizeof buf - 1;
913 memcpy(buf, text, len);
914 buf[len] = 0;
915 h = dc.font.ascent + dc.font.descent;
916 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
917 x = dc.x + (h / 2);
918 /* shorten text if necessary */
919 while(len && (w = textnw(buf, len)) > dc.w - h)
920 buf[--len] = 0;
921 if(len < olen) {
922 if(len > 1)
923 buf[len - 1] = '.';
924 if(len > 2)
925 buf[len - 2] = '.';
926 if(len > 3)
927 buf[len - 3] = '.';
928 }
929 if(w > dc.w)
930 return; /* too long */
931 gcv.foreground = col[ColFG];
932 if(dc.font.set) {
933 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
934 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
935 } else {
936 gcv.font = dc.font.xfont->fid;
937 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
938 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
939 }
940 }
942 void
943 drawbar(void) {
944 int x;
945 unsigned long black[ColLast];
946 black[ColBG] = getcolor("#000000");
947 black[ColFG] = getcolor("#ffffff");
949 /* views */
950 dc.x = dc.y = 0;
951 dc.w = textw(NAMETAGGED);
952 drawtext(NAMETAGGED, ( (seltag&&!viewfloats) ? dc.sel : dc.norm ));
954 dc.x += dc.w;
955 dc.w = BORDERPX;
956 drawtext(NULL, black);
958 dc.x += dc.w;
959 dc.w = textw(NAMEUNTAGGED);
960 drawtext(NAMEUNTAGGED, ( (!seltag&&!viewfloats) ? dc.sel : dc.norm ));
962 dc.x += dc.w;
963 dc.w = BORDERPX;
964 drawtext(NULL, black);
966 /* status text */
967 x = dc.x + dc.w;
968 dc.w = textw(stext);
969 dc.x = sw - dc.w;
970 if(dc.x < x) {
971 dc.x = x;
972 dc.w = sw - x;
973 }
974 drawtext(stext, dc.norm);
976 /* client title */
977 if((dc.w = dc.x - x) > bh) {
978 dc.x = x;
979 drawtext(sel ? sel->name : NULL, dc.norm);
981 dc.x += dc.w;
982 dc.w = BORDERPX;
983 drawtext(NULL, black);
984 }
986 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
987 XSync(dpy, False);
988 }
990 unsigned long
991 getcolor(const char *colstr) {
992 Colormap cmap = DefaultColormap(dpy, screen);
993 XColor color;
995 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
996 die("error, cannot allocate color '%s'\n", colstr);
997 return color.pixel;
998 }
1000 void
1001 setfont(const char *fontstr) {
1002 char *def, **missing;
1003 int i, n;
1005 missing = NULL;
1006 if(dc.font.set)
1007 XFreeFontSet(dpy, dc.font.set);
1008 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
1009 if(missing) {
1010 while(n--)
1011 fprintf(stderr, "missing fontset: %s\n", missing[n]);
1012 XFreeStringList(missing);
1014 if(dc.font.set) {
1015 XFontSetExtents *font_extents;
1016 XFontStruct **xfonts;
1017 char **font_names;
1018 dc.font.ascent = dc.font.descent = 0;
1019 font_extents = XExtentsOfFontSet(dc.font.set);
1020 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
1021 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
1022 if(dc.font.ascent < (*xfonts)->ascent)
1023 dc.font.ascent = (*xfonts)->ascent;
1024 if(dc.font.descent < (*xfonts)->descent)
1025 dc.font.descent = (*xfonts)->descent;
1026 xfonts++;
1028 } else {
1029 if(dc.font.xfont)
1030 XFreeFont(dpy, dc.font.xfont);
1031 dc.font.xfont = NULL;
1032 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
1033 die("error, cannot load font: '%s'\n", fontstr);
1034 dc.font.ascent = dc.font.xfont->ascent;
1035 dc.font.descent = dc.font.xfont->descent;
1037 dc.font.height = dc.font.ascent + dc.font.descent;
1040 unsigned int
1041 textw(const char *text) {
1042 return textnw(text, strlen(text)) + dc.font.height;
1045 /* from client.c */
1047 void
1048 detachstack(Client *c) {
1049 Client **tc;
1050 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
1051 *tc = c->snext;
1054 void
1055 grabbuttons(Client *c, Bool focused) {
1056 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1058 if(focused) {
1059 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
1060 GrabModeAsync, GrabModeSync, None, None);
1061 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
1062 GrabModeAsync, GrabModeSync, None, None);
1063 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
1064 GrabModeAsync, GrabModeSync, None, None);
1065 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
1066 GrabModeAsync, GrabModeSync, None, None);
1068 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
1069 GrabModeAsync, GrabModeSync, None, None);
1070 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
1071 GrabModeAsync, GrabModeSync, None, None);
1072 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
1073 GrabModeAsync, GrabModeSync, None, None);
1074 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
1075 GrabModeAsync, GrabModeSync, None, None);
1077 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
1078 GrabModeAsync, GrabModeSync, None, None);
1079 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
1080 GrabModeAsync, GrabModeSync, None, None);
1081 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
1082 GrabModeAsync, GrabModeSync, None, None);
1083 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
1084 GrabModeAsync, GrabModeSync, None, None);
1085 } else {
1086 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
1087 GrabModeAsync, GrabModeSync, None, None);
1091 void
1092 setclientstate(Client *c, long state) {
1093 long data[] = {state, None};
1094 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1095 PropModeReplace, (unsigned char *)data, 2);
1098 int
1099 xerrordummy(Display *dsply, XErrorEvent *ee) {
1100 return 0;
1103 void
1104 configure(Client *c) {
1105 XEvent synev;
1107 synev.type = ConfigureNotify;
1108 synev.xconfigure.display = dpy;
1109 synev.xconfigure.event = c->win;
1110 synev.xconfigure.window = c->win;
1111 synev.xconfigure.x = c->x;
1112 synev.xconfigure.y = c->y;
1113 synev.xconfigure.width = c->w;
1114 synev.xconfigure.height = c->h;
1115 synev.xconfigure.border_width = c->border;
1116 synev.xconfigure.above = None;
1117 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
1120 void
1121 focus(Client *c) {
1122 if(c && !isvisible(c))
1123 return;
1124 if(sel && sel != c) {
1125 grabbuttons(sel, False);
1126 XSetWindowBorder(dpy, sel->win, dc.norm[ColBG]);
1128 if(c) {
1129 detachstack(c);
1130 c->snext = stack;
1131 stack = c;
1132 grabbuttons(c, True);
1134 sel = c;
1135 drawbar();
1136 if(!selscreen)
1137 return;
1138 if(c) {
1139 XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
1140 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1141 } else {
1142 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1146 Client *
1147 getclient(Window w) {
1148 Client *c;
1150 for(c = clients; c; c = c->next) {
1151 if(c->win == w) {
1152 return c;
1155 return NULL;
1158 Bool
1159 isprotodel(Client *c) {
1160 int i, n;
1161 Atom *protocols;
1162 Bool ret = False;
1164 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1165 for(i = 0; !ret && i < n; i++)
1166 if(protocols[i] == wmatom[WMDelete])
1167 ret = True;
1168 XFree(protocols);
1170 return ret;
1173 void
1174 killclient() {
1175 if(!sel)
1176 return;
1177 if(isprotodel(sel))
1178 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
1179 else
1180 XKillClient(dpy, sel->win);
1183 void
1184 manage(Window w, XWindowAttributes *wa) {
1185 Client *c;
1186 Window trans;
1188 c = emallocz(sizeof(Client));
1189 c->tag = True;
1190 c->win = w;
1191 c->x = wa->x;
1192 c->y = wa->y;
1193 c->w = wa->width;
1194 c->h = wa->height;
1195 if(c->w == sw && c->h == sh) {
1196 c->border = 0;
1197 c->x = sx;
1198 c->y = sy;
1199 } else {
1200 c->border = BORDERPX;
1201 if(c->x + c->w + 2 * c->border > wax + waw)
1202 c->x = wax + waw - c->w - 2 * c->border;
1203 if(c->y + c->h + 2 * c->border > way + wah)
1204 c->y = way + wah - c->h - 2 * c->border;
1205 if(c->x < wax)
1206 c->x = wax;
1207 if(c->y < way)
1208 c->y = way;
1210 updatesizehints(c);
1211 XSelectInput(dpy, c->win,
1212 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
1213 XGetTransientForHint(dpy, c->win, &trans);
1214 grabbuttons(c, False);
1215 XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
1216 updatetitle(c);
1217 settag(c, getclient(trans));
1218 if(!c->ispopup)
1219 c->ispopup = trans || c->isfixed;
1220 if(clients)
1221 clients->prev = c;
1222 c->next = clients;
1223 c->snext = stack;
1224 stack = clients = c;
1225 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
1226 XMapWindow(dpy, c->win);
1227 setclientstate(c, NormalState);
1228 if(isvisible(c))
1229 focus(c);
1230 arrange();
1233 void
1234 resize(Client *c, Bool sizehints) {
1235 float actual, dx, dy, max, min;
1236 XWindowChanges wc;
1238 if(c->w <= 0 || c->h <= 0)
1239 return;
1240 if(sizehints) {
1241 if(c->minw && c->w < c->minw)
1242 c->w = c->minw;
1243 if(c->minh && c->h < c->minh)
1244 c->h = c->minh;
1245 if(c->maxw && c->w > c->maxw)
1246 c->w = c->maxw;
1247 if(c->maxh && c->h > c->maxh)
1248 c->h = c->maxh;
1249 /* inspired by algorithm from fluxbox */
1250 if(c->minay > 0 && c->maxay && (c->h - c->baseh) > 0) {
1251 dx = (float)(c->w - c->basew);
1252 dy = (float)(c->h - c->baseh);
1253 min = (float)(c->minax) / (float)(c->minay);
1254 max = (float)(c->maxax) / (float)(c->maxay);
1255 actual = dx / dy;
1256 if(max > 0 && min > 0 && actual > 0) {
1257 if(actual < min) {
1258 dy = (dx * min + dy) / (min * min + 1);
1259 dx = dy * min;
1260 c->w = (int)dx + c->basew;
1261 c->h = (int)dy + c->baseh;
1263 else if(actual > max) {
1264 dy = (dx * min + dy) / (max * max + 1);
1265 dx = dy * min;
1266 c->w = (int)dx + c->basew;
1267 c->h = (int)dy + c->baseh;
1271 if(c->incw)
1272 c->w -= (c->w - c->basew) % c->incw;
1273 if(c->inch)
1274 c->h -= (c->h - c->baseh) % c->inch;
1276 if(c->w == sw && c->h == sh)
1277 c->border = 0;
1278 else
1279 c->border = BORDERPX;
1280 /* offscreen appearance fixes */
1281 if(c->x > sw)
1282 c->x = sw - c->w - 2 * c->border;
1283 if(c->y > sh)
1284 c->y = sh - c->h - 2 * c->border;
1285 if(c->x + c->w + 2 * c->border < sx)
1286 c->x = sx;
1287 if(c->y + c->h + 2 * c->border < sy)
1288 c->y = sy;
1289 wc.x = c->x;
1290 wc.y = c->y;
1291 wc.width = c->w;
1292 wc.height = c->h;
1293 wc.border_width = c->border;
1294 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
1295 configure(c);
1296 XSync(dpy, False);
1299 void
1300 updatesizehints(Client *c) {
1301 long msize;
1302 XSizeHints size;
1304 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
1305 size.flags = PSize;
1306 c->flags = size.flags;
1307 if(c->flags & PBaseSize) {
1308 c->basew = size.base_width;
1309 c->baseh = size.base_height;
1310 } else {
1311 c->basew = c->baseh = 0;
1313 if(c->flags & PResizeInc) {
1314 c->incw = size.width_inc;
1315 c->inch = size.height_inc;
1316 } else {
1317 c->incw = c->inch = 0;
1319 if(c->flags & PMaxSize) {
1320 c->maxw = size.max_width;
1321 c->maxh = size.max_height;
1322 } else {
1323 c->maxw = c->maxh = 0;
1325 if(c->flags & PMinSize) {
1326 c->minw = size.min_width;
1327 c->minh = size.min_height;
1328 } else {
1329 c->minw = c->minh = 0;
1331 if(c->flags & PAspect) {
1332 c->minax = size.min_aspect.x;
1333 c->minay = size.min_aspect.y;
1334 c->maxax = size.max_aspect.x;
1335 c->maxay = size.max_aspect.y;
1336 } else {
1337 c->minax = c->minay = c->maxax = c->maxay = 0;
1339 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
1340 c->maxw == c->minw && c->maxh == c->minh);
1343 void
1344 updatetitle(Client *c) {
1345 if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name)) {
1346 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
1351 Bool
1352 gettextprop(Window w, Atom atom, char* text, unsigned int size) {
1353 char **list = NULL;
1354 int n;
1355 XTextProperty name;
1357 if (!text || size == 0) {
1358 return False;
1360 text[0] = '\0';
1361 XGetTextProperty(dpy, w, &name, atom);
1362 if(!name.nitems)
1363 return False;
1364 if(name.encoding == XA_STRING)
1365 strncpy(text, (char *)name.value, size - 1);
1366 else {
1367 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
1368 strncpy(text, *list, size - 1);
1369 XFreeStringList(list);
1372 text[size - 1] = '\0';
1373 XFree(name.value);
1374 return True;
1377 void
1378 unmanage(Client *c) {
1379 Client *nc;
1381 /* The server grab construct avoids race conditions. */
1382 XGrabServer(dpy);
1383 XSetErrorHandler(xerrordummy);
1384 detach(c);
1385 detachstack(c);
1386 if(sel == c) {
1387 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
1388 focus(nc);
1390 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1391 setclientstate(c, WithdrawnState);
1392 free(c);
1393 XSync(dpy, False);
1394 XSetErrorHandler(xerror);
1395 XUngrabServer(dpy);
1396 arrange();
1399 /* from main.c */
1401 void
1402 cleanup(void) {
1403 while(stack) {
1404 resize(stack, True);
1405 unmanage(stack);
1407 if(dc.font.set)
1408 XFreeFontSet(dpy, dc.font.set);
1409 else
1410 XFreeFont(dpy, dc.font.xfont);
1411 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1412 XFreePixmap(dpy, dc.drawable);
1413 XFreeGC(dpy, dc.gc);
1414 XDestroyWindow(dpy, barwin);
1415 XFreeCursor(dpy, cursor[CurNormal]);
1416 XFreeCursor(dpy, cursor[CurResize]);
1417 XFreeCursor(dpy, cursor[CurMove]);
1418 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
1419 XSync(dpy, False);
1422 void
1423 scan(void) {
1424 unsigned int i, num;
1425 Window *wins, d1, d2;
1426 XWindowAttributes wa;
1428 wins = NULL;
1429 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1430 for(i = 0; i < num; i++) {
1431 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1432 continue;
1433 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1434 continue;
1435 if(wa.map_state == IsViewable)
1436 manage(wins[i], &wa);
1439 if(wins)
1440 XFree(wins);
1443 void
1444 setup(void) {
1445 int i, j;
1446 unsigned int mask;
1447 Window w;
1448 XModifierKeymap *modmap;
1449 XSetWindowAttributes wa;
1451 screen = DefaultScreen(dpy);
1452 root = RootWindow(dpy, screen);
1454 /* init atoms */
1455 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1456 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1457 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1458 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1459 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1460 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1461 PropModeReplace, (unsigned char *) netatom, NetLast);
1462 /* init cursors */
1463 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1464 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1465 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1466 /* init modifier map */
1467 numlockmask = 0;
1468 modmap = XGetModifierMapping(dpy);
1469 for (i = 0; i < 8; i++) {
1470 for (j = 0; j < modmap->max_keypermod; j++) {
1471 if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
1472 numlockmask = (1 << i);
1475 XFreeModifiermap(modmap);
1476 /* select for events */
1477 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
1478 | EnterWindowMask | LeaveWindowMask | PropertyChangeMask;
1479 wa.cursor = cursor[CurNormal];
1480 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
1481 grabkeys();
1482 seltag = False;
1483 viewfloats = False;
1484 /* style */
1485 dc.norm[ColBG] = getcolor(NORMBGCOLOR);
1486 dc.norm[ColFG] = getcolor(NORMFGCOLOR);
1487 dc.sel[ColBG] = getcolor(SELBGCOLOR);
1488 dc.sel[ColFG] = getcolor(SELFGCOLOR);
1489 setfont(FONT);
1490 /* geometry */
1491 sx = sy = 0;
1492 sw = DisplayWidth(dpy, screen);
1493 sh = DisplayHeight(dpy, screen);
1494 nmaster = NMASTER;
1495 arrange1 = DEFMODE;
1496 arrange2 = DEFMODE;
1497 /* bar */
1498 dc.h = bh = dc.font.height + 2;
1499 wa.override_redirect = 1;
1500 wa.background_pixmap = ParentRelative;
1501 wa.event_mask = ButtonPressMask | ExposureMask;
1502 barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0,
1503 DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
1504 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
1505 XDefineCursor(dpy, barwin, cursor[CurNormal]);
1506 XMapRaised(dpy, barwin);
1507 /* windowarea */
1508 wax = sx;
1509 way = sy + bh;
1510 wah = sh - bh;
1511 waw = sw;
1512 /* pixmap for everything */
1513 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
1514 dc.gc = XCreateGC(dpy, root, 0, 0);
1515 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1516 /* multihead support */
1517 selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
1519 updatestatus();
1522 /*
1523 * Startup Error handler to check if another window manager
1524 * is already running.
1525 */
1526 int
1527 xerrorstart(Display *dsply, XErrorEvent *ee) {
1528 otherwm = True;
1529 return -1;
1532 void
1533 sendevent(Window w, Atom a, long value) {
1534 XEvent e;
1536 e.type = ClientMessage;
1537 e.xclient.window = w;
1538 e.xclient.message_type = a;
1539 e.xclient.format = 32;
1540 e.xclient.data.l[0] = value;
1541 e.xclient.data.l[1] = CurrentTime;
1542 XSendEvent(dpy, w, False, NoEventMask, &e);
1543 XSync(dpy, False);
1546 void
1547 quit() {
1548 running = False;
1551 /* There's no way to check accesses to destroyed windows, thus those cases are
1552 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1553 * default error handler, which may call exit.
1554 */
1555 int
1556 xerror(Display *dpy, XErrorEvent *ee) {
1557 if(ee->error_code == BadWindow
1558 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
1559 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
1560 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
1561 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
1562 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
1563 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
1564 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
1565 return 0;
1566 fprintf(stderr, "aewl: fatal error: request code=%d, error code=%d\n",
1567 ee->request_code, ee->error_code);
1568 return xerrorxlib(dpy, ee); /* may call exit */
1571 void
1572 checkotherwm(void) {
1573 otherwm = False;
1574 xerrorxlib = XSetErrorHandler(xerrorstart);
1575 /* this causes an error if some other window manager is running */
1576 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
1577 XSync(dpy, False);
1578 if(otherwm) {
1579 die("aewl: another window manager is already running\n");
1582 XSetErrorHandler(xerror);
1583 XSync(dpy, False);
1586 void
1587 run(void) {
1588 XEvent ev;
1590 XSync(dpy, False);
1591 /* main event loop */
1592 while(running && !XNextEvent(dpy, &ev)) {
1593 if(handler[ev.type]) {
1594 (handler[ev.type])(&ev); /* call handler */
1599 void
1600 updatestatus(void) {
1601 if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext))) {
1602 strcpy(stext, "aewl-"VERSION);
1604 drawbar();
1607 int
1608 main(int argc, char *argv[]) {
1610 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
1611 fputs("aewl-"VERSION", Copyright 2008 markus schnalke <meillo@marmaro.de>\n", stdout);
1612 fputs("forked off dwm-3.4, (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout);
1613 exit(EXIT_SUCCESS);
1614 } else if(argc != 1) {
1615 die("usage: aewl [-v]\n");
1617 setlocale(LC_CTYPE, "");
1618 dpy = XOpenDisplay(0);
1619 if(!dpy) {
1620 die("aewl: cannot open display\n");
1623 checkotherwm();
1624 setup();
1625 scan();
1626 run();
1627 cleanup();
1629 XCloseDisplay(dpy);
1630 return 0;