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 "1.0"
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)
153 #define SWM_LIB "/usr/local/lib/libswmhack.so"
160 volatile sig_atomic_t running = 1;
162 int (*xerrorxlib)(Display *, XErrorEvent *);
166 int xrandr_eventbase;
167 int ignore_enter = 0;
168 unsigned int numlockmask = 0;
172 int cycle_visible = 0;
174 int font_adjusted = 0;
175 unsigned int mod_key = MODKEY;
178 double dialog_ratio = .6;
180 #define SWM_BAR_MAX (256)
181 char *bar_argv[] = { NULL, NULL };
183 char bar_ext[SWM_BAR_MAX];
184 char bar_vertext[SWM_BAR_MAX];
186 sig_atomic_t bar_alarm = 0;
190 int bar_extra_running = 0;
193 int stack_enabled = 1;
194 int clock_enabled = 1;
195 int title_name_enabled = 0;
196 int title_class_enabled = 0;
202 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
203 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
205 #define SWM_MENU_FN (2)
206 #define SWM_MENU_NB (4)
207 #define SWM_MENU_NF (6)
208 #define SWM_MENU_SB (8)
209 #define SWM_MENU_SF (10)
211 /* layout manager data */
212 struct swm_geometry {
222 /* virtual "screens" */
224 TAILQ_ENTRY(swm_region) entry;
225 struct swm_geometry g;
226 struct workspace *ws; /* current workspace on this region */
227 struct swm_screen *s; /* screen idx */
230 TAILQ_HEAD(swm_region_list, swm_region);
233 TAILQ_ENTRY(ws_win) entry;
235 struct swm_geometry g;
239 int font_size_boundary[SWM_MAX_FONT_STEPS];
244 unsigned long quirks;
245 struct workspace *ws; /* always valid */
246 struct swm_screen *s; /* always valid, never changes */
247 XWindowAttributes wa;
251 TAILQ_HEAD(ws_win_list, ws_win);
253 /* user/key callable function IDs */
310 /* layout handlers */
312 void vertical_config(struct workspace *, int);
313 void vertical_stack(struct workspace *, struct swm_geometry *);
314 void horizontal_config(struct workspace *, int);
315 void horizontal_stack(struct workspace *, struct swm_geometry *);
316 void max_stack(struct workspace *, struct swm_geometry *);
318 void grabbuttons(struct ws_win *, int);
319 void new_region(struct swm_screen *, int, int, int, int);
320 void unmanage_window(struct ws_win *);
323 void (*l_stack)(struct workspace *, struct swm_geometry *);
324 void (*l_config)(struct workspace *, int);
326 #define SWM_L_FOCUSPREV (1<<0)
327 #define SWM_L_MAPONFOCUS (1<<1)
330 /* stack, configure */
331 { vertical_stack, vertical_config, 0, "[|]" },
332 { horizontal_stack, horizontal_config, 0, "[-]" },
334 SWM_L_FOCUSPREV | SWM_L_MAPONFOCUS, "[ ]"},
338 #define SWM_H_SLICE (32)
339 #define SWM_V_SLICE (32)
341 /* define work spaces */
343 int idx; /* workspace index */
344 int restack; /* restack on switch */
345 struct layout *cur_layout; /* current layout handlers */
346 struct ws_win *focus; /* may be NULL */
347 struct ws_win *focus_prev; /* may be NULL */
348 struct swm_region *r; /* may be NULL */
349 struct swm_region *old_r; /* may be NULL */
350 struct ws_win_list winlist; /* list of windows in ws */
354 int horizontal_msize;
356 int horizontal_stacks;
363 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
364 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
366 /* physical screen mapping */
367 #define SWM_WS_MAX (10) /* XXX Too small? */
369 int idx; /* screen index */
370 struct swm_region_list rl; /* list of regions on this screen */
371 struct swm_region_list orl; /* list of old regions */
373 struct workspace ws[SWM_WS_MAX];
379 } c[SWM_S_COLOR_MAX];
381 struct swm_screen *screens;
384 /* args to functions */
387 #define SWM_ARG_ID_FOCUSNEXT (0)
388 #define SWM_ARG_ID_FOCUSPREV (1)
389 #define SWM_ARG_ID_FOCUSMAIN (2)
390 #define SWM_ARG_ID_SWAPNEXT (3)
391 #define SWM_ARG_ID_SWAPPREV (4)
392 #define SWM_ARG_ID_SWAPMAIN (5)
393 #define SWM_ARG_ID_MASTERSHRINK (6)
394 #define SWM_ARG_ID_MASTERGROW (7)
395 #define SWM_ARG_ID_MASTERADD (8)
396 #define SWM_ARG_ID_MASTERDEL (9)
397 #define SWM_ARG_ID_STACKRESET (10)
398 #define SWM_ARG_ID_STACKINIT (11)
399 #define SWM_ARG_ID_CYCLEWS_UP (12)
400 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
401 #define SWM_ARG_ID_CYCLESC_UP (14)
402 #define SWM_ARG_ID_CYCLESC_DOWN (15)
403 #define SWM_ARG_ID_STACKINC (16)
404 #define SWM_ARG_ID_STACKDEC (17)
405 #define SWM_ARG_ID_SS_ALL (0)
406 #define SWM_ARG_ID_SS_WINDOW (1)
407 #define SWM_ARG_ID_DONTCENTER (0)
408 #define SWM_ARG_ID_CENTER (1)
409 #define SWM_ARG_ID_KILLWINDOW (0)
410 #define SWM_ARG_ID_DELETEWINDOW (1)
419 #define SWM_Q_FLOAT (1<<0) /* float this window */
420 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
421 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
422 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
423 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
425 int quirks_size = 0, quirks_length = 0;
426 struct quirk *quirks = NULL;
443 name = "ButtonPress";
446 name = "ButtonRelease";
449 name = "MotionNotify";
452 name = "EnterNotify";
455 name = "LeaveNotify";
464 name = "KeymapNotify";
470 name = "GraphicsExpose";
475 case VisibilityNotify:
476 name = "VisibilityNotify";
479 name = "CreateNotify";
482 name = "DestroyNotify";
485 name = "UnmapNotify";
494 name = "ReparentNotify";
496 case ConfigureNotify:
497 name = "ConfigureNotify";
499 case ConfigureRequest:
500 name = "ConfigureRequest";
503 name = "GravityNotify";
506 name = "ResizeRequest";
508 case CirculateNotify:
509 name = "CirculateNotify";
511 case CirculateRequest:
512 name = "CirculateRequest";
515 name = "PropertyNotify";
518 name = "SelectionClear";
520 case SelectionRequest:
521 name = "SelectionRequest";
523 case SelectionNotify:
524 name = "SelectionNotify";
527 name = "ColormapNotify";
530 name = "ClientMessage";
533 name = "MappingNotify";
538 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
540 e->xany.window, name, e->type, QLength(display));
542 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
544 e->xany.window, e->type, QLength(display));
548 #endif /* SWM_DEBUG */
550 void expose(XEvent *);
551 void keypress(XEvent *);
552 void buttonpress(XEvent *);
553 void configurerequest(XEvent *);
554 void configurenotify(XEvent *);
555 void destroynotify(XEvent *);
556 void enternotify(XEvent *);
557 void focusin(XEvent *);
558 void focusout(XEvent *);
559 void mapnotify(XEvent *);
560 void mappingnotify(XEvent *);
561 void maprequest(XEvent *);
562 void propertynotify(XEvent *);
563 void unmapnotify(XEvent *);
564 void visibilitynotify(XEvent *);
566 void (*handler[LASTEvent])(XEvent *) = {
568 [KeyPress] = keypress,
569 [ButtonPress] = buttonpress,
570 [ConfigureRequest] = configurerequest,
571 [ConfigureNotify] = configurenotify,
572 [DestroyNotify] = destroynotify,
573 [EnterNotify] = enternotify,
575 [FocusOut] = focusout,
576 [MapNotify] = mapnotify,
577 [MappingNotify] = mappingnotify,
578 [MapRequest] = maprequest,
579 [PropertyNotify] = propertynotify,
580 [UnmapNotify] = unmapnotify,
581 [VisibilityNotify] = visibilitynotify,
591 while ((pid = waitpid(WAIT_ANY, NULL, WNOHANG)) != -1) {
592 DNPRINTF(SWM_D_MISC, "reaping: %d\n", pid);
607 installsignal(int sig, char *name)
611 sa.sa_handler = sighdlr;
612 sigemptyset(&sa.sa_mask);
614 if (sigaction(sig, &sa, NULL) == -1)
615 err(1, "could not install %s handler", name);
619 name_to_color(char *colorname)
623 XColor screen_def, exact_def;
624 unsigned long result = 0;
625 char cname[32] = "#";
627 cmap = DefaultColormap(display, screens[0].idx);
628 status = XAllocNamedColor(display, cmap, colorname,
629 &screen_def, &exact_def);
631 strlcat(cname, colorname + 2, sizeof cname - 1);
632 status = XAllocNamedColor(display, cmap, cname, &screen_def,
636 result = screen_def.pixel;
638 fprintf(stderr, "color '%s' not found.\n", colorname);
644 setscreencolor(char *val, int i, int c)
646 if (i > 0 && i <= ScreenCount(display)) {
647 screens[i - 1].c[c].color = name_to_color(val);
648 free(screens[i - 1].c[c].name);
649 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
651 } else if (i == -1) {
652 for (i = 0; i < ScreenCount(display); i++) {
653 screens[i].c[c].color = name_to_color(val);
654 free(screens[i].c[c].name);
655 if ((screens[i].c[c].name = strdup(val)) == NULL)
659 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
660 i, ScreenCount(display));
664 custom_region(char *val)
666 unsigned int sidx, x, y, w, h;
668 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
669 errx(1, "invalid custom region, "
670 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
671 if (sidx < 1 || sidx > ScreenCount(display))
672 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
673 sidx, ScreenCount(display));
677 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
679 if (x < 0 || x > DisplayWidth(display, sidx) ||
680 y < 0 || y > DisplayHeight(display, sidx) ||
681 w + x > DisplayWidth(display, sidx) ||
682 h + y > DisplayHeight(display, sidx))
683 errx(1, "region %ux%u+%u+%u not within screen boundaries "
684 "(%ux%u)\n", w, h, x, y,
685 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
687 new_region(&screens[sidx], x, y, w, h);
691 socket_setnonblock(int fd)
695 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
696 err(1, "fcntl F_GETFL");
698 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
699 err(1, "fcntl F_SETFL");
703 bar_print(struct swm_region *r, char *s)
705 XClearWindow(display, r->bar_window);
706 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
707 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
716 bzero(bar_pipe, sizeof bar_pipe);
719 kill(bar_pid, SIGTERM);
722 strlcpy(bar_ext, "", sizeof bar_ext);
727 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
729 int do_class, do_name;
731 XClassHint *xch = NULL;
733 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
735 if ((xch = XAllocClassHint()) == NULL)
737 status = XGetClassHint(display, cur_focus->id, xch);
738 if (status == BadWindow || status == BadAlloc)
740 do_class = (title_class_enabled && xch->res_class != NULL);
741 do_name = (title_name_enabled && xch->res_name != NULL);
743 strlcat(s, xch->res_class, sz);
744 if (do_class && do_name)
747 strlcat(s, xch->res_name, sz);
760 struct swm_region *r;
764 char loc[SWM_BAR_MAX];
768 if (bar_enabled == 0)
770 if (bar_extra && bar_extra_running) {
771 /* ignore short reads; it'll correct itself */
772 while ((b = fgetln(stdin, &len)) != NULL)
773 if (b && b[len - 1] == '\n') {
775 strlcpy(bar_ext, b, sizeof bar_ext);
777 if (b == NULL && errno != EAGAIN) {
778 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
779 errno, strerror(errno));
783 strlcpy(bar_ext, "", sizeof bar_ext);
785 if (clock_enabled == 0)
786 strlcpy(s, "", sizeof s);
789 localtime_r(&tmt, &tm);
790 strftime(s, sizeof s, "%a %b %d %R %Z %Y ", &tm);
793 for (i = 0; i < ScreenCount(display); i++) {
795 TAILQ_FOREACH(r, &screens[i].rl, entry) {
797 bar_class_name(s, sizeof s, r->ws->focus);
800 stack = r->ws->cur_layout->name;
802 snprintf(loc, sizeof loc, "%d:%d %s %s %s %s",
803 x++, r->ws->idx + 1, stack, s, bar_ext,
818 bar_toggle(struct swm_region *r, union arg *args)
820 struct swm_region *tmpr;
821 int i, j, sc = ScreenCount(display);
823 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
826 for (i = 0; i < sc; i++)
827 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
828 XUnmapWindow(display, tmpr->bar_window);
830 for (i = 0; i < sc; i++)
831 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
832 XMapRaised(display, tmpr->bar_window);
834 bar_enabled = !bar_enabled;
835 for (i = 0; i < sc; i++)
836 for (j = 0; j < SWM_WS_MAX; j++)
837 screens[i].ws[j].restack = 1;
840 /* must be after stack */
847 XSetWindowAttributes wa;
848 struct swm_region *r;
851 /* do this here because the conf file is in memory */
852 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
853 /* launch external status app */
854 bar_extra_running = 1;
855 if (pipe(bar_pipe) == -1)
856 err(1, "pipe error");
857 socket_setnonblock(bar_pipe[0]);
858 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
859 if (dup2(bar_pipe[0], 0) == -1)
861 if (dup2(bar_pipe[1], 1) == -1)
863 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
864 err(1, "could not disable SIGPIPE");
865 switch (bar_pid = fork()) {
867 err(1, "cannot fork");
871 execvp(bar_argv[0], bar_argv);
872 err(1, "%s external app failed", bar_argv[0]);
874 default: /* parent */
880 bzero(&wa, sizeof wa);
881 for (i = 0; i < ScreenCount(display); i++)
882 TAILQ_FOREACH(r, &screens[i].rl, entry) {
884 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
885 wa.background_pixel =
886 screens[i].c[SWM_S_COLOR_BAR].color;
887 XChangeWindowAttributes(display, r->bar_window,
888 CWBackPixel | CWBorderPixel, &wa);
894 bar_setup(struct swm_region *r)
898 for (i = 0; bar_fonts[i] != NULL; i++) {
899 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
905 if (bar_fonts[i] == NULL)
906 errx(1, "couldn't load font");
907 bar_height = bar_fs->ascent + bar_fs->descent + 3;
909 r->bar_window = XCreateSimpleWindow(display,
910 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
911 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
912 r->s->c[SWM_S_COLOR_BAR].color);
913 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
914 XSetFont(display, bar_gc, bar_fs->fid);
915 XSelectInput(display, r->bar_window, VisibilityChangeMask);
917 XMapRaised(display, r->bar_window);
918 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
920 if (signal(SIGALRM, bar_signal) == SIG_ERR)
921 err(1, "could not install bar_signal");
926 set_win_notify_cb(Display *d, XEvent *e, char *arg)
928 struct ws_win *win = (struct ws_win *)arg;
930 if (win && win->id == e->xany.window && e->xany.type == PropertyNotify)
936 set_win_state(struct ws_win *win, long state)
938 long data[] = {state, None};
940 XWindowAttributes wa;
942 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
946 /* make sure we drain everything */
947 XSync(display, True);
949 /* make sure we still exist too */
950 if (XGetWindowAttributes(display, win->id, &wa) == BadWindow)
953 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
954 (unsigned char *)data, 2);
956 /* wait for completion of XChangeProperty */
957 while (XCheckIfEvent(display, &ev, set_win_notify_cb, (char *)win))
966 unsigned char *p = NULL;
967 unsigned long n, extra;
970 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
971 &real, &format, &n, &extra, (unsigned char **)&p);
972 if (status != Success)
975 result = *((long *)p);
981 version(struct swm_region *r, union arg *args)
983 bar_version = !bar_version;
985 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
986 SWM_VERSION, cvstag);
988 strlcpy(bar_vertext, "", sizeof bar_vertext);
993 client_msg(struct ws_win *win, Atom a)
995 XClientMessageEvent cm;
997 bzero(&cm, sizeof cm);
998 cm.type = ClientMessage;
1000 cm.message_type = aprot;
1003 cm.data.l[1] = CurrentTime;
1004 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1008 config_win(struct ws_win *win)
1012 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
1013 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1014 ce.type = ConfigureNotify;
1015 ce.display = display;
1017 ce.window = win->id;
1020 ce.width = win->g.w;
1021 ce.height = win->g.h;
1022 ce.border_width = 1; /* XXX store this! */
1024 ce.override_redirect = False;
1025 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1029 count_win(struct workspace *ws, int count_transient)
1036 * Under stress conditions windows sometimes do not get removed from
1037 * the managed list quickly enough. Use a very large hammer to get rid
1038 * of them. A smaller hammer would be nice.
1040 TAILQ_FOREACH(win, &ws->winlist, entry) {
1041 state = getstate(win->id);
1043 DNPRINTF(SWM_D_MISC, "count_win:removing: %lu\n",
1045 unmanage_window(win);
1049 TAILQ_FOREACH(win, &ws->winlist, entry) {
1050 if (count_transient == 0 && win->floating)
1052 if (count_transient == 0 && win->transient)
1056 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1062 quit(struct swm_region *r, union arg *args)
1064 DNPRINTF(SWM_D_MISC, "quit\n");
1069 unmap_window_cb(Display *d, XEvent *e, char *arg)
1071 struct ws_win *win = (struct ws_win *)arg;
1073 if (win && win->id == e->xany.window && e->xany.type == UnmapNotify)
1079 unmap_window(struct ws_win *win)
1082 XWindowAttributes wa;
1087 /* make sure we still exist too */
1088 if (XGetWindowAttributes(display, win->id, &wa) == BadWindow)
1091 /* don't unmap again */
1092 if (wa.map_state == IsUnmapped && getstate(win->id) == IconicState)
1095 /* java shits itself when windows are set to iconic state */
1097 set_win_state(win, IconicState);
1099 XUnmapWindow(display, win->id);
1101 /* make sure we wait for XUnmapWindow completion */
1102 while (XCheckIfEvent(display, &ev, unmap_window_cb, (char *)win))
1112 for (i = 0; i < ScreenCount(display); i++)
1113 for (j = 0; j < SWM_WS_MAX; j++)
1114 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1119 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1123 event.display = display; /* Ignored, but what the hell */
1124 event.window = win->id;
1125 event.root = win->s->root;
1126 event.subwindow = None;
1127 event.time = CurrentTime;
1132 event.same_screen = True;
1133 event.keycode = XKeysymToKeycode(display, keysym);
1134 event.state = modifiers;
1136 event.type = KeyPress;
1137 XSendEvent(event.display, event.window, True,
1138 KeyPressMask, (XEvent *)&event);
1140 event.type = KeyRelease;
1141 XSendEvent(event.display, event.window, True,
1142 KeyPressMask, (XEvent *)&event);
1147 restart(struct swm_region *r, union arg *args)
1149 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1151 /* disable alarm because the following code may not be interrupted */
1153 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1154 errx(1, "can't disable alarm");
1159 XCloseDisplay(display);
1160 execvp(start_argv[0], start_argv);
1161 fprintf(stderr, "execvp failed\n");
1167 root_to_region(Window root)
1169 struct swm_region *r = NULL;
1171 int i, x, y, wx, wy;
1174 for (i = 0; i < ScreenCount(display); i++)
1175 if (screens[i].root == root)
1178 if (XQueryPointer(display, screens[i].root,
1179 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1180 /* choose a region based on pointer location */
1181 TAILQ_FOREACH(r, &screens[i].rl, entry)
1182 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1183 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1188 r = TAILQ_FIRST(&screens[i].rl);
1194 find_window(Window id)
1199 for (i = 0; i < ScreenCount(display); i++)
1200 for (j = 0; j < SWM_WS_MAX; j++)
1201 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1208 spawn(struct swm_region *r, union arg *args)
1213 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1215 * The double-fork construct avoids zombie processes and keeps the code
1216 * clean from stupid signal handlers.
1221 close(ConnectionNumber(display));
1222 setenv("LD_PRELOAD", SWM_LIB, 1);
1223 if (asprintf(&ret, "%d", r->ws->idx)) {
1224 setenv("_SWM_WS", ret, 1);
1227 if (asprintf(&ret, "%d", getpid())) {
1228 setenv("_SWM_PID", ret, 1);
1232 /* kill stdin, mplayer, ssh-add etc. need that */
1233 si = open("/dev/null", O_RDONLY, 0);
1235 err(1, "open /dev/null");
1236 if (dup2(si, 0) == -1)
1237 err(1, "dup2 /dev/null");
1238 execvp(args->argv[0], args->argv);
1239 fprintf(stderr, "execvp failed\n");
1247 spawnterm(struct swm_region *r, union arg *args)
1249 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1252 setenv("_SWM_XTERM_FONTADJ", "", 1);
1257 unfocus_win(struct ws_win *win)
1262 if (win->ws->r == NULL)
1265 if (win->ws->focus == win) {
1266 win->ws->focus = NULL;
1267 win->ws->focus_prev = win;
1270 grabbuttons(win, 0);
1271 XSetWindowBorder(display, win->id,
1272 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1281 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
1283 for (i = 0; i < ScreenCount(display); i++)
1284 for (j = 0; j < SWM_WS_MAX; j++)
1285 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1290 focus_win(struct ws_win *win)
1292 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1297 /* use big hammer to make sure it works under all use cases */
1299 win->ws->focus = win;
1301 if (win->ws->r != NULL) {
1302 XSetWindowBorder(display, win->id,
1303 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1304 grabbuttons(win, 1);
1305 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS)
1306 XMapRaised(display, win->id);
1307 XSetInputFocus(display, win->id,
1308 RevertToPointerRoot, CurrentTime);
1313 switchws(struct swm_region *r, union arg *args)
1315 int wsid = args->id;
1316 struct swm_region *this_r, *other_r;
1317 struct ws_win *win, *winfocus = NULL, *parent = NULL;
1318 struct workspace *new_ws, *old_ws;
1321 old_ws = this_r->ws;
1322 new_ws = &this_r->s->ws[wsid];
1324 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1325 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1328 if (new_ws == old_ws)
1331 /* get focus window */
1333 winfocus = new_ws->focus;
1334 else if (new_ws->focus_prev)
1335 winfocus = new_ws->focus_prev;
1337 winfocus = TAILQ_FIRST(&new_ws->winlist);
1339 other_r = new_ws->r;
1340 if (other_r == NULL) {
1341 /* if the other workspace is hidden, switch windows */
1342 if (old_ws->r != NULL)
1343 old_ws->old_r = old_ws->r;
1345 old_ws->restack = 1;
1348 * Map new windows first if they were here before
1349 * to minimize ugly blinking.
1351 if (new_ws->old_r == this_r)
1352 TAILQ_FOREACH(win, &new_ws->winlist, entry)
1353 if (!(win->ws->cur_layout->flags &
1355 XMapRaised(display, win->id);
1357 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1360 other_r->ws = old_ws;
1361 old_ws->r = other_r;
1363 this_r->ws = new_ws;
1369 /* make sure we see the parent window */
1370 if (winfocus->transient) {
1371 parent = find_window(winfocus->transient);
1376 focus_win(winfocus);
1383 cyclews(struct swm_region *r, union arg *args)
1386 struct swm_screen *s = r->s;
1388 DNPRINTF(SWM_D_WS, "cyclews id %d "
1389 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1390 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1395 case SWM_ARG_ID_CYCLEWS_UP:
1396 if (a.id < SWM_WS_MAX - 1)
1401 case SWM_ARG_ID_CYCLEWS_DOWN:
1405 a.id = SWM_WS_MAX - 1;
1411 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1413 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1417 } while (a.id != r->ws->idx);
1421 cyclescr(struct swm_region *r, union arg *args)
1423 struct swm_region *rr = NULL;
1424 struct workspace *ws = NULL;
1425 struct ws_win *winfocus = NULL;
1428 /* do nothing if we don't have more than one screen */
1429 if (!(ScreenCount(display) > 1 || outputs > 1))
1434 case SWM_ARG_ID_CYCLESC_UP:
1435 rr = TAILQ_NEXT(r, entry);
1437 rr = TAILQ_FIRST(&screens[i].rl);
1439 case SWM_ARG_ID_CYCLESC_DOWN:
1440 rr = TAILQ_PREV(r, swm_region_list, entry);
1442 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1451 winfocus = ws->focus;
1452 if (winfocus == NULL)
1453 winfocus = ws->focus_prev;
1455 /* use window coordinates */
1456 x = winfocus->g.x + 1;
1457 y = winfocus->g.y + 1;
1459 /* use region coordinates */
1461 y = rr->g.y + 1 + bar_enabled ? bar_height : 0;
1465 XSetInputFocus(display, PointerRoot, RevertToPointerRoot, CurrentTime);
1466 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1468 focus_win(winfocus);
1472 swapwin(struct swm_region *r, union arg *args)
1474 struct ws_win *target, *source;
1475 struct ws_win *cur_focus;
1476 struct ws_win_list *wl;
1479 DNPRINTF(SWM_D_WS, "swapwin id %d "
1480 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1481 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1483 cur_focus = r->ws->focus;
1484 if (cur_focus == NULL)
1488 wl = &source->ws->winlist;
1491 case SWM_ARG_ID_SWAPPREV:
1492 target = TAILQ_PREV(source, ws_win_list, entry);
1493 TAILQ_REMOVE(wl, cur_focus, entry);
1495 TAILQ_INSERT_TAIL(wl, source, entry);
1497 TAILQ_INSERT_BEFORE(target, source, entry);
1499 case SWM_ARG_ID_SWAPNEXT:
1500 target = TAILQ_NEXT(source, entry);
1501 TAILQ_REMOVE(wl, source, entry);
1503 TAILQ_INSERT_HEAD(wl, source, entry);
1505 TAILQ_INSERT_AFTER(wl, target, source, entry);
1507 case SWM_ARG_ID_SWAPMAIN:
1508 target = TAILQ_FIRST(wl);
1509 if (target == source) {
1510 if (source->ws->focus_prev != NULL &&
1511 source->ws->focus_prev != target)
1513 source = source->ws->focus_prev;
1517 source->ws->focus_prev = target;
1518 TAILQ_REMOVE(wl, target, entry);
1519 TAILQ_INSERT_BEFORE(source, target, entry);
1520 TAILQ_REMOVE(wl, source, entry);
1521 TAILQ_INSERT_HEAD(wl, source, entry);
1524 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1533 focus(struct swm_region *r, union arg *args)
1535 struct ws_win *winfocus, *winlostfocus;
1536 struct ws_win_list *wl;
1537 struct ws_win *cur_focus;
1539 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1541 cur_focus = r->ws->focus;
1542 if (cur_focus == NULL)
1545 wl = &cur_focus->ws->winlist;
1547 winlostfocus = cur_focus;
1550 case SWM_ARG_ID_FOCUSPREV:
1551 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1552 if (winfocus == NULL)
1553 winfocus = TAILQ_LAST(wl, ws_win_list);
1556 case SWM_ARG_ID_FOCUSNEXT:
1557 winfocus = TAILQ_NEXT(cur_focus, entry);
1558 if (winfocus == NULL)
1559 winfocus = TAILQ_FIRST(wl);
1562 case SWM_ARG_ID_FOCUSMAIN:
1563 winfocus = TAILQ_FIRST(wl);
1564 if (winfocus == cur_focus)
1565 winfocus = cur_focus->ws->focus_prev;
1566 if (winfocus == NULL)
1574 if (winfocus == winlostfocus || winfocus == NULL)
1577 focus_win(winfocus);
1581 cycle_layout(struct swm_region *r, union arg *args)
1583 struct workspace *ws = r->ws;
1584 struct ws_win *winfocus, *parent = NULL;
1586 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1588 winfocus = ws->focus;
1591 if (ws->cur_layout->l_stack == NULL)
1592 ws->cur_layout = &layouts[0];
1596 /* make sure we see the parent window */
1598 if (winfocus->transient)
1599 parent = find_window(winfocus->transient);
1602 focus_win(winfocus);
1608 stack_config(struct swm_region *r, union arg *args)
1610 struct workspace *ws = r->ws;
1612 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1615 if (ws->cur_layout->l_config != NULL)
1616 ws->cur_layout->l_config(ws, args->id);
1618 if (args->id != SWM_ARG_ID_STACKINIT);
1624 struct swm_geometry g;
1625 struct swm_region *r;
1628 DNPRINTF(SWM_D_STACK, "stack\n");
1630 for (i = 0; i < ScreenCount(display); i++) {
1632 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1633 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1634 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1636 /* start with screen geometry, adjust for bar */
1646 r->ws->cur_layout->l_stack(r->ws, &g);
1654 stack_floater(struct ws_win *win, struct swm_region *r)
1662 bzero(&wc, sizeof wc);
1663 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1664 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w == WIDTH(r)) &&
1665 (win->g.h == HEIGHT(r)))
1666 wc.border_width = 0;
1668 wc.border_width = 1;
1669 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
1670 win->g.w = (double)WIDTH(r) * dialog_ratio;
1671 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1673 wc.width = win->g.w;
1674 wc.height = win->g.h;
1679 wc.x = (WIDTH(r) - win->g.w) / 2;
1680 wc.y = (HEIGHT(r) - win->g.h) / 2;
1683 /* adjust for region */
1687 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1688 win->id, wc.x, wc.y, wc.width, wc.height);
1690 XConfigureWindow(display, win->id, mask, &wc);
1694 * Send keystrokes to terminal to decrease/increase the font size as the
1695 * window size changes.
1698 adjust_font(struct ws_win *win)
1700 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
1701 win->floating || win->transient)
1704 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
1705 win->g.w / win->sh.width_inc < term_width &&
1706 win->font_steps < SWM_MAX_FONT_STEPS) {
1707 win->font_size_boundary[win->font_steps] =
1708 (win->sh.width_inc * term_width) + win->sh.base_width;
1711 win->last_inc = win->sh.width_inc;
1712 fake_keypress(win, XK_KP_Subtract, ShiftMask);
1713 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
1714 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
1717 win->last_inc = win->sh.width_inc;
1718 fake_keypress(win, XK_KP_Add, ShiftMask);
1722 #define SWAPXY(g) do { \
1724 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1725 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1728 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1731 struct swm_geometry win_g, r_g = *g;
1733 int i, j, s, stacks;
1734 int w_inc = 1, h_inc, w_base = 1, h_base;
1735 int hrh, extra = 0, h_slice, last_h = 0;
1736 int split, colno, winno, mwin, msize, mscale;
1737 int remain, missing, v_slice;
1740 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1741 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1743 winno = count_win(ws, 0);
1744 if (winno == 0 && count_win(ws, 1) == 0)
1747 TAILQ_FOREACH(win, &ws->winlist, entry)
1748 if (win->transient == 0 && win->floating == 0)
1755 w_inc = win->sh.width_inc;
1756 w_base = win->sh.base_width;
1757 mwin = ws->l_state.horizontal_mwin;
1758 mscale = ws->l_state.horizontal_msize;
1759 stacks = ws->l_state.horizontal_stacks;
1762 w_inc = win->sh.height_inc;
1763 w_base = win->sh.base_height;
1764 mwin = ws->l_state.vertical_mwin;
1765 mscale = ws->l_state.vertical_msize;
1766 stacks = ws->l_state.vertical_stacks;
1770 if (stacks > winno - mwin)
1771 stacks = winno - mwin;
1775 h_slice = r_g.h / SWM_H_SLICE;
1776 if (mwin && winno > mwin) {
1777 v_slice = r_g.w / SWM_V_SLICE;
1781 win_g.w = v_slice * mscale;
1783 if (w_inc > 1 && w_inc < v_slice) {
1784 /* adjust for window's requested size increment */
1785 remain = (win_g.w - w_base) % w_inc;
1786 missing = w_inc - remain;
1793 win_g.x += r_g.w - msize;
1796 colno = split = winno / stacks;
1797 win_g.w = ((r_g.w - (stacks * 2) + 2) / stacks);
1799 hrh = r_g.h / colno;
1800 extra = r_g.h - (colno * hrh);
1803 /* stack all the tiled windows */
1804 i = j = 0, s = stacks;
1805 TAILQ_FOREACH(win, &ws->winlist, entry) {
1806 if (win->transient != 0 || win->floating != 0)
1809 if (split && i == split) {
1810 colno = (winno - mwin) / stacks;
1811 if (s <= (winno - mwin) % stacks)
1813 split = split + colno;
1814 hrh = (r_g.h / colno);
1815 extra = r_g.h - (colno * hrh);
1819 win_g.x += win_g.w + 2;
1820 win_g.w = (r_g.w - msize - (stacks * 2)) / stacks;
1822 win_g.w += (r_g.w - msize - (stacks * 2)) %
1829 h_inc = win->sh.width_inc;
1830 h_base = win->sh.base_width;
1832 h_inc = win->sh.height_inc;
1833 h_base = win->sh.base_height;
1835 if (j == colno - 1) {
1836 win_g.h = hrh + extra;
1837 } else if (h_inc > 1 && h_inc < h_slice) {
1838 /* adjust for window's requested size increment */
1839 remain = (win_g.h - h_base) % h_inc;
1840 missing = h_inc - remain;
1842 if (missing <= extra || j == 0) {
1854 win_g.y += last_h + 2;
1856 bzero(&wc, sizeof wc);
1857 wc.border_width = 1;
1859 win->g.x = wc.x = win_g.y;
1860 win->g.y = wc.y = win_g.x;
1861 win->g.w = wc.width = win_g.h;
1862 win->g.h = wc.height = win_g.w;
1864 win->g.x = wc.x = win_g.x;
1865 win->g.y = wc.y = win_g.y;
1866 win->g.w = wc.width = win_g.w;
1867 win->g.h = wc.height = win_g.h;
1870 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1871 XConfigureWindow(display, win->id, mask, &wc);
1872 XMapRaised(display, win->id);
1880 /* now, stack all the floaters and transients */
1881 TAILQ_FOREACH(win, &ws->winlist, entry) {
1882 if (win->transient == 0 && win->floating == 0)
1885 stack_floater(win, ws->r);
1886 XMapRaised(display, win->id);
1891 vertical_config(struct workspace *ws, int id)
1893 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1896 case SWM_ARG_ID_STACKRESET:
1897 case SWM_ARG_ID_STACKINIT:
1898 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1899 ws->l_state.vertical_mwin = 1;
1900 ws->l_state.vertical_stacks = 1;
1902 case SWM_ARG_ID_MASTERSHRINK:
1903 if (ws->l_state.vertical_msize > 1)
1904 ws->l_state.vertical_msize--;
1906 case SWM_ARG_ID_MASTERGROW:
1907 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1908 ws->l_state.vertical_msize++;
1910 case SWM_ARG_ID_MASTERADD:
1911 ws->l_state.vertical_mwin++;
1913 case SWM_ARG_ID_MASTERDEL:
1914 if (ws->l_state.vertical_mwin > 0)
1915 ws->l_state.vertical_mwin--;
1917 case SWM_ARG_ID_STACKINC:
1918 ws->l_state.vertical_stacks++;
1920 case SWM_ARG_ID_STACKDEC:
1921 if (ws->l_state.vertical_stacks > 1)
1922 ws->l_state.vertical_stacks--;
1930 vertical_stack(struct workspace *ws, struct swm_geometry *g)
1932 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1934 stack_master(ws, g, 0, 0);
1938 horizontal_config(struct workspace *ws, int id)
1940 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1943 case SWM_ARG_ID_STACKRESET:
1944 case SWM_ARG_ID_STACKINIT:
1945 ws->l_state.horizontal_mwin = 1;
1946 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1947 ws->l_state.horizontal_stacks = 1;
1949 case SWM_ARG_ID_MASTERSHRINK:
1950 if (ws->l_state.horizontal_msize > 1)
1951 ws->l_state.horizontal_msize--;
1953 case SWM_ARG_ID_MASTERGROW:
1954 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1955 ws->l_state.horizontal_msize++;
1957 case SWM_ARG_ID_MASTERADD:
1958 ws->l_state.horizontal_mwin++;
1960 case SWM_ARG_ID_MASTERDEL:
1961 if (ws->l_state.horizontal_mwin > 0)
1962 ws->l_state.horizontal_mwin--;
1964 case SWM_ARG_ID_STACKINC:
1965 ws->l_state.horizontal_stacks++;
1967 case SWM_ARG_ID_STACKDEC:
1968 if (ws->l_state.horizontal_stacks > 1)
1969 ws->l_state.horizontal_stacks--;
1977 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
1979 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1981 stack_master(ws, g, 1, 0);
1984 /* fullscreen view */
1986 max_stack(struct workspace *ws, struct swm_geometry *g)
1989 struct swm_geometry gg = *g;
1990 struct ws_win *win, *wintrans = NULL;
1994 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1999 winno = count_win(ws, 0);
2000 if (winno == 0 && count_win(ws, 1) == 0)
2003 TAILQ_FOREACH(win, &ws->winlist, entry) {
2004 if (win->transient != 0) {
2007 bzero(&wc, sizeof wc);
2008 wc.border_width = 1;
2009 win->g.x = wc.x = gg.x;
2010 win->g.y = wc.y = gg.y;
2011 win->g.w = wc.width = gg.w;
2012 win->g.h = wc.height = gg.h;
2013 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2014 XConfigureWindow(display, win->id, mask, &wc);
2016 /* unmap only if we don't have multi screen */
2017 if (win != ws->focus)
2018 if (!(ScreenCount(display) > 1 || outputs > 1))
2023 /* put the last transient on top */
2025 stack_floater(wintrans, ws->r);
2026 focus_win(wintrans); /* override */
2031 send_to_ws(struct swm_region *r, union arg *args)
2033 int wsid = args->id;
2034 struct ws_win *win = win, *winfocus = NULL;
2035 struct workspace *ws, *nws;
2036 Atom ws_idx_atom = 0;
2037 unsigned char ws_idx_str[SWM_PROPLEN];
2046 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2049 nws = &win->s->ws[wsid];
2051 /* find a window to focus */
2052 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2053 if (TAILQ_FIRST(&ws->winlist) == win)
2054 winfocus = TAILQ_NEXT(win, entry);
2056 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2057 if (winfocus == NULL)
2058 winfocus = TAILQ_LAST(&ws->winlist, ws_win_list);
2060 /* out of windows in ws so focus on nws instead if we multi screen */
2061 if (winfocus == NULL)
2062 if (ScreenCount(display) > 1 || outputs > 1)
2066 TAILQ_REMOVE(&ws->winlist, win, entry);
2067 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2070 /* Try to update the window's workspace property */
2071 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2073 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2074 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2076 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2077 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2080 if (count_win(nws, 1) == 1)
2087 focus_win(winfocus);
2091 wkill(struct swm_region *r, union arg *args)
2093 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
2095 if(r->ws->focus == NULL)
2098 if (args->id == SWM_ARG_ID_KILLWINDOW)
2099 XKillClient(display, r->ws->focus->id);
2101 if (r->ws->focus->can_delete)
2102 client_msg(r->ws->focus, adelete);
2106 floating_toggle(struct swm_region *r, union arg *args)
2108 struct ws_win *win = r->ws->focus;
2113 win->floating = !win->floating;
2120 resize_window(struct ws_win *win, int center)
2124 struct swm_region *r;
2126 r = root_to_region(win->wa.root);
2127 bzero(&wc, sizeof wc);
2128 mask = CWBorderWidth | CWWidth | CWHeight;
2129 wc.border_width = 1;
2130 wc.width = win->g.w;
2131 wc.height = win->g.h;
2132 if (center == SWM_ARG_ID_CENTER) {
2133 wc.x = (WIDTH(r) - win->g.w) / 2;
2134 wc.y = (HEIGHT(r) - win->g.h) / 2;
2138 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
2139 win->id, wc.x, wc.y, wc.width, wc.height);
2141 XConfigureWindow(display, win->id, mask, &wc);
2146 resize(struct ws_win *win, union arg *args)
2151 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %d\n",
2152 win->id, win->floating, win->transient);
2154 if (!(win->transient != 0 || win->floating != 0))
2157 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2158 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2160 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w, win->g.h);
2162 XMaskEvent(display, MOUSEMASK | ExposureMask |
2163 SubstructureRedirectMask, &ev);
2165 case ConfigureRequest:
2168 handler[ev.type](&ev);
2171 if (ev.xmotion.x <= 1)
2173 if (ev.xmotion.y <= 1)
2175 win->g.w = ev.xmotion.x;
2176 win->g.h = ev.xmotion.y;
2178 /* not free, don't sync more than 60 times / second */
2179 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2180 time = ev.xmotion.time;
2181 XSync(display, False);
2182 resize_window(win, args->id);
2186 } while (ev.type != ButtonRelease);
2188 XSync(display, False);
2189 resize_window(win, args->id);
2191 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
2193 XUngrabPointer(display, CurrentTime);
2196 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2200 move_window(struct ws_win *win)
2204 struct swm_region *r;
2206 r = root_to_region(win->wa.root);
2207 bzero(&wc, sizeof wc);
2212 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
2213 win->id, wc.x, wc.y, wc.width, wc.height);
2215 XConfigureWindow(display, win->id, mask, &wc);
2220 move(struct ws_win *win, union arg *args)
2226 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %d\n",
2227 win->id, win->floating, win->transient);
2229 if (win->floating == 0) {
2235 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2236 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2238 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2240 XMaskEvent(display, MOUSEMASK | ExposureMask |
2241 SubstructureRedirectMask, &ev);
2243 case ConfigureRequest:
2246 handler[ev.type](&ev);
2249 win->g.x = ev.xmotion.x_root;
2250 win->g.y = ev.xmotion.y_root;
2252 /* not free, don't sync more than 60 times / second */
2253 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2254 time = ev.xmotion.time;
2255 XSync(display, False);
2260 } while (ev.type != ButtonRelease);
2262 XSync(display, False);
2265 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2266 XUngrabPointer(display, CurrentTime);
2271 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2274 /* key definitions */
2276 dummykeyfunc(struct swm_region *r, union arg *args)
2281 legacyfunc(struct swm_region *r, union arg *args)
2286 char name[SWM_FUNCNAME_LEN];
2287 void (*func)(struct swm_region *r, union arg *);
2289 } keyfuncs[kf_invalid + 1] = {
2290 /* name function argument */
2291 { "cycle_layout", cycle_layout, {0} },
2292 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
2293 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
2294 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
2295 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
2296 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
2297 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
2298 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
2299 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
2300 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
2301 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
2302 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
2303 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
2304 { "spawn_term", spawnterm, {.argv = spawn_term} },
2305 { "spawn_menu", legacyfunc, {0} },
2306 { "quit", quit, {0} },
2307 { "restart", restart, {0} },
2308 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
2309 { "ws_1", switchws, {.id = 0} },
2310 { "ws_2", switchws, {.id = 1} },
2311 { "ws_3", switchws, {.id = 2} },
2312 { "ws_4", switchws, {.id = 3} },
2313 { "ws_5", switchws, {.id = 4} },
2314 { "ws_6", switchws, {.id = 5} },
2315 { "ws_7", switchws, {.id = 6} },
2316 { "ws_8", switchws, {.id = 7} },
2317 { "ws_9", switchws, {.id = 8} },
2318 { "ws_10", switchws, {.id = 9} },
2319 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
2320 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
2321 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
2322 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
2323 { "mvws_1", send_to_ws, {.id = 0} },
2324 { "mvws_2", send_to_ws, {.id = 1} },
2325 { "mvws_3", send_to_ws, {.id = 2} },
2326 { "mvws_4", send_to_ws, {.id = 3} },
2327 { "mvws_5", send_to_ws, {.id = 4} },
2328 { "mvws_6", send_to_ws, {.id = 5} },
2329 { "mvws_7", send_to_ws, {.id = 6} },
2330 { "mvws_8", send_to_ws, {.id = 7} },
2331 { "mvws_9", send_to_ws, {.id = 8} },
2332 { "mvws_10", send_to_ws, {.id = 9} },
2333 { "bar_toggle", bar_toggle, {0} },
2334 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
2335 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
2336 { "screenshot_all", legacyfunc, {0} },
2337 { "screenshot_wind", legacyfunc, {0} },
2338 { "float_toggle", floating_toggle,{0} },
2339 { "version", version, {0} },
2340 { "spawn_lock", legacyfunc, {0} },
2341 { "spawn_initscr", legacyfunc, {0} },
2342 { "spawn_custom", dummykeyfunc, {0} },
2343 { "invalid key func", NULL, {0} },
2348 enum keyfuncid funcid;
2351 int keys_size = 0, keys_length = 0;
2352 struct key *keys = NULL;
2355 enum { client_click, root_click };
2357 unsigned int action;
2359 unsigned int button;
2360 void (*func)(struct ws_win *, union arg *);
2363 /* action key mouse button func args */
2364 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
2365 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
2366 { client_click, MODKEY, Button1, move, {0} },
2370 update_modkey(unsigned int mod)
2375 for (i = 0; i < keys_length; i++)
2376 if (keys[i].mod & ShiftMask)
2377 keys[i].mod = mod | ShiftMask;
2381 for (i = 0; i < LENGTH(buttons); i++)
2382 if (buttons[i].mask & ShiftMask)
2383 buttons[i].mask = mod | ShiftMask;
2385 buttons[i].mask = mod;
2395 int spawns_size = 0, spawns_length = 0;
2396 struct spawn_prog *spawns = NULL;
2399 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
2402 struct spawn_prog *prog = NULL;
2404 char *ap, **real_args;
2406 DNPRINTF(SWM_D_SPAWN, "spawn_custom %s\n", spawn_name);
2409 for (i = 0; i < spawns_length; i++) {
2410 if (!strcasecmp(spawn_name, spawns[i].name))
2414 fprintf(stderr, "spawn_custom: program %s not found\n",
2419 /* make room for expanded args */
2420 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
2421 err(1, "spawn_custom: calloc real_args");
2423 /* expand spawn_args into real_args */
2424 for (i = 0; i < prog->argc; i++) {
2426 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
2427 if (!strcasecmp(ap, "$bar_border")) {
2429 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
2431 err(1, "spawn_custom border color");
2432 } else if (!strcasecmp(ap, "$bar_color")) {
2434 strdup(r->s->c[SWM_S_COLOR_BAR].name))
2436 err(1, "spawn_custom bar color");
2437 } else if (!strcasecmp(ap, "$bar_font")) {
2438 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
2440 err(1, "spawn_custom bar fonts");
2441 } else if (!strcasecmp(ap, "$bar_font_color")) {
2443 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
2445 err(1, "spawn_custom color font");
2446 } else if (!strcasecmp(ap, "$color_focus")) {
2448 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
2450 err(1, "spawn_custom color focus");
2451 } else if (!strcasecmp(ap, "$color_unfocus")) {
2453 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
2455 err(1, "spawn_custom color unfocus");
2457 /* no match --> copy as is */
2458 if ((real_args[i] = strdup(ap)) == NULL)
2459 err(1, "spawn_custom strdup(ap)");
2461 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
2466 if ((swm_debug & SWM_D_SPAWN) != 0) {
2467 fprintf(stderr, "spawn_custom: result = ");
2468 for (i = 0; i < prog->argc; i++)
2469 fprintf(stderr, "\"%s\" ", real_args[i]);
2470 fprintf(stderr, "\n");
2476 for (i = 0; i < prog->argc; i++)
2482 setspawn(struct spawn_prog *prog)
2486 if (prog == NULL || prog->name == NULL)
2490 for (i = 0; i < spawns_length; i++) {
2491 if (!strcmp(spawns[i].name, prog->name)) {
2493 if (prog->argv == NULL) {
2495 DNPRINTF(SWM_D_SPAWN,
2496 "setspawn: delete #%d %s\n",
2498 free(spawns[i].name);
2499 for (j = 0; j < spawns[i].argc; j++)
2500 free(spawns[i].argv[j]);
2501 free(spawns[i].argv);
2502 j = spawns_length - 1;
2504 spawns[i] = spawns[j];
2509 DNPRINTF(SWM_D_SPAWN,
2510 "setspawn: replace #%d %s\n",
2512 free(spawns[i].name);
2513 for (j = 0; j < spawns[i].argc; j++)
2514 free(spawns[i].argv[j]);
2515 free(spawns[i].argv);
2518 /* found case handled */
2524 if (prog->argv == NULL) {
2526 "error: setspawn: cannot find program %s", prog->name);
2531 /* not found: add */
2532 if (spawns_size == 0 || spawns == NULL) {
2534 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
2535 spawns = malloc((size_t)spawns_size *
2536 sizeof(struct spawn_prog));
2537 if (spawns == NULL) {
2538 fprintf(stderr, "setspawn: malloc failed\n");
2542 } else if (spawns_length == spawns_size) {
2544 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
2545 spawns = realloc(spawns, (size_t)spawns_size *
2546 sizeof(struct spawn_prog));
2547 if (spawns == NULL) {
2548 fprintf(stderr, "setspawn: realloc failed\n");
2554 if (spawns_length < spawns_size) {
2555 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
2556 spawns_length, prog->name);
2557 i = spawns_length++;
2560 fprintf(stderr, "spawns array problem?\n");
2561 if (spawns == NULL) {
2562 fprintf(stderr, "spawns array is NULL!\n");
2570 setconfspawn(char *selector, char *value, int flags)
2572 struct spawn_prog *prog;
2573 char *vp, *cp, *word;
2575 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
2576 if ((prog = calloc(1, sizeof *prog)) == NULL)
2577 err(1, "setconfspawn: calloc prog");
2578 prog->name = strdup(selector);
2579 if (prog->name == NULL)
2580 err(1, "setconfspawn prog->name");
2581 if ((cp = vp = strdup(value)) == NULL)
2582 err(1, "setconfspawn: strdup(value) ");
2583 while ((word = strsep(&cp, " \t")) != NULL) {
2584 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
2586 cp += (long)strspn(cp, " \t");
2587 if (strlen(word) > 0) {
2589 prog->argv = realloc(prog->argv,
2590 prog->argc * sizeof(char *));
2591 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
2592 err(1, "setconfspawn: strdup");
2599 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
2606 setconfspawn("term", "xterm", 0);
2607 setconfspawn("screenshot_all", "screenshot.sh full", 0);
2608 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
2609 setconfspawn("lock", "xlock", 0);
2610 setconfspawn("initscr", "initscreen.sh", 0);
2611 setconfspawn("menu", "dmenu_run"
2614 " -nf $bar_font_color"
2616 " -sf $bar_color", 0);
2620 #define SWM_MODNAME_SIZE 32
2621 #define SWM_KEY_WS "\n+ \t"
2623 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
2627 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
2628 if (mod == NULL || ks == NULL) {
2629 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
2632 if (keystr == NULL || strlen(keystr) == 0) {
2633 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
2639 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
2640 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
2642 cp += (long)strspn(cp, SWM_KEY_WS);
2643 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
2645 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
2647 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
2649 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
2651 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
2653 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
2655 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
2656 *mod |= ControlMask;
2658 *ks = XStringToKeysym(name);
2659 XConvertCase(*ks, ks, &uks);
2660 if (ks == NoSymbol) {
2662 "parsekeys: invalid key %s\n",
2668 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
2673 strdupsafe(char *str)
2678 return (strdup(str));
2682 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
2685 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
2686 keyfuncs[kfid].name, spawn_name);
2688 for (i = 0; i < keys_length; i++) {
2689 if (keys[i].mod == mod && keys[i].keysym == ks) {
2690 if (kfid == kf_invalid) {
2693 "setkeybinding: delete #%d %s\n",
2694 i, keyfuncs[keys[i].funcid].name);
2695 free(keys[i].spawn_name);
2696 j = keys_length - 1;
2700 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2703 /* found: replace */
2705 "setkeybinding: replace #%d %s %s\n",
2706 i, keyfuncs[keys[i].funcid].name,
2708 free(keys[i].spawn_name);
2710 keys[i].keysym = ks;
2711 keys[i].funcid = kfid;
2712 keys[i].spawn_name = strdupsafe(spawn_name);
2713 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2718 if (kfid == kf_invalid) {
2720 "error: setkeybinding: cannot find mod/key combination");
2721 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2724 /* not found: add */
2725 if (keys_size == 0 || keys == NULL) {
2727 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
2728 keys = malloc((size_t)keys_size * sizeof(struct key));
2730 fprintf(stderr, "malloc failed\n");
2734 } else if (keys_length == keys_size) {
2736 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
2737 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
2739 fprintf(stderr, "realloc failed\n");
2744 if (keys_length < keys_size) {
2746 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
2747 j, keyfuncs[kfid].name, spawn_name);
2749 keys[j].keysym = ks;
2750 keys[j].funcid = kfid;
2751 keys[j].spawn_name = strdupsafe(spawn_name);
2753 fprintf(stderr, "keys array problem?\n");
2755 fprintf(stderr, "keys array problem\n");
2759 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2763 setconfbinding(char *selector, char *value, int flags)
2765 enum keyfuncid kfid;
2769 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
2770 if (selector == NULL) {
2771 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
2772 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
2774 setkeybinding(mod, ks, kfid, NULL);
2779 /* search by key function name */
2780 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
2781 if (strncasecmp(selector, keyfuncs[kfid].name,
2782 SWM_FUNCNAME_LEN) == 0) {
2783 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
2785 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
2786 setkeybinding(mod, ks, kfid, NULL);
2792 /* search by custom spawn name */
2793 for (i = 0; i < spawns_length; i++) {
2794 if (strcasecmp(selector, spawns[i].name) == 0) {
2795 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
2797 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
2798 setkeybinding(mod, ks, kf_spawn_custom,
2805 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
2812 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
2813 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
2814 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
2815 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
2816 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
2817 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
2818 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
2819 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
2820 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
2821 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
2822 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
2823 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
2824 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
2825 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
2826 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
2827 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
2828 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
2829 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
2830 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
2831 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
2832 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
2833 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
2834 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
2835 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
2836 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
2837 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
2838 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
2839 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
2840 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
2841 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
2842 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
2843 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
2844 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
2845 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
2846 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
2847 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
2848 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
2849 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
2850 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
2851 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
2852 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
2853 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
2854 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
2855 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
2856 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
2857 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
2858 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
2859 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
2860 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
2861 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
2862 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
2863 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
2864 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
2868 updatenumlockmask(void)
2871 XModifierKeymap *modmap;
2873 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
2875 modmap = XGetModifierMapping(display);
2876 for (i = 0; i < 8; i++)
2877 for (j = 0; j < modmap->max_keypermod; j++)
2878 if (modmap->modifiermap[i * modmap->max_keypermod + j]
2879 == XKeysymToKeycode(display, XK_Num_Lock))
2880 numlockmask = (1 << i);
2882 XFreeModifiermap(modmap);
2888 unsigned int i, j, k;
2890 unsigned int modifiers[] =
2891 { 0, LockMask, numlockmask, numlockmask | LockMask };
2893 DNPRINTF(SWM_D_MISC, "grabkeys\n");
2894 updatenumlockmask();
2896 for (k = 0; k < ScreenCount(display); k++) {
2897 if (TAILQ_EMPTY(&screens[k].rl))
2899 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
2900 for (i = 0; i < keys_length; i++) {
2901 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
2902 for (j = 0; j < LENGTH(modifiers); j++)
2903 XGrabKey(display, code,
2904 keys[i].mod | modifiers[j],
2905 screens[k].root, True,
2906 GrabModeAsync, GrabModeAsync);
2912 grabbuttons(struct ws_win *win, int focused)
2915 unsigned int modifiers[] =
2916 { 0, LockMask, numlockmask, numlockmask|LockMask };
2918 updatenumlockmask();
2919 XUngrabButton(display, AnyButton, AnyModifier, win->id);
2921 for (i = 0; i < LENGTH(buttons); i++)
2922 if (buttons[i].action == client_click)
2923 for (j = 0; j < LENGTH(modifiers); j++)
2924 XGrabButton(display, buttons[i].button,
2925 buttons[i].mask | modifiers[j],
2926 win->id, False, BUTTONMASK,
2927 GrabModeAsync, GrabModeSync, None,
2930 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
2931 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
2934 const char *quirkname[] = {
2935 "NONE", /* config string for "no value" */
2943 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
2944 #define SWM_Q_WS "\n|+ \t"
2946 parsequirks(char *qstr, unsigned long *quirk)
2956 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
2958 cp += (long)strspn(cp, SWM_Q_WS);
2959 for (i = 0; i < LENGTH(quirkname); i++) {
2960 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
2961 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
2966 *quirk |= 1 << (i-1);
2970 if (i >= LENGTH(quirkname)) {
2971 DNPRINTF(SWM_D_QUIRK,
2972 "parsequirks: invalid quirk [%s]\n", name);
2980 setquirk(const char *class, const char *name, const int quirk)
2985 for (i = 0; i < quirks_length; i++) {
2986 if (!strcmp(quirks[i].class, class) &&
2987 !strcmp(quirks[i].name, name)) {
2990 DNPRINTF(SWM_D_QUIRK,
2991 "setquirk: delete #%d %s:%s\n",
2992 i, quirks[i].class, quirks[i].name);
2993 free(quirks[i].class);
2994 free(quirks[i].name);
2995 j = quirks_length - 1;
2997 quirks[i] = quirks[j];
3001 /* found: replace */
3002 DNPRINTF(SWM_D_QUIRK,
3003 "setquirk: replace #%d %s:%s\n",
3004 i, quirks[i].class, quirks[i].name);
3005 free(quirks[i].class);
3006 free(quirks[i].name);
3007 quirks[i].class = strdup(class);
3008 quirks[i].name = strdup(name);
3009 quirks[i].quirk = quirk;
3016 "error: setquirk: cannot find class/name combination");
3019 /* not found: add */
3020 if (quirks_size == 0 || quirks == NULL) {
3022 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
3023 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
3025 fprintf(stderr, "setquirk: malloc failed\n");
3029 } else if (quirks_length == quirks_size) {
3031 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
3032 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
3034 fprintf(stderr, "setquirk: realloc failed\n");
3039 if (quirks_length < quirks_size) {
3040 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
3041 j = quirks_length++;
3042 quirks[j].class = strdup(class);
3043 quirks[j].name = strdup(name);
3044 quirks[j].quirk = quirk;
3046 fprintf(stderr, "quirks array problem?\n");
3048 fprintf(stderr, "quirks array problem!\n");
3055 setconfquirk(char *selector, char *value, int flags)
3057 char *cp, *class, *name;
3059 unsigned long quirks;
3060 if (selector == NULL)
3062 if ((cp = strchr(selector, ':')) == NULL)
3067 if ((retval = parsequirks(value, &quirks)) == 0)
3068 setquirk(class, name, quirks);
3075 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
3076 setquirk("OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT);
3077 setquirk("OpenOffice.org 3.0", "VCLSalFrame", SWM_Q_FLOAT);
3078 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
3079 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
3080 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3081 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
3082 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3083 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3084 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3085 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3086 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
3087 setquirk("pcb", "pcb", SWM_Q_FLOAT);
3090 /* conf file stuff */
3091 #define SWM_CONF_FILE "scrotwm.conf"
3093 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_STACK_ENABLED,
3094 SWM_S_CLOCK_ENABLED, SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE,
3095 SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
3096 SWM_S_TITLE_NAME_ENABLED, SWM_S_BAR_FONT, SWM_S_BAR_ACTION,
3097 SWM_S_SPAWN_TERM, SWM_S_SS_APP, SWM_S_DIALOG_RATIO };
3100 setconfvalue(char *selector, char *value, int flags)
3103 case SWM_S_BAR_DELAY:
3104 bar_delay = atoi(value);
3106 case SWM_S_BAR_ENABLED:
3107 bar_enabled = atoi(value);
3109 case SWM_S_STACK_ENABLED:
3110 stack_enabled = atoi(value);
3112 case SWM_S_CLOCK_ENABLED:
3113 clock_enabled = atoi(value);
3115 case SWM_S_CYCLE_EMPTY:
3116 cycle_empty = atoi(value);
3118 case SWM_S_CYCLE_VISIBLE:
3119 cycle_visible = atoi(value);
3121 case SWM_S_SS_ENABLED:
3122 ss_enabled = atoi(value);
3124 case SWM_S_TERM_WIDTH:
3125 term_width = atoi(value);
3127 case SWM_S_TITLE_CLASS_ENABLED:
3128 title_class_enabled = atoi(value);
3130 case SWM_S_TITLE_NAME_ENABLED:
3131 title_name_enabled = atoi(value);
3133 case SWM_S_BAR_FONT:
3135 if ((bar_fonts[0] = strdup(value)) == NULL)
3136 err(1, "setconfvalue: bar_font");
3138 case SWM_S_BAR_ACTION:
3140 if ((bar_argv[0] = strdup(value)) == NULL)
3141 err(1, "setconfvalue: bar_action");
3143 case SWM_S_SPAWN_TERM:
3144 free(spawn_term[0]);
3145 if ((spawn_term[0] = strdup(value)) == NULL)
3146 err(1, "setconfvalue: spawn_term");
3150 case SWM_S_DIALOG_RATIO:
3151 dialog_ratio = atof(value);
3152 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
3162 setconfmodkey(char *selector, char *value, int flags)
3164 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
3165 update_modkey(Mod1Mask);
3166 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
3167 update_modkey(Mod2Mask);
3168 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
3169 update_modkey(Mod3Mask);
3170 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
3171 update_modkey(Mod4Mask);
3178 setconfcolor(char *selector, char *value, int flags)
3180 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
3185 setconfregion(char *selector, char *value, int flags)
3187 custom_region(value);
3191 /* config options */
3192 struct config_option {
3194 int (*func)(char*, char*, int);
3197 struct config_option configopt[] = {
3198 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
3199 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
3200 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
3201 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
3202 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
3203 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
3204 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
3205 { "bind", setconfbinding, 0 },
3206 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
3207 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
3208 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
3209 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
3210 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
3211 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
3212 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
3213 { "modkey", setconfmodkey, 0 },
3214 { "program", setconfspawn, 0 },
3215 { "quirk", setconfquirk, 0 },
3216 { "region", setconfregion, 0 },
3217 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
3218 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
3219 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
3220 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
3221 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
3222 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED }
3227 conf_load(char *filename)
3230 char *line, *cp, *optsub, *optval;
3231 size_t linelen, lineno = 0;
3232 int wordlen, i, optind;
3233 struct config_option *opt;
3235 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
3237 if (filename == NULL) {
3238 fprintf(stderr, "conf_load: no filename\n");
3241 if ((config = fopen(filename, "r")) == NULL) {
3242 warn("conf_load: fopen");
3246 while (!feof(config)) {
3247 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
3250 err(1, "%s", filename);
3255 cp += strspn(cp, " \t\n"); /* eat whitespace */
3256 if (cp[0] == '\0') {
3261 /* get config option */
3262 wordlen = strcspn(cp, "=[ \t\n");
3264 warnx("%s: line %zd: no option found",
3269 for (i = 0; i < LENGTH(configopt); i++) {
3270 opt = &configopt[i];
3271 if (!strncasecmp(cp, opt->optname, wordlen) &&
3272 strlen(opt->optname) == wordlen) {
3278 warnx("%s: line %zd: unknown option %.*s",
3279 filename, lineno, wordlen, cp);
3283 cp += strspn(cp, " \t\n"); /* eat whitespace */
3284 /* get [selector] if any */
3288 wordlen = strcspn(cp, "]");
3291 warnx("%s: line %zd: syntax error",
3295 asprintf(&optsub, "%.*s", wordlen, cp);
3298 cp += strspn(cp, "] \t\n"); /* eat trailing */
3300 cp += strspn(cp, "= \t\n"); /* eat trailing */
3302 optval = strdup(cp);
3303 /* call function to deal with it all */
3304 if (configopt[optind].func(optsub, optval,
3305 configopt[optind].funcflags) != 0) {
3306 fprintf(stderr, "%s line %zd: %s\n",
3307 filename, lineno, line);
3308 errx(1, "%s: line %zd: invalid data for %s",
3309 filename, lineno, configopt[optind].optname);
3317 DNPRINTF(SWM_D_CONF, "conf_load end\n");
3323 manage_window(Window id)
3326 struct workspace *ws;
3327 struct ws_win *win, *ww;
3328 int format, i, ws_idx, n, border_me = 0;
3329 unsigned long nitems, bytes;
3330 Atom ws_idx_atom = 0, type;
3331 Atom *prot = NULL, *pp;
3332 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
3333 struct swm_region *r;
3338 if ((win = find_window(id)) != NULL)
3339 return (win); /* already being managed */
3341 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
3342 errx(1, "calloc: failed to allocate memory for new window");
3344 /* Get all the window data in one shot */
3345 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3347 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
3348 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
3349 XGetWindowAttributes(display, id, &win->wa);
3350 XGetWMNormalHints(display, id, &win->sh, &mask);
3351 XGetTransientForHint(display, id, &trans);
3353 win->transient = trans;
3354 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
3355 (unsigned)win->id, win->transient);
3357 /* get supported protocols */
3358 if (XGetWMProtocols(display, id, &prot, &n)) {
3359 for (i = 0, pp = prot; i < n; i++, pp++)
3361 win->can_delete = 1;
3367 * Figure out where to put the window. If it was previously assigned to
3368 * a workspace (either by spawn() or manually moving), and isn't
3369 * transient, * put it in the same workspace
3371 r = root_to_region(win->wa.root);
3372 if (prop && win->transient == 0) {
3373 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
3374 ws_idx = strtonum(prop, 0, 9, &errstr);
3376 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
3379 ws = &r->s->ws[ws_idx];
3382 /* this should launch transients in the same ws as parent */
3384 if ((ww = find_window(trans)) != NULL)
3391 "fix this bug mcbride\n");
3396 /* set up the window layout */
3399 win->s = r->s; /* this never changes */
3400 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
3402 win->g.w = win->wa.width;
3403 win->g.h = win->wa.height;
3404 win->g.x = win->wa.x;
3405 win->g.y = win->wa.y;
3407 /* Set window properties so we can remember this after reincarnation */
3408 if (ws_idx_atom && prop == NULL &&
3409 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
3410 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3412 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3413 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3417 if (XGetClassHint(display, win->id, &win->ch)) {
3418 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
3419 win->ch.res_class, win->ch.res_name);
3421 /* java is retarded so treat it special */
3422 if (strstr(win->ch.res_name, "sun-awt"))
3425 for (i = 0; i < quirks_length; i++){
3426 if (!strcmp(win->ch.res_class, quirks[i].class) &&
3427 !strcmp(win->ch.res_name, quirks[i].name)) {
3428 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
3429 win->ch.res_class, win->ch.res_name);
3430 if (quirks[i].quirk & SWM_Q_FLOAT)
3432 win->quirks = quirks[i].quirk;
3437 /* alter window position if quirky */
3438 if (win->quirks & SWM_Q_ANYWHERE) {
3439 win->manual = 1; /* don't center the quirky windows */
3440 bzero(&wc, sizeof wc);
3442 if (win->g.y < bar_height) {
3443 win->g.y = wc.y = bar_height;
3446 if (win->g.w + win->g.x > WIDTH(r)) {
3447 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
3453 /* Reset font sizes (the bruteforce way; no default keybinding). */
3454 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
3455 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3456 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3457 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3458 fake_keypress(win, XK_KP_Add, ShiftMask);
3463 bzero(&wc, sizeof wc);
3464 wc.border_width = 1;
3465 mask = CWBorderWidth;
3466 XConfigureWindow(display, win->id, mask, &wc);
3469 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
3470 PropertyChangeMask | StructureNotifyMask);
3472 set_win_state(win, NormalState);
3474 /* floaters need to be mapped if they are in the current workspace */
3475 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
3476 XMapRaised(display, win->id);
3482 unmanage_window(struct ws_win *win)
3484 struct workspace *ws;
3489 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
3492 TAILQ_REMOVE(&win->ws->winlist, win, entry);
3493 if (win->ch.res_class)
3494 XFree(win->ch.res_class);
3495 if (win->ch.res_name)
3496 XFree(win->ch.res_name);
3503 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
3511 XKeyEvent *ev = &e->xkey;
3513 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
3515 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
3516 for (i = 0; i < keys_length; i++)
3517 if (keysym == keys[i].keysym
3518 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
3519 && keyfuncs[keys[i].funcid].func) {
3520 if (keys[i].funcid == kf_spawn_custom)
3522 root_to_region(ev->root),
3523 &(keyfuncs[keys[i].funcid].args),
3527 keyfuncs[keys[i].funcid].func(
3528 root_to_region(ev->root),
3529 &(keyfuncs[keys[i].funcid].args)
3535 buttonpress(XEvent *e)
3537 XButtonPressedEvent *ev = &e->xbutton;
3542 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
3544 action = root_click;
3545 if ((win = find_window(ev->window)) == NULL)
3549 action = client_click;
3552 for (i = 0; i < LENGTH(buttons); i++)
3553 if (action == buttons[i].action && buttons[i].func &&
3554 buttons[i].button == ev->button &&
3555 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
3556 buttons[i].func(win, &buttons[i].args);
3560 configurerequest(XEvent *e)
3562 XConfigureRequestEvent *ev = &e->xconfigurerequest;
3567 if ((win = find_window(ev->window)) == NULL)
3571 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
3573 bzero(&wc, sizeof wc);
3576 wc.width = ev->width;
3577 wc.height = ev->height;
3578 wc.border_width = ev->border_width;
3579 wc.sibling = ev->above;
3580 wc.stack_mode = ev->detail;
3581 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
3583 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
3585 if (win->floating) {
3586 if (ev->value_mask & CWX)
3588 if (ev->value_mask & CWY)
3590 if (ev->value_mask & CWWidth)
3591 win->g.w = ev->width;
3592 if (ev->value_mask & CWHeight)
3593 win->g.h = ev->height;
3594 if (win->ws->r != NULL) {
3595 /* this seems to be full screen */
3596 if (win->g.w >= WIDTH(win->ws->r)) {
3598 win->g.w = WIDTH(win->ws->r);
3599 ev->value_mask |= CWX | CWWidth;
3601 if (win->g.h >= HEIGHT(win->ws->r)) {
3604 win->g.h = HEIGHT(win->ws->r);
3605 ev->value_mask |= CWY | CWHeight;
3608 if ((ev->value_mask & (CWX | CWY)) &&
3609 !(ev->value_mask & (CWWidth | CWHeight)))
3611 XMoveResizeWindow(display, win->id,
3612 win->g.x, win->g.y, win->g.w, win->g.h);
3619 configurenotify(XEvent *e)
3624 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
3625 e->xconfigure.window);
3627 XMapWindow(display, e->xconfigure.window);
3628 win = find_window(e->xconfigure.window);
3630 XGetWMNormalHints(display, win->id, &win->sh, &mask);
3632 XMapWindow(display, win->id);
3639 destroynotify(XEvent *e)
3641 struct ws_win *win, *winfocus = NULL;
3642 struct workspace *ws;
3643 struct ws_win_list *wl;
3644 XDestroyWindowEvent *ev = &e->xdestroywindow;
3646 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
3648 if ((win = find_window(ev->window)) != NULL) {
3649 /* find a window to focus */
3653 /* if we are transient give focus to parent */
3655 winfocus = find_window(win->transient);
3656 else if (ws->focus == win) {
3657 /* if in max_stack try harder */
3658 if (ws->cur_layout->flags & SWM_L_FOCUSPREV) {
3659 if (win != ws->focus_prev)
3660 winfocus = ws->focus_prev;
3661 else if (win != ws->focus)
3662 winfocus = ws->focus;
3665 /* fallback and normal handling */
3666 if (winfocus == NULL) {
3667 if (TAILQ_FIRST(wl) == win)
3668 winfocus = TAILQ_NEXT(win, entry);
3670 winfocus = TAILQ_PREV(ws->focus,
3671 ws_win_list, entry);
3672 if (winfocus == NULL)
3673 winfocus = TAILQ_LAST(wl,
3678 unmanage_window(win);
3683 focus_win(winfocus);
3689 enternotify(XEvent *e)
3691 XCrossingEvent *ev = &e->xcrossing;
3694 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
3697 /* eat event(r) to prevent autofocus */
3702 * happens when a window is created or destroyed and the border
3703 * crosses the mouse pointer
3705 if (QLength(display))
3708 if ((win = find_window(ev->window)) != NULL) {
3709 if (win->ws->focus == win)
3718 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
3724 DNPRINTF(SWM_D_EVENT, "focusout: window: %lu\n", e->xfocus.window);
3728 mapnotify(XEvent *e)
3731 XMapEvent *ev = &e->xmap;
3733 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
3735 win = find_window(ev->window);
3737 set_win_state(win, NormalState);
3741 mappingnotify(XEvent *e)
3743 XMappingEvent *ev = &e->xmapping;
3745 XRefreshKeyboardMapping(ev);
3746 if (ev->request == MappingKeyboard)
3751 maprequest(XEvent *e)
3754 struct swm_region *r;
3756 XMapRequestEvent *ev = &e->xmaprequest;
3757 XWindowAttributes wa;
3759 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
3760 e->xmaprequest.window);
3762 if (!XGetWindowAttributes(display, ev->window, &wa))
3764 if (wa.override_redirect)
3767 win = manage_window(e->xmaprequest.window);
3769 return; /* can't happen */
3775 /* make new win focused */
3776 r = root_to_region(win->wa.root);
3777 if (win->ws == r->ws)
3782 propertynotify(XEvent *e)
3785 XPropertyEvent *ev = &e->xproperty;
3787 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
3790 if (ev->state == PropertyDelete)
3791 return; /* ignore */
3792 win = find_window(ev->window);
3797 case XA_WM_NORMAL_HINTS:
3800 XGetWMNormalHints(display, win->id, &win->sh, &mask);
3801 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
3802 if (win->sh.flags & PMinSize) {
3803 win->g.w = win->sh.min_width;
3804 win->g.h = win->sh.min_height;
3805 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
3807 XMoveResizeWindow(display, win->id,
3808 win->g.x, win->g.y, win->g.w, win->g.h);
3817 unmapnotify(XEvent *e)
3819 struct ws_win *win, *winfocus = NULL;
3820 struct workspace *ws;
3822 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
3824 /* determine if we need to help unmanage this window */
3825 win = find_window(e->xunmap.window);
3829 /* java can not deal with this heuristic */
3833 if (getstate(e->xunmap.window) == NormalState) {
3835 * this window does not have a destroy event but but it is no
3836 * longer visible due to the app unmapping it so unmanage it
3839 /* if we are max_stack try harder to focus on something */
3840 if (ws->cur_layout->flags & SWM_L_FOCUSPREV) {
3842 winfocus = find_window(win->transient);
3843 else if (win != ws->focus_prev)
3844 winfocus = ws->focus_prev;
3845 else if (win != ws->focus)
3846 winfocus = ws->focus;
3849 /* normal and fallback if haven't found anything to focus on */
3850 if (winfocus == NULL) {
3851 winfocus = TAILQ_PREV(win, ws_win_list, entry);
3852 if (TAILQ_FIRST(&ws->winlist) == win)
3853 winfocus = TAILQ_NEXT(win, entry);
3856 winfocus = TAILQ_PREV(ws->focus,
3857 ws_win_list, entry);
3858 if (winfocus == NULL)
3859 winfocus = TAILQ_LAST(&ws->winlist,
3864 /* trash window and refocus */
3865 unmanage_window(win);
3868 focus_win(winfocus);
3874 visibilitynotify(XEvent *e)
3877 struct swm_region *r;
3879 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
3880 e->xvisibility.window);
3881 if (e->xvisibility.state == VisibilityUnobscured)
3882 for (i = 0; i < ScreenCount(display); i++)
3883 TAILQ_FOREACH(r, &screens[i].rl, entry)
3884 if (e->xvisibility.window == r->bar_window)
3889 xerror_start(Display *d, XErrorEvent *ee)
3896 xerror(Display *d, XErrorEvent *ee)
3898 /* fprintf(stderr, "error: %p %p\n", display, ee); */
3906 xerrorxlib = XSetErrorHandler(xerror_start);
3908 /* this causes an error if some other window manager is running */
3909 XSelectInput(display, DefaultRootWindow(display),
3910 SubstructureRedirectMask);
3911 XSync(display, False);
3915 XSetErrorHandler(xerror);
3916 XSync(display, False);
3921 new_region(struct swm_screen *s, int x, int y, int w, int h)
3923 struct swm_region *r, *n;
3924 struct workspace *ws = NULL;
3927 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
3928 s->idx, w, h, x, y);
3930 /* remove any conflicting regions */
3931 n = TAILQ_FIRST(&s->rl);
3934 n = TAILQ_NEXT(r, entry);
3935 if (X(r) < (x + w) &&
3936 (X(r) + WIDTH(r)) > x &&
3938 (Y(r) + HEIGHT(r)) > y) {
3939 XDestroyWindow(display, r->bar_window);
3940 TAILQ_REMOVE(&s->rl, r, entry);
3941 TAILQ_INSERT_TAIL(&s->orl, r, entry);
3945 /* search old regions for one to reuse */
3947 /* size + location match */
3948 TAILQ_FOREACH(r, &s->orl, entry)
3949 if (X(r) == x && Y(r) == y &&
3950 HEIGHT(r) == h && WIDTH(r) == w)
3954 TAILQ_FOREACH(r, &s->orl, entry)
3955 if (HEIGHT(r) == h && WIDTH(r) == w)
3959 TAILQ_REMOVE(&s->orl, r, entry);
3960 /* try to use old region's workspace */
3961 if (r->ws->r == NULL)
3964 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
3965 errx(1, "calloc: failed to allocate memory for screen");
3967 /* if we don't have a workspace already, find one */
3969 for (i = 0; i < SWM_WS_MAX; i++)
3970 if (s->ws[i].r == NULL) {
3977 errx(1, "no free workspaces\n");
3986 TAILQ_INSERT_TAIL(&s->rl, r, entry);
3992 #ifdef SWM_XRR_HAS_CRTC
3994 XRRScreenResources *sr;
3997 #endif /* SWM_XRR_HAS_CRTC */
3998 struct swm_region *r;
4001 if (i >= ScreenCount(display))
4002 errx(1, "invalid screen");
4004 /* remove any old regions */
4005 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
4006 r->ws->old_r = r->ws->r = NULL;
4007 XDestroyWindow(display, r->bar_window);
4008 TAILQ_REMOVE(&screens[i].rl, r, entry);
4009 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
4012 /* map virtual screens onto physical screens */
4013 #ifdef SWM_XRR_HAS_CRTC
4015 if (xrandr_support) {
4016 sr = XRRGetScreenResources(display, screens[i].root);
4018 new_region(&screens[i], 0, 0,
4019 DisplayWidth(display, i),
4020 DisplayHeight(display, i));
4024 for (c = 0, ci = NULL; c < ncrtc; c++) {
4025 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
4026 if (ci->noutput == 0)
4030 if (ci != NULL && ci->mode == None)
4031 new_region(&screens[i], 0, 0,
4032 DisplayWidth(display, i),
4033 DisplayHeight(display, i));
4035 new_region(&screens[i],
4036 ci->x, ci->y, ci->width, ci->height);
4039 XRRFreeCrtcInfo(ci);
4040 XRRFreeScreenResources(sr);
4042 #endif /* SWM_XRR_HAS_CRTC */
4044 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
4045 DisplayHeight(display, i));
4050 screenchange(XEvent *e) {
4051 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
4052 struct swm_region *r;
4055 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
4057 if (!XRRUpdateConfiguration(e))
4060 /* silly event doesn't include the screen index */
4061 for (i = 0; i < ScreenCount(display); i++)
4062 if (screens[i].root == xe->root)
4064 if (i >= ScreenCount(display))
4065 errx(1, "screenchange: screen not found\n");
4067 /* brute force for now, just re-enumerate the regions */
4070 /* add bars to all regions */
4071 for (i = 0; i < ScreenCount(display); i++)
4072 TAILQ_FOREACH(r, &screens[i].rl, entry)
4080 Window d1, d2, *wins = NULL;
4081 XWindowAttributes wa;
4084 int errorbase, major, minor;
4085 struct workspace *ws;
4089 if ((screens = calloc(ScreenCount(display),
4090 sizeof(struct swm_screen))) == NULL)
4091 errx(1, "calloc: screens");
4093 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4095 /* initial Xrandr setup */
4096 xrandr_support = XRRQueryExtension(display,
4097 &xrandr_eventbase, &errorbase);
4099 if (XRRQueryVersion(display, &major, &minor) && major < 1)
4102 /* map physical screens */
4103 for (i = 0; i < ScreenCount(display); i++) {
4104 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
4106 TAILQ_INIT(&screens[i].rl);
4107 TAILQ_INIT(&screens[i].orl);
4108 screens[i].root = RootWindow(display, i);
4110 /* set default colors */
4111 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
4112 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
4113 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
4114 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
4115 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
4117 /* init all workspaces */
4118 /* XXX these should be dynamically allocated too */
4119 for (j = 0; j < SWM_WS_MAX; j++) {
4120 ws = &screens[i].ws[j];
4126 TAILQ_INIT(&ws->winlist);
4128 for (k = 0; layouts[k].l_stack != NULL; k++)
4129 if (layouts[k].l_config != NULL)
4130 layouts[k].l_config(ws,
4131 SWM_ARG_ID_STACKINIT);
4132 ws->cur_layout = &layouts[0];
4134 /* grab existing windows (before we build the bars)*/
4135 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
4141 XRRSelectInput(display, screens[i].root,
4142 RRScreenChangeNotifyMask);
4144 /* attach windows to a region */
4145 /* normal windows */
4146 for (j = 0; j < no; j++) {
4147 XGetWindowAttributes(display, wins[j], &wa);
4148 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4149 wa.override_redirect ||
4150 XGetTransientForHint(display, wins[j], &d1))
4153 state = getstate(wins[j]);
4154 manage = state == NormalState || state == IconicState;
4155 if (wa.map_state == IsViewable || manage)
4156 manage_window(wins[j]);
4158 /* transient windows */
4159 for (j = 0; j < no; j++) {
4160 if (!XGetWindowAttributes(display, wins[j], &wa))
4163 state = getstate(wins[j]);
4164 manage = state == NormalState || state == IconicState;
4165 if (XGetTransientForHint(display, wins[j], &d1) &&
4167 manage_window(wins[j]);
4179 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
4181 err(1, "setup_globals: strdup");
4182 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
4184 err(1, "setup_globals: strdup");
4185 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
4187 err(1, "setup_globals: strdup");
4188 if ((spawn_term[0] = strdup("xterm")) == NULL)
4189 err(1, "setup_globals: strdup");
4196 Atom netwmcheck, netwmname, utf8_string;
4199 /* work around sun jdk bugs, code from wmname */
4200 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
4201 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
4202 utf8_string = XInternAtom(display, "UTF8_STRING", False);
4203 for (i = 0; i < ScreenCount(display); i++) {
4204 root = screens[i].root;
4205 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
4206 PropModeReplace, (unsigned char *)&root, 1);
4207 XChangeProperty(display, root, netwmname, utf8_string, 8,
4208 PropModeReplace, "LG3D", strlen("LG3D"));
4213 main(int argc, char *argv[])
4216 struct swm_region *r, *rr;
4217 struct ws_win *winfocus = NULL;
4218 char conf[PATH_MAX], *cfile = NULL;
4225 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
4226 SWM_VERSION, cvstag);
4227 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
4228 warnx("no locale support");
4230 if (!(display = XOpenDisplay(0)))
4231 errx(1, "can not open display");
4234 errx(1, "other wm running");
4236 /* handle some signale */
4237 installsignal(SIGINT, "INT");
4238 installsignal(SIGHUP, "HUP");
4239 installsignal(SIGQUIT, "QUIT");
4240 installsignal(SIGTERM, "TERM");
4241 installsignal(SIGCHLD, "CHLD");
4243 astate = XInternAtom(display, "WM_STATE", False);
4244 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
4245 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
4247 /* look for local and global conf file */
4248 pwd = getpwuid(getuid());
4250 errx(1, "invalid user %d", getuid());
4258 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
4259 if (stat(conf, &sb) != -1) {
4260 if (S_ISREG(sb.st_mode))
4263 /* try global conf file */
4264 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
4265 if (!stat(conf, &sb))
4266 if (S_ISREG(sb.st_mode))
4272 /* setup all bars */
4273 for (i = 0; i < ScreenCount(display); i++)
4274 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4275 if (winfocus == NULL)
4276 winfocus = TAILQ_FIRST(&r->ws->winlist);
4280 /* set some values to work around bad programs */
4286 xfd = ConnectionNumber(display);
4288 while (XPending(display)) {
4289 XNextEvent(display, &e);
4292 if (e.type < LASTEvent) {
4294 if (handler[e.type])
4295 handler[e.type](&e);
4297 DNPRINTF(SWM_D_EVENT,
4298 "win: %lu unknown event: %d\n",
4299 e.xany.window, e.type);
4301 switch (e.type - xrandr_eventbase) {
4302 case RRScreenChangeNotify:
4306 DNPRINTF(SWM_D_EVENT,
4307 "win: %lu unknown xrandr event: "
4308 "%d\n", e.xany.window, e.type);
4314 /* if we are being restarted go focus on first window */
4316 rr = TAILQ_FIRST(&screens[0].rl);
4317 /* move pointer to first screen if multi screen */
4318 if (ScreenCount(display) > 1 || outputs > 1)
4319 XWarpPointer(display, None, rr->s[0].root,
4320 0, 0, 0, 0, rr->g.x,
4321 rr->g.y + bar_enabled ? bar_height : 0);
4323 focus_win(winfocus);
4330 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
4332 DNPRINTF(SWM_D_MISC, "select failed");
4343 XCloseDisplay(display);