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"
66 #include <sys/types.h>
70 #include <sys/queue.h>
71 #include <sys/param.h>
72 #include <sys/select.h>
74 #include <X11/cursorfont.h>
75 #include <X11/keysym.h>
76 #include <X11/Xatom.h>
78 #include <X11/Xproto.h>
79 #include <X11/Xutil.h>
80 #include <X11/extensions/Xrandr.h>
83 # error XRandR versions less than 1.0 are not supported
88 #define SWM_XRR_HAS_CRTC
92 /* #define SWM_DEBUG */
94 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
95 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
96 #define SWM_D_MISC 0x0001
97 #define SWM_D_EVENT 0x0002
98 #define SWM_D_WS 0x0004
99 #define SWM_D_FOCUS 0x0008
100 #define SWM_D_MOVE 0x0010
101 #define SWM_D_STACK 0x0020
103 u_int32_t swm_debug = 0
112 #define DPRINTF(x...)
113 #define DNPRINTF(n,x...)
116 #define LENGTH(x) (sizeof x / sizeof x[0])
117 #define MODKEY Mod1Mask
118 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
120 #define X(r) (r)->g.x
121 #define Y(r) (r)->g.y
122 #define WIDTH(r) (r)->g.w
123 #define HEIGHT(r) (r)->g.h
127 int (*xerrorxlib)(Display *, XErrorEvent *);
130 int xrandr_eventbase;
131 int ignore_enter = 0;
132 unsigned int numlockmask = 0;
136 int cycle_visible = 0;
139 double dialog_ratio = .6;
141 #define SWM_BAR_MAX (128)
142 sig_atomic_t bar_alarm = 0;
149 char *bar_fonts[] = {
150 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
151 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
155 /* terminal + args */
156 char *spawn_term[] = { "xterm", NULL };
157 char *spawn_menu[] = { "dmenu_run", NULL };
159 /* layout manager data */
160 struct swm_geometry {
170 /* virtual "screens" */
172 TAILQ_ENTRY(swm_region) entry;
173 struct swm_geometry g;
175 struct workspace *ws; /* current workspace on this region */
176 struct swm_screen *s; /* screen idx */
178 TAILQ_HEAD(swm_region_list, swm_region);
182 TAILQ_ENTRY(ws_win) entry;
184 struct swm_geometry g;
188 struct workspace *ws; /* always valid */
189 struct swm_screen *s; /* always valid, never changes */
190 XWindowAttributes wa;
192 TAILQ_HEAD(ws_win_list, ws_win);
194 /* layout handlers */
196 void vertical_config(struct workspace *, int);
197 void vertical_stack(struct workspace *, struct swm_geometry *);
198 void horizontal_config(struct workspace *, int);
199 void horizontal_stack(struct workspace *, struct swm_geometry *);
200 void max_stack(struct workspace *, struct swm_geometry *);
203 void (*l_stack)(struct workspace *, struct swm_geometry *);
204 void (*l_config)(struct workspace *, int);
206 /* stack, configure */
207 { vertical_stack, vertical_config},
208 { horizontal_stack, horizontal_config},
213 #define SWM_H_SLICE (32)
214 #define SWM_V_SLICE (32)
216 /* define work spaces */
218 int idx; /* workspace index */
219 int restack; /* restack on switch */
220 struct layout *cur_layout; /* current layout handlers */
221 struct ws_win *focus; /* may be NULL */
222 struct swm_region *r; /* may be NULL */
223 struct ws_win_list winlist; /* list of windows in ws */
227 int horizontal_msize;
234 /* physical screen mapping */
235 #define SWM_WS_MAX (10) /* XXX Too small? */
237 int idx; /* screen index */
238 struct swm_region_list rl; /* list of regions on this screen */
241 struct workspace ws[SWM_WS_MAX];
244 unsigned long bar_border;
245 unsigned long bar_color;
246 unsigned long bar_font_color;
247 unsigned long color_focus; /* XXX should this be per ws? */
248 unsigned long color_unfocus;
250 struct swm_screen *screens;
252 Window rootclick = 0;
254 struct ws_win *cur_focus = NULL;
256 /* args to functions */
259 #define SWM_ARG_ID_FOCUSNEXT (0)
260 #define SWM_ARG_ID_FOCUSPREV (1)
261 #define SWM_ARG_ID_FOCUSMAIN (2)
262 #define SWM_ARG_ID_SWAPNEXT (3)
263 #define SWM_ARG_ID_SWAPPREV (4)
264 #define SWM_ARG_ID_SWAPMAIN (5)
265 #define SWM_ARG_ID_MASTERSHRINK (6)
266 #define SWM_ARG_ID_MASTERGROW (7)
267 #define SWM_ARG_ID_MASTERADD (8)
268 #define SWM_ARG_ID_MASTERDEL (9)
269 #define SWM_ARG_ID_STACKRESET (10)
270 #define SWM_ARG_ID_STACKINIT (11)
271 #define SWM_ARG_ID_CYCLEWS_UP (12)
272 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
277 name_to_color(char *colorname)
281 XColor screen_def, exact_def;
282 unsigned long result = 0;
283 char cname[32] = "#";
285 cmap = DefaultColormap(display, screens[0].idx);
286 status = XAllocNamedColor(display, cmap, colorname,
287 &screen_def, &exact_def);
289 strlcat(cname, colorname + 2, sizeof cname - 1);
290 status = XAllocNamedColor(display, cmap, cname, &screen_def,
294 result = screen_def.pixel;
296 fprintf(stderr, "color '%s' not found.\n", colorname);
302 varmatch(char *var, char *name, int *index)
307 i = strncmp(var, name, 255);
314 p = var + strlen(name);
317 bzero(buf, sizeof buf);
319 while (isdigit(*p) && i < sizeof buf)
321 if (i == 0 || i >= sizeof buf || *p != ']')
323 *index = strtonum(buf, 0, 99, NULL);
327 /* conf file stuff */
328 #define SWM_CONF_WS "\n= \t"
329 #define SWM_CONF_FILE "scrotwm.conf"
331 conf_load(char *filename)
334 char *line, *cp, *var, *val;
335 size_t len, lineno = 0;
338 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
340 if (filename == NULL)
343 if ((config = fopen(filename, "r")) == NULL)
346 for (sc = ScreenCount(display);;) {
347 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
351 cp += (long)strspn(cp, SWM_CONF_WS);
357 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
358 if (cp == NULL || (var = strsep(&cp, SWM_CONF_WS)) == NULL)
360 cp += (long)strspn(cp, SWM_CONF_WS);
361 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
364 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
367 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
368 bar_enabled = atoi(val);
369 else if (!varmatch(var, "bar_border", &i))
370 if (i > 0 && i <= sc)
371 screens[i - 1].bar_border =
374 for (i = 0; i < sc; i++)
375 screens[i].bar_border =
379 else if (!varmatch(var, "bar_color", &i))
380 if (i > 0 && i <= sc)
381 screens[i - 1].bar_color =
384 for (i = 0; i < sc; i++)
385 screens[i].bar_color =
389 else if (!varmatch(var, "bar_font_color", &i))
390 if (i > 0 && i <= sc)
391 screens[i - 1].bar_font_color =
394 for (i = 0; i < sc; i++)
395 screens[i].bar_font_color =
400 else if (!strncmp(var, "bar_font", strlen("bar_font")))
401 asprintf(&bar_fonts[0], "%s", val);
407 if (!varmatch(var, "color_focus", &i))
408 if (i > 0 && i <= sc)
409 screens[i - 1].color_focus =
412 for (i = 0; i < sc; i++)
413 screens[i].color_focus =
417 else if (!varmatch(var, "color_unfocus", &i))
418 if (i > 0 && i <= sc)
419 screens[i - 1].color_unfocus =
422 for (i = 0; i < sc; i++)
423 screens[i].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? */
458 errx(1, "invalid conf file entry: %s=%s", var, val);
460 errx(1, "invalid screen index: %s out of bounds", var);
464 bar_print(struct swm_region *r, char *s)
466 XClearWindow(display, r->bar_window);
467 XSetForeground(display, bar_gc, r->s->bar_font_color);
468 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
477 struct swm_region *r;
482 if (bar_enabled == 0)
486 localtime_r(&tmt, &tm);
487 strftime(s, sizeof s, "%a %b %d %R %Z %Y", &tm);
488 for (i = 0; i < ScreenCount(display); i++) {
490 TAILQ_FOREACH(r, &screens[i].rl, entry) {
491 snprintf(e, sizeof e, "%s %d:%d",
492 s, x++, r->ws->idx + 1);
496 XSync(display, False);
507 bar_toggle(struct swm_region *r, union arg *args)
509 struct swm_region *tmpr;
510 int i, j, sc = ScreenCount(display);
512 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
515 for (i = 0; i < sc; i++)
516 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
517 XUnmapWindow(display, tmpr->bar_window);
519 for (i = 0; i < sc; i++)
520 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
521 XMapRaised(display, tmpr->bar_window);
523 bar_enabled = !bar_enabled;
524 XSync(display, False);
525 for (i = 0; i < sc; i++)
526 for (j = 0; j < SWM_WS_MAX; j++)
527 screens[i].ws[j].restack = 1;
530 /* must be after stack */
531 for (i = 0; i < sc; i++)
532 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
537 bar_setup(struct swm_region *r)
541 for (i = 0; bar_fonts[i] != NULL; i++) {
542 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
546 if (bar_fonts[i] == NULL)
547 errx(1, "couldn't load font");
548 bar_height = bar_fs->ascent + bar_fs->descent + 3;
550 r->bar_window = XCreateSimpleWindow(display,
551 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
552 1, r->s->bar_border, r->s->bar_color);
553 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
554 XSetFont(display, bar_gc, bar_fs->fid);
555 XSelectInput(display, r->bar_window, VisibilityChangeMask);
557 XMapRaised(display, r->bar_window);
558 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
560 if (signal(SIGALRM, bar_signal) == SIG_ERR)
561 err(1, "could not install bar_signal");
568 XSetWindowAttributes wa;
569 struct swm_region *r;
572 bzero(&wa, sizeof wa);
573 for (i = 0; i < ScreenCount(display); i++)
574 TAILQ_FOREACH(r, &screens[i].rl, entry) {
575 wa.border_pixel = screens[i].bar_border;
576 wa.background_pixel = screens[i].bar_color;
577 XChangeWindowAttributes(display, r->bar_window,
578 CWBackPixel | CWBorderPixel, &wa);
584 config_win(struct ws_win *win)
588 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
589 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
590 ce.type = ConfigureNotify;
591 ce.display = display;
597 ce.height = win->g.h;
598 ce.border_width = 1; /* XXX store this! */
600 ce.override_redirect = False;
601 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
605 count_win(struct workspace *ws, int count_transient)
610 TAILQ_FOREACH(win, &ws->winlist, entry) {
611 if (count_transient == 0 && win->floating)
613 if (count_transient == 0 && win->transient)
617 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
623 quit(struct swm_region *r, union arg *args)
625 DNPRINTF(SWM_D_MISC, "quit\n");
630 restart(struct swm_region *r, union arg *args)
632 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
634 /* disable alarm because the following code may not be interrupted */
636 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
637 errx(1, "can't disable alarm");
639 XCloseDisplay(display);
640 execvp(start_argv[0], start_argv);
641 fprintf(stderr, "execvp failed\n");
647 root_to_region(Window root)
649 struct swm_region *r;
654 for (i = 0; i < ScreenCount(display); i++)
655 if (screens[i].root == root)
658 if (rootclick != root && /* if root was just clicked in, use cursor */
659 cur_focus && cur_focus->ws->r && cur_focus->s == &screens[i])
660 r = cur_focus->ws->r;
662 if (XQueryPointer(display, screens[i].root,
663 &rr, &cr, &x, &y, &wx, &wy, &mask) == False) {
664 r = TAILQ_FIRST(&screens[i].rl);
666 TAILQ_FOREACH(r, &screens[i].rl, entry) {
667 if (x > X(r) && x < X(r) + WIDTH(r) &&
668 y > Y(r) && y < Y(r) + HEIGHT(r))
673 r = TAILQ_FIRST(&screens[i].rl);
680 find_window(Window id)
685 for (i = 0; i < ScreenCount(display); i++)
686 for (j = 0; j < SWM_WS_MAX; j++)
687 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
694 spawn(struct swm_region *r, union arg *args)
696 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
698 * The double-fork construct avoids zombie processes and keeps the code
699 * clean from stupid signal handlers.
704 close(ConnectionNumber(display));
706 execvp(args->argv[0], args->argv);
707 fprintf(stderr, "execvp failed\n");
716 unfocus_win(struct ws_win *win)
718 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
719 if (win->ws->r && win->focus)
720 XSetWindowBorder(display, win->id,
721 win->ws->r->s->color_unfocus);
723 if (win->ws->focus == win)
724 win->ws->focus = NULL;
725 if (cur_focus == win)
731 focus_win(struct ws_win *win)
733 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
737 win->ws->focus = win;
738 if (win->ws->r != NULL) {
739 if (cur_focus && cur_focus != win)
740 unfocus_win(cur_focus);
743 XSetWindowBorder(display, win->id,
744 win->ws->r->s->color_focus);
746 XSetInputFocus(display, win->id,
747 RevertToPointerRoot, CurrentTime);
752 switchws(struct swm_region *r, union arg *args)
755 struct swm_region *this_r, *other_r;
757 struct workspace *new_ws, *old_ws;
761 new_ws = &this_r->s->ws[wsid];
763 DNPRINTF(SWM_D_WS, "switchws screen %d region %dx%d+%d+%d: "
764 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
767 if (new_ws == old_ws)
772 /* if the other workspace is hidden, switch windows */
773 /* map new window first to prevent ugly blinking */
774 TAILQ_FOREACH(win, &new_ws->winlist, entry)
775 XMapRaised(display, win->id);
777 TAILQ_FOREACH(win, &old_ws->winlist, entry)
778 XUnmapWindow(display, win->id);
783 other_r->ws = old_ws;
792 focus_win(new_ws->focus);
798 cyclews(struct swm_region *r, union arg *args)
801 struct swm_screen *s = r->s;
803 DNPRINTF(SWM_D_WS, "cyclews id %d "
804 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
805 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
811 case SWM_ARG_ID_CYCLEWS_UP:
812 if (a.id < SWM_WS_MAX - 1)
817 case SWM_ARG_ID_CYCLEWS_DOWN:
821 a.id = SWM_WS_MAX - 1;
827 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
829 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
833 } while (a.id != r->ws->idx);
837 swapwin(struct swm_region *r, union arg *args)
839 struct ws_win *target;
840 struct ws_win_list *wl;
843 DNPRINTF(SWM_D_WS, "swapwin id %d "
844 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
845 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
846 if (cur_focus == NULL)
849 wl = &cur_focus->ws->winlist;
852 case SWM_ARG_ID_SWAPPREV:
853 target = TAILQ_PREV(cur_focus, ws_win_list, entry);
854 TAILQ_REMOVE(wl, cur_focus, entry);
856 TAILQ_INSERT_TAIL(wl, cur_focus, entry);
858 TAILQ_INSERT_BEFORE(target, cur_focus, entry);
860 case SWM_ARG_ID_SWAPNEXT:
861 target = TAILQ_NEXT(cur_focus, entry);
862 TAILQ_REMOVE(wl, cur_focus, entry);
864 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
866 TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
868 case SWM_ARG_ID_SWAPMAIN:
869 target = TAILQ_FIRST(wl);
870 if (target == cur_focus)
872 TAILQ_REMOVE(wl, target, entry);
873 TAILQ_INSERT_BEFORE(cur_focus, target, entry);
874 TAILQ_REMOVE(wl, cur_focus, entry);
875 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
878 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
887 focus(struct swm_region *r, union arg *args)
889 struct ws_win *winfocus, *winlostfocus;
890 struct ws_win_list *wl;
892 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
893 if (cur_focus == NULL)
896 wl = &cur_focus->ws->winlist;
898 winlostfocus = cur_focus;
901 case SWM_ARG_ID_FOCUSPREV:
902 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
903 if (winfocus == NULL)
904 winfocus = TAILQ_LAST(wl, ws_win_list);
907 case SWM_ARG_ID_FOCUSNEXT:
908 winfocus = TAILQ_NEXT(cur_focus, entry);
909 if (winfocus == NULL)
910 winfocus = TAILQ_FIRST(wl);
913 case SWM_ARG_ID_FOCUSMAIN:
914 winfocus = TAILQ_FIRST(wl);
921 if (winfocus == winlostfocus)
925 XSync(display, False);
929 cycle_layout(struct swm_region *r, union arg *args)
931 struct workspace *ws = r->ws;
933 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
936 if (ws->cur_layout->l_stack == NULL)
937 ws->cur_layout = &layouts[0];
944 stack_config(struct swm_region *r, union arg *args)
946 struct workspace *ws = r->ws;
948 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
951 if (ws->cur_layout->l_config != NULL)
952 ws->cur_layout->l_config(ws, args->id);
954 if (args->id != SWM_ARG_ID_STACKINIT);
960 struct swm_geometry g;
961 struct swm_region *r;
964 DNPRINTF(SWM_D_STACK, "stack\n");
966 for (i = 0; i < ScreenCount(display); i++) {
968 TAILQ_FOREACH(r, &screens[i].rl, entry) {
969 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
970 "(screen %d, region %d)\n", r->ws->idx, i, j++);
972 /* start with screen geometry, adjust for bar */
982 r->ws->cur_layout->l_stack(r->ws, &g);
985 XSync(display, False);
989 stack_floater(struct ws_win *win, struct swm_region *r)
994 bzero(&wc, sizeof wc);
995 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
997 if (win->transient) {
998 win->g.w = (double)WIDTH(r) * dialog_ratio;
999 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1001 wc.width = win->g.w;
1002 wc.height = win->g.h;
1003 wc.x = (WIDTH(r) - win->g.w) / 2;
1004 wc.y = (HEIGHT(r) - win->g.h) / 2;
1006 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1007 win->id, wc.x, wc.y, wc.width, wc.height);
1009 XConfigureWindow(display, win->id, mask, &wc);
1014 vertical_config(struct workspace *ws, int id)
1016 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1019 case SWM_ARG_ID_STACKRESET:
1020 case SWM_ARG_ID_STACKINIT:
1021 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1022 ws->l_state.vertical_mwin = 1;
1024 case SWM_ARG_ID_MASTERSHRINK:
1025 if (ws->l_state.vertical_msize > 1)
1026 ws->l_state.vertical_msize--;
1028 case SWM_ARG_ID_MASTERGROW:
1029 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1030 ws->l_state.vertical_msize++;
1032 case SWM_ARG_ID_MASTERADD:
1033 ws->l_state.vertical_mwin++;
1035 case SWM_ARG_ID_MASTERDEL:
1036 if (ws->l_state.vertical_mwin > 0)
1037 ws->l_state.vertical_mwin--;
1045 vertical_stack(struct workspace *ws, struct swm_geometry *g) {
1047 struct swm_geometry gg = *g;
1048 struct ws_win *win, *winfocus;
1049 int i, j, split, colno, hrh, winno, main_width;
1052 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1054 if ((winno = count_win(ws, 0)) == 0)
1057 if (ws->focus == NULL)
1058 ws->focus = TAILQ_FIRST(&ws->winlist);
1059 winfocus = cur_focus ? cur_focus : ws->focus;
1061 if (ws->l_state.vertical_mwin &&
1062 winno > ws->l_state.vertical_mwin) {
1063 split = ws->l_state.vertical_mwin;
1065 main_width = (g->w / SWM_V_SLICE) *
1066 ws->l_state.vertical_msize;
1076 TAILQ_FOREACH(win, &ws->winlist, entry) {
1077 if (split && i == split) {
1078 colno = winno - split;
1079 hrh = (g->h / colno);
1080 gg.x += main_width + 2;
1081 gg.w = g->w - (main_width + 2);
1086 gg.h = (hrh + (g->h - (colno * hrh)));
1094 if (win->transient != 0 || win->floating != 0)
1095 stack_floater(win, ws->r);
1097 bzero(&wc, sizeof wc);
1098 wc.border_width = 1;
1099 win->g.x = wc.x = gg.x;
1100 win->g.y = wc.y = gg.y;
1101 win->g.w = wc.width = gg.w;
1102 win->g.h = wc.height = gg.h;
1103 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1104 XConfigureWindow(display, win->id, mask, &wc);
1107 XMapRaised(display, win->id);
1113 focus_win(winfocus); /* has to be done outside of the loop */
1118 horizontal_config(struct workspace *ws, int id)
1120 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1123 case SWM_ARG_ID_STACKRESET:
1124 case SWM_ARG_ID_STACKINIT:
1125 ws->l_state.horizontal_mwin = 1;
1126 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1128 case SWM_ARG_ID_MASTERSHRINK:
1129 if (ws->l_state.horizontal_msize > 1)
1130 ws->l_state.horizontal_msize--;
1132 case SWM_ARG_ID_MASTERGROW:
1133 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1134 ws->l_state.horizontal_msize++;
1136 case SWM_ARG_ID_MASTERADD:
1137 ws->l_state.horizontal_mwin++;
1139 case SWM_ARG_ID_MASTERDEL:
1140 if (ws->l_state.horizontal_mwin > 0)
1141 ws->l_state.horizontal_mwin--;
1149 horizontal_stack(struct workspace *ws, struct swm_geometry *g) {
1151 struct swm_geometry gg = *g;
1152 struct ws_win *win, *winfocus;
1153 int i, j, split, rowno, hrw, winno, main_height;
1156 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
1158 if ((winno = count_win(ws, 0)) == 0)
1161 if (ws->focus == NULL)
1162 ws->focus = TAILQ_FIRST(&ws->winlist);
1163 winfocus = cur_focus ? cur_focus : ws->focus;
1165 if (ws->l_state.horizontal_mwin &&
1166 winno > ws->l_state.horizontal_mwin) {
1167 split = ws->l_state.horizontal_mwin;
1169 main_height = (g->h / SWM_V_SLICE) *
1170 ws->l_state.horizontal_msize;
1180 TAILQ_FOREACH(win, &ws->winlist, entry) {
1181 if (split && i == split) {
1182 rowno = winno - split;
1183 hrw = (g->w / rowno);
1184 gg.y += main_height + 2;
1185 gg.h = g->h - (main_height + 2);
1190 gg.w = (hrw + (g->w - (rowno * hrw)));
1197 if (win->transient != 0 || win->floating != 0)
1198 stack_floater(win, ws->r);
1200 bzero(&wc, sizeof wc);
1201 wc.border_width = 1;
1202 win->g.x = wc.x = gg.x;
1203 win->g.y = wc.y = gg.y;
1204 win->g.w = wc.width = gg.w;
1205 win->g.h = wc.height = gg.h;
1206 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1207 XConfigureWindow(display, win->id, mask, &wc);
1210 XMapRaised(display, win->id);
1216 focus_win(winfocus); /* this has to be done outside of the loop */
1219 /* fullscreen view */
1221 max_stack(struct workspace *ws, struct swm_geometry *g) {
1223 struct swm_geometry gg = *g;
1224 struct ws_win *win, *winfocus;
1227 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1229 if (count_win(ws, 0) == 0)
1232 if (ws->focus == NULL)
1233 ws->focus = TAILQ_FIRST(&ws->winlist);
1234 winfocus = cur_focus ? cur_focus : ws->focus;
1236 TAILQ_FOREACH(win, &ws->winlist, entry) {
1237 if (win->transient != 0 || win->floating != 0) {
1238 if (win == ws->focus) {
1240 stack_floater(win, ws->r);
1241 XMapRaised(display, win->id);
1243 XUnmapWindow(display, win->id);
1245 bzero(&wc, sizeof wc);
1246 wc.border_width = 1;
1247 win->g.x = wc.x = gg.x;
1248 win->g.y = wc.y = gg.y;
1249 win->g.w = wc.width = gg.w;
1250 win->g.h = wc.height = gg.h;
1251 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1252 XConfigureWindow(display, win->id, mask, &wc);
1254 if (win == ws->focus) {
1255 XMapRaised(display, win->id);
1257 XUnmapWindow(display, win->id);
1262 focus_win(winfocus); /* has to be done outside of the loop */
1266 send_to_ws(struct swm_region *r, union arg *args)
1268 int wsid = args->id;
1269 struct ws_win *win = cur_focus;
1270 struct workspace *ws, *nws;
1272 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1275 nws = &win->s->ws[wsid];
1277 XUnmapWindow(display, win->id);
1279 /* find a window to focus */
1280 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1281 if (ws->focus == NULL)
1282 ws->focus = TAILQ_FIRST(&ws->winlist);
1283 if (ws->focus == win)
1286 TAILQ_REMOVE(&ws->winlist, win, entry);
1288 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1291 if (count_win(nws, 1) == 1)
1299 /* key definitions */
1303 void (*func)(struct swm_region *r, union arg *);
1306 /* modifier key function argument */
1307 { MODKEY, XK_space, cycle_layout, {0} },
1308 { MODKEY | ShiftMask, XK_space, stack_config, {.id = SWM_ARG_ID_STACKRESET} },
1309 { MODKEY, XK_h, stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
1310 { MODKEY, XK_l, stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
1311 { MODKEY, XK_comma, stack_config, {.id = SWM_ARG_ID_MASTERADD} },
1312 { MODKEY, XK_period, stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
1313 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1314 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1315 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1316 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1317 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1318 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1319 { MODKEY, XK_p, spawn, {.argv = spawn_menu} },
1320 { MODKEY | ShiftMask, XK_q, quit, {0} },
1321 { MODKEY, XK_q, restart, {0} },
1322 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1323 { MODKEY, XK_1, switchws, {.id = 0} },
1324 { MODKEY, XK_2, switchws, {.id = 1} },
1325 { MODKEY, XK_3, switchws, {.id = 2} },
1326 { MODKEY, XK_4, switchws, {.id = 3} },
1327 { MODKEY, XK_5, switchws, {.id = 4} },
1328 { MODKEY, XK_6, switchws, {.id = 5} },
1329 { MODKEY, XK_7, switchws, {.id = 6} },
1330 { MODKEY, XK_8, switchws, {.id = 7} },
1331 { MODKEY, XK_9, switchws, {.id = 8} },
1332 { MODKEY, XK_0, switchws, {.id = 9} },
1333 { MODKEY, XK_Right, cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
1334 { MODKEY, XK_Left, cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
1335 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1336 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1337 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1338 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1339 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1340 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1341 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1342 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1343 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1344 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1345 { MODKEY, XK_b, bar_toggle, {0} },
1346 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1347 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1351 updatenumlockmask(void)
1354 XModifierKeymap *modmap;
1356 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1358 modmap = XGetModifierMapping(display);
1359 for (i = 0; i < 8; i++)
1360 for (j = 0; j < modmap->max_keypermod; j++)
1361 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1362 == XKeysymToKeycode(display, XK_Num_Lock))
1363 numlockmask = (1 << i);
1365 XFreeModifiermap(modmap);
1371 unsigned int i, j, k;
1373 unsigned int modifiers[] =
1374 { 0, LockMask, numlockmask, numlockmask | LockMask };
1376 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1377 updatenumlockmask();
1379 for (k = 0; k < ScreenCount(display); k++) {
1380 if (TAILQ_EMPTY(&screens[k].rl))
1382 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
1383 for (i = 0; i < LENGTH(keys); i++) {
1384 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
1385 for (j = 0; j < LENGTH(modifiers); j++)
1386 XGrabKey(display, code,
1387 keys[i].mod | modifiers[j],
1388 screens[k].root, True,
1389 GrabModeAsync, GrabModeAsync);
1396 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1404 XKeyEvent *ev = &e->xkey;
1406 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1408 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1409 for (i = 0; i < LENGTH(keys); i++)
1410 if (keysym == keys[i].keysym
1411 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1413 keys[i].func(root_to_region(ev->root),
1418 buttonpress(XEvent *e)
1420 XButtonPressedEvent *ev = &e->xbutton;
1421 #ifdef SWM_CLICKTOFOCUS
1423 struct workspace *ws;
1424 struct swm_region *r;
1427 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1429 if (ev->window == ev->root) {
1430 rootclick = ev->root;
1433 if (ev->window == cur_focus->id)
1435 #ifdef SWM_CLICKTOFOCUS
1436 r = root_to_region(ev->root);
1438 TAILQ_FOREACH(win, &ws->winlist, entry)
1439 if (win->id == ev->window) {
1440 /* focus in the clicked window */
1441 XSetWindowBorder(display, ev->window, 0xff0000);
1442 XSetWindowBorder(display, ws->focus->id, 0x888888);
1443 XSetInputFocus(display, ev->window, RevertToPointerRoot,
1446 XSync(display, False);
1453 set_win_state(struct ws_win *win, long state)
1455 long data[] = {state, None};
1457 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1459 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1460 (unsigned char *)data, 2);
1464 manage_window(Window id, struct workspace *ws)
1470 TAILQ_FOREACH(win, &ws->winlist, entry) {
1472 return (win); /* already being managed */
1475 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1476 errx(1, "calloc: failed to allocate memory for new window");
1480 win->s = ws->r->s; /* this never changes */
1481 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
1483 /* make new win focused */
1486 XGetTransientForHint(display, win->id, &trans);
1488 win->transient = trans;
1489 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1490 (unsigned)win->id, win->transient);
1492 XGetWindowAttributes(display, id, &win->wa);
1493 win->g.w = win->wa.width;
1494 win->g.h = win->wa.height;
1495 win->g.x = win->wa.x;
1496 win->g.y = win->wa.y;
1498 /* XXX make this a table */
1499 bzero(&ch, sizeof ch);
1500 if (XGetClassHint(display, win->id, &ch)) {
1501 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1502 if (!strcmp(ch.res_class, "MPlayer") && !strcmp(ch.res_name, "xv")) {
1506 XFree(ch.res_class);
1511 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
1512 PropertyChangeMask | StructureNotifyMask);
1514 set_win_state(win, NormalState);
1520 configurerequest(XEvent *e)
1522 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1527 if ((win = find_window(ev->window)) == NULL)
1531 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
1533 bzero(&wc, sizeof wc);
1536 wc.width = ev->width;
1537 wc.height = ev->height;
1538 wc.border_width = ev->border_width;
1539 wc.sibling = ev->above;
1540 wc.stack_mode = ev->detail;
1541 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
1543 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
1545 if (win->floating) {
1546 if (ev->value_mask & CWX)
1548 if (ev->value_mask & CWY)
1550 if (ev->value_mask & CWWidth)
1551 win->g.w = ev->width;
1552 if (ev->value_mask & CWHeight)
1553 win->g.h = ev->height;
1554 if (win->ws->r != NULL) {
1555 /* this seems to be full screen */
1556 if (win->g.w > WIDTH(win->ws->r)) {
1561 if (win->g.h > HEIGHT(win->ws->r)) {
1567 if ((ev->value_mask & (CWX|CWY)) &&
1568 !(ev->value_mask & (CWWidth|CWHeight)))
1570 XMoveResizeWindow(display, win->id,
1571 win->g.x, win->g.y, win->g.w, win->g.h);
1578 configurenotify(XEvent *e)
1580 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1581 e->xconfigure.window);
1585 destroynotify(XEvent *e)
1588 XDestroyWindowEvent *ev = &e->xdestroywindow;
1590 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1592 if ((win = find_window(ev->window)) != NULL) {
1593 struct workspace *ws = win->ws;
1594 /* find a window to focus */
1595 if (ws->focus == win)
1596 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1597 if (ws->focus == NULL)
1598 ws->focus = TAILQ_FIRST(&ws->winlist);
1599 if (ws->focus == win)
1601 if (cur_focus == win) {
1602 if (ws->focus == NULL)
1603 unfocus_win(win); /* XXX focus another ws? */
1605 focus_win(ws->focus);
1608 TAILQ_REMOVE(&ws->winlist, win, entry);
1609 set_win_state(win, WithdrawnState);
1616 enternotify(XEvent *e)
1618 XCrossingEvent *ev = &e->xcrossing;
1622 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1624 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
1625 ev->window != ev->root)
1628 /* eat event(r) to prevent autofocus */
1632 /* brute force for now */
1633 for (i = 0; i < ScreenCount(display); i++) {
1634 for (j = 0; j < SWM_WS_MAX; j++) {
1635 TAILQ_FOREACH(win, &screens[i].ws[j].winlist , entry) {
1636 if (win->id == ev->window)
1646 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
1648 /* XXX this probably needs better handling now.
1649 if (ev->window == ev->root)
1653 * kill grab for now so that we can cut and paste , this screws up
1657 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1658 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1659 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1664 mappingnotify(XEvent *e)
1666 XMappingEvent *ev = &e->xmapping;
1668 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1670 XRefreshKeyboardMapping(ev);
1671 if (ev->request == MappingKeyboard)
1676 maprequest(XEvent *e)
1678 XMapRequestEvent *ev = &e->xmaprequest;
1679 XWindowAttributes wa;
1680 struct swm_region *r;
1682 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1683 e->xmaprequest.window);
1685 if (!XGetWindowAttributes(display, ev->window, &wa))
1687 if (wa.override_redirect)
1689 r = root_to_region(wa.root);
1690 manage_window(e->xmaprequest.window, r->ws);
1695 propertynotify(XEvent *e)
1697 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1698 e->xproperty.window);
1702 unmapnotify(XEvent *e)
1704 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1708 visibilitynotify(XEvent *e)
1711 struct swm_region *r;
1713 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
1714 e->xvisibility.window);
1715 if (e->xvisibility.state == VisibilityUnobscured)
1716 for (i = 0; i < ScreenCount(display); i++)
1717 TAILQ_FOREACH(r, &screens[i].rl, entry)
1718 if (e->xvisibility.window == r->bar_window)
1722 void (*handler[LASTEvent])(XEvent *) = {
1724 [KeyPress] = keypress,
1725 [ButtonPress] = buttonpress,
1726 [ConfigureRequest] = configurerequest,
1727 [ConfigureNotify] = configurenotify,
1728 [DestroyNotify] = destroynotify,
1729 [EnterNotify] = enternotify,
1730 [FocusIn] = focusin,
1731 [MappingNotify] = mappingnotify,
1732 [MapRequest] = maprequest,
1733 [PropertyNotify] = propertynotify,
1734 [UnmapNotify] = unmapnotify,
1735 [VisibilityNotify] = visibilitynotify,
1739 xerror_start(Display *d, XErrorEvent *ee)
1746 xerror(Display *d, XErrorEvent *ee)
1748 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1756 xerrorxlib = XSetErrorHandler(xerror_start);
1758 /* this causes an error if some other window manager is running */
1759 XSelectInput(display, DefaultRootWindow(display),
1760 SubstructureRedirectMask);
1761 XSync(display, False);
1765 XSetErrorHandler(xerror);
1766 XSync(display, False);
1775 unsigned char *p = NULL;
1776 unsigned long n, extra;
1779 astate = XInternAtom(display, "WM_STATE", False);
1780 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
1781 &real, &format, &n, &extra, (unsigned char **)&p);
1782 if (status != Success)
1791 new_region(struct swm_screen *s, struct workspace *ws,
1792 int x, int y, int w, int h)
1794 struct swm_region *r;
1796 DNPRINTF(SWM_D_MISC, "new region on screen %d: %dx%d (%d, %d)\n",
1797 s->idx, x, y, w, h);
1799 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
1800 errx(1, "calloc: failed to allocate memory for screen");
1809 TAILQ_INSERT_TAIL(&s->rl, r, entry);
1816 #ifdef SWM_XRR_HAS_CRTC
1818 XRRScreenResources *sr;
1820 #endif /* SWM_XRR_HAS_CRTC */
1821 Window d1, d2, *wins = NULL;
1822 XWindowAttributes wa;
1823 struct swm_region *r;
1825 int errorbase, major, minor;
1826 int ncrtc = 0, w = 0;
1828 struct workspace *ws;
1830 if ((screens = calloc(ScreenCount(display),
1831 sizeof(struct swm_screen))) == NULL)
1832 errx(1, "calloc: screens");
1834 /* map physical screens */
1835 for (i = 0; i < ScreenCount(display); i++) {
1836 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
1838 TAILQ_INIT(&screens[i].rl);
1839 screens[i].root = RootWindow(display, i);
1840 XGetWindowAttributes(display, screens[i].root, &wa);
1841 XSelectInput(display, screens[i].root,
1842 ButtonPressMask | wa.your_event_mask);
1844 /* set default colors */
1845 screens[i].color_focus = name_to_color("red");
1846 screens[i].color_unfocus = name_to_color("rgb:88/88/88");
1847 screens[i].bar_border = name_to_color("rgb:00/80/80");
1848 screens[i].bar_color = name_to_color("black");
1849 screens[i].bar_font_color = name_to_color("rgb:a0/a0/a0");
1851 /* init all workspaces */
1852 for (j = 0; j < SWM_WS_MAX; j++) {
1853 ws = &screens[i].ws[j];
1858 TAILQ_INIT(&ws->winlist);
1860 for (k = 0; layouts[k].l_stack != NULL; k++)
1861 if (layouts[k].l_config != NULL)
1862 layouts[k].l_config(ws,
1863 SWM_ARG_ID_STACKINIT);
1864 ws->cur_layout = &layouts[0];
1867 /* map virtual screens onto physical screens */
1868 screens[i].xrandr_support = XRRQueryExtension(display,
1869 &xrandr_eventbase, &errorbase);
1870 if (screens[i].xrandr_support)
1871 if (XRRQueryVersion(display, &major, &minor) &&
1873 screens[i].xrandr_support = 0;
1875 #if 0 /* not ready for dynamic screen changes */
1876 if (screens[i].xrandr_support)
1877 XRRSelectInput(display,
1879 RRScreenChangeNotifyMask);
1882 /* grab existing windows (before we build the bars)*/
1883 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
1886 #ifdef SWM_XRR_HAS_CRTC
1887 sr = XRRGetScreenResources(display, screens[i].root);
1889 new_region(&screens[i], &screens[i].ws[w],
1890 0, 0, DisplayWidth(display, i),
1891 DisplayHeight(display, i));
1895 for (c = 0; c < ncrtc; c++) {
1896 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
1897 if (ci->noutput == 0)
1900 if (ci != NULL && ci->mode == None)
1901 new_region(&screens[i], &screens[i].ws[w], 0, 0,
1902 DisplayWidth(display, i),
1903 DisplayHeight(display, i));
1905 new_region(&screens[i], &screens[i].ws[w],
1906 ci->x, ci->y, ci->width, ci->height);
1909 XRRFreeCrtcInfo(ci);
1910 XRRFreeScreenResources(sr);
1912 new_region(&screens[i], &screens[i].ws[w], 0, 0,
1913 DisplayWidth(display, i),
1914 DisplayHeight(display, i));
1915 #endif /* SWM_XRR_HAS_CRTC */
1917 /* attach windows to a region */
1918 /* normal windows */
1919 if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
1920 errx(1, "no regions on screen %d", i);
1922 for (i = 0; i < no; i++) {
1923 XGetWindowAttributes(display, wins[i], &wa);
1924 if (!XGetWindowAttributes(display, wins[i], &wa) ||
1925 wa.override_redirect ||
1926 XGetTransientForHint(display, wins[i], &d1))
1929 if (wa.map_state == IsViewable ||
1930 getstate(wins[i]) == NormalState)
1931 manage_window(wins[i], r->ws);
1933 /* transient windows */
1934 for (i = 0; i < no; i++) {
1935 if (!XGetWindowAttributes(display, wins[i], &wa))
1938 if (XGetTransientForHint(display, wins[i], &d1) &&
1939 (wa.map_state == IsViewable || getstate(wins[i]) ==
1941 manage_window(wins[i], r->ws);
1951 main(int argc, char *argv[])
1954 char conf[PATH_MAX], *cfile = NULL;
1961 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
1962 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
1963 warnx("no locale support");
1965 if (!(display = XOpenDisplay(0)))
1966 errx(1, "can not open display");
1969 errx(1, "other wm running");
1971 astate = XInternAtom(display, "WM_STATE", False);
1973 /* look for local and global conf file */
1974 pwd = getpwuid(getuid());
1976 errx(1, "invalid user %d", getuid());
1980 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
1981 if (stat(conf, &sb) != -1) {
1982 if (S_ISREG(sb.st_mode))
1985 /* try global conf file */
1986 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
1987 if (!stat(conf, &sb))
1988 if (S_ISREG(sb.st_mode))
1995 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2000 xfd = ConnectionNumber(display);
2003 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
2005 errx(1, "select failed");
2010 while(XPending(display)) {
2011 XNextEvent(display, &e);
2012 if (handler[e.type])
2013 handler[e.type](&e);
2017 XCloseDisplay(display);