3 * Copyright (c) 2009-2010 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
5 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
6 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Much code and ideas taken from dwm under the following license:
22 * MIT/X Consortium License
24 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
25 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
26 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
27 * 2007 Premysl Hruby <dfenze at gmail dot com>
28 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
29 * 2007 Christof Musik <christof at sendfax dot de>
30 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
31 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
32 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
34 * Permission is hereby granted, free of charge, to any person obtaining a
35 * copy of this software and associated documentation files (the "Software"),
36 * to deal in the Software without restriction, including without limitation
37 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
38 * and/or sell copies of the Software, and to permit persons to whom the
39 * Software is furnished to do so, subject to the following conditions:
41 * The above copyright notice and this permission notice shall be included in
42 * all copies or substantial portions of the Software.
44 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
47 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
49 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
50 * DEALINGS IN THE SOFTWARE.
53 static const char *cvstag = "$scrotwm$";
55 #define SWM_VERSION "0.9.30"
72 #include <sys/types.h>
76 #include <sys/queue.h>
77 #include <sys/param.h>
78 #include <sys/select.h>
80 #include <X11/cursorfont.h>
81 #include <X11/keysym.h>
82 #include <X11/Xatom.h>
84 #include <X11/Xproto.h>
85 #include <X11/Xutil.h>
86 #include <X11/extensions/Xrandr.h>
93 # error XRandR versions less than 1.0 are not supported
98 #define SWM_XRR_HAS_CRTC
102 /* #define SWM_DEBUG */
104 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
105 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
106 #define SWM_D_MISC 0x0001
107 #define SWM_D_EVENT 0x0002
108 #define SWM_D_WS 0x0004
109 #define SWM_D_FOCUS 0x0008
110 #define SWM_D_MOVE 0x0010
111 #define SWM_D_STACK 0x0020
112 #define SWM_D_MOUSE 0x0040
113 #define SWM_D_PROP 0x0080
114 #define SWM_D_CLASS 0x0100
115 #define SWM_D_KEY 0x0200
116 #define SWM_D_QUIRK 0x0400
117 #define SWM_D_SPAWN 0x0800
118 #define SWM_D_EVENTQ 0x1000
119 #define SWM_D_CONF 0x2000
121 u_int32_t swm_debug = 0
138 #define DPRINTF(x...)
139 #define DNPRINTF(n,x...)
142 #define LENGTH(x) (sizeof x / sizeof x[0])
143 #define MODKEY Mod1Mask
144 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
145 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
146 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
147 #define SWM_PROPLEN (16)
148 #define SWM_FUNCNAME_LEN (32)
149 #define SWM_KEYS_LEN (255)
150 #define SWM_QUIRK_LEN (64)
151 #define X(r) (r)->g.x
152 #define Y(r) (r)->g.y
153 #define WIDTH(r) (r)->g.w
154 #define HEIGHT(r) (r)->g.h
155 #define SWM_MAX_FONT_STEPS (3)
156 #define WINID(w) (w ? w->id : 0)
158 #define SWM_FOCUS_DEFAULT (0)
159 #define SWM_FOCUS_SYNERGY (1)
160 #define SWM_FOCUS_FOLLOW (2)
163 #define SWM_LIB "/usr/local/lib/libswmhack.so"
171 volatile sig_atomic_t running = 1;
172 volatile sig_atomic_t restart_wm = 0;
174 int last_focus_event = FocusOut;
175 int (*xerrorxlib)(Display *, XErrorEvent *);
179 int xrandr_eventbase;
180 unsigned int numlockmask = 0;
184 int cycle_visible = 0;
186 int font_adjusted = 0;
187 unsigned int mod_key = MODKEY;
190 double dialog_ratio = .6;
192 #define SWM_BAR_MAX (256)
193 char *bar_argv[] = { NULL, NULL };
195 char bar_ext[SWM_BAR_MAX];
196 char bar_vertext[SWM_BAR_MAX];
198 sig_atomic_t bar_alarm = 0;
201 int bar_border_width = 1;
202 int bar_at_bottom = 0;
204 int bar_extra_running = 0;
207 int stack_enabled = 1;
208 int clock_enabled = 1;
209 char *clock_format = NULL;
210 int title_name_enabled = 0;
211 int title_class_enabled = 0;
212 int window_name_enabled = 0;
213 int focus_mode = SWM_FOCUS_DEFAULT;
214 int disable_border = 0;
215 int border_width = 1;
221 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
222 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
224 #define SWM_MENU_FN (2)
225 #define SWM_MENU_NB (4)
226 #define SWM_MENU_NF (6)
227 #define SWM_MENU_SB (8)
228 #define SWM_MENU_SF (10)
230 /* layout manager data */
231 struct swm_geometry {
241 /* virtual "screens" */
243 TAILQ_ENTRY(swm_region) entry;
244 struct swm_geometry g;
245 struct workspace *ws; /* current workspace on this region */
246 struct workspace *ws_prior; /* prior workspace on this region */
247 struct swm_screen *s; /* screen idx */
250 TAILQ_HEAD(swm_region_list, swm_region);
253 TAILQ_ENTRY(ws_win) entry;
256 struct ws_win *child_trans; /* transient child window */
257 struct swm_geometry g; /* current geometry */
258 struct swm_geometry g_float; /* geometry when floating */
259 struct swm_geometry rg_float; /* region geom when floating */
260 int g_floatvalid; /* flag: geometry in g_float is valid */
261 int floatmaxed; /* flag: floater was maxed in max_stack */
264 unsigned int ewmh_flags;
265 int font_size_boundary[SWM_MAX_FONT_STEPS];
271 unsigned long quirks;
272 struct workspace *ws; /* always valid */
273 struct swm_screen *s; /* always valid, never changes */
274 XWindowAttributes wa;
278 TAILQ_HEAD(ws_win_list, ws_win);
280 /* layout handlers */
282 void vertical_config(struct workspace *, int);
283 void vertical_stack(struct workspace *, struct swm_geometry *);
284 void horizontal_config(struct workspace *, int);
285 void horizontal_stack(struct workspace *, struct swm_geometry *);
286 void max_stack(struct workspace *, struct swm_geometry *);
288 struct ws_win *find_window(Window);
290 void grabbuttons(struct ws_win *, int);
291 void new_region(struct swm_screen *, int, int, int, int);
292 void unmanage_window(struct ws_win *);
293 long getstate(Window);
296 void (*l_stack)(struct workspace *, struct swm_geometry *);
297 void (*l_config)(struct workspace *, int);
299 #define SWM_L_FOCUSPREV (1<<0)
300 #define SWM_L_MAPONFOCUS (1<<1)
303 /* stack, configure */
304 { vertical_stack, vertical_config, 0, "[|]" },
305 { horizontal_stack, horizontal_config, 0, "[-]" },
307 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, "[ ]"},
308 { NULL, NULL, 0, NULL },
311 /* position of max_stack mode in the layouts array */
312 #define SWM_MAX_STACK 2
314 #define SWM_H_SLICE (32)
315 #define SWM_V_SLICE (32)
317 /* define work spaces */
319 int idx; /* workspace index */
320 struct layout *cur_layout; /* current layout handlers */
321 struct ws_win *focus; /* may be NULL */
322 struct ws_win *focus_prev; /* may be NULL */
323 struct swm_region *r; /* may be NULL */
324 struct swm_region *old_r; /* may be NULL */
325 struct ws_win_list winlist; /* list of windows in ws */
326 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
330 int horizontal_msize;
332 int horizontal_stacks;
339 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
340 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
342 /* physical screen mapping */
343 #define SWM_WS_MAX (10)
345 int idx; /* screen index */
346 struct swm_region_list rl; /* list of regions on this screen */
347 struct swm_region_list orl; /* list of old regions */
349 struct workspace ws[SWM_WS_MAX];
355 } c[SWM_S_COLOR_MAX];
357 struct swm_screen *screens;
360 /* args to functions */
363 #define SWM_ARG_ID_FOCUSNEXT (0)
364 #define SWM_ARG_ID_FOCUSPREV (1)
365 #define SWM_ARG_ID_FOCUSMAIN (2)
366 #define SWM_ARG_ID_FOCUSCUR (4)
367 #define SWM_ARG_ID_SWAPNEXT (10)
368 #define SWM_ARG_ID_SWAPPREV (11)
369 #define SWM_ARG_ID_SWAPMAIN (12)
370 #define SWM_ARG_ID_MOVELAST (13)
371 #define SWM_ARG_ID_MASTERSHRINK (20)
372 #define SWM_ARG_ID_MASTERGROW (21)
373 #define SWM_ARG_ID_MASTERADD (22)
374 #define SWM_ARG_ID_MASTERDEL (23)
375 #define SWM_ARG_ID_STACKRESET (30)
376 #define SWM_ARG_ID_STACKINIT (31)
377 #define SWM_ARG_ID_CYCLEWS_UP (40)
378 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
379 #define SWM_ARG_ID_CYCLESC_UP (42)
380 #define SWM_ARG_ID_CYCLESC_DOWN (43)
381 #define SWM_ARG_ID_STACKINC (50)
382 #define SWM_ARG_ID_STACKDEC (51)
383 #define SWM_ARG_ID_SS_ALL (60)
384 #define SWM_ARG_ID_SS_WINDOW (61)
385 #define SWM_ARG_ID_DONTCENTER (70)
386 #define SWM_ARG_ID_CENTER (71)
387 #define SWM_ARG_ID_KILLWINDOW (80)
388 #define SWM_ARG_ID_DELETEWINDOW (81)
392 void focus(struct swm_region *, union arg *);
393 void focus_magic(struct ws_win *, int);
394 #define SWM_F_GENERIC (0)
395 #define SWM_F_TRANSIENT (1)
401 #define SWM_Q_FLOAT (1<<0) /* float this window */
402 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
403 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
404 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
405 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
407 int quirks_size = 0, quirks_length = 0;
408 struct quirk *quirks = NULL;
411 * Supported EWMH hints should be added to
412 * both the enum and the ewmh array
414 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
415 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
416 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
417 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
418 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
419 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
420 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
421 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
422 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
423 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
429 } ewmh[SWM_EWMH_HINT_MAX] = {
430 /* must be in same order as in the enum */
431 {"_NET_ACTIVE_WINDOW", None},
432 {"_NET_MOVERESIZE_WINDOW", None},
433 {"_NET_CLOSE_WINDOW", None},
434 {"_NET_WM_WINDOW_TYPE", None},
435 {"_NET_WM_WINDOW_TYPE_DOCK", None},
436 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
437 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
438 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
439 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
440 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
441 {"_NET_WM_STATE", None},
442 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
443 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
444 {"_NET_WM_STATE_SKIP_TASKBAR", None},
445 {"_NET_WM_STATE_SKIP_PAGER", None},
446 {"_NET_WM_STATE_HIDDEN", None},
447 {"_NET_WM_STATE_ABOVE", None},
448 {"_SWM_WM_STATE_MANUAL", None},
449 {"_NET_WM_STATE_FULLSCREEN", None},
450 {"_NET_WM_ALLOWED_ACTIONS", None},
451 {"_NET_WM_ACTION_MOVE", None},
452 {"_NET_WM_ACTION_RESIZE", None},
453 {"_NET_WM_ACTION_FULLSCREEN", None},
454 {"_NET_WM_ACTION_CLOSE", None},
457 void store_float_geom(struct ws_win *win, struct swm_region *r);
458 int floating_toggle_win(struct ws_win *win);
461 get_property(Window id, Atom atom, long count, Atom type,
462 unsigned long *n, unsigned char **data)
465 unsigned long tmp, extra;
466 unsigned long *nitems;
469 nitems = n != NULL ? n : &tmp;
470 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
471 &real, &format, nitems, &extra, data);
473 if (status != Success)
487 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
489 for (i = 0; i < LENGTH(ewmh); i++)
490 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
492 for (i = 0; i < ScreenCount(display); i++) {
493 /* Support check window will be created by workaround(). */
495 /* Report supported atoms */
496 XDeleteProperty(display, screens[i].root, sup_list);
497 for (j = 0; j < LENGTH(ewmh); j++)
498 XChangeProperty(display, screens[i].root, sup_list, XA_ATOM, 32,
499 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
507 unsigned char *data = NULL;
509 Atom sup_check, sup_list;
512 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
513 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
515 for (i = 0; i < ScreenCount(display); i++) {
516 /* Get the support check window and destroy it */
517 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
522 XDestroyWindow(display, id);
523 XDeleteProperty(display, screens[i].root, sup_check);
524 XDeleteProperty(display, screens[i].root, sup_list);
532 ewmh_autoquirk(struct ws_win *win)
535 unsigned long *data = NULL;
539 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
540 XA_ATOM, &n, (unsigned char **)&data);
547 for (i = 0; i < n; i++) {
549 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
551 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
552 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
553 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
555 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
558 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
559 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
561 win->quirks = SWM_Q_FLOAT;
569 #define SWM_EWMH_ACTION_COUNT_MAX (6)
570 #define EWMH_F_FULLSCREEN (1<<0)
571 #define EWMH_F_ABOVE (1<<1)
572 #define EWMH_F_HIDDEN (1<<2)
573 #define EWMH_F_SKIP_PAGER (1<<3)
574 #define EWMH_F_SKIP_TASKBAR (1<<4)
575 #define SWM_F_MANUAL (1<<5)
578 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
580 struct swm_geometry rg;
588 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
594 store_float_geom(win, win->ws->r);
601 if (win->g_floatvalid) {
602 /* refloat at last floating relative position */
603 win->g.x = win->g_float.x - win->rg_float.x + rg.x;
604 win->g.y = win->g_float.y - win->rg_float.y + rg.y;
605 win->g.w = win->g_float.w;
606 win->g.h = win->g_float.h;
614 ewmh_update_actions(struct ws_win *win)
616 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
622 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
625 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
626 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
629 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
630 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
633 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
634 #define _NET_WM_STATE_ADD 1 /* add/set property */
635 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
638 ewmh_update_win_state(struct ws_win *win, long state, long action)
640 unsigned int mask = 0;
641 unsigned int changed = 0;
642 unsigned int orig_flags;
647 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
648 mask = EWMH_F_FULLSCREEN;
649 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
651 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
653 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
654 mask = EWMH_F_SKIP_PAGER;
655 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
656 mask = EWMH_F_SKIP_TASKBAR;
659 orig_flags = win->ewmh_flags;
662 case _NET_WM_STATE_REMOVE:
663 win->ewmh_flags &= ~mask;
665 case _NET_WM_STATE_ADD:
666 win->ewmh_flags |= mask;
668 case _NET_WM_STATE_TOGGLE:
669 win->ewmh_flags ^= mask;
673 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
675 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
677 if (!floating_toggle_win(win))
678 win->ewmh_flags = orig_flags; /* revert */
679 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
681 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
682 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
684 if (!ewmh_set_win_fullscreen(win, win->ewmh_flags & EWMH_F_FULLSCREEN))
685 win->ewmh_flags = orig_flags; /* revert */
687 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
689 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
690 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
691 XA_ATOM, 32, PropModeAppend,
692 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
693 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
694 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
695 XA_ATOM, 32, PropModeAppend,
696 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
697 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
698 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
699 XA_ATOM, 32, PropModeAppend,
700 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
701 if (win->ewmh_flags & EWMH_F_ABOVE)
702 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
703 XA_ATOM, 32, PropModeAppend,
704 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
705 if (win->ewmh_flags & SWM_F_MANUAL)
706 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
707 XA_ATOM, 32, PropModeAppend,
708 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
712 ewmh_get_win_state(struct ws_win *win)
723 win->ewmh_flags |= EWMH_F_ABOVE;
725 win->ewmh_flags |= SWM_F_MANUAL;
727 success = get_property(win->id, ewmh[_NET_WM_STATE].atom, (~0L), XA_ATOM,
728 &n, (unsigned char **)&states);
733 for (i = 0; i < n; i++)
734 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
754 name = "ButtonPress";
757 name = "ButtonRelease";
760 name = "MotionNotify";
763 name = "EnterNotify";
766 name = "LeaveNotify";
775 name = "KeymapNotify";
781 name = "GraphicsExpose";
786 case VisibilityNotify:
787 name = "VisibilityNotify";
790 name = "CreateNotify";
793 name = "DestroyNotify";
796 name = "UnmapNotify";
805 name = "ReparentNotify";
807 case ConfigureNotify:
808 name = "ConfigureNotify";
810 case ConfigureRequest:
811 name = "ConfigureRequest";
814 name = "GravityNotify";
817 name = "ResizeRequest";
819 case CirculateNotify:
820 name = "CirculateNotify";
822 case CirculateRequest:
823 name = "CirculateRequest";
826 name = "PropertyNotify";
829 name = "SelectionClear";
831 case SelectionRequest:
832 name = "SelectionRequest";
834 case SelectionNotify:
835 name = "SelectionNotify";
838 name = "ColormapNotify";
841 name = "ClientMessage";
844 name = "MappingNotify";
849 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
851 e->xany.window, name, e->type, QLength(display));
853 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
855 e->xany.window, e->type, QLength(display));
859 dumpwins(struct swm_region *r, union arg *args)
863 XWindowAttributes wa;
866 fprintf(stderr, "invalid workspace\n");
870 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
872 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
873 state = getstate(win->id);
874 if (!XGetWindowAttributes(display, win->id, &wa))
875 fprintf(stderr, "window: %lu failed "
876 "XGetWindowAttributes\n", win->id);
877 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
878 win->id, wa.map_state, state);
881 fprintf(stderr, "===== unmanaged window list =====\n");
882 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
883 state = getstate(win->id);
884 if (!XGetWindowAttributes(display, win->id, &wa))
885 fprintf(stderr, "window: %lu failed "
886 "XGetWindowAttributes\n", win->id);
887 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
888 win->id, wa.map_state, state);
891 fprintf(stderr, "=================================\n");
896 dumpwins(struct swm_region *r, union arg *args)
899 #endif /* SWM_DEBUG */
901 void expose(XEvent *);
902 void keypress(XEvent *);
903 void buttonpress(XEvent *);
904 void configurerequest(XEvent *);
905 void configurenotify(XEvent *);
906 void destroynotify(XEvent *);
907 void enternotify(XEvent *);
908 void focusevent(XEvent *);
909 void mapnotify(XEvent *);
910 void mappingnotify(XEvent *);
911 void maprequest(XEvent *);
912 void propertynotify(XEvent *);
913 void unmapnotify(XEvent *);
914 void visibilitynotify(XEvent *);
915 void clientmessage(XEvent *);
917 void (*handler[LASTEvent])(XEvent *) = {
919 [KeyPress] = keypress,
920 [ButtonPress] = buttonpress,
921 [ConfigureRequest] = configurerequest,
922 [ConfigureNotify] = configurenotify,
923 [DestroyNotify] = destroynotify,
924 [EnterNotify] = enternotify,
925 [FocusIn] = focusevent,
926 [FocusOut] = focusevent,
927 [MapNotify] = mapnotify,
928 [MappingNotify] = mappingnotify,
929 [MapRequest] = maprequest,
930 [PropertyNotify] = propertynotify,
931 [UnmapNotify] = unmapnotify,
932 [VisibilityNotify] = visibilitynotify,
933 [ClientMessage] = clientmessage,
939 int saved_errno, status;
946 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
952 warn("sighdlr: waitpid");
953 #endif /* SWM_DEBUG */
958 if (WIFEXITED(status)) {
959 if (WEXITSTATUS(status) != 0)
960 warnx("sighdlr: child exit status: %d",
961 WEXITSTATUS(status));
963 warnx("sighdlr: child is terminated "
965 #endif /* SWM_DEBUG */
983 name_to_color(char *colorname)
987 XColor screen_def, exact_def;
988 unsigned long result = 0;
989 char cname[32] = "#";
991 cmap = DefaultColormap(display, screens[0].idx);
992 status = XAllocNamedColor(display, cmap, colorname,
993 &screen_def, &exact_def);
995 strlcat(cname, colorname + 2, sizeof cname - 1);
996 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1000 result = screen_def.pixel;
1002 fprintf(stderr, "color '%s' not found.\n", colorname);
1008 setscreencolor(char *val, int i, int c)
1010 if (i > 0 && i <= ScreenCount(display)) {
1011 screens[i - 1].c[c].color = name_to_color(val);
1012 free(screens[i - 1].c[c].name);
1013 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1015 } else if (i == -1) {
1016 for (i = 0; i < ScreenCount(display); i++) {
1017 screens[i].c[c].color = name_to_color(val);
1018 free(screens[i].c[c].name);
1019 if ((screens[i].c[c].name = strdup(val)) == NULL)
1023 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1024 i, ScreenCount(display));
1028 custom_region(char *val)
1030 unsigned int sidx, x, y, w, h;
1032 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1033 errx(1, "invalid custom region, "
1034 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
1035 if (sidx < 1 || sidx > ScreenCount(display))
1036 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1037 sidx, ScreenCount(display));
1041 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
1043 if (x < 0 || x > DisplayWidth(display, sidx) ||
1044 y < 0 || y > DisplayHeight(display, sidx) ||
1045 w + x > DisplayWidth(display, sidx) ||
1046 h + y > DisplayHeight(display, sidx)) {
1047 fprintf(stderr, "ignoring region %ux%u+%u+%u - not within screen boundaries "
1048 "(%ux%u)\n", w, h, x, y,
1049 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1053 new_region(&screens[sidx], x, y, w, h);
1057 socket_setnonblock(int fd)
1061 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1062 err(1, "fcntl F_GETFL");
1063 flags |= O_NONBLOCK;
1064 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1065 err(1, "fcntl F_SETFL");
1069 bar_print(struct swm_region *r, char *s)
1071 XClearWindow(display, r->bar_window);
1072 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
1073 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
1078 bar_extra_stop(void)
1082 bzero(bar_pipe, sizeof bar_pipe);
1085 kill(bar_pid, SIGTERM);
1088 strlcpy(bar_ext, "", sizeof bar_ext);
1093 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1095 int do_class, do_name;
1097 XClassHint *xch = NULL;
1099 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
1100 cur_focus != NULL) {
1101 if ((xch = XAllocClassHint()) == NULL)
1103 status = XGetClassHint(display, cur_focus->id, xch);
1104 if (status == BadWindow || status == BadAlloc)
1106 do_class = (title_class_enabled && xch->res_class != NULL);
1107 do_name = (title_name_enabled && xch->res_name != NULL);
1109 strlcat(s, xch->res_class, sz);
1110 if (do_class && do_name)
1111 strlcat(s, ":", sz);
1113 strlcat(s, xch->res_name, sz);
1114 strlcat(s, " ", sz);
1122 bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1126 if (window_name_enabled && cur_focus != NULL) {
1127 XFetchName(display, cur_focus->id, &title);
1129 if (cur_focus->floating)
1130 strlcat(s, "(f) ", sz);
1131 strlcat(s, title, sz);
1132 strlcat(s, " ", sz);
1143 struct swm_region *r;
1146 char s[SWM_BAR_MAX];
1147 char cn[SWM_BAR_MAX];
1148 char loc[SWM_BAR_MAX];
1152 if (bar_enabled == 0)
1154 if (bar_extra && bar_extra_running) {
1155 /* ignore short reads; it'll correct itself */
1156 while ((b = fgetln(stdin, &len)) != NULL)
1157 if (b && b[len - 1] == '\n') {
1159 strlcpy(bar_ext, b, sizeof bar_ext);
1161 if (b == NULL && errno != EAGAIN) {
1162 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
1163 errno, strerror(errno));
1167 strlcpy(bar_ext, "", sizeof bar_ext);
1169 if (clock_enabled == 0)
1170 strlcpy(s, "", sizeof s);
1173 localtime_r(&tmt, &tm);
1174 strftime(s, sizeof s, clock_format, &tm);
1175 strlcat(s, " ", sizeof s);
1178 for (i = 0; i < ScreenCount(display); i++) {
1180 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1181 strlcpy(cn, "", sizeof cn);
1183 bar_class_name(cn, sizeof cn, r->ws->focus);
1184 bar_window_name(cn, sizeof cn, r->ws->focus);
1188 stack = r->ws->cur_layout->name;
1190 snprintf(loc, sizeof loc, "%d:%d %s %s%s %s %s",
1191 x++, r->ws->idx + 1, stack, s, cn, bar_ext,
1206 bar_toggle(struct swm_region *r, union arg *args)
1208 struct swm_region *tmpr;
1209 int i, sc = ScreenCount(display);
1211 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
1214 for (i = 0; i < sc; i++)
1215 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1216 XUnmapWindow(display, tmpr->bar_window);
1218 for (i = 0; i < sc; i++)
1219 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1220 XMapRaised(display, tmpr->bar_window);
1222 bar_enabled = !bar_enabled;
1225 /* must be after stack */
1232 XSetWindowAttributes wa;
1233 struct swm_region *r;
1236 /* do this here because the conf file is in memory */
1237 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1238 /* launch external status app */
1239 bar_extra_running = 1;
1240 if (pipe(bar_pipe) == -1)
1241 err(1, "pipe error");
1242 socket_setnonblock(bar_pipe[0]);
1243 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1244 if (dup2(bar_pipe[0], 0) == -1)
1246 if (dup2(bar_pipe[1], 1) == -1)
1248 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1249 err(1, "could not disable SIGPIPE");
1250 switch (bar_pid = fork()) {
1252 err(1, "cannot fork");
1256 execvp(bar_argv[0], bar_argv);
1257 err(1, "%s external app failed", bar_argv[0]);
1259 default: /* parent */
1265 bzero(&wa, sizeof wa);
1266 for (i = 0; i < ScreenCount(display); i++)
1267 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1269 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1270 wa.background_pixel =
1271 screens[i].c[SWM_S_COLOR_BAR].color;
1272 XChangeWindowAttributes(display, r->bar_window,
1273 CWBackPixel | CWBorderPixel, &wa);
1279 bar_setup(struct swm_region *r)
1284 XFreeFont(display, bar_fs);
1288 for (i = 0; bar_fonts[i] != NULL; i++) {
1289 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
1295 if (bar_fonts[i] == NULL)
1296 errx(1, "couldn't load font");
1298 errx(1, "couldn't create font structure");
1300 bar_height = bar_fs->ascent + bar_fs->descent + 1 + 2 * bar_border_width;
1302 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1304 r->bar_window = XCreateSimpleWindow(display,
1305 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width, bar_height - 2 * bar_border_width,
1306 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1307 r->s->c[SWM_S_COLOR_BAR].color);
1308 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
1309 XSetFont(display, bar_gc, bar_fs->fid);
1310 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1312 XMapRaised(display, r->bar_window);
1313 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
1315 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1316 err(1, "could not install bar_signal");
1321 set_win_state(struct ws_win *win, long state)
1323 long data[] = {state, None};
1325 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1330 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1331 (unsigned char *)data, 2);
1338 unsigned char *p = NULL;
1341 if (!get_property(w, astate, 2L, astate, &n, &p))
1344 result = *((long *)p);
1350 version(struct swm_region *r, union arg *args)
1352 bar_version = !bar_version;
1354 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
1355 SWM_VERSION, cvstag);
1357 strlcpy(bar_vertext, "", sizeof bar_vertext);
1362 client_msg(struct ws_win *win, Atom a)
1364 XClientMessageEvent cm;
1369 bzero(&cm, sizeof cm);
1370 cm.type = ClientMessage;
1371 cm.window = win->id;
1372 cm.message_type = aprot;
1375 cm.data.l[1] = CurrentTime;
1376 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1380 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1388 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
1389 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1391 ce.type = ConfigureNotify;
1392 ce.display = display;
1394 ce.window = win->id;
1397 ce.width = win->g.w;
1398 ce.height = win->g.h;
1399 ce.border_width = border_width;
1401 ce.override_redirect = False;
1403 DNPRINTF(SWM_D_MISC, "config_win: ev win %lu x %d y %d w %d h %d\n",
1404 ev->window, ev->x, ev->y, ev->width, ev->height);
1405 ce.type = ConfigureNotify;
1406 ce.display = ev->display;
1407 ce.event = ev->window;
1408 ce.window = ev->window;
1411 ce.width = ev->width;
1412 ce.height = ev->height;
1413 ce.border_width = ev->border_width;
1414 ce.above = ev->above;
1415 ce.override_redirect = False;
1418 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1422 count_win(struct workspace *ws, int count_transient)
1427 TAILQ_FOREACH(win, &ws->winlist, entry) {
1428 if (count_transient == 0 && win->floating)
1430 if (count_transient == 0 && win->transient)
1434 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1440 quit(struct swm_region *r, union arg *args)
1442 DNPRINTF(SWM_D_MISC, "quit\n");
1447 unmap_window(struct ws_win *win)
1452 /* don't unmap again */
1453 if (getstate(win->id) == IconicState)
1456 set_win_state(win, IconicState);
1458 XUnmapWindow(display, win->id);
1459 XSetWindowBorder(display, win->id,
1460 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1469 for (i = 0; i < ScreenCount(display); i++)
1470 for (j = 0; j < SWM_WS_MAX; j++)
1471 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1476 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1483 event.display = display; /* Ignored, but what the hell */
1484 event.window = win->id;
1485 event.root = win->s->root;
1486 event.subwindow = None;
1487 event.time = CurrentTime;
1492 event.same_screen = True;
1493 event.keycode = XKeysymToKeycode(display, keysym);
1494 event.state = modifiers;
1496 event.type = KeyPress;
1497 XSendEvent(event.display, event.window, True,
1498 KeyPressMask, (XEvent *)&event);
1500 event.type = KeyRelease;
1501 XSendEvent(event.display, event.window, True,
1502 KeyPressMask, (XEvent *)&event);
1507 restart(struct swm_region *r, union arg *args)
1509 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1511 /* disable alarm because the following code may not be interrupted */
1513 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1514 errx(1, "can't disable alarm");
1519 XCloseDisplay(display);
1520 execvp(start_argv[0], start_argv);
1521 fprintf(stderr, "execvp failed\n");
1527 root_to_region(Window root)
1529 struct swm_region *r = NULL;
1531 int i, x, y, wx, wy;
1534 for (i = 0; i < ScreenCount(display); i++)
1535 if (screens[i].root == root)
1538 if (XQueryPointer(display, screens[i].root,
1539 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1540 /* choose a region based on pointer location */
1541 TAILQ_FOREACH(r, &screens[i].rl, entry)
1542 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1543 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1548 r = TAILQ_FIRST(&screens[i].rl);
1554 find_unmanaged_window(Window id)
1559 for (i = 0; i < ScreenCount(display); i++)
1560 for (j = 0; j < SWM_WS_MAX; j++)
1561 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1569 find_window(Window id)
1572 Window wrr, wpr, *wcr = NULL;
1576 for (i = 0; i < ScreenCount(display); i++)
1577 for (j = 0; j < SWM_WS_MAX; j++)
1578 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1582 /* if we were looking for the parent return that window instead */
1583 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1588 /* ignore not found and root */
1589 if (wpr == 0 || wrr == wpr)
1592 /* look for parent */
1593 for (i = 0; i < ScreenCount(display); i++)
1594 for (j = 0; j < SWM_WS_MAX; j++)
1595 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1603 spawn(struct swm_region *r, union arg *args)
1608 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1612 close(ConnectionNumber(display));
1614 setenv("LD_PRELOAD", SWM_LIB, 1);
1616 if (asprintf(&ret, "%d", r->ws->idx) == -1) {
1620 setenv("_SWM_WS", ret, 1);
1624 if (asprintf(&ret, "%d", getpid()) == -1) {
1628 setenv("_SWM_PID", ret, 1);
1632 if (setsid() == -1) {
1638 * close stdin and stdout to prevent interaction between apps
1639 * and the baraction script
1640 * leave stderr open to record errors
1642 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
1646 dup2(fd, STDIN_FILENO);
1647 dup2(fd, STDOUT_FILENO);
1651 execvp(args->argv[0], args->argv);
1659 spawnterm(struct swm_region *r, union arg *args)
1661 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1664 setenv("_SWM_XTERM_FONTADJ", "", 1);
1669 kill_refs(struct ws_win *win)
1672 struct swm_region *r;
1673 struct workspace *ws;
1678 for (i = 0; i < ScreenCount(display); i++)
1679 TAILQ_FOREACH(r, &screens[i].rl, entry)
1680 for (x = 0; x < SWM_WS_MAX; x++) {
1682 if (win == ws->focus)
1684 if (win == ws->focus_prev)
1685 ws->focus_prev = NULL;
1690 validate_win(struct ws_win *testwin)
1693 struct workspace *ws;
1694 struct swm_region *r;
1695 int i, x, foundit = 0;
1697 if (testwin == NULL)
1700 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1701 TAILQ_FOREACH(r, &screens[i].rl, entry)
1702 for (x = 0; x < SWM_WS_MAX; x++) {
1704 TAILQ_FOREACH(win, &ws->winlist, entry)
1712 validate_ws(struct workspace *testws)
1714 struct swm_region *r;
1715 struct workspace *ws;
1718 /* validate all ws */
1719 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1720 TAILQ_FOREACH(r, &screens[i].rl, entry)
1721 for (x = 0; x < SWM_WS_MAX; x++) {
1730 unfocus_win(struct ws_win *win)
1735 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1739 if (win->ws == NULL)
1742 if (validate_ws(win->ws))
1743 return; /* XXX this gets hit with thunderbird, needs fixing */
1745 if (win->ws->r == NULL)
1748 if (validate_win(win)) {
1753 if (win->ws->focus == win) {
1754 win->ws->focus = NULL;
1755 win->ws->focus_prev = win;
1758 if (validate_win(win->ws->focus)) {
1759 kill_refs(win->ws->focus);
1760 win->ws->focus = NULL;
1762 if (validate_win(win->ws->focus_prev)) {
1763 kill_refs(win->ws->focus_prev);
1764 win->ws->focus_prev = NULL;
1767 /* drain all previous unfocus events */
1768 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
1771 grabbuttons(win, 0);
1772 XSetWindowBorder(display, win->id,
1773 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1775 XChangeProperty(display, win->s->root,
1776 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1777 PropModeReplace, (unsigned char *)&none,1);
1786 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
1788 for (i = 0; i < ScreenCount(display); i++)
1789 for (j = 0; j < SWM_WS_MAX; j++)
1790 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1795 focus_win(struct ws_win *win)
1800 struct ws_win *cfw = NULL;
1803 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1807 if (win->ws == NULL)
1810 if (validate_ws(win->ws))
1811 return; /* XXX this gets hit with thunderbird, needs fixing */
1813 if (validate_win(win)) {
1818 if (validate_win(win)) {
1823 XGetInputFocus(display, &cur_focus, &rr);
1824 if ((cfw = find_window(cur_focus)) != NULL)
1827 win->ws->focus = win;
1829 if (win->ws->r != NULL) {
1830 /* drain all previous focus events */
1831 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
1835 XSetInputFocus(display, win->id,
1836 RevertToParent, CurrentTime);
1837 grabbuttons(win, 1);
1838 XSetWindowBorder(display, win->id,
1839 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1840 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS)
1841 XMapRaised(display, win->id);
1843 XChangeProperty(display, win->s->root,
1844 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1845 PropModeReplace, (unsigned char *)&win->id,1);
1848 if (window_name_enabled)
1853 switchws(struct swm_region *r, union arg *args)
1855 int wsid = args->id, unmap_old = 0;
1856 struct swm_region *this_r, *other_r;
1858 struct workspace *new_ws, *old_ws;
1865 old_ws = this_r->ws;
1866 new_ws = &this_r->s->ws[wsid];
1868 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1869 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1872 if (new_ws == NULL || old_ws == NULL)
1874 if (new_ws == old_ws)
1877 other_r = new_ws->r;
1878 if (other_r == NULL) {
1879 /* the other workspace is hidden, hide this one */
1883 /* the other ws is visible in another region, exchange them */
1884 other_r->ws_prior = new_ws;
1885 other_r->ws = old_ws;
1886 old_ws->r = other_r;
1888 this_r->ws_prior = old_ws;
1889 this_r->ws = new_ws;
1893 a.id = SWM_ARG_ID_FOCUSCUR;
1894 focus(new_ws->r, &a);
1897 /* unmap old windows */
1899 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1904 cyclews(struct swm_region *r, union arg *args)
1907 struct swm_screen *s = r->s;
1909 DNPRINTF(SWM_D_WS, "cyclews id %d "
1910 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1911 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1916 case SWM_ARG_ID_CYCLEWS_UP:
1917 if (a.id < SWM_WS_MAX - 1)
1922 case SWM_ARG_ID_CYCLEWS_DOWN:
1926 a.id = SWM_WS_MAX - 1;
1932 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1934 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1938 } while (a.id != r->ws->idx);
1942 priorws(struct swm_region *r, union arg *args)
1946 DNPRINTF(SWM_D_WS, "priorws id %d "
1947 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1948 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1950 if (r->ws_prior == NULL)
1953 a.id = r->ws_prior->idx;
1958 cyclescr(struct swm_region *r, union arg *args)
1960 struct swm_region *rr = NULL;
1964 /* do nothing if we don't have more than one screen */
1965 if (!(ScreenCount(display) > 1 || outputs > 1))
1970 case SWM_ARG_ID_CYCLESC_UP:
1971 rr = TAILQ_NEXT(r, entry);
1973 rr = TAILQ_FIRST(&screens[i].rl);
1975 case SWM_ARG_ID_CYCLESC_DOWN:
1976 rr = TAILQ_PREV(r, swm_region_list, entry);
1978 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1986 /* move mouse to region */
1988 y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
1989 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1991 a.id = SWM_ARG_ID_FOCUSCUR;
1994 if (rr->ws->focus) {
1995 /* move to focus window */
1996 x = rr->ws->focus->g.x + 1;
1997 y = rr->ws->focus->g.y + 1;
1998 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2003 swapwin(struct swm_region *r, union arg *args)
2005 struct ws_win *target, *source;
2006 struct ws_win *cur_focus;
2007 struct ws_win_list *wl;
2010 DNPRINTF(SWM_D_WS, "swapwin id %d "
2011 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
2012 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2014 cur_focus = r->ws->focus;
2015 if (cur_focus == NULL)
2019 wl = &source->ws->winlist;
2022 case SWM_ARG_ID_SWAPPREV:
2023 target = TAILQ_PREV(source, ws_win_list, entry);
2024 TAILQ_REMOVE(wl, cur_focus, entry);
2026 TAILQ_INSERT_TAIL(wl, source, entry);
2028 TAILQ_INSERT_BEFORE(target, source, entry);
2030 case SWM_ARG_ID_SWAPNEXT:
2031 target = TAILQ_NEXT(source, entry);
2032 TAILQ_REMOVE(wl, source, entry);
2034 TAILQ_INSERT_HEAD(wl, source, entry);
2036 TAILQ_INSERT_AFTER(wl, target, source, entry);
2038 case SWM_ARG_ID_SWAPMAIN:
2039 target = TAILQ_FIRST(wl);
2040 if (target == source) {
2041 if (source->ws->focus_prev != NULL &&
2042 source->ws->focus_prev != target)
2044 source = source->ws->focus_prev;
2048 if (target == NULL || source == NULL)
2050 source->ws->focus_prev = target;
2051 TAILQ_REMOVE(wl, target, entry);
2052 TAILQ_INSERT_BEFORE(source, target, entry);
2053 TAILQ_REMOVE(wl, source, entry);
2054 TAILQ_INSERT_HEAD(wl, source, entry);
2056 case SWM_ARG_ID_MOVELAST:
2057 TAILQ_REMOVE(wl, source, entry);
2058 TAILQ_INSERT_TAIL(wl, source, entry);
2061 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
2069 focus_prev(struct ws_win *win)
2071 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2072 struct ws_win *cur_focus = NULL;
2073 struct ws_win_list *wl = NULL;
2074 struct workspace *ws = NULL;
2076 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
2078 if (!(win && win->ws))
2083 cur_focus = ws->focus;
2084 winlostfocus = cur_focus;
2086 /* pickle, just focus on whatever */
2087 if (cur_focus == NULL) {
2088 /* use prev_focus if valid */
2089 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2090 find_window(WINID(ws->focus_prev)))
2091 winfocus = ws->focus_prev;
2092 if (winfocus == NULL)
2093 winfocus = TAILQ_FIRST(wl);
2097 /* if transient focus on parent */
2098 if (cur_focus->transient) {
2099 winfocus = find_window(cur_focus->transient);
2103 /* if in max_stack try harder */
2104 if (ws->cur_layout->flags & SWM_L_FOCUSPREV) {
2105 if (cur_focus != ws->focus_prev)
2106 winfocus = ws->focus_prev;
2107 else if (cur_focus != ws->focus)
2108 winfocus = ws->focus;
2110 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2115 if (cur_focus == win)
2116 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2117 if (winfocus == NULL)
2118 winfocus = TAILQ_LAST(wl, ws_win_list);
2119 if (winfocus == NULL || winfocus == win)
2120 winfocus = TAILQ_NEXT(cur_focus, entry);
2122 if (winfocus == winlostfocus || winfocus == NULL)
2125 focus_magic(winfocus, SWM_F_GENERIC);
2129 focus(struct swm_region *r, union arg *args)
2131 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2132 struct ws_win *cur_focus = NULL;
2133 struct ws_win_list *wl = NULL;
2134 struct workspace *ws = NULL;
2139 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
2141 /* treat FOCUS_CUR special */
2142 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2144 winfocus = r->ws->focus;
2145 else if (r->ws->focus_prev)
2146 winfocus = r->ws->focus_prev;
2148 winfocus = TAILQ_FIRST(&r->ws->winlist);
2150 focus_magic(winfocus, SWM_F_GENERIC);
2154 if ((cur_focus = r->ws->focus) == NULL)
2159 winlostfocus = cur_focus;
2162 case SWM_ARG_ID_FOCUSPREV:
2163 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
2164 if (winfocus == NULL)
2165 winfocus = TAILQ_LAST(wl, ws_win_list);
2168 case SWM_ARG_ID_FOCUSNEXT:
2169 winfocus = TAILQ_NEXT(cur_focus, entry);
2170 if (winfocus == NULL)
2171 winfocus = TAILQ_FIRST(wl);
2174 case SWM_ARG_ID_FOCUSMAIN:
2175 winfocus = TAILQ_FIRST(wl);
2176 if (winfocus == cur_focus)
2177 winfocus = cur_focus->ws->focus_prev;
2184 if (winfocus == winlostfocus || winfocus == NULL)
2187 focus_magic(winfocus, SWM_F_GENERIC);
2191 cycle_layout(struct swm_region *r, union arg *args)
2193 struct workspace *ws = r->ws;
2194 struct ws_win *winfocus;
2197 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2199 winfocus = ws->focus;
2202 if (ws->cur_layout->l_stack == NULL)
2203 ws->cur_layout = &layouts[0];
2206 a.id = SWM_ARG_ID_FOCUSCUR;
2212 stack_config(struct swm_region *r, union arg *args)
2214 struct workspace *ws = r->ws;
2216 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
2219 if (ws->cur_layout->l_config != NULL)
2220 ws->cur_layout->l_config(ws, args->id);
2222 if (args->id != SWM_ARG_ID_STACKINIT);
2228 struct swm_geometry g;
2229 struct swm_region *r;
2232 DNPRINTF(SWM_D_STACK, "stack\n");
2234 for (i = 0; i < ScreenCount(display); i++) {
2236 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2237 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
2238 "(screen %d, region %d)\n", r->ws->idx, i, j++);
2240 /* start with screen geometry, adjust for bar */
2242 g.w -= 2 * border_width;
2243 g.h -= 2 * border_width;
2249 r->ws->cur_layout->l_stack(r->ws, &g);
2250 /* save r so we can track region changes */
2259 store_float_geom(struct ws_win *win, struct swm_region *r)
2261 /* retain window geom and region geom */
2262 win->g_float.x = win->g.x;
2263 win->g_float.y = win->g.y;
2264 win->g_float.w = win->g.w;
2265 win->g_float.h = win->g.h;
2266 win->rg_float.x = r->g.x;
2267 win->rg_float.y = r->g.y;
2268 win->rg_float.w = r->g.w;
2269 win->rg_float.h = r->g.h;
2270 win->g_floatvalid = 1;
2274 stack_floater(struct ws_win *win, struct swm_region *r)
2282 bzero(&wc, sizeof wc);
2283 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2286 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2287 * geom on ws switches or return from max mode
2290 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2291 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2293 * use stored g and rg to set relative position and size
2294 * as in old region or before max stack mode
2296 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2297 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2298 win->g.w = win->g_float.w;
2299 win->g.h = win->g_float.h;
2300 win->g_floatvalid = 0;
2303 win->floatmaxed = 0;
2305 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
2306 (win->g.h >= HEIGHT(r)))
2307 wc.border_width = 0;
2309 wc.border_width = border_width;
2310 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2311 win->g.w = (double)WIDTH(r) * dialog_ratio;
2312 win->g.h = (double)HEIGHT(r) * dialog_ratio;
2317 * floaters and transients are auto-centred unless moved
2320 win->g.x = r->g.x + (WIDTH(r) - win->g.w) / 2 - border_width;
2321 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) / 2 - border_width;
2324 /* win can be outside r if new r smaller than old r */
2325 /* Ensure top left corner inside r (move probs otherwise) */
2326 if (win->g.x < r->g.x - border_width)
2327 win->g.x = r->g.x - border_width;
2328 if (win->g.x > r->g.x + r->g.w - 1)
2329 win->g.x = (win->g.w > r->g.w) ? r->g.x :
2330 (r->g.x + r->g.w - win->g.w - 2 * border_width);
2331 if (win->g.y < r->g.y - border_width)
2332 win->g.y = r->g.y - border_width;
2333 if (win->g.y > r->g.y + r->g.h - 1)
2334 win->g.y = (win->g.h > r->g.h) ? r->g.y :
2335 (r->g.y + r->g.h - win->g.h - 2 * border_width);
2339 wc.width = win->g.w;
2340 wc.height = win->g.h;
2343 * Retain floater and transient geometry for correct positioning
2344 * when ws changes region
2346 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2347 store_float_geom(win, r);
2349 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
2350 win->id, wc.x, wc.y, wc.width, wc.height);
2352 XConfigureWindow(display, win->id, mask, &wc);
2356 * Send keystrokes to terminal to decrease/increase the font size as the
2357 * window size changes.
2360 adjust_font(struct ws_win *win)
2362 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2363 win->floating || win->transient)
2366 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2367 win->g.w / win->sh.width_inc < term_width &&
2368 win->font_steps < SWM_MAX_FONT_STEPS) {
2369 win->font_size_boundary[win->font_steps] =
2370 (win->sh.width_inc * term_width) + win->sh.base_width;
2373 win->last_inc = win->sh.width_inc;
2374 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2375 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2376 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
2379 win->last_inc = win->sh.width_inc;
2380 fake_keypress(win, XK_KP_Add, ShiftMask);
2384 #define SWAPXY(g) do { \
2386 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2387 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2390 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2393 XWindowAttributes wa;
2394 struct swm_geometry win_g, r_g = *g;
2395 struct ws_win *win, *fs_win = 0;
2396 int i, j, s, stacks;
2397 int w_inc = 1, h_inc, w_base = 1, h_base;
2398 int hrh, extra = 0, h_slice, last_h = 0;
2399 int split, colno, winno, mwin, msize, mscale;
2400 int remain, missing, v_slice, reconfigure;
2403 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2404 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2406 winno = count_win(ws, 0);
2407 if (winno == 0 && count_win(ws, 1) == 0)
2410 TAILQ_FOREACH(win, &ws->winlist, entry)
2411 if (win->transient == 0 && win->floating == 0)
2418 w_inc = win->sh.width_inc;
2419 w_base = win->sh.base_width;
2420 mwin = ws->l_state.horizontal_mwin;
2421 mscale = ws->l_state.horizontal_msize;
2422 stacks = ws->l_state.horizontal_stacks;
2425 w_inc = win->sh.height_inc;
2426 w_base = win->sh.base_height;
2427 mwin = ws->l_state.vertical_mwin;
2428 mscale = ws->l_state.vertical_msize;
2429 stacks = ws->l_state.vertical_stacks;
2433 if (stacks > winno - mwin)
2434 stacks = winno - mwin;
2438 h_slice = r_g.h / SWM_H_SLICE;
2439 if (mwin && winno > mwin) {
2440 v_slice = r_g.w / SWM_V_SLICE;
2444 win_g.w = v_slice * mscale;
2446 if (w_inc > 1 && w_inc < v_slice) {
2447 /* adjust for window's requested size increment */
2448 remain = (win_g.w - w_base) % w_inc;
2449 missing = w_inc - remain;
2456 win_g.x += r_g.w - msize;
2459 colno = split = winno / stacks;
2460 win_g.w = ((r_g.w - (stacks * 2 * border_width) + 2 * border_width) / stacks);
2462 hrh = r_g.h / colno;
2463 extra = r_g.h - (colno * hrh);
2464 win_g.h = hrh - 2 * border_width;
2466 /* stack all the tiled windows */
2467 i = j = 0, s = stacks;
2468 TAILQ_FOREACH(win, &ws->winlist, entry) {
2469 if (win->transient != 0 || win->floating != 0)
2472 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2477 if (split && i == split) {
2478 colno = (winno - mwin) / stacks;
2479 if (s <= (winno - mwin) % stacks)
2481 split = split + colno;
2482 hrh = (r_g.h / colno);
2483 extra = r_g.h - (colno * hrh);
2487 win_g.x += win_g.w + 2 * border_width;
2488 win_g.w = (r_g.w - msize - (stacks * 2 * border_width)) / stacks;
2490 win_g.w += (r_g.w - msize - (stacks * 2 * border_width)) %
2495 win_g.h = hrh - 2 * border_width;
2497 h_inc = win->sh.width_inc;
2498 h_base = win->sh.base_width;
2500 h_inc = win->sh.height_inc;
2501 h_base = win->sh.base_height;
2503 if (j == colno - 1) {
2504 win_g.h = hrh + extra;
2505 } else if (h_inc > 1 && h_inc < h_slice) {
2506 /* adjust for window's requested size increment */
2507 remain = (win_g.h - h_base) % h_inc;
2508 missing = h_inc - remain;
2510 if (missing <= extra || j == 0) {
2522 win_g.y += last_h + 2 * border_width;
2524 bzero(&wc, sizeof wc);
2525 if (disable_border && bar_enabled == 0 && winno == 1){
2526 wc.border_width = 0;
2527 win_g.w += 2 * border_width;
2528 win_g.h += 2 * border_width;
2530 wc.border_width = border_width;
2533 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2534 win->g.w != win_g.h || win->g.h != win_g.w) {
2536 win->g.x = wc.x = win_g.y;
2537 win->g.y = wc.y = win_g.x;
2538 win->g.w = wc.width = win_g.h;
2539 win->g.h = wc.height = win_g.w;
2542 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2543 win->g.w != win_g.w || win->g.h != win_g.h) {
2545 win->g.x = wc.x = win_g.x;
2546 win->g.y = wc.y = win_g.y;
2547 win->g.w = wc.width = win_g.w;
2548 win->g.h = wc.height = win_g.h;
2553 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2554 XConfigureWindow(display, win->id, mask, &wc);
2557 if (XGetWindowAttributes(display, win->id, &wa))
2558 if (wa.map_state == IsUnmapped)
2559 XMapRaised(display, win->id);
2567 /* now, stack all the floaters and transients */
2568 TAILQ_FOREACH(win, &ws->winlist, entry) {
2569 if (win->transient == 0 && win->floating == 0)
2572 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2577 stack_floater(win, ws->r);
2578 XMapRaised(display, win->id);
2582 stack_floater(fs_win, ws->r);
2583 XMapRaised(display, fs_win->id);
2588 vertical_config(struct workspace *ws, int id)
2590 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2593 case SWM_ARG_ID_STACKRESET:
2594 case SWM_ARG_ID_STACKINIT:
2595 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2596 ws->l_state.vertical_mwin = 1;
2597 ws->l_state.vertical_stacks = 1;
2599 case SWM_ARG_ID_MASTERSHRINK:
2600 if (ws->l_state.vertical_msize > 1)
2601 ws->l_state.vertical_msize--;
2603 case SWM_ARG_ID_MASTERGROW:
2604 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2605 ws->l_state.vertical_msize++;
2607 case SWM_ARG_ID_MASTERADD:
2608 ws->l_state.vertical_mwin++;
2610 case SWM_ARG_ID_MASTERDEL:
2611 if (ws->l_state.vertical_mwin > 0)
2612 ws->l_state.vertical_mwin--;
2614 case SWM_ARG_ID_STACKINC:
2615 ws->l_state.vertical_stacks++;
2617 case SWM_ARG_ID_STACKDEC:
2618 if (ws->l_state.vertical_stacks > 1)
2619 ws->l_state.vertical_stacks--;
2627 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2629 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2631 stack_master(ws, g, 0, 0);
2635 horizontal_config(struct workspace *ws, int id)
2637 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2640 case SWM_ARG_ID_STACKRESET:
2641 case SWM_ARG_ID_STACKINIT:
2642 ws->l_state.horizontal_mwin = 1;
2643 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2644 ws->l_state.horizontal_stacks = 1;
2646 case SWM_ARG_ID_MASTERSHRINK:
2647 if (ws->l_state.horizontal_msize > 1)
2648 ws->l_state.horizontal_msize--;
2650 case SWM_ARG_ID_MASTERGROW:
2651 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2652 ws->l_state.horizontal_msize++;
2654 case SWM_ARG_ID_MASTERADD:
2655 ws->l_state.horizontal_mwin++;
2657 case SWM_ARG_ID_MASTERDEL:
2658 if (ws->l_state.horizontal_mwin > 0)
2659 ws->l_state.horizontal_mwin--;
2661 case SWM_ARG_ID_STACKINC:
2662 ws->l_state.horizontal_stacks++;
2664 case SWM_ARG_ID_STACKDEC:
2665 if (ws->l_state.horizontal_stacks > 1)
2666 ws->l_state.horizontal_stacks--;
2674 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2676 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
2678 stack_master(ws, g, 1, 0);
2681 /* fullscreen view */
2683 max_stack(struct workspace *ws, struct swm_geometry *g)
2686 struct swm_geometry gg = *g;
2687 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2691 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2696 winno = count_win(ws, 0);
2697 if (winno == 0 && count_win(ws, 1) == 0)
2700 TAILQ_FOREACH(win, &ws->winlist, entry) {
2701 if (win->transient) {
2703 parent = find_window(win->transient);
2707 if (win->floating && win->floatmaxed == 0 ) {
2709 * retain geometry for retrieval on exit from
2712 store_float_geom(win, ws->r);
2713 win->floatmaxed = 1;
2716 /* only reconfigure if necessary */
2717 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2719 bzero(&wc, sizeof wc);
2720 win->g.x = wc.x = gg.x;
2721 win->g.y = wc.y = gg.y;
2723 wc.border_width = border_width;
2724 win->g.w = wc.width = gg.w;
2725 win->g.h = wc.height = gg.h;
2727 wc.border_width = 0;
2728 win->g.w = wc.width = gg.w + 2 * border_width;
2729 win->g.h = wc.height = gg.h + 2 * border_width;
2731 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2732 XConfigureWindow(display, win->id, mask, &wc);
2734 /* unmap only if we don't have multi screen */
2735 if (win != ws->focus)
2736 if (!(ScreenCount(display) > 1 || outputs > 1))
2740 /* put the last transient on top */
2743 XMapRaised(display, parent->id);
2744 stack_floater(wintrans, ws->r);
2745 focus_magic(wintrans, SWM_F_TRANSIENT);
2750 send_to_ws(struct swm_region *r, union arg *args)
2752 int wsid = args->id;
2753 struct ws_win *win = win;
2754 struct workspace *ws, *nws;
2755 Atom ws_idx_atom = 0;
2756 unsigned char ws_idx_str[SWM_PROPLEN];
2765 if (win->ws->idx == wsid)
2768 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2771 nws = &win->s->ws[wsid];
2773 a.id = SWM_ARG_ID_FOCUSPREV;
2776 TAILQ_REMOVE(&ws->winlist, win, entry);
2777 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2780 /* Try to update the window's workspace property */
2781 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2783 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2784 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2786 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2787 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2794 wkill(struct swm_region *r, union arg *args)
2796 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
2798 if (r->ws->focus == NULL)
2801 if (args->id == SWM_ARG_ID_KILLWINDOW)
2802 XKillClient(display, r->ws->focus->id);
2804 if (r->ws->focus->can_delete)
2805 client_msg(r->ws->focus, adelete);
2810 floating_toggle_win(struct ws_win *win)
2812 struct swm_region *r;
2822 /* reject floating toggles in max stack mode */
2823 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
2826 if (win->floating) {
2827 if (!win->floatmaxed) {
2828 /* retain position for refloat */
2829 store_float_geom(win, r);
2833 if (win->g_floatvalid) {
2834 /* refloat at last floating relative position */
2835 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2836 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2837 win->g.w = win->g_float.w;
2838 win->g.h = win->g_float.h;
2843 ewmh_update_actions(win);
2849 floating_toggle(struct swm_region *r, union arg *args)
2851 struct ws_win *win = r->ws->focus;
2857 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
2858 _NET_WM_STATE_TOGGLE);
2862 if (win == win->ws->focus) {
2863 a.id = SWM_ARG_ID_FOCUSCUR;
2864 focus(win->ws->r, &a);
2869 resize_window(struct ws_win *win, int center)
2873 struct swm_region *r;
2875 r = root_to_region(win->wa.root);
2876 bzero(&wc, sizeof wc);
2877 mask = CWBorderWidth | CWWidth | CWHeight;
2878 wc.border_width = border_width;
2879 wc.width = win->g.w;
2880 wc.height = win->g.h;
2881 if (center == SWM_ARG_ID_CENTER) {
2882 wc.x = (WIDTH(r) - win->g.w) / 2 - border_width;
2883 wc.y = (HEIGHT(r) - win->g.h) / 2 - border_width;
2887 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
2888 win->id, wc.x, wc.y, wc.width, wc.height);
2890 XConfigureWindow(display, win->id, mask, &wc);
2894 resize(struct ws_win *win, union arg *args)
2898 struct swm_region *r = win->ws->r;
2903 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
2904 win->id, win->floating, win->transient);
2906 if (!(win->transient != 0 || win->floating != 0))
2909 /* reject resizes in max mode for floaters (transient ok) */
2910 if (win->floatmaxed)
2914 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
2916 /* raise the window = move to last in window list */
2917 a.id = SWM_ARG_ID_MOVELAST;
2921 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2922 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2925 /* place pointer at bottom left corner or nearest point inside r */
2926 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
2927 relx = win->g.w - 1;
2929 relx = r->g.x + r->g.w - win->g.x - 1;
2931 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
2932 rely = win->g.h - 1;
2934 rely = r->g.y + r->g.h - win->g.y - 1;
2936 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
2938 XMaskEvent(display, MOUSEMASK | ExposureMask |
2939 SubstructureRedirectMask, &ev);
2941 case ConfigureRequest:
2944 handler[ev.type](&ev);
2947 /* do not allow resize outside of region */
2948 if ( ev.xmotion.x_root < r->g.x ||
2949 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
2950 ev.xmotion.y_root < r->g.y ||
2951 ev.xmotion.y_root > r->g.y + r->g.h - 1)
2954 if (ev.xmotion.x <= 1)
2956 if (ev.xmotion.y <= 1)
2958 win->g.w = ev.xmotion.x + 1;
2959 win->g.h = ev.xmotion.y + 1;
2961 /* not free, don't sync more than 120 times / second */
2962 if ((ev.xmotion.time - time) > (1000 / 120) ) {
2963 time = ev.xmotion.time;
2964 XSync(display, False);
2965 resize_window(win, args->id);
2969 } while (ev.type != ButtonRelease);
2971 XSync(display, False);
2972 resize_window(win, args->id);
2974 store_float_geom(win,r);
2976 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
2978 XUngrabPointer(display, CurrentTime);
2981 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2985 move_window(struct ws_win *win)
2989 struct swm_region *r;
2991 r = root_to_region(win->wa.root);
2992 bzero(&wc, sizeof wc);
2996 wc.border_width = border_width;
2998 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
2999 win->id, wc.x, wc.y, wc.width, wc.height);
3001 XConfigureWindow(display, win->id, mask, &wc);
3005 move(struct ws_win *win, union arg *args)
3009 struct swm_region *r = win->ws->r;
3012 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
3013 win->id, win->floating, win->transient);
3015 /* in max_stack mode should only move transients */
3016 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
3020 if (win->floating == 0 && !win->transient) {
3022 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3025 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3028 /* raise the window = move to last in window list */
3029 a.id = SWM_ARG_ID_MOVELAST;
3033 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3034 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3036 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3038 XMaskEvent(display, MOUSEMASK | ExposureMask |
3039 SubstructureRedirectMask, &ev);
3041 case ConfigureRequest:
3044 handler[ev.type](&ev);
3047 /* don't allow to move window origin out of region */
3048 if ( ev.xmotion.x_root < r->g.x ||
3049 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3050 ev.xmotion.y_root < r->g.y ||
3051 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3054 win->g.x = ev.xmotion.x_root - border_width;
3055 win->g.y = ev.xmotion.y_root - border_width;
3057 /* not free, don't sync more than 120 times / second */
3058 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3059 time = ev.xmotion.time;
3060 XSync(display, False);
3065 } while (ev.type != ButtonRelease);
3067 XSync(display, False);
3070 store_float_geom(win,r);
3071 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3072 XUngrabPointer(display, CurrentTime);
3075 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
3078 /* user/key callable function IDs */
3137 /* key definitions */
3139 dummykeyfunc(struct swm_region *r, union arg *args)
3144 legacyfunc(struct swm_region *r, union arg *args)
3149 char name[SWM_FUNCNAME_LEN];
3150 void (*func)(struct swm_region *r, union arg *);
3152 } keyfuncs[kf_invalid + 1] = {
3153 /* name function argument */
3154 { "cycle_layout", cycle_layout, {0} },
3155 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
3156 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
3157 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
3158 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
3159 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
3160 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
3161 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
3162 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
3163 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
3164 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
3165 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
3166 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
3167 { "spawn_term", spawnterm, {.argv = spawn_term} },
3168 { "spawn_menu", legacyfunc, {0} },
3169 { "quit", quit, {0} },
3170 { "restart", restart, {0} },
3171 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
3172 { "ws_1", switchws, {.id = 0} },
3173 { "ws_2", switchws, {.id = 1} },
3174 { "ws_3", switchws, {.id = 2} },
3175 { "ws_4", switchws, {.id = 3} },
3176 { "ws_5", switchws, {.id = 4} },
3177 { "ws_6", switchws, {.id = 5} },
3178 { "ws_7", switchws, {.id = 6} },
3179 { "ws_8", switchws, {.id = 7} },
3180 { "ws_9", switchws, {.id = 8} },
3181 { "ws_10", switchws, {.id = 9} },
3182 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
3183 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
3184 { "ws_prior", priorws, {0} },
3185 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
3186 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
3187 { "mvws_1", send_to_ws, {.id = 0} },
3188 { "mvws_2", send_to_ws, {.id = 1} },
3189 { "mvws_3", send_to_ws, {.id = 2} },
3190 { "mvws_4", send_to_ws, {.id = 3} },
3191 { "mvws_5", send_to_ws, {.id = 4} },
3192 { "mvws_6", send_to_ws, {.id = 5} },
3193 { "mvws_7", send_to_ws, {.id = 6} },
3194 { "mvws_8", send_to_ws, {.id = 7} },
3195 { "mvws_9", send_to_ws, {.id = 8} },
3196 { "mvws_10", send_to_ws, {.id = 9} },
3197 { "bar_toggle", bar_toggle, {0} },
3198 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
3199 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
3200 { "screenshot_all", legacyfunc, {0} },
3201 { "screenshot_wind", legacyfunc, {0} },
3202 { "float_toggle", floating_toggle,{0} },
3203 { "version", version, {0} },
3204 { "spawn_lock", legacyfunc, {0} },
3205 { "spawn_initscr", legacyfunc, {0} },
3206 { "spawn_custom", dummykeyfunc, {0} },
3207 { "dumpwins", dumpwins, {0} },
3208 { "invalid key func", NULL, {0} },
3213 enum keyfuncid funcid;
3216 int keys_size = 0, keys_length = 0;
3217 struct key *keys = NULL;
3220 enum { client_click, root_click };
3222 unsigned int action;
3224 unsigned int button;
3225 void (*func)(struct ws_win *, union arg *);
3228 /* action key mouse button func args */
3229 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
3230 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
3231 { client_click, MODKEY, Button1, move, {0} },
3235 update_modkey(unsigned int mod)
3240 for (i = 0; i < keys_length; i++)
3241 if (keys[i].mod & ShiftMask)
3242 keys[i].mod = mod | ShiftMask;
3246 for (i = 0; i < LENGTH(buttons); i++)
3247 if (buttons[i].mask & ShiftMask)
3248 buttons[i].mask = mod | ShiftMask;
3250 buttons[i].mask = mod;
3260 int spawns_size = 0, spawns_length = 0;
3261 struct spawn_prog *spawns = NULL;
3264 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
3267 struct spawn_prog *prog = NULL;
3269 char *ap, **real_args;
3271 DNPRINTF(SWM_D_SPAWN, "spawn_custom %s\n", spawn_name);
3274 for (i = 0; i < spawns_length; i++) {
3275 if (!strcasecmp(spawn_name, spawns[i].name))
3279 fprintf(stderr, "spawn_custom: program %s not found\n",
3284 /* make room for expanded args */
3285 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
3286 err(1, "spawn_custom: calloc real_args");
3288 /* expand spawn_args into real_args */
3289 for (i = 0; i < prog->argc; i++) {
3291 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
3292 if (!strcasecmp(ap, "$bar_border")) {
3294 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
3296 err(1, "spawn_custom border color");
3297 } else if (!strcasecmp(ap, "$bar_color")) {
3299 strdup(r->s->c[SWM_S_COLOR_BAR].name))
3301 err(1, "spawn_custom bar color");
3302 } else if (!strcasecmp(ap, "$bar_font")) {
3303 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
3305 err(1, "spawn_custom bar fonts");
3306 } else if (!strcasecmp(ap, "$bar_font_color")) {
3308 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
3310 err(1, "spawn_custom color font");
3311 } else if (!strcasecmp(ap, "$color_focus")) {
3313 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
3315 err(1, "spawn_custom color focus");
3316 } else if (!strcasecmp(ap, "$color_unfocus")) {
3318 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
3320 err(1, "spawn_custom color unfocus");
3322 /* no match --> copy as is */
3323 if ((real_args[i] = strdup(ap)) == NULL)
3324 err(1, "spawn_custom strdup(ap)");
3326 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
3331 if ((swm_debug & SWM_D_SPAWN) != 0) {
3332 fprintf(stderr, "spawn_custom: result = ");
3333 for (i = 0; i < prog->argc; i++)
3334 fprintf(stderr, "\"%s\" ", real_args[i]);
3335 fprintf(stderr, "\n");
3341 for (i = 0; i < prog->argc; i++)
3347 setspawn(struct spawn_prog *prog)
3351 if (prog == NULL || prog->name == NULL)
3355 for (i = 0; i < spawns_length; i++) {
3356 if (!strcmp(spawns[i].name, prog->name)) {
3358 if (prog->argv == NULL) {
3360 DNPRINTF(SWM_D_SPAWN,
3361 "setspawn: delete #%d %s\n",
3363 free(spawns[i].name);
3364 for (j = 0; j < spawns[i].argc; j++)
3365 free(spawns[i].argv[j]);
3366 free(spawns[i].argv);
3367 j = spawns_length - 1;
3369 spawns[i] = spawns[j];
3374 DNPRINTF(SWM_D_SPAWN,
3375 "setspawn: replace #%d %s\n",
3377 free(spawns[i].name);
3378 for (j = 0; j < spawns[i].argc; j++)
3379 free(spawns[i].argv[j]);
3380 free(spawns[i].argv);
3383 /* found case handled */
3389 if (prog->argv == NULL) {
3391 "error: setspawn: cannot find program %s", prog->name);
3396 /* not found: add */
3397 if (spawns_size == 0 || spawns == NULL) {
3399 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
3400 spawns = malloc((size_t)spawns_size *
3401 sizeof(struct spawn_prog));
3402 if (spawns == NULL) {
3403 fprintf(stderr, "setspawn: malloc failed\n");
3407 } else if (spawns_length == spawns_size) {
3409 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
3410 spawns = realloc(spawns, (size_t)spawns_size *
3411 sizeof(struct spawn_prog));
3412 if (spawns == NULL) {
3413 fprintf(stderr, "setspawn: realloc failed\n");
3419 if (spawns_length < spawns_size) {
3420 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
3421 spawns_length, prog->name);
3422 i = spawns_length++;
3425 fprintf(stderr, "spawns array problem?\n");
3426 if (spawns == NULL) {
3427 fprintf(stderr, "spawns array is NULL!\n");
3435 setconfspawn(char *selector, char *value, int flags)
3437 struct spawn_prog *prog;
3438 char *vp, *cp, *word;
3440 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
3441 if ((prog = calloc(1, sizeof *prog)) == NULL)
3442 err(1, "setconfspawn: calloc prog");
3443 prog->name = strdup(selector);
3444 if (prog->name == NULL)
3445 err(1, "setconfspawn prog->name");
3446 if ((cp = vp = strdup(value)) == NULL)
3447 err(1, "setconfspawn: strdup(value) ");
3448 while ((word = strsep(&cp, " \t")) != NULL) {
3449 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
3451 cp += (long)strspn(cp, " \t");
3452 if (strlen(word) > 0) {
3454 prog->argv = realloc(prog->argv,
3455 prog->argc * sizeof(char *));
3456 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
3457 err(1, "setconfspawn: strdup");
3464 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
3471 setconfspawn("term", "xterm", 0);
3472 setconfspawn("screenshot_all", "screenshot.sh full", 0);
3473 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
3474 setconfspawn("lock", "xlock", 0);
3475 setconfspawn("initscr", "initscreen.sh", 0);
3476 setconfspawn("menu", "dmenu_run"
3479 " -nf $bar_font_color"
3481 " -sf $bar_color", 0);
3485 #define SWM_MODNAME_SIZE 32
3486 #define SWM_KEY_WS "\n+ \t"
3488 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
3492 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
3493 if (mod == NULL || ks == NULL) {
3494 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
3497 if (keystr == NULL || strlen(keystr) == 0) {
3498 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
3504 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
3505 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
3507 cp += (long)strspn(cp, SWM_KEY_WS);
3508 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
3510 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
3512 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
3514 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
3516 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
3518 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
3520 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
3521 *mod |= ControlMask;
3523 *ks = XStringToKeysym(name);
3524 XConvertCase(*ks, ks, &uks);
3525 if (ks == NoSymbol) {
3527 "parsekeys: invalid key %s\n",
3533 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
3538 strdupsafe(char *str)
3543 return (strdup(str));
3547 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
3550 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
3551 keyfuncs[kfid].name, spawn_name);
3553 for (i = 0; i < keys_length; i++) {
3554 if (keys[i].mod == mod && keys[i].keysym == ks) {
3555 if (kfid == kf_invalid) {
3558 "setkeybinding: delete #%d %s\n",
3559 i, keyfuncs[keys[i].funcid].name);
3560 free(keys[i].spawn_name);
3561 j = keys_length - 1;
3565 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3568 /* found: replace */
3570 "setkeybinding: replace #%d %s %s\n",
3571 i, keyfuncs[keys[i].funcid].name,
3573 free(keys[i].spawn_name);
3575 keys[i].keysym = ks;
3576 keys[i].funcid = kfid;
3577 keys[i].spawn_name = strdupsafe(spawn_name);
3578 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3583 if (kfid == kf_invalid) {
3585 "error: setkeybinding: cannot find mod/key combination");
3586 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3589 /* not found: add */
3590 if (keys_size == 0 || keys == NULL) {
3592 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
3593 keys = malloc((size_t)keys_size * sizeof(struct key));
3595 fprintf(stderr, "malloc failed\n");
3599 } else if (keys_length == keys_size) {
3601 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
3602 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
3604 fprintf(stderr, "realloc failed\n");
3609 if (keys_length < keys_size) {
3611 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
3612 j, keyfuncs[kfid].name, spawn_name);
3614 keys[j].keysym = ks;
3615 keys[j].funcid = kfid;
3616 keys[j].spawn_name = strdupsafe(spawn_name);
3618 fprintf(stderr, "keys array problem?\n");
3620 fprintf(stderr, "keys array problem\n");
3624 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3628 setconfbinding(char *selector, char *value, int flags)
3630 enum keyfuncid kfid;
3634 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
3635 if (selector == NULL) {
3636 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
3637 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3639 setkeybinding(mod, ks, kfid, NULL);
3644 /* search by key function name */
3645 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
3646 if (strncasecmp(selector, keyfuncs[kfid].name,
3647 SWM_FUNCNAME_LEN) == 0) {
3648 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3650 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3651 setkeybinding(mod, ks, kfid, NULL);
3657 /* search by custom spawn name */
3658 for (i = 0; i < spawns_length; i++) {
3659 if (strcasecmp(selector, spawns[i].name) == 0) {
3660 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3662 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3663 setkeybinding(mod, ks, kf_spawn_custom,
3670 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
3677 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
3678 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
3679 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
3680 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
3681 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
3682 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
3683 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
3684 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
3685 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
3686 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
3687 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
3688 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
3689 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
3690 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
3691 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
3692 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
3693 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
3694 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
3695 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
3696 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
3697 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
3698 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
3699 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
3700 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
3701 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
3702 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
3703 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
3704 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
3705 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
3706 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
3707 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
3708 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
3709 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
3710 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
3711 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
3712 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
3713 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
3714 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
3715 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
3716 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
3717 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
3718 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
3719 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
3720 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
3721 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
3722 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
3723 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
3724 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
3725 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
3726 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
3727 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
3728 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
3729 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
3730 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
3732 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
3737 updatenumlockmask(void)
3740 XModifierKeymap *modmap;
3742 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
3744 modmap = XGetModifierMapping(display);
3745 for (i = 0; i < 8; i++)
3746 for (j = 0; j < modmap->max_keypermod; j++)
3747 if (modmap->modifiermap[i * modmap->max_keypermod + j]
3748 == XKeysymToKeycode(display, XK_Num_Lock))
3749 numlockmask = (1 << i);
3751 XFreeModifiermap(modmap);
3757 unsigned int i, j, k;
3759 unsigned int modifiers[] =
3760 { 0, LockMask, numlockmask, numlockmask | LockMask };
3762 DNPRINTF(SWM_D_MISC, "grabkeys\n");
3763 updatenumlockmask();
3765 for (k = 0; k < ScreenCount(display); k++) {
3766 if (TAILQ_EMPTY(&screens[k].rl))
3768 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
3769 for (i = 0; i < keys_length; i++) {
3770 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
3771 for (j = 0; j < LENGTH(modifiers); j++)
3772 XGrabKey(display, code,
3773 keys[i].mod | modifiers[j],
3774 screens[k].root, True,
3775 GrabModeAsync, GrabModeAsync);
3781 grabbuttons(struct ws_win *win, int focused)
3784 unsigned int modifiers[] =
3785 { 0, LockMask, numlockmask, numlockmask|LockMask };
3787 updatenumlockmask();
3788 XUngrabButton(display, AnyButton, AnyModifier, win->id);
3790 for (i = 0; i < LENGTH(buttons); i++)
3791 if (buttons[i].action == client_click)
3792 for (j = 0; j < LENGTH(modifiers); j++)
3793 XGrabButton(display, buttons[i].button,
3794 buttons[i].mask | modifiers[j],
3795 win->id, False, BUTTONMASK,
3796 GrabModeAsync, GrabModeSync, None,
3799 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
3800 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
3803 const char *quirkname[] = {
3804 "NONE", /* config string for "no value" */
3812 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
3813 #define SWM_Q_WS "\n|+ \t"
3815 parsequirks(char *qstr, unsigned long *quirk)
3825 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
3827 cp += (long)strspn(cp, SWM_Q_WS);
3828 for (i = 0; i < LENGTH(quirkname); i++) {
3829 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
3830 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
3835 *quirk |= 1 << (i-1);
3839 if (i >= LENGTH(quirkname)) {
3840 DNPRINTF(SWM_D_QUIRK,
3841 "parsequirks: invalid quirk [%s]\n", name);
3849 setquirk(const char *class, const char *name, const int quirk)
3854 for (i = 0; i < quirks_length; i++) {
3855 if (!strcmp(quirks[i].class, class) &&
3856 !strcmp(quirks[i].name, name)) {
3859 DNPRINTF(SWM_D_QUIRK,
3860 "setquirk: delete #%d %s:%s\n",
3861 i, quirks[i].class, quirks[i].name);
3862 free(quirks[i].class);
3863 free(quirks[i].name);
3864 j = quirks_length - 1;
3866 quirks[i] = quirks[j];
3870 /* found: replace */
3871 DNPRINTF(SWM_D_QUIRK,
3872 "setquirk: replace #%d %s:%s\n",
3873 i, quirks[i].class, quirks[i].name);
3874 free(quirks[i].class);
3875 free(quirks[i].name);
3876 quirks[i].class = strdup(class);
3877 quirks[i].name = strdup(name);
3878 quirks[i].quirk = quirk;
3885 "error: setquirk: cannot find class/name combination");
3888 /* not found: add */
3889 if (quirks_size == 0 || quirks == NULL) {
3891 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
3892 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
3894 fprintf(stderr, "setquirk: malloc failed\n");
3898 } else if (quirks_length == quirks_size) {
3900 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
3901 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
3903 fprintf(stderr, "setquirk: realloc failed\n");
3908 if (quirks_length < quirks_size) {
3909 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
3910 j = quirks_length++;
3911 quirks[j].class = strdup(class);
3912 quirks[j].name = strdup(name);
3913 quirks[j].quirk = quirk;
3915 fprintf(stderr, "quirks array problem?\n");
3917 fprintf(stderr, "quirks array problem!\n");
3924 setconfquirk(char *selector, char *value, int flags)
3926 char *cp, *class, *name;
3928 unsigned long quirks;
3929 if (selector == NULL)
3931 if ((cp = strchr(selector, ':')) == NULL)
3936 if ((retval = parsequirks(value, &quirks)) == 0)
3937 setquirk(class, name, quirks);
3944 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
3945 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
3946 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
3947 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
3948 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3949 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
3950 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3951 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3952 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3953 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3954 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
3955 setquirk("pcb", "pcb", SWM_Q_FLOAT);
3958 /* conf file stuff */
3959 #define SWM_CONF_FILE "scrotwm.conf"
3961 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH, SWM_S_STACK_ENABLED,
3962 SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT, SWM_S_CYCLE_EMPTY,
3963 SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH,
3964 SWM_S_TITLE_CLASS_ENABLED, SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED,
3965 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH, SWM_S_BAR_FONT,
3966 SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM, SWM_S_SS_APP, SWM_S_DIALOG_RATIO,
3971 setconfvalue(char *selector, char *value, int flags)
3974 case SWM_S_BAR_DELAY:
3975 bar_delay = atoi(value);
3977 case SWM_S_BAR_ENABLED:
3978 bar_enabled = atoi(value);
3980 case SWM_S_BAR_BORDER_WIDTH:
3981 bar_border_width = atoi(value);
3983 case SWM_S_BAR_AT_BOTTOM:
3984 bar_at_bottom = atoi(value);
3986 case SWM_S_STACK_ENABLED:
3987 stack_enabled = atoi(value);
3989 case SWM_S_CLOCK_ENABLED:
3990 clock_enabled = atoi(value);
3992 case SWM_S_CLOCK_FORMAT:
3993 #ifndef SWM_DENY_CLOCK_FORMAT
3995 if ((clock_format = strdup(value)) == NULL)
3996 err(1, "setconfvalue: clock_format");
3999 case SWM_S_CYCLE_EMPTY:
4000 cycle_empty = atoi(value);
4002 case SWM_S_CYCLE_VISIBLE:
4003 cycle_visible = atoi(value);
4005 case SWM_S_SS_ENABLED:
4006 ss_enabled = atoi(value);
4008 case SWM_S_TERM_WIDTH:
4009 term_width = atoi(value);
4011 case SWM_S_TITLE_CLASS_ENABLED:
4012 title_class_enabled = atoi(value);
4014 case SWM_S_WINDOW_NAME_ENABLED:
4015 window_name_enabled = atoi(value);
4017 case SWM_S_TITLE_NAME_ENABLED:
4018 title_name_enabled = atoi(value);
4020 case SWM_S_FOCUS_MODE:
4021 if (!strcmp(value, "default"))
4022 focus_mode = SWM_FOCUS_DEFAULT;
4023 else if (!strcmp(value, "follow_cursor"))
4024 focus_mode = SWM_FOCUS_FOLLOW;
4025 else if (!strcmp(value, "synergy"))
4026 focus_mode = SWM_FOCUS_SYNERGY;
4028 err(1, "focus_mode");
4030 case SWM_S_DISABLE_BORDER:
4031 disable_border = atoi(value);
4033 case SWM_S_BORDER_WIDTH:
4034 border_width = atoi(value);
4036 case SWM_S_BAR_FONT:
4038 if ((bar_fonts[0] = strdup(value)) == NULL)
4039 err(1, "setconfvalue: bar_font");
4041 case SWM_S_BAR_ACTION:
4043 if ((bar_argv[0] = strdup(value)) == NULL)
4044 err(1, "setconfvalue: bar_action");
4046 case SWM_S_SPAWN_TERM:
4047 free(spawn_term[0]);
4048 if ((spawn_term[0] = strdup(value)) == NULL)
4049 err(1, "setconfvalue: spawn_term");
4053 case SWM_S_DIALOG_RATIO:
4054 dialog_ratio = atof(value);
4055 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
4065 setconfmodkey(char *selector, char *value, int flags)
4067 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
4068 update_modkey(Mod1Mask);
4069 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
4070 update_modkey(Mod2Mask);
4071 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
4072 update_modkey(Mod3Mask);
4073 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
4074 update_modkey(Mod4Mask);
4081 setconfcolor(char *selector, char *value, int flags)
4083 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
4088 setconfregion(char *selector, char *value, int flags)
4090 custom_region(value);
4094 /* config options */
4095 struct config_option {
4097 int (*func)(char*, char*, int);
4100 struct config_option configopt[] = {
4101 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
4102 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
4103 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
4104 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
4105 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
4106 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
4107 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
4108 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
4109 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
4110 { "bind", setconfbinding, 0 },
4111 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
4112 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
4113 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
4114 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
4115 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
4116 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
4117 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
4118 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
4119 { "modkey", setconfmodkey, 0 },
4120 { "program", setconfspawn, 0 },
4121 { "quirk", setconfquirk, 0 },
4122 { "region", setconfregion, 0 },
4123 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
4124 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
4125 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
4126 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
4127 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
4128 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
4129 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
4130 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
4131 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
4132 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
4137 conf_load(char *filename)
4140 char *line, *cp, *optsub, *optval;
4141 size_t linelen, lineno = 0;
4142 int wordlen, i, optind;
4143 struct config_option *opt;
4145 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
4147 if (filename == NULL) {
4148 fprintf(stderr, "conf_load: no filename\n");
4151 if ((config = fopen(filename, "r")) == NULL) {
4152 warn("conf_load: fopen");
4156 while (!feof(config)) {
4157 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
4160 err(1, "%s", filename);
4165 cp += strspn(cp, " \t\n"); /* eat whitespace */
4166 if (cp[0] == '\0') {
4171 /* get config option */
4172 wordlen = strcspn(cp, "=[ \t\n");
4174 warnx("%s: line %zd: no option found",
4179 for (i = 0; i < LENGTH(configopt); i++) {
4180 opt = &configopt[i];
4181 if (!strncasecmp(cp, opt->optname, wordlen) &&
4182 strlen(opt->optname) == wordlen) {
4188 warnx("%s: line %zd: unknown option %.*s",
4189 filename, lineno, wordlen, cp);
4193 cp += strspn(cp, " \t\n"); /* eat whitespace */
4194 /* get [selector] if any */
4198 wordlen = strcspn(cp, "]");
4201 warnx("%s: line %zd: syntax error",
4205 asprintf(&optsub, "%.*s", wordlen, cp);
4208 cp += strspn(cp, "] \t\n"); /* eat trailing */
4210 cp += strspn(cp, "= \t\n"); /* eat trailing */
4212 optval = strdup(cp);
4213 /* call function to deal with it all */
4214 if (configopt[optind].func(optsub, optval,
4215 configopt[optind].funcflags) != 0) {
4216 fprintf(stderr, "%s line %zd: %s\n",
4217 filename, lineno, line);
4218 errx(1, "%s: line %zd: invalid data for %s",
4219 filename, lineno, configopt[optind].optname);
4227 DNPRINTF(SWM_D_CONF, "conf_load end\n");
4233 set_child_transient(struct ws_win *win)
4235 struct ws_win *parent;
4237 parent = find_window(win->transient);
4239 parent->child_trans = win;
4243 manage_window(Window id)
4246 struct workspace *ws;
4247 struct ws_win *win, *ww;
4248 int format, i, ws_idx, n, border_me = 0;
4249 unsigned long nitems, bytes;
4250 Atom ws_idx_atom = 0, type;
4251 Atom *prot = NULL, *pp;
4252 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
4253 struct swm_region *r;
4258 if ((win = find_window(id)) != NULL)
4259 return (win); /* already being managed */
4261 /* see if we are on the unmanaged list */
4262 if ((win = find_unmanaged_window(id)) != NULL) {
4263 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
4265 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
4266 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
4268 set_child_transient(win);
4269 ewmh_update_actions(win);
4273 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
4274 errx(1, "calloc: failed to allocate memory for new window");
4276 /* Get all the window data in one shot */
4277 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4279 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
4280 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
4281 XGetWindowAttributes(display, id, &win->wa);
4282 XGetWMNormalHints(display, id, &win->sh, &mask);
4283 XGetTransientForHint(display, id, &trans);
4285 win->transient = trans;
4286 set_child_transient(win);
4287 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
4288 win->id, win->transient);
4291 /* get supported protocols */
4292 if (XGetWMProtocols(display, id, &prot, &n)) {
4293 for (i = 0, pp = prot; i < n; i++, pp++) {
4294 if (*pp == takefocus)
4295 win->take_focus = 1;
4297 win->can_delete = 1;
4304 * Figure out where to put the window. If it was previously assigned to
4305 * a workspace (either by spawn() or manually moving), and isn't
4306 * transient, * put it in the same workspace
4308 r = root_to_region(win->wa.root);
4309 if (prop && win->transient == 0) {
4310 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
4311 ws_idx = strtonum(prop, 0, 9, &errstr);
4313 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
4316 ws = &r->s->ws[ws_idx];
4319 /* this should launch transients in the same ws as parent */
4321 if ((ww = find_window(trans)) != NULL)
4328 "fix this bug mcbride\n");
4333 /* set up the window layout */
4336 win->s = r->s; /* this never changes */
4337 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
4339 win->g.w = win->wa.width;
4340 win->g.h = win->wa.height;
4341 win->g.x = win->wa.x;
4342 win->g.y = win->wa.y;
4343 win->g_floatvalid = 0;
4344 win->floatmaxed = 0;
4345 win->ewmh_flags = 0;
4347 /* Set window properties so we can remember this after reincarnation */
4348 if (ws_idx_atom && prop == NULL &&
4349 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
4350 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
4352 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
4353 PropModeReplace, ws_idx_str, SWM_PROPLEN);
4358 ewmh_autoquirk(win);
4360 if (XGetClassHint(display, win->id, &win->ch)) {
4361 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
4362 win->ch.res_class, win->ch.res_name);
4364 /* java is retarded so treat it special */
4365 if (strstr(win->ch.res_name, "sun-awt")) {
4370 for (i = 0; i < quirks_length; i++){
4371 if (!strcmp(win->ch.res_class, quirks[i].class) &&
4372 !strcmp(win->ch.res_name, quirks[i].name)) {
4373 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
4374 win->ch.res_class, win->ch.res_name);
4375 if (quirks[i].quirk & SWM_Q_FLOAT) {
4379 win->quirks = quirks[i].quirk;
4384 /* alter window position if quirky */
4385 if (win->quirks & SWM_Q_ANYWHERE) {
4386 win->manual = 1; /* don't center the quirky windows */
4387 bzero(&wc, sizeof wc);
4389 if (bar_enabled && win->g.y < bar_height) {
4390 win->g.y = wc.y = bar_height;
4393 if (win->g.w + win->g.x > WIDTH(r)) {
4394 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
4400 /* Reset font sizes (the bruteforce way; no default keybinding). */
4401 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
4402 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
4403 fake_keypress(win, XK_KP_Subtract, ShiftMask);
4404 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
4405 fake_keypress(win, XK_KP_Add, ShiftMask);
4408 ewmh_get_win_state(win);
4409 ewmh_update_actions(win);
4410 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
4414 bzero(&wc, sizeof wc);
4415 wc.border_width = border_width;
4416 mask = CWBorderWidth;
4417 XConfigureWindow(display, win->id, mask, &wc);
4420 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
4421 PropertyChangeMask | StructureNotifyMask);
4423 set_win_state(win, NormalState);
4425 /* floaters need to be mapped if they are in the current workspace */
4426 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
4427 XMapRaised(display, win->id);
4433 free_window(struct ws_win *win)
4435 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
4440 /* needed for restart wm */
4441 set_win_state(win, WithdrawnState);
4443 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
4445 if (win->ch.res_class)
4446 XFree(win->ch.res_class);
4447 if (win->ch.res_name)
4448 XFree(win->ch.res_name);
4453 memset(win, 0xff, sizeof *win); /* XXX kill later */
4459 unmanage_window(struct ws_win *win)
4461 struct ws_win *parent;
4466 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
4468 if (win->transient) {
4469 parent = find_window(win->transient);
4471 parent->child_trans = NULL;
4474 /* focus on root just in case */
4475 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
4480 TAILQ_REMOVE(&win->ws->winlist, win, entry);
4481 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
4487 focus_magic(struct ws_win *win, int do_trans)
4489 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu %d\n", WINID(win), do_trans);
4494 if (do_trans == SWM_F_TRANSIENT && win->child_trans) {
4495 /* win = parent & has a transient so focus on that */
4497 focus_win(win->child_trans);
4498 if (win->child_trans->take_focus)
4499 client_msg(win, takefocus);
4501 /* make sure transient hasn't dissapeared */
4502 if (validate_win(win->child_trans) == 0) {
4503 focus_win(win->child_trans);
4504 if (win->child_trans->take_focus)
4505 client_msg(win->child_trans, takefocus);
4507 win->child_trans = NULL;
4509 if (win->take_focus)
4510 client_msg(win, takefocus);
4516 if (win->take_focus)
4517 client_msg(win, takefocus);
4524 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
4532 XKeyEvent *ev = &e->xkey;
4534 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
4536 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
4537 for (i = 0; i < keys_length; i++)
4538 if (keysym == keys[i].keysym
4539 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
4540 && keyfuncs[keys[i].funcid].func) {
4541 if (keys[i].funcid == kf_spawn_custom)
4543 root_to_region(ev->root),
4544 &(keyfuncs[keys[i].funcid].args),
4548 keyfuncs[keys[i].funcid].func(
4549 root_to_region(ev->root),
4550 &(keyfuncs[keys[i].funcid].args)
4556 buttonpress(XEvent *e)
4560 XButtonPressedEvent *ev = &e->xbutton;
4562 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
4564 action = root_click;
4565 if ((win = find_window(ev->window)) == NULL)
4568 focus_magic(win, SWM_F_TRANSIENT);
4569 action = client_click;
4571 for (i = 0; i < LENGTH(buttons); i++)
4572 if (action == buttons[i].action && buttons[i].func &&
4573 buttons[i].button == ev->button &&
4574 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
4575 buttons[i].func(win, &buttons[i].args);
4579 configurerequest(XEvent *e)
4581 XConfigureRequestEvent *ev = &e->xconfigurerequest;
4586 if ((win = find_window(ev->window)) == NULL)
4587 if ((win = find_unmanaged_window(ev->window)) == NULL)
4591 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
4593 bzero(&wc, sizeof wc);
4596 wc.width = ev->width;
4597 wc.height = ev->height;
4598 wc.border_width = ev->border_width;
4599 wc.sibling = ev->above;
4600 wc.stack_mode = ev->detail;
4601 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
4603 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
4605 config_win(win, ev);
4610 configurenotify(XEvent *e)
4615 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
4616 e->xconfigure.window);
4618 win = find_window(e->xconfigure.window);
4620 XGetWMNormalHints(display, win->id, &win->sh, &mask);
4628 destroynotify(XEvent *e)
4631 XDestroyWindowEvent *ev = &e->xdestroywindow;
4633 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
4635 if ((win = find_window(ev->window)) == NULL) {
4636 if ((win = find_unmanaged_window(ev->window)) == NULL)
4642 /* make sure we focus on something */
4645 unmanage_window(win);
4651 enternotify(XEvent *e)
4653 XCrossingEvent *ev = &e->xcrossing;
4658 Window focus_return;
4659 int revert_to_return;
4661 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
4662 "%lu subwindow %lu same_screen %d focus %d state %d\n",
4663 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
4664 ev->same_screen, ev->focus, ev->state);
4666 switch (focus_mode) {
4667 case SWM_FOCUS_DEFAULT:
4668 if (QLength(display)) {
4669 DNPRINTF(SWM_D_EVENT, "ignore enternotify %d\n",
4674 case SWM_FOCUS_FOLLOW:
4676 case SWM_FOCUS_SYNERGY:
4679 * all these checks need to be in this order because the
4680 * XCheckTypedWindowEvent relies on weeding out the previous events
4682 * making this code an option would enable a follow mouse for focus
4687 * state is set when we are switching workspaces and focus is set when
4688 * the window or a subwindow already has focus (occurs during restart).
4690 * Only honor the focus flag if last_focus_event is not FocusOut,
4691 * this allows scrotwm to continue to control focus when another
4692 * program is also playing with it.
4694 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
4695 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
4700 * happens when a window is created or destroyed and the border
4701 * crosses the mouse pointer and when switching ws
4703 * we need the subwindow test to see if we came from root in order
4704 * to give focus to floaters
4706 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
4707 ev->subwindow == 0) {
4708 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
4712 /* this window already has focus */
4713 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
4714 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
4718 /* this window is being deleted or moved to another ws */
4719 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
4721 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
4722 XPutBackEvent(display, &cne);
4726 if ((win = find_window(ev->window)) == NULL) {
4727 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
4732 * In fullstack kill all enters unless they come from a different ws
4733 * (i.e. another region) or focus has been grabbed externally.
4735 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
4736 last_focus_event != FocusOut) {
4737 XGetInputFocus(display, &focus_return, &revert_to_return);
4738 if ((w = find_window(focus_return)) == NULL ||
4740 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
4748 if ((win = find_window(ev->window)) == NULL) {
4749 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
4754 * if we have more enternotifies let them handle it in due time
4756 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
4757 DNPRINTF(SWM_D_EVENT,
4758 "ignoring enternotify: got more enternotify\n");
4759 XPutBackEvent(display, &cne);
4763 focus_magic(win, SWM_F_TRANSIENT);
4766 /* lets us use one switch statement for arbitrary mode/detail combinations */
4767 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
4770 focusevent(XEvent *e)
4774 u_int32_t mode_detail;
4775 XFocusChangeEvent *ev = &e->xfocus;
4777 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
4778 ev->type == FocusIn ? "entering" : "leaving",
4779 ev->window, ev->mode, ev->detail);
4781 if (last_focus_event == ev->type) {
4782 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
4786 last_focus_event = ev->type;
4787 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
4789 switch (mode_detail) {
4790 /* synergy client focus operations */
4791 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
4792 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
4794 /* synergy server focus operations */
4795 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
4797 /* Entering applications like rdesktop that mangle the pointer */
4798 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
4800 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
4801 XSetWindowBorder(display, win->id,
4802 win->ws->r->s->c[ev->type == FocusIn ?
4803 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
4806 fprintf(stderr, "ignoring focusevent\n");
4807 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
4814 mapnotify(XEvent *e)
4817 XMapEvent *ev = &e->xmap;
4819 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
4821 win = manage_window(ev->window);
4823 set_win_state(win, NormalState);
4827 mappingnotify(XEvent *e)
4829 XMappingEvent *ev = &e->xmapping;
4831 XRefreshKeyboardMapping(ev);
4832 if (ev->request == MappingKeyboard)
4837 maprequest(XEvent *e)
4840 struct swm_region *r;
4841 XWindowAttributes wa;
4842 XMapRequestEvent *ev = &e->xmaprequest;
4844 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
4845 e->xmaprequest.window);
4847 if (!XGetWindowAttributes(display, ev->window, &wa))
4849 if (wa.override_redirect)
4852 win = manage_window(e->xmaprequest.window);
4854 return; /* can't happen */
4858 /* make new win focused */
4859 r = root_to_region(win->wa.root);
4860 if (win->ws == r->ws)
4861 focus_magic(win, SWM_F_GENERIC);
4865 propertynotify(XEvent *e)
4868 XPropertyEvent *ev = &e->xproperty;
4870 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
4873 if (ev->state == PropertyDelete)
4874 return; /* ignore */
4875 win = find_window(ev->window);
4880 case XA_WM_NORMAL_HINTS:
4883 XGetWMNormalHints(display, win->id, &win->sh, &mask);
4884 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
4885 if (win->sh.flags & PMinSize) {
4886 win->g.w = win->sh.min_width;
4887 win->g.h = win->sh.min_height;
4888 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
4890 XMoveResizeWindow(display, win->id,
4891 win->g.x, win->g.y, win->g.w, win->g.h);
4893 if (window_name_enabled)
4902 unmapnotify(XEvent *e)
4906 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
4908 /* determine if we need to help unmanage this window */
4909 win = find_window(e->xunmap.window);
4913 if (getstate(e->xunmap.window) == NormalState) {
4914 unmanage_window(win);
4920 visibilitynotify(XEvent *e)
4923 struct swm_region *r;
4925 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
4926 e->xvisibility.window);
4927 if (e->xvisibility.state == VisibilityUnobscured)
4928 for (i = 0; i < ScreenCount(display); i++)
4929 TAILQ_FOREACH(r, &screens[i].rl, entry)
4930 if (e->xvisibility.window == r->bar_window)
4935 clientmessage(XEvent *e)
4937 XClientMessageEvent *ev;
4942 win = find_window(ev->window);
4946 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx type: %ld \n",
4947 ev->window, ev->message_type);
4949 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
4950 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW \n");
4953 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
4954 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW \n");
4955 if (win->can_delete)
4956 client_msg(win, adelete);
4958 XKillClient(display, win->id);
4960 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
4961 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_MOVERESIZE_WINDOW \n");
4962 if (win->floating) {
4963 if (ev->data.l[0] & (1<<8)) /* x */
4964 win->g.x = ev->data.l[1];
4965 if (ev->data.l[0] & (1<<9)) /* y */
4966 win->g.y = ev->data.l[2];
4967 if (ev->data.l[0] & (1<<10)) /* width */
4968 win->g.w = ev->data.l[3];
4969 if (ev->data.l[0] & (1<<11)) /* height */
4970 win->g.h = ev->data.l[4];
4973 /* TODO: Change stack sizes */
4975 config_win(win, NULL);
4977 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
4978 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE \n");
4979 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
4981 ewmh_update_win_state(win, ev->data.l[2], ev->data.l[0]);
4988 xerror_start(Display *d, XErrorEvent *ee)
4995 xerror(Display *d, XErrorEvent *ee)
4997 /* fprintf(stderr, "error: %p %p\n", display, ee); */
5005 xerrorxlib = XSetErrorHandler(xerror_start);
5007 /* this causes an error if some other window manager is running */
5008 XSelectInput(display, DefaultRootWindow(display),
5009 SubstructureRedirectMask);
5010 XSync(display, False);
5014 XSetErrorHandler(xerror);
5015 XSync(display, False);
5020 new_region(struct swm_screen *s, int x, int y, int w, int h)
5022 struct swm_region *r, *n;
5023 struct workspace *ws = NULL;
5026 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
5027 s->idx, w, h, x, y);
5029 /* remove any conflicting regions */
5030 n = TAILQ_FIRST(&s->rl);
5033 n = TAILQ_NEXT(r, entry);
5034 if (X(r) < (x + w) &&
5035 (X(r) + WIDTH(r)) > x &&
5037 (Y(r) + HEIGHT(r)) > y) {
5038 if (r->ws->r != NULL)
5039 r->ws->old_r = r->ws->r;
5041 XDestroyWindow(display, r->bar_window);
5042 TAILQ_REMOVE(&s->rl, r, entry);
5043 TAILQ_INSERT_TAIL(&s->orl, r, entry);
5047 /* search old regions for one to reuse */
5049 /* size + location match */
5050 TAILQ_FOREACH(r, &s->orl, entry)
5051 if (X(r) == x && Y(r) == y &&
5052 HEIGHT(r) == h && WIDTH(r) == w)
5056 TAILQ_FOREACH(r, &s->orl, entry)
5057 if (HEIGHT(r) == h && WIDTH(r) == w)
5061 TAILQ_REMOVE(&s->orl, r, entry);
5062 /* try to use old region's workspace */
5063 if (r->ws->r == NULL)
5066 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
5067 errx(1, "calloc: failed to allocate memory for screen");
5069 /* if we don't have a workspace already, find one */
5071 for (i = 0; i < SWM_WS_MAX; i++)
5072 if (s->ws[i].r == NULL) {
5079 errx(1, "no free workspaces\n");
5090 TAILQ_INSERT_TAIL(&s->rl, r, entry);
5096 #ifdef SWM_XRR_HAS_CRTC
5098 XRRScreenResources *sr;
5101 #endif /* SWM_XRR_HAS_CRTC */
5102 struct swm_region *r;
5105 if (i >= ScreenCount(display))
5106 errx(1, "invalid screen");
5108 /* remove any old regions */
5109 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
5110 r->ws->old_r = r->ws->r = NULL;
5111 XDestroyWindow(display, r->bar_window);
5112 TAILQ_REMOVE(&screens[i].rl, r, entry);
5113 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
5117 /* map virtual screens onto physical screens */
5118 #ifdef SWM_XRR_HAS_CRTC
5119 if (xrandr_support) {
5120 sr = XRRGetScreenResources(display, screens[i].root);
5122 new_region(&screens[i], 0, 0,
5123 DisplayWidth(display, i),
5124 DisplayHeight(display, i));
5128 for (c = 0, ci = NULL; c < ncrtc; c++) {
5129 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
5130 if (ci->noutput == 0)
5133 if (ci != NULL && ci->mode == None)
5134 new_region(&screens[i], 0, 0,
5135 DisplayWidth(display, i),
5136 DisplayHeight(display, i));
5138 new_region(&screens[i],
5139 ci->x, ci->y, ci->width, ci->height);
5142 XRRFreeCrtcInfo(ci);
5143 XRRFreeScreenResources(sr);
5145 #endif /* SWM_XRR_HAS_CRTC */
5147 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
5148 DisplayHeight(display, i));
5153 screenchange(XEvent *e) {
5154 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
5155 struct swm_region *r;
5158 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
5160 if (!XRRUpdateConfiguration(e))
5163 /* silly event doesn't include the screen index */
5164 for (i = 0; i < ScreenCount(display); i++)
5165 if (screens[i].root == xe->root)
5167 if (i >= ScreenCount(display))
5168 errx(1, "screenchange: screen not found\n");
5170 /* brute force for now, just re-enumerate the regions */
5173 /* add bars to all regions */
5174 for (i = 0; i < ScreenCount(display); i++)
5175 TAILQ_FOREACH(r, &screens[i].rl, entry)
5183 Window d1, d2, *wins = NULL;
5184 XWindowAttributes wa;
5189 for (i = 0; i < ScreenCount(display); i++) {
5190 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
5193 /* attach windows to a region */
5194 /* normal windows */
5195 for (j = 0; j < no; j++) {
5196 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5197 wa.override_redirect ||
5198 XGetTransientForHint(display, wins[j], &d1))
5201 state = getstate(wins[j]);
5202 manage = state == IconicState;
5203 if (wa.map_state == IsViewable || manage)
5204 manage_window(wins[j]);
5206 /* transient windows */
5207 for (j = 0; j < no; j++) {
5208 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5209 wa.override_redirect)
5212 state = getstate(wins[j]);
5213 manage = state == IconicState;
5214 if (XGetTransientForHint(display, wins[j], &d1) &&
5216 manage_window(wins[j]);
5229 int errorbase, major, minor;
5230 struct workspace *ws;
5233 if ((screens = calloc(ScreenCount(display),
5234 sizeof(struct swm_screen))) == NULL)
5235 errx(1, "calloc: screens");
5237 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
5239 /* initial Xrandr setup */
5240 xrandr_support = XRRQueryExtension(display,
5241 &xrandr_eventbase, &errorbase);
5243 if (XRRQueryVersion(display, &major, &minor) && major < 1)
5246 /* map physical screens */
5247 for (i = 0; i < ScreenCount(display); i++) {
5248 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
5250 TAILQ_INIT(&screens[i].rl);
5251 TAILQ_INIT(&screens[i].orl);
5252 screens[i].root = RootWindow(display, i);
5254 /* set default colors */
5255 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
5256 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
5257 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
5258 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
5259 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
5261 /* set default cursor */
5262 XDefineCursor(display, screens[i].root,
5263 XCreateFontCursor(display, XC_left_ptr));
5265 /* init all workspaces */
5266 /* XXX these should be dynamically allocated too */
5267 for (j = 0; j < SWM_WS_MAX; j++) {
5268 ws = &screens[i].ws[j];
5273 TAILQ_INIT(&ws->winlist);
5274 TAILQ_INIT(&ws->unmanagedlist);
5276 for (k = 0; layouts[k].l_stack != NULL; k++)
5277 if (layouts[k].l_config != NULL)
5278 layouts[k].l_config(ws,
5279 SWM_ARG_ID_STACKINIT);
5280 ws->cur_layout = &layouts[0];
5286 XRRSelectInput(display, screens[i].root,
5287 RRScreenChangeNotifyMask);
5294 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
5296 err(1, "setup_globals: strdup");
5297 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
5299 err(1, "setup_globals: strdup");
5300 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
5302 err(1, "setup_globals: strdup");
5303 if ((spawn_term[0] = strdup("xterm")) == NULL)
5304 err(1, "setup_globals: strdup");
5305 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
5313 Atom netwmcheck, netwmname, utf8_string;
5316 /* work around sun jdk bugs, code from wmname */
5317 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
5318 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
5319 utf8_string = XInternAtom(display, "UTF8_STRING", False);
5320 for (i = 0; i < ScreenCount(display); i++) {
5321 root = screens[i].root;
5322 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
5323 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
5324 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
5326 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
5327 PropModeReplace, (unsigned char *)&win,1);
5328 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
5329 PropModeReplace, (unsigned char *)&win,1);
5330 XChangeProperty(display, win, netwmname, utf8_string, 8,
5331 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
5336 main(int argc, char *argv[])
5339 struct swm_region *r, *rr;
5340 struct ws_win *winfocus = NULL;
5343 char conf[PATH_MAX], *cfile = NULL;
5348 struct sigaction sact;
5351 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
5352 SWM_VERSION, cvstag);
5353 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
5354 warnx("no locale support");
5356 if (!(display = XOpenDisplay(0)))
5357 errx(1, "can not open display");
5360 errx(1, "other wm running");
5362 /* handle some signals */
5363 bzero(&sact, sizeof(sact));
5364 sigemptyset(&sact.sa_mask);
5366 sact.sa_handler = sighdlr;
5367 sigaction(SIGINT, &sact, NULL);
5368 sigaction(SIGQUIT, &sact, NULL);
5369 sigaction(SIGTERM, &sact, NULL);
5370 sigaction(SIGHUP, &sact, NULL);
5372 sact.sa_handler = sighdlr;
5373 sact.sa_flags = SA_NOCLDSTOP;
5374 sigaction(SIGCHLD, &sact, NULL);
5376 astate = XInternAtom(display, "WM_STATE", False);
5377 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
5378 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
5379 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
5381 /* look for local and global conf file */
5382 pwd = getpwuid(getuid());
5384 errx(1, "invalid user %d", getuid());
5393 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
5394 if (stat(conf, &sb) != -1) {
5395 if (S_ISREG(sb.st_mode))
5398 /* try global conf file */
5399 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
5400 if (!stat(conf, &sb))
5401 if (S_ISREG(sb.st_mode))
5408 /* set some values to work around bad programs */
5411 /* grab existing windows (before we build the bars) */
5414 /* setup all bars */
5415 for (i = 0; i < ScreenCount(display); i++)
5416 TAILQ_FOREACH(r, &screens[i].rl, entry) {
5417 if (winfocus == NULL)
5418 winfocus = TAILQ_FIRST(&r->ws->winlist);
5427 xfd = ConnectionNumber(display);
5429 while (XPending(display)) {
5430 XNextEvent(display, &e);
5433 if (e.type < LASTEvent) {
5435 if (handler[e.type])
5436 handler[e.type](&e);
5438 DNPRINTF(SWM_D_EVENT,
5439 "win: %lu unknown event: %d\n",
5440 e.xany.window, e.type);
5442 switch (e.type - xrandr_eventbase) {
5443 case RRScreenChangeNotify:
5447 DNPRINTF(SWM_D_EVENT,
5448 "win: %lu unknown xrandr event: "
5449 "%d\n", e.xany.window, e.type);
5455 /* if we are being restarted go focus on first window */
5457 rr = winfocus->ws->r;
5459 /* not a visible window */
5463 /* move pointer to first screen if multi screen */
5464 if (ScreenCount(display) > 1 || outputs > 1)
5465 XWarpPointer(display, None, rr->s[0].root,
5466 0, 0, 0, 0, rr->g.x,
5467 rr->g.y + (bar_enabled ? bar_height : 0));
5469 a.id = SWM_ARG_ID_FOCUSCUR;
5479 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
5481 DNPRINTF(SWM_D_MISC, "select failed");
5482 if (restart_wm == 1)
5483 restart(NULL, NULL);
5494 XFreeGC(display, bar_gc);
5495 XCloseDisplay(display);