3 * Copyright (c) 2009 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
5 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
6 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Much code and ideas taken from dwm under the following license:
22 * MIT/X Consortium License
24 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
25 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
26 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
27 * 2007 Premysl Hruby <dfenze at gmail dot com>
28 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
29 * 2007 Christof Musik <christof at sendfax dot de>
30 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
31 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
32 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
34 * Permission is hereby granted, free of charge, to any person obtaining a
35 * copy of this software and associated documentation files (the "Software"),
36 * to deal in the Software without restriction, including without limitation
37 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
38 * and/or sell copies of the Software, and to permit persons to whom the
39 * Software is furnished to do so, subject to the following conditions:
41 * The above copyright notice and this permission notice shall be included in
42 * all copies or substantial portions of the Software.
44 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
47 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
49 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
50 * DEALINGS IN THE SOFTWARE.
53 static const char *cvstag = "$scrotwm$";
55 #define SWM_VERSION "0.9.18"
71 #include <sys/types.h>
75 #include <sys/queue.h>
76 #include <sys/param.h>
77 #include <sys/select.h>
79 #include <X11/cursorfont.h>
80 #include <X11/keysym.h>
81 #include <X11/Xatom.h>
83 #include <X11/Xproto.h>
84 #include <X11/Xutil.h>
85 #include <X11/extensions/Xrandr.h>
88 # error XRandR versions less than 1.0 are not supported
93 #define SWM_XRR_HAS_CRTC
97 /* #define SWM_DEBUG */
99 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
100 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
101 #define SWM_D_MISC 0x0001
102 #define SWM_D_EVENT 0x0002
103 #define SWM_D_WS 0x0004
104 #define SWM_D_FOCUS 0x0008
105 #define SWM_D_MOVE 0x0010
106 #define SWM_D_STACK 0x0020
107 #define SWM_D_MOUSE 0x0040
108 #define SWM_D_PROP 0x0080
109 #define SWM_D_CLASS 0x0100
110 #define SWM_D_KEY 0x0200
111 #define SWM_D_QUIRK 0x0400
112 #define SWM_D_SPAWN 0x0800
113 #define SWM_D_EVENTQ 0x1000
114 #define SWM_D_CONF 0x2000
116 u_int32_t swm_debug = 0
133 #define DPRINTF(x...)
134 #define DNPRINTF(n,x...)
137 #define LENGTH(x) (sizeof x / sizeof x[0])
138 #define MODKEY Mod1Mask
139 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
140 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
141 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
142 #define SWM_PROPLEN (16)
143 #define SWM_FUNCNAME_LEN (32)
144 #define SWM_KEYS_LEN (255)
145 #define SWM_QUIRK_LEN (64)
146 #define X(r) (r)->g.x
147 #define Y(r) (r)->g.y
148 #define WIDTH(r) (r)->g.w
149 #define HEIGHT(r) (r)->g.h
150 #define SWM_MAX_FONT_STEPS (3)
151 #define WINID(w) (w ? w->id : 0)
154 #define SWM_LIB "/usr/local/lib/libswmhack.so"
162 volatile sig_atomic_t running = 1;
164 int (*xerrorxlib)(Display *, XErrorEvent *);
168 int xrandr_eventbase;
169 unsigned int numlockmask = 0;
173 int cycle_visible = 0;
175 int font_adjusted = 0;
176 unsigned int mod_key = MODKEY;
179 double dialog_ratio = .6;
181 #define SWM_BAR_MAX (256)
182 char *bar_argv[] = { NULL, NULL };
184 char bar_ext[SWM_BAR_MAX];
185 char bar_vertext[SWM_BAR_MAX];
187 sig_atomic_t bar_alarm = 0;
191 int bar_extra_running = 0;
194 int stack_enabled = 1;
195 int clock_enabled = 1;
196 int title_name_enabled = 0;
197 int title_class_enabled = 0;
203 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
204 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
206 #define SWM_MENU_FN (2)
207 #define SWM_MENU_NB (4)
208 #define SWM_MENU_NF (6)
209 #define SWM_MENU_SB (8)
210 #define SWM_MENU_SF (10)
212 /* layout manager data */
213 struct swm_geometry {
223 /* virtual "screens" */
225 TAILQ_ENTRY(swm_region) entry;
226 struct swm_geometry g;
227 struct workspace *ws; /* current workspace on this region */
228 struct swm_screen *s; /* screen idx */
231 TAILQ_HEAD(swm_region_list, swm_region);
234 TAILQ_ENTRY(ws_win) entry;
237 struct ws_win *child_trans; /* transient child window */
238 struct swm_geometry g;
241 int font_size_boundary[SWM_MAX_FONT_STEPS];
247 unsigned long quirks;
248 struct workspace *ws; /* always valid */
249 struct swm_screen *s; /* always valid, never changes */
250 XWindowAttributes wa;
254 TAILQ_HEAD(ws_win_list, ws_win);
256 /* user/key callable function IDs */
314 /* layout handlers */
316 void vertical_config(struct workspace *, int);
317 void vertical_stack(struct workspace *, struct swm_geometry *);
318 void horizontal_config(struct workspace *, int);
319 void horizontal_stack(struct workspace *, struct swm_geometry *);
320 void max_stack(struct workspace *, struct swm_geometry *);
322 struct ws_win *find_window(Window);
324 void grabbuttons(struct ws_win *, int);
325 void new_region(struct swm_screen *, int, int, int, int);
326 void unmanage_window(struct ws_win *);
327 long getstate(Window);
330 void (*l_stack)(struct workspace *, struct swm_geometry *);
331 void (*l_config)(struct workspace *, int);
333 #define SWM_L_FOCUSPREV (1<<0)
334 #define SWM_L_MAPONFOCUS (1<<1)
337 /* stack, configure */
338 { vertical_stack, vertical_config, 0, "[|]" },
339 { horizontal_stack, horizontal_config, 0, "[-]" },
341 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, "[ ]"},
342 { NULL, NULL, 0, NULL },
345 #define SWM_H_SLICE (32)
346 #define SWM_V_SLICE (32)
348 /* define work spaces */
350 int idx; /* workspace index */
351 struct layout *cur_layout; /* current layout handlers */
352 struct ws_win *focus; /* may be NULL */
353 struct ws_win *focus_prev; /* may be NULL */
354 struct swm_region *r; /* may be NULL */
355 struct swm_region *old_r; /* may be NULL */
356 struct ws_win_list winlist; /* list of windows in ws */
357 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
361 int horizontal_msize;
363 int horizontal_stacks;
370 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
371 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
373 /* physical screen mapping */
374 #define SWM_WS_MAX (10)
376 int idx; /* screen index */
377 struct swm_region_list rl; /* list of regions on this screen */
378 struct swm_region_list orl; /* list of old regions */
380 struct workspace ws[SWM_WS_MAX];
386 } c[SWM_S_COLOR_MAX];
388 struct swm_screen *screens;
391 /* args to functions */
394 #define SWM_ARG_ID_FOCUSNEXT (0)
395 #define SWM_ARG_ID_FOCUSPREV (1)
396 #define SWM_ARG_ID_FOCUSMAIN (2)
397 #define SWM_ARG_ID_FOCUSCUR (4)
398 #define SWM_ARG_ID_SWAPNEXT (10)
399 #define SWM_ARG_ID_SWAPPREV (11)
400 #define SWM_ARG_ID_SWAPMAIN (12)
401 #define SWM_ARG_ID_MASTERSHRINK (20)
402 #define SWM_ARG_ID_MASTERGROW (21)
403 #define SWM_ARG_ID_MASTERADD (22)
404 #define SWM_ARG_ID_MASTERDEL (23)
405 #define SWM_ARG_ID_STACKRESET (30)
406 #define SWM_ARG_ID_STACKINIT (31)
407 #define SWM_ARG_ID_CYCLEWS_UP (40)
408 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
409 #define SWM_ARG_ID_CYCLESC_UP (42)
410 #define SWM_ARG_ID_CYCLESC_DOWN (43)
411 #define SWM_ARG_ID_STACKINC (50)
412 #define SWM_ARG_ID_STACKDEC (51)
413 #define SWM_ARG_ID_SS_ALL (60)
414 #define SWM_ARG_ID_SS_WINDOW (61)
415 #define SWM_ARG_ID_DONTCENTER (70)
416 #define SWM_ARG_ID_CENTER (71)
417 #define SWM_ARG_ID_KILLWINDOW (80)
418 #define SWM_ARG_ID_DELETEWINDOW (81)
422 void focus(struct swm_region *, union arg *);
423 void focus_magic(struct ws_win *, int);
424 #define SWM_F_GENERIC (0)
425 #define SWM_F_TRANSIENT (1)
431 #define SWM_Q_FLOAT (1<<0) /* float this window */
432 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
433 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
434 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
435 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
437 int quirks_size = 0, quirks_length = 0;
438 struct quirk *quirks = NULL;
455 name = "ButtonPress";
458 name = "ButtonRelease";
461 name = "MotionNotify";
464 name = "EnterNotify";
467 name = "LeaveNotify";
476 name = "KeymapNotify";
482 name = "GraphicsExpose";
487 case VisibilityNotify:
488 name = "VisibilityNotify";
491 name = "CreateNotify";
494 name = "DestroyNotify";
497 name = "UnmapNotify";
506 name = "ReparentNotify";
508 case ConfigureNotify:
509 name = "ConfigureNotify";
511 case ConfigureRequest:
512 name = "ConfigureRequest";
515 name = "GravityNotify";
518 name = "ResizeRequest";
520 case CirculateNotify:
521 name = "CirculateNotify";
523 case CirculateRequest:
524 name = "CirculateRequest";
527 name = "PropertyNotify";
530 name = "SelectionClear";
532 case SelectionRequest:
533 name = "SelectionRequest";
535 case SelectionNotify:
536 name = "SelectionNotify";
539 name = "ColormapNotify";
542 name = "ClientMessage";
545 name = "MappingNotify";
550 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
552 e->xany.window, name, e->type, QLength(display));
554 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
556 e->xany.window, e->type, QLength(display));
560 dumpwins(struct swm_region *r, union arg *args)
564 XWindowAttributes wa;
567 fprintf(stderr, "invalid workspace\n");
571 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
573 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
574 state = getstate(win->id);
575 if (!XGetWindowAttributes(display, win->id, &wa))
576 fprintf(stderr, "window: %lu failed "
577 "XGetWindowAttributes\n", win->id);
578 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
579 win->id, wa.map_state, state);
582 fprintf(stderr, "===== unmanaged window list =====\n");
583 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
584 state = getstate(win->id);
585 if (!XGetWindowAttributes(display, win->id, &wa))
586 fprintf(stderr, "window: %lu failed "
587 "XGetWindowAttributes\n", win->id);
588 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
589 win->id, wa.map_state, state);
592 fprintf(stderr, "=================================\n");
597 dumpwins(struct swm_region *r, union arg *args)
600 #endif /* SWM_DEBUG */
602 void expose(XEvent *);
603 void keypress(XEvent *);
604 void buttonpress(XEvent *);
605 void configurerequest(XEvent *);
606 void configurenotify(XEvent *);
607 void destroynotify(XEvent *);
608 void enternotify(XEvent *);
609 void focusin(XEvent *);
610 void focusout(XEvent *);
611 void mapnotify(XEvent *);
612 void mappingnotify(XEvent *);
613 void maprequest(XEvent *);
614 void propertynotify(XEvent *);
615 void unmapnotify(XEvent *);
616 void visibilitynotify(XEvent *);
618 void (*handler[LASTEvent])(XEvent *) = {
620 [KeyPress] = keypress,
621 [ButtonPress] = buttonpress,
622 [ConfigureRequest] = configurerequest,
623 [ConfigureNotify] = configurenotify,
624 [DestroyNotify] = destroynotify,
625 [EnterNotify] = enternotify,
627 [FocusOut] = focusout,
628 [MapNotify] = mapnotify,
629 [MappingNotify] = mappingnotify,
630 [MapRequest] = maprequest,
631 [PropertyNotify] = propertynotify,
632 [UnmapNotify] = unmapnotify,
633 [VisibilityNotify] = visibilitynotify,
643 while ((pid = waitpid(WAIT_ANY, NULL, WNOHANG)) != -1) {
644 DNPRINTF(SWM_D_MISC, "reaping: %d\n", pid);
659 installsignal(int sig, char *name)
663 sa.sa_handler = sighdlr;
664 sigemptyset(&sa.sa_mask);
666 if (sigaction(sig, &sa, NULL) == -1)
667 err(1, "could not install %s handler", name);
671 name_to_color(char *colorname)
675 XColor screen_def, exact_def;
676 unsigned long result = 0;
677 char cname[32] = "#";
679 cmap = DefaultColormap(display, screens[0].idx);
680 status = XAllocNamedColor(display, cmap, colorname,
681 &screen_def, &exact_def);
683 strlcat(cname, colorname + 2, sizeof cname - 1);
684 status = XAllocNamedColor(display, cmap, cname, &screen_def,
688 result = screen_def.pixel;
690 fprintf(stderr, "color '%s' not found.\n", colorname);
696 setscreencolor(char *val, int i, int c)
698 if (i > 0 && i <= ScreenCount(display)) {
699 screens[i - 1].c[c].color = name_to_color(val);
700 free(screens[i - 1].c[c].name);
701 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
703 } else if (i == -1) {
704 for (i = 0; i < ScreenCount(display); i++) {
705 screens[i].c[c].color = name_to_color(val);
706 free(screens[i].c[c].name);
707 if ((screens[i].c[c].name = strdup(val)) == NULL)
711 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
712 i, ScreenCount(display));
716 custom_region(char *val)
718 unsigned int sidx, x, y, w, h;
720 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
721 errx(1, "invalid custom region, "
722 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
723 if (sidx < 1 || sidx > ScreenCount(display))
724 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
725 sidx, ScreenCount(display));
729 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
731 if (x < 0 || x > DisplayWidth(display, sidx) ||
732 y < 0 || y > DisplayHeight(display, sidx) ||
733 w + x > DisplayWidth(display, sidx) ||
734 h + y > DisplayHeight(display, sidx))
735 errx(1, "region %ux%u+%u+%u not within screen boundaries "
736 "(%ux%u)\n", w, h, x, y,
737 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
739 new_region(&screens[sidx], x, y, w, h);
743 socket_setnonblock(int fd)
747 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
748 err(1, "fcntl F_GETFL");
750 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
751 err(1, "fcntl F_SETFL");
755 bar_print(struct swm_region *r, char *s)
757 XClearWindow(display, r->bar_window);
758 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
759 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
768 bzero(bar_pipe, sizeof bar_pipe);
771 kill(bar_pid, SIGTERM);
774 strlcpy(bar_ext, "", sizeof bar_ext);
779 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
781 int do_class, do_name;
783 XClassHint *xch = NULL;
785 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
787 if ((xch = XAllocClassHint()) == NULL)
789 status = XGetClassHint(display, cur_focus->id, xch);
790 if (status == BadWindow || status == BadAlloc)
792 do_class = (title_class_enabled && xch->res_class != NULL);
793 do_name = (title_name_enabled && xch->res_name != NULL);
795 strlcat(s, xch->res_class, sz);
796 if (do_class && do_name)
799 strlcat(s, xch->res_name, sz);
812 struct swm_region *r;
816 char loc[SWM_BAR_MAX];
820 if (bar_enabled == 0)
822 if (bar_extra && bar_extra_running) {
823 /* ignore short reads; it'll correct itself */
824 while ((b = fgetln(stdin, &len)) != NULL)
825 if (b && b[len - 1] == '\n') {
827 strlcpy(bar_ext, b, sizeof bar_ext);
829 if (b == NULL && errno != EAGAIN) {
830 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
831 errno, strerror(errno));
835 strlcpy(bar_ext, "", sizeof bar_ext);
837 if (clock_enabled == 0)
838 strlcpy(s, "", sizeof s);
841 localtime_r(&tmt, &tm);
842 strftime(s, sizeof s, "%a %b %d %R %Z %Y ", &tm);
845 for (i = 0; i < ScreenCount(display); i++) {
847 TAILQ_FOREACH(r, &screens[i].rl, entry) {
849 bar_class_name(s, sizeof s, r->ws->focus);
852 stack = r->ws->cur_layout->name;
854 snprintf(loc, sizeof loc, "%d:%d %s %s %s %s",
855 x++, r->ws->idx + 1, stack, s, bar_ext,
870 bar_toggle(struct swm_region *r, union arg *args)
872 struct swm_region *tmpr;
873 int i, sc = ScreenCount(display);
875 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
878 for (i = 0; i < sc; i++)
879 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
880 XUnmapWindow(display, tmpr->bar_window);
882 for (i = 0; i < sc; i++)
883 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
884 XMapRaised(display, tmpr->bar_window);
886 bar_enabled = !bar_enabled;
889 /* must be after stack */
896 XSetWindowAttributes wa;
897 struct swm_region *r;
900 /* do this here because the conf file is in memory */
901 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
902 /* launch external status app */
903 bar_extra_running = 1;
904 if (pipe(bar_pipe) == -1)
905 err(1, "pipe error");
906 socket_setnonblock(bar_pipe[0]);
907 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
908 if (dup2(bar_pipe[0], 0) == -1)
910 if (dup2(bar_pipe[1], 1) == -1)
912 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
913 err(1, "could not disable SIGPIPE");
914 switch (bar_pid = fork()) {
916 err(1, "cannot fork");
920 execvp(bar_argv[0], bar_argv);
921 err(1, "%s external app failed", bar_argv[0]);
923 default: /* parent */
929 bzero(&wa, sizeof wa);
930 for (i = 0; i < ScreenCount(display); i++)
931 TAILQ_FOREACH(r, &screens[i].rl, entry) {
933 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
934 wa.background_pixel =
935 screens[i].c[SWM_S_COLOR_BAR].color;
936 XChangeWindowAttributes(display, r->bar_window,
937 CWBackPixel | CWBorderPixel, &wa);
943 bar_setup(struct swm_region *r)
947 for (i = 0; bar_fonts[i] != NULL; i++) {
948 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
954 if (bar_fonts[i] == NULL)
955 errx(1, "couldn't load font");
956 bar_height = bar_fs->ascent + bar_fs->descent + 3;
958 r->bar_window = XCreateSimpleWindow(display,
959 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
960 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
961 r->s->c[SWM_S_COLOR_BAR].color);
962 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
963 XSetFont(display, bar_gc, bar_fs->fid);
964 XSelectInput(display, r->bar_window, VisibilityChangeMask);
966 XMapRaised(display, r->bar_window);
967 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
969 if (signal(SIGALRM, bar_signal) == SIG_ERR)
970 err(1, "could not install bar_signal");
975 set_win_state(struct ws_win *win, long state)
977 long data[] = {state, None};
979 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
984 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
985 (unsigned char *)data, 2);
993 unsigned char *p = NULL;
994 unsigned long n, extra;
997 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
998 &real, &format, &n, &extra, (unsigned char **)&p);
999 if (status != Success)
1002 result = *((long *)p);
1008 version(struct swm_region *r, union arg *args)
1010 bar_version = !bar_version;
1012 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
1013 SWM_VERSION, cvstag);
1015 strlcpy(bar_vertext, "", sizeof bar_vertext);
1020 client_msg(struct ws_win *win, Atom a)
1022 XClientMessageEvent cm;
1027 bzero(&cm, sizeof cm);
1028 cm.type = ClientMessage;
1029 cm.window = win->id;
1030 cm.message_type = aprot;
1033 cm.data.l[1] = CurrentTime;
1034 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1038 configreq_win(struct ws_win *win)
1040 XConfigureRequestEvent cr;
1045 bzero(&cr, sizeof cr);
1046 cr.type = ConfigureRequest;
1047 cr.display = display;
1048 cr.parent = win->id;
1049 cr.window = win->id;
1052 cr.width = win->g.w;
1053 cr.height = win->g.h;
1054 cr.border_width = 1;
1056 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&cr);
1060 config_win(struct ws_win *win)
1064 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
1065 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1070 ce.type = ConfigureNotify;
1071 ce.display = display;
1073 ce.window = win->id;
1076 ce.width = win->g.w;
1077 ce.height = win->g.h;
1078 ce.border_width = 1; /* XXX store this! */
1080 ce.override_redirect = False;
1081 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1085 count_win(struct workspace *ws, int count_transient)
1090 TAILQ_FOREACH(win, &ws->winlist, entry) {
1091 if (count_transient == 0 && win->floating)
1093 if (count_transient == 0 && win->transient)
1097 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1103 quit(struct swm_region *r, union arg *args)
1105 DNPRINTF(SWM_D_MISC, "quit\n");
1110 unmap_window(struct ws_win *win)
1115 /* don't unmap again */
1116 if (getstate(win->id) == IconicState)
1119 set_win_state(win, IconicState);
1121 XUnmapWindow(display, win->id);
1130 for (i = 0; i < ScreenCount(display); i++)
1131 for (j = 0; j < SWM_WS_MAX; j++)
1132 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1137 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1144 event.display = display; /* Ignored, but what the hell */
1145 event.window = win->id;
1146 event.root = win->s->root;
1147 event.subwindow = None;
1148 event.time = CurrentTime;
1153 event.same_screen = True;
1154 event.keycode = XKeysymToKeycode(display, keysym);
1155 event.state = modifiers;
1157 event.type = KeyPress;
1158 XSendEvent(event.display, event.window, True,
1159 KeyPressMask, (XEvent *)&event);
1161 event.type = KeyRelease;
1162 XSendEvent(event.display, event.window, True,
1163 KeyPressMask, (XEvent *)&event);
1168 restart(struct swm_region *r, union arg *args)
1170 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1172 /* disable alarm because the following code may not be interrupted */
1174 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1175 errx(1, "can't disable alarm");
1180 XCloseDisplay(display);
1181 execvp(start_argv[0], start_argv);
1182 fprintf(stderr, "execvp failed\n");
1188 root_to_region(Window root)
1190 struct swm_region *r = NULL;
1192 int i, x, y, wx, wy;
1195 for (i = 0; i < ScreenCount(display); i++)
1196 if (screens[i].root == root)
1199 if (XQueryPointer(display, screens[i].root,
1200 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1201 /* choose a region based on pointer location */
1202 TAILQ_FOREACH(r, &screens[i].rl, entry)
1203 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1204 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1209 r = TAILQ_FIRST(&screens[i].rl);
1215 find_unmanaged_window(Window id)
1220 for (i = 0; i < ScreenCount(display); i++)
1221 for (j = 0; j < SWM_WS_MAX; j++)
1222 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1230 find_window(Window id)
1235 for (i = 0; i < ScreenCount(display); i++)
1236 for (j = 0; j < SWM_WS_MAX; j++)
1237 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1244 spawn(struct swm_region *r, union arg *args)
1249 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1251 * The double-fork construct avoids zombie processes and keeps the code
1252 * clean from stupid signal handlers.
1257 close(ConnectionNumber(display));
1258 setenv("LD_PRELOAD", SWM_LIB, 1);
1259 if (asprintf(&ret, "%d", r->ws->idx)) {
1260 setenv("_SWM_WS", ret, 1);
1263 if (asprintf(&ret, "%d", getpid())) {
1264 setenv("_SWM_PID", ret, 1);
1268 /* kill stdin, mplayer, ssh-add etc. need that */
1269 si = open("/dev/null", O_RDONLY, 0);
1271 err(1, "open /dev/null");
1272 if (dup2(si, 0) == -1)
1273 err(1, "dup2 /dev/null");
1274 execvp(args->argv[0], args->argv);
1275 fprintf(stderr, "execvp failed\n");
1283 spawnterm(struct swm_region *r, union arg *args)
1285 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1288 setenv("_SWM_XTERM_FONTADJ", "", 1);
1293 kill_refs(struct ws_win *win)
1296 struct swm_region *r;
1297 struct workspace *ws;
1302 for (i = 0; i < ScreenCount(display); i++)
1303 TAILQ_FOREACH(r, &screens[i].rl, entry)
1304 for (x = 0; x < SWM_WS_MAX; x++) {
1306 if (win == ws->focus)
1308 if (win == ws->focus_prev)
1309 ws->focus_prev = NULL;
1314 validate_win(struct ws_win *testwin)
1317 struct workspace *ws;
1318 struct swm_region *r;
1319 int i, x, foundit = 0;
1321 if (testwin == NULL)
1324 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1325 TAILQ_FOREACH(r, &screens[i].rl, entry)
1326 for (x = 0; x < SWM_WS_MAX; x++) {
1328 TAILQ_FOREACH(win, &ws->winlist, entry)
1336 validate_ws(struct workspace *testws)
1338 struct swm_region *r;
1339 struct workspace *ws;
1342 /* validate all ws */
1343 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1344 TAILQ_FOREACH(r, &screens[i].rl, entry)
1345 for (x = 0; x < SWM_WS_MAX; x++) {
1354 unfocus_win(struct ws_win *win)
1358 if (win->ws == NULL)
1361 if (validate_ws(win->ws))
1364 if (win->ws->r == NULL)
1367 if (validate_win(win)) {
1372 if (win->ws->focus == win) {
1373 win->ws->focus = NULL;
1374 win->ws->focus_prev = win;
1377 if (validate_win(win->ws->focus)) {
1378 kill_refs(win->ws->focus);
1379 win->ws->focus = NULL;
1381 if (validate_win(win->ws->focus_prev)) {
1382 kill_refs(win->ws->focus_prev);
1383 win->ws->focus_prev = NULL;
1386 grabbuttons(win, 0);
1387 XSetWindowBorder(display, win->id,
1388 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1392 unfocus_all_except(struct ws_win *except)
1397 DNPRINTF(SWM_D_FOCUS, "unfocus_all_except(%d):\n", except->id);
1399 for (i = 0; i < ScreenCount(display); i++)
1400 for (j = 0; j < SWM_WS_MAX; j++)
1401 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1407 focus_win(struct ws_win *win)
1409 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1413 if (win->ws == NULL)
1416 if (validate_ws(win->ws))
1418 if (validate_win(win)) {
1423 /* use big hammer to make sure it works under all use cases */
1424 unfocus_all_except(win);
1426 if (validate_win(win)) {
1431 win->ws->focus = win;
1433 if (win->ws->r != NULL) {
1434 grabbuttons(win, 1);
1435 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS)
1436 XMapRaised(display, win->id);
1437 XSetWindowBorder(display, win->id,
1438 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1440 XSetInputFocus(display, win->id,
1441 RevertToPointerRoot, CurrentTime);
1446 switchws(struct swm_region *r, union arg *args)
1448 int wsid = args->id;
1449 struct swm_region *this_r, *other_r;
1451 struct workspace *new_ws, *old_ws;
1458 old_ws = this_r->ws;
1459 new_ws = &this_r->s->ws[wsid];
1461 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1462 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1465 if (new_ws == NULL || old_ws == NULL)
1467 if (new_ws == old_ws)
1470 other_r = new_ws->r;
1471 if (other_r == NULL) {
1472 /* if the other workspace is hidden, switch windows */
1473 if (old_ws->r != NULL)
1474 old_ws->old_r = old_ws->r;
1477 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1480 other_r->ws = old_ws;
1481 old_ws->r = other_r;
1483 this_r->ws = new_ws;
1487 a.id = SWM_ARG_ID_FOCUSCUR;
1488 focus(new_ws->r, &a);
1493 cyclews(struct swm_region *r, union arg *args)
1496 struct swm_screen *s = r->s;
1498 DNPRINTF(SWM_D_WS, "cyclews id %d "
1499 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1500 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1505 case SWM_ARG_ID_CYCLEWS_UP:
1506 if (a.id < SWM_WS_MAX - 1)
1511 case SWM_ARG_ID_CYCLEWS_DOWN:
1515 a.id = SWM_WS_MAX - 1;
1521 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1523 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1527 } while (a.id != r->ws->idx);
1531 cyclescr(struct swm_region *r, union arg *args)
1533 struct swm_region *rr = NULL;
1537 /* do nothing if we don't have more than one screen */
1538 if (!(ScreenCount(display) > 1 || outputs > 1))
1543 case SWM_ARG_ID_CYCLESC_UP:
1544 rr = TAILQ_NEXT(r, entry);
1546 rr = TAILQ_FIRST(&screens[i].rl);
1548 case SWM_ARG_ID_CYCLESC_DOWN:
1549 rr = TAILQ_PREV(r, swm_region_list, entry);
1551 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1559 /* move mouse to region */
1561 y = rr->g.y + 1 + bar_enabled ? bar_height : 0;
1562 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1564 a.id = SWM_ARG_ID_FOCUSCUR;
1567 if (rr->ws->focus) {
1568 /* move to focus window */
1569 x = rr->ws->focus->g.x + 1;
1570 y = rr->ws->focus->g.y + 1;
1571 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1576 swapwin(struct swm_region *r, union arg *args)
1578 struct ws_win *target, *source;
1579 struct ws_win *cur_focus;
1580 struct ws_win_list *wl;
1583 DNPRINTF(SWM_D_WS, "swapwin id %d "
1584 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1585 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1587 cur_focus = r->ws->focus;
1588 if (cur_focus == NULL)
1592 wl = &source->ws->winlist;
1595 case SWM_ARG_ID_SWAPPREV:
1596 target = TAILQ_PREV(source, ws_win_list, entry);
1597 TAILQ_REMOVE(wl, cur_focus, entry);
1599 TAILQ_INSERT_TAIL(wl, source, entry);
1601 TAILQ_INSERT_BEFORE(target, source, entry);
1603 case SWM_ARG_ID_SWAPNEXT:
1604 target = TAILQ_NEXT(source, entry);
1605 TAILQ_REMOVE(wl, source, entry);
1607 TAILQ_INSERT_HEAD(wl, source, entry);
1609 TAILQ_INSERT_AFTER(wl, target, source, entry);
1611 case SWM_ARG_ID_SWAPMAIN:
1612 target = TAILQ_FIRST(wl);
1613 if (target == source) {
1614 if (source->ws->focus_prev != NULL &&
1615 source->ws->focus_prev != target)
1617 source = source->ws->focus_prev;
1621 if (target == NULL || source == NULL)
1623 source->ws->focus_prev = target;
1624 TAILQ_REMOVE(wl, target, entry);
1625 TAILQ_INSERT_BEFORE(source, target, entry);
1626 TAILQ_REMOVE(wl, source, entry);
1627 TAILQ_INSERT_HEAD(wl, source, entry);
1630 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1638 focus_prev(struct ws_win *win)
1640 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
1641 struct ws_win *cur_focus = NULL;
1642 struct ws_win_list *wl = NULL;
1643 struct workspace *ws = NULL;
1645 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
1647 if (!(win && win->ws))
1652 cur_focus = ws->focus;
1653 winlostfocus = cur_focus;
1655 /* pickle, just focus on whatever */
1656 if (cur_focus == NULL) {
1657 /* use prev_focus if valid */
1658 if (ws->focus_prev && ws->focus_prev != cur_focus &&
1659 find_window(WINID(ws->focus_prev)))
1660 winfocus = ws->focus_prev;
1661 if (winfocus == NULL)
1662 winfocus = TAILQ_FIRST(wl);
1666 /* if transient focus on parent */
1667 if (cur_focus->transient) {
1668 winfocus = find_window(cur_focus->transient);
1672 /* if in max_stack try harder */
1673 /* XXX needs more love */
1674 if (ws->cur_layout->flags & SWM_L_FOCUSPREV) {
1675 if (cur_focus != ws->focus_prev)
1676 winfocus = ws->focus_prev;
1677 else if (cur_focus != ws->focus)
1678 winfocus = ws->focus;
1682 if (cur_focus == win)
1683 winfocus = TAILQ_PREV(win, ws_win_list, entry);
1684 if (winfocus == NULL)
1685 winfocus = TAILQ_FIRST(wl);
1686 if (winfocus == NULL || winfocus == win)
1687 winfocus = TAILQ_NEXT(cur_focus, entry);
1689 if (winfocus == winlostfocus || winfocus == NULL)
1691 focus_magic(winfocus, SWM_F_GENERIC);
1695 focus(struct swm_region *r, union arg *args)
1697 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
1698 struct ws_win *cur_focus = NULL;
1699 struct ws_win_list *wl = NULL;
1700 struct workspace *ws = NULL;
1705 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1707 /* treat FOCUS_CUR special */
1708 if (args->id == SWM_ARG_ID_FOCUSCUR) {
1710 winfocus = r->ws->focus;
1711 else if (r->ws->focus_prev)
1712 winfocus = r->ws->focus_prev;
1714 winfocus = TAILQ_FIRST(&r->ws->winlist);
1716 focus_magic(winfocus, SWM_F_GENERIC);
1720 if ((cur_focus = r->ws->focus) == NULL)
1725 winlostfocus = cur_focus;
1728 case SWM_ARG_ID_FOCUSPREV:
1729 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1730 if (winfocus == NULL)
1731 winfocus = TAILQ_LAST(wl, ws_win_list);
1734 case SWM_ARG_ID_FOCUSNEXT:
1735 winfocus = TAILQ_NEXT(cur_focus, entry);
1736 if (winfocus == NULL)
1737 winfocus = TAILQ_FIRST(wl);
1740 case SWM_ARG_ID_FOCUSMAIN:
1741 winfocus = TAILQ_FIRST(wl);
1742 if (winfocus == cur_focus)
1743 winfocus = cur_focus->ws->focus_prev;
1750 if (winfocus == winlostfocus || winfocus == NULL)
1753 focus_magic(winfocus, SWM_F_GENERIC);
1757 cycle_layout(struct swm_region *r, union arg *args)
1759 struct workspace *ws = r->ws;
1760 struct ws_win *winfocus;
1763 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1765 winfocus = ws->focus;
1768 if (ws->cur_layout->l_stack == NULL)
1769 ws->cur_layout = &layouts[0];
1772 a.id = SWM_ARG_ID_FOCUSCUR;
1778 stack_config(struct swm_region *r, union arg *args)
1780 struct workspace *ws = r->ws;
1782 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1785 if (ws->cur_layout->l_config != NULL)
1786 ws->cur_layout->l_config(ws, args->id);
1788 if (args->id != SWM_ARG_ID_STACKINIT);
1794 struct swm_geometry g;
1795 struct swm_region *r;
1798 DNPRINTF(SWM_D_STACK, "stack\n");
1800 for (i = 0; i < ScreenCount(display); i++) {
1802 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1803 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1804 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1806 /* start with screen geometry, adjust for bar */
1814 r->ws->cur_layout->l_stack(r->ws, &g);
1822 stack_floater(struct ws_win *win, struct swm_region *r)
1830 bzero(&wc, sizeof wc);
1831 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1832 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
1833 (win->g.h >= HEIGHT(r)))
1834 wc.border_width = 0;
1836 wc.border_width = 1;
1837 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
1838 win->g.w = (double)WIDTH(r) * dialog_ratio;
1839 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1841 wc.width = win->g.w;
1842 wc.height = win->g.h;
1847 wc.x = (WIDTH(r) - win->g.w) / 2;
1848 wc.y = (HEIGHT(r) - win->g.h) / 2;
1851 /* adjust for region */
1859 win->g.w = wc.width;
1860 win->g.h = wc.height;
1862 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
1863 win->id, wc.x, wc.y, wc.width, wc.height);
1865 XConfigureWindow(display, win->id, mask, &wc);
1870 * Send keystrokes to terminal to decrease/increase the font size as the
1871 * window size changes.
1874 adjust_font(struct ws_win *win)
1876 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
1877 win->floating || win->transient)
1880 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
1881 win->g.w / win->sh.width_inc < term_width &&
1882 win->font_steps < SWM_MAX_FONT_STEPS) {
1883 win->font_size_boundary[win->font_steps] =
1884 (win->sh.width_inc * term_width) + win->sh.base_width;
1887 win->last_inc = win->sh.width_inc;
1888 fake_keypress(win, XK_KP_Subtract, ShiftMask);
1889 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
1890 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
1893 win->last_inc = win->sh.width_inc;
1894 fake_keypress(win, XK_KP_Add, ShiftMask);
1898 #define SWAPXY(g) do { \
1900 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1901 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1904 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1907 struct swm_geometry win_g, r_g = *g;
1909 int i, j, s, stacks;
1910 int w_inc = 1, h_inc, w_base = 1, h_base;
1911 int hrh, extra = 0, h_slice, last_h = 0;
1912 int split, colno, winno, mwin, msize, mscale;
1913 int remain, missing, v_slice, reconfigure;
1916 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1917 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1919 winno = count_win(ws, 0);
1920 if (winno == 0 && count_win(ws, 1) == 0)
1923 TAILQ_FOREACH(win, &ws->winlist, entry)
1924 if (win->transient == 0 && win->floating == 0)
1931 w_inc = win->sh.width_inc;
1932 w_base = win->sh.base_width;
1933 mwin = ws->l_state.horizontal_mwin;
1934 mscale = ws->l_state.horizontal_msize;
1935 stacks = ws->l_state.horizontal_stacks;
1938 w_inc = win->sh.height_inc;
1939 w_base = win->sh.base_height;
1940 mwin = ws->l_state.vertical_mwin;
1941 mscale = ws->l_state.vertical_msize;
1942 stacks = ws->l_state.vertical_stacks;
1946 if (stacks > winno - mwin)
1947 stacks = winno - mwin;
1951 h_slice = r_g.h / SWM_H_SLICE;
1952 if (mwin && winno > mwin) {
1953 v_slice = r_g.w / SWM_V_SLICE;
1957 win_g.w = v_slice * mscale;
1959 if (w_inc > 1 && w_inc < v_slice) {
1960 /* adjust for window's requested size increment */
1961 remain = (win_g.w - w_base) % w_inc;
1962 missing = w_inc - remain;
1969 win_g.x += r_g.w - msize;
1972 colno = split = winno / stacks;
1973 win_g.w = ((r_g.w - (stacks * 2) + 2) / stacks);
1975 hrh = r_g.h / colno;
1976 extra = r_g.h - (colno * hrh);
1979 /* stack all the tiled windows */
1980 i = j = 0, s = stacks;
1981 TAILQ_FOREACH(win, &ws->winlist, entry) {
1982 if (win->transient != 0 || win->floating != 0)
1985 if (split && i == split) {
1986 colno = (winno - mwin) / stacks;
1987 if (s <= (winno - mwin) % stacks)
1989 split = split + colno;
1990 hrh = (r_g.h / colno);
1991 extra = r_g.h - (colno * hrh);
1995 win_g.x += win_g.w + 2;
1996 win_g.w = (r_g.w - msize - (stacks * 2)) / stacks;
1998 win_g.w += (r_g.w - msize - (stacks * 2)) %
2005 h_inc = win->sh.width_inc;
2006 h_base = win->sh.base_width;
2008 h_inc = win->sh.height_inc;
2009 h_base = win->sh.base_height;
2011 if (j == colno - 1) {
2012 win_g.h = hrh + extra;
2013 } else if (h_inc > 1 && h_inc < h_slice) {
2014 /* adjust for window's requested size increment */
2015 remain = (win_g.h - h_base) % h_inc;
2016 missing = h_inc - remain;
2018 if (missing <= extra || j == 0) {
2030 win_g.y += last_h + 2;
2032 bzero(&wc, sizeof wc);
2033 wc.border_width = 1;
2036 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2037 win->g.w != win_g.h || win->g.h != win_g.w) {
2039 win->g.x = wc.x = win_g.y;
2040 win->g.y = wc.y = win_g.x;
2041 win->g.w = wc.width = win_g.h;
2042 win->g.h = wc.height = win_g.w;
2045 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2046 win->g.w != win_g.w || win->g.h != win_g.h) {
2048 win->g.x = wc.x = win_g.x;
2049 win->g.y = wc.y = win_g.y;
2050 win->g.w = wc.width = win_g.w;
2051 win->g.h = wc.height = win_g.h;
2056 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2057 XConfigureWindow(display, win->id, mask, &wc);
2060 XMapRaised(display, win->id);
2068 /* now, stack all the floaters and transients */
2069 TAILQ_FOREACH(win, &ws->winlist, entry) {
2070 if (win->transient == 0 && win->floating == 0)
2073 stack_floater(win, ws->r);
2074 XMapRaised(display, win->id);
2079 vertical_config(struct workspace *ws, int id)
2081 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2084 case SWM_ARG_ID_STACKRESET:
2085 case SWM_ARG_ID_STACKINIT:
2086 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2087 ws->l_state.vertical_mwin = 1;
2088 ws->l_state.vertical_stacks = 1;
2090 case SWM_ARG_ID_MASTERSHRINK:
2091 if (ws->l_state.vertical_msize > 1)
2092 ws->l_state.vertical_msize--;
2094 case SWM_ARG_ID_MASTERGROW:
2095 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2096 ws->l_state.vertical_msize++;
2098 case SWM_ARG_ID_MASTERADD:
2099 ws->l_state.vertical_mwin++;
2101 case SWM_ARG_ID_MASTERDEL:
2102 if (ws->l_state.vertical_mwin > 0)
2103 ws->l_state.vertical_mwin--;
2105 case SWM_ARG_ID_STACKINC:
2106 ws->l_state.vertical_stacks++;
2108 case SWM_ARG_ID_STACKDEC:
2109 if (ws->l_state.vertical_stacks > 1)
2110 ws->l_state.vertical_stacks--;
2118 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2120 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2122 stack_master(ws, g, 0, 0);
2126 horizontal_config(struct workspace *ws, int id)
2128 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2131 case SWM_ARG_ID_STACKRESET:
2132 case SWM_ARG_ID_STACKINIT:
2133 ws->l_state.horizontal_mwin = 1;
2134 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2135 ws->l_state.horizontal_stacks = 1;
2137 case SWM_ARG_ID_MASTERSHRINK:
2138 if (ws->l_state.horizontal_msize > 1)
2139 ws->l_state.horizontal_msize--;
2141 case SWM_ARG_ID_MASTERGROW:
2142 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2143 ws->l_state.horizontal_msize++;
2145 case SWM_ARG_ID_MASTERADD:
2146 ws->l_state.horizontal_mwin++;
2148 case SWM_ARG_ID_MASTERDEL:
2149 if (ws->l_state.horizontal_mwin > 0)
2150 ws->l_state.horizontal_mwin--;
2152 case SWM_ARG_ID_STACKINC:
2153 ws->l_state.horizontal_stacks++;
2155 case SWM_ARG_ID_STACKDEC:
2156 if (ws->l_state.horizontal_stacks > 1)
2157 ws->l_state.horizontal_stacks--;
2165 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2167 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2169 stack_master(ws, g, 1, 0);
2172 /* fullscreen view */
2174 max_stack(struct workspace *ws, struct swm_geometry *g)
2177 struct swm_geometry gg = *g;
2178 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2182 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2187 winno = count_win(ws, 0);
2188 if (winno == 0 && count_win(ws, 1) == 0)
2191 TAILQ_FOREACH(win, &ws->winlist, entry) {
2192 if (win->transient) {
2194 parent = find_window(win->transient);
2198 /* only reconfigure if necessary */
2199 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2201 bzero(&wc, sizeof wc);
2202 wc.border_width = 1;
2203 win->g.x = wc.x = gg.x;
2204 win->g.y = wc.y = gg.y;
2205 win->g.w = wc.width = gg.w;
2206 win->g.h = wc.height = gg.h;
2207 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2208 XConfigureWindow(display, win->id, mask, &wc);
2211 /* unmap only if we don't have multi screen */
2212 if (win != ws->focus)
2213 if (!(ScreenCount(display) > 1 || outputs > 1))
2217 /* put the last transient on top */
2220 XMapRaised(display, parent->id);
2221 stack_floater(wintrans, ws->r);
2222 focus_magic(wintrans, SWM_F_TRANSIENT);
2227 send_to_ws(struct swm_region *r, union arg *args)
2229 int wsid = args->id;
2230 struct ws_win *win = win;
2231 struct workspace *ws, *nws;
2232 Atom ws_idx_atom = 0;
2233 unsigned char ws_idx_str[SWM_PROPLEN];
2242 if (win->ws->idx == wsid)
2245 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2248 nws = &win->s->ws[wsid];
2250 a.id = SWM_ARG_ID_FOCUSPREV;
2253 TAILQ_REMOVE(&ws->winlist, win, entry);
2254 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2257 /* Try to update the window's workspace property */
2258 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2260 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2261 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2263 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2264 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2270 wkill(struct swm_region *r, union arg *args)
2272 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
2274 if(r->ws->focus == NULL)
2277 if (args->id == SWM_ARG_ID_KILLWINDOW)
2278 XKillClient(display, r->ws->focus->id);
2280 if (r->ws->focus->can_delete)
2281 client_msg(r->ws->focus, adelete);
2285 floating_toggle(struct swm_region *r, union arg *args)
2287 struct ws_win *win = r->ws->focus;
2293 win->floating = !win->floating;
2296 a.id = SWM_ARG_ID_FOCUSCUR;
2297 focus(win->ws->r, &a);
2301 resize_window(struct ws_win *win, int center)
2305 struct swm_region *r;
2307 r = root_to_region(win->wa.root);
2308 bzero(&wc, sizeof wc);
2309 mask = CWBorderWidth | CWWidth | CWHeight;
2310 wc.border_width = 1;
2311 wc.width = win->g.w;
2312 wc.height = win->g.h;
2313 if (center == SWM_ARG_ID_CENTER) {
2314 wc.x = (WIDTH(r) - win->g.w) / 2;
2315 wc.y = (HEIGHT(r) - win->g.h) / 2;
2319 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
2320 win->id, wc.x, wc.y, wc.width, wc.height);
2322 XConfigureWindow(display, win->id, mask, &wc);
2327 resize(struct ws_win *win, union arg *args)
2331 struct swm_region *r = win->ws->r;
2333 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
2334 win->id, win->floating, win->transient);
2336 if (!(win->transient != 0 || win->floating != 0))
2339 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2340 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2342 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w, win->g.h);
2344 XMaskEvent(display, MOUSEMASK | ExposureMask |
2345 SubstructureRedirectMask, &ev);
2347 case ConfigureRequest:
2350 handler[ev.type](&ev);
2353 /* do not allow resize outside of region */
2354 if (ev.xmotion.y_root < r->g.y ||
2355 ev.xmotion.y_root >= r->g.y + r->g.h - 1)
2357 if (ev.xmotion.x_root < r->g.x ||
2358 ev.xmotion.x_root >= r->g.x + r->g.w - 1)
2361 if (ev.xmotion.x <= 1)
2363 if (ev.xmotion.y <= 1)
2365 win->g.w = ev.xmotion.x;
2366 win->g.h = ev.xmotion.y;
2368 /* not free, don't sync more than 60 times / second */
2369 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2370 time = ev.xmotion.time;
2371 XSync(display, False);
2372 resize_window(win, args->id);
2376 } while (ev.type != ButtonRelease);
2378 XSync(display, False);
2379 resize_window(win, args->id);
2381 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
2383 XUngrabPointer(display, CurrentTime);
2386 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2390 move_window(struct ws_win *win)
2394 struct swm_region *r;
2396 r = root_to_region(win->wa.root);
2397 bzero(&wc, sizeof wc);
2402 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
2403 win->id, wc.x, wc.y, wc.width, wc.height);
2405 XConfigureWindow(display, win->id, mask, &wc);
2410 move(struct ws_win *win, union arg *args)
2415 struct swm_region *r = win->ws->r;
2417 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
2418 win->id, win->floating, win->transient);
2420 if (win->floating == 0) {
2426 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2427 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2429 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2431 XMaskEvent(display, MOUSEMASK | ExposureMask |
2432 SubstructureRedirectMask, &ev);
2434 case ConfigureRequest:
2437 handler[ev.type](&ev);
2440 /* don't allow to move window out of region */
2441 if (ev.xmotion.y_root < r->g.y ||
2442 ev.xmotion.y_root + win->g.h >= r->g.y + r->g.h - 1)
2444 if (ev.xmotion.x_root < r->g.x ||
2445 ev.xmotion.x_root + win->g.w >= r->g.x + r->g.w - 1)
2448 win->g.x = ev.xmotion.x_root;
2449 win->g.y = ev.xmotion.y_root;
2451 /* not free, don't sync more than 60 times / second */
2452 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2453 time = ev.xmotion.time;
2454 XSync(display, False);
2459 } while (ev.type != ButtonRelease);
2461 XSync(display, False);
2464 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2465 XUngrabPointer(display, CurrentTime);
2470 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2473 /* key definitions */
2475 dummykeyfunc(struct swm_region *r, union arg *args)
2480 legacyfunc(struct swm_region *r, union arg *args)
2485 char name[SWM_FUNCNAME_LEN];
2486 void (*func)(struct swm_region *r, union arg *);
2488 } keyfuncs[kf_invalid + 1] = {
2489 /* name function argument */
2490 { "cycle_layout", cycle_layout, {0} },
2491 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
2492 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
2493 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
2494 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
2495 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
2496 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
2497 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
2498 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
2499 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
2500 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
2501 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
2502 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
2503 { "spawn_term", spawnterm, {.argv = spawn_term} },
2504 { "spawn_menu", legacyfunc, {0} },
2505 { "quit", quit, {0} },
2506 { "restart", restart, {0} },
2507 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
2508 { "ws_1", switchws, {.id = 0} },
2509 { "ws_2", switchws, {.id = 1} },
2510 { "ws_3", switchws, {.id = 2} },
2511 { "ws_4", switchws, {.id = 3} },
2512 { "ws_5", switchws, {.id = 4} },
2513 { "ws_6", switchws, {.id = 5} },
2514 { "ws_7", switchws, {.id = 6} },
2515 { "ws_8", switchws, {.id = 7} },
2516 { "ws_9", switchws, {.id = 8} },
2517 { "ws_10", switchws, {.id = 9} },
2518 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
2519 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
2520 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
2521 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
2522 { "mvws_1", send_to_ws, {.id = 0} },
2523 { "mvws_2", send_to_ws, {.id = 1} },
2524 { "mvws_3", send_to_ws, {.id = 2} },
2525 { "mvws_4", send_to_ws, {.id = 3} },
2526 { "mvws_5", send_to_ws, {.id = 4} },
2527 { "mvws_6", send_to_ws, {.id = 5} },
2528 { "mvws_7", send_to_ws, {.id = 6} },
2529 { "mvws_8", send_to_ws, {.id = 7} },
2530 { "mvws_9", send_to_ws, {.id = 8} },
2531 { "mvws_10", send_to_ws, {.id = 9} },
2532 { "bar_toggle", bar_toggle, {0} },
2533 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
2534 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
2535 { "screenshot_all", legacyfunc, {0} },
2536 { "screenshot_wind", legacyfunc, {0} },
2537 { "float_toggle", floating_toggle,{0} },
2538 { "version", version, {0} },
2539 { "spawn_lock", legacyfunc, {0} },
2540 { "spawn_initscr", legacyfunc, {0} },
2541 { "spawn_custom", dummykeyfunc, {0} },
2542 { "dumpwins", dumpwins, {0} },
2543 { "invalid key func", NULL, {0} },
2548 enum keyfuncid funcid;
2551 int keys_size = 0, keys_length = 0;
2552 struct key *keys = NULL;
2555 enum { client_click, root_click };
2557 unsigned int action;
2559 unsigned int button;
2560 void (*func)(struct ws_win *, union arg *);
2563 /* action key mouse button func args */
2564 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
2565 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
2566 { client_click, MODKEY, Button1, move, {0} },
2570 update_modkey(unsigned int mod)
2575 for (i = 0; i < keys_length; i++)
2576 if (keys[i].mod & ShiftMask)
2577 keys[i].mod = mod | ShiftMask;
2581 for (i = 0; i < LENGTH(buttons); i++)
2582 if (buttons[i].mask & ShiftMask)
2583 buttons[i].mask = mod | ShiftMask;
2585 buttons[i].mask = mod;
2595 int spawns_size = 0, spawns_length = 0;
2596 struct spawn_prog *spawns = NULL;
2599 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
2602 struct spawn_prog *prog = NULL;
2604 char *ap, **real_args;
2606 DNPRINTF(SWM_D_SPAWN, "spawn_custom %s\n", spawn_name);
2609 for (i = 0; i < spawns_length; i++) {
2610 if (!strcasecmp(spawn_name, spawns[i].name))
2614 fprintf(stderr, "spawn_custom: program %s not found\n",
2619 /* make room for expanded args */
2620 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
2621 err(1, "spawn_custom: calloc real_args");
2623 /* expand spawn_args into real_args */
2624 for (i = 0; i < prog->argc; i++) {
2626 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
2627 if (!strcasecmp(ap, "$bar_border")) {
2629 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
2631 err(1, "spawn_custom border color");
2632 } else if (!strcasecmp(ap, "$bar_color")) {
2634 strdup(r->s->c[SWM_S_COLOR_BAR].name))
2636 err(1, "spawn_custom bar color");
2637 } else if (!strcasecmp(ap, "$bar_font")) {
2638 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
2640 err(1, "spawn_custom bar fonts");
2641 } else if (!strcasecmp(ap, "$bar_font_color")) {
2643 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
2645 err(1, "spawn_custom color font");
2646 } else if (!strcasecmp(ap, "$color_focus")) {
2648 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
2650 err(1, "spawn_custom color focus");
2651 } else if (!strcasecmp(ap, "$color_unfocus")) {
2653 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
2655 err(1, "spawn_custom color unfocus");
2657 /* no match --> copy as is */
2658 if ((real_args[i] = strdup(ap)) == NULL)
2659 err(1, "spawn_custom strdup(ap)");
2661 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
2666 if ((swm_debug & SWM_D_SPAWN) != 0) {
2667 fprintf(stderr, "spawn_custom: result = ");
2668 for (i = 0; i < prog->argc; i++)
2669 fprintf(stderr, "\"%s\" ", real_args[i]);
2670 fprintf(stderr, "\n");
2676 for (i = 0; i < prog->argc; i++)
2682 setspawn(struct spawn_prog *prog)
2686 if (prog == NULL || prog->name == NULL)
2690 for (i = 0; i < spawns_length; i++) {
2691 if (!strcmp(spawns[i].name, prog->name)) {
2693 if (prog->argv == NULL) {
2695 DNPRINTF(SWM_D_SPAWN,
2696 "setspawn: delete #%d %s\n",
2698 free(spawns[i].name);
2699 for (j = 0; j < spawns[i].argc; j++)
2700 free(spawns[i].argv[j]);
2701 free(spawns[i].argv);
2702 j = spawns_length - 1;
2704 spawns[i] = spawns[j];
2709 DNPRINTF(SWM_D_SPAWN,
2710 "setspawn: replace #%d %s\n",
2712 free(spawns[i].name);
2713 for (j = 0; j < spawns[i].argc; j++)
2714 free(spawns[i].argv[j]);
2715 free(spawns[i].argv);
2718 /* found case handled */
2724 if (prog->argv == NULL) {
2726 "error: setspawn: cannot find program %s", prog->name);
2731 /* not found: add */
2732 if (spawns_size == 0 || spawns == NULL) {
2734 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
2735 spawns = malloc((size_t)spawns_size *
2736 sizeof(struct spawn_prog));
2737 if (spawns == NULL) {
2738 fprintf(stderr, "setspawn: malloc failed\n");
2742 } else if (spawns_length == spawns_size) {
2744 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
2745 spawns = realloc(spawns, (size_t)spawns_size *
2746 sizeof(struct spawn_prog));
2747 if (spawns == NULL) {
2748 fprintf(stderr, "setspawn: realloc failed\n");
2754 if (spawns_length < spawns_size) {
2755 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
2756 spawns_length, prog->name);
2757 i = spawns_length++;
2760 fprintf(stderr, "spawns array problem?\n");
2761 if (spawns == NULL) {
2762 fprintf(stderr, "spawns array is NULL!\n");
2770 setconfspawn(char *selector, char *value, int flags)
2772 struct spawn_prog *prog;
2773 char *vp, *cp, *word;
2775 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
2776 if ((prog = calloc(1, sizeof *prog)) == NULL)
2777 err(1, "setconfspawn: calloc prog");
2778 prog->name = strdup(selector);
2779 if (prog->name == NULL)
2780 err(1, "setconfspawn prog->name");
2781 if ((cp = vp = strdup(value)) == NULL)
2782 err(1, "setconfspawn: strdup(value) ");
2783 while ((word = strsep(&cp, " \t")) != NULL) {
2784 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
2786 cp += (long)strspn(cp, " \t");
2787 if (strlen(word) > 0) {
2789 prog->argv = realloc(prog->argv,
2790 prog->argc * sizeof(char *));
2791 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
2792 err(1, "setconfspawn: strdup");
2799 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
2806 setconfspawn("term", "xterm", 0);
2807 setconfspawn("screenshot_all", "screenshot.sh full", 0);
2808 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
2809 setconfspawn("lock", "xlock", 0);
2810 setconfspawn("initscr", "initscreen.sh", 0);
2811 setconfspawn("menu", "dmenu_run"
2814 " -nf $bar_font_color"
2816 " -sf $bar_color", 0);
2820 #define SWM_MODNAME_SIZE 32
2821 #define SWM_KEY_WS "\n+ \t"
2823 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
2827 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
2828 if (mod == NULL || ks == NULL) {
2829 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
2832 if (keystr == NULL || strlen(keystr) == 0) {
2833 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
2839 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
2840 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
2842 cp += (long)strspn(cp, SWM_KEY_WS);
2843 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
2845 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
2847 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
2849 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
2851 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
2853 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
2855 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
2856 *mod |= ControlMask;
2858 *ks = XStringToKeysym(name);
2859 XConvertCase(*ks, ks, &uks);
2860 if (ks == NoSymbol) {
2862 "parsekeys: invalid key %s\n",
2868 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
2873 strdupsafe(char *str)
2878 return (strdup(str));
2882 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
2885 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
2886 keyfuncs[kfid].name, spawn_name);
2888 for (i = 0; i < keys_length; i++) {
2889 if (keys[i].mod == mod && keys[i].keysym == ks) {
2890 if (kfid == kf_invalid) {
2893 "setkeybinding: delete #%d %s\n",
2894 i, keyfuncs[keys[i].funcid].name);
2895 free(keys[i].spawn_name);
2896 j = keys_length - 1;
2900 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2903 /* found: replace */
2905 "setkeybinding: replace #%d %s %s\n",
2906 i, keyfuncs[keys[i].funcid].name,
2908 free(keys[i].spawn_name);
2910 keys[i].keysym = ks;
2911 keys[i].funcid = kfid;
2912 keys[i].spawn_name = strdupsafe(spawn_name);
2913 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2918 if (kfid == kf_invalid) {
2920 "error: setkeybinding: cannot find mod/key combination");
2921 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2924 /* not found: add */
2925 if (keys_size == 0 || keys == NULL) {
2927 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
2928 keys = malloc((size_t)keys_size * sizeof(struct key));
2930 fprintf(stderr, "malloc failed\n");
2934 } else if (keys_length == keys_size) {
2936 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
2937 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
2939 fprintf(stderr, "realloc failed\n");
2944 if (keys_length < keys_size) {
2946 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
2947 j, keyfuncs[kfid].name, spawn_name);
2949 keys[j].keysym = ks;
2950 keys[j].funcid = kfid;
2951 keys[j].spawn_name = strdupsafe(spawn_name);
2953 fprintf(stderr, "keys array problem?\n");
2955 fprintf(stderr, "keys array problem\n");
2959 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2963 setconfbinding(char *selector, char *value, int flags)
2965 enum keyfuncid kfid;
2969 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
2970 if (selector == NULL) {
2971 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
2972 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
2974 setkeybinding(mod, ks, kfid, NULL);
2979 /* search by key function name */
2980 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
2981 if (strncasecmp(selector, keyfuncs[kfid].name,
2982 SWM_FUNCNAME_LEN) == 0) {
2983 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
2985 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
2986 setkeybinding(mod, ks, kfid, NULL);
2992 /* search by custom spawn name */
2993 for (i = 0; i < spawns_length; i++) {
2994 if (strcasecmp(selector, spawns[i].name) == 0) {
2995 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
2997 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
2998 setkeybinding(mod, ks, kf_spawn_custom,
3005 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
3012 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
3013 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
3014 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
3015 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
3016 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
3017 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
3018 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
3019 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
3020 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
3021 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
3022 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
3023 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
3024 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
3025 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
3026 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
3027 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
3028 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
3029 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
3030 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
3031 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
3032 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
3033 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
3034 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
3035 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
3036 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
3037 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
3038 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
3039 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
3040 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
3041 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
3042 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
3043 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
3044 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
3045 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
3046 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
3047 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
3048 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
3049 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
3050 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
3051 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
3052 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
3053 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
3054 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
3055 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
3056 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
3057 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
3058 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
3059 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
3060 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
3061 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
3062 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
3063 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
3064 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
3066 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
3071 updatenumlockmask(void)
3074 XModifierKeymap *modmap;
3076 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
3078 modmap = XGetModifierMapping(display);
3079 for (i = 0; i < 8; i++)
3080 for (j = 0; j < modmap->max_keypermod; j++)
3081 if (modmap->modifiermap[i * modmap->max_keypermod + j]
3082 == XKeysymToKeycode(display, XK_Num_Lock))
3083 numlockmask = (1 << i);
3085 XFreeModifiermap(modmap);
3091 unsigned int i, j, k;
3093 unsigned int modifiers[] =
3094 { 0, LockMask, numlockmask, numlockmask | LockMask };
3096 DNPRINTF(SWM_D_MISC, "grabkeys\n");
3097 updatenumlockmask();
3099 for (k = 0; k < ScreenCount(display); k++) {
3100 if (TAILQ_EMPTY(&screens[k].rl))
3102 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
3103 for (i = 0; i < keys_length; i++) {
3104 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
3105 for (j = 0; j < LENGTH(modifiers); j++)
3106 XGrabKey(display, code,
3107 keys[i].mod | modifiers[j],
3108 screens[k].root, True,
3109 GrabModeAsync, GrabModeAsync);
3115 grabbuttons(struct ws_win *win, int focused)
3118 unsigned int modifiers[] =
3119 { 0, LockMask, numlockmask, numlockmask|LockMask };
3121 updatenumlockmask();
3122 XUngrabButton(display, AnyButton, AnyModifier, win->id);
3124 for (i = 0; i < LENGTH(buttons); i++)
3125 if (buttons[i].action == client_click)
3126 for (j = 0; j < LENGTH(modifiers); j++)
3127 XGrabButton(display, buttons[i].button,
3128 buttons[i].mask | modifiers[j],
3129 win->id, False, BUTTONMASK,
3130 GrabModeAsync, GrabModeSync, None,
3133 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
3134 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
3137 const char *quirkname[] = {
3138 "NONE", /* config string for "no value" */
3146 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
3147 #define SWM_Q_WS "\n|+ \t"
3149 parsequirks(char *qstr, unsigned long *quirk)
3159 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
3161 cp += (long)strspn(cp, SWM_Q_WS);
3162 for (i = 0; i < LENGTH(quirkname); i++) {
3163 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
3164 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
3169 *quirk |= 1 << (i-1);
3173 if (i >= LENGTH(quirkname)) {
3174 DNPRINTF(SWM_D_QUIRK,
3175 "parsequirks: invalid quirk [%s]\n", name);
3183 setquirk(const char *class, const char *name, const int quirk)
3188 for (i = 0; i < quirks_length; i++) {
3189 if (!strcmp(quirks[i].class, class) &&
3190 !strcmp(quirks[i].name, name)) {
3193 DNPRINTF(SWM_D_QUIRK,
3194 "setquirk: delete #%d %s:%s\n",
3195 i, quirks[i].class, quirks[i].name);
3196 free(quirks[i].class);
3197 free(quirks[i].name);
3198 j = quirks_length - 1;
3200 quirks[i] = quirks[j];
3204 /* found: replace */
3205 DNPRINTF(SWM_D_QUIRK,
3206 "setquirk: replace #%d %s:%s\n",
3207 i, quirks[i].class, quirks[i].name);
3208 free(quirks[i].class);
3209 free(quirks[i].name);
3210 quirks[i].class = strdup(class);
3211 quirks[i].name = strdup(name);
3212 quirks[i].quirk = quirk;
3219 "error: setquirk: cannot find class/name combination");
3222 /* not found: add */
3223 if (quirks_size == 0 || quirks == NULL) {
3225 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
3226 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
3228 fprintf(stderr, "setquirk: malloc failed\n");
3232 } else if (quirks_length == quirks_size) {
3234 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
3235 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
3237 fprintf(stderr, "setquirk: realloc failed\n");
3242 if (quirks_length < quirks_size) {
3243 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
3244 j = quirks_length++;
3245 quirks[j].class = strdup(class);
3246 quirks[j].name = strdup(name);
3247 quirks[j].quirk = quirk;
3249 fprintf(stderr, "quirks array problem?\n");
3251 fprintf(stderr, "quirks array problem!\n");
3258 setconfquirk(char *selector, char *value, int flags)
3260 char *cp, *class, *name;
3262 unsigned long quirks;
3263 if (selector == NULL)
3265 if ((cp = strchr(selector, ':')) == NULL)
3270 if ((retval = parsequirks(value, &quirks)) == 0)
3271 setquirk(class, name, quirks);
3278 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
3279 setquirk("OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT);
3280 setquirk("OpenOffice.org 3.0", "VCLSalFrame", SWM_Q_FLOAT);
3281 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
3282 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
3283 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3284 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
3285 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3286 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3287 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3288 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3289 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
3290 setquirk("pcb", "pcb", SWM_Q_FLOAT);
3293 /* conf file stuff */
3294 #define SWM_CONF_FILE "scrotwm.conf"
3296 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_STACK_ENABLED,
3297 SWM_S_CLOCK_ENABLED, SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE,
3298 SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
3299 SWM_S_TITLE_NAME_ENABLED, SWM_S_BAR_FONT, SWM_S_BAR_ACTION,
3300 SWM_S_SPAWN_TERM, SWM_S_SS_APP, SWM_S_DIALOG_RATIO };
3303 setconfvalue(char *selector, char *value, int flags)
3306 case SWM_S_BAR_DELAY:
3307 bar_delay = atoi(value);
3309 case SWM_S_BAR_ENABLED:
3310 bar_enabled = atoi(value);
3312 case SWM_S_STACK_ENABLED:
3313 stack_enabled = atoi(value);
3315 case SWM_S_CLOCK_ENABLED:
3316 clock_enabled = atoi(value);
3318 case SWM_S_CYCLE_EMPTY:
3319 cycle_empty = atoi(value);
3321 case SWM_S_CYCLE_VISIBLE:
3322 cycle_visible = atoi(value);
3324 case SWM_S_SS_ENABLED:
3325 ss_enabled = atoi(value);
3327 case SWM_S_TERM_WIDTH:
3328 term_width = atoi(value);
3330 case SWM_S_TITLE_CLASS_ENABLED:
3331 title_class_enabled = atoi(value);
3333 case SWM_S_TITLE_NAME_ENABLED:
3334 title_name_enabled = atoi(value);
3336 case SWM_S_BAR_FONT:
3338 if ((bar_fonts[0] = strdup(value)) == NULL)
3339 err(1, "setconfvalue: bar_font");
3341 case SWM_S_BAR_ACTION:
3343 if ((bar_argv[0] = strdup(value)) == NULL)
3344 err(1, "setconfvalue: bar_action");
3346 case SWM_S_SPAWN_TERM:
3347 free(spawn_term[0]);
3348 if ((spawn_term[0] = strdup(value)) == NULL)
3349 err(1, "setconfvalue: spawn_term");
3353 case SWM_S_DIALOG_RATIO:
3354 dialog_ratio = atof(value);
3355 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
3365 setconfmodkey(char *selector, char *value, int flags)
3367 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
3368 update_modkey(Mod1Mask);
3369 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
3370 update_modkey(Mod2Mask);
3371 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
3372 update_modkey(Mod3Mask);
3373 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
3374 update_modkey(Mod4Mask);
3381 setconfcolor(char *selector, char *value, int flags)
3383 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
3388 setconfregion(char *selector, char *value, int flags)
3390 custom_region(value);
3394 /* config options */
3395 struct config_option {
3397 int (*func)(char*, char*, int);
3400 struct config_option configopt[] = {
3401 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
3402 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
3403 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
3404 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
3405 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
3406 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
3407 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
3408 { "bind", setconfbinding, 0 },
3409 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
3410 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
3411 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
3412 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
3413 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
3414 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
3415 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
3416 { "modkey", setconfmodkey, 0 },
3417 { "program", setconfspawn, 0 },
3418 { "quirk", setconfquirk, 0 },
3419 { "region", setconfregion, 0 },
3420 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
3421 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
3422 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
3423 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
3424 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
3425 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED }
3430 conf_load(char *filename)
3433 char *line, *cp, *optsub, *optval;
3434 size_t linelen, lineno = 0;
3435 int wordlen, i, optind;
3436 struct config_option *opt;
3438 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
3440 if (filename == NULL) {
3441 fprintf(stderr, "conf_load: no filename\n");
3444 if ((config = fopen(filename, "r")) == NULL) {
3445 warn("conf_load: fopen");
3449 while (!feof(config)) {
3450 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
3453 err(1, "%s", filename);
3458 cp += strspn(cp, " \t\n"); /* eat whitespace */
3459 if (cp[0] == '\0') {
3464 /* get config option */
3465 wordlen = strcspn(cp, "=[ \t\n");
3467 warnx("%s: line %zd: no option found",
3472 for (i = 0; i < LENGTH(configopt); i++) {
3473 opt = &configopt[i];
3474 if (!strncasecmp(cp, opt->optname, wordlen) &&
3475 strlen(opt->optname) == wordlen) {
3481 warnx("%s: line %zd: unknown option %.*s",
3482 filename, lineno, wordlen, cp);
3486 cp += strspn(cp, " \t\n"); /* eat whitespace */
3487 /* get [selector] if any */
3491 wordlen = strcspn(cp, "]");
3494 warnx("%s: line %zd: syntax error",
3498 asprintf(&optsub, "%.*s", wordlen, cp);
3501 cp += strspn(cp, "] \t\n"); /* eat trailing */
3503 cp += strspn(cp, "= \t\n"); /* eat trailing */
3505 optval = strdup(cp);
3506 /* call function to deal with it all */
3507 if (configopt[optind].func(optsub, optval,
3508 configopt[optind].funcflags) != 0) {
3509 fprintf(stderr, "%s line %zd: %s\n",
3510 filename, lineno, line);
3511 errx(1, "%s: line %zd: invalid data for %s",
3512 filename, lineno, configopt[optind].optname);
3520 DNPRINTF(SWM_D_CONF, "conf_load end\n");
3526 set_child_transient(struct ws_win *win)
3528 struct ws_win *parent;
3530 parent = find_window(win->transient);
3532 parent->child_trans = win;
3536 manage_window(Window id)
3539 struct workspace *ws;
3540 struct ws_win *win, *ww;
3541 int format, i, ws_idx, n, border_me = 0;
3542 unsigned long nitems, bytes;
3543 Atom ws_idx_atom = 0, type;
3544 Atom *prot = NULL, *pp;
3545 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
3546 struct swm_region *r;
3551 if ((win = find_window(id)) != NULL)
3552 return (win); /* already being managed */
3554 /* see if we are on the unmanaged list */
3555 if ((win = find_unmanaged_window(id)) != NULL) {
3556 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
3558 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
3559 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
3561 set_child_transient(win);
3565 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
3566 errx(1, "calloc: failed to allocate memory for new window");
3568 /* Get all the window data in one shot */
3569 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3571 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
3572 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
3573 XGetWindowAttributes(display, id, &win->wa);
3574 XGetWMNormalHints(display, id, &win->sh, &mask);
3575 XGetTransientForHint(display, id, &trans);
3577 win->transient = trans;
3578 set_child_transient(win);
3579 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
3580 win->id, win->transient);
3582 /* get supported protocols */
3583 if (XGetWMProtocols(display, id, &prot, &n)) {
3584 for (i = 0, pp = prot; i < n; i++, pp++) {
3585 if (*pp == takefocus)
3586 win->take_focus = 1;
3588 win->can_delete = 1;
3595 * Figure out where to put the window. If it was previously assigned to
3596 * a workspace (either by spawn() or manually moving), and isn't
3597 * transient, * put it in the same workspace
3599 r = root_to_region(win->wa.root);
3600 if (prop && win->transient == 0) {
3601 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
3602 ws_idx = strtonum(prop, 0, 9, &errstr);
3604 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
3607 ws = &r->s->ws[ws_idx];
3610 /* this should launch transients in the same ws as parent */
3612 if ((ww = find_window(trans)) != NULL)
3619 "fix this bug mcbride\n");
3624 /* set up the window layout */
3627 win->s = r->s; /* this never changes */
3628 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
3630 win->g.w = win->wa.width;
3631 win->g.h = win->wa.height;
3632 win->g.x = win->wa.x;
3633 win->g.y = win->wa.y;
3635 /* Set window properties so we can remember this after reincarnation */
3636 if (ws_idx_atom && prop == NULL &&
3637 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
3638 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3640 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3641 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3645 if (XGetClassHint(display, win->id, &win->ch)) {
3646 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
3647 win->ch.res_class, win->ch.res_name);
3649 /* java is retarded so treat it special */
3650 if (strstr(win->ch.res_name, "sun-awt")) {
3655 for (i = 0; i < quirks_length; i++){
3656 if (!strcmp(win->ch.res_class, quirks[i].class) &&
3657 !strcmp(win->ch.res_name, quirks[i].name)) {
3658 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
3659 win->ch.res_class, win->ch.res_name);
3660 if (quirks[i].quirk & SWM_Q_FLOAT) {
3664 win->quirks = quirks[i].quirk;
3669 /* alter window position if quirky */
3670 if (win->quirks & SWM_Q_ANYWHERE) {
3671 win->manual = 1; /* don't center the quirky windows */
3672 bzero(&wc, sizeof wc);
3674 if (win->g.y < bar_height) {
3675 win->g.y = wc.y = bar_height;
3678 if (win->g.w + win->g.x > WIDTH(r)) {
3679 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
3685 /* Reset font sizes (the bruteforce way; no default keybinding). */
3686 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
3687 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3688 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3689 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3690 fake_keypress(win, XK_KP_Add, ShiftMask);
3695 bzero(&wc, sizeof wc);
3696 wc.border_width = 1;
3697 mask = CWBorderWidth;
3698 XConfigureWindow(display, win->id, mask, &wc);
3702 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
3703 PropertyChangeMask | StructureNotifyMask);
3705 set_win_state(win, NormalState);
3707 /* floaters need to be mapped if they are in the current workspace */
3708 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
3709 XMapRaised(display, win->id);
3715 free_window(struct ws_win *win)
3717 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
3722 /* needed for restart wm */
3723 set_win_state(win, WithdrawnState);
3725 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
3727 if (win->ch.res_class)
3728 XFree(win->ch.res_class);
3729 if (win->ch.res_name)
3730 XFree(win->ch.res_name);
3735 memset(win, 0xff, sizeof *win); /* XXX kill later */
3741 unmanage_window(struct ws_win *win)
3743 struct ws_win *parent;
3748 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
3750 if (win->transient) {
3751 parent = find_window(win->transient);
3753 parent->child_trans = NULL;
3757 TAILQ_REMOVE(&win->ws->winlist, win, entry);
3758 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
3764 focus_magic(struct ws_win *win, int do_trans)
3766 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu %d\n", WINID(win), do_trans);
3771 if (do_trans == SWM_F_TRANSIENT && win->child_trans) {
3772 /* win = parent & has a transient so focus on that */
3774 focus_win(win->child_trans);
3775 if (win->child_trans->take_focus)
3776 client_msg(win, takefocus);
3778 focus_win(win->child_trans);
3779 if (win->child_trans->take_focus)
3780 client_msg(win->child_trans, takefocus);
3785 if (win->take_focus)
3786 client_msg(win, takefocus);
3793 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
3801 XKeyEvent *ev = &e->xkey;
3803 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
3805 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
3806 for (i = 0; i < keys_length; i++)
3807 if (keysym == keys[i].keysym
3808 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
3809 && keyfuncs[keys[i].funcid].func) {
3810 if (keys[i].funcid == kf_spawn_custom)
3812 root_to_region(ev->root),
3813 &(keyfuncs[keys[i].funcid].args),
3817 keyfuncs[keys[i].funcid].func(
3818 root_to_region(ev->root),
3819 &(keyfuncs[keys[i].funcid].args)
3825 buttonpress(XEvent *e)
3829 XButtonPressedEvent *ev = &e->xbutton;
3831 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
3833 action = root_click;
3834 if ((win = find_window(ev->window)) == NULL)
3837 focus_magic(win, SWM_F_TRANSIENT);
3838 action = client_click;
3840 for (i = 0; i < LENGTH(buttons); i++)
3841 if (action == buttons[i].action && buttons[i].func &&
3842 buttons[i].button == ev->button &&
3843 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
3844 buttons[i].func(win, &buttons[i].args);
3848 configurerequest(XEvent *e)
3850 XConfigureRequestEvent *ev = &e->xconfigurerequest;
3855 if ((win = find_window(ev->window)) == NULL)
3856 if ((win = find_unmanaged_window(ev->window)) == NULL)
3860 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
3862 bzero(&wc, sizeof wc);
3865 wc.width = ev->width;
3866 wc.height = ev->height;
3867 wc.border_width = ev->border_width;
3868 wc.sibling = ev->above;
3869 wc.stack_mode = ev->detail;
3870 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
3872 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
3874 if (win->floating) {
3875 if (ev->value_mask & CWX)
3877 if (ev->value_mask & CWY)
3879 if (ev->value_mask & CWWidth)
3880 win->g.w = ev->width;
3881 if (ev->value_mask & CWHeight)
3882 win->g.h = ev->height;
3889 configurenotify(XEvent *e)
3894 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
3895 e->xconfigure.window);
3897 win = find_window(e->xconfigure.window);
3899 XGetWMNormalHints(display, win->id, &win->sh, &mask);
3907 destroynotify(XEvent *e)
3910 XDestroyWindowEvent *ev = &e->xdestroywindow;
3912 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
3914 if ((win = find_window(ev->window)) == NULL) {
3915 if ((win = find_unmanaged_window(ev->window)) == NULL)
3921 unmanage_window(win);
3927 enternotify(XEvent *e)
3929 XCrossingEvent *ev = &e->xcrossing;
3932 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
3935 * happens when a window is created or destroyed and the border
3936 * crosses the mouse pointer
3938 if (QLength(display))
3941 if ((win = find_window(ev->window)) == NULL)
3944 focus_magic(win, SWM_F_TRANSIENT);
3950 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
3956 DNPRINTF(SWM_D_EVENT, "focusout: window: %lu\n", e->xfocus.window);
3960 mapnotify(XEvent *e)
3963 XMapEvent *ev = &e->xmap;
3965 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
3967 win = manage_window(ev->window);
3969 set_win_state(win, NormalState);
3973 mappingnotify(XEvent *e)
3975 XMappingEvent *ev = &e->xmapping;
3977 XRefreshKeyboardMapping(ev);
3978 if (ev->request == MappingKeyboard)
3983 maprequest(XEvent *e)
3986 struct swm_region *r;
3987 XWindowAttributes wa;
3988 XMapRequestEvent *ev = &e->xmaprequest;
3990 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
3991 e->xmaprequest.window);
3993 if (!XGetWindowAttributes(display, ev->window, &wa))
3995 if (wa.override_redirect)
3998 win = manage_window(e->xmaprequest.window);
4000 return; /* can't happen */
4004 /* make new win focused */
4005 r = root_to_region(win->wa.root);
4006 if (win->ws == r->ws)
4007 focus_magic(win, SWM_F_GENERIC);
4011 propertynotify(XEvent *e)
4014 XPropertyEvent *ev = &e->xproperty;
4016 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
4019 if (ev->state == PropertyDelete)
4020 return; /* ignore */
4021 win = find_window(ev->window);
4026 case XA_WM_NORMAL_HINTS:
4029 XGetWMNormalHints(display, win->id, &win->sh, &mask);
4030 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
4031 if (win->sh.flags & PMinSize) {
4032 win->g.w = win->sh.min_width;
4033 win->g.h = win->sh.min_height;
4034 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
4036 XMoveResizeWindow(display, win->id,
4037 win->g.x, win->g.y, win->g.w, win->g.h);
4046 unmapnotify(XEvent *e)
4050 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
4052 /* determine if we need to help unmanage this window */
4053 win = find_window(e->xunmap.window);
4058 * XXX this is a work around for going fullscreen on mplayer
4059 * remove this and find a better heuristic
4064 if (getstate(e->xunmap.window) == NormalState) {
4065 unmanage_window(win);
4071 visibilitynotify(XEvent *e)
4074 struct swm_region *r;
4076 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
4077 e->xvisibility.window);
4078 if (e->xvisibility.state == VisibilityUnobscured)
4079 for (i = 0; i < ScreenCount(display); i++)
4080 TAILQ_FOREACH(r, &screens[i].rl, entry)
4081 if (e->xvisibility.window == r->bar_window)
4086 xerror_start(Display *d, XErrorEvent *ee)
4093 xerror(Display *d, XErrorEvent *ee)
4095 /* fprintf(stderr, "error: %p %p\n", display, ee); */
4103 xerrorxlib = XSetErrorHandler(xerror_start);
4105 /* this causes an error if some other window manager is running */
4106 XSelectInput(display, DefaultRootWindow(display),
4107 SubstructureRedirectMask);
4108 XSync(display, False);
4112 XSetErrorHandler(xerror);
4113 XSync(display, False);
4118 new_region(struct swm_screen *s, int x, int y, int w, int h)
4120 struct swm_region *r, *n;
4121 struct workspace *ws = NULL;
4124 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
4125 s->idx, w, h, x, y);
4127 /* remove any conflicting regions */
4128 n = TAILQ_FIRST(&s->rl);
4131 n = TAILQ_NEXT(r, entry);
4132 if (X(r) < (x + w) &&
4133 (X(r) + WIDTH(r)) > x &&
4135 (Y(r) + HEIGHT(r)) > y) {
4136 XDestroyWindow(display, r->bar_window);
4137 TAILQ_REMOVE(&s->rl, r, entry);
4138 TAILQ_INSERT_TAIL(&s->orl, r, entry);
4142 /* search old regions for one to reuse */
4144 /* size + location match */
4145 TAILQ_FOREACH(r, &s->orl, entry)
4146 if (X(r) == x && Y(r) == y &&
4147 HEIGHT(r) == h && WIDTH(r) == w)
4151 TAILQ_FOREACH(r, &s->orl, entry)
4152 if (HEIGHT(r) == h && WIDTH(r) == w)
4156 TAILQ_REMOVE(&s->orl, r, entry);
4157 /* try to use old region's workspace */
4158 if (r->ws->r == NULL)
4161 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
4162 errx(1, "calloc: failed to allocate memory for screen");
4164 /* if we don't have a workspace already, find one */
4166 for (i = 0; i < SWM_WS_MAX; i++)
4167 if (s->ws[i].r == NULL) {
4174 errx(1, "no free workspaces\n");
4183 TAILQ_INSERT_TAIL(&s->rl, r, entry);
4189 #ifdef SWM_XRR_HAS_CRTC
4191 XRRScreenResources *sr;
4194 #endif /* SWM_XRR_HAS_CRTC */
4195 struct swm_region *r;
4198 if (i >= ScreenCount(display))
4199 errx(1, "invalid screen");
4201 /* remove any old regions */
4202 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
4203 r->ws->old_r = r->ws->r = NULL;
4204 XDestroyWindow(display, r->bar_window);
4205 TAILQ_REMOVE(&screens[i].rl, r, entry);
4206 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
4209 /* map virtual screens onto physical screens */
4210 #ifdef SWM_XRR_HAS_CRTC
4212 if (xrandr_support) {
4213 sr = XRRGetScreenResources(display, screens[i].root);
4215 new_region(&screens[i], 0, 0,
4216 DisplayWidth(display, i),
4217 DisplayHeight(display, i));
4221 for (c = 0, ci = NULL; c < ncrtc; c++) {
4222 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
4223 if (ci->noutput == 0)
4227 if (ci != NULL && ci->mode == None)
4228 new_region(&screens[i], 0, 0,
4229 DisplayWidth(display, i),
4230 DisplayHeight(display, i));
4232 new_region(&screens[i],
4233 ci->x, ci->y, ci->width, ci->height);
4236 XRRFreeCrtcInfo(ci);
4237 XRRFreeScreenResources(sr);
4239 #endif /* SWM_XRR_HAS_CRTC */
4241 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
4242 DisplayHeight(display, i));
4247 screenchange(XEvent *e) {
4248 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
4249 struct swm_region *r;
4252 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
4254 if (!XRRUpdateConfiguration(e))
4257 /* silly event doesn't include the screen index */
4258 for (i = 0; i < ScreenCount(display); i++)
4259 if (screens[i].root == xe->root)
4261 if (i >= ScreenCount(display))
4262 errx(1, "screenchange: screen not found\n");
4264 /* brute force for now, just re-enumerate the regions */
4267 /* add bars to all regions */
4268 for (i = 0; i < ScreenCount(display); i++)
4269 TAILQ_FOREACH(r, &screens[i].rl, entry)
4277 Window d1, d2, *wins = NULL;
4278 XWindowAttributes wa;
4281 int errorbase, major, minor;
4282 struct workspace *ws;
4286 if ((screens = calloc(ScreenCount(display),
4287 sizeof(struct swm_screen))) == NULL)
4288 errx(1, "calloc: screens");
4290 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4292 /* initial Xrandr setup */
4293 xrandr_support = XRRQueryExtension(display,
4294 &xrandr_eventbase, &errorbase);
4296 if (XRRQueryVersion(display, &major, &minor) && major < 1)
4299 /* map physical screens */
4300 for (i = 0; i < ScreenCount(display); i++) {
4301 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
4303 TAILQ_INIT(&screens[i].rl);
4304 TAILQ_INIT(&screens[i].orl);
4305 screens[i].root = RootWindow(display, i);
4307 /* set default colors */
4308 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
4309 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
4310 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
4311 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
4312 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
4314 /* init all workspaces */
4315 /* XXX these should be dynamically allocated too */
4316 for (j = 0; j < SWM_WS_MAX; j++) {
4317 ws = &screens[i].ws[j];
4322 TAILQ_INIT(&ws->winlist);
4323 TAILQ_INIT(&ws->unmanagedlist);
4325 for (k = 0; layouts[k].l_stack != NULL; k++)
4326 if (layouts[k].l_config != NULL)
4327 layouts[k].l_config(ws,
4328 SWM_ARG_ID_STACKINIT);
4329 ws->cur_layout = &layouts[0];
4331 /* grab existing windows (before we build the bars)*/
4332 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
4338 XRRSelectInput(display, screens[i].root,
4339 RRScreenChangeNotifyMask);
4341 /* attach windows to a region */
4342 /* normal windows */
4343 for (j = 0; j < no; j++) {
4344 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4345 wa.override_redirect ||
4346 XGetTransientForHint(display, wins[j], &d1))
4349 state = getstate(wins[j]);
4350 manage = state == IconicState;
4351 if (wa.map_state == IsViewable || manage)
4352 manage_window(wins[j]);
4354 /* transient windows */
4355 for (j = 0; j < no; j++) {
4356 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4357 wa.override_redirect)
4360 state = getstate(wins[j]);
4361 manage = state == IconicState;
4362 if (XGetTransientForHint(display, wins[j], &d1) &&
4364 manage_window(wins[j]);
4376 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
4378 err(1, "setup_globals: strdup");
4379 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
4381 err(1, "setup_globals: strdup");
4382 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
4384 err(1, "setup_globals: strdup");
4385 if ((spawn_term[0] = strdup("xterm")) == NULL)
4386 err(1, "setup_globals: strdup");
4393 Atom netwmcheck, netwmname, utf8_string;
4396 /* work around sun jdk bugs, code from wmname */
4397 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
4398 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
4399 utf8_string = XInternAtom(display, "UTF8_STRING", False);
4400 for (i = 0; i < ScreenCount(display); i++) {
4401 root = screens[i].root;
4402 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
4403 PropModeReplace, (unsigned char *)&root, 1);
4404 XChangeProperty(display, root, netwmname, utf8_string, 8,
4405 PropModeReplace, "LG3D", strlen("LG3D"));
4410 main(int argc, char *argv[])
4413 struct swm_region *r, *rr;
4414 struct ws_win *winfocus = NULL;
4417 char conf[PATH_MAX], *cfile = NULL;
4424 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
4425 SWM_VERSION, cvstag);
4426 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
4427 warnx("no locale support");
4429 if (!(display = XOpenDisplay(0)))
4430 errx(1, "can not open display");
4433 errx(1, "other wm running");
4435 /* handle some signale */
4436 installsignal(SIGINT, "INT");
4437 installsignal(SIGHUP, "HUP");
4438 installsignal(SIGQUIT, "QUIT");
4439 installsignal(SIGTERM, "TERM");
4440 installsignal(SIGCHLD, "CHLD");
4442 astate = XInternAtom(display, "WM_STATE", False);
4443 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
4444 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
4445 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
4447 /* look for local and global conf file */
4448 pwd = getpwuid(getuid());
4450 errx(1, "invalid user %d", getuid());
4458 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
4459 if (stat(conf, &sb) != -1) {
4460 if (S_ISREG(sb.st_mode))
4463 /* try global conf file */
4464 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
4465 if (!stat(conf, &sb))
4466 if (S_ISREG(sb.st_mode))
4472 /* setup all bars */
4473 for (i = 0; i < ScreenCount(display); i++)
4474 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4475 if (winfocus == NULL)
4476 winfocus = TAILQ_FIRST(&r->ws->winlist);
4480 /* set some values to work around bad programs */
4486 xfd = ConnectionNumber(display);
4488 while (XPending(display)) {
4489 XNextEvent(display, &e);
4492 if (e.type < LASTEvent) {
4494 if (handler[e.type])
4495 handler[e.type](&e);
4497 DNPRINTF(SWM_D_EVENT,
4498 "win: %lu unknown event: %d\n",
4499 e.xany.window, e.type);
4501 switch (e.type - xrandr_eventbase) {
4502 case RRScreenChangeNotify:
4506 DNPRINTF(SWM_D_EVENT,
4507 "win: %lu unknown xrandr event: "
4508 "%d\n", e.xany.window, e.type);
4514 /* if we are being restarted go focus on first window */
4516 rr = winfocus->ws->r;
4518 /* not a visible window */
4522 /* move pointer to first screen if multi screen */
4523 if (ScreenCount(display) > 1 || outputs > 1)
4524 XWarpPointer(display, None, rr->s[0].root,
4525 0, 0, 0, 0, rr->g.x,
4526 rr->g.y + bar_enabled ? bar_height : 0);
4528 a.id = SWM_ARG_ID_FOCUSCUR;
4538 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
4540 DNPRINTF(SWM_D_MISC, "select failed");
4551 XCloseDisplay(display);