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.25"
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;
202 int bar_extra_running = 0;
205 int stack_enabled = 1;
206 int clock_enabled = 1;
207 char *clock_format = NULL;
208 int title_name_enabled = 0;
209 int title_class_enabled = 0;
210 int focus_mode = SWM_FOCUS_DEFAULT;
211 int disable_border = 0;
217 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
218 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
220 #define SWM_MENU_FN (2)
221 #define SWM_MENU_NB (4)
222 #define SWM_MENU_NF (6)
223 #define SWM_MENU_SB (8)
224 #define SWM_MENU_SF (10)
226 /* layout manager data */
227 struct swm_geometry {
237 /* virtual "screens" */
239 TAILQ_ENTRY(swm_region) entry;
240 struct swm_geometry g;
241 struct workspace *ws; /* current workspace on this region */
242 struct workspace *ws_prior; /* prior workspace on this region */
243 struct swm_screen *s; /* screen idx */
246 TAILQ_HEAD(swm_region_list, swm_region);
249 TAILQ_ENTRY(ws_win) entry;
252 struct ws_win *child_trans; /* transient child window */
253 struct swm_geometry g; /* current geometry */
254 struct swm_geometry g_float; /* geometry when floating */
255 struct swm_geometry rg_float; /* region geom when floating */
256 int g_floatvalid; /* flag: geometry in g_float is valid */
257 int floatmaxed; /* flag: floater was maxed in max_stack */
260 int font_size_boundary[SWM_MAX_FONT_STEPS];
266 unsigned long quirks;
267 struct workspace *ws; /* always valid */
268 struct swm_screen *s; /* always valid, never changes */
269 XWindowAttributes wa;
273 TAILQ_HEAD(ws_win_list, ws_win);
275 /* layout handlers */
277 void vertical_config(struct workspace *, int);
278 void vertical_stack(struct workspace *, struct swm_geometry *);
279 void horizontal_config(struct workspace *, int);
280 void horizontal_stack(struct workspace *, struct swm_geometry *);
281 void max_stack(struct workspace *, struct swm_geometry *);
283 struct ws_win *find_window(Window);
285 void grabbuttons(struct ws_win *, int);
286 void new_region(struct swm_screen *, int, int, int, int);
287 void unmanage_window(struct ws_win *);
288 long getstate(Window);
291 void (*l_stack)(struct workspace *, struct swm_geometry *);
292 void (*l_config)(struct workspace *, int);
294 #define SWM_L_FOCUSPREV (1<<0)
295 #define SWM_L_MAPONFOCUS (1<<1)
298 /* stack, configure */
299 { vertical_stack, vertical_config, 0, "[|]" },
300 { horizontal_stack, horizontal_config, 0, "[-]" },
302 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, "[ ]"},
303 { NULL, NULL, 0, NULL },
306 /* position of max_stack mode in the layouts array */
307 #define SWM_MAX_STACK 2
309 #define SWM_H_SLICE (32)
310 #define SWM_V_SLICE (32)
312 /* define work spaces */
314 int idx; /* workspace index */
315 struct layout *cur_layout; /* current layout handlers */
316 struct ws_win *focus; /* may be NULL */
317 struct ws_win *focus_prev; /* may be NULL */
318 struct swm_region *r; /* may be NULL */
319 struct swm_region *old_r; /* may be NULL */
320 struct ws_win_list winlist; /* list of windows in ws */
321 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
325 int horizontal_msize;
327 int horizontal_stacks;
334 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
335 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
337 /* physical screen mapping */
338 #define SWM_WS_MAX (10)
340 int idx; /* screen index */
341 struct swm_region_list rl; /* list of regions on this screen */
342 struct swm_region_list orl; /* list of old regions */
344 struct workspace ws[SWM_WS_MAX];
350 } c[SWM_S_COLOR_MAX];
352 struct swm_screen *screens;
355 /* args to functions */
358 #define SWM_ARG_ID_FOCUSNEXT (0)
359 #define SWM_ARG_ID_FOCUSPREV (1)
360 #define SWM_ARG_ID_FOCUSMAIN (2)
361 #define SWM_ARG_ID_FOCUSCUR (4)
362 #define SWM_ARG_ID_SWAPNEXT (10)
363 #define SWM_ARG_ID_SWAPPREV (11)
364 #define SWM_ARG_ID_SWAPMAIN (12)
365 #define SWM_ARG_ID_MOVELAST (13)
366 #define SWM_ARG_ID_MASTERSHRINK (20)
367 #define SWM_ARG_ID_MASTERGROW (21)
368 #define SWM_ARG_ID_MASTERADD (22)
369 #define SWM_ARG_ID_MASTERDEL (23)
370 #define SWM_ARG_ID_STACKRESET (30)
371 #define SWM_ARG_ID_STACKINIT (31)
372 #define SWM_ARG_ID_CYCLEWS_UP (40)
373 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
374 #define SWM_ARG_ID_CYCLESC_UP (42)
375 #define SWM_ARG_ID_CYCLESC_DOWN (43)
376 #define SWM_ARG_ID_STACKINC (50)
377 #define SWM_ARG_ID_STACKDEC (51)
378 #define SWM_ARG_ID_SS_ALL (60)
379 #define SWM_ARG_ID_SS_WINDOW (61)
380 #define SWM_ARG_ID_DONTCENTER (70)
381 #define SWM_ARG_ID_CENTER (71)
382 #define SWM_ARG_ID_KILLWINDOW (80)
383 #define SWM_ARG_ID_DELETEWINDOW (81)
387 void focus(struct swm_region *, union arg *);
388 void focus_magic(struct ws_win *, int);
389 #define SWM_F_GENERIC (0)
390 #define SWM_F_TRANSIENT (1)
396 #define SWM_Q_FLOAT (1<<0) /* float this window */
397 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
398 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
399 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
400 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
402 int quirks_size = 0, quirks_length = 0;
403 struct quirk *quirks = NULL;
420 name = "ButtonPress";
423 name = "ButtonRelease";
426 name = "MotionNotify";
429 name = "EnterNotify";
432 name = "LeaveNotify";
441 name = "KeymapNotify";
447 name = "GraphicsExpose";
452 case VisibilityNotify:
453 name = "VisibilityNotify";
456 name = "CreateNotify";
459 name = "DestroyNotify";
462 name = "UnmapNotify";
471 name = "ReparentNotify";
473 case ConfigureNotify:
474 name = "ConfigureNotify";
476 case ConfigureRequest:
477 name = "ConfigureRequest";
480 name = "GravityNotify";
483 name = "ResizeRequest";
485 case CirculateNotify:
486 name = "CirculateNotify";
488 case CirculateRequest:
489 name = "CirculateRequest";
492 name = "PropertyNotify";
495 name = "SelectionClear";
497 case SelectionRequest:
498 name = "SelectionRequest";
500 case SelectionNotify:
501 name = "SelectionNotify";
504 name = "ColormapNotify";
507 name = "ClientMessage";
510 name = "MappingNotify";
515 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
517 e->xany.window, name, e->type, QLength(display));
519 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
521 e->xany.window, e->type, QLength(display));
525 dumpwins(struct swm_region *r, union arg *args)
529 XWindowAttributes wa;
532 fprintf(stderr, "invalid workspace\n");
536 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
538 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
539 state = getstate(win->id);
540 if (!XGetWindowAttributes(display, win->id, &wa))
541 fprintf(stderr, "window: %lu failed "
542 "XGetWindowAttributes\n", win->id);
543 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
544 win->id, wa.map_state, state);
547 fprintf(stderr, "===== unmanaged window list =====\n");
548 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
549 state = getstate(win->id);
550 if (!XGetWindowAttributes(display, win->id, &wa))
551 fprintf(stderr, "window: %lu failed "
552 "XGetWindowAttributes\n", win->id);
553 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
554 win->id, wa.map_state, state);
557 fprintf(stderr, "=================================\n");
562 dumpwins(struct swm_region *r, union arg *args)
565 #endif /* SWM_DEBUG */
567 void expose(XEvent *);
568 void keypress(XEvent *);
569 void buttonpress(XEvent *);
570 void configurerequest(XEvent *);
571 void configurenotify(XEvent *);
572 void destroynotify(XEvent *);
573 void enternotify(XEvent *);
574 void focusevent(XEvent *);
575 void mapnotify(XEvent *);
576 void mappingnotify(XEvent *);
577 void maprequest(XEvent *);
578 void propertynotify(XEvent *);
579 void unmapnotify(XEvent *);
580 void visibilitynotify(XEvent *);
582 void (*handler[LASTEvent])(XEvent *) = {
584 [KeyPress] = keypress,
585 [ButtonPress] = buttonpress,
586 [ConfigureRequest] = configurerequest,
587 [ConfigureNotify] = configurenotify,
588 [DestroyNotify] = destroynotify,
589 [EnterNotify] = enternotify,
590 [FocusIn] = focusevent,
591 [FocusOut] = focusevent,
592 [MapNotify] = mapnotify,
593 [MappingNotify] = mappingnotify,
594 [MapRequest] = maprequest,
595 [PropertyNotify] = propertynotify,
596 [UnmapNotify] = unmapnotify,
597 [VisibilityNotify] = visibilitynotify,
603 int saved_errno, status;
610 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
616 warn("sighdlr: waitpid");
617 #endif /* SWM_DEBUG */
622 if (WIFEXITED(status)) {
623 if (WEXITSTATUS(status) != 0)
624 warnx("sighdlr: child exit status: %d",
625 WEXITSTATUS(status));
627 warnx("sighdlr: child is terminated "
629 #endif /* SWM_DEBUG */
647 name_to_color(char *colorname)
651 XColor screen_def, exact_def;
652 unsigned long result = 0;
653 char cname[32] = "#";
655 cmap = DefaultColormap(display, screens[0].idx);
656 status = XAllocNamedColor(display, cmap, colorname,
657 &screen_def, &exact_def);
659 strlcat(cname, colorname + 2, sizeof cname - 1);
660 status = XAllocNamedColor(display, cmap, cname, &screen_def,
664 result = screen_def.pixel;
666 fprintf(stderr, "color '%s' not found.\n", colorname);
672 setscreencolor(char *val, int i, int c)
674 if (i > 0 && i <= ScreenCount(display)) {
675 screens[i - 1].c[c].color = name_to_color(val);
676 free(screens[i - 1].c[c].name);
677 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
679 } else if (i == -1) {
680 for (i = 0; i < ScreenCount(display); i++) {
681 screens[i].c[c].color = name_to_color(val);
682 free(screens[i].c[c].name);
683 if ((screens[i].c[c].name = strdup(val)) == NULL)
687 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
688 i, ScreenCount(display));
692 custom_region(char *val)
694 unsigned int sidx, x, y, w, h;
696 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
697 errx(1, "invalid custom region, "
698 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
699 if (sidx < 1 || sidx > ScreenCount(display))
700 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
701 sidx, ScreenCount(display));
705 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
707 if (x < 0 || x > DisplayWidth(display, sidx) ||
708 y < 0 || y > DisplayHeight(display, sidx) ||
709 w + x > DisplayWidth(display, sidx) ||
710 h + y > DisplayHeight(display, sidx))
711 errx(1, "region %ux%u+%u+%u not within screen boundaries "
712 "(%ux%u)\n", w, h, x, y,
713 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
715 new_region(&screens[sidx], x, y, w, h);
719 socket_setnonblock(int fd)
723 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
724 err(1, "fcntl F_GETFL");
726 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
727 err(1, "fcntl F_SETFL");
731 bar_print(struct swm_region *r, char *s)
733 XClearWindow(display, r->bar_window);
734 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
735 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
744 bzero(bar_pipe, sizeof bar_pipe);
747 kill(bar_pid, SIGTERM);
750 strlcpy(bar_ext, "", sizeof bar_ext);
755 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
757 int do_class, do_name;
759 XClassHint *xch = NULL;
761 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
763 if ((xch = XAllocClassHint()) == NULL)
765 status = XGetClassHint(display, cur_focus->id, xch);
766 if (status == BadWindow || status == BadAlloc)
768 do_class = (title_class_enabled && xch->res_class != NULL);
769 do_name = (title_name_enabled && xch->res_name != NULL);
771 strlcat(s, xch->res_class, sz);
772 if (do_class && do_name)
775 strlcat(s, xch->res_name, sz);
788 struct swm_region *r;
792 char cn[SWM_BAR_MAX];
793 char loc[SWM_BAR_MAX];
797 if (bar_enabled == 0)
799 if (bar_extra && bar_extra_running) {
800 /* ignore short reads; it'll correct itself */
801 while ((b = fgetln(stdin, &len)) != NULL)
802 if (b && b[len - 1] == '\n') {
804 strlcpy(bar_ext, b, sizeof bar_ext);
806 if (b == NULL && errno != EAGAIN) {
807 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
808 errno, strerror(errno));
812 strlcpy(bar_ext, "", sizeof bar_ext);
814 if (clock_enabled == 0)
815 strlcpy(s, "", sizeof s);
818 localtime_r(&tmt, &tm);
819 strftime(s, sizeof s, clock_format, &tm);
820 strlcat(s, " ", sizeof s);
823 for (i = 0; i < ScreenCount(display); i++) {
825 TAILQ_FOREACH(r, &screens[i].rl, entry) {
826 strlcpy(cn, "", sizeof cn);
828 bar_class_name(cn, sizeof cn, r->ws->focus);
831 stack = r->ws->cur_layout->name;
833 snprintf(loc, sizeof loc, "%d:%d %s %s%s %s %s",
834 x++, r->ws->idx + 1, stack, s, cn, bar_ext,
849 bar_toggle(struct swm_region *r, union arg *args)
851 struct swm_region *tmpr;
852 int i, sc = ScreenCount(display);
854 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
857 for (i = 0; i < sc; i++)
858 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
859 XUnmapWindow(display, tmpr->bar_window);
861 for (i = 0; i < sc; i++)
862 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
863 XMapRaised(display, tmpr->bar_window);
865 bar_enabled = !bar_enabled;
868 /* must be after stack */
875 XSetWindowAttributes wa;
876 struct swm_region *r;
879 /* do this here because the conf file is in memory */
880 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
881 /* launch external status app */
882 bar_extra_running = 1;
883 if (pipe(bar_pipe) == -1)
884 err(1, "pipe error");
885 socket_setnonblock(bar_pipe[0]);
886 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
887 if (dup2(bar_pipe[0], 0) == -1)
889 if (dup2(bar_pipe[1], 1) == -1)
891 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
892 err(1, "could not disable SIGPIPE");
893 switch (bar_pid = fork()) {
895 err(1, "cannot fork");
899 execvp(bar_argv[0], bar_argv);
900 err(1, "%s external app failed", bar_argv[0]);
902 default: /* parent */
908 bzero(&wa, sizeof wa);
909 for (i = 0; i < ScreenCount(display); i++)
910 TAILQ_FOREACH(r, &screens[i].rl, entry) {
912 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
913 wa.background_pixel =
914 screens[i].c[SWM_S_COLOR_BAR].color;
915 XChangeWindowAttributes(display, r->bar_window,
916 CWBackPixel | CWBorderPixel, &wa);
922 bar_setup(struct swm_region *r)
927 XFreeFont(display, bar_fs);
931 for (i = 0; bar_fonts[i] != NULL; i++) {
932 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
938 if (bar_fonts[i] == NULL)
939 errx(1, "couldn't load font");
941 errx(1, "couldn't create font structure");
943 bar_height = bar_fs->ascent + bar_fs->descent + 3;
945 r->bar_window = XCreateSimpleWindow(display,
946 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
947 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
948 r->s->c[SWM_S_COLOR_BAR].color);
949 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
950 XSetFont(display, bar_gc, bar_fs->fid);
951 XSelectInput(display, r->bar_window, VisibilityChangeMask);
953 XMapRaised(display, r->bar_window);
954 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
956 if (signal(SIGALRM, bar_signal) == SIG_ERR)
957 err(1, "could not install bar_signal");
962 set_win_state(struct ws_win *win, long state)
964 long data[] = {state, None};
966 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
971 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
972 (unsigned char *)data, 2);
980 unsigned char *p = NULL;
981 unsigned long n, extra;
984 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
985 &real, &format, &n, &extra, (unsigned char **)&p);
986 if (status != Success)
989 result = *((long *)p);
995 version(struct swm_region *r, union arg *args)
997 bar_version = !bar_version;
999 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
1000 SWM_VERSION, cvstag);
1002 strlcpy(bar_vertext, "", sizeof bar_vertext);
1007 client_msg(struct ws_win *win, Atom a)
1009 XClientMessageEvent cm;
1014 bzero(&cm, sizeof cm);
1015 cm.type = ClientMessage;
1016 cm.window = win->id;
1017 cm.message_type = aprot;
1020 cm.data.l[1] = CurrentTime;
1021 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1025 configreq_win(struct ws_win *win)
1027 XConfigureRequestEvent cr;
1032 bzero(&cr, sizeof cr);
1033 cr.type = ConfigureRequest;
1034 cr.display = display;
1035 cr.parent = win->id;
1036 cr.window = win->id;
1039 cr.width = win->g.w;
1040 cr.height = win->g.h;
1041 cr.border_width = 1;
1043 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&cr);
1047 config_win(struct ws_win *win)
1051 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
1052 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1057 ce.type = ConfigureNotify;
1058 ce.display = display;
1060 ce.window = win->id;
1063 ce.width = win->g.w;
1064 ce.height = win->g.h;
1065 ce.border_width = 1; /* XXX store this! */
1067 ce.override_redirect = False;
1068 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1072 count_win(struct workspace *ws, int count_transient)
1077 TAILQ_FOREACH(win, &ws->winlist, entry) {
1078 if (count_transient == 0 && win->floating)
1080 if (count_transient == 0 && win->transient)
1084 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1090 quit(struct swm_region *r, union arg *args)
1092 DNPRINTF(SWM_D_MISC, "quit\n");
1097 unmap_window(struct ws_win *win)
1102 /* don't unmap again */
1103 if (getstate(win->id) == IconicState)
1106 set_win_state(win, IconicState);
1108 XUnmapWindow(display, win->id);
1110 XSetWindowBorder(display, win->id,
1111 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1120 for (i = 0; i < ScreenCount(display); i++)
1121 for (j = 0; j < SWM_WS_MAX; j++)
1122 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1127 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1134 event.display = display; /* Ignored, but what the hell */
1135 event.window = win->id;
1136 event.root = win->s->root;
1137 event.subwindow = None;
1138 event.time = CurrentTime;
1143 event.same_screen = True;
1144 event.keycode = XKeysymToKeycode(display, keysym);
1145 event.state = modifiers;
1147 event.type = KeyPress;
1148 XSendEvent(event.display, event.window, True,
1149 KeyPressMask, (XEvent *)&event);
1151 event.type = KeyRelease;
1152 XSendEvent(event.display, event.window, True,
1153 KeyPressMask, (XEvent *)&event);
1158 restart(struct swm_region *r, union arg *args)
1160 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1162 /* disable alarm because the following code may not be interrupted */
1164 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1165 errx(1, "can't disable alarm");
1170 XCloseDisplay(display);
1171 execvp(start_argv[0], start_argv);
1172 fprintf(stderr, "execvp failed\n");
1178 root_to_region(Window root)
1180 struct swm_region *r = NULL;
1182 int i, x, y, wx, wy;
1185 for (i = 0; i < ScreenCount(display); i++)
1186 if (screens[i].root == root)
1189 if (XQueryPointer(display, screens[i].root,
1190 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1191 /* choose a region based on pointer location */
1192 TAILQ_FOREACH(r, &screens[i].rl, entry)
1193 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1194 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1199 r = TAILQ_FIRST(&screens[i].rl);
1205 find_unmanaged_window(Window id)
1210 for (i = 0; i < ScreenCount(display); i++)
1211 for (j = 0; j < SWM_WS_MAX; j++)
1212 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1220 find_window(Window id)
1223 Window wrr, wpr, *wcr = NULL;
1226 for (i = 0; i < ScreenCount(display); i++)
1227 for (j = 0; j < SWM_WS_MAX; j++)
1228 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1232 /* if we were looking for the parent return that window instead */
1233 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1238 /* ignore not found and root */
1239 if (wpr == 0 || wrr == wpr)
1242 /* look for parent */
1243 for (i = 0; i < ScreenCount(display); i++)
1244 for (j = 0; j < SWM_WS_MAX; j++)
1245 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1253 spawn(struct swm_region *r, union arg *args)
1258 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1262 close(ConnectionNumber(display));
1264 setenv("LD_PRELOAD", SWM_LIB, 1);
1266 if (asprintf(&ret, "%d", r->ws->idx) == -1) {
1270 setenv("_SWM_WS", ret, 1);
1274 if (asprintf(&ret, "%d", getpid()) == -1) {
1278 setenv("_SWM_PID", ret, 1);
1282 if (setsid() == -1) {
1288 * close stdin and stdout to prevent interaction between apps
1289 * and the baraction script
1290 * leave stderr open to record errors
1292 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
1296 dup2(fd, STDIN_FILENO);
1297 dup2(fd, STDOUT_FILENO);
1301 execvp(args->argv[0], args->argv);
1309 spawnterm(struct swm_region *r, union arg *args)
1311 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1314 setenv("_SWM_XTERM_FONTADJ", "", 1);
1319 kill_refs(struct ws_win *win)
1322 struct swm_region *r;
1323 struct workspace *ws;
1328 for (i = 0; i < ScreenCount(display); i++)
1329 TAILQ_FOREACH(r, &screens[i].rl, entry)
1330 for (x = 0; x < SWM_WS_MAX; x++) {
1332 if (win == ws->focus)
1334 if (win == ws->focus_prev)
1335 ws->focus_prev = NULL;
1340 validate_win(struct ws_win *testwin)
1343 struct workspace *ws;
1344 struct swm_region *r;
1345 int i, x, foundit = 0;
1347 if (testwin == NULL)
1350 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1351 TAILQ_FOREACH(r, &screens[i].rl, entry)
1352 for (x = 0; x < SWM_WS_MAX; x++) {
1354 TAILQ_FOREACH(win, &ws->winlist, entry)
1362 validate_ws(struct workspace *testws)
1364 struct swm_region *r;
1365 struct workspace *ws;
1368 /* validate all ws */
1369 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1370 TAILQ_FOREACH(r, &screens[i].rl, entry)
1371 for (x = 0; x < SWM_WS_MAX; x++) {
1380 unfocus_win(struct ws_win *win)
1384 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1388 if (win->ws == NULL)
1391 if (validate_ws(win->ws))
1394 if (win->ws->r == NULL)
1397 if (validate_win(win)) {
1402 if (win->ws->focus == win) {
1403 win->ws->focus = NULL;
1404 win->ws->focus_prev = win;
1407 if (validate_win(win->ws->focus)) {
1408 kill_refs(win->ws->focus);
1409 win->ws->focus = NULL;
1411 if (validate_win(win->ws->focus_prev)) {
1412 kill_refs(win->ws->focus_prev);
1413 win->ws->focus_prev = NULL;
1416 /* drain all previous unfocus events */
1417 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
1420 grabbuttons(win, 0);
1421 XSetWindowBorder(display, win->id,
1422 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1432 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
1434 for (i = 0; i < ScreenCount(display); i++)
1435 for (j = 0; j < SWM_WS_MAX; j++)
1436 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1441 focus_win(struct ws_win *win)
1446 struct ws_win *cfw = NULL;
1449 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1453 if (win->ws == NULL)
1456 if (validate_ws(win->ws))
1458 if (validate_win(win)) {
1463 if (validate_win(win)) {
1468 XGetInputFocus(display, &cur_focus, &rr);
1469 if ((cfw = find_window(cur_focus)) != NULL)
1472 win->ws->focus = win;
1474 if (win->ws->r != NULL) {
1475 /* drain all previous focus events */
1476 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
1480 XSetInputFocus(display, win->id,
1481 RevertToParent, CurrentTime);
1482 grabbuttons(win, 1);
1483 XSetWindowBorder(display, win->id,
1484 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1485 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS)
1486 XMapRaised(display, win->id);
1491 switchws(struct swm_region *r, union arg *args)
1493 int wsid = args->id, unmap_old = 0;
1494 struct swm_region *this_r, *other_r;
1496 struct workspace *new_ws, *old_ws;
1503 old_ws = this_r->ws;
1504 new_ws = &this_r->s->ws[wsid];
1506 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1507 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1510 if (new_ws == NULL || old_ws == NULL)
1512 if (new_ws == old_ws)
1515 other_r = new_ws->r;
1516 if (other_r == NULL) {
1517 /* the other workspace is hidden, hide this one */
1521 /* the other ws is visible in another region, exchange them */
1522 other_r->ws_prior = new_ws;
1523 other_r->ws = old_ws;
1524 old_ws->r = other_r;
1526 this_r->ws_prior = old_ws;
1527 this_r->ws = new_ws;
1531 a.id = SWM_ARG_ID_FOCUSCUR;
1532 focus(new_ws->r, &a);
1535 /* unmap old windows */
1537 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1542 cyclews(struct swm_region *r, union arg *args)
1545 struct swm_screen *s = r->s;
1547 DNPRINTF(SWM_D_WS, "cyclews id %d "
1548 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1549 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1554 case SWM_ARG_ID_CYCLEWS_UP:
1555 if (a.id < SWM_WS_MAX - 1)
1560 case SWM_ARG_ID_CYCLEWS_DOWN:
1564 a.id = SWM_WS_MAX - 1;
1570 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1572 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1576 } while (a.id != r->ws->idx);
1580 priorws(struct swm_region *r, union arg *args)
1584 DNPRINTF(SWM_D_WS, "priorws id %d "
1585 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1586 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1588 if (r->ws_prior == NULL)
1591 a.id = r->ws_prior->idx;
1596 cyclescr(struct swm_region *r, union arg *args)
1598 struct swm_region *rr = NULL;
1602 /* do nothing if we don't have more than one screen */
1603 if (!(ScreenCount(display) > 1 || outputs > 1))
1608 case SWM_ARG_ID_CYCLESC_UP:
1609 rr = TAILQ_NEXT(r, entry);
1611 rr = TAILQ_FIRST(&screens[i].rl);
1613 case SWM_ARG_ID_CYCLESC_DOWN:
1614 rr = TAILQ_PREV(r, swm_region_list, entry);
1616 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1624 /* move mouse to region */
1626 y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
1627 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1629 a.id = SWM_ARG_ID_FOCUSCUR;
1632 if (rr->ws->focus) {
1633 /* move to focus window */
1634 x = rr->ws->focus->g.x + 1;
1635 y = rr->ws->focus->g.y + 1;
1636 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1641 swapwin(struct swm_region *r, union arg *args)
1643 struct ws_win *target, *source;
1644 struct ws_win *cur_focus;
1645 struct ws_win_list *wl;
1648 DNPRINTF(SWM_D_WS, "swapwin id %d "
1649 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1650 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1652 cur_focus = r->ws->focus;
1653 if (cur_focus == NULL)
1657 wl = &source->ws->winlist;
1660 case SWM_ARG_ID_SWAPPREV:
1661 target = TAILQ_PREV(source, ws_win_list, entry);
1662 TAILQ_REMOVE(wl, cur_focus, entry);
1664 TAILQ_INSERT_TAIL(wl, source, entry);
1666 TAILQ_INSERT_BEFORE(target, source, entry);
1668 case SWM_ARG_ID_SWAPNEXT:
1669 target = TAILQ_NEXT(source, entry);
1670 TAILQ_REMOVE(wl, source, entry);
1672 TAILQ_INSERT_HEAD(wl, source, entry);
1674 TAILQ_INSERT_AFTER(wl, target, source, entry);
1676 case SWM_ARG_ID_SWAPMAIN:
1677 target = TAILQ_FIRST(wl);
1678 if (target == source) {
1679 if (source->ws->focus_prev != NULL &&
1680 source->ws->focus_prev != target)
1682 source = source->ws->focus_prev;
1686 if (target == NULL || source == NULL)
1688 source->ws->focus_prev = target;
1689 TAILQ_REMOVE(wl, target, entry);
1690 TAILQ_INSERT_BEFORE(source, target, entry);
1691 TAILQ_REMOVE(wl, source, entry);
1692 TAILQ_INSERT_HEAD(wl, source, entry);
1694 case SWM_ARG_ID_MOVELAST:
1695 TAILQ_REMOVE(wl, source, entry);
1696 TAILQ_INSERT_TAIL(wl, source, entry);
1699 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1707 focus_prev(struct ws_win *win)
1709 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
1710 struct ws_win *cur_focus = NULL;
1711 struct ws_win_list *wl = NULL;
1712 struct workspace *ws = NULL;
1714 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
1716 if (!(win && win->ws))
1721 cur_focus = ws->focus;
1722 winlostfocus = cur_focus;
1724 /* pickle, just focus on whatever */
1725 if (cur_focus == NULL) {
1726 /* use prev_focus if valid */
1727 if (ws->focus_prev && ws->focus_prev != cur_focus &&
1728 find_window(WINID(ws->focus_prev)))
1729 winfocus = ws->focus_prev;
1730 if (winfocus == NULL)
1731 winfocus = TAILQ_FIRST(wl);
1735 /* if transient focus on parent */
1736 if (cur_focus->transient) {
1737 winfocus = find_window(cur_focus->transient);
1741 /* if in max_stack try harder */
1742 if (ws->cur_layout->flags & SWM_L_FOCUSPREV) {
1743 if (cur_focus != ws->focus_prev)
1744 winfocus = ws->focus_prev;
1745 else if (cur_focus != ws->focus)
1746 winfocus = ws->focus;
1748 winfocus = TAILQ_PREV(win, ws_win_list, entry);
1753 if (cur_focus == win)
1754 winfocus = TAILQ_PREV(win, ws_win_list, entry);
1755 if (winfocus == NULL)
1756 winfocus = TAILQ_LAST(wl, ws_win_list);
1757 if (winfocus == NULL || winfocus == win)
1758 winfocus = TAILQ_NEXT(cur_focus, entry);
1760 if (winfocus == winlostfocus || winfocus == NULL)
1763 focus_magic(winfocus, SWM_F_GENERIC);
1767 focus(struct swm_region *r, union arg *args)
1769 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
1770 struct ws_win *cur_focus = NULL;
1771 struct ws_win_list *wl = NULL;
1772 struct workspace *ws = NULL;
1777 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1779 /* treat FOCUS_CUR special */
1780 if (args->id == SWM_ARG_ID_FOCUSCUR) {
1782 winfocus = r->ws->focus;
1783 else if (r->ws->focus_prev)
1784 winfocus = r->ws->focus_prev;
1786 winfocus = TAILQ_FIRST(&r->ws->winlist);
1788 focus_magic(winfocus, SWM_F_GENERIC);
1792 if ((cur_focus = r->ws->focus) == NULL)
1797 winlostfocus = cur_focus;
1800 case SWM_ARG_ID_FOCUSPREV:
1801 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1802 if (winfocus == NULL)
1803 winfocus = TAILQ_LAST(wl, ws_win_list);
1806 case SWM_ARG_ID_FOCUSNEXT:
1807 winfocus = TAILQ_NEXT(cur_focus, entry);
1808 if (winfocus == NULL)
1809 winfocus = TAILQ_FIRST(wl);
1812 case SWM_ARG_ID_FOCUSMAIN:
1813 winfocus = TAILQ_FIRST(wl);
1814 if (winfocus == cur_focus)
1815 winfocus = cur_focus->ws->focus_prev;
1822 if (winfocus == winlostfocus || winfocus == NULL)
1825 focus_magic(winfocus, SWM_F_GENERIC);
1829 cycle_layout(struct swm_region *r, union arg *args)
1831 struct workspace *ws = r->ws;
1832 struct ws_win *winfocus;
1835 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1837 winfocus = ws->focus;
1840 if (ws->cur_layout->l_stack == NULL)
1841 ws->cur_layout = &layouts[0];
1844 a.id = SWM_ARG_ID_FOCUSCUR;
1850 stack_config(struct swm_region *r, union arg *args)
1852 struct workspace *ws = r->ws;
1854 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1857 if (ws->cur_layout->l_config != NULL)
1858 ws->cur_layout->l_config(ws, args->id);
1860 if (args->id != SWM_ARG_ID_STACKINIT);
1866 struct swm_geometry g;
1867 struct swm_region *r;
1870 DNPRINTF(SWM_D_STACK, "stack\n");
1872 for (i = 0; i < ScreenCount(display); i++) {
1874 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1875 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1876 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1878 /* start with screen geometry, adjust for bar */
1886 r->ws->cur_layout->l_stack(r->ws, &g);
1887 /* save r so we can track region changes */
1896 store_float_geom(struct ws_win *win, struct swm_region *r)
1898 /* retain window geom and region geom */
1899 win->g_float.x = win->g.x;
1900 win->g_float.y = win->g.y;
1901 win->g_float.w = win->g.w;
1902 win->g_float.h = win->g.h;
1903 win->rg_float.x = r->g.x;
1904 win->rg_float.y = r->g.y;
1905 win->rg_float.w = r->g.w;
1906 win->rg_float.h = r->g.h;
1907 win->g_floatvalid = 1;
1911 stack_floater(struct ws_win *win, struct swm_region *r)
1919 bzero(&wc, sizeof wc);
1920 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1923 * to allow windows to change their size (e.g. mplayer fs) only retrieve
1924 * geom on ws switches or return from max mode
1927 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid) ) {
1929 * use stored g and rg to set relative position and size
1930 * as in old region or before max stack mode
1932 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
1933 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
1934 win->g.w = win->g_float.w;
1935 win->g.h = win->g_float.h;
1936 win->g_floatvalid = 0;
1939 win->floatmaxed = 0;
1941 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
1942 (win->g.h >= HEIGHT(r)))
1943 wc.border_width = 0;
1945 wc.border_width = 1;
1946 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
1947 win->g.w = (double)WIDTH(r) * dialog_ratio;
1948 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1953 * floaters and transients are auto-centred unless moved
1956 win->g.x = r->g.x + (WIDTH(r) - win->g.w) / 2;
1957 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) / 2;
1960 /* win can be outside r if new r smaller than old r */
1961 /* Ensure top left corner inside r (move probs otherwise) */
1962 if (win->g.x < r->g.x )
1964 if (win->g.x > r->g.x + r->g.w - 1)
1965 win->g.x = (win->g.w > r->g.w) ? r->g.x :
1966 (r->g.x + r->g.w - win->g.w - 2);
1967 if (win->g.y < r->g.y )
1969 if (win->g.y > r->g.y + r->g.h - 1)
1970 win->g.y = (win->g.h > r->g.h) ? r->g.y :
1971 (r->g.y + r->g.h - win->g.h - 2);
1975 wc.width = win->g.w;
1976 wc.height = win->g.h;
1979 * Retain floater and transient geometry for correct positioning
1980 * when ws changes region
1982 store_float_geom(win, r);
1984 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
1985 win->id, wc.x, wc.y, wc.width, wc.height);
1987 XConfigureWindow(display, win->id, mask, &wc);
1992 * Send keystrokes to terminal to decrease/increase the font size as the
1993 * window size changes.
1996 adjust_font(struct ws_win *win)
1998 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
1999 win->floating || win->transient)
2002 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2003 win->g.w / win->sh.width_inc < term_width &&
2004 win->font_steps < SWM_MAX_FONT_STEPS) {
2005 win->font_size_boundary[win->font_steps] =
2006 (win->sh.width_inc * term_width) + win->sh.base_width;
2009 win->last_inc = win->sh.width_inc;
2010 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2011 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2012 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
2015 win->last_inc = win->sh.width_inc;
2016 fake_keypress(win, XK_KP_Add, ShiftMask);
2020 #define SWAPXY(g) do { \
2022 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2023 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2026 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2029 XWindowAttributes wa;
2030 struct swm_geometry win_g, r_g = *g;
2032 int i, j, s, stacks;
2033 int w_inc = 1, h_inc, w_base = 1, h_base;
2034 int hrh, extra = 0, h_slice, last_h = 0;
2035 int split, colno, winno, mwin, msize, mscale;
2036 int remain, missing, v_slice, reconfigure;
2039 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2040 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2042 winno = count_win(ws, 0);
2043 if (winno == 0 && count_win(ws, 1) == 0)
2046 TAILQ_FOREACH(win, &ws->winlist, entry)
2047 if (win->transient == 0 && win->floating == 0)
2054 w_inc = win->sh.width_inc;
2055 w_base = win->sh.base_width;
2056 mwin = ws->l_state.horizontal_mwin;
2057 mscale = ws->l_state.horizontal_msize;
2058 stacks = ws->l_state.horizontal_stacks;
2061 w_inc = win->sh.height_inc;
2062 w_base = win->sh.base_height;
2063 mwin = ws->l_state.vertical_mwin;
2064 mscale = ws->l_state.vertical_msize;
2065 stacks = ws->l_state.vertical_stacks;
2069 if (stacks > winno - mwin)
2070 stacks = winno - mwin;
2074 h_slice = r_g.h / SWM_H_SLICE;
2075 if (mwin && winno > mwin) {
2076 v_slice = r_g.w / SWM_V_SLICE;
2080 win_g.w = v_slice * mscale;
2082 if (w_inc > 1 && w_inc < v_slice) {
2083 /* adjust for window's requested size increment */
2084 remain = (win_g.w - w_base) % w_inc;
2085 missing = w_inc - remain;
2092 win_g.x += r_g.w - msize;
2095 colno = split = winno / stacks;
2096 win_g.w = ((r_g.w - (stacks * 2) + 2) / stacks);
2098 hrh = r_g.h / colno;
2099 extra = r_g.h - (colno * hrh);
2102 /* stack all the tiled windows */
2103 i = j = 0, s = stacks;
2104 TAILQ_FOREACH(win, &ws->winlist, entry) {
2105 if (win->transient != 0 || win->floating != 0)
2108 if (split && i == split) {
2109 colno = (winno - mwin) / stacks;
2110 if (s <= (winno - mwin) % stacks)
2112 split = split + colno;
2113 hrh = (r_g.h / colno);
2114 extra = r_g.h - (colno * hrh);
2118 win_g.x += win_g.w + 2;
2119 win_g.w = (r_g.w - msize - (stacks * 2)) / stacks;
2121 win_g.w += (r_g.w - msize - (stacks * 2)) %
2128 h_inc = win->sh.width_inc;
2129 h_base = win->sh.base_width;
2131 h_inc = win->sh.height_inc;
2132 h_base = win->sh.base_height;
2134 if (j == colno - 1) {
2135 win_g.h = hrh + extra;
2136 } else if (h_inc > 1 && h_inc < h_slice) {
2137 /* adjust for window's requested size increment */
2138 remain = (win_g.h - h_base) % h_inc;
2139 missing = h_inc - remain;
2141 if (missing <= extra || j == 0) {
2153 win_g.y += last_h + 2;
2155 bzero(&wc, sizeof wc);
2156 if (disable_border && bar_enabled == 0 && winno == 1){
2157 wc.border_width = 0;
2161 wc.border_width = 1;
2164 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2165 win->g.w != win_g.h || win->g.h != win_g.w) {
2167 win->g.x = wc.x = win_g.y;
2168 win->g.y = wc.y = win_g.x;
2169 win->g.w = wc.width = win_g.h;
2170 win->g.h = wc.height = win_g.w;
2173 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2174 win->g.w != win_g.w || win->g.h != win_g.h) {
2176 win->g.x = wc.x = win_g.x;
2177 win->g.y = wc.y = win_g.y;
2178 win->g.w = wc.width = win_g.w;
2179 win->g.h = wc.height = win_g.h;
2184 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2185 XConfigureWindow(display, win->id, mask, &wc);
2189 if (XGetWindowAttributes(display, win->id, &wa))
2190 if (wa.map_state == IsUnmapped)
2191 XMapRaised(display, win->id);
2199 /* now, stack all the floaters and transients */
2200 TAILQ_FOREACH(win, &ws->winlist, entry) {
2201 if (win->transient == 0 && win->floating == 0)
2204 stack_floater(win, ws->r);
2205 XMapRaised(display, win->id);
2210 vertical_config(struct workspace *ws, int id)
2212 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2215 case SWM_ARG_ID_STACKRESET:
2216 case SWM_ARG_ID_STACKINIT:
2217 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2218 ws->l_state.vertical_mwin = 1;
2219 ws->l_state.vertical_stacks = 1;
2221 case SWM_ARG_ID_MASTERSHRINK:
2222 if (ws->l_state.vertical_msize > 1)
2223 ws->l_state.vertical_msize--;
2225 case SWM_ARG_ID_MASTERGROW:
2226 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2227 ws->l_state.vertical_msize++;
2229 case SWM_ARG_ID_MASTERADD:
2230 ws->l_state.vertical_mwin++;
2232 case SWM_ARG_ID_MASTERDEL:
2233 if (ws->l_state.vertical_mwin > 0)
2234 ws->l_state.vertical_mwin--;
2236 case SWM_ARG_ID_STACKINC:
2237 ws->l_state.vertical_stacks++;
2239 case SWM_ARG_ID_STACKDEC:
2240 if (ws->l_state.vertical_stacks > 1)
2241 ws->l_state.vertical_stacks--;
2249 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2251 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2253 stack_master(ws, g, 0, 0);
2257 horizontal_config(struct workspace *ws, int id)
2259 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2262 case SWM_ARG_ID_STACKRESET:
2263 case SWM_ARG_ID_STACKINIT:
2264 ws->l_state.horizontal_mwin = 1;
2265 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2266 ws->l_state.horizontal_stacks = 1;
2268 case SWM_ARG_ID_MASTERSHRINK:
2269 if (ws->l_state.horizontal_msize > 1)
2270 ws->l_state.horizontal_msize--;
2272 case SWM_ARG_ID_MASTERGROW:
2273 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2274 ws->l_state.horizontal_msize++;
2276 case SWM_ARG_ID_MASTERADD:
2277 ws->l_state.horizontal_mwin++;
2279 case SWM_ARG_ID_MASTERDEL:
2280 if (ws->l_state.horizontal_mwin > 0)
2281 ws->l_state.horizontal_mwin--;
2283 case SWM_ARG_ID_STACKINC:
2284 ws->l_state.horizontal_stacks++;
2286 case SWM_ARG_ID_STACKDEC:
2287 if (ws->l_state.horizontal_stacks > 1)
2288 ws->l_state.horizontal_stacks--;
2296 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2298 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
2300 stack_master(ws, g, 1, 0);
2303 /* fullscreen view */
2305 max_stack(struct workspace *ws, struct swm_geometry *g)
2308 struct swm_geometry gg = *g;
2309 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2313 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2318 winno = count_win(ws, 0);
2319 if (winno == 0 && count_win(ws, 1) == 0)
2322 TAILQ_FOREACH(win, &ws->winlist, entry) {
2323 if (win->transient) {
2325 parent = find_window(win->transient);
2329 if (win->floating && win->floatmaxed == 0 ) {
2331 * retain geometry for retrieval on exit from
2334 store_float_geom(win, ws->r);
2335 win->floatmaxed = 1;
2338 /* only reconfigure if necessary */
2339 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2341 bzero(&wc, sizeof wc);
2342 win->g.x = wc.x = gg.x;
2343 win->g.y = wc.y = gg.y;
2345 wc.border_width = 1;
2346 win->g.w = wc.width = gg.w;
2347 win->g.h = wc.height = gg.h;
2349 wc.border_width = 0;
2350 win->g.w = wc.width = gg.w + 2;
2351 win->g.h = wc.height = gg.h + 2;
2353 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2354 XConfigureWindow(display, win->id, mask, &wc);
2357 /* unmap only if we don't have multi screen */
2358 if (win != ws->focus)
2359 if (!(ScreenCount(display) > 1 || outputs > 1))
2363 /* put the last transient on top */
2366 XMapRaised(display, parent->id);
2367 stack_floater(wintrans, ws->r);
2368 focus_magic(wintrans, SWM_F_TRANSIENT);
2373 send_to_ws(struct swm_region *r, union arg *args)
2375 int wsid = args->id;
2376 struct ws_win *win = win;
2377 struct workspace *ws, *nws;
2378 Atom ws_idx_atom = 0;
2379 unsigned char ws_idx_str[SWM_PROPLEN];
2388 if (win->ws->idx == wsid)
2391 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2394 nws = &win->s->ws[wsid];
2396 a.id = SWM_ARG_ID_FOCUSPREV;
2399 TAILQ_REMOVE(&ws->winlist, win, entry);
2400 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2403 /* Try to update the window's workspace property */
2404 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2406 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2407 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2409 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2410 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2416 wkill(struct swm_region *r, union arg *args)
2418 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
2420 if(r->ws->focus == NULL)
2423 if (args->id == SWM_ARG_ID_KILLWINDOW)
2424 XKillClient(display, r->ws->focus->id);
2426 if (r->ws->focus->can_delete)
2427 client_msg(r->ws->focus, adelete);
2431 floating_toggle(struct swm_region *r, union arg *args)
2433 struct ws_win *win = r->ws->focus;
2439 /* reject floating toggles in max stack mode */
2440 if (r->ws->cur_layout == &layouts[SWM_MAX_STACK])
2443 if (win->floating) {
2444 if (!win->floatmaxed) {
2445 /* retain position for refloat */
2446 store_float_geom(win, r);
2450 if (win->g_floatvalid) {
2451 /* refloat at last floating relative position */
2452 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2453 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2454 win->g.w = win->g_float.w;
2455 win->g.h = win->g_float.h;
2461 a.id = SWM_ARG_ID_FOCUSCUR;
2462 focus(win->ws->r, &a);
2466 resize_window(struct ws_win *win, int center)
2470 struct swm_region *r;
2472 r = root_to_region(win->wa.root);
2473 bzero(&wc, sizeof wc);
2474 mask = CWBorderWidth | CWWidth | CWHeight;
2475 wc.border_width = 1;
2476 wc.width = win->g.w;
2477 wc.height = win->g.h;
2478 if (center == SWM_ARG_ID_CENTER) {
2479 wc.x = (WIDTH(r) - win->g.w) / 2;
2480 wc.y = (HEIGHT(r) - win->g.h) / 2;
2484 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
2485 win->id, wc.x, wc.y, wc.width, wc.height);
2487 XConfigureWindow(display, win->id, mask, &wc);
2492 resize(struct ws_win *win, union arg *args)
2496 struct swm_region *r = win->ws->r;
2501 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
2502 win->id, win->floating, win->transient);
2504 if (!(win->transient != 0 || win->floating != 0))
2507 /* reject resizes in max mode for floaters (transient ok) */
2508 if (win->floatmaxed)
2512 /* raise the window = move to last in window list */
2513 a.id = SWM_ARG_ID_MOVELAST;
2517 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2518 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2521 /* place pointer at bottom left corner or nearest point inside r */
2522 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
2525 relx = r->g.x + r->g.w - win->g.x - 2;
2527 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
2530 rely = r->g.y + r->g.h - win->g.y - 2;
2532 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
2534 XMaskEvent(display, MOUSEMASK | ExposureMask |
2535 SubstructureRedirectMask, &ev);
2537 case ConfigureRequest:
2540 handler[ev.type](&ev);
2543 /* do not allow resize outside of region */
2544 if ( ev.xmotion.x_root < r->g.x ||
2545 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
2546 ev.xmotion.y_root < r->g.y ||
2547 ev.xmotion.y_root > r->g.y + r->g.h - 1)
2550 if (ev.xmotion.x <= 1)
2552 if (ev.xmotion.y <= 1)
2554 win->g.w = ev.xmotion.x;
2555 win->g.h = ev.xmotion.y;
2557 /* not free, don't sync more than 60 times / second */
2558 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2559 time = ev.xmotion.time;
2560 XSync(display, False);
2561 resize_window(win, args->id);
2565 } while (ev.type != ButtonRelease);
2567 XSync(display, False);
2568 resize_window(win, args->id);
2570 store_float_geom(win,r);
2572 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
2574 XUngrabPointer(display, CurrentTime);
2577 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2581 move_window(struct ws_win *win)
2585 struct swm_region *r;
2587 r = root_to_region(win->wa.root);
2588 bzero(&wc, sizeof wc);
2593 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
2594 win->id, wc.x, wc.y, wc.width, wc.height);
2596 XConfigureWindow(display, win->id, mask, &wc);
2601 move(struct ws_win *win, union arg *args)
2605 struct swm_region *r = win->ws->r;
2608 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
2609 win->id, win->floating, win->transient);
2611 /* in max_stack mode should only move transients */
2612 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
2616 if (win->floating == 0 && !win->transient) {
2620 /* raise the window = move to last in window list */
2621 a.id = SWM_ARG_ID_MOVELAST;
2625 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2626 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2628 XWarpPointer(display, None, win->id, 0, 0, 0, 0, -1, -1);
2630 XMaskEvent(display, MOUSEMASK | ExposureMask |
2631 SubstructureRedirectMask, &ev);
2633 case ConfigureRequest:
2636 handler[ev.type](&ev);
2639 /* don't allow to move window origin out of region */
2640 if ( ev.xmotion.x_root < r->g.x ||
2641 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
2642 ev.xmotion.y_root < r->g.y ||
2643 ev.xmotion.y_root > r->g.y + r->g.h - 1)
2646 win->g.x = ev.xmotion.x_root;
2647 win->g.y = ev.xmotion.y_root;
2649 /* not free, don't sync more than 60 times / second */
2650 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2651 time = ev.xmotion.time;
2652 XSync(display, False);
2657 } while (ev.type != ButtonRelease);
2659 XSync(display, False);
2662 store_float_geom(win,r);
2663 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2664 XUngrabPointer(display, CurrentTime);
2667 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2670 /* user/key callable function IDs */
2729 /* key definitions */
2731 dummykeyfunc(struct swm_region *r, union arg *args)
2736 legacyfunc(struct swm_region *r, union arg *args)
2741 char name[SWM_FUNCNAME_LEN];
2742 void (*func)(struct swm_region *r, union arg *);
2744 } keyfuncs[kf_invalid + 1] = {
2745 /* name function argument */
2746 { "cycle_layout", cycle_layout, {0} },
2747 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
2748 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
2749 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
2750 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
2751 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
2752 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
2753 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
2754 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
2755 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
2756 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
2757 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
2758 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
2759 { "spawn_term", spawnterm, {.argv = spawn_term} },
2760 { "spawn_menu", legacyfunc, {0} },
2761 { "quit", quit, {0} },
2762 { "restart", restart, {0} },
2763 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
2764 { "ws_1", switchws, {.id = 0} },
2765 { "ws_2", switchws, {.id = 1} },
2766 { "ws_3", switchws, {.id = 2} },
2767 { "ws_4", switchws, {.id = 3} },
2768 { "ws_5", switchws, {.id = 4} },
2769 { "ws_6", switchws, {.id = 5} },
2770 { "ws_7", switchws, {.id = 6} },
2771 { "ws_8", switchws, {.id = 7} },
2772 { "ws_9", switchws, {.id = 8} },
2773 { "ws_10", switchws, {.id = 9} },
2774 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
2775 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
2776 { "ws_prior", priorws, {0} },
2777 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
2778 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
2779 { "mvws_1", send_to_ws, {.id = 0} },
2780 { "mvws_2", send_to_ws, {.id = 1} },
2781 { "mvws_3", send_to_ws, {.id = 2} },
2782 { "mvws_4", send_to_ws, {.id = 3} },
2783 { "mvws_5", send_to_ws, {.id = 4} },
2784 { "mvws_6", send_to_ws, {.id = 5} },
2785 { "mvws_7", send_to_ws, {.id = 6} },
2786 { "mvws_8", send_to_ws, {.id = 7} },
2787 { "mvws_9", send_to_ws, {.id = 8} },
2788 { "mvws_10", send_to_ws, {.id = 9} },
2789 { "bar_toggle", bar_toggle, {0} },
2790 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
2791 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
2792 { "screenshot_all", legacyfunc, {0} },
2793 { "screenshot_wind", legacyfunc, {0} },
2794 { "float_toggle", floating_toggle,{0} },
2795 { "version", version, {0} },
2796 { "spawn_lock", legacyfunc, {0} },
2797 { "spawn_initscr", legacyfunc, {0} },
2798 { "spawn_custom", dummykeyfunc, {0} },
2799 { "dumpwins", dumpwins, {0} },
2800 { "invalid key func", NULL, {0} },
2805 enum keyfuncid funcid;
2808 int keys_size = 0, keys_length = 0;
2809 struct key *keys = NULL;
2812 enum { client_click, root_click };
2814 unsigned int action;
2816 unsigned int button;
2817 void (*func)(struct ws_win *, union arg *);
2820 /* action key mouse button func args */
2821 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
2822 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
2823 { client_click, MODKEY, Button1, move, {0} },
2827 update_modkey(unsigned int mod)
2832 for (i = 0; i < keys_length; i++)
2833 if (keys[i].mod & ShiftMask)
2834 keys[i].mod = mod | ShiftMask;
2838 for (i = 0; i < LENGTH(buttons); i++)
2839 if (buttons[i].mask & ShiftMask)
2840 buttons[i].mask = mod | ShiftMask;
2842 buttons[i].mask = mod;
2852 int spawns_size = 0, spawns_length = 0;
2853 struct spawn_prog *spawns = NULL;
2856 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
2859 struct spawn_prog *prog = NULL;
2861 char *ap, **real_args;
2863 DNPRINTF(SWM_D_SPAWN, "spawn_custom %s\n", spawn_name);
2866 for (i = 0; i < spawns_length; i++) {
2867 if (!strcasecmp(spawn_name, spawns[i].name))
2871 fprintf(stderr, "spawn_custom: program %s not found\n",
2876 /* make room for expanded args */
2877 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
2878 err(1, "spawn_custom: calloc real_args");
2880 /* expand spawn_args into real_args */
2881 for (i = 0; i < prog->argc; i++) {
2883 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
2884 if (!strcasecmp(ap, "$bar_border")) {
2886 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
2888 err(1, "spawn_custom border color");
2889 } else if (!strcasecmp(ap, "$bar_color")) {
2891 strdup(r->s->c[SWM_S_COLOR_BAR].name))
2893 err(1, "spawn_custom bar color");
2894 } else if (!strcasecmp(ap, "$bar_font")) {
2895 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
2897 err(1, "spawn_custom bar fonts");
2898 } else if (!strcasecmp(ap, "$bar_font_color")) {
2900 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
2902 err(1, "spawn_custom color font");
2903 } else if (!strcasecmp(ap, "$color_focus")) {
2905 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
2907 err(1, "spawn_custom color focus");
2908 } else if (!strcasecmp(ap, "$color_unfocus")) {
2910 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
2912 err(1, "spawn_custom color unfocus");
2914 /* no match --> copy as is */
2915 if ((real_args[i] = strdup(ap)) == NULL)
2916 err(1, "spawn_custom strdup(ap)");
2918 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
2923 if ((swm_debug & SWM_D_SPAWN) != 0) {
2924 fprintf(stderr, "spawn_custom: result = ");
2925 for (i = 0; i < prog->argc; i++)
2926 fprintf(stderr, "\"%s\" ", real_args[i]);
2927 fprintf(stderr, "\n");
2933 for (i = 0; i < prog->argc; i++)
2939 setspawn(struct spawn_prog *prog)
2943 if (prog == NULL || prog->name == NULL)
2947 for (i = 0; i < spawns_length; i++) {
2948 if (!strcmp(spawns[i].name, prog->name)) {
2950 if (prog->argv == NULL) {
2952 DNPRINTF(SWM_D_SPAWN,
2953 "setspawn: delete #%d %s\n",
2955 free(spawns[i].name);
2956 for (j = 0; j < spawns[i].argc; j++)
2957 free(spawns[i].argv[j]);
2958 free(spawns[i].argv);
2959 j = spawns_length - 1;
2961 spawns[i] = spawns[j];
2966 DNPRINTF(SWM_D_SPAWN,
2967 "setspawn: replace #%d %s\n",
2969 free(spawns[i].name);
2970 for (j = 0; j < spawns[i].argc; j++)
2971 free(spawns[i].argv[j]);
2972 free(spawns[i].argv);
2975 /* found case handled */
2981 if (prog->argv == NULL) {
2983 "error: setspawn: cannot find program %s", prog->name);
2988 /* not found: add */
2989 if (spawns_size == 0 || spawns == NULL) {
2991 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
2992 spawns = malloc((size_t)spawns_size *
2993 sizeof(struct spawn_prog));
2994 if (spawns == NULL) {
2995 fprintf(stderr, "setspawn: malloc failed\n");
2999 } else if (spawns_length == spawns_size) {
3001 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
3002 spawns = realloc(spawns, (size_t)spawns_size *
3003 sizeof(struct spawn_prog));
3004 if (spawns == NULL) {
3005 fprintf(stderr, "setspawn: realloc failed\n");
3011 if (spawns_length < spawns_size) {
3012 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
3013 spawns_length, prog->name);
3014 i = spawns_length++;
3017 fprintf(stderr, "spawns array problem?\n");
3018 if (spawns == NULL) {
3019 fprintf(stderr, "spawns array is NULL!\n");
3027 setconfspawn(char *selector, char *value, int flags)
3029 struct spawn_prog *prog;
3030 char *vp, *cp, *word;
3032 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
3033 if ((prog = calloc(1, sizeof *prog)) == NULL)
3034 err(1, "setconfspawn: calloc prog");
3035 prog->name = strdup(selector);
3036 if (prog->name == NULL)
3037 err(1, "setconfspawn prog->name");
3038 if ((cp = vp = strdup(value)) == NULL)
3039 err(1, "setconfspawn: strdup(value) ");
3040 while ((word = strsep(&cp, " \t")) != NULL) {
3041 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
3043 cp += (long)strspn(cp, " \t");
3044 if (strlen(word) > 0) {
3046 prog->argv = realloc(prog->argv,
3047 prog->argc * sizeof(char *));
3048 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
3049 err(1, "setconfspawn: strdup");
3056 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
3063 setconfspawn("term", "xterm", 0);
3064 setconfspawn("screenshot_all", "screenshot.sh full", 0);
3065 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
3066 setconfspawn("lock", "xlock", 0);
3067 setconfspawn("initscr", "initscreen.sh", 0);
3068 setconfspawn("menu", "dmenu_run"
3071 " -nf $bar_font_color"
3073 " -sf $bar_color", 0);
3077 #define SWM_MODNAME_SIZE 32
3078 #define SWM_KEY_WS "\n+ \t"
3080 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
3084 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
3085 if (mod == NULL || ks == NULL) {
3086 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
3089 if (keystr == NULL || strlen(keystr) == 0) {
3090 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
3096 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
3097 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
3099 cp += (long)strspn(cp, SWM_KEY_WS);
3100 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
3102 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
3104 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
3106 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
3108 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
3110 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
3112 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
3113 *mod |= ControlMask;
3115 *ks = XStringToKeysym(name);
3116 XConvertCase(*ks, ks, &uks);
3117 if (ks == NoSymbol) {
3119 "parsekeys: invalid key %s\n",
3125 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
3130 strdupsafe(char *str)
3135 return (strdup(str));
3139 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
3142 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
3143 keyfuncs[kfid].name, spawn_name);
3145 for (i = 0; i < keys_length; i++) {
3146 if (keys[i].mod == mod && keys[i].keysym == ks) {
3147 if (kfid == kf_invalid) {
3150 "setkeybinding: delete #%d %s\n",
3151 i, keyfuncs[keys[i].funcid].name);
3152 free(keys[i].spawn_name);
3153 j = keys_length - 1;
3157 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3160 /* found: replace */
3162 "setkeybinding: replace #%d %s %s\n",
3163 i, keyfuncs[keys[i].funcid].name,
3165 free(keys[i].spawn_name);
3167 keys[i].keysym = ks;
3168 keys[i].funcid = kfid;
3169 keys[i].spawn_name = strdupsafe(spawn_name);
3170 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3175 if (kfid == kf_invalid) {
3177 "error: setkeybinding: cannot find mod/key combination");
3178 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3181 /* not found: add */
3182 if (keys_size == 0 || keys == NULL) {
3184 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
3185 keys = malloc((size_t)keys_size * sizeof(struct key));
3187 fprintf(stderr, "malloc failed\n");
3191 } else if (keys_length == keys_size) {
3193 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
3194 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
3196 fprintf(stderr, "realloc failed\n");
3201 if (keys_length < keys_size) {
3203 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
3204 j, keyfuncs[kfid].name, spawn_name);
3206 keys[j].keysym = ks;
3207 keys[j].funcid = kfid;
3208 keys[j].spawn_name = strdupsafe(spawn_name);
3210 fprintf(stderr, "keys array problem?\n");
3212 fprintf(stderr, "keys array problem\n");
3216 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3220 setconfbinding(char *selector, char *value, int flags)
3222 enum keyfuncid kfid;
3226 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
3227 if (selector == NULL) {
3228 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
3229 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3231 setkeybinding(mod, ks, kfid, NULL);
3236 /* search by key function name */
3237 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
3238 if (strncasecmp(selector, keyfuncs[kfid].name,
3239 SWM_FUNCNAME_LEN) == 0) {
3240 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3242 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3243 setkeybinding(mod, ks, kfid, NULL);
3249 /* search by custom spawn name */
3250 for (i = 0; i < spawns_length; i++) {
3251 if (strcasecmp(selector, spawns[i].name) == 0) {
3252 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3254 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3255 setkeybinding(mod, ks, kf_spawn_custom,
3262 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
3269 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
3270 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
3271 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
3272 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
3273 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
3274 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
3275 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
3276 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
3277 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
3278 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
3279 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
3280 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
3281 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
3282 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
3283 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
3284 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
3285 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
3286 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
3287 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
3288 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
3289 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
3290 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
3291 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
3292 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
3293 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
3294 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
3295 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
3296 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
3297 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
3298 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
3299 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
3300 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
3301 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
3302 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
3303 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
3304 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
3305 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
3306 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
3307 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
3308 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
3309 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
3310 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
3311 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
3312 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
3313 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
3314 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
3315 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
3316 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
3317 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
3318 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
3319 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
3320 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
3321 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
3322 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
3324 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
3329 updatenumlockmask(void)
3332 XModifierKeymap *modmap;
3334 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
3336 modmap = XGetModifierMapping(display);
3337 for (i = 0; i < 8; i++)
3338 for (j = 0; j < modmap->max_keypermod; j++)
3339 if (modmap->modifiermap[i * modmap->max_keypermod + j]
3340 == XKeysymToKeycode(display, XK_Num_Lock))
3341 numlockmask = (1 << i);
3343 XFreeModifiermap(modmap);
3349 unsigned int i, j, k;
3351 unsigned int modifiers[] =
3352 { 0, LockMask, numlockmask, numlockmask | LockMask };
3354 DNPRINTF(SWM_D_MISC, "grabkeys\n");
3355 updatenumlockmask();
3357 for (k = 0; k < ScreenCount(display); k++) {
3358 if (TAILQ_EMPTY(&screens[k].rl))
3360 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
3361 for (i = 0; i < keys_length; i++) {
3362 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
3363 for (j = 0; j < LENGTH(modifiers); j++)
3364 XGrabKey(display, code,
3365 keys[i].mod | modifiers[j],
3366 screens[k].root, True,
3367 GrabModeAsync, GrabModeAsync);
3373 grabbuttons(struct ws_win *win, int focused)
3376 unsigned int modifiers[] =
3377 { 0, LockMask, numlockmask, numlockmask|LockMask };
3379 updatenumlockmask();
3380 XUngrabButton(display, AnyButton, AnyModifier, win->id);
3382 for (i = 0; i < LENGTH(buttons); i++)
3383 if (buttons[i].action == client_click)
3384 for (j = 0; j < LENGTH(modifiers); j++)
3385 XGrabButton(display, buttons[i].button,
3386 buttons[i].mask | modifiers[j],
3387 win->id, False, BUTTONMASK,
3388 GrabModeAsync, GrabModeSync, None,
3391 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
3392 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
3395 const char *quirkname[] = {
3396 "NONE", /* config string for "no value" */
3404 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
3405 #define SWM_Q_WS "\n|+ \t"
3407 parsequirks(char *qstr, unsigned long *quirk)
3417 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
3419 cp += (long)strspn(cp, SWM_Q_WS);
3420 for (i = 0; i < LENGTH(quirkname); i++) {
3421 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
3422 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
3427 *quirk |= 1 << (i-1);
3431 if (i >= LENGTH(quirkname)) {
3432 DNPRINTF(SWM_D_QUIRK,
3433 "parsequirks: invalid quirk [%s]\n", name);
3441 setquirk(const char *class, const char *name, const int quirk)
3446 for (i = 0; i < quirks_length; i++) {
3447 if (!strcmp(quirks[i].class, class) &&
3448 !strcmp(quirks[i].name, name)) {
3451 DNPRINTF(SWM_D_QUIRK,
3452 "setquirk: delete #%d %s:%s\n",
3453 i, quirks[i].class, quirks[i].name);
3454 free(quirks[i].class);
3455 free(quirks[i].name);
3456 j = quirks_length - 1;
3458 quirks[i] = quirks[j];
3462 /* found: replace */
3463 DNPRINTF(SWM_D_QUIRK,
3464 "setquirk: replace #%d %s:%s\n",
3465 i, quirks[i].class, quirks[i].name);
3466 free(quirks[i].class);
3467 free(quirks[i].name);
3468 quirks[i].class = strdup(class);
3469 quirks[i].name = strdup(name);
3470 quirks[i].quirk = quirk;
3477 "error: setquirk: cannot find class/name combination");
3480 /* not found: add */
3481 if (quirks_size == 0 || quirks == NULL) {
3483 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
3484 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
3486 fprintf(stderr, "setquirk: malloc failed\n");
3490 } else if (quirks_length == quirks_size) {
3492 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
3493 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
3495 fprintf(stderr, "setquirk: realloc failed\n");
3500 if (quirks_length < quirks_size) {
3501 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
3502 j = quirks_length++;
3503 quirks[j].class = strdup(class);
3504 quirks[j].name = strdup(name);
3505 quirks[j].quirk = quirk;
3507 fprintf(stderr, "quirks array problem?\n");
3509 fprintf(stderr, "quirks array problem!\n");
3516 setconfquirk(char *selector, char *value, int flags)
3518 char *cp, *class, *name;
3520 unsigned long quirks;
3521 if (selector == NULL)
3523 if ((cp = strchr(selector, ':')) == NULL)
3528 if ((retval = parsequirks(value, &quirks)) == 0)
3529 setquirk(class, name, quirks);
3536 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
3537 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
3538 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
3539 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
3540 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3541 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
3542 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3543 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3544 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3545 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3546 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
3547 setquirk("pcb", "pcb", SWM_Q_FLOAT);
3550 /* conf file stuff */
3551 #define SWM_CONF_FILE "scrotwm.conf"
3553 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_STACK_ENABLED,
3554 SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT, SWM_S_CYCLE_EMPTY,
3555 SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH,
3556 SWM_S_TITLE_CLASS_ENABLED, SWM_S_TITLE_NAME_ENABLED,
3557 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BAR_FONT,
3558 SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM, SWM_S_SS_APP, SWM_S_DIALOG_RATIO
3562 setconfvalue(char *selector, char *value, int flags)
3565 case SWM_S_BAR_DELAY:
3566 bar_delay = atoi(value);
3568 case SWM_S_BAR_ENABLED:
3569 bar_enabled = atoi(value);
3571 case SWM_S_STACK_ENABLED:
3572 stack_enabled = atoi(value);
3574 case SWM_S_CLOCK_ENABLED:
3575 clock_enabled = atoi(value);
3577 case SWM_S_CLOCK_FORMAT:
3578 #ifndef SWM_DENY_CLOCK_FORMAT
3580 if ((clock_format = strdup(value)) == NULL)
3581 err(1, "setconfvalue: clock_format");
3584 case SWM_S_CYCLE_EMPTY:
3585 cycle_empty = atoi(value);
3587 case SWM_S_CYCLE_VISIBLE:
3588 cycle_visible = atoi(value);
3590 case SWM_S_SS_ENABLED:
3591 ss_enabled = atoi(value);
3593 case SWM_S_TERM_WIDTH:
3594 term_width = atoi(value);
3596 case SWM_S_TITLE_CLASS_ENABLED:
3597 title_class_enabled = atoi(value);
3599 case SWM_S_TITLE_NAME_ENABLED:
3600 title_name_enabled = atoi(value);
3602 case SWM_S_FOCUS_MODE:
3603 if (!strcmp(value, "default"))
3604 focus_mode = SWM_FOCUS_DEFAULT;
3605 else if (!strcmp(value, "follow_cursor"))
3606 focus_mode = SWM_FOCUS_FOLLOW;
3607 else if (!strcmp(value, "synergy"))
3608 focus_mode = SWM_FOCUS_SYNERGY;
3610 err(1, "focus_mode");
3612 case SWM_S_DISABLE_BORDER:
3613 disable_border = atoi(value);
3615 case SWM_S_BAR_FONT:
3617 if ((bar_fonts[0] = strdup(value)) == NULL)
3618 err(1, "setconfvalue: bar_font");
3620 case SWM_S_BAR_ACTION:
3622 if ((bar_argv[0] = strdup(value)) == NULL)
3623 err(1, "setconfvalue: bar_action");
3625 case SWM_S_SPAWN_TERM:
3626 free(spawn_term[0]);
3627 if ((spawn_term[0] = strdup(value)) == NULL)
3628 err(1, "setconfvalue: spawn_term");
3632 case SWM_S_DIALOG_RATIO:
3633 dialog_ratio = atof(value);
3634 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
3644 setconfmodkey(char *selector, char *value, int flags)
3646 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
3647 update_modkey(Mod1Mask);
3648 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
3649 update_modkey(Mod2Mask);
3650 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
3651 update_modkey(Mod3Mask);
3652 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
3653 update_modkey(Mod4Mask);
3660 setconfcolor(char *selector, char *value, int flags)
3662 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
3667 setconfregion(char *selector, char *value, int flags)
3669 custom_region(value);
3673 /* config options */
3674 struct config_option {
3676 int (*func)(char*, char*, int);
3679 struct config_option configopt[] = {
3680 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
3681 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
3682 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
3683 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
3684 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
3685 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
3686 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
3687 { "bind", setconfbinding, 0 },
3688 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
3689 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
3690 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
3691 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
3692 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
3693 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
3694 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
3695 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
3696 { "modkey", setconfmodkey, 0 },
3697 { "program", setconfspawn, 0 },
3698 { "quirk", setconfquirk, 0 },
3699 { "region", setconfregion, 0 },
3700 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
3701 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
3702 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
3703 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
3704 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
3705 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
3706 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
3707 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
3712 conf_load(char *filename)
3715 char *line, *cp, *optsub, *optval;
3716 size_t linelen, lineno = 0;
3717 int wordlen, i, optind;
3718 struct config_option *opt;
3720 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
3722 if (filename == NULL) {
3723 fprintf(stderr, "conf_load: no filename\n");
3726 if ((config = fopen(filename, "r")) == NULL) {
3727 warn("conf_load: fopen");
3731 while (!feof(config)) {
3732 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
3735 err(1, "%s", filename);
3740 cp += strspn(cp, " \t\n"); /* eat whitespace */
3741 if (cp[0] == '\0') {
3746 /* get config option */
3747 wordlen = strcspn(cp, "=[ \t\n");
3749 warnx("%s: line %zd: no option found",
3754 for (i = 0; i < LENGTH(configopt); i++) {
3755 opt = &configopt[i];
3756 if (!strncasecmp(cp, opt->optname, wordlen) &&
3757 strlen(opt->optname) == wordlen) {
3763 warnx("%s: line %zd: unknown option %.*s",
3764 filename, lineno, wordlen, cp);
3768 cp += strspn(cp, " \t\n"); /* eat whitespace */
3769 /* get [selector] if any */
3773 wordlen = strcspn(cp, "]");
3776 warnx("%s: line %zd: syntax error",
3780 asprintf(&optsub, "%.*s", wordlen, cp);
3783 cp += strspn(cp, "] \t\n"); /* eat trailing */
3785 cp += strspn(cp, "= \t\n"); /* eat trailing */
3787 optval = strdup(cp);
3788 /* call function to deal with it all */
3789 if (configopt[optind].func(optsub, optval,
3790 configopt[optind].funcflags) != 0) {
3791 fprintf(stderr, "%s line %zd: %s\n",
3792 filename, lineno, line);
3793 errx(1, "%s: line %zd: invalid data for %s",
3794 filename, lineno, configopt[optind].optname);
3802 DNPRINTF(SWM_D_CONF, "conf_load end\n");
3808 set_child_transient(struct ws_win *win)
3810 struct ws_win *parent;
3812 parent = find_window(win->transient);
3814 parent->child_trans = win;
3818 manage_window(Window id)
3821 struct workspace *ws;
3822 struct ws_win *win, *ww;
3823 int format, i, ws_idx, n, border_me = 0;
3824 unsigned long nitems, bytes;
3825 Atom ws_idx_atom = 0, type;
3826 Atom *prot = NULL, *pp;
3827 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
3828 struct swm_region *r;
3833 if ((win = find_window(id)) != NULL)
3834 return (win); /* already being managed */
3836 /* see if we are on the unmanaged list */
3837 if ((win = find_unmanaged_window(id)) != NULL) {
3838 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
3840 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
3841 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
3843 set_child_transient(win);
3847 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
3848 errx(1, "calloc: failed to allocate memory for new window");
3850 /* Get all the window data in one shot */
3851 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3853 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
3854 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
3855 XGetWindowAttributes(display, id, &win->wa);
3856 XGetWMNormalHints(display, id, &win->sh, &mask);
3857 XGetTransientForHint(display, id, &trans);
3859 win->transient = trans;
3860 set_child_transient(win);
3861 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
3862 win->id, win->transient);
3864 /* get supported protocols */
3865 if (XGetWMProtocols(display, id, &prot, &n)) {
3866 for (i = 0, pp = prot; i < n; i++, pp++) {
3867 if (*pp == takefocus)
3868 win->take_focus = 1;
3870 win->can_delete = 1;
3877 * Figure out where to put the window. If it was previously assigned to
3878 * a workspace (either by spawn() or manually moving), and isn't
3879 * transient, * put it in the same workspace
3881 r = root_to_region(win->wa.root);
3882 if (prop && win->transient == 0) {
3883 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
3884 ws_idx = strtonum(prop, 0, 9, &errstr);
3886 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
3889 ws = &r->s->ws[ws_idx];
3892 /* this should launch transients in the same ws as parent */
3894 if ((ww = find_window(trans)) != NULL)
3901 "fix this bug mcbride\n");
3906 /* set up the window layout */
3909 win->s = r->s; /* this never changes */
3910 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
3912 win->g.w = win->wa.width;
3913 win->g.h = win->wa.height;
3914 win->g.x = win->wa.x;
3915 win->g.y = win->wa.y;
3916 win->g_floatvalid = 0;
3917 win->floatmaxed = 0;
3919 /* Set window properties so we can remember this after reincarnation */
3920 if (ws_idx_atom && prop == NULL &&
3921 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
3922 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3924 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3925 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3929 if (XGetClassHint(display, win->id, &win->ch)) {
3930 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
3931 win->ch.res_class, win->ch.res_name);
3933 /* java is retarded so treat it special */
3934 if (strstr(win->ch.res_name, "sun-awt")) {
3939 for (i = 0; i < quirks_length; i++){
3940 if (!strcmp(win->ch.res_class, quirks[i].class) &&
3941 !strcmp(win->ch.res_name, quirks[i].name)) {
3942 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
3943 win->ch.res_class, win->ch.res_name);
3944 if (quirks[i].quirk & SWM_Q_FLOAT) {
3948 win->quirks = quirks[i].quirk;
3953 /* alter window position if quirky */
3954 if (win->quirks & SWM_Q_ANYWHERE) {
3955 win->manual = 1; /* don't center the quirky windows */
3956 bzero(&wc, sizeof wc);
3958 if (win->g.y < bar_height) {
3959 win->g.y = wc.y = bar_height;
3962 if (win->g.w + win->g.x > WIDTH(r)) {
3963 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
3969 /* Reset font sizes (the bruteforce way; no default keybinding). */
3970 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
3971 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3972 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3973 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3974 fake_keypress(win, XK_KP_Add, ShiftMask);
3979 bzero(&wc, sizeof wc);
3980 wc.border_width = 1;
3981 mask = CWBorderWidth;
3982 XConfigureWindow(display, win->id, mask, &wc);
3986 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
3987 PropertyChangeMask | StructureNotifyMask);
3989 set_win_state(win, NormalState);
3991 /* floaters need to be mapped if they are in the current workspace */
3992 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
3993 XMapRaised(display, win->id);
3999 free_window(struct ws_win *win)
4001 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
4006 /* needed for restart wm */
4007 set_win_state(win, WithdrawnState);
4009 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
4011 if (win->ch.res_class)
4012 XFree(win->ch.res_class);
4013 if (win->ch.res_name)
4014 XFree(win->ch.res_name);
4019 memset(win, 0xff, sizeof *win); /* XXX kill later */
4025 unmanage_window(struct ws_win *win)
4027 struct ws_win *parent;
4032 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
4034 if (win->transient) {
4035 parent = find_window(win->transient);
4037 parent->child_trans = NULL;
4040 /* work around for mplayer going full screen */
4044 TAILQ_REMOVE(&win->ws->winlist, win, entry);
4045 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
4051 focus_magic(struct ws_win *win, int do_trans)
4053 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu %d\n", WINID(win), do_trans);
4058 if (do_trans == SWM_F_TRANSIENT && win->child_trans) {
4059 /* win = parent & has a transient so focus on that */
4061 focus_win(win->child_trans);
4062 if (win->child_trans->take_focus)
4063 client_msg(win, takefocus);
4065 focus_win(win->child_trans);
4066 if (win->child_trans->take_focus)
4067 client_msg(win->child_trans, takefocus);
4072 if (win->take_focus)
4073 client_msg(win, takefocus);
4080 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
4088 XKeyEvent *ev = &e->xkey;
4090 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
4092 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
4093 for (i = 0; i < keys_length; i++)
4094 if (keysym == keys[i].keysym
4095 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
4096 && keyfuncs[keys[i].funcid].func) {
4097 if (keys[i].funcid == kf_spawn_custom)
4099 root_to_region(ev->root),
4100 &(keyfuncs[keys[i].funcid].args),
4104 keyfuncs[keys[i].funcid].func(
4105 root_to_region(ev->root),
4106 &(keyfuncs[keys[i].funcid].args)
4112 buttonpress(XEvent *e)
4116 XButtonPressedEvent *ev = &e->xbutton;
4118 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
4120 action = root_click;
4121 if ((win = find_window(ev->window)) == NULL)
4124 focus_magic(win, SWM_F_TRANSIENT);
4125 action = client_click;
4127 for (i = 0; i < LENGTH(buttons); i++)
4128 if (action == buttons[i].action && buttons[i].func &&
4129 buttons[i].button == ev->button &&
4130 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
4131 buttons[i].func(win, &buttons[i].args);
4135 configurerequest(XEvent *e)
4137 XConfigureRequestEvent *ev = &e->xconfigurerequest;
4142 if ((win = find_window(ev->window)) == NULL)
4143 if ((win = find_unmanaged_window(ev->window)) == NULL)
4147 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
4149 bzero(&wc, sizeof wc);
4152 wc.width = ev->width;
4153 wc.height = ev->height;
4154 wc.border_width = ev->border_width;
4155 wc.sibling = ev->above;
4156 wc.stack_mode = ev->detail;
4157 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
4159 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
4161 if (win->floating) {
4162 if (ev->value_mask & CWX)
4164 if (ev->value_mask & CWY)
4166 if (ev->value_mask & CWWidth)
4167 win->g.w = ev->width;
4168 if (ev->value_mask & CWHeight)
4169 win->g.h = ev->height;
4176 configurenotify(XEvent *e)
4181 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
4182 e->xconfigure.window);
4184 win = find_window(e->xconfigure.window);
4186 XGetWMNormalHints(display, win->id, &win->sh, &mask);
4194 destroynotify(XEvent *e)
4197 XDestroyWindowEvent *ev = &e->xdestroywindow;
4199 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
4201 if ((win = find_window(ev->window)) == NULL) {
4202 if ((win = find_unmanaged_window(ev->window)) == NULL)
4208 /* make sure we focus on something */
4211 unmanage_window(win);
4217 enternotify(XEvent *e)
4219 XCrossingEvent *ev = &e->xcrossing;
4224 Window focus_return;
4225 int revert_to_return;
4227 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
4228 "%lu subwindow %lu same_screen %d focus %d state %d\n",
4229 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
4230 ev->same_screen, ev->focus, ev->state);
4232 switch (focus_mode) {
4233 case SWM_FOCUS_DEFAULT:
4234 if (QLength(display)) {
4235 DNPRINTF(SWM_D_EVENT, "ignore enternotify %d\n",
4240 case SWM_FOCUS_FOLLOW:
4242 case SWM_FOCUS_SYNERGY:
4245 * all these checks need to be in this order because the
4246 * XCheckTypedWindowEvent relies on weeding out the previous events
4248 * making this code an option would enable a follow mouse for focus
4253 * state is set when we are switching workspaces and focus is set when
4254 * the window or a subwindow already has focus (occurs during restart).
4256 * Only honor the focus flag if last_focus_event is not FocusOut,
4257 * this allows scrotwm to continue to control focus when another
4258 * program is also playing with it.
4260 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
4261 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
4266 * happens when a window is created or destroyed and the border
4267 * crosses the mouse pointer and when switching ws
4269 * we need the subwindow test to see if we came from root in order
4270 * to give focus to floaters
4272 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
4273 ev->subwindow == 0) {
4274 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
4278 /* this window already has focus */
4279 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
4280 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
4284 /* this window is being deleted or moved to another ws */
4285 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
4287 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
4288 XPutBackEvent(display, &cne);
4292 if ((win = find_window(ev->window)) == NULL) {
4293 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
4298 * In fullstack kill all enters unless they come from a different ws
4299 * (i.e. another region) or focus has been grabbed externally.
4301 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
4302 last_focus_event != FocusOut) {
4303 XGetInputFocus(display, &focus_return, &revert_to_return);
4304 if ((w = find_window(focus_return)) == NULL ||
4306 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
4314 if ((win = find_window(ev->window)) == NULL) {
4315 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
4320 * if we have more enternotifies let them handle it in due time
4322 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
4323 DNPRINTF(SWM_D_EVENT,
4324 "ignoring enternotify: got more enternotify\n");
4325 XPutBackEvent(display, &cne);
4329 focus_magic(win, SWM_F_TRANSIENT);
4332 /* lets us use one switch statement for arbitrary mode/detail combinations */
4333 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
4336 focusevent(XEvent *e)
4340 u_int32_t mode_detail;
4341 XFocusChangeEvent *ev = &e->xfocus;
4343 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
4344 ev->type == FocusIn ? "entering" : "leaving",
4345 ev->window, ev->mode, ev->detail);
4347 if (last_focus_event == ev->type) {
4348 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
4352 last_focus_event = ev->type;
4353 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
4355 switch (mode_detail) {
4356 /* synergy client focus operations */
4357 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
4358 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
4360 /* synergy server focus operations */
4361 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
4363 /* Entering applications like rdesktop that mangle the pointer */
4364 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
4366 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
4367 XSetWindowBorder(display, win->id,
4368 win->ws->r->s->c[ev->type == FocusIn ?
4369 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
4372 fprintf(stderr, "ignoring focusevent\n");
4373 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
4380 mapnotify(XEvent *e)
4383 XMapEvent *ev = &e->xmap;
4385 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
4387 win = manage_window(ev->window);
4389 set_win_state(win, NormalState);
4393 mappingnotify(XEvent *e)
4395 XMappingEvent *ev = &e->xmapping;
4397 XRefreshKeyboardMapping(ev);
4398 if (ev->request == MappingKeyboard)
4403 maprequest(XEvent *e)
4406 struct swm_region *r;
4407 XWindowAttributes wa;
4408 XMapRequestEvent *ev = &e->xmaprequest;
4410 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
4411 e->xmaprequest.window);
4413 if (!XGetWindowAttributes(display, ev->window, &wa))
4415 if (wa.override_redirect)
4418 win = manage_window(e->xmaprequest.window);
4420 return; /* can't happen */
4424 /* make new win focused */
4425 r = root_to_region(win->wa.root);
4426 if (win->ws == r->ws)
4427 focus_magic(win, SWM_F_GENERIC);
4431 propertynotify(XEvent *e)
4434 XPropertyEvent *ev = &e->xproperty;
4436 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
4439 if (ev->state == PropertyDelete)
4440 return; /* ignore */
4441 win = find_window(ev->window);
4446 case XA_WM_NORMAL_HINTS:
4449 XGetWMNormalHints(display, win->id, &win->sh, &mask);
4450 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
4451 if (win->sh.flags & PMinSize) {
4452 win->g.w = win->sh.min_width;
4453 win->g.h = win->sh.min_height;
4454 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
4456 XMoveResizeWindow(display, win->id,
4457 win->g.x, win->g.y, win->g.w, win->g.h);
4466 unmapnotify(XEvent *e)
4470 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
4472 /* determine if we need to help unmanage this window */
4473 win = find_window(e->xunmap.window);
4477 if (getstate(e->xunmap.window) == NormalState) {
4478 unmanage_window(win);
4484 visibilitynotify(XEvent *e)
4487 struct swm_region *r;
4489 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
4490 e->xvisibility.window);
4491 if (e->xvisibility.state == VisibilityUnobscured)
4492 for (i = 0; i < ScreenCount(display); i++)
4493 TAILQ_FOREACH(r, &screens[i].rl, entry)
4494 if (e->xvisibility.window == r->bar_window)
4499 xerror_start(Display *d, XErrorEvent *ee)
4506 xerror(Display *d, XErrorEvent *ee)
4508 /* fprintf(stderr, "error: %p %p\n", display, ee); */
4516 xerrorxlib = XSetErrorHandler(xerror_start);
4518 /* this causes an error if some other window manager is running */
4519 XSelectInput(display, DefaultRootWindow(display),
4520 SubstructureRedirectMask);
4521 XSync(display, False);
4525 XSetErrorHandler(xerror);
4526 XSync(display, False);
4531 new_region(struct swm_screen *s, int x, int y, int w, int h)
4533 struct swm_region *r, *n;
4534 struct workspace *ws = NULL;
4537 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
4538 s->idx, w, h, x, y);
4540 /* remove any conflicting regions */
4541 n = TAILQ_FIRST(&s->rl);
4544 n = TAILQ_NEXT(r, entry);
4545 if (X(r) < (x + w) &&
4546 (X(r) + WIDTH(r)) > x &&
4548 (Y(r) + HEIGHT(r)) > y) {
4549 if (r->ws->r != NULL)
4550 r->ws->old_r = r->ws->r;
4552 XDestroyWindow(display, r->bar_window);
4553 TAILQ_REMOVE(&s->rl, r, entry);
4554 TAILQ_INSERT_TAIL(&s->orl, r, entry);
4558 /* search old regions for one to reuse */
4560 /* size + location match */
4561 TAILQ_FOREACH(r, &s->orl, entry)
4562 if (X(r) == x && Y(r) == y &&
4563 HEIGHT(r) == h && WIDTH(r) == w)
4567 TAILQ_FOREACH(r, &s->orl, entry)
4568 if (HEIGHT(r) == h && WIDTH(r) == w)
4572 TAILQ_REMOVE(&s->orl, r, entry);
4573 /* try to use old region's workspace */
4574 if (r->ws->r == NULL)
4577 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
4578 errx(1, "calloc: failed to allocate memory for screen");
4580 /* if we don't have a workspace already, find one */
4582 for (i = 0; i < SWM_WS_MAX; i++)
4583 if (s->ws[i].r == NULL) {
4590 errx(1, "no free workspaces\n");
4601 TAILQ_INSERT_TAIL(&s->rl, r, entry);
4607 #ifdef SWM_XRR_HAS_CRTC
4609 XRRScreenResources *sr;
4612 #endif /* SWM_XRR_HAS_CRTC */
4613 struct swm_region *r;
4616 if (i >= ScreenCount(display))
4617 errx(1, "invalid screen");
4619 /* remove any old regions */
4620 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
4621 r->ws->old_r = r->ws->r = NULL;
4622 XDestroyWindow(display, r->bar_window);
4623 TAILQ_REMOVE(&screens[i].rl, r, entry);
4624 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
4628 /* map virtual screens onto physical screens */
4629 #ifdef SWM_XRR_HAS_CRTC
4630 if (xrandr_support) {
4631 sr = XRRGetScreenResources(display, screens[i].root);
4633 new_region(&screens[i], 0, 0,
4634 DisplayWidth(display, i),
4635 DisplayHeight(display, i));
4639 for (c = 0, ci = NULL; c < ncrtc; c++) {
4640 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
4641 if (ci->noutput == 0)
4644 if (ci != NULL && ci->mode == None)
4645 new_region(&screens[i], 0, 0,
4646 DisplayWidth(display, i),
4647 DisplayHeight(display, i));
4649 new_region(&screens[i],
4650 ci->x, ci->y, ci->width, ci->height);
4653 XRRFreeCrtcInfo(ci);
4654 XRRFreeScreenResources(sr);
4656 #endif /* SWM_XRR_HAS_CRTC */
4658 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
4659 DisplayHeight(display, i));
4664 screenchange(XEvent *e) {
4665 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
4666 struct swm_region *r;
4669 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
4671 if (!XRRUpdateConfiguration(e))
4674 /* silly event doesn't include the screen index */
4675 for (i = 0; i < ScreenCount(display); i++)
4676 if (screens[i].root == xe->root)
4678 if (i >= ScreenCount(display))
4679 errx(1, "screenchange: screen not found\n");
4681 /* brute force for now, just re-enumerate the regions */
4684 /* add bars to all regions */
4685 for (i = 0; i < ScreenCount(display); i++)
4686 TAILQ_FOREACH(r, &screens[i].rl, entry)
4694 Window d1, d2, *wins = NULL;
4695 XWindowAttributes wa;
4698 int errorbase, major, minor;
4699 struct workspace *ws;
4703 if ((screens = calloc(ScreenCount(display),
4704 sizeof(struct swm_screen))) == NULL)
4705 errx(1, "calloc: screens");
4707 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4709 /* initial Xrandr setup */
4710 xrandr_support = XRRQueryExtension(display,
4711 &xrandr_eventbase, &errorbase);
4713 if (XRRQueryVersion(display, &major, &minor) && major < 1)
4716 /* map physical screens */
4717 for (i = 0; i < ScreenCount(display); i++) {
4718 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
4720 TAILQ_INIT(&screens[i].rl);
4721 TAILQ_INIT(&screens[i].orl);
4722 screens[i].root = RootWindow(display, i);
4724 /* set default colors */
4725 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
4726 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
4727 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
4728 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
4729 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
4731 /* set default cursor */
4732 XDefineCursor(display, screens[i].root,
4733 XCreateFontCursor(display, XC_left_ptr));
4735 /* init all workspaces */
4736 /* XXX these should be dynamically allocated too */
4737 for (j = 0; j < SWM_WS_MAX; j++) {
4738 ws = &screens[i].ws[j];
4743 TAILQ_INIT(&ws->winlist);
4744 TAILQ_INIT(&ws->unmanagedlist);
4746 for (k = 0; layouts[k].l_stack != NULL; k++)
4747 if (layouts[k].l_config != NULL)
4748 layouts[k].l_config(ws,
4749 SWM_ARG_ID_STACKINIT);
4750 ws->cur_layout = &layouts[0];
4752 /* grab existing windows (before we build the bars)*/
4753 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
4759 XRRSelectInput(display, screens[i].root,
4760 RRScreenChangeNotifyMask);
4762 /* attach windows to a region */
4763 /* normal windows */
4764 for (j = 0; j < no; j++) {
4765 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4766 wa.override_redirect ||
4767 XGetTransientForHint(display, wins[j], &d1))
4770 state = getstate(wins[j]);
4771 manage = state == IconicState;
4772 if (wa.map_state == IsViewable || manage)
4773 manage_window(wins[j]);
4775 /* transient windows */
4776 for (j = 0; j < no; j++) {
4777 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4778 wa.override_redirect)
4781 state = getstate(wins[j]);
4782 manage = state == IconicState;
4783 if (XGetTransientForHint(display, wins[j], &d1) &&
4785 manage_window(wins[j]);
4797 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
4799 err(1, "setup_globals: strdup");
4800 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
4802 err(1, "setup_globals: strdup");
4803 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
4805 err(1, "setup_globals: strdup");
4806 if ((spawn_term[0] = strdup("xterm")) == NULL)
4807 err(1, "setup_globals: strdup");
4808 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
4816 Atom netwmcheck, netwmname, utf8_string;
4819 /* work around sun jdk bugs, code from wmname */
4820 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
4821 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
4822 utf8_string = XInternAtom(display, "UTF8_STRING", False);
4823 for (i = 0; i < ScreenCount(display); i++) {
4824 root = screens[i].root;
4825 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
4826 PropModeReplace, (unsigned char *)&root, 1);
4827 XChangeProperty(display, root, netwmname, utf8_string, 8,
4828 PropModeReplace, "LG3D", strlen("LG3D"));
4833 main(int argc, char *argv[])
4836 struct swm_region *r, *rr;
4837 struct ws_win *winfocus = NULL;
4840 char conf[PATH_MAX], *cfile = NULL;
4845 struct sigaction sact;
4848 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
4849 SWM_VERSION, cvstag);
4850 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
4851 warnx("no locale support");
4853 if (!(display = XOpenDisplay(0)))
4854 errx(1, "can not open display");
4857 errx(1, "other wm running");
4859 /* handle some signals */
4860 bzero(&sact, sizeof(sact));
4861 sigemptyset(&sact.sa_mask);
4863 sact.sa_handler = sighdlr;
4864 sigaction(SIGINT, &sact, NULL);
4865 sigaction(SIGQUIT, &sact, NULL);
4866 sigaction(SIGTERM, &sact, NULL);
4867 sigaction(SIGHUP, &sact, NULL);
4869 sact.sa_handler = sighdlr;
4870 sact.sa_flags = SA_NOCLDSTOP;
4871 sigaction(SIGCHLD, &sact, NULL);
4873 astate = XInternAtom(display, "WM_STATE", False);
4874 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
4875 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
4876 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
4878 /* look for local and global conf file */
4879 pwd = getpwuid(getuid());
4881 errx(1, "invalid user %d", getuid());
4889 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
4890 if (stat(conf, &sb) != -1) {
4891 if (S_ISREG(sb.st_mode))
4894 /* try global conf file */
4895 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
4896 if (!stat(conf, &sb))
4897 if (S_ISREG(sb.st_mode))
4903 /* setup all bars */
4904 for (i = 0; i < ScreenCount(display); i++)
4905 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4906 if (winfocus == NULL)
4907 winfocus = TAILQ_FIRST(&r->ws->winlist);
4911 /* set some values to work around bad programs */
4919 xfd = ConnectionNumber(display);
4921 while (XPending(display)) {
4922 XNextEvent(display, &e);
4925 if (e.type < LASTEvent) {
4927 if (handler[e.type])
4928 handler[e.type](&e);
4930 DNPRINTF(SWM_D_EVENT,
4931 "win: %lu unknown event: %d\n",
4932 e.xany.window, e.type);
4934 switch (e.type - xrandr_eventbase) {
4935 case RRScreenChangeNotify:
4939 DNPRINTF(SWM_D_EVENT,
4940 "win: %lu unknown xrandr event: "
4941 "%d\n", e.xany.window, e.type);
4947 /* if we are being restarted go focus on first window */
4949 rr = winfocus->ws->r;
4951 /* not a visible window */
4955 /* move pointer to first screen if multi screen */
4956 if (ScreenCount(display) > 1 || outputs > 1)
4957 XWarpPointer(display, None, rr->s[0].root,
4958 0, 0, 0, 0, rr->g.x,
4959 rr->g.y + (bar_enabled ? bar_height : 0));
4961 a.id = SWM_ARG_ID_FOCUSCUR;
4971 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
4973 DNPRINTF(SWM_D_MISC, "select failed");
4974 if (restart_wm == 1)
4975 restart(NULL, NULL);
4985 XFreeGC(display, bar_gc);
4986 XCloseDisplay(display);