3 * Copyright (c) 2009-2010 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.26"
72 #include <sys/types.h>
76 #include <sys/queue.h>
77 #include <sys/param.h>
78 #include <sys/select.h>
80 #include <X11/cursorfont.h>
81 #include <X11/keysym.h>
82 #include <X11/Xatom.h>
84 #include <X11/Xproto.h>
85 #include <X11/Xutil.h>
86 #include <X11/extensions/Xrandr.h>
93 # error XRandR versions less than 1.0 are not supported
98 #define SWM_XRR_HAS_CRTC
102 /* #define SWM_DEBUG */
104 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
105 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
106 #define SWM_D_MISC 0x0001
107 #define SWM_D_EVENT 0x0002
108 #define SWM_D_WS 0x0004
109 #define SWM_D_FOCUS 0x0008
110 #define SWM_D_MOVE 0x0010
111 #define SWM_D_STACK 0x0020
112 #define SWM_D_MOUSE 0x0040
113 #define SWM_D_PROP 0x0080
114 #define SWM_D_CLASS 0x0100
115 #define SWM_D_KEY 0x0200
116 #define SWM_D_QUIRK 0x0400
117 #define SWM_D_SPAWN 0x0800
118 #define SWM_D_EVENTQ 0x1000
119 #define SWM_D_CONF 0x2000
121 u_int32_t swm_debug = 0
138 #define DPRINTF(x...)
139 #define DNPRINTF(n,x...)
142 #define LENGTH(x) (sizeof x / sizeof x[0])
143 #define MODKEY Mod1Mask
144 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
145 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
146 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
147 #define SWM_PROPLEN (16)
148 #define SWM_FUNCNAME_LEN (32)
149 #define SWM_KEYS_LEN (255)
150 #define SWM_QUIRK_LEN (64)
151 #define X(r) (r)->g.x
152 #define Y(r) (r)->g.y
153 #define WIDTH(r) (r)->g.w
154 #define HEIGHT(r) (r)->g.h
155 #define SWM_MAX_FONT_STEPS (3)
156 #define WINID(w) (w ? w->id : 0)
158 #define SWM_FOCUS_DEFAULT (0)
159 #define SWM_FOCUS_SYNERGY (1)
160 #define SWM_FOCUS_FOLLOW (2)
163 #define SWM_LIB "/usr/local/lib/libswmhack.so"
171 volatile sig_atomic_t running = 1;
172 volatile sig_atomic_t restart_wm = 0;
174 int last_focus_event = FocusOut;
175 int (*xerrorxlib)(Display *, XErrorEvent *);
179 int xrandr_eventbase;
180 unsigned int numlockmask = 0;
184 int cycle_visible = 0;
186 int font_adjusted = 0;
187 unsigned int mod_key = MODKEY;
190 double dialog_ratio = .6;
192 #define SWM_BAR_MAX (256)
193 char *bar_argv[] = { NULL, NULL };
195 char bar_ext[SWM_BAR_MAX];
196 char bar_vertext[SWM_BAR_MAX];
198 sig_atomic_t bar_alarm = 0;
201 int bar_at_bottom = 0;
203 int bar_extra_running = 0;
206 int stack_enabled = 1;
207 int clock_enabled = 1;
208 char *clock_format = NULL;
209 int title_name_enabled = 0;
210 int title_class_enabled = 0;
211 int focus_mode = SWM_FOCUS_DEFAULT;
212 int disable_border = 0;
218 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
219 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
221 #define SWM_MENU_FN (2)
222 #define SWM_MENU_NB (4)
223 #define SWM_MENU_NF (6)
224 #define SWM_MENU_SB (8)
225 #define SWM_MENU_SF (10)
227 /* layout manager data */
228 struct swm_geometry {
238 /* virtual "screens" */
240 TAILQ_ENTRY(swm_region) entry;
241 struct swm_geometry g;
242 struct workspace *ws; /* current workspace on this region */
243 struct workspace *ws_prior; /* prior workspace on this region */
244 struct swm_screen *s; /* screen idx */
247 TAILQ_HEAD(swm_region_list, swm_region);
250 TAILQ_ENTRY(ws_win) entry;
253 struct ws_win *child_trans; /* transient child window */
254 struct swm_geometry g; /* current geometry */
255 struct swm_geometry g_float; /* geometry when floating */
256 struct swm_geometry rg_float; /* region geom when floating */
257 int g_floatvalid; /* flag: geometry in g_float is valid */
258 int floatmaxed; /* flag: floater was maxed in max_stack */
261 int font_size_boundary[SWM_MAX_FONT_STEPS];
267 unsigned long quirks;
268 struct workspace *ws; /* always valid */
269 struct swm_screen *s; /* always valid, never changes */
270 XWindowAttributes wa;
274 TAILQ_HEAD(ws_win_list, ws_win);
276 /* layout handlers */
278 void vertical_config(struct workspace *, int);
279 void vertical_stack(struct workspace *, struct swm_geometry *);
280 void horizontal_config(struct workspace *, int);
281 void horizontal_stack(struct workspace *, struct swm_geometry *);
282 void max_stack(struct workspace *, struct swm_geometry *);
284 struct ws_win *find_window(Window);
286 void grabbuttons(struct ws_win *, int);
287 void new_region(struct swm_screen *, int, int, int, int);
288 void unmanage_window(struct ws_win *);
289 long getstate(Window);
292 void (*l_stack)(struct workspace *, struct swm_geometry *);
293 void (*l_config)(struct workspace *, int);
295 #define SWM_L_FOCUSPREV (1<<0)
296 #define SWM_L_MAPONFOCUS (1<<1)
299 /* stack, configure */
300 { vertical_stack, vertical_config, 0, "[|]" },
301 { horizontal_stack, horizontal_config, 0, "[-]" },
303 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, "[ ]"},
304 { NULL, NULL, 0, NULL },
307 /* position of max_stack mode in the layouts array */
308 #define SWM_MAX_STACK 2
310 #define SWM_H_SLICE (32)
311 #define SWM_V_SLICE (32)
313 /* define work spaces */
315 int idx; /* workspace index */
316 struct layout *cur_layout; /* current layout handlers */
317 struct ws_win *focus; /* may be NULL */
318 struct ws_win *focus_prev; /* may be NULL */
319 struct swm_region *r; /* may be NULL */
320 struct swm_region *old_r; /* may be NULL */
321 struct ws_win_list winlist; /* list of windows in ws */
322 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
326 int horizontal_msize;
328 int horizontal_stacks;
335 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
336 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
338 /* physical screen mapping */
339 #define SWM_WS_MAX (10)
341 int idx; /* screen index */
342 struct swm_region_list rl; /* list of regions on this screen */
343 struct swm_region_list orl; /* list of old regions */
345 struct workspace ws[SWM_WS_MAX];
351 } c[SWM_S_COLOR_MAX];
353 struct swm_screen *screens;
356 /* args to functions */
359 #define SWM_ARG_ID_FOCUSNEXT (0)
360 #define SWM_ARG_ID_FOCUSPREV (1)
361 #define SWM_ARG_ID_FOCUSMAIN (2)
362 #define SWM_ARG_ID_FOCUSCUR (4)
363 #define SWM_ARG_ID_SWAPNEXT (10)
364 #define SWM_ARG_ID_SWAPPREV (11)
365 #define SWM_ARG_ID_SWAPMAIN (12)
366 #define SWM_ARG_ID_MOVELAST (13)
367 #define SWM_ARG_ID_MASTERSHRINK (20)
368 #define SWM_ARG_ID_MASTERGROW (21)
369 #define SWM_ARG_ID_MASTERADD (22)
370 #define SWM_ARG_ID_MASTERDEL (23)
371 #define SWM_ARG_ID_STACKRESET (30)
372 #define SWM_ARG_ID_STACKINIT (31)
373 #define SWM_ARG_ID_CYCLEWS_UP (40)
374 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
375 #define SWM_ARG_ID_CYCLESC_UP (42)
376 #define SWM_ARG_ID_CYCLESC_DOWN (43)
377 #define SWM_ARG_ID_STACKINC (50)
378 #define SWM_ARG_ID_STACKDEC (51)
379 #define SWM_ARG_ID_SS_ALL (60)
380 #define SWM_ARG_ID_SS_WINDOW (61)
381 #define SWM_ARG_ID_DONTCENTER (70)
382 #define SWM_ARG_ID_CENTER (71)
383 #define SWM_ARG_ID_KILLWINDOW (80)
384 #define SWM_ARG_ID_DELETEWINDOW (81)
388 void focus(struct swm_region *, union arg *);
389 void focus_magic(struct ws_win *, int);
390 #define SWM_F_GENERIC (0)
391 #define SWM_F_TRANSIENT (1)
397 #define SWM_Q_FLOAT (1<<0) /* float this window */
398 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
399 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
400 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
401 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
403 int quirks_size = 0, quirks_length = 0;
404 struct quirk *quirks = NULL;
421 name = "ButtonPress";
424 name = "ButtonRelease";
427 name = "MotionNotify";
430 name = "EnterNotify";
433 name = "LeaveNotify";
442 name = "KeymapNotify";
448 name = "GraphicsExpose";
453 case VisibilityNotify:
454 name = "VisibilityNotify";
457 name = "CreateNotify";
460 name = "DestroyNotify";
463 name = "UnmapNotify";
472 name = "ReparentNotify";
474 case ConfigureNotify:
475 name = "ConfigureNotify";
477 case ConfigureRequest:
478 name = "ConfigureRequest";
481 name = "GravityNotify";
484 name = "ResizeRequest";
486 case CirculateNotify:
487 name = "CirculateNotify";
489 case CirculateRequest:
490 name = "CirculateRequest";
493 name = "PropertyNotify";
496 name = "SelectionClear";
498 case SelectionRequest:
499 name = "SelectionRequest";
501 case SelectionNotify:
502 name = "SelectionNotify";
505 name = "ColormapNotify";
508 name = "ClientMessage";
511 name = "MappingNotify";
516 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
518 e->xany.window, name, e->type, QLength(display));
520 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
522 e->xany.window, e->type, QLength(display));
526 dumpwins(struct swm_region *r, union arg *args)
530 XWindowAttributes wa;
533 fprintf(stderr, "invalid workspace\n");
537 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
539 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
540 state = getstate(win->id);
541 if (!XGetWindowAttributes(display, win->id, &wa))
542 fprintf(stderr, "window: %lu failed "
543 "XGetWindowAttributes\n", win->id);
544 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
545 win->id, wa.map_state, state);
548 fprintf(stderr, "===== unmanaged window list =====\n");
549 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
550 state = getstate(win->id);
551 if (!XGetWindowAttributes(display, win->id, &wa))
552 fprintf(stderr, "window: %lu failed "
553 "XGetWindowAttributes\n", win->id);
554 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
555 win->id, wa.map_state, state);
558 fprintf(stderr, "=================================\n");
563 dumpwins(struct swm_region *r, union arg *args)
566 #endif /* SWM_DEBUG */
568 void expose(XEvent *);
569 void keypress(XEvent *);
570 void buttonpress(XEvent *);
571 void configurerequest(XEvent *);
572 void configurenotify(XEvent *);
573 void destroynotify(XEvent *);
574 void enternotify(XEvent *);
575 void focusevent(XEvent *);
576 void mapnotify(XEvent *);
577 void mappingnotify(XEvent *);
578 void maprequest(XEvent *);
579 void propertynotify(XEvent *);
580 void unmapnotify(XEvent *);
581 void visibilitynotify(XEvent *);
583 void (*handler[LASTEvent])(XEvent *) = {
585 [KeyPress] = keypress,
586 [ButtonPress] = buttonpress,
587 [ConfigureRequest] = configurerequest,
588 [ConfigureNotify] = configurenotify,
589 [DestroyNotify] = destroynotify,
590 [EnterNotify] = enternotify,
591 [FocusIn] = focusevent,
592 [FocusOut] = focusevent,
593 [MapNotify] = mapnotify,
594 [MappingNotify] = mappingnotify,
595 [MapRequest] = maprequest,
596 [PropertyNotify] = propertynotify,
597 [UnmapNotify] = unmapnotify,
598 [VisibilityNotify] = visibilitynotify,
604 int saved_errno, status;
611 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
617 warn("sighdlr: waitpid");
618 #endif /* SWM_DEBUG */
623 if (WIFEXITED(status)) {
624 if (WEXITSTATUS(status) != 0)
625 warnx("sighdlr: child exit status: %d",
626 WEXITSTATUS(status));
628 warnx("sighdlr: child is terminated "
630 #endif /* SWM_DEBUG */
648 name_to_color(char *colorname)
652 XColor screen_def, exact_def;
653 unsigned long result = 0;
654 char cname[32] = "#";
656 cmap = DefaultColormap(display, screens[0].idx);
657 status = XAllocNamedColor(display, cmap, colorname,
658 &screen_def, &exact_def);
660 strlcat(cname, colorname + 2, sizeof cname - 1);
661 status = XAllocNamedColor(display, cmap, cname, &screen_def,
665 result = screen_def.pixel;
667 fprintf(stderr, "color '%s' not found.\n", colorname);
673 setscreencolor(char *val, int i, int c)
675 if (i > 0 && i <= ScreenCount(display)) {
676 screens[i - 1].c[c].color = name_to_color(val);
677 free(screens[i - 1].c[c].name);
678 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
680 } else if (i == -1) {
681 for (i = 0; i < ScreenCount(display); i++) {
682 screens[i].c[c].color = name_to_color(val);
683 free(screens[i].c[c].name);
684 if ((screens[i].c[c].name = strdup(val)) == NULL)
688 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
689 i, ScreenCount(display));
693 custom_region(char *val)
695 unsigned int sidx, x, y, w, h;
697 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
698 errx(1, "invalid custom region, "
699 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
700 if (sidx < 1 || sidx > ScreenCount(display))
701 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
702 sidx, ScreenCount(display));
706 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
708 if (x < 0 || x > DisplayWidth(display, sidx) ||
709 y < 0 || y > DisplayHeight(display, sidx) ||
710 w + x > DisplayWidth(display, sidx) ||
711 h + y > DisplayHeight(display, sidx))
712 errx(1, "region %ux%u+%u+%u not within screen boundaries "
713 "(%ux%u)\n", w, h, x, y,
714 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
716 new_region(&screens[sidx], x, y, w, h);
720 socket_setnonblock(int fd)
724 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
725 err(1, "fcntl F_GETFL");
727 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
728 err(1, "fcntl F_SETFL");
732 bar_print(struct swm_region *r, char *s)
734 XClearWindow(display, r->bar_window);
735 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
736 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
745 bzero(bar_pipe, sizeof bar_pipe);
748 kill(bar_pid, SIGTERM);
751 strlcpy(bar_ext, "", sizeof bar_ext);
756 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
758 int do_class, do_name;
760 XClassHint *xch = NULL;
762 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
764 if ((xch = XAllocClassHint()) == NULL)
766 status = XGetClassHint(display, cur_focus->id, xch);
767 if (status == BadWindow || status == BadAlloc)
769 do_class = (title_class_enabled && xch->res_class != NULL);
770 do_name = (title_name_enabled && xch->res_name != NULL);
772 strlcat(s, xch->res_class, sz);
773 if (do_class && do_name)
776 strlcat(s, xch->res_name, sz);
789 struct swm_region *r;
793 char cn[SWM_BAR_MAX];
794 char loc[SWM_BAR_MAX];
798 if (bar_enabled == 0)
800 if (bar_extra && bar_extra_running) {
801 /* ignore short reads; it'll correct itself */
802 while ((b = fgetln(stdin, &len)) != NULL)
803 if (b && b[len - 1] == '\n') {
805 strlcpy(bar_ext, b, sizeof bar_ext);
807 if (b == NULL && errno != EAGAIN) {
808 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
809 errno, strerror(errno));
813 strlcpy(bar_ext, "", sizeof bar_ext);
815 if (clock_enabled == 0)
816 strlcpy(s, "", sizeof s);
819 localtime_r(&tmt, &tm);
820 strftime(s, sizeof s, clock_format, &tm);
821 strlcat(s, " ", sizeof s);
824 for (i = 0; i < ScreenCount(display); i++) {
826 TAILQ_FOREACH(r, &screens[i].rl, entry) {
827 strlcpy(cn, "", sizeof cn);
829 bar_class_name(cn, sizeof cn, r->ws->focus);
832 stack = r->ws->cur_layout->name;
834 snprintf(loc, sizeof loc, "%d:%d %s %s%s %s %s",
835 x++, r->ws->idx + 1, stack, s, cn, bar_ext,
850 bar_toggle(struct swm_region *r, union arg *args)
852 struct swm_region *tmpr;
853 int i, sc = ScreenCount(display);
855 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
858 for (i = 0; i < sc; i++)
859 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
860 XUnmapWindow(display, tmpr->bar_window);
862 for (i = 0; i < sc; i++)
863 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
864 XMapRaised(display, tmpr->bar_window);
866 bar_enabled = !bar_enabled;
869 /* must be after stack */
876 XSetWindowAttributes wa;
877 struct swm_region *r;
880 /* do this here because the conf file is in memory */
881 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
882 /* launch external status app */
883 bar_extra_running = 1;
884 if (pipe(bar_pipe) == -1)
885 err(1, "pipe error");
886 socket_setnonblock(bar_pipe[0]);
887 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
888 if (dup2(bar_pipe[0], 0) == -1)
890 if (dup2(bar_pipe[1], 1) == -1)
892 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
893 err(1, "could not disable SIGPIPE");
894 switch (bar_pid = fork()) {
896 err(1, "cannot fork");
900 execvp(bar_argv[0], bar_argv);
901 err(1, "%s external app failed", bar_argv[0]);
903 default: /* parent */
909 bzero(&wa, sizeof wa);
910 for (i = 0; i < ScreenCount(display); i++)
911 TAILQ_FOREACH(r, &screens[i].rl, entry) {
913 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
914 wa.background_pixel =
915 screens[i].c[SWM_S_COLOR_BAR].color;
916 XChangeWindowAttributes(display, r->bar_window,
917 CWBackPixel | CWBorderPixel, &wa);
923 bar_setup(struct swm_region *r)
928 XFreeFont(display, bar_fs);
932 for (i = 0; bar_fonts[i] != NULL; i++) {
933 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
939 if (bar_fonts[i] == NULL)
940 errx(1, "couldn't load font");
942 errx(1, "couldn't create font structure");
944 bar_height = bar_fs->ascent + bar_fs->descent + 3;
946 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
948 r->bar_window = XCreateSimpleWindow(display,
949 r->s->root, x, y, WIDTH(r) - 2, bar_height - 2,
950 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
951 r->s->c[SWM_S_COLOR_BAR].color);
952 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
953 XSetFont(display, bar_gc, bar_fs->fid);
954 XSelectInput(display, r->bar_window, VisibilityChangeMask);
956 XMapRaised(display, r->bar_window);
957 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
959 if (signal(SIGALRM, bar_signal) == SIG_ERR)
960 err(1, "could not install bar_signal");
965 set_win_state(struct ws_win *win, long state)
967 long data[] = {state, None};
969 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
974 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
975 (unsigned char *)data, 2);
983 unsigned char *p = NULL;
984 unsigned long n, extra;
987 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
988 &real, &format, &n, &extra, (unsigned char **)&p);
989 if (status != Success)
992 result = *((long *)p);
998 version(struct swm_region *r, union arg *args)
1000 bar_version = !bar_version;
1002 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
1003 SWM_VERSION, cvstag);
1005 strlcpy(bar_vertext, "", sizeof bar_vertext);
1010 client_msg(struct ws_win *win, Atom a)
1012 XClientMessageEvent cm;
1017 bzero(&cm, sizeof cm);
1018 cm.type = ClientMessage;
1019 cm.window = win->id;
1020 cm.message_type = aprot;
1023 cm.data.l[1] = CurrentTime;
1024 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1028 configreq_win(struct ws_win *win)
1030 XConfigureRequestEvent cr;
1035 bzero(&cr, sizeof cr);
1036 cr.type = ConfigureRequest;
1037 cr.display = display;
1038 cr.parent = win->id;
1039 cr.window = win->id;
1042 cr.width = win->g.w;
1043 cr.height = win->g.h;
1044 cr.border_width = 1;
1046 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&cr);
1050 config_win(struct ws_win *win)
1054 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
1055 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1060 ce.type = ConfigureNotify;
1061 ce.display = display;
1063 ce.window = win->id;
1066 ce.width = win->g.w;
1067 ce.height = win->g.h;
1068 ce.border_width = 1; /* XXX store this! */
1070 ce.override_redirect = False;
1071 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1075 count_win(struct workspace *ws, int count_transient)
1080 TAILQ_FOREACH(win, &ws->winlist, entry) {
1081 if (count_transient == 0 && win->floating)
1083 if (count_transient == 0 && win->transient)
1087 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1093 quit(struct swm_region *r, union arg *args)
1095 DNPRINTF(SWM_D_MISC, "quit\n");
1100 unmap_window(struct ws_win *win)
1105 /* don't unmap again */
1106 if (getstate(win->id) == IconicState)
1109 set_win_state(win, IconicState);
1111 XUnmapWindow(display, win->id);
1112 XSetWindowBorder(display, win->id,
1113 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1122 for (i = 0; i < ScreenCount(display); i++)
1123 for (j = 0; j < SWM_WS_MAX; j++)
1124 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1129 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1136 event.display = display; /* Ignored, but what the hell */
1137 event.window = win->id;
1138 event.root = win->s->root;
1139 event.subwindow = None;
1140 event.time = CurrentTime;
1145 event.same_screen = True;
1146 event.keycode = XKeysymToKeycode(display, keysym);
1147 event.state = modifiers;
1149 event.type = KeyPress;
1150 XSendEvent(event.display, event.window, True,
1151 KeyPressMask, (XEvent *)&event);
1153 event.type = KeyRelease;
1154 XSendEvent(event.display, event.window, True,
1155 KeyPressMask, (XEvent *)&event);
1160 restart(struct swm_region *r, union arg *args)
1162 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1164 /* disable alarm because the following code may not be interrupted */
1166 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1167 errx(1, "can't disable alarm");
1172 XCloseDisplay(display);
1173 execvp(start_argv[0], start_argv);
1174 fprintf(stderr, "execvp failed\n");
1180 root_to_region(Window root)
1182 struct swm_region *r = NULL;
1184 int i, x, y, wx, wy;
1187 for (i = 0; i < ScreenCount(display); i++)
1188 if (screens[i].root == root)
1191 if (XQueryPointer(display, screens[i].root,
1192 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1193 /* choose a region based on pointer location */
1194 TAILQ_FOREACH(r, &screens[i].rl, entry)
1195 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1196 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1201 r = TAILQ_FIRST(&screens[i].rl);
1207 find_unmanaged_window(Window id)
1212 for (i = 0; i < ScreenCount(display); i++)
1213 for (j = 0; j < SWM_WS_MAX; j++)
1214 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1222 find_window(Window id)
1225 Window wrr, wpr, *wcr = NULL;
1228 for (i = 0; i < ScreenCount(display); i++)
1229 for (j = 0; j < SWM_WS_MAX; j++)
1230 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1234 /* if we were looking for the parent return that window instead */
1235 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1240 /* ignore not found and root */
1241 if (wpr == 0 || wrr == wpr)
1244 /* look for parent */
1245 for (i = 0; i < ScreenCount(display); i++)
1246 for (j = 0; j < SWM_WS_MAX; j++)
1247 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1255 spawn(struct swm_region *r, union arg *args)
1260 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1264 close(ConnectionNumber(display));
1266 setenv("LD_PRELOAD", SWM_LIB, 1);
1268 if (asprintf(&ret, "%d", r->ws->idx) == -1) {
1272 setenv("_SWM_WS", ret, 1);
1276 if (asprintf(&ret, "%d", getpid()) == -1) {
1280 setenv("_SWM_PID", ret, 1);
1284 if (setsid() == -1) {
1290 * close stdin and stdout to prevent interaction between apps
1291 * and the baraction script
1292 * leave stderr open to record errors
1294 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
1298 dup2(fd, STDIN_FILENO);
1299 dup2(fd, STDOUT_FILENO);
1303 execvp(args->argv[0], args->argv);
1311 spawnterm(struct swm_region *r, union arg *args)
1313 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1316 setenv("_SWM_XTERM_FONTADJ", "", 1);
1321 kill_refs(struct ws_win *win)
1324 struct swm_region *r;
1325 struct workspace *ws;
1330 for (i = 0; i < ScreenCount(display); i++)
1331 TAILQ_FOREACH(r, &screens[i].rl, entry)
1332 for (x = 0; x < SWM_WS_MAX; x++) {
1334 if (win == ws->focus)
1336 if (win == ws->focus_prev)
1337 ws->focus_prev = NULL;
1342 validate_win(struct ws_win *testwin)
1345 struct workspace *ws;
1346 struct swm_region *r;
1347 int i, x, foundit = 0;
1349 if (testwin == NULL)
1352 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1353 TAILQ_FOREACH(r, &screens[i].rl, entry)
1354 for (x = 0; x < SWM_WS_MAX; x++) {
1356 TAILQ_FOREACH(win, &ws->winlist, entry)
1364 validate_ws(struct workspace *testws)
1366 struct swm_region *r;
1367 struct workspace *ws;
1370 /* validate all ws */
1371 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1372 TAILQ_FOREACH(r, &screens[i].rl, entry)
1373 for (x = 0; x < SWM_WS_MAX; x++) {
1382 unfocus_win(struct ws_win *win)
1386 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1390 if (win->ws == NULL)
1393 if (validate_ws(win->ws))
1394 abort(); /* XXX replace with return at some point */
1396 if (win->ws->r == NULL)
1399 if (validate_win(win)) {
1404 if (win->ws->focus == win) {
1405 win->ws->focus = NULL;
1406 win->ws->focus_prev = win;
1409 if (validate_win(win->ws->focus)) {
1410 kill_refs(win->ws->focus);
1411 win->ws->focus = NULL;
1413 if (validate_win(win->ws->focus_prev)) {
1414 kill_refs(win->ws->focus_prev);
1415 win->ws->focus_prev = NULL;
1418 /* drain all previous unfocus events */
1419 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
1422 grabbuttons(win, 0);
1423 XSetWindowBorder(display, win->id,
1424 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1434 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
1436 for (i = 0; i < ScreenCount(display); i++)
1437 for (j = 0; j < SWM_WS_MAX; j++)
1438 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1443 focus_win(struct ws_win *win)
1448 struct ws_win *cfw = NULL;
1451 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1455 if (win->ws == NULL)
1458 if (validate_ws(win->ws))
1459 abort(); /* XXX replace with return at some point */
1461 if (validate_win(win)) {
1466 if (validate_win(win)) {
1471 XGetInputFocus(display, &cur_focus, &rr);
1472 if ((cfw = find_window(cur_focus)) != NULL)
1475 win->ws->focus = win;
1477 if (win->ws->r != NULL) {
1478 /* drain all previous focus events */
1479 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
1483 XSetInputFocus(display, win->id,
1484 RevertToParent, CurrentTime);
1485 grabbuttons(win, 1);
1486 XSetWindowBorder(display, win->id,
1487 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1488 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS)
1489 XMapRaised(display, win->id);
1494 switchws(struct swm_region *r, union arg *args)
1496 int wsid = args->id, unmap_old = 0;
1497 struct swm_region *this_r, *other_r;
1499 struct workspace *new_ws, *old_ws;
1506 old_ws = this_r->ws;
1507 new_ws = &this_r->s->ws[wsid];
1509 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1510 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1513 if (new_ws == NULL || old_ws == NULL)
1515 if (new_ws == old_ws)
1518 other_r = new_ws->r;
1519 if (other_r == NULL) {
1520 /* the other workspace is hidden, hide this one */
1524 /* the other ws is visible in another region, exchange them */
1525 other_r->ws_prior = new_ws;
1526 other_r->ws = old_ws;
1527 old_ws->r = other_r;
1529 this_r->ws_prior = old_ws;
1530 this_r->ws = new_ws;
1534 a.id = SWM_ARG_ID_FOCUSCUR;
1535 focus(new_ws->r, &a);
1538 /* unmap old windows */
1540 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1545 cyclews(struct swm_region *r, union arg *args)
1548 struct swm_screen *s = r->s;
1550 DNPRINTF(SWM_D_WS, "cyclews id %d "
1551 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1552 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1557 case SWM_ARG_ID_CYCLEWS_UP:
1558 if (a.id < SWM_WS_MAX - 1)
1563 case SWM_ARG_ID_CYCLEWS_DOWN:
1567 a.id = SWM_WS_MAX - 1;
1573 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1575 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1579 } while (a.id != r->ws->idx);
1583 priorws(struct swm_region *r, union arg *args)
1587 DNPRINTF(SWM_D_WS, "priorws id %d "
1588 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1589 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1591 if (r->ws_prior == NULL)
1594 a.id = r->ws_prior->idx;
1599 cyclescr(struct swm_region *r, union arg *args)
1601 struct swm_region *rr = NULL;
1605 /* do nothing if we don't have more than one screen */
1606 if (!(ScreenCount(display) > 1 || outputs > 1))
1611 case SWM_ARG_ID_CYCLESC_UP:
1612 rr = TAILQ_NEXT(r, entry);
1614 rr = TAILQ_FIRST(&screens[i].rl);
1616 case SWM_ARG_ID_CYCLESC_DOWN:
1617 rr = TAILQ_PREV(r, swm_region_list, entry);
1619 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1627 /* move mouse to region */
1629 y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
1630 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1632 a.id = SWM_ARG_ID_FOCUSCUR;
1635 if (rr->ws->focus) {
1636 /* move to focus window */
1637 x = rr->ws->focus->g.x + 1;
1638 y = rr->ws->focus->g.y + 1;
1639 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1644 swapwin(struct swm_region *r, union arg *args)
1646 struct ws_win *target, *source;
1647 struct ws_win *cur_focus;
1648 struct ws_win_list *wl;
1651 DNPRINTF(SWM_D_WS, "swapwin id %d "
1652 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1653 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1655 cur_focus = r->ws->focus;
1656 if (cur_focus == NULL)
1660 wl = &source->ws->winlist;
1663 case SWM_ARG_ID_SWAPPREV:
1664 target = TAILQ_PREV(source, ws_win_list, entry);
1665 TAILQ_REMOVE(wl, cur_focus, entry);
1667 TAILQ_INSERT_TAIL(wl, source, entry);
1669 TAILQ_INSERT_BEFORE(target, source, entry);
1671 case SWM_ARG_ID_SWAPNEXT:
1672 target = TAILQ_NEXT(source, entry);
1673 TAILQ_REMOVE(wl, source, entry);
1675 TAILQ_INSERT_HEAD(wl, source, entry);
1677 TAILQ_INSERT_AFTER(wl, target, source, entry);
1679 case SWM_ARG_ID_SWAPMAIN:
1680 target = TAILQ_FIRST(wl);
1681 if (target == source) {
1682 if (source->ws->focus_prev != NULL &&
1683 source->ws->focus_prev != target)
1685 source = source->ws->focus_prev;
1689 if (target == NULL || source == NULL)
1691 source->ws->focus_prev = target;
1692 TAILQ_REMOVE(wl, target, entry);
1693 TAILQ_INSERT_BEFORE(source, target, entry);
1694 TAILQ_REMOVE(wl, source, entry);
1695 TAILQ_INSERT_HEAD(wl, source, entry);
1697 case SWM_ARG_ID_MOVELAST:
1698 TAILQ_REMOVE(wl, source, entry);
1699 TAILQ_INSERT_TAIL(wl, source, entry);
1702 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1710 focus_prev(struct ws_win *win)
1712 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
1713 struct ws_win *cur_focus = NULL;
1714 struct ws_win_list *wl = NULL;
1715 struct workspace *ws = NULL;
1717 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
1719 if (!(win && win->ws))
1724 cur_focus = ws->focus;
1725 winlostfocus = cur_focus;
1727 /* pickle, just focus on whatever */
1728 if (cur_focus == NULL) {
1729 /* use prev_focus if valid */
1730 if (ws->focus_prev && ws->focus_prev != cur_focus &&
1731 find_window(WINID(ws->focus_prev)))
1732 winfocus = ws->focus_prev;
1733 if (winfocus == NULL)
1734 winfocus = TAILQ_FIRST(wl);
1738 /* if transient focus on parent */
1739 if (cur_focus->transient) {
1740 winfocus = find_window(cur_focus->transient);
1744 /* if in max_stack try harder */
1745 if (ws->cur_layout->flags & SWM_L_FOCUSPREV) {
1746 if (cur_focus != ws->focus_prev)
1747 winfocus = ws->focus_prev;
1748 else if (cur_focus != ws->focus)
1749 winfocus = ws->focus;
1751 winfocus = TAILQ_PREV(win, ws_win_list, entry);
1756 if (cur_focus == win)
1757 winfocus = TAILQ_PREV(win, ws_win_list, entry);
1758 if (winfocus == NULL)
1759 winfocus = TAILQ_LAST(wl, ws_win_list);
1760 if (winfocus == NULL || winfocus == win)
1761 winfocus = TAILQ_NEXT(cur_focus, entry);
1763 if (winfocus == winlostfocus || winfocus == NULL)
1766 focus_magic(winfocus, SWM_F_GENERIC);
1770 focus(struct swm_region *r, union arg *args)
1772 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
1773 struct ws_win *cur_focus = NULL;
1774 struct ws_win_list *wl = NULL;
1775 struct workspace *ws = NULL;
1780 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1782 /* treat FOCUS_CUR special */
1783 if (args->id == SWM_ARG_ID_FOCUSCUR) {
1785 winfocus = r->ws->focus;
1786 else if (r->ws->focus_prev)
1787 winfocus = r->ws->focus_prev;
1789 winfocus = TAILQ_FIRST(&r->ws->winlist);
1791 focus_magic(winfocus, SWM_F_GENERIC);
1795 if ((cur_focus = r->ws->focus) == NULL)
1800 winlostfocus = cur_focus;
1803 case SWM_ARG_ID_FOCUSPREV:
1804 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1805 if (winfocus == NULL)
1806 winfocus = TAILQ_LAST(wl, ws_win_list);
1809 case SWM_ARG_ID_FOCUSNEXT:
1810 winfocus = TAILQ_NEXT(cur_focus, entry);
1811 if (winfocus == NULL)
1812 winfocus = TAILQ_FIRST(wl);
1815 case SWM_ARG_ID_FOCUSMAIN:
1816 winfocus = TAILQ_FIRST(wl);
1817 if (winfocus == cur_focus)
1818 winfocus = cur_focus->ws->focus_prev;
1825 if (winfocus == winlostfocus || winfocus == NULL)
1828 focus_magic(winfocus, SWM_F_GENERIC);
1832 cycle_layout(struct swm_region *r, union arg *args)
1834 struct workspace *ws = r->ws;
1835 struct ws_win *winfocus;
1838 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1840 winfocus = ws->focus;
1843 if (ws->cur_layout->l_stack == NULL)
1844 ws->cur_layout = &layouts[0];
1847 a.id = SWM_ARG_ID_FOCUSCUR;
1853 stack_config(struct swm_region *r, union arg *args)
1855 struct workspace *ws = r->ws;
1857 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1860 if (ws->cur_layout->l_config != NULL)
1861 ws->cur_layout->l_config(ws, args->id);
1863 if (args->id != SWM_ARG_ID_STACKINIT);
1869 struct swm_geometry g;
1870 struct swm_region *r;
1873 DNPRINTF(SWM_D_STACK, "stack\n");
1875 for (i = 0; i < ScreenCount(display); i++) {
1877 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1878 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1879 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1881 /* start with screen geometry, adjust for bar */
1890 r->ws->cur_layout->l_stack(r->ws, &g);
1891 /* save r so we can track region changes */
1900 store_float_geom(struct ws_win *win, struct swm_region *r)
1902 /* retain window geom and region geom */
1903 win->g_float.x = win->g.x;
1904 win->g_float.y = win->g.y;
1905 win->g_float.w = win->g.w;
1906 win->g_float.h = win->g.h;
1907 win->rg_float.x = r->g.x;
1908 win->rg_float.y = r->g.y;
1909 win->rg_float.w = r->g.w;
1910 win->rg_float.h = r->g.h;
1911 win->g_floatvalid = 1;
1915 stack_floater(struct ws_win *win, struct swm_region *r)
1923 bzero(&wc, sizeof wc);
1924 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1927 * to allow windows to change their size (e.g. mplayer fs) only retrieve
1928 * geom on ws switches or return from max mode
1931 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid) ) {
1933 * use stored g and rg to set relative position and size
1934 * as in old region or before max stack mode
1936 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
1937 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
1938 win->g.w = win->g_float.w;
1939 win->g.h = win->g_float.h;
1940 win->g_floatvalid = 0;
1943 win->floatmaxed = 0;
1945 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
1946 (win->g.h >= HEIGHT(r)))
1947 wc.border_width = 0;
1949 wc.border_width = 1;
1950 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
1951 win->g.w = (double)WIDTH(r) * dialog_ratio;
1952 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1957 * floaters and transients are auto-centred unless moved
1960 win->g.x = r->g.x + (WIDTH(r) - win->g.w) / 2;
1961 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) / 2;
1964 /* win can be outside r if new r smaller than old r */
1965 /* Ensure top left corner inside r (move probs otherwise) */
1966 if (win->g.x < r->g.x )
1968 if (win->g.x > r->g.x + r->g.w - 1)
1969 win->g.x = (win->g.w > r->g.w) ? r->g.x :
1970 (r->g.x + r->g.w - win->g.w - 2);
1971 if (win->g.y < r->g.y )
1973 if (win->g.y > r->g.y + r->g.h - 1)
1974 win->g.y = (win->g.h > r->g.h) ? r->g.y :
1975 (r->g.y + r->g.h - win->g.h - 2);
1979 wc.width = win->g.w;
1980 wc.height = win->g.h;
1983 * Retain floater and transient geometry for correct positioning
1984 * when ws changes region
1986 store_float_geom(win, r);
1988 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
1989 win->id, wc.x, wc.y, wc.width, wc.height);
1991 XConfigureWindow(display, win->id, mask, &wc);
1996 * Send keystrokes to terminal to decrease/increase the font size as the
1997 * window size changes.
2000 adjust_font(struct ws_win *win)
2002 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2003 win->floating || win->transient)
2006 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2007 win->g.w / win->sh.width_inc < term_width &&
2008 win->font_steps < SWM_MAX_FONT_STEPS) {
2009 win->font_size_boundary[win->font_steps] =
2010 (win->sh.width_inc * term_width) + win->sh.base_width;
2013 win->last_inc = win->sh.width_inc;
2014 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2015 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2016 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
2019 win->last_inc = win->sh.width_inc;
2020 fake_keypress(win, XK_KP_Add, ShiftMask);
2024 #define SWAPXY(g) do { \
2026 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2027 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2030 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2033 XWindowAttributes wa;
2034 struct swm_geometry win_g, r_g = *g;
2036 int i, j, s, stacks;
2037 int w_inc = 1, h_inc, w_base = 1, h_base;
2038 int hrh, extra = 0, h_slice, last_h = 0;
2039 int split, colno, winno, mwin, msize, mscale;
2040 int remain, missing, v_slice, reconfigure;
2043 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2044 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2046 winno = count_win(ws, 0);
2047 if (winno == 0 && count_win(ws, 1) == 0)
2050 TAILQ_FOREACH(win, &ws->winlist, entry)
2051 if (win->transient == 0 && win->floating == 0)
2058 w_inc = win->sh.width_inc;
2059 w_base = win->sh.base_width;
2060 mwin = ws->l_state.horizontal_mwin;
2061 mscale = ws->l_state.horizontal_msize;
2062 stacks = ws->l_state.horizontal_stacks;
2065 w_inc = win->sh.height_inc;
2066 w_base = win->sh.base_height;
2067 mwin = ws->l_state.vertical_mwin;
2068 mscale = ws->l_state.vertical_msize;
2069 stacks = ws->l_state.vertical_stacks;
2073 if (stacks > winno - mwin)
2074 stacks = winno - mwin;
2078 h_slice = r_g.h / SWM_H_SLICE;
2079 if (mwin && winno > mwin) {
2080 v_slice = r_g.w / SWM_V_SLICE;
2084 win_g.w = v_slice * mscale;
2086 if (w_inc > 1 && w_inc < v_slice) {
2087 /* adjust for window's requested size increment */
2088 remain = (win_g.w - w_base) % w_inc;
2089 missing = w_inc - remain;
2096 win_g.x += r_g.w - msize;
2099 colno = split = winno / stacks;
2100 win_g.w = ((r_g.w - (stacks * 2) + 2) / stacks);
2102 hrh = r_g.h / colno;
2103 extra = r_g.h - (colno * hrh);
2106 /* stack all the tiled windows */
2107 i = j = 0, s = stacks;
2108 TAILQ_FOREACH(win, &ws->winlist, entry) {
2109 if (win->transient != 0 || win->floating != 0)
2112 if (split && i == split) {
2113 colno = (winno - mwin) / stacks;
2114 if (s <= (winno - mwin) % stacks)
2116 split = split + colno;
2117 hrh = (r_g.h / colno);
2118 extra = r_g.h - (colno * hrh);
2122 win_g.x += win_g.w + 2;
2123 win_g.w = (r_g.w - msize - (stacks * 2)) / stacks;
2125 win_g.w += (r_g.w - msize - (stacks * 2)) %
2132 h_inc = win->sh.width_inc;
2133 h_base = win->sh.base_width;
2135 h_inc = win->sh.height_inc;
2136 h_base = win->sh.base_height;
2138 if (j == colno - 1) {
2139 win_g.h = hrh + extra;
2140 } else if (h_inc > 1 && h_inc < h_slice) {
2141 /* adjust for window's requested size increment */
2142 remain = (win_g.h - h_base) % h_inc;
2143 missing = h_inc - remain;
2145 if (missing <= extra || j == 0) {
2157 win_g.y += last_h + 2;
2159 bzero(&wc, sizeof wc);
2160 if (disable_border && bar_enabled == 0 && winno == 1){
2161 wc.border_width = 0;
2165 wc.border_width = 1;
2168 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2169 win->g.w != win_g.h || win->g.h != win_g.w) {
2171 win->g.x = wc.x = win_g.y;
2172 win->g.y = wc.y = win_g.x;
2173 win->g.w = wc.width = win_g.h;
2174 win->g.h = wc.height = win_g.w;
2177 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2178 win->g.w != win_g.w || win->g.h != win_g.h) {
2180 win->g.x = wc.x = win_g.x;
2181 win->g.y = wc.y = win_g.y;
2182 win->g.w = wc.width = win_g.w;
2183 win->g.h = wc.height = win_g.h;
2188 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2189 XConfigureWindow(display, win->id, mask, &wc);
2193 if (XGetWindowAttributes(display, win->id, &wa))
2194 if (wa.map_state == IsUnmapped)
2195 XMapRaised(display, win->id);
2203 /* now, stack all the floaters and transients */
2204 TAILQ_FOREACH(win, &ws->winlist, entry) {
2205 if (win->transient == 0 && win->floating == 0)
2208 stack_floater(win, ws->r);
2209 XMapRaised(display, win->id);
2214 vertical_config(struct workspace *ws, int id)
2216 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2219 case SWM_ARG_ID_STACKRESET:
2220 case SWM_ARG_ID_STACKINIT:
2221 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2222 ws->l_state.vertical_mwin = 1;
2223 ws->l_state.vertical_stacks = 1;
2225 case SWM_ARG_ID_MASTERSHRINK:
2226 if (ws->l_state.vertical_msize > 1)
2227 ws->l_state.vertical_msize--;
2229 case SWM_ARG_ID_MASTERGROW:
2230 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2231 ws->l_state.vertical_msize++;
2233 case SWM_ARG_ID_MASTERADD:
2234 ws->l_state.vertical_mwin++;
2236 case SWM_ARG_ID_MASTERDEL:
2237 if (ws->l_state.vertical_mwin > 0)
2238 ws->l_state.vertical_mwin--;
2240 case SWM_ARG_ID_STACKINC:
2241 ws->l_state.vertical_stacks++;
2243 case SWM_ARG_ID_STACKDEC:
2244 if (ws->l_state.vertical_stacks > 1)
2245 ws->l_state.vertical_stacks--;
2253 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2255 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2257 stack_master(ws, g, 0, 0);
2261 horizontal_config(struct workspace *ws, int id)
2263 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2266 case SWM_ARG_ID_STACKRESET:
2267 case SWM_ARG_ID_STACKINIT:
2268 ws->l_state.horizontal_mwin = 1;
2269 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2270 ws->l_state.horizontal_stacks = 1;
2272 case SWM_ARG_ID_MASTERSHRINK:
2273 if (ws->l_state.horizontal_msize > 1)
2274 ws->l_state.horizontal_msize--;
2276 case SWM_ARG_ID_MASTERGROW:
2277 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2278 ws->l_state.horizontal_msize++;
2280 case SWM_ARG_ID_MASTERADD:
2281 ws->l_state.horizontal_mwin++;
2283 case SWM_ARG_ID_MASTERDEL:
2284 if (ws->l_state.horizontal_mwin > 0)
2285 ws->l_state.horizontal_mwin--;
2287 case SWM_ARG_ID_STACKINC:
2288 ws->l_state.horizontal_stacks++;
2290 case SWM_ARG_ID_STACKDEC:
2291 if (ws->l_state.horizontal_stacks > 1)
2292 ws->l_state.horizontal_stacks--;
2300 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2302 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
2304 stack_master(ws, g, 1, 0);
2307 /* fullscreen view */
2309 max_stack(struct workspace *ws, struct swm_geometry *g)
2312 struct swm_geometry gg = *g;
2313 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2317 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2322 winno = count_win(ws, 0);
2323 if (winno == 0 && count_win(ws, 1) == 0)
2326 TAILQ_FOREACH(win, &ws->winlist, entry) {
2327 if (win->transient) {
2329 parent = find_window(win->transient);
2333 if (win->floating && win->floatmaxed == 0 ) {
2335 * retain geometry for retrieval on exit from
2338 store_float_geom(win, ws->r);
2339 win->floatmaxed = 1;
2342 /* only reconfigure if necessary */
2343 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2345 bzero(&wc, sizeof wc);
2346 win->g.x = wc.x = gg.x;
2347 win->g.y = wc.y = gg.y;
2349 wc.border_width = 1;
2350 win->g.w = wc.width = gg.w;
2351 win->g.h = wc.height = gg.h;
2353 wc.border_width = 0;
2354 win->g.w = wc.width = gg.w + 2;
2355 win->g.h = wc.height = gg.h + 2;
2357 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2358 XConfigureWindow(display, win->id, mask, &wc);
2361 /* unmap only if we don't have multi screen */
2362 if (win != ws->focus)
2363 if (!(ScreenCount(display) > 1 || outputs > 1))
2367 /* put the last transient on top */
2370 XMapRaised(display, parent->id);
2371 stack_floater(wintrans, ws->r);
2372 focus_magic(wintrans, SWM_F_TRANSIENT);
2377 send_to_ws(struct swm_region *r, union arg *args)
2379 int wsid = args->id;
2380 struct ws_win *win = win;
2381 struct workspace *ws, *nws;
2382 Atom ws_idx_atom = 0;
2383 unsigned char ws_idx_str[SWM_PROPLEN];
2392 if (win->ws->idx == wsid)
2395 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2398 nws = &win->s->ws[wsid];
2400 a.id = SWM_ARG_ID_FOCUSPREV;
2403 TAILQ_REMOVE(&ws->winlist, win, entry);
2404 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2407 /* Try to update the window's workspace property */
2408 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2410 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2411 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2413 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2414 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2420 wkill(struct swm_region *r, union arg *args)
2422 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
2424 if(r->ws->focus == NULL)
2427 if (args->id == SWM_ARG_ID_KILLWINDOW)
2428 XKillClient(display, r->ws->focus->id);
2430 if (r->ws->focus->can_delete)
2431 client_msg(r->ws->focus, adelete);
2435 floating_toggle(struct swm_region *r, union arg *args)
2437 struct ws_win *win = r->ws->focus;
2443 /* reject floating toggles in max stack mode */
2444 if (r->ws->cur_layout == &layouts[SWM_MAX_STACK])
2447 if (win->floating) {
2448 if (!win->floatmaxed) {
2449 /* retain position for refloat */
2450 store_float_geom(win, r);
2454 if (win->g_floatvalid) {
2455 /* refloat at last floating relative position */
2456 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2457 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2458 win->g.w = win->g_float.w;
2459 win->g.h = win->g_float.h;
2465 a.id = SWM_ARG_ID_FOCUSCUR;
2466 focus(win->ws->r, &a);
2470 resize_window(struct ws_win *win, int center)
2474 struct swm_region *r;
2476 r = root_to_region(win->wa.root);
2477 bzero(&wc, sizeof wc);
2478 mask = CWBorderWidth | CWWidth | CWHeight;
2479 wc.border_width = 1;
2480 wc.width = win->g.w;
2481 wc.height = win->g.h;
2482 if (center == SWM_ARG_ID_CENTER) {
2483 wc.x = (WIDTH(r) - win->g.w) / 2;
2484 wc.y = (HEIGHT(r) - win->g.h) / 2;
2488 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
2489 win->id, wc.x, wc.y, wc.width, wc.height);
2491 XConfigureWindow(display, win->id, mask, &wc);
2496 resize(struct ws_win *win, union arg *args)
2500 struct swm_region *r = win->ws->r;
2505 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
2506 win->id, win->floating, win->transient);
2508 if (!(win->transient != 0 || win->floating != 0))
2511 /* reject resizes in max mode for floaters (transient ok) */
2512 if (win->floatmaxed)
2516 /* raise the window = move to last in window list */
2517 a.id = SWM_ARG_ID_MOVELAST;
2521 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2522 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2525 /* place pointer at bottom left corner or nearest point inside r */
2526 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
2529 relx = r->g.x + r->g.w - win->g.x - 2;
2531 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
2534 rely = r->g.y + r->g.h - win->g.y - 2;
2536 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
2538 XMaskEvent(display, MOUSEMASK | ExposureMask |
2539 SubstructureRedirectMask, &ev);
2541 case ConfigureRequest:
2544 handler[ev.type](&ev);
2547 /* do not allow resize outside of region */
2548 if ( ev.xmotion.x_root < r->g.x ||
2549 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
2550 ev.xmotion.y_root < r->g.y ||
2551 ev.xmotion.y_root > r->g.y + r->g.h - 1)
2554 if (ev.xmotion.x <= 1)
2556 if (ev.xmotion.y <= 1)
2558 win->g.w = ev.xmotion.x;
2559 win->g.h = ev.xmotion.y;
2561 /* not free, don't sync more than 60 times / second */
2562 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2563 time = ev.xmotion.time;
2564 XSync(display, False);
2565 resize_window(win, args->id);
2569 } while (ev.type != ButtonRelease);
2571 XSync(display, False);
2572 resize_window(win, args->id);
2574 store_float_geom(win,r);
2576 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
2578 XUngrabPointer(display, CurrentTime);
2581 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2585 move_window(struct ws_win *win)
2589 struct swm_region *r;
2591 r = root_to_region(win->wa.root);
2592 bzero(&wc, sizeof wc);
2597 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
2598 win->id, wc.x, wc.y, wc.width, wc.height);
2600 XConfigureWindow(display, win->id, mask, &wc);
2605 move(struct ws_win *win, union arg *args)
2609 struct swm_region *r = win->ws->r;
2612 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
2613 win->id, win->floating, win->transient);
2615 /* in max_stack mode should only move transients */
2616 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
2620 if (win->floating == 0 && !win->transient) {
2624 /* raise the window = move to last in window list */
2625 a.id = SWM_ARG_ID_MOVELAST;
2629 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2630 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2632 XWarpPointer(display, None, win->id, 0, 0, 0, 0, -1, -1);
2634 XMaskEvent(display, MOUSEMASK | ExposureMask |
2635 SubstructureRedirectMask, &ev);
2637 case ConfigureRequest:
2640 handler[ev.type](&ev);
2643 /* don't allow to move window origin out of region */
2644 if ( ev.xmotion.x_root < r->g.x ||
2645 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
2646 ev.xmotion.y_root < r->g.y ||
2647 ev.xmotion.y_root > r->g.y + r->g.h - 1)
2650 win->g.x = ev.xmotion.x_root;
2651 win->g.y = ev.xmotion.y_root;
2653 /* not free, don't sync more than 60 times / second */
2654 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2655 time = ev.xmotion.time;
2656 XSync(display, False);
2661 } while (ev.type != ButtonRelease);
2663 XSync(display, False);
2666 store_float_geom(win,r);
2667 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2668 XUngrabPointer(display, CurrentTime);
2671 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2674 /* user/key callable function IDs */
2733 /* key definitions */
2735 dummykeyfunc(struct swm_region *r, union arg *args)
2740 legacyfunc(struct swm_region *r, union arg *args)
2745 char name[SWM_FUNCNAME_LEN];
2746 void (*func)(struct swm_region *r, union arg *);
2748 } keyfuncs[kf_invalid + 1] = {
2749 /* name function argument */
2750 { "cycle_layout", cycle_layout, {0} },
2751 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
2752 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
2753 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
2754 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
2755 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
2756 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
2757 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
2758 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
2759 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
2760 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
2761 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
2762 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
2763 { "spawn_term", spawnterm, {.argv = spawn_term} },
2764 { "spawn_menu", legacyfunc, {0} },
2765 { "quit", quit, {0} },
2766 { "restart", restart, {0} },
2767 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
2768 { "ws_1", switchws, {.id = 0} },
2769 { "ws_2", switchws, {.id = 1} },
2770 { "ws_3", switchws, {.id = 2} },
2771 { "ws_4", switchws, {.id = 3} },
2772 { "ws_5", switchws, {.id = 4} },
2773 { "ws_6", switchws, {.id = 5} },
2774 { "ws_7", switchws, {.id = 6} },
2775 { "ws_8", switchws, {.id = 7} },
2776 { "ws_9", switchws, {.id = 8} },
2777 { "ws_10", switchws, {.id = 9} },
2778 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
2779 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
2780 { "ws_prior", priorws, {0} },
2781 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
2782 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
2783 { "mvws_1", send_to_ws, {.id = 0} },
2784 { "mvws_2", send_to_ws, {.id = 1} },
2785 { "mvws_3", send_to_ws, {.id = 2} },
2786 { "mvws_4", send_to_ws, {.id = 3} },
2787 { "mvws_5", send_to_ws, {.id = 4} },
2788 { "mvws_6", send_to_ws, {.id = 5} },
2789 { "mvws_7", send_to_ws, {.id = 6} },
2790 { "mvws_8", send_to_ws, {.id = 7} },
2791 { "mvws_9", send_to_ws, {.id = 8} },
2792 { "mvws_10", send_to_ws, {.id = 9} },
2793 { "bar_toggle", bar_toggle, {0} },
2794 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
2795 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
2796 { "screenshot_all", legacyfunc, {0} },
2797 { "screenshot_wind", legacyfunc, {0} },
2798 { "float_toggle", floating_toggle,{0} },
2799 { "version", version, {0} },
2800 { "spawn_lock", legacyfunc, {0} },
2801 { "spawn_initscr", legacyfunc, {0} },
2802 { "spawn_custom", dummykeyfunc, {0} },
2803 { "dumpwins", dumpwins, {0} },
2804 { "invalid key func", NULL, {0} },
2809 enum keyfuncid funcid;
2812 int keys_size = 0, keys_length = 0;
2813 struct key *keys = NULL;
2816 enum { client_click, root_click };
2818 unsigned int action;
2820 unsigned int button;
2821 void (*func)(struct ws_win *, union arg *);
2824 /* action key mouse button func args */
2825 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
2826 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
2827 { client_click, MODKEY, Button1, move, {0} },
2831 update_modkey(unsigned int mod)
2836 for (i = 0; i < keys_length; i++)
2837 if (keys[i].mod & ShiftMask)
2838 keys[i].mod = mod | ShiftMask;
2842 for (i = 0; i < LENGTH(buttons); i++)
2843 if (buttons[i].mask & ShiftMask)
2844 buttons[i].mask = mod | ShiftMask;
2846 buttons[i].mask = mod;
2856 int spawns_size = 0, spawns_length = 0;
2857 struct spawn_prog *spawns = NULL;
2860 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
2863 struct spawn_prog *prog = NULL;
2865 char *ap, **real_args;
2867 DNPRINTF(SWM_D_SPAWN, "spawn_custom %s\n", spawn_name);
2870 for (i = 0; i < spawns_length; i++) {
2871 if (!strcasecmp(spawn_name, spawns[i].name))
2875 fprintf(stderr, "spawn_custom: program %s not found\n",
2880 /* make room for expanded args */
2881 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
2882 err(1, "spawn_custom: calloc real_args");
2884 /* expand spawn_args into real_args */
2885 for (i = 0; i < prog->argc; i++) {
2887 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
2888 if (!strcasecmp(ap, "$bar_border")) {
2890 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
2892 err(1, "spawn_custom border color");
2893 } else if (!strcasecmp(ap, "$bar_color")) {
2895 strdup(r->s->c[SWM_S_COLOR_BAR].name))
2897 err(1, "spawn_custom bar color");
2898 } else if (!strcasecmp(ap, "$bar_font")) {
2899 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
2901 err(1, "spawn_custom bar fonts");
2902 } else if (!strcasecmp(ap, "$bar_font_color")) {
2904 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
2906 err(1, "spawn_custom color font");
2907 } else if (!strcasecmp(ap, "$color_focus")) {
2909 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
2911 err(1, "spawn_custom color focus");
2912 } else if (!strcasecmp(ap, "$color_unfocus")) {
2914 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
2916 err(1, "spawn_custom color unfocus");
2918 /* no match --> copy as is */
2919 if ((real_args[i] = strdup(ap)) == NULL)
2920 err(1, "spawn_custom strdup(ap)");
2922 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
2927 if ((swm_debug & SWM_D_SPAWN) != 0) {
2928 fprintf(stderr, "spawn_custom: result = ");
2929 for (i = 0; i < prog->argc; i++)
2930 fprintf(stderr, "\"%s\" ", real_args[i]);
2931 fprintf(stderr, "\n");
2937 for (i = 0; i < prog->argc; i++)
2943 setspawn(struct spawn_prog *prog)
2947 if (prog == NULL || prog->name == NULL)
2951 for (i = 0; i < spawns_length; i++) {
2952 if (!strcmp(spawns[i].name, prog->name)) {
2954 if (prog->argv == NULL) {
2956 DNPRINTF(SWM_D_SPAWN,
2957 "setspawn: delete #%d %s\n",
2959 free(spawns[i].name);
2960 for (j = 0; j < spawns[i].argc; j++)
2961 free(spawns[i].argv[j]);
2962 free(spawns[i].argv);
2963 j = spawns_length - 1;
2965 spawns[i] = spawns[j];
2970 DNPRINTF(SWM_D_SPAWN,
2971 "setspawn: replace #%d %s\n",
2973 free(spawns[i].name);
2974 for (j = 0; j < spawns[i].argc; j++)
2975 free(spawns[i].argv[j]);
2976 free(spawns[i].argv);
2979 /* found case handled */
2985 if (prog->argv == NULL) {
2987 "error: setspawn: cannot find program %s", prog->name);
2992 /* not found: add */
2993 if (spawns_size == 0 || spawns == NULL) {
2995 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
2996 spawns = malloc((size_t)spawns_size *
2997 sizeof(struct spawn_prog));
2998 if (spawns == NULL) {
2999 fprintf(stderr, "setspawn: malloc failed\n");
3003 } else if (spawns_length == spawns_size) {
3005 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
3006 spawns = realloc(spawns, (size_t)spawns_size *
3007 sizeof(struct spawn_prog));
3008 if (spawns == NULL) {
3009 fprintf(stderr, "setspawn: realloc failed\n");
3015 if (spawns_length < spawns_size) {
3016 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
3017 spawns_length, prog->name);
3018 i = spawns_length++;
3021 fprintf(stderr, "spawns array problem?\n");
3022 if (spawns == NULL) {
3023 fprintf(stderr, "spawns array is NULL!\n");
3031 setconfspawn(char *selector, char *value, int flags)
3033 struct spawn_prog *prog;
3034 char *vp, *cp, *word;
3036 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
3037 if ((prog = calloc(1, sizeof *prog)) == NULL)
3038 err(1, "setconfspawn: calloc prog");
3039 prog->name = strdup(selector);
3040 if (prog->name == NULL)
3041 err(1, "setconfspawn prog->name");
3042 if ((cp = vp = strdup(value)) == NULL)
3043 err(1, "setconfspawn: strdup(value) ");
3044 while ((word = strsep(&cp, " \t")) != NULL) {
3045 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
3047 cp += (long)strspn(cp, " \t");
3048 if (strlen(word) > 0) {
3050 prog->argv = realloc(prog->argv,
3051 prog->argc * sizeof(char *));
3052 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
3053 err(1, "setconfspawn: strdup");
3060 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
3067 setconfspawn("term", "xterm", 0);
3068 setconfspawn("screenshot_all", "screenshot.sh full", 0);
3069 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
3070 setconfspawn("lock", "xlock", 0);
3071 setconfspawn("initscr", "initscreen.sh", 0);
3072 setconfspawn("menu", "dmenu_run"
3075 " -nf $bar_font_color"
3077 " -sf $bar_color", 0);
3081 #define SWM_MODNAME_SIZE 32
3082 #define SWM_KEY_WS "\n+ \t"
3084 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
3088 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
3089 if (mod == NULL || ks == NULL) {
3090 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
3093 if (keystr == NULL || strlen(keystr) == 0) {
3094 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
3100 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
3101 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
3103 cp += (long)strspn(cp, SWM_KEY_WS);
3104 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
3106 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
3108 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
3110 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
3112 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
3114 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
3116 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
3117 *mod |= ControlMask;
3119 *ks = XStringToKeysym(name);
3120 XConvertCase(*ks, ks, &uks);
3121 if (ks == NoSymbol) {
3123 "parsekeys: invalid key %s\n",
3129 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
3134 strdupsafe(char *str)
3139 return (strdup(str));
3143 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
3146 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
3147 keyfuncs[kfid].name, spawn_name);
3149 for (i = 0; i < keys_length; i++) {
3150 if (keys[i].mod == mod && keys[i].keysym == ks) {
3151 if (kfid == kf_invalid) {
3154 "setkeybinding: delete #%d %s\n",
3155 i, keyfuncs[keys[i].funcid].name);
3156 free(keys[i].spawn_name);
3157 j = keys_length - 1;
3161 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3164 /* found: replace */
3166 "setkeybinding: replace #%d %s %s\n",
3167 i, keyfuncs[keys[i].funcid].name,
3169 free(keys[i].spawn_name);
3171 keys[i].keysym = ks;
3172 keys[i].funcid = kfid;
3173 keys[i].spawn_name = strdupsafe(spawn_name);
3174 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3179 if (kfid == kf_invalid) {
3181 "error: setkeybinding: cannot find mod/key combination");
3182 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3185 /* not found: add */
3186 if (keys_size == 0 || keys == NULL) {
3188 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
3189 keys = malloc((size_t)keys_size * sizeof(struct key));
3191 fprintf(stderr, "malloc failed\n");
3195 } else if (keys_length == keys_size) {
3197 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
3198 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
3200 fprintf(stderr, "realloc failed\n");
3205 if (keys_length < keys_size) {
3207 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
3208 j, keyfuncs[kfid].name, spawn_name);
3210 keys[j].keysym = ks;
3211 keys[j].funcid = kfid;
3212 keys[j].spawn_name = strdupsafe(spawn_name);
3214 fprintf(stderr, "keys array problem?\n");
3216 fprintf(stderr, "keys array problem\n");
3220 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3224 setconfbinding(char *selector, char *value, int flags)
3226 enum keyfuncid kfid;
3230 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
3231 if (selector == NULL) {
3232 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
3233 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3235 setkeybinding(mod, ks, kfid, NULL);
3240 /* search by key function name */
3241 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
3242 if (strncasecmp(selector, keyfuncs[kfid].name,
3243 SWM_FUNCNAME_LEN) == 0) {
3244 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3246 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3247 setkeybinding(mod, ks, kfid, NULL);
3253 /* search by custom spawn name */
3254 for (i = 0; i < spawns_length; i++) {
3255 if (strcasecmp(selector, spawns[i].name) == 0) {
3256 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3258 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3259 setkeybinding(mod, ks, kf_spawn_custom,
3266 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
3273 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
3274 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
3275 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
3276 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
3277 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
3278 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
3279 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
3280 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
3281 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
3282 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
3283 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
3284 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
3285 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
3286 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
3287 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
3288 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
3289 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
3290 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
3291 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
3292 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
3293 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
3294 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
3295 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
3296 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
3297 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
3298 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
3299 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
3300 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
3301 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
3302 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
3303 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
3304 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
3305 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
3306 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
3307 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
3308 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
3309 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
3310 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
3311 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
3312 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
3313 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
3314 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
3315 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
3316 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
3317 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
3318 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
3319 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
3320 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
3321 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
3322 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
3323 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
3324 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
3325 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
3326 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
3328 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
3333 updatenumlockmask(void)
3336 XModifierKeymap *modmap;
3338 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
3340 modmap = XGetModifierMapping(display);
3341 for (i = 0; i < 8; i++)
3342 for (j = 0; j < modmap->max_keypermod; j++)
3343 if (modmap->modifiermap[i * modmap->max_keypermod + j]
3344 == XKeysymToKeycode(display, XK_Num_Lock))
3345 numlockmask = (1 << i);
3347 XFreeModifiermap(modmap);
3353 unsigned int i, j, k;
3355 unsigned int modifiers[] =
3356 { 0, LockMask, numlockmask, numlockmask | LockMask };
3358 DNPRINTF(SWM_D_MISC, "grabkeys\n");
3359 updatenumlockmask();
3361 for (k = 0; k < ScreenCount(display); k++) {
3362 if (TAILQ_EMPTY(&screens[k].rl))
3364 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
3365 for (i = 0; i < keys_length; i++) {
3366 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
3367 for (j = 0; j < LENGTH(modifiers); j++)
3368 XGrabKey(display, code,
3369 keys[i].mod | modifiers[j],
3370 screens[k].root, True,
3371 GrabModeAsync, GrabModeAsync);
3377 grabbuttons(struct ws_win *win, int focused)
3380 unsigned int modifiers[] =
3381 { 0, LockMask, numlockmask, numlockmask|LockMask };
3383 updatenumlockmask();
3384 XUngrabButton(display, AnyButton, AnyModifier, win->id);
3386 for (i = 0; i < LENGTH(buttons); i++)
3387 if (buttons[i].action == client_click)
3388 for (j = 0; j < LENGTH(modifiers); j++)
3389 XGrabButton(display, buttons[i].button,
3390 buttons[i].mask | modifiers[j],
3391 win->id, False, BUTTONMASK,
3392 GrabModeAsync, GrabModeSync, None,
3395 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
3396 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
3399 const char *quirkname[] = {
3400 "NONE", /* config string for "no value" */
3408 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
3409 #define SWM_Q_WS "\n|+ \t"
3411 parsequirks(char *qstr, unsigned long *quirk)
3421 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
3423 cp += (long)strspn(cp, SWM_Q_WS);
3424 for (i = 0; i < LENGTH(quirkname); i++) {
3425 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
3426 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
3431 *quirk |= 1 << (i-1);
3435 if (i >= LENGTH(quirkname)) {
3436 DNPRINTF(SWM_D_QUIRK,
3437 "parsequirks: invalid quirk [%s]\n", name);
3445 setquirk(const char *class, const char *name, const int quirk)
3450 for (i = 0; i < quirks_length; i++) {
3451 if (!strcmp(quirks[i].class, class) &&
3452 !strcmp(quirks[i].name, name)) {
3455 DNPRINTF(SWM_D_QUIRK,
3456 "setquirk: delete #%d %s:%s\n",
3457 i, quirks[i].class, quirks[i].name);
3458 free(quirks[i].class);
3459 free(quirks[i].name);
3460 j = quirks_length - 1;
3462 quirks[i] = quirks[j];
3466 /* found: replace */
3467 DNPRINTF(SWM_D_QUIRK,
3468 "setquirk: replace #%d %s:%s\n",
3469 i, quirks[i].class, quirks[i].name);
3470 free(quirks[i].class);
3471 free(quirks[i].name);
3472 quirks[i].class = strdup(class);
3473 quirks[i].name = strdup(name);
3474 quirks[i].quirk = quirk;
3481 "error: setquirk: cannot find class/name combination");
3484 /* not found: add */
3485 if (quirks_size == 0 || quirks == NULL) {
3487 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
3488 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
3490 fprintf(stderr, "setquirk: malloc failed\n");
3494 } else if (quirks_length == quirks_size) {
3496 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
3497 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
3499 fprintf(stderr, "setquirk: realloc failed\n");
3504 if (quirks_length < quirks_size) {
3505 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
3506 j = quirks_length++;
3507 quirks[j].class = strdup(class);
3508 quirks[j].name = strdup(name);
3509 quirks[j].quirk = quirk;
3511 fprintf(stderr, "quirks array problem?\n");
3513 fprintf(stderr, "quirks array problem!\n");
3520 setconfquirk(char *selector, char *value, int flags)
3522 char *cp, *class, *name;
3524 unsigned long quirks;
3525 if (selector == NULL)
3527 if ((cp = strchr(selector, ':')) == NULL)
3532 if ((retval = parsequirks(value, &quirks)) == 0)
3533 setquirk(class, name, quirks);
3540 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
3541 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
3542 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
3543 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
3544 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3545 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
3546 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3547 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3548 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3549 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3550 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
3551 setquirk("pcb", "pcb", SWM_Q_FLOAT);
3554 /* conf file stuff */
3555 #define SWM_CONF_FILE "scrotwm.conf"
3557 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_STACK_ENABLED,
3558 SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT, SWM_S_CYCLE_EMPTY,
3559 SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH,
3560 SWM_S_TITLE_CLASS_ENABLED, SWM_S_TITLE_NAME_ENABLED,
3561 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BAR_FONT,
3562 SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM, SWM_S_SS_APP, SWM_S_DIALOG_RATIO,
3567 setconfvalue(char *selector, char *value, int flags)
3570 case SWM_S_BAR_DELAY:
3571 bar_delay = atoi(value);
3573 case SWM_S_BAR_ENABLED:
3574 bar_enabled = atoi(value);
3576 case SWM_S_BAR_AT_BOTTOM:
3577 bar_at_bottom = atoi(value);
3579 case SWM_S_STACK_ENABLED:
3580 stack_enabled = atoi(value);
3582 case SWM_S_CLOCK_ENABLED:
3583 clock_enabled = atoi(value);
3585 case SWM_S_CLOCK_FORMAT:
3586 #ifndef SWM_DENY_CLOCK_FORMAT
3588 if ((clock_format = strdup(value)) == NULL)
3589 err(1, "setconfvalue: clock_format");
3592 case SWM_S_CYCLE_EMPTY:
3593 cycle_empty = atoi(value);
3595 case SWM_S_CYCLE_VISIBLE:
3596 cycle_visible = atoi(value);
3598 case SWM_S_SS_ENABLED:
3599 ss_enabled = atoi(value);
3601 case SWM_S_TERM_WIDTH:
3602 term_width = atoi(value);
3604 case SWM_S_TITLE_CLASS_ENABLED:
3605 title_class_enabled = atoi(value);
3607 case SWM_S_TITLE_NAME_ENABLED:
3608 title_name_enabled = atoi(value);
3610 case SWM_S_FOCUS_MODE:
3611 if (!strcmp(value, "default"))
3612 focus_mode = SWM_FOCUS_DEFAULT;
3613 else if (!strcmp(value, "follow_cursor"))
3614 focus_mode = SWM_FOCUS_FOLLOW;
3615 else if (!strcmp(value, "synergy"))
3616 focus_mode = SWM_FOCUS_SYNERGY;
3618 err(1, "focus_mode");
3620 case SWM_S_DISABLE_BORDER:
3621 disable_border = atoi(value);
3623 case SWM_S_BAR_FONT:
3625 if ((bar_fonts[0] = strdup(value)) == NULL)
3626 err(1, "setconfvalue: bar_font");
3628 case SWM_S_BAR_ACTION:
3630 if ((bar_argv[0] = strdup(value)) == NULL)
3631 err(1, "setconfvalue: bar_action");
3633 case SWM_S_SPAWN_TERM:
3634 free(spawn_term[0]);
3635 if ((spawn_term[0] = strdup(value)) == NULL)
3636 err(1, "setconfvalue: spawn_term");
3640 case SWM_S_DIALOG_RATIO:
3641 dialog_ratio = atof(value);
3642 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
3652 setconfmodkey(char *selector, char *value, int flags)
3654 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
3655 update_modkey(Mod1Mask);
3656 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
3657 update_modkey(Mod2Mask);
3658 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
3659 update_modkey(Mod3Mask);
3660 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
3661 update_modkey(Mod4Mask);
3668 setconfcolor(char *selector, char *value, int flags)
3670 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
3675 setconfregion(char *selector, char *value, int flags)
3677 custom_region(value);
3681 /* config options */
3682 struct config_option {
3684 int (*func)(char*, char*, int);
3687 struct config_option configopt[] = {
3688 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
3689 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
3690 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
3691 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
3692 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
3693 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
3694 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
3695 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
3696 { "bind", setconfbinding, 0 },
3697 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
3698 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
3699 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
3700 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
3701 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
3702 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
3703 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
3704 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
3705 { "modkey", setconfmodkey, 0 },
3706 { "program", setconfspawn, 0 },
3707 { "quirk", setconfquirk, 0 },
3708 { "region", setconfregion, 0 },
3709 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
3710 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
3711 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
3712 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
3713 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
3714 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
3715 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
3716 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
3721 conf_load(char *filename)
3724 char *line, *cp, *optsub, *optval;
3725 size_t linelen, lineno = 0;
3726 int wordlen, i, optind;
3727 struct config_option *opt;
3729 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
3731 if (filename == NULL) {
3732 fprintf(stderr, "conf_load: no filename\n");
3735 if ((config = fopen(filename, "r")) == NULL) {
3736 warn("conf_load: fopen");
3740 while (!feof(config)) {
3741 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
3744 err(1, "%s", filename);
3749 cp += strspn(cp, " \t\n"); /* eat whitespace */
3750 if (cp[0] == '\0') {
3755 /* get config option */
3756 wordlen = strcspn(cp, "=[ \t\n");
3758 warnx("%s: line %zd: no option found",
3763 for (i = 0; i < LENGTH(configopt); i++) {
3764 opt = &configopt[i];
3765 if (!strncasecmp(cp, opt->optname, wordlen) &&
3766 strlen(opt->optname) == wordlen) {
3772 warnx("%s: line %zd: unknown option %.*s",
3773 filename, lineno, wordlen, cp);
3777 cp += strspn(cp, " \t\n"); /* eat whitespace */
3778 /* get [selector] if any */
3782 wordlen = strcspn(cp, "]");
3785 warnx("%s: line %zd: syntax error",
3789 asprintf(&optsub, "%.*s", wordlen, cp);
3792 cp += strspn(cp, "] \t\n"); /* eat trailing */
3794 cp += strspn(cp, "= \t\n"); /* eat trailing */
3796 optval = strdup(cp);
3797 /* call function to deal with it all */
3798 if (configopt[optind].func(optsub, optval,
3799 configopt[optind].funcflags) != 0) {
3800 fprintf(stderr, "%s line %zd: %s\n",
3801 filename, lineno, line);
3802 errx(1, "%s: line %zd: invalid data for %s",
3803 filename, lineno, configopt[optind].optname);
3811 DNPRINTF(SWM_D_CONF, "conf_load end\n");
3817 set_child_transient(struct ws_win *win)
3819 struct ws_win *parent;
3821 parent = find_window(win->transient);
3823 parent->child_trans = win;
3827 manage_window(Window id)
3830 struct workspace *ws;
3831 struct ws_win *win, *ww;
3832 int format, i, ws_idx, n, border_me = 0;
3833 unsigned long nitems, bytes;
3834 Atom ws_idx_atom = 0, type;
3835 Atom *prot = NULL, *pp;
3836 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
3837 struct swm_region *r;
3842 if ((win = find_window(id)) != NULL)
3843 return (win); /* already being managed */
3845 /* see if we are on the unmanaged list */
3846 if ((win = find_unmanaged_window(id)) != NULL) {
3847 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
3849 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
3850 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
3852 set_child_transient(win);
3856 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
3857 errx(1, "calloc: failed to allocate memory for new window");
3859 /* Get all the window data in one shot */
3860 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3862 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
3863 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
3864 XGetWindowAttributes(display, id, &win->wa);
3865 XGetWMNormalHints(display, id, &win->sh, &mask);
3866 XGetTransientForHint(display, id, &trans);
3868 win->transient = trans;
3869 set_child_transient(win);
3870 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
3871 win->id, win->transient);
3873 /* get supported protocols */
3874 if (XGetWMProtocols(display, id, &prot, &n)) {
3875 for (i = 0, pp = prot; i < n; i++, pp++) {
3876 if (*pp == takefocus)
3877 win->take_focus = 1;
3879 win->can_delete = 1;
3886 * Figure out where to put the window. If it was previously assigned to
3887 * a workspace (either by spawn() or manually moving), and isn't
3888 * transient, * put it in the same workspace
3890 r = root_to_region(win->wa.root);
3891 if (prop && win->transient == 0) {
3892 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
3893 ws_idx = strtonum(prop, 0, 9, &errstr);
3895 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
3898 ws = &r->s->ws[ws_idx];
3901 /* this should launch transients in the same ws as parent */
3903 if ((ww = find_window(trans)) != NULL)
3910 "fix this bug mcbride\n");
3915 /* set up the window layout */
3918 win->s = r->s; /* this never changes */
3919 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
3921 win->g.w = win->wa.width;
3922 win->g.h = win->wa.height;
3923 win->g.x = win->wa.x;
3924 win->g.y = win->wa.y;
3925 win->g_floatvalid = 0;
3926 win->floatmaxed = 0;
3928 /* Set window properties so we can remember this after reincarnation */
3929 if (ws_idx_atom && prop == NULL &&
3930 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
3931 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3933 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3934 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3938 if (XGetClassHint(display, win->id, &win->ch)) {
3939 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
3940 win->ch.res_class, win->ch.res_name);
3942 /* java is retarded so treat it special */
3943 if (strstr(win->ch.res_name, "sun-awt")) {
3948 for (i = 0; i < quirks_length; i++){
3949 if (!strcmp(win->ch.res_class, quirks[i].class) &&
3950 !strcmp(win->ch.res_name, quirks[i].name)) {
3951 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
3952 win->ch.res_class, win->ch.res_name);
3953 if (quirks[i].quirk & SWM_Q_FLOAT) {
3957 win->quirks = quirks[i].quirk;
3962 /* alter window position if quirky */
3963 if (win->quirks & SWM_Q_ANYWHERE) {
3964 win->manual = 1; /* don't center the quirky windows */
3965 bzero(&wc, sizeof wc);
3967 if (win->g.y < bar_height) {
3968 win->g.y = wc.y = bar_height;
3971 if (win->g.w + win->g.x > WIDTH(r)) {
3972 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
3978 /* Reset font sizes (the bruteforce way; no default keybinding). */
3979 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
3980 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3981 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3982 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3983 fake_keypress(win, XK_KP_Add, ShiftMask);
3988 bzero(&wc, sizeof wc);
3989 wc.border_width = 1;
3990 mask = CWBorderWidth;
3991 XConfigureWindow(display, win->id, mask, &wc);
3995 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
3996 PropertyChangeMask | StructureNotifyMask);
3998 set_win_state(win, NormalState);
4000 /* floaters need to be mapped if they are in the current workspace */
4001 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
4002 XMapRaised(display, win->id);
4008 free_window(struct ws_win *win)
4010 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
4015 /* needed for restart wm */
4016 set_win_state(win, WithdrawnState);
4018 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
4020 if (win->ch.res_class)
4021 XFree(win->ch.res_class);
4022 if (win->ch.res_name)
4023 XFree(win->ch.res_name);
4028 memset(win, 0xff, sizeof *win); /* XXX kill later */
4034 unmanage_window(struct ws_win *win)
4036 struct ws_win *parent;
4041 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
4043 if (win->transient) {
4044 parent = find_window(win->transient);
4046 parent->child_trans = NULL;
4049 /* work around for mplayer going full screen */
4053 TAILQ_REMOVE(&win->ws->winlist, win, entry);
4054 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
4060 focus_magic(struct ws_win *win, int do_trans)
4062 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu %d\n", WINID(win), do_trans);
4067 if (do_trans == SWM_F_TRANSIENT && win->child_trans) {
4068 /* win = parent & has a transient so focus on that */
4070 focus_win(win->child_trans);
4071 if (win->child_trans->take_focus)
4072 client_msg(win, takefocus);
4074 /* make sure transient hasn't dissapeared */
4075 if (validate_win(win->child_trans) == 0) {
4076 focus_win(win->child_trans);
4077 if (win->child_trans->take_focus)
4078 client_msg(win->child_trans, takefocus);
4080 win->child_trans = NULL;
4082 if (win->take_focus)
4083 client_msg(win, takefocus);
4089 if (win->take_focus)
4090 client_msg(win, takefocus);
4097 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
4105 XKeyEvent *ev = &e->xkey;
4107 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
4109 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
4110 for (i = 0; i < keys_length; i++)
4111 if (keysym == keys[i].keysym
4112 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
4113 && keyfuncs[keys[i].funcid].func) {
4114 if (keys[i].funcid == kf_spawn_custom)
4116 root_to_region(ev->root),
4117 &(keyfuncs[keys[i].funcid].args),
4121 keyfuncs[keys[i].funcid].func(
4122 root_to_region(ev->root),
4123 &(keyfuncs[keys[i].funcid].args)
4129 buttonpress(XEvent *e)
4133 XButtonPressedEvent *ev = &e->xbutton;
4135 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
4137 action = root_click;
4138 if ((win = find_window(ev->window)) == NULL)
4141 focus_magic(win, SWM_F_TRANSIENT);
4142 action = client_click;
4144 for (i = 0; i < LENGTH(buttons); i++)
4145 if (action == buttons[i].action && buttons[i].func &&
4146 buttons[i].button == ev->button &&
4147 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
4148 buttons[i].func(win, &buttons[i].args);
4152 configurerequest(XEvent *e)
4154 XConfigureRequestEvent *ev = &e->xconfigurerequest;
4159 if ((win = find_window(ev->window)) == NULL)
4160 if ((win = find_unmanaged_window(ev->window)) == NULL)
4164 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
4166 bzero(&wc, sizeof wc);
4169 wc.width = ev->width;
4170 wc.height = ev->height;
4171 wc.border_width = ev->border_width;
4172 wc.sibling = ev->above;
4173 wc.stack_mode = ev->detail;
4174 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
4176 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
4178 if (win->floating) {
4179 if (ev->value_mask & CWX)
4181 if (ev->value_mask & CWY)
4183 if (ev->value_mask & CWWidth)
4184 win->g.w = ev->width;
4185 if (ev->value_mask & CWHeight)
4186 win->g.h = ev->height;
4193 configurenotify(XEvent *e)
4198 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
4199 e->xconfigure.window);
4201 win = find_window(e->xconfigure.window);
4203 XGetWMNormalHints(display, win->id, &win->sh, &mask);
4211 destroynotify(XEvent *e)
4214 XDestroyWindowEvent *ev = &e->xdestroywindow;
4216 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
4218 if ((win = find_window(ev->window)) == NULL) {
4219 if ((win = find_unmanaged_window(ev->window)) == NULL)
4225 /* make sure we focus on something */
4228 unmanage_window(win);
4234 enternotify(XEvent *e)
4236 XCrossingEvent *ev = &e->xcrossing;
4241 Window focus_return;
4242 int revert_to_return;
4244 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
4245 "%lu subwindow %lu same_screen %d focus %d state %d\n",
4246 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
4247 ev->same_screen, ev->focus, ev->state);
4249 switch (focus_mode) {
4250 case SWM_FOCUS_DEFAULT:
4251 if (QLength(display)) {
4252 DNPRINTF(SWM_D_EVENT, "ignore enternotify %d\n",
4257 case SWM_FOCUS_FOLLOW:
4259 case SWM_FOCUS_SYNERGY:
4262 * all these checks need to be in this order because the
4263 * XCheckTypedWindowEvent relies on weeding out the previous events
4265 * making this code an option would enable a follow mouse for focus
4270 * state is set when we are switching workspaces and focus is set when
4271 * the window or a subwindow already has focus (occurs during restart).
4273 * Only honor the focus flag if last_focus_event is not FocusOut,
4274 * this allows scrotwm to continue to control focus when another
4275 * program is also playing with it.
4277 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
4278 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
4283 * happens when a window is created or destroyed and the border
4284 * crosses the mouse pointer and when switching ws
4286 * we need the subwindow test to see if we came from root in order
4287 * to give focus to floaters
4289 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
4290 ev->subwindow == 0) {
4291 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
4295 /* this window already has focus */
4296 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
4297 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
4301 /* this window is being deleted or moved to another ws */
4302 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
4304 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
4305 XPutBackEvent(display, &cne);
4309 if ((win = find_window(ev->window)) == NULL) {
4310 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
4315 * In fullstack kill all enters unless they come from a different ws
4316 * (i.e. another region) or focus has been grabbed externally.
4318 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
4319 last_focus_event != FocusOut) {
4320 XGetInputFocus(display, &focus_return, &revert_to_return);
4321 if ((w = find_window(focus_return)) == NULL ||
4323 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
4331 if ((win = find_window(ev->window)) == NULL) {
4332 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
4337 * if we have more enternotifies let them handle it in due time
4339 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
4340 DNPRINTF(SWM_D_EVENT,
4341 "ignoring enternotify: got more enternotify\n");
4342 XPutBackEvent(display, &cne);
4346 focus_magic(win, SWM_F_TRANSIENT);
4349 /* lets us use one switch statement for arbitrary mode/detail combinations */
4350 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
4353 focusevent(XEvent *e)
4357 u_int32_t mode_detail;
4358 XFocusChangeEvent *ev = &e->xfocus;
4360 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
4361 ev->type == FocusIn ? "entering" : "leaving",
4362 ev->window, ev->mode, ev->detail);
4364 if (last_focus_event == ev->type) {
4365 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
4369 last_focus_event = ev->type;
4370 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
4372 switch (mode_detail) {
4373 /* synergy client focus operations */
4374 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
4375 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
4377 /* synergy server focus operations */
4378 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
4380 /* Entering applications like rdesktop that mangle the pointer */
4381 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
4383 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
4384 XSetWindowBorder(display, win->id,
4385 win->ws->r->s->c[ev->type == FocusIn ?
4386 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
4389 fprintf(stderr, "ignoring focusevent\n");
4390 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
4397 mapnotify(XEvent *e)
4400 XMapEvent *ev = &e->xmap;
4402 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
4404 win = manage_window(ev->window);
4406 set_win_state(win, NormalState);
4410 mappingnotify(XEvent *e)
4412 XMappingEvent *ev = &e->xmapping;
4414 XRefreshKeyboardMapping(ev);
4415 if (ev->request == MappingKeyboard)
4420 maprequest(XEvent *e)
4423 struct swm_region *r;
4424 XWindowAttributes wa;
4425 XMapRequestEvent *ev = &e->xmaprequest;
4427 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
4428 e->xmaprequest.window);
4430 if (!XGetWindowAttributes(display, ev->window, &wa))
4432 if (wa.override_redirect)
4435 win = manage_window(e->xmaprequest.window);
4437 return; /* can't happen */
4441 /* make new win focused */
4442 r = root_to_region(win->wa.root);
4443 if (win->ws == r->ws)
4444 focus_magic(win, SWM_F_GENERIC);
4448 propertynotify(XEvent *e)
4451 XPropertyEvent *ev = &e->xproperty;
4453 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
4456 if (ev->state == PropertyDelete)
4457 return; /* ignore */
4458 win = find_window(ev->window);
4463 case XA_WM_NORMAL_HINTS:
4466 XGetWMNormalHints(display, win->id, &win->sh, &mask);
4467 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
4468 if (win->sh.flags & PMinSize) {
4469 win->g.w = win->sh.min_width;
4470 win->g.h = win->sh.min_height;
4471 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
4473 XMoveResizeWindow(display, win->id,
4474 win->g.x, win->g.y, win->g.w, win->g.h);
4483 unmapnotify(XEvent *e)
4487 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
4489 /* determine if we need to help unmanage this window */
4490 win = find_window(e->xunmap.window);
4494 if (getstate(e->xunmap.window) == NormalState) {
4495 unmanage_window(win);
4501 visibilitynotify(XEvent *e)
4504 struct swm_region *r;
4506 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
4507 e->xvisibility.window);
4508 if (e->xvisibility.state == VisibilityUnobscured)
4509 for (i = 0; i < ScreenCount(display); i++)
4510 TAILQ_FOREACH(r, &screens[i].rl, entry)
4511 if (e->xvisibility.window == r->bar_window)
4516 xerror_start(Display *d, XErrorEvent *ee)
4523 xerror(Display *d, XErrorEvent *ee)
4525 /* fprintf(stderr, "error: %p %p\n", display, ee); */
4533 xerrorxlib = XSetErrorHandler(xerror_start);
4535 /* this causes an error if some other window manager is running */
4536 XSelectInput(display, DefaultRootWindow(display),
4537 SubstructureRedirectMask);
4538 XSync(display, False);
4542 XSetErrorHandler(xerror);
4543 XSync(display, False);
4548 new_region(struct swm_screen *s, int x, int y, int w, int h)
4550 struct swm_region *r, *n;
4551 struct workspace *ws = NULL;
4554 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
4555 s->idx, w, h, x, y);
4557 /* remove any conflicting regions */
4558 n = TAILQ_FIRST(&s->rl);
4561 n = TAILQ_NEXT(r, entry);
4562 if (X(r) < (x + w) &&
4563 (X(r) + WIDTH(r)) > x &&
4565 (Y(r) + HEIGHT(r)) > y) {
4566 if (r->ws->r != NULL)
4567 r->ws->old_r = r->ws->r;
4569 XDestroyWindow(display, r->bar_window);
4570 TAILQ_REMOVE(&s->rl, r, entry);
4571 TAILQ_INSERT_TAIL(&s->orl, r, entry);
4575 /* search old regions for one to reuse */
4577 /* size + location match */
4578 TAILQ_FOREACH(r, &s->orl, entry)
4579 if (X(r) == x && Y(r) == y &&
4580 HEIGHT(r) == h && WIDTH(r) == w)
4584 TAILQ_FOREACH(r, &s->orl, entry)
4585 if (HEIGHT(r) == h && WIDTH(r) == w)
4589 TAILQ_REMOVE(&s->orl, r, entry);
4590 /* try to use old region's workspace */
4591 if (r->ws->r == NULL)
4594 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
4595 errx(1, "calloc: failed to allocate memory for screen");
4597 /* if we don't have a workspace already, find one */
4599 for (i = 0; i < SWM_WS_MAX; i++)
4600 if (s->ws[i].r == NULL) {
4607 errx(1, "no free workspaces\n");
4618 TAILQ_INSERT_TAIL(&s->rl, r, entry);
4624 #ifdef SWM_XRR_HAS_CRTC
4626 XRRScreenResources *sr;
4629 #endif /* SWM_XRR_HAS_CRTC */
4630 struct swm_region *r;
4633 if (i >= ScreenCount(display))
4634 errx(1, "invalid screen");
4636 /* remove any old regions */
4637 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
4638 r->ws->old_r = r->ws->r = NULL;
4639 XDestroyWindow(display, r->bar_window);
4640 TAILQ_REMOVE(&screens[i].rl, r, entry);
4641 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
4645 /* map virtual screens onto physical screens */
4646 #ifdef SWM_XRR_HAS_CRTC
4647 if (xrandr_support) {
4648 sr = XRRGetScreenResources(display, screens[i].root);
4650 new_region(&screens[i], 0, 0,
4651 DisplayWidth(display, i),
4652 DisplayHeight(display, i));
4656 for (c = 0, ci = NULL; c < ncrtc; c++) {
4657 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
4658 if (ci->noutput == 0)
4661 if (ci != NULL && ci->mode == None)
4662 new_region(&screens[i], 0, 0,
4663 DisplayWidth(display, i),
4664 DisplayHeight(display, i));
4666 new_region(&screens[i],
4667 ci->x, ci->y, ci->width, ci->height);
4670 XRRFreeCrtcInfo(ci);
4671 XRRFreeScreenResources(sr);
4673 #endif /* SWM_XRR_HAS_CRTC */
4675 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
4676 DisplayHeight(display, i));
4681 screenchange(XEvent *e) {
4682 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
4683 struct swm_region *r;
4686 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
4688 if (!XRRUpdateConfiguration(e))
4691 /* silly event doesn't include the screen index */
4692 for (i = 0; i < ScreenCount(display); i++)
4693 if (screens[i].root == xe->root)
4695 if (i >= ScreenCount(display))
4696 errx(1, "screenchange: screen not found\n");
4698 /* brute force for now, just re-enumerate the regions */
4701 /* add bars to all regions */
4702 for (i = 0; i < ScreenCount(display); i++)
4703 TAILQ_FOREACH(r, &screens[i].rl, entry)
4711 Window d1, d2, *wins = NULL;
4712 XWindowAttributes wa;
4715 int errorbase, major, minor;
4716 struct workspace *ws;
4720 if ((screens = calloc(ScreenCount(display),
4721 sizeof(struct swm_screen))) == NULL)
4722 errx(1, "calloc: screens");
4724 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4726 /* initial Xrandr setup */
4727 xrandr_support = XRRQueryExtension(display,
4728 &xrandr_eventbase, &errorbase);
4730 if (XRRQueryVersion(display, &major, &minor) && major < 1)
4733 /* map physical screens */
4734 for (i = 0; i < ScreenCount(display); i++) {
4735 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
4737 TAILQ_INIT(&screens[i].rl);
4738 TAILQ_INIT(&screens[i].orl);
4739 screens[i].root = RootWindow(display, i);
4741 /* set default colors */
4742 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
4743 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
4744 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
4745 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
4746 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
4748 /* set default cursor */
4749 XDefineCursor(display, screens[i].root,
4750 XCreateFontCursor(display, XC_left_ptr));
4752 /* init all workspaces */
4753 /* XXX these should be dynamically allocated too */
4754 for (j = 0; j < SWM_WS_MAX; j++) {
4755 ws = &screens[i].ws[j];
4760 TAILQ_INIT(&ws->winlist);
4761 TAILQ_INIT(&ws->unmanagedlist);
4763 for (k = 0; layouts[k].l_stack != NULL; k++)
4764 if (layouts[k].l_config != NULL)
4765 layouts[k].l_config(ws,
4766 SWM_ARG_ID_STACKINIT);
4767 ws->cur_layout = &layouts[0];
4769 /* grab existing windows (before we build the bars)*/
4770 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
4776 XRRSelectInput(display, screens[i].root,
4777 RRScreenChangeNotifyMask);
4779 /* attach windows to a region */
4780 /* normal windows */
4781 for (j = 0; j < no; j++) {
4782 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4783 wa.override_redirect ||
4784 XGetTransientForHint(display, wins[j], &d1))
4787 state = getstate(wins[j]);
4788 manage = state == IconicState;
4789 if (wa.map_state == IsViewable || manage)
4790 manage_window(wins[j]);
4792 /* transient windows */
4793 for (j = 0; j < no; j++) {
4794 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4795 wa.override_redirect)
4798 state = getstate(wins[j]);
4799 manage = state == IconicState;
4800 if (XGetTransientForHint(display, wins[j], &d1) &&
4802 manage_window(wins[j]);
4814 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
4816 err(1, "setup_globals: strdup");
4817 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
4819 err(1, "setup_globals: strdup");
4820 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
4822 err(1, "setup_globals: strdup");
4823 if ((spawn_term[0] = strdup("xterm")) == NULL)
4824 err(1, "setup_globals: strdup");
4825 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
4833 Atom netwmcheck, netwmname, utf8_string;
4836 /* work around sun jdk bugs, code from wmname */
4837 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
4838 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
4839 utf8_string = XInternAtom(display, "UTF8_STRING", False);
4840 for (i = 0; i < ScreenCount(display); i++) {
4841 root = screens[i].root;
4842 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
4843 PropModeReplace, (unsigned char *)&root, 1);
4844 XChangeProperty(display, root, netwmname, utf8_string, 8,
4845 PropModeReplace, "LG3D", strlen("LG3D"));
4850 main(int argc, char *argv[])
4853 struct swm_region *r, *rr;
4854 struct ws_win *winfocus = NULL;
4857 char conf[PATH_MAX], *cfile = NULL;
4862 struct sigaction sact;
4865 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
4866 SWM_VERSION, cvstag);
4867 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
4868 warnx("no locale support");
4870 if (!(display = XOpenDisplay(0)))
4871 errx(1, "can not open display");
4874 errx(1, "other wm running");
4876 /* handle some signals */
4877 bzero(&sact, sizeof(sact));
4878 sigemptyset(&sact.sa_mask);
4880 sact.sa_handler = sighdlr;
4881 sigaction(SIGINT, &sact, NULL);
4882 sigaction(SIGQUIT, &sact, NULL);
4883 sigaction(SIGTERM, &sact, NULL);
4884 sigaction(SIGHUP, &sact, NULL);
4886 sact.sa_handler = sighdlr;
4887 sact.sa_flags = SA_NOCLDSTOP;
4888 sigaction(SIGCHLD, &sact, NULL);
4890 astate = XInternAtom(display, "WM_STATE", False);
4891 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
4892 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
4893 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
4895 /* look for local and global conf file */
4896 pwd = getpwuid(getuid());
4898 errx(1, "invalid user %d", getuid());
4906 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
4907 if (stat(conf, &sb) != -1) {
4908 if (S_ISREG(sb.st_mode))
4911 /* try global conf file */
4912 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
4913 if (!stat(conf, &sb))
4914 if (S_ISREG(sb.st_mode))
4920 /* setup all bars */
4921 for (i = 0; i < ScreenCount(display); i++)
4922 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4923 if (winfocus == NULL)
4924 winfocus = TAILQ_FIRST(&r->ws->winlist);
4928 /* set some values to work around bad programs */
4936 xfd = ConnectionNumber(display);
4938 while (XPending(display)) {
4939 XNextEvent(display, &e);
4942 if (e.type < LASTEvent) {
4944 if (handler[e.type])
4945 handler[e.type](&e);
4947 DNPRINTF(SWM_D_EVENT,
4948 "win: %lu unknown event: %d\n",
4949 e.xany.window, e.type);
4951 switch (e.type - xrandr_eventbase) {
4952 case RRScreenChangeNotify:
4956 DNPRINTF(SWM_D_EVENT,
4957 "win: %lu unknown xrandr event: "
4958 "%d\n", e.xany.window, e.type);
4964 /* if we are being restarted go focus on first window */
4966 rr = winfocus->ws->r;
4968 /* not a visible window */
4972 /* move pointer to first screen if multi screen */
4973 if (ScreenCount(display) > 1 || outputs > 1)
4974 XWarpPointer(display, None, rr->s[0].root,
4975 0, 0, 0, 0, rr->g.x,
4976 rr->g.y + (bar_enabled ? bar_height : 0));
4978 a.id = SWM_ARG_ID_FOCUSCUR;
4988 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
4990 DNPRINTF(SWM_D_MISC, "select failed");
4991 if (restart_wm == 1)
4992 restart(NULL, NULL);
5002 XFreeGC(display, bar_gc);
5003 XCloseDisplay(display);