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))
105 int (*xerrorxlib)(Display *, XErrorEvent *);
109 int ignore_enter = 0;
110 unsigned int numlockmask = 0;
111 unsigned long color_focus = 0xff0000; /* XXX should this be per ws? */
112 unsigned long color_unfocus = 0x888888;
119 unsigned long bar_border = 0x008080;
120 unsigned long bar_color = 0x000000;
121 unsigned long bar_font_color = 0xa0a0a0;
127 char *bar_fonts[] = {
128 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
129 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
133 /* terminal + args */
134 char *spawn_term[] = { "xterm", NULL };
135 char *spawn_menu[] = { "dmenu_run", NULL };
137 /* layout manager data */
138 struct swm_geometry {
147 TAILQ_ENTRY(ws_win) entry;
149 struct swm_geometry g;
152 XWindowAttributes wa;
155 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 XMapRaised(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 XMapRaised(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 config_win(struct ws_win *win)
379 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
380 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
381 ce.type = ConfigureNotify;
382 ce.display = display;
388 ce.height = win->g.h;
389 ce.border_width = 1; /* XXX store this! */
391 ce.override_redirect = False;
392 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
396 count_win(int wsid, int count_transient)
401 TAILQ_FOREACH (win, &ws[wsid].winlist, entry) {
402 if (count_transient == 0 && win->floating)
404 if (count_transient == 0 && win->transient)
408 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
413 quit(union arg *args)
415 DNPRINTF(SWM_D_MISC, "quit\n");
420 restart(union arg *args)
422 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
424 XCloseDisplay(display);
425 execvp(start_argv[0], start_argv);
426 fprintf(stderr, "execvp failed\n");
432 spawn(union arg *args)
434 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
436 * The double-fork construct avoids zombie processes and keeps the code
437 * clean from stupid signal handlers.
442 close(ConnectionNumber(display));
444 execvp(args->argv[0], args->argv);
445 fprintf(stderr, "execvp failed\n");
454 focus_win(struct ws_win *win)
456 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
457 XSetWindowBorder(display, win->id, color_focus);
458 XSetInputFocus(display, win->id, RevertToPointerRoot, CurrentTime);
459 ws[current_ws].focus = win;
463 unfocus_win(struct ws_win *win)
465 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
466 XSetWindowBorder(display, win->id, color_unfocus);
467 if (ws[current_ws].focus == win)
468 ws[current_ws].focus = NULL;
472 switchws(union arg *args)
477 DNPRINTF(SWM_D_WS, "switchws: %d\n", wsid + 1);
479 if (wsid == current_ws)
482 /* map new window first to prevent ugly blinking */
483 TAILQ_FOREACH (win, &ws[wsid].winlist, entry)
484 XMapRaised(display, win->id);
485 ws[wsid].visible = 1;
487 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry)
488 XUnmapWindow(display, win->id);
489 ws[current_ws].visible = 0;
494 if (ws[wsid].restack) {
498 if (ws[wsid].focus != NULL)
499 focus_win(ws[wsid].focus);
500 XSync(display, False);
505 swapwin(union arg *args)
507 struct ws_win *target;
509 DNPRINTF(SWM_D_MOVE, "swapwin: id %d\n", args->id);
510 if (ws[current_ws].focus == NULL)
514 case SWM_ARG_ID_SWAPPREV:
515 target = TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
516 TAILQ_REMOVE(&ws[current_ws].winlist,
517 ws[current_ws].focus, entry);
519 TAILQ_INSERT_TAIL(&ws[current_ws].winlist,
520 ws[current_ws].focus, entry);
522 TAILQ_INSERT_BEFORE(target, ws[current_ws].focus,
525 case SWM_ARG_ID_SWAPNEXT:
526 target = TAILQ_NEXT(ws[current_ws].focus, entry);
527 TAILQ_REMOVE(&ws[current_ws].winlist,
528 ws[current_ws].focus, entry);
530 TAILQ_INSERT_HEAD(&ws[current_ws].winlist,
531 ws[current_ws].focus, entry);
533 TAILQ_INSERT_AFTER(&ws[current_ws].winlist, target,
534 ws[current_ws].focus, entry);
536 case SWM_ARG_ID_SWAPMAIN:
537 target = TAILQ_FIRST(&ws[current_ws].winlist);
538 if (target == ws[current_ws].focus)
540 TAILQ_REMOVE(&ws[current_ws].winlist, target, entry);
541 TAILQ_INSERT_BEFORE(ws[current_ws].focus, target, entry);
542 TAILQ_REMOVE(&ws[current_ws].winlist,
543 ws[current_ws].focus, entry);
544 TAILQ_INSERT_HEAD(&ws[current_ws].winlist,
545 ws[current_ws].focus, entry);
548 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
557 focus(union arg *args)
559 struct ws_win *winfocus, *winlostfocus;
561 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
562 if (ws[current_ws].focus == NULL)
565 winlostfocus = ws[current_ws].focus;
568 case SWM_ARG_ID_FOCUSPREV:
569 winfocus = TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
570 if (winfocus == NULL)
572 TAILQ_LAST(&ws[current_ws].winlist, ws_win_list);
575 case SWM_ARG_ID_FOCUSNEXT:
576 winfocus = TAILQ_NEXT(ws[current_ws].focus, entry);
577 if (winfocus == NULL)
578 winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
581 case SWM_ARG_ID_FOCUSMAIN:
582 winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
589 if (winfocus == winlostfocus)
592 unfocus_win(winlostfocus);
594 /* XXX if we hook in focus_win(), we get a nasty cycle */
595 if (ws[current_ws].cur_layout->l_focus != NULL)
596 ws[current_ws].cur_layout->l_focus(winfocus);
597 XSync(display, False);
601 cycle_layout(union arg *args)
603 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", current_ws);
605 ws[current_ws].cur_layout++;
606 if (ws[current_ws].cur_layout->l_stack == NULL)
607 ws[current_ws].cur_layout = &layouts[0];
614 resize_master(union arg *args)
616 DNPRINTF(SWM_D_EVENT, "resize_master: id %d\n", args->id);
618 if (ws[current_ws].cur_layout->l_resize != NULL)
619 ws[current_ws].cur_layout->l_resize(args->id);
623 stack_reset(union arg *args)
625 DNPRINTF(SWM_D_EVENT, "stack_reset: ws %d\n", current_ws);
627 if (ws[current_ws].cur_layout->l_init != NULL) {
628 ws[current_ws].cur_layout->l_init(current_ws);
635 struct swm_geometry g;
636 DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws);
638 /* start with workspace geometry, adjust for bar */
639 g = ws[current_ws].g;
645 ws[current_ws].restack = 0;
646 ws[current_ws].cur_layout->l_stack(&g);
647 XSync(display, False);
651 stack_floater(struct ws_win *win)
657 bzero(&wc, sizeof wc);
659 mask = CWX | CWY | CWBorderWidth;
661 win->g.w = wc.width = win->wa.width;
662 win->g.h = wc.height = win->wa.height;
663 win->g.x = wc.x = (ws[current_ws].g.w - win->wa.width) / 2;
664 win->g.y = wc.y = (ws[current_ws].g.h - win->wa.height) / 2;
666 DNPRINTF(SWM_D_EVENT, "stack_floater: win %d x %d y %d w %d h %d\n",
667 win, wc.x, wc.y, wc.width, wc.height);
669 XConfigureWindow(display, win->id, mask, &wc);
672 bzero(&wc, sizeof wc);
674 mask = CWX | CWY | CWBorderWidth;
676 /* use obsolete width height */
677 if (win->sh.flags & USPosition) {
678 win->g.w = wc.width = win->sh.width;
679 win->g.h = wc.height = win->sh.height;
680 mask |= CWWidth | CWHeight;
684 if (win->sh.flags & PMinSize) {
685 /* some hints are retarded */
686 if (win->sh.min_width < ws[current_ws].g.w / 10)
687 win->sh.min_width = ws[current_ws].g.w / 3;
688 if (win->sh.min_height < ws[current_ws].g.h / 10)
689 win->sh.height = ws[current_ws].g.h / 3;
691 win->g.w = wc.width = win->sh.min_width * 2;
692 win->g.h = wc.height = win->sh.min_height * 2;
693 mask |= CWWidth | CWHeight;
695 if (win->sh.flags & PMaxSize) {
696 /* potentially override min values */
697 if (win->sh.max_width < ws[current_ws].g.w) {
698 win->g.w = wc.width = win->sh.max_width;
701 if (win->sh.max_height < ws[current_ws].g.h) {
702 win->g.h = wc.height = win->sh.max_height;
707 /* make sure we don't clobber the screen */
708 if ((mask & CWWidth) && win->wa.width > ws[current_ws].g.w)
709 win->wa.width = ws[current_ws].g.w - 4;
710 if ((mask & CWHeight) && win->wa.height > ws[current_ws].g.h)
711 win->wa.height = ws[current_ws].g.h - 4;
713 /* supposed to be obsolete */
714 if (win->sh.flags & USPosition) {
715 win->g.x = wc.x = win->sh.x;
716 win->g.y = wc.y = win->sh.y;
718 win->g.x = wc.x = (ws[current_ws].g.w - win->wa.width) / 2;
719 win->g.y = wc.y = (ws[current_ws].g.h - win->wa.height) / 2;
721 DNPRINTF(SWM_D_EVENT, "stack_floater: win %d x %d y %d w %d h %d\n",
722 win, wc.x, wc.y, wc.width, wc.height);
724 XConfigureWindow(display, win->id, mask, &wc);
728 int vertical_msize[SWM_WS_MAX];
731 vertical_init(int ws_idx)
733 DNPRINTF(SWM_D_MISC, "vertical_init: workspace: %d\n", current_ws);
735 vertical_msize[ws_idx] = ws[ws_idx].g.w / 2;
739 vertical_resize(int id)
741 DNPRINTF(SWM_D_MISC, "vertical_resize: workspace: %d\n", current_ws);
744 case SWM_ARG_ID_MASTERSHRINK:
745 vertical_msize[current_ws] -= ws[current_ws].g.w / 32;
746 if ( vertical_msize[current_ws] < ws[current_ws].g.w / 16)
747 vertical_msize[current_ws] = ws[current_ws].g.w / 16;
749 case SWM_ARG_ID_MASTERGROW:
750 vertical_msize[current_ws] += ws[current_ws].g.w / 32;
751 if ( vertical_msize[current_ws] >
752 (ws[current_ws].g.w - (ws[current_ws].g.w / 16)))
753 vertical_msize[current_ws] =
754 ws[current_ws].g.w - ws[current_ws].g.w / 16;
763 vertical_stack(struct swm_geometry *g) {
765 struct swm_geometry gg = *g;
766 struct ws_win wf, *win, *winfocus = &wf;
770 DNPRINTF(SWM_D_EVENT, "vertical_stack: workspace: %d\n", current_ws);
772 winno = count_win(current_ws, 0);
778 gg.w = vertical_msize[current_ws];
781 hrh = g->h / (winno - 1);
786 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
788 gg.x += vertical_msize[current_ws] + 2;
789 gg.w = g->w - (vertical_msize[current_ws] + 2);
791 if (i != 0 && hrh != 0) {
792 /* correct the last window for lost pixels */
793 if (win == TAILQ_LAST(&ws[current_ws].winlist,
795 gg.h = hrh + (g->h - (i * hrh));
799 /* leave first right hand window at y = 0 */
805 if (win->transient != 0 || win->floating != 0)
808 bzero(&wc, sizeof wc);
810 win->g.x = wc.x = gg.x;
811 win->g.y = wc.y = gg.y;
812 win->g.w = wc.width = gg.w;
813 win->g.h = wc.height = gg.h;
814 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
815 XConfigureWindow(display, win->id, mask, &wc);
818 if (win == ws[current_ws].focus)
822 XMapRaised(display, win->id);
826 focus_win(winfocus); /* this has to be done outside of the loop */
829 int horizontal_msize[SWM_WS_MAX];
832 horizontal_init(int ws_idx)
834 DNPRINTF(SWM_D_MISC, "horizontal_init: workspace: %d\n", current_ws);
836 horizontal_msize[ws_idx] = ws[ws_idx].g.h / 2;
840 horizontal_resize(int id)
842 DNPRINTF(SWM_D_MISC, "horizontal_resize: workspace: %d\n", current_ws);
845 case SWM_ARG_ID_MASTERSHRINK:
846 horizontal_msize[current_ws] -= ws[current_ws].g.h / 32;
847 if ( horizontal_msize[current_ws] < ws[current_ws].g.h / 16)
848 horizontal_msize[current_ws] = ws[current_ws].g.h / 16;
850 case SWM_ARG_ID_MASTERGROW:
851 horizontal_msize[current_ws] += ws[current_ws].g.h / 32;
852 if ( horizontal_msize[current_ws] >
853 (ws[current_ws].g.h - (ws[current_ws].g.h / 16)))
854 horizontal_msize[current_ws] =
855 ws[current_ws].g.h - ws[current_ws].g.h / 16;
864 horizontal_stack(struct swm_geometry *g) {
866 struct swm_geometry gg = *g;
867 struct ws_win wf, *win, *winfocus = &wf;
871 DNPRINTF(SWM_D_EVENT, "horizontal_stack: workspace: %d\n", current_ws);
873 winno = count_win(current_ws, 0);
879 gg.h = horizontal_msize[current_ws];
882 hrw = g->w / (winno - 1);
887 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
889 gg.y += horizontal_msize[current_ws] + 2;
890 gg.h = g->h - (horizontal_msize[current_ws] + 2);
892 if (i != 0 && hrw != 0) {
893 /* correct the last window for lost pixels */
894 if (win == TAILQ_LAST(&ws[current_ws].winlist,
896 gg.w = hrw + (g->w - (i * hrw));
900 /* leave first bottom window at x = 0 */
906 if (win->transient != 0 || win->floating != 0)
909 bzero(&wc, sizeof wc);
911 win->g.x = wc.x = gg.x;
912 win->g.y = wc.y = gg.y;
913 win->g.w = wc.width = gg.w;
914 win->g.h = wc.height = gg.h;
915 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
916 XConfigureWindow(display, win->id, mask, &wc);
919 if (win == ws[current_ws].focus)
923 XMapRaised(display, win->id);
927 focus_win(winfocus); /* this has to be done outside of the loop */
931 max_focus(struct ws_win *fwin)
935 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
936 if (win->transient == 0 && win->floating == 0 && win == fwin)
937 XMapRaised(display, win->id);
939 XUnmapWindow(display, win->id);
943 /* fullscreen view */
945 max_stack(struct swm_geometry *g) {
947 struct swm_geometry gg = *g;
948 struct ws_win *win, *winfocus;
951 DNPRINTF(SWM_D_EVENT, "max_stack: workspace: %d\n", current_ws);
953 if (count_win(current_ws, 0) == 0)
956 winfocus = ws[current_ws].focus;
958 winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
960 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
961 if (win->transient != 0 || win->floating != 0) {
963 stack_floater(win); /* XXX maximize? */
965 XUnmapWindow(display, win->id);
967 bzero(&wc, sizeof wc);
969 win->g.x = wc.x = gg.x;
970 win->g.y = wc.y = gg.y;
971 win->g.w = wc.width = gg.w;
972 win->g.h = wc.height = gg.h;
973 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
974 XConfigureWindow(display, win->id, mask, &wc);
977 XMapRaised(display, win->id);
979 XUnmapWindow(display, win->id);
983 focus_win(winfocus); /* this has to be done outside of the loop */
987 send_to_ws(union arg *args)
990 struct ws_win *win = ws[current_ws].focus;
992 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
994 XUnmapWindow(display, win->id);
996 /* find a window to focus */
997 ws[current_ws].focus = TAILQ_PREV(win, ws_win_list, entry);
998 if (ws[current_ws].focus == NULL)
999 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
1000 if (ws[current_ws].focus == win)
1001 ws[current_ws].focus = NULL;
1003 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
1005 TAILQ_INSERT_TAIL(&ws[wsid].winlist, win, entry);
1006 if (count_win(wsid, 1) == 1)
1007 ws[wsid].focus = win;
1008 ws[wsid].restack = 1;
1013 /* key definitions */
1017 void (*func)(union arg *);
1020 /* modifier key function argument */
1021 { MODKEY, XK_space, cycle_layout, {0} },
1022 { MODKEY | ShiftMask, XK_space, stack_reset, {0} },
1023 { MODKEY, XK_h, resize_master, {.id = SWM_ARG_ID_MASTERSHRINK} },
1024 { MODKEY, XK_l, resize_master, {.id = SWM_ARG_ID_MASTERGROW} },
1025 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1026 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1027 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1028 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1029 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1030 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1031 { MODKEY, XK_p, spawn, {.argv = spawn_menu} },
1032 { MODKEY | ShiftMask, XK_q, quit, {0} },
1033 { MODKEY, XK_q, restart, {0} },
1034 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1035 { MODKEY, XK_1, switchws, {.id = 0} },
1036 { MODKEY, XK_2, switchws, {.id = 1} },
1037 { MODKEY, XK_3, switchws, {.id = 2} },
1038 { MODKEY, XK_4, switchws, {.id = 3} },
1039 { MODKEY, XK_5, switchws, {.id = 4} },
1040 { MODKEY, XK_6, switchws, {.id = 5} },
1041 { MODKEY, XK_7, switchws, {.id = 6} },
1042 { MODKEY, XK_8, switchws, {.id = 7} },
1043 { MODKEY, XK_9, switchws, {.id = 8} },
1044 { MODKEY, XK_0, switchws, {.id = 9} },
1045 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1046 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1047 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1048 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1049 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1050 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1051 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1052 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1053 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1054 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1055 { MODKEY, XK_b, bar_toggle, {0} },
1056 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1057 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1061 updatenumlockmask(void)
1064 XModifierKeymap *modmap;
1066 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1068 modmap = XGetModifierMapping(display);
1069 for (i = 0; i < 8; i++)
1070 for (j = 0; j < modmap->max_keypermod; j++)
1071 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1072 == XKeysymToKeycode(display, XK_Num_Lock))
1073 numlockmask = (1 << i);
1075 XFreeModifiermap(modmap);
1083 unsigned int modifiers[] =
1084 { 0, LockMask, numlockmask, numlockmask | LockMask };
1086 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1087 updatenumlockmask();
1089 XUngrabKey(display, AnyKey, AnyModifier, root);
1090 for (i = 0; i < LENGTH(keys); i++) {
1091 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
1092 for (j = 0; j < LENGTH(modifiers); j++)
1093 XGrabKey(display, code,
1094 keys[i].mod | modifiers[j], root,
1095 True, GrabModeAsync, GrabModeAsync);
1101 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1109 XKeyEvent *ev = &e->xkey;
1111 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1113 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1114 for (i = 0; i < LENGTH(keys); i++)
1115 if (keysym == keys[i].keysym
1116 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1118 keys[i].func(&(keys[i].args));
1122 buttonpress(XEvent *e)
1124 XButtonPressedEvent *ev = &e->xbutton;
1125 #ifdef SWM_CLICKTOFOCUS
1130 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1132 if (ev->window == root)
1134 if (ev->window == ws[current_ws].focus->id)
1136 #ifdef SWM_CLICKTOFOCUS
1137 TAILQ_FOREACH(win, &ws[current_ws].winlist, entry)
1138 if (win->id == ev->window) {
1139 /* focus in the clicked window */
1140 XSetWindowBorder(display, ev->window, 0xff0000);
1141 XSetWindowBorder(display,
1142 ws[current_ws].focus->id, 0x888888);
1143 XSetInputFocus(display, ev->window, RevertToPointerRoot,
1145 ws[current_ws].focus = win;
1146 XSync(display, False);
1153 set_win_state(struct ws_win *win, long state)
1155 long data[] = {state, None};
1157 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1159 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1160 (unsigned char *)data, 2);
1164 manage_window(Window id)
1170 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1172 return (win); /* already being managed */
1175 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1176 errx(1, "calloc: failed to allocate memory for new window");
1179 TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
1180 ws[current_ws].focus = win; /* make new win focused */
1182 XGetTransientForHint(display, win->id, &trans);
1184 win->transient = trans;
1185 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1186 (unsigned)win->id, win->transient);
1188 XGetWindowAttributes(display, win->id, &win->wa);
1189 XGetNormalHints(display, win->id, &win->sh);
1192 bzero(&ch, sizeof ch);
1193 if (XGetClassHint(display, win->id, &ch)) {
1194 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1195 if (!strcmp(ch.res_class, "MPlayer") && !strcmp(ch.res_name, "xv")) {
1199 XFree(ch.res_class);
1204 XSelectInput(display, id, ButtonPressMask | EnterWindowMask |
1205 FocusChangeMask | ExposureMask);
1207 set_win_state(win, NormalState);
1213 configurerequest(XEvent *e)
1215 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1220 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1221 if (win->id == ev->window) {
1228 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
1230 bzero(&wc, sizeof wc);
1233 wc.width = ev->width;
1234 wc.height = ev->height;
1235 wc.border_width = ev->border_width;
1236 wc.sibling = ev->above;
1237 wc.stack_mode = ev->detail;
1238 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
1240 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
1247 configurenotify(XEvent *e)
1249 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1250 e->xconfigure.window);
1254 destroynotify(XEvent *e)
1257 XDestroyWindowEvent *ev = &e->xdestroywindow;
1259 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1261 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1262 if (ev->window == win->id) {
1263 /* find a window to focus */
1264 ws[current_ws].focus = TAILQ_PREV(win,
1265 ws_win_list, entry);
1266 if (ws[current_ws].focus == NULL)
1267 ws[current_ws].focus =
1268 TAILQ_FIRST(&ws[current_ws].winlist);
1269 if (win == ws[current_ws].focus)
1270 ws[current_ws].focus = NULL;
1272 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
1273 set_win_state(win, WithdrawnState);
1283 enternotify(XEvent *e)
1285 XCrossingEvent *ev = &e->xcrossing;
1288 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1290 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
1294 /* eat event(s) to prevent autofocus */
1298 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1299 if (win->id == ev->window)
1309 XFocusChangeEvent *ev = &e->xfocus;
1311 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", ev->window);
1313 if (ev->window == root)
1316 * kill grab for now so that we can cut and paste , this screws up
1320 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1321 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1322 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1327 mappingnotify(XEvent *e)
1329 XMappingEvent *ev = &e->xmapping;
1331 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1333 XRefreshKeyboardMapping(ev);
1334 if (ev->request == MappingKeyboard)
1339 maprequest(XEvent *e)
1341 XMapRequestEvent *ev = &e->xmaprequest;
1342 XWindowAttributes wa;
1344 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1345 e->xmaprequest.window);
1347 if (!XGetWindowAttributes(display, ev->window, &wa))
1349 if (wa.override_redirect)
1351 manage_window(e->xmaprequest.window);
1356 propertynotify(XEvent *e)
1358 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1359 e->xproperty.window);
1363 unmapnotify(XEvent *e)
1365 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1369 visibilitynotify(XEvent *e)
1371 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n", e->xvisibility.window);
1373 if (e->xvisibility.window == bar_window &&
1374 e->xvisibility.state == VisibilityUnobscured)
1378 void (*handler[LASTEvent])(XEvent *) = {
1380 [KeyPress] = keypress,
1381 [ButtonPress] = buttonpress,
1382 [ConfigureRequest] = configurerequest,
1383 [ConfigureNotify] = configurenotify,
1384 [DestroyNotify] = destroynotify,
1385 [EnterNotify] = enternotify,
1386 [FocusIn] = focusin,
1387 [MappingNotify] = mappingnotify,
1388 [MapRequest] = maprequest,
1389 [PropertyNotify] = propertynotify,
1390 [UnmapNotify] = unmapnotify,
1391 [VisibilityNotify] = visibilitynotify,
1395 xerror_start(Display *d, XErrorEvent *ee)
1402 xerror(Display *d, XErrorEvent *ee)
1404 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1412 xerrorxlib = XSetErrorHandler(xerror_start);
1414 /* this causes an error if some other window manager is running */
1415 XSelectInput(display, DefaultRootWindow(display),
1416 SubstructureRedirectMask);
1417 XSync(display, False);
1421 XSetErrorHandler(xerror);
1422 XSync(display, False);
1431 unsigned char *p = NULL;
1432 unsigned long n, extra;
1435 astate = XInternAtom(display, "WM_STATE", False);
1436 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
1437 &real, &format, &n, &extra, (unsigned char **)&p);
1438 if (status != Success)
1447 main(int argc, char *argv[])
1450 char conf[PATH_MAX], *cfile = NULL;
1453 unsigned int i, j, num;
1454 Window d1, d2, *wins = NULL;
1455 XWindowAttributes wa;
1458 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
1459 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
1460 warnx("no locale support");
1462 if (!(display = XOpenDisplay(0)))
1463 errx(1, "can not open display");
1466 errx(1, "other wm running");
1468 screen = DefaultScreen(display);
1469 root = RootWindow(display, screen);
1470 astate = XInternAtom(display, "WM_STATE", False);
1472 /* look for local and global conf file */
1473 pwd = getpwuid(getuid());
1475 errx(1, "invalid user %d", getuid());
1477 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
1478 if (stat(conf, &sb) != -1) {
1479 if (S_ISREG(sb.st_mode))
1482 /* try global conf file */
1483 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
1484 if (!stat(conf, &sb))
1485 if (S_ISREG(sb.st_mode))
1491 /* init all workspaces */
1492 for (i = 0; i < SWM_WS_MAX; i++) {
1498 ws[i].g.w = DisplayWidth(display, screen) - 2;
1499 ws[i].g.h = DisplayHeight(display, screen) - 2;
1500 TAILQ_INIT(&ws[i].winlist);
1502 for (j = 0; layouts[j].l_stack != NULL; j++) {
1503 if (layouts[j].l_init != NULL)
1504 layouts[j].l_init(i);
1506 ws[i].cur_layout = &layouts[0];
1508 /* make work space 1 active */
1511 /* grab existing windows */
1512 if (XQueryTree(display, root, &d1, &d2, &wins, &num)) {
1513 /* normal windows */
1514 for (i = 0; i < num; i++) {
1515 XGetWindowAttributes(display, wins[i], &wa);
1516 if (!XGetWindowAttributes(display, wins[i], &wa) ||
1517 wa.override_redirect || XGetTransientForHint(display, wins[i], &d1))
1519 if (wa.map_state == IsViewable || getstate(wins[i]) == NormalState)
1520 manage_window(wins[i]);
1522 /* transient windows */
1523 for (i = 0; i < num; i++) {
1524 if (!XGetWindowAttributes(display, wins[i], &wa))
1526 if (XGetTransientForHint(display, wins[i], &d1) &&
1527 (wa.map_state == IsViewable || getstate(wins[i]) ==
1529 manage_window(wins[i]);
1534 ws[0].focus = TAILQ_FIRST(&ws[0].winlist);
1536 /* setup status bar */
1539 XSelectInput(display, root, SubstructureRedirectMask |
1540 SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
1541 EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
1542 FocusChangeMask | PropertyChangeMask | ExposureMask);
1548 XNextEvent(display, &e);
1549 if (handler[e.type])
1550 handler[e.type](&e);
1553 XCloseDisplay(display);