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.6"
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[] = { "screenshot.sh", NULL, NULL };
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;
221 TAILQ_HEAD(ws_win_list, ws_win);
223 /* layout handlers */
225 void vertical_config(struct workspace *, int);
226 void vertical_stack(struct workspace *, struct swm_geometry *);
227 void horizontal_config(struct workspace *, int);
228 void horizontal_stack(struct workspace *, struct swm_geometry *);
229 void max_stack(struct workspace *, struct swm_geometry *);
231 void grabbuttons(struct ws_win *, int);
234 void (*l_stack)(struct workspace *, struct swm_geometry *);
235 void (*l_config)(struct workspace *, int);
237 /* stack, configure */
238 { vertical_stack, vertical_config},
239 { horizontal_stack, horizontal_config},
244 #define SWM_H_SLICE (32)
245 #define SWM_V_SLICE (32)
247 /* define work spaces */
249 int idx; /* workspace index */
250 int restack; /* restack on switch */
251 struct layout *cur_layout; /* current layout handlers */
252 struct ws_win *focus; /* may be NULL */
253 struct swm_region *r; /* may be NULL */
254 struct ws_win_list winlist; /* list of windows in ws */
258 int horizontal_msize;
265 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
266 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
268 /* physical screen mapping */
269 #define SWM_WS_MAX (10) /* XXX Too small? */
271 int idx; /* screen index */
272 struct swm_region_list rl; /* list of regions on this screen */
275 struct workspace ws[SWM_WS_MAX];
281 } c[SWM_S_COLOR_MAX];
283 struct swm_screen *screens;
286 struct ws_win *cur_focus = NULL;
288 /* args to functions */
291 #define SWM_ARG_ID_FOCUSNEXT (0)
292 #define SWM_ARG_ID_FOCUSPREV (1)
293 #define SWM_ARG_ID_FOCUSMAIN (2)
294 #define SWM_ARG_ID_SWAPNEXT (3)
295 #define SWM_ARG_ID_SWAPPREV (4)
296 #define SWM_ARG_ID_SWAPMAIN (5)
297 #define SWM_ARG_ID_MASTERSHRINK (6)
298 #define SWM_ARG_ID_MASTERGROW (7)
299 #define SWM_ARG_ID_MASTERADD (8)
300 #define SWM_ARG_ID_MASTERDEL (9)
301 #define SWM_ARG_ID_STACKRESET (10)
302 #define SWM_ARG_ID_STACKINIT (11)
303 #define SWM_ARG_ID_CYCLEWS_UP (12)
304 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
305 #define SWM_ARG_ID_CYCLESC_UP (14)
306 #define SWM_ARG_ID_CYCLESC_DOWN (15)
307 #define SWM_ARG_ID_SS_ALL (0)
308 #define SWM_ARG_ID_SS_WINDOW (1)
313 name_to_color(char *colorname)
317 XColor screen_def, exact_def;
318 unsigned long result = 0;
319 char cname[32] = "#";
321 cmap = DefaultColormap(display, screens[0].idx);
322 status = XAllocNamedColor(display, cmap, colorname,
323 &screen_def, &exact_def);
325 strlcat(cname, colorname + 2, sizeof cname - 1);
326 status = XAllocNamedColor(display, cmap, cname, &screen_def,
330 result = screen_def.pixel;
332 fprintf(stderr, "color '%s' not found.\n", colorname);
338 setscreencolor(char *val, int i, int c)
340 if (i > 0 && i <= ScreenCount(display)) {
341 screens[i - 1].c[c].color = name_to_color(val);
342 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
344 } else if (i == -1) {
345 for (i = 0; i < ScreenCount(display); i++)
346 screens[i].c[c].color = name_to_color(val);
347 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
350 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
351 i, ScreenCount(display));
355 varmatch(char *var, char *name, int *index)
360 i = strncmp(var, name, 255);
367 p = var + strlen(name);
370 bzero(buf, sizeof buf);
372 while (isdigit(*p) && i < sizeof buf)
374 if (i == 0 || i >= sizeof buf || *p != ']')
376 *index = strtonum(buf, 0, 99, NULL);
380 /* conf file stuff */
381 #define SWM_CONF_WS "\n= \t"
382 #define SWM_CONF_FILE "scrotwm.conf"
384 conf_load(char *filename)
387 char *line, *cp, *var, *val;
388 size_t len, lineno = 0;
391 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
393 if (filename == NULL)
396 if ((config = fopen(filename, "r")) == NULL)
399 for (sc = ScreenCount(display);;) {
400 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
404 cp += (long)strspn(cp, SWM_CONF_WS);
410 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
412 cp += (long)strspn(cp, SWM_CONF_WS);
413 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
416 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
419 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
420 bar_enabled = atoi(val);
421 else if (!varmatch(var, "bar_border", &i))
422 setscreencolor(val, i, SWM_S_COLOR_BAR_BORDER);
423 else if (!varmatch(var, "bar_color", &i))
424 setscreencolor(val, i, SWM_S_COLOR_BAR);
425 else if (!varmatch(var, "bar_font_color", &i))
426 setscreencolor(val, i, SWM_S_COLOR_BAR_FONT);
427 else if (!strncmp(var, "bar_font", strlen("bar_font")))
428 asprintf(&bar_fonts[0], "%s", val);
429 else if (!strncmp(var, "bar_action", strlen("bar_action")))
430 asprintf(&bar_argv[0], "%s", val);
431 else if (!strncmp(var, "bar_delay", strlen("bar_delay")))
432 bar_delay = atoi(val);
438 if (!varmatch(var, "color_focus", &i))
439 setscreencolor(val, i, SWM_S_COLOR_FOCUS);
440 else if (!varmatch(var, "color_unfocus", &i))
441 setscreencolor(val, i, SWM_S_COLOR_UNFOCUS);
442 else if (!strncmp(var, "cycle_empty", strlen("cycle_empty")))
443 cycle_visible = atoi(val);
444 else if (!strncmp(var, "cycle_visible", strlen("cycle_visible")))
445 cycle_visible = atoi(val);
451 if (!strncmp(var, "dialog_ratio",
452 strlen("dialog_ratio"))) {
453 dialog_ratio = atof(val);
454 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
461 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
462 asprintf(&spawn_term[0], "%s", val);
463 if (!strncmp(var, "screenshot_enabled",
464 strlen("screenshot_enabled")))
465 ss_enabled = atoi(val);
466 if (!strncmp(var, "screenshot_app",
467 strlen("screenshot_app")))
468 asprintf(&spawn_screenshot[0], "%s", val);
480 errx(1, "invalid conf file entry: %s=%s", var, val);
484 socket_setnonblock(int fd)
488 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
489 err(1, "fcntl F_GETFL");
491 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
492 err(1, "fcntl F_SETFL");
496 bar_print(struct swm_region *r, char *s)
498 XClearWindow(display, r->bar_window);
499 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
500 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
509 bzero(bar_pipe, sizeof bar_pipe);
512 kill(bar_pid, SIGTERM);
515 strlcpy(bar_ext, "", sizeof bar_ext);
524 struct swm_region *r;
528 char loc[SWM_BAR_MAX];
531 if (bar_enabled == 0)
534 if (bar_extra && bar_extra_running) {
535 /* ignore short reads; it'll correct itself */
536 while ((b = fgetln(stdin, &len)) != NULL)
537 if (b && b[len - 1] == '\n') {
539 strlcpy(bar_ext, b, sizeof bar_ext);
541 if (b == NULL && errno != EAGAIN) {
542 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
543 errno, strerror(errno));
547 strlcpy(bar_ext, "", sizeof bar_ext);
550 localtime_r(&tmt, &tm);
551 strftime(s, sizeof s, "%a %b %d %R %Z %Y", &tm);
552 for (i = 0; i < ScreenCount(display); i++) {
554 TAILQ_FOREACH(r, &screens[i].rl, entry) {
555 snprintf(loc, sizeof loc, "%s %d:%d %s",
556 s, x++, r->ws->idx + 1, bar_ext);
560 XSync(display, False);
571 bar_toggle(struct swm_region *r, union arg *args)
573 struct swm_region *tmpr;
574 int i, j, sc = ScreenCount(display);
576 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
579 for (i = 0; i < sc; i++)
580 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
581 XUnmapWindow(display, tmpr->bar_window);
583 for (i = 0; i < sc; i++)
584 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
585 XMapRaised(display, tmpr->bar_window);
587 bar_enabled = !bar_enabled;
588 XSync(display, False);
589 for (i = 0; i < sc; i++)
590 for (j = 0; j < SWM_WS_MAX; j++)
591 screens[i].ws[j].restack = 1;
594 /* must be after stack */
595 for (i = 0; i < sc; i++)
596 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
603 XSetWindowAttributes wa;
604 struct swm_region *r;
607 /* do this here because the conf file is in memory */
608 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
609 /* launch external status app */
610 bar_extra_running = 1;
611 if (pipe(bar_pipe) == -1)
612 err(1, "pipe error");
613 socket_setnonblock(bar_pipe[0]);
614 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
615 if (dup2(bar_pipe[0], 0) == -1)
617 if (dup2(bar_pipe[1], 1) == -1)
619 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
620 err(1, "could not disable SIGPIPE");
621 switch (bar_pid = fork()) {
623 err(1, "cannot fork");
627 execvp(bar_argv[0], bar_argv);
628 err(1, "%s external app failed", bar_argv[0]);
630 default: /* parent */
636 bzero(&wa, sizeof wa);
637 for (i = 0; i < ScreenCount(display); i++)
638 TAILQ_FOREACH(r, &screens[i].rl, entry) {
640 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
641 wa.background_pixel =
642 screens[i].c[SWM_S_COLOR_BAR].color;
643 XChangeWindowAttributes(display, r->bar_window,
644 CWBackPixel | CWBorderPixel, &wa);
650 bar_setup(struct swm_region *r)
654 for (i = 0; bar_fonts[i] != NULL; i++) {
655 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
661 if (bar_fonts[i] == NULL)
662 errx(1, "couldn't load font");
663 bar_height = bar_fs->ascent + bar_fs->descent + 3;
665 r->bar_window = XCreateSimpleWindow(display,
666 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
667 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
668 r->s->c[SWM_S_COLOR_BAR].color);
669 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
670 XSetFont(display, bar_gc, bar_fs->fid);
671 XSelectInput(display, r->bar_window, VisibilityChangeMask);
673 XMapRaised(display, r->bar_window);
674 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
676 if (signal(SIGALRM, bar_signal) == SIG_ERR)
677 err(1, "could not install bar_signal");
682 config_win(struct ws_win *win)
686 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
687 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
688 ce.type = ConfigureNotify;
689 ce.display = display;
695 ce.height = win->g.h;
696 ce.border_width = 1; /* XXX store this! */
698 ce.override_redirect = False;
699 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
703 count_win(struct workspace *ws, int count_transient)
708 TAILQ_FOREACH(win, &ws->winlist, entry) {
709 if (count_transient == 0 && win->floating)
711 if (count_transient == 0 && win->transient)
715 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
721 quit(struct swm_region *r, union arg *args)
723 DNPRINTF(SWM_D_MISC, "quit\n");
733 for (i = 0; i < ScreenCount(display); i++)
734 for (j = 0; j < SWM_WS_MAX; j++)
735 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
736 XUnmapWindow(display, win->id);
740 restart(struct swm_region *r, union arg *args)
742 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
744 /* disable alarm because the following code may not be interrupted */
746 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
747 errx(1, "can't disable alarm");
752 XCloseDisplay(display);
753 execvp(start_argv[0], start_argv);
754 fprintf(stderr, "execvp failed\n");
760 root_to_region(Window root)
762 struct swm_region *r;
767 for (i = 0; i < ScreenCount(display); i++)
768 if (screens[i].root == root)
771 if (XQueryPointer(display, screens[i].root,
772 &rr, &cr, &x, &y, &wx, &wy, &mask) == False) {
773 /* if we can't query the pointer, grab the first region */
774 r = TAILQ_FIRST(&screens[i].rl);
776 /* otherwise, choose a region based on pointer location */
777 TAILQ_FOREACH(r, &screens[i].rl, entry) {
778 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
779 y >= Y(r) && y <= Y(r) + HEIGHT(r))
784 r = TAILQ_FIRST(&screens[i].rl);
790 find_window(Window id)
795 for (i = 0; i < ScreenCount(display); i++)
796 for (j = 0; j < SWM_WS_MAX; j++)
797 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
804 spawn(struct swm_region *r, union arg *args)
808 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
810 * The double-fork construct avoids zombie processes and keeps the code
811 * clean from stupid signal handlers.
816 close(ConnectionNumber(display));
817 setenv("LD_PRELOAD", SWM_LIB, 1);
818 if (asprintf(&ret, "%d", r->ws->idx)) {
819 setenv("_SWM_WS", ret, 1);
822 if (asprintf(&ret, "%d", getpid())) {
823 setenv("_SWM_PID", ret, 1);
827 execvp(args->argv[0], args->argv);
828 fprintf(stderr, "execvp failed\n");
837 spawnmenu(struct swm_region *r, union arg *args)
839 DNPRINTF(SWM_D_MISC, "spawnmenu\n");
841 spawn_menu[SWM_MENU_FN] = bar_fonts[bar_fidx];
842 spawn_menu[SWM_MENU_NB] = r->s->c[SWM_S_COLOR_BAR].name;
843 spawn_menu[SWM_MENU_NF] = r->s->c[SWM_S_COLOR_BAR_FONT].name;
844 spawn_menu[SWM_MENU_SB] = r->s->c[SWM_S_COLOR_BAR_BORDER].name;
845 spawn_menu[SWM_MENU_SF] = r->s->c[SWM_S_COLOR_BAR].name;
856 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
858 for (i = 0; i < ScreenCount(display); i++)
859 for (j = 0; j < SWM_WS_MAX; j++)
860 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
861 if (win->ws->r == NULL)
864 XSetWindowBorder(display, win->id,
865 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
867 win->ws->focus = NULL;
873 focus_win(struct ws_win *win)
875 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
881 win->ws->focus = win;
882 if (win->ws->r != NULL) {
884 if (!win->got_focus) {
885 XSetWindowBorder(display, win->id,
886 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
890 XSetInputFocus(display, win->id,
891 RevertToPointerRoot, CurrentTime);
896 switchws(struct swm_region *r, union arg *args)
899 struct swm_region *this_r, *other_r;
901 struct workspace *new_ws, *old_ws;
905 new_ws = &this_r->s->ws[wsid];
907 DNPRINTF(SWM_D_WS, "switchws screen %d region %dx%d+%d+%d: "
908 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
911 if (new_ws == old_ws)
916 /* if the other workspace is hidden, switch windows */
917 /* map new window first to prevent ugly blinking */
918 TAILQ_FOREACH(win, &new_ws->winlist, entry)
919 XMapRaised(display, win->id);
921 TAILQ_FOREACH(win, &old_ws->winlist, entry)
922 XUnmapWindow(display, win->id);
926 other_r->ws = old_ws;
935 focus_win(new_ws->focus);
941 cyclews(struct swm_region *r, union arg *args)
944 struct swm_screen *s = r->s;
946 DNPRINTF(SWM_D_WS, "cyclews id %d "
947 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
948 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
953 case SWM_ARG_ID_CYCLEWS_UP:
954 if (a.id < SWM_WS_MAX - 1)
959 case SWM_ARG_ID_CYCLEWS_DOWN:
963 a.id = SWM_WS_MAX - 1;
969 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
971 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
975 } while (a.id != r->ws->idx);
979 cyclescr(struct swm_region *r, union arg *args)
981 struct swm_region *rr;
986 case SWM_ARG_ID_CYCLESC_UP:
987 rr = TAILQ_NEXT(r, entry);
989 rr = TAILQ_FIRST(&screens[i].rl);
991 case SWM_ARG_ID_CYCLESC_DOWN:
992 rr = TAILQ_PREV(r, swm_region_list, entry);
994 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1000 XSetInputFocus(display, PointerRoot, RevertToPointerRoot, CurrentTime);
1001 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, rr->g.x,
1002 rr->g.y + bar_enabled ? bar_height : 0);
1006 swapwin(struct swm_region *r, union arg *args)
1008 struct ws_win *target;
1009 struct ws_win_list *wl;
1012 DNPRINTF(SWM_D_WS, "swapwin id %d "
1013 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1014 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1015 if (cur_focus == NULL)
1018 wl = &cur_focus->ws->winlist;
1021 case SWM_ARG_ID_SWAPPREV:
1022 target = TAILQ_PREV(cur_focus, ws_win_list, entry);
1023 TAILQ_REMOVE(wl, cur_focus, entry);
1025 TAILQ_INSERT_TAIL(wl, cur_focus, entry);
1027 TAILQ_INSERT_BEFORE(target, cur_focus, entry);
1029 case SWM_ARG_ID_SWAPNEXT:
1030 target = TAILQ_NEXT(cur_focus, entry);
1031 TAILQ_REMOVE(wl, cur_focus, entry);
1033 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
1035 TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
1037 case SWM_ARG_ID_SWAPMAIN:
1038 target = TAILQ_FIRST(wl);
1039 if (target == cur_focus)
1041 TAILQ_REMOVE(wl, target, entry);
1042 TAILQ_INSERT_BEFORE(cur_focus, target, entry);
1043 TAILQ_REMOVE(wl, cur_focus, entry);
1044 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
1047 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1056 focus(struct swm_region *r, union arg *args)
1058 struct ws_win *winfocus, *winlostfocus;
1059 struct ws_win_list *wl;
1061 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1062 if (cur_focus == NULL)
1065 wl = &cur_focus->ws->winlist;
1067 winlostfocus = cur_focus;
1070 case SWM_ARG_ID_FOCUSPREV:
1071 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1072 if (winfocus == NULL)
1073 winfocus = TAILQ_LAST(wl, ws_win_list);
1076 case SWM_ARG_ID_FOCUSNEXT:
1077 winfocus = TAILQ_NEXT(cur_focus, entry);
1078 if (winfocus == NULL)
1079 winfocus = TAILQ_FIRST(wl);
1082 case SWM_ARG_ID_FOCUSMAIN:
1083 winfocus = TAILQ_FIRST(wl);
1090 if (winfocus == winlostfocus || winfocus == NULL)
1093 XMapRaised(display, winfocus->id);
1094 focus_win(winfocus);
1095 XSync(display, False);
1099 cycle_layout(struct swm_region *r, union arg *args)
1101 struct workspace *ws = r->ws;
1103 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1106 if (ws->cur_layout->l_stack == NULL)
1107 ws->cur_layout = &layouts[0];
1113 stack_config(struct swm_region *r, union arg *args)
1115 struct workspace *ws = r->ws;
1117 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1120 if (ws->cur_layout->l_config != NULL)
1121 ws->cur_layout->l_config(ws, args->id);
1123 if (args->id != SWM_ARG_ID_STACKINIT);
1129 struct swm_geometry g;
1130 struct swm_region *r;
1133 DNPRINTF(SWM_D_STACK, "stack\n");
1135 for (i = 0; i < ScreenCount(display); i++) {
1137 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1138 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1139 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1141 /* start with screen geometry, adjust for bar */
1151 r->ws->cur_layout->l_stack(r->ws, &g);
1154 XSync(display, False);
1158 stack_floater(struct ws_win *win, struct swm_region *r)
1163 bzero(&wc, sizeof wc);
1164 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1165 wc.border_width = 1;
1166 if (win->transient) {
1167 win->g.w = (double)WIDTH(r) * dialog_ratio;
1168 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1170 wc.width = win->g.w;
1171 wc.height = win->g.h;
1172 wc.x = (WIDTH(r) - win->g.w) / 2;
1173 wc.y = (HEIGHT(r) - win->g.h) / 2;
1175 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1176 win->id, wc.x, wc.y, wc.width, wc.height);
1178 XConfigureWindow(display, win->id, mask, &wc);
1181 #define SWAPXY(g) do { \
1183 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1184 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1187 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1190 struct swm_geometry win_g, r_g = *g;
1191 struct ws_win *win, *winfocus;
1192 int i, j, w_inc, h_inc, w_base, h_base;
1193 int hrh, extra, h_slice, last_h = 0;
1194 int split, colno, winno, mwin, msize, mscale;
1195 int remain, missing, v_slice;;
1198 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1199 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1201 if ((winno = count_win(ws, 0)) == 0)
1204 if (ws->focus == NULL)
1205 ws->focus = TAILQ_FIRST(&ws->winlist);
1206 winfocus = cur_focus ? cur_focus : ws->focus;
1208 win = TAILQ_FIRST(&ws->winlist);
1210 w_inc = win->sh.width_inc;
1211 w_base = win->sh.base_width;
1212 mwin = ws->l_state.horizontal_mwin;
1213 mscale = ws->l_state.horizontal_msize;
1216 w_inc = win->sh.height_inc;
1217 w_base = win->sh.base_height;
1218 mwin = ws->l_state.vertical_mwin;
1219 mscale = ws->l_state.vertical_msize;
1223 h_slice = r_g.h / SWM_H_SLICE;
1224 if (mwin && winno > mwin) {
1225 v_slice = r_g.w / SWM_V_SLICE;
1229 msize = v_slice * mscale;
1231 if (w_inc > 1 && w_inc < v_slice) {
1232 /* adjust for window's requested size increment */
1233 remain = (win_g.w - w_base) % w_inc;
1234 missing = w_inc - remain;
1236 if (missing <= extra || j == 0) {
1247 win_g.x += r_g.w - msize;
1252 hrh = r_g.h / colno;
1253 extra = r_g.h - (colno * hrh);
1257 TAILQ_FOREACH(win, &ws->winlist, entry) {
1258 if (split && i == split) {
1259 colno = winno - split;
1260 hrh = (r_g.h / colno);
1261 extra = r_g.h - (colno * hrh);
1265 win_g.x += msize + 2;
1266 win_g.w = r_g.w - (msize + 2);
1271 h_inc = win->sh.width_inc;
1272 h_base = win->sh.base_width;
1274 h_inc = win->sh.height_inc;
1275 h_base = win->sh.base_height;
1277 if (j == colno - 1) {
1278 win_g.h = hrh + extra;
1279 } else if (h_inc > 1 && h_inc < h_slice) {
1280 /* adjust for window's requested size increment */
1281 remain = (win_g.h - h_base) % h_inc;
1282 missing = h_inc - remain;
1284 if (missing <= extra || j == 0) {
1296 win_g.y += last_h + 2;
1298 if (win->transient != 0 || win->floating != 0)
1299 stack_floater(win, ws->r);
1301 bzero(&wc, sizeof wc);
1302 wc.border_width = 1;
1304 win->g.x = wc.x = win_g.y;
1305 win->g.y = wc.y = win_g.x;
1306 win->g.w = wc.width = win_g.h;
1307 win->g.h = wc.height = win_g.w;
1309 win->g.x = wc.x = win_g.x;
1310 win->g.y = wc.y = win_g.y;
1311 win->g.w = wc.width = win_g.w;
1312 win->g.h = wc.height = win_g.h;
1314 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1315 XConfigureWindow(display, win->id, mask, &wc);
1317 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);
1321 XMapRaised(display, win->id);
1328 focus_win(winfocus); /* has to be done outside of the loop */
1332 vertical_config(struct workspace *ws, int id)
1334 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1337 case SWM_ARG_ID_STACKRESET:
1338 case SWM_ARG_ID_STACKINIT:
1339 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1340 ws->l_state.vertical_mwin = 1;
1342 case SWM_ARG_ID_MASTERSHRINK:
1343 if (ws->l_state.vertical_msize > 1)
1344 ws->l_state.vertical_msize--;
1346 case SWM_ARG_ID_MASTERGROW:
1347 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1348 ws->l_state.vertical_msize++;
1350 case SWM_ARG_ID_MASTERADD:
1351 ws->l_state.vertical_mwin++;
1353 case SWM_ARG_ID_MASTERDEL:
1354 if (ws->l_state.vertical_mwin > 0)
1355 ws->l_state.vertical_mwin--;
1363 vertical_stack(struct workspace *ws, struct swm_geometry *g)
1365 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1367 stack_master(ws, g, 0, 0);
1371 horizontal_config(struct workspace *ws, int id)
1373 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1376 case SWM_ARG_ID_STACKRESET:
1377 case SWM_ARG_ID_STACKINIT:
1378 ws->l_state.horizontal_mwin = 1;
1379 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1381 case SWM_ARG_ID_MASTERSHRINK:
1382 if (ws->l_state.horizontal_msize > 1)
1383 ws->l_state.horizontal_msize--;
1385 case SWM_ARG_ID_MASTERGROW:
1386 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1387 ws->l_state.horizontal_msize++;
1389 case SWM_ARG_ID_MASTERADD:
1390 ws->l_state.horizontal_mwin++;
1392 case SWM_ARG_ID_MASTERDEL:
1393 if (ws->l_state.horizontal_mwin > 0)
1394 ws->l_state.horizontal_mwin--;
1402 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
1404 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1406 stack_master(ws, g, 1, 0);
1409 /* fullscreen view */
1411 max_stack(struct workspace *ws, struct swm_geometry *g) {
1413 struct swm_geometry gg = *g;
1414 struct ws_win *win, *winfocus;
1417 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1419 if (count_win(ws, 0) == 0)
1422 if (ws->focus == NULL)
1423 ws->focus = TAILQ_FIRST(&ws->winlist);
1424 winfocus = cur_focus ? cur_focus : ws->focus;
1426 TAILQ_FOREACH(win, &ws->winlist, entry) {
1427 if (win->transient != 0 || win->floating != 0) {
1428 if (win == ws->focus) {
1430 stack_floater(win, ws->r);
1431 XMapRaised(display, win->id);
1433 XUnmapWindow(display, win->id);
1435 bzero(&wc, sizeof wc);
1436 wc.border_width = 1;
1437 win->g.x = wc.x = gg.x;
1438 win->g.y = wc.y = gg.y;
1439 win->g.w = wc.width = gg.w;
1440 win->g.h = wc.height = gg.h;
1441 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1442 XConfigureWindow(display, win->id, mask, &wc);
1444 if (win == ws->focus) {
1445 XMapRaised(display, win->id);
1447 XUnmapWindow(display, win->id);
1452 focus_win(winfocus); /* has to be done outside of the loop */
1456 send_to_ws(struct swm_region *r, union arg *args)
1458 int wsid = args->id;
1459 struct ws_win *win = cur_focus;
1460 struct workspace *ws, *nws;
1461 Atom ws_idx_atom = 0;
1462 unsigned char ws_idx_str[SWM_PROPLEN];
1464 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1467 nws = &win->s->ws[wsid];
1469 XUnmapWindow(display, win->id);
1471 /* find a window to focus */
1472 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1473 if (ws->focus == NULL)
1474 ws->focus = TAILQ_FIRST(&ws->winlist);
1475 if (ws->focus == win)
1478 TAILQ_REMOVE(&ws->winlist, win, entry);
1480 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1483 /* Try to update the window's workspace property */
1484 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1486 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
1487 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1489 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1490 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1493 if (count_win(nws, 1) == 1)
1502 wkill(struct swm_region *r, union arg *args)
1504 DNPRINTF(SWM_D_MISC, "wkill\n");
1505 if(r->ws->focus != NULL)
1506 XKillClient(display, r->ws->focus->id);
1510 screenshot(struct swm_region *r, union arg *args)
1514 DNPRINTF(SWM_D_MISC, "screenshot\n");
1516 if (ss_enabled == 0)
1520 case SWM_ARG_ID_SS_ALL:
1521 spawn_screenshot[1] = "full";
1523 case SWM_ARG_ID_SS_WINDOW:
1524 spawn_screenshot[1] = "window";
1529 a.argv = spawn_screenshot;
1533 /* key definitions */
1537 void (*func)(struct swm_region *r, union arg *);
1540 /* modifier key function argument */
1541 { MODKEY, XK_space, cycle_layout, {0} },
1542 { MODKEY | ShiftMask, XK_space, stack_config, {.id = SWM_ARG_ID_STACKRESET} },
1543 { MODKEY, XK_h, stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
1544 { MODKEY, XK_l, stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
1545 { MODKEY, XK_comma, stack_config, {.id = SWM_ARG_ID_MASTERADD} },
1546 { MODKEY, XK_period, stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
1547 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1548 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1549 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1550 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1551 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1552 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1553 { MODKEY, XK_p, spawnmenu, {.argv = spawn_menu} },
1554 { MODKEY | ShiftMask, XK_q, quit, {0} },
1555 { MODKEY, XK_q, restart, {0} },
1556 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1557 { MODKEY, XK_1, switchws, {.id = 0} },
1558 { MODKEY, XK_2, switchws, {.id = 1} },
1559 { MODKEY, XK_3, switchws, {.id = 2} },
1560 { MODKEY, XK_4, switchws, {.id = 3} },
1561 { MODKEY, XK_5, switchws, {.id = 4} },
1562 { MODKEY, XK_6, switchws, {.id = 5} },
1563 { MODKEY, XK_7, switchws, {.id = 6} },
1564 { MODKEY, XK_8, switchws, {.id = 7} },
1565 { MODKEY, XK_9, switchws, {.id = 8} },
1566 { MODKEY, XK_0, switchws, {.id = 9} },
1567 { MODKEY, XK_Right, cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
1568 { MODKEY, XK_Left, cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
1569 { MODKEY | ShiftMask, XK_Right, cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
1570 { MODKEY | ShiftMask, XK_Left, cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
1571 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1572 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1573 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1574 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1575 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1576 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1577 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1578 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1579 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1580 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1581 { MODKEY, XK_b, bar_toggle, {0} },
1582 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1583 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1584 { MODKEY | ShiftMask, XK_x, wkill, {0} },
1585 { MODKEY, XK_s, screenshot, {.id = SWM_ARG_ID_SS_ALL} },
1586 { MODKEY | ShiftMask, XK_s, screenshot, {.id = SWM_ARG_ID_SS_WINDOW} },
1590 click(struct swm_region *r, union arg *args)
1592 DNPRINTF(SWM_D_MOUSE, "click: button: %d\n", args->id);
1607 enum { client_click, root_click };
1609 unsigned int action;
1611 unsigned int button;
1612 void (*func)(struct swm_region *r, union arg *);
1615 /* action key mouse button func args */
1616 { client_click, MODKEY, Button1, click, {.id=Button1} },
1620 updatenumlockmask(void)
1623 XModifierKeymap *modmap;
1625 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1627 modmap = XGetModifierMapping(display);
1628 for (i = 0; i < 8; i++)
1629 for (j = 0; j < modmap->max_keypermod; j++)
1630 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1631 == XKeysymToKeycode(display, XK_Num_Lock))
1632 numlockmask = (1 << i);
1634 XFreeModifiermap(modmap);
1640 unsigned int i, j, k;
1642 unsigned int modifiers[] =
1643 { 0, LockMask, numlockmask, numlockmask | LockMask };
1645 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1646 updatenumlockmask();
1648 for (k = 0; k < ScreenCount(display); k++) {
1649 if (TAILQ_EMPTY(&screens[k].rl))
1651 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
1652 for (i = 0; i < LENGTH(keys); i++) {
1653 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
1654 for (j = 0; j < LENGTH(modifiers); j++)
1655 XGrabKey(display, code,
1656 keys[i].mod | modifiers[j],
1657 screens[k].root, True,
1658 GrabModeAsync, GrabModeAsync);
1664 grabbuttons(struct ws_win *win, int focused)
1667 unsigned int modifiers[] =
1668 { 0, LockMask, numlockmask, numlockmask|LockMask };
1670 updatenumlockmask();
1671 XUngrabButton(display, AnyButton, AnyModifier, win->id);
1673 for (i = 0; i < LENGTH(buttons); i++)
1674 if (buttons[i].action == client_click)
1675 for (j = 0; j < LENGTH(modifiers); j++)
1676 XGrabButton(display, buttons[i].button,
1677 buttons[i].mask | modifiers[j],
1678 win->id, False, BUTTONMASK,
1679 GrabModeAsync, GrabModeSync, None,
1682 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
1683 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1689 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1697 XKeyEvent *ev = &e->xkey;
1699 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1701 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1702 for (i = 0; i < LENGTH(keys); i++)
1703 if (keysym == keys[i].keysym
1704 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1706 keys[i].func(root_to_region(ev->root),
1711 buttonpress(XEvent *e)
1713 XButtonPressedEvent *ev = &e->xbutton;
1718 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1720 action = root_click;
1721 if ((win = find_window(ev->window)) == NULL)
1725 action = client_click;
1728 for (i = 0; i < LENGTH(buttons); i++)
1729 if (action == buttons[i].action && buttons[i].func &&
1730 buttons[i].button == ev->button &&
1731 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
1732 buttons[i].func(root_to_region(ev->root),
1737 set_win_state(struct ws_win *win, long state)
1739 long data[] = {state, None};
1741 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1743 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1744 (unsigned char *)data, 2);
1748 manage_window(Window id)
1751 struct workspace *ws;
1755 unsigned long nitems, bytes;
1756 Atom ws_idx_atom = 0, type;
1757 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
1758 struct swm_region *r;
1763 if ((win = find_window(id)) != NULL)
1764 return (win); /* already being managed */
1766 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1767 errx(1, "calloc: failed to allocate memory for new window");
1769 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1771 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
1772 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
1774 XGetWindowAttributes(display, id, &win->wa);
1775 r = root_to_region(win->wa.root);
1776 /* If the window was managed before, put it in the same workspace */
1778 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
1779 ws_idx = strtonum(prop, 0, 9, &errstr);
1781 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
1783 ws = &r->s->ws[ws_idx];
1789 win->s = r->s; /* this never changes */
1790 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
1792 /* make new win focused */
1795 XGetTransientForHint(display, win->id, &trans);
1797 win->transient = trans;
1798 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1799 (unsigned)win->id, win->transient);
1801 win->g.w = win->wa.width;
1802 win->g.h = win->wa.height;
1803 win->g.x = win->wa.x;
1804 win->g.y = win->wa.y;
1806 XGetWMNormalHints(display, win->id, &win->sh, &mask);
1808 if (ws_idx_atom && prop == NULL &&
1809 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
1810 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1812 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1813 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1818 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);
1821 /* XXX make this a table */
1822 bzero(&ch, sizeof ch);
1823 if (XGetClassHint(display, win->id, &ch)) {
1824 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1825 if (!strcmp(ch.res_class, "MPlayer") && !strcmp(ch.res_name, "xv")) {
1829 XFree(ch.res_class);
1834 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
1835 PropertyChangeMask | StructureNotifyMask);
1837 set_win_state(win, NormalState);
1843 configurerequest(XEvent *e)
1845 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1850 if ((win = find_window(ev->window)) == NULL)
1854 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
1857 fprintf(stderr, "configurerequest: new window: %lu x %d y %d w %d h %d bw %d s %d sm %d\n",
1858 ev->window, ev->x, ev->y, ev->width, ev->height, ev->border_width, ev->above, ev->detail);
1860 bzero(&wc, sizeof wc);
1863 wc.width = ev->width;
1864 wc.height = ev->height;
1865 wc.border_width = ev->border_width;
1866 wc.sibling = ev->above;
1867 wc.stack_mode = ev->detail;
1868 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
1871 fprintf(stderr, "configurerequest: change window: %lu\n",
1874 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
1876 if (win->floating) {
1877 if (ev->value_mask & CWX)
1879 if (ev->value_mask & CWY)
1881 if (ev->value_mask & CWWidth)
1882 win->g.w = ev->width;
1883 if (ev->value_mask & CWHeight)
1884 win->g.h = ev->height;
1885 if (win->ws->r != NULL) {
1886 /* this seems to be full screen */
1887 if (win->g.w > WIDTH(win->ws->r)) {
1892 if (win->g.h > HEIGHT(win->ws->r)) {
1898 if ((ev->value_mask & (CWX|CWY)) &&
1899 !(ev->value_mask & (CWWidth|CWHeight)))
1901 XMoveResizeWindow(display, win->id,
1902 win->g.x, win->g.y, win->g.w, win->g.h);
1909 configurenotify(XEvent *e)
1911 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1912 e->xconfigure.window);
1916 destroynotify(XEvent *e)
1919 XDestroyWindowEvent *ev = &e->xdestroywindow;
1920 struct workspace *ws;
1922 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1924 if ((win = find_window(ev->window)) != NULL) {
1926 /* find a window to focus */
1927 if (ws->focus == win)
1928 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1929 if (ws->focus == NULL)
1930 ws->focus = TAILQ_FIRST(&ws->winlist);
1931 if (ws->focus == NULL || ws->focus == win) {
1935 focus_win(ws->focus);
1936 TAILQ_REMOVE(&ws->winlist, win, entry);
1937 set_win_state(win, WithdrawnState);
1944 enternotify(XEvent *e)
1946 XCrossingEvent *ev = &e->xcrossing;
1949 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1952 /* eat event(r) to prevent autofocus */
1957 if ((win = find_window(ev->window)) != NULL)
1964 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
1968 mappingnotify(XEvent *e)
1970 XMappingEvent *ev = &e->xmapping;
1972 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1974 XRefreshKeyboardMapping(ev);
1975 if (ev->request == MappingKeyboard)
1980 maprequest(XEvent *e)
1982 XMapRequestEvent *ev = &e->xmaprequest;
1983 XWindowAttributes wa;
1985 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1986 e->xmaprequest.window);
1988 if (!XGetWindowAttributes(display, ev->window, &wa))
1990 if (wa.override_redirect)
1992 manage_window(e->xmaprequest.window);
1997 propertynotify(XEvent *e)
1999 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
2000 e->xproperty.window);
2004 unmapnotify(XEvent *e)
2006 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
2010 visibilitynotify(XEvent *e)
2013 struct swm_region *r;
2015 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
2016 e->xvisibility.window);
2017 if (e->xvisibility.state == VisibilityUnobscured)
2018 for (i = 0; i < ScreenCount(display); i++)
2019 TAILQ_FOREACH(r, &screens[i].rl, entry)
2020 if (e->xvisibility.window == r->bar_window)
2024 void (*handler[LASTEvent])(XEvent *) = {
2026 [KeyPress] = keypress,
2027 [ButtonPress] = buttonpress,
2028 [ConfigureRequest] = configurerequest,
2029 [ConfigureNotify] = configurenotify,
2030 [DestroyNotify] = destroynotify,
2031 [EnterNotify] = enternotify,
2032 [FocusIn] = focusin,
2033 [MappingNotify] = mappingnotify,
2034 [MapRequest] = maprequest,
2035 [PropertyNotify] = propertynotify,
2036 [UnmapNotify] = unmapnotify,
2037 [VisibilityNotify] = visibilitynotify,
2041 xerror_start(Display *d, XErrorEvent *ee)
2048 xerror(Display *d, XErrorEvent *ee)
2050 /* fprintf(stderr, "error: %p %p\n", display, ee); */
2058 xerrorxlib = XSetErrorHandler(xerror_start);
2060 /* this causes an error if some other window manager is running */
2061 XSelectInput(display, DefaultRootWindow(display),
2062 SubstructureRedirectMask);
2063 XSync(display, False);
2067 XSetErrorHandler(xerror);
2068 XSync(display, False);
2077 unsigned char *p = NULL;
2078 unsigned long n, extra;
2081 astate = XInternAtom(display, "WM_STATE", False);
2082 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
2083 &real, &format, &n, &extra, (unsigned char **)&p);
2084 if (status != Success)
2093 new_region(struct swm_screen *s, struct workspace *ws,
2094 int x, int y, int w, int h)
2096 struct swm_region *r;
2098 DNPRINTF(SWM_D_MISC, "new region on screen %d: %dx%d (%d, %d)\n",
2099 s->idx, x, y, w, h);
2101 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
2102 errx(1, "calloc: failed to allocate memory for screen");
2111 TAILQ_INSERT_TAIL(&s->rl, r, entry);
2118 #ifdef SWM_XRR_HAS_CRTC
2120 XRRScreenResources *sr;
2122 #endif /* SWM_XRR_HAS_CRTC */
2123 Window d1, d2, *wins = NULL;
2124 XWindowAttributes wa;
2125 struct swm_region *r;
2127 int errorbase, major, minor;
2128 int ncrtc = 0, w = 0;
2130 struct workspace *ws;
2134 if ((screens = calloc(ScreenCount(display),
2135 sizeof(struct swm_screen))) == NULL)
2136 errx(1, "calloc: screens");
2138 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2141 /* map physical screens */
2142 for (i = 0; i < ScreenCount(display); i++) {
2143 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
2145 TAILQ_INIT(&screens[i].rl);
2146 screens[i].root = RootWindow(display, i);
2148 /* set default colors */
2149 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
2150 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
2151 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
2152 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
2153 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
2155 /* init all workspaces */
2156 for (j = 0; j < SWM_WS_MAX; j++) {
2157 ws = &screens[i].ws[j];
2162 TAILQ_INIT(&ws->winlist);
2164 for (k = 0; layouts[k].l_stack != NULL; k++)
2165 if (layouts[k].l_config != NULL)
2166 layouts[k].l_config(ws,
2167 SWM_ARG_ID_STACKINIT);
2168 ws->cur_layout = &layouts[0];
2171 /* map virtual screens onto physical screens */
2172 screens[i].xrandr_support = XRRQueryExtension(display,
2173 &xrandr_eventbase, &errorbase);
2174 if (screens[i].xrandr_support)
2175 if (XRRQueryVersion(display, &major, &minor) &&
2177 screens[i].xrandr_support = 0;
2179 #if 0 /* not ready for dynamic screen changes */
2180 if (screens[i].xrandr_support)
2181 XRRSelectInput(display,
2183 RRScreenChangeNotifyMask);
2186 /* grab existing windows (before we build the bars)*/
2187 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
2190 #ifdef SWM_XRR_HAS_CRTC
2191 sr = XRRGetScreenResources(display, screens[i].root);
2193 new_region(&screens[i], &screens[i].ws[w],
2194 0, 0, DisplayWidth(display, i),
2195 DisplayHeight(display, i));
2199 for (c = 0, ci = NULL; c < ncrtc; c++) {
2200 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
2201 if (ci->noutput == 0)
2204 if (ci != NULL && ci->mode == None)
2205 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2206 DisplayWidth(display, i),
2207 DisplayHeight(display, i));
2209 new_region(&screens[i], &screens[i].ws[w],
2210 ci->x, ci->y, ci->width, ci->height);
2214 XRRFreeCrtcInfo(ci);
2215 XRRFreeScreenResources(sr);
2217 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2218 DisplayWidth(display, i),
2219 DisplayHeight(display, i));
2220 #endif /* SWM_XRR_HAS_CRTC */
2222 /* attach windows to a region */
2223 /* normal windows */
2224 if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
2225 errx(1, "no regions on screen %d", i);
2227 for (i = 0; i < no; i++) {
2228 XGetWindowAttributes(display, wins[i], &wa);
2229 if (!XGetWindowAttributes(display, wins[i], &wa) ||
2230 wa.override_redirect ||
2231 XGetTransientForHint(display, wins[i], &d1))
2234 if (wa.map_state == IsViewable ||
2235 getstate(wins[i]) == NormalState)
2236 manage_window(wins[i]);
2238 /* transient windows */
2239 for (i = 0; i < no; i++) {
2240 if (!XGetWindowAttributes(display, wins[i], &wa))
2243 if (XGetTransientForHint(display, wins[i], &d1) &&
2244 (wa.map_state == IsViewable || getstate(wins[i]) ==
2246 manage_window(wins[i]);
2256 main(int argc, char *argv[])
2259 char conf[PATH_MAX], *cfile = NULL;
2266 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
2267 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2268 warnx("no locale support");
2270 if (!(display = XOpenDisplay(0)))
2271 errx(1, "can not open display");
2274 errx(1, "other wm running");
2276 astate = XInternAtom(display, "WM_STATE", False);
2278 /* look for local and global conf file */
2279 pwd = getpwuid(getuid());
2281 errx(1, "invalid user %d", getuid());
2285 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
2286 if (stat(conf, &sb) != -1) {
2287 if (S_ISREG(sb.st_mode))
2290 /* try global conf file */
2291 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
2292 if (!stat(conf, &sb))
2293 if (S_ISREG(sb.st_mode))
2300 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2305 xfd = ConnectionNumber(display);
2309 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
2311 errx(1, "select failed");
2316 while(XPending(display)) {
2317 XNextEvent(display, &e);
2318 if (handler[e.type])
2319 handler[e.type](&e);
2323 XCloseDisplay(display);