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;
153 int bar_extra_running = 0;
161 char *bar_fonts[] = {
162 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
163 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
167 /* terminal + args */
168 char *spawn_term[] = { "xterm", NULL };
169 char *spawn_menu[] = { "dmenu_run", "-fn", NULL,
170 "-nb", NULL, "-nf", NULL, "-sb", NULL, "-sf", NULL, NULL };
172 #define SWM_MENU_FN (2)
173 #define SWM_MENU_NB (4)
174 #define SWM_MENU_NF (6)
175 #define SWM_MENU_SB (8)
176 #define SWM_MENU_SF (10)
178 /* layout manager data */
179 struct swm_geometry {
189 /* virtual "screens" */
191 TAILQ_ENTRY(swm_region) entry;
192 struct swm_geometry g;
194 struct workspace *ws; /* current workspace on this region */
195 struct swm_screen *s; /* screen idx */
197 TAILQ_HEAD(swm_region_list, swm_region);
200 TAILQ_ENTRY(ws_win) entry;
202 struct swm_geometry g;
206 struct workspace *ws; /* always valid */
207 struct swm_screen *s; /* always valid, never changes */
208 XWindowAttributes wa;
210 TAILQ_HEAD(ws_win_list, ws_win);
212 /* layout handlers */
214 void vertical_config(struct workspace *, int);
215 void vertical_stack(struct workspace *, struct swm_geometry *);
216 void horizontal_config(struct workspace *, int);
217 void horizontal_stack(struct workspace *, struct swm_geometry *);
218 void max_stack(struct workspace *, struct swm_geometry *);
220 void grabbuttons(struct ws_win *, int);
223 void (*l_stack)(struct workspace *, struct swm_geometry *);
224 void (*l_config)(struct workspace *, int);
226 /* stack, configure */
227 { vertical_stack, vertical_config},
228 { horizontal_stack, horizontal_config},
233 #define SWM_H_SLICE (32)
234 #define SWM_V_SLICE (32)
236 /* define work spaces */
238 int idx; /* workspace index */
239 int restack; /* restack on switch */
240 struct layout *cur_layout; /* current layout handlers */
241 struct ws_win *focus; /* may be NULL */
242 struct swm_region *r; /* may be NULL */
243 struct ws_win_list winlist; /* list of windows in ws */
247 int horizontal_msize;
254 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
255 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
257 /* physical screen mapping */
258 #define SWM_WS_MAX (10) /* XXX Too small? */
260 int idx; /* screen index */
261 struct swm_region_list rl; /* list of regions on this screen */
264 struct workspace ws[SWM_WS_MAX];
270 } c[SWM_S_COLOR_MAX];
272 struct swm_screen *screens;
275 struct ws_win *cur_focus = NULL;
277 /* args to functions */
280 #define SWM_ARG_ID_FOCUSNEXT (0)
281 #define SWM_ARG_ID_FOCUSPREV (1)
282 #define SWM_ARG_ID_FOCUSMAIN (2)
283 #define SWM_ARG_ID_SWAPNEXT (3)
284 #define SWM_ARG_ID_SWAPPREV (4)
285 #define SWM_ARG_ID_SWAPMAIN (5)
286 #define SWM_ARG_ID_MASTERSHRINK (6)
287 #define SWM_ARG_ID_MASTERGROW (7)
288 #define SWM_ARG_ID_MASTERADD (8)
289 #define SWM_ARG_ID_MASTERDEL (9)
290 #define SWM_ARG_ID_STACKRESET (10)
291 #define SWM_ARG_ID_STACKINIT (11)
292 #define SWM_ARG_ID_CYCLEWS_UP (12)
293 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
298 name_to_color(char *colorname)
302 XColor screen_def, exact_def;
303 unsigned long result = 0;
304 char cname[32] = "#";
306 cmap = DefaultColormap(display, screens[0].idx);
307 status = XAllocNamedColor(display, cmap, colorname,
308 &screen_def, &exact_def);
310 strlcat(cname, colorname + 2, sizeof cname - 1);
311 status = XAllocNamedColor(display, cmap, cname, &screen_def,
315 result = screen_def.pixel;
317 fprintf(stderr, "color '%s' not found.\n", colorname);
323 setscreencolor(char *val, int i, int c)
325 if (i > 0 && i <= ScreenCount(display)) {
326 screens[i - 1].c[c].color = name_to_color(val);
327 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
329 } else if (i == -1) {
330 for (i = 0; i < ScreenCount(display); i++)
331 screens[i].c[c].color = name_to_color(val);
332 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
335 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
336 i, ScreenCount(display));
340 varmatch(char *var, char *name, int *index)
345 i = strncmp(var, name, 255);
352 p = var + strlen(name);
355 bzero(buf, sizeof buf);
357 while (isdigit(*p) && i < sizeof buf)
359 if (i == 0 || i >= sizeof buf || *p != ']')
361 *index = strtonum(buf, 0, 99, NULL);
365 /* conf file stuff */
366 #define SWM_CONF_WS "\n= \t"
367 #define SWM_CONF_FILE "scrotwm.conf"
369 conf_load(char *filename)
372 char *line, *cp, *var, *val;
373 size_t len, lineno = 0;
376 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
378 if (filename == NULL)
381 if ((config = fopen(filename, "r")) == NULL)
384 for (sc = ScreenCount(display);;) {
385 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
389 cp += (long)strspn(cp, SWM_CONF_WS);
395 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
397 cp += (long)strspn(cp, SWM_CONF_WS);
398 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
401 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
404 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
405 bar_enabled = atoi(val);
406 else if (!varmatch(var, "bar_border", &i))
407 setscreencolor(val, i, SWM_S_COLOR_BAR_BORDER);
408 else if (!varmatch(var, "bar_color", &i))
409 setscreencolor(val, i, SWM_S_COLOR_BAR);
410 else if (!varmatch(var, "bar_font_color", &i))
411 setscreencolor(val, i, SWM_S_COLOR_BAR_FONT);
412 else if (!strncmp(var, "bar_font", strlen("bar_font")))
413 asprintf(&bar_fonts[0], "%s", val);
414 else if (!strncmp(var, "bar_action", strlen("bar_action")))
415 asprintf(&bar_argv[0], "%s", val);
416 else if (!strncmp(var, "bar_delay", strlen("bar_delay")))
417 bar_delay = atoi(val);
423 if (!varmatch(var, "color_focus", &i))
424 setscreencolor(val, i, SWM_S_COLOR_FOCUS);
425 else if (!varmatch(var, "color_unfocus", &i))
426 setscreencolor(val, i, SWM_S_COLOR_UNFOCUS);
427 else if (!strncmp(var, "cycle_empty", strlen("cycle_empty")))
428 cycle_visible = atoi(val);
429 else if (!strncmp(var, "cycle_visible", strlen("cycle_visible")))
430 cycle_visible = atoi(val);
436 if (!strncmp(var, "dialog_ratio",
437 strlen("dialog_ratio"))) {
438 dialog_ratio = atof(val);
439 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
446 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
447 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
459 errx(1, "invalid conf file entry: %s=%s", var, val);
463 socket_setnonblock(int fd)
467 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
468 err(1, "fcntl F_GETFL");
470 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
471 err(1, "fcntl F_SETFL");
475 bar_print(struct swm_region *r, char *s)
477 XClearWindow(display, r->bar_window);
478 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
479 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
488 bzero(bar_pipe, sizeof bar_pipe);
491 kill(bar_pid, SIGTERM);
494 strlcpy(bar_ext, "", sizeof bar_ext);
503 struct swm_region *r;
507 char loc[SWM_BAR_MAX];
510 if (bar_enabled == 0)
513 if (bar_extra && bar_extra_running) {
514 /* ignore short reads; it'll correct itself */
515 while ((b = fgetln(stdin, &len)) != NULL)
516 if (b && b[len - 1] == '\n') {
518 strlcpy(bar_ext, b, sizeof bar_ext);
520 if (b == NULL && errno != EAGAIN) {
521 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
522 errno, strerror(errno));
526 strlcpy(bar_ext, "", sizeof bar_ext);
529 localtime_r(&tmt, &tm);
530 strftime(s, sizeof s, "%a %b %d %R %Z %Y", &tm);
531 for (i = 0; i < ScreenCount(display); i++) {
533 TAILQ_FOREACH(r, &screens[i].rl, entry) {
534 snprintf(loc, sizeof loc, "%s %d:%d %s",
535 s, x++, r->ws->idx + 1, bar_ext);
539 XSync(display, False);
550 bar_toggle(struct swm_region *r, union arg *args)
552 struct swm_region *tmpr;
553 int i, j, sc = ScreenCount(display);
555 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
558 for (i = 0; i < sc; i++)
559 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
560 XUnmapWindow(display, tmpr->bar_window);
562 for (i = 0; i < sc; i++)
563 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
564 XMapRaised(display, tmpr->bar_window);
566 bar_enabled = !bar_enabled;
567 XSync(display, False);
568 for (i = 0; i < sc; i++)
569 for (j = 0; j < SWM_WS_MAX; j++)
570 screens[i].ws[j].restack = 1;
573 /* must be after stack */
574 for (i = 0; i < sc; i++)
575 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
582 XSetWindowAttributes wa;
583 struct swm_region *r;
586 /* do this here because the conf file is in memory */
587 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
588 /* launch external status app */
589 bar_extra_running = 1;
590 if (pipe(bar_pipe) == -1)
591 err(1, "pipe error");
592 socket_setnonblock(bar_pipe[0]);
593 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
594 if (dup2(bar_pipe[0], 0) == -1)
596 if (dup2(bar_pipe[1], 1) == -1)
598 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
599 err(1, "could not disable SIGPIPE");
600 switch (bar_pid = fork()) {
602 err(1, "cannot fork");
606 execvp(bar_argv[0], bar_argv);
607 err(1, "%s external app failed", bar_argv[0]);
609 default: /* parent */
615 bzero(&wa, sizeof wa);
616 for (i = 0; i < ScreenCount(display); i++)
617 TAILQ_FOREACH(r, &screens[i].rl, entry) {
619 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
620 wa.background_pixel =
621 screens[i].c[SWM_S_COLOR_BAR].color;
622 XChangeWindowAttributes(display, r->bar_window,
623 CWBackPixel | CWBorderPixel, &wa);
629 bar_setup(struct swm_region *r)
633 for (i = 0; bar_fonts[i] != NULL; i++) {
634 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
640 if (bar_fonts[i] == NULL)
641 errx(1, "couldn't load font");
642 bar_height = bar_fs->ascent + bar_fs->descent + 3;
644 r->bar_window = XCreateSimpleWindow(display,
645 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
646 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
647 r->s->c[SWM_S_COLOR_BAR].color);
648 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
649 XSetFont(display, bar_gc, bar_fs->fid);
650 XSelectInput(display, r->bar_window, VisibilityChangeMask);
652 XMapRaised(display, r->bar_window);
653 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
655 if (signal(SIGALRM, bar_signal) == SIG_ERR)
656 err(1, "could not install bar_signal");
661 config_win(struct ws_win *win)
665 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
666 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
667 ce.type = ConfigureNotify;
668 ce.display = display;
674 ce.height = win->g.h;
675 ce.border_width = 1; /* XXX store this! */
677 ce.override_redirect = False;
678 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
682 count_win(struct workspace *ws, int count_transient)
687 TAILQ_FOREACH(win, &ws->winlist, entry) {
688 if (count_transient == 0 && win->floating)
690 if (count_transient == 0 && win->transient)
694 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
700 quit(struct swm_region *r, union arg *args)
702 DNPRINTF(SWM_D_MISC, "quit\n");
707 restart(struct swm_region *r, union arg *args)
709 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
711 /* disable alarm because the following code may not be interrupted */
713 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
714 errx(1, "can't disable alarm");
718 XCloseDisplay(display);
719 execvp(start_argv[0], start_argv);
720 fprintf(stderr, "execvp failed\n");
726 root_to_region(Window root)
728 struct swm_region *r;
733 for (i = 0; i < ScreenCount(display); i++)
734 if (screens[i].root == root)
737 if (XQueryPointer(display, screens[i].root,
738 &rr, &cr, &x, &y, &wx, &wy, &mask) == False) {
739 /* if we can't query the pointer, grab the first region */
740 r = TAILQ_FIRST(&screens[i].rl);
742 /* otherwise, choose a region based on pointer location */
743 TAILQ_FOREACH(r, &screens[i].rl, entry) {
744 if (x > X(r) && x < X(r) + WIDTH(r) &&
745 y > Y(r) && y < Y(r) + HEIGHT(r))
750 r = TAILQ_FIRST(&screens[i].rl);
756 find_window(Window id)
761 for (i = 0; i < ScreenCount(display); i++)
762 for (j = 0; j < SWM_WS_MAX; j++)
763 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
770 spawn(struct swm_region *r, union arg *args)
772 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
774 * The double-fork construct avoids zombie processes and keeps the code
775 * clean from stupid signal handlers.
780 close(ConnectionNumber(display));
782 execvp(args->argv[0], args->argv);
783 fprintf(stderr, "execvp failed\n");
792 spawnmenu(struct swm_region *r, union arg *args)
794 DNPRINTF(SWM_D_MISC, "spawnmenu\n");
796 spawn_menu[SWM_MENU_FN] = bar_fonts[bar_fidx];
797 spawn_menu[SWM_MENU_NB] = r->s->c[SWM_S_COLOR_BAR].name;
798 spawn_menu[SWM_MENU_NF] = r->s->c[SWM_S_COLOR_BAR_FONT].name;
799 spawn_menu[SWM_MENU_SB] = r->s->c[SWM_S_COLOR_BAR_BORDER].name;
800 spawn_menu[SWM_MENU_SF] = r->s->c[SWM_S_COLOR_BAR].name;
811 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
813 for (i = 0; i < ScreenCount(display); i++)
814 for (j = 0; j < SWM_WS_MAX; j++)
815 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
816 if (win->ws->r == NULL)
819 XSetWindowBorder(display, win->id,
820 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
822 win->ws->focus = NULL;
828 focus_win(struct ws_win *win)
830 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
836 win->ws->focus = win;
837 if (win->ws->r != NULL) {
839 if (!win->got_focus) {
840 XSetWindowBorder(display, win->id,
841 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
845 XSetInputFocus(display, win->id,
846 RevertToPointerRoot, CurrentTime);
851 switchws(struct swm_region *r, union arg *args)
854 struct swm_region *this_r, *other_r;
856 struct workspace *new_ws, *old_ws;
860 new_ws = &this_r->s->ws[wsid];
862 DNPRINTF(SWM_D_WS, "switchws screen %d region %dx%d+%d+%d: "
863 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
866 if (new_ws == old_ws)
871 /* if the other workspace is hidden, switch windows */
872 /* map new window first to prevent ugly blinking */
873 TAILQ_FOREACH(win, &new_ws->winlist, entry)
874 XMapRaised(display, win->id);
876 TAILQ_FOREACH(win, &old_ws->winlist, entry)
877 XUnmapWindow(display, win->id);
882 other_r->ws = old_ws;
891 focus_win(new_ws->focus);
897 cyclews(struct swm_region *r, union arg *args)
900 struct swm_screen *s = r->s;
902 DNPRINTF(SWM_D_WS, "cyclews id %d "
903 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
904 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
910 case SWM_ARG_ID_CYCLEWS_UP:
911 if (a.id < SWM_WS_MAX - 1)
916 case SWM_ARG_ID_CYCLEWS_DOWN:
920 a.id = SWM_WS_MAX - 1;
926 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
928 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
932 } while (a.id != r->ws->idx);
936 swapwin(struct swm_region *r, union arg *args)
938 struct ws_win *target;
939 struct ws_win_list *wl;
942 DNPRINTF(SWM_D_WS, "swapwin id %d "
943 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
944 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
945 if (cur_focus == NULL)
948 wl = &cur_focus->ws->winlist;
951 case SWM_ARG_ID_SWAPPREV:
952 target = TAILQ_PREV(cur_focus, ws_win_list, entry);
953 TAILQ_REMOVE(wl, cur_focus, entry);
955 TAILQ_INSERT_TAIL(wl, cur_focus, entry);
957 TAILQ_INSERT_BEFORE(target, cur_focus, entry);
959 case SWM_ARG_ID_SWAPNEXT:
960 target = TAILQ_NEXT(cur_focus, entry);
961 TAILQ_REMOVE(wl, cur_focus, entry);
963 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
965 TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
967 case SWM_ARG_ID_SWAPMAIN:
968 target = TAILQ_FIRST(wl);
969 if (target == cur_focus)
971 TAILQ_REMOVE(wl, target, entry);
972 TAILQ_INSERT_BEFORE(cur_focus, target, entry);
973 TAILQ_REMOVE(wl, cur_focus, entry);
974 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
977 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
986 focus(struct swm_region *r, union arg *args)
988 struct ws_win *winfocus, *winlostfocus;
989 struct ws_win_list *wl;
991 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
992 if (cur_focus == NULL)
995 wl = &cur_focus->ws->winlist;
997 winlostfocus = cur_focus;
1000 case SWM_ARG_ID_FOCUSPREV:
1001 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1002 if (winfocus == NULL)
1003 winfocus = TAILQ_LAST(wl, ws_win_list);
1006 case SWM_ARG_ID_FOCUSNEXT:
1007 winfocus = TAILQ_NEXT(cur_focus, entry);
1008 if (winfocus == NULL)
1009 winfocus = TAILQ_FIRST(wl);
1012 case SWM_ARG_ID_FOCUSMAIN:
1013 winfocus = TAILQ_FIRST(wl);
1020 if (winfocus == winlostfocus)
1023 focus_win(winfocus);
1024 XSync(display, False);
1028 cycle_layout(struct swm_region *r, union arg *args)
1030 struct workspace *ws = r->ws;
1032 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1035 if (ws->cur_layout->l_stack == NULL)
1036 ws->cur_layout = &layouts[0];
1043 stack_config(struct swm_region *r, union arg *args)
1045 struct workspace *ws = r->ws;
1047 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1050 if (ws->cur_layout->l_config != NULL)
1051 ws->cur_layout->l_config(ws, args->id);
1053 if (args->id != SWM_ARG_ID_STACKINIT);
1059 struct swm_geometry g;
1060 struct swm_region *r;
1063 DNPRINTF(SWM_D_STACK, "stack\n");
1065 for (i = 0; i < ScreenCount(display); i++) {
1067 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1068 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1069 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1071 /* start with screen geometry, adjust for bar */
1081 r->ws->cur_layout->l_stack(r->ws, &g);
1084 XSync(display, False);
1088 stack_floater(struct ws_win *win, struct swm_region *r)
1093 bzero(&wc, sizeof wc);
1094 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1095 wc.border_width = 1;
1096 if (win->transient) {
1097 win->g.w = (double)WIDTH(r) * dialog_ratio;
1098 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1100 wc.width = win->g.w;
1101 wc.height = win->g.h;
1102 wc.x = (WIDTH(r) - win->g.w) / 2;
1103 wc.y = (HEIGHT(r) - win->g.h) / 2;
1105 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1106 win->id, wc.x, wc.y, wc.width, wc.height);
1108 XConfigureWindow(display, win->id, mask, &wc);
1112 vertical_config(struct workspace *ws, int id)
1114 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1117 case SWM_ARG_ID_STACKRESET:
1118 case SWM_ARG_ID_STACKINIT:
1119 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1120 ws->l_state.vertical_mwin = 1;
1122 case SWM_ARG_ID_MASTERSHRINK:
1123 if (ws->l_state.vertical_msize > 1)
1124 ws->l_state.vertical_msize--;
1126 case SWM_ARG_ID_MASTERGROW:
1127 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1128 ws->l_state.vertical_msize++;
1130 case SWM_ARG_ID_MASTERADD:
1131 ws->l_state.vertical_mwin++;
1133 case SWM_ARG_ID_MASTERDEL:
1134 if (ws->l_state.vertical_mwin > 0)
1135 ws->l_state.vertical_mwin--;
1143 vertical_stack(struct workspace *ws, struct swm_geometry *g) {
1145 struct swm_geometry gg = *g;
1146 struct ws_win *win, *winfocus;
1147 int i, j, split, colno, hrh, winno, main_width;
1150 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1152 if ((winno = count_win(ws, 0)) == 0)
1155 if (ws->focus == NULL)
1156 ws->focus = TAILQ_FIRST(&ws->winlist);
1157 winfocus = cur_focus ? cur_focus : ws->focus;
1159 if (ws->l_state.vertical_mwin &&
1160 winno > ws->l_state.vertical_mwin) {
1161 split = ws->l_state.vertical_mwin;
1163 main_width = (g->w / SWM_V_SLICE) *
1164 ws->l_state.vertical_msize;
1174 TAILQ_FOREACH(win, &ws->winlist, entry) {
1175 if (split && i == split) {
1176 colno = winno - split;
1177 hrh = (g->h / colno);
1178 gg.x += main_width + 2;
1179 gg.w = g->w - (main_width + 2);
1184 gg.h = (hrh + (g->h - (colno * hrh)));
1192 if (win->transient != 0 || win->floating != 0)
1193 stack_floater(win, ws->r);
1195 bzero(&wc, sizeof wc);
1196 wc.border_width = 1;
1197 win->g.x = wc.x = gg.x;
1198 win->g.y = wc.y = gg.y;
1199 win->g.w = wc.width = gg.w;
1200 win->g.h = wc.height = gg.h;
1201 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1202 XConfigureWindow(display, win->id, mask, &wc);
1204 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);
1208 XMapRaised(display, win->id);
1214 focus_win(winfocus); /* has to be done outside of the loop */
1218 horizontal_config(struct workspace *ws, int id)
1220 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1223 case SWM_ARG_ID_STACKRESET:
1224 case SWM_ARG_ID_STACKINIT:
1225 ws->l_state.horizontal_mwin = 1;
1226 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1228 case SWM_ARG_ID_MASTERSHRINK:
1229 if (ws->l_state.horizontal_msize > 1)
1230 ws->l_state.horizontal_msize--;
1232 case SWM_ARG_ID_MASTERGROW:
1233 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1234 ws->l_state.horizontal_msize++;
1236 case SWM_ARG_ID_MASTERADD:
1237 ws->l_state.horizontal_mwin++;
1239 case SWM_ARG_ID_MASTERDEL:
1240 if (ws->l_state.horizontal_mwin > 0)
1241 ws->l_state.horizontal_mwin--;
1249 horizontal_stack(struct workspace *ws, struct swm_geometry *g) {
1251 struct swm_geometry gg = *g;
1252 struct ws_win *win, *winfocus;
1253 int i, j, split, rowno, hrw, winno, main_height;
1256 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
1258 if ((winno = count_win(ws, 0)) == 0)
1261 if (ws->focus == NULL)
1262 ws->focus = TAILQ_FIRST(&ws->winlist);
1263 winfocus = cur_focus ? cur_focus : ws->focus;
1265 if (ws->l_state.horizontal_mwin &&
1266 winno > ws->l_state.horizontal_mwin) {
1267 split = ws->l_state.horizontal_mwin;
1269 main_height = (g->h / SWM_V_SLICE) *
1270 ws->l_state.horizontal_msize;
1280 TAILQ_FOREACH(win, &ws->winlist, entry) {
1281 if (split && i == split) {
1282 rowno = winno - split;
1283 hrw = (g->w / rowno);
1284 gg.y += main_height + 2;
1285 gg.h = g->h - (main_height + 2);
1290 gg.w = (hrw + (g->w - (rowno * hrw)));
1297 if (win->transient != 0 || win->floating != 0)
1298 stack_floater(win, ws->r);
1300 bzero(&wc, sizeof wc);
1301 wc.border_width = 1;
1302 win->g.x = wc.x = gg.x;
1303 win->g.y = wc.y = gg.y;
1304 win->g.w = wc.width = gg.w;
1305 win->g.h = wc.height = gg.h;
1306 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1307 XConfigureWindow(display, win->id, mask, &wc);
1310 XMapRaised(display, win->id);
1316 focus_win(winfocus); /* this has to be done outside of the loop */
1319 /* fullscreen view */
1321 max_stack(struct workspace *ws, struct swm_geometry *g) {
1323 struct swm_geometry gg = *g;
1324 struct ws_win *win, *winfocus;
1327 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1329 if (count_win(ws, 0) == 0)
1332 if (ws->focus == NULL)
1333 ws->focus = TAILQ_FIRST(&ws->winlist);
1334 winfocus = cur_focus ? cur_focus : ws->focus;
1336 TAILQ_FOREACH(win, &ws->winlist, entry) {
1337 if (win->transient != 0 || win->floating != 0) {
1338 if (win == ws->focus) {
1340 stack_floater(win, ws->r);
1341 XMapRaised(display, win->id);
1343 XUnmapWindow(display, win->id);
1345 bzero(&wc, sizeof wc);
1346 wc.border_width = 1;
1347 win->g.x = wc.x = gg.x;
1348 win->g.y = wc.y = gg.y;
1349 win->g.w = wc.width = gg.w;
1350 win->g.h = wc.height = gg.h;
1351 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1352 XConfigureWindow(display, win->id, mask, &wc);
1354 if (win == ws->focus) {
1355 XMapRaised(display, win->id);
1357 XUnmapWindow(display, win->id);
1362 focus_win(winfocus); /* has to be done outside of the loop */
1366 send_to_ws(struct swm_region *r, union arg *args)
1368 int wsid = args->id;
1369 struct ws_win *win = cur_focus;
1370 struct workspace *ws, *nws;
1371 Atom ws_idx_atom = 0;
1372 unsigned char ws_idx_str[1];
1374 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1377 nws = &win->s->ws[wsid];
1379 XUnmapWindow(display, win->id);
1381 /* find a window to focus */
1382 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1383 if (ws->focus == NULL)
1384 ws->focus = TAILQ_FIRST(&ws->winlist);
1385 if (ws->focus == win)
1388 TAILQ_REMOVE(&ws->winlist, win, entry);
1390 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1393 /* Try to update the window's workspace property */
1394 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1395 ws_idx_str[0] = (unsigned char)nws->idx;
1396 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1397 PropModeReplace, ws_idx_str, 1);
1399 if (count_win(nws, 1) == 1)
1408 wkill(struct swm_region *r, union arg *args)
1410 DNPRINTF(SWM_D_MISC, "wkill\n");
1411 if(r->ws->focus != NULL)
1412 XKillClient(display, r->ws->focus->id);
1415 /* key definitions */
1419 void (*func)(struct swm_region *r, union arg *);
1422 /* modifier key function argument */
1423 { MODKEY, XK_space, cycle_layout, {0} },
1424 { MODKEY | ShiftMask, XK_space, stack_config, {.id = SWM_ARG_ID_STACKRESET} },
1425 { MODKEY, XK_h, stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
1426 { MODKEY, XK_l, stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
1427 { MODKEY, XK_comma, stack_config, {.id = SWM_ARG_ID_MASTERADD} },
1428 { MODKEY, XK_period, stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
1429 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1430 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1431 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1432 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1433 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1434 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1435 { MODKEY, XK_p, spawnmenu, {.argv = spawn_menu} },
1436 { MODKEY | ShiftMask, XK_q, quit, {0} },
1437 { MODKEY, XK_q, restart, {0} },
1438 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1439 { MODKEY, XK_1, switchws, {.id = 0} },
1440 { MODKEY, XK_2, switchws, {.id = 1} },
1441 { MODKEY, XK_3, switchws, {.id = 2} },
1442 { MODKEY, XK_4, switchws, {.id = 3} },
1443 { MODKEY, XK_5, switchws, {.id = 4} },
1444 { MODKEY, XK_6, switchws, {.id = 5} },
1445 { MODKEY, XK_7, switchws, {.id = 6} },
1446 { MODKEY, XK_8, switchws, {.id = 7} },
1447 { MODKEY, XK_9, switchws, {.id = 8} },
1448 { MODKEY, XK_0, switchws, {.id = 9} },
1449 { MODKEY, XK_Right, cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
1450 { MODKEY, XK_Left, cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
1451 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1452 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1453 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1454 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1455 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1456 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1457 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1458 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1459 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1460 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1461 { MODKEY, XK_b, bar_toggle, {0} },
1462 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1463 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1464 { MODKEY | ShiftMask, XK_x, wkill, {0} },
1468 click(struct swm_region *r, union arg *args)
1470 DNPRINTF(SWM_D_MOUSE, "click: button: %d\n", args->id);
1485 enum { client_click, root_click };
1487 unsigned int action;
1489 unsigned int button;
1490 void (*func)(struct swm_region *r, union arg *);
1493 /* action key mouse button func args */
1494 { client_click, MODKEY, Button1, click, {.id=Button1} },
1498 updatenumlockmask(void)
1501 XModifierKeymap *modmap;
1503 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1505 modmap = XGetModifierMapping(display);
1506 for (i = 0; i < 8; i++)
1507 for (j = 0; j < modmap->max_keypermod; j++)
1508 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1509 == XKeysymToKeycode(display, XK_Num_Lock))
1510 numlockmask = (1 << i);
1512 XFreeModifiermap(modmap);
1518 unsigned int i, j, k;
1520 unsigned int modifiers[] =
1521 { 0, LockMask, numlockmask, numlockmask | LockMask };
1523 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1524 updatenumlockmask();
1526 for (k = 0; k < ScreenCount(display); k++) {
1527 if (TAILQ_EMPTY(&screens[k].rl))
1529 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
1530 for (i = 0; i < LENGTH(keys); i++) {
1531 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
1532 for (j = 0; j < LENGTH(modifiers); j++)
1533 XGrabKey(display, code,
1534 keys[i].mod | modifiers[j],
1535 screens[k].root, True,
1536 GrabModeAsync, GrabModeAsync);
1542 grabbuttons(struct ws_win *win, int focused)
1545 unsigned int modifiers[] =
1546 { 0, LockMask, numlockmask, numlockmask|LockMask };
1548 updatenumlockmask();
1549 XUngrabButton(display, AnyButton, AnyModifier, win->id);
1551 for (i = 0; i < LENGTH(buttons); i++)
1552 if (buttons[i].action == client_click)
1553 for (j = 0; j < LENGTH(modifiers); j++)
1554 XGrabButton(display, buttons[i].button,
1555 buttons[i].mask | modifiers[j],
1556 win->id, False, BUTTONMASK,
1557 GrabModeAsync, GrabModeSync, None,
1560 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
1561 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1567 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1575 XKeyEvent *ev = &e->xkey;
1577 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1579 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1580 for (i = 0; i < LENGTH(keys); i++)
1581 if (keysym == keys[i].keysym
1582 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1584 keys[i].func(root_to_region(ev->root),
1589 buttonpress(XEvent *e)
1591 XButtonPressedEvent *ev = &e->xbutton;
1596 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1598 action = root_click;
1599 if ((win = find_window(ev->window)) == NULL)
1603 action = client_click;
1606 for (i = 0; i < LENGTH(buttons); i++)
1607 if (action == buttons[i].action && buttons[i].func &&
1608 buttons[i].button == ev->button &&
1609 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
1610 buttons[i].func(root_to_region(ev->root),
1615 set_win_state(struct ws_win *win, long state)
1617 long data[] = {state, None};
1619 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1621 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1622 (unsigned char *)data, 2);
1626 manage_window(Window id)
1629 struct workspace *ws;
1633 unsigned long nitems, bytes;
1634 Atom ws_idx_atom = 0, type;
1635 unsigned char ws_idx_str[1], *prop = NULL;
1636 struct swm_region *r;
1638 if ((win = find_window(id)) != NULL)
1639 return (win); /* already being managed */
1641 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1642 errx(1, "calloc: failed to allocate memory for new window");
1644 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1646 XGetWindowProperty(display, id, ws_idx_atom, 0, 1, False, XA_STRING,
1647 &type, &format, &nitems, &bytes, &prop);
1649 XGetWindowAttributes(display, id, &win->wa);
1650 r = root_to_region(win->wa.root);
1651 /* If the window was managed before, put it in the same workspace */
1653 ws = &r->s->ws[prop[0]];
1659 win->s = r->s; /* this never changes */
1660 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
1662 /* make new win focused */
1665 XGetTransientForHint(display, win->id, &trans);
1667 win->transient = trans;
1668 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1669 (unsigned)win->id, win->transient);
1671 win->g.w = win->wa.width;
1672 win->g.h = win->wa.height;
1673 win->g.x = win->wa.x;
1674 win->g.y = win->wa.y;
1676 if (ws_idx_atom && prop == NULL) {
1677 /* set the window's workspace property if it wasn't there */
1678 ws_idx_str[0] = (unsigned char)ws->idx;
1679 XChangeProperty(display, id, ws_idx_atom, XA_STRING, 1,
1680 PropModeReplace, ws_idx_str, 1);
1684 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);
1687 /* XXX make this a table */
1688 bzero(&ch, sizeof ch);
1689 if (XGetClassHint(display, win->id, &ch)) {
1690 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1691 if (!strcmp(ch.res_class, "MPlayer") && !strcmp(ch.res_name, "xv")) {
1695 XFree(ch.res_class);
1700 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
1701 PropertyChangeMask | StructureNotifyMask);
1703 set_win_state(win, NormalState);
1709 configurerequest(XEvent *e)
1711 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1716 if ((win = find_window(ev->window)) == NULL)
1720 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
1723 fprintf(stderr, "configurerequest: new window: %lu x %d y %d w %d h %d bw %d s %d sm %d\n",
1724 ev->window, ev->x, ev->y, ev->width, ev->height, ev->border_width, ev->above, ev->detail);
1726 bzero(&wc, sizeof wc);
1729 wc.width = ev->width;
1730 wc.height = ev->height;
1731 wc.border_width = ev->border_width;
1732 wc.sibling = ev->above;
1733 wc.stack_mode = ev->detail;
1734 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
1737 fprintf(stderr, "configurerequest: change window: %lu\n",
1740 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
1742 if (win->floating) {
1743 if (ev->value_mask & CWX)
1745 if (ev->value_mask & CWY)
1747 if (ev->value_mask & CWWidth)
1748 win->g.w = ev->width;
1749 if (ev->value_mask & CWHeight)
1750 win->g.h = ev->height;
1751 if (win->ws->r != NULL) {
1752 /* this seems to be full screen */
1753 if (win->g.w > WIDTH(win->ws->r)) {
1758 if (win->g.h > HEIGHT(win->ws->r)) {
1764 if ((ev->value_mask & (CWX|CWY)) &&
1765 !(ev->value_mask & (CWWidth|CWHeight)))
1767 XMoveResizeWindow(display, win->id,
1768 win->g.x, win->g.y, win->g.w, win->g.h);
1775 configurenotify(XEvent *e)
1777 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1778 e->xconfigure.window);
1782 destroynotify(XEvent *e)
1785 XDestroyWindowEvent *ev = &e->xdestroywindow;
1786 struct workspace *ws;
1788 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1790 if ((win = find_window(ev->window)) != NULL) {
1792 /* find a window to focus */
1793 if (ws->focus == win)
1794 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1795 if (ws->focus == NULL)
1796 ws->focus = TAILQ_FIRST(&ws->winlist);
1797 if (ws->focus == win)
1799 if (cur_focus == win)
1800 focus_win(ws->focus);
1802 TAILQ_REMOVE(&ws->winlist, win, entry);
1803 set_win_state(win, WithdrawnState);
1810 enternotify(XEvent *e)
1812 XCrossingEvent *ev = &e->xcrossing;
1815 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1818 /* eat event(r) to prevent autofocus */
1823 if ((win = find_window(ev->window)) != NULL)
1830 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
1834 mappingnotify(XEvent *e)
1836 XMappingEvent *ev = &e->xmapping;
1838 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1840 XRefreshKeyboardMapping(ev);
1841 if (ev->request == MappingKeyboard)
1846 maprequest(XEvent *e)
1848 XMapRequestEvent *ev = &e->xmaprequest;
1849 XWindowAttributes wa;
1851 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1852 e->xmaprequest.window);
1854 if (!XGetWindowAttributes(display, ev->window, &wa))
1856 if (wa.override_redirect)
1858 manage_window(e->xmaprequest.window);
1863 propertynotify(XEvent *e)
1865 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1866 e->xproperty.window);
1870 unmapnotify(XEvent *e)
1872 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1876 visibilitynotify(XEvent *e)
1879 struct swm_region *r;
1881 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
1882 e->xvisibility.window);
1883 if (e->xvisibility.state == VisibilityUnobscured)
1884 for (i = 0; i < ScreenCount(display); i++)
1885 TAILQ_FOREACH(r, &screens[i].rl, entry)
1886 if (e->xvisibility.window == r->bar_window)
1890 void (*handler[LASTEvent])(XEvent *) = {
1892 [KeyPress] = keypress,
1893 [ButtonPress] = buttonpress,
1894 [ConfigureRequest] = configurerequest,
1895 [ConfigureNotify] = configurenotify,
1896 [DestroyNotify] = destroynotify,
1897 [EnterNotify] = enternotify,
1898 [FocusIn] = focusin,
1899 [MappingNotify] = mappingnotify,
1900 [MapRequest] = maprequest,
1901 [PropertyNotify] = propertynotify,
1902 [UnmapNotify] = unmapnotify,
1903 [VisibilityNotify] = visibilitynotify,
1907 xerror_start(Display *d, XErrorEvent *ee)
1914 xerror(Display *d, XErrorEvent *ee)
1916 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1924 xerrorxlib = XSetErrorHandler(xerror_start);
1926 /* this causes an error if some other window manager is running */
1927 XSelectInput(display, DefaultRootWindow(display),
1928 SubstructureRedirectMask);
1929 XSync(display, False);
1933 XSetErrorHandler(xerror);
1934 XSync(display, False);
1943 unsigned char *p = NULL;
1944 unsigned long n, extra;
1947 astate = XInternAtom(display, "WM_STATE", False);
1948 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
1949 &real, &format, &n, &extra, (unsigned char **)&p);
1950 if (status != Success)
1959 new_region(struct swm_screen *s, struct workspace *ws,
1960 int x, int y, int w, int h)
1962 struct swm_region *r;
1964 DNPRINTF(SWM_D_MISC, "new region on screen %d: %dx%d (%d, %d)\n",
1965 s->idx, x, y, w, h);
1967 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
1968 errx(1, "calloc: failed to allocate memory for screen");
1977 TAILQ_INSERT_TAIL(&s->rl, r, entry);
1984 #ifdef SWM_XRR_HAS_CRTC
1986 XRRScreenResources *sr;
1988 #endif /* SWM_XRR_HAS_CRTC */
1989 Window d1, d2, *wins = NULL;
1990 XWindowAttributes wa;
1991 struct swm_region *r;
1993 int errorbase, major, minor;
1994 int ncrtc = 0, w = 0;
1996 struct workspace *ws;
2000 if ((screens = calloc(ScreenCount(display),
2001 sizeof(struct swm_screen))) == NULL)
2002 errx(1, "calloc: screens");
2004 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2007 /* map physical screens */
2008 for (i = 0; i < ScreenCount(display); i++) {
2009 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
2011 TAILQ_INIT(&screens[i].rl);
2012 screens[i].root = RootWindow(display, i);
2014 /* set default colors */
2015 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
2016 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
2017 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
2018 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
2019 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
2021 /* init all workspaces */
2022 for (j = 0; j < SWM_WS_MAX; j++) {
2023 ws = &screens[i].ws[j];
2028 TAILQ_INIT(&ws->winlist);
2030 for (k = 0; layouts[k].l_stack != NULL; k++)
2031 if (layouts[k].l_config != NULL)
2032 layouts[k].l_config(ws,
2033 SWM_ARG_ID_STACKINIT);
2034 ws->cur_layout = &layouts[0];
2037 /* map virtual screens onto physical screens */
2038 screens[i].xrandr_support = XRRQueryExtension(display,
2039 &xrandr_eventbase, &errorbase);
2040 if (screens[i].xrandr_support)
2041 if (XRRQueryVersion(display, &major, &minor) &&
2043 screens[i].xrandr_support = 0;
2045 #if 0 /* not ready for dynamic screen changes */
2046 if (screens[i].xrandr_support)
2047 XRRSelectInput(display,
2049 RRScreenChangeNotifyMask);
2052 /* grab existing windows (before we build the bars)*/
2053 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
2056 #ifdef SWM_XRR_HAS_CRTC
2057 sr = XRRGetScreenResources(display, screens[i].root);
2059 new_region(&screens[i], &screens[i].ws[w],
2060 0, 0, DisplayWidth(display, i),
2061 DisplayHeight(display, i));
2065 for (c = 0, ci = NULL; c < ncrtc; c++) {
2066 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
2067 if (ci->noutput == 0)
2070 if (ci != NULL && ci->mode == None)
2071 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2072 DisplayWidth(display, i),
2073 DisplayHeight(display, i));
2075 new_region(&screens[i], &screens[i].ws[w],
2076 ci->x, ci->y, ci->width, ci->height);
2080 XRRFreeCrtcInfo(ci);
2081 XRRFreeScreenResources(sr);
2083 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2084 DisplayWidth(display, i),
2085 DisplayHeight(display, i));
2086 #endif /* SWM_XRR_HAS_CRTC */
2088 /* attach windows to a region */
2089 /* normal windows */
2090 if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
2091 errx(1, "no regions on screen %d", i);
2093 for (i = 0; i < no; i++) {
2094 XGetWindowAttributes(display, wins[i], &wa);
2095 if (!XGetWindowAttributes(display, wins[i], &wa) ||
2096 wa.override_redirect ||
2097 XGetTransientForHint(display, wins[i], &d1))
2100 if (wa.map_state == IsViewable ||
2101 getstate(wins[i]) == NormalState)
2102 manage_window(wins[i]);
2104 /* transient windows */
2105 for (i = 0; i < no; i++) {
2106 if (!XGetWindowAttributes(display, wins[i], &wa))
2109 if (XGetTransientForHint(display, wins[i], &d1) &&
2110 (wa.map_state == IsViewable || getstate(wins[i]) ==
2112 manage_window(wins[i]);
2122 main(int argc, char *argv[])
2125 char conf[PATH_MAX], *cfile = NULL;
2132 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
2133 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2134 warnx("no locale support");
2136 if (!(display = XOpenDisplay(0)))
2137 errx(1, "can not open display");
2140 errx(1, "other wm running");
2142 astate = XInternAtom(display, "WM_STATE", False);
2144 /* look for local and global conf file */
2145 pwd = getpwuid(getuid());
2147 errx(1, "invalid user %d", getuid());
2151 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
2152 if (stat(conf, &sb) != -1) {
2153 if (S_ISREG(sb.st_mode))
2156 /* try global conf file */
2157 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
2158 if (!stat(conf, &sb))
2159 if (S_ISREG(sb.st_mode))
2166 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2171 xfd = ConnectionNumber(display);
2175 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
2177 errx(1, "select failed");
2182 while(XPending(display)) {
2183 XNextEvent(display, &e);
2184 if (handler[e.type])
2185 handler[e.type](&e);
2189 XCloseDisplay(display);