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)
359 cp += (long)strspn(cp, SWM_CONF_WS);
360 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
363 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
366 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
367 bar_enabled = atoi(val);
368 else if (!varmatch(var, "bar_border", &i))
369 if (i > 0 && i <= sc)
370 screens[i - 1].bar_border =
373 for (i = 0; i < sc; i++)
374 screens[i].bar_border =
378 else if (!varmatch(var, "bar_color", &i))
379 if (i > 0 && i <= sc)
380 screens[i - 1].bar_color =
383 for (i = 0; i < sc; i++)
384 screens[i].bar_color =
388 else if (!varmatch(var, "bar_font_color", &i))
389 if (i > 0 && i <= sc)
390 screens[i - 1].bar_font_color =
393 for (i = 0; i < sc; i++)
394 screens[i].bar_font_color =
399 else if (!strncmp(var, "bar_font", strlen("bar_font")))
400 asprintf(&bar_fonts[0], "%s", val);
406 if (!varmatch(var, "color_focus", &i))
407 if (i > 0 && i <= sc)
408 screens[i - 1].color_focus =
411 for (i = 0; i < sc; i++)
412 screens[i].color_focus =
416 else if (!varmatch(var, "color_unfocus", &i))
417 if (i > 0 && i <= sc)
418 screens[i - 1].color_unfocus =
421 for (i = 0; i < sc; i++)
422 screens[i].color_unfocus =
426 else if (!strncmp(var, "cycle_empty", strlen("cycle_empty")))
427 cycle_visible = atoi(val);
428 else if (!strncmp(var, "cycle_visible", strlen("cycle_visible")))
429 cycle_visible = atoi(val);
435 if (!strncmp(var, "dialog_ratio",
436 strlen("dialog_ratio"))) {
437 dialog_ratio = atof(val);
438 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
445 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
446 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
457 errx(1, "invalid conf file entry: %s=%s", var, val);
459 errx(1, "invalid screen index: %s out of bounds", var);
463 bar_print(struct swm_region *r, char *s)
465 XClearWindow(display, r->bar_window);
466 XSetForeground(display, bar_gc, r->s->bar_font_color);
467 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
476 struct swm_region *r;
481 if (bar_enabled == 0)
485 localtime_r(&tmt, &tm);
486 strftime(s, sizeof s, "%a %b %d %R %Z %Y", &tm);
487 for (i = 0; i < ScreenCount(display); i++) {
489 TAILQ_FOREACH(r, &screens[i].rl, entry) {
490 snprintf(e, sizeof e, "%s %d:%d",
491 s, x++, r->ws->idx + 1);
495 XSync(display, False);
506 bar_toggle(struct swm_region *r, union arg *args)
508 struct swm_region *tmpr;
509 int i, j, sc = ScreenCount(display);
511 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
514 for (i = 0; i < sc; i++)
515 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
516 XUnmapWindow(display, tmpr->bar_window);
518 for (i = 0; i < sc; i++)
519 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
520 XMapRaised(display, tmpr->bar_window);
522 bar_enabled = !bar_enabled;
523 XSync(display, False);
524 for (i = 0; i < sc; i++)
525 for (j = 0; j < SWM_WS_MAX; j++)
526 screens[i].ws[j].restack = 1;
529 /* must be after stack */
530 for (i = 0; i < sc; i++)
531 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
536 bar_setup(struct swm_region *r)
540 for (i = 0; bar_fonts[i] != NULL; i++) {
541 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
545 if (bar_fonts[i] == NULL)
546 errx(1, "couldn't load font");
547 bar_height = bar_fs->ascent + bar_fs->descent + 3;
549 r->bar_window = XCreateSimpleWindow(display,
550 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
551 1, r->s->bar_border, r->s->bar_color);
552 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
553 XSetFont(display, bar_gc, bar_fs->fid);
554 XSelectInput(display, r->bar_window, VisibilityChangeMask);
556 XMapRaised(display, r->bar_window);
557 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
559 if (signal(SIGALRM, bar_signal) == SIG_ERR)
560 err(1, "could not install bar_signal");
567 XSetWindowAttributes wa;
568 struct swm_region *r;
571 bzero(&wa, sizeof wa);
572 for (i = 0; i < ScreenCount(display); i++)
573 TAILQ_FOREACH(r, &screens[i].rl, entry) {
574 wa.border_pixel = screens[i].bar_border;
575 wa.background_pixel = screens[i].bar_color;
576 XChangeWindowAttributes(display, r->bar_window,
577 CWBackPixel | CWBorderPixel, &wa);
583 config_win(struct ws_win *win)
587 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
588 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
589 ce.type = ConfigureNotify;
590 ce.display = display;
596 ce.height = win->g.h;
597 ce.border_width = 1; /* XXX store this! */
599 ce.override_redirect = False;
600 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
604 count_win(struct workspace *ws, int count_transient)
609 TAILQ_FOREACH(win, &ws->winlist, entry) {
610 if (count_transient == 0 && win->floating)
612 if (count_transient == 0 && win->transient)
616 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
622 quit(struct swm_region *r, union arg *args)
624 DNPRINTF(SWM_D_MISC, "quit\n");
629 restart(struct swm_region *r, union arg *args)
631 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
633 /* disable alarm because the following code may not be interrupted */
635 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
636 errx(1, "can't disable alarm");
638 XCloseDisplay(display);
639 execvp(start_argv[0], start_argv);
640 fprintf(stderr, "execvp failed\n");
646 root_to_region(Window root)
648 struct swm_region *r;
653 for (i = 0; i < ScreenCount(display); i++)
654 if (screens[i].root == root)
657 if (rootclick != root && /* if root was just clicked in, use cursor */
658 cur_focus && cur_focus->ws->r && cur_focus->s == &screens[i])
659 r = cur_focus->ws->r;
661 if (XQueryPointer(display, screens[i].root,
662 &rr, &cr, &x, &y, &wx, &wy, &mask) == False) {
663 r = TAILQ_FIRST(&screens[i].rl);
665 TAILQ_FOREACH(r, &screens[i].rl, entry) {
666 if (x > X(r) && x < X(r) + WIDTH(r) &&
667 y > Y(r) && y < Y(r) + HEIGHT(r))
672 r = TAILQ_FIRST(&screens[i].rl);
679 find_window(Window id)
684 for (i = 0; i < ScreenCount(display); i++)
685 for (j = 0; j < SWM_WS_MAX; j++)
686 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
693 spawn(struct swm_region *r, union arg *args)
695 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
697 * The double-fork construct avoids zombie processes and keeps the code
698 * clean from stupid signal handlers.
703 close(ConnectionNumber(display));
705 execvp(args->argv[0], args->argv);
706 fprintf(stderr, "execvp failed\n");
715 unfocus_win(struct ws_win *win)
717 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
718 if (win->ws->r && win->focus)
719 XSetWindowBorder(display, win->id,
720 win->ws->r->s->color_unfocus);
722 if (win->ws->focus == win)
723 win->ws->focus = NULL;
724 if (cur_focus == win)
730 focus_win(struct ws_win *win)
732 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
736 win->ws->focus = win;
737 if (win->ws->r != NULL) {
738 if (cur_focus && cur_focus != win)
739 unfocus_win(cur_focus);
742 XSetWindowBorder(display, win->id,
743 win->ws->r->s->color_focus);
745 XSetInputFocus(display, win->id,
746 RevertToPointerRoot, CurrentTime);
751 switchws(struct swm_region *r, union arg *args)
754 struct swm_region *this_r, *other_r;
756 struct workspace *new_ws, *old_ws;
760 new_ws = &this_r->s->ws[wsid];
762 DNPRINTF(SWM_D_WS, "switchws screen %d region %dx%d+%d+%d: "
763 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
766 if (new_ws == old_ws)
771 /* if the other workspace is hidden, switch windows */
772 /* map new window first to prevent ugly blinking */
773 TAILQ_FOREACH(win, &new_ws->winlist, entry)
774 XMapRaised(display, win->id);
776 TAILQ_FOREACH(win, &old_ws->winlist, entry)
777 XUnmapWindow(display, win->id);
782 other_r->ws = old_ws;
791 focus_win(new_ws->focus);
797 cyclews(struct swm_region *r, union arg *args)
800 struct swm_screen *s = r->s;
802 DNPRINTF(SWM_D_WS, "cyclews id %d "
803 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
804 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
810 case SWM_ARG_ID_CYCLEWS_UP:
811 if (a.id < SWM_WS_MAX - 1)
816 case SWM_ARG_ID_CYCLEWS_DOWN:
820 a.id = SWM_WS_MAX - 1;
826 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
828 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
832 } while (a.id != r->ws->idx);
836 swapwin(struct swm_region *r, union arg *args)
838 struct ws_win *target;
839 struct ws_win_list *wl;
842 DNPRINTF(SWM_D_WS, "swapwin id %d "
843 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
844 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
845 if (cur_focus == NULL)
848 wl = &cur_focus->ws->winlist;
851 case SWM_ARG_ID_SWAPPREV:
852 target = TAILQ_PREV(cur_focus, ws_win_list, entry);
853 TAILQ_REMOVE(wl, cur_focus, entry);
855 TAILQ_INSERT_TAIL(wl, cur_focus, entry);
857 TAILQ_INSERT_BEFORE(target, cur_focus, entry);
859 case SWM_ARG_ID_SWAPNEXT:
860 target = TAILQ_NEXT(cur_focus, entry);
861 TAILQ_REMOVE(wl, cur_focus, entry);
863 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
865 TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
867 case SWM_ARG_ID_SWAPMAIN:
868 target = TAILQ_FIRST(wl);
869 if (target == cur_focus)
871 TAILQ_REMOVE(wl, target, entry);
872 TAILQ_INSERT_BEFORE(cur_focus, target, entry);
873 TAILQ_REMOVE(wl, cur_focus, entry);
874 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
877 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
886 focus(struct swm_region *r, union arg *args)
888 struct ws_win *winfocus, *winlostfocus;
889 struct ws_win_list *wl;
891 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
892 if (cur_focus == NULL)
895 wl = &cur_focus->ws->winlist;
897 winlostfocus = cur_focus;
900 case SWM_ARG_ID_FOCUSPREV:
901 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
902 if (winfocus == NULL)
903 winfocus = TAILQ_LAST(wl, ws_win_list);
906 case SWM_ARG_ID_FOCUSNEXT:
907 winfocus = TAILQ_NEXT(cur_focus, entry);
908 if (winfocus == NULL)
909 winfocus = TAILQ_FIRST(wl);
912 case SWM_ARG_ID_FOCUSMAIN:
913 winfocus = TAILQ_FIRST(wl);
920 if (winfocus == winlostfocus)
924 XSync(display, False);
928 cycle_layout(struct swm_region *r, union arg *args)
930 struct workspace *ws = r->ws;
932 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
935 if (ws->cur_layout->l_stack == NULL)
936 ws->cur_layout = &layouts[0];
943 stack_config(struct swm_region *r, union arg *args)
945 struct workspace *ws = r->ws;
947 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
950 if (ws->cur_layout->l_config != NULL)
951 ws->cur_layout->l_config(ws, args->id);
953 if (args->id != SWM_ARG_ID_STACKINIT);
959 struct swm_geometry g;
960 struct swm_region *r;
963 DNPRINTF(SWM_D_STACK, "stack\n");
965 for (i = 0; i < ScreenCount(display); i++) {
967 TAILQ_FOREACH(r, &screens[i].rl, entry) {
968 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
969 "(screen %d, region %d)\n", r->ws->idx, i, j++);
971 /* start with screen geometry, adjust for bar */
981 r->ws->cur_layout->l_stack(r->ws, &g);
984 XSync(display, False);
988 stack_floater(struct ws_win *win, struct swm_region *r)
993 bzero(&wc, sizeof wc);
994 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
996 if (win->transient) {
997 win->g.w = (double)WIDTH(r) * dialog_ratio;
998 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1000 wc.width = win->g.w;
1001 wc.height = win->g.h;
1002 wc.x = (WIDTH(r) - win->g.w) / 2;
1003 wc.y = (HEIGHT(r) - win->g.h) / 2;
1005 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1006 win->id, wc.x, wc.y, wc.width, wc.height);
1008 XConfigureWindow(display, win->id, mask, &wc);
1013 vertical_config(struct workspace *ws, int id)
1015 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1018 case SWM_ARG_ID_STACKRESET:
1019 case SWM_ARG_ID_STACKINIT:
1020 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1021 ws->l_state.vertical_mwin = 1;
1023 case SWM_ARG_ID_MASTERSHRINK:
1024 if (ws->l_state.vertical_msize > 1)
1025 ws->l_state.vertical_msize--;
1027 case SWM_ARG_ID_MASTERGROW:
1028 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1029 ws->l_state.vertical_msize++;
1031 case SWM_ARG_ID_MASTERADD:
1032 ws->l_state.vertical_mwin++;
1034 case SWM_ARG_ID_MASTERDEL:
1035 if (ws->l_state.vertical_mwin > 0)
1036 ws->l_state.vertical_mwin--;
1044 vertical_stack(struct workspace *ws, struct swm_geometry *g) {
1046 struct swm_geometry gg = *g;
1047 struct ws_win *win, *winfocus;
1048 int i, j, split, colno, hrh, winno, main_width;
1051 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1053 if ((winno = count_win(ws, 0)) == 0)
1056 if (ws->focus == NULL)
1057 ws->focus = TAILQ_FIRST(&ws->winlist);
1058 winfocus = cur_focus ? cur_focus : ws->focus;
1060 if (ws->l_state.vertical_mwin &&
1061 winno > ws->l_state.vertical_mwin) {
1062 split = ws->l_state.vertical_mwin;
1064 main_width = (g->w / SWM_V_SLICE) *
1065 ws->l_state.vertical_msize;
1075 TAILQ_FOREACH(win, &ws->winlist, entry) {
1076 if (split && i == split) {
1077 colno = winno - split;
1078 hrh = (g->h / colno);
1079 gg.x += main_width + 2;
1080 gg.w = g->w - (main_width + 2);
1085 gg.h = (hrh + (g->h - (colno * hrh)));
1093 if (win->transient != 0 || win->floating != 0)
1094 stack_floater(win, ws->r);
1096 bzero(&wc, sizeof wc);
1097 wc.border_width = 1;
1098 win->g.x = wc.x = gg.x;
1099 win->g.y = wc.y = gg.y;
1100 win->g.w = wc.width = gg.w;
1101 win->g.h = wc.height = gg.h;
1102 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1103 XConfigureWindow(display, win->id, mask, &wc);
1106 XMapRaised(display, win->id);
1112 focus_win(winfocus); /* has to be done outside of the loop */
1117 horizontal_config(struct workspace *ws, int id)
1119 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1122 case SWM_ARG_ID_STACKRESET:
1123 case SWM_ARG_ID_STACKINIT:
1124 ws->l_state.horizontal_mwin = 1;
1125 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1127 case SWM_ARG_ID_MASTERSHRINK:
1128 if (ws->l_state.horizontal_msize > 1)
1129 ws->l_state.horizontal_msize--;
1131 case SWM_ARG_ID_MASTERGROW:
1132 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1133 ws->l_state.horizontal_msize++;
1135 case SWM_ARG_ID_MASTERADD:
1136 ws->l_state.horizontal_mwin++;
1138 case SWM_ARG_ID_MASTERDEL:
1139 if (ws->l_state.horizontal_mwin > 0)
1140 ws->l_state.horizontal_mwin--;
1148 horizontal_stack(struct workspace *ws, struct swm_geometry *g) {
1150 struct swm_geometry gg = *g;
1151 struct ws_win *win, *winfocus;
1152 int i, j, split, rowno, hrw, winno, main_height;
1155 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
1157 if ((winno = count_win(ws, 0)) == 0)
1160 if (ws->focus == NULL)
1161 ws->focus = TAILQ_FIRST(&ws->winlist);
1162 winfocus = cur_focus ? cur_focus : ws->focus;
1164 if (ws->l_state.horizontal_mwin &&
1165 winno > ws->l_state.horizontal_mwin) {
1166 split = ws->l_state.horizontal_mwin;
1168 main_height = (g->h / SWM_V_SLICE) *
1169 ws->l_state.horizontal_msize;
1179 TAILQ_FOREACH(win, &ws->winlist, entry) {
1180 if (split && i == split) {
1181 rowno = winno - split;
1182 hrw = (g->w / rowno);
1183 gg.y += main_height + 2;
1184 gg.h = g->h - (main_height + 2);
1189 gg.w = (hrw + (g->w - (rowno * hrw)));
1196 if (win->transient != 0 || win->floating != 0)
1197 stack_floater(win, ws->r);
1199 bzero(&wc, sizeof wc);
1200 wc.border_width = 1;
1201 win->g.x = wc.x = gg.x;
1202 win->g.y = wc.y = gg.y;
1203 win->g.w = wc.width = gg.w;
1204 win->g.h = wc.height = gg.h;
1205 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1206 XConfigureWindow(display, win->id, mask, &wc);
1209 XMapRaised(display, win->id);
1215 focus_win(winfocus); /* this has to be done outside of the loop */
1218 /* fullscreen view */
1220 max_stack(struct workspace *ws, struct swm_geometry *g) {
1222 struct swm_geometry gg = *g;
1223 struct ws_win *win, *winfocus;
1226 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1228 if (count_win(ws, 0) == 0)
1231 if (ws->focus == NULL)
1232 ws->focus = TAILQ_FIRST(&ws->winlist);
1233 winfocus = cur_focus ? cur_focus : ws->focus;
1235 TAILQ_FOREACH(win, &ws->winlist, entry) {
1236 if (win->transient != 0 || win->floating != 0) {
1237 if (win == ws->focus) {
1239 stack_floater(win, ws->r);
1240 XMapRaised(display, win->id);
1242 XUnmapWindow(display, win->id);
1244 bzero(&wc, sizeof wc);
1245 wc.border_width = 1;
1246 win->g.x = wc.x = gg.x;
1247 win->g.y = wc.y = gg.y;
1248 win->g.w = wc.width = gg.w;
1249 win->g.h = wc.height = gg.h;
1250 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1251 XConfigureWindow(display, win->id, mask, &wc);
1253 if (win == ws->focus) {
1254 XMapRaised(display, win->id);
1256 XUnmapWindow(display, win->id);
1261 focus_win(winfocus); /* has to be done outside of the loop */
1265 send_to_ws(struct swm_region *r, union arg *args)
1267 int wsid = args->id;
1268 struct ws_win *win = cur_focus;
1269 struct workspace *ws, *nws;
1271 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1274 nws = &win->s->ws[wsid];
1276 XUnmapWindow(display, win->id);
1278 /* find a window to focus */
1279 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1280 if (ws->focus == NULL)
1281 ws->focus = TAILQ_FIRST(&ws->winlist);
1282 if (ws->focus == win)
1285 TAILQ_REMOVE(&ws->winlist, win, entry);
1287 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1290 if (count_win(nws, 1) == 1)
1298 /* key definitions */
1302 void (*func)(struct swm_region *r, union arg *);
1305 /* modifier key function argument */
1306 { MODKEY, XK_space, cycle_layout, {0} },
1307 { MODKEY | ShiftMask, XK_space, stack_config, {.id = SWM_ARG_ID_STACKRESET} },
1308 { MODKEY, XK_h, stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
1309 { MODKEY, XK_l, stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
1310 { MODKEY, XK_comma, stack_config, {.id = SWM_ARG_ID_MASTERADD} },
1311 { MODKEY, XK_period, stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
1312 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1313 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1314 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1315 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1316 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1317 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1318 { MODKEY, XK_p, spawn, {.argv = spawn_menu} },
1319 { MODKEY | ShiftMask, XK_q, quit, {0} },
1320 { MODKEY, XK_q, restart, {0} },
1321 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1322 { MODKEY, XK_1, switchws, {.id = 0} },
1323 { MODKEY, XK_2, switchws, {.id = 1} },
1324 { MODKEY, XK_3, switchws, {.id = 2} },
1325 { MODKEY, XK_4, switchws, {.id = 3} },
1326 { MODKEY, XK_5, switchws, {.id = 4} },
1327 { MODKEY, XK_6, switchws, {.id = 5} },
1328 { MODKEY, XK_7, switchws, {.id = 6} },
1329 { MODKEY, XK_8, switchws, {.id = 7} },
1330 { MODKEY, XK_9, switchws, {.id = 8} },
1331 { MODKEY, XK_0, switchws, {.id = 9} },
1332 { MODKEY, XK_Right, cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
1333 { MODKEY, XK_Left, cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
1334 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1335 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1336 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1337 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1338 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1339 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1340 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1341 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1342 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1343 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1344 { MODKEY, XK_b, bar_toggle, {0} },
1345 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1346 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1350 updatenumlockmask(void)
1353 XModifierKeymap *modmap;
1355 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1357 modmap = XGetModifierMapping(display);
1358 for (i = 0; i < 8; i++)
1359 for (j = 0; j < modmap->max_keypermod; j++)
1360 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1361 == XKeysymToKeycode(display, XK_Num_Lock))
1362 numlockmask = (1 << i);
1364 XFreeModifiermap(modmap);
1370 unsigned int i, j, k;
1372 unsigned int modifiers[] =
1373 { 0, LockMask, numlockmask, numlockmask | LockMask };
1375 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1376 updatenumlockmask();
1378 for (k = 0; k < ScreenCount(display); k++) {
1379 if (TAILQ_EMPTY(&screens[k].rl))
1381 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
1382 for (i = 0; i < LENGTH(keys); i++) {
1383 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
1384 for (j = 0; j < LENGTH(modifiers); j++)
1385 XGrabKey(display, code,
1386 keys[i].mod | modifiers[j],
1387 screens[k].root, True,
1388 GrabModeAsync, GrabModeAsync);
1395 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1403 XKeyEvent *ev = &e->xkey;
1405 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1407 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1408 for (i = 0; i < LENGTH(keys); i++)
1409 if (keysym == keys[i].keysym
1410 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1412 keys[i].func(root_to_region(ev->root),
1417 buttonpress(XEvent *e)
1419 XButtonPressedEvent *ev = &e->xbutton;
1420 #ifdef SWM_CLICKTOFOCUS
1422 struct workspace *ws;
1423 struct swm_region *r;
1426 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1428 if (ev->window == ev->root) {
1429 rootclick = ev->root;
1432 if (ev->window == cur_focus->id)
1434 #ifdef SWM_CLICKTOFOCUS
1435 r = root_to_region(ev->root);
1437 TAILQ_FOREACH(win, &ws->winlist, entry)
1438 if (win->id == ev->window) {
1439 /* focus in the clicked window */
1440 XSetWindowBorder(display, ev->window, 0xff0000);
1441 XSetWindowBorder(display, ws->focus->id, 0x888888);
1442 XSetInputFocus(display, ev->window, RevertToPointerRoot,
1445 XSync(display, False);
1452 set_win_state(struct ws_win *win, long state)
1454 long data[] = {state, None};
1456 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1458 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1459 (unsigned char *)data, 2);
1463 manage_window(Window id, struct workspace *ws)
1469 TAILQ_FOREACH(win, &ws->winlist, entry) {
1471 return (win); /* already being managed */
1474 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1475 errx(1, "calloc: failed to allocate memory for new window");
1479 win->s = ws->r->s; /* this never changes */
1480 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
1482 /* make new win focused */
1485 XGetTransientForHint(display, win->id, &trans);
1487 win->transient = trans;
1488 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1489 (unsigned)win->id, win->transient);
1491 XGetWindowAttributes(display, id, &win->wa);
1492 win->g.w = win->wa.width;
1493 win->g.h = win->wa.height;
1494 win->g.x = win->wa.x;
1495 win->g.y = win->wa.y;
1497 /* XXX make this a table */
1498 bzero(&ch, sizeof ch);
1499 if (XGetClassHint(display, win->id, &ch)) {
1500 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1501 if (!strcmp(ch.res_class, "MPlayer") && !strcmp(ch.res_name, "xv")) {
1505 XFree(ch.res_class);
1510 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
1511 PropertyChangeMask | StructureNotifyMask);
1513 set_win_state(win, NormalState);
1519 configurerequest(XEvent *e)
1521 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1526 if ((win = find_window(ev->window)) == NULL)
1530 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
1532 bzero(&wc, sizeof wc);
1535 wc.width = ev->width;
1536 wc.height = ev->height;
1537 wc.border_width = ev->border_width;
1538 wc.sibling = ev->above;
1539 wc.stack_mode = ev->detail;
1540 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
1542 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
1544 if (win->floating) {
1545 if (ev->value_mask & CWX)
1547 if (ev->value_mask & CWY)
1549 if (ev->value_mask & CWWidth)
1550 win->g.w = ev->width;
1551 if (ev->value_mask & CWHeight)
1552 win->g.h = ev->height;
1553 if (win->ws->r != NULL) {
1554 /* this seems to be full screen */
1555 if (win->g.w > WIDTH(win->ws->r)) {
1560 if (win->g.h > HEIGHT(win->ws->r)) {
1566 if ((ev->value_mask & (CWX|CWY)) &&
1567 !(ev->value_mask & (CWWidth|CWHeight)))
1569 XMoveResizeWindow(display, win->id,
1570 win->g.x, win->g.y, win->g.w, win->g.h);
1577 configurenotify(XEvent *e)
1579 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1580 e->xconfigure.window);
1584 destroynotify(XEvent *e)
1587 XDestroyWindowEvent *ev = &e->xdestroywindow;
1589 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1591 if ((win = find_window(ev->window)) != NULL) {
1592 struct workspace *ws = win->ws;
1593 /* find a window to focus */
1594 if (ws->focus == win)
1595 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1596 if (ws->focus == NULL)
1597 ws->focus = TAILQ_FIRST(&ws->winlist);
1598 if (ws->focus == win)
1600 if (cur_focus == win) {
1601 if (ws->focus == NULL)
1602 unfocus_win(win); /* XXX focus another ws? */
1604 focus_win(ws->focus);
1607 TAILQ_REMOVE(&ws->winlist, win, entry);
1608 set_win_state(win, WithdrawnState);
1615 enternotify(XEvent *e)
1617 XCrossingEvent *ev = &e->xcrossing;
1621 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1623 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
1624 ev->window != ev->root)
1627 /* eat event(r) to prevent autofocus */
1631 /* brute force for now */
1632 for (i = 0; i < ScreenCount(display); i++) {
1633 for (j = 0; j < SWM_WS_MAX; j++) {
1634 TAILQ_FOREACH(win, &screens[i].ws[j].winlist , entry) {
1635 if (win->id == ev->window)
1645 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
1647 /* XXX this probably needs better handling now.
1648 if (ev->window == ev->root)
1652 * kill grab for now so that we can cut and paste , this screws up
1656 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1657 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1658 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1663 mappingnotify(XEvent *e)
1665 XMappingEvent *ev = &e->xmapping;
1667 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1669 XRefreshKeyboardMapping(ev);
1670 if (ev->request == MappingKeyboard)
1675 maprequest(XEvent *e)
1677 XMapRequestEvent *ev = &e->xmaprequest;
1678 XWindowAttributes wa;
1679 struct swm_region *r;
1681 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1682 e->xmaprequest.window);
1684 if (!XGetWindowAttributes(display, ev->window, &wa))
1686 if (wa.override_redirect)
1688 r = root_to_region(wa.root);
1689 manage_window(e->xmaprequest.window, r->ws);
1694 propertynotify(XEvent *e)
1696 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1697 e->xproperty.window);
1701 unmapnotify(XEvent *e)
1703 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1707 visibilitynotify(XEvent *e)
1710 struct swm_region *r;
1712 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
1713 e->xvisibility.window);
1714 if (e->xvisibility.state == VisibilityUnobscured)
1715 for (i = 0; i < ScreenCount(display); i++)
1716 TAILQ_FOREACH(r, &screens[i].rl, entry)
1717 if (e->xvisibility.window == r->bar_window)
1721 void (*handler[LASTEvent])(XEvent *) = {
1723 [KeyPress] = keypress,
1724 [ButtonPress] = buttonpress,
1725 [ConfigureRequest] = configurerequest,
1726 [ConfigureNotify] = configurenotify,
1727 [DestroyNotify] = destroynotify,
1728 [EnterNotify] = enternotify,
1729 [FocusIn] = focusin,
1730 [MappingNotify] = mappingnotify,
1731 [MapRequest] = maprequest,
1732 [PropertyNotify] = propertynotify,
1733 [UnmapNotify] = unmapnotify,
1734 [VisibilityNotify] = visibilitynotify,
1738 xerror_start(Display *d, XErrorEvent *ee)
1745 xerror(Display *d, XErrorEvent *ee)
1747 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1755 xerrorxlib = XSetErrorHandler(xerror_start);
1757 /* this causes an error if some other window manager is running */
1758 XSelectInput(display, DefaultRootWindow(display),
1759 SubstructureRedirectMask);
1760 XSync(display, False);
1764 XSetErrorHandler(xerror);
1765 XSync(display, False);
1774 unsigned char *p = NULL;
1775 unsigned long n, extra;
1778 astate = XInternAtom(display, "WM_STATE", False);
1779 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
1780 &real, &format, &n, &extra, (unsigned char **)&p);
1781 if (status != Success)
1790 new_region(struct swm_screen *s, struct workspace *ws,
1791 int x, int y, int w, int h)
1793 struct swm_region *r;
1795 DNPRINTF(SWM_D_MISC, "new region on screen %d: %dx%d (%d, %d)\n",
1796 s->idx, x, y, w, h);
1798 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
1799 errx(1, "calloc: failed to allocate memory for screen");
1808 TAILQ_INSERT_TAIL(&s->rl, r, entry);
1815 #ifdef SWM_XRR_HAS_CRTC
1817 XRRScreenResources *sr;
1819 #endif /* SWM_XRR_HAS_CRTC */
1820 Window d1, d2, *wins = NULL;
1821 XWindowAttributes wa;
1822 struct swm_region *r;
1824 int errorbase, major, minor;
1825 int ncrtc = 0, w = 0;
1827 struct workspace *ws;
1829 if ((screens = calloc(ScreenCount(display),
1830 sizeof(struct swm_screen))) == NULL)
1831 errx(1, "calloc: screens");
1833 /* map physical screens */
1834 for (i = 0; i < ScreenCount(display); i++) {
1835 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
1837 TAILQ_INIT(&screens[i].rl);
1838 screens[i].root = RootWindow(display, i);
1839 XGetWindowAttributes(display, screens[i].root, &wa);
1840 XSelectInput(display, screens[i].root,
1841 ButtonPressMask | wa.your_event_mask);
1843 /* set default colors */
1844 screens[i].color_focus = name_to_color("red");
1845 screens[i].color_unfocus = name_to_color("rgb:88/88/88");
1846 screens[i].bar_border = name_to_color("rgb:00/80/80");
1847 screens[i].bar_color = name_to_color("black");
1848 screens[i].bar_font_color = name_to_color("rgb:a0/a0/a0");
1850 /* init all workspaces */
1851 for (j = 0; j < SWM_WS_MAX; j++) {
1852 ws = &screens[i].ws[j];
1857 TAILQ_INIT(&ws->winlist);
1859 for (k = 0; layouts[k].l_stack != NULL; k++)
1860 if (layouts[k].l_config != NULL)
1861 layouts[k].l_config(ws,
1862 SWM_ARG_ID_STACKINIT);
1863 ws->cur_layout = &layouts[0];
1866 /* map virtual screens onto physical screens */
1867 screens[i].xrandr_support = XRRQueryExtension(display,
1868 &xrandr_eventbase, &errorbase);
1869 if (screens[i].xrandr_support)
1870 if (XRRQueryVersion(display, &major, &minor) &&
1872 screens[i].xrandr_support = 0;
1874 #if 0 /* not ready for dynamic screen changes */
1875 if (screens[i].xrandr_support)
1876 XRRSelectInput(display,
1878 RRScreenChangeNotifyMask);
1881 /* grab existing windows (before we build the bars)*/
1882 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
1885 #ifdef SWM_XRR_HAS_CRTC
1886 sr = XRRGetScreenResources(display, screens[i].root);
1888 new_region(&screens[i], &screens[i].ws[w],
1889 0, 0, DisplayWidth(display, i),
1890 DisplayHeight(display, i));
1894 for (c = 0; c < ncrtc; c++) {
1895 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
1896 if (ci->noutput == 0)
1899 if (ci != NULL && ci->mode == None)
1900 new_region(&screens[i], &screens[i].ws[w], 0, 0,
1901 DisplayWidth(display, i),
1902 DisplayHeight(display, i));
1904 new_region(&screens[i], &screens[i].ws[w],
1905 ci->x, ci->y, ci->width, ci->height);
1908 XRRFreeCrtcInfo(ci);
1909 XRRFreeScreenResources(sr);
1911 new_region(&screens[i], &screens[i].ws[w], 0, 0,
1912 DisplayWidth(display, i),
1913 DisplayHeight(display, i));
1914 #endif /* SWM_XRR_HAS_CRTC */
1916 /* attach windows to a region */
1917 /* normal windows */
1918 if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
1919 errx(1, "no regions on screen %d", i);
1921 for (i = 0; i < no; i++) {
1922 XGetWindowAttributes(display, wins[i], &wa);
1923 if (!XGetWindowAttributes(display, wins[i], &wa) ||
1924 wa.override_redirect ||
1925 XGetTransientForHint(display, wins[i], &d1))
1928 if (wa.map_state == IsViewable ||
1929 getstate(wins[i]) == NormalState)
1930 manage_window(wins[i], r->ws);
1932 /* transient windows */
1933 for (i = 0; i < no; i++) {
1934 if (!XGetWindowAttributes(display, wins[i], &wa))
1937 if (XGetTransientForHint(display, wins[i], &d1) &&
1938 (wa.map_state == IsViewable || getstate(wins[i]) ==
1940 manage_window(wins[i], r->ws);
1950 main(int argc, char *argv[])
1953 char conf[PATH_MAX], *cfile = NULL;
1960 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
1961 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
1962 warnx("no locale support");
1964 if (!(display = XOpenDisplay(0)))
1965 errx(1, "can not open display");
1968 errx(1, "other wm running");
1970 astate = XInternAtom(display, "WM_STATE", False);
1972 /* look for local and global conf file */
1973 pwd = getpwuid(getuid());
1975 errx(1, "invalid user %d", getuid());
1979 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
1980 if (stat(conf, &sb) != -1) {
1981 if (S_ISREG(sb.st_mode))
1984 /* try global conf file */
1985 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
1986 if (!stat(conf, &sb))
1987 if (S_ISREG(sb.st_mode))
1994 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
1999 xfd = ConnectionNumber(display);
2002 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
2004 errx(1, "select failed");
2009 while(XPending(display)) {
2010 XNextEvent(display, &e);
2011 if (handler[e.type])
2012 handler[e.type](&e);
2016 XCloseDisplay(display);