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