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.22"
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)
158 #define SWM_LIB "/usr/local/lib/libswmhack.so"
166 volatile sig_atomic_t running = 1;
168 int last_focus_event = FocusOut;
169 int (*xerrorxlib)(Display *, XErrorEvent *);
173 int xrandr_eventbase;
174 unsigned int numlockmask = 0;
178 int cycle_visible = 0;
180 int font_adjusted = 0;
181 unsigned int mod_key = MODKEY;
184 double dialog_ratio = .6;
186 #define SWM_BAR_MAX (256)
187 char *bar_argv[] = { NULL, NULL };
189 char bar_ext[SWM_BAR_MAX];
190 char bar_vertext[SWM_BAR_MAX];
192 sig_atomic_t bar_alarm = 0;
196 int bar_extra_running = 0;
199 int stack_enabled = 1;
200 int clock_enabled = 1;
201 char *clock_format = NULL;
202 int title_name_enabled = 0;
203 int title_class_enabled = 0;
209 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
210 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
212 #define SWM_MENU_FN (2)
213 #define SWM_MENU_NB (4)
214 #define SWM_MENU_NF (6)
215 #define SWM_MENU_SB (8)
216 #define SWM_MENU_SF (10)
218 /* layout manager data */
219 struct swm_geometry {
229 /* virtual "screens" */
231 TAILQ_ENTRY(swm_region) entry;
232 struct swm_geometry g;
233 struct workspace *ws; /* current workspace on this region */
234 struct swm_screen *s; /* screen idx */
237 TAILQ_HEAD(swm_region_list, swm_region);
240 TAILQ_ENTRY(ws_win) entry;
243 struct ws_win *child_trans; /* transient child window */
244 struct swm_geometry g;
247 int font_size_boundary[SWM_MAX_FONT_STEPS];
253 unsigned long quirks;
254 struct workspace *ws; /* always valid */
255 struct swm_screen *s; /* always valid, never changes */
256 XWindowAttributes wa;
260 TAILQ_HEAD(ws_win_list, ws_win);
262 /* layout handlers */
264 void vertical_config(struct workspace *, int);
265 void vertical_stack(struct workspace *, struct swm_geometry *);
266 void horizontal_config(struct workspace *, int);
267 void horizontal_stack(struct workspace *, struct swm_geometry *);
268 void max_stack(struct workspace *, struct swm_geometry *);
270 struct ws_win *find_window(Window);
272 void grabbuttons(struct ws_win *, int);
273 void new_region(struct swm_screen *, int, int, int, int);
274 void unmanage_window(struct ws_win *);
275 long getstate(Window);
278 void (*l_stack)(struct workspace *, struct swm_geometry *);
279 void (*l_config)(struct workspace *, int);
281 #define SWM_L_FOCUSPREV (1<<0)
282 #define SWM_L_MAPONFOCUS (1<<1)
285 /* stack, configure */
286 { vertical_stack, vertical_config, 0, "[|]" },
287 { horizontal_stack, horizontal_config, 0, "[-]" },
289 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, "[ ]"},
290 { NULL, NULL, 0, NULL },
293 #define SWM_H_SLICE (32)
294 #define SWM_V_SLICE (32)
296 /* define work spaces */
298 int idx; /* workspace index */
299 struct layout *cur_layout; /* current layout handlers */
300 struct ws_win *focus; /* may be NULL */
301 struct ws_win *focus_prev; /* may be NULL */
302 struct swm_region *r; /* may be NULL */
303 struct swm_region *old_r; /* may be NULL */
304 struct ws_win_list winlist; /* list of windows in ws */
305 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
309 int horizontal_msize;
311 int horizontal_stacks;
318 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
319 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
321 /* physical screen mapping */
322 #define SWM_WS_MAX (10)
324 int idx; /* screen index */
325 struct swm_region_list rl; /* list of regions on this screen */
326 struct swm_region_list orl; /* list of old regions */
328 struct workspace ws[SWM_WS_MAX];
334 } c[SWM_S_COLOR_MAX];
336 struct swm_screen *screens;
339 /* args to functions */
342 #define SWM_ARG_ID_FOCUSNEXT (0)
343 #define SWM_ARG_ID_FOCUSPREV (1)
344 #define SWM_ARG_ID_FOCUSMAIN (2)
345 #define SWM_ARG_ID_FOCUSCUR (4)
346 #define SWM_ARG_ID_SWAPNEXT (10)
347 #define SWM_ARG_ID_SWAPPREV (11)
348 #define SWM_ARG_ID_SWAPMAIN (12)
349 #define SWM_ARG_ID_MASTERSHRINK (20)
350 #define SWM_ARG_ID_MASTERGROW (21)
351 #define SWM_ARG_ID_MASTERADD (22)
352 #define SWM_ARG_ID_MASTERDEL (23)
353 #define SWM_ARG_ID_STACKRESET (30)
354 #define SWM_ARG_ID_STACKINIT (31)
355 #define SWM_ARG_ID_CYCLEWS_UP (40)
356 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
357 #define SWM_ARG_ID_CYCLESC_UP (42)
358 #define SWM_ARG_ID_CYCLESC_DOWN (43)
359 #define SWM_ARG_ID_STACKINC (50)
360 #define SWM_ARG_ID_STACKDEC (51)
361 #define SWM_ARG_ID_SS_ALL (60)
362 #define SWM_ARG_ID_SS_WINDOW (61)
363 #define SWM_ARG_ID_DONTCENTER (70)
364 #define SWM_ARG_ID_CENTER (71)
365 #define SWM_ARG_ID_KILLWINDOW (80)
366 #define SWM_ARG_ID_DELETEWINDOW (81)
370 void focus(struct swm_region *, union arg *);
371 void focus_magic(struct ws_win *, int);
372 #define SWM_F_GENERIC (0)
373 #define SWM_F_TRANSIENT (1)
379 #define SWM_Q_FLOAT (1<<0) /* float this window */
380 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
381 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
382 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
383 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
385 int quirks_size = 0, quirks_length = 0;
386 struct quirk *quirks = NULL;
403 name = "ButtonPress";
406 name = "ButtonRelease";
409 name = "MotionNotify";
412 name = "EnterNotify";
415 name = "LeaveNotify";
424 name = "KeymapNotify";
430 name = "GraphicsExpose";
435 case VisibilityNotify:
436 name = "VisibilityNotify";
439 name = "CreateNotify";
442 name = "DestroyNotify";
445 name = "UnmapNotify";
454 name = "ReparentNotify";
456 case ConfigureNotify:
457 name = "ConfigureNotify";
459 case ConfigureRequest:
460 name = "ConfigureRequest";
463 name = "GravityNotify";
466 name = "ResizeRequest";
468 case CirculateNotify:
469 name = "CirculateNotify";
471 case CirculateRequest:
472 name = "CirculateRequest";
475 name = "PropertyNotify";
478 name = "SelectionClear";
480 case SelectionRequest:
481 name = "SelectionRequest";
483 case SelectionNotify:
484 name = "SelectionNotify";
487 name = "ColormapNotify";
490 name = "ClientMessage";
493 name = "MappingNotify";
498 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
500 e->xany.window, name, e->type, QLength(display));
502 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
504 e->xany.window, e->type, QLength(display));
508 dumpwins(struct swm_region *r, union arg *args)
512 XWindowAttributes wa;
515 fprintf(stderr, "invalid workspace\n");
519 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
521 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
522 state = getstate(win->id);
523 if (!XGetWindowAttributes(display, win->id, &wa))
524 fprintf(stderr, "window: %lu failed "
525 "XGetWindowAttributes\n", win->id);
526 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
527 win->id, wa.map_state, state);
530 fprintf(stderr, "===== unmanaged window list =====\n");
531 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
532 state = getstate(win->id);
533 if (!XGetWindowAttributes(display, win->id, &wa))
534 fprintf(stderr, "window: %lu failed "
535 "XGetWindowAttributes\n", win->id);
536 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
537 win->id, wa.map_state, state);
540 fprintf(stderr, "=================================\n");
545 dumpwins(struct swm_region *r, union arg *args)
548 #endif /* SWM_DEBUG */
550 void expose(XEvent *);
551 void keypress(XEvent *);
552 void buttonpress(XEvent *);
553 void configurerequest(XEvent *);
554 void configurenotify(XEvent *);
555 void destroynotify(XEvent *);
556 void enternotify(XEvent *);
557 void focusevent(XEvent *);
558 void mapnotify(XEvent *);
559 void mappingnotify(XEvent *);
560 void maprequest(XEvent *);
561 void propertynotify(XEvent *);
562 void unmapnotify(XEvent *);
563 void visibilitynotify(XEvent *);
565 void (*handler[LASTEvent])(XEvent *) = {
567 [KeyPress] = keypress,
568 [ButtonPress] = buttonpress,
569 [ConfigureRequest] = configurerequest,
570 [ConfigureNotify] = configurenotify,
571 [DestroyNotify] = destroynotify,
572 [EnterNotify] = enternotify,
573 [FocusIn] = focusevent,
574 [FocusOut] = focusevent,
575 [MapNotify] = mapnotify,
576 [MappingNotify] = mappingnotify,
577 [MapRequest] = maprequest,
578 [PropertyNotify] = propertynotify,
579 [UnmapNotify] = unmapnotify,
580 [VisibilityNotify] = visibilitynotify,
590 while ((pid = waitpid(WAIT_ANY, NULL, WNOHANG)) != -1) {
591 DNPRINTF(SWM_D_MISC, "reaping: %d\n", pid);
606 installsignal(int sig, char *name)
610 sa.sa_handler = sighdlr;
611 sigemptyset(&sa.sa_mask);
613 if (sigaction(sig, &sa, NULL) == -1)
614 err(1, "could not install %s handler", name);
618 name_to_color(char *colorname)
622 XColor screen_def, exact_def;
623 unsigned long result = 0;
624 char cname[32] = "#";
626 cmap = DefaultColormap(display, screens[0].idx);
627 status = XAllocNamedColor(display, cmap, colorname,
628 &screen_def, &exact_def);
630 strlcat(cname, colorname + 2, sizeof cname - 1);
631 status = XAllocNamedColor(display, cmap, cname, &screen_def,
635 result = screen_def.pixel;
637 fprintf(stderr, "color '%s' not found.\n", colorname);
643 setscreencolor(char *val, int i, int c)
645 if (i > 0 && i <= ScreenCount(display)) {
646 screens[i - 1].c[c].color = name_to_color(val);
647 free(screens[i - 1].c[c].name);
648 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
650 } else if (i == -1) {
651 for (i = 0; i < ScreenCount(display); i++) {
652 screens[i].c[c].color = name_to_color(val);
653 free(screens[i].c[c].name);
654 if ((screens[i].c[c].name = strdup(val)) == NULL)
658 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
659 i, ScreenCount(display));
663 custom_region(char *val)
665 unsigned int sidx, x, y, w, h;
667 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
668 errx(1, "invalid custom region, "
669 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
670 if (sidx < 1 || sidx > ScreenCount(display))
671 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
672 sidx, ScreenCount(display));
676 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
678 if (x < 0 || x > DisplayWidth(display, sidx) ||
679 y < 0 || y > DisplayHeight(display, sidx) ||
680 w + x > DisplayWidth(display, sidx) ||
681 h + y > DisplayHeight(display, sidx))
682 errx(1, "region %ux%u+%u+%u not within screen boundaries "
683 "(%ux%u)\n", w, h, x, y,
684 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
686 new_region(&screens[sidx], x, y, w, h);
690 socket_setnonblock(int fd)
694 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
695 err(1, "fcntl F_GETFL");
697 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
698 err(1, "fcntl F_SETFL");
702 bar_print(struct swm_region *r, char *s)
704 XClearWindow(display, r->bar_window);
705 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
706 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
715 bzero(bar_pipe, sizeof bar_pipe);
718 kill(bar_pid, SIGTERM);
721 strlcpy(bar_ext, "", sizeof bar_ext);
726 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
728 int do_class, do_name;
730 XClassHint *xch = NULL;
732 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
734 if ((xch = XAllocClassHint()) == NULL)
736 status = XGetClassHint(display, cur_focus->id, xch);
737 if (status == BadWindow || status == BadAlloc)
739 do_class = (title_class_enabled && xch->res_class != NULL);
740 do_name = (title_name_enabled && xch->res_name != NULL);
742 strlcat(s, xch->res_class, sz);
743 if (do_class && do_name)
746 strlcat(s, xch->res_name, sz);
759 struct swm_region *r;
763 char loc[SWM_BAR_MAX];
767 if (bar_enabled == 0)
769 if (bar_extra && bar_extra_running) {
770 /* ignore short reads; it'll correct itself */
771 while ((b = fgetln(stdin, &len)) != NULL)
772 if (b && b[len - 1] == '\n') {
774 strlcpy(bar_ext, b, sizeof bar_ext);
776 if (b == NULL && errno != EAGAIN) {
777 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
778 errno, strerror(errno));
782 strlcpy(bar_ext, "", sizeof bar_ext);
784 if (clock_enabled == 0)
785 strlcpy(s, "", sizeof s);
788 localtime_r(&tmt, &tm);
789 strftime(s, sizeof s, clock_format, &tm);
790 strlcat(s, " ", sizeof s);
793 for (i = 0; i < ScreenCount(display); i++) {
795 TAILQ_FOREACH(r, &screens[i].rl, entry) {
797 bar_class_name(s, sizeof s, r->ws->focus);
800 stack = r->ws->cur_layout->name;
802 snprintf(loc, sizeof loc, "%d:%d %s %s %s %s",
803 x++, r->ws->idx + 1, stack, s, bar_ext,
818 bar_toggle(struct swm_region *r, union arg *args)
820 struct swm_region *tmpr;
821 int i, sc = ScreenCount(display);
823 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
826 for (i = 0; i < sc; i++)
827 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
828 XUnmapWindow(display, tmpr->bar_window);
830 for (i = 0; i < sc; i++)
831 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
832 XMapRaised(display, tmpr->bar_window);
834 bar_enabled = !bar_enabled;
837 /* must be after stack */
844 XSetWindowAttributes wa;
845 struct swm_region *r;
848 /* do this here because the conf file is in memory */
849 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
850 /* launch external status app */
851 bar_extra_running = 1;
852 if (pipe(bar_pipe) == -1)
853 err(1, "pipe error");
854 socket_setnonblock(bar_pipe[0]);
855 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
856 if (dup2(bar_pipe[0], 0) == -1)
858 if (dup2(bar_pipe[1], 1) == -1)
860 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
861 err(1, "could not disable SIGPIPE");
862 switch (bar_pid = fork()) {
864 err(1, "cannot fork");
868 execvp(bar_argv[0], bar_argv);
869 err(1, "%s external app failed", bar_argv[0]);
871 default: /* parent */
877 bzero(&wa, sizeof wa);
878 for (i = 0; i < ScreenCount(display); i++)
879 TAILQ_FOREACH(r, &screens[i].rl, entry) {
881 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
882 wa.background_pixel =
883 screens[i].c[SWM_S_COLOR_BAR].color;
884 XChangeWindowAttributes(display, r->bar_window,
885 CWBackPixel | CWBorderPixel, &wa);
891 bar_setup(struct swm_region *r)
895 for (i = 0; bar_fonts[i] != NULL; i++) {
896 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
902 if (bar_fonts[i] == NULL)
903 errx(1, "couldn't load font");
904 bar_height = bar_fs->ascent + bar_fs->descent + 3;
906 r->bar_window = XCreateSimpleWindow(display,
907 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
908 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
909 r->s->c[SWM_S_COLOR_BAR].color);
910 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
911 XSetFont(display, bar_gc, bar_fs->fid);
912 XSelectInput(display, r->bar_window, VisibilityChangeMask);
914 XMapRaised(display, r->bar_window);
915 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
917 if (signal(SIGALRM, bar_signal) == SIG_ERR)
918 err(1, "could not install bar_signal");
923 set_win_state(struct ws_win *win, long state)
925 long data[] = {state, None};
927 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
932 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
933 (unsigned char *)data, 2);
941 unsigned char *p = NULL;
942 unsigned long n, extra;
945 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
946 &real, &format, &n, &extra, (unsigned char **)&p);
947 if (status != Success)
950 result = *((long *)p);
956 version(struct swm_region *r, union arg *args)
958 bar_version = !bar_version;
960 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
961 SWM_VERSION, cvstag);
963 strlcpy(bar_vertext, "", sizeof bar_vertext);
968 client_msg(struct ws_win *win, Atom a)
970 XClientMessageEvent cm;
975 bzero(&cm, sizeof cm);
976 cm.type = ClientMessage;
978 cm.message_type = aprot;
981 cm.data.l[1] = CurrentTime;
982 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
986 configreq_win(struct ws_win *win)
988 XConfigureRequestEvent cr;
993 bzero(&cr, sizeof cr);
994 cr.type = ConfigureRequest;
995 cr.display = display;
1000 cr.width = win->g.w;
1001 cr.height = win->g.h;
1002 cr.border_width = 1;
1004 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&cr);
1008 config_win(struct ws_win *win)
1012 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
1013 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1018 ce.type = ConfigureNotify;
1019 ce.display = display;
1021 ce.window = win->id;
1024 ce.width = win->g.w;
1025 ce.height = win->g.h;
1026 ce.border_width = 1; /* XXX store this! */
1028 ce.override_redirect = False;
1029 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1033 count_win(struct workspace *ws, int count_transient)
1038 TAILQ_FOREACH(win, &ws->winlist, entry) {
1039 if (count_transient == 0 && win->floating)
1041 if (count_transient == 0 && win->transient)
1045 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1051 quit(struct swm_region *r, union arg *args)
1053 DNPRINTF(SWM_D_MISC, "quit\n");
1058 unmap_window(struct ws_win *win)
1063 /* don't unmap again */
1064 if (getstate(win->id) == IconicState)
1067 set_win_state(win, IconicState);
1069 XUnmapWindow(display, win->id);
1071 XSetWindowBorder(display, win->id,
1072 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1081 for (i = 0; i < ScreenCount(display); i++)
1082 for (j = 0; j < SWM_WS_MAX; j++)
1083 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1088 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1095 event.display = display; /* Ignored, but what the hell */
1096 event.window = win->id;
1097 event.root = win->s->root;
1098 event.subwindow = None;
1099 event.time = CurrentTime;
1104 event.same_screen = True;
1105 event.keycode = XKeysymToKeycode(display, keysym);
1106 event.state = modifiers;
1108 event.type = KeyPress;
1109 XSendEvent(event.display, event.window, True,
1110 KeyPressMask, (XEvent *)&event);
1112 event.type = KeyRelease;
1113 XSendEvent(event.display, event.window, True,
1114 KeyPressMask, (XEvent *)&event);
1119 restart(struct swm_region *r, union arg *args)
1121 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1123 /* disable alarm because the following code may not be interrupted */
1125 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1126 errx(1, "can't disable alarm");
1131 XCloseDisplay(display);
1132 execvp(start_argv[0], start_argv);
1133 fprintf(stderr, "execvp failed\n");
1139 root_to_region(Window root)
1141 struct swm_region *r = NULL;
1143 int i, x, y, wx, wy;
1146 for (i = 0; i < ScreenCount(display); i++)
1147 if (screens[i].root == root)
1150 if (XQueryPointer(display, screens[i].root,
1151 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1152 /* choose a region based on pointer location */
1153 TAILQ_FOREACH(r, &screens[i].rl, entry)
1154 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1155 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1160 r = TAILQ_FIRST(&screens[i].rl);
1166 find_unmanaged_window(Window id)
1171 for (i = 0; i < ScreenCount(display); i++)
1172 for (j = 0; j < SWM_WS_MAX; j++)
1173 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1181 find_window(Window id)
1184 Window wrr, wpr, *wcr = NULL;
1187 for (i = 0; i < ScreenCount(display); i++)
1188 for (j = 0; j < SWM_WS_MAX; j++)
1189 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1193 /* if we were looking for the parent return that window instead */
1194 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1199 /* ignore not found and root */
1200 if (wpr == 0 || wrr == wpr)
1203 /* look for parent */
1204 for (i = 0; i < ScreenCount(display); i++)
1205 for (j = 0; j < SWM_WS_MAX; j++)
1206 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1214 spawn(struct swm_region *r, union arg *args)
1219 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1221 * The double-fork construct avoids zombie processes and keeps the code
1222 * clean from stupid signal handlers.
1227 close(ConnectionNumber(display));
1228 setenv("LD_PRELOAD", SWM_LIB, 1);
1229 if (asprintf(&ret, "%d", r->ws->idx)) {
1230 setenv("_SWM_WS", ret, 1);
1233 if (asprintf(&ret, "%d", getpid())) {
1234 setenv("_SWM_PID", ret, 1);
1238 /* kill stdin, mplayer, ssh-add etc. need that */
1239 si = open("/dev/null", O_RDONLY, 0);
1241 err(1, "open /dev/null");
1242 if (dup2(si, 0) == -1)
1243 err(1, "dup2 /dev/null");
1244 execvp(args->argv[0], args->argv);
1245 fprintf(stderr, "execvp failed\n");
1253 spawnterm(struct swm_region *r, union arg *args)
1255 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1258 setenv("_SWM_XTERM_FONTADJ", "", 1);
1263 kill_refs(struct ws_win *win)
1266 struct swm_region *r;
1267 struct workspace *ws;
1272 for (i = 0; i < ScreenCount(display); i++)
1273 TAILQ_FOREACH(r, &screens[i].rl, entry)
1274 for (x = 0; x < SWM_WS_MAX; x++) {
1276 if (win == ws->focus)
1278 if (win == ws->focus_prev)
1279 ws->focus_prev = NULL;
1284 validate_win(struct ws_win *testwin)
1287 struct workspace *ws;
1288 struct swm_region *r;
1289 int i, x, foundit = 0;
1291 if (testwin == NULL)
1294 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1295 TAILQ_FOREACH(r, &screens[i].rl, entry)
1296 for (x = 0; x < SWM_WS_MAX; x++) {
1298 TAILQ_FOREACH(win, &ws->winlist, entry)
1306 validate_ws(struct workspace *testws)
1308 struct swm_region *r;
1309 struct workspace *ws;
1312 /* validate all ws */
1313 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1314 TAILQ_FOREACH(r, &screens[i].rl, entry)
1315 for (x = 0; x < SWM_WS_MAX; x++) {
1324 unfocus_win(struct ws_win *win)
1328 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1332 if (win->ws == NULL)
1335 if (validate_ws(win->ws))
1338 if (win->ws->r == NULL)
1341 if (validate_win(win)) {
1346 if (win->ws->focus == win) {
1347 win->ws->focus = NULL;
1348 win->ws->focus_prev = win;
1351 if (validate_win(win->ws->focus)) {
1352 kill_refs(win->ws->focus);
1353 win->ws->focus = NULL;
1355 if (validate_win(win->ws->focus_prev)) {
1356 kill_refs(win->ws->focus_prev);
1357 win->ws->focus_prev = NULL;
1360 /* drain all previous unfocus events */
1361 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
1364 grabbuttons(win, 0);
1365 XSetWindowBorder(display, win->id,
1366 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1376 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
1378 for (i = 0; i < ScreenCount(display); i++)
1379 for (j = 0; j < SWM_WS_MAX; j++)
1380 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1385 focus_win(struct ws_win *win)
1390 struct ws_win *cfw = NULL;
1393 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1397 if (win->ws == NULL)
1400 if (validate_ws(win->ws))
1402 if (validate_win(win)) {
1407 if (validate_win(win)) {
1412 XGetInputFocus(display, &cur_focus, &rr);
1413 if ((cfw = find_window(cur_focus)) != NULL)
1416 win->ws->focus = win;
1418 if (win->ws->r != NULL) {
1419 /* drain all previous focus events */
1420 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
1424 XSetInputFocus(display, win->id,
1425 RevertToParent, CurrentTime);
1426 grabbuttons(win, 1);
1427 XSetWindowBorder(display, win->id,
1428 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1429 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS)
1430 XMapRaised(display, win->id);
1435 switchws(struct swm_region *r, union arg *args)
1437 int wsid = args->id;
1438 struct swm_region *this_r, *other_r;
1440 struct workspace *new_ws, *old_ws;
1447 old_ws = this_r->ws;
1448 new_ws = &this_r->s->ws[wsid];
1450 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1451 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1454 if (new_ws == NULL || old_ws == NULL)
1456 if (new_ws == old_ws)
1459 other_r = new_ws->r;
1460 if (other_r == NULL) {
1461 /* if the other workspace is hidden, switch windows */
1462 if (old_ws->r != NULL)
1463 old_ws->old_r = old_ws->r;
1466 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1469 other_r->ws = old_ws;
1470 old_ws->r = other_r;
1472 this_r->ws = new_ws;
1476 a.id = SWM_ARG_ID_FOCUSCUR;
1477 focus(new_ws->r, &a);
1482 cyclews(struct swm_region *r, union arg *args)
1485 struct swm_screen *s = r->s;
1487 DNPRINTF(SWM_D_WS, "cyclews id %d "
1488 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1489 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1494 case SWM_ARG_ID_CYCLEWS_UP:
1495 if (a.id < SWM_WS_MAX - 1)
1500 case SWM_ARG_ID_CYCLEWS_DOWN:
1504 a.id = SWM_WS_MAX - 1;
1510 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1512 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1516 } while (a.id != r->ws->idx);
1520 cyclescr(struct swm_region *r, union arg *args)
1522 struct swm_region *rr = NULL;
1526 /* do nothing if we don't have more than one screen */
1527 if (!(ScreenCount(display) > 1 || outputs > 1))
1532 case SWM_ARG_ID_CYCLESC_UP:
1533 rr = TAILQ_NEXT(r, entry);
1535 rr = TAILQ_FIRST(&screens[i].rl);
1537 case SWM_ARG_ID_CYCLESC_DOWN:
1538 rr = TAILQ_PREV(r, swm_region_list, entry);
1540 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1548 /* move mouse to region */
1550 y = rr->g.y + 1 + bar_enabled ? bar_height : 0;
1551 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1553 a.id = SWM_ARG_ID_FOCUSCUR;
1556 if (rr->ws->focus) {
1557 /* move to focus window */
1558 x = rr->ws->focus->g.x + 1;
1559 y = rr->ws->focus->g.y + 1;
1560 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1565 swapwin(struct swm_region *r, union arg *args)
1567 struct ws_win *target, *source;
1568 struct ws_win *cur_focus;
1569 struct ws_win_list *wl;
1572 DNPRINTF(SWM_D_WS, "swapwin id %d "
1573 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1574 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1576 cur_focus = r->ws->focus;
1577 if (cur_focus == NULL)
1581 wl = &source->ws->winlist;
1584 case SWM_ARG_ID_SWAPPREV:
1585 target = TAILQ_PREV(source, ws_win_list, entry);
1586 TAILQ_REMOVE(wl, cur_focus, entry);
1588 TAILQ_INSERT_TAIL(wl, source, entry);
1590 TAILQ_INSERT_BEFORE(target, source, entry);
1592 case SWM_ARG_ID_SWAPNEXT:
1593 target = TAILQ_NEXT(source, entry);
1594 TAILQ_REMOVE(wl, source, entry);
1596 TAILQ_INSERT_HEAD(wl, source, entry);
1598 TAILQ_INSERT_AFTER(wl, target, source, entry);
1600 case SWM_ARG_ID_SWAPMAIN:
1601 target = TAILQ_FIRST(wl);
1602 if (target == source) {
1603 if (source->ws->focus_prev != NULL &&
1604 source->ws->focus_prev != target)
1606 source = source->ws->focus_prev;
1610 if (target == NULL || source == NULL)
1612 source->ws->focus_prev = target;
1613 TAILQ_REMOVE(wl, target, entry);
1614 TAILQ_INSERT_BEFORE(source, target, entry);
1615 TAILQ_REMOVE(wl, source, entry);
1616 TAILQ_INSERT_HEAD(wl, source, entry);
1619 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1627 focus_prev(struct ws_win *win)
1629 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
1630 struct ws_win *cur_focus = NULL;
1631 struct ws_win_list *wl = NULL;
1632 struct workspace *ws = NULL;
1634 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
1636 if (!(win && win->ws))
1641 cur_focus = ws->focus;
1642 winlostfocus = cur_focus;
1644 /* pickle, just focus on whatever */
1645 if (cur_focus == NULL) {
1646 /* use prev_focus if valid */
1647 if (ws->focus_prev && ws->focus_prev != cur_focus &&
1648 find_window(WINID(ws->focus_prev)))
1649 winfocus = ws->focus_prev;
1650 if (winfocus == NULL)
1651 winfocus = TAILQ_FIRST(wl);
1655 /* if transient focus on parent */
1656 if (cur_focus->transient) {
1657 winfocus = find_window(cur_focus->transient);
1661 /* if in max_stack try harder */
1662 if (ws->cur_layout->flags & SWM_L_FOCUSPREV) {
1663 if (cur_focus != ws->focus_prev)
1664 winfocus = ws->focus_prev;
1665 else if (cur_focus != ws->focus)
1666 winfocus = ws->focus;
1668 winfocus = TAILQ_PREV(win, ws_win_list, entry);
1673 if (cur_focus == win)
1674 winfocus = TAILQ_PREV(win, ws_win_list, entry);
1675 if (winfocus == NULL)
1676 winfocus = TAILQ_LAST(wl, ws_win_list);
1677 if (winfocus == NULL || winfocus == win)
1678 winfocus = TAILQ_NEXT(cur_focus, entry);
1680 if (winfocus == winlostfocus || winfocus == NULL)
1683 focus_magic(winfocus, SWM_F_GENERIC);
1687 focus(struct swm_region *r, union arg *args)
1689 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
1690 struct ws_win *cur_focus = NULL;
1691 struct ws_win_list *wl = NULL;
1692 struct workspace *ws = NULL;
1697 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1699 /* treat FOCUS_CUR special */
1700 if (args->id == SWM_ARG_ID_FOCUSCUR) {
1702 winfocus = r->ws->focus;
1703 else if (r->ws->focus_prev)
1704 winfocus = r->ws->focus_prev;
1706 winfocus = TAILQ_FIRST(&r->ws->winlist);
1708 focus_magic(winfocus, SWM_F_GENERIC);
1712 if ((cur_focus = r->ws->focus) == NULL)
1717 winlostfocus = cur_focus;
1720 case SWM_ARG_ID_FOCUSPREV:
1721 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1722 if (winfocus == NULL)
1723 winfocus = TAILQ_LAST(wl, ws_win_list);
1726 case SWM_ARG_ID_FOCUSNEXT:
1727 winfocus = TAILQ_NEXT(cur_focus, entry);
1728 if (winfocus == NULL)
1729 winfocus = TAILQ_FIRST(wl);
1732 case SWM_ARG_ID_FOCUSMAIN:
1733 winfocus = TAILQ_FIRST(wl);
1734 if (winfocus == cur_focus)
1735 winfocus = cur_focus->ws->focus_prev;
1742 if (winfocus == winlostfocus || winfocus == NULL)
1745 focus_magic(winfocus, SWM_F_GENERIC);
1749 cycle_layout(struct swm_region *r, union arg *args)
1751 struct workspace *ws = r->ws;
1752 struct ws_win *winfocus;
1755 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1757 winfocus = ws->focus;
1760 if (ws->cur_layout->l_stack == NULL)
1761 ws->cur_layout = &layouts[0];
1764 a.id = SWM_ARG_ID_FOCUSCUR;
1770 stack_config(struct swm_region *r, union arg *args)
1772 struct workspace *ws = r->ws;
1774 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1777 if (ws->cur_layout->l_config != NULL)
1778 ws->cur_layout->l_config(ws, args->id);
1780 if (args->id != SWM_ARG_ID_STACKINIT);
1786 struct swm_geometry g;
1787 struct swm_region *r;
1790 DNPRINTF(SWM_D_STACK, "stack\n");
1792 for (i = 0; i < ScreenCount(display); i++) {
1794 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1795 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1796 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1798 /* start with screen geometry, adjust for bar */
1806 r->ws->cur_layout->l_stack(r->ws, &g);
1814 stack_floater(struct ws_win *win, struct swm_region *r)
1822 bzero(&wc, sizeof wc);
1823 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1824 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
1825 (win->g.h >= HEIGHT(r)))
1826 wc.border_width = 0;
1828 wc.border_width = 1;
1829 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
1830 win->g.w = (double)WIDTH(r) * dialog_ratio;
1831 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1833 wc.width = win->g.w;
1834 wc.height = win->g.h;
1839 wc.x = (WIDTH(r) - win->g.w) / 2;
1840 wc.y = (HEIGHT(r) - win->g.h) / 2;
1843 /* adjust for region */
1851 win->g.w = wc.width;
1852 win->g.h = wc.height;
1854 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
1855 win->id, wc.x, wc.y, wc.width, wc.height);
1857 XConfigureWindow(display, win->id, mask, &wc);
1862 * Send keystrokes to terminal to decrease/increase the font size as the
1863 * window size changes.
1866 adjust_font(struct ws_win *win)
1868 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
1869 win->floating || win->transient)
1872 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
1873 win->g.w / win->sh.width_inc < term_width &&
1874 win->font_steps < SWM_MAX_FONT_STEPS) {
1875 win->font_size_boundary[win->font_steps] =
1876 (win->sh.width_inc * term_width) + win->sh.base_width;
1879 win->last_inc = win->sh.width_inc;
1880 fake_keypress(win, XK_KP_Subtract, ShiftMask);
1881 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
1882 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
1885 win->last_inc = win->sh.width_inc;
1886 fake_keypress(win, XK_KP_Add, ShiftMask);
1890 #define SWAPXY(g) do { \
1892 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1893 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1896 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1899 XWindowAttributes wa;
1900 struct swm_geometry win_g, r_g = *g;
1902 int i, j, s, stacks;
1903 int w_inc = 1, h_inc, w_base = 1, h_base;
1904 int hrh, extra = 0, h_slice, last_h = 0;
1905 int split, colno, winno, mwin, msize, mscale;
1906 int remain, missing, v_slice, reconfigure;
1909 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1910 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1912 winno = count_win(ws, 0);
1913 if (winno == 0 && count_win(ws, 1) == 0)
1916 TAILQ_FOREACH(win, &ws->winlist, entry)
1917 if (win->transient == 0 && win->floating == 0)
1924 w_inc = win->sh.width_inc;
1925 w_base = win->sh.base_width;
1926 mwin = ws->l_state.horizontal_mwin;
1927 mscale = ws->l_state.horizontal_msize;
1928 stacks = ws->l_state.horizontal_stacks;
1931 w_inc = win->sh.height_inc;
1932 w_base = win->sh.base_height;
1933 mwin = ws->l_state.vertical_mwin;
1934 mscale = ws->l_state.vertical_msize;
1935 stacks = ws->l_state.vertical_stacks;
1939 if (stacks > winno - mwin)
1940 stacks = winno - mwin;
1944 h_slice = r_g.h / SWM_H_SLICE;
1945 if (mwin && winno > mwin) {
1946 v_slice = r_g.w / SWM_V_SLICE;
1950 win_g.w = v_slice * mscale;
1952 if (w_inc > 1 && w_inc < v_slice) {
1953 /* adjust for window's requested size increment */
1954 remain = (win_g.w - w_base) % w_inc;
1955 missing = w_inc - remain;
1962 win_g.x += r_g.w - msize;
1965 colno = split = winno / stacks;
1966 win_g.w = ((r_g.w - (stacks * 2) + 2) / stacks);
1968 hrh = r_g.h / colno;
1969 extra = r_g.h - (colno * hrh);
1972 /* stack all the tiled windows */
1973 i = j = 0, s = stacks;
1974 TAILQ_FOREACH(win, &ws->winlist, entry) {
1975 if (win->transient != 0 || win->floating != 0)
1978 if (split && i == split) {
1979 colno = (winno - mwin) / stacks;
1980 if (s <= (winno - mwin) % stacks)
1982 split = split + colno;
1983 hrh = (r_g.h / colno);
1984 extra = r_g.h - (colno * hrh);
1988 win_g.x += win_g.w + 2;
1989 win_g.w = (r_g.w - msize - (stacks * 2)) / stacks;
1991 win_g.w += (r_g.w - msize - (stacks * 2)) %
1998 h_inc = win->sh.width_inc;
1999 h_base = win->sh.base_width;
2001 h_inc = win->sh.height_inc;
2002 h_base = win->sh.base_height;
2004 if (j == colno - 1) {
2005 win_g.h = hrh + extra;
2006 } else if (h_inc > 1 && h_inc < h_slice) {
2007 /* adjust for window's requested size increment */
2008 remain = (win_g.h - h_base) % h_inc;
2009 missing = h_inc - remain;
2011 if (missing <= extra || j == 0) {
2023 win_g.y += last_h + 2;
2025 bzero(&wc, sizeof wc);
2026 wc.border_width = 1;
2029 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2030 win->g.w != win_g.h || win->g.h != win_g.w) {
2032 win->g.x = wc.x = win_g.y;
2033 win->g.y = wc.y = win_g.x;
2034 win->g.w = wc.width = win_g.h;
2035 win->g.h = wc.height = win_g.w;
2038 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2039 win->g.w != win_g.w || win->g.h != win_g.h) {
2041 win->g.x = wc.x = win_g.x;
2042 win->g.y = wc.y = win_g.y;
2043 win->g.w = wc.width = win_g.w;
2044 win->g.h = wc.height = win_g.h;
2049 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2050 XConfigureWindow(display, win->id, mask, &wc);
2054 if (XGetWindowAttributes(display, win->id, &wa))
2055 if (wa.map_state == IsUnmapped)
2056 XMapRaised(display, win->id);
2064 /* now, stack all the floaters and transients */
2065 TAILQ_FOREACH(win, &ws->winlist, entry) {
2066 if (win->transient == 0 && win->floating == 0)
2069 stack_floater(win, ws->r);
2070 XMapRaised(display, win->id);
2075 vertical_config(struct workspace *ws, int id)
2077 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2080 case SWM_ARG_ID_STACKRESET:
2081 case SWM_ARG_ID_STACKINIT:
2082 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2083 ws->l_state.vertical_mwin = 1;
2084 ws->l_state.vertical_stacks = 1;
2086 case SWM_ARG_ID_MASTERSHRINK:
2087 if (ws->l_state.vertical_msize > 1)
2088 ws->l_state.vertical_msize--;
2090 case SWM_ARG_ID_MASTERGROW:
2091 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2092 ws->l_state.vertical_msize++;
2094 case SWM_ARG_ID_MASTERADD:
2095 ws->l_state.vertical_mwin++;
2097 case SWM_ARG_ID_MASTERDEL:
2098 if (ws->l_state.vertical_mwin > 0)
2099 ws->l_state.vertical_mwin--;
2101 case SWM_ARG_ID_STACKINC:
2102 ws->l_state.vertical_stacks++;
2104 case SWM_ARG_ID_STACKDEC:
2105 if (ws->l_state.vertical_stacks > 1)
2106 ws->l_state.vertical_stacks--;
2114 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2116 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2118 stack_master(ws, g, 0, 0);
2122 horizontal_config(struct workspace *ws, int id)
2124 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2127 case SWM_ARG_ID_STACKRESET:
2128 case SWM_ARG_ID_STACKINIT:
2129 ws->l_state.horizontal_mwin = 1;
2130 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2131 ws->l_state.horizontal_stacks = 1;
2133 case SWM_ARG_ID_MASTERSHRINK:
2134 if (ws->l_state.horizontal_msize > 1)
2135 ws->l_state.horizontal_msize--;
2137 case SWM_ARG_ID_MASTERGROW:
2138 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2139 ws->l_state.horizontal_msize++;
2141 case SWM_ARG_ID_MASTERADD:
2142 ws->l_state.horizontal_mwin++;
2144 case SWM_ARG_ID_MASTERDEL:
2145 if (ws->l_state.horizontal_mwin > 0)
2146 ws->l_state.horizontal_mwin--;
2148 case SWM_ARG_ID_STACKINC:
2149 ws->l_state.horizontal_stacks++;
2151 case SWM_ARG_ID_STACKDEC:
2152 if (ws->l_state.horizontal_stacks > 1)
2153 ws->l_state.horizontal_stacks--;
2161 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2163 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2165 stack_master(ws, g, 1, 0);
2168 /* fullscreen view */
2170 max_stack(struct workspace *ws, struct swm_geometry *g)
2173 struct swm_geometry gg = *g;
2174 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2178 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2183 winno = count_win(ws, 0);
2184 if (winno == 0 && count_win(ws, 1) == 0)
2187 TAILQ_FOREACH(win, &ws->winlist, entry) {
2188 if (win->transient) {
2190 parent = find_window(win->transient);
2194 /* only reconfigure if necessary */
2195 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2197 bzero(&wc, sizeof wc);
2198 wc.border_width = 1;
2199 win->g.x = wc.x = gg.x;
2200 win->g.y = wc.y = gg.y;
2201 win->g.w = wc.width = gg.w;
2202 win->g.h = wc.height = gg.h;
2203 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2204 XConfigureWindow(display, win->id, mask, &wc);
2207 /* unmap only if we don't have multi screen */
2208 if (win != ws->focus)
2209 if (!(ScreenCount(display) > 1 || outputs > 1))
2213 /* put the last transient on top */
2216 XMapRaised(display, parent->id);
2217 stack_floater(wintrans, ws->r);
2218 focus_magic(wintrans, SWM_F_TRANSIENT);
2223 send_to_ws(struct swm_region *r, union arg *args)
2225 int wsid = args->id;
2226 struct ws_win *win = win;
2227 struct workspace *ws, *nws;
2228 Atom ws_idx_atom = 0;
2229 unsigned char ws_idx_str[SWM_PROPLEN];
2238 if (win->ws->idx == wsid)
2241 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2244 nws = &win->s->ws[wsid];
2246 a.id = SWM_ARG_ID_FOCUSPREV;
2249 TAILQ_REMOVE(&ws->winlist, win, entry);
2250 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2253 /* Try to update the window's workspace property */
2254 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2256 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2257 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2259 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2260 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2266 wkill(struct swm_region *r, union arg *args)
2268 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
2270 if(r->ws->focus == NULL)
2273 if (args->id == SWM_ARG_ID_KILLWINDOW)
2274 XKillClient(display, r->ws->focus->id);
2276 if (r->ws->focus->can_delete)
2277 client_msg(r->ws->focus, adelete);
2281 floating_toggle(struct swm_region *r, union arg *args)
2283 struct ws_win *win = r->ws->focus;
2289 win->floating = !win->floating;
2292 a.id = SWM_ARG_ID_FOCUSCUR;
2293 focus(win->ws->r, &a);
2297 resize_window(struct ws_win *win, int center)
2301 struct swm_region *r;
2303 r = root_to_region(win->wa.root);
2304 bzero(&wc, sizeof wc);
2305 mask = CWBorderWidth | CWWidth | CWHeight;
2306 wc.border_width = 1;
2307 wc.width = win->g.w;
2308 wc.height = win->g.h;
2309 if (center == SWM_ARG_ID_CENTER) {
2310 wc.x = (WIDTH(r) - win->g.w) / 2;
2311 wc.y = (HEIGHT(r) - win->g.h) / 2;
2315 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
2316 win->id, wc.x, wc.y, wc.width, wc.height);
2318 XConfigureWindow(display, win->id, mask, &wc);
2323 resize(struct ws_win *win, union arg *args)
2327 struct swm_region *r = win->ws->r;
2329 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
2330 win->id, win->floating, win->transient);
2332 if (!(win->transient != 0 || win->floating != 0))
2335 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2336 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2338 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w, win->g.h);
2340 XMaskEvent(display, MOUSEMASK | ExposureMask |
2341 SubstructureRedirectMask, &ev);
2343 case ConfigureRequest:
2346 handler[ev.type](&ev);
2349 /* do not allow resize outside of region */
2350 if (ev.xmotion.y_root < r->g.y ||
2351 ev.xmotion.y_root >= r->g.y + r->g.h - 1)
2353 if (ev.xmotion.x_root < r->g.x ||
2354 ev.xmotion.x_root >= r->g.x + r->g.w - 1)
2357 if (ev.xmotion.x <= 1)
2359 if (ev.xmotion.y <= 1)
2361 win->g.w = ev.xmotion.x;
2362 win->g.h = ev.xmotion.y;
2364 /* not free, don't sync more than 60 times / second */
2365 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2366 time = ev.xmotion.time;
2367 XSync(display, False);
2368 resize_window(win, args->id);
2372 } while (ev.type != ButtonRelease);
2374 XSync(display, False);
2375 resize_window(win, args->id);
2377 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
2379 XUngrabPointer(display, CurrentTime);
2382 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2386 move_window(struct ws_win *win)
2390 struct swm_region *r;
2392 r = root_to_region(win->wa.root);
2393 bzero(&wc, sizeof wc);
2398 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
2399 win->id, wc.x, wc.y, wc.width, wc.height);
2401 XConfigureWindow(display, win->id, mask, &wc);
2406 move(struct ws_win *win, union arg *args)
2411 struct swm_region *r = win->ws->r;
2413 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
2414 win->id, win->floating, win->transient);
2416 if (win->floating == 0) {
2422 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2423 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2425 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2427 XMaskEvent(display, MOUSEMASK | ExposureMask |
2428 SubstructureRedirectMask, &ev);
2430 case ConfigureRequest:
2433 handler[ev.type](&ev);
2436 /* don't allow to move window out of region */
2437 if (ev.xmotion.y_root < r->g.y ||
2438 ev.xmotion.y_root + win->g.h >= r->g.y + r->g.h - 1)
2440 if (ev.xmotion.x_root < r->g.x ||
2441 ev.xmotion.x_root + win->g.w >= r->g.x + r->g.w - 1)
2444 win->g.x = ev.xmotion.x_root;
2445 win->g.y = ev.xmotion.y_root;
2447 /* not free, don't sync more than 60 times / second */
2448 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2449 time = ev.xmotion.time;
2450 XSync(display, False);
2455 } while (ev.type != ButtonRelease);
2457 XSync(display, False);
2460 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2461 XUngrabPointer(display, CurrentTime);
2466 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2469 /* user/key callable function IDs */
2527 /* key definitions */
2529 dummykeyfunc(struct swm_region *r, union arg *args)
2534 legacyfunc(struct swm_region *r, union arg *args)
2539 char name[SWM_FUNCNAME_LEN];
2540 void (*func)(struct swm_region *r, union arg *);
2542 } keyfuncs[kf_invalid + 1] = {
2543 /* name function argument */
2544 { "cycle_layout", cycle_layout, {0} },
2545 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
2546 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
2547 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
2548 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
2549 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
2550 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
2551 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
2552 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
2553 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
2554 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
2555 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
2556 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
2557 { "spawn_term", spawnterm, {.argv = spawn_term} },
2558 { "spawn_menu", legacyfunc, {0} },
2559 { "quit", quit, {0} },
2560 { "restart", restart, {0} },
2561 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
2562 { "ws_1", switchws, {.id = 0} },
2563 { "ws_2", switchws, {.id = 1} },
2564 { "ws_3", switchws, {.id = 2} },
2565 { "ws_4", switchws, {.id = 3} },
2566 { "ws_5", switchws, {.id = 4} },
2567 { "ws_6", switchws, {.id = 5} },
2568 { "ws_7", switchws, {.id = 6} },
2569 { "ws_8", switchws, {.id = 7} },
2570 { "ws_9", switchws, {.id = 8} },
2571 { "ws_10", switchws, {.id = 9} },
2572 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
2573 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
2574 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
2575 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
2576 { "mvws_1", send_to_ws, {.id = 0} },
2577 { "mvws_2", send_to_ws, {.id = 1} },
2578 { "mvws_3", send_to_ws, {.id = 2} },
2579 { "mvws_4", send_to_ws, {.id = 3} },
2580 { "mvws_5", send_to_ws, {.id = 4} },
2581 { "mvws_6", send_to_ws, {.id = 5} },
2582 { "mvws_7", send_to_ws, {.id = 6} },
2583 { "mvws_8", send_to_ws, {.id = 7} },
2584 { "mvws_9", send_to_ws, {.id = 8} },
2585 { "mvws_10", send_to_ws, {.id = 9} },
2586 { "bar_toggle", bar_toggle, {0} },
2587 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
2588 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
2589 { "screenshot_all", legacyfunc, {0} },
2590 { "screenshot_wind", legacyfunc, {0} },
2591 { "float_toggle", floating_toggle,{0} },
2592 { "version", version, {0} },
2593 { "spawn_lock", legacyfunc, {0} },
2594 { "spawn_initscr", legacyfunc, {0} },
2595 { "spawn_custom", dummykeyfunc, {0} },
2596 { "dumpwins", dumpwins, {0} },
2597 { "invalid key func", NULL, {0} },
2602 enum keyfuncid funcid;
2605 int keys_size = 0, keys_length = 0;
2606 struct key *keys = NULL;
2609 enum { client_click, root_click };
2611 unsigned int action;
2613 unsigned int button;
2614 void (*func)(struct ws_win *, union arg *);
2617 /* action key mouse button func args */
2618 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
2619 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
2620 { client_click, MODKEY, Button1, move, {0} },
2624 update_modkey(unsigned int mod)
2629 for (i = 0; i < keys_length; i++)
2630 if (keys[i].mod & ShiftMask)
2631 keys[i].mod = mod | ShiftMask;
2635 for (i = 0; i < LENGTH(buttons); i++)
2636 if (buttons[i].mask & ShiftMask)
2637 buttons[i].mask = mod | ShiftMask;
2639 buttons[i].mask = mod;
2649 int spawns_size = 0, spawns_length = 0;
2650 struct spawn_prog *spawns = NULL;
2653 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
2656 struct spawn_prog *prog = NULL;
2658 char *ap, **real_args;
2660 DNPRINTF(SWM_D_SPAWN, "spawn_custom %s\n", spawn_name);
2663 for (i = 0; i < spawns_length; i++) {
2664 if (!strcasecmp(spawn_name, spawns[i].name))
2668 fprintf(stderr, "spawn_custom: program %s not found\n",
2673 /* make room for expanded args */
2674 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
2675 err(1, "spawn_custom: calloc real_args");
2677 /* expand spawn_args into real_args */
2678 for (i = 0; i < prog->argc; i++) {
2680 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
2681 if (!strcasecmp(ap, "$bar_border")) {
2683 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
2685 err(1, "spawn_custom border color");
2686 } else if (!strcasecmp(ap, "$bar_color")) {
2688 strdup(r->s->c[SWM_S_COLOR_BAR].name))
2690 err(1, "spawn_custom bar color");
2691 } else if (!strcasecmp(ap, "$bar_font")) {
2692 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
2694 err(1, "spawn_custom bar fonts");
2695 } else if (!strcasecmp(ap, "$bar_font_color")) {
2697 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
2699 err(1, "spawn_custom color font");
2700 } else if (!strcasecmp(ap, "$color_focus")) {
2702 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
2704 err(1, "spawn_custom color focus");
2705 } else if (!strcasecmp(ap, "$color_unfocus")) {
2707 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
2709 err(1, "spawn_custom color unfocus");
2711 /* no match --> copy as is */
2712 if ((real_args[i] = strdup(ap)) == NULL)
2713 err(1, "spawn_custom strdup(ap)");
2715 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
2720 if ((swm_debug & SWM_D_SPAWN) != 0) {
2721 fprintf(stderr, "spawn_custom: result = ");
2722 for (i = 0; i < prog->argc; i++)
2723 fprintf(stderr, "\"%s\" ", real_args[i]);
2724 fprintf(stderr, "\n");
2730 for (i = 0; i < prog->argc; i++)
2736 setspawn(struct spawn_prog *prog)
2740 if (prog == NULL || prog->name == NULL)
2744 for (i = 0; i < spawns_length; i++) {
2745 if (!strcmp(spawns[i].name, prog->name)) {
2747 if (prog->argv == NULL) {
2749 DNPRINTF(SWM_D_SPAWN,
2750 "setspawn: delete #%d %s\n",
2752 free(spawns[i].name);
2753 for (j = 0; j < spawns[i].argc; j++)
2754 free(spawns[i].argv[j]);
2755 free(spawns[i].argv);
2756 j = spawns_length - 1;
2758 spawns[i] = spawns[j];
2763 DNPRINTF(SWM_D_SPAWN,
2764 "setspawn: replace #%d %s\n",
2766 free(spawns[i].name);
2767 for (j = 0; j < spawns[i].argc; j++)
2768 free(spawns[i].argv[j]);
2769 free(spawns[i].argv);
2772 /* found case handled */
2778 if (prog->argv == NULL) {
2780 "error: setspawn: cannot find program %s", prog->name);
2785 /* not found: add */
2786 if (spawns_size == 0 || spawns == NULL) {
2788 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
2789 spawns = malloc((size_t)spawns_size *
2790 sizeof(struct spawn_prog));
2791 if (spawns == NULL) {
2792 fprintf(stderr, "setspawn: malloc failed\n");
2796 } else if (spawns_length == spawns_size) {
2798 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
2799 spawns = realloc(spawns, (size_t)spawns_size *
2800 sizeof(struct spawn_prog));
2801 if (spawns == NULL) {
2802 fprintf(stderr, "setspawn: realloc failed\n");
2808 if (spawns_length < spawns_size) {
2809 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
2810 spawns_length, prog->name);
2811 i = spawns_length++;
2814 fprintf(stderr, "spawns array problem?\n");
2815 if (spawns == NULL) {
2816 fprintf(stderr, "spawns array is NULL!\n");
2824 setconfspawn(char *selector, char *value, int flags)
2826 struct spawn_prog *prog;
2827 char *vp, *cp, *word;
2829 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
2830 if ((prog = calloc(1, sizeof *prog)) == NULL)
2831 err(1, "setconfspawn: calloc prog");
2832 prog->name = strdup(selector);
2833 if (prog->name == NULL)
2834 err(1, "setconfspawn prog->name");
2835 if ((cp = vp = strdup(value)) == NULL)
2836 err(1, "setconfspawn: strdup(value) ");
2837 while ((word = strsep(&cp, " \t")) != NULL) {
2838 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
2840 cp += (long)strspn(cp, " \t");
2841 if (strlen(word) > 0) {
2843 prog->argv = realloc(prog->argv,
2844 prog->argc * sizeof(char *));
2845 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
2846 err(1, "setconfspawn: strdup");
2853 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
2860 setconfspawn("term", "xterm", 0);
2861 setconfspawn("screenshot_all", "screenshot.sh full", 0);
2862 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
2863 setconfspawn("lock", "xlock", 0);
2864 setconfspawn("initscr", "initscreen.sh", 0);
2865 setconfspawn("menu", "dmenu_run"
2868 " -nf $bar_font_color"
2870 " -sf $bar_color", 0);
2874 #define SWM_MODNAME_SIZE 32
2875 #define SWM_KEY_WS "\n+ \t"
2877 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
2881 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
2882 if (mod == NULL || ks == NULL) {
2883 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
2886 if (keystr == NULL || strlen(keystr) == 0) {
2887 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
2893 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
2894 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
2896 cp += (long)strspn(cp, SWM_KEY_WS);
2897 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
2899 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
2901 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
2903 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
2905 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
2907 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
2909 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
2910 *mod |= ControlMask;
2912 *ks = XStringToKeysym(name);
2913 XConvertCase(*ks, ks, &uks);
2914 if (ks == NoSymbol) {
2916 "parsekeys: invalid key %s\n",
2922 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
2927 strdupsafe(char *str)
2932 return (strdup(str));
2936 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
2939 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
2940 keyfuncs[kfid].name, spawn_name);
2942 for (i = 0; i < keys_length; i++) {
2943 if (keys[i].mod == mod && keys[i].keysym == ks) {
2944 if (kfid == kf_invalid) {
2947 "setkeybinding: delete #%d %s\n",
2948 i, keyfuncs[keys[i].funcid].name);
2949 free(keys[i].spawn_name);
2950 j = keys_length - 1;
2954 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2957 /* found: replace */
2959 "setkeybinding: replace #%d %s %s\n",
2960 i, keyfuncs[keys[i].funcid].name,
2962 free(keys[i].spawn_name);
2964 keys[i].keysym = ks;
2965 keys[i].funcid = kfid;
2966 keys[i].spawn_name = strdupsafe(spawn_name);
2967 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2972 if (kfid == kf_invalid) {
2974 "error: setkeybinding: cannot find mod/key combination");
2975 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2978 /* not found: add */
2979 if (keys_size == 0 || keys == NULL) {
2981 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
2982 keys = malloc((size_t)keys_size * sizeof(struct key));
2984 fprintf(stderr, "malloc failed\n");
2988 } else if (keys_length == keys_size) {
2990 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
2991 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
2993 fprintf(stderr, "realloc failed\n");
2998 if (keys_length < keys_size) {
3000 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
3001 j, keyfuncs[kfid].name, spawn_name);
3003 keys[j].keysym = ks;
3004 keys[j].funcid = kfid;
3005 keys[j].spawn_name = strdupsafe(spawn_name);
3007 fprintf(stderr, "keys array problem?\n");
3009 fprintf(stderr, "keys array problem\n");
3013 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3017 setconfbinding(char *selector, char *value, int flags)
3019 enum keyfuncid kfid;
3023 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
3024 if (selector == NULL) {
3025 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
3026 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3028 setkeybinding(mod, ks, kfid, NULL);
3033 /* search by key function name */
3034 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
3035 if (strncasecmp(selector, keyfuncs[kfid].name,
3036 SWM_FUNCNAME_LEN) == 0) {
3037 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3039 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3040 setkeybinding(mod, ks, kfid, NULL);
3046 /* search by custom spawn name */
3047 for (i = 0; i < spawns_length; i++) {
3048 if (strcasecmp(selector, spawns[i].name) == 0) {
3049 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3051 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3052 setkeybinding(mod, ks, kf_spawn_custom,
3059 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
3066 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
3067 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
3068 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
3069 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
3070 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
3071 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
3072 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
3073 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
3074 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
3075 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
3076 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
3077 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
3078 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
3079 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
3080 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
3081 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
3082 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
3083 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
3084 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
3085 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
3086 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
3087 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
3088 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
3089 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
3090 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
3091 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
3092 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
3093 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
3094 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
3095 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
3096 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
3097 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
3098 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
3099 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
3100 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
3101 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
3102 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
3103 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
3104 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
3105 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
3106 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
3107 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
3108 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
3109 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
3110 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
3111 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
3112 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
3113 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
3114 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
3115 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
3116 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
3117 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
3118 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
3120 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
3125 updatenumlockmask(void)
3128 XModifierKeymap *modmap;
3130 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
3132 modmap = XGetModifierMapping(display);
3133 for (i = 0; i < 8; i++)
3134 for (j = 0; j < modmap->max_keypermod; j++)
3135 if (modmap->modifiermap[i * modmap->max_keypermod + j]
3136 == XKeysymToKeycode(display, XK_Num_Lock))
3137 numlockmask = (1 << i);
3139 XFreeModifiermap(modmap);
3145 unsigned int i, j, k;
3147 unsigned int modifiers[] =
3148 { 0, LockMask, numlockmask, numlockmask | LockMask };
3150 DNPRINTF(SWM_D_MISC, "grabkeys\n");
3151 updatenumlockmask();
3153 for (k = 0; k < ScreenCount(display); k++) {
3154 if (TAILQ_EMPTY(&screens[k].rl))
3156 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
3157 for (i = 0; i < keys_length; i++) {
3158 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
3159 for (j = 0; j < LENGTH(modifiers); j++)
3160 XGrabKey(display, code,
3161 keys[i].mod | modifiers[j],
3162 screens[k].root, True,
3163 GrabModeAsync, GrabModeAsync);
3169 grabbuttons(struct ws_win *win, int focused)
3172 unsigned int modifiers[] =
3173 { 0, LockMask, numlockmask, numlockmask|LockMask };
3175 updatenumlockmask();
3176 XUngrabButton(display, AnyButton, AnyModifier, win->id);
3178 for (i = 0; i < LENGTH(buttons); i++)
3179 if (buttons[i].action == client_click)
3180 for (j = 0; j < LENGTH(modifiers); j++)
3181 XGrabButton(display, buttons[i].button,
3182 buttons[i].mask | modifiers[j],
3183 win->id, False, BUTTONMASK,
3184 GrabModeAsync, GrabModeSync, None,
3187 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
3188 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
3191 const char *quirkname[] = {
3192 "NONE", /* config string for "no value" */
3200 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
3201 #define SWM_Q_WS "\n|+ \t"
3203 parsequirks(char *qstr, unsigned long *quirk)
3213 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
3215 cp += (long)strspn(cp, SWM_Q_WS);
3216 for (i = 0; i < LENGTH(quirkname); i++) {
3217 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
3218 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
3223 *quirk |= 1 << (i-1);
3227 if (i >= LENGTH(quirkname)) {
3228 DNPRINTF(SWM_D_QUIRK,
3229 "parsequirks: invalid quirk [%s]\n", name);
3237 setquirk(const char *class, const char *name, const int quirk)
3242 for (i = 0; i < quirks_length; i++) {
3243 if (!strcmp(quirks[i].class, class) &&
3244 !strcmp(quirks[i].name, name)) {
3247 DNPRINTF(SWM_D_QUIRK,
3248 "setquirk: delete #%d %s:%s\n",
3249 i, quirks[i].class, quirks[i].name);
3250 free(quirks[i].class);
3251 free(quirks[i].name);
3252 j = quirks_length - 1;
3254 quirks[i] = quirks[j];
3258 /* found: replace */
3259 DNPRINTF(SWM_D_QUIRK,
3260 "setquirk: replace #%d %s:%s\n",
3261 i, quirks[i].class, quirks[i].name);
3262 free(quirks[i].class);
3263 free(quirks[i].name);
3264 quirks[i].class = strdup(class);
3265 quirks[i].name = strdup(name);
3266 quirks[i].quirk = quirk;
3273 "error: setquirk: cannot find class/name combination");
3276 /* not found: add */
3277 if (quirks_size == 0 || quirks == NULL) {
3279 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
3280 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
3282 fprintf(stderr, "setquirk: malloc failed\n");
3286 } else if (quirks_length == quirks_size) {
3288 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
3289 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
3291 fprintf(stderr, "setquirk: realloc failed\n");
3296 if (quirks_length < quirks_size) {
3297 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
3298 j = quirks_length++;
3299 quirks[j].class = strdup(class);
3300 quirks[j].name = strdup(name);
3301 quirks[j].quirk = quirk;
3303 fprintf(stderr, "quirks array problem?\n");
3305 fprintf(stderr, "quirks array problem!\n");
3312 setconfquirk(char *selector, char *value, int flags)
3314 char *cp, *class, *name;
3316 unsigned long quirks;
3317 if (selector == NULL)
3319 if ((cp = strchr(selector, ':')) == NULL)
3324 if ((retval = parsequirks(value, &quirks)) == 0)
3325 setquirk(class, name, quirks);
3332 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
3333 setquirk("OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT);
3334 setquirk("OpenOffice.org 3.0", "VCLSalFrame", SWM_Q_FLOAT);
3335 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
3336 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
3337 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3338 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
3339 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3340 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3341 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3342 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3343 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
3344 setquirk("pcb", "pcb", SWM_Q_FLOAT);
3347 /* conf file stuff */
3348 #define SWM_CONF_FILE "scrotwm.conf"
3350 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_STACK_ENABLED,
3351 SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT, SWM_S_CYCLE_EMPTY,
3352 SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH,
3353 SWM_S_TITLE_CLASS_ENABLED, SWM_S_TITLE_NAME_ENABLED, SWM_S_BAR_FONT,
3354 SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM, SWM_S_SS_APP, SWM_S_DIALOG_RATIO
3358 setconfvalue(char *selector, char *value, int flags)
3361 case SWM_S_BAR_DELAY:
3362 bar_delay = atoi(value);
3364 case SWM_S_BAR_ENABLED:
3365 bar_enabled = atoi(value);
3367 case SWM_S_STACK_ENABLED:
3368 stack_enabled = atoi(value);
3370 case SWM_S_CLOCK_ENABLED:
3371 clock_enabled = atoi(value);
3373 case SWM_S_CLOCK_FORMAT:
3374 #ifndef SWM_DENY_CLOCK_FORMAT
3376 if ((clock_format = strdup(value)) == NULL)
3377 err(1, "setconfvalue: clock_format");
3380 case SWM_S_CYCLE_EMPTY:
3381 cycle_empty = atoi(value);
3383 case SWM_S_CYCLE_VISIBLE:
3384 cycle_visible = atoi(value);
3386 case SWM_S_SS_ENABLED:
3387 ss_enabled = atoi(value);
3389 case SWM_S_TERM_WIDTH:
3390 term_width = atoi(value);
3392 case SWM_S_TITLE_CLASS_ENABLED:
3393 title_class_enabled = atoi(value);
3395 case SWM_S_TITLE_NAME_ENABLED:
3396 title_name_enabled = atoi(value);
3398 case SWM_S_BAR_FONT:
3400 if ((bar_fonts[0] = strdup(value)) == NULL)
3401 err(1, "setconfvalue: bar_font");
3403 case SWM_S_BAR_ACTION:
3405 if ((bar_argv[0] = strdup(value)) == NULL)
3406 err(1, "setconfvalue: bar_action");
3408 case SWM_S_SPAWN_TERM:
3409 free(spawn_term[0]);
3410 if ((spawn_term[0] = strdup(value)) == NULL)
3411 err(1, "setconfvalue: spawn_term");
3415 case SWM_S_DIALOG_RATIO:
3416 dialog_ratio = atof(value);
3417 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
3427 setconfmodkey(char *selector, char *value, int flags)
3429 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
3430 update_modkey(Mod1Mask);
3431 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
3432 update_modkey(Mod2Mask);
3433 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
3434 update_modkey(Mod3Mask);
3435 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
3436 update_modkey(Mod4Mask);
3443 setconfcolor(char *selector, char *value, int flags)
3445 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
3450 setconfregion(char *selector, char *value, int flags)
3452 custom_region(value);
3456 /* config options */
3457 struct config_option {
3459 int (*func)(char*, char*, int);
3462 struct config_option configopt[] = {
3463 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
3464 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
3465 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
3466 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
3467 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
3468 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
3469 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
3470 { "bind", setconfbinding, 0 },
3471 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
3472 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
3473 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
3474 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
3475 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
3476 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
3477 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
3478 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
3479 { "modkey", setconfmodkey, 0 },
3480 { "program", setconfspawn, 0 },
3481 { "quirk", setconfquirk, 0 },
3482 { "region", setconfregion, 0 },
3483 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
3484 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
3485 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
3486 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
3487 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
3488 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED }
3493 conf_load(char *filename)
3496 char *line, *cp, *optsub, *optval;
3497 size_t linelen, lineno = 0;
3498 int wordlen, i, optind;
3499 struct config_option *opt;
3501 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
3503 if (filename == NULL) {
3504 fprintf(stderr, "conf_load: no filename\n");
3507 if ((config = fopen(filename, "r")) == NULL) {
3508 warn("conf_load: fopen");
3512 while (!feof(config)) {
3513 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
3516 err(1, "%s", filename);
3521 cp += strspn(cp, " \t\n"); /* eat whitespace */
3522 if (cp[0] == '\0') {
3527 /* get config option */
3528 wordlen = strcspn(cp, "=[ \t\n");
3530 warnx("%s: line %zd: no option found",
3535 for (i = 0; i < LENGTH(configopt); i++) {
3536 opt = &configopt[i];
3537 if (!strncasecmp(cp, opt->optname, wordlen) &&
3538 strlen(opt->optname) == wordlen) {
3544 warnx("%s: line %zd: unknown option %.*s",
3545 filename, lineno, wordlen, cp);
3549 cp += strspn(cp, " \t\n"); /* eat whitespace */
3550 /* get [selector] if any */
3554 wordlen = strcspn(cp, "]");
3557 warnx("%s: line %zd: syntax error",
3561 asprintf(&optsub, "%.*s", wordlen, cp);
3564 cp += strspn(cp, "] \t\n"); /* eat trailing */
3566 cp += strspn(cp, "= \t\n"); /* eat trailing */
3568 optval = strdup(cp);
3569 /* call function to deal with it all */
3570 if (configopt[optind].func(optsub, optval,
3571 configopt[optind].funcflags) != 0) {
3572 fprintf(stderr, "%s line %zd: %s\n",
3573 filename, lineno, line);
3574 errx(1, "%s: line %zd: invalid data for %s",
3575 filename, lineno, configopt[optind].optname);
3583 DNPRINTF(SWM_D_CONF, "conf_load end\n");
3589 set_child_transient(struct ws_win *win)
3591 struct ws_win *parent;
3593 parent = find_window(win->transient);
3595 parent->child_trans = win;
3599 manage_window(Window id)
3602 struct workspace *ws;
3603 struct ws_win *win, *ww;
3604 int format, i, ws_idx, n, border_me = 0;
3605 unsigned long nitems, bytes;
3606 Atom ws_idx_atom = 0, type;
3607 Atom *prot = NULL, *pp;
3608 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
3609 struct swm_region *r;
3614 if ((win = find_window(id)) != NULL)
3615 return (win); /* already being managed */
3617 /* see if we are on the unmanaged list */
3618 if ((win = find_unmanaged_window(id)) != NULL) {
3619 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
3621 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
3622 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
3624 set_child_transient(win);
3628 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
3629 errx(1, "calloc: failed to allocate memory for new window");
3631 /* Get all the window data in one shot */
3632 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3634 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
3635 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
3636 XGetWindowAttributes(display, id, &win->wa);
3637 XGetWMNormalHints(display, id, &win->sh, &mask);
3638 XGetTransientForHint(display, id, &trans);
3640 win->transient = trans;
3641 set_child_transient(win);
3642 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
3643 win->id, win->transient);
3645 /* get supported protocols */
3646 if (XGetWMProtocols(display, id, &prot, &n)) {
3647 for (i = 0, pp = prot; i < n; i++, pp++) {
3648 if (*pp == takefocus)
3649 win->take_focus = 1;
3651 win->can_delete = 1;
3658 * Figure out where to put the window. If it was previously assigned to
3659 * a workspace (either by spawn() or manually moving), and isn't
3660 * transient, * put it in the same workspace
3662 r = root_to_region(win->wa.root);
3663 if (prop && win->transient == 0) {
3664 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
3665 ws_idx = strtonum(prop, 0, 9, &errstr);
3667 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
3670 ws = &r->s->ws[ws_idx];
3673 /* this should launch transients in the same ws as parent */
3675 if ((ww = find_window(trans)) != NULL)
3682 "fix this bug mcbride\n");
3687 /* set up the window layout */
3690 win->s = r->s; /* this never changes */
3691 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
3693 win->g.w = win->wa.width;
3694 win->g.h = win->wa.height;
3695 win->g.x = win->wa.x;
3696 win->g.y = win->wa.y;
3698 /* Set window properties so we can remember this after reincarnation */
3699 if (ws_idx_atom && prop == NULL &&
3700 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
3701 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3703 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3704 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3708 if (XGetClassHint(display, win->id, &win->ch)) {
3709 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
3710 win->ch.res_class, win->ch.res_name);
3712 /* java is retarded so treat it special */
3713 if (strstr(win->ch.res_name, "sun-awt")) {
3718 for (i = 0; i < quirks_length; i++){
3719 if (!strcmp(win->ch.res_class, quirks[i].class) &&
3720 !strcmp(win->ch.res_name, quirks[i].name)) {
3721 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
3722 win->ch.res_class, win->ch.res_name);
3723 if (quirks[i].quirk & SWM_Q_FLOAT) {
3727 win->quirks = quirks[i].quirk;
3732 /* alter window position if quirky */
3733 if (win->quirks & SWM_Q_ANYWHERE) {
3734 win->manual = 1; /* don't center the quirky windows */
3735 bzero(&wc, sizeof wc);
3737 if (win->g.y < bar_height) {
3738 win->g.y = wc.y = bar_height;
3741 if (win->g.w + win->g.x > WIDTH(r)) {
3742 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
3748 /* Reset font sizes (the bruteforce way; no default keybinding). */
3749 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
3750 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3751 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3752 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3753 fake_keypress(win, XK_KP_Add, ShiftMask);
3758 bzero(&wc, sizeof wc);
3759 wc.border_width = 1;
3760 mask = CWBorderWidth;
3761 XConfigureWindow(display, win->id, mask, &wc);
3765 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
3766 PropertyChangeMask | StructureNotifyMask);
3768 set_win_state(win, NormalState);
3770 /* floaters need to be mapped if they are in the current workspace */
3771 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
3772 XMapRaised(display, win->id);
3778 free_window(struct ws_win *win)
3780 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
3785 /* needed for restart wm */
3786 set_win_state(win, WithdrawnState);
3788 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
3790 if (win->ch.res_class)
3791 XFree(win->ch.res_class);
3792 if (win->ch.res_name)
3793 XFree(win->ch.res_name);
3798 memset(win, 0xff, sizeof *win); /* XXX kill later */
3804 unmanage_window(struct ws_win *win)
3806 struct ws_win *parent;
3811 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
3813 if (win->transient) {
3814 parent = find_window(win->transient);
3816 parent->child_trans = NULL;
3819 /* work around for mplayer going full screen */
3823 TAILQ_REMOVE(&win->ws->winlist, win, entry);
3824 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
3830 focus_magic(struct ws_win *win, int do_trans)
3832 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu %d\n", WINID(win), do_trans);
3837 if (do_trans == SWM_F_TRANSIENT && win->child_trans) {
3838 /* win = parent & has a transient so focus on that */
3840 focus_win(win->child_trans);
3841 if (win->child_trans->take_focus)
3842 client_msg(win, takefocus);
3844 focus_win(win->child_trans);
3845 if (win->child_trans->take_focus)
3846 client_msg(win->child_trans, takefocus);
3851 if (win->take_focus)
3852 client_msg(win, takefocus);
3859 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
3867 XKeyEvent *ev = &e->xkey;
3869 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
3871 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
3872 for (i = 0; i < keys_length; i++)
3873 if (keysym == keys[i].keysym
3874 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
3875 && keyfuncs[keys[i].funcid].func) {
3876 if (keys[i].funcid == kf_spawn_custom)
3878 root_to_region(ev->root),
3879 &(keyfuncs[keys[i].funcid].args),
3883 keyfuncs[keys[i].funcid].func(
3884 root_to_region(ev->root),
3885 &(keyfuncs[keys[i].funcid].args)
3891 buttonpress(XEvent *e)
3895 XButtonPressedEvent *ev = &e->xbutton;
3897 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
3899 action = root_click;
3900 if ((win = find_window(ev->window)) == NULL)
3903 focus_magic(win, SWM_F_TRANSIENT);
3904 action = client_click;
3906 for (i = 0; i < LENGTH(buttons); i++)
3907 if (action == buttons[i].action && buttons[i].func &&
3908 buttons[i].button == ev->button &&
3909 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
3910 buttons[i].func(win, &buttons[i].args);
3914 configurerequest(XEvent *e)
3916 XConfigureRequestEvent *ev = &e->xconfigurerequest;
3921 if ((win = find_window(ev->window)) == NULL)
3922 if ((win = find_unmanaged_window(ev->window)) == NULL)
3926 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
3928 bzero(&wc, sizeof wc);
3931 wc.width = ev->width;
3932 wc.height = ev->height;
3933 wc.border_width = ev->border_width;
3934 wc.sibling = ev->above;
3935 wc.stack_mode = ev->detail;
3936 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
3938 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
3940 if (win->floating) {
3941 if (ev->value_mask & CWX)
3943 if (ev->value_mask & CWY)
3945 if (ev->value_mask & CWWidth)
3946 win->g.w = ev->width;
3947 if (ev->value_mask & CWHeight)
3948 win->g.h = ev->height;
3955 configurenotify(XEvent *e)
3960 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
3961 e->xconfigure.window);
3963 win = find_window(e->xconfigure.window);
3965 XGetWMNormalHints(display, win->id, &win->sh, &mask);
3973 destroynotify(XEvent *e)
3976 XDestroyWindowEvent *ev = &e->xdestroywindow;
3978 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
3980 if ((win = find_window(ev->window)) == NULL) {
3981 if ((win = find_unmanaged_window(ev->window)) == NULL)
3987 /* make sure we focus on something */
3990 unmanage_window(win);
3996 enternotify(XEvent *e)
3998 XCrossingEvent *ev = &e->xcrossing;
4003 Window focus_return;
4004 int revert_to_return;
4006 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
4007 "%lu subwindow %lu same_screen %d focus %d state %d\n",
4008 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
4009 ev->same_screen, ev->focus, ev->state);
4014 * all these checks need to be in this order because the
4015 * XCheckTypedWindowEvent relies on weeding out the previous events
4017 * making this code an option would enable a follow mouse for focus
4022 * state is set when we are switching workspaces and focus is set when
4023 * the window or a subwindow already has focus (occurs during restart).
4025 * Only honor the focus flag if last_focus_event is not FocusOut,
4026 * this allows scrotwm to continue to control focus when another
4027 * program is also playing with it.
4029 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
4030 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
4035 * happens when a window is created or destroyed and the border
4036 * crosses the mouse pointer and when switching ws
4038 * we need the subwindow test to see if we came from root in order
4039 * to give focus to floaters
4041 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
4042 ev->subwindow == 0) {
4043 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
4047 /* this window already has focus */
4048 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
4049 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
4053 /* this window is being deleted or moved to another ws */
4054 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
4056 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
4057 XPutBackEvent(display, &cne);
4061 if ((win = find_window(ev->window)) == NULL) {
4062 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
4067 * In fullstack kill all enters unless they come from a different ws
4068 * (i.e. another region) or focus has been grabbed externally.
4070 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
4071 last_focus_event != FocusOut) {
4072 XGetInputFocus(display, &focus_return, &revert_to_return);
4073 if ((w = find_window(focus_return)) == NULL ||
4075 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
4081 if (QLength(display)) {
4082 DNPRINTF(SWM_D_EVENT, "ignore enternotify %d\n",
4087 if ((win = find_window(ev->window)) == NULL) {
4088 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
4093 * if we have more enternotifies let them handle it in due time
4095 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
4096 DNPRINTF(SWM_D_EVENT,
4097 "ignoring enternotify: got more enternotify\n");
4098 XPutBackEvent(display, &cne);
4102 focus_magic(win, SWM_F_TRANSIENT);
4105 /* lets us use one switch statement for arbitrary mode/detail combinations */
4106 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
4109 focusevent(XEvent *e)
4111 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
4112 ev->type == FocusIn ? "entering" : "leaving",
4113 ev->window, ev->mode, ev->detail);
4116 u_int32_t mode_detail;
4117 XFocusChangeEvent *ev = &e->xfocus;
4119 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
4120 ev->type == FocusIn ? "entering" : "leaving",
4121 ev->window, ev->mode, ev->detail);
4123 if (last_focus_event == ev->type) {
4124 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
4128 last_focus_event = ev->type;
4129 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
4131 switch (mode_detail) {
4132 /* synergy client focus operations */
4133 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
4134 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
4136 /* synergy server focus operations */
4137 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
4139 /* Entering applications like rdesktop that mangle the pointer */
4140 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
4142 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
4143 XSetWindowBorder(display, win->id,
4144 win->ws->r->s->c[ev->type == FocusIn ?
4145 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
4148 fprintf(stderr, "ignoring focusevent\n");
4149 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
4156 mapnotify(XEvent *e)
4159 XMapEvent *ev = &e->xmap;
4161 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
4163 win = manage_window(ev->window);
4165 set_win_state(win, NormalState);
4169 mappingnotify(XEvent *e)
4171 XMappingEvent *ev = &e->xmapping;
4173 XRefreshKeyboardMapping(ev);
4174 if (ev->request == MappingKeyboard)
4179 maprequest(XEvent *e)
4182 struct swm_region *r;
4183 XWindowAttributes wa;
4184 XMapRequestEvent *ev = &e->xmaprequest;
4186 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
4187 e->xmaprequest.window);
4189 if (!XGetWindowAttributes(display, ev->window, &wa))
4191 if (wa.override_redirect)
4194 win = manage_window(e->xmaprequest.window);
4196 return; /* can't happen */
4200 /* make new win focused */
4201 r = root_to_region(win->wa.root);
4202 if (win->ws == r->ws)
4203 focus_magic(win, SWM_F_GENERIC);
4207 propertynotify(XEvent *e)
4210 XPropertyEvent *ev = &e->xproperty;
4212 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
4215 if (ev->state == PropertyDelete)
4216 return; /* ignore */
4217 win = find_window(ev->window);
4222 case XA_WM_NORMAL_HINTS:
4225 XGetWMNormalHints(display, win->id, &win->sh, &mask);
4226 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
4227 if (win->sh.flags & PMinSize) {
4228 win->g.w = win->sh.min_width;
4229 win->g.h = win->sh.min_height;
4230 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
4232 XMoveResizeWindow(display, win->id,
4233 win->g.x, win->g.y, win->g.w, win->g.h);
4242 unmapnotify(XEvent *e)
4246 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
4248 /* determine if we need to help unmanage this window */
4249 win = find_window(e->xunmap.window);
4253 if (getstate(e->xunmap.window) == NormalState) {
4254 unmanage_window(win);
4260 visibilitynotify(XEvent *e)
4263 struct swm_region *r;
4265 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
4266 e->xvisibility.window);
4267 if (e->xvisibility.state == VisibilityUnobscured)
4268 for (i = 0; i < ScreenCount(display); i++)
4269 TAILQ_FOREACH(r, &screens[i].rl, entry)
4270 if (e->xvisibility.window == r->bar_window)
4275 xerror_start(Display *d, XErrorEvent *ee)
4282 xerror(Display *d, XErrorEvent *ee)
4284 /* fprintf(stderr, "error: %p %p\n", display, ee); */
4292 xerrorxlib = XSetErrorHandler(xerror_start);
4294 /* this causes an error if some other window manager is running */
4295 XSelectInput(display, DefaultRootWindow(display),
4296 SubstructureRedirectMask);
4297 XSync(display, False);
4301 XSetErrorHandler(xerror);
4302 XSync(display, False);
4307 new_region(struct swm_screen *s, int x, int y, int w, int h)
4309 struct swm_region *r, *n;
4310 struct workspace *ws = NULL;
4313 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
4314 s->idx, w, h, x, y);
4316 /* remove any conflicting regions */
4317 n = TAILQ_FIRST(&s->rl);
4320 n = TAILQ_NEXT(r, entry);
4321 if (X(r) < (x + w) &&
4322 (X(r) + WIDTH(r)) > x &&
4324 (Y(r) + HEIGHT(r)) > y) {
4325 XDestroyWindow(display, r->bar_window);
4326 TAILQ_REMOVE(&s->rl, r, entry);
4327 TAILQ_INSERT_TAIL(&s->orl, r, entry);
4331 /* search old regions for one to reuse */
4333 /* size + location match */
4334 TAILQ_FOREACH(r, &s->orl, entry)
4335 if (X(r) == x && Y(r) == y &&
4336 HEIGHT(r) == h && WIDTH(r) == w)
4340 TAILQ_FOREACH(r, &s->orl, entry)
4341 if (HEIGHT(r) == h && WIDTH(r) == w)
4345 TAILQ_REMOVE(&s->orl, r, entry);
4346 /* try to use old region's workspace */
4347 if (r->ws->r == NULL)
4350 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
4351 errx(1, "calloc: failed to allocate memory for screen");
4353 /* if we don't have a workspace already, find one */
4355 for (i = 0; i < SWM_WS_MAX; i++)
4356 if (s->ws[i].r == NULL) {
4363 errx(1, "no free workspaces\n");
4372 TAILQ_INSERT_TAIL(&s->rl, r, entry);
4378 #ifdef SWM_XRR_HAS_CRTC
4380 XRRScreenResources *sr;
4383 #endif /* SWM_XRR_HAS_CRTC */
4384 struct swm_region *r;
4387 if (i >= ScreenCount(display))
4388 errx(1, "invalid screen");
4390 /* remove any old regions */
4391 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
4392 r->ws->old_r = r->ws->r = NULL;
4393 XDestroyWindow(display, r->bar_window);
4394 TAILQ_REMOVE(&screens[i].rl, r, entry);
4395 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
4398 /* map virtual screens onto physical screens */
4399 #ifdef SWM_XRR_HAS_CRTC
4401 if (xrandr_support) {
4402 sr = XRRGetScreenResources(display, screens[i].root);
4404 new_region(&screens[i], 0, 0,
4405 DisplayWidth(display, i),
4406 DisplayHeight(display, i));
4410 for (c = 0, ci = NULL; c < ncrtc; c++) {
4411 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
4412 if (ci->noutput == 0)
4416 if (ci != NULL && ci->mode == None)
4417 new_region(&screens[i], 0, 0,
4418 DisplayWidth(display, i),
4419 DisplayHeight(display, i));
4421 new_region(&screens[i],
4422 ci->x, ci->y, ci->width, ci->height);
4425 XRRFreeCrtcInfo(ci);
4426 XRRFreeScreenResources(sr);
4428 #endif /* SWM_XRR_HAS_CRTC */
4430 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
4431 DisplayHeight(display, i));
4436 screenchange(XEvent *e) {
4437 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
4438 struct swm_region *r;
4441 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
4443 if (!XRRUpdateConfiguration(e))
4446 /* silly event doesn't include the screen index */
4447 for (i = 0; i < ScreenCount(display); i++)
4448 if (screens[i].root == xe->root)
4450 if (i >= ScreenCount(display))
4451 errx(1, "screenchange: screen not found\n");
4453 /* brute force for now, just re-enumerate the regions */
4456 /* add bars to all regions */
4457 for (i = 0; i < ScreenCount(display); i++)
4458 TAILQ_FOREACH(r, &screens[i].rl, entry)
4466 Window d1, d2, *wins = NULL;
4467 XWindowAttributes wa;
4470 int errorbase, major, minor;
4471 struct workspace *ws;
4475 if ((screens = calloc(ScreenCount(display),
4476 sizeof(struct swm_screen))) == NULL)
4477 errx(1, "calloc: screens");
4479 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4481 /* initial Xrandr setup */
4482 xrandr_support = XRRQueryExtension(display,
4483 &xrandr_eventbase, &errorbase);
4485 if (XRRQueryVersion(display, &major, &minor) && major < 1)
4488 /* map physical screens */
4489 for (i = 0; i < ScreenCount(display); i++) {
4490 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
4492 TAILQ_INIT(&screens[i].rl);
4493 TAILQ_INIT(&screens[i].orl);
4494 screens[i].root = RootWindow(display, i);
4496 /* set default colors */
4497 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
4498 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
4499 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
4500 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
4501 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
4503 /* init all workspaces */
4504 /* XXX these should be dynamically allocated too */
4505 for (j = 0; j < SWM_WS_MAX; j++) {
4506 ws = &screens[i].ws[j];
4511 TAILQ_INIT(&ws->winlist);
4512 TAILQ_INIT(&ws->unmanagedlist);
4514 for (k = 0; layouts[k].l_stack != NULL; k++)
4515 if (layouts[k].l_config != NULL)
4516 layouts[k].l_config(ws,
4517 SWM_ARG_ID_STACKINIT);
4518 ws->cur_layout = &layouts[0];
4520 /* grab existing windows (before we build the bars)*/
4521 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
4527 XRRSelectInput(display, screens[i].root,
4528 RRScreenChangeNotifyMask);
4530 /* attach windows to a region */
4531 /* normal windows */
4532 for (j = 0; j < no; j++) {
4533 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4534 wa.override_redirect ||
4535 XGetTransientForHint(display, wins[j], &d1))
4538 state = getstate(wins[j]);
4539 manage = state == IconicState;
4540 if (wa.map_state == IsViewable || manage)
4541 manage_window(wins[j]);
4543 /* transient windows */
4544 for (j = 0; j < no; j++) {
4545 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4546 wa.override_redirect)
4549 state = getstate(wins[j]);
4550 manage = state == IconicState;
4551 if (XGetTransientForHint(display, wins[j], &d1) &&
4553 manage_window(wins[j]);
4565 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
4567 err(1, "setup_globals: strdup");
4568 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
4570 err(1, "setup_globals: strdup");
4571 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
4573 err(1, "setup_globals: strdup");
4574 if ((spawn_term[0] = strdup("xterm")) == NULL)
4575 err(1, "setup_globals: strdup");
4576 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
4584 Atom netwmcheck, netwmname, utf8_string;
4587 /* work around sun jdk bugs, code from wmname */
4588 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
4589 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
4590 utf8_string = XInternAtom(display, "UTF8_STRING", False);
4591 for (i = 0; i < ScreenCount(display); i++) {
4592 root = screens[i].root;
4593 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
4594 PropModeReplace, (unsigned char *)&root, 1);
4595 XChangeProperty(display, root, netwmname, utf8_string, 8,
4596 PropModeReplace, "LG3D", strlen("LG3D"));
4601 main(int argc, char *argv[])
4604 struct swm_region *r, *rr;
4605 struct ws_win *winfocus = NULL;
4608 char conf[PATH_MAX], *cfile = NULL;
4615 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
4616 SWM_VERSION, cvstag);
4617 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
4618 warnx("no locale support");
4620 if (!(display = XOpenDisplay(0)))
4621 errx(1, "can not open display");
4624 errx(1, "other wm running");
4626 /* handle some signale */
4627 installsignal(SIGINT, "INT");
4628 installsignal(SIGHUP, "HUP");
4629 installsignal(SIGQUIT, "QUIT");
4630 installsignal(SIGTERM, "TERM");
4631 installsignal(SIGCHLD, "CHLD");
4633 astate = XInternAtom(display, "WM_STATE", False);
4634 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
4635 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
4636 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
4638 /* look for local and global conf file */
4639 pwd = getpwuid(getuid());
4641 errx(1, "invalid user %d", getuid());
4649 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
4650 if (stat(conf, &sb) != -1) {
4651 if (S_ISREG(sb.st_mode))
4654 /* try global conf file */
4655 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
4656 if (!stat(conf, &sb))
4657 if (S_ISREG(sb.st_mode))
4663 /* setup all bars */
4664 for (i = 0; i < ScreenCount(display); i++)
4665 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4666 if (winfocus == NULL)
4667 winfocus = TAILQ_FIRST(&r->ws->winlist);
4671 /* set some values to work around bad programs */
4679 xfd = ConnectionNumber(display);
4681 while (XPending(display)) {
4682 XNextEvent(display, &e);
4685 if (e.type < LASTEvent) {
4687 if (handler[e.type])
4688 handler[e.type](&e);
4690 DNPRINTF(SWM_D_EVENT,
4691 "win: %lu unknown event: %d\n",
4692 e.xany.window, e.type);
4694 switch (e.type - xrandr_eventbase) {
4695 case RRScreenChangeNotify:
4699 DNPRINTF(SWM_D_EVENT,
4700 "win: %lu unknown xrandr event: "
4701 "%d\n", e.xany.window, e.type);
4707 /* if we are being restarted go focus on first window */
4709 rr = winfocus->ws->r;
4711 /* not a visible window */
4715 /* move pointer to first screen if multi screen */
4716 if (ScreenCount(display) > 1 || outputs > 1)
4717 XWarpPointer(display, None, rr->s[0].root,
4718 0, 0, 0, 0, rr->g.x,
4719 rr->g.y + bar_enabled ? bar_height : 0);
4721 a.id = SWM_ARG_ID_FOCUSCUR;
4731 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
4733 DNPRINTF(SWM_D_MISC, "select failed");
4744 XCloseDisplay(display);