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"
65 #include <sys/types.h>
69 #include <sys/queue.h>
70 #include <sys/param.h>
71 #include <sys/select.h>
73 #include <X11/cursorfont.h>
74 #include <X11/keysym.h>
75 #include <X11/Xatom.h>
77 #include <X11/Xproto.h>
78 #include <X11/Xutil.h>
79 #include <X11/extensions/Xrandr.h>
82 # error XRandR versions less than 1.0 are not supported
87 #define SWM_XRR_HAS_CRTC
91 /* #define SWM_DEBUG */
93 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
94 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
95 #define SWM_D_MISC 0x0001
96 #define SWM_D_EVENT 0x0002
97 #define SWM_D_WS 0x0004
98 #define SWM_D_FOCUS 0x0008
99 #define SWM_D_MOVE 0x0010
100 #define SWM_D_STACK 0x0020
102 u_int32_t swm_debug = 0
111 #define DPRINTF(x...)
112 #define DNPRINTF(n,x...)
115 #define LENGTH(x) (sizeof x / sizeof x[0])
116 #define MODKEY Mod1Mask
117 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
119 #define X(r) (r)->g.x
120 #define Y(r) (r)->g.y
121 #define WIDTH(r) (r)->g.w
122 #define HEIGHT(r) (r)->g.h
126 int (*xerrorxlib)(Display *, XErrorEvent *);
129 int xrandr_eventbase;
130 int ignore_enter = 0;
131 unsigned int numlockmask = 0;
135 double dialog_ratio = .6;
137 #define SWM_BAR_MAX (128)
138 sig_atomic_t bar_alarm = 0;
145 char *bar_fonts[] = {
146 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
147 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
151 /* terminal + args */
152 char *spawn_term[] = { "xterm", NULL };
153 char *spawn_menu[] = { "dmenu_run", NULL };
155 /* layout manager data */
156 struct swm_geometry {
166 /* virtual "screens" */
168 TAILQ_ENTRY(swm_region) entry;
169 struct swm_geometry g;
171 struct workspace *ws; /* current workspace on this region */
172 struct swm_screen *s; /* screen idx */
174 TAILQ_HEAD(swm_region_list, swm_region);
178 TAILQ_ENTRY(ws_win) entry;
180 struct swm_geometry g;
184 struct workspace *ws; /* always valid */
185 struct swm_screen *s; /* always valid, never changes */
186 XWindowAttributes wa;
188 TAILQ_HEAD(ws_win_list, ws_win);
190 /* layout handlers */
192 void vertical_init(struct workspace *);
193 void vertical_resize(struct workspace *, int);
194 void vertical_stack(struct workspace *, struct swm_geometry *);
195 void horizontal_init(struct workspace *);
196 void horizontal_resize(struct workspace *, int);
197 void horizontal_stack(struct workspace *, struct swm_geometry *);
198 void max_init(struct workspace *);
199 void max_stack(struct workspace *, struct swm_geometry *);
202 void (*l_init)(struct workspace *); /* init/reset */
203 void (*l_stack)(struct workspace *, struct swm_geometry *);
204 void (*l_resize)(struct workspace *, int);
206 /* init stack, resize */
207 { vertical_init, vertical_stack, vertical_resize},
208 { horizontal_init, horizontal_stack, horizontal_resize},
209 { NULL, max_stack, NULL},
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;
232 /* physical screen mapping */
233 #define SWM_WS_MAX (10) /* XXX Too small? */
235 int idx; /* screen index */
236 struct swm_region_list rl; /* list of regions on this screen */
239 struct workspace ws[SWM_WS_MAX];
242 unsigned long bar_border;
243 unsigned long bar_color;
244 unsigned long bar_font_color;
245 unsigned long color_focus; /* XXX should this be per ws? */
246 unsigned long color_unfocus;
247 char bar_text[SWM_BAR_MAX];
249 struct swm_screen *screens;
251 Window rootclick = 0;
253 struct ws_win *cur_focus = NULL;
255 /* args to functions */
258 #define SWM_ARG_ID_FOCUSNEXT (0)
259 #define SWM_ARG_ID_FOCUSPREV (1)
260 #define SWM_ARG_ID_FOCUSMAIN (2)
261 #define SWM_ARG_ID_SWAPNEXT (3)
262 #define SWM_ARG_ID_SWAPPREV (4)
263 #define SWM_ARG_ID_SWAPMAIN (5)
264 #define SWM_ARG_ID_MASTERSHRINK (6)
265 #define SWM_ARG_ID_MASTERGROW (7)
270 name_to_color(char *colorname)
274 XColor screen_def, exact_def;
275 unsigned long result = 0;
276 char cname[32] = "#";
278 cmap = DefaultColormap(display, screens[0].idx);
279 status = XAllocNamedColor(display, cmap, colorname,
280 &screen_def, &exact_def);
282 strlcat(cname, colorname + 2, sizeof cname - 1);
283 status = XAllocNamedColor(display, cmap, cname, &screen_def,
287 result = screen_def.pixel;
289 fprintf(stderr, "color '%s' not found.\n", colorname);
294 /* conf file stuff */
295 #define SWM_CONF_WS "\n= \t"
296 #define SWM_CONF_FILE "scrotwm.conf"
298 conf_load(char *filename)
301 char *line, *cp, *var, *val;
302 size_t len, lineno = 0;
305 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
307 if (filename == NULL)
310 if ((config = fopen(filename, "r")) == NULL)
314 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
318 cp += (long)strspn(cp, SWM_CONF_WS);
324 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
326 cp += (long)strspn(cp, SWM_CONF_WS);
327 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
330 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
333 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
334 bar_enabled = atoi(val);
335 else if (!strncmp(var, "bar_border",
336 strlen("bar_border")))
337 for (i = 0; i < ScreenCount(display); i++)
338 screens[i].bar_border = name_to_color(val);
339 else if (!strncmp(var, "bar_color",
340 strlen("bar_color")))
341 for (i = 0; i < ScreenCount(display); i++)
342 screens[i].bar_color = name_to_color(val);
343 else if (!strncmp(var, "bar_font_color",
344 strlen("bar_font_color")))
345 for (i = 0; i < ScreenCount(display); i++)
346 screens[i].bar_font_color = name_to_color(val);
347 else if (!strncmp(var, "bar_font", strlen("bar_font")))
348 asprintf(&bar_fonts[0], "%s", val);
354 if (!strncmp(var, "color_focus", strlen("color_focus")))
355 for (i = 0; i < ScreenCount(display); i++)
356 screens[i].color_focus = name_to_color(val);
357 else if (!strncmp(var, "color_unfocus",
358 strlen("color_unfocus")))
359 for (i = 0; i < ScreenCount(display); i++)
360 screens[i].color_unfocus = name_to_color(val);
366 if (!strncmp(var, "dialog_ratio",
367 strlen("dialog_ratio"))) {
368 dialog_ratio = atof(val);
369 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
376 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
377 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
388 errx(1, "invalid conf file entry: %s=%s", var, val);
392 bar_print(struct swm_region *r, char *s, int erase)
395 XSetForeground(display, bar_gc, r->s->bar_color);
396 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent,
397 r->s->bar_text, strlen(r->s->bar_text));
400 strlcpy(r->s->bar_text, s, sizeof r->s->bar_text);
401 XSetForeground(display, bar_gc, r->s->bar_font_color);
402 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent,
403 r->s->bar_text, strlen(r->s->bar_text));
411 struct swm_region *r;
416 if (bar_enabled == 0)
420 localtime_r(&tmt, &tm);
421 strftime(s, sizeof s, "%a %b %d %R %Z %Y", &tm);
422 for (i = 0; i < ScreenCount(display); i++) {
424 TAILQ_FOREACH(r, &screens[i].rl, entry) {
425 snprintf(e, sizeof e, "%s %d:%d",
426 s, x++, r->ws->idx + 1);
430 XSync(display, False);
441 bar_toggle(struct swm_region *r, union arg *args)
443 struct swm_region *tmpr;
446 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
449 for (i = 0; i < ScreenCount(display); i++)
450 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
451 XUnmapWindow(display, tmpr->bar_window);
453 for (i = 0; i < ScreenCount(display); i++)
454 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
455 XMapRaised(display, tmpr->bar_window);
457 bar_enabled = !bar_enabled;
458 XSync(display, False);
459 for (i = 0; i < ScreenCount(display); i++)
460 for (j = 0; j < SWM_WS_MAX; j++)
461 screens[i].ws[j].restack = 1;
464 /* must be after stack */
465 for (i = 0; i < ScreenCount(display); i++)
466 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
471 bar_setup(struct swm_region *r)
475 for (i = 0; bar_fonts[i] != NULL; i++) {
476 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
480 if (bar_fonts[i] == NULL)
481 errx(1, "couldn't load font");
482 bar_height = bar_fs->ascent + bar_fs->descent + 3;
484 r->bar_window = XCreateSimpleWindow(display,
485 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
486 1, r->s->bar_border, r->s->bar_color);
487 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
488 XSetFont(display, bar_gc, bar_fs->fid);
489 XSelectInput(display, r->bar_window, VisibilityChangeMask);
491 XMapRaised(display, r->bar_window);
492 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
494 if (signal(SIGALRM, bar_signal) == SIG_ERR)
495 err(1, "could not install bar_signal");
500 config_win(struct ws_win *win)
504 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
505 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
506 ce.type = ConfigureNotify;
507 ce.display = display;
513 ce.height = win->g.h;
514 ce.border_width = 1; /* XXX store this! */
516 ce.override_redirect = False;
517 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
521 count_win(struct workspace *ws, int count_transient)
526 TAILQ_FOREACH(win, &ws->winlist, entry) {
527 if (count_transient == 0 && win->floating)
529 if (count_transient == 0 && win->transient)
533 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
539 quit(struct swm_region *r, union arg *args)
541 DNPRINTF(SWM_D_MISC, "quit\n");
546 restart(struct swm_region *r, union arg *args)
548 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
550 /* disable alarm because the following code may not be interrupted */
552 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
553 errx(1, "can't disable alarm");
555 XCloseDisplay(display);
556 execvp(start_argv[0], start_argv);
557 fprintf(stderr, "execvp failed\n");
563 root_to_region(Window root)
565 struct swm_region *r;
570 for (i = 0; i < ScreenCount(display); i++)
571 if (screens[i].root == root)
574 if (rootclick != root && /* if root was just clicked in, use cursor */
575 cur_focus && cur_focus->ws->r && cur_focus->s == &screens[i])
576 r = cur_focus->ws->r;
578 if (XQueryPointer(display, screens[i].root,
579 &rr, &cr, &x, &y, &wx, &wy, &mask) == False) {
580 r = TAILQ_FIRST(&screens[i].rl);
582 TAILQ_FOREACH(r, &screens[i].rl, entry) {
583 if (x > X(r) && x < X(r) + WIDTH(r) &&
584 y > Y(r) && y < Y(r) + HEIGHT(r))
589 r = TAILQ_FIRST(&screens[i].rl);
596 find_window(Window id)
601 for (i = 0; i < ScreenCount(display); i++)
602 for (j = 0; j < SWM_WS_MAX; j++)
603 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
610 spawn(struct swm_region *r, union arg *args)
612 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
614 * The double-fork construct avoids zombie processes and keeps the code
615 * clean from stupid signal handlers.
620 close(ConnectionNumber(display));
622 execvp(args->argv[0], args->argv);
623 fprintf(stderr, "execvp failed\n");
632 unfocus_win(struct ws_win *win)
634 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
635 if (win->ws->r && win->focus)
636 XSetWindowBorder(display, win->id,
637 win->ws->r->s->color_unfocus);
639 if (win->ws->focus == win)
640 win->ws->focus = NULL;
641 if (cur_focus == win)
647 focus_win(struct ws_win *win)
649 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
653 win->ws->focus = win;
654 if (win->ws->r != NULL) {
655 if (cur_focus && cur_focus != win)
656 unfocus_win(cur_focus);
659 XSetWindowBorder(display, win->id,
660 win->ws->r->s->color_focus);
662 XSetInputFocus(display, win->id,
663 RevertToPointerRoot, CurrentTime);
668 switchws(struct swm_region *r, union arg *args)
671 struct swm_region *this_r, *other_r;
673 struct workspace *new_ws, *old_ws;
677 new_ws = &this_r->s->ws[wsid];
679 DNPRINTF(SWM_D_WS, "switchws screen %d region %dx%d+%d+%d: "
680 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
683 if (new_ws == old_ws)
688 /* if the other workspace is hidden, switch windows */
689 /* map new window first to prevent ugly blinking */
690 TAILQ_FOREACH(win, &new_ws->winlist, entry)
691 XMapRaised(display, win->id);
693 TAILQ_FOREACH(win, &old_ws->winlist, entry)
694 XUnmapWindow(display, win->id);
699 other_r->ws = old_ws;
708 focus_win(new_ws->focus);
714 swapwin(struct swm_region *r, union arg *args)
716 struct ws_win *target;
717 struct ws_win_list *wl;
720 DNPRINTF(SWM_D_WS, "swapwin id %d "
721 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
722 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
723 if (cur_focus == NULL)
726 wl = &cur_focus->ws->winlist;
729 case SWM_ARG_ID_SWAPPREV:
730 target = TAILQ_PREV(cur_focus, ws_win_list, entry);
731 TAILQ_REMOVE(wl, cur_focus, entry);
733 TAILQ_INSERT_TAIL(wl, cur_focus, entry);
735 TAILQ_INSERT_BEFORE(target, cur_focus, entry);
737 case SWM_ARG_ID_SWAPNEXT:
738 target = TAILQ_NEXT(cur_focus, entry);
739 TAILQ_REMOVE(wl, cur_focus, entry);
741 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
743 TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
745 case SWM_ARG_ID_SWAPMAIN:
746 target = TAILQ_FIRST(wl);
747 if (target == cur_focus)
749 TAILQ_REMOVE(wl, target, entry);
750 TAILQ_INSERT_BEFORE(cur_focus, target, entry);
751 TAILQ_REMOVE(wl, cur_focus, entry);
752 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
755 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
764 focus(struct swm_region *r, union arg *args)
766 struct ws_win *winfocus, *winlostfocus;
767 struct ws_win_list *wl;
769 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
770 if (cur_focus == NULL)
773 wl = &cur_focus->ws->winlist;
775 winlostfocus = cur_focus;
778 case SWM_ARG_ID_FOCUSPREV:
779 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
780 if (winfocus == NULL)
781 winfocus = TAILQ_LAST(wl, ws_win_list);
784 case SWM_ARG_ID_FOCUSNEXT:
785 winfocus = TAILQ_NEXT(cur_focus, entry);
786 if (winfocus == NULL)
787 winfocus = TAILQ_FIRST(wl);
790 case SWM_ARG_ID_FOCUSMAIN:
791 winfocus = TAILQ_FIRST(wl);
798 if (winfocus == winlostfocus)
802 XSync(display, False);
806 cycle_layout(struct swm_region *r, union arg *args)
808 struct workspace *ws = r->ws;
810 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
813 if (ws->cur_layout->l_stack == NULL)
814 ws->cur_layout = &layouts[0];
821 resize_master(struct swm_region *r, union arg *args)
823 struct workspace *ws = r->ws;
825 DNPRINTF(SWM_D_STACK, "resize_master for workspace %d (id %d\n",
828 if (ws->cur_layout->l_resize != NULL)
829 ws->cur_layout->l_resize(ws, args->id);
833 stack_reset(struct swm_region *r, union arg *args)
835 struct workspace *ws = r->ws;
837 DNPRINTF(SWM_D_STACK, "stack_reset: ws %d\n", ws->idx);
839 if (ws->cur_layout->l_init != NULL) {
840 ws->cur_layout->l_init(ws);
847 struct swm_geometry g;
848 struct swm_region *r;
851 DNPRINTF(SWM_D_STACK, "stack\n");
853 for (i = 0; i < ScreenCount(display); i++) {
855 TAILQ_FOREACH(r, &screens[i].rl, entry) {
856 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
857 "(screen %d, region %d)\n", r->ws->idx, i, j++);
859 /* start with screen geometry, adjust for bar */
869 r->ws->cur_layout->l_stack(r->ws, &g);
872 XSync(display, False);
876 stack_floater(struct ws_win *win, struct swm_region *r)
881 bzero(&wc, sizeof wc);
882 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
884 if (win->transient) {
885 win->g.w = (double)WIDTH(r) * dialog_ratio;
886 win->g.h = (double)HEIGHT(r) * dialog_ratio;
889 wc.height = win->g.h;
890 wc.x = (WIDTH(r) - win->g.w) / 2;
891 wc.y = (HEIGHT(r) - win->g.h) / 2;
893 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
894 win->id, wc.x, wc.y, wc.width, wc.height);
896 XConfigureWindow(display, win->id, mask, &wc);
899 int vertical_msize[SWM_WS_MAX];
902 vertical_init(struct workspace *ws)
904 DNPRINTF(SWM_D_MISC, "vertical_init: workspace: %d\n", ws->idx);
906 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
910 vertical_resize(struct workspace *ws, int id)
912 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
915 case SWM_ARG_ID_MASTERSHRINK:
916 ws->l_state.vertical_msize--;
917 if (ws->l_state.vertical_msize < 1)
918 ws->l_state.vertical_msize = 1;
920 case SWM_ARG_ID_MASTERGROW:
921 ws->l_state.vertical_msize++;
922 if (ws->l_state.vertical_msize > SWM_V_SLICE - 1)
923 ws->l_state.vertical_msize = SWM_V_SLICE - 1;
932 vertical_stack(struct workspace *ws, struct swm_geometry *g) {
934 struct swm_geometry gg = *g;
935 struct ws_win *win, *winfocus;
936 int i, hrh, winno, main_width;
939 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
941 if ((winno = count_win(ws, 0)) == 0)
944 if (ws->focus == NULL)
945 ws->focus = TAILQ_FIRST(&ws->winlist);
946 winfocus = cur_focus ? cur_focus : ws->focus;
949 main_width = (g->w / SWM_V_SLICE) *
950 ws->l_state.vertical_msize;
955 hrh = g->h / (winno - 1);
960 TAILQ_FOREACH(win, &ws->winlist, entry) {
962 gg.x += main_width + 2;
963 gg.w = g->w - (main_width + 2);
965 if (i != 0 && hrh != 0) {
966 /* correct the last window for lost pixels */
967 if (win == TAILQ_LAST(&ws->winlist, ws_win_list)) {
968 gg.h = hrh + (g->h - (i * hrh));
972 /* leave first right hand window at y = 0 */
978 if (win->transient != 0 || win->floating != 0)
979 stack_floater(win, ws->r);
981 bzero(&wc, sizeof wc);
983 win->g.x = wc.x = gg.x;
984 win->g.y = wc.y = gg.y;
985 win->g.w = wc.width = gg.w;
986 win->g.h = wc.height = gg.h;
987 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
988 XConfigureWindow(display, win->id, mask, &wc);
991 XMapRaised(display, win->id);
996 focus_win(winfocus); /* has to be done outside of the loop */
1001 horizontal_init(struct workspace *ws)
1003 DNPRINTF(SWM_D_STACK, "horizontal_init: workspace: %d\n", ws->idx);
1005 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1009 horizontal_resize(struct workspace *ws, int id)
1011 DNPRINTF(SWM_D_STACK, "horizontal_resize: workspace: %d\n", ws->idx);
1014 case SWM_ARG_ID_MASTERSHRINK:
1015 ws->l_state.horizontal_msize--;
1016 if (ws->l_state.horizontal_msize < 1)
1017 ws->l_state.horizontal_msize = 1;
1019 case SWM_ARG_ID_MASTERGROW:
1020 ws->l_state.horizontal_msize++;
1021 if (ws->l_state.horizontal_msize > SWM_H_SLICE - 1)
1022 ws->l_state.horizontal_msize = SWM_H_SLICE - 1;
1031 horizontal_stack(struct workspace *ws, struct swm_geometry *g) {
1033 struct swm_geometry gg = *g;
1034 struct ws_win *win, *winfocus;
1035 int i, hrw, winno, main_height;
1038 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
1040 if ((winno = count_win(ws, 0)) == 0)
1043 if (ws->focus == NULL)
1044 ws->focus = TAILQ_FIRST(&ws->winlist);
1045 winfocus = cur_focus ? cur_focus : ws->focus;
1048 main_height = (g->h / SWM_H_SLICE) *
1049 ws->l_state.horizontal_msize;
1054 hrw = g->w / (winno - 1);
1059 TAILQ_FOREACH(win, &ws->winlist, entry) {
1061 gg.y += main_height + 2;
1062 gg.h = g->h - (main_height + 2);
1064 if (i != 0 && hrw != 0) {
1065 /* correct the last window for lost pixels */
1066 if (win == TAILQ_LAST(&ws->winlist, ws_win_list)) {
1067 gg.w = hrw + (g->w - (i * hrw));
1071 /* leave first bottom window at x = 0 */
1077 if (win->transient != 0 || win->floating != 0)
1078 stack_floater(win, ws->r);
1080 bzero(&wc, sizeof wc);
1081 wc.border_width = 1;
1082 win->g.x = wc.x = gg.x;
1083 win->g.y = wc.y = gg.y;
1084 win->g.w = wc.width = gg.w;
1085 win->g.h = wc.height = gg.h;
1086 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1087 XConfigureWindow(display, win->id, mask, &wc);
1090 XMapRaised(display, win->id);
1095 focus_win(winfocus); /* this has to be done outside of the loop */
1098 /* fullscreen view */
1100 max_stack(struct workspace *ws, struct swm_geometry *g) {
1102 struct swm_geometry gg = *g;
1103 struct ws_win *win, *winfocus;
1106 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1108 if (count_win(ws, 0) == 0)
1111 if (ws->focus == NULL)
1112 ws->focus = TAILQ_FIRST(&ws->winlist);
1113 winfocus = cur_focus ? cur_focus : ws->focus;
1115 TAILQ_FOREACH(win, &ws->winlist, entry) {
1116 if (win->transient != 0 || win->floating != 0) {
1117 if (win == ws->focus) {
1119 stack_floater(win, ws->r);
1120 XMapRaised(display, win->id);
1122 XUnmapWindow(display, win->id);
1124 bzero(&wc, sizeof wc);
1125 wc.border_width = 1;
1126 win->g.x = wc.x = gg.x;
1127 win->g.y = wc.y = gg.y;
1128 win->g.w = wc.width = gg.w;
1129 win->g.h = wc.height = gg.h;
1130 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1131 XConfigureWindow(display, win->id, mask, &wc);
1133 if (win == ws->focus) {
1134 XMapRaised(display, win->id);
1136 XUnmapWindow(display, win->id);
1141 focus_win(winfocus); /* has to be done outside of the loop */
1145 send_to_ws(struct swm_region *r, union arg *args)
1147 int wsid = args->id;
1148 struct ws_win *win = cur_focus;
1149 struct workspace *ws, *nws;
1151 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1154 nws = &win->s->ws[wsid];
1156 XUnmapWindow(display, win->id);
1158 /* find a window to focus */
1159 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1160 if (ws->focus == NULL)
1161 ws->focus = TAILQ_FIRST(&ws->winlist);
1162 if (ws->focus == win)
1165 TAILQ_REMOVE(&ws->winlist, win, entry);
1167 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1170 if (count_win(nws, 1) == 1)
1178 /* key definitions */
1182 void (*func)(struct swm_region *r, union arg *);
1185 /* modifier key function argument */
1186 { MODKEY, XK_space, cycle_layout, {0} },
1187 { MODKEY | ShiftMask, XK_space, stack_reset, {0} },
1188 { MODKEY, XK_h, resize_master, {.id = SWM_ARG_ID_MASTERSHRINK} },
1189 { MODKEY, XK_l, resize_master, {.id = SWM_ARG_ID_MASTERGROW} },
1190 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1191 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1192 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1193 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1194 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1195 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1196 { MODKEY, XK_p, spawn, {.argv = spawn_menu} },
1197 { MODKEY | ShiftMask, XK_q, quit, {0} },
1198 { MODKEY, XK_q, restart, {0} },
1199 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1200 { MODKEY, XK_1, switchws, {.id = 0} },
1201 { MODKEY, XK_2, switchws, {.id = 1} },
1202 { MODKEY, XK_3, switchws, {.id = 2} },
1203 { MODKEY, XK_4, switchws, {.id = 3} },
1204 { MODKEY, XK_5, switchws, {.id = 4} },
1205 { MODKEY, XK_6, switchws, {.id = 5} },
1206 { MODKEY, XK_7, switchws, {.id = 6} },
1207 { MODKEY, XK_8, switchws, {.id = 7} },
1208 { MODKEY, XK_9, switchws, {.id = 8} },
1209 { MODKEY, XK_0, switchws, {.id = 9} },
1210 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1211 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1212 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1213 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1214 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1215 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1216 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1217 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1218 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1219 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1220 { MODKEY, XK_b, bar_toggle, {0} },
1221 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1222 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1226 updatenumlockmask(void)
1229 XModifierKeymap *modmap;
1231 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1233 modmap = XGetModifierMapping(display);
1234 for (i = 0; i < 8; i++)
1235 for (j = 0; j < modmap->max_keypermod; j++)
1236 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1237 == XKeysymToKeycode(display, XK_Num_Lock))
1238 numlockmask = (1 << i);
1240 XFreeModifiermap(modmap);
1246 unsigned int i, j, k;
1248 unsigned int modifiers[] =
1249 { 0, LockMask, numlockmask, numlockmask | LockMask };
1251 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1252 updatenumlockmask();
1254 for (k = 0; k < ScreenCount(display); k++) {
1255 if (TAILQ_EMPTY(&screens[k].rl))
1257 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
1258 for (i = 0; i < LENGTH(keys); i++) {
1259 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
1260 for (j = 0; j < LENGTH(modifiers); j++)
1261 XGrabKey(display, code,
1262 keys[i].mod | modifiers[j],
1263 screens[k].root, True,
1264 GrabModeAsync, GrabModeAsync);
1271 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1279 XKeyEvent *ev = &e->xkey;
1281 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1283 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1284 for (i = 0; i < LENGTH(keys); i++)
1285 if (keysym == keys[i].keysym
1286 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1288 keys[i].func(root_to_region(ev->root),
1293 buttonpress(XEvent *e)
1295 XButtonPressedEvent *ev = &e->xbutton;
1296 #ifdef SWM_CLICKTOFOCUS
1298 struct workspace *ws;
1299 struct swm_region *r;
1302 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1304 if (ev->window == ev->root) {
1305 rootclick = ev->root;
1308 if (ev->window == cur_focus->id)
1310 #ifdef SWM_CLICKTOFOCUS
1311 r = root_to_region(ev->root);
1313 TAILQ_FOREACH(win, &ws->winlist, entry)
1314 if (win->id == ev->window) {
1315 /* focus in the clicked window */
1316 XSetWindowBorder(display, ev->window, 0xff0000);
1317 XSetWindowBorder(display, ws->focus->id, 0x888888);
1318 XSetInputFocus(display, ev->window, RevertToPointerRoot,
1321 XSync(display, False);
1328 set_win_state(struct ws_win *win, long state)
1330 long data[] = {state, None};
1332 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1334 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1335 (unsigned char *)data, 2);
1339 manage_window(Window id, struct workspace *ws)
1345 TAILQ_FOREACH(win, &ws->winlist, entry) {
1347 return (win); /* already being managed */
1350 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1351 errx(1, "calloc: failed to allocate memory for new window");
1355 win->s = ws->r->s; /* this never changes */
1356 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
1358 /* make new win focused */
1361 XGetTransientForHint(display, win->id, &trans);
1363 win->transient = trans;
1364 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1365 (unsigned)win->id, win->transient);
1367 XGetWindowAttributes(display, id, &win->wa);
1368 win->g.w = win->wa.width;
1369 win->g.h = win->wa.height;
1370 win->g.x = win->wa.x;
1371 win->g.y = win->wa.y;
1373 /* XXX make this a table */
1374 bzero(&ch, sizeof ch);
1375 if (XGetClassHint(display, win->id, &ch)) {
1376 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1377 if (!strcmp(ch.res_class, "MPlayer") && !strcmp(ch.res_name, "xv")) {
1381 XFree(ch.res_class);
1386 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
1387 PropertyChangeMask | StructureNotifyMask);
1389 set_win_state(win, NormalState);
1395 configurerequest(XEvent *e)
1397 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1402 if ((win = find_window(ev->window)) == NULL)
1406 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
1408 bzero(&wc, sizeof wc);
1411 wc.width = ev->width;
1412 wc.height = ev->height;
1413 wc.border_width = ev->border_width;
1414 wc.sibling = ev->above;
1415 wc.stack_mode = ev->detail;
1416 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
1418 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
1420 if (win->floating) {
1421 if (ev->value_mask & CWX)
1423 if (ev->value_mask & CWY)
1425 if (ev->value_mask & CWWidth)
1426 win->g.w = ev->width;
1427 if (ev->value_mask & CWHeight)
1428 win->g.h = ev->height;
1429 if (win->ws->r != NULL) {
1430 /* this seems to be full screen */
1431 if (win->g.w > WIDTH(win->ws->r)) {
1436 if (win->g.h > HEIGHT(win->ws->r)) {
1442 if ((ev->value_mask & (CWX|CWY)) &&
1443 !(ev->value_mask & (CWWidth|CWHeight)))
1445 XMoveResizeWindow(display, win->id,
1446 win->g.x, win->g.y, win->g.w, win->g.h);
1453 configurenotify(XEvent *e)
1455 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1456 e->xconfigure.window);
1460 destroynotify(XEvent *e)
1463 XDestroyWindowEvent *ev = &e->xdestroywindow;
1465 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1467 if ((win = find_window(ev->window)) != NULL) {
1468 struct workspace *ws = win->ws;
1469 /* find a window to focus */
1470 if (ws->focus == win)
1471 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1472 if (ws->focus == NULL)
1473 ws->focus = TAILQ_FIRST(&ws->winlist);
1474 if (ws->focus == win)
1476 if (cur_focus == win) {
1477 if (ws->focus == NULL)
1478 unfocus_win(win); /* XXX focus another ws? */
1480 focus_win(ws->focus);
1483 TAILQ_REMOVE(&ws->winlist, win, entry);
1484 set_win_state(win, WithdrawnState);
1491 enternotify(XEvent *e)
1493 XCrossingEvent *ev = &e->xcrossing;
1497 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1499 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
1500 ev->window != ev->root)
1503 /* eat event(r) to prevent autofocus */
1507 /* brute force for now */
1508 for (i = 0; i < ScreenCount(display); i++) {
1509 for (j = 0; j < SWM_WS_MAX; j++) {
1510 TAILQ_FOREACH(win, &screens[i].ws[j].winlist , entry) {
1511 if (win->id == ev->window)
1521 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
1523 /* XXX this probably needs better handling now.
1524 if (ev->window == ev->root)
1528 * kill grab for now so that we can cut and paste , this screws up
1532 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1533 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1534 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1539 mappingnotify(XEvent *e)
1541 XMappingEvent *ev = &e->xmapping;
1543 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1545 XRefreshKeyboardMapping(ev);
1546 if (ev->request == MappingKeyboard)
1551 maprequest(XEvent *e)
1553 XMapRequestEvent *ev = &e->xmaprequest;
1554 XWindowAttributes wa;
1555 struct swm_region *r;
1557 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1558 e->xmaprequest.window);
1560 if (!XGetWindowAttributes(display, ev->window, &wa))
1562 if (wa.override_redirect)
1564 r = root_to_region(wa.root);
1565 manage_window(e->xmaprequest.window, r->ws);
1570 propertynotify(XEvent *e)
1572 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1573 e->xproperty.window);
1577 unmapnotify(XEvent *e)
1579 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1583 visibilitynotify(XEvent *e)
1586 struct swm_region *r;
1588 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
1589 e->xvisibility.window);
1590 if (e->xvisibility.state == VisibilityUnobscured)
1591 for (i = 0; i < ScreenCount(display); i++)
1592 TAILQ_FOREACH(r, &screens[i].rl, entry)
1593 if (e->xvisibility.window == r->bar_window)
1597 void (*handler[LASTEvent])(XEvent *) = {
1599 [KeyPress] = keypress,
1600 [ButtonPress] = buttonpress,
1601 [ConfigureRequest] = configurerequest,
1602 [ConfigureNotify] = configurenotify,
1603 [DestroyNotify] = destroynotify,
1604 [EnterNotify] = enternotify,
1605 [FocusIn] = focusin,
1606 [MappingNotify] = mappingnotify,
1607 [MapRequest] = maprequest,
1608 [PropertyNotify] = propertynotify,
1609 [UnmapNotify] = unmapnotify,
1610 [VisibilityNotify] = visibilitynotify,
1614 xerror_start(Display *d, XErrorEvent *ee)
1621 xerror(Display *d, XErrorEvent *ee)
1623 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1631 xerrorxlib = XSetErrorHandler(xerror_start);
1633 /* this causes an error if some other window manager is running */
1634 XSelectInput(display, DefaultRootWindow(display),
1635 SubstructureRedirectMask);
1636 XSync(display, False);
1640 XSetErrorHandler(xerror);
1641 XSync(display, False);
1650 unsigned char *p = NULL;
1651 unsigned long n, extra;
1654 astate = XInternAtom(display, "WM_STATE", False);
1655 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
1656 &real, &format, &n, &extra, (unsigned char **)&p);
1657 if (status != Success)
1666 new_region(struct swm_screen *s, struct workspace *ws,
1667 int x, int y, int w, int h)
1669 struct swm_region *r;
1671 DNPRINTF(SWM_D_MISC, "new region on screen %d: %dx%d (%d, %d)\n",
1672 s->idx, x, y, w, h);
1674 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
1675 errx(1, "calloc: failed to allocate memory for screen");
1684 TAILQ_INSERT_TAIL(&s->rl, r, entry);
1691 #ifdef SWM_XRR_HAS_CRTC
1693 XRRScreenResources *sr;
1695 #endif /* SWM_XRR_HAS_CRTC */
1696 Window d1, d2, *wins = NULL;
1697 XWindowAttributes wa;
1698 struct swm_region *r;
1700 int errorbase, major, minor;
1701 int ncrtc = 0, w = 0;
1703 struct workspace *ws;
1705 if ((screens = calloc(ScreenCount(display),
1706 sizeof(struct swm_screen))) == NULL)
1707 errx(1, "calloc: screens");
1709 /* map physical screens */
1710 for (i = 0; i < ScreenCount(display); i++) {
1711 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
1713 TAILQ_INIT(&screens[i].rl);
1714 screens[i].root = RootWindow(display, i);
1715 XGetWindowAttributes(display, screens[i].root, &wa);
1716 XSelectInput(display, screens[i].root,
1717 ButtonPressMask | wa.your_event_mask);
1719 /* set default colors */
1720 screens[i].color_focus = name_to_color("red");
1721 screens[i].color_unfocus = name_to_color("rgb:88/88/88");
1722 screens[i].bar_border = name_to_color("rgb:00/80/80");
1723 screens[i].bar_color = name_to_color("black");
1724 screens[i].bar_font_color = name_to_color("rgb:a0/a0/a0");
1726 /* init all workspaces */
1727 for (j = 0; j < SWM_WS_MAX; j++) {
1728 ws = &screens[i].ws[j];
1733 TAILQ_INIT(&ws->winlist);
1735 for (k = 0; layouts[k].l_stack != NULL; k++)
1736 if (layouts[k].l_init != NULL)
1737 layouts[k].l_init(ws);
1738 ws->cur_layout = &layouts[0];
1741 /* map virtual screens onto physical screens */
1742 screens[i].xrandr_support = XRRQueryExtension(display,
1743 &xrandr_eventbase, &errorbase);
1744 if (screens[i].xrandr_support)
1745 if (XRRQueryVersion(display, &major, &minor) &&
1747 screens[i].xrandr_support = 0;
1749 #if 0 /* not ready for dynamic screen changes */
1750 if (screens[i].xrandr_support)
1751 XRRSelectInput(display,
1753 RRScreenChangeNotifyMask);
1756 /* grab existing windows (before we build the bars)*/
1757 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
1760 #ifdef SWM_XRR_HAS_CRTC
1761 sr = XRRGetScreenResources(display, screens[i].root);
1763 new_region(&screens[i], &screens[i].ws[w],
1764 0, 0, DisplayWidth(display, i),
1765 DisplayHeight(display, i));
1769 for (c = 0; c < ncrtc; c++) {
1770 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
1771 if (ci->noutput == 0)
1774 if (ci != NULL && ci->mode == None)
1775 new_region(&screens[i], &screens[i].ws[w], 0, 0,
1776 DisplayWidth(display, i),
1777 DisplayHeight(display, i));
1779 new_region(&screens[i], &screens[i].ws[w],
1780 ci->x, ci->y, ci->width, ci->height);
1783 XRRFreeCrtcInfo(ci);
1784 XRRFreeScreenResources(sr);
1786 new_region(&screens[i], &screens[i].ws[w], 0, 0,
1787 DisplayWidth(display, i),
1788 DisplayHeight(display, i));
1789 #endif /* SWM_XRR_HAS_CRTC */
1791 /* attach windows to a region */
1792 /* normal windows */
1793 if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
1794 errx(1, "no regions on screen %d", i);
1796 for (i = 0; i < no; i++) {
1797 XGetWindowAttributes(display, wins[i], &wa);
1798 if (!XGetWindowAttributes(display, wins[i], &wa) ||
1799 wa.override_redirect ||
1800 XGetTransientForHint(display, wins[i], &d1))
1803 if (wa.map_state == IsViewable ||
1804 getstate(wins[i]) == NormalState)
1805 manage_window(wins[i], r->ws);
1807 /* transient windows */
1808 for (i = 0; i < no; i++) {
1809 if (!XGetWindowAttributes(display, wins[i], &wa))
1812 if (XGetTransientForHint(display, wins[i], &d1) &&
1813 (wa.map_state == IsViewable || getstate(wins[i]) ==
1815 manage_window(wins[i], r->ws);
1825 main(int argc, char *argv[])
1828 char conf[PATH_MAX], *cfile = NULL;
1835 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
1836 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
1837 warnx("no locale support");
1839 if (!(display = XOpenDisplay(0)))
1840 errx(1, "can not open display");
1843 errx(1, "other wm running");
1845 astate = XInternAtom(display, "WM_STATE", False);
1847 /* look for local and global conf file */
1848 pwd = getpwuid(getuid());
1850 errx(1, "invalid user %d", getuid());
1852 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
1853 if (stat(conf, &sb) != -1) {
1854 if (S_ISREG(sb.st_mode))
1857 /* try global conf file */
1858 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
1859 if (!stat(conf, &sb))
1860 if (S_ISREG(sb.st_mode))
1870 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
1875 xfd = ConnectionNumber(display);
1878 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
1880 errx(1, "select failed");
1885 while(XPending(display)) {
1886 XNextEvent(display, &e);
1887 if (handler[e.type])
1888 handler[e.type](&e);
1892 XCloseDisplay(display);