3 * Copyright (c) 2009 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Much code and ideas taken from dwm under the following license:
20 * MIT/X Consortium License
22 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
23 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
24 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
25 * 2007 Premysl Hruby <dfenze at gmail dot com>
26 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
27 * 2007 Christof Musik <christof at sendfax dot de>
28 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
29 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
30 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
32 * Permission is hereby granted, free of charge, to any person obtaining a
33 * copy of this software and associated documentation files (the "Software"),
34 * to deal in the Software without restriction, including without limitation
35 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
36 * and/or sell copies of the Software, and to permit persons to whom the
37 * Software is furnished to do so, subject to the following conditions:
39 * The above copyright notice and this permission notice shall be included in
40 * all copies or substantial portions of the Software.
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
45 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48 * DEALINGS IN THE SOFTWARE.
51 #define SWM_VERSION "0.5"
64 #include <sys/types.h>
67 #include <sys/queue.h>
68 #include <sys/param.h>
70 #include <X11/cursorfont.h>
71 #include <X11/keysym.h>
72 #include <X11/Xatom.h>
74 #include <X11/Xproto.h>
75 #include <X11/Xutil.h>
77 /* #define SWM_DEBUG */
79 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
80 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
81 #define SWM_D_MISC 0x0001
82 #define SWM_D_EVENT 0x0002
83 #define SWM_D_WS 0x0004
84 #define SWM_D_FOCUS 0x0008
85 #define SWM_D_MOVE 0x0010
87 u_int32_t swm_debug = 0
96 #define DNPRINTF(n,x...)
99 #define LENGTH(x) (sizeof x / sizeof x[0])
100 #define MODKEY Mod1Mask
101 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
103 int (*xerrorxlib)(Display *, XErrorEvent *);
107 int ignore_enter = 0;
108 unsigned int numlockmask = 0;
109 unsigned long color_focus = 0xff0000; /* XXX should this be per ws? */
110 unsigned long color_unfocus = 0x888888;
117 unsigned long bar_border = 0x008080;
118 unsigned long bar_color = 0x000000;
119 unsigned long bar_font_color = 0xa0a0a0;
125 char *bar_fonts[] = {
126 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
127 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
131 /* terminal + args */
132 char *spawn_term[] = { "xterm", NULL };
133 char *spawn_menu[] = { "dmenu_run", NULL };
134 char *spawn_scrotwm[] = { "scrotwm", NULL };
136 /* layout manager data */
137 struct swm_geometry {
145 void vertical_init(int);
146 void vertical_resize(int);
147 void vertical_stack(struct swm_geometry *);
148 void horizontal_init(int);
149 void horizontal_resize(int);
150 void horizontal_stack(struct swm_geometry *);
152 void max_stack(struct swm_geometry *);
155 void (*l_init)(int); /* init/reset */
156 void (*l_stack)(struct swm_geometry *);
157 void (*l_resize)(int);
159 /* init stack, resize*/
160 { vertical_init, vertical_stack, vertical_resize},
161 { horizontal_init, horizontal_stack, horizontal_resize},
162 /* XXX not working yet
163 * { max_init, max_stack, NULL},
169 TAILQ_ENTRY(ws_win) entry;
171 struct swm_geometry g;
174 XWindowAttributes wa;
178 TAILQ_HEAD(ws_win_list, ws_win);
180 /* define work spaces */
181 #define SWM_WS_MAX (10)
183 int visible; /* workspace visible */
184 int restack; /* restack on switch */
185 struct swm_geometry g;
186 struct layout *cur_layout; /* current layout handlers */
187 struct ws_win *focus; /* which win has focus */
188 struct ws_win_list winlist; /* list of windows in ws */
192 /* args to functions */
195 #define SWM_ARG_ID_FOCUSNEXT (0)
196 #define SWM_ARG_ID_FOCUSPREV (1)
197 #define SWM_ARG_ID_FOCUSMAIN (2)
198 #define SWM_ARG_ID_SWAPNEXT (3)
199 #define SWM_ARG_ID_SWAPPREV (4)
200 #define SWM_ARG_ID_SWAPMAIN (5)
201 #define SWM_ARG_ID_MASTERSHRINK (6)
202 #define SWM_ARG_ID_MASTERGROW (7)
206 /* conf file stuff */
207 #define SWM_CONF_WS "\n= \t"
208 #define SWM_CONF_FILE "scrotwm.conf"
210 conf_load(char *filename)
213 char *line, *cp, *var, *val;
214 size_t len, lineno = 0;
216 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
218 if (filename == NULL)
221 if ((config = fopen(filename, "r")) == NULL)
225 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
229 cp += (long)strspn(cp, SWM_CONF_WS);
235 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
237 cp += (long)strspn(cp, SWM_CONF_WS);
238 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
241 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
244 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
245 bar_enabled = atoi(val);
246 else if (!strncmp(var, "bar_border",
247 strlen("bar_border")))
248 bar_border = strtol(val, NULL, 16);
249 else if (!strncmp(var, "bar_color",
250 strlen("bar_color")))
251 bar_color = strtol(val, NULL, 16);
252 else if (!strncmp(var, "bar_font_color",
253 strlen("bar_font_color")))
254 bar_font_color = strtol(val, NULL, 16);
255 else if (!strncmp(var, "bar_font", strlen("bar_font")))
256 asprintf(&bar_fonts[0], "%s", val);
262 if (!strncmp(var, "color_focus", strlen("color_focus")))
263 color_focus = strtol(val, NULL, 16);
264 else if (!strncmp(var, "color_unfocus",
265 strlen("color_unfocus")))
266 color_unfocus = strtol(val, NULL, 16);
272 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
273 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
284 errx(1, "invalid conf file entry: %s=%s", var, val);
293 if (bar_enabled == 0)
297 XSetForeground(display, bar_gc, bar_color);
298 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
303 localtime_r(&tmt, &tm);
304 strftime(bar_text, sizeof bar_text, "%a %b %d %R %Z %Y", &tm);
305 XSetForeground(display, bar_gc, bar_font_color);
306 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
308 XSync(display, False);
316 /* XXX yeah yeah byte me */
321 bar_toggle(union arg *args)
325 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
329 XUnmapWindow(display, bar_window);
332 XMapWindow(display, bar_window);
334 XSync(display, False);
335 for (i = 0; i < SWM_WS_MAX; i++)
339 bar_print(); /* must be after stack */
347 for (i = 0; bar_fonts[i] != NULL; i++) {
348 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
352 if (bar_fonts[i] == NULL)
353 errx(1, "couldn't load font");
354 bar_height = bar_fs->ascent + bar_fs->descent + 3;
356 bar_window = XCreateSimpleWindow(display, root, 0, 0,
357 ws[current_ws].g.w, bar_height - 2, 1, bar_border, bar_color);
358 bar_gc = XCreateGC(display, bar_window, 0, &bar_gcv);
359 XSetFont(display, bar_gc, bar_fs->fid);
360 XSelectInput(display, bar_window, VisibilityChangeMask);
362 XMapWindow(display, bar_window);
364 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %d\n", (int)bar_window);
366 if (signal(SIGALRM, bar_signal) == SIG_ERR)
367 err(1, "could not install bar_signal");
372 count_win(int wsid, int count_transient)
377 TAILQ_FOREACH (win, &ws[wsid].winlist, entry) {
378 if (count_transient == 0 && win->transient)
382 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
387 quit(union arg *args)
389 DNPRINTF(SWM_D_MISC, "quit\n");
395 restart(union arg *args)
397 XCloseDisplay(display);
398 execvp(args->argv[0], args->argv);
399 fprintf(stderr, "execvp failed\n");
405 spawn(union arg *args)
407 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
409 * The double-fork construct avoids zombie processes and keeps the code
410 * clean from stupid signal handlers.
415 close(ConnectionNumber(display));
417 execvp(args->argv[0], args->argv);
418 fprintf(stderr, "execvp failed\n");
427 focus_win(struct ws_win *win)
429 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
430 XSetWindowBorder(display, win->id, color_focus);
431 XSetInputFocus(display, win->id, RevertToPointerRoot, CurrentTime);
432 ws[current_ws].focus = win;
436 unfocus_win(struct ws_win *win)
438 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
439 XSetWindowBorder(display, win->id, color_unfocus);
440 if (ws[current_ws].focus == win)
441 ws[current_ws].focus = NULL;
445 switchws(union arg *args)
450 DNPRINTF(SWM_D_WS, "switchws: %d\n", wsid + 1);
452 if (wsid == current_ws)
455 /* map new window first to prevent ugly blinking */
456 TAILQ_FOREACH (win, &ws[wsid].winlist, entry)
457 XMapRaised(display, win->id);
458 ws[wsid].visible = 1;
460 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry)
461 XUnmapWindow(display, win->id);
462 ws[current_ws].visible = 0;
467 if (ws[wsid].restack) {
471 if (ws[wsid].focus != NULL)
472 focus_win(ws[wsid].focus);
473 XSync(display, False);
478 swapwin(union arg *args)
480 struct ws_win *target;
482 DNPRINTF(SWM_D_MOVE, "swapwin: id %d\n", args->id);
483 if (ws[current_ws].focus == NULL)
487 case SWM_ARG_ID_SWAPPREV:
488 target = TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
489 TAILQ_REMOVE(&ws[current_ws].winlist,
490 ws[current_ws].focus, entry);
492 TAILQ_INSERT_TAIL(&ws[current_ws].winlist,
493 ws[current_ws].focus, entry);
495 TAILQ_INSERT_BEFORE(target, ws[current_ws].focus,
498 case SWM_ARG_ID_SWAPNEXT:
499 target = TAILQ_NEXT(ws[current_ws].focus, entry);
500 TAILQ_REMOVE(&ws[current_ws].winlist,
501 ws[current_ws].focus, entry);
503 TAILQ_INSERT_HEAD(&ws[current_ws].winlist,
504 ws[current_ws].focus, entry);
506 TAILQ_INSERT_AFTER(&ws[current_ws].winlist, target,
507 ws[current_ws].focus, entry);
509 case SWM_ARG_ID_SWAPMAIN:
510 target = TAILQ_FIRST(&ws[current_ws].winlist);
511 if (target == ws[current_ws].focus)
513 TAILQ_REMOVE(&ws[current_ws].winlist, target, entry);
514 TAILQ_INSERT_BEFORE(ws[current_ws].focus, target, entry);
515 TAILQ_REMOVE(&ws[current_ws].winlist,
516 ws[current_ws].focus, entry);
517 TAILQ_INSERT_HEAD(&ws[current_ws].winlist,
518 ws[current_ws].focus, entry);
521 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
530 focus(union arg *args)
532 struct ws_win *winfocus, *winlostfocus;
534 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
535 if (ws[current_ws].focus == NULL)
538 winlostfocus = ws[current_ws].focus;
541 case SWM_ARG_ID_FOCUSPREV:
542 winfocus = TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
543 if (winfocus == NULL)
545 TAILQ_LAST(&ws[current_ws].winlist, ws_win_list);
548 case SWM_ARG_ID_FOCUSNEXT:
549 winfocus = TAILQ_NEXT(ws[current_ws].focus, entry);
550 if (winfocus == NULL)
551 winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
554 case SWM_ARG_ID_FOCUSMAIN:
555 winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
562 if (winfocus == winlostfocus)
565 unfocus_win(winlostfocus);
567 XSync(display, False);
571 cycle_layout(union arg *args)
573 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", current_ws);
575 ws[current_ws].cur_layout++;
576 if (ws[current_ws].cur_layout->l_stack == NULL)
577 ws[current_ws].cur_layout = &layouts[0];
582 resize_master(union arg *args)
584 DNPRINTF(SWM_D_EVENT, "resize_master: id %d\n", args->id);
586 if (ws[current_ws].cur_layout->l_resize != NULL)
587 ws[current_ws].cur_layout->l_resize(args->id);
591 stack_reset(union arg *args)
593 DNPRINTF(SWM_D_EVENT, "stack_reset: ws %d\n", current_ws);
595 if (ws[current_ws].cur_layout->l_init != NULL) {
596 ws[current_ws].cur_layout->l_init(current_ws);
603 struct swm_geometry g;
604 DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws);
606 /* start with workspace geometry, adjust for bar */
607 g = ws[current_ws].g;
613 ws[current_ws].restack = 0;
614 ws[current_ws].cur_layout->l_stack(&g);
615 XSync(display, False);
619 stack_floater(struct ws_win *win)
624 bzero(&wc, sizeof wc);
626 mask = CWX | CWY | CWBorderWidth;
628 /* use obsolete width height */
629 if (win->sh.flags & USPosition) {
630 win->g.w = wc.width = win->sh.width;
631 win->g.h = wc.height = win->sh.height;
632 mask |= CWWidth | CWHeight;
636 if (win->sh.flags & PMinSize) {
637 /* some hints are retarded */
638 if (win->sh.min_width < ws[current_ws].g.w / 10)
639 win->sh.min_width = ws[current_ws].g.w / 3;
640 if (win->sh.min_height < ws[current_ws].g.h / 10)
641 win->wa.height = ws[current_ws].g.h / 3;
643 win->g.w = wc.width = win->sh.min_width * 2;
644 win->g.h = wc.height = win->sh.min_height * 2;
645 mask |= CWWidth | CWHeight;
647 if (win->sh.flags & PMaxSize) {
648 /* potentially override min values */
649 if (win->sh.max_width < ws[current_ws].g.w) {
650 win->g.w = wc.width = win->sh.max_width;
653 if (win->sh.max_height < ws[current_ws].g.h) {
654 win->g.h = wc.height = win->sh.max_height;
659 /* make sure we don't clobber the screen */
660 if ((mask & CWWidth) && win->wa.width > ws[current_ws].g.w)
661 win->wa.width = ws[current_ws].g.w - 4;
662 if ((mask & CWHeight) && win->wa.height > ws[current_ws].g.h)
663 win->wa.height = ws[current_ws].g.h - 4;
665 /* supposed to be obsolete */
666 if (win->sh.flags & USPosition) {
667 win->g.x = wc.x = win->sh.x;
668 win->g.y = wc.y = win->sh.y;
670 win->g.x = wc.x = (ws[current_ws].g.w - win->wa.width) / 2;
671 win->g.y = wc.y = (ws[current_ws].g.h - win->wa.height) / 2;
673 DNPRINTF(SWM_D_EVENT, "stack_floater: win %d x %d y %d w %d h %d\n",
674 win, wc.x, wc.y, wc.width, wc.height);
676 XConfigureWindow(display, win->id, mask, &wc);
680 int vertical_msize[SWM_WS_MAX];
683 vertical_init(int ws_idx)
685 DNPRINTF(SWM_D_MISC, "vertical_init: workspace: %d\n", current_ws);
687 vertical_msize[ws_idx] = ws[ws_idx].g.w / 2;
691 vertical_resize(int id)
693 DNPRINTF(SWM_D_MISC, "vertical_resize: workspace: %d\n", current_ws);
696 case SWM_ARG_ID_MASTERSHRINK:
697 vertical_msize[current_ws] -= ws[current_ws].g.w / 32;
698 if ( vertical_msize[current_ws] < ws[current_ws].g.w / 16)
699 vertical_msize[current_ws] = ws[current_ws].g.w / 16;
701 case SWM_ARG_ID_MASTERGROW:
702 vertical_msize[current_ws] += ws[current_ws].g.w / 32;
703 if ( vertical_msize[current_ws] >
704 (ws[current_ws].g.w - (ws[current_ws].g.w / 16)))
705 vertical_msize[current_ws] =
706 ws[current_ws].g.w - ws[current_ws].g.w / 16;
714 /* I know this sucks but it works well enough */
716 vertical_stack(struct swm_geometry *g) {
718 struct swm_geometry gg = *g;
719 struct ws_win wf, *win, *winfocus = &wf;
723 DNPRINTF(SWM_D_EVENT, "vertical_stack: workspace: %d\n", current_ws);
727 winno = count_win(current_ws, 0);
732 gg.w = vertical_msize[current_ws];
735 hrh = g->h / (winno - 1);
740 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
742 gg.x += vertical_msize[current_ws] + 2;
743 gg.w = g->w - (vertical_msize[current_ws] + 2);
745 if (i != 0 && hrh != 0) {
746 /* correct the last window for lost pixels */
747 if (win == TAILQ_LAST(&ws[current_ws].winlist,
749 gg.h = hrh + (g->h - (i * hrh));
753 /* leave first right hand window at y = 0 */
759 if (win->transient != 0 || win->floating != 0)
762 bzero(&wc, sizeof wc);
764 win->g.x = wc.x = gg.x;
765 win->g.y = wc.y = gg.y;
766 win->g.w = wc.width = gg.w;
767 win->g.h = wc.height = gg.h;
768 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
769 XConfigureWindow(display, win->id, mask, &wc);
772 if (win == ws[current_ws].focus)
776 XMapRaised(display, win->id);
780 focus_win(winfocus); /* this has to be done outside of the loop */
783 int horizontal_msize[SWM_WS_MAX];
786 horizontal_init(int ws_idx)
788 DNPRINTF(SWM_D_MISC, "horizontal_init: workspace: %d\n", current_ws);
790 horizontal_msize[ws_idx] = ws[ws_idx].g.h / 2;
794 horizontal_resize(int id)
796 DNPRINTF(SWM_D_MISC, "horizontal_resize: workspace: %d\n", current_ws);
799 case SWM_ARG_ID_MASTERSHRINK:
800 horizontal_msize[current_ws] -= ws[current_ws].g.h / 32;
801 if ( horizontal_msize[current_ws] < ws[current_ws].g.h / 16)
802 horizontal_msize[current_ws] = ws[current_ws].g.h / 16;
804 case SWM_ARG_ID_MASTERGROW:
805 horizontal_msize[current_ws] += ws[current_ws].g.h / 32;
806 if ( horizontal_msize[current_ws] >
807 (ws[current_ws].g.h - (ws[current_ws].g.h / 16)))
808 horizontal_msize[current_ws] =
809 ws[current_ws].g.h - ws[current_ws].g.h / 16;
818 horizontal_stack(struct swm_geometry *g) {
820 struct swm_geometry gg = *g;
821 struct ws_win wf, *win, *winfocus = &wf;
825 DNPRINTF(SWM_D_EVENT, "horizontal_stack: workspace: %d\n", current_ws);
829 winno = count_win(current_ws, 0);
834 gg.h = horizontal_msize[current_ws];
837 hrw = g->w / (winno - 1);
842 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
844 gg.y += horizontal_msize[current_ws] + 2;
845 gg.h = g->h - (horizontal_msize[current_ws] + 2);
847 if (i != 0 && hrw != 0) {
848 /* correct the last window for lost pixels */
849 if (win == TAILQ_LAST(&ws[current_ws].winlist,
851 gg.w = hrw + (g->w - (i * hrw));
855 /* leave first bottom window at x = 0 */
861 if (win->transient != 0 || win->floating != 0)
864 bzero(&wc, sizeof wc);
866 win->g.x = wc.x = gg.x;
867 win->g.y = wc.y = gg.y;
868 win->g.w = wc.width = gg.w;
869 win->g.h = wc.height = gg.h;
870 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
871 XConfigureWindow(display, win->id, mask, &wc);
874 if (win == ws[current_ws].focus)
878 XMapRaised(display, win->id);
882 focus_win(winfocus); /* this has to be done outside of the loop */
885 /* fullscreen view */
889 DNPRINTF(SWM_D_EVENT, "max_init: workspace: %d\n", ws_idx);
893 max_stack(struct swm_geometry *g) {
895 struct swm_geometry gg = *g;
896 struct ws_win wf, *win, *winfocus = &wf;
900 DNPRINTF(SWM_D_EVENT, "max_stack: workspace: %d\n", current_ws);
904 winno = count_win(current_ws, 0);
908 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
909 if (i == 1 && win->transient == 0 && win->floating != 0) {
915 bzero(&wc, sizeof wc);
917 win->g.x = wc.x = gg.x;
918 win->g.y = wc.y = gg.y;
919 win->g.w = wc.width = gg.w;
920 win->g.h = wc.height = gg.h;
921 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
922 XConfigureWindow(display, win->id, mask, &wc);
924 XMapRaised(display, win->id);
926 /* hide all but the master window */
927 XUnmapWindow(display, win->id);
932 focus_win(winfocus); /* this has to be done outside of the loop */
936 send_to_ws(union arg *args)
939 struct ws_win *win = ws[current_ws].focus;
941 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
943 XUnmapWindow(display, win->id);
945 /* find a window to focus */
946 ws[current_ws].focus = TAILQ_PREV(win, ws_win_list, entry);
947 if (ws[current_ws].focus == NULL)
948 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
949 if (ws[current_ws].focus == win)
950 ws[current_ws].focus = NULL;
952 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
954 TAILQ_INSERT_TAIL(&ws[wsid].winlist, win, entry);
955 if (count_win(wsid, 1) == 1)
956 ws[wsid].focus = win;
957 ws[wsid].restack = 1;
962 /* key definitions */
966 void (*func)(union arg *);
969 /* modifier key function argument */
970 { MODKEY, XK_space, cycle_layout, {0} },
971 { MODKEY | ShiftMask, XK_space, stack_reset, {0} },
972 { MODKEY, XK_h, resize_master, {.id = SWM_ARG_ID_MASTERSHRINK} },
973 { MODKEY, XK_l, resize_master, {.id = SWM_ARG_ID_MASTERGROW} },
974 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
975 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
976 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
977 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
978 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
979 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
980 { MODKEY, XK_p, spawn, {.argv = spawn_menu} },
981 { MODKEY | ShiftMask, XK_q, quit, {0} },
982 { MODKEY, XK_q, restart, {.argv = spawn_scrotwm } },
983 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
984 { MODKEY, XK_1, switchws, {.id = 0} },
985 { MODKEY, XK_2, switchws, {.id = 1} },
986 { MODKEY, XK_3, switchws, {.id = 2} },
987 { MODKEY, XK_4, switchws, {.id = 3} },
988 { MODKEY, XK_5, switchws, {.id = 4} },
989 { MODKEY, XK_6, switchws, {.id = 5} },
990 { MODKEY, XK_7, switchws, {.id = 6} },
991 { MODKEY, XK_8, switchws, {.id = 7} },
992 { MODKEY, XK_9, switchws, {.id = 8} },
993 { MODKEY, XK_0, switchws, {.id = 9} },
994 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
995 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
996 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
997 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
998 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
999 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1000 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1001 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1002 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1003 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1004 { MODKEY, XK_b, bar_toggle, {0} },
1005 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1006 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1010 updatenumlockmask(void)
1013 XModifierKeymap *modmap;
1015 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1017 modmap = XGetModifierMapping(display);
1018 for (i = 0; i < 8; i++)
1019 for (j = 0; j < modmap->max_keypermod; j++)
1020 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1021 == XKeysymToKeycode(display, XK_Num_Lock))
1022 numlockmask = (1 << i);
1024 XFreeModifiermap(modmap);
1032 unsigned int modifiers[] =
1033 { 0, LockMask, numlockmask, numlockmask | LockMask };
1035 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1036 updatenumlockmask();
1038 XUngrabKey(display, AnyKey, AnyModifier, root);
1039 for(i = 0; i < LENGTH(keys); i++) {
1040 if((code = XKeysymToKeycode(display, keys[i].keysym)))
1041 for(j = 0; j < LENGTH(modifiers); j++)
1042 XGrabKey(display, code,
1043 keys[i].mod | modifiers[j], root,
1044 True, GrabModeAsync, GrabModeAsync);
1050 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1058 XKeyEvent *ev = &e->xkey;
1060 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1062 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1063 for(i = 0; i < LENGTH(keys); i++)
1064 if(keysym == keys[i].keysym
1065 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1067 keys[i].func(&(keys[i].args));
1071 buttonpress(XEvent *e)
1073 XButtonPressedEvent *ev = &e->xbutton;
1074 #ifdef SWM_CLICKTOFOCUS
1079 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1081 if (ev->window == root)
1083 if (ev->window == ws[current_ws].focus->id)
1085 #ifdef SWM_CLICKTOFOCUS
1086 TAILQ_FOREACH(win, &ws[current_ws].winlist, entry)
1087 if (win->id == ev->window) {
1088 /* focus in the clicked window */
1089 XSetWindowBorder(display, ev->window, 0xff0000);
1090 XSetWindowBorder(display,
1091 ws[current_ws].focus->id, 0x888888);
1092 XSetInputFocus(display, ev->window, RevertToPointerRoot,
1094 ws[current_ws].focus = win;
1095 XSync(display, False);
1102 manage_window(Window id)
1106 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1108 return (win); /* already being managed */
1111 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1112 errx(1, "calloc: failed to allocate memory for new window");
1115 TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
1117 XSelectInput(display, id, ButtonPressMask | EnterWindowMask |
1118 FocusChangeMask | ExposureMask);
1124 configurerequest(XEvent *e)
1126 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1130 DNPRINTF(SWM_D_EVENT, "configurerequest: window: %lu\n", ev->window);
1133 win = manage_window(ev->window);
1134 ws[current_ws].focus = win; /* make new win focused */
1136 XGetTransientForHint(display, win->id, &trans);
1138 win->transient = trans;
1139 DNPRINTF(SWM_D_MISC, "configurerequest: win %u transient %u\n",
1140 (unsigned)win->id, win->transient);
1142 XGetWindowAttributes(display, win->id, &win->wa);
1143 XGetNormalHints(display, win->id, &win->sh);
1145 XClassHint ch = { 0 };
1146 if(XGetClassHint(display, win->id, &ch)) {
1147 fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name);
1148 if (!strcmp(ch.res_class, "Gvim") && !strcmp(ch.res_name, "gvim")) {
1152 XFree(ch.res_class);
1161 configurenotify(XEvent *e)
1164 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1165 e->xconfigure.window);
1169 destroynotify(XEvent *e)
1172 XDestroyWindowEvent *ev = &e->xdestroywindow;
1174 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1176 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1177 if (ev->window == win->id) {
1178 /* find a window to focus */
1179 ws[current_ws].focus = TAILQ_PREV(win,
1180 ws_win_list, entry);
1181 if (ws[current_ws].focus == NULL)
1182 ws[current_ws].focus =
1183 TAILQ_FIRST(&ws[current_ws].winlist);
1184 if (win == ws[current_ws].focus)
1185 ws[current_ws].focus = NULL;
1187 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
1197 enternotify(XEvent *e)
1199 XCrossingEvent *ev = &e->xcrossing;
1202 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1204 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
1208 /* eat event(s) to prevent autofocus */
1212 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1213 if (win->id == ev->window)
1223 XFocusChangeEvent *ev = &e->xfocus;
1225 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", ev->window);
1227 XSync(display, False); /* not sure this helps redrawing graphic apps */
1229 if (ev->window == root)
1232 * kill grab for now so that we can cut and paste , this screws up
1236 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1237 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1238 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1243 mappingnotify(XEvent *e)
1245 XMappingEvent *ev = &e->xmapping;
1247 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1249 XRefreshKeyboardMapping(ev);
1250 if(ev->request == MappingKeyboard)
1255 maprequest(XEvent *e)
1257 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1258 e->xmaprequest.window);
1260 manage_window(e->xmaprequest.window);
1265 propertynotify(XEvent *e)
1267 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1268 e->xproperty.window);
1272 unmapnotify(XEvent *e)
1274 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1278 visibilitynotify(XEvent *e)
1280 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n", e->xvisibility.window);
1282 if (e->xvisibility.window == bar_window &&
1283 e->xvisibility.state == VisibilityUnobscured)
1287 void (*handler[LASTEvent])(XEvent *) = {
1289 [KeyPress] = keypress,
1290 [ButtonPress] = buttonpress,
1291 [ConfigureRequest] = configurerequest,
1292 [ConfigureNotify] = configurenotify,
1293 [DestroyNotify] = destroynotify,
1294 [EnterNotify] = enternotify,
1295 [FocusIn] = focusin,
1296 [MappingNotify] = mappingnotify,
1297 [MapRequest] = maprequest,
1298 [PropertyNotify] = propertynotify,
1299 [UnmapNotify] = unmapnotify,
1300 [VisibilityNotify] = visibilitynotify,
1304 xerror_start(Display *d, XErrorEvent *ee)
1311 xerror(Display *d, XErrorEvent *ee)
1313 fprintf(stderr, "error: %p %p\n", display, ee);
1322 xerrorxlib = XSetErrorHandler(xerror_start);
1324 /* this causes an error if some other window manager is running */
1325 XSelectInput(display, DefaultRootWindow(display),
1326 SubstructureRedirectMask);
1327 XSync(display, False);
1331 XSetErrorHandler(xerror);
1332 XSync(display, False);
1337 main(int argc, char *argv[])
1340 char conf[PATH_MAX], *cfile = NULL;
1343 unsigned int i, j, num;
1344 Window d1, d2, *wins = NULL;
1345 XWindowAttributes wa;
1347 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
1348 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
1349 warnx("no locale support");
1351 if(!(display = XOpenDisplay(0)))
1352 errx(1, "can not open display");
1355 errx(1, "other wm running");
1357 screen = DefaultScreen(display);
1358 root = RootWindow(display, screen);
1360 /* look for local and global conf file */
1361 pwd = getpwuid(getuid());
1363 errx(1, "invalid user %d", getuid());
1365 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
1366 if (stat(conf, &sb) != -1) {
1367 if (S_ISREG(sb.st_mode))
1370 /* try global conf file */
1371 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
1372 if (!stat(conf, &sb))
1373 if (S_ISREG(sb.st_mode))
1379 /* init all workspaces */
1380 for (i = 0; i < SWM_WS_MAX; i++) {
1386 ws[i].g.w = DisplayWidth(display, screen) - 2;
1387 ws[i].g.h = DisplayHeight(display, screen) - 2;
1388 TAILQ_INIT(&ws[i].winlist);
1390 for (j = 0; layouts[j].l_stack != NULL; j++) {
1391 if (layouts[j].l_init != NULL)
1392 layouts[j].l_init(i);
1394 ws[i].cur_layout = &layouts[0];
1396 /* make work space 1 active */
1399 /* grab existing windows */
1400 if (XQueryTree(display, root, &d1, &d2, &wins, &num)) {
1401 for (i = 0; i < num; i++) {
1402 if (!XGetWindowAttributes(display, wins[i], &wa)
1403 || wa.override_redirect ||
1404 XGetTransientForHint(display, wins[i], &d1))
1406 manage_window(wins[i]);
1411 ws[0].focus = TAILQ_FIRST(&ws[0].winlist);
1413 /* setup status bar */
1416 XSelectInput(display, root, SubstructureRedirectMask |
1417 SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
1418 EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
1419 FocusChangeMask | PropertyChangeMask | ExposureMask);
1425 XNextEvent(display, &e);
1426 if (handler[e.type])
1427 handler[e.type](&e);
1430 XCloseDisplay(display);