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;
221 struct workspace *ws; /* always valid */
222 struct swm_screen *s; /* always valid, never changes */
223 XWindowAttributes wa;
227 TAILQ_HEAD(ws_win_list, ws_win);
229 /* layout handlers */
231 void vertical_config(struct workspace *, int);
232 void vertical_stack(struct workspace *, struct swm_geometry *);
233 void horizontal_config(struct workspace *, int);
234 void horizontal_stack(struct workspace *, struct swm_geometry *);
235 void max_stack(struct workspace *, struct swm_geometry *);
237 void grabbuttons(struct ws_win *, int);
240 void (*l_stack)(struct workspace *, struct swm_geometry *);
241 void (*l_config)(struct workspace *, int);
243 /* stack, configure */
244 { vertical_stack, vertical_config},
245 { horizontal_stack, horizontal_config},
250 #define SWM_H_SLICE (32)
251 #define SWM_V_SLICE (32)
253 /* define work spaces */
255 int idx; /* workspace index */
256 int restack; /* restack on switch */
257 struct layout *cur_layout; /* current layout handlers */
258 struct ws_win *focus; /* may be NULL */
259 struct swm_region *r; /* may be NULL */
260 struct ws_win_list winlist; /* list of windows in ws */
264 int horizontal_msize;
271 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
272 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
274 /* physical screen mapping */
275 #define SWM_WS_MAX (10) /* XXX Too small? */
277 int idx; /* screen index */
278 struct swm_region_list rl; /* list of regions on this screen */
281 struct workspace ws[SWM_WS_MAX];
287 } c[SWM_S_COLOR_MAX];
289 struct swm_screen *screens;
292 struct ws_win *cur_focus = NULL;
294 /* args to functions */
297 #define SWM_ARG_ID_FOCUSNEXT (0)
298 #define SWM_ARG_ID_FOCUSPREV (1)
299 #define SWM_ARG_ID_FOCUSMAIN (2)
300 #define SWM_ARG_ID_SWAPNEXT (3)
301 #define SWM_ARG_ID_SWAPPREV (4)
302 #define SWM_ARG_ID_SWAPMAIN (5)
303 #define SWM_ARG_ID_MASTERSHRINK (6)
304 #define SWM_ARG_ID_MASTERGROW (7)
305 #define SWM_ARG_ID_MASTERADD (8)
306 #define SWM_ARG_ID_MASTERDEL (9)
307 #define SWM_ARG_ID_STACKRESET (10)
308 #define SWM_ARG_ID_STACKINIT (11)
309 #define SWM_ARG_ID_CYCLEWS_UP (12)
310 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
311 #define SWM_ARG_ID_CYCLESC_UP (14)
312 #define SWM_ARG_ID_CYCLESC_DOWN (15)
313 #define SWM_ARG_ID_SS_ALL (0)
314 #define SWM_ARG_ID_SS_WINDOW (1)
323 #define SWM_Q_FLOAT (1<<0)
325 { "MPlayer", "xv", SWM_Q_FLOAT },
326 { "OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT },
331 void expose(XEvent *);
332 void keypress(XEvent *);
333 void buttonpress(XEvent *);
334 void configurerequest(XEvent *);
335 void configurenotify(XEvent *);
336 void destroynotify(XEvent *);
337 void enternotify(XEvent *);
338 void focusin(XEvent *);
339 void mappingnotify(XEvent *);
340 void maprequest(XEvent *);
341 void propertynotify(XEvent *);
342 void unmapnotify(XEvent *);
343 void visibilitynotify(XEvent *);
345 void (*handler[LASTEvent])(XEvent *) = {
347 [KeyPress] = keypress,
348 [ButtonPress] = buttonpress,
349 [ConfigureRequest] = configurerequest,
350 [ConfigureNotify] = configurenotify,
351 [DestroyNotify] = destroynotify,
352 [EnterNotify] = enternotify,
354 [MappingNotify] = mappingnotify,
355 [MapRequest] = maprequest,
356 [PropertyNotify] = propertynotify,
357 [UnmapNotify] = unmapnotify,
358 [VisibilityNotify] = visibilitynotify,
362 name_to_color(char *colorname)
366 XColor screen_def, exact_def;
367 unsigned long result = 0;
368 char cname[32] = "#";
370 cmap = DefaultColormap(display, screens[0].idx);
371 status = XAllocNamedColor(display, cmap, colorname,
372 &screen_def, &exact_def);
374 strlcat(cname, colorname + 2, sizeof cname - 1);
375 status = XAllocNamedColor(display, cmap, cname, &screen_def,
379 result = screen_def.pixel;
381 fprintf(stderr, "color '%s' not found.\n", colorname);
387 setscreencolor(char *val, int i, int c)
389 if (i > 0 && i <= ScreenCount(display)) {
390 screens[i - 1].c[c].color = name_to_color(val);
391 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
393 } else if (i == -1) {
394 for (i = 0; i < ScreenCount(display); i++)
395 screens[i].c[c].color = name_to_color(val);
396 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
399 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
400 i, ScreenCount(display));
404 varmatch(char *var, char *name, int *index)
409 i = strncmp(var, name, 255);
416 p = var + strlen(name);
419 bzero(buf, sizeof buf);
421 while (isdigit(*p) && i < sizeof buf)
423 if (i == 0 || i >= sizeof buf || *p != ']')
425 *index = strtonum(buf, 0, 99, NULL);
429 /* conf file stuff */
430 #define SWM_CONF_WS "\n= \t"
431 #define SWM_CONF_FILE "scrotwm.conf"
433 conf_load(char *filename)
436 char *line, *cp, *var, *val;
437 size_t len, lineno = 0;
440 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
442 if (filename == NULL)
445 if ((config = fopen(filename, "r")) == NULL)
448 for (sc = ScreenCount(display);;) {
449 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
453 cp += (long)strspn(cp, SWM_CONF_WS);
459 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
461 cp += (long)strspn(cp, SWM_CONF_WS);
462 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
465 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
468 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
469 bar_enabled = atoi(val);
470 else if (!varmatch(var, "bar_border", &i))
471 setscreencolor(val, i, SWM_S_COLOR_BAR_BORDER);
472 else if (!varmatch(var, "bar_color", &i))
473 setscreencolor(val, i, SWM_S_COLOR_BAR);
474 else if (!varmatch(var, "bar_font_color", &i))
475 setscreencolor(val, i, SWM_S_COLOR_BAR_FONT);
476 else if (!strncmp(var, "bar_font", strlen("bar_font")))
477 asprintf(&bar_fonts[0], "%s", val);
478 else if (!strncmp(var, "bar_action", strlen("bar_action")))
479 asprintf(&bar_argv[0], "%s", val);
480 else if (!strncmp(var, "bar_delay", strlen("bar_delay")))
481 bar_delay = atoi(val);
487 if (!varmatch(var, "color_focus", &i))
488 setscreencolor(val, i, SWM_S_COLOR_FOCUS);
489 else if (!varmatch(var, "color_unfocus", &i))
490 setscreencolor(val, i, SWM_S_COLOR_UNFOCUS);
491 else if (!strncmp(var, "cycle_empty", strlen("cycle_empty")))
492 cycle_visible = atoi(val);
493 else if (!strncmp(var, "cycle_visible", strlen("cycle_visible")))
494 cycle_visible = atoi(val);
500 if (!strncmp(var, "dialog_ratio",
501 strlen("dialog_ratio"))) {
502 dialog_ratio = atof(val);
503 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
510 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
511 asprintf(&spawn_term[0], "%s", val);
512 if (!strncmp(var, "screenshot_enabled",
513 strlen("screenshot_enabled")))
514 ss_enabled = atoi(val);
515 if (!strncmp(var, "screenshot_app",
516 strlen("screenshot_app")))
517 asprintf(&spawn_screenshot[0], "%s", val);
529 errx(1, "invalid conf file entry: %s=%s", var, val);
533 socket_setnonblock(int fd)
537 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
538 err(1, "fcntl F_GETFL");
540 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
541 err(1, "fcntl F_SETFL");
545 bar_print(struct swm_region *r, char *s)
547 XClearWindow(display, r->bar_window);
548 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
549 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
558 bzero(bar_pipe, sizeof bar_pipe);
561 kill(bar_pid, SIGTERM);
564 strlcpy(bar_ext, "", sizeof bar_ext);
573 struct swm_region *r;
577 char loc[SWM_BAR_MAX];
580 if (bar_enabled == 0)
583 if (bar_extra && bar_extra_running) {
584 /* ignore short reads; it'll correct itself */
585 while ((b = fgetln(stdin, &len)) != NULL)
586 if (b && b[len - 1] == '\n') {
588 strlcpy(bar_ext, b, sizeof bar_ext);
590 if (b == NULL && errno != EAGAIN) {
591 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
592 errno, strerror(errno));
596 strlcpy(bar_ext, "", sizeof bar_ext);
599 localtime_r(&tmt, &tm);
600 strftime(s, sizeof s, "%a %b %d %R %Z %Y", &tm);
601 for (i = 0; i < ScreenCount(display); i++) {
603 TAILQ_FOREACH(r, &screens[i].rl, entry) {
604 snprintf(loc, sizeof loc, "%s %d:%d %s %s",
605 s, x++, r->ws->idx + 1, bar_ext, bar_vertext);
609 XSync(display, False);
620 bar_toggle(struct swm_region *r, union arg *args)
622 struct swm_region *tmpr;
623 int i, j, sc = ScreenCount(display);
625 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
628 for (i = 0; i < sc; i++)
629 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
630 XUnmapWindow(display, tmpr->bar_window);
632 for (i = 0; i < sc; i++)
633 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
634 XMapRaised(display, tmpr->bar_window);
636 bar_enabled = !bar_enabled;
637 XSync(display, False);
638 for (i = 0; i < sc; i++)
639 for (j = 0; j < SWM_WS_MAX; j++)
640 screens[i].ws[j].restack = 1;
643 /* must be after stack */
644 for (i = 0; i < sc; i++)
645 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
652 XSetWindowAttributes wa;
653 struct swm_region *r;
656 /* do this here because the conf file is in memory */
657 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
658 /* launch external status app */
659 bar_extra_running = 1;
660 if (pipe(bar_pipe) == -1)
661 err(1, "pipe error");
662 socket_setnonblock(bar_pipe[0]);
663 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
664 if (dup2(bar_pipe[0], 0) == -1)
666 if (dup2(bar_pipe[1], 1) == -1)
668 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
669 err(1, "could not disable SIGPIPE");
670 switch (bar_pid = fork()) {
672 err(1, "cannot fork");
676 execvp(bar_argv[0], bar_argv);
677 err(1, "%s external app failed", bar_argv[0]);
679 default: /* parent */
685 bzero(&wa, sizeof wa);
686 for (i = 0; i < ScreenCount(display); i++)
687 TAILQ_FOREACH(r, &screens[i].rl, entry) {
689 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
690 wa.background_pixel =
691 screens[i].c[SWM_S_COLOR_BAR].color;
692 XChangeWindowAttributes(display, r->bar_window,
693 CWBackPixel | CWBorderPixel, &wa);
699 bar_setup(struct swm_region *r)
703 for (i = 0; bar_fonts[i] != NULL; i++) {
704 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
710 if (bar_fonts[i] == NULL)
711 errx(1, "couldn't load font");
712 bar_height = bar_fs->ascent + bar_fs->descent + 3;
714 r->bar_window = XCreateSimpleWindow(display,
715 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
716 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
717 r->s->c[SWM_S_COLOR_BAR].color);
718 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
719 XSetFont(display, bar_gc, bar_fs->fid);
720 XSelectInput(display, r->bar_window, VisibilityChangeMask);
722 XMapRaised(display, r->bar_window);
723 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
725 if (signal(SIGALRM, bar_signal) == SIG_ERR)
726 err(1, "could not install bar_signal");
731 version(struct swm_region *r, union arg *args)
733 bar_version = !bar_version;
735 strlcpy(bar_vertext, cvstag, sizeof bar_vertext);
737 strlcpy(bar_vertext, "", sizeof bar_vertext);
742 config_win(struct ws_win *win)
746 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
747 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
748 ce.type = ConfigureNotify;
749 ce.display = display;
755 ce.height = win->g.h;
756 ce.border_width = 1; /* XXX store this! */
758 ce.override_redirect = False;
759 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
763 count_win(struct workspace *ws, int count_transient)
768 TAILQ_FOREACH(win, &ws->winlist, entry) {
769 if (count_transient == 0 && win->floating)
771 if (count_transient == 0 && win->transient)
775 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
781 quit(struct swm_region *r, union arg *args)
783 DNPRINTF(SWM_D_MISC, "quit\n");
793 for (i = 0; i < ScreenCount(display); i++)
794 for (j = 0; j < SWM_WS_MAX; j++)
795 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
796 XUnmapWindow(display, win->id);
800 restart(struct swm_region *r, union arg *args)
802 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
804 /* disable alarm because the following code may not be interrupted */
806 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
807 errx(1, "can't disable alarm");
812 XCloseDisplay(display);
813 execvp(start_argv[0], start_argv);
814 fprintf(stderr, "execvp failed\n");
820 root_to_region(Window root)
822 struct swm_region *r;
827 for (i = 0; i < ScreenCount(display); i++)
828 if (screens[i].root == root)
831 if (XQueryPointer(display, screens[i].root,
832 &rr, &cr, &x, &y, &wx, &wy, &mask) == False) {
833 /* if we can't query the pointer, grab the first region */
834 r = TAILQ_FIRST(&screens[i].rl);
836 /* otherwise, choose a region based on pointer location */
837 TAILQ_FOREACH(r, &screens[i].rl, entry) {
838 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
839 y >= Y(r) && y <= Y(r) + HEIGHT(r))
844 r = TAILQ_FIRST(&screens[i].rl);
850 find_window(Window id)
855 for (i = 0; i < ScreenCount(display); i++)
856 for (j = 0; j < SWM_WS_MAX; j++)
857 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
864 spawn(struct swm_region *r, union arg *args)
869 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
871 * The double-fork construct avoids zombie processes and keeps the code
872 * clean from stupid signal handlers.
877 close(ConnectionNumber(display));
878 setenv("LD_PRELOAD", SWM_LIB, 1);
879 if (asprintf(&ret, "%d", r->ws->idx)) {
880 setenv("_SWM_WS", ret, 1);
883 if (asprintf(&ret, "%d", getpid())) {
884 setenv("_SWM_PID", ret, 1);
888 /* kill stdin, mplayer, ssh-add etc. need that */
889 si = open("/dev/null", O_RDONLY, 0);
891 err(1, "open /dev/null");
892 if (dup2(si, 0) == -1)
893 err(1, "dup2 /dev/null");
894 execvp(args->argv[0], args->argv);
895 fprintf(stderr, "execvp failed\n");
904 spawnmenu(struct swm_region *r, union arg *args)
906 DNPRINTF(SWM_D_MISC, "spawnmenu\n");
908 spawn_menu[SWM_MENU_FN] = bar_fonts[bar_fidx];
909 spawn_menu[SWM_MENU_NB] = r->s->c[SWM_S_COLOR_BAR].name;
910 spawn_menu[SWM_MENU_NF] = r->s->c[SWM_S_COLOR_BAR_FONT].name;
911 spawn_menu[SWM_MENU_SB] = r->s->c[SWM_S_COLOR_BAR_BORDER].name;
912 spawn_menu[SWM_MENU_SF] = r->s->c[SWM_S_COLOR_BAR].name;
923 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
925 for (i = 0; i < ScreenCount(display); i++)
926 for (j = 0; j < SWM_WS_MAX; j++)
927 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
928 if (win->ws->r == NULL)
931 XSetWindowBorder(display, win->id,
932 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
934 win->ws->focus = NULL;
940 focus_win(struct ws_win *win)
942 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
948 win->ws->focus = win;
949 if (win->ws->r != NULL) {
951 if (!win->got_focus) {
952 XSetWindowBorder(display, win->id,
953 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
957 XSetInputFocus(display, win->id,
958 RevertToPointerRoot, CurrentTime);
963 switchws(struct swm_region *r, union arg *args)
966 struct swm_region *this_r, *other_r;
968 struct workspace *new_ws, *old_ws;
972 new_ws = &this_r->s->ws[wsid];
974 DNPRINTF(SWM_D_WS, "switchws screen %d region %dx%d+%d+%d: "
975 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
978 if (new_ws == old_ws)
983 /* if the other workspace is hidden, switch windows */
984 /* map new window first to prevent ugly blinking */
985 TAILQ_FOREACH(win, &new_ws->winlist, entry)
986 XMapRaised(display, win->id);
988 TAILQ_FOREACH(win, &old_ws->winlist, entry)
989 XUnmapWindow(display, win->id);
993 other_r->ws = old_ws;
1002 focus_win(new_ws->focus);
1008 cyclews(struct swm_region *r, union arg *args)
1011 struct swm_screen *s = r->s;
1013 DNPRINTF(SWM_D_WS, "cyclews id %d "
1014 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1015 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1020 case SWM_ARG_ID_CYCLEWS_UP:
1021 if (a.id < SWM_WS_MAX - 1)
1026 case SWM_ARG_ID_CYCLEWS_DOWN:
1030 a.id = SWM_WS_MAX - 1;
1036 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1038 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1042 } while (a.id != r->ws->idx);
1046 cyclescr(struct swm_region *r, union arg *args)
1048 struct swm_region *rr;
1053 case SWM_ARG_ID_CYCLESC_UP:
1054 rr = TAILQ_NEXT(r, entry);
1056 rr = TAILQ_FIRST(&screens[i].rl);
1058 case SWM_ARG_ID_CYCLESC_DOWN:
1059 rr = TAILQ_PREV(r, swm_region_list, entry);
1061 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1067 XSetInputFocus(display, PointerRoot, RevertToPointerRoot, CurrentTime);
1068 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, rr->g.x,
1069 rr->g.y + bar_enabled ? bar_height : 0);
1073 swapwin(struct swm_region *r, union arg *args)
1075 struct ws_win *target;
1076 struct ws_win_list *wl;
1079 DNPRINTF(SWM_D_WS, "swapwin id %d "
1080 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1081 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1082 if (cur_focus == NULL)
1085 wl = &cur_focus->ws->winlist;
1088 case SWM_ARG_ID_SWAPPREV:
1089 target = TAILQ_PREV(cur_focus, ws_win_list, entry);
1090 TAILQ_REMOVE(wl, cur_focus, entry);
1092 TAILQ_INSERT_TAIL(wl, cur_focus, entry);
1094 TAILQ_INSERT_BEFORE(target, cur_focus, entry);
1096 case SWM_ARG_ID_SWAPNEXT:
1097 target = TAILQ_NEXT(cur_focus, entry);
1098 TAILQ_REMOVE(wl, cur_focus, entry);
1100 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
1102 TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
1104 case SWM_ARG_ID_SWAPMAIN:
1105 target = TAILQ_FIRST(wl);
1106 if (target == cur_focus)
1108 TAILQ_REMOVE(wl, target, entry);
1109 TAILQ_INSERT_BEFORE(cur_focus, target, entry);
1110 TAILQ_REMOVE(wl, cur_focus, entry);
1111 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
1114 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1123 focus(struct swm_region *r, union arg *args)
1125 struct ws_win *winfocus, *winlostfocus;
1126 struct ws_win_list *wl;
1128 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1129 if (cur_focus == NULL)
1132 wl = &cur_focus->ws->winlist;
1134 winlostfocus = cur_focus;
1137 case SWM_ARG_ID_FOCUSPREV:
1138 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1139 if (winfocus == NULL)
1140 winfocus = TAILQ_LAST(wl, ws_win_list);
1143 case SWM_ARG_ID_FOCUSNEXT:
1144 winfocus = TAILQ_NEXT(cur_focus, entry);
1145 if (winfocus == NULL)
1146 winfocus = TAILQ_FIRST(wl);
1149 case SWM_ARG_ID_FOCUSMAIN:
1150 winfocus = TAILQ_FIRST(wl);
1157 if (winfocus == winlostfocus || winfocus == NULL)
1160 XMapRaised(display, winfocus->id);
1161 focus_win(winfocus);
1162 XSync(display, False);
1166 cycle_layout(struct swm_region *r, union arg *args)
1168 struct workspace *ws = r->ws;
1170 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1173 if (ws->cur_layout->l_stack == NULL)
1174 ws->cur_layout = &layouts[0];
1180 stack_config(struct swm_region *r, union arg *args)
1182 struct workspace *ws = r->ws;
1184 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1187 if (ws->cur_layout->l_config != NULL)
1188 ws->cur_layout->l_config(ws, args->id);
1190 if (args->id != SWM_ARG_ID_STACKINIT);
1196 struct swm_geometry g;
1197 struct swm_region *r;
1200 DNPRINTF(SWM_D_STACK, "stack\n");
1202 for (i = 0; i < ScreenCount(display); i++) {
1204 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1205 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1206 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1208 /* start with screen geometry, adjust for bar */
1218 r->ws->cur_layout->l_stack(r->ws, &g);
1221 XSync(display, False);
1225 stack_floater(struct ws_win *win, struct swm_region *r)
1230 bzero(&wc, sizeof wc);
1231 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1232 wc.border_width = 1;
1233 if (win->transient) {
1234 win->g.w = (double)WIDTH(r) * dialog_ratio;
1235 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1237 wc.width = win->g.w;
1238 wc.height = win->g.h;
1239 wc.x = (WIDTH(r) - win->g.w) / 2;
1240 wc.y = (HEIGHT(r) - win->g.h) / 2;
1242 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1243 win->id, wc.x, wc.y, wc.width, wc.height);
1245 XConfigureWindow(display, win->id, mask, &wc);
1248 #define SWAPXY(g) do { \
1250 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1251 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1254 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1257 struct swm_geometry win_g, r_g = *g;
1258 struct ws_win *win, *winfocus;
1259 int i, j, w_inc, h_inc, w_base, h_base;
1260 int hrh, extra, h_slice, last_h = 0;
1261 int split, colno, winno, mwin, msize, mscale;
1262 int remain, missing, v_slice;;
1265 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1266 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1268 if ((winno = count_win(ws, 0)) == 0)
1271 if (ws->focus == NULL)
1272 ws->focus = TAILQ_FIRST(&ws->winlist);
1273 winfocus = cur_focus ? cur_focus : ws->focus;
1275 win = TAILQ_FIRST(&ws->winlist);
1277 w_inc = win->sh.width_inc;
1278 w_base = win->sh.base_width;
1279 mwin = ws->l_state.horizontal_mwin;
1280 mscale = ws->l_state.horizontal_msize;
1283 w_inc = win->sh.height_inc;
1284 w_base = win->sh.base_height;
1285 mwin = ws->l_state.vertical_mwin;
1286 mscale = ws->l_state.vertical_msize;
1290 h_slice = r_g.h / SWM_H_SLICE;
1291 if (mwin && winno > mwin) {
1292 v_slice = r_g.w / SWM_V_SLICE;
1296 msize = v_slice * mscale;
1298 if (w_inc > 1 && w_inc < v_slice) {
1299 /* adjust for window's requested size increment */
1300 remain = (win_g.w - w_base) % w_inc;
1301 missing = w_inc - remain;
1303 if (missing <= extra || j == 0) {
1314 win_g.x += r_g.w - msize;
1319 hrh = r_g.h / colno;
1320 extra = r_g.h - (colno * hrh);
1324 TAILQ_FOREACH(win, &ws->winlist, entry) {
1325 if (split && i == split) {
1326 colno = winno - split;
1327 hrh = (r_g.h / colno);
1328 extra = r_g.h - (colno * hrh);
1332 win_g.x += msize + 2;
1333 win_g.w = r_g.w - (msize + 2);
1338 h_inc = win->sh.width_inc;
1339 h_base = win->sh.base_width;
1341 h_inc = win->sh.height_inc;
1342 h_base = win->sh.base_height;
1344 if (j == colno - 1) {
1345 win_g.h = hrh + extra;
1346 } else if (h_inc > 1 && h_inc < h_slice) {
1347 /* adjust for window's requested size increment */
1348 remain = (win_g.h - h_base) % h_inc;
1349 missing = h_inc - remain;
1351 if (missing <= extra || j == 0) {
1363 win_g.y += last_h + 2;
1365 if (win->transient != 0 || win->floating != 0)
1366 stack_floater(win, ws->r);
1368 bzero(&wc, sizeof wc);
1369 wc.border_width = 1;
1371 win->g.x = wc.x = win_g.y;
1372 win->g.y = wc.y = win_g.x;
1373 win->g.w = wc.width = win_g.h;
1374 win->g.h = wc.height = win_g.w;
1376 win->g.x = wc.x = win_g.x;
1377 win->g.y = wc.y = win_g.y;
1378 win->g.w = wc.width = win_g.w;
1379 win->g.h = wc.height = win_g.h;
1381 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1382 XConfigureWindow(display, win->id, mask, &wc);
1384 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);
1388 XMapRaised(display, win->id);
1395 focus_win(winfocus); /* has to be done outside of the loop */
1399 vertical_config(struct workspace *ws, int id)
1401 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1404 case SWM_ARG_ID_STACKRESET:
1405 case SWM_ARG_ID_STACKINIT:
1406 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1407 ws->l_state.vertical_mwin = 1;
1409 case SWM_ARG_ID_MASTERSHRINK:
1410 if (ws->l_state.vertical_msize > 1)
1411 ws->l_state.vertical_msize--;
1413 case SWM_ARG_ID_MASTERGROW:
1414 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1415 ws->l_state.vertical_msize++;
1417 case SWM_ARG_ID_MASTERADD:
1418 ws->l_state.vertical_mwin++;
1420 case SWM_ARG_ID_MASTERDEL:
1421 if (ws->l_state.vertical_mwin > 0)
1422 ws->l_state.vertical_mwin--;
1430 vertical_stack(struct workspace *ws, struct swm_geometry *g)
1432 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1434 stack_master(ws, g, 0, 0);
1438 horizontal_config(struct workspace *ws, int id)
1440 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1443 case SWM_ARG_ID_STACKRESET:
1444 case SWM_ARG_ID_STACKINIT:
1445 ws->l_state.horizontal_mwin = 1;
1446 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1448 case SWM_ARG_ID_MASTERSHRINK:
1449 if (ws->l_state.horizontal_msize > 1)
1450 ws->l_state.horizontal_msize--;
1452 case SWM_ARG_ID_MASTERGROW:
1453 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1454 ws->l_state.horizontal_msize++;
1456 case SWM_ARG_ID_MASTERADD:
1457 ws->l_state.horizontal_mwin++;
1459 case SWM_ARG_ID_MASTERDEL:
1460 if (ws->l_state.horizontal_mwin > 0)
1461 ws->l_state.horizontal_mwin--;
1469 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
1471 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1473 stack_master(ws, g, 1, 0);
1476 /* fullscreen view */
1478 max_stack(struct workspace *ws, struct swm_geometry *g) {
1480 struct swm_geometry gg = *g;
1481 struct ws_win *win, *winfocus;
1484 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1486 if (count_win(ws, 0) == 0)
1489 if (ws->focus == NULL)
1490 ws->focus = TAILQ_FIRST(&ws->winlist);
1491 winfocus = cur_focus ? cur_focus : ws->focus;
1493 TAILQ_FOREACH(win, &ws->winlist, entry) {
1494 if (win->transient != 0 || win->floating != 0) {
1495 if (win == ws->focus) {
1497 stack_floater(win, ws->r);
1498 XMapRaised(display, win->id);
1500 XUnmapWindow(display, win->id);
1502 bzero(&wc, sizeof wc);
1503 wc.border_width = 1;
1504 win->g.x = wc.x = gg.x;
1505 win->g.y = wc.y = gg.y;
1506 win->g.w = wc.width = gg.w;
1507 win->g.h = wc.height = gg.h;
1508 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1509 XConfigureWindow(display, win->id, mask, &wc);
1511 if (win == ws->focus) {
1512 XMapRaised(display, win->id);
1514 XUnmapWindow(display, win->id);
1519 focus_win(winfocus); /* has to be done outside of the loop */
1523 send_to_ws(struct swm_region *r, union arg *args)
1525 int wsid = args->id;
1526 struct ws_win *win = cur_focus;
1527 struct workspace *ws, *nws;
1528 Atom ws_idx_atom = 0;
1529 unsigned char ws_idx_str[SWM_PROPLEN];
1534 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1537 nws = &win->s->ws[wsid];
1539 XUnmapWindow(display, win->id);
1541 /* find a window to focus */
1542 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1543 if (ws->focus == NULL)
1544 ws->focus = TAILQ_FIRST(&ws->winlist);
1545 if (ws->focus == win)
1548 TAILQ_REMOVE(&ws->winlist, win, entry);
1550 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1553 /* Try to update the window's workspace property */
1554 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1556 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
1557 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1559 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1560 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1563 if (count_win(nws, 1) == 1)
1572 wkill(struct swm_region *r, union arg *args)
1574 DNPRINTF(SWM_D_MISC, "wkill\n");
1575 if(r->ws->focus != NULL)
1576 XKillClient(display, r->ws->focus->id);
1580 screenshot(struct swm_region *r, union arg *args)
1584 DNPRINTF(SWM_D_MISC, "screenshot\n");
1586 if (ss_enabled == 0)
1590 case SWM_ARG_ID_SS_ALL:
1591 spawn_screenshot[1] = "full";
1593 case SWM_ARG_ID_SS_WINDOW:
1594 spawn_screenshot[1] = "window";
1599 a.argv = spawn_screenshot;
1603 /* key definitions */
1607 void (*func)(struct swm_region *r, union arg *);
1610 /* modifier key function argument */
1611 { MODKEY, XK_space, cycle_layout, {0} },
1612 { MODKEY | ShiftMask, XK_space, stack_config, {.id = SWM_ARG_ID_STACKRESET} },
1613 { MODKEY, XK_h, stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
1614 { MODKEY, XK_l, stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
1615 { MODKEY, XK_comma, stack_config, {.id = SWM_ARG_ID_MASTERADD} },
1616 { MODKEY, XK_period, stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
1617 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1618 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1619 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1620 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1621 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1622 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1623 { MODKEY, XK_p, spawnmenu, {.argv = spawn_menu} },
1624 { MODKEY | ShiftMask, XK_q, quit, {0} },
1625 { MODKEY, XK_q, restart, {0} },
1626 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1627 { MODKEY, XK_1, switchws, {.id = 0} },
1628 { MODKEY, XK_2, switchws, {.id = 1} },
1629 { MODKEY, XK_3, switchws, {.id = 2} },
1630 { MODKEY, XK_4, switchws, {.id = 3} },
1631 { MODKEY, XK_5, switchws, {.id = 4} },
1632 { MODKEY, XK_6, switchws, {.id = 5} },
1633 { MODKEY, XK_7, switchws, {.id = 6} },
1634 { MODKEY, XK_8, switchws, {.id = 7} },
1635 { MODKEY, XK_9, switchws, {.id = 8} },
1636 { MODKEY, XK_0, switchws, {.id = 9} },
1637 { MODKEY, XK_Right, cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
1638 { MODKEY, XK_Left, cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
1639 { MODKEY | ShiftMask, XK_Right, cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
1640 { MODKEY | ShiftMask, XK_Left, cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
1641 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1642 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1643 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1644 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1645 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1646 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1647 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1648 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1649 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1650 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1651 { MODKEY, XK_b, bar_toggle, {0} },
1652 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1653 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1654 { MODKEY | ShiftMask, XK_x, wkill, {0} },
1655 { MODKEY, XK_s, screenshot, {.id = SWM_ARG_ID_SS_ALL} },
1656 { MODKEY | ShiftMask, XK_s, screenshot, {.id = SWM_ARG_ID_SS_WINDOW} },
1657 { MODKEY | ShiftMask, XK_v, version, {0} },
1661 resize_window(struct ws_win *win)
1665 struct swm_region *r;
1667 r = root_to_region(win->wa.root);
1668 bzero(&wc, sizeof wc);
1669 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1670 wc.border_width = 1;
1671 wc.width = win->g.w;
1672 wc.height = win->g.h;
1673 wc.x = (WIDTH(r) - win->g.w) / 2;
1674 wc.y = (HEIGHT(r) - win->g.h) / 2;
1676 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
1677 win->id, wc.x, wc.y, wc.width, wc.height);
1679 XConfigureWindow(display, win->id, mask, &wc);
1684 resize(struct ws_win *win, union arg *args)
1688 DNPRINTF(SWM_D_MOUSE, "resize: win %d floating %d trans %d\n",
1689 win->id, win->floating, win->transient);
1691 if (!(win->transient != 0 || win->floating != 0))
1694 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
1695 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
1697 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w, win->g.h);
1699 XMaskEvent(display, MOUSEMASK | ExposureMask |
1700 SubstructureRedirectMask, &ev);
1702 case ConfigureRequest:
1705 handler[ev.type](&ev);
1708 XSync(display, False);
1709 if (ev.xmotion.x < 0)
1711 if (ev.xmotion.y < 0)
1713 win->g.w = ev.xmotion.x;
1714 win->g.h = ev.xmotion.y;
1718 } while (ev.type != ButtonRelease);
1719 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
1721 XUngrabPointer(display, CurrentTime);
1724 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
1728 move_window(struct ws_win *win)
1732 struct swm_region *r;
1734 r = root_to_region(win->wa.root);
1735 bzero(&wc, sizeof wc);
1740 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
1741 win->id, wc.x, wc.y, wc.width, wc.height);
1743 XConfigureWindow(display, win->id, mask, &wc);
1748 move(struct ws_win *win, union arg *args)
1752 DNPRINTF(SWM_D_MOUSE, "move: win %d floating %d trans %d\n",
1753 win->id, win->floating, win->transient);
1755 if (!(win->transient != 0 || win->floating != 0))
1758 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
1759 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
1761 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
1763 XMaskEvent(display, MOUSEMASK | ExposureMask |
1764 SubstructureRedirectMask, &ev);
1766 case ConfigureRequest:
1769 handler[ev.type](&ev);
1772 XSync(display, False);
1773 win->g.x = ev.xmotion.x_root;
1774 win->g.y = ev.xmotion.y_root;
1778 } while (ev.type != ButtonRelease);
1779 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
1780 XUngrabPointer(display, CurrentTime);
1783 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
1787 enum { client_click, root_click };
1789 unsigned int action;
1791 unsigned int button;
1792 void (*func)(struct ws_win *, union arg *);
1795 /* action key mouse button func args */
1796 { client_click, MODKEY, Button3, resize, {0} },
1797 { client_click, MODKEY, Button1, move, {0} },
1801 updatenumlockmask(void)
1804 XModifierKeymap *modmap;
1806 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1808 modmap = XGetModifierMapping(display);
1809 for (i = 0; i < 8; i++)
1810 for (j = 0; j < modmap->max_keypermod; j++)
1811 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1812 == XKeysymToKeycode(display, XK_Num_Lock))
1813 numlockmask = (1 << i);
1815 XFreeModifiermap(modmap);
1821 unsigned int i, j, k;
1823 unsigned int modifiers[] =
1824 { 0, LockMask, numlockmask, numlockmask | LockMask };
1826 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1827 updatenumlockmask();
1829 for (k = 0; k < ScreenCount(display); k++) {
1830 if (TAILQ_EMPTY(&screens[k].rl))
1832 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
1833 for (i = 0; i < LENGTH(keys); i++) {
1834 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
1835 for (j = 0; j < LENGTH(modifiers); j++)
1836 XGrabKey(display, code,
1837 keys[i].mod | modifiers[j],
1838 screens[k].root, True,
1839 GrabModeAsync, GrabModeAsync);
1845 grabbuttons(struct ws_win *win, int focused)
1848 unsigned int modifiers[] =
1849 { 0, LockMask, numlockmask, numlockmask|LockMask };
1851 updatenumlockmask();
1852 XUngrabButton(display, AnyButton, AnyModifier, win->id);
1854 for (i = 0; i < LENGTH(buttons); i++)
1855 if (buttons[i].action == client_click)
1856 for (j = 0; j < LENGTH(modifiers); j++)
1857 XGrabButton(display, buttons[i].button,
1858 buttons[i].mask | modifiers[j],
1859 win->id, False, BUTTONMASK,
1860 GrabModeAsync, GrabModeSync, None,
1863 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
1864 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1870 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1878 XKeyEvent *ev = &e->xkey;
1880 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1882 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1883 for (i = 0; i < LENGTH(keys); i++)
1884 if (keysym == keys[i].keysym
1885 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1887 keys[i].func(root_to_region(ev->root),
1892 buttonpress(XEvent *e)
1894 XButtonPressedEvent *ev = &e->xbutton;
1899 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1901 action = root_click;
1902 if ((win = find_window(ev->window)) == NULL)
1906 action = client_click;
1909 for (i = 0; i < LENGTH(buttons); i++)
1910 if (action == buttons[i].action && buttons[i].func &&
1911 buttons[i].button == ev->button &&
1912 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
1913 buttons[i].func(win, &buttons[i].args);
1917 set_win_state(struct ws_win *win, long state)
1919 long data[] = {state, None};
1921 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1923 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1924 (unsigned char *)data, 2);
1928 manage_window(Window id)
1931 struct workspace *ws;
1933 int format, i, ws_idx;
1934 unsigned long nitems, bytes;
1935 Atom ws_idx_atom = 0, type;
1936 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
1937 struct swm_region *r;
1941 if ((win = find_window(id)) != NULL)
1942 return (win); /* already being managed */
1944 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1945 errx(1, "calloc: failed to allocate memory for new window");
1947 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1949 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
1950 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
1952 XGetWindowAttributes(display, id, &win->wa);
1953 r = root_to_region(win->wa.root);
1954 /* If the window was managed before, put it in the same workspace */
1956 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
1957 ws_idx = strtonum(prop, 0, 9, &errstr);
1959 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
1961 ws = &r->s->ws[ws_idx];
1967 win->s = r->s; /* this never changes */
1968 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
1970 XGetTransientForHint(display, win->id, &trans);
1972 win->transient = trans;
1973 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1974 (unsigned)win->id, win->transient);
1976 win->g.w = win->wa.width;
1977 win->g.h = win->wa.height;
1978 win->g.x = win->wa.x;
1979 win->g.y = win->wa.y;
1981 XGetWMNormalHints(display, win->id, &win->sh, &mask); /* XXX function? */
1983 if (ws_idx_atom && prop == NULL &&
1984 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
1985 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1987 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1988 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1993 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);
1996 if (XGetClassHint(display, win->id, &win->ch)) {
1997 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
1998 win->ch.res_class, win->ch.res_name);
1999 for (i = 0; quirks[i].class != NULL && quirks[i].name != NULL &&
2000 quirks[i].quirk != 0; i++){
2001 if (!strcmp(win->ch.res_class, quirks[i].class) &&
2002 !strcmp(win->ch.res_name, quirks[i].name)) {
2003 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
2004 win->ch.res_class, win->ch.res_name);
2005 if (quirks[i].quirk & SWM_Q_FLOAT)
2011 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
2012 PropertyChangeMask | StructureNotifyMask);
2014 set_win_state(win, NormalState);
2016 /* make new win focused */
2023 configurerequest(XEvent *e)
2025 XConfigureRequestEvent *ev = &e->xconfigurerequest;
2030 if ((win = find_window(ev->window)) == NULL)
2034 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
2037 fprintf(stderr, "configurerequest: new window: %lu x %d y %d w %d h %d bw %d s %d sm %d\n",
2038 ev->window, ev->x, ev->y, ev->width, ev->height, ev->border_width, ev->above, ev->detail);
2040 bzero(&wc, sizeof wc);
2043 wc.width = ev->width;
2044 wc.height = ev->height;
2045 wc.border_width = ev->border_width;
2046 wc.sibling = ev->above;
2047 wc.stack_mode = ev->detail;
2048 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
2051 fprintf(stderr, "configurerequest: change window: %lu\n",
2054 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
2056 if (win->floating) {
2057 if (ev->value_mask & CWX)
2059 if (ev->value_mask & CWY)
2061 if (ev->value_mask & CWWidth)
2062 win->g.w = ev->width;
2063 if (ev->value_mask & CWHeight)
2064 win->g.h = ev->height;
2065 if (win->ws->r != NULL) {
2066 /* this seems to be full screen */
2067 if (win->g.w >= WIDTH(win->ws->r)) {
2069 win->g.w = WIDTH(win->ws->r);
2070 ev->value_mask |= CWX | CWWidth;
2072 if (win->g.h >= HEIGHT(win->ws->r)) {
2075 win->g.h = HEIGHT(win->ws->r);
2076 ev->value_mask |= CWY | CWHeight;
2079 if ((ev->value_mask & (CWX|CWY)) &&
2080 !(ev->value_mask & (CWWidth|CWHeight)))
2082 XMoveResizeWindow(display, win->id,
2083 win->g.x, win->g.y, win->g.w, win->g.h);
2090 configurenotify(XEvent *e)
2092 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
2093 e->xconfigure.window);
2097 destroynotify(XEvent *e)
2100 XDestroyWindowEvent *ev = &e->xdestroywindow;
2101 struct workspace *ws;
2103 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
2105 if ((win = find_window(ev->window)) != NULL) {
2107 /* find a window to focus */
2108 if (ws->focus == win)
2109 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
2110 if (ws->focus == NULL)
2111 ws->focus = TAILQ_FIRST(&ws->winlist);
2112 if (ws->focus == NULL || ws->focus == win) {
2116 focus_win(ws->focus);
2117 TAILQ_REMOVE(&ws->winlist, win, entry);
2118 set_win_state(win, WithdrawnState);
2119 if (win->ch.res_class)
2120 XFree(win->ch.res_class);
2121 if (win->ch.res_name)
2122 XFree(win->ch.res_name);
2129 enternotify(XEvent *e)
2131 XCrossingEvent *ev = &e->xcrossing;
2134 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
2137 /* eat event(r) to prevent autofocus */
2142 if ((win = find_window(ev->window)) != NULL)
2149 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
2153 mappingnotify(XEvent *e)
2155 XMappingEvent *ev = &e->xmapping;
2157 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
2159 XRefreshKeyboardMapping(ev);
2160 if (ev->request == MappingKeyboard)
2165 maprequest(XEvent *e)
2167 XMapRequestEvent *ev = &e->xmaprequest;
2168 XWindowAttributes wa;
2170 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
2171 e->xmaprequest.window);
2173 if (!XGetWindowAttributes(display, ev->window, &wa))
2175 if (wa.override_redirect)
2177 manage_window(e->xmaprequest.window);
2182 propertynotify(XEvent *e)
2185 XPropertyEvent *ev = &e->xproperty;
2187 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
2190 if (ev->state == PropertyDelete)
2191 return; /* ignore */
2192 win = find_window(ev->window);
2197 case XA_WM_NORMAL_HINTS:
2200 XGetWMNormalHints(display, win->id, &win->sh, &mask);
2201 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
2202 if (win->sh.flags & PMinSize) {
2203 win->g.w = win->sh.min_width;
2204 win->g.h = win->sh.min_height;
2205 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
2207 XMoveResizeWindow(display, win->id,
2208 win->g.x, win->g.y, win->g.w, win->g.h);
2217 unmapnotify(XEvent *e)
2219 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
2223 visibilitynotify(XEvent *e)
2226 struct swm_region *r;
2228 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
2229 e->xvisibility.window);
2230 if (e->xvisibility.state == VisibilityUnobscured)
2231 for (i = 0; i < ScreenCount(display); i++)
2232 TAILQ_FOREACH(r, &screens[i].rl, entry)
2233 if (e->xvisibility.window == r->bar_window)
2238 xerror_start(Display *d, XErrorEvent *ee)
2245 xerror(Display *d, XErrorEvent *ee)
2247 /* fprintf(stderr, "error: %p %p\n", display, ee); */
2255 xerrorxlib = XSetErrorHandler(xerror_start);
2257 /* this causes an error if some other window manager is running */
2258 XSelectInput(display, DefaultRootWindow(display),
2259 SubstructureRedirectMask);
2260 XSync(display, False);
2264 XSetErrorHandler(xerror);
2265 XSync(display, False);
2274 unsigned char *p = NULL;
2275 unsigned long n, extra;
2278 astate = XInternAtom(display, "WM_STATE", False);
2279 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
2280 &real, &format, &n, &extra, (unsigned char **)&p);
2281 if (status != Success)
2290 new_region(struct swm_screen *s, struct workspace *ws,
2291 int x, int y, int w, int h)
2293 struct swm_region *r;
2295 DNPRINTF(SWM_D_MISC, "new region on screen %d: %dx%d (%d, %d)\n",
2296 s->idx, x, y, w, h);
2298 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
2299 errx(1, "calloc: failed to allocate memory for screen");
2308 TAILQ_INSERT_TAIL(&s->rl, r, entry);
2315 #ifdef SWM_XRR_HAS_CRTC
2317 XRRScreenResources *sr;
2319 #endif /* SWM_XRR_HAS_CRTC */
2320 Window d1, d2, *wins = NULL;
2321 XWindowAttributes wa;
2322 struct swm_region *r;
2324 int errorbase, major, minor;
2325 int ncrtc = 0, w = 0;
2327 struct workspace *ws;
2331 if ((screens = calloc(ScreenCount(display),
2332 sizeof(struct swm_screen))) == NULL)
2333 errx(1, "calloc: screens");
2335 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2338 /* map physical screens */
2339 for (i = 0; i < ScreenCount(display); i++) {
2340 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
2342 TAILQ_INIT(&screens[i].rl);
2343 screens[i].root = RootWindow(display, i);
2345 /* set default colors */
2346 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
2347 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
2348 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
2349 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
2350 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
2352 /* init all workspaces */
2353 for (j = 0; j < SWM_WS_MAX; j++) {
2354 ws = &screens[i].ws[j];
2359 TAILQ_INIT(&ws->winlist);
2361 for (k = 0; layouts[k].l_stack != NULL; k++)
2362 if (layouts[k].l_config != NULL)
2363 layouts[k].l_config(ws,
2364 SWM_ARG_ID_STACKINIT);
2365 ws->cur_layout = &layouts[0];
2368 /* map virtual screens onto physical screens */
2369 screens[i].xrandr_support = XRRQueryExtension(display,
2370 &xrandr_eventbase, &errorbase);
2371 if (screens[i].xrandr_support)
2372 if (XRRQueryVersion(display, &major, &minor) &&
2374 screens[i].xrandr_support = 0;
2376 #if 0 /* not ready for dynamic screen changes */
2377 if (screens[i].xrandr_support)
2378 XRRSelectInput(display,
2380 RRScreenChangeNotifyMask);
2383 /* grab existing windows (before we build the bars)*/
2384 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
2387 #ifdef SWM_XRR_HAS_CRTC
2388 sr = XRRGetScreenResources(display, screens[i].root);
2390 new_region(&screens[i], &screens[i].ws[w],
2391 0, 0, DisplayWidth(display, i),
2392 DisplayHeight(display, i));
2396 for (c = 0, ci = NULL; c < ncrtc; c++) {
2397 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
2398 if (ci->noutput == 0)
2401 if (ci != NULL && ci->mode == None)
2402 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2403 DisplayWidth(display, i),
2404 DisplayHeight(display, i));
2406 new_region(&screens[i], &screens[i].ws[w],
2407 ci->x, ci->y, ci->width, ci->height);
2411 XRRFreeCrtcInfo(ci);
2412 XRRFreeScreenResources(sr);
2414 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2415 DisplayWidth(display, i),
2416 DisplayHeight(display, i));
2417 #endif /* SWM_XRR_HAS_CRTC */
2419 /* attach windows to a region */
2420 /* normal windows */
2421 if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
2422 errx(1, "no regions on screen %d", i);
2424 for (i = 0; i < no; i++) {
2425 XGetWindowAttributes(display, wins[i], &wa);
2426 if (!XGetWindowAttributes(display, wins[i], &wa) ||
2427 wa.override_redirect ||
2428 XGetTransientForHint(display, wins[i], &d1))
2431 if (wa.map_state == IsViewable ||
2432 getstate(wins[i]) == NormalState)
2433 manage_window(wins[i]);
2435 /* transient windows */
2436 for (i = 0; i < no; i++) {
2437 if (!XGetWindowAttributes(display, wins[i], &wa))
2440 if (XGetTransientForHint(display, wins[i], &d1) &&
2441 (wa.map_state == IsViewable || getstate(wins[i]) ==
2443 manage_window(wins[i]);
2453 main(int argc, char *argv[])
2456 char conf[PATH_MAX], *cfile = NULL;
2463 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
2464 SWM_VERSION, cvstag);
2465 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2466 warnx("no locale support");
2468 if (!(display = XOpenDisplay(0)))
2469 errx(1, "can not open display");
2472 errx(1, "other wm running");
2474 astate = XInternAtom(display, "WM_STATE", False);
2476 /* look for local and global conf file */
2477 pwd = getpwuid(getuid());
2479 errx(1, "invalid user %d", getuid());
2483 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
2484 if (stat(conf, &sb) != -1) {
2485 if (S_ISREG(sb.st_mode))
2488 /* try global conf file */
2489 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
2490 if (!stat(conf, &sb))
2491 if (S_ISREG(sb.st_mode))
2498 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2503 xfd = ConnectionNumber(display);
2507 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
2509 errx(1, "select failed");
2514 while(XPending(display)) {
2515 XNextEvent(display, &e);
2516 if (handler[e.type])
2517 handler[e.type](&e);
2521 XCloseDisplay(display);