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)
213 /* conf file stuff */
214 #define SWM_CONF_WS "\n= \t"
215 #define SWM_CONF_FILE "scrotwm.conf"
217 conf_load(char *filename)
220 char *line, *cp, *var, *val;
221 size_t len, lineno = 0;
223 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
225 if (filename == NULL)
228 if ((config = fopen(filename, "r")) == NULL)
232 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
236 cp += (long)strspn(cp, SWM_CONF_WS);
242 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
244 cp += (long)strspn(cp, SWM_CONF_WS);
245 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
248 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
251 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
252 bar_enabled = atoi(val);
253 else if (!strncmp(var, "bar_border",
254 strlen("bar_border")))
255 bar_border = strtol(val, NULL, 16);
256 else if (!strncmp(var, "bar_color",
257 strlen("bar_color")))
258 bar_color = strtol(val, NULL, 16);
259 else if (!strncmp(var, "bar_font_color",
260 strlen("bar_font_color")))
261 bar_font_color = strtol(val, NULL, 16);
262 else if (!strncmp(var, "bar_font", strlen("bar_font")))
263 asprintf(&bar_fonts[0], "%s", val);
269 if (!strncmp(var, "color_focus", strlen("color_focus")))
270 color_focus = strtol(val, NULL, 16);
271 else if (!strncmp(var, "color_unfocus",
272 strlen("color_unfocus")))
273 color_unfocus = strtol(val, NULL, 16);
279 if (!strncmp(var, "dialog_ratio",
280 strlen("dialog_ratio"))) {
281 dialog_ratio = atof(val);
282 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
289 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
290 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
301 errx(1, "invalid conf file entry: %s=%s", var, val);
310 if (bar_enabled == 0)
314 XSetForeground(display, bar_gc, bar_color);
315 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
320 localtime_r(&tmt, &tm);
321 strftime(bar_text, sizeof bar_text, "%a %b %d %R %Z %Y", &tm);
322 XSetForeground(display, bar_gc, bar_font_color);
323 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
325 XSync(display, False);
333 /* XXX yeah yeah byte me */
338 bar_toggle(union arg *args)
342 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
346 XUnmapWindow(display, bar_window);
349 XMapRaised(display, bar_window);
351 XSync(display, False);
352 for (i = 0; i < SWM_WS_MAX; i++)
356 bar_print(); /* must be after stack */
364 for (i = 0; bar_fonts[i] != NULL; i++) {
365 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
369 if (bar_fonts[i] == NULL)
370 errx(1, "couldn't load font");
371 bar_height = bar_fs->ascent + bar_fs->descent + 3;
373 bar_window = XCreateSimpleWindow(display, root, 0, 0,
374 WIDTH, bar_height - 2, 1, bar_border, bar_color);
375 bar_gc = XCreateGC(display, bar_window, 0, &bar_gcv);
376 XSetFont(display, bar_gc, bar_fs->fid);
377 XSelectInput(display, bar_window, VisibilityChangeMask);
379 XMapRaised(display, bar_window);
381 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %d\n", (int)bar_window);
383 if (signal(SIGALRM, bar_signal) == SIG_ERR)
384 err(1, "could not install bar_signal");
389 config_win(struct ws_win *win)
393 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
394 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
395 ce.type = ConfigureNotify;
396 ce.display = display;
402 ce.height = win->g.h;
403 ce.border_width = 1; /* XXX store this! */
405 ce.override_redirect = False;
406 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
410 count_win(int wsid, int count_transient)
415 TAILQ_FOREACH (win, &ws[wsid].winlist, entry) {
416 if (count_transient == 0 && win->floating)
418 if (count_transient == 0 && win->transient)
422 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
427 quit(union arg *args)
429 DNPRINTF(SWM_D_MISC, "quit\n");
434 restart(union arg *args)
436 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
438 XCloseDisplay(display);
439 execvp(start_argv[0], start_argv);
440 fprintf(stderr, "execvp failed\n");
446 spawn(union arg *args)
448 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
450 * The double-fork construct avoids zombie processes and keeps the code
451 * clean from stupid signal handlers.
456 close(ConnectionNumber(display));
458 execvp(args->argv[0], args->argv);
459 fprintf(stderr, "execvp failed\n");
468 focus_win(struct ws_win *win)
470 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
471 XSetWindowBorder(display, win->id, color_focus);
472 XSetInputFocus(display, win->id, RevertToPointerRoot, CurrentTime);
473 ws[current_ws].focus = win;
477 unfocus_win(struct ws_win *win)
479 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
480 XSetWindowBorder(display, win->id, color_unfocus);
481 if (ws[current_ws].focus == win)
482 ws[current_ws].focus = NULL;
486 switchws(union arg *args)
491 DNPRINTF(SWM_D_WS, "switchws: %d\n", wsid + 1);
493 if (wsid == current_ws)
496 /* map new window first to prevent ugly blinking */
497 TAILQ_FOREACH (win, &ws[wsid].winlist, entry)
498 XMapRaised(display, win->id);
499 ws[wsid].visible = 1;
501 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry)
502 XUnmapWindow(display, win->id);
503 ws[current_ws].visible = 0;
508 if (ws[wsid].restack) {
512 if (ws[wsid].focus != NULL)
513 focus_win(ws[wsid].focus);
514 XSync(display, False);
519 swapwin(union arg *args)
521 struct ws_win *target;
523 DNPRINTF(SWM_D_MOVE, "swapwin: id %d\n", args->id);
524 if (ws[current_ws].focus == NULL)
528 case SWM_ARG_ID_SWAPPREV:
529 target = TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
530 TAILQ_REMOVE(&ws[current_ws].winlist,
531 ws[current_ws].focus, entry);
533 TAILQ_INSERT_TAIL(&ws[current_ws].winlist,
534 ws[current_ws].focus, entry);
536 TAILQ_INSERT_BEFORE(target, ws[current_ws].focus,
539 case SWM_ARG_ID_SWAPNEXT:
540 target = TAILQ_NEXT(ws[current_ws].focus, entry);
541 TAILQ_REMOVE(&ws[current_ws].winlist,
542 ws[current_ws].focus, entry);
544 TAILQ_INSERT_HEAD(&ws[current_ws].winlist,
545 ws[current_ws].focus, entry);
547 TAILQ_INSERT_AFTER(&ws[current_ws].winlist, target,
548 ws[current_ws].focus, entry);
550 case SWM_ARG_ID_SWAPMAIN:
551 target = TAILQ_FIRST(&ws[current_ws].winlist);
552 if (target == ws[current_ws].focus)
554 TAILQ_REMOVE(&ws[current_ws].winlist, target, entry);
555 TAILQ_INSERT_BEFORE(ws[current_ws].focus, target, entry);
556 TAILQ_REMOVE(&ws[current_ws].winlist,
557 ws[current_ws].focus, entry);
558 TAILQ_INSERT_HEAD(&ws[current_ws].winlist,
559 ws[current_ws].focus, entry);
562 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
571 focus(union arg *args)
573 struct ws_win *winfocus, *winlostfocus;
575 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
576 if (ws[current_ws].focus == NULL)
579 winlostfocus = ws[current_ws].focus;
582 case SWM_ARG_ID_FOCUSPREV:
583 winfocus = TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
584 if (winfocus == NULL)
586 TAILQ_LAST(&ws[current_ws].winlist, ws_win_list);
589 case SWM_ARG_ID_FOCUSNEXT:
590 winfocus = TAILQ_NEXT(ws[current_ws].focus, entry);
591 if (winfocus == NULL)
592 winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
595 case SWM_ARG_ID_FOCUSMAIN:
596 winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
603 if (winfocus == winlostfocus)
606 unfocus_win(winlostfocus);
608 /* XXX if we hook in focus_win(), we get a nasty cycle */
609 if (ws[current_ws].cur_layout->l_focus != NULL)
610 ws[current_ws].cur_layout->l_focus(winfocus);
611 XSync(display, False);
615 cycle_layout(union arg *args)
617 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", current_ws);
619 ws[current_ws].cur_layout++;
620 if (ws[current_ws].cur_layout->l_stack == NULL)
621 ws[current_ws].cur_layout = &layouts[0];
628 resize_master(union arg *args)
630 DNPRINTF(SWM_D_EVENT, "resize_master: id %d\n", args->id);
632 if (ws[current_ws].cur_layout->l_resize != NULL)
633 ws[current_ws].cur_layout->l_resize(args->id);
637 stack_reset(union arg *args)
639 DNPRINTF(SWM_D_EVENT, "stack_reset: ws %d\n", current_ws);
641 if (ws[current_ws].cur_layout->l_init != NULL) {
642 ws[current_ws].cur_layout->l_init(current_ws);
649 struct swm_geometry g;
650 DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws);
652 /* start with workspace geometry, adjust for bar */
653 g = ws[current_ws].g;
659 ws[current_ws].restack = 0;
660 ws[current_ws].cur_layout->l_stack(&g);
661 XSync(display, False);
665 stack_floater(struct ws_win *win)
670 bzero(&wc, sizeof wc);
671 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
673 if (win->transient) {
674 win->g.w = (double)WIDTH * dialog_ratio;
675 win->g.h = (double)HEIGHT * dialog_ratio;
678 wc.height = win->g.h;
679 wc.x = (WIDTH - win->g.w) / 2;
680 wc.y = (HEIGHT - win->g.h) / 2;
682 DNPRINTF(SWM_D_EVENT, "stack_floater: win %d x %d y %d w %d h %d\n",
683 win, wc.x, wc.y, wc.width, wc.height);
685 XConfigureWindow(display, win->id, mask, &wc);
689 int vertical_msize[SWM_WS_MAX];
692 vertical_init(int ws_idx)
694 DNPRINTF(SWM_D_MISC, "vertical_init: workspace: %d\n", current_ws);
696 vertical_msize[ws_idx] = ws[ws_idx].g.w / 2;
700 vertical_resize(int id)
702 DNPRINTF(SWM_D_MISC, "vertical_resize: workspace: %d\n", current_ws);
705 case SWM_ARG_ID_MASTERSHRINK:
706 vertical_msize[current_ws] -= WIDTH / 32;
707 if ( vertical_msize[current_ws] < WIDTH / 16)
708 vertical_msize[current_ws] = WIDTH / 16;
710 case SWM_ARG_ID_MASTERGROW:
711 vertical_msize[current_ws] += WIDTH / 32;
712 if ( vertical_msize[current_ws] >
713 (WIDTH - (WIDTH / 16)))
714 vertical_msize[current_ws] =
724 vertical_stack(struct swm_geometry *g) {
726 struct swm_geometry gg = *g;
727 struct ws_win wf, *win, *winfocus = &wf;
731 DNPRINTF(SWM_D_EVENT, "vertical_stack: workspace: %d\n", current_ws);
733 winno = count_win(current_ws, 0);
739 gg.w = vertical_msize[current_ws];
742 hrh = g->h / (winno - 1);
747 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
749 gg.x += vertical_msize[current_ws] + 2;
750 gg.w = g->w - (vertical_msize[current_ws] + 2);
752 if (i != 0 && hrh != 0) {
753 /* correct the last window for lost pixels */
754 if (win == TAILQ_LAST(&ws[current_ws].winlist,
756 gg.h = hrh + (g->h - (i * hrh));
760 /* leave first right hand window at y = 0 */
766 if (win->transient != 0 || win->floating != 0)
769 bzero(&wc, sizeof wc);
771 win->g.x = wc.x = gg.x;
772 win->g.y = wc.y = gg.y;
773 win->g.w = wc.width = gg.w;
774 win->g.h = wc.height = gg.h;
775 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
776 XConfigureWindow(display, win->id, mask, &wc);
779 if (win == ws[current_ws].focus)
783 XMapRaised(display, win->id);
787 focus_win(winfocus); /* this has to be done outside of the loop */
790 int horizontal_msize[SWM_WS_MAX];
793 horizontal_init(int ws_idx)
795 DNPRINTF(SWM_D_MISC, "horizontal_init: workspace: %d\n", current_ws);
797 horizontal_msize[ws_idx] = ws[ws_idx].g.h / 2;
801 horizontal_resize(int id)
803 DNPRINTF(SWM_D_MISC, "horizontal_resize: workspace: %d\n", current_ws);
806 case SWM_ARG_ID_MASTERSHRINK:
807 horizontal_msize[current_ws] -= HEIGHT / 32;
808 if ( horizontal_msize[current_ws] < HEIGHT / 16)
809 horizontal_msize[current_ws] = HEIGHT / 16;
811 case SWM_ARG_ID_MASTERGROW:
812 horizontal_msize[current_ws] += HEIGHT / 32;
813 if ( horizontal_msize[current_ws] >
814 (HEIGHT - (HEIGHT / 16)))
815 horizontal_msize[current_ws] =
816 HEIGHT - HEIGHT / 16;
825 horizontal_stack(struct swm_geometry *g) {
827 struct swm_geometry gg = *g;
828 struct ws_win wf, *win, *winfocus = &wf;
832 DNPRINTF(SWM_D_EVENT, "horizontal_stack: workspace: %d\n", current_ws);
834 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;
912 DNPRINTF(SWM_D_EVENT, "max_stack: workspace: %d\n", current_ws);
914 if (count_win(current_ws, 0) == 0)
917 winfocus = ws[current_ws].focus;
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) {
923 if (win == winfocus) {
924 stack_floater(win); /* XXX maximize? */
925 XMapRaised(display, win->id);
927 XUnmapWindow(display, win->id);
929 bzero(&wc, sizeof wc);
931 win->g.x = wc.x = gg.x;
932 win->g.y = wc.y = gg.y;
933 win->g.w = wc.width = gg.w;
934 win->g.h = wc.height = gg.h;
935 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
936 XConfigureWindow(display, win->id, mask, &wc);
939 XMapRaised(display, win->id);
941 XUnmapWindow(display, win->id);
945 focus_win(winfocus); /* this has to be done outside of the loop */
949 send_to_ws(union arg *args)
952 struct ws_win *win = ws[current_ws].focus;
954 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
956 XUnmapWindow(display, win->id);
958 /* find a window to focus */
959 ws[current_ws].focus = TAILQ_PREV(win, ws_win_list, entry);
960 if (ws[current_ws].focus == NULL)
961 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
962 if (ws[current_ws].focus == win)
963 ws[current_ws].focus = NULL;
965 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
967 TAILQ_INSERT_TAIL(&ws[wsid].winlist, win, entry);
968 if (count_win(wsid, 1) == 1)
969 ws[wsid].focus = win;
970 ws[wsid].restack = 1;
975 /* key definitions */
979 void (*func)(union arg *);
982 /* modifier key function argument */
983 { MODKEY, XK_space, cycle_layout, {0} },
984 { MODKEY | ShiftMask, XK_space, stack_reset, {0} },
985 { MODKEY, XK_h, resize_master, {.id = SWM_ARG_ID_MASTERSHRINK} },
986 { MODKEY, XK_l, resize_master, {.id = SWM_ARG_ID_MASTERGROW} },
987 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
988 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
989 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
990 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
991 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
992 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
993 { MODKEY, XK_p, spawn, {.argv = spawn_menu} },
994 { MODKEY | ShiftMask, XK_q, quit, {0} },
995 { MODKEY, XK_q, restart, {0} },
996 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
997 { MODKEY, XK_1, switchws, {.id = 0} },
998 { MODKEY, XK_2, switchws, {.id = 1} },
999 { MODKEY, XK_3, switchws, {.id = 2} },
1000 { MODKEY, XK_4, switchws, {.id = 3} },
1001 { MODKEY, XK_5, switchws, {.id = 4} },
1002 { MODKEY, XK_6, switchws, {.id = 5} },
1003 { MODKEY, XK_7, switchws, {.id = 6} },
1004 { MODKEY, XK_8, switchws, {.id = 7} },
1005 { MODKEY, XK_9, switchws, {.id = 8} },
1006 { MODKEY, XK_0, switchws, {.id = 9} },
1007 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1008 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1009 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1010 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1011 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1012 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1013 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1014 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1015 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1016 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1017 { MODKEY, XK_b, bar_toggle, {0} },
1018 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1019 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1023 updatenumlockmask(void)
1026 XModifierKeymap *modmap;
1028 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1030 modmap = XGetModifierMapping(display);
1031 for (i = 0; i < 8; i++)
1032 for (j = 0; j < modmap->max_keypermod; j++)
1033 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1034 == XKeysymToKeycode(display, XK_Num_Lock))
1035 numlockmask = (1 << i);
1037 XFreeModifiermap(modmap);
1045 unsigned int modifiers[] =
1046 { 0, LockMask, numlockmask, numlockmask | LockMask };
1048 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1049 updatenumlockmask();
1051 XUngrabKey(display, AnyKey, AnyModifier, root);
1052 for (i = 0; i < LENGTH(keys); i++) {
1053 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
1054 for (j = 0; j < LENGTH(modifiers); j++)
1055 XGrabKey(display, code,
1056 keys[i].mod | modifiers[j], root,
1057 True, GrabModeAsync, GrabModeAsync);
1063 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1071 XKeyEvent *ev = &e->xkey;
1073 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1075 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1076 for (i = 0; i < LENGTH(keys); i++)
1077 if (keysym == keys[i].keysym
1078 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1080 keys[i].func(&(keys[i].args));
1084 buttonpress(XEvent *e)
1086 XButtonPressedEvent *ev = &e->xbutton;
1087 #ifdef SWM_CLICKTOFOCUS
1092 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1094 if (ev->window == root)
1096 if (ev->window == ws[current_ws].focus->id)
1098 #ifdef SWM_CLICKTOFOCUS
1099 TAILQ_FOREACH(win, &ws[current_ws].winlist, entry)
1100 if (win->id == ev->window) {
1101 /* focus in the clicked window */
1102 XSetWindowBorder(display, ev->window, 0xff0000);
1103 XSetWindowBorder(display,
1104 ws[current_ws].focus->id, 0x888888);
1105 XSetInputFocus(display, ev->window, RevertToPointerRoot,
1107 ws[current_ws].focus = win;
1108 XSync(display, False);
1115 set_win_state(struct ws_win *win, long state)
1117 long data[] = {state, None};
1119 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1121 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1122 (unsigned char *)data, 2);
1126 manage_window(Window id)
1132 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1134 return (win); /* already being managed */
1137 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1138 errx(1, "calloc: failed to allocate memory for new window");
1141 TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
1142 ws[current_ws].focus = win; /* make new win focused */
1144 XGetTransientForHint(display, win->id, &trans);
1146 win->transient = trans;
1147 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1148 (unsigned)win->id, win->transient);
1150 XGetWindowAttributes(display, win->id, &win->wa);
1151 win->g.w = win->wa.width;
1152 win->g.h = win->wa.height;
1153 win->g.x = win->wa.x;
1154 win->g.y = win->wa.y;
1156 /* XXX make this a table */
1157 bzero(&ch, sizeof ch);
1158 if (XGetClassHint(display, win->id, &ch)) {
1159 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1160 if (!strcmp(ch.res_class, "MPlayer") && !strcmp(ch.res_name, "xv")) {
1164 XFree(ch.res_class);
1169 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
1170 PropertyChangeMask | StructureNotifyMask);
1172 set_win_state(win, NormalState);
1178 configurerequest(XEvent *e)
1180 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1185 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1186 if (win->id == ev->window) {
1193 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
1195 bzero(&wc, sizeof wc);
1198 wc.width = ev->width;
1199 wc.height = ev->height;
1200 wc.border_width = ev->border_width;
1201 wc.sibling = ev->above;
1202 wc.stack_mode = ev->detail;
1203 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
1205 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
1208 if(ev->value_mask & CWX)
1210 if(ev->value_mask & CWY)
1212 if(ev->value_mask & CWWidth)
1213 win->g.w = ev->width;
1214 if(ev->value_mask & CWHeight)
1215 win->g.h = ev->height;
1216 /* this seems to be full screen */
1217 if (win->g.w > WIDTH) {
1222 if (win->g.h > HEIGHT) {
1227 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
1229 XMoveResizeWindow(display, win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1236 configurenotify(XEvent *e)
1238 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1239 e->xconfigure.window);
1243 destroynotify(XEvent *e)
1246 XDestroyWindowEvent *ev = &e->xdestroywindow;
1248 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1250 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1251 if (ev->window == win->id) {
1252 /* find a window to focus */
1253 ws[current_ws].focus = TAILQ_PREV(win,
1254 ws_win_list, entry);
1255 if (ws[current_ws].focus == NULL)
1256 ws[current_ws].focus =
1257 TAILQ_FIRST(&ws[current_ws].winlist);
1258 if (win == ws[current_ws].focus)
1259 ws[current_ws].focus = NULL;
1261 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
1262 set_win_state(win, WithdrawnState);
1272 enternotify(XEvent *e)
1274 XCrossingEvent *ev = &e->xcrossing;
1277 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1279 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
1283 /* eat event(s) to prevent autofocus */
1287 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1288 if (win->id == ev->window)
1298 XFocusChangeEvent *ev = &e->xfocus;
1300 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", ev->window);
1302 if (ev->window == root)
1305 * kill grab for now so that we can cut and paste , this screws up
1309 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1310 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1311 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1316 mappingnotify(XEvent *e)
1318 XMappingEvent *ev = &e->xmapping;
1320 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1322 XRefreshKeyboardMapping(ev);
1323 if (ev->request == MappingKeyboard)
1328 maprequest(XEvent *e)
1330 XMapRequestEvent *ev = &e->xmaprequest;
1331 XWindowAttributes wa;
1333 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1334 e->xmaprequest.window);
1336 if (!XGetWindowAttributes(display, ev->window, &wa))
1338 if (wa.override_redirect)
1340 manage_window(e->xmaprequest.window);
1345 propertynotify(XEvent *e)
1347 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1348 e->xproperty.window);
1352 unmapnotify(XEvent *e)
1354 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1358 visibilitynotify(XEvent *e)
1360 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n", e->xvisibility.window);
1362 if (e->xvisibility.window == bar_window &&
1363 e->xvisibility.state == VisibilityUnobscured)
1367 void (*handler[LASTEvent])(XEvent *) = {
1369 [KeyPress] = keypress,
1370 [ButtonPress] = buttonpress,
1371 [ConfigureRequest] = configurerequest,
1372 [ConfigureNotify] = configurenotify,
1373 [DestroyNotify] = destroynotify,
1374 [EnterNotify] = enternotify,
1375 [FocusIn] = focusin,
1376 [MappingNotify] = mappingnotify,
1377 [MapRequest] = maprequest,
1378 [PropertyNotify] = propertynotify,
1379 [UnmapNotify] = unmapnotify,
1380 [VisibilityNotify] = visibilitynotify,
1384 xerror_start(Display *d, XErrorEvent *ee)
1391 xerror(Display *d, XErrorEvent *ee)
1393 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1401 xerrorxlib = XSetErrorHandler(xerror_start);
1403 /* this causes an error if some other window manager is running */
1404 XSelectInput(display, DefaultRootWindow(display),
1405 SubstructureRedirectMask);
1406 XSync(display, False);
1410 XSetErrorHandler(xerror);
1411 XSync(display, False);
1420 unsigned char *p = NULL;
1421 unsigned long n, extra;
1424 astate = XInternAtom(display, "WM_STATE", False);
1425 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
1426 &real, &format, &n, &extra, (unsigned char **)&p);
1427 if (status != Success)
1436 main(int argc, char *argv[])
1439 char conf[PATH_MAX], *cfile = NULL;
1442 unsigned int i, j, num;
1443 Window d1, d2, *wins = NULL;
1444 XWindowAttributes wa;
1447 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
1448 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
1449 warnx("no locale support");
1451 if (!(display = XOpenDisplay(0)))
1452 errx(1, "can not open display");
1455 errx(1, "other wm running");
1457 screen = DefaultScreen(display);
1458 root = RootWindow(display, screen);
1459 astate = XInternAtom(display, "WM_STATE", False);
1461 /* look for local and global conf file */
1462 pwd = getpwuid(getuid());
1464 errx(1, "invalid user %d", getuid());
1466 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
1467 if (stat(conf, &sb) != -1) {
1468 if (S_ISREG(sb.st_mode))
1471 /* try global conf file */
1472 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
1473 if (!stat(conf, &sb))
1474 if (S_ISREG(sb.st_mode))
1480 /* init all workspaces */
1481 for (i = 0; i < SWM_WS_MAX; i++) {
1487 ws[i].g.w = DisplayWidth(display, screen) - 2;
1488 ws[i].g.h = DisplayHeight(display, screen) - 2;
1489 TAILQ_INIT(&ws[i].winlist);
1491 for (j = 0; layouts[j].l_stack != NULL; j++) {
1492 if (layouts[j].l_init != NULL)
1493 layouts[j].l_init(i);
1495 ws[i].cur_layout = &layouts[0];
1497 /* make work space 1 active */
1500 /* grab existing windows */
1501 if (XQueryTree(display, root, &d1, &d2, &wins, &num)) {
1502 /* normal windows */
1503 for (i = 0; i < num; i++) {
1504 XGetWindowAttributes(display, wins[i], &wa);
1505 if (!XGetWindowAttributes(display, wins[i], &wa) ||
1506 wa.override_redirect || XGetTransientForHint(display, wins[i], &d1))
1508 if (wa.map_state == IsViewable || getstate(wins[i]) == NormalState)
1509 manage_window(wins[i]);
1511 /* transient windows */
1512 for (i = 0; i < num; i++) {
1513 if (!XGetWindowAttributes(display, wins[i], &wa))
1515 if (XGetTransientForHint(display, wins[i], &d1) &&
1516 (wa.map_state == IsViewable || getstate(wins[i]) ==
1518 manage_window(wins[i]);
1523 ws[0].focus = TAILQ_FIRST(&ws[0].winlist);
1525 /* setup status bar */
1528 XSelectInput(display, root, SubstructureRedirectMask |
1529 SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
1530 EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
1531 FocusChangeMask | PropertyChangeMask | ExposureMask);
1537 XNextEvent(display, &e);
1538 if (handler[e.type])
1539 handler[e.type](&e);
1542 XCloseDisplay(display);