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 =
57 #define SWM_VERSION "0.9.30"
74 #include <sys/types.h>
78 #include <sys/queue.h>
79 #include <sys/param.h>
80 #include <sys/select.h>
82 #include <X11/cursorfont.h>
83 #include <X11/keysym.h>
84 #include <X11/Xatom.h>
86 #include <X11/Xproto.h>
87 #include <X11/Xutil.h>
88 #include <X11/extensions/Xrandr.h>
95 # error XRandR versions less than 1.0 are not supported
100 #define SWM_XRR_HAS_CRTC
104 /*#define SWM_DEBUG*/
106 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
107 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
108 #define SWM_D_MISC 0x0001
109 #define SWM_D_EVENT 0x0002
110 #define SWM_D_WS 0x0004
111 #define SWM_D_FOCUS 0x0008
112 #define SWM_D_MOVE 0x0010
113 #define SWM_D_STACK 0x0020
114 #define SWM_D_MOUSE 0x0040
115 #define SWM_D_PROP 0x0080
116 #define SWM_D_CLASS 0x0100
117 #define SWM_D_KEY 0x0200
118 #define SWM_D_QUIRK 0x0400
119 #define SWM_D_SPAWN 0x0800
120 #define SWM_D_EVENTQ 0x1000
121 #define SWM_D_CONF 0x2000
123 u_int32_t swm_debug = 0
140 #define DPRINTF(x...)
141 #define DNPRINTF(n,x...)
144 #define LENGTH(x) (sizeof x / sizeof x[0])
145 #define MODKEY Mod1Mask
146 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
147 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
148 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
149 #define SWM_PROPLEN (16)
150 #define SWM_FUNCNAME_LEN (32)
151 #define SWM_KEYS_LEN (255)
152 #define SWM_QUIRK_LEN (64)
153 #define X(r) (r)->g.x
154 #define Y(r) (r)->g.y
155 #define WIDTH(r) (r)->g.w
156 #define HEIGHT(r) (r)->g.h
157 #define SWM_MAX_FONT_STEPS (3)
158 #define WINID(w) (w ? w->id : 0)
160 #define SWM_FOCUS_DEFAULT (0)
161 #define SWM_FOCUS_SYNERGY (1)
162 #define SWM_FOCUS_FOLLOW (2)
165 #define SWM_LIB "/usr/local/lib/libswmhack.so"
177 volatile sig_atomic_t running = 1;
178 volatile sig_atomic_t restart_wm = 0;
180 int last_focus_event = FocusOut;
181 int (*xerrorxlib)(Display *, XErrorEvent *);
185 int xrandr_eventbase;
186 unsigned int numlockmask = 0;
190 int cycle_visible = 0;
192 int font_adjusted = 0;
193 unsigned int mod_key = MODKEY;
196 struct swm_region *search_r;
197 int select_list_pipe[2];
198 int select_resp_pipe[2];
200 volatile sig_atomic_t search_resp;
203 double dialog_ratio = .6;
205 #define SWM_BAR_MAX (256)
206 char *bar_argv[] = { NULL, NULL };
208 char bar_ext[SWM_BAR_MAX];
209 char bar_vertext[SWM_BAR_MAX];
211 sig_atomic_t bar_alarm = 0;
214 int bar_border_width = 1;
215 int bar_at_bottom = 0;
217 int bar_extra_running = 0;
220 int stack_enabled = 1;
221 int clock_enabled = 1;
222 char *clock_format = NULL;
223 int title_name_enabled = 0;
224 int title_class_enabled = 0;
225 int window_name_enabled = 0;
226 int focus_mode = SWM_FOCUS_DEFAULT;
227 int disable_border = 0;
228 int border_width = 1;
234 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
235 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
237 #define SWM_MENU_FN (2)
238 #define SWM_MENU_NB (4)
239 #define SWM_MENU_NF (6)
240 #define SWM_MENU_SB (8)
241 #define SWM_MENU_SF (10)
243 /* layout manager data */
244 struct swm_geometry {
254 /* virtual "screens" */
256 TAILQ_ENTRY(swm_region) entry;
257 struct swm_geometry g;
258 struct workspace *ws; /* current workspace on this region */
259 struct workspace *ws_prior; /* prior workspace on this region */
260 struct swm_screen *s; /* screen idx */
263 TAILQ_HEAD(swm_region_list, swm_region);
266 TAILQ_ENTRY(ws_win) entry;
269 struct ws_win *child_trans; /* transient child window */
270 struct swm_geometry g; /* current geometry */
271 struct swm_geometry g_float; /* geometry when floating */
272 struct swm_geometry rg_float; /* region geom when floating */
273 int g_floatvalid; /* flag: geometry in g_float is valid */
274 int floatmaxed; /* flag: floater was maxed in max_stack */
278 unsigned int ewmh_flags;
279 int font_size_boundary[SWM_MAX_FONT_STEPS];
285 unsigned long quirks;
286 struct workspace *ws; /* always valid */
287 struct swm_screen *s; /* always valid, never changes */
288 XWindowAttributes wa;
293 TAILQ_HEAD(ws_win_list, ws_win);
297 TAILQ_ENTRY(pid_e) entry;
301 TAILQ_HEAD(pid_list, pid_e);
302 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
304 /* layout handlers */
306 void vertical_config(struct workspace *, int);
307 void vertical_stack(struct workspace *, struct swm_geometry *);
308 void horizontal_config(struct workspace *, int);
309 void horizontal_stack(struct workspace *, struct swm_geometry *);
310 void max_stack(struct workspace *, struct swm_geometry *);
312 struct ws_win *find_window(Window);
314 void grabbuttons(struct ws_win *, int);
315 void new_region(struct swm_screen *, int, int, int, int);
316 void unmanage_window(struct ws_win *);
317 long getstate(Window);
320 void (*l_stack)(struct workspace *, struct swm_geometry *);
321 void (*l_config)(struct workspace *, int);
323 #define SWM_L_FOCUSPREV (1<<0)
324 #define SWM_L_MAPONFOCUS (1<<1)
327 /* stack, configure */
328 { vertical_stack, vertical_config, 0, "[|]" },
329 { horizontal_stack, horizontal_config, 0, "[-]" },
331 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, "[ ]" },
332 { NULL, NULL, 0, NULL },
335 /* position of max_stack mode in the layouts array */
336 #define SWM_MAX_STACK 2
338 #define SWM_H_SLICE (32)
339 #define SWM_V_SLICE (32)
341 /* define work spaces */
343 int idx; /* workspace index */
344 struct layout *cur_layout; /* current layout handlers */
345 struct ws_win *focus; /* may be NULL */
346 struct ws_win *focus_prev; /* may be NULL */
347 struct swm_region *r; /* may be NULL */
348 struct swm_region *old_r; /* may be NULL */
349 struct ws_win_list winlist; /* list of windows in ws */
350 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
354 int horizontal_msize;
356 int horizontal_stacks;
363 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
364 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
366 /* physical screen mapping */
367 #define SWM_WS_MAX (10)
369 int idx; /* screen index */
370 struct swm_region_list rl; /* list of regions on this screen */
371 struct swm_region_list orl; /* list of old regions */
373 struct workspace ws[SWM_WS_MAX];
379 } c[SWM_S_COLOR_MAX];
381 struct swm_screen *screens;
384 /* args to functions */
387 #define SWM_ARG_ID_FOCUSNEXT (0)
388 #define SWM_ARG_ID_FOCUSPREV (1)
389 #define SWM_ARG_ID_FOCUSMAIN (2)
390 #define SWM_ARG_ID_FOCUSCUR (4)
391 #define SWM_ARG_ID_SWAPNEXT (10)
392 #define SWM_ARG_ID_SWAPPREV (11)
393 #define SWM_ARG_ID_SWAPMAIN (12)
394 #define SWM_ARG_ID_MOVELAST (13)
395 #define SWM_ARG_ID_MASTERSHRINK (20)
396 #define SWM_ARG_ID_MASTERGROW (21)
397 #define SWM_ARG_ID_MASTERADD (22)
398 #define SWM_ARG_ID_MASTERDEL (23)
399 #define SWM_ARG_ID_STACKRESET (30)
400 #define SWM_ARG_ID_STACKINIT (31)
401 #define SWM_ARG_ID_CYCLEWS_UP (40)
402 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
403 #define SWM_ARG_ID_CYCLESC_UP (42)
404 #define SWM_ARG_ID_CYCLESC_DOWN (43)
405 #define SWM_ARG_ID_STACKINC (50)
406 #define SWM_ARG_ID_STACKDEC (51)
407 #define SWM_ARG_ID_SS_ALL (60)
408 #define SWM_ARG_ID_SS_WINDOW (61)
409 #define SWM_ARG_ID_DONTCENTER (70)
410 #define SWM_ARG_ID_CENTER (71)
411 #define SWM_ARG_ID_KILLWINDOW (80)
412 #define SWM_ARG_ID_DELETEWINDOW (81)
416 void focus(struct swm_region *, union arg *);
417 void focus_magic(struct ws_win *);
424 #define SWM_Q_FLOAT (1<<0) /* float this window */
425 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
426 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
427 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
428 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
429 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
431 int quirks_size = 0, quirks_length = 0;
432 struct quirk *quirks = NULL;
435 * Supported EWMH hints should be added to
436 * both the enum and the ewmh array
438 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
439 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
440 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
441 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
442 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
443 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
444 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
445 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
446 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
447 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
453 } ewmh[SWM_EWMH_HINT_MAX] = {
454 /* must be in same order as in the enum */
455 {"_NET_ACTIVE_WINDOW", None},
456 {"_NET_MOVERESIZE_WINDOW", None},
457 {"_NET_CLOSE_WINDOW", None},
458 {"_NET_WM_WINDOW_TYPE", None},
459 {"_NET_WM_WINDOW_TYPE_DOCK", None},
460 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
461 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
462 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
463 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
464 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
465 {"_NET_WM_STATE", None},
466 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
467 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
468 {"_NET_WM_STATE_SKIP_TASKBAR", None},
469 {"_NET_WM_STATE_SKIP_PAGER", None},
470 {"_NET_WM_STATE_HIDDEN", None},
471 {"_NET_WM_STATE_ABOVE", None},
472 {"_SWM_WM_STATE_MANUAL", None},
473 {"_NET_WM_STATE_FULLSCREEN", None},
474 {"_NET_WM_ALLOWED_ACTIONS", None},
475 {"_NET_WM_ACTION_MOVE", None},
476 {"_NET_WM_ACTION_RESIZE", None},
477 {"_NET_WM_ACTION_FULLSCREEN", None},
478 {"_NET_WM_ACTION_CLOSE", None},
481 void store_float_geom(struct ws_win *win, struct swm_region *r);
482 int floating_toggle_win(struct ws_win *win);
483 void spawn_select(struct swm_region *, union arg *, char *, int *);
486 get_property(Window id, Atom atom, long count, Atom type,
487 unsigned long *n, unsigned char **data)
490 unsigned long tmp, extra;
491 unsigned long *nitems;
494 nitems = n != NULL ? n : &tmp;
495 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
496 &real, &format, nitems, &extra, data);
498 if (status != Success)
507 update_iconic(struct ws_win *win, int newv)
514 iprop = XInternAtom(display, "_SWM_ICONIC", False);
518 XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
519 PropModeReplace, (unsigned char *)&v, 1);
521 XDeleteProperty(display, win->id, iprop);
525 get_iconic(struct ws_win *win)
530 unsigned long nitems, extra;
531 unsigned char *prop = NULL;
533 iprop = XInternAtom(display, "_SWM_ICONIC", False);
536 status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
537 False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
538 if (status != Success)
540 if (rettype != XA_INTEGER || retfmt != 32)
544 v = *((int32_t *)prop);
558 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
560 for (i = 0; i < LENGTH(ewmh); i++)
561 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
563 for (i = 0; i < ScreenCount(display); i++) {
564 /* Support check window will be created by workaround(). */
566 /* Report supported atoms */
567 XDeleteProperty(display, screens[i].root, sup_list);
568 for (j = 0; j < LENGTH(ewmh); j++)
569 XChangeProperty(display, screens[i].root,
570 sup_list, XA_ATOM, 32,
571 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
579 unsigned char *data = NULL;
581 Atom sup_check, sup_list;
584 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
585 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
587 for (i = 0; i < ScreenCount(display); i++) {
588 /* Get the support check window and destroy it */
589 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
594 XDestroyWindow(display, id);
595 XDeleteProperty(display, screens[i].root, sup_check);
596 XDeleteProperty(display, screens[i].root, sup_list);
604 ewmh_autoquirk(struct ws_win *win)
607 unsigned long *data = NULL;
611 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
612 XA_ATOM, &n, (unsigned char **)&data);
619 for (i = 0; i < n; i++) {
621 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
623 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
624 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
625 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
627 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
630 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
631 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
633 win->quirks = SWM_Q_FLOAT;
641 #define SWM_EWMH_ACTION_COUNT_MAX (6)
642 #define EWMH_F_FULLSCREEN (1<<0)
643 #define EWMH_F_ABOVE (1<<1)
644 #define EWMH_F_HIDDEN (1<<2)
645 #define EWMH_F_SKIP_PAGER (1<<3)
646 #define EWMH_F_SKIP_TASKBAR (1<<4)
647 #define SWM_F_MANUAL (1<<5)
650 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
652 struct swm_geometry rg;
660 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
666 store_float_geom(win, win->ws->r);
673 if (win->g_floatvalid) {
674 /* refloat at last floating relative position */
675 win->g.x = win->g_float.x - win->rg_float.x + rg.x;
676 win->g.y = win->g_float.y - win->rg_float.y + rg.y;
677 win->g.w = win->g_float.w;
678 win->g.h = win->g_float.h;
686 ewmh_update_actions(struct ws_win *win)
688 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
694 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
697 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
698 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
701 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
702 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
705 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
706 #define _NET_WM_STATE_ADD 1 /* add/set property */
707 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
710 ewmh_update_win_state(struct ws_win *win, long state, long action)
712 unsigned int mask = 0;
713 unsigned int changed = 0;
714 unsigned int orig_flags;
719 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
720 mask = EWMH_F_FULLSCREEN;
721 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
723 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
725 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
726 mask = EWMH_F_SKIP_PAGER;
727 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
728 mask = EWMH_F_SKIP_TASKBAR;
731 orig_flags = win->ewmh_flags;
734 case _NET_WM_STATE_REMOVE:
735 win->ewmh_flags &= ~mask;
737 case _NET_WM_STATE_ADD:
738 win->ewmh_flags |= mask;
740 case _NET_WM_STATE_TOGGLE:
741 win->ewmh_flags ^= mask;
745 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
747 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
749 if (!floating_toggle_win(win))
750 win->ewmh_flags = orig_flags; /* revert */
751 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
753 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
754 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
756 if (!ewmh_set_win_fullscreen(win,
757 win->ewmh_flags & EWMH_F_FULLSCREEN))
758 win->ewmh_flags = orig_flags; /* revert */
760 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
762 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
763 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
764 XA_ATOM, 32, PropModeAppend,
765 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
766 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
767 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
768 XA_ATOM, 32, PropModeAppend,
769 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
770 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
771 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
772 XA_ATOM, 32, PropModeAppend,
773 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
774 if (win->ewmh_flags & EWMH_F_ABOVE)
775 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
776 XA_ATOM, 32, PropModeAppend,
777 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
778 if (win->ewmh_flags & SWM_F_MANUAL)
779 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
780 XA_ATOM, 32, PropModeAppend,
781 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
785 ewmh_get_win_state(struct ws_win *win)
796 win->ewmh_flags |= EWMH_F_ABOVE;
798 win->ewmh_flags |= SWM_F_MANUAL;
800 success = get_property(win->id, ewmh[_NET_WM_STATE].atom,
801 (~0L), XA_ATOM, &n, (unsigned char **)&states);
806 for (i = 0; i < n; i++)
807 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
827 name = "ButtonPress";
830 name = "ButtonRelease";
833 name = "MotionNotify";
836 name = "EnterNotify";
839 name = "LeaveNotify";
848 name = "KeymapNotify";
854 name = "GraphicsExpose";
859 case VisibilityNotify:
860 name = "VisibilityNotify";
863 name = "CreateNotify";
866 name = "DestroyNotify";
869 name = "UnmapNotify";
878 name = "ReparentNotify";
880 case ConfigureNotify:
881 name = "ConfigureNotify";
883 case ConfigureRequest:
884 name = "ConfigureRequest";
887 name = "GravityNotify";
890 name = "ResizeRequest";
892 case CirculateNotify:
893 name = "CirculateNotify";
895 case CirculateRequest:
896 name = "CirculateRequest";
899 name = "PropertyNotify";
902 name = "SelectionClear";
904 case SelectionRequest:
905 name = "SelectionRequest";
907 case SelectionNotify:
908 name = "SelectionNotify";
911 name = "ColormapNotify";
914 name = "ClientMessage";
917 name = "MappingNotify";
922 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
924 e->xany.window, name, e->type, QLength(display));
926 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
928 e->xany.window, e->type, QLength(display));
932 dumpwins(struct swm_region *r, union arg *args)
936 XWindowAttributes wa;
939 fprintf(stderr, "invalid workspace\n");
943 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
945 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
946 state = getstate(win->id);
947 if (!XGetWindowAttributes(display, win->id, &wa))
948 fprintf(stderr, "window: %lu failed "
949 "XGetWindowAttributes\n", win->id);
950 fprintf(stderr, "window: %lu map_state: %d state: %d "
952 win->id, wa.map_state, state, win->transient);
955 fprintf(stderr, "===== unmanaged window list =====\n");
956 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
957 state = getstate(win->id);
958 if (!XGetWindowAttributes(display, win->id, &wa))
959 fprintf(stderr, "window: %lu failed "
960 "XGetWindowAttributes\n", win->id);
961 fprintf(stderr, "window: %lu map_state: %d state: %d "
963 win->id, wa.map_state, state, win->transient);
966 fprintf(stderr, "=================================\n");
971 dumpwins(struct swm_region *r, union arg *args)
974 #endif /* SWM_DEBUG */
976 void expose(XEvent *);
977 void keypress(XEvent *);
978 void buttonpress(XEvent *);
979 void configurerequest(XEvent *);
980 void configurenotify(XEvent *);
981 void destroynotify(XEvent *);
982 void enternotify(XEvent *);
983 void focusevent(XEvent *);
984 void mapnotify(XEvent *);
985 void mappingnotify(XEvent *);
986 void maprequest(XEvent *);
987 void propertynotify(XEvent *);
988 void unmapnotify(XEvent *);
989 void visibilitynotify(XEvent *);
990 void clientmessage(XEvent *);
992 void (*handler[LASTEvent])(XEvent *) = {
994 [KeyPress] = keypress,
995 [ButtonPress] = buttonpress,
996 [ConfigureRequest] = configurerequest,
997 [ConfigureNotify] = configurenotify,
998 [DestroyNotify] = destroynotify,
999 [EnterNotify] = enternotify,
1000 [FocusIn] = focusevent,
1001 [FocusOut] = focusevent,
1002 [MapNotify] = mapnotify,
1003 [MappingNotify] = mappingnotify,
1004 [MapRequest] = maprequest,
1005 [PropertyNotify] = propertynotify,
1006 [UnmapNotify] = unmapnotify,
1007 [VisibilityNotify] = visibilitynotify,
1008 [ClientMessage] = clientmessage,
1014 int saved_errno, status;
1017 saved_errno = errno;
1021 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1026 if (errno != ECHILD)
1027 warn("sighdlr: waitpid");
1028 #endif /* SWM_DEBUG */
1031 if (pid == searchpid)
1035 if (WIFEXITED(status)) {
1036 if (WEXITSTATUS(status) != 0)
1037 warnx("sighdlr: child exit status: %d",
1038 WEXITSTATUS(status));
1040 warnx("sighdlr: child is terminated "
1042 #endif /* SWM_DEBUG */
1056 errno = saved_errno;
1062 struct pid_e *p = NULL;
1064 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1066 TAILQ_FOREACH(p, &pidlist, entry) {
1075 name_to_color(char *colorname)
1079 XColor screen_def, exact_def;
1080 unsigned long result = 0;
1081 char cname[32] = "#";
1083 cmap = DefaultColormap(display, screens[0].idx);
1084 status = XAllocNamedColor(display, cmap, colorname,
1085 &screen_def, &exact_def);
1087 strlcat(cname, colorname + 2, sizeof cname - 1);
1088 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1092 result = screen_def.pixel;
1094 fprintf(stderr, "color '%s' not found.\n", colorname);
1100 setscreencolor(char *val, int i, int c)
1102 if (i > 0 && i <= ScreenCount(display)) {
1103 screens[i - 1].c[c].color = name_to_color(val);
1104 free(screens[i - 1].c[c].name);
1105 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1107 } else if (i == -1) {
1108 for (i = 0; i < ScreenCount(display); i++) {
1109 screens[i].c[c].color = name_to_color(val);
1110 free(screens[i].c[c].name);
1111 if ((screens[i].c[c].name = strdup(val)) == NULL)
1115 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1116 i, ScreenCount(display));
1120 custom_region(char *val)
1122 unsigned int sidx, x, y, w, h;
1124 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1125 errx(1, "invalid custom region, "
1126 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
1127 if (sidx < 1 || sidx > ScreenCount(display))
1128 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1129 sidx, ScreenCount(display));
1133 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
1135 if (x < 0 || x > DisplayWidth(display, sidx) ||
1136 y < 0 || y > DisplayHeight(display, sidx) ||
1137 w + x > DisplayWidth(display, sidx) ||
1138 h + y > DisplayHeight(display, sidx)) {
1139 fprintf(stderr, "ignoring region %ux%u+%u+%u "
1140 "- not within screen boundaries "
1141 "(%ux%u)\n", w, h, x, y,
1142 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1146 new_region(&screens[sidx], x, y, w, h);
1150 socket_setnonblock(int fd)
1154 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1155 err(1, "fcntl F_GETFL");
1156 flags |= O_NONBLOCK;
1157 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1158 err(1, "fcntl F_SETFL");
1162 bar_print(struct swm_region *r, char *s)
1164 XClearWindow(display, r->bar_window);
1165 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
1166 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
1171 bar_extra_stop(void)
1175 bzero(bar_pipe, sizeof bar_pipe);
1178 kill(bar_pid, SIGTERM);
1181 strlcpy(bar_ext, "", sizeof bar_ext);
1186 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1188 int do_class, do_name;
1190 XClassHint *xch = NULL;
1192 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
1193 cur_focus != NULL) {
1194 if ((xch = XAllocClassHint()) == NULL)
1196 status = XGetClassHint(display, cur_focus->id, xch);
1197 if (status == BadWindow || status == BadAlloc)
1199 do_class = (title_class_enabled && xch->res_class != NULL);
1200 do_name = (title_name_enabled && xch->res_name != NULL);
1202 strlcat(s, xch->res_class, sz);
1203 if (do_class && do_name)
1204 strlcat(s, ":", sz);
1206 strlcat(s, xch->res_name, sz);
1207 strlcat(s, " ", sz);
1215 bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1219 if (window_name_enabled && cur_focus != NULL) {
1220 XFetchName(display, cur_focus->id, &title);
1222 if (cur_focus->floating)
1223 strlcat(s, "(f) ", sz);
1224 strlcat(s, title, sz);
1225 strlcat(s, " ", sz);
1236 struct swm_region *r;
1239 char s[SWM_BAR_MAX];
1240 char cn[SWM_BAR_MAX];
1241 char loc[SWM_BAR_MAX];
1245 if (bar_enabled == 0)
1247 if (bar_extra && bar_extra_running) {
1248 /* ignore short reads; it'll correct itself */
1249 while ((b = fgetln(stdin, &len)) != NULL)
1250 if (b && b[len - 1] == '\n') {
1252 strlcpy(bar_ext, b, sizeof bar_ext);
1254 if (b == NULL && errno != EAGAIN) {
1255 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
1256 errno, strerror(errno));
1260 strlcpy(bar_ext, "", sizeof bar_ext);
1262 if (clock_enabled == 0)
1263 strlcpy(s, "", sizeof s);
1266 localtime_r(&tmt, &tm);
1267 strftime(s, sizeof s, clock_format, &tm);
1268 strlcat(s, " ", sizeof s);
1271 for (i = 0; i < ScreenCount(display); i++) {
1273 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1274 strlcpy(cn, "", sizeof cn);
1276 bar_class_name(cn, sizeof cn, r->ws->focus);
1277 bar_window_name(cn, sizeof cn, r->ws->focus);
1281 stack = r->ws->cur_layout->name;
1283 snprintf(loc, sizeof loc, "%d:%d %s %s%s %s %s",
1284 x++, r->ws->idx + 1, stack, s, cn, bar_ext,
1299 bar_toggle(struct swm_region *r, union arg *args)
1301 struct swm_region *tmpr;
1302 int i, sc = ScreenCount(display);
1304 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
1307 for (i = 0; i < sc; i++)
1308 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1309 XUnmapWindow(display, tmpr->bar_window);
1311 for (i = 0; i < sc; i++)
1312 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1313 XMapRaised(display, tmpr->bar_window);
1315 bar_enabled = !bar_enabled;
1318 /* must be after stack */
1325 XSetWindowAttributes wa;
1326 struct swm_region *r;
1329 /* do this here because the conf file is in memory */
1330 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1331 /* launch external status app */
1332 bar_extra_running = 1;
1333 if (pipe(bar_pipe) == -1)
1334 err(1, "pipe error");
1335 socket_setnonblock(bar_pipe[0]);
1336 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1337 if (dup2(bar_pipe[0], 0) == -1)
1339 if (dup2(bar_pipe[1], 1) == -1)
1341 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1342 err(1, "could not disable SIGPIPE");
1343 switch (bar_pid = fork()) {
1345 err(1, "cannot fork");
1349 execvp(bar_argv[0], bar_argv);
1350 err(1, "%s external app failed", bar_argv[0]);
1352 default: /* parent */
1358 bzero(&wa, sizeof wa);
1359 for (i = 0; i < ScreenCount(display); i++)
1360 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1362 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1363 wa.background_pixel =
1364 screens[i].c[SWM_S_COLOR_BAR].color;
1365 XChangeWindowAttributes(display, r->bar_window,
1366 CWBackPixel | CWBorderPixel, &wa);
1372 bar_setup(struct swm_region *r)
1377 XFreeFont(display, bar_fs);
1381 for (i = 0; bar_fonts[i] != NULL; i++) {
1382 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
1388 if (bar_fonts[i] == NULL)
1389 errx(1, "couldn't load font");
1391 errx(1, "couldn't create font structure");
1393 bar_height = bar_fs->ascent + bar_fs->descent + 1 +
1394 2 * bar_border_width;
1396 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1398 r->bar_window = XCreateSimpleWindow(display,
1399 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width,
1400 bar_height - 2 * bar_border_width,
1401 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1402 r->s->c[SWM_S_COLOR_BAR].color);
1403 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
1404 XSetFont(display, bar_gc, bar_fs->fid);
1405 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1407 XMapRaised(display, r->bar_window);
1408 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
1410 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1411 err(1, "could not install bar_signal");
1416 drain_enter_notify(void)
1421 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1424 DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained %d\n", i);
1428 set_win_state(struct ws_win *win, long state)
1430 long data[] = {state, None};
1432 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1437 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1438 (unsigned char *)data, 2);
1445 unsigned char *p = NULL;
1448 if (!get_property(w, astate, 2L, astate, &n, &p))
1451 result = *((long *)p);
1457 version(struct swm_region *r, union arg *args)
1459 bar_version = !bar_version;
1461 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
1462 SWM_VERSION, cvstag);
1464 strlcpy(bar_vertext, "", sizeof bar_vertext);
1469 client_msg(struct ws_win *win, Atom a)
1471 XClientMessageEvent cm;
1476 bzero(&cm, sizeof cm);
1477 cm.type = ClientMessage;
1478 cm.window = win->id;
1479 cm.message_type = aprot;
1482 cm.data.l[1] = CurrentTime;
1483 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1487 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1495 DNPRINTF(SWM_D_MISC,
1496 "config_win: win %lu x %d y %d w %d h %d\n",
1497 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1499 ce.type = ConfigureNotify;
1500 ce.display = display;
1502 ce.window = win->id;
1505 ce.width = win->g.w;
1506 ce.height = win->g.h;
1507 ce.border_width = border_width;
1509 ce.override_redirect = False;
1511 DNPRINTF(SWM_D_MISC,
1512 "config_win: ev win %lu x %d y %d w %d h %d\n",
1513 ev->window, ev->x, ev->y, ev->width, ev->height);
1514 ce.type = ConfigureNotify;
1515 ce.display = ev->display;
1516 ce.event = ev->window;
1517 ce.window = ev->window;
1520 ce.width = ev->width;
1521 ce.height = ev->height;
1522 ce.border_width = ev->border_width;
1523 ce.above = ev->above;
1524 ce.override_redirect = False;
1527 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1531 count_win(struct workspace *ws, int count_transient)
1536 TAILQ_FOREACH(win, &ws->winlist, entry) {
1537 if (count_transient == 0 && win->floating)
1539 if (count_transient == 0 && win->transient)
1545 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1551 quit(struct swm_region *r, union arg *args)
1553 DNPRINTF(SWM_D_MISC, "quit\n");
1558 unmap_window(struct ws_win *win)
1563 /* don't unmap again */
1564 if (getstate(win->id) == IconicState)
1567 set_win_state(win, IconicState);
1569 XUnmapWindow(display, win->id);
1570 XSetWindowBorder(display, win->id,
1571 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1580 for (i = 0; i < ScreenCount(display); i++)
1581 for (j = 0; j < SWM_WS_MAX; j++)
1582 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1587 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1594 event.display = display; /* Ignored, but what the hell */
1595 event.window = win->id;
1596 event.root = win->s->root;
1597 event.subwindow = None;
1598 event.time = CurrentTime;
1603 event.same_screen = True;
1604 event.keycode = XKeysymToKeycode(display, keysym);
1605 event.state = modifiers;
1607 event.type = KeyPress;
1608 XSendEvent(event.display, event.window, True,
1609 KeyPressMask, (XEvent *)&event);
1611 event.type = KeyRelease;
1612 XSendEvent(event.display, event.window, True,
1613 KeyPressMask, (XEvent *)&event);
1618 restart(struct swm_region *r, union arg *args)
1620 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1622 /* disable alarm because the following code may not be interrupted */
1624 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1625 errx(1, "can't disable alarm");
1630 XCloseDisplay(display);
1631 execvp(start_argv[0], start_argv);
1632 fprintf(stderr, "execvp failed\n");
1638 root_to_region(Window root)
1640 struct swm_region *r = NULL;
1642 int i, x, y, wx, wy;
1645 for (i = 0; i < ScreenCount(display); i++)
1646 if (screens[i].root == root)
1649 if (XQueryPointer(display, screens[i].root,
1650 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1651 /* choose a region based on pointer location */
1652 TAILQ_FOREACH(r, &screens[i].rl, entry)
1653 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1654 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1659 r = TAILQ_FIRST(&screens[i].rl);
1665 find_unmanaged_window(Window id)
1670 for (i = 0; i < ScreenCount(display); i++)
1671 for (j = 0; j < SWM_WS_MAX; j++)
1672 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1680 find_window(Window id)
1683 Window wrr, wpr, *wcr = NULL;
1687 for (i = 0; i < ScreenCount(display); i++)
1688 for (j = 0; j < SWM_WS_MAX; j++)
1689 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1693 /* if we were looking for the parent return that window instead */
1694 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1699 /* ignore not found and root */
1700 if (wpr == 0 || wrr == wpr)
1703 /* look for parent */
1704 for (i = 0; i < ScreenCount(display); i++)
1705 for (j = 0; j < SWM_WS_MAX; j++)
1706 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1714 spawn(int ws_idx, union arg *args, int close_fd)
1719 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1722 close(ConnectionNumber(display));
1724 setenv("LD_PRELOAD", SWM_LIB, 1);
1726 if (asprintf(&ret, "%d", ws_idx) == -1) {
1730 setenv("_SWM_WS", ret, 1);
1734 if (asprintf(&ret, "%d", getpid()) == -1) {
1738 setenv("_SWM_PID", ret, 1);
1742 if (setsid() == -1) {
1749 * close stdin and stdout to prevent interaction between apps
1750 * and the baraction script
1751 * leave stderr open to record errors
1753 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
1757 dup2(fd, STDIN_FILENO);
1758 dup2(fd, STDOUT_FILENO);
1763 execvp(args->argv[0], args->argv);
1770 spawnterm(struct swm_region *r, union arg *args)
1772 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1776 setenv("_SWM_XTERM_FONTADJ", "", 1);
1777 spawn(r->ws->idx, args, 1);
1782 kill_refs(struct ws_win *win)
1785 struct swm_region *r;
1786 struct workspace *ws;
1791 for (i = 0; i < ScreenCount(display); i++)
1792 TAILQ_FOREACH(r, &screens[i].rl, entry)
1793 for (x = 0; x < SWM_WS_MAX; x++) {
1795 if (win == ws->focus)
1797 if (win == ws->focus_prev)
1798 ws->focus_prev = NULL;
1803 validate_win(struct ws_win *testwin)
1806 struct workspace *ws;
1807 struct swm_region *r;
1808 int i, x, foundit = 0;
1810 if (testwin == NULL)
1813 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1814 TAILQ_FOREACH(r, &screens[i].rl, entry)
1815 for (x = 0; x < SWM_WS_MAX; x++) {
1817 TAILQ_FOREACH(win, &ws->winlist, entry)
1825 validate_ws(struct workspace *testws)
1827 struct swm_region *r;
1828 struct workspace *ws;
1831 /* validate all ws */
1832 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1833 TAILQ_FOREACH(r, &screens[i].rl, entry)
1834 for (x = 0; x < SWM_WS_MAX; x++) {
1843 unfocus_win(struct ws_win *win)
1848 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1852 if (win->ws == NULL)
1855 if (validate_ws(win->ws))
1856 return; /* XXX this gets hit with thunderbird, needs fixing */
1858 if (win->ws->r == NULL)
1861 if (validate_win(win)) {
1866 if (win->ws->focus == win) {
1867 win->ws->focus = NULL;
1868 win->ws->focus_prev = win;
1871 if (validate_win(win->ws->focus)) {
1872 kill_refs(win->ws->focus);
1873 win->ws->focus = NULL;
1875 if (validate_win(win->ws->focus_prev)) {
1876 kill_refs(win->ws->focus_prev);
1877 win->ws->focus_prev = NULL;
1880 /* drain all previous unfocus events */
1881 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
1884 grabbuttons(win, 0);
1885 XSetWindowBorder(display, win->id,
1886 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1888 XChangeProperty(display, win->s->root,
1889 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1890 PropModeReplace, (unsigned char *)&none,1);
1899 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
1901 for (i = 0; i < ScreenCount(display); i++)
1902 for (j = 0; j < SWM_WS_MAX; j++)
1903 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1908 focus_win(struct ws_win *win)
1913 struct ws_win *cfw = NULL;
1916 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1920 if (win->ws == NULL)
1923 if (validate_ws(win->ws))
1924 return; /* XXX this gets hit with thunderbird, needs fixing */
1926 if (validate_win(win)) {
1931 if (validate_win(win)) {
1936 XGetInputFocus(display, &cur_focus, &rr);
1937 if ((cfw = find_window(cur_focus)) != NULL)
1940 /* use larger hammer since the window was killed somehow */
1941 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
1942 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
1943 XSetWindowBorder(display, cfw->id,
1944 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1947 win->ws->focus = win;
1949 if (win->ws->r != NULL) {
1950 /* drain all previous focus events */
1951 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
1955 XSetInputFocus(display, win->id,
1956 RevertToParent, CurrentTime);
1957 XMapRaised(display, win->id);
1958 grabbuttons(win, 1);
1959 XSetWindowBorder(display, win->id,
1960 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1961 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS)
1962 XMapRaised(display, win->id);
1964 XChangeProperty(display, win->s->root,
1965 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1966 PropModeReplace, (unsigned char *)&win->id,1);
1969 if (window_name_enabled)
1974 switchws(struct swm_region *r, union arg *args)
1976 int wsid = args->id, unmap_old = 0;
1977 struct swm_region *this_r, *other_r;
1979 struct workspace *new_ws, *old_ws;
1986 old_ws = this_r->ws;
1987 new_ws = &this_r->s->ws[wsid];
1989 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1990 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1993 if (new_ws == NULL || old_ws == NULL)
1995 if (new_ws == old_ws)
1998 other_r = new_ws->r;
1999 if (other_r == NULL) {
2000 /* the other workspace is hidden, hide this one */
2004 /* the other ws is visible in another region, exchange them */
2005 other_r->ws_prior = new_ws;
2006 other_r->ws = old_ws;
2007 old_ws->r = other_r;
2009 this_r->ws_prior = old_ws;
2010 this_r->ws = new_ws;
2013 /* this is needed so that we can click on a window after a restart */
2017 a.id = SWM_ARG_ID_FOCUSCUR;
2018 focus(new_ws->r, &a);
2022 /* unmap old windows */
2024 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2027 if (focus_mode == SWM_FOCUS_DEFAULT)
2028 drain_enter_notify();
2032 cyclews(struct swm_region *r, union arg *args)
2035 struct swm_screen *s = r->s;
2037 DNPRINTF(SWM_D_WS, "cyclews id %d "
2038 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2039 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2044 case SWM_ARG_ID_CYCLEWS_UP:
2045 if (a.id < SWM_WS_MAX - 1)
2050 case SWM_ARG_ID_CYCLEWS_DOWN:
2054 a.id = SWM_WS_MAX - 1;
2060 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
2062 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2066 } while (a.id != r->ws->idx);
2070 priorws(struct swm_region *r, union arg *args)
2074 DNPRINTF(SWM_D_WS, "priorws id %d "
2075 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2076 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2078 if (r->ws_prior == NULL)
2081 a.id = r->ws_prior->idx;
2086 cyclescr(struct swm_region *r, union arg *args)
2088 struct swm_region *rr = NULL;
2092 /* do nothing if we don't have more than one screen */
2093 if (!(ScreenCount(display) > 1 || outputs > 1))
2098 case SWM_ARG_ID_CYCLESC_UP:
2099 rr = TAILQ_NEXT(r, entry);
2101 rr = TAILQ_FIRST(&screens[i].rl);
2103 case SWM_ARG_ID_CYCLESC_DOWN:
2104 rr = TAILQ_PREV(r, swm_region_list, entry);
2106 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2114 /* move mouse to region */
2116 y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
2117 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2119 a.id = SWM_ARG_ID_FOCUSCUR;
2122 if (rr->ws->focus) {
2123 /* move to focus window */
2124 x = rr->ws->focus->g.x + 1;
2125 y = rr->ws->focus->g.y + 1;
2126 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2131 sort_windows(struct ws_win_list *wl)
2133 struct ws_win *win, *parent, *nxt;
2138 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2139 nxt = TAILQ_NEXT(win, entry);
2140 if (win->transient) {
2141 parent = find_window(win->transient);
2142 if (parent == NULL) {
2143 fprintf(stderr, "not possible bug\n");
2146 TAILQ_REMOVE(wl, win, entry);
2147 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2154 swapwin(struct swm_region *r, union arg *args)
2156 struct ws_win *target, *source;
2157 struct ws_win *cur_focus;
2158 struct ws_win_list *wl;
2161 DNPRINTF(SWM_D_WS, "swapwin id %d "
2162 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
2163 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2165 cur_focus = r->ws->focus;
2166 if (cur_focus == NULL)
2170 wl = &source->ws->winlist;
2173 case SWM_ARG_ID_SWAPPREV:
2174 if (source->transient)
2175 source = find_window(source->transient);
2176 target = TAILQ_PREV(source, ws_win_list, entry);
2177 if (target && target->transient)
2178 target = find_window(target->transient);
2179 TAILQ_REMOVE(wl, source, entry);
2181 TAILQ_INSERT_TAIL(wl, source, entry);
2183 TAILQ_INSERT_BEFORE(target, source, entry);
2185 case SWM_ARG_ID_SWAPNEXT:
2186 target = TAILQ_NEXT(source, entry);
2187 /* move the parent and let the sort handle the move */
2188 if (source->transient)
2189 source = find_window(source->transient);
2190 TAILQ_REMOVE(wl, source, entry);
2192 TAILQ_INSERT_HEAD(wl, source, entry);
2194 TAILQ_INSERT_AFTER(wl, target, source, entry);
2196 case SWM_ARG_ID_SWAPMAIN:
2197 target = TAILQ_FIRST(wl);
2198 if (target == source) {
2199 if (source->ws->focus_prev != NULL &&
2200 source->ws->focus_prev != target)
2202 source = source->ws->focus_prev;
2206 if (target == NULL || source == NULL)
2208 source->ws->focus_prev = target;
2209 TAILQ_REMOVE(wl, target, entry);
2210 TAILQ_INSERT_BEFORE(source, target, entry);
2211 TAILQ_REMOVE(wl, source, entry);
2212 TAILQ_INSERT_HEAD(wl, source, entry);
2214 case SWM_ARG_ID_MOVELAST:
2215 TAILQ_REMOVE(wl, source, entry);
2216 TAILQ_INSERT_TAIL(wl, source, entry);
2219 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
2229 focus_prev(struct ws_win *win)
2231 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2232 struct ws_win *cur_focus = NULL;
2233 struct ws_win_list *wl = NULL;
2234 struct workspace *ws = NULL;
2236 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
2238 if (!(win && win->ws))
2243 cur_focus = ws->focus;
2244 winlostfocus = cur_focus;
2246 /* pickle, just focus on whatever */
2247 if (cur_focus == NULL) {
2248 /* use prev_focus if valid */
2249 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2250 find_window(WINID(ws->focus_prev)))
2251 winfocus = ws->focus_prev;
2252 if (winfocus == NULL)
2253 winfocus = TAILQ_FIRST(wl);
2257 /* if transient focus on parent */
2258 if (cur_focus->transient) {
2259 winfocus = find_window(cur_focus->transient);
2263 /* if in max_stack try harder */
2264 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2265 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2266 if (cur_focus != ws->focus_prev)
2267 winfocus = ws->focus_prev;
2268 else if (cur_focus != ws->focus)
2269 winfocus = ws->focus;
2271 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2276 if (cur_focus == win)
2277 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2278 if (winfocus == NULL)
2279 winfocus = TAILQ_LAST(wl, ws_win_list);
2280 if (winfocus == NULL || winfocus == win)
2281 winfocus = TAILQ_NEXT(cur_focus, entry);
2283 if (winfocus == winlostfocus || winfocus == NULL)
2286 focus_magic(winfocus);
2290 focus(struct swm_region *r, union arg *args)
2292 struct ws_win *winfocus = NULL, *winlostfocus = NULL, *head;
2293 struct ws_win *cur_focus = NULL;
2294 struct ws_win_list *wl = NULL;
2295 struct workspace *ws = NULL;
2300 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
2302 /* treat FOCUS_CUR special */
2303 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2304 if (r->ws->focus && r->ws->focus->iconic == 0)
2305 winfocus = r->ws->focus;
2306 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2307 winfocus = r->ws->focus_prev;
2309 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2310 if (winfocus->iconic == 0)
2313 focus_magic(winfocus);
2317 if ((cur_focus = r->ws->focus) == NULL)
2322 winlostfocus = cur_focus;
2325 case SWM_ARG_ID_FOCUSPREV:
2326 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2328 head = TAILQ_LAST(wl, ws_win_list);
2330 if (WINID(winfocus) == cur_focus->transient) {
2331 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2333 head = TAILQ_LAST(wl, ws_win_list);
2338 if (winfocus && winfocus->iconic) {
2339 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
2340 if (winfocus->iconic == 0)
2345 case SWM_ARG_ID_FOCUSNEXT:
2346 head = TAILQ_NEXT(cur_focus, entry);
2348 head = TAILQ_FIRST(wl);
2352 if (winfocus && winfocus->iconic) {
2353 TAILQ_FOREACH(winfocus, wl, entry)
2354 if (winfocus->iconic == 0)
2359 case SWM_ARG_ID_FOCUSMAIN:
2360 winfocus = TAILQ_FIRST(wl);
2361 if (winfocus == cur_focus)
2362 winfocus = cur_focus->ws->focus_prev;
2368 if (winfocus == winlostfocus || winfocus == NULL)
2371 focus_magic(winfocus);
2375 cycle_layout(struct swm_region *r, union arg *args)
2377 struct workspace *ws = r->ws;
2378 struct ws_win *winfocus;
2381 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2383 winfocus = ws->focus;
2386 if (ws->cur_layout->l_stack == NULL)
2387 ws->cur_layout = &layouts[0];
2390 if (focus_mode == SWM_FOCUS_DEFAULT)
2391 drain_enter_notify();
2392 a.id = SWM_ARG_ID_FOCUSCUR;
2398 stack_config(struct swm_region *r, union arg *args)
2400 struct workspace *ws = r->ws;
2402 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
2405 if (ws->cur_layout->l_config != NULL)
2406 ws->cur_layout->l_config(ws, args->id);
2408 if (args->id != SWM_ARG_ID_STACKINIT)
2414 struct swm_geometry g;
2415 struct swm_region *r;
2418 DNPRINTF(SWM_D_STACK, "stack\n");
2420 for (i = 0; i < ScreenCount(display); i++) {
2422 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2423 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
2424 "(screen %d, region %d)\n", r->ws->idx, i, j++);
2426 /* start with screen geometry, adjust for bar */
2428 g.w -= 2 * border_width;
2429 g.h -= 2 * border_width;
2435 r->ws->cur_layout->l_stack(r->ws, &g);
2436 /* save r so we can track region changes */
2443 if (focus_mode == SWM_FOCUS_DEFAULT)
2444 drain_enter_notify();
2448 store_float_geom(struct ws_win *win, struct swm_region *r)
2450 /* retain window geom and region geom */
2451 win->g_float.x = win->g.x;
2452 win->g_float.y = win->g.y;
2453 win->g_float.w = win->g.w;
2454 win->g_float.h = win->g.h;
2455 win->rg_float.x = r->g.x;
2456 win->rg_float.y = r->g.y;
2457 win->rg_float.w = r->g.w;
2458 win->rg_float.h = r->g.h;
2459 win->g_floatvalid = 1;
2463 stack_floater(struct ws_win *win, struct swm_region *r)
2471 bzero(&wc, sizeof wc);
2472 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2475 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2476 * geom on ws switches or return from max mode
2479 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2480 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2482 * use stored g and rg to set relative position and size
2483 * as in old region or before max stack mode
2485 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2486 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2487 win->g.w = win->g_float.w;
2488 win->g.h = win->g_float.h;
2489 win->g_floatvalid = 0;
2492 win->floatmaxed = 0;
2494 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
2495 (win->g.h >= HEIGHT(r)))
2496 wc.border_width = 0;
2498 wc.border_width = border_width;
2499 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2500 win->g.w = (double)WIDTH(r) * dialog_ratio;
2501 win->g.h = (double)HEIGHT(r) * dialog_ratio;
2506 * floaters and transients are auto-centred unless moved
2509 win->g.x = r->g.x + (WIDTH(r) - win->g.w) /
2510 2 - wc.border_width;
2511 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) /
2512 2 - wc.border_width;
2515 /* win can be outside r if new r smaller than old r */
2516 /* Ensure top left corner inside r (move probs otherwise) */
2517 if (win->g.x < r->g.x - wc.border_width)
2518 win->g.x = r->g.x - wc.border_width;
2519 if (win->g.x > r->g.x + r->g.w - 1)
2520 win->g.x = (win->g.w > r->g.w) ? r->g.x :
2521 (r->g.x + r->g.w - win->g.w - 2 * wc.border_width);
2522 if (win->g.y < r->g.y - wc.border_width)
2523 win->g.y = r->g.y - wc.border_width;
2524 if (win->g.y > r->g.y + r->g.h - 1)
2525 win->g.y = (win->g.h > r->g.h) ? r->g.y :
2526 (r->g.y + r->g.h - win->g.h - 2 * wc.border_width);
2530 wc.width = win->g.w;
2531 wc.height = win->g.h;
2534 * Retain floater and transient geometry for correct positioning
2535 * when ws changes region
2537 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2538 store_float_geom(win, r);
2540 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
2541 win->id, wc.x, wc.y, wc.width, wc.height);
2543 XConfigureWindow(display, win->id, mask, &wc);
2547 * Send keystrokes to terminal to decrease/increase the font size as the
2548 * window size changes.
2551 adjust_font(struct ws_win *win)
2553 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2554 win->floating || win->transient)
2557 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2558 win->g.w / win->sh.width_inc < term_width &&
2559 win->font_steps < SWM_MAX_FONT_STEPS) {
2560 win->font_size_boundary[win->font_steps] =
2561 (win->sh.width_inc * term_width) + win->sh.base_width;
2564 win->last_inc = win->sh.width_inc;
2565 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2566 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2567 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
2570 win->last_inc = win->sh.width_inc;
2571 fake_keypress(win, XK_KP_Add, ShiftMask);
2575 #define SWAPXY(g) do { \
2577 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2578 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2581 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2584 XWindowAttributes wa;
2585 struct swm_geometry win_g, r_g = *g;
2586 struct ws_win *win, *fs_win = 0;
2587 int i, j, s, stacks;
2588 int w_inc = 1, h_inc, w_base = 1, h_base;
2589 int hrh, extra = 0, h_slice, last_h = 0;
2590 int split, colno, winno, mwin, msize, mscale;
2591 int remain, missing, v_slice, reconfigure;
2594 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2595 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2597 winno = count_win(ws, 0);
2598 if (winno == 0 && count_win(ws, 1) == 0)
2601 TAILQ_FOREACH(win, &ws->winlist, entry)
2602 if (win->transient == 0 && win->floating == 0
2603 && win->iconic == 0)
2610 w_inc = win->sh.width_inc;
2611 w_base = win->sh.base_width;
2612 mwin = ws->l_state.horizontal_mwin;
2613 mscale = ws->l_state.horizontal_msize;
2614 stacks = ws->l_state.horizontal_stacks;
2617 w_inc = win->sh.height_inc;
2618 w_base = win->sh.base_height;
2619 mwin = ws->l_state.vertical_mwin;
2620 mscale = ws->l_state.vertical_msize;
2621 stacks = ws->l_state.vertical_stacks;
2625 if (stacks > winno - mwin)
2626 stacks = winno - mwin;
2630 h_slice = r_g.h / SWM_H_SLICE;
2631 if (mwin && winno > mwin) {
2632 v_slice = r_g.w / SWM_V_SLICE;
2636 win_g.w = v_slice * mscale;
2638 if (w_inc > 1 && w_inc < v_slice) {
2639 /* adjust for window's requested size increment */
2640 remain = (win_g.w - w_base) % w_inc;
2641 missing = w_inc - remain;
2648 win_g.x += r_g.w - msize;
2651 colno = split = winno / stacks;
2652 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
2653 2 * border_width) / stacks);
2655 hrh = r_g.h / colno;
2656 extra = r_g.h - (colno * hrh);
2657 win_g.h = hrh - 2 * border_width;
2659 /* stack all the tiled windows */
2660 i = j = 0, s = stacks;
2661 TAILQ_FOREACH(win, &ws->winlist, entry) {
2662 if (win->transient != 0 || win->floating != 0)
2664 if (win->iconic != 0)
2667 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2672 if (split && i == split) {
2673 colno = (winno - mwin) / stacks;
2674 if (s <= (winno - mwin) % stacks)
2676 split = split + colno;
2677 hrh = (r_g.h / colno);
2678 extra = r_g.h - (colno * hrh);
2682 win_g.x += win_g.w + 2 * border_width;
2683 win_g.w = (r_g.w - msize -
2684 (stacks * 2 * border_width)) / stacks;
2686 win_g.w += (r_g.w - msize -
2687 (stacks * 2 * border_width)) % stacks;
2691 win_g.h = hrh - 2 * border_width;
2693 h_inc = win->sh.width_inc;
2694 h_base = win->sh.base_width;
2696 h_inc = win->sh.height_inc;
2697 h_base = win->sh.base_height;
2699 if (j == colno - 1) {
2700 win_g.h = hrh + extra;
2701 } else if (h_inc > 1 && h_inc < h_slice) {
2702 /* adjust for window's requested size increment */
2703 remain = (win_g.h - h_base) % h_inc;
2704 missing = h_inc - remain;
2706 if (missing <= extra || j == 0) {
2718 win_g.y += last_h + 2 * border_width;
2720 bzero(&wc, sizeof wc);
2721 if (disable_border && bar_enabled == 0 && winno == 1){
2722 wc.border_width = 0;
2723 win_g.w += 2 * border_width;
2724 win_g.h += 2 * border_width;
2726 wc.border_width = border_width;
2729 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2730 win->g.w != win_g.h || win->g.h != win_g.w) {
2732 win->g.x = wc.x = win_g.y;
2733 win->g.y = wc.y = win_g.x;
2734 win->g.w = wc.width = win_g.h;
2735 win->g.h = wc.height = win_g.w;
2738 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2739 win->g.w != win_g.w || win->g.h != win_g.h) {
2741 win->g.x = wc.x = win_g.x;
2742 win->g.y = wc.y = win_g.y;
2743 win->g.w = wc.width = win_g.w;
2744 win->g.h = wc.height = win_g.h;
2749 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2750 XConfigureWindow(display, win->id, mask, &wc);
2753 if (XGetWindowAttributes(display, win->id, &wa))
2754 if (wa.map_state == IsUnmapped)
2755 XMapRaised(display, win->id);
2763 /* now, stack all the floaters and transients */
2764 TAILQ_FOREACH(win, &ws->winlist, entry) {
2765 if (win->transient == 0 && win->floating == 0)
2767 if (win->iconic == 1)
2769 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2774 stack_floater(win, ws->r);
2775 XMapRaised(display, win->id);
2779 stack_floater(fs_win, ws->r);
2780 XMapRaised(display, fs_win->id);
2785 vertical_config(struct workspace *ws, int id)
2787 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2790 case SWM_ARG_ID_STACKRESET:
2791 case SWM_ARG_ID_STACKINIT:
2792 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2793 ws->l_state.vertical_mwin = 1;
2794 ws->l_state.vertical_stacks = 1;
2796 case SWM_ARG_ID_MASTERSHRINK:
2797 if (ws->l_state.vertical_msize > 1)
2798 ws->l_state.vertical_msize--;
2800 case SWM_ARG_ID_MASTERGROW:
2801 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2802 ws->l_state.vertical_msize++;
2804 case SWM_ARG_ID_MASTERADD:
2805 ws->l_state.vertical_mwin++;
2807 case SWM_ARG_ID_MASTERDEL:
2808 if (ws->l_state.vertical_mwin > 0)
2809 ws->l_state.vertical_mwin--;
2811 case SWM_ARG_ID_STACKINC:
2812 ws->l_state.vertical_stacks++;
2814 case SWM_ARG_ID_STACKDEC:
2815 if (ws->l_state.vertical_stacks > 1)
2816 ws->l_state.vertical_stacks--;
2824 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2826 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2828 stack_master(ws, g, 0, 0);
2832 horizontal_config(struct workspace *ws, int id)
2834 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2837 case SWM_ARG_ID_STACKRESET:
2838 case SWM_ARG_ID_STACKINIT:
2839 ws->l_state.horizontal_mwin = 1;
2840 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2841 ws->l_state.horizontal_stacks = 1;
2843 case SWM_ARG_ID_MASTERSHRINK:
2844 if (ws->l_state.horizontal_msize > 1)
2845 ws->l_state.horizontal_msize--;
2847 case SWM_ARG_ID_MASTERGROW:
2848 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2849 ws->l_state.horizontal_msize++;
2851 case SWM_ARG_ID_MASTERADD:
2852 ws->l_state.horizontal_mwin++;
2854 case SWM_ARG_ID_MASTERDEL:
2855 if (ws->l_state.horizontal_mwin > 0)
2856 ws->l_state.horizontal_mwin--;
2858 case SWM_ARG_ID_STACKINC:
2859 ws->l_state.horizontal_stacks++;
2861 case SWM_ARG_ID_STACKDEC:
2862 if (ws->l_state.horizontal_stacks > 1)
2863 ws->l_state.horizontal_stacks--;
2871 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2873 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
2875 stack_master(ws, g, 1, 0);
2878 /* fullscreen view */
2880 max_stack(struct workspace *ws, struct swm_geometry *g)
2883 struct swm_geometry gg = *g;
2884 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2888 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2893 winno = count_win(ws, 0);
2894 if (winno == 0 && count_win(ws, 1) == 0)
2897 TAILQ_FOREACH(win, &ws->winlist, entry) {
2898 if (win->transient) {
2900 parent = find_window(win->transient);
2904 if (win->floating && win->floatmaxed == 0 ) {
2906 * retain geometry for retrieval on exit from
2909 store_float_geom(win, ws->r);
2910 win->floatmaxed = 1;
2913 /* only reconfigure if necessary */
2914 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2916 bzero(&wc, sizeof wc);
2917 win->g.x = wc.x = gg.x;
2918 win->g.y = wc.y = gg.y;
2920 wc.border_width = border_width;
2921 win->g.w = wc.width = gg.w;
2922 win->g.h = wc.height = gg.h;
2924 wc.border_width = 0;
2925 win->g.w = wc.width = gg.w + 2 * border_width;
2926 win->g.h = wc.height = gg.h + 2 * border_width;
2928 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2929 XConfigureWindow(display, win->id, mask, &wc);
2931 /* unmap only if we don't have multi screen */
2932 if (win != ws->focus)
2933 if (!(ScreenCount(display) > 1 || outputs > 1))
2937 /* put the last transient on top */
2940 XMapRaised(display, parent->id);
2941 stack_floater(wintrans, ws->r);
2942 focus_magic(wintrans);
2947 send_to_ws(struct swm_region *r, union arg *args)
2949 int wsid = args->id;
2950 struct ws_win *win = NULL, *parent;
2951 struct workspace *ws, *nws;
2952 Atom ws_idx_atom = 0;
2953 unsigned char ws_idx_str[SWM_PROPLEN];
2962 if (win->ws->idx == wsid)
2965 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2968 nws = &win->s->ws[wsid];
2970 a.id = SWM_ARG_ID_FOCUSPREV;
2972 if (win->transient) {
2973 parent = find_window(win->transient);
2975 unmap_window(parent);
2976 TAILQ_REMOVE(&ws->winlist, parent, entry);
2977 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
2982 TAILQ_REMOVE(&ws->winlist, win, entry);
2983 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2986 /* Try to update the window's workspace property */
2987 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2989 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2990 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2992 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2993 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3000 iconify(struct swm_region *r, union arg *args)
3004 if (r->ws->focus == NULL)
3006 unmap_window(r->ws->focus);
3007 update_iconic(r->ws->focus, 1);
3009 if (focus_mode == SWM_FOCUS_DEFAULT)
3010 drain_enter_notify();
3011 r->ws->focus = NULL;
3012 a.id = SWM_ARG_ID_FOCUSCUR;
3017 get_win_name(Display *dpy, Window win, Atom wname, Atom stype,
3018 unsigned long *slen)
3021 unsigned long nitems, nbytes, nextra;
3022 unsigned char *prop = NULL;
3025 status = XGetWindowProperty(dpy, win, wname, 0L, 0L, False, stype,
3026 &rettype, &retfmt, &nitems, &nbytes, &prop);
3027 if (status != Success)
3031 status = XGetWindowProperty(dpy, win, wname, 0L, nbytes, False,
3032 stype, &rettype, &retfmt, &nitems, &nextra, &prop);
3033 if (status != Success) {
3037 if (rettype != stype) {
3046 uniconify(struct swm_region *r, union arg *args)
3054 DNPRINTF(SWM_D_MISC, "uniconify\n");
3056 if (r && r->ws == NULL)
3059 /* make sure we have anything to uniconify */
3060 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3061 if (win->ws == NULL)
3062 continue; /* should never happen */
3063 if (win->iconic == 0)
3072 spawn_select(r, args, "uniconify", &searchpid);
3074 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3077 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3078 if (win->ws == NULL)
3079 continue; /* should never happen */
3080 if (win->iconic == 0)
3083 name = get_win_name(display, win->id, a_wmname, a_string,
3087 fprintf(lfile, "%s.%lu\n", name, win->id);
3094 #define MAX_RESP_LEN 1024
3097 search_do_resp(void)
3101 char *name, *resp, *s;
3104 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3109 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3110 fprintf(stderr, "search: calloc\n");
3114 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3116 fprintf(stderr, "search: read error: %s\n", strerror(errno));
3119 resp[rbytes] = '\0';
3122 DNPRINTF(SWM_D_MISC, "search_do_resp: resp %s\n", resp);
3123 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3124 if (win->iconic == 0)
3126 name = get_win_name(display, win->id, a_wmname, a_string, &len);
3129 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3134 if (strncmp(s, resp, len) == 0) {
3135 /* XXX this should be a callback to generalize */
3136 update_iconic(win, 0);
3147 wkill(struct swm_region *r, union arg *args)
3149 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
3151 if (r->ws->focus == NULL)
3154 if (args->id == SWM_ARG_ID_KILLWINDOW)
3155 XKillClient(display, r->ws->focus->id);
3157 if (r->ws->focus->can_delete)
3158 client_msg(r->ws->focus, adelete);
3163 floating_toggle_win(struct ws_win *win)
3165 struct swm_region *r;
3175 /* reject floating toggles in max stack mode */
3176 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3179 if (win->floating) {
3180 if (!win->floatmaxed) {
3181 /* retain position for refloat */
3182 store_float_geom(win, r);
3186 if (win->g_floatvalid) {
3187 /* refloat at last floating relative position */
3188 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
3189 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
3190 win->g.w = win->g_float.w;
3191 win->g.h = win->g_float.h;
3196 ewmh_update_actions(win);
3202 floating_toggle(struct swm_region *r, union arg *args)
3204 struct ws_win *win = r->ws->focus;
3210 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3211 _NET_WM_STATE_TOGGLE);
3214 if (focus_mode == SWM_FOCUS_DEFAULT)
3215 drain_enter_notify();
3217 if (win == win->ws->focus) {
3218 a.id = SWM_ARG_ID_FOCUSCUR;
3219 focus(win->ws->r, &a);
3224 resize_window(struct ws_win *win, int center)
3228 struct swm_region *r;
3230 r = root_to_region(win->wa.root);
3231 bzero(&wc, sizeof wc);
3232 mask = CWBorderWidth | CWWidth | CWHeight;
3233 wc.border_width = border_width;
3234 wc.width = win->g.w;
3235 wc.height = win->g.h;
3236 if (center == SWM_ARG_ID_CENTER) {
3237 wc.x = (WIDTH(r) - win->g.w) / 2 - border_width;
3238 wc.y = (HEIGHT(r) - win->g.h) / 2 - border_width;
3242 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
3243 win->id, wc.x, wc.y, wc.width, wc.height);
3245 XConfigureWindow(display, win->id, mask, &wc);
3249 resize(struct ws_win *win, union arg *args)
3253 struct swm_region *r = win->ws->r;
3257 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
3258 win->id, win->floating, win->transient);
3260 if (!(win->transient != 0 || win->floating != 0))
3263 /* reject resizes in max mode for floaters (transient ok) */
3264 if (win->floatmaxed)
3268 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3272 if (focus_mode == SWM_FOCUS_DEFAULT)
3273 drain_enter_notify();
3275 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3276 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3279 /* place pointer at bottom left corner or nearest point inside r */
3280 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
3281 relx = win->g.w - 1;
3283 relx = r->g.x + r->g.w - win->g.x - 1;
3285 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
3286 rely = win->g.h - 1;
3288 rely = r->g.y + r->g.h - win->g.y - 1;
3290 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
3292 XMaskEvent(display, MOUSEMASK | ExposureMask |
3293 SubstructureRedirectMask, &ev);
3295 case ConfigureRequest:
3298 handler[ev.type](&ev);
3301 /* do not allow resize outside of region */
3302 if ( ev.xmotion.x_root < r->g.x ||
3303 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3304 ev.xmotion.y_root < r->g.y ||
3305 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3308 if (ev.xmotion.x <= 1)
3310 if (ev.xmotion.y <= 1)
3312 win->g.w = ev.xmotion.x + 1;
3313 win->g.h = ev.xmotion.y + 1;
3315 /* not free, don't sync more than 120 times / second */
3316 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3317 time = ev.xmotion.time;
3318 XSync(display, False);
3319 resize_window(win, args->id);
3323 } while (ev.type != ButtonRelease);
3325 XSync(display, False);
3326 resize_window(win, args->id);
3328 store_float_geom(win,r);
3330 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
3332 XUngrabPointer(display, CurrentTime);
3335 drain_enter_notify();
3339 move_window(struct ws_win *win)
3343 struct swm_region *r;
3345 r = root_to_region(win->wa.root);
3346 bzero(&wc, sizeof wc);
3350 wc.border_width = border_width;
3352 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
3353 win->id, wc.x, wc.y, wc.width, wc.height);
3355 XConfigureWindow(display, win->id, mask, &wc);
3359 move(struct ws_win *win, union arg *args)
3363 struct swm_region *r = win->ws->r;
3365 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
3366 win->id, win->floating, win->transient);
3368 /* in max_stack mode should only move transients */
3369 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
3373 if (win->floating == 0 && !win->transient) {
3374 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3377 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3382 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3383 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3385 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3387 XMaskEvent(display, MOUSEMASK | ExposureMask |
3388 SubstructureRedirectMask, &ev);
3390 case ConfigureRequest:
3393 handler[ev.type](&ev);
3396 /* don't allow to move window origin out of region */
3397 if ( ev.xmotion.x_root < r->g.x ||
3398 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3399 ev.xmotion.y_root < r->g.y ||
3400 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3403 win->g.x = ev.xmotion.x_root - border_width;
3404 win->g.y = ev.xmotion.y_root - border_width;
3406 /* not free, don't sync more than 120 times / second */
3407 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3408 time = ev.xmotion.time;
3409 XSync(display, False);
3414 } while (ev.type != ButtonRelease);
3416 XSync(display, False);
3419 store_float_geom(win,r);
3420 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3421 XUngrabPointer(display, CurrentTime);
3424 drain_enter_notify();
3427 /* user/key callable function IDs */
3484 kf_dumpwins, /* MUST BE LAST */
3488 /* key definitions */
3490 dummykeyfunc(struct swm_region *r, union arg *args)
3495 legacyfunc(struct swm_region *r, union arg *args)
3500 char name[SWM_FUNCNAME_LEN];
3501 void (*func)(struct swm_region *r, union arg *);
3503 } keyfuncs[kf_invalid + 1] = {
3504 /* name function argument */
3505 { "cycle_layout", cycle_layout, {0} },
3506 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
3507 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
3508 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
3509 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
3510 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
3511 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
3512 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
3513 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
3514 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
3515 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
3516 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
3517 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
3518 { "spawn_term", spawnterm, {.argv = spawn_term} },
3519 { "spawn_menu", legacyfunc, {0} },
3520 { "quit", quit, {0} },
3521 { "restart", restart, {0} },
3522 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
3523 { "ws_1", switchws, {.id = 0} },
3524 { "ws_2", switchws, {.id = 1} },
3525 { "ws_3", switchws, {.id = 2} },
3526 { "ws_4", switchws, {.id = 3} },
3527 { "ws_5", switchws, {.id = 4} },
3528 { "ws_6", switchws, {.id = 5} },
3529 { "ws_7", switchws, {.id = 6} },
3530 { "ws_8", switchws, {.id = 7} },
3531 { "ws_9", switchws, {.id = 8} },
3532 { "ws_10", switchws, {.id = 9} },
3533 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
3534 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
3535 { "ws_prior", priorws, {0} },
3536 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
3537 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
3538 { "mvws_1", send_to_ws, {.id = 0} },
3539 { "mvws_2", send_to_ws, {.id = 1} },
3540 { "mvws_3", send_to_ws, {.id = 2} },
3541 { "mvws_4", send_to_ws, {.id = 3} },
3542 { "mvws_5", send_to_ws, {.id = 4} },
3543 { "mvws_6", send_to_ws, {.id = 5} },
3544 { "mvws_7", send_to_ws, {.id = 6} },
3545 { "mvws_8", send_to_ws, {.id = 7} },
3546 { "mvws_9", send_to_ws, {.id = 8} },
3547 { "mvws_10", send_to_ws, {.id = 9} },
3548 { "bar_toggle", bar_toggle, {0} },
3549 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
3550 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
3551 { "screenshot_all", legacyfunc, {0} },
3552 { "screenshot_wind", legacyfunc, {0} },
3553 { "float_toggle", floating_toggle,{0} },
3554 { "version", version, {0} },
3555 { "spawn_lock", legacyfunc, {0} },
3556 { "spawn_initscr", legacyfunc, {0} },
3557 { "spawn_custom", dummykeyfunc, {0} },
3558 { "iconify", iconify, {0} },
3559 { "uniconify", uniconify, {0} },
3560 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
3561 { "invalid key func", NULL, {0} },
3566 enum keyfuncid funcid;
3569 int keys_size = 0, keys_length = 0;
3570 struct key *keys = NULL;
3573 enum { client_click, root_click };
3575 unsigned int action;
3577 unsigned int button;
3578 void (*func)(struct ws_win *, union arg *);
3581 /* action key mouse button func args */
3582 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
3583 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
3584 { client_click, MODKEY, Button1, move, {0} },
3588 update_modkey(unsigned int mod)
3593 for (i = 0; i < keys_length; i++)
3594 if (keys[i].mod & ShiftMask)
3595 keys[i].mod = mod | ShiftMask;
3599 for (i = 0; i < LENGTH(buttons); i++)
3600 if (buttons[i].mask & ShiftMask)
3601 buttons[i].mask = mod | ShiftMask;
3603 buttons[i].mask = mod;
3613 int spawns_size = 0, spawns_length = 0;
3614 struct spawn_prog *spawns = NULL;
3617 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
3620 struct spawn_prog *prog = NULL;
3622 char *ap, **real_args;
3624 DNPRINTF(SWM_D_SPAWN, "spawn_expand %s\n", spawn_name);
3627 for (i = 0; i < spawns_length; i++) {
3628 if (!strcasecmp(spawn_name, spawns[i].name))
3632 fprintf(stderr, "spawn_custom: program %s not found\n",
3637 /* make room for expanded args */
3638 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
3639 err(1, "spawn_custom: calloc real_args");
3641 /* expand spawn_args into real_args */
3642 for (i = 0; i < prog->argc; i++) {
3644 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
3645 if (!strcasecmp(ap, "$bar_border")) {
3647 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
3649 err(1, "spawn_custom border color");
3650 } else if (!strcasecmp(ap, "$bar_color")) {
3652 strdup(r->s->c[SWM_S_COLOR_BAR].name))
3654 err(1, "spawn_custom bar color");
3655 } else if (!strcasecmp(ap, "$bar_font")) {
3656 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
3658 err(1, "spawn_custom bar fonts");
3659 } else if (!strcasecmp(ap, "$bar_font_color")) {
3661 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
3663 err(1, "spawn_custom color font");
3664 } else if (!strcasecmp(ap, "$color_focus")) {
3666 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
3668 err(1, "spawn_custom color focus");
3669 } else if (!strcasecmp(ap, "$color_unfocus")) {
3671 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
3673 err(1, "spawn_custom color unfocus");
3675 /* no match --> copy as is */
3676 if ((real_args[i] = strdup(ap)) == NULL)
3677 err(1, "spawn_custom strdup(ap)");
3679 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
3684 if ((swm_debug & SWM_D_SPAWN) != 0) {
3685 fprintf(stderr, "spawn_custom: result = ");
3686 for (i = 0; i < prog->argc; i++)
3687 fprintf(stderr, "\"%s\" ", real_args[i]);
3688 fprintf(stderr, "\n");
3691 *ret_args = real_args;
3692 return (prog->argc);
3696 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
3702 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3706 spawn(r->ws->idx, &a, 1);
3708 for (i = 0; i < spawn_argc; i++)
3714 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
3720 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3724 if (pipe(select_list_pipe) == -1)
3725 err(1, "pipe error");
3726 if (pipe(select_resp_pipe) == -1)
3727 err(1, "pipe error");
3729 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
3730 err(1, "could not disable SIGPIPE");
3731 switch (*pid = fork()) {
3733 err(1, "cannot fork");
3736 if (dup2(select_list_pipe[0], 0) == -1)
3738 if (dup2(select_resp_pipe[1], 1) == -1)
3740 close(select_list_pipe[1]);
3741 close(select_resp_pipe[0]);
3742 spawn(r->ws->idx, &a, 0);
3744 default: /* parent */
3745 close(select_list_pipe[0]);
3746 close(select_resp_pipe[1]);
3750 for (i = 0; i < spawn_argc; i++)
3756 setspawn(struct spawn_prog *prog)
3760 if (prog == NULL || prog->name == NULL)
3764 for (i = 0; i < spawns_length; i++) {
3765 if (!strcmp(spawns[i].name, prog->name)) {
3767 if (prog->argv == NULL) {
3769 DNPRINTF(SWM_D_SPAWN,
3770 "setspawn: delete #%d %s\n",
3772 free(spawns[i].name);
3773 for (j = 0; j < spawns[i].argc; j++)
3774 free(spawns[i].argv[j]);
3775 free(spawns[i].argv);
3776 j = spawns_length - 1;
3778 spawns[i] = spawns[j];
3783 DNPRINTF(SWM_D_SPAWN,
3784 "setspawn: replace #%d %s\n",
3786 free(spawns[i].name);
3787 for (j = 0; j < spawns[i].argc; j++)
3788 free(spawns[i].argv[j]);
3789 free(spawns[i].argv);
3792 /* found case handled */
3798 if (prog->argv == NULL) {
3800 "error: setspawn: cannot find program %s", prog->name);
3805 /* not found: add */
3806 if (spawns_size == 0 || spawns == NULL) {
3808 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
3809 spawns = malloc((size_t)spawns_size *
3810 sizeof(struct spawn_prog));
3811 if (spawns == NULL) {
3812 fprintf(stderr, "setspawn: malloc failed\n");
3816 } else if (spawns_length == spawns_size) {
3818 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
3819 spawns = realloc(spawns, (size_t)spawns_size *
3820 sizeof(struct spawn_prog));
3821 if (spawns == NULL) {
3822 fprintf(stderr, "setspawn: realloc failed\n");
3828 if (spawns_length < spawns_size) {
3829 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
3830 spawns_length, prog->name);
3831 i = spawns_length++;
3834 fprintf(stderr, "spawns array problem?\n");
3835 if (spawns == NULL) {
3836 fprintf(stderr, "spawns array is NULL!\n");
3844 setconfspawn(char *selector, char *value, int flags)
3846 struct spawn_prog *prog;
3847 char *vp, *cp, *word;
3849 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
3850 if ((prog = calloc(1, sizeof *prog)) == NULL)
3851 err(1, "setconfspawn: calloc prog");
3852 prog->name = strdup(selector);
3853 if (prog->name == NULL)
3854 err(1, "setconfspawn prog->name");
3855 if ((cp = vp = strdup(value)) == NULL)
3856 err(1, "setconfspawn: strdup(value) ");
3857 while ((word = strsep(&cp, " \t")) != NULL) {
3858 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
3860 cp += (long)strspn(cp, " \t");
3861 if (strlen(word) > 0) {
3863 if ((prog->argv = realloc(prog->argv,
3864 prog->argc * sizeof(char *))) == NULL)
3865 err(1, "setconfspawn: realloc");
3866 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
3867 err(1, "setconfspawn: strdup");
3874 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
3881 setconfspawn("term", "xterm", 0);
3882 setconfspawn("screenshot_all", "screenshot.sh full", 0);
3883 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
3884 setconfspawn("lock", "xlock", 0);
3885 setconfspawn("initscr", "initscreen.sh", 0);
3886 setconfspawn("menu", "dmenu_run"
3889 " -nf $bar_font_color"
3891 " -sf $bar_color", 0);
3892 setconfspawn("uniconify", "dmenu"
3896 " -nf $bar_font_color"
3898 " -sf $bar_color", 0);
3902 #define SWM_MODNAME_SIZE 32
3903 #define SWM_KEY_WS "\n+ \t"
3905 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
3909 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
3910 if (mod == NULL || ks == NULL) {
3911 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
3914 if (keystr == NULL || strlen(keystr) == 0) {
3915 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
3921 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
3922 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
3924 cp += (long)strspn(cp, SWM_KEY_WS);
3925 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
3927 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
3929 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
3931 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
3933 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
3935 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
3937 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
3938 *mod |= ControlMask;
3940 *ks = XStringToKeysym(name);
3941 XConvertCase(*ks, ks, &uks);
3942 if (ks == NoSymbol) {
3944 "parsekeys: invalid key %s\n",
3950 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
3955 strdupsafe(char *str)
3960 return (strdup(str));
3964 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
3968 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
3969 keyfuncs[kfid].name, spawn_name);
3971 for (i = 0; i < keys_length; i++) {
3972 if (keys[i].mod == mod && keys[i].keysym == ks) {
3973 if (kfid == kf_invalid) {
3976 "setkeybinding: delete #%d %s\n",
3977 i, keyfuncs[keys[i].funcid].name);
3978 free(keys[i].spawn_name);
3979 j = keys_length - 1;
3983 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3986 /* found: replace */
3988 "setkeybinding: replace #%d %s %s\n",
3989 i, keyfuncs[keys[i].funcid].name,
3991 free(keys[i].spawn_name);
3993 keys[i].keysym = ks;
3994 keys[i].funcid = kfid;
3995 keys[i].spawn_name = strdupsafe(spawn_name);
3996 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4001 if (kfid == kf_invalid) {
4003 "error: setkeybinding: cannot find mod/key combination");
4004 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4007 /* not found: add */
4008 if (keys_size == 0 || keys == NULL) {
4010 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
4011 keys = malloc((size_t)keys_size * sizeof(struct key));
4013 fprintf(stderr, "malloc failed\n");
4017 } else if (keys_length == keys_size) {
4019 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
4020 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
4022 fprintf(stderr, "realloc failed\n");
4027 if (keys_length < keys_size) {
4029 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
4030 j, keyfuncs[kfid].name, spawn_name);
4032 keys[j].keysym = ks;
4033 keys[j].funcid = kfid;
4034 keys[j].spawn_name = strdupsafe(spawn_name);
4036 fprintf(stderr, "keys array problem?\n");
4038 fprintf(stderr, "keys array problem\n");
4042 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4046 setconfbinding(char *selector, char *value, int flags)
4048 enum keyfuncid kfid;
4052 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
4053 if (selector == NULL) {
4054 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
4055 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4057 setkeybinding(mod, ks, kfid, NULL);
4062 /* search by key function name */
4063 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
4064 if (strncasecmp(selector, keyfuncs[kfid].name,
4065 SWM_FUNCNAME_LEN) == 0) {
4066 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4068 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4069 setkeybinding(mod, ks, kfid, NULL);
4075 /* search by custom spawn name */
4076 for (i = 0; i < spawns_length; i++) {
4077 if (strcasecmp(selector, spawns[i].name) == 0) {
4078 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4080 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4081 setkeybinding(mod, ks, kf_spawn_custom,
4088 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
4095 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
4096 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
4097 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
4098 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
4099 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
4100 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
4101 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
4102 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
4103 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
4104 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
4105 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
4106 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
4107 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
4108 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
4109 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
4110 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
4111 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
4112 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
4113 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
4114 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
4115 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
4116 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
4117 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
4118 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
4119 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
4120 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
4121 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
4122 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
4123 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
4124 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
4125 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
4126 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
4127 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
4128 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
4129 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
4130 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
4131 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
4132 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
4133 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
4134 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
4135 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
4136 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
4137 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
4138 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
4139 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
4140 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
4141 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
4142 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
4143 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
4144 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
4145 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
4146 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
4147 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
4148 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
4149 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
4150 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
4152 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
4157 updatenumlockmask(void)
4160 XModifierKeymap *modmap;
4162 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
4164 modmap = XGetModifierMapping(display);
4165 for (i = 0; i < 8; i++)
4166 for (j = 0; j < modmap->max_keypermod; j++)
4167 if (modmap->modifiermap[i * modmap->max_keypermod + j]
4168 == XKeysymToKeycode(display, XK_Num_Lock))
4169 numlockmask = (1 << i);
4171 XFreeModifiermap(modmap);
4177 unsigned int i, j, k;
4179 unsigned int modifiers[] =
4180 { 0, LockMask, numlockmask, numlockmask | LockMask };
4182 DNPRINTF(SWM_D_MISC, "grabkeys\n");
4183 updatenumlockmask();
4185 for (k = 0; k < ScreenCount(display); k++) {
4186 if (TAILQ_EMPTY(&screens[k].rl))
4188 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
4189 for (i = 0; i < keys_length; i++) {
4190 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
4191 for (j = 0; j < LENGTH(modifiers); j++)
4192 XGrabKey(display, code,
4193 keys[i].mod | modifiers[j],
4194 screens[k].root, True,
4195 GrabModeAsync, GrabModeAsync);
4201 grabbuttons(struct ws_win *win, int focused)
4204 unsigned int modifiers[] =
4205 { 0, LockMask, numlockmask, numlockmask|LockMask };
4207 updatenumlockmask();
4208 XUngrabButton(display, AnyButton, AnyModifier, win->id);
4210 for (i = 0; i < LENGTH(buttons); i++)
4211 if (buttons[i].action == client_click)
4212 for (j = 0; j < LENGTH(modifiers); j++)
4213 XGrabButton(display, buttons[i].button,
4214 buttons[i].mask | modifiers[j],
4215 win->id, False, BUTTONMASK,
4216 GrabModeAsync, GrabModeSync, None,
4219 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
4220 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
4223 const char *quirkname[] = {
4224 "NONE", /* config string for "no value" */
4233 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
4234 #define SWM_Q_WS "\n|+ \t"
4236 parsequirks(char *qstr, unsigned long *quirk)
4246 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
4248 cp += (long)strspn(cp, SWM_Q_WS);
4249 for (i = 0; i < LENGTH(quirkname); i++) {
4250 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
4251 DNPRINTF(SWM_D_QUIRK,
4252 "parsequirks: %s\n", name);
4257 *quirk |= 1 << (i-1);
4261 if (i >= LENGTH(quirkname)) {
4262 DNPRINTF(SWM_D_QUIRK,
4263 "parsequirks: invalid quirk [%s]\n", name);
4271 setquirk(const char *class, const char *name, const int quirk)
4276 for (i = 0; i < quirks_length; i++) {
4277 if (!strcmp(quirks[i].class, class) &&
4278 !strcmp(quirks[i].name, name)) {
4281 DNPRINTF(SWM_D_QUIRK,
4282 "setquirk: delete #%d %s:%s\n",
4283 i, quirks[i].class, quirks[i].name);
4284 free(quirks[i].class);
4285 free(quirks[i].name);
4286 j = quirks_length - 1;
4288 quirks[i] = quirks[j];
4292 /* found: replace */
4293 DNPRINTF(SWM_D_QUIRK,
4294 "setquirk: replace #%d %s:%s\n",
4295 i, quirks[i].class, quirks[i].name);
4296 free(quirks[i].class);
4297 free(quirks[i].name);
4298 quirks[i].class = strdup(class);
4299 quirks[i].name = strdup(name);
4300 quirks[i].quirk = quirk;
4307 "error: setquirk: cannot find class/name combination");
4310 /* not found: add */
4311 if (quirks_size == 0 || quirks == NULL) {
4313 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
4314 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
4315 if (quirks == NULL) {
4316 fprintf(stderr, "setquirk: malloc failed\n");
4320 } else if (quirks_length == quirks_size) {
4322 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
4323 quirks = realloc(quirks,
4324 (size_t)quirks_size * sizeof(struct quirk));
4325 if (quirks == NULL) {
4326 fprintf(stderr, "setquirk: realloc failed\n");
4331 if (quirks_length < quirks_size) {
4332 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
4333 j = quirks_length++;
4334 quirks[j].class = strdup(class);
4335 quirks[j].name = strdup(name);
4336 quirks[j].quirk = quirk;
4338 fprintf(stderr, "quirks array problem?\n");
4339 if (quirks == NULL) {
4340 fprintf(stderr, "quirks array problem!\n");
4347 setconfquirk(char *selector, char *value, int flags)
4349 char *cp, *class, *name;
4351 unsigned long quirks;
4352 if (selector == NULL)
4354 if ((cp = strchr(selector, ':')) == NULL)
4359 if ((retval = parsequirks(value, &quirks)) == 0)
4360 setquirk(class, name, quirks);
4367 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
4368 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
4369 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
4370 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
4371 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4372 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
4373 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4374 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4375 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4376 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4377 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
4378 setquirk("pcb", "pcb", SWM_Q_FLOAT);
4379 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
4382 /* conf file stuff */
4383 #define SWM_CONF_FILE "scrotwm.conf"
4385 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH,
4386 SWM_S_STACK_ENABLED, SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT,
4387 SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED,
4388 SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
4389 SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED,
4390 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH,
4391 SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM,
4392 SWM_S_SS_APP, SWM_S_DIALOG_RATIO, SWM_S_BAR_AT_BOTTOM
4396 setconfvalue(char *selector, char *value, int flags)
4399 case SWM_S_BAR_DELAY:
4400 bar_delay = atoi(value);
4402 case SWM_S_BAR_ENABLED:
4403 bar_enabled = atoi(value);
4405 case SWM_S_BAR_BORDER_WIDTH:
4406 bar_border_width = atoi(value);
4408 case SWM_S_BAR_AT_BOTTOM:
4409 bar_at_bottom = atoi(value);
4411 case SWM_S_STACK_ENABLED:
4412 stack_enabled = atoi(value);
4414 case SWM_S_CLOCK_ENABLED:
4415 clock_enabled = atoi(value);
4417 case SWM_S_CLOCK_FORMAT:
4418 #ifndef SWM_DENY_CLOCK_FORMAT
4420 if ((clock_format = strdup(value)) == NULL)
4421 err(1, "setconfvalue: clock_format");
4424 case SWM_S_CYCLE_EMPTY:
4425 cycle_empty = atoi(value);
4427 case SWM_S_CYCLE_VISIBLE:
4428 cycle_visible = atoi(value);
4430 case SWM_S_SS_ENABLED:
4431 ss_enabled = atoi(value);
4433 case SWM_S_TERM_WIDTH:
4434 term_width = atoi(value);
4436 case SWM_S_TITLE_CLASS_ENABLED:
4437 title_class_enabled = atoi(value);
4439 case SWM_S_WINDOW_NAME_ENABLED:
4440 window_name_enabled = atoi(value);
4442 case SWM_S_TITLE_NAME_ENABLED:
4443 title_name_enabled = atoi(value);
4445 case SWM_S_FOCUS_MODE:
4446 if (!strcmp(value, "default"))
4447 focus_mode = SWM_FOCUS_DEFAULT;
4448 else if (!strcmp(value, "follow_cursor"))
4449 focus_mode = SWM_FOCUS_FOLLOW;
4450 else if (!strcmp(value, "synergy"))
4451 focus_mode = SWM_FOCUS_SYNERGY;
4453 err(1, "focus_mode");
4455 case SWM_S_DISABLE_BORDER:
4456 disable_border = atoi(value);
4458 case SWM_S_BORDER_WIDTH:
4459 border_width = atoi(value);
4461 case SWM_S_BAR_FONT:
4463 if ((bar_fonts[0] = strdup(value)) == NULL)
4464 err(1, "setconfvalue: bar_font");
4466 case SWM_S_BAR_ACTION:
4468 if ((bar_argv[0] = strdup(value)) == NULL)
4469 err(1, "setconfvalue: bar_action");
4471 case SWM_S_SPAWN_TERM:
4472 free(spawn_term[0]);
4473 if ((spawn_term[0] = strdup(value)) == NULL)
4474 err(1, "setconfvalue: spawn_term");
4478 case SWM_S_DIALOG_RATIO:
4479 dialog_ratio = atof(value);
4480 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
4490 setconfmodkey(char *selector, char *value, int flags)
4492 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
4493 update_modkey(Mod1Mask);
4494 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
4495 update_modkey(Mod2Mask);
4496 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
4497 update_modkey(Mod3Mask);
4498 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
4499 update_modkey(Mod4Mask);
4506 setconfcolor(char *selector, char *value, int flags)
4508 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
4513 setconfregion(char *selector, char *value, int flags)
4515 custom_region(value);
4520 setautorun(char *selector, char *value, int flags)
4525 char *real_args[] = { NULL, NULL };
4529 if (getenv("SWM_STARTED"))
4533 if (sscanf(value, "ws[%d]:%1023s", &ws_id, s) != 2)
4534 errx(1, "invalid autorun entry, should be 'ws:command'\n");
4536 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
4537 errx(1, "invalid workspace %d\n", ws_id + 1);
4540 * This is a little intricate
4542 * If the pid already exists we simply reuse it because it means it was
4543 * used before AND not claimed by manage_window. We get away with
4544 * altering it in the parent after INSERT because this can not be a race
4547 a.argv = real_args; /* XXX this sucks and should have args for real */
4548 if ((pid = fork()) == 0) {
4549 spawn(ws_id, &a, 1);
4557 p = calloc(1, sizeof *p);
4560 TAILQ_INSERT_TAIL(&pidlist, p, entry);
4569 /* config options */
4570 struct config_option {
4572 int (*func)(char*, char*, int);
4575 struct config_option configopt[] = {
4576 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
4577 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
4578 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
4579 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
4580 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
4581 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
4582 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
4583 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
4584 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
4585 { "bind", setconfbinding, 0 },
4586 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
4587 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
4588 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
4589 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
4590 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
4591 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
4592 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
4593 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
4594 { "modkey", setconfmodkey, 0 },
4595 { "program", setconfspawn, 0 },
4596 { "quirk", setconfquirk, 0 },
4597 { "region", setconfregion, 0 },
4598 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
4599 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
4600 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
4601 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
4602 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
4603 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
4604 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
4605 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
4606 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
4607 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
4608 { "autorun", setautorun, 0 },
4613 conf_load(char *filename)
4616 char *line, *cp, *optsub, *optval;
4617 size_t linelen, lineno = 0;
4618 int wordlen, i, optind;
4619 struct config_option *opt;
4621 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
4623 if (filename == NULL) {
4624 fprintf(stderr, "conf_load: no filename\n");
4627 if ((config = fopen(filename, "r")) == NULL) {
4628 warn("conf_load: fopen");
4632 while (!feof(config)) {
4633 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
4636 err(1, "%s", filename);
4641 cp += strspn(cp, " \t\n"); /* eat whitespace */
4642 if (cp[0] == '\0') {
4647 /* get config option */
4648 wordlen = strcspn(cp, "=[ \t\n");
4650 warnx("%s: line %zd: no option found",
4655 for (i = 0; i < LENGTH(configopt); i++) {
4656 opt = &configopt[i];
4657 if (!strncasecmp(cp, opt->optname, wordlen) &&
4658 strlen(opt->optname) == wordlen) {
4664 warnx("%s: line %zd: unknown option %.*s",
4665 filename, lineno, wordlen, cp);
4669 cp += strspn(cp, " \t\n"); /* eat whitespace */
4670 /* get [selector] if any */
4674 wordlen = strcspn(cp, "]");
4677 warnx("%s: line %zd: syntax error",
4681 asprintf(&optsub, "%.*s", wordlen, cp);
4684 cp += strspn(cp, "] \t\n"); /* eat trailing */
4686 cp += strspn(cp, "= \t\n"); /* eat trailing */
4688 optval = strdup(cp);
4689 /* call function to deal with it all */
4690 if (configopt[optind].func(optsub, optval,
4691 configopt[optind].funcflags) != 0) {
4692 fprintf(stderr, "%s line %zd: %s\n",
4693 filename, lineno, line);
4694 errx(1, "%s: line %zd: invalid data for %s",
4695 filename, lineno, configopt[optind].optname);
4703 DNPRINTF(SWM_D_CONF, "conf_load end\n");
4709 set_child_transient(struct ws_win *win, Window *trans)
4711 struct ws_win *parent, *w;
4712 XWMHints *wmh = NULL;
4713 struct swm_region *r;
4714 struct workspace *ws;
4716 parent = find_window(win->transient);
4718 parent->child_trans = win;
4720 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
4721 " for %lu trans %lu\n", win->id, win->transient);
4723 if (win->hints == NULL) {
4724 fprintf(stderr, "no hints for %lu\n", win->id);
4728 r = root_to_region(win->wa.root);
4730 /* parent doen't exist in our window list */
4731 TAILQ_FOREACH(w, &ws->winlist, entry) {
4735 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
4736 fprintf(stderr, "can't get hints for %lu\n",
4741 if (win->hints->window_group != wmh->window_group)
4744 w->child_trans = win;
4745 win->transient = w->id;
4747 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
4748 "transient to %lu\n", win->transient);
4758 window_get_pid(Window win)
4760 Atom actual_type_return;
4761 int actual_format_return = 0;
4762 unsigned long nitems_return = 0;
4763 unsigned long bytes_after_return = 0;
4767 if (XGetWindowProperty(display, win,
4768 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
4769 XA_CARDINAL, &actual_type_return, &actual_format_return,
4770 &nitems_return, &bytes_after_return,
4771 (unsigned char**)(void*)&pid) != Success)
4781 manage_window(Window id)
4784 struct workspace *ws;
4785 struct ws_win *win, *ww;
4786 int format, i, ws_idx, n, border_me = 0;
4787 unsigned long nitems, bytes;
4788 Atom ws_idx_atom = 0, type;
4789 Atom *prot = NULL, *pp;
4790 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
4791 struct swm_region *r;
4797 if ((win = find_window(id)) != NULL)
4798 return (win); /* already being managed */
4800 /* see if we are on the unmanaged list */
4801 if ((win = find_unmanaged_window(id)) != NULL) {
4802 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
4804 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
4805 if (win->transient) {
4806 set_child_transient(win, &trans);
4807 } if (trans && (ww = find_window(trans)))
4808 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
4810 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
4811 ewmh_update_actions(win);
4815 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
4816 errx(1, "calloc: failed to allocate memory for new window");
4820 /* see if we need to override the workspace */
4821 p = find_pid(window_get_pid(id));
4823 /* Get all the window data in one shot */
4824 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4826 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
4827 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
4829 XGetWindowAttributes(display, id, &win->wa);
4830 XGetWMNormalHints(display, id, &win->sh, &mask);
4831 win->hints = XGetWMHints(display, id);
4832 XGetTransientForHint(display, id, &trans);
4834 win->transient = trans;
4835 set_child_transient(win, &trans);
4836 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
4837 win->id, win->transient);
4840 /* get supported protocols */
4841 if (XGetWMProtocols(display, id, &prot, &n)) {
4842 for (i = 0, pp = prot; i < n; i++, pp++) {
4843 if (*pp == takefocus)
4844 win->take_focus = 1;
4846 win->can_delete = 1;
4852 win->iconic = get_iconic(win);
4855 * Figure out where to put the window. If it was previously assigned to
4856 * a workspace (either by spawn() or manually moving), and isn't
4857 * transient, * put it in the same workspace
4859 r = root_to_region(win->wa.root);
4861 ws = &r->s->ws[p->ws];
4862 TAILQ_REMOVE(&pidlist, p, entry);
4865 } else if (prop && win->transient == 0) {
4866 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
4867 ws_idx = strtonum(prop, 0, 9, &errstr);
4869 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
4872 ws = &r->s->ws[ws_idx];
4875 /* this should launch transients in the same ws as parent */
4877 if ((ww = find_window(trans)) != NULL)
4884 "fix this bug mcbride\n");
4889 /* set up the window layout */
4892 win->s = r->s; /* this never changes */
4893 if (trans && (ww = find_window(trans)))
4894 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
4896 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
4898 win->g.w = win->wa.width;
4899 win->g.h = win->wa.height;
4900 win->g.x = win->wa.x;
4901 win->g.y = win->wa.y;
4902 win->g_floatvalid = 0;
4903 win->floatmaxed = 0;
4904 win->ewmh_flags = 0;
4906 /* Set window properties so we can remember this after reincarnation */
4907 if (ws_idx_atom && prop == NULL &&
4908 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
4909 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
4911 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
4912 PropModeReplace, ws_idx_str, SWM_PROPLEN);
4917 ewmh_autoquirk(win);
4919 if (XGetClassHint(display, win->id, &win->ch)) {
4920 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
4921 win->ch.res_class, win->ch.res_name);
4923 /* java is retarded so treat it special */
4924 if (strstr(win->ch.res_name, "sun-awt")) {
4929 for (i = 0; i < quirks_length; i++){
4930 if (!strcmp(win->ch.res_class, quirks[i].class) &&
4931 !strcmp(win->ch.res_name, quirks[i].name)) {
4932 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
4933 win->ch.res_class, win->ch.res_name);
4934 if (quirks[i].quirk & SWM_Q_FLOAT) {
4938 win->quirks = quirks[i].quirk;
4943 /* alter window position if quirky */
4944 if (win->quirks & SWM_Q_ANYWHERE) {
4945 win->manual = 1; /* don't center the quirky windows */
4946 bzero(&wc, sizeof wc);
4948 if (bar_enabled && win->g.y < bar_height) {
4949 win->g.y = wc.y = bar_height;
4952 if (win->g.w + win->g.x > WIDTH(r)) {
4953 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
4959 /* Reset font sizes (the bruteforce way; no default keybinding). */
4960 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
4961 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
4962 fake_keypress(win, XK_KP_Subtract, ShiftMask);
4963 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
4964 fake_keypress(win, XK_KP_Add, ShiftMask);
4967 ewmh_get_win_state(win);
4968 ewmh_update_actions(win);
4969 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
4973 bzero(&wc, sizeof wc);
4974 wc.border_width = border_width;
4975 mask = CWBorderWidth;
4976 XConfigureWindow(display, win->id, mask, &wc);
4979 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
4980 PropertyChangeMask | StructureNotifyMask);
4982 /* floaters need to be mapped if they are in the current workspace */
4983 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
4984 XMapRaised(display, win->id);
4990 free_window(struct ws_win *win)
4992 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
4997 /* needed for restart wm */
4998 set_win_state(win, WithdrawnState);
5000 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5002 if (win->ch.res_class)
5003 XFree(win->ch.res_class);
5004 if (win->ch.res_name)
5005 XFree(win->ch.res_name);
5010 memset(win, 0xff, sizeof *win); /* XXX kill later */
5016 unmanage_window(struct ws_win *win)
5018 struct ws_win *parent;
5023 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
5025 if (win->transient) {
5026 parent = find_window(win->transient);
5028 parent->child_trans = NULL;
5031 /* focus on root just in case */
5032 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
5036 TAILQ_REMOVE(&win->ws->winlist, win, entry);
5037 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
5043 focus_magic(struct ws_win *win)
5045 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu\n", WINID(win));
5050 if (win->child_trans) {
5051 /* win = parent & has a transient so focus on that */
5053 focus_win(win->child_trans);
5054 if (win->child_trans->take_focus)
5055 client_msg(win, takefocus);
5057 /* make sure transient hasn't dissapeared */
5058 if (validate_win(win->child_trans) == 0) {
5059 focus_win(win->child_trans);
5060 if (win->child_trans->take_focus)
5061 client_msg(win->child_trans, takefocus);
5063 win->child_trans = NULL;
5065 if (win->take_focus)
5066 client_msg(win, takefocus);
5072 if (win->take_focus)
5073 client_msg(win, takefocus);
5080 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
5088 XKeyEvent *ev = &e->xkey;
5090 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
5092 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
5093 for (i = 0; i < keys_length; i++)
5094 if (keysym == keys[i].keysym
5095 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
5096 && keyfuncs[keys[i].funcid].func) {
5097 if (keys[i].funcid == kf_spawn_custom)
5099 root_to_region(ev->root),
5100 &(keyfuncs[keys[i].funcid].args),
5104 keyfuncs[keys[i].funcid].func(
5105 root_to_region(ev->root),
5106 &(keyfuncs[keys[i].funcid].args)
5112 buttonpress(XEvent *e)
5116 XButtonPressedEvent *ev = &e->xbutton;
5118 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
5120 action = root_click;
5121 if ((win = find_window(ev->window)) == NULL)
5125 action = client_click;
5127 for (i = 0; i < LENGTH(buttons); i++)
5128 if (action == buttons[i].action && buttons[i].func &&
5129 buttons[i].button == ev->button &&
5130 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
5131 buttons[i].func(win, &buttons[i].args);
5135 configurerequest(XEvent *e)
5137 XConfigureRequestEvent *ev = &e->xconfigurerequest;
5142 if ((win = find_window(ev->window)) == NULL)
5143 if ((win = find_unmanaged_window(ev->window)) == NULL)
5147 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
5149 bzero(&wc, sizeof wc);
5152 wc.width = ev->width;
5153 wc.height = ev->height;
5154 wc.border_width = ev->border_width;
5155 wc.sibling = ev->above;
5156 wc.stack_mode = ev->detail;
5157 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
5159 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
5161 config_win(win, ev);
5166 configurenotify(XEvent *e)
5171 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
5172 e->xconfigure.window);
5174 win = find_window(e->xconfigure.window);
5176 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5180 if (focus_mode == SWM_FOCUS_DEFAULT)
5181 drain_enter_notify();
5186 destroynotify(XEvent *e)
5189 XDestroyWindowEvent *ev = &e->xdestroywindow;
5191 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
5193 if ((win = find_window(ev->window)) == NULL) {
5194 if ((win = find_unmanaged_window(ev->window)) == NULL)
5200 /* make sure we focus on something */
5203 unmanage_window(win);
5205 if (focus_mode == SWM_FOCUS_DEFAULT)
5206 drain_enter_notify();
5211 enternotify(XEvent *e)
5213 XCrossingEvent *ev = &e->xcrossing;
5218 Window focus_return;
5219 int revert_to_return;
5221 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
5222 "%lu subwindow %lu same_screen %d focus %d state %d\n",
5223 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
5224 ev->same_screen, ev->focus, ev->state);
5226 switch (focus_mode) {
5227 case SWM_FOCUS_DEFAULT:
5229 case SWM_FOCUS_FOLLOW:
5231 case SWM_FOCUS_SYNERGY:
5234 * all these checks need to be in this order because the
5235 * XCheckTypedWindowEvent relies on weeding out the previous events
5237 * making this code an option would enable a follow mouse for focus
5242 * state is set when we are switching workspaces and focus is set when
5243 * the window or a subwindow already has focus (occurs during restart).
5245 * Only honor the focus flag if last_focus_event is not FocusOut,
5246 * this allows scrotwm to continue to control focus when another
5247 * program is also playing with it.
5249 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
5250 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
5255 * happens when a window is created or destroyed and the border
5256 * crosses the mouse pointer and when switching ws
5258 * we need the subwindow test to see if we came from root in order
5259 * to give focus to floaters
5261 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
5262 ev->subwindow == 0) {
5263 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
5267 /* this window already has focus */
5268 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
5269 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
5273 /* this window is being deleted or moved to another ws */
5274 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
5276 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
5277 XPutBackEvent(display, &cne);
5281 if ((win = find_window(ev->window)) == NULL) {
5282 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5287 * In fullstack kill all enters unless they come from a different ws
5288 * (i.e. another region) or focus has been grabbed externally.
5290 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
5291 last_focus_event != FocusOut) {
5292 XGetInputFocus(display, &focus_return, &revert_to_return);
5293 if ((w = find_window(focus_return)) == NULL ||
5295 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
5303 if ((win = find_window(ev->window)) == NULL) {
5304 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5309 * if we have more enternotifies let them handle it in due time
5311 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
5312 DNPRINTF(SWM_D_EVENT,
5313 "ignoring enternotify: got more enternotify\n");
5314 XPutBackEvent(display, &cne);
5321 /* lets us use one switch statement for arbitrary mode/detail combinations */
5322 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
5325 focusevent(XEvent *e)
5329 u_int32_t mode_detail;
5330 XFocusChangeEvent *ev = &e->xfocus;
5332 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
5333 ev->type == FocusIn ? "entering" : "leaving",
5334 ev->window, ev->mode, ev->detail);
5336 if (last_focus_event == ev->type) {
5337 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
5341 last_focus_event = ev->type;
5342 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
5344 switch (mode_detail) {
5345 /* synergy client focus operations */
5346 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
5347 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
5349 /* synergy server focus operations */
5350 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
5352 /* Entering applications like rdesktop that mangle the pointer */
5353 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
5355 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
5356 XSetWindowBorder(display, win->id,
5357 win->ws->r->s->c[ev->type == FocusIn ?
5358 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
5361 fprintf(stderr, "ignoring focusevent\n");
5362 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
5369 mapnotify(XEvent *e)
5372 XMapEvent *ev = &e->xmap;
5374 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
5376 win = manage_window(ev->window);
5378 set_win_state(win, NormalState);
5382 mappingnotify(XEvent *e)
5384 XMappingEvent *ev = &e->xmapping;
5386 XRefreshKeyboardMapping(ev);
5387 if (ev->request == MappingKeyboard)
5392 maprequest(XEvent *e)
5395 struct swm_region *r;
5396 XWindowAttributes wa;
5397 XMapRequestEvent *ev = &e->xmaprequest;
5399 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
5400 e->xmaprequest.window);
5402 if (!XGetWindowAttributes(display, ev->window, &wa))
5404 if (wa.override_redirect)
5407 win = manage_window(e->xmaprequest.window);
5409 return; /* can't happen */
5413 /* make new win focused */
5414 r = root_to_region(win->wa.root);
5415 if (win->ws == r->ws)
5420 propertynotify(XEvent *e)
5423 XPropertyEvent *ev = &e->xproperty;
5425 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
5428 win = find_window(ev->window);
5432 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
5433 update_iconic(win, 0);
5434 XMapRaised(display, win->id);
5441 case XA_WM_NORMAL_HINTS:
5444 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5445 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
5446 if (win->sh.flags & PMinSize) {
5447 win->g.w = win->sh.min_width;
5448 win->g.h = win->sh.min_height;
5449 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
5451 XMoveResizeWindow(display, win->id,
5452 win->g.x, win->g.y, win->g.w, win->g.h);
5454 if (window_name_enabled)
5463 unmapnotify(XEvent *e)
5467 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
5469 /* determine if we need to help unmanage this window */
5470 win = find_window(e->xunmap.window);
5474 if (getstate(e->xunmap.window) == NormalState) {
5475 unmanage_window(win);
5478 /* giant hack for apps that don't destroy transient windows */
5479 /* eat a bunch of events to prevent remanaging the window */
5481 while (XCheckWindowEvent(display, e->xunmap.window,
5482 EnterWindowMask, &cne))
5484 while (XCheckWindowEvent(display, e->xunmap.window,
5485 StructureNotifyMask, &cne))
5487 while (XCheckWindowEvent(display, e->xunmap.window,
5488 SubstructureNotifyMask, &cne))
5490 /* resend unmap because we ated it */
5491 XUnmapWindow(display, e->xunmap.window);
5494 if (focus_mode == SWM_FOCUS_DEFAULT)
5495 drain_enter_notify();
5499 visibilitynotify(XEvent *e)
5502 struct swm_region *r;
5504 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
5505 e->xvisibility.window);
5506 if (e->xvisibility.state == VisibilityUnobscured)
5507 for (i = 0; i < ScreenCount(display); i++)
5508 TAILQ_FOREACH(r, &screens[i].rl, entry)
5509 if (e->xvisibility.window == r->bar_window)
5514 clientmessage(XEvent *e)
5516 XClientMessageEvent *ev;
5521 win = find_window(ev->window);
5525 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx type: %ld \n",
5526 ev->window, ev->message_type);
5528 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
5529 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW \n");
5532 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
5533 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW \n");
5534 if (win->can_delete)
5535 client_msg(win, adelete);
5537 XKillClient(display, win->id);
5539 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
5540 DNPRINTF(SWM_D_EVENT,
5541 "clientmessage: _NET_MOVERESIZE_WINDOW \n");
5542 if (win->floating) {
5543 if (ev->data.l[0] & (1<<8)) /* x */
5544 win->g.x = ev->data.l[1];
5545 if (ev->data.l[0] & (1<<9)) /* y */
5546 win->g.y = ev->data.l[2];
5547 if (ev->data.l[0] & (1<<10)) /* width */
5548 win->g.w = ev->data.l[3];
5549 if (ev->data.l[0] & (1<<11)) /* height */
5550 win->g.h = ev->data.l[4];
5553 /* TODO: Change stack sizes */
5555 config_win(win, NULL);
5557 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
5558 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE \n");
5559 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
5561 ewmh_update_win_state(win, ev->data.l[2],
5569 xerror_start(Display *d, XErrorEvent *ee)
5576 xerror(Display *d, XErrorEvent *ee)
5578 /* fprintf(stderr, "error: %p %p\n", display, ee); */
5586 xerrorxlib = XSetErrorHandler(xerror_start);
5588 /* this causes an error if some other window manager is running */
5589 XSelectInput(display, DefaultRootWindow(display),
5590 SubstructureRedirectMask);
5591 XSync(display, False);
5595 XSetErrorHandler(xerror);
5596 XSync(display, False);
5601 new_region(struct swm_screen *s, int x, int y, int w, int h)
5603 struct swm_region *r, *n;
5604 struct workspace *ws = NULL;
5607 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
5608 s->idx, w, h, x, y);
5610 /* remove any conflicting regions */
5611 n = TAILQ_FIRST(&s->rl);
5614 n = TAILQ_NEXT(r, entry);
5615 if (X(r) < (x + w) &&
5616 (X(r) + WIDTH(r)) > x &&
5618 (Y(r) + HEIGHT(r)) > y) {
5619 if (r->ws->r != NULL)
5620 r->ws->old_r = r->ws->r;
5622 XDestroyWindow(display, r->bar_window);
5623 TAILQ_REMOVE(&s->rl, r, entry);
5624 TAILQ_INSERT_TAIL(&s->orl, r, entry);
5628 /* search old regions for one to reuse */
5630 /* size + location match */
5631 TAILQ_FOREACH(r, &s->orl, entry)
5632 if (X(r) == x && Y(r) == y &&
5633 HEIGHT(r) == h && WIDTH(r) == w)
5637 TAILQ_FOREACH(r, &s->orl, entry)
5638 if (HEIGHT(r) == h && WIDTH(r) == w)
5642 TAILQ_REMOVE(&s->orl, r, entry);
5643 /* try to use old region's workspace */
5644 if (r->ws->r == NULL)
5647 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
5648 errx(1, "calloc: failed to allocate memory for screen");
5650 /* if we don't have a workspace already, find one */
5652 for (i = 0; i < SWM_WS_MAX; i++)
5653 if (s->ws[i].r == NULL) {
5660 errx(1, "no free workspaces\n");
5671 TAILQ_INSERT_TAIL(&s->rl, r, entry);
5677 #ifdef SWM_XRR_HAS_CRTC
5679 XRRScreenResources *sr;
5682 #endif /* SWM_XRR_HAS_CRTC */
5683 struct swm_region *r;
5686 if (i >= ScreenCount(display))
5687 errx(1, "invalid screen");
5689 /* remove any old regions */
5690 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
5691 r->ws->old_r = r->ws->r = NULL;
5692 XDestroyWindow(display, r->bar_window);
5693 TAILQ_REMOVE(&screens[i].rl, r, entry);
5694 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
5698 /* map virtual screens onto physical screens */
5699 #ifdef SWM_XRR_HAS_CRTC
5700 if (xrandr_support) {
5701 sr = XRRGetScreenResources(display, screens[i].root);
5703 new_region(&screens[i], 0, 0,
5704 DisplayWidth(display, i),
5705 DisplayHeight(display, i));
5709 for (c = 0, ci = NULL; c < ncrtc; c++) {
5710 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
5711 if (ci->noutput == 0)
5714 if (ci != NULL && ci->mode == None)
5715 new_region(&screens[i], 0, 0,
5716 DisplayWidth(display, i),
5717 DisplayHeight(display, i));
5719 new_region(&screens[i],
5720 ci->x, ci->y, ci->width, ci->height);
5723 XRRFreeCrtcInfo(ci);
5724 XRRFreeScreenResources(sr);
5726 #endif /* SWM_XRR_HAS_CRTC */
5728 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
5729 DisplayHeight(display, i));
5734 screenchange(XEvent *e) {
5735 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
5736 struct swm_region *r;
5739 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
5741 if (!XRRUpdateConfiguration(e))
5744 /* silly event doesn't include the screen index */
5745 for (i = 0; i < ScreenCount(display); i++)
5746 if (screens[i].root == xe->root)
5748 if (i >= ScreenCount(display))
5749 errx(1, "screenchange: screen not found\n");
5751 /* brute force for now, just re-enumerate the regions */
5754 /* add bars to all regions */
5755 for (i = 0; i < ScreenCount(display); i++)
5756 TAILQ_FOREACH(r, &screens[i].rl, entry)
5759 if (focus_mode == SWM_FOCUS_DEFAULT)
5760 drain_enter_notify();
5766 Window d1, d2, *wins = NULL;
5767 XWindowAttributes wa;
5772 for (i = 0; i < ScreenCount(display); i++) {
5773 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
5776 /* attach windows to a region */
5777 /* normal windows */
5778 for (j = 0; j < no; j++) {
5779 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5780 wa.override_redirect ||
5781 XGetTransientForHint(display, wins[j], &d1))
5784 state = getstate(wins[j]);
5785 manage = state == IconicState;
5786 if (wa.map_state == IsViewable || manage)
5787 manage_window(wins[j]);
5789 /* transient windows */
5790 for (j = 0; j < no; j++) {
5791 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5792 wa.override_redirect)
5795 state = getstate(wins[j]);
5796 manage = state == IconicState;
5797 if (XGetTransientForHint(display, wins[j], &d1) &&
5799 manage_window(wins[j]);
5812 int errorbase, major, minor;
5813 struct workspace *ws;
5815 if ((screens = calloc(ScreenCount(display),
5816 sizeof(struct swm_screen))) == NULL)
5817 errx(1, "calloc: screens");
5819 /* initial Xrandr setup */
5820 xrandr_support = XRRQueryExtension(display,
5821 &xrandr_eventbase, &errorbase);
5823 if (XRRQueryVersion(display, &major, &minor) && major < 1)
5826 /* map physical screens */
5827 for (i = 0; i < ScreenCount(display); i++) {
5828 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
5830 TAILQ_INIT(&screens[i].rl);
5831 TAILQ_INIT(&screens[i].orl);
5832 screens[i].root = RootWindow(display, i);
5834 /* set default colors */
5835 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
5836 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
5837 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
5838 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
5839 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
5841 /* set default cursor */
5842 XDefineCursor(display, screens[i].root,
5843 XCreateFontCursor(display, XC_left_ptr));
5845 /* init all workspaces */
5846 /* XXX these should be dynamically allocated too */
5847 for (j = 0; j < SWM_WS_MAX; j++) {
5848 ws = &screens[i].ws[j];
5853 TAILQ_INIT(&ws->winlist);
5854 TAILQ_INIT(&ws->unmanagedlist);
5856 for (k = 0; layouts[k].l_stack != NULL; k++)
5857 if (layouts[k].l_config != NULL)
5858 layouts[k].l_config(ws,
5859 SWM_ARG_ID_STACKINIT);
5860 ws->cur_layout = &layouts[0];
5866 XRRSelectInput(display, screens[i].root,
5867 RRScreenChangeNotifyMask);
5874 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
5876 err(1, "setup_globals: strdup");
5877 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
5879 err(1, "setup_globals: strdup");
5880 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
5882 err(1, "setup_globals: strdup");
5883 if ((spawn_term[0] = strdup("xterm")) == NULL)
5884 err(1, "setup_globals: strdup");
5885 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
5893 Atom netwmcheck, netwmname, utf8_string;
5896 /* work around sun jdk bugs, code from wmname */
5897 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
5898 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
5899 utf8_string = XInternAtom(display, "UTF8_STRING", False);
5900 for (i = 0; i < ScreenCount(display); i++) {
5901 root = screens[i].root;
5902 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
5903 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
5904 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
5906 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
5907 PropModeReplace, (unsigned char *)&win,1);
5908 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
5909 PropModeReplace, (unsigned char *)&win,1);
5910 XChangeProperty(display, win, netwmname, utf8_string, 8,
5911 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
5916 main(int argc, char *argv[])
5919 struct swm_region *r, *rr;
5920 struct ws_win *winfocus = NULL;
5923 char conf[PATH_MAX], *cfile = NULL;
5928 struct sigaction sact;
5931 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
5932 SWM_VERSION, cvstag);
5933 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
5934 warnx("no locale support");
5936 if (!(display = XOpenDisplay(0)))
5937 errx(1, "can not open display");
5940 errx(1, "other wm running");
5942 /* handle some signals */
5943 bzero(&sact, sizeof(sact));
5944 sigemptyset(&sact.sa_mask);
5946 sact.sa_handler = sighdlr;
5947 sigaction(SIGINT, &sact, NULL);
5948 sigaction(SIGQUIT, &sact, NULL);
5949 sigaction(SIGTERM, &sact, NULL);
5950 sigaction(SIGHUP, &sact, NULL);
5952 sact.sa_handler = sighdlr;
5953 sact.sa_flags = SA_NOCLDSTOP;
5954 sigaction(SIGCHLD, &sact, NULL);
5956 astate = XInternAtom(display, "WM_STATE", False);
5957 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
5958 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
5959 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
5960 a_wmname = XInternAtom(display, "WM_NAME", False);
5961 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
5962 a_string = XInternAtom(display, "STRING", False);
5963 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
5965 /* look for local and global conf file */
5966 pwd = getpwuid(getuid());
5968 errx(1, "invalid user %d", getuid());
5977 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
5978 if (stat(conf, &sb) != -1) {
5979 if (S_ISREG(sb.st_mode))
5982 /* try global conf file */
5983 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
5984 if (!stat(conf, &sb))
5985 if (S_ISREG(sb.st_mode))
5992 /* set some values to work around bad programs */
5995 /* grab existing windows (before we build the bars) */
5998 if (getenv("SWM_STARTED") == NULL)
5999 setenv("SWM_STARTED", "YES", 1);
6001 /* setup all bars */
6002 for (i = 0; i < ScreenCount(display); i++)
6003 TAILQ_FOREACH(r, &screens[i].rl, entry) {
6004 if (winfocus == NULL)
6005 winfocus = TAILQ_FIRST(&r->ws->winlist);
6013 if (focus_mode == SWM_FOCUS_DEFAULT)
6014 drain_enter_notify();
6016 xfd = ConnectionNumber(display);
6018 while (XPending(display)) {
6019 XNextEvent(display, &e);
6022 if (e.type < LASTEvent) {
6024 if (handler[e.type])
6025 handler[e.type](&e);
6027 DNPRINTF(SWM_D_EVENT,
6028 "win: %lu unknown event: %d\n",
6029 e.xany.window, e.type);
6031 switch (e.type - xrandr_eventbase) {
6032 case RRScreenChangeNotify:
6036 DNPRINTF(SWM_D_EVENT,
6037 "win: %lu unknown xrandr event: "
6038 "%d\n", e.xany.window, e.type);
6044 /* if we are being restarted go focus on first window */
6046 rr = winfocus->ws->r;
6048 /* not a visible window */
6052 /* move pointer to first screen if multi screen */
6053 if (ScreenCount(display) > 1 || outputs > 1)
6054 XWarpPointer(display, None, rr->s[0].root,
6055 0, 0, 0, 0, rr->g.x,
6056 rr->g.y + (bar_enabled ? bar_height : 0));
6058 a.id = SWM_ARG_ID_FOCUSCUR;
6068 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
6070 DNPRINTF(SWM_D_MISC, "select failed");
6071 if (restart_wm == 1)
6072 restart(NULL, NULL);
6073 if (search_resp == 1)
6085 XFreeGC(display, bar_gc);
6086 XCloseDisplay(display);