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