JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Sprinkle debug
[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
59 #include <sys/types.h>
60 #include <sys/wait.h>
61 #include <sys/queue.h>
62
63 #include <X11/cursorfont.h>
64 #include <X11/keysym.h>
65 #include <X11/Xatom.h>
66 #include <X11/Xlib.h>
67 #include <X11/Xproto.h>
68 #include <X11/Xutil.h>
69
70 #define SWM_DEBUG
71 #ifdef SWM_DEBUG
72 #define DPRINTF(x...)           do { if (swm_debug) fprintf(stderr, x); } while(0)
73 #define DNPRINTF(n,x...)        do { if (swm_debug & n) fprintf(stderr, x); } while(0)
74 #define SWM_D_EVENT             0x0001
75 #define SWM_D_WS                0x0002
76 #define SWM_D_FOCUS             0x0004
77 #define SWM_D_MISC              0x0008
78
79 uint32_t                swm_debug = 0
80                             | SWM_D_EVENT
81                             | SWM_D_WS
82                             | SWM_D_FOCUS
83                             | SWM_D_MISC
84                             ;
85 #else
86 #define DPRINTF(x...)
87 #define DNPRINTF(n,x...)
88 #endif
89
90 #define LENGTH(x)               (sizeof x / sizeof x[0])
91 #define MODKEY                  Mod1Mask
92 #define CLEANMASK(mask)         (mask & ~(numlockmask | LockMask))
93
94 int                     (*xerrorxlib)(Display *, XErrorEvent *);
95 int                     other_wm;
96 int                     screen;
97 int                     width, height;
98 int                     running = 1;
99 int                     ignore_enter = 0;
100 unsigned int            numlockmask = 0;
101 unsigned long           col_focus = 0xff0000;   /* XXX should this be per ws? */
102 unsigned long           col_unfocus = 0x888888;
103 Display                 *display;
104 Window                  root;
105
106 struct ws_win {
107         TAILQ_ENTRY(ws_win)     entry;
108         Window                  id;
109         int                     x;
110         int                     y;
111         int                     width;
112         int                     height;
113 };
114
115 TAILQ_HEAD(ws_win_list, ws_win);
116
117 /* define work spaces */
118 #define SWM_WS_MAX              (10)
119 struct workspace {
120         int                      visible;       /* workspace visible */
121         struct ws_win           *focus;         /* which win has focus */
122         int                      winno;         /* total nr of windows */
123         struct ws_win_list       winlist;
124 } ws[SWM_WS_MAX];
125 int                     current_ws = 0;
126
127 /* args to functions */
128 union arg {
129         int             id;
130 #define SWM_ARG_ID_FOCUSNEXT    (0)
131 #define SWM_ARG_ID_FOCUSPREV    (1)
132 #define SWM_ARG_ID_FOCUSMAIN    (2)
133         char            **argv;
134 };
135
136 void
137 quit(union arg *args)
138 {
139         DNPRINTF(SWM_D_MISC, "quit\n");
140         running = 0;
141 }
142
143 void
144 spawn(union arg *args)
145 {
146         DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
147         /*
148          * The double-fork construct avoids zombie processes and keeps the code
149          * clean from stupid signal handlers.
150          */
151         if(fork() == 0) {
152                 if(fork() == 0) {
153                         if(display)
154                                 close(ConnectionNumber(display));
155                         setsid();
156                         execvp(args->argv[0], args->argv);
157                         fprintf(stderr, "execvp failed\n");
158                         perror(" failed");
159                 }
160                 exit(0);
161         }
162         wait(0);
163 }
164
165 void
166 focus_win(struct ws_win *win)
167 {
168         DNPRINTF(SWM_D_FOCUS, "focus_win: id: %d\n", win->id);
169         XSetWindowBorder(display, win->id, col_focus);
170         XSetInputFocus(display, win->id, RevertToPointerRoot, CurrentTime);
171         ws[current_ws].focus = win;
172 }
173
174 void
175 unfocus_win(struct ws_win *win)
176 {
177         DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %d\n", win->id);
178         XSetWindowBorder(display, win->id, col_unfocus);
179         if (ws[current_ws].focus == win)
180                 ws[current_ws].focus = NULL;
181 }
182
183 void
184 switchws(union arg *args)
185 {
186         int                     wsid = args->id;
187         struct ws_win           *win;
188         Window                  winfocus;
189
190         DNPRINTF(SWM_D_WS, "switchws: %d\n", wsid + 1);
191
192         if (wsid == current_ws)
193                 return;
194
195         /* map new window first to prevent ugly blinking */
196         TAILQ_FOREACH (win, &ws[wsid].winlist, entry)
197                 XMapWindow(display, win->id);
198         ws[wsid].visible = 1;
199
200         TAILQ_FOREACH (win, &ws[current_ws].winlist, entry)
201                 XUnmapWindow(display, win->id);
202         ws[current_ws].visible = 0;
203
204         current_ws = wsid;
205
206         ignore_enter = 1;
207         if (ws[wsid].focus != NULL)
208                 focus_win(ws[wsid].focus);
209         XSync(display, False);
210 }
211
212 void
213 focus(union arg *args)
214 {
215         struct ws_win           *winfocus, *winlostfocus;
216
217         DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
218         if (ws[current_ws].focus == NULL || ws[current_ws].winno == 0)
219                 return;
220
221         winlostfocus = ws[current_ws].focus;
222
223         switch (args->id) {
224         case SWM_ARG_ID_FOCUSPREV:
225                 if (ws[current_ws].focus ==
226                     TAILQ_FIRST(&ws[current_ws].winlist))
227                         ws[current_ws].focus =
228                             TAILQ_LAST(&ws[current_ws].winlist, ws_win_list);
229                 else
230                         ws[current_ws].focus =TAILQ_PREV(ws[current_ws].focus,
231                             ws_win_list, entry);
232                 break;
233
234         case SWM_ARG_ID_FOCUSNEXT:
235                 if (ws[current_ws].focus == TAILQ_LAST(&ws[current_ws].winlist,
236                     ws_win_list))
237                         ws[current_ws].focus =
238                             TAILQ_FIRST(&ws[current_ws].winlist);
239                 else
240                         ws[current_ws].focus =
241                             TAILQ_NEXT(ws[current_ws].focus, entry);
242                 break;
243
244         case SWM_ARG_ID_FOCUSMAIN:
245                 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);;
246                 break;
247
248         default:
249                 return;
250         }
251
252         winfocus = ws[current_ws].focus;
253         unfocus_win(winlostfocus);
254         focus_win(winfocus);
255         XSync(display, False);
256 }
257
258 /* I know this sucks but it works well enough */
259 void
260 stack(void)
261 {
262         XWindowChanges          wc;
263         struct ws_win           wf, *win, *winfocus = &wf;
264         int                     i, h, w, x, y, winno, hrh;
265
266         DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws);
267
268         winfocus->id = root;
269
270         if (ws[current_ws].winno == 0)
271                 return;
272
273         if (ws[current_ws].winno > 1)
274                 w = width / 2;
275         else
276                 w = width;
277
278         if (ws[current_ws].winno > 2)
279                 hrh = height / (ws[current_ws].winno - 1);
280         else
281                 hrh = 0;
282
283         x = 0;
284         y = 0;
285         h = height;
286         i = 0;
287         TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
288                 if (i == 1) {
289                         x += w + 2;
290                         w -= 2;
291                 }
292                 if (i != 0 && hrh != 0) {
293                         /* correct the last window for lost pixels */
294                         if (win == TAILQ_LAST(&ws[current_ws].winlist,
295                             ws_win_list)) {
296                                 h = height - (i * hrh);
297                                 if (h == 0)
298                                         h = hrh;
299                                 else
300                                         h += hrh;
301                                 y += hrh;
302                         } else {
303                                 h = hrh - 2;
304                                 /* leave first right hand window at y = 0 */
305                                 if (i > 1)
306                                         y += h + 2;
307                         }
308                 }
309
310                 bzero(&wc, sizeof wc);
311                 win->x = wc.x = x;
312                 win->y = wc.y = y;
313                 win->width = wc.width = w;
314                 win->height = wc.height = h;
315                 wc.border_width = 1;
316                 XConfigureWindow(display, win->id, CWX | CWY | CWWidth |
317                     CWHeight | CWBorderWidth, &wc);
318                 if (win == ws[current_ws].focus)
319                         winfocus = win;
320                 else
321                         unfocus_win(win);
322                 XMapWindow(display, win->id);
323                 i++;
324         }
325
326         focus_win(winfocus); /* this has to be done outside of the loop */
327         XSync(display, False);
328 }
329
330 void
331 swap_to_main(union arg *args)
332 {
333         struct ws_win           *tmpwin = TAILQ_FIRST(&ws[current_ws].winlist);
334
335         DNPRINTF(SWM_D_MISC, "swap_to_main: win: %d\n", ws[current_ws].focus->id);
336         TAILQ_REMOVE(&ws[current_ws].winlist, tmpwin, entry);
337         TAILQ_INSERT_AFTER(&ws[current_ws].winlist, ws[current_ws].focus,
338             tmpwin, entry);
339         TAILQ_REMOVE(&ws[current_ws].winlist, ws[current_ws].focus, entry);
340         TAILQ_INSERT_HEAD(&ws[current_ws].winlist, ws[current_ws].focus, entry);
341         ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
342         ignore_enter = 2;
343         stack();
344 }
345
346 /* terminal + args */
347 char                            *term[] = { "xterm", NULL };
348
349 /* key definitions */
350 struct key {
351         unsigned int            mod;
352         KeySym                  keysym;
353         void                    (*func)(union arg *);
354         union arg               args;
355 } keys[] = {
356         /* modifier             key     function                argument */
357         { MODKEY,               XK_Return,      swap_to_main,   {0} },
358         { MODKEY | ShiftMask,   XK_Return,      spawn,          {.argv = term } },
359         { MODKEY | ShiftMask,   XK_q,           quit,           {0} },
360         { MODKEY,               XK_m,           focus,          {.id = SWM_ARG_ID_FOCUSMAIN} },
361         { MODKEY,               XK_1,           switchws,       {.id = 0} },
362         { MODKEY,               XK_2,           switchws,       {.id = 1} },
363         { MODKEY,               XK_3,           switchws,       {.id = 2} },
364         { MODKEY,               XK_4,           switchws,       {.id = 3} },
365         { MODKEY,               XK_5,           switchws,       {.id = 4} },
366         { MODKEY,               XK_6,           switchws,       {.id = 5} },
367         { MODKEY,               XK_7,           switchws,       {.id = 6} },
368         { MODKEY,               XK_8,           switchws,       {.id = 7} },
369         { MODKEY,               XK_9,           switchws,       {.id = 8} },
370         { MODKEY,               XK_0,           switchws,       {.id = 9} },
371         { MODKEY,               XK_Tab,         focus,          {.id = SWM_ARG_ID_FOCUSNEXT} },
372         { MODKEY | ShiftMask,   XK_Tab,         focus,          {.id = SWM_ARG_ID_FOCUSPREV} },
373 };
374
375 void
376 updatenumlockmask(void)
377 {
378         unsigned int            i, j;
379         XModifierKeymap         *modmap;
380
381         DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
382         numlockmask = 0;
383         modmap = XGetModifierMapping(display);
384         for (i = 0; i < 8; i++)
385                 for (j = 0; j < modmap->max_keypermod; j++)
386                         if (modmap->modifiermap[i * modmap->max_keypermod + j]
387                           == XKeysymToKeycode(display, XK_Num_Lock))
388                                 numlockmask = (1 << i);
389
390         XFreeModifiermap(modmap);
391 }
392
393 void
394 grabkeys(void)
395 {
396         unsigned int            i, j;
397         KeyCode                 code;
398         unsigned int            modifiers[] =
399             { 0, LockMask, numlockmask, numlockmask | LockMask };
400
401         DNPRINTF(SWM_D_MISC, "grabkeys\n");
402         updatenumlockmask();
403
404         XUngrabKey(display, AnyKey, AnyModifier, root);
405         for(i = 0; i < LENGTH(keys); i++) {
406                 if((code = XKeysymToKeycode(display, keys[i].keysym)))
407                         for(j = 0; j < LENGTH(modifiers); j++)
408                                 XGrabKey(display, code,
409                                     keys[i].mod | modifiers[j], root,
410                                     True, GrabModeAsync, GrabModeAsync);
411         }
412 }
413 void
414 expose(XEvent *e)
415 {
416         XExposeEvent            *ev = &e->xexpose;
417
418         DNPRINTF(SWM_D_EVENT, "expose: window: %d\n", ev->window);
419 }
420
421 void
422 keypress(XEvent *e)
423 {
424         unsigned int            i;
425         KeySym                  keysym;
426         XKeyEvent               *ev = &e->xkey;
427
428         DNPRINTF(SWM_D_EVENT, "keypress: window: %d\n", ev->window);
429
430         keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
431         for(i = 0; i < LENGTH(keys); i++)
432                 if(keysym == keys[i].keysym
433                    && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
434                    && keys[i].func)
435                         keys[i].func(&(keys[i].args));
436 }
437
438 void
439 buttonpress(XEvent *e)
440 {
441         XButtonPressedEvent     *ev = &e->xbutton;
442         struct ws_win           *win;
443
444         DNPRINTF(SWM_D_EVENT, "buttonpress: window: %d\n", ev->window);
445
446         if (ev->window == root)
447                 return;
448         if (ev->window == ws[current_ws].focus->id)
449                 return;
450 #if 0
451         TAILQ_FOREACH(win, &ws[current_ws].winlist, entry)
452                 if (win->id == ev->window) {
453                         /* focus in the clicked window */
454                         XSetWindowBorder(display, ev->window, 0xff0000);
455                         XSetWindowBorder(display,
456                             ws[current_ws].focus->id, 0x888888);
457                         XSetInputFocus(display, ev->window, RevertToPointerRoot,
458                             CurrentTime);
459                         ws[current_ws].focus = win;
460                         XSync(display, False);
461                         break;
462         }
463 #endif
464 }
465
466 void
467 configurerequest(XEvent *e)
468 {
469         XConfigureRequestEvent  *ev = &e->xconfigurerequest;
470         struct ws_win           *win;
471
472         DNPRINTF(SWM_D_EVENT, "configurerequest: window: %d\n", ev->window);
473
474         XSelectInput(display, ev->window, ButtonPressMask | EnterWindowMask |
475             FocusChangeMask);
476
477         if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
478                 errx(1, "calloc: failed to allocate memory for new window");
479
480         win->id = ev->window;
481         TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
482         ws[current_ws].focus = win; /* make new win focused */
483         ws[current_ws].winno++;
484         stack();
485 }
486
487 void
488 configurenotify(XEvent *e)
489 {
490         XConfigureEvent         *ev = &e->xconfigure;
491
492         DNPRINTF(SWM_D_EVENT, "configurenotify: window: %d\n", ev->window);
493 }
494
495 void
496 destroynotify(XEvent *e)
497 {
498         size_t                   sz;
499         struct ws_win           *win;
500         XDestroyWindowEvent     *ev = &e->xdestroywindow;
501
502         DNPRINTF(SWM_D_EVENT, "destroynotify: window %d\n", ev->window);
503
504         TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
505                 if (ev->window == win->id) {
506                         /* find a window to focus */
507                         ws[current_ws].focus =
508                             TAILQ_PREV(win, ws_win_list,entry);
509                         if (ws[current_ws].focus == NULL)
510                                 ws[current_ws].focus =
511                                     TAILQ_FIRST(&ws[current_ws].winlist);
512         
513                         TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
514                         free(win);
515                         ws[current_ws].winno--;
516                         break;
517                 }
518         }
519
520         stack();
521 }
522
523 void
524 enternotify(XEvent *e)
525 {
526         XCrossingEvent          *ev = &e->xcrossing;
527         struct ws_win           *win;
528
529         DNPRINTF(SWM_D_EVENT, "enternotify: window: %d\n", ev->window);
530
531         if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
532             ev->window != root)
533                 return;
534         if (ignore_enter) {
535                 /* eat event(s) to prevent autofocus */
536                 ignore_enter--;
537                 return;
538         }
539         TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
540                 if (win->id == ev->window)
541                         focus_win(win);
542                 else
543                         unfocus_win(win);
544         }
545 }
546
547 void
548 focusin(XEvent *e)
549 {
550         XFocusChangeEvent       *ev = &e->xfocus;
551
552         DNPRINTF(SWM_D_EVENT, "focusin: window: %d\n", ev->window);
553
554         if (ev->window == root)
555                 return;
556
557         /*
558          * kill grab for now so that we can cut and paste , this screws up
559          * click to focus
560          */
561         /*
562         DNPRINTF(SWM_D_EVENT, "focusin: window: %d grabbing\n", ev->window);
563         XGrabButton(display, Button1, AnyModifier, ev->window, False,
564             ButtonPress, GrabModeAsync, GrabModeSync, None, None);
565         */
566 }
567
568 void
569 mappingnotify(XEvent *e)
570 {
571         XMappingEvent           *ev = &e->xmapping;
572
573         DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %d\n", ev->window);
574
575         XRefreshKeyboardMapping(ev);
576         if(ev->request == MappingKeyboard)
577                 grabkeys();
578 }
579
580 void
581 maprequest(XEvent *e)
582 {
583         XMapRequestEvent        *ev = &e->xmaprequest;
584
585         DNPRINTF(SWM_D_EVENT, "maprequest: window: %d\n", ev->window);
586 }
587
588 void
589 propertynotify(XEvent *e)
590 {
591         XPropertyEvent          *ev = &e->xproperty;
592
593         DNPRINTF(SWM_D_EVENT, "propertynotify: window: %d\n", ev->window);
594 }
595
596 void
597 unmapnotify(XEvent *e)
598 {
599         XUnmapEvent             *ev = &e->xunmap;
600
601         DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %d\n", ev->window);
602 }
603
604 void                    (*handler[LASTEvent])(XEvent *) = {
605                                 [Expose] = expose,
606                                 [KeyPress] = keypress,
607                                 [ButtonPress] = buttonpress,
608                                 [ConfigureRequest] = configurerequest,
609                                 [ConfigureNotify] = configurenotify,
610                                 [DestroyNotify] = destroynotify,
611                                 [EnterNotify] = enternotify,
612                                 [FocusIn] = focusin,
613                                 [MappingNotify] = mappingnotify,
614                                 [MapRequest] = maprequest,
615                                 [PropertyNotify] = propertynotify,
616                                 [UnmapNotify] = unmapnotify,
617 };
618
619 int
620 xerror_start(Display *d, XErrorEvent *ee)
621 {
622         other_wm = 1;
623         return (-1);
624 }
625
626 int
627 xerror(Display *d, XErrorEvent *ee)
628 {
629         fprintf(stderr, "error: %p %p\n", display, ee);
630
631         return (-1);
632 }
633
634 int
635 active_wm(void)
636 {
637         other_wm = 0;
638         xerrorxlib = XSetErrorHandler(xerror_start);
639
640         /* this causes an error if some other window manager is running */
641         XSelectInput(display, DefaultRootWindow(display),
642             SubstructureRedirectMask);
643         XSync(display, False);
644         if(other_wm)
645                 return (1);
646
647         XSetErrorHandler(xerror);
648         XSync(display, False);
649         return (0);
650 }
651
652 int
653 main(int argc, char *argv[])
654 {
655         XEvent                  e;
656         int                     i;
657
658         fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
659         if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
660                 warnx("no locale support");
661
662         if(!(display = XOpenDisplay(0)))
663                 errx(1, "can not open display");
664
665         if (active_wm())
666                 errx(1, "other wm running");
667
668         screen = DefaultScreen(display);
669         root = RootWindow(display, screen);
670         width = DisplayWidth(display, screen) - 2;
671         height = DisplayHeight(display, screen) - 2;
672
673         /* make work space 1 active */
674         ws[0].visible = 1;
675         ws[0].focus = NULL;
676         ws[0].winno = 0;
677         TAILQ_INIT(&ws[0].winlist);
678         for (i = 1; i < SWM_WS_MAX; i++) {
679                 ws[i].visible = 0;
680                 ws[i].focus = NULL;
681                 ws[i].winno = 0;
682                 TAILQ_INIT(&ws[i].winlist);
683         }
684
685         XSelectInput(display, root, SubstructureRedirectMask |
686             SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
687             EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
688             FocusChangeMask | PropertyChangeMask);
689
690         grabkeys();
691
692         while (running) {
693                 XNextEvent(display, &e);
694                 if (handler[e.type])
695                         handler[e.type](&e);
696         }
697
698         XCloseDisplay(display);
699
700         return (0);
701 }