3 * Copyright (c) 2009-2010-2011 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009-2010-2011 Ryan McBride <mcbride@countersiege.com>
5 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
6 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Much code and ideas taken from dwm under the following license:
23 * MIT/X Consortium License
25 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
26 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
27 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
28 * 2007 Premysl Hruby <dfenze at gmail dot com>
29 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
30 * 2007 Christof Musik <christof at sendfax dot de>
31 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
32 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
33 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
35 * Permission is hereby granted, free of charge, to any person obtaining a
36 * copy of this software and associated documentation files (the "Software"),
37 * to deal in the Software without restriction, including without limitation
38 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
39 * and/or sell copies of the Software, and to permit persons to whom the
40 * Software is furnished to do so, subject to the following conditions:
42 * The above copyright notice and this permission notice shall be included in
43 * all copies or substantial portions of the Software.
45 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
46 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
47 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
48 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
49 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
50 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
51 * DEALINGS IN THE SOFTWARE.
54 static const char *cvstag = "$scrotwm$";
56 #define SWM_VERSION "0.9.30"
73 #include <sys/types.h>
77 #include <sys/queue.h>
78 #include <sys/param.h>
79 #include <sys/select.h>
81 #include <X11/cursorfont.h>
82 #include <X11/keysym.h>
83 #include <X11/Xatom.h>
85 #include <X11/Xproto.h>
86 #include <X11/Xutil.h>
87 #include <X11/extensions/Xrandr.h>
94 # error XRandR versions less than 1.0 are not supported
99 #define SWM_XRR_HAS_CRTC
103 /*#define SWM_DEBUG*/
105 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
106 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
107 #define SWM_D_MISC 0x0001
108 #define SWM_D_EVENT 0x0002
109 #define SWM_D_WS 0x0004
110 #define SWM_D_FOCUS 0x0008
111 #define SWM_D_MOVE 0x0010
112 #define SWM_D_STACK 0x0020
113 #define SWM_D_MOUSE 0x0040
114 #define SWM_D_PROP 0x0080
115 #define SWM_D_CLASS 0x0100
116 #define SWM_D_KEY 0x0200
117 #define SWM_D_QUIRK 0x0400
118 #define SWM_D_SPAWN 0x0800
119 #define SWM_D_EVENTQ 0x1000
120 #define SWM_D_CONF 0x2000
122 u_int32_t swm_debug = 0
139 #define DPRINTF(x...)
140 #define DNPRINTF(n,x...)
143 #define LENGTH(x) (sizeof x / sizeof x[0])
144 #define MODKEY Mod1Mask
145 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
146 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
147 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
148 #define SWM_PROPLEN (16)
149 #define SWM_FUNCNAME_LEN (32)
150 #define SWM_KEYS_LEN (255)
151 #define SWM_QUIRK_LEN (64)
152 #define X(r) (r)->g.x
153 #define Y(r) (r)->g.y
154 #define WIDTH(r) (r)->g.w
155 #define HEIGHT(r) (r)->g.h
156 #define SWM_MAX_FONT_STEPS (3)
157 #define WINID(w) (w ? w->id : 0)
159 #define SWM_FOCUS_DEFAULT (0)
160 #define SWM_FOCUS_SYNERGY (1)
161 #define SWM_FOCUS_FOLLOW (2)
164 #define SWM_LIB "/usr/local/lib/libswmhack.so"
176 volatile sig_atomic_t running = 1;
177 volatile sig_atomic_t restart_wm = 0;
179 int last_focus_event = FocusOut;
180 int (*xerrorxlib)(Display *, XErrorEvent *);
184 int xrandr_eventbase;
185 unsigned int numlockmask = 0;
189 int cycle_visible = 0;
191 int font_adjusted = 0;
192 unsigned int mod_key = MODKEY;
195 struct swm_region *search_r;
196 int select_list_pipe[2];
197 int select_resp_pipe[2];
199 volatile sig_atomic_t search_resp;
202 double dialog_ratio = .6;
204 #define SWM_BAR_MAX (256)
205 char *bar_argv[] = { NULL, NULL };
207 char bar_ext[SWM_BAR_MAX];
208 char bar_vertext[SWM_BAR_MAX];
210 sig_atomic_t bar_alarm = 0;
213 int bar_border_width = 1;
214 int bar_at_bottom = 0;
216 int bar_extra_running = 0;
219 int stack_enabled = 1;
220 int clock_enabled = 1;
221 char *clock_format = NULL;
222 int title_name_enabled = 0;
223 int title_class_enabled = 0;
224 int window_name_enabled = 0;
225 int focus_mode = SWM_FOCUS_DEFAULT;
226 int disable_border = 0;
227 int border_width = 1;
233 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
234 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
236 #define SWM_MENU_FN (2)
237 #define SWM_MENU_NB (4)
238 #define SWM_MENU_NF (6)
239 #define SWM_MENU_SB (8)
240 #define SWM_MENU_SF (10)
242 /* layout manager data */
243 struct swm_geometry {
253 /* virtual "screens" */
255 TAILQ_ENTRY(swm_region) entry;
256 struct swm_geometry g;
257 struct workspace *ws; /* current workspace on this region */
258 struct workspace *ws_prior; /* prior workspace on this region */
259 struct swm_screen *s; /* screen idx */
262 TAILQ_HEAD(swm_region_list, swm_region);
265 TAILQ_ENTRY(ws_win) entry;
268 struct ws_win *child_trans; /* transient child window */
269 struct swm_geometry g; /* current geometry */
270 struct swm_geometry g_float; /* geometry when floating */
271 struct swm_geometry rg_float; /* region geom when floating */
272 int g_floatvalid; /* flag: geometry in g_float is valid */
273 int floatmaxed; /* flag: floater was maxed in max_stack */
277 unsigned int ewmh_flags;
278 int font_size_boundary[SWM_MAX_FONT_STEPS];
284 unsigned long quirks;
285 struct workspace *ws; /* always valid */
286 struct swm_screen *s; /* always valid, never changes */
287 XWindowAttributes wa;
291 TAILQ_HEAD(ws_win_list, ws_win);
293 /* layout handlers */
295 void vertical_config(struct workspace *, int);
296 void vertical_stack(struct workspace *, struct swm_geometry *);
297 void horizontal_config(struct workspace *, int);
298 void horizontal_stack(struct workspace *, struct swm_geometry *);
299 void max_stack(struct workspace *, struct swm_geometry *);
301 struct ws_win *find_window(Window);
303 void grabbuttons(struct ws_win *, int);
304 void new_region(struct swm_screen *, int, int, int, int);
305 void unmanage_window(struct ws_win *);
306 long getstate(Window);
309 void (*l_stack)(struct workspace *, struct swm_geometry *);
310 void (*l_config)(struct workspace *, int);
312 #define SWM_L_FOCUSPREV (1<<0)
313 #define SWM_L_MAPONFOCUS (1<<1)
316 /* stack, configure */
317 { vertical_stack, vertical_config, 0, "[|]" },
318 { horizontal_stack, horizontal_config, 0, "[-]" },
320 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, "[ ]"},
321 { NULL, NULL, 0, NULL },
324 /* position of max_stack mode in the layouts array */
325 #define SWM_MAX_STACK 2
327 #define SWM_H_SLICE (32)
328 #define SWM_V_SLICE (32)
330 /* define work spaces */
332 int idx; /* workspace index */
333 struct layout *cur_layout; /* current layout handlers */
334 struct ws_win *focus; /* may be NULL */
335 struct ws_win *focus_prev; /* may be NULL */
336 struct swm_region *r; /* may be NULL */
337 struct swm_region *old_r; /* may be NULL */
338 struct ws_win_list winlist; /* list of windows in ws */
339 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
343 int horizontal_msize;
345 int horizontal_stacks;
352 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
353 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
355 /* physical screen mapping */
356 #define SWM_WS_MAX (10)
358 int idx; /* screen index */
359 struct swm_region_list rl; /* list of regions on this screen */
360 struct swm_region_list orl; /* list of old regions */
362 struct workspace ws[SWM_WS_MAX];
368 } c[SWM_S_COLOR_MAX];
370 struct swm_screen *screens;
373 /* args to functions */
376 #define SWM_ARG_ID_FOCUSNEXT (0)
377 #define SWM_ARG_ID_FOCUSPREV (1)
378 #define SWM_ARG_ID_FOCUSMAIN (2)
379 #define SWM_ARG_ID_FOCUSCUR (4)
380 #define SWM_ARG_ID_SWAPNEXT (10)
381 #define SWM_ARG_ID_SWAPPREV (11)
382 #define SWM_ARG_ID_SWAPMAIN (12)
383 #define SWM_ARG_ID_MOVELAST (13)
384 #define SWM_ARG_ID_MASTERSHRINK (20)
385 #define SWM_ARG_ID_MASTERGROW (21)
386 #define SWM_ARG_ID_MASTERADD (22)
387 #define SWM_ARG_ID_MASTERDEL (23)
388 #define SWM_ARG_ID_STACKRESET (30)
389 #define SWM_ARG_ID_STACKINIT (31)
390 #define SWM_ARG_ID_CYCLEWS_UP (40)
391 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
392 #define SWM_ARG_ID_CYCLESC_UP (42)
393 #define SWM_ARG_ID_CYCLESC_DOWN (43)
394 #define SWM_ARG_ID_STACKINC (50)
395 #define SWM_ARG_ID_STACKDEC (51)
396 #define SWM_ARG_ID_SS_ALL (60)
397 #define SWM_ARG_ID_SS_WINDOW (61)
398 #define SWM_ARG_ID_DONTCENTER (70)
399 #define SWM_ARG_ID_CENTER (71)
400 #define SWM_ARG_ID_KILLWINDOW (80)
401 #define SWM_ARG_ID_DELETEWINDOW (81)
405 void focus(struct swm_region *, union arg *);
406 void focus_magic(struct ws_win *);
413 #define SWM_Q_FLOAT (1<<0) /* float this window */
414 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
415 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
416 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
417 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
419 int quirks_size = 0, quirks_length = 0;
420 struct quirk *quirks = NULL;
423 * Supported EWMH hints should be added to
424 * both the enum and the ewmh array
426 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
427 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
428 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
429 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
430 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
431 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
432 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
433 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
434 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
435 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
441 } ewmh[SWM_EWMH_HINT_MAX] = {
442 /* must be in same order as in the enum */
443 {"_NET_ACTIVE_WINDOW", None},
444 {"_NET_MOVERESIZE_WINDOW", None},
445 {"_NET_CLOSE_WINDOW", None},
446 {"_NET_WM_WINDOW_TYPE", None},
447 {"_NET_WM_WINDOW_TYPE_DOCK", None},
448 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
449 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
450 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
451 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
452 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
453 {"_NET_WM_STATE", None},
454 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
455 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
456 {"_NET_WM_STATE_SKIP_TASKBAR", None},
457 {"_NET_WM_STATE_SKIP_PAGER", None},
458 {"_NET_WM_STATE_HIDDEN", None},
459 {"_NET_WM_STATE_ABOVE", None},
460 {"_SWM_WM_STATE_MANUAL", None},
461 {"_NET_WM_STATE_FULLSCREEN", None},
462 {"_NET_WM_ALLOWED_ACTIONS", None},
463 {"_NET_WM_ACTION_MOVE", None},
464 {"_NET_WM_ACTION_RESIZE", None},
465 {"_NET_WM_ACTION_FULLSCREEN", None},
466 {"_NET_WM_ACTION_CLOSE", None},
469 void store_float_geom(struct ws_win *win, struct swm_region *r);
470 int floating_toggle_win(struct ws_win *win);
471 void spawn_select(struct swm_region *, union arg *, char *, int *);
474 get_property(Window id, Atom atom, long count, Atom type,
475 unsigned long *n, unsigned char **data)
478 unsigned long tmp, extra;
479 unsigned long *nitems;
482 nitems = n != NULL ? n : &tmp;
483 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
484 &real, &format, nitems, &extra, data);
486 if (status != Success)
495 update_iconic(struct ws_win *win, int newv)
502 iprop = XInternAtom(display, "_SWM_ICONIC", False);
506 XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
507 PropModeReplace, (unsigned char *)&v, 1);
509 XDeleteProperty(display, win->id, iprop);
513 get_iconic(struct ws_win *win)
518 unsigned long nitems, extra;
519 unsigned char *prop = NULL;
521 iprop = XInternAtom(display, "_SWM_ICONIC", False);
524 status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
525 False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
526 if (status != Success)
528 if (rettype != XA_INTEGER || retfmt != 32)
532 v = *((int32_t *)prop);
546 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
548 for (i = 0; i < LENGTH(ewmh); i++)
549 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
551 for (i = 0; i < ScreenCount(display); i++) {
552 /* Support check window will be created by workaround(). */
554 /* Report supported atoms */
555 XDeleteProperty(display, screens[i].root, sup_list);
556 for (j = 0; j < LENGTH(ewmh); j++)
557 XChangeProperty(display, screens[i].root, sup_list, XA_ATOM, 32,
558 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
566 unsigned char *data = NULL;
568 Atom sup_check, sup_list;
571 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
572 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
574 for (i = 0; i < ScreenCount(display); i++) {
575 /* Get the support check window and destroy it */
576 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
581 XDestroyWindow(display, id);
582 XDeleteProperty(display, screens[i].root, sup_check);
583 XDeleteProperty(display, screens[i].root, sup_list);
591 ewmh_autoquirk(struct ws_win *win)
594 unsigned long *data = NULL;
598 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
599 XA_ATOM, &n, (unsigned char **)&data);
606 for (i = 0; i < n; i++) {
608 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
610 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
611 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
612 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
614 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
617 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
618 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
620 win->quirks = SWM_Q_FLOAT;
628 #define SWM_EWMH_ACTION_COUNT_MAX (6)
629 #define EWMH_F_FULLSCREEN (1<<0)
630 #define EWMH_F_ABOVE (1<<1)
631 #define EWMH_F_HIDDEN (1<<2)
632 #define EWMH_F_SKIP_PAGER (1<<3)
633 #define EWMH_F_SKIP_TASKBAR (1<<4)
634 #define SWM_F_MANUAL (1<<5)
637 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
639 struct swm_geometry rg;
647 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
653 store_float_geom(win, win->ws->r);
660 if (win->g_floatvalid) {
661 /* refloat at last floating relative position */
662 win->g.x = win->g_float.x - win->rg_float.x + rg.x;
663 win->g.y = win->g_float.y - win->rg_float.y + rg.y;
664 win->g.w = win->g_float.w;
665 win->g.h = win->g_float.h;
673 ewmh_update_actions(struct ws_win *win)
675 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
681 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
684 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
685 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
688 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
689 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
692 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
693 #define _NET_WM_STATE_ADD 1 /* add/set property */
694 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
697 ewmh_update_win_state(struct ws_win *win, long state, long action)
699 unsigned int mask = 0;
700 unsigned int changed = 0;
701 unsigned int orig_flags;
706 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
707 mask = EWMH_F_FULLSCREEN;
708 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
710 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
712 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
713 mask = EWMH_F_SKIP_PAGER;
714 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
715 mask = EWMH_F_SKIP_TASKBAR;
718 orig_flags = win->ewmh_flags;
721 case _NET_WM_STATE_REMOVE:
722 win->ewmh_flags &= ~mask;
724 case _NET_WM_STATE_ADD:
725 win->ewmh_flags |= mask;
727 case _NET_WM_STATE_TOGGLE:
728 win->ewmh_flags ^= mask;
732 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
734 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
736 if (!floating_toggle_win(win))
737 win->ewmh_flags = orig_flags; /* revert */
738 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
740 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
741 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
743 if (!ewmh_set_win_fullscreen(win, win->ewmh_flags & EWMH_F_FULLSCREEN))
744 win->ewmh_flags = orig_flags; /* revert */
746 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
748 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
749 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
750 XA_ATOM, 32, PropModeAppend,
751 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
752 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
753 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
754 XA_ATOM, 32, PropModeAppend,
755 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
756 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
757 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
758 XA_ATOM, 32, PropModeAppend,
759 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
760 if (win->ewmh_flags & EWMH_F_ABOVE)
761 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
762 XA_ATOM, 32, PropModeAppend,
763 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
764 if (win->ewmh_flags & SWM_F_MANUAL)
765 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
766 XA_ATOM, 32, PropModeAppend,
767 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
771 ewmh_get_win_state(struct ws_win *win)
782 win->ewmh_flags |= EWMH_F_ABOVE;
784 win->ewmh_flags |= SWM_F_MANUAL;
786 success = get_property(win->id, ewmh[_NET_WM_STATE].atom, (~0L), XA_ATOM,
787 &n, (unsigned char **)&states);
792 for (i = 0; i < n; i++)
793 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
813 name = "ButtonPress";
816 name = "ButtonRelease";
819 name = "MotionNotify";
822 name = "EnterNotify";
825 name = "LeaveNotify";
834 name = "KeymapNotify";
840 name = "GraphicsExpose";
845 case VisibilityNotify:
846 name = "VisibilityNotify";
849 name = "CreateNotify";
852 name = "DestroyNotify";
855 name = "UnmapNotify";
864 name = "ReparentNotify";
866 case ConfigureNotify:
867 name = "ConfigureNotify";
869 case ConfigureRequest:
870 name = "ConfigureRequest";
873 name = "GravityNotify";
876 name = "ResizeRequest";
878 case CirculateNotify:
879 name = "CirculateNotify";
881 case CirculateRequest:
882 name = "CirculateRequest";
885 name = "PropertyNotify";
888 name = "SelectionClear";
890 case SelectionRequest:
891 name = "SelectionRequest";
893 case SelectionNotify:
894 name = "SelectionNotify";
897 name = "ColormapNotify";
900 name = "ClientMessage";
903 name = "MappingNotify";
908 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
910 e->xany.window, name, e->type, QLength(display));
912 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
914 e->xany.window, e->type, QLength(display));
918 dumpwins(struct swm_region *r, union arg *args)
922 XWindowAttributes wa;
925 fprintf(stderr, "invalid workspace\n");
929 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
931 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
932 state = getstate(win->id);
933 if (!XGetWindowAttributes(display, win->id, &wa))
934 fprintf(stderr, "window: %lu failed "
935 "XGetWindowAttributes\n", win->id);
936 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
937 win->id, wa.map_state, state);
940 fprintf(stderr, "===== unmanaged window list =====\n");
941 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
942 state = getstate(win->id);
943 if (!XGetWindowAttributes(display, win->id, &wa))
944 fprintf(stderr, "window: %lu failed "
945 "XGetWindowAttributes\n", win->id);
946 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
947 win->id, wa.map_state, state);
950 fprintf(stderr, "=================================\n");
955 dumpwins(struct swm_region *r, union arg *args)
958 #endif /* SWM_DEBUG */
960 void expose(XEvent *);
961 void keypress(XEvent *);
962 void buttonpress(XEvent *);
963 void configurerequest(XEvent *);
964 void configurenotify(XEvent *);
965 void destroynotify(XEvent *);
966 void enternotify(XEvent *);
967 void focusevent(XEvent *);
968 void mapnotify(XEvent *);
969 void mappingnotify(XEvent *);
970 void maprequest(XEvent *);
971 void propertynotify(XEvent *);
972 void unmapnotify(XEvent *);
973 void visibilitynotify(XEvent *);
974 void clientmessage(XEvent *);
976 void (*handler[LASTEvent])(XEvent *) = {
978 [KeyPress] = keypress,
979 [ButtonPress] = buttonpress,
980 [ConfigureRequest] = configurerequest,
981 [ConfigureNotify] = configurenotify,
982 [DestroyNotify] = destroynotify,
983 [EnterNotify] = enternotify,
984 [FocusIn] = focusevent,
985 [FocusOut] = focusevent,
986 [MapNotify] = mapnotify,
987 [MappingNotify] = mappingnotify,
988 [MapRequest] = maprequest,
989 [PropertyNotify] = propertynotify,
990 [UnmapNotify] = unmapnotify,
991 [VisibilityNotify] = visibilitynotify,
992 [ClientMessage] = clientmessage,
998 int saved_errno, status;
1001 saved_errno = errno;
1005 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1010 if (errno != ECHILD)
1011 warn("sighdlr: waitpid");
1012 #endif /* SWM_DEBUG */
1015 if (pid == searchpid)
1019 if (WIFEXITED(status)) {
1020 if (WEXITSTATUS(status) != 0)
1021 warnx("sighdlr: child exit status: %d",
1022 WEXITSTATUS(status));
1024 warnx("sighdlr: child is terminated "
1026 #endif /* SWM_DEBUG */
1040 errno = saved_errno;
1044 name_to_color(char *colorname)
1048 XColor screen_def, exact_def;
1049 unsigned long result = 0;
1050 char cname[32] = "#";
1052 cmap = DefaultColormap(display, screens[0].idx);
1053 status = XAllocNamedColor(display, cmap, colorname,
1054 &screen_def, &exact_def);
1056 strlcat(cname, colorname + 2, sizeof cname - 1);
1057 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1061 result = screen_def.pixel;
1063 fprintf(stderr, "color '%s' not found.\n", colorname);
1069 setscreencolor(char *val, int i, int c)
1071 if (i > 0 && i <= ScreenCount(display)) {
1072 screens[i - 1].c[c].color = name_to_color(val);
1073 free(screens[i - 1].c[c].name);
1074 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1076 } else if (i == -1) {
1077 for (i = 0; i < ScreenCount(display); i++) {
1078 screens[i].c[c].color = name_to_color(val);
1079 free(screens[i].c[c].name);
1080 if ((screens[i].c[c].name = strdup(val)) == NULL)
1084 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1085 i, ScreenCount(display));
1089 custom_region(char *val)
1091 unsigned int sidx, x, y, w, h;
1093 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1094 errx(1, "invalid custom region, "
1095 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
1096 if (sidx < 1 || sidx > ScreenCount(display))
1097 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1098 sidx, ScreenCount(display));
1102 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
1104 if (x < 0 || x > DisplayWidth(display, sidx) ||
1105 y < 0 || y > DisplayHeight(display, sidx) ||
1106 w + x > DisplayWidth(display, sidx) ||
1107 h + y > DisplayHeight(display, sidx)) {
1108 fprintf(stderr, "ignoring region %ux%u+%u+%u - not within screen boundaries "
1109 "(%ux%u)\n", w, h, x, y,
1110 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1114 new_region(&screens[sidx], x, y, w, h);
1118 socket_setnonblock(int fd)
1122 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1123 err(1, "fcntl F_GETFL");
1124 flags |= O_NONBLOCK;
1125 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1126 err(1, "fcntl F_SETFL");
1130 bar_print(struct swm_region *r, char *s)
1132 XClearWindow(display, r->bar_window);
1133 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
1134 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
1139 bar_extra_stop(void)
1143 bzero(bar_pipe, sizeof bar_pipe);
1146 kill(bar_pid, SIGTERM);
1149 strlcpy(bar_ext, "", sizeof bar_ext);
1154 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1156 int do_class, do_name;
1158 XClassHint *xch = NULL;
1160 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
1161 cur_focus != NULL) {
1162 if ((xch = XAllocClassHint()) == NULL)
1164 status = XGetClassHint(display, cur_focus->id, xch);
1165 if (status == BadWindow || status == BadAlloc)
1167 do_class = (title_class_enabled && xch->res_class != NULL);
1168 do_name = (title_name_enabled && xch->res_name != NULL);
1170 strlcat(s, xch->res_class, sz);
1171 if (do_class && do_name)
1172 strlcat(s, ":", sz);
1174 strlcat(s, xch->res_name, sz);
1175 strlcat(s, " ", sz);
1183 bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1187 if (window_name_enabled && cur_focus != NULL) {
1188 XFetchName(display, cur_focus->id, &title);
1190 if (cur_focus->floating)
1191 strlcat(s, "(f) ", sz);
1192 strlcat(s, title, sz);
1193 strlcat(s, " ", sz);
1204 struct swm_region *r;
1207 char s[SWM_BAR_MAX];
1208 char cn[SWM_BAR_MAX];
1209 char loc[SWM_BAR_MAX];
1213 if (bar_enabled == 0)
1215 if (bar_extra && bar_extra_running) {
1216 /* ignore short reads; it'll correct itself */
1217 while ((b = fgetln(stdin, &len)) != NULL)
1218 if (b && b[len - 1] == '\n') {
1220 strlcpy(bar_ext, b, sizeof bar_ext);
1222 if (b == NULL && errno != EAGAIN) {
1223 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
1224 errno, strerror(errno));
1228 strlcpy(bar_ext, "", sizeof bar_ext);
1230 if (clock_enabled == 0)
1231 strlcpy(s, "", sizeof s);
1234 localtime_r(&tmt, &tm);
1235 strftime(s, sizeof s, clock_format, &tm);
1236 strlcat(s, " ", sizeof s);
1239 for (i = 0; i < ScreenCount(display); i++) {
1241 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1242 strlcpy(cn, "", sizeof cn);
1244 bar_class_name(cn, sizeof cn, r->ws->focus);
1245 bar_window_name(cn, sizeof cn, r->ws->focus);
1249 stack = r->ws->cur_layout->name;
1251 snprintf(loc, sizeof loc, "%d:%d %s %s%s %s %s",
1252 x++, r->ws->idx + 1, stack, s, cn, bar_ext,
1267 bar_toggle(struct swm_region *r, union arg *args)
1269 struct swm_region *tmpr;
1270 int i, sc = ScreenCount(display);
1272 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
1275 for (i = 0; i < sc; i++)
1276 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1277 XUnmapWindow(display, tmpr->bar_window);
1279 for (i = 0; i < sc; i++)
1280 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1281 XMapRaised(display, tmpr->bar_window);
1283 bar_enabled = !bar_enabled;
1286 /* must be after stack */
1293 XSetWindowAttributes wa;
1294 struct swm_region *r;
1297 /* do this here because the conf file is in memory */
1298 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1299 /* launch external status app */
1300 bar_extra_running = 1;
1301 if (pipe(bar_pipe) == -1)
1302 err(1, "pipe error");
1303 socket_setnonblock(bar_pipe[0]);
1304 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1305 if (dup2(bar_pipe[0], 0) == -1)
1307 if (dup2(bar_pipe[1], 1) == -1)
1309 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1310 err(1, "could not disable SIGPIPE");
1311 switch (bar_pid = fork()) {
1313 err(1, "cannot fork");
1317 execvp(bar_argv[0], bar_argv);
1318 err(1, "%s external app failed", bar_argv[0]);
1320 default: /* parent */
1326 bzero(&wa, sizeof wa);
1327 for (i = 0; i < ScreenCount(display); i++)
1328 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1330 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1331 wa.background_pixel =
1332 screens[i].c[SWM_S_COLOR_BAR].color;
1333 XChangeWindowAttributes(display, r->bar_window,
1334 CWBackPixel | CWBorderPixel, &wa);
1340 bar_setup(struct swm_region *r)
1345 XFreeFont(display, bar_fs);
1349 for (i = 0; bar_fonts[i] != NULL; i++) {
1350 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
1356 if (bar_fonts[i] == NULL)
1357 errx(1, "couldn't load font");
1359 errx(1, "couldn't create font structure");
1361 bar_height = bar_fs->ascent + bar_fs->descent + 1 + 2 * bar_border_width;
1363 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1365 r->bar_window = XCreateSimpleWindow(display,
1366 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width, bar_height - 2 * bar_border_width,
1367 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1368 r->s->c[SWM_S_COLOR_BAR].color);
1369 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
1370 XSetFont(display, bar_gc, bar_fs->fid);
1371 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1373 XMapRaised(display, r->bar_window);
1374 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
1376 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1377 err(1, "could not install bar_signal");
1382 set_win_state(struct ws_win *win, long state)
1384 long data[] = {state, None};
1386 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1391 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1392 (unsigned char *)data, 2);
1399 unsigned char *p = NULL;
1402 if (!get_property(w, astate, 2L, astate, &n, &p))
1405 result = *((long *)p);
1411 version(struct swm_region *r, union arg *args)
1413 bar_version = !bar_version;
1415 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
1416 SWM_VERSION, cvstag);
1418 strlcpy(bar_vertext, "", sizeof bar_vertext);
1423 client_msg(struct ws_win *win, Atom a)
1425 XClientMessageEvent cm;
1430 bzero(&cm, sizeof cm);
1431 cm.type = ClientMessage;
1432 cm.window = win->id;
1433 cm.message_type = aprot;
1436 cm.data.l[1] = CurrentTime;
1437 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1441 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1449 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
1450 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1452 ce.type = ConfigureNotify;
1453 ce.display = display;
1455 ce.window = win->id;
1458 ce.width = win->g.w;
1459 ce.height = win->g.h;
1460 ce.border_width = border_width;
1462 ce.override_redirect = False;
1464 DNPRINTF(SWM_D_MISC, "config_win: ev win %lu x %d y %d w %d h %d\n",
1465 ev->window, ev->x, ev->y, ev->width, ev->height);
1466 ce.type = ConfigureNotify;
1467 ce.display = ev->display;
1468 ce.event = ev->window;
1469 ce.window = ev->window;
1472 ce.width = ev->width;
1473 ce.height = ev->height;
1474 ce.border_width = ev->border_width;
1475 ce.above = ev->above;
1476 ce.override_redirect = False;
1479 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1483 count_win(struct workspace *ws, int count_transient)
1488 TAILQ_FOREACH(win, &ws->winlist, entry) {
1489 if (count_transient == 0 && win->floating)
1491 if (count_transient == 0 && win->transient)
1497 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1503 quit(struct swm_region *r, union arg *args)
1505 DNPRINTF(SWM_D_MISC, "quit\n");
1510 unmap_window(struct ws_win *win)
1515 /* don't unmap again */
1516 if (getstate(win->id) == IconicState)
1519 set_win_state(win, IconicState);
1521 XUnmapWindow(display, win->id);
1522 XSetWindowBorder(display, win->id,
1523 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1532 for (i = 0; i < ScreenCount(display); i++)
1533 for (j = 0; j < SWM_WS_MAX; j++)
1534 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1539 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1546 event.display = display; /* Ignored, but what the hell */
1547 event.window = win->id;
1548 event.root = win->s->root;
1549 event.subwindow = None;
1550 event.time = CurrentTime;
1555 event.same_screen = True;
1556 event.keycode = XKeysymToKeycode(display, keysym);
1557 event.state = modifiers;
1559 event.type = KeyPress;
1560 XSendEvent(event.display, event.window, True,
1561 KeyPressMask, (XEvent *)&event);
1563 event.type = KeyRelease;
1564 XSendEvent(event.display, event.window, True,
1565 KeyPressMask, (XEvent *)&event);
1570 restart(struct swm_region *r, union arg *args)
1572 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1574 /* disable alarm because the following code may not be interrupted */
1576 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1577 errx(1, "can't disable alarm");
1582 XCloseDisplay(display);
1583 execvp(start_argv[0], start_argv);
1584 fprintf(stderr, "execvp failed\n");
1590 root_to_region(Window root)
1592 struct swm_region *r = NULL;
1594 int i, x, y, wx, wy;
1597 for (i = 0; i < ScreenCount(display); i++)
1598 if (screens[i].root == root)
1601 if (XQueryPointer(display, screens[i].root,
1602 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1603 /* choose a region based on pointer location */
1604 TAILQ_FOREACH(r, &screens[i].rl, entry)
1605 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1606 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1611 r = TAILQ_FIRST(&screens[i].rl);
1617 find_unmanaged_window(Window id)
1622 for (i = 0; i < ScreenCount(display); i++)
1623 for (j = 0; j < SWM_WS_MAX; j++)
1624 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1632 find_window(Window id)
1635 Window wrr, wpr, *wcr = NULL;
1639 for (i = 0; i < ScreenCount(display); i++)
1640 for (j = 0; j < SWM_WS_MAX; j++)
1641 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1645 /* if we were looking for the parent return that window instead */
1646 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1651 /* ignore not found and root */
1652 if (wpr == 0 || wrr == wpr)
1655 /* look for parent */
1656 for (i = 0; i < ScreenCount(display); i++)
1657 for (j = 0; j < SWM_WS_MAX; j++)
1658 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1666 spawn(struct swm_region *r, union arg *args, int close_fd)
1671 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1674 close(ConnectionNumber(display));
1676 setenv("LD_PRELOAD", SWM_LIB, 1);
1678 if (asprintf(&ret, "%d", r->ws->idx) == -1) {
1682 setenv("_SWM_WS", ret, 1);
1686 if (asprintf(&ret, "%d", getpid()) == -1) {
1690 setenv("_SWM_PID", ret, 1);
1694 if (setsid() == -1) {
1701 * close stdin and stdout to prevent interaction between apps
1702 * and the baraction script
1703 * leave stderr open to record errors
1705 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
1709 dup2(fd, STDIN_FILENO);
1710 dup2(fd, STDOUT_FILENO);
1715 execvp(args->argv[0], args->argv);
1722 spawnterm(struct swm_region *r, union arg *args)
1724 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1727 setenv("_SWM_XTERM_FONTADJ", "", 1);
1733 kill_refs(struct ws_win *win)
1736 struct swm_region *r;
1737 struct workspace *ws;
1742 for (i = 0; i < ScreenCount(display); i++)
1743 TAILQ_FOREACH(r, &screens[i].rl, entry)
1744 for (x = 0; x < SWM_WS_MAX; x++) {
1746 if (win == ws->focus)
1748 if (win == ws->focus_prev)
1749 ws->focus_prev = NULL;
1754 validate_win(struct ws_win *testwin)
1757 struct workspace *ws;
1758 struct swm_region *r;
1759 int i, x, foundit = 0;
1761 if (testwin == NULL)
1764 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1765 TAILQ_FOREACH(r, &screens[i].rl, entry)
1766 for (x = 0; x < SWM_WS_MAX; x++) {
1768 TAILQ_FOREACH(win, &ws->winlist, entry)
1776 validate_ws(struct workspace *testws)
1778 struct swm_region *r;
1779 struct workspace *ws;
1782 /* validate all ws */
1783 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1784 TAILQ_FOREACH(r, &screens[i].rl, entry)
1785 for (x = 0; x < SWM_WS_MAX; x++) {
1794 unfocus_win(struct ws_win *win)
1799 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1803 if (win->ws == NULL)
1806 if (validate_ws(win->ws))
1807 return; /* XXX this gets hit with thunderbird, needs fixing */
1809 if (win->ws->r == NULL)
1812 if (validate_win(win)) {
1817 if (win->ws->focus == win) {
1818 win->ws->focus = NULL;
1819 win->ws->focus_prev = win;
1822 if (validate_win(win->ws->focus)) {
1823 kill_refs(win->ws->focus);
1824 win->ws->focus = NULL;
1826 if (validate_win(win->ws->focus_prev)) {
1827 kill_refs(win->ws->focus_prev);
1828 win->ws->focus_prev = NULL;
1831 /* drain all previous unfocus events */
1832 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
1835 grabbuttons(win, 0);
1836 XSetWindowBorder(display, win->id,
1837 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1839 XChangeProperty(display, win->s->root,
1840 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1841 PropModeReplace, (unsigned char *)&none,1);
1850 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
1852 for (i = 0; i < ScreenCount(display); i++)
1853 for (j = 0; j < SWM_WS_MAX; j++)
1854 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1859 focus_win(struct ws_win *win)
1864 struct ws_win *cfw = NULL;
1867 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1871 if (win->ws == NULL)
1874 if (validate_ws(win->ws))
1875 return; /* XXX this gets hit with thunderbird, needs fixing */
1877 if (validate_win(win)) {
1882 if (validate_win(win)) {
1887 XGetInputFocus(display, &cur_focus, &rr);
1888 if ((cfw = find_window(cur_focus)) != NULL)
1891 win->ws->focus = win;
1893 if (win->ws->r != NULL) {
1894 /* drain all previous focus events */
1895 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
1899 XSetInputFocus(display, win->id,
1900 RevertToParent, CurrentTime);
1901 XMapRaised(display, win->id);
1902 grabbuttons(win, 1);
1903 XSetWindowBorder(display, win->id,
1904 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1905 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS)
1906 XMapRaised(display, win->id);
1908 XChangeProperty(display, win->s->root,
1909 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1910 PropModeReplace, (unsigned char *)&win->id,1);
1913 if (window_name_enabled)
1918 switchws(struct swm_region *r, union arg *args)
1920 int wsid = args->id, unmap_old = 0;
1921 struct swm_region *this_r, *other_r;
1923 struct workspace *new_ws, *old_ws;
1930 old_ws = this_r->ws;
1931 new_ws = &this_r->s->ws[wsid];
1933 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1934 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1937 if (new_ws == NULL || old_ws == NULL)
1939 if (new_ws == old_ws)
1942 other_r = new_ws->r;
1943 if (other_r == NULL) {
1944 /* the other workspace is hidden, hide this one */
1948 /* the other ws is visible in another region, exchange them */
1949 other_r->ws_prior = new_ws;
1950 other_r->ws = old_ws;
1951 old_ws->r = other_r;
1953 this_r->ws_prior = old_ws;
1954 this_r->ws = new_ws;
1958 a.id = SWM_ARG_ID_FOCUSCUR;
1959 focus(new_ws->r, &a);
1962 /* unmap old windows */
1964 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1969 cyclews(struct swm_region *r, union arg *args)
1972 struct swm_screen *s = r->s;
1974 DNPRINTF(SWM_D_WS, "cyclews id %d "
1975 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1976 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1981 case SWM_ARG_ID_CYCLEWS_UP:
1982 if (a.id < SWM_WS_MAX - 1)
1987 case SWM_ARG_ID_CYCLEWS_DOWN:
1991 a.id = SWM_WS_MAX - 1;
1997 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1999 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2003 } while (a.id != r->ws->idx);
2007 priorws(struct swm_region *r, union arg *args)
2011 DNPRINTF(SWM_D_WS, "priorws id %d "
2012 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2013 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2015 if (r->ws_prior == NULL)
2018 a.id = r->ws_prior->idx;
2023 cyclescr(struct swm_region *r, union arg *args)
2025 struct swm_region *rr = NULL;
2029 /* do nothing if we don't have more than one screen */
2030 if (!(ScreenCount(display) > 1 || outputs > 1))
2035 case SWM_ARG_ID_CYCLESC_UP:
2036 rr = TAILQ_NEXT(r, entry);
2038 rr = TAILQ_FIRST(&screens[i].rl);
2040 case SWM_ARG_ID_CYCLESC_DOWN:
2041 rr = TAILQ_PREV(r, swm_region_list, entry);
2043 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2051 /* move mouse to region */
2053 y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
2054 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2056 a.id = SWM_ARG_ID_FOCUSCUR;
2059 if (rr->ws->focus) {
2060 /* move to focus window */
2061 x = rr->ws->focus->g.x + 1;
2062 y = rr->ws->focus->g.y + 1;
2063 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2068 swapwin(struct swm_region *r, union arg *args)
2070 struct ws_win *target, *source;
2071 struct ws_win *cur_focus;
2072 struct ws_win_list *wl;
2075 DNPRINTF(SWM_D_WS, "swapwin id %d "
2076 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
2077 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2079 cur_focus = r->ws->focus;
2080 if (cur_focus == NULL)
2084 wl = &source->ws->winlist;
2087 case SWM_ARG_ID_SWAPPREV:
2088 target = TAILQ_PREV(source, ws_win_list, entry);
2089 TAILQ_REMOVE(wl, cur_focus, entry);
2091 TAILQ_INSERT_TAIL(wl, source, entry);
2093 TAILQ_INSERT_BEFORE(target, source, entry);
2095 case SWM_ARG_ID_SWAPNEXT:
2096 target = TAILQ_NEXT(source, entry);
2097 TAILQ_REMOVE(wl, source, entry);
2099 TAILQ_INSERT_HEAD(wl, source, entry);
2101 TAILQ_INSERT_AFTER(wl, target, source, entry);
2103 case SWM_ARG_ID_SWAPMAIN:
2104 target = TAILQ_FIRST(wl);
2105 if (target == source) {
2106 if (source->ws->focus_prev != NULL &&
2107 source->ws->focus_prev != target)
2109 source = source->ws->focus_prev;
2113 if (target == NULL || source == NULL)
2115 source->ws->focus_prev = target;
2116 TAILQ_REMOVE(wl, target, entry);
2117 TAILQ_INSERT_BEFORE(source, target, entry);
2118 TAILQ_REMOVE(wl, source, entry);
2119 TAILQ_INSERT_HEAD(wl, source, entry);
2121 case SWM_ARG_ID_MOVELAST:
2122 TAILQ_REMOVE(wl, source, entry);
2123 TAILQ_INSERT_TAIL(wl, source, entry);
2126 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
2134 focus_prev(struct ws_win *win)
2136 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2137 struct ws_win *cur_focus = NULL;
2138 struct ws_win_list *wl = NULL;
2139 struct workspace *ws = NULL;
2141 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
2143 if (!(win && win->ws))
2148 cur_focus = ws->focus;
2149 winlostfocus = cur_focus;
2151 /* pickle, just focus on whatever */
2152 if (cur_focus == NULL) {
2153 /* use prev_focus if valid */
2154 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2155 find_window(WINID(ws->focus_prev)))
2156 winfocus = ws->focus_prev;
2157 if (winfocus == NULL)
2158 winfocus = TAILQ_FIRST(wl);
2162 /* if transient focus on parent */
2163 if (cur_focus->transient) {
2164 winfocus = find_window(cur_focus->transient);
2168 /* if in max_stack try harder */
2169 if (ws->cur_layout->flags & SWM_L_FOCUSPREV) {
2170 if (cur_focus != ws->focus_prev)
2171 winfocus = ws->focus_prev;
2172 else if (cur_focus != ws->focus)
2173 winfocus = ws->focus;
2175 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2180 if (cur_focus == win)
2181 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2182 if (winfocus == NULL)
2183 winfocus = TAILQ_LAST(wl, ws_win_list);
2184 if (winfocus == NULL || winfocus == win)
2185 winfocus = TAILQ_NEXT(cur_focus, entry);
2187 if (winfocus == winlostfocus || winfocus == NULL)
2190 focus_magic(winfocus);
2194 focus(struct swm_region *r, union arg *args)
2196 struct ws_win *winfocus = NULL, *winlostfocus = NULL, *head;
2197 struct ws_win *cur_focus = NULL;
2198 struct ws_win_list *wl = NULL;
2199 struct workspace *ws = NULL;
2204 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
2206 /* treat FOCUS_CUR special */
2207 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2208 if (r->ws->focus && r->ws->focus->iconic == 0)
2209 winfocus = r->ws->focus;
2210 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2211 winfocus = r->ws->focus_prev;
2213 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2214 if (winfocus->iconic == 0)
2217 focus_magic(winfocus);
2221 if ((cur_focus = r->ws->focus) == NULL)
2226 winlostfocus = cur_focus;
2229 case SWM_ARG_ID_FOCUSPREV:
2230 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2232 head = TAILQ_LAST(wl, ws_win_list);
2234 if (WINID(winfocus) == cur_focus->transient) {
2235 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2237 head = TAILQ_LAST(wl, ws_win_list);
2242 if (winfocus && winfocus->iconic) {
2243 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
2244 if (winfocus->iconic == 0)
2249 case SWM_ARG_ID_FOCUSNEXT:
2250 head = TAILQ_NEXT(cur_focus, entry);
2252 head = TAILQ_FIRST(wl);
2256 if (winfocus && winfocus->iconic) {
2257 TAILQ_FOREACH(winfocus, wl, entry)
2258 if (winfocus->iconic == 0)
2263 case SWM_ARG_ID_FOCUSMAIN:
2264 winfocus = TAILQ_FIRST(wl);
2265 if (winfocus == cur_focus)
2266 winfocus = cur_focus->ws->focus_prev;
2273 if (winfocus == winlostfocus || winfocus == NULL)
2276 focus_magic(winfocus);
2280 cycle_layout(struct swm_region *r, union arg *args)
2282 struct workspace *ws = r->ws;
2283 struct ws_win *winfocus;
2286 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2288 winfocus = ws->focus;
2291 if (ws->cur_layout->l_stack == NULL)
2292 ws->cur_layout = &layouts[0];
2295 a.id = SWM_ARG_ID_FOCUSCUR;
2301 stack_config(struct swm_region *r, union arg *args)
2303 struct workspace *ws = r->ws;
2305 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
2308 if (ws->cur_layout->l_config != NULL)
2309 ws->cur_layout->l_config(ws, args->id);
2311 if (args->id != SWM_ARG_ID_STACKINIT);
2317 struct swm_geometry g;
2318 struct swm_region *r;
2321 DNPRINTF(SWM_D_STACK, "stack\n");
2323 for (i = 0; i < ScreenCount(display); i++) {
2325 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2326 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
2327 "(screen %d, region %d)\n", r->ws->idx, i, j++);
2329 /* start with screen geometry, adjust for bar */
2331 g.w -= 2 * border_width;
2332 g.h -= 2 * border_width;
2338 r->ws->cur_layout->l_stack(r->ws, &g);
2339 /* save r so we can track region changes */
2348 store_float_geom(struct ws_win *win, struct swm_region *r)
2350 /* retain window geom and region geom */
2351 win->g_float.x = win->g.x;
2352 win->g_float.y = win->g.y;
2353 win->g_float.w = win->g.w;
2354 win->g_float.h = win->g.h;
2355 win->rg_float.x = r->g.x;
2356 win->rg_float.y = r->g.y;
2357 win->rg_float.w = r->g.w;
2358 win->rg_float.h = r->g.h;
2359 win->g_floatvalid = 1;
2363 stack_floater(struct ws_win *win, struct swm_region *r)
2371 bzero(&wc, sizeof wc);
2372 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2375 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2376 * geom on ws switches or return from max mode
2379 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2380 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2382 * use stored g and rg to set relative position and size
2383 * as in old region or before max stack mode
2385 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2386 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2387 win->g.w = win->g_float.w;
2388 win->g.h = win->g_float.h;
2389 win->g_floatvalid = 0;
2392 win->floatmaxed = 0;
2394 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
2395 (win->g.h >= HEIGHT(r)))
2396 wc.border_width = 0;
2398 wc.border_width = border_width;
2399 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2400 win->g.w = (double)WIDTH(r) * dialog_ratio;
2401 win->g.h = (double)HEIGHT(r) * dialog_ratio;
2406 * floaters and transients are auto-centred unless moved
2409 win->g.x = r->g.x + (WIDTH(r) - win->g.w) / 2 - border_width;
2410 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) / 2 - border_width;
2413 /* win can be outside r if new r smaller than old r */
2414 /* Ensure top left corner inside r (move probs otherwise) */
2415 if (win->g.x < r->g.x - border_width)
2416 win->g.x = r->g.x - border_width;
2417 if (win->g.x > r->g.x + r->g.w - 1)
2418 win->g.x = (win->g.w > r->g.w) ? r->g.x :
2419 (r->g.x + r->g.w - win->g.w - 2 * border_width);
2420 if (win->g.y < r->g.y - border_width)
2421 win->g.y = r->g.y - border_width;
2422 if (win->g.y > r->g.y + r->g.h - 1)
2423 win->g.y = (win->g.h > r->g.h) ? r->g.y :
2424 (r->g.y + r->g.h - win->g.h - 2 * border_width);
2428 wc.width = win->g.w;
2429 wc.height = win->g.h;
2432 * Retain floater and transient geometry for correct positioning
2433 * when ws changes region
2435 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2436 store_float_geom(win, r);
2438 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
2439 win->id, wc.x, wc.y, wc.width, wc.height);
2441 XConfigureWindow(display, win->id, mask, &wc);
2445 * Send keystrokes to terminal to decrease/increase the font size as the
2446 * window size changes.
2449 adjust_font(struct ws_win *win)
2451 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2452 win->floating || win->transient)
2455 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2456 win->g.w / win->sh.width_inc < term_width &&
2457 win->font_steps < SWM_MAX_FONT_STEPS) {
2458 win->font_size_boundary[win->font_steps] =
2459 (win->sh.width_inc * term_width) + win->sh.base_width;
2462 win->last_inc = win->sh.width_inc;
2463 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2464 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2465 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
2468 win->last_inc = win->sh.width_inc;
2469 fake_keypress(win, XK_KP_Add, ShiftMask);
2473 #define SWAPXY(g) do { \
2475 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2476 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2479 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2482 XWindowAttributes wa;
2483 struct swm_geometry win_g, r_g = *g;
2484 struct ws_win *win, *fs_win = 0;
2485 int i, j, s, stacks;
2486 int w_inc = 1, h_inc, w_base = 1, h_base;
2487 int hrh, extra = 0, h_slice, last_h = 0;
2488 int split, colno, winno, mwin, msize, mscale;
2489 int remain, missing, v_slice, reconfigure;
2492 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2493 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2495 winno = count_win(ws, 0);
2496 if (winno == 0 && count_win(ws, 1) == 0)
2499 TAILQ_FOREACH(win, &ws->winlist, entry)
2500 if (win->transient == 0 && win->floating == 0 && win->iconic == 0)
2507 w_inc = win->sh.width_inc;
2508 w_base = win->sh.base_width;
2509 mwin = ws->l_state.horizontal_mwin;
2510 mscale = ws->l_state.horizontal_msize;
2511 stacks = ws->l_state.horizontal_stacks;
2514 w_inc = win->sh.height_inc;
2515 w_base = win->sh.base_height;
2516 mwin = ws->l_state.vertical_mwin;
2517 mscale = ws->l_state.vertical_msize;
2518 stacks = ws->l_state.vertical_stacks;
2522 if (stacks > winno - mwin)
2523 stacks = winno - mwin;
2527 h_slice = r_g.h / SWM_H_SLICE;
2528 if (mwin && winno > mwin) {
2529 v_slice = r_g.w / SWM_V_SLICE;
2533 win_g.w = v_slice * mscale;
2535 if (w_inc > 1 && w_inc < v_slice) {
2536 /* adjust for window's requested size increment */
2537 remain = (win_g.w - w_base) % w_inc;
2538 missing = w_inc - remain;
2545 win_g.x += r_g.w - msize;
2548 colno = split = winno / stacks;
2549 win_g.w = ((r_g.w - (stacks * 2 * border_width) + 2 * border_width) / stacks);
2551 hrh = r_g.h / colno;
2552 extra = r_g.h - (colno * hrh);
2553 win_g.h = hrh - 2 * border_width;
2555 /* stack all the tiled windows */
2556 i = j = 0, s = stacks;
2557 TAILQ_FOREACH(win, &ws->winlist, entry) {
2558 if (win->transient != 0 || win->floating != 0)
2560 if (win->iconic != 0)
2563 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2568 if (split && i == split) {
2569 colno = (winno - mwin) / stacks;
2570 if (s <= (winno - mwin) % stacks)
2572 split = split + colno;
2573 hrh = (r_g.h / colno);
2574 extra = r_g.h - (colno * hrh);
2578 win_g.x += win_g.w + 2 * border_width;
2579 win_g.w = (r_g.w - msize - (stacks * 2 * border_width)) / stacks;
2581 win_g.w += (r_g.w - msize - (stacks * 2 * border_width)) %
2586 win_g.h = hrh - 2 * border_width;
2588 h_inc = win->sh.width_inc;
2589 h_base = win->sh.base_width;
2591 h_inc = win->sh.height_inc;
2592 h_base = win->sh.base_height;
2594 if (j == colno - 1) {
2595 win_g.h = hrh + extra;
2596 } else if (h_inc > 1 && h_inc < h_slice) {
2597 /* adjust for window's requested size increment */
2598 remain = (win_g.h - h_base) % h_inc;
2599 missing = h_inc - remain;
2601 if (missing <= extra || j == 0) {
2613 win_g.y += last_h + 2 * border_width;
2615 bzero(&wc, sizeof wc);
2616 if (disable_border && bar_enabled == 0 && winno == 1){
2617 wc.border_width = 0;
2618 win_g.w += 2 * border_width;
2619 win_g.h += 2 * border_width;
2621 wc.border_width = border_width;
2624 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2625 win->g.w != win_g.h || win->g.h != win_g.w) {
2627 win->g.x = wc.x = win_g.y;
2628 win->g.y = wc.y = win_g.x;
2629 win->g.w = wc.width = win_g.h;
2630 win->g.h = wc.height = win_g.w;
2633 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2634 win->g.w != win_g.w || win->g.h != win_g.h) {
2636 win->g.x = wc.x = win_g.x;
2637 win->g.y = wc.y = win_g.y;
2638 win->g.w = wc.width = win_g.w;
2639 win->g.h = wc.height = win_g.h;
2644 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2645 XConfigureWindow(display, win->id, mask, &wc);
2648 if (XGetWindowAttributes(display, win->id, &wa))
2649 if (wa.map_state == IsUnmapped)
2650 XMapRaised(display, win->id);
2658 /* now, stack all the floaters and transients */
2659 TAILQ_FOREACH(win, &ws->winlist, entry) {
2660 if (win->transient == 0 && win->floating == 0)
2662 if (win->iconic == 1)
2664 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2669 stack_floater(win, ws->r);
2670 XMapRaised(display, win->id);
2674 stack_floater(fs_win, ws->r);
2675 XMapRaised(display, fs_win->id);
2680 vertical_config(struct workspace *ws, int id)
2682 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2685 case SWM_ARG_ID_STACKRESET:
2686 case SWM_ARG_ID_STACKINIT:
2687 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2688 ws->l_state.vertical_mwin = 1;
2689 ws->l_state.vertical_stacks = 1;
2691 case SWM_ARG_ID_MASTERSHRINK:
2692 if (ws->l_state.vertical_msize > 1)
2693 ws->l_state.vertical_msize--;
2695 case SWM_ARG_ID_MASTERGROW:
2696 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2697 ws->l_state.vertical_msize++;
2699 case SWM_ARG_ID_MASTERADD:
2700 ws->l_state.vertical_mwin++;
2702 case SWM_ARG_ID_MASTERDEL:
2703 if (ws->l_state.vertical_mwin > 0)
2704 ws->l_state.vertical_mwin--;
2706 case SWM_ARG_ID_STACKINC:
2707 ws->l_state.vertical_stacks++;
2709 case SWM_ARG_ID_STACKDEC:
2710 if (ws->l_state.vertical_stacks > 1)
2711 ws->l_state.vertical_stacks--;
2719 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2721 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2723 stack_master(ws, g, 0, 0);
2727 horizontal_config(struct workspace *ws, int id)
2729 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2732 case SWM_ARG_ID_STACKRESET:
2733 case SWM_ARG_ID_STACKINIT:
2734 ws->l_state.horizontal_mwin = 1;
2735 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2736 ws->l_state.horizontal_stacks = 1;
2738 case SWM_ARG_ID_MASTERSHRINK:
2739 if (ws->l_state.horizontal_msize > 1)
2740 ws->l_state.horizontal_msize--;
2742 case SWM_ARG_ID_MASTERGROW:
2743 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2744 ws->l_state.horizontal_msize++;
2746 case SWM_ARG_ID_MASTERADD:
2747 ws->l_state.horizontal_mwin++;
2749 case SWM_ARG_ID_MASTERDEL:
2750 if (ws->l_state.horizontal_mwin > 0)
2751 ws->l_state.horizontal_mwin--;
2753 case SWM_ARG_ID_STACKINC:
2754 ws->l_state.horizontal_stacks++;
2756 case SWM_ARG_ID_STACKDEC:
2757 if (ws->l_state.horizontal_stacks > 1)
2758 ws->l_state.horizontal_stacks--;
2766 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2768 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
2770 stack_master(ws, g, 1, 0);
2773 /* fullscreen view */
2775 max_stack(struct workspace *ws, struct swm_geometry *g)
2778 struct swm_geometry gg = *g;
2779 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2783 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2788 winno = count_win(ws, 0);
2789 if (winno == 0 && count_win(ws, 1) == 0)
2792 TAILQ_FOREACH(win, &ws->winlist, entry) {
2793 if (win->transient) {
2795 parent = find_window(win->transient);
2799 if (win->floating && win->floatmaxed == 0 ) {
2801 * retain geometry for retrieval on exit from
2804 store_float_geom(win, ws->r);
2805 win->floatmaxed = 1;
2808 /* only reconfigure if necessary */
2809 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2811 bzero(&wc, sizeof wc);
2812 win->g.x = wc.x = gg.x;
2813 win->g.y = wc.y = gg.y;
2815 wc.border_width = border_width;
2816 win->g.w = wc.width = gg.w;
2817 win->g.h = wc.height = gg.h;
2819 wc.border_width = 0;
2820 win->g.w = wc.width = gg.w + 2 * border_width;
2821 win->g.h = wc.height = gg.h + 2 * border_width;
2823 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2824 XConfigureWindow(display, win->id, mask, &wc);
2826 /* unmap only if we don't have multi screen */
2827 if (win != ws->focus)
2828 if (!(ScreenCount(display) > 1 || outputs > 1))
2832 /* put the last transient on top */
2835 XMapRaised(display, parent->id);
2836 stack_floater(wintrans, ws->r);
2837 focus_magic(wintrans);
2842 send_to_ws(struct swm_region *r, union arg *args)
2844 int wsid = args->id;
2845 struct ws_win *win = win;
2846 struct workspace *ws, *nws;
2847 Atom ws_idx_atom = 0;
2848 unsigned char ws_idx_str[SWM_PROPLEN];
2857 if (win->ws->idx == wsid)
2860 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2863 nws = &win->s->ws[wsid];
2865 a.id = SWM_ARG_ID_FOCUSPREV;
2868 TAILQ_REMOVE(&ws->winlist, win, entry);
2869 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2872 /* Try to update the window's workspace property */
2873 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2875 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2876 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2878 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2879 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2886 iconify(struct swm_region *r, union arg *args)
2890 if (r->ws->focus == NULL)
2892 unmap_window(r->ws->focus);
2893 update_iconic(r->ws->focus, 1);
2895 r->ws->focus = NULL;
2896 a.id = SWM_ARG_ID_FOCUSCUR;
2901 get_win_name(Display *dpy, Window win, Atom wname, Atom stype,
2902 unsigned long *slen)
2905 unsigned long nitems, nbytes, nextra;
2906 unsigned char *prop = NULL;
2909 status = XGetWindowProperty(dpy, win, wname, 0L, 0L, False, stype,
2910 &rettype, &retfmt, &nitems, &nbytes, &prop);
2911 if (status != Success)
2915 status = XGetWindowProperty(dpy, win, wname, 0L, nbytes, False,
2916 stype, &rettype, &retfmt, &nitems, &nextra, &prop);
2917 if (status != Success) {
2921 if (rettype != stype) {
2930 uniconify(struct swm_region *r, union arg *args)
2938 DNPRINTF(SWM_D_MISC, "uniconify\n");
2940 if (r && r->ws == NULL)
2943 /* make sure we have anything to uniconify */
2944 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
2945 if (win->ws == NULL)
2946 continue; /* should never happen */
2947 if (win->iconic == 0)
2956 spawn_select(r, args, "uniconify", &searchpid);
2958 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
2961 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
2962 if (win->ws == NULL)
2963 continue; /* should never happen */
2964 if (win->iconic == 0)
2967 name = get_win_name(display, win->id, a_wmname, a_string,
2971 fprintf(lfile, "%s.%lu\n", name, win->id);
2978 #define MAX_RESP_LEN 1024
2981 search_do_resp(void)
2985 char *name, *resp, *s;
2988 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
2993 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
2994 fprintf(stderr, "search: calloc\n");
2998 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3000 fprintf(stderr, "search: read error: %s\n", strerror(errno));
3003 resp[rbytes] = '\0';
3006 DNPRINTF(SWM_D_MISC, "search_do_resp: resp %s\n", resp);
3007 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3008 if (win->iconic == 0)
3010 name = get_win_name(display, win->id, a_wmname, a_string, &len);
3013 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3018 if (strncmp(s, resp, len) == 0) {
3019 /* XXX this should be a callback to generalize */
3020 update_iconic(win, 0);
3031 wkill(struct swm_region *r, union arg *args)
3033 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
3035 if (r->ws->focus == NULL)
3038 if (args->id == SWM_ARG_ID_KILLWINDOW)
3039 XKillClient(display, r->ws->focus->id);
3041 if (r->ws->focus->can_delete)
3042 client_msg(r->ws->focus, adelete);
3047 floating_toggle_win(struct ws_win *win)
3049 struct swm_region *r;
3059 /* reject floating toggles in max stack mode */
3060 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3063 if (win->floating) {
3064 if (!win->floatmaxed) {
3065 /* retain position for refloat */
3066 store_float_geom(win, r);
3070 if (win->g_floatvalid) {
3071 /* refloat at last floating relative position */
3072 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
3073 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
3074 win->g.w = win->g_float.w;
3075 win->g.h = win->g_float.h;
3080 ewmh_update_actions(win);
3086 floating_toggle(struct swm_region *r, union arg *args)
3088 struct ws_win *win = r->ws->focus;
3094 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3095 _NET_WM_STATE_TOGGLE);
3099 if (win == win->ws->focus) {
3100 a.id = SWM_ARG_ID_FOCUSCUR;
3101 focus(win->ws->r, &a);
3106 resize_window(struct ws_win *win, int center)
3110 struct swm_region *r;
3112 r = root_to_region(win->wa.root);
3113 bzero(&wc, sizeof wc);
3114 mask = CWBorderWidth | CWWidth | CWHeight;
3115 wc.border_width = border_width;
3116 wc.width = win->g.w;
3117 wc.height = win->g.h;
3118 if (center == SWM_ARG_ID_CENTER) {
3119 wc.x = (WIDTH(r) - win->g.w) / 2 - border_width;
3120 wc.y = (HEIGHT(r) - win->g.h) / 2 - border_width;
3124 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
3125 win->id, wc.x, wc.y, wc.width, wc.height);
3127 XConfigureWindow(display, win->id, mask, &wc);
3131 resize(struct ws_win *win, union arg *args)
3135 struct swm_region *r = win->ws->r;
3140 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
3141 win->id, win->floating, win->transient);
3143 if (!(win->transient != 0 || win->floating != 0))
3146 /* reject resizes in max mode for floaters (transient ok) */
3147 if (win->floatmaxed)
3151 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3153 /* raise the window = move to last in window list */
3154 a.id = SWM_ARG_ID_MOVELAST;
3158 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3159 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3162 /* place pointer at bottom left corner or nearest point inside r */
3163 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
3164 relx = win->g.w - 1;
3166 relx = r->g.x + r->g.w - win->g.x - 1;
3168 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
3169 rely = win->g.h - 1;
3171 rely = r->g.y + r->g.h - win->g.y - 1;
3173 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
3175 XMaskEvent(display, MOUSEMASK | ExposureMask |
3176 SubstructureRedirectMask, &ev);
3178 case ConfigureRequest:
3181 handler[ev.type](&ev);
3184 /* do not allow resize outside of region */
3185 if ( ev.xmotion.x_root < r->g.x ||
3186 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3187 ev.xmotion.y_root < r->g.y ||
3188 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3191 if (ev.xmotion.x <= 1)
3193 if (ev.xmotion.y <= 1)
3195 win->g.w = ev.xmotion.x + 1;
3196 win->g.h = ev.xmotion.y + 1;
3198 /* not free, don't sync more than 120 times / second */
3199 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3200 time = ev.xmotion.time;
3201 XSync(display, False);
3202 resize_window(win, args->id);
3206 } while (ev.type != ButtonRelease);
3208 XSync(display, False);
3209 resize_window(win, args->id);
3211 store_float_geom(win,r);
3213 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
3215 XUngrabPointer(display, CurrentTime);
3218 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
3222 move_window(struct ws_win *win)
3226 struct swm_region *r;
3228 r = root_to_region(win->wa.root);
3229 bzero(&wc, sizeof wc);
3233 wc.border_width = border_width;
3235 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
3236 win->id, wc.x, wc.y, wc.width, wc.height);
3238 XConfigureWindow(display, win->id, mask, &wc);
3242 move(struct ws_win *win, union arg *args)
3246 struct swm_region *r = win->ws->r;
3249 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
3250 win->id, win->floating, win->transient);
3252 /* in max_stack mode should only move transients */
3253 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
3257 if (win->floating == 0 && !win->transient) {
3259 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3262 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3265 /* raise the window = move to last in window list */
3266 a.id = SWM_ARG_ID_MOVELAST;
3270 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3271 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3273 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3275 XMaskEvent(display, MOUSEMASK | ExposureMask |
3276 SubstructureRedirectMask, &ev);
3278 case ConfigureRequest:
3281 handler[ev.type](&ev);
3284 /* don't allow to move window origin out of region */
3285 if ( ev.xmotion.x_root < r->g.x ||
3286 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3287 ev.xmotion.y_root < r->g.y ||
3288 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3291 win->g.x = ev.xmotion.x_root - border_width;
3292 win->g.y = ev.xmotion.y_root - border_width;
3294 /* not free, don't sync more than 120 times / second */
3295 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3296 time = ev.xmotion.time;
3297 XSync(display, False);
3302 } while (ev.type != ButtonRelease);
3304 XSync(display, False);
3307 store_float_geom(win,r);
3308 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3309 XUngrabPointer(display, CurrentTime);
3312 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
3315 /* user/key callable function IDs */
3372 kf_dumpwins, /* MUST BE LAST */
3376 /* key definitions */
3378 dummykeyfunc(struct swm_region *r, union arg *args)
3383 legacyfunc(struct swm_region *r, union arg *args)
3388 char name[SWM_FUNCNAME_LEN];
3389 void (*func)(struct swm_region *r, union arg *);
3391 } keyfuncs[kf_invalid + 1] = {
3392 /* name function argument */
3393 { "cycle_layout", cycle_layout, {0} },
3394 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
3395 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
3396 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
3397 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
3398 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
3399 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
3400 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
3401 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
3402 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
3403 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
3404 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
3405 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
3406 { "spawn_term", spawnterm, {.argv = spawn_term} },
3407 { "spawn_menu", legacyfunc, {0} },
3408 { "quit", quit, {0} },
3409 { "restart", restart, {0} },
3410 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
3411 { "ws_1", switchws, {.id = 0} },
3412 { "ws_2", switchws, {.id = 1} },
3413 { "ws_3", switchws, {.id = 2} },
3414 { "ws_4", switchws, {.id = 3} },
3415 { "ws_5", switchws, {.id = 4} },
3416 { "ws_6", switchws, {.id = 5} },
3417 { "ws_7", switchws, {.id = 6} },
3418 { "ws_8", switchws, {.id = 7} },
3419 { "ws_9", switchws, {.id = 8} },
3420 { "ws_10", switchws, {.id = 9} },
3421 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
3422 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
3423 { "ws_prior", priorws, {0} },
3424 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
3425 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
3426 { "mvws_1", send_to_ws, {.id = 0} },
3427 { "mvws_2", send_to_ws, {.id = 1} },
3428 { "mvws_3", send_to_ws, {.id = 2} },
3429 { "mvws_4", send_to_ws, {.id = 3} },
3430 { "mvws_5", send_to_ws, {.id = 4} },
3431 { "mvws_6", send_to_ws, {.id = 5} },
3432 { "mvws_7", send_to_ws, {.id = 6} },
3433 { "mvws_8", send_to_ws, {.id = 7} },
3434 { "mvws_9", send_to_ws, {.id = 8} },
3435 { "mvws_10", send_to_ws, {.id = 9} },
3436 { "bar_toggle", bar_toggle, {0} },
3437 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
3438 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
3439 { "screenshot_all", legacyfunc, {0} },
3440 { "screenshot_wind", legacyfunc, {0} },
3441 { "float_toggle", floating_toggle,{0} },
3442 { "version", version, {0} },
3443 { "spawn_lock", legacyfunc, {0} },
3444 { "spawn_initscr", legacyfunc, {0} },
3445 { "spawn_custom", dummykeyfunc, {0} },
3446 { "iconify", iconify, {0} },
3447 { "uniconify", uniconify, {0} },
3448 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
3449 { "invalid key func", NULL, {0} },
3454 enum keyfuncid funcid;
3457 int keys_size = 0, keys_length = 0;
3458 struct key *keys = NULL;
3461 enum { client_click, root_click };
3463 unsigned int action;
3465 unsigned int button;
3466 void (*func)(struct ws_win *, union arg *);
3469 /* action key mouse button func args */
3470 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
3471 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
3472 { client_click, MODKEY, Button1, move, {0} },
3476 update_modkey(unsigned int mod)
3481 for (i = 0; i < keys_length; i++)
3482 if (keys[i].mod & ShiftMask)
3483 keys[i].mod = mod | ShiftMask;
3487 for (i = 0; i < LENGTH(buttons); i++)
3488 if (buttons[i].mask & ShiftMask)
3489 buttons[i].mask = mod | ShiftMask;
3491 buttons[i].mask = mod;
3501 int spawns_size = 0, spawns_length = 0;
3502 struct spawn_prog *spawns = NULL;
3505 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
3508 struct spawn_prog *prog = NULL;
3510 char *ap, **real_args;
3512 DNPRINTF(SWM_D_SPAWN, "spawn_expand %s\n", spawn_name);
3515 for (i = 0; i < spawns_length; i++) {
3516 if (!strcasecmp(spawn_name, spawns[i].name))
3520 fprintf(stderr, "spawn_custom: program %s not found\n",
3525 /* make room for expanded args */
3526 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
3527 err(1, "spawn_custom: calloc real_args");
3529 /* expand spawn_args into real_args */
3530 for (i = 0; i < prog->argc; i++) {
3532 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
3533 if (!strcasecmp(ap, "$bar_border")) {
3535 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
3537 err(1, "spawn_custom border color");
3538 } else if (!strcasecmp(ap, "$bar_color")) {
3540 strdup(r->s->c[SWM_S_COLOR_BAR].name))
3542 err(1, "spawn_custom bar color");
3543 } else if (!strcasecmp(ap, "$bar_font")) {
3544 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
3546 err(1, "spawn_custom bar fonts");
3547 } else if (!strcasecmp(ap, "$bar_font_color")) {
3549 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
3551 err(1, "spawn_custom color font");
3552 } else if (!strcasecmp(ap, "$color_focus")) {
3554 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
3556 err(1, "spawn_custom color focus");
3557 } else if (!strcasecmp(ap, "$color_unfocus")) {
3559 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
3561 err(1, "spawn_custom color unfocus");
3563 /* no match --> copy as is */
3564 if ((real_args[i] = strdup(ap)) == NULL)
3565 err(1, "spawn_custom strdup(ap)");
3567 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
3572 if ((swm_debug & SWM_D_SPAWN) != 0) {
3573 fprintf(stderr, "spawn_custom: result = ");
3574 for (i = 0; i < prog->argc; i++)
3575 fprintf(stderr, "\"%s\" ", real_args[i]);
3576 fprintf(stderr, "\n");
3579 *ret_args = real_args;
3580 return (prog->argc);
3584 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
3590 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3596 for (i = 0; i < spawn_argc; i++)
3602 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
3608 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3612 if (pipe(select_list_pipe) == -1)
3613 err(1, "pipe error");
3614 if (pipe(select_resp_pipe) == -1)
3615 err(1, "pipe error");
3617 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
3618 err(1, "could not disable SIGPIPE");
3619 switch (*pid = fork()) {
3621 err(1, "cannot fork");
3624 if (dup2(select_list_pipe[0], 0) == -1)
3626 if (dup2(select_resp_pipe[1], 1) == -1)
3628 close(select_list_pipe[1]);
3629 close(select_resp_pipe[0]);
3632 default: /* parent */
3633 close(select_list_pipe[0]);
3634 close(select_resp_pipe[1]);
3638 for (i = 0; i < spawn_argc; i++)
3644 setspawn(struct spawn_prog *prog)
3648 if (prog == NULL || prog->name == NULL)
3652 for (i = 0; i < spawns_length; i++) {
3653 if (!strcmp(spawns[i].name, prog->name)) {
3655 if (prog->argv == NULL) {
3657 DNPRINTF(SWM_D_SPAWN,
3658 "setspawn: delete #%d %s\n",
3660 free(spawns[i].name);
3661 for (j = 0; j < spawns[i].argc; j++)
3662 free(spawns[i].argv[j]);
3663 free(spawns[i].argv);
3664 j = spawns_length - 1;
3666 spawns[i] = spawns[j];
3671 DNPRINTF(SWM_D_SPAWN,
3672 "setspawn: replace #%d %s\n",
3674 free(spawns[i].name);
3675 for (j = 0; j < spawns[i].argc; j++)
3676 free(spawns[i].argv[j]);
3677 free(spawns[i].argv);
3680 /* found case handled */
3686 if (prog->argv == NULL) {
3688 "error: setspawn: cannot find program %s", prog->name);
3693 /* not found: add */
3694 if (spawns_size == 0 || spawns == NULL) {
3696 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
3697 spawns = malloc((size_t)spawns_size *
3698 sizeof(struct spawn_prog));
3699 if (spawns == NULL) {
3700 fprintf(stderr, "setspawn: malloc failed\n");
3704 } else if (spawns_length == spawns_size) {
3706 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
3707 spawns = realloc(spawns, (size_t)spawns_size *
3708 sizeof(struct spawn_prog));
3709 if (spawns == NULL) {
3710 fprintf(stderr, "setspawn: realloc failed\n");
3716 if (spawns_length < spawns_size) {
3717 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
3718 spawns_length, prog->name);
3719 i = spawns_length++;
3722 fprintf(stderr, "spawns array problem?\n");
3723 if (spawns == NULL) {
3724 fprintf(stderr, "spawns array is NULL!\n");
3732 setconfspawn(char *selector, char *value, int flags)
3734 struct spawn_prog *prog;
3735 char *vp, *cp, *word;
3737 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
3738 if ((prog = calloc(1, sizeof *prog)) == NULL)
3739 err(1, "setconfspawn: calloc prog");
3740 prog->name = strdup(selector);
3741 if (prog->name == NULL)
3742 err(1, "setconfspawn prog->name");
3743 if ((cp = vp = strdup(value)) == NULL)
3744 err(1, "setconfspawn: strdup(value) ");
3745 while ((word = strsep(&cp, " \t")) != NULL) {
3746 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
3748 cp += (long)strspn(cp, " \t");
3749 if (strlen(word) > 0) {
3751 prog->argv = realloc(prog->argv,
3752 prog->argc * sizeof(char *));
3753 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
3754 err(1, "setconfspawn: strdup");
3761 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
3768 setconfspawn("term", "xterm", 0);
3769 setconfspawn("screenshot_all", "screenshot.sh full", 0);
3770 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
3771 setconfspawn("lock", "xlock", 0);
3772 setconfspawn("initscr", "initscreen.sh", 0);
3773 setconfspawn("menu", "dmenu_run"
3776 " -nf $bar_font_color"
3778 " -sf $bar_color", 0);
3779 setconfspawn("uniconify", "dmenu"
3783 " -nf $bar_font_color"
3785 " -sf $bar_color", 0);
3789 #define SWM_MODNAME_SIZE 32
3790 #define SWM_KEY_WS "\n+ \t"
3792 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
3796 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
3797 if (mod == NULL || ks == NULL) {
3798 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
3801 if (keystr == NULL || strlen(keystr) == 0) {
3802 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
3808 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
3809 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
3811 cp += (long)strspn(cp, SWM_KEY_WS);
3812 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
3814 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
3816 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
3818 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
3820 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
3822 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
3824 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
3825 *mod |= ControlMask;
3827 *ks = XStringToKeysym(name);
3828 XConvertCase(*ks, ks, &uks);
3829 if (ks == NoSymbol) {
3831 "parsekeys: invalid key %s\n",
3837 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
3842 strdupsafe(char *str)
3847 return (strdup(str));
3851 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
3854 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
3855 keyfuncs[kfid].name, spawn_name);
3857 for (i = 0; i < keys_length; i++) {
3858 if (keys[i].mod == mod && keys[i].keysym == ks) {
3859 if (kfid == kf_invalid) {
3862 "setkeybinding: delete #%d %s\n",
3863 i, keyfuncs[keys[i].funcid].name);
3864 free(keys[i].spawn_name);
3865 j = keys_length - 1;
3869 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3872 /* found: replace */
3874 "setkeybinding: replace #%d %s %s\n",
3875 i, keyfuncs[keys[i].funcid].name,
3877 free(keys[i].spawn_name);
3879 keys[i].keysym = ks;
3880 keys[i].funcid = kfid;
3881 keys[i].spawn_name = strdupsafe(spawn_name);
3882 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3887 if (kfid == kf_invalid) {
3889 "error: setkeybinding: cannot find mod/key combination");
3890 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3893 /* not found: add */
3894 if (keys_size == 0 || keys == NULL) {
3896 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
3897 keys = malloc((size_t)keys_size * sizeof(struct key));
3899 fprintf(stderr, "malloc failed\n");
3903 } else if (keys_length == keys_size) {
3905 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
3906 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
3908 fprintf(stderr, "realloc failed\n");
3913 if (keys_length < keys_size) {
3915 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
3916 j, keyfuncs[kfid].name, spawn_name);
3918 keys[j].keysym = ks;
3919 keys[j].funcid = kfid;
3920 keys[j].spawn_name = strdupsafe(spawn_name);
3922 fprintf(stderr, "keys array problem?\n");
3924 fprintf(stderr, "keys array problem\n");
3928 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3932 setconfbinding(char *selector, char *value, int flags)
3934 enum keyfuncid kfid;
3938 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
3939 if (selector == NULL) {
3940 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
3941 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3943 setkeybinding(mod, ks, kfid, NULL);
3948 /* search by key function name */
3949 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
3950 if (strncasecmp(selector, keyfuncs[kfid].name,
3951 SWM_FUNCNAME_LEN) == 0) {
3952 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3954 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3955 setkeybinding(mod, ks, kfid, NULL);
3961 /* search by custom spawn name */
3962 for (i = 0; i < spawns_length; i++) {
3963 if (strcasecmp(selector, spawns[i].name) == 0) {
3964 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
3966 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
3967 setkeybinding(mod, ks, kf_spawn_custom,
3974 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
3981 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
3982 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
3983 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
3984 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
3985 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
3986 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
3987 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
3988 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
3989 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
3990 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
3991 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
3992 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
3993 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
3994 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
3995 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
3996 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
3997 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
3998 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
3999 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
4000 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
4001 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
4002 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
4003 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
4004 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
4005 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
4006 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
4007 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
4008 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
4009 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
4010 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
4011 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
4012 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
4013 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
4014 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
4015 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
4016 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
4017 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
4018 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
4019 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
4020 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
4021 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
4022 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
4023 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
4024 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
4025 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
4026 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
4027 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
4028 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
4029 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
4030 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
4031 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
4032 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
4033 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
4034 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
4035 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
4036 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
4038 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
4043 updatenumlockmask(void)
4046 XModifierKeymap *modmap;
4048 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
4050 modmap = XGetModifierMapping(display);
4051 for (i = 0; i < 8; i++)
4052 for (j = 0; j < modmap->max_keypermod; j++)
4053 if (modmap->modifiermap[i * modmap->max_keypermod + j]
4054 == XKeysymToKeycode(display, XK_Num_Lock))
4055 numlockmask = (1 << i);
4057 XFreeModifiermap(modmap);
4063 unsigned int i, j, k;
4065 unsigned int modifiers[] =
4066 { 0, LockMask, numlockmask, numlockmask | LockMask };
4068 DNPRINTF(SWM_D_MISC, "grabkeys\n");
4069 updatenumlockmask();
4071 for (k = 0; k < ScreenCount(display); k++) {
4072 if (TAILQ_EMPTY(&screens[k].rl))
4074 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
4075 for (i = 0; i < keys_length; i++) {
4076 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
4077 for (j = 0; j < LENGTH(modifiers); j++)
4078 XGrabKey(display, code,
4079 keys[i].mod | modifiers[j],
4080 screens[k].root, True,
4081 GrabModeAsync, GrabModeAsync);
4087 grabbuttons(struct ws_win *win, int focused)
4090 unsigned int modifiers[] =
4091 { 0, LockMask, numlockmask, numlockmask|LockMask };
4093 updatenumlockmask();
4094 XUngrabButton(display, AnyButton, AnyModifier, win->id);
4096 for (i = 0; i < LENGTH(buttons); i++)
4097 if (buttons[i].action == client_click)
4098 for (j = 0; j < LENGTH(modifiers); j++)
4099 XGrabButton(display, buttons[i].button,
4100 buttons[i].mask | modifiers[j],
4101 win->id, False, BUTTONMASK,
4102 GrabModeAsync, GrabModeSync, None,
4105 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
4106 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
4109 const char *quirkname[] = {
4110 "NONE", /* config string for "no value" */
4118 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
4119 #define SWM_Q_WS "\n|+ \t"
4121 parsequirks(char *qstr, unsigned long *quirk)
4131 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
4133 cp += (long)strspn(cp, SWM_Q_WS);
4134 for (i = 0; i < LENGTH(quirkname); i++) {
4135 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
4136 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
4141 *quirk |= 1 << (i-1);
4145 if (i >= LENGTH(quirkname)) {
4146 DNPRINTF(SWM_D_QUIRK,
4147 "parsequirks: invalid quirk [%s]\n", name);
4155 setquirk(const char *class, const char *name, const int quirk)
4160 for (i = 0; i < quirks_length; i++) {
4161 if (!strcmp(quirks[i].class, class) &&
4162 !strcmp(quirks[i].name, name)) {
4165 DNPRINTF(SWM_D_QUIRK,
4166 "setquirk: delete #%d %s:%s\n",
4167 i, quirks[i].class, quirks[i].name);
4168 free(quirks[i].class);
4169 free(quirks[i].name);
4170 j = quirks_length - 1;
4172 quirks[i] = quirks[j];
4176 /* found: replace */
4177 DNPRINTF(SWM_D_QUIRK,
4178 "setquirk: replace #%d %s:%s\n",
4179 i, quirks[i].class, quirks[i].name);
4180 free(quirks[i].class);
4181 free(quirks[i].name);
4182 quirks[i].class = strdup(class);
4183 quirks[i].name = strdup(name);
4184 quirks[i].quirk = quirk;
4191 "error: setquirk: cannot find class/name combination");
4194 /* not found: add */
4195 if (quirks_size == 0 || quirks == NULL) {
4197 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
4198 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
4200 fprintf(stderr, "setquirk: malloc failed\n");
4204 } else if (quirks_length == quirks_size) {
4206 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
4207 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
4209 fprintf(stderr, "setquirk: realloc failed\n");
4214 if (quirks_length < quirks_size) {
4215 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
4216 j = quirks_length++;
4217 quirks[j].class = strdup(class);
4218 quirks[j].name = strdup(name);
4219 quirks[j].quirk = quirk;
4221 fprintf(stderr, "quirks array problem?\n");
4223 fprintf(stderr, "quirks array problem!\n");
4230 setconfquirk(char *selector, char *value, int flags)
4232 char *cp, *class, *name;
4234 unsigned long quirks;
4235 if (selector == NULL)
4237 if ((cp = strchr(selector, ':')) == NULL)
4242 if ((retval = parsequirks(value, &quirks)) == 0)
4243 setquirk(class, name, quirks);
4250 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
4251 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
4252 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
4253 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
4254 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4255 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
4256 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4257 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4258 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4259 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4260 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
4261 setquirk("pcb", "pcb", SWM_Q_FLOAT);
4264 /* conf file stuff */
4265 #define SWM_CONF_FILE "scrotwm.conf"
4267 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH, SWM_S_STACK_ENABLED,
4268 SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT, SWM_S_CYCLE_EMPTY,
4269 SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH,
4270 SWM_S_TITLE_CLASS_ENABLED, SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED,
4271 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH, SWM_S_BAR_FONT,
4272 SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM, SWM_S_SS_APP, SWM_S_DIALOG_RATIO,
4277 setconfvalue(char *selector, char *value, int flags)
4280 case SWM_S_BAR_DELAY:
4281 bar_delay = atoi(value);
4283 case SWM_S_BAR_ENABLED:
4284 bar_enabled = atoi(value);
4286 case SWM_S_BAR_BORDER_WIDTH:
4287 bar_border_width = atoi(value);
4289 case SWM_S_BAR_AT_BOTTOM:
4290 bar_at_bottom = atoi(value);
4292 case SWM_S_STACK_ENABLED:
4293 stack_enabled = atoi(value);
4295 case SWM_S_CLOCK_ENABLED:
4296 clock_enabled = atoi(value);
4298 case SWM_S_CLOCK_FORMAT:
4299 #ifndef SWM_DENY_CLOCK_FORMAT
4301 if ((clock_format = strdup(value)) == NULL)
4302 err(1, "setconfvalue: clock_format");
4305 case SWM_S_CYCLE_EMPTY:
4306 cycle_empty = atoi(value);
4308 case SWM_S_CYCLE_VISIBLE:
4309 cycle_visible = atoi(value);
4311 case SWM_S_SS_ENABLED:
4312 ss_enabled = atoi(value);
4314 case SWM_S_TERM_WIDTH:
4315 term_width = atoi(value);
4317 case SWM_S_TITLE_CLASS_ENABLED:
4318 title_class_enabled = atoi(value);
4320 case SWM_S_WINDOW_NAME_ENABLED:
4321 window_name_enabled = atoi(value);
4323 case SWM_S_TITLE_NAME_ENABLED:
4324 title_name_enabled = atoi(value);
4326 case SWM_S_FOCUS_MODE:
4327 if (!strcmp(value, "default"))
4328 focus_mode = SWM_FOCUS_DEFAULT;
4329 else if (!strcmp(value, "follow_cursor"))
4330 focus_mode = SWM_FOCUS_FOLLOW;
4331 else if (!strcmp(value, "synergy"))
4332 focus_mode = SWM_FOCUS_SYNERGY;
4334 err(1, "focus_mode");
4336 case SWM_S_DISABLE_BORDER:
4337 disable_border = atoi(value);
4339 case SWM_S_BORDER_WIDTH:
4340 border_width = atoi(value);
4342 case SWM_S_BAR_FONT:
4344 if ((bar_fonts[0] = strdup(value)) == NULL)
4345 err(1, "setconfvalue: bar_font");
4347 case SWM_S_BAR_ACTION:
4349 if ((bar_argv[0] = strdup(value)) == NULL)
4350 err(1, "setconfvalue: bar_action");
4352 case SWM_S_SPAWN_TERM:
4353 free(spawn_term[0]);
4354 if ((spawn_term[0] = strdup(value)) == NULL)
4355 err(1, "setconfvalue: spawn_term");
4359 case SWM_S_DIALOG_RATIO:
4360 dialog_ratio = atof(value);
4361 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
4371 setconfmodkey(char *selector, char *value, int flags)
4373 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
4374 update_modkey(Mod1Mask);
4375 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
4376 update_modkey(Mod2Mask);
4377 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
4378 update_modkey(Mod3Mask);
4379 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
4380 update_modkey(Mod4Mask);
4387 setconfcolor(char *selector, char *value, int flags)
4389 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
4394 setconfregion(char *selector, char *value, int flags)
4396 custom_region(value);
4400 /* config options */
4401 struct config_option {
4403 int (*func)(char*, char*, int);
4406 struct config_option configopt[] = {
4407 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
4408 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
4409 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
4410 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
4411 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
4412 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
4413 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
4414 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
4415 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
4416 { "bind", setconfbinding, 0 },
4417 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
4418 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
4419 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
4420 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
4421 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
4422 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
4423 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
4424 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
4425 { "modkey", setconfmodkey, 0 },
4426 { "program", setconfspawn, 0 },
4427 { "quirk", setconfquirk, 0 },
4428 { "region", setconfregion, 0 },
4429 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
4430 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
4431 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
4432 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
4433 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
4434 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
4435 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
4436 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
4437 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
4438 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
4443 conf_load(char *filename)
4446 char *line, *cp, *optsub, *optval;
4447 size_t linelen, lineno = 0;
4448 int wordlen, i, optind;
4449 struct config_option *opt;
4451 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
4453 if (filename == NULL) {
4454 fprintf(stderr, "conf_load: no filename\n");
4457 if ((config = fopen(filename, "r")) == NULL) {
4458 warn("conf_load: fopen");
4462 while (!feof(config)) {
4463 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
4466 err(1, "%s", filename);
4471 cp += strspn(cp, " \t\n"); /* eat whitespace */
4472 if (cp[0] == '\0') {
4477 /* get config option */
4478 wordlen = strcspn(cp, "=[ \t\n");
4480 warnx("%s: line %zd: no option found",
4485 for (i = 0; i < LENGTH(configopt); i++) {
4486 opt = &configopt[i];
4487 if (!strncasecmp(cp, opt->optname, wordlen) &&
4488 strlen(opt->optname) == wordlen) {
4494 warnx("%s: line %zd: unknown option %.*s",
4495 filename, lineno, wordlen, cp);
4499 cp += strspn(cp, " \t\n"); /* eat whitespace */
4500 /* get [selector] if any */
4504 wordlen = strcspn(cp, "]");
4507 warnx("%s: line %zd: syntax error",
4511 asprintf(&optsub, "%.*s", wordlen, cp);
4514 cp += strspn(cp, "] \t\n"); /* eat trailing */
4516 cp += strspn(cp, "= \t\n"); /* eat trailing */
4518 optval = strdup(cp);
4519 /* call function to deal with it all */
4520 if (configopt[optind].func(optsub, optval,
4521 configopt[optind].funcflags) != 0) {
4522 fprintf(stderr, "%s line %zd: %s\n",
4523 filename, lineno, line);
4524 errx(1, "%s: line %zd: invalid data for %s",
4525 filename, lineno, configopt[optind].optname);
4533 DNPRINTF(SWM_D_CONF, "conf_load end\n");
4539 set_child_transient(struct ws_win *win)
4541 struct ws_win *parent;
4543 parent = find_window(win->transient);
4545 parent->child_trans = win;
4549 manage_window(Window id)
4552 struct workspace *ws;
4553 struct ws_win *win, *ww;
4554 int format, i, ws_idx, n, border_me = 0;
4555 unsigned long nitems, bytes;
4556 Atom ws_idx_atom = 0, type;
4557 Atom *prot = NULL, *pp;
4558 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
4559 struct swm_region *r;
4564 if ((win = find_window(id)) != NULL)
4565 return (win); /* already being managed */
4567 /* see if we are on the unmanaged list */
4568 if ((win = find_unmanaged_window(id)) != NULL) {
4569 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
4571 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
4573 set_child_transient(win);
4574 if (trans && (ww = find_window(trans)))
4575 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
4577 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
4578 ewmh_update_actions(win);
4582 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
4583 errx(1, "calloc: failed to allocate memory for new window");
4587 /* Get all the window data in one shot */
4588 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4590 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
4591 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
4592 XGetWindowAttributes(display, id, &win->wa);
4593 XGetWMNormalHints(display, id, &win->sh, &mask);
4594 XGetTransientForHint(display, id, &trans);
4596 win->transient = trans;
4597 set_child_transient(win);
4598 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
4599 win->id, win->transient);
4602 /* get supported protocols */
4603 if (XGetWMProtocols(display, id, &prot, &n)) {
4604 for (i = 0, pp = prot; i < n; i++, pp++) {
4605 if (*pp == takefocus)
4606 win->take_focus = 1;
4608 win->can_delete = 1;
4614 win->iconic = get_iconic(win);
4617 * Figure out where to put the window. If it was previously assigned to
4618 * a workspace (either by spawn() or manually moving), and isn't
4619 * transient, * put it in the same workspace
4621 r = root_to_region(win->wa.root);
4622 if (prop && win->transient == 0) {
4623 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
4624 ws_idx = strtonum(prop, 0, 9, &errstr);
4626 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
4629 ws = &r->s->ws[ws_idx];
4632 /* this should launch transients in the same ws as parent */
4634 if ((ww = find_window(trans)) != NULL)
4641 "fix this bug mcbride\n");
4646 /* set up the window layout */
4649 win->s = r->s; /* this never changes */
4650 if (trans && (ww = find_window(trans)))
4651 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
4653 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
4655 win->g.w = win->wa.width;
4656 win->g.h = win->wa.height;
4657 win->g.x = win->wa.x;
4658 win->g.y = win->wa.y;
4659 win->g_floatvalid = 0;
4660 win->floatmaxed = 0;
4661 win->ewmh_flags = 0;
4663 /* Set window properties so we can remember this after reincarnation */
4664 if (ws_idx_atom && prop == NULL &&
4665 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
4666 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
4668 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
4669 PropModeReplace, ws_idx_str, SWM_PROPLEN);
4674 ewmh_autoquirk(win);
4676 if (XGetClassHint(display, win->id, &win->ch)) {
4677 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
4678 win->ch.res_class, win->ch.res_name);
4680 /* java is retarded so treat it special */
4681 if (strstr(win->ch.res_name, "sun-awt")) {
4686 for (i = 0; i < quirks_length; i++){
4687 if (!strcmp(win->ch.res_class, quirks[i].class) &&
4688 !strcmp(win->ch.res_name, quirks[i].name)) {
4689 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
4690 win->ch.res_class, win->ch.res_name);
4691 if (quirks[i].quirk & SWM_Q_FLOAT) {
4695 win->quirks = quirks[i].quirk;
4700 /* alter window position if quirky */
4701 if (win->quirks & SWM_Q_ANYWHERE) {
4702 win->manual = 1; /* don't center the quirky windows */
4703 bzero(&wc, sizeof wc);
4705 if (bar_enabled && win->g.y < bar_height) {
4706 win->g.y = wc.y = bar_height;
4709 if (win->g.w + win->g.x > WIDTH(r)) {
4710 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
4716 /* Reset font sizes (the bruteforce way; no default keybinding). */
4717 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
4718 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
4719 fake_keypress(win, XK_KP_Subtract, ShiftMask);
4720 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
4721 fake_keypress(win, XK_KP_Add, ShiftMask);
4724 ewmh_get_win_state(win);
4725 ewmh_update_actions(win);
4726 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
4730 bzero(&wc, sizeof wc);
4731 wc.border_width = border_width;
4732 mask = CWBorderWidth;
4733 XConfigureWindow(display, win->id, mask, &wc);
4736 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
4737 PropertyChangeMask | StructureNotifyMask);
4739 set_win_state(win, IconicState);
4741 set_win_state(win, NormalState);
4743 /* floaters need to be mapped if they are in the current workspace */
4744 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
4745 XMapRaised(display, win->id);
4751 free_window(struct ws_win *win)
4753 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
4758 /* needed for restart wm */
4759 set_win_state(win, WithdrawnState);
4761 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
4763 if (win->ch.res_class)
4764 XFree(win->ch.res_class);
4765 if (win->ch.res_name)
4766 XFree(win->ch.res_name);
4771 memset(win, 0xff, sizeof *win); /* XXX kill later */
4777 unmanage_window(struct ws_win *win)
4779 struct ws_win *parent;
4784 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
4786 if (win->transient) {
4787 parent = find_window(win->transient);
4789 parent->child_trans = NULL;
4792 /* focus on root just in case */
4793 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
4797 TAILQ_REMOVE(&win->ws->winlist, win, entry);
4798 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
4804 focus_magic(struct ws_win *win)
4806 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu\n", WINID(win));
4811 if (win->child_trans) {
4812 /* win = parent & has a transient so focus on that */
4814 focus_win(win->child_trans);
4815 if (win->child_trans->take_focus)
4816 client_msg(win, takefocus);
4818 /* make sure transient hasn't dissapeared */
4819 if (validate_win(win->child_trans) == 0) {
4820 focus_win(win->child_trans);
4821 if (win->child_trans->take_focus)
4822 client_msg(win->child_trans, takefocus);
4824 win->child_trans = NULL;
4826 if (win->take_focus)
4827 client_msg(win, takefocus);
4833 if (win->take_focus)
4834 client_msg(win, takefocus);
4841 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
4849 XKeyEvent *ev = &e->xkey;
4851 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
4853 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
4854 for (i = 0; i < keys_length; i++)
4855 if (keysym == keys[i].keysym
4856 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
4857 && keyfuncs[keys[i].funcid].func) {
4858 if (keys[i].funcid == kf_spawn_custom)
4860 root_to_region(ev->root),
4861 &(keyfuncs[keys[i].funcid].args),
4865 keyfuncs[keys[i].funcid].func(
4866 root_to_region(ev->root),
4867 &(keyfuncs[keys[i].funcid].args)
4873 buttonpress(XEvent *e)
4877 XButtonPressedEvent *ev = &e->xbutton;
4879 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
4881 action = root_click;
4882 if ((win = find_window(ev->window)) == NULL)
4886 action = client_click;
4888 for (i = 0; i < LENGTH(buttons); i++)
4889 if (action == buttons[i].action && buttons[i].func &&
4890 buttons[i].button == ev->button &&
4891 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
4892 buttons[i].func(win, &buttons[i].args);
4896 configurerequest(XEvent *e)
4898 XConfigureRequestEvent *ev = &e->xconfigurerequest;
4903 if ((win = find_window(ev->window)) == NULL)
4904 if ((win = find_unmanaged_window(ev->window)) == NULL)
4908 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
4910 bzero(&wc, sizeof wc);
4913 wc.width = ev->width;
4914 wc.height = ev->height;
4915 wc.border_width = ev->border_width;
4916 wc.sibling = ev->above;
4917 wc.stack_mode = ev->detail;
4918 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
4920 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
4922 config_win(win, ev);
4927 configurenotify(XEvent *e)
4932 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
4933 e->xconfigure.window);
4935 win = find_window(e->xconfigure.window);
4937 XGetWMNormalHints(display, win->id, &win->sh, &mask);
4945 destroynotify(XEvent *e)
4948 XDestroyWindowEvent *ev = &e->xdestroywindow;
4950 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
4952 if ((win = find_window(ev->window)) == NULL) {
4953 if ((win = find_unmanaged_window(ev->window)) == NULL)
4959 /* make sure we focus on something */
4962 unmanage_window(win);
4968 enternotify(XEvent *e)
4970 XCrossingEvent *ev = &e->xcrossing;
4975 Window focus_return;
4976 int revert_to_return;
4978 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
4979 "%lu subwindow %lu same_screen %d focus %d state %d\n",
4980 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
4981 ev->same_screen, ev->focus, ev->state);
4983 switch (focus_mode) {
4984 case SWM_FOCUS_DEFAULT:
4985 if (QLength(display)) {
4986 DNPRINTF(SWM_D_EVENT, "ignore enternotify %d\n",
4991 case SWM_FOCUS_FOLLOW:
4993 case SWM_FOCUS_SYNERGY:
4996 * all these checks need to be in this order because the
4997 * XCheckTypedWindowEvent relies on weeding out the previous events
4999 * making this code an option would enable a follow mouse for focus
5004 * state is set when we are switching workspaces and focus is set when
5005 * the window or a subwindow already has focus (occurs during restart).
5007 * Only honor the focus flag if last_focus_event is not FocusOut,
5008 * this allows scrotwm to continue to control focus when another
5009 * program is also playing with it.
5011 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
5012 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
5017 * happens when a window is created or destroyed and the border
5018 * crosses the mouse pointer and when switching ws
5020 * we need the subwindow test to see if we came from root in order
5021 * to give focus to floaters
5023 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
5024 ev->subwindow == 0) {
5025 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
5029 /* this window already has focus */
5030 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
5031 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
5035 /* this window is being deleted or moved to another ws */
5036 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
5038 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
5039 XPutBackEvent(display, &cne);
5043 if ((win = find_window(ev->window)) == NULL) {
5044 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5049 * In fullstack kill all enters unless they come from a different ws
5050 * (i.e. another region) or focus has been grabbed externally.
5052 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
5053 last_focus_event != FocusOut) {
5054 XGetInputFocus(display, &focus_return, &revert_to_return);
5055 if ((w = find_window(focus_return)) == NULL ||
5057 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
5065 if ((win = find_window(ev->window)) == NULL) {
5066 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5071 * if we have more enternotifies let them handle it in due time
5073 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
5074 DNPRINTF(SWM_D_EVENT,
5075 "ignoring enternotify: got more enternotify\n");
5076 XPutBackEvent(display, &cne);
5083 /* lets us use one switch statement for arbitrary mode/detail combinations */
5084 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
5087 focusevent(XEvent *e)
5091 u_int32_t mode_detail;
5092 XFocusChangeEvent *ev = &e->xfocus;
5094 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
5095 ev->type == FocusIn ? "entering" : "leaving",
5096 ev->window, ev->mode, ev->detail);
5098 if (last_focus_event == ev->type) {
5099 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
5103 last_focus_event = ev->type;
5104 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
5106 switch (mode_detail) {
5107 /* synergy client focus operations */
5108 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
5109 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
5111 /* synergy server focus operations */
5112 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
5114 /* Entering applications like rdesktop that mangle the pointer */
5115 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
5117 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
5118 XSetWindowBorder(display, win->id,
5119 win->ws->r->s->c[ev->type == FocusIn ?
5120 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
5123 fprintf(stderr, "ignoring focusevent\n");
5124 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
5131 mapnotify(XEvent *e)
5134 XMapEvent *ev = &e->xmap;
5136 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
5138 win = manage_window(ev->window);
5140 set_win_state(win, NormalState);
5144 mappingnotify(XEvent *e)
5146 XMappingEvent *ev = &e->xmapping;
5148 XRefreshKeyboardMapping(ev);
5149 if (ev->request == MappingKeyboard)
5154 maprequest(XEvent *e)
5157 struct swm_region *r;
5158 XWindowAttributes wa;
5159 XMapRequestEvent *ev = &e->xmaprequest;
5161 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
5162 e->xmaprequest.window);
5164 if (!XGetWindowAttributes(display, ev->window, &wa))
5166 if (wa.override_redirect)
5169 win = manage_window(e->xmaprequest.window);
5171 return; /* can't happen */
5175 /* make new win focused */
5176 r = root_to_region(win->wa.root);
5177 if (win->ws == r->ws)
5182 propertynotify(XEvent *e)
5185 XPropertyEvent *ev = &e->xproperty;
5187 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
5190 win = find_window(ev->window);
5194 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
5195 update_iconic(win, 0);
5196 XMapRaised(display, win->id);
5203 case XA_WM_NORMAL_HINTS:
5206 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5207 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
5208 if (win->sh.flags & PMinSize) {
5209 win->g.w = win->sh.min_width;
5210 win->g.h = win->sh.min_height;
5211 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
5213 XMoveResizeWindow(display, win->id,
5214 win->g.x, win->g.y, win->g.w, win->g.h);
5216 if (window_name_enabled)
5225 unmapnotify(XEvent *e)
5229 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
5231 /* determine if we need to help unmanage this window */
5232 win = find_window(e->xunmap.window);
5236 if (getstate(e->xunmap.window) == NormalState) {
5237 unmanage_window(win);
5243 visibilitynotify(XEvent *e)
5246 struct swm_region *r;
5248 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
5249 e->xvisibility.window);
5250 if (e->xvisibility.state == VisibilityUnobscured)
5251 for (i = 0; i < ScreenCount(display); i++)
5252 TAILQ_FOREACH(r, &screens[i].rl, entry)
5253 if (e->xvisibility.window == r->bar_window)
5258 clientmessage(XEvent *e)
5260 XClientMessageEvent *ev;
5265 win = find_window(ev->window);
5269 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx type: %ld \n",
5270 ev->window, ev->message_type);
5272 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
5273 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW \n");
5276 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
5277 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW \n");
5278 if (win->can_delete)
5279 client_msg(win, adelete);
5281 XKillClient(display, win->id);
5283 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
5284 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_MOVERESIZE_WINDOW \n");
5285 if (win->floating) {
5286 if (ev->data.l[0] & (1<<8)) /* x */
5287 win->g.x = ev->data.l[1];
5288 if (ev->data.l[0] & (1<<9)) /* y */
5289 win->g.y = ev->data.l[2];
5290 if (ev->data.l[0] & (1<<10)) /* width */
5291 win->g.w = ev->data.l[3];
5292 if (ev->data.l[0] & (1<<11)) /* height */
5293 win->g.h = ev->data.l[4];
5296 /* TODO: Change stack sizes */
5298 config_win(win, NULL);
5300 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
5301 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE \n");
5302 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
5304 ewmh_update_win_state(win, ev->data.l[2], ev->data.l[0]);
5311 xerror_start(Display *d, XErrorEvent *ee)
5318 xerror(Display *d, XErrorEvent *ee)
5320 /* fprintf(stderr, "error: %p %p\n", display, ee); */
5328 xerrorxlib = XSetErrorHandler(xerror_start);
5330 /* this causes an error if some other window manager is running */
5331 XSelectInput(display, DefaultRootWindow(display),
5332 SubstructureRedirectMask);
5333 XSync(display, False);
5337 XSetErrorHandler(xerror);
5338 XSync(display, False);
5343 new_region(struct swm_screen *s, int x, int y, int w, int h)
5345 struct swm_region *r, *n;
5346 struct workspace *ws = NULL;
5349 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
5350 s->idx, w, h, x, y);
5352 /* remove any conflicting regions */
5353 n = TAILQ_FIRST(&s->rl);
5356 n = TAILQ_NEXT(r, entry);
5357 if (X(r) < (x + w) &&
5358 (X(r) + WIDTH(r)) > x &&
5360 (Y(r) + HEIGHT(r)) > y) {
5361 if (r->ws->r != NULL)
5362 r->ws->old_r = r->ws->r;
5364 XDestroyWindow(display, r->bar_window);
5365 TAILQ_REMOVE(&s->rl, r, entry);
5366 TAILQ_INSERT_TAIL(&s->orl, r, entry);
5370 /* search old regions for one to reuse */
5372 /* size + location match */
5373 TAILQ_FOREACH(r, &s->orl, entry)
5374 if (X(r) == x && Y(r) == y &&
5375 HEIGHT(r) == h && WIDTH(r) == w)
5379 TAILQ_FOREACH(r, &s->orl, entry)
5380 if (HEIGHT(r) == h && WIDTH(r) == w)
5384 TAILQ_REMOVE(&s->orl, r, entry);
5385 /* try to use old region's workspace */
5386 if (r->ws->r == NULL)
5389 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
5390 errx(1, "calloc: failed to allocate memory for screen");
5392 /* if we don't have a workspace already, find one */
5394 for (i = 0; i < SWM_WS_MAX; i++)
5395 if (s->ws[i].r == NULL) {
5402 errx(1, "no free workspaces\n");
5413 TAILQ_INSERT_TAIL(&s->rl, r, entry);
5419 #ifdef SWM_XRR_HAS_CRTC
5421 XRRScreenResources *sr;
5424 #endif /* SWM_XRR_HAS_CRTC */
5425 struct swm_region *r;
5428 if (i >= ScreenCount(display))
5429 errx(1, "invalid screen");
5431 /* remove any old regions */
5432 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
5433 r->ws->old_r = r->ws->r = NULL;
5434 XDestroyWindow(display, r->bar_window);
5435 TAILQ_REMOVE(&screens[i].rl, r, entry);
5436 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
5440 /* map virtual screens onto physical screens */
5441 #ifdef SWM_XRR_HAS_CRTC
5442 if (xrandr_support) {
5443 sr = XRRGetScreenResources(display, screens[i].root);
5445 new_region(&screens[i], 0, 0,
5446 DisplayWidth(display, i),
5447 DisplayHeight(display, i));
5451 for (c = 0, ci = NULL; c < ncrtc; c++) {
5452 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
5453 if (ci->noutput == 0)
5456 if (ci != NULL && ci->mode == None)
5457 new_region(&screens[i], 0, 0,
5458 DisplayWidth(display, i),
5459 DisplayHeight(display, i));
5461 new_region(&screens[i],
5462 ci->x, ci->y, ci->width, ci->height);
5465 XRRFreeCrtcInfo(ci);
5466 XRRFreeScreenResources(sr);
5468 #endif /* SWM_XRR_HAS_CRTC */
5470 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
5471 DisplayHeight(display, i));
5476 screenchange(XEvent *e) {
5477 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
5478 struct swm_region *r;
5481 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
5483 if (!XRRUpdateConfiguration(e))
5486 /* silly event doesn't include the screen index */
5487 for (i = 0; i < ScreenCount(display); i++)
5488 if (screens[i].root == xe->root)
5490 if (i >= ScreenCount(display))
5491 errx(1, "screenchange: screen not found\n");
5493 /* brute force for now, just re-enumerate the regions */
5496 /* add bars to all regions */
5497 for (i = 0; i < ScreenCount(display); i++)
5498 TAILQ_FOREACH(r, &screens[i].rl, entry)
5506 Window d1, d2, *wins = NULL;
5507 XWindowAttributes wa;
5512 for (i = 0; i < ScreenCount(display); i++) {
5513 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
5516 /* attach windows to a region */
5517 /* normal windows */
5518 for (j = 0; j < no; j++) {
5519 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5520 wa.override_redirect ||
5521 XGetTransientForHint(display, wins[j], &d1))
5524 state = getstate(wins[j]);
5525 manage = state == IconicState;
5526 if (wa.map_state == IsViewable || manage)
5527 manage_window(wins[j]);
5529 /* transient windows */
5530 for (j = 0; j < no; j++) {
5531 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5532 wa.override_redirect)
5535 state = getstate(wins[j]);
5536 manage = state == IconicState;
5537 if (XGetTransientForHint(display, wins[j], &d1) &&
5539 manage_window(wins[j]);
5552 int errorbase, major, minor;
5553 struct workspace *ws;
5556 if ((screens = calloc(ScreenCount(display),
5557 sizeof(struct swm_screen))) == NULL)
5558 errx(1, "calloc: screens");
5560 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
5562 /* initial Xrandr setup */
5563 xrandr_support = XRRQueryExtension(display,
5564 &xrandr_eventbase, &errorbase);
5566 if (XRRQueryVersion(display, &major, &minor) && major < 1)
5569 /* map physical screens */
5570 for (i = 0; i < ScreenCount(display); i++) {
5571 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
5573 TAILQ_INIT(&screens[i].rl);
5574 TAILQ_INIT(&screens[i].orl);
5575 screens[i].root = RootWindow(display, i);
5577 /* set default colors */
5578 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
5579 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
5580 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
5581 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
5582 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
5584 /* set default cursor */
5585 XDefineCursor(display, screens[i].root,
5586 XCreateFontCursor(display, XC_left_ptr));
5588 /* init all workspaces */
5589 /* XXX these should be dynamically allocated too */
5590 for (j = 0; j < SWM_WS_MAX; j++) {
5591 ws = &screens[i].ws[j];
5596 TAILQ_INIT(&ws->winlist);
5597 TAILQ_INIT(&ws->unmanagedlist);
5599 for (k = 0; layouts[k].l_stack != NULL; k++)
5600 if (layouts[k].l_config != NULL)
5601 layouts[k].l_config(ws,
5602 SWM_ARG_ID_STACKINIT);
5603 ws->cur_layout = &layouts[0];
5609 XRRSelectInput(display, screens[i].root,
5610 RRScreenChangeNotifyMask);
5617 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
5619 err(1, "setup_globals: strdup");
5620 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
5622 err(1, "setup_globals: strdup");
5623 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
5625 err(1, "setup_globals: strdup");
5626 if ((spawn_term[0] = strdup("xterm")) == NULL)
5627 err(1, "setup_globals: strdup");
5628 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
5636 Atom netwmcheck, netwmname, utf8_string;
5639 /* work around sun jdk bugs, code from wmname */
5640 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
5641 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
5642 utf8_string = XInternAtom(display, "UTF8_STRING", False);
5643 for (i = 0; i < ScreenCount(display); i++) {
5644 root = screens[i].root;
5645 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
5646 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
5647 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
5649 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
5650 PropModeReplace, (unsigned char *)&win,1);
5651 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
5652 PropModeReplace, (unsigned char *)&win,1);
5653 XChangeProperty(display, win, netwmname, utf8_string, 8,
5654 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
5659 main(int argc, char *argv[])
5662 struct swm_region *r, *rr;
5663 struct ws_win *winfocus = NULL;
5666 char conf[PATH_MAX], *cfile = NULL;
5671 struct sigaction sact;
5674 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
5675 SWM_VERSION, cvstag);
5676 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
5677 warnx("no locale support");
5679 if (!(display = XOpenDisplay(0)))
5680 errx(1, "can not open display");
5683 errx(1, "other wm running");
5685 /* handle some signals */
5686 bzero(&sact, sizeof(sact));
5687 sigemptyset(&sact.sa_mask);
5689 sact.sa_handler = sighdlr;
5690 sigaction(SIGINT, &sact, NULL);
5691 sigaction(SIGQUIT, &sact, NULL);
5692 sigaction(SIGTERM, &sact, NULL);
5693 sigaction(SIGHUP, &sact, NULL);
5695 sact.sa_handler = sighdlr;
5696 sact.sa_flags = SA_NOCLDSTOP;
5697 sigaction(SIGCHLD, &sact, NULL);
5699 astate = XInternAtom(display, "WM_STATE", False);
5700 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
5701 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
5702 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
5703 a_wmname = XInternAtom(display, "WM_NAME", False);
5704 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
5705 a_string = XInternAtom(display, "STRING", False);
5706 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
5708 /* look for local and global conf file */
5709 pwd = getpwuid(getuid());
5711 errx(1, "invalid user %d", getuid());
5720 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
5721 if (stat(conf, &sb) != -1) {
5722 if (S_ISREG(sb.st_mode))
5725 /* try global conf file */
5726 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
5727 if (!stat(conf, &sb))
5728 if (S_ISREG(sb.st_mode))
5735 /* set some values to work around bad programs */
5738 /* grab existing windows (before we build the bars) */
5741 /* setup all bars */
5742 for (i = 0; i < ScreenCount(display); i++)
5743 TAILQ_FOREACH(r, &screens[i].rl, entry) {
5744 if (winfocus == NULL)
5745 winfocus = TAILQ_FIRST(&r->ws->winlist);
5754 xfd = ConnectionNumber(display);
5756 while (XPending(display)) {
5757 XNextEvent(display, &e);
5760 if (e.type < LASTEvent) {
5762 if (handler[e.type])
5763 handler[e.type](&e);
5765 DNPRINTF(SWM_D_EVENT,
5766 "win: %lu unknown event: %d\n",
5767 e.xany.window, e.type);
5769 switch (e.type - xrandr_eventbase) {
5770 case RRScreenChangeNotify:
5774 DNPRINTF(SWM_D_EVENT,
5775 "win: %lu unknown xrandr event: "
5776 "%d\n", e.xany.window, e.type);
5782 /* if we are being restarted go focus on first window */
5784 rr = winfocus->ws->r;
5786 /* not a visible window */
5790 /* move pointer to first screen if multi screen */
5791 if (ScreenCount(display) > 1 || outputs > 1)
5792 XWarpPointer(display, None, rr->s[0].root,
5793 0, 0, 0, 0, rr->g.x,
5794 rr->g.y + (bar_enabled ? bar_height : 0));
5796 a.id = SWM_ARG_ID_FOCUSCUR;
5806 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
5808 DNPRINTF(SWM_D_MISC, "select failed");
5809 if (restart_wm == 1)
5810 restart(NULL, NULL);
5811 if (search_resp == 1)
5823 XFreeGC(display, bar_gc);
5824 XCloseDisplay(display);