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"
67 #include <sys/types.h>
71 #include <sys/queue.h>
72 #include <sys/param.h>
73 #include <sys/select.h>
75 #include <X11/cursorfont.h>
76 #include <X11/keysym.h>
77 #include <X11/Xatom.h>
79 #include <X11/Xproto.h>
80 #include <X11/Xutil.h>
81 #include <X11/extensions/Xrandr.h>
84 # error XRandR versions less than 1.0 are not supported
89 #define SWM_XRR_HAS_CRTC
93 /* #define SWM_DEBUG */
95 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
96 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
97 #define SWM_D_MISC 0x0001
98 #define SWM_D_EVENT 0x0002
99 #define SWM_D_WS 0x0004
100 #define SWM_D_FOCUS 0x0008
101 #define SWM_D_MOVE 0x0010
102 #define SWM_D_STACK 0x0020
103 #define SWM_D_MOUSE 0x0040
104 #define SWM_D_PROP 0x0080
107 u_int32_t swm_debug = 0
117 #define DPRINTF(x...)
118 #define DNPRINTF(n,x...)
121 #define LENGTH(x) (sizeof x / sizeof x[0])
122 #define MODKEY Mod1Mask
123 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
124 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
126 #define X(r) (r)->g.x
127 #define Y(r) (r)->g.y
128 #define WIDTH(r) (r)->g.w
129 #define HEIGHT(r) (r)->g.h
132 #define SWM_LIB "/usr/X11R6/lib/swmhack.so"
135 #define SWM_PROPLEN (16)
139 int (*xerrorxlib)(Display *, XErrorEvent *);
143 int xrandr_eventbase;
144 int ignore_enter = 0;
145 unsigned int numlockmask = 0;
149 int cycle_visible = 0;
152 double dialog_ratio = .6;
154 #define SWM_BAR_MAX (128)
155 char *bar_argv[] = { NULL, NULL };
157 char bar_ext[SWM_BAR_MAX];
158 sig_atomic_t bar_alarm = 0;
162 int bar_extra_running = 0;
170 char *bar_fonts[] = {
171 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
172 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
176 /* terminal + args */
177 char *spawn_term[] = { "xterm", NULL };
178 char *spawn_screenshot[] = { "scrot", NULL, NULL }; /* XXX get from conf */
179 char *spawn_menu[] = { "dmenu_run", "-fn", NULL,
180 "-nb", NULL, "-nf", NULL, "-sb", NULL, "-sf", NULL, NULL };
182 #define SWM_MENU_FN (2)
183 #define SWM_MENU_NB (4)
184 #define SWM_MENU_NF (6)
185 #define SWM_MENU_SB (8)
186 #define SWM_MENU_SF (10)
188 /* layout manager data */
189 struct swm_geometry {
199 /* virtual "screens" */
201 TAILQ_ENTRY(swm_region) entry;
202 struct swm_geometry g;
204 struct workspace *ws; /* current workspace on this region */
205 struct swm_screen *s; /* screen idx */
207 TAILQ_HEAD(swm_region_list, swm_region);
210 TAILQ_ENTRY(ws_win) entry;
212 struct swm_geometry g;
216 struct workspace *ws; /* always valid */
217 struct swm_screen *s; /* always valid, never changes */
218 XWindowAttributes wa;
220 TAILQ_HEAD(ws_win_list, ws_win);
222 /* layout handlers */
224 void vertical_config(struct workspace *, int);
225 void vertical_stack(struct workspace *, struct swm_geometry *);
226 void horizontal_config(struct workspace *, int);
227 void horizontal_stack(struct workspace *, struct swm_geometry *);
228 void max_stack(struct workspace *, struct swm_geometry *);
230 void grabbuttons(struct ws_win *, int);
233 void (*l_stack)(struct workspace *, struct swm_geometry *);
234 void (*l_config)(struct workspace *, int);
236 /* stack, configure */
237 { vertical_stack, vertical_config},
238 { horizontal_stack, horizontal_config},
243 #define SWM_H_SLICE (32)
244 #define SWM_V_SLICE (32)
246 /* define work spaces */
248 int idx; /* workspace index */
249 int restack; /* restack on switch */
250 struct layout *cur_layout; /* current layout handlers */
251 struct ws_win *focus; /* may be NULL */
252 struct swm_region *r; /* may be NULL */
253 struct ws_win_list winlist; /* list of windows in ws */
257 int horizontal_msize;
264 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
265 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
267 /* physical screen mapping */
268 #define SWM_WS_MAX (10) /* XXX Too small? */
270 int idx; /* screen index */
271 struct swm_region_list rl; /* list of regions on this screen */
274 struct workspace ws[SWM_WS_MAX];
280 } c[SWM_S_COLOR_MAX];
282 struct swm_screen *screens;
285 struct ws_win *cur_focus = NULL;
287 /* args to functions */
290 #define SWM_ARG_ID_FOCUSNEXT (0)
291 #define SWM_ARG_ID_FOCUSPREV (1)
292 #define SWM_ARG_ID_FOCUSMAIN (2)
293 #define SWM_ARG_ID_SWAPNEXT (3)
294 #define SWM_ARG_ID_SWAPPREV (4)
295 #define SWM_ARG_ID_SWAPMAIN (5)
296 #define SWM_ARG_ID_MASTERSHRINK (6)
297 #define SWM_ARG_ID_MASTERGROW (7)
298 #define SWM_ARG_ID_MASTERADD (8)
299 #define SWM_ARG_ID_MASTERDEL (9)
300 #define SWM_ARG_ID_STACKRESET (10)
301 #define SWM_ARG_ID_STACKINIT (11)
302 #define SWM_ARG_ID_CYCLEWS_UP (12)
303 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
304 #define SWM_ARG_ID_SS_ALL (0)
305 #define SWM_ARG_ID_SS_WINDOW (1)
310 name_to_color(char *colorname)
314 XColor screen_def, exact_def;
315 unsigned long result = 0;
316 char cname[32] = "#";
318 cmap = DefaultColormap(display, screens[0].idx);
319 status = XAllocNamedColor(display, cmap, colorname,
320 &screen_def, &exact_def);
322 strlcat(cname, colorname + 2, sizeof cname - 1);
323 status = XAllocNamedColor(display, cmap, cname, &screen_def,
327 result = screen_def.pixel;
329 fprintf(stderr, "color '%s' not found.\n", colorname);
335 setscreencolor(char *val, int i, int c)
337 if (i > 0 && i <= ScreenCount(display)) {
338 screens[i - 1].c[c].color = name_to_color(val);
339 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
341 } else if (i == -1) {
342 for (i = 0; i < ScreenCount(display); i++)
343 screens[i].c[c].color = name_to_color(val);
344 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
347 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
348 i, ScreenCount(display));
352 varmatch(char *var, char *name, int *index)
357 i = strncmp(var, name, 255);
364 p = var + strlen(name);
367 bzero(buf, sizeof buf);
369 while (isdigit(*p) && i < sizeof buf)
371 if (i == 0 || i >= sizeof buf || *p != ']')
373 *index = strtonum(buf, 0, 99, NULL);
377 /* conf file stuff */
378 #define SWM_CONF_WS "\n= \t"
379 #define SWM_CONF_FILE "scrotwm.conf"
381 conf_load(char *filename)
384 char *line, *cp, *var, *val;
385 size_t len, lineno = 0;
388 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
390 if (filename == NULL)
393 if ((config = fopen(filename, "r")) == NULL)
396 for (sc = ScreenCount(display);;) {
397 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
401 cp += (long)strspn(cp, SWM_CONF_WS);
407 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
409 cp += (long)strspn(cp, SWM_CONF_WS);
410 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
413 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
416 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
417 bar_enabled = atoi(val);
418 else if (!varmatch(var, "bar_border", &i))
419 setscreencolor(val, i, SWM_S_COLOR_BAR_BORDER);
420 else if (!varmatch(var, "bar_color", &i))
421 setscreencolor(val, i, SWM_S_COLOR_BAR);
422 else if (!varmatch(var, "bar_font_color", &i))
423 setscreencolor(val, i, SWM_S_COLOR_BAR_FONT);
424 else if (!strncmp(var, "bar_font", strlen("bar_font")))
425 asprintf(&bar_fonts[0], "%s", val);
426 else if (!strncmp(var, "bar_action", strlen("bar_action")))
427 asprintf(&bar_argv[0], "%s", val);
428 else if (!strncmp(var, "bar_delay", strlen("bar_delay")))
429 bar_delay = atoi(val);
435 if (!varmatch(var, "color_focus", &i))
436 setscreencolor(val, i, SWM_S_COLOR_FOCUS);
437 else if (!varmatch(var, "color_unfocus", &i))
438 setscreencolor(val, i, SWM_S_COLOR_UNFOCUS);
439 else if (!strncmp(var, "cycle_empty", strlen("cycle_empty")))
440 cycle_visible = atoi(val);
441 else if (!strncmp(var, "cycle_visible", strlen("cycle_visible")))
442 cycle_visible = atoi(val);
448 if (!strncmp(var, "dialog_ratio",
449 strlen("dialog_ratio"))) {
450 dialog_ratio = atof(val);
451 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
458 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
459 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
460 if (!strncmp(var, "screenshot_enabled",
461 strlen("screenshot_enabled")))
462 ss_enabled = atoi(val);
474 errx(1, "invalid conf file entry: %s=%s", var, val);
478 socket_setnonblock(int fd)
482 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
483 err(1, "fcntl F_GETFL");
485 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
486 err(1, "fcntl F_SETFL");
490 bar_print(struct swm_region *r, char *s)
492 XClearWindow(display, r->bar_window);
493 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
494 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
503 bzero(bar_pipe, sizeof bar_pipe);
506 kill(bar_pid, SIGTERM);
509 strlcpy(bar_ext, "", sizeof bar_ext);
518 struct swm_region *r;
522 char loc[SWM_BAR_MAX];
525 if (bar_enabled == 0)
528 if (bar_extra && bar_extra_running) {
529 /* ignore short reads; it'll correct itself */
530 while ((b = fgetln(stdin, &len)) != NULL)
531 if (b && b[len - 1] == '\n') {
533 strlcpy(bar_ext, b, sizeof bar_ext);
535 if (b == NULL && errno != EAGAIN) {
536 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
537 errno, strerror(errno));
541 strlcpy(bar_ext, "", sizeof bar_ext);
544 localtime_r(&tmt, &tm);
545 strftime(s, sizeof s, "%a %b %d %R %Z %Y", &tm);
546 for (i = 0; i < ScreenCount(display); i++) {
548 TAILQ_FOREACH(r, &screens[i].rl, entry) {
549 snprintf(loc, sizeof loc, "%s %d:%d %s",
550 s, x++, r->ws->idx + 1, bar_ext);
554 XSync(display, False);
565 bar_toggle(struct swm_region *r, union arg *args)
567 struct swm_region *tmpr;
568 int i, j, sc = ScreenCount(display);
570 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
573 for (i = 0; i < sc; i++)
574 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
575 XUnmapWindow(display, tmpr->bar_window);
577 for (i = 0; i < sc; i++)
578 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
579 XMapRaised(display, tmpr->bar_window);
581 bar_enabled = !bar_enabled;
582 XSync(display, False);
583 for (i = 0; i < sc; i++)
584 for (j = 0; j < SWM_WS_MAX; j++)
585 screens[i].ws[j].restack = 1;
588 /* must be after stack */
589 for (i = 0; i < sc; i++)
590 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
597 XSetWindowAttributes wa;
598 struct swm_region *r;
601 /* do this here because the conf file is in memory */
602 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
603 /* launch external status app */
604 bar_extra_running = 1;
605 if (pipe(bar_pipe) == -1)
606 err(1, "pipe error");
607 socket_setnonblock(bar_pipe[0]);
608 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
609 if (dup2(bar_pipe[0], 0) == -1)
611 if (dup2(bar_pipe[1], 1) == -1)
613 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
614 err(1, "could not disable SIGPIPE");
615 switch (bar_pid = fork()) {
617 err(1, "cannot fork");
621 execvp(bar_argv[0], bar_argv);
622 err(1, "%s external app failed", bar_argv[0]);
624 default: /* parent */
630 bzero(&wa, sizeof wa);
631 for (i = 0; i < ScreenCount(display); i++)
632 TAILQ_FOREACH(r, &screens[i].rl, entry) {
634 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
635 wa.background_pixel =
636 screens[i].c[SWM_S_COLOR_BAR].color;
637 XChangeWindowAttributes(display, r->bar_window,
638 CWBackPixel | CWBorderPixel, &wa);
644 bar_setup(struct swm_region *r)
648 for (i = 0; bar_fonts[i] != NULL; i++) {
649 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
655 if (bar_fonts[i] == NULL)
656 errx(1, "couldn't load font");
657 bar_height = bar_fs->ascent + bar_fs->descent + 3;
659 r->bar_window = XCreateSimpleWindow(display,
660 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
661 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
662 r->s->c[SWM_S_COLOR_BAR].color);
663 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
664 XSetFont(display, bar_gc, bar_fs->fid);
665 XSelectInput(display, r->bar_window, VisibilityChangeMask);
667 XMapRaised(display, r->bar_window);
668 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
670 if (signal(SIGALRM, bar_signal) == SIG_ERR)
671 err(1, "could not install bar_signal");
676 config_win(struct ws_win *win)
680 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
681 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
682 ce.type = ConfigureNotify;
683 ce.display = display;
689 ce.height = win->g.h;
690 ce.border_width = 1; /* XXX store this! */
692 ce.override_redirect = False;
693 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
697 count_win(struct workspace *ws, int count_transient)
702 TAILQ_FOREACH(win, &ws->winlist, entry) {
703 if (count_transient == 0 && win->floating)
705 if (count_transient == 0 && win->transient)
709 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
715 quit(struct swm_region *r, union arg *args)
717 DNPRINTF(SWM_D_MISC, "quit\n");
722 restart(struct swm_region *r, union arg *args)
724 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
726 /* disable alarm because the following code may not be interrupted */
728 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
729 errx(1, "can't disable alarm");
733 XCloseDisplay(display);
734 execvp(start_argv[0], start_argv);
735 fprintf(stderr, "execvp failed\n");
741 root_to_region(Window root)
743 struct swm_region *r;
748 for (i = 0; i < ScreenCount(display); i++)
749 if (screens[i].root == root)
752 if (XQueryPointer(display, screens[i].root,
753 &rr, &cr, &x, &y, &wx, &wy, &mask) == False) {
754 /* if we can't query the pointer, grab the first region */
755 r = TAILQ_FIRST(&screens[i].rl);
757 /* otherwise, choose a region based on pointer location */
758 TAILQ_FOREACH(r, &screens[i].rl, entry) {
759 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
760 y >= Y(r) && y <= Y(r) + HEIGHT(r))
765 r = TAILQ_FIRST(&screens[i].rl);
771 find_window(Window id)
776 for (i = 0; i < ScreenCount(display); i++)
777 for (j = 0; j < SWM_WS_MAX; j++)
778 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
785 spawn(struct swm_region *r, union arg *args)
787 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
789 * The double-fork construct avoids zombie processes and keeps the code
790 * clean from stupid signal handlers.
796 close(ConnectionNumber(display));
797 setenv("LD_PRELOAD", SWM_LIB, 1);
798 if (asprintf(&ret, "%d", r->ws->idx))
799 setenv("_SWM_WS", ret, 1);
800 if (asprintf(&ret, "%d", getpid()))
801 setenv("_SWM_PID", ret, 1);
803 execvp(args->argv[0], args->argv);
804 fprintf(stderr, "execvp failed\n");
813 spawnmenu(struct swm_region *r, union arg *args)
815 DNPRINTF(SWM_D_MISC, "spawnmenu\n");
817 spawn_menu[SWM_MENU_FN] = bar_fonts[bar_fidx];
818 spawn_menu[SWM_MENU_NB] = r->s->c[SWM_S_COLOR_BAR].name;
819 spawn_menu[SWM_MENU_NF] = r->s->c[SWM_S_COLOR_BAR_FONT].name;
820 spawn_menu[SWM_MENU_SB] = r->s->c[SWM_S_COLOR_BAR_BORDER].name;
821 spawn_menu[SWM_MENU_SF] = r->s->c[SWM_S_COLOR_BAR].name;
832 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
834 for (i = 0; i < ScreenCount(display); i++)
835 for (j = 0; j < SWM_WS_MAX; j++)
836 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
837 if (win->ws->r == NULL)
840 XSetWindowBorder(display, win->id,
841 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
843 win->ws->focus = NULL;
849 focus_win(struct ws_win *win)
851 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
857 win->ws->focus = win;
858 if (win->ws->r != NULL) {
860 if (!win->got_focus) {
861 XSetWindowBorder(display, win->id,
862 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
866 XSetInputFocus(display, win->id,
867 RevertToPointerRoot, CurrentTime);
872 switchws(struct swm_region *r, union arg *args)
875 struct swm_region *this_r, *other_r;
877 struct workspace *new_ws, *old_ws;
881 new_ws = &this_r->s->ws[wsid];
883 DNPRINTF(SWM_D_WS, "switchws screen %d region %dx%d+%d+%d: "
884 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
887 if (new_ws == old_ws)
892 /* if the other workspace is hidden, switch windows */
893 /* map new window first to prevent ugly blinking */
894 TAILQ_FOREACH(win, &new_ws->winlist, entry)
895 XMapRaised(display, win->id);
897 TAILQ_FOREACH(win, &old_ws->winlist, entry)
898 XUnmapWindow(display, win->id);
903 other_r->ws = old_ws;
912 focus_win(new_ws->focus);
918 cyclews(struct swm_region *r, union arg *args)
921 struct swm_screen *s = r->s;
923 DNPRINTF(SWM_D_WS, "cyclews id %d "
924 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
925 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
931 case SWM_ARG_ID_CYCLEWS_UP:
932 if (a.id < SWM_WS_MAX - 1)
937 case SWM_ARG_ID_CYCLEWS_DOWN:
941 a.id = SWM_WS_MAX - 1;
947 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
949 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
953 } while (a.id != r->ws->idx);
957 swapwin(struct swm_region *r, union arg *args)
959 struct ws_win *target;
960 struct ws_win_list *wl;
963 DNPRINTF(SWM_D_WS, "swapwin id %d "
964 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
965 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
966 if (cur_focus == NULL)
969 wl = &cur_focus->ws->winlist;
972 case SWM_ARG_ID_SWAPPREV:
973 target = TAILQ_PREV(cur_focus, ws_win_list, entry);
974 TAILQ_REMOVE(wl, cur_focus, entry);
976 TAILQ_INSERT_TAIL(wl, cur_focus, entry);
978 TAILQ_INSERT_BEFORE(target, cur_focus, entry);
980 case SWM_ARG_ID_SWAPNEXT:
981 target = TAILQ_NEXT(cur_focus, entry);
982 TAILQ_REMOVE(wl, cur_focus, entry);
984 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
986 TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
988 case SWM_ARG_ID_SWAPMAIN:
989 target = TAILQ_FIRST(wl);
990 if (target == cur_focus)
992 TAILQ_REMOVE(wl, target, entry);
993 TAILQ_INSERT_BEFORE(cur_focus, target, entry);
994 TAILQ_REMOVE(wl, cur_focus, entry);
995 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
998 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1007 focus(struct swm_region *r, union arg *args)
1009 struct ws_win *winfocus, *winlostfocus;
1010 struct ws_win_list *wl;
1012 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1013 if (cur_focus == NULL)
1016 wl = &cur_focus->ws->winlist;
1018 winlostfocus = cur_focus;
1021 case SWM_ARG_ID_FOCUSPREV:
1022 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1023 if (winfocus == NULL)
1024 winfocus = TAILQ_LAST(wl, ws_win_list);
1027 case SWM_ARG_ID_FOCUSNEXT:
1028 winfocus = TAILQ_NEXT(cur_focus, entry);
1029 if (winfocus == NULL)
1030 winfocus = TAILQ_FIRST(wl);
1033 case SWM_ARG_ID_FOCUSMAIN:
1034 winfocus = TAILQ_FIRST(wl);
1041 if (winfocus == winlostfocus)
1044 XMapRaised(display, winfocus->id);
1045 focus_win(winfocus);
1046 XSync(display, False);
1050 cycle_layout(struct swm_region *r, union arg *args)
1052 struct workspace *ws = r->ws;
1054 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1057 if (ws->cur_layout->l_stack == NULL)
1058 ws->cur_layout = &layouts[0];
1065 stack_config(struct swm_region *r, union arg *args)
1067 struct workspace *ws = r->ws;
1069 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1072 if (ws->cur_layout->l_config != NULL)
1073 ws->cur_layout->l_config(ws, args->id);
1075 if (args->id != SWM_ARG_ID_STACKINIT);
1081 struct swm_geometry g;
1082 struct swm_region *r;
1085 DNPRINTF(SWM_D_STACK, "stack\n");
1087 for (i = 0; i < ScreenCount(display); i++) {
1089 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1090 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1091 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1093 /* start with screen geometry, adjust for bar */
1103 r->ws->cur_layout->l_stack(r->ws, &g);
1106 XSync(display, False);
1110 stack_floater(struct ws_win *win, struct swm_region *r)
1115 bzero(&wc, sizeof wc);
1116 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1117 wc.border_width = 1;
1118 if (win->transient) {
1119 win->g.w = (double)WIDTH(r) * dialog_ratio;
1120 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1122 wc.width = win->g.w;
1123 wc.height = win->g.h;
1124 wc.x = (WIDTH(r) - win->g.w) / 2;
1125 wc.y = (HEIGHT(r) - win->g.h) / 2;
1127 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1128 win->id, wc.x, wc.y, wc.width, wc.height);
1130 XConfigureWindow(display, win->id, mask, &wc);
1134 vertical_config(struct workspace *ws, int id)
1136 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1139 case SWM_ARG_ID_STACKRESET:
1140 case SWM_ARG_ID_STACKINIT:
1141 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1142 ws->l_state.vertical_mwin = 1;
1144 case SWM_ARG_ID_MASTERSHRINK:
1145 if (ws->l_state.vertical_msize > 1)
1146 ws->l_state.vertical_msize--;
1148 case SWM_ARG_ID_MASTERGROW:
1149 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1150 ws->l_state.vertical_msize++;
1152 case SWM_ARG_ID_MASTERADD:
1153 ws->l_state.vertical_mwin++;
1155 case SWM_ARG_ID_MASTERDEL:
1156 if (ws->l_state.vertical_mwin > 0)
1157 ws->l_state.vertical_mwin--;
1165 vertical_stack(struct workspace *ws, struct swm_geometry *g) {
1167 struct swm_geometry gg = *g;
1168 struct ws_win *win, *winfocus;
1169 int i, j, split, colno, hrh, winno, main_width;
1172 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1174 if ((winno = count_win(ws, 0)) == 0)
1177 if (ws->focus == NULL)
1178 ws->focus = TAILQ_FIRST(&ws->winlist);
1179 winfocus = cur_focus ? cur_focus : ws->focus;
1181 if (ws->l_state.vertical_mwin &&
1182 winno > ws->l_state.vertical_mwin) {
1183 split = ws->l_state.vertical_mwin;
1185 main_width = (g->w / SWM_V_SLICE) *
1186 ws->l_state.vertical_msize;
1196 TAILQ_FOREACH(win, &ws->winlist, entry) {
1197 if (split && i == split) {
1198 colno = winno - split;
1199 hrh = (g->h / colno);
1200 gg.x += main_width + 2;
1201 gg.w = g->w - (main_width + 2);
1206 gg.h = (hrh + (g->h - (colno * hrh)));
1214 if (win->transient != 0 || win->floating != 0)
1215 stack_floater(win, ws->r);
1217 bzero(&wc, sizeof wc);
1218 wc.border_width = 1;
1219 win->g.x = wc.x = gg.x;
1220 win->g.y = wc.y = gg.y;
1221 win->g.w = wc.width = gg.w;
1222 win->g.h = wc.height = gg.h;
1223 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1224 XConfigureWindow(display, win->id, mask, &wc);
1226 fprintf(stderr, "vertical_stack: win %d x %d y %d w %d h %d bw %d\n", win->id, win->g.x, win->g.y, win->g.w , win->g.h, wc.border_width);
1230 XMapRaised(display, win->id);
1236 focus_win(winfocus); /* has to be done outside of the loop */
1240 horizontal_config(struct workspace *ws, int id)
1242 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1245 case SWM_ARG_ID_STACKRESET:
1246 case SWM_ARG_ID_STACKINIT:
1247 ws->l_state.horizontal_mwin = 1;
1248 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1250 case SWM_ARG_ID_MASTERSHRINK:
1251 if (ws->l_state.horizontal_msize > 1)
1252 ws->l_state.horizontal_msize--;
1254 case SWM_ARG_ID_MASTERGROW:
1255 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1256 ws->l_state.horizontal_msize++;
1258 case SWM_ARG_ID_MASTERADD:
1259 ws->l_state.horizontal_mwin++;
1261 case SWM_ARG_ID_MASTERDEL:
1262 if (ws->l_state.horizontal_mwin > 0)
1263 ws->l_state.horizontal_mwin--;
1271 horizontal_stack(struct workspace *ws, struct swm_geometry *g) {
1273 struct swm_geometry gg = *g;
1274 struct ws_win *win, *winfocus;
1275 int i, j, split, rowno, hrw, winno, main_height;
1278 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
1280 if ((winno = count_win(ws, 0)) == 0)
1283 if (ws->focus == NULL)
1284 ws->focus = TAILQ_FIRST(&ws->winlist);
1285 winfocus = cur_focus ? cur_focus : ws->focus;
1287 if (ws->l_state.horizontal_mwin &&
1288 winno > ws->l_state.horizontal_mwin) {
1289 split = ws->l_state.horizontal_mwin;
1291 main_height = (g->h / SWM_V_SLICE) *
1292 ws->l_state.horizontal_msize;
1302 TAILQ_FOREACH(win, &ws->winlist, entry) {
1303 if (split && i == split) {
1304 rowno = winno - split;
1305 hrw = (g->w / rowno);
1306 gg.y += main_height + 2;
1307 gg.h = g->h - (main_height + 2);
1312 gg.w = (hrw + (g->w - (rowno * hrw)));
1319 if (win->transient != 0 || win->floating != 0)
1320 stack_floater(win, ws->r);
1322 bzero(&wc, sizeof wc);
1323 wc.border_width = 1;
1324 win->g.x = wc.x = gg.x;
1325 win->g.y = wc.y = gg.y;
1326 win->g.w = wc.width = gg.w;
1327 win->g.h = wc.height = gg.h;
1328 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1329 XConfigureWindow(display, win->id, mask, &wc);
1332 XMapRaised(display, win->id);
1338 focus_win(winfocus); /* this has to be done outside of the loop */
1341 /* fullscreen view */
1343 max_stack(struct workspace *ws, struct swm_geometry *g) {
1345 struct swm_geometry gg = *g;
1346 struct ws_win *win, *winfocus;
1349 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1351 if (count_win(ws, 0) == 0)
1354 if (ws->focus == NULL)
1355 ws->focus = TAILQ_FIRST(&ws->winlist);
1356 winfocus = cur_focus ? cur_focus : ws->focus;
1358 TAILQ_FOREACH(win, &ws->winlist, entry) {
1359 if (win->transient != 0 || win->floating != 0) {
1360 if (win == ws->focus) {
1362 stack_floater(win, ws->r);
1363 XMapRaised(display, win->id);
1365 XUnmapWindow(display, win->id);
1367 bzero(&wc, sizeof wc);
1368 wc.border_width = 1;
1369 win->g.x = wc.x = gg.x;
1370 win->g.y = wc.y = gg.y;
1371 win->g.w = wc.width = gg.w;
1372 win->g.h = wc.height = gg.h;
1373 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1374 XConfigureWindow(display, win->id, mask, &wc);
1376 if (win == ws->focus) {
1377 XMapRaised(display, win->id);
1379 XUnmapWindow(display, win->id);
1384 focus_win(winfocus); /* has to be done outside of the loop */
1388 send_to_ws(struct swm_region *r, union arg *args)
1390 int wsid = args->id;
1391 struct ws_win *win = cur_focus;
1392 struct workspace *ws, *nws;
1393 Atom ws_idx_atom = 0;
1394 unsigned char ws_idx_str[SWM_PROPLEN];
1396 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1399 nws = &win->s->ws[wsid];
1401 XUnmapWindow(display, win->id);
1403 /* find a window to focus */
1404 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1405 if (ws->focus == NULL)
1406 ws->focus = TAILQ_FIRST(&ws->winlist);
1407 if (ws->focus == win)
1410 TAILQ_REMOVE(&ws->winlist, win, entry);
1412 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1415 /* Try to update the window's workspace property */
1416 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1418 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
1419 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1421 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1422 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1425 if (count_win(nws, 1) == 1)
1434 wkill(struct swm_region *r, union arg *args)
1436 DNPRINTF(SWM_D_MISC, "wkill\n");
1437 if(r->ws->focus != NULL)
1438 XKillClient(display, r->ws->focus->id);
1442 screenshot(struct swm_region *r, union arg *args)
1446 DNPRINTF(SWM_D_MISC, "screenshot\n");
1448 if (ss_enabled == 0)
1452 case SWM_ARG_ID_SS_ALL:
1453 spawn_screenshot[1] = "-m";
1455 case SWM_ARG_ID_SS_WINDOW:
1456 spawn_screenshot[1] = "-s"; /* XXX doesn't work with scrot */
1461 a.argv = spawn_screenshot;
1465 /* key definitions */
1469 void (*func)(struct swm_region *r, union arg *);
1472 /* modifier key function argument */
1473 { MODKEY, XK_space, cycle_layout, {0} },
1474 { MODKEY | ShiftMask, XK_space, stack_config, {.id = SWM_ARG_ID_STACKRESET} },
1475 { MODKEY, XK_h, stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
1476 { MODKEY, XK_l, stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
1477 { MODKEY, XK_comma, stack_config, {.id = SWM_ARG_ID_MASTERADD} },
1478 { MODKEY, XK_period, stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
1479 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1480 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1481 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1482 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1483 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1484 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1485 { MODKEY, XK_p, spawnmenu, {.argv = spawn_menu} },
1486 { MODKEY | ShiftMask, XK_q, quit, {0} },
1487 { MODKEY, XK_q, restart, {0} },
1488 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1489 { MODKEY, XK_1, switchws, {.id = 0} },
1490 { MODKEY, XK_2, switchws, {.id = 1} },
1491 { MODKEY, XK_3, switchws, {.id = 2} },
1492 { MODKEY, XK_4, switchws, {.id = 3} },
1493 { MODKEY, XK_5, switchws, {.id = 4} },
1494 { MODKEY, XK_6, switchws, {.id = 5} },
1495 { MODKEY, XK_7, switchws, {.id = 6} },
1496 { MODKEY, XK_8, switchws, {.id = 7} },
1497 { MODKEY, XK_9, switchws, {.id = 8} },
1498 { MODKEY, XK_0, switchws, {.id = 9} },
1499 { MODKEY, XK_Right, cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
1500 { MODKEY, XK_Left, cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
1501 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1502 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1503 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1504 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1505 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1506 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1507 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1508 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1509 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1510 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1511 { MODKEY, XK_b, bar_toggle, {0} },
1512 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1513 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1514 { MODKEY | ShiftMask, XK_x, wkill, {0} },
1515 { MODKEY, XK_s, screenshot, {.id = SWM_ARG_ID_SS_ALL} },
1516 { MODKEY | ShiftMask, XK_s, screenshot, {.id = SWM_ARG_ID_SS_WINDOW} },
1520 click(struct swm_region *r, union arg *args)
1522 DNPRINTF(SWM_D_MOUSE, "click: button: %d\n", args->id);
1537 enum { client_click, root_click };
1539 unsigned int action;
1541 unsigned int button;
1542 void (*func)(struct swm_region *r, union arg *);
1545 /* action key mouse button func args */
1546 { client_click, MODKEY, Button1, click, {.id=Button1} },
1550 updatenumlockmask(void)
1553 XModifierKeymap *modmap;
1555 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1557 modmap = XGetModifierMapping(display);
1558 for (i = 0; i < 8; i++)
1559 for (j = 0; j < modmap->max_keypermod; j++)
1560 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1561 == XKeysymToKeycode(display, XK_Num_Lock))
1562 numlockmask = (1 << i);
1564 XFreeModifiermap(modmap);
1570 unsigned int i, j, k;
1572 unsigned int modifiers[] =
1573 { 0, LockMask, numlockmask, numlockmask | LockMask };
1575 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1576 updatenumlockmask();
1578 for (k = 0; k < ScreenCount(display); k++) {
1579 if (TAILQ_EMPTY(&screens[k].rl))
1581 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
1582 for (i = 0; i < LENGTH(keys); i++) {
1583 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
1584 for (j = 0; j < LENGTH(modifiers); j++)
1585 XGrabKey(display, code,
1586 keys[i].mod | modifiers[j],
1587 screens[k].root, True,
1588 GrabModeAsync, GrabModeAsync);
1594 grabbuttons(struct ws_win *win, int focused)
1597 unsigned int modifiers[] =
1598 { 0, LockMask, numlockmask, numlockmask|LockMask };
1600 updatenumlockmask();
1601 XUngrabButton(display, AnyButton, AnyModifier, win->id);
1603 for (i = 0; i < LENGTH(buttons); i++)
1604 if (buttons[i].action == client_click)
1605 for (j = 0; j < LENGTH(modifiers); j++)
1606 XGrabButton(display, buttons[i].button,
1607 buttons[i].mask | modifiers[j],
1608 win->id, False, BUTTONMASK,
1609 GrabModeAsync, GrabModeSync, None,
1612 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
1613 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1619 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1627 XKeyEvent *ev = &e->xkey;
1629 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1631 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1632 for (i = 0; i < LENGTH(keys); i++)
1633 if (keysym == keys[i].keysym
1634 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1636 keys[i].func(root_to_region(ev->root),
1641 buttonpress(XEvent *e)
1643 XButtonPressedEvent *ev = &e->xbutton;
1648 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1650 action = root_click;
1651 if ((win = find_window(ev->window)) == NULL)
1655 action = client_click;
1658 for (i = 0; i < LENGTH(buttons); i++)
1659 if (action == buttons[i].action && buttons[i].func &&
1660 buttons[i].button == ev->button &&
1661 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
1662 buttons[i].func(root_to_region(ev->root),
1667 set_win_state(struct ws_win *win, long state)
1669 long data[] = {state, None};
1671 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1673 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1674 (unsigned char *)data, 2);
1678 manage_window(Window id)
1681 struct workspace *ws;
1685 unsigned long nitems, bytes;
1686 Atom ws_idx_atom = 0, type;
1687 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
1688 struct swm_region *r;
1690 if ((win = find_window(id)) != NULL)
1691 return (win); /* already being managed */
1693 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1694 errx(1, "calloc: failed to allocate memory for new window");
1696 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1698 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
1699 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
1701 XGetWindowAttributes(display, id, &win->wa);
1702 r = root_to_region(win->wa.root);
1703 /* If the window was managed before, put it in the same workspace */
1708 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
1709 ws_idx = strtonum(prop, 0, 9, &errstr);
1711 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
1713 ws = &r->s->ws[ws_idx];
1719 win->s = r->s; /* this never changes */
1720 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
1722 /* make new win focused */
1725 XGetTransientForHint(display, win->id, &trans);
1727 win->transient = trans;
1728 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1729 (unsigned)win->id, win->transient);
1731 win->g.w = win->wa.width;
1732 win->g.h = win->wa.height;
1733 win->g.x = win->wa.x;
1734 win->g.y = win->wa.y;
1736 if (ws_idx_atom && prop == NULL &&
1737 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
1738 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1740 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1741 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1746 fprintf(stderr, "manage window: %d x %d y %d w %d h %d\n", win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1749 /* XXX make this a table */
1750 bzero(&ch, sizeof ch);
1751 if (XGetClassHint(display, win->id, &ch)) {
1752 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1753 if (!strcmp(ch.res_class, "MPlayer") && !strcmp(ch.res_name, "xv")) {
1757 XFree(ch.res_class);
1762 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
1763 PropertyChangeMask | StructureNotifyMask);
1765 set_win_state(win, NormalState);
1771 configurerequest(XEvent *e)
1773 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1778 if ((win = find_window(ev->window)) == NULL)
1782 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
1785 fprintf(stderr, "configurerequest: new window: %lu x %d y %d w %d h %d bw %d s %d sm %d\n",
1786 ev->window, ev->x, ev->y, ev->width, ev->height, ev->border_width, ev->above, ev->detail);
1788 bzero(&wc, sizeof wc);
1791 wc.width = ev->width;
1792 wc.height = ev->height;
1793 wc.border_width = ev->border_width;
1794 wc.sibling = ev->above;
1795 wc.stack_mode = ev->detail;
1796 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
1799 fprintf(stderr, "configurerequest: change window: %lu\n",
1802 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
1804 if (win->floating) {
1805 if (ev->value_mask & CWX)
1807 if (ev->value_mask & CWY)
1809 if (ev->value_mask & CWWidth)
1810 win->g.w = ev->width;
1811 if (ev->value_mask & CWHeight)
1812 win->g.h = ev->height;
1813 if (win->ws->r != NULL) {
1814 /* this seems to be full screen */
1815 if (win->g.w > WIDTH(win->ws->r)) {
1820 if (win->g.h > HEIGHT(win->ws->r)) {
1826 if ((ev->value_mask & (CWX|CWY)) &&
1827 !(ev->value_mask & (CWWidth|CWHeight)))
1829 XMoveResizeWindow(display, win->id,
1830 win->g.x, win->g.y, win->g.w, win->g.h);
1837 configurenotify(XEvent *e)
1839 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1840 e->xconfigure.window);
1844 destroynotify(XEvent *e)
1847 XDestroyWindowEvent *ev = &e->xdestroywindow;
1848 struct workspace *ws;
1850 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1852 if ((win = find_window(ev->window)) != NULL) {
1854 /* find a window to focus */
1855 if (ws->focus == win)
1856 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1857 if (ws->focus == NULL)
1858 ws->focus = TAILQ_FIRST(&ws->winlist);
1859 if (ws->focus == win)
1861 if (cur_focus == win)
1862 focus_win(ws->focus);
1864 TAILQ_REMOVE(&ws->winlist, win, entry);
1865 set_win_state(win, WithdrawnState);
1872 enternotify(XEvent *e)
1874 XCrossingEvent *ev = &e->xcrossing;
1877 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1880 /* eat event(r) to prevent autofocus */
1885 if ((win = find_window(ev->window)) != NULL)
1892 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
1896 mappingnotify(XEvent *e)
1898 XMappingEvent *ev = &e->xmapping;
1900 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1902 XRefreshKeyboardMapping(ev);
1903 if (ev->request == MappingKeyboard)
1908 maprequest(XEvent *e)
1910 XMapRequestEvent *ev = &e->xmaprequest;
1911 XWindowAttributes wa;
1913 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1914 e->xmaprequest.window);
1916 if (!XGetWindowAttributes(display, ev->window, &wa))
1918 if (wa.override_redirect)
1920 manage_window(e->xmaprequest.window);
1925 propertynotify(XEvent *e)
1927 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1928 e->xproperty.window);
1932 unmapnotify(XEvent *e)
1934 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1938 visibilitynotify(XEvent *e)
1941 struct swm_region *r;
1943 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
1944 e->xvisibility.window);
1945 if (e->xvisibility.state == VisibilityUnobscured)
1946 for (i = 0; i < ScreenCount(display); i++)
1947 TAILQ_FOREACH(r, &screens[i].rl, entry)
1948 if (e->xvisibility.window == r->bar_window)
1952 void (*handler[LASTEvent])(XEvent *) = {
1954 [KeyPress] = keypress,
1955 [ButtonPress] = buttonpress,
1956 [ConfigureRequest] = configurerequest,
1957 [ConfigureNotify] = configurenotify,
1958 [DestroyNotify] = destroynotify,
1959 [EnterNotify] = enternotify,
1960 [FocusIn] = focusin,
1961 [MappingNotify] = mappingnotify,
1962 [MapRequest] = maprequest,
1963 [PropertyNotify] = propertynotify,
1964 [UnmapNotify] = unmapnotify,
1965 [VisibilityNotify] = visibilitynotify,
1969 xerror_start(Display *d, XErrorEvent *ee)
1976 xerror(Display *d, XErrorEvent *ee)
1978 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1986 xerrorxlib = XSetErrorHandler(xerror_start);
1988 /* this causes an error if some other window manager is running */
1989 XSelectInput(display, DefaultRootWindow(display),
1990 SubstructureRedirectMask);
1991 XSync(display, False);
1995 XSetErrorHandler(xerror);
1996 XSync(display, False);
2005 unsigned char *p = NULL;
2006 unsigned long n, extra;
2009 astate = XInternAtom(display, "WM_STATE", False);
2010 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
2011 &real, &format, &n, &extra, (unsigned char **)&p);
2012 if (status != Success)
2021 new_region(struct swm_screen *s, struct workspace *ws,
2022 int x, int y, int w, int h)
2024 struct swm_region *r;
2026 DNPRINTF(SWM_D_MISC, "new region on screen %d: %dx%d (%d, %d)\n",
2027 s->idx, x, y, w, h);
2029 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
2030 errx(1, "calloc: failed to allocate memory for screen");
2039 TAILQ_INSERT_TAIL(&s->rl, r, entry);
2046 #ifdef SWM_XRR_HAS_CRTC
2048 XRRScreenResources *sr;
2050 #endif /* SWM_XRR_HAS_CRTC */
2051 Window d1, d2, *wins = NULL;
2052 XWindowAttributes wa;
2053 struct swm_region *r;
2055 int errorbase, major, minor;
2056 int ncrtc = 0, w = 0;
2058 struct workspace *ws;
2062 if ((screens = calloc(ScreenCount(display),
2063 sizeof(struct swm_screen))) == NULL)
2064 errx(1, "calloc: screens");
2066 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2069 /* map physical screens */
2070 for (i = 0; i < ScreenCount(display); i++) {
2071 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
2073 TAILQ_INIT(&screens[i].rl);
2074 screens[i].root = RootWindow(display, i);
2076 /* set default colors */
2077 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
2078 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
2079 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
2080 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
2081 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
2083 /* init all workspaces */
2084 for (j = 0; j < SWM_WS_MAX; j++) {
2085 ws = &screens[i].ws[j];
2090 TAILQ_INIT(&ws->winlist);
2092 for (k = 0; layouts[k].l_stack != NULL; k++)
2093 if (layouts[k].l_config != NULL)
2094 layouts[k].l_config(ws,
2095 SWM_ARG_ID_STACKINIT);
2096 ws->cur_layout = &layouts[0];
2099 /* map virtual screens onto physical screens */
2100 screens[i].xrandr_support = XRRQueryExtension(display,
2101 &xrandr_eventbase, &errorbase);
2102 if (screens[i].xrandr_support)
2103 if (XRRQueryVersion(display, &major, &minor) &&
2105 screens[i].xrandr_support = 0;
2107 #if 0 /* not ready for dynamic screen changes */
2108 if (screens[i].xrandr_support)
2109 XRRSelectInput(display,
2111 RRScreenChangeNotifyMask);
2114 /* grab existing windows (before we build the bars)*/
2115 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
2118 #ifdef SWM_XRR_HAS_CRTC
2119 sr = XRRGetScreenResources(display, screens[i].root);
2121 new_region(&screens[i], &screens[i].ws[w],
2122 0, 0, DisplayWidth(display, i),
2123 DisplayHeight(display, i));
2127 for (c = 0, ci = NULL; c < ncrtc; c++) {
2128 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
2129 if (ci->noutput == 0)
2132 if (ci != NULL && ci->mode == None)
2133 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2134 DisplayWidth(display, i),
2135 DisplayHeight(display, i));
2137 new_region(&screens[i], &screens[i].ws[w],
2138 ci->x, ci->y, ci->width, ci->height);
2142 XRRFreeCrtcInfo(ci);
2143 XRRFreeScreenResources(sr);
2145 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2146 DisplayWidth(display, i),
2147 DisplayHeight(display, i));
2148 #endif /* SWM_XRR_HAS_CRTC */
2150 /* attach windows to a region */
2151 /* normal windows */
2152 if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
2153 errx(1, "no regions on screen %d", i);
2155 for (i = 0; i < no; i++) {
2156 XGetWindowAttributes(display, wins[i], &wa);
2157 if (!XGetWindowAttributes(display, wins[i], &wa) ||
2158 wa.override_redirect ||
2159 XGetTransientForHint(display, wins[i], &d1))
2162 if (wa.map_state == IsViewable ||
2163 getstate(wins[i]) == NormalState)
2164 manage_window(wins[i]);
2166 /* transient windows */
2167 for (i = 0; i < no; i++) {
2168 if (!XGetWindowAttributes(display, wins[i], &wa))
2171 if (XGetTransientForHint(display, wins[i], &d1) &&
2172 (wa.map_state == IsViewable || getstate(wins[i]) ==
2174 manage_window(wins[i]);
2184 main(int argc, char *argv[])
2187 char conf[PATH_MAX], *cfile = NULL;
2194 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
2195 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2196 warnx("no locale support");
2198 if (!(display = XOpenDisplay(0)))
2199 errx(1, "can not open display");
2202 errx(1, "other wm running");
2204 astate = XInternAtom(display, "WM_STATE", False);
2206 /* look for local and global conf file */
2207 pwd = getpwuid(getuid());
2209 errx(1, "invalid user %d", getuid());
2213 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
2214 if (stat(conf, &sb) != -1) {
2215 if (S_ISREG(sb.st_mode))
2218 /* try global conf file */
2219 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
2220 if (!stat(conf, &sb))
2221 if (S_ISREG(sb.st_mode))
2228 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2233 xfd = ConnectionNumber(display);
2237 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
2239 errx(1, "select failed");
2244 while(XPending(display)) {
2245 XNextEvent(display, &e);
2246 if (handler[e.type])
2247 handler[e.type](&e);
2251 XCloseDisplay(display);