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
88 /* #define SWM_DEBUG */
90 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
91 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
92 #define SWM_D_MISC 0x0001
93 #define SWM_D_EVENT 0x0002
94 #define SWM_D_WS 0x0004
95 #define SWM_D_FOCUS 0x0008
96 #define SWM_D_MOVE 0x0010
97 #define SWM_D_STACK 0x0020
99 u_int32_t swm_debug = 0
108 #define DPRINTF(x...)
109 #define DNPRINTF(n,x...)
112 #define LENGTH(x) (sizeof x / sizeof x[0])
113 #define MODKEY Mod1Mask
114 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
116 #define X(r) (r)->g.x
117 #define Y(r) (r)->g.y
118 #define WIDTH(r) (r)->g.w
119 #define HEIGHT(r) (r)->g.h
123 int (*xerrorxlib)(Display *, XErrorEvent *);
126 int xrandr_eventbase;
127 int ignore_enter = 0;
128 unsigned int numlockmask = 0;
132 double dialog_ratio = .6;
140 char *bar_fonts[] = {
141 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
142 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
146 /* terminal + args */
147 char *spawn_term[] = { "xterm", NULL };
148 char *spawn_menu[] = { "dmenu_run", NULL };
150 /* layout manager data */
151 struct swm_geometry {
161 /* virtual "screens" */
163 TAILQ_ENTRY(swm_region) entry;
164 struct swm_geometry g;
166 struct workspace *ws; /* current workspace on this region */
167 struct swm_screen *s; /* screen idx */
169 TAILQ_HEAD(swm_region_list, swm_region);
173 TAILQ_ENTRY(ws_win) entry;
175 struct swm_geometry g;
179 struct workspace *ws; /* always valid */
180 struct swm_screen *s; /* always valid, never changes */
181 XWindowAttributes wa;
183 TAILQ_HEAD(ws_win_list, ws_win);
185 /* layout handlers */
187 void vertical_init(struct workspace *);
188 void vertical_resize(int);
189 void vertical_stack(struct workspace *, struct swm_geometry *);
190 void horizontal_init(struct workspace *);
191 void horizontal_resize(int);
192 void horizontal_stack(struct workspace *, struct swm_geometry *);
193 void max_init(struct workspace *);
194 void max_stack(struct workspace *, struct swm_geometry *);
197 void (*l_init)(struct workspace *); /* init/reset */
198 void (*l_stack)(struct workspace *, struct swm_geometry *);
199 void (*l_resize)(int);
201 /* init stack, resize */
202 { vertical_init, vertical_stack, vertical_resize},
203 { horizontal_init, horizontal_stack, horizontal_resize},
204 { NULL, max_stack, NULL},
208 #define SWM_H_SLICE (32)
209 #define SWM_V_SLICE (32)
212 /* define work spaces */
214 int idx; /* workspace index */
215 int restack; /* restack on switch */
216 struct layout *cur_layout; /* current layout handlers */
217 struct ws_win *focus; /* may be NULL */
218 struct swm_region *r; /* may be NULL */
219 struct ws_win_list winlist; /* list of windows in ws */
223 int horizontal_msize;
228 /* physical screen mapping */
229 #define SWM_WS_MAX (10) /* XXX Too small? */
231 int idx; /* screen index */
232 struct swm_region_list rl; /* list of regions on this screen */
235 struct workspace ws[SWM_WS_MAX];
238 unsigned long bar_border;
239 unsigned long bar_color;
240 unsigned long bar_font_color;
241 unsigned long color_focus; /* XXX should this be per ws? */
242 unsigned long color_unfocus;
244 struct swm_screen *screens;
247 struct ws_win *cur_focus = NULL;
249 /* args to functions */
252 #define SWM_ARG_ID_FOCUSNEXT (0)
253 #define SWM_ARG_ID_FOCUSPREV (1)
254 #define SWM_ARG_ID_FOCUSMAIN (2)
255 #define SWM_ARG_ID_SWAPNEXT (3)
256 #define SWM_ARG_ID_SWAPPREV (4)
257 #define SWM_ARG_ID_SWAPMAIN (5)
258 #define SWM_ARG_ID_MASTERSHRINK (6)
259 #define SWM_ARG_ID_MASTERGROW (7)
264 name_to_color(char *colorname)
268 XColor screen_def, exact_def;
269 unsigned long result = 0;
270 char cname[32] = "#";
272 cmap = DefaultColormap(display, screens[0].idx);
273 status = XAllocNamedColor(display, cmap, colorname,
274 &screen_def, &exact_def);
276 strlcat(cname, colorname + 2, sizeof cname - 1);
277 status = XAllocNamedColor(display, cmap, cname, &screen_def,
281 result = screen_def.pixel;
283 fprintf(stderr, "color '%s' not found.\n", colorname);
288 /* conf file stuff */
289 #define SWM_CONF_WS "\n= \t"
290 #define SWM_CONF_FILE "scrotwm.conf"
292 conf_load(char *filename)
295 char *line, *cp, *var, *val;
296 size_t len, lineno = 0;
299 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
301 if (filename == NULL)
304 if ((config = fopen(filename, "r")) == NULL)
308 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
312 cp += (long)strspn(cp, SWM_CONF_WS);
318 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
320 cp += (long)strspn(cp, SWM_CONF_WS);
321 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
324 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
327 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
328 bar_enabled = atoi(val);
329 else if (!strncmp(var, "bar_border",
330 strlen("bar_border")))
331 for (i = 0; i < ScreenCount(display); i++)
332 screens[i].bar_border = name_to_color(val);
333 else if (!strncmp(var, "bar_color",
334 strlen("bar_color")))
335 for (i = 0; i < ScreenCount(display); i++)
336 screens[i].bar_color = name_to_color(val);
337 else if (!strncmp(var, "bar_font_color",
338 strlen("bar_font_color")))
339 for (i = 0; i < ScreenCount(display); i++)
340 screens[i].bar_font_color = name_to_color(val);
341 else if (!strncmp(var, "bar_font", strlen("bar_font")))
342 asprintf(&bar_fonts[0], "%s", val);
348 if (!strncmp(var, "color_focus", strlen("color_focus")))
349 for (i = 0; i < ScreenCount(display); i++)
350 screens[i].color_focus = name_to_color(val);
351 else if (!strncmp(var, "color_unfocus",
352 strlen("color_unfocus")))
353 for (i = 0; i < ScreenCount(display); i++)
354 screens[i].color_unfocus = name_to_color(val);
360 if (!strncmp(var, "dialog_ratio",
361 strlen("dialog_ratio"))) {
362 dialog_ratio = atof(val);
363 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
370 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
371 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
382 errx(1, "invalid conf file entry: %s=%s", var, val);
386 bar_print(struct swm_region *r)
391 if (bar_enabled == 0)
395 XSetForeground(display, bar_gc, r->s->bar_color);
396 XDrawString(display, r->bar_window,
397 bar_gc, 4, bar_fs->ascent, bar_text, strlen(bar_text));
401 localtime_r(&tmt, &tm);
402 strftime(bar_text, sizeof bar_text, "%a %b %d %R %Z %Y", &tm);
403 XSetForeground(display, bar_gc, r->s->bar_font_color);
404 XDrawString(display, r->bar_window, bar_gc, 4,
405 bar_fs->ascent, bar_text, strlen(bar_text));
406 XSync(display, False);
414 /* XXX yeah yeah byte me */
416 bar_print(cur_focus->ws->r);
420 bar_toggle(union arg *args)
422 struct swm_region *r;
425 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
428 for (i = 0; i < ScreenCount(display); i++)
429 TAILQ_FOREACH(r, &screens[i].rl, entry)
430 XUnmapWindow(display, r->bar_window);
432 for (i = 0; i < ScreenCount(display); i++)
433 TAILQ_FOREACH(r, &screens[i].rl, entry)
434 XMapRaised(display, r->bar_window);
436 bar_enabled = !bar_enabled;
437 XSync(display, False);
438 for (i = 0; i < ScreenCount(display); i++)
439 for (j = 0; j < SWM_WS_MAX; j++)
440 screens[i].ws[j].restack = 1;
443 /* must be after stack */
444 for (i = 0; i < ScreenCount(display); i++)
445 TAILQ_FOREACH(r, &screens[i].rl, entry)
450 bar_setup(struct swm_region *r)
454 for (i = 0; bar_fonts[i] != NULL; i++) {
455 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
459 if (bar_fonts[i] == NULL)
460 errx(1, "couldn't load font");
461 bar_height = bar_fs->ascent + bar_fs->descent + 3;
463 r->bar_window = XCreateSimpleWindow(display,
464 r->s->root, X(r), Y(r), WIDTH(r), bar_height - 2,
465 1, r->s->bar_border, r->s->bar_color);
466 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
467 XSetFont(display, bar_gc, bar_fs->fid);
468 XSelectInput(display, r->bar_window, VisibilityChangeMask);
470 XMapRaised(display, r->bar_window);
471 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
473 if (signal(SIGALRM, bar_signal) == SIG_ERR)
474 err(1, "could not install bar_signal");
479 config_win(struct ws_win *win)
483 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
484 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
485 ce.type = ConfigureNotify;
486 ce.display = display;
492 ce.height = win->g.h;
493 ce.border_width = 1; /* XXX store this! */
495 ce.override_redirect = False;
496 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
500 count_win(struct workspace *ws, int count_transient)
505 TAILQ_FOREACH(win, &ws->winlist, entry) {
506 if (count_transient == 0 && win->floating)
508 if (count_transient == 0 && win->transient)
512 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
518 quit(union arg *args)
520 DNPRINTF(SWM_D_MISC, "quit\n");
525 restart(union arg *args)
527 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
529 XCloseDisplay(display);
530 execvp(start_argv[0], start_argv);
531 fprintf(stderr, "execvp failed\n");
539 if (cur_focus && cur_focus->ws->r) {
540 return (cur_focus->ws->r);
542 /* XXX get region from pointer */
544 struct swm_region *r;
545 for (i = 0; i < ScreenCount(display); i++)
546 if ((r = TAILQ_FIRST(&screens[i].rl)) != NULL)
549 errx(1, "cur_region: no regions!?!?!\n");
555 return (cur_region()->ws);
559 find_window(Window id)
564 for (i = 0; i < ScreenCount(display); i++)
565 for (j = 0; j < SWM_WS_MAX; j++)
566 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
573 spawn(union arg *args)
575 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
577 * The double-fork construct avoids zombie processes and keeps the code
578 * clean from stupid signal handlers.
583 close(ConnectionNumber(display));
585 execvp(args->argv[0], args->argv);
586 fprintf(stderr, "execvp failed\n");
595 unfocus_win(struct ws_win *win)
597 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
598 if (win->ws->r && win->focus)
599 XSetWindowBorder(display, win->id,
600 win->ws->r->s->color_unfocus);
602 if (win->ws->focus == win)
603 win->ws->focus = NULL;
604 if (cur_focus == win)
610 focus_win(struct ws_win *win)
612 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
614 win->ws->focus = win;
615 if (win->ws->r != NULL) {
616 if (cur_focus && cur_focus != win)
617 unfocus_win(cur_focus);
620 XSetWindowBorder(display, win->id,
621 win->ws->r->s->color_focus);
623 XSetInputFocus(display, win->id,
624 RevertToPointerRoot, CurrentTime);
629 switchws(union arg *args)
632 struct swm_region *this_r, *other_r;
634 struct workspace *new_ws, *old_ws;
636 DNPRINTF(SWM_D_WS, "switchws: %d\n", wsid + 1);
638 this_r = cur_region();
640 new_ws = &this_r->s->ws[wsid];
642 if (new_ws == old_ws)
647 /* if the other region is hidden, switch windows */
649 /* map new window first to prevent ugly blinking */
650 TAILQ_FOREACH(win, &new_ws->winlist, entry)
651 XMapRaised(display, win->id);
653 TAILQ_FOREACH(win, &old_ws->winlist, entry)
654 XUnmapWindow(display, win->id);
658 other_r->ws = old_ws;
667 if (new_ws->restack) {
672 focus_win(new_ws->focus);
673 XSync(display, False);
678 swapwin(union arg *args)
680 struct ws_win *target;
681 struct ws_win_list *wl;
684 DNPRINTF(SWM_D_MOVE, "swapwin: id %d\n", args->id);
685 if (cur_focus == NULL)
688 wl = &cur_focus->ws->winlist;
691 case SWM_ARG_ID_SWAPPREV:
692 target = TAILQ_PREV(cur_focus, ws_win_list, entry);
693 TAILQ_REMOVE(wl, cur_focus, entry);
695 TAILQ_INSERT_TAIL(wl, cur_focus, entry);
697 TAILQ_INSERT_BEFORE(target, cur_focus, entry);
699 case SWM_ARG_ID_SWAPNEXT:
700 target = TAILQ_NEXT(cur_focus, entry);
701 TAILQ_REMOVE(wl, cur_focus, entry);
703 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
705 TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
707 case SWM_ARG_ID_SWAPMAIN:
708 target = TAILQ_FIRST(wl);
709 if (target == cur_focus)
711 TAILQ_REMOVE(wl, target, entry);
712 TAILQ_INSERT_BEFORE(cur_focus, target, entry);
713 TAILQ_REMOVE(wl, cur_focus, entry);
714 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
717 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
726 focus(union arg *args)
728 struct ws_win *winfocus, *winlostfocus;
729 struct ws_win_list *wl;
731 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
732 if (cur_focus == NULL)
735 wl = &cur_focus->ws->winlist;
737 winlostfocus = cur_focus;
740 case SWM_ARG_ID_FOCUSPREV:
741 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
742 if (winfocus == NULL)
743 winfocus = TAILQ_LAST(wl, ws_win_list);
746 case SWM_ARG_ID_FOCUSNEXT:
747 winfocus = TAILQ_NEXT(cur_focus, entry);
748 if (winfocus == NULL)
749 winfocus = TAILQ_FIRST(wl);
752 case SWM_ARG_ID_FOCUSMAIN:
753 winfocus = TAILQ_FIRST(wl);
760 if (winfocus == winlostfocus)
764 XSync(display, False);
768 cycle_layout(union arg *args)
770 struct workspace *ws;
774 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
777 if (ws->cur_layout->l_stack == NULL)
778 ws->cur_layout = &layouts[0];
785 resize_master(union arg *args)
787 struct workspace *ws = cur_ws();
789 DNPRINTF(SWM_D_STACK, "resize_master for workspace %d (id %d\n",
792 if (ws->cur_layout->l_resize != NULL)
793 ws->cur_layout->l_resize(args->id);
797 stack_reset(union arg *args)
799 struct workspace *ws = cur_ws();
801 DNPRINTF(SWM_D_STACK, "stack_reset: ws %d\n", ws->idx);
803 if (ws->cur_layout->l_init != NULL) {
804 ws->cur_layout->l_init(ws);
811 struct swm_geometry g;
812 struct swm_region *r;
815 DNPRINTF(SWM_D_STACK, "stack\n");
817 for (i = 0; i < ScreenCount(display); i++) {
819 TAILQ_FOREACH(r, &screens[i].rl, entry) {
820 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
821 "(screen %d, region %d)\n", r->ws->idx, i, j++);
823 /* start with screen geometry, adjust for bar */
833 r->ws->cur_layout->l_stack(r->ws, &g);
836 XSync(display, False);
840 stack_floater(struct ws_win *win, struct swm_region *r)
845 bzero(&wc, sizeof wc);
846 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
848 if (win->transient) {
849 win->g.w = (double)WIDTH(r) * dialog_ratio;
850 win->g.h = (double)HEIGHT(r) * dialog_ratio;
853 wc.height = win->g.h;
854 wc.x = (WIDTH(r) - win->g.w) / 2;
855 wc.y = (HEIGHT(r) - win->g.h) / 2;
857 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
858 win->id, wc.x, wc.y, wc.width, wc.height);
860 XConfigureWindow(display, win->id, mask, &wc);
863 int vertical_msize[SWM_WS_MAX];
866 vertical_init(struct workspace *ws)
868 DNPRINTF(SWM_D_MISC, "vertical_init: workspace: %d\n", ws->idx);
870 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
874 vertical_resize(int id)
876 struct workspace *ws = cur_ws();
878 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
881 case SWM_ARG_ID_MASTERSHRINK:
882 ws->l_state.vertical_msize--;
883 if (ws->l_state.vertical_msize < 1)
884 ws->l_state.vertical_msize = 1;
886 case SWM_ARG_ID_MASTERGROW:
887 ws->l_state.vertical_msize++;
888 if (ws->l_state.vertical_msize > SWM_V_SLICE - 1)
889 ws->l_state.vertical_msize = SWM_V_SLICE - 1;
898 vertical_stack(struct workspace *ws, struct swm_geometry *g) {
900 struct swm_geometry gg = *g;
901 struct ws_win *win, *winfocus;
902 int i, hrh, winno, main_width;
905 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
907 if ((winno = count_win(ws, 0)) == 0)
910 if (ws->focus == NULL)
911 ws->focus = TAILQ_FIRST(&ws->winlist);
912 winfocus = cur_focus ? cur_focus : ws->focus;
915 main_width = (g->w / SWM_V_SLICE) *
916 ws->l_state.vertical_msize;
921 hrh = g->h / (winno - 1);
926 TAILQ_FOREACH(win, &ws->winlist, entry) {
928 gg.x += main_width + 2;
929 gg.w = g->w - (main_width + 2);
931 if (i != 0 && hrh != 0) {
932 /* correct the last window for lost pixels */
933 if (win == TAILQ_LAST(&ws->winlist, ws_win_list)) {
934 gg.h = hrh + (g->h - (i * hrh));
938 /* leave first right hand window at y = 0 */
944 if (win->transient != 0 || win->floating != 0)
945 stack_floater(win, ws->r);
947 bzero(&wc, sizeof wc);
949 win->g.x = wc.x = gg.x;
950 win->g.y = wc.y = gg.y;
951 win->g.w = wc.width = gg.w;
952 win->g.h = wc.height = gg.h;
953 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
954 XConfigureWindow(display, win->id, mask, &wc);
957 XMapRaised(display, win->id);
962 focus_win(winfocus); /* has to be done outside of the loop */
967 horizontal_init(struct workspace *ws)
969 DNPRINTF(SWM_D_STACK, "horizontal_init: workspace: %d\n", ws->idx);
971 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
975 horizontal_resize(int id)
977 struct workspace *ws = cur_ws();
978 DNPRINTF(SWM_D_STACK, "horizontal_resize: workspace: %d\n", ws->idx);
981 case SWM_ARG_ID_MASTERSHRINK:
982 ws->l_state.horizontal_msize--;
983 if (ws->l_state.horizontal_msize < 1)
984 ws->l_state.horizontal_msize = 1;
986 case SWM_ARG_ID_MASTERGROW:
987 ws->l_state.horizontal_msize++;
988 if (ws->l_state.horizontal_msize > SWM_H_SLICE - 1)
989 ws->l_state.horizontal_msize = SWM_H_SLICE - 1;
998 horizontal_stack(struct workspace *ws, struct swm_geometry *g) {
1000 struct swm_geometry gg = *g;
1001 struct ws_win *win, *winfocus;
1002 int i, hrw, winno, main_height;
1005 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
1007 if ((winno = count_win(ws, 0)) == 0)
1010 if (ws->focus == NULL)
1011 ws->focus = TAILQ_FIRST(&ws->winlist);
1012 winfocus = cur_focus ? cur_focus : ws->focus;
1015 main_height = (g->h / SWM_H_SLICE) *
1016 ws->l_state.horizontal_msize;
1021 hrw = g->w / (winno - 1);
1026 TAILQ_FOREACH(win, &ws->winlist, entry) {
1028 gg.y += main_height + 2;
1029 gg.h = g->h - (main_height + 2);
1031 if (i != 0 && hrw != 0) {
1032 /* correct the last window for lost pixels */
1033 if (win == TAILQ_LAST(&ws->winlist, ws_win_list)) {
1034 gg.w = hrw + (g->w - (i * hrw));
1038 /* leave first bottom window at x = 0 */
1044 if (win->transient != 0 || win->floating != 0)
1045 stack_floater(win, ws->r);
1047 bzero(&wc, sizeof wc);
1048 wc.border_width = 1;
1049 win->g.x = wc.x = gg.x;
1050 win->g.y = wc.y = gg.y;
1051 win->g.w = wc.width = gg.w;
1052 win->g.h = wc.height = gg.h;
1053 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1054 XConfigureWindow(display, win->id, mask, &wc);
1057 XMapRaised(display, win->id);
1062 focus_win(winfocus); /* this has to be done outside of the loop */
1065 /* fullscreen view */
1067 max_stack(struct workspace *ws, struct swm_geometry *g) {
1069 struct swm_geometry gg = *g;
1070 struct ws_win *win, *winfocus;
1073 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1075 if (count_win(ws, 0) == 0)
1078 if (ws->focus == NULL)
1079 ws->focus = TAILQ_FIRST(&ws->winlist);
1080 winfocus = cur_focus ? cur_focus : ws->focus;
1082 TAILQ_FOREACH(win, &ws->winlist, entry) {
1083 if (win->transient != 0 || win->floating != 0) {
1084 if (win == ws->focus) {
1086 stack_floater(win, ws->r);
1087 XMapRaised(display, win->id);
1089 XUnmapWindow(display, win->id);
1091 bzero(&wc, sizeof wc);
1092 wc.border_width = 1;
1093 win->g.x = wc.x = gg.x;
1094 win->g.y = wc.y = gg.y;
1095 win->g.w = wc.width = gg.w;
1096 win->g.h = wc.height = gg.h;
1097 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1098 XConfigureWindow(display, win->id, mask, &wc);
1100 if (win == ws->focus) {
1101 XMapRaised(display, win->id);
1103 XUnmapWindow(display, win->id);
1108 focus_win(winfocus); /* has to be done outside of the loop */
1112 send_to_ws(union arg *args)
1114 int wsid = args->id;
1115 struct ws_win *win = cur_focus;
1116 struct workspace *ws, *nws;
1118 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1121 nws = &win->s->ws[wsid];
1123 XUnmapWindow(display, win->id);
1125 /* find a window to focus */
1126 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1127 if (ws->focus == NULL)
1128 ws->focus = TAILQ_FIRST(&ws->winlist);
1129 if (ws->focus == win)
1132 TAILQ_REMOVE(&ws->winlist, win, entry);
1134 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1137 if (count_win(nws, 1) == 1)
1145 /* key definitions */
1149 void (*func)(union arg *);
1152 /* modifier key function argument */
1153 { MODKEY, XK_space, cycle_layout, {0} },
1154 { MODKEY | ShiftMask, XK_space, stack_reset, {0} },
1155 { MODKEY, XK_h, resize_master, {.id = SWM_ARG_ID_MASTERSHRINK} },
1156 { MODKEY, XK_l, resize_master, {.id = SWM_ARG_ID_MASTERGROW} },
1157 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1158 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1159 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1160 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1161 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1162 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1163 { MODKEY, XK_p, spawn, {.argv = spawn_menu} },
1164 { MODKEY | ShiftMask, XK_q, quit, {0} },
1165 { MODKEY, XK_q, restart, {0} },
1166 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1167 { MODKEY, XK_1, switchws, {.id = 0} },
1168 { MODKEY, XK_2, switchws, {.id = 1} },
1169 { MODKEY, XK_3, switchws, {.id = 2} },
1170 { MODKEY, XK_4, switchws, {.id = 3} },
1171 { MODKEY, XK_5, switchws, {.id = 4} },
1172 { MODKEY, XK_6, switchws, {.id = 5} },
1173 { MODKEY, XK_7, switchws, {.id = 6} },
1174 { MODKEY, XK_8, switchws, {.id = 7} },
1175 { MODKEY, XK_9, switchws, {.id = 8} },
1176 { MODKEY, XK_0, switchws, {.id = 9} },
1177 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1178 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1179 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1180 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1181 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1182 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1183 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1184 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1185 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1186 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1187 { MODKEY, XK_b, bar_toggle, {0} },
1188 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1189 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1193 updatenumlockmask(void)
1196 XModifierKeymap *modmap;
1198 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1200 modmap = XGetModifierMapping(display);
1201 for (i = 0; i < 8; i++)
1202 for (j = 0; j < modmap->max_keypermod; j++)
1203 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1204 == XKeysymToKeycode(display, XK_Num_Lock))
1205 numlockmask = (1 << i);
1207 XFreeModifiermap(modmap);
1213 unsigned int i, j, k;
1215 unsigned int modifiers[] =
1216 { 0, LockMask, numlockmask, numlockmask | LockMask };
1218 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1219 updatenumlockmask();
1221 for (k = 0; k < ScreenCount(display); k++) {
1222 if (TAILQ_EMPTY(&screens[k].rl))
1224 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
1225 for(i = 0; i < LENGTH(keys); i++) {
1226 if((code = XKeysymToKeycode(display, keys[i].keysym)))
1227 for(j = 0; j < LENGTH(modifiers); j++)
1228 XGrabKey(display, code,
1229 keys[i].mod | modifiers[j],
1230 screens[k].root, True,
1231 GrabModeAsync, GrabModeAsync);
1238 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1246 XKeyEvent *ev = &e->xkey;
1248 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1250 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1251 for (i = 0; i < LENGTH(keys); i++)
1252 if (keysym == keys[i].keysym
1253 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1255 keys[i].func(&(keys[i].args));
1259 buttonpress(XEvent *e)
1261 XButtonPressedEvent *ev = &e->xbutton;
1262 #ifdef SWM_CLICKTOFOCUS
1264 struct workspace *ws = cur_ws();
1267 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1269 if (ev->window == ev->root)
1271 if (ev->window == cur_focus->id)
1273 #ifdef SWM_CLICKTOFOCUS
1274 TAILQ_FOREACH(win, &ws->winlist, entry)
1275 if (win->id == ev->window) {
1276 /* focus in the clicked window */
1277 XSetWindowBorder(display, ev->window, 0xff0000);
1278 XSetWindowBorder(display, ws->focus->id, 0x888888);
1279 XSetInputFocus(display, ev->window, RevertToPointerRoot,
1282 XSync(display, False);
1289 set_win_state(struct ws_win *win, long state)
1291 long data[] = {state, None};
1293 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1295 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1296 (unsigned char *)data, 2);
1300 manage_window(Window id, struct workspace *ws)
1306 TAILQ_FOREACH(win, &ws->winlist, entry) {
1308 return (win); /* already being managed */
1311 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1312 errx(1, "calloc: failed to allocate memory for new window");
1316 win->s = ws->r->s; /* this never changes */
1317 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
1319 /* make new win focused */
1322 XGetTransientForHint(display, win->id, &trans);
1324 win->transient = trans;
1325 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1326 (unsigned)win->id, win->transient);
1328 XGetWindowAttributes(display, win->id, &win->wa);
1329 win->g.w = win->wa.width;
1330 win->g.h = win->wa.height;
1331 win->g.x = win->wa.x;
1332 win->g.y = win->wa.y;
1334 /* XXX make this a table */
1335 bzero(&ch, sizeof ch);
1336 if (XGetClassHint(display, win->id, &ch)) {
1337 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1338 if (!strcmp(ch.res_class, "MPlayer") && !strcmp(ch.res_name, "xv")) {
1342 XFree(ch.res_class);
1347 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
1348 PropertyChangeMask | StructureNotifyMask);
1350 set_win_state(win, NormalState);
1356 configurerequest(XEvent *e)
1358 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1363 if ((win = find_window(ev->window)) == NULL)
1367 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
1369 bzero(&wc, sizeof wc);
1372 wc.width = ev->width;
1373 wc.height = ev->height;
1374 wc.border_width = ev->border_width;
1375 wc.sibling = ev->above;
1376 wc.stack_mode = ev->detail;
1377 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
1379 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
1381 if (win->floating) {
1382 if (ev->value_mask & CWX)
1384 if (ev->value_mask & CWY)
1386 if (ev->value_mask & CWWidth)
1387 win->g.w = ev->width;
1388 if (ev->value_mask & CWHeight)
1389 win->g.h = ev->height;
1390 if (win->ws->r != NULL) {
1391 /* this seems to be full screen */
1392 if (win->g.w > WIDTH(win->ws->r)) {
1397 if (win->g.h > HEIGHT(win->ws->r)) {
1403 if ((ev->value_mask & (CWX|CWY)) &&
1404 !(ev->value_mask & (CWWidth|CWHeight)))
1406 XMoveResizeWindow(display, win->id,
1407 win->g.x, win->g.y, win->g.w, win->g.h);
1414 configurenotify(XEvent *e)
1416 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1417 e->xconfigure.window);
1421 destroynotify(XEvent *e)
1424 XDestroyWindowEvent *ev = &e->xdestroywindow;
1426 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1428 if ((win = find_window(ev->window)) != NULL) {
1429 struct workspace *ws = win->ws;
1430 /* find a window to focus */
1431 if (ws->focus == win)
1432 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1433 if (ws->focus == NULL)
1434 ws->focus = TAILQ_FIRST(&ws->winlist);
1435 if (ws->focus == win)
1437 if (cur_focus == win) {
1438 if (ws->focus == NULL)
1439 unfocus_win(win); /* XXX focus another ws? */
1441 focus_win(ws->focus);
1444 TAILQ_REMOVE(&ws->winlist, win, entry);
1445 set_win_state(win, WithdrawnState);
1453 enternotify(XEvent *e)
1455 XCrossingEvent *ev = &e->xcrossing;
1459 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1461 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
1462 ev->window != ev->root)
1465 /* eat event(r) to prevent autofocus */
1469 /* XXX update cur_ws here?... brute force for now */
1470 for (i = 0; i < ScreenCount(display); i++) {
1471 for (j = 0; j < SWM_WS_MAX; j++) {
1472 TAILQ_FOREACH(win, &screens[i].ws[j].winlist , entry) {
1473 if (win->id == ev->window)
1483 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
1485 /* XXX this probably needs better handling now.
1486 if (ev->window == ev->root)
1490 * kill grab for now so that we can cut and paste , this screws up
1494 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1495 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1496 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1501 mappingnotify(XEvent *e)
1503 XMappingEvent *ev = &e->xmapping;
1505 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1507 XRefreshKeyboardMapping(ev);
1508 if (ev->request == MappingKeyboard)
1513 maprequest(XEvent *e)
1515 XMapRequestEvent *ev = &e->xmaprequest;
1516 XWindowAttributes wa;
1517 struct workspace *ws = cur_ws();
1519 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1520 e->xmaprequest.window);
1522 if (!XGetWindowAttributes(display, ev->window, &wa))
1524 if (wa.override_redirect)
1526 manage_window(e->xmaprequest.window, ws); /* RRR */
1531 propertynotify(XEvent *e)
1533 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1534 e->xproperty.window);
1538 unmapnotify(XEvent *e)
1540 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1544 visibilitynotify(XEvent *e)
1547 struct swm_region *r;
1548 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n", e->xvisibility.window);
1549 if (e->xvisibility.state == VisibilityUnobscured)
1550 for (i = 0; i < ScreenCount(display); i++)
1551 TAILQ_FOREACH(r, &screens[i].rl, entry)
1552 if (e->xvisibility.window == r->bar_window)
1556 void (*handler[LASTEvent])(XEvent *) = {
1558 [KeyPress] = keypress,
1559 [ButtonPress] = buttonpress,
1560 [ConfigureRequest] = configurerequest,
1561 [ConfigureNotify] = configurenotify,
1562 [DestroyNotify] = destroynotify,
1563 [EnterNotify] = enternotify,
1564 [FocusIn] = focusin,
1565 [MappingNotify] = mappingnotify,
1566 [MapRequest] = maprequest,
1567 [PropertyNotify] = propertynotify,
1568 [UnmapNotify] = unmapnotify,
1569 [VisibilityNotify] = visibilitynotify,
1573 xerror_start(Display *d, XErrorEvent *ee)
1580 xerror(Display *d, XErrorEvent *ee)
1582 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1590 xerrorxlib = XSetErrorHandler(xerror_start);
1592 /* this causes an error if some other window manager is running */
1593 XSelectInput(display, DefaultRootWindow(display),
1594 SubstructureRedirectMask);
1595 XSync(display, False);
1599 XSetErrorHandler(xerror);
1600 XSync(display, False);
1609 unsigned char *p = NULL;
1610 unsigned long n, extra;
1613 astate = XInternAtom(display, "WM_STATE", False);
1614 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
1615 &real, &format, &n, &extra, (unsigned char **)&p);
1616 if (status != Success)
1625 new_region(struct swm_screen *s, struct workspace *ws,
1626 int x, int y, int w, int h)
1628 struct swm_region *r;
1630 DNPRINTF(SWM_D_MISC, "new region on screen %d: %dx%d (%d, %d)\n",
1631 s->idx, x, y, w, h);
1633 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
1634 errx(1, "calloc: failed to allocate memory for screen");
1643 TAILQ_INSERT_TAIL(&s->rl, r, entry);
1653 if ((screens = calloc(ScreenCount(display),
1654 sizeof(struct swm_screen))) == NULL)
1655 errx(1, "calloc: screens");
1657 /* map physical screens */
1658 for (i = 0; i < ScreenCount(display); i++) {
1659 #ifdef SWM_XRR_HAS_CRTC
1660 XRRCrtcInfo *crtc_info;
1661 XRRScreenResources *res;
1663 #endif /* SWM_XRR_HAS_CRTC */
1664 Window d1, d2, *wins = NULL;
1665 XWindowAttributes wa;
1666 struct swm_region *r;
1667 int errorbase, major, minor;
1668 int j, ncrtc, w = 0;
1672 TAILQ_INIT(&screens[i].rl);
1673 screens[i].root = ScreenOfDisplay(display, i)->root;
1674 screens[i].root = RootWindow(display, i);
1676 /* set default colors */
1677 screens[i].color_focus = name_to_color("red");
1678 screens[i].color_unfocus = name_to_color("rgb:88/88/88");
1679 screens[i].bar_border = name_to_color("rgb:00/80/80");
1680 screens[i].bar_color = name_to_color("black");
1681 screens[i].bar_font_color = name_to_color("rgb:a0/a0/a0");
1683 /* init all workspaces */
1684 for (j = 0; j < SWM_WS_MAX; j++) {
1686 struct workspace *ws = &screens[i].ws[j];
1692 TAILQ_INIT(&ws->winlist);
1694 for (k = 0; layouts[k].l_stack != NULL; k++) {
1695 if (layouts[k].l_init != NULL)
1696 layouts[k].l_init(ws);
1698 ws->cur_layout = &layouts[0];
1701 /* map virtual screens onto physical screens */
1702 screens[i].xrandr_support = XRRQueryExtension(display,
1703 &xrandr_eventbase, &errorbase);
1704 if (screens[i].xrandr_support)
1705 if (XRRQueryVersion(display, &major, &minor) &&
1707 screens[i].xrandr_support = 0;
1709 #if 0 /* not ready for dynamic screen changes */
1710 if (screens[i].xrandr_support)
1711 XRRSelectInput(display,
1713 RRScreenChangeNotifyMask);
1716 /* grab existing windows (before we build the bars)*/
1717 if (!XQueryTree(display, screens[i].root,
1718 &d1, &d2, &wins, &num)) {
1719 if (wins) /* not sure if this is necessary */
1725 #ifdef SWM_XRR_HAS_CRTC
1726 res = XRRGetScreenResources (display, screens[i].root);
1729 new_region(&screens[i], &screens[i].ws[w],
1730 0, 0, DisplayWidth(display, i),
1731 DisplayHeight(display, i));
1735 for (c = 0; c < ncrtc; c++) {
1736 crtc_info = XRRGetCrtcInfo(display, res, res->crtcs[c]);
1737 if (crtc_info != NULL && crtc_info->mode == None)
1738 new_region(&screens[i], &screens[i].ws[w],
1739 0, 0, DisplayWidth(display, i),
1740 DisplayHeight(display, i));
1742 new_region(&screens[i], &screens[i].ws[w],
1743 crtc_info->x, crtc_info->y,
1744 crtc_info->width, crtc_info->height);
1748 new_region(&screens[i], &screens[i].ws[w],
1749 0, 0, DisplayWidth(display, i),
1750 DisplayHeight(display, i));
1751 #endif /* SWM_XRR_HAS_CRTC */
1753 /* attach windows to a region */
1754 /* normal windows */
1755 if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
1756 errx(1, "no regions on screen %d", i);
1758 for (i = 0; i < num; i++) {
1759 XGetWindowAttributes(display, wins[i], &wa);
1760 if (!XGetWindowAttributes(display, wins[i], &wa) ||
1761 wa.override_redirect ||
1762 XGetTransientForHint(display, wins[i], &d1))
1764 if (wa.map_state == IsViewable ||
1765 getstate(wins[i]) == NormalState)
1766 manage_window(wins[i], r->ws);
1768 /* transient windows */
1769 for (i = 0; i < num; i++) {
1770 if (!XGetWindowAttributes(display, wins[i], &wa))
1772 if (XGetTransientForHint(display, wins[i], &d1) &&
1773 (wa.map_state == IsViewable || getstate(wins[i]) ==
1775 manage_window(wins[i], r->ws);
1783 main(int argc, char *argv[])
1786 char conf[PATH_MAX], *cfile = NULL;
1791 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
1792 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
1793 warnx("no locale support");
1795 if (!(display = XOpenDisplay(0)))
1796 errx(1, "can not open display");
1799 errx(1, "other wm running");
1801 astate = XInternAtom(display, "WM_STATE", False);
1803 /* look for local and global conf file */
1804 pwd = getpwuid(getuid());
1806 errx(1, "invalid user %d", getuid());
1808 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
1809 if (stat(conf, &sb) != -1) {
1810 if (S_ISREG(sb.st_mode))
1813 /* try global conf file */
1814 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
1815 if (!stat(conf, &sb))
1816 if (S_ISREG(sb.st_mode))
1824 //ws[0].focus = TAILQ_FIRST(&ws[0].winlist);
1830 XNextEvent(display, &e);
1831 if (handler[e.type])
1832 handler[e.type](&e);
1835 XCloseDisplay(display);