3 * Copyright (c) 2009 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
5 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
6 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Much code and ideas taken from dwm under the following license:
22 * MIT/X Consortium License
24 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
25 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
26 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
27 * 2007 Premysl Hruby <dfenze at gmail dot com>
28 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
29 * 2007 Christof Musik <christof at sendfax dot de>
30 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
31 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
32 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
34 * Permission is hereby granted, free of charge, to any person obtaining a
35 * copy of this software and associated documentation files (the "Software"),
36 * to deal in the Software without restriction, including without limitation
37 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
38 * and/or sell copies of the Software, and to permit persons to whom the
39 * Software is furnished to do so, subject to the following conditions:
41 * The above copyright notice and this permission notice shall be included in
42 * all copies or substantial portions of the Software.
44 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
47 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
49 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
50 * DEALINGS IN THE SOFTWARE.
53 static const char *cvstag = "$scrotwm$";
55 #define SWM_VERSION "0.9.25"
71 #include <sys/types.h>
75 #include <sys/queue.h>
76 #include <sys/param.h>
77 #include <sys/select.h>
79 #include <X11/cursorfont.h>
80 #include <X11/keysym.h>
81 #include <X11/Xatom.h>
83 #include <X11/Xproto.h>
84 #include <X11/Xutil.h>
85 #include <X11/extensions/Xrandr.h>
92 # error XRandR versions less than 1.0 are not supported
97 #define SWM_XRR_HAS_CRTC
101 /* #define SWM_DEBUG */
103 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
104 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
105 #define SWM_D_MISC 0x0001
106 #define SWM_D_EVENT 0x0002
107 #define SWM_D_WS 0x0004
108 #define SWM_D_FOCUS 0x0008
109 #define SWM_D_MOVE 0x0010
110 #define SWM_D_STACK 0x0020
111 #define SWM_D_MOUSE 0x0040
112 #define SWM_D_PROP 0x0080
113 #define SWM_D_CLASS 0x0100
114 #define SWM_D_KEY 0x0200
115 #define SWM_D_QUIRK 0x0400
116 #define SWM_D_SPAWN 0x0800
117 #define SWM_D_EVENTQ 0x1000
118 #define SWM_D_CONF 0x2000
120 u_int32_t swm_debug = 0
137 #define DPRINTF(x...)
138 #define DNPRINTF(n,x...)
141 #define LENGTH(x) (sizeof x / sizeof x[0])
142 #define MODKEY Mod1Mask
143 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
144 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
145 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
146 #define SWM_PROPLEN (16)
147 #define SWM_FUNCNAME_LEN (32)
148 #define SWM_KEYS_LEN (255)
149 #define SWM_QUIRK_LEN (64)
150 #define X(r) (r)->g.x
151 #define Y(r) (r)->g.y
152 #define WIDTH(r) (r)->g.w
153 #define HEIGHT(r) (r)->g.h
154 #define SWM_MAX_FONT_STEPS (3)
155 #define WINID(w) (w ? w->id : 0)
157 #define SWM_FOCUS_DEFAULT (0)
158 #define SWM_FOCUS_SYNERGY (1)
159 #define SWM_FOCUS_FOLLOW (2)
162 #define SWM_LIB "/usr/local/lib/libswmhack.so"
170 volatile sig_atomic_t running = 1;
171 volatile sig_atomic_t restart_wm = 0;
173 int last_focus_event = FocusOut;
174 int (*xerrorxlib)(Display *, XErrorEvent *);
178 int xrandr_eventbase;
179 unsigned int numlockmask = 0;
183 int cycle_visible = 0;
185 int font_adjusted = 0;
186 unsigned int mod_key = MODKEY;
189 double dialog_ratio = .6;
191 #define SWM_BAR_MAX (256)
192 char *bar_argv[] = { NULL, NULL };
194 char bar_ext[SWM_BAR_MAX];
195 char bar_vertext[SWM_BAR_MAX];
197 sig_atomic_t bar_alarm = 0;
201 int bar_extra_running = 0;
204 int stack_enabled = 1;
205 int clock_enabled = 1;
206 char *clock_format = NULL;
207 int title_name_enabled = 0;
208 int title_class_enabled = 0;
209 int focus_mode = SWM_FOCUS_DEFAULT;
210 int disable_border = 0;
216 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
217 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
219 #define SWM_MENU_FN (2)
220 #define SWM_MENU_NB (4)
221 #define SWM_MENU_NF (6)
222 #define SWM_MENU_SB (8)
223 #define SWM_MENU_SF (10)
225 /* layout manager data */
226 struct swm_geometry {
236 /* virtual "screens" */
238 TAILQ_ENTRY(swm_region) entry;
239 struct swm_geometry g;
240 struct workspace *ws; /* current workspace on this region */
241 struct workspace *ws_prior; /* prior workspace on this region */
242 struct swm_screen *s; /* screen idx */
245 TAILQ_HEAD(swm_region_list, swm_region);
248 TAILQ_ENTRY(ws_win) entry;
251 struct ws_win *child_trans; /* transient child window */
252 struct swm_geometry g; /* current geometry */
253 struct swm_geometry g_float; /* geometry when floating */
254 struct swm_geometry rg_float; /* region geom when floating */
255 int g_floatvalid; /* flag: geometry in g_float is valid */
256 int floatmaxed; /* flag: floater was maxed in max_stack */
259 int font_size_boundary[SWM_MAX_FONT_STEPS];
265 unsigned long quirks;
266 struct workspace *ws; /* always valid */
267 struct swm_screen *s; /* always valid, never changes */
268 XWindowAttributes wa;
272 TAILQ_HEAD(ws_win_list, ws_win);
274 /* layout handlers */
276 void vertical_config(struct workspace *, int);
277 void vertical_stack(struct workspace *, struct swm_geometry *);
278 void horizontal_config(struct workspace *, int);
279 void horizontal_stack(struct workspace *, struct swm_geometry *);
280 void max_stack(struct workspace *, struct swm_geometry *);
282 struct ws_win *find_window(Window);
284 void grabbuttons(struct ws_win *, int);
285 void new_region(struct swm_screen *, int, int, int, int);
286 void unmanage_window(struct ws_win *);
287 long getstate(Window);
290 void (*l_stack)(struct workspace *, struct swm_geometry *);
291 void (*l_config)(struct workspace *, int);
293 #define SWM_L_FOCUSPREV (1<<0)
294 #define SWM_L_MAPONFOCUS (1<<1)
297 /* stack, configure */
298 { vertical_stack, vertical_config, 0, "[|]" },
299 { horizontal_stack, horizontal_config, 0, "[-]" },
301 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, "[ ]"},
302 { NULL, NULL, 0, NULL },
305 /* position of max_stack mode in the layouts array */
306 #define SWM_MAX_STACK 2
308 #define SWM_H_SLICE (32)
309 #define SWM_V_SLICE (32)
311 /* define work spaces */
313 int idx; /* workspace index */
314 struct layout *cur_layout; /* current layout handlers */
315 struct ws_win *focus; /* may be NULL */
316 struct ws_win *focus_prev; /* may be NULL */
317 struct swm_region *r; /* may be NULL */
318 struct swm_region *old_r; /* may be NULL */
319 struct ws_win_list winlist; /* list of windows in ws */
320 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
324 int horizontal_msize;
326 int horizontal_stacks;
333 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
334 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
336 /* physical screen mapping */
337 #define SWM_WS_MAX (10)
339 int idx; /* screen index */
340 struct swm_region_list rl; /* list of regions on this screen */
341 struct swm_region_list orl; /* list of old regions */
343 struct workspace ws[SWM_WS_MAX];
349 } c[SWM_S_COLOR_MAX];
351 struct swm_screen *screens;
354 /* args to functions */
357 #define SWM_ARG_ID_FOCUSNEXT (0)
358 #define SWM_ARG_ID_FOCUSPREV (1)
359 #define SWM_ARG_ID_FOCUSMAIN (2)
360 #define SWM_ARG_ID_FOCUSCUR (4)
361 #define SWM_ARG_ID_SWAPNEXT (10)
362 #define SWM_ARG_ID_SWAPPREV (11)
363 #define SWM_ARG_ID_SWAPMAIN (12)
364 #define SWM_ARG_ID_MOVELAST (13)
365 #define SWM_ARG_ID_MASTERSHRINK (20)
366 #define SWM_ARG_ID_MASTERGROW (21)
367 #define SWM_ARG_ID_MASTERADD (22)
368 #define SWM_ARG_ID_MASTERDEL (23)
369 #define SWM_ARG_ID_STACKRESET (30)
370 #define SWM_ARG_ID_STACKINIT (31)
371 #define SWM_ARG_ID_CYCLEWS_UP (40)
372 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
373 #define SWM_ARG_ID_CYCLESC_UP (42)
374 #define SWM_ARG_ID_CYCLESC_DOWN (43)
375 #define SWM_ARG_ID_STACKINC (50)
376 #define SWM_ARG_ID_STACKDEC (51)
377 #define SWM_ARG_ID_SS_ALL (60)
378 #define SWM_ARG_ID_SS_WINDOW (61)
379 #define SWM_ARG_ID_DONTCENTER (70)
380 #define SWM_ARG_ID_CENTER (71)
381 #define SWM_ARG_ID_KILLWINDOW (80)
382 #define SWM_ARG_ID_DELETEWINDOW (81)
386 void focus(struct swm_region *, union arg *);
387 void focus_magic(struct ws_win *, int);
388 #define SWM_F_GENERIC (0)
389 #define SWM_F_TRANSIENT (1)
395 #define SWM_Q_FLOAT (1<<0) /* float this window */
396 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
397 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
398 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
399 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
401 int quirks_size = 0, quirks_length = 0;
402 struct quirk *quirks = NULL;
419 name = "ButtonPress";
422 name = "ButtonRelease";
425 name = "MotionNotify";
428 name = "EnterNotify";
431 name = "LeaveNotify";
440 name = "KeymapNotify";
446 name = "GraphicsExpose";
451 case VisibilityNotify:
452 name = "VisibilityNotify";
455 name = "CreateNotify";
458 name = "DestroyNotify";
461 name = "UnmapNotify";
470 name = "ReparentNotify";
472 case ConfigureNotify:
473 name = "ConfigureNotify";
475 case ConfigureRequest:
476 name = "ConfigureRequest";
479 name = "GravityNotify";
482 name = "ResizeRequest";
484 case CirculateNotify:
485 name = "CirculateNotify";
487 case CirculateRequest:
488 name = "CirculateRequest";
491 name = "PropertyNotify";
494 name = "SelectionClear";
496 case SelectionRequest:
497 name = "SelectionRequest";
499 case SelectionNotify:
500 name = "SelectionNotify";
503 name = "ColormapNotify";
506 name = "ClientMessage";
509 name = "MappingNotify";
514 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
516 e->xany.window, name, e->type, QLength(display));
518 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
520 e->xany.window, e->type, QLength(display));
524 dumpwins(struct swm_region *r, union arg *args)
528 XWindowAttributes wa;
531 fprintf(stderr, "invalid workspace\n");
535 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
537 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
538 state = getstate(win->id);
539 if (!XGetWindowAttributes(display, win->id, &wa))
540 fprintf(stderr, "window: %lu failed "
541 "XGetWindowAttributes\n", win->id);
542 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
543 win->id, wa.map_state, state);
546 fprintf(stderr, "===== unmanaged window list =====\n");
547 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
548 state = getstate(win->id);
549 if (!XGetWindowAttributes(display, win->id, &wa))
550 fprintf(stderr, "window: %lu failed "
551 "XGetWindowAttributes\n", win->id);
552 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
553 win->id, wa.map_state, state);
556 fprintf(stderr, "=================================\n");
561 dumpwins(struct swm_region *r, union arg *args)
564 #endif /* SWM_DEBUG */
566 void expose(XEvent *);
567 void keypress(XEvent *);
568 void buttonpress(XEvent *);
569 void configurerequest(XEvent *);
570 void configurenotify(XEvent *);
571 void destroynotify(XEvent *);
572 void enternotify(XEvent *);
573 void focusevent(XEvent *);
574 void mapnotify(XEvent *);
575 void mappingnotify(XEvent *);
576 void maprequest(XEvent *);
577 void propertynotify(XEvent *);
578 void unmapnotify(XEvent *);
579 void visibilitynotify(XEvent *);
581 void (*handler[LASTEvent])(XEvent *) = {
583 [KeyPress] = keypress,
584 [ButtonPress] = buttonpress,
585 [ConfigureRequest] = configurerequest,
586 [ConfigureNotify] = configurenotify,
587 [DestroyNotify] = destroynotify,
588 [EnterNotify] = enternotify,
589 [FocusIn] = focusevent,
590 [FocusOut] = focusevent,
591 [MapNotify] = mapnotify,
592 [MappingNotify] = mappingnotify,
593 [MapRequest] = maprequest,
594 [PropertyNotify] = propertynotify,
595 [UnmapNotify] = unmapnotify,
596 [VisibilityNotify] = visibilitynotify,
602 int saved_errno, status;
609 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
615 warn("sighdlr: waitpid");
616 #endif /* SWM_DEBUG */
621 if (WIFEXITED(status)) {
622 if (WEXITSTATUS(status) != 0)
623 warnx("sighdlr: child exit status: %d",
624 WEXITSTATUS(status));
626 warnx("sighdlr: child is terminated "
628 #endif /* SWM_DEBUG */
646 name_to_color(char *colorname)
650 XColor screen_def, exact_def;
651 unsigned long result = 0;
652 char cname[32] = "#";
654 cmap = DefaultColormap(display, screens[0].idx);
655 status = XAllocNamedColor(display, cmap, colorname,
656 &screen_def, &exact_def);
658 strlcat(cname, colorname + 2, sizeof cname - 1);
659 status = XAllocNamedColor(display, cmap, cname, &screen_def,
663 result = screen_def.pixel;
665 fprintf(stderr, "color '%s' not found.\n", colorname);
671 setscreencolor(char *val, int i, int c)
673 if (i > 0 && i <= ScreenCount(display)) {
674 screens[i - 1].c[c].color = name_to_color(val);
675 free(screens[i - 1].c[c].name);
676 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
678 } else if (i == -1) {
679 for (i = 0; i < ScreenCount(display); i++) {
680 screens[i].c[c].color = name_to_color(val);
681 free(screens[i].c[c].name);
682 if ((screens[i].c[c].name = strdup(val)) == NULL)
686 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
687 i, ScreenCount(display));
691 custom_region(char *val)
693 unsigned int sidx, x, y, w, h;
695 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
696 errx(1, "invalid custom region, "
697 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
698 if (sidx < 1 || sidx > ScreenCount(display))
699 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
700 sidx, ScreenCount(display));
704 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
706 if (x < 0 || x > DisplayWidth(display, sidx) ||
707 y < 0 || y > DisplayHeight(display, sidx) ||
708 w + x > DisplayWidth(display, sidx) ||
709 h + y > DisplayHeight(display, sidx))
710 errx(1, "region %ux%u+%u+%u not within screen boundaries "
711 "(%ux%u)\n", w, h, x, y,
712 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
714 new_region(&screens[sidx], x, y, w, h);
718 socket_setnonblock(int fd)
722 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
723 err(1, "fcntl F_GETFL");
725 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
726 err(1, "fcntl F_SETFL");
730 bar_print(struct swm_region *r, char *s)
732 XClearWindow(display, r->bar_window);
733 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
734 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
743 bzero(bar_pipe, sizeof bar_pipe);
746 kill(bar_pid, SIGTERM);
749 strlcpy(bar_ext, "", sizeof bar_ext);
754 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
756 int do_class, do_name;
758 XClassHint *xch = NULL;
760 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
762 if ((xch = XAllocClassHint()) == NULL)
764 status = XGetClassHint(display, cur_focus->id, xch);
765 if (status == BadWindow || status == BadAlloc)
767 do_class = (title_class_enabled && xch->res_class != NULL);
768 do_name = (title_name_enabled && xch->res_name != NULL);
770 strlcat(s, xch->res_class, sz);
771 if (do_class && do_name)
774 strlcat(s, xch->res_name, sz);
787 struct swm_region *r;
791 char cn[SWM_BAR_MAX];
792 char loc[SWM_BAR_MAX];
796 if (bar_enabled == 0)
798 if (bar_extra && bar_extra_running) {
799 /* ignore short reads; it'll correct itself */
800 while ((b = fgetln(stdin, &len)) != NULL)
801 if (b && b[len - 1] == '\n') {
803 strlcpy(bar_ext, b, sizeof bar_ext);
805 if (b == NULL && errno != EAGAIN) {
806 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
807 errno, strerror(errno));
811 strlcpy(bar_ext, "", sizeof bar_ext);
813 if (clock_enabled == 0)
814 strlcpy(s, "", sizeof s);
817 localtime_r(&tmt, &tm);
818 strftime(s, sizeof s, clock_format, &tm);
819 strlcat(s, " ", sizeof s);
822 for (i = 0; i < ScreenCount(display); i++) {
824 TAILQ_FOREACH(r, &screens[i].rl, entry) {
825 strlcpy(cn, "", sizeof cn);
827 bar_class_name(cn, sizeof cn, r->ws->focus);
830 stack = r->ws->cur_layout->name;
832 snprintf(loc, sizeof loc, "%d:%d %s %s%s %s %s",
833 x++, r->ws->idx + 1, stack, s, cn, bar_ext,
848 bar_toggle(struct swm_region *r, union arg *args)
850 struct swm_region *tmpr;
851 int i, sc = ScreenCount(display);
853 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
856 for (i = 0; i < sc; i++)
857 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
858 XUnmapWindow(display, tmpr->bar_window);
860 for (i = 0; i < sc; i++)
861 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
862 XMapRaised(display, tmpr->bar_window);
864 bar_enabled = !bar_enabled;
867 /* must be after stack */
874 XSetWindowAttributes wa;
875 struct swm_region *r;
878 /* do this here because the conf file is in memory */
879 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
880 /* launch external status app */
881 bar_extra_running = 1;
882 if (pipe(bar_pipe) == -1)
883 err(1, "pipe error");
884 socket_setnonblock(bar_pipe[0]);
885 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
886 if (dup2(bar_pipe[0], 0) == -1)
888 if (dup2(bar_pipe[1], 1) == -1)
890 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
891 err(1, "could not disable SIGPIPE");
892 switch (bar_pid = fork()) {
894 err(1, "cannot fork");
898 execvp(bar_argv[0], bar_argv);
899 err(1, "%s external app failed", bar_argv[0]);
901 default: /* parent */
907 bzero(&wa, sizeof wa);
908 for (i = 0; i < ScreenCount(display); i++)
909 TAILQ_FOREACH(r, &screens[i].rl, entry) {
911 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
912 wa.background_pixel =
913 screens[i].c[SWM_S_COLOR_BAR].color;
914 XChangeWindowAttributes(display, r->bar_window,
915 CWBackPixel | CWBorderPixel, &wa);
921 bar_setup(struct swm_region *r)
926 XFreeFont(display, bar_fs);
930 for (i = 0; bar_fonts[i] != NULL; i++) {
931 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
937 if (bar_fonts[i] == NULL)
938 errx(1, "couldn't load font");
940 errx(1, "couldn't create font structure");
942 bar_height = bar_fs->ascent + bar_fs->descent + 3;
944 r->bar_window = XCreateSimpleWindow(display,
945 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
946 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
947 r->s->c[SWM_S_COLOR_BAR].color);
948 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
949 XSetFont(display, bar_gc, bar_fs->fid);
950 XSelectInput(display, r->bar_window, VisibilityChangeMask);
952 XMapRaised(display, r->bar_window);
953 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
955 if (signal(SIGALRM, bar_signal) == SIG_ERR)
956 err(1, "could not install bar_signal");
961 set_win_state(struct ws_win *win, long state)
963 long data[] = {state, None};
965 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
970 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
971 (unsigned char *)data, 2);
979 unsigned char *p = NULL;
980 unsigned long n, extra;
983 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
984 &real, &format, &n, &extra, (unsigned char **)&p);
985 if (status != Success)
988 result = *((long *)p);
994 version(struct swm_region *r, union arg *args)
996 bar_version = !bar_version;
998 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
999 SWM_VERSION, cvstag);
1001 strlcpy(bar_vertext, "", sizeof bar_vertext);
1006 client_msg(struct ws_win *win, Atom a)
1008 XClientMessageEvent cm;
1013 bzero(&cm, sizeof cm);
1014 cm.type = ClientMessage;
1015 cm.window = win->id;
1016 cm.message_type = aprot;
1019 cm.data.l[1] = CurrentTime;
1020 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1024 configreq_win(struct ws_win *win)
1026 XConfigureRequestEvent cr;
1031 bzero(&cr, sizeof cr);
1032 cr.type = ConfigureRequest;
1033 cr.display = display;
1034 cr.parent = win->id;
1035 cr.window = win->id;
1038 cr.width = win->g.w;
1039 cr.height = win->g.h;
1040 cr.border_width = 1;
1042 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&cr);
1046 config_win(struct ws_win *win)
1050 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
1051 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1056 ce.type = ConfigureNotify;
1057 ce.display = display;
1059 ce.window = win->id;
1062 ce.width = win->g.w;
1063 ce.height = win->g.h;
1064 ce.border_width = 1; /* XXX store this! */
1066 ce.override_redirect = False;
1067 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1071 count_win(struct workspace *ws, int count_transient)
1076 TAILQ_FOREACH(win, &ws->winlist, entry) {
1077 if (count_transient == 0 && win->floating)
1079 if (count_transient == 0 && win->transient)
1083 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1089 quit(struct swm_region *r, union arg *args)
1091 DNPRINTF(SWM_D_MISC, "quit\n");
1096 unmap_window(struct ws_win *win)
1101 /* don't unmap again */
1102 if (getstate(win->id) == IconicState)
1105 set_win_state(win, IconicState);
1107 XUnmapWindow(display, win->id);
1109 XSetWindowBorder(display, win->id,
1110 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1119 for (i = 0; i < ScreenCount(display); i++)
1120 for (j = 0; j < SWM_WS_MAX; j++)
1121 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1126 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1133 event.display = display; /* Ignored, but what the hell */
1134 event.window = win->id;
1135 event.root = win->s->root;
1136 event.subwindow = None;
1137 event.time = CurrentTime;
1142 event.same_screen = True;
1143 event.keycode = XKeysymToKeycode(display, keysym);
1144 event.state = modifiers;
1146 event.type = KeyPress;
1147 XSendEvent(event.display, event.window, True,
1148 KeyPressMask, (XEvent *)&event);
1150 event.type = KeyRelease;
1151 XSendEvent(event.display, event.window, True,
1152 KeyPressMask, (XEvent *)&event);
1157 restart(struct swm_region *r, union arg *args)
1159 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1161 /* disable alarm because the following code may not be interrupted */
1163 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1164 errx(1, "can't disable alarm");
1169 XCloseDisplay(display);
1170 execvp(start_argv[0], start_argv);
1171 fprintf(stderr, "execvp failed\n");
1177 root_to_region(Window root)
1179 struct swm_region *r = NULL;
1181 int i, x, y, wx, wy;
1184 for (i = 0; i < ScreenCount(display); i++)
1185 if (screens[i].root == root)
1188 if (XQueryPointer(display, screens[i].root,
1189 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1190 /* choose a region based on pointer location */
1191 TAILQ_FOREACH(r, &screens[i].rl, entry)
1192 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1193 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1198 r = TAILQ_FIRST(&screens[i].rl);
1204 find_unmanaged_window(Window id)
1209 for (i = 0; i < ScreenCount(display); i++)
1210 for (j = 0; j < SWM_WS_MAX; j++)
1211 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1219 find_window(Window id)
1222 Window wrr, wpr, *wcr = NULL;
1225 for (i = 0; i < ScreenCount(display); i++)
1226 for (j = 0; j < SWM_WS_MAX; j++)
1227 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1231 /* if we were looking for the parent return that window instead */
1232 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1237 /* ignore not found and root */
1238 if (wpr == 0 || wrr == wpr)
1241 /* look for parent */
1242 for (i = 0; i < ScreenCount(display); i++)
1243 for (j = 0; j < SWM_WS_MAX; j++)
1244 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1252 spawn(struct swm_region *r, union arg *args)
1257 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1259 * The double-fork construct avoids zombie processes and keeps the code
1260 * clean from stupid signal handlers.
1265 close(ConnectionNumber(display));
1266 setenv("LD_PRELOAD", SWM_LIB, 1);
1267 if (asprintf(&ret, "%d", r->ws->idx)) {
1268 setenv("_SWM_WS", ret, 1);
1271 if (asprintf(&ret, "%d", getpid())) {
1272 setenv("_SWM_PID", ret, 1);
1276 /* kill stdin, mplayer, ssh-add etc. need that */
1277 si = open("/dev/null", O_RDONLY, 0);
1279 err(1, "open /dev/null");
1280 if (dup2(si, 0) == -1)
1281 err(1, "dup2 /dev/null");
1282 execvp(args->argv[0], args->argv);
1283 fprintf(stderr, "execvp failed\n");
1291 spawnterm(struct swm_region *r, union arg *args)
1293 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1296 setenv("_SWM_XTERM_FONTADJ", "", 1);
1301 kill_refs(struct ws_win *win)
1304 struct swm_region *r;
1305 struct workspace *ws;
1310 for (i = 0; i < ScreenCount(display); i++)
1311 TAILQ_FOREACH(r, &screens[i].rl, entry)
1312 for (x = 0; x < SWM_WS_MAX; x++) {
1314 if (win == ws->focus)
1316 if (win == ws->focus_prev)
1317 ws->focus_prev = NULL;
1322 validate_win(struct ws_win *testwin)
1325 struct workspace *ws;
1326 struct swm_region *r;
1327 int i, x, foundit = 0;
1329 if (testwin == NULL)
1332 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1333 TAILQ_FOREACH(r, &screens[i].rl, entry)
1334 for (x = 0; x < SWM_WS_MAX; x++) {
1336 TAILQ_FOREACH(win, &ws->winlist, entry)
1344 validate_ws(struct workspace *testws)
1346 struct swm_region *r;
1347 struct workspace *ws;
1350 /* validate all ws */
1351 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1352 TAILQ_FOREACH(r, &screens[i].rl, entry)
1353 for (x = 0; x < SWM_WS_MAX; x++) {
1362 unfocus_win(struct ws_win *win)
1366 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1370 if (win->ws == NULL)
1373 if (validate_ws(win->ws))
1376 if (win->ws->r == NULL)
1379 if (validate_win(win)) {
1384 if (win->ws->focus == win) {
1385 win->ws->focus = NULL;
1386 win->ws->focus_prev = win;
1389 if (validate_win(win->ws->focus)) {
1390 kill_refs(win->ws->focus);
1391 win->ws->focus = NULL;
1393 if (validate_win(win->ws->focus_prev)) {
1394 kill_refs(win->ws->focus_prev);
1395 win->ws->focus_prev = NULL;
1398 /* drain all previous unfocus events */
1399 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
1402 grabbuttons(win, 0);
1403 XSetWindowBorder(display, win->id,
1404 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1414 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
1416 for (i = 0; i < ScreenCount(display); i++)
1417 for (j = 0; j < SWM_WS_MAX; j++)
1418 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1423 focus_win(struct ws_win *win)
1428 struct ws_win *cfw = NULL;
1431 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1435 if (win->ws == NULL)
1438 if (validate_ws(win->ws))
1440 if (validate_win(win)) {
1445 if (validate_win(win)) {
1450 XGetInputFocus(display, &cur_focus, &rr);
1451 if ((cfw = find_window(cur_focus)) != NULL)
1454 win->ws->focus = win;
1456 if (win->ws->r != NULL) {
1457 /* drain all previous focus events */
1458 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
1462 XSetInputFocus(display, win->id,
1463 RevertToParent, CurrentTime);
1464 grabbuttons(win, 1);
1465 XSetWindowBorder(display, win->id,
1466 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1467 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS)
1468 XMapRaised(display, win->id);
1473 switchws(struct swm_region *r, union arg *args)
1475 int wsid = args->id, unmap_old = 0;
1476 struct swm_region *this_r, *other_r;
1478 struct workspace *new_ws, *old_ws;
1485 old_ws = this_r->ws;
1486 new_ws = &this_r->s->ws[wsid];
1488 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1489 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1492 if (new_ws == NULL || old_ws == NULL)
1494 if (new_ws == old_ws)
1497 other_r = new_ws->r;
1498 if (other_r == NULL) {
1499 /* the other workspace is hidden, hide this one */
1503 /* the other ws is visible in another region, exchange them */
1504 other_r->ws_prior = new_ws;
1505 other_r->ws = old_ws;
1506 old_ws->r = other_r;
1508 this_r->ws_prior = old_ws;
1509 this_r->ws = new_ws;
1513 a.id = SWM_ARG_ID_FOCUSCUR;
1514 focus(new_ws->r, &a);
1517 /* unmap old windows */
1519 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1524 cyclews(struct swm_region *r, union arg *args)
1527 struct swm_screen *s = r->s;
1529 DNPRINTF(SWM_D_WS, "cyclews id %d "
1530 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1531 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1536 case SWM_ARG_ID_CYCLEWS_UP:
1537 if (a.id < SWM_WS_MAX - 1)
1542 case SWM_ARG_ID_CYCLEWS_DOWN:
1546 a.id = SWM_WS_MAX - 1;
1552 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1554 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1558 } while (a.id != r->ws->idx);
1562 priorws(struct swm_region *r, union arg *args)
1566 DNPRINTF(SWM_D_WS, "priorws id %d "
1567 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1568 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1570 if (r->ws_prior == NULL)
1573 a.id = r->ws_prior->idx;
1578 cyclescr(struct swm_region *r, union arg *args)
1580 struct swm_region *rr = NULL;
1584 /* do nothing if we don't have more than one screen */
1585 if (!(ScreenCount(display) > 1 || outputs > 1))
1590 case SWM_ARG_ID_CYCLESC_UP:
1591 rr = TAILQ_NEXT(r, entry);
1593 rr = TAILQ_FIRST(&screens[i].rl);
1595 case SWM_ARG_ID_CYCLESC_DOWN:
1596 rr = TAILQ_PREV(r, swm_region_list, entry);
1598 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1606 /* move mouse to region */
1608 y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
1609 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1611 a.id = SWM_ARG_ID_FOCUSCUR;
1614 if (rr->ws->focus) {
1615 /* move to focus window */
1616 x = rr->ws->focus->g.x + 1;
1617 y = rr->ws->focus->g.y + 1;
1618 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1623 swapwin(struct swm_region *r, union arg *args)
1625 struct ws_win *target, *source;
1626 struct ws_win *cur_focus;
1627 struct ws_win_list *wl;
1630 DNPRINTF(SWM_D_WS, "swapwin id %d "
1631 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1632 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1634 cur_focus = r->ws->focus;
1635 if (cur_focus == NULL)
1639 wl = &source->ws->winlist;
1642 case SWM_ARG_ID_SWAPPREV:
1643 target = TAILQ_PREV(source, ws_win_list, entry);
1644 TAILQ_REMOVE(wl, cur_focus, entry);
1646 TAILQ_INSERT_TAIL(wl, source, entry);
1648 TAILQ_INSERT_BEFORE(target, source, entry);
1650 case SWM_ARG_ID_SWAPNEXT:
1651 target = TAILQ_NEXT(source, entry);
1652 TAILQ_REMOVE(wl, source, entry);
1654 TAILQ_INSERT_HEAD(wl, source, entry);
1656 TAILQ_INSERT_AFTER(wl, target, source, entry);
1658 case SWM_ARG_ID_SWAPMAIN:
1659 target = TAILQ_FIRST(wl);
1660 if (target == source) {
1661 if (source->ws->focus_prev != NULL &&
1662 source->ws->focus_prev != target)
1664 source = source->ws->focus_prev;
1668 if (target == NULL || source == NULL)
1670 source->ws->focus_prev = target;
1671 TAILQ_REMOVE(wl, target, entry);
1672 TAILQ_INSERT_BEFORE(source, target, entry);
1673 TAILQ_REMOVE(wl, source, entry);
1674 TAILQ_INSERT_HEAD(wl, source, entry);
1676 case SWM_ARG_ID_MOVELAST:
1677 TAILQ_REMOVE(wl, source, entry);
1678 TAILQ_INSERT_TAIL(wl, source, entry);
1681 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1689 focus_prev(struct ws_win *win)
1691 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
1692 struct ws_win *cur_focus = NULL;
1693 struct ws_win_list *wl = NULL;
1694 struct workspace *ws = NULL;
1696 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
1698 if (!(win && win->ws))
1703 cur_focus = ws->focus;
1704 winlostfocus = cur_focus;
1706 /* pickle, just focus on whatever */
1707 if (cur_focus == NULL) {
1708 /* use prev_focus if valid */
1709 if (ws->focus_prev && ws->focus_prev != cur_focus &&
1710 find_window(WINID(ws->focus_prev)))
1711 winfocus = ws->focus_prev;
1712 if (winfocus == NULL)
1713 winfocus = TAILQ_FIRST(wl);
1717 /* if transient focus on parent */
1718 if (cur_focus->transient) {
1719 winfocus = find_window(cur_focus->transient);
1723 /* if in max_stack try harder */
1724 if (ws->cur_layout->flags & SWM_L_FOCUSPREV) {
1725 if (cur_focus != ws->focus_prev)
1726 winfocus = ws->focus_prev;
1727 else if (cur_focus != ws->focus)
1728 winfocus = ws->focus;
1730 winfocus = TAILQ_PREV(win, ws_win_list, entry);
1735 if (cur_focus == win)
1736 winfocus = TAILQ_PREV(win, ws_win_list, entry);
1737 if (winfocus == NULL)
1738 winfocus = TAILQ_LAST(wl, ws_win_list);
1739 if (winfocus == NULL || winfocus == win)
1740 winfocus = TAILQ_NEXT(cur_focus, entry);
1742 if (winfocus == winlostfocus || winfocus == NULL)
1745 focus_magic(winfocus, SWM_F_GENERIC);
1749 focus(struct swm_region *r, union arg *args)
1751 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
1752 struct ws_win *cur_focus = NULL;
1753 struct ws_win_list *wl = NULL;
1754 struct workspace *ws = NULL;
1759 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1761 /* treat FOCUS_CUR special */
1762 if (args->id == SWM_ARG_ID_FOCUSCUR) {
1764 winfocus = r->ws->focus;
1765 else if (r->ws->focus_prev)
1766 winfocus = r->ws->focus_prev;
1768 winfocus = TAILQ_FIRST(&r->ws->winlist);
1770 focus_magic(winfocus, SWM_F_GENERIC);
1774 if ((cur_focus = r->ws->focus) == NULL)
1779 winlostfocus = cur_focus;
1782 case SWM_ARG_ID_FOCUSPREV:
1783 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1784 if (winfocus == NULL)
1785 winfocus = TAILQ_LAST(wl, ws_win_list);
1788 case SWM_ARG_ID_FOCUSNEXT:
1789 winfocus = TAILQ_NEXT(cur_focus, entry);
1790 if (winfocus == NULL)
1791 winfocus = TAILQ_FIRST(wl);
1794 case SWM_ARG_ID_FOCUSMAIN:
1795 winfocus = TAILQ_FIRST(wl);
1796 if (winfocus == cur_focus)
1797 winfocus = cur_focus->ws->focus_prev;
1804 if (winfocus == winlostfocus || winfocus == NULL)
1807 focus_magic(winfocus, SWM_F_GENERIC);
1811 cycle_layout(struct swm_region *r, union arg *args)
1813 struct workspace *ws = r->ws;
1814 struct ws_win *winfocus;
1817 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1819 winfocus = ws->focus;
1822 if (ws->cur_layout->l_stack == NULL)
1823 ws->cur_layout = &layouts[0];
1826 a.id = SWM_ARG_ID_FOCUSCUR;
1832 stack_config(struct swm_region *r, union arg *args)
1834 struct workspace *ws = r->ws;
1836 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1839 if (ws->cur_layout->l_config != NULL)
1840 ws->cur_layout->l_config(ws, args->id);
1842 if (args->id != SWM_ARG_ID_STACKINIT);
1848 struct swm_geometry g;
1849 struct swm_region *r;
1852 DNPRINTF(SWM_D_STACK, "stack\n");
1854 for (i = 0; i < ScreenCount(display); i++) {
1856 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1857 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1858 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1860 /* start with screen geometry, adjust for bar */
1868 r->ws->cur_layout->l_stack(r->ws, &g);
1869 /* save r so we can track region changes */
1878 store_float_geom(struct ws_win *win, struct swm_region *r)
1880 /* retain window geom and region geom */
1881 win->g_float.x = win->g.x;
1882 win->g_float.y = win->g.y;
1883 win->g_float.w = win->g.w;
1884 win->g_float.h = win->g.h;
1885 win->rg_float.x = r->g.x;
1886 win->rg_float.y = r->g.y;
1887 win->rg_float.w = r->g.w;
1888 win->rg_float.h = r->g.h;
1889 win->g_floatvalid = 1;
1893 stack_floater(struct ws_win *win, struct swm_region *r)
1901 bzero(&wc, sizeof wc);
1902 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1905 * to allow windows to change their size (e.g. mplayer fs) only retrieve
1906 * geom on ws switches or return from max mode
1909 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid) ) {
1911 * use stored g and rg to set relative position and size
1912 * as in old region or before max stack mode
1914 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
1915 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
1916 win->g.w = win->g_float.w;
1917 win->g.h = win->g_float.h;
1918 win->g_floatvalid = 0;
1921 win->floatmaxed = 0;
1923 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
1924 (win->g.h >= HEIGHT(r)))
1925 wc.border_width = 0;
1927 wc.border_width = 1;
1928 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
1929 win->g.w = (double)WIDTH(r) * dialog_ratio;
1930 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1935 * floaters and transients are auto-centred unless moved
1938 win->g.x = r->g.x + (WIDTH(r) - win->g.w) / 2;
1939 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) / 2;
1942 /* win can be outside r if new r smaller than old r */
1943 /* Ensure top left corner inside r (move probs otherwise) */
1944 if (win->g.x < r->g.x )
1946 if (win->g.x > r->g.x + r->g.w - 1)
1947 win->g.x = (win->g.w > r->g.w) ? r->g.x :
1948 (r->g.x + r->g.w - win->g.w - 2);
1949 if (win->g.y < r->g.y )
1951 if (win->g.y > r->g.y + r->g.h - 1)
1952 win->g.y = (win->g.h > r->g.h) ? r->g.y :
1953 (r->g.y + r->g.h - win->g.h - 2);
1957 wc.width = win->g.w;
1958 wc.height = win->g.h;
1961 * Retain floater and transient geometry for correct positioning
1962 * when ws changes region
1964 store_float_geom(win, r);
1966 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
1967 win->id, wc.x, wc.y, wc.width, wc.height);
1969 XConfigureWindow(display, win->id, mask, &wc);
1974 * Send keystrokes to terminal to decrease/increase the font size as the
1975 * window size changes.
1978 adjust_font(struct ws_win *win)
1980 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
1981 win->floating || win->transient)
1984 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
1985 win->g.w / win->sh.width_inc < term_width &&
1986 win->font_steps < SWM_MAX_FONT_STEPS) {
1987 win->font_size_boundary[win->font_steps] =
1988 (win->sh.width_inc * term_width) + win->sh.base_width;
1991 win->last_inc = win->sh.width_inc;
1992 fake_keypress(win, XK_KP_Subtract, ShiftMask);
1993 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
1994 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
1997 win->last_inc = win->sh.width_inc;
1998 fake_keypress(win, XK_KP_Add, ShiftMask);
2002 #define SWAPXY(g) do { \
2004 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2005 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2008 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2011 XWindowAttributes wa;
2012 struct swm_geometry win_g, r_g = *g;
2014 int i, j, s, stacks;
2015 int w_inc = 1, h_inc, w_base = 1, h_base;
2016 int hrh, extra = 0, h_slice, last_h = 0;
2017 int split, colno, winno, mwin, msize, mscale;
2018 int remain, missing, v_slice, reconfigure;
2021 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2022 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2024 winno = count_win(ws, 0);
2025 if (winno == 0 && count_win(ws, 1) == 0)
2028 TAILQ_FOREACH(win, &ws->winlist, entry)
2029 if (win->transient == 0 && win->floating == 0)
2036 w_inc = win->sh.width_inc;
2037 w_base = win->sh.base_width;
2038 mwin = ws->l_state.horizontal_mwin;
2039 mscale = ws->l_state.horizontal_msize;
2040 stacks = ws->l_state.horizontal_stacks;
2043 w_inc = win->sh.height_inc;
2044 w_base = win->sh.base_height;
2045 mwin = ws->l_state.vertical_mwin;
2046 mscale = ws->l_state.vertical_msize;
2047 stacks = ws->l_state.vertical_stacks;
2051 if (stacks > winno - mwin)
2052 stacks = winno - mwin;
2056 h_slice = r_g.h / SWM_H_SLICE;
2057 if (mwin && winno > mwin) {
2058 v_slice = r_g.w / SWM_V_SLICE;
2062 win_g.w = v_slice * mscale;
2064 if (w_inc > 1 && w_inc < v_slice) {
2065 /* adjust for window's requested size increment */
2066 remain = (win_g.w - w_base) % w_inc;
2067 missing = w_inc - remain;
2074 win_g.x += r_g.w - msize;
2077 colno = split = winno / stacks;
2078 win_g.w = ((r_g.w - (stacks * 2) + 2) / stacks);
2080 hrh = r_g.h / colno;
2081 extra = r_g.h - (colno * hrh);
2084 /* stack all the tiled windows */
2085 i = j = 0, s = stacks;
2086 TAILQ_FOREACH(win, &ws->winlist, entry) {
2087 if (win->transient != 0 || win->floating != 0)
2090 if (split && i == split) {
2091 colno = (winno - mwin) / stacks;
2092 if (s <= (winno - mwin) % stacks)
2094 split = split + colno;
2095 hrh = (r_g.h / colno);
2096 extra = r_g.h - (colno * hrh);
2100 win_g.x += win_g.w + 2;
2101 win_g.w = (r_g.w - msize - (stacks * 2)) / stacks;
2103 win_g.w += (r_g.w - msize - (stacks * 2)) %
2110 h_inc = win->sh.width_inc;
2111 h_base = win->sh.base_width;
2113 h_inc = win->sh.height_inc;
2114 h_base = win->sh.base_height;
2116 if (j == colno - 1) {
2117 win_g.h = hrh + extra;
2118 } else if (h_inc > 1 && h_inc < h_slice) {
2119 /* adjust for window's requested size increment */
2120 remain = (win_g.h - h_base) % h_inc;
2121 missing = h_inc - remain;
2123 if (missing <= extra || j == 0) {
2135 win_g.y += last_h + 2;
2137 bzero(&wc, sizeof wc);
2138 if (disable_border && bar_enabled == 0 && winno == 1){
2139 wc.border_width = 0;
2143 wc.border_width = 1;
2146 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2147 win->g.w != win_g.h || win->g.h != win_g.w) {
2149 win->g.x = wc.x = win_g.y;
2150 win->g.y = wc.y = win_g.x;
2151 win->g.w = wc.width = win_g.h;
2152 win->g.h = wc.height = win_g.w;
2155 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2156 win->g.w != win_g.w || win->g.h != win_g.h) {
2158 win->g.x = wc.x = win_g.x;
2159 win->g.y = wc.y = win_g.y;
2160 win->g.w = wc.width = win_g.w;
2161 win->g.h = wc.height = win_g.h;
2166 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2167 XConfigureWindow(display, win->id, mask, &wc);
2171 if (XGetWindowAttributes(display, win->id, &wa))
2172 if (wa.map_state == IsUnmapped)
2173 XMapRaised(display, win->id);
2181 /* now, stack all the floaters and transients */
2182 TAILQ_FOREACH(win, &ws->winlist, entry) {
2183 if (win->transient == 0 && win->floating == 0)
2186 stack_floater(win, ws->r);
2187 XMapRaised(display, win->id);
2192 vertical_config(struct workspace *ws, int id)
2194 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2197 case SWM_ARG_ID_STACKRESET:
2198 case SWM_ARG_ID_STACKINIT:
2199 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2200 ws->l_state.vertical_mwin = 1;
2201 ws->l_state.vertical_stacks = 1;
2203 case SWM_ARG_ID_MASTERSHRINK:
2204 if (ws->l_state.vertical_msize > 1)
2205 ws->l_state.vertical_msize--;
2207 case SWM_ARG_ID_MASTERGROW:
2208 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2209 ws->l_state.vertical_msize++;
2211 case SWM_ARG_ID_MASTERADD:
2212 ws->l_state.vertical_mwin++;
2214 case SWM_ARG_ID_MASTERDEL:
2215 if (ws->l_state.vertical_mwin > 0)
2216 ws->l_state.vertical_mwin--;
2218 case SWM_ARG_ID_STACKINC:
2219 ws->l_state.vertical_stacks++;
2221 case SWM_ARG_ID_STACKDEC:
2222 if (ws->l_state.vertical_stacks > 1)
2223 ws->l_state.vertical_stacks--;
2231 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2233 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2235 stack_master(ws, g, 0, 0);
2239 horizontal_config(struct workspace *ws, int id)
2241 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2244 case SWM_ARG_ID_STACKRESET:
2245 case SWM_ARG_ID_STACKINIT:
2246 ws->l_state.horizontal_mwin = 1;
2247 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2248 ws->l_state.horizontal_stacks = 1;
2250 case SWM_ARG_ID_MASTERSHRINK:
2251 if (ws->l_state.horizontal_msize > 1)
2252 ws->l_state.horizontal_msize--;
2254 case SWM_ARG_ID_MASTERGROW:
2255 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2256 ws->l_state.horizontal_msize++;
2258 case SWM_ARG_ID_MASTERADD:
2259 ws->l_state.horizontal_mwin++;
2261 case SWM_ARG_ID_MASTERDEL:
2262 if (ws->l_state.horizontal_mwin > 0)
2263 ws->l_state.horizontal_mwin--;
2265 case SWM_ARG_ID_STACKINC:
2266 ws->l_state.horizontal_stacks++;
2268 case SWM_ARG_ID_STACKDEC:
2269 if (ws->l_state.horizontal_stacks > 1)
2270 ws->l_state.horizontal_stacks--;
2278 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2280 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
2282 stack_master(ws, g, 1, 0);
2285 /* fullscreen view */
2287 max_stack(struct workspace *ws, struct swm_geometry *g)
2290 struct swm_geometry gg = *g;
2291 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2295 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2300 winno = count_win(ws, 0);
2301 if (winno == 0 && count_win(ws, 1) == 0)
2304 TAILQ_FOREACH(win, &ws->winlist, entry) {
2305 if (win->transient) {
2307 parent = find_window(win->transient);
2311 if (win->floating && win->floatmaxed == 0 ) {
2313 * retain geometry for retrieval on exit from
2316 store_float_geom(win, ws->r);
2317 win->floatmaxed = 1;
2320 /* only reconfigure if necessary */
2321 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2323 bzero(&wc, sizeof wc);
2324 win->g.x = wc.x = gg.x;
2325 win->g.y = wc.y = gg.y;
2327 wc.border_width = 1;
2328 win->g.w = wc.width = gg.w;
2329 win->g.h = wc.height = gg.h;
2331 wc.border_width = 0;
2332 win->g.w = wc.width = gg.w + 2;
2333 win->g.h = wc.height = gg.h + 2;
2335 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2336 XConfigureWindow(display, win->id, mask, &wc);
2339 /* unmap only if we don't have multi screen */
2340 if (win != ws->focus)
2341 if (!(ScreenCount(display) > 1 || outputs > 1))
2345 /* put the last transient on top */
2348 XMapRaised(display, parent->id);
2349 stack_floater(wintrans, ws->r);
2350 focus_magic(wintrans, SWM_F_TRANSIENT);
2355 send_to_ws(struct swm_region *r, union arg *args)
2357 int wsid = args->id;
2358 struct ws_win *win = win;
2359 struct workspace *ws, *nws;
2360 Atom ws_idx_atom = 0;
2361 unsigned char ws_idx_str[SWM_PROPLEN];
2370 if (win->ws->idx == wsid)
2373 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2376 nws = &win->s->ws[wsid];
2378 a.id = SWM_ARG_ID_FOCUSPREV;
2381 TAILQ_REMOVE(&ws->winlist, win, entry);
2382 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2385 /* Try to update the window's workspace property */
2386 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2388 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2389 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2391 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2392 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2398 wkill(struct swm_region *r, union arg *args)
2400 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
2402 if(r->ws->focus == NULL)
2405 if (args->id == SWM_ARG_ID_KILLWINDOW)
2406 XKillClient(display, r->ws->focus->id);
2408 if (r->ws->focus->can_delete)
2409 client_msg(r->ws->focus, adelete);
2413 floating_toggle(struct swm_region *r, union arg *args)
2415 struct ws_win *win = r->ws->focus;
2421 /* reject floating toggles in max stack mode */
2422 if (r->ws->cur_layout == &layouts[SWM_MAX_STACK])
2425 if (win->floating) {
2426 if (!win->floatmaxed) {
2427 /* retain position for refloat */
2428 store_float_geom(win, r);
2432 if (win->g_floatvalid) {
2433 /* refloat at last floating relative position */
2434 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2435 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2436 win->g.w = win->g_float.w;
2437 win->g.h = win->g_float.h;
2443 a.id = SWM_ARG_ID_FOCUSCUR;
2444 focus(win->ws->r, &a);
2448 resize_window(struct ws_win *win, int center)
2452 struct swm_region *r;
2454 r = root_to_region(win->wa.root);
2455 bzero(&wc, sizeof wc);
2456 mask = CWBorderWidth | CWWidth | CWHeight;
2457 wc.border_width = 1;
2458 wc.width = win->g.w;
2459 wc.height = win->g.h;
2460 if (center == SWM_ARG_ID_CENTER) {
2461 wc.x = (WIDTH(r) - win->g.w) / 2;
2462 wc.y = (HEIGHT(r) - win->g.h) / 2;
2466 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
2467 win->id, wc.x, wc.y, wc.width, wc.height);
2469 XConfigureWindow(display, win->id, mask, &wc);
2474 resize(struct ws_win *win, union arg *args)
2478 struct swm_region *r = win->ws->r;
2483 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
2484 win->id, win->floating, win->transient);
2486 if (!(win->transient != 0 || win->floating != 0))
2489 /* reject resizes in max mode for floaters (transient ok) */
2490 if (win->floatmaxed)
2494 /* raise the window = move to last in window list */
2495 a.id = SWM_ARG_ID_MOVELAST;
2499 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2500 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2503 /* place pointer at bottom left corner or nearest point inside r */
2504 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
2507 relx = r->g.x + r->g.w - win->g.x - 2;
2509 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
2512 rely = r->g.y + r->g.h - win->g.y - 2;
2514 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
2516 XMaskEvent(display, MOUSEMASK | ExposureMask |
2517 SubstructureRedirectMask, &ev);
2519 case ConfigureRequest:
2522 handler[ev.type](&ev);
2525 /* do not allow resize outside of region */
2526 if ( ev.xmotion.x_root < r->g.x ||
2527 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
2528 ev.xmotion.y_root < r->g.y ||
2529 ev.xmotion.y_root > r->g.y + r->g.h - 1)
2532 if (ev.xmotion.x <= 1)
2534 if (ev.xmotion.y <= 1)
2536 win->g.w = ev.xmotion.x;
2537 win->g.h = ev.xmotion.y;
2539 /* not free, don't sync more than 60 times / second */
2540 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2541 time = ev.xmotion.time;
2542 XSync(display, False);
2543 resize_window(win, args->id);
2547 } while (ev.type != ButtonRelease);
2549 XSync(display, False);
2550 resize_window(win, args->id);
2552 store_float_geom(win,r);
2554 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
2556 XUngrabPointer(display, CurrentTime);
2559 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2563 move_window(struct ws_win *win)
2567 struct swm_region *r;
2569 r = root_to_region(win->wa.root);
2570 bzero(&wc, sizeof wc);
2575 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
2576 win->id, wc.x, wc.y, wc.width, wc.height);
2578 XConfigureWindow(display, win->id, mask, &wc);
2583 move(struct ws_win *win, union arg *args)
2587 struct swm_region *r = win->ws->r;
2590 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
2591 win->id, win->floating, win->transient);
2593 /* in max_stack mode should only move transients */
2594 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
2598 if (win->floating == 0 && !win->transient) {
2602 /* raise the window = move to last in window list */
2603 a.id = SWM_ARG_ID_MOVELAST;
2607 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2608 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2610 XWarpPointer(display, None, win->id, 0, 0, 0, 0, -1, -1);
2612 XMaskEvent(display, MOUSEMASK | ExposureMask |
2613 SubstructureRedirectMask, &ev);
2615 case ConfigureRequest:
2618 handler[ev.type](&ev);
2621 /* don't allow to move window origin out of region */
2622 if ( ev.xmotion.x_root < r->g.x ||
2623 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
2624 ev.xmotion.y_root < r->g.y ||
2625 ev.xmotion.y_root > r->g.y + r->g.h - 1)
2628 win->g.x = ev.xmotion.x_root;
2629 win->g.y = ev.xmotion.y_root;
2631 /* not free, don't sync more than 60 times / second */
2632 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2633 time = ev.xmotion.time;
2634 XSync(display, False);
2639 } while (ev.type != ButtonRelease);
2641 XSync(display, False);
2644 store_float_geom(win,r);
2645 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2646 XUngrabPointer(display, CurrentTime);
2649 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2652 /* user/key callable function IDs */
2711 /* key definitions */
2713 dummykeyfunc(struct swm_region *r, union arg *args)
2718 legacyfunc(struct swm_region *r, union arg *args)
2723 char name[SWM_FUNCNAME_LEN];
2724 void (*func)(struct swm_region *r, union arg *);
2726 } keyfuncs[kf_invalid + 1] = {
2727 /* name function argument */
2728 { "cycle_layout", cycle_layout, {0} },
2729 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
2730 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
2731 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
2732 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
2733 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
2734 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
2735 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
2736 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
2737 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
2738 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
2739 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
2740 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
2741 { "spawn_term", spawnterm, {.argv = spawn_term} },
2742 { "spawn_menu", legacyfunc, {0} },
2743 { "quit", quit, {0} },
2744 { "restart", restart, {0} },
2745 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
2746 { "ws_1", switchws, {.id = 0} },
2747 { "ws_2", switchws, {.id = 1} },
2748 { "ws_3", switchws, {.id = 2} },
2749 { "ws_4", switchws, {.id = 3} },
2750 { "ws_5", switchws, {.id = 4} },
2751 { "ws_6", switchws, {.id = 5} },
2752 { "ws_7", switchws, {.id = 6} },
2753 { "ws_8", switchws, {.id = 7} },
2754 { "ws_9", switchws, {.id = 8} },
2755 { "ws_10", switchws, {.id = 9} },
2756 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
2757 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
2758 { "ws_prior", priorws, {0} },
2759 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
2760 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
2761 { "mvws_1", send_to_ws, {.id = 0} },
2762 { "mvws_2", send_to_ws, {.id = 1} },
2763 { "mvws_3", send_to_ws, {.id = 2} },
2764 { "mvws_4", send_to_ws, {.id = 3} },
2765 { "mvws_5", send_to_ws, {.id = 4} },
2766 { "mvws_6", send_to_ws, {.id = 5} },
2767 { "mvws_7", send_to_ws, {.id = 6} },
2768 { "mvws_8", send_to_ws, {.id = 7} },
2769 { "mvws_9", send_to_ws, {.id = 8} },
2770 { "mvws_10", send_to_ws, {.id = 9} },
2771 { "bar_toggle", bar_toggle, {0} },
2772 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
2773 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
2774 { "screenshot_all", legacyfunc, {0} },
2775 { "screenshot_wind", legacyfunc, {0} },
2776 { "float_toggle", floating_toggle,{0} },
2777 { "version", version, {0} },
2778 { "spawn_lock", legacyfunc, {0} },
2779 { "spawn_initscr", legacyfunc, {0} },
2780 { "spawn_custom", dummykeyfunc, {0} },
2781 { "dumpwins", dumpwins, {0} },
2782 { "invalid key func", NULL, {0} },
2787 enum keyfuncid funcid;
2790 int keys_size = 0, keys_length = 0;
2791 struct key *keys = NULL;
2794 enum { client_click, root_click };
2796 unsigned int action;
2798 unsigned int button;
2799 void (*func)(struct ws_win *, union arg *);
2802 /* action key mouse button func args */
2803 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
2804 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
2805 { client_click, MODKEY, Button1, move, {0} },
2809 update_modkey(unsigned int mod)
2814 for (i = 0; i < keys_length; i++)
2815 if (keys[i].mod & ShiftMask)
2816 keys[i].mod = mod | ShiftMask;
2820 for (i = 0; i < LENGTH(buttons); i++)
2821 if (buttons[i].mask & ShiftMask)
2822 buttons[i].mask = mod | ShiftMask;
2824 buttons[i].mask = mod;
2834 int spawns_size = 0, spawns_length = 0;
2835 struct spawn_prog *spawns = NULL;
2838 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
2841 struct spawn_prog *prog = NULL;
2843 char *ap, **real_args;
2845 DNPRINTF(SWM_D_SPAWN, "spawn_custom %s\n", spawn_name);
2848 for (i = 0; i < spawns_length; i++) {
2849 if (!strcasecmp(spawn_name, spawns[i].name))
2853 fprintf(stderr, "spawn_custom: program %s not found\n",
2858 /* make room for expanded args */
2859 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
2860 err(1, "spawn_custom: calloc real_args");
2862 /* expand spawn_args into real_args */
2863 for (i = 0; i < prog->argc; i++) {
2865 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
2866 if (!strcasecmp(ap, "$bar_border")) {
2868 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
2870 err(1, "spawn_custom border color");
2871 } else if (!strcasecmp(ap, "$bar_color")) {
2873 strdup(r->s->c[SWM_S_COLOR_BAR].name))
2875 err(1, "spawn_custom bar color");
2876 } else if (!strcasecmp(ap, "$bar_font")) {
2877 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
2879 err(1, "spawn_custom bar fonts");
2880 } else if (!strcasecmp(ap, "$bar_font_color")) {
2882 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
2884 err(1, "spawn_custom color font");
2885 } else if (!strcasecmp(ap, "$color_focus")) {
2887 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
2889 err(1, "spawn_custom color focus");
2890 } else if (!strcasecmp(ap, "$color_unfocus")) {
2892 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
2894 err(1, "spawn_custom color unfocus");
2896 /* no match --> copy as is */
2897 if ((real_args[i] = strdup(ap)) == NULL)
2898 err(1, "spawn_custom strdup(ap)");
2900 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
2905 if ((swm_debug & SWM_D_SPAWN) != 0) {
2906 fprintf(stderr, "spawn_custom: result = ");
2907 for (i = 0; i < prog->argc; i++)
2908 fprintf(stderr, "\"%s\" ", real_args[i]);
2909 fprintf(stderr, "\n");
2915 for (i = 0; i < prog->argc; i++)
2921 setspawn(struct spawn_prog *prog)
2925 if (prog == NULL || prog->name == NULL)
2929 for (i = 0; i < spawns_length; i++) {
2930 if (!strcmp(spawns[i].name, prog->name)) {
2932 if (prog->argv == NULL) {
2934 DNPRINTF(SWM_D_SPAWN,
2935 "setspawn: delete #%d %s\n",
2937 free(spawns[i].name);
2938 for (j = 0; j < spawns[i].argc; j++)
2939 free(spawns[i].argv[j]);
2940 free(spawns[i].argv);
2941 j = spawns_length - 1;
2943 spawns[i] = spawns[j];
2948 DNPRINTF(SWM_D_SPAWN,
2949 "setspawn: replace #%d %s\n",
2951 free(spawns[i].name);
2952 for (j = 0; j < spawns[i].argc; j++)
2953 free(spawns[i].argv[j]);
2954 free(spawns[i].argv);
2957 /* found case handled */
2963 if (prog->argv == NULL) {
2965 "error: setspawn: cannot find program %s", prog->name);
2970 /* not found: add */
2971 if (spawns_size == 0 || spawns == NULL) {
2973 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
2974 spawns = malloc((size_t)spawns_size *
2975 sizeof(struct spawn_prog));
2976 if (spawns == NULL) {
2977 fprintf(stderr, "setspawn: malloc failed\n");
2981 } else if (spawns_length == spawns_size) {
2983 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
2984 spawns = realloc(spawns, (size_t)spawns_size *
2985 sizeof(struct spawn_prog));
2986 if (spawns == NULL) {
2987 fprintf(stderr, "setspawn: realloc failed\n");
2993 if (spawns_length < spawns_size) {
2994 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
2995 spawns_length, prog->name);
2996 i = spawns_length++;
2999 fprintf(stderr, "spawns array problem?\n");
3000 if (spawns == NULL) {
3001 fprintf(stderr, "spawns array is NULL!\n");
3009 setconfspawn(char *selector, char *value, int flags)
3011 struct spawn_prog *prog;
3012 char *vp, *cp, *word;
3014 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
3015 if ((prog = calloc(1, sizeof *prog)) == NULL)
3016 err(1, "setconfspawn: calloc prog");
3017 prog->name = strdup(selector);
3018 if (prog->name == NULL)
3019 err(1, "setconfspawn prog->name");
3020 if ((cp = vp = strdup(value)) == NULL)
3021 err(1, "setconfspawn: strdup(value) ");
3022 while ((word = strsep(&cp, " \t")) != NULL) {
3023 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
3025 cp += (long)strspn(cp, " \t");
3026 if (strlen(word) > 0) {
3028 prog->argv = realloc(prog->argv,
3029 prog->argc * sizeof(char *));
3030 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
3031 err(1, "setconfspawn: strdup");
3038 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
3045 setconfspawn("term", "xterm", 0);
3046 setconfspawn("screenshot_all", "screenshot.sh full", 0);
3047 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
3048 setconfspawn("lock", "xlock", 0);
3049 setconfspawn("initscr", "initscreen.sh", 0);
3050 setconfspawn("menu", "dmenu_run"
3053 " -nf $bar_font_color"
3055 " -sf $bar_color", 0);
3059 #define SWM_MODNAME_SIZE 32
3060 #define SWM_KEY_WS "\n+ \t"
3062 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
3066 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
3067 if (mod == NULL || ks == NULL) {
3068 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
3071 if (keystr == NULL || strlen(keystr) == 0) {
3072 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
3078 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
3079 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
3081 cp += (long)strspn(cp, SWM_KEY_WS);
3082 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
3084 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
3086 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
3088 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
3090 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
3092 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
3094 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
3095 *mod |= ControlMask;
3097 *ks = XStringToKeysym(name);
3098 XConvertCase(*ks, ks, &uks);
3099 if (ks == NoSymbol) {
3101 "parsekeys: invalid key %s\n",
3107 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
3112 strdupsafe(char *str)
3117 return (strdup(str));
3121 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
3124 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
3125 keyfuncs[kfid].name, spawn_name);
3127 for (i = 0; i < keys_length; i++) {
3128 if (keys[i].mod == mod && keys[i].keysym == ks) {
3129 if (kfid == kf_invalid) {
3132 "setkeybinding: delete #%d %s\n",
3133 i, keyfuncs[keys[i].funcid].name);
3134 free(keys[i].spawn_name);
3135 j = keys_length - 1;
3139 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3142 /* found: replace */
3144 "setkeybinding: replace #%d %s %s\n",
3145 i, keyfuncs[keys[i].funcid].name,
3147 free(keys[i].spawn_name);
3149 keys[i].keysym = ks;
3150 keys[i].funcid = kfid;
3151 keys[i].spawn_name = strdupsafe(spawn_name);
3152 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3157 if (kfid == kf_invalid) {
3159 "error: setkeybinding: cannot find mod/key combination");
3160 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3163 /* not found: add */
3164 if (keys_size == 0 || keys == NULL) {
3166 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
3167 keys = malloc((size_t)keys_size * sizeof(struct key));
3169 fprintf(stderr, "malloc failed\n");
3173 } else if (keys_length == keys_size) {
3175 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
3176 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
3178 fprintf(stderr, "realloc failed\n");
3183 if (keys_length < keys_size) {
3185 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
3186 j, keyfuncs[kfid].name, spawn_name);
3188 keys[j].keysym = ks;
3189 keys[j].funcid = kfid;
3190 keys[j].spawn_name = strdupsafe(spawn_name);
3192 fprintf(stderr, "keys array problem?\n");
3194 fprintf(stderr, "keys array problem\n");
3198 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3202 setconfbinding(char *selector, char *value, int flags)
3204 enum keyfuncid kfid;
3208 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
3209 if (selector == NULL) {
3210 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
3211 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3213 setkeybinding(mod, ks, kfid, NULL);
3218 /* search by key function name */
3219 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
3220 if (strncasecmp(selector, keyfuncs[kfid].name,
3221 SWM_FUNCNAME_LEN) == 0) {
3222 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3224 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3225 setkeybinding(mod, ks, kfid, NULL);
3231 /* search by custom spawn name */
3232 for (i = 0; i < spawns_length; i++) {
3233 if (strcasecmp(selector, spawns[i].name) == 0) {
3234 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3236 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3237 setkeybinding(mod, ks, kf_spawn_custom,
3244 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
3251 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
3252 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
3253 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
3254 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
3255 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
3256 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
3257 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
3258 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
3259 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
3260 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
3261 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
3262 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
3263 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
3264 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
3265 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
3266 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
3267 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
3268 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
3269 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
3270 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
3271 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
3272 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
3273 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
3274 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
3275 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
3276 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
3277 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
3278 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
3279 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
3280 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
3281 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
3282 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
3283 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
3284 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
3285 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
3286 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
3287 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
3288 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
3289 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
3290 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
3291 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
3292 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
3293 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
3294 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
3295 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
3296 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
3297 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
3298 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
3299 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
3300 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
3301 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
3302 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
3303 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
3304 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
3306 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
3311 updatenumlockmask(void)
3314 XModifierKeymap *modmap;
3316 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
3318 modmap = XGetModifierMapping(display);
3319 for (i = 0; i < 8; i++)
3320 for (j = 0; j < modmap->max_keypermod; j++)
3321 if (modmap->modifiermap[i * modmap->max_keypermod + j]
3322 == XKeysymToKeycode(display, XK_Num_Lock))
3323 numlockmask = (1 << i);
3325 XFreeModifiermap(modmap);
3331 unsigned int i, j, k;
3333 unsigned int modifiers[] =
3334 { 0, LockMask, numlockmask, numlockmask | LockMask };
3336 DNPRINTF(SWM_D_MISC, "grabkeys\n");
3337 updatenumlockmask();
3339 for (k = 0; k < ScreenCount(display); k++) {
3340 if (TAILQ_EMPTY(&screens[k].rl))
3342 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
3343 for (i = 0; i < keys_length; i++) {
3344 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
3345 for (j = 0; j < LENGTH(modifiers); j++)
3346 XGrabKey(display, code,
3347 keys[i].mod | modifiers[j],
3348 screens[k].root, True,
3349 GrabModeAsync, GrabModeAsync);
3355 grabbuttons(struct ws_win *win, int focused)
3358 unsigned int modifiers[] =
3359 { 0, LockMask, numlockmask, numlockmask|LockMask };
3361 updatenumlockmask();
3362 XUngrabButton(display, AnyButton, AnyModifier, win->id);
3364 for (i = 0; i < LENGTH(buttons); i++)
3365 if (buttons[i].action == client_click)
3366 for (j = 0; j < LENGTH(modifiers); j++)
3367 XGrabButton(display, buttons[i].button,
3368 buttons[i].mask | modifiers[j],
3369 win->id, False, BUTTONMASK,
3370 GrabModeAsync, GrabModeSync, None,
3373 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
3374 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
3377 const char *quirkname[] = {
3378 "NONE", /* config string for "no value" */
3386 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
3387 #define SWM_Q_WS "\n|+ \t"
3389 parsequirks(char *qstr, unsigned long *quirk)
3399 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
3401 cp += (long)strspn(cp, SWM_Q_WS);
3402 for (i = 0; i < LENGTH(quirkname); i++) {
3403 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
3404 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
3409 *quirk |= 1 << (i-1);
3413 if (i >= LENGTH(quirkname)) {
3414 DNPRINTF(SWM_D_QUIRK,
3415 "parsequirks: invalid quirk [%s]\n", name);
3423 setquirk(const char *class, const char *name, const int quirk)
3428 for (i = 0; i < quirks_length; i++) {
3429 if (!strcmp(quirks[i].class, class) &&
3430 !strcmp(quirks[i].name, name)) {
3433 DNPRINTF(SWM_D_QUIRK,
3434 "setquirk: delete #%d %s:%s\n",
3435 i, quirks[i].class, quirks[i].name);
3436 free(quirks[i].class);
3437 free(quirks[i].name);
3438 j = quirks_length - 1;
3440 quirks[i] = quirks[j];
3444 /* found: replace */
3445 DNPRINTF(SWM_D_QUIRK,
3446 "setquirk: replace #%d %s:%s\n",
3447 i, quirks[i].class, quirks[i].name);
3448 free(quirks[i].class);
3449 free(quirks[i].name);
3450 quirks[i].class = strdup(class);
3451 quirks[i].name = strdup(name);
3452 quirks[i].quirk = quirk;
3459 "error: setquirk: cannot find class/name combination");
3462 /* not found: add */
3463 if (quirks_size == 0 || quirks == NULL) {
3465 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
3466 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
3468 fprintf(stderr, "setquirk: malloc failed\n");
3472 } else if (quirks_length == quirks_size) {
3474 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
3475 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
3477 fprintf(stderr, "setquirk: realloc failed\n");
3482 if (quirks_length < quirks_size) {
3483 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
3484 j = quirks_length++;
3485 quirks[j].class = strdup(class);
3486 quirks[j].name = strdup(name);
3487 quirks[j].quirk = quirk;
3489 fprintf(stderr, "quirks array problem?\n");
3491 fprintf(stderr, "quirks array problem!\n");
3498 setconfquirk(char *selector, char *value, int flags)
3500 char *cp, *class, *name;
3502 unsigned long quirks;
3503 if (selector == NULL)
3505 if ((cp = strchr(selector, ':')) == NULL)
3510 if ((retval = parsequirks(value, &quirks)) == 0)
3511 setquirk(class, name, quirks);
3518 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
3519 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
3520 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
3521 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
3522 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3523 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
3524 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3525 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3526 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3527 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3528 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
3529 setquirk("pcb", "pcb", SWM_Q_FLOAT);
3532 /* conf file stuff */
3533 #define SWM_CONF_FILE "scrotwm.conf"
3535 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_STACK_ENABLED,
3536 SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT, SWM_S_CYCLE_EMPTY,
3537 SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH,
3538 SWM_S_TITLE_CLASS_ENABLED, SWM_S_TITLE_NAME_ENABLED,
3539 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BAR_FONT,
3540 SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM, SWM_S_SS_APP, SWM_S_DIALOG_RATIO
3544 setconfvalue(char *selector, char *value, int flags)
3547 case SWM_S_BAR_DELAY:
3548 bar_delay = atoi(value);
3550 case SWM_S_BAR_ENABLED:
3551 bar_enabled = atoi(value);
3553 case SWM_S_STACK_ENABLED:
3554 stack_enabled = atoi(value);
3556 case SWM_S_CLOCK_ENABLED:
3557 clock_enabled = atoi(value);
3559 case SWM_S_CLOCK_FORMAT:
3560 #ifndef SWM_DENY_CLOCK_FORMAT
3562 if ((clock_format = strdup(value)) == NULL)
3563 err(1, "setconfvalue: clock_format");
3566 case SWM_S_CYCLE_EMPTY:
3567 cycle_empty = atoi(value);
3569 case SWM_S_CYCLE_VISIBLE:
3570 cycle_visible = atoi(value);
3572 case SWM_S_SS_ENABLED:
3573 ss_enabled = atoi(value);
3575 case SWM_S_TERM_WIDTH:
3576 term_width = atoi(value);
3578 case SWM_S_TITLE_CLASS_ENABLED:
3579 title_class_enabled = atoi(value);
3581 case SWM_S_TITLE_NAME_ENABLED:
3582 title_name_enabled = atoi(value);
3584 case SWM_S_FOCUS_MODE:
3585 if (!strcmp(value, "default"))
3586 focus_mode = SWM_FOCUS_DEFAULT;
3587 else if (!strcmp(value, "follow_cursor"))
3588 focus_mode = SWM_FOCUS_FOLLOW;
3589 else if (!strcmp(value, "synergy"))
3590 focus_mode = SWM_FOCUS_SYNERGY;
3592 err(1, "focus_mode");
3594 case SWM_S_DISABLE_BORDER:
3595 disable_border = atoi(value);
3597 case SWM_S_BAR_FONT:
3599 if ((bar_fonts[0] = strdup(value)) == NULL)
3600 err(1, "setconfvalue: bar_font");
3602 case SWM_S_BAR_ACTION:
3604 if ((bar_argv[0] = strdup(value)) == NULL)
3605 err(1, "setconfvalue: bar_action");
3607 case SWM_S_SPAWN_TERM:
3608 free(spawn_term[0]);
3609 if ((spawn_term[0] = strdup(value)) == NULL)
3610 err(1, "setconfvalue: spawn_term");
3614 case SWM_S_DIALOG_RATIO:
3615 dialog_ratio = atof(value);
3616 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
3626 setconfmodkey(char *selector, char *value, int flags)
3628 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
3629 update_modkey(Mod1Mask);
3630 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
3631 update_modkey(Mod2Mask);
3632 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
3633 update_modkey(Mod3Mask);
3634 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
3635 update_modkey(Mod4Mask);
3642 setconfcolor(char *selector, char *value, int flags)
3644 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
3649 setconfregion(char *selector, char *value, int flags)
3651 custom_region(value);
3655 /* config options */
3656 struct config_option {
3658 int (*func)(char*, char*, int);
3661 struct config_option configopt[] = {
3662 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
3663 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
3664 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
3665 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
3666 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
3667 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
3668 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
3669 { "bind", setconfbinding, 0 },
3670 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
3671 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
3672 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
3673 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
3674 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
3675 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
3676 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
3677 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
3678 { "modkey", setconfmodkey, 0 },
3679 { "program", setconfspawn, 0 },
3680 { "quirk", setconfquirk, 0 },
3681 { "region", setconfregion, 0 },
3682 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
3683 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
3684 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
3685 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
3686 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
3687 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
3688 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
3689 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
3694 conf_load(char *filename)
3697 char *line, *cp, *optsub, *optval;
3698 size_t linelen, lineno = 0;
3699 int wordlen, i, optind;
3700 struct config_option *opt;
3702 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
3704 if (filename == NULL) {
3705 fprintf(stderr, "conf_load: no filename\n");
3708 if ((config = fopen(filename, "r")) == NULL) {
3709 warn("conf_load: fopen");
3713 while (!feof(config)) {
3714 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
3717 err(1, "%s", filename);
3722 cp += strspn(cp, " \t\n"); /* eat whitespace */
3723 if (cp[0] == '\0') {
3728 /* get config option */
3729 wordlen = strcspn(cp, "=[ \t\n");
3731 warnx("%s: line %zd: no option found",
3736 for (i = 0; i < LENGTH(configopt); i++) {
3737 opt = &configopt[i];
3738 if (!strncasecmp(cp, opt->optname, wordlen) &&
3739 strlen(opt->optname) == wordlen) {
3745 warnx("%s: line %zd: unknown option %.*s",
3746 filename, lineno, wordlen, cp);
3750 cp += strspn(cp, " \t\n"); /* eat whitespace */
3751 /* get [selector] if any */
3755 wordlen = strcspn(cp, "]");
3758 warnx("%s: line %zd: syntax error",
3762 asprintf(&optsub, "%.*s", wordlen, cp);
3765 cp += strspn(cp, "] \t\n"); /* eat trailing */
3767 cp += strspn(cp, "= \t\n"); /* eat trailing */
3769 optval = strdup(cp);
3770 /* call function to deal with it all */
3771 if (configopt[optind].func(optsub, optval,
3772 configopt[optind].funcflags) != 0) {
3773 fprintf(stderr, "%s line %zd: %s\n",
3774 filename, lineno, line);
3775 errx(1, "%s: line %zd: invalid data for %s",
3776 filename, lineno, configopt[optind].optname);
3784 DNPRINTF(SWM_D_CONF, "conf_load end\n");
3790 set_child_transient(struct ws_win *win)
3792 struct ws_win *parent;
3794 parent = find_window(win->transient);
3796 parent->child_trans = win;
3800 manage_window(Window id)
3803 struct workspace *ws;
3804 struct ws_win *win, *ww;
3805 int format, i, ws_idx, n, border_me = 0;
3806 unsigned long nitems, bytes;
3807 Atom ws_idx_atom = 0, type;
3808 Atom *prot = NULL, *pp;
3809 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
3810 struct swm_region *r;
3815 if ((win = find_window(id)) != NULL)
3816 return (win); /* already being managed */
3818 /* see if we are on the unmanaged list */
3819 if ((win = find_unmanaged_window(id)) != NULL) {
3820 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
3822 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
3823 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
3825 set_child_transient(win);
3829 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
3830 errx(1, "calloc: failed to allocate memory for new window");
3832 /* Get all the window data in one shot */
3833 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3835 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
3836 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
3837 XGetWindowAttributes(display, id, &win->wa);
3838 XGetWMNormalHints(display, id, &win->sh, &mask);
3839 XGetTransientForHint(display, id, &trans);
3841 win->transient = trans;
3842 set_child_transient(win);
3843 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
3844 win->id, win->transient);
3846 /* get supported protocols */
3847 if (XGetWMProtocols(display, id, &prot, &n)) {
3848 for (i = 0, pp = prot; i < n; i++, pp++) {
3849 if (*pp == takefocus)
3850 win->take_focus = 1;
3852 win->can_delete = 1;
3859 * Figure out where to put the window. If it was previously assigned to
3860 * a workspace (either by spawn() or manually moving), and isn't
3861 * transient, * put it in the same workspace
3863 r = root_to_region(win->wa.root);
3864 if (prop && win->transient == 0) {
3865 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
3866 ws_idx = strtonum(prop, 0, 9, &errstr);
3868 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
3871 ws = &r->s->ws[ws_idx];
3874 /* this should launch transients in the same ws as parent */
3876 if ((ww = find_window(trans)) != NULL)
3883 "fix this bug mcbride\n");
3888 /* set up the window layout */
3891 win->s = r->s; /* this never changes */
3892 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
3894 win->g.w = win->wa.width;
3895 win->g.h = win->wa.height;
3896 win->g.x = win->wa.x;
3897 win->g.y = win->wa.y;
3898 win->g_floatvalid = 0;
3899 win->floatmaxed = 0;
3901 /* Set window properties so we can remember this after reincarnation */
3902 if (ws_idx_atom && prop == NULL &&
3903 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
3904 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3906 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3907 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3911 if (XGetClassHint(display, win->id, &win->ch)) {
3912 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
3913 win->ch.res_class, win->ch.res_name);
3915 /* java is retarded so treat it special */
3916 if (strstr(win->ch.res_name, "sun-awt")) {
3921 for (i = 0; i < quirks_length; i++){
3922 if (!strcmp(win->ch.res_class, quirks[i].class) &&
3923 !strcmp(win->ch.res_name, quirks[i].name)) {
3924 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
3925 win->ch.res_class, win->ch.res_name);
3926 if (quirks[i].quirk & SWM_Q_FLOAT) {
3930 win->quirks = quirks[i].quirk;
3935 /* alter window position if quirky */
3936 if (win->quirks & SWM_Q_ANYWHERE) {
3937 win->manual = 1; /* don't center the quirky windows */
3938 bzero(&wc, sizeof wc);
3940 if (win->g.y < bar_height) {
3941 win->g.y = wc.y = bar_height;
3944 if (win->g.w + win->g.x > WIDTH(r)) {
3945 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
3951 /* Reset font sizes (the bruteforce way; no default keybinding). */
3952 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
3953 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3954 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3955 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3956 fake_keypress(win, XK_KP_Add, ShiftMask);
3961 bzero(&wc, sizeof wc);
3962 wc.border_width = 1;
3963 mask = CWBorderWidth;
3964 XConfigureWindow(display, win->id, mask, &wc);
3968 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
3969 PropertyChangeMask | StructureNotifyMask);
3971 set_win_state(win, NormalState);
3973 /* floaters need to be mapped if they are in the current workspace */
3974 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
3975 XMapRaised(display, win->id);
3981 free_window(struct ws_win *win)
3983 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
3988 /* needed for restart wm */
3989 set_win_state(win, WithdrawnState);
3991 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
3993 if (win->ch.res_class)
3994 XFree(win->ch.res_class);
3995 if (win->ch.res_name)
3996 XFree(win->ch.res_name);
4001 memset(win, 0xff, sizeof *win); /* XXX kill later */
4007 unmanage_window(struct ws_win *win)
4009 struct ws_win *parent;
4014 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
4016 if (win->transient) {
4017 parent = find_window(win->transient);
4019 parent->child_trans = NULL;
4022 /* work around for mplayer going full screen */
4026 TAILQ_REMOVE(&win->ws->winlist, win, entry);
4027 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
4033 focus_magic(struct ws_win *win, int do_trans)
4035 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu %d\n", WINID(win), do_trans);
4040 if (do_trans == SWM_F_TRANSIENT && win->child_trans) {
4041 /* win = parent & has a transient so focus on that */
4043 focus_win(win->child_trans);
4044 if (win->child_trans->take_focus)
4045 client_msg(win, takefocus);
4047 focus_win(win->child_trans);
4048 if (win->child_trans->take_focus)
4049 client_msg(win->child_trans, takefocus);
4054 if (win->take_focus)
4055 client_msg(win, takefocus);
4062 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
4070 XKeyEvent *ev = &e->xkey;
4072 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
4074 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
4075 for (i = 0; i < keys_length; i++)
4076 if (keysym == keys[i].keysym
4077 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
4078 && keyfuncs[keys[i].funcid].func) {
4079 if (keys[i].funcid == kf_spawn_custom)
4081 root_to_region(ev->root),
4082 &(keyfuncs[keys[i].funcid].args),
4086 keyfuncs[keys[i].funcid].func(
4087 root_to_region(ev->root),
4088 &(keyfuncs[keys[i].funcid].args)
4094 buttonpress(XEvent *e)
4098 XButtonPressedEvent *ev = &e->xbutton;
4100 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
4102 action = root_click;
4103 if ((win = find_window(ev->window)) == NULL)
4106 focus_magic(win, SWM_F_TRANSIENT);
4107 action = client_click;
4109 for (i = 0; i < LENGTH(buttons); i++)
4110 if (action == buttons[i].action && buttons[i].func &&
4111 buttons[i].button == ev->button &&
4112 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
4113 buttons[i].func(win, &buttons[i].args);
4117 configurerequest(XEvent *e)
4119 XConfigureRequestEvent *ev = &e->xconfigurerequest;
4124 if ((win = find_window(ev->window)) == NULL)
4125 if ((win = find_unmanaged_window(ev->window)) == NULL)
4129 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
4131 bzero(&wc, sizeof wc);
4134 wc.width = ev->width;
4135 wc.height = ev->height;
4136 wc.border_width = ev->border_width;
4137 wc.sibling = ev->above;
4138 wc.stack_mode = ev->detail;
4139 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
4141 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
4143 if (win->floating) {
4144 if (ev->value_mask & CWX)
4146 if (ev->value_mask & CWY)
4148 if (ev->value_mask & CWWidth)
4149 win->g.w = ev->width;
4150 if (ev->value_mask & CWHeight)
4151 win->g.h = ev->height;
4158 configurenotify(XEvent *e)
4163 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
4164 e->xconfigure.window);
4166 win = find_window(e->xconfigure.window);
4168 XGetWMNormalHints(display, win->id, &win->sh, &mask);
4176 destroynotify(XEvent *e)
4179 XDestroyWindowEvent *ev = &e->xdestroywindow;
4181 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
4183 if ((win = find_window(ev->window)) == NULL) {
4184 if ((win = find_unmanaged_window(ev->window)) == NULL)
4190 /* make sure we focus on something */
4193 unmanage_window(win);
4199 enternotify(XEvent *e)
4201 XCrossingEvent *ev = &e->xcrossing;
4206 Window focus_return;
4207 int revert_to_return;
4209 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
4210 "%lu subwindow %lu same_screen %d focus %d state %d\n",
4211 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
4212 ev->same_screen, ev->focus, ev->state);
4214 switch (focus_mode) {
4215 case SWM_FOCUS_DEFAULT:
4216 if (QLength(display)) {
4217 DNPRINTF(SWM_D_EVENT, "ignore enternotify %d\n",
4222 case SWM_FOCUS_FOLLOW:
4224 case SWM_FOCUS_SYNERGY:
4227 * all these checks need to be in this order because the
4228 * XCheckTypedWindowEvent relies on weeding out the previous events
4230 * making this code an option would enable a follow mouse for focus
4235 * state is set when we are switching workspaces and focus is set when
4236 * the window or a subwindow already has focus (occurs during restart).
4238 * Only honor the focus flag if last_focus_event is not FocusOut,
4239 * this allows scrotwm to continue to control focus when another
4240 * program is also playing with it.
4242 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
4243 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
4248 * happens when a window is created or destroyed and the border
4249 * crosses the mouse pointer and when switching ws
4251 * we need the subwindow test to see if we came from root in order
4252 * to give focus to floaters
4254 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
4255 ev->subwindow == 0) {
4256 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
4260 /* this window already has focus */
4261 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
4262 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
4266 /* this window is being deleted or moved to another ws */
4267 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
4269 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
4270 XPutBackEvent(display, &cne);
4274 if ((win = find_window(ev->window)) == NULL) {
4275 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
4280 * In fullstack kill all enters unless they come from a different ws
4281 * (i.e. another region) or focus has been grabbed externally.
4283 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
4284 last_focus_event != FocusOut) {
4285 XGetInputFocus(display, &focus_return, &revert_to_return);
4286 if ((w = find_window(focus_return)) == NULL ||
4288 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
4296 if ((win = find_window(ev->window)) == NULL) {
4297 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
4302 * if we have more enternotifies let them handle it in due time
4304 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
4305 DNPRINTF(SWM_D_EVENT,
4306 "ignoring enternotify: got more enternotify\n");
4307 XPutBackEvent(display, &cne);
4311 focus_magic(win, SWM_F_TRANSIENT);
4314 /* lets us use one switch statement for arbitrary mode/detail combinations */
4315 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
4318 focusevent(XEvent *e)
4322 u_int32_t mode_detail;
4323 XFocusChangeEvent *ev = &e->xfocus;
4325 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
4326 ev->type == FocusIn ? "entering" : "leaving",
4327 ev->window, ev->mode, ev->detail);
4329 if (last_focus_event == ev->type) {
4330 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
4334 last_focus_event = ev->type;
4335 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
4337 switch (mode_detail) {
4338 /* synergy client focus operations */
4339 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
4340 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
4342 /* synergy server focus operations */
4343 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
4345 /* Entering applications like rdesktop that mangle the pointer */
4346 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
4348 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
4349 XSetWindowBorder(display, win->id,
4350 win->ws->r->s->c[ev->type == FocusIn ?
4351 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
4354 fprintf(stderr, "ignoring focusevent\n");
4355 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
4362 mapnotify(XEvent *e)
4365 XMapEvent *ev = &e->xmap;
4367 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
4369 win = manage_window(ev->window);
4371 set_win_state(win, NormalState);
4375 mappingnotify(XEvent *e)
4377 XMappingEvent *ev = &e->xmapping;
4379 XRefreshKeyboardMapping(ev);
4380 if (ev->request == MappingKeyboard)
4385 maprequest(XEvent *e)
4388 struct swm_region *r;
4389 XWindowAttributes wa;
4390 XMapRequestEvent *ev = &e->xmaprequest;
4392 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
4393 e->xmaprequest.window);
4395 if (!XGetWindowAttributes(display, ev->window, &wa))
4397 if (wa.override_redirect)
4400 win = manage_window(e->xmaprequest.window);
4402 return; /* can't happen */
4406 /* make new win focused */
4407 r = root_to_region(win->wa.root);
4408 if (win->ws == r->ws)
4409 focus_magic(win, SWM_F_GENERIC);
4413 propertynotify(XEvent *e)
4416 XPropertyEvent *ev = &e->xproperty;
4418 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
4421 if (ev->state == PropertyDelete)
4422 return; /* ignore */
4423 win = find_window(ev->window);
4428 case XA_WM_NORMAL_HINTS:
4431 XGetWMNormalHints(display, win->id, &win->sh, &mask);
4432 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
4433 if (win->sh.flags & PMinSize) {
4434 win->g.w = win->sh.min_width;
4435 win->g.h = win->sh.min_height;
4436 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
4438 XMoveResizeWindow(display, win->id,
4439 win->g.x, win->g.y, win->g.w, win->g.h);
4448 unmapnotify(XEvent *e)
4452 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
4454 /* determine if we need to help unmanage this window */
4455 win = find_window(e->xunmap.window);
4459 if (getstate(e->xunmap.window) == NormalState) {
4460 unmanage_window(win);
4466 visibilitynotify(XEvent *e)
4469 struct swm_region *r;
4471 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
4472 e->xvisibility.window);
4473 if (e->xvisibility.state == VisibilityUnobscured)
4474 for (i = 0; i < ScreenCount(display); i++)
4475 TAILQ_FOREACH(r, &screens[i].rl, entry)
4476 if (e->xvisibility.window == r->bar_window)
4481 xerror_start(Display *d, XErrorEvent *ee)
4488 xerror(Display *d, XErrorEvent *ee)
4490 /* fprintf(stderr, "error: %p %p\n", display, ee); */
4498 xerrorxlib = XSetErrorHandler(xerror_start);
4500 /* this causes an error if some other window manager is running */
4501 XSelectInput(display, DefaultRootWindow(display),
4502 SubstructureRedirectMask);
4503 XSync(display, False);
4507 XSetErrorHandler(xerror);
4508 XSync(display, False);
4513 new_region(struct swm_screen *s, int x, int y, int w, int h)
4515 struct swm_region *r, *n;
4516 struct workspace *ws = NULL;
4519 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
4520 s->idx, w, h, x, y);
4522 /* remove any conflicting regions */
4523 n = TAILQ_FIRST(&s->rl);
4526 n = TAILQ_NEXT(r, entry);
4527 if (X(r) < (x + w) &&
4528 (X(r) + WIDTH(r)) > x &&
4530 (Y(r) + HEIGHT(r)) > y) {
4531 if (r->ws->r != NULL)
4532 r->ws->old_r = r->ws->r;
4534 XDestroyWindow(display, r->bar_window);
4535 TAILQ_REMOVE(&s->rl, r, entry);
4536 TAILQ_INSERT_TAIL(&s->orl, r, entry);
4540 /* search old regions for one to reuse */
4542 /* size + location match */
4543 TAILQ_FOREACH(r, &s->orl, entry)
4544 if (X(r) == x && Y(r) == y &&
4545 HEIGHT(r) == h && WIDTH(r) == w)
4549 TAILQ_FOREACH(r, &s->orl, entry)
4550 if (HEIGHT(r) == h && WIDTH(r) == w)
4554 TAILQ_REMOVE(&s->orl, r, entry);
4555 /* try to use old region's workspace */
4556 if (r->ws->r == NULL)
4559 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
4560 errx(1, "calloc: failed to allocate memory for screen");
4562 /* if we don't have a workspace already, find one */
4564 for (i = 0; i < SWM_WS_MAX; i++)
4565 if (s->ws[i].r == NULL) {
4572 errx(1, "no free workspaces\n");
4583 TAILQ_INSERT_TAIL(&s->rl, r, entry);
4589 #ifdef SWM_XRR_HAS_CRTC
4591 XRRScreenResources *sr;
4594 #endif /* SWM_XRR_HAS_CRTC */
4595 struct swm_region *r;
4598 if (i >= ScreenCount(display))
4599 errx(1, "invalid screen");
4601 /* remove any old regions */
4602 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
4603 r->ws->old_r = r->ws->r = NULL;
4604 XDestroyWindow(display, r->bar_window);
4605 TAILQ_REMOVE(&screens[i].rl, r, entry);
4606 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
4610 /* map virtual screens onto physical screens */
4611 #ifdef SWM_XRR_HAS_CRTC
4612 if (xrandr_support) {
4613 sr = XRRGetScreenResources(display, screens[i].root);
4615 new_region(&screens[i], 0, 0,
4616 DisplayWidth(display, i),
4617 DisplayHeight(display, i));
4621 for (c = 0, ci = NULL; c < ncrtc; c++) {
4622 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
4623 if (ci->noutput == 0)
4626 if (ci != NULL && ci->mode == None)
4627 new_region(&screens[i], 0, 0,
4628 DisplayWidth(display, i),
4629 DisplayHeight(display, i));
4631 new_region(&screens[i],
4632 ci->x, ci->y, ci->width, ci->height);
4635 XRRFreeCrtcInfo(ci);
4636 XRRFreeScreenResources(sr);
4638 #endif /* SWM_XRR_HAS_CRTC */
4640 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
4641 DisplayHeight(display, i));
4646 screenchange(XEvent *e) {
4647 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
4648 struct swm_region *r;
4651 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
4653 if (!XRRUpdateConfiguration(e))
4656 /* silly event doesn't include the screen index */
4657 for (i = 0; i < ScreenCount(display); i++)
4658 if (screens[i].root == xe->root)
4660 if (i >= ScreenCount(display))
4661 errx(1, "screenchange: screen not found\n");
4663 /* brute force for now, just re-enumerate the regions */
4666 /* add bars to all regions */
4667 for (i = 0; i < ScreenCount(display); i++)
4668 TAILQ_FOREACH(r, &screens[i].rl, entry)
4676 Window d1, d2, *wins = NULL;
4677 XWindowAttributes wa;
4680 int errorbase, major, minor;
4681 struct workspace *ws;
4685 if ((screens = calloc(ScreenCount(display),
4686 sizeof(struct swm_screen))) == NULL)
4687 errx(1, "calloc: screens");
4689 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4691 /* initial Xrandr setup */
4692 xrandr_support = XRRQueryExtension(display,
4693 &xrandr_eventbase, &errorbase);
4695 if (XRRQueryVersion(display, &major, &minor) && major < 1)
4698 /* map physical screens */
4699 for (i = 0; i < ScreenCount(display); i++) {
4700 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
4702 TAILQ_INIT(&screens[i].rl);
4703 TAILQ_INIT(&screens[i].orl);
4704 screens[i].root = RootWindow(display, i);
4706 /* set default colors */
4707 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
4708 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
4709 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
4710 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
4711 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
4713 /* set default cursor */
4714 XDefineCursor(display, screens[i].root,
4715 XCreateFontCursor(display, XC_left_ptr));
4717 /* init all workspaces */
4718 /* XXX these should be dynamically allocated too */
4719 for (j = 0; j < SWM_WS_MAX; j++) {
4720 ws = &screens[i].ws[j];
4725 TAILQ_INIT(&ws->winlist);
4726 TAILQ_INIT(&ws->unmanagedlist);
4728 for (k = 0; layouts[k].l_stack != NULL; k++)
4729 if (layouts[k].l_config != NULL)
4730 layouts[k].l_config(ws,
4731 SWM_ARG_ID_STACKINIT);
4732 ws->cur_layout = &layouts[0];
4734 /* grab existing windows (before we build the bars)*/
4735 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
4741 XRRSelectInput(display, screens[i].root,
4742 RRScreenChangeNotifyMask);
4744 /* attach windows to a region */
4745 /* normal windows */
4746 for (j = 0; j < no; j++) {
4747 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4748 wa.override_redirect ||
4749 XGetTransientForHint(display, wins[j], &d1))
4752 state = getstate(wins[j]);
4753 manage = state == IconicState;
4754 if (wa.map_state == IsViewable || manage)
4755 manage_window(wins[j]);
4757 /* transient windows */
4758 for (j = 0; j < no; j++) {
4759 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4760 wa.override_redirect)
4763 state = getstate(wins[j]);
4764 manage = state == IconicState;
4765 if (XGetTransientForHint(display, wins[j], &d1) &&
4767 manage_window(wins[j]);
4779 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
4781 err(1, "setup_globals: strdup");
4782 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
4784 err(1, "setup_globals: strdup");
4785 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
4787 err(1, "setup_globals: strdup");
4788 if ((spawn_term[0] = strdup("xterm")) == NULL)
4789 err(1, "setup_globals: strdup");
4790 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
4798 Atom netwmcheck, netwmname, utf8_string;
4801 /* work around sun jdk bugs, code from wmname */
4802 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
4803 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
4804 utf8_string = XInternAtom(display, "UTF8_STRING", False);
4805 for (i = 0; i < ScreenCount(display); i++) {
4806 root = screens[i].root;
4807 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
4808 PropModeReplace, (unsigned char *)&root, 1);
4809 XChangeProperty(display, root, netwmname, utf8_string, 8,
4810 PropModeReplace, "LG3D", strlen("LG3D"));
4815 main(int argc, char *argv[])
4818 struct swm_region *r, *rr;
4819 struct ws_win *winfocus = NULL;
4822 char conf[PATH_MAX], *cfile = NULL;
4827 struct sigaction sact;
4830 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
4831 SWM_VERSION, cvstag);
4832 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
4833 warnx("no locale support");
4835 if (!(display = XOpenDisplay(0)))
4836 errx(1, "can not open display");
4839 errx(1, "other wm running");
4841 /* handle some signals */
4842 bzero(&sact, sizeof(sact));
4843 sigemptyset(&sact.sa_mask);
4845 sact.sa_handler = sighdlr;
4846 sigaction(SIGINT, &sact, NULL);
4847 sigaction(SIGQUIT, &sact, NULL);
4848 sigaction(SIGTERM, &sact, NULL);
4849 sigaction(SIGHUP, &sact, NULL);
4851 sact.sa_handler = sighdlr;
4852 sact.sa_flags = SA_NOCLDSTOP;
4853 sigaction(SIGCHLD, &sact, NULL);
4855 astate = XInternAtom(display, "WM_STATE", False);
4856 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
4857 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
4858 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
4860 /* look for local and global conf file */
4861 pwd = getpwuid(getuid());
4863 errx(1, "invalid user %d", getuid());
4871 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
4872 if (stat(conf, &sb) != -1) {
4873 if (S_ISREG(sb.st_mode))
4876 /* try global conf file */
4877 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
4878 if (!stat(conf, &sb))
4879 if (S_ISREG(sb.st_mode))
4885 /* setup all bars */
4886 for (i = 0; i < ScreenCount(display); i++)
4887 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4888 if (winfocus == NULL)
4889 winfocus = TAILQ_FIRST(&r->ws->winlist);
4893 /* set some values to work around bad programs */
4901 xfd = ConnectionNumber(display);
4903 while (XPending(display)) {
4904 XNextEvent(display, &e);
4907 if (e.type < LASTEvent) {
4909 if (handler[e.type])
4910 handler[e.type](&e);
4912 DNPRINTF(SWM_D_EVENT,
4913 "win: %lu unknown event: %d\n",
4914 e.xany.window, e.type);
4916 switch (e.type - xrandr_eventbase) {
4917 case RRScreenChangeNotify:
4921 DNPRINTF(SWM_D_EVENT,
4922 "win: %lu unknown xrandr event: "
4923 "%d\n", e.xany.window, e.type);
4929 /* if we are being restarted go focus on first window */
4931 rr = winfocus->ws->r;
4933 /* not a visible window */
4937 /* move pointer to first screen if multi screen */
4938 if (ScreenCount(display) > 1 || outputs > 1)
4939 XWarpPointer(display, None, rr->s[0].root,
4940 0, 0, 0, 0, rr->g.x,
4941 rr->g.y + (bar_enabled ? bar_height : 0));
4943 a.id = SWM_ARG_ID_FOCUSCUR;
4953 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
4955 DNPRINTF(SWM_D_MISC, "select failed");
4956 if (restart_wm == 1)
4957 restart(NULL, NULL);
4967 XFreeGC(display, bar_gc);
4968 XCloseDisplay(display);