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