JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Initial revision
[spectrwm.git] / scrotwm.c
1 /* cc -O2 -I /usr/X11R6/include/ scrotwm.c -L /usr/X11R6/lib/ -lX11 -o scrotwm */
2 /* then add to .xinitrc */
3 /*
4  * Copyright (c) 2009 Marco Peereboom <marco@peereboom.us>
5  * Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 /*
20  * Much code and ideas taken from dwm under the following license:
21  * MIT/X Consortium License
22  * 
23  * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
24  * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
25  * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
26  * 2007 Premysl Hruby <dfenze at gmail dot com>
27  * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
28  * 2007 Christof Musik <christof at sendfax dot de>
29  * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
30  * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
31  * 2008 Martin Hurton <martin dot hurton at gmail dot com>
32  * 
33  * Permission is hereby granted, free of charge, to any person obtaining a
34  * copy of this software and associated documentation files (the "Software"),
35  * to deal in the Software without restriction, including without limitation
36  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
37  * and/or sell copies of the Software, and to permit persons to whom the
38  * Software is furnished to do so, subject to the following conditions:
39  * 
40  * The above copyright notice and this permission notice shall be included in
41  * all copies or substantial portions of the Software.
42  * 
43  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
44  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
45  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
46  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
47  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
48  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
49  * DEALINGS IN THE SOFTWARE.
50  */
51
52 #define SWM_VERSION     "0.5"
53
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <err.h>
57 #include <locale.h>
58 #include <unistd.h>
59
60 #include <sys/types.h>
61 #include <sys/wait.h>
62 #include <sys/queue.h>
63
64 #include <X11/cursorfont.h>
65 #include <X11/keysym.h>
66 #include <X11/Xatom.h>
67 #include <X11/Xlib.h>
68 #include <X11/Xproto.h>
69 #include <X11/Xutil.h>
70
71 /* #define SWM_DEBUG */
72 #ifdef SWM_DEBUG
73 #define DPRINTF(x...)           do { if (swm_debug) fprintf(stderr, x); } while(0)
74 #define DNPRINTF(n,x...)        do { if (swm_debug & n) fprintf(stderr, x); } while(0)
75 #define SWM_D_EVENT             0x0001
76 #define SWM_D_WS                0x0002
77
78 uint32_t                swm_debug = 0
79                             | SWM_D_EVENT
80                             | SWM_D_WS
81                             ;
82 #else
83 #define DPRINTF(x...)
84 #define DNPRINTF(n,x...)
85 #endif
86
87 #define LENGTH(x)               (sizeof x / sizeof x[0])
88 #define MODKEY                  Mod1Mask
89 #define CLEANMASK(mask)         (mask & ~(numlockmask | LockMask))
90
91 int                     (*xerrorxlib)(Display *, XErrorEvent *);
92 int                     other_wm;
93 int                     screen;
94 int                     width, height;
95 int                     running = 1;
96 int                     ignore_enter = 0;
97 unsigned int            numlockmask = 0;
98 Display                 *display;
99 Window                  root;
100
101 struct ws_win {
102         TAILQ_ENTRY(ws_win)     entry;
103         Window                  id;
104 };
105
106 TAILQ_HEAD(ws_win_list, ws_win);
107
108 /* define work spaces */
109 #define SWM_WS_MAX              (10)
110 struct workspace {
111         int                      visible;       /* workspace visible */
112         struct ws_win           *focus;         /* which win has focus */
113         int                      winno;         /* total nr of windows */
114         struct ws_win_list       winlist;
115 } ws[SWM_WS_MAX];
116 int                     current_ws = 0;
117
118 /* args to functions */
119 union arg {
120         int             id;
121 #define SWM_ARG_ID_FOCUSNEXT    (0)
122 #define SWM_ARG_ID_FOCUSPREV    (1)
123 #define SWM_ARG_ID_FOCUSMAIN    (2)
124 };
125
126 void
127 quit(union arg *args)
128 {
129         running = 0;
130 }
131
132 void
133 spawn(union arg *args)
134 {
135         char                    *argv[] = { "xterm", NULL }; /* XXX make this in args */
136
137         /*
138          * The double-fork construct avoids zombie processes and keeps the code
139          * clean from stupid signal handlers.
140          */
141         if(fork() == 0) {
142                 if(fork() == 0) {
143                         if(display)
144                                 close(ConnectionNumber(display));
145                         setsid();
146                         execvp(argv[0], argv);
147                         fprintf(stderr, "execvp failed\n");
148                         perror(" failed");
149                 }
150                 exit(0);
151         }
152         wait(0);
153 }
154
155 void
156 switchws(union arg *args)
157 {
158         int                     wsid = args->id;
159         struct ws_win           *win;
160         Window                  winfocus;
161
162         DNPRINTF(SWM_D_WS, "switchws: %d\n", wsid + 1);
163
164         if (wsid == current_ws)
165                 return;
166
167         /* map new window first to prevent ugly blinking */
168         TAILQ_FOREACH (win, &ws[wsid].winlist, entry)
169                 XMapWindow(display, win->id);
170         ws[wsid].visible = 1;
171
172         TAILQ_FOREACH (win, &ws[current_ws].winlist, entry)
173                 XUnmapWindow(display, win->id);
174         ws[current_ws].visible = 0;
175
176         current_ws = wsid;
177
178         ignore_enter = 1;
179         if (ws[wsid].focus != NULL) {
180                 DNPRINTF(SWM_D_WS, "switchws: focus %d\n", ws[wsid].focus);
181                 XSetInputFocus(display, ws[wsid].focus->id, RevertToPointerRoot,
182                     CurrentTime);
183         }
184         XSync(display, False);
185 }
186
187 void
188 focus(union arg *args)
189 {
190         Window                  winfocus, winlostfocus;
191
192         if (ws[current_ws].focus == NULL || ws[current_ws].winno == 0)
193                 return;
194
195         winlostfocus = ws[current_ws].focus->id;
196
197         switch (args->id) {
198         case SWM_ARG_ID_FOCUSPREV:
199                 if (ws[current_ws].focus ==
200                     TAILQ_FIRST(&ws[current_ws].winlist))
201                         ws[current_ws].focus =
202                             TAILQ_LAST(&ws[current_ws].winlist, ws_win_list);
203                 else
204                         ws[current_ws].focus =TAILQ_PREV(ws[current_ws].focus,
205                             ws_win_list, entry);
206                 break;
207
208         case SWM_ARG_ID_FOCUSNEXT:
209                 if (ws[current_ws].focus == TAILQ_LAST(&ws[current_ws].winlist,
210                     ws_win_list))
211                         ws[current_ws].focus =
212                             TAILQ_FIRST(&ws[current_ws].winlist);
213                 else
214                         ws[current_ws].focus =
215                             TAILQ_NEXT(ws[current_ws].focus, entry);
216                 break;
217
218         case SWM_ARG_ID_FOCUSMAIN:
219                 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);;
220                 break;
221
222         default:
223                 return;
224         }
225
226         winfocus = ws[current_ws].focus->id;
227         XSetWindowBorder(display, winlostfocus, 0x888888);
228         XSetWindowBorder(display, winfocus, 0xff0000);
229         XSetInputFocus(display, winfocus, RevertToPointerRoot, CurrentTime);
230         XSync(display, False);
231 }
232
233 /* I know this sucks but it works well enough */
234 void
235 stack(void)
236 {
237         XWindowChanges          wc;
238         struct ws_win           *win;
239         Window                  winfocus = root;
240         int                     i, h, w, x, y, winno, hrh;
241
242         DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws);
243
244         if (ws[current_ws].winno == 0)
245                 return;
246
247         if (ws[current_ws].winno > 1)
248                 w = width / 2;
249         else
250                 w = width;
251
252         if (ws[current_ws].winno > 2)
253                 hrh = height / (ws[current_ws].winno - 1);
254         else
255                 hrh = 0;
256
257         x = 0;
258         y = 0;
259         h = height;
260         i = 0;
261         TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
262                 if (i == 1) {
263                         x += w + 2;
264                         w -= 2;
265                 }
266                 if (i != 0 && hrh != 0) {
267                         /* correct the last window for lost pixels */
268                         if (win == TAILQ_LAST(&ws[current_ws].winlist,
269                             ws_win_list)) {
270                                 h = height - (i * hrh);
271                                 if (h == 0)
272                                         h = hrh;
273                                 else
274                                         h += hrh;
275                                 y += hrh;
276                         } else {
277                                 h = hrh - 2;
278                                 /* leave first right hand window at y = 0 */
279                                 if (i > 1)
280                                         y += h + 2;
281                         }
282                 }
283
284                 bzero(&wc, sizeof wc);
285                 wc.x = x;
286                 wc.y = y;
287                 wc.width = w;
288                 wc.height = h;
289                 wc.border_width = 1;
290                 XConfigureWindow(display, win->id, CWX | CWY | CWWidth |
291                     CWHeight | CWBorderWidth, &wc);
292                 if (win == ws[current_ws].focus) {
293                         winfocus = win->id;
294                         XSetWindowBorder(display, win->id, 0xff0000);
295                 } else
296                         XSetWindowBorder(display, win->id, 0x888888);
297                 XMapWindow(display, win->id);
298                 i++;
299         }
300
301         XSetInputFocus(display, winfocus, RevertToPointerRoot, CurrentTime);
302         XSync(display, False);
303 }
304
305 void
306 swap_to_main(union arg *args)
307 {
308         struct ws_win           *tmpwin = TAILQ_FIRST(&ws[current_ws].winlist);
309
310         TAILQ_REMOVE(&ws[current_ws].winlist, tmpwin, entry);
311         TAILQ_INSERT_AFTER(&ws[current_ws].winlist, ws[current_ws].focus,
312             tmpwin, entry);
313         TAILQ_REMOVE(&ws[current_ws].winlist, ws[current_ws].focus, entry);
314         TAILQ_INSERT_HEAD(&ws[current_ws].winlist, ws[current_ws].focus, entry);
315         ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
316         ignore_enter = 2;
317         stack();
318 }
319
320 /* key definitions */
321 struct key {
322         unsigned int            mod;
323         KeySym                  keysym;
324         void                    (*func)(union arg *);
325         union arg               args;
326 } keys[] = {
327         /* modifier             key     function                argument */
328         { MODKEY,               XK_Return,      swap_to_main,   {0} },
329         { MODKEY | ShiftMask,   XK_Return,      spawn,          {0} },
330         { MODKEY | ShiftMask,   XK_q,           quit,           {0} },
331         { MODKEY,               XK_m,           focus,          {.id = SWM_ARG_ID_FOCUSMAIN} },
332         { MODKEY,               XK_1,           switchws,       {.id = 0} },
333         { MODKEY,               XK_2,           switchws,       {.id = 1} },
334         { MODKEY,               XK_3,           switchws,       {.id = 2} },
335         { MODKEY,               XK_4,           switchws,       {.id = 3} },
336         { MODKEY,               XK_5,           switchws,       {.id = 4} },
337         { MODKEY,               XK_6,           switchws,       {.id = 5} },
338         { MODKEY,               XK_7,           switchws,       {.id = 6} },
339         { MODKEY,               XK_8,           switchws,       {.id = 7} },
340         { MODKEY,               XK_9,           switchws,       {.id = 8} },
341         { MODKEY,               XK_0,           switchws,       {.id = 9} },
342         { MODKEY,               XK_Tab,         focus,          {.id = SWM_ARG_ID_FOCUSNEXT} },
343         { MODKEY | ShiftMask,   XK_Tab,         focus,          {.id = SWM_ARG_ID_FOCUSPREV} },
344 };
345
346 void
347 updatenumlockmask(void)
348 {
349         unsigned int            i, j;
350         XModifierKeymap         *modmap;
351
352         numlockmask = 0;
353         modmap = XGetModifierMapping(display);
354         for (i = 0; i < 8; i++)
355                 for (j = 0; j < modmap->max_keypermod; j++)
356                         if (modmap->modifiermap[i * modmap->max_keypermod + j]
357                           == XKeysymToKeycode(display, XK_Num_Lock))
358                                 numlockmask = (1 << i);
359
360         XFreeModifiermap(modmap);
361 }
362
363 void
364 grabkeys(void)
365 {
366         unsigned int            i, j;
367         KeyCode                 code;
368         unsigned int            modifiers[] =
369             { 0, LockMask, numlockmask, numlockmask | LockMask };
370
371         updatenumlockmask();
372
373         XUngrabKey(display, AnyKey, AnyModifier, root);
374         for(i = 0; i < LENGTH(keys); i++) {
375                 if((code = XKeysymToKeycode(display, keys[i].keysym)))
376                         for(j = 0; j < LENGTH(modifiers); j++)
377                                 XGrabKey(display, code,
378                                     keys[i].mod | modifiers[j], root,
379                                     True, GrabModeAsync, GrabModeAsync);
380         }
381 }
382 void
383 expose(XEvent *e)
384 {
385         XExposeEvent            *ev = &e->xexpose;
386
387         DNPRINTF(SWM_D_EVENT, "expose: window: %d\n", ev->window);
388 }
389
390 void
391 keypress(XEvent *e)
392 {
393         unsigned int            i;
394         KeySym                  keysym;
395         XKeyEvent               *ev = &e->xkey;
396
397         DNPRINTF(SWM_D_EVENT, "keypress: window: %d\n", ev->window);
398
399         keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
400         for(i = 0; i < LENGTH(keys); i++)
401                 if(keysym == keys[i].keysym
402                    && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
403                    && keys[i].func)
404                         keys[i].func(&(keys[i].args));
405 }
406
407 void
408 buttonpress(XEvent *e)
409 {
410         XButtonPressedEvent     *ev = &e->xbutton;
411         struct ws_win           *win;
412
413         DNPRINTF(SWM_D_EVENT, "buttonpress: window: %d\n", ev->window);
414
415         if (ev->window == root)
416                 return;
417         if (ev->window == ws[current_ws].focus->id)
418                 return;
419
420         TAILQ_FOREACH(win, &ws[current_ws].winlist, entry)
421                 if (win->id == ev->window) {
422                         /* focus in the clicked window */
423                         XSetWindowBorder(display, ev->window, 0xff0000);
424                         XSetWindowBorder(display,
425                             ws[current_ws].focus->id, 0x888888);
426                         XSetInputFocus(display, ev->window, RevertToPointerRoot,
427                             CurrentTime);
428                         ws[current_ws].focus = win;
429                         XSync(display, False);
430                         break;
431         }
432 }
433
434 void
435 configurerequest(XEvent *e)
436 {
437         XConfigureRequestEvent  *ev = &e->xconfigurerequest;
438         struct ws_win           *win;
439
440         DNPRINTF(SWM_D_EVENT, "configurerequest: window: %d\n", ev->window);
441
442         XSelectInput(display, ev->window, ButtonPressMask | EnterWindowMask |
443             FocusChangeMask);
444
445         if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
446                 errx(1, "calloc: failed to allocate memory for new window");
447
448         win->id = ev->window;
449         TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
450         ws[current_ws].focus = win; /* make new win focused */
451         ws[current_ws].winno++;
452         stack();
453 }
454
455 void
456 configurenotify(XEvent *e)
457 {
458         XConfigureEvent         *ev = &e->xconfigure;
459
460         DNPRINTF(SWM_D_EVENT, "configurenotify: window: %d\n", ev->window);
461 }
462
463 void
464 destroynotify(XEvent *e)
465 {
466         size_t                   sz;
467         struct ws_win           *win;
468         XDestroyWindowEvent     *ev = &e->xdestroywindow;
469
470         DNPRINTF(SWM_D_EVENT, "destroynotify: window %d\n", ev->window);
471
472         TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
473                 DNPRINTF(SWM_D_EVENT, "trying: %x\n", win->id);
474                 if (ev->window == win->id) {
475                         /* find a window to focus */
476                         ws[current_ws].focus =
477                             TAILQ_PREV(win, ws_win_list,entry);
478                         if (ws[current_ws].focus == NULL)
479                                 ws[current_ws].focus =
480                                     TAILQ_FIRST(&ws[current_ws].winlist);
481         
482                         TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
483                         free(win);
484                         ws[current_ws].winno--;
485                         break;
486                 }
487         }
488
489         stack();
490 }
491
492 void
493 enternotify(XEvent *e)
494 {
495         XCrossingEvent          *ev = &e->xcrossing;
496         struct ws_win           *win;
497
498         DNPRINTF(SWM_D_EVENT, "enternotify: window: %d\n", ev->window);
499
500         if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
501             ev->window != root)
502                 return;
503         if (ignore_enter) {
504                 /* eat event(s) to prevent autofocus */
505                 ignore_enter--;
506                 return;
507         }
508         TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
509                 if (win->id == ev->window) {
510                         XSetInputFocus(display, ev->window, RevertToPointerRoot,
511                             CurrentTime);
512                         XSetWindowBorder(display, ev->window, 0xff0000);
513                         ws[current_ws].focus = win;
514                 } else
515                         XSetWindowBorder(display, win->id, 0x888888);
516         }
517 }
518
519 void
520 focusin(XEvent *e)
521 {
522         XFocusChangeEvent       *ev = &e->xfocus;
523
524         DNPRINTF(SWM_D_EVENT, "focusin: window: %d\n", ev->window);
525
526         if (ev->window == root)
527                 return;
528
529         /*
530          * kill grab for now so that we can cut and paste , this screws up
531          * click to focus
532          */
533         /*
534         DNPRINTF(SWM_D_EVENT, "focusin: window: %d grabbing\n", ev->window);
535         XGrabButton(display, Button1, AnyModifier, ev->window, False,
536             ButtonPress, GrabModeAsync, GrabModeSync, None, None);
537         */
538 }
539
540 void
541 mappingnotify(XEvent *e)
542 {
543         XMappingEvent           *ev = &e->xmapping;
544
545         DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %d\n", ev->window);
546
547         XRefreshKeyboardMapping(ev);
548         if(ev->request == MappingKeyboard)
549                 grabkeys();
550 }
551
552 void
553 maprequest(XEvent *e)
554 {
555         XMapRequestEvent        *ev = &e->xmaprequest;
556
557         DNPRINTF(SWM_D_EVENT, "maprequest: window: %d\n", ev->window);
558 }
559
560 void
561 propertynotify(XEvent *e)
562 {
563         XPropertyEvent          *ev = &e->xproperty;
564
565         DNPRINTF(SWM_D_EVENT, "propertynotify: window: %d\n", ev->window);
566 }
567
568 void
569 unmapnotify(XEvent *e)
570 {
571         XUnmapEvent             *ev = &e->xunmap;
572
573         DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %d\n", ev->window);
574 }
575
576 void                    (*handler[LASTEvent])(XEvent *) = {
577                                 [Expose] = expose,
578                                 [KeyPress] = keypress,
579                                 [ButtonPress] = buttonpress,
580                                 [ConfigureRequest] = configurerequest,
581                                 [ConfigureNotify] = configurenotify,
582                                 [DestroyNotify] = destroynotify,
583                                 [EnterNotify] = enternotify,
584                                 [FocusIn] = focusin,
585                                 [MappingNotify] = mappingnotify,
586                                 [MapRequest] = maprequest,
587                                 [PropertyNotify] = propertynotify,
588                                 [UnmapNotify] = unmapnotify,
589 };
590
591 int
592 xerror_start(Display *d, XErrorEvent *ee)
593 {
594         other_wm = 1;
595         return (-1);
596 }
597
598 int
599 xerror(Display *d, XErrorEvent *ee)
600 {
601         fprintf(stderr, "error: %p %p\n", display, ee);
602
603         return (-1);
604 }
605
606 int
607 active_wm(void)
608 {
609         other_wm = 0;
610         xerrorxlib = XSetErrorHandler(xerror_start);
611
612         /* this causes an error if some other window manager is running */
613         XSelectInput(display, DefaultRootWindow(display),
614             SubstructureRedirectMask);
615         XSync(display, False);
616         if(other_wm)
617                 return (1);
618
619         XSetErrorHandler(xerror);
620         XSync(display, False);
621         return (0);
622 }
623
624 int
625 main(int argc, char *argv[])
626 {
627         XEvent                  e;
628         int                     i;
629
630         fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
631         if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
632                 warnx("no locale support");
633
634         if(!(display = XOpenDisplay(0)))
635                 errx(1, "can not open display");
636
637         if (active_wm())
638                 errx(1, "other wm running");
639
640         screen = DefaultScreen(display);
641         root = RootWindow(display, screen);
642         width = DisplayWidth(display, screen) - 2;
643         height = DisplayHeight(display, screen) - 2;
644
645         /* make work space 1 active */
646         ws[0].visible = 1;
647         ws[0].focus = NULL;
648         ws[0].winno = 0;
649         TAILQ_INIT(&ws[0].winlist);
650         for (i = 1; i < SWM_WS_MAX; i++) {
651                 ws[i].visible = 0;
652                 ws[i].focus = NULL;
653                 ws[i].winno = 0;
654                 TAILQ_INIT(&ws[i].winlist);
655         }
656
657         XSelectInput(display, root, SubstructureRedirectMask |
658             SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
659             EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
660             FocusChangeMask | PropertyChangeMask);
661
662         grabkeys();
663
664         while (running) {
665                 XNextEvent(display, &e);
666                 if (handler[e.type])
667                         handler[e.type](&e);
668         }
669
670         XCloseDisplay(display);
671
672         return (0);
673 }