3 * Copyright (c) 2009 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
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.
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.
19 * Much code and ideas taken from dwm under the following license:
20 * MIT/X Consortium License
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>
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:
39 * The above copyright notice and this permission notice shall be included in
40 * all copies or substantial portions of the Software.
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.
51 #define SWM_VERSION "0.5"
64 #include <sys/types.h>
67 #include <sys/queue.h>
68 #include <sys/param.h>
70 #include <X11/cursorfont.h>
71 #include <X11/keysym.h>
72 #include <X11/Xatom.h>
74 #include <X11/Xproto.h>
75 #include <X11/Xutil.h>
77 /* #define 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 0x0001
82 #define SWM_D_EVENT 0x0002
83 #define SWM_D_WS 0x0004
84 #define SWM_D_FOCUS 0x0008
85 #define SWM_D_MOVE 0x0010
87 u_int32_t swm_debug = 0
96 #define DNPRINTF(n,x...)
99 #define LENGTH(x) (sizeof x / sizeof x[0])
100 #define MODKEY Mod1Mask
101 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
103 int (*xerrorxlib)(Display *, XErrorEvent *);
107 int ignore_enter = 0;
108 unsigned int numlockmask = 0;
109 unsigned long color_focus = 0xff0000; /* XXX should this be per ws? */
110 unsigned long color_unfocus = 0x888888;
117 unsigned long bar_border = 0x008080;
118 unsigned long bar_color = 0x000000;
119 unsigned long bar_font_color = 0xa0a0a0;
125 char *bar_fonts[] = {
126 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
127 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
131 /* terminal + args */
132 char *spawn_term[] = { "xterm", NULL };
133 char *spawn_menu[] = { "dmenu_run", NULL };
134 char *spawn_scrotwm[] = { "scrotwm", NULL };
136 /* layout manager data */
137 struct swm_geometry {
146 TAILQ_ENTRY(ws_win) entry;
148 struct swm_geometry g;
151 XWindowAttributes wa;
154 TAILQ_HEAD(ws_win_list, ws_win);
157 /* layout handlers */
159 void vertical_init(int);
160 void vertical_resize(int);
161 void vertical_stack(struct swm_geometry *);
162 void horizontal_init(int);
163 void horizontal_resize(int);
164 void horizontal_stack(struct swm_geometry *);
166 void max_focus(struct ws_win *);
167 void max_stack(struct swm_geometry *);
170 void (*l_init)(int); /* init/reset */
171 void (*l_stack)(struct swm_geometry *);
172 void (*l_resize)(int);
173 void (*l_focus)(struct ws_win *);
175 /* init stack, resize */
176 { vertical_init, vertical_stack, vertical_resize, NULL},
177 { horizontal_init, horizontal_stack, horizontal_resize, NULL},
178 { NULL, max_stack, NULL, max_focus},
179 { NULL, NULL, NULL, NULL},
183 /* define work spaces */
184 #define SWM_WS_MAX (10)
186 int visible; /* workspace visible */
187 int restack; /* restack on switch */
188 struct swm_geometry g;
189 struct layout *cur_layout; /* current layout handlers */
190 struct ws_win *focus; /* which win has focus */
191 struct ws_win_list winlist; /* list of windows in ws */
195 /* args to functions */
198 #define SWM_ARG_ID_FOCUSNEXT (0)
199 #define SWM_ARG_ID_FOCUSPREV (1)
200 #define SWM_ARG_ID_FOCUSMAIN (2)
201 #define SWM_ARG_ID_SWAPNEXT (3)
202 #define SWM_ARG_ID_SWAPPREV (4)
203 #define SWM_ARG_ID_SWAPMAIN (5)
204 #define SWM_ARG_ID_MASTERSHRINK (6)
205 #define SWM_ARG_ID_MASTERGROW (7)
209 /* conf file stuff */
210 #define SWM_CONF_WS "\n= \t"
211 #define SWM_CONF_FILE "scrotwm.conf"
213 conf_load(char *filename)
216 char *line, *cp, *var, *val;
217 size_t len, lineno = 0;
219 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
221 if (filename == NULL)
224 if ((config = fopen(filename, "r")) == NULL)
228 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
232 cp += (long)strspn(cp, SWM_CONF_WS);
238 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
240 cp += (long)strspn(cp, SWM_CONF_WS);
241 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
244 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
247 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
248 bar_enabled = atoi(val);
249 else if (!strncmp(var, "bar_border",
250 strlen("bar_border")))
251 bar_border = strtol(val, NULL, 16);
252 else if (!strncmp(var, "bar_color",
253 strlen("bar_color")))
254 bar_color = strtol(val, NULL, 16);
255 else if (!strncmp(var, "bar_font_color",
256 strlen("bar_font_color")))
257 bar_font_color = strtol(val, NULL, 16);
258 else if (!strncmp(var, "bar_font", strlen("bar_font")))
259 asprintf(&bar_fonts[0], "%s", val);
265 if (!strncmp(var, "color_focus", strlen("color_focus")))
266 color_focus = strtol(val, NULL, 16);
267 else if (!strncmp(var, "color_unfocus",
268 strlen("color_unfocus")))
269 color_unfocus = strtol(val, NULL, 16);
275 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
276 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
287 errx(1, "invalid conf file entry: %s=%s", var, val);
296 if (bar_enabled == 0)
300 XSetForeground(display, bar_gc, bar_color);
301 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
306 localtime_r(&tmt, &tm);
307 strftime(bar_text, sizeof bar_text, "%a %b %d %R %Z %Y", &tm);
308 XSetForeground(display, bar_gc, bar_font_color);
309 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
311 XSync(display, False);
319 /* XXX yeah yeah byte me */
324 bar_toggle(union arg *args)
328 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
332 XUnmapWindow(display, bar_window);
335 XMapWindow(display, bar_window);
337 XSync(display, False);
338 for (i = 0; i < SWM_WS_MAX; i++)
342 bar_print(); /* must be after stack */
350 for (i = 0; bar_fonts[i] != NULL; i++) {
351 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
355 if (bar_fonts[i] == NULL)
356 errx(1, "couldn't load font");
357 bar_height = bar_fs->ascent + bar_fs->descent + 3;
359 bar_window = XCreateSimpleWindow(display, root, 0, 0,
360 ws[current_ws].g.w, bar_height - 2, 1, bar_border, bar_color);
361 bar_gc = XCreateGC(display, bar_window, 0, &bar_gcv);
362 XSetFont(display, bar_gc, bar_fs->fid);
363 XSelectInput(display, bar_window, VisibilityChangeMask);
365 XMapWindow(display, bar_window);
367 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %d\n", (int)bar_window);
369 if (signal(SIGALRM, bar_signal) == SIG_ERR)
370 err(1, "could not install bar_signal");
375 count_win(int wsid, int count_transient)
380 TAILQ_FOREACH (win, &ws[wsid].winlist, entry) {
381 if (count_transient == 0 && win->transient)
385 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
390 quit(union arg *args)
392 DNPRINTF(SWM_D_MISC, "quit\n");
398 restart(union arg *args)
400 XCloseDisplay(display);
401 execvp(args->argv[0], args->argv);
402 fprintf(stderr, "execvp failed\n");
408 spawn(union arg *args)
410 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
412 * The double-fork construct avoids zombie processes and keeps the code
413 * clean from stupid signal handlers.
418 close(ConnectionNumber(display));
420 execvp(args->argv[0], args->argv);
421 fprintf(stderr, "execvp failed\n");
430 focus_win(struct ws_win *win)
432 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
433 XSetWindowBorder(display, win->id, color_focus);
434 XSetInputFocus(display, win->id, RevertToPointerRoot, CurrentTime);
435 ws[current_ws].focus = win;
439 unfocus_win(struct ws_win *win)
441 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
442 XSetWindowBorder(display, win->id, color_unfocus);
443 if (ws[current_ws].focus == win)
444 ws[current_ws].focus = NULL;
448 switchws(union arg *args)
453 DNPRINTF(SWM_D_WS, "switchws: %d\n", wsid + 1);
455 if (wsid == current_ws)
458 /* map new window first to prevent ugly blinking */
459 TAILQ_FOREACH (win, &ws[wsid].winlist, entry)
460 XMapRaised(display, win->id);
461 ws[wsid].visible = 1;
463 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry)
464 XUnmapWindow(display, win->id);
465 ws[current_ws].visible = 0;
470 if (ws[wsid].restack) {
474 if (ws[wsid].focus != NULL)
475 focus_win(ws[wsid].focus);
476 XSync(display, False);
481 swapwin(union arg *args)
483 struct ws_win *target;
485 DNPRINTF(SWM_D_MOVE, "swapwin: id %d\n", args->id);
486 if (ws[current_ws].focus == NULL)
490 case SWM_ARG_ID_SWAPPREV:
491 target = TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
492 TAILQ_REMOVE(&ws[current_ws].winlist,
493 ws[current_ws].focus, entry);
495 TAILQ_INSERT_TAIL(&ws[current_ws].winlist,
496 ws[current_ws].focus, entry);
498 TAILQ_INSERT_BEFORE(target, ws[current_ws].focus,
501 case SWM_ARG_ID_SWAPNEXT:
502 target = TAILQ_NEXT(ws[current_ws].focus, entry);
503 TAILQ_REMOVE(&ws[current_ws].winlist,
504 ws[current_ws].focus, entry);
506 TAILQ_INSERT_HEAD(&ws[current_ws].winlist,
507 ws[current_ws].focus, entry);
509 TAILQ_INSERT_AFTER(&ws[current_ws].winlist, target,
510 ws[current_ws].focus, entry);
512 case SWM_ARG_ID_SWAPMAIN:
513 target = TAILQ_FIRST(&ws[current_ws].winlist);
514 if (target == ws[current_ws].focus)
516 TAILQ_REMOVE(&ws[current_ws].winlist, target, entry);
517 TAILQ_INSERT_BEFORE(ws[current_ws].focus, target, entry);
518 TAILQ_REMOVE(&ws[current_ws].winlist,
519 ws[current_ws].focus, entry);
520 TAILQ_INSERT_HEAD(&ws[current_ws].winlist,
521 ws[current_ws].focus, entry);
524 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
533 focus(union arg *args)
535 struct ws_win *winfocus, *winlostfocus;
537 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
538 if (ws[current_ws].focus == NULL)
541 winlostfocus = ws[current_ws].focus;
544 case SWM_ARG_ID_FOCUSPREV:
545 winfocus = TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
546 if (winfocus == NULL)
548 TAILQ_LAST(&ws[current_ws].winlist, ws_win_list);
551 case SWM_ARG_ID_FOCUSNEXT:
552 winfocus = TAILQ_NEXT(ws[current_ws].focus, entry);
553 if (winfocus == NULL)
554 winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
557 case SWM_ARG_ID_FOCUSMAIN:
558 winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
565 if (winfocus == winlostfocus)
568 unfocus_win(winlostfocus);
570 /* XXX if we hook in focus_win(), we get a nasty cycle */
571 if (ws[current_ws].cur_layout->l_focus != NULL)
572 ws[current_ws].cur_layout->l_focus(winfocus);
573 XSync(display, False);
577 cycle_layout(union arg *args)
579 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", current_ws);
581 ws[current_ws].cur_layout++;
582 if (ws[current_ws].cur_layout->l_stack == NULL)
583 ws[current_ws].cur_layout = &layouts[0];
589 resize_master(union arg *args)
591 DNPRINTF(SWM_D_EVENT, "resize_master: id %d\n", args->id);
593 if (ws[current_ws].cur_layout->l_resize != NULL)
594 ws[current_ws].cur_layout->l_resize(args->id);
598 stack_reset(union arg *args)
600 DNPRINTF(SWM_D_EVENT, "stack_reset: ws %d\n", current_ws);
602 if (ws[current_ws].cur_layout->l_init != NULL) {
603 ws[current_ws].cur_layout->l_init(current_ws);
610 struct swm_geometry g;
611 DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws);
613 /* start with workspace geometry, adjust for bar */
614 g = ws[current_ws].g;
620 ws[current_ws].restack = 0;
621 ws[current_ws].cur_layout->l_stack(&g);
622 XSync(display, False);
626 stack_floater(struct ws_win *win)
631 bzero(&wc, sizeof wc);
633 mask = CWX | CWY | CWBorderWidth;
635 /* use obsolete width height */
636 if (win->sh.flags & USPosition) {
637 win->g.w = wc.width = win->sh.width;
638 win->g.h = wc.height = win->sh.height;
639 mask |= CWWidth | CWHeight;
643 if (win->sh.flags & PMinSize) {
644 /* some hints are retarded */
645 if (win->sh.min_width < ws[current_ws].g.w / 10)
646 win->sh.min_width = ws[current_ws].g.w / 3;
647 if (win->sh.min_height < ws[current_ws].g.h / 10)
648 win->wa.height = ws[current_ws].g.h / 3;
650 win->g.w = wc.width = win->sh.min_width * 2;
651 win->g.h = wc.height = win->sh.min_height * 2;
652 mask |= CWWidth | CWHeight;
654 if (win->sh.flags & PMaxSize) {
655 /* potentially override min values */
656 if (win->sh.max_width < ws[current_ws].g.w) {
657 win->g.w = wc.width = win->sh.max_width;
660 if (win->sh.max_height < ws[current_ws].g.h) {
661 win->g.h = wc.height = win->sh.max_height;
666 /* make sure we don't clobber the screen */
667 if ((mask & CWWidth) && win->wa.width > ws[current_ws].g.w)
668 win->wa.width = ws[current_ws].g.w - 4;
669 if ((mask & CWHeight) && win->wa.height > ws[current_ws].g.h)
670 win->wa.height = ws[current_ws].g.h - 4;
672 /* supposed to be obsolete */
673 if (win->sh.flags & USPosition) {
674 win->g.x = wc.x = win->sh.x;
675 win->g.y = wc.y = win->sh.y;
677 win->g.x = wc.x = (ws[current_ws].g.w - win->wa.width) / 2;
678 win->g.y = wc.y = (ws[current_ws].g.h - win->wa.height) / 2;
680 DNPRINTF(SWM_D_EVENT, "stack_floater: win %d x %d y %d w %d h %d\n",
681 win, wc.x, wc.y, wc.width, wc.height);
683 XConfigureWindow(display, win->id, mask, &wc);
687 int vertical_msize[SWM_WS_MAX];
690 vertical_init(int ws_idx)
692 DNPRINTF(SWM_D_MISC, "vertical_init: workspace: %d\n", current_ws);
694 vertical_msize[ws_idx] = ws[ws_idx].g.w / 2;
698 vertical_resize(int id)
700 DNPRINTF(SWM_D_MISC, "vertical_resize: workspace: %d\n", current_ws);
703 case SWM_ARG_ID_MASTERSHRINK:
704 vertical_msize[current_ws] -= ws[current_ws].g.w / 32;
705 if ( vertical_msize[current_ws] < ws[current_ws].g.w / 16)
706 vertical_msize[current_ws] = ws[current_ws].g.w / 16;
708 case SWM_ARG_ID_MASTERGROW:
709 vertical_msize[current_ws] += ws[current_ws].g.w / 32;
710 if ( vertical_msize[current_ws] >
711 (ws[current_ws].g.w - (ws[current_ws].g.w / 16)))
712 vertical_msize[current_ws] =
713 ws[current_ws].g.w - ws[current_ws].g.w / 16;
722 vertical_stack(struct swm_geometry *g) {
724 struct swm_geometry gg = *g;
725 struct ws_win wf, *win, *winfocus = &wf;
729 DNPRINTF(SWM_D_EVENT, "vertical_stack: workspace: %d\n", current_ws);
733 winno = count_win(current_ws, 0);
738 gg.w = vertical_msize[current_ws];
741 hrh = g->h / (winno - 1);
746 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
748 gg.x += vertical_msize[current_ws] + 2;
749 gg.w = g->w - (vertical_msize[current_ws] + 2);
751 if (i != 0 && hrh != 0) {
752 /* correct the last window for lost pixels */
753 if (win == TAILQ_LAST(&ws[current_ws].winlist,
755 gg.h = hrh + (g->h - (i * hrh));
759 /* leave first right hand window at y = 0 */
765 if (win->transient != 0 || win->floating != 0)
768 bzero(&wc, sizeof wc);
770 win->g.x = wc.x = gg.x;
771 win->g.y = wc.y = gg.y;
772 win->g.w = wc.width = gg.w;
773 win->g.h = wc.height = gg.h;
774 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
775 XConfigureWindow(display, win->id, mask, &wc);
778 if (win == ws[current_ws].focus)
782 XMapRaised(display, win->id);
786 focus_win(winfocus); /* this has to be done outside of the loop */
789 int horizontal_msize[SWM_WS_MAX];
792 horizontal_init(int ws_idx)
794 DNPRINTF(SWM_D_MISC, "horizontal_init: workspace: %d\n", current_ws);
796 horizontal_msize[ws_idx] = ws[ws_idx].g.h / 2;
800 horizontal_resize(int id)
802 DNPRINTF(SWM_D_MISC, "horizontal_resize: workspace: %d\n", current_ws);
805 case SWM_ARG_ID_MASTERSHRINK:
806 horizontal_msize[current_ws] -= ws[current_ws].g.h / 32;
807 if ( horizontal_msize[current_ws] < ws[current_ws].g.h / 16)
808 horizontal_msize[current_ws] = ws[current_ws].g.h / 16;
810 case SWM_ARG_ID_MASTERGROW:
811 horizontal_msize[current_ws] += ws[current_ws].g.h / 32;
812 if ( horizontal_msize[current_ws] >
813 (ws[current_ws].g.h - (ws[current_ws].g.h / 16)))
814 horizontal_msize[current_ws] =
815 ws[current_ws].g.h - ws[current_ws].g.h / 16;
824 horizontal_stack(struct swm_geometry *g) {
826 struct swm_geometry gg = *g;
827 struct ws_win wf, *win, *winfocus = &wf;
831 DNPRINTF(SWM_D_EVENT, "horizontal_stack: workspace: %d\n", current_ws);
835 winno = count_win(current_ws, 0);
840 gg.h = horizontal_msize[current_ws];
843 hrw = g->w / (winno - 1);
848 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
850 gg.y += horizontal_msize[current_ws] + 2;
851 gg.h = g->h - (horizontal_msize[current_ws] + 2);
853 if (i != 0 && hrw != 0) {
854 /* correct the last window for lost pixels */
855 if (win == TAILQ_LAST(&ws[current_ws].winlist,
857 gg.w = hrw + (g->w - (i * hrw));
861 /* leave first bottom window at x = 0 */
867 if (win->transient != 0 || win->floating != 0)
870 bzero(&wc, sizeof wc);
872 win->g.x = wc.x = gg.x;
873 win->g.y = wc.y = gg.y;
874 win->g.w = wc.width = gg.w;
875 win->g.h = wc.height = gg.h;
876 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
877 XConfigureWindow(display, win->id, mask, &wc);
880 if (win == ws[current_ws].focus)
884 XMapRaised(display, win->id);
888 focus_win(winfocus); /* this has to be done outside of the loop */
892 max_focus(struct ws_win *fwin)
896 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
897 if (win->transient == 0 && win->floating == 0 && win == fwin)
898 XMapRaised(display, win->id);
900 XUnmapWindow(display, win->id);
904 /* fullscreen view */
906 max_stack(struct swm_geometry *g) {
908 struct swm_geometry gg = *g;
909 struct ws_win *win, *winfocus;
910 int i, found = 0, winno;
913 DNPRINTF(SWM_D_EVENT, "max_stack: workspace: %d\n", current_ws);
915 winno = count_win(current_ws, 0);
919 winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
921 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
922 if (win->transient != 0 || win->floating != 0) {
925 bzero(&wc, sizeof wc);
927 win->g.x = wc.x = gg.x;
928 win->g.y = wc.y = gg.y;
929 win->g.w = wc.width = gg.w;
930 win->g.h = wc.height = gg.h;
931 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
932 XConfigureWindow(display, win->id, mask, &wc);
936 XMapRaised(display, win->id);
938 /* hide all but the master window */
939 XUnmapWindow(display, win->id);
942 if (win == ws[current_ws].focus)
949 focus_win(winfocus); /* this has to be done outside of the loop */
953 send_to_ws(union arg *args)
956 struct ws_win *win = ws[current_ws].focus;
958 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
960 XUnmapWindow(display, win->id);
962 /* find a window to focus */
963 ws[current_ws].focus = TAILQ_PREV(win, ws_win_list, entry);
964 if (ws[current_ws].focus == NULL)
965 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
966 if (ws[current_ws].focus == win)
967 ws[current_ws].focus = NULL;
969 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
971 TAILQ_INSERT_TAIL(&ws[wsid].winlist, win, entry);
972 if (count_win(wsid, 1) == 1)
973 ws[wsid].focus = win;
974 ws[wsid].restack = 1;
979 /* key definitions */
983 void (*func)(union arg *);
986 /* modifier key function argument */
987 { MODKEY, XK_space, cycle_layout, {0} },
988 { MODKEY | ShiftMask, XK_space, stack_reset, {0} },
989 { MODKEY, XK_h, resize_master, {.id = SWM_ARG_ID_MASTERSHRINK} },
990 { MODKEY, XK_l, resize_master, {.id = SWM_ARG_ID_MASTERGROW} },
991 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
992 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
993 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
994 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
995 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
996 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
997 { MODKEY, XK_p, spawn, {.argv = spawn_menu} },
998 { MODKEY | ShiftMask, XK_q, quit, {0} },
999 { MODKEY, XK_q, restart, {.argv = spawn_scrotwm } },
1000 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1001 { MODKEY, XK_1, switchws, {.id = 0} },
1002 { MODKEY, XK_2, switchws, {.id = 1} },
1003 { MODKEY, XK_3, switchws, {.id = 2} },
1004 { MODKEY, XK_4, switchws, {.id = 3} },
1005 { MODKEY, XK_5, switchws, {.id = 4} },
1006 { MODKEY, XK_6, switchws, {.id = 5} },
1007 { MODKEY, XK_7, switchws, {.id = 6} },
1008 { MODKEY, XK_8, switchws, {.id = 7} },
1009 { MODKEY, XK_9, switchws, {.id = 8} },
1010 { MODKEY, XK_0, switchws, {.id = 9} },
1011 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1012 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1013 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1014 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1015 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1016 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1017 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1018 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1019 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1020 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1021 { MODKEY, XK_b, bar_toggle, {0} },
1022 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1023 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1027 updatenumlockmask(void)
1030 XModifierKeymap *modmap;
1032 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1034 modmap = XGetModifierMapping(display);
1035 for (i = 0; i < 8; i++)
1036 for (j = 0; j < modmap->max_keypermod; j++)
1037 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1038 == XKeysymToKeycode(display, XK_Num_Lock))
1039 numlockmask = (1 << i);
1041 XFreeModifiermap(modmap);
1049 unsigned int modifiers[] =
1050 { 0, LockMask, numlockmask, numlockmask | LockMask };
1052 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1053 updatenumlockmask();
1055 XUngrabKey(display, AnyKey, AnyModifier, root);
1056 for(i = 0; i < LENGTH(keys); i++) {
1057 if((code = XKeysymToKeycode(display, keys[i].keysym)))
1058 for(j = 0; j < LENGTH(modifiers); j++)
1059 XGrabKey(display, code,
1060 keys[i].mod | modifiers[j], root,
1061 True, GrabModeAsync, GrabModeAsync);
1067 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1075 XKeyEvent *ev = &e->xkey;
1077 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1079 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1080 for(i = 0; i < LENGTH(keys); i++)
1081 if(keysym == keys[i].keysym
1082 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1084 keys[i].func(&(keys[i].args));
1088 buttonpress(XEvent *e)
1090 XButtonPressedEvent *ev = &e->xbutton;
1091 #ifdef SWM_CLICKTOFOCUS
1096 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1098 if (ev->window == root)
1100 if (ev->window == ws[current_ws].focus->id)
1102 #ifdef SWM_CLICKTOFOCUS
1103 TAILQ_FOREACH(win, &ws[current_ws].winlist, entry)
1104 if (win->id == ev->window) {
1105 /* focus in the clicked window */
1106 XSetWindowBorder(display, ev->window, 0xff0000);
1107 XSetWindowBorder(display,
1108 ws[current_ws].focus->id, 0x888888);
1109 XSetInputFocus(display, ev->window, RevertToPointerRoot,
1111 ws[current_ws].focus = win;
1112 XSync(display, False);
1119 manage_window(Window id)
1124 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1126 return (win); /* already being managed */
1129 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1130 errx(1, "calloc: failed to allocate memory for new window");
1133 TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
1135 XGetTransientForHint(display, win->id, &trans);
1137 win->transient = trans;
1138 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1139 (unsigned)win->id, win->transient);
1141 XGetWindowAttributes(display, win->id, &win->wa);
1142 XGetNormalHints(display, win->id, &win->sh);
1144 XSelectInput(display, id, ButtonPressMask | EnterWindowMask |
1145 FocusChangeMask | ExposureMask);
1151 configurerequest(XEvent *e)
1153 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1156 DNPRINTF(SWM_D_EVENT, "configurerequest: window: %lu\n", ev->window);
1159 win = manage_window(ev->window);
1160 ws[current_ws].focus = win; /* make new win focused */
1163 XClassHint ch = { 0 };
1164 if(XGetClassHint(display, win->id, &ch)) {
1165 fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name);
1166 if (!strcmp(ch.res_class, "Gvim") && !strcmp(ch.res_name, "gvim")) {
1170 XFree(ch.res_class);
1179 configurenotify(XEvent *e)
1182 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1183 e->xconfigure.window);
1187 destroynotify(XEvent *e)
1190 XDestroyWindowEvent *ev = &e->xdestroywindow;
1192 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1194 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1195 if (ev->window == win->id) {
1196 /* find a window to focus */
1197 ws[current_ws].focus = TAILQ_PREV(win,
1198 ws_win_list, entry);
1199 if (ws[current_ws].focus == NULL)
1200 ws[current_ws].focus =
1201 TAILQ_FIRST(&ws[current_ws].winlist);
1202 if (win == ws[current_ws].focus)
1203 ws[current_ws].focus = NULL;
1205 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
1215 enternotify(XEvent *e)
1217 XCrossingEvent *ev = &e->xcrossing;
1220 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1222 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
1226 /* eat event(s) to prevent autofocus */
1230 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1231 if (win->id == ev->window)
1241 XFocusChangeEvent *ev = &e->xfocus;
1243 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", ev->window);
1245 XSync(display, False); /* not sure this helps redrawing graphic apps */
1247 if (ev->window == root)
1250 * kill grab for now so that we can cut and paste , this screws up
1254 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1255 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1256 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1261 mappingnotify(XEvent *e)
1263 XMappingEvent *ev = &e->xmapping;
1265 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1267 XRefreshKeyboardMapping(ev);
1268 if(ev->request == MappingKeyboard)
1273 maprequest(XEvent *e)
1275 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1276 e->xmaprequest.window);
1278 manage_window(e->xmaprequest.window);
1279 XMapRaised(display, e->xmaprequest.window);
1283 propertynotify(XEvent *e)
1285 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1286 e->xproperty.window);
1290 unmapnotify(XEvent *e)
1292 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1296 visibilitynotify(XEvent *e)
1298 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n", e->xvisibility.window);
1300 if (e->xvisibility.window == bar_window &&
1301 e->xvisibility.state == VisibilityUnobscured)
1305 void (*handler[LASTEvent])(XEvent *) = {
1307 [KeyPress] = keypress,
1308 [ButtonPress] = buttonpress,
1309 [ConfigureRequest] = configurerequest,
1310 [ConfigureNotify] = configurenotify,
1311 [DestroyNotify] = destroynotify,
1312 [EnterNotify] = enternotify,
1313 [FocusIn] = focusin,
1314 [MappingNotify] = mappingnotify,
1315 [MapRequest] = maprequest,
1316 [PropertyNotify] = propertynotify,
1317 [UnmapNotify] = unmapnotify,
1318 [VisibilityNotify] = visibilitynotify,
1322 xerror_start(Display *d, XErrorEvent *ee)
1329 xerror(Display *d, XErrorEvent *ee)
1331 fprintf(stderr, "error: %p %p\n", display, ee);
1340 xerrorxlib = XSetErrorHandler(xerror_start);
1342 /* this causes an error if some other window manager is running */
1343 XSelectInput(display, DefaultRootWindow(display),
1344 SubstructureRedirectMask);
1345 XSync(display, False);
1349 XSetErrorHandler(xerror);
1350 XSync(display, False);
1355 main(int argc, char *argv[])
1358 char conf[PATH_MAX], *cfile = NULL;
1361 unsigned int i, j, num;
1362 Window d1, d2, *wins = NULL;
1363 XWindowAttributes wa;
1365 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
1366 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
1367 warnx("no locale support");
1369 if(!(display = XOpenDisplay(0)))
1370 errx(1, "can not open display");
1373 errx(1, "other wm running");
1375 screen = DefaultScreen(display);
1376 root = RootWindow(display, screen);
1378 /* look for local and global conf file */
1379 pwd = getpwuid(getuid());
1381 errx(1, "invalid user %d", getuid());
1383 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
1384 if (stat(conf, &sb) != -1) {
1385 if (S_ISREG(sb.st_mode))
1388 /* try global conf file */
1389 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
1390 if (!stat(conf, &sb))
1391 if (S_ISREG(sb.st_mode))
1397 /* init all workspaces */
1398 for (i = 0; i < SWM_WS_MAX; i++) {
1404 ws[i].g.w = DisplayWidth(display, screen) - 2;
1405 ws[i].g.h = DisplayHeight(display, screen) - 2;
1406 TAILQ_INIT(&ws[i].winlist);
1408 for (j = 0; layouts[j].l_stack != NULL; j++) {
1409 if (layouts[j].l_init != NULL)
1410 layouts[j].l_init(i);
1412 ws[i].cur_layout = &layouts[0];
1414 /* make work space 1 active */
1417 /* grab existing windows */
1418 if (XQueryTree(display, root, &d1, &d2, &wins, &num)) {
1419 for (i = 0; i < num; i++) {
1420 if (!XGetWindowAttributes(display, wins[i], &wa)
1421 || wa.override_redirect ||
1422 XGetTransientForHint(display, wins[i], &d1))
1424 manage_window(wins[i]);
1429 ws[0].focus = TAILQ_FIRST(&ws[0].winlist);
1431 /* setup status bar */
1434 XSelectInput(display, root, SubstructureRedirectMask |
1435 SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
1436 EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
1437 FocusChangeMask | PropertyChangeMask | ExposureMask);
1443 XNextEvent(display, &e);
1444 if (handler[e.type])
1445 handler[e.type](&e);
1448 XCloseDisplay(display);