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 sig_atomic_t bar_alarm = 0;
165 int bar_extra_running = 0;
173 char *bar_fonts[] = {
174 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
175 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
179 /* terminal + args */
180 char *spawn_term[] = { "xterm", NULL };
181 char *spawn_screenshot[] = { "screenshot.sh", NULL, NULL };
182 char *spawn_menu[] = { "dmenu_run", "-fn", NULL, "-nb", NULL,
183 "-nf", NULL, "-sb", NULL, "-sf", NULL, NULL };
185 #define SWM_MENU_FN (2)
186 #define SWM_MENU_NB (4)
187 #define SWM_MENU_NF (6)
188 #define SWM_MENU_SB (8)
189 #define SWM_MENU_SF (10)
191 /* layout manager data */
192 struct swm_geometry {
202 /* virtual "screens" */
204 TAILQ_ENTRY(swm_region) entry;
205 struct swm_geometry g;
207 struct workspace *ws; /* current workspace on this region */
208 struct swm_screen *s; /* screen idx */
210 TAILQ_HEAD(swm_region_list, swm_region);
213 TAILQ_ENTRY(ws_win) entry;
215 struct swm_geometry g;
219 struct workspace *ws; /* always valid */
220 struct swm_screen *s; /* always valid, never changes */
221 XWindowAttributes wa;
225 TAILQ_HEAD(ws_win_list, ws_win);
227 /* layout handlers */
229 void vertical_config(struct workspace *, int);
230 void vertical_stack(struct workspace *, struct swm_geometry *);
231 void horizontal_config(struct workspace *, int);
232 void horizontal_stack(struct workspace *, struct swm_geometry *);
233 void max_stack(struct workspace *, struct swm_geometry *);
235 void grabbuttons(struct ws_win *, int);
238 void (*l_stack)(struct workspace *, struct swm_geometry *);
239 void (*l_config)(struct workspace *, int);
241 /* stack, configure */
242 { vertical_stack, vertical_config},
243 { horizontal_stack, horizontal_config},
248 #define SWM_H_SLICE (32)
249 #define SWM_V_SLICE (32)
251 /* define work spaces */
253 int idx; /* workspace index */
254 int restack; /* restack on switch */
255 struct layout *cur_layout; /* current layout handlers */
256 struct ws_win *focus; /* may be NULL */
257 struct swm_region *r; /* may be NULL */
258 struct ws_win_list winlist; /* list of windows in ws */
262 int horizontal_msize;
269 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
270 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
272 /* physical screen mapping */
273 #define SWM_WS_MAX (10) /* XXX Too small? */
275 int idx; /* screen index */
276 struct swm_region_list rl; /* list of regions on this screen */
279 struct workspace ws[SWM_WS_MAX];
285 } c[SWM_S_COLOR_MAX];
287 struct swm_screen *screens;
290 struct ws_win *cur_focus = NULL;
292 /* args to functions */
295 #define SWM_ARG_ID_FOCUSNEXT (0)
296 #define SWM_ARG_ID_FOCUSPREV (1)
297 #define SWM_ARG_ID_FOCUSMAIN (2)
298 #define SWM_ARG_ID_SWAPNEXT (3)
299 #define SWM_ARG_ID_SWAPPREV (4)
300 #define SWM_ARG_ID_SWAPMAIN (5)
301 #define SWM_ARG_ID_MASTERSHRINK (6)
302 #define SWM_ARG_ID_MASTERGROW (7)
303 #define SWM_ARG_ID_MASTERADD (8)
304 #define SWM_ARG_ID_MASTERDEL (9)
305 #define SWM_ARG_ID_STACKRESET (10)
306 #define SWM_ARG_ID_STACKINIT (11)
307 #define SWM_ARG_ID_CYCLEWS_UP (12)
308 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
309 #define SWM_ARG_ID_CYCLESC_UP (14)
310 #define SWM_ARG_ID_CYCLESC_DOWN (15)
311 #define SWM_ARG_ID_SS_ALL (0)
312 #define SWM_ARG_ID_SS_WINDOW (1)
321 #define SWM_Q_FLOAT (1<<0)
323 { "MPlayer", "xv", SWM_Q_FLOAT },
324 { "OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT },
329 void expose(XEvent *);
330 void keypress(XEvent *);
331 void buttonpress(XEvent *);
332 void configurerequest(XEvent *);
333 void configurenotify(XEvent *);
334 void destroynotify(XEvent *);
335 void enternotify(XEvent *);
336 void focusin(XEvent *);
337 void mappingnotify(XEvent *);
338 void maprequest(XEvent *);
339 void propertynotify(XEvent *);
340 void unmapnotify(XEvent *);
341 void visibilitynotify(XEvent *);
343 void (*handler[LASTEvent])(XEvent *) = {
345 [KeyPress] = keypress,
346 [ButtonPress] = buttonpress,
347 [ConfigureRequest] = configurerequest,
348 [ConfigureNotify] = configurenotify,
349 [DestroyNotify] = destroynotify,
350 [EnterNotify] = enternotify,
352 [MappingNotify] = mappingnotify,
353 [MapRequest] = maprequest,
354 [PropertyNotify] = propertynotify,
355 [UnmapNotify] = unmapnotify,
356 [VisibilityNotify] = visibilitynotify,
360 name_to_color(char *colorname)
364 XColor screen_def, exact_def;
365 unsigned long result = 0;
366 char cname[32] = "#";
368 cmap = DefaultColormap(display, screens[0].idx);
369 status = XAllocNamedColor(display, cmap, colorname,
370 &screen_def, &exact_def);
372 strlcat(cname, colorname + 2, sizeof cname - 1);
373 status = XAllocNamedColor(display, cmap, cname, &screen_def,
377 result = screen_def.pixel;
379 fprintf(stderr, "color '%s' not found.\n", colorname);
385 setscreencolor(char *val, int i, int c)
387 if (i > 0 && i <= ScreenCount(display)) {
388 screens[i - 1].c[c].color = name_to_color(val);
389 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
391 } else if (i == -1) {
392 for (i = 0; i < ScreenCount(display); i++)
393 screens[i].c[c].color = name_to_color(val);
394 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
397 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
398 i, ScreenCount(display));
402 varmatch(char *var, char *name, int *index)
407 i = strncmp(var, name, 255);
414 p = var + strlen(name);
417 bzero(buf, sizeof buf);
419 while (isdigit(*p) && i < sizeof buf)
421 if (i == 0 || i >= sizeof buf || *p != ']')
423 *index = strtonum(buf, 0, 99, NULL);
427 /* conf file stuff */
428 #define SWM_CONF_WS "\n= \t"
429 #define SWM_CONF_FILE "scrotwm.conf"
431 conf_load(char *filename)
434 char *line, *cp, *var, *val;
435 size_t len, lineno = 0;
438 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
440 if (filename == NULL)
443 if ((config = fopen(filename, "r")) == NULL)
446 for (sc = ScreenCount(display);;) {
447 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
451 cp += (long)strspn(cp, SWM_CONF_WS);
457 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
459 cp += (long)strspn(cp, SWM_CONF_WS);
460 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
463 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
466 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
467 bar_enabled = atoi(val);
468 else if (!varmatch(var, "bar_border", &i))
469 setscreencolor(val, i, SWM_S_COLOR_BAR_BORDER);
470 else if (!varmatch(var, "bar_color", &i))
471 setscreencolor(val, i, SWM_S_COLOR_BAR);
472 else if (!varmatch(var, "bar_font_color", &i))
473 setscreencolor(val, i, SWM_S_COLOR_BAR_FONT);
474 else if (!strncmp(var, "bar_font", strlen("bar_font")))
475 asprintf(&bar_fonts[0], "%s", val);
476 else if (!strncmp(var, "bar_action", strlen("bar_action")))
477 asprintf(&bar_argv[0], "%s", val);
478 else if (!strncmp(var, "bar_delay", strlen("bar_delay")))
479 bar_delay = atoi(val);
485 if (!varmatch(var, "color_focus", &i))
486 setscreencolor(val, i, SWM_S_COLOR_FOCUS);
487 else if (!varmatch(var, "color_unfocus", &i))
488 setscreencolor(val, i, SWM_S_COLOR_UNFOCUS);
489 else if (!strncmp(var, "cycle_empty", strlen("cycle_empty")))
490 cycle_visible = atoi(val);
491 else if (!strncmp(var, "cycle_visible", strlen("cycle_visible")))
492 cycle_visible = atoi(val);
498 if (!strncmp(var, "dialog_ratio",
499 strlen("dialog_ratio"))) {
500 dialog_ratio = atof(val);
501 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
508 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
509 asprintf(&spawn_term[0], "%s", val);
510 if (!strncmp(var, "screenshot_enabled",
511 strlen("screenshot_enabled")))
512 ss_enabled = atoi(val);
513 if (!strncmp(var, "screenshot_app",
514 strlen("screenshot_app")))
515 asprintf(&spawn_screenshot[0], "%s", val);
527 errx(1, "invalid conf file entry: %s=%s", var, val);
531 socket_setnonblock(int fd)
535 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
536 err(1, "fcntl F_GETFL");
538 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
539 err(1, "fcntl F_SETFL");
543 bar_print(struct swm_region *r, char *s)
545 XClearWindow(display, r->bar_window);
546 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
547 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
556 bzero(bar_pipe, sizeof bar_pipe);
559 kill(bar_pid, SIGTERM);
562 strlcpy(bar_ext, "", sizeof bar_ext);
571 struct swm_region *r;
575 char loc[SWM_BAR_MAX];
578 if (bar_enabled == 0)
581 if (bar_extra && bar_extra_running) {
582 /* ignore short reads; it'll correct itself */
583 while ((b = fgetln(stdin, &len)) != NULL)
584 if (b && b[len - 1] == '\n') {
586 strlcpy(bar_ext, b, sizeof bar_ext);
588 if (b == NULL && errno != EAGAIN) {
589 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
590 errno, strerror(errno));
594 strlcpy(bar_ext, "", sizeof bar_ext);
597 localtime_r(&tmt, &tm);
598 strftime(s, sizeof s, "%a %b %d %R %Z %Y", &tm);
599 for (i = 0; i < ScreenCount(display); i++) {
601 TAILQ_FOREACH(r, &screens[i].rl, entry) {
602 snprintf(loc, sizeof loc, "%s %d:%d %s",
603 s, x++, r->ws->idx + 1, bar_ext);
607 XSync(display, False);
618 bar_toggle(struct swm_region *r, union arg *args)
620 struct swm_region *tmpr;
621 int i, j, sc = ScreenCount(display);
623 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
626 for (i = 0; i < sc; i++)
627 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
628 XUnmapWindow(display, tmpr->bar_window);
630 for (i = 0; i < sc; i++)
631 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
632 XMapRaised(display, tmpr->bar_window);
634 bar_enabled = !bar_enabled;
635 XSync(display, False);
636 for (i = 0; i < sc; i++)
637 for (j = 0; j < SWM_WS_MAX; j++)
638 screens[i].ws[j].restack = 1;
641 /* must be after stack */
642 for (i = 0; i < sc; i++)
643 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
650 XSetWindowAttributes wa;
651 struct swm_region *r;
654 /* do this here because the conf file is in memory */
655 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
656 /* launch external status app */
657 bar_extra_running = 1;
658 if (pipe(bar_pipe) == -1)
659 err(1, "pipe error");
660 socket_setnonblock(bar_pipe[0]);
661 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
662 if (dup2(bar_pipe[0], 0) == -1)
664 if (dup2(bar_pipe[1], 1) == -1)
666 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
667 err(1, "could not disable SIGPIPE");
668 switch (bar_pid = fork()) {
670 err(1, "cannot fork");
674 execvp(bar_argv[0], bar_argv);
675 err(1, "%s external app failed", bar_argv[0]);
677 default: /* parent */
683 bzero(&wa, sizeof wa);
684 for (i = 0; i < ScreenCount(display); i++)
685 TAILQ_FOREACH(r, &screens[i].rl, entry) {
687 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
688 wa.background_pixel =
689 screens[i].c[SWM_S_COLOR_BAR].color;
690 XChangeWindowAttributes(display, r->bar_window,
691 CWBackPixel | CWBorderPixel, &wa);
697 bar_setup(struct swm_region *r)
701 for (i = 0; bar_fonts[i] != NULL; i++) {
702 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
708 if (bar_fonts[i] == NULL)
709 errx(1, "couldn't load font");
710 bar_height = bar_fs->ascent + bar_fs->descent + 3;
712 r->bar_window = XCreateSimpleWindow(display,
713 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
714 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
715 r->s->c[SWM_S_COLOR_BAR].color);
716 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
717 XSetFont(display, bar_gc, bar_fs->fid);
718 XSelectInput(display, r->bar_window, VisibilityChangeMask);
720 XMapRaised(display, r->bar_window);
721 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
723 if (signal(SIGALRM, bar_signal) == SIG_ERR)
724 err(1, "could not install bar_signal");
729 config_win(struct ws_win *win)
733 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
734 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
735 ce.type = ConfigureNotify;
736 ce.display = display;
742 ce.height = win->g.h;
743 ce.border_width = 1; /* XXX store this! */
745 ce.override_redirect = False;
746 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
750 count_win(struct workspace *ws, int count_transient)
755 TAILQ_FOREACH(win, &ws->winlist, entry) {
756 if (count_transient == 0 && win->floating)
758 if (count_transient == 0 && win->transient)
762 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
768 quit(struct swm_region *r, union arg *args)
770 DNPRINTF(SWM_D_MISC, "quit\n");
780 for (i = 0; i < ScreenCount(display); i++)
781 for (j = 0; j < SWM_WS_MAX; j++)
782 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
783 XUnmapWindow(display, win->id);
787 restart(struct swm_region *r, union arg *args)
789 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
791 /* disable alarm because the following code may not be interrupted */
793 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
794 errx(1, "can't disable alarm");
799 XCloseDisplay(display);
800 execvp(start_argv[0], start_argv);
801 fprintf(stderr, "execvp failed\n");
807 root_to_region(Window root)
809 struct swm_region *r;
814 for (i = 0; i < ScreenCount(display); i++)
815 if (screens[i].root == root)
818 if (XQueryPointer(display, screens[i].root,
819 &rr, &cr, &x, &y, &wx, &wy, &mask) == False) {
820 /* if we can't query the pointer, grab the first region */
821 r = TAILQ_FIRST(&screens[i].rl);
823 /* otherwise, choose a region based on pointer location */
824 TAILQ_FOREACH(r, &screens[i].rl, entry) {
825 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
826 y >= Y(r) && y <= Y(r) + HEIGHT(r))
831 r = TAILQ_FIRST(&screens[i].rl);
837 find_window(Window id)
842 for (i = 0; i < ScreenCount(display); i++)
843 for (j = 0; j < SWM_WS_MAX; j++)
844 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
851 spawn(struct swm_region *r, union arg *args)
856 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
858 * The double-fork construct avoids zombie processes and keeps the code
859 * clean from stupid signal handlers.
864 close(ConnectionNumber(display));
865 setenv("LD_PRELOAD", SWM_LIB, 1);
866 if (asprintf(&ret, "%d", r->ws->idx)) {
867 setenv("_SWM_WS", ret, 1);
870 if (asprintf(&ret, "%d", getpid())) {
871 setenv("_SWM_PID", ret, 1);
875 /* kill stdin, mplayer, ssh-add etc. need that */
876 si = open("/dev/null", O_RDONLY, 0);
878 err(1, "open /dev/null");
879 if (dup2(si, 0) == -1)
880 err(1, "dup2 /dev/null");
881 execvp(args->argv[0], args->argv);
882 fprintf(stderr, "execvp failed\n");
891 spawnmenu(struct swm_region *r, union arg *args)
893 DNPRINTF(SWM_D_MISC, "spawnmenu\n");
895 spawn_menu[SWM_MENU_FN] = bar_fonts[bar_fidx];
896 spawn_menu[SWM_MENU_NB] = r->s->c[SWM_S_COLOR_BAR].name;
897 spawn_menu[SWM_MENU_NF] = r->s->c[SWM_S_COLOR_BAR_FONT].name;
898 spawn_menu[SWM_MENU_SB] = r->s->c[SWM_S_COLOR_BAR_BORDER].name;
899 spawn_menu[SWM_MENU_SF] = r->s->c[SWM_S_COLOR_BAR].name;
910 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
912 for (i = 0; i < ScreenCount(display); i++)
913 for (j = 0; j < SWM_WS_MAX; j++)
914 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
915 if (win->ws->r == NULL)
918 XSetWindowBorder(display, win->id,
919 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
921 win->ws->focus = NULL;
927 focus_win(struct ws_win *win)
929 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
935 win->ws->focus = win;
936 if (win->ws->r != NULL) {
938 if (!win->got_focus) {
939 XSetWindowBorder(display, win->id,
940 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
944 XSetInputFocus(display, win->id,
945 RevertToPointerRoot, CurrentTime);
950 switchws(struct swm_region *r, union arg *args)
953 struct swm_region *this_r, *other_r;
955 struct workspace *new_ws, *old_ws;
959 new_ws = &this_r->s->ws[wsid];
961 DNPRINTF(SWM_D_WS, "switchws screen %d region %dx%d+%d+%d: "
962 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
965 if (new_ws == old_ws)
970 /* if the other workspace is hidden, switch windows */
971 /* map new window first to prevent ugly blinking */
972 TAILQ_FOREACH(win, &new_ws->winlist, entry)
973 XMapRaised(display, win->id);
975 TAILQ_FOREACH(win, &old_ws->winlist, entry)
976 XUnmapWindow(display, win->id);
980 other_r->ws = old_ws;
989 focus_win(new_ws->focus);
995 cyclews(struct swm_region *r, union arg *args)
998 struct swm_screen *s = r->s;
1000 DNPRINTF(SWM_D_WS, "cyclews id %d "
1001 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1002 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1007 case SWM_ARG_ID_CYCLEWS_UP:
1008 if (a.id < SWM_WS_MAX - 1)
1013 case SWM_ARG_ID_CYCLEWS_DOWN:
1017 a.id = SWM_WS_MAX - 1;
1023 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1025 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1029 } while (a.id != r->ws->idx);
1033 cyclescr(struct swm_region *r, union arg *args)
1035 struct swm_region *rr;
1040 case SWM_ARG_ID_CYCLESC_UP:
1041 rr = TAILQ_NEXT(r, entry);
1043 rr = TAILQ_FIRST(&screens[i].rl);
1045 case SWM_ARG_ID_CYCLESC_DOWN:
1046 rr = TAILQ_PREV(r, swm_region_list, entry);
1048 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1054 XSetInputFocus(display, PointerRoot, RevertToPointerRoot, CurrentTime);
1055 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, rr->g.x,
1056 rr->g.y + bar_enabled ? bar_height : 0);
1060 swapwin(struct swm_region *r, union arg *args)
1062 struct ws_win *target;
1063 struct ws_win_list *wl;
1066 DNPRINTF(SWM_D_WS, "swapwin id %d "
1067 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1068 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1069 if (cur_focus == NULL)
1072 wl = &cur_focus->ws->winlist;
1075 case SWM_ARG_ID_SWAPPREV:
1076 target = TAILQ_PREV(cur_focus, ws_win_list, entry);
1077 TAILQ_REMOVE(wl, cur_focus, entry);
1079 TAILQ_INSERT_TAIL(wl, cur_focus, entry);
1081 TAILQ_INSERT_BEFORE(target, cur_focus, entry);
1083 case SWM_ARG_ID_SWAPNEXT:
1084 target = TAILQ_NEXT(cur_focus, entry);
1085 TAILQ_REMOVE(wl, cur_focus, entry);
1087 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
1089 TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
1091 case SWM_ARG_ID_SWAPMAIN:
1092 target = TAILQ_FIRST(wl);
1093 if (target == cur_focus)
1095 TAILQ_REMOVE(wl, target, entry);
1096 TAILQ_INSERT_BEFORE(cur_focus, target, entry);
1097 TAILQ_REMOVE(wl, cur_focus, entry);
1098 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
1101 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1110 focus(struct swm_region *r, union arg *args)
1112 struct ws_win *winfocus, *winlostfocus;
1113 struct ws_win_list *wl;
1115 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1116 if (cur_focus == NULL)
1119 wl = &cur_focus->ws->winlist;
1121 winlostfocus = cur_focus;
1124 case SWM_ARG_ID_FOCUSPREV:
1125 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1126 if (winfocus == NULL)
1127 winfocus = TAILQ_LAST(wl, ws_win_list);
1130 case SWM_ARG_ID_FOCUSNEXT:
1131 winfocus = TAILQ_NEXT(cur_focus, entry);
1132 if (winfocus == NULL)
1133 winfocus = TAILQ_FIRST(wl);
1136 case SWM_ARG_ID_FOCUSMAIN:
1137 winfocus = TAILQ_FIRST(wl);
1144 if (winfocus == winlostfocus || winfocus == NULL)
1147 XMapRaised(display, winfocus->id);
1148 focus_win(winfocus);
1149 XSync(display, False);
1153 cycle_layout(struct swm_region *r, union arg *args)
1155 struct workspace *ws = r->ws;
1157 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1160 if (ws->cur_layout->l_stack == NULL)
1161 ws->cur_layout = &layouts[0];
1167 stack_config(struct swm_region *r, union arg *args)
1169 struct workspace *ws = r->ws;
1171 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1174 if (ws->cur_layout->l_config != NULL)
1175 ws->cur_layout->l_config(ws, args->id);
1177 if (args->id != SWM_ARG_ID_STACKINIT);
1183 struct swm_geometry g;
1184 struct swm_region *r;
1187 DNPRINTF(SWM_D_STACK, "stack\n");
1189 for (i = 0; i < ScreenCount(display); i++) {
1191 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1192 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1193 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1195 /* start with screen geometry, adjust for bar */
1205 r->ws->cur_layout->l_stack(r->ws, &g);
1208 XSync(display, False);
1212 stack_floater(struct ws_win *win, struct swm_region *r)
1217 bzero(&wc, sizeof wc);
1218 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1219 wc.border_width = 1;
1220 if (win->transient) {
1221 win->g.w = (double)WIDTH(r) * dialog_ratio;
1222 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1224 wc.width = win->g.w;
1225 wc.height = win->g.h;
1226 wc.x = (WIDTH(r) - win->g.w) / 2;
1227 wc.y = (HEIGHT(r) - win->g.h) / 2;
1229 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1230 win->id, wc.x, wc.y, wc.width, wc.height);
1232 XConfigureWindow(display, win->id, mask, &wc);
1235 #define SWAPXY(g) do { \
1237 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1238 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1241 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1244 struct swm_geometry win_g, r_g = *g;
1245 struct ws_win *win, *winfocus;
1246 int i, j, w_inc, h_inc, w_base, h_base;
1247 int hrh, extra, h_slice, last_h = 0;
1248 int split, colno, winno, mwin, msize, mscale;
1249 int remain, missing, v_slice;;
1252 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1253 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1255 if ((winno = count_win(ws, 0)) == 0)
1258 if (ws->focus == NULL)
1259 ws->focus = TAILQ_FIRST(&ws->winlist);
1260 winfocus = cur_focus ? cur_focus : ws->focus;
1262 win = TAILQ_FIRST(&ws->winlist);
1264 w_inc = win->sh.width_inc;
1265 w_base = win->sh.base_width;
1266 mwin = ws->l_state.horizontal_mwin;
1267 mscale = ws->l_state.horizontal_msize;
1270 w_inc = win->sh.height_inc;
1271 w_base = win->sh.base_height;
1272 mwin = ws->l_state.vertical_mwin;
1273 mscale = ws->l_state.vertical_msize;
1277 h_slice = r_g.h / SWM_H_SLICE;
1278 if (mwin && winno > mwin) {
1279 v_slice = r_g.w / SWM_V_SLICE;
1283 msize = v_slice * mscale;
1285 if (w_inc > 1 && w_inc < v_slice) {
1286 /* adjust for window's requested size increment */
1287 remain = (win_g.w - w_base) % w_inc;
1288 missing = w_inc - remain;
1290 if (missing <= extra || j == 0) {
1301 win_g.x += r_g.w - msize;
1306 hrh = r_g.h / colno;
1307 extra = r_g.h - (colno * hrh);
1311 TAILQ_FOREACH(win, &ws->winlist, entry) {
1312 if (split && i == split) {
1313 colno = winno - split;
1314 hrh = (r_g.h / colno);
1315 extra = r_g.h - (colno * hrh);
1319 win_g.x += msize + 2;
1320 win_g.w = r_g.w - (msize + 2);
1325 h_inc = win->sh.width_inc;
1326 h_base = win->sh.base_width;
1328 h_inc = win->sh.height_inc;
1329 h_base = win->sh.base_height;
1331 if (j == colno - 1) {
1332 win_g.h = hrh + extra;
1333 } else if (h_inc > 1 && h_inc < h_slice) {
1334 /* adjust for window's requested size increment */
1335 remain = (win_g.h - h_base) % h_inc;
1336 missing = h_inc - remain;
1338 if (missing <= extra || j == 0) {
1350 win_g.y += last_h + 2;
1352 if (win->transient != 0 || win->floating != 0)
1353 stack_floater(win, ws->r);
1355 bzero(&wc, sizeof wc);
1356 wc.border_width = 1;
1358 win->g.x = wc.x = win_g.y;
1359 win->g.y = wc.y = win_g.x;
1360 win->g.w = wc.width = win_g.h;
1361 win->g.h = wc.height = win_g.w;
1363 win->g.x = wc.x = win_g.x;
1364 win->g.y = wc.y = win_g.y;
1365 win->g.w = wc.width = win_g.w;
1366 win->g.h = wc.height = win_g.h;
1368 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1369 XConfigureWindow(display, win->id, mask, &wc);
1371 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);
1375 XMapRaised(display, win->id);
1382 focus_win(winfocus); /* has to be done outside of the loop */
1386 vertical_config(struct workspace *ws, int id)
1388 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1391 case SWM_ARG_ID_STACKRESET:
1392 case SWM_ARG_ID_STACKINIT:
1393 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1394 ws->l_state.vertical_mwin = 1;
1396 case SWM_ARG_ID_MASTERSHRINK:
1397 if (ws->l_state.vertical_msize > 1)
1398 ws->l_state.vertical_msize--;
1400 case SWM_ARG_ID_MASTERGROW:
1401 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1402 ws->l_state.vertical_msize++;
1404 case SWM_ARG_ID_MASTERADD:
1405 ws->l_state.vertical_mwin++;
1407 case SWM_ARG_ID_MASTERDEL:
1408 if (ws->l_state.vertical_mwin > 0)
1409 ws->l_state.vertical_mwin--;
1417 vertical_stack(struct workspace *ws, struct swm_geometry *g)
1419 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1421 stack_master(ws, g, 0, 0);
1425 horizontal_config(struct workspace *ws, int id)
1427 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1430 case SWM_ARG_ID_STACKRESET:
1431 case SWM_ARG_ID_STACKINIT:
1432 ws->l_state.horizontal_mwin = 1;
1433 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1435 case SWM_ARG_ID_MASTERSHRINK:
1436 if (ws->l_state.horizontal_msize > 1)
1437 ws->l_state.horizontal_msize--;
1439 case SWM_ARG_ID_MASTERGROW:
1440 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1441 ws->l_state.horizontal_msize++;
1443 case SWM_ARG_ID_MASTERADD:
1444 ws->l_state.horizontal_mwin++;
1446 case SWM_ARG_ID_MASTERDEL:
1447 if (ws->l_state.horizontal_mwin > 0)
1448 ws->l_state.horizontal_mwin--;
1456 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
1458 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1460 stack_master(ws, g, 1, 0);
1463 /* fullscreen view */
1465 max_stack(struct workspace *ws, struct swm_geometry *g) {
1467 struct swm_geometry gg = *g;
1468 struct ws_win *win, *winfocus;
1471 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1473 if (count_win(ws, 0) == 0)
1476 if (ws->focus == NULL)
1477 ws->focus = TAILQ_FIRST(&ws->winlist);
1478 winfocus = cur_focus ? cur_focus : ws->focus;
1480 TAILQ_FOREACH(win, &ws->winlist, entry) {
1481 if (win->transient != 0 || win->floating != 0) {
1482 if (win == ws->focus) {
1484 stack_floater(win, ws->r);
1485 XMapRaised(display, win->id);
1487 XUnmapWindow(display, win->id);
1489 bzero(&wc, sizeof wc);
1490 wc.border_width = 1;
1491 win->g.x = wc.x = gg.x;
1492 win->g.y = wc.y = gg.y;
1493 win->g.w = wc.width = gg.w;
1494 win->g.h = wc.height = gg.h;
1495 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1496 XConfigureWindow(display, win->id, mask, &wc);
1498 if (win == ws->focus) {
1499 XMapRaised(display, win->id);
1501 XUnmapWindow(display, win->id);
1506 focus_win(winfocus); /* has to be done outside of the loop */
1510 send_to_ws(struct swm_region *r, union arg *args)
1512 int wsid = args->id;
1513 struct ws_win *win = cur_focus;
1514 struct workspace *ws, *nws;
1515 Atom ws_idx_atom = 0;
1516 unsigned char ws_idx_str[SWM_PROPLEN];
1518 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1521 nws = &win->s->ws[wsid];
1523 XUnmapWindow(display, win->id);
1525 /* find a window to focus */
1526 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1527 if (ws->focus == NULL)
1528 ws->focus = TAILQ_FIRST(&ws->winlist);
1529 if (ws->focus == win)
1532 TAILQ_REMOVE(&ws->winlist, win, entry);
1534 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1537 /* Try to update the window's workspace property */
1538 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1540 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
1541 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1543 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1544 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1547 if (count_win(nws, 1) == 1)
1556 wkill(struct swm_region *r, union arg *args)
1558 DNPRINTF(SWM_D_MISC, "wkill\n");
1559 if(r->ws->focus != NULL)
1560 XKillClient(display, r->ws->focus->id);
1564 screenshot(struct swm_region *r, union arg *args)
1568 DNPRINTF(SWM_D_MISC, "screenshot\n");
1570 if (ss_enabled == 0)
1574 case SWM_ARG_ID_SS_ALL:
1575 spawn_screenshot[1] = "full";
1577 case SWM_ARG_ID_SS_WINDOW:
1578 spawn_screenshot[1] = "window";
1583 a.argv = spawn_screenshot;
1587 /* key definitions */
1591 void (*func)(struct swm_region *r, union arg *);
1594 /* modifier key function argument */
1595 { MODKEY, XK_space, cycle_layout, {0} },
1596 { MODKEY | ShiftMask, XK_space, stack_config, {.id = SWM_ARG_ID_STACKRESET} },
1597 { MODKEY, XK_h, stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
1598 { MODKEY, XK_l, stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
1599 { MODKEY, XK_comma, stack_config, {.id = SWM_ARG_ID_MASTERADD} },
1600 { MODKEY, XK_period, stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
1601 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1602 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1603 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1604 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1605 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1606 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1607 { MODKEY, XK_p, spawnmenu, {.argv = spawn_menu} },
1608 { MODKEY | ShiftMask, XK_q, quit, {0} },
1609 { MODKEY, XK_q, restart, {0} },
1610 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1611 { MODKEY, XK_1, switchws, {.id = 0} },
1612 { MODKEY, XK_2, switchws, {.id = 1} },
1613 { MODKEY, XK_3, switchws, {.id = 2} },
1614 { MODKEY, XK_4, switchws, {.id = 3} },
1615 { MODKEY, XK_5, switchws, {.id = 4} },
1616 { MODKEY, XK_6, switchws, {.id = 5} },
1617 { MODKEY, XK_7, switchws, {.id = 6} },
1618 { MODKEY, XK_8, switchws, {.id = 7} },
1619 { MODKEY, XK_9, switchws, {.id = 8} },
1620 { MODKEY, XK_0, switchws, {.id = 9} },
1621 { MODKEY, XK_Right, cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
1622 { MODKEY, XK_Left, cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
1623 { MODKEY | ShiftMask, XK_Right, cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
1624 { MODKEY | ShiftMask, XK_Left, cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
1625 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1626 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1627 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1628 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1629 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1630 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1631 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1632 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1633 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1634 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1635 { MODKEY, XK_b, bar_toggle, {0} },
1636 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1637 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1638 { MODKEY | ShiftMask, XK_x, wkill, {0} },
1639 { MODKEY, XK_s, screenshot, {.id = SWM_ARG_ID_SS_ALL} },
1640 { MODKEY | ShiftMask, XK_s, screenshot, {.id = SWM_ARG_ID_SS_WINDOW} },
1644 resize_window(struct ws_win *win)
1648 struct swm_region *r;
1650 r = root_to_region(win->wa.root);
1651 bzero(&wc, sizeof wc);
1652 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1653 wc.border_width = 1;
1654 wc.width = win->g.w;
1655 wc.height = win->g.h;
1656 wc.x = (WIDTH(r) - win->g.w) / 2;
1657 wc.y = (HEIGHT(r) - win->g.h) / 2;
1659 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
1660 win->id, wc.x, wc.y, wc.width, wc.height);
1662 XConfigureWindow(display, win->id, mask, &wc);
1667 resize(struct ws_win *win)
1672 DNPRINTF(SWM_D_MOUSE, "resize: win %d floating %d trans %d\n",
1673 win->id, win->floating, win->transient);
1675 if (!(win->transient != 0 || win->floating != 0))
1678 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
1679 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
1681 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w, win->g.h);
1683 XMaskEvent(display, MOUSEMASK | ExposureMask |
1684 SubstructureRedirectMask, &ev);
1686 case ConfigureRequest:
1689 handler[ev.type](&ev);
1692 XSync(display, False);
1693 win->g.w = nw = ev.xmotion.x;
1694 win->g.h = nh = ev.xmotion.y;
1698 } while (ev.type != ButtonRelease);
1699 XUngrabPointer(display, CurrentTime);
1702 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
1706 click(struct ws_win *win, union arg *args)
1708 DNPRINTF(SWM_D_MOUSE, "click: button: %d\n", args->id);
1724 enum { client_click, root_click };
1726 unsigned int action;
1728 unsigned int button;
1729 void (*func)(struct ws_win *, union arg *);
1732 /* action key mouse button func args */
1733 { client_click, MODKEY, Button3, click, {.id=Button3} },
1737 updatenumlockmask(void)
1740 XModifierKeymap *modmap;
1742 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1744 modmap = XGetModifierMapping(display);
1745 for (i = 0; i < 8; i++)
1746 for (j = 0; j < modmap->max_keypermod; j++)
1747 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1748 == XKeysymToKeycode(display, XK_Num_Lock))
1749 numlockmask = (1 << i);
1751 XFreeModifiermap(modmap);
1757 unsigned int i, j, k;
1759 unsigned int modifiers[] =
1760 { 0, LockMask, numlockmask, numlockmask | LockMask };
1762 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1763 updatenumlockmask();
1765 for (k = 0; k < ScreenCount(display); k++) {
1766 if (TAILQ_EMPTY(&screens[k].rl))
1768 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
1769 for (i = 0; i < LENGTH(keys); i++) {
1770 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
1771 for (j = 0; j < LENGTH(modifiers); j++)
1772 XGrabKey(display, code,
1773 keys[i].mod | modifiers[j],
1774 screens[k].root, True,
1775 GrabModeAsync, GrabModeAsync);
1781 grabbuttons(struct ws_win *win, int focused)
1784 unsigned int modifiers[] =
1785 { 0, LockMask, numlockmask, numlockmask|LockMask };
1787 updatenumlockmask();
1788 XUngrabButton(display, AnyButton, AnyModifier, win->id);
1790 for (i = 0; i < LENGTH(buttons); i++)
1791 if (buttons[i].action == client_click)
1792 for (j = 0; j < LENGTH(modifiers); j++)
1793 XGrabButton(display, buttons[i].button,
1794 buttons[i].mask | modifiers[j],
1795 win->id, False, BUTTONMASK,
1796 GrabModeAsync, GrabModeSync, None,
1799 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
1800 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1806 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1814 XKeyEvent *ev = &e->xkey;
1816 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1818 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1819 for (i = 0; i < LENGTH(keys); i++)
1820 if (keysym == keys[i].keysym
1821 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1823 keys[i].func(root_to_region(ev->root),
1828 buttonpress(XEvent *e)
1830 XButtonPressedEvent *ev = &e->xbutton;
1835 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1837 action = root_click;
1838 if ((win = find_window(ev->window)) == NULL)
1842 action = client_click;
1845 for (i = 0; i < LENGTH(buttons); i++)
1846 if (action == buttons[i].action && buttons[i].func &&
1847 buttons[i].button == ev->button &&
1848 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
1849 buttons[i].func(win, &buttons[i].args);
1853 set_win_state(struct ws_win *win, long state)
1855 long data[] = {state, None};
1857 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1859 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1860 (unsigned char *)data, 2);
1864 manage_window(Window id)
1867 struct workspace *ws;
1869 int format, i, ws_idx;
1870 unsigned long nitems, bytes;
1871 Atom ws_idx_atom = 0, type;
1872 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
1873 struct swm_region *r;
1877 if ((win = find_window(id)) != NULL)
1878 return (win); /* already being managed */
1880 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1881 errx(1, "calloc: failed to allocate memory for new window");
1883 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1885 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
1886 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
1888 XGetWindowAttributes(display, id, &win->wa);
1889 r = root_to_region(win->wa.root);
1890 /* If the window was managed before, put it in the same workspace */
1892 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
1893 ws_idx = strtonum(prop, 0, 9, &errstr);
1895 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
1897 ws = &r->s->ws[ws_idx];
1903 win->s = r->s; /* this never changes */
1904 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
1906 XGetTransientForHint(display, win->id, &trans);
1908 win->transient = trans;
1909 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1910 (unsigned)win->id, win->transient);
1912 win->g.w = win->wa.width;
1913 win->g.h = win->wa.height;
1914 win->g.x = win->wa.x;
1915 win->g.y = win->wa.y;
1917 XGetWMNormalHints(display, win->id, &win->sh, &mask); /* XXX function? */
1919 if (ws_idx_atom && prop == NULL &&
1920 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
1921 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1923 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1924 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1929 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);
1932 if (XGetClassHint(display, win->id, &win->ch)) {
1933 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
1934 win->ch.res_class, win->ch.res_name);
1935 for (i = 0; quirks[i].class != NULL && quirks[i].name != NULL &&
1936 quirks[i].quirk != 0; i++){
1937 if (!strcmp(win->ch.res_class, quirks[i].class) &&
1938 !strcmp(win->ch.res_name, quirks[i].name)) {
1939 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
1940 win->ch.res_class, win->ch.res_name);
1941 if (quirks[i].quirk & SWM_Q_FLOAT)
1947 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
1948 PropertyChangeMask | StructureNotifyMask);
1950 set_win_state(win, NormalState);
1952 /* make new win focused */
1959 configurerequest(XEvent *e)
1961 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1966 if ((win = find_window(ev->window)) == NULL)
1970 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
1973 fprintf(stderr, "configurerequest: new window: %lu x %d y %d w %d h %d bw %d s %d sm %d\n",
1974 ev->window, ev->x, ev->y, ev->width, ev->height, ev->border_width, ev->above, ev->detail);
1976 bzero(&wc, sizeof wc);
1979 wc.width = ev->width;
1980 wc.height = ev->height;
1981 wc.border_width = ev->border_width;
1982 wc.sibling = ev->above;
1983 wc.stack_mode = ev->detail;
1984 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
1987 fprintf(stderr, "configurerequest: change window: %lu\n",
1990 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
1992 if (win->floating) {
1993 if (ev->value_mask & CWX)
1995 if (ev->value_mask & CWY)
1997 if (ev->value_mask & CWWidth)
1998 win->g.w = ev->width;
1999 if (ev->value_mask & CWHeight)
2000 win->g.h = ev->height;
2001 if (win->ws->r != NULL) {
2002 /* this seems to be full screen */
2003 if (win->g.w >= WIDTH(win->ws->r)) {
2005 win->g.w = WIDTH(win->ws->r);
2006 ev->value_mask |= CWX | CWWidth;
2008 if (win->g.h >= HEIGHT(win->ws->r)) {
2011 win->g.h = HEIGHT(win->ws->r);
2012 ev->value_mask |= CWY | CWHeight;
2015 if ((ev->value_mask & (CWX|CWY)) &&
2016 !(ev->value_mask & (CWWidth|CWHeight)))
2018 XMoveResizeWindow(display, win->id,
2019 win->g.x, win->g.y, win->g.w, win->g.h);
2026 configurenotify(XEvent *e)
2028 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
2029 e->xconfigure.window);
2033 destroynotify(XEvent *e)
2036 XDestroyWindowEvent *ev = &e->xdestroywindow;
2037 struct workspace *ws;
2039 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
2041 if ((win = find_window(ev->window)) != NULL) {
2043 /* find a window to focus */
2044 if (ws->focus == win)
2045 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
2046 if (ws->focus == NULL)
2047 ws->focus = TAILQ_FIRST(&ws->winlist);
2048 if (ws->focus == NULL || ws->focus == win) {
2052 focus_win(ws->focus);
2053 TAILQ_REMOVE(&ws->winlist, win, entry);
2054 set_win_state(win, WithdrawnState);
2055 if (win->ch.res_class)
2056 XFree(win->ch.res_class);
2057 if (win->ch.res_name)
2058 XFree(win->ch.res_name);
2065 enternotify(XEvent *e)
2067 XCrossingEvent *ev = &e->xcrossing;
2070 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
2073 /* eat event(r) to prevent autofocus */
2078 if ((win = find_window(ev->window)) != NULL)
2085 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
2089 mappingnotify(XEvent *e)
2091 XMappingEvent *ev = &e->xmapping;
2093 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
2095 XRefreshKeyboardMapping(ev);
2096 if (ev->request == MappingKeyboard)
2101 maprequest(XEvent *e)
2103 XMapRequestEvent *ev = &e->xmaprequest;
2104 XWindowAttributes wa;
2106 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
2107 e->xmaprequest.window);
2109 if (!XGetWindowAttributes(display, ev->window, &wa))
2111 if (wa.override_redirect)
2113 manage_window(e->xmaprequest.window);
2118 propertynotify(XEvent *e)
2121 XPropertyEvent *ev = &e->xproperty;
2123 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
2126 if (ev->state == PropertyDelete)
2127 return; /* ignore */
2128 win = find_window(ev->window);
2133 case XA_WM_NORMAL_HINTS:
2136 XGetWMNormalHints(display, win->id, &win->sh, &mask);
2137 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
2138 if (win->sh.flags & PMinSize) {
2139 win->g.w = win->sh.min_width;
2140 win->g.h = win->sh.min_height;
2141 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
2143 XMoveResizeWindow(display, win->id,
2144 win->g.x, win->g.y, win->g.w, win->g.h);
2153 unmapnotify(XEvent *e)
2155 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
2159 visibilitynotify(XEvent *e)
2162 struct swm_region *r;
2164 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
2165 e->xvisibility.window);
2166 if (e->xvisibility.state == VisibilityUnobscured)
2167 for (i = 0; i < ScreenCount(display); i++)
2168 TAILQ_FOREACH(r, &screens[i].rl, entry)
2169 if (e->xvisibility.window == r->bar_window)
2174 xerror_start(Display *d, XErrorEvent *ee)
2181 xerror(Display *d, XErrorEvent *ee)
2183 /* fprintf(stderr, "error: %p %p\n", display, ee); */
2191 xerrorxlib = XSetErrorHandler(xerror_start);
2193 /* this causes an error if some other window manager is running */
2194 XSelectInput(display, DefaultRootWindow(display),
2195 SubstructureRedirectMask);
2196 XSync(display, False);
2200 XSetErrorHandler(xerror);
2201 XSync(display, False);
2210 unsigned char *p = NULL;
2211 unsigned long n, extra;
2214 astate = XInternAtom(display, "WM_STATE", False);
2215 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
2216 &real, &format, &n, &extra, (unsigned char **)&p);
2217 if (status != Success)
2226 new_region(struct swm_screen *s, struct workspace *ws,
2227 int x, int y, int w, int h)
2229 struct swm_region *r;
2231 DNPRINTF(SWM_D_MISC, "new region on screen %d: %dx%d (%d, %d)\n",
2232 s->idx, x, y, w, h);
2234 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
2235 errx(1, "calloc: failed to allocate memory for screen");
2244 TAILQ_INSERT_TAIL(&s->rl, r, entry);
2251 #ifdef SWM_XRR_HAS_CRTC
2253 XRRScreenResources *sr;
2255 #endif /* SWM_XRR_HAS_CRTC */
2256 Window d1, d2, *wins = NULL;
2257 XWindowAttributes wa;
2258 struct swm_region *r;
2260 int errorbase, major, minor;
2261 int ncrtc = 0, w = 0;
2263 struct workspace *ws;
2267 if ((screens = calloc(ScreenCount(display),
2268 sizeof(struct swm_screen))) == NULL)
2269 errx(1, "calloc: screens");
2271 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2274 /* map physical screens */
2275 for (i = 0; i < ScreenCount(display); i++) {
2276 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
2278 TAILQ_INIT(&screens[i].rl);
2279 screens[i].root = RootWindow(display, i);
2281 /* set default colors */
2282 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
2283 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
2284 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
2285 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
2286 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
2288 /* init all workspaces */
2289 for (j = 0; j < SWM_WS_MAX; j++) {
2290 ws = &screens[i].ws[j];
2295 TAILQ_INIT(&ws->winlist);
2297 for (k = 0; layouts[k].l_stack != NULL; k++)
2298 if (layouts[k].l_config != NULL)
2299 layouts[k].l_config(ws,
2300 SWM_ARG_ID_STACKINIT);
2301 ws->cur_layout = &layouts[0];
2304 /* map virtual screens onto physical screens */
2305 screens[i].xrandr_support = XRRQueryExtension(display,
2306 &xrandr_eventbase, &errorbase);
2307 if (screens[i].xrandr_support)
2308 if (XRRQueryVersion(display, &major, &minor) &&
2310 screens[i].xrandr_support = 0;
2312 #if 0 /* not ready for dynamic screen changes */
2313 if (screens[i].xrandr_support)
2314 XRRSelectInput(display,
2316 RRScreenChangeNotifyMask);
2319 /* grab existing windows (before we build the bars)*/
2320 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
2323 #ifdef SWM_XRR_HAS_CRTC
2324 sr = XRRGetScreenResources(display, screens[i].root);
2326 new_region(&screens[i], &screens[i].ws[w],
2327 0, 0, DisplayWidth(display, i),
2328 DisplayHeight(display, i));
2332 for (c = 0, ci = NULL; c < ncrtc; c++) {
2333 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
2334 if (ci->noutput == 0)
2337 if (ci != NULL && ci->mode == None)
2338 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2339 DisplayWidth(display, i),
2340 DisplayHeight(display, i));
2342 new_region(&screens[i], &screens[i].ws[w],
2343 ci->x, ci->y, ci->width, ci->height);
2347 XRRFreeCrtcInfo(ci);
2348 XRRFreeScreenResources(sr);
2350 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2351 DisplayWidth(display, i),
2352 DisplayHeight(display, i));
2353 #endif /* SWM_XRR_HAS_CRTC */
2355 /* attach windows to a region */
2356 /* normal windows */
2357 if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
2358 errx(1, "no regions on screen %d", i);
2360 for (i = 0; i < no; i++) {
2361 XGetWindowAttributes(display, wins[i], &wa);
2362 if (!XGetWindowAttributes(display, wins[i], &wa) ||
2363 wa.override_redirect ||
2364 XGetTransientForHint(display, wins[i], &d1))
2367 if (wa.map_state == IsViewable ||
2368 getstate(wins[i]) == NormalState)
2369 manage_window(wins[i]);
2371 /* transient windows */
2372 for (i = 0; i < no; i++) {
2373 if (!XGetWindowAttributes(display, wins[i], &wa))
2376 if (XGetTransientForHint(display, wins[i], &d1) &&
2377 (wa.map_state == IsViewable || getstate(wins[i]) ==
2379 manage_window(wins[i]);
2389 main(int argc, char *argv[])
2392 char conf[PATH_MAX], *cfile = NULL;
2399 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
2400 SWM_VERSION, cvstag);
2401 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2402 warnx("no locale support");
2404 if (!(display = XOpenDisplay(0)))
2405 errx(1, "can not open display");
2408 errx(1, "other wm running");
2410 astate = XInternAtom(display, "WM_STATE", False);
2412 /* look for local and global conf file */
2413 pwd = getpwuid(getuid());
2415 errx(1, "invalid user %d", getuid());
2419 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
2420 if (stat(conf, &sb) != -1) {
2421 if (S_ISREG(sb.st_mode))
2424 /* try global conf file */
2425 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
2426 if (!stat(conf, &sb))
2427 if (S_ISREG(sb.st_mode))
2434 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2439 xfd = ConnectionNumber(display);
2443 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
2445 errx(1, "select failed");
2450 while(XPending(display)) {
2451 XNextEvent(display, &e);
2452 if (handler[e.type])
2453 handler[e.type](&e);
2457 XCloseDisplay(display);