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 #define WIDTH ws[current_ws].g.w
104 #define HEIGHT ws[current_ws].g.h
108 int (*xerrorxlib)(Display *, XErrorEvent *);
112 int ignore_enter = 0;
113 unsigned int numlockmask = 0;
114 unsigned long color_focus = 0xff0000; /* XXX should this be per ws? */
115 unsigned long color_unfocus = 0x888888;
120 double dialog_ratio = .6;
124 unsigned long bar_border = 0x008080;
125 unsigned long bar_color = 0x000000;
126 unsigned long bar_font_color = 0xa0a0a0;
132 char *bar_fonts[] = {
133 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
134 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
138 /* terminal + args */
139 char *spawn_term[] = { "xterm", NULL };
140 char *spawn_menu[] = { "dmenu_run", NULL };
142 /* layout manager data */
143 struct swm_geometry {
152 TAILQ_ENTRY(ws_win) entry;
154 struct swm_geometry g;
157 XWindowAttributes wa;
159 TAILQ_HEAD(ws_win_list, ws_win);
161 /* layout handlers */
163 void vertical_init(int);
164 void vertical_resize(int);
165 void vertical_stack(struct swm_geometry *);
166 void horizontal_init(int);
167 void horizontal_resize(int);
168 void horizontal_stack(struct swm_geometry *);
170 void max_focus(struct ws_win *);
171 void max_stack(struct swm_geometry *);
174 void (*l_init)(int); /* init/reset */
175 void (*l_stack)(struct swm_geometry *);
176 void (*l_resize)(int);
177 void (*l_focus)(struct ws_win *);
179 /* init stack, resize */
180 { vertical_init, vertical_stack, vertical_resize, NULL},
181 { horizontal_init, horizontal_stack, horizontal_resize, NULL},
182 { NULL, max_stack, NULL, max_focus},
183 { NULL, NULL, NULL, NULL},
187 /* define work spaces */
188 #define SWM_WS_MAX (10)
190 int visible; /* workspace visible */
191 int restack; /* restack on switch */
192 struct swm_geometry g;
193 struct layout *cur_layout; /* current layout handlers */
194 struct ws_win *focus; /* which win has focus */
195 struct ws_win_list winlist; /* list of windows in ws */
199 /* args to functions */
202 #define SWM_ARG_ID_FOCUSNEXT (0)
203 #define SWM_ARG_ID_FOCUSPREV (1)
204 #define SWM_ARG_ID_FOCUSMAIN (2)
205 #define SWM_ARG_ID_SWAPNEXT (3)
206 #define SWM_ARG_ID_SWAPPREV (4)
207 #define SWM_ARG_ID_SWAPMAIN (5)
208 #define SWM_ARG_ID_MASTERSHRINK (6)
209 #define SWM_ARG_ID_MASTERGROW (7)
214 name_to_color(char *colorname)
218 XColor screen_def, exact_def;
219 unsigned long result = 0;
220 char cname[32] = "#";
222 cmap = DefaultColormap(display, screen);
223 r = XAllocNamedColor(display, cmap, colorname, &screen_def, &exact_def);
225 strlcat(cname, colorname + 2, sizeof cname - 1);
226 r = XAllocNamedColor(display, cmap, cname, &screen_def,
230 result = screen_def.pixel;
232 fprintf(stderr, "color '%s' not found.\n", colorname);
237 /* conf file stuff */
238 #define SWM_CONF_WS "\n= \t"
239 #define SWM_CONF_FILE "scrotwm.conf"
241 conf_load(char *filename)
244 char *line, *cp, *var, *val;
245 size_t len, lineno = 0;
247 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
249 if (filename == NULL)
252 if ((config = fopen(filename, "r")) == NULL)
256 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
260 cp += (long)strspn(cp, SWM_CONF_WS);
266 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
268 cp += (long)strspn(cp, SWM_CONF_WS);
269 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
272 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
275 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
276 bar_enabled = atoi(val);
277 else if (!strncmp(var, "bar_border",
278 strlen("bar_border")))
279 bar_border = name_to_color(val);
280 else if (!strncmp(var, "bar_color",
281 strlen("bar_color")))
282 bar_color = name_to_color(val);
283 else if (!strncmp(var, "bar_font_color",
284 strlen("bar_font_color")))
285 bar_font_color = name_to_color(val);
286 else if (!strncmp(var, "bar_font", strlen("bar_font")))
287 asprintf(&bar_fonts[0], "%s", val);
293 if (!strncmp(var, "color_focus", strlen("color_focus")))
294 color_focus = name_to_color(val);
295 else if (!strncmp(var, "color_unfocus",
296 strlen("color_unfocus")))
297 color_unfocus = name_to_color(val);
303 if (!strncmp(var, "dialog_ratio",
304 strlen("dialog_ratio"))) {
305 dialog_ratio = atof(val);
306 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
313 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
314 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
325 errx(1, "invalid conf file entry: %s=%s", var, val);
334 if (bar_enabled == 0)
338 XSetForeground(display, bar_gc, bar_color);
339 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
344 localtime_r(&tmt, &tm);
345 strftime(bar_text, sizeof bar_text, "%a %b %d %R %Z %Y", &tm);
346 XSetForeground(display, bar_gc, bar_font_color);
347 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
349 XSync(display, False);
357 /* XXX yeah yeah byte me */
362 bar_toggle(union arg *args)
366 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
369 XUnmapWindow(display, bar_window);
371 XMapRaised(display, bar_window);
372 bar_enabled = !bar_enabled;
373 XSync(display, False);
374 for (i = 0; i < SWM_WS_MAX; i++)
378 bar_print(); /* must be after stack */
386 for (i = 0; bar_fonts[i] != NULL; i++) {
387 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
391 if (bar_fonts[i] == NULL)
392 errx(1, "couldn't load font");
393 bar_height = bar_fs->ascent + bar_fs->descent + 3;
395 bar_window = XCreateSimpleWindow(display, root, 0, 0,
396 WIDTH, bar_height - 2, 1, bar_border, bar_color);
397 bar_gc = XCreateGC(display, bar_window, 0, &bar_gcv);
398 XSetFont(display, bar_gc, bar_fs->fid);
399 XSelectInput(display, bar_window, VisibilityChangeMask);
401 XMapRaised(display, bar_window);
402 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %d\n", (int)bar_window);
404 if (signal(SIGALRM, bar_signal) == SIG_ERR)
405 err(1, "could not install bar_signal");
410 config_win(struct ws_win *win)
414 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
415 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
416 ce.type = ConfigureNotify;
417 ce.display = display;
423 ce.height = win->g.h;
424 ce.border_width = 1; /* XXX store this! */
426 ce.override_redirect = False;
427 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
431 count_win(int wsid, int count_transient)
436 TAILQ_FOREACH (win, &ws[wsid].winlist, entry) {
437 if (count_transient == 0 && win->floating)
439 if (count_transient == 0 && win->transient)
443 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
448 quit(union arg *args)
450 DNPRINTF(SWM_D_MISC, "quit\n");
455 restart(union arg *args)
457 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
459 XCloseDisplay(display);
460 execvp(start_argv[0], start_argv);
461 fprintf(stderr, "execvp failed\n");
467 spawn(union arg *args)
469 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
471 * The double-fork construct avoids zombie processes and keeps the code
472 * clean from stupid signal handlers.
477 close(ConnectionNumber(display));
479 execvp(args->argv[0], args->argv);
480 fprintf(stderr, "execvp failed\n");
489 focus_win(struct ws_win *win)
491 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
492 XSetWindowBorder(display, win->id, color_focus);
493 XSetInputFocus(display, win->id, RevertToPointerRoot, CurrentTime);
494 ws[current_ws].focus = win;
498 unfocus_win(struct ws_win *win)
500 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
501 XSetWindowBorder(display, win->id, color_unfocus);
502 if (ws[current_ws].focus == win)
503 ws[current_ws].focus = NULL;
507 switchws(union arg *args)
512 DNPRINTF(SWM_D_WS, "switchws: %d\n", wsid + 1);
514 if (wsid == current_ws)
517 /* map new window first to prevent ugly blinking */
518 TAILQ_FOREACH (win, &ws[wsid].winlist, entry)
519 XMapRaised(display, win->id);
520 ws[wsid].visible = 1;
522 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry)
523 XUnmapWindow(display, win->id);
524 ws[current_ws].visible = 0;
529 if (ws[wsid].restack) {
533 if (ws[wsid].focus != NULL)
534 focus_win(ws[wsid].focus);
535 XSync(display, False);
540 swapwin(union arg *args)
542 struct ws_win *target;
544 DNPRINTF(SWM_D_MOVE, "swapwin: id %d\n", args->id);
545 if (ws[current_ws].focus == NULL)
549 case SWM_ARG_ID_SWAPPREV:
550 target = TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
551 TAILQ_REMOVE(&ws[current_ws].winlist,
552 ws[current_ws].focus, entry);
554 TAILQ_INSERT_TAIL(&ws[current_ws].winlist,
555 ws[current_ws].focus, entry);
557 TAILQ_INSERT_BEFORE(target, ws[current_ws].focus,
560 case SWM_ARG_ID_SWAPNEXT:
561 target = TAILQ_NEXT(ws[current_ws].focus, entry);
562 TAILQ_REMOVE(&ws[current_ws].winlist,
563 ws[current_ws].focus, entry);
565 TAILQ_INSERT_HEAD(&ws[current_ws].winlist,
566 ws[current_ws].focus, entry);
568 TAILQ_INSERT_AFTER(&ws[current_ws].winlist, target,
569 ws[current_ws].focus, entry);
571 case SWM_ARG_ID_SWAPMAIN:
572 target = TAILQ_FIRST(&ws[current_ws].winlist);
573 if (target == ws[current_ws].focus)
575 TAILQ_REMOVE(&ws[current_ws].winlist, target, entry);
576 TAILQ_INSERT_BEFORE(ws[current_ws].focus, target, entry);
577 TAILQ_REMOVE(&ws[current_ws].winlist,
578 ws[current_ws].focus, entry);
579 TAILQ_INSERT_HEAD(&ws[current_ws].winlist,
580 ws[current_ws].focus, entry);
583 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
592 focus(union arg *args)
594 struct ws_win *winfocus, *winlostfocus;
596 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
597 if (ws[current_ws].focus == NULL)
600 winlostfocus = ws[current_ws].focus;
603 case SWM_ARG_ID_FOCUSPREV:
604 winfocus = TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
605 if (winfocus == NULL)
607 TAILQ_LAST(&ws[current_ws].winlist, ws_win_list);
610 case SWM_ARG_ID_FOCUSNEXT:
611 winfocus = TAILQ_NEXT(ws[current_ws].focus, entry);
612 if (winfocus == NULL)
613 winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
616 case SWM_ARG_ID_FOCUSMAIN:
617 winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
624 if (winfocus == winlostfocus)
627 unfocus_win(winlostfocus);
629 /* XXX if we hook in focus_win(), we get a nasty cycle */
630 if (ws[current_ws].cur_layout->l_focus != NULL)
631 ws[current_ws].cur_layout->l_focus(winfocus);
632 XSync(display, False);
636 cycle_layout(union arg *args)
638 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", current_ws);
640 ws[current_ws].cur_layout++;
641 if (ws[current_ws].cur_layout->l_stack == NULL)
642 ws[current_ws].cur_layout = &layouts[0];
649 resize_master(union arg *args)
651 DNPRINTF(SWM_D_EVENT, "resize_master: id %d\n", args->id);
653 if (ws[current_ws].cur_layout->l_resize != NULL)
654 ws[current_ws].cur_layout->l_resize(args->id);
658 stack_reset(union arg *args)
660 DNPRINTF(SWM_D_EVENT, "stack_reset: ws %d\n", current_ws);
662 if (ws[current_ws].cur_layout->l_init != NULL) {
663 ws[current_ws].cur_layout->l_init(current_ws);
670 struct swm_geometry g;
671 DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws);
673 /* start with workspace geometry, adjust for bar */
674 g = ws[current_ws].g;
680 ws[current_ws].restack = 0;
681 ws[current_ws].cur_layout->l_stack(&g);
682 XSync(display, False);
686 stack_floater(struct ws_win *win)
691 bzero(&wc, sizeof wc);
692 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
694 if (win->transient) {
695 win->g.w = (double)WIDTH * dialog_ratio;
696 win->g.h = (double)HEIGHT * dialog_ratio;
699 wc.height = win->g.h;
700 wc.x = (WIDTH - win->g.w) / 2;
701 wc.y = (HEIGHT - win->g.h) / 2;
703 DNPRINTF(SWM_D_EVENT, "stack_floater: win %d x %d y %d w %d h %d\n",
704 win, wc.x, wc.y, wc.width, wc.height);
706 XConfigureWindow(display, win->id, mask, &wc);
710 int vertical_msize[SWM_WS_MAX];
713 vertical_init(int ws_idx)
715 DNPRINTF(SWM_D_MISC, "vertical_init: workspace: %d\n", current_ws);
717 vertical_msize[ws_idx] = ws[ws_idx].g.w / 2;
721 vertical_resize(int id)
723 DNPRINTF(SWM_D_MISC, "vertical_resize: workspace: %d\n", current_ws);
726 case SWM_ARG_ID_MASTERSHRINK:
727 vertical_msize[current_ws] -= WIDTH / 32;
728 if ( vertical_msize[current_ws] < WIDTH / 16)
729 vertical_msize[current_ws] = WIDTH / 16;
731 case SWM_ARG_ID_MASTERGROW:
732 vertical_msize[current_ws] += WIDTH / 32;
733 if ( vertical_msize[current_ws] >
734 (WIDTH - (WIDTH / 16)))
735 vertical_msize[current_ws] =
745 vertical_stack(struct swm_geometry *g) {
747 struct swm_geometry gg = *g;
748 struct ws_win wf, *win, *winfocus = &wf;
752 DNPRINTF(SWM_D_EVENT, "vertical_stack: workspace: %d\n", current_ws);
754 winno = count_win(current_ws, 0);
760 gg.w = vertical_msize[current_ws];
763 hrh = g->h / (winno - 1);
768 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
770 gg.x += vertical_msize[current_ws] + 2;
771 gg.w = g->w - (vertical_msize[current_ws] + 2);
773 if (i != 0 && hrh != 0) {
774 /* correct the last window for lost pixels */
775 if (win == TAILQ_LAST(&ws[current_ws].winlist,
777 gg.h = hrh + (g->h - (i * hrh));
781 /* leave first right hand window at y = 0 */
787 if (win->transient != 0 || win->floating != 0)
790 bzero(&wc, sizeof wc);
792 win->g.x = wc.x = gg.x;
793 win->g.y = wc.y = gg.y;
794 win->g.w = wc.width = gg.w;
795 win->g.h = wc.height = gg.h;
796 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
797 XConfigureWindow(display, win->id, mask, &wc);
800 if (win == ws[current_ws].focus)
804 XMapRaised(display, win->id);
808 focus_win(winfocus); /* this has to be done outside of the loop */
811 int horizontal_msize[SWM_WS_MAX];
814 horizontal_init(int ws_idx)
816 DNPRINTF(SWM_D_MISC, "horizontal_init: workspace: %d\n", current_ws);
818 horizontal_msize[ws_idx] = ws[ws_idx].g.h / 2;
822 horizontal_resize(int id)
824 DNPRINTF(SWM_D_MISC, "horizontal_resize: workspace: %d\n", current_ws);
827 case SWM_ARG_ID_MASTERSHRINK:
828 horizontal_msize[current_ws] -= HEIGHT / 32;
829 if ( horizontal_msize[current_ws] < HEIGHT / 16)
830 horizontal_msize[current_ws] = HEIGHT / 16;
832 case SWM_ARG_ID_MASTERGROW:
833 horizontal_msize[current_ws] += HEIGHT / 32;
834 if ( horizontal_msize[current_ws] >
835 (HEIGHT - (HEIGHT / 16)))
836 horizontal_msize[current_ws] =
837 HEIGHT - HEIGHT / 16;
846 horizontal_stack(struct swm_geometry *g) {
848 struct swm_geometry gg = *g;
849 struct ws_win wf, *win, *winfocus = &wf;
853 DNPRINTF(SWM_D_EVENT, "horizontal_stack: workspace: %d\n", current_ws);
855 winno = count_win(current_ws, 0);
861 gg.h = horizontal_msize[current_ws];
864 hrw = g->w / (winno - 1);
869 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
871 gg.y += horizontal_msize[current_ws] + 2;
872 gg.h = g->h - (horizontal_msize[current_ws] + 2);
874 if (i != 0 && hrw != 0) {
875 /* correct the last window for lost pixels */
876 if (win == TAILQ_LAST(&ws[current_ws].winlist,
878 gg.w = hrw + (g->w - (i * hrw));
882 /* leave first bottom window at x = 0 */
888 if (win->transient != 0 || win->floating != 0)
891 bzero(&wc, sizeof wc);
893 win->g.x = wc.x = gg.x;
894 win->g.y = wc.y = gg.y;
895 win->g.w = wc.width = gg.w;
896 win->g.h = wc.height = gg.h;
897 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
898 XConfigureWindow(display, win->id, mask, &wc);
901 if (win == ws[current_ws].focus)
905 XMapRaised(display, win->id);
909 focus_win(winfocus); /* this has to be done outside of the loop */
913 max_focus(struct ws_win *fwin)
917 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
918 if (win->transient == 0 && win->floating == 0 && win == fwin)
919 XMapRaised(display, win->id);
921 XUnmapWindow(display, win->id);
925 /* fullscreen view */
927 max_stack(struct swm_geometry *g) {
929 struct swm_geometry gg = *g;
930 struct ws_win *win, *winfocus;
933 DNPRINTF(SWM_D_EVENT, "max_stack: workspace: %d\n", current_ws);
935 if (count_win(current_ws, 0) == 0)
938 winfocus = ws[current_ws].focus;
940 winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
942 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
943 if (win->transient != 0 || win->floating != 0) {
944 if (win == winfocus) {
945 stack_floater(win); /* XXX maximize? */
946 XMapRaised(display, win->id);
948 XUnmapWindow(display, win->id);
950 bzero(&wc, sizeof wc);
952 win->g.x = wc.x = gg.x;
953 win->g.y = wc.y = gg.y;
954 win->g.w = wc.width = gg.w;
955 win->g.h = wc.height = gg.h;
956 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
957 XConfigureWindow(display, win->id, mask, &wc);
960 XMapRaised(display, win->id);
962 XUnmapWindow(display, win->id);
966 focus_win(winfocus); /* this has to be done outside of the loop */
970 send_to_ws(union arg *args)
973 struct ws_win *win = ws[current_ws].focus;
975 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
977 XUnmapWindow(display, win->id);
979 /* find a window to focus */
980 ws[current_ws].focus = TAILQ_PREV(win, ws_win_list, entry);
981 if (ws[current_ws].focus == NULL)
982 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
983 if (ws[current_ws].focus == win)
984 ws[current_ws].focus = NULL;
986 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
988 TAILQ_INSERT_TAIL(&ws[wsid].winlist, win, entry);
989 if (count_win(wsid, 1) == 1)
990 ws[wsid].focus = win;
991 ws[wsid].restack = 1;
996 /* key definitions */
1000 void (*func)(union arg *);
1003 /* modifier key function argument */
1004 { MODKEY, XK_space, cycle_layout, {0} },
1005 { MODKEY | ShiftMask, XK_space, stack_reset, {0} },
1006 { MODKEY, XK_h, resize_master, {.id = SWM_ARG_ID_MASTERSHRINK} },
1007 { MODKEY, XK_l, resize_master, {.id = SWM_ARG_ID_MASTERGROW} },
1008 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1009 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1010 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1011 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1012 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1013 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1014 { MODKEY, XK_p, spawn, {.argv = spawn_menu} },
1015 { MODKEY | ShiftMask, XK_q, quit, {0} },
1016 { MODKEY, XK_q, restart, {0} },
1017 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1018 { MODKEY, XK_1, switchws, {.id = 0} },
1019 { MODKEY, XK_2, switchws, {.id = 1} },
1020 { MODKEY, XK_3, switchws, {.id = 2} },
1021 { MODKEY, XK_4, switchws, {.id = 3} },
1022 { MODKEY, XK_5, switchws, {.id = 4} },
1023 { MODKEY, XK_6, switchws, {.id = 5} },
1024 { MODKEY, XK_7, switchws, {.id = 6} },
1025 { MODKEY, XK_8, switchws, {.id = 7} },
1026 { MODKEY, XK_9, switchws, {.id = 8} },
1027 { MODKEY, XK_0, switchws, {.id = 9} },
1028 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1029 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1030 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1031 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1032 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1033 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1034 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1035 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1036 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1037 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1038 { MODKEY, XK_b, bar_toggle, {0} },
1039 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1040 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1044 updatenumlockmask(void)
1047 XModifierKeymap *modmap;
1049 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1051 modmap = XGetModifierMapping(display);
1052 for (i = 0; i < 8; i++)
1053 for (j = 0; j < modmap->max_keypermod; j++)
1054 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1055 == XKeysymToKeycode(display, XK_Num_Lock))
1056 numlockmask = (1 << i);
1058 XFreeModifiermap(modmap);
1066 unsigned int modifiers[] =
1067 { 0, LockMask, numlockmask, numlockmask | LockMask };
1069 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1070 updatenumlockmask();
1072 XUngrabKey(display, AnyKey, AnyModifier, root);
1073 for (i = 0; i < LENGTH(keys); i++) {
1074 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
1075 for (j = 0; j < LENGTH(modifiers); j++)
1076 XGrabKey(display, code,
1077 keys[i].mod | modifiers[j], root,
1078 True, GrabModeAsync, GrabModeAsync);
1084 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1092 XKeyEvent *ev = &e->xkey;
1094 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1096 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1097 for (i = 0; i < LENGTH(keys); i++)
1098 if (keysym == keys[i].keysym
1099 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1101 keys[i].func(&(keys[i].args));
1105 buttonpress(XEvent *e)
1107 XButtonPressedEvent *ev = &e->xbutton;
1108 #ifdef SWM_CLICKTOFOCUS
1113 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1115 if (ev->window == root)
1117 if (ev->window == ws[current_ws].focus->id)
1119 #ifdef SWM_CLICKTOFOCUS
1120 TAILQ_FOREACH(win, &ws[current_ws].winlist, entry)
1121 if (win->id == ev->window) {
1122 /* focus in the clicked window */
1123 XSetWindowBorder(display, ev->window, 0xff0000);
1124 XSetWindowBorder(display,
1125 ws[current_ws].focus->id, 0x888888);
1126 XSetInputFocus(display, ev->window, RevertToPointerRoot,
1128 ws[current_ws].focus = win;
1129 XSync(display, False);
1136 set_win_state(struct ws_win *win, long state)
1138 long data[] = {state, None};
1140 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1142 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1143 (unsigned char *)data, 2);
1147 manage_window(Window id)
1153 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1155 return (win); /* already being managed */
1158 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1159 errx(1, "calloc: failed to allocate memory for new window");
1162 TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
1163 ws[current_ws].focus = win; /* make new win focused */
1165 XGetTransientForHint(display, win->id, &trans);
1167 win->transient = trans;
1168 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1169 (unsigned)win->id, win->transient);
1171 XGetWindowAttributes(display, win->id, &win->wa);
1172 win->g.w = win->wa.width;
1173 win->g.h = win->wa.height;
1174 win->g.x = win->wa.x;
1175 win->g.y = win->wa.y;
1177 /* XXX make this a table */
1178 bzero(&ch, sizeof ch);
1179 if (XGetClassHint(display, win->id, &ch)) {
1180 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1181 if (!strcmp(ch.res_class, "MPlayer") && !strcmp(ch.res_name, "xv")) {
1185 XFree(ch.res_class);
1190 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
1191 PropertyChangeMask | StructureNotifyMask);
1193 set_win_state(win, NormalState);
1199 configurerequest(XEvent *e)
1201 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1206 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1207 if (win->id == ev->window) {
1214 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
1216 bzero(&wc, sizeof wc);
1219 wc.width = ev->width;
1220 wc.height = ev->height;
1221 wc.border_width = ev->border_width;
1222 wc.sibling = ev->above;
1223 wc.stack_mode = ev->detail;
1224 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
1226 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
1229 if(ev->value_mask & CWX)
1231 if(ev->value_mask & CWY)
1233 if(ev->value_mask & CWWidth)
1234 win->g.w = ev->width;
1235 if(ev->value_mask & CWHeight)
1236 win->g.h = ev->height;
1237 /* this seems to be full screen */
1238 if (win->g.w > WIDTH) {
1243 if (win->g.h > HEIGHT) {
1248 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
1250 XMoveResizeWindow(display, win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1257 configurenotify(XEvent *e)
1259 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1260 e->xconfigure.window);
1264 destroynotify(XEvent *e)
1267 XDestroyWindowEvent *ev = &e->xdestroywindow;
1269 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1271 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1272 if (ev->window == win->id) {
1273 /* find a window to focus */
1274 ws[current_ws].focus = TAILQ_PREV(win,
1275 ws_win_list, entry);
1276 if (ws[current_ws].focus == NULL)
1277 ws[current_ws].focus =
1278 TAILQ_FIRST(&ws[current_ws].winlist);
1279 if (win == ws[current_ws].focus)
1280 ws[current_ws].focus = NULL;
1282 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
1283 set_win_state(win, WithdrawnState);
1293 enternotify(XEvent *e)
1295 XCrossingEvent *ev = &e->xcrossing;
1298 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1300 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
1304 /* eat event(s) to prevent autofocus */
1308 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1309 if (win->id == ev->window)
1319 XFocusChangeEvent *ev = &e->xfocus;
1321 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", ev->window);
1323 if (ev->window == root)
1326 * kill grab for now so that we can cut and paste , this screws up
1330 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1331 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1332 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1337 mappingnotify(XEvent *e)
1339 XMappingEvent *ev = &e->xmapping;
1341 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1343 XRefreshKeyboardMapping(ev);
1344 if (ev->request == MappingKeyboard)
1349 maprequest(XEvent *e)
1351 XMapRequestEvent *ev = &e->xmaprequest;
1352 XWindowAttributes wa;
1354 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1355 e->xmaprequest.window);
1357 if (!XGetWindowAttributes(display, ev->window, &wa))
1359 if (wa.override_redirect)
1361 manage_window(e->xmaprequest.window);
1366 propertynotify(XEvent *e)
1368 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1369 e->xproperty.window);
1373 unmapnotify(XEvent *e)
1375 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1379 visibilitynotify(XEvent *e)
1381 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n", e->xvisibility.window);
1383 if (e->xvisibility.window == bar_window &&
1384 e->xvisibility.state == VisibilityUnobscured)
1388 void (*handler[LASTEvent])(XEvent *) = {
1390 [KeyPress] = keypress,
1391 [ButtonPress] = buttonpress,
1392 [ConfigureRequest] = configurerequest,
1393 [ConfigureNotify] = configurenotify,
1394 [DestroyNotify] = destroynotify,
1395 [EnterNotify] = enternotify,
1396 [FocusIn] = focusin,
1397 [MappingNotify] = mappingnotify,
1398 [MapRequest] = maprequest,
1399 [PropertyNotify] = propertynotify,
1400 [UnmapNotify] = unmapnotify,
1401 [VisibilityNotify] = visibilitynotify,
1405 xerror_start(Display *d, XErrorEvent *ee)
1412 xerror(Display *d, XErrorEvent *ee)
1414 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1422 xerrorxlib = XSetErrorHandler(xerror_start);
1424 /* this causes an error if some other window manager is running */
1425 XSelectInput(display, DefaultRootWindow(display),
1426 SubstructureRedirectMask);
1427 XSync(display, False);
1431 XSetErrorHandler(xerror);
1432 XSync(display, False);
1441 unsigned char *p = NULL;
1442 unsigned long n, extra;
1445 astate = XInternAtom(display, "WM_STATE", False);
1446 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
1447 &real, &format, &n, &extra, (unsigned char **)&p);
1448 if (status != Success)
1457 main(int argc, char *argv[])
1460 char conf[PATH_MAX], *cfile = NULL;
1463 unsigned int i, j, num;
1464 Window d1, d2, *wins = NULL;
1465 XWindowAttributes wa;
1468 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
1469 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
1470 warnx("no locale support");
1472 if (!(display = XOpenDisplay(0)))
1473 errx(1, "can not open display");
1476 errx(1, "other wm running");
1478 screen = DefaultScreen(display);
1479 root = RootWindow(display, screen);
1480 astate = XInternAtom(display, "WM_STATE", False);
1482 /* set default colors */
1483 color_focus = name_to_color("red");
1484 color_unfocus = name_to_color("rgb:88/88/88");
1485 bar_border = name_to_color("rgb:00/80/80");
1486 bar_color = name_to_color("black");
1487 bar_font_color = name_to_color("rgb:a0/a0/a0");
1489 /* look for local and global conf file */
1490 pwd = getpwuid(getuid());
1492 errx(1, "invalid user %d", getuid());
1494 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
1495 if (stat(conf, &sb) != -1) {
1496 if (S_ISREG(sb.st_mode))
1499 /* try global conf file */
1500 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
1501 if (!stat(conf, &sb))
1502 if (S_ISREG(sb.st_mode))
1508 /* init all workspaces */
1509 for (i = 0; i < SWM_WS_MAX; i++) {
1515 ws[i].g.w = DisplayWidth(display, screen) - 2;
1516 ws[i].g.h = DisplayHeight(display, screen) - 2;
1517 TAILQ_INIT(&ws[i].winlist);
1519 for (j = 0; layouts[j].l_stack != NULL; j++) {
1520 if (layouts[j].l_init != NULL)
1521 layouts[j].l_init(i);
1523 ws[i].cur_layout = &layouts[0];
1525 /* make work space 1 active */
1528 /* grab existing windows */
1529 if (XQueryTree(display, root, &d1, &d2, &wins, &num)) {
1530 /* normal windows */
1531 for (i = 0; i < num; i++) {
1532 XGetWindowAttributes(display, wins[i], &wa);
1533 if (!XGetWindowAttributes(display, wins[i], &wa) ||
1534 wa.override_redirect || XGetTransientForHint(display, wins[i], &d1))
1536 if (wa.map_state == IsViewable || getstate(wins[i]) == NormalState)
1537 manage_window(wins[i]);
1539 /* transient windows */
1540 for (i = 0; i < num; i++) {
1541 if (!XGetWindowAttributes(display, wins[i], &wa))
1543 if (XGetTransientForHint(display, wins[i], &d1) &&
1544 (wa.map_state == IsViewable || getstate(wins[i]) ==
1546 manage_window(wins[i]);
1551 ws[0].focus = TAILQ_FIRST(&ws[0].winlist);
1553 /* setup status bar */
1556 XSelectInput(display, root, SubstructureRedirectMask |
1557 SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
1558 EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
1559 FocusChangeMask | PropertyChangeMask | ExposureMask);
1565 XNextEvent(display, &e);
1566 if (handler[e.type])
1567 handler[e.type](&e);
1570 XCloseDisplay(display);