]> code.delx.au - spectrwm/blob - scrotwm.c
Complie with warnings, and cleanup.
[spectrwm] / 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 struct ws_win *focus; /* which win has focus */
133 int winno; /* total nr of windows */
134 struct ws_win_list winlist;
135 } ws[SWM_WS_MAX];
136 int current_ws = 0;
137
138 /* args to functions */
139 union arg {
140 int id;
141 #define SWM_ARG_ID_FOCUSNEXT (0)
142 #define SWM_ARG_ID_FOCUSPREV (1)
143 #define SWM_ARG_ID_FOCUSMAIN (2)
144 char **argv;
145 };
146
147 void
148 bar_print(void)
149 {
150 time_t tmt;
151 struct tm tm;
152
153 /* clear old text */
154 XSetForeground(display, bar_gc, 0x000000);
155 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
156 strlen(bar_text));
157
158 /* draw new text */
159 time(&tmt);
160 localtime_r(&tmt, &tm);
161 strftime(bar_text, sizeof bar_text, "%a %b %d %R %Z %Y", &tm);
162 XSetForeground(display, bar_gc, 0xa0a0a0);
163 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
164 strlen(bar_text));
165 XSync(display, False);
166 }
167 void
168 quit(union arg *args)
169 {
170 DNPRINTF(SWM_D_MISC, "quit\n");
171 running = 0;
172 }
173
174 void
175 spawn(union arg *args)
176 {
177 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
178 /*
179 * The double-fork construct avoids zombie processes and keeps the code
180 * clean from stupid signal handlers.
181 */
182 if(fork() == 0) {
183 if(fork() == 0) {
184 if(display)
185 close(ConnectionNumber(display));
186 setsid();
187 execvp(args->argv[0], args->argv);
188 fprintf(stderr, "execvp failed\n");
189 perror(" failed");
190 }
191 exit(0);
192 }
193 wait(0);
194 }
195
196 void
197 focus_win(struct ws_win *win)
198 {
199 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
200 XSetWindowBorder(display, win->id, col_focus);
201 XSetInputFocus(display, win->id, RevertToPointerRoot, CurrentTime);
202 ws[current_ws].focus = win;
203 }
204
205 void
206 unfocus_win(struct ws_win *win)
207 {
208 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
209 XSetWindowBorder(display, win->id, col_unfocus);
210 if (ws[current_ws].focus == win)
211 ws[current_ws].focus = NULL;
212 }
213
214 void
215 switchws(union arg *args)
216 {
217 int wsid = args->id;
218 struct ws_win *win;
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, 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: %lu\n",
366 ws[current_ws].focus->id);
367
368 TAILQ_REMOVE(&ws[current_ws].winlist, tmpwin, entry);
369 TAILQ_INSERT_AFTER(&ws[current_ws].winlist, ws[current_ws].focus,
370 tmpwin, entry);
371 TAILQ_REMOVE(&ws[current_ws].winlist, ws[current_ws].focus, entry);
372 TAILQ_INSERT_HEAD(&ws[current_ws].winlist, ws[current_ws].focus, entry);
373 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
374 ignore_enter = 2;
375 stack();
376 }
377
378 /* terminal + args */
379 char *term[] = { "xterm", NULL };
380
381 /* key definitions */
382 struct key {
383 unsigned int mod;
384 KeySym keysym;
385 void (*func)(union arg *);
386 union arg args;
387 } keys[] = {
388 /* modifier key function argument */
389 { MODKEY, XK_Return, swap_to_main, {0} },
390 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = term } },
391 { MODKEY | ShiftMask, XK_q, quit, {0} },
392 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
393 { MODKEY, XK_1, switchws, {.id = 0} },
394 { MODKEY, XK_2, switchws, {.id = 1} },
395 { MODKEY, XK_3, switchws, {.id = 2} },
396 { MODKEY, XK_4, switchws, {.id = 3} },
397 { MODKEY, XK_5, switchws, {.id = 4} },
398 { MODKEY, XK_6, switchws, {.id = 5} },
399 { MODKEY, XK_7, switchws, {.id = 6} },
400 { MODKEY, XK_8, switchws, {.id = 7} },
401 { MODKEY, XK_9, switchws, {.id = 8} },
402 { MODKEY, XK_0, switchws, {.id = 9} },
403 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
404 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
405 };
406
407 void
408 updatenumlockmask(void)
409 {
410 unsigned int i, j;
411 XModifierKeymap *modmap;
412
413 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
414 numlockmask = 0;
415 modmap = XGetModifierMapping(display);
416 for (i = 0; i < 8; i++)
417 for (j = 0; j < modmap->max_keypermod; j++)
418 if (modmap->modifiermap[i * modmap->max_keypermod + j]
419 == XKeysymToKeycode(display, XK_Num_Lock))
420 numlockmask = (1 << i);
421
422 XFreeModifiermap(modmap);
423 }
424
425 void
426 grabkeys(void)
427 {
428 unsigned int i, j;
429 KeyCode code;
430 unsigned int modifiers[] =
431 { 0, LockMask, numlockmask, numlockmask | LockMask };
432
433 DNPRINTF(SWM_D_MISC, "grabkeys\n");
434 updatenumlockmask();
435
436 XUngrabKey(display, AnyKey, AnyModifier, root);
437 for(i = 0; i < LENGTH(keys); i++) {
438 if((code = XKeysymToKeycode(display, keys[i].keysym)))
439 for(j = 0; j < LENGTH(modifiers); j++)
440 XGrabKey(display, code,
441 keys[i].mod | modifiers[j], root,
442 True, GrabModeAsync, GrabModeAsync);
443 }
444 }
445 void
446 expose(XEvent *e)
447 {
448 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.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: %lu\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 #ifdef SWM_CLICKTOFOCUS
473 struct ws_win *win;
474 #endif
475
476
477 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
478
479 if (ev->window == root)
480 return;
481 if (ev->window == ws[current_ws].focus->id)
482 return;
483 #ifdef SWM_CLICKTOFOCUS
484 TAILQ_FOREACH(win, &ws[current_ws].winlist, entry)
485 if (win->id == ev->window) {
486 /* focus in the clicked window */
487 XSetWindowBorder(display, ev->window, 0xff0000);
488 XSetWindowBorder(display,
489 ws[current_ws].focus->id, 0x888888);
490 XSetInputFocus(display, ev->window, RevertToPointerRoot,
491 CurrentTime);
492 ws[current_ws].focus = win;
493 XSync(display, False);
494 break;
495 }
496 #endif
497 }
498
499 void
500 configurerequest(XEvent *e)
501 {
502 XConfigureRequestEvent *ev = &e->xconfigurerequest;
503 struct ws_win *win;
504
505 DNPRINTF(SWM_D_EVENT, "configurerequest: window: %lu\n", ev->window);
506
507 XSelectInput(display, ev->window, ButtonPressMask | EnterWindowMask |
508 FocusChangeMask);
509
510 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
511 errx(1, "calloc: failed to allocate memory for new window");
512
513 win->id = ev->window;
514 TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
515 ws[current_ws].focus = win; /* make new win focused */
516 ws[current_ws].winno++;
517 stack();
518 }
519
520 void
521 configurenotify(XEvent *e)
522 {
523 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
524 e->xconfigure.window);
525 }
526
527 void
528 destroynotify(XEvent *e)
529 {
530 struct ws_win *win;
531 XDestroyWindowEvent *ev = &e->xdestroywindow;
532
533 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
534
535 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
536 if (ev->window == win->id) {
537 /* find a window to focus */
538 ws[current_ws].focus =
539 TAILQ_PREV(win, ws_win_list,entry);
540 if (ws[current_ws].focus == NULL)
541 ws[current_ws].focus =
542 TAILQ_FIRST(&ws[current_ws].winlist);
543
544 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
545 free(win);
546 ws[current_ws].winno--;
547 break;
548 }
549 }
550
551 stack();
552 }
553
554 void
555 enternotify(XEvent *e)
556 {
557 XCrossingEvent *ev = &e->xcrossing;
558 struct ws_win *win;
559
560 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
561
562 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
563 ev->window != root)
564 return;
565 if (ignore_enter) {
566 /* eat event(s) to prevent autofocus */
567 ignore_enter--;
568 return;
569 }
570 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
571 if (win->id == ev->window)
572 focus_win(win);
573 else
574 unfocus_win(win);
575 }
576 }
577
578 void
579 focusin(XEvent *e)
580 {
581 XFocusChangeEvent *ev = &e->xfocus;
582
583 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", ev->window);
584
585 if (ev->window == root)
586 return;
587
588 /*
589 * kill grab for now so that we can cut and paste , this screws up
590 * click to focus
591 */
592 /*
593 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
594 XGrabButton(display, Button1, AnyModifier, ev->window, False,
595 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
596 */
597 }
598
599 void
600 mappingnotify(XEvent *e)
601 {
602 XMappingEvent *ev = &e->xmapping;
603
604 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
605
606 XRefreshKeyboardMapping(ev);
607 if(ev->request == MappingKeyboard)
608 grabkeys();
609 }
610
611 void
612 maprequest(XEvent *e)
613 {
614 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
615 e->xmaprequest.window);
616 }
617
618 void
619 propertynotify(XEvent *e)
620 {
621 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
622 e->xproperty.window);
623 }
624
625 void
626 unmapnotify(XEvent *e)
627 {
628 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
629 }
630
631 void (*handler[LASTEvent])(XEvent *) = {
632 [Expose] = expose,
633 [KeyPress] = keypress,
634 [ButtonPress] = buttonpress,
635 [ConfigureRequest] = configurerequest,
636 [ConfigureNotify] = configurenotify,
637 [DestroyNotify] = destroynotify,
638 [EnterNotify] = enternotify,
639 [FocusIn] = focusin,
640 [MappingNotify] = mappingnotify,
641 [MapRequest] = maprequest,
642 [PropertyNotify] = propertynotify,
643 [UnmapNotify] = unmapnotify,
644 };
645
646 int
647 xerror_start(Display *d, XErrorEvent *ee)
648 {
649 other_wm = 1;
650 return (-1);
651 }
652
653 int
654 xerror(Display *d, XErrorEvent *ee)
655 {
656 fprintf(stderr, "error: %p %p\n", display, ee);
657
658 return (-1);
659 }
660
661 int
662 active_wm(void)
663 {
664 other_wm = 0;
665 xerrorxlib = XSetErrorHandler(xerror_start);
666
667 /* this causes an error if some other window manager is running */
668 XSelectInput(display, DefaultRootWindow(display),
669 SubstructureRedirectMask);
670 XSync(display, False);
671 if(other_wm)
672 return (1);
673
674 XSetErrorHandler(xerror);
675 XSync(display, False);
676 return (0);
677 }
678
679 int
680 main(int argc, char *argv[])
681 {
682 XEvent e;
683 int i;
684
685 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
686 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
687 warnx("no locale support");
688
689 if(!(display = XOpenDisplay(0)))
690 errx(1, "can not open display");
691
692 if (active_wm())
693 errx(1, "other wm running");
694
695 screen = DefaultScreen(display);
696 root = RootWindow(display, screen);
697 width = DisplayWidth(display, screen) - 2;
698 height = DisplayHeight(display, screen) - 2;
699
700 /* make work space 1 active */
701 ws[0].visible = 1;
702 ws[0].focus = NULL;
703 ws[0].winno = 0;
704 TAILQ_INIT(&ws[0].winlist);
705 for (i = 1; i < SWM_WS_MAX; i++) {
706 ws[i].visible = 0;
707 ws[i].focus = NULL;
708 ws[i].winno = 0;
709 TAILQ_INIT(&ws[i].winlist);
710 }
711
712 /* setup status bar */
713 bar_fs = XLoadQueryFont(display,
714 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*");
715 if (bar_fs == NULL) {
716 /* load a font that is default */
717 bar_fs = XLoadQueryFont(display,
718 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*");
719 if (bar_fs == NULL)
720 errx(1, "couldn't load font");
721 }
722 bar_height = bar_fs->ascent + bar_fs->descent + 3;
723
724 XSelectInput(display, root, SubstructureRedirectMask |
725 SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
726 EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
727 FocusChangeMask | PropertyChangeMask);
728
729 grabkeys();
730
731 bar_window = XCreateSimpleWindow(display, root, 0, 0, width,
732 bar_height - 2, 1, 0x008080, 0x000000);
733 bar_gc = XCreateGC(display, bar_window, 0, &bar_gcv);
734 XSetFont(display, bar_gc, bar_fs->fid);
735 if (bar_enabled) {
736 height -= bar_height; /* correct screen height */
737 XMapWindow(display, bar_window);
738 }
739 bar_print();
740
741 while (running) {
742 XNextEvent(display, &e);
743 if (handler[e.type])
744 handler[e.type](&e);
745 }
746
747 XCloseDisplay(display);
748
749 return (0);
750 }