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,
606 while ((pid = waitpid(WAIT_ANY, NULL, WNOHANG)) != -1) {
607 DNPRINTF(SWM_D_MISC, "reaping: %d\n", pid);
624 installsignal(int sig, char *name)
628 sa.sa_handler = sighdlr;
629 sigemptyset(&sa.sa_mask);
631 if (sigaction(sig, &sa, NULL) == -1)
632 err(1, "could not install %s handler", name);
636 name_to_color(char *colorname)
640 XColor screen_def, exact_def;
641 unsigned long result = 0;
642 char cname[32] = "#";
644 cmap = DefaultColormap(display, screens[0].idx);
645 status = XAllocNamedColor(display, cmap, colorname,
646 &screen_def, &exact_def);
648 strlcat(cname, colorname + 2, sizeof cname - 1);
649 status = XAllocNamedColor(display, cmap, cname, &screen_def,
653 result = screen_def.pixel;
655 fprintf(stderr, "color '%s' not found.\n", colorname);
661 setscreencolor(char *val, int i, int c)
663 if (i > 0 && i <= ScreenCount(display)) {
664 screens[i - 1].c[c].color = name_to_color(val);
665 free(screens[i - 1].c[c].name);
666 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
668 } else if (i == -1) {
669 for (i = 0; i < ScreenCount(display); i++) {
670 screens[i].c[c].color = name_to_color(val);
671 free(screens[i].c[c].name);
672 if ((screens[i].c[c].name = strdup(val)) == NULL)
676 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
677 i, ScreenCount(display));
681 custom_region(char *val)
683 unsigned int sidx, x, y, w, h;
685 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
686 errx(1, "invalid custom region, "
687 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
688 if (sidx < 1 || sidx > ScreenCount(display))
689 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
690 sidx, ScreenCount(display));
694 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
696 if (x < 0 || x > DisplayWidth(display, sidx) ||
697 y < 0 || y > DisplayHeight(display, sidx) ||
698 w + x > DisplayWidth(display, sidx) ||
699 h + y > DisplayHeight(display, sidx))
700 errx(1, "region %ux%u+%u+%u not within screen boundaries "
701 "(%ux%u)\n", w, h, x, y,
702 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
704 new_region(&screens[sidx], x, y, w, h);
708 socket_setnonblock(int fd)
712 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
713 err(1, "fcntl F_GETFL");
715 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
716 err(1, "fcntl F_SETFL");
720 bar_print(struct swm_region *r, char *s)
722 XClearWindow(display, r->bar_window);
723 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
724 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
733 bzero(bar_pipe, sizeof bar_pipe);
736 kill(bar_pid, SIGTERM);
739 strlcpy(bar_ext, "", sizeof bar_ext);
744 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
746 int do_class, do_name;
748 XClassHint *xch = NULL;
750 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
752 if ((xch = XAllocClassHint()) == NULL)
754 status = XGetClassHint(display, cur_focus->id, xch);
755 if (status == BadWindow || status == BadAlloc)
757 do_class = (title_class_enabled && xch->res_class != NULL);
758 do_name = (title_name_enabled && xch->res_name != NULL);
760 strlcat(s, xch->res_class, sz);
761 if (do_class && do_name)
764 strlcat(s, xch->res_name, sz);
777 struct swm_region *r;
781 char cn[SWM_BAR_MAX];
782 char loc[SWM_BAR_MAX];
786 if (bar_enabled == 0)
788 if (bar_extra && bar_extra_running) {
789 /* ignore short reads; it'll correct itself */
790 while ((b = fgetln(stdin, &len)) != NULL)
791 if (b && b[len - 1] == '\n') {
793 strlcpy(bar_ext, b, sizeof bar_ext);
795 if (b == NULL && errno != EAGAIN) {
796 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
797 errno, strerror(errno));
801 strlcpy(bar_ext, "", sizeof bar_ext);
803 if (clock_enabled == 0)
804 strlcpy(s, "", sizeof s);
807 localtime_r(&tmt, &tm);
808 strftime(s, sizeof s, clock_format, &tm);
809 strlcat(s, " ", sizeof s);
812 for (i = 0; i < ScreenCount(display); i++) {
814 TAILQ_FOREACH(r, &screens[i].rl, entry) {
815 strlcpy(cn, "", sizeof cn);
817 bar_class_name(cn, sizeof cn, r->ws->focus);
820 stack = r->ws->cur_layout->name;
822 snprintf(loc, sizeof loc, "%d:%d %s %s%s %s %s",
823 x++, r->ws->idx + 1, stack, s, cn, bar_ext,
838 bar_toggle(struct swm_region *r, union arg *args)
840 struct swm_region *tmpr;
841 int i, sc = ScreenCount(display);
843 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
846 for (i = 0; i < sc; i++)
847 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
848 XUnmapWindow(display, tmpr->bar_window);
850 for (i = 0; i < sc; i++)
851 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
852 XMapRaised(display, tmpr->bar_window);
854 bar_enabled = !bar_enabled;
857 /* must be after stack */
864 XSetWindowAttributes wa;
865 struct swm_region *r;
868 /* do this here because the conf file is in memory */
869 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
870 /* launch external status app */
871 bar_extra_running = 1;
872 if (pipe(bar_pipe) == -1)
873 err(1, "pipe error");
874 socket_setnonblock(bar_pipe[0]);
875 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
876 if (dup2(bar_pipe[0], 0) == -1)
878 if (dup2(bar_pipe[1], 1) == -1)
880 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
881 err(1, "could not disable SIGPIPE");
882 switch (bar_pid = fork()) {
884 err(1, "cannot fork");
888 execvp(bar_argv[0], bar_argv);
889 err(1, "%s external app failed", bar_argv[0]);
891 default: /* parent */
897 bzero(&wa, sizeof wa);
898 for (i = 0; i < ScreenCount(display); i++)
899 TAILQ_FOREACH(r, &screens[i].rl, entry) {
901 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
902 wa.background_pixel =
903 screens[i].c[SWM_S_COLOR_BAR].color;
904 XChangeWindowAttributes(display, r->bar_window,
905 CWBackPixel | CWBorderPixel, &wa);
911 bar_setup(struct swm_region *r)
915 for (i = 0; bar_fonts[i] != NULL; i++) {
916 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
922 if (bar_fonts[i] == NULL)
923 errx(1, "couldn't load font");
924 bar_height = bar_fs->ascent + bar_fs->descent + 3;
926 r->bar_window = XCreateSimpleWindow(display,
927 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
928 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
929 r->s->c[SWM_S_COLOR_BAR].color);
930 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
931 XSetFont(display, bar_gc, bar_fs->fid);
932 XSelectInput(display, r->bar_window, VisibilityChangeMask);
934 XMapRaised(display, r->bar_window);
935 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
937 if (signal(SIGALRM, bar_signal) == SIG_ERR)
938 err(1, "could not install bar_signal");
943 set_win_state(struct ws_win *win, long state)
945 long data[] = {state, None};
947 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
952 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
953 (unsigned char *)data, 2);
961 unsigned char *p = NULL;
962 unsigned long n, extra;
965 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
966 &real, &format, &n, &extra, (unsigned char **)&p);
967 if (status != Success)
970 result = *((long *)p);
976 version(struct swm_region *r, union arg *args)
978 bar_version = !bar_version;
980 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
981 SWM_VERSION, cvstag);
983 strlcpy(bar_vertext, "", sizeof bar_vertext);
988 client_msg(struct ws_win *win, Atom a)
990 XClientMessageEvent cm;
995 bzero(&cm, sizeof cm);
996 cm.type = ClientMessage;
998 cm.message_type = aprot;
1001 cm.data.l[1] = CurrentTime;
1002 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1006 configreq_win(struct ws_win *win)
1008 XConfigureRequestEvent cr;
1013 bzero(&cr, sizeof cr);
1014 cr.type = ConfigureRequest;
1015 cr.display = display;
1016 cr.parent = win->id;
1017 cr.window = win->id;
1020 cr.width = win->g.w;
1021 cr.height = win->g.h;
1022 cr.border_width = 1;
1024 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&cr);
1028 config_win(struct ws_win *win)
1032 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
1033 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1038 ce.type = ConfigureNotify;
1039 ce.display = display;
1041 ce.window = win->id;
1044 ce.width = win->g.w;
1045 ce.height = win->g.h;
1046 ce.border_width = 1; /* XXX store this! */
1048 ce.override_redirect = False;
1049 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1053 count_win(struct workspace *ws, int count_transient)
1058 TAILQ_FOREACH(win, &ws->winlist, entry) {
1059 if (count_transient == 0 && win->floating)
1061 if (count_transient == 0 && win->transient)
1065 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1071 quit(struct swm_region *r, union arg *args)
1073 DNPRINTF(SWM_D_MISC, "quit\n");
1078 unmap_window(struct ws_win *win)
1083 /* don't unmap again */
1084 if (getstate(win->id) == IconicState)
1087 set_win_state(win, IconicState);
1089 XUnmapWindow(display, win->id);
1091 XSetWindowBorder(display, win->id,
1092 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1101 for (i = 0; i < ScreenCount(display); i++)
1102 for (j = 0; j < SWM_WS_MAX; j++)
1103 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1108 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1115 event.display = display; /* Ignored, but what the hell */
1116 event.window = win->id;
1117 event.root = win->s->root;
1118 event.subwindow = None;
1119 event.time = CurrentTime;
1124 event.same_screen = True;
1125 event.keycode = XKeysymToKeycode(display, keysym);
1126 event.state = modifiers;
1128 event.type = KeyPress;
1129 XSendEvent(event.display, event.window, True,
1130 KeyPressMask, (XEvent *)&event);
1132 event.type = KeyRelease;
1133 XSendEvent(event.display, event.window, True,
1134 KeyPressMask, (XEvent *)&event);
1139 restart(struct swm_region *r, union arg *args)
1141 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1143 /* disable alarm because the following code may not be interrupted */
1145 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1146 errx(1, "can't disable alarm");
1151 XCloseDisplay(display);
1152 execvp(start_argv[0], start_argv);
1153 fprintf(stderr, "execvp failed\n");
1159 root_to_region(Window root)
1161 struct swm_region *r = NULL;
1163 int i, x, y, wx, wy;
1166 for (i = 0; i < ScreenCount(display); i++)
1167 if (screens[i].root == root)
1170 if (XQueryPointer(display, screens[i].root,
1171 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1172 /* choose a region based on pointer location */
1173 TAILQ_FOREACH(r, &screens[i].rl, entry)
1174 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1175 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1180 r = TAILQ_FIRST(&screens[i].rl);
1186 find_unmanaged_window(Window id)
1191 for (i = 0; i < ScreenCount(display); i++)
1192 for (j = 0; j < SWM_WS_MAX; j++)
1193 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1201 find_window(Window id)
1204 Window wrr, wpr, *wcr = NULL;
1207 for (i = 0; i < ScreenCount(display); i++)
1208 for (j = 0; j < SWM_WS_MAX; j++)
1209 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1213 /* if we were looking for the parent return that window instead */
1214 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1219 /* ignore not found and root */
1220 if (wpr == 0 || wrr == wpr)
1223 /* look for parent */
1224 for (i = 0; i < ScreenCount(display); i++)
1225 for (j = 0; j < SWM_WS_MAX; j++)
1226 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1234 spawn(struct swm_region *r, union arg *args)
1239 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1241 * The double-fork construct avoids zombie processes and keeps the code
1242 * clean from stupid signal handlers.
1247 close(ConnectionNumber(display));
1248 setenv("LD_PRELOAD", SWM_LIB, 1);
1249 if (asprintf(&ret, "%d", r->ws->idx)) {
1250 setenv("_SWM_WS", ret, 1);
1253 if (asprintf(&ret, "%d", getpid())) {
1254 setenv("_SWM_PID", ret, 1);
1258 /* kill stdin, mplayer, ssh-add etc. need that */
1259 si = open("/dev/null", O_RDONLY, 0);
1261 err(1, "open /dev/null");
1262 if (dup2(si, 0) == -1)
1263 err(1, "dup2 /dev/null");
1264 execvp(args->argv[0], args->argv);
1265 fprintf(stderr, "execvp failed\n");
1273 spawnterm(struct swm_region *r, union arg *args)
1275 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1278 setenv("_SWM_XTERM_FONTADJ", "", 1);
1283 kill_refs(struct ws_win *win)
1286 struct swm_region *r;
1287 struct workspace *ws;
1292 for (i = 0; i < ScreenCount(display); i++)
1293 TAILQ_FOREACH(r, &screens[i].rl, entry)
1294 for (x = 0; x < SWM_WS_MAX; x++) {
1296 if (win == ws->focus)
1298 if (win == ws->focus_prev)
1299 ws->focus_prev = NULL;
1304 validate_win(struct ws_win *testwin)
1307 struct workspace *ws;
1308 struct swm_region *r;
1309 int i, x, foundit = 0;
1311 if (testwin == NULL)
1314 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1315 TAILQ_FOREACH(r, &screens[i].rl, entry)
1316 for (x = 0; x < SWM_WS_MAX; x++) {
1318 TAILQ_FOREACH(win, &ws->winlist, entry)
1326 validate_ws(struct workspace *testws)
1328 struct swm_region *r;
1329 struct workspace *ws;
1332 /* validate all ws */
1333 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1334 TAILQ_FOREACH(r, &screens[i].rl, entry)
1335 for (x = 0; x < SWM_WS_MAX; x++) {
1344 unfocus_win(struct ws_win *win)
1348 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1352 if (win->ws == NULL)
1355 if (validate_ws(win->ws))
1358 if (win->ws->r == NULL)
1361 if (validate_win(win)) {
1366 if (win->ws->focus == win) {
1367 win->ws->focus = NULL;
1368 win->ws->focus_prev = win;
1371 if (validate_win(win->ws->focus)) {
1372 kill_refs(win->ws->focus);
1373 win->ws->focus = NULL;
1375 if (validate_win(win->ws->focus_prev)) {
1376 kill_refs(win->ws->focus_prev);
1377 win->ws->focus_prev = NULL;
1380 /* drain all previous unfocus events */
1381 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
1384 grabbuttons(win, 0);
1385 XSetWindowBorder(display, win->id,
1386 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1396 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
1398 for (i = 0; i < ScreenCount(display); i++)
1399 for (j = 0; j < SWM_WS_MAX; j++)
1400 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1405 focus_win(struct ws_win *win)
1410 struct ws_win *cfw = NULL;
1413 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1417 if (win->ws == NULL)
1420 if (validate_ws(win->ws))
1422 if (validate_win(win)) {
1427 if (validate_win(win)) {
1432 XGetInputFocus(display, &cur_focus, &rr);
1433 if ((cfw = find_window(cur_focus)) != NULL)
1436 win->ws->focus = win;
1438 if (win->ws->r != NULL) {
1439 /* drain all previous focus events */
1440 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
1444 XSetInputFocus(display, win->id,
1445 RevertToParent, CurrentTime);
1446 grabbuttons(win, 1);
1447 XSetWindowBorder(display, win->id,
1448 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1449 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS)
1450 XMapRaised(display, win->id);
1455 switchws(struct swm_region *r, union arg *args)
1457 int wsid = args->id, unmap_old = 0;
1458 struct swm_region *this_r, *other_r;
1460 struct workspace *new_ws, *old_ws;
1467 old_ws = this_r->ws;
1468 new_ws = &this_r->s->ws[wsid];
1470 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1471 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1474 if (new_ws == NULL || old_ws == NULL)
1476 if (new_ws == old_ws)
1479 other_r = new_ws->r;
1480 if (other_r == NULL) {
1481 /* the other workspace is hidden, hide this one */
1485 /* the other ws is visible in another region, exchange them */
1486 other_r->ws_prior = new_ws;
1487 other_r->ws = old_ws;
1488 old_ws->r = other_r;
1490 this_r->ws_prior = old_ws;
1491 this_r->ws = new_ws;
1495 a.id = SWM_ARG_ID_FOCUSCUR;
1496 focus(new_ws->r, &a);
1499 /* unmap old windows */
1501 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1506 cyclews(struct swm_region *r, union arg *args)
1509 struct swm_screen *s = r->s;
1511 DNPRINTF(SWM_D_WS, "cyclews id %d "
1512 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1513 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1518 case SWM_ARG_ID_CYCLEWS_UP:
1519 if (a.id < SWM_WS_MAX - 1)
1524 case SWM_ARG_ID_CYCLEWS_DOWN:
1528 a.id = SWM_WS_MAX - 1;
1534 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1536 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1540 } while (a.id != r->ws->idx);
1544 priorws(struct swm_region *r, union arg *args)
1548 DNPRINTF(SWM_D_WS, "priorws id %d "
1549 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1550 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1552 if (r->ws_prior == NULL)
1555 a.id = r->ws_prior->idx;
1560 cyclescr(struct swm_region *r, union arg *args)
1562 struct swm_region *rr = NULL;
1566 /* do nothing if we don't have more than one screen */
1567 if (!(ScreenCount(display) > 1 || outputs > 1))
1572 case SWM_ARG_ID_CYCLESC_UP:
1573 rr = TAILQ_NEXT(r, entry);
1575 rr = TAILQ_FIRST(&screens[i].rl);
1577 case SWM_ARG_ID_CYCLESC_DOWN:
1578 rr = TAILQ_PREV(r, swm_region_list, entry);
1580 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1588 /* move mouse to region */
1590 y = rr->g.y + 1 + bar_enabled ? bar_height : 0;
1591 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1593 a.id = SWM_ARG_ID_FOCUSCUR;
1596 if (rr->ws->focus) {
1597 /* move to focus window */
1598 x = rr->ws->focus->g.x + 1;
1599 y = rr->ws->focus->g.y + 1;
1600 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1605 swapwin(struct swm_region *r, union arg *args)
1607 struct ws_win *target, *source;
1608 struct ws_win *cur_focus;
1609 struct ws_win_list *wl;
1612 DNPRINTF(SWM_D_WS, "swapwin id %d "
1613 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1614 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1616 cur_focus = r->ws->focus;
1617 if (cur_focus == NULL)
1621 wl = &source->ws->winlist;
1624 case SWM_ARG_ID_SWAPPREV:
1625 target = TAILQ_PREV(source, ws_win_list, entry);
1626 TAILQ_REMOVE(wl, cur_focus, entry);
1628 TAILQ_INSERT_TAIL(wl, source, entry);
1630 TAILQ_INSERT_BEFORE(target, source, entry);
1632 case SWM_ARG_ID_SWAPNEXT:
1633 target = TAILQ_NEXT(source, entry);
1634 TAILQ_REMOVE(wl, source, entry);
1636 TAILQ_INSERT_HEAD(wl, source, entry);
1638 TAILQ_INSERT_AFTER(wl, target, source, entry);
1640 case SWM_ARG_ID_SWAPMAIN:
1641 target = TAILQ_FIRST(wl);
1642 if (target == source) {
1643 if (source->ws->focus_prev != NULL &&
1644 source->ws->focus_prev != target)
1646 source = source->ws->focus_prev;
1650 if (target == NULL || source == NULL)
1652 source->ws->focus_prev = target;
1653 TAILQ_REMOVE(wl, target, entry);
1654 TAILQ_INSERT_BEFORE(source, target, entry);
1655 TAILQ_REMOVE(wl, source, entry);
1656 TAILQ_INSERT_HEAD(wl, source, entry);
1658 case SWM_ARG_ID_MOVELAST:
1659 TAILQ_REMOVE(wl, source, entry);
1660 TAILQ_INSERT_TAIL(wl, source, entry);
1663 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1671 focus_prev(struct ws_win *win)
1673 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
1674 struct ws_win *cur_focus = NULL;
1675 struct ws_win_list *wl = NULL;
1676 struct workspace *ws = NULL;
1678 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
1680 if (!(win && win->ws))
1685 cur_focus = ws->focus;
1686 winlostfocus = cur_focus;
1688 /* pickle, just focus on whatever */
1689 if (cur_focus == NULL) {
1690 /* use prev_focus if valid */
1691 if (ws->focus_prev && ws->focus_prev != cur_focus &&
1692 find_window(WINID(ws->focus_prev)))
1693 winfocus = ws->focus_prev;
1694 if (winfocus == NULL)
1695 winfocus = TAILQ_FIRST(wl);
1699 /* if transient focus on parent */
1700 if (cur_focus->transient) {
1701 winfocus = find_window(cur_focus->transient);
1705 /* if in max_stack try harder */
1706 if (ws->cur_layout->flags & SWM_L_FOCUSPREV) {
1707 if (cur_focus != ws->focus_prev)
1708 winfocus = ws->focus_prev;
1709 else if (cur_focus != ws->focus)
1710 winfocus = ws->focus;
1712 winfocus = TAILQ_PREV(win, ws_win_list, entry);
1717 if (cur_focus == win)
1718 winfocus = TAILQ_PREV(win, ws_win_list, entry);
1719 if (winfocus == NULL)
1720 winfocus = TAILQ_LAST(wl, ws_win_list);
1721 if (winfocus == NULL || winfocus == win)
1722 winfocus = TAILQ_NEXT(cur_focus, entry);
1724 if (winfocus == winlostfocus || winfocus == NULL)
1727 focus_magic(winfocus, SWM_F_GENERIC);
1731 focus(struct swm_region *r, union arg *args)
1733 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
1734 struct ws_win *cur_focus = NULL;
1735 struct ws_win_list *wl = NULL;
1736 struct workspace *ws = NULL;
1741 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1743 /* treat FOCUS_CUR special */
1744 if (args->id == SWM_ARG_ID_FOCUSCUR) {
1746 winfocus = r->ws->focus;
1747 else if (r->ws->focus_prev)
1748 winfocus = r->ws->focus_prev;
1750 winfocus = TAILQ_FIRST(&r->ws->winlist);
1752 focus_magic(winfocus, SWM_F_GENERIC);
1756 if ((cur_focus = r->ws->focus) == NULL)
1761 winlostfocus = cur_focus;
1764 case SWM_ARG_ID_FOCUSPREV:
1765 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1766 if (winfocus == NULL)
1767 winfocus = TAILQ_LAST(wl, ws_win_list);
1770 case SWM_ARG_ID_FOCUSNEXT:
1771 winfocus = TAILQ_NEXT(cur_focus, entry);
1772 if (winfocus == NULL)
1773 winfocus = TAILQ_FIRST(wl);
1776 case SWM_ARG_ID_FOCUSMAIN:
1777 winfocus = TAILQ_FIRST(wl);
1778 if (winfocus == cur_focus)
1779 winfocus = cur_focus->ws->focus_prev;
1786 if (winfocus == winlostfocus || winfocus == NULL)
1789 focus_magic(winfocus, SWM_F_GENERIC);
1793 cycle_layout(struct swm_region *r, union arg *args)
1795 struct workspace *ws = r->ws;
1796 struct ws_win *winfocus;
1799 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1801 winfocus = ws->focus;
1804 if (ws->cur_layout->l_stack == NULL)
1805 ws->cur_layout = &layouts[0];
1808 a.id = SWM_ARG_ID_FOCUSCUR;
1814 stack_config(struct swm_region *r, union arg *args)
1816 struct workspace *ws = r->ws;
1818 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1821 if (ws->cur_layout->l_config != NULL)
1822 ws->cur_layout->l_config(ws, args->id);
1824 if (args->id != SWM_ARG_ID_STACKINIT);
1830 struct swm_geometry g;
1831 struct swm_region *r;
1834 DNPRINTF(SWM_D_STACK, "stack\n");
1836 for (i = 0; i < ScreenCount(display); i++) {
1838 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1839 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1840 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1842 /* start with screen geometry, adjust for bar */
1850 r->ws->cur_layout->l_stack(r->ws, &g);
1851 /* save r so we can track region changes */
1860 store_float_geom(struct ws_win *win, struct swm_region *r)
1862 /* retain window geom and region geom */
1863 win->g_float.x = win->g.x;
1864 win->g_float.y = win->g.y;
1865 win->g_float.w = win->g.w;
1866 win->g_float.h = win->g.h;
1867 win->rg_float.x = r->g.x;
1868 win->rg_float.y = r->g.y;
1869 win->rg_float.w = r->g.w;
1870 win->rg_float.h = r->g.h;
1871 win->g_floatvalid = 1;
1875 stack_floater(struct ws_win *win, struct swm_region *r)
1883 bzero(&wc, sizeof wc);
1884 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1887 * to allow windows to change their size (e.g. mplayer fs) only retrieve
1888 * geom on ws switches or return from max mode
1891 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid) ) {
1893 * use stored g and rg to set relative position and size
1894 * as in old region or before max stack mode
1896 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
1897 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
1898 win->g.w = win->g_float.w;
1899 win->g.h = win->g_float.h;
1900 win->g_floatvalid = 0;
1903 win->floatmaxed = 0;
1905 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
1906 (win->g.h >= HEIGHT(r)))
1907 wc.border_width = 0;
1909 wc.border_width = 1;
1910 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
1911 win->g.w = (double)WIDTH(r) * dialog_ratio;
1912 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1917 * floaters and transients are auto-centred unless moved
1920 win->g.x = r->g.x + (WIDTH(r) - win->g.w) / 2;
1921 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) / 2;
1924 /* win can be outside r if new r smaller than old r */
1925 /* Ensure top left corner inside r (move probs otherwise) */
1926 if (win->g.x < r->g.x )
1928 if (win->g.x > r->g.x + r->g.w - 1)
1929 win->g.x = (win->g.w > r->g.w) ? r->g.x :
1930 (r->g.x + r->g.w - win->g.w - 2);
1931 if (win->g.y < r->g.y )
1933 if (win->g.y > r->g.y + r->g.h - 1)
1934 win->g.y = (win->g.h > r->g.h) ? r->g.y :
1935 (r->g.y + r->g.h - win->g.h - 2);
1939 wc.width = win->g.w;
1940 wc.height = win->g.h;
1943 * Retain floater and transient geometry for correct positioning
1944 * when ws changes region
1946 store_float_geom(win, r);
1948 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
1949 win->id, wc.x, wc.y, wc.width, wc.height);
1951 XConfigureWindow(display, win->id, mask, &wc);
1956 * Send keystrokes to terminal to decrease/increase the font size as the
1957 * window size changes.
1960 adjust_font(struct ws_win *win)
1962 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
1963 win->floating || win->transient)
1966 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
1967 win->g.w / win->sh.width_inc < term_width &&
1968 win->font_steps < SWM_MAX_FONT_STEPS) {
1969 win->font_size_boundary[win->font_steps] =
1970 (win->sh.width_inc * term_width) + win->sh.base_width;
1973 win->last_inc = win->sh.width_inc;
1974 fake_keypress(win, XK_KP_Subtract, ShiftMask);
1975 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
1976 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
1979 win->last_inc = win->sh.width_inc;
1980 fake_keypress(win, XK_KP_Add, ShiftMask);
1984 #define SWAPXY(g) do { \
1986 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1987 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1990 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1993 XWindowAttributes wa;
1994 struct swm_geometry win_g, r_g = *g;
1996 int i, j, s, stacks;
1997 int w_inc = 1, h_inc, w_base = 1, h_base;
1998 int hrh, extra = 0, h_slice, last_h = 0;
1999 int split, colno, winno, mwin, msize, mscale;
2000 int remain, missing, v_slice, reconfigure;
2003 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2004 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2006 winno = count_win(ws, 0);
2007 if (winno == 0 && count_win(ws, 1) == 0)
2010 TAILQ_FOREACH(win, &ws->winlist, entry)
2011 if (win->transient == 0 && win->floating == 0)
2018 w_inc = win->sh.width_inc;
2019 w_base = win->sh.base_width;
2020 mwin = ws->l_state.horizontal_mwin;
2021 mscale = ws->l_state.horizontal_msize;
2022 stacks = ws->l_state.horizontal_stacks;
2025 w_inc = win->sh.height_inc;
2026 w_base = win->sh.base_height;
2027 mwin = ws->l_state.vertical_mwin;
2028 mscale = ws->l_state.vertical_msize;
2029 stacks = ws->l_state.vertical_stacks;
2033 if (stacks > winno - mwin)
2034 stacks = winno - mwin;
2038 h_slice = r_g.h / SWM_H_SLICE;
2039 if (mwin && winno > mwin) {
2040 v_slice = r_g.w / SWM_V_SLICE;
2044 win_g.w = v_slice * mscale;
2046 if (w_inc > 1 && w_inc < v_slice) {
2047 /* adjust for window's requested size increment */
2048 remain = (win_g.w - w_base) % w_inc;
2049 missing = w_inc - remain;
2056 win_g.x += r_g.w - msize;
2059 colno = split = winno / stacks;
2060 win_g.w = ((r_g.w - (stacks * 2) + 2) / stacks);
2062 hrh = r_g.h / colno;
2063 extra = r_g.h - (colno * hrh);
2066 /* stack all the tiled windows */
2067 i = j = 0, s = stacks;
2068 TAILQ_FOREACH(win, &ws->winlist, entry) {
2069 if (win->transient != 0 || win->floating != 0)
2072 if (split && i == split) {
2073 colno = (winno - mwin) / stacks;
2074 if (s <= (winno - mwin) % stacks)
2076 split = split + colno;
2077 hrh = (r_g.h / colno);
2078 extra = r_g.h - (colno * hrh);
2082 win_g.x += win_g.w + 2;
2083 win_g.w = (r_g.w - msize - (stacks * 2)) / stacks;
2085 win_g.w += (r_g.w - msize - (stacks * 2)) %
2092 h_inc = win->sh.width_inc;
2093 h_base = win->sh.base_width;
2095 h_inc = win->sh.height_inc;
2096 h_base = win->sh.base_height;
2098 if (j == colno - 1) {
2099 win_g.h = hrh + extra;
2100 } else if (h_inc > 1 && h_inc < h_slice) {
2101 /* adjust for window's requested size increment */
2102 remain = (win_g.h - h_base) % h_inc;
2103 missing = h_inc - remain;
2105 if (missing <= extra || j == 0) {
2117 win_g.y += last_h + 2;
2119 bzero(&wc, sizeof wc);
2120 if (disable_border && bar_enabled == 0 && winno == 1){
2121 wc.border_width = 0;
2125 wc.border_width = 1;
2128 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2129 win->g.w != win_g.h || win->g.h != win_g.w) {
2131 win->g.x = wc.x = win_g.y;
2132 win->g.y = wc.y = win_g.x;
2133 win->g.w = wc.width = win_g.h;
2134 win->g.h = wc.height = win_g.w;
2137 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2138 win->g.w != win_g.w || win->g.h != win_g.h) {
2140 win->g.x = wc.x = win_g.x;
2141 win->g.y = wc.y = win_g.y;
2142 win->g.w = wc.width = win_g.w;
2143 win->g.h = wc.height = win_g.h;
2148 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2149 XConfigureWindow(display, win->id, mask, &wc);
2153 if (XGetWindowAttributes(display, win->id, &wa))
2154 if (wa.map_state == IsUnmapped)
2155 XMapRaised(display, win->id);
2163 /* now, stack all the floaters and transients */
2164 TAILQ_FOREACH(win, &ws->winlist, entry) {
2165 if (win->transient == 0 && win->floating == 0)
2168 stack_floater(win, ws->r);
2169 XMapRaised(display, win->id);
2174 vertical_config(struct workspace *ws, int id)
2176 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2179 case SWM_ARG_ID_STACKRESET:
2180 case SWM_ARG_ID_STACKINIT:
2181 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2182 ws->l_state.vertical_mwin = 1;
2183 ws->l_state.vertical_stacks = 1;
2185 case SWM_ARG_ID_MASTERSHRINK:
2186 if (ws->l_state.vertical_msize > 1)
2187 ws->l_state.vertical_msize--;
2189 case SWM_ARG_ID_MASTERGROW:
2190 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2191 ws->l_state.vertical_msize++;
2193 case SWM_ARG_ID_MASTERADD:
2194 ws->l_state.vertical_mwin++;
2196 case SWM_ARG_ID_MASTERDEL:
2197 if (ws->l_state.vertical_mwin > 0)
2198 ws->l_state.vertical_mwin--;
2200 case SWM_ARG_ID_STACKINC:
2201 ws->l_state.vertical_stacks++;
2203 case SWM_ARG_ID_STACKDEC:
2204 if (ws->l_state.vertical_stacks > 1)
2205 ws->l_state.vertical_stacks--;
2213 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2215 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2217 stack_master(ws, g, 0, 0);
2221 horizontal_config(struct workspace *ws, int id)
2223 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2226 case SWM_ARG_ID_STACKRESET:
2227 case SWM_ARG_ID_STACKINIT:
2228 ws->l_state.horizontal_mwin = 1;
2229 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2230 ws->l_state.horizontal_stacks = 1;
2232 case SWM_ARG_ID_MASTERSHRINK:
2233 if (ws->l_state.horizontal_msize > 1)
2234 ws->l_state.horizontal_msize--;
2236 case SWM_ARG_ID_MASTERGROW:
2237 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2238 ws->l_state.horizontal_msize++;
2240 case SWM_ARG_ID_MASTERADD:
2241 ws->l_state.horizontal_mwin++;
2243 case SWM_ARG_ID_MASTERDEL:
2244 if (ws->l_state.horizontal_mwin > 0)
2245 ws->l_state.horizontal_mwin--;
2247 case SWM_ARG_ID_STACKINC:
2248 ws->l_state.horizontal_stacks++;
2250 case SWM_ARG_ID_STACKDEC:
2251 if (ws->l_state.horizontal_stacks > 1)
2252 ws->l_state.horizontal_stacks--;
2260 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2262 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
2264 stack_master(ws, g, 1, 0);
2267 /* fullscreen view */
2269 max_stack(struct workspace *ws, struct swm_geometry *g)
2272 struct swm_geometry gg = *g;
2273 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2277 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2282 winno = count_win(ws, 0);
2283 if (winno == 0 && count_win(ws, 1) == 0)
2286 TAILQ_FOREACH(win, &ws->winlist, entry) {
2287 if (win->transient) {
2289 parent = find_window(win->transient);
2293 if (win->floating && win->floatmaxed == 0 ) {
2295 * retain geometry for retrieval on exit from
2298 store_float_geom(win, ws->r);
2299 win->floatmaxed = 1;
2302 /* only reconfigure if necessary */
2303 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2305 bzero(&wc, sizeof wc);
2306 win->g.x = wc.x = gg.x;
2307 win->g.y = wc.y = gg.y;
2309 wc.border_width = 1;
2310 win->g.w = wc.width = gg.w;
2311 win->g.h = wc.height = gg.h;
2313 wc.border_width = 0;
2314 win->g.w = wc.width = gg.w + 2;
2315 win->g.h = wc.height = gg.h + 2;
2317 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2318 XConfigureWindow(display, win->id, mask, &wc);
2321 /* unmap only if we don't have multi screen */
2322 if (win != ws->focus)
2323 if (!(ScreenCount(display) > 1 || outputs > 1))
2327 /* put the last transient on top */
2330 XMapRaised(display, parent->id);
2331 stack_floater(wintrans, ws->r);
2332 focus_magic(wintrans, SWM_F_TRANSIENT);
2337 send_to_ws(struct swm_region *r, union arg *args)
2339 int wsid = args->id;
2340 struct ws_win *win = win;
2341 struct workspace *ws, *nws;
2342 Atom ws_idx_atom = 0;
2343 unsigned char ws_idx_str[SWM_PROPLEN];
2352 if (win->ws->idx == wsid)
2355 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2358 nws = &win->s->ws[wsid];
2360 a.id = SWM_ARG_ID_FOCUSPREV;
2363 TAILQ_REMOVE(&ws->winlist, win, entry);
2364 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2367 /* Try to update the window's workspace property */
2368 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2370 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2371 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2373 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2374 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2380 wkill(struct swm_region *r, union arg *args)
2382 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
2384 if(r->ws->focus == NULL)
2387 if (args->id == SWM_ARG_ID_KILLWINDOW)
2388 XKillClient(display, r->ws->focus->id);
2390 if (r->ws->focus->can_delete)
2391 client_msg(r->ws->focus, adelete);
2395 floating_toggle(struct swm_region *r, union arg *args)
2397 struct ws_win *win = r->ws->focus;
2403 /* reject floating toggles in max stack mode */
2404 if (r->ws->cur_layout == &layouts[SWM_MAX_STACK])
2407 if (win->floating) {
2408 if (!win->floatmaxed) {
2409 /* retain position for refloat */
2410 store_float_geom(win, r);
2414 if (win->g_floatvalid) {
2415 /* refloat at last floating relative position */
2416 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2417 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2418 win->g.w = win->g_float.w;
2419 win->g.h = win->g_float.h;
2425 a.id = SWM_ARG_ID_FOCUSCUR;
2426 focus(win->ws->r, &a);
2430 resize_window(struct ws_win *win, int center)
2434 struct swm_region *r;
2436 r = root_to_region(win->wa.root);
2437 bzero(&wc, sizeof wc);
2438 mask = CWBorderWidth | CWWidth | CWHeight;
2439 wc.border_width = 1;
2440 wc.width = win->g.w;
2441 wc.height = win->g.h;
2442 if (center == SWM_ARG_ID_CENTER) {
2443 wc.x = (WIDTH(r) - win->g.w) / 2;
2444 wc.y = (HEIGHT(r) - win->g.h) / 2;
2448 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
2449 win->id, wc.x, wc.y, wc.width, wc.height);
2451 XConfigureWindow(display, win->id, mask, &wc);
2456 resize(struct ws_win *win, union arg *args)
2460 struct swm_region *r = win->ws->r;
2465 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
2466 win->id, win->floating, win->transient);
2468 if (!(win->transient != 0 || win->floating != 0))
2471 /* reject resizes in max mode for floaters (transient ok) */
2472 if (win->floatmaxed)
2476 /* raise the window = move to last in window list */
2477 a.id = SWM_ARG_ID_MOVELAST;
2481 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2482 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2485 /* place pointer at bottom left corner or nearest point inside r */
2486 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
2489 relx = r->g.x + r->g.w - win->g.x - 2;
2491 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
2494 rely = r->g.y + r->g.h - win->g.y - 2;
2496 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
2498 XMaskEvent(display, MOUSEMASK | ExposureMask |
2499 SubstructureRedirectMask, &ev);
2501 case ConfigureRequest:
2504 handler[ev.type](&ev);
2507 /* do not allow resize outside of region */
2508 if ( ev.xmotion.x_root < r->g.x ||
2509 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
2510 ev.xmotion.y_root < r->g.y ||
2511 ev.xmotion.y_root > r->g.y + r->g.h - 1)
2514 if (ev.xmotion.x <= 1)
2516 if (ev.xmotion.y <= 1)
2518 win->g.w = ev.xmotion.x;
2519 win->g.h = ev.xmotion.y;
2521 /* not free, don't sync more than 60 times / second */
2522 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2523 time = ev.xmotion.time;
2524 XSync(display, False);
2525 resize_window(win, args->id);
2529 } while (ev.type != ButtonRelease);
2531 XSync(display, False);
2532 resize_window(win, args->id);
2534 store_float_geom(win,r);
2536 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
2538 XUngrabPointer(display, CurrentTime);
2541 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2545 move_window(struct ws_win *win)
2549 struct swm_region *r;
2551 r = root_to_region(win->wa.root);
2552 bzero(&wc, sizeof wc);
2557 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
2558 win->id, wc.x, wc.y, wc.width, wc.height);
2560 XConfigureWindow(display, win->id, mask, &wc);
2565 move(struct ws_win *win, union arg *args)
2569 struct swm_region *r = win->ws->r;
2572 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
2573 win->id, win->floating, win->transient);
2575 /* in max_stack mode should only move transients */
2576 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
2580 if (win->floating == 0 && !win->transient) {
2584 /* raise the window = move to last in window list */
2585 a.id = SWM_ARG_ID_MOVELAST;
2589 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2590 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2592 XWarpPointer(display, None, win->id, 0, 0, 0, 0, -1, -1);
2594 XMaskEvent(display, MOUSEMASK | ExposureMask |
2595 SubstructureRedirectMask, &ev);
2597 case ConfigureRequest:
2600 handler[ev.type](&ev);
2603 /* don't allow to move window origin out of region */
2604 if ( ev.xmotion.x_root < r->g.x ||
2605 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
2606 ev.xmotion.y_root < r->g.y ||
2607 ev.xmotion.y_root > r->g.y + r->g.h - 1)
2610 win->g.x = ev.xmotion.x_root;
2611 win->g.y = ev.xmotion.y_root;
2613 /* not free, don't sync more than 60 times / second */
2614 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2615 time = ev.xmotion.time;
2616 XSync(display, False);
2621 } while (ev.type != ButtonRelease);
2623 XSync(display, False);
2626 store_float_geom(win,r);
2627 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2628 XUngrabPointer(display, CurrentTime);
2631 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2634 /* user/key callable function IDs */
2693 /* key definitions */
2695 dummykeyfunc(struct swm_region *r, union arg *args)
2700 legacyfunc(struct swm_region *r, union arg *args)
2705 char name[SWM_FUNCNAME_LEN];
2706 void (*func)(struct swm_region *r, union arg *);
2708 } keyfuncs[kf_invalid + 1] = {
2709 /* name function argument */
2710 { "cycle_layout", cycle_layout, {0} },
2711 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
2712 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
2713 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
2714 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
2715 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
2716 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
2717 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
2718 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
2719 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
2720 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
2721 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
2722 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
2723 { "spawn_term", spawnterm, {.argv = spawn_term} },
2724 { "spawn_menu", legacyfunc, {0} },
2725 { "quit", quit, {0} },
2726 { "restart", restart, {0} },
2727 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
2728 { "ws_1", switchws, {.id = 0} },
2729 { "ws_2", switchws, {.id = 1} },
2730 { "ws_3", switchws, {.id = 2} },
2731 { "ws_4", switchws, {.id = 3} },
2732 { "ws_5", switchws, {.id = 4} },
2733 { "ws_6", switchws, {.id = 5} },
2734 { "ws_7", switchws, {.id = 6} },
2735 { "ws_8", switchws, {.id = 7} },
2736 { "ws_9", switchws, {.id = 8} },
2737 { "ws_10", switchws, {.id = 9} },
2738 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
2739 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
2740 { "ws_prior", priorws, {0} },
2741 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
2742 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
2743 { "mvws_1", send_to_ws, {.id = 0} },
2744 { "mvws_2", send_to_ws, {.id = 1} },
2745 { "mvws_3", send_to_ws, {.id = 2} },
2746 { "mvws_4", send_to_ws, {.id = 3} },
2747 { "mvws_5", send_to_ws, {.id = 4} },
2748 { "mvws_6", send_to_ws, {.id = 5} },
2749 { "mvws_7", send_to_ws, {.id = 6} },
2750 { "mvws_8", send_to_ws, {.id = 7} },
2751 { "mvws_9", send_to_ws, {.id = 8} },
2752 { "mvws_10", send_to_ws, {.id = 9} },
2753 { "bar_toggle", bar_toggle, {0} },
2754 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
2755 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
2756 { "screenshot_all", legacyfunc, {0} },
2757 { "screenshot_wind", legacyfunc, {0} },
2758 { "float_toggle", floating_toggle,{0} },
2759 { "version", version, {0} },
2760 { "spawn_lock", legacyfunc, {0} },
2761 { "spawn_initscr", legacyfunc, {0} },
2762 { "spawn_custom", dummykeyfunc, {0} },
2763 { "dumpwins", dumpwins, {0} },
2764 { "invalid key func", NULL, {0} },
2769 enum keyfuncid funcid;
2772 int keys_size = 0, keys_length = 0;
2773 struct key *keys = NULL;
2776 enum { client_click, root_click };
2778 unsigned int action;
2780 unsigned int button;
2781 void (*func)(struct ws_win *, union arg *);
2784 /* action key mouse button func args */
2785 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
2786 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
2787 { client_click, MODKEY, Button1, move, {0} },
2791 update_modkey(unsigned int mod)
2796 for (i = 0; i < keys_length; i++)
2797 if (keys[i].mod & ShiftMask)
2798 keys[i].mod = mod | ShiftMask;
2802 for (i = 0; i < LENGTH(buttons); i++)
2803 if (buttons[i].mask & ShiftMask)
2804 buttons[i].mask = mod | ShiftMask;
2806 buttons[i].mask = mod;
2816 int spawns_size = 0, spawns_length = 0;
2817 struct spawn_prog *spawns = NULL;
2820 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
2823 struct spawn_prog *prog = NULL;
2825 char *ap, **real_args;
2827 DNPRINTF(SWM_D_SPAWN, "spawn_custom %s\n", spawn_name);
2830 for (i = 0; i < spawns_length; i++) {
2831 if (!strcasecmp(spawn_name, spawns[i].name))
2835 fprintf(stderr, "spawn_custom: program %s not found\n",
2840 /* make room for expanded args */
2841 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
2842 err(1, "spawn_custom: calloc real_args");
2844 /* expand spawn_args into real_args */
2845 for (i = 0; i < prog->argc; i++) {
2847 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
2848 if (!strcasecmp(ap, "$bar_border")) {
2850 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
2852 err(1, "spawn_custom border color");
2853 } else if (!strcasecmp(ap, "$bar_color")) {
2855 strdup(r->s->c[SWM_S_COLOR_BAR].name))
2857 err(1, "spawn_custom bar color");
2858 } else if (!strcasecmp(ap, "$bar_font")) {
2859 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
2861 err(1, "spawn_custom bar fonts");
2862 } else if (!strcasecmp(ap, "$bar_font_color")) {
2864 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
2866 err(1, "spawn_custom color font");
2867 } else if (!strcasecmp(ap, "$color_focus")) {
2869 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
2871 err(1, "spawn_custom color focus");
2872 } else if (!strcasecmp(ap, "$color_unfocus")) {
2874 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
2876 err(1, "spawn_custom color unfocus");
2878 /* no match --> copy as is */
2879 if ((real_args[i] = strdup(ap)) == NULL)
2880 err(1, "spawn_custom strdup(ap)");
2882 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
2887 if ((swm_debug & SWM_D_SPAWN) != 0) {
2888 fprintf(stderr, "spawn_custom: result = ");
2889 for (i = 0; i < prog->argc; i++)
2890 fprintf(stderr, "\"%s\" ", real_args[i]);
2891 fprintf(stderr, "\n");
2897 for (i = 0; i < prog->argc; i++)
2903 setspawn(struct spawn_prog *prog)
2907 if (prog == NULL || prog->name == NULL)
2911 for (i = 0; i < spawns_length; i++) {
2912 if (!strcmp(spawns[i].name, prog->name)) {
2914 if (prog->argv == NULL) {
2916 DNPRINTF(SWM_D_SPAWN,
2917 "setspawn: delete #%d %s\n",
2919 free(spawns[i].name);
2920 for (j = 0; j < spawns[i].argc; j++)
2921 free(spawns[i].argv[j]);
2922 free(spawns[i].argv);
2923 j = spawns_length - 1;
2925 spawns[i] = spawns[j];
2930 DNPRINTF(SWM_D_SPAWN,
2931 "setspawn: replace #%d %s\n",
2933 free(spawns[i].name);
2934 for (j = 0; j < spawns[i].argc; j++)
2935 free(spawns[i].argv[j]);
2936 free(spawns[i].argv);
2939 /* found case handled */
2945 if (prog->argv == NULL) {
2947 "error: setspawn: cannot find program %s", prog->name);
2952 /* not found: add */
2953 if (spawns_size == 0 || spawns == NULL) {
2955 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
2956 spawns = malloc((size_t)spawns_size *
2957 sizeof(struct spawn_prog));
2958 if (spawns == NULL) {
2959 fprintf(stderr, "setspawn: malloc failed\n");
2963 } else if (spawns_length == spawns_size) {
2965 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
2966 spawns = realloc(spawns, (size_t)spawns_size *
2967 sizeof(struct spawn_prog));
2968 if (spawns == NULL) {
2969 fprintf(stderr, "setspawn: realloc failed\n");
2975 if (spawns_length < spawns_size) {
2976 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
2977 spawns_length, prog->name);
2978 i = spawns_length++;
2981 fprintf(stderr, "spawns array problem?\n");
2982 if (spawns == NULL) {
2983 fprintf(stderr, "spawns array is NULL!\n");
2991 setconfspawn(char *selector, char *value, int flags)
2993 struct spawn_prog *prog;
2994 char *vp, *cp, *word;
2996 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
2997 if ((prog = calloc(1, sizeof *prog)) == NULL)
2998 err(1, "setconfspawn: calloc prog");
2999 prog->name = strdup(selector);
3000 if (prog->name == NULL)
3001 err(1, "setconfspawn prog->name");
3002 if ((cp = vp = strdup(value)) == NULL)
3003 err(1, "setconfspawn: strdup(value) ");
3004 while ((word = strsep(&cp, " \t")) != NULL) {
3005 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
3007 cp += (long)strspn(cp, " \t");
3008 if (strlen(word) > 0) {
3010 prog->argv = realloc(prog->argv,
3011 prog->argc * sizeof(char *));
3012 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
3013 err(1, "setconfspawn: strdup");
3020 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
3027 setconfspawn("term", "xterm", 0);
3028 setconfspawn("screenshot_all", "screenshot.sh full", 0);
3029 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
3030 setconfspawn("lock", "xlock", 0);
3031 setconfspawn("initscr", "initscreen.sh", 0);
3032 setconfspawn("menu", "dmenu_run"
3035 " -nf $bar_font_color"
3037 " -sf $bar_color", 0);
3041 #define SWM_MODNAME_SIZE 32
3042 #define SWM_KEY_WS "\n+ \t"
3044 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
3048 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
3049 if (mod == NULL || ks == NULL) {
3050 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
3053 if (keystr == NULL || strlen(keystr) == 0) {
3054 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
3060 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
3061 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
3063 cp += (long)strspn(cp, SWM_KEY_WS);
3064 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
3066 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
3068 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
3070 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
3072 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
3074 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
3076 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
3077 *mod |= ControlMask;
3079 *ks = XStringToKeysym(name);
3080 XConvertCase(*ks, ks, &uks);
3081 if (ks == NoSymbol) {
3083 "parsekeys: invalid key %s\n",
3089 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
3094 strdupsafe(char *str)
3099 return (strdup(str));
3103 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
3106 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
3107 keyfuncs[kfid].name, spawn_name);
3109 for (i = 0; i < keys_length; i++) {
3110 if (keys[i].mod == mod && keys[i].keysym == ks) {
3111 if (kfid == kf_invalid) {
3114 "setkeybinding: delete #%d %s\n",
3115 i, keyfuncs[keys[i].funcid].name);
3116 free(keys[i].spawn_name);
3117 j = keys_length - 1;
3121 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3124 /* found: replace */
3126 "setkeybinding: replace #%d %s %s\n",
3127 i, keyfuncs[keys[i].funcid].name,
3129 free(keys[i].spawn_name);
3131 keys[i].keysym = ks;
3132 keys[i].funcid = kfid;
3133 keys[i].spawn_name = strdupsafe(spawn_name);
3134 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3139 if (kfid == kf_invalid) {
3141 "error: setkeybinding: cannot find mod/key combination");
3142 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3145 /* not found: add */
3146 if (keys_size == 0 || keys == NULL) {
3148 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
3149 keys = malloc((size_t)keys_size * sizeof(struct key));
3151 fprintf(stderr, "malloc failed\n");
3155 } else if (keys_length == keys_size) {
3157 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
3158 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
3160 fprintf(stderr, "realloc failed\n");
3165 if (keys_length < keys_size) {
3167 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
3168 j, keyfuncs[kfid].name, spawn_name);
3170 keys[j].keysym = ks;
3171 keys[j].funcid = kfid;
3172 keys[j].spawn_name = strdupsafe(spawn_name);
3174 fprintf(stderr, "keys array problem?\n");
3176 fprintf(stderr, "keys array problem\n");
3180 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3184 setconfbinding(char *selector, char *value, int flags)
3186 enum keyfuncid kfid;
3190 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
3191 if (selector == NULL) {
3192 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
3193 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3195 setkeybinding(mod, ks, kfid, NULL);
3200 /* search by key function name */
3201 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
3202 if (strncasecmp(selector, keyfuncs[kfid].name,
3203 SWM_FUNCNAME_LEN) == 0) {
3204 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3206 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3207 setkeybinding(mod, ks, kfid, NULL);
3213 /* search by custom spawn name */
3214 for (i = 0; i < spawns_length; i++) {
3215 if (strcasecmp(selector, spawns[i].name) == 0) {
3216 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3218 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3219 setkeybinding(mod, ks, kf_spawn_custom,
3226 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
3233 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
3234 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
3235 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
3236 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
3237 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
3238 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
3239 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
3240 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
3241 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
3242 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
3243 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
3244 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
3245 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
3246 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
3247 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
3248 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
3249 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
3250 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
3251 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
3252 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
3253 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
3254 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
3255 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
3256 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
3257 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
3258 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
3259 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
3260 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
3261 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
3262 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
3263 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
3264 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
3265 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
3266 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
3267 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
3268 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
3269 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
3270 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
3271 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
3272 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
3273 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
3274 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
3275 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
3276 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
3277 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
3278 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
3279 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
3280 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
3281 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
3282 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
3283 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
3284 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
3285 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
3286 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
3288 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
3293 updatenumlockmask(void)
3296 XModifierKeymap *modmap;
3298 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
3300 modmap = XGetModifierMapping(display);
3301 for (i = 0; i < 8; i++)
3302 for (j = 0; j < modmap->max_keypermod; j++)
3303 if (modmap->modifiermap[i * modmap->max_keypermod + j]
3304 == XKeysymToKeycode(display, XK_Num_Lock))
3305 numlockmask = (1 << i);
3307 XFreeModifiermap(modmap);
3313 unsigned int i, j, k;
3315 unsigned int modifiers[] =
3316 { 0, LockMask, numlockmask, numlockmask | LockMask };
3318 DNPRINTF(SWM_D_MISC, "grabkeys\n");
3319 updatenumlockmask();
3321 for (k = 0; k < ScreenCount(display); k++) {
3322 if (TAILQ_EMPTY(&screens[k].rl))
3324 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
3325 for (i = 0; i < keys_length; i++) {
3326 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
3327 for (j = 0; j < LENGTH(modifiers); j++)
3328 XGrabKey(display, code,
3329 keys[i].mod | modifiers[j],
3330 screens[k].root, True,
3331 GrabModeAsync, GrabModeAsync);
3337 grabbuttons(struct ws_win *win, int focused)
3340 unsigned int modifiers[] =
3341 { 0, LockMask, numlockmask, numlockmask|LockMask };
3343 updatenumlockmask();
3344 XUngrabButton(display, AnyButton, AnyModifier, win->id);
3346 for (i = 0; i < LENGTH(buttons); i++)
3347 if (buttons[i].action == client_click)
3348 for (j = 0; j < LENGTH(modifiers); j++)
3349 XGrabButton(display, buttons[i].button,
3350 buttons[i].mask | modifiers[j],
3351 win->id, False, BUTTONMASK,
3352 GrabModeAsync, GrabModeSync, None,
3355 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
3356 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
3359 const char *quirkname[] = {
3360 "NONE", /* config string for "no value" */
3368 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
3369 #define SWM_Q_WS "\n|+ \t"
3371 parsequirks(char *qstr, unsigned long *quirk)
3381 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
3383 cp += (long)strspn(cp, SWM_Q_WS);
3384 for (i = 0; i < LENGTH(quirkname); i++) {
3385 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
3386 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
3391 *quirk |= 1 << (i-1);
3395 if (i >= LENGTH(quirkname)) {
3396 DNPRINTF(SWM_D_QUIRK,
3397 "parsequirks: invalid quirk [%s]\n", name);
3405 setquirk(const char *class, const char *name, const int quirk)
3410 for (i = 0; i < quirks_length; i++) {
3411 if (!strcmp(quirks[i].class, class) &&
3412 !strcmp(quirks[i].name, name)) {
3415 DNPRINTF(SWM_D_QUIRK,
3416 "setquirk: delete #%d %s:%s\n",
3417 i, quirks[i].class, quirks[i].name);
3418 free(quirks[i].class);
3419 free(quirks[i].name);
3420 j = quirks_length - 1;
3422 quirks[i] = quirks[j];
3426 /* found: replace */
3427 DNPRINTF(SWM_D_QUIRK,
3428 "setquirk: replace #%d %s:%s\n",
3429 i, quirks[i].class, quirks[i].name);
3430 free(quirks[i].class);
3431 free(quirks[i].name);
3432 quirks[i].class = strdup(class);
3433 quirks[i].name = strdup(name);
3434 quirks[i].quirk = quirk;
3441 "error: setquirk: cannot find class/name combination");
3444 /* not found: add */
3445 if (quirks_size == 0 || quirks == NULL) {
3447 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
3448 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
3450 fprintf(stderr, "setquirk: malloc failed\n");
3454 } else if (quirks_length == quirks_size) {
3456 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
3457 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
3459 fprintf(stderr, "setquirk: realloc failed\n");
3464 if (quirks_length < quirks_size) {
3465 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
3466 j = quirks_length++;
3467 quirks[j].class = strdup(class);
3468 quirks[j].name = strdup(name);
3469 quirks[j].quirk = quirk;
3471 fprintf(stderr, "quirks array problem?\n");
3473 fprintf(stderr, "quirks array problem!\n");
3480 setconfquirk(char *selector, char *value, int flags)
3482 char *cp, *class, *name;
3484 unsigned long quirks;
3485 if (selector == NULL)
3487 if ((cp = strchr(selector, ':')) == NULL)
3492 if ((retval = parsequirks(value, &quirks)) == 0)
3493 setquirk(class, name, quirks);
3500 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
3501 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
3502 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
3503 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
3504 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3505 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
3506 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3507 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3508 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3509 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3510 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
3511 setquirk("pcb", "pcb", SWM_Q_FLOAT);
3514 /* conf file stuff */
3515 #define SWM_CONF_FILE "scrotwm.conf"
3517 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_STACK_ENABLED,
3518 SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT, SWM_S_CYCLE_EMPTY,
3519 SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH,
3520 SWM_S_TITLE_CLASS_ENABLED, SWM_S_TITLE_NAME_ENABLED,
3521 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BAR_FONT,
3522 SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM, SWM_S_SS_APP, SWM_S_DIALOG_RATIO
3526 setconfvalue(char *selector, char *value, int flags)
3529 case SWM_S_BAR_DELAY:
3530 bar_delay = atoi(value);
3532 case SWM_S_BAR_ENABLED:
3533 bar_enabled = atoi(value);
3535 case SWM_S_STACK_ENABLED:
3536 stack_enabled = atoi(value);
3538 case SWM_S_CLOCK_ENABLED:
3539 clock_enabled = atoi(value);
3541 case SWM_S_CLOCK_FORMAT:
3542 #ifndef SWM_DENY_CLOCK_FORMAT
3544 if ((clock_format = strdup(value)) == NULL)
3545 err(1, "setconfvalue: clock_format");
3548 case SWM_S_CYCLE_EMPTY:
3549 cycle_empty = atoi(value);
3551 case SWM_S_CYCLE_VISIBLE:
3552 cycle_visible = atoi(value);
3554 case SWM_S_SS_ENABLED:
3555 ss_enabled = atoi(value);
3557 case SWM_S_TERM_WIDTH:
3558 term_width = atoi(value);
3560 case SWM_S_TITLE_CLASS_ENABLED:
3561 title_class_enabled = atoi(value);
3563 case SWM_S_TITLE_NAME_ENABLED:
3564 title_name_enabled = atoi(value);
3566 case SWM_S_FOCUS_MODE:
3567 if (!strcmp(value, "default"))
3568 focus_mode = SWM_FOCUS_DEFAULT;
3569 else if (!strcmp(value, "follow_cursor"))
3570 focus_mode = SWM_FOCUS_FOLLOW;
3571 else if (!strcmp(value, "synergy"))
3572 focus_mode = SWM_FOCUS_SYNERGY;
3574 err(1, "focus_mode");
3576 case SWM_S_DISABLE_BORDER:
3577 disable_border = atoi(value);
3579 case SWM_S_BAR_FONT:
3581 if ((bar_fonts[0] = strdup(value)) == NULL)
3582 err(1, "setconfvalue: bar_font");
3584 case SWM_S_BAR_ACTION:
3586 if ((bar_argv[0] = strdup(value)) == NULL)
3587 err(1, "setconfvalue: bar_action");
3589 case SWM_S_SPAWN_TERM:
3590 free(spawn_term[0]);
3591 if ((spawn_term[0] = strdup(value)) == NULL)
3592 err(1, "setconfvalue: spawn_term");
3596 case SWM_S_DIALOG_RATIO:
3597 dialog_ratio = atof(value);
3598 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
3608 setconfmodkey(char *selector, char *value, int flags)
3610 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
3611 update_modkey(Mod1Mask);
3612 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
3613 update_modkey(Mod2Mask);
3614 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
3615 update_modkey(Mod3Mask);
3616 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
3617 update_modkey(Mod4Mask);
3624 setconfcolor(char *selector, char *value, int flags)
3626 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
3631 setconfregion(char *selector, char *value, int flags)
3633 custom_region(value);
3637 /* config options */
3638 struct config_option {
3640 int (*func)(char*, char*, int);
3643 struct config_option configopt[] = {
3644 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
3645 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
3646 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
3647 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
3648 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
3649 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
3650 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
3651 { "bind", setconfbinding, 0 },
3652 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
3653 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
3654 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
3655 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
3656 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
3657 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
3658 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
3659 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
3660 { "modkey", setconfmodkey, 0 },
3661 { "program", setconfspawn, 0 },
3662 { "quirk", setconfquirk, 0 },
3663 { "region", setconfregion, 0 },
3664 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
3665 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
3666 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
3667 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
3668 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
3669 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
3670 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
3671 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
3676 conf_load(char *filename)
3679 char *line, *cp, *optsub, *optval;
3680 size_t linelen, lineno = 0;
3681 int wordlen, i, optind;
3682 struct config_option *opt;
3684 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
3686 if (filename == NULL) {
3687 fprintf(stderr, "conf_load: no filename\n");
3690 if ((config = fopen(filename, "r")) == NULL) {
3691 warn("conf_load: fopen");
3695 while (!feof(config)) {
3696 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
3699 err(1, "%s", filename);
3704 cp += strspn(cp, " \t\n"); /* eat whitespace */
3705 if (cp[0] == '\0') {
3710 /* get config option */
3711 wordlen = strcspn(cp, "=[ \t\n");
3713 warnx("%s: line %zd: no option found",
3718 for (i = 0; i < LENGTH(configopt); i++) {
3719 opt = &configopt[i];
3720 if (!strncasecmp(cp, opt->optname, wordlen) &&
3721 strlen(opt->optname) == wordlen) {
3727 warnx("%s: line %zd: unknown option %.*s",
3728 filename, lineno, wordlen, cp);
3732 cp += strspn(cp, " \t\n"); /* eat whitespace */
3733 /* get [selector] if any */
3737 wordlen = strcspn(cp, "]");
3740 warnx("%s: line %zd: syntax error",
3744 asprintf(&optsub, "%.*s", wordlen, cp);
3747 cp += strspn(cp, "] \t\n"); /* eat trailing */
3749 cp += strspn(cp, "= \t\n"); /* eat trailing */
3751 optval = strdup(cp);
3752 /* call function to deal with it all */
3753 if (configopt[optind].func(optsub, optval,
3754 configopt[optind].funcflags) != 0) {
3755 fprintf(stderr, "%s line %zd: %s\n",
3756 filename, lineno, line);
3757 errx(1, "%s: line %zd: invalid data for %s",
3758 filename, lineno, configopt[optind].optname);
3766 DNPRINTF(SWM_D_CONF, "conf_load end\n");
3772 set_child_transient(struct ws_win *win)
3774 struct ws_win *parent;
3776 parent = find_window(win->transient);
3778 parent->child_trans = win;
3782 manage_window(Window id)
3785 struct workspace *ws;
3786 struct ws_win *win, *ww;
3787 int format, i, ws_idx, n, border_me = 0;
3788 unsigned long nitems, bytes;
3789 Atom ws_idx_atom = 0, type;
3790 Atom *prot = NULL, *pp;
3791 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
3792 struct swm_region *r;
3797 if ((win = find_window(id)) != NULL)
3798 return (win); /* already being managed */
3800 /* see if we are on the unmanaged list */
3801 if ((win = find_unmanaged_window(id)) != NULL) {
3802 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
3804 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
3805 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
3807 set_child_transient(win);
3811 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
3812 errx(1, "calloc: failed to allocate memory for new window");
3814 /* Get all the window data in one shot */
3815 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3817 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
3818 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
3819 XGetWindowAttributes(display, id, &win->wa);
3820 XGetWMNormalHints(display, id, &win->sh, &mask);
3821 XGetTransientForHint(display, id, &trans);
3823 win->transient = trans;
3824 set_child_transient(win);
3825 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
3826 win->id, win->transient);
3828 /* get supported protocols */
3829 if (XGetWMProtocols(display, id, &prot, &n)) {
3830 for (i = 0, pp = prot; i < n; i++, pp++) {
3831 if (*pp == takefocus)
3832 win->take_focus = 1;
3834 win->can_delete = 1;
3841 * Figure out where to put the window. If it was previously assigned to
3842 * a workspace (either by spawn() or manually moving), and isn't
3843 * transient, * put it in the same workspace
3845 r = root_to_region(win->wa.root);
3846 if (prop && win->transient == 0) {
3847 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
3848 ws_idx = strtonum(prop, 0, 9, &errstr);
3850 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
3853 ws = &r->s->ws[ws_idx];
3856 /* this should launch transients in the same ws as parent */
3858 if ((ww = find_window(trans)) != NULL)
3865 "fix this bug mcbride\n");
3870 /* set up the window layout */
3873 win->s = r->s; /* this never changes */
3874 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
3876 win->g.w = win->wa.width;
3877 win->g.h = win->wa.height;
3878 win->g.x = win->wa.x;
3879 win->g.y = win->wa.y;
3880 win->g_floatvalid = 0;
3881 win->floatmaxed = 0;
3883 /* Set window properties so we can remember this after reincarnation */
3884 if (ws_idx_atom && prop == NULL &&
3885 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
3886 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3888 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3889 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3893 if (XGetClassHint(display, win->id, &win->ch)) {
3894 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
3895 win->ch.res_class, win->ch.res_name);
3897 /* java is retarded so treat it special */
3898 if (strstr(win->ch.res_name, "sun-awt")) {
3903 for (i = 0; i < quirks_length; i++){
3904 if (!strcmp(win->ch.res_class, quirks[i].class) &&
3905 !strcmp(win->ch.res_name, quirks[i].name)) {
3906 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
3907 win->ch.res_class, win->ch.res_name);
3908 if (quirks[i].quirk & SWM_Q_FLOAT) {
3912 win->quirks = quirks[i].quirk;
3917 /* alter window position if quirky */
3918 if (win->quirks & SWM_Q_ANYWHERE) {
3919 win->manual = 1; /* don't center the quirky windows */
3920 bzero(&wc, sizeof wc);
3922 if (win->g.y < bar_height) {
3923 win->g.y = wc.y = bar_height;
3926 if (win->g.w + win->g.x > WIDTH(r)) {
3927 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
3933 /* Reset font sizes (the bruteforce way; no default keybinding). */
3934 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
3935 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3936 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3937 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3938 fake_keypress(win, XK_KP_Add, ShiftMask);
3943 bzero(&wc, sizeof wc);
3944 wc.border_width = 1;
3945 mask = CWBorderWidth;
3946 XConfigureWindow(display, win->id, mask, &wc);
3950 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
3951 PropertyChangeMask | StructureNotifyMask);
3953 set_win_state(win, NormalState);
3955 /* floaters need to be mapped if they are in the current workspace */
3956 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
3957 XMapRaised(display, win->id);
3963 free_window(struct ws_win *win)
3965 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
3970 /* needed for restart wm */
3971 set_win_state(win, WithdrawnState);
3973 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
3975 if (win->ch.res_class)
3976 XFree(win->ch.res_class);
3977 if (win->ch.res_name)
3978 XFree(win->ch.res_name);
3983 memset(win, 0xff, sizeof *win); /* XXX kill later */
3989 unmanage_window(struct ws_win *win)
3991 struct ws_win *parent;
3996 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
3998 if (win->transient) {
3999 parent = find_window(win->transient);
4001 parent->child_trans = NULL;
4004 /* work around for mplayer going full screen */
4008 TAILQ_REMOVE(&win->ws->winlist, win, entry);
4009 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
4015 focus_magic(struct ws_win *win, int do_trans)
4017 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu %d\n", WINID(win), do_trans);
4022 if (do_trans == SWM_F_TRANSIENT && win->child_trans) {
4023 /* win = parent & has a transient so focus on that */
4025 focus_win(win->child_trans);
4026 if (win->child_trans->take_focus)
4027 client_msg(win, takefocus);
4029 focus_win(win->child_trans);
4030 if (win->child_trans->take_focus)
4031 client_msg(win->child_trans, takefocus);
4036 if (win->take_focus)
4037 client_msg(win, takefocus);
4044 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
4052 XKeyEvent *ev = &e->xkey;
4054 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
4056 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
4057 for (i = 0; i < keys_length; i++)
4058 if (keysym == keys[i].keysym
4059 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
4060 && keyfuncs[keys[i].funcid].func) {
4061 if (keys[i].funcid == kf_spawn_custom)
4063 root_to_region(ev->root),
4064 &(keyfuncs[keys[i].funcid].args),
4068 keyfuncs[keys[i].funcid].func(
4069 root_to_region(ev->root),
4070 &(keyfuncs[keys[i].funcid].args)
4076 buttonpress(XEvent *e)
4080 XButtonPressedEvent *ev = &e->xbutton;
4082 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
4084 action = root_click;
4085 if ((win = find_window(ev->window)) == NULL)
4088 focus_magic(win, SWM_F_TRANSIENT);
4089 action = client_click;
4091 for (i = 0; i < LENGTH(buttons); i++)
4092 if (action == buttons[i].action && buttons[i].func &&
4093 buttons[i].button == ev->button &&
4094 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
4095 buttons[i].func(win, &buttons[i].args);
4099 configurerequest(XEvent *e)
4101 XConfigureRequestEvent *ev = &e->xconfigurerequest;
4106 if ((win = find_window(ev->window)) == NULL)
4107 if ((win = find_unmanaged_window(ev->window)) == NULL)
4111 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
4113 bzero(&wc, sizeof wc);
4116 wc.width = ev->width;
4117 wc.height = ev->height;
4118 wc.border_width = ev->border_width;
4119 wc.sibling = ev->above;
4120 wc.stack_mode = ev->detail;
4121 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
4123 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
4125 if (win->floating) {
4126 if (ev->value_mask & CWX)
4128 if (ev->value_mask & CWY)
4130 if (ev->value_mask & CWWidth)
4131 win->g.w = ev->width;
4132 if (ev->value_mask & CWHeight)
4133 win->g.h = ev->height;
4140 configurenotify(XEvent *e)
4145 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
4146 e->xconfigure.window);
4148 win = find_window(e->xconfigure.window);
4150 XGetWMNormalHints(display, win->id, &win->sh, &mask);
4158 destroynotify(XEvent *e)
4161 XDestroyWindowEvent *ev = &e->xdestroywindow;
4163 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
4165 if ((win = find_window(ev->window)) == NULL) {
4166 if ((win = find_unmanaged_window(ev->window)) == NULL)
4172 /* make sure we focus on something */
4175 unmanage_window(win);
4181 enternotify(XEvent *e)
4183 XCrossingEvent *ev = &e->xcrossing;
4188 Window focus_return;
4189 int revert_to_return;
4191 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
4192 "%lu subwindow %lu same_screen %d focus %d state %d\n",
4193 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
4194 ev->same_screen, ev->focus, ev->state);
4196 switch (focus_mode) {
4197 case SWM_FOCUS_DEFAULT:
4198 if (QLength(display)) {
4199 DNPRINTF(SWM_D_EVENT, "ignore enternotify %d\n",
4204 case SWM_FOCUS_FOLLOW:
4206 case SWM_FOCUS_SYNERGY:
4209 * all these checks need to be in this order because the
4210 * XCheckTypedWindowEvent relies on weeding out the previous events
4212 * making this code an option would enable a follow mouse for focus
4217 * state is set when we are switching workspaces and focus is set when
4218 * the window or a subwindow already has focus (occurs during restart).
4220 * Only honor the focus flag if last_focus_event is not FocusOut,
4221 * this allows scrotwm to continue to control focus when another
4222 * program is also playing with it.
4224 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
4225 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
4230 * happens when a window is created or destroyed and the border
4231 * crosses the mouse pointer and when switching ws
4233 * we need the subwindow test to see if we came from root in order
4234 * to give focus to floaters
4236 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
4237 ev->subwindow == 0) {
4238 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
4242 /* this window already has focus */
4243 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
4244 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
4248 /* this window is being deleted or moved to another ws */
4249 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
4251 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
4252 XPutBackEvent(display, &cne);
4256 if ((win = find_window(ev->window)) == NULL) {
4257 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
4262 * In fullstack kill all enters unless they come from a different ws
4263 * (i.e. another region) or focus has been grabbed externally.
4265 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
4266 last_focus_event != FocusOut) {
4267 XGetInputFocus(display, &focus_return, &revert_to_return);
4268 if ((w = find_window(focus_return)) == NULL ||
4270 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
4278 if ((win = find_window(ev->window)) == NULL) {
4279 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
4284 * if we have more enternotifies let them handle it in due time
4286 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
4287 DNPRINTF(SWM_D_EVENT,
4288 "ignoring enternotify: got more enternotify\n");
4289 XPutBackEvent(display, &cne);
4293 focus_magic(win, SWM_F_TRANSIENT);
4296 /* lets us use one switch statement for arbitrary mode/detail combinations */
4297 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
4300 focusevent(XEvent *e)
4304 u_int32_t mode_detail;
4305 XFocusChangeEvent *ev = &e->xfocus;
4307 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
4308 ev->type == FocusIn ? "entering" : "leaving",
4309 ev->window, ev->mode, ev->detail);
4311 if (last_focus_event == ev->type) {
4312 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
4316 last_focus_event = ev->type;
4317 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
4319 switch (mode_detail) {
4320 /* synergy client focus operations */
4321 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
4322 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
4324 /* synergy server focus operations */
4325 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
4327 /* Entering applications like rdesktop that mangle the pointer */
4328 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
4330 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
4331 XSetWindowBorder(display, win->id,
4332 win->ws->r->s->c[ev->type == FocusIn ?
4333 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
4336 fprintf(stderr, "ignoring focusevent\n");
4337 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
4344 mapnotify(XEvent *e)
4347 XMapEvent *ev = &e->xmap;
4349 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
4351 win = manage_window(ev->window);
4353 set_win_state(win, NormalState);
4357 mappingnotify(XEvent *e)
4359 XMappingEvent *ev = &e->xmapping;
4361 XRefreshKeyboardMapping(ev);
4362 if (ev->request == MappingKeyboard)
4367 maprequest(XEvent *e)
4370 struct swm_region *r;
4371 XWindowAttributes wa;
4372 XMapRequestEvent *ev = &e->xmaprequest;
4374 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
4375 e->xmaprequest.window);
4377 if (!XGetWindowAttributes(display, ev->window, &wa))
4379 if (wa.override_redirect)
4382 win = manage_window(e->xmaprequest.window);
4384 return; /* can't happen */
4388 /* make new win focused */
4389 r = root_to_region(win->wa.root);
4390 if (win->ws == r->ws)
4391 focus_magic(win, SWM_F_GENERIC);
4395 propertynotify(XEvent *e)
4398 XPropertyEvent *ev = &e->xproperty;
4400 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
4403 if (ev->state == PropertyDelete)
4404 return; /* ignore */
4405 win = find_window(ev->window);
4410 case XA_WM_NORMAL_HINTS:
4413 XGetWMNormalHints(display, win->id, &win->sh, &mask);
4414 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
4415 if (win->sh.flags & PMinSize) {
4416 win->g.w = win->sh.min_width;
4417 win->g.h = win->sh.min_height;
4418 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
4420 XMoveResizeWindow(display, win->id,
4421 win->g.x, win->g.y, win->g.w, win->g.h);
4430 unmapnotify(XEvent *e)
4434 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
4436 /* determine if we need to help unmanage this window */
4437 win = find_window(e->xunmap.window);
4441 if (getstate(e->xunmap.window) == NormalState) {
4442 unmanage_window(win);
4448 visibilitynotify(XEvent *e)
4451 struct swm_region *r;
4453 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
4454 e->xvisibility.window);
4455 if (e->xvisibility.state == VisibilityUnobscured)
4456 for (i = 0; i < ScreenCount(display); i++)
4457 TAILQ_FOREACH(r, &screens[i].rl, entry)
4458 if (e->xvisibility.window == r->bar_window)
4463 xerror_start(Display *d, XErrorEvent *ee)
4470 xerror(Display *d, XErrorEvent *ee)
4472 /* fprintf(stderr, "error: %p %p\n", display, ee); */
4480 xerrorxlib = XSetErrorHandler(xerror_start);
4482 /* this causes an error if some other window manager is running */
4483 XSelectInput(display, DefaultRootWindow(display),
4484 SubstructureRedirectMask);
4485 XSync(display, False);
4489 XSetErrorHandler(xerror);
4490 XSync(display, False);
4495 new_region(struct swm_screen *s, int x, int y, int w, int h)
4497 struct swm_region *r, *n;
4498 struct workspace *ws = NULL;
4501 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
4502 s->idx, w, h, x, y);
4504 /* remove any conflicting regions */
4505 n = TAILQ_FIRST(&s->rl);
4508 n = TAILQ_NEXT(r, entry);
4509 if (X(r) < (x + w) &&
4510 (X(r) + WIDTH(r)) > x &&
4512 (Y(r) + HEIGHT(r)) > y) {
4513 if (r->ws->r != NULL)
4514 r->ws->old_r = r->ws->r;
4516 XDestroyWindow(display, r->bar_window);
4517 TAILQ_REMOVE(&s->rl, r, entry);
4518 TAILQ_INSERT_TAIL(&s->orl, r, entry);
4522 /* search old regions for one to reuse */
4524 /* size + location match */
4525 TAILQ_FOREACH(r, &s->orl, entry)
4526 if (X(r) == x && Y(r) == y &&
4527 HEIGHT(r) == h && WIDTH(r) == w)
4531 TAILQ_FOREACH(r, &s->orl, entry)
4532 if (HEIGHT(r) == h && WIDTH(r) == w)
4536 TAILQ_REMOVE(&s->orl, r, entry);
4537 /* try to use old region's workspace */
4538 if (r->ws->r == NULL)
4541 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
4542 errx(1, "calloc: failed to allocate memory for screen");
4544 /* if we don't have a workspace already, find one */
4546 for (i = 0; i < SWM_WS_MAX; i++)
4547 if (s->ws[i].r == NULL) {
4554 errx(1, "no free workspaces\n");
4564 TAILQ_INSERT_TAIL(&s->rl, r, entry);
4570 #ifdef SWM_XRR_HAS_CRTC
4572 XRRScreenResources *sr;
4575 #endif /* SWM_XRR_HAS_CRTC */
4576 struct swm_region *r;
4579 if (i >= ScreenCount(display))
4580 errx(1, "invalid screen");
4582 /* remove any old regions */
4583 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
4584 r->ws->old_r = r->ws->r = NULL;
4585 XDestroyWindow(display, r->bar_window);
4586 TAILQ_REMOVE(&screens[i].rl, r, entry);
4587 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
4590 /* map virtual screens onto physical screens */
4591 #ifdef SWM_XRR_HAS_CRTC
4593 if (xrandr_support) {
4594 sr = XRRGetScreenResources(display, screens[i].root);
4596 new_region(&screens[i], 0, 0,
4597 DisplayWidth(display, i),
4598 DisplayHeight(display, i));
4602 for (c = 0, ci = NULL; c < ncrtc; c++) {
4603 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
4604 if (ci->noutput == 0)
4608 if (ci != NULL && ci->mode == None)
4609 new_region(&screens[i], 0, 0,
4610 DisplayWidth(display, i),
4611 DisplayHeight(display, i));
4613 new_region(&screens[i],
4614 ci->x, ci->y, ci->width, ci->height);
4617 XRRFreeCrtcInfo(ci);
4618 XRRFreeScreenResources(sr);
4620 #endif /* SWM_XRR_HAS_CRTC */
4622 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
4623 DisplayHeight(display, i));
4628 screenchange(XEvent *e) {
4629 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
4630 struct swm_region *r;
4633 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
4635 if (!XRRUpdateConfiguration(e))
4638 /* silly event doesn't include the screen index */
4639 for (i = 0; i < ScreenCount(display); i++)
4640 if (screens[i].root == xe->root)
4642 if (i >= ScreenCount(display))
4643 errx(1, "screenchange: screen not found\n");
4645 /* brute force for now, just re-enumerate the regions */
4648 /* add bars to all regions */
4649 for (i = 0; i < ScreenCount(display); i++)
4650 TAILQ_FOREACH(r, &screens[i].rl, entry)
4658 Window d1, d2, *wins = NULL;
4659 XWindowAttributes wa;
4662 int errorbase, major, minor;
4663 struct workspace *ws;
4667 if ((screens = calloc(ScreenCount(display),
4668 sizeof(struct swm_screen))) == NULL)
4669 errx(1, "calloc: screens");
4671 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4673 /* initial Xrandr setup */
4674 xrandr_support = XRRQueryExtension(display,
4675 &xrandr_eventbase, &errorbase);
4677 if (XRRQueryVersion(display, &major, &minor) && major < 1)
4680 /* map physical screens */
4681 for (i = 0; i < ScreenCount(display); i++) {
4682 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
4684 TAILQ_INIT(&screens[i].rl);
4685 TAILQ_INIT(&screens[i].orl);
4686 screens[i].root = RootWindow(display, i);
4688 /* set default colors */
4689 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
4690 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
4691 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
4692 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
4693 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
4695 /* set default cursor */
4696 XDefineCursor(display, screens[i].root,
4697 XCreateFontCursor(display, XC_left_ptr));
4699 /* init all workspaces */
4700 /* XXX these should be dynamically allocated too */
4701 for (j = 0; j < SWM_WS_MAX; j++) {
4702 ws = &screens[i].ws[j];
4707 TAILQ_INIT(&ws->winlist);
4708 TAILQ_INIT(&ws->unmanagedlist);
4710 for (k = 0; layouts[k].l_stack != NULL; k++)
4711 if (layouts[k].l_config != NULL)
4712 layouts[k].l_config(ws,
4713 SWM_ARG_ID_STACKINIT);
4714 ws->cur_layout = &layouts[0];
4716 /* grab existing windows (before we build the bars)*/
4717 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
4723 XRRSelectInput(display, screens[i].root,
4724 RRScreenChangeNotifyMask);
4726 /* attach windows to a region */
4727 /* normal windows */
4728 for (j = 0; j < no; j++) {
4729 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4730 wa.override_redirect ||
4731 XGetTransientForHint(display, wins[j], &d1))
4734 state = getstate(wins[j]);
4735 manage = state == IconicState;
4736 if (wa.map_state == IsViewable || manage)
4737 manage_window(wins[j]);
4739 /* transient windows */
4740 for (j = 0; j < no; j++) {
4741 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4742 wa.override_redirect)
4745 state = getstate(wins[j]);
4746 manage = state == IconicState;
4747 if (XGetTransientForHint(display, wins[j], &d1) &&
4749 manage_window(wins[j]);
4761 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
4763 err(1, "setup_globals: strdup");
4764 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
4766 err(1, "setup_globals: strdup");
4767 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
4769 err(1, "setup_globals: strdup");
4770 if ((spawn_term[0] = strdup("xterm")) == NULL)
4771 err(1, "setup_globals: strdup");
4772 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
4780 Atom netwmcheck, netwmname, utf8_string;
4783 /* work around sun jdk bugs, code from wmname */
4784 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
4785 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
4786 utf8_string = XInternAtom(display, "UTF8_STRING", False);
4787 for (i = 0; i < ScreenCount(display); i++) {
4788 root = screens[i].root;
4789 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
4790 PropModeReplace, (unsigned char *)&root, 1);
4791 XChangeProperty(display, root, netwmname, utf8_string, 8,
4792 PropModeReplace, "LG3D", strlen("LG3D"));
4797 main(int argc, char *argv[])
4800 struct swm_region *r, *rr;
4801 struct ws_win *winfocus = NULL;
4804 char conf[PATH_MAX], *cfile = NULL;
4811 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
4812 SWM_VERSION, cvstag);
4813 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
4814 warnx("no locale support");
4816 if (!(display = XOpenDisplay(0)))
4817 errx(1, "can not open display");
4820 errx(1, "other wm running");
4822 /* handle some signale */
4823 installsignal(SIGINT, "INT");
4824 installsignal(SIGHUP, "HUP");
4825 installsignal(SIGQUIT, "QUIT");
4826 installsignal(SIGTERM, "TERM");
4827 installsignal(SIGCHLD, "CHLD");
4829 astate = XInternAtom(display, "WM_STATE", False);
4830 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
4831 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
4832 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
4834 /* look for local and global conf file */
4835 pwd = getpwuid(getuid());
4837 errx(1, "invalid user %d", getuid());
4845 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
4846 if (stat(conf, &sb) != -1) {
4847 if (S_ISREG(sb.st_mode))
4850 /* try global conf file */
4851 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
4852 if (!stat(conf, &sb))
4853 if (S_ISREG(sb.st_mode))
4859 /* setup all bars */
4860 for (i = 0; i < ScreenCount(display); i++)
4861 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4862 if (winfocus == NULL)
4863 winfocus = TAILQ_FIRST(&r->ws->winlist);
4867 /* set some values to work around bad programs */
4875 xfd = ConnectionNumber(display);
4877 while (XPending(display)) {
4878 XNextEvent(display, &e);
4881 if (e.type < LASTEvent) {
4883 if (handler[e.type])
4884 handler[e.type](&e);
4886 DNPRINTF(SWM_D_EVENT,
4887 "win: %lu unknown event: %d\n",
4888 e.xany.window, e.type);
4890 switch (e.type - xrandr_eventbase) {
4891 case RRScreenChangeNotify:
4895 DNPRINTF(SWM_D_EVENT,
4896 "win: %lu unknown xrandr event: "
4897 "%d\n", e.xany.window, e.type);
4903 /* if we are being restarted go focus on first window */
4905 rr = winfocus->ws->r;
4907 /* not a visible window */
4911 /* move pointer to first screen if multi screen */
4912 if (ScreenCount(display) > 1 || outputs > 1)
4913 XWarpPointer(display, None, rr->s[0].root,
4914 0, 0, 0, 0, rr->g.x,
4915 rr->g.y + bar_enabled ? bar_height : 0);
4917 a.id = SWM_ARG_ID_FOCUSCUR;
4927 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
4929 DNPRINTF(SWM_D_MISC, "select failed");
4930 if (restart_wm == 1)
4931 restart(NULL, NULL);
4941 XFreeGC(display, bar_gc);
4942 XCloseDisplay(display);