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.9"
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 SWM_EV_PROLOGUE(x) do { XGrabServer(x); } while (0)
152 #define SWM_EV_EPILOGUE(x) do { XUngrabServer(x); XFlush(x); } while (0)
155 #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 int ignore_enter = 0;
170 unsigned int numlockmask = 0;
174 int cycle_visible = 0;
176 int font_adjusted = 0;
177 unsigned int mod_key = MODKEY;
180 double dialog_ratio = .6;
182 #define SWM_BAR_MAX (256)
183 char *bar_argv[] = { NULL, NULL };
185 char bar_ext[SWM_BAR_MAX];
186 char bar_vertext[SWM_BAR_MAX];
188 sig_atomic_t bar_alarm = 0;
192 int bar_extra_running = 0;
195 int stack_enabled = 1;
196 int clock_enabled = 1;
197 int title_name_enabled = 0;
198 int title_class_enabled = 0;
204 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
205 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
207 #define SWM_MENU_FN (2)
208 #define SWM_MENU_NB (4)
209 #define SWM_MENU_NF (6)
210 #define SWM_MENU_SB (8)
211 #define SWM_MENU_SF (10)
213 /* layout manager data */
214 struct swm_geometry {
224 /* virtual "screens" */
226 TAILQ_ENTRY(swm_region) entry;
227 struct swm_geometry g;
228 struct workspace *ws; /* current workspace on this region */
229 struct swm_screen *s; /* screen idx */
232 TAILQ_HEAD(swm_region_list, swm_region);
235 TAILQ_ENTRY(ws_win) entry;
237 struct swm_geometry g;
241 int font_size_boundary[SWM_MAX_FONT_STEPS];
245 unsigned long quirks;
246 struct workspace *ws; /* always valid */
247 struct swm_screen *s; /* always valid, never changes */
248 XWindowAttributes wa;
252 TAILQ_HEAD(ws_win_list, ws_win);
254 /* user/key callable function IDs */
311 /* layout handlers */
313 void vertical_config(struct workspace *, int);
314 void vertical_stack(struct workspace *, struct swm_geometry *);
315 void horizontal_config(struct workspace *, int);
316 void horizontal_stack(struct workspace *, struct swm_geometry *);
317 void max_stack(struct workspace *, struct swm_geometry *);
319 void grabbuttons(struct ws_win *, int);
320 void new_region(struct swm_screen *, int, int, int, int);
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 maprequest(XEvent *);
561 void propertynotify(XEvent *);
562 void unmapnotify(XEvent *);
563 void visibilitynotify(XEvent *);
565 void (*handler[LASTEvent])(XEvent *) = {
567 [KeyPress] = keypress,
568 [ButtonPress] = buttonpress,
569 [ConfigureRequest] = configurerequest,
570 [ConfigureNotify] = configurenotify,
571 [DestroyNotify] = destroynotify,
572 [EnterNotify] = enternotify,
574 [FocusOut] = focusout,
575 [MapNotify] = mapnotify,
576 [MapRequest] = maprequest,
577 [PropertyNotify] = propertynotify,
578 [UnmapNotify] = unmapnotify,
579 [VisibilityNotify] = visibilitynotify,
589 while ((pid = waitpid(WAIT_ANY, NULL, WNOHANG)) != -1) {
590 DNPRINTF(SWM_D_MISC, "reaping: %d\n", pid);
605 installsignal(int sig, char *name)
609 sa.sa_handler = sighdlr;
610 sigemptyset(&sa.sa_mask);
612 if (sigaction(sig, &sa, NULL) == -1)
613 err(1, "could not install %s handler", name);
617 name_to_color(char *colorname)
621 XColor screen_def, exact_def;
622 unsigned long result = 0;
623 char cname[32] = "#";
625 cmap = DefaultColormap(display, screens[0].idx);
626 status = XAllocNamedColor(display, cmap, colorname,
627 &screen_def, &exact_def);
629 strlcat(cname, colorname + 2, sizeof cname - 1);
630 status = XAllocNamedColor(display, cmap, cname, &screen_def,
634 result = screen_def.pixel;
636 fprintf(stderr, "color '%s' not found.\n", colorname);
642 setscreencolor(char *val, int i, int c)
644 if (i > 0 && i <= ScreenCount(display)) {
645 screens[i - 1].c[c].color = name_to_color(val);
646 free(screens[i - 1].c[c].name);
647 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
649 } else if (i == -1) {
650 for (i = 0; i < ScreenCount(display); i++) {
651 screens[i].c[c].color = name_to_color(val);
652 free(screens[i].c[c].name);
653 if ((screens[i].c[c].name = strdup(val)) == NULL)
657 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
658 i, ScreenCount(display));
662 custom_region(char *val)
664 unsigned int sidx, x, y, w, h;
666 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
667 errx(1, "invalid custom region, "
668 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
669 if (sidx < 1 || sidx > ScreenCount(display))
670 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
671 sidx, ScreenCount(display));
675 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
677 if (x < 0 || x > DisplayWidth(display, sidx) ||
678 y < 0 || y > DisplayHeight(display, sidx) ||
679 w + x > DisplayWidth(display, sidx) ||
680 h + y > DisplayHeight(display, sidx))
681 errx(1, "region %ux%u+%u+%u not within screen boundaries "
682 "(%ux%u)\n", w, h, x, y,
683 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
685 new_region(&screens[sidx], x, y, w, h);
689 socket_setnonblock(int fd)
693 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
694 err(1, "fcntl F_GETFL");
696 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
697 err(1, "fcntl F_SETFL");
701 bar_print(struct swm_region *r, char *s)
703 XClearWindow(display, r->bar_window);
704 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
705 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
714 bzero(bar_pipe, sizeof bar_pipe);
717 kill(bar_pid, SIGTERM);
720 strlcpy(bar_ext, "", sizeof bar_ext);
725 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
727 int do_class, do_name;
729 XClassHint *xch = NULL;
731 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
733 if ((xch = XAllocClassHint()) == NULL)
735 status = XGetClassHint(display, cur_focus->id, xch);
736 if (status == BadWindow || status == BadAlloc)
738 do_class = (title_class_enabled && xch->res_class != NULL);
739 do_name = (title_name_enabled && xch->res_name != NULL);
741 strlcat(s, xch->res_class, sz);
742 if (do_class && do_name)
745 strlcat(s, xch->res_name, sz);
758 struct swm_region *r;
762 char loc[SWM_BAR_MAX];
766 if (bar_enabled == 0)
768 if (bar_extra && bar_extra_running) {
769 /* ignore short reads; it'll correct itself */
770 while ((b = fgetln(stdin, &len)) != NULL)
771 if (b && b[len - 1] == '\n') {
773 strlcpy(bar_ext, b, sizeof bar_ext);
775 if (b == NULL && errno != EAGAIN) {
776 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
777 errno, strerror(errno));
781 strlcpy(bar_ext, "", sizeof bar_ext);
783 if (clock_enabled == 0)
784 strlcpy(s, "", sizeof s);
787 localtime_r(&tmt, &tm);
788 strftime(s, sizeof s, "%a %b %d %R %Z %Y ", &tm);
791 for (i = 0; i < ScreenCount(display); i++) {
793 TAILQ_FOREACH(r, &screens[i].rl, entry) {
795 bar_class_name(s, sizeof s, r->ws->focus);
798 stack = r->ws->cur_layout->name;
800 snprintf(loc, sizeof loc, "%d:%d %s %s %s %s",
801 x++, r->ws->idx + 1, stack, s, bar_ext,
816 bar_toggle(struct swm_region *r, union arg *args)
818 struct swm_region *tmpr;
819 int i, j, sc = ScreenCount(display);
821 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
824 for (i = 0; i < sc; i++)
825 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
826 XUnmapWindow(display, tmpr->bar_window);
828 for (i = 0; i < sc; i++)
829 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
830 XMapRaised(display, tmpr->bar_window);
832 bar_enabled = !bar_enabled;
833 for (i = 0; i < sc; i++)
834 for (j = 0; j < SWM_WS_MAX; j++)
835 screens[i].ws[j].restack = 1;
838 /* must be after stack */
845 XSetWindowAttributes wa;
846 struct swm_region *r;
849 /* do this here because the conf file is in memory */
850 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
851 /* launch external status app */
852 bar_extra_running = 1;
853 if (pipe(bar_pipe) == -1)
854 err(1, "pipe error");
855 socket_setnonblock(bar_pipe[0]);
856 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
857 if (dup2(bar_pipe[0], 0) == -1)
859 if (dup2(bar_pipe[1], 1) == -1)
861 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
862 err(1, "could not disable SIGPIPE");
863 switch (bar_pid = fork()) {
865 err(1, "cannot fork");
869 execvp(bar_argv[0], bar_argv);
870 err(1, "%s external app failed", bar_argv[0]);
872 default: /* parent */
878 bzero(&wa, sizeof wa);
879 for (i = 0; i < ScreenCount(display); i++)
880 TAILQ_FOREACH(r, &screens[i].rl, entry) {
882 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
883 wa.background_pixel =
884 screens[i].c[SWM_S_COLOR_BAR].color;
885 XChangeWindowAttributes(display, r->bar_window,
886 CWBackPixel | CWBorderPixel, &wa);
892 bar_setup(struct swm_region *r)
896 for (i = 0; bar_fonts[i] != NULL; i++) {
897 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
903 if (bar_fonts[i] == NULL)
904 errx(1, "couldn't load font");
905 bar_height = bar_fs->ascent + bar_fs->descent + 3;
907 r->bar_window = XCreateSimpleWindow(display,
908 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
909 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
910 r->s->c[SWM_S_COLOR_BAR].color);
911 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
912 XSetFont(display, bar_gc, bar_fs->fid);
913 XSelectInput(display, r->bar_window, VisibilityChangeMask);
915 XMapRaised(display, r->bar_window);
916 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
918 if (signal(SIGALRM, bar_signal) == SIG_ERR)
919 err(1, "could not install bar_signal");
924 set_win_state(struct ws_win *win, long state)
926 long data[] = {state, None};
928 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
930 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
931 (unsigned char *)data, 2);
939 unsigned char *p = NULL;
940 unsigned long n, extra;
943 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
944 &real, &format, &n, &extra, (unsigned char **)&p);
945 if (status != Success)
948 result = *((long *)p);
954 version(struct swm_region *r, union arg *args)
956 bar_version = !bar_version;
958 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
959 SWM_VERSION, cvstag);
961 strlcpy(bar_vertext, "", sizeof bar_vertext);
966 client_msg(struct ws_win *win, Atom a)
968 XClientMessageEvent cm;
970 bzero(&cm, sizeof cm);
971 cm.type = ClientMessage;
973 cm.message_type = aprot;
976 cm.data.l[1] = CurrentTime;
977 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
981 config_win(struct ws_win *win)
985 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
986 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
987 ce.type = ConfigureNotify;
988 ce.display = display;
994 ce.height = win->g.h;
995 ce.border_width = 1; /* XXX store this! */
997 ce.override_redirect = False;
998 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1002 count_win(struct workspace *ws, int count_transient)
1007 TAILQ_FOREACH(win, &ws->winlist, entry) {
1008 if (count_transient == 0 && win->floating)
1010 if (count_transient == 0 && win->transient)
1014 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1020 quit(struct swm_region *r, union arg *args)
1022 DNPRINTF(SWM_D_MISC, "quit\n");
1027 unmap_window(struct ws_win *win)
1032 set_win_state(win, IconicState);
1033 XUnmapWindow(display, win->id);
1042 for (i = 0; i < ScreenCount(display); i++)
1043 for (j = 0; j < SWM_WS_MAX; j++)
1044 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1049 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1053 event.display = display; /* Ignored, but what the hell */
1054 event.window = win->id;
1055 event.root = win->s->root;
1056 event.subwindow = None;
1057 event.time = CurrentTime;
1062 event.same_screen = True;
1063 event.keycode = XKeysymToKeycode(display, keysym);
1064 event.state = modifiers;
1066 event.type = KeyPress;
1067 XSendEvent(event.display, event.window, True,
1068 KeyPressMask, (XEvent *)&event);
1070 event.type = KeyRelease;
1071 XSendEvent(event.display, event.window, True,
1072 KeyPressMask, (XEvent *)&event);
1077 restart(struct swm_region *r, union arg *args)
1079 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1081 /* disable alarm because the following code may not be interrupted */
1083 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1084 errx(1, "can't disable alarm");
1089 XCloseDisplay(display);
1090 execvp(start_argv[0], start_argv);
1091 fprintf(stderr, "execvp failed\n");
1097 root_to_region(Window root)
1099 struct swm_region *r = NULL;
1101 int i, x, y, wx, wy;
1104 for (i = 0; i < ScreenCount(display); i++)
1105 if (screens[i].root == root)
1108 if (XQueryPointer(display, screens[i].root,
1109 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1110 /* choose a region based on pointer location */
1111 TAILQ_FOREACH(r, &screens[i].rl, entry)
1112 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1113 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1118 r = TAILQ_FIRST(&screens[i].rl);
1124 find_window(Window id)
1129 for (i = 0; i < ScreenCount(display); i++)
1130 for (j = 0; j < SWM_WS_MAX; j++)
1131 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1138 spawn(struct swm_region *r, union arg *args)
1143 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1145 * The double-fork construct avoids zombie processes and keeps the code
1146 * clean from stupid signal handlers.
1151 close(ConnectionNumber(display));
1152 setenv("LD_PRELOAD", SWM_LIB, 1);
1153 if (asprintf(&ret, "%d", r->ws->idx)) {
1154 setenv("_SWM_WS", ret, 1);
1157 if (asprintf(&ret, "%d", getpid())) {
1158 setenv("_SWM_PID", ret, 1);
1162 /* kill stdin, mplayer, ssh-add etc. need that */
1163 si = open("/dev/null", O_RDONLY, 0);
1165 err(1, "open /dev/null");
1166 if (dup2(si, 0) == -1)
1167 err(1, "dup2 /dev/null");
1168 execvp(args->argv[0], args->argv);
1169 fprintf(stderr, "execvp failed\n");
1178 spawnterm(struct swm_region *r, union arg *args)
1180 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1183 setenv("_SWM_XTERM_FONTADJ", "", 1);
1188 unfocus_win(struct ws_win *win)
1193 if (win->ws->r == NULL)
1196 grabbuttons(win, 0);
1197 XSetWindowBorder(display, win->id,
1198 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1200 if (win->ws->focus == win) {
1201 win->ws->focus = NULL;
1202 win->ws->focus_prev = win;
1212 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
1214 for (i = 0; i < ScreenCount(display); i++)
1215 for (j = 0; j < SWM_WS_MAX; j++)
1216 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1218 XSync(display, False);
1222 focus_win(struct ws_win *win)
1224 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1229 /* use big hammer to make sure it works under all use cases */
1231 win->ws->focus = win;
1233 if (win->ws->r != NULL) {
1234 XSetWindowBorder(display, win->id,
1235 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1236 grabbuttons(win, 1);
1237 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS)
1238 XMapRaised(display, win->id);
1239 XSetInputFocus(display, win->id,
1240 RevertToPointerRoot, CurrentTime);
1241 XSync(display, False);
1246 switchws(struct swm_region *r, union arg *args)
1248 int wsid = args->id;
1249 struct swm_region *this_r, *other_r;
1250 struct ws_win *win, *winfocus = NULL, *parent = NULL;
1251 struct workspace *new_ws, *old_ws;
1254 old_ws = this_r->ws;
1255 new_ws = &this_r->s->ws[wsid];
1257 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1258 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1261 if (new_ws == old_ws)
1264 /* get focus window */
1266 winfocus = new_ws->focus;
1267 else if (new_ws->focus_prev)
1268 winfocus = new_ws->focus_prev;
1270 winfocus = TAILQ_FIRST(&new_ws->winlist);
1272 other_r = new_ws->r;
1273 if (other_r == NULL) {
1274 /* if the other workspace is hidden, switch windows */
1275 if (old_ws->r != NULL)
1276 old_ws->old_r = old_ws->r;
1278 old_ws->restack = 1;
1281 * Map new windows first if they were here before
1282 * to minimize ugly blinking.
1284 if (new_ws->old_r == this_r)
1285 TAILQ_FOREACH(win, &new_ws->winlist, entry)
1286 if (!(win->ws->cur_layout->flags &
1288 XMapRaised(display, win->id);
1290 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1293 other_r->ws = old_ws;
1294 old_ws->r = other_r;
1296 this_r->ws = new_ws;
1302 /* make sure we see the parent window */
1303 if (winfocus->transient) {
1304 parent = find_window(winfocus->transient);
1309 focus_win(winfocus);
1316 cyclews(struct swm_region *r, union arg *args)
1319 struct swm_screen *s = r->s;
1321 DNPRINTF(SWM_D_WS, "cyclews id %d "
1322 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1323 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1328 case SWM_ARG_ID_CYCLEWS_UP:
1329 if (a.id < SWM_WS_MAX - 1)
1334 case SWM_ARG_ID_CYCLEWS_DOWN:
1338 a.id = SWM_WS_MAX - 1;
1344 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1346 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1350 } while (a.id != r->ws->idx);
1354 cyclescr(struct swm_region *r, union arg *args)
1356 struct swm_region *rr;
1359 /* do nothing if we don't have more than one screen */
1360 if (!(ScreenCount(display) > 1 || outputs > 1))
1365 case SWM_ARG_ID_CYCLESC_UP:
1366 rr = TAILQ_NEXT(r, entry);
1368 rr = TAILQ_FIRST(&screens[i].rl);
1370 case SWM_ARG_ID_CYCLESC_DOWN:
1371 rr = TAILQ_PREV(r, swm_region_list, entry);
1373 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1379 XSetInputFocus(display, PointerRoot, RevertToPointerRoot, CurrentTime);
1380 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, rr->g.x + 1,
1381 rr->g.y + bar_enabled + 1 ? bar_height : 0);
1385 swapwin(struct swm_region *r, union arg *args)
1387 struct ws_win *target, *source;
1388 struct ws_win *cur_focus;
1389 struct ws_win_list *wl;
1392 DNPRINTF(SWM_D_WS, "swapwin id %d "
1393 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1394 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1396 cur_focus = r->ws->focus;
1397 if (cur_focus == NULL)
1401 wl = &source->ws->winlist;
1404 case SWM_ARG_ID_SWAPPREV:
1405 target = TAILQ_PREV(source, ws_win_list, entry);
1406 TAILQ_REMOVE(wl, cur_focus, entry);
1408 TAILQ_INSERT_TAIL(wl, source, entry);
1410 TAILQ_INSERT_BEFORE(target, source, entry);
1412 case SWM_ARG_ID_SWAPNEXT:
1413 target = TAILQ_NEXT(source, entry);
1414 TAILQ_REMOVE(wl, source, entry);
1416 TAILQ_INSERT_HEAD(wl, source, entry);
1418 TAILQ_INSERT_AFTER(wl, target, source, entry);
1420 case SWM_ARG_ID_SWAPMAIN:
1421 target = TAILQ_FIRST(wl);
1422 if (target == source) {
1423 if (source->ws->focus_prev != NULL &&
1424 source->ws->focus_prev != target)
1426 source = source->ws->focus_prev;
1430 source->ws->focus_prev = target;
1431 TAILQ_REMOVE(wl, target, entry);
1432 TAILQ_INSERT_BEFORE(source, target, entry);
1433 TAILQ_REMOVE(wl, source, entry);
1434 TAILQ_INSERT_HEAD(wl, source, entry);
1437 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1446 focus(struct swm_region *r, union arg *args)
1448 struct ws_win *winfocus, *winlostfocus;
1449 struct ws_win_list *wl;
1450 struct ws_win *cur_focus;
1452 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1454 cur_focus = r->ws->focus;
1455 if (cur_focus == NULL)
1458 wl = &cur_focus->ws->winlist;
1460 winlostfocus = cur_focus;
1463 case SWM_ARG_ID_FOCUSPREV:
1464 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1465 if (winfocus == NULL)
1466 winfocus = TAILQ_LAST(wl, ws_win_list);
1469 case SWM_ARG_ID_FOCUSNEXT:
1470 winfocus = TAILQ_NEXT(cur_focus, entry);
1471 if (winfocus == NULL)
1472 winfocus = TAILQ_FIRST(wl);
1475 case SWM_ARG_ID_FOCUSMAIN:
1476 winfocus = TAILQ_FIRST(wl);
1477 if (winfocus == cur_focus)
1478 winfocus = cur_focus->ws->focus_prev;
1479 if (winfocus == NULL)
1487 if (winfocus == winlostfocus || winfocus == NULL)
1490 focus_win(winfocus);
1494 cycle_layout(struct swm_region *r, union arg *args)
1496 struct workspace *ws = r->ws;
1497 struct ws_win *winfocus, *parent = NULL;
1499 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1501 winfocus = ws->focus;
1504 if (ws->cur_layout->l_stack == NULL)
1505 ws->cur_layout = &layouts[0];
1509 /* make sure we see the parent window */
1511 if (winfocus->transient)
1512 parent = find_window(winfocus->transient);
1515 focus_win(winfocus);
1521 stack_config(struct swm_region *r, union arg *args)
1523 struct workspace *ws = r->ws;
1525 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1528 if (ws->cur_layout->l_config != NULL)
1529 ws->cur_layout->l_config(ws, args->id);
1531 if (args->id != SWM_ARG_ID_STACKINIT);
1537 struct swm_geometry g;
1538 struct swm_region *r;
1541 DNPRINTF(SWM_D_STACK, "stack\n");
1543 for (i = 0; i < ScreenCount(display); i++) {
1545 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1546 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1547 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1549 /* start with screen geometry, adjust for bar */
1559 r->ws->cur_layout->l_stack(r->ws, &g);
1564 XSync(display, False);
1568 stack_floater(struct ws_win *win, struct swm_region *r)
1576 bzero(&wc, sizeof wc);
1577 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1578 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w == WIDTH(r)) &&
1579 (win->g.h == HEIGHT(r)))
1580 wc.border_width = 0;
1582 wc.border_width = 1;
1583 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
1584 win->g.w = (double)WIDTH(r) * dialog_ratio;
1585 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1587 wc.width = win->g.w;
1588 wc.height = win->g.h;
1593 wc.x = (WIDTH(r) - win->g.w) / 2;
1594 wc.y = (HEIGHT(r) - win->g.h) / 2;
1597 /* adjust for region */
1601 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1602 win->id, wc.x, wc.y, wc.width, wc.height);
1604 XConfigureWindow(display, win->id, mask, &wc);
1608 * Send keystrokes to terminal to decrease/increase the font size as the
1609 * window size changes.
1612 adjust_font(struct ws_win *win)
1614 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
1615 win->floating || win->transient)
1618 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
1619 win->g.w / win->sh.width_inc < term_width &&
1620 win->font_steps < SWM_MAX_FONT_STEPS) {
1621 win->font_size_boundary[win->font_steps] =
1622 (win->sh.width_inc * term_width) + win->sh.base_width;
1625 win->last_inc = win->sh.width_inc;
1626 fake_keypress(win, XK_KP_Subtract, ShiftMask);
1627 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
1628 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
1631 win->last_inc = win->sh.width_inc;
1632 fake_keypress(win, XK_KP_Add, ShiftMask);
1636 #define SWAPXY(g) do { \
1638 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1639 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1642 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1645 struct swm_geometry win_g, r_g = *g;
1647 int i, j, s, stacks;
1648 int w_inc = 1, h_inc, w_base = 1, h_base;
1649 int hrh, extra = 0, h_slice, last_h = 0;
1650 int split, colno, winno, mwin, msize, mscale;
1651 int remain, missing, v_slice;
1654 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1655 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1657 winno = count_win(ws, 0);
1658 if (winno == 0 && count_win(ws, 1) == 0)
1661 TAILQ_FOREACH(win, &ws->winlist, entry)
1662 if (win->transient == 0 && win->floating == 0)
1669 w_inc = win->sh.width_inc;
1670 w_base = win->sh.base_width;
1671 mwin = ws->l_state.horizontal_mwin;
1672 mscale = ws->l_state.horizontal_msize;
1673 stacks = ws->l_state.horizontal_stacks;
1676 w_inc = win->sh.height_inc;
1677 w_base = win->sh.base_height;
1678 mwin = ws->l_state.vertical_mwin;
1679 mscale = ws->l_state.vertical_msize;
1680 stacks = ws->l_state.vertical_stacks;
1684 if (stacks > winno - mwin)
1685 stacks = winno - mwin;
1689 h_slice = r_g.h / SWM_H_SLICE;
1690 if (mwin && winno > mwin) {
1691 v_slice = r_g.w / SWM_V_SLICE;
1695 win_g.w = v_slice * mscale;
1697 if (w_inc > 1 && w_inc < v_slice) {
1698 /* adjust for window's requested size increment */
1699 remain = (win_g.w - w_base) % w_inc;
1700 missing = w_inc - remain;
1707 win_g.x += r_g.w - msize;
1710 colno = split = winno / stacks;
1711 win_g.w = ((r_g.w - (stacks * 2) + 2) / stacks);
1713 hrh = r_g.h / colno;
1714 extra = r_g.h - (colno * hrh);
1717 /* stack all the tiled windows */
1718 i = j = 0, s = stacks;
1719 TAILQ_FOREACH(win, &ws->winlist, entry) {
1720 if (win->transient != 0 || win->floating != 0)
1723 if (split && i == split) {
1724 colno = (winno - mwin) / stacks;
1725 if (s <= (winno - mwin) % stacks)
1727 split = split + colno;
1728 hrh = (r_g.h / colno);
1729 extra = r_g.h - (colno * hrh);
1733 win_g.x += win_g.w + 2;
1734 win_g.w = (r_g.w - msize - (stacks * 2)) / stacks;
1736 win_g.w += (r_g.w - msize - (stacks * 2)) %
1743 h_inc = win->sh.width_inc;
1744 h_base = win->sh.base_width;
1746 h_inc = win->sh.height_inc;
1747 h_base = win->sh.base_height;
1749 if (j == colno - 1) {
1750 win_g.h = hrh + extra;
1751 } else if (h_inc > 1 && h_inc < h_slice) {
1752 /* adjust for window's requested size increment */
1753 remain = (win_g.h - h_base) % h_inc;
1754 missing = h_inc - remain;
1756 if (missing <= extra || j == 0) {
1768 win_g.y += last_h + 2;
1770 bzero(&wc, sizeof wc);
1771 wc.border_width = 1;
1773 win->g.x = wc.x = win_g.y;
1774 win->g.y = wc.y = win_g.x;
1775 win->g.w = wc.width = win_g.h;
1776 win->g.h = wc.height = win_g.w;
1778 win->g.x = wc.x = win_g.x;
1779 win->g.y = wc.y = win_g.y;
1780 win->g.w = wc.width = win_g.w;
1781 win->g.h = wc.height = win_g.h;
1784 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1785 XConfigureWindow(display, win->id, mask, &wc);
1786 XMapRaised(display, win->id);
1794 /* now, stack all the floaters and transients */
1795 TAILQ_FOREACH(win, &ws->winlist, entry) {
1796 if (win->transient == 0 && win->floating == 0)
1799 stack_floater(win, ws->r);
1800 XMapRaised(display, win->id);
1805 vertical_config(struct workspace *ws, int id)
1807 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1810 case SWM_ARG_ID_STACKRESET:
1811 case SWM_ARG_ID_STACKINIT:
1812 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1813 ws->l_state.vertical_mwin = 1;
1814 ws->l_state.vertical_stacks = 1;
1816 case SWM_ARG_ID_MASTERSHRINK:
1817 if (ws->l_state.vertical_msize > 1)
1818 ws->l_state.vertical_msize--;
1820 case SWM_ARG_ID_MASTERGROW:
1821 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1822 ws->l_state.vertical_msize++;
1824 case SWM_ARG_ID_MASTERADD:
1825 ws->l_state.vertical_mwin++;
1827 case SWM_ARG_ID_MASTERDEL:
1828 if (ws->l_state.vertical_mwin > 0)
1829 ws->l_state.vertical_mwin--;
1831 case SWM_ARG_ID_STACKINC:
1832 ws->l_state.vertical_stacks++;
1834 case SWM_ARG_ID_STACKDEC:
1835 if (ws->l_state.vertical_stacks > 1)
1836 ws->l_state.vertical_stacks--;
1844 vertical_stack(struct workspace *ws, struct swm_geometry *g)
1846 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1848 stack_master(ws, g, 0, 0);
1852 horizontal_config(struct workspace *ws, int id)
1854 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1857 case SWM_ARG_ID_STACKRESET:
1858 case SWM_ARG_ID_STACKINIT:
1859 ws->l_state.horizontal_mwin = 1;
1860 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1861 ws->l_state.horizontal_stacks = 1;
1863 case SWM_ARG_ID_MASTERSHRINK:
1864 if (ws->l_state.horizontal_msize > 1)
1865 ws->l_state.horizontal_msize--;
1867 case SWM_ARG_ID_MASTERGROW:
1868 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1869 ws->l_state.horizontal_msize++;
1871 case SWM_ARG_ID_MASTERADD:
1872 ws->l_state.horizontal_mwin++;
1874 case SWM_ARG_ID_MASTERDEL:
1875 if (ws->l_state.horizontal_mwin > 0)
1876 ws->l_state.horizontal_mwin--;
1878 case SWM_ARG_ID_STACKINC:
1879 ws->l_state.horizontal_stacks++;
1881 case SWM_ARG_ID_STACKDEC:
1882 if (ws->l_state.horizontal_stacks > 1)
1883 ws->l_state.horizontal_stacks--;
1891 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
1893 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1895 stack_master(ws, g, 1, 0);
1898 /* fullscreen view */
1900 max_stack(struct workspace *ws, struct swm_geometry *g)
1903 struct swm_geometry gg = *g;
1904 struct ws_win *win, *wintrans = NULL;
1908 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1913 winno = count_win(ws, 0);
1914 if (winno == 0 && count_win(ws, 1) == 0)
1917 TAILQ_FOREACH(win, &ws->winlist, entry) {
1918 if (win->transient != 0) {
1921 bzero(&wc, sizeof wc);
1922 wc.border_width = 1;
1923 win->g.x = wc.x = gg.x;
1924 win->g.y = wc.y = gg.y;
1925 win->g.w = wc.width = gg.w;
1926 win->g.h = wc.height = gg.h;
1927 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1928 XConfigureWindow(display, win->id, mask, &wc);
1930 /* unmap only if we don't have multi screen */
1931 if (win != ws->focus)
1932 if (!(ScreenCount(display) > 1 || outputs > 1))
1937 /* put the last transient on top */
1939 stack_floater(wintrans, ws->r);
1940 focus_win(wintrans); /* override */
1945 send_to_ws(struct swm_region *r, union arg *args)
1947 int wsid = args->id;
1948 struct ws_win *win = win, *winfocus = NULL;
1949 struct workspace *ws, *nws;
1950 Atom ws_idx_atom = 0;
1951 unsigned char ws_idx_str[SWM_PROPLEN];
1960 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1963 nws = &win->s->ws[wsid];
1965 /* find a window to focus */
1966 winfocus = TAILQ_PREV(win, ws_win_list, entry);
1967 if (TAILQ_FIRST(&ws->winlist) == win)
1968 winfocus = TAILQ_NEXT(win, entry);
1970 winfocus = TAILQ_PREV(win, ws_win_list, entry);
1971 if (winfocus == NULL)
1972 winfocus = TAILQ_LAST(&ws->winlist, ws_win_list);
1974 /* out of windows in ws so focus on nws instead if we multi screen */
1975 if (winfocus == NULL)
1976 if (ScreenCount(display) > 1 || outputs > 1)
1981 TAILQ_REMOVE(&ws->winlist, win, entry);
1982 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1985 /* Try to update the window's workspace property */
1986 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1988 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
1989 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1991 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1992 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1995 if (count_win(nws, 1) == 1)
2002 focus_win(winfocus);
2006 wkill(struct swm_region *r, union arg *args)
2008 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
2010 if(r->ws->focus == NULL)
2013 if (args->id == SWM_ARG_ID_KILLWINDOW)
2014 XKillClient(display, r->ws->focus->id);
2016 if (r->ws->focus->can_delete)
2017 client_msg(r->ws->focus, adelete);
2021 floating_toggle(struct swm_region *r, union arg *args)
2023 struct ws_win *win = r->ws->focus;
2028 win->floating = !win->floating;
2035 resize_window(struct ws_win *win, int center)
2039 struct swm_region *r;
2041 r = root_to_region(win->wa.root);
2042 bzero(&wc, sizeof wc);
2043 mask = CWBorderWidth | CWWidth | CWHeight;
2044 wc.border_width = 1;
2045 wc.width = win->g.w;
2046 wc.height = win->g.h;
2047 if (center == SWM_ARG_ID_CENTER) {
2048 wc.x = (WIDTH(r) - win->g.w) / 2;
2049 wc.y = (HEIGHT(r) - win->g.h) / 2;
2053 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
2054 win->id, wc.x, wc.y, wc.width, wc.height);
2056 XConfigureWindow(display, win->id, mask, &wc);
2061 resize(struct ws_win *win, union arg *args)
2066 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %d\n",
2067 win->id, win->floating, win->transient);
2069 if (!(win->transient != 0 || win->floating != 0))
2072 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2073 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2075 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w, win->g.h);
2077 XMaskEvent(display, MOUSEMASK | ExposureMask |
2078 SubstructureRedirectMask, &ev);
2080 case ConfigureRequest:
2083 handler[ev.type](&ev);
2086 if (ev.xmotion.x <= 1)
2088 if (ev.xmotion.y <= 1)
2090 win->g.w = ev.xmotion.x;
2091 win->g.h = ev.xmotion.y;
2093 /* not free, don't sync more than 60 times / second */
2094 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2095 time = ev.xmotion.time;
2096 XSync(display, False);
2097 resize_window(win, args->id);
2101 } while (ev.type != ButtonRelease);
2103 XSync(display, False);
2104 resize_window(win, args->id);
2106 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
2108 XUngrabPointer(display, CurrentTime);
2111 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2115 move_window(struct ws_win *win)
2119 struct swm_region *r;
2121 r = root_to_region(win->wa.root);
2122 bzero(&wc, sizeof wc);
2127 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
2128 win->id, wc.x, wc.y, wc.width, wc.height);
2130 XConfigureWindow(display, win->id, mask, &wc);
2135 move(struct ws_win *win, union arg *args)
2141 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %d\n",
2142 win->id, win->floating, win->transient);
2144 if (win->floating == 0) {
2150 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2151 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2153 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2155 XMaskEvent(display, MOUSEMASK | ExposureMask |
2156 SubstructureRedirectMask, &ev);
2158 case ConfigureRequest:
2161 handler[ev.type](&ev);
2164 win->g.x = ev.xmotion.x_root;
2165 win->g.y = ev.xmotion.y_root;
2167 /* not free, don't sync more than 60 times / second */
2168 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2169 time = ev.xmotion.time;
2170 XSync(display, False);
2175 } while (ev.type != ButtonRelease);
2177 XSync(display, False);
2180 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2181 XUngrabPointer(display, CurrentTime);
2186 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2189 /* key definitions */
2191 dummykeyfunc(struct swm_region *r, union arg *args)
2196 legacyfunc(struct swm_region *r, union arg *args)
2201 char name[SWM_FUNCNAME_LEN];
2202 void (*func)(struct swm_region *r, union arg *);
2204 } keyfuncs[kf_invalid + 1] = {
2205 /* name function argument */
2206 { "cycle_layout", cycle_layout, {0} },
2207 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
2208 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
2209 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
2210 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
2211 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
2212 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
2213 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
2214 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
2215 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
2216 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
2217 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
2218 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
2219 { "spawn_term", spawnterm, {.argv = spawn_term} },
2220 { "spawn_menu", legacyfunc, {0} },
2221 { "quit", quit, {0} },
2222 { "restart", restart, {0} },
2223 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
2224 { "ws_1", switchws, {.id = 0} },
2225 { "ws_2", switchws, {.id = 1} },
2226 { "ws_3", switchws, {.id = 2} },
2227 { "ws_4", switchws, {.id = 3} },
2228 { "ws_5", switchws, {.id = 4} },
2229 { "ws_6", switchws, {.id = 5} },
2230 { "ws_7", switchws, {.id = 6} },
2231 { "ws_8", switchws, {.id = 7} },
2232 { "ws_9", switchws, {.id = 8} },
2233 { "ws_10", switchws, {.id = 9} },
2234 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
2235 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
2236 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
2237 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
2238 { "mvws_1", send_to_ws, {.id = 0} },
2239 { "mvws_2", send_to_ws, {.id = 1} },
2240 { "mvws_3", send_to_ws, {.id = 2} },
2241 { "mvws_4", send_to_ws, {.id = 3} },
2242 { "mvws_5", send_to_ws, {.id = 4} },
2243 { "mvws_6", send_to_ws, {.id = 5} },
2244 { "mvws_7", send_to_ws, {.id = 6} },
2245 { "mvws_8", send_to_ws, {.id = 7} },
2246 { "mvws_9", send_to_ws, {.id = 8} },
2247 { "mvws_10", send_to_ws, {.id = 9} },
2248 { "bar_toggle", bar_toggle, {0} },
2249 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
2250 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
2251 { "screenshot_all", legacyfunc, {0} },
2252 { "screenshot_wind", legacyfunc, {0} },
2253 { "float_toggle", floating_toggle,{0} },
2254 { "version", version, {0} },
2255 { "spawn_lock", legacyfunc, {0} },
2256 { "spawn_initscr", legacyfunc, {0} },
2257 { "spawn_custom", dummykeyfunc, {0} },
2258 { "invalid key func", NULL, {0} },
2263 enum keyfuncid funcid;
2266 int keys_size = 0, keys_length = 0;
2267 struct key *keys = NULL;
2270 enum { client_click, root_click };
2272 unsigned int action;
2274 unsigned int button;
2275 void (*func)(struct ws_win *, union arg *);
2278 /* action key mouse button func args */
2279 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
2280 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
2281 { client_click, MODKEY, Button1, move, {0} },
2285 update_modkey(unsigned int mod)
2290 for (i = 0; i < keys_length; i++)
2291 if (keys[i].mod & ShiftMask)
2292 keys[i].mod = mod | ShiftMask;
2296 for (i = 0; i < LENGTH(buttons); i++)
2297 if (buttons[i].mask & ShiftMask)
2298 buttons[i].mask = mod | ShiftMask;
2300 buttons[i].mask = mod;
2310 int spawns_size = 0, spawns_length = 0;
2311 struct spawn_prog *spawns = NULL;
2314 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
2317 struct spawn_prog *prog = NULL;
2319 char *ap, **real_args;
2321 DNPRINTF(SWM_D_SPAWN, "spawn_custom %s\n", spawn_name);
2324 for (i = 0; i < spawns_length; i++) {
2325 if (!strcasecmp(spawn_name, spawns[i].name))
2329 fprintf(stderr, "spawn_custom: program %s not found\n",
2334 /* make room for expanded args */
2335 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
2336 err(1, "spawn_custom: calloc real_args");
2338 /* expand spawn_args into real_args */
2339 for (i = 0; i < prog->argc; i++) {
2341 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
2342 if (!strcasecmp(ap, "$bar_border")) {
2344 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
2346 err(1, "spawn_custom border color");
2347 } else if (!strcasecmp(ap, "$bar_color")) {
2349 strdup(r->s->c[SWM_S_COLOR_BAR].name))
2351 err(1, "spawn_custom bar color");
2352 } else if (!strcasecmp(ap, "$bar_font")) {
2353 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
2355 err(1, "spawn_custom bar fonts");
2356 } else if (!strcasecmp(ap, "$bar_font_color")) {
2358 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
2360 err(1, "spawn_custom color font");
2361 } else if (!strcasecmp(ap, "$color_focus")) {
2363 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
2365 err(1, "spawn_custom color focus");
2366 } else if (!strcasecmp(ap, "$color_unfocus")) {
2368 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
2370 err(1, "spawn_custom color unfocus");
2372 /* no match --> copy as is */
2373 if ((real_args[i] = strdup(ap)) == NULL)
2374 err(1, "spawn_custom strdup(ap)");
2376 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
2381 if ((swm_debug & SWM_D_SPAWN) != 0) {
2382 fprintf(stderr, "spawn_custom: result = ");
2383 for (i = 0; i < prog->argc; i++)
2384 fprintf(stderr, "\"%s\" ", real_args[i]);
2385 fprintf(stderr, "\n");
2391 for (i = 0; i < prog->argc; i++)
2397 setspawn(struct spawn_prog *prog)
2401 if (prog == NULL || prog->name == NULL)
2405 for (i = 0; i < spawns_length; i++) {
2406 if (!strcmp(spawns[i].name, prog->name)) {
2408 if (prog->argv == NULL) {
2410 DNPRINTF(SWM_D_SPAWN,
2411 "setspawn: delete #%d %s\n",
2413 free(spawns[i].name);
2414 for (j = 0; j < spawns[i].argc; j++)
2415 free(spawns[i].argv[j]);
2416 free(spawns[i].argv);
2417 j = spawns_length - 1;
2419 spawns[i] = spawns[j];
2424 DNPRINTF(SWM_D_SPAWN,
2425 "setspawn: replace #%d %s\n",
2427 free(spawns[i].name);
2428 for (j = 0; j < spawns[i].argc; j++)
2429 free(spawns[i].argv[j]);
2430 free(spawns[i].argv);
2433 /* found case handled */
2439 if (prog->argv == NULL) {
2441 "error: setspawn: cannot find program %s", prog->name);
2446 /* not found: add */
2447 if (spawns_size == 0 || spawns == NULL) {
2449 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
2450 spawns = malloc((size_t)spawns_size *
2451 sizeof(struct spawn_prog));
2452 if (spawns == NULL) {
2453 fprintf(stderr, "setspawn: malloc failed\n");
2457 } else if (spawns_length == spawns_size) {
2459 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
2460 spawns = realloc(spawns, (size_t)spawns_size *
2461 sizeof(struct spawn_prog));
2462 if (spawns == NULL) {
2463 fprintf(stderr, "setspawn: realloc failed\n");
2469 if (spawns_length < spawns_size) {
2470 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
2471 spawns_length, prog->name);
2472 i = spawns_length++;
2475 fprintf(stderr, "spawns array problem?\n");
2476 if (spawns == NULL) {
2477 fprintf(stderr, "spawns array is NULL!\n");
2485 setconfspawn(char *selector, char *value, int flags)
2487 struct spawn_prog *prog;
2488 char *vp, *cp, *word;
2490 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
2491 if ((prog = calloc(1, sizeof *prog)) == NULL)
2492 err(1, "setconfspawn: calloc prog");
2493 prog->name = strdup(selector);
2494 if (prog->name == NULL)
2495 err(1, "setconfspawn prog->name");
2496 if ((cp = vp = strdup(value)) == NULL)
2497 err(1, "setconfspawn: strdup(value) ");
2498 while ((word = strsep(&cp, " \t")) != NULL) {
2499 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
2501 cp += (long)strspn(cp, " \t");
2502 if (strlen(word) > 0) {
2504 prog->argv = realloc(prog->argv,
2505 prog->argc * sizeof(char *));
2506 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
2507 err(1, "setconfspawn: strdup");
2514 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
2521 setconfspawn("term", "xterm", 0);
2522 setconfspawn("screenshot_all", "screenshot.sh full", 0);
2523 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
2524 setconfspawn("lock", "xlock", 0);
2525 setconfspawn("initscr", "initscreen.sh", 0);
2526 setconfspawn("menu", "dmenu_run"
2529 " -nf $bar_font_color"
2531 " -sf $bar_color", 0);
2535 #define SWM_MODNAME_SIZE 32
2536 #define SWM_KEY_WS "\n+ \t"
2538 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
2542 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
2543 if (mod == NULL || ks == NULL) {
2544 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
2547 if (keystr == NULL || strlen(keystr) == 0) {
2548 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
2554 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
2555 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
2557 cp += (long)strspn(cp, SWM_KEY_WS);
2558 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
2560 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
2562 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
2564 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
2566 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
2568 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
2570 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
2571 *mod |= ControlMask;
2573 *ks = XStringToKeysym(name);
2574 XConvertCase(*ks, ks, &uks);
2575 if (ks == NoSymbol) {
2577 "parsekeys: invalid key %s\n",
2583 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
2587 strdupsafe(char *str)
2592 return (strdup(str));
2595 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
2598 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
2599 keyfuncs[kfid].name, spawn_name);
2601 for (i = 0; i < keys_length; i++) {
2602 if (keys[i].mod == mod && keys[i].keysym == ks) {
2603 if (kfid == kf_invalid) {
2606 "setkeybinding: delete #%d %s\n",
2607 i, keyfuncs[keys[i].funcid].name);
2608 free(keys[i].spawn_name);
2609 j = keys_length - 1;
2613 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2616 /* found: replace */
2618 "setkeybinding: replace #%d %s %s\n",
2619 i, keyfuncs[keys[i].funcid].name,
2621 free(keys[i].spawn_name);
2623 keys[i].keysym = ks;
2624 keys[i].funcid = kfid;
2625 keys[i].spawn_name = strdupsafe(spawn_name);
2626 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2631 if (kfid == kf_invalid) {
2633 "error: setkeybinding: cannot find mod/key combination");
2634 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2637 /* not found: add */
2638 if (keys_size == 0 || keys == NULL) {
2640 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
2641 keys = malloc((size_t)keys_size * sizeof(struct key));
2643 fprintf(stderr, "malloc failed\n");
2647 } else if (keys_length == keys_size) {
2649 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
2650 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
2652 fprintf(stderr, "realloc failed\n");
2657 if (keys_length < keys_size) {
2659 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
2660 j, keyfuncs[kfid].name, spawn_name);
2662 keys[j].keysym = ks;
2663 keys[j].funcid = kfid;
2664 keys[j].spawn_name = strdupsafe(spawn_name);
2666 fprintf(stderr, "keys array problem?\n");
2668 fprintf(stderr, "keys array problem\n");
2672 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2675 setconfbinding(char *selector, char *value, int flags)
2677 enum keyfuncid kfid;
2681 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
2682 if (selector == NULL) {
2683 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
2684 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
2686 setkeybinding(mod, ks, kfid, NULL);
2691 /* search by key function name */
2692 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
2693 if (strncasecmp(selector, keyfuncs[kfid].name,
2694 SWM_FUNCNAME_LEN) == 0) {
2695 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
2697 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
2698 setkeybinding(mod, ks, kfid, NULL);
2704 /* search by custom spawn name */
2705 for (i = 0; i < spawns_length; i++) {
2706 if (strcasecmp(selector, spawns[i].name) == 0) {
2707 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
2709 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
2710 setkeybinding(mod, ks, kf_spawn_custom,
2717 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
2723 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
2724 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
2725 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
2726 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
2727 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
2728 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
2729 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
2730 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
2731 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
2732 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
2733 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
2734 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
2735 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
2736 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
2737 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
2738 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
2739 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
2740 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
2741 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
2742 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
2743 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
2744 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
2745 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
2746 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
2747 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
2748 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
2749 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
2750 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
2751 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
2752 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
2753 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
2754 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
2755 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
2756 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
2757 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
2758 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
2759 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
2760 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
2761 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
2762 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
2763 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
2764 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
2765 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
2766 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
2767 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
2768 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
2769 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
2770 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
2771 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
2772 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
2773 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
2774 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
2775 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
2778 updatenumlockmask(void)
2781 XModifierKeymap *modmap;
2783 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
2785 modmap = XGetModifierMapping(display);
2786 for (i = 0; i < 8; i++)
2787 for (j = 0; j < modmap->max_keypermod; j++)
2788 if (modmap->modifiermap[i * modmap->max_keypermod + j]
2789 == XKeysymToKeycode(display, XK_Num_Lock))
2790 numlockmask = (1 << i);
2792 XFreeModifiermap(modmap);
2798 unsigned int i, j, k;
2800 unsigned int modifiers[] =
2801 { 0, LockMask, numlockmask, numlockmask | LockMask };
2803 DNPRINTF(SWM_D_MISC, "grabkeys\n");
2804 updatenumlockmask();
2806 for (k = 0; k < ScreenCount(display); k++) {
2807 if (TAILQ_EMPTY(&screens[k].rl))
2809 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
2810 for (i = 0; i < keys_length; i++) {
2811 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
2812 for (j = 0; j < LENGTH(modifiers); j++)
2813 XGrabKey(display, code,
2814 keys[i].mod | modifiers[j],
2815 screens[k].root, True,
2816 GrabModeAsync, GrabModeAsync);
2822 grabbuttons(struct ws_win *win, int focused)
2825 unsigned int modifiers[] =
2826 { 0, LockMask, numlockmask, numlockmask|LockMask };
2828 updatenumlockmask();
2829 XUngrabButton(display, AnyButton, AnyModifier, win->id);
2831 for (i = 0; i < LENGTH(buttons); i++)
2832 if (buttons[i].action == client_click)
2833 for (j = 0; j < LENGTH(modifiers); j++)
2834 XGrabButton(display, buttons[i].button,
2835 buttons[i].mask | modifiers[j],
2836 win->id, False, BUTTONMASK,
2837 GrabModeAsync, GrabModeSync, None,
2840 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
2841 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
2844 const char *quirkname[] = {
2845 "NONE", /* config string for "no value" */
2853 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
2854 #define SWM_Q_WS "\n|+ \t"
2856 parsequirks(char *qstr, unsigned long *quirk)
2866 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
2868 cp += (long)strspn(cp, SWM_Q_WS);
2869 for (i = 0; i < LENGTH(quirkname); i++) {
2870 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
2871 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
2876 *quirk |= 1 << (i-1);
2880 if (i >= LENGTH(quirkname)) {
2881 DNPRINTF(SWM_D_QUIRK,
2882 "parsequirks: invalid quirk [%s]\n", name);
2889 setquirk(const char *class, const char *name, const int quirk)
2894 for (i = 0; i < quirks_length; i++) {
2895 if (!strcmp(quirks[i].class, class) &&
2896 !strcmp(quirks[i].name, name)) {
2899 DNPRINTF(SWM_D_QUIRK,
2900 "setquirk: delete #%d %s:%s\n",
2901 i, quirks[i].class, quirks[i].name);
2902 free(quirks[i].class);
2903 free(quirks[i].name);
2904 j = quirks_length - 1;
2906 quirks[i] = quirks[j];
2910 /* found: replace */
2911 DNPRINTF(SWM_D_QUIRK,
2912 "setquirk: replace #%d %s:%s\n",
2913 i, quirks[i].class, quirks[i].name);
2914 free(quirks[i].class);
2915 free(quirks[i].name);
2916 quirks[i].class = strdup(class);
2917 quirks[i].name = strdup(name);
2918 quirks[i].quirk = quirk;
2925 "error: setquirk: cannot find class/name combination");
2928 /* not found: add */
2929 if (quirks_size == 0 || quirks == NULL) {
2931 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
2932 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
2934 fprintf(stderr, "setquirk: malloc failed\n");
2938 } else if (quirks_length == quirks_size) {
2940 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
2941 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
2943 fprintf(stderr, "setquirk: realloc failed\n");
2948 if (quirks_length < quirks_size) {
2949 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
2950 j = quirks_length++;
2951 quirks[j].class = strdup(class);
2952 quirks[j].name = strdup(name);
2953 quirks[j].quirk = quirk;
2955 fprintf(stderr, "quirks array problem?\n");
2957 fprintf(stderr, "quirks array problem!\n");
2963 setconfquirk(char *selector, char *value, int flags)
2965 char *cp, *class, *name;
2967 unsigned long quirks;
2968 if (selector == NULL)
2970 if ((cp = strchr(selector, ':')) == NULL)
2975 if ((retval = parsequirks(value, &quirks)) == 0)
2976 setquirk(class, name, quirks);
2983 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
2984 setquirk("OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT);
2985 setquirk("OpenOffice.org 3.0", "VCLSalFrame", SWM_Q_FLOAT);
2986 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
2987 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
2988 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
2989 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
2990 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
2991 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
2992 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
2993 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
2994 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
2995 setquirk("pcb", "pcb", SWM_Q_FLOAT);
2998 /* conf file stuff */
2999 #define SWM_CONF_FILE "scrotwm.conf"
3001 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_STACK_ENABLED,
3002 SWM_S_CLOCK_ENABLED, SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE,
3003 SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
3004 SWM_S_TITLE_NAME_ENABLED, SWM_S_BAR_FONT, SWM_S_BAR_ACTION,
3005 SWM_S_SPAWN_TERM, SWM_S_SS_APP, SWM_S_DIALOG_RATIO };
3008 setconfvalue(char *selector, char *value, int flags)
3011 case SWM_S_BAR_DELAY:
3012 bar_delay = atoi(value);
3014 case SWM_S_BAR_ENABLED:
3015 bar_enabled = atoi(value);
3017 case SWM_S_STACK_ENABLED:
3018 stack_enabled = atoi(value);
3020 case SWM_S_CLOCK_ENABLED:
3021 clock_enabled = atoi(value);
3023 case SWM_S_CYCLE_EMPTY:
3024 cycle_empty = atoi(value);
3026 case SWM_S_CYCLE_VISIBLE:
3027 cycle_visible = atoi(value);
3029 case SWM_S_SS_ENABLED:
3030 ss_enabled = atoi(value);
3032 case SWM_S_TERM_WIDTH:
3033 term_width = atoi(value);
3035 case SWM_S_TITLE_CLASS_ENABLED:
3036 title_class_enabled = atoi(value);
3038 case SWM_S_TITLE_NAME_ENABLED:
3039 title_name_enabled = atoi(value);
3041 case SWM_S_BAR_FONT:
3043 if ((bar_fonts[0] = strdup(value)) == NULL)
3044 err(1, "setconfvalue: bar_font");
3046 case SWM_S_BAR_ACTION:
3048 if ((bar_argv[0] = strdup(value)) == NULL)
3049 err(1, "setconfvalue: bar_action");
3051 case SWM_S_SPAWN_TERM:
3052 free(spawn_term[0]);
3053 if ((spawn_term[0] = strdup(value)) == NULL)
3054 err(1, "setconfvalue: spawn_term");
3058 case SWM_S_DIALOG_RATIO:
3059 dialog_ratio = atof(value);
3060 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
3070 setconfmodkey(char *selector, char *value, int flags)
3072 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
3073 update_modkey(Mod1Mask);
3074 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
3075 update_modkey(Mod2Mask);
3076 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
3077 update_modkey(Mod3Mask);
3078 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
3079 update_modkey(Mod4Mask);
3086 setconfcolor(char *selector, char *value, int flags)
3088 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
3093 setconfregion(char *selector, char *value, int flags)
3095 custom_region(value);
3099 /* config options */
3100 struct config_option {
3102 int (*func)(char*, char*, int);
3105 struct config_option configopt[] = {
3106 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
3107 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
3108 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
3109 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
3110 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
3111 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
3112 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
3113 { "bind", setconfbinding, 0 },
3114 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
3115 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
3116 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
3117 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
3118 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
3119 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
3120 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
3121 { "modkey", setconfmodkey, 0 },
3122 { "program", setconfspawn, 0 },
3123 { "quirk", setconfquirk, 0 },
3124 { "region", setconfregion, 0 },
3125 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
3126 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
3127 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
3128 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
3129 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
3130 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED }
3135 conf_load(char *filename)
3138 char *line, *cp, *optsub, *optval;
3139 size_t linelen, lineno = 0;
3140 int wordlen, i, optind;
3141 struct config_option *opt;
3143 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
3145 if (filename == NULL) {
3146 fprintf(stderr, "conf_load: no filename\n");
3149 if ((config = fopen(filename, "r")) == NULL) {
3150 warn("conf_load: fopen");
3154 while (!feof(config)) {
3155 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
3158 err(1, "%s", filename);
3163 cp += strspn(cp, " \t\n"); /* eat whitespace */
3164 if (cp[0] == '\0') {
3169 /* get config option */
3170 wordlen = strcspn(cp, "=[ \t\n");
3172 warnx("%s: line %zd: no option found",
3177 for (i = 0; i < LENGTH(configopt); i++) {
3178 opt = &configopt[i];
3179 if (!strncasecmp(cp, opt->optname, wordlen) &&
3180 strlen(opt->optname) == wordlen) {
3186 warnx("%s: line %zd: unknown option %.*s",
3187 filename, lineno, wordlen, cp);
3191 cp += strspn(cp, " \t\n"); /* eat whitespace */
3192 /* get [selector] if any */
3196 wordlen = strcspn(cp, "]");
3199 warnx("%s: line %zd: syntax error",
3203 asprintf(&optsub, "%.*s", wordlen, cp);
3206 cp += strspn(cp, "] \t\n"); /* eat trailing */
3208 cp += strspn(cp, "= \t\n"); /* eat trailing */
3210 optval = strdup(cp);
3211 /* call function to deal with it all */
3212 if (configopt[optind].func(optsub, optval,
3213 configopt[optind].funcflags) != 0) {
3214 fprintf(stderr, "%s line %zd: %s\n",
3215 filename, lineno, line);
3216 errx(1, "%s: line %zd: invalid data for %s",
3217 filename, lineno, configopt[optind].optname);
3225 DNPRINTF(SWM_D_CONF, "conf_load end\n");
3231 manage_window(Window id)
3234 struct workspace *ws;
3235 struct ws_win *win, *ww;
3236 int format, i, ws_idx, n, border_me = 0;
3237 unsigned long nitems, bytes;
3238 Atom ws_idx_atom = 0, type;
3239 Atom *prot = NULL, *pp;
3240 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
3241 struct swm_region *r;
3246 if ((win = find_window(id)) != NULL)
3247 return (win); /* already being managed */
3249 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
3250 errx(1, "calloc: failed to allocate memory for new window");
3252 /* Get all the window data in one shot */
3253 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3255 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
3256 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
3257 XGetWindowAttributes(display, id, &win->wa);
3258 XGetWMNormalHints(display, id, &win->sh, &mask);
3259 XGetTransientForHint(display, id, &trans);
3261 win->transient = trans;
3262 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
3263 (unsigned)win->id, win->transient);
3265 /* get supported protocols */
3266 if (XGetWMProtocols(display, id, &prot, &n)) {
3267 for (i = 0, pp = prot; i < n; i++, pp++)
3269 win->can_delete = 1;
3275 * Figure out where to put the window. If it was previously assigned to
3276 * a workspace (either by spawn() or manually moving), and isn't
3277 * transient, * put it in the same workspace
3279 r = root_to_region(win->wa.root);
3280 if (prop && win->transient == 0) {
3281 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
3282 ws_idx = strtonum(prop, 0, 9, &errstr);
3284 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
3287 ws = &r->s->ws[ws_idx];
3290 /* this should launch transients in the same ws as parent */
3292 if ((ww = find_window(trans)) != NULL)
3299 "fix this bug mcbride\n");
3304 /* set up the window layout */
3307 win->s = r->s; /* this never changes */
3308 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
3310 win->g.w = win->wa.width;
3311 win->g.h = win->wa.height;
3312 win->g.x = win->wa.x;
3313 win->g.y = win->wa.y;
3315 /* Set window properties so we can remember this after reincarnation */
3316 if (ws_idx_atom && prop == NULL &&
3317 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
3318 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3320 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3321 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3325 if (XGetClassHint(display, win->id, &win->ch)) {
3326 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
3327 win->ch.res_class, win->ch.res_name);
3328 for (i = 0; i < quirks_length; i++){
3329 if (!strcmp(win->ch.res_class, quirks[i].class) &&
3330 !strcmp(win->ch.res_name, quirks[i].name)) {
3331 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
3332 win->ch.res_class, win->ch.res_name);
3333 if (quirks[i].quirk & SWM_Q_FLOAT)
3335 win->quirks = quirks[i].quirk;
3340 /* alter window position if quirky */
3341 if (win->quirks & SWM_Q_ANYWHERE) {
3342 win->manual = 1; /* don't center the quirky windows */
3343 bzero(&wc, sizeof wc);
3345 if (win->g.y < bar_height) {
3346 win->g.y = wc.y = bar_height;
3349 if (win->g.w + win->g.x > WIDTH(r)) {
3350 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
3356 /* Reset font sizes (the bruteforce way; no default keybinding). */
3357 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
3358 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3359 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3360 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3361 fake_keypress(win, XK_KP_Add, ShiftMask);
3366 bzero(&wc, sizeof wc);
3367 wc.border_width = 1;
3368 mask = CWBorderWidth;
3369 XConfigureWindow(display, win->id, mask, &wc);
3372 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
3373 PropertyChangeMask | StructureNotifyMask);
3375 set_win_state(win, NormalState);
3377 /* floaters need to be mapped if they are in the current workspace */
3378 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
3379 XMapRaised(display, win->id);
3385 unmanage_window(struct ws_win *win)
3387 struct workspace *ws;
3392 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
3395 TAILQ_REMOVE(&win->ws->winlist, win, entry);
3396 set_win_state(win, WithdrawnState);
3397 if (win->ch.res_class)
3398 XFree(win->ch.res_class);
3399 if (win->ch.res_name)
3400 XFree(win->ch.res_name);
3407 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
3415 XKeyEvent *ev = &e->xkey;
3417 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
3419 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
3420 for (i = 0; i < keys_length; i++)
3421 if (keysym == keys[i].keysym
3422 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
3423 && keyfuncs[keys[i].funcid].func) {
3424 if (keys[i].funcid == kf_spawn_custom)
3426 root_to_region(ev->root),
3427 &(keyfuncs[keys[i].funcid].args),
3431 keyfuncs[keys[i].funcid].func(
3432 root_to_region(ev->root),
3433 &(keyfuncs[keys[i].funcid].args)
3439 buttonpress(XEvent *e)
3441 XButtonPressedEvent *ev = &e->xbutton;
3446 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
3448 action = root_click;
3449 if ((win = find_window(ev->window)) == NULL)
3453 action = client_click;
3456 for (i = 0; i < LENGTH(buttons); i++)
3457 if (action == buttons[i].action && buttons[i].func &&
3458 buttons[i].button == ev->button &&
3459 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
3460 buttons[i].func(win, &buttons[i].args);
3464 configurerequest(XEvent *e)
3466 XConfigureRequestEvent *ev = &e->xconfigurerequest;
3471 if ((win = find_window(ev->window)) == NULL)
3475 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
3477 bzero(&wc, sizeof wc);
3480 wc.width = ev->width;
3481 wc.height = ev->height;
3482 wc.border_width = ev->border_width;
3483 wc.sibling = ev->above;
3484 wc.stack_mode = ev->detail;
3485 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
3487 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
3489 if (win->floating) {
3490 if (ev->value_mask & CWX)
3492 if (ev->value_mask & CWY)
3494 if (ev->value_mask & CWWidth)
3495 win->g.w = ev->width;
3496 if (ev->value_mask & CWHeight)
3497 win->g.h = ev->height;
3498 if (win->ws->r != NULL) {
3499 /* this seems to be full screen */
3500 if (win->g.w >= WIDTH(win->ws->r)) {
3502 win->g.w = WIDTH(win->ws->r);
3503 ev->value_mask |= CWX | CWWidth;
3505 if (win->g.h >= HEIGHT(win->ws->r)) {
3508 win->g.h = HEIGHT(win->ws->r);
3509 ev->value_mask |= CWY | CWHeight;
3512 if ((ev->value_mask & (CWX | CWY)) &&
3513 !(ev->value_mask & (CWWidth | CWHeight)))
3515 XMoveResizeWindow(display, win->id,
3516 win->g.x, win->g.y, win->g.w, win->g.h);
3523 configurenotify(XEvent *e)
3528 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
3529 e->xconfigure.window);
3531 XMapWindow(display, e->xconfigure.window);
3532 win = find_window(e->xconfigure.window);
3534 XGetWMNormalHints(display, win->id, &win->sh, &mask);
3536 XMapWindow(display, win->id);
3543 destroynotify(XEvent *e)
3545 struct ws_win *win, *winfocus = NULL;
3546 struct workspace *ws;
3547 struct ws_win_list *wl;
3549 XDestroyWindowEvent *ev = &e->xdestroywindow;
3551 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
3553 SWM_EV_PROLOGUE(display);
3555 if ((win = find_window(ev->window)) != NULL) {
3556 /* find a window to focus */
3560 /* if we are transient give focus to parent */
3562 winfocus = find_window(win->transient);
3563 else if (ws->focus == win) {
3564 /* if in max_stack try harder */
3565 if (ws->cur_layout->flags & SWM_L_FOCUSPREV)
3566 if (win != ws->focus && win != ws->focus_prev)
3567 winfocus = ws->focus_prev;
3569 /* fallback and normal handling */
3570 if (winfocus == NULL) {
3571 if (TAILQ_FIRST(wl) == win)
3572 winfocus = TAILQ_NEXT(win, entry);
3574 winfocus = TAILQ_PREV(ws->focus,
3575 ws_win_list, entry);
3576 if (winfocus == NULL)
3577 winfocus = TAILQ_LAST(wl,
3583 unmanage_window(win);
3586 focus_win(winfocus);
3590 SWM_EV_EPILOGUE(display);
3594 enternotify(XEvent *e)
3596 XCrossingEvent *ev = &e->xcrossing;
3599 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
3602 /* eat event(r) to prevent autofocus */
3607 * happens when a window is created or destroyed and the border
3608 * crosses the mouse pointer
3610 if (QLength(display))
3613 if ((win = find_window(ev->window)) != NULL) {
3614 if (win->ws->focus == win)
3623 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
3629 DNPRINTF(SWM_D_EVENT, "focusout: window: %lu\n", e->xfocus.window);
3633 mapnotify(XEvent *e)
3636 XMappingEvent *ev = &e->xmapping;
3638 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
3640 SWM_EV_PROLOGUE(display);
3642 win = find_window(ev->window);
3644 set_win_state(win, NormalState);
3646 XRefreshKeyboardMapping(ev);
3647 if (ev->request == MappingKeyboard)
3650 SWM_EV_EPILOGUE(display);
3654 maprequest(XEvent *e)
3657 struct swm_region *r;
3659 XMapRequestEvent *ev = &e->xmaprequest;
3660 XWindowAttributes wa;
3662 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
3663 e->xmaprequest.window);
3665 SWM_EV_PROLOGUE(display);
3667 if (!XGetWindowAttributes(display, ev->window, &wa))
3669 if (wa.override_redirect)
3672 manage_window(e->xmaprequest.window);
3676 /* make new win focused */
3677 win = find_window(ev->window);
3678 r = root_to_region(win->wa.root);
3680 if (win->ws == r->ws)
3684 SWM_EV_EPILOGUE(display);
3688 propertynotify(XEvent *e)
3691 XPropertyEvent *ev = &e->xproperty;
3693 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
3696 if (ev->state == PropertyDelete)
3697 return; /* ignore */
3698 win = find_window(ev->window);
3703 case XA_WM_NORMAL_HINTS:
3706 XGetWMNormalHints(display, win->id, &win->sh, &mask);
3707 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
3708 if (win->sh.flags & PMinSize) {
3709 win->g.w = win->sh.min_width;
3710 win->g.h = win->sh.min_height;
3711 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
3713 XMoveResizeWindow(display, win->id,
3714 win->g.x, win->g.y, win->g.w, win->g.h);
3723 unmapnotify(XEvent *e)
3725 struct ws_win *win, *winfocus = NULL;
3726 struct workspace *ws;
3728 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
3730 SWM_EV_PROLOGUE(display);
3732 /* determine if we need to help unmanage this window */
3733 win = find_window(e->xunmap.window);
3737 if (getstate(e->xunmap.window) == NormalState) {
3739 * this window does not have a destroy event but but it is no
3740 * longer visible due to the app unmapping it so unmanage it
3744 /* if we are max_stack try harder to focus on something */
3745 if (ws->cur_layout->flags & SWM_L_FOCUSPREV) {
3747 winfocus = find_window(win->transient);
3748 else if (win != ws->focus && win != ws->focus_prev)
3749 winfocus = ws->focus_prev;
3752 /* normal and fallback if haven't found anything to focus on */
3753 if (winfocus == NULL) {
3754 winfocus = TAILQ_PREV(win, ws_win_list, entry);
3755 if (TAILQ_FIRST(&ws->winlist) == win)
3756 winfocus = TAILQ_NEXT(win, entry);
3759 winfocus = TAILQ_PREV(ws->focus,
3760 ws_win_list, entry);
3761 if (winfocus == NULL)
3762 winfocus = TAILQ_LAST(&ws->winlist,
3767 /* trash window and refocus */
3768 unmanage_window(win);
3771 focus_win(winfocus);
3776 SWM_EV_EPILOGUE(display);
3780 visibilitynotify(XEvent *e)
3783 struct swm_region *r;
3785 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
3786 e->xvisibility.window);
3787 if (e->xvisibility.state == VisibilityUnobscured)
3788 for (i = 0; i < ScreenCount(display); i++)
3789 TAILQ_FOREACH(r, &screens[i].rl, entry)
3790 if (e->xvisibility.window == r->bar_window)
3795 xerror_start(Display *d, XErrorEvent *ee)
3802 xerror(Display *d, XErrorEvent *ee)
3804 /* fprintf(stderr, "error: %p %p\n", display, ee); */
3812 xerrorxlib = XSetErrorHandler(xerror_start);
3814 /* this causes an error if some other window manager is running */
3815 XSelectInput(display, DefaultRootWindow(display),
3816 SubstructureRedirectMask);
3817 XSync(display, False);
3821 XSetErrorHandler(xerror);
3822 XSync(display, False);
3827 new_region(struct swm_screen *s, int x, int y, int w, int h)
3829 struct swm_region *r, *n;
3830 struct workspace *ws = NULL;
3833 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
3834 s->idx, w, h, x, y);
3836 /* remove any conflicting regions */
3837 n = TAILQ_FIRST(&s->rl);
3840 n = TAILQ_NEXT(r, entry);
3841 if (X(r) < (x + w) &&
3842 (X(r) + WIDTH(r)) > x &&
3844 (Y(r) + HEIGHT(r)) > y) {
3845 XDestroyWindow(display, r->bar_window);
3846 TAILQ_REMOVE(&s->rl, r, entry);
3847 TAILQ_INSERT_TAIL(&s->orl, r, entry);
3851 /* search old regions for one to reuse */
3853 /* size + location match */
3854 TAILQ_FOREACH(r, &s->orl, entry)
3855 if (X(r) == x && Y(r) == y &&
3856 HEIGHT(r) == h && WIDTH(r) == w)
3860 TAILQ_FOREACH(r, &s->orl, entry)
3861 if (HEIGHT(r) == h && WIDTH(r) == w)
3865 TAILQ_REMOVE(&s->orl, r, entry);
3866 /* try to use old region's workspace */
3867 if (r->ws->r == NULL)
3870 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
3871 errx(1, "calloc: failed to allocate memory for screen");
3873 /* if we don't have a workspace already, find one */
3875 for (i = 0; i < SWM_WS_MAX; i++)
3876 if (s->ws[i].r == NULL) {
3883 errx(1, "no free workspaces\n");
3892 TAILQ_INSERT_TAIL(&s->rl, r, entry);
3898 #ifdef SWM_XRR_HAS_CRTC
3900 XRRScreenResources *sr;
3903 #endif /* SWM_XRR_HAS_CRTC */
3904 struct swm_region *r;
3907 if (i >= ScreenCount(display))
3908 errx(1, "invalid screen");
3910 /* remove any old regions */
3911 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
3912 r->ws->old_r = r->ws->r = NULL;
3913 XDestroyWindow(display, r->bar_window);
3914 TAILQ_REMOVE(&screens[i].rl, r, entry);
3915 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
3918 /* map virtual screens onto physical screens */
3919 #ifdef SWM_XRR_HAS_CRTC
3921 if (xrandr_support) {
3922 sr = XRRGetScreenResources(display, screens[i].root);
3924 new_region(&screens[i], 0, 0,
3925 DisplayWidth(display, i),
3926 DisplayHeight(display, i));
3930 for (c = 0, ci = NULL; c < ncrtc; c++) {
3931 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
3932 if (ci->noutput == 0)
3936 if (ci != NULL && ci->mode == None)
3937 new_region(&screens[i], 0, 0,
3938 DisplayWidth(display, i),
3939 DisplayHeight(display, i));
3941 new_region(&screens[i],
3942 ci->x, ci->y, ci->width, ci->height);
3945 XRRFreeCrtcInfo(ci);
3946 XRRFreeScreenResources(sr);
3948 #endif /* SWM_XRR_HAS_CRTC */
3950 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
3951 DisplayHeight(display, i));
3956 screenchange(XEvent *e) {
3957 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
3958 struct swm_region *r;
3961 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
3963 if (!XRRUpdateConfiguration(e))
3966 /* silly event doesn't include the screen index */
3967 for (i = 0; i < ScreenCount(display); i++)
3968 if (screens[i].root == xe->root)
3970 if (i >= ScreenCount(display))
3971 errx(1, "screenchange: screen not found\n");
3973 /* brute force for now, just re-enumerate the regions */
3976 /* add bars to all regions */
3977 for (i = 0; i < ScreenCount(display); i++)
3978 TAILQ_FOREACH(r, &screens[i].rl, entry)
3986 Window d1, d2, *wins = NULL;
3987 XWindowAttributes wa;
3990 int errorbase, major, minor;
3991 struct workspace *ws;
3995 if ((screens = calloc(ScreenCount(display),
3996 sizeof(struct swm_screen))) == NULL)
3997 errx(1, "calloc: screens");
3999 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4001 /* initial Xrandr setup */
4002 xrandr_support = XRRQueryExtension(display,
4003 &xrandr_eventbase, &errorbase);
4005 if (XRRQueryVersion(display, &major, &minor) && major < 1)
4008 /* map physical screens */
4009 for (i = 0; i < ScreenCount(display); i++) {
4010 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
4012 TAILQ_INIT(&screens[i].rl);
4013 TAILQ_INIT(&screens[i].orl);
4014 screens[i].root = RootWindow(display, i);
4016 /* set default colors */
4017 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
4018 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
4019 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
4020 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
4021 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
4023 /* init all workspaces */
4024 /* XXX these should be dynamically allocated too */
4025 for (j = 0; j < SWM_WS_MAX; j++) {
4026 ws = &screens[i].ws[j];
4032 TAILQ_INIT(&ws->winlist);
4034 for (k = 0; layouts[k].l_stack != NULL; k++)
4035 if (layouts[k].l_config != NULL)
4036 layouts[k].l_config(ws,
4037 SWM_ARG_ID_STACKINIT);
4038 ws->cur_layout = &layouts[0];
4040 /* grab existing windows (before we build the bars)*/
4041 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
4047 XRRSelectInput(display, screens[i].root,
4048 RRScreenChangeNotifyMask);
4050 /* attach windows to a region */
4051 /* normal windows */
4052 for (j = 0; j < no; j++) {
4053 XGetWindowAttributes(display, wins[j], &wa);
4054 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4055 wa.override_redirect ||
4056 XGetTransientForHint(display, wins[j], &d1))
4059 state = getstate(wins[j]);
4060 manage = state == NormalState || state == IconicState;
4061 if (wa.map_state == IsViewable || manage)
4062 manage_window(wins[j]);
4064 /* transient windows */
4065 for (j = 0; j < no; j++) {
4066 if (!XGetWindowAttributes(display, wins[j], &wa))
4069 state = getstate(wins[j]);
4070 manage = state == NormalState || state == IconicState;
4071 if (XGetTransientForHint(display, wins[j], &d1) &&
4073 manage_window(wins[j]);
4084 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
4086 err(1, "setup_globals: strdup");
4087 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
4089 err(1, "setup_globals: strdup");
4090 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
4092 err(1, "setup_globals: strdup");
4093 if ((spawn_term[0] = strdup("xterm")) == NULL)
4094 err(1, "setup_globals: strdup");
4101 Atom netwmcheck, netwmname, utf8_string;
4104 /* work around sun jdk bugs, code from wmname */
4105 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
4106 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
4107 utf8_string = XInternAtom(display, "UTF8_STRING", False);
4108 for (i = 0; i < ScreenCount(display); i++) {
4109 root = screens[i].root;
4110 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
4111 PropModeReplace, (unsigned char *)&root, 1);
4112 XChangeProperty(display, root, netwmname, utf8_string, 8,
4113 PropModeReplace, "LG3D", strlen("LG3D"));
4118 main(int argc, char *argv[])
4121 struct swm_region *r, *rr;
4122 struct ws_win *winfocus = NULL;
4123 char conf[PATH_MAX], *cfile = NULL;
4130 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
4131 SWM_VERSION, cvstag);
4132 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
4133 warnx("no locale support");
4135 if (!(display = XOpenDisplay(0)))
4136 errx(1, "can not open display");
4139 errx(1, "other wm running");
4141 /* handle some signale */
4142 installsignal(SIGINT, "INT");
4143 installsignal(SIGHUP, "HUP");
4144 installsignal(SIGQUIT, "QUIT");
4145 installsignal(SIGTERM, "TERM");
4146 installsignal(SIGCHLD, "CHLD");
4148 astate = XInternAtom(display, "WM_STATE", False);
4149 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
4150 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
4152 /* look for local and global conf file */
4153 pwd = getpwuid(getuid());
4155 errx(1, "invalid user %d", getuid());
4163 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
4164 if (stat(conf, &sb) != -1) {
4165 if (S_ISREG(sb.st_mode))
4168 /* try global conf file */
4169 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
4170 if (!stat(conf, &sb))
4171 if (S_ISREG(sb.st_mode))
4177 /* setup all bars */
4178 for (i = 0; i < ScreenCount(display); i++)
4179 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4180 if (winfocus == NULL)
4181 winfocus = TAILQ_FIRST(&r->ws->winlist);
4185 /* set some values to work around bad programs */
4191 xfd = ConnectionNumber(display);
4193 while (XPending(display)) {
4194 XNextEvent(display, &e);
4197 if (e.type < LASTEvent) {
4199 if (handler[e.type])
4200 handler[e.type](&e);
4202 DNPRINTF(SWM_D_EVENT,
4203 "win: %lu unknown event: %d\n",
4204 e.xany.window, e.type);
4206 switch (e.type - xrandr_eventbase) {
4207 case RRScreenChangeNotify:
4211 DNPRINTF(SWM_D_EVENT,
4212 "win: %lu unknown xrandr event: "
4213 "%d\n", e.xany.window, e.type);
4219 /* if we are being restarted go focus on first window */
4221 rr = TAILQ_FIRST(&screens[0].rl);
4222 /* move pointer to first screen if multi screen */
4223 if (ScreenCount(display) > 1 || outputs > 1)
4224 XWarpPointer(display, None, rr->s[0].root,
4225 0, 0, 0, 0, rr->g.x,
4226 rr->g.y + bar_enabled ? bar_height : 0);
4228 focus_win(winfocus);
4235 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
4237 DNPRINTF(SWM_D_MISC, "select failed");
4248 XCloseDisplay(display);