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 */
94 /* #define SWM_DEBUG */
96 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
97 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
98 #define SWM_D_MISC 0x0001
99 #define SWM_D_EVENT 0x0002
100 #define SWM_D_WS 0x0004
101 #define SWM_D_FOCUS 0x0008
102 #define SWM_D_MOVE 0x0010
103 #define SWM_D_STACK 0x0020
104 #define SWM_D_MOUSE 0x0040
106 u_int32_t swm_debug = 0
115 #define DPRINTF(x...)
116 #define DNPRINTF(n,x...)
119 #define LENGTH(x) (sizeof x / sizeof x[0])
120 #define MODKEY Mod1Mask
121 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
122 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
124 #define X(r) (r)->g.x
125 #define Y(r) (r)->g.y
126 #define WIDTH(r) (r)->g.w
127 #define HEIGHT(r) (r)->g.h
131 int (*xerrorxlib)(Display *, XErrorEvent *);
134 int xrandr_eventbase;
135 int ignore_enter = 0;
136 unsigned int numlockmask = 0;
140 int cycle_visible = 0;
143 double dialog_ratio = .6;
145 #define SWM_BAR_MAX (128)
146 char *bar_argv[] = { NULL, NULL };
148 char bar_ext[SWM_BAR_MAX];
149 sig_atomic_t bar_alarm = 0;
152 int bar_extra_running = 0;
160 char *bar_fonts[] = {
161 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
162 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
166 /* terminal + args */
167 char *spawn_term[] = { "xterm", NULL };
168 char *spawn_menu[] = { "dmenu_run", "-fn", NULL,
169 "-nb", NULL, "-nf", NULL, "-sb", NULL, "-sf", NULL, NULL };
171 #define SWM_MENU_FN (2)
172 #define SWM_MENU_NB (4)
173 #define SWM_MENU_NF (6)
174 #define SWM_MENU_SB (8)
175 #define SWM_MENU_SF (10)
177 /* layout manager data */
178 struct swm_geometry {
188 /* virtual "screens" */
190 TAILQ_ENTRY(swm_region) entry;
191 struct swm_geometry g;
193 struct workspace *ws; /* current workspace on this region */
194 struct swm_screen *s; /* screen idx */
196 TAILQ_HEAD(swm_region_list, swm_region);
199 TAILQ_ENTRY(ws_win) entry;
201 struct swm_geometry g;
205 struct workspace *ws; /* always valid */
206 struct swm_screen *s; /* always valid, never changes */
207 XWindowAttributes wa;
209 TAILQ_HEAD(ws_win_list, ws_win);
211 /* layout handlers */
213 void vertical_config(struct workspace *, int);
214 void vertical_stack(struct workspace *, struct swm_geometry *);
215 void horizontal_config(struct workspace *, int);
216 void horizontal_stack(struct workspace *, struct swm_geometry *);
217 void max_stack(struct workspace *, struct swm_geometry *);
219 void grabbuttons(struct ws_win *, int);
222 void (*l_stack)(struct workspace *, struct swm_geometry *);
223 void (*l_config)(struct workspace *, int);
225 /* stack, configure */
226 { vertical_stack, vertical_config},
227 { horizontal_stack, horizontal_config},
232 #define SWM_H_SLICE (32)
233 #define SWM_V_SLICE (32)
235 /* define work spaces */
237 int idx; /* workspace index */
238 int restack; /* restack on switch */
239 struct layout *cur_layout; /* current layout handlers */
240 struct ws_win *focus; /* may be NULL */
241 struct swm_region *r; /* may be NULL */
242 struct ws_win_list winlist; /* list of windows in ws */
246 int horizontal_msize;
253 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
254 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
256 /* physical screen mapping */
257 #define SWM_WS_MAX (10) /* XXX Too small? */
259 int idx; /* screen index */
260 struct swm_region_list rl; /* list of regions on this screen */
263 struct workspace ws[SWM_WS_MAX];
269 } c[SWM_S_COLOR_MAX];
271 struct swm_screen *screens;
274 struct ws_win *cur_focus = NULL;
276 /* args to functions */
279 #define SWM_ARG_ID_FOCUSNEXT (0)
280 #define SWM_ARG_ID_FOCUSPREV (1)
281 #define SWM_ARG_ID_FOCUSMAIN (2)
282 #define SWM_ARG_ID_SWAPNEXT (3)
283 #define SWM_ARG_ID_SWAPPREV (4)
284 #define SWM_ARG_ID_SWAPMAIN (5)
285 #define SWM_ARG_ID_MASTERSHRINK (6)
286 #define SWM_ARG_ID_MASTERGROW (7)
287 #define SWM_ARG_ID_MASTERADD (8)
288 #define SWM_ARG_ID_MASTERDEL (9)
289 #define SWM_ARG_ID_STACKRESET (10)
290 #define SWM_ARG_ID_STACKINIT (11)
291 #define SWM_ARG_ID_CYCLEWS_UP (12)
292 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
297 name_to_color(char *colorname)
301 XColor screen_def, exact_def;
302 unsigned long result = 0;
303 char cname[32] = "#";
305 cmap = DefaultColormap(display, screens[0].idx);
306 status = XAllocNamedColor(display, cmap, colorname,
307 &screen_def, &exact_def);
309 strlcat(cname, colorname + 2, sizeof cname - 1);
310 status = XAllocNamedColor(display, cmap, cname, &screen_def,
314 result = screen_def.pixel;
316 fprintf(stderr, "color '%s' not found.\n", colorname);
322 setscreencolor(char *val, int i, int c)
324 if (i > 0 && i <= ScreenCount(display)) {
325 screens[i - 1].c[c].color = name_to_color(val);
326 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
328 } else if (i == -1) {
329 for (i = 0; i < ScreenCount(display); i++)
330 screens[i].c[c].color = name_to_color(val);
331 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
334 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
335 i, ScreenCount(display));
339 varmatch(char *var, char *name, int *index)
344 i = strncmp(var, name, 255);
351 p = var + strlen(name);
354 bzero(buf, sizeof buf);
356 while (isdigit(*p) && i < sizeof buf)
358 if (i == 0 || i >= sizeof buf || *p != ']')
360 *index = strtonum(buf, 0, 99, NULL);
364 /* conf file stuff */
365 #define SWM_CONF_WS "\n= \t"
366 #define SWM_CONF_FILE "scrotwm.conf"
368 conf_load(char *filename)
371 char *line, *cp, *var, *val;
372 size_t len, lineno = 0;
375 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
377 if (filename == NULL)
380 if ((config = fopen(filename, "r")) == NULL)
383 for (sc = ScreenCount(display);;) {
384 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
388 cp += (long)strspn(cp, SWM_CONF_WS);
394 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
396 cp += (long)strspn(cp, SWM_CONF_WS);
397 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
400 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
403 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
404 bar_enabled = atoi(val);
405 else if (!varmatch(var, "bar_border", &i))
406 setscreencolor(val, i, SWM_S_COLOR_BAR_BORDER);
407 else if (!varmatch(var, "bar_color", &i))
408 setscreencolor(val, i, SWM_S_COLOR_BAR);
409 else if (!varmatch(var, "bar_font_color", &i))
410 setscreencolor(val, i, SWM_S_COLOR_BAR_FONT);
411 else if (!strncmp(var, "bar_font", strlen("bar_font")))
412 asprintf(&bar_fonts[0], "%s", val);
413 else if (!strncmp(var, "bar_action", strlen("bar_action")))
414 asprintf(&bar_argv[0], "%s", val);
420 if (!varmatch(var, "color_focus", &i))
421 setscreencolor(val, i, SWM_S_COLOR_FOCUS);
422 else if (!varmatch(var, "color_unfocus", &i))
423 setscreencolor(val, i, SWM_S_COLOR_UNFOCUS);
424 else if (!strncmp(var, "cycle_empty", strlen("cycle_empty")))
425 cycle_visible = atoi(val);
426 else if (!strncmp(var, "cycle_visible", strlen("cycle_visible")))
427 cycle_visible = atoi(val);
433 if (!strncmp(var, "dialog_ratio",
434 strlen("dialog_ratio"))) {
435 dialog_ratio = atof(val);
436 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
443 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
444 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
456 errx(1, "invalid conf file entry: %s=%s", var, val);
460 socket_setnonblock(int fd)
464 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
465 err(1, "fcntl F_GETFL");
467 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
468 err(1, "fcntl F_SETFL");
472 bar_print(struct swm_region *r, char *s)
474 XClearWindow(display, r->bar_window);
475 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
476 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
485 bzero(bar_pipe, sizeof bar_pipe);
488 kill(bar_pid, SIGTERM);
491 strlcpy(bar_ext, "", sizeof bar_ext);
500 struct swm_region *r;
504 char loc[SWM_BAR_MAX];
507 if (bar_enabled == 0)
510 if (bar_extra && bar_extra_running) {
511 /* ignore short reads; it'll correct itself */
512 while ((b = fgetln(stdin, &len)) != NULL)
513 if (b && b[len - 1] == '\n') {
515 strlcpy(bar_ext, b, sizeof bar_ext);
517 if (b == NULL && errno != EAGAIN) {
518 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
519 errno, strerror(errno));
523 strlcpy(bar_ext, "", sizeof bar_ext);
526 localtime_r(&tmt, &tm);
527 strftime(s, sizeof s, "%a %b %d %R %Z %Y", &tm);
528 for (i = 0; i < ScreenCount(display); i++) {
530 TAILQ_FOREACH(r, &screens[i].rl, entry) {
531 snprintf(loc, sizeof loc, "%s %d:%d %s",
532 s, x++, r->ws->idx + 1, bar_ext);
536 XSync(display, False);
548 bar_toggle(struct swm_region *r, union arg *args)
550 struct swm_region *tmpr;
551 int i, j, sc = ScreenCount(display);
553 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
556 for (i = 0; i < sc; i++)
557 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
558 XUnmapWindow(display, tmpr->bar_window);
560 for (i = 0; i < sc; i++)
561 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
562 XMapRaised(display, tmpr->bar_window);
564 bar_enabled = !bar_enabled;
565 XSync(display, False);
566 for (i = 0; i < sc; i++)
567 for (j = 0; j < SWM_WS_MAX; j++)
568 screens[i].ws[j].restack = 1;
571 /* must be after stack */
572 for (i = 0; i < sc; i++)
573 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
580 XSetWindowAttributes wa;
581 struct swm_region *r;
584 /* do this here because the conf file is in memory */
585 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
586 /* launch external status app */
587 bar_extra_running = 1;
588 if (pipe(bar_pipe) == -1)
589 err(1, "pipe error");
590 socket_setnonblock(bar_pipe[0]);
591 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
592 if (dup2(bar_pipe[0], 0) == -1)
594 if (dup2(bar_pipe[1], 1) == -1)
596 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
597 err(1, "could not disable SIGPIPE");
598 switch (bar_pid = fork()) {
600 err(1, "cannot fork");
604 execvp(bar_argv[0], bar_argv);
605 err(1, "%s external app failed", bar_argv[0]);
607 default: /* parent */
613 bzero(&wa, sizeof wa);
614 for (i = 0; i < ScreenCount(display); i++)
615 TAILQ_FOREACH(r, &screens[i].rl, entry) {
617 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
618 wa.background_pixel =
619 screens[i].c[SWM_S_COLOR_BAR].color;
620 XChangeWindowAttributes(display, r->bar_window,
621 CWBackPixel | CWBorderPixel, &wa);
627 bar_setup(struct swm_region *r)
631 for (i = 0; bar_fonts[i] != NULL; i++) {
632 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
638 if (bar_fonts[i] == NULL)
639 errx(1, "couldn't load font");
640 bar_height = bar_fs->ascent + bar_fs->descent + 3;
642 r->bar_window = XCreateSimpleWindow(display,
643 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
644 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
645 r->s->c[SWM_S_COLOR_BAR].color);
646 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
647 XSetFont(display, bar_gc, bar_fs->fid);
648 XSelectInput(display, r->bar_window, VisibilityChangeMask);
650 XMapRaised(display, r->bar_window);
651 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
653 if (signal(SIGALRM, bar_signal) == SIG_ERR)
654 err(1, "could not install bar_signal");
659 config_win(struct ws_win *win)
663 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
664 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
665 ce.type = ConfigureNotify;
666 ce.display = display;
672 ce.height = win->g.h;
673 ce.border_width = 1; /* XXX store this! */
675 ce.override_redirect = False;
676 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
680 count_win(struct workspace *ws, int count_transient)
685 TAILQ_FOREACH(win, &ws->winlist, entry) {
686 if (count_transient == 0 && win->floating)
688 if (count_transient == 0 && win->transient)
692 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
698 quit(struct swm_region *r, union arg *args)
700 DNPRINTF(SWM_D_MISC, "quit\n");
705 restart(struct swm_region *r, union arg *args)
707 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
709 /* disable alarm because the following code may not be interrupted */
711 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
712 errx(1, "can't disable alarm");
716 XCloseDisplay(display);
717 execvp(start_argv[0], start_argv);
718 fprintf(stderr, "execvp failed\n");
724 root_to_region(Window root)
726 struct swm_region *r;
731 for (i = 0; i < ScreenCount(display); i++)
732 if (screens[i].root == root)
735 if (XQueryPointer(display, screens[i].root,
736 &rr, &cr, &x, &y, &wx, &wy, &mask) == False) {
737 /* if we can't query the pointer, grab the first region */
738 r = TAILQ_FIRST(&screens[i].rl);
740 /* otherwise, choose a region based on pointer location */
741 TAILQ_FOREACH(r, &screens[i].rl, entry) {
742 if (x > X(r) && x < X(r) + WIDTH(r) &&
743 y > Y(r) && y < Y(r) + HEIGHT(r))
748 r = TAILQ_FIRST(&screens[i].rl);
754 find_window(Window id)
759 for (i = 0; i < ScreenCount(display); i++)
760 for (j = 0; j < SWM_WS_MAX; j++)
761 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
768 spawn(struct swm_region *r, union arg *args)
770 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
772 * The double-fork construct avoids zombie processes and keeps the code
773 * clean from stupid signal handlers.
778 close(ConnectionNumber(display));
780 execvp(args->argv[0], args->argv);
781 fprintf(stderr, "execvp failed\n");
790 spawnmenu(struct swm_region *r, union arg *args)
792 DNPRINTF(SWM_D_MISC, "spawnmenu\n");
794 spawn_menu[SWM_MENU_FN] = bar_fonts[bar_fidx];
795 spawn_menu[SWM_MENU_NB] = r->s->c[SWM_S_COLOR_BAR].name;
796 spawn_menu[SWM_MENU_NF] = r->s->c[SWM_S_COLOR_BAR_FONT].name;
797 spawn_menu[SWM_MENU_SB] = r->s->c[SWM_S_COLOR_BAR_BORDER].name;
798 spawn_menu[SWM_MENU_SF] = r->s->c[SWM_S_COLOR_BAR].name;
809 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
811 for (i = 0; i < ScreenCount(display); i++)
812 for (j = 0; j < SWM_WS_MAX; j++)
813 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
814 if (win->ws->r == NULL)
817 XSetWindowBorder(display, win->id,
818 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
820 win->ws->focus = NULL;
826 focus_win(struct ws_win *win)
828 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
834 win->ws->focus = win;
835 if (win->ws->r != NULL) {
837 if (!win->got_focus) {
838 XSetWindowBorder(display, win->id,
839 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
843 XSetInputFocus(display, win->id,
844 RevertToPointerRoot, CurrentTime);
849 switchws(struct swm_region *r, union arg *args)
852 struct swm_region *this_r, *other_r;
854 struct workspace *new_ws, *old_ws;
858 new_ws = &this_r->s->ws[wsid];
860 DNPRINTF(SWM_D_WS, "switchws screen %d region %dx%d+%d+%d: "
861 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
864 if (new_ws == old_ws)
869 /* if the other workspace is hidden, switch windows */
870 /* map new window first to prevent ugly blinking */
871 TAILQ_FOREACH(win, &new_ws->winlist, entry)
872 XMapRaised(display, win->id);
874 TAILQ_FOREACH(win, &old_ws->winlist, entry)
875 XUnmapWindow(display, win->id);
880 other_r->ws = old_ws;
889 focus_win(new_ws->focus);
895 cyclews(struct swm_region *r, union arg *args)
898 struct swm_screen *s = r->s;
900 DNPRINTF(SWM_D_WS, "cyclews id %d "
901 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
902 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
908 case SWM_ARG_ID_CYCLEWS_UP:
909 if (a.id < SWM_WS_MAX - 1)
914 case SWM_ARG_ID_CYCLEWS_DOWN:
918 a.id = SWM_WS_MAX - 1;
924 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
926 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
930 } while (a.id != r->ws->idx);
934 swapwin(struct swm_region *r, union arg *args)
936 struct ws_win *target;
937 struct ws_win_list *wl;
940 DNPRINTF(SWM_D_WS, "swapwin id %d "
941 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
942 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
943 if (cur_focus == NULL)
946 wl = &cur_focus->ws->winlist;
949 case SWM_ARG_ID_SWAPPREV:
950 target = TAILQ_PREV(cur_focus, ws_win_list, entry);
951 TAILQ_REMOVE(wl, cur_focus, entry);
953 TAILQ_INSERT_TAIL(wl, cur_focus, entry);
955 TAILQ_INSERT_BEFORE(target, cur_focus, entry);
957 case SWM_ARG_ID_SWAPNEXT:
958 target = TAILQ_NEXT(cur_focus, entry);
959 TAILQ_REMOVE(wl, cur_focus, entry);
961 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
963 TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
965 case SWM_ARG_ID_SWAPMAIN:
966 target = TAILQ_FIRST(wl);
967 if (target == cur_focus)
969 TAILQ_REMOVE(wl, target, entry);
970 TAILQ_INSERT_BEFORE(cur_focus, target, entry);
971 TAILQ_REMOVE(wl, cur_focus, entry);
972 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
975 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
984 focus(struct swm_region *r, union arg *args)
986 struct ws_win *winfocus, *winlostfocus;
987 struct ws_win_list *wl;
989 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
990 if (cur_focus == NULL)
993 wl = &cur_focus->ws->winlist;
995 winlostfocus = cur_focus;
998 case SWM_ARG_ID_FOCUSPREV:
999 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1000 if (winfocus == NULL)
1001 winfocus = TAILQ_LAST(wl, ws_win_list);
1004 case SWM_ARG_ID_FOCUSNEXT:
1005 winfocus = TAILQ_NEXT(cur_focus, entry);
1006 if (winfocus == NULL)
1007 winfocus = TAILQ_FIRST(wl);
1010 case SWM_ARG_ID_FOCUSMAIN:
1011 winfocus = TAILQ_FIRST(wl);
1018 if (winfocus == winlostfocus)
1021 focus_win(winfocus);
1022 XSync(display, False);
1026 cycle_layout(struct swm_region *r, union arg *args)
1028 struct workspace *ws = r->ws;
1030 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1033 if (ws->cur_layout->l_stack == NULL)
1034 ws->cur_layout = &layouts[0];
1041 stack_config(struct swm_region *r, union arg *args)
1043 struct workspace *ws = r->ws;
1045 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1048 if (ws->cur_layout->l_config != NULL)
1049 ws->cur_layout->l_config(ws, args->id);
1051 if (args->id != SWM_ARG_ID_STACKINIT);
1057 struct swm_geometry g;
1058 struct swm_region *r;
1061 DNPRINTF(SWM_D_STACK, "stack\n");
1063 for (i = 0; i < ScreenCount(display); i++) {
1065 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1066 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1067 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1069 /* start with screen geometry, adjust for bar */
1079 r->ws->cur_layout->l_stack(r->ws, &g);
1082 XSync(display, False);
1086 stack_floater(struct ws_win *win, struct swm_region *r)
1091 bzero(&wc, sizeof wc);
1092 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1093 wc.border_width = 1;
1094 if (win->transient) {
1095 win->g.w = (double)WIDTH(r) * dialog_ratio;
1096 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1098 wc.width = win->g.w;
1099 wc.height = win->g.h;
1100 wc.x = (WIDTH(r) - win->g.w) / 2;
1101 wc.y = (HEIGHT(r) - win->g.h) / 2;
1103 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1104 win->id, wc.x, wc.y, wc.width, wc.height);
1106 XConfigureWindow(display, win->id, mask, &wc);
1110 vertical_config(struct workspace *ws, int id)
1112 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1115 case SWM_ARG_ID_STACKRESET:
1116 case SWM_ARG_ID_STACKINIT:
1117 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1118 ws->l_state.vertical_mwin = 1;
1120 case SWM_ARG_ID_MASTERSHRINK:
1121 if (ws->l_state.vertical_msize > 1)
1122 ws->l_state.vertical_msize--;
1124 case SWM_ARG_ID_MASTERGROW:
1125 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1126 ws->l_state.vertical_msize++;
1128 case SWM_ARG_ID_MASTERADD:
1129 ws->l_state.vertical_mwin++;
1131 case SWM_ARG_ID_MASTERDEL:
1132 if (ws->l_state.vertical_mwin > 0)
1133 ws->l_state.vertical_mwin--;
1141 vertical_stack(struct workspace *ws, struct swm_geometry *g) {
1143 struct swm_geometry gg = *g;
1144 struct ws_win *win, *winfocus;
1145 int i, j, split, colno, hrh, winno, main_width;
1148 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1150 if ((winno = count_win(ws, 0)) == 0)
1153 if (ws->focus == NULL)
1154 ws->focus = TAILQ_FIRST(&ws->winlist);
1155 winfocus = cur_focus ? cur_focus : ws->focus;
1157 if (ws->l_state.vertical_mwin &&
1158 winno > ws->l_state.vertical_mwin) {
1159 split = ws->l_state.vertical_mwin;
1161 main_width = (g->w / SWM_V_SLICE) *
1162 ws->l_state.vertical_msize;
1172 TAILQ_FOREACH(win, &ws->winlist, entry) {
1173 if (split && i == split) {
1174 colno = winno - split;
1175 hrh = (g->h / colno);
1176 gg.x += main_width + 2;
1177 gg.w = g->w - (main_width + 2);
1182 gg.h = (hrh + (g->h - (colno * hrh)));
1190 if (win->transient != 0 || win->floating != 0)
1191 stack_floater(win, ws->r);
1193 bzero(&wc, sizeof wc);
1194 wc.border_width = 1;
1195 win->g.x = wc.x = gg.x;
1196 win->g.y = wc.y = gg.y;
1197 win->g.w = wc.width = gg.w;
1198 win->g.h = wc.height = gg.h;
1199 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1200 XConfigureWindow(display, win->id, mask, &wc);
1202 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);
1206 XMapRaised(display, win->id);
1212 focus_win(winfocus); /* has to be done outside of the loop */
1216 horizontal_config(struct workspace *ws, int id)
1218 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1221 case SWM_ARG_ID_STACKRESET:
1222 case SWM_ARG_ID_STACKINIT:
1223 ws->l_state.horizontal_mwin = 1;
1224 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1226 case SWM_ARG_ID_MASTERSHRINK:
1227 if (ws->l_state.horizontal_msize > 1)
1228 ws->l_state.horizontal_msize--;
1230 case SWM_ARG_ID_MASTERGROW:
1231 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1232 ws->l_state.horizontal_msize++;
1234 case SWM_ARG_ID_MASTERADD:
1235 ws->l_state.horizontal_mwin++;
1237 case SWM_ARG_ID_MASTERDEL:
1238 if (ws->l_state.horizontal_mwin > 0)
1239 ws->l_state.horizontal_mwin--;
1247 horizontal_stack(struct workspace *ws, struct swm_geometry *g) {
1249 struct swm_geometry gg = *g;
1250 struct ws_win *win, *winfocus;
1251 int i, j, split, rowno, hrw, winno, main_height;
1254 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
1256 if ((winno = count_win(ws, 0)) == 0)
1259 if (ws->focus == NULL)
1260 ws->focus = TAILQ_FIRST(&ws->winlist);
1261 winfocus = cur_focus ? cur_focus : ws->focus;
1263 if (ws->l_state.horizontal_mwin &&
1264 winno > ws->l_state.horizontal_mwin) {
1265 split = ws->l_state.horizontal_mwin;
1267 main_height = (g->h / SWM_V_SLICE) *
1268 ws->l_state.horizontal_msize;
1278 TAILQ_FOREACH(win, &ws->winlist, entry) {
1279 if (split && i == split) {
1280 rowno = winno - split;
1281 hrw = (g->w / rowno);
1282 gg.y += main_height + 2;
1283 gg.h = g->h - (main_height + 2);
1288 gg.w = (hrw + (g->w - (rowno * hrw)));
1295 if (win->transient != 0 || win->floating != 0)
1296 stack_floater(win, ws->r);
1298 bzero(&wc, sizeof wc);
1299 wc.border_width = 1;
1300 win->g.x = wc.x = gg.x;
1301 win->g.y = wc.y = gg.y;
1302 win->g.w = wc.width = gg.w;
1303 win->g.h = wc.height = gg.h;
1304 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1305 XConfigureWindow(display, win->id, mask, &wc);
1308 XMapRaised(display, win->id);
1314 focus_win(winfocus); /* this has to be done outside of the loop */
1317 /* fullscreen view */
1319 max_stack(struct workspace *ws, struct swm_geometry *g) {
1321 struct swm_geometry gg = *g;
1322 struct ws_win *win, *winfocus;
1325 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1327 if (count_win(ws, 0) == 0)
1330 if (ws->focus == NULL)
1331 ws->focus = TAILQ_FIRST(&ws->winlist);
1332 winfocus = cur_focus ? cur_focus : ws->focus;
1334 TAILQ_FOREACH(win, &ws->winlist, entry) {
1335 if (win->transient != 0 || win->floating != 0) {
1336 if (win == ws->focus) {
1338 stack_floater(win, ws->r);
1339 XMapRaised(display, win->id);
1341 XUnmapWindow(display, win->id);
1343 bzero(&wc, sizeof wc);
1344 wc.border_width = 1;
1345 win->g.x = wc.x = gg.x;
1346 win->g.y = wc.y = gg.y;
1347 win->g.w = wc.width = gg.w;
1348 win->g.h = wc.height = gg.h;
1349 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1350 XConfigureWindow(display, win->id, mask, &wc);
1352 if (win == ws->focus) {
1353 XMapRaised(display, win->id);
1355 XUnmapWindow(display, win->id);
1360 focus_win(winfocus); /* has to be done outside of the loop */
1364 send_to_ws(struct swm_region *r, union arg *args)
1366 int wsid = args->id;
1367 struct ws_win *win = cur_focus;
1368 struct workspace *ws, *nws;
1369 Atom ws_idx_atom = 0;
1370 unsigned char ws_idx_str[1];
1372 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1375 nws = &win->s->ws[wsid];
1377 XUnmapWindow(display, win->id);
1379 /* find a window to focus */
1380 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1381 if (ws->focus == NULL)
1382 ws->focus = TAILQ_FIRST(&ws->winlist);
1383 if (ws->focus == win)
1386 TAILQ_REMOVE(&ws->winlist, win, entry);
1388 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1391 /* Try to update the window's workspace property */
1392 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1393 ws_idx_str[0] = (unsigned char)nws->idx;
1394 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1395 PropModeReplace, ws_idx_str, 1);
1397 if (count_win(nws, 1) == 1)
1406 wkill(struct swm_region *r, union arg *args)
1408 DNPRINTF(SWM_D_MISC, "wkill\n");
1409 if(r->ws->focus != NULL)
1410 XKillClient(display, r->ws->focus->id);
1413 /* key definitions */
1417 void (*func)(struct swm_region *r, union arg *);
1420 /* modifier key function argument */
1421 { MODKEY, XK_space, cycle_layout, {0} },
1422 { MODKEY | ShiftMask, XK_space, stack_config, {.id = SWM_ARG_ID_STACKRESET} },
1423 { MODKEY, XK_h, stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
1424 { MODKEY, XK_l, stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
1425 { MODKEY, XK_comma, stack_config, {.id = SWM_ARG_ID_MASTERADD} },
1426 { MODKEY, XK_period, stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
1427 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1428 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1429 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1430 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1431 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1432 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1433 { MODKEY, XK_p, spawnmenu, {.argv = spawn_menu} },
1434 { MODKEY | ShiftMask, XK_q, quit, {0} },
1435 { MODKEY, XK_q, restart, {0} },
1436 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1437 { MODKEY, XK_1, switchws, {.id = 0} },
1438 { MODKEY, XK_2, switchws, {.id = 1} },
1439 { MODKEY, XK_3, switchws, {.id = 2} },
1440 { MODKEY, XK_4, switchws, {.id = 3} },
1441 { MODKEY, XK_5, switchws, {.id = 4} },
1442 { MODKEY, XK_6, switchws, {.id = 5} },
1443 { MODKEY, XK_7, switchws, {.id = 6} },
1444 { MODKEY, XK_8, switchws, {.id = 7} },
1445 { MODKEY, XK_9, switchws, {.id = 8} },
1446 { MODKEY, XK_0, switchws, {.id = 9} },
1447 { MODKEY, XK_Right, cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
1448 { MODKEY, XK_Left, cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
1449 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1450 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1451 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1452 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1453 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1454 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1455 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1456 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1457 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1458 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1459 { MODKEY, XK_b, bar_toggle, {0} },
1460 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1461 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1462 { MODKEY | ShiftMask, XK_x, wkill, {0} },
1466 click(struct swm_region *r, union arg *args)
1468 DNPRINTF(SWM_D_MOUSE, "click: button: %d\n", args->id);
1483 enum { client_click, root_click };
1485 unsigned int action;
1487 unsigned int button;
1488 void (*func)(struct swm_region *r, union arg *);
1491 /* action key mouse button func args */
1492 { client_click, MODKEY, Button1, click, {.id=Button1} },
1496 updatenumlockmask(void)
1499 XModifierKeymap *modmap;
1501 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1503 modmap = XGetModifierMapping(display);
1504 for (i = 0; i < 8; i++)
1505 for (j = 0; j < modmap->max_keypermod; j++)
1506 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1507 == XKeysymToKeycode(display, XK_Num_Lock))
1508 numlockmask = (1 << i);
1510 XFreeModifiermap(modmap);
1516 unsigned int i, j, k;
1518 unsigned int modifiers[] =
1519 { 0, LockMask, numlockmask, numlockmask | LockMask };
1521 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1522 updatenumlockmask();
1524 for (k = 0; k < ScreenCount(display); k++) {
1525 if (TAILQ_EMPTY(&screens[k].rl))
1527 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
1528 for (i = 0; i < LENGTH(keys); i++) {
1529 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
1530 for (j = 0; j < LENGTH(modifiers); j++)
1531 XGrabKey(display, code,
1532 keys[i].mod | modifiers[j],
1533 screens[k].root, True,
1534 GrabModeAsync, GrabModeAsync);
1540 grabbuttons(struct ws_win *win, int focused)
1543 unsigned int modifiers[] =
1544 { 0, LockMask, numlockmask, numlockmask|LockMask };
1546 updatenumlockmask();
1547 XUngrabButton(display, AnyButton, AnyModifier, win->id);
1549 for (i = 0; i < LENGTH(buttons); i++)
1550 if (buttons[i].action == client_click)
1551 for (j = 0; j < LENGTH(modifiers); j++)
1552 XGrabButton(display, buttons[i].button,
1553 buttons[i].mask | modifiers[j],
1554 win->id, False, BUTTONMASK,
1555 GrabModeAsync, GrabModeSync, None,
1558 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
1559 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1565 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1573 XKeyEvent *ev = &e->xkey;
1575 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1577 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1578 for (i = 0; i < LENGTH(keys); i++)
1579 if (keysym == keys[i].keysym
1580 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1582 keys[i].func(root_to_region(ev->root),
1587 buttonpress(XEvent *e)
1589 XButtonPressedEvent *ev = &e->xbutton;
1594 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1596 action = root_click;
1597 if ((win = find_window(ev->window)) == NULL)
1601 action = client_click;
1604 for (i = 0; i < LENGTH(buttons); i++)
1605 if (action == buttons[i].action && buttons[i].func &&
1606 buttons[i].button == ev->button &&
1607 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
1608 buttons[i].func(root_to_region(ev->root),
1613 set_win_state(struct ws_win *win, long state)
1615 long data[] = {state, None};
1617 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1619 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1620 (unsigned char *)data, 2);
1624 manage_window(Window id)
1627 struct workspace *ws;
1631 unsigned long nitems, bytes;
1632 Atom ws_idx_atom = 0, type;
1633 unsigned char ws_idx_str[1], *prop = NULL;
1634 struct swm_region *r;
1636 if ((win = find_window(id)) != NULL)
1637 return (win); /* already being managed */
1639 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1640 errx(1, "calloc: failed to allocate memory for new window");
1642 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1644 XGetWindowProperty(display, id, ws_idx_atom, 0, 1, False, XA_STRING,
1645 &type, &format, &nitems, &bytes, &prop);
1647 XGetWindowAttributes(display, id, &win->wa);
1648 r = root_to_region(win->wa.root);
1649 /* If the window was managed before, put it in the same workspace */
1651 ws = &r->s->ws[prop[0]];
1657 win->s = r->s; /* this never changes */
1658 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
1660 /* make new win focused */
1663 XGetTransientForHint(display, win->id, &trans);
1665 win->transient = trans;
1666 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1667 (unsigned)win->id, win->transient);
1669 win->g.w = win->wa.width;
1670 win->g.h = win->wa.height;
1671 win->g.x = win->wa.x;
1672 win->g.y = win->wa.y;
1674 if (ws_idx_atom && prop == NULL) {
1675 /* set the window's workspace property if it wasn't there */
1676 ws_idx_str[0] = (unsigned char)ws->idx;
1677 XChangeProperty(display, id, ws_idx_atom, XA_STRING, 1,
1678 PropModeReplace, ws_idx_str, 1);
1682 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);
1685 /* XXX make this a table */
1686 bzero(&ch, sizeof ch);
1687 if (XGetClassHint(display, win->id, &ch)) {
1688 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1689 if (!strcmp(ch.res_class, "MPlayer") && !strcmp(ch.res_name, "xv")) {
1693 XFree(ch.res_class);
1698 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
1699 PropertyChangeMask | StructureNotifyMask);
1701 set_win_state(win, NormalState);
1707 configurerequest(XEvent *e)
1709 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1714 if ((win = find_window(ev->window)) == NULL)
1718 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
1721 fprintf(stderr, "configurerequest: new window: %lu x %d y %d w %d h %d bw %d s %d sm %d\n",
1722 ev->window, ev->x, ev->y, ev->width, ev->height, ev->border_width, ev->above, ev->detail);
1724 bzero(&wc, sizeof wc);
1727 wc.width = ev->width;
1728 wc.height = ev->height;
1729 wc.border_width = ev->border_width;
1730 wc.sibling = ev->above;
1731 wc.stack_mode = ev->detail;
1732 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
1735 fprintf(stderr, "configurerequest: change window: %lu\n",
1738 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
1740 if (win->floating) {
1741 if (ev->value_mask & CWX)
1743 if (ev->value_mask & CWY)
1745 if (ev->value_mask & CWWidth)
1746 win->g.w = ev->width;
1747 if (ev->value_mask & CWHeight)
1748 win->g.h = ev->height;
1749 if (win->ws->r != NULL) {
1750 /* this seems to be full screen */
1751 if (win->g.w > WIDTH(win->ws->r)) {
1756 if (win->g.h > HEIGHT(win->ws->r)) {
1762 if ((ev->value_mask & (CWX|CWY)) &&
1763 !(ev->value_mask & (CWWidth|CWHeight)))
1765 XMoveResizeWindow(display, win->id,
1766 win->g.x, win->g.y, win->g.w, win->g.h);
1773 configurenotify(XEvent *e)
1775 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1776 e->xconfigure.window);
1780 destroynotify(XEvent *e)
1783 XDestroyWindowEvent *ev = &e->xdestroywindow;
1784 struct workspace *ws;
1786 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1788 if ((win = find_window(ev->window)) != NULL) {
1790 /* find a window to focus */
1791 if (ws->focus == win)
1792 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1793 if (ws->focus == NULL)
1794 ws->focus = TAILQ_FIRST(&ws->winlist);
1795 if (ws->focus == win)
1797 if (cur_focus == win)
1798 focus_win(ws->focus);
1800 TAILQ_REMOVE(&ws->winlist, win, entry);
1801 set_win_state(win, WithdrawnState);
1808 enternotify(XEvent *e)
1810 XCrossingEvent *ev = &e->xcrossing;
1813 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1816 /* eat event(r) to prevent autofocus */
1821 if ((win = find_window(ev->window)) != NULL)
1828 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
1832 mappingnotify(XEvent *e)
1834 XMappingEvent *ev = &e->xmapping;
1836 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1838 XRefreshKeyboardMapping(ev);
1839 if (ev->request == MappingKeyboard)
1844 maprequest(XEvent *e)
1846 XMapRequestEvent *ev = &e->xmaprequest;
1847 XWindowAttributes wa;
1849 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1850 e->xmaprequest.window);
1852 if (!XGetWindowAttributes(display, ev->window, &wa))
1854 if (wa.override_redirect)
1856 manage_window(e->xmaprequest.window);
1861 propertynotify(XEvent *e)
1863 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1864 e->xproperty.window);
1868 unmapnotify(XEvent *e)
1870 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1874 visibilitynotify(XEvent *e)
1877 struct swm_region *r;
1879 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
1880 e->xvisibility.window);
1881 if (e->xvisibility.state == VisibilityUnobscured)
1882 for (i = 0; i < ScreenCount(display); i++)
1883 TAILQ_FOREACH(r, &screens[i].rl, entry)
1884 if (e->xvisibility.window == r->bar_window)
1888 void (*handler[LASTEvent])(XEvent *) = {
1890 [KeyPress] = keypress,
1891 [ButtonPress] = buttonpress,
1892 [ConfigureRequest] = configurerequest,
1893 [ConfigureNotify] = configurenotify,
1894 [DestroyNotify] = destroynotify,
1895 [EnterNotify] = enternotify,
1896 [FocusIn] = focusin,
1897 [MappingNotify] = mappingnotify,
1898 [MapRequest] = maprequest,
1899 [PropertyNotify] = propertynotify,
1900 [UnmapNotify] = unmapnotify,
1901 [VisibilityNotify] = visibilitynotify,
1905 xerror_start(Display *d, XErrorEvent *ee)
1912 xerror(Display *d, XErrorEvent *ee)
1914 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1922 xerrorxlib = XSetErrorHandler(xerror_start);
1924 /* this causes an error if some other window manager is running */
1925 XSelectInput(display, DefaultRootWindow(display),
1926 SubstructureRedirectMask);
1927 XSync(display, False);
1931 XSetErrorHandler(xerror);
1932 XSync(display, False);
1941 unsigned char *p = NULL;
1942 unsigned long n, extra;
1945 astate = XInternAtom(display, "WM_STATE", False);
1946 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
1947 &real, &format, &n, &extra, (unsigned char **)&p);
1948 if (status != Success)
1957 new_region(struct swm_screen *s, struct workspace *ws,
1958 int x, int y, int w, int h)
1960 struct swm_region *r;
1962 DNPRINTF(SWM_D_MISC, "new region on screen %d: %dx%d (%d, %d)\n",
1963 s->idx, x, y, w, h);
1965 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
1966 errx(1, "calloc: failed to allocate memory for screen");
1975 TAILQ_INSERT_TAIL(&s->rl, r, entry);
1982 #ifdef SWM_XRR_HAS_CRTC
1984 XRRScreenResources *sr;
1986 #endif /* SWM_XRR_HAS_CRTC */
1987 Window d1, d2, *wins = NULL;
1988 XWindowAttributes wa;
1989 struct swm_region *r;
1991 int errorbase, major, minor;
1992 int ncrtc = 0, w = 0;
1994 struct workspace *ws;
1998 if ((screens = calloc(ScreenCount(display),
1999 sizeof(struct swm_screen))) == NULL)
2000 errx(1, "calloc: screens");
2002 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2005 /* map physical screens */
2006 for (i = 0; i < ScreenCount(display); i++) {
2007 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
2009 TAILQ_INIT(&screens[i].rl);
2010 screens[i].root = RootWindow(display, i);
2012 /* set default colors */
2013 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
2014 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
2015 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
2016 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
2017 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
2019 /* init all workspaces */
2020 for (j = 0; j < SWM_WS_MAX; j++) {
2021 ws = &screens[i].ws[j];
2026 TAILQ_INIT(&ws->winlist);
2028 for (k = 0; layouts[k].l_stack != NULL; k++)
2029 if (layouts[k].l_config != NULL)
2030 layouts[k].l_config(ws,
2031 SWM_ARG_ID_STACKINIT);
2032 ws->cur_layout = &layouts[0];
2035 /* map virtual screens onto physical screens */
2036 screens[i].xrandr_support = XRRQueryExtension(display,
2037 &xrandr_eventbase, &errorbase);
2038 if (screens[i].xrandr_support)
2039 if (XRRQueryVersion(display, &major, &minor) &&
2041 screens[i].xrandr_support = 0;
2043 #if 0 /* not ready for dynamic screen changes */
2044 if (screens[i].xrandr_support)
2045 XRRSelectInput(display,
2047 RRScreenChangeNotifyMask);
2050 /* grab existing windows (before we build the bars)*/
2051 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
2054 #ifdef SWM_XRR_HAS_CRTC
2055 sr = XRRGetScreenResources(display, screens[i].root);
2057 new_region(&screens[i], &screens[i].ws[w],
2058 0, 0, DisplayWidth(display, i),
2059 DisplayHeight(display, i));
2063 for (c = 0, ci = NULL; c < ncrtc; c++) {
2064 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
2065 if (ci->noutput == 0)
2068 if (ci != NULL && ci->mode == None)
2069 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2070 DisplayWidth(display, i),
2071 DisplayHeight(display, i));
2073 new_region(&screens[i], &screens[i].ws[w],
2074 ci->x, ci->y, ci->width, ci->height);
2078 XRRFreeCrtcInfo(ci);
2079 XRRFreeScreenResources(sr);
2081 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2082 DisplayWidth(display, i),
2083 DisplayHeight(display, i));
2084 #endif /* SWM_XRR_HAS_CRTC */
2086 /* attach windows to a region */
2087 /* normal windows */
2088 if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
2089 errx(1, "no regions on screen %d", i);
2091 for (i = 0; i < no; i++) {
2092 XGetWindowAttributes(display, wins[i], &wa);
2093 if (!XGetWindowAttributes(display, wins[i], &wa) ||
2094 wa.override_redirect ||
2095 XGetTransientForHint(display, wins[i], &d1))
2098 if (wa.map_state == IsViewable ||
2099 getstate(wins[i]) == NormalState)
2100 manage_window(wins[i]);
2102 /* transient windows */
2103 for (i = 0; i < no; i++) {
2104 if (!XGetWindowAttributes(display, wins[i], &wa))
2107 if (XGetTransientForHint(display, wins[i], &d1) &&
2108 (wa.map_state == IsViewable || getstate(wins[i]) ==
2110 manage_window(wins[i]);
2120 main(int argc, char *argv[])
2123 char conf[PATH_MAX], *cfile = NULL;
2130 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
2131 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2132 warnx("no locale support");
2134 if (!(display = XOpenDisplay(0)))
2135 errx(1, "can not open display");
2138 errx(1, "other wm running");
2140 astate = XInternAtom(display, "WM_STATE", False);
2142 /* look for local and global conf file */
2143 pwd = getpwuid(getuid());
2145 errx(1, "invalid user %d", getuid());
2149 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
2150 if (stat(conf, &sb) != -1) {
2151 if (S_ISREG(sb.st_mode))
2154 /* try global conf file */
2155 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
2156 if (!stat(conf, &sb))
2157 if (S_ISREG(sb.st_mode))
2164 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2169 xfd = ConnectionNumber(display);
2173 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
2175 errx(1, "select failed");
2180 while(XPending(display)) {
2181 XNextEvent(display, &e);
2182 if (handler[e.type])
2183 handler[e.type](&e);
2187 XCloseDisplay(display);