]> code.delx.au - spectrwm/blob - scrotwm.c
Let mod-shift-N send a window to another workspace.
[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 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->id);
375
376 TAILQ_REMOVE(&ws[current_ws].winlist, tmpwin, entry);
377 TAILQ_INSERT_AFTER(&ws[current_ws].winlist, ws[current_ws].focus,
378 tmpwin, entry);
379 TAILQ_REMOVE(&ws[current_ws].winlist, ws[current_ws].focus, entry);
380 TAILQ_INSERT_HEAD(&ws[current_ws].winlist, ws[current_ws].focus, entry);
381 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
382 ignore_enter = 2;
383 stack();
384 }
385
386 void
387 send_to_ws(union arg *args)
388 {
389 int wsid = args->id;
390 struct ws_win *win = ws[current_ws].focus;
391
392 DNPRINTF(SWM_D_MISC, "send_to_ws: win: %lu\n", win->id);
393
394 XUnmapWindow(display, win->id);
395
396 /* find a window to focus */
397 ws[current_ws].focus = TAILQ_PREV(win, ws_win_list,entry);
398 if (ws[current_ws].focus == NULL)
399 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
400
401 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
402 ws[current_ws].winno--;
403
404 TAILQ_INSERT_TAIL(&ws[wsid].winlist, win, entry);
405 if (ws[wsid].winno == 0)
406 ws[wsid].focus = win;
407 ws[wsid].winno++;
408 ws[wsid].restack = 1;
409
410 stack();
411 }
412
413
414 /* terminal + args */
415 char *term[] = { "xterm", NULL };
416
417 /* key definitions */
418 struct key {
419 unsigned int mod;
420 KeySym keysym;
421 void (*func)(union arg *);
422 union arg args;
423 } keys[] = {
424 /* modifier key function argument */
425 { MODKEY, XK_Return, swap_to_main, {0} },
426 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = term } },
427 { MODKEY | ShiftMask, XK_q, quit, {0} },
428 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
429 { MODKEY, XK_1, switchws, {.id = 0} },
430 { MODKEY, XK_2, switchws, {.id = 1} },
431 { MODKEY, XK_3, switchws, {.id = 2} },
432 { MODKEY, XK_4, switchws, {.id = 3} },
433 { MODKEY, XK_5, switchws, {.id = 4} },
434 { MODKEY, XK_6, switchws, {.id = 5} },
435 { MODKEY, XK_7, switchws, {.id = 6} },
436 { MODKEY, XK_8, switchws, {.id = 7} },
437 { MODKEY, XK_9, switchws, {.id = 8} },
438 { MODKEY, XK_0, switchws, {.id = 9} },
439 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
440 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
441 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
442 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
443 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
444 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
445 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
446 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
447 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
448 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
449 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
450 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
451 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
452 };
453
454 void
455 updatenumlockmask(void)
456 {
457 unsigned int i, j;
458 XModifierKeymap *modmap;
459
460 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
461 numlockmask = 0;
462 modmap = XGetModifierMapping(display);
463 for (i = 0; i < 8; i++)
464 for (j = 0; j < modmap->max_keypermod; j++)
465 if (modmap->modifiermap[i * modmap->max_keypermod + j]
466 == XKeysymToKeycode(display, XK_Num_Lock))
467 numlockmask = (1 << i);
468
469 XFreeModifiermap(modmap);
470 }
471
472 void
473 grabkeys(void)
474 {
475 unsigned int i, j;
476 KeyCode code;
477 unsigned int modifiers[] =
478 { 0, LockMask, numlockmask, numlockmask | LockMask };
479
480 DNPRINTF(SWM_D_MISC, "grabkeys\n");
481 updatenumlockmask();
482
483 XUngrabKey(display, AnyKey, AnyModifier, root);
484 for(i = 0; i < LENGTH(keys); i++) {
485 if((code = XKeysymToKeycode(display, keys[i].keysym)))
486 for(j = 0; j < LENGTH(modifiers); j++)
487 XGrabKey(display, code,
488 keys[i].mod | modifiers[j], root,
489 True, GrabModeAsync, GrabModeAsync);
490 }
491 }
492 void
493 expose(XEvent *e)
494 {
495 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
496 }
497
498 void
499 keypress(XEvent *e)
500 {
501 unsigned int i;
502 KeySym keysym;
503 XKeyEvent *ev = &e->xkey;
504
505 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
506
507 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
508 for(i = 0; i < LENGTH(keys); i++)
509 if(keysym == keys[i].keysym
510 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
511 && keys[i].func)
512 keys[i].func(&(keys[i].args));
513 }
514
515 void
516 buttonpress(XEvent *e)
517 {
518 XButtonPressedEvent *ev = &e->xbutton;
519 #ifdef SWM_CLICKTOFOCUS
520 struct ws_win *win;
521 #endif
522
523
524 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
525
526 if (ev->window == root)
527 return;
528 if (ev->window == ws[current_ws].focus->id)
529 return;
530 #ifdef SWM_CLICKTOFOCUS
531 TAILQ_FOREACH(win, &ws[current_ws].winlist, entry)
532 if (win->id == ev->window) {
533 /* focus in the clicked window */
534 XSetWindowBorder(display, ev->window, 0xff0000);
535 XSetWindowBorder(display,
536 ws[current_ws].focus->id, 0x888888);
537 XSetInputFocus(display, ev->window, RevertToPointerRoot,
538 CurrentTime);
539 ws[current_ws].focus = win;
540 XSync(display, False);
541 break;
542 }
543 #endif
544 }
545
546 void
547 configurerequest(XEvent *e)
548 {
549 XConfigureRequestEvent *ev = &e->xconfigurerequest;
550 struct ws_win *win;
551
552 DNPRINTF(SWM_D_EVENT, "configurerequest: window: %lu\n", ev->window);
553
554 XSelectInput(display, ev->window, ButtonPressMask | EnterWindowMask |
555 FocusChangeMask);
556
557 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
558 errx(1, "calloc: failed to allocate memory for new window");
559
560 win->id = ev->window;
561 TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
562 ws[current_ws].focus = win; /* make new win focused */
563 ws[current_ws].winno++;
564 stack();
565 }
566
567 void
568 configurenotify(XEvent *e)
569 {
570 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
571 e->xconfigure.window);
572 }
573
574 void
575 destroynotify(XEvent *e)
576 {
577 struct ws_win *win;
578 XDestroyWindowEvent *ev = &e->xdestroywindow;
579
580 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
581
582 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
583 if (ev->window == win->id) {
584 /* find a window to focus */
585 ws[current_ws].focus =
586 TAILQ_PREV(win, ws_win_list,entry);
587 if (ws[current_ws].focus == NULL)
588 ws[current_ws].focus =
589 TAILQ_FIRST(&ws[current_ws].winlist);
590
591 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
592 free(win);
593 ws[current_ws].winno--;
594 break;
595 }
596 }
597
598 stack();
599 }
600
601 void
602 enternotify(XEvent *e)
603 {
604 XCrossingEvent *ev = &e->xcrossing;
605 struct ws_win *win;
606
607 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
608
609 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
610 ev->window != root)
611 return;
612 if (ignore_enter) {
613 /* eat event(s) to prevent autofocus */
614 ignore_enter--;
615 return;
616 }
617 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
618 if (win->id == ev->window)
619 focus_win(win);
620 else
621 unfocus_win(win);
622 }
623 }
624
625 void
626 focusin(XEvent *e)
627 {
628 XFocusChangeEvent *ev = &e->xfocus;
629
630 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", ev->window);
631
632 if (ev->window == root)
633 return;
634
635 /*
636 * kill grab for now so that we can cut and paste , this screws up
637 * click to focus
638 */
639 /*
640 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
641 XGrabButton(display, Button1, AnyModifier, ev->window, False,
642 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
643 */
644 }
645
646 void
647 mappingnotify(XEvent *e)
648 {
649 XMappingEvent *ev = &e->xmapping;
650
651 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
652
653 XRefreshKeyboardMapping(ev);
654 if(ev->request == MappingKeyboard)
655 grabkeys();
656 }
657
658 void
659 maprequest(XEvent *e)
660 {
661 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
662 e->xmaprequest.window);
663 }
664
665 void
666 propertynotify(XEvent *e)
667 {
668 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
669 e->xproperty.window);
670 }
671
672 void
673 unmapnotify(XEvent *e)
674 {
675 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
676 }
677
678 void (*handler[LASTEvent])(XEvent *) = {
679 [Expose] = expose,
680 [KeyPress] = keypress,
681 [ButtonPress] = buttonpress,
682 [ConfigureRequest] = configurerequest,
683 [ConfigureNotify] = configurenotify,
684 [DestroyNotify] = destroynotify,
685 [EnterNotify] = enternotify,
686 [FocusIn] = focusin,
687 [MappingNotify] = mappingnotify,
688 [MapRequest] = maprequest,
689 [PropertyNotify] = propertynotify,
690 [UnmapNotify] = unmapnotify,
691 };
692
693 int
694 xerror_start(Display *d, XErrorEvent *ee)
695 {
696 other_wm = 1;
697 return (-1);
698 }
699
700 int
701 xerror(Display *d, XErrorEvent *ee)
702 {
703 fprintf(stderr, "error: %p %p\n", display, ee);
704
705 return (-1);
706 }
707
708 int
709 active_wm(void)
710 {
711 other_wm = 0;
712 xerrorxlib = XSetErrorHandler(xerror_start);
713
714 /* this causes an error if some other window manager is running */
715 XSelectInput(display, DefaultRootWindow(display),
716 SubstructureRedirectMask);
717 XSync(display, False);
718 if(other_wm)
719 return (1);
720
721 XSetErrorHandler(xerror);
722 XSync(display, False);
723 return (0);
724 }
725
726 int
727 main(int argc, char *argv[])
728 {
729 XEvent e;
730 int i;
731
732 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
733 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
734 warnx("no locale support");
735
736 if(!(display = XOpenDisplay(0)))
737 errx(1, "can not open display");
738
739 if (active_wm())
740 errx(1, "other wm running");
741
742 screen = DefaultScreen(display);
743 root = RootWindow(display, screen);
744 width = DisplayWidth(display, screen) - 2;
745 height = DisplayHeight(display, screen) - 2;
746
747 /* make work space 1 active */
748 ws[0].visible = 1;
749 ws[0].restack = 0;
750 ws[0].focus = NULL;
751 ws[0].winno = 0;
752 TAILQ_INIT(&ws[0].winlist);
753 for (i = 1; i < SWM_WS_MAX; i++) {
754 ws[i].visible = 0;
755 ws[i].restack = 0;
756 ws[i].focus = NULL;
757 ws[i].winno = 0;
758 TAILQ_INIT(&ws[i].winlist);
759 }
760
761 /* setup status bar */
762 bar_fs = XLoadQueryFont(display,
763 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*");
764 if (bar_fs == NULL) {
765 /* load a font that is default */
766 bar_fs = XLoadQueryFont(display,
767 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*");
768 if (bar_fs == NULL)
769 errx(1, "couldn't load font");
770 }
771 bar_height = bar_fs->ascent + bar_fs->descent + 3;
772
773 XSelectInput(display, root, SubstructureRedirectMask |
774 SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
775 EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
776 FocusChangeMask | PropertyChangeMask);
777
778 grabkeys();
779
780 bar_window = XCreateSimpleWindow(display, root, 0, 0, width,
781 bar_height - 2, 1, 0x008080, 0x000000);
782 bar_gc = XCreateGC(display, bar_window, 0, &bar_gcv);
783 XSetFont(display, bar_gc, bar_fs->fid);
784 if (bar_enabled) {
785 height -= bar_height; /* correct screen height */
786 XMapWindow(display, bar_window);
787 }
788 bar_print();
789
790 while (running) {
791 XNextEvent(display, &e);
792 if (handler[e.type])
793 handler[e.type](&e);
794 }
795
796 XCloseDisplay(display);
797
798 return (0);
799 }