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.10"
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];
243 unsigned long quirks;
244 struct workspace *ws; /* always valid */
245 struct swm_screen *s; /* always valid, never changes */
246 XWindowAttributes wa;
250 TAILQ_HEAD(ws_win_list, ws_win);
252 /* user/key callable function IDs */
309 /* layout handlers */
311 void vertical_config(struct workspace *, int);
312 void vertical_stack(struct workspace *, struct swm_geometry *);
313 void horizontal_config(struct workspace *, int);
314 void horizontal_stack(struct workspace *, struct swm_geometry *);
315 void max_stack(struct workspace *, struct swm_geometry *);
317 void grabbuttons(struct ws_win *, int);
318 void new_region(struct swm_screen *, int, int, int, int);
319 void unmanage_window(struct ws_win *);
322 void (*l_stack)(struct workspace *, struct swm_geometry *);
323 void (*l_config)(struct workspace *, int);
325 #define SWM_L_FOCUSPREV (1<<0)
326 #define SWM_L_MAPONFOCUS (1<<1)
329 /* stack, configure */
330 { vertical_stack, vertical_config, 0, "[|]" },
331 { horizontal_stack, horizontal_config, 0, "[-]" },
333 SWM_L_FOCUSPREV | SWM_L_MAPONFOCUS, "[ ]"},
337 #define SWM_H_SLICE (32)
338 #define SWM_V_SLICE (32)
340 /* define work spaces */
342 int idx; /* workspace index */
343 int restack; /* restack on switch */
344 struct layout *cur_layout; /* current layout handlers */
345 struct ws_win *focus; /* may be NULL */
346 struct ws_win *focus_prev; /* may be NULL */
347 struct swm_region *r; /* may be NULL */
348 struct swm_region *old_r; /* may be NULL */
349 struct ws_win_list winlist; /* list of windows in ws */
353 int horizontal_msize;
355 int horizontal_stacks;
362 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
363 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
365 /* physical screen mapping */
366 #define SWM_WS_MAX (10) /* XXX Too small? */
368 int idx; /* screen index */
369 struct swm_region_list rl; /* list of regions on this screen */
370 struct swm_region_list orl; /* list of old regions */
372 struct workspace ws[SWM_WS_MAX];
378 } c[SWM_S_COLOR_MAX];
380 struct swm_screen *screens;
383 /* args to functions */
386 #define SWM_ARG_ID_FOCUSNEXT (0)
387 #define SWM_ARG_ID_FOCUSPREV (1)
388 #define SWM_ARG_ID_FOCUSMAIN (2)
389 #define SWM_ARG_ID_SWAPNEXT (3)
390 #define SWM_ARG_ID_SWAPPREV (4)
391 #define SWM_ARG_ID_SWAPMAIN (5)
392 #define SWM_ARG_ID_MASTERSHRINK (6)
393 #define SWM_ARG_ID_MASTERGROW (7)
394 #define SWM_ARG_ID_MASTERADD (8)
395 #define SWM_ARG_ID_MASTERDEL (9)
396 #define SWM_ARG_ID_STACKRESET (10)
397 #define SWM_ARG_ID_STACKINIT (11)
398 #define SWM_ARG_ID_CYCLEWS_UP (12)
399 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
400 #define SWM_ARG_ID_CYCLESC_UP (14)
401 #define SWM_ARG_ID_CYCLESC_DOWN (15)
402 #define SWM_ARG_ID_STACKINC (16)
403 #define SWM_ARG_ID_STACKDEC (17)
404 #define SWM_ARG_ID_SS_ALL (0)
405 #define SWM_ARG_ID_SS_WINDOW (1)
406 #define SWM_ARG_ID_DONTCENTER (0)
407 #define SWM_ARG_ID_CENTER (1)
408 #define SWM_ARG_ID_KILLWINDOW (0)
409 #define SWM_ARG_ID_DELETEWINDOW (1)
418 #define SWM_Q_FLOAT (1<<0) /* float this window */
419 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
420 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
421 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
422 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
424 int quirks_size = 0, quirks_length = 0;
425 struct quirk *quirks = NULL;
442 name = "ButtonPress";
445 name = "ButtonRelease";
448 name = "MotionNotify";
451 name = "EnterNotify";
454 name = "LeaveNotify";
463 name = "KeymapNotify";
469 name = "GraphicsExpose";
474 case VisibilityNotify:
475 name = "VisibilityNotify";
478 name = "CreateNotify";
481 name = "DestroyNotify";
484 name = "UnmapNotify";
493 name = "ReparentNotify";
495 case ConfigureNotify:
496 name = "ConfigureNotify";
498 case ConfigureRequest:
499 name = "ConfigureRequest";
502 name = "GravityNotify";
505 name = "ResizeRequest";
507 case CirculateNotify:
508 name = "CirculateNotify";
510 case CirculateRequest:
511 name = "CirculateRequest";
514 name = "PropertyNotify";
517 name = "SelectionClear";
519 case SelectionRequest:
520 name = "SelectionRequest";
522 case SelectionNotify:
523 name = "SelectionNotify";
526 name = "ColormapNotify";
529 name = "ClientMessage";
532 name = "MappingNotify";
537 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
539 e->xany.window, name, e->type, QLength(display));
541 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
543 e->xany.window, e->type, QLength(display));
547 #endif /* SWM_DEBUG */
549 void expose(XEvent *);
550 void keypress(XEvent *);
551 void buttonpress(XEvent *);
552 void configurerequest(XEvent *);
553 void configurenotify(XEvent *);
554 void destroynotify(XEvent *);
555 void enternotify(XEvent *);
556 void focusin(XEvent *);
557 void focusout(XEvent *);
558 void mapnotify(XEvent *);
559 void mappingnotify(XEvent *);
560 void maprequest(XEvent *);
561 void propertynotify(XEvent *);
562 void unmapnotify(XEvent *);
563 void visibilitynotify(XEvent *);
565 void (*handler[LASTEvent])(XEvent *) = {
567 [KeyPress] = keypress,
568 [ButtonPress] = buttonpress,
569 [ConfigureRequest] = configurerequest,
570 [ConfigureNotify] = configurenotify,
571 [DestroyNotify] = destroynotify,
572 [EnterNotify] = enternotify,
574 [FocusOut] = focusout,
575 [MapNotify] = mapnotify,
576 [MappingNotify] = mappingnotify,
577 [MapRequest] = maprequest,
578 [PropertyNotify] = propertynotify,
579 [UnmapNotify] = unmapnotify,
580 [VisibilityNotify] = visibilitynotify,
590 while ((pid = waitpid(WAIT_ANY, NULL, WNOHANG)) != -1) {
591 DNPRINTF(SWM_D_MISC, "reaping: %d\n", pid);
606 installsignal(int sig, char *name)
610 sa.sa_handler = sighdlr;
611 sigemptyset(&sa.sa_mask);
613 if (sigaction(sig, &sa, NULL) == -1)
614 err(1, "could not install %s handler", name);
618 name_to_color(char *colorname)
622 XColor screen_def, exact_def;
623 unsigned long result = 0;
624 char cname[32] = "#";
626 cmap = DefaultColormap(display, screens[0].idx);
627 status = XAllocNamedColor(display, cmap, colorname,
628 &screen_def, &exact_def);
630 strlcat(cname, colorname + 2, sizeof cname - 1);
631 status = XAllocNamedColor(display, cmap, cname, &screen_def,
635 result = screen_def.pixel;
637 fprintf(stderr, "color '%s' not found.\n", colorname);
643 setscreencolor(char *val, int i, int c)
645 if (i > 0 && i <= ScreenCount(display)) {
646 screens[i - 1].c[c].color = name_to_color(val);
647 free(screens[i - 1].c[c].name);
648 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
650 } else if (i == -1) {
651 for (i = 0; i < ScreenCount(display); i++) {
652 screens[i].c[c].color = name_to_color(val);
653 free(screens[i].c[c].name);
654 if ((screens[i].c[c].name = strdup(val)) == NULL)
658 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
659 i, ScreenCount(display));
663 custom_region(char *val)
665 unsigned int sidx, x, y, w, h;
667 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
668 errx(1, "invalid custom region, "
669 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
670 if (sidx < 1 || sidx > ScreenCount(display))
671 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
672 sidx, ScreenCount(display));
676 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
678 if (x < 0 || x > DisplayWidth(display, sidx) ||
679 y < 0 || y > DisplayHeight(display, sidx) ||
680 w + x > DisplayWidth(display, sidx) ||
681 h + y > DisplayHeight(display, sidx))
682 errx(1, "region %ux%u+%u+%u not within screen boundaries "
683 "(%ux%u)\n", w, h, x, y,
684 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
686 new_region(&screens[sidx], x, y, w, h);
690 socket_setnonblock(int fd)
694 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
695 err(1, "fcntl F_GETFL");
697 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
698 err(1, "fcntl F_SETFL");
702 bar_print(struct swm_region *r, char *s)
704 XClearWindow(display, r->bar_window);
705 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
706 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
715 bzero(bar_pipe, sizeof bar_pipe);
718 kill(bar_pid, SIGTERM);
721 strlcpy(bar_ext, "", sizeof bar_ext);
726 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
728 int do_class, do_name;
730 XClassHint *xch = NULL;
732 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
734 if ((xch = XAllocClassHint()) == NULL)
736 status = XGetClassHint(display, cur_focus->id, xch);
737 if (status == BadWindow || status == BadAlloc)
739 do_class = (title_class_enabled && xch->res_class != NULL);
740 do_name = (title_name_enabled && xch->res_name != NULL);
742 strlcat(s, xch->res_class, sz);
743 if (do_class && do_name)
746 strlcat(s, xch->res_name, sz);
759 struct swm_region *r;
763 char loc[SWM_BAR_MAX];
767 if (bar_enabled == 0)
769 if (bar_extra && bar_extra_running) {
770 /* ignore short reads; it'll correct itself */
771 while ((b = fgetln(stdin, &len)) != NULL)
772 if (b && b[len - 1] == '\n') {
774 strlcpy(bar_ext, b, sizeof bar_ext);
776 if (b == NULL && errno != EAGAIN) {
777 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
778 errno, strerror(errno));
782 strlcpy(bar_ext, "", sizeof bar_ext);
784 if (clock_enabled == 0)
785 strlcpy(s, "", sizeof s);
788 localtime_r(&tmt, &tm);
789 strftime(s, sizeof s, "%a %b %d %R %Z %Y ", &tm);
792 for (i = 0; i < ScreenCount(display); i++) {
794 TAILQ_FOREACH(r, &screens[i].rl, entry) {
796 bar_class_name(s, sizeof s, r->ws->focus);
799 stack = r->ws->cur_layout->name;
801 snprintf(loc, sizeof loc, "%d:%d %s %s %s %s",
802 x++, r->ws->idx + 1, stack, s, bar_ext,
817 bar_toggle(struct swm_region *r, union arg *args)
819 struct swm_region *tmpr;
820 int i, j, sc = ScreenCount(display);
822 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
825 for (i = 0; i < sc; i++)
826 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
827 XUnmapWindow(display, tmpr->bar_window);
829 for (i = 0; i < sc; i++)
830 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
831 XMapRaised(display, tmpr->bar_window);
833 bar_enabled = !bar_enabled;
834 for (i = 0; i < sc; i++)
835 for (j = 0; j < SWM_WS_MAX; j++)
836 screens[i].ws[j].restack = 1;
839 /* must be after stack */
846 XSetWindowAttributes wa;
847 struct swm_region *r;
850 /* do this here because the conf file is in memory */
851 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
852 /* launch external status app */
853 bar_extra_running = 1;
854 if (pipe(bar_pipe) == -1)
855 err(1, "pipe error");
856 socket_setnonblock(bar_pipe[0]);
857 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
858 if (dup2(bar_pipe[0], 0) == -1)
860 if (dup2(bar_pipe[1], 1) == -1)
862 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
863 err(1, "could not disable SIGPIPE");
864 switch (bar_pid = fork()) {
866 err(1, "cannot fork");
870 execvp(bar_argv[0], bar_argv);
871 err(1, "%s external app failed", bar_argv[0]);
873 default: /* parent */
879 bzero(&wa, sizeof wa);
880 for (i = 0; i < ScreenCount(display); i++)
881 TAILQ_FOREACH(r, &screens[i].rl, entry) {
883 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
884 wa.background_pixel =
885 screens[i].c[SWM_S_COLOR_BAR].color;
886 XChangeWindowAttributes(display, r->bar_window,
887 CWBackPixel | CWBorderPixel, &wa);
893 bar_setup(struct swm_region *r)
897 for (i = 0; bar_fonts[i] != NULL; i++) {
898 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
904 if (bar_fonts[i] == NULL)
905 errx(1, "couldn't load font");
906 bar_height = bar_fs->ascent + bar_fs->descent + 3;
908 r->bar_window = XCreateSimpleWindow(display,
909 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
910 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
911 r->s->c[SWM_S_COLOR_BAR].color);
912 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
913 XSetFont(display, bar_gc, bar_fs->fid);
914 XSelectInput(display, r->bar_window, VisibilityChangeMask);
916 XMapRaised(display, r->bar_window);
917 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
919 if (signal(SIGALRM, bar_signal) == SIG_ERR)
920 err(1, "could not install bar_signal");
925 set_win_notify_cb(Display *d, XEvent *e, char *arg)
927 struct ws_win *win = (struct ws_win *)arg;
929 if (win && win->id == e->xany.window && e->xany.type == PropertyNotify)
935 set_win_state(struct ws_win *win, long state)
937 long data[] = {state, None};
939 XWindowAttributes wa;
941 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
943 /* make sure we drain everything */
944 XSync(display, True);
946 /* make sure we still exist too */
947 if (XGetWindowAttributes(display, win->id, &wa) == BadWindow)
950 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
951 (unsigned char *)data, 2);
953 /* wait for completion of XChangeProperty */
954 while (XCheckIfEvent(display, &ev, set_win_notify_cb, (char *)win))
963 unsigned char *p = NULL;
964 unsigned long n, extra;
967 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
968 &real, &format, &n, &extra, (unsigned char **)&p);
969 if (status != Success)
972 result = *((long *)p);
978 version(struct swm_region *r, union arg *args)
980 bar_version = !bar_version;
982 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
983 SWM_VERSION, cvstag);
985 strlcpy(bar_vertext, "", sizeof bar_vertext);
990 client_msg(struct ws_win *win, Atom a)
992 XClientMessageEvent cm;
994 bzero(&cm, sizeof cm);
995 cm.type = ClientMessage;
997 cm.message_type = aprot;
1000 cm.data.l[1] = CurrentTime;
1001 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1005 config_win(struct ws_win *win)
1009 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
1010 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1011 ce.type = ConfigureNotify;
1012 ce.display = display;
1014 ce.window = win->id;
1017 ce.width = win->g.w;
1018 ce.height = win->g.h;
1019 ce.border_width = 1; /* XXX store this! */
1021 ce.override_redirect = False;
1022 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1026 count_win(struct workspace *ws, int count_transient)
1033 * Under stress conditions windows sometimes do not get removed from
1034 * the managed list quickly enough. Use a very large hammer to get rid
1035 * of them. A smaller hammer would be nice.
1037 TAILQ_FOREACH(win, &ws->winlist, entry) {
1038 state = getstate(win->id);
1040 DNPRINTF(SWM_D_MISC, "count_win:removing: %lu\n",
1042 unmanage_window(win);
1046 TAILQ_FOREACH(win, &ws->winlist, entry) {
1047 if (count_transient == 0 && win->floating)
1049 if (count_transient == 0 && win->transient)
1053 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1059 quit(struct swm_region *r, union arg *args)
1061 DNPRINTF(SWM_D_MISC, "quit\n");
1066 unmap_window_cb(Display *d, XEvent *e, char *arg)
1068 struct ws_win *win = (struct ws_win *)arg;
1070 if (win && win->id == e->xany.window && e->xany.type == UnmapNotify)
1076 unmap_window(struct ws_win *win)
1079 XWindowAttributes wa;
1084 /* make sure we still exist too */
1085 if (XGetWindowAttributes(display, win->id, &wa) == BadWindow)
1088 /* don't unmap again */
1089 if (wa.map_state == IsUnmapped && getstate(win->id) == IconicState)
1092 set_win_state(win, IconicState);
1093 XUnmapWindow(display, win->id);
1095 /* make sure we wait for XUnmapWindow completion */
1096 while (XCheckIfEvent(display, &ev, unmap_window_cb, (char *)win))
1106 for (i = 0; i < ScreenCount(display); i++)
1107 for (j = 0; j < SWM_WS_MAX; j++)
1108 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1113 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1117 event.display = display; /* Ignored, but what the hell */
1118 event.window = win->id;
1119 event.root = win->s->root;
1120 event.subwindow = None;
1121 event.time = CurrentTime;
1126 event.same_screen = True;
1127 event.keycode = XKeysymToKeycode(display, keysym);
1128 event.state = modifiers;
1130 event.type = KeyPress;
1131 XSendEvent(event.display, event.window, True,
1132 KeyPressMask, (XEvent *)&event);
1134 event.type = KeyRelease;
1135 XSendEvent(event.display, event.window, True,
1136 KeyPressMask, (XEvent *)&event);
1141 restart(struct swm_region *r, union arg *args)
1143 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1145 /* disable alarm because the following code may not be interrupted */
1147 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1148 errx(1, "can't disable alarm");
1153 XCloseDisplay(display);
1154 execvp(start_argv[0], start_argv);
1155 fprintf(stderr, "execvp failed\n");
1161 root_to_region(Window root)
1163 struct swm_region *r = NULL;
1165 int i, x, y, wx, wy;
1168 for (i = 0; i < ScreenCount(display); i++)
1169 if (screens[i].root == root)
1172 if (XQueryPointer(display, screens[i].root,
1173 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1174 /* choose a region based on pointer location */
1175 TAILQ_FOREACH(r, &screens[i].rl, entry)
1176 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1177 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1182 r = TAILQ_FIRST(&screens[i].rl);
1188 find_window(Window id)
1193 for (i = 0; i < ScreenCount(display); i++)
1194 for (j = 0; j < SWM_WS_MAX; j++)
1195 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1202 spawn(struct swm_region *r, union arg *args)
1207 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1209 * The double-fork construct avoids zombie processes and keeps the code
1210 * clean from stupid signal handlers.
1215 close(ConnectionNumber(display));
1216 setenv("LD_PRELOAD", SWM_LIB, 1);
1217 if (asprintf(&ret, "%d", r->ws->idx)) {
1218 setenv("_SWM_WS", ret, 1);
1221 if (asprintf(&ret, "%d", getpid())) {
1222 setenv("_SWM_PID", ret, 1);
1226 /* kill stdin, mplayer, ssh-add etc. need that */
1227 si = open("/dev/null", O_RDONLY, 0);
1229 err(1, "open /dev/null");
1230 if (dup2(si, 0) == -1)
1231 err(1, "dup2 /dev/null");
1232 execvp(args->argv[0], args->argv);
1233 fprintf(stderr, "execvp failed\n");
1241 spawnterm(struct swm_region *r, union arg *args)
1243 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1246 setenv("_SWM_XTERM_FONTADJ", "", 1);
1251 unfocus_win(struct ws_win *win)
1256 if (win->ws->r == NULL)
1259 if (win->ws->focus == win) {
1260 win->ws->focus = NULL;
1261 win->ws->focus_prev = win;
1264 grabbuttons(win, 0);
1265 XSetWindowBorder(display, win->id,
1266 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1275 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
1277 for (i = 0; i < ScreenCount(display); i++)
1278 for (j = 0; j < SWM_WS_MAX; j++)
1279 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1284 focus_win(struct ws_win *win)
1286 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1291 /* use big hammer to make sure it works under all use cases */
1293 win->ws->focus = win;
1295 if (win->ws->r != NULL) {
1296 XSetWindowBorder(display, win->id,
1297 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1298 grabbuttons(win, 1);
1299 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS)
1300 XMapRaised(display, win->id);
1301 XSetInputFocus(display, win->id,
1302 RevertToPointerRoot, CurrentTime);
1307 switchws(struct swm_region *r, union arg *args)
1309 int wsid = args->id;
1310 struct swm_region *this_r, *other_r;
1311 struct ws_win *win, *winfocus = NULL, *parent = NULL;
1312 struct workspace *new_ws, *old_ws;
1315 old_ws = this_r->ws;
1316 new_ws = &this_r->s->ws[wsid];
1318 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1319 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1322 if (new_ws == old_ws)
1325 /* get focus window */
1327 winfocus = new_ws->focus;
1328 else if (new_ws->focus_prev)
1329 winfocus = new_ws->focus_prev;
1331 winfocus = TAILQ_FIRST(&new_ws->winlist);
1333 other_r = new_ws->r;
1334 if (other_r == NULL) {
1335 /* if the other workspace is hidden, switch windows */
1336 if (old_ws->r != NULL)
1337 old_ws->old_r = old_ws->r;
1339 old_ws->restack = 1;
1342 * Map new windows first if they were here before
1343 * to minimize ugly blinking.
1345 if (new_ws->old_r == this_r)
1346 TAILQ_FOREACH(win, &new_ws->winlist, entry)
1347 if (!(win->ws->cur_layout->flags &
1349 XMapRaised(display, win->id);
1351 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1354 other_r->ws = old_ws;
1355 old_ws->r = other_r;
1357 this_r->ws = new_ws;
1363 /* make sure we see the parent window */
1364 if (winfocus->transient) {
1365 parent = find_window(winfocus->transient);
1370 focus_win(winfocus);
1377 cyclews(struct swm_region *r, union arg *args)
1380 struct swm_screen *s = r->s;
1382 DNPRINTF(SWM_D_WS, "cyclews id %d "
1383 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1384 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1389 case SWM_ARG_ID_CYCLEWS_UP:
1390 if (a.id < SWM_WS_MAX - 1)
1395 case SWM_ARG_ID_CYCLEWS_DOWN:
1399 a.id = SWM_WS_MAX - 1;
1405 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1407 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1411 } while (a.id != r->ws->idx);
1415 cyclescr(struct swm_region *r, union arg *args)
1417 struct swm_region *rr = NULL;
1418 struct workspace *ws = NULL;
1419 struct ws_win *winfocus = NULL;
1422 /* do nothing if we don't have more than one screen */
1423 if (!(ScreenCount(display) > 1 || outputs > 1))
1428 case SWM_ARG_ID_CYCLESC_UP:
1429 rr = TAILQ_NEXT(r, entry);
1431 rr = TAILQ_FIRST(&screens[i].rl);
1433 case SWM_ARG_ID_CYCLESC_DOWN:
1434 rr = TAILQ_PREV(r, swm_region_list, entry);
1436 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1445 winfocus = ws->focus;
1446 if (winfocus == NULL)
1447 winfocus = ws->focus_prev;
1449 /* use window coordinates */
1450 x = winfocus->g.x + 1;
1451 y = winfocus->g.y + 1;
1453 /* use region coordinates */
1455 y = rr->g.y + 1 + bar_enabled ? bar_height : 0;
1459 XSetInputFocus(display, PointerRoot, RevertToPointerRoot, CurrentTime);
1460 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1462 focus_win(winfocus);
1466 swapwin(struct swm_region *r, union arg *args)
1468 struct ws_win *target, *source;
1469 struct ws_win *cur_focus;
1470 struct ws_win_list *wl;
1473 DNPRINTF(SWM_D_WS, "swapwin id %d "
1474 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1475 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1477 cur_focus = r->ws->focus;
1478 if (cur_focus == NULL)
1482 wl = &source->ws->winlist;
1485 case SWM_ARG_ID_SWAPPREV:
1486 target = TAILQ_PREV(source, ws_win_list, entry);
1487 TAILQ_REMOVE(wl, cur_focus, entry);
1489 TAILQ_INSERT_TAIL(wl, source, entry);
1491 TAILQ_INSERT_BEFORE(target, source, entry);
1493 case SWM_ARG_ID_SWAPNEXT:
1494 target = TAILQ_NEXT(source, entry);
1495 TAILQ_REMOVE(wl, source, entry);
1497 TAILQ_INSERT_HEAD(wl, source, entry);
1499 TAILQ_INSERT_AFTER(wl, target, source, entry);
1501 case SWM_ARG_ID_SWAPMAIN:
1502 target = TAILQ_FIRST(wl);
1503 if (target == source) {
1504 if (source->ws->focus_prev != NULL &&
1505 source->ws->focus_prev != target)
1507 source = source->ws->focus_prev;
1511 source->ws->focus_prev = target;
1512 TAILQ_REMOVE(wl, target, entry);
1513 TAILQ_INSERT_BEFORE(source, target, entry);
1514 TAILQ_REMOVE(wl, source, entry);
1515 TAILQ_INSERT_HEAD(wl, source, entry);
1518 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1527 focus(struct swm_region *r, union arg *args)
1529 struct ws_win *winfocus, *winlostfocus;
1530 struct ws_win_list *wl;
1531 struct ws_win *cur_focus;
1533 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1535 cur_focus = r->ws->focus;
1536 if (cur_focus == NULL)
1539 wl = &cur_focus->ws->winlist;
1541 winlostfocus = cur_focus;
1544 case SWM_ARG_ID_FOCUSPREV:
1545 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1546 if (winfocus == NULL)
1547 winfocus = TAILQ_LAST(wl, ws_win_list);
1550 case SWM_ARG_ID_FOCUSNEXT:
1551 winfocus = TAILQ_NEXT(cur_focus, entry);
1552 if (winfocus == NULL)
1553 winfocus = TAILQ_FIRST(wl);
1556 case SWM_ARG_ID_FOCUSMAIN:
1557 winfocus = TAILQ_FIRST(wl);
1558 if (winfocus == cur_focus)
1559 winfocus = cur_focus->ws->focus_prev;
1560 if (winfocus == NULL)
1568 if (winfocus == winlostfocus || winfocus == NULL)
1571 focus_win(winfocus);
1575 cycle_layout(struct swm_region *r, union arg *args)
1577 struct workspace *ws = r->ws;
1578 struct ws_win *winfocus, *parent = NULL;
1580 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1582 winfocus = ws->focus;
1585 if (ws->cur_layout->l_stack == NULL)
1586 ws->cur_layout = &layouts[0];
1590 /* make sure we see the parent window */
1592 if (winfocus->transient)
1593 parent = find_window(winfocus->transient);
1596 focus_win(winfocus);
1602 stack_config(struct swm_region *r, union arg *args)
1604 struct workspace *ws = r->ws;
1606 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1609 if (ws->cur_layout->l_config != NULL)
1610 ws->cur_layout->l_config(ws, args->id);
1612 if (args->id != SWM_ARG_ID_STACKINIT);
1618 struct swm_geometry g;
1619 struct swm_region *r;
1622 DNPRINTF(SWM_D_STACK, "stack\n");
1624 for (i = 0; i < ScreenCount(display); i++) {
1626 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1627 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1628 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1630 /* start with screen geometry, adjust for bar */
1640 r->ws->cur_layout->l_stack(r->ws, &g);
1648 stack_floater(struct ws_win *win, struct swm_region *r)
1656 bzero(&wc, sizeof wc);
1657 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1658 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w == WIDTH(r)) &&
1659 (win->g.h == HEIGHT(r)))
1660 wc.border_width = 0;
1662 wc.border_width = 1;
1663 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
1664 win->g.w = (double)WIDTH(r) * dialog_ratio;
1665 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1667 wc.width = win->g.w;
1668 wc.height = win->g.h;
1673 wc.x = (WIDTH(r) - win->g.w) / 2;
1674 wc.y = (HEIGHT(r) - win->g.h) / 2;
1677 /* adjust for region */
1681 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1682 win->id, wc.x, wc.y, wc.width, wc.height);
1684 XConfigureWindow(display, win->id, mask, &wc);
1688 * Send keystrokes to terminal to decrease/increase the font size as the
1689 * window size changes.
1692 adjust_font(struct ws_win *win)
1694 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
1695 win->floating || win->transient)
1698 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
1699 win->g.w / win->sh.width_inc < term_width &&
1700 win->font_steps < SWM_MAX_FONT_STEPS) {
1701 win->font_size_boundary[win->font_steps] =
1702 (win->sh.width_inc * term_width) + win->sh.base_width;
1705 win->last_inc = win->sh.width_inc;
1706 fake_keypress(win, XK_KP_Subtract, ShiftMask);
1707 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
1708 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
1711 win->last_inc = win->sh.width_inc;
1712 fake_keypress(win, XK_KP_Add, ShiftMask);
1716 #define SWAPXY(g) do { \
1718 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1719 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1722 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1725 struct swm_geometry win_g, r_g = *g;
1727 int i, j, s, stacks;
1728 int w_inc = 1, h_inc, w_base = 1, h_base;
1729 int hrh, extra = 0, h_slice, last_h = 0;
1730 int split, colno, winno, mwin, msize, mscale;
1731 int remain, missing, v_slice;
1734 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1735 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1737 winno = count_win(ws, 0);
1738 if (winno == 0 && count_win(ws, 1) == 0)
1741 TAILQ_FOREACH(win, &ws->winlist, entry)
1742 if (win->transient == 0 && win->floating == 0)
1749 w_inc = win->sh.width_inc;
1750 w_base = win->sh.base_width;
1751 mwin = ws->l_state.horizontal_mwin;
1752 mscale = ws->l_state.horizontal_msize;
1753 stacks = ws->l_state.horizontal_stacks;
1756 w_inc = win->sh.height_inc;
1757 w_base = win->sh.base_height;
1758 mwin = ws->l_state.vertical_mwin;
1759 mscale = ws->l_state.vertical_msize;
1760 stacks = ws->l_state.vertical_stacks;
1764 if (stacks > winno - mwin)
1765 stacks = winno - mwin;
1769 h_slice = r_g.h / SWM_H_SLICE;
1770 if (mwin && winno > mwin) {
1771 v_slice = r_g.w / SWM_V_SLICE;
1775 win_g.w = v_slice * mscale;
1777 if (w_inc > 1 && w_inc < v_slice) {
1778 /* adjust for window's requested size increment */
1779 remain = (win_g.w - w_base) % w_inc;
1780 missing = w_inc - remain;
1787 win_g.x += r_g.w - msize;
1790 colno = split = winno / stacks;
1791 win_g.w = ((r_g.w - (stacks * 2) + 2) / stacks);
1793 hrh = r_g.h / colno;
1794 extra = r_g.h - (colno * hrh);
1797 /* stack all the tiled windows */
1798 i = j = 0, s = stacks;
1799 TAILQ_FOREACH(win, &ws->winlist, entry) {
1800 if (win->transient != 0 || win->floating != 0)
1803 if (split && i == split) {
1804 colno = (winno - mwin) / stacks;
1805 if (s <= (winno - mwin) % stacks)
1807 split = split + colno;
1808 hrh = (r_g.h / colno);
1809 extra = r_g.h - (colno * hrh);
1813 win_g.x += win_g.w + 2;
1814 win_g.w = (r_g.w - msize - (stacks * 2)) / stacks;
1816 win_g.w += (r_g.w - msize - (stacks * 2)) %
1823 h_inc = win->sh.width_inc;
1824 h_base = win->sh.base_width;
1826 h_inc = win->sh.height_inc;
1827 h_base = win->sh.base_height;
1829 if (j == colno - 1) {
1830 win_g.h = hrh + extra;
1831 } else if (h_inc > 1 && h_inc < h_slice) {
1832 /* adjust for window's requested size increment */
1833 remain = (win_g.h - h_base) % h_inc;
1834 missing = h_inc - remain;
1836 if (missing <= extra || j == 0) {
1848 win_g.y += last_h + 2;
1850 bzero(&wc, sizeof wc);
1851 wc.border_width = 1;
1853 win->g.x = wc.x = win_g.y;
1854 win->g.y = wc.y = win_g.x;
1855 win->g.w = wc.width = win_g.h;
1856 win->g.h = wc.height = win_g.w;
1858 win->g.x = wc.x = win_g.x;
1859 win->g.y = wc.y = win_g.y;
1860 win->g.w = wc.width = win_g.w;
1861 win->g.h = wc.height = win_g.h;
1864 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1865 XConfigureWindow(display, win->id, mask, &wc);
1866 XMapRaised(display, win->id);
1874 /* now, stack all the floaters and transients */
1875 TAILQ_FOREACH(win, &ws->winlist, entry) {
1876 if (win->transient == 0 && win->floating == 0)
1879 stack_floater(win, ws->r);
1880 XMapRaised(display, win->id);
1885 vertical_config(struct workspace *ws, int id)
1887 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1890 case SWM_ARG_ID_STACKRESET:
1891 case SWM_ARG_ID_STACKINIT:
1892 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1893 ws->l_state.vertical_mwin = 1;
1894 ws->l_state.vertical_stacks = 1;
1896 case SWM_ARG_ID_MASTERSHRINK:
1897 if (ws->l_state.vertical_msize > 1)
1898 ws->l_state.vertical_msize--;
1900 case SWM_ARG_ID_MASTERGROW:
1901 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1902 ws->l_state.vertical_msize++;
1904 case SWM_ARG_ID_MASTERADD:
1905 ws->l_state.vertical_mwin++;
1907 case SWM_ARG_ID_MASTERDEL:
1908 if (ws->l_state.vertical_mwin > 0)
1909 ws->l_state.vertical_mwin--;
1911 case SWM_ARG_ID_STACKINC:
1912 ws->l_state.vertical_stacks++;
1914 case SWM_ARG_ID_STACKDEC:
1915 if (ws->l_state.vertical_stacks > 1)
1916 ws->l_state.vertical_stacks--;
1924 vertical_stack(struct workspace *ws, struct swm_geometry *g)
1926 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1928 stack_master(ws, g, 0, 0);
1932 horizontal_config(struct workspace *ws, int id)
1934 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1937 case SWM_ARG_ID_STACKRESET:
1938 case SWM_ARG_ID_STACKINIT:
1939 ws->l_state.horizontal_mwin = 1;
1940 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1941 ws->l_state.horizontal_stacks = 1;
1943 case SWM_ARG_ID_MASTERSHRINK:
1944 if (ws->l_state.horizontal_msize > 1)
1945 ws->l_state.horizontal_msize--;
1947 case SWM_ARG_ID_MASTERGROW:
1948 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1949 ws->l_state.horizontal_msize++;
1951 case SWM_ARG_ID_MASTERADD:
1952 ws->l_state.horizontal_mwin++;
1954 case SWM_ARG_ID_MASTERDEL:
1955 if (ws->l_state.horizontal_mwin > 0)
1956 ws->l_state.horizontal_mwin--;
1958 case SWM_ARG_ID_STACKINC:
1959 ws->l_state.horizontal_stacks++;
1961 case SWM_ARG_ID_STACKDEC:
1962 if (ws->l_state.horizontal_stacks > 1)
1963 ws->l_state.horizontal_stacks--;
1971 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
1973 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1975 stack_master(ws, g, 1, 0);
1978 /* fullscreen view */
1980 max_stack(struct workspace *ws, struct swm_geometry *g)
1983 struct swm_geometry gg = *g;
1984 struct ws_win *win, *wintrans = NULL;
1988 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1993 winno = count_win(ws, 0);
1994 if (winno == 0 && count_win(ws, 1) == 0)
1997 TAILQ_FOREACH(win, &ws->winlist, entry) {
1998 if (win->transient != 0) {
2001 bzero(&wc, sizeof wc);
2002 wc.border_width = 1;
2003 win->g.x = wc.x = gg.x;
2004 win->g.y = wc.y = gg.y;
2005 win->g.w = wc.width = gg.w;
2006 win->g.h = wc.height = gg.h;
2007 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2008 XConfigureWindow(display, win->id, mask, &wc);
2010 /* unmap only if we don't have multi screen */
2011 if (win != ws->focus)
2012 if (!(ScreenCount(display) > 1 || outputs > 1))
2017 /* put the last transient on top */
2019 stack_floater(wintrans, ws->r);
2020 focus_win(wintrans); /* override */
2025 send_to_ws(struct swm_region *r, union arg *args)
2027 int wsid = args->id;
2028 struct ws_win *win = win, *winfocus = NULL;
2029 struct workspace *ws, *nws;
2030 Atom ws_idx_atom = 0;
2031 unsigned char ws_idx_str[SWM_PROPLEN];
2040 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2043 nws = &win->s->ws[wsid];
2045 /* find a window to focus */
2046 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2047 if (TAILQ_FIRST(&ws->winlist) == win)
2048 winfocus = TAILQ_NEXT(win, entry);
2050 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2051 if (winfocus == NULL)
2052 winfocus = TAILQ_LAST(&ws->winlist, ws_win_list);
2054 /* out of windows in ws so focus on nws instead if we multi screen */
2055 if (winfocus == NULL)
2056 if (ScreenCount(display) > 1 || outputs > 1)
2060 TAILQ_REMOVE(&ws->winlist, win, entry);
2061 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2064 /* Try to update the window's workspace property */
2065 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2067 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2068 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2070 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2071 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2074 if (count_win(nws, 1) == 1)
2081 focus_win(winfocus);
2085 wkill(struct swm_region *r, union arg *args)
2087 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
2089 if(r->ws->focus == NULL)
2092 if (args->id == SWM_ARG_ID_KILLWINDOW)
2093 XKillClient(display, r->ws->focus->id);
2095 if (r->ws->focus->can_delete)
2096 client_msg(r->ws->focus, adelete);
2100 floating_toggle(struct swm_region *r, union arg *args)
2102 struct ws_win *win = r->ws->focus;
2107 win->floating = !win->floating;
2114 resize_window(struct ws_win *win, int center)
2118 struct swm_region *r;
2120 r = root_to_region(win->wa.root);
2121 bzero(&wc, sizeof wc);
2122 mask = CWBorderWidth | CWWidth | CWHeight;
2123 wc.border_width = 1;
2124 wc.width = win->g.w;
2125 wc.height = win->g.h;
2126 if (center == SWM_ARG_ID_CENTER) {
2127 wc.x = (WIDTH(r) - win->g.w) / 2;
2128 wc.y = (HEIGHT(r) - win->g.h) / 2;
2132 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
2133 win->id, wc.x, wc.y, wc.width, wc.height);
2135 XConfigureWindow(display, win->id, mask, &wc);
2140 resize(struct ws_win *win, union arg *args)
2145 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %d\n",
2146 win->id, win->floating, win->transient);
2148 if (!(win->transient != 0 || win->floating != 0))
2151 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2152 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2154 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w, win->g.h);
2156 XMaskEvent(display, MOUSEMASK | ExposureMask |
2157 SubstructureRedirectMask, &ev);
2159 case ConfigureRequest:
2162 handler[ev.type](&ev);
2165 if (ev.xmotion.x <= 1)
2167 if (ev.xmotion.y <= 1)
2169 win->g.w = ev.xmotion.x;
2170 win->g.h = ev.xmotion.y;
2172 /* not free, don't sync more than 60 times / second */
2173 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2174 time = ev.xmotion.time;
2175 XSync(display, False);
2176 resize_window(win, args->id);
2180 } while (ev.type != ButtonRelease);
2182 XSync(display, False);
2183 resize_window(win, args->id);
2185 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
2187 XUngrabPointer(display, CurrentTime);
2190 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2194 move_window(struct ws_win *win)
2198 struct swm_region *r;
2200 r = root_to_region(win->wa.root);
2201 bzero(&wc, sizeof wc);
2206 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
2207 win->id, wc.x, wc.y, wc.width, wc.height);
2209 XConfigureWindow(display, win->id, mask, &wc);
2214 move(struct ws_win *win, union arg *args)
2220 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %d\n",
2221 win->id, win->floating, win->transient);
2223 if (win->floating == 0) {
2229 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2230 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2232 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2234 XMaskEvent(display, MOUSEMASK | ExposureMask |
2235 SubstructureRedirectMask, &ev);
2237 case ConfigureRequest:
2240 handler[ev.type](&ev);
2243 win->g.x = ev.xmotion.x_root;
2244 win->g.y = ev.xmotion.y_root;
2246 /* not free, don't sync more than 60 times / second */
2247 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2248 time = ev.xmotion.time;
2249 XSync(display, False);
2254 } while (ev.type != ButtonRelease);
2256 XSync(display, False);
2259 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2260 XUngrabPointer(display, CurrentTime);
2265 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2268 /* key definitions */
2270 dummykeyfunc(struct swm_region *r, union arg *args)
2275 legacyfunc(struct swm_region *r, union arg *args)
2280 char name[SWM_FUNCNAME_LEN];
2281 void (*func)(struct swm_region *r, union arg *);
2283 } keyfuncs[kf_invalid + 1] = {
2284 /* name function argument */
2285 { "cycle_layout", cycle_layout, {0} },
2286 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
2287 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
2288 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
2289 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
2290 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
2291 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
2292 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
2293 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
2294 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
2295 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
2296 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
2297 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
2298 { "spawn_term", spawnterm, {.argv = spawn_term} },
2299 { "spawn_menu", legacyfunc, {0} },
2300 { "quit", quit, {0} },
2301 { "restart", restart, {0} },
2302 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
2303 { "ws_1", switchws, {.id = 0} },
2304 { "ws_2", switchws, {.id = 1} },
2305 { "ws_3", switchws, {.id = 2} },
2306 { "ws_4", switchws, {.id = 3} },
2307 { "ws_5", switchws, {.id = 4} },
2308 { "ws_6", switchws, {.id = 5} },
2309 { "ws_7", switchws, {.id = 6} },
2310 { "ws_8", switchws, {.id = 7} },
2311 { "ws_9", switchws, {.id = 8} },
2312 { "ws_10", switchws, {.id = 9} },
2313 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
2314 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
2315 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
2316 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
2317 { "mvws_1", send_to_ws, {.id = 0} },
2318 { "mvws_2", send_to_ws, {.id = 1} },
2319 { "mvws_3", send_to_ws, {.id = 2} },
2320 { "mvws_4", send_to_ws, {.id = 3} },
2321 { "mvws_5", send_to_ws, {.id = 4} },
2322 { "mvws_6", send_to_ws, {.id = 5} },
2323 { "mvws_7", send_to_ws, {.id = 6} },
2324 { "mvws_8", send_to_ws, {.id = 7} },
2325 { "mvws_9", send_to_ws, {.id = 8} },
2326 { "mvws_10", send_to_ws, {.id = 9} },
2327 { "bar_toggle", bar_toggle, {0} },
2328 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
2329 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
2330 { "screenshot_all", legacyfunc, {0} },
2331 { "screenshot_wind", legacyfunc, {0} },
2332 { "float_toggle", floating_toggle,{0} },
2333 { "version", version, {0} },
2334 { "spawn_lock", legacyfunc, {0} },
2335 { "spawn_initscr", legacyfunc, {0} },
2336 { "spawn_custom", dummykeyfunc, {0} },
2337 { "invalid key func", NULL, {0} },
2342 enum keyfuncid funcid;
2345 int keys_size = 0, keys_length = 0;
2346 struct key *keys = NULL;
2349 enum { client_click, root_click };
2351 unsigned int action;
2353 unsigned int button;
2354 void (*func)(struct ws_win *, union arg *);
2357 /* action key mouse button func args */
2358 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
2359 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
2360 { client_click, MODKEY, Button1, move, {0} },
2364 update_modkey(unsigned int mod)
2369 for (i = 0; i < keys_length; i++)
2370 if (keys[i].mod & ShiftMask)
2371 keys[i].mod = mod | ShiftMask;
2375 for (i = 0; i < LENGTH(buttons); i++)
2376 if (buttons[i].mask & ShiftMask)
2377 buttons[i].mask = mod | ShiftMask;
2379 buttons[i].mask = mod;
2389 int spawns_size = 0, spawns_length = 0;
2390 struct spawn_prog *spawns = NULL;
2393 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
2396 struct spawn_prog *prog = NULL;
2398 char *ap, **real_args;
2400 DNPRINTF(SWM_D_SPAWN, "spawn_custom %s\n", spawn_name);
2403 for (i = 0; i < spawns_length; i++) {
2404 if (!strcasecmp(spawn_name, spawns[i].name))
2408 fprintf(stderr, "spawn_custom: program %s not found\n",
2413 /* make room for expanded args */
2414 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
2415 err(1, "spawn_custom: calloc real_args");
2417 /* expand spawn_args into real_args */
2418 for (i = 0; i < prog->argc; i++) {
2420 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
2421 if (!strcasecmp(ap, "$bar_border")) {
2423 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
2425 err(1, "spawn_custom border color");
2426 } else if (!strcasecmp(ap, "$bar_color")) {
2428 strdup(r->s->c[SWM_S_COLOR_BAR].name))
2430 err(1, "spawn_custom bar color");
2431 } else if (!strcasecmp(ap, "$bar_font")) {
2432 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
2434 err(1, "spawn_custom bar fonts");
2435 } else if (!strcasecmp(ap, "$bar_font_color")) {
2437 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
2439 err(1, "spawn_custom color font");
2440 } else if (!strcasecmp(ap, "$color_focus")) {
2442 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
2444 err(1, "spawn_custom color focus");
2445 } else if (!strcasecmp(ap, "$color_unfocus")) {
2447 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
2449 err(1, "spawn_custom color unfocus");
2451 /* no match --> copy as is */
2452 if ((real_args[i] = strdup(ap)) == NULL)
2453 err(1, "spawn_custom strdup(ap)");
2455 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
2460 if ((swm_debug & SWM_D_SPAWN) != 0) {
2461 fprintf(stderr, "spawn_custom: result = ");
2462 for (i = 0; i < prog->argc; i++)
2463 fprintf(stderr, "\"%s\" ", real_args[i]);
2464 fprintf(stderr, "\n");
2470 for (i = 0; i < prog->argc; i++)
2476 setspawn(struct spawn_prog *prog)
2480 if (prog == NULL || prog->name == NULL)
2484 for (i = 0; i < spawns_length; i++) {
2485 if (!strcmp(spawns[i].name, prog->name)) {
2487 if (prog->argv == NULL) {
2489 DNPRINTF(SWM_D_SPAWN,
2490 "setspawn: delete #%d %s\n",
2492 free(spawns[i].name);
2493 for (j = 0; j < spawns[i].argc; j++)
2494 free(spawns[i].argv[j]);
2495 free(spawns[i].argv);
2496 j = spawns_length - 1;
2498 spawns[i] = spawns[j];
2503 DNPRINTF(SWM_D_SPAWN,
2504 "setspawn: replace #%d %s\n",
2506 free(spawns[i].name);
2507 for (j = 0; j < spawns[i].argc; j++)
2508 free(spawns[i].argv[j]);
2509 free(spawns[i].argv);
2512 /* found case handled */
2518 if (prog->argv == NULL) {
2520 "error: setspawn: cannot find program %s", prog->name);
2525 /* not found: add */
2526 if (spawns_size == 0 || spawns == NULL) {
2528 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
2529 spawns = malloc((size_t)spawns_size *
2530 sizeof(struct spawn_prog));
2531 if (spawns == NULL) {
2532 fprintf(stderr, "setspawn: malloc failed\n");
2536 } else if (spawns_length == spawns_size) {
2538 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
2539 spawns = realloc(spawns, (size_t)spawns_size *
2540 sizeof(struct spawn_prog));
2541 if (spawns == NULL) {
2542 fprintf(stderr, "setspawn: realloc failed\n");
2548 if (spawns_length < spawns_size) {
2549 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
2550 spawns_length, prog->name);
2551 i = spawns_length++;
2554 fprintf(stderr, "spawns array problem?\n");
2555 if (spawns == NULL) {
2556 fprintf(stderr, "spawns array is NULL!\n");
2564 setconfspawn(char *selector, char *value, int flags)
2566 struct spawn_prog *prog;
2567 char *vp, *cp, *word;
2569 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
2570 if ((prog = calloc(1, sizeof *prog)) == NULL)
2571 err(1, "setconfspawn: calloc prog");
2572 prog->name = strdup(selector);
2573 if (prog->name == NULL)
2574 err(1, "setconfspawn prog->name");
2575 if ((cp = vp = strdup(value)) == NULL)
2576 err(1, "setconfspawn: strdup(value) ");
2577 while ((word = strsep(&cp, " \t")) != NULL) {
2578 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
2580 cp += (long)strspn(cp, " \t");
2581 if (strlen(word) > 0) {
2583 prog->argv = realloc(prog->argv,
2584 prog->argc * sizeof(char *));
2585 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
2586 err(1, "setconfspawn: strdup");
2593 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
2600 setconfspawn("term", "xterm", 0);
2601 setconfspawn("screenshot_all", "screenshot.sh full", 0);
2602 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
2603 setconfspawn("lock", "xlock", 0);
2604 setconfspawn("initscr", "initscreen.sh", 0);
2605 setconfspawn("menu", "dmenu_run"
2608 " -nf $bar_font_color"
2610 " -sf $bar_color", 0);
2614 #define SWM_MODNAME_SIZE 32
2615 #define SWM_KEY_WS "\n+ \t"
2617 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
2621 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
2622 if (mod == NULL || ks == NULL) {
2623 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
2626 if (keystr == NULL || strlen(keystr) == 0) {
2627 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
2633 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
2634 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
2636 cp += (long)strspn(cp, SWM_KEY_WS);
2637 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
2639 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
2641 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
2643 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
2645 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
2647 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
2649 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
2650 *mod |= ControlMask;
2652 *ks = XStringToKeysym(name);
2653 XConvertCase(*ks, ks, &uks);
2654 if (ks == NoSymbol) {
2656 "parsekeys: invalid key %s\n",
2662 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
2667 strdupsafe(char *str)
2672 return (strdup(str));
2676 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
2679 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
2680 keyfuncs[kfid].name, spawn_name);
2682 for (i = 0; i < keys_length; i++) {
2683 if (keys[i].mod == mod && keys[i].keysym == ks) {
2684 if (kfid == kf_invalid) {
2687 "setkeybinding: delete #%d %s\n",
2688 i, keyfuncs[keys[i].funcid].name);
2689 free(keys[i].spawn_name);
2690 j = keys_length - 1;
2694 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2697 /* found: replace */
2699 "setkeybinding: replace #%d %s %s\n",
2700 i, keyfuncs[keys[i].funcid].name,
2702 free(keys[i].spawn_name);
2704 keys[i].keysym = ks;
2705 keys[i].funcid = kfid;
2706 keys[i].spawn_name = strdupsafe(spawn_name);
2707 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2712 if (kfid == kf_invalid) {
2714 "error: setkeybinding: cannot find mod/key combination");
2715 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2718 /* not found: add */
2719 if (keys_size == 0 || keys == NULL) {
2721 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
2722 keys = malloc((size_t)keys_size * sizeof(struct key));
2724 fprintf(stderr, "malloc failed\n");
2728 } else if (keys_length == keys_size) {
2730 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
2731 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
2733 fprintf(stderr, "realloc failed\n");
2738 if (keys_length < keys_size) {
2740 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
2741 j, keyfuncs[kfid].name, spawn_name);
2743 keys[j].keysym = ks;
2744 keys[j].funcid = kfid;
2745 keys[j].spawn_name = strdupsafe(spawn_name);
2747 fprintf(stderr, "keys array problem?\n");
2749 fprintf(stderr, "keys array problem\n");
2753 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2757 setconfbinding(char *selector, char *value, int flags)
2759 enum keyfuncid kfid;
2763 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
2764 if (selector == NULL) {
2765 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
2766 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
2768 setkeybinding(mod, ks, kfid, NULL);
2773 /* search by key function name */
2774 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
2775 if (strncasecmp(selector, keyfuncs[kfid].name,
2776 SWM_FUNCNAME_LEN) == 0) {
2777 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
2779 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
2780 setkeybinding(mod, ks, kfid, NULL);
2786 /* search by custom spawn name */
2787 for (i = 0; i < spawns_length; i++) {
2788 if (strcasecmp(selector, spawns[i].name) == 0) {
2789 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
2791 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
2792 setkeybinding(mod, ks, kf_spawn_custom,
2799 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
2806 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
2807 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
2808 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
2809 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
2810 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
2811 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
2812 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
2813 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
2814 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
2815 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
2816 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
2817 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
2818 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
2819 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
2820 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
2821 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
2822 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
2823 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
2824 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
2825 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
2826 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
2827 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
2828 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
2829 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
2830 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
2831 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
2832 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
2833 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
2834 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
2835 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
2836 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
2837 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
2838 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
2839 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
2840 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
2841 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
2842 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
2843 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
2844 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
2845 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
2846 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
2847 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
2848 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
2849 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
2850 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
2851 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
2852 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
2853 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
2854 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
2855 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
2856 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
2857 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
2858 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
2862 updatenumlockmask(void)
2865 XModifierKeymap *modmap;
2867 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
2869 modmap = XGetModifierMapping(display);
2870 for (i = 0; i < 8; i++)
2871 for (j = 0; j < modmap->max_keypermod; j++)
2872 if (modmap->modifiermap[i * modmap->max_keypermod + j]
2873 == XKeysymToKeycode(display, XK_Num_Lock))
2874 numlockmask = (1 << i);
2876 XFreeModifiermap(modmap);
2882 unsigned int i, j, k;
2884 unsigned int modifiers[] =
2885 { 0, LockMask, numlockmask, numlockmask | LockMask };
2887 DNPRINTF(SWM_D_MISC, "grabkeys\n");
2888 updatenumlockmask();
2890 for (k = 0; k < ScreenCount(display); k++) {
2891 if (TAILQ_EMPTY(&screens[k].rl))
2893 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
2894 for (i = 0; i < keys_length; i++) {
2895 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
2896 for (j = 0; j < LENGTH(modifiers); j++)
2897 XGrabKey(display, code,
2898 keys[i].mod | modifiers[j],
2899 screens[k].root, True,
2900 GrabModeAsync, GrabModeAsync);
2906 grabbuttons(struct ws_win *win, int focused)
2909 unsigned int modifiers[] =
2910 { 0, LockMask, numlockmask, numlockmask|LockMask };
2912 updatenumlockmask();
2913 XUngrabButton(display, AnyButton, AnyModifier, win->id);
2915 for (i = 0; i < LENGTH(buttons); i++)
2916 if (buttons[i].action == client_click)
2917 for (j = 0; j < LENGTH(modifiers); j++)
2918 XGrabButton(display, buttons[i].button,
2919 buttons[i].mask | modifiers[j],
2920 win->id, False, BUTTONMASK,
2921 GrabModeAsync, GrabModeSync, None,
2924 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
2925 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
2928 const char *quirkname[] = {
2929 "NONE", /* config string for "no value" */
2937 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
2938 #define SWM_Q_WS "\n|+ \t"
2940 parsequirks(char *qstr, unsigned long *quirk)
2950 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
2952 cp += (long)strspn(cp, SWM_Q_WS);
2953 for (i = 0; i < LENGTH(quirkname); i++) {
2954 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
2955 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
2960 *quirk |= 1 << (i-1);
2964 if (i >= LENGTH(quirkname)) {
2965 DNPRINTF(SWM_D_QUIRK,
2966 "parsequirks: invalid quirk [%s]\n", name);
2974 setquirk(const char *class, const char *name, const int quirk)
2979 for (i = 0; i < quirks_length; i++) {
2980 if (!strcmp(quirks[i].class, class) &&
2981 !strcmp(quirks[i].name, name)) {
2984 DNPRINTF(SWM_D_QUIRK,
2985 "setquirk: delete #%d %s:%s\n",
2986 i, quirks[i].class, quirks[i].name);
2987 free(quirks[i].class);
2988 free(quirks[i].name);
2989 j = quirks_length - 1;
2991 quirks[i] = quirks[j];
2995 /* found: replace */
2996 DNPRINTF(SWM_D_QUIRK,
2997 "setquirk: replace #%d %s:%s\n",
2998 i, quirks[i].class, quirks[i].name);
2999 free(quirks[i].class);
3000 free(quirks[i].name);
3001 quirks[i].class = strdup(class);
3002 quirks[i].name = strdup(name);
3003 quirks[i].quirk = quirk;
3010 "error: setquirk: cannot find class/name combination");
3013 /* not found: add */
3014 if (quirks_size == 0 || quirks == NULL) {
3016 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
3017 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
3019 fprintf(stderr, "setquirk: malloc failed\n");
3023 } else if (quirks_length == quirks_size) {
3025 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
3026 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
3028 fprintf(stderr, "setquirk: realloc failed\n");
3033 if (quirks_length < quirks_size) {
3034 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
3035 j = quirks_length++;
3036 quirks[j].class = strdup(class);
3037 quirks[j].name = strdup(name);
3038 quirks[j].quirk = quirk;
3040 fprintf(stderr, "quirks array problem?\n");
3042 fprintf(stderr, "quirks array problem!\n");
3049 setconfquirk(char *selector, char *value, int flags)
3051 char *cp, *class, *name;
3053 unsigned long quirks;
3054 if (selector == NULL)
3056 if ((cp = strchr(selector, ':')) == NULL)
3061 if ((retval = parsequirks(value, &quirks)) == 0)
3062 setquirk(class, name, quirks);
3069 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
3070 setquirk("OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT);
3071 setquirk("OpenOffice.org 3.0", "VCLSalFrame", SWM_Q_FLOAT);
3072 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
3073 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
3074 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3075 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
3076 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3077 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3078 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3079 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3080 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
3081 setquirk("pcb", "pcb", SWM_Q_FLOAT);
3084 /* conf file stuff */
3085 #define SWM_CONF_FILE "scrotwm.conf"
3087 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_STACK_ENABLED,
3088 SWM_S_CLOCK_ENABLED, SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE,
3089 SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
3090 SWM_S_TITLE_NAME_ENABLED, SWM_S_BAR_FONT, SWM_S_BAR_ACTION,
3091 SWM_S_SPAWN_TERM, SWM_S_SS_APP, SWM_S_DIALOG_RATIO };
3094 setconfvalue(char *selector, char *value, int flags)
3097 case SWM_S_BAR_DELAY:
3098 bar_delay = atoi(value);
3100 case SWM_S_BAR_ENABLED:
3101 bar_enabled = atoi(value);
3103 case SWM_S_STACK_ENABLED:
3104 stack_enabled = atoi(value);
3106 case SWM_S_CLOCK_ENABLED:
3107 clock_enabled = atoi(value);
3109 case SWM_S_CYCLE_EMPTY:
3110 cycle_empty = atoi(value);
3112 case SWM_S_CYCLE_VISIBLE:
3113 cycle_visible = atoi(value);
3115 case SWM_S_SS_ENABLED:
3116 ss_enabled = atoi(value);
3118 case SWM_S_TERM_WIDTH:
3119 term_width = atoi(value);
3121 case SWM_S_TITLE_CLASS_ENABLED:
3122 title_class_enabled = atoi(value);
3124 case SWM_S_TITLE_NAME_ENABLED:
3125 title_name_enabled = atoi(value);
3127 case SWM_S_BAR_FONT:
3129 if ((bar_fonts[0] = strdup(value)) == NULL)
3130 err(1, "setconfvalue: bar_font");
3132 case SWM_S_BAR_ACTION:
3134 if ((bar_argv[0] = strdup(value)) == NULL)
3135 err(1, "setconfvalue: bar_action");
3137 case SWM_S_SPAWN_TERM:
3138 free(spawn_term[0]);
3139 if ((spawn_term[0] = strdup(value)) == NULL)
3140 err(1, "setconfvalue: spawn_term");
3144 case SWM_S_DIALOG_RATIO:
3145 dialog_ratio = atof(value);
3146 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
3156 setconfmodkey(char *selector, char *value, int flags)
3158 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
3159 update_modkey(Mod1Mask);
3160 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
3161 update_modkey(Mod2Mask);
3162 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
3163 update_modkey(Mod3Mask);
3164 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
3165 update_modkey(Mod4Mask);
3172 setconfcolor(char *selector, char *value, int flags)
3174 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
3179 setconfregion(char *selector, char *value, int flags)
3181 custom_region(value);
3185 /* config options */
3186 struct config_option {
3188 int (*func)(char*, char*, int);
3191 struct config_option configopt[] = {
3192 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
3193 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
3194 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
3195 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
3196 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
3197 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
3198 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
3199 { "bind", setconfbinding, 0 },
3200 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
3201 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
3202 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
3203 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
3204 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
3205 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
3206 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
3207 { "modkey", setconfmodkey, 0 },
3208 { "program", setconfspawn, 0 },
3209 { "quirk", setconfquirk, 0 },
3210 { "region", setconfregion, 0 },
3211 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
3212 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
3213 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
3214 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
3215 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
3216 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED }
3221 conf_load(char *filename)
3224 char *line, *cp, *optsub, *optval;
3225 size_t linelen, lineno = 0;
3226 int wordlen, i, optind;
3227 struct config_option *opt;
3229 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
3231 if (filename == NULL) {
3232 fprintf(stderr, "conf_load: no filename\n");
3235 if ((config = fopen(filename, "r")) == NULL) {
3236 warn("conf_load: fopen");
3240 while (!feof(config)) {
3241 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
3244 err(1, "%s", filename);
3249 cp += strspn(cp, " \t\n"); /* eat whitespace */
3250 if (cp[0] == '\0') {
3255 /* get config option */
3256 wordlen = strcspn(cp, "=[ \t\n");
3258 warnx("%s: line %zd: no option found",
3263 for (i = 0; i < LENGTH(configopt); i++) {
3264 opt = &configopt[i];
3265 if (!strncasecmp(cp, opt->optname, wordlen) &&
3266 strlen(opt->optname) == wordlen) {
3272 warnx("%s: line %zd: unknown option %.*s",
3273 filename, lineno, wordlen, cp);
3277 cp += strspn(cp, " \t\n"); /* eat whitespace */
3278 /* get [selector] if any */
3282 wordlen = strcspn(cp, "]");
3285 warnx("%s: line %zd: syntax error",
3289 asprintf(&optsub, "%.*s", wordlen, cp);
3292 cp += strspn(cp, "] \t\n"); /* eat trailing */
3294 cp += strspn(cp, "= \t\n"); /* eat trailing */
3296 optval = strdup(cp);
3297 /* call function to deal with it all */
3298 if (configopt[optind].func(optsub, optval,
3299 configopt[optind].funcflags) != 0) {
3300 fprintf(stderr, "%s line %zd: %s\n",
3301 filename, lineno, line);
3302 errx(1, "%s: line %zd: invalid data for %s",
3303 filename, lineno, configopt[optind].optname);
3311 DNPRINTF(SWM_D_CONF, "conf_load end\n");
3317 manage_window(Window id)
3320 struct workspace *ws;
3321 struct ws_win *win, *ww;
3322 int format, i, ws_idx, n, border_me = 0;
3323 unsigned long nitems, bytes;
3324 Atom ws_idx_atom = 0, type;
3325 Atom *prot = NULL, *pp;
3326 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
3327 struct swm_region *r;
3332 if ((win = find_window(id)) != NULL)
3333 return (win); /* already being managed */
3335 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
3336 errx(1, "calloc: failed to allocate memory for new window");
3338 /* Get all the window data in one shot */
3339 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3341 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
3342 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
3343 XGetWindowAttributes(display, id, &win->wa);
3344 XGetWMNormalHints(display, id, &win->sh, &mask);
3345 XGetTransientForHint(display, id, &trans);
3347 win->transient = trans;
3348 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
3349 (unsigned)win->id, win->transient);
3351 /* get supported protocols */
3352 if (XGetWMProtocols(display, id, &prot, &n)) {
3353 for (i = 0, pp = prot; i < n; i++, pp++)
3355 win->can_delete = 1;
3361 * Figure out where to put the window. If it was previously assigned to
3362 * a workspace (either by spawn() or manually moving), and isn't
3363 * transient, * put it in the same workspace
3365 r = root_to_region(win->wa.root);
3366 if (prop && win->transient == 0) {
3367 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
3368 ws_idx = strtonum(prop, 0, 9, &errstr);
3370 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
3373 ws = &r->s->ws[ws_idx];
3376 /* this should launch transients in the same ws as parent */
3378 if ((ww = find_window(trans)) != NULL)
3385 "fix this bug mcbride\n");
3390 /* set up the window layout */
3393 win->s = r->s; /* this never changes */
3394 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
3396 win->g.w = win->wa.width;
3397 win->g.h = win->wa.height;
3398 win->g.x = win->wa.x;
3399 win->g.y = win->wa.y;
3401 /* Set window properties so we can remember this after reincarnation */
3402 if (ws_idx_atom && prop == NULL &&
3403 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
3404 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3406 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3407 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3411 if (XGetClassHint(display, win->id, &win->ch)) {
3412 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
3413 win->ch.res_class, win->ch.res_name);
3414 for (i = 0; i < quirks_length; i++){
3415 if (!strcmp(win->ch.res_class, quirks[i].class) &&
3416 !strcmp(win->ch.res_name, quirks[i].name)) {
3417 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
3418 win->ch.res_class, win->ch.res_name);
3419 if (quirks[i].quirk & SWM_Q_FLOAT)
3421 win->quirks = quirks[i].quirk;
3426 /* alter window position if quirky */
3427 if (win->quirks & SWM_Q_ANYWHERE) {
3428 win->manual = 1; /* don't center the quirky windows */
3429 bzero(&wc, sizeof wc);
3431 if (win->g.y < bar_height) {
3432 win->g.y = wc.y = bar_height;
3435 if (win->g.w + win->g.x > WIDTH(r)) {
3436 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
3442 /* Reset font sizes (the bruteforce way; no default keybinding). */
3443 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
3444 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3445 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3446 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3447 fake_keypress(win, XK_KP_Add, ShiftMask);
3452 bzero(&wc, sizeof wc);
3453 wc.border_width = 1;
3454 mask = CWBorderWidth;
3455 XConfigureWindow(display, win->id, mask, &wc);
3458 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
3459 PropertyChangeMask | StructureNotifyMask);
3461 set_win_state(win, NormalState);
3463 /* floaters need to be mapped if they are in the current workspace */
3464 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
3465 XMapRaised(display, win->id);
3471 unmanage_window(struct ws_win *win)
3473 struct workspace *ws;
3478 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
3481 TAILQ_REMOVE(&win->ws->winlist, win, entry);
3482 if (win->ch.res_class)
3483 XFree(win->ch.res_class);
3484 if (win->ch.res_name)
3485 XFree(win->ch.res_name);
3492 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
3500 XKeyEvent *ev = &e->xkey;
3502 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
3504 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
3505 for (i = 0; i < keys_length; i++)
3506 if (keysym == keys[i].keysym
3507 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
3508 && keyfuncs[keys[i].funcid].func) {
3509 if (keys[i].funcid == kf_spawn_custom)
3511 root_to_region(ev->root),
3512 &(keyfuncs[keys[i].funcid].args),
3516 keyfuncs[keys[i].funcid].func(
3517 root_to_region(ev->root),
3518 &(keyfuncs[keys[i].funcid].args)
3524 buttonpress(XEvent *e)
3526 XButtonPressedEvent *ev = &e->xbutton;
3531 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
3533 action = root_click;
3534 if ((win = find_window(ev->window)) == NULL)
3538 action = client_click;
3541 for (i = 0; i < LENGTH(buttons); i++)
3542 if (action == buttons[i].action && buttons[i].func &&
3543 buttons[i].button == ev->button &&
3544 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
3545 buttons[i].func(win, &buttons[i].args);
3549 configurerequest(XEvent *e)
3551 XConfigureRequestEvent *ev = &e->xconfigurerequest;
3556 if ((win = find_window(ev->window)) == NULL)
3560 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
3562 bzero(&wc, sizeof wc);
3565 wc.width = ev->width;
3566 wc.height = ev->height;
3567 wc.border_width = ev->border_width;
3568 wc.sibling = ev->above;
3569 wc.stack_mode = ev->detail;
3570 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
3572 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
3574 if (win->floating) {
3575 if (ev->value_mask & CWX)
3577 if (ev->value_mask & CWY)
3579 if (ev->value_mask & CWWidth)
3580 win->g.w = ev->width;
3581 if (ev->value_mask & CWHeight)
3582 win->g.h = ev->height;
3583 if (win->ws->r != NULL) {
3584 /* this seems to be full screen */
3585 if (win->g.w >= WIDTH(win->ws->r)) {
3587 win->g.w = WIDTH(win->ws->r);
3588 ev->value_mask |= CWX | CWWidth;
3590 if (win->g.h >= HEIGHT(win->ws->r)) {
3593 win->g.h = HEIGHT(win->ws->r);
3594 ev->value_mask |= CWY | CWHeight;
3597 if ((ev->value_mask & (CWX | CWY)) &&
3598 !(ev->value_mask & (CWWidth | CWHeight)))
3600 XMoveResizeWindow(display, win->id,
3601 win->g.x, win->g.y, win->g.w, win->g.h);
3608 configurenotify(XEvent *e)
3613 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
3614 e->xconfigure.window);
3616 XMapWindow(display, e->xconfigure.window);
3617 win = find_window(e->xconfigure.window);
3619 XGetWMNormalHints(display, win->id, &win->sh, &mask);
3621 XMapWindow(display, win->id);
3628 destroynotify(XEvent *e)
3630 struct ws_win *win, *winfocus = NULL;
3631 struct workspace *ws;
3632 struct ws_win_list *wl;
3633 XDestroyWindowEvent *ev = &e->xdestroywindow;
3635 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
3637 if ((win = find_window(ev->window)) != NULL) {
3638 /* find a window to focus */
3642 /* if we are transient give focus to parent */
3644 winfocus = find_window(win->transient);
3645 else if (ws->focus == win) {
3646 /* if in max_stack try harder */
3647 if (ws->cur_layout->flags & SWM_L_FOCUSPREV)
3648 if (win != ws->focus && win != ws->focus_prev)
3649 winfocus = ws->focus_prev;
3651 /* fallback and normal handling */
3652 if (winfocus == NULL) {
3653 if (TAILQ_FIRST(wl) == win)
3654 winfocus = TAILQ_NEXT(win, entry);
3656 winfocus = TAILQ_PREV(ws->focus,
3657 ws_win_list, entry);
3658 if (winfocus == NULL)
3659 winfocus = TAILQ_LAST(wl,
3664 unmanage_window(win);
3669 focus_win(winfocus);
3675 enternotify(XEvent *e)
3677 XCrossingEvent *ev = &e->xcrossing;
3680 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
3683 /* eat event(r) to prevent autofocus */
3688 * happens when a window is created or destroyed and the border
3689 * crosses the mouse pointer
3691 if (QLength(display))
3694 if ((win = find_window(ev->window)) != NULL) {
3695 if (win->ws->focus == win)
3704 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
3710 DNPRINTF(SWM_D_EVENT, "focusout: window: %lu\n", e->xfocus.window);
3714 mapnotify(XEvent *e)
3717 XMapEvent *ev = &e->xmap;
3719 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
3721 win = find_window(ev->window);
3723 set_win_state(win, NormalState);
3727 mappingnotify(XEvent *e)
3729 XMappingEvent *ev = &e->xmapping;
3731 XRefreshKeyboardMapping(ev);
3732 if (ev->request == MappingKeyboard)
3737 maprequest(XEvent *e)
3740 struct swm_region *r;
3742 XMapRequestEvent *ev = &e->xmaprequest;
3743 XWindowAttributes wa;
3745 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
3746 e->xmaprequest.window);
3748 if (!XGetWindowAttributes(display, ev->window, &wa))
3750 if (wa.override_redirect)
3753 win = manage_window(e->xmaprequest.window);
3755 return; /* can't happen */
3761 /* make new win focused */
3762 r = root_to_region(win->wa.root);
3763 if (win->ws == r->ws)
3768 propertynotify(XEvent *e)
3771 XPropertyEvent *ev = &e->xproperty;
3773 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
3776 if (ev->state == PropertyDelete)
3777 return; /* ignore */
3778 win = find_window(ev->window);
3783 case XA_WM_NORMAL_HINTS:
3786 XGetWMNormalHints(display, win->id, &win->sh, &mask);
3787 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
3788 if (win->sh.flags & PMinSize) {
3789 win->g.w = win->sh.min_width;
3790 win->g.h = win->sh.min_height;
3791 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
3793 XMoveResizeWindow(display, win->id,
3794 win->g.x, win->g.y, win->g.w, win->g.h);
3803 unmapnotify(XEvent *e)
3805 struct ws_win *win, *winfocus = NULL;
3806 struct workspace *ws;
3808 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
3810 /* determine if we need to help unmanage this window */
3811 win = find_window(e->xunmap.window);
3815 if (getstate(e->xunmap.window) == NormalState) {
3817 * this window does not have a destroy event but but it is no
3818 * longer visible due to the app unmapping it so unmanage it
3821 /* if we are max_stack try harder to focus on something */
3822 if (ws->cur_layout->flags & SWM_L_FOCUSPREV) {
3824 winfocus = find_window(win->transient);
3825 else if (win != ws->focus && win != ws->focus_prev)
3826 winfocus = ws->focus_prev;
3829 /* normal and fallback if haven't found anything to focus on */
3830 if (winfocus == NULL) {
3831 winfocus = TAILQ_PREV(win, ws_win_list, entry);
3832 if (TAILQ_FIRST(&ws->winlist) == win)
3833 winfocus = TAILQ_NEXT(win, entry);
3836 winfocus = TAILQ_PREV(ws->focus,
3837 ws_win_list, entry);
3838 if (winfocus == NULL)
3839 winfocus = TAILQ_LAST(&ws->winlist,
3844 /* trash window and refocus */
3845 unmanage_window(win);
3848 focus_win(winfocus);
3854 visibilitynotify(XEvent *e)
3857 struct swm_region *r;
3859 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
3860 e->xvisibility.window);
3861 if (e->xvisibility.state == VisibilityUnobscured)
3862 for (i = 0; i < ScreenCount(display); i++)
3863 TAILQ_FOREACH(r, &screens[i].rl, entry)
3864 if (e->xvisibility.window == r->bar_window)
3869 xerror_start(Display *d, XErrorEvent *ee)
3876 xerror(Display *d, XErrorEvent *ee)
3878 /* fprintf(stderr, "error: %p %p\n", display, ee); */
3886 xerrorxlib = XSetErrorHandler(xerror_start);
3888 /* this causes an error if some other window manager is running */
3889 XSelectInput(display, DefaultRootWindow(display),
3890 SubstructureRedirectMask);
3891 XSync(display, False);
3895 XSetErrorHandler(xerror);
3896 XSync(display, False);
3901 new_region(struct swm_screen *s, int x, int y, int w, int h)
3903 struct swm_region *r, *n;
3904 struct workspace *ws = NULL;
3907 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
3908 s->idx, w, h, x, y);
3910 /* remove any conflicting regions */
3911 n = TAILQ_FIRST(&s->rl);
3914 n = TAILQ_NEXT(r, entry);
3915 if (X(r) < (x + w) &&
3916 (X(r) + WIDTH(r)) > x &&
3918 (Y(r) + HEIGHT(r)) > y) {
3919 XDestroyWindow(display, r->bar_window);
3920 TAILQ_REMOVE(&s->rl, r, entry);
3921 TAILQ_INSERT_TAIL(&s->orl, r, entry);
3925 /* search old regions for one to reuse */
3927 /* size + location match */
3928 TAILQ_FOREACH(r, &s->orl, entry)
3929 if (X(r) == x && Y(r) == y &&
3930 HEIGHT(r) == h && WIDTH(r) == w)
3934 TAILQ_FOREACH(r, &s->orl, entry)
3935 if (HEIGHT(r) == h && WIDTH(r) == w)
3939 TAILQ_REMOVE(&s->orl, r, entry);
3940 /* try to use old region's workspace */
3941 if (r->ws->r == NULL)
3944 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
3945 errx(1, "calloc: failed to allocate memory for screen");
3947 /* if we don't have a workspace already, find one */
3949 for (i = 0; i < SWM_WS_MAX; i++)
3950 if (s->ws[i].r == NULL) {
3957 errx(1, "no free workspaces\n");
3966 TAILQ_INSERT_TAIL(&s->rl, r, entry);
3972 #ifdef SWM_XRR_HAS_CRTC
3974 XRRScreenResources *sr;
3977 #endif /* SWM_XRR_HAS_CRTC */
3978 struct swm_region *r;
3981 if (i >= ScreenCount(display))
3982 errx(1, "invalid screen");
3984 /* remove any old regions */
3985 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
3986 r->ws->old_r = r->ws->r = NULL;
3987 XDestroyWindow(display, r->bar_window);
3988 TAILQ_REMOVE(&screens[i].rl, r, entry);
3989 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
3992 /* map virtual screens onto physical screens */
3993 #ifdef SWM_XRR_HAS_CRTC
3995 if (xrandr_support) {
3996 sr = XRRGetScreenResources(display, screens[i].root);
3998 new_region(&screens[i], 0, 0,
3999 DisplayWidth(display, i),
4000 DisplayHeight(display, i));
4004 for (c = 0, ci = NULL; c < ncrtc; c++) {
4005 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
4006 if (ci->noutput == 0)
4010 if (ci != NULL && ci->mode == None)
4011 new_region(&screens[i], 0, 0,
4012 DisplayWidth(display, i),
4013 DisplayHeight(display, i));
4015 new_region(&screens[i],
4016 ci->x, ci->y, ci->width, ci->height);
4019 XRRFreeCrtcInfo(ci);
4020 XRRFreeScreenResources(sr);
4022 #endif /* SWM_XRR_HAS_CRTC */
4024 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
4025 DisplayHeight(display, i));
4030 screenchange(XEvent *e) {
4031 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
4032 struct swm_region *r;
4035 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
4037 if (!XRRUpdateConfiguration(e))
4040 /* silly event doesn't include the screen index */
4041 for (i = 0; i < ScreenCount(display); i++)
4042 if (screens[i].root == xe->root)
4044 if (i >= ScreenCount(display))
4045 errx(1, "screenchange: screen not found\n");
4047 /* brute force for now, just re-enumerate the regions */
4050 /* add bars to all regions */
4051 for (i = 0; i < ScreenCount(display); i++)
4052 TAILQ_FOREACH(r, &screens[i].rl, entry)
4060 Window d1, d2, *wins = NULL;
4061 XWindowAttributes wa;
4064 int errorbase, major, minor;
4065 struct workspace *ws;
4069 if ((screens = calloc(ScreenCount(display),
4070 sizeof(struct swm_screen))) == NULL)
4071 errx(1, "calloc: screens");
4073 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4075 /* initial Xrandr setup */
4076 xrandr_support = XRRQueryExtension(display,
4077 &xrandr_eventbase, &errorbase);
4079 if (XRRQueryVersion(display, &major, &minor) && major < 1)
4082 /* map physical screens */
4083 for (i = 0; i < ScreenCount(display); i++) {
4084 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
4086 TAILQ_INIT(&screens[i].rl);
4087 TAILQ_INIT(&screens[i].orl);
4088 screens[i].root = RootWindow(display, i);
4090 /* set default colors */
4091 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
4092 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
4093 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
4094 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
4095 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
4097 /* init all workspaces */
4098 /* XXX these should be dynamically allocated too */
4099 for (j = 0; j < SWM_WS_MAX; j++) {
4100 ws = &screens[i].ws[j];
4106 TAILQ_INIT(&ws->winlist);
4108 for (k = 0; layouts[k].l_stack != NULL; k++)
4109 if (layouts[k].l_config != NULL)
4110 layouts[k].l_config(ws,
4111 SWM_ARG_ID_STACKINIT);
4112 ws->cur_layout = &layouts[0];
4114 /* grab existing windows (before we build the bars)*/
4115 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
4121 XRRSelectInput(display, screens[i].root,
4122 RRScreenChangeNotifyMask);
4124 /* attach windows to a region */
4125 /* normal windows */
4126 for (j = 0; j < no; j++) {
4127 XGetWindowAttributes(display, wins[j], &wa);
4128 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4129 wa.override_redirect ||
4130 XGetTransientForHint(display, wins[j], &d1))
4133 state = getstate(wins[j]);
4134 manage = state == NormalState || state == IconicState;
4135 if (wa.map_state == IsViewable || manage)
4136 manage_window(wins[j]);
4138 /* transient windows */
4139 for (j = 0; j < no; j++) {
4140 if (!XGetWindowAttributes(display, wins[j], &wa))
4143 state = getstate(wins[j]);
4144 manage = state == NormalState || state == IconicState;
4145 if (XGetTransientForHint(display, wins[j], &d1) &&
4147 manage_window(wins[j]);
4159 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
4161 err(1, "setup_globals: strdup");
4162 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
4164 err(1, "setup_globals: strdup");
4165 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
4167 err(1, "setup_globals: strdup");
4168 if ((spawn_term[0] = strdup("xterm")) == NULL)
4169 err(1, "setup_globals: strdup");
4176 Atom netwmcheck, netwmname, utf8_string;
4179 /* work around sun jdk bugs, code from wmname */
4180 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
4181 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
4182 utf8_string = XInternAtom(display, "UTF8_STRING", False);
4183 for (i = 0; i < ScreenCount(display); i++) {
4184 root = screens[i].root;
4185 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
4186 PropModeReplace, (unsigned char *)&root, 1);
4187 XChangeProperty(display, root, netwmname, utf8_string, 8,
4188 PropModeReplace, "LG3D", strlen("LG3D"));
4193 main(int argc, char *argv[])
4196 struct swm_region *r, *rr;
4197 struct ws_win *winfocus = NULL;
4198 char conf[PATH_MAX], *cfile = NULL;
4205 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
4206 SWM_VERSION, cvstag);
4207 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
4208 warnx("no locale support");
4210 if (!(display = XOpenDisplay(0)))
4211 errx(1, "can not open display");
4214 errx(1, "other wm running");
4216 /* handle some signale */
4217 installsignal(SIGINT, "INT");
4218 installsignal(SIGHUP, "HUP");
4219 installsignal(SIGQUIT, "QUIT");
4220 installsignal(SIGTERM, "TERM");
4221 installsignal(SIGCHLD, "CHLD");
4223 astate = XInternAtom(display, "WM_STATE", False);
4224 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
4225 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
4227 /* look for local and global conf file */
4228 pwd = getpwuid(getuid());
4230 errx(1, "invalid user %d", getuid());
4238 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
4239 if (stat(conf, &sb) != -1) {
4240 if (S_ISREG(sb.st_mode))
4243 /* try global conf file */
4244 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
4245 if (!stat(conf, &sb))
4246 if (S_ISREG(sb.st_mode))
4252 /* setup all bars */
4253 for (i = 0; i < ScreenCount(display); i++)
4254 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4255 if (winfocus == NULL)
4256 winfocus = TAILQ_FIRST(&r->ws->winlist);
4260 /* set some values to work around bad programs */
4266 xfd = ConnectionNumber(display);
4268 while (XPending(display)) {
4269 XNextEvent(display, &e);
4272 if (e.type < LASTEvent) {
4274 if (handler[e.type])
4275 handler[e.type](&e);
4277 DNPRINTF(SWM_D_EVENT,
4278 "win: %lu unknown event: %d\n",
4279 e.xany.window, e.type);
4281 switch (e.type - xrandr_eventbase) {
4282 case RRScreenChangeNotify:
4286 DNPRINTF(SWM_D_EVENT,
4287 "win: %lu unknown xrandr event: "
4288 "%d\n", e.xany.window, e.type);
4294 /* if we are being restarted go focus on first window */
4296 rr = TAILQ_FIRST(&screens[0].rl);
4297 /* move pointer to first screen if multi screen */
4298 if (ScreenCount(display) > 1 || outputs > 1)
4299 XWarpPointer(display, None, rr->s[0].root,
4300 0, 0, 0, 0, rr->g.x,
4301 rr->g.y + bar_enabled ? bar_height : 0);
4303 focus_win(winfocus);
4310 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
4312 DNPRINTF(SWM_D_MISC, "select failed");
4323 XCloseDisplay(display);