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 #define SWM_VERSION "0.5"
67 #include <sys/types.h>
71 #include <sys/queue.h>
72 #include <sys/param.h>
73 #include <sys/select.h>
75 #include <X11/cursorfont.h>
76 #include <X11/keysym.h>
77 #include <X11/Xatom.h>
79 #include <X11/Xproto.h>
80 #include <X11/Xutil.h>
81 #include <X11/extensions/Xrandr.h>
84 # error XRandR versions less than 1.0 are not supported
89 #define SWM_XRR_HAS_CRTC
93 /* #define SWM_DEBUG */
95 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
96 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
97 #define SWM_D_MISC 0x0001
98 #define SWM_D_EVENT 0x0002
99 #define SWM_D_WS 0x0004
100 #define SWM_D_FOCUS 0x0008
101 #define SWM_D_MOVE 0x0010
102 #define SWM_D_STACK 0x0020
103 #define SWM_D_MOUSE 0x0040
104 #define SWM_D_PROP 0x0080
107 u_int32_t swm_debug = 0
117 #define DPRINTF(x...)
118 #define DNPRINTF(n,x...)
121 #define LENGTH(x) (sizeof x / sizeof x[0])
122 #define MODKEY Mod1Mask
123 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
124 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
126 #define X(r) (r)->g.x
127 #define Y(r) (r)->g.y
128 #define WIDTH(r) (r)->g.w
129 #define HEIGHT(r) (r)->g.h
132 #define SWM_LIB "/usr/X11R6/lib/swmhack.so"
135 #define SWM_PROPLEN (16)
139 int (*xerrorxlib)(Display *, XErrorEvent *);
143 int xrandr_eventbase;
144 int ignore_enter = 0;
145 unsigned int numlockmask = 0;
149 int cycle_visible = 0;
152 double dialog_ratio = .6;
154 #define SWM_BAR_MAX (128)
155 char *bar_argv[] = { NULL, NULL };
157 char bar_ext[SWM_BAR_MAX];
158 sig_atomic_t bar_alarm = 0;
162 int bar_extra_running = 0;
170 char *bar_fonts[] = {
171 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
172 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
176 /* terminal + args */
177 char *spawn_term[] = { "xterm", NULL };
178 char *spawn_screenshot[] = { "scrot", NULL, NULL }; /* XXX get from conf */
179 char *spawn_menu[] = { "dmenu_run", "-fn", NULL,
180 "-nb", NULL, "-nf", NULL, "-sb", NULL, "-sf", NULL, NULL };
182 #define SWM_MENU_FN (2)
183 #define SWM_MENU_NB (4)
184 #define SWM_MENU_NF (6)
185 #define SWM_MENU_SB (8)
186 #define SWM_MENU_SF (10)
188 /* layout manager data */
189 struct swm_geometry {
199 /* virtual "screens" */
201 TAILQ_ENTRY(swm_region) entry;
202 struct swm_geometry g;
204 struct workspace *ws; /* current workspace on this region */
205 struct swm_screen *s; /* screen idx */
207 TAILQ_HEAD(swm_region_list, swm_region);
210 TAILQ_ENTRY(ws_win) entry;
212 struct swm_geometry g;
216 struct workspace *ws; /* always valid */
217 struct swm_screen *s; /* always valid, never changes */
218 XWindowAttributes wa;
221 TAILQ_HEAD(ws_win_list, ws_win);
223 /* layout handlers */
225 void vertical_config(struct workspace *, int);
226 void vertical_stack(struct workspace *, struct swm_geometry *);
227 void horizontal_config(struct workspace *, int);
228 void horizontal_stack(struct workspace *, struct swm_geometry *);
229 void max_stack(struct workspace *, struct swm_geometry *);
231 void grabbuttons(struct ws_win *, int);
234 void (*l_stack)(struct workspace *, struct swm_geometry *);
235 void (*l_config)(struct workspace *, int);
237 /* stack, configure */
238 { vertical_stack, vertical_config},
239 { horizontal_stack, horizontal_config},
244 #define SWM_H_SLICE (32)
245 #define SWM_V_SLICE (32)
247 /* define work spaces */
249 int idx; /* workspace index */
250 int restack; /* restack on switch */
251 struct layout *cur_layout; /* current layout handlers */
252 struct ws_win *focus; /* may be NULL */
253 struct swm_region *r; /* may be NULL */
254 struct ws_win_list winlist; /* list of windows in ws */
258 int horizontal_msize;
265 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
266 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
268 /* physical screen mapping */
269 #define SWM_WS_MAX (10) /* XXX Too small? */
271 int idx; /* screen index */
272 struct swm_region_list rl; /* list of regions on this screen */
275 struct workspace ws[SWM_WS_MAX];
281 } c[SWM_S_COLOR_MAX];
283 struct swm_screen *screens;
286 struct ws_win *cur_focus = NULL;
288 /* args to functions */
291 #define SWM_ARG_ID_FOCUSNEXT (0)
292 #define SWM_ARG_ID_FOCUSPREV (1)
293 #define SWM_ARG_ID_FOCUSMAIN (2)
294 #define SWM_ARG_ID_SWAPNEXT (3)
295 #define SWM_ARG_ID_SWAPPREV (4)
296 #define SWM_ARG_ID_SWAPMAIN (5)
297 #define SWM_ARG_ID_MASTERSHRINK (6)
298 #define SWM_ARG_ID_MASTERGROW (7)
299 #define SWM_ARG_ID_MASTERADD (8)
300 #define SWM_ARG_ID_MASTERDEL (9)
301 #define SWM_ARG_ID_STACKRESET (10)
302 #define SWM_ARG_ID_STACKINIT (11)
303 #define SWM_ARG_ID_CYCLEWS_UP (12)
304 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
305 #define SWM_ARG_ID_SS_ALL (0)
306 #define SWM_ARG_ID_SS_WINDOW (1)
311 name_to_color(char *colorname)
315 XColor screen_def, exact_def;
316 unsigned long result = 0;
317 char cname[32] = "#";
319 cmap = DefaultColormap(display, screens[0].idx);
320 status = XAllocNamedColor(display, cmap, colorname,
321 &screen_def, &exact_def);
323 strlcat(cname, colorname + 2, sizeof cname - 1);
324 status = XAllocNamedColor(display, cmap, cname, &screen_def,
328 result = screen_def.pixel;
330 fprintf(stderr, "color '%s' not found.\n", colorname);
336 setscreencolor(char *val, int i, int c)
338 if (i > 0 && i <= ScreenCount(display)) {
339 screens[i - 1].c[c].color = name_to_color(val);
340 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
342 } else if (i == -1) {
343 for (i = 0; i < ScreenCount(display); i++)
344 screens[i].c[c].color = name_to_color(val);
345 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
348 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
349 i, ScreenCount(display));
353 varmatch(char *var, char *name, int *index)
358 i = strncmp(var, name, 255);
365 p = var + strlen(name);
368 bzero(buf, sizeof buf);
370 while (isdigit(*p) && i < sizeof buf)
372 if (i == 0 || i >= sizeof buf || *p != ']')
374 *index = strtonum(buf, 0, 99, NULL);
378 /* conf file stuff */
379 #define SWM_CONF_WS "\n= \t"
380 #define SWM_CONF_FILE "scrotwm.conf"
382 conf_load(char *filename)
385 char *line, *cp, *var, *val;
386 size_t len, lineno = 0;
389 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
391 if (filename == NULL)
394 if ((config = fopen(filename, "r")) == NULL)
397 for (sc = ScreenCount(display);;) {
398 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
402 cp += (long)strspn(cp, SWM_CONF_WS);
408 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
410 cp += (long)strspn(cp, SWM_CONF_WS);
411 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
414 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
417 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
418 bar_enabled = atoi(val);
419 else if (!varmatch(var, "bar_border", &i))
420 setscreencolor(val, i, SWM_S_COLOR_BAR_BORDER);
421 else if (!varmatch(var, "bar_color", &i))
422 setscreencolor(val, i, SWM_S_COLOR_BAR);
423 else if (!varmatch(var, "bar_font_color", &i))
424 setscreencolor(val, i, SWM_S_COLOR_BAR_FONT);
425 else if (!strncmp(var, "bar_font", strlen("bar_font")))
426 asprintf(&bar_fonts[0], "%s", val);
427 else if (!strncmp(var, "bar_action", strlen("bar_action")))
428 asprintf(&bar_argv[0], "%s", val);
429 else if (!strncmp(var, "bar_delay", strlen("bar_delay")))
430 bar_delay = atoi(val);
436 if (!varmatch(var, "color_focus", &i))
437 setscreencolor(val, i, SWM_S_COLOR_FOCUS);
438 else if (!varmatch(var, "color_unfocus", &i))
439 setscreencolor(val, i, SWM_S_COLOR_UNFOCUS);
440 else if (!strncmp(var, "cycle_empty", strlen("cycle_empty")))
441 cycle_visible = atoi(val);
442 else if (!strncmp(var, "cycle_visible", strlen("cycle_visible")))
443 cycle_visible = atoi(val);
449 if (!strncmp(var, "dialog_ratio",
450 strlen("dialog_ratio"))) {
451 dialog_ratio = atof(val);
452 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
459 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
460 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
461 if (!strncmp(var, "screenshot_enabled",
462 strlen("screenshot_enabled")))
463 ss_enabled = atoi(val);
475 errx(1, "invalid conf file entry: %s=%s", var, val);
479 socket_setnonblock(int fd)
483 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
484 err(1, "fcntl F_GETFL");
486 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
487 err(1, "fcntl F_SETFL");
491 bar_print(struct swm_region *r, char *s)
493 XClearWindow(display, r->bar_window);
494 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
495 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
504 bzero(bar_pipe, sizeof bar_pipe);
507 kill(bar_pid, SIGTERM);
510 strlcpy(bar_ext, "", sizeof bar_ext);
519 struct swm_region *r;
523 char loc[SWM_BAR_MAX];
526 if (bar_enabled == 0)
529 if (bar_extra && bar_extra_running) {
530 /* ignore short reads; it'll correct itself */
531 while ((b = fgetln(stdin, &len)) != NULL)
532 if (b && b[len - 1] == '\n') {
534 strlcpy(bar_ext, b, sizeof bar_ext);
536 if (b == NULL && errno != EAGAIN) {
537 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
538 errno, strerror(errno));
542 strlcpy(bar_ext, "", sizeof bar_ext);
545 localtime_r(&tmt, &tm);
546 strftime(s, sizeof s, "%a %b %d %R %Z %Y", &tm);
547 for (i = 0; i < ScreenCount(display); i++) {
549 TAILQ_FOREACH(r, &screens[i].rl, entry) {
550 snprintf(loc, sizeof loc, "%s %d:%d %s",
551 s, x++, r->ws->idx + 1, bar_ext);
555 XSync(display, False);
566 bar_toggle(struct swm_region *r, union arg *args)
568 struct swm_region *tmpr;
569 int i, j, sc = ScreenCount(display);
571 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
574 for (i = 0; i < sc; i++)
575 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
576 XUnmapWindow(display, tmpr->bar_window);
578 for (i = 0; i < sc; i++)
579 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
580 XMapRaised(display, tmpr->bar_window);
582 bar_enabled = !bar_enabled;
583 XSync(display, False);
584 for (i = 0; i < sc; i++)
585 for (j = 0; j < SWM_WS_MAX; j++)
586 screens[i].ws[j].restack = 1;
589 /* must be after stack */
590 for (i = 0; i < sc; i++)
591 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
598 XSetWindowAttributes wa;
599 struct swm_region *r;
602 /* do this here because the conf file is in memory */
603 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
604 /* launch external status app */
605 bar_extra_running = 1;
606 if (pipe(bar_pipe) == -1)
607 err(1, "pipe error");
608 socket_setnonblock(bar_pipe[0]);
609 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
610 if (dup2(bar_pipe[0], 0) == -1)
612 if (dup2(bar_pipe[1], 1) == -1)
614 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
615 err(1, "could not disable SIGPIPE");
616 switch (bar_pid = fork()) {
618 err(1, "cannot fork");
622 execvp(bar_argv[0], bar_argv);
623 err(1, "%s external app failed", bar_argv[0]);
625 default: /* parent */
631 bzero(&wa, sizeof wa);
632 for (i = 0; i < ScreenCount(display); i++)
633 TAILQ_FOREACH(r, &screens[i].rl, entry) {
635 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
636 wa.background_pixel =
637 screens[i].c[SWM_S_COLOR_BAR].color;
638 XChangeWindowAttributes(display, r->bar_window,
639 CWBackPixel | CWBorderPixel, &wa);
645 bar_setup(struct swm_region *r)
649 for (i = 0; bar_fonts[i] != NULL; i++) {
650 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
656 if (bar_fonts[i] == NULL)
657 errx(1, "couldn't load font");
658 bar_height = bar_fs->ascent + bar_fs->descent + 3;
660 r->bar_window = XCreateSimpleWindow(display,
661 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
662 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
663 r->s->c[SWM_S_COLOR_BAR].color);
664 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
665 XSetFont(display, bar_gc, bar_fs->fid);
666 XSelectInput(display, r->bar_window, VisibilityChangeMask);
668 XMapRaised(display, r->bar_window);
669 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
671 if (signal(SIGALRM, bar_signal) == SIG_ERR)
672 err(1, "could not install bar_signal");
677 config_win(struct ws_win *win)
681 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
682 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
683 ce.type = ConfigureNotify;
684 ce.display = display;
690 ce.height = win->g.h;
691 ce.border_width = 1; /* XXX store this! */
693 ce.override_redirect = False;
694 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
698 count_win(struct workspace *ws, int count_transient)
703 TAILQ_FOREACH(win, &ws->winlist, entry) {
704 if (count_transient == 0 && win->floating)
706 if (count_transient == 0 && win->transient)
710 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
716 quit(struct swm_region *r, union arg *args)
718 DNPRINTF(SWM_D_MISC, "quit\n");
723 restart(struct swm_region *r, union arg *args)
725 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
727 /* disable alarm because the following code may not be interrupted */
729 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
730 errx(1, "can't disable alarm");
734 XCloseDisplay(display);
735 execvp(start_argv[0], start_argv);
736 fprintf(stderr, "execvp failed\n");
742 root_to_region(Window root)
744 struct swm_region *r;
749 for (i = 0; i < ScreenCount(display); i++)
750 if (screens[i].root == root)
753 if (XQueryPointer(display, screens[i].root,
754 &rr, &cr, &x, &y, &wx, &wy, &mask) == False) {
755 /* if we can't query the pointer, grab the first region */
756 r = TAILQ_FIRST(&screens[i].rl);
758 /* otherwise, choose a region based on pointer location */
759 TAILQ_FOREACH(r, &screens[i].rl, entry) {
760 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
761 y >= Y(r) && y <= Y(r) + HEIGHT(r))
766 r = TAILQ_FIRST(&screens[i].rl);
772 find_window(Window id)
777 for (i = 0; i < ScreenCount(display); i++)
778 for (j = 0; j < SWM_WS_MAX; j++)
779 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
786 spawn(struct swm_region *r, union arg *args)
788 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
790 * The double-fork construct avoids zombie processes and keeps the code
791 * clean from stupid signal handlers.
797 close(ConnectionNumber(display));
798 setenv("LD_PRELOAD", SWM_LIB, 1);
799 if (asprintf(&ret, "%d", r->ws->idx))
800 setenv("_SWM_WS", ret, 1);
801 if (asprintf(&ret, "%d", getpid()))
802 setenv("_SWM_PID", ret, 1);
804 execvp(args->argv[0], args->argv);
805 fprintf(stderr, "execvp failed\n");
814 spawnmenu(struct swm_region *r, union arg *args)
816 DNPRINTF(SWM_D_MISC, "spawnmenu\n");
818 spawn_menu[SWM_MENU_FN] = bar_fonts[bar_fidx];
819 spawn_menu[SWM_MENU_NB] = r->s->c[SWM_S_COLOR_BAR].name;
820 spawn_menu[SWM_MENU_NF] = r->s->c[SWM_S_COLOR_BAR_FONT].name;
821 spawn_menu[SWM_MENU_SB] = r->s->c[SWM_S_COLOR_BAR_BORDER].name;
822 spawn_menu[SWM_MENU_SF] = r->s->c[SWM_S_COLOR_BAR].name;
833 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
835 for (i = 0; i < ScreenCount(display); i++)
836 for (j = 0; j < SWM_WS_MAX; j++)
837 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
838 if (win->ws->r == NULL)
841 XSetWindowBorder(display, win->id,
842 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
844 win->ws->focus = NULL;
850 focus_win(struct ws_win *win)
852 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
858 win->ws->focus = win;
859 if (win->ws->r != NULL) {
861 if (!win->got_focus) {
862 XSetWindowBorder(display, win->id,
863 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
867 XSetInputFocus(display, win->id,
868 RevertToPointerRoot, CurrentTime);
873 switchws(struct swm_region *r, union arg *args)
876 struct swm_region *this_r, *other_r;
878 struct workspace *new_ws, *old_ws;
882 new_ws = &this_r->s->ws[wsid];
884 DNPRINTF(SWM_D_WS, "switchws screen %d region %dx%d+%d+%d: "
885 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
888 if (new_ws == old_ws)
893 /* if the other workspace is hidden, switch windows */
894 /* map new window first to prevent ugly blinking */
895 TAILQ_FOREACH(win, &new_ws->winlist, entry)
896 XMapRaised(display, win->id);
898 TAILQ_FOREACH(win, &old_ws->winlist, entry)
899 XUnmapWindow(display, win->id);
904 other_r->ws = old_ws;
913 focus_win(new_ws->focus);
919 cyclews(struct swm_region *r, union arg *args)
922 struct swm_screen *s = r->s;
924 DNPRINTF(SWM_D_WS, "cyclews id %d "
925 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
926 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
932 case SWM_ARG_ID_CYCLEWS_UP:
933 if (a.id < SWM_WS_MAX - 1)
938 case SWM_ARG_ID_CYCLEWS_DOWN:
942 a.id = SWM_WS_MAX - 1;
948 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
950 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
954 } while (a.id != r->ws->idx);
958 swapwin(struct swm_region *r, union arg *args)
960 struct ws_win *target;
961 struct ws_win_list *wl;
964 DNPRINTF(SWM_D_WS, "swapwin id %d "
965 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
966 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
967 if (cur_focus == NULL)
970 wl = &cur_focus->ws->winlist;
973 case SWM_ARG_ID_SWAPPREV:
974 target = TAILQ_PREV(cur_focus, ws_win_list, entry);
975 TAILQ_REMOVE(wl, cur_focus, entry);
977 TAILQ_INSERT_TAIL(wl, cur_focus, entry);
979 TAILQ_INSERT_BEFORE(target, cur_focus, entry);
981 case SWM_ARG_ID_SWAPNEXT:
982 target = TAILQ_NEXT(cur_focus, entry);
983 TAILQ_REMOVE(wl, cur_focus, entry);
985 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
987 TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
989 case SWM_ARG_ID_SWAPMAIN:
990 target = TAILQ_FIRST(wl);
991 if (target == cur_focus)
993 TAILQ_REMOVE(wl, target, entry);
994 TAILQ_INSERT_BEFORE(cur_focus, target, entry);
995 TAILQ_REMOVE(wl, cur_focus, entry);
996 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
999 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1008 focus(struct swm_region *r, union arg *args)
1010 struct ws_win *winfocus, *winlostfocus;
1011 struct ws_win_list *wl;
1013 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1014 if (cur_focus == NULL)
1017 wl = &cur_focus->ws->winlist;
1019 winlostfocus = cur_focus;
1022 case SWM_ARG_ID_FOCUSPREV:
1023 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1024 if (winfocus == NULL)
1025 winfocus = TAILQ_LAST(wl, ws_win_list);
1028 case SWM_ARG_ID_FOCUSNEXT:
1029 winfocus = TAILQ_NEXT(cur_focus, entry);
1030 if (winfocus == NULL)
1031 winfocus = TAILQ_FIRST(wl);
1034 case SWM_ARG_ID_FOCUSMAIN:
1035 winfocus = TAILQ_FIRST(wl);
1042 if (winfocus == winlostfocus)
1045 XMapRaised(display, winfocus->id);
1046 focus_win(winfocus);
1047 XSync(display, False);
1051 cycle_layout(struct swm_region *r, union arg *args)
1053 struct workspace *ws = r->ws;
1055 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1058 if (ws->cur_layout->l_stack == NULL)
1059 ws->cur_layout = &layouts[0];
1066 stack_config(struct swm_region *r, union arg *args)
1068 struct workspace *ws = r->ws;
1070 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1073 if (ws->cur_layout->l_config != NULL)
1074 ws->cur_layout->l_config(ws, args->id);
1076 if (args->id != SWM_ARG_ID_STACKINIT);
1082 struct swm_geometry g;
1083 struct swm_region *r;
1086 DNPRINTF(SWM_D_STACK, "stack\n");
1088 for (i = 0; i < ScreenCount(display); i++) {
1090 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1091 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1092 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1094 /* start with screen geometry, adjust for bar */
1104 r->ws->cur_layout->l_stack(r->ws, &g);
1107 XSync(display, False);
1111 stack_floater(struct ws_win *win, struct swm_region *r)
1116 bzero(&wc, sizeof wc);
1117 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1118 wc.border_width = 1;
1119 if (win->transient) {
1120 win->g.w = (double)WIDTH(r) * dialog_ratio;
1121 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1123 wc.width = win->g.w;
1124 wc.height = win->g.h;
1125 wc.x = (WIDTH(r) - win->g.w) / 2;
1126 wc.y = (HEIGHT(r) - win->g.h) / 2;
1128 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1129 win->id, wc.x, wc.y, wc.width, wc.height);
1131 XConfigureWindow(display, win->id, mask, &wc);
1134 #define SWAPXY(g) do { \
1136 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1137 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1140 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1143 struct swm_geometry win_g, r_g = *g;
1144 struct ws_win *win, *winfocus;
1145 int i, j, w_inc, h_inc, w_base, h_base;
1146 int hrh, extra, h_slice, last_h = 0;
1147 int split, colno, winno, mwin, msize, mscale;
1148 int remain, missing, v_slice;;
1151 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1152 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1154 if ((winno = count_win(ws, 0)) == 0)
1157 if (ws->focus == NULL)
1158 ws->focus = TAILQ_FIRST(&ws->winlist);
1159 winfocus = cur_focus ? cur_focus : ws->focus;
1161 win = TAILQ_FIRST(&ws->winlist);
1163 w_inc = win->sh.width_inc;
1164 w_base = win->sh.base_width;
1165 mwin = ws->l_state.horizontal_mwin;
1166 mscale = ws->l_state.horizontal_msize;
1169 w_inc = win->sh.height_inc;
1170 w_base = win->sh.base_height;
1171 mwin = ws->l_state.vertical_mwin;
1172 mscale = ws->l_state.vertical_msize;
1176 h_slice = r_g.h / SWM_H_SLICE;
1177 if (mwin && winno > mwin) {
1178 v_slice = r_g.w / SWM_V_SLICE;
1182 msize = v_slice * mscale;
1184 if (w_inc > 1 && w_inc < v_slice) {
1185 /* adjust for window's requested size increment */
1186 remain = (win_g.w - w_base) % w_inc;
1187 missing = w_inc - remain;
1189 if (missing <= extra || j == 0) {
1200 win_g.x += r_g.w - msize;
1205 hrh = r_g.h / colno;
1206 extra = r_g.h - (colno * hrh);
1210 TAILQ_FOREACH(win, &ws->winlist, entry) {
1211 if (split && i == split) {
1212 colno = winno - split;
1213 hrh = (r_g.h / colno);
1214 extra = r_g.h - (colno * hrh);
1218 win_g.x += msize + 2;
1219 win_g.w = r_g.w - (msize + 2);
1224 h_inc = win->sh.width_inc;
1225 h_base = win->sh.base_width;
1227 h_inc = win->sh.height_inc;
1228 h_base = win->sh.base_height;
1230 if (j == colno - 1) {
1231 win_g.h = hrh + extra;
1232 } else if (h_inc > 1 && h_inc < h_slice) {
1233 /* adjust for window's requested size increment */
1234 remain = (win_g.h - h_base) % h_inc;
1235 missing = h_inc - remain;
1237 if (missing <= extra || j == 0) {
1249 win_g.y += last_h + 2;
1251 if (win->transient != 0 || win->floating != 0)
1252 stack_floater(win, ws->r);
1254 bzero(&wc, sizeof wc);
1255 wc.border_width = 1;
1257 win->g.x = wc.x = win_g.y;
1258 win->g.y = wc.y = win_g.x;
1259 win->g.w = wc.width = win_g.h;
1260 win->g.h = wc.height = win_g.w;
1262 win->g.x = wc.x = win_g.x;
1263 win->g.y = wc.y = win_g.y;
1264 win->g.w = wc.width = win_g.w;
1265 win->g.h = wc.height = win_g.h;
1267 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1268 XConfigureWindow(display, win->id, mask, &wc);
1270 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);
1274 XMapRaised(display, win->id);
1281 focus_win(winfocus); /* has to be done outside of the loop */
1285 vertical_config(struct workspace *ws, int id)
1287 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1290 case SWM_ARG_ID_STACKRESET:
1291 case SWM_ARG_ID_STACKINIT:
1292 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1293 ws->l_state.vertical_mwin = 1;
1295 case SWM_ARG_ID_MASTERSHRINK:
1296 if (ws->l_state.vertical_msize > 1)
1297 ws->l_state.vertical_msize--;
1299 case SWM_ARG_ID_MASTERGROW:
1300 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1301 ws->l_state.vertical_msize++;
1303 case SWM_ARG_ID_MASTERADD:
1304 ws->l_state.vertical_mwin++;
1306 case SWM_ARG_ID_MASTERDEL:
1307 if (ws->l_state.vertical_mwin > 0)
1308 ws->l_state.vertical_mwin--;
1316 vertical_stack(struct workspace *ws, struct swm_geometry *g)
1318 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1320 stack_master(ws, g, 0, 0);
1324 horizontal_config(struct workspace *ws, int id)
1326 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1329 case SWM_ARG_ID_STACKRESET:
1330 case SWM_ARG_ID_STACKINIT:
1331 ws->l_state.horizontal_mwin = 1;
1332 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1334 case SWM_ARG_ID_MASTERSHRINK:
1335 if (ws->l_state.horizontal_msize > 1)
1336 ws->l_state.horizontal_msize--;
1338 case SWM_ARG_ID_MASTERGROW:
1339 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1340 ws->l_state.horizontal_msize++;
1342 case SWM_ARG_ID_MASTERADD:
1343 ws->l_state.horizontal_mwin++;
1345 case SWM_ARG_ID_MASTERDEL:
1346 if (ws->l_state.horizontal_mwin > 0)
1347 ws->l_state.horizontal_mwin--;
1355 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
1357 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1359 stack_master(ws, g, 1, 0);
1362 /* fullscreen view */
1364 max_stack(struct workspace *ws, struct swm_geometry *g) {
1366 struct swm_geometry gg = *g;
1367 struct ws_win *win, *winfocus;
1370 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1372 if (count_win(ws, 0) == 0)
1375 if (ws->focus == NULL)
1376 ws->focus = TAILQ_FIRST(&ws->winlist);
1377 winfocus = cur_focus ? cur_focus : ws->focus;
1379 TAILQ_FOREACH(win, &ws->winlist, entry) {
1380 if (win->transient != 0 || win->floating != 0) {
1381 if (win == ws->focus) {
1383 stack_floater(win, ws->r);
1384 XMapRaised(display, win->id);
1386 XUnmapWindow(display, win->id);
1388 bzero(&wc, sizeof wc);
1389 wc.border_width = 1;
1390 win->g.x = wc.x = gg.x;
1391 win->g.y = wc.y = gg.y;
1392 win->g.w = wc.width = gg.w;
1393 win->g.h = wc.height = gg.h;
1394 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1395 XConfigureWindow(display, win->id, mask, &wc);
1397 if (win == ws->focus) {
1398 XMapRaised(display, win->id);
1400 XUnmapWindow(display, win->id);
1405 focus_win(winfocus); /* has to be done outside of the loop */
1409 send_to_ws(struct swm_region *r, union arg *args)
1411 int wsid = args->id;
1412 struct ws_win *win = cur_focus;
1413 struct workspace *ws, *nws;
1414 Atom ws_idx_atom = 0;
1415 unsigned char ws_idx_str[SWM_PROPLEN];
1417 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1420 nws = &win->s->ws[wsid];
1422 XUnmapWindow(display, win->id);
1424 /* find a window to focus */
1425 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1426 if (ws->focus == NULL)
1427 ws->focus = TAILQ_FIRST(&ws->winlist);
1428 if (ws->focus == win)
1431 TAILQ_REMOVE(&ws->winlist, win, entry);
1433 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1436 /* Try to update the window's workspace property */
1437 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1439 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
1440 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1442 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1443 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1446 if (count_win(nws, 1) == 1)
1455 wkill(struct swm_region *r, union arg *args)
1457 DNPRINTF(SWM_D_MISC, "wkill\n");
1458 if(r->ws->focus != NULL)
1459 XKillClient(display, r->ws->focus->id);
1463 screenshot(struct swm_region *r, union arg *args)
1467 DNPRINTF(SWM_D_MISC, "screenshot\n");
1469 if (ss_enabled == 0)
1473 case SWM_ARG_ID_SS_ALL:
1474 spawn_screenshot[1] = "-m";
1476 case SWM_ARG_ID_SS_WINDOW:
1477 spawn_screenshot[1] = "-s"; /* XXX doesn't work with scrot */
1482 a.argv = spawn_screenshot;
1486 /* key definitions */
1490 void (*func)(struct swm_region *r, union arg *);
1493 /* modifier key function argument */
1494 { MODKEY, XK_space, cycle_layout, {0} },
1495 { MODKEY | ShiftMask, XK_space, stack_config, {.id = SWM_ARG_ID_STACKRESET} },
1496 { MODKEY, XK_h, stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
1497 { MODKEY, XK_l, stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
1498 { MODKEY, XK_comma, stack_config, {.id = SWM_ARG_ID_MASTERADD} },
1499 { MODKEY, XK_period, stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
1500 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1501 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1502 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1503 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1504 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1505 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1506 { MODKEY, XK_p, spawnmenu, {.argv = spawn_menu} },
1507 { MODKEY | ShiftMask, XK_q, quit, {0} },
1508 { MODKEY, XK_q, restart, {0} },
1509 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1510 { MODKEY, XK_1, switchws, {.id = 0} },
1511 { MODKEY, XK_2, switchws, {.id = 1} },
1512 { MODKEY, XK_3, switchws, {.id = 2} },
1513 { MODKEY, XK_4, switchws, {.id = 3} },
1514 { MODKEY, XK_5, switchws, {.id = 4} },
1515 { MODKEY, XK_6, switchws, {.id = 5} },
1516 { MODKEY, XK_7, switchws, {.id = 6} },
1517 { MODKEY, XK_8, switchws, {.id = 7} },
1518 { MODKEY, XK_9, switchws, {.id = 8} },
1519 { MODKEY, XK_0, switchws, {.id = 9} },
1520 { MODKEY, XK_Right, cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
1521 { MODKEY, XK_Left, cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
1522 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1523 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1524 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1525 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1526 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1527 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1528 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1529 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1530 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1531 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1532 { MODKEY, XK_b, bar_toggle, {0} },
1533 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1534 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1535 { MODKEY | ShiftMask, XK_x, wkill, {0} },
1536 { MODKEY, XK_s, screenshot, {.id = SWM_ARG_ID_SS_ALL} },
1537 { MODKEY | ShiftMask, XK_s, screenshot, {.id = SWM_ARG_ID_SS_WINDOW} },
1541 click(struct swm_region *r, union arg *args)
1543 DNPRINTF(SWM_D_MOUSE, "click: button: %d\n", args->id);
1558 enum { client_click, root_click };
1560 unsigned int action;
1562 unsigned int button;
1563 void (*func)(struct swm_region *r, union arg *);
1566 /* action key mouse button func args */
1567 { client_click, MODKEY, Button1, click, {.id=Button1} },
1571 updatenumlockmask(void)
1574 XModifierKeymap *modmap;
1576 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1578 modmap = XGetModifierMapping(display);
1579 for (i = 0; i < 8; i++)
1580 for (j = 0; j < modmap->max_keypermod; j++)
1581 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1582 == XKeysymToKeycode(display, XK_Num_Lock))
1583 numlockmask = (1 << i);
1585 XFreeModifiermap(modmap);
1591 unsigned int i, j, k;
1593 unsigned int modifiers[] =
1594 { 0, LockMask, numlockmask, numlockmask | LockMask };
1596 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1597 updatenumlockmask();
1599 for (k = 0; k < ScreenCount(display); k++) {
1600 if (TAILQ_EMPTY(&screens[k].rl))
1602 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
1603 for (i = 0; i < LENGTH(keys); i++) {
1604 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
1605 for (j = 0; j < LENGTH(modifiers); j++)
1606 XGrabKey(display, code,
1607 keys[i].mod | modifiers[j],
1608 screens[k].root, True,
1609 GrabModeAsync, GrabModeAsync);
1615 grabbuttons(struct ws_win *win, int focused)
1618 unsigned int modifiers[] =
1619 { 0, LockMask, numlockmask, numlockmask|LockMask };
1621 updatenumlockmask();
1622 XUngrabButton(display, AnyButton, AnyModifier, win->id);
1624 for (i = 0; i < LENGTH(buttons); i++)
1625 if (buttons[i].action == client_click)
1626 for (j = 0; j < LENGTH(modifiers); j++)
1627 XGrabButton(display, buttons[i].button,
1628 buttons[i].mask | modifiers[j],
1629 win->id, False, BUTTONMASK,
1630 GrabModeAsync, GrabModeSync, None,
1633 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
1634 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1640 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1648 XKeyEvent *ev = &e->xkey;
1650 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1652 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1653 for (i = 0; i < LENGTH(keys); i++)
1654 if (keysym == keys[i].keysym
1655 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1657 keys[i].func(root_to_region(ev->root),
1662 buttonpress(XEvent *e)
1664 XButtonPressedEvent *ev = &e->xbutton;
1669 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1671 action = root_click;
1672 if ((win = find_window(ev->window)) == NULL)
1676 action = client_click;
1679 for (i = 0; i < LENGTH(buttons); i++)
1680 if (action == buttons[i].action && buttons[i].func &&
1681 buttons[i].button == ev->button &&
1682 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
1683 buttons[i].func(root_to_region(ev->root),
1688 set_win_state(struct ws_win *win, long state)
1690 long data[] = {state, None};
1692 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1694 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1695 (unsigned char *)data, 2);
1699 manage_window(Window id)
1702 struct workspace *ws;
1706 unsigned long nitems, bytes;
1707 Atom ws_idx_atom = 0, type;
1708 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
1709 struct swm_region *r;
1712 if ((win = find_window(id)) != NULL)
1713 return (win); /* already being managed */
1715 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1716 errx(1, "calloc: failed to allocate memory for new window");
1718 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1720 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
1721 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
1723 XGetWindowAttributes(display, id, &win->wa);
1724 r = root_to_region(win->wa.root);
1725 /* If the window was managed before, put it in the same workspace */
1730 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
1731 ws_idx = strtonum(prop, 0, 9, &errstr);
1733 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
1735 ws = &r->s->ws[ws_idx];
1741 win->s = r->s; /* this never changes */
1742 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
1744 /* make new win focused */
1747 XGetTransientForHint(display, win->id, &trans);
1749 win->transient = trans;
1750 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1751 (unsigned)win->id, win->transient);
1753 win->g.w = win->wa.width;
1754 win->g.h = win->wa.height;
1755 win->g.x = win->wa.x;
1756 win->g.y = win->wa.y;
1758 XGetWMNormalHints(display, win->id, &win->sh, &mask);
1760 if (ws_idx_atom && prop == NULL &&
1761 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
1762 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1764 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1765 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1770 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);
1773 /* XXX make this a table */
1774 bzero(&ch, sizeof ch);
1775 if (XGetClassHint(display, win->id, &ch)) {
1776 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1777 if (!strcmp(ch.res_class, "MPlayer") && !strcmp(ch.res_name, "xv")) {
1781 XFree(ch.res_class);
1786 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
1787 PropertyChangeMask | StructureNotifyMask);
1789 set_win_state(win, NormalState);
1795 configurerequest(XEvent *e)
1797 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1802 if ((win = find_window(ev->window)) == NULL)
1806 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
1809 fprintf(stderr, "configurerequest: new window: %lu x %d y %d w %d h %d bw %d s %d sm %d\n",
1810 ev->window, ev->x, ev->y, ev->width, ev->height, ev->border_width, ev->above, ev->detail);
1812 bzero(&wc, sizeof wc);
1815 wc.width = ev->width;
1816 wc.height = ev->height;
1817 wc.border_width = ev->border_width;
1818 wc.sibling = ev->above;
1819 wc.stack_mode = ev->detail;
1820 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
1823 fprintf(stderr, "configurerequest: change window: %lu\n",
1826 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
1828 if (win->floating) {
1829 if (ev->value_mask & CWX)
1831 if (ev->value_mask & CWY)
1833 if (ev->value_mask & CWWidth)
1834 win->g.w = ev->width;
1835 if (ev->value_mask & CWHeight)
1836 win->g.h = ev->height;
1837 if (win->ws->r != NULL) {
1838 /* this seems to be full screen */
1839 if (win->g.w > WIDTH(win->ws->r)) {
1844 if (win->g.h > HEIGHT(win->ws->r)) {
1850 if ((ev->value_mask & (CWX|CWY)) &&
1851 !(ev->value_mask & (CWWidth|CWHeight)))
1853 XMoveResizeWindow(display, win->id,
1854 win->g.x, win->g.y, win->g.w, win->g.h);
1861 configurenotify(XEvent *e)
1863 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1864 e->xconfigure.window);
1868 destroynotify(XEvent *e)
1871 XDestroyWindowEvent *ev = &e->xdestroywindow;
1872 struct workspace *ws;
1874 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1876 if ((win = find_window(ev->window)) != NULL) {
1878 /* find a window to focus */
1879 if (ws->focus == win)
1880 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1881 if (ws->focus == NULL)
1882 ws->focus = TAILQ_FIRST(&ws->winlist);
1883 if (ws->focus == win)
1885 if (cur_focus == win)
1886 focus_win(ws->focus);
1888 TAILQ_REMOVE(&ws->winlist, win, entry);
1889 set_win_state(win, WithdrawnState);
1896 enternotify(XEvent *e)
1898 XCrossingEvent *ev = &e->xcrossing;
1901 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1904 /* eat event(r) to prevent autofocus */
1909 if ((win = find_window(ev->window)) != NULL)
1916 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
1920 mappingnotify(XEvent *e)
1922 XMappingEvent *ev = &e->xmapping;
1924 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1926 XRefreshKeyboardMapping(ev);
1927 if (ev->request == MappingKeyboard)
1932 maprequest(XEvent *e)
1934 XMapRequestEvent *ev = &e->xmaprequest;
1935 XWindowAttributes wa;
1937 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1938 e->xmaprequest.window);
1940 if (!XGetWindowAttributes(display, ev->window, &wa))
1942 if (wa.override_redirect)
1944 manage_window(e->xmaprequest.window);
1949 propertynotify(XEvent *e)
1951 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1952 e->xproperty.window);
1956 unmapnotify(XEvent *e)
1958 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1962 visibilitynotify(XEvent *e)
1965 struct swm_region *r;
1967 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
1968 e->xvisibility.window);
1969 if (e->xvisibility.state == VisibilityUnobscured)
1970 for (i = 0; i < ScreenCount(display); i++)
1971 TAILQ_FOREACH(r, &screens[i].rl, entry)
1972 if (e->xvisibility.window == r->bar_window)
1976 void (*handler[LASTEvent])(XEvent *) = {
1978 [KeyPress] = keypress,
1979 [ButtonPress] = buttonpress,
1980 [ConfigureRequest] = configurerequest,
1981 [ConfigureNotify] = configurenotify,
1982 [DestroyNotify] = destroynotify,
1983 [EnterNotify] = enternotify,
1984 [FocusIn] = focusin,
1985 [MappingNotify] = mappingnotify,
1986 [MapRequest] = maprequest,
1987 [PropertyNotify] = propertynotify,
1988 [UnmapNotify] = unmapnotify,
1989 [VisibilityNotify] = visibilitynotify,
1993 xerror_start(Display *d, XErrorEvent *ee)
2000 xerror(Display *d, XErrorEvent *ee)
2002 /* fprintf(stderr, "error: %p %p\n", display, ee); */
2010 xerrorxlib = XSetErrorHandler(xerror_start);
2012 /* this causes an error if some other window manager is running */
2013 XSelectInput(display, DefaultRootWindow(display),
2014 SubstructureRedirectMask);
2015 XSync(display, False);
2019 XSetErrorHandler(xerror);
2020 XSync(display, False);
2029 unsigned char *p = NULL;
2030 unsigned long n, extra;
2033 astate = XInternAtom(display, "WM_STATE", False);
2034 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
2035 &real, &format, &n, &extra, (unsigned char **)&p);
2036 if (status != Success)
2045 new_region(struct swm_screen *s, struct workspace *ws,
2046 int x, int y, int w, int h)
2048 struct swm_region *r;
2050 DNPRINTF(SWM_D_MISC, "new region on screen %d: %dx%d (%d, %d)\n",
2051 s->idx, x, y, w, h);
2053 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
2054 errx(1, "calloc: failed to allocate memory for screen");
2063 TAILQ_INSERT_TAIL(&s->rl, r, entry);
2070 #ifdef SWM_XRR_HAS_CRTC
2072 XRRScreenResources *sr;
2074 #endif /* SWM_XRR_HAS_CRTC */
2075 Window d1, d2, *wins = NULL;
2076 XWindowAttributes wa;
2077 struct swm_region *r;
2079 int errorbase, major, minor;
2080 int ncrtc = 0, w = 0;
2082 struct workspace *ws;
2086 if ((screens = calloc(ScreenCount(display),
2087 sizeof(struct swm_screen))) == NULL)
2088 errx(1, "calloc: screens");
2090 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2093 /* map physical screens */
2094 for (i = 0; i < ScreenCount(display); i++) {
2095 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
2097 TAILQ_INIT(&screens[i].rl);
2098 screens[i].root = RootWindow(display, i);
2100 /* set default colors */
2101 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
2102 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
2103 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
2104 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
2105 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
2107 /* init all workspaces */
2108 for (j = 0; j < SWM_WS_MAX; j++) {
2109 ws = &screens[i].ws[j];
2114 TAILQ_INIT(&ws->winlist);
2116 for (k = 0; layouts[k].l_stack != NULL; k++)
2117 if (layouts[k].l_config != NULL)
2118 layouts[k].l_config(ws,
2119 SWM_ARG_ID_STACKINIT);
2120 ws->cur_layout = &layouts[0];
2123 /* map virtual screens onto physical screens */
2124 screens[i].xrandr_support = XRRQueryExtension(display,
2125 &xrandr_eventbase, &errorbase);
2126 if (screens[i].xrandr_support)
2127 if (XRRQueryVersion(display, &major, &minor) &&
2129 screens[i].xrandr_support = 0;
2131 #if 0 /* not ready for dynamic screen changes */
2132 if (screens[i].xrandr_support)
2133 XRRSelectInput(display,
2135 RRScreenChangeNotifyMask);
2138 /* grab existing windows (before we build the bars)*/
2139 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
2142 #ifdef SWM_XRR_HAS_CRTC
2143 sr = XRRGetScreenResources(display, screens[i].root);
2145 new_region(&screens[i], &screens[i].ws[w],
2146 0, 0, DisplayWidth(display, i),
2147 DisplayHeight(display, i));
2151 for (c = 0, ci = NULL; c < ncrtc; c++) {
2152 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
2153 if (ci->noutput == 0)
2156 if (ci != NULL && ci->mode == None)
2157 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2158 DisplayWidth(display, i),
2159 DisplayHeight(display, i));
2161 new_region(&screens[i], &screens[i].ws[w],
2162 ci->x, ci->y, ci->width, ci->height);
2166 XRRFreeCrtcInfo(ci);
2167 XRRFreeScreenResources(sr);
2169 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2170 DisplayWidth(display, i),
2171 DisplayHeight(display, i));
2172 #endif /* SWM_XRR_HAS_CRTC */
2174 /* attach windows to a region */
2175 /* normal windows */
2176 if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
2177 errx(1, "no regions on screen %d", i);
2179 for (i = 0; i < no; i++) {
2180 XGetWindowAttributes(display, wins[i], &wa);
2181 if (!XGetWindowAttributes(display, wins[i], &wa) ||
2182 wa.override_redirect ||
2183 XGetTransientForHint(display, wins[i], &d1))
2186 if (wa.map_state == IsViewable ||
2187 getstate(wins[i]) == NormalState)
2188 manage_window(wins[i]);
2190 /* transient windows */
2191 for (i = 0; i < no; i++) {
2192 if (!XGetWindowAttributes(display, wins[i], &wa))
2195 if (XGetTransientForHint(display, wins[i], &d1) &&
2196 (wa.map_state == IsViewable || getstate(wins[i]) ==
2198 manage_window(wins[i]);
2208 main(int argc, char *argv[])
2211 char conf[PATH_MAX], *cfile = NULL;
2218 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
2219 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2220 warnx("no locale support");
2222 if (!(display = XOpenDisplay(0)))
2223 errx(1, "can not open display");
2226 errx(1, "other wm running");
2228 astate = XInternAtom(display, "WM_STATE", False);
2230 /* look for local and global conf file */
2231 pwd = getpwuid(getuid());
2233 errx(1, "invalid user %d", getuid());
2237 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
2238 if (stat(conf, &sb) != -1) {
2239 if (S_ISREG(sb.st_mode))
2242 /* try global conf file */
2243 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
2244 if (!stat(conf, &sb))
2245 if (S_ISREG(sb.st_mode))
2252 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2257 xfd = ConnectionNumber(display);
2261 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
2263 errx(1, "select failed");
2268 while(XPending(display)) {
2269 XNextEvent(display, &e);
2270 if (handler[e.type])
2271 handler[e.type](&e);
2275 XCloseDisplay(display);