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.24"
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>
92 # error XRandR versions less than 1.0 are not supported
97 #define SWM_XRR_HAS_CRTC
101 /* #define SWM_DEBUG */
103 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
104 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
105 #define SWM_D_MISC 0x0001
106 #define SWM_D_EVENT 0x0002
107 #define SWM_D_WS 0x0004
108 #define SWM_D_FOCUS 0x0008
109 #define SWM_D_MOVE 0x0010
110 #define SWM_D_STACK 0x0020
111 #define SWM_D_MOUSE 0x0040
112 #define SWM_D_PROP 0x0080
113 #define SWM_D_CLASS 0x0100
114 #define SWM_D_KEY 0x0200
115 #define SWM_D_QUIRK 0x0400
116 #define SWM_D_SPAWN 0x0800
117 #define SWM_D_EVENTQ 0x1000
118 #define SWM_D_CONF 0x2000
120 u_int32_t swm_debug = 0
137 #define DPRINTF(x...)
138 #define DNPRINTF(n,x...)
141 #define LENGTH(x) (sizeof x / sizeof x[0])
142 #define MODKEY Mod1Mask
143 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
144 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
145 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
146 #define SWM_PROPLEN (16)
147 #define SWM_FUNCNAME_LEN (32)
148 #define SWM_KEYS_LEN (255)
149 #define SWM_QUIRK_LEN (64)
150 #define X(r) (r)->g.x
151 #define Y(r) (r)->g.y
152 #define WIDTH(r) (r)->g.w
153 #define HEIGHT(r) (r)->g.h
154 #define SWM_MAX_FONT_STEPS (3)
155 #define WINID(w) (w ? w->id : 0)
157 #define SWM_FOCUS_DEFAULT (0)
158 #define SWM_FOCUS_SYNERGY (1)
159 #define SWM_FOCUS_FOLLOW (2)
162 #define SWM_LIB "/usr/local/lib/libswmhack.so"
170 volatile sig_atomic_t running = 1;
172 int last_focus_event = FocusOut;
173 int (*xerrorxlib)(Display *, XErrorEvent *);
177 int xrandr_eventbase;
178 unsigned int numlockmask = 0;
182 int cycle_visible = 0;
184 int font_adjusted = 0;
185 unsigned int mod_key = MODKEY;
188 double dialog_ratio = .6;
190 #define SWM_BAR_MAX (256)
191 char *bar_argv[] = { NULL, NULL };
193 char bar_ext[SWM_BAR_MAX];
194 char bar_vertext[SWM_BAR_MAX];
196 sig_atomic_t bar_alarm = 0;
200 int bar_extra_running = 0;
203 int stack_enabled = 1;
204 int clock_enabled = 1;
205 char *clock_format = NULL;
206 int title_name_enabled = 0;
207 int title_class_enabled = 0;
208 int focus_mode = SWM_FOCUS_DEFAULT;
214 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
215 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
217 #define SWM_MENU_FN (2)
218 #define SWM_MENU_NB (4)
219 #define SWM_MENU_NF (6)
220 #define SWM_MENU_SB (8)
221 #define SWM_MENU_SF (10)
223 /* layout manager data */
224 struct swm_geometry {
234 /* virtual "screens" */
236 TAILQ_ENTRY(swm_region) entry;
237 struct swm_geometry g;
238 struct workspace *ws; /* current workspace on this region */
239 struct swm_screen *s; /* screen idx */
242 TAILQ_HEAD(swm_region_list, swm_region);
245 TAILQ_ENTRY(ws_win) entry;
248 struct ws_win *child_trans; /* transient child window */
249 struct swm_geometry g;
252 int font_size_boundary[SWM_MAX_FONT_STEPS];
258 unsigned long quirks;
259 struct workspace *ws; /* always valid */
260 struct swm_screen *s; /* always valid, never changes */
261 XWindowAttributes wa;
265 TAILQ_HEAD(ws_win_list, ws_win);
267 /* layout handlers */
269 void vertical_config(struct workspace *, int);
270 void vertical_stack(struct workspace *, struct swm_geometry *);
271 void horizontal_config(struct workspace *, int);
272 void horizontal_stack(struct workspace *, struct swm_geometry *);
273 void max_stack(struct workspace *, struct swm_geometry *);
275 struct ws_win *find_window(Window);
277 void grabbuttons(struct ws_win *, int);
278 void new_region(struct swm_screen *, int, int, int, int);
279 void unmanage_window(struct ws_win *);
280 long getstate(Window);
283 void (*l_stack)(struct workspace *, struct swm_geometry *);
284 void (*l_config)(struct workspace *, int);
286 #define SWM_L_FOCUSPREV (1<<0)
287 #define SWM_L_MAPONFOCUS (1<<1)
290 /* stack, configure */
291 { vertical_stack, vertical_config, 0, "[|]" },
292 { horizontal_stack, horizontal_config, 0, "[-]" },
294 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, "[ ]"},
295 { NULL, NULL, 0, NULL },
298 #define SWM_H_SLICE (32)
299 #define SWM_V_SLICE (32)
301 /* define work spaces */
303 int idx; /* workspace index */
304 struct layout *cur_layout; /* current layout handlers */
305 struct ws_win *focus; /* may be NULL */
306 struct ws_win *focus_prev; /* may be NULL */
307 struct swm_region *r; /* may be NULL */
308 struct swm_region *old_r; /* may be NULL */
309 struct ws_win_list winlist; /* list of windows in ws */
310 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
314 int horizontal_msize;
316 int horizontal_stacks;
323 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
324 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
326 /* physical screen mapping */
327 #define SWM_WS_MAX (10)
329 int idx; /* screen index */
330 struct swm_region_list rl; /* list of regions on this screen */
331 struct swm_region_list orl; /* list of old regions */
333 struct workspace ws[SWM_WS_MAX];
339 } c[SWM_S_COLOR_MAX];
341 struct swm_screen *screens;
344 /* args to functions */
347 #define SWM_ARG_ID_FOCUSNEXT (0)
348 #define SWM_ARG_ID_FOCUSPREV (1)
349 #define SWM_ARG_ID_FOCUSMAIN (2)
350 #define SWM_ARG_ID_FOCUSCUR (4)
351 #define SWM_ARG_ID_SWAPNEXT (10)
352 #define SWM_ARG_ID_SWAPPREV (11)
353 #define SWM_ARG_ID_SWAPMAIN (12)
354 #define SWM_ARG_ID_MASTERSHRINK (20)
355 #define SWM_ARG_ID_MASTERGROW (21)
356 #define SWM_ARG_ID_MASTERADD (22)
357 #define SWM_ARG_ID_MASTERDEL (23)
358 #define SWM_ARG_ID_STACKRESET (30)
359 #define SWM_ARG_ID_STACKINIT (31)
360 #define SWM_ARG_ID_CYCLEWS_UP (40)
361 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
362 #define SWM_ARG_ID_CYCLESC_UP (42)
363 #define SWM_ARG_ID_CYCLESC_DOWN (43)
364 #define SWM_ARG_ID_STACKINC (50)
365 #define SWM_ARG_ID_STACKDEC (51)
366 #define SWM_ARG_ID_SS_ALL (60)
367 #define SWM_ARG_ID_SS_WINDOW (61)
368 #define SWM_ARG_ID_DONTCENTER (70)
369 #define SWM_ARG_ID_CENTER (71)
370 #define SWM_ARG_ID_KILLWINDOW (80)
371 #define SWM_ARG_ID_DELETEWINDOW (81)
375 void focus(struct swm_region *, union arg *);
376 void focus_magic(struct ws_win *, int);
377 #define SWM_F_GENERIC (0)
378 #define SWM_F_TRANSIENT (1)
384 #define SWM_Q_FLOAT (1<<0) /* float this window */
385 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
386 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
387 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
388 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
390 int quirks_size = 0, quirks_length = 0;
391 struct quirk *quirks = NULL;
408 name = "ButtonPress";
411 name = "ButtonRelease";
414 name = "MotionNotify";
417 name = "EnterNotify";
420 name = "LeaveNotify";
429 name = "KeymapNotify";
435 name = "GraphicsExpose";
440 case VisibilityNotify:
441 name = "VisibilityNotify";
444 name = "CreateNotify";
447 name = "DestroyNotify";
450 name = "UnmapNotify";
459 name = "ReparentNotify";
461 case ConfigureNotify:
462 name = "ConfigureNotify";
464 case ConfigureRequest:
465 name = "ConfigureRequest";
468 name = "GravityNotify";
471 name = "ResizeRequest";
473 case CirculateNotify:
474 name = "CirculateNotify";
476 case CirculateRequest:
477 name = "CirculateRequest";
480 name = "PropertyNotify";
483 name = "SelectionClear";
485 case SelectionRequest:
486 name = "SelectionRequest";
488 case SelectionNotify:
489 name = "SelectionNotify";
492 name = "ColormapNotify";
495 name = "ClientMessage";
498 name = "MappingNotify";
503 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
505 e->xany.window, name, e->type, QLength(display));
507 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
509 e->xany.window, e->type, QLength(display));
513 dumpwins(struct swm_region *r, union arg *args)
517 XWindowAttributes wa;
520 fprintf(stderr, "invalid workspace\n");
524 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
526 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
527 state = getstate(win->id);
528 if (!XGetWindowAttributes(display, win->id, &wa))
529 fprintf(stderr, "window: %lu failed "
530 "XGetWindowAttributes\n", win->id);
531 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
532 win->id, wa.map_state, state);
535 fprintf(stderr, "===== unmanaged window list =====\n");
536 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
537 state = getstate(win->id);
538 if (!XGetWindowAttributes(display, win->id, &wa))
539 fprintf(stderr, "window: %lu failed "
540 "XGetWindowAttributes\n", win->id);
541 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
542 win->id, wa.map_state, state);
545 fprintf(stderr, "=================================\n");
550 dumpwins(struct swm_region *r, union arg *args)
553 #endif /* SWM_DEBUG */
555 void expose(XEvent *);
556 void keypress(XEvent *);
557 void buttonpress(XEvent *);
558 void configurerequest(XEvent *);
559 void configurenotify(XEvent *);
560 void destroynotify(XEvent *);
561 void enternotify(XEvent *);
562 void focusevent(XEvent *);
563 void mapnotify(XEvent *);
564 void mappingnotify(XEvent *);
565 void maprequest(XEvent *);
566 void propertynotify(XEvent *);
567 void unmapnotify(XEvent *);
568 void visibilitynotify(XEvent *);
570 void (*handler[LASTEvent])(XEvent *) = {
572 [KeyPress] = keypress,
573 [ButtonPress] = buttonpress,
574 [ConfigureRequest] = configurerequest,
575 [ConfigureNotify] = configurenotify,
576 [DestroyNotify] = destroynotify,
577 [EnterNotify] = enternotify,
578 [FocusIn] = focusevent,
579 [FocusOut] = focusevent,
580 [MapNotify] = mapnotify,
581 [MappingNotify] = mappingnotify,
582 [MapRequest] = maprequest,
583 [PropertyNotify] = propertynotify,
584 [UnmapNotify] = unmapnotify,
585 [VisibilityNotify] = visibilitynotify,
595 while ((pid = waitpid(WAIT_ANY, NULL, WNOHANG)) != -1) {
596 DNPRINTF(SWM_D_MISC, "reaping: %d\n", pid);
611 installsignal(int sig, char *name)
615 sa.sa_handler = sighdlr;
616 sigemptyset(&sa.sa_mask);
618 if (sigaction(sig, &sa, NULL) == -1)
619 err(1, "could not install %s handler", name);
623 name_to_color(char *colorname)
627 XColor screen_def, exact_def;
628 unsigned long result = 0;
629 char cname[32] = "#";
631 cmap = DefaultColormap(display, screens[0].idx);
632 status = XAllocNamedColor(display, cmap, colorname,
633 &screen_def, &exact_def);
635 strlcat(cname, colorname + 2, sizeof cname - 1);
636 status = XAllocNamedColor(display, cmap, cname, &screen_def,
640 result = screen_def.pixel;
642 fprintf(stderr, "color '%s' not found.\n", colorname);
648 setscreencolor(char *val, int i, int c)
650 if (i > 0 && i <= ScreenCount(display)) {
651 screens[i - 1].c[c].color = name_to_color(val);
652 free(screens[i - 1].c[c].name);
653 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
655 } else if (i == -1) {
656 for (i = 0; i < ScreenCount(display); i++) {
657 screens[i].c[c].color = name_to_color(val);
658 free(screens[i].c[c].name);
659 if ((screens[i].c[c].name = strdup(val)) == NULL)
663 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
664 i, ScreenCount(display));
668 custom_region(char *val)
670 unsigned int sidx, x, y, w, h;
672 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
673 errx(1, "invalid custom region, "
674 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
675 if (sidx < 1 || sidx > ScreenCount(display))
676 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
677 sidx, ScreenCount(display));
681 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
683 if (x < 0 || x > DisplayWidth(display, sidx) ||
684 y < 0 || y > DisplayHeight(display, sidx) ||
685 w + x > DisplayWidth(display, sidx) ||
686 h + y > DisplayHeight(display, sidx))
687 errx(1, "region %ux%u+%u+%u not within screen boundaries "
688 "(%ux%u)\n", w, h, x, y,
689 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
691 new_region(&screens[sidx], x, y, w, h);
695 socket_setnonblock(int fd)
699 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
700 err(1, "fcntl F_GETFL");
702 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
703 err(1, "fcntl F_SETFL");
707 bar_print(struct swm_region *r, char *s)
709 XClearWindow(display, r->bar_window);
710 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
711 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
720 bzero(bar_pipe, sizeof bar_pipe);
723 kill(bar_pid, SIGTERM);
726 strlcpy(bar_ext, "", sizeof bar_ext);
731 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
733 int do_class, do_name;
735 XClassHint *xch = NULL;
737 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
739 if ((xch = XAllocClassHint()) == NULL)
741 status = XGetClassHint(display, cur_focus->id, xch);
742 if (status == BadWindow || status == BadAlloc)
744 do_class = (title_class_enabled && xch->res_class != NULL);
745 do_name = (title_name_enabled && xch->res_name != NULL);
747 strlcat(s, xch->res_class, sz);
748 if (do_class && do_name)
751 strlcat(s, xch->res_name, sz);
764 struct swm_region *r;
768 char loc[SWM_BAR_MAX];
772 if (bar_enabled == 0)
774 if (bar_extra && bar_extra_running) {
775 /* ignore short reads; it'll correct itself */
776 while ((b = fgetln(stdin, &len)) != NULL)
777 if (b && b[len - 1] == '\n') {
779 strlcpy(bar_ext, b, sizeof bar_ext);
781 if (b == NULL && errno != EAGAIN) {
782 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
783 errno, strerror(errno));
787 strlcpy(bar_ext, "", sizeof bar_ext);
789 if (clock_enabled == 0)
790 strlcpy(s, "", sizeof s);
793 localtime_r(&tmt, &tm);
794 strftime(s, sizeof s, clock_format, &tm);
795 strlcat(s, " ", sizeof s);
798 for (i = 0; i < ScreenCount(display); i++) {
800 TAILQ_FOREACH(r, &screens[i].rl, entry) {
802 bar_class_name(s, sizeof s, r->ws->focus);
805 stack = r->ws->cur_layout->name;
807 snprintf(loc, sizeof loc, "%d:%d %s %s %s %s",
808 x++, r->ws->idx + 1, stack, s, bar_ext,
823 bar_toggle(struct swm_region *r, union arg *args)
825 struct swm_region *tmpr;
826 int i, sc = ScreenCount(display);
828 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
831 for (i = 0; i < sc; i++)
832 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
833 XUnmapWindow(display, tmpr->bar_window);
835 for (i = 0; i < sc; i++)
836 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
837 XMapRaised(display, tmpr->bar_window);
839 bar_enabled = !bar_enabled;
842 /* must be after stack */
849 XSetWindowAttributes wa;
850 struct swm_region *r;
853 /* do this here because the conf file is in memory */
854 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
855 /* launch external status app */
856 bar_extra_running = 1;
857 if (pipe(bar_pipe) == -1)
858 err(1, "pipe error");
859 socket_setnonblock(bar_pipe[0]);
860 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
861 if (dup2(bar_pipe[0], 0) == -1)
863 if (dup2(bar_pipe[1], 1) == -1)
865 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
866 err(1, "could not disable SIGPIPE");
867 switch (bar_pid = fork()) {
869 err(1, "cannot fork");
873 execvp(bar_argv[0], bar_argv);
874 err(1, "%s external app failed", bar_argv[0]);
876 default: /* parent */
882 bzero(&wa, sizeof wa);
883 for (i = 0; i < ScreenCount(display); i++)
884 TAILQ_FOREACH(r, &screens[i].rl, entry) {
886 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
887 wa.background_pixel =
888 screens[i].c[SWM_S_COLOR_BAR].color;
889 XChangeWindowAttributes(display, r->bar_window,
890 CWBackPixel | CWBorderPixel, &wa);
896 bar_setup(struct swm_region *r)
900 for (i = 0; bar_fonts[i] != NULL; i++) {
901 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
907 if (bar_fonts[i] == NULL)
908 errx(1, "couldn't load font");
909 bar_height = bar_fs->ascent + bar_fs->descent + 3;
911 r->bar_window = XCreateSimpleWindow(display,
912 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
913 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
914 r->s->c[SWM_S_COLOR_BAR].color);
915 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
916 XSetFont(display, bar_gc, bar_fs->fid);
917 XSelectInput(display, r->bar_window, VisibilityChangeMask);
919 XMapRaised(display, r->bar_window);
920 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
922 if (signal(SIGALRM, bar_signal) == SIG_ERR)
923 err(1, "could not install bar_signal");
928 set_win_state(struct ws_win *win, long state)
930 long data[] = {state, None};
932 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
937 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
938 (unsigned char *)data, 2);
946 unsigned char *p = NULL;
947 unsigned long n, extra;
950 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
951 &real, &format, &n, &extra, (unsigned char **)&p);
952 if (status != Success)
955 result = *((long *)p);
961 version(struct swm_region *r, union arg *args)
963 bar_version = !bar_version;
965 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
966 SWM_VERSION, cvstag);
968 strlcpy(bar_vertext, "", sizeof bar_vertext);
973 client_msg(struct ws_win *win, Atom a)
975 XClientMessageEvent cm;
980 bzero(&cm, sizeof cm);
981 cm.type = ClientMessage;
983 cm.message_type = aprot;
986 cm.data.l[1] = CurrentTime;
987 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
991 configreq_win(struct ws_win *win)
993 XConfigureRequestEvent cr;
998 bzero(&cr, sizeof cr);
999 cr.type = ConfigureRequest;
1000 cr.display = display;
1001 cr.parent = win->id;
1002 cr.window = win->id;
1005 cr.width = win->g.w;
1006 cr.height = win->g.h;
1007 cr.border_width = 1;
1009 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&cr);
1013 config_win(struct ws_win *win)
1017 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
1018 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1023 ce.type = ConfigureNotify;
1024 ce.display = display;
1026 ce.window = win->id;
1029 ce.width = win->g.w;
1030 ce.height = win->g.h;
1031 ce.border_width = 1; /* XXX store this! */
1033 ce.override_redirect = False;
1034 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1038 count_win(struct workspace *ws, int count_transient)
1043 TAILQ_FOREACH(win, &ws->winlist, entry) {
1044 if (count_transient == 0 && win->floating)
1046 if (count_transient == 0 && win->transient)
1050 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1056 quit(struct swm_region *r, union arg *args)
1058 DNPRINTF(SWM_D_MISC, "quit\n");
1063 unmap_window(struct ws_win *win)
1068 /* don't unmap again */
1069 if (getstate(win->id) == IconicState)
1072 set_win_state(win, IconicState);
1074 XUnmapWindow(display, win->id);
1076 XSetWindowBorder(display, win->id,
1077 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1086 for (i = 0; i < ScreenCount(display); i++)
1087 for (j = 0; j < SWM_WS_MAX; j++)
1088 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1093 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1100 event.display = display; /* Ignored, but what the hell */
1101 event.window = win->id;
1102 event.root = win->s->root;
1103 event.subwindow = None;
1104 event.time = CurrentTime;
1109 event.same_screen = True;
1110 event.keycode = XKeysymToKeycode(display, keysym);
1111 event.state = modifiers;
1113 event.type = KeyPress;
1114 XSendEvent(event.display, event.window, True,
1115 KeyPressMask, (XEvent *)&event);
1117 event.type = KeyRelease;
1118 XSendEvent(event.display, event.window, True,
1119 KeyPressMask, (XEvent *)&event);
1124 restart(struct swm_region *r, union arg *args)
1126 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1128 /* disable alarm because the following code may not be interrupted */
1130 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1131 errx(1, "can't disable alarm");
1136 XCloseDisplay(display);
1137 execvp(start_argv[0], start_argv);
1138 fprintf(stderr, "execvp failed\n");
1144 root_to_region(Window root)
1146 struct swm_region *r = NULL;
1148 int i, x, y, wx, wy;
1151 for (i = 0; i < ScreenCount(display); i++)
1152 if (screens[i].root == root)
1155 if (XQueryPointer(display, screens[i].root,
1156 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1157 /* choose a region based on pointer location */
1158 TAILQ_FOREACH(r, &screens[i].rl, entry)
1159 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1160 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1165 r = TAILQ_FIRST(&screens[i].rl);
1171 find_unmanaged_window(Window id)
1176 for (i = 0; i < ScreenCount(display); i++)
1177 for (j = 0; j < SWM_WS_MAX; j++)
1178 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1186 find_window(Window id)
1189 Window wrr, wpr, *wcr = NULL;
1192 for (i = 0; i < ScreenCount(display); i++)
1193 for (j = 0; j < SWM_WS_MAX; j++)
1194 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1198 /* if we were looking for the parent return that window instead */
1199 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1204 /* ignore not found and root */
1205 if (wpr == 0 || wrr == wpr)
1208 /* look for parent */
1209 for (i = 0; i < ScreenCount(display); i++)
1210 for (j = 0; j < SWM_WS_MAX; j++)
1211 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1219 spawn(struct swm_region *r, union arg *args)
1224 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1226 * The double-fork construct avoids zombie processes and keeps the code
1227 * clean from stupid signal handlers.
1232 close(ConnectionNumber(display));
1233 setenv("LD_PRELOAD", SWM_LIB, 1);
1234 if (asprintf(&ret, "%d", r->ws->idx)) {
1235 setenv("_SWM_WS", ret, 1);
1238 if (asprintf(&ret, "%d", getpid())) {
1239 setenv("_SWM_PID", ret, 1);
1243 /* kill stdin, mplayer, ssh-add etc. need that */
1244 si = open("/dev/null", O_RDONLY, 0);
1246 err(1, "open /dev/null");
1247 if (dup2(si, 0) == -1)
1248 err(1, "dup2 /dev/null");
1249 execvp(args->argv[0], args->argv);
1250 fprintf(stderr, "execvp failed\n");
1258 spawnterm(struct swm_region *r, union arg *args)
1260 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1263 setenv("_SWM_XTERM_FONTADJ", "", 1);
1268 kill_refs(struct ws_win *win)
1271 struct swm_region *r;
1272 struct workspace *ws;
1277 for (i = 0; i < ScreenCount(display); i++)
1278 TAILQ_FOREACH(r, &screens[i].rl, entry)
1279 for (x = 0; x < SWM_WS_MAX; x++) {
1281 if (win == ws->focus)
1283 if (win == ws->focus_prev)
1284 ws->focus_prev = NULL;
1289 validate_win(struct ws_win *testwin)
1292 struct workspace *ws;
1293 struct swm_region *r;
1294 int i, x, foundit = 0;
1296 if (testwin == NULL)
1299 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1300 TAILQ_FOREACH(r, &screens[i].rl, entry)
1301 for (x = 0; x < SWM_WS_MAX; x++) {
1303 TAILQ_FOREACH(win, &ws->winlist, entry)
1311 validate_ws(struct workspace *testws)
1313 struct swm_region *r;
1314 struct workspace *ws;
1317 /* validate all ws */
1318 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1319 TAILQ_FOREACH(r, &screens[i].rl, entry)
1320 for (x = 0; x < SWM_WS_MAX; x++) {
1329 unfocus_win(struct ws_win *win)
1333 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1337 if (win->ws == NULL)
1340 if (validate_ws(win->ws))
1343 if (win->ws->r == NULL)
1346 if (validate_win(win)) {
1351 if (win->ws->focus == win) {
1352 win->ws->focus = NULL;
1353 win->ws->focus_prev = win;
1356 if (validate_win(win->ws->focus)) {
1357 kill_refs(win->ws->focus);
1358 win->ws->focus = NULL;
1360 if (validate_win(win->ws->focus_prev)) {
1361 kill_refs(win->ws->focus_prev);
1362 win->ws->focus_prev = NULL;
1365 /* drain all previous unfocus events */
1366 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
1369 grabbuttons(win, 0);
1370 XSetWindowBorder(display, win->id,
1371 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1381 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
1383 for (i = 0; i < ScreenCount(display); i++)
1384 for (j = 0; j < SWM_WS_MAX; j++)
1385 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1390 focus_win(struct ws_win *win)
1395 struct ws_win *cfw = NULL;
1398 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1402 if (win->ws == NULL)
1405 if (validate_ws(win->ws))
1407 if (validate_win(win)) {
1412 if (validate_win(win)) {
1417 XGetInputFocus(display, &cur_focus, &rr);
1418 if ((cfw = find_window(cur_focus)) != NULL)
1421 win->ws->focus = win;
1423 if (win->ws->r != NULL) {
1424 /* drain all previous focus events */
1425 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
1429 XSetInputFocus(display, win->id,
1430 RevertToParent, CurrentTime);
1431 grabbuttons(win, 1);
1432 XSetWindowBorder(display, win->id,
1433 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1434 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS)
1435 XMapRaised(display, win->id);
1440 switchws(struct swm_region *r, union arg *args)
1442 int wsid = args->id, unmap_old = 0;
1443 struct swm_region *this_r, *other_r;
1445 struct workspace *new_ws, *old_ws;
1452 old_ws = this_r->ws;
1453 new_ws = &this_r->s->ws[wsid];
1455 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1456 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1459 if (new_ws == NULL || old_ws == NULL)
1461 if (new_ws == old_ws)
1464 other_r = new_ws->r;
1465 if (other_r == NULL) {
1466 /* if the other workspace is hidden, switch windows */
1467 if (old_ws->r != NULL)
1468 old_ws->old_r = old_ws->r;
1472 other_r->ws = old_ws;
1473 old_ws->r = other_r;
1475 this_r->ws = new_ws;
1479 a.id = SWM_ARG_ID_FOCUSCUR;
1480 focus(new_ws->r, &a);
1483 /* unmap old windows */
1485 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1490 cyclews(struct swm_region *r, union arg *args)
1493 struct swm_screen *s = r->s;
1495 DNPRINTF(SWM_D_WS, "cyclews id %d "
1496 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1497 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1502 case SWM_ARG_ID_CYCLEWS_UP:
1503 if (a.id < SWM_WS_MAX - 1)
1508 case SWM_ARG_ID_CYCLEWS_DOWN:
1512 a.id = SWM_WS_MAX - 1;
1518 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1520 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1524 } while (a.id != r->ws->idx);
1528 cyclescr(struct swm_region *r, union arg *args)
1530 struct swm_region *rr = NULL;
1534 /* do nothing if we don't have more than one screen */
1535 if (!(ScreenCount(display) > 1 || outputs > 1))
1540 case SWM_ARG_ID_CYCLESC_UP:
1541 rr = TAILQ_NEXT(r, entry);
1543 rr = TAILQ_FIRST(&screens[i].rl);
1545 case SWM_ARG_ID_CYCLESC_DOWN:
1546 rr = TAILQ_PREV(r, swm_region_list, entry);
1548 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1556 /* move mouse to region */
1558 y = rr->g.y + 1 + bar_enabled ? bar_height : 0;
1559 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1561 a.id = SWM_ARG_ID_FOCUSCUR;
1564 if (rr->ws->focus) {
1565 /* move to focus window */
1566 x = rr->ws->focus->g.x + 1;
1567 y = rr->ws->focus->g.y + 1;
1568 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1573 swapwin(struct swm_region *r, union arg *args)
1575 struct ws_win *target, *source;
1576 struct ws_win *cur_focus;
1577 struct ws_win_list *wl;
1580 DNPRINTF(SWM_D_WS, "swapwin id %d "
1581 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1582 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1584 cur_focus = r->ws->focus;
1585 if (cur_focus == NULL)
1589 wl = &source->ws->winlist;
1592 case SWM_ARG_ID_SWAPPREV:
1593 target = TAILQ_PREV(source, ws_win_list, entry);
1594 TAILQ_REMOVE(wl, cur_focus, entry);
1596 TAILQ_INSERT_TAIL(wl, source, entry);
1598 TAILQ_INSERT_BEFORE(target, source, entry);
1600 case SWM_ARG_ID_SWAPNEXT:
1601 target = TAILQ_NEXT(source, entry);
1602 TAILQ_REMOVE(wl, source, entry);
1604 TAILQ_INSERT_HEAD(wl, source, entry);
1606 TAILQ_INSERT_AFTER(wl, target, source, entry);
1608 case SWM_ARG_ID_SWAPMAIN:
1609 target = TAILQ_FIRST(wl);
1610 if (target == source) {
1611 if (source->ws->focus_prev != NULL &&
1612 source->ws->focus_prev != target)
1614 source = source->ws->focus_prev;
1618 if (target == NULL || source == NULL)
1620 source->ws->focus_prev = target;
1621 TAILQ_REMOVE(wl, target, entry);
1622 TAILQ_INSERT_BEFORE(source, target, entry);
1623 TAILQ_REMOVE(wl, source, entry);
1624 TAILQ_INSERT_HEAD(wl, source, entry);
1627 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1635 focus_prev(struct ws_win *win)
1637 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
1638 struct ws_win *cur_focus = NULL;
1639 struct ws_win_list *wl = NULL;
1640 struct workspace *ws = NULL;
1642 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
1644 if (!(win && win->ws))
1649 cur_focus = ws->focus;
1650 winlostfocus = cur_focus;
1652 /* pickle, just focus on whatever */
1653 if (cur_focus == NULL) {
1654 /* use prev_focus if valid */
1655 if (ws->focus_prev && ws->focus_prev != cur_focus &&
1656 find_window(WINID(ws->focus_prev)))
1657 winfocus = ws->focus_prev;
1658 if (winfocus == NULL)
1659 winfocus = TAILQ_FIRST(wl);
1663 /* if transient focus on parent */
1664 if (cur_focus->transient) {
1665 winfocus = find_window(cur_focus->transient);
1669 /* if in max_stack try harder */
1670 if (ws->cur_layout->flags & SWM_L_FOCUSPREV) {
1671 if (cur_focus != ws->focus_prev)
1672 winfocus = ws->focus_prev;
1673 else if (cur_focus != ws->focus)
1674 winfocus = ws->focus;
1676 winfocus = TAILQ_PREV(win, ws_win_list, entry);
1681 if (cur_focus == win)
1682 winfocus = TAILQ_PREV(win, ws_win_list, entry);
1683 if (winfocus == NULL)
1684 winfocus = TAILQ_LAST(wl, ws_win_list);
1685 if (winfocus == NULL || winfocus == win)
1686 winfocus = TAILQ_NEXT(cur_focus, entry);
1688 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 XWindowAttributes wa;
1908 struct swm_geometry win_g, r_g = *g;
1910 int i, j, s, stacks;
1911 int w_inc = 1, h_inc, w_base = 1, h_base;
1912 int hrh, extra = 0, h_slice, last_h = 0;
1913 int split, colno, winno, mwin, msize, mscale;
1914 int remain, missing, v_slice, reconfigure;
1917 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1918 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1920 winno = count_win(ws, 0);
1921 if (winno == 0 && count_win(ws, 1) == 0)
1924 TAILQ_FOREACH(win, &ws->winlist, entry)
1925 if (win->transient == 0 && win->floating == 0)
1932 w_inc = win->sh.width_inc;
1933 w_base = win->sh.base_width;
1934 mwin = ws->l_state.horizontal_mwin;
1935 mscale = ws->l_state.horizontal_msize;
1936 stacks = ws->l_state.horizontal_stacks;
1939 w_inc = win->sh.height_inc;
1940 w_base = win->sh.base_height;
1941 mwin = ws->l_state.vertical_mwin;
1942 mscale = ws->l_state.vertical_msize;
1943 stacks = ws->l_state.vertical_stacks;
1947 if (stacks > winno - mwin)
1948 stacks = winno - mwin;
1952 h_slice = r_g.h / SWM_H_SLICE;
1953 if (mwin && winno > mwin) {
1954 v_slice = r_g.w / SWM_V_SLICE;
1958 win_g.w = v_slice * mscale;
1960 if (w_inc > 1 && w_inc < v_slice) {
1961 /* adjust for window's requested size increment */
1962 remain = (win_g.w - w_base) % w_inc;
1963 missing = w_inc - remain;
1970 win_g.x += r_g.w - msize;
1973 colno = split = winno / stacks;
1974 win_g.w = ((r_g.w - (stacks * 2) + 2) / stacks);
1976 hrh = r_g.h / colno;
1977 extra = r_g.h - (colno * hrh);
1980 /* stack all the tiled windows */
1981 i = j = 0, s = stacks;
1982 TAILQ_FOREACH(win, &ws->winlist, entry) {
1983 if (win->transient != 0 || win->floating != 0)
1986 if (split && i == split) {
1987 colno = (winno - mwin) / stacks;
1988 if (s <= (winno - mwin) % stacks)
1990 split = split + colno;
1991 hrh = (r_g.h / colno);
1992 extra = r_g.h - (colno * hrh);
1996 win_g.x += win_g.w + 2;
1997 win_g.w = (r_g.w - msize - (stacks * 2)) / stacks;
1999 win_g.w += (r_g.w - msize - (stacks * 2)) %
2006 h_inc = win->sh.width_inc;
2007 h_base = win->sh.base_width;
2009 h_inc = win->sh.height_inc;
2010 h_base = win->sh.base_height;
2012 if (j == colno - 1) {
2013 win_g.h = hrh + extra;
2014 } else if (h_inc > 1 && h_inc < h_slice) {
2015 /* adjust for window's requested size increment */
2016 remain = (win_g.h - h_base) % h_inc;
2017 missing = h_inc - remain;
2019 if (missing <= extra || j == 0) {
2031 win_g.y += last_h + 2;
2033 bzero(&wc, sizeof wc);
2034 wc.border_width = 1;
2037 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2038 win->g.w != win_g.h || win->g.h != win_g.w) {
2040 win->g.x = wc.x = win_g.y;
2041 win->g.y = wc.y = win_g.x;
2042 win->g.w = wc.width = win_g.h;
2043 win->g.h = wc.height = win_g.w;
2046 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2047 win->g.w != win_g.w || win->g.h != win_g.h) {
2049 win->g.x = wc.x = win_g.x;
2050 win->g.y = wc.y = win_g.y;
2051 win->g.w = wc.width = win_g.w;
2052 win->g.h = wc.height = win_g.h;
2057 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2058 XConfigureWindow(display, win->id, mask, &wc);
2062 if (XGetWindowAttributes(display, win->id, &wa))
2063 if (wa.map_state == IsUnmapped)
2064 XMapRaised(display, win->id);
2072 /* now, stack all the floaters and transients */
2073 TAILQ_FOREACH(win, &ws->winlist, entry) {
2074 if (win->transient == 0 && win->floating == 0)
2077 stack_floater(win, ws->r);
2078 XMapRaised(display, win->id);
2083 vertical_config(struct workspace *ws, int id)
2085 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2088 case SWM_ARG_ID_STACKRESET:
2089 case SWM_ARG_ID_STACKINIT:
2090 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2091 ws->l_state.vertical_mwin = 1;
2092 ws->l_state.vertical_stacks = 1;
2094 case SWM_ARG_ID_MASTERSHRINK:
2095 if (ws->l_state.vertical_msize > 1)
2096 ws->l_state.vertical_msize--;
2098 case SWM_ARG_ID_MASTERGROW:
2099 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2100 ws->l_state.vertical_msize++;
2102 case SWM_ARG_ID_MASTERADD:
2103 ws->l_state.vertical_mwin++;
2105 case SWM_ARG_ID_MASTERDEL:
2106 if (ws->l_state.vertical_mwin > 0)
2107 ws->l_state.vertical_mwin--;
2109 case SWM_ARG_ID_STACKINC:
2110 ws->l_state.vertical_stacks++;
2112 case SWM_ARG_ID_STACKDEC:
2113 if (ws->l_state.vertical_stacks > 1)
2114 ws->l_state.vertical_stacks--;
2122 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2124 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2126 stack_master(ws, g, 0, 0);
2130 horizontal_config(struct workspace *ws, int id)
2132 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2135 case SWM_ARG_ID_STACKRESET:
2136 case SWM_ARG_ID_STACKINIT:
2137 ws->l_state.horizontal_mwin = 1;
2138 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2139 ws->l_state.horizontal_stacks = 1;
2141 case SWM_ARG_ID_MASTERSHRINK:
2142 if (ws->l_state.horizontal_msize > 1)
2143 ws->l_state.horizontal_msize--;
2145 case SWM_ARG_ID_MASTERGROW:
2146 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2147 ws->l_state.horizontal_msize++;
2149 case SWM_ARG_ID_MASTERADD:
2150 ws->l_state.horizontal_mwin++;
2152 case SWM_ARG_ID_MASTERDEL:
2153 if (ws->l_state.horizontal_mwin > 0)
2154 ws->l_state.horizontal_mwin--;
2156 case SWM_ARG_ID_STACKINC:
2157 ws->l_state.horizontal_stacks++;
2159 case SWM_ARG_ID_STACKDEC:
2160 if (ws->l_state.horizontal_stacks > 1)
2161 ws->l_state.horizontal_stacks--;
2169 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2171 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2173 stack_master(ws, g, 1, 0);
2176 /* fullscreen view */
2178 max_stack(struct workspace *ws, struct swm_geometry *g)
2181 struct swm_geometry gg = *g;
2182 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2186 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2191 winno = count_win(ws, 0);
2192 if (winno == 0 && count_win(ws, 1) == 0)
2195 TAILQ_FOREACH(win, &ws->winlist, entry) {
2196 if (win->transient) {
2198 parent = find_window(win->transient);
2202 /* only reconfigure if necessary */
2203 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2205 bzero(&wc, sizeof wc);
2206 wc.border_width = 1;
2207 win->g.x = wc.x = gg.x;
2208 win->g.y = wc.y = gg.y;
2209 win->g.w = wc.width = gg.w;
2210 win->g.h = wc.height = gg.h;
2211 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2212 XConfigureWindow(display, win->id, mask, &wc);
2215 /* unmap only if we don't have multi screen */
2216 if (win != ws->focus)
2217 if (!(ScreenCount(display) > 1 || outputs > 1))
2221 /* put the last transient on top */
2224 XMapRaised(display, parent->id);
2225 stack_floater(wintrans, ws->r);
2226 focus_magic(wintrans, SWM_F_TRANSIENT);
2231 send_to_ws(struct swm_region *r, union arg *args)
2233 int wsid = args->id;
2234 struct ws_win *win = win;
2235 struct workspace *ws, *nws;
2236 Atom ws_idx_atom = 0;
2237 unsigned char ws_idx_str[SWM_PROPLEN];
2246 if (win->ws->idx == wsid)
2249 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2252 nws = &win->s->ws[wsid];
2254 a.id = SWM_ARG_ID_FOCUSPREV;
2257 TAILQ_REMOVE(&ws->winlist, win, entry);
2258 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2261 /* Try to update the window's workspace property */
2262 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2264 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2265 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2267 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2268 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2274 wkill(struct swm_region *r, union arg *args)
2276 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
2278 if(r->ws->focus == NULL)
2281 if (args->id == SWM_ARG_ID_KILLWINDOW)
2282 XKillClient(display, r->ws->focus->id);
2284 if (r->ws->focus->can_delete)
2285 client_msg(r->ws->focus, adelete);
2289 floating_toggle(struct swm_region *r, union arg *args)
2291 struct ws_win *win = r->ws->focus;
2297 win->floating = !win->floating;
2300 a.id = SWM_ARG_ID_FOCUSCUR;
2301 focus(win->ws->r, &a);
2305 resize_window(struct ws_win *win, int center)
2309 struct swm_region *r;
2311 r = root_to_region(win->wa.root);
2312 bzero(&wc, sizeof wc);
2313 mask = CWBorderWidth | CWWidth | CWHeight;
2314 wc.border_width = 1;
2315 wc.width = win->g.w;
2316 wc.height = win->g.h;
2317 if (center == SWM_ARG_ID_CENTER) {
2318 wc.x = (WIDTH(r) - win->g.w) / 2;
2319 wc.y = (HEIGHT(r) - win->g.h) / 2;
2323 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
2324 win->id, wc.x, wc.y, wc.width, wc.height);
2326 XConfigureWindow(display, win->id, mask, &wc);
2331 resize(struct ws_win *win, union arg *args)
2335 struct swm_region *r = win->ws->r;
2337 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
2338 win->id, win->floating, win->transient);
2340 if (!(win->transient != 0 || win->floating != 0))
2343 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2344 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2346 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w, win->g.h);
2348 XMaskEvent(display, MOUSEMASK | ExposureMask |
2349 SubstructureRedirectMask, &ev);
2351 case ConfigureRequest:
2354 handler[ev.type](&ev);
2357 /* do not allow resize outside of region */
2358 if (ev.xmotion.y_root < r->g.y ||
2359 ev.xmotion.y_root >= r->g.y + r->g.h - 1)
2361 if (ev.xmotion.x_root < r->g.x ||
2362 ev.xmotion.x_root >= r->g.x + r->g.w - 1)
2365 if (ev.xmotion.x <= 1)
2367 if (ev.xmotion.y <= 1)
2369 win->g.w = ev.xmotion.x;
2370 win->g.h = ev.xmotion.y;
2372 /* not free, don't sync more than 60 times / second */
2373 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2374 time = ev.xmotion.time;
2375 XSync(display, False);
2376 resize_window(win, args->id);
2380 } while (ev.type != ButtonRelease);
2382 XSync(display, False);
2383 resize_window(win, args->id);
2385 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
2387 XUngrabPointer(display, CurrentTime);
2390 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2394 move_window(struct ws_win *win)
2398 struct swm_region *r;
2400 r = root_to_region(win->wa.root);
2401 bzero(&wc, sizeof wc);
2406 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
2407 win->id, wc.x, wc.y, wc.width, wc.height);
2409 XConfigureWindow(display, win->id, mask, &wc);
2414 move(struct ws_win *win, union arg *args)
2419 struct swm_region *r = win->ws->r;
2421 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
2422 win->id, win->floating, win->transient);
2424 if (win->floating == 0) {
2430 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2431 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2433 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2435 XMaskEvent(display, MOUSEMASK | ExposureMask |
2436 SubstructureRedirectMask, &ev);
2438 case ConfigureRequest:
2441 handler[ev.type](&ev);
2444 /* don't allow to move window out of region */
2445 if (ev.xmotion.y_root < r->g.y ||
2446 ev.xmotion.y_root + win->g.h >= r->g.y + r->g.h - 1)
2448 if (ev.xmotion.x_root < r->g.x ||
2449 ev.xmotion.x_root + win->g.w >= r->g.x + r->g.w - 1)
2452 win->g.x = ev.xmotion.x_root;
2453 win->g.y = ev.xmotion.y_root;
2455 /* not free, don't sync more than 60 times / second */
2456 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2457 time = ev.xmotion.time;
2458 XSync(display, False);
2463 } while (ev.type != ButtonRelease);
2465 XSync(display, False);
2468 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2469 XUngrabPointer(display, CurrentTime);
2474 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2477 /* user/key callable function IDs */
2535 /* key definitions */
2537 dummykeyfunc(struct swm_region *r, union arg *args)
2542 legacyfunc(struct swm_region *r, union arg *args)
2547 char name[SWM_FUNCNAME_LEN];
2548 void (*func)(struct swm_region *r, union arg *);
2550 } keyfuncs[kf_invalid + 1] = {
2551 /* name function argument */
2552 { "cycle_layout", cycle_layout, {0} },
2553 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
2554 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
2555 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
2556 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
2557 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
2558 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
2559 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
2560 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
2561 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
2562 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
2563 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
2564 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
2565 { "spawn_term", spawnterm, {.argv = spawn_term} },
2566 { "spawn_menu", legacyfunc, {0} },
2567 { "quit", quit, {0} },
2568 { "restart", restart, {0} },
2569 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
2570 { "ws_1", switchws, {.id = 0} },
2571 { "ws_2", switchws, {.id = 1} },
2572 { "ws_3", switchws, {.id = 2} },
2573 { "ws_4", switchws, {.id = 3} },
2574 { "ws_5", switchws, {.id = 4} },
2575 { "ws_6", switchws, {.id = 5} },
2576 { "ws_7", switchws, {.id = 6} },
2577 { "ws_8", switchws, {.id = 7} },
2578 { "ws_9", switchws, {.id = 8} },
2579 { "ws_10", switchws, {.id = 9} },
2580 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
2581 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
2582 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
2583 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
2584 { "mvws_1", send_to_ws, {.id = 0} },
2585 { "mvws_2", send_to_ws, {.id = 1} },
2586 { "mvws_3", send_to_ws, {.id = 2} },
2587 { "mvws_4", send_to_ws, {.id = 3} },
2588 { "mvws_5", send_to_ws, {.id = 4} },
2589 { "mvws_6", send_to_ws, {.id = 5} },
2590 { "mvws_7", send_to_ws, {.id = 6} },
2591 { "mvws_8", send_to_ws, {.id = 7} },
2592 { "mvws_9", send_to_ws, {.id = 8} },
2593 { "mvws_10", send_to_ws, {.id = 9} },
2594 { "bar_toggle", bar_toggle, {0} },
2595 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
2596 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
2597 { "screenshot_all", legacyfunc, {0} },
2598 { "screenshot_wind", legacyfunc, {0} },
2599 { "float_toggle", floating_toggle,{0} },
2600 { "version", version, {0} },
2601 { "spawn_lock", legacyfunc, {0} },
2602 { "spawn_initscr", legacyfunc, {0} },
2603 { "spawn_custom", dummykeyfunc, {0} },
2604 { "dumpwins", dumpwins, {0} },
2605 { "invalid key func", NULL, {0} },
2610 enum keyfuncid funcid;
2613 int keys_size = 0, keys_length = 0;
2614 struct key *keys = NULL;
2617 enum { client_click, root_click };
2619 unsigned int action;
2621 unsigned int button;
2622 void (*func)(struct ws_win *, union arg *);
2625 /* action key mouse button func args */
2626 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
2627 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
2628 { client_click, MODKEY, Button1, move, {0} },
2632 update_modkey(unsigned int mod)
2637 for (i = 0; i < keys_length; i++)
2638 if (keys[i].mod & ShiftMask)
2639 keys[i].mod = mod | ShiftMask;
2643 for (i = 0; i < LENGTH(buttons); i++)
2644 if (buttons[i].mask & ShiftMask)
2645 buttons[i].mask = mod | ShiftMask;
2647 buttons[i].mask = mod;
2657 int spawns_size = 0, spawns_length = 0;
2658 struct spawn_prog *spawns = NULL;
2661 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
2664 struct spawn_prog *prog = NULL;
2666 char *ap, **real_args;
2668 DNPRINTF(SWM_D_SPAWN, "spawn_custom %s\n", spawn_name);
2671 for (i = 0; i < spawns_length; i++) {
2672 if (!strcasecmp(spawn_name, spawns[i].name))
2676 fprintf(stderr, "spawn_custom: program %s not found\n",
2681 /* make room for expanded args */
2682 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
2683 err(1, "spawn_custom: calloc real_args");
2685 /* expand spawn_args into real_args */
2686 for (i = 0; i < prog->argc; i++) {
2688 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
2689 if (!strcasecmp(ap, "$bar_border")) {
2691 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
2693 err(1, "spawn_custom border color");
2694 } else if (!strcasecmp(ap, "$bar_color")) {
2696 strdup(r->s->c[SWM_S_COLOR_BAR].name))
2698 err(1, "spawn_custom bar color");
2699 } else if (!strcasecmp(ap, "$bar_font")) {
2700 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
2702 err(1, "spawn_custom bar fonts");
2703 } else if (!strcasecmp(ap, "$bar_font_color")) {
2705 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
2707 err(1, "spawn_custom color font");
2708 } else if (!strcasecmp(ap, "$color_focus")) {
2710 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
2712 err(1, "spawn_custom color focus");
2713 } else if (!strcasecmp(ap, "$color_unfocus")) {
2715 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
2717 err(1, "spawn_custom color unfocus");
2719 /* no match --> copy as is */
2720 if ((real_args[i] = strdup(ap)) == NULL)
2721 err(1, "spawn_custom strdup(ap)");
2723 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
2728 if ((swm_debug & SWM_D_SPAWN) != 0) {
2729 fprintf(stderr, "spawn_custom: result = ");
2730 for (i = 0; i < prog->argc; i++)
2731 fprintf(stderr, "\"%s\" ", real_args[i]);
2732 fprintf(stderr, "\n");
2738 for (i = 0; i < prog->argc; i++)
2744 setspawn(struct spawn_prog *prog)
2748 if (prog == NULL || prog->name == NULL)
2752 for (i = 0; i < spawns_length; i++) {
2753 if (!strcmp(spawns[i].name, prog->name)) {
2755 if (prog->argv == NULL) {
2757 DNPRINTF(SWM_D_SPAWN,
2758 "setspawn: delete #%d %s\n",
2760 free(spawns[i].name);
2761 for (j = 0; j < spawns[i].argc; j++)
2762 free(spawns[i].argv[j]);
2763 free(spawns[i].argv);
2764 j = spawns_length - 1;
2766 spawns[i] = spawns[j];
2771 DNPRINTF(SWM_D_SPAWN,
2772 "setspawn: replace #%d %s\n",
2774 free(spawns[i].name);
2775 for (j = 0; j < spawns[i].argc; j++)
2776 free(spawns[i].argv[j]);
2777 free(spawns[i].argv);
2780 /* found case handled */
2786 if (prog->argv == NULL) {
2788 "error: setspawn: cannot find program %s", prog->name);
2793 /* not found: add */
2794 if (spawns_size == 0 || spawns == NULL) {
2796 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
2797 spawns = malloc((size_t)spawns_size *
2798 sizeof(struct spawn_prog));
2799 if (spawns == NULL) {
2800 fprintf(stderr, "setspawn: malloc failed\n");
2804 } else if (spawns_length == spawns_size) {
2806 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
2807 spawns = realloc(spawns, (size_t)spawns_size *
2808 sizeof(struct spawn_prog));
2809 if (spawns == NULL) {
2810 fprintf(stderr, "setspawn: realloc failed\n");
2816 if (spawns_length < spawns_size) {
2817 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
2818 spawns_length, prog->name);
2819 i = spawns_length++;
2822 fprintf(stderr, "spawns array problem?\n");
2823 if (spawns == NULL) {
2824 fprintf(stderr, "spawns array is NULL!\n");
2832 setconfspawn(char *selector, char *value, int flags)
2834 struct spawn_prog *prog;
2835 char *vp, *cp, *word;
2837 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
2838 if ((prog = calloc(1, sizeof *prog)) == NULL)
2839 err(1, "setconfspawn: calloc prog");
2840 prog->name = strdup(selector);
2841 if (prog->name == NULL)
2842 err(1, "setconfspawn prog->name");
2843 if ((cp = vp = strdup(value)) == NULL)
2844 err(1, "setconfspawn: strdup(value) ");
2845 while ((word = strsep(&cp, " \t")) != NULL) {
2846 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
2848 cp += (long)strspn(cp, " \t");
2849 if (strlen(word) > 0) {
2851 prog->argv = realloc(prog->argv,
2852 prog->argc * sizeof(char *));
2853 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
2854 err(1, "setconfspawn: strdup");
2861 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
2868 setconfspawn("term", "xterm", 0);
2869 setconfspawn("screenshot_all", "screenshot.sh full", 0);
2870 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
2871 setconfspawn("lock", "xlock", 0);
2872 setconfspawn("initscr", "initscreen.sh", 0);
2873 setconfspawn("menu", "dmenu_run"
2876 " -nf $bar_font_color"
2878 " -sf $bar_color", 0);
2882 #define SWM_MODNAME_SIZE 32
2883 #define SWM_KEY_WS "\n+ \t"
2885 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
2889 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
2890 if (mod == NULL || ks == NULL) {
2891 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
2894 if (keystr == NULL || strlen(keystr) == 0) {
2895 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
2901 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
2902 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
2904 cp += (long)strspn(cp, SWM_KEY_WS);
2905 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
2907 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
2909 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
2911 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
2913 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
2915 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
2917 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
2918 *mod |= ControlMask;
2920 *ks = XStringToKeysym(name);
2921 XConvertCase(*ks, ks, &uks);
2922 if (ks == NoSymbol) {
2924 "parsekeys: invalid key %s\n",
2930 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
2935 strdupsafe(char *str)
2940 return (strdup(str));
2944 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
2947 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
2948 keyfuncs[kfid].name, spawn_name);
2950 for (i = 0; i < keys_length; i++) {
2951 if (keys[i].mod == mod && keys[i].keysym == ks) {
2952 if (kfid == kf_invalid) {
2955 "setkeybinding: delete #%d %s\n",
2956 i, keyfuncs[keys[i].funcid].name);
2957 free(keys[i].spawn_name);
2958 j = keys_length - 1;
2962 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2965 /* found: replace */
2967 "setkeybinding: replace #%d %s %s\n",
2968 i, keyfuncs[keys[i].funcid].name,
2970 free(keys[i].spawn_name);
2972 keys[i].keysym = ks;
2973 keys[i].funcid = kfid;
2974 keys[i].spawn_name = strdupsafe(spawn_name);
2975 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2980 if (kfid == kf_invalid) {
2982 "error: setkeybinding: cannot find mod/key combination");
2983 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2986 /* not found: add */
2987 if (keys_size == 0 || keys == NULL) {
2989 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
2990 keys = malloc((size_t)keys_size * sizeof(struct key));
2992 fprintf(stderr, "malloc failed\n");
2996 } else if (keys_length == keys_size) {
2998 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
2999 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
3001 fprintf(stderr, "realloc failed\n");
3006 if (keys_length < keys_size) {
3008 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
3009 j, keyfuncs[kfid].name, spawn_name);
3011 keys[j].keysym = ks;
3012 keys[j].funcid = kfid;
3013 keys[j].spawn_name = strdupsafe(spawn_name);
3015 fprintf(stderr, "keys array problem?\n");
3017 fprintf(stderr, "keys array problem\n");
3021 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3025 setconfbinding(char *selector, char *value, int flags)
3027 enum keyfuncid kfid;
3031 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
3032 if (selector == NULL) {
3033 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
3034 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3036 setkeybinding(mod, ks, kfid, NULL);
3041 /* search by key function name */
3042 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
3043 if (strncasecmp(selector, keyfuncs[kfid].name,
3044 SWM_FUNCNAME_LEN) == 0) {
3045 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3047 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3048 setkeybinding(mod, ks, kfid, NULL);
3054 /* search by custom spawn name */
3055 for (i = 0; i < spawns_length; i++) {
3056 if (strcasecmp(selector, spawns[i].name) == 0) {
3057 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3059 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3060 setkeybinding(mod, ks, kf_spawn_custom,
3067 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
3074 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
3075 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
3076 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
3077 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
3078 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
3079 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
3080 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
3081 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
3082 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
3083 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
3084 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
3085 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
3086 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
3087 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
3088 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
3089 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
3090 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
3091 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
3092 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
3093 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
3094 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
3095 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
3096 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
3097 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
3098 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
3099 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
3100 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
3101 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
3102 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
3103 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
3104 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
3105 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
3106 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
3107 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
3108 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
3109 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
3110 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
3111 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
3112 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
3113 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
3114 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
3115 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
3116 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
3117 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
3118 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
3119 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
3120 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
3121 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
3122 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
3123 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
3124 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
3125 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
3126 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
3128 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
3133 updatenumlockmask(void)
3136 XModifierKeymap *modmap;
3138 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
3140 modmap = XGetModifierMapping(display);
3141 for (i = 0; i < 8; i++)
3142 for (j = 0; j < modmap->max_keypermod; j++)
3143 if (modmap->modifiermap[i * modmap->max_keypermod + j]
3144 == XKeysymToKeycode(display, XK_Num_Lock))
3145 numlockmask = (1 << i);
3147 XFreeModifiermap(modmap);
3153 unsigned int i, j, k;
3155 unsigned int modifiers[] =
3156 { 0, LockMask, numlockmask, numlockmask | LockMask };
3158 DNPRINTF(SWM_D_MISC, "grabkeys\n");
3159 updatenumlockmask();
3161 for (k = 0; k < ScreenCount(display); k++) {
3162 if (TAILQ_EMPTY(&screens[k].rl))
3164 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
3165 for (i = 0; i < keys_length; i++) {
3166 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
3167 for (j = 0; j < LENGTH(modifiers); j++)
3168 XGrabKey(display, code,
3169 keys[i].mod | modifiers[j],
3170 screens[k].root, True,
3171 GrabModeAsync, GrabModeAsync);
3177 grabbuttons(struct ws_win *win, int focused)
3180 unsigned int modifiers[] =
3181 { 0, LockMask, numlockmask, numlockmask|LockMask };
3183 updatenumlockmask();
3184 XUngrabButton(display, AnyButton, AnyModifier, win->id);
3186 for (i = 0; i < LENGTH(buttons); i++)
3187 if (buttons[i].action == client_click)
3188 for (j = 0; j < LENGTH(modifiers); j++)
3189 XGrabButton(display, buttons[i].button,
3190 buttons[i].mask | modifiers[j],
3191 win->id, False, BUTTONMASK,
3192 GrabModeAsync, GrabModeSync, None,
3195 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
3196 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
3199 const char *quirkname[] = {
3200 "NONE", /* config string for "no value" */
3208 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
3209 #define SWM_Q_WS "\n|+ \t"
3211 parsequirks(char *qstr, unsigned long *quirk)
3221 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
3223 cp += (long)strspn(cp, SWM_Q_WS);
3224 for (i = 0; i < LENGTH(quirkname); i++) {
3225 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
3226 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
3231 *quirk |= 1 << (i-1);
3235 if (i >= LENGTH(quirkname)) {
3236 DNPRINTF(SWM_D_QUIRK,
3237 "parsequirks: invalid quirk [%s]\n", name);
3245 setquirk(const char *class, const char *name, const int quirk)
3250 for (i = 0; i < quirks_length; i++) {
3251 if (!strcmp(quirks[i].class, class) &&
3252 !strcmp(quirks[i].name, name)) {
3255 DNPRINTF(SWM_D_QUIRK,
3256 "setquirk: delete #%d %s:%s\n",
3257 i, quirks[i].class, quirks[i].name);
3258 free(quirks[i].class);
3259 free(quirks[i].name);
3260 j = quirks_length - 1;
3262 quirks[i] = quirks[j];
3266 /* found: replace */
3267 DNPRINTF(SWM_D_QUIRK,
3268 "setquirk: replace #%d %s:%s\n",
3269 i, quirks[i].class, quirks[i].name);
3270 free(quirks[i].class);
3271 free(quirks[i].name);
3272 quirks[i].class = strdup(class);
3273 quirks[i].name = strdup(name);
3274 quirks[i].quirk = quirk;
3281 "error: setquirk: cannot find class/name combination");
3284 /* not found: add */
3285 if (quirks_size == 0 || quirks == NULL) {
3287 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
3288 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
3290 fprintf(stderr, "setquirk: malloc failed\n");
3294 } else if (quirks_length == quirks_size) {
3296 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
3297 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
3299 fprintf(stderr, "setquirk: realloc failed\n");
3304 if (quirks_length < quirks_size) {
3305 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
3306 j = quirks_length++;
3307 quirks[j].class = strdup(class);
3308 quirks[j].name = strdup(name);
3309 quirks[j].quirk = quirk;
3311 fprintf(stderr, "quirks array problem?\n");
3313 fprintf(stderr, "quirks array problem!\n");
3320 setconfquirk(char *selector, char *value, int flags)
3322 char *cp, *class, *name;
3324 unsigned long quirks;
3325 if (selector == NULL)
3327 if ((cp = strchr(selector, ':')) == NULL)
3332 if ((retval = parsequirks(value, &quirks)) == 0)
3333 setquirk(class, name, quirks);
3340 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
3341 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
3342 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
3343 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
3344 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3345 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
3346 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3347 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3348 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3349 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3350 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
3351 setquirk("pcb", "pcb", SWM_Q_FLOAT);
3354 /* conf file stuff */
3355 #define SWM_CONF_FILE "scrotwm.conf"
3357 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_STACK_ENABLED,
3358 SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT, SWM_S_CYCLE_EMPTY,
3359 SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH,
3360 SWM_S_TITLE_CLASS_ENABLED, SWM_S_TITLE_NAME_ENABLED,
3361 SWM_S_FOCUS_MODE, SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM,
3362 SWM_S_SS_APP, SWM_S_DIALOG_RATIO
3366 setconfvalue(char *selector, char *value, int flags)
3369 case SWM_S_BAR_DELAY:
3370 bar_delay = atoi(value);
3372 case SWM_S_BAR_ENABLED:
3373 bar_enabled = atoi(value);
3375 case SWM_S_STACK_ENABLED:
3376 stack_enabled = atoi(value);
3378 case SWM_S_CLOCK_ENABLED:
3379 clock_enabled = atoi(value);
3381 case SWM_S_CLOCK_FORMAT:
3382 #ifndef SWM_DENY_CLOCK_FORMAT
3384 if ((clock_format = strdup(value)) == NULL)
3385 err(1, "setconfvalue: clock_format");
3388 case SWM_S_CYCLE_EMPTY:
3389 cycle_empty = atoi(value);
3391 case SWM_S_CYCLE_VISIBLE:
3392 cycle_visible = atoi(value);
3394 case SWM_S_SS_ENABLED:
3395 ss_enabled = atoi(value);
3397 case SWM_S_TERM_WIDTH:
3398 term_width = atoi(value);
3400 case SWM_S_TITLE_CLASS_ENABLED:
3401 title_class_enabled = atoi(value);
3403 case SWM_S_TITLE_NAME_ENABLED:
3404 title_name_enabled = atoi(value);
3406 case SWM_S_FOCUS_MODE:
3407 if (!strcmp(value, "default"))
3408 focus_mode = SWM_FOCUS_DEFAULT;
3409 else if (!strcmp(value, "follow_cursor"))
3410 focus_mode = SWM_FOCUS_FOLLOW;
3411 else if (!strcmp(value, "synergy"))
3412 focus_mode = SWM_FOCUS_SYNERGY;
3414 err(1, "focus_mode");
3416 case SWM_S_BAR_FONT:
3418 if ((bar_fonts[0] = strdup(value)) == NULL)
3419 err(1, "setconfvalue: bar_font");
3421 case SWM_S_BAR_ACTION:
3423 if ((bar_argv[0] = strdup(value)) == NULL)
3424 err(1, "setconfvalue: bar_action");
3426 case SWM_S_SPAWN_TERM:
3427 free(spawn_term[0]);
3428 if ((spawn_term[0] = strdup(value)) == NULL)
3429 err(1, "setconfvalue: spawn_term");
3433 case SWM_S_DIALOG_RATIO:
3434 dialog_ratio = atof(value);
3435 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
3445 setconfmodkey(char *selector, char *value, int flags)
3447 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
3448 update_modkey(Mod1Mask);
3449 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
3450 update_modkey(Mod2Mask);
3451 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
3452 update_modkey(Mod3Mask);
3453 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
3454 update_modkey(Mod4Mask);
3461 setconfcolor(char *selector, char *value, int flags)
3463 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
3468 setconfregion(char *selector, char *value, int flags)
3470 custom_region(value);
3474 /* config options */
3475 struct config_option {
3477 int (*func)(char*, char*, int);
3480 struct config_option configopt[] = {
3481 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
3482 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
3483 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
3484 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
3485 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
3486 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
3487 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
3488 { "bind", setconfbinding, 0 },
3489 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
3490 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
3491 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
3492 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
3493 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
3494 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
3495 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
3496 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
3497 { "modkey", setconfmodkey, 0 },
3498 { "program", setconfspawn, 0 },
3499 { "quirk", setconfquirk, 0 },
3500 { "region", setconfregion, 0 },
3501 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
3502 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
3503 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
3504 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
3505 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
3506 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
3507 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
3512 conf_load(char *filename)
3515 char *line, *cp, *optsub, *optval;
3516 size_t linelen, lineno = 0;
3517 int wordlen, i, optind;
3518 struct config_option *opt;
3520 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
3522 if (filename == NULL) {
3523 fprintf(stderr, "conf_load: no filename\n");
3526 if ((config = fopen(filename, "r")) == NULL) {
3527 warn("conf_load: fopen");
3531 while (!feof(config)) {
3532 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
3535 err(1, "%s", filename);
3540 cp += strspn(cp, " \t\n"); /* eat whitespace */
3541 if (cp[0] == '\0') {
3546 /* get config option */
3547 wordlen = strcspn(cp, "=[ \t\n");
3549 warnx("%s: line %zd: no option found",
3554 for (i = 0; i < LENGTH(configopt); i++) {
3555 opt = &configopt[i];
3556 if (!strncasecmp(cp, opt->optname, wordlen) &&
3557 strlen(opt->optname) == wordlen) {
3563 warnx("%s: line %zd: unknown option %.*s",
3564 filename, lineno, wordlen, cp);
3568 cp += strspn(cp, " \t\n"); /* eat whitespace */
3569 /* get [selector] if any */
3573 wordlen = strcspn(cp, "]");
3576 warnx("%s: line %zd: syntax error",
3580 asprintf(&optsub, "%.*s", wordlen, cp);
3583 cp += strspn(cp, "] \t\n"); /* eat trailing */
3585 cp += strspn(cp, "= \t\n"); /* eat trailing */
3587 optval = strdup(cp);
3588 /* call function to deal with it all */
3589 if (configopt[optind].func(optsub, optval,
3590 configopt[optind].funcflags) != 0) {
3591 fprintf(stderr, "%s line %zd: %s\n",
3592 filename, lineno, line);
3593 errx(1, "%s: line %zd: invalid data for %s",
3594 filename, lineno, configopt[optind].optname);
3602 DNPRINTF(SWM_D_CONF, "conf_load end\n");
3608 set_child_transient(struct ws_win *win)
3610 struct ws_win *parent;
3612 parent = find_window(win->transient);
3614 parent->child_trans = win;
3618 manage_window(Window id)
3621 struct workspace *ws;
3622 struct ws_win *win, *ww;
3623 int format, i, ws_idx, n, border_me = 0;
3624 unsigned long nitems, bytes;
3625 Atom ws_idx_atom = 0, type;
3626 Atom *prot = NULL, *pp;
3627 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
3628 struct swm_region *r;
3633 if ((win = find_window(id)) != NULL)
3634 return (win); /* already being managed */
3636 /* see if we are on the unmanaged list */
3637 if ((win = find_unmanaged_window(id)) != NULL) {
3638 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
3640 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
3641 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
3643 set_child_transient(win);
3647 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
3648 errx(1, "calloc: failed to allocate memory for new window");
3650 /* Get all the window data in one shot */
3651 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3653 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
3654 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
3655 XGetWindowAttributes(display, id, &win->wa);
3656 XGetWMNormalHints(display, id, &win->sh, &mask);
3657 XGetTransientForHint(display, id, &trans);
3659 win->transient = trans;
3660 set_child_transient(win);
3661 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
3662 win->id, win->transient);
3664 /* get supported protocols */
3665 if (XGetWMProtocols(display, id, &prot, &n)) {
3666 for (i = 0, pp = prot; i < n; i++, pp++) {
3667 if (*pp == takefocus)
3668 win->take_focus = 1;
3670 win->can_delete = 1;
3677 * Figure out where to put the window. If it was previously assigned to
3678 * a workspace (either by spawn() or manually moving), and isn't
3679 * transient, * put it in the same workspace
3681 r = root_to_region(win->wa.root);
3682 if (prop && win->transient == 0) {
3683 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
3684 ws_idx = strtonum(prop, 0, 9, &errstr);
3686 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
3689 ws = &r->s->ws[ws_idx];
3692 /* this should launch transients in the same ws as parent */
3694 if ((ww = find_window(trans)) != NULL)
3701 "fix this bug mcbride\n");
3706 /* set up the window layout */
3709 win->s = r->s; /* this never changes */
3710 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
3712 win->g.w = win->wa.width;
3713 win->g.h = win->wa.height;
3714 win->g.x = win->wa.x;
3715 win->g.y = win->wa.y;
3717 /* Set window properties so we can remember this after reincarnation */
3718 if (ws_idx_atom && prop == NULL &&
3719 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
3720 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3722 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3723 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3727 if (XGetClassHint(display, win->id, &win->ch)) {
3728 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
3729 win->ch.res_class, win->ch.res_name);
3731 /* java is retarded so treat it special */
3732 if (strstr(win->ch.res_name, "sun-awt")) {
3737 for (i = 0; i < quirks_length; i++){
3738 if (!strcmp(win->ch.res_class, quirks[i].class) &&
3739 !strcmp(win->ch.res_name, quirks[i].name)) {
3740 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
3741 win->ch.res_class, win->ch.res_name);
3742 if (quirks[i].quirk & SWM_Q_FLOAT) {
3746 win->quirks = quirks[i].quirk;
3751 /* alter window position if quirky */
3752 if (win->quirks & SWM_Q_ANYWHERE) {
3753 win->manual = 1; /* don't center the quirky windows */
3754 bzero(&wc, sizeof wc);
3756 if (win->g.y < bar_height) {
3757 win->g.y = wc.y = bar_height;
3760 if (win->g.w + win->g.x > WIDTH(r)) {
3761 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
3767 /* Reset font sizes (the bruteforce way; no default keybinding). */
3768 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
3769 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3770 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3771 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3772 fake_keypress(win, XK_KP_Add, ShiftMask);
3777 bzero(&wc, sizeof wc);
3778 wc.border_width = 1;
3779 mask = CWBorderWidth;
3780 XConfigureWindow(display, win->id, mask, &wc);
3784 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
3785 PropertyChangeMask | StructureNotifyMask);
3787 set_win_state(win, NormalState);
3789 /* floaters need to be mapped if they are in the current workspace */
3790 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
3791 XMapRaised(display, win->id);
3797 free_window(struct ws_win *win)
3799 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
3804 /* needed for restart wm */
3805 set_win_state(win, WithdrawnState);
3807 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
3809 if (win->ch.res_class)
3810 XFree(win->ch.res_class);
3811 if (win->ch.res_name)
3812 XFree(win->ch.res_name);
3817 memset(win, 0xff, sizeof *win); /* XXX kill later */
3823 unmanage_window(struct ws_win *win)
3825 struct ws_win *parent;
3830 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
3832 if (win->transient) {
3833 parent = find_window(win->transient);
3835 parent->child_trans = NULL;
3838 /* work around for mplayer going full screen */
3842 TAILQ_REMOVE(&win->ws->winlist, win, entry);
3843 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
3849 focus_magic(struct ws_win *win, int do_trans)
3851 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu %d\n", WINID(win), do_trans);
3856 if (do_trans == SWM_F_TRANSIENT && win->child_trans) {
3857 /* win = parent & has a transient so focus on that */
3859 focus_win(win->child_trans);
3860 if (win->child_trans->take_focus)
3861 client_msg(win, takefocus);
3863 focus_win(win->child_trans);
3864 if (win->child_trans->take_focus)
3865 client_msg(win->child_trans, takefocus);
3870 if (win->take_focus)
3871 client_msg(win, takefocus);
3878 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
3886 XKeyEvent *ev = &e->xkey;
3888 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
3890 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
3891 for (i = 0; i < keys_length; i++)
3892 if (keysym == keys[i].keysym
3893 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
3894 && keyfuncs[keys[i].funcid].func) {
3895 if (keys[i].funcid == kf_spawn_custom)
3897 root_to_region(ev->root),
3898 &(keyfuncs[keys[i].funcid].args),
3902 keyfuncs[keys[i].funcid].func(
3903 root_to_region(ev->root),
3904 &(keyfuncs[keys[i].funcid].args)
3910 buttonpress(XEvent *e)
3914 XButtonPressedEvent *ev = &e->xbutton;
3916 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
3918 action = root_click;
3919 if ((win = find_window(ev->window)) == NULL)
3922 focus_magic(win, SWM_F_TRANSIENT);
3923 action = client_click;
3925 for (i = 0; i < LENGTH(buttons); i++)
3926 if (action == buttons[i].action && buttons[i].func &&
3927 buttons[i].button == ev->button &&
3928 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
3929 buttons[i].func(win, &buttons[i].args);
3933 configurerequest(XEvent *e)
3935 XConfigureRequestEvent *ev = &e->xconfigurerequest;
3940 if ((win = find_window(ev->window)) == NULL)
3941 if ((win = find_unmanaged_window(ev->window)) == NULL)
3945 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
3947 bzero(&wc, sizeof wc);
3950 wc.width = ev->width;
3951 wc.height = ev->height;
3952 wc.border_width = ev->border_width;
3953 wc.sibling = ev->above;
3954 wc.stack_mode = ev->detail;
3955 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
3957 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
3959 if (win->floating) {
3960 if (ev->value_mask & CWX)
3962 if (ev->value_mask & CWY)
3964 if (ev->value_mask & CWWidth)
3965 win->g.w = ev->width;
3966 if (ev->value_mask & CWHeight)
3967 win->g.h = ev->height;
3974 configurenotify(XEvent *e)
3979 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
3980 e->xconfigure.window);
3982 win = find_window(e->xconfigure.window);
3984 XGetWMNormalHints(display, win->id, &win->sh, &mask);
3992 destroynotify(XEvent *e)
3995 XDestroyWindowEvent *ev = &e->xdestroywindow;
3997 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
3999 if ((win = find_window(ev->window)) == NULL) {
4000 if ((win = find_unmanaged_window(ev->window)) == NULL)
4006 /* make sure we focus on something */
4009 unmanage_window(win);
4015 enternotify(XEvent *e)
4017 XCrossingEvent *ev = &e->xcrossing;
4022 Window focus_return;
4023 int revert_to_return;
4025 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
4026 "%lu subwindow %lu same_screen %d focus %d state %d\n",
4027 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
4028 ev->same_screen, ev->focus, ev->state);
4030 switch (focus_mode) {
4031 case SWM_FOCUS_DEFAULT:
4032 if (QLength(display)) {
4033 DNPRINTF(SWM_D_EVENT, "ignore enternotify %d\n",
4038 case SWM_FOCUS_FOLLOW:
4040 case SWM_FOCUS_SYNERGY:
4043 * all these checks need to be in this order because the
4044 * XCheckTypedWindowEvent relies on weeding out the previous events
4046 * making this code an option would enable a follow mouse for focus
4051 * state is set when we are switching workspaces and focus is set when
4052 * the window or a subwindow already has focus (occurs during restart).
4054 * Only honor the focus flag if last_focus_event is not FocusOut,
4055 * this allows scrotwm to continue to control focus when another
4056 * program is also playing with it.
4058 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
4059 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
4064 * happens when a window is created or destroyed and the border
4065 * crosses the mouse pointer and when switching ws
4067 * we need the subwindow test to see if we came from root in order
4068 * to give focus to floaters
4070 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
4071 ev->subwindow == 0) {
4072 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
4076 /* this window already has focus */
4077 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
4078 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
4082 /* this window is being deleted or moved to another ws */
4083 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
4085 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
4086 XPutBackEvent(display, &cne);
4090 if ((win = find_window(ev->window)) == NULL) {
4091 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
4096 * In fullstack kill all enters unless they come from a different ws
4097 * (i.e. another region) or focus has been grabbed externally.
4099 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
4100 last_focus_event != FocusOut) {
4101 XGetInputFocus(display, &focus_return, &revert_to_return);
4102 if ((w = find_window(focus_return)) == NULL ||
4104 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
4112 if ((win = find_window(ev->window)) == NULL) {
4113 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
4118 * if we have more enternotifies let them handle it in due time
4120 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
4121 DNPRINTF(SWM_D_EVENT,
4122 "ignoring enternotify: got more enternotify\n");
4123 XPutBackEvent(display, &cne);
4127 focus_magic(win, SWM_F_TRANSIENT);
4130 /* lets us use one switch statement for arbitrary mode/detail combinations */
4131 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
4134 focusevent(XEvent *e)
4136 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
4137 ev->type == FocusIn ? "entering" : "leaving",
4138 ev->window, ev->mode, ev->detail);
4141 u_int32_t mode_detail;
4142 XFocusChangeEvent *ev = &e->xfocus;
4144 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
4145 ev->type == FocusIn ? "entering" : "leaving",
4146 ev->window, ev->mode, ev->detail);
4148 if (last_focus_event == ev->type) {
4149 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
4153 last_focus_event = ev->type;
4154 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
4156 switch (mode_detail) {
4157 /* synergy client focus operations */
4158 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
4159 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
4161 /* synergy server focus operations */
4162 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
4164 /* Entering applications like rdesktop that mangle the pointer */
4165 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
4167 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
4168 XSetWindowBorder(display, win->id,
4169 win->ws->r->s->c[ev->type == FocusIn ?
4170 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
4173 fprintf(stderr, "ignoring focusevent\n");
4174 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
4181 mapnotify(XEvent *e)
4184 XMapEvent *ev = &e->xmap;
4186 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
4188 win = manage_window(ev->window);
4190 set_win_state(win, NormalState);
4194 mappingnotify(XEvent *e)
4196 XMappingEvent *ev = &e->xmapping;
4198 XRefreshKeyboardMapping(ev);
4199 if (ev->request == MappingKeyboard)
4204 maprequest(XEvent *e)
4207 struct swm_region *r;
4208 XWindowAttributes wa;
4209 XMapRequestEvent *ev = &e->xmaprequest;
4211 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
4212 e->xmaprequest.window);
4214 if (!XGetWindowAttributes(display, ev->window, &wa))
4216 if (wa.override_redirect)
4219 win = manage_window(e->xmaprequest.window);
4221 return; /* can't happen */
4225 /* make new win focused */
4226 r = root_to_region(win->wa.root);
4227 if (win->ws == r->ws)
4228 focus_magic(win, SWM_F_GENERIC);
4232 propertynotify(XEvent *e)
4235 XPropertyEvent *ev = &e->xproperty;
4237 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
4240 if (ev->state == PropertyDelete)
4241 return; /* ignore */
4242 win = find_window(ev->window);
4247 case XA_WM_NORMAL_HINTS:
4250 XGetWMNormalHints(display, win->id, &win->sh, &mask);
4251 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
4252 if (win->sh.flags & PMinSize) {
4253 win->g.w = win->sh.min_width;
4254 win->g.h = win->sh.min_height;
4255 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
4257 XMoveResizeWindow(display, win->id,
4258 win->g.x, win->g.y, win->g.w, win->g.h);
4267 unmapnotify(XEvent *e)
4271 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
4273 /* determine if we need to help unmanage this window */
4274 win = find_window(e->xunmap.window);
4278 if (getstate(e->xunmap.window) == NormalState) {
4279 unmanage_window(win);
4285 visibilitynotify(XEvent *e)
4288 struct swm_region *r;
4290 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
4291 e->xvisibility.window);
4292 if (e->xvisibility.state == VisibilityUnobscured)
4293 for (i = 0; i < ScreenCount(display); i++)
4294 TAILQ_FOREACH(r, &screens[i].rl, entry)
4295 if (e->xvisibility.window == r->bar_window)
4300 xerror_start(Display *d, XErrorEvent *ee)
4307 xerror(Display *d, XErrorEvent *ee)
4309 /* fprintf(stderr, "error: %p %p\n", display, ee); */
4317 xerrorxlib = XSetErrorHandler(xerror_start);
4319 /* this causes an error if some other window manager is running */
4320 XSelectInput(display, DefaultRootWindow(display),
4321 SubstructureRedirectMask);
4322 XSync(display, False);
4326 XSetErrorHandler(xerror);
4327 XSync(display, False);
4332 new_region(struct swm_screen *s, int x, int y, int w, int h)
4334 struct swm_region *r, *n;
4335 struct workspace *ws = NULL;
4338 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
4339 s->idx, w, h, x, y);
4341 /* remove any conflicting regions */
4342 n = TAILQ_FIRST(&s->rl);
4345 n = TAILQ_NEXT(r, entry);
4346 if (X(r) < (x + w) &&
4347 (X(r) + WIDTH(r)) > x &&
4349 (Y(r) + HEIGHT(r)) > y) {
4350 XDestroyWindow(display, r->bar_window);
4351 TAILQ_REMOVE(&s->rl, r, entry);
4352 TAILQ_INSERT_TAIL(&s->orl, r, entry);
4356 /* search old regions for one to reuse */
4358 /* size + location match */
4359 TAILQ_FOREACH(r, &s->orl, entry)
4360 if (X(r) == x && Y(r) == y &&
4361 HEIGHT(r) == h && WIDTH(r) == w)
4365 TAILQ_FOREACH(r, &s->orl, entry)
4366 if (HEIGHT(r) == h && WIDTH(r) == w)
4370 TAILQ_REMOVE(&s->orl, r, entry);
4371 /* try to use old region's workspace */
4372 if (r->ws->r == NULL)
4375 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
4376 errx(1, "calloc: failed to allocate memory for screen");
4378 /* if we don't have a workspace already, find one */
4380 for (i = 0; i < SWM_WS_MAX; i++)
4381 if (s->ws[i].r == NULL) {
4388 errx(1, "no free workspaces\n");
4397 TAILQ_INSERT_TAIL(&s->rl, r, entry);
4403 #ifdef SWM_XRR_HAS_CRTC
4405 XRRScreenResources *sr;
4408 #endif /* SWM_XRR_HAS_CRTC */
4409 struct swm_region *r;
4412 if (i >= ScreenCount(display))
4413 errx(1, "invalid screen");
4415 /* remove any old regions */
4416 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
4417 r->ws->old_r = r->ws->r = NULL;
4418 XDestroyWindow(display, r->bar_window);
4419 TAILQ_REMOVE(&screens[i].rl, r, entry);
4420 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
4423 /* map virtual screens onto physical screens */
4424 #ifdef SWM_XRR_HAS_CRTC
4426 if (xrandr_support) {
4427 sr = XRRGetScreenResources(display, screens[i].root);
4429 new_region(&screens[i], 0, 0,
4430 DisplayWidth(display, i),
4431 DisplayHeight(display, i));
4435 for (c = 0, ci = NULL; c < ncrtc; c++) {
4436 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
4437 if (ci->noutput == 0)
4441 if (ci != NULL && ci->mode == None)
4442 new_region(&screens[i], 0, 0,
4443 DisplayWidth(display, i),
4444 DisplayHeight(display, i));
4446 new_region(&screens[i],
4447 ci->x, ci->y, ci->width, ci->height);
4450 XRRFreeCrtcInfo(ci);
4451 XRRFreeScreenResources(sr);
4453 #endif /* SWM_XRR_HAS_CRTC */
4455 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
4456 DisplayHeight(display, i));
4461 screenchange(XEvent *e) {
4462 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
4463 struct swm_region *r;
4466 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
4468 if (!XRRUpdateConfiguration(e))
4471 /* silly event doesn't include the screen index */
4472 for (i = 0; i < ScreenCount(display); i++)
4473 if (screens[i].root == xe->root)
4475 if (i >= ScreenCount(display))
4476 errx(1, "screenchange: screen not found\n");
4478 /* brute force for now, just re-enumerate the regions */
4481 /* add bars to all regions */
4482 for (i = 0; i < ScreenCount(display); i++)
4483 TAILQ_FOREACH(r, &screens[i].rl, entry)
4491 Window d1, d2, *wins = NULL;
4492 XWindowAttributes wa;
4495 int errorbase, major, minor;
4496 struct workspace *ws;
4500 if ((screens = calloc(ScreenCount(display),
4501 sizeof(struct swm_screen))) == NULL)
4502 errx(1, "calloc: screens");
4504 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4506 /* initial Xrandr setup */
4507 xrandr_support = XRRQueryExtension(display,
4508 &xrandr_eventbase, &errorbase);
4510 if (XRRQueryVersion(display, &major, &minor) && major < 1)
4513 /* map physical screens */
4514 for (i = 0; i < ScreenCount(display); i++) {
4515 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
4517 TAILQ_INIT(&screens[i].rl);
4518 TAILQ_INIT(&screens[i].orl);
4519 screens[i].root = RootWindow(display, i);
4521 /* set default colors */
4522 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
4523 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
4524 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
4525 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
4526 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
4528 /* init all workspaces */
4529 /* XXX these should be dynamically allocated too */
4530 for (j = 0; j < SWM_WS_MAX; j++) {
4531 ws = &screens[i].ws[j];
4536 TAILQ_INIT(&ws->winlist);
4537 TAILQ_INIT(&ws->unmanagedlist);
4539 for (k = 0; layouts[k].l_stack != NULL; k++)
4540 if (layouts[k].l_config != NULL)
4541 layouts[k].l_config(ws,
4542 SWM_ARG_ID_STACKINIT);
4543 ws->cur_layout = &layouts[0];
4545 /* grab existing windows (before we build the bars)*/
4546 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
4552 XRRSelectInput(display, screens[i].root,
4553 RRScreenChangeNotifyMask);
4555 /* attach windows to a region */
4556 /* normal windows */
4557 for (j = 0; j < no; j++) {
4558 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4559 wa.override_redirect ||
4560 XGetTransientForHint(display, wins[j], &d1))
4563 state = getstate(wins[j]);
4564 manage = state == IconicState;
4565 if (wa.map_state == IsViewable || manage)
4566 manage_window(wins[j]);
4568 /* transient windows */
4569 for (j = 0; j < no; j++) {
4570 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4571 wa.override_redirect)
4574 state = getstate(wins[j]);
4575 manage = state == IconicState;
4576 if (XGetTransientForHint(display, wins[j], &d1) &&
4578 manage_window(wins[j]);
4590 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
4592 err(1, "setup_globals: strdup");
4593 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
4595 err(1, "setup_globals: strdup");
4596 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
4598 err(1, "setup_globals: strdup");
4599 if ((spawn_term[0] = strdup("xterm")) == NULL)
4600 err(1, "setup_globals: strdup");
4601 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
4609 Atom netwmcheck, netwmname, utf8_string;
4612 /* work around sun jdk bugs, code from wmname */
4613 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
4614 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
4615 utf8_string = XInternAtom(display, "UTF8_STRING", False);
4616 for (i = 0; i < ScreenCount(display); i++) {
4617 root = screens[i].root;
4618 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
4619 PropModeReplace, (unsigned char *)&root, 1);
4620 XChangeProperty(display, root, netwmname, utf8_string, 8,
4621 PropModeReplace, "LG3D", strlen("LG3D"));
4626 main(int argc, char *argv[])
4629 struct swm_region *r, *rr;
4630 struct ws_win *winfocus = NULL;
4633 char conf[PATH_MAX], *cfile = NULL;
4640 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
4641 SWM_VERSION, cvstag);
4642 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
4643 warnx("no locale support");
4645 if (!(display = XOpenDisplay(0)))
4646 errx(1, "can not open display");
4649 errx(1, "other wm running");
4651 /* handle some signale */
4652 installsignal(SIGINT, "INT");
4653 installsignal(SIGHUP, "HUP");
4654 installsignal(SIGQUIT, "QUIT");
4655 installsignal(SIGTERM, "TERM");
4656 installsignal(SIGCHLD, "CHLD");
4658 astate = XInternAtom(display, "WM_STATE", False);
4659 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
4660 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
4661 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
4663 /* look for local and global conf file */
4664 pwd = getpwuid(getuid());
4666 errx(1, "invalid user %d", getuid());
4674 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
4675 if (stat(conf, &sb) != -1) {
4676 if (S_ISREG(sb.st_mode))
4679 /* try global conf file */
4680 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
4681 if (!stat(conf, &sb))
4682 if (S_ISREG(sb.st_mode))
4688 /* setup all bars */
4689 for (i = 0; i < ScreenCount(display); i++)
4690 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4691 if (winfocus == NULL)
4692 winfocus = TAILQ_FIRST(&r->ws->winlist);
4696 /* set some values to work around bad programs */
4704 xfd = ConnectionNumber(display);
4706 while (XPending(display)) {
4707 XNextEvent(display, &e);
4710 if (e.type < LASTEvent) {
4712 if (handler[e.type])
4713 handler[e.type](&e);
4715 DNPRINTF(SWM_D_EVENT,
4716 "win: %lu unknown event: %d\n",
4717 e.xany.window, e.type);
4719 switch (e.type - xrandr_eventbase) {
4720 case RRScreenChangeNotify:
4724 DNPRINTF(SWM_D_EVENT,
4725 "win: %lu unknown xrandr event: "
4726 "%d\n", e.xany.window, e.type);
4732 /* if we are being restarted go focus on first window */
4734 rr = winfocus->ws->r;
4736 /* not a visible window */
4740 /* move pointer to first screen if multi screen */
4741 if (ScreenCount(display) > 1 || outputs > 1)
4742 XWarpPointer(display, None, rr->s[0].root,
4743 0, 0, 0, 0, rr->g.x,
4744 rr->g.y + bar_enabled ? bar_height : 0);
4746 a.id = SWM_ARG_ID_FOCUSCUR;
4756 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
4758 DNPRINTF(SWM_D_MISC, "select failed");
4768 XFreeGC(display, bar_gc);
4769 XCloseDisplay(display);