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)
315 #define SWM_ARG_ID_DONTCENTER (0)
316 #define SWM_ARG_ID_CENTER (1)
325 #define SWM_Q_FLOAT (1<<0)
327 { "MPlayer", "xv", SWM_Q_FLOAT },
328 { "OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT },
333 void expose(XEvent *);
334 void keypress(XEvent *);
335 void buttonpress(XEvent *);
336 void configurerequest(XEvent *);
337 void configurenotify(XEvent *);
338 void destroynotify(XEvent *);
339 void enternotify(XEvent *);
340 void focusin(XEvent *);
341 void mappingnotify(XEvent *);
342 void maprequest(XEvent *);
343 void propertynotify(XEvent *);
344 void unmapnotify(XEvent *);
345 void visibilitynotify(XEvent *);
347 void (*handler[LASTEvent])(XEvent *) = {
349 [KeyPress] = keypress,
350 [ButtonPress] = buttonpress,
351 [ConfigureRequest] = configurerequest,
352 [ConfigureNotify] = configurenotify,
353 [DestroyNotify] = destroynotify,
354 [EnterNotify] = enternotify,
356 [MappingNotify] = mappingnotify,
357 [MapRequest] = maprequest,
358 [PropertyNotify] = propertynotify,
359 [UnmapNotify] = unmapnotify,
360 [VisibilityNotify] = visibilitynotify,
364 name_to_color(char *colorname)
368 XColor screen_def, exact_def;
369 unsigned long result = 0;
370 char cname[32] = "#";
372 cmap = DefaultColormap(display, screens[0].idx);
373 status = XAllocNamedColor(display, cmap, colorname,
374 &screen_def, &exact_def);
376 strlcat(cname, colorname + 2, sizeof cname - 1);
377 status = XAllocNamedColor(display, cmap, cname, &screen_def,
381 result = screen_def.pixel;
383 fprintf(stderr, "color '%s' not found.\n", colorname);
389 setscreencolor(char *val, int i, int c)
391 if (i > 0 && i <= ScreenCount(display)) {
392 screens[i - 1].c[c].color = name_to_color(val);
393 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
395 } else if (i == -1) {
396 for (i = 0; i < ScreenCount(display); i++)
397 screens[i].c[c].color = name_to_color(val);
398 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
401 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
402 i, ScreenCount(display));
406 varmatch(char *var, char *name, int *index)
411 i = strncmp(var, name, 255);
418 p = var + strlen(name);
421 bzero(buf, sizeof buf);
423 while (isdigit(*p) && i < sizeof buf)
425 if (i == 0 || i >= sizeof buf || *p != ']')
427 *index = strtonum(buf, 0, 99, NULL);
431 /* conf file stuff */
432 #define SWM_CONF_WS "\n= \t"
433 #define SWM_CONF_FILE "scrotwm.conf"
435 conf_load(char *filename)
438 char *line, *cp, *var, *val;
439 size_t len, lineno = 0;
442 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
444 if (filename == NULL)
447 if ((config = fopen(filename, "r")) == NULL)
450 for (sc = ScreenCount(display);;) {
451 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
455 cp += (long)strspn(cp, SWM_CONF_WS);
461 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
463 cp += (long)strspn(cp, SWM_CONF_WS);
464 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
467 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
470 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
471 bar_enabled = atoi(val);
472 else if (!varmatch(var, "bar_border", &i))
473 setscreencolor(val, i, SWM_S_COLOR_BAR_BORDER);
474 else if (!varmatch(var, "bar_color", &i))
475 setscreencolor(val, i, SWM_S_COLOR_BAR);
476 else if (!varmatch(var, "bar_font_color", &i))
477 setscreencolor(val, i, SWM_S_COLOR_BAR_FONT);
478 else if (!strncmp(var, "bar_font", strlen("bar_font")))
479 asprintf(&bar_fonts[0], "%s", val);
480 else if (!strncmp(var, "bar_action", strlen("bar_action")))
481 asprintf(&bar_argv[0], "%s", val);
482 else if (!strncmp(var, "bar_delay", strlen("bar_delay")))
483 bar_delay = atoi(val);
489 if (!varmatch(var, "color_focus", &i))
490 setscreencolor(val, i, SWM_S_COLOR_FOCUS);
491 else if (!varmatch(var, "color_unfocus", &i))
492 setscreencolor(val, i, SWM_S_COLOR_UNFOCUS);
493 else if (!strncmp(var, "cycle_empty", strlen("cycle_empty")))
494 cycle_visible = atoi(val);
495 else if (!strncmp(var, "cycle_visible", strlen("cycle_visible")))
496 cycle_visible = atoi(val);
502 if (!strncmp(var, "dialog_ratio",
503 strlen("dialog_ratio"))) {
504 dialog_ratio = atof(val);
505 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
512 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
513 asprintf(&spawn_term[0], "%s", val);
514 if (!strncmp(var, "screenshot_enabled",
515 strlen("screenshot_enabled")))
516 ss_enabled = atoi(val);
517 if (!strncmp(var, "screenshot_app",
518 strlen("screenshot_app")))
519 asprintf(&spawn_screenshot[0], "%s", val);
531 errx(1, "invalid conf file entry: %s=%s", var, val);
535 socket_setnonblock(int fd)
539 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
540 err(1, "fcntl F_GETFL");
542 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
543 err(1, "fcntl F_SETFL");
547 bar_print(struct swm_region *r, char *s)
549 XClearWindow(display, r->bar_window);
550 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
551 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
560 bzero(bar_pipe, sizeof bar_pipe);
563 kill(bar_pid, SIGTERM);
566 strlcpy(bar_ext, "", sizeof bar_ext);
575 struct swm_region *r;
579 char loc[SWM_BAR_MAX];
582 if (bar_enabled == 0)
585 if (bar_extra && bar_extra_running) {
586 /* ignore short reads; it'll correct itself */
587 while ((b = fgetln(stdin, &len)) != NULL)
588 if (b && b[len - 1] == '\n') {
590 strlcpy(bar_ext, b, sizeof bar_ext);
592 if (b == NULL && errno != EAGAIN) {
593 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
594 errno, strerror(errno));
598 strlcpy(bar_ext, "", sizeof bar_ext);
601 localtime_r(&tmt, &tm);
602 strftime(s, sizeof s, "%a %b %d %R %Z %Y", &tm);
603 for (i = 0; i < ScreenCount(display); i++) {
605 TAILQ_FOREACH(r, &screens[i].rl, entry) {
606 snprintf(loc, sizeof loc, "%s %d:%d %s %s",
607 s, x++, r->ws->idx + 1, bar_ext, bar_vertext);
611 XSync(display, False);
622 bar_toggle(struct swm_region *r, union arg *args)
624 struct swm_region *tmpr;
625 int i, j, sc = ScreenCount(display);
627 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
630 for (i = 0; i < sc; i++)
631 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
632 XUnmapWindow(display, tmpr->bar_window);
634 for (i = 0; i < sc; i++)
635 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
636 XMapRaised(display, tmpr->bar_window);
638 bar_enabled = !bar_enabled;
639 XSync(display, False);
640 for (i = 0; i < sc; i++)
641 for (j = 0; j < SWM_WS_MAX; j++)
642 screens[i].ws[j].restack = 1;
645 /* must be after stack */
646 for (i = 0; i < sc; i++)
647 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
654 XSetWindowAttributes wa;
655 struct swm_region *r;
658 /* do this here because the conf file is in memory */
659 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
660 /* launch external status app */
661 bar_extra_running = 1;
662 if (pipe(bar_pipe) == -1)
663 err(1, "pipe error");
664 socket_setnonblock(bar_pipe[0]);
665 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
666 if (dup2(bar_pipe[0], 0) == -1)
668 if (dup2(bar_pipe[1], 1) == -1)
670 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
671 err(1, "could not disable SIGPIPE");
672 switch (bar_pid = fork()) {
674 err(1, "cannot fork");
678 execvp(bar_argv[0], bar_argv);
679 err(1, "%s external app failed", bar_argv[0]);
681 default: /* parent */
687 bzero(&wa, sizeof wa);
688 for (i = 0; i < ScreenCount(display); i++)
689 TAILQ_FOREACH(r, &screens[i].rl, entry) {
691 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
692 wa.background_pixel =
693 screens[i].c[SWM_S_COLOR_BAR].color;
694 XChangeWindowAttributes(display, r->bar_window,
695 CWBackPixel | CWBorderPixel, &wa);
701 bar_setup(struct swm_region *r)
705 for (i = 0; bar_fonts[i] != NULL; i++) {
706 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
712 if (bar_fonts[i] == NULL)
713 errx(1, "couldn't load font");
714 bar_height = bar_fs->ascent + bar_fs->descent + 3;
716 r->bar_window = XCreateSimpleWindow(display,
717 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
718 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
719 r->s->c[SWM_S_COLOR_BAR].color);
720 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
721 XSetFont(display, bar_gc, bar_fs->fid);
722 XSelectInput(display, r->bar_window, VisibilityChangeMask);
724 XMapRaised(display, r->bar_window);
725 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
727 if (signal(SIGALRM, bar_signal) == SIG_ERR)
728 err(1, "could not install bar_signal");
733 version(struct swm_region *r, union arg *args)
735 bar_version = !bar_version;
737 strlcpy(bar_vertext, cvstag, sizeof bar_vertext);
739 strlcpy(bar_vertext, "", sizeof bar_vertext);
744 config_win(struct ws_win *win)
748 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
749 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
750 ce.type = ConfigureNotify;
751 ce.display = display;
757 ce.height = win->g.h;
758 ce.border_width = 1; /* XXX store this! */
760 ce.override_redirect = False;
761 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
765 count_win(struct workspace *ws, int count_transient)
770 TAILQ_FOREACH(win, &ws->winlist, entry) {
771 if (count_transient == 0 && win->floating)
773 if (count_transient == 0 && win->transient)
777 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
783 quit(struct swm_region *r, union arg *args)
785 DNPRINTF(SWM_D_MISC, "quit\n");
795 for (i = 0; i < ScreenCount(display); i++)
796 for (j = 0; j < SWM_WS_MAX; j++)
797 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
798 XUnmapWindow(display, win->id);
802 restart(struct swm_region *r, union arg *args)
804 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
806 /* disable alarm because the following code may not be interrupted */
808 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
809 errx(1, "can't disable alarm");
814 XCloseDisplay(display);
815 execvp(start_argv[0], start_argv);
816 fprintf(stderr, "execvp failed\n");
822 root_to_region(Window root)
824 struct swm_region *r;
829 for (i = 0; i < ScreenCount(display); i++)
830 if (screens[i].root == root)
833 if (XQueryPointer(display, screens[i].root,
834 &rr, &cr, &x, &y, &wx, &wy, &mask) == False) {
835 /* if we can't query the pointer, grab the first region */
836 r = TAILQ_FIRST(&screens[i].rl);
838 /* otherwise, choose a region based on pointer location */
839 TAILQ_FOREACH(r, &screens[i].rl, entry) {
840 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
841 y >= Y(r) && y <= Y(r) + HEIGHT(r))
846 r = TAILQ_FIRST(&screens[i].rl);
852 find_window(Window id)
857 for (i = 0; i < ScreenCount(display); i++)
858 for (j = 0; j < SWM_WS_MAX; j++)
859 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
866 spawn(struct swm_region *r, union arg *args)
871 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
873 * The double-fork construct avoids zombie processes and keeps the code
874 * clean from stupid signal handlers.
879 close(ConnectionNumber(display));
880 setenv("LD_PRELOAD", SWM_LIB, 1);
881 if (asprintf(&ret, "%d", r->ws->idx)) {
882 setenv("_SWM_WS", ret, 1);
885 if (asprintf(&ret, "%d", getpid())) {
886 setenv("_SWM_PID", ret, 1);
890 /* kill stdin, mplayer, ssh-add etc. need that */
891 si = open("/dev/null", O_RDONLY, 0);
893 err(1, "open /dev/null");
894 if (dup2(si, 0) == -1)
895 err(1, "dup2 /dev/null");
896 execvp(args->argv[0], args->argv);
897 fprintf(stderr, "execvp failed\n");
906 spawnmenu(struct swm_region *r, union arg *args)
908 DNPRINTF(SWM_D_MISC, "spawnmenu\n");
910 spawn_menu[SWM_MENU_FN] = bar_fonts[bar_fidx];
911 spawn_menu[SWM_MENU_NB] = r->s->c[SWM_S_COLOR_BAR].name;
912 spawn_menu[SWM_MENU_NF] = r->s->c[SWM_S_COLOR_BAR_FONT].name;
913 spawn_menu[SWM_MENU_SB] = r->s->c[SWM_S_COLOR_BAR_BORDER].name;
914 spawn_menu[SWM_MENU_SF] = r->s->c[SWM_S_COLOR_BAR].name;
925 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
927 for (i = 0; i < ScreenCount(display); i++)
928 for (j = 0; j < SWM_WS_MAX; j++)
929 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
930 if (win->ws->r == NULL)
933 XSetWindowBorder(display, win->id,
934 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
936 win->ws->focus = NULL;
942 focus_win(struct ws_win *win)
944 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
950 win->ws->focus = win;
951 if (win->ws->r != NULL) {
953 if (!win->got_focus) {
954 XSetWindowBorder(display, win->id,
955 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
959 XSetInputFocus(display, win->id,
960 RevertToPointerRoot, CurrentTime);
965 switchws(struct swm_region *r, union arg *args)
968 struct swm_region *this_r, *other_r;
970 struct workspace *new_ws, *old_ws;
974 new_ws = &this_r->s->ws[wsid];
976 DNPRINTF(SWM_D_WS, "switchws screen %d region %dx%d+%d+%d: "
977 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
980 if (new_ws == old_ws)
985 /* if the other workspace is hidden, switch windows */
986 /* map new window first to prevent ugly blinking */
987 TAILQ_FOREACH(win, &new_ws->winlist, entry)
988 XMapRaised(display, win->id);
990 TAILQ_FOREACH(win, &old_ws->winlist, entry)
991 XUnmapWindow(display, win->id);
995 other_r->ws = old_ws;
1004 focus_win(new_ws->focus);
1010 cyclews(struct swm_region *r, union arg *args)
1013 struct swm_screen *s = r->s;
1015 DNPRINTF(SWM_D_WS, "cyclews id %d "
1016 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1017 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1022 case SWM_ARG_ID_CYCLEWS_UP:
1023 if (a.id < SWM_WS_MAX - 1)
1028 case SWM_ARG_ID_CYCLEWS_DOWN:
1032 a.id = SWM_WS_MAX - 1;
1038 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1040 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1044 } while (a.id != r->ws->idx);
1048 cyclescr(struct swm_region *r, union arg *args)
1050 struct swm_region *rr;
1055 case SWM_ARG_ID_CYCLESC_UP:
1056 rr = TAILQ_NEXT(r, entry);
1058 rr = TAILQ_FIRST(&screens[i].rl);
1060 case SWM_ARG_ID_CYCLESC_DOWN:
1061 rr = TAILQ_PREV(r, swm_region_list, entry);
1063 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1069 XSetInputFocus(display, PointerRoot, RevertToPointerRoot, CurrentTime);
1070 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, rr->g.x,
1071 rr->g.y + bar_enabled ? bar_height : 0);
1075 swapwin(struct swm_region *r, union arg *args)
1077 struct ws_win *target;
1078 struct ws_win_list *wl;
1081 DNPRINTF(SWM_D_WS, "swapwin id %d "
1082 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1083 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1084 if (cur_focus == NULL)
1087 wl = &cur_focus->ws->winlist;
1090 case SWM_ARG_ID_SWAPPREV:
1091 target = TAILQ_PREV(cur_focus, ws_win_list, entry);
1092 TAILQ_REMOVE(wl, cur_focus, entry);
1094 TAILQ_INSERT_TAIL(wl, cur_focus, entry);
1096 TAILQ_INSERT_BEFORE(target, cur_focus, entry);
1098 case SWM_ARG_ID_SWAPNEXT:
1099 target = TAILQ_NEXT(cur_focus, entry);
1100 TAILQ_REMOVE(wl, cur_focus, entry);
1102 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
1104 TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
1106 case SWM_ARG_ID_SWAPMAIN:
1107 target = TAILQ_FIRST(wl);
1108 if (target == cur_focus)
1110 TAILQ_REMOVE(wl, target, entry);
1111 TAILQ_INSERT_BEFORE(cur_focus, target, entry);
1112 TAILQ_REMOVE(wl, cur_focus, entry);
1113 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
1116 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1125 focus(struct swm_region *r, union arg *args)
1127 struct ws_win *winfocus, *winlostfocus;
1128 struct ws_win_list *wl;
1130 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1131 if (cur_focus == NULL)
1134 wl = &cur_focus->ws->winlist;
1136 winlostfocus = cur_focus;
1139 case SWM_ARG_ID_FOCUSPREV:
1140 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1141 if (winfocus == NULL)
1142 winfocus = TAILQ_LAST(wl, ws_win_list);
1145 case SWM_ARG_ID_FOCUSNEXT:
1146 winfocus = TAILQ_NEXT(cur_focus, entry);
1147 if (winfocus == NULL)
1148 winfocus = TAILQ_FIRST(wl);
1151 case SWM_ARG_ID_FOCUSMAIN:
1152 winfocus = TAILQ_FIRST(wl);
1159 if (winfocus == winlostfocus || winfocus == NULL)
1162 XMapRaised(display, winfocus->id);
1163 focus_win(winfocus);
1164 XSync(display, False);
1168 cycle_layout(struct swm_region *r, union arg *args)
1170 struct workspace *ws = r->ws;
1172 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1175 if (ws->cur_layout->l_stack == NULL)
1176 ws->cur_layout = &layouts[0];
1182 stack_config(struct swm_region *r, union arg *args)
1184 struct workspace *ws = r->ws;
1186 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1189 if (ws->cur_layout->l_config != NULL)
1190 ws->cur_layout->l_config(ws, args->id);
1192 if (args->id != SWM_ARG_ID_STACKINIT);
1198 struct swm_geometry g;
1199 struct swm_region *r;
1202 DNPRINTF(SWM_D_STACK, "stack\n");
1204 for (i = 0; i < ScreenCount(display); i++) {
1206 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1207 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1208 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1210 /* start with screen geometry, adjust for bar */
1220 r->ws->cur_layout->l_stack(r->ws, &g);
1223 XSync(display, False);
1227 stack_floater(struct ws_win *win, struct swm_region *r)
1232 bzero(&wc, sizeof wc);
1233 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1234 wc.border_width = 1;
1235 if (win->transient) {
1236 win->g.w = (double)WIDTH(r) * dialog_ratio;
1237 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1239 wc.width = win->g.w;
1240 wc.height = win->g.h;
1241 wc.x = (WIDTH(r) - win->g.w) / 2;
1242 wc.y = (HEIGHT(r) - win->g.h) / 2;
1244 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1245 win->id, wc.x, wc.y, wc.width, wc.height);
1247 XConfigureWindow(display, win->id, mask, &wc);
1250 #define SWAPXY(g) do { \
1252 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1253 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1256 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1259 struct swm_geometry win_g, r_g = *g;
1260 struct ws_win *win, *winfocus;
1261 int i, j, w_inc, h_inc, w_base, h_base;
1262 int hrh, extra, h_slice, last_h = 0;
1263 int split, colno, winno, mwin, msize, mscale;
1264 int remain, missing, v_slice;;
1267 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1268 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1270 if ((winno = count_win(ws, 0)) == 0)
1273 if (ws->focus == NULL)
1274 ws->focus = TAILQ_FIRST(&ws->winlist);
1275 winfocus = cur_focus ? cur_focus : ws->focus;
1277 win = TAILQ_FIRST(&ws->winlist);
1279 w_inc = win->sh.width_inc;
1280 w_base = win->sh.base_width;
1281 mwin = ws->l_state.horizontal_mwin;
1282 mscale = ws->l_state.horizontal_msize;
1285 w_inc = win->sh.height_inc;
1286 w_base = win->sh.base_height;
1287 mwin = ws->l_state.vertical_mwin;
1288 mscale = ws->l_state.vertical_msize;
1292 h_slice = r_g.h / SWM_H_SLICE;
1293 if (mwin && winno > mwin) {
1294 v_slice = r_g.w / SWM_V_SLICE;
1298 msize = v_slice * mscale;
1300 if (w_inc > 1 && w_inc < v_slice) {
1301 /* adjust for window's requested size increment */
1302 remain = (win_g.w - w_base) % w_inc;
1303 missing = w_inc - remain;
1305 if (missing <= extra || j == 0) {
1316 win_g.x += r_g.w - msize;
1321 hrh = r_g.h / colno;
1322 extra = r_g.h - (colno * hrh);
1326 TAILQ_FOREACH(win, &ws->winlist, entry) {
1327 if (split && i == split) {
1328 colno = winno - split;
1329 hrh = (r_g.h / colno);
1330 extra = r_g.h - (colno * hrh);
1334 win_g.x += msize + 2;
1335 win_g.w = r_g.w - (msize + 2);
1340 h_inc = win->sh.width_inc;
1341 h_base = win->sh.base_width;
1343 h_inc = win->sh.height_inc;
1344 h_base = win->sh.base_height;
1346 if (j == colno - 1) {
1347 win_g.h = hrh + extra;
1348 } else if (h_inc > 1 && h_inc < h_slice) {
1349 /* adjust for window's requested size increment */
1350 remain = (win_g.h - h_base) % h_inc;
1351 missing = h_inc - remain;
1353 if (missing <= extra || j == 0) {
1365 win_g.y += last_h + 2;
1367 if (win->transient != 0 || win->floating != 0)
1368 stack_floater(win, ws->r);
1370 bzero(&wc, sizeof wc);
1371 wc.border_width = 1;
1373 win->g.x = wc.x = win_g.y;
1374 win->g.y = wc.y = win_g.x;
1375 win->g.w = wc.width = win_g.h;
1376 win->g.h = wc.height = win_g.w;
1378 win->g.x = wc.x = win_g.x;
1379 win->g.y = wc.y = win_g.y;
1380 win->g.w = wc.width = win_g.w;
1381 win->g.h = wc.height = win_g.h;
1383 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1384 XConfigureWindow(display, win->id, mask, &wc);
1386 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);
1390 XMapRaised(display, win->id);
1397 focus_win(winfocus); /* has to be done outside of the loop */
1401 vertical_config(struct workspace *ws, int id)
1403 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1406 case SWM_ARG_ID_STACKRESET:
1407 case SWM_ARG_ID_STACKINIT:
1408 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1409 ws->l_state.vertical_mwin = 1;
1411 case SWM_ARG_ID_MASTERSHRINK:
1412 if (ws->l_state.vertical_msize > 1)
1413 ws->l_state.vertical_msize--;
1415 case SWM_ARG_ID_MASTERGROW:
1416 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1417 ws->l_state.vertical_msize++;
1419 case SWM_ARG_ID_MASTERADD:
1420 ws->l_state.vertical_mwin++;
1422 case SWM_ARG_ID_MASTERDEL:
1423 if (ws->l_state.vertical_mwin > 0)
1424 ws->l_state.vertical_mwin--;
1432 vertical_stack(struct workspace *ws, struct swm_geometry *g)
1434 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1436 stack_master(ws, g, 0, 0);
1440 horizontal_config(struct workspace *ws, int id)
1442 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1445 case SWM_ARG_ID_STACKRESET:
1446 case SWM_ARG_ID_STACKINIT:
1447 ws->l_state.horizontal_mwin = 1;
1448 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1450 case SWM_ARG_ID_MASTERSHRINK:
1451 if (ws->l_state.horizontal_msize > 1)
1452 ws->l_state.horizontal_msize--;
1454 case SWM_ARG_ID_MASTERGROW:
1455 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1456 ws->l_state.horizontal_msize++;
1458 case SWM_ARG_ID_MASTERADD:
1459 ws->l_state.horizontal_mwin++;
1461 case SWM_ARG_ID_MASTERDEL:
1462 if (ws->l_state.horizontal_mwin > 0)
1463 ws->l_state.horizontal_mwin--;
1471 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
1473 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1475 stack_master(ws, g, 1, 0);
1478 /* fullscreen view */
1480 max_stack(struct workspace *ws, struct swm_geometry *g) {
1482 struct swm_geometry gg = *g;
1483 struct ws_win *win, *winfocus;
1486 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1488 if (count_win(ws, 0) == 0)
1491 if (ws->focus == NULL)
1492 ws->focus = TAILQ_FIRST(&ws->winlist);
1493 winfocus = cur_focus ? cur_focus : ws->focus;
1495 TAILQ_FOREACH(win, &ws->winlist, entry) {
1496 if (win->transient != 0 || win->floating != 0) {
1497 if (win == ws->focus) {
1499 stack_floater(win, ws->r);
1500 XMapRaised(display, win->id);
1502 XUnmapWindow(display, win->id);
1504 bzero(&wc, sizeof wc);
1505 wc.border_width = 1;
1506 win->g.x = wc.x = gg.x;
1507 win->g.y = wc.y = gg.y;
1508 win->g.w = wc.width = gg.w;
1509 win->g.h = wc.height = gg.h;
1510 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1511 XConfigureWindow(display, win->id, mask, &wc);
1513 if (win == ws->focus) {
1514 XMapRaised(display, win->id);
1516 XUnmapWindow(display, win->id);
1521 focus_win(winfocus); /* has to be done outside of the loop */
1525 send_to_ws(struct swm_region *r, union arg *args)
1527 int wsid = args->id;
1528 struct ws_win *win = cur_focus;
1529 struct workspace *ws, *nws;
1530 Atom ws_idx_atom = 0;
1531 unsigned char ws_idx_str[SWM_PROPLEN];
1536 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1539 nws = &win->s->ws[wsid];
1541 XUnmapWindow(display, win->id);
1543 /* find a window to focus */
1544 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1545 if (ws->focus == NULL)
1546 ws->focus = TAILQ_FIRST(&ws->winlist);
1547 if (ws->focus == win)
1550 TAILQ_REMOVE(&ws->winlist, win, entry);
1552 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1555 /* Try to update the window's workspace property */
1556 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1558 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
1559 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1561 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1562 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1565 if (count_win(nws, 1) == 1)
1574 wkill(struct swm_region *r, union arg *args)
1576 DNPRINTF(SWM_D_MISC, "wkill\n");
1577 if(r->ws->focus != NULL)
1578 XKillClient(display, r->ws->focus->id);
1582 screenshot(struct swm_region *r, union arg *args)
1586 DNPRINTF(SWM_D_MISC, "screenshot\n");
1588 if (ss_enabled == 0)
1592 case SWM_ARG_ID_SS_ALL:
1593 spawn_screenshot[1] = "full";
1595 case SWM_ARG_ID_SS_WINDOW:
1596 spawn_screenshot[1] = "window";
1601 a.argv = spawn_screenshot;
1605 /* key definitions */
1609 void (*func)(struct swm_region *r, union arg *);
1612 /* modifier key function argument */
1613 { MODKEY, XK_space, cycle_layout, {0} },
1614 { MODKEY | ShiftMask, XK_space, stack_config, {.id = SWM_ARG_ID_STACKRESET} },
1615 { MODKEY, XK_h, stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
1616 { MODKEY, XK_l, stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
1617 { MODKEY, XK_comma, stack_config, {.id = SWM_ARG_ID_MASTERADD} },
1618 { MODKEY, XK_period, stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
1619 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1620 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1621 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1622 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1623 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1624 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1625 { MODKEY, XK_p, spawnmenu, {.argv = spawn_menu} },
1626 { MODKEY | ShiftMask, XK_q, quit, {0} },
1627 { MODKEY, XK_q, restart, {0} },
1628 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1629 { MODKEY, XK_1, switchws, {.id = 0} },
1630 { MODKEY, XK_2, switchws, {.id = 1} },
1631 { MODKEY, XK_3, switchws, {.id = 2} },
1632 { MODKEY, XK_4, switchws, {.id = 3} },
1633 { MODKEY, XK_5, switchws, {.id = 4} },
1634 { MODKEY, XK_6, switchws, {.id = 5} },
1635 { MODKEY, XK_7, switchws, {.id = 6} },
1636 { MODKEY, XK_8, switchws, {.id = 7} },
1637 { MODKEY, XK_9, switchws, {.id = 8} },
1638 { MODKEY, XK_0, switchws, {.id = 9} },
1639 { MODKEY, XK_Right, cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
1640 { MODKEY, XK_Left, cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
1641 { MODKEY | ShiftMask, XK_Right, cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
1642 { MODKEY | ShiftMask, XK_Left, cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
1643 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1644 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1645 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1646 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1647 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1648 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1649 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1650 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1651 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1652 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1653 { MODKEY, XK_b, bar_toggle, {0} },
1654 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1655 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1656 { MODKEY | ShiftMask, XK_x, wkill, {0} },
1657 { MODKEY, XK_s, screenshot, {.id = SWM_ARG_ID_SS_ALL} },
1658 { MODKEY | ShiftMask, XK_s, screenshot, {.id = SWM_ARG_ID_SS_WINDOW} },
1659 { MODKEY | ShiftMask, XK_v, version, {0} },
1663 resize_window(struct ws_win *win, int center)
1667 struct swm_region *r;
1669 r = root_to_region(win->wa.root);
1670 bzero(&wc, sizeof wc);
1671 mask = CWBorderWidth | CWWidth | CWHeight;
1672 wc.border_width = 1;
1673 wc.width = win->g.w;
1674 wc.height = win->g.h;
1675 if (center == SWM_ARG_ID_CENTER) {
1676 wc.x = (WIDTH(r) - win->g.w) / 2;
1677 wc.y = (HEIGHT(r) - win->g.h) / 2;
1681 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
1682 win->id, wc.x, wc.y, wc.width, wc.height);
1684 XConfigureWindow(display, win->id, mask, &wc);
1689 resize(struct ws_win *win, union arg *args)
1693 DNPRINTF(SWM_D_MOUSE, "resize: win %d floating %d trans %d\n",
1694 win->id, win->floating, win->transient);
1696 if (!(win->transient != 0 || win->floating != 0))
1699 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
1700 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
1702 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w, win->g.h);
1704 XMaskEvent(display, MOUSEMASK | ExposureMask |
1705 SubstructureRedirectMask, &ev);
1707 case ConfigureRequest:
1710 handler[ev.type](&ev);
1713 XSync(display, False);
1714 if (ev.xmotion.x < 0)
1716 if (ev.xmotion.y < 0)
1718 win->g.w = ev.xmotion.x;
1719 win->g.h = ev.xmotion.y;
1720 resize_window(win, args->id);
1723 } while (ev.type != ButtonRelease);
1724 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
1726 XUngrabPointer(display, CurrentTime);
1729 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
1733 move_window(struct ws_win *win)
1737 struct swm_region *r;
1739 r = root_to_region(win->wa.root);
1740 bzero(&wc, sizeof wc);
1745 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
1746 win->id, wc.x, wc.y, wc.width, wc.height);
1748 XConfigureWindow(display, win->id, mask, &wc);
1753 move(struct ws_win *win, union arg *args)
1757 DNPRINTF(SWM_D_MOUSE, "move: win %d floating %d trans %d\n",
1758 win->id, win->floating, win->transient);
1760 if (!(win->transient != 0 || win->floating != 0))
1763 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
1764 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
1766 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
1768 XMaskEvent(display, MOUSEMASK | ExposureMask |
1769 SubstructureRedirectMask, &ev);
1771 case ConfigureRequest:
1774 handler[ev.type](&ev);
1777 XSync(display, False);
1778 win->g.x = ev.xmotion.x_root;
1779 win->g.y = ev.xmotion.y_root;
1783 } while (ev.type != ButtonRelease);
1784 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
1785 XUngrabPointer(display, CurrentTime);
1788 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
1792 enum { client_click, root_click };
1794 unsigned int action;
1796 unsigned int button;
1797 void (*func)(struct ws_win *, union arg *);
1800 /* action key mouse button func args */
1801 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
1802 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
1803 { client_click, MODKEY, Button1, move, {0} },
1807 updatenumlockmask(void)
1810 XModifierKeymap *modmap;
1812 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1814 modmap = XGetModifierMapping(display);
1815 for (i = 0; i < 8; i++)
1816 for (j = 0; j < modmap->max_keypermod; j++)
1817 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1818 == XKeysymToKeycode(display, XK_Num_Lock))
1819 numlockmask = (1 << i);
1821 XFreeModifiermap(modmap);
1827 unsigned int i, j, k;
1829 unsigned int modifiers[] =
1830 { 0, LockMask, numlockmask, numlockmask | LockMask };
1832 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1833 updatenumlockmask();
1835 for (k = 0; k < ScreenCount(display); k++) {
1836 if (TAILQ_EMPTY(&screens[k].rl))
1838 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
1839 for (i = 0; i < LENGTH(keys); i++) {
1840 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
1841 for (j = 0; j < LENGTH(modifiers); j++)
1842 XGrabKey(display, code,
1843 keys[i].mod | modifiers[j],
1844 screens[k].root, True,
1845 GrabModeAsync, GrabModeAsync);
1851 grabbuttons(struct ws_win *win, int focused)
1854 unsigned int modifiers[] =
1855 { 0, LockMask, numlockmask, numlockmask|LockMask };
1857 updatenumlockmask();
1858 XUngrabButton(display, AnyButton, AnyModifier, win->id);
1860 for (i = 0; i < LENGTH(buttons); i++)
1861 if (buttons[i].action == client_click)
1862 for (j = 0; j < LENGTH(modifiers); j++)
1863 XGrabButton(display, buttons[i].button,
1864 buttons[i].mask | modifiers[j],
1865 win->id, False, BUTTONMASK,
1866 GrabModeAsync, GrabModeSync, None,
1869 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
1870 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1876 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1884 XKeyEvent *ev = &e->xkey;
1886 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1888 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1889 for (i = 0; i < LENGTH(keys); i++)
1890 if (keysym == keys[i].keysym
1891 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1893 keys[i].func(root_to_region(ev->root),
1898 buttonpress(XEvent *e)
1900 XButtonPressedEvent *ev = &e->xbutton;
1905 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1907 action = root_click;
1908 if ((win = find_window(ev->window)) == NULL)
1912 action = client_click;
1915 for (i = 0; i < LENGTH(buttons); i++)
1916 if (action == buttons[i].action && buttons[i].func &&
1917 buttons[i].button == ev->button &&
1918 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
1919 buttons[i].func(win, &buttons[i].args);
1923 set_win_state(struct ws_win *win, long state)
1925 long data[] = {state, None};
1927 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1929 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1930 (unsigned char *)data, 2);
1934 manage_window(Window id)
1937 struct workspace *ws;
1939 int format, i, ws_idx;
1940 unsigned long nitems, bytes;
1941 Atom ws_idx_atom = 0, type;
1942 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
1943 struct swm_region *r;
1947 if ((win = find_window(id)) != NULL)
1948 return (win); /* already being managed */
1950 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1951 errx(1, "calloc: failed to allocate memory for new window");
1953 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1955 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
1956 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
1958 XGetWindowAttributes(display, id, &win->wa);
1959 r = root_to_region(win->wa.root);
1960 /* If the window was managed before, put it in the same workspace */
1962 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
1963 ws_idx = strtonum(prop, 0, 9, &errstr);
1965 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
1967 ws = &r->s->ws[ws_idx];
1973 win->s = r->s; /* this never changes */
1974 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
1976 XGetTransientForHint(display, win->id, &trans);
1978 win->transient = trans;
1979 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1980 (unsigned)win->id, win->transient);
1982 win->g.w = win->wa.width;
1983 win->g.h = win->wa.height;
1984 win->g.x = win->wa.x;
1985 win->g.y = win->wa.y;
1987 XGetWMNormalHints(display, win->id, &win->sh, &mask); /* XXX function? */
1989 if (ws_idx_atom && prop == NULL &&
1990 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
1991 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1993 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1994 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1999 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);
2002 if (XGetClassHint(display, win->id, &win->ch)) {
2003 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
2004 win->ch.res_class, win->ch.res_name);
2005 for (i = 0; quirks[i].class != NULL && quirks[i].name != NULL &&
2006 quirks[i].quirk != 0; i++){
2007 if (!strcmp(win->ch.res_class, quirks[i].class) &&
2008 !strcmp(win->ch.res_name, quirks[i].name)) {
2009 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
2010 win->ch.res_class, win->ch.res_name);
2011 if (quirks[i].quirk & SWM_Q_FLOAT)
2017 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
2018 PropertyChangeMask | StructureNotifyMask);
2020 set_win_state(win, NormalState);
2022 /* make new win focused */
2029 configurerequest(XEvent *e)
2031 XConfigureRequestEvent *ev = &e->xconfigurerequest;
2036 if ((win = find_window(ev->window)) == NULL)
2040 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
2043 fprintf(stderr, "configurerequest: new window: %lu x %d y %d w %d h %d bw %d s %d sm %d\n",
2044 ev->window, ev->x, ev->y, ev->width, ev->height, ev->border_width, ev->above, ev->detail);
2046 bzero(&wc, sizeof wc);
2049 wc.width = ev->width;
2050 wc.height = ev->height;
2051 wc.border_width = ev->border_width;
2052 wc.sibling = ev->above;
2053 wc.stack_mode = ev->detail;
2054 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
2057 fprintf(stderr, "configurerequest: change window: %lu\n",
2060 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
2062 if (win->floating) {
2063 if (ev->value_mask & CWX)
2065 if (ev->value_mask & CWY)
2067 if (ev->value_mask & CWWidth)
2068 win->g.w = ev->width;
2069 if (ev->value_mask & CWHeight)
2070 win->g.h = ev->height;
2071 if (win->ws->r != NULL) {
2072 /* this seems to be full screen */
2073 if (win->g.w >= WIDTH(win->ws->r)) {
2075 win->g.w = WIDTH(win->ws->r);
2076 ev->value_mask |= CWX | CWWidth;
2078 if (win->g.h >= HEIGHT(win->ws->r)) {
2081 win->g.h = HEIGHT(win->ws->r);
2082 ev->value_mask |= CWY | CWHeight;
2085 if ((ev->value_mask & (CWX|CWY)) &&
2086 !(ev->value_mask & (CWWidth|CWHeight)))
2088 XMoveResizeWindow(display, win->id,
2089 win->g.x, win->g.y, win->g.w, win->g.h);
2096 configurenotify(XEvent *e)
2098 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
2099 e->xconfigure.window);
2103 destroynotify(XEvent *e)
2106 XDestroyWindowEvent *ev = &e->xdestroywindow;
2107 struct workspace *ws;
2109 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
2111 if ((win = find_window(ev->window)) != NULL) {
2113 /* find a window to focus */
2114 if (ws->focus == win)
2115 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
2116 if (ws->focus == NULL)
2117 ws->focus = TAILQ_FIRST(&ws->winlist);
2118 if (ws->focus == NULL || ws->focus == win) {
2122 focus_win(ws->focus);
2123 TAILQ_REMOVE(&ws->winlist, win, entry);
2124 set_win_state(win, WithdrawnState);
2125 if (win->ch.res_class)
2126 XFree(win->ch.res_class);
2127 if (win->ch.res_name)
2128 XFree(win->ch.res_name);
2135 enternotify(XEvent *e)
2137 XCrossingEvent *ev = &e->xcrossing;
2140 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
2143 /* eat event(r) to prevent autofocus */
2148 if ((win = find_window(ev->window)) != NULL)
2155 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
2159 mappingnotify(XEvent *e)
2161 XMappingEvent *ev = &e->xmapping;
2163 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
2165 XRefreshKeyboardMapping(ev);
2166 if (ev->request == MappingKeyboard)
2171 maprequest(XEvent *e)
2173 XMapRequestEvent *ev = &e->xmaprequest;
2174 XWindowAttributes wa;
2176 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
2177 e->xmaprequest.window);
2179 if (!XGetWindowAttributes(display, ev->window, &wa))
2181 if (wa.override_redirect)
2183 manage_window(e->xmaprequest.window);
2188 propertynotify(XEvent *e)
2191 XPropertyEvent *ev = &e->xproperty;
2193 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
2196 if (ev->state == PropertyDelete)
2197 return; /* ignore */
2198 win = find_window(ev->window);
2203 case XA_WM_NORMAL_HINTS:
2206 XGetWMNormalHints(display, win->id, &win->sh, &mask);
2207 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
2208 if (win->sh.flags & PMinSize) {
2209 win->g.w = win->sh.min_width;
2210 win->g.h = win->sh.min_height;
2211 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
2213 XMoveResizeWindow(display, win->id,
2214 win->g.x, win->g.y, win->g.w, win->g.h);
2223 unmapnotify(XEvent *e)
2225 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
2229 visibilitynotify(XEvent *e)
2232 struct swm_region *r;
2234 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
2235 e->xvisibility.window);
2236 if (e->xvisibility.state == VisibilityUnobscured)
2237 for (i = 0; i < ScreenCount(display); i++)
2238 TAILQ_FOREACH(r, &screens[i].rl, entry)
2239 if (e->xvisibility.window == r->bar_window)
2244 xerror_start(Display *d, XErrorEvent *ee)
2251 xerror(Display *d, XErrorEvent *ee)
2253 /* fprintf(stderr, "error: %p %p\n", display, ee); */
2261 xerrorxlib = XSetErrorHandler(xerror_start);
2263 /* this causes an error if some other window manager is running */
2264 XSelectInput(display, DefaultRootWindow(display),
2265 SubstructureRedirectMask);
2266 XSync(display, False);
2270 XSetErrorHandler(xerror);
2271 XSync(display, False);
2280 unsigned char *p = NULL;
2281 unsigned long n, extra;
2284 astate = XInternAtom(display, "WM_STATE", False);
2285 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
2286 &real, &format, &n, &extra, (unsigned char **)&p);
2287 if (status != Success)
2296 new_region(struct swm_screen *s, struct workspace *ws,
2297 int x, int y, int w, int h)
2299 struct swm_region *r;
2301 DNPRINTF(SWM_D_MISC, "new region on screen %d: %dx%d (%d, %d)\n",
2302 s->idx, x, y, w, h);
2304 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
2305 errx(1, "calloc: failed to allocate memory for screen");
2314 TAILQ_INSERT_TAIL(&s->rl, r, entry);
2321 #ifdef SWM_XRR_HAS_CRTC
2323 XRRScreenResources *sr;
2325 #endif /* SWM_XRR_HAS_CRTC */
2326 Window d1, d2, *wins = NULL;
2327 XWindowAttributes wa;
2328 struct swm_region *r;
2330 int errorbase, major, minor;
2331 int ncrtc = 0, w = 0;
2333 struct workspace *ws;
2337 if ((screens = calloc(ScreenCount(display),
2338 sizeof(struct swm_screen))) == NULL)
2339 errx(1, "calloc: screens");
2341 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2344 /* map physical screens */
2345 for (i = 0; i < ScreenCount(display); i++) {
2346 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
2348 TAILQ_INIT(&screens[i].rl);
2349 screens[i].root = RootWindow(display, i);
2351 /* set default colors */
2352 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
2353 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
2354 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
2355 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
2356 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
2358 /* init all workspaces */
2359 for (j = 0; j < SWM_WS_MAX; j++) {
2360 ws = &screens[i].ws[j];
2365 TAILQ_INIT(&ws->winlist);
2367 for (k = 0; layouts[k].l_stack != NULL; k++)
2368 if (layouts[k].l_config != NULL)
2369 layouts[k].l_config(ws,
2370 SWM_ARG_ID_STACKINIT);
2371 ws->cur_layout = &layouts[0];
2374 /* map virtual screens onto physical screens */
2375 screens[i].xrandr_support = XRRQueryExtension(display,
2376 &xrandr_eventbase, &errorbase);
2377 if (screens[i].xrandr_support)
2378 if (XRRQueryVersion(display, &major, &minor) &&
2380 screens[i].xrandr_support = 0;
2382 #if 0 /* not ready for dynamic screen changes */
2383 if (screens[i].xrandr_support)
2384 XRRSelectInput(display,
2386 RRScreenChangeNotifyMask);
2389 /* grab existing windows (before we build the bars)*/
2390 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
2393 #ifdef SWM_XRR_HAS_CRTC
2394 sr = XRRGetScreenResources(display, screens[i].root);
2396 new_region(&screens[i], &screens[i].ws[w],
2397 0, 0, DisplayWidth(display, i),
2398 DisplayHeight(display, i));
2402 for (c = 0, ci = NULL; c < ncrtc; c++) {
2403 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
2404 if (ci->noutput == 0)
2407 if (ci != NULL && ci->mode == None)
2408 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2409 DisplayWidth(display, i),
2410 DisplayHeight(display, i));
2412 new_region(&screens[i], &screens[i].ws[w],
2413 ci->x, ci->y, ci->width, ci->height);
2417 XRRFreeCrtcInfo(ci);
2418 XRRFreeScreenResources(sr);
2420 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2421 DisplayWidth(display, i),
2422 DisplayHeight(display, i));
2423 #endif /* SWM_XRR_HAS_CRTC */
2425 /* attach windows to a region */
2426 /* normal windows */
2427 if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
2428 errx(1, "no regions on screen %d", i);
2430 for (i = 0; i < no; i++) {
2431 XGetWindowAttributes(display, wins[i], &wa);
2432 if (!XGetWindowAttributes(display, wins[i], &wa) ||
2433 wa.override_redirect ||
2434 XGetTransientForHint(display, wins[i], &d1))
2437 if (wa.map_state == IsViewable ||
2438 getstate(wins[i]) == NormalState)
2439 manage_window(wins[i]);
2441 /* transient windows */
2442 for (i = 0; i < no; i++) {
2443 if (!XGetWindowAttributes(display, wins[i], &wa))
2446 if (XGetTransientForHint(display, wins[i], &d1) &&
2447 (wa.map_state == IsViewable || getstate(wins[i]) ==
2449 manage_window(wins[i]);
2459 main(int argc, char *argv[])
2462 char conf[PATH_MAX], *cfile = NULL;
2469 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
2470 SWM_VERSION, cvstag);
2471 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2472 warnx("no locale support");
2474 if (!(display = XOpenDisplay(0)))
2475 errx(1, "can not open display");
2478 errx(1, "other wm running");
2480 astate = XInternAtom(display, "WM_STATE", False);
2482 /* look for local and global conf file */
2483 pwd = getpwuid(getuid());
2485 errx(1, "invalid user %d", getuid());
2489 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
2490 if (stat(conf, &sb) != -1) {
2491 if (S_ISREG(sb.st_mode))
2494 /* try global conf file */
2495 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
2496 if (!stat(conf, &sb))
2497 if (S_ISREG(sb.st_mode))
2504 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2509 xfd = ConnectionNumber(display);
2513 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
2515 errx(1, "select failed");
2520 while(XPending(display)) {
2521 XNextEvent(display, &e);
2522 if (handler[e.type])
2523 handler[e.type](&e);
2527 XCloseDisplay(display);