3 * Copyright (c) 2009-2010-2011 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009-2010-2011 Ryan McBride <mcbride@countersiege.com>
5 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
6 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Much code and ideas taken from dwm under the following license:
23 * MIT/X Consortium License
25 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
26 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
27 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
28 * 2007 Premysl Hruby <dfenze at gmail dot com>
29 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
30 * 2007 Christof Musik <christof at sendfax dot de>
31 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
32 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
33 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
35 * Permission is hereby granted, free of charge, to any person obtaining a
36 * copy of this software and associated documentation files (the "Software"),
37 * to deal in the Software without restriction, including without limitation
38 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
39 * and/or sell copies of the Software, and to permit persons to whom the
40 * Software is furnished to do so, subject to the following conditions:
42 * The above copyright notice and this permission notice shall be included in
43 * all copies or substantial portions of the Software.
45 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
46 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
47 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
48 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
49 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
50 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
51 * DEALINGS IN THE SOFTWARE.
54 static const char *cvstag = "$scrotwm$";
56 #define SWM_VERSION "0.9.30"
73 #include <sys/types.h>
77 #include <sys/queue.h>
78 #include <sys/param.h>
79 #include <sys/select.h>
81 #include <X11/cursorfont.h>
82 #include <X11/keysym.h>
83 #include <X11/Xatom.h>
85 #include <X11/Xproto.h>
86 #include <X11/Xutil.h>
87 #include <X11/extensions/Xrandr.h>
94 # error XRandR versions less than 1.0 are not supported
99 #define SWM_XRR_HAS_CRTC
103 /*#define SWM_DEBUG*/
105 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
106 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
107 #define SWM_D_MISC 0x0001
108 #define SWM_D_EVENT 0x0002
109 #define SWM_D_WS 0x0004
110 #define SWM_D_FOCUS 0x0008
111 #define SWM_D_MOVE 0x0010
112 #define SWM_D_STACK 0x0020
113 #define SWM_D_MOUSE 0x0040
114 #define SWM_D_PROP 0x0080
115 #define SWM_D_CLASS 0x0100
116 #define SWM_D_KEY 0x0200
117 #define SWM_D_QUIRK 0x0400
118 #define SWM_D_SPAWN 0x0800
119 #define SWM_D_EVENTQ 0x1000
120 #define SWM_D_CONF 0x2000
122 u_int32_t swm_debug = 0
139 #define DPRINTF(x...)
140 #define DNPRINTF(n,x...)
143 #define LENGTH(x) (sizeof x / sizeof x[0])
144 #define MODKEY Mod1Mask
145 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
146 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
147 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
148 #define SWM_PROPLEN (16)
149 #define SWM_FUNCNAME_LEN (32)
150 #define SWM_KEYS_LEN (255)
151 #define SWM_QUIRK_LEN (64)
152 #define X(r) (r)->g.x
153 #define Y(r) (r)->g.y
154 #define WIDTH(r) (r)->g.w
155 #define HEIGHT(r) (r)->g.h
156 #define SWM_MAX_FONT_STEPS (3)
157 #define WINID(w) (w ? w->id : 0)
159 #define SWM_FOCUS_DEFAULT (0)
160 #define SWM_FOCUS_SYNERGY (1)
161 #define SWM_FOCUS_FOLLOW (2)
164 #define SWM_LIB "/usr/local/lib/libswmhack.so"
176 volatile sig_atomic_t running = 1;
177 volatile sig_atomic_t restart_wm = 0;
179 int last_focus_event = FocusOut;
180 int (*xerrorxlib)(Display *, XErrorEvent *);
184 int xrandr_eventbase;
185 unsigned int numlockmask = 0;
189 int cycle_visible = 0;
191 int font_adjusted = 0;
192 unsigned int mod_key = MODKEY;
195 struct swm_region *search_r;
196 int select_list_pipe[2];
197 int select_resp_pipe[2];
199 volatile sig_atomic_t search_resp;
202 double dialog_ratio = .6;
204 #define SWM_BAR_MAX (256)
205 char *bar_argv[] = { NULL, NULL };
207 char bar_ext[SWM_BAR_MAX];
208 char bar_vertext[SWM_BAR_MAX];
210 sig_atomic_t bar_alarm = 0;
213 int bar_border_width = 1;
214 int bar_at_bottom = 0;
216 int bar_extra_running = 0;
219 int stack_enabled = 1;
220 int clock_enabled = 1;
221 char *clock_format = NULL;
222 int title_name_enabled = 0;
223 int title_class_enabled = 0;
224 int window_name_enabled = 0;
225 int focus_mode = SWM_FOCUS_DEFAULT;
226 int disable_border = 0;
227 int border_width = 1;
233 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
234 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
236 #define SWM_MENU_FN (2)
237 #define SWM_MENU_NB (4)
238 #define SWM_MENU_NF (6)
239 #define SWM_MENU_SB (8)
240 #define SWM_MENU_SF (10)
242 /* layout manager data */
243 struct swm_geometry {
253 /* virtual "screens" */
255 TAILQ_ENTRY(swm_region) entry;
256 struct swm_geometry g;
257 struct workspace *ws; /* current workspace on this region */
258 struct workspace *ws_prior; /* prior workspace on this region */
259 struct swm_screen *s; /* screen idx */
262 TAILQ_HEAD(swm_region_list, swm_region);
265 TAILQ_ENTRY(ws_win) entry;
268 struct ws_win *child_trans; /* transient child window */
269 struct swm_geometry g; /* current geometry */
270 struct swm_geometry g_float; /* geometry when floating */
271 struct swm_geometry rg_float; /* region geom when floating */
272 int g_floatvalid; /* flag: geometry in g_float is valid */
273 int floatmaxed; /* flag: floater was maxed in max_stack */
277 unsigned int ewmh_flags;
278 int font_size_boundary[SWM_MAX_FONT_STEPS];
284 unsigned long quirks;
285 struct workspace *ws; /* always valid */
286 struct swm_screen *s; /* always valid, never changes */
287 XWindowAttributes wa;
292 TAILQ_HEAD(ws_win_list, ws_win);
294 /* layout handlers */
296 void vertical_config(struct workspace *, int);
297 void vertical_stack(struct workspace *, struct swm_geometry *);
298 void horizontal_config(struct workspace *, int);
299 void horizontal_stack(struct workspace *, struct swm_geometry *);
300 void max_stack(struct workspace *, struct swm_geometry *);
302 struct ws_win *find_window(Window);
304 void grabbuttons(struct ws_win *, int);
305 void new_region(struct swm_screen *, int, int, int, int);
306 void unmanage_window(struct ws_win *);
307 long getstate(Window);
310 void (*l_stack)(struct workspace *, struct swm_geometry *);
311 void (*l_config)(struct workspace *, int);
313 #define SWM_L_FOCUSPREV (1<<0)
314 #define SWM_L_MAPONFOCUS (1<<1)
317 /* stack, configure */
318 { vertical_stack, vertical_config, 0, "[|]" },
319 { horizontal_stack, horizontal_config, 0, "[-]" },
321 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, "[ ]"},
322 { NULL, NULL, 0, NULL },
325 /* position of max_stack mode in the layouts array */
326 #define SWM_MAX_STACK 2
328 #define SWM_H_SLICE (32)
329 #define SWM_V_SLICE (32)
331 /* define work spaces */
333 int idx; /* workspace index */
334 struct layout *cur_layout; /* current layout handlers */
335 struct ws_win *focus; /* may be NULL */
336 struct ws_win *focus_prev; /* may be NULL */
337 struct swm_region *r; /* may be NULL */
338 struct swm_region *old_r; /* may be NULL */
339 struct ws_win_list winlist; /* list of windows in ws */
340 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
344 int horizontal_msize;
346 int horizontal_stacks;
353 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
354 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
356 /* physical screen mapping */
357 #define SWM_WS_MAX (10)
359 int idx; /* screen index */
360 struct swm_region_list rl; /* list of regions on this screen */
361 struct swm_region_list orl; /* list of old regions */
363 struct workspace ws[SWM_WS_MAX];
369 } c[SWM_S_COLOR_MAX];
371 struct swm_screen *screens;
374 /* args to functions */
377 #define SWM_ARG_ID_FOCUSNEXT (0)
378 #define SWM_ARG_ID_FOCUSPREV (1)
379 #define SWM_ARG_ID_FOCUSMAIN (2)
380 #define SWM_ARG_ID_FOCUSCUR (4)
381 #define SWM_ARG_ID_SWAPNEXT (10)
382 #define SWM_ARG_ID_SWAPPREV (11)
383 #define SWM_ARG_ID_SWAPMAIN (12)
384 #define SWM_ARG_ID_MOVELAST (13)
385 #define SWM_ARG_ID_MASTERSHRINK (20)
386 #define SWM_ARG_ID_MASTERGROW (21)
387 #define SWM_ARG_ID_MASTERADD (22)
388 #define SWM_ARG_ID_MASTERDEL (23)
389 #define SWM_ARG_ID_STACKRESET (30)
390 #define SWM_ARG_ID_STACKINIT (31)
391 #define SWM_ARG_ID_CYCLEWS_UP (40)
392 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
393 #define SWM_ARG_ID_CYCLESC_UP (42)
394 #define SWM_ARG_ID_CYCLESC_DOWN (43)
395 #define SWM_ARG_ID_STACKINC (50)
396 #define SWM_ARG_ID_STACKDEC (51)
397 #define SWM_ARG_ID_SS_ALL (60)
398 #define SWM_ARG_ID_SS_WINDOW (61)
399 #define SWM_ARG_ID_DONTCENTER (70)
400 #define SWM_ARG_ID_CENTER (71)
401 #define SWM_ARG_ID_KILLWINDOW (80)
402 #define SWM_ARG_ID_DELETEWINDOW (81)
406 void focus(struct swm_region *, union arg *);
407 void focus_magic(struct ws_win *);
414 #define SWM_Q_FLOAT (1<<0) /* float this window */
415 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
416 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
417 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
418 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
419 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
421 int quirks_size = 0, quirks_length = 0;
422 struct quirk *quirks = NULL;
425 * Supported EWMH hints should be added to
426 * both the enum and the ewmh array
428 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
429 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
430 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
431 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
432 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
433 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
434 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
435 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
436 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
437 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
443 } ewmh[SWM_EWMH_HINT_MAX] = {
444 /* must be in same order as in the enum */
445 {"_NET_ACTIVE_WINDOW", None},
446 {"_NET_MOVERESIZE_WINDOW", None},
447 {"_NET_CLOSE_WINDOW", None},
448 {"_NET_WM_WINDOW_TYPE", None},
449 {"_NET_WM_WINDOW_TYPE_DOCK", None},
450 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
451 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
452 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
453 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
454 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
455 {"_NET_WM_STATE", None},
456 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
457 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
458 {"_NET_WM_STATE_SKIP_TASKBAR", None},
459 {"_NET_WM_STATE_SKIP_PAGER", None},
460 {"_NET_WM_STATE_HIDDEN", None},
461 {"_NET_WM_STATE_ABOVE", None},
462 {"_SWM_WM_STATE_MANUAL", None},
463 {"_NET_WM_STATE_FULLSCREEN", None},
464 {"_NET_WM_ALLOWED_ACTIONS", None},
465 {"_NET_WM_ACTION_MOVE", None},
466 {"_NET_WM_ACTION_RESIZE", None},
467 {"_NET_WM_ACTION_FULLSCREEN", None},
468 {"_NET_WM_ACTION_CLOSE", None},
471 void store_float_geom(struct ws_win *win, struct swm_region *r);
472 int floating_toggle_win(struct ws_win *win);
473 void spawn_select(struct swm_region *, union arg *, char *, int *);
476 get_property(Window id, Atom atom, long count, Atom type,
477 unsigned long *n, unsigned char **data)
480 unsigned long tmp, extra;
481 unsigned long *nitems;
484 nitems = n != NULL ? n : &tmp;
485 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
486 &real, &format, nitems, &extra, data);
488 if (status != Success)
497 update_iconic(struct ws_win *win, int newv)
504 iprop = XInternAtom(display, "_SWM_ICONIC", False);
508 XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
509 PropModeReplace, (unsigned char *)&v, 1);
511 XDeleteProperty(display, win->id, iprop);
515 get_iconic(struct ws_win *win)
520 unsigned long nitems, extra;
521 unsigned char *prop = NULL;
523 iprop = XInternAtom(display, "_SWM_ICONIC", False);
526 status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
527 False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
528 if (status != Success)
530 if (rettype != XA_INTEGER || retfmt != 32)
534 v = *((int32_t *)prop);
548 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
550 for (i = 0; i < LENGTH(ewmh); i++)
551 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
553 for (i = 0; i < ScreenCount(display); i++) {
554 /* Support check window will be created by workaround(). */
556 /* Report supported atoms */
557 XDeleteProperty(display, screens[i].root, sup_list);
558 for (j = 0; j < LENGTH(ewmh); j++)
559 XChangeProperty(display, screens[i].root, sup_list, XA_ATOM, 32,
560 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
568 unsigned char *data = NULL;
570 Atom sup_check, sup_list;
573 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
574 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
576 for (i = 0; i < ScreenCount(display); i++) {
577 /* Get the support check window and destroy it */
578 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
583 XDestroyWindow(display, id);
584 XDeleteProperty(display, screens[i].root, sup_check);
585 XDeleteProperty(display, screens[i].root, sup_list);
593 ewmh_autoquirk(struct ws_win *win)
596 unsigned long *data = NULL;
600 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
601 XA_ATOM, &n, (unsigned char **)&data);
608 for (i = 0; i < n; i++) {
610 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
612 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
613 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
614 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
616 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
619 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
620 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
622 win->quirks = SWM_Q_FLOAT;
630 #define SWM_EWMH_ACTION_COUNT_MAX (6)
631 #define EWMH_F_FULLSCREEN (1<<0)
632 #define EWMH_F_ABOVE (1<<1)
633 #define EWMH_F_HIDDEN (1<<2)
634 #define EWMH_F_SKIP_PAGER (1<<3)
635 #define EWMH_F_SKIP_TASKBAR (1<<4)
636 #define SWM_F_MANUAL (1<<5)
639 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
641 struct swm_geometry rg;
649 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
655 store_float_geom(win, win->ws->r);
662 if (win->g_floatvalid) {
663 /* refloat at last floating relative position */
664 win->g.x = win->g_float.x - win->rg_float.x + rg.x;
665 win->g.y = win->g_float.y - win->rg_float.y + rg.y;
666 win->g.w = win->g_float.w;
667 win->g.h = win->g_float.h;
675 ewmh_update_actions(struct ws_win *win)
677 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
683 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
686 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
687 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
690 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
691 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
694 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
695 #define _NET_WM_STATE_ADD 1 /* add/set property */
696 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
699 ewmh_update_win_state(struct ws_win *win, long state, long action)
701 unsigned int mask = 0;
702 unsigned int changed = 0;
703 unsigned int orig_flags;
708 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
709 mask = EWMH_F_FULLSCREEN;
710 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
712 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
714 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
715 mask = EWMH_F_SKIP_PAGER;
716 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
717 mask = EWMH_F_SKIP_TASKBAR;
720 orig_flags = win->ewmh_flags;
723 case _NET_WM_STATE_REMOVE:
724 win->ewmh_flags &= ~mask;
726 case _NET_WM_STATE_ADD:
727 win->ewmh_flags |= mask;
729 case _NET_WM_STATE_TOGGLE:
730 win->ewmh_flags ^= mask;
734 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
736 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
738 if (!floating_toggle_win(win))
739 win->ewmh_flags = orig_flags; /* revert */
740 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
742 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
743 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
745 if (!ewmh_set_win_fullscreen(win, win->ewmh_flags & EWMH_F_FULLSCREEN))
746 win->ewmh_flags = orig_flags; /* revert */
748 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
750 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
751 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
752 XA_ATOM, 32, PropModeAppend,
753 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
754 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
755 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
756 XA_ATOM, 32, PropModeAppend,
757 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
758 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
759 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
760 XA_ATOM, 32, PropModeAppend,
761 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
762 if (win->ewmh_flags & EWMH_F_ABOVE)
763 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
764 XA_ATOM, 32, PropModeAppend,
765 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
766 if (win->ewmh_flags & SWM_F_MANUAL)
767 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
768 XA_ATOM, 32, PropModeAppend,
769 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
773 ewmh_get_win_state(struct ws_win *win)
784 win->ewmh_flags |= EWMH_F_ABOVE;
786 win->ewmh_flags |= SWM_F_MANUAL;
788 success = get_property(win->id, ewmh[_NET_WM_STATE].atom, (~0L), XA_ATOM,
789 &n, (unsigned char **)&states);
794 for (i = 0; i < n; i++)
795 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
815 name = "ButtonPress";
818 name = "ButtonRelease";
821 name = "MotionNotify";
824 name = "EnterNotify";
827 name = "LeaveNotify";
836 name = "KeymapNotify";
842 name = "GraphicsExpose";
847 case VisibilityNotify:
848 name = "VisibilityNotify";
851 name = "CreateNotify";
854 name = "DestroyNotify";
857 name = "UnmapNotify";
866 name = "ReparentNotify";
868 case ConfigureNotify:
869 name = "ConfigureNotify";
871 case ConfigureRequest:
872 name = "ConfigureRequest";
875 name = "GravityNotify";
878 name = "ResizeRequest";
880 case CirculateNotify:
881 name = "CirculateNotify";
883 case CirculateRequest:
884 name = "CirculateRequest";
887 name = "PropertyNotify";
890 name = "SelectionClear";
892 case SelectionRequest:
893 name = "SelectionRequest";
895 case SelectionNotify:
896 name = "SelectionNotify";
899 name = "ColormapNotify";
902 name = "ClientMessage";
905 name = "MappingNotify";
910 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
912 e->xany.window, name, e->type, QLength(display));
914 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
916 e->xany.window, e->type, QLength(display));
920 dumpwins(struct swm_region *r, union arg *args)
924 XWindowAttributes wa;
927 fprintf(stderr, "invalid workspace\n");
931 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
933 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
934 state = getstate(win->id);
935 if (!XGetWindowAttributes(display, win->id, &wa))
936 fprintf(stderr, "window: %lu failed "
937 "XGetWindowAttributes\n", win->id);
938 fprintf(stderr, "window: %lu map_state: %d state: %d "
940 win->id, wa.map_state, state, win->transient);
943 fprintf(stderr, "===== unmanaged window list =====\n");
944 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
945 state = getstate(win->id);
946 if (!XGetWindowAttributes(display, win->id, &wa))
947 fprintf(stderr, "window: %lu failed "
948 "XGetWindowAttributes\n", win->id);
949 fprintf(stderr, "window: %lu map_state: %d state: %d "
951 win->id, wa.map_state, state, win->transient);
954 fprintf(stderr, "=================================\n");
959 dumpwins(struct swm_region *r, union arg *args)
962 #endif /* SWM_DEBUG */
964 void expose(XEvent *);
965 void keypress(XEvent *);
966 void buttonpress(XEvent *);
967 void configurerequest(XEvent *);
968 void configurenotify(XEvent *);
969 void destroynotify(XEvent *);
970 void enternotify(XEvent *);
971 void focusevent(XEvent *);
972 void mapnotify(XEvent *);
973 void mappingnotify(XEvent *);
974 void maprequest(XEvent *);
975 void propertynotify(XEvent *);
976 void unmapnotify(XEvent *);
977 void visibilitynotify(XEvent *);
978 void clientmessage(XEvent *);
980 void (*handler[LASTEvent])(XEvent *) = {
982 [KeyPress] = keypress,
983 [ButtonPress] = buttonpress,
984 [ConfigureRequest] = configurerequest,
985 [ConfigureNotify] = configurenotify,
986 [DestroyNotify] = destroynotify,
987 [EnterNotify] = enternotify,
988 [FocusIn] = focusevent,
989 [FocusOut] = focusevent,
990 [MapNotify] = mapnotify,
991 [MappingNotify] = mappingnotify,
992 [MapRequest] = maprequest,
993 [PropertyNotify] = propertynotify,
994 [UnmapNotify] = unmapnotify,
995 [VisibilityNotify] = visibilitynotify,
996 [ClientMessage] = clientmessage,
1002 int saved_errno, status;
1005 saved_errno = errno;
1009 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1014 if (errno != ECHILD)
1015 warn("sighdlr: waitpid");
1016 #endif /* SWM_DEBUG */
1019 if (pid == searchpid)
1023 if (WIFEXITED(status)) {
1024 if (WEXITSTATUS(status) != 0)
1025 warnx("sighdlr: child exit status: %d",
1026 WEXITSTATUS(status));
1028 warnx("sighdlr: child is terminated "
1030 #endif /* SWM_DEBUG */
1044 errno = saved_errno;
1048 name_to_color(char *colorname)
1052 XColor screen_def, exact_def;
1053 unsigned long result = 0;
1054 char cname[32] = "#";
1056 cmap = DefaultColormap(display, screens[0].idx);
1057 status = XAllocNamedColor(display, cmap, colorname,
1058 &screen_def, &exact_def);
1060 strlcat(cname, colorname + 2, sizeof cname - 1);
1061 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1065 result = screen_def.pixel;
1067 fprintf(stderr, "color '%s' not found.\n", colorname);
1073 setscreencolor(char *val, int i, int c)
1075 if (i > 0 && i <= ScreenCount(display)) {
1076 screens[i - 1].c[c].color = name_to_color(val);
1077 free(screens[i - 1].c[c].name);
1078 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1080 } else if (i == -1) {
1081 for (i = 0; i < ScreenCount(display); i++) {
1082 screens[i].c[c].color = name_to_color(val);
1083 free(screens[i].c[c].name);
1084 if ((screens[i].c[c].name = strdup(val)) == NULL)
1088 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1089 i, ScreenCount(display));
1093 custom_region(char *val)
1095 unsigned int sidx, x, y, w, h;
1097 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1098 errx(1, "invalid custom region, "
1099 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
1100 if (sidx < 1 || sidx > ScreenCount(display))
1101 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1102 sidx, ScreenCount(display));
1106 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
1108 if (x < 0 || x > DisplayWidth(display, sidx) ||
1109 y < 0 || y > DisplayHeight(display, sidx) ||
1110 w + x > DisplayWidth(display, sidx) ||
1111 h + y > DisplayHeight(display, sidx)) {
1112 fprintf(stderr, "ignoring region %ux%u+%u+%u - not within screen boundaries "
1113 "(%ux%u)\n", w, h, x, y,
1114 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1118 new_region(&screens[sidx], x, y, w, h);
1122 socket_setnonblock(int fd)
1126 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1127 err(1, "fcntl F_GETFL");
1128 flags |= O_NONBLOCK;
1129 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1130 err(1, "fcntl F_SETFL");
1134 bar_print(struct swm_region *r, char *s)
1136 XClearWindow(display, r->bar_window);
1137 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
1138 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
1143 bar_extra_stop(void)
1147 bzero(bar_pipe, sizeof bar_pipe);
1150 kill(bar_pid, SIGTERM);
1153 strlcpy(bar_ext, "", sizeof bar_ext);
1158 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1160 int do_class, do_name;
1162 XClassHint *xch = NULL;
1164 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
1165 cur_focus != NULL) {
1166 if ((xch = XAllocClassHint()) == NULL)
1168 status = XGetClassHint(display, cur_focus->id, xch);
1169 if (status == BadWindow || status == BadAlloc)
1171 do_class = (title_class_enabled && xch->res_class != NULL);
1172 do_name = (title_name_enabled && xch->res_name != NULL);
1174 strlcat(s, xch->res_class, sz);
1175 if (do_class && do_name)
1176 strlcat(s, ":", sz);
1178 strlcat(s, xch->res_name, sz);
1179 strlcat(s, " ", sz);
1187 bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1191 if (window_name_enabled && cur_focus != NULL) {
1192 XFetchName(display, cur_focus->id, &title);
1194 if (cur_focus->floating)
1195 strlcat(s, "(f) ", sz);
1196 strlcat(s, title, sz);
1197 strlcat(s, " ", sz);
1208 struct swm_region *r;
1211 char s[SWM_BAR_MAX];
1212 char cn[SWM_BAR_MAX];
1213 char loc[SWM_BAR_MAX];
1217 if (bar_enabled == 0)
1219 if (bar_extra && bar_extra_running) {
1220 /* ignore short reads; it'll correct itself */
1221 while ((b = fgetln(stdin, &len)) != NULL)
1222 if (b && b[len - 1] == '\n') {
1224 strlcpy(bar_ext, b, sizeof bar_ext);
1226 if (b == NULL && errno != EAGAIN) {
1227 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
1228 errno, strerror(errno));
1232 strlcpy(bar_ext, "", sizeof bar_ext);
1234 if (clock_enabled == 0)
1235 strlcpy(s, "", sizeof s);
1238 localtime_r(&tmt, &tm);
1239 strftime(s, sizeof s, clock_format, &tm);
1240 strlcat(s, " ", sizeof s);
1243 for (i = 0; i < ScreenCount(display); i++) {
1245 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1246 strlcpy(cn, "", sizeof cn);
1248 bar_class_name(cn, sizeof cn, r->ws->focus);
1249 bar_window_name(cn, sizeof cn, r->ws->focus);
1253 stack = r->ws->cur_layout->name;
1255 snprintf(loc, sizeof loc, "%d:%d %s %s%s %s %s",
1256 x++, r->ws->idx + 1, stack, s, cn, bar_ext,
1271 bar_toggle(struct swm_region *r, union arg *args)
1273 struct swm_region *tmpr;
1274 int i, sc = ScreenCount(display);
1276 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
1279 for (i = 0; i < sc; i++)
1280 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1281 XUnmapWindow(display, tmpr->bar_window);
1283 for (i = 0; i < sc; i++)
1284 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1285 XMapRaised(display, tmpr->bar_window);
1287 bar_enabled = !bar_enabled;
1290 /* must be after stack */
1297 XSetWindowAttributes wa;
1298 struct swm_region *r;
1301 /* do this here because the conf file is in memory */
1302 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1303 /* launch external status app */
1304 bar_extra_running = 1;
1305 if (pipe(bar_pipe) == -1)
1306 err(1, "pipe error");
1307 socket_setnonblock(bar_pipe[0]);
1308 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1309 if (dup2(bar_pipe[0], 0) == -1)
1311 if (dup2(bar_pipe[1], 1) == -1)
1313 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1314 err(1, "could not disable SIGPIPE");
1315 switch (bar_pid = fork()) {
1317 err(1, "cannot fork");
1321 execvp(bar_argv[0], bar_argv);
1322 err(1, "%s external app failed", bar_argv[0]);
1324 default: /* parent */
1330 bzero(&wa, sizeof wa);
1331 for (i = 0; i < ScreenCount(display); i++)
1332 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1334 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1335 wa.background_pixel =
1336 screens[i].c[SWM_S_COLOR_BAR].color;
1337 XChangeWindowAttributes(display, r->bar_window,
1338 CWBackPixel | CWBorderPixel, &wa);
1344 bar_setup(struct swm_region *r)
1349 XFreeFont(display, bar_fs);
1353 for (i = 0; bar_fonts[i] != NULL; i++) {
1354 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
1360 if (bar_fonts[i] == NULL)
1361 errx(1, "couldn't load font");
1363 errx(1, "couldn't create font structure");
1365 bar_height = bar_fs->ascent + bar_fs->descent + 1 + 2 * bar_border_width;
1367 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1369 r->bar_window = XCreateSimpleWindow(display,
1370 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width, bar_height - 2 * bar_border_width,
1371 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1372 r->s->c[SWM_S_COLOR_BAR].color);
1373 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
1374 XSetFont(display, bar_gc, bar_fs->fid);
1375 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1377 XMapRaised(display, r->bar_window);
1378 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
1380 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1381 err(1, "could not install bar_signal");
1386 drain_enter_notify(void)
1391 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1394 DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained %d\n", i);
1398 set_win_state(struct ws_win *win, long state)
1400 long data[] = {state, None};
1402 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1407 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1408 (unsigned char *)data, 2);
1415 unsigned char *p = NULL;
1418 if (!get_property(w, astate, 2L, astate, &n, &p))
1421 result = *((long *)p);
1427 version(struct swm_region *r, union arg *args)
1429 bar_version = !bar_version;
1431 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
1432 SWM_VERSION, cvstag);
1434 strlcpy(bar_vertext, "", sizeof bar_vertext);
1439 client_msg(struct ws_win *win, Atom a)
1441 XClientMessageEvent cm;
1446 bzero(&cm, sizeof cm);
1447 cm.type = ClientMessage;
1448 cm.window = win->id;
1449 cm.message_type = aprot;
1452 cm.data.l[1] = CurrentTime;
1453 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1457 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1465 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
1466 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1468 ce.type = ConfigureNotify;
1469 ce.display = display;
1471 ce.window = win->id;
1474 ce.width = win->g.w;
1475 ce.height = win->g.h;
1476 ce.border_width = border_width;
1478 ce.override_redirect = False;
1480 DNPRINTF(SWM_D_MISC, "config_win: ev win %lu x %d y %d w %d h %d\n",
1481 ev->window, ev->x, ev->y, ev->width, ev->height);
1482 ce.type = ConfigureNotify;
1483 ce.display = ev->display;
1484 ce.event = ev->window;
1485 ce.window = ev->window;
1488 ce.width = ev->width;
1489 ce.height = ev->height;
1490 ce.border_width = ev->border_width;
1491 ce.above = ev->above;
1492 ce.override_redirect = False;
1495 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1499 count_win(struct workspace *ws, int count_transient)
1504 TAILQ_FOREACH(win, &ws->winlist, entry) {
1505 if (count_transient == 0 && win->floating)
1507 if (count_transient == 0 && win->transient)
1513 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1519 quit(struct swm_region *r, union arg *args)
1521 DNPRINTF(SWM_D_MISC, "quit\n");
1526 unmap_window(struct ws_win *win)
1531 /* don't unmap again */
1532 if (getstate(win->id) == IconicState)
1535 set_win_state(win, IconicState);
1537 XUnmapWindow(display, win->id);
1538 XSetWindowBorder(display, win->id,
1539 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1548 for (i = 0; i < ScreenCount(display); i++)
1549 for (j = 0; j < SWM_WS_MAX; j++)
1550 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1555 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1562 event.display = display; /* Ignored, but what the hell */
1563 event.window = win->id;
1564 event.root = win->s->root;
1565 event.subwindow = None;
1566 event.time = CurrentTime;
1571 event.same_screen = True;
1572 event.keycode = XKeysymToKeycode(display, keysym);
1573 event.state = modifiers;
1575 event.type = KeyPress;
1576 XSendEvent(event.display, event.window, True,
1577 KeyPressMask, (XEvent *)&event);
1579 event.type = KeyRelease;
1580 XSendEvent(event.display, event.window, True,
1581 KeyPressMask, (XEvent *)&event);
1586 restart(struct swm_region *r, union arg *args)
1588 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1590 /* disable alarm because the following code may not be interrupted */
1592 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1593 errx(1, "can't disable alarm");
1598 XCloseDisplay(display);
1599 execvp(start_argv[0], start_argv);
1600 fprintf(stderr, "execvp failed\n");
1606 root_to_region(Window root)
1608 struct swm_region *r = NULL;
1610 int i, x, y, wx, wy;
1613 for (i = 0; i < ScreenCount(display); i++)
1614 if (screens[i].root == root)
1617 if (XQueryPointer(display, screens[i].root,
1618 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1619 /* choose a region based on pointer location */
1620 TAILQ_FOREACH(r, &screens[i].rl, entry)
1621 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1622 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1627 r = TAILQ_FIRST(&screens[i].rl);
1633 find_unmanaged_window(Window id)
1638 for (i = 0; i < ScreenCount(display); i++)
1639 for (j = 0; j < SWM_WS_MAX; j++)
1640 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1648 find_window(Window id)
1651 Window wrr, wpr, *wcr = NULL;
1655 for (i = 0; i < ScreenCount(display); i++)
1656 for (j = 0; j < SWM_WS_MAX; j++)
1657 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1661 /* if we were looking for the parent return that window instead */
1662 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1667 /* ignore not found and root */
1668 if (wpr == 0 || wrr == wpr)
1671 /* look for parent */
1672 for (i = 0; i < ScreenCount(display); i++)
1673 for (j = 0; j < SWM_WS_MAX; j++)
1674 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1682 spawn(struct swm_region *r, union arg *args, int close_fd)
1687 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1690 close(ConnectionNumber(display));
1692 setenv("LD_PRELOAD", SWM_LIB, 1);
1694 if (asprintf(&ret, "%d", r->ws->idx) == -1) {
1698 setenv("_SWM_WS", ret, 1);
1702 if (asprintf(&ret, "%d", getpid()) == -1) {
1706 setenv("_SWM_PID", ret, 1);
1710 if (setsid() == -1) {
1717 * close stdin and stdout to prevent interaction between apps
1718 * and the baraction script
1719 * leave stderr open to record errors
1721 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
1725 dup2(fd, STDIN_FILENO);
1726 dup2(fd, STDOUT_FILENO);
1731 execvp(args->argv[0], args->argv);
1738 spawnterm(struct swm_region *r, union arg *args)
1740 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1743 setenv("_SWM_XTERM_FONTADJ", "", 1);
1749 kill_refs(struct ws_win *win)
1752 struct swm_region *r;
1753 struct workspace *ws;
1758 for (i = 0; i < ScreenCount(display); i++)
1759 TAILQ_FOREACH(r, &screens[i].rl, entry)
1760 for (x = 0; x < SWM_WS_MAX; x++) {
1762 if (win == ws->focus)
1764 if (win == ws->focus_prev)
1765 ws->focus_prev = NULL;
1770 validate_win(struct ws_win *testwin)
1773 struct workspace *ws;
1774 struct swm_region *r;
1775 int i, x, foundit = 0;
1777 if (testwin == NULL)
1780 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1781 TAILQ_FOREACH(r, &screens[i].rl, entry)
1782 for (x = 0; x < SWM_WS_MAX; x++) {
1784 TAILQ_FOREACH(win, &ws->winlist, entry)
1792 validate_ws(struct workspace *testws)
1794 struct swm_region *r;
1795 struct workspace *ws;
1798 /* validate all ws */
1799 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1800 TAILQ_FOREACH(r, &screens[i].rl, entry)
1801 for (x = 0; x < SWM_WS_MAX; x++) {
1810 unfocus_win(struct ws_win *win)
1815 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1819 if (win->ws == NULL)
1822 if (validate_ws(win->ws))
1823 return; /* XXX this gets hit with thunderbird, needs fixing */
1825 if (win->ws->r == NULL)
1828 if (validate_win(win)) {
1833 if (win->ws->focus == win) {
1834 win->ws->focus = NULL;
1835 win->ws->focus_prev = win;
1838 if (validate_win(win->ws->focus)) {
1839 kill_refs(win->ws->focus);
1840 win->ws->focus = NULL;
1842 if (validate_win(win->ws->focus_prev)) {
1843 kill_refs(win->ws->focus_prev);
1844 win->ws->focus_prev = NULL;
1847 /* drain all previous unfocus events */
1848 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
1851 grabbuttons(win, 0);
1852 XSetWindowBorder(display, win->id,
1853 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1855 XChangeProperty(display, win->s->root,
1856 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1857 PropModeReplace, (unsigned char *)&none,1);
1866 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
1868 for (i = 0; i < ScreenCount(display); i++)
1869 for (j = 0; j < SWM_WS_MAX; j++)
1870 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1875 focus_win(struct ws_win *win)
1880 struct ws_win *cfw = NULL;
1883 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1887 if (win->ws == NULL)
1890 if (validate_ws(win->ws))
1891 return; /* XXX this gets hit with thunderbird, needs fixing */
1893 if (validate_win(win)) {
1898 if (validate_win(win)) {
1903 XGetInputFocus(display, &cur_focus, &rr);
1904 if ((cfw = find_window(cur_focus)) != NULL)
1907 /* use larger hammer since the window was killed somehow */
1908 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
1909 XSetWindowBorder(display, cfw->id,
1910 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1913 win->ws->focus = win;
1915 if (win->ws->r != NULL) {
1916 /* drain all previous focus events */
1917 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
1921 XSetInputFocus(display, win->id,
1922 RevertToParent, CurrentTime);
1923 XMapRaised(display, win->id);
1924 grabbuttons(win, 1);
1925 XSetWindowBorder(display, win->id,
1926 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1927 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS)
1928 XMapRaised(display, win->id);
1930 XChangeProperty(display, win->s->root,
1931 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1932 PropModeReplace, (unsigned char *)&win->id,1);
1935 if (window_name_enabled)
1940 switchws(struct swm_region *r, union arg *args)
1942 int wsid = args->id, unmap_old = 0;
1943 struct swm_region *this_r, *other_r;
1945 struct workspace *new_ws, *old_ws;
1952 old_ws = this_r->ws;
1953 new_ws = &this_r->s->ws[wsid];
1955 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1956 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1959 if (new_ws == NULL || old_ws == NULL)
1961 if (new_ws == old_ws)
1964 other_r = new_ws->r;
1965 if (other_r == NULL) {
1966 /* the other workspace is hidden, hide this one */
1970 /* the other ws is visible in another region, exchange them */
1971 other_r->ws_prior = new_ws;
1972 other_r->ws = old_ws;
1973 old_ws->r = other_r;
1975 this_r->ws_prior = old_ws;
1976 this_r->ws = new_ws;
1979 /* this is needed so that we can click on a window after a restart */
1983 a.id = SWM_ARG_ID_FOCUSCUR;
1984 focus(new_ws->r, &a);
1988 /* unmap old windows */
1990 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1993 if (focus_mode == SWM_FOCUS_DEFAULT)
1994 drain_enter_notify();
1998 cyclews(struct swm_region *r, union arg *args)
2001 struct swm_screen *s = r->s;
2003 DNPRINTF(SWM_D_WS, "cyclews id %d "
2004 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2005 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2010 case SWM_ARG_ID_CYCLEWS_UP:
2011 if (a.id < SWM_WS_MAX - 1)
2016 case SWM_ARG_ID_CYCLEWS_DOWN:
2020 a.id = SWM_WS_MAX - 1;
2026 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
2028 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2032 } while (a.id != r->ws->idx);
2036 priorws(struct swm_region *r, union arg *args)
2040 DNPRINTF(SWM_D_WS, "priorws id %d "
2041 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2042 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2044 if (r->ws_prior == NULL)
2047 a.id = r->ws_prior->idx;
2052 cyclescr(struct swm_region *r, union arg *args)
2054 struct swm_region *rr = NULL;
2058 /* do nothing if we don't have more than one screen */
2059 if (!(ScreenCount(display) > 1 || outputs > 1))
2064 case SWM_ARG_ID_CYCLESC_UP:
2065 rr = TAILQ_NEXT(r, entry);
2067 rr = TAILQ_FIRST(&screens[i].rl);
2069 case SWM_ARG_ID_CYCLESC_DOWN:
2070 rr = TAILQ_PREV(r, swm_region_list, entry);
2072 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2080 /* move mouse to region */
2082 y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
2083 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2085 a.id = SWM_ARG_ID_FOCUSCUR;
2088 if (rr->ws->focus) {
2089 /* move to focus window */
2090 x = rr->ws->focus->g.x + 1;
2091 y = rr->ws->focus->g.y + 1;
2092 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2097 sort_windows(struct ws_win_list *wl)
2099 struct ws_win *win, *parent, *nxt;
2104 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2105 nxt = TAILQ_NEXT(win, entry);
2106 if (win->transient) {
2107 parent = find_window(win->transient);
2108 if (parent == NULL) {
2109 fprintf(stderr, "not possible bug\n");
2112 TAILQ_REMOVE(wl, win, entry);
2113 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2120 swapwin(struct swm_region *r, union arg *args)
2122 struct ws_win *target, *source;
2123 struct ws_win *cur_focus;
2124 struct ws_win_list *wl;
2127 DNPRINTF(SWM_D_WS, "swapwin id %d "
2128 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
2129 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2131 cur_focus = r->ws->focus;
2132 if (cur_focus == NULL)
2136 wl = &source->ws->winlist;
2139 case SWM_ARG_ID_SWAPPREV:
2140 if (source->transient)
2141 source = find_window(source->transient);
2142 target = TAILQ_PREV(source, ws_win_list, entry);
2143 if (target && target->transient)
2144 target = find_window(target->transient);
2145 TAILQ_REMOVE(wl, source, entry);
2147 TAILQ_INSERT_TAIL(wl, source, entry);
2149 TAILQ_INSERT_BEFORE(target, source, entry);
2151 case SWM_ARG_ID_SWAPNEXT:
2152 target = TAILQ_NEXT(source, entry);
2153 /* move the parent and let the sort handle the move */
2154 if (source->transient)
2155 source = find_window(source->transient);
2156 TAILQ_REMOVE(wl, source, entry);
2158 TAILQ_INSERT_HEAD(wl, source, entry);
2160 TAILQ_INSERT_AFTER(wl, target, source, entry);
2162 case SWM_ARG_ID_SWAPMAIN:
2163 target = TAILQ_FIRST(wl);
2164 if (target == source) {
2165 if (source->ws->focus_prev != NULL &&
2166 source->ws->focus_prev != target)
2168 source = source->ws->focus_prev;
2172 if (target == NULL || source == NULL)
2174 source->ws->focus_prev = target;
2175 TAILQ_REMOVE(wl, target, entry);
2176 TAILQ_INSERT_BEFORE(source, target, entry);
2177 TAILQ_REMOVE(wl, source, entry);
2178 TAILQ_INSERT_HEAD(wl, source, entry);
2180 case SWM_ARG_ID_MOVELAST:
2181 TAILQ_REMOVE(wl, source, entry);
2182 TAILQ_INSERT_TAIL(wl, source, entry);
2185 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
2195 focus_prev(struct ws_win *win)
2197 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2198 struct ws_win *cur_focus = NULL;
2199 struct ws_win_list *wl = NULL;
2200 struct workspace *ws = NULL;
2202 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
2204 if (!(win && win->ws))
2209 cur_focus = ws->focus;
2210 winlostfocus = cur_focus;
2212 /* pickle, just focus on whatever */
2213 if (cur_focus == NULL) {
2214 /* use prev_focus if valid */
2215 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2216 find_window(WINID(ws->focus_prev)))
2217 winfocus = ws->focus_prev;
2218 if (winfocus == NULL)
2219 winfocus = TAILQ_FIRST(wl);
2223 /* if transient focus on parent */
2224 if (cur_focus->transient) {
2225 winfocus = find_window(cur_focus->transient);
2229 /* if in max_stack try harder */
2230 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2231 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2232 if (cur_focus != ws->focus_prev)
2233 winfocus = ws->focus_prev;
2234 else if (cur_focus != ws->focus)
2235 winfocus = ws->focus;
2237 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2242 if (cur_focus == win)
2243 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2244 if (winfocus == NULL)
2245 winfocus = TAILQ_LAST(wl, ws_win_list);
2246 if (winfocus == NULL || winfocus == win)
2247 winfocus = TAILQ_NEXT(cur_focus, entry);
2249 if (winfocus == winlostfocus || winfocus == NULL)
2252 focus_magic(winfocus);
2256 focus(struct swm_region *r, union arg *args)
2258 struct ws_win *winfocus = NULL, *winlostfocus = NULL, *head;
2259 struct ws_win *cur_focus = NULL;
2260 struct ws_win_list *wl = NULL;
2261 struct workspace *ws = NULL;
2266 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
2268 /* treat FOCUS_CUR special */
2269 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2270 if (r->ws->focus && r->ws->focus->iconic == 0)
2271 winfocus = r->ws->focus;
2272 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2273 winfocus = r->ws->focus_prev;
2275 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2276 if (winfocus->iconic == 0)
2279 focus_magic(winfocus);
2283 if ((cur_focus = r->ws->focus) == NULL)
2288 winlostfocus = cur_focus;
2291 case SWM_ARG_ID_FOCUSPREV:
2292 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2294 head = TAILQ_LAST(wl, ws_win_list);
2296 if (WINID(winfocus) == cur_focus->transient) {
2297 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2299 head = TAILQ_LAST(wl, ws_win_list);
2304 if (winfocus && winfocus->iconic) {
2305 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
2306 if (winfocus->iconic == 0)
2311 case SWM_ARG_ID_FOCUSNEXT:
2312 head = TAILQ_NEXT(cur_focus, entry);
2314 head = TAILQ_FIRST(wl);
2318 if (winfocus && winfocus->iconic) {
2319 TAILQ_FOREACH(winfocus, wl, entry)
2320 if (winfocus->iconic == 0)
2325 case SWM_ARG_ID_FOCUSMAIN:
2326 winfocus = TAILQ_FIRST(wl);
2327 if (winfocus == cur_focus)
2328 winfocus = cur_focus->ws->focus_prev;
2334 if (winfocus == winlostfocus || winfocus == NULL)
2337 focus_magic(winfocus);
2341 cycle_layout(struct swm_region *r, union arg *args)
2343 struct workspace *ws = r->ws;
2344 struct ws_win *winfocus;
2347 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2349 winfocus = ws->focus;
2352 if (ws->cur_layout->l_stack == NULL)
2353 ws->cur_layout = &layouts[0];
2356 if (focus_mode == SWM_FOCUS_DEFAULT)
2357 drain_enter_notify();
2358 a.id = SWM_ARG_ID_FOCUSCUR;
2364 stack_config(struct swm_region *r, union arg *args)
2366 struct workspace *ws = r->ws;
2368 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
2371 if (ws->cur_layout->l_config != NULL)
2372 ws->cur_layout->l_config(ws, args->id);
2374 if (args->id != SWM_ARG_ID_STACKINIT);
2380 struct swm_geometry g;
2381 struct swm_region *r;
2384 DNPRINTF(SWM_D_STACK, "stack\n");
2386 for (i = 0; i < ScreenCount(display); i++) {
2388 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2389 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
2390 "(screen %d, region %d)\n", r->ws->idx, i, j++);
2392 /* start with screen geometry, adjust for bar */
2394 g.w -= 2 * border_width;
2395 g.h -= 2 * border_width;
2401 r->ws->cur_layout->l_stack(r->ws, &g);
2402 /* save r so we can track region changes */
2409 if (focus_mode == SWM_FOCUS_DEFAULT)
2410 drain_enter_notify();
2414 store_float_geom(struct ws_win *win, struct swm_region *r)
2416 /* retain window geom and region geom */
2417 win->g_float.x = win->g.x;
2418 win->g_float.y = win->g.y;
2419 win->g_float.w = win->g.w;
2420 win->g_float.h = win->g.h;
2421 win->rg_float.x = r->g.x;
2422 win->rg_float.y = r->g.y;
2423 win->rg_float.w = r->g.w;
2424 win->rg_float.h = r->g.h;
2425 win->g_floatvalid = 1;
2429 stack_floater(struct ws_win *win, struct swm_region *r)
2437 bzero(&wc, sizeof wc);
2438 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2441 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2442 * geom on ws switches or return from max mode
2445 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2446 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2448 * use stored g and rg to set relative position and size
2449 * as in old region or before max stack mode
2451 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2452 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2453 win->g.w = win->g_float.w;
2454 win->g.h = win->g_float.h;
2455 win->g_floatvalid = 0;
2458 win->floatmaxed = 0;
2460 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
2461 (win->g.h >= HEIGHT(r)))
2462 wc.border_width = 0;
2464 wc.border_width = border_width;
2465 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2466 win->g.w = (double)WIDTH(r) * dialog_ratio;
2467 win->g.h = (double)HEIGHT(r) * dialog_ratio;
2472 * floaters and transients are auto-centred unless moved
2475 win->g.x = r->g.x + (WIDTH(r) - win->g.w) / 2 - wc.border_width;
2476 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) / 2 - wc.border_width;
2479 /* win can be outside r if new r smaller than old r */
2480 /* Ensure top left corner inside r (move probs otherwise) */
2481 if (win->g.x < r->g.x - wc.border_width)
2482 win->g.x = r->g.x - wc.border_width;
2483 if (win->g.x > r->g.x + r->g.w - 1)
2484 win->g.x = (win->g.w > r->g.w) ? r->g.x :
2485 (r->g.x + r->g.w - win->g.w - 2 * wc.border_width);
2486 if (win->g.y < r->g.y - wc.border_width)
2487 win->g.y = r->g.y - wc.border_width;
2488 if (win->g.y > r->g.y + r->g.h - 1)
2489 win->g.y = (win->g.h > r->g.h) ? r->g.y :
2490 (r->g.y + r->g.h - win->g.h - 2 * wc.border_width);
2494 wc.width = win->g.w;
2495 wc.height = win->g.h;
2498 * Retain floater and transient geometry for correct positioning
2499 * when ws changes region
2501 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2502 store_float_geom(win, r);
2504 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
2505 win->id, wc.x, wc.y, wc.width, wc.height);
2507 XConfigureWindow(display, win->id, mask, &wc);
2511 * Send keystrokes to terminal to decrease/increase the font size as the
2512 * window size changes.
2515 adjust_font(struct ws_win *win)
2517 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2518 win->floating || win->transient)
2521 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2522 win->g.w / win->sh.width_inc < term_width &&
2523 win->font_steps < SWM_MAX_FONT_STEPS) {
2524 win->font_size_boundary[win->font_steps] =
2525 (win->sh.width_inc * term_width) + win->sh.base_width;
2528 win->last_inc = win->sh.width_inc;
2529 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2530 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2531 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
2534 win->last_inc = win->sh.width_inc;
2535 fake_keypress(win, XK_KP_Add, ShiftMask);
2539 #define SWAPXY(g) do { \
2541 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2542 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2545 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2548 XWindowAttributes wa;
2549 struct swm_geometry win_g, r_g = *g;
2550 struct ws_win *win, *fs_win = 0;
2551 int i, j, s, stacks;
2552 int w_inc = 1, h_inc, w_base = 1, h_base;
2553 int hrh, extra = 0, h_slice, last_h = 0;
2554 int split, colno, winno, mwin, msize, mscale;
2555 int remain, missing, v_slice, reconfigure;
2558 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2559 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2561 winno = count_win(ws, 0);
2562 if (winno == 0 && count_win(ws, 1) == 0)
2565 TAILQ_FOREACH(win, &ws->winlist, entry)
2566 if (win->transient == 0 && win->floating == 0 && win->iconic == 0)
2573 w_inc = win->sh.width_inc;
2574 w_base = win->sh.base_width;
2575 mwin = ws->l_state.horizontal_mwin;
2576 mscale = ws->l_state.horizontal_msize;
2577 stacks = ws->l_state.horizontal_stacks;
2580 w_inc = win->sh.height_inc;
2581 w_base = win->sh.base_height;
2582 mwin = ws->l_state.vertical_mwin;
2583 mscale = ws->l_state.vertical_msize;
2584 stacks = ws->l_state.vertical_stacks;
2588 if (stacks > winno - mwin)
2589 stacks = winno - mwin;
2593 h_slice = r_g.h / SWM_H_SLICE;
2594 if (mwin && winno > mwin) {
2595 v_slice = r_g.w / SWM_V_SLICE;
2599 win_g.w = v_slice * mscale;
2601 if (w_inc > 1 && w_inc < v_slice) {
2602 /* adjust for window's requested size increment */
2603 remain = (win_g.w - w_base) % w_inc;
2604 missing = w_inc - remain;
2611 win_g.x += r_g.w - msize;
2614 colno = split = winno / stacks;
2615 win_g.w = ((r_g.w - (stacks * 2 * border_width) + 2 * border_width) / stacks);
2617 hrh = r_g.h / colno;
2618 extra = r_g.h - (colno * hrh);
2619 win_g.h = hrh - 2 * border_width;
2621 /* stack all the tiled windows */
2622 i = j = 0, s = stacks;
2623 TAILQ_FOREACH(win, &ws->winlist, entry) {
2624 if (win->transient != 0 || win->floating != 0)
2626 if (win->iconic != 0)
2629 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2634 if (split && i == split) {
2635 colno = (winno - mwin) / stacks;
2636 if (s <= (winno - mwin) % stacks)
2638 split = split + colno;
2639 hrh = (r_g.h / colno);
2640 extra = r_g.h - (colno * hrh);
2644 win_g.x += win_g.w + 2 * border_width;
2645 win_g.w = (r_g.w - msize - (stacks * 2 * border_width)) / stacks;
2647 win_g.w += (r_g.w - msize - (stacks * 2 * border_width)) %
2652 win_g.h = hrh - 2 * border_width;
2654 h_inc = win->sh.width_inc;
2655 h_base = win->sh.base_width;
2657 h_inc = win->sh.height_inc;
2658 h_base = win->sh.base_height;
2660 if (j == colno - 1) {
2661 win_g.h = hrh + extra;
2662 } else if (h_inc > 1 && h_inc < h_slice) {
2663 /* adjust for window's requested size increment */
2664 remain = (win_g.h - h_base) % h_inc;
2665 missing = h_inc - remain;
2667 if (missing <= extra || j == 0) {
2679 win_g.y += last_h + 2 * border_width;
2681 bzero(&wc, sizeof wc);
2682 if (disable_border && bar_enabled == 0 && winno == 1){
2683 wc.border_width = 0;
2684 win_g.w += 2 * border_width;
2685 win_g.h += 2 * border_width;
2687 wc.border_width = border_width;
2690 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2691 win->g.w != win_g.h || win->g.h != win_g.w) {
2693 win->g.x = wc.x = win_g.y;
2694 win->g.y = wc.y = win_g.x;
2695 win->g.w = wc.width = win_g.h;
2696 win->g.h = wc.height = win_g.w;
2699 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2700 win->g.w != win_g.w || win->g.h != win_g.h) {
2702 win->g.x = wc.x = win_g.x;
2703 win->g.y = wc.y = win_g.y;
2704 win->g.w = wc.width = win_g.w;
2705 win->g.h = wc.height = win_g.h;
2710 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2711 XConfigureWindow(display, win->id, mask, &wc);
2714 if (XGetWindowAttributes(display, win->id, &wa))
2715 if (wa.map_state == IsUnmapped)
2716 XMapRaised(display, win->id);
2724 /* now, stack all the floaters and transients */
2725 TAILQ_FOREACH(win, &ws->winlist, entry) {
2726 if (win->transient == 0 && win->floating == 0)
2728 if (win->iconic == 1)
2730 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2735 stack_floater(win, ws->r);
2736 XMapRaised(display, win->id);
2740 stack_floater(fs_win, ws->r);
2741 XMapRaised(display, fs_win->id);
2746 vertical_config(struct workspace *ws, int id)
2748 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2751 case SWM_ARG_ID_STACKRESET:
2752 case SWM_ARG_ID_STACKINIT:
2753 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2754 ws->l_state.vertical_mwin = 1;
2755 ws->l_state.vertical_stacks = 1;
2757 case SWM_ARG_ID_MASTERSHRINK:
2758 if (ws->l_state.vertical_msize > 1)
2759 ws->l_state.vertical_msize--;
2761 case SWM_ARG_ID_MASTERGROW:
2762 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2763 ws->l_state.vertical_msize++;
2765 case SWM_ARG_ID_MASTERADD:
2766 ws->l_state.vertical_mwin++;
2768 case SWM_ARG_ID_MASTERDEL:
2769 if (ws->l_state.vertical_mwin > 0)
2770 ws->l_state.vertical_mwin--;
2772 case SWM_ARG_ID_STACKINC:
2773 ws->l_state.vertical_stacks++;
2775 case SWM_ARG_ID_STACKDEC:
2776 if (ws->l_state.vertical_stacks > 1)
2777 ws->l_state.vertical_stacks--;
2785 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2787 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2789 stack_master(ws, g, 0, 0);
2793 horizontal_config(struct workspace *ws, int id)
2795 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2798 case SWM_ARG_ID_STACKRESET:
2799 case SWM_ARG_ID_STACKINIT:
2800 ws->l_state.horizontal_mwin = 1;
2801 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2802 ws->l_state.horizontal_stacks = 1;
2804 case SWM_ARG_ID_MASTERSHRINK:
2805 if (ws->l_state.horizontal_msize > 1)
2806 ws->l_state.horizontal_msize--;
2808 case SWM_ARG_ID_MASTERGROW:
2809 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2810 ws->l_state.horizontal_msize++;
2812 case SWM_ARG_ID_MASTERADD:
2813 ws->l_state.horizontal_mwin++;
2815 case SWM_ARG_ID_MASTERDEL:
2816 if (ws->l_state.horizontal_mwin > 0)
2817 ws->l_state.horizontal_mwin--;
2819 case SWM_ARG_ID_STACKINC:
2820 ws->l_state.horizontal_stacks++;
2822 case SWM_ARG_ID_STACKDEC:
2823 if (ws->l_state.horizontal_stacks > 1)
2824 ws->l_state.horizontal_stacks--;
2832 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2834 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
2836 stack_master(ws, g, 1, 0);
2839 /* fullscreen view */
2841 max_stack(struct workspace *ws, struct swm_geometry *g)
2844 struct swm_geometry gg = *g;
2845 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2849 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2854 winno = count_win(ws, 0);
2855 if (winno == 0 && count_win(ws, 1) == 0)
2858 TAILQ_FOREACH(win, &ws->winlist, entry) {
2859 if (win->transient) {
2861 parent = find_window(win->transient);
2865 if (win->floating && win->floatmaxed == 0 ) {
2867 * retain geometry for retrieval on exit from
2870 store_float_geom(win, ws->r);
2871 win->floatmaxed = 1;
2874 /* only reconfigure if necessary */
2875 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2877 bzero(&wc, sizeof wc);
2878 win->g.x = wc.x = gg.x;
2879 win->g.y = wc.y = gg.y;
2881 wc.border_width = border_width;
2882 win->g.w = wc.width = gg.w;
2883 win->g.h = wc.height = gg.h;
2885 wc.border_width = 0;
2886 win->g.w = wc.width = gg.w + 2 * border_width;
2887 win->g.h = wc.height = gg.h + 2 * border_width;
2889 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2890 XConfigureWindow(display, win->id, mask, &wc);
2892 /* unmap only if we don't have multi screen */
2893 if (win != ws->focus)
2894 if (!(ScreenCount(display) > 1 || outputs > 1))
2898 /* put the last transient on top */
2901 XMapRaised(display, parent->id);
2902 stack_floater(wintrans, ws->r);
2903 focus_magic(wintrans);
2908 send_to_ws(struct swm_region *r, union arg *args)
2910 int wsid = args->id;
2911 struct ws_win *win = NULL, *parent;
2912 struct workspace *ws, *nws;
2913 Atom ws_idx_atom = 0;
2914 unsigned char ws_idx_str[SWM_PROPLEN];
2923 if (win->ws->idx == wsid)
2926 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2929 nws = &win->s->ws[wsid];
2931 a.id = SWM_ARG_ID_FOCUSPREV;
2933 if (win->transient) {
2934 parent = find_window(win->transient);
2936 unmap_window(parent);
2937 TAILQ_REMOVE(&ws->winlist, parent, entry);
2938 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
2943 TAILQ_REMOVE(&ws->winlist, win, entry);
2944 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2947 /* Try to update the window's workspace property */
2948 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2950 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2951 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2953 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2954 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2961 iconify(struct swm_region *r, union arg *args)
2965 if (r->ws->focus == NULL)
2967 unmap_window(r->ws->focus);
2968 update_iconic(r->ws->focus, 1);
2970 if (focus_mode == SWM_FOCUS_DEFAULT)
2971 drain_enter_notify();
2972 r->ws->focus = NULL;
2973 a.id = SWM_ARG_ID_FOCUSCUR;
2978 get_win_name(Display *dpy, Window win, Atom wname, Atom stype,
2979 unsigned long *slen)
2982 unsigned long nitems, nbytes, nextra;
2983 unsigned char *prop = NULL;
2986 status = XGetWindowProperty(dpy, win, wname, 0L, 0L, False, stype,
2987 &rettype, &retfmt, &nitems, &nbytes, &prop);
2988 if (status != Success)
2992 status = XGetWindowProperty(dpy, win, wname, 0L, nbytes, False,
2993 stype, &rettype, &retfmt, &nitems, &nextra, &prop);
2994 if (status != Success) {
2998 if (rettype != stype) {
3007 uniconify(struct swm_region *r, union arg *args)
3015 DNPRINTF(SWM_D_MISC, "uniconify\n");
3017 if (r && r->ws == NULL)
3020 /* make sure we have anything to uniconify */
3021 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3022 if (win->ws == NULL)
3023 continue; /* should never happen */
3024 if (win->iconic == 0)
3033 spawn_select(r, args, "uniconify", &searchpid);
3035 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3038 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3039 if (win->ws == NULL)
3040 continue; /* should never happen */
3041 if (win->iconic == 0)
3044 name = get_win_name(display, win->id, a_wmname, a_string,
3048 fprintf(lfile, "%s.%lu\n", name, win->id);
3055 #define MAX_RESP_LEN 1024
3058 search_do_resp(void)
3062 char *name, *resp, *s;
3065 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3070 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3071 fprintf(stderr, "search: calloc\n");
3075 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3077 fprintf(stderr, "search: read error: %s\n", strerror(errno));
3080 resp[rbytes] = '\0';
3083 DNPRINTF(SWM_D_MISC, "search_do_resp: resp %s\n", resp);
3084 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3085 if (win->iconic == 0)
3087 name = get_win_name(display, win->id, a_wmname, a_string, &len);
3090 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3095 if (strncmp(s, resp, len) == 0) {
3096 /* XXX this should be a callback to generalize */
3097 update_iconic(win, 0);
3108 wkill(struct swm_region *r, union arg *args)
3110 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
3112 if (r->ws->focus == NULL)
3115 if (args->id == SWM_ARG_ID_KILLWINDOW)
3116 XKillClient(display, r->ws->focus->id);
3118 if (r->ws->focus->can_delete)
3119 client_msg(r->ws->focus, adelete);
3124 floating_toggle_win(struct ws_win *win)
3126 struct swm_region *r;
3136 /* reject floating toggles in max stack mode */
3137 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3140 if (win->floating) {
3141 if (!win->floatmaxed) {
3142 /* retain position for refloat */
3143 store_float_geom(win, r);
3147 if (win->g_floatvalid) {
3148 /* refloat at last floating relative position */
3149 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
3150 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
3151 win->g.w = win->g_float.w;
3152 win->g.h = win->g_float.h;
3157 ewmh_update_actions(win);
3163 floating_toggle(struct swm_region *r, union arg *args)
3165 struct ws_win *win = r->ws->focus;
3171 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3172 _NET_WM_STATE_TOGGLE);
3175 if (focus_mode == SWM_FOCUS_DEFAULT)
3176 drain_enter_notify();
3178 if (win == win->ws->focus) {
3179 a.id = SWM_ARG_ID_FOCUSCUR;
3180 focus(win->ws->r, &a);
3185 resize_window(struct ws_win *win, int center)
3189 struct swm_region *r;
3191 r = root_to_region(win->wa.root);
3192 bzero(&wc, sizeof wc);
3193 mask = CWBorderWidth | CWWidth | CWHeight;
3194 wc.border_width = border_width;
3195 wc.width = win->g.w;
3196 wc.height = win->g.h;
3197 if (center == SWM_ARG_ID_CENTER) {
3198 wc.x = (WIDTH(r) - win->g.w) / 2 - border_width;
3199 wc.y = (HEIGHT(r) - win->g.h) / 2 - border_width;
3203 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
3204 win->id, wc.x, wc.y, wc.width, wc.height);
3206 XConfigureWindow(display, win->id, mask, &wc);
3210 resize(struct ws_win *win, union arg *args)
3214 struct swm_region *r = win->ws->r;
3218 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
3219 win->id, win->floating, win->transient);
3221 if (!(win->transient != 0 || win->floating != 0))
3224 /* reject resizes in max mode for floaters (transient ok) */
3225 if (win->floatmaxed)
3229 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3233 if (focus_mode == SWM_FOCUS_DEFAULT)
3234 drain_enter_notify();
3236 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3237 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3240 /* place pointer at bottom left corner or nearest point inside r */
3241 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
3242 relx = win->g.w - 1;
3244 relx = r->g.x + r->g.w - win->g.x - 1;
3246 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
3247 rely = win->g.h - 1;
3249 rely = r->g.y + r->g.h - win->g.y - 1;
3251 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
3253 XMaskEvent(display, MOUSEMASK | ExposureMask |
3254 SubstructureRedirectMask, &ev);
3256 case ConfigureRequest:
3259 handler[ev.type](&ev);
3262 /* do not allow resize outside of region */
3263 if ( ev.xmotion.x_root < r->g.x ||
3264 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3265 ev.xmotion.y_root < r->g.y ||
3266 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3269 if (ev.xmotion.x <= 1)
3271 if (ev.xmotion.y <= 1)
3273 win->g.w = ev.xmotion.x + 1;
3274 win->g.h = ev.xmotion.y + 1;
3276 /* not free, don't sync more than 120 times / second */
3277 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3278 time = ev.xmotion.time;
3279 XSync(display, False);
3280 resize_window(win, args->id);
3284 } while (ev.type != ButtonRelease);
3286 XSync(display, False);
3287 resize_window(win, args->id);
3289 store_float_geom(win,r);
3291 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
3293 XUngrabPointer(display, CurrentTime);
3296 drain_enter_notify();
3300 move_window(struct ws_win *win)
3304 struct swm_region *r;
3306 r = root_to_region(win->wa.root);
3307 bzero(&wc, sizeof wc);
3311 wc.border_width = border_width;
3313 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
3314 win->id, wc.x, wc.y, wc.width, wc.height);
3316 XConfigureWindow(display, win->id, mask, &wc);
3320 move(struct ws_win *win, union arg *args)
3324 struct swm_region *r = win->ws->r;
3326 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
3327 win->id, win->floating, win->transient);
3329 /* in max_stack mode should only move transients */
3330 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
3334 if (win->floating == 0 && !win->transient) {
3335 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3338 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3343 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3344 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3346 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3348 XMaskEvent(display, MOUSEMASK | ExposureMask |
3349 SubstructureRedirectMask, &ev);
3351 case ConfigureRequest:
3354 handler[ev.type](&ev);
3357 /* don't allow to move window origin out of region */
3358 if ( ev.xmotion.x_root < r->g.x ||
3359 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3360 ev.xmotion.y_root < r->g.y ||
3361 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3364 win->g.x = ev.xmotion.x_root - border_width;
3365 win->g.y = ev.xmotion.y_root - border_width;
3367 /* not free, don't sync more than 120 times / second */
3368 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3369 time = ev.xmotion.time;
3370 XSync(display, False);
3375 } while (ev.type != ButtonRelease);
3377 XSync(display, False);
3380 store_float_geom(win,r);
3381 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3382 XUngrabPointer(display, CurrentTime);
3385 drain_enter_notify();
3388 /* user/key callable function IDs */
3445 kf_dumpwins, /* MUST BE LAST */
3449 /* key definitions */
3451 dummykeyfunc(struct swm_region *r, union arg *args)
3456 legacyfunc(struct swm_region *r, union arg *args)
3461 char name[SWM_FUNCNAME_LEN];
3462 void (*func)(struct swm_region *r, union arg *);
3464 } keyfuncs[kf_invalid + 1] = {
3465 /* name function argument */
3466 { "cycle_layout", cycle_layout, {0} },
3467 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
3468 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
3469 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
3470 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
3471 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
3472 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
3473 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
3474 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
3475 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
3476 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
3477 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
3478 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
3479 { "spawn_term", spawnterm, {.argv = spawn_term} },
3480 { "spawn_menu", legacyfunc, {0} },
3481 { "quit", quit, {0} },
3482 { "restart", restart, {0} },
3483 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
3484 { "ws_1", switchws, {.id = 0} },
3485 { "ws_2", switchws, {.id = 1} },
3486 { "ws_3", switchws, {.id = 2} },
3487 { "ws_4", switchws, {.id = 3} },
3488 { "ws_5", switchws, {.id = 4} },
3489 { "ws_6", switchws, {.id = 5} },
3490 { "ws_7", switchws, {.id = 6} },
3491 { "ws_8", switchws, {.id = 7} },
3492 { "ws_9", switchws, {.id = 8} },
3493 { "ws_10", switchws, {.id = 9} },
3494 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
3495 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
3496 { "ws_prior", priorws, {0} },
3497 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
3498 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
3499 { "mvws_1", send_to_ws, {.id = 0} },
3500 { "mvws_2", send_to_ws, {.id = 1} },
3501 { "mvws_3", send_to_ws, {.id = 2} },
3502 { "mvws_4", send_to_ws, {.id = 3} },
3503 { "mvws_5", send_to_ws, {.id = 4} },
3504 { "mvws_6", send_to_ws, {.id = 5} },
3505 { "mvws_7", send_to_ws, {.id = 6} },
3506 { "mvws_8", send_to_ws, {.id = 7} },
3507 { "mvws_9", send_to_ws, {.id = 8} },
3508 { "mvws_10", send_to_ws, {.id = 9} },
3509 { "bar_toggle", bar_toggle, {0} },
3510 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
3511 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
3512 { "screenshot_all", legacyfunc, {0} },
3513 { "screenshot_wind", legacyfunc, {0} },
3514 { "float_toggle", floating_toggle,{0} },
3515 { "version", version, {0} },
3516 { "spawn_lock", legacyfunc, {0} },
3517 { "spawn_initscr", legacyfunc, {0} },
3518 { "spawn_custom", dummykeyfunc, {0} },
3519 { "iconify", iconify, {0} },
3520 { "uniconify", uniconify, {0} },
3521 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
3522 { "invalid key func", NULL, {0} },
3527 enum keyfuncid funcid;
3530 int keys_size = 0, keys_length = 0;
3531 struct key *keys = NULL;
3534 enum { client_click, root_click };
3536 unsigned int action;
3538 unsigned int button;
3539 void (*func)(struct ws_win *, union arg *);
3542 /* action key mouse button func args */
3543 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
3544 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
3545 { client_click, MODKEY, Button1, move, {0} },
3549 update_modkey(unsigned int mod)
3554 for (i = 0; i < keys_length; i++)
3555 if (keys[i].mod & ShiftMask)
3556 keys[i].mod = mod | ShiftMask;
3560 for (i = 0; i < LENGTH(buttons); i++)
3561 if (buttons[i].mask & ShiftMask)
3562 buttons[i].mask = mod | ShiftMask;
3564 buttons[i].mask = mod;
3574 int spawns_size = 0, spawns_length = 0;
3575 struct spawn_prog *spawns = NULL;
3578 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
3581 struct spawn_prog *prog = NULL;
3583 char *ap, **real_args;
3585 DNPRINTF(SWM_D_SPAWN, "spawn_expand %s\n", spawn_name);
3588 for (i = 0; i < spawns_length; i++) {
3589 if (!strcasecmp(spawn_name, spawns[i].name))
3593 fprintf(stderr, "spawn_custom: program %s not found\n",
3598 /* make room for expanded args */
3599 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
3600 err(1, "spawn_custom: calloc real_args");
3602 /* expand spawn_args into real_args */
3603 for (i = 0; i < prog->argc; i++) {
3605 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
3606 if (!strcasecmp(ap, "$bar_border")) {
3608 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
3610 err(1, "spawn_custom border color");
3611 } else if (!strcasecmp(ap, "$bar_color")) {
3613 strdup(r->s->c[SWM_S_COLOR_BAR].name))
3615 err(1, "spawn_custom bar color");
3616 } else if (!strcasecmp(ap, "$bar_font")) {
3617 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
3619 err(1, "spawn_custom bar fonts");
3620 } else if (!strcasecmp(ap, "$bar_font_color")) {
3622 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
3624 err(1, "spawn_custom color font");
3625 } else if (!strcasecmp(ap, "$color_focus")) {
3627 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
3629 err(1, "spawn_custom color focus");
3630 } else if (!strcasecmp(ap, "$color_unfocus")) {
3632 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
3634 err(1, "spawn_custom color unfocus");
3636 /* no match --> copy as is */
3637 if ((real_args[i] = strdup(ap)) == NULL)
3638 err(1, "spawn_custom strdup(ap)");
3640 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
3645 if ((swm_debug & SWM_D_SPAWN) != 0) {
3646 fprintf(stderr, "spawn_custom: result = ");
3647 for (i = 0; i < prog->argc; i++)
3648 fprintf(stderr, "\"%s\" ", real_args[i]);
3649 fprintf(stderr, "\n");
3652 *ret_args = real_args;
3653 return (prog->argc);
3657 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
3663 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3669 for (i = 0; i < spawn_argc; i++)
3675 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
3681 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3685 if (pipe(select_list_pipe) == -1)
3686 err(1, "pipe error");
3687 if (pipe(select_resp_pipe) == -1)
3688 err(1, "pipe error");
3690 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
3691 err(1, "could not disable SIGPIPE");
3692 switch (*pid = fork()) {
3694 err(1, "cannot fork");
3697 if (dup2(select_list_pipe[0], 0) == -1)
3699 if (dup2(select_resp_pipe[1], 1) == -1)
3701 close(select_list_pipe[1]);
3702 close(select_resp_pipe[0]);
3705 default: /* parent */
3706 close(select_list_pipe[0]);
3707 close(select_resp_pipe[1]);
3711 for (i = 0; i < spawn_argc; i++)
3717 setspawn(struct spawn_prog *prog)
3721 if (prog == NULL || prog->name == NULL)
3725 for (i = 0; i < spawns_length; i++) {
3726 if (!strcmp(spawns[i].name, prog->name)) {
3728 if (prog->argv == NULL) {
3730 DNPRINTF(SWM_D_SPAWN,
3731 "setspawn: delete #%d %s\n",
3733 free(spawns[i].name);
3734 for (j = 0; j < spawns[i].argc; j++)
3735 free(spawns[i].argv[j]);
3736 free(spawns[i].argv);
3737 j = spawns_length - 1;
3739 spawns[i] = spawns[j];
3744 DNPRINTF(SWM_D_SPAWN,
3745 "setspawn: replace #%d %s\n",
3747 free(spawns[i].name);
3748 for (j = 0; j < spawns[i].argc; j++)
3749 free(spawns[i].argv[j]);
3750 free(spawns[i].argv);
3753 /* found case handled */
3759 if (prog->argv == NULL) {
3761 "error: setspawn: cannot find program %s", prog->name);
3766 /* not found: add */
3767 if (spawns_size == 0 || spawns == NULL) {
3769 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
3770 spawns = malloc((size_t)spawns_size *
3771 sizeof(struct spawn_prog));
3772 if (spawns == NULL) {
3773 fprintf(stderr, "setspawn: malloc failed\n");
3777 } else if (spawns_length == spawns_size) {
3779 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
3780 spawns = realloc(spawns, (size_t)spawns_size *
3781 sizeof(struct spawn_prog));
3782 if (spawns == NULL) {
3783 fprintf(stderr, "setspawn: realloc failed\n");
3789 if (spawns_length < spawns_size) {
3790 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
3791 spawns_length, prog->name);
3792 i = spawns_length++;
3795 fprintf(stderr, "spawns array problem?\n");
3796 if (spawns == NULL) {
3797 fprintf(stderr, "spawns array is NULL!\n");
3805 setconfspawn(char *selector, char *value, int flags)
3807 struct spawn_prog *prog;
3808 char *vp, *cp, *word;
3810 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
3811 if ((prog = calloc(1, sizeof *prog)) == NULL)
3812 err(1, "setconfspawn: calloc prog");
3813 prog->name = strdup(selector);
3814 if (prog->name == NULL)
3815 err(1, "setconfspawn prog->name");
3816 if ((cp = vp = strdup(value)) == NULL)
3817 err(1, "setconfspawn: strdup(value) ");
3818 while ((word = strsep(&cp, " \t")) != NULL) {
3819 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
3821 cp += (long)strspn(cp, " \t");
3822 if (strlen(word) > 0) {
3824 prog->argv = realloc(prog->argv,
3825 prog->argc * sizeof(char *));
3826 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
3827 err(1, "setconfspawn: strdup");
3834 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
3841 setconfspawn("term", "xterm", 0);
3842 setconfspawn("screenshot_all", "screenshot.sh full", 0);
3843 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
3844 setconfspawn("lock", "xlock", 0);
3845 setconfspawn("initscr", "initscreen.sh", 0);
3846 setconfspawn("menu", "dmenu_run"
3849 " -nf $bar_font_color"
3851 " -sf $bar_color", 0);
3852 setconfspawn("uniconify", "dmenu"
3856 " -nf $bar_font_color"
3858 " -sf $bar_color", 0);
3862 #define SWM_MODNAME_SIZE 32
3863 #define SWM_KEY_WS "\n+ \t"
3865 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
3869 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
3870 if (mod == NULL || ks == NULL) {
3871 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
3874 if (keystr == NULL || strlen(keystr) == 0) {
3875 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
3881 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
3882 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
3884 cp += (long)strspn(cp, SWM_KEY_WS);
3885 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
3887 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
3889 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
3891 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
3893 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
3895 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
3897 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
3898 *mod |= ControlMask;
3900 *ks = XStringToKeysym(name);
3901 XConvertCase(*ks, ks, &uks);
3902 if (ks == NoSymbol) {
3904 "parsekeys: invalid key %s\n",
3910 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
3915 strdupsafe(char *str)
3920 return (strdup(str));
3924 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
3927 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
3928 keyfuncs[kfid].name, spawn_name);
3930 for (i = 0; i < keys_length; i++) {
3931 if (keys[i].mod == mod && keys[i].keysym == ks) {
3932 if (kfid == kf_invalid) {
3935 "setkeybinding: delete #%d %s\n",
3936 i, keyfuncs[keys[i].funcid].name);
3937 free(keys[i].spawn_name);
3938 j = keys_length - 1;
3942 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3945 /* found: replace */
3947 "setkeybinding: replace #%d %s %s\n",
3948 i, keyfuncs[keys[i].funcid].name,
3950 free(keys[i].spawn_name);
3952 keys[i].keysym = ks;
3953 keys[i].funcid = kfid;
3954 keys[i].spawn_name = strdupsafe(spawn_name);
3955 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3960 if (kfid == kf_invalid) {
3962 "error: setkeybinding: cannot find mod/key combination");
3963 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3966 /* not found: add */
3967 if (keys_size == 0 || keys == NULL) {
3969 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
3970 keys = malloc((size_t)keys_size * sizeof(struct key));
3972 fprintf(stderr, "malloc failed\n");
3976 } else if (keys_length == keys_size) {
3978 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
3979 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
3981 fprintf(stderr, "realloc failed\n");
3986 if (keys_length < keys_size) {
3988 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
3989 j, keyfuncs[kfid].name, spawn_name);
3991 keys[j].keysym = ks;
3992 keys[j].funcid = kfid;
3993 keys[j].spawn_name = strdupsafe(spawn_name);
3995 fprintf(stderr, "keys array problem?\n");
3997 fprintf(stderr, "keys array problem\n");
4001 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4005 setconfbinding(char *selector, char *value, int flags)
4007 enum keyfuncid kfid;
4011 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
4012 if (selector == NULL) {
4013 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
4014 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4016 setkeybinding(mod, ks, kfid, NULL);
4021 /* search by key function name */
4022 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
4023 if (strncasecmp(selector, keyfuncs[kfid].name,
4024 SWM_FUNCNAME_LEN) == 0) {
4025 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4027 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4028 setkeybinding(mod, ks, kfid, NULL);
4034 /* search by custom spawn name */
4035 for (i = 0; i < spawns_length; i++) {
4036 if (strcasecmp(selector, spawns[i].name) == 0) {
4037 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4039 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4040 setkeybinding(mod, ks, kf_spawn_custom,
4047 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
4054 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
4055 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
4056 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
4057 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
4058 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
4059 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
4060 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
4061 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
4062 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
4063 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
4064 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
4065 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
4066 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
4067 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
4068 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
4069 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
4070 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
4071 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
4072 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
4073 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
4074 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
4075 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
4076 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
4077 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
4078 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
4079 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
4080 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
4081 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
4082 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
4083 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
4084 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
4085 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
4086 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
4087 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
4088 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
4089 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
4090 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
4091 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
4092 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
4093 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
4094 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
4095 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
4096 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
4097 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
4098 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
4099 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
4100 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
4101 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
4102 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
4103 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
4104 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
4105 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
4106 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
4107 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
4108 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
4109 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
4111 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
4116 updatenumlockmask(void)
4119 XModifierKeymap *modmap;
4121 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
4123 modmap = XGetModifierMapping(display);
4124 for (i = 0; i < 8; i++)
4125 for (j = 0; j < modmap->max_keypermod; j++)
4126 if (modmap->modifiermap[i * modmap->max_keypermod + j]
4127 == XKeysymToKeycode(display, XK_Num_Lock))
4128 numlockmask = (1 << i);
4130 XFreeModifiermap(modmap);
4136 unsigned int i, j, k;
4138 unsigned int modifiers[] =
4139 { 0, LockMask, numlockmask, numlockmask | LockMask };
4141 DNPRINTF(SWM_D_MISC, "grabkeys\n");
4142 updatenumlockmask();
4144 for (k = 0; k < ScreenCount(display); k++) {
4145 if (TAILQ_EMPTY(&screens[k].rl))
4147 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
4148 for (i = 0; i < keys_length; i++) {
4149 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
4150 for (j = 0; j < LENGTH(modifiers); j++)
4151 XGrabKey(display, code,
4152 keys[i].mod | modifiers[j],
4153 screens[k].root, True,
4154 GrabModeAsync, GrabModeAsync);
4160 grabbuttons(struct ws_win *win, int focused)
4163 unsigned int modifiers[] =
4164 { 0, LockMask, numlockmask, numlockmask|LockMask };
4166 updatenumlockmask();
4167 XUngrabButton(display, AnyButton, AnyModifier, win->id);
4169 for (i = 0; i < LENGTH(buttons); i++)
4170 if (buttons[i].action == client_click)
4171 for (j = 0; j < LENGTH(modifiers); j++)
4172 XGrabButton(display, buttons[i].button,
4173 buttons[i].mask | modifiers[j],
4174 win->id, False, BUTTONMASK,
4175 GrabModeAsync, GrabModeSync, None,
4178 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
4179 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
4182 const char *quirkname[] = {
4183 "NONE", /* config string for "no value" */
4191 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
4192 #define SWM_Q_WS "\n|+ \t"
4194 parsequirks(char *qstr, unsigned long *quirk)
4204 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
4206 cp += (long)strspn(cp, SWM_Q_WS);
4207 for (i = 0; i < LENGTH(quirkname); i++) {
4208 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
4209 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
4214 *quirk |= 1 << (i-1);
4218 if (i >= LENGTH(quirkname)) {
4219 DNPRINTF(SWM_D_QUIRK,
4220 "parsequirks: invalid quirk [%s]\n", name);
4228 setquirk(const char *class, const char *name, const int quirk)
4233 for (i = 0; i < quirks_length; i++) {
4234 if (!strcmp(quirks[i].class, class) &&
4235 !strcmp(quirks[i].name, name)) {
4238 DNPRINTF(SWM_D_QUIRK,
4239 "setquirk: delete #%d %s:%s\n",
4240 i, quirks[i].class, quirks[i].name);
4241 free(quirks[i].class);
4242 free(quirks[i].name);
4243 j = quirks_length - 1;
4245 quirks[i] = quirks[j];
4249 /* found: replace */
4250 DNPRINTF(SWM_D_QUIRK,
4251 "setquirk: replace #%d %s:%s\n",
4252 i, quirks[i].class, quirks[i].name);
4253 free(quirks[i].class);
4254 free(quirks[i].name);
4255 quirks[i].class = strdup(class);
4256 quirks[i].name = strdup(name);
4257 quirks[i].quirk = quirk;
4264 "error: setquirk: cannot find class/name combination");
4267 /* not found: add */
4268 if (quirks_size == 0 || quirks == NULL) {
4270 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
4271 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
4273 fprintf(stderr, "setquirk: malloc failed\n");
4277 } else if (quirks_length == quirks_size) {
4279 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
4280 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
4282 fprintf(stderr, "setquirk: realloc failed\n");
4287 if (quirks_length < quirks_size) {
4288 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
4289 j = quirks_length++;
4290 quirks[j].class = strdup(class);
4291 quirks[j].name = strdup(name);
4292 quirks[j].quirk = quirk;
4294 fprintf(stderr, "quirks array problem?\n");
4296 fprintf(stderr, "quirks array problem!\n");
4303 setconfquirk(char *selector, char *value, int flags)
4305 char *cp, *class, *name;
4307 unsigned long quirks;
4308 if (selector == NULL)
4310 if ((cp = strchr(selector, ':')) == NULL)
4315 if ((retval = parsequirks(value, &quirks)) == 0)
4316 setquirk(class, name, quirks);
4323 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
4324 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
4325 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
4326 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
4327 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4328 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
4329 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4330 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4331 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4332 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4333 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
4334 setquirk("pcb", "pcb", SWM_Q_FLOAT);
4335 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
4338 /* conf file stuff */
4339 #define SWM_CONF_FILE "scrotwm.conf"
4341 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH, SWM_S_STACK_ENABLED,
4342 SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT, SWM_S_CYCLE_EMPTY,
4343 SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH,
4344 SWM_S_TITLE_CLASS_ENABLED, SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED,
4345 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH, SWM_S_BAR_FONT,
4346 SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM, SWM_S_SS_APP, SWM_S_DIALOG_RATIO,
4351 setconfvalue(char *selector, char *value, int flags)
4354 case SWM_S_BAR_DELAY:
4355 bar_delay = atoi(value);
4357 case SWM_S_BAR_ENABLED:
4358 bar_enabled = atoi(value);
4360 case SWM_S_BAR_BORDER_WIDTH:
4361 bar_border_width = atoi(value);
4363 case SWM_S_BAR_AT_BOTTOM:
4364 bar_at_bottom = atoi(value);
4366 case SWM_S_STACK_ENABLED:
4367 stack_enabled = atoi(value);
4369 case SWM_S_CLOCK_ENABLED:
4370 clock_enabled = atoi(value);
4372 case SWM_S_CLOCK_FORMAT:
4373 #ifndef SWM_DENY_CLOCK_FORMAT
4375 if ((clock_format = strdup(value)) == NULL)
4376 err(1, "setconfvalue: clock_format");
4379 case SWM_S_CYCLE_EMPTY:
4380 cycle_empty = atoi(value);
4382 case SWM_S_CYCLE_VISIBLE:
4383 cycle_visible = atoi(value);
4385 case SWM_S_SS_ENABLED:
4386 ss_enabled = atoi(value);
4388 case SWM_S_TERM_WIDTH:
4389 term_width = atoi(value);
4391 case SWM_S_TITLE_CLASS_ENABLED:
4392 title_class_enabled = atoi(value);
4394 case SWM_S_WINDOW_NAME_ENABLED:
4395 window_name_enabled = atoi(value);
4397 case SWM_S_TITLE_NAME_ENABLED:
4398 title_name_enabled = atoi(value);
4400 case SWM_S_FOCUS_MODE:
4401 if (!strcmp(value, "default"))
4402 focus_mode = SWM_FOCUS_DEFAULT;
4403 else if (!strcmp(value, "follow_cursor"))
4404 focus_mode = SWM_FOCUS_FOLLOW;
4405 else if (!strcmp(value, "synergy"))
4406 focus_mode = SWM_FOCUS_SYNERGY;
4408 err(1, "focus_mode");
4410 case SWM_S_DISABLE_BORDER:
4411 disable_border = atoi(value);
4413 case SWM_S_BORDER_WIDTH:
4414 border_width = atoi(value);
4416 case SWM_S_BAR_FONT:
4418 if ((bar_fonts[0] = strdup(value)) == NULL)
4419 err(1, "setconfvalue: bar_font");
4421 case SWM_S_BAR_ACTION:
4423 if ((bar_argv[0] = strdup(value)) == NULL)
4424 err(1, "setconfvalue: bar_action");
4426 case SWM_S_SPAWN_TERM:
4427 free(spawn_term[0]);
4428 if ((spawn_term[0] = strdup(value)) == NULL)
4429 err(1, "setconfvalue: spawn_term");
4433 case SWM_S_DIALOG_RATIO:
4434 dialog_ratio = atof(value);
4435 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
4445 setconfmodkey(char *selector, char *value, int flags)
4447 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
4448 update_modkey(Mod1Mask);
4449 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
4450 update_modkey(Mod2Mask);
4451 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
4452 update_modkey(Mod3Mask);
4453 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
4454 update_modkey(Mod4Mask);
4461 setconfcolor(char *selector, char *value, int flags)
4463 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
4468 setconfregion(char *selector, char *value, int flags)
4470 custom_region(value);
4474 /* config options */
4475 struct config_option {
4477 int (*func)(char*, char*, int);
4480 struct config_option configopt[] = {
4481 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
4482 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
4483 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
4484 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
4485 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
4486 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
4487 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
4488 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
4489 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
4490 { "bind", setconfbinding, 0 },
4491 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
4492 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
4493 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
4494 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
4495 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
4496 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
4497 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
4498 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
4499 { "modkey", setconfmodkey, 0 },
4500 { "program", setconfspawn, 0 },
4501 { "quirk", setconfquirk, 0 },
4502 { "region", setconfregion, 0 },
4503 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
4504 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
4505 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
4506 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
4507 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
4508 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
4509 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
4510 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
4511 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
4512 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
4517 conf_load(char *filename)
4520 char *line, *cp, *optsub, *optval;
4521 size_t linelen, lineno = 0;
4522 int wordlen, i, optind;
4523 struct config_option *opt;
4525 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
4527 if (filename == NULL) {
4528 fprintf(stderr, "conf_load: no filename\n");
4531 if ((config = fopen(filename, "r")) == NULL) {
4532 warn("conf_load: fopen");
4536 while (!feof(config)) {
4537 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
4540 err(1, "%s", filename);
4545 cp += strspn(cp, " \t\n"); /* eat whitespace */
4546 if (cp[0] == '\0') {
4551 /* get config option */
4552 wordlen = strcspn(cp, "=[ \t\n");
4554 warnx("%s: line %zd: no option found",
4559 for (i = 0; i < LENGTH(configopt); i++) {
4560 opt = &configopt[i];
4561 if (!strncasecmp(cp, opt->optname, wordlen) &&
4562 strlen(opt->optname) == wordlen) {
4568 warnx("%s: line %zd: unknown option %.*s",
4569 filename, lineno, wordlen, cp);
4573 cp += strspn(cp, " \t\n"); /* eat whitespace */
4574 /* get [selector] if any */
4578 wordlen = strcspn(cp, "]");
4581 warnx("%s: line %zd: syntax error",
4585 asprintf(&optsub, "%.*s", wordlen, cp);
4588 cp += strspn(cp, "] \t\n"); /* eat trailing */
4590 cp += strspn(cp, "= \t\n"); /* eat trailing */
4592 optval = strdup(cp);
4593 /* call function to deal with it all */
4594 if (configopt[optind].func(optsub, optval,
4595 configopt[optind].funcflags) != 0) {
4596 fprintf(stderr, "%s line %zd: %s\n",
4597 filename, lineno, line);
4598 errx(1, "%s: line %zd: invalid data for %s",
4599 filename, lineno, configopt[optind].optname);
4607 DNPRINTF(SWM_D_CONF, "conf_load end\n");
4613 set_child_transient(struct ws_win *win, Window *trans)
4615 struct ws_win *parent, *w;
4616 XWMHints *wmh = NULL;
4617 struct swm_region *r;
4618 struct workspace *ws;
4620 parent = find_window(win->transient);
4622 parent->child_trans = win;
4624 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
4625 " for %lu trans %lu\n", win->id, win->transient);
4627 if (win->hints == NULL) {
4628 fprintf(stderr, "no hints for %lu\n", win->id);
4632 r = root_to_region(win->wa.root);
4634 /* parent doen't exist in our window list */
4635 TAILQ_FOREACH(w, &ws->winlist, entry) {
4639 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
4640 fprintf(stderr, "can't get hints for %lu\n",
4645 if (win->hints->window_group != wmh->window_group)
4648 w->child_trans = win;
4649 win->transient = w->id;
4651 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
4652 "transient to %lu\n", win->transient);
4662 manage_window(Window id)
4665 struct workspace *ws;
4666 struct ws_win *win, *ww;
4667 int format, i, ws_idx, n, border_me = 0;
4668 unsigned long nitems, bytes;
4669 Atom ws_idx_atom = 0, type;
4670 Atom *prot = NULL, *pp;
4671 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
4672 struct swm_region *r;
4677 if ((win = find_window(id)) != NULL)
4678 return (win); /* already being managed */
4680 /* see if we are on the unmanaged list */
4681 if ((win = find_unmanaged_window(id)) != NULL) {
4682 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
4684 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
4685 if (win->transient) {
4686 set_child_transient(win, &trans);
4687 } if (trans && (ww = find_window(trans)))
4688 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
4690 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
4691 ewmh_update_actions(win);
4695 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
4696 errx(1, "calloc: failed to allocate memory for new window");
4700 /* Get all the window data in one shot */
4701 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4703 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
4704 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
4705 XGetWindowAttributes(display, id, &win->wa);
4706 XGetWMNormalHints(display, id, &win->sh, &mask);
4707 win->hints = XGetWMHints(display, id);
4708 XGetTransientForHint(display, id, &trans);
4710 win->transient = trans;
4711 set_child_transient(win, &trans);
4712 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
4713 win->id, win->transient);
4716 /* get supported protocols */
4717 if (XGetWMProtocols(display, id, &prot, &n)) {
4718 for (i = 0, pp = prot; i < n; i++, pp++) {
4719 if (*pp == takefocus)
4720 win->take_focus = 1;
4722 win->can_delete = 1;
4728 win->iconic = get_iconic(win);
4731 * Figure out where to put the window. If it was previously assigned to
4732 * a workspace (either by spawn() or manually moving), and isn't
4733 * transient, * put it in the same workspace
4735 r = root_to_region(win->wa.root);
4736 if (prop && win->transient == 0) {
4737 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
4738 ws_idx = strtonum(prop, 0, 9, &errstr);
4740 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
4743 ws = &r->s->ws[ws_idx];
4746 /* this should launch transients in the same ws as parent */
4748 if ((ww = find_window(trans)) != NULL)
4755 "fix this bug mcbride\n");
4760 /* set up the window layout */
4763 win->s = r->s; /* this never changes */
4764 if (trans && (ww = find_window(trans)))
4765 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
4767 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
4769 win->g.w = win->wa.width;
4770 win->g.h = win->wa.height;
4771 win->g.x = win->wa.x;
4772 win->g.y = win->wa.y;
4773 win->g_floatvalid = 0;
4774 win->floatmaxed = 0;
4775 win->ewmh_flags = 0;
4777 /* Set window properties so we can remember this after reincarnation */
4778 if (ws_idx_atom && prop == NULL &&
4779 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
4780 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
4782 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
4783 PropModeReplace, ws_idx_str, SWM_PROPLEN);
4788 ewmh_autoquirk(win);
4790 if (XGetClassHint(display, win->id, &win->ch)) {
4791 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
4792 win->ch.res_class, win->ch.res_name);
4794 /* java is retarded so treat it special */
4795 if (strstr(win->ch.res_name, "sun-awt")) {
4800 for (i = 0; i < quirks_length; i++){
4801 if (!strcmp(win->ch.res_class, quirks[i].class) &&
4802 !strcmp(win->ch.res_name, quirks[i].name)) {
4803 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
4804 win->ch.res_class, win->ch.res_name);
4805 if (quirks[i].quirk & SWM_Q_FLOAT) {
4809 win->quirks = quirks[i].quirk;
4814 /* alter window position if quirky */
4815 if (win->quirks & SWM_Q_ANYWHERE) {
4816 win->manual = 1; /* don't center the quirky windows */
4817 bzero(&wc, sizeof wc);
4819 if (bar_enabled && win->g.y < bar_height) {
4820 win->g.y = wc.y = bar_height;
4823 if (win->g.w + win->g.x > WIDTH(r)) {
4824 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
4830 /* Reset font sizes (the bruteforce way; no default keybinding). */
4831 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
4832 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
4833 fake_keypress(win, XK_KP_Subtract, ShiftMask);
4834 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
4835 fake_keypress(win, XK_KP_Add, ShiftMask);
4838 ewmh_get_win_state(win);
4839 ewmh_update_actions(win);
4840 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
4844 bzero(&wc, sizeof wc);
4845 wc.border_width = border_width;
4846 mask = CWBorderWidth;
4847 XConfigureWindow(display, win->id, mask, &wc);
4850 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
4851 PropertyChangeMask | StructureNotifyMask);
4853 /* floaters need to be mapped if they are in the current workspace */
4854 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
4855 XMapRaised(display, win->id);
4861 free_window(struct ws_win *win)
4863 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
4868 /* needed for restart wm */
4869 set_win_state(win, WithdrawnState);
4871 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
4873 if (win->ch.res_class)
4874 XFree(win->ch.res_class);
4875 if (win->ch.res_name)
4876 XFree(win->ch.res_name);
4881 memset(win, 0xff, sizeof *win); /* XXX kill later */
4887 unmanage_window(struct ws_win *win)
4889 struct ws_win *parent;
4894 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
4896 if (win->transient) {
4897 parent = find_window(win->transient);
4899 parent->child_trans = NULL;
4902 /* focus on root just in case */
4903 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
4907 TAILQ_REMOVE(&win->ws->winlist, win, entry);
4908 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
4914 focus_magic(struct ws_win *win)
4916 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu\n", WINID(win));
4921 if (win->child_trans) {
4922 /* win = parent & has a transient so focus on that */
4924 focus_win(win->child_trans);
4925 if (win->child_trans->take_focus)
4926 client_msg(win, takefocus);
4928 /* make sure transient hasn't dissapeared */
4929 if (validate_win(win->child_trans) == 0) {
4930 focus_win(win->child_trans);
4931 if (win->child_trans->take_focus)
4932 client_msg(win->child_trans, takefocus);
4934 win->child_trans = NULL;
4936 if (win->take_focus)
4937 client_msg(win, takefocus);
4943 if (win->take_focus)
4944 client_msg(win, takefocus);
4951 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
4959 XKeyEvent *ev = &e->xkey;
4961 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
4963 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
4964 for (i = 0; i < keys_length; i++)
4965 if (keysym == keys[i].keysym
4966 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
4967 && keyfuncs[keys[i].funcid].func) {
4968 if (keys[i].funcid == kf_spawn_custom)
4970 root_to_region(ev->root),
4971 &(keyfuncs[keys[i].funcid].args),
4975 keyfuncs[keys[i].funcid].func(
4976 root_to_region(ev->root),
4977 &(keyfuncs[keys[i].funcid].args)
4983 buttonpress(XEvent *e)
4987 XButtonPressedEvent *ev = &e->xbutton;
4989 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
4991 action = root_click;
4992 if ((win = find_window(ev->window)) == NULL)
4996 action = client_click;
4998 for (i = 0; i < LENGTH(buttons); i++)
4999 if (action == buttons[i].action && buttons[i].func &&
5000 buttons[i].button == ev->button &&
5001 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
5002 buttons[i].func(win, &buttons[i].args);
5006 configurerequest(XEvent *e)
5008 XConfigureRequestEvent *ev = &e->xconfigurerequest;
5013 if ((win = find_window(ev->window)) == NULL)
5014 if ((win = find_unmanaged_window(ev->window)) == NULL)
5018 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
5020 bzero(&wc, sizeof wc);
5023 wc.width = ev->width;
5024 wc.height = ev->height;
5025 wc.border_width = ev->border_width;
5026 wc.sibling = ev->above;
5027 wc.stack_mode = ev->detail;
5028 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
5030 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
5032 config_win(win, ev);
5037 configurenotify(XEvent *e)
5042 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
5043 e->xconfigure.window);
5045 win = find_window(e->xconfigure.window);
5047 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5051 if (focus_mode == SWM_FOCUS_DEFAULT)
5052 drain_enter_notify();
5057 destroynotify(XEvent *e)
5060 XDestroyWindowEvent *ev = &e->xdestroywindow;
5062 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
5064 if ((win = find_window(ev->window)) == NULL) {
5065 if ((win = find_unmanaged_window(ev->window)) == NULL)
5071 /* make sure we focus on something */
5074 unmanage_window(win);
5076 if (focus_mode == SWM_FOCUS_DEFAULT)
5077 drain_enter_notify();
5082 enternotify(XEvent *e)
5084 XCrossingEvent *ev = &e->xcrossing;
5089 Window focus_return;
5090 int revert_to_return;
5092 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
5093 "%lu subwindow %lu same_screen %d focus %d state %d\n",
5094 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
5095 ev->same_screen, ev->focus, ev->state);
5097 switch (focus_mode) {
5098 case SWM_FOCUS_DEFAULT:
5100 case SWM_FOCUS_FOLLOW:
5102 case SWM_FOCUS_SYNERGY:
5105 * all these checks need to be in this order because the
5106 * XCheckTypedWindowEvent relies on weeding out the previous events
5108 * making this code an option would enable a follow mouse for focus
5113 * state is set when we are switching workspaces and focus is set when
5114 * the window or a subwindow already has focus (occurs during restart).
5116 * Only honor the focus flag if last_focus_event is not FocusOut,
5117 * this allows scrotwm to continue to control focus when another
5118 * program is also playing with it.
5120 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
5121 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
5126 * happens when a window is created or destroyed and the border
5127 * crosses the mouse pointer and when switching ws
5129 * we need the subwindow test to see if we came from root in order
5130 * to give focus to floaters
5132 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
5133 ev->subwindow == 0) {
5134 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
5138 /* this window already has focus */
5139 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
5140 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
5144 /* this window is being deleted or moved to another ws */
5145 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
5147 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
5148 XPutBackEvent(display, &cne);
5152 if ((win = find_window(ev->window)) == NULL) {
5153 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5158 * In fullstack kill all enters unless they come from a different ws
5159 * (i.e. another region) or focus has been grabbed externally.
5161 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
5162 last_focus_event != FocusOut) {
5163 XGetInputFocus(display, &focus_return, &revert_to_return);
5164 if ((w = find_window(focus_return)) == NULL ||
5166 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
5174 if ((win = find_window(ev->window)) == NULL) {
5175 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5180 * if we have more enternotifies let them handle it in due time
5182 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
5183 DNPRINTF(SWM_D_EVENT,
5184 "ignoring enternotify: got more enternotify\n");
5185 XPutBackEvent(display, &cne);
5192 /* lets us use one switch statement for arbitrary mode/detail combinations */
5193 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
5196 focusevent(XEvent *e)
5200 u_int32_t mode_detail;
5201 XFocusChangeEvent *ev = &e->xfocus;
5203 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
5204 ev->type == FocusIn ? "entering" : "leaving",
5205 ev->window, ev->mode, ev->detail);
5207 if (last_focus_event == ev->type) {
5208 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
5212 last_focus_event = ev->type;
5213 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
5215 switch (mode_detail) {
5216 /* synergy client focus operations */
5217 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
5218 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
5220 /* synergy server focus operations */
5221 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
5223 /* Entering applications like rdesktop that mangle the pointer */
5224 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
5226 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
5227 XSetWindowBorder(display, win->id,
5228 win->ws->r->s->c[ev->type == FocusIn ?
5229 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
5232 fprintf(stderr, "ignoring focusevent\n");
5233 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
5240 mapnotify(XEvent *e)
5243 XMapEvent *ev = &e->xmap;
5245 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
5247 win = manage_window(ev->window);
5249 set_win_state(win, NormalState);
5253 mappingnotify(XEvent *e)
5255 XMappingEvent *ev = &e->xmapping;
5257 XRefreshKeyboardMapping(ev);
5258 if (ev->request == MappingKeyboard)
5263 maprequest(XEvent *e)
5266 struct swm_region *r;
5267 XWindowAttributes wa;
5268 XMapRequestEvent *ev = &e->xmaprequest;
5270 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
5271 e->xmaprequest.window);
5273 if (!XGetWindowAttributes(display, ev->window, &wa))
5275 if (wa.override_redirect)
5278 win = manage_window(e->xmaprequest.window);
5280 return; /* can't happen */
5284 /* make new win focused */
5285 r = root_to_region(win->wa.root);
5286 if (win->ws == r->ws)
5291 propertynotify(XEvent *e)
5294 XPropertyEvent *ev = &e->xproperty;
5296 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
5299 win = find_window(ev->window);
5303 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
5304 update_iconic(win, 0);
5305 XMapRaised(display, win->id);
5312 case XA_WM_NORMAL_HINTS:
5315 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5316 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
5317 if (win->sh.flags & PMinSize) {
5318 win->g.w = win->sh.min_width;
5319 win->g.h = win->sh.min_height;
5320 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
5322 XMoveResizeWindow(display, win->id,
5323 win->g.x, win->g.y, win->g.w, win->g.h);
5325 if (window_name_enabled)
5334 unmapnotify(XEvent *e)
5338 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
5340 /* determine if we need to help unmanage this window */
5341 win = find_window(e->xunmap.window);
5345 if (getstate(e->xunmap.window) == NormalState) {
5346 unmanage_window(win);
5349 /* giant hack for apps that don't destroy transient windows */
5350 /* eat a bunch of events to prevent remanaging the window */
5352 while (XCheckWindowEvent(display, e->xunmap.window,
5353 EnterWindowMask, &cne))
5355 while (XCheckWindowEvent(display, e->xunmap.window,
5356 StructureNotifyMask, &cne))
5358 while (XCheckWindowEvent(display, e->xunmap.window,
5359 SubstructureNotifyMask, &cne))
5361 /* resend unmap because we ated it */
5362 XUnmapWindow(display, e->xunmap.window);
5365 if (focus_mode == SWM_FOCUS_DEFAULT)
5366 drain_enter_notify();
5370 visibilitynotify(XEvent *e)
5373 struct swm_region *r;
5375 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
5376 e->xvisibility.window);
5377 if (e->xvisibility.state == VisibilityUnobscured)
5378 for (i = 0; i < ScreenCount(display); i++)
5379 TAILQ_FOREACH(r, &screens[i].rl, entry)
5380 if (e->xvisibility.window == r->bar_window)
5385 clientmessage(XEvent *e)
5387 XClientMessageEvent *ev;
5392 win = find_window(ev->window);
5396 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx type: %ld \n",
5397 ev->window, ev->message_type);
5399 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
5400 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW \n");
5403 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
5404 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW \n");
5405 if (win->can_delete)
5406 client_msg(win, adelete);
5408 XKillClient(display, win->id);
5410 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
5411 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_MOVERESIZE_WINDOW \n");
5412 if (win->floating) {
5413 if (ev->data.l[0] & (1<<8)) /* x */
5414 win->g.x = ev->data.l[1];
5415 if (ev->data.l[0] & (1<<9)) /* y */
5416 win->g.y = ev->data.l[2];
5417 if (ev->data.l[0] & (1<<10)) /* width */
5418 win->g.w = ev->data.l[3];
5419 if (ev->data.l[0] & (1<<11)) /* height */
5420 win->g.h = ev->data.l[4];
5423 /* TODO: Change stack sizes */
5425 config_win(win, NULL);
5427 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
5428 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE \n");
5429 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
5431 ewmh_update_win_state(win, ev->data.l[2], ev->data.l[0]);
5438 xerror_start(Display *d, XErrorEvent *ee)
5445 xerror(Display *d, XErrorEvent *ee)
5447 /* fprintf(stderr, "error: %p %p\n", display, ee); */
5455 xerrorxlib = XSetErrorHandler(xerror_start);
5457 /* this causes an error if some other window manager is running */
5458 XSelectInput(display, DefaultRootWindow(display),
5459 SubstructureRedirectMask);
5460 XSync(display, False);
5464 XSetErrorHandler(xerror);
5465 XSync(display, False);
5470 new_region(struct swm_screen *s, int x, int y, int w, int h)
5472 struct swm_region *r, *n;
5473 struct workspace *ws = NULL;
5476 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
5477 s->idx, w, h, x, y);
5479 /* remove any conflicting regions */
5480 n = TAILQ_FIRST(&s->rl);
5483 n = TAILQ_NEXT(r, entry);
5484 if (X(r) < (x + w) &&
5485 (X(r) + WIDTH(r)) > x &&
5487 (Y(r) + HEIGHT(r)) > y) {
5488 if (r->ws->r != NULL)
5489 r->ws->old_r = r->ws->r;
5491 XDestroyWindow(display, r->bar_window);
5492 TAILQ_REMOVE(&s->rl, r, entry);
5493 TAILQ_INSERT_TAIL(&s->orl, r, entry);
5497 /* search old regions for one to reuse */
5499 /* size + location match */
5500 TAILQ_FOREACH(r, &s->orl, entry)
5501 if (X(r) == x && Y(r) == y &&
5502 HEIGHT(r) == h && WIDTH(r) == w)
5506 TAILQ_FOREACH(r, &s->orl, entry)
5507 if (HEIGHT(r) == h && WIDTH(r) == w)
5511 TAILQ_REMOVE(&s->orl, r, entry);
5512 /* try to use old region's workspace */
5513 if (r->ws->r == NULL)
5516 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
5517 errx(1, "calloc: failed to allocate memory for screen");
5519 /* if we don't have a workspace already, find one */
5521 for (i = 0; i < SWM_WS_MAX; i++)
5522 if (s->ws[i].r == NULL) {
5529 errx(1, "no free workspaces\n");
5540 TAILQ_INSERT_TAIL(&s->rl, r, entry);
5546 #ifdef SWM_XRR_HAS_CRTC
5548 XRRScreenResources *sr;
5551 #endif /* SWM_XRR_HAS_CRTC */
5552 struct swm_region *r;
5555 if (i >= ScreenCount(display))
5556 errx(1, "invalid screen");
5558 /* remove any old regions */
5559 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
5560 r->ws->old_r = r->ws->r = NULL;
5561 XDestroyWindow(display, r->bar_window);
5562 TAILQ_REMOVE(&screens[i].rl, r, entry);
5563 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
5567 /* map virtual screens onto physical screens */
5568 #ifdef SWM_XRR_HAS_CRTC
5569 if (xrandr_support) {
5570 sr = XRRGetScreenResources(display, screens[i].root);
5572 new_region(&screens[i], 0, 0,
5573 DisplayWidth(display, i),
5574 DisplayHeight(display, i));
5578 for (c = 0, ci = NULL; c < ncrtc; c++) {
5579 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
5580 if (ci->noutput == 0)
5583 if (ci != NULL && ci->mode == None)
5584 new_region(&screens[i], 0, 0,
5585 DisplayWidth(display, i),
5586 DisplayHeight(display, i));
5588 new_region(&screens[i],
5589 ci->x, ci->y, ci->width, ci->height);
5592 XRRFreeCrtcInfo(ci);
5593 XRRFreeScreenResources(sr);
5595 #endif /* SWM_XRR_HAS_CRTC */
5597 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
5598 DisplayHeight(display, i));
5603 screenchange(XEvent *e) {
5604 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
5605 struct swm_region *r;
5608 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
5610 if (!XRRUpdateConfiguration(e))
5613 /* silly event doesn't include the screen index */
5614 for (i = 0; i < ScreenCount(display); i++)
5615 if (screens[i].root == xe->root)
5617 if (i >= ScreenCount(display))
5618 errx(1, "screenchange: screen not found\n");
5620 /* brute force for now, just re-enumerate the regions */
5623 /* add bars to all regions */
5624 for (i = 0; i < ScreenCount(display); i++)
5625 TAILQ_FOREACH(r, &screens[i].rl, entry)
5628 if (focus_mode == SWM_FOCUS_DEFAULT)
5629 drain_enter_notify();
5635 Window d1, d2, *wins = NULL;
5636 XWindowAttributes wa;
5641 for (i = 0; i < ScreenCount(display); i++) {
5642 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
5645 /* attach windows to a region */
5646 /* normal windows */
5647 for (j = 0; j < no; j++) {
5648 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5649 wa.override_redirect ||
5650 XGetTransientForHint(display, wins[j], &d1))
5653 state = getstate(wins[j]);
5654 manage = state == IconicState;
5655 if (wa.map_state == IsViewable || manage)
5656 manage_window(wins[j]);
5658 /* transient windows */
5659 for (j = 0; j < no; j++) {
5660 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5661 wa.override_redirect)
5664 state = getstate(wins[j]);
5665 manage = state == IconicState;
5666 if (XGetTransientForHint(display, wins[j], &d1) &&
5668 manage_window(wins[j]);
5681 int errorbase, major, minor;
5682 struct workspace *ws;
5685 if ((screens = calloc(ScreenCount(display),
5686 sizeof(struct swm_screen))) == NULL)
5687 errx(1, "calloc: screens");
5689 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
5691 /* initial Xrandr setup */
5692 xrandr_support = XRRQueryExtension(display,
5693 &xrandr_eventbase, &errorbase);
5695 if (XRRQueryVersion(display, &major, &minor) && major < 1)
5698 /* map physical screens */
5699 for (i = 0; i < ScreenCount(display); i++) {
5700 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
5702 TAILQ_INIT(&screens[i].rl);
5703 TAILQ_INIT(&screens[i].orl);
5704 screens[i].root = RootWindow(display, i);
5706 /* set default colors */
5707 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
5708 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
5709 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
5710 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
5711 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
5713 /* set default cursor */
5714 XDefineCursor(display, screens[i].root,
5715 XCreateFontCursor(display, XC_left_ptr));
5717 /* init all workspaces */
5718 /* XXX these should be dynamically allocated too */
5719 for (j = 0; j < SWM_WS_MAX; j++) {
5720 ws = &screens[i].ws[j];
5725 TAILQ_INIT(&ws->winlist);
5726 TAILQ_INIT(&ws->unmanagedlist);
5728 for (k = 0; layouts[k].l_stack != NULL; k++)
5729 if (layouts[k].l_config != NULL)
5730 layouts[k].l_config(ws,
5731 SWM_ARG_ID_STACKINIT);
5732 ws->cur_layout = &layouts[0];
5738 XRRSelectInput(display, screens[i].root,
5739 RRScreenChangeNotifyMask);
5746 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
5748 err(1, "setup_globals: strdup");
5749 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
5751 err(1, "setup_globals: strdup");
5752 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
5754 err(1, "setup_globals: strdup");
5755 if ((spawn_term[0] = strdup("xterm")) == NULL)
5756 err(1, "setup_globals: strdup");
5757 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
5765 Atom netwmcheck, netwmname, utf8_string;
5768 /* work around sun jdk bugs, code from wmname */
5769 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
5770 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
5771 utf8_string = XInternAtom(display, "UTF8_STRING", False);
5772 for (i = 0; i < ScreenCount(display); i++) {
5773 root = screens[i].root;
5774 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
5775 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
5776 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
5778 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
5779 PropModeReplace, (unsigned char *)&win,1);
5780 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
5781 PropModeReplace, (unsigned char *)&win,1);
5782 XChangeProperty(display, win, netwmname, utf8_string, 8,
5783 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
5788 main(int argc, char *argv[])
5791 struct swm_region *r, *rr;
5792 struct ws_win *winfocus = NULL;
5795 char conf[PATH_MAX], *cfile = NULL;
5800 struct sigaction sact;
5803 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
5804 SWM_VERSION, cvstag);
5805 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
5806 warnx("no locale support");
5808 if (!(display = XOpenDisplay(0)))
5809 errx(1, "can not open display");
5812 errx(1, "other wm running");
5814 /* handle some signals */
5815 bzero(&sact, sizeof(sact));
5816 sigemptyset(&sact.sa_mask);
5818 sact.sa_handler = sighdlr;
5819 sigaction(SIGINT, &sact, NULL);
5820 sigaction(SIGQUIT, &sact, NULL);
5821 sigaction(SIGTERM, &sact, NULL);
5822 sigaction(SIGHUP, &sact, NULL);
5824 sact.sa_handler = sighdlr;
5825 sact.sa_flags = SA_NOCLDSTOP;
5826 sigaction(SIGCHLD, &sact, NULL);
5828 astate = XInternAtom(display, "WM_STATE", False);
5829 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
5830 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
5831 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
5832 a_wmname = XInternAtom(display, "WM_NAME", False);
5833 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
5834 a_string = XInternAtom(display, "STRING", False);
5835 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
5837 /* look for local and global conf file */
5838 pwd = getpwuid(getuid());
5840 errx(1, "invalid user %d", getuid());
5849 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
5850 if (stat(conf, &sb) != -1) {
5851 if (S_ISREG(sb.st_mode))
5854 /* try global conf file */
5855 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
5856 if (!stat(conf, &sb))
5857 if (S_ISREG(sb.st_mode))
5864 /* set some values to work around bad programs */
5867 /* grab existing windows (before we build the bars) */
5870 /* setup all bars */
5871 for (i = 0; i < ScreenCount(display); i++)
5872 TAILQ_FOREACH(r, &screens[i].rl, entry) {
5873 if (winfocus == NULL)
5874 winfocus = TAILQ_FIRST(&r->ws->winlist);
5882 if (focus_mode == SWM_FOCUS_DEFAULT)
5883 drain_enter_notify();
5885 xfd = ConnectionNumber(display);
5887 while (XPending(display)) {
5888 XNextEvent(display, &e);
5891 if (e.type < LASTEvent) {
5893 if (handler[e.type])
5894 handler[e.type](&e);
5896 DNPRINTF(SWM_D_EVENT,
5897 "win: %lu unknown event: %d\n",
5898 e.xany.window, e.type);
5900 switch (e.type - xrandr_eventbase) {
5901 case RRScreenChangeNotify:
5905 DNPRINTF(SWM_D_EVENT,
5906 "win: %lu unknown xrandr event: "
5907 "%d\n", e.xany.window, e.type);
5913 /* if we are being restarted go focus on first window */
5915 rr = winfocus->ws->r;
5917 /* not a visible window */
5921 /* move pointer to first screen if multi screen */
5922 if (ScreenCount(display) > 1 || outputs > 1)
5923 XWarpPointer(display, None, rr->s[0].root,
5924 0, 0, 0, 0, rr->g.x,
5925 rr->g.y + (bar_enabled ? bar_height : 0));
5927 a.id = SWM_ARG_ID_FOCUSCUR;
5937 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
5939 DNPRINTF(SWM_D_MISC, "select failed");
5940 if (restart_wm == 1)
5941 restart(NULL, NULL);
5942 if (search_resp == 1)
5954 XFreeGC(display, bar_gc);
5955 XCloseDisplay(display);