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