JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Partly un-globalize workspace geometry.
[spectrwm.git] / scrotwm.c
1 /* $scrotwm$ */
2 /*
3  * Copyright (c) 2009 Marco Peereboom <marco@peereboom.us>
4  * Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 /*
19  * Much code and ideas taken from dwm under the following license:
20  * MIT/X Consortium License
21  * 
22  * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
23  * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
24  * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
25  * 2007 Premysl Hruby <dfenze at gmail dot com>
26  * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
27  * 2007 Christof Musik <christof at sendfax dot de>
28  * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
29  * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
30  * 2008 Martin Hurton <martin dot hurton at gmail dot com>
31  * 
32  * Permission is hereby granted, free of charge, to any person obtaining a
33  * copy of this software and associated documentation files (the "Software"),
34  * to deal in the Software without restriction, including without limitation
35  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
36  * and/or sell copies of the Software, and to permit persons to whom the
37  * Software is furnished to do so, subject to the following conditions:
38  * 
39  * The above copyright notice and this permission notice shall be included in
40  * all copies or substantial portions of the Software.
41  * 
42  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
45  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48  * DEALINGS IN THE SOFTWARE.
49  */
50
51 #define SWM_VERSION     "0.5"
52
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <err.h>
56 #include <locale.h>
57 #include <unistd.h>
58 #include <time.h>
59 #include <signal.h>
60 #include <string.h>
61 #include <util.h>
62 #include <pwd.h>
63
64 #include <sys/types.h>
65 #include <sys/stat.h>
66 #include <sys/wait.h>
67 #include <sys/queue.h>
68 #include <sys/param.h>
69
70 #include <X11/cursorfont.h>
71 #include <X11/keysym.h>
72 #include <X11/Xatom.h>
73 #include <X11/Xlib.h>
74 #include <X11/Xproto.h>
75 #include <X11/Xutil.h>
76
77 /* #define SWM_DEBUG */
78 #ifdef SWM_DEBUG
79 #define DPRINTF(x...)           do { if (swm_debug) fprintf(stderr, x); } while(0)
80 #define DNPRINTF(n,x...)        do { if (swm_debug & n) fprintf(stderr, x); } while(0)
81 #define SWM_D_MISC              0x0001
82 #define SWM_D_EVENT             0x0002
83 #define SWM_D_WS                0x0004
84 #define SWM_D_FOCUS             0x0008
85 #define SWM_D_MOVE              0x0010
86
87 u_int32_t               swm_debug = 0
88                             | SWM_D_MISC
89                             | SWM_D_EVENT
90                             | SWM_D_WS
91                             | SWM_D_FOCUS
92                             | SWM_D_MOVE
93                             ;
94 #else
95 #define DPRINTF(x...)
96 #define DNPRINTF(n,x...)
97 #endif
98
99 #define LENGTH(x)               (sizeof x / sizeof x[0])
100 #define MODKEY                  Mod1Mask
101 #define CLEANMASK(mask)         (mask & ~(numlockmask | LockMask))
102
103 int                     (*xerrorxlib)(Display *, XErrorEvent *);
104 int                     other_wm;
105 int                     screen;
106 int                     running = 1;
107 int                     ignore_enter = 0;
108 unsigned int            numlockmask = 0;
109 unsigned long           color_focus = 0xff0000; /* XXX should this be per ws? */
110 unsigned long           color_unfocus = 0x888888;
111 Display                 *display;
112 Window                  root;
113
114 /* status bar */
115 int                     bar_enabled = 1;
116 int                     bar_height = 0;
117 unsigned long           bar_border = 0x008080;
118 unsigned long           bar_color = 0x000000;
119 unsigned long           bar_font_color = 0xa0a0a0;
120 Window                  bar_window;
121 GC                      bar_gc;
122 XGCValues               bar_gcv;
123 XFontStruct             *bar_fs;
124 char                    bar_text[128];
125 char                    *bar_fonts[] = {
126                             "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
127                             "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
128                             NULL
129 };
130
131 /* terminal + args */
132 char                            *spawn_term[] = { "xterm", NULL };
133 char                            *spawn_menu[] = { "dmenu_run", NULL };
134 char                            *spawn_scrotwm[] = { "scrotwm", NULL };
135
136 /* layout manager data */
137 struct swm_geometry {
138         int                     x;
139         int                     y;
140         int                     w;
141         int                     h;
142 };
143
144 void    stack(void);
145 void    vertical_init(void);
146 void    vertical_stack(struct swm_geometry *);
147 void    horizontal_init(void);
148 void    horizontal_stack(struct swm_geometry *);
149 void    max_init(void);
150 void    max_stack(struct swm_geometry *);
151
152 struct layout {
153         void                    (*l_init)(void);        /* init/reset */
154         void                    (*l_stack)(struct swm_geometry *);
155 } layouts[] =  {
156         /* init                 stack */
157         { vertical_init,        vertical_stack},
158         { horizontal_init,      horizontal_stack},
159         /* XXX not working yet
160          * { max_init,          max_stack},
161          */
162         { NULL,                 NULL},
163 };
164
165 struct ws_win {
166         TAILQ_ENTRY(ws_win)     entry;
167         Window                  id;
168         struct swm_geometry     g;
169         int                     floating;
170         int                     transient;
171         XWindowAttributes       wa;
172         XSizeHints              sh;
173 };
174
175 TAILQ_HEAD(ws_win_list, ws_win);
176
177 /* define work spaces */
178 #define SWM_WS_MAX              (10)
179 struct workspace {
180         int                     visible;        /* workspace visible */
181         int                     restack;        /* restack on switch */
182         struct swm_geometry     g;
183         struct layout           *cur_layout;    /* current layout handlers */
184         struct ws_win           *focus;         /* which win has focus */
185         struct ws_win_list      winlist;        /* list of windows in ws */
186 } ws[SWM_WS_MAX];
187 int                     current_ws = 0;
188
189 /* args to functions */
190 union arg {
191         int                     id;
192 #define SWM_ARG_ID_FOCUSNEXT    (0)
193 #define SWM_ARG_ID_FOCUSPREV    (1)
194 #define SWM_ARG_ID_FOCUSMAIN    (2)
195 #define SWM_ARG_ID_SWAPNEXT     (3)
196 #define SWM_ARG_ID_SWAPPREV     (4)
197 #define SWM_ARG_ID_SWAPMAIN     (5)
198         char                    **argv;
199 };
200
201 /* conf file stuff */
202 #define SWM_CONF_WS     "\n= \t"
203 #define SWM_CONF_FILE   "scrotwm.conf"
204 int
205 conf_load(char *filename)
206 {
207         FILE                    *config;
208         char                    *line, *cp, *var, *val;
209         size_t                   len, lineno = 0;
210
211         DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
212
213         if (filename == NULL)
214                 return (1);
215
216         if ((config = fopen(filename, "r")) == NULL)
217                 return (1);
218
219         for (;;) {
220                 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
221                         if (feof(config))
222                                 break;
223                 cp = line;
224                 cp += (long)strspn(cp, SWM_CONF_WS);
225                 if (cp[0] == '\0') {
226                         /* empty line */
227                         free(line);
228                         continue;
229                 }
230                 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
231                         break;
232                 cp += (long)strspn(cp, SWM_CONF_WS);
233                 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
234                         break;
235
236                 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
237                 switch (var[0]) {
238                 case 'b':
239                         if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
240                                 bar_enabled = atoi(val);
241                         else if (!strncmp(var, "bar_border",
242                             strlen("bar_border")))
243                                 bar_border = strtol(val, NULL, 16);
244                         else if (!strncmp(var, "bar_color",
245                             strlen("bar_color")))
246                                 bar_color = strtol(val, NULL, 16);
247                         else if (!strncmp(var, "bar_font_color",
248                             strlen("bar_font_color")))
249                                 bar_font_color = strtol(val, NULL, 16);
250                         else if (!strncmp(var, "bar_font", strlen("bar_font")))
251                                 asprintf(&bar_fonts[0], "%s", val);
252                         else
253                                 goto bad;
254                         break;
255
256                 case 'c':
257                         if (!strncmp(var, "color_focus", strlen("color_focus")))
258                                 color_focus = strtol(val, NULL, 16);
259                         else if (!strncmp(var, "color_unfocus",
260                             strlen("color_unfocus")))
261                                 color_unfocus = strtol(val, NULL, 16);
262                         else
263                                 goto bad;
264                         break;
265
266                 case 's':
267                         if (!strncmp(var, "spawn_term", strlen("spawn_term")))
268                                 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
269                         break;
270                 default:
271                         goto bad;
272                 }
273                 free(line);
274         }
275
276         fclose(config);
277         return (0);
278 bad:
279         errx(1, "invalid conf file entry: %s=%s", var, val);
280 }
281
282 void
283 bar_print(void)
284 {
285         time_t                  tmt;
286         struct tm               tm;
287
288         if (bar_enabled == 0)
289                 return;
290
291         /* clear old text */
292         XSetForeground(display, bar_gc, bar_color);
293         XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
294             strlen(bar_text));
295
296         /* draw new text */
297         time(&tmt);
298         localtime_r(&tmt, &tm);
299         strftime(bar_text, sizeof bar_text, "%a %b %d %R %Z %Y", &tm);
300         XSetForeground(display, bar_gc, bar_font_color);
301         XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
302             strlen(bar_text));
303         XSync(display, False);
304
305         alarm(60);
306 }
307
308 void
309 bar_signal(int sig)
310 {
311         /* XXX yeah yeah byte me */
312         bar_print();
313 }
314
315 void
316 bar_toggle(union arg *args)
317 {
318         int i;
319
320         DNPRINTF(SWM_D_MISC, "bar_toggle\n");
321
322         if (bar_enabled) {
323                 bar_enabled = 0;
324                 XUnmapWindow(display, bar_window);
325         } else {
326                 bar_enabled = 1;
327                 XMapWindow(display, bar_window);
328         }
329         XSync(display, False);
330         for (i = 0; i < SWM_WS_MAX; i++)
331                 ws[i].restack = 1;
332
333         stack();
334         bar_print(); /* must be after stack */
335 }
336
337 void
338 bar_setup(void)
339 {
340         int                     i;
341
342         for (i = 0; bar_fonts[i] != NULL; i++) {
343                 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
344                 if (bar_fs)
345                         break;
346         }
347         if (bar_fonts[i] == NULL)
348                         errx(1, "couldn't load font");
349         bar_height = bar_fs->ascent + bar_fs->descent + 3;
350
351         bar_window = XCreateSimpleWindow(display, root, 0, 0,
352             ws[current_ws].g.w, bar_height - 2, 1, bar_border, bar_color);
353         bar_gc = XCreateGC(display, bar_window, 0, &bar_gcv);
354         XSetFont(display, bar_gc, bar_fs->fid);
355         XSelectInput(display, bar_window, VisibilityChangeMask);
356         if (bar_enabled) {
357                 XMapWindow(display, bar_window);
358         }
359         DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %d\n", (int)bar_window);
360
361         if (signal(SIGALRM, bar_signal) == SIG_ERR)
362                 err(1, "could not install bar_signal");
363         bar_print();
364 }
365
366 int
367 count_win(int wsid, int count_transient)
368 {
369         struct ws_win           *win;
370         int                     count = 0;
371
372         TAILQ_FOREACH (win, &ws[wsid].winlist, entry) {
373                 if (count_transient == 0 && win->transient)
374                         continue;
375                 count++;
376         }
377         DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
378
379         return (count);
380 }
381 void
382 quit(union arg *args)
383 {
384         DNPRINTF(SWM_D_MISC, "quit\n");
385         running = 0;
386 }
387
388
389 void
390 restart(union arg *args)
391 {
392         XCloseDisplay(display);
393         execvp(args->argv[0], args->argv);
394         fprintf(stderr, "execvp failed\n");
395         perror(" failed");
396         quit(NULL);
397 }
398
399 void
400 spawn(union arg *args)
401 {
402         DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
403         /*
404          * The double-fork construct avoids zombie processes and keeps the code
405          * clean from stupid signal handlers.
406          */
407         if(fork() == 0) {
408                 if(fork() == 0) {
409                         if(display)
410                                 close(ConnectionNumber(display));
411                         setsid();
412                         execvp(args->argv[0], args->argv);
413                         fprintf(stderr, "execvp failed\n");
414                         perror(" failed");
415                 }
416                 exit(0);
417         }
418         wait(0);
419 }
420
421 void
422 focus_win(struct ws_win *win)
423 {
424         DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
425         XSetWindowBorder(display, win->id, color_focus);
426         XSetInputFocus(display, win->id, RevertToPointerRoot, CurrentTime);
427         ws[current_ws].focus = win;
428 }
429
430 void
431 unfocus_win(struct ws_win *win)
432 {
433         DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
434         XSetWindowBorder(display, win->id, color_unfocus);
435         if (ws[current_ws].focus == win)
436                 ws[current_ws].focus = NULL;
437 }
438
439 void
440 switchws(union arg *args)
441 {
442         int                     wsid = args->id;
443         struct ws_win           *win;
444
445         DNPRINTF(SWM_D_WS, "switchws: %d\n", wsid + 1);
446
447         if (wsid == current_ws)
448                 return;
449
450         /* map new window first to prevent ugly blinking */
451         TAILQ_FOREACH (win, &ws[wsid].winlist, entry)
452                 XMapRaised(display, win->id);
453         ws[wsid].visible = 1;
454
455         TAILQ_FOREACH (win, &ws[current_ws].winlist, entry)
456                 XUnmapWindow(display, win->id);
457         ws[current_ws].visible = 0;
458
459         current_ws = wsid;
460
461         ignore_enter = 1;
462         if (ws[wsid].restack) {
463                 stack();
464                 bar_print();
465         } else {
466                 if (ws[wsid].focus != NULL)
467                         focus_win(ws[wsid].focus);
468                 XSync(display, False);
469         }
470 }
471
472 void
473 swapwin(union arg *args)
474 {
475         struct ws_win           *target;
476
477         DNPRINTF(SWM_D_MOVE, "swapwin: id %d\n", args->id);
478         if (ws[current_ws].focus == NULL)
479                 return;
480
481         switch (args->id) {
482         case SWM_ARG_ID_SWAPPREV:
483                 target = TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
484                 TAILQ_REMOVE(&ws[current_ws].winlist,
485                     ws[current_ws].focus, entry);
486                 if (target == NULL)
487                         TAILQ_INSERT_TAIL(&ws[current_ws].winlist,
488                             ws[current_ws].focus, entry);
489                 else
490                         TAILQ_INSERT_BEFORE(target, ws[current_ws].focus,
491                             entry);
492                 break;
493         case SWM_ARG_ID_SWAPNEXT: 
494                 target = TAILQ_NEXT(ws[current_ws].focus, entry);
495                 TAILQ_REMOVE(&ws[current_ws].winlist,
496                     ws[current_ws].focus, entry);
497                 if (target == NULL)
498                         TAILQ_INSERT_HEAD(&ws[current_ws].winlist,
499                             ws[current_ws].focus, entry);
500                 else
501                         TAILQ_INSERT_AFTER(&ws[current_ws].winlist, target,
502                             ws[current_ws].focus, entry);
503                 break;
504         case SWM_ARG_ID_SWAPMAIN:
505                 target = TAILQ_FIRST(&ws[current_ws].winlist);
506                 if (target == ws[current_ws].focus)
507                         return;
508                 TAILQ_REMOVE(&ws[current_ws].winlist, target, entry);
509                 TAILQ_INSERT_BEFORE(ws[current_ws].focus, target, entry);
510                 TAILQ_REMOVE(&ws[current_ws].winlist,
511                     ws[current_ws].focus, entry);
512                 TAILQ_INSERT_HEAD(&ws[current_ws].winlist,
513                     ws[current_ws].focus, entry);
514                 break;
515         default:
516                 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
517                 return;
518         }
519
520         ignore_enter = 2;
521         stack();
522 }
523
524 void
525 focus(union arg *args)
526 {
527         struct ws_win           *winfocus, *winlostfocus;
528
529         DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
530         if (ws[current_ws].focus == NULL)
531                 return;
532
533         winlostfocus = ws[current_ws].focus;
534
535         switch (args->id) {
536         case SWM_ARG_ID_FOCUSPREV:
537                 winfocus = TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
538                 if (winfocus == NULL)
539                         winfocus =
540                             TAILQ_LAST(&ws[current_ws].winlist, ws_win_list);
541                 break;
542
543         case SWM_ARG_ID_FOCUSNEXT:
544                 winfocus = TAILQ_NEXT(ws[current_ws].focus, entry);
545                 if (winfocus == NULL)
546                         winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
547                 break;
548
549         case SWM_ARG_ID_FOCUSMAIN:
550                 winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
551                 break;
552
553         default:
554                 return;
555         }
556
557         if (winfocus == winlostfocus)
558                 return;
559
560         unfocus_win(winlostfocus);
561         focus_win(winfocus);
562         XSync(display, False);
563 }
564
565 void
566 cycle_layout(union arg *args)
567 {
568         DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", current_ws);
569
570         ws[current_ws].cur_layout++;
571         if (ws[current_ws].cur_layout->l_stack == NULL)
572                 ws[current_ws].cur_layout = &layouts[0];
573         stack();
574 }
575
576 void
577 stack(void) {
578         struct swm_geometry g;
579         DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws);
580
581         /* start with workspace geometry, adjust for bar */
582         g = ws[current_ws].g;
583         if (bar_enabled) {
584                 g.y += bar_height;
585                 g.h -= bar_height;
586         } 
587
588         ws[current_ws].restack = 0;
589         ws[current_ws].cur_layout->l_stack(&g);
590         XSync(display, False);
591 }
592
593 void
594 stack_floater(struct ws_win *win)
595 {
596         unsigned int            mask;
597         XWindowChanges          wc;
598
599         bzero(&wc, sizeof wc);
600         wc.border_width = 1;
601         mask = CWX | CWY | CWBorderWidth;
602
603         /* use obsolete width height */
604         if (win->sh.flags & USPosition) {
605                 win->g.w = wc.width = win->sh.width;
606                 win->g.h = wc.height = win->sh.height;
607                 mask |= CWWidth | CWHeight;
608         }
609
610         /* try min max */
611         if (win->sh.flags & PMinSize) {
612                 /* some hints are retarded */
613                 if (win->sh.min_width < ws[current_ws].g.w / 10)
614                         win->sh.min_width = ws[current_ws].g.w / 3;
615                 if (win->sh.min_height < ws[current_ws].g.h / 10)
616                         win->wa.height = ws[current_ws].g.h / 3;
617
618                 win->g.w = wc.width = win->sh.min_width * 2;
619                 win->g.h = wc.height = win->sh.min_height * 2;
620                 mask |= CWWidth | CWHeight;
621         }
622         if (win->sh.flags & PMaxSize) {
623                 /* potentially override min values */
624                 if (win->sh.max_width < ws[current_ws].g.w) {
625                         win->g.w = wc.width = win->sh.max_width;
626                         mask |= CWWidth;
627                 }
628                 if (win->sh.max_height < ws[current_ws].g.h) {
629                         win->g.h = wc.height = win->sh.max_height;
630                         mask |= CWHeight;
631                 }
632         }
633
634         /* make sure we don't clobber the screen */
635         if ((mask & CWWidth) && win->wa.width > ws[current_ws].g.w)
636                 win->wa.width = ws[current_ws].g.w - 4;
637         if ((mask & CWHeight) && win->wa.height > ws[current_ws].g.h)
638                 win->wa.height = ws[current_ws].g.h - 4;
639
640         /* supposed to be obsolete */
641         if (win->sh.flags & USPosition) {
642                 win->g.x = wc.x = win->sh.x;
643                 win->g.y = wc.y = win->sh.y;
644         } else {
645                 win->g.x = wc.x = (ws[current_ws].g.w - win->wa.width) / 2;
646                 win->g.y = wc.y = (ws[current_ws].g.h - win->wa.height) / 2;
647         }
648         DNPRINTF(SWM_D_EVENT, "stack_floater: win %d x %d y %d w %d h %d\n",
649             win, wc.x, wc.y, wc.width, wc.height);
650
651         XConfigureWindow(display, win->id, mask, &wc);
652 }
653
654 void
655 vertical_init(void)
656 {
657         DNPRINTF(SWM_D_EVENT, "vertical_init: workspace: %d\n", current_ws);
658 }
659
660 /* I know this sucks but it works well enough */
661 void
662 vertical_stack(struct swm_geometry *g) {
663         XWindowChanges          wc;
664         struct swm_geometry     gg = *g;
665         struct ws_win           wf, *win, *winfocus = &wf;
666         int                     i, hrh, winno;
667         unsigned int            mask;
668
669         DNPRINTF(SWM_D_EVENT, "vertical_stack: workspace: %d\n", current_ws);
670
671         winfocus->id = root;
672
673         winno = count_win(current_ws, 0);
674         if (winno == 0)
675                 return;
676
677         if (winno > 1)
678                 gg.w = g->w / 2;
679
680         if (winno > 2)
681                 hrh = g->h / (winno - 1);
682         else
683                 hrh = 0;
684
685         i = 0;
686         TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
687                 if (i == 1) {
688                         gg.x += gg.w + 2;
689                         gg.w -= 2;
690                 }
691                 if (i != 0 && hrh != 0) {
692                         /* correct the last window for lost pixels */
693                         if (win == TAILQ_LAST(&ws[current_ws].winlist,
694                             ws_win_list)) {
695                                 gg.h = hrh + (g->h - (i * hrh));
696                                 gg.y += hrh;
697                         } else {
698                                 gg.h = hrh - 2;
699                                 /* leave first right hand window at y = 0 */
700                                 if (i > 1)
701                                         gg.y += gg.h + 2;
702                         }
703                 }
704
705                 if (win->transient != 0 || win->floating != 0)
706                         stack_floater(win);
707                 else {
708                         bzero(&wc, sizeof wc);
709                         wc.border_width = 1;
710                         win->g.x = wc.x = gg.x;
711                         win->g.y = wc.y = gg.y;
712                         win->g.w = wc.width = gg.w;
713                         win->g.h = wc.height = gg.h;
714                         mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
715                         XConfigureWindow(display, win->id, mask, &wc);
716                 }
717
718                 if (win == ws[current_ws].focus)
719                         winfocus = win;
720                 else
721                         unfocus_win(win);
722                 XMapRaised(display, win->id);
723                 i++;
724         }
725
726         focus_win(winfocus); /* this has to be done outside of the loop */
727 }
728
729 void
730 horizontal_init(void)
731 {
732         DNPRINTF(SWM_D_EVENT, "horizontal_init: workspace: %d\n", current_ws);
733 }
734
735 void
736 horizontal_stack(struct swm_geometry *g) {
737         XWindowChanges          wc;
738         struct swm_geometry     gg = *g;
739         struct ws_win           wf, *win, *winfocus = &wf;
740         int                     i, hrw, winno;
741         unsigned int            mask;
742
743         DNPRINTF(SWM_D_EVENT, "horizontal_stack: workspace: %d\n", current_ws);
744
745         winfocus->id = root;
746
747         winno = count_win(current_ws, 0);
748         if (winno == 0)
749                 return;
750
751         if (winno > 1)
752                 gg.h = g->h / 2;
753
754         if (winno > 2)
755                 hrw = g->w / (winno - 1);
756         else
757                 hrw = 0;
758
759         i = 0;
760         TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
761                 if (i == 1) {
762                         gg.y += gg.h + 2;
763                         gg.h -= (2 - g->h % 2);
764                 }
765                 if (i != 0 && hrw != 0) {
766                         /* correct the last window for lost pixels */
767                         if (win == TAILQ_LAST(&ws[current_ws].winlist,
768                             ws_win_list)) {
769                                 gg.w = hrw + (g->w - (i * hrw));
770                                 gg.x += hrw;
771                         } else {
772                                 gg.w = hrw - 2;
773                                 /* leave first bottom window at x = 0 */
774                                 if (i > 1)
775                                         gg.x += gg.w + 2;
776                         }
777                 }
778
779                 if (win->transient != 0 || win->floating != 0)
780                         stack_floater(win);
781                 else {
782                         bzero(&wc, sizeof wc);
783                         wc.border_width = 1;
784                         win->g.x = wc.x = gg.x;
785                         win->g.y = wc.y = gg.y;
786                         win->g.w = wc.width = gg.w;
787                         win->g.h = wc.height = gg.h;
788                         mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
789                         XConfigureWindow(display, win->id, mask, &wc);
790                 }
791
792                 if (win == ws[current_ws].focus)
793                         winfocus = win;
794                 else
795                         unfocus_win(win);
796                 XMapRaised(display, win->id);
797                 i++;
798         }
799
800         focus_win(winfocus); /* this has to be done outside of the loop */
801 }
802
803 /* fullscreen view */
804 void
805 max_init(void)
806 {
807         DNPRINTF(SWM_D_EVENT, "max_init: workspace: %d\n", current_ws);
808 }
809
810 void
811 max_stack(struct swm_geometry *g) {
812         XWindowChanges          wc;
813         struct swm_geometry     gg = *g;
814         struct ws_win           wf, *win, *winfocus = &wf;
815         int                     i, winno;
816         unsigned int mask;
817
818         DNPRINTF(SWM_D_EVENT, "max_stack: workspace: %d\n", current_ws);
819
820         winfocus->id = root;
821
822         winno = count_win(current_ws, 0);
823         if (winno == 0)
824                 return;
825
826         TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
827                 if (i == 1 && win->transient == 0 && win->floating != 0) {
828                         gg.h -= 2;
829                         gg.w -= 2;
830
831                         winfocus = win;
832
833                         bzero(&wc, sizeof wc);
834                         wc.border_width = 1;
835                         win->g.x = wc.x = gg.x;
836                         win->g.y = wc.y = gg.y;
837                         win->g.w = wc.width = gg.w;
838                         win->g.h = wc.height = gg.h;
839                         mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
840                         XConfigureWindow(display, win->id, mask, &wc);
841
842                         XMapRaised(display, win->id);
843                 } else {
844                         /* hide all but the master window */
845                         XUnmapWindow(display, win->id);
846                 }
847                 i++;
848         }
849
850         focus_win(winfocus); /* this has to be done outside of the loop */
851 }
852
853 void
854 send_to_ws(union arg *args)
855 {
856         int                     wsid = args->id;
857         struct ws_win           *win = ws[current_ws].focus;
858
859         DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
860
861         XUnmapWindow(display, win->id);
862
863         /* find a window to focus */
864         ws[current_ws].focus = TAILQ_PREV(win, ws_win_list, entry);
865         if (ws[current_ws].focus == NULL)
866                 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
867         if (ws[current_ws].focus == win)
868                 ws[current_ws].focus = NULL;
869
870         TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
871
872         TAILQ_INSERT_TAIL(&ws[wsid].winlist, win, entry);
873         if (count_win(wsid, 1) == 1)
874                 ws[wsid].focus = win;
875         ws[wsid].restack = 1;
876
877         stack();
878 }
879
880 /* key definitions */
881 struct key {
882         unsigned int            mod;
883         KeySym                  keysym;
884         void                    (*func)(union arg *);
885         union arg               args;
886 } keys[] = {
887         /* modifier             key     function                argument */
888         /* XXX alt-c is temporary, until I figure out how to grab spacebar */
889         { MODKEY,               XK_c,           cycle_layout,   {0} }, 
890         { MODKEY,               XK_Return,      swapwin,        {.id = SWM_ARG_ID_SWAPMAIN} },
891         { MODKEY,               XK_j,           focus,          {.id = SWM_ARG_ID_FOCUSNEXT} },
892         { MODKEY,               XK_k,           focus,          {.id = SWM_ARG_ID_FOCUSPREV} },
893         { MODKEY | ShiftMask,   XK_j,           swapwin,        {.id = SWM_ARG_ID_SWAPNEXT} },
894         { MODKEY | ShiftMask,   XK_k,           swapwin,        {.id = SWM_ARG_ID_SWAPPREV} },
895         { MODKEY | ShiftMask,   XK_Return,      spawn,          {.argv = spawn_term} },
896         { MODKEY,               XK_p,           spawn,          {.argv = spawn_menu} },
897         { MODKEY | ShiftMask,   XK_q,           quit,           {0} },
898         { MODKEY,               XK_q,           restart,        {.argv = spawn_scrotwm } },
899         { MODKEY,               XK_m,           focus,          {.id = SWM_ARG_ID_FOCUSMAIN} },
900         { MODKEY,               XK_1,           switchws,       {.id = 0} },
901         { MODKEY,               XK_2,           switchws,       {.id = 1} },
902         { MODKEY,               XK_3,           switchws,       {.id = 2} },
903         { MODKEY,               XK_4,           switchws,       {.id = 3} },
904         { MODKEY,               XK_5,           switchws,       {.id = 4} },
905         { MODKEY,               XK_6,           switchws,       {.id = 5} },
906         { MODKEY,               XK_7,           switchws,       {.id = 6} },
907         { MODKEY,               XK_8,           switchws,       {.id = 7} },
908         { MODKEY,               XK_9,           switchws,       {.id = 8} },
909         { MODKEY,               XK_0,           switchws,       {.id = 9} },
910         { MODKEY | ShiftMask,   XK_1,           send_to_ws,     {.id = 0} },
911         { MODKEY | ShiftMask,   XK_2,           send_to_ws,     {.id = 1} },
912         { MODKEY | ShiftMask,   XK_3,           send_to_ws,     {.id = 2} },
913         { MODKEY | ShiftMask,   XK_4,           send_to_ws,     {.id = 3} },
914         { MODKEY | ShiftMask,   XK_5,           send_to_ws,     {.id = 4} },
915         { MODKEY | ShiftMask,   XK_6,           send_to_ws,     {.id = 5} },
916         { MODKEY | ShiftMask,   XK_7,           send_to_ws,     {.id = 6} },
917         { MODKEY | ShiftMask,   XK_8,           send_to_ws,     {.id = 7} },
918         { MODKEY | ShiftMask,   XK_9,           send_to_ws,     {.id = 8} },
919         { MODKEY | ShiftMask,   XK_0,           send_to_ws,     {.id = 9} },
920         { MODKEY,               XK_b,           bar_toggle,     {0} },
921         { MODKEY,               XK_Tab,         focus,          {.id = SWM_ARG_ID_FOCUSNEXT} },
922         { MODKEY | ShiftMask,   XK_Tab,         focus,          {.id = SWM_ARG_ID_FOCUSPREV} },
923 };
924
925 void
926 updatenumlockmask(void)
927 {
928         unsigned int            i, j;
929         XModifierKeymap         *modmap;
930
931         DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
932         numlockmask = 0;
933         modmap = XGetModifierMapping(display);
934         for (i = 0; i < 8; i++)
935                 for (j = 0; j < modmap->max_keypermod; j++)
936                         if (modmap->modifiermap[i * modmap->max_keypermod + j]
937                           == XKeysymToKeycode(display, XK_Num_Lock))
938                                 numlockmask = (1 << i);
939
940         XFreeModifiermap(modmap);
941 }
942
943 void
944 grabkeys(void)
945 {
946         unsigned int            i, j;
947         KeyCode                 code;
948         unsigned int            modifiers[] =
949             { 0, LockMask, numlockmask, numlockmask | LockMask };
950
951         DNPRINTF(SWM_D_MISC, "grabkeys\n");
952         updatenumlockmask();
953
954         XUngrabKey(display, AnyKey, AnyModifier, root);
955         for(i = 0; i < LENGTH(keys); i++) {
956                 if((code = XKeysymToKeycode(display, keys[i].keysym)))
957                         for(j = 0; j < LENGTH(modifiers); j++)
958                                 XGrabKey(display, code,
959                                     keys[i].mod | modifiers[j], root,
960                                     True, GrabModeAsync, GrabModeAsync);
961         }
962 }
963 void
964 expose(XEvent *e)
965 {
966         DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
967 }
968
969 void
970 keypress(XEvent *e)
971 {
972         unsigned int            i;
973         KeySym                  keysym;
974         XKeyEvent               *ev = &e->xkey;
975
976         DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
977
978         keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
979         for(i = 0; i < LENGTH(keys); i++)
980                 if(keysym == keys[i].keysym
981                    && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
982                    && keys[i].func)
983                         keys[i].func(&(keys[i].args));
984 }
985
986 void
987 buttonpress(XEvent *e)
988 {
989         XButtonPressedEvent     *ev = &e->xbutton;
990 #ifdef SWM_CLICKTOFOCUS
991         struct ws_win           *win;
992 #endif
993
994
995         DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
996
997         if (ev->window == root)
998                 return;
999         if (ev->window == ws[current_ws].focus->id)
1000                 return;
1001 #ifdef SWM_CLICKTOFOCUS
1002         TAILQ_FOREACH(win, &ws[current_ws].winlist, entry)
1003                 if (win->id == ev->window) {
1004                         /* focus in the clicked window */
1005                         XSetWindowBorder(display, ev->window, 0xff0000);
1006                         XSetWindowBorder(display,
1007                             ws[current_ws].focus->id, 0x888888);
1008                         XSetInputFocus(display, ev->window, RevertToPointerRoot,
1009                             CurrentTime);
1010                         ws[current_ws].focus = win;
1011                         XSync(display, False);
1012                         break;
1013         }
1014 #endif
1015 }
1016
1017 struct ws_win *
1018 manage_window(Window id)
1019 {
1020         struct ws_win           *win;
1021
1022         TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1023                 if (win->id == id)
1024                         return (win);   /* already being managed */
1025         }
1026
1027         if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1028                 errx(1, "calloc: failed to allocate memory for new window");
1029
1030         win->id = id;
1031         TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
1032
1033         XSelectInput(display, id, ButtonPressMask | EnterWindowMask |
1034             FocusChangeMask | ExposureMask);
1035
1036         return win;
1037 }
1038
1039 void
1040 configurerequest(XEvent *e)
1041 {
1042         XConfigureRequestEvent  *ev = &e->xconfigurerequest;
1043         Window                  trans;
1044         struct ws_win           *win;
1045
1046         DNPRINTF(SWM_D_EVENT, "configurerequest: window: %lu\n", ev->window);
1047
1048
1049         win = manage_window(ev->window);
1050         ws[current_ws].focus = win; /* make new win focused */
1051
1052         XGetTransientForHint(display, win->id, &trans);
1053         if (trans) {
1054                 win->transient = trans;
1055                 DNPRINTF(SWM_D_MISC, "configurerequest: win %u transient %u\n",
1056                     (unsigned)win->id, win->transient);
1057         }
1058         XGetWindowAttributes(display, win->id, &win->wa);
1059         XGetNormalHints(display, win->id, &win->sh);
1060 #if 0
1061         XClassHint ch = { 0 };
1062         if(XGetClassHint(display, win->id, &ch)) {
1063                 fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name);
1064                 if (!strcmp(ch.res_class, "Gvim") && !strcmp(ch.res_name, "gvim")) {
1065                         win->floating = 0;
1066                 }
1067                 if(ch.res_class)
1068                         XFree(ch.res_class);
1069                 if(ch.res_name)
1070                         XFree(ch.res_name);
1071         }
1072 #endif
1073         stack();
1074 }
1075
1076 void
1077 configurenotify(XEvent *e)
1078 {
1079
1080         DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1081             e->xconfigure.window);
1082 }
1083
1084 void
1085 destroynotify(XEvent *e)
1086 {
1087         struct ws_win           *win;
1088         XDestroyWindowEvent     *ev = &e->xdestroywindow;
1089
1090         DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1091
1092         TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1093                 if (ev->window == win->id) {
1094                         /* find a window to focus */
1095                         ws[current_ws].focus = TAILQ_PREV(win,
1096                             ws_win_list, entry);
1097                         if (ws[current_ws].focus == NULL)
1098                                 ws[current_ws].focus =
1099                                     TAILQ_FIRST(&ws[current_ws].winlist);
1100                         if (win == ws[current_ws].focus)
1101                                 ws[current_ws].focus = NULL;
1102         
1103                         TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
1104                         free(win);
1105                         break;
1106                 }
1107         }
1108
1109         stack();
1110 }
1111
1112 void
1113 enternotify(XEvent *e)
1114 {
1115         XCrossingEvent          *ev = &e->xcrossing;
1116         struct ws_win           *win;
1117
1118         DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1119
1120         if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
1121             ev->window != root)
1122                 return;
1123         if (ignore_enter) {
1124                 /* eat event(s) to prevent autofocus */
1125                 ignore_enter--;
1126                 return;
1127         }
1128         TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1129                 if (win->id == ev->window)
1130                         focus_win(win);
1131                 else
1132                         unfocus_win(win);
1133         }
1134 }
1135
1136 void
1137 focusin(XEvent *e)
1138 {
1139         XFocusChangeEvent       *ev = &e->xfocus;
1140
1141         DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", ev->window);
1142
1143         XSync(display, False); /* not sure this helps redrawing graphic apps */
1144
1145         if (ev->window == root)
1146                 return;
1147         /*
1148          * kill grab for now so that we can cut and paste , this screws up
1149          * click to focus
1150          */
1151         /*
1152         DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1153         XGrabButton(display, Button1, AnyModifier, ev->window, False,
1154             ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1155         */
1156 }
1157
1158 void
1159 mappingnotify(XEvent *e)
1160 {
1161         XMappingEvent           *ev = &e->xmapping;
1162
1163         DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1164
1165         XRefreshKeyboardMapping(ev);
1166         if(ev->request == MappingKeyboard)
1167                 grabkeys();
1168 }
1169
1170 void
1171 maprequest(XEvent *e)
1172 {
1173         DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1174             e->xmaprequest.window);
1175
1176         manage_window(e->xmaprequest.window);
1177         stack();
1178 }
1179
1180 void
1181 propertynotify(XEvent *e)
1182 {
1183         DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1184             e->xproperty.window);
1185 }
1186
1187 void
1188 unmapnotify(XEvent *e)
1189 {
1190         DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1191 }
1192
1193 void
1194 visibilitynotify(XEvent *e)
1195 {
1196         DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n", e->xvisibility.window);
1197
1198         if (e->xvisibility.window == bar_window &&
1199             e->xvisibility.state == VisibilityUnobscured)
1200                 bar_print();
1201 }
1202
1203 void                    (*handler[LASTEvent])(XEvent *) = {
1204                                 [Expose] = expose,
1205                                 [KeyPress] = keypress,
1206                                 [ButtonPress] = buttonpress,
1207                                 [ConfigureRequest] = configurerequest,
1208                                 [ConfigureNotify] = configurenotify,
1209                                 [DestroyNotify] = destroynotify,
1210                                 [EnterNotify] = enternotify,
1211                                 [FocusIn] = focusin,
1212                                 [MappingNotify] = mappingnotify,
1213                                 [MapRequest] = maprequest,
1214                                 [PropertyNotify] = propertynotify,
1215                                 [UnmapNotify] = unmapnotify,
1216                                 [VisibilityNotify] = visibilitynotify,
1217 };
1218
1219 int
1220 xerror_start(Display *d, XErrorEvent *ee)
1221 {
1222         other_wm = 1;
1223         return (-1);
1224 }
1225
1226 int
1227 xerror(Display *d, XErrorEvent *ee)
1228 {
1229         fprintf(stderr, "error: %p %p\n", display, ee);
1230
1231         return (-1);
1232 }
1233
1234 int
1235 active_wm(void)
1236 {
1237         other_wm = 0;
1238         xerrorxlib = XSetErrorHandler(xerror_start);
1239
1240         /* this causes an error if some other window manager is running */
1241         XSelectInput(display, DefaultRootWindow(display),
1242             SubstructureRedirectMask);
1243         XSync(display, False);
1244         if(other_wm)
1245                 return (1);
1246
1247         XSetErrorHandler(xerror);
1248         XSync(display, False);
1249         return (0);
1250 }
1251
1252 int
1253 main(int argc, char *argv[])
1254 {
1255         struct passwd           *pwd;
1256         char                    conf[PATH_MAX], *cfile = NULL;
1257         struct stat             sb;
1258         XEvent                  e;
1259         unsigned int            i, num;
1260         Window                  d1, d2, *wins = NULL;
1261         XWindowAttributes       wa;
1262
1263         fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
1264         if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
1265                 warnx("no locale support");
1266
1267         if(!(display = XOpenDisplay(0)))
1268                 errx(1, "can not open display");
1269
1270         if (active_wm())
1271                 errx(1, "other wm running");
1272
1273         screen = DefaultScreen(display);
1274         root = RootWindow(display, screen);
1275
1276         /* look for local and global conf file */
1277         pwd = getpwuid(getuid());
1278         if (pwd == NULL)
1279                 errx(1, "invalid user %d", getuid());
1280
1281         snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
1282         if (stat(conf, &sb) != -1) {
1283                 if (S_ISREG(sb.st_mode))
1284                         cfile = conf;
1285         } else {
1286                 /* try global conf file */
1287                 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
1288                 if (!stat(conf, &sb))
1289                         if (S_ISREG(sb.st_mode))
1290                                 cfile = conf;
1291         }
1292         if (cfile)
1293                 conf_load(cfile);
1294
1295         /* init all workspaces */
1296         for (i = 0; i < SWM_WS_MAX; i++) {
1297                 ws[i].visible = 0;
1298                 ws[i].restack = 1;
1299                 ws[i].focus = NULL;
1300                 ws[i].g.x = 0;
1301                 ws[i].g.y = 0;
1302                 ws[i].g.w = DisplayWidth(display, screen) - 2;
1303                 ws[i].g.h = DisplayHeight(display, screen) - 2;
1304                 TAILQ_INIT(&ws[i].winlist);
1305                 ws[i].cur_layout = &layouts[0];
1306         }
1307         /* make work space 1 active */
1308         ws[0].visible = 1;
1309
1310         /* grab existing windows */
1311         if (XQueryTree(display, root, &d1, &d2, &wins, &num)) {
1312                 for (i = 0; i < num; i++) {
1313                         if (!XGetWindowAttributes(display, wins[i], &wa)
1314                             || wa.override_redirect ||
1315                             XGetTransientForHint(display, wins[i], &d1))
1316                                 continue;
1317                         manage_window(wins[i]);
1318                 }
1319                 if(wins)
1320                         XFree(wins);
1321         }
1322         ws[0].focus = TAILQ_FIRST(&ws[0].winlist);
1323
1324         /* setup status bar */
1325         bar_setup();
1326
1327         XSelectInput(display, root, SubstructureRedirectMask |
1328             SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
1329             EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
1330             FocusChangeMask | PropertyChangeMask | ExposureMask);
1331
1332         grabkeys();
1333         stack();
1334
1335         while (running) {
1336                 XNextEvent(display, &e);
1337                 if (handler[e.type])
1338                         handler[e.type](&e);
1339         }
1340
1341         XCloseDisplay(display);
1342
1343         return (0);
1344 }