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