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.19"
71 #include <sys/types.h>
75 #include <sys/queue.h>
76 #include <sys/param.h>
77 #include <sys/select.h>
79 #include <X11/cursorfont.h>
80 #include <X11/keysym.h>
81 #include <X11/Xatom.h>
83 #include <X11/Xproto.h>
84 #include <X11/Xutil.h>
85 #include <X11/extensions/Xrandr.h>
88 # error XRandR versions less than 1.0 are not supported
93 #define SWM_XRR_HAS_CRTC
97 /* #define SWM_DEBUG */
99 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
100 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
101 #define SWM_D_MISC 0x0001
102 #define SWM_D_EVENT 0x0002
103 #define SWM_D_WS 0x0004
104 #define SWM_D_FOCUS 0x0008
105 #define SWM_D_MOVE 0x0010
106 #define SWM_D_STACK 0x0020
107 #define SWM_D_MOUSE 0x0040
108 #define SWM_D_PROP 0x0080
109 #define SWM_D_CLASS 0x0100
110 #define SWM_D_KEY 0x0200
111 #define SWM_D_QUIRK 0x0400
112 #define SWM_D_SPAWN 0x0800
113 #define SWM_D_EVENTQ 0x1000
114 #define SWM_D_CONF 0x2000
116 u_int32_t swm_debug = 0
133 #define DPRINTF(x...)
134 #define DNPRINTF(n,x...)
137 #define LENGTH(x) (sizeof x / sizeof x[0])
138 #define MODKEY Mod1Mask
139 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
140 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
141 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
142 #define SWM_PROPLEN (16)
143 #define SWM_FUNCNAME_LEN (32)
144 #define SWM_KEYS_LEN (255)
145 #define SWM_QUIRK_LEN (64)
146 #define X(r) (r)->g.x
147 #define Y(r) (r)->g.y
148 #define WIDTH(r) (r)->g.w
149 #define HEIGHT(r) (r)->g.h
150 #define SWM_MAX_FONT_STEPS (3)
151 #define WINID(w) (w ? w->id : 0)
154 #define SWM_LIB "/usr/local/lib/libswmhack.so"
162 volatile sig_atomic_t running = 1;
164 int (*xerrorxlib)(Display *, XErrorEvent *);
168 int xrandr_eventbase;
169 unsigned int numlockmask = 0;
173 int cycle_visible = 0;
175 int font_adjusted = 0;
176 unsigned int mod_key = MODKEY;
179 double dialog_ratio = .6;
181 #define SWM_BAR_MAX (256)
182 char *bar_argv[] = { NULL, NULL };
184 char bar_ext[SWM_BAR_MAX];
185 char bar_vertext[SWM_BAR_MAX];
187 sig_atomic_t bar_alarm = 0;
191 int bar_extra_running = 0;
194 int stack_enabled = 1;
195 int clock_enabled = 1;
196 int title_name_enabled = 0;
197 int title_class_enabled = 0;
203 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
204 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
206 #define SWM_MENU_FN (2)
207 #define SWM_MENU_NB (4)
208 #define SWM_MENU_NF (6)
209 #define SWM_MENU_SB (8)
210 #define SWM_MENU_SF (10)
212 /* layout manager data */
213 struct swm_geometry {
223 /* virtual "screens" */
225 TAILQ_ENTRY(swm_region) entry;
226 struct swm_geometry g;
227 struct workspace *ws; /* current workspace on this region */
228 struct swm_screen *s; /* screen idx */
231 TAILQ_HEAD(swm_region_list, swm_region);
234 TAILQ_ENTRY(ws_win) entry;
237 struct ws_win *child_trans; /* transient child window */
238 struct swm_geometry g;
241 int font_size_boundary[SWM_MAX_FONT_STEPS];
247 unsigned long quirks;
248 struct workspace *ws; /* always valid */
249 struct swm_screen *s; /* always valid, never changes */
250 XWindowAttributes wa;
254 TAILQ_HEAD(ws_win_list, ws_win);
256 /* user/key callable function IDs */
314 /* layout handlers */
316 void vertical_config(struct workspace *, int);
317 void vertical_stack(struct workspace *, struct swm_geometry *);
318 void horizontal_config(struct workspace *, int);
319 void horizontal_stack(struct workspace *, struct swm_geometry *);
320 void max_stack(struct workspace *, struct swm_geometry *);
322 struct ws_win *find_window(Window);
324 void grabbuttons(struct ws_win *, int);
325 void new_region(struct swm_screen *, int, int, int, int);
326 void unmanage_window(struct ws_win *);
327 long getstate(Window);
330 void (*l_stack)(struct workspace *, struct swm_geometry *);
331 void (*l_config)(struct workspace *, int);
333 #define SWM_L_FOCUSPREV (1<<0)
334 #define SWM_L_MAPONFOCUS (1<<1)
337 /* stack, configure */
338 { vertical_stack, vertical_config, 0, "[|]" },
339 { horizontal_stack, horizontal_config, 0, "[-]" },
341 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, "[ ]"},
342 { NULL, NULL, 0, NULL },
345 #define SWM_H_SLICE (32)
346 #define SWM_V_SLICE (32)
348 /* define work spaces */
350 int idx; /* workspace index */
351 struct layout *cur_layout; /* current layout handlers */
352 struct ws_win *focus; /* may be NULL */
353 struct ws_win *focus_prev; /* may be NULL */
354 struct swm_region *r; /* may be NULL */
355 struct swm_region *old_r; /* may be NULL */
356 struct ws_win_list winlist; /* list of windows in ws */
357 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
361 int horizontal_msize;
363 int horizontal_stacks;
370 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
371 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
373 /* physical screen mapping */
374 #define SWM_WS_MAX (10)
376 int idx; /* screen index */
377 struct swm_region_list rl; /* list of regions on this screen */
378 struct swm_region_list orl; /* list of old regions */
380 struct workspace ws[SWM_WS_MAX];
386 } c[SWM_S_COLOR_MAX];
388 struct swm_screen *screens;
391 /* args to functions */
394 #define SWM_ARG_ID_FOCUSNEXT (0)
395 #define SWM_ARG_ID_FOCUSPREV (1)
396 #define SWM_ARG_ID_FOCUSMAIN (2)
397 #define SWM_ARG_ID_FOCUSCUR (4)
398 #define SWM_ARG_ID_SWAPNEXT (10)
399 #define SWM_ARG_ID_SWAPPREV (11)
400 #define SWM_ARG_ID_SWAPMAIN (12)
401 #define SWM_ARG_ID_MASTERSHRINK (20)
402 #define SWM_ARG_ID_MASTERGROW (21)
403 #define SWM_ARG_ID_MASTERADD (22)
404 #define SWM_ARG_ID_MASTERDEL (23)
405 #define SWM_ARG_ID_STACKRESET (30)
406 #define SWM_ARG_ID_STACKINIT (31)
407 #define SWM_ARG_ID_CYCLEWS_UP (40)
408 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
409 #define SWM_ARG_ID_CYCLESC_UP (42)
410 #define SWM_ARG_ID_CYCLESC_DOWN (43)
411 #define SWM_ARG_ID_STACKINC (50)
412 #define SWM_ARG_ID_STACKDEC (51)
413 #define SWM_ARG_ID_SS_ALL (60)
414 #define SWM_ARG_ID_SS_WINDOW (61)
415 #define SWM_ARG_ID_DONTCENTER (70)
416 #define SWM_ARG_ID_CENTER (71)
417 #define SWM_ARG_ID_KILLWINDOW (80)
418 #define SWM_ARG_ID_DELETEWINDOW (81)
422 void focus(struct swm_region *, union arg *);
423 void focus_magic(struct ws_win *, int);
424 #define SWM_F_GENERIC (0)
425 #define SWM_F_TRANSIENT (1)
431 #define SWM_Q_FLOAT (1<<0) /* float this window */
432 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
433 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
434 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
435 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
437 int quirks_size = 0, quirks_length = 0;
438 struct quirk *quirks = NULL;
455 name = "ButtonPress";
458 name = "ButtonRelease";
461 name = "MotionNotify";
464 name = "EnterNotify";
467 name = "LeaveNotify";
476 name = "KeymapNotify";
482 name = "GraphicsExpose";
487 case VisibilityNotify:
488 name = "VisibilityNotify";
491 name = "CreateNotify";
494 name = "DestroyNotify";
497 name = "UnmapNotify";
506 name = "ReparentNotify";
508 case ConfigureNotify:
509 name = "ConfigureNotify";
511 case ConfigureRequest:
512 name = "ConfigureRequest";
515 name = "GravityNotify";
518 name = "ResizeRequest";
520 case CirculateNotify:
521 name = "CirculateNotify";
523 case CirculateRequest:
524 name = "CirculateRequest";
527 name = "PropertyNotify";
530 name = "SelectionClear";
532 case SelectionRequest:
533 name = "SelectionRequest";
535 case SelectionNotify:
536 name = "SelectionNotify";
539 name = "ColormapNotify";
542 name = "ClientMessage";
545 name = "MappingNotify";
550 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
552 e->xany.window, name, e->type, QLength(display));
554 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
556 e->xany.window, e->type, QLength(display));
560 dumpwins(struct swm_region *r, union arg *args)
564 XWindowAttributes wa;
567 fprintf(stderr, "invalid workspace\n");
571 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
573 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
574 state = getstate(win->id);
575 if (!XGetWindowAttributes(display, win->id, &wa))
576 fprintf(stderr, "window: %lu failed "
577 "XGetWindowAttributes\n", win->id);
578 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
579 win->id, wa.map_state, state);
582 fprintf(stderr, "===== unmanaged window list =====\n");
583 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
584 state = getstate(win->id);
585 if (!XGetWindowAttributes(display, win->id, &wa))
586 fprintf(stderr, "window: %lu failed "
587 "XGetWindowAttributes\n", win->id);
588 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
589 win->id, wa.map_state, state);
592 fprintf(stderr, "=================================\n");
597 dumpwins(struct swm_region *r, union arg *args)
600 #endif /* SWM_DEBUG */
602 void expose(XEvent *);
603 void keypress(XEvent *);
604 void buttonpress(XEvent *);
605 void configurerequest(XEvent *);
606 void configurenotify(XEvent *);
607 void destroynotify(XEvent *);
608 void enternotify(XEvent *);
609 void focusin(XEvent *);
610 void focusout(XEvent *);
611 void mapnotify(XEvent *);
612 void mappingnotify(XEvent *);
613 void maprequest(XEvent *);
614 void propertynotify(XEvent *);
615 void unmapnotify(XEvent *);
616 void visibilitynotify(XEvent *);
618 void (*handler[LASTEvent])(XEvent *) = {
620 [KeyPress] = keypress,
621 [ButtonPress] = buttonpress,
622 [ConfigureRequest] = configurerequest,
623 [ConfigureNotify] = configurenotify,
624 [DestroyNotify] = destroynotify,
625 [EnterNotify] = enternotify,
627 [FocusOut] = focusout,
628 [MapNotify] = mapnotify,
629 [MappingNotify] = mappingnotify,
630 [MapRequest] = maprequest,
631 [PropertyNotify] = propertynotify,
632 [UnmapNotify] = unmapnotify,
633 [VisibilityNotify] = visibilitynotify,
643 while ((pid = waitpid(WAIT_ANY, NULL, WNOHANG)) != -1) {
644 DNPRINTF(SWM_D_MISC, "reaping: %d\n", pid);
659 installsignal(int sig, char *name)
663 sa.sa_handler = sighdlr;
664 sigemptyset(&sa.sa_mask);
666 if (sigaction(sig, &sa, NULL) == -1)
667 err(1, "could not install %s handler", name);
671 name_to_color(char *colorname)
675 XColor screen_def, exact_def;
676 unsigned long result = 0;
677 char cname[32] = "#";
679 cmap = DefaultColormap(display, screens[0].idx);
680 status = XAllocNamedColor(display, cmap, colorname,
681 &screen_def, &exact_def);
683 strlcat(cname, colorname + 2, sizeof cname - 1);
684 status = XAllocNamedColor(display, cmap, cname, &screen_def,
688 result = screen_def.pixel;
690 fprintf(stderr, "color '%s' not found.\n", colorname);
696 setscreencolor(char *val, int i, int c)
698 if (i > 0 && i <= ScreenCount(display)) {
699 screens[i - 1].c[c].color = name_to_color(val);
700 free(screens[i - 1].c[c].name);
701 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
703 } else if (i == -1) {
704 for (i = 0; i < ScreenCount(display); i++) {
705 screens[i].c[c].color = name_to_color(val);
706 free(screens[i].c[c].name);
707 if ((screens[i].c[c].name = strdup(val)) == NULL)
711 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
712 i, ScreenCount(display));
716 custom_region(char *val)
718 unsigned int sidx, x, y, w, h;
720 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
721 errx(1, "invalid custom region, "
722 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
723 if (sidx < 1 || sidx > ScreenCount(display))
724 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
725 sidx, ScreenCount(display));
729 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
731 if (x < 0 || x > DisplayWidth(display, sidx) ||
732 y < 0 || y > DisplayHeight(display, sidx) ||
733 w + x > DisplayWidth(display, sidx) ||
734 h + y > DisplayHeight(display, sidx))
735 errx(1, "region %ux%u+%u+%u not within screen boundaries "
736 "(%ux%u)\n", w, h, x, y,
737 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
739 new_region(&screens[sidx], x, y, w, h);
743 socket_setnonblock(int fd)
747 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
748 err(1, "fcntl F_GETFL");
750 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
751 err(1, "fcntl F_SETFL");
755 bar_print(struct swm_region *r, char *s)
757 XClearWindow(display, r->bar_window);
758 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
759 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
768 bzero(bar_pipe, sizeof bar_pipe);
771 kill(bar_pid, SIGTERM);
774 strlcpy(bar_ext, "", sizeof bar_ext);
779 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
781 int do_class, do_name;
783 XClassHint *xch = NULL;
785 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
787 if ((xch = XAllocClassHint()) == NULL)
789 status = XGetClassHint(display, cur_focus->id, xch);
790 if (status == BadWindow || status == BadAlloc)
792 do_class = (title_class_enabled && xch->res_class != NULL);
793 do_name = (title_name_enabled && xch->res_name != NULL);
795 strlcat(s, xch->res_class, sz);
796 if (do_class && do_name)
799 strlcat(s, xch->res_name, sz);
812 struct swm_region *r;
816 char loc[SWM_BAR_MAX];
820 if (bar_enabled == 0)
822 if (bar_extra && bar_extra_running) {
823 /* ignore short reads; it'll correct itself */
824 while ((b = fgetln(stdin, &len)) != NULL)
825 if (b && b[len - 1] == '\n') {
827 strlcpy(bar_ext, b, sizeof bar_ext);
829 if (b == NULL && errno != EAGAIN) {
830 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
831 errno, strerror(errno));
835 strlcpy(bar_ext, "", sizeof bar_ext);
837 if (clock_enabled == 0)
838 strlcpy(s, "", sizeof s);
841 localtime_r(&tmt, &tm);
842 strftime(s, sizeof s, "%a %b %d %R %Z %Y ", &tm);
845 for (i = 0; i < ScreenCount(display); i++) {
847 TAILQ_FOREACH(r, &screens[i].rl, entry) {
849 bar_class_name(s, sizeof s, r->ws->focus);
852 stack = r->ws->cur_layout->name;
854 snprintf(loc, sizeof loc, "%d:%d %s %s %s %s",
855 x++, r->ws->idx + 1, stack, s, bar_ext,
870 bar_toggle(struct swm_region *r, union arg *args)
872 struct swm_region *tmpr;
873 int i, sc = ScreenCount(display);
875 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
878 for (i = 0; i < sc; i++)
879 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
880 XUnmapWindow(display, tmpr->bar_window);
882 for (i = 0; i < sc; i++)
883 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
884 XMapRaised(display, tmpr->bar_window);
886 bar_enabled = !bar_enabled;
889 /* must be after stack */
896 XSetWindowAttributes wa;
897 struct swm_region *r;
900 /* do this here because the conf file is in memory */
901 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
902 /* launch external status app */
903 bar_extra_running = 1;
904 if (pipe(bar_pipe) == -1)
905 err(1, "pipe error");
906 socket_setnonblock(bar_pipe[0]);
907 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
908 if (dup2(bar_pipe[0], 0) == -1)
910 if (dup2(bar_pipe[1], 1) == -1)
912 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
913 err(1, "could not disable SIGPIPE");
914 switch (bar_pid = fork()) {
916 err(1, "cannot fork");
920 execvp(bar_argv[0], bar_argv);
921 err(1, "%s external app failed", bar_argv[0]);
923 default: /* parent */
929 bzero(&wa, sizeof wa);
930 for (i = 0; i < ScreenCount(display); i++)
931 TAILQ_FOREACH(r, &screens[i].rl, entry) {
933 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
934 wa.background_pixel =
935 screens[i].c[SWM_S_COLOR_BAR].color;
936 XChangeWindowAttributes(display, r->bar_window,
937 CWBackPixel | CWBorderPixel, &wa);
943 bar_setup(struct swm_region *r)
947 for (i = 0; bar_fonts[i] != NULL; i++) {
948 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
954 if (bar_fonts[i] == NULL)
955 errx(1, "couldn't load font");
956 bar_height = bar_fs->ascent + bar_fs->descent + 3;
958 r->bar_window = XCreateSimpleWindow(display,
959 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
960 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
961 r->s->c[SWM_S_COLOR_BAR].color);
962 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
963 XSetFont(display, bar_gc, bar_fs->fid);
964 XSelectInput(display, r->bar_window, VisibilityChangeMask);
966 XMapRaised(display, r->bar_window);
967 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
969 if (signal(SIGALRM, bar_signal) == SIG_ERR)
970 err(1, "could not install bar_signal");
975 set_win_state(struct ws_win *win, long state)
977 long data[] = {state, None};
979 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
984 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
985 (unsigned char *)data, 2);
993 unsigned char *p = NULL;
994 unsigned long n, extra;
997 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
998 &real, &format, &n, &extra, (unsigned char **)&p);
999 if (status != Success)
1002 result = *((long *)p);
1008 version(struct swm_region *r, union arg *args)
1010 bar_version = !bar_version;
1012 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
1013 SWM_VERSION, cvstag);
1015 strlcpy(bar_vertext, "", sizeof bar_vertext);
1020 client_msg(struct ws_win *win, Atom a)
1022 XClientMessageEvent cm;
1027 bzero(&cm, sizeof cm);
1028 cm.type = ClientMessage;
1029 cm.window = win->id;
1030 cm.message_type = aprot;
1033 cm.data.l[1] = CurrentTime;
1034 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1038 configreq_win(struct ws_win *win)
1040 XConfigureRequestEvent cr;
1045 bzero(&cr, sizeof cr);
1046 cr.type = ConfigureRequest;
1047 cr.display = display;
1048 cr.parent = win->id;
1049 cr.window = win->id;
1052 cr.width = win->g.w;
1053 cr.height = win->g.h;
1054 cr.border_width = 1;
1056 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&cr);
1060 config_win(struct ws_win *win)
1064 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
1065 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1070 ce.type = ConfigureNotify;
1071 ce.display = display;
1073 ce.window = win->id;
1076 ce.width = win->g.w;
1077 ce.height = win->g.h;
1078 ce.border_width = 1; /* XXX store this! */
1080 ce.override_redirect = False;
1081 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1085 count_win(struct workspace *ws, int count_transient)
1090 TAILQ_FOREACH(win, &ws->winlist, entry) {
1091 if (count_transient == 0 && win->floating)
1093 if (count_transient == 0 && win->transient)
1097 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1103 quit(struct swm_region *r, union arg *args)
1105 DNPRINTF(SWM_D_MISC, "quit\n");
1110 unmap_window(struct ws_win *win)
1115 /* don't unmap again */
1116 if (getstate(win->id) == IconicState)
1119 set_win_state(win, IconicState);
1121 XUnmapWindow(display, win->id);
1130 for (i = 0; i < ScreenCount(display); i++)
1131 for (j = 0; j < SWM_WS_MAX; j++)
1132 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1137 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1144 event.display = display; /* Ignored, but what the hell */
1145 event.window = win->id;
1146 event.root = win->s->root;
1147 event.subwindow = None;
1148 event.time = CurrentTime;
1153 event.same_screen = True;
1154 event.keycode = XKeysymToKeycode(display, keysym);
1155 event.state = modifiers;
1157 event.type = KeyPress;
1158 XSendEvent(event.display, event.window, True,
1159 KeyPressMask, (XEvent *)&event);
1161 event.type = KeyRelease;
1162 XSendEvent(event.display, event.window, True,
1163 KeyPressMask, (XEvent *)&event);
1168 restart(struct swm_region *r, union arg *args)
1170 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1172 /* disable alarm because the following code may not be interrupted */
1174 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1175 errx(1, "can't disable alarm");
1180 XCloseDisplay(display);
1181 execvp(start_argv[0], start_argv);
1182 fprintf(stderr, "execvp failed\n");
1188 root_to_region(Window root)
1190 struct swm_region *r = NULL;
1192 int i, x, y, wx, wy;
1195 for (i = 0; i < ScreenCount(display); i++)
1196 if (screens[i].root == root)
1199 if (XQueryPointer(display, screens[i].root,
1200 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1201 /* choose a region based on pointer location */
1202 TAILQ_FOREACH(r, &screens[i].rl, entry)
1203 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1204 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1209 r = TAILQ_FIRST(&screens[i].rl);
1215 find_unmanaged_window(Window id)
1220 for (i = 0; i < ScreenCount(display); i++)
1221 for (j = 0; j < SWM_WS_MAX; j++)
1222 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1230 find_window(Window id)
1235 for (i = 0; i < ScreenCount(display); i++)
1236 for (j = 0; j < SWM_WS_MAX; j++)
1237 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1244 spawn(struct swm_region *r, union arg *args)
1249 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1251 * The double-fork construct avoids zombie processes and keeps the code
1252 * clean from stupid signal handlers.
1257 close(ConnectionNumber(display));
1258 setenv("LD_PRELOAD", SWM_LIB, 1);
1259 if (asprintf(&ret, "%d", r->ws->idx)) {
1260 setenv("_SWM_WS", ret, 1);
1263 if (asprintf(&ret, "%d", getpid())) {
1264 setenv("_SWM_PID", ret, 1);
1268 /* kill stdin, mplayer, ssh-add etc. need that */
1269 si = open("/dev/null", O_RDONLY, 0);
1271 err(1, "open /dev/null");
1272 if (dup2(si, 0) == -1)
1273 err(1, "dup2 /dev/null");
1274 execvp(args->argv[0], args->argv);
1275 fprintf(stderr, "execvp failed\n");
1283 spawnterm(struct swm_region *r, union arg *args)
1285 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1288 setenv("_SWM_XTERM_FONTADJ", "", 1);
1293 kill_refs(struct ws_win *win)
1296 struct swm_region *r;
1297 struct workspace *ws;
1302 for (i = 0; i < ScreenCount(display); i++)
1303 TAILQ_FOREACH(r, &screens[i].rl, entry)
1304 for (x = 0; x < SWM_WS_MAX; x++) {
1306 if (win == ws->focus)
1308 if (win == ws->focus_prev)
1309 ws->focus_prev = NULL;
1314 validate_win(struct ws_win *testwin)
1317 struct workspace *ws;
1318 struct swm_region *r;
1319 int i, x, foundit = 0;
1321 if (testwin == NULL)
1324 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1325 TAILQ_FOREACH(r, &screens[i].rl, entry)
1326 for (x = 0; x < SWM_WS_MAX; x++) {
1328 TAILQ_FOREACH(win, &ws->winlist, entry)
1336 validate_ws(struct workspace *testws)
1338 struct swm_region *r;
1339 struct workspace *ws;
1342 /* validate all ws */
1343 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1344 TAILQ_FOREACH(r, &screens[i].rl, entry)
1345 for (x = 0; x < SWM_WS_MAX; x++) {
1354 unfocus_win(struct ws_win *win)
1356 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1360 if (win->ws == NULL)
1363 if (validate_ws(win->ws))
1366 if (win->ws->r == NULL)
1369 if (validate_win(win)) {
1374 if (win->ws->focus == win) {
1375 win->ws->focus = NULL;
1376 win->ws->focus_prev = win;
1379 if (validate_win(win->ws->focus)) {
1380 kill_refs(win->ws->focus);
1381 win->ws->focus = NULL;
1383 if (validate_win(win->ws->focus_prev)) {
1384 kill_refs(win->ws->focus_prev);
1385 win->ws->focus_prev = NULL;
1388 grabbuttons(win, 0);
1389 XSetWindowBorder(display, win->id,
1390 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1394 unfocus_all_except(struct ws_win *except)
1399 DNPRINTF(SWM_D_FOCUS, "unfocus_all_except: id: %lu\n", except->id);
1401 for (i = 0; i < ScreenCount(display); i++)
1402 for (j = 0; j < SWM_WS_MAX; j++)
1403 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1409 focus_win(struct ws_win *win)
1414 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1418 if (win->ws == NULL)
1421 if (validate_ws(win->ws))
1423 if (validate_win(win)) {
1428 /* use big hammer to make sure it works under all use cases */
1429 unfocus_all_except(win);
1431 if (validate_win(win)) {
1436 win->ws->focus = win;
1438 if (win->ws->r != NULL) {
1439 XGetInputFocus(display, &wf, &rtr);
1440 if (wf != win->id) {
1441 grabbuttons(win, 1);
1443 XSetInputFocus(display, win->id,
1444 RevertToParent, CurrentTime);
1446 XSetWindowBorder(display, win->id,
1447 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1448 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS)
1449 XMapRaised(display, win->id);
1454 switchws(struct swm_region *r, union arg *args)
1456 int wsid = args->id;
1457 struct swm_region *this_r, *other_r;
1459 struct workspace *new_ws, *old_ws;
1466 old_ws = this_r->ws;
1467 new_ws = &this_r->s->ws[wsid];
1469 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1470 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1473 if (new_ws == NULL || old_ws == NULL)
1475 if (new_ws == old_ws)
1478 other_r = new_ws->r;
1479 if (other_r == NULL) {
1480 /* if the other workspace is hidden, switch windows */
1481 if (old_ws->r != NULL)
1482 old_ws->old_r = old_ws->r;
1485 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1488 other_r->ws = old_ws;
1489 old_ws->r = other_r;
1491 this_r->ws = new_ws;
1495 a.id = SWM_ARG_ID_FOCUSCUR;
1496 focus(new_ws->r, &a);
1501 cyclews(struct swm_region *r, union arg *args)
1504 struct swm_screen *s = r->s;
1506 DNPRINTF(SWM_D_WS, "cyclews id %d "
1507 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1508 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1513 case SWM_ARG_ID_CYCLEWS_UP:
1514 if (a.id < SWM_WS_MAX - 1)
1519 case SWM_ARG_ID_CYCLEWS_DOWN:
1523 a.id = SWM_WS_MAX - 1;
1529 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1531 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1535 } while (a.id != r->ws->idx);
1539 cyclescr(struct swm_region *r, union arg *args)
1541 struct swm_region *rr = NULL;
1545 /* do nothing if we don't have more than one screen */
1546 if (!(ScreenCount(display) > 1 || outputs > 1))
1551 case SWM_ARG_ID_CYCLESC_UP:
1552 rr = TAILQ_NEXT(r, entry);
1554 rr = TAILQ_FIRST(&screens[i].rl);
1556 case SWM_ARG_ID_CYCLESC_DOWN:
1557 rr = TAILQ_PREV(r, swm_region_list, entry);
1559 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1567 /* move mouse to region */
1569 y = rr->g.y + 1 + bar_enabled ? bar_height : 0;
1570 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1572 a.id = SWM_ARG_ID_FOCUSCUR;
1575 if (rr->ws->focus) {
1576 /* move to focus window */
1577 x = rr->ws->focus->g.x + 1;
1578 y = rr->ws->focus->g.y + 1;
1579 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1584 swapwin(struct swm_region *r, union arg *args)
1586 struct ws_win *target, *source;
1587 struct ws_win *cur_focus;
1588 struct ws_win_list *wl;
1591 DNPRINTF(SWM_D_WS, "swapwin id %d "
1592 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1593 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1595 cur_focus = r->ws->focus;
1596 if (cur_focus == NULL)
1600 wl = &source->ws->winlist;
1603 case SWM_ARG_ID_SWAPPREV:
1604 target = TAILQ_PREV(source, ws_win_list, entry);
1605 TAILQ_REMOVE(wl, cur_focus, entry);
1607 TAILQ_INSERT_TAIL(wl, source, entry);
1609 TAILQ_INSERT_BEFORE(target, source, entry);
1611 case SWM_ARG_ID_SWAPNEXT:
1612 target = TAILQ_NEXT(source, entry);
1613 TAILQ_REMOVE(wl, source, entry);
1615 TAILQ_INSERT_HEAD(wl, source, entry);
1617 TAILQ_INSERT_AFTER(wl, target, source, entry);
1619 case SWM_ARG_ID_SWAPMAIN:
1620 target = TAILQ_FIRST(wl);
1621 if (target == source) {
1622 if (source->ws->focus_prev != NULL &&
1623 source->ws->focus_prev != target)
1625 source = source->ws->focus_prev;
1629 if (target == NULL || source == NULL)
1631 source->ws->focus_prev = target;
1632 TAILQ_REMOVE(wl, target, entry);
1633 TAILQ_INSERT_BEFORE(source, target, entry);
1634 TAILQ_REMOVE(wl, source, entry);
1635 TAILQ_INSERT_HEAD(wl, source, entry);
1638 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1646 focus_prev(struct ws_win *win)
1648 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
1649 struct ws_win *cur_focus = NULL;
1650 struct ws_win_list *wl = NULL;
1651 struct workspace *ws = NULL;
1653 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
1655 if (!(win && win->ws))
1660 cur_focus = ws->focus;
1661 winlostfocus = cur_focus;
1663 /* pickle, just focus on whatever */
1664 if (cur_focus == NULL) {
1665 /* use prev_focus if valid */
1666 if (ws->focus_prev && ws->focus_prev != cur_focus &&
1667 find_window(WINID(ws->focus_prev)))
1668 winfocus = ws->focus_prev;
1669 if (winfocus == NULL)
1670 winfocus = TAILQ_FIRST(wl);
1674 /* if transient focus on parent */
1675 if (cur_focus->transient) {
1676 winfocus = find_window(cur_focus->transient);
1680 /* if in max_stack try harder */
1681 if (ws->cur_layout->flags & SWM_L_FOCUSPREV) {
1682 if (cur_focus != ws->focus_prev)
1683 winfocus = ws->focus_prev;
1684 else if (cur_focus != ws->focus)
1685 winfocus = ws->focus;
1687 winfocus = TAILQ_PREV(win, ws_win_list, entry);
1692 if (cur_focus == win)
1693 winfocus = TAILQ_PREV(win, ws_win_list, entry);
1694 if (winfocus == NULL)
1695 winfocus = TAILQ_LAST(wl, ws_win_list);
1696 if (winfocus == NULL || winfocus == win)
1697 winfocus = TAILQ_NEXT(cur_focus, entry);
1699 if (winfocus == winlostfocus || winfocus == NULL)
1702 focus_magic(winfocus, SWM_F_GENERIC);
1706 focus(struct swm_region *r, union arg *args)
1708 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
1709 struct ws_win *cur_focus = NULL;
1710 struct ws_win_list *wl = NULL;
1711 struct workspace *ws = NULL;
1716 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1718 /* treat FOCUS_CUR special */
1719 if (args->id == SWM_ARG_ID_FOCUSCUR) {
1721 winfocus = r->ws->focus;
1722 else if (r->ws->focus_prev)
1723 winfocus = r->ws->focus_prev;
1725 winfocus = TAILQ_FIRST(&r->ws->winlist);
1727 focus_magic(winfocus, SWM_F_GENERIC);
1731 if ((cur_focus = r->ws->focus) == NULL)
1736 winlostfocus = cur_focus;
1739 case SWM_ARG_ID_FOCUSPREV:
1740 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1741 if (winfocus == NULL)
1742 winfocus = TAILQ_LAST(wl, ws_win_list);
1745 case SWM_ARG_ID_FOCUSNEXT:
1746 winfocus = TAILQ_NEXT(cur_focus, entry);
1747 if (winfocus == NULL)
1748 winfocus = TAILQ_FIRST(wl);
1751 case SWM_ARG_ID_FOCUSMAIN:
1752 winfocus = TAILQ_FIRST(wl);
1753 if (winfocus == cur_focus)
1754 winfocus = cur_focus->ws->focus_prev;
1761 if (winfocus == winlostfocus || winfocus == NULL)
1764 focus_magic(winfocus, SWM_F_GENERIC);
1768 cycle_layout(struct swm_region *r, union arg *args)
1770 struct workspace *ws = r->ws;
1771 struct ws_win *winfocus;
1774 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1776 winfocus = ws->focus;
1779 if (ws->cur_layout->l_stack == NULL)
1780 ws->cur_layout = &layouts[0];
1783 a.id = SWM_ARG_ID_FOCUSCUR;
1789 stack_config(struct swm_region *r, union arg *args)
1791 struct workspace *ws = r->ws;
1793 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1796 if (ws->cur_layout->l_config != NULL)
1797 ws->cur_layout->l_config(ws, args->id);
1799 if (args->id != SWM_ARG_ID_STACKINIT);
1805 struct swm_geometry g;
1806 struct swm_region *r;
1809 DNPRINTF(SWM_D_STACK, "stack\n");
1811 for (i = 0; i < ScreenCount(display); i++) {
1813 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1814 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1815 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1817 /* start with screen geometry, adjust for bar */
1825 r->ws->cur_layout->l_stack(r->ws, &g);
1833 stack_floater(struct ws_win *win, struct swm_region *r)
1841 bzero(&wc, sizeof wc);
1842 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1843 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
1844 (win->g.h >= HEIGHT(r)))
1845 wc.border_width = 0;
1847 wc.border_width = 1;
1848 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
1849 win->g.w = (double)WIDTH(r) * dialog_ratio;
1850 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1852 wc.width = win->g.w;
1853 wc.height = win->g.h;
1858 wc.x = (WIDTH(r) - win->g.w) / 2;
1859 wc.y = (HEIGHT(r) - win->g.h) / 2;
1862 /* adjust for region */
1870 win->g.w = wc.width;
1871 win->g.h = wc.height;
1873 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
1874 win->id, wc.x, wc.y, wc.width, wc.height);
1876 XConfigureWindow(display, win->id, mask, &wc);
1881 * Send keystrokes to terminal to decrease/increase the font size as the
1882 * window size changes.
1885 adjust_font(struct ws_win *win)
1887 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
1888 win->floating || win->transient)
1891 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
1892 win->g.w / win->sh.width_inc < term_width &&
1893 win->font_steps < SWM_MAX_FONT_STEPS) {
1894 win->font_size_boundary[win->font_steps] =
1895 (win->sh.width_inc * term_width) + win->sh.base_width;
1898 win->last_inc = win->sh.width_inc;
1899 fake_keypress(win, XK_KP_Subtract, ShiftMask);
1900 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
1901 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
1904 win->last_inc = win->sh.width_inc;
1905 fake_keypress(win, XK_KP_Add, ShiftMask);
1909 #define SWAPXY(g) do { \
1911 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1912 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1915 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1918 struct swm_geometry win_g, r_g = *g;
1920 int i, j, s, stacks;
1921 int w_inc = 1, h_inc, w_base = 1, h_base;
1922 int hrh, extra = 0, h_slice, last_h = 0;
1923 int split, colno, winno, mwin, msize, mscale;
1924 int remain, missing, v_slice, reconfigure;
1927 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1928 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1930 winno = count_win(ws, 0);
1931 if (winno == 0 && count_win(ws, 1) == 0)
1934 TAILQ_FOREACH(win, &ws->winlist, entry)
1935 if (win->transient == 0 && win->floating == 0)
1942 w_inc = win->sh.width_inc;
1943 w_base = win->sh.base_width;
1944 mwin = ws->l_state.horizontal_mwin;
1945 mscale = ws->l_state.horizontal_msize;
1946 stacks = ws->l_state.horizontal_stacks;
1949 w_inc = win->sh.height_inc;
1950 w_base = win->sh.base_height;
1951 mwin = ws->l_state.vertical_mwin;
1952 mscale = ws->l_state.vertical_msize;
1953 stacks = ws->l_state.vertical_stacks;
1957 if (stacks > winno - mwin)
1958 stacks = winno - mwin;
1962 h_slice = r_g.h / SWM_H_SLICE;
1963 if (mwin && winno > mwin) {
1964 v_slice = r_g.w / SWM_V_SLICE;
1968 win_g.w = v_slice * mscale;
1970 if (w_inc > 1 && w_inc < v_slice) {
1971 /* adjust for window's requested size increment */
1972 remain = (win_g.w - w_base) % w_inc;
1973 missing = w_inc - remain;
1980 win_g.x += r_g.w - msize;
1983 colno = split = winno / stacks;
1984 win_g.w = ((r_g.w - (stacks * 2) + 2) / stacks);
1986 hrh = r_g.h / colno;
1987 extra = r_g.h - (colno * hrh);
1990 /* stack all the tiled windows */
1991 i = j = 0, s = stacks;
1992 TAILQ_FOREACH(win, &ws->winlist, entry) {
1993 if (win->transient != 0 || win->floating != 0)
1996 if (split && i == split) {
1997 colno = (winno - mwin) / stacks;
1998 if (s <= (winno - mwin) % stacks)
2000 split = split + colno;
2001 hrh = (r_g.h / colno);
2002 extra = r_g.h - (colno * hrh);
2006 win_g.x += win_g.w + 2;
2007 win_g.w = (r_g.w - msize - (stacks * 2)) / stacks;
2009 win_g.w += (r_g.w - msize - (stacks * 2)) %
2016 h_inc = win->sh.width_inc;
2017 h_base = win->sh.base_width;
2019 h_inc = win->sh.height_inc;
2020 h_base = win->sh.base_height;
2022 if (j == colno - 1) {
2023 win_g.h = hrh + extra;
2024 } else if (h_inc > 1 && h_inc < h_slice) {
2025 /* adjust for window's requested size increment */
2026 remain = (win_g.h - h_base) % h_inc;
2027 missing = h_inc - remain;
2029 if (missing <= extra || j == 0) {
2041 win_g.y += last_h + 2;
2043 bzero(&wc, sizeof wc);
2044 wc.border_width = 1;
2047 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2048 win->g.w != win_g.h || win->g.h != win_g.w) {
2050 win->g.x = wc.x = win_g.y;
2051 win->g.y = wc.y = win_g.x;
2052 win->g.w = wc.width = win_g.h;
2053 win->g.h = wc.height = win_g.w;
2056 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2057 win->g.w != win_g.w || win->g.h != win_g.h) {
2059 win->g.x = wc.x = win_g.x;
2060 win->g.y = wc.y = win_g.y;
2061 win->g.w = wc.width = win_g.w;
2062 win->g.h = wc.height = win_g.h;
2067 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2068 XConfigureWindow(display, win->id, mask, &wc);
2071 XMapRaised(display, win->id);
2079 /* now, stack all the floaters and transients */
2080 TAILQ_FOREACH(win, &ws->winlist, entry) {
2081 if (win->transient == 0 && win->floating == 0)
2084 stack_floater(win, ws->r);
2085 XMapRaised(display, win->id);
2090 vertical_config(struct workspace *ws, int id)
2092 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2095 case SWM_ARG_ID_STACKRESET:
2096 case SWM_ARG_ID_STACKINIT:
2097 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2098 ws->l_state.vertical_mwin = 1;
2099 ws->l_state.vertical_stacks = 1;
2101 case SWM_ARG_ID_MASTERSHRINK:
2102 if (ws->l_state.vertical_msize > 1)
2103 ws->l_state.vertical_msize--;
2105 case SWM_ARG_ID_MASTERGROW:
2106 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2107 ws->l_state.vertical_msize++;
2109 case SWM_ARG_ID_MASTERADD:
2110 ws->l_state.vertical_mwin++;
2112 case SWM_ARG_ID_MASTERDEL:
2113 if (ws->l_state.vertical_mwin > 0)
2114 ws->l_state.vertical_mwin--;
2116 case SWM_ARG_ID_STACKINC:
2117 ws->l_state.vertical_stacks++;
2119 case SWM_ARG_ID_STACKDEC:
2120 if (ws->l_state.vertical_stacks > 1)
2121 ws->l_state.vertical_stacks--;
2129 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2131 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2133 stack_master(ws, g, 0, 0);
2137 horizontal_config(struct workspace *ws, int id)
2139 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2142 case SWM_ARG_ID_STACKRESET:
2143 case SWM_ARG_ID_STACKINIT:
2144 ws->l_state.horizontal_mwin = 1;
2145 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2146 ws->l_state.horizontal_stacks = 1;
2148 case SWM_ARG_ID_MASTERSHRINK:
2149 if (ws->l_state.horizontal_msize > 1)
2150 ws->l_state.horizontal_msize--;
2152 case SWM_ARG_ID_MASTERGROW:
2153 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2154 ws->l_state.horizontal_msize++;
2156 case SWM_ARG_ID_MASTERADD:
2157 ws->l_state.horizontal_mwin++;
2159 case SWM_ARG_ID_MASTERDEL:
2160 if (ws->l_state.horizontal_mwin > 0)
2161 ws->l_state.horizontal_mwin--;
2163 case SWM_ARG_ID_STACKINC:
2164 ws->l_state.horizontal_stacks++;
2166 case SWM_ARG_ID_STACKDEC:
2167 if (ws->l_state.horizontal_stacks > 1)
2168 ws->l_state.horizontal_stacks--;
2176 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2178 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2180 stack_master(ws, g, 1, 0);
2183 /* fullscreen view */
2185 max_stack(struct workspace *ws, struct swm_geometry *g)
2188 struct swm_geometry gg = *g;
2189 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2193 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2198 winno = count_win(ws, 0);
2199 if (winno == 0 && count_win(ws, 1) == 0)
2202 TAILQ_FOREACH(win, &ws->winlist, entry) {
2203 if (win->transient) {
2205 parent = find_window(win->transient);
2209 /* only reconfigure if necessary */
2210 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2212 bzero(&wc, sizeof wc);
2213 wc.border_width = 1;
2214 win->g.x = wc.x = gg.x;
2215 win->g.y = wc.y = gg.y;
2216 win->g.w = wc.width = gg.w;
2217 win->g.h = wc.height = gg.h;
2218 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2219 XConfigureWindow(display, win->id, mask, &wc);
2222 /* unmap only if we don't have multi screen */
2223 if (win != ws->focus)
2224 if (!(ScreenCount(display) > 1 || outputs > 1))
2228 /* put the last transient on top */
2231 XMapRaised(display, parent->id);
2232 stack_floater(wintrans, ws->r);
2233 focus_magic(wintrans, SWM_F_TRANSIENT);
2238 send_to_ws(struct swm_region *r, union arg *args)
2240 int wsid = args->id;
2241 struct ws_win *win = win;
2242 struct workspace *ws, *nws;
2243 Atom ws_idx_atom = 0;
2244 unsigned char ws_idx_str[SWM_PROPLEN];
2253 if (win->ws->idx == wsid)
2256 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2259 nws = &win->s->ws[wsid];
2261 a.id = SWM_ARG_ID_FOCUSPREV;
2264 TAILQ_REMOVE(&ws->winlist, win, entry);
2265 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2268 /* Try to update the window's workspace property */
2269 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2271 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2272 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2274 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2275 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2281 wkill(struct swm_region *r, union arg *args)
2283 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
2285 if(r->ws->focus == NULL)
2288 if (args->id == SWM_ARG_ID_KILLWINDOW)
2289 XKillClient(display, r->ws->focus->id);
2291 if (r->ws->focus->can_delete)
2292 client_msg(r->ws->focus, adelete);
2296 floating_toggle(struct swm_region *r, union arg *args)
2298 struct ws_win *win = r->ws->focus;
2304 win->floating = !win->floating;
2307 a.id = SWM_ARG_ID_FOCUSCUR;
2308 focus(win->ws->r, &a);
2312 resize_window(struct ws_win *win, int center)
2316 struct swm_region *r;
2318 r = root_to_region(win->wa.root);
2319 bzero(&wc, sizeof wc);
2320 mask = CWBorderWidth | CWWidth | CWHeight;
2321 wc.border_width = 1;
2322 wc.width = win->g.w;
2323 wc.height = win->g.h;
2324 if (center == SWM_ARG_ID_CENTER) {
2325 wc.x = (WIDTH(r) - win->g.w) / 2;
2326 wc.y = (HEIGHT(r) - win->g.h) / 2;
2330 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
2331 win->id, wc.x, wc.y, wc.width, wc.height);
2333 XConfigureWindow(display, win->id, mask, &wc);
2338 resize(struct ws_win *win, union arg *args)
2342 struct swm_region *r = win->ws->r;
2344 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
2345 win->id, win->floating, win->transient);
2347 if (!(win->transient != 0 || win->floating != 0))
2350 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2351 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2353 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w, win->g.h);
2355 XMaskEvent(display, MOUSEMASK | ExposureMask |
2356 SubstructureRedirectMask, &ev);
2358 case ConfigureRequest:
2361 handler[ev.type](&ev);
2364 /* do not allow resize outside of region */
2365 if (ev.xmotion.y_root < r->g.y ||
2366 ev.xmotion.y_root >= r->g.y + r->g.h - 1)
2368 if (ev.xmotion.x_root < r->g.x ||
2369 ev.xmotion.x_root >= r->g.x + r->g.w - 1)
2372 if (ev.xmotion.x <= 1)
2374 if (ev.xmotion.y <= 1)
2376 win->g.w = ev.xmotion.x;
2377 win->g.h = ev.xmotion.y;
2379 /* not free, don't sync more than 60 times / second */
2380 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2381 time = ev.xmotion.time;
2382 XSync(display, False);
2383 resize_window(win, args->id);
2387 } while (ev.type != ButtonRelease);
2389 XSync(display, False);
2390 resize_window(win, args->id);
2392 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
2394 XUngrabPointer(display, CurrentTime);
2397 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2401 move_window(struct ws_win *win)
2405 struct swm_region *r;
2407 r = root_to_region(win->wa.root);
2408 bzero(&wc, sizeof wc);
2413 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
2414 win->id, wc.x, wc.y, wc.width, wc.height);
2416 XConfigureWindow(display, win->id, mask, &wc);
2421 move(struct ws_win *win, union arg *args)
2426 struct swm_region *r = win->ws->r;
2428 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
2429 win->id, win->floating, win->transient);
2431 if (win->floating == 0) {
2437 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2438 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2440 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2442 XMaskEvent(display, MOUSEMASK | ExposureMask |
2443 SubstructureRedirectMask, &ev);
2445 case ConfigureRequest:
2448 handler[ev.type](&ev);
2451 /* don't allow to move window out of region */
2452 if (ev.xmotion.y_root < r->g.y ||
2453 ev.xmotion.y_root + win->g.h >= r->g.y + r->g.h - 1)
2455 if (ev.xmotion.x_root < r->g.x ||
2456 ev.xmotion.x_root + win->g.w >= r->g.x + r->g.w - 1)
2459 win->g.x = ev.xmotion.x_root;
2460 win->g.y = ev.xmotion.y_root;
2462 /* not free, don't sync more than 60 times / second */
2463 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2464 time = ev.xmotion.time;
2465 XSync(display, False);
2470 } while (ev.type != ButtonRelease);
2472 XSync(display, False);
2475 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2476 XUngrabPointer(display, CurrentTime);
2481 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2484 /* key definitions */
2486 dummykeyfunc(struct swm_region *r, union arg *args)
2491 legacyfunc(struct swm_region *r, union arg *args)
2496 char name[SWM_FUNCNAME_LEN];
2497 void (*func)(struct swm_region *r, union arg *);
2499 } keyfuncs[kf_invalid + 1] = {
2500 /* name function argument */
2501 { "cycle_layout", cycle_layout, {0} },
2502 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
2503 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
2504 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
2505 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
2506 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
2507 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
2508 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
2509 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
2510 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
2511 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
2512 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
2513 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
2514 { "spawn_term", spawnterm, {.argv = spawn_term} },
2515 { "spawn_menu", legacyfunc, {0} },
2516 { "quit", quit, {0} },
2517 { "restart", restart, {0} },
2518 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
2519 { "ws_1", switchws, {.id = 0} },
2520 { "ws_2", switchws, {.id = 1} },
2521 { "ws_3", switchws, {.id = 2} },
2522 { "ws_4", switchws, {.id = 3} },
2523 { "ws_5", switchws, {.id = 4} },
2524 { "ws_6", switchws, {.id = 5} },
2525 { "ws_7", switchws, {.id = 6} },
2526 { "ws_8", switchws, {.id = 7} },
2527 { "ws_9", switchws, {.id = 8} },
2528 { "ws_10", switchws, {.id = 9} },
2529 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
2530 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
2531 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
2532 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
2533 { "mvws_1", send_to_ws, {.id = 0} },
2534 { "mvws_2", send_to_ws, {.id = 1} },
2535 { "mvws_3", send_to_ws, {.id = 2} },
2536 { "mvws_4", send_to_ws, {.id = 3} },
2537 { "mvws_5", send_to_ws, {.id = 4} },
2538 { "mvws_6", send_to_ws, {.id = 5} },
2539 { "mvws_7", send_to_ws, {.id = 6} },
2540 { "mvws_8", send_to_ws, {.id = 7} },
2541 { "mvws_9", send_to_ws, {.id = 8} },
2542 { "mvws_10", send_to_ws, {.id = 9} },
2543 { "bar_toggle", bar_toggle, {0} },
2544 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
2545 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
2546 { "screenshot_all", legacyfunc, {0} },
2547 { "screenshot_wind", legacyfunc, {0} },
2548 { "float_toggle", floating_toggle,{0} },
2549 { "version", version, {0} },
2550 { "spawn_lock", legacyfunc, {0} },
2551 { "spawn_initscr", legacyfunc, {0} },
2552 { "spawn_custom", dummykeyfunc, {0} },
2553 { "dumpwins", dumpwins, {0} },
2554 { "invalid key func", NULL, {0} },
2559 enum keyfuncid funcid;
2562 int keys_size = 0, keys_length = 0;
2563 struct key *keys = NULL;
2566 enum { client_click, root_click };
2568 unsigned int action;
2570 unsigned int button;
2571 void (*func)(struct ws_win *, union arg *);
2574 /* action key mouse button func args */
2575 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
2576 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
2577 { client_click, MODKEY, Button1, move, {0} },
2581 update_modkey(unsigned int mod)
2586 for (i = 0; i < keys_length; i++)
2587 if (keys[i].mod & ShiftMask)
2588 keys[i].mod = mod | ShiftMask;
2592 for (i = 0; i < LENGTH(buttons); i++)
2593 if (buttons[i].mask & ShiftMask)
2594 buttons[i].mask = mod | ShiftMask;
2596 buttons[i].mask = mod;
2606 int spawns_size = 0, spawns_length = 0;
2607 struct spawn_prog *spawns = NULL;
2610 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
2613 struct spawn_prog *prog = NULL;
2615 char *ap, **real_args;
2617 DNPRINTF(SWM_D_SPAWN, "spawn_custom %s\n", spawn_name);
2620 for (i = 0; i < spawns_length; i++) {
2621 if (!strcasecmp(spawn_name, spawns[i].name))
2625 fprintf(stderr, "spawn_custom: program %s not found\n",
2630 /* make room for expanded args */
2631 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
2632 err(1, "spawn_custom: calloc real_args");
2634 /* expand spawn_args into real_args */
2635 for (i = 0; i < prog->argc; i++) {
2637 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
2638 if (!strcasecmp(ap, "$bar_border")) {
2640 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
2642 err(1, "spawn_custom border color");
2643 } else if (!strcasecmp(ap, "$bar_color")) {
2645 strdup(r->s->c[SWM_S_COLOR_BAR].name))
2647 err(1, "spawn_custom bar color");
2648 } else if (!strcasecmp(ap, "$bar_font")) {
2649 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
2651 err(1, "spawn_custom bar fonts");
2652 } else if (!strcasecmp(ap, "$bar_font_color")) {
2654 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
2656 err(1, "spawn_custom color font");
2657 } else if (!strcasecmp(ap, "$color_focus")) {
2659 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
2661 err(1, "spawn_custom color focus");
2662 } else if (!strcasecmp(ap, "$color_unfocus")) {
2664 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
2666 err(1, "spawn_custom color unfocus");
2668 /* no match --> copy as is */
2669 if ((real_args[i] = strdup(ap)) == NULL)
2670 err(1, "spawn_custom strdup(ap)");
2672 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
2677 if ((swm_debug & SWM_D_SPAWN) != 0) {
2678 fprintf(stderr, "spawn_custom: result = ");
2679 for (i = 0; i < prog->argc; i++)
2680 fprintf(stderr, "\"%s\" ", real_args[i]);
2681 fprintf(stderr, "\n");
2687 for (i = 0; i < prog->argc; i++)
2693 setspawn(struct spawn_prog *prog)
2697 if (prog == NULL || prog->name == NULL)
2701 for (i = 0; i < spawns_length; i++) {
2702 if (!strcmp(spawns[i].name, prog->name)) {
2704 if (prog->argv == NULL) {
2706 DNPRINTF(SWM_D_SPAWN,
2707 "setspawn: delete #%d %s\n",
2709 free(spawns[i].name);
2710 for (j = 0; j < spawns[i].argc; j++)
2711 free(spawns[i].argv[j]);
2712 free(spawns[i].argv);
2713 j = spawns_length - 1;
2715 spawns[i] = spawns[j];
2720 DNPRINTF(SWM_D_SPAWN,
2721 "setspawn: replace #%d %s\n",
2723 free(spawns[i].name);
2724 for (j = 0; j < spawns[i].argc; j++)
2725 free(spawns[i].argv[j]);
2726 free(spawns[i].argv);
2729 /* found case handled */
2735 if (prog->argv == NULL) {
2737 "error: setspawn: cannot find program %s", prog->name);
2742 /* not found: add */
2743 if (spawns_size == 0 || spawns == NULL) {
2745 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
2746 spawns = malloc((size_t)spawns_size *
2747 sizeof(struct spawn_prog));
2748 if (spawns == NULL) {
2749 fprintf(stderr, "setspawn: malloc failed\n");
2753 } else if (spawns_length == spawns_size) {
2755 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
2756 spawns = realloc(spawns, (size_t)spawns_size *
2757 sizeof(struct spawn_prog));
2758 if (spawns == NULL) {
2759 fprintf(stderr, "setspawn: realloc failed\n");
2765 if (spawns_length < spawns_size) {
2766 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
2767 spawns_length, prog->name);
2768 i = spawns_length++;
2771 fprintf(stderr, "spawns array problem?\n");
2772 if (spawns == NULL) {
2773 fprintf(stderr, "spawns array is NULL!\n");
2781 setconfspawn(char *selector, char *value, int flags)
2783 struct spawn_prog *prog;
2784 char *vp, *cp, *word;
2786 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
2787 if ((prog = calloc(1, sizeof *prog)) == NULL)
2788 err(1, "setconfspawn: calloc prog");
2789 prog->name = strdup(selector);
2790 if (prog->name == NULL)
2791 err(1, "setconfspawn prog->name");
2792 if ((cp = vp = strdup(value)) == NULL)
2793 err(1, "setconfspawn: strdup(value) ");
2794 while ((word = strsep(&cp, " \t")) != NULL) {
2795 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
2797 cp += (long)strspn(cp, " \t");
2798 if (strlen(word) > 0) {
2800 prog->argv = realloc(prog->argv,
2801 prog->argc * sizeof(char *));
2802 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
2803 err(1, "setconfspawn: strdup");
2810 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
2817 setconfspawn("term", "xterm", 0);
2818 setconfspawn("screenshot_all", "screenshot.sh full", 0);
2819 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
2820 setconfspawn("lock", "xlock", 0);
2821 setconfspawn("initscr", "initscreen.sh", 0);
2822 setconfspawn("menu", "dmenu_run"
2825 " -nf $bar_font_color"
2827 " -sf $bar_color", 0);
2831 #define SWM_MODNAME_SIZE 32
2832 #define SWM_KEY_WS "\n+ \t"
2834 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
2838 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
2839 if (mod == NULL || ks == NULL) {
2840 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
2843 if (keystr == NULL || strlen(keystr) == 0) {
2844 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
2850 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
2851 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
2853 cp += (long)strspn(cp, SWM_KEY_WS);
2854 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
2856 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
2858 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
2860 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
2862 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
2864 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
2866 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
2867 *mod |= ControlMask;
2869 *ks = XStringToKeysym(name);
2870 XConvertCase(*ks, ks, &uks);
2871 if (ks == NoSymbol) {
2873 "parsekeys: invalid key %s\n",
2879 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
2884 strdupsafe(char *str)
2889 return (strdup(str));
2893 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
2896 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
2897 keyfuncs[kfid].name, spawn_name);
2899 for (i = 0; i < keys_length; i++) {
2900 if (keys[i].mod == mod && keys[i].keysym == ks) {
2901 if (kfid == kf_invalid) {
2904 "setkeybinding: delete #%d %s\n",
2905 i, keyfuncs[keys[i].funcid].name);
2906 free(keys[i].spawn_name);
2907 j = keys_length - 1;
2911 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2914 /* found: replace */
2916 "setkeybinding: replace #%d %s %s\n",
2917 i, keyfuncs[keys[i].funcid].name,
2919 free(keys[i].spawn_name);
2921 keys[i].keysym = ks;
2922 keys[i].funcid = kfid;
2923 keys[i].spawn_name = strdupsafe(spawn_name);
2924 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2929 if (kfid == kf_invalid) {
2931 "error: setkeybinding: cannot find mod/key combination");
2932 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2935 /* not found: add */
2936 if (keys_size == 0 || keys == NULL) {
2938 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
2939 keys = malloc((size_t)keys_size * sizeof(struct key));
2941 fprintf(stderr, "malloc failed\n");
2945 } else if (keys_length == keys_size) {
2947 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
2948 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
2950 fprintf(stderr, "realloc failed\n");
2955 if (keys_length < keys_size) {
2957 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
2958 j, keyfuncs[kfid].name, spawn_name);
2960 keys[j].keysym = ks;
2961 keys[j].funcid = kfid;
2962 keys[j].spawn_name = strdupsafe(spawn_name);
2964 fprintf(stderr, "keys array problem?\n");
2966 fprintf(stderr, "keys array problem\n");
2970 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2974 setconfbinding(char *selector, char *value, int flags)
2976 enum keyfuncid kfid;
2980 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
2981 if (selector == NULL) {
2982 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
2983 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
2985 setkeybinding(mod, ks, kfid, NULL);
2990 /* search by key function name */
2991 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
2992 if (strncasecmp(selector, keyfuncs[kfid].name,
2993 SWM_FUNCNAME_LEN) == 0) {
2994 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
2996 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
2997 setkeybinding(mod, ks, kfid, NULL);
3003 /* search by custom spawn name */
3004 for (i = 0; i < spawns_length; i++) {
3005 if (strcasecmp(selector, spawns[i].name) == 0) {
3006 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3008 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3009 setkeybinding(mod, ks, kf_spawn_custom,
3016 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
3023 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
3024 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
3025 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
3026 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
3027 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
3028 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
3029 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
3030 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
3031 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
3032 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
3033 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
3034 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
3035 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
3036 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
3037 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
3038 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
3039 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
3040 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
3041 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
3042 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
3043 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
3044 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
3045 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
3046 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
3047 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
3048 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
3049 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
3050 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
3051 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
3052 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
3053 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
3054 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
3055 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
3056 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
3057 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
3058 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
3059 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
3060 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
3061 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
3062 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
3063 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
3064 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
3065 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
3066 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
3067 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
3068 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
3069 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
3070 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
3071 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
3072 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
3073 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
3074 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
3075 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
3077 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
3082 updatenumlockmask(void)
3085 XModifierKeymap *modmap;
3087 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
3089 modmap = XGetModifierMapping(display);
3090 for (i = 0; i < 8; i++)
3091 for (j = 0; j < modmap->max_keypermod; j++)
3092 if (modmap->modifiermap[i * modmap->max_keypermod + j]
3093 == XKeysymToKeycode(display, XK_Num_Lock))
3094 numlockmask = (1 << i);
3096 XFreeModifiermap(modmap);
3102 unsigned int i, j, k;
3104 unsigned int modifiers[] =
3105 { 0, LockMask, numlockmask, numlockmask | LockMask };
3107 DNPRINTF(SWM_D_MISC, "grabkeys\n");
3108 updatenumlockmask();
3110 for (k = 0; k < ScreenCount(display); k++) {
3111 if (TAILQ_EMPTY(&screens[k].rl))
3113 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
3114 for (i = 0; i < keys_length; i++) {
3115 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
3116 for (j = 0; j < LENGTH(modifiers); j++)
3117 XGrabKey(display, code,
3118 keys[i].mod | modifiers[j],
3119 screens[k].root, True,
3120 GrabModeAsync, GrabModeAsync);
3126 grabbuttons(struct ws_win *win, int focused)
3129 unsigned int modifiers[] =
3130 { 0, LockMask, numlockmask, numlockmask|LockMask };
3132 updatenumlockmask();
3133 XUngrabButton(display, AnyButton, AnyModifier, win->id);
3135 for (i = 0; i < LENGTH(buttons); i++)
3136 if (buttons[i].action == client_click)
3137 for (j = 0; j < LENGTH(modifiers); j++)
3138 XGrabButton(display, buttons[i].button,
3139 buttons[i].mask | modifiers[j],
3140 win->id, False, BUTTONMASK,
3141 GrabModeAsync, GrabModeSync, None,
3144 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
3145 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
3148 const char *quirkname[] = {
3149 "NONE", /* config string for "no value" */
3157 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
3158 #define SWM_Q_WS "\n|+ \t"
3160 parsequirks(char *qstr, unsigned long *quirk)
3170 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
3172 cp += (long)strspn(cp, SWM_Q_WS);
3173 for (i = 0; i < LENGTH(quirkname); i++) {
3174 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
3175 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
3180 *quirk |= 1 << (i-1);
3184 if (i >= LENGTH(quirkname)) {
3185 DNPRINTF(SWM_D_QUIRK,
3186 "parsequirks: invalid quirk [%s]\n", name);
3194 setquirk(const char *class, const char *name, const int quirk)
3199 for (i = 0; i < quirks_length; i++) {
3200 if (!strcmp(quirks[i].class, class) &&
3201 !strcmp(quirks[i].name, name)) {
3204 DNPRINTF(SWM_D_QUIRK,
3205 "setquirk: delete #%d %s:%s\n",
3206 i, quirks[i].class, quirks[i].name);
3207 free(quirks[i].class);
3208 free(quirks[i].name);
3209 j = quirks_length - 1;
3211 quirks[i] = quirks[j];
3215 /* found: replace */
3216 DNPRINTF(SWM_D_QUIRK,
3217 "setquirk: replace #%d %s:%s\n",
3218 i, quirks[i].class, quirks[i].name);
3219 free(quirks[i].class);
3220 free(quirks[i].name);
3221 quirks[i].class = strdup(class);
3222 quirks[i].name = strdup(name);
3223 quirks[i].quirk = quirk;
3230 "error: setquirk: cannot find class/name combination");
3233 /* not found: add */
3234 if (quirks_size == 0 || quirks == NULL) {
3236 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
3237 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
3239 fprintf(stderr, "setquirk: malloc failed\n");
3243 } else if (quirks_length == quirks_size) {
3245 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
3246 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
3248 fprintf(stderr, "setquirk: realloc failed\n");
3253 if (quirks_length < quirks_size) {
3254 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
3255 j = quirks_length++;
3256 quirks[j].class = strdup(class);
3257 quirks[j].name = strdup(name);
3258 quirks[j].quirk = quirk;
3260 fprintf(stderr, "quirks array problem?\n");
3262 fprintf(stderr, "quirks array problem!\n");
3269 setconfquirk(char *selector, char *value, int flags)
3271 char *cp, *class, *name;
3273 unsigned long quirks;
3274 if (selector == NULL)
3276 if ((cp = strchr(selector, ':')) == NULL)
3281 if ((retval = parsequirks(value, &quirks)) == 0)
3282 setquirk(class, name, quirks);
3289 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
3290 setquirk("OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT);
3291 setquirk("OpenOffice.org 3.0", "VCLSalFrame", SWM_Q_FLOAT);
3292 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
3293 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
3294 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3295 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
3296 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3297 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3298 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3299 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3300 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
3301 setquirk("pcb", "pcb", SWM_Q_FLOAT);
3304 /* conf file stuff */
3305 #define SWM_CONF_FILE "scrotwm.conf"
3307 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_STACK_ENABLED,
3308 SWM_S_CLOCK_ENABLED, SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE,
3309 SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
3310 SWM_S_TITLE_NAME_ENABLED, SWM_S_BAR_FONT, SWM_S_BAR_ACTION,
3311 SWM_S_SPAWN_TERM, SWM_S_SS_APP, SWM_S_DIALOG_RATIO };
3314 setconfvalue(char *selector, char *value, int flags)
3317 case SWM_S_BAR_DELAY:
3318 bar_delay = atoi(value);
3320 case SWM_S_BAR_ENABLED:
3321 bar_enabled = atoi(value);
3323 case SWM_S_STACK_ENABLED:
3324 stack_enabled = atoi(value);
3326 case SWM_S_CLOCK_ENABLED:
3327 clock_enabled = atoi(value);
3329 case SWM_S_CYCLE_EMPTY:
3330 cycle_empty = atoi(value);
3332 case SWM_S_CYCLE_VISIBLE:
3333 cycle_visible = atoi(value);
3335 case SWM_S_SS_ENABLED:
3336 ss_enabled = atoi(value);
3338 case SWM_S_TERM_WIDTH:
3339 term_width = atoi(value);
3341 case SWM_S_TITLE_CLASS_ENABLED:
3342 title_class_enabled = atoi(value);
3344 case SWM_S_TITLE_NAME_ENABLED:
3345 title_name_enabled = atoi(value);
3347 case SWM_S_BAR_FONT:
3349 if ((bar_fonts[0] = strdup(value)) == NULL)
3350 err(1, "setconfvalue: bar_font");
3352 case SWM_S_BAR_ACTION:
3354 if ((bar_argv[0] = strdup(value)) == NULL)
3355 err(1, "setconfvalue: bar_action");
3357 case SWM_S_SPAWN_TERM:
3358 free(spawn_term[0]);
3359 if ((spawn_term[0] = strdup(value)) == NULL)
3360 err(1, "setconfvalue: spawn_term");
3364 case SWM_S_DIALOG_RATIO:
3365 dialog_ratio = atof(value);
3366 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
3376 setconfmodkey(char *selector, char *value, int flags)
3378 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
3379 update_modkey(Mod1Mask);
3380 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
3381 update_modkey(Mod2Mask);
3382 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
3383 update_modkey(Mod3Mask);
3384 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
3385 update_modkey(Mod4Mask);
3392 setconfcolor(char *selector, char *value, int flags)
3394 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
3399 setconfregion(char *selector, char *value, int flags)
3401 custom_region(value);
3405 /* config options */
3406 struct config_option {
3408 int (*func)(char*, char*, int);
3411 struct config_option configopt[] = {
3412 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
3413 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
3414 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
3415 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
3416 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
3417 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
3418 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
3419 { "bind", setconfbinding, 0 },
3420 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
3421 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
3422 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
3423 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
3424 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
3425 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
3426 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
3427 { "modkey", setconfmodkey, 0 },
3428 { "program", setconfspawn, 0 },
3429 { "quirk", setconfquirk, 0 },
3430 { "region", setconfregion, 0 },
3431 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
3432 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
3433 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
3434 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
3435 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
3436 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED }
3441 conf_load(char *filename)
3444 char *line, *cp, *optsub, *optval;
3445 size_t linelen, lineno = 0;
3446 int wordlen, i, optind;
3447 struct config_option *opt;
3449 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
3451 if (filename == NULL) {
3452 fprintf(stderr, "conf_load: no filename\n");
3455 if ((config = fopen(filename, "r")) == NULL) {
3456 warn("conf_load: fopen");
3460 while (!feof(config)) {
3461 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
3464 err(1, "%s", filename);
3469 cp += strspn(cp, " \t\n"); /* eat whitespace */
3470 if (cp[0] == '\0') {
3475 /* get config option */
3476 wordlen = strcspn(cp, "=[ \t\n");
3478 warnx("%s: line %zd: no option found",
3483 for (i = 0; i < LENGTH(configopt); i++) {
3484 opt = &configopt[i];
3485 if (!strncasecmp(cp, opt->optname, wordlen) &&
3486 strlen(opt->optname) == wordlen) {
3492 warnx("%s: line %zd: unknown option %.*s",
3493 filename, lineno, wordlen, cp);
3497 cp += strspn(cp, " \t\n"); /* eat whitespace */
3498 /* get [selector] if any */
3502 wordlen = strcspn(cp, "]");
3505 warnx("%s: line %zd: syntax error",
3509 asprintf(&optsub, "%.*s", wordlen, cp);
3512 cp += strspn(cp, "] \t\n"); /* eat trailing */
3514 cp += strspn(cp, "= \t\n"); /* eat trailing */
3516 optval = strdup(cp);
3517 /* call function to deal with it all */
3518 if (configopt[optind].func(optsub, optval,
3519 configopt[optind].funcflags) != 0) {
3520 fprintf(stderr, "%s line %zd: %s\n",
3521 filename, lineno, line);
3522 errx(1, "%s: line %zd: invalid data for %s",
3523 filename, lineno, configopt[optind].optname);
3531 DNPRINTF(SWM_D_CONF, "conf_load end\n");
3537 set_child_transient(struct ws_win *win)
3539 struct ws_win *parent;
3541 parent = find_window(win->transient);
3543 parent->child_trans = win;
3547 manage_window(Window id)
3550 struct workspace *ws;
3551 struct ws_win *win, *ww;
3552 int format, i, ws_idx, n, border_me = 0;
3553 unsigned long nitems, bytes;
3554 Atom ws_idx_atom = 0, type;
3555 Atom *prot = NULL, *pp;
3556 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
3557 struct swm_region *r;
3562 if ((win = find_window(id)) != NULL)
3563 return (win); /* already being managed */
3565 /* see if we are on the unmanaged list */
3566 if ((win = find_unmanaged_window(id)) != NULL) {
3567 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
3569 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
3570 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
3572 set_child_transient(win);
3576 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
3577 errx(1, "calloc: failed to allocate memory for new window");
3579 /* Get all the window data in one shot */
3580 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3582 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
3583 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
3584 XGetWindowAttributes(display, id, &win->wa);
3585 XGetWMNormalHints(display, id, &win->sh, &mask);
3586 XGetTransientForHint(display, id, &trans);
3588 win->transient = trans;
3589 set_child_transient(win);
3590 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
3591 win->id, win->transient);
3593 /* get supported protocols */
3594 if (XGetWMProtocols(display, id, &prot, &n)) {
3595 for (i = 0, pp = prot; i < n; i++, pp++) {
3596 if (*pp == takefocus)
3597 win->take_focus = 1;
3599 win->can_delete = 1;
3606 * Figure out where to put the window. If it was previously assigned to
3607 * a workspace (either by spawn() or manually moving), and isn't
3608 * transient, * put it in the same workspace
3610 r = root_to_region(win->wa.root);
3611 if (prop && win->transient == 0) {
3612 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
3613 ws_idx = strtonum(prop, 0, 9, &errstr);
3615 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
3618 ws = &r->s->ws[ws_idx];
3621 /* this should launch transients in the same ws as parent */
3623 if ((ww = find_window(trans)) != NULL)
3630 "fix this bug mcbride\n");
3635 /* set up the window layout */
3638 win->s = r->s; /* this never changes */
3639 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
3641 win->g.w = win->wa.width;
3642 win->g.h = win->wa.height;
3643 win->g.x = win->wa.x;
3644 win->g.y = win->wa.y;
3646 /* Set window properties so we can remember this after reincarnation */
3647 if (ws_idx_atom && prop == NULL &&
3648 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
3649 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3651 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3652 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3656 if (XGetClassHint(display, win->id, &win->ch)) {
3657 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
3658 win->ch.res_class, win->ch.res_name);
3660 /* java is retarded so treat it special */
3661 if (strstr(win->ch.res_name, "sun-awt")) {
3666 for (i = 0; i < quirks_length; i++){
3667 if (!strcmp(win->ch.res_class, quirks[i].class) &&
3668 !strcmp(win->ch.res_name, quirks[i].name)) {
3669 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
3670 win->ch.res_class, win->ch.res_name);
3671 if (quirks[i].quirk & SWM_Q_FLOAT) {
3675 win->quirks = quirks[i].quirk;
3680 /* alter window position if quirky */
3681 if (win->quirks & SWM_Q_ANYWHERE) {
3682 win->manual = 1; /* don't center the quirky windows */
3683 bzero(&wc, sizeof wc);
3685 if (win->g.y < bar_height) {
3686 win->g.y = wc.y = bar_height;
3689 if (win->g.w + win->g.x > WIDTH(r)) {
3690 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
3696 /* Reset font sizes (the bruteforce way; no default keybinding). */
3697 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
3698 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3699 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3700 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3701 fake_keypress(win, XK_KP_Add, ShiftMask);
3706 bzero(&wc, sizeof wc);
3707 wc.border_width = 1;
3708 mask = CWBorderWidth;
3709 XConfigureWindow(display, win->id, mask, &wc);
3713 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
3714 PropertyChangeMask | StructureNotifyMask);
3716 set_win_state(win, NormalState);
3718 /* floaters need to be mapped if they are in the current workspace */
3719 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
3720 XMapRaised(display, win->id);
3726 free_window(struct ws_win *win)
3728 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
3733 /* needed for restart wm */
3734 set_win_state(win, WithdrawnState);
3736 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
3738 if (win->ch.res_class)
3739 XFree(win->ch.res_class);
3740 if (win->ch.res_name)
3741 XFree(win->ch.res_name);
3746 memset(win, 0xff, sizeof *win); /* XXX kill later */
3752 unmanage_window(struct ws_win *win)
3754 struct ws_win *parent;
3759 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
3761 if (win->transient) {
3762 parent = find_window(win->transient);
3764 parent->child_trans = NULL;
3768 TAILQ_REMOVE(&win->ws->winlist, win, entry);
3769 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
3775 focus_magic(struct ws_win *win, int do_trans)
3777 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu %d\n", WINID(win), do_trans);
3782 if (do_trans == SWM_F_TRANSIENT && win->child_trans) {
3783 /* win = parent & has a transient so focus on that */
3785 focus_win(win->child_trans);
3786 if (win->child_trans->take_focus)
3787 client_msg(win, takefocus);
3789 focus_win(win->child_trans);
3790 if (win->child_trans->take_focus)
3791 client_msg(win->child_trans, takefocus);
3796 if (win->take_focus)
3797 client_msg(win, takefocus);
3804 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
3812 XKeyEvent *ev = &e->xkey;
3814 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
3816 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
3817 for (i = 0; i < keys_length; i++)
3818 if (keysym == keys[i].keysym
3819 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
3820 && keyfuncs[keys[i].funcid].func) {
3821 if (keys[i].funcid == kf_spawn_custom)
3823 root_to_region(ev->root),
3824 &(keyfuncs[keys[i].funcid].args),
3828 keyfuncs[keys[i].funcid].func(
3829 root_to_region(ev->root),
3830 &(keyfuncs[keys[i].funcid].args)
3836 buttonpress(XEvent *e)
3840 XButtonPressedEvent *ev = &e->xbutton;
3842 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
3844 action = root_click;
3845 if ((win = find_window(ev->window)) == NULL)
3848 focus_magic(win, SWM_F_TRANSIENT);
3849 action = client_click;
3851 for (i = 0; i < LENGTH(buttons); i++)
3852 if (action == buttons[i].action && buttons[i].func &&
3853 buttons[i].button == ev->button &&
3854 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
3855 buttons[i].func(win, &buttons[i].args);
3859 configurerequest(XEvent *e)
3861 XConfigureRequestEvent *ev = &e->xconfigurerequest;
3866 if ((win = find_window(ev->window)) == NULL)
3867 if ((win = find_unmanaged_window(ev->window)) == NULL)
3871 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
3873 bzero(&wc, sizeof wc);
3876 wc.width = ev->width;
3877 wc.height = ev->height;
3878 wc.border_width = ev->border_width;
3879 wc.sibling = ev->above;
3880 wc.stack_mode = ev->detail;
3881 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
3883 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
3885 if (win->floating) {
3886 if (ev->value_mask & CWX)
3888 if (ev->value_mask & CWY)
3890 if (ev->value_mask & CWWidth)
3891 win->g.w = ev->width;
3892 if (ev->value_mask & CWHeight)
3893 win->g.h = ev->height;
3900 configurenotify(XEvent *e)
3905 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
3906 e->xconfigure.window);
3908 win = find_window(e->xconfigure.window);
3910 XGetWMNormalHints(display, win->id, &win->sh, &mask);
3918 destroynotify(XEvent *e)
3921 XDestroyWindowEvent *ev = &e->xdestroywindow;
3923 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
3925 if ((win = find_window(ev->window)) == NULL) {
3926 if ((win = find_unmanaged_window(ev->window)) == NULL)
3932 unmanage_window(win);
3938 enternotify(XEvent *e)
3940 XCrossingEvent *ev = &e->xcrossing;
3944 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
3945 "%lu subwindow %lu same_screen %d focus %d state %d\n",
3946 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
3947 ev->same_screen, ev->focus, ev->state);
3950 * all these checks need to be in this order because the
3951 * XCheckTypedWindowEvent relies on weeding out the previous events
3953 * making this code an option would enable a follow mouse for focus
3958 * state is set when we are switching workspaces and focus is set when
3959 * scrotwm launches via a restart
3961 if (ev->state || ev->focus) {
3962 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
3967 * happens when a window is created or destroyed and the border
3968 * crosses the mouse pointer and when switching ws
3970 * we need the subwindow test to see if we came from root in order
3971 * to give focus to floaters
3973 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
3974 ev->subwindow == 0) {
3975 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
3979 /* this window already has focus */
3980 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
3981 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
3985 /* this window is being deleted or moved to another ws */
3986 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
3988 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
3989 XPutBackEvent(display, &cne);
3993 if ((win = find_window(ev->window)) == NULL) {
3994 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
3998 /* in fullstack kill all enters */
3999 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV) {
4000 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
4004 focus_magic(win, SWM_F_TRANSIENT);
4010 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
4016 DNPRINTF(SWM_D_EVENT, "focusout: window: %lu\n", e->xfocus.window);
4020 mapnotify(XEvent *e)
4023 XMapEvent *ev = &e->xmap;
4025 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
4027 win = manage_window(ev->window);
4029 set_win_state(win, NormalState);
4033 mappingnotify(XEvent *e)
4035 XMappingEvent *ev = &e->xmapping;
4037 XRefreshKeyboardMapping(ev);
4038 if (ev->request == MappingKeyboard)
4043 maprequest(XEvent *e)
4046 struct swm_region *r;
4047 XWindowAttributes wa;
4048 XMapRequestEvent *ev = &e->xmaprequest;
4050 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
4051 e->xmaprequest.window);
4053 if (!XGetWindowAttributes(display, ev->window, &wa))
4055 if (wa.override_redirect)
4058 win = manage_window(e->xmaprequest.window);
4060 return; /* can't happen */
4064 /* make new win focused */
4065 r = root_to_region(win->wa.root);
4066 if (win->ws == r->ws)
4067 focus_magic(win, SWM_F_GENERIC);
4071 propertynotify(XEvent *e)
4074 XPropertyEvent *ev = &e->xproperty;
4076 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
4079 if (ev->state == PropertyDelete)
4080 return; /* ignore */
4081 win = find_window(ev->window);
4086 case XA_WM_NORMAL_HINTS:
4089 XGetWMNormalHints(display, win->id, &win->sh, &mask);
4090 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
4091 if (win->sh.flags & PMinSize) {
4092 win->g.w = win->sh.min_width;
4093 win->g.h = win->sh.min_height;
4094 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
4096 XMoveResizeWindow(display, win->id,
4097 win->g.x, win->g.y, win->g.w, win->g.h);
4106 unmapnotify(XEvent *e)
4110 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
4112 /* determine if we need to help unmanage this window */
4113 win = find_window(e->xunmap.window);
4118 * XXX this is a work around for going fullscreen on mplayer
4119 * remove this and find a better heuristic
4124 if (getstate(e->xunmap.window) == NormalState) {
4125 unmanage_window(win);
4131 visibilitynotify(XEvent *e)
4134 struct swm_region *r;
4136 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
4137 e->xvisibility.window);
4138 if (e->xvisibility.state == VisibilityUnobscured)
4139 for (i = 0; i < ScreenCount(display); i++)
4140 TAILQ_FOREACH(r, &screens[i].rl, entry)
4141 if (e->xvisibility.window == r->bar_window)
4146 xerror_start(Display *d, XErrorEvent *ee)
4153 xerror(Display *d, XErrorEvent *ee)
4155 /* fprintf(stderr, "error: %p %p\n", display, ee); */
4163 xerrorxlib = XSetErrorHandler(xerror_start);
4165 /* this causes an error if some other window manager is running */
4166 XSelectInput(display, DefaultRootWindow(display),
4167 SubstructureRedirectMask);
4168 XSync(display, False);
4172 XSetErrorHandler(xerror);
4173 XSync(display, False);
4178 new_region(struct swm_screen *s, int x, int y, int w, int h)
4180 struct swm_region *r, *n;
4181 struct workspace *ws = NULL;
4184 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
4185 s->idx, w, h, x, y);
4187 /* remove any conflicting regions */
4188 n = TAILQ_FIRST(&s->rl);
4191 n = TAILQ_NEXT(r, entry);
4192 if (X(r) < (x + w) &&
4193 (X(r) + WIDTH(r)) > x &&
4195 (Y(r) + HEIGHT(r)) > y) {
4196 XDestroyWindow(display, r->bar_window);
4197 TAILQ_REMOVE(&s->rl, r, entry);
4198 TAILQ_INSERT_TAIL(&s->orl, r, entry);
4202 /* search old regions for one to reuse */
4204 /* size + location match */
4205 TAILQ_FOREACH(r, &s->orl, entry)
4206 if (X(r) == x && Y(r) == y &&
4207 HEIGHT(r) == h && WIDTH(r) == w)
4211 TAILQ_FOREACH(r, &s->orl, entry)
4212 if (HEIGHT(r) == h && WIDTH(r) == w)
4216 TAILQ_REMOVE(&s->orl, r, entry);
4217 /* try to use old region's workspace */
4218 if (r->ws->r == NULL)
4221 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
4222 errx(1, "calloc: failed to allocate memory for screen");
4224 /* if we don't have a workspace already, find one */
4226 for (i = 0; i < SWM_WS_MAX; i++)
4227 if (s->ws[i].r == NULL) {
4234 errx(1, "no free workspaces\n");
4243 TAILQ_INSERT_TAIL(&s->rl, r, entry);
4249 #ifdef SWM_XRR_HAS_CRTC
4251 XRRScreenResources *sr;
4254 #endif /* SWM_XRR_HAS_CRTC */
4255 struct swm_region *r;
4258 if (i >= ScreenCount(display))
4259 errx(1, "invalid screen");
4261 /* remove any old regions */
4262 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
4263 r->ws->old_r = r->ws->r = NULL;
4264 XDestroyWindow(display, r->bar_window);
4265 TAILQ_REMOVE(&screens[i].rl, r, entry);
4266 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
4269 /* map virtual screens onto physical screens */
4270 #ifdef SWM_XRR_HAS_CRTC
4272 if (xrandr_support) {
4273 sr = XRRGetScreenResources(display, screens[i].root);
4275 new_region(&screens[i], 0, 0,
4276 DisplayWidth(display, i),
4277 DisplayHeight(display, i));
4281 for (c = 0, ci = NULL; c < ncrtc; c++) {
4282 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
4283 if (ci->noutput == 0)
4287 if (ci != NULL && ci->mode == None)
4288 new_region(&screens[i], 0, 0,
4289 DisplayWidth(display, i),
4290 DisplayHeight(display, i));
4292 new_region(&screens[i],
4293 ci->x, ci->y, ci->width, ci->height);
4296 XRRFreeCrtcInfo(ci);
4297 XRRFreeScreenResources(sr);
4299 #endif /* SWM_XRR_HAS_CRTC */
4301 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
4302 DisplayHeight(display, i));
4307 screenchange(XEvent *e) {
4308 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
4309 struct swm_region *r;
4312 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
4314 if (!XRRUpdateConfiguration(e))
4317 /* silly event doesn't include the screen index */
4318 for (i = 0; i < ScreenCount(display); i++)
4319 if (screens[i].root == xe->root)
4321 if (i >= ScreenCount(display))
4322 errx(1, "screenchange: screen not found\n");
4324 /* brute force for now, just re-enumerate the regions */
4327 /* add bars to all regions */
4328 for (i = 0; i < ScreenCount(display); i++)
4329 TAILQ_FOREACH(r, &screens[i].rl, entry)
4337 Window d1, d2, *wins = NULL;
4338 XWindowAttributes wa;
4341 int errorbase, major, minor;
4342 struct workspace *ws;
4346 if ((screens = calloc(ScreenCount(display),
4347 sizeof(struct swm_screen))) == NULL)
4348 errx(1, "calloc: screens");
4350 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4352 /* initial Xrandr setup */
4353 xrandr_support = XRRQueryExtension(display,
4354 &xrandr_eventbase, &errorbase);
4356 if (XRRQueryVersion(display, &major, &minor) && major < 1)
4359 /* map physical screens */
4360 for (i = 0; i < ScreenCount(display); i++) {
4361 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
4363 TAILQ_INIT(&screens[i].rl);
4364 TAILQ_INIT(&screens[i].orl);
4365 screens[i].root = RootWindow(display, i);
4367 /* set default colors */
4368 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
4369 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
4370 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
4371 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
4372 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
4374 /* init all workspaces */
4375 /* XXX these should be dynamically allocated too */
4376 for (j = 0; j < SWM_WS_MAX; j++) {
4377 ws = &screens[i].ws[j];
4382 TAILQ_INIT(&ws->winlist);
4383 TAILQ_INIT(&ws->unmanagedlist);
4385 for (k = 0; layouts[k].l_stack != NULL; k++)
4386 if (layouts[k].l_config != NULL)
4387 layouts[k].l_config(ws,
4388 SWM_ARG_ID_STACKINIT);
4389 ws->cur_layout = &layouts[0];
4391 /* grab existing windows (before we build the bars)*/
4392 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
4398 XRRSelectInput(display, screens[i].root,
4399 RRScreenChangeNotifyMask);
4401 /* attach windows to a region */
4402 /* normal windows */
4403 for (j = 0; j < no; j++) {
4404 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4405 wa.override_redirect ||
4406 XGetTransientForHint(display, wins[j], &d1))
4409 state = getstate(wins[j]);
4410 manage = state == IconicState;
4411 if (wa.map_state == IsViewable || manage)
4412 manage_window(wins[j]);
4414 /* transient windows */
4415 for (j = 0; j < no; j++) {
4416 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4417 wa.override_redirect)
4420 state = getstate(wins[j]);
4421 manage = state == IconicState;
4422 if (XGetTransientForHint(display, wins[j], &d1) &&
4424 manage_window(wins[j]);
4436 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
4438 err(1, "setup_globals: strdup");
4439 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
4441 err(1, "setup_globals: strdup");
4442 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
4444 err(1, "setup_globals: strdup");
4445 if ((spawn_term[0] = strdup("xterm")) == NULL)
4446 err(1, "setup_globals: strdup");
4453 Atom netwmcheck, netwmname, utf8_string;
4456 /* work around sun jdk bugs, code from wmname */
4457 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
4458 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
4459 utf8_string = XInternAtom(display, "UTF8_STRING", False);
4460 for (i = 0; i < ScreenCount(display); i++) {
4461 root = screens[i].root;
4462 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
4463 PropModeReplace, (unsigned char *)&root, 1);
4464 XChangeProperty(display, root, netwmname, utf8_string, 8,
4465 PropModeReplace, "LG3D", strlen("LG3D"));
4470 main(int argc, char *argv[])
4473 struct swm_region *r, *rr;
4474 struct ws_win *winfocus = NULL;
4477 char conf[PATH_MAX], *cfile = NULL;
4484 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
4485 SWM_VERSION, cvstag);
4486 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
4487 warnx("no locale support");
4489 if (!(display = XOpenDisplay(0)))
4490 errx(1, "can not open display");
4493 errx(1, "other wm running");
4495 /* handle some signale */
4496 installsignal(SIGINT, "INT");
4497 installsignal(SIGHUP, "HUP");
4498 installsignal(SIGQUIT, "QUIT");
4499 installsignal(SIGTERM, "TERM");
4500 installsignal(SIGCHLD, "CHLD");
4502 astate = XInternAtom(display, "WM_STATE", False);
4503 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
4504 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
4505 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
4507 /* look for local and global conf file */
4508 pwd = getpwuid(getuid());
4510 errx(1, "invalid user %d", getuid());
4518 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
4519 if (stat(conf, &sb) != -1) {
4520 if (S_ISREG(sb.st_mode))
4523 /* try global conf file */
4524 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
4525 if (!stat(conf, &sb))
4526 if (S_ISREG(sb.st_mode))
4532 /* setup all bars */
4533 for (i = 0; i < ScreenCount(display); i++)
4534 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4535 if (winfocus == NULL)
4536 winfocus = TAILQ_FIRST(&r->ws->winlist);
4540 /* set some values to work around bad programs */
4546 xfd = ConnectionNumber(display);
4548 while (XPending(display)) {
4549 XNextEvent(display, &e);
4552 if (e.type < LASTEvent) {
4554 if (handler[e.type])
4555 handler[e.type](&e);
4557 DNPRINTF(SWM_D_EVENT,
4558 "win: %lu unknown event: %d\n",
4559 e.xany.window, e.type);
4561 switch (e.type - xrandr_eventbase) {
4562 case RRScreenChangeNotify:
4566 DNPRINTF(SWM_D_EVENT,
4567 "win: %lu unknown xrandr event: "
4568 "%d\n", e.xany.window, e.type);
4574 /* if we are being restarted go focus on first window */
4576 rr = winfocus->ws->r;
4578 /* not a visible window */
4582 /* move pointer to first screen if multi screen */
4583 if (ScreenCount(display) > 1 || outputs > 1)
4584 XWarpPointer(display, None, rr->s[0].root,
4585 0, 0, 0, 0, rr->g.x,
4586 rr->g.y + bar_enabled ? bar_height : 0);
4588 a.id = SWM_ARG_ID_FOCUSCUR;
4598 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
4600 DNPRINTF(SWM_D_MISC, "select failed");
4611 XCloseDisplay(display);