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 MOUSEMASK (BUTTONMASK|PointerMotionMask)
130 #define SWM_PROPLEN (16)
131 #define X(r) (r)->g.x
132 #define Y(r) (r)->g.y
133 #define WIDTH(r) (r)->g.w
134 #define HEIGHT(r) (r)->g.h
137 #define SWM_LIB "/usr/X11R6/lib/swmhack.so"
142 int (*xerrorxlib)(Display *, XErrorEvent *);
146 int xrandr_eventbase;
147 int ignore_enter = 0;
148 unsigned int numlockmask = 0;
152 int cycle_visible = 0;
155 double dialog_ratio = .6;
157 #define SWM_BAR_MAX (128)
158 char *bar_argv[] = { NULL, NULL };
160 char bar_ext[SWM_BAR_MAX];
161 char bar_vertext[SWM_BAR_MAX];
163 sig_atomic_t bar_alarm = 0;
167 int bar_extra_running = 0;
175 char *bar_fonts[] = {
176 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
177 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
181 /* terminal + args */
182 char *spawn_term[] = { "xterm", NULL };
183 char *spawn_screenshot[] = { "screenshot.sh", NULL, NULL };
184 char *spawn_menu[] = { "dmenu_run", "-fn", NULL, "-nb", NULL,
185 "-nf", NULL, "-sb", NULL, "-sf", NULL, NULL };
187 #define SWM_MENU_FN (2)
188 #define SWM_MENU_NB (4)
189 #define SWM_MENU_NF (6)
190 #define SWM_MENU_SB (8)
191 #define SWM_MENU_SF (10)
193 /* layout manager data */
194 struct swm_geometry {
204 /* virtual "screens" */
206 TAILQ_ENTRY(swm_region) entry;
207 struct swm_geometry g;
209 struct workspace *ws; /* current workspace on this region */
210 struct swm_screen *s; /* screen idx */
212 TAILQ_HEAD(swm_region_list, swm_region);
215 TAILQ_ENTRY(ws_win) entry;
217 struct swm_geometry g;
222 struct workspace *ws; /* always valid */
223 struct swm_screen *s; /* always valid, never changes */
224 XWindowAttributes wa;
228 TAILQ_HEAD(ws_win_list, ws_win);
230 /* layout handlers */
232 void vertical_config(struct workspace *, int);
233 void vertical_stack(struct workspace *, struct swm_geometry *);
234 void horizontal_config(struct workspace *, int);
235 void horizontal_stack(struct workspace *, struct swm_geometry *);
236 void max_stack(struct workspace *, struct swm_geometry *);
238 void grabbuttons(struct ws_win *, int);
241 void (*l_stack)(struct workspace *, struct swm_geometry *);
242 void (*l_config)(struct workspace *, int);
244 /* stack, configure */
245 { vertical_stack, vertical_config},
246 { horizontal_stack, horizontal_config},
251 #define SWM_H_SLICE (32)
252 #define SWM_V_SLICE (32)
254 /* define work spaces */
256 int idx; /* workspace index */
257 int restack; /* restack on switch */
258 struct layout *cur_layout; /* current layout handlers */
259 struct ws_win *focus; /* may be NULL */
260 struct swm_region *r; /* may be NULL */
261 struct ws_win_list winlist; /* list of windows in ws */
265 int horizontal_msize;
272 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
273 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
275 /* physical screen mapping */
276 #define SWM_WS_MAX (10) /* XXX Too small? */
278 int idx; /* screen index */
279 struct swm_region_list rl; /* list of regions on this screen */
282 struct workspace ws[SWM_WS_MAX];
288 } c[SWM_S_COLOR_MAX];
290 struct swm_screen *screens;
293 struct ws_win *cur_focus = NULL;
295 /* args to functions */
298 #define SWM_ARG_ID_FOCUSNEXT (0)
299 #define SWM_ARG_ID_FOCUSPREV (1)
300 #define SWM_ARG_ID_FOCUSMAIN (2)
301 #define SWM_ARG_ID_SWAPNEXT (3)
302 #define SWM_ARG_ID_SWAPPREV (4)
303 #define SWM_ARG_ID_SWAPMAIN (5)
304 #define SWM_ARG_ID_MASTERSHRINK (6)
305 #define SWM_ARG_ID_MASTERGROW (7)
306 #define SWM_ARG_ID_MASTERADD (8)
307 #define SWM_ARG_ID_MASTERDEL (9)
308 #define SWM_ARG_ID_STACKRESET (10)
309 #define SWM_ARG_ID_STACKINIT (11)
310 #define SWM_ARG_ID_CYCLEWS_UP (12)
311 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
312 #define SWM_ARG_ID_CYCLESC_UP (14)
313 #define SWM_ARG_ID_CYCLESC_DOWN (15)
314 #define SWM_ARG_ID_SS_ALL (0)
315 #define SWM_ARG_ID_SS_WINDOW (1)
316 #define SWM_ARG_ID_DONTCENTER (0)
317 #define SWM_ARG_ID_CENTER (1)
326 #define SWM_Q_FLOAT (1<<0)
328 { "MPlayer", "xv", SWM_Q_FLOAT },
329 { "OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT },
334 void expose(XEvent *);
335 void keypress(XEvent *);
336 void buttonpress(XEvent *);
337 void configurerequest(XEvent *);
338 void configurenotify(XEvent *);
339 void destroynotify(XEvent *);
340 void enternotify(XEvent *);
341 void focusin(XEvent *);
342 void mappingnotify(XEvent *);
343 void maprequest(XEvent *);
344 void propertynotify(XEvent *);
345 void unmapnotify(XEvent *);
346 void visibilitynotify(XEvent *);
348 void (*handler[LASTEvent])(XEvent *) = {
350 [KeyPress] = keypress,
351 [ButtonPress] = buttonpress,
352 [ConfigureRequest] = configurerequest,
353 [ConfigureNotify] = configurenotify,
354 [DestroyNotify] = destroynotify,
355 [EnterNotify] = enternotify,
357 [MappingNotify] = mappingnotify,
358 [MapRequest] = maprequest,
359 [PropertyNotify] = propertynotify,
360 [UnmapNotify] = unmapnotify,
361 [VisibilityNotify] = visibilitynotify,
365 name_to_color(char *colorname)
369 XColor screen_def, exact_def;
370 unsigned long result = 0;
371 char cname[32] = "#";
373 cmap = DefaultColormap(display, screens[0].idx);
374 status = XAllocNamedColor(display, cmap, colorname,
375 &screen_def, &exact_def);
377 strlcat(cname, colorname + 2, sizeof cname - 1);
378 status = XAllocNamedColor(display, cmap, cname, &screen_def,
382 result = screen_def.pixel;
384 fprintf(stderr, "color '%s' not found.\n", colorname);
390 setscreencolor(char *val, int i, int c)
392 if (i > 0 && i <= ScreenCount(display)) {
393 screens[i - 1].c[c].color = name_to_color(val);
394 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
396 } else if (i == -1) {
397 for (i = 0; i < ScreenCount(display); i++)
398 screens[i].c[c].color = name_to_color(val);
399 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
402 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
403 i, ScreenCount(display));
407 varmatch(char *var, char *name, int *index)
412 i = strncmp(var, name, 255);
419 p = var + strlen(name);
422 bzero(buf, sizeof buf);
424 while (isdigit(*p) && i < sizeof buf)
426 if (i == 0 || i >= sizeof buf || *p != ']')
428 *index = strtonum(buf, 0, 99, NULL);
432 /* conf file stuff */
433 #define SWM_CONF_WS "\n= \t"
434 #define SWM_CONF_FILE "scrotwm.conf"
436 conf_load(char *filename)
439 char *line, *cp, *var, *val;
440 size_t len, lineno = 0;
443 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
445 if (filename == NULL)
448 if ((config = fopen(filename, "r")) == NULL)
451 for (sc = ScreenCount(display);;) {
452 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
456 cp += (long)strspn(cp, SWM_CONF_WS);
462 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
464 cp += (long)strspn(cp, SWM_CONF_WS);
465 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
468 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
471 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
472 bar_enabled = atoi(val);
473 else if (!varmatch(var, "bar_border", &i))
474 setscreencolor(val, i, SWM_S_COLOR_BAR_BORDER);
475 else if (!varmatch(var, "bar_color", &i))
476 setscreencolor(val, i, SWM_S_COLOR_BAR);
477 else if (!varmatch(var, "bar_font_color", &i))
478 setscreencolor(val, i, SWM_S_COLOR_BAR_FONT);
479 else if (!strncmp(var, "bar_font", strlen("bar_font")))
480 asprintf(&bar_fonts[0], "%s", val);
481 else if (!strncmp(var, "bar_action", strlen("bar_action")))
482 asprintf(&bar_argv[0], "%s", val);
483 else if (!strncmp(var, "bar_delay", strlen("bar_delay")))
484 bar_delay = atoi(val);
490 if (!varmatch(var, "color_focus", &i))
491 setscreencolor(val, i, SWM_S_COLOR_FOCUS);
492 else if (!varmatch(var, "color_unfocus", &i))
493 setscreencolor(val, i, SWM_S_COLOR_UNFOCUS);
494 else if (!strncmp(var, "cycle_empty", strlen("cycle_empty")))
495 cycle_visible = atoi(val);
496 else if (!strncmp(var, "cycle_visible", strlen("cycle_visible")))
497 cycle_visible = atoi(val);
503 if (!strncmp(var, "dialog_ratio",
504 strlen("dialog_ratio"))) {
505 dialog_ratio = atof(val);
506 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
513 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
514 asprintf(&spawn_term[0], "%s", val);
515 if (!strncmp(var, "screenshot_enabled",
516 strlen("screenshot_enabled")))
517 ss_enabled = atoi(val);
518 if (!strncmp(var, "screenshot_app",
519 strlen("screenshot_app")))
520 asprintf(&spawn_screenshot[0], "%s", val);
532 errx(1, "invalid conf file entry: %s=%s", var, val);
536 socket_setnonblock(int fd)
540 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
541 err(1, "fcntl F_GETFL");
543 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
544 err(1, "fcntl F_SETFL");
548 bar_print(struct swm_region *r, char *s)
550 XClearWindow(display, r->bar_window);
551 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
552 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
561 bzero(bar_pipe, sizeof bar_pipe);
564 kill(bar_pid, SIGTERM);
567 strlcpy(bar_ext, "", sizeof bar_ext);
576 struct swm_region *r;
580 char loc[SWM_BAR_MAX];
583 if (bar_enabled == 0)
586 if (bar_extra && bar_extra_running) {
587 /* ignore short reads; it'll correct itself */
588 while ((b = fgetln(stdin, &len)) != NULL)
589 if (b && b[len - 1] == '\n') {
591 strlcpy(bar_ext, b, sizeof bar_ext);
593 if (b == NULL && errno != EAGAIN) {
594 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
595 errno, strerror(errno));
599 strlcpy(bar_ext, "", sizeof bar_ext);
602 localtime_r(&tmt, &tm);
603 strftime(s, sizeof s, "%a %b %d %R %Z %Y", &tm);
604 for (i = 0; i < ScreenCount(display); i++) {
606 TAILQ_FOREACH(r, &screens[i].rl, entry) {
607 snprintf(loc, sizeof loc, "%s %d:%d %s %s",
608 s, x++, r->ws->idx + 1, bar_ext, bar_vertext);
612 XSync(display, False);
623 bar_toggle(struct swm_region *r, union arg *args)
625 struct swm_region *tmpr;
626 int i, j, sc = ScreenCount(display);
628 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
631 for (i = 0; i < sc; i++)
632 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
633 XUnmapWindow(display, tmpr->bar_window);
635 for (i = 0; i < sc; i++)
636 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
637 XMapRaised(display, tmpr->bar_window);
639 bar_enabled = !bar_enabled;
640 XSync(display, False);
641 for (i = 0; i < sc; i++)
642 for (j = 0; j < SWM_WS_MAX; j++)
643 screens[i].ws[j].restack = 1;
646 /* must be after stack */
647 for (i = 0; i < sc; i++)
648 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
655 XSetWindowAttributes wa;
656 struct swm_region *r;
659 /* do this here because the conf file is in memory */
660 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
661 /* launch external status app */
662 bar_extra_running = 1;
663 if (pipe(bar_pipe) == -1)
664 err(1, "pipe error");
665 socket_setnonblock(bar_pipe[0]);
666 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
667 if (dup2(bar_pipe[0], 0) == -1)
669 if (dup2(bar_pipe[1], 1) == -1)
671 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
672 err(1, "could not disable SIGPIPE");
673 switch (bar_pid = fork()) {
675 err(1, "cannot fork");
679 execvp(bar_argv[0], bar_argv);
680 err(1, "%s external app failed", bar_argv[0]);
682 default: /* parent */
688 bzero(&wa, sizeof wa);
689 for (i = 0; i < ScreenCount(display); i++)
690 TAILQ_FOREACH(r, &screens[i].rl, entry) {
692 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
693 wa.background_pixel =
694 screens[i].c[SWM_S_COLOR_BAR].color;
695 XChangeWindowAttributes(display, r->bar_window,
696 CWBackPixel | CWBorderPixel, &wa);
702 bar_setup(struct swm_region *r)
706 for (i = 0; bar_fonts[i] != NULL; i++) {
707 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
713 if (bar_fonts[i] == NULL)
714 errx(1, "couldn't load font");
715 bar_height = bar_fs->ascent + bar_fs->descent + 3;
717 r->bar_window = XCreateSimpleWindow(display,
718 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
719 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
720 r->s->c[SWM_S_COLOR_BAR].color);
721 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
722 XSetFont(display, bar_gc, bar_fs->fid);
723 XSelectInput(display, r->bar_window, VisibilityChangeMask);
725 XMapRaised(display, r->bar_window);
726 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
728 if (signal(SIGALRM, bar_signal) == SIG_ERR)
729 err(1, "could not install bar_signal");
734 version(struct swm_region *r, union arg *args)
736 bar_version = !bar_version;
738 strlcpy(bar_vertext, cvstag, sizeof bar_vertext);
740 strlcpy(bar_vertext, "", sizeof bar_vertext);
745 config_win(struct ws_win *win)
749 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
750 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
751 ce.type = ConfigureNotify;
752 ce.display = display;
758 ce.height = win->g.h;
759 ce.border_width = 1; /* XXX store this! */
761 ce.override_redirect = False;
762 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
766 count_win(struct workspace *ws, int count_transient)
771 TAILQ_FOREACH(win, &ws->winlist, entry) {
772 if (count_transient == 0 && win->floating)
774 if (count_transient == 0 && win->transient)
778 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
784 quit(struct swm_region *r, union arg *args)
786 DNPRINTF(SWM_D_MISC, "quit\n");
796 for (i = 0; i < ScreenCount(display); i++)
797 for (j = 0; j < SWM_WS_MAX; j++)
798 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
799 XUnmapWindow(display, win->id);
803 restart(struct swm_region *r, union arg *args)
805 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
807 /* disable alarm because the following code may not be interrupted */
809 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
810 errx(1, "can't disable alarm");
815 XCloseDisplay(display);
816 execvp(start_argv[0], start_argv);
817 fprintf(stderr, "execvp failed\n");
823 root_to_region(Window root)
825 struct swm_region *r;
830 for (i = 0; i < ScreenCount(display); i++)
831 if (screens[i].root == root)
834 if (XQueryPointer(display, screens[i].root,
835 &rr, &cr, &x, &y, &wx, &wy, &mask) == False) {
836 /* if we can't query the pointer, grab the first region */
837 r = TAILQ_FIRST(&screens[i].rl);
839 /* otherwise, choose a region based on pointer location */
840 TAILQ_FOREACH(r, &screens[i].rl, entry) {
841 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
842 y >= Y(r) && y <= Y(r) + HEIGHT(r))
847 r = TAILQ_FIRST(&screens[i].rl);
853 find_window(Window id)
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)
867 spawn(struct swm_region *r, union arg *args)
872 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
874 * The double-fork construct avoids zombie processes and keeps the code
875 * clean from stupid signal handlers.
880 close(ConnectionNumber(display));
881 setenv("LD_PRELOAD", SWM_LIB, 1);
882 if (asprintf(&ret, "%d", r->ws->idx)) {
883 setenv("_SWM_WS", ret, 1);
886 if (asprintf(&ret, "%d", getpid())) {
887 setenv("_SWM_PID", ret, 1);
891 /* kill stdin, mplayer, ssh-add etc. need that */
892 si = open("/dev/null", O_RDONLY, 0);
894 err(1, "open /dev/null");
895 if (dup2(si, 0) == -1)
896 err(1, "dup2 /dev/null");
897 execvp(args->argv[0], args->argv);
898 fprintf(stderr, "execvp failed\n");
907 spawnmenu(struct swm_region *r, union arg *args)
909 DNPRINTF(SWM_D_MISC, "spawnmenu\n");
911 spawn_menu[SWM_MENU_FN] = bar_fonts[bar_fidx];
912 spawn_menu[SWM_MENU_NB] = r->s->c[SWM_S_COLOR_BAR].name;
913 spawn_menu[SWM_MENU_NF] = r->s->c[SWM_S_COLOR_BAR_FONT].name;
914 spawn_menu[SWM_MENU_SB] = r->s->c[SWM_S_COLOR_BAR_BORDER].name;
915 spawn_menu[SWM_MENU_SF] = r->s->c[SWM_S_COLOR_BAR].name;
926 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
928 for (i = 0; i < ScreenCount(display); i++)
929 for (j = 0; j < SWM_WS_MAX; j++)
930 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
931 if (win->ws->r == NULL)
934 XSetWindowBorder(display, win->id,
935 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
937 win->ws->focus = NULL;
943 focus_win(struct ws_win *win)
945 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
951 win->ws->focus = win;
952 if (win->ws->r != NULL) {
954 if (!win->got_focus) {
955 XSetWindowBorder(display, win->id,
956 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
960 XSetInputFocus(display, win->id,
961 RevertToPointerRoot, CurrentTime);
966 switchws(struct swm_region *r, union arg *args)
969 struct swm_region *this_r, *other_r;
971 struct workspace *new_ws, *old_ws;
975 new_ws = &this_r->s->ws[wsid];
977 DNPRINTF(SWM_D_WS, "switchws screen %d region %dx%d+%d+%d: "
978 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
981 if (new_ws == old_ws)
986 /* if the other workspace is hidden, switch windows */
987 /* map new window first to prevent ugly blinking */
988 TAILQ_FOREACH(win, &new_ws->winlist, entry)
989 XMapRaised(display, win->id);
991 TAILQ_FOREACH(win, &old_ws->winlist, entry)
992 XUnmapWindow(display, win->id);
996 other_r->ws = old_ws;
1005 focus_win(new_ws->focus);
1011 cyclews(struct swm_region *r, union arg *args)
1014 struct swm_screen *s = r->s;
1016 DNPRINTF(SWM_D_WS, "cyclews id %d "
1017 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1018 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1023 case SWM_ARG_ID_CYCLEWS_UP:
1024 if (a.id < SWM_WS_MAX - 1)
1029 case SWM_ARG_ID_CYCLEWS_DOWN:
1033 a.id = SWM_WS_MAX - 1;
1039 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1041 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1045 } while (a.id != r->ws->idx);
1049 cyclescr(struct swm_region *r, union arg *args)
1051 struct swm_region *rr;
1056 case SWM_ARG_ID_CYCLESC_UP:
1057 rr = TAILQ_NEXT(r, entry);
1059 rr = TAILQ_FIRST(&screens[i].rl);
1061 case SWM_ARG_ID_CYCLESC_DOWN:
1062 rr = TAILQ_PREV(r, swm_region_list, entry);
1064 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1070 XSetInputFocus(display, PointerRoot, RevertToPointerRoot, CurrentTime);
1071 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, rr->g.x,
1072 rr->g.y + bar_enabled ? bar_height : 0);
1076 swapwin(struct swm_region *r, union arg *args)
1078 struct ws_win *target;
1079 struct ws_win_list *wl;
1082 DNPRINTF(SWM_D_WS, "swapwin id %d "
1083 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1084 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1085 if (cur_focus == NULL)
1088 wl = &cur_focus->ws->winlist;
1091 case SWM_ARG_ID_SWAPPREV:
1092 target = TAILQ_PREV(cur_focus, ws_win_list, entry);
1093 TAILQ_REMOVE(wl, cur_focus, entry);
1095 TAILQ_INSERT_TAIL(wl, cur_focus, entry);
1097 TAILQ_INSERT_BEFORE(target, cur_focus, entry);
1099 case SWM_ARG_ID_SWAPNEXT:
1100 target = TAILQ_NEXT(cur_focus, entry);
1101 TAILQ_REMOVE(wl, cur_focus, entry);
1103 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
1105 TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
1107 case SWM_ARG_ID_SWAPMAIN:
1108 target = TAILQ_FIRST(wl);
1109 if (target == cur_focus)
1111 TAILQ_REMOVE(wl, target, entry);
1112 TAILQ_INSERT_BEFORE(cur_focus, target, entry);
1113 TAILQ_REMOVE(wl, cur_focus, entry);
1114 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
1117 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1126 focus(struct swm_region *r, union arg *args)
1128 struct ws_win *winfocus, *winlostfocus;
1129 struct ws_win_list *wl;
1131 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1132 if (cur_focus == NULL)
1135 wl = &cur_focus->ws->winlist;
1137 winlostfocus = cur_focus;
1140 case SWM_ARG_ID_FOCUSPREV:
1141 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1142 if (winfocus == NULL)
1143 winfocus = TAILQ_LAST(wl, ws_win_list);
1146 case SWM_ARG_ID_FOCUSNEXT:
1147 winfocus = TAILQ_NEXT(cur_focus, entry);
1148 if (winfocus == NULL)
1149 winfocus = TAILQ_FIRST(wl);
1152 case SWM_ARG_ID_FOCUSMAIN:
1153 winfocus = TAILQ_FIRST(wl);
1160 if (winfocus == winlostfocus || winfocus == NULL)
1163 XMapRaised(display, winfocus->id);
1164 focus_win(winfocus);
1165 XSync(display, False);
1169 cycle_layout(struct swm_region *r, union arg *args)
1171 struct workspace *ws = r->ws;
1173 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1176 if (ws->cur_layout->l_stack == NULL)
1177 ws->cur_layout = &layouts[0];
1183 stack_config(struct swm_region *r, union arg *args)
1185 struct workspace *ws = r->ws;
1187 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1190 if (ws->cur_layout->l_config != NULL)
1191 ws->cur_layout->l_config(ws, args->id);
1193 if (args->id != SWM_ARG_ID_STACKINIT);
1199 struct swm_geometry g;
1200 struct swm_region *r;
1203 DNPRINTF(SWM_D_STACK, "stack\n");
1205 for (i = 0; i < ScreenCount(display); i++) {
1207 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1208 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1209 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1211 /* start with screen geometry, adjust for bar */
1221 r->ws->cur_layout->l_stack(r->ws, &g);
1224 XSync(display, False);
1228 stack_floater(struct ws_win *win, struct swm_region *r)
1233 bzero(&wc, sizeof wc);
1234 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1235 wc.border_width = 1;
1236 if (win->transient) {
1237 win->g.w = (double)WIDTH(r) * dialog_ratio;
1238 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1240 wc.width = win->g.w;
1241 wc.height = win->g.h;
1246 wc.x = (WIDTH(r) - win->g.w) / 2;
1247 wc.y = (HEIGHT(r) - win->g.h) / 2;
1250 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1251 win->id, wc.x, wc.y, wc.width, wc.height);
1253 XConfigureWindow(display, win->id, mask, &wc);
1256 #define SWAPXY(g) do { \
1258 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1259 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1262 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1265 struct swm_geometry win_g, r_g = *g;
1266 struct ws_win *win, *winfocus;
1267 int i, j, w_inc, h_inc, w_base, h_base;
1268 int hrh, extra, h_slice, last_h = 0;
1269 int split, colno, winno, mwin, msize, mscale;
1270 int remain, missing, v_slice;;
1273 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1274 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1276 if ((winno = count_win(ws, 0)) == 0)
1279 if (ws->focus == NULL)
1280 ws->focus = TAILQ_FIRST(&ws->winlist);
1281 winfocus = cur_focus ? cur_focus : ws->focus;
1283 win = TAILQ_FIRST(&ws->winlist);
1285 w_inc = win->sh.width_inc;
1286 w_base = win->sh.base_width;
1287 mwin = ws->l_state.horizontal_mwin;
1288 mscale = ws->l_state.horizontal_msize;
1291 w_inc = win->sh.height_inc;
1292 w_base = win->sh.base_height;
1293 mwin = ws->l_state.vertical_mwin;
1294 mscale = ws->l_state.vertical_msize;
1298 h_slice = r_g.h / SWM_H_SLICE;
1299 if (mwin && winno > mwin) {
1300 v_slice = r_g.w / SWM_V_SLICE;
1304 msize = v_slice * mscale;
1306 if (w_inc > 1 && w_inc < v_slice) {
1307 /* adjust for window's requested size increment */
1308 remain = (win_g.w - w_base) % w_inc;
1309 missing = w_inc - remain;
1311 if (missing <= extra || j == 0) {
1322 win_g.x += r_g.w - msize;
1327 hrh = r_g.h / colno;
1328 extra = r_g.h - (colno * hrh);
1331 /* stack all the tiled windows */
1333 TAILQ_FOREACH(win, &ws->winlist, entry) {
1334 if (win->transient != 0 || win->floating != 0)
1337 if (split && i == split) {
1338 colno = winno - split;
1339 hrh = (r_g.h / colno);
1340 extra = r_g.h - (colno * hrh);
1344 win_g.x += msize + 2;
1345 win_g.w = r_g.w - (msize + 2);
1350 h_inc = win->sh.width_inc;
1351 h_base = win->sh.base_width;
1353 h_inc = win->sh.height_inc;
1354 h_base = win->sh.base_height;
1356 if (j == colno - 1) {
1357 win_g.h = hrh + extra;
1358 } else if (h_inc > 1 && h_inc < h_slice) {
1359 /* adjust for window's requested size increment */
1360 remain = (win_g.h - h_base) % h_inc;
1361 missing = h_inc - remain;
1363 if (missing <= extra || j == 0) {
1375 win_g.y += last_h + 2;
1377 bzero(&wc, sizeof wc);
1378 wc.border_width = 1;
1380 win->g.x = wc.x = win_g.y;
1381 win->g.y = wc.y = win_g.x;
1382 win->g.w = wc.width = win_g.h;
1383 win->g.h = wc.height = win_g.w;
1385 win->g.x = wc.x = win_g.x;
1386 win->g.y = wc.y = win_g.y;
1387 win->g.w = wc.width = win_g.w;
1388 win->g.h = wc.height = win_g.h;
1390 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1391 XConfigureWindow(display, win->id, mask, &wc);
1392 XMapRaised(display, win->id);
1394 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);
1402 /* now, stack all the floaters and transients */
1403 TAILQ_FOREACH(win, &ws->winlist, entry) {
1404 if (win->transient == 0 && win->floating == 0)
1407 stack_floater(win, ws->r);
1408 XMapRaised(display, win->id);
1412 focus_win(winfocus); /* has to be done outside of the loop */
1416 vertical_config(struct workspace *ws, int id)
1418 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1421 case SWM_ARG_ID_STACKRESET:
1422 case SWM_ARG_ID_STACKINIT:
1423 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1424 ws->l_state.vertical_mwin = 1;
1426 case SWM_ARG_ID_MASTERSHRINK:
1427 if (ws->l_state.vertical_msize > 1)
1428 ws->l_state.vertical_msize--;
1430 case SWM_ARG_ID_MASTERGROW:
1431 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1432 ws->l_state.vertical_msize++;
1434 case SWM_ARG_ID_MASTERADD:
1435 ws->l_state.vertical_mwin++;
1437 case SWM_ARG_ID_MASTERDEL:
1438 if (ws->l_state.vertical_mwin > 0)
1439 ws->l_state.vertical_mwin--;
1447 vertical_stack(struct workspace *ws, struct swm_geometry *g)
1449 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1451 stack_master(ws, g, 0, 0);
1455 horizontal_config(struct workspace *ws, int id)
1457 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1460 case SWM_ARG_ID_STACKRESET:
1461 case SWM_ARG_ID_STACKINIT:
1462 ws->l_state.horizontal_mwin = 1;
1463 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1465 case SWM_ARG_ID_MASTERSHRINK:
1466 if (ws->l_state.horizontal_msize > 1)
1467 ws->l_state.horizontal_msize--;
1469 case SWM_ARG_ID_MASTERGROW:
1470 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1471 ws->l_state.horizontal_msize++;
1473 case SWM_ARG_ID_MASTERADD:
1474 ws->l_state.horizontal_mwin++;
1476 case SWM_ARG_ID_MASTERDEL:
1477 if (ws->l_state.horizontal_mwin > 0)
1478 ws->l_state.horizontal_mwin--;
1486 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
1488 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1490 stack_master(ws, g, 1, 0);
1493 /* fullscreen view */
1495 max_stack(struct workspace *ws, struct swm_geometry *g) {
1497 struct swm_geometry gg = *g;
1498 struct ws_win *win, *winfocus;
1501 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1503 if (count_win(ws, 0) == 0)
1506 if (ws->focus == NULL)
1507 ws->focus = TAILQ_FIRST(&ws->winlist);
1508 winfocus = cur_focus ? cur_focus : ws->focus;
1510 TAILQ_FOREACH(win, &ws->winlist, entry) {
1511 if (win->transient != 0 || win->floating != 0) {
1512 if (win == ws->focus) {
1514 stack_floater(win, ws->r);
1515 XMapRaised(display, win->id);
1517 XUnmapWindow(display, win->id);
1519 bzero(&wc, sizeof wc);
1520 wc.border_width = 1;
1521 win->g.x = wc.x = gg.x;
1522 win->g.y = wc.y = gg.y;
1523 win->g.w = wc.width = gg.w;
1524 win->g.h = wc.height = gg.h;
1525 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1526 XConfigureWindow(display, win->id, mask, &wc);
1528 if (win == ws->focus) {
1529 XMapRaised(display, win->id);
1531 XUnmapWindow(display, win->id);
1536 focus_win(winfocus); /* has to be done outside of the loop */
1540 send_to_ws(struct swm_region *r, union arg *args)
1542 int wsid = args->id;
1543 struct ws_win *win = cur_focus;
1544 struct workspace *ws, *nws;
1545 Atom ws_idx_atom = 0;
1546 unsigned char ws_idx_str[SWM_PROPLEN];
1551 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1554 nws = &win->s->ws[wsid];
1556 XUnmapWindow(display, win->id);
1558 /* find a window to focus */
1559 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1560 if (ws->focus == NULL)
1561 ws->focus = TAILQ_FIRST(&ws->winlist);
1562 if (ws->focus == win)
1565 TAILQ_REMOVE(&ws->winlist, win, entry);
1567 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1570 /* Try to update the window's workspace property */
1571 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1573 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
1574 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1576 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1577 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1580 if (count_win(nws, 1) == 1)
1589 wkill(struct swm_region *r, union arg *args)
1591 DNPRINTF(SWM_D_MISC, "wkill\n");
1592 if(r->ws->focus != NULL)
1593 XKillClient(display, r->ws->focus->id);
1597 screenshot(struct swm_region *r, union arg *args)
1601 DNPRINTF(SWM_D_MISC, "screenshot\n");
1603 if (ss_enabled == 0)
1607 case SWM_ARG_ID_SS_ALL:
1608 spawn_screenshot[1] = "full";
1610 case SWM_ARG_ID_SS_WINDOW:
1611 spawn_screenshot[1] = "window";
1616 a.argv = spawn_screenshot;
1621 floating_toggle(struct swm_region *r, union arg *args)
1623 struct ws_win *win = cur_focus;
1628 win->floating = !win->floating;
1634 /* key definitions */
1638 void (*func)(struct swm_region *r, union arg *);
1641 /* modifier key function argument */
1642 { MODKEY, XK_space, cycle_layout, {0} },
1643 { MODKEY | ShiftMask, XK_space, stack_config, {.id = SWM_ARG_ID_STACKRESET} },
1644 { MODKEY, XK_h, stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
1645 { MODKEY, XK_l, stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
1646 { MODKEY, XK_comma, stack_config, {.id = SWM_ARG_ID_MASTERADD} },
1647 { MODKEY, XK_period, stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
1648 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1649 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1650 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1651 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1652 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1653 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1654 { MODKEY, XK_p, spawnmenu, {.argv = spawn_menu} },
1655 { MODKEY | ShiftMask, XK_q, quit, {0} },
1656 { MODKEY, XK_q, restart, {0} },
1657 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1658 { MODKEY, XK_1, switchws, {.id = 0} },
1659 { MODKEY, XK_2, switchws, {.id = 1} },
1660 { MODKEY, XK_3, switchws, {.id = 2} },
1661 { MODKEY, XK_4, switchws, {.id = 3} },
1662 { MODKEY, XK_5, switchws, {.id = 4} },
1663 { MODKEY, XK_6, switchws, {.id = 5} },
1664 { MODKEY, XK_7, switchws, {.id = 6} },
1665 { MODKEY, XK_8, switchws, {.id = 7} },
1666 { MODKEY, XK_9, switchws, {.id = 8} },
1667 { MODKEY, XK_0, switchws, {.id = 9} },
1668 { MODKEY, XK_Right, cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
1669 { MODKEY, XK_Left, cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
1670 { MODKEY | ShiftMask, XK_Right, cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
1671 { MODKEY | ShiftMask, XK_Left, cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
1672 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1673 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1674 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1675 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1676 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1677 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1678 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1679 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1680 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1681 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1682 { MODKEY, XK_b, bar_toggle, {0} },
1683 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1684 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1685 { MODKEY | ShiftMask, XK_x, wkill, {0} },
1686 { MODKEY, XK_s, screenshot, {.id = SWM_ARG_ID_SS_ALL} },
1687 { MODKEY | ShiftMask, XK_s, screenshot, {.id = SWM_ARG_ID_SS_WINDOW} },
1688 { MODKEY, XK_t, floating_toggle,{0} },
1689 { MODKEY | ShiftMask, XK_v, version, {0} },
1693 resize_window(struct ws_win *win, int center)
1697 struct swm_region *r;
1699 r = root_to_region(win->wa.root);
1700 bzero(&wc, sizeof wc);
1701 mask = CWBorderWidth | CWWidth | CWHeight;
1702 wc.border_width = 1;
1703 wc.width = win->g.w;
1704 wc.height = win->g.h;
1705 if (center == SWM_ARG_ID_CENTER) {
1706 wc.x = (WIDTH(r) - win->g.w) / 2;
1707 wc.y = (HEIGHT(r) - win->g.h) / 2;
1711 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
1712 win->id, wc.x, wc.y, wc.width, wc.height);
1714 XConfigureWindow(display, win->id, mask, &wc);
1719 resize(struct ws_win *win, union arg *args)
1724 DNPRINTF(SWM_D_MOUSE, "resize: win %d floating %d trans %d\n",
1725 win->id, win->floating, win->transient);
1727 if (!(win->transient != 0 || win->floating != 0))
1730 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
1731 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
1733 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w, win->g.h);
1735 XMaskEvent(display, MOUSEMASK | ExposureMask |
1736 SubstructureRedirectMask, &ev);
1738 case ConfigureRequest:
1741 handler[ev.type](&ev);
1744 if (ev.xmotion.x < 0)
1746 if (ev.xmotion.y < 0)
1748 win->g.w = ev.xmotion.x;
1749 win->g.h = ev.xmotion.y;
1751 /* not free, don't sync more than 60 times / second */
1752 if ((ev.xmotion.time - time) > (1000 / 60) ) {
1753 time = ev.xmotion.time;
1754 XSync(display, False);
1755 resize_window(win, args->id);
1759 } while (ev.type != ButtonRelease);
1761 XSync(display, False);
1762 resize_window(win, args->id);
1764 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
1766 XUngrabPointer(display, CurrentTime);
1769 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
1773 move_window(struct ws_win *win)
1777 struct swm_region *r;
1779 r = root_to_region(win->wa.root);
1780 bzero(&wc, sizeof wc);
1785 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
1786 win->id, wc.x, wc.y, wc.width, wc.height);
1788 XConfigureWindow(display, win->id, mask, &wc);
1793 move(struct ws_win *win, union arg *args)
1799 DNPRINTF(SWM_D_MOUSE, "move: win %d floating %d trans %d\n",
1800 win->id, win->floating, win->transient);
1802 if (win->floating == 0) {
1808 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
1809 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
1811 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
1813 XMaskEvent(display, MOUSEMASK | ExposureMask |
1814 SubstructureRedirectMask, &ev);
1816 case ConfigureRequest:
1819 handler[ev.type](&ev);
1822 win->g.x = ev.xmotion.x_root;
1823 win->g.y = ev.xmotion.y_root;
1825 /* not free, don't sync more than 60 times / second */
1826 if ((ev.xmotion.time - time) > (1000 / 60) ) {
1827 time = ev.xmotion.time;
1828 XSync(display, False);
1833 } while (ev.type != ButtonRelease);
1835 XSync(display, False);
1838 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
1839 XUngrabPointer(display, CurrentTime);
1844 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
1848 enum { client_click, root_click };
1850 unsigned int action;
1852 unsigned int button;
1853 void (*func)(struct ws_win *, union arg *);
1856 /* action key mouse button func args */
1857 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
1858 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
1859 { client_click, MODKEY, Button1, move, {0} },
1863 updatenumlockmask(void)
1866 XModifierKeymap *modmap;
1868 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1870 modmap = XGetModifierMapping(display);
1871 for (i = 0; i < 8; i++)
1872 for (j = 0; j < modmap->max_keypermod; j++)
1873 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1874 == XKeysymToKeycode(display, XK_Num_Lock))
1875 numlockmask = (1 << i);
1877 XFreeModifiermap(modmap);
1883 unsigned int i, j, k;
1885 unsigned int modifiers[] =
1886 { 0, LockMask, numlockmask, numlockmask | LockMask };
1888 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1889 updatenumlockmask();
1891 for (k = 0; k < ScreenCount(display); k++) {
1892 if (TAILQ_EMPTY(&screens[k].rl))
1894 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
1895 for (i = 0; i < LENGTH(keys); i++) {
1896 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
1897 for (j = 0; j < LENGTH(modifiers); j++)
1898 XGrabKey(display, code,
1899 keys[i].mod | modifiers[j],
1900 screens[k].root, True,
1901 GrabModeAsync, GrabModeAsync);
1907 grabbuttons(struct ws_win *win, int focused)
1910 unsigned int modifiers[] =
1911 { 0, LockMask, numlockmask, numlockmask|LockMask };
1913 updatenumlockmask();
1914 XUngrabButton(display, AnyButton, AnyModifier, win->id);
1916 for (i = 0; i < LENGTH(buttons); i++)
1917 if (buttons[i].action == client_click)
1918 for (j = 0; j < LENGTH(modifiers); j++)
1919 XGrabButton(display, buttons[i].button,
1920 buttons[i].mask | modifiers[j],
1921 win->id, False, BUTTONMASK,
1922 GrabModeAsync, GrabModeSync, None,
1925 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
1926 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1932 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1940 XKeyEvent *ev = &e->xkey;
1942 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1944 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1945 for (i = 0; i < LENGTH(keys); i++)
1946 if (keysym == keys[i].keysym
1947 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1949 keys[i].func(root_to_region(ev->root),
1954 buttonpress(XEvent *e)
1956 XButtonPressedEvent *ev = &e->xbutton;
1961 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1963 action = root_click;
1964 if ((win = find_window(ev->window)) == NULL)
1968 action = client_click;
1971 for (i = 0; i < LENGTH(buttons); i++)
1972 if (action == buttons[i].action && buttons[i].func &&
1973 buttons[i].button == ev->button &&
1974 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
1975 buttons[i].func(win, &buttons[i].args);
1979 set_win_state(struct ws_win *win, long state)
1981 long data[] = {state, None};
1983 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1985 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1986 (unsigned char *)data, 2);
1990 manage_window(Window id)
1993 struct workspace *ws;
1995 int format, i, ws_idx;
1996 unsigned long nitems, bytes;
1997 Atom ws_idx_atom = 0, type;
1998 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
1999 struct swm_region *r;
2003 if ((win = find_window(id)) != NULL)
2004 return (win); /* already being managed */
2006 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
2007 errx(1, "calloc: failed to allocate memory for new window");
2009 /* Get all the window data in one shot */
2010 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2012 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
2013 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
2014 XGetWindowAttributes(display, id, &win->wa);
2015 XGetTransientForHint(display, id, &trans);
2016 XGetWMNormalHints(display, id, &win->sh, &mask); /* XXX function? */
2018 win->transient = trans;
2019 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
2020 (unsigned)win->id, win->transient);
2024 * Figure out where to put the window. If it was previously assigned to
2025 * a workspace (either by spawn() or manually moving), and isn't
2026 * transient, * put it in the same workspace
2028 r = root_to_region(win->wa.root);
2029 if (prop && win->transient == 0) {
2030 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
2031 ws_idx = strtonum(prop, 0, 9, &errstr);
2033 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
2035 ws = &r->s->ws[ws_idx];
2039 /* set up the window layout */
2042 win->s = r->s; /* this never changes */
2043 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
2045 win->g.w = win->wa.width;
2046 win->g.h = win->wa.height;
2047 win->g.x = win->wa.x;
2048 win->g.y = win->wa.y;
2050 /* Set window properties so we can remember this after reincarnation */
2051 if (ws_idx_atom && prop == NULL &&
2052 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
2053 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2055 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2056 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2061 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);
2064 if (XGetClassHint(display, win->id, &win->ch)) {
2065 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
2066 win->ch.res_class, win->ch.res_name);
2067 for (i = 0; quirks[i].class != NULL && quirks[i].name != NULL &&
2068 quirks[i].quirk != 0; i++){
2069 if (!strcmp(win->ch.res_class, quirks[i].class) &&
2070 !strcmp(win->ch.res_name, quirks[i].name)) {
2071 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
2072 win->ch.res_class, win->ch.res_name);
2073 if (quirks[i].quirk & SWM_Q_FLOAT)
2079 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
2080 PropertyChangeMask | StructureNotifyMask);
2082 set_win_state(win, NormalState);
2084 /* make new win focused */
2091 configurerequest(XEvent *e)
2093 XConfigureRequestEvent *ev = &e->xconfigurerequest;
2098 if ((win = find_window(ev->window)) == NULL)
2102 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
2105 fprintf(stderr, "configurerequest: new window: %lu x %d y %d w %d h %d bw %d s %d sm %d\n",
2106 ev->window, ev->x, ev->y, ev->width, ev->height, ev->border_width, ev->above, ev->detail);
2108 bzero(&wc, sizeof wc);
2111 wc.width = ev->width;
2112 wc.height = ev->height;
2113 wc.border_width = ev->border_width;
2114 wc.sibling = ev->above;
2115 wc.stack_mode = ev->detail;
2116 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
2119 fprintf(stderr, "configurerequest: change window: %lu\n",
2122 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
2124 if (win->floating) {
2125 if (ev->value_mask & CWX)
2127 if (ev->value_mask & CWY)
2129 if (ev->value_mask & CWWidth)
2130 win->g.w = ev->width;
2131 if (ev->value_mask & CWHeight)
2132 win->g.h = ev->height;
2133 if (win->ws->r != NULL) {
2134 /* this seems to be full screen */
2135 if (win->g.w >= WIDTH(win->ws->r)) {
2137 win->g.w = WIDTH(win->ws->r);
2138 ev->value_mask |= CWX | CWWidth;
2140 if (win->g.h >= HEIGHT(win->ws->r)) {
2143 win->g.h = HEIGHT(win->ws->r);
2144 ev->value_mask |= CWY | CWHeight;
2147 if ((ev->value_mask & (CWX|CWY)) &&
2148 !(ev->value_mask & (CWWidth|CWHeight)))
2150 XMoveResizeWindow(display, win->id,
2151 win->g.x, win->g.y, win->g.w, win->g.h);
2158 configurenotify(XEvent *e)
2160 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
2161 e->xconfigure.window);
2165 destroynotify(XEvent *e)
2168 XDestroyWindowEvent *ev = &e->xdestroywindow;
2169 struct workspace *ws;
2171 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
2173 if ((win = find_window(ev->window)) != NULL) {
2175 /* find a window to focus */
2176 if (ws->focus == win)
2177 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
2178 if (ws->focus == NULL)
2179 ws->focus = TAILQ_FIRST(&ws->winlist);
2180 if (ws->focus == NULL || ws->focus == win) {
2184 focus_win(ws->focus);
2185 TAILQ_REMOVE(&ws->winlist, win, entry);
2186 set_win_state(win, WithdrawnState);
2187 if (win->ch.res_class)
2188 XFree(win->ch.res_class);
2189 if (win->ch.res_name)
2190 XFree(win->ch.res_name);
2197 enternotify(XEvent *e)
2199 XCrossingEvent *ev = &e->xcrossing;
2202 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
2205 /* eat event(r) to prevent autofocus */
2210 if ((win = find_window(ev->window)) != NULL)
2217 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
2221 mappingnotify(XEvent *e)
2223 XMappingEvent *ev = &e->xmapping;
2225 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
2227 XRefreshKeyboardMapping(ev);
2228 if (ev->request == MappingKeyboard)
2233 maprequest(XEvent *e)
2235 XMapRequestEvent *ev = &e->xmaprequest;
2236 XWindowAttributes wa;
2238 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
2239 e->xmaprequest.window);
2241 if (!XGetWindowAttributes(display, ev->window, &wa))
2243 if (wa.override_redirect)
2245 manage_window(e->xmaprequest.window);
2250 propertynotify(XEvent *e)
2253 XPropertyEvent *ev = &e->xproperty;
2255 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
2258 if (ev->state == PropertyDelete)
2259 return; /* ignore */
2260 win = find_window(ev->window);
2265 case XA_WM_NORMAL_HINTS:
2268 XGetWMNormalHints(display, win->id, &win->sh, &mask);
2269 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
2270 if (win->sh.flags & PMinSize) {
2271 win->g.w = win->sh.min_width;
2272 win->g.h = win->sh.min_height;
2273 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
2275 XMoveResizeWindow(display, win->id,
2276 win->g.x, win->g.y, win->g.w, win->g.h);
2285 unmapnotify(XEvent *e)
2287 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
2291 visibilitynotify(XEvent *e)
2294 struct swm_region *r;
2296 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
2297 e->xvisibility.window);
2298 if (e->xvisibility.state == VisibilityUnobscured)
2299 for (i = 0; i < ScreenCount(display); i++)
2300 TAILQ_FOREACH(r, &screens[i].rl, entry)
2301 if (e->xvisibility.window == r->bar_window)
2306 xerror_start(Display *d, XErrorEvent *ee)
2313 xerror(Display *d, XErrorEvent *ee)
2315 /* fprintf(stderr, "error: %p %p\n", display, ee); */
2323 xerrorxlib = XSetErrorHandler(xerror_start);
2325 /* this causes an error if some other window manager is running */
2326 XSelectInput(display, DefaultRootWindow(display),
2327 SubstructureRedirectMask);
2328 XSync(display, False);
2332 XSetErrorHandler(xerror);
2333 XSync(display, False);
2342 unsigned char *p = NULL;
2343 unsigned long n, extra;
2346 astate = XInternAtom(display, "WM_STATE", False);
2347 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
2348 &real, &format, &n, &extra, (unsigned char **)&p);
2349 if (status != Success)
2358 new_region(struct swm_screen *s, struct workspace *ws,
2359 int x, int y, int w, int h)
2361 struct swm_region *r;
2363 DNPRINTF(SWM_D_MISC, "new region on screen %d: %dx%d (%d, %d)\n",
2364 s->idx, x, y, w, h);
2366 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
2367 errx(1, "calloc: failed to allocate memory for screen");
2376 TAILQ_INSERT_TAIL(&s->rl, r, entry);
2383 #ifdef SWM_XRR_HAS_CRTC
2385 XRRScreenResources *sr;
2387 #endif /* SWM_XRR_HAS_CRTC */
2388 Window d1, d2, *wins = NULL;
2389 XWindowAttributes wa;
2390 struct swm_region *r;
2392 int errorbase, major, minor;
2393 int ncrtc = 0, w = 0;
2395 struct workspace *ws;
2399 if ((screens = calloc(ScreenCount(display),
2400 sizeof(struct swm_screen))) == NULL)
2401 errx(1, "calloc: screens");
2403 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2406 /* map physical screens */
2407 for (i = 0; i < ScreenCount(display); i++) {
2408 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
2410 TAILQ_INIT(&screens[i].rl);
2411 screens[i].root = RootWindow(display, i);
2413 /* set default colors */
2414 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
2415 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
2416 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
2417 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
2418 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
2420 /* init all workspaces */
2421 for (j = 0; j < SWM_WS_MAX; j++) {
2422 ws = &screens[i].ws[j];
2427 TAILQ_INIT(&ws->winlist);
2429 for (k = 0; layouts[k].l_stack != NULL; k++)
2430 if (layouts[k].l_config != NULL)
2431 layouts[k].l_config(ws,
2432 SWM_ARG_ID_STACKINIT);
2433 ws->cur_layout = &layouts[0];
2436 /* map virtual screens onto physical screens */
2437 screens[i].xrandr_support = XRRQueryExtension(display,
2438 &xrandr_eventbase, &errorbase);
2439 if (screens[i].xrandr_support)
2440 if (XRRQueryVersion(display, &major, &minor) &&
2442 screens[i].xrandr_support = 0;
2444 #if 0 /* not ready for dynamic screen changes */
2445 if (screens[i].xrandr_support)
2446 XRRSelectInput(display,
2448 RRScreenChangeNotifyMask);
2451 /* grab existing windows (before we build the bars)*/
2452 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
2455 #ifdef SWM_XRR_HAS_CRTC
2456 sr = XRRGetScreenResources(display, screens[i].root);
2458 new_region(&screens[i], &screens[i].ws[w],
2459 0, 0, DisplayWidth(display, i),
2460 DisplayHeight(display, i));
2464 for (c = 0, ci = NULL; c < ncrtc; c++) {
2465 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
2466 if (ci->noutput == 0)
2469 if (ci != NULL && ci->mode == None)
2470 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2471 DisplayWidth(display, i),
2472 DisplayHeight(display, i));
2474 new_region(&screens[i], &screens[i].ws[w],
2475 ci->x, ci->y, ci->width, ci->height);
2479 XRRFreeCrtcInfo(ci);
2480 XRRFreeScreenResources(sr);
2482 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2483 DisplayWidth(display, i),
2484 DisplayHeight(display, i));
2485 #endif /* SWM_XRR_HAS_CRTC */
2487 /* attach windows to a region */
2488 /* normal windows */
2489 if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
2490 errx(1, "no regions on screen %d", i);
2492 for (i = 0; i < no; i++) {
2493 XGetWindowAttributes(display, wins[i], &wa);
2494 if (!XGetWindowAttributes(display, wins[i], &wa) ||
2495 wa.override_redirect ||
2496 XGetTransientForHint(display, wins[i], &d1))
2499 if (wa.map_state == IsViewable ||
2500 getstate(wins[i]) == NormalState)
2501 manage_window(wins[i]);
2503 /* transient windows */
2504 for (i = 0; i < no; i++) {
2505 if (!XGetWindowAttributes(display, wins[i], &wa))
2508 if (XGetTransientForHint(display, wins[i], &d1) &&
2509 (wa.map_state == IsViewable || getstate(wins[i]) ==
2511 manage_window(wins[i]);
2521 main(int argc, char *argv[])
2524 char conf[PATH_MAX], *cfile = NULL;
2531 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
2532 SWM_VERSION, cvstag);
2533 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2534 warnx("no locale support");
2536 if (!(display = XOpenDisplay(0)))
2537 errx(1, "can not open display");
2540 errx(1, "other wm running");
2542 astate = XInternAtom(display, "WM_STATE", False);
2544 /* look for local and global conf file */
2545 pwd = getpwuid(getuid());
2547 errx(1, "invalid user %d", getuid());
2551 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
2552 if (stat(conf, &sb) != -1) {
2553 if (S_ISREG(sb.st_mode))
2556 /* try global conf file */
2557 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
2558 if (!stat(conf, &sb))
2559 if (S_ISREG(sb.st_mode))
2566 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2571 xfd = ConnectionNumber(display);
2575 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
2577 errx(1, "select failed");
2582 while(XPending(display)) {
2583 XNextEvent(display, &e);
2584 if (handler[e.type])
2585 handler[e.type](&e);
2589 XCloseDisplay(display);