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 static const char *cvstag = "$scrotwm$";
53 #define SWM_VERSION "0.6"
69 #include <sys/types.h>
73 #include <sys/queue.h>
74 #include <sys/param.h>
75 #include <sys/select.h>
77 #include <X11/cursorfont.h>
78 #include <X11/keysym.h>
79 #include <X11/Xatom.h>
81 #include <X11/Xproto.h>
82 #include <X11/Xutil.h>
83 #include <X11/extensions/Xrandr.h>
86 # error XRandR versions less than 1.0 are not supported
91 #define SWM_XRR_HAS_CRTC
95 /* #define SWM_DEBUG */
97 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
98 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
99 #define SWM_D_MISC 0x0001
100 #define SWM_D_EVENT 0x0002
101 #define SWM_D_WS 0x0004
102 #define SWM_D_FOCUS 0x0008
103 #define SWM_D_MOVE 0x0010
104 #define SWM_D_STACK 0x0020
105 #define SWM_D_MOUSE 0x0040
106 #define SWM_D_PROP 0x0080
107 #define SWM_D_CLASS 0x0100
109 u_int32_t swm_debug = 0
121 #define DPRINTF(x...)
122 #define DNPRINTF(n,x...)
125 #define LENGTH(x) (sizeof x / sizeof x[0])
126 #define MODKEY Mod1Mask
127 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
128 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
129 #define SWM_PROPLEN (16)
130 #define X(r) (r)->g.x
131 #define Y(r) (r)->g.y
132 #define WIDTH(r) (r)->g.w
133 #define HEIGHT(r) (r)->g.h
136 #define SWM_LIB "/usr/X11R6/lib/swmhack.so"
141 int (*xerrorxlib)(Display *, XErrorEvent *);
145 int xrandr_eventbase;
146 int ignore_enter = 0;
147 unsigned int numlockmask = 0;
151 int cycle_visible = 0;
154 double dialog_ratio = .6;
156 #define SWM_BAR_MAX (128)
157 char *bar_argv[] = { NULL, NULL };
159 char bar_ext[SWM_BAR_MAX];
160 sig_atomic_t bar_alarm = 0;
164 int bar_extra_running = 0;
172 char *bar_fonts[] = {
173 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
174 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
178 /* terminal + args */
179 char *spawn_term[] = { "xterm", NULL };
180 char *spawn_screenshot[] = { "screenshot.sh", NULL, NULL };
181 char *spawn_menu[] = { "dmenu_run", "-fn", NULL, "-nb", NULL,
182 "-nf", NULL, "-sb", NULL, "-sf", NULL, NULL };
184 #define SWM_MENU_FN (2)
185 #define SWM_MENU_NB (4)
186 #define SWM_MENU_NF (6)
187 #define SWM_MENU_SB (8)
188 #define SWM_MENU_SF (10)
190 /* layout manager data */
191 struct swm_geometry {
201 /* virtual "screens" */
203 TAILQ_ENTRY(swm_region) entry;
204 struct swm_geometry g;
206 struct workspace *ws; /* current workspace on this region */
207 struct swm_screen *s; /* screen idx */
209 TAILQ_HEAD(swm_region_list, swm_region);
212 TAILQ_ENTRY(ws_win) entry;
214 struct swm_geometry g;
218 struct workspace *ws; /* always valid */
219 struct swm_screen *s; /* always valid, never changes */
220 XWindowAttributes wa;
223 TAILQ_HEAD(ws_win_list, ws_win);
225 /* layout handlers */
227 void vertical_config(struct workspace *, int);
228 void vertical_stack(struct workspace *, struct swm_geometry *);
229 void horizontal_config(struct workspace *, int);
230 void horizontal_stack(struct workspace *, struct swm_geometry *);
231 void max_stack(struct workspace *, struct swm_geometry *);
233 void grabbuttons(struct ws_win *, int);
236 void (*l_stack)(struct workspace *, struct swm_geometry *);
237 void (*l_config)(struct workspace *, int);
239 /* stack, configure */
240 { vertical_stack, vertical_config},
241 { horizontal_stack, horizontal_config},
246 #define SWM_H_SLICE (32)
247 #define SWM_V_SLICE (32)
249 /* define work spaces */
251 int idx; /* workspace index */
252 int restack; /* restack on switch */
253 struct layout *cur_layout; /* current layout handlers */
254 struct ws_win *focus; /* may be NULL */
255 struct swm_region *r; /* may be NULL */
256 struct ws_win_list winlist; /* list of windows in ws */
260 int horizontal_msize;
267 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
268 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
270 /* physical screen mapping */
271 #define SWM_WS_MAX (10) /* XXX Too small? */
273 int idx; /* screen index */
274 struct swm_region_list rl; /* list of regions on this screen */
277 struct workspace ws[SWM_WS_MAX];
283 } c[SWM_S_COLOR_MAX];
285 struct swm_screen *screens;
288 struct ws_win *cur_focus = NULL;
290 /* args to functions */
293 #define SWM_ARG_ID_FOCUSNEXT (0)
294 #define SWM_ARG_ID_FOCUSPREV (1)
295 #define SWM_ARG_ID_FOCUSMAIN (2)
296 #define SWM_ARG_ID_SWAPNEXT (3)
297 #define SWM_ARG_ID_SWAPPREV (4)
298 #define SWM_ARG_ID_SWAPMAIN (5)
299 #define SWM_ARG_ID_MASTERSHRINK (6)
300 #define SWM_ARG_ID_MASTERGROW (7)
301 #define SWM_ARG_ID_MASTERADD (8)
302 #define SWM_ARG_ID_MASTERDEL (9)
303 #define SWM_ARG_ID_STACKRESET (10)
304 #define SWM_ARG_ID_STACKINIT (11)
305 #define SWM_ARG_ID_CYCLEWS_UP (12)
306 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
307 #define SWM_ARG_ID_CYCLESC_UP (14)
308 #define SWM_ARG_ID_CYCLESC_DOWN (15)
309 #define SWM_ARG_ID_SS_ALL (0)
310 #define SWM_ARG_ID_SS_WINDOW (1)
318 #define SWM_Q_FLOAT (1<<0)
320 { "MPlayer", "xv", SWM_Q_FLOAT },
321 { "OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT },
326 name_to_color(char *colorname)
330 XColor screen_def, exact_def;
331 unsigned long result = 0;
332 char cname[32] = "#";
334 cmap = DefaultColormap(display, screens[0].idx);
335 status = XAllocNamedColor(display, cmap, colorname,
336 &screen_def, &exact_def);
338 strlcat(cname, colorname + 2, sizeof cname - 1);
339 status = XAllocNamedColor(display, cmap, cname, &screen_def,
343 result = screen_def.pixel;
345 fprintf(stderr, "color '%s' not found.\n", colorname);
351 setscreencolor(char *val, int i, int c)
353 if (i > 0 && i <= ScreenCount(display)) {
354 screens[i - 1].c[c].color = name_to_color(val);
355 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
357 } else if (i == -1) {
358 for (i = 0; i < ScreenCount(display); i++)
359 screens[i].c[c].color = name_to_color(val);
360 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
363 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
364 i, ScreenCount(display));
368 varmatch(char *var, char *name, int *index)
373 i = strncmp(var, name, 255);
380 p = var + strlen(name);
383 bzero(buf, sizeof buf);
385 while (isdigit(*p) && i < sizeof buf)
387 if (i == 0 || i >= sizeof buf || *p != ']')
389 *index = strtonum(buf, 0, 99, NULL);
393 /* conf file stuff */
394 #define SWM_CONF_WS "\n= \t"
395 #define SWM_CONF_FILE "scrotwm.conf"
397 conf_load(char *filename)
400 char *line, *cp, *var, *val;
401 size_t len, lineno = 0;
404 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
406 if (filename == NULL)
409 if ((config = fopen(filename, "r")) == NULL)
412 for (sc = ScreenCount(display);;) {
413 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
417 cp += (long)strspn(cp, SWM_CONF_WS);
423 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
425 cp += (long)strspn(cp, SWM_CONF_WS);
426 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
429 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
432 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
433 bar_enabled = atoi(val);
434 else if (!varmatch(var, "bar_border", &i))
435 setscreencolor(val, i, SWM_S_COLOR_BAR_BORDER);
436 else if (!varmatch(var, "bar_color", &i))
437 setscreencolor(val, i, SWM_S_COLOR_BAR);
438 else if (!varmatch(var, "bar_font_color", &i))
439 setscreencolor(val, i, SWM_S_COLOR_BAR_FONT);
440 else if (!strncmp(var, "bar_font", strlen("bar_font")))
441 asprintf(&bar_fonts[0], "%s", val);
442 else if (!strncmp(var, "bar_action", strlen("bar_action")))
443 asprintf(&bar_argv[0], "%s", val);
444 else if (!strncmp(var, "bar_delay", strlen("bar_delay")))
445 bar_delay = atoi(val);
451 if (!varmatch(var, "color_focus", &i))
452 setscreencolor(val, i, SWM_S_COLOR_FOCUS);
453 else if (!varmatch(var, "color_unfocus", &i))
454 setscreencolor(val, i, SWM_S_COLOR_UNFOCUS);
455 else if (!strncmp(var, "cycle_empty", strlen("cycle_empty")))
456 cycle_visible = atoi(val);
457 else if (!strncmp(var, "cycle_visible", strlen("cycle_visible")))
458 cycle_visible = atoi(val);
464 if (!strncmp(var, "dialog_ratio",
465 strlen("dialog_ratio"))) {
466 dialog_ratio = atof(val);
467 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
474 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
475 asprintf(&spawn_term[0], "%s", val);
476 if (!strncmp(var, "screenshot_enabled",
477 strlen("screenshot_enabled")))
478 ss_enabled = atoi(val);
479 if (!strncmp(var, "screenshot_app",
480 strlen("screenshot_app")))
481 asprintf(&spawn_screenshot[0], "%s", val);
493 errx(1, "invalid conf file entry: %s=%s", var, val);
497 socket_setnonblock(int fd)
501 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
502 err(1, "fcntl F_GETFL");
504 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
505 err(1, "fcntl F_SETFL");
509 bar_print(struct swm_region *r, char *s)
511 XClearWindow(display, r->bar_window);
512 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
513 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
522 bzero(bar_pipe, sizeof bar_pipe);
525 kill(bar_pid, SIGTERM);
528 strlcpy(bar_ext, "", sizeof bar_ext);
537 struct swm_region *r;
541 char loc[SWM_BAR_MAX];
544 if (bar_enabled == 0)
547 if (bar_extra && bar_extra_running) {
548 /* ignore short reads; it'll correct itself */
549 while ((b = fgetln(stdin, &len)) != NULL)
550 if (b && b[len - 1] == '\n') {
552 strlcpy(bar_ext, b, sizeof bar_ext);
554 if (b == NULL && errno != EAGAIN) {
555 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
556 errno, strerror(errno));
560 strlcpy(bar_ext, "", sizeof bar_ext);
563 localtime_r(&tmt, &tm);
564 strftime(s, sizeof s, "%a %b %d %R %Z %Y", &tm);
565 for (i = 0; i < ScreenCount(display); i++) {
567 TAILQ_FOREACH(r, &screens[i].rl, entry) {
568 snprintf(loc, sizeof loc, "%s %d:%d %s",
569 s, x++, r->ws->idx + 1, bar_ext);
573 XSync(display, False);
584 bar_toggle(struct swm_region *r, union arg *args)
586 struct swm_region *tmpr;
587 int i, j, sc = ScreenCount(display);
589 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
592 for (i = 0; i < sc; i++)
593 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
594 XUnmapWindow(display, tmpr->bar_window);
596 for (i = 0; i < sc; i++)
597 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
598 XMapRaised(display, tmpr->bar_window);
600 bar_enabled = !bar_enabled;
601 XSync(display, False);
602 for (i = 0; i < sc; i++)
603 for (j = 0; j < SWM_WS_MAX; j++)
604 screens[i].ws[j].restack = 1;
607 /* must be after stack */
608 for (i = 0; i < sc; i++)
609 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
616 XSetWindowAttributes wa;
617 struct swm_region *r;
620 /* do this here because the conf file is in memory */
621 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
622 /* launch external status app */
623 bar_extra_running = 1;
624 if (pipe(bar_pipe) == -1)
625 err(1, "pipe error");
626 socket_setnonblock(bar_pipe[0]);
627 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
628 if (dup2(bar_pipe[0], 0) == -1)
630 if (dup2(bar_pipe[1], 1) == -1)
632 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
633 err(1, "could not disable SIGPIPE");
634 switch (bar_pid = fork()) {
636 err(1, "cannot fork");
640 execvp(bar_argv[0], bar_argv);
641 err(1, "%s external app failed", bar_argv[0]);
643 default: /* parent */
649 bzero(&wa, sizeof wa);
650 for (i = 0; i < ScreenCount(display); i++)
651 TAILQ_FOREACH(r, &screens[i].rl, entry) {
653 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
654 wa.background_pixel =
655 screens[i].c[SWM_S_COLOR_BAR].color;
656 XChangeWindowAttributes(display, r->bar_window,
657 CWBackPixel | CWBorderPixel, &wa);
663 bar_setup(struct swm_region *r)
667 for (i = 0; bar_fonts[i] != NULL; i++) {
668 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
674 if (bar_fonts[i] == NULL)
675 errx(1, "couldn't load font");
676 bar_height = bar_fs->ascent + bar_fs->descent + 3;
678 r->bar_window = XCreateSimpleWindow(display,
679 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
680 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
681 r->s->c[SWM_S_COLOR_BAR].color);
682 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
683 XSetFont(display, bar_gc, bar_fs->fid);
684 XSelectInput(display, r->bar_window, VisibilityChangeMask);
686 XMapRaised(display, r->bar_window);
687 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
689 if (signal(SIGALRM, bar_signal) == SIG_ERR)
690 err(1, "could not install bar_signal");
695 config_win(struct ws_win *win)
699 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
700 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
701 ce.type = ConfigureNotify;
702 ce.display = display;
708 ce.height = win->g.h;
709 ce.border_width = 1; /* XXX store this! */
711 ce.override_redirect = False;
712 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
716 count_win(struct workspace *ws, int count_transient)
721 TAILQ_FOREACH(win, &ws->winlist, entry) {
722 if (count_transient == 0 && win->floating)
724 if (count_transient == 0 && win->transient)
728 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
734 quit(struct swm_region *r, union arg *args)
736 DNPRINTF(SWM_D_MISC, "quit\n");
746 for (i = 0; i < ScreenCount(display); i++)
747 for (j = 0; j < SWM_WS_MAX; j++)
748 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
749 XUnmapWindow(display, win->id);
753 restart(struct swm_region *r, union arg *args)
755 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
757 /* disable alarm because the following code may not be interrupted */
759 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
760 errx(1, "can't disable alarm");
765 XCloseDisplay(display);
766 execvp(start_argv[0], start_argv);
767 fprintf(stderr, "execvp failed\n");
773 root_to_region(Window root)
775 struct swm_region *r;
780 for (i = 0; i < ScreenCount(display); i++)
781 if (screens[i].root == root)
784 if (XQueryPointer(display, screens[i].root,
785 &rr, &cr, &x, &y, &wx, &wy, &mask) == False) {
786 /* if we can't query the pointer, grab the first region */
787 r = TAILQ_FIRST(&screens[i].rl);
789 /* otherwise, choose a region based on pointer location */
790 TAILQ_FOREACH(r, &screens[i].rl, entry) {
791 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
792 y >= Y(r) && y <= Y(r) + HEIGHT(r))
797 r = TAILQ_FIRST(&screens[i].rl);
803 find_window(Window id)
808 for (i = 0; i < ScreenCount(display); i++)
809 for (j = 0; j < SWM_WS_MAX; j++)
810 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
817 spawn(struct swm_region *r, union arg *args)
822 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
824 * The double-fork construct avoids zombie processes and keeps the code
825 * clean from stupid signal handlers.
830 close(ConnectionNumber(display));
831 setenv("LD_PRELOAD", SWM_LIB, 1);
832 if (asprintf(&ret, "%d", r->ws->idx)) {
833 setenv("_SWM_WS", ret, 1);
836 if (asprintf(&ret, "%d", getpid())) {
837 setenv("_SWM_PID", ret, 1);
841 /* kill stdin, mplayer, ssh-add etc. need that */
842 si = open("/dev/null", O_RDONLY, 0);
844 err(1, "open /dev/null");
845 if (dup2(si, 0) == -1)
846 err(1, "dup2 /dev/null");
847 execvp(args->argv[0], args->argv);
848 fprintf(stderr, "execvp failed\n");
857 spawnmenu(struct swm_region *r, union arg *args)
859 DNPRINTF(SWM_D_MISC, "spawnmenu\n");
861 spawn_menu[SWM_MENU_FN] = bar_fonts[bar_fidx];
862 spawn_menu[SWM_MENU_NB] = r->s->c[SWM_S_COLOR_BAR].name;
863 spawn_menu[SWM_MENU_NF] = r->s->c[SWM_S_COLOR_BAR_FONT].name;
864 spawn_menu[SWM_MENU_SB] = r->s->c[SWM_S_COLOR_BAR_BORDER].name;
865 spawn_menu[SWM_MENU_SF] = r->s->c[SWM_S_COLOR_BAR].name;
876 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
878 for (i = 0; i < ScreenCount(display); i++)
879 for (j = 0; j < SWM_WS_MAX; j++)
880 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
881 if (win->ws->r == NULL)
884 XSetWindowBorder(display, win->id,
885 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
887 win->ws->focus = NULL;
893 focus_win(struct ws_win *win)
895 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
901 win->ws->focus = win;
902 if (win->ws->r != NULL) {
904 if (!win->got_focus) {
905 XSetWindowBorder(display, win->id,
906 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
910 XSetInputFocus(display, win->id,
911 RevertToPointerRoot, CurrentTime);
916 switchws(struct swm_region *r, union arg *args)
919 struct swm_region *this_r, *other_r;
921 struct workspace *new_ws, *old_ws;
925 new_ws = &this_r->s->ws[wsid];
927 DNPRINTF(SWM_D_WS, "switchws screen %d region %dx%d+%d+%d: "
928 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
931 if (new_ws == old_ws)
936 /* if the other workspace is hidden, switch windows */
937 /* map new window first to prevent ugly blinking */
938 TAILQ_FOREACH(win, &new_ws->winlist, entry)
939 XMapRaised(display, win->id);
941 TAILQ_FOREACH(win, &old_ws->winlist, entry)
942 XUnmapWindow(display, win->id);
946 other_r->ws = old_ws;
955 focus_win(new_ws->focus);
961 cyclews(struct swm_region *r, union arg *args)
964 struct swm_screen *s = r->s;
966 DNPRINTF(SWM_D_WS, "cyclews id %d "
967 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
968 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
973 case SWM_ARG_ID_CYCLEWS_UP:
974 if (a.id < SWM_WS_MAX - 1)
979 case SWM_ARG_ID_CYCLEWS_DOWN:
983 a.id = SWM_WS_MAX - 1;
989 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
991 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
995 } while (a.id != r->ws->idx);
999 cyclescr(struct swm_region *r, union arg *args)
1001 struct swm_region *rr;
1006 case SWM_ARG_ID_CYCLESC_UP:
1007 rr = TAILQ_NEXT(r, entry);
1009 rr = TAILQ_FIRST(&screens[i].rl);
1011 case SWM_ARG_ID_CYCLESC_DOWN:
1012 rr = TAILQ_PREV(r, swm_region_list, entry);
1014 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1020 XSetInputFocus(display, PointerRoot, RevertToPointerRoot, CurrentTime);
1021 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, rr->g.x,
1022 rr->g.y + bar_enabled ? bar_height : 0);
1026 swapwin(struct swm_region *r, union arg *args)
1028 struct ws_win *target;
1029 struct ws_win_list *wl;
1032 DNPRINTF(SWM_D_WS, "swapwin id %d "
1033 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1034 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1035 if (cur_focus == NULL)
1038 wl = &cur_focus->ws->winlist;
1041 case SWM_ARG_ID_SWAPPREV:
1042 target = TAILQ_PREV(cur_focus, ws_win_list, entry);
1043 TAILQ_REMOVE(wl, cur_focus, entry);
1045 TAILQ_INSERT_TAIL(wl, cur_focus, entry);
1047 TAILQ_INSERT_BEFORE(target, cur_focus, entry);
1049 case SWM_ARG_ID_SWAPNEXT:
1050 target = TAILQ_NEXT(cur_focus, entry);
1051 TAILQ_REMOVE(wl, cur_focus, entry);
1053 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
1055 TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
1057 case SWM_ARG_ID_SWAPMAIN:
1058 target = TAILQ_FIRST(wl);
1059 if (target == cur_focus)
1061 TAILQ_REMOVE(wl, target, entry);
1062 TAILQ_INSERT_BEFORE(cur_focus, target, entry);
1063 TAILQ_REMOVE(wl, cur_focus, entry);
1064 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
1067 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1076 focus(struct swm_region *r, union arg *args)
1078 struct ws_win *winfocus, *winlostfocus;
1079 struct ws_win_list *wl;
1081 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1082 if (cur_focus == NULL)
1085 wl = &cur_focus->ws->winlist;
1087 winlostfocus = cur_focus;
1090 case SWM_ARG_ID_FOCUSPREV:
1091 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1092 if (winfocus == NULL)
1093 winfocus = TAILQ_LAST(wl, ws_win_list);
1096 case SWM_ARG_ID_FOCUSNEXT:
1097 winfocus = TAILQ_NEXT(cur_focus, entry);
1098 if (winfocus == NULL)
1099 winfocus = TAILQ_FIRST(wl);
1102 case SWM_ARG_ID_FOCUSMAIN:
1103 winfocus = TAILQ_FIRST(wl);
1110 if (winfocus == winlostfocus || winfocus == NULL)
1113 XMapRaised(display, winfocus->id);
1114 focus_win(winfocus);
1115 XSync(display, False);
1119 cycle_layout(struct swm_region *r, union arg *args)
1121 struct workspace *ws = r->ws;
1123 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1126 if (ws->cur_layout->l_stack == NULL)
1127 ws->cur_layout = &layouts[0];
1133 stack_config(struct swm_region *r, union arg *args)
1135 struct workspace *ws = r->ws;
1137 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1140 if (ws->cur_layout->l_config != NULL)
1141 ws->cur_layout->l_config(ws, args->id);
1143 if (args->id != SWM_ARG_ID_STACKINIT);
1149 struct swm_geometry g;
1150 struct swm_region *r;
1153 DNPRINTF(SWM_D_STACK, "stack\n");
1155 for (i = 0; i < ScreenCount(display); i++) {
1157 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1158 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1159 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1161 /* start with screen geometry, adjust for bar */
1171 r->ws->cur_layout->l_stack(r->ws, &g);
1174 XSync(display, False);
1178 stack_floater(struct ws_win *win, struct swm_region *r)
1183 bzero(&wc, sizeof wc);
1184 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1185 wc.border_width = 1;
1186 if (win->transient) {
1187 win->g.w = (double)WIDTH(r) * dialog_ratio;
1188 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1190 wc.width = win->g.w;
1191 wc.height = win->g.h;
1192 wc.x = (WIDTH(r) - win->g.w) / 2;
1193 wc.y = (HEIGHT(r) - win->g.h) / 2;
1195 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1196 win->id, wc.x, wc.y, wc.width, wc.height);
1198 XConfigureWindow(display, win->id, mask, &wc);
1201 #define SWAPXY(g) do { \
1203 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1204 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1207 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1210 struct swm_geometry win_g, r_g = *g;
1211 struct ws_win *win, *winfocus;
1212 int i, j, w_inc, h_inc, w_base, h_base;
1213 int hrh, extra, h_slice, last_h = 0;
1214 int split, colno, winno, mwin, msize, mscale;
1215 int remain, missing, v_slice;;
1218 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1219 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1221 if ((winno = count_win(ws, 0)) == 0)
1224 if (ws->focus == NULL)
1225 ws->focus = TAILQ_FIRST(&ws->winlist);
1226 winfocus = cur_focus ? cur_focus : ws->focus;
1228 win = TAILQ_FIRST(&ws->winlist);
1230 w_inc = win->sh.width_inc;
1231 w_base = win->sh.base_width;
1232 mwin = ws->l_state.horizontal_mwin;
1233 mscale = ws->l_state.horizontal_msize;
1236 w_inc = win->sh.height_inc;
1237 w_base = win->sh.base_height;
1238 mwin = ws->l_state.vertical_mwin;
1239 mscale = ws->l_state.vertical_msize;
1243 h_slice = r_g.h / SWM_H_SLICE;
1244 if (mwin && winno > mwin) {
1245 v_slice = r_g.w / SWM_V_SLICE;
1249 msize = v_slice * mscale;
1251 if (w_inc > 1 && w_inc < v_slice) {
1252 /* adjust for window's requested size increment */
1253 remain = (win_g.w - w_base) % w_inc;
1254 missing = w_inc - remain;
1256 if (missing <= extra || j == 0) {
1267 win_g.x += r_g.w - msize;
1272 hrh = r_g.h / colno;
1273 extra = r_g.h - (colno * hrh);
1277 TAILQ_FOREACH(win, &ws->winlist, entry) {
1278 if (split && i == split) {
1279 colno = winno - split;
1280 hrh = (r_g.h / colno);
1281 extra = r_g.h - (colno * hrh);
1285 win_g.x += msize + 2;
1286 win_g.w = r_g.w - (msize + 2);
1291 h_inc = win->sh.width_inc;
1292 h_base = win->sh.base_width;
1294 h_inc = win->sh.height_inc;
1295 h_base = win->sh.base_height;
1297 if (j == colno - 1) {
1298 win_g.h = hrh + extra;
1299 } else if (h_inc > 1 && h_inc < h_slice) {
1300 /* adjust for window's requested size increment */
1301 remain = (win_g.h - h_base) % h_inc;
1302 missing = h_inc - remain;
1304 if (missing <= extra || j == 0) {
1316 win_g.y += last_h + 2;
1318 if (win->transient != 0 || win->floating != 0)
1319 stack_floater(win, ws->r);
1321 bzero(&wc, sizeof wc);
1322 wc.border_width = 1;
1324 win->g.x = wc.x = win_g.y;
1325 win->g.y = wc.y = win_g.x;
1326 win->g.w = wc.width = win_g.h;
1327 win->g.h = wc.height = win_g.w;
1329 win->g.x = wc.x = win_g.x;
1330 win->g.y = wc.y = win_g.y;
1331 win->g.w = wc.width = win_g.w;
1332 win->g.h = wc.height = win_g.h;
1334 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1335 XConfigureWindow(display, win->id, mask, &wc);
1337 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);
1341 XMapRaised(display, win->id);
1348 focus_win(winfocus); /* has to be done outside of the loop */
1352 vertical_config(struct workspace *ws, int id)
1354 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1357 case SWM_ARG_ID_STACKRESET:
1358 case SWM_ARG_ID_STACKINIT:
1359 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1360 ws->l_state.vertical_mwin = 1;
1362 case SWM_ARG_ID_MASTERSHRINK:
1363 if (ws->l_state.vertical_msize > 1)
1364 ws->l_state.vertical_msize--;
1366 case SWM_ARG_ID_MASTERGROW:
1367 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1368 ws->l_state.vertical_msize++;
1370 case SWM_ARG_ID_MASTERADD:
1371 ws->l_state.vertical_mwin++;
1373 case SWM_ARG_ID_MASTERDEL:
1374 if (ws->l_state.vertical_mwin > 0)
1375 ws->l_state.vertical_mwin--;
1383 vertical_stack(struct workspace *ws, struct swm_geometry *g)
1385 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1387 stack_master(ws, g, 0, 0);
1391 horizontal_config(struct workspace *ws, int id)
1393 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1396 case SWM_ARG_ID_STACKRESET:
1397 case SWM_ARG_ID_STACKINIT:
1398 ws->l_state.horizontal_mwin = 1;
1399 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1401 case SWM_ARG_ID_MASTERSHRINK:
1402 if (ws->l_state.horizontal_msize > 1)
1403 ws->l_state.horizontal_msize--;
1405 case SWM_ARG_ID_MASTERGROW:
1406 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1407 ws->l_state.horizontal_msize++;
1409 case SWM_ARG_ID_MASTERADD:
1410 ws->l_state.horizontal_mwin++;
1412 case SWM_ARG_ID_MASTERDEL:
1413 if (ws->l_state.horizontal_mwin > 0)
1414 ws->l_state.horizontal_mwin--;
1422 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
1424 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1426 stack_master(ws, g, 1, 0);
1429 /* fullscreen view */
1431 max_stack(struct workspace *ws, struct swm_geometry *g) {
1433 struct swm_geometry gg = *g;
1434 struct ws_win *win, *winfocus;
1437 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1439 if (count_win(ws, 0) == 0)
1442 if (ws->focus == NULL)
1443 ws->focus = TAILQ_FIRST(&ws->winlist);
1444 winfocus = cur_focus ? cur_focus : ws->focus;
1446 TAILQ_FOREACH(win, &ws->winlist, entry) {
1447 if (win->transient != 0 || win->floating != 0) {
1448 if (win == ws->focus) {
1450 stack_floater(win, ws->r);
1451 XMapRaised(display, win->id);
1453 XUnmapWindow(display, win->id);
1455 bzero(&wc, sizeof wc);
1456 wc.border_width = 1;
1457 win->g.x = wc.x = gg.x;
1458 win->g.y = wc.y = gg.y;
1459 win->g.w = wc.width = gg.w;
1460 win->g.h = wc.height = gg.h;
1461 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1462 XConfigureWindow(display, win->id, mask, &wc);
1464 if (win == ws->focus) {
1465 XMapRaised(display, win->id);
1467 XUnmapWindow(display, win->id);
1472 focus_win(winfocus); /* has to be done outside of the loop */
1476 send_to_ws(struct swm_region *r, union arg *args)
1478 int wsid = args->id;
1479 struct ws_win *win = cur_focus;
1480 struct workspace *ws, *nws;
1481 Atom ws_idx_atom = 0;
1482 unsigned char ws_idx_str[SWM_PROPLEN];
1484 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1487 nws = &win->s->ws[wsid];
1489 XUnmapWindow(display, win->id);
1491 /* find a window to focus */
1492 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1493 if (ws->focus == NULL)
1494 ws->focus = TAILQ_FIRST(&ws->winlist);
1495 if (ws->focus == win)
1498 TAILQ_REMOVE(&ws->winlist, win, entry);
1500 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1503 /* Try to update the window's workspace property */
1504 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1506 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
1507 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1509 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1510 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1513 if (count_win(nws, 1) == 1)
1522 wkill(struct swm_region *r, union arg *args)
1524 DNPRINTF(SWM_D_MISC, "wkill\n");
1525 if(r->ws->focus != NULL)
1526 XKillClient(display, r->ws->focus->id);
1530 screenshot(struct swm_region *r, union arg *args)
1534 DNPRINTF(SWM_D_MISC, "screenshot\n");
1536 if (ss_enabled == 0)
1540 case SWM_ARG_ID_SS_ALL:
1541 spawn_screenshot[1] = "full";
1543 case SWM_ARG_ID_SS_WINDOW:
1544 spawn_screenshot[1] = "window";
1549 a.argv = spawn_screenshot;
1553 /* key definitions */
1557 void (*func)(struct swm_region *r, union arg *);
1560 /* modifier key function argument */
1561 { MODKEY, XK_space, cycle_layout, {0} },
1562 { MODKEY | ShiftMask, XK_space, stack_config, {.id = SWM_ARG_ID_STACKRESET} },
1563 { MODKEY, XK_h, stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
1564 { MODKEY, XK_l, stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
1565 { MODKEY, XK_comma, stack_config, {.id = SWM_ARG_ID_MASTERADD} },
1566 { MODKEY, XK_period, stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
1567 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1568 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1569 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1570 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1571 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1572 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1573 { MODKEY, XK_p, spawnmenu, {.argv = spawn_menu} },
1574 { MODKEY | ShiftMask, XK_q, quit, {0} },
1575 { MODKEY, XK_q, restart, {0} },
1576 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1577 { MODKEY, XK_1, switchws, {.id = 0} },
1578 { MODKEY, XK_2, switchws, {.id = 1} },
1579 { MODKEY, XK_3, switchws, {.id = 2} },
1580 { MODKEY, XK_4, switchws, {.id = 3} },
1581 { MODKEY, XK_5, switchws, {.id = 4} },
1582 { MODKEY, XK_6, switchws, {.id = 5} },
1583 { MODKEY, XK_7, switchws, {.id = 6} },
1584 { MODKEY, XK_8, switchws, {.id = 7} },
1585 { MODKEY, XK_9, switchws, {.id = 8} },
1586 { MODKEY, XK_0, switchws, {.id = 9} },
1587 { MODKEY, XK_Right, cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
1588 { MODKEY, XK_Left, cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
1589 { MODKEY | ShiftMask, XK_Right, cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
1590 { MODKEY | ShiftMask, XK_Left, cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
1591 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1592 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1593 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1594 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1595 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1596 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1597 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1598 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1599 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1600 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1601 { MODKEY, XK_b, bar_toggle, {0} },
1602 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1603 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1604 { MODKEY | ShiftMask, XK_x, wkill, {0} },
1605 { MODKEY, XK_s, screenshot, {.id = SWM_ARG_ID_SS_ALL} },
1606 { MODKEY | ShiftMask, XK_s, screenshot, {.id = SWM_ARG_ID_SS_WINDOW} },
1610 click(struct swm_region *r, union arg *args)
1612 DNPRINTF(SWM_D_MOUSE, "click: button: %d\n", args->id);
1627 enum { client_click, root_click };
1629 unsigned int action;
1631 unsigned int button;
1632 void (*func)(struct swm_region *r, union arg *);
1635 /* action key mouse button func args */
1636 { client_click, MODKEY, Button1, click, {.id=Button1} },
1640 updatenumlockmask(void)
1643 XModifierKeymap *modmap;
1645 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1647 modmap = XGetModifierMapping(display);
1648 for (i = 0; i < 8; i++)
1649 for (j = 0; j < modmap->max_keypermod; j++)
1650 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1651 == XKeysymToKeycode(display, XK_Num_Lock))
1652 numlockmask = (1 << i);
1654 XFreeModifiermap(modmap);
1660 unsigned int i, j, k;
1662 unsigned int modifiers[] =
1663 { 0, LockMask, numlockmask, numlockmask | LockMask };
1665 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1666 updatenumlockmask();
1668 for (k = 0; k < ScreenCount(display); k++) {
1669 if (TAILQ_EMPTY(&screens[k].rl))
1671 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
1672 for (i = 0; i < LENGTH(keys); i++) {
1673 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
1674 for (j = 0; j < LENGTH(modifiers); j++)
1675 XGrabKey(display, code,
1676 keys[i].mod | modifiers[j],
1677 screens[k].root, True,
1678 GrabModeAsync, GrabModeAsync);
1684 grabbuttons(struct ws_win *win, int focused)
1687 unsigned int modifiers[] =
1688 { 0, LockMask, numlockmask, numlockmask|LockMask };
1690 updatenumlockmask();
1691 XUngrabButton(display, AnyButton, AnyModifier, win->id);
1693 for (i = 0; i < LENGTH(buttons); i++)
1694 if (buttons[i].action == client_click)
1695 for (j = 0; j < LENGTH(modifiers); j++)
1696 XGrabButton(display, buttons[i].button,
1697 buttons[i].mask | modifiers[j],
1698 win->id, False, BUTTONMASK,
1699 GrabModeAsync, GrabModeSync, None,
1702 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
1703 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1709 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1717 XKeyEvent *ev = &e->xkey;
1719 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1721 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1722 for (i = 0; i < LENGTH(keys); i++)
1723 if (keysym == keys[i].keysym
1724 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1726 keys[i].func(root_to_region(ev->root),
1731 buttonpress(XEvent *e)
1733 XButtonPressedEvent *ev = &e->xbutton;
1738 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1740 action = root_click;
1741 if ((win = find_window(ev->window)) == NULL)
1745 action = client_click;
1748 for (i = 0; i < LENGTH(buttons); i++)
1749 if (action == buttons[i].action && buttons[i].func &&
1750 buttons[i].button == ev->button &&
1751 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
1752 buttons[i].func(root_to_region(ev->root),
1757 set_win_state(struct ws_win *win, long state)
1759 long data[] = {state, None};
1761 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1763 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1764 (unsigned char *)data, 2);
1768 manage_window(Window id)
1771 struct workspace *ws;
1774 int format, i, ws_idx;
1775 unsigned long nitems, bytes;
1776 Atom ws_idx_atom = 0, type;
1777 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
1778 struct swm_region *r;
1782 if ((win = find_window(id)) != NULL)
1783 return (win); /* already being managed */
1785 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1786 errx(1, "calloc: failed to allocate memory for new window");
1788 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1790 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
1791 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
1793 XGetWindowAttributes(display, id, &win->wa);
1794 r = root_to_region(win->wa.root);
1795 /* If the window was managed before, put it in the same workspace */
1797 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
1798 ws_idx = strtonum(prop, 0, 9, &errstr);
1800 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
1802 ws = &r->s->ws[ws_idx];
1808 win->s = r->s; /* this never changes */
1809 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
1811 XGetTransientForHint(display, win->id, &trans);
1813 win->transient = trans;
1814 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1815 (unsigned)win->id, win->transient);
1817 win->g.w = win->wa.width;
1818 win->g.h = win->wa.height;
1819 win->g.x = win->wa.x;
1820 win->g.y = win->wa.y;
1822 XGetWMNormalHints(display, win->id, &win->sh, &mask); /* XXX function? */
1824 if (ws_idx_atom && prop == NULL &&
1825 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
1826 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1828 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1829 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1834 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);
1837 bzero(&ch, sizeof ch);
1838 if (XGetClassHint(display, win->id, &ch)) {
1839 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
1840 ch.res_class, ch.res_name);
1841 for (i = 0; quirks[i].class != NULL && quirks[i].name != NULL &&
1842 quirks[i].quirk != 0; i++){
1843 if (!strcmp(ch.res_class, quirks[i].class) &&
1844 !strcmp(ch.res_name, quirks[i].name)) {
1845 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
1846 ch.res_class, ch.res_name);
1847 if (quirks[i].quirk & SWM_Q_FLOAT)
1851 /* XXX logic says to do this but get a double free */
1853 XFree(ch.res_class);
1860 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
1861 PropertyChangeMask | StructureNotifyMask);
1863 set_win_state(win, NormalState);
1865 /* make new win focused */
1872 configurerequest(XEvent *e)
1874 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1879 if ((win = find_window(ev->window)) == NULL)
1883 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
1886 fprintf(stderr, "configurerequest: new window: %lu x %d y %d w %d h %d bw %d s %d sm %d\n",
1887 ev->window, ev->x, ev->y, ev->width, ev->height, ev->border_width, ev->above, ev->detail);
1889 bzero(&wc, sizeof wc);
1892 wc.width = ev->width;
1893 wc.height = ev->height;
1894 wc.border_width = ev->border_width;
1895 wc.sibling = ev->above;
1896 wc.stack_mode = ev->detail;
1897 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
1900 fprintf(stderr, "configurerequest: change window: %lu\n",
1903 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
1905 if (win->floating) {
1906 if (ev->value_mask & CWX)
1908 if (ev->value_mask & CWY)
1910 if (ev->value_mask & CWWidth)
1911 win->g.w = ev->width;
1912 if (ev->value_mask & CWHeight)
1913 win->g.h = ev->height;
1914 if (win->ws->r != NULL) {
1915 /* this seems to be full screen */
1916 if (win->g.w >= WIDTH(win->ws->r)) {
1918 win->g.w = WIDTH(win->ws->r);
1919 ev->value_mask |= CWX | CWWidth;
1921 if (win->g.h >= HEIGHT(win->ws->r)) {
1924 win->g.h = HEIGHT(win->ws->r);
1925 ev->value_mask |= CWY | CWHeight;
1928 if ((ev->value_mask & (CWX|CWY)) &&
1929 !(ev->value_mask & (CWWidth|CWHeight)))
1931 XMoveResizeWindow(display, win->id,
1932 win->g.x, win->g.y, win->g.w, win->g.h);
1939 configurenotify(XEvent *e)
1941 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1942 e->xconfigure.window);
1946 destroynotify(XEvent *e)
1949 XDestroyWindowEvent *ev = &e->xdestroywindow;
1950 struct workspace *ws;
1952 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1954 if ((win = find_window(ev->window)) != NULL) {
1956 /* find a window to focus */
1957 if (ws->focus == win)
1958 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1959 if (ws->focus == NULL)
1960 ws->focus = TAILQ_FIRST(&ws->winlist);
1961 if (ws->focus == NULL || ws->focus == win) {
1965 focus_win(ws->focus);
1966 TAILQ_REMOVE(&ws->winlist, win, entry);
1967 set_win_state(win, WithdrawnState);
1974 enternotify(XEvent *e)
1976 XCrossingEvent *ev = &e->xcrossing;
1979 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1982 /* eat event(r) to prevent autofocus */
1987 if ((win = find_window(ev->window)) != NULL)
1994 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
1998 mappingnotify(XEvent *e)
2000 XMappingEvent *ev = &e->xmapping;
2002 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
2004 XRefreshKeyboardMapping(ev);
2005 if (ev->request == MappingKeyboard)
2010 maprequest(XEvent *e)
2012 XMapRequestEvent *ev = &e->xmaprequest;
2013 XWindowAttributes wa;
2015 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
2016 e->xmaprequest.window);
2018 if (!XGetWindowAttributes(display, ev->window, &wa))
2020 if (wa.override_redirect)
2022 manage_window(e->xmaprequest.window);
2027 propertynotify(XEvent *e)
2030 XPropertyEvent *ev = &e->xproperty;
2032 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
2035 if (ev->state == PropertyDelete)
2036 return; /* ignore */
2037 win = find_window(ev->window);
2042 case XA_WM_NORMAL_HINTS:
2045 XGetWMNormalHints(display, win->id, &win->sh, &mask);
2046 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
2047 if (win->sh.flags & PMinSize) {
2048 win->g.w = win->sh.min_width;
2049 win->g.h = win->sh.min_height;
2050 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
2052 XMoveResizeWindow(display, win->id,
2053 win->g.x, win->g.y, win->g.w, win->g.h);
2062 unmapnotify(XEvent *e)
2064 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
2068 visibilitynotify(XEvent *e)
2071 struct swm_region *r;
2073 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
2074 e->xvisibility.window);
2075 if (e->xvisibility.state == VisibilityUnobscured)
2076 for (i = 0; i < ScreenCount(display); i++)
2077 TAILQ_FOREACH(r, &screens[i].rl, entry)
2078 if (e->xvisibility.window == r->bar_window)
2082 void (*handler[LASTEvent])(XEvent *) = {
2084 [KeyPress] = keypress,
2085 [ButtonPress] = buttonpress,
2086 [ConfigureRequest] = configurerequest,
2087 [ConfigureNotify] = configurenotify,
2088 [DestroyNotify] = destroynotify,
2089 [EnterNotify] = enternotify,
2090 [FocusIn] = focusin,
2091 [MappingNotify] = mappingnotify,
2092 [MapRequest] = maprequest,
2093 [PropertyNotify] = propertynotify,
2094 [UnmapNotify] = unmapnotify,
2095 [VisibilityNotify] = visibilitynotify,
2099 xerror_start(Display *d, XErrorEvent *ee)
2106 xerror(Display *d, XErrorEvent *ee)
2108 /* fprintf(stderr, "error: %p %p\n", display, ee); */
2116 xerrorxlib = XSetErrorHandler(xerror_start);
2118 /* this causes an error if some other window manager is running */
2119 XSelectInput(display, DefaultRootWindow(display),
2120 SubstructureRedirectMask);
2121 XSync(display, False);
2125 XSetErrorHandler(xerror);
2126 XSync(display, False);
2135 unsigned char *p = NULL;
2136 unsigned long n, extra;
2139 astate = XInternAtom(display, "WM_STATE", False);
2140 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
2141 &real, &format, &n, &extra, (unsigned char **)&p);
2142 if (status != Success)
2151 new_region(struct swm_screen *s, struct workspace *ws,
2152 int x, int y, int w, int h)
2154 struct swm_region *r;
2156 DNPRINTF(SWM_D_MISC, "new region on screen %d: %dx%d (%d, %d)\n",
2157 s->idx, x, y, w, h);
2159 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
2160 errx(1, "calloc: failed to allocate memory for screen");
2169 TAILQ_INSERT_TAIL(&s->rl, r, entry);
2176 #ifdef SWM_XRR_HAS_CRTC
2178 XRRScreenResources *sr;
2180 #endif /* SWM_XRR_HAS_CRTC */
2181 Window d1, d2, *wins = NULL;
2182 XWindowAttributes wa;
2183 struct swm_region *r;
2185 int errorbase, major, minor;
2186 int ncrtc = 0, w = 0;
2188 struct workspace *ws;
2192 if ((screens = calloc(ScreenCount(display),
2193 sizeof(struct swm_screen))) == NULL)
2194 errx(1, "calloc: screens");
2196 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2199 /* map physical screens */
2200 for (i = 0; i < ScreenCount(display); i++) {
2201 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
2203 TAILQ_INIT(&screens[i].rl);
2204 screens[i].root = RootWindow(display, i);
2206 /* set default colors */
2207 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
2208 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
2209 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
2210 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
2211 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
2213 /* init all workspaces */
2214 for (j = 0; j < SWM_WS_MAX; j++) {
2215 ws = &screens[i].ws[j];
2220 TAILQ_INIT(&ws->winlist);
2222 for (k = 0; layouts[k].l_stack != NULL; k++)
2223 if (layouts[k].l_config != NULL)
2224 layouts[k].l_config(ws,
2225 SWM_ARG_ID_STACKINIT);
2226 ws->cur_layout = &layouts[0];
2229 /* map virtual screens onto physical screens */
2230 screens[i].xrandr_support = XRRQueryExtension(display,
2231 &xrandr_eventbase, &errorbase);
2232 if (screens[i].xrandr_support)
2233 if (XRRQueryVersion(display, &major, &minor) &&
2235 screens[i].xrandr_support = 0;
2237 #if 0 /* not ready for dynamic screen changes */
2238 if (screens[i].xrandr_support)
2239 XRRSelectInput(display,
2241 RRScreenChangeNotifyMask);
2244 /* grab existing windows (before we build the bars)*/
2245 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
2248 #ifdef SWM_XRR_HAS_CRTC
2249 sr = XRRGetScreenResources(display, screens[i].root);
2251 new_region(&screens[i], &screens[i].ws[w],
2252 0, 0, DisplayWidth(display, i),
2253 DisplayHeight(display, i));
2257 for (c = 0, ci = NULL; c < ncrtc; c++) {
2258 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
2259 if (ci->noutput == 0)
2262 if (ci != NULL && ci->mode == None)
2263 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2264 DisplayWidth(display, i),
2265 DisplayHeight(display, i));
2267 new_region(&screens[i], &screens[i].ws[w],
2268 ci->x, ci->y, ci->width, ci->height);
2272 XRRFreeCrtcInfo(ci);
2273 XRRFreeScreenResources(sr);
2275 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2276 DisplayWidth(display, i),
2277 DisplayHeight(display, i));
2278 #endif /* SWM_XRR_HAS_CRTC */
2280 /* attach windows to a region */
2281 /* normal windows */
2282 if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
2283 errx(1, "no regions on screen %d", i);
2285 for (i = 0; i < no; i++) {
2286 XGetWindowAttributes(display, wins[i], &wa);
2287 if (!XGetWindowAttributes(display, wins[i], &wa) ||
2288 wa.override_redirect ||
2289 XGetTransientForHint(display, wins[i], &d1))
2292 if (wa.map_state == IsViewable ||
2293 getstate(wins[i]) == NormalState)
2294 manage_window(wins[i]);
2296 /* transient windows */
2297 for (i = 0; i < no; i++) {
2298 if (!XGetWindowAttributes(display, wins[i], &wa))
2301 if (XGetTransientForHint(display, wins[i], &d1) &&
2302 (wa.map_state == IsViewable || getstate(wins[i]) ==
2304 manage_window(wins[i]);
2314 main(int argc, char *argv[])
2317 char conf[PATH_MAX], *cfile = NULL;
2324 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
2325 SWM_VERSION, cvstag);
2326 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2327 warnx("no locale support");
2329 if (!(display = XOpenDisplay(0)))
2330 errx(1, "can not open display");
2333 errx(1, "other wm running");
2335 astate = XInternAtom(display, "WM_STATE", False);
2337 /* look for local and global conf file */
2338 pwd = getpwuid(getuid());
2340 errx(1, "invalid user %d", getuid());
2344 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
2345 if (stat(conf, &sb) != -1) {
2346 if (S_ISREG(sb.st_mode))
2349 /* try global conf file */
2350 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
2351 if (!stat(conf, &sb))
2352 if (S_ISREG(sb.st_mode))
2359 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2364 xfd = ConnectionNumber(display);
2368 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
2370 errx(1, "select failed");
2375 while(XPending(display)) {
2376 XNextEvent(display, &e);
2377 if (handler[e.type])
2378 handler[e.type](&e);
2382 XCloseDisplay(display);