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 workspace *ws_prior; /* prior workspace on this region */
240 struct swm_screen *s; /* screen idx */
243 TAILQ_HEAD(swm_region_list, swm_region);
246 TAILQ_ENTRY(ws_win) entry;
249 struct ws_win *child_trans; /* transient child window */
250 struct swm_geometry g;
253 int font_size_boundary[SWM_MAX_FONT_STEPS];
259 unsigned long quirks;
260 struct workspace *ws; /* always valid */
261 struct swm_screen *s; /* always valid, never changes */
262 XWindowAttributes wa;
266 TAILQ_HEAD(ws_win_list, ws_win);
268 /* layout handlers */
270 void vertical_config(struct workspace *, int);
271 void vertical_stack(struct workspace *, struct swm_geometry *);
272 void horizontal_config(struct workspace *, int);
273 void horizontal_stack(struct workspace *, struct swm_geometry *);
274 void max_stack(struct workspace *, struct swm_geometry *);
276 struct ws_win *find_window(Window);
278 void grabbuttons(struct ws_win *, int);
279 void new_region(struct swm_screen *, int, int, int, int);
280 void unmanage_window(struct ws_win *);
281 long getstate(Window);
284 void (*l_stack)(struct workspace *, struct swm_geometry *);
285 void (*l_config)(struct workspace *, int);
287 #define SWM_L_FOCUSPREV (1<<0)
288 #define SWM_L_MAPONFOCUS (1<<1)
291 /* stack, configure */
292 { vertical_stack, vertical_config, 0, "[|]" },
293 { horizontal_stack, horizontal_config, 0, "[-]" },
295 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, "[ ]"},
296 { NULL, NULL, 0, NULL },
299 #define SWM_H_SLICE (32)
300 #define SWM_V_SLICE (32)
302 /* define work spaces */
304 int idx; /* workspace index */
305 struct layout *cur_layout; /* current layout handlers */
306 struct ws_win *focus; /* may be NULL */
307 struct ws_win *focus_prev; /* may be NULL */
308 struct swm_region *r; /* may be NULL */
309 struct swm_region *old_r; /* may be NULL */
310 struct ws_win_list winlist; /* list of windows in ws */
311 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
315 int horizontal_msize;
317 int horizontal_stacks;
324 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
325 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
327 /* physical screen mapping */
328 #define SWM_WS_MAX (10)
330 int idx; /* screen index */
331 struct swm_region_list rl; /* list of regions on this screen */
332 struct swm_region_list orl; /* list of old regions */
334 struct workspace ws[SWM_WS_MAX];
340 } c[SWM_S_COLOR_MAX];
342 struct swm_screen *screens;
345 /* args to functions */
348 #define SWM_ARG_ID_FOCUSNEXT (0)
349 #define SWM_ARG_ID_FOCUSPREV (1)
350 #define SWM_ARG_ID_FOCUSMAIN (2)
351 #define SWM_ARG_ID_FOCUSCUR (4)
352 #define SWM_ARG_ID_SWAPNEXT (10)
353 #define SWM_ARG_ID_SWAPPREV (11)
354 #define SWM_ARG_ID_SWAPMAIN (12)
355 #define SWM_ARG_ID_MASTERSHRINK (20)
356 #define SWM_ARG_ID_MASTERGROW (21)
357 #define SWM_ARG_ID_MASTERADD (22)
358 #define SWM_ARG_ID_MASTERDEL (23)
359 #define SWM_ARG_ID_STACKRESET (30)
360 #define SWM_ARG_ID_STACKINIT (31)
361 #define SWM_ARG_ID_CYCLEWS_UP (40)
362 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
363 #define SWM_ARG_ID_CYCLESC_UP (42)
364 #define SWM_ARG_ID_CYCLESC_DOWN (43)
365 #define SWM_ARG_ID_STACKINC (50)
366 #define SWM_ARG_ID_STACKDEC (51)
367 #define SWM_ARG_ID_SS_ALL (60)
368 #define SWM_ARG_ID_SS_WINDOW (61)
369 #define SWM_ARG_ID_DONTCENTER (70)
370 #define SWM_ARG_ID_CENTER (71)
371 #define SWM_ARG_ID_KILLWINDOW (80)
372 #define SWM_ARG_ID_DELETEWINDOW (81)
376 void focus(struct swm_region *, union arg *);
377 void focus_magic(struct ws_win *, int);
378 #define SWM_F_GENERIC (0)
379 #define SWM_F_TRANSIENT (1)
385 #define SWM_Q_FLOAT (1<<0) /* float this window */
386 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
387 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
388 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
389 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
391 int quirks_size = 0, quirks_length = 0;
392 struct quirk *quirks = NULL;
409 name = "ButtonPress";
412 name = "ButtonRelease";
415 name = "MotionNotify";
418 name = "EnterNotify";
421 name = "LeaveNotify";
430 name = "KeymapNotify";
436 name = "GraphicsExpose";
441 case VisibilityNotify:
442 name = "VisibilityNotify";
445 name = "CreateNotify";
448 name = "DestroyNotify";
451 name = "UnmapNotify";
460 name = "ReparentNotify";
462 case ConfigureNotify:
463 name = "ConfigureNotify";
465 case ConfigureRequest:
466 name = "ConfigureRequest";
469 name = "GravityNotify";
472 name = "ResizeRequest";
474 case CirculateNotify:
475 name = "CirculateNotify";
477 case CirculateRequest:
478 name = "CirculateRequest";
481 name = "PropertyNotify";
484 name = "SelectionClear";
486 case SelectionRequest:
487 name = "SelectionRequest";
489 case SelectionNotify:
490 name = "SelectionNotify";
493 name = "ColormapNotify";
496 name = "ClientMessage";
499 name = "MappingNotify";
504 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
506 e->xany.window, name, e->type, QLength(display));
508 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
510 e->xany.window, e->type, QLength(display));
514 dumpwins(struct swm_region *r, union arg *args)
518 XWindowAttributes wa;
521 fprintf(stderr, "invalid workspace\n");
525 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
527 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
528 state = getstate(win->id);
529 if (!XGetWindowAttributes(display, win->id, &wa))
530 fprintf(stderr, "window: %lu failed "
531 "XGetWindowAttributes\n", win->id);
532 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
533 win->id, wa.map_state, state);
536 fprintf(stderr, "===== unmanaged window list =====\n");
537 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
538 state = getstate(win->id);
539 if (!XGetWindowAttributes(display, win->id, &wa))
540 fprintf(stderr, "window: %lu failed "
541 "XGetWindowAttributes\n", win->id);
542 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
543 win->id, wa.map_state, state);
546 fprintf(stderr, "=================================\n");
551 dumpwins(struct swm_region *r, union arg *args)
554 #endif /* SWM_DEBUG */
556 void expose(XEvent *);
557 void keypress(XEvent *);
558 void buttonpress(XEvent *);
559 void configurerequest(XEvent *);
560 void configurenotify(XEvent *);
561 void destroynotify(XEvent *);
562 void enternotify(XEvent *);
563 void focusevent(XEvent *);
564 void mapnotify(XEvent *);
565 void mappingnotify(XEvent *);
566 void maprequest(XEvent *);
567 void propertynotify(XEvent *);
568 void unmapnotify(XEvent *);
569 void visibilitynotify(XEvent *);
571 void (*handler[LASTEvent])(XEvent *) = {
573 [KeyPress] = keypress,
574 [ButtonPress] = buttonpress,
575 [ConfigureRequest] = configurerequest,
576 [ConfigureNotify] = configurenotify,
577 [DestroyNotify] = destroynotify,
578 [EnterNotify] = enternotify,
579 [FocusIn] = focusevent,
580 [FocusOut] = focusevent,
581 [MapNotify] = mapnotify,
582 [MappingNotify] = mappingnotify,
583 [MapRequest] = maprequest,
584 [PropertyNotify] = propertynotify,
585 [UnmapNotify] = unmapnotify,
586 [VisibilityNotify] = visibilitynotify,
596 while ((pid = waitpid(WAIT_ANY, NULL, WNOHANG)) != -1) {
597 DNPRINTF(SWM_D_MISC, "reaping: %d\n", pid);
612 installsignal(int sig, char *name)
616 sa.sa_handler = sighdlr;
617 sigemptyset(&sa.sa_mask);
619 if (sigaction(sig, &sa, NULL) == -1)
620 err(1, "could not install %s handler", name);
624 name_to_color(char *colorname)
628 XColor screen_def, exact_def;
629 unsigned long result = 0;
630 char cname[32] = "#";
632 cmap = DefaultColormap(display, screens[0].idx);
633 status = XAllocNamedColor(display, cmap, colorname,
634 &screen_def, &exact_def);
636 strlcat(cname, colorname + 2, sizeof cname - 1);
637 status = XAllocNamedColor(display, cmap, cname, &screen_def,
641 result = screen_def.pixel;
643 fprintf(stderr, "color '%s' not found.\n", colorname);
649 setscreencolor(char *val, int i, int c)
651 if (i > 0 && i <= ScreenCount(display)) {
652 screens[i - 1].c[c].color = name_to_color(val);
653 free(screens[i - 1].c[c].name);
654 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
656 } else if (i == -1) {
657 for (i = 0; i < ScreenCount(display); i++) {
658 screens[i].c[c].color = name_to_color(val);
659 free(screens[i].c[c].name);
660 if ((screens[i].c[c].name = strdup(val)) == NULL)
664 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
665 i, ScreenCount(display));
669 custom_region(char *val)
671 unsigned int sidx, x, y, w, h;
673 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
674 errx(1, "invalid custom region, "
675 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
676 if (sidx < 1 || sidx > ScreenCount(display))
677 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
678 sidx, ScreenCount(display));
682 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
684 if (x < 0 || x > DisplayWidth(display, sidx) ||
685 y < 0 || y > DisplayHeight(display, sidx) ||
686 w + x > DisplayWidth(display, sidx) ||
687 h + y > DisplayHeight(display, sidx))
688 errx(1, "region %ux%u+%u+%u not within screen boundaries "
689 "(%ux%u)\n", w, h, x, y,
690 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
692 new_region(&screens[sidx], x, y, w, h);
696 socket_setnonblock(int fd)
700 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
701 err(1, "fcntl F_GETFL");
703 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
704 err(1, "fcntl F_SETFL");
708 bar_print(struct swm_region *r, char *s)
710 XClearWindow(display, r->bar_window);
711 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
712 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
721 bzero(bar_pipe, sizeof bar_pipe);
724 kill(bar_pid, SIGTERM);
727 strlcpy(bar_ext, "", sizeof bar_ext);
732 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
734 int do_class, do_name;
736 XClassHint *xch = NULL;
738 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
740 if ((xch = XAllocClassHint()) == NULL)
742 status = XGetClassHint(display, cur_focus->id, xch);
743 if (status == BadWindow || status == BadAlloc)
745 do_class = (title_class_enabled && xch->res_class != NULL);
746 do_name = (title_name_enabled && xch->res_name != NULL);
748 strlcat(s, xch->res_class, sz);
749 if (do_class && do_name)
752 strlcat(s, xch->res_name, sz);
765 struct swm_region *r;
769 char loc[SWM_BAR_MAX];
773 if (bar_enabled == 0)
775 if (bar_extra && bar_extra_running) {
776 /* ignore short reads; it'll correct itself */
777 while ((b = fgetln(stdin, &len)) != NULL)
778 if (b && b[len - 1] == '\n') {
780 strlcpy(bar_ext, b, sizeof bar_ext);
782 if (b == NULL && errno != EAGAIN) {
783 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
784 errno, strerror(errno));
788 strlcpy(bar_ext, "", sizeof bar_ext);
790 if (clock_enabled == 0)
791 strlcpy(s, "", sizeof s);
794 localtime_r(&tmt, &tm);
795 strftime(s, sizeof s, clock_format, &tm);
796 strlcat(s, " ", sizeof s);
799 for (i = 0; i < ScreenCount(display); i++) {
801 TAILQ_FOREACH(r, &screens[i].rl, entry) {
803 bar_class_name(s, sizeof s, r->ws->focus);
806 stack = r->ws->cur_layout->name;
808 snprintf(loc, sizeof loc, "%d:%d %s %s %s %s",
809 x++, r->ws->idx + 1, stack, s, bar_ext,
824 bar_toggle(struct swm_region *r, union arg *args)
826 struct swm_region *tmpr;
827 int i, sc = ScreenCount(display);
829 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
832 for (i = 0; i < sc; i++)
833 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
834 XUnmapWindow(display, tmpr->bar_window);
836 for (i = 0; i < sc; i++)
837 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
838 XMapRaised(display, tmpr->bar_window);
840 bar_enabled = !bar_enabled;
843 /* must be after stack */
850 XSetWindowAttributes wa;
851 struct swm_region *r;
854 /* do this here because the conf file is in memory */
855 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
856 /* launch external status app */
857 bar_extra_running = 1;
858 if (pipe(bar_pipe) == -1)
859 err(1, "pipe error");
860 socket_setnonblock(bar_pipe[0]);
861 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
862 if (dup2(bar_pipe[0], 0) == -1)
864 if (dup2(bar_pipe[1], 1) == -1)
866 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
867 err(1, "could not disable SIGPIPE");
868 switch (bar_pid = fork()) {
870 err(1, "cannot fork");
874 execvp(bar_argv[0], bar_argv);
875 err(1, "%s external app failed", bar_argv[0]);
877 default: /* parent */
883 bzero(&wa, sizeof wa);
884 for (i = 0; i < ScreenCount(display); i++)
885 TAILQ_FOREACH(r, &screens[i].rl, entry) {
887 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
888 wa.background_pixel =
889 screens[i].c[SWM_S_COLOR_BAR].color;
890 XChangeWindowAttributes(display, r->bar_window,
891 CWBackPixel | CWBorderPixel, &wa);
897 bar_setup(struct swm_region *r)
901 for (i = 0; bar_fonts[i] != NULL; i++) {
902 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
908 if (bar_fonts[i] == NULL)
909 errx(1, "couldn't load font");
910 bar_height = bar_fs->ascent + bar_fs->descent + 3;
912 r->bar_window = XCreateSimpleWindow(display,
913 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
914 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
915 r->s->c[SWM_S_COLOR_BAR].color);
916 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
917 XSetFont(display, bar_gc, bar_fs->fid);
918 XSelectInput(display, r->bar_window, VisibilityChangeMask);
920 XMapRaised(display, r->bar_window);
921 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
923 if (signal(SIGALRM, bar_signal) == SIG_ERR)
924 err(1, "could not install bar_signal");
929 set_win_state(struct ws_win *win, long state)
931 long data[] = {state, None};
933 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
938 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
939 (unsigned char *)data, 2);
947 unsigned char *p = NULL;
948 unsigned long n, extra;
951 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
952 &real, &format, &n, &extra, (unsigned char **)&p);
953 if (status != Success)
956 result = *((long *)p);
962 version(struct swm_region *r, union arg *args)
964 bar_version = !bar_version;
966 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
967 SWM_VERSION, cvstag);
969 strlcpy(bar_vertext, "", sizeof bar_vertext);
974 client_msg(struct ws_win *win, Atom a)
976 XClientMessageEvent cm;
981 bzero(&cm, sizeof cm);
982 cm.type = ClientMessage;
984 cm.message_type = aprot;
987 cm.data.l[1] = CurrentTime;
988 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
992 configreq_win(struct ws_win *win)
994 XConfigureRequestEvent cr;
999 bzero(&cr, sizeof cr);
1000 cr.type = ConfigureRequest;
1001 cr.display = display;
1002 cr.parent = win->id;
1003 cr.window = win->id;
1006 cr.width = win->g.w;
1007 cr.height = win->g.h;
1008 cr.border_width = 1;
1010 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&cr);
1014 config_win(struct ws_win *win)
1018 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
1019 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1024 ce.type = ConfigureNotify;
1025 ce.display = display;
1027 ce.window = win->id;
1030 ce.width = win->g.w;
1031 ce.height = win->g.h;
1032 ce.border_width = 1; /* XXX store this! */
1034 ce.override_redirect = False;
1035 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1039 count_win(struct workspace *ws, int count_transient)
1044 TAILQ_FOREACH(win, &ws->winlist, entry) {
1045 if (count_transient == 0 && win->floating)
1047 if (count_transient == 0 && win->transient)
1051 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1057 quit(struct swm_region *r, union arg *args)
1059 DNPRINTF(SWM_D_MISC, "quit\n");
1064 unmap_window(struct ws_win *win)
1069 /* don't unmap again */
1070 if (getstate(win->id) == IconicState)
1073 set_win_state(win, IconicState);
1075 XUnmapWindow(display, win->id);
1077 XSetWindowBorder(display, win->id,
1078 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1087 for (i = 0; i < ScreenCount(display); i++)
1088 for (j = 0; j < SWM_WS_MAX; j++)
1089 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1094 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1101 event.display = display; /* Ignored, but what the hell */
1102 event.window = win->id;
1103 event.root = win->s->root;
1104 event.subwindow = None;
1105 event.time = CurrentTime;
1110 event.same_screen = True;
1111 event.keycode = XKeysymToKeycode(display, keysym);
1112 event.state = modifiers;
1114 event.type = KeyPress;
1115 XSendEvent(event.display, event.window, True,
1116 KeyPressMask, (XEvent *)&event);
1118 event.type = KeyRelease;
1119 XSendEvent(event.display, event.window, True,
1120 KeyPressMask, (XEvent *)&event);
1125 restart(struct swm_region *r, union arg *args)
1127 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1129 /* disable alarm because the following code may not be interrupted */
1131 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1132 errx(1, "can't disable alarm");
1137 XCloseDisplay(display);
1138 execvp(start_argv[0], start_argv);
1139 fprintf(stderr, "execvp failed\n");
1145 root_to_region(Window root)
1147 struct swm_region *r = NULL;
1149 int i, x, y, wx, wy;
1152 for (i = 0; i < ScreenCount(display); i++)
1153 if (screens[i].root == root)
1156 if (XQueryPointer(display, screens[i].root,
1157 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1158 /* choose a region based on pointer location */
1159 TAILQ_FOREACH(r, &screens[i].rl, entry)
1160 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1161 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1166 r = TAILQ_FIRST(&screens[i].rl);
1172 find_unmanaged_window(Window id)
1177 for (i = 0; i < ScreenCount(display); i++)
1178 for (j = 0; j < SWM_WS_MAX; j++)
1179 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1187 find_window(Window id)
1190 Window wrr, wpr, *wcr = NULL;
1193 for (i = 0; i < ScreenCount(display); i++)
1194 for (j = 0; j < SWM_WS_MAX; j++)
1195 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1199 /* if we were looking for the parent return that window instead */
1200 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1205 /* ignore not found and root */
1206 if (wpr == 0 || wrr == wpr)
1209 /* look for parent */
1210 for (i = 0; i < ScreenCount(display); i++)
1211 for (j = 0; j < SWM_WS_MAX; j++)
1212 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1220 spawn(struct swm_region *r, union arg *args)
1225 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1227 * The double-fork construct avoids zombie processes and keeps the code
1228 * clean from stupid signal handlers.
1233 close(ConnectionNumber(display));
1234 setenv("LD_PRELOAD", SWM_LIB, 1);
1235 if (asprintf(&ret, "%d", r->ws->idx)) {
1236 setenv("_SWM_WS", ret, 1);
1239 if (asprintf(&ret, "%d", getpid())) {
1240 setenv("_SWM_PID", ret, 1);
1244 /* kill stdin, mplayer, ssh-add etc. need that */
1245 si = open("/dev/null", O_RDONLY, 0);
1247 err(1, "open /dev/null");
1248 if (dup2(si, 0) == -1)
1249 err(1, "dup2 /dev/null");
1250 execvp(args->argv[0], args->argv);
1251 fprintf(stderr, "execvp failed\n");
1259 spawnterm(struct swm_region *r, union arg *args)
1261 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1264 setenv("_SWM_XTERM_FONTADJ", "", 1);
1269 kill_refs(struct ws_win *win)
1272 struct swm_region *r;
1273 struct workspace *ws;
1278 for (i = 0; i < ScreenCount(display); i++)
1279 TAILQ_FOREACH(r, &screens[i].rl, entry)
1280 for (x = 0; x < SWM_WS_MAX; x++) {
1282 if (win == ws->focus)
1284 if (win == ws->focus_prev)
1285 ws->focus_prev = NULL;
1290 validate_win(struct ws_win *testwin)
1293 struct workspace *ws;
1294 struct swm_region *r;
1295 int i, x, foundit = 0;
1297 if (testwin == NULL)
1300 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1301 TAILQ_FOREACH(r, &screens[i].rl, entry)
1302 for (x = 0; x < SWM_WS_MAX; x++) {
1304 TAILQ_FOREACH(win, &ws->winlist, entry)
1312 validate_ws(struct workspace *testws)
1314 struct swm_region *r;
1315 struct workspace *ws;
1318 /* validate all ws */
1319 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1320 TAILQ_FOREACH(r, &screens[i].rl, entry)
1321 for (x = 0; x < SWM_WS_MAX; x++) {
1330 unfocus_win(struct ws_win *win)
1334 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1338 if (win->ws == NULL)
1341 if (validate_ws(win->ws))
1344 if (win->ws->r == NULL)
1347 if (validate_win(win)) {
1352 if (win->ws->focus == win) {
1353 win->ws->focus = NULL;
1354 win->ws->focus_prev = win;
1357 if (validate_win(win->ws->focus)) {
1358 kill_refs(win->ws->focus);
1359 win->ws->focus = NULL;
1361 if (validate_win(win->ws->focus_prev)) {
1362 kill_refs(win->ws->focus_prev);
1363 win->ws->focus_prev = NULL;
1366 /* drain all previous unfocus events */
1367 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
1370 grabbuttons(win, 0);
1371 XSetWindowBorder(display, win->id,
1372 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1382 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
1384 for (i = 0; i < ScreenCount(display); i++)
1385 for (j = 0; j < SWM_WS_MAX; j++)
1386 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1391 focus_win(struct ws_win *win)
1396 struct ws_win *cfw = NULL;
1399 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1403 if (win->ws == NULL)
1406 if (validate_ws(win->ws))
1408 if (validate_win(win)) {
1413 if (validate_win(win)) {
1418 XGetInputFocus(display, &cur_focus, &rr);
1419 if ((cfw = find_window(cur_focus)) != NULL)
1422 win->ws->focus = win;
1424 if (win->ws->r != NULL) {
1425 /* drain all previous focus events */
1426 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
1430 XSetInputFocus(display, win->id,
1431 RevertToParent, CurrentTime);
1432 grabbuttons(win, 1);
1433 XSetWindowBorder(display, win->id,
1434 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1435 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS)
1436 XMapRaised(display, win->id);
1441 switchws(struct swm_region *r, union arg *args)
1443 int wsid = args->id, unmap_old = 0;
1444 struct swm_region *this_r, *other_r;
1446 struct workspace *new_ws, *old_ws;
1453 old_ws = this_r->ws;
1454 new_ws = &this_r->s->ws[wsid];
1456 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1457 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1460 if (new_ws == NULL || old_ws == NULL)
1462 if (new_ws == old_ws)
1465 other_r = new_ws->r;
1466 if (other_r == NULL) {
1467 /* if the other workspace is hidden, switch windows */
1468 if (old_ws->r != NULL)
1469 old_ws->old_r = old_ws->r;
1473 other_r->ws_prior = new_ws;
1474 other_r->ws = old_ws;
1475 old_ws->r = other_r;
1477 this_r->ws_prior = old_ws;
1478 this_r->ws = new_ws;
1482 a.id = SWM_ARG_ID_FOCUSCUR;
1483 focus(new_ws->r, &a);
1486 /* unmap old windows */
1488 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1493 cyclews(struct swm_region *r, union arg *args)
1496 struct swm_screen *s = r->s;
1498 DNPRINTF(SWM_D_WS, "cyclews id %d "
1499 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1500 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1505 case SWM_ARG_ID_CYCLEWS_UP:
1506 if (a.id < SWM_WS_MAX - 1)
1511 case SWM_ARG_ID_CYCLEWS_DOWN:
1515 a.id = SWM_WS_MAX - 1;
1521 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1523 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1527 } while (a.id != r->ws->idx);
1531 priorws(struct swm_region *r, union arg *args)
1534 struct swm_screen *s = r->s;
1536 DNPRINTF(SWM_D_WS, "priorws id %d "
1537 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1538 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1540 if (r->ws_prior == NULL)
1543 a.id = r->ws_prior->idx;
1548 cyclescr(struct swm_region *r, union arg *args)
1550 struct swm_region *rr = NULL;
1554 /* do nothing if we don't have more than one screen */
1555 if (!(ScreenCount(display) > 1 || outputs > 1))
1560 case SWM_ARG_ID_CYCLESC_UP:
1561 rr = TAILQ_NEXT(r, entry);
1563 rr = TAILQ_FIRST(&screens[i].rl);
1565 case SWM_ARG_ID_CYCLESC_DOWN:
1566 rr = TAILQ_PREV(r, swm_region_list, entry);
1568 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1576 /* move mouse to region */
1578 y = rr->g.y + 1 + bar_enabled ? bar_height : 0;
1579 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1581 a.id = SWM_ARG_ID_FOCUSCUR;
1584 if (rr->ws->focus) {
1585 /* move to focus window */
1586 x = rr->ws->focus->g.x + 1;
1587 y = rr->ws->focus->g.y + 1;
1588 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1593 swapwin(struct swm_region *r, union arg *args)
1595 struct ws_win *target, *source;
1596 struct ws_win *cur_focus;
1597 struct ws_win_list *wl;
1600 DNPRINTF(SWM_D_WS, "swapwin id %d "
1601 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1602 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1604 cur_focus = r->ws->focus;
1605 if (cur_focus == NULL)
1609 wl = &source->ws->winlist;
1612 case SWM_ARG_ID_SWAPPREV:
1613 target = TAILQ_PREV(source, ws_win_list, entry);
1614 TAILQ_REMOVE(wl, cur_focus, entry);
1616 TAILQ_INSERT_TAIL(wl, source, entry);
1618 TAILQ_INSERT_BEFORE(target, source, entry);
1620 case SWM_ARG_ID_SWAPNEXT:
1621 target = TAILQ_NEXT(source, entry);
1622 TAILQ_REMOVE(wl, source, entry);
1624 TAILQ_INSERT_HEAD(wl, source, entry);
1626 TAILQ_INSERT_AFTER(wl, target, source, entry);
1628 case SWM_ARG_ID_SWAPMAIN:
1629 target = TAILQ_FIRST(wl);
1630 if (target == source) {
1631 if (source->ws->focus_prev != NULL &&
1632 source->ws->focus_prev != target)
1634 source = source->ws->focus_prev;
1638 if (target == NULL || source == NULL)
1640 source->ws->focus_prev = target;
1641 TAILQ_REMOVE(wl, target, entry);
1642 TAILQ_INSERT_BEFORE(source, target, entry);
1643 TAILQ_REMOVE(wl, source, entry);
1644 TAILQ_INSERT_HEAD(wl, source, entry);
1647 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1655 focus_prev(struct ws_win *win)
1657 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
1658 struct ws_win *cur_focus = NULL;
1659 struct ws_win_list *wl = NULL;
1660 struct workspace *ws = NULL;
1662 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
1664 if (!(win && win->ws))
1669 cur_focus = ws->focus;
1670 winlostfocus = cur_focus;
1672 /* pickle, just focus on whatever */
1673 if (cur_focus == NULL) {
1674 /* use prev_focus if valid */
1675 if (ws->focus_prev && ws->focus_prev != cur_focus &&
1676 find_window(WINID(ws->focus_prev)))
1677 winfocus = ws->focus_prev;
1678 if (winfocus == NULL)
1679 winfocus = TAILQ_FIRST(wl);
1683 /* if transient focus on parent */
1684 if (cur_focus->transient) {
1685 winfocus = find_window(cur_focus->transient);
1689 /* if in max_stack try harder */
1690 if (ws->cur_layout->flags & SWM_L_FOCUSPREV) {
1691 if (cur_focus != ws->focus_prev)
1692 winfocus = ws->focus_prev;
1693 else if (cur_focus != ws->focus)
1694 winfocus = ws->focus;
1696 winfocus = TAILQ_PREV(win, ws_win_list, entry);
1701 if (cur_focus == win)
1702 winfocus = TAILQ_PREV(win, ws_win_list, entry);
1703 if (winfocus == NULL)
1704 winfocus = TAILQ_LAST(wl, ws_win_list);
1705 if (winfocus == NULL || winfocus == win)
1706 winfocus = TAILQ_NEXT(cur_focus, entry);
1708 if (winfocus == winlostfocus || winfocus == NULL)
1711 focus_magic(winfocus, SWM_F_GENERIC);
1715 focus(struct swm_region *r, union arg *args)
1717 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
1718 struct ws_win *cur_focus = NULL;
1719 struct ws_win_list *wl = NULL;
1720 struct workspace *ws = NULL;
1725 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1727 /* treat FOCUS_CUR special */
1728 if (args->id == SWM_ARG_ID_FOCUSCUR) {
1730 winfocus = r->ws->focus;
1731 else if (r->ws->focus_prev)
1732 winfocus = r->ws->focus_prev;
1734 winfocus = TAILQ_FIRST(&r->ws->winlist);
1736 focus_magic(winfocus, SWM_F_GENERIC);
1740 if ((cur_focus = r->ws->focus) == NULL)
1745 winlostfocus = cur_focus;
1748 case SWM_ARG_ID_FOCUSPREV:
1749 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1750 if (winfocus == NULL)
1751 winfocus = TAILQ_LAST(wl, ws_win_list);
1754 case SWM_ARG_ID_FOCUSNEXT:
1755 winfocus = TAILQ_NEXT(cur_focus, entry);
1756 if (winfocus == NULL)
1757 winfocus = TAILQ_FIRST(wl);
1760 case SWM_ARG_ID_FOCUSMAIN:
1761 winfocus = TAILQ_FIRST(wl);
1762 if (winfocus == cur_focus)
1763 winfocus = cur_focus->ws->focus_prev;
1770 if (winfocus == winlostfocus || winfocus == NULL)
1773 focus_magic(winfocus, SWM_F_GENERIC);
1777 cycle_layout(struct swm_region *r, union arg *args)
1779 struct workspace *ws = r->ws;
1780 struct ws_win *winfocus;
1783 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1785 winfocus = ws->focus;
1788 if (ws->cur_layout->l_stack == NULL)
1789 ws->cur_layout = &layouts[0];
1792 a.id = SWM_ARG_ID_FOCUSCUR;
1798 stack_config(struct swm_region *r, union arg *args)
1800 struct workspace *ws = r->ws;
1802 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1805 if (ws->cur_layout->l_config != NULL)
1806 ws->cur_layout->l_config(ws, args->id);
1808 if (args->id != SWM_ARG_ID_STACKINIT);
1814 struct swm_geometry g;
1815 struct swm_region *r;
1818 DNPRINTF(SWM_D_STACK, "stack\n");
1820 for (i = 0; i < ScreenCount(display); i++) {
1822 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1823 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1824 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1826 /* start with screen geometry, adjust for bar */
1834 r->ws->cur_layout->l_stack(r->ws, &g);
1842 stack_floater(struct ws_win *win, struct swm_region *r)
1850 bzero(&wc, sizeof wc);
1851 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1852 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
1853 (win->g.h >= HEIGHT(r)))
1854 wc.border_width = 0;
1856 wc.border_width = 1;
1857 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
1858 win->g.w = (double)WIDTH(r) * dialog_ratio;
1859 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1861 wc.width = win->g.w;
1862 wc.height = win->g.h;
1867 wc.x = (WIDTH(r) - win->g.w) / 2;
1868 wc.y = (HEIGHT(r) - win->g.h) / 2;
1871 /* adjust for region */
1879 win->g.w = wc.width;
1880 win->g.h = wc.height;
1882 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
1883 win->id, wc.x, wc.y, wc.width, wc.height);
1885 XConfigureWindow(display, win->id, mask, &wc);
1890 * Send keystrokes to terminal to decrease/increase the font size as the
1891 * window size changes.
1894 adjust_font(struct ws_win *win)
1896 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
1897 win->floating || win->transient)
1900 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
1901 win->g.w / win->sh.width_inc < term_width &&
1902 win->font_steps < SWM_MAX_FONT_STEPS) {
1903 win->font_size_boundary[win->font_steps] =
1904 (win->sh.width_inc * term_width) + win->sh.base_width;
1907 win->last_inc = win->sh.width_inc;
1908 fake_keypress(win, XK_KP_Subtract, ShiftMask);
1909 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
1910 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
1913 win->last_inc = win->sh.width_inc;
1914 fake_keypress(win, XK_KP_Add, ShiftMask);
1918 #define SWAPXY(g) do { \
1920 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1921 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1924 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1927 XWindowAttributes wa;
1928 struct swm_geometry win_g, r_g = *g;
1930 int i, j, s, stacks;
1931 int w_inc = 1, h_inc, w_base = 1, h_base;
1932 int hrh, extra = 0, h_slice, last_h = 0;
1933 int split, colno, winno, mwin, msize, mscale;
1934 int remain, missing, v_slice, reconfigure;
1937 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1938 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1940 winno = count_win(ws, 0);
1941 if (winno == 0 && count_win(ws, 1) == 0)
1944 TAILQ_FOREACH(win, &ws->winlist, entry)
1945 if (win->transient == 0 && win->floating == 0)
1952 w_inc = win->sh.width_inc;
1953 w_base = win->sh.base_width;
1954 mwin = ws->l_state.horizontal_mwin;
1955 mscale = ws->l_state.horizontal_msize;
1956 stacks = ws->l_state.horizontal_stacks;
1959 w_inc = win->sh.height_inc;
1960 w_base = win->sh.base_height;
1961 mwin = ws->l_state.vertical_mwin;
1962 mscale = ws->l_state.vertical_msize;
1963 stacks = ws->l_state.vertical_stacks;
1967 if (stacks > winno - mwin)
1968 stacks = winno - mwin;
1972 h_slice = r_g.h / SWM_H_SLICE;
1973 if (mwin && winno > mwin) {
1974 v_slice = r_g.w / SWM_V_SLICE;
1978 win_g.w = v_slice * mscale;
1980 if (w_inc > 1 && w_inc < v_slice) {
1981 /* adjust for window's requested size increment */
1982 remain = (win_g.w - w_base) % w_inc;
1983 missing = w_inc - remain;
1990 win_g.x += r_g.w - msize;
1993 colno = split = winno / stacks;
1994 win_g.w = ((r_g.w - (stacks * 2) + 2) / stacks);
1996 hrh = r_g.h / colno;
1997 extra = r_g.h - (colno * hrh);
2000 /* stack all the tiled windows */
2001 i = j = 0, s = stacks;
2002 TAILQ_FOREACH(win, &ws->winlist, entry) {
2003 if (win->transient != 0 || win->floating != 0)
2006 if (split && i == split) {
2007 colno = (winno - mwin) / stacks;
2008 if (s <= (winno - mwin) % stacks)
2010 split = split + colno;
2011 hrh = (r_g.h / colno);
2012 extra = r_g.h - (colno * hrh);
2016 win_g.x += win_g.w + 2;
2017 win_g.w = (r_g.w - msize - (stacks * 2)) / stacks;
2019 win_g.w += (r_g.w - msize - (stacks * 2)) %
2026 h_inc = win->sh.width_inc;
2027 h_base = win->sh.base_width;
2029 h_inc = win->sh.height_inc;
2030 h_base = win->sh.base_height;
2032 if (j == colno - 1) {
2033 win_g.h = hrh + extra;
2034 } else if (h_inc > 1 && h_inc < h_slice) {
2035 /* adjust for window's requested size increment */
2036 remain = (win_g.h - h_base) % h_inc;
2037 missing = h_inc - remain;
2039 if (missing <= extra || j == 0) {
2051 win_g.y += last_h + 2;
2053 bzero(&wc, sizeof wc);
2054 wc.border_width = 1;
2057 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2058 win->g.w != win_g.h || win->g.h != win_g.w) {
2060 win->g.x = wc.x = win_g.y;
2061 win->g.y = wc.y = win_g.x;
2062 win->g.w = wc.width = win_g.h;
2063 win->g.h = wc.height = win_g.w;
2066 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2067 win->g.w != win_g.w || win->g.h != win_g.h) {
2069 win->g.x = wc.x = win_g.x;
2070 win->g.y = wc.y = win_g.y;
2071 win->g.w = wc.width = win_g.w;
2072 win->g.h = wc.height = win_g.h;
2077 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2078 XConfigureWindow(display, win->id, mask, &wc);
2082 if (XGetWindowAttributes(display, win->id, &wa))
2083 if (wa.map_state == IsUnmapped)
2084 XMapRaised(display, win->id);
2092 /* now, stack all the floaters and transients */
2093 TAILQ_FOREACH(win, &ws->winlist, entry) {
2094 if (win->transient == 0 && win->floating == 0)
2097 stack_floater(win, ws->r);
2098 XMapRaised(display, win->id);
2103 vertical_config(struct workspace *ws, int id)
2105 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2108 case SWM_ARG_ID_STACKRESET:
2109 case SWM_ARG_ID_STACKINIT:
2110 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2111 ws->l_state.vertical_mwin = 1;
2112 ws->l_state.vertical_stacks = 1;
2114 case SWM_ARG_ID_MASTERSHRINK:
2115 if (ws->l_state.vertical_msize > 1)
2116 ws->l_state.vertical_msize--;
2118 case SWM_ARG_ID_MASTERGROW:
2119 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2120 ws->l_state.vertical_msize++;
2122 case SWM_ARG_ID_MASTERADD:
2123 ws->l_state.vertical_mwin++;
2125 case SWM_ARG_ID_MASTERDEL:
2126 if (ws->l_state.vertical_mwin > 0)
2127 ws->l_state.vertical_mwin--;
2129 case SWM_ARG_ID_STACKINC:
2130 ws->l_state.vertical_stacks++;
2132 case SWM_ARG_ID_STACKDEC:
2133 if (ws->l_state.vertical_stacks > 1)
2134 ws->l_state.vertical_stacks--;
2142 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2144 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2146 stack_master(ws, g, 0, 0);
2150 horizontal_config(struct workspace *ws, int id)
2152 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2155 case SWM_ARG_ID_STACKRESET:
2156 case SWM_ARG_ID_STACKINIT:
2157 ws->l_state.horizontal_mwin = 1;
2158 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2159 ws->l_state.horizontal_stacks = 1;
2161 case SWM_ARG_ID_MASTERSHRINK:
2162 if (ws->l_state.horizontal_msize > 1)
2163 ws->l_state.horizontal_msize--;
2165 case SWM_ARG_ID_MASTERGROW:
2166 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2167 ws->l_state.horizontal_msize++;
2169 case SWM_ARG_ID_MASTERADD:
2170 ws->l_state.horizontal_mwin++;
2172 case SWM_ARG_ID_MASTERDEL:
2173 if (ws->l_state.horizontal_mwin > 0)
2174 ws->l_state.horizontal_mwin--;
2176 case SWM_ARG_ID_STACKINC:
2177 ws->l_state.horizontal_stacks++;
2179 case SWM_ARG_ID_STACKDEC:
2180 if (ws->l_state.horizontal_stacks > 1)
2181 ws->l_state.horizontal_stacks--;
2189 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2191 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2193 stack_master(ws, g, 1, 0);
2196 /* fullscreen view */
2198 max_stack(struct workspace *ws, struct swm_geometry *g)
2201 struct swm_geometry gg = *g;
2202 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2206 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2211 winno = count_win(ws, 0);
2212 if (winno == 0 && count_win(ws, 1) == 0)
2215 TAILQ_FOREACH(win, &ws->winlist, entry) {
2216 if (win->transient) {
2218 parent = find_window(win->transient);
2222 /* only reconfigure if necessary */
2223 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2225 bzero(&wc, sizeof wc);
2226 wc.border_width = 1;
2227 win->g.x = wc.x = gg.x;
2228 win->g.y = wc.y = gg.y;
2229 win->g.w = wc.width = gg.w;
2230 win->g.h = wc.height = gg.h;
2231 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2232 XConfigureWindow(display, win->id, mask, &wc);
2235 /* unmap only if we don't have multi screen */
2236 if (win != ws->focus)
2237 if (!(ScreenCount(display) > 1 || outputs > 1))
2241 /* put the last transient on top */
2244 XMapRaised(display, parent->id);
2245 stack_floater(wintrans, ws->r);
2246 focus_magic(wintrans, SWM_F_TRANSIENT);
2251 send_to_ws(struct swm_region *r, union arg *args)
2253 int wsid = args->id;
2254 struct ws_win *win = win;
2255 struct workspace *ws, *nws;
2256 Atom ws_idx_atom = 0;
2257 unsigned char ws_idx_str[SWM_PROPLEN];
2266 if (win->ws->idx == wsid)
2269 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2272 nws = &win->s->ws[wsid];
2274 a.id = SWM_ARG_ID_FOCUSPREV;
2277 TAILQ_REMOVE(&ws->winlist, win, entry);
2278 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2281 /* Try to update the window's workspace property */
2282 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2284 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2285 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2287 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2288 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2294 wkill(struct swm_region *r, union arg *args)
2296 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
2298 if(r->ws->focus == NULL)
2301 if (args->id == SWM_ARG_ID_KILLWINDOW)
2302 XKillClient(display, r->ws->focus->id);
2304 if (r->ws->focus->can_delete)
2305 client_msg(r->ws->focus, adelete);
2309 floating_toggle(struct swm_region *r, union arg *args)
2311 struct ws_win *win = r->ws->focus;
2317 win->floating = !win->floating;
2320 a.id = SWM_ARG_ID_FOCUSCUR;
2321 focus(win->ws->r, &a);
2325 resize_window(struct ws_win *win, int center)
2329 struct swm_region *r;
2331 r = root_to_region(win->wa.root);
2332 bzero(&wc, sizeof wc);
2333 mask = CWBorderWidth | CWWidth | CWHeight;
2334 wc.border_width = 1;
2335 wc.width = win->g.w;
2336 wc.height = win->g.h;
2337 if (center == SWM_ARG_ID_CENTER) {
2338 wc.x = (WIDTH(r) - win->g.w) / 2;
2339 wc.y = (HEIGHT(r) - win->g.h) / 2;
2343 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
2344 win->id, wc.x, wc.y, wc.width, wc.height);
2346 XConfigureWindow(display, win->id, mask, &wc);
2351 resize(struct ws_win *win, union arg *args)
2355 struct swm_region *r = win->ws->r;
2357 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
2358 win->id, win->floating, win->transient);
2360 if (!(win->transient != 0 || win->floating != 0))
2363 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2364 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2366 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w, win->g.h);
2368 XMaskEvent(display, MOUSEMASK | ExposureMask |
2369 SubstructureRedirectMask, &ev);
2371 case ConfigureRequest:
2374 handler[ev.type](&ev);
2377 /* do not allow resize outside of region */
2378 if (ev.xmotion.y_root < r->g.y ||
2379 ev.xmotion.y_root >= r->g.y + r->g.h - 1)
2381 if (ev.xmotion.x_root < r->g.x ||
2382 ev.xmotion.x_root >= r->g.x + r->g.w - 1)
2385 if (ev.xmotion.x <= 1)
2387 if (ev.xmotion.y <= 1)
2389 win->g.w = ev.xmotion.x;
2390 win->g.h = ev.xmotion.y;
2392 /* not free, don't sync more than 60 times / second */
2393 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2394 time = ev.xmotion.time;
2395 XSync(display, False);
2396 resize_window(win, args->id);
2400 } while (ev.type != ButtonRelease);
2402 XSync(display, False);
2403 resize_window(win, args->id);
2405 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
2407 XUngrabPointer(display, CurrentTime);
2410 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2414 move_window(struct ws_win *win)
2418 struct swm_region *r;
2420 r = root_to_region(win->wa.root);
2421 bzero(&wc, sizeof wc);
2426 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
2427 win->id, wc.x, wc.y, wc.width, wc.height);
2429 XConfigureWindow(display, win->id, mask, &wc);
2434 move(struct ws_win *win, union arg *args)
2439 struct swm_region *r = win->ws->r;
2441 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
2442 win->id, win->floating, win->transient);
2444 if (win->floating == 0) {
2450 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2451 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2453 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2455 XMaskEvent(display, MOUSEMASK | ExposureMask |
2456 SubstructureRedirectMask, &ev);
2458 case ConfigureRequest:
2461 handler[ev.type](&ev);
2464 /* don't allow to move window out of region */
2465 if (ev.xmotion.y_root < r->g.y ||
2466 ev.xmotion.y_root + win->g.h >= r->g.y + r->g.h - 1)
2468 if (ev.xmotion.x_root < r->g.x ||
2469 ev.xmotion.x_root + win->g.w >= r->g.x + r->g.w - 1)
2472 win->g.x = ev.xmotion.x_root;
2473 win->g.y = ev.xmotion.y_root;
2475 /* not free, don't sync more than 60 times / second */
2476 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2477 time = ev.xmotion.time;
2478 XSync(display, False);
2483 } while (ev.type != ButtonRelease);
2485 XSync(display, False);
2488 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2489 XUngrabPointer(display, CurrentTime);
2494 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2497 /* user/key callable function IDs */
2556 /* key definitions */
2558 dummykeyfunc(struct swm_region *r, union arg *args)
2563 legacyfunc(struct swm_region *r, union arg *args)
2568 char name[SWM_FUNCNAME_LEN];
2569 void (*func)(struct swm_region *r, union arg *);
2571 } keyfuncs[kf_invalid + 1] = {
2572 /* name function argument */
2573 { "cycle_layout", cycle_layout, {0} },
2574 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
2575 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
2576 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
2577 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
2578 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
2579 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
2580 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
2581 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
2582 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
2583 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
2584 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
2585 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
2586 { "spawn_term", spawnterm, {.argv = spawn_term} },
2587 { "spawn_menu", legacyfunc, {0} },
2588 { "quit", quit, {0} },
2589 { "restart", restart, {0} },
2590 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
2591 { "ws_1", switchws, {.id = 0} },
2592 { "ws_2", switchws, {.id = 1} },
2593 { "ws_3", switchws, {.id = 2} },
2594 { "ws_4", switchws, {.id = 3} },
2595 { "ws_5", switchws, {.id = 4} },
2596 { "ws_6", switchws, {.id = 5} },
2597 { "ws_7", switchws, {.id = 6} },
2598 { "ws_8", switchws, {.id = 7} },
2599 { "ws_9", switchws, {.id = 8} },
2600 { "ws_10", switchws, {.id = 9} },
2601 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
2602 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
2603 { "ws_prior", priorws, {0} },
2604 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
2605 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
2606 { "mvws_1", send_to_ws, {.id = 0} },
2607 { "mvws_2", send_to_ws, {.id = 1} },
2608 { "mvws_3", send_to_ws, {.id = 2} },
2609 { "mvws_4", send_to_ws, {.id = 3} },
2610 { "mvws_5", send_to_ws, {.id = 4} },
2611 { "mvws_6", send_to_ws, {.id = 5} },
2612 { "mvws_7", send_to_ws, {.id = 6} },
2613 { "mvws_8", send_to_ws, {.id = 7} },
2614 { "mvws_9", send_to_ws, {.id = 8} },
2615 { "mvws_10", send_to_ws, {.id = 9} },
2616 { "bar_toggle", bar_toggle, {0} },
2617 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
2618 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
2619 { "screenshot_all", legacyfunc, {0} },
2620 { "screenshot_wind", legacyfunc, {0} },
2621 { "float_toggle", floating_toggle,{0} },
2622 { "version", version, {0} },
2623 { "spawn_lock", legacyfunc, {0} },
2624 { "spawn_initscr", legacyfunc, {0} },
2625 { "spawn_custom", dummykeyfunc, {0} },
2626 { "dumpwins", dumpwins, {0} },
2627 { "invalid key func", NULL, {0} },
2632 enum keyfuncid funcid;
2635 int keys_size = 0, keys_length = 0;
2636 struct key *keys = NULL;
2639 enum { client_click, root_click };
2641 unsigned int action;
2643 unsigned int button;
2644 void (*func)(struct ws_win *, union arg *);
2647 /* action key mouse button func args */
2648 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
2649 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
2650 { client_click, MODKEY, Button1, move, {0} },
2654 update_modkey(unsigned int mod)
2659 for (i = 0; i < keys_length; i++)
2660 if (keys[i].mod & ShiftMask)
2661 keys[i].mod = mod | ShiftMask;
2665 for (i = 0; i < LENGTH(buttons); i++)
2666 if (buttons[i].mask & ShiftMask)
2667 buttons[i].mask = mod | ShiftMask;
2669 buttons[i].mask = mod;
2679 int spawns_size = 0, spawns_length = 0;
2680 struct spawn_prog *spawns = NULL;
2683 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
2686 struct spawn_prog *prog = NULL;
2688 char *ap, **real_args;
2690 DNPRINTF(SWM_D_SPAWN, "spawn_custom %s\n", spawn_name);
2693 for (i = 0; i < spawns_length; i++) {
2694 if (!strcasecmp(spawn_name, spawns[i].name))
2698 fprintf(stderr, "spawn_custom: program %s not found\n",
2703 /* make room for expanded args */
2704 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
2705 err(1, "spawn_custom: calloc real_args");
2707 /* expand spawn_args into real_args */
2708 for (i = 0; i < prog->argc; i++) {
2710 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
2711 if (!strcasecmp(ap, "$bar_border")) {
2713 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
2715 err(1, "spawn_custom border color");
2716 } else if (!strcasecmp(ap, "$bar_color")) {
2718 strdup(r->s->c[SWM_S_COLOR_BAR].name))
2720 err(1, "spawn_custom bar color");
2721 } else if (!strcasecmp(ap, "$bar_font")) {
2722 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
2724 err(1, "spawn_custom bar fonts");
2725 } else if (!strcasecmp(ap, "$bar_font_color")) {
2727 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
2729 err(1, "spawn_custom color font");
2730 } else if (!strcasecmp(ap, "$color_focus")) {
2732 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
2734 err(1, "spawn_custom color focus");
2735 } else if (!strcasecmp(ap, "$color_unfocus")) {
2737 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
2739 err(1, "spawn_custom color unfocus");
2741 /* no match --> copy as is */
2742 if ((real_args[i] = strdup(ap)) == NULL)
2743 err(1, "spawn_custom strdup(ap)");
2745 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
2750 if ((swm_debug & SWM_D_SPAWN) != 0) {
2751 fprintf(stderr, "spawn_custom: result = ");
2752 for (i = 0; i < prog->argc; i++)
2753 fprintf(stderr, "\"%s\" ", real_args[i]);
2754 fprintf(stderr, "\n");
2760 for (i = 0; i < prog->argc; i++)
2766 setspawn(struct spawn_prog *prog)
2770 if (prog == NULL || prog->name == NULL)
2774 for (i = 0; i < spawns_length; i++) {
2775 if (!strcmp(spawns[i].name, prog->name)) {
2777 if (prog->argv == NULL) {
2779 DNPRINTF(SWM_D_SPAWN,
2780 "setspawn: delete #%d %s\n",
2782 free(spawns[i].name);
2783 for (j = 0; j < spawns[i].argc; j++)
2784 free(spawns[i].argv[j]);
2785 free(spawns[i].argv);
2786 j = spawns_length - 1;
2788 spawns[i] = spawns[j];
2793 DNPRINTF(SWM_D_SPAWN,
2794 "setspawn: replace #%d %s\n",
2796 free(spawns[i].name);
2797 for (j = 0; j < spawns[i].argc; j++)
2798 free(spawns[i].argv[j]);
2799 free(spawns[i].argv);
2802 /* found case handled */
2808 if (prog->argv == NULL) {
2810 "error: setspawn: cannot find program %s", prog->name);
2815 /* not found: add */
2816 if (spawns_size == 0 || spawns == NULL) {
2818 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
2819 spawns = malloc((size_t)spawns_size *
2820 sizeof(struct spawn_prog));
2821 if (spawns == NULL) {
2822 fprintf(stderr, "setspawn: malloc failed\n");
2826 } else if (spawns_length == spawns_size) {
2828 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
2829 spawns = realloc(spawns, (size_t)spawns_size *
2830 sizeof(struct spawn_prog));
2831 if (spawns == NULL) {
2832 fprintf(stderr, "setspawn: realloc failed\n");
2838 if (spawns_length < spawns_size) {
2839 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
2840 spawns_length, prog->name);
2841 i = spawns_length++;
2844 fprintf(stderr, "spawns array problem?\n");
2845 if (spawns == NULL) {
2846 fprintf(stderr, "spawns array is NULL!\n");
2854 setconfspawn(char *selector, char *value, int flags)
2856 struct spawn_prog *prog;
2857 char *vp, *cp, *word;
2859 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
2860 if ((prog = calloc(1, sizeof *prog)) == NULL)
2861 err(1, "setconfspawn: calloc prog");
2862 prog->name = strdup(selector);
2863 if (prog->name == NULL)
2864 err(1, "setconfspawn prog->name");
2865 if ((cp = vp = strdup(value)) == NULL)
2866 err(1, "setconfspawn: strdup(value) ");
2867 while ((word = strsep(&cp, " \t")) != NULL) {
2868 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
2870 cp += (long)strspn(cp, " \t");
2871 if (strlen(word) > 0) {
2873 prog->argv = realloc(prog->argv,
2874 prog->argc * sizeof(char *));
2875 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
2876 err(1, "setconfspawn: strdup");
2883 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
2890 setconfspawn("term", "xterm", 0);
2891 setconfspawn("screenshot_all", "screenshot.sh full", 0);
2892 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
2893 setconfspawn("lock", "xlock", 0);
2894 setconfspawn("initscr", "initscreen.sh", 0);
2895 setconfspawn("menu", "dmenu_run"
2898 " -nf $bar_font_color"
2900 " -sf $bar_color", 0);
2904 #define SWM_MODNAME_SIZE 32
2905 #define SWM_KEY_WS "\n+ \t"
2907 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
2911 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
2912 if (mod == NULL || ks == NULL) {
2913 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
2916 if (keystr == NULL || strlen(keystr) == 0) {
2917 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
2923 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
2924 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
2926 cp += (long)strspn(cp, SWM_KEY_WS);
2927 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
2929 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
2931 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
2933 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
2935 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
2937 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
2939 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
2940 *mod |= ControlMask;
2942 *ks = XStringToKeysym(name);
2943 XConvertCase(*ks, ks, &uks);
2944 if (ks == NoSymbol) {
2946 "parsekeys: invalid key %s\n",
2952 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
2957 strdupsafe(char *str)
2962 return (strdup(str));
2966 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
2969 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
2970 keyfuncs[kfid].name, spawn_name);
2972 for (i = 0; i < keys_length; i++) {
2973 if (keys[i].mod == mod && keys[i].keysym == ks) {
2974 if (kfid == kf_invalid) {
2977 "setkeybinding: delete #%d %s\n",
2978 i, keyfuncs[keys[i].funcid].name);
2979 free(keys[i].spawn_name);
2980 j = keys_length - 1;
2984 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2987 /* found: replace */
2989 "setkeybinding: replace #%d %s %s\n",
2990 i, keyfuncs[keys[i].funcid].name,
2992 free(keys[i].spawn_name);
2994 keys[i].keysym = ks;
2995 keys[i].funcid = kfid;
2996 keys[i].spawn_name = strdupsafe(spawn_name);
2997 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3002 if (kfid == kf_invalid) {
3004 "error: setkeybinding: cannot find mod/key combination");
3005 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3008 /* not found: add */
3009 if (keys_size == 0 || keys == NULL) {
3011 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
3012 keys = malloc((size_t)keys_size * sizeof(struct key));
3014 fprintf(stderr, "malloc failed\n");
3018 } else if (keys_length == keys_size) {
3020 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
3021 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
3023 fprintf(stderr, "realloc failed\n");
3028 if (keys_length < keys_size) {
3030 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
3031 j, keyfuncs[kfid].name, spawn_name);
3033 keys[j].keysym = ks;
3034 keys[j].funcid = kfid;
3035 keys[j].spawn_name = strdupsafe(spawn_name);
3037 fprintf(stderr, "keys array problem?\n");
3039 fprintf(stderr, "keys array problem\n");
3043 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3047 setconfbinding(char *selector, char *value, int flags)
3049 enum keyfuncid kfid;
3053 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
3054 if (selector == NULL) {
3055 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
3056 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3058 setkeybinding(mod, ks, kfid, NULL);
3063 /* search by key function name */
3064 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
3065 if (strncasecmp(selector, keyfuncs[kfid].name,
3066 SWM_FUNCNAME_LEN) == 0) {
3067 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3069 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3070 setkeybinding(mod, ks, kfid, NULL);
3076 /* search by custom spawn name */
3077 for (i = 0; i < spawns_length; i++) {
3078 if (strcasecmp(selector, spawns[i].name) == 0) {
3079 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3081 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3082 setkeybinding(mod, ks, kf_spawn_custom,
3089 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
3096 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
3097 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
3098 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
3099 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
3100 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
3101 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
3102 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
3103 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
3104 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
3105 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
3106 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
3107 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
3108 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
3109 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
3110 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
3111 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
3112 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
3113 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
3114 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
3115 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
3116 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
3117 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
3118 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
3119 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
3120 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
3121 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
3122 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
3123 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
3124 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
3125 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
3126 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
3127 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
3128 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
3129 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
3130 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
3131 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
3132 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
3133 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
3134 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
3135 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
3136 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
3137 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
3138 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
3139 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
3140 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
3141 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
3142 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
3143 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
3144 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
3145 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
3146 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
3147 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
3148 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
3149 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
3151 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
3156 updatenumlockmask(void)
3159 XModifierKeymap *modmap;
3161 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
3163 modmap = XGetModifierMapping(display);
3164 for (i = 0; i < 8; i++)
3165 for (j = 0; j < modmap->max_keypermod; j++)
3166 if (modmap->modifiermap[i * modmap->max_keypermod + j]
3167 == XKeysymToKeycode(display, XK_Num_Lock))
3168 numlockmask = (1 << i);
3170 XFreeModifiermap(modmap);
3176 unsigned int i, j, k;
3178 unsigned int modifiers[] =
3179 { 0, LockMask, numlockmask, numlockmask | LockMask };
3181 DNPRINTF(SWM_D_MISC, "grabkeys\n");
3182 updatenumlockmask();
3184 for (k = 0; k < ScreenCount(display); k++) {
3185 if (TAILQ_EMPTY(&screens[k].rl))
3187 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
3188 for (i = 0; i < keys_length; i++) {
3189 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
3190 for (j = 0; j < LENGTH(modifiers); j++)
3191 XGrabKey(display, code,
3192 keys[i].mod | modifiers[j],
3193 screens[k].root, True,
3194 GrabModeAsync, GrabModeAsync);
3200 grabbuttons(struct ws_win *win, int focused)
3203 unsigned int modifiers[] =
3204 { 0, LockMask, numlockmask, numlockmask|LockMask };
3206 updatenumlockmask();
3207 XUngrabButton(display, AnyButton, AnyModifier, win->id);
3209 for (i = 0; i < LENGTH(buttons); i++)
3210 if (buttons[i].action == client_click)
3211 for (j = 0; j < LENGTH(modifiers); j++)
3212 XGrabButton(display, buttons[i].button,
3213 buttons[i].mask | modifiers[j],
3214 win->id, False, BUTTONMASK,
3215 GrabModeAsync, GrabModeSync, None,
3218 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
3219 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
3222 const char *quirkname[] = {
3223 "NONE", /* config string for "no value" */
3231 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
3232 #define SWM_Q_WS "\n|+ \t"
3234 parsequirks(char *qstr, unsigned long *quirk)
3244 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
3246 cp += (long)strspn(cp, SWM_Q_WS);
3247 for (i = 0; i < LENGTH(quirkname); i++) {
3248 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
3249 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
3254 *quirk |= 1 << (i-1);
3258 if (i >= LENGTH(quirkname)) {
3259 DNPRINTF(SWM_D_QUIRK,
3260 "parsequirks: invalid quirk [%s]\n", name);
3268 setquirk(const char *class, const char *name, const int quirk)
3273 for (i = 0; i < quirks_length; i++) {
3274 if (!strcmp(quirks[i].class, class) &&
3275 !strcmp(quirks[i].name, name)) {
3278 DNPRINTF(SWM_D_QUIRK,
3279 "setquirk: delete #%d %s:%s\n",
3280 i, quirks[i].class, quirks[i].name);
3281 free(quirks[i].class);
3282 free(quirks[i].name);
3283 j = quirks_length - 1;
3285 quirks[i] = quirks[j];
3289 /* found: replace */
3290 DNPRINTF(SWM_D_QUIRK,
3291 "setquirk: replace #%d %s:%s\n",
3292 i, quirks[i].class, quirks[i].name);
3293 free(quirks[i].class);
3294 free(quirks[i].name);
3295 quirks[i].class = strdup(class);
3296 quirks[i].name = strdup(name);
3297 quirks[i].quirk = quirk;
3304 "error: setquirk: cannot find class/name combination");
3307 /* not found: add */
3308 if (quirks_size == 0 || quirks == NULL) {
3310 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
3311 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
3313 fprintf(stderr, "setquirk: malloc failed\n");
3317 } else if (quirks_length == quirks_size) {
3319 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
3320 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
3322 fprintf(stderr, "setquirk: realloc failed\n");
3327 if (quirks_length < quirks_size) {
3328 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
3329 j = quirks_length++;
3330 quirks[j].class = strdup(class);
3331 quirks[j].name = strdup(name);
3332 quirks[j].quirk = quirk;
3334 fprintf(stderr, "quirks array problem?\n");
3336 fprintf(stderr, "quirks array problem!\n");
3343 setconfquirk(char *selector, char *value, int flags)
3345 char *cp, *class, *name;
3347 unsigned long quirks;
3348 if (selector == NULL)
3350 if ((cp = strchr(selector, ':')) == NULL)
3355 if ((retval = parsequirks(value, &quirks)) == 0)
3356 setquirk(class, name, quirks);
3363 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
3364 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
3365 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
3366 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
3367 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3368 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
3369 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3370 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3371 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3372 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3373 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
3374 setquirk("pcb", "pcb", SWM_Q_FLOAT);
3377 /* conf file stuff */
3378 #define SWM_CONF_FILE "scrotwm.conf"
3380 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_STACK_ENABLED,
3381 SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT, SWM_S_CYCLE_EMPTY,
3382 SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH,
3383 SWM_S_TITLE_CLASS_ENABLED, SWM_S_TITLE_NAME_ENABLED,
3384 SWM_S_FOCUS_MODE, SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM,
3385 SWM_S_SS_APP, SWM_S_DIALOG_RATIO
3389 setconfvalue(char *selector, char *value, int flags)
3392 case SWM_S_BAR_DELAY:
3393 bar_delay = atoi(value);
3395 case SWM_S_BAR_ENABLED:
3396 bar_enabled = atoi(value);
3398 case SWM_S_STACK_ENABLED:
3399 stack_enabled = atoi(value);
3401 case SWM_S_CLOCK_ENABLED:
3402 clock_enabled = atoi(value);
3404 case SWM_S_CLOCK_FORMAT:
3405 #ifndef SWM_DENY_CLOCK_FORMAT
3407 if ((clock_format = strdup(value)) == NULL)
3408 err(1, "setconfvalue: clock_format");
3411 case SWM_S_CYCLE_EMPTY:
3412 cycle_empty = atoi(value);
3414 case SWM_S_CYCLE_VISIBLE:
3415 cycle_visible = atoi(value);
3417 case SWM_S_SS_ENABLED:
3418 ss_enabled = atoi(value);
3420 case SWM_S_TERM_WIDTH:
3421 term_width = atoi(value);
3423 case SWM_S_TITLE_CLASS_ENABLED:
3424 title_class_enabled = atoi(value);
3426 case SWM_S_TITLE_NAME_ENABLED:
3427 title_name_enabled = atoi(value);
3429 case SWM_S_FOCUS_MODE:
3430 if (!strcmp(value, "default"))
3431 focus_mode = SWM_FOCUS_DEFAULT;
3432 else if (!strcmp(value, "follow_cursor"))
3433 focus_mode = SWM_FOCUS_FOLLOW;
3434 else if (!strcmp(value, "synergy"))
3435 focus_mode = SWM_FOCUS_SYNERGY;
3437 err(1, "focus_mode");
3439 case SWM_S_BAR_FONT:
3441 if ((bar_fonts[0] = strdup(value)) == NULL)
3442 err(1, "setconfvalue: bar_font");
3444 case SWM_S_BAR_ACTION:
3446 if ((bar_argv[0] = strdup(value)) == NULL)
3447 err(1, "setconfvalue: bar_action");
3449 case SWM_S_SPAWN_TERM:
3450 free(spawn_term[0]);
3451 if ((spawn_term[0] = strdup(value)) == NULL)
3452 err(1, "setconfvalue: spawn_term");
3456 case SWM_S_DIALOG_RATIO:
3457 dialog_ratio = atof(value);
3458 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
3468 setconfmodkey(char *selector, char *value, int flags)
3470 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
3471 update_modkey(Mod1Mask);
3472 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
3473 update_modkey(Mod2Mask);
3474 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
3475 update_modkey(Mod3Mask);
3476 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
3477 update_modkey(Mod4Mask);
3484 setconfcolor(char *selector, char *value, int flags)
3486 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
3491 setconfregion(char *selector, char *value, int flags)
3493 custom_region(value);
3497 /* config options */
3498 struct config_option {
3500 int (*func)(char*, char*, int);
3503 struct config_option configopt[] = {
3504 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
3505 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
3506 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
3507 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
3508 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
3509 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
3510 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
3511 { "bind", setconfbinding, 0 },
3512 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
3513 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
3514 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
3515 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
3516 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
3517 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
3518 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
3519 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
3520 { "modkey", setconfmodkey, 0 },
3521 { "program", setconfspawn, 0 },
3522 { "quirk", setconfquirk, 0 },
3523 { "region", setconfregion, 0 },
3524 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
3525 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
3526 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
3527 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
3528 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
3529 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
3530 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
3535 conf_load(char *filename)
3538 char *line, *cp, *optsub, *optval;
3539 size_t linelen, lineno = 0;
3540 int wordlen, i, optind;
3541 struct config_option *opt;
3543 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
3545 if (filename == NULL) {
3546 fprintf(stderr, "conf_load: no filename\n");
3549 if ((config = fopen(filename, "r")) == NULL) {
3550 warn("conf_load: fopen");
3554 while (!feof(config)) {
3555 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
3558 err(1, "%s", filename);
3563 cp += strspn(cp, " \t\n"); /* eat whitespace */
3564 if (cp[0] == '\0') {
3569 /* get config option */
3570 wordlen = strcspn(cp, "=[ \t\n");
3572 warnx("%s: line %zd: no option found",
3577 for (i = 0; i < LENGTH(configopt); i++) {
3578 opt = &configopt[i];
3579 if (!strncasecmp(cp, opt->optname, wordlen) &&
3580 strlen(opt->optname) == wordlen) {
3586 warnx("%s: line %zd: unknown option %.*s",
3587 filename, lineno, wordlen, cp);
3591 cp += strspn(cp, " \t\n"); /* eat whitespace */
3592 /* get [selector] if any */
3596 wordlen = strcspn(cp, "]");
3599 warnx("%s: line %zd: syntax error",
3603 asprintf(&optsub, "%.*s", wordlen, cp);
3606 cp += strspn(cp, "] \t\n"); /* eat trailing */
3608 cp += strspn(cp, "= \t\n"); /* eat trailing */
3610 optval = strdup(cp);
3611 /* call function to deal with it all */
3612 if (configopt[optind].func(optsub, optval,
3613 configopt[optind].funcflags) != 0) {
3614 fprintf(stderr, "%s line %zd: %s\n",
3615 filename, lineno, line);
3616 errx(1, "%s: line %zd: invalid data for %s",
3617 filename, lineno, configopt[optind].optname);
3625 DNPRINTF(SWM_D_CONF, "conf_load end\n");
3631 set_child_transient(struct ws_win *win)
3633 struct ws_win *parent;
3635 parent = find_window(win->transient);
3637 parent->child_trans = win;
3641 manage_window(Window id)
3644 struct workspace *ws;
3645 struct ws_win *win, *ww;
3646 int format, i, ws_idx, n, border_me = 0;
3647 unsigned long nitems, bytes;
3648 Atom ws_idx_atom = 0, type;
3649 Atom *prot = NULL, *pp;
3650 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
3651 struct swm_region *r;
3656 if ((win = find_window(id)) != NULL)
3657 return (win); /* already being managed */
3659 /* see if we are on the unmanaged list */
3660 if ((win = find_unmanaged_window(id)) != NULL) {
3661 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
3663 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
3664 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
3666 set_child_transient(win);
3670 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
3671 errx(1, "calloc: failed to allocate memory for new window");
3673 /* Get all the window data in one shot */
3674 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3676 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
3677 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
3678 XGetWindowAttributes(display, id, &win->wa);
3679 XGetWMNormalHints(display, id, &win->sh, &mask);
3680 XGetTransientForHint(display, id, &trans);
3682 win->transient = trans;
3683 set_child_transient(win);
3684 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
3685 win->id, win->transient);
3687 /* get supported protocols */
3688 if (XGetWMProtocols(display, id, &prot, &n)) {
3689 for (i = 0, pp = prot; i < n; i++, pp++) {
3690 if (*pp == takefocus)
3691 win->take_focus = 1;
3693 win->can_delete = 1;
3700 * Figure out where to put the window. If it was previously assigned to
3701 * a workspace (either by spawn() or manually moving), and isn't
3702 * transient, * put it in the same workspace
3704 r = root_to_region(win->wa.root);
3705 if (prop && win->transient == 0) {
3706 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
3707 ws_idx = strtonum(prop, 0, 9, &errstr);
3709 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
3712 ws = &r->s->ws[ws_idx];
3715 /* this should launch transients in the same ws as parent */
3717 if ((ww = find_window(trans)) != NULL)
3724 "fix this bug mcbride\n");
3729 /* set up the window layout */
3732 win->s = r->s; /* this never changes */
3733 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
3735 win->g.w = win->wa.width;
3736 win->g.h = win->wa.height;
3737 win->g.x = win->wa.x;
3738 win->g.y = win->wa.y;
3740 /* Set window properties so we can remember this after reincarnation */
3741 if (ws_idx_atom && prop == NULL &&
3742 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
3743 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3745 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3746 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3750 if (XGetClassHint(display, win->id, &win->ch)) {
3751 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
3752 win->ch.res_class, win->ch.res_name);
3754 /* java is retarded so treat it special */
3755 if (strstr(win->ch.res_name, "sun-awt")) {
3760 for (i = 0; i < quirks_length; i++){
3761 if (!strcmp(win->ch.res_class, quirks[i].class) &&
3762 !strcmp(win->ch.res_name, quirks[i].name)) {
3763 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
3764 win->ch.res_class, win->ch.res_name);
3765 if (quirks[i].quirk & SWM_Q_FLOAT) {
3769 win->quirks = quirks[i].quirk;
3774 /* alter window position if quirky */
3775 if (win->quirks & SWM_Q_ANYWHERE) {
3776 win->manual = 1; /* don't center the quirky windows */
3777 bzero(&wc, sizeof wc);
3779 if (win->g.y < bar_height) {
3780 win->g.y = wc.y = bar_height;
3783 if (win->g.w + win->g.x > WIDTH(r)) {
3784 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
3790 /* Reset font sizes (the bruteforce way; no default keybinding). */
3791 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
3792 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3793 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3794 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3795 fake_keypress(win, XK_KP_Add, ShiftMask);
3800 bzero(&wc, sizeof wc);
3801 wc.border_width = 1;
3802 mask = CWBorderWidth;
3803 XConfigureWindow(display, win->id, mask, &wc);
3807 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
3808 PropertyChangeMask | StructureNotifyMask);
3810 set_win_state(win, NormalState);
3812 /* floaters need to be mapped if they are in the current workspace */
3813 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
3814 XMapRaised(display, win->id);
3820 free_window(struct ws_win *win)
3822 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
3827 /* needed for restart wm */
3828 set_win_state(win, WithdrawnState);
3830 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
3832 if (win->ch.res_class)
3833 XFree(win->ch.res_class);
3834 if (win->ch.res_name)
3835 XFree(win->ch.res_name);
3840 memset(win, 0xff, sizeof *win); /* XXX kill later */
3846 unmanage_window(struct ws_win *win)
3848 struct ws_win *parent;
3853 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
3855 if (win->transient) {
3856 parent = find_window(win->transient);
3858 parent->child_trans = NULL;
3861 /* work around for mplayer going full screen */
3865 TAILQ_REMOVE(&win->ws->winlist, win, entry);
3866 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
3872 focus_magic(struct ws_win *win, int do_trans)
3874 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu %d\n", WINID(win), do_trans);
3879 if (do_trans == SWM_F_TRANSIENT && win->child_trans) {
3880 /* win = parent & has a transient so focus on that */
3882 focus_win(win->child_trans);
3883 if (win->child_trans->take_focus)
3884 client_msg(win, takefocus);
3886 focus_win(win->child_trans);
3887 if (win->child_trans->take_focus)
3888 client_msg(win->child_trans, takefocus);
3893 if (win->take_focus)
3894 client_msg(win, takefocus);
3901 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
3909 XKeyEvent *ev = &e->xkey;
3911 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
3913 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
3914 for (i = 0; i < keys_length; i++)
3915 if (keysym == keys[i].keysym
3916 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
3917 && keyfuncs[keys[i].funcid].func) {
3918 if (keys[i].funcid == kf_spawn_custom)
3920 root_to_region(ev->root),
3921 &(keyfuncs[keys[i].funcid].args),
3925 keyfuncs[keys[i].funcid].func(
3926 root_to_region(ev->root),
3927 &(keyfuncs[keys[i].funcid].args)
3933 buttonpress(XEvent *e)
3937 XButtonPressedEvent *ev = &e->xbutton;
3939 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
3941 action = root_click;
3942 if ((win = find_window(ev->window)) == NULL)
3945 focus_magic(win, SWM_F_TRANSIENT);
3946 action = client_click;
3948 for (i = 0; i < LENGTH(buttons); i++)
3949 if (action == buttons[i].action && buttons[i].func &&
3950 buttons[i].button == ev->button &&
3951 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
3952 buttons[i].func(win, &buttons[i].args);
3956 configurerequest(XEvent *e)
3958 XConfigureRequestEvent *ev = &e->xconfigurerequest;
3963 if ((win = find_window(ev->window)) == NULL)
3964 if ((win = find_unmanaged_window(ev->window)) == NULL)
3968 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
3970 bzero(&wc, sizeof wc);
3973 wc.width = ev->width;
3974 wc.height = ev->height;
3975 wc.border_width = ev->border_width;
3976 wc.sibling = ev->above;
3977 wc.stack_mode = ev->detail;
3978 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
3980 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
3982 if (win->floating) {
3983 if (ev->value_mask & CWX)
3985 if (ev->value_mask & CWY)
3987 if (ev->value_mask & CWWidth)
3988 win->g.w = ev->width;
3989 if (ev->value_mask & CWHeight)
3990 win->g.h = ev->height;
3997 configurenotify(XEvent *e)
4002 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
4003 e->xconfigure.window);
4005 win = find_window(e->xconfigure.window);
4007 XGetWMNormalHints(display, win->id, &win->sh, &mask);
4015 destroynotify(XEvent *e)
4018 XDestroyWindowEvent *ev = &e->xdestroywindow;
4020 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
4022 if ((win = find_window(ev->window)) == NULL) {
4023 if ((win = find_unmanaged_window(ev->window)) == NULL)
4029 /* make sure we focus on something */
4032 unmanage_window(win);
4038 enternotify(XEvent *e)
4040 XCrossingEvent *ev = &e->xcrossing;
4045 Window focus_return;
4046 int revert_to_return;
4048 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
4049 "%lu subwindow %lu same_screen %d focus %d state %d\n",
4050 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
4051 ev->same_screen, ev->focus, ev->state);
4053 switch (focus_mode) {
4054 case SWM_FOCUS_DEFAULT:
4055 if (QLength(display)) {
4056 DNPRINTF(SWM_D_EVENT, "ignore enternotify %d\n",
4061 case SWM_FOCUS_FOLLOW:
4063 case SWM_FOCUS_SYNERGY:
4066 * all these checks need to be in this order because the
4067 * XCheckTypedWindowEvent relies on weeding out the previous events
4069 * making this code an option would enable a follow mouse for focus
4074 * state is set when we are switching workspaces and focus is set when
4075 * the window or a subwindow already has focus (occurs during restart).
4077 * Only honor the focus flag if last_focus_event is not FocusOut,
4078 * this allows scrotwm to continue to control focus when another
4079 * program is also playing with it.
4081 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
4082 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
4087 * happens when a window is created or destroyed and the border
4088 * crosses the mouse pointer and when switching ws
4090 * we need the subwindow test to see if we came from root in order
4091 * to give focus to floaters
4093 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
4094 ev->subwindow == 0) {
4095 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
4099 /* this window already has focus */
4100 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
4101 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
4105 /* this window is being deleted or moved to another ws */
4106 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
4108 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
4109 XPutBackEvent(display, &cne);
4113 if ((win = find_window(ev->window)) == NULL) {
4114 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
4119 * In fullstack kill all enters unless they come from a different ws
4120 * (i.e. another region) or focus has been grabbed externally.
4122 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
4123 last_focus_event != FocusOut) {
4124 XGetInputFocus(display, &focus_return, &revert_to_return);
4125 if ((w = find_window(focus_return)) == NULL ||
4127 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
4135 if ((win = find_window(ev->window)) == NULL) {
4136 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
4141 * if we have more enternotifies let them handle it in due time
4143 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
4144 DNPRINTF(SWM_D_EVENT,
4145 "ignoring enternotify: got more enternotify\n");
4146 XPutBackEvent(display, &cne);
4150 focus_magic(win, SWM_F_TRANSIENT);
4153 /* lets us use one switch statement for arbitrary mode/detail combinations */
4154 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
4157 focusevent(XEvent *e)
4159 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
4160 ev->type == FocusIn ? "entering" : "leaving",
4161 ev->window, ev->mode, ev->detail);
4164 u_int32_t mode_detail;
4165 XFocusChangeEvent *ev = &e->xfocus;
4167 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
4168 ev->type == FocusIn ? "entering" : "leaving",
4169 ev->window, ev->mode, ev->detail);
4171 if (last_focus_event == ev->type) {
4172 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
4176 last_focus_event = ev->type;
4177 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
4179 switch (mode_detail) {
4180 /* synergy client focus operations */
4181 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
4182 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
4184 /* synergy server focus operations */
4185 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
4187 /* Entering applications like rdesktop that mangle the pointer */
4188 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
4190 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
4191 XSetWindowBorder(display, win->id,
4192 win->ws->r->s->c[ev->type == FocusIn ?
4193 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
4196 fprintf(stderr, "ignoring focusevent\n");
4197 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
4204 mapnotify(XEvent *e)
4207 XMapEvent *ev = &e->xmap;
4209 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
4211 win = manage_window(ev->window);
4213 set_win_state(win, NormalState);
4217 mappingnotify(XEvent *e)
4219 XMappingEvent *ev = &e->xmapping;
4221 XRefreshKeyboardMapping(ev);
4222 if (ev->request == MappingKeyboard)
4227 maprequest(XEvent *e)
4230 struct swm_region *r;
4231 XWindowAttributes wa;
4232 XMapRequestEvent *ev = &e->xmaprequest;
4234 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
4235 e->xmaprequest.window);
4237 if (!XGetWindowAttributes(display, ev->window, &wa))
4239 if (wa.override_redirect)
4242 win = manage_window(e->xmaprequest.window);
4244 return; /* can't happen */
4248 /* make new win focused */
4249 r = root_to_region(win->wa.root);
4250 if (win->ws == r->ws)
4251 focus_magic(win, SWM_F_GENERIC);
4255 propertynotify(XEvent *e)
4258 XPropertyEvent *ev = &e->xproperty;
4260 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
4263 if (ev->state == PropertyDelete)
4264 return; /* ignore */
4265 win = find_window(ev->window);
4270 case XA_WM_NORMAL_HINTS:
4273 XGetWMNormalHints(display, win->id, &win->sh, &mask);
4274 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
4275 if (win->sh.flags & PMinSize) {
4276 win->g.w = win->sh.min_width;
4277 win->g.h = win->sh.min_height;
4278 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
4280 XMoveResizeWindow(display, win->id,
4281 win->g.x, win->g.y, win->g.w, win->g.h);
4290 unmapnotify(XEvent *e)
4294 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
4296 /* determine if we need to help unmanage this window */
4297 win = find_window(e->xunmap.window);
4301 if (getstate(e->xunmap.window) == NormalState) {
4302 unmanage_window(win);
4308 visibilitynotify(XEvent *e)
4311 struct swm_region *r;
4313 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
4314 e->xvisibility.window);
4315 if (e->xvisibility.state == VisibilityUnobscured)
4316 for (i = 0; i < ScreenCount(display); i++)
4317 TAILQ_FOREACH(r, &screens[i].rl, entry)
4318 if (e->xvisibility.window == r->bar_window)
4323 xerror_start(Display *d, XErrorEvent *ee)
4330 xerror(Display *d, XErrorEvent *ee)
4332 /* fprintf(stderr, "error: %p %p\n", display, ee); */
4340 xerrorxlib = XSetErrorHandler(xerror_start);
4342 /* this causes an error if some other window manager is running */
4343 XSelectInput(display, DefaultRootWindow(display),
4344 SubstructureRedirectMask);
4345 XSync(display, False);
4349 XSetErrorHandler(xerror);
4350 XSync(display, False);
4355 new_region(struct swm_screen *s, int x, int y, int w, int h)
4357 struct swm_region *r, *n;
4358 struct workspace *ws = NULL;
4361 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
4362 s->idx, w, h, x, y);
4364 /* remove any conflicting regions */
4365 n = TAILQ_FIRST(&s->rl);
4368 n = TAILQ_NEXT(r, entry);
4369 if (X(r) < (x + w) &&
4370 (X(r) + WIDTH(r)) > x &&
4372 (Y(r) + HEIGHT(r)) > y) {
4373 XDestroyWindow(display, r->bar_window);
4374 TAILQ_REMOVE(&s->rl, r, entry);
4375 TAILQ_INSERT_TAIL(&s->orl, r, entry);
4379 /* search old regions for one to reuse */
4381 /* size + location match */
4382 TAILQ_FOREACH(r, &s->orl, entry)
4383 if (X(r) == x && Y(r) == y &&
4384 HEIGHT(r) == h && WIDTH(r) == w)
4388 TAILQ_FOREACH(r, &s->orl, entry)
4389 if (HEIGHT(r) == h && WIDTH(r) == w)
4393 TAILQ_REMOVE(&s->orl, r, entry);
4394 /* try to use old region's workspace */
4395 if (r->ws->r == NULL)
4398 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
4399 errx(1, "calloc: failed to allocate memory for screen");
4401 /* if we don't have a workspace already, find one */
4403 for (i = 0; i < SWM_WS_MAX; i++)
4404 if (s->ws[i].r == NULL) {
4411 errx(1, "no free workspaces\n");
4421 TAILQ_INSERT_TAIL(&s->rl, r, entry);
4427 #ifdef SWM_XRR_HAS_CRTC
4429 XRRScreenResources *sr;
4432 #endif /* SWM_XRR_HAS_CRTC */
4433 struct swm_region *r;
4436 if (i >= ScreenCount(display))
4437 errx(1, "invalid screen");
4439 /* remove any old regions */
4440 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
4441 r->ws->old_r = r->ws->r = NULL;
4442 XDestroyWindow(display, r->bar_window);
4443 TAILQ_REMOVE(&screens[i].rl, r, entry);
4444 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
4447 /* map virtual screens onto physical screens */
4448 #ifdef SWM_XRR_HAS_CRTC
4450 if (xrandr_support) {
4451 sr = XRRGetScreenResources(display, screens[i].root);
4453 new_region(&screens[i], 0, 0,
4454 DisplayWidth(display, i),
4455 DisplayHeight(display, i));
4459 for (c = 0, ci = NULL; c < ncrtc; c++) {
4460 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
4461 if (ci->noutput == 0)
4465 if (ci != NULL && ci->mode == None)
4466 new_region(&screens[i], 0, 0,
4467 DisplayWidth(display, i),
4468 DisplayHeight(display, i));
4470 new_region(&screens[i],
4471 ci->x, ci->y, ci->width, ci->height);
4474 XRRFreeCrtcInfo(ci);
4475 XRRFreeScreenResources(sr);
4477 #endif /* SWM_XRR_HAS_CRTC */
4479 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
4480 DisplayHeight(display, i));
4485 screenchange(XEvent *e) {
4486 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
4487 struct swm_region *r;
4490 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
4492 if (!XRRUpdateConfiguration(e))
4495 /* silly event doesn't include the screen index */
4496 for (i = 0; i < ScreenCount(display); i++)
4497 if (screens[i].root == xe->root)
4499 if (i >= ScreenCount(display))
4500 errx(1, "screenchange: screen not found\n");
4502 /* brute force for now, just re-enumerate the regions */
4505 /* add bars to all regions */
4506 for (i = 0; i < ScreenCount(display); i++)
4507 TAILQ_FOREACH(r, &screens[i].rl, entry)
4515 Window d1, d2, *wins = NULL;
4516 XWindowAttributes wa;
4519 int errorbase, major, minor;
4520 struct workspace *ws;
4524 if ((screens = calloc(ScreenCount(display),
4525 sizeof(struct swm_screen))) == NULL)
4526 errx(1, "calloc: screens");
4528 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4530 /* initial Xrandr setup */
4531 xrandr_support = XRRQueryExtension(display,
4532 &xrandr_eventbase, &errorbase);
4534 if (XRRQueryVersion(display, &major, &minor) && major < 1)
4537 /* map physical screens */
4538 for (i = 0; i < ScreenCount(display); i++) {
4539 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
4541 TAILQ_INIT(&screens[i].rl);
4542 TAILQ_INIT(&screens[i].orl);
4543 screens[i].root = RootWindow(display, i);
4545 /* set default colors */
4546 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
4547 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
4548 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
4549 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
4550 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
4552 /* init all workspaces */
4553 /* XXX these should be dynamically allocated too */
4554 for (j = 0; j < SWM_WS_MAX; j++) {
4555 ws = &screens[i].ws[j];
4560 TAILQ_INIT(&ws->winlist);
4561 TAILQ_INIT(&ws->unmanagedlist);
4563 for (k = 0; layouts[k].l_stack != NULL; k++)
4564 if (layouts[k].l_config != NULL)
4565 layouts[k].l_config(ws,
4566 SWM_ARG_ID_STACKINIT);
4567 ws->cur_layout = &layouts[0];
4569 /* grab existing windows (before we build the bars)*/
4570 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
4576 XRRSelectInput(display, screens[i].root,
4577 RRScreenChangeNotifyMask);
4579 /* attach windows to a region */
4580 /* normal windows */
4581 for (j = 0; j < no; j++) {
4582 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4583 wa.override_redirect ||
4584 XGetTransientForHint(display, wins[j], &d1))
4587 state = getstate(wins[j]);
4588 manage = state == IconicState;
4589 if (wa.map_state == IsViewable || manage)
4590 manage_window(wins[j]);
4592 /* transient windows */
4593 for (j = 0; j < no; j++) {
4594 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4595 wa.override_redirect)
4598 state = getstate(wins[j]);
4599 manage = state == IconicState;
4600 if (XGetTransientForHint(display, wins[j], &d1) &&
4602 manage_window(wins[j]);
4614 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
4616 err(1, "setup_globals: strdup");
4617 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
4619 err(1, "setup_globals: strdup");
4620 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
4622 err(1, "setup_globals: strdup");
4623 if ((spawn_term[0] = strdup("xterm")) == NULL)
4624 err(1, "setup_globals: strdup");
4625 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
4633 Atom netwmcheck, netwmname, utf8_string;
4636 /* work around sun jdk bugs, code from wmname */
4637 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
4638 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
4639 utf8_string = XInternAtom(display, "UTF8_STRING", False);
4640 for (i = 0; i < ScreenCount(display); i++) {
4641 root = screens[i].root;
4642 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
4643 PropModeReplace, (unsigned char *)&root, 1);
4644 XChangeProperty(display, root, netwmname, utf8_string, 8,
4645 PropModeReplace, "LG3D", strlen("LG3D"));
4650 main(int argc, char *argv[])
4653 struct swm_region *r, *rr;
4654 struct ws_win *winfocus = NULL;
4657 char conf[PATH_MAX], *cfile = NULL;
4664 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
4665 SWM_VERSION, cvstag);
4666 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
4667 warnx("no locale support");
4669 if (!(display = XOpenDisplay(0)))
4670 errx(1, "can not open display");
4673 errx(1, "other wm running");
4675 /* handle some signale */
4676 installsignal(SIGINT, "INT");
4677 installsignal(SIGHUP, "HUP");
4678 installsignal(SIGQUIT, "QUIT");
4679 installsignal(SIGTERM, "TERM");
4680 installsignal(SIGCHLD, "CHLD");
4682 astate = XInternAtom(display, "WM_STATE", False);
4683 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
4684 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
4685 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
4687 /* look for local and global conf file */
4688 pwd = getpwuid(getuid());
4690 errx(1, "invalid user %d", getuid());
4698 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
4699 if (stat(conf, &sb) != -1) {
4700 if (S_ISREG(sb.st_mode))
4703 /* try global conf file */
4704 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
4705 if (!stat(conf, &sb))
4706 if (S_ISREG(sb.st_mode))
4712 /* setup all bars */
4713 for (i = 0; i < ScreenCount(display); i++)
4714 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4715 if (winfocus == NULL)
4716 winfocus = TAILQ_FIRST(&r->ws->winlist);
4720 /* set some values to work around bad programs */
4728 xfd = ConnectionNumber(display);
4730 while (XPending(display)) {
4731 XNextEvent(display, &e);
4734 if (e.type < LASTEvent) {
4736 if (handler[e.type])
4737 handler[e.type](&e);
4739 DNPRINTF(SWM_D_EVENT,
4740 "win: %lu unknown event: %d\n",
4741 e.xany.window, e.type);
4743 switch (e.type - xrandr_eventbase) {
4744 case RRScreenChangeNotify:
4748 DNPRINTF(SWM_D_EVENT,
4749 "win: %lu unknown xrandr event: "
4750 "%d\n", e.xany.window, e.type);
4756 /* if we are being restarted go focus on first window */
4758 rr = winfocus->ws->r;
4760 /* not a visible window */
4764 /* move pointer to first screen if multi screen */
4765 if (ScreenCount(display) > 1 || outputs > 1)
4766 XWarpPointer(display, None, rr->s[0].root,
4767 0, 0, 0, 0, rr->g.x,
4768 rr->g.y + bar_enabled ? bar_height : 0);
4770 a.id = SWM_ARG_ID_FOCUSCUR;
4780 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
4782 DNPRINTF(SWM_D_MISC, "select failed");
4792 XFreeGC(display, bar_gc);
4793 XCloseDisplay(display);