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"
64 #include <sys/types.h>
67 #include <sys/queue.h>
68 #include <sys/param.h>
70 #include <X11/cursorfont.h>
71 #include <X11/keysym.h>
72 #include <X11/Xatom.h>
74 #include <X11/Xproto.h>
75 #include <X11/Xutil.h>
76 #include <X11/extensions/Xrandr.h>
79 # error XRandR versions less than 1.0 are not supported
84 #define SWM_XRR_HAS_CRTC
89 /* #define SWM_DEBUG */
91 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
92 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
93 #define SWM_D_MISC 0x0001
94 #define SWM_D_EVENT 0x0002
95 #define SWM_D_WS 0x0004
96 #define SWM_D_FOCUS 0x0008
97 #define SWM_D_MOVE 0x0010
98 #define SWM_D_STACK 0x0020
100 u_int32_t swm_debug = 0
109 #define DPRINTF(x...)
110 #define DNPRINTF(n,x...)
113 #define LENGTH(x) (sizeof x / sizeof x[0])
114 #define MODKEY Mod1Mask
115 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
117 #define X(r) (r)->g.x
118 #define Y(r) (r)->g.y
119 #define WIDTH(r) (r)->g.w
120 #define HEIGHT(r) (r)->g.h
124 int (*xerrorxlib)(Display *, XErrorEvent *);
127 int xrandr_eventbase;
128 int ignore_enter = 0;
129 unsigned int numlockmask = 0;
133 double dialog_ratio = .6;
141 char *bar_fonts[] = {
142 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
143 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
147 /* terminal + args */
148 char *spawn_term[] = { "xterm", NULL };
149 char *spawn_menu[] = { "dmenu_run", NULL };
151 /* layout manager data */
152 struct swm_geometry {
162 /* virtual "screens" */
164 TAILQ_ENTRY(swm_region) entry;
165 struct swm_geometry g;
167 struct workspace *ws; /* current workspace on this region */
168 struct swm_screen *s; /* screen idx */
170 TAILQ_HEAD(swm_region_list, swm_region);
174 TAILQ_ENTRY(ws_win) entry;
176 struct swm_geometry g;
180 struct workspace *ws; /* always valid */
181 struct swm_screen *s; /* always valid, never changes */
182 XWindowAttributes wa;
184 TAILQ_HEAD(ws_win_list, ws_win);
186 /* layout handlers */
188 void vertical_init(struct workspace *);
189 void vertical_resize(struct workspace *, int);
190 void vertical_stack(struct workspace *, struct swm_geometry *);
191 void horizontal_init(struct workspace *);
192 void horizontal_resize(struct workspace *, int);
193 void horizontal_stack(struct workspace *, struct swm_geometry *);
194 void max_init(struct workspace *);
195 void max_stack(struct workspace *, struct swm_geometry *);
198 void (*l_init)(struct workspace *); /* init/reset */
199 void (*l_stack)(struct workspace *, struct swm_geometry *);
200 void (*l_resize)(struct workspace *, int);
202 /* init stack, resize */
203 { vertical_init, vertical_stack, vertical_resize},
204 { horizontal_init, horizontal_stack, horizontal_resize},
205 { NULL, max_stack, NULL},
209 #define SWM_H_SLICE (32)
210 #define SWM_V_SLICE (32)
213 /* define work spaces */
215 int idx; /* workspace index */
216 int restack; /* restack on switch */
217 struct layout *cur_layout; /* current layout handlers */
218 struct ws_win *focus; /* may be NULL */
219 struct swm_region *r; /* may be NULL */
220 struct ws_win_list winlist; /* list of windows in ws */
224 int horizontal_msize;
229 /* physical screen mapping */
230 #define SWM_WS_MAX (10) /* XXX Too small? */
232 int idx; /* screen index */
233 struct swm_region_list rl; /* list of regions on this screen */
236 struct workspace ws[SWM_WS_MAX];
239 unsigned long bar_border;
240 unsigned long bar_color;
241 unsigned long bar_font_color;
242 unsigned long color_focus; /* XXX should this be per ws? */
243 unsigned long color_unfocus;
245 struct swm_screen *screens;
247 Window rootclick = 0;
249 struct ws_win *cur_focus = NULL;
251 /* args to functions */
254 #define SWM_ARG_ID_FOCUSNEXT (0)
255 #define SWM_ARG_ID_FOCUSPREV (1)
256 #define SWM_ARG_ID_FOCUSMAIN (2)
257 #define SWM_ARG_ID_SWAPNEXT (3)
258 #define SWM_ARG_ID_SWAPPREV (4)
259 #define SWM_ARG_ID_SWAPMAIN (5)
260 #define SWM_ARG_ID_MASTERSHRINK (6)
261 #define SWM_ARG_ID_MASTERGROW (7)
266 name_to_color(char *colorname)
270 XColor screen_def, exact_def;
271 unsigned long result = 0;
272 char cname[32] = "#";
274 cmap = DefaultColormap(display, screens[0].idx);
275 status = XAllocNamedColor(display, cmap, colorname,
276 &screen_def, &exact_def);
278 strlcat(cname, colorname + 2, sizeof cname - 1);
279 status = XAllocNamedColor(display, cmap, cname, &screen_def,
283 result = screen_def.pixel;
285 fprintf(stderr, "color '%s' not found.\n", colorname);
290 /* conf file stuff */
291 #define SWM_CONF_WS "\n= \t"
292 #define SWM_CONF_FILE "scrotwm.conf"
294 conf_load(char *filename)
297 char *line, *cp, *var, *val;
298 size_t len, lineno = 0;
301 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
303 if (filename == NULL)
306 if ((config = fopen(filename, "r")) == NULL)
310 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
314 cp += (long)strspn(cp, SWM_CONF_WS);
320 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
322 cp += (long)strspn(cp, SWM_CONF_WS);
323 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
326 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
329 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
330 bar_enabled = atoi(val);
331 else if (!strncmp(var, "bar_border",
332 strlen("bar_border")))
333 for (i = 0; i < ScreenCount(display); i++)
334 screens[i].bar_border = name_to_color(val);
335 else if (!strncmp(var, "bar_color",
336 strlen("bar_color")))
337 for (i = 0; i < ScreenCount(display); i++)
338 screens[i].bar_color = name_to_color(val);
339 else if (!strncmp(var, "bar_font_color",
340 strlen("bar_font_color")))
341 for (i = 0; i < ScreenCount(display); i++)
342 screens[i].bar_font_color = name_to_color(val);
343 else if (!strncmp(var, "bar_font", strlen("bar_font")))
344 asprintf(&bar_fonts[0], "%s", val);
350 if (!strncmp(var, "color_focus", strlen("color_focus")))
351 for (i = 0; i < ScreenCount(display); i++)
352 screens[i].color_focus = name_to_color(val);
353 else if (!strncmp(var, "color_unfocus",
354 strlen("color_unfocus")))
355 for (i = 0; i < ScreenCount(display); i++)
356 screens[i].color_unfocus = name_to_color(val);
362 if (!strncmp(var, "dialog_ratio",
363 strlen("dialog_ratio"))) {
364 dialog_ratio = atof(val);
365 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
372 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
373 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
384 errx(1, "invalid conf file entry: %s=%s", var, val);
388 bar_print(struct swm_region *r)
393 if (bar_enabled == 0)
397 XSetForeground(display, bar_gc, r->s->bar_color);
398 XDrawString(display, r->bar_window,
399 bar_gc, 4, bar_fs->ascent, bar_text, strlen(bar_text));
403 localtime_r(&tmt, &tm);
404 strftime(bar_text, sizeof bar_text, "%a %b %d %R %Z %Y", &tm);
405 XSetForeground(display, bar_gc, r->s->bar_font_color);
406 XDrawString(display, r->bar_window, bar_gc, 4,
407 bar_fs->ascent, bar_text, strlen(bar_text));
408 XSync(display, False);
416 /* XXX yeah yeah byte me */
418 bar_print(cur_focus->ws->r);
422 bar_toggle(struct swm_region *r, union arg *args)
424 struct swm_region *tmpr;
427 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
430 for (i = 0; i < ScreenCount(display); i++)
431 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
432 XUnmapWindow(display, tmpr->bar_window);
434 for (i = 0; i < ScreenCount(display); i++)
435 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
436 XMapRaised(display, tmpr->bar_window);
438 bar_enabled = !bar_enabled;
439 XSync(display, False);
440 for (i = 0; i < ScreenCount(display); i++)
441 for (j = 0; j < SWM_WS_MAX; j++)
442 screens[i].ws[j].restack = 1;
445 /* must be after stack */
446 for (i = 0; i < ScreenCount(display); i++)
447 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
452 bar_setup(struct swm_region *r)
456 for (i = 0; bar_fonts[i] != NULL; i++) {
457 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
461 if (bar_fonts[i] == NULL)
462 errx(1, "couldn't load font");
463 bar_height = bar_fs->ascent + bar_fs->descent + 3;
465 r->bar_window = XCreateSimpleWindow(display,
466 r->s->root, X(r), Y(r), WIDTH(r), bar_height - 2,
467 1, r->s->bar_border, r->s->bar_color);
468 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
469 XSetFont(display, bar_gc, bar_fs->fid);
470 XSelectInput(display, r->bar_window, VisibilityChangeMask);
472 XMapRaised(display, r->bar_window);
473 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
475 if (signal(SIGALRM, bar_signal) == SIG_ERR)
476 err(1, "could not install bar_signal");
481 config_win(struct ws_win *win)
485 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
486 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
487 ce.type = ConfigureNotify;
488 ce.display = display;
494 ce.height = win->g.h;
495 ce.border_width = 1; /* XXX store this! */
497 ce.override_redirect = False;
498 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
502 count_win(struct workspace *ws, int count_transient)
507 TAILQ_FOREACH(win, &ws->winlist, entry) {
508 if (count_transient == 0 && win->floating)
510 if (count_transient == 0 && win->transient)
514 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
520 quit(struct swm_region *r, union arg *args)
522 DNPRINTF(SWM_D_MISC, "quit\n");
527 restart(struct swm_region *r, union arg *args)
529 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
531 XCloseDisplay(display);
532 execvp(start_argv[0], start_argv);
533 fprintf(stderr, "execvp failed\n");
539 root_to_region(Window root)
541 struct swm_region *r;
546 for (i = 0; i < ScreenCount(display); i++)
547 if (screens[i].root == root)
550 if (rootclick != root && /* if root was just clicked in, use cursor */
551 cur_focus && cur_focus->ws->r && cur_focus->s == &screens[i])
552 r = cur_focus->ws->r;
554 if (XQueryPointer(display, screens[i].root,
555 &rr, &cr, &x, &y, &wx, &wy, &mask) == False) {
556 r = TAILQ_FIRST(&screens[i].rl);
558 TAILQ_FOREACH(r, &screens[i].rl, entry) {
559 if (x > X(r) && x < X(r) + WIDTH(r) &&
560 y > Y(r) && y < Y(r) + HEIGHT(r))
565 r = TAILQ_FIRST(&screens[i].rl);
572 find_window(Window id)
577 for (i = 0; i < ScreenCount(display); i++)
578 for (j = 0; j < SWM_WS_MAX; j++)
579 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
586 spawn(struct swm_region *r, union arg *args)
588 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
590 * The double-fork construct avoids zombie processes and keeps the code
591 * clean from stupid signal handlers.
596 close(ConnectionNumber(display));
598 execvp(args->argv[0], args->argv);
599 fprintf(stderr, "execvp failed\n");
608 unfocus_win(struct ws_win *win)
610 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
611 if (win->ws->r && win->focus)
612 XSetWindowBorder(display, win->id,
613 win->ws->r->s->color_unfocus);
615 if (win->ws->focus == win)
616 win->ws->focus = NULL;
617 if (cur_focus == win)
623 focus_win(struct ws_win *win)
625 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
629 win->ws->focus = win;
630 if (win->ws->r != NULL) {
631 if (cur_focus && cur_focus != win)
632 unfocus_win(cur_focus);
635 XSetWindowBorder(display, win->id,
636 win->ws->r->s->color_focus);
638 XSetInputFocus(display, win->id,
639 RevertToPointerRoot, CurrentTime);
644 switchws(struct swm_region *r, union arg *args)
647 struct swm_region *this_r, *other_r;
649 struct workspace *new_ws, *old_ws;
653 new_ws = &this_r->s->ws[wsid];
655 DNPRINTF(SWM_D_WS, "switchws screen %d region %dx%d+%d+%d: "
656 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
659 if (new_ws == old_ws)
663 fprintf(stderr, "switchws: other_r %p\n", other_r);
665 /* if the other workspace is hidden, switch windows */
667 /* map new window first to prevent ugly blinking */
668 TAILQ_FOREACH(win, &new_ws->winlist, entry)
669 XMapRaised(display, win->id);
671 TAILQ_FOREACH(win, &old_ws->winlist, entry)
672 XUnmapWindow(display, win->id);
677 other_r->ws = old_ws;
686 focus_win(new_ws->focus);
691 swapwin(struct swm_region *r, union arg *args)
693 struct ws_win *target;
694 struct ws_win_list *wl;
697 DNPRINTF(SWM_D_WS, "swapwin id %d "
698 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
699 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
700 if (cur_focus == NULL)
703 wl = &cur_focus->ws->winlist;
706 case SWM_ARG_ID_SWAPPREV:
707 target = TAILQ_PREV(cur_focus, ws_win_list, entry);
708 TAILQ_REMOVE(wl, cur_focus, entry);
710 TAILQ_INSERT_TAIL(wl, cur_focus, entry);
712 TAILQ_INSERT_BEFORE(target, cur_focus, entry);
714 case SWM_ARG_ID_SWAPNEXT:
715 target = TAILQ_NEXT(cur_focus, entry);
716 TAILQ_REMOVE(wl, cur_focus, entry);
718 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
720 TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
722 case SWM_ARG_ID_SWAPMAIN:
723 target = TAILQ_FIRST(wl);
724 if (target == cur_focus)
726 TAILQ_REMOVE(wl, target, entry);
727 TAILQ_INSERT_BEFORE(cur_focus, target, entry);
728 TAILQ_REMOVE(wl, cur_focus, entry);
729 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
732 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
741 focus(struct swm_region *r, union arg *args)
743 struct ws_win *winfocus, *winlostfocus;
744 struct ws_win_list *wl;
746 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
747 if (cur_focus == NULL)
750 wl = &cur_focus->ws->winlist;
752 winlostfocus = cur_focus;
755 case SWM_ARG_ID_FOCUSPREV:
756 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
757 if (winfocus == NULL)
758 winfocus = TAILQ_LAST(wl, ws_win_list);
761 case SWM_ARG_ID_FOCUSNEXT:
762 winfocus = TAILQ_NEXT(cur_focus, entry);
763 if (winfocus == NULL)
764 winfocus = TAILQ_FIRST(wl);
767 case SWM_ARG_ID_FOCUSMAIN:
768 winfocus = TAILQ_FIRST(wl);
775 if (winfocus == winlostfocus)
779 XSync(display, False);
783 cycle_layout(struct swm_region *r, union arg *args)
785 struct workspace *ws = r->ws;
787 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
790 if (ws->cur_layout->l_stack == NULL)
791 ws->cur_layout = &layouts[0];
798 resize_master(struct swm_region *r, union arg *args)
800 struct workspace *ws = r->ws;
802 DNPRINTF(SWM_D_STACK, "resize_master for workspace %d (id %d\n",
805 if (ws->cur_layout->l_resize != NULL)
806 ws->cur_layout->l_resize(ws, args->id);
810 stack_reset(struct swm_region *r, union arg *args)
812 struct workspace *ws = r->ws;
814 DNPRINTF(SWM_D_STACK, "stack_reset: ws %d\n", ws->idx);
816 if (ws->cur_layout->l_init != NULL) {
817 ws->cur_layout->l_init(ws);
824 struct swm_geometry g;
825 struct swm_region *r;
828 DNPRINTF(SWM_D_STACK, "stack\n");
830 for (i = 0; i < ScreenCount(display); i++) {
832 TAILQ_FOREACH(r, &screens[i].rl, entry) {
833 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
834 "(screen %d, region %d)\n", r->ws->idx, i, j++);
836 /* start with screen geometry, adjust for bar */
846 r->ws->cur_layout->l_stack(r->ws, &g);
849 XSync(display, False);
853 stack_floater(struct ws_win *win, struct swm_region *r)
858 bzero(&wc, sizeof wc);
859 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
861 if (win->transient) {
862 win->g.w = (double)WIDTH(r) * dialog_ratio;
863 win->g.h = (double)HEIGHT(r) * dialog_ratio;
866 wc.height = win->g.h;
867 wc.x = (WIDTH(r) - win->g.w) / 2;
868 wc.y = (HEIGHT(r) - win->g.h) / 2;
870 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
871 win->id, wc.x, wc.y, wc.width, wc.height);
873 XConfigureWindow(display, win->id, mask, &wc);
876 int vertical_msize[SWM_WS_MAX];
879 vertical_init(struct workspace *ws)
881 DNPRINTF(SWM_D_MISC, "vertical_init: workspace: %d\n", ws->idx);
883 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
887 vertical_resize(struct workspace *ws, int id)
889 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
892 case SWM_ARG_ID_MASTERSHRINK:
893 ws->l_state.vertical_msize--;
894 if (ws->l_state.vertical_msize < 1)
895 ws->l_state.vertical_msize = 1;
897 case SWM_ARG_ID_MASTERGROW:
898 ws->l_state.vertical_msize++;
899 if (ws->l_state.vertical_msize > SWM_V_SLICE - 1)
900 ws->l_state.vertical_msize = SWM_V_SLICE - 1;
909 vertical_stack(struct workspace *ws, struct swm_geometry *g) {
911 struct swm_geometry gg = *g;
912 struct ws_win *win, *winfocus;
913 int i, hrh, winno, main_width;
916 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
918 if ((winno = count_win(ws, 0)) == 0)
921 if (ws->focus == NULL)
922 ws->focus = TAILQ_FIRST(&ws->winlist);
923 winfocus = cur_focus ? cur_focus : ws->focus;
926 main_width = (g->w / SWM_V_SLICE) *
927 ws->l_state.vertical_msize;
932 hrh = g->h / (winno - 1);
937 TAILQ_FOREACH(win, &ws->winlist, entry) {
939 gg.x += main_width + 2;
940 gg.w = g->w - (main_width + 2);
942 if (i != 0 && hrh != 0) {
943 /* correct the last window for lost pixels */
944 if (win == TAILQ_LAST(&ws->winlist, ws_win_list)) {
945 gg.h = hrh + (g->h - (i * hrh));
949 /* leave first right hand window at y = 0 */
955 if (win->transient != 0 || win->floating != 0)
956 stack_floater(win, ws->r);
958 bzero(&wc, sizeof wc);
960 win->g.x = wc.x = gg.x;
961 win->g.y = wc.y = gg.y;
962 win->g.w = wc.width = gg.w;
963 win->g.h = wc.height = gg.h;
964 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
965 XConfigureWindow(display, win->id, mask, &wc);
968 XMapRaised(display, win->id);
973 focus_win(winfocus); /* has to be done outside of the loop */
978 horizontal_init(struct workspace *ws)
980 DNPRINTF(SWM_D_STACK, "horizontal_init: workspace: %d\n", ws->idx);
982 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
986 horizontal_resize(struct workspace *ws, int id)
988 DNPRINTF(SWM_D_STACK, "horizontal_resize: workspace: %d\n", ws->idx);
991 case SWM_ARG_ID_MASTERSHRINK:
992 ws->l_state.horizontal_msize--;
993 if (ws->l_state.horizontal_msize < 1)
994 ws->l_state.horizontal_msize = 1;
996 case SWM_ARG_ID_MASTERGROW:
997 ws->l_state.horizontal_msize++;
998 if (ws->l_state.horizontal_msize > SWM_H_SLICE - 1)
999 ws->l_state.horizontal_msize = SWM_H_SLICE - 1;
1008 horizontal_stack(struct workspace *ws, struct swm_geometry *g) {
1010 struct swm_geometry gg = *g;
1011 struct ws_win *win, *winfocus;
1012 int i, hrw, winno, main_height;
1015 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
1017 if ((winno = count_win(ws, 0)) == 0)
1020 if (ws->focus == NULL)
1021 ws->focus = TAILQ_FIRST(&ws->winlist);
1022 winfocus = cur_focus ? cur_focus : ws->focus;
1025 main_height = (g->h / SWM_H_SLICE) *
1026 ws->l_state.horizontal_msize;
1031 hrw = g->w / (winno - 1);
1036 TAILQ_FOREACH(win, &ws->winlist, entry) {
1038 gg.y += main_height + 2;
1039 gg.h = g->h - (main_height + 2);
1041 if (i != 0 && hrw != 0) {
1042 /* correct the last window for lost pixels */
1043 if (win == TAILQ_LAST(&ws->winlist, ws_win_list)) {
1044 gg.w = hrw + (g->w - (i * hrw));
1048 /* leave first bottom window at x = 0 */
1054 if (win->transient != 0 || win->floating != 0)
1055 stack_floater(win, ws->r);
1057 bzero(&wc, sizeof wc);
1058 wc.border_width = 1;
1059 win->g.x = wc.x = gg.x;
1060 win->g.y = wc.y = gg.y;
1061 win->g.w = wc.width = gg.w;
1062 win->g.h = wc.height = gg.h;
1063 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1064 XConfigureWindow(display, win->id, mask, &wc);
1067 XMapRaised(display, win->id);
1072 focus_win(winfocus); /* this has to be done outside of the loop */
1075 /* fullscreen view */
1077 max_stack(struct workspace *ws, struct swm_geometry *g) {
1079 struct swm_geometry gg = *g;
1080 struct ws_win *win, *winfocus;
1083 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1085 if (count_win(ws, 0) == 0)
1088 if (ws->focus == NULL)
1089 ws->focus = TAILQ_FIRST(&ws->winlist);
1090 winfocus = cur_focus ? cur_focus : ws->focus;
1092 TAILQ_FOREACH(win, &ws->winlist, entry) {
1093 if (win->transient != 0 || win->floating != 0) {
1094 if (win == ws->focus) {
1096 stack_floater(win, ws->r);
1097 XMapRaised(display, win->id);
1099 XUnmapWindow(display, win->id);
1101 bzero(&wc, sizeof wc);
1102 wc.border_width = 1;
1103 win->g.x = wc.x = gg.x;
1104 win->g.y = wc.y = gg.y;
1105 win->g.w = wc.width = gg.w;
1106 win->g.h = wc.height = gg.h;
1107 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1108 XConfigureWindow(display, win->id, mask, &wc);
1110 if (win == ws->focus) {
1111 XMapRaised(display, win->id);
1113 XUnmapWindow(display, win->id);
1118 focus_win(winfocus); /* has to be done outside of the loop */
1122 send_to_ws(struct swm_region *r, union arg *args)
1124 int wsid = args->id;
1125 struct ws_win *win = cur_focus;
1126 struct workspace *ws, *nws;
1128 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1131 nws = &win->s->ws[wsid];
1133 XUnmapWindow(display, win->id);
1135 /* find a window to focus */
1136 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1137 if (ws->focus == NULL)
1138 ws->focus = TAILQ_FIRST(&ws->winlist);
1139 if (ws->focus == win)
1142 TAILQ_REMOVE(&ws->winlist, win, entry);
1144 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1147 if (count_win(nws, 1) == 1)
1155 /* key definitions */
1159 void (*func)(struct swm_region *r, union arg *);
1162 /* modifier key function argument */
1163 { MODKEY, XK_space, cycle_layout, {0} },
1164 { MODKEY | ShiftMask, XK_space, stack_reset, {0} },
1165 { MODKEY, XK_h, resize_master, {.id = SWM_ARG_ID_MASTERSHRINK} },
1166 { MODKEY, XK_l, resize_master, {.id = SWM_ARG_ID_MASTERGROW} },
1167 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1168 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1169 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1170 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1171 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1172 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1173 { MODKEY, XK_p, spawn, {.argv = spawn_menu} },
1174 { MODKEY | ShiftMask, XK_q, quit, {0} },
1175 { MODKEY, XK_q, restart, {0} },
1176 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1177 { MODKEY, XK_1, switchws, {.id = 0} },
1178 { MODKEY, XK_2, switchws, {.id = 1} },
1179 { MODKEY, XK_3, switchws, {.id = 2} },
1180 { MODKEY, XK_4, switchws, {.id = 3} },
1181 { MODKEY, XK_5, switchws, {.id = 4} },
1182 { MODKEY, XK_6, switchws, {.id = 5} },
1183 { MODKEY, XK_7, switchws, {.id = 6} },
1184 { MODKEY, XK_8, switchws, {.id = 7} },
1185 { MODKEY, XK_9, switchws, {.id = 8} },
1186 { MODKEY, XK_0, switchws, {.id = 9} },
1187 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1188 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1189 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1190 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1191 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1192 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1193 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1194 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1195 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1196 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1197 { MODKEY, XK_b, bar_toggle, {0} },
1198 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1199 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1203 updatenumlockmask(void)
1206 XModifierKeymap *modmap;
1208 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1210 modmap = XGetModifierMapping(display);
1211 for (i = 0; i < 8; i++)
1212 for (j = 0; j < modmap->max_keypermod; j++)
1213 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1214 == XKeysymToKeycode(display, XK_Num_Lock))
1215 numlockmask = (1 << i);
1217 XFreeModifiermap(modmap);
1223 unsigned int i, j, k;
1225 unsigned int modifiers[] =
1226 { 0, LockMask, numlockmask, numlockmask | LockMask };
1228 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1229 updatenumlockmask();
1231 for (k = 0; k < ScreenCount(display); k++) {
1232 if (TAILQ_EMPTY(&screens[k].rl))
1234 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
1235 for(i = 0; i < LENGTH(keys); i++) {
1236 if((code = XKeysymToKeycode(display, keys[i].keysym)))
1237 for(j = 0; j < LENGTH(modifiers); j++)
1238 XGrabKey(display, code,
1239 keys[i].mod | modifiers[j],
1240 screens[k].root, True,
1241 GrabModeAsync, GrabModeAsync);
1248 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1256 XKeyEvent *ev = &e->xkey;
1258 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1260 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1261 for (i = 0; i < LENGTH(keys); i++)
1262 if (keysym == keys[i].keysym
1263 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1265 keys[i].func(root_to_region(ev->root),
1270 buttonpress(XEvent *e)
1272 XButtonPressedEvent *ev = &e->xbutton;
1273 #ifdef SWM_CLICKTOFOCUS
1275 struct workspace *ws;
1276 struct swm_region *r;
1279 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1281 if (ev->window == ev->root) {
1282 rootclick = ev->root;
1285 if (ev->window == cur_focus->id)
1287 #ifdef SWM_CLICKTOFOCUS
1288 r = root_to_region(ev->root);
1290 TAILQ_FOREACH(win, &ws->winlist, entry)
1291 if (win->id == ev->window) {
1292 /* focus in the clicked window */
1293 XSetWindowBorder(display, ev->window, 0xff0000);
1294 XSetWindowBorder(display, ws->focus->id, 0x888888);
1295 XSetInputFocus(display, ev->window, RevertToPointerRoot,
1298 XSync(display, False);
1305 set_win_state(struct ws_win *win, long state)
1307 long data[] = {state, None};
1309 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1311 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1312 (unsigned char *)data, 2);
1316 manage_window(Window id, struct workspace *ws)
1322 TAILQ_FOREACH(win, &ws->winlist, entry) {
1324 return (win); /* already being managed */
1327 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1328 errx(1, "calloc: failed to allocate memory for new window");
1332 win->s = ws->r->s; /* this never changes */
1333 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
1335 /* make new win focused */
1338 XGetTransientForHint(display, win->id, &trans);
1340 win->transient = trans;
1341 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1342 (unsigned)win->id, win->transient);
1344 XGetWindowAttributes(display, id, &win->wa);
1345 win->g.w = win->wa.width;
1346 win->g.h = win->wa.height;
1347 win->g.x = win->wa.x;
1348 win->g.y = win->wa.y;
1350 /* XXX make this a table */
1351 bzero(&ch, sizeof ch);
1352 if (XGetClassHint(display, win->id, &ch)) {
1353 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1354 if (!strcmp(ch.res_class, "MPlayer") && !strcmp(ch.res_name, "xv")) {
1358 XFree(ch.res_class);
1363 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
1364 PropertyChangeMask | StructureNotifyMask);
1366 set_win_state(win, NormalState);
1372 configurerequest(XEvent *e)
1374 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1379 if ((win = find_window(ev->window)) == NULL)
1383 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
1385 bzero(&wc, sizeof wc);
1388 wc.width = ev->width;
1389 wc.height = ev->height;
1390 wc.border_width = ev->border_width;
1391 wc.sibling = ev->above;
1392 wc.stack_mode = ev->detail;
1393 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
1395 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
1397 if (win->floating) {
1398 if (ev->value_mask & CWX)
1400 if (ev->value_mask & CWY)
1402 if (ev->value_mask & CWWidth)
1403 win->g.w = ev->width;
1404 if (ev->value_mask & CWHeight)
1405 win->g.h = ev->height;
1406 if (win->ws->r != NULL) {
1407 /* this seems to be full screen */
1408 if (win->g.w > WIDTH(win->ws->r)) {
1413 if (win->g.h > HEIGHT(win->ws->r)) {
1419 if ((ev->value_mask & (CWX|CWY)) &&
1420 !(ev->value_mask & (CWWidth|CWHeight)))
1422 XMoveResizeWindow(display, win->id,
1423 win->g.x, win->g.y, win->g.w, win->g.h);
1430 configurenotify(XEvent *e)
1432 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1433 e->xconfigure.window);
1437 destroynotify(XEvent *e)
1440 XDestroyWindowEvent *ev = &e->xdestroywindow;
1442 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1444 if ((win = find_window(ev->window)) != NULL) {
1445 struct workspace *ws = win->ws;
1446 /* find a window to focus */
1447 if (ws->focus == win)
1448 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1449 if (ws->focus == NULL)
1450 ws->focus = TAILQ_FIRST(&ws->winlist);
1451 if (ws->focus == win)
1453 if (cur_focus == win) {
1454 if (ws->focus == NULL)
1455 unfocus_win(win); /* XXX focus another ws? */
1457 focus_win(ws->focus);
1460 TAILQ_REMOVE(&ws->winlist, win, entry);
1461 set_win_state(win, WithdrawnState);
1469 enternotify(XEvent *e)
1471 XCrossingEvent *ev = &e->xcrossing;
1475 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1477 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
1478 ev->window != ev->root)
1481 /* eat event(r) to prevent autofocus */
1485 /* brute force for now */
1486 for (i = 0; i < ScreenCount(display); i++) {
1487 for (j = 0; j < SWM_WS_MAX; j++) {
1488 TAILQ_FOREACH(win, &screens[i].ws[j].winlist , entry) {
1489 if (win->id == ev->window)
1499 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
1501 /* XXX this probably needs better handling now.
1502 if (ev->window == ev->root)
1506 * kill grab for now so that we can cut and paste , this screws up
1510 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1511 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1512 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1517 mappingnotify(XEvent *e)
1519 XMappingEvent *ev = &e->xmapping;
1521 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1523 XRefreshKeyboardMapping(ev);
1524 if (ev->request == MappingKeyboard)
1529 maprequest(XEvent *e)
1531 XMapRequestEvent *ev = &e->xmaprequest;
1532 XWindowAttributes wa;
1533 struct swm_region *r;
1535 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1536 e->xmaprequest.window);
1538 if (!XGetWindowAttributes(display, ev->window, &wa))
1540 if (wa.override_redirect)
1542 r = root_to_region(wa.root);
1543 manage_window(e->xmaprequest.window, r->ws);
1548 propertynotify(XEvent *e)
1550 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1551 e->xproperty.window);
1555 unmapnotify(XEvent *e)
1557 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1561 visibilitynotify(XEvent *e)
1564 struct swm_region *r;
1566 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
1567 e->xvisibility.window);
1568 if (e->xvisibility.state == VisibilityUnobscured)
1569 for (i = 0; i < ScreenCount(display); i++)
1570 TAILQ_FOREACH(r, &screens[i].rl, entry)
1571 if (e->xvisibility.window == r->bar_window)
1575 void (*handler[LASTEvent])(XEvent *) = {
1577 [KeyPress] = keypress,
1578 [ButtonPress] = buttonpress,
1579 [ConfigureRequest] = configurerequest,
1580 [ConfigureNotify] = configurenotify,
1581 [DestroyNotify] = destroynotify,
1582 [EnterNotify] = enternotify,
1583 [FocusIn] = focusin,
1584 [MappingNotify] = mappingnotify,
1585 [MapRequest] = maprequest,
1586 [PropertyNotify] = propertynotify,
1587 [UnmapNotify] = unmapnotify,
1588 [VisibilityNotify] = visibilitynotify,
1592 xerror_start(Display *d, XErrorEvent *ee)
1599 xerror(Display *d, XErrorEvent *ee)
1601 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1609 xerrorxlib = XSetErrorHandler(xerror_start);
1611 /* this causes an error if some other window manager is running */
1612 XSelectInput(display, DefaultRootWindow(display),
1613 SubstructureRedirectMask);
1614 XSync(display, False);
1618 XSetErrorHandler(xerror);
1619 XSync(display, False);
1628 unsigned char *p = NULL;
1629 unsigned long n, extra;
1632 astate = XInternAtom(display, "WM_STATE", False);
1633 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
1634 &real, &format, &n, &extra, (unsigned char **)&p);
1635 if (status != Success)
1644 new_region(struct swm_screen *s, struct workspace *ws,
1645 int x, int y, int w, int h)
1647 struct swm_region *r;
1649 DNPRINTF(SWM_D_MISC, "new region on screen %d: %dx%d (%d, %d)\n",
1650 s->idx, x, y, w, h);
1652 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
1653 errx(1, "calloc: failed to allocate memory for screen");
1662 TAILQ_INSERT_TAIL(&s->rl, r, entry);
1669 #ifdef SWM_XRR_HAS_CRTC
1670 XRRCrtcInfo *crtc_info;
1671 XRRScreenResources *res;
1673 #endif /* SWM_XRR_HAS_CRTC */
1674 Window d1, d2, *wins = NULL;
1675 XWindowAttributes wa;
1676 struct swm_region *r;
1678 int errorbase, major, minor;
1681 struct workspace *ws;
1683 if ((screens = calloc(ScreenCount(display),
1684 sizeof(struct swm_screen))) == NULL)
1685 errx(1, "calloc: screens");
1687 /* map physical screens */
1688 for (i = 0; i < ScreenCount(display); i++) {
1689 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
1691 TAILQ_INIT(&screens[i].rl);
1692 screens[i].root = RootWindow(display, i);
1693 XGetWindowAttributes(display, screens[i].root, &wa);
1694 XSelectInput(display, screens[i].root,
1695 ButtonPressMask | wa.your_event_mask);
1697 /* set default colors */
1698 screens[i].color_focus = name_to_color("red");
1699 screens[i].color_unfocus = name_to_color("rgb:88/88/88");
1700 screens[i].bar_border = name_to_color("rgb:00/80/80");
1701 screens[i].bar_color = name_to_color("black");
1702 screens[i].bar_font_color = name_to_color("rgb:a0/a0/a0");
1704 /* init all workspaces */
1705 for (j = 0; j < SWM_WS_MAX; j++) {
1706 ws = &screens[i].ws[j];
1711 TAILQ_INIT(&ws->winlist);
1713 for (k = 0; layouts[k].l_stack != NULL; k++) {
1714 if (layouts[k].l_init != NULL)
1715 layouts[k].l_init(ws);
1717 ws->cur_layout = &layouts[0];
1720 /* map virtual screens onto physical screens */
1721 screens[i].xrandr_support = XRRQueryExtension(display,
1722 &xrandr_eventbase, &errorbase);
1723 if (screens[i].xrandr_support)
1724 if (XRRQueryVersion(display, &major, &minor) &&
1726 screens[i].xrandr_support = 0;
1728 #if 0 /* not ready for dynamic screen changes */
1729 if (screens[i].xrandr_support)
1730 XRRSelectInput(display,
1732 RRScreenChangeNotifyMask);
1735 /* grab existing windows (before we build the bars)*/
1736 if (!XQueryTree(display, screens[i].root,
1737 &d1, &d2, &wins, &num)) {
1738 if (wins) /* not sure if this is necessary */
1743 #ifdef SWM_XRR_HAS_CRTC
1744 res = XRRGetScreenResources(display, screens[i].root);
1747 new_region(&screens[i], &screens[i].ws[w],
1748 0, 0, DisplayWidth(display, i),
1749 DisplayHeight(display, i));
1753 for (c = 0; c < ncrtc; c++) {
1754 crtc_info = XRRGetCrtcInfo(display, res, res->crtcs[c]);
1755 if (crtc_info->noutput == 0)
1758 if (crtc_info != NULL && crtc_info->mode == None)
1759 new_region(&screens[i], &screens[i].ws[w],
1760 0, 0, DisplayWidth(display, i),
1761 DisplayHeight(display, i));
1763 new_region(&screens[i], &screens[i].ws[w],
1764 crtc_info->x, crtc_info->y,
1765 crtc_info->width, crtc_info->height);
1769 new_region(&screens[i], &screens[i].ws[w],
1770 0, 0, DisplayWidth(display, i),
1771 DisplayHeight(display, i));
1772 #endif /* SWM_XRR_HAS_CRTC */
1774 /* attach windows to a region */
1775 /* normal windows */
1776 if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
1777 errx(1, "no regions on screen %d", i);
1779 for (i = 0; i < num; i++) {
1780 XGetWindowAttributes(display, wins[i], &wa);
1781 if (!XGetWindowAttributes(display, wins[i], &wa) ||
1782 wa.override_redirect ||
1783 XGetTransientForHint(display, wins[i], &d1))
1785 if (wa.map_state == IsViewable ||
1786 getstate(wins[i]) == NormalState)
1787 manage_window(wins[i], r->ws);
1789 /* transient windows */
1790 for (i = 0; i < num; i++) {
1791 if (!XGetWindowAttributes(display, wins[i], &wa))
1793 if (XGetTransientForHint(display, wins[i], &d1) &&
1794 (wa.map_state == IsViewable || getstate(wins[i]) ==
1796 manage_window(wins[i], r->ws);
1804 main(int argc, char *argv[])
1807 char conf[PATH_MAX], *cfile = NULL;
1812 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
1813 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
1814 warnx("no locale support");
1816 if (!(display = XOpenDisplay(0)))
1817 errx(1, "can not open display");
1820 errx(1, "other wm running");
1822 astate = XInternAtom(display, "WM_STATE", False);
1824 /* look for local and global conf file */
1825 pwd = getpwuid(getuid());
1827 errx(1, "invalid user %d", getuid());
1829 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
1830 if (stat(conf, &sb) != -1) {
1831 if (S_ISREG(sb.st_mode))
1834 /* try global conf file */
1835 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
1836 if (!stat(conf, &sb))
1837 if (S_ISREG(sb.st_mode))
1845 //ws[0].focus = TAILQ_FIRST(&ws[0].winlist);
1851 XNextEvent(display, &e);
1852 if (handler[e.type])
1853 handler[e.type](&e);
1856 XCloseDisplay(display);