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) 2010 Tuukka Kataja <stuge@xor.fi>
8 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Much code and ideas taken from dwm under the following license:
24 * MIT/X Consortium License
26 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
27 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
28 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
29 * 2007 Premysl Hruby <dfenze at gmail dot com>
30 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
31 * 2007 Christof Musik <christof at sendfax dot de>
32 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
33 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
34 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
36 * Permission is hereby granted, free of charge, to any person obtaining a
37 * copy of this software and associated documentation files (the "Software"),
38 * to deal in the Software without restriction, including without limitation
39 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
40 * and/or sell copies of the Software, and to permit persons to whom the
41 * Software is furnished to do so, subject to the following conditions:
43 * The above copyright notice and this permission notice shall be included in
44 * all copies or substantial portions of the Software.
46 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
49 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
50 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
51 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
52 * DEALINGS IN THE SOFTWARE.
55 static const char *cvstag =
58 #define SWM_VERSION "0.9.32"
75 #include <sys/types.h>
79 #include <sys/queue.h>
80 #include <sys/param.h>
81 #include <sys/select.h>
83 #include <X11/cursorfont.h>
84 #include <X11/keysym.h>
85 #include <X11/Xatom.h>
87 #include <X11/Xproto.h>
88 #include <X11/Xutil.h>
89 #include <X11/extensions/Xrandr.h>
96 # error XRandR versions less than 1.0 are not supported
101 #define SWM_XRR_HAS_CRTC
105 /*#define SWM_DEBUG*/
107 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
108 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
109 #define SWM_D_MISC 0x0001
110 #define SWM_D_EVENT 0x0002
111 #define SWM_D_WS 0x0004
112 #define SWM_D_FOCUS 0x0008
113 #define SWM_D_MOVE 0x0010
114 #define SWM_D_STACK 0x0020
115 #define SWM_D_MOUSE 0x0040
116 #define SWM_D_PROP 0x0080
117 #define SWM_D_CLASS 0x0100
118 #define SWM_D_KEY 0x0200
119 #define SWM_D_QUIRK 0x0400
120 #define SWM_D_SPAWN 0x0800
121 #define SWM_D_EVENTQ 0x1000
122 #define SWM_D_CONF 0x2000
124 u_int32_t swm_debug = 0
141 #define DPRINTF(x...)
142 #define DNPRINTF(n,x...)
145 #define LENGTH(x) (sizeof x / sizeof x[0])
146 #define MODKEY Mod1Mask
147 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
148 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
149 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
150 #define SWM_PROPLEN (16)
151 #define SWM_FUNCNAME_LEN (32)
152 #define SWM_KEYS_LEN (255)
153 #define SWM_QUIRK_LEN (64)
154 #define X(r) (r)->g.x
155 #define Y(r) (r)->g.y
156 #define WIDTH(r) (r)->g.w
157 #define HEIGHT(r) (r)->g.h
158 #define SWM_MAX_FONT_STEPS (3)
159 #define WINID(w) (w ? w->id : 0)
161 #define SWM_FOCUS_DEFAULT (0)
162 #define SWM_FOCUS_SYNERGY (1)
163 #define SWM_FOCUS_FOLLOW (2)
166 #define SWM_LIB "/usr/local/lib/libswmhack.so"
178 volatile sig_atomic_t running = 1;
179 volatile sig_atomic_t restart_wm = 0;
181 int last_focus_event = FocusOut;
182 int (*xerrorxlib)(Display *, XErrorEvent *);
186 int xrandr_eventbase;
187 unsigned int numlockmask = 0;
191 int cycle_visible = 0;
193 int font_adjusted = 0;
194 unsigned int mod_key = MODKEY;
197 struct swm_region *search_r;
198 int select_list_pipe[2];
199 int select_resp_pipe[2];
201 volatile sig_atomic_t search_resp;
204 double dialog_ratio = .6;
206 #define SWM_BAR_MAX (256)
207 char *bar_argv[] = { NULL, NULL };
209 char bar_ext[SWM_BAR_MAX];
210 char bar_vertext[SWM_BAR_MAX];
212 sig_atomic_t bar_alarm = 0;
215 int bar_border_width = 1;
216 int bar_at_bottom = 0;
218 int bar_extra_running = 0;
221 int stack_enabled = 1;
222 int clock_enabled = 1;
223 char *clock_format = NULL;
224 int title_name_enabled = 0;
225 int title_class_enabled = 0;
226 int window_name_enabled = 0;
227 int focus_mode = SWM_FOCUS_DEFAULT;
228 int disable_border = 0;
229 int border_width = 1;
235 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
236 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
238 #define SWM_MENU_FN (2)
239 #define SWM_MENU_NB (4)
240 #define SWM_MENU_NF (6)
241 #define SWM_MENU_SB (8)
242 #define SWM_MENU_SF (10)
244 /* layout manager data */
245 struct swm_geometry {
255 /* virtual "screens" */
257 TAILQ_ENTRY(swm_region) entry;
258 struct swm_geometry g;
259 struct workspace *ws; /* current workspace on this region */
260 struct workspace *ws_prior; /* prior workspace on this region */
261 struct swm_screen *s; /* screen idx */
264 TAILQ_HEAD(swm_region_list, swm_region);
267 TAILQ_ENTRY(ws_win) entry;
270 struct ws_win *child_trans; /* transient child window */
271 struct swm_geometry g; /* current geometry */
272 struct swm_geometry g_float; /* geometry when floating */
273 struct swm_geometry rg_float; /* region geom when floating */
274 int g_floatvalid; /* flag: geometry in g_float is valid */
275 int floatmaxed; /* flag: floater was maxed in max_stack */
279 unsigned int ewmh_flags;
280 int font_size_boundary[SWM_MAX_FONT_STEPS];
286 unsigned long quirks;
287 struct workspace *ws; /* always valid */
288 struct swm_screen *s; /* always valid, never changes */
289 XWindowAttributes wa;
294 TAILQ_HEAD(ws_win_list, ws_win);
298 TAILQ_ENTRY(pid_e) entry;
302 TAILQ_HEAD(pid_list, pid_e);
303 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
305 /* layout handlers */
307 void vertical_config(struct workspace *, int);
308 void vertical_stack(struct workspace *, struct swm_geometry *);
309 void horizontal_config(struct workspace *, int);
310 void horizontal_stack(struct workspace *, struct swm_geometry *);
311 void max_stack(struct workspace *, struct swm_geometry *);
313 struct ws_win *find_window(Window);
315 void grabbuttons(struct ws_win *, int);
316 void new_region(struct swm_screen *, int, int, int, int);
317 void unmanage_window(struct ws_win *);
318 long getstate(Window);
321 void (*l_stack)(struct workspace *, struct swm_geometry *);
322 void (*l_config)(struct workspace *, int);
324 #define SWM_L_FOCUSPREV (1<<0)
325 #define SWM_L_MAPONFOCUS (1<<1)
328 /* stack, configure */
329 { vertical_stack, vertical_config, 0, "[|]" },
330 { horizontal_stack, horizontal_config, 0, "[-]" },
332 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, "[ ]" },
333 { NULL, NULL, 0, NULL },
336 /* position of max_stack mode in the layouts array, index into layouts! */
337 #define SWM_V_STACK (0)
338 #define SWM_H_STACK (1)
339 #define SWM_MAX_STACK (2)
341 #define SWM_H_SLICE (32)
342 #define SWM_V_SLICE (32)
344 /* define work spaces */
346 int idx; /* workspace index */
347 int always_raise; /* raise windows on focus */
348 struct layout *cur_layout; /* current layout handlers */
349 struct ws_win *focus; /* may be NULL */
350 struct ws_win *focus_prev; /* may be NULL */
351 struct swm_region *r; /* may be NULL */
352 struct swm_region *old_r; /* may be NULL */
353 struct ws_win_list winlist; /* list of windows in ws */
354 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
358 int horizontal_msize;
360 int horizontal_stacks;
367 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
368 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
370 /* physical screen mapping */
371 #define SWM_WS_MAX (10)
373 int idx; /* screen index */
374 struct swm_region_list rl; /* list of regions on this screen */
375 struct swm_region_list orl; /* list of old regions */
377 struct workspace ws[SWM_WS_MAX];
383 } c[SWM_S_COLOR_MAX];
385 struct swm_screen *screens;
388 /* args to functions */
391 #define SWM_ARG_ID_FOCUSNEXT (0)
392 #define SWM_ARG_ID_FOCUSPREV (1)
393 #define SWM_ARG_ID_FOCUSMAIN (2)
394 #define SWM_ARG_ID_FOCUSCUR (4)
395 #define SWM_ARG_ID_SWAPNEXT (10)
396 #define SWM_ARG_ID_SWAPPREV (11)
397 #define SWM_ARG_ID_SWAPMAIN (12)
398 #define SWM_ARG_ID_MOVELAST (13)
399 #define SWM_ARG_ID_MASTERSHRINK (20)
400 #define SWM_ARG_ID_MASTERGROW (21)
401 #define SWM_ARG_ID_MASTERADD (22)
402 #define SWM_ARG_ID_MASTERDEL (23)
403 #define SWM_ARG_ID_STACKRESET (30)
404 #define SWM_ARG_ID_STACKINIT (31)
405 #define SWM_ARG_ID_CYCLEWS_UP (40)
406 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
407 #define SWM_ARG_ID_CYCLESC_UP (42)
408 #define SWM_ARG_ID_CYCLESC_DOWN (43)
409 #define SWM_ARG_ID_STACKINC (50)
410 #define SWM_ARG_ID_STACKDEC (51)
411 #define SWM_ARG_ID_SS_ALL (60)
412 #define SWM_ARG_ID_SS_WINDOW (61)
413 #define SWM_ARG_ID_DONTCENTER (70)
414 #define SWM_ARG_ID_CENTER (71)
415 #define SWM_ARG_ID_KILLWINDOW (80)
416 #define SWM_ARG_ID_DELETEWINDOW (81)
420 void focus(struct swm_region *, union arg *);
421 void focus_magic(struct ws_win *);
428 #define SWM_Q_FLOAT (1<<0) /* float this window */
429 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
430 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
431 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
432 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
433 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
435 int quirks_size = 0, quirks_length = 0;
436 struct quirk *quirks = NULL;
439 * Supported EWMH hints should be added to
440 * both the enum and the ewmh array
442 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
443 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
444 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
445 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
446 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
447 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
448 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
449 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
450 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
451 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
457 } ewmh[SWM_EWMH_HINT_MAX] = {
458 /* must be in same order as in the enum */
459 {"_NET_ACTIVE_WINDOW", None},
460 {"_NET_MOVERESIZE_WINDOW", None},
461 {"_NET_CLOSE_WINDOW", None},
462 {"_NET_WM_WINDOW_TYPE", None},
463 {"_NET_WM_WINDOW_TYPE_DOCK", None},
464 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
465 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
466 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
467 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
468 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
469 {"_NET_WM_STATE", None},
470 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
471 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
472 {"_NET_WM_STATE_SKIP_TASKBAR", None},
473 {"_NET_WM_STATE_SKIP_PAGER", None},
474 {"_NET_WM_STATE_HIDDEN", None},
475 {"_NET_WM_STATE_ABOVE", None},
476 {"_SWM_WM_STATE_MANUAL", None},
477 {"_NET_WM_STATE_FULLSCREEN", None},
478 {"_NET_WM_ALLOWED_ACTIONS", None},
479 {"_NET_WM_ACTION_MOVE", None},
480 {"_NET_WM_ACTION_RESIZE", None},
481 {"_NET_WM_ACTION_FULLSCREEN", None},
482 {"_NET_WM_ACTION_CLOSE", None},
485 void store_float_geom(struct ws_win *win, struct swm_region *r);
486 int floating_toggle_win(struct ws_win *win);
487 void spawn_select(struct swm_region *, union arg *, char *, int *);
490 get_property(Window id, Atom atom, long count, Atom type,
491 unsigned long *n, unsigned char **data)
494 unsigned long tmp, extra;
495 unsigned long *nitems;
498 nitems = n != NULL ? n : &tmp;
499 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
500 &real, &format, nitems, &extra, data);
502 if (status != Success)
511 update_iconic(struct ws_win *win, int newv)
518 iprop = XInternAtom(display, "_SWM_ICONIC", False);
522 XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
523 PropModeReplace, (unsigned char *)&v, 1);
525 XDeleteProperty(display, win->id, iprop);
529 get_iconic(struct ws_win *win)
534 unsigned long nitems, extra;
535 unsigned char *prop = NULL;
537 iprop = XInternAtom(display, "_SWM_ICONIC", False);
540 status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
541 False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
542 if (status != Success)
544 if (rettype != XA_INTEGER || retfmt != 32)
548 v = *((int32_t *)prop);
562 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
564 for (i = 0; i < LENGTH(ewmh); i++)
565 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
567 for (i = 0; i < ScreenCount(display); i++) {
568 /* Support check window will be created by workaround(). */
570 /* Report supported atoms */
571 XDeleteProperty(display, screens[i].root, sup_list);
572 for (j = 0; j < LENGTH(ewmh); j++)
573 XChangeProperty(display, screens[i].root,
574 sup_list, XA_ATOM, 32,
575 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
583 unsigned char *data = NULL;
585 Atom sup_check, sup_list;
588 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
589 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
591 for (i = 0; i < ScreenCount(display); i++) {
592 /* Get the support check window and destroy it */
593 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
598 XDestroyWindow(display, id);
599 XDeleteProperty(display, screens[i].root, sup_check);
600 XDeleteProperty(display, screens[i].root, sup_list);
608 ewmh_autoquirk(struct ws_win *win)
611 unsigned long *data = NULL;
615 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
616 XA_ATOM, &n, (unsigned char **)&data);
623 for (i = 0; i < n; i++) {
625 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
627 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
628 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
629 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
631 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
634 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
635 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
637 win->quirks = SWM_Q_FLOAT;
645 #define SWM_EWMH_ACTION_COUNT_MAX (6)
646 #define EWMH_F_FULLSCREEN (1<<0)
647 #define EWMH_F_ABOVE (1<<1)
648 #define EWMH_F_HIDDEN (1<<2)
649 #define EWMH_F_SKIP_PAGER (1<<3)
650 #define EWMH_F_SKIP_TASKBAR (1<<4)
651 #define SWM_F_MANUAL (1<<5)
654 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
656 struct swm_geometry rg;
664 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
670 store_float_geom(win, win->ws->r);
677 if (win->g_floatvalid) {
678 /* refloat at last floating relative position */
679 win->g.x = win->g_float.x - win->rg_float.x + rg.x;
680 win->g.y = win->g_float.y - win->rg_float.y + rg.y;
681 win->g.w = win->g_float.w;
682 win->g.h = win->g_float.h;
690 ewmh_update_actions(struct ws_win *win)
692 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
698 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
701 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
702 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
705 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
706 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
709 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
710 #define _NET_WM_STATE_ADD 1 /* add/set property */
711 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
714 ewmh_update_win_state(struct ws_win *win, long state, long action)
716 unsigned int mask = 0;
717 unsigned int changed = 0;
718 unsigned int orig_flags;
723 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
724 mask = EWMH_F_FULLSCREEN;
725 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
727 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
729 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
730 mask = EWMH_F_SKIP_PAGER;
731 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
732 mask = EWMH_F_SKIP_TASKBAR;
735 orig_flags = win->ewmh_flags;
738 case _NET_WM_STATE_REMOVE:
739 win->ewmh_flags &= ~mask;
741 case _NET_WM_STATE_ADD:
742 win->ewmh_flags |= mask;
744 case _NET_WM_STATE_TOGGLE:
745 win->ewmh_flags ^= mask;
749 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
751 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
753 if (!floating_toggle_win(win))
754 win->ewmh_flags = orig_flags; /* revert */
755 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
757 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
758 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
760 if (!ewmh_set_win_fullscreen(win,
761 win->ewmh_flags & EWMH_F_FULLSCREEN))
762 win->ewmh_flags = orig_flags; /* revert */
764 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
766 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
767 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
768 XA_ATOM, 32, PropModeAppend,
769 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
770 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
771 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
772 XA_ATOM, 32, PropModeAppend,
773 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
774 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
775 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
776 XA_ATOM, 32, PropModeAppend,
777 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
778 if (win->ewmh_flags & EWMH_F_ABOVE)
779 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
780 XA_ATOM, 32, PropModeAppend,
781 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
782 if (win->ewmh_flags & SWM_F_MANUAL)
783 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
784 XA_ATOM, 32, PropModeAppend,
785 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
789 ewmh_get_win_state(struct ws_win *win)
800 win->ewmh_flags |= EWMH_F_ABOVE;
802 win->ewmh_flags |= SWM_F_MANUAL;
804 success = get_property(win->id, ewmh[_NET_WM_STATE].atom,
805 (~0L), XA_ATOM, &n, (unsigned char **)&states);
810 for (i = 0; i < n; i++)
811 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
831 name = "ButtonPress";
834 name = "ButtonRelease";
837 name = "MotionNotify";
840 name = "EnterNotify";
843 name = "LeaveNotify";
852 name = "KeymapNotify";
858 name = "GraphicsExpose";
863 case VisibilityNotify:
864 name = "VisibilityNotify";
867 name = "CreateNotify";
870 name = "DestroyNotify";
873 name = "UnmapNotify";
882 name = "ReparentNotify";
884 case ConfigureNotify:
885 name = "ConfigureNotify";
887 case ConfigureRequest:
888 name = "ConfigureRequest";
891 name = "GravityNotify";
894 name = "ResizeRequest";
896 case CirculateNotify:
897 name = "CirculateNotify";
899 case CirculateRequest:
900 name = "CirculateRequest";
903 name = "PropertyNotify";
906 name = "SelectionClear";
908 case SelectionRequest:
909 name = "SelectionRequest";
911 case SelectionNotify:
912 name = "SelectionNotify";
915 name = "ColormapNotify";
918 name = "ClientMessage";
921 name = "MappingNotify";
926 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
928 e->xany.window, name, e->type, QLength(display));
930 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
932 e->xany.window, e->type, QLength(display));
936 dumpwins(struct swm_region *r, union arg *args)
940 XWindowAttributes wa;
943 fprintf(stderr, "invalid workspace\n");
947 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
949 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
950 state = getstate(win->id);
951 if (!XGetWindowAttributes(display, win->id, &wa))
952 fprintf(stderr, "window: %lu failed "
953 "XGetWindowAttributes\n", win->id);
954 fprintf(stderr, "window: %lu map_state: %d state: %d "
956 win->id, wa.map_state, state, win->transient);
959 fprintf(stderr, "===== unmanaged window list =====\n");
960 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
961 state = getstate(win->id);
962 if (!XGetWindowAttributes(display, win->id, &wa))
963 fprintf(stderr, "window: %lu failed "
964 "XGetWindowAttributes\n", win->id);
965 fprintf(stderr, "window: %lu map_state: %d state: %d "
967 win->id, wa.map_state, state, win->transient);
970 fprintf(stderr, "=================================\n");
975 dumpwins(struct swm_region *r, union arg *args)
978 #endif /* SWM_DEBUG */
980 void expose(XEvent *);
981 void keypress(XEvent *);
982 void buttonpress(XEvent *);
983 void configurerequest(XEvent *);
984 void configurenotify(XEvent *);
985 void destroynotify(XEvent *);
986 void enternotify(XEvent *);
987 void focusevent(XEvent *);
988 void mapnotify(XEvent *);
989 void mappingnotify(XEvent *);
990 void maprequest(XEvent *);
991 void propertynotify(XEvent *);
992 void unmapnotify(XEvent *);
993 void visibilitynotify(XEvent *);
994 void clientmessage(XEvent *);
996 void (*handler[LASTEvent])(XEvent *) = {
998 [KeyPress] = keypress,
999 [ButtonPress] = buttonpress,
1000 [ConfigureRequest] = configurerequest,
1001 [ConfigureNotify] = configurenotify,
1002 [DestroyNotify] = destroynotify,
1003 [EnterNotify] = enternotify,
1004 [FocusIn] = focusevent,
1005 [FocusOut] = focusevent,
1006 [MapNotify] = mapnotify,
1007 [MappingNotify] = mappingnotify,
1008 [MapRequest] = maprequest,
1009 [PropertyNotify] = propertynotify,
1010 [UnmapNotify] = unmapnotify,
1011 [VisibilityNotify] = visibilitynotify,
1012 [ClientMessage] = clientmessage,
1018 int saved_errno, status;
1021 saved_errno = errno;
1025 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1030 if (errno != ECHILD)
1031 warn("sighdlr: waitpid");
1032 #endif /* SWM_DEBUG */
1035 if (pid == searchpid)
1039 if (WIFEXITED(status)) {
1040 if (WEXITSTATUS(status) != 0)
1041 warnx("sighdlr: child exit status: %d",
1042 WEXITSTATUS(status));
1044 warnx("sighdlr: child is terminated "
1046 #endif /* SWM_DEBUG */
1060 errno = saved_errno;
1066 struct pid_e *p = NULL;
1068 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1073 TAILQ_FOREACH(p, &pidlist, entry) {
1082 name_to_color(char *colorname)
1086 XColor screen_def, exact_def;
1087 unsigned long result = 0;
1088 char cname[32] = "#";
1090 cmap = DefaultColormap(display, screens[0].idx);
1091 status = XAllocNamedColor(display, cmap, colorname,
1092 &screen_def, &exact_def);
1094 strlcat(cname, colorname + 2, sizeof cname - 1);
1095 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1099 result = screen_def.pixel;
1101 fprintf(stderr, "color '%s' not found.\n", colorname);
1107 setscreencolor(char *val, int i, int c)
1109 if (i > 0 && i <= ScreenCount(display)) {
1110 screens[i - 1].c[c].color = name_to_color(val);
1111 free(screens[i - 1].c[c].name);
1112 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1114 } else if (i == -1) {
1115 for (i = 0; i < ScreenCount(display); i++) {
1116 screens[i].c[c].color = name_to_color(val);
1117 free(screens[i].c[c].name);
1118 if ((screens[i].c[c].name = strdup(val)) == NULL)
1122 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1123 i, ScreenCount(display));
1127 custom_region(char *val)
1129 unsigned int sidx, x, y, w, h;
1131 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1132 errx(1, "invalid custom region, "
1133 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
1134 if (sidx < 1 || sidx > ScreenCount(display))
1135 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1136 sidx, ScreenCount(display));
1140 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
1142 if (x < 0 || x > DisplayWidth(display, sidx) ||
1143 y < 0 || y > DisplayHeight(display, sidx) ||
1144 w + x > DisplayWidth(display, sidx) ||
1145 h + y > DisplayHeight(display, sidx)) {
1146 fprintf(stderr, "ignoring region %ux%u+%u+%u "
1147 "- not within screen boundaries "
1148 "(%ux%u)\n", w, h, x, y,
1149 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1153 new_region(&screens[sidx], x, y, w, h);
1157 socket_setnonblock(int fd)
1161 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1162 err(1, "fcntl F_GETFL");
1163 flags |= O_NONBLOCK;
1164 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1165 err(1, "fcntl F_SETFL");
1169 bar_print(struct swm_region *r, char *s)
1171 XClearWindow(display, r->bar_window);
1172 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
1173 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
1178 bar_extra_stop(void)
1182 bzero(bar_pipe, sizeof bar_pipe);
1185 kill(bar_pid, SIGTERM);
1188 strlcpy(bar_ext, "", sizeof bar_ext);
1193 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1195 int do_class, do_name;
1197 XClassHint *xch = NULL;
1199 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
1200 cur_focus != NULL) {
1201 if ((xch = XAllocClassHint()) == NULL)
1203 status = XGetClassHint(display, cur_focus->id, xch);
1204 if (status == BadWindow || status == BadAlloc)
1206 do_class = (title_class_enabled && xch->res_class != NULL);
1207 do_name = (title_name_enabled && xch->res_name != NULL);
1209 strlcat(s, xch->res_class, sz);
1210 if (do_class && do_name)
1211 strlcat(s, ":", sz);
1213 strlcat(s, xch->res_name, sz);
1214 strlcat(s, " ", sz);
1222 bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1226 if (window_name_enabled && cur_focus != NULL) {
1227 XFetchName(display, cur_focus->id, &title);
1229 if (cur_focus->floating)
1230 strlcat(s, "(f) ", sz);
1231 strlcat(s, title, sz);
1232 strlcat(s, " ", sz);
1243 struct swm_region *r;
1246 char s[SWM_BAR_MAX];
1247 char cn[SWM_BAR_MAX];
1248 char loc[SWM_BAR_MAX];
1252 if (bar_enabled == 0)
1254 if (bar_extra && bar_extra_running) {
1255 /* ignore short reads; it'll correct itself */
1256 while ((b = fgetln(stdin, &len)) != NULL)
1257 if (b && b[len - 1] == '\n') {
1259 strlcpy(bar_ext, b, sizeof bar_ext);
1261 if (b == NULL && errno != EAGAIN) {
1262 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
1263 errno, strerror(errno));
1267 strlcpy(bar_ext, "", sizeof bar_ext);
1269 if (clock_enabled == 0)
1270 strlcpy(s, "", sizeof s);
1273 localtime_r(&tmt, &tm);
1274 strftime(s, sizeof s, clock_format, &tm);
1275 strlcat(s, " ", sizeof s);
1278 for (i = 0; i < ScreenCount(display); i++) {
1280 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1281 strlcpy(cn, "", sizeof cn);
1283 bar_class_name(cn, sizeof cn, r->ws->focus);
1284 bar_window_name(cn, sizeof cn, r->ws->focus);
1288 stack = r->ws->cur_layout->name;
1290 snprintf(loc, sizeof loc, "%d:%d %s %s%s %s %s",
1291 x++, r->ws->idx + 1, stack, s, cn, bar_ext,
1306 bar_toggle(struct swm_region *r, union arg *args)
1308 struct swm_region *tmpr;
1309 int i, sc = ScreenCount(display);
1311 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
1314 for (i = 0; i < sc; i++)
1315 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1316 XUnmapWindow(display, tmpr->bar_window);
1318 for (i = 0; i < sc; i++)
1319 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1320 XMapRaised(display, tmpr->bar_window);
1322 bar_enabled = !bar_enabled;
1325 /* must be after stack */
1332 XSetWindowAttributes wa;
1333 struct swm_region *r;
1336 /* do this here because the conf file is in memory */
1337 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1338 /* launch external status app */
1339 bar_extra_running = 1;
1340 if (pipe(bar_pipe) == -1)
1341 err(1, "pipe error");
1342 socket_setnonblock(bar_pipe[0]);
1343 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1344 if (dup2(bar_pipe[0], 0) == -1)
1346 if (dup2(bar_pipe[1], 1) == -1)
1348 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1349 err(1, "could not disable SIGPIPE");
1350 switch (bar_pid = fork()) {
1352 err(1, "cannot fork");
1356 execvp(bar_argv[0], bar_argv);
1357 err(1, "%s external app failed", bar_argv[0]);
1359 default: /* parent */
1365 bzero(&wa, sizeof wa);
1366 for (i = 0; i < ScreenCount(display); i++)
1367 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1369 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1370 wa.background_pixel =
1371 screens[i].c[SWM_S_COLOR_BAR].color;
1372 XChangeWindowAttributes(display, r->bar_window,
1373 CWBackPixel | CWBorderPixel, &wa);
1379 bar_setup(struct swm_region *r)
1384 XFreeFont(display, bar_fs);
1388 for (i = 0; bar_fonts[i] != NULL; i++) {
1389 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
1395 if (bar_fonts[i] == NULL)
1396 errx(1, "couldn't load font");
1398 errx(1, "couldn't create font structure");
1400 bar_height = bar_fs->ascent + bar_fs->descent + 1 +
1401 2 * bar_border_width;
1403 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1405 r->bar_window = XCreateSimpleWindow(display,
1406 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width,
1407 bar_height - 2 * bar_border_width,
1408 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1409 r->s->c[SWM_S_COLOR_BAR].color);
1410 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
1411 XSetFont(display, bar_gc, bar_fs->fid);
1412 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1414 XMapRaised(display, r->bar_window);
1415 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
1417 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1418 err(1, "could not install bar_signal");
1423 drain_enter_notify(void)
1428 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1431 DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained %d\n", i);
1435 set_win_state(struct ws_win *win, long state)
1437 long data[] = {state, None};
1439 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1444 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1445 (unsigned char *)data, 2);
1452 unsigned char *p = NULL;
1455 if (!get_property(w, astate, 2L, astate, &n, &p))
1458 result = *((long *)p);
1464 version(struct swm_region *r, union arg *args)
1466 bar_version = !bar_version;
1468 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
1469 SWM_VERSION, cvstag);
1471 strlcpy(bar_vertext, "", sizeof bar_vertext);
1476 client_msg(struct ws_win *win, Atom a)
1478 XClientMessageEvent cm;
1483 bzero(&cm, sizeof cm);
1484 cm.type = ClientMessage;
1485 cm.window = win->id;
1486 cm.message_type = aprot;
1489 cm.data.l[1] = CurrentTime;
1490 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1494 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1502 DNPRINTF(SWM_D_MISC,
1503 "config_win: win %lu x %d y %d w %d h %d\n",
1504 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1506 ce.type = ConfigureNotify;
1507 ce.display = display;
1509 ce.window = win->id;
1512 ce.width = win->g.w;
1513 ce.height = win->g.h;
1514 ce.border_width = border_width;
1516 ce.override_redirect = False;
1518 DNPRINTF(SWM_D_MISC,
1519 "config_win: ev win %lu x %d y %d w %d h %d\n",
1520 ev->window, ev->x, ev->y, ev->width, ev->height);
1521 ce.type = ConfigureNotify;
1522 ce.display = ev->display;
1523 ce.event = ev->window;
1524 ce.window = ev->window;
1527 ce.width = ev->width;
1528 ce.height = ev->height;
1529 ce.border_width = ev->border_width;
1530 ce.above = ev->above;
1531 ce.override_redirect = False;
1534 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1538 count_win(struct workspace *ws, int count_transient)
1543 TAILQ_FOREACH(win, &ws->winlist, entry) {
1544 if (count_transient == 0 && win->floating)
1546 if (count_transient == 0 && win->transient)
1552 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1558 quit(struct swm_region *r, union arg *args)
1560 DNPRINTF(SWM_D_MISC, "quit\n");
1565 unmap_window(struct ws_win *win)
1570 /* don't unmap again */
1571 if (getstate(win->id) == IconicState)
1574 set_win_state(win, IconicState);
1576 XUnmapWindow(display, win->id);
1577 XSetWindowBorder(display, win->id,
1578 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1587 for (i = 0; i < ScreenCount(display); i++)
1588 for (j = 0; j < SWM_WS_MAX; j++)
1589 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1594 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1601 event.display = display; /* Ignored, but what the hell */
1602 event.window = win->id;
1603 event.root = win->s->root;
1604 event.subwindow = None;
1605 event.time = CurrentTime;
1610 event.same_screen = True;
1611 event.keycode = XKeysymToKeycode(display, keysym);
1612 event.state = modifiers;
1614 event.type = KeyPress;
1615 XSendEvent(event.display, event.window, True,
1616 KeyPressMask, (XEvent *)&event);
1618 event.type = KeyRelease;
1619 XSendEvent(event.display, event.window, True,
1620 KeyPressMask, (XEvent *)&event);
1625 restart(struct swm_region *r, union arg *args)
1627 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1629 /* disable alarm because the following code may not be interrupted */
1631 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1632 errx(1, "can't disable alarm");
1637 XCloseDisplay(display);
1638 execvp(start_argv[0], start_argv);
1639 fprintf(stderr, "execvp failed\n");
1645 root_to_region(Window root)
1647 struct swm_region *r = NULL;
1649 int i, x, y, wx, wy;
1652 for (i = 0; i < ScreenCount(display); i++)
1653 if (screens[i].root == root)
1656 if (XQueryPointer(display, screens[i].root,
1657 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1658 /* choose a region based on pointer location */
1659 TAILQ_FOREACH(r, &screens[i].rl, entry)
1660 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1661 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1666 r = TAILQ_FIRST(&screens[i].rl);
1672 find_unmanaged_window(Window id)
1677 for (i = 0; i < ScreenCount(display); i++)
1678 for (j = 0; j < SWM_WS_MAX; j++)
1679 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1687 find_window(Window id)
1690 Window wrr, wpr, *wcr = NULL;
1694 for (i = 0; i < ScreenCount(display); i++)
1695 for (j = 0; j < SWM_WS_MAX; j++)
1696 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1700 /* if we were looking for the parent return that window instead */
1701 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1706 /* ignore not found and root */
1707 if (wpr == 0 || wrr == wpr)
1710 /* look for parent */
1711 for (i = 0; i < ScreenCount(display); i++)
1712 for (j = 0; j < SWM_WS_MAX; j++)
1713 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1721 spawn(int ws_idx, union arg *args, int close_fd)
1726 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1729 close(ConnectionNumber(display));
1731 setenv("LD_PRELOAD", SWM_LIB, 1);
1733 if (asprintf(&ret, "%d", ws_idx) == -1) {
1737 setenv("_SWM_WS", ret, 1);
1741 if (asprintf(&ret, "%d", getpid()) == -1) {
1745 setenv("_SWM_PID", ret, 1);
1749 if (setsid() == -1) {
1756 * close stdin and stdout to prevent interaction between apps
1757 * and the baraction script
1758 * leave stderr open to record errors
1760 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
1764 dup2(fd, STDIN_FILENO);
1765 dup2(fd, STDOUT_FILENO);
1770 execvp(args->argv[0], args->argv);
1777 spawnterm(struct swm_region *r, union arg *args)
1779 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1783 setenv("_SWM_XTERM_FONTADJ", "", 1);
1784 spawn(r->ws->idx, args, 1);
1789 kill_refs(struct ws_win *win)
1792 struct swm_region *r;
1793 struct workspace *ws;
1798 for (i = 0; i < ScreenCount(display); i++)
1799 TAILQ_FOREACH(r, &screens[i].rl, entry)
1800 for (x = 0; x < SWM_WS_MAX; x++) {
1802 if (win == ws->focus)
1804 if (win == ws->focus_prev)
1805 ws->focus_prev = NULL;
1810 validate_win(struct ws_win *testwin)
1813 struct workspace *ws;
1814 struct swm_region *r;
1815 int i, x, foundit = 0;
1817 if (testwin == NULL)
1820 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1821 TAILQ_FOREACH(r, &screens[i].rl, entry)
1822 for (x = 0; x < SWM_WS_MAX; x++) {
1824 TAILQ_FOREACH(win, &ws->winlist, entry)
1832 validate_ws(struct workspace *testws)
1834 struct swm_region *r;
1835 struct workspace *ws;
1838 /* validate all ws */
1839 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1840 TAILQ_FOREACH(r, &screens[i].rl, entry)
1841 for (x = 0; x < SWM_WS_MAX; x++) {
1850 unfocus_win(struct ws_win *win)
1855 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1859 if (win->ws == NULL)
1862 if (validate_ws(win->ws))
1863 return; /* XXX this gets hit with thunderbird, needs fixing */
1865 if (win->ws->r == NULL)
1868 if (validate_win(win)) {
1873 if (win->ws->focus == win) {
1874 win->ws->focus = NULL;
1875 win->ws->focus_prev = win;
1878 if (validate_win(win->ws->focus)) {
1879 kill_refs(win->ws->focus);
1880 win->ws->focus = NULL;
1882 if (validate_win(win->ws->focus_prev)) {
1883 kill_refs(win->ws->focus_prev);
1884 win->ws->focus_prev = NULL;
1887 /* drain all previous unfocus events */
1888 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
1891 grabbuttons(win, 0);
1892 XSetWindowBorder(display, win->id,
1893 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1895 XChangeProperty(display, win->s->root,
1896 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1897 PropModeReplace, (unsigned char *)&none,1);
1906 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
1908 for (i = 0; i < ScreenCount(display); i++)
1909 for (j = 0; j < SWM_WS_MAX; j++)
1910 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1915 focus_win(struct ws_win *win)
1920 struct ws_win *cfw = NULL;
1923 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1927 if (win->ws == NULL)
1930 if (validate_ws(win->ws))
1931 return; /* XXX this gets hit with thunderbird, needs fixing */
1933 if (validate_win(win)) {
1938 if (validate_win(win)) {
1943 XGetInputFocus(display, &cur_focus, &rr);
1944 if ((cfw = find_window(cur_focus)) != NULL)
1947 /* use larger hammer since the window was killed somehow */
1948 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
1949 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
1950 XSetWindowBorder(display, cfw->id,
1951 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1954 win->ws->focus = win;
1956 if (win->ws->r != NULL) {
1957 /* drain all previous focus events */
1958 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
1962 XSetInputFocus(display, win->id,
1963 RevertToParent, CurrentTime);
1964 grabbuttons(win, 1);
1965 XSetWindowBorder(display, win->id,
1966 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1967 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
1968 win->ws->always_raise)
1969 XMapRaised(display, win->id);
1971 XChangeProperty(display, win->s->root,
1972 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1973 PropModeReplace, (unsigned char *)&win->id,1);
1976 if (window_name_enabled)
1981 switchws(struct swm_region *r, union arg *args)
1983 int wsid = args->id, unmap_old = 0;
1984 struct swm_region *this_r, *other_r;
1986 struct workspace *new_ws, *old_ws;
1993 old_ws = this_r->ws;
1994 new_ws = &this_r->s->ws[wsid];
1996 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1997 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
2000 if (new_ws == NULL || old_ws == NULL)
2002 if (new_ws == old_ws)
2005 other_r = new_ws->r;
2006 if (other_r == NULL) {
2007 /* the other workspace is hidden, hide this one */
2011 /* the other ws is visible in another region, exchange them */
2012 other_r->ws_prior = new_ws;
2013 other_r->ws = old_ws;
2014 old_ws->r = other_r;
2016 this_r->ws_prior = old_ws;
2017 this_r->ws = new_ws;
2020 /* this is needed so that we can click on a window after a restart */
2024 a.id = SWM_ARG_ID_FOCUSCUR;
2025 focus(new_ws->r, &a);
2029 /* unmap old windows */
2031 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2034 if (focus_mode == SWM_FOCUS_DEFAULT)
2035 drain_enter_notify();
2039 cyclews(struct swm_region *r, union arg *args)
2042 struct swm_screen *s = r->s;
2044 DNPRINTF(SWM_D_WS, "cyclews id %d "
2045 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2046 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2051 case SWM_ARG_ID_CYCLEWS_UP:
2052 if (a.id < SWM_WS_MAX - 1)
2057 case SWM_ARG_ID_CYCLEWS_DOWN:
2061 a.id = SWM_WS_MAX - 1;
2067 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
2069 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2073 } while (a.id != r->ws->idx);
2077 priorws(struct swm_region *r, union arg *args)
2081 DNPRINTF(SWM_D_WS, "priorws id %d "
2082 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2083 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2085 if (r->ws_prior == NULL)
2088 a.id = r->ws_prior->idx;
2093 cyclescr(struct swm_region *r, union arg *args)
2095 struct swm_region *rr = NULL;
2099 /* do nothing if we don't have more than one screen */
2100 if (!(ScreenCount(display) > 1 || outputs > 1))
2105 case SWM_ARG_ID_CYCLESC_UP:
2106 rr = TAILQ_NEXT(r, entry);
2108 rr = TAILQ_FIRST(&screens[i].rl);
2110 case SWM_ARG_ID_CYCLESC_DOWN:
2111 rr = TAILQ_PREV(r, swm_region_list, entry);
2113 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2121 /* move mouse to region */
2123 y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
2124 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2126 a.id = SWM_ARG_ID_FOCUSCUR;
2129 if (rr->ws->focus) {
2130 /* move to focus window */
2131 x = rr->ws->focus->g.x + 1;
2132 y = rr->ws->focus->g.y + 1;
2133 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2138 sort_windows(struct ws_win_list *wl)
2140 struct ws_win *win, *parent, *nxt;
2145 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2146 nxt = TAILQ_NEXT(win, entry);
2147 if (win->transient) {
2148 parent = find_window(win->transient);
2149 if (parent == NULL) {
2150 fprintf(stderr, "not possible bug\n");
2153 TAILQ_REMOVE(wl, win, entry);
2154 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2161 swapwin(struct swm_region *r, union arg *args)
2163 struct ws_win *target, *source;
2164 struct ws_win *cur_focus;
2165 struct ws_win_list *wl;
2168 DNPRINTF(SWM_D_WS, "swapwin id %d "
2169 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
2170 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2172 cur_focus = r->ws->focus;
2173 if (cur_focus == NULL)
2177 wl = &source->ws->winlist;
2180 case SWM_ARG_ID_SWAPPREV:
2181 if (source->transient)
2182 source = find_window(source->transient);
2183 target = TAILQ_PREV(source, ws_win_list, entry);
2184 if (target && target->transient)
2185 target = find_window(target->transient);
2186 TAILQ_REMOVE(wl, source, entry);
2188 TAILQ_INSERT_TAIL(wl, source, entry);
2190 TAILQ_INSERT_BEFORE(target, source, entry);
2192 case SWM_ARG_ID_SWAPNEXT:
2193 target = TAILQ_NEXT(source, entry);
2194 /* move the parent and let the sort handle the move */
2195 if (source->transient)
2196 source = find_window(source->transient);
2197 TAILQ_REMOVE(wl, source, entry);
2199 TAILQ_INSERT_HEAD(wl, source, entry);
2201 TAILQ_INSERT_AFTER(wl, target, source, entry);
2203 case SWM_ARG_ID_SWAPMAIN:
2204 target = TAILQ_FIRST(wl);
2205 if (target == source) {
2206 if (source->ws->focus_prev != NULL &&
2207 source->ws->focus_prev != target)
2209 source = source->ws->focus_prev;
2213 if (target == NULL || source == NULL)
2215 source->ws->focus_prev = target;
2216 TAILQ_REMOVE(wl, target, entry);
2217 TAILQ_INSERT_BEFORE(source, target, entry);
2218 TAILQ_REMOVE(wl, source, entry);
2219 TAILQ_INSERT_HEAD(wl, source, entry);
2221 case SWM_ARG_ID_MOVELAST:
2222 TAILQ_REMOVE(wl, source, entry);
2223 TAILQ_INSERT_TAIL(wl, source, entry);
2226 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
2236 focus_prev(struct ws_win *win)
2238 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2239 struct ws_win *cur_focus = NULL;
2240 struct ws_win_list *wl = NULL;
2241 struct workspace *ws = NULL;
2243 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
2245 if (!(win && win->ws))
2250 cur_focus = ws->focus;
2251 winlostfocus = cur_focus;
2253 /* pickle, just focus on whatever */
2254 if (cur_focus == NULL) {
2255 /* use prev_focus if valid */
2256 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2257 find_window(WINID(ws->focus_prev)))
2258 winfocus = ws->focus_prev;
2259 if (winfocus == NULL)
2260 winfocus = TAILQ_FIRST(wl);
2264 /* if transient focus on parent */
2265 if (cur_focus->transient) {
2266 winfocus = find_window(cur_focus->transient);
2270 /* if in max_stack try harder */
2271 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2272 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2273 if (cur_focus != ws->focus_prev)
2274 winfocus = ws->focus_prev;
2275 else if (cur_focus != ws->focus)
2276 winfocus = ws->focus;
2278 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2283 if (cur_focus == win)
2284 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2285 if (winfocus == NULL)
2286 winfocus = TAILQ_LAST(wl, ws_win_list);
2287 if (winfocus == NULL || winfocus == win)
2288 winfocus = TAILQ_NEXT(cur_focus, entry);
2290 if (winfocus == winlostfocus || winfocus == NULL)
2293 focus_magic(winfocus);
2297 focus(struct swm_region *r, union arg *args)
2299 struct ws_win *winfocus = NULL, *winlostfocus = NULL, *head;
2300 struct ws_win *cur_focus = NULL;
2301 struct ws_win_list *wl = NULL;
2302 struct workspace *ws = NULL;
2307 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
2309 /* treat FOCUS_CUR special */
2310 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2311 if (r->ws->focus && r->ws->focus->iconic == 0)
2312 winfocus = r->ws->focus;
2313 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2314 winfocus = r->ws->focus_prev;
2316 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2317 if (winfocus->iconic == 0)
2320 focus_magic(winfocus);
2324 if ((cur_focus = r->ws->focus) == NULL)
2329 winlostfocus = cur_focus;
2332 case SWM_ARG_ID_FOCUSPREV:
2333 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2335 head = TAILQ_LAST(wl, ws_win_list);
2337 if (WINID(winfocus) == cur_focus->transient) {
2338 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2340 head = TAILQ_LAST(wl, ws_win_list);
2345 if (winfocus && winfocus->iconic) {
2346 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
2347 if (winfocus->iconic == 0)
2352 case SWM_ARG_ID_FOCUSNEXT:
2353 head = TAILQ_NEXT(cur_focus, entry);
2355 head = TAILQ_FIRST(wl);
2359 if (winfocus && winfocus->iconic) {
2360 TAILQ_FOREACH(winfocus, wl, entry)
2361 if (winfocus->iconic == 0)
2366 case SWM_ARG_ID_FOCUSMAIN:
2367 winfocus = TAILQ_FIRST(wl);
2368 if (winfocus == cur_focus)
2369 winfocus = cur_focus->ws->focus_prev;
2375 if (winfocus == winlostfocus || winfocus == NULL)
2378 focus_magic(winfocus);
2382 cycle_layout(struct swm_region *r, union arg *args)
2384 struct workspace *ws = r->ws;
2385 struct ws_win *winfocus;
2388 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2390 winfocus = ws->focus;
2393 if (ws->cur_layout->l_stack == NULL)
2394 ws->cur_layout = &layouts[0];
2397 if (focus_mode == SWM_FOCUS_DEFAULT)
2398 drain_enter_notify();
2399 a.id = SWM_ARG_ID_FOCUSCUR;
2405 stack_config(struct swm_region *r, union arg *args)
2407 struct workspace *ws = r->ws;
2409 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
2412 if (ws->cur_layout->l_config != NULL)
2413 ws->cur_layout->l_config(ws, args->id);
2415 if (args->id != SWM_ARG_ID_STACKINIT)
2421 struct swm_geometry g;
2422 struct swm_region *r;
2425 DNPRINTF(SWM_D_STACK, "stack\n");
2427 for (i = 0; i < ScreenCount(display); i++) {
2429 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2430 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
2431 "(screen %d, region %d)\n", r->ws->idx, i, j++);
2433 /* start with screen geometry, adjust for bar */
2435 g.w -= 2 * border_width;
2436 g.h -= 2 * border_width;
2442 r->ws->cur_layout->l_stack(r->ws, &g);
2443 /* save r so we can track region changes */
2450 if (focus_mode == SWM_FOCUS_DEFAULT)
2451 drain_enter_notify();
2455 store_float_geom(struct ws_win *win, struct swm_region *r)
2457 /* retain window geom and region geom */
2458 win->g_float.x = win->g.x;
2459 win->g_float.y = win->g.y;
2460 win->g_float.w = win->g.w;
2461 win->g_float.h = win->g.h;
2462 win->rg_float.x = r->g.x;
2463 win->rg_float.y = r->g.y;
2464 win->rg_float.w = r->g.w;
2465 win->rg_float.h = r->g.h;
2466 win->g_floatvalid = 1;
2470 stack_floater(struct ws_win *win, struct swm_region *r)
2478 bzero(&wc, sizeof wc);
2479 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2482 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2483 * geom on ws switches or return from max mode
2486 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2487 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2489 * use stored g and rg to set relative position and size
2490 * as in old region or before max stack mode
2492 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2493 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2494 win->g.w = win->g_float.w;
2495 win->g.h = win->g_float.h;
2496 win->g_floatvalid = 0;
2499 win->floatmaxed = 0;
2501 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
2502 (win->g.h >= HEIGHT(r)))
2503 wc.border_width = 0;
2505 wc.border_width = border_width;
2506 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2507 win->g.w = (double)WIDTH(r) * dialog_ratio;
2508 win->g.h = (double)HEIGHT(r) * dialog_ratio;
2513 * floaters and transients are auto-centred unless moved
2516 win->g.x = r->g.x + (WIDTH(r) - win->g.w) /
2517 2 - wc.border_width;
2518 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) /
2519 2 - wc.border_width;
2522 /* win can be outside r if new r smaller than old r */
2523 /* Ensure top left corner inside r (move probs otherwise) */
2524 if (win->g.x < r->g.x - wc.border_width)
2525 win->g.x = r->g.x - wc.border_width;
2526 if (win->g.x > r->g.x + r->g.w - 1)
2527 win->g.x = (win->g.w > r->g.w) ? r->g.x :
2528 (r->g.x + r->g.w - win->g.w - 2 * wc.border_width);
2529 if (win->g.y < r->g.y - wc.border_width)
2530 win->g.y = r->g.y - wc.border_width;
2531 if (win->g.y > r->g.y + r->g.h - 1)
2532 win->g.y = (win->g.h > r->g.h) ? r->g.y :
2533 (r->g.y + r->g.h - win->g.h - 2 * wc.border_width);
2537 wc.width = win->g.w;
2538 wc.height = win->g.h;
2541 * Retain floater and transient geometry for correct positioning
2542 * when ws changes region
2544 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2545 store_float_geom(win, r);
2547 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
2548 win->id, wc.x, wc.y, wc.width, wc.height);
2550 XConfigureWindow(display, win->id, mask, &wc);
2554 * Send keystrokes to terminal to decrease/increase the font size as the
2555 * window size changes.
2558 adjust_font(struct ws_win *win)
2560 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2561 win->floating || win->transient)
2564 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2565 win->g.w / win->sh.width_inc < term_width &&
2566 win->font_steps < SWM_MAX_FONT_STEPS) {
2567 win->font_size_boundary[win->font_steps] =
2568 (win->sh.width_inc * term_width) + win->sh.base_width;
2571 win->last_inc = win->sh.width_inc;
2572 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2573 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2574 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
2577 win->last_inc = win->sh.width_inc;
2578 fake_keypress(win, XK_KP_Add, ShiftMask);
2582 #define SWAPXY(g) do { \
2584 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2585 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2588 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2591 XWindowAttributes wa;
2592 struct swm_geometry win_g, r_g = *g;
2593 struct ws_win *win, *fs_win = 0;
2594 int i, j, s, stacks;
2595 int w_inc = 1, h_inc, w_base = 1, h_base;
2596 int hrh, extra = 0, h_slice, last_h = 0;
2597 int split, colno, winno, mwin, msize, mscale;
2598 int remain, missing, v_slice, reconfigure;
2601 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2602 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2604 winno = count_win(ws, 0);
2605 if (winno == 0 && count_win(ws, 1) == 0)
2608 TAILQ_FOREACH(win, &ws->winlist, entry)
2609 if (win->transient == 0 && win->floating == 0
2610 && win->iconic == 0)
2617 w_inc = win->sh.width_inc;
2618 w_base = win->sh.base_width;
2619 mwin = ws->l_state.horizontal_mwin;
2620 mscale = ws->l_state.horizontal_msize;
2621 stacks = ws->l_state.horizontal_stacks;
2624 w_inc = win->sh.height_inc;
2625 w_base = win->sh.base_height;
2626 mwin = ws->l_state.vertical_mwin;
2627 mscale = ws->l_state.vertical_msize;
2628 stacks = ws->l_state.vertical_stacks;
2632 if (stacks > winno - mwin)
2633 stacks = winno - mwin;
2637 h_slice = r_g.h / SWM_H_SLICE;
2638 if (mwin && winno > mwin) {
2639 v_slice = r_g.w / SWM_V_SLICE;
2643 win_g.w = v_slice * mscale;
2645 if (w_inc > 1 && w_inc < v_slice) {
2646 /* adjust for window's requested size increment */
2647 remain = (win_g.w - w_base) % w_inc;
2648 missing = w_inc - remain;
2655 win_g.x += r_g.w - msize;
2658 colno = split = winno / stacks;
2659 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
2660 2 * border_width) / stacks);
2662 hrh = r_g.h / colno;
2663 extra = r_g.h - (colno * hrh);
2664 win_g.h = hrh - 2 * border_width;
2666 /* stack all the tiled windows */
2667 i = j = 0, s = stacks;
2668 TAILQ_FOREACH(win, &ws->winlist, entry) {
2669 if (win->transient != 0 || win->floating != 0)
2671 if (win->iconic != 0)
2674 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2679 if (split && i == split) {
2680 colno = (winno - mwin) / stacks;
2681 if (s <= (winno - mwin) % stacks)
2683 split = split + colno;
2684 hrh = (r_g.h / colno);
2685 extra = r_g.h - (colno * hrh);
2689 win_g.x += win_g.w + 2 * border_width;
2690 win_g.w = (r_g.w - msize -
2691 (stacks * 2 * border_width)) / stacks;
2693 win_g.w += (r_g.w - msize -
2694 (stacks * 2 * border_width)) % stacks;
2698 win_g.h = hrh - 2 * border_width;
2700 h_inc = win->sh.width_inc;
2701 h_base = win->sh.base_width;
2703 h_inc = win->sh.height_inc;
2704 h_base = win->sh.base_height;
2706 if (j == colno - 1) {
2707 win_g.h = hrh + extra;
2708 } else if (h_inc > 1 && h_inc < h_slice) {
2709 /* adjust for window's requested size increment */
2710 remain = (win_g.h - h_base) % h_inc;
2711 missing = h_inc - remain;
2713 if (missing <= extra || j == 0) {
2725 win_g.y += last_h + 2 * border_width;
2727 bzero(&wc, sizeof wc);
2728 if (disable_border && bar_enabled == 0 && winno == 1){
2729 wc.border_width = 0;
2730 win_g.w += 2 * border_width;
2731 win_g.h += 2 * border_width;
2733 wc.border_width = border_width;
2736 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2737 win->g.w != win_g.h || win->g.h != win_g.w) {
2739 win->g.x = wc.x = win_g.y;
2740 win->g.y = wc.y = win_g.x;
2741 win->g.w = wc.width = win_g.h;
2742 win->g.h = wc.height = win_g.w;
2745 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2746 win->g.w != win_g.w || win->g.h != win_g.h) {
2748 win->g.x = wc.x = win_g.x;
2749 win->g.y = wc.y = win_g.y;
2750 win->g.w = wc.width = win_g.w;
2751 win->g.h = wc.height = win_g.h;
2756 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2757 XConfigureWindow(display, win->id, mask, &wc);
2760 if (XGetWindowAttributes(display, win->id, &wa))
2761 if (wa.map_state == IsUnmapped)
2762 XMapRaised(display, win->id);
2770 /* now, stack all the floaters and transients */
2771 TAILQ_FOREACH(win, &ws->winlist, entry) {
2772 if (win->transient == 0 && win->floating == 0)
2774 if (win->iconic == 1)
2776 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2781 stack_floater(win, ws->r);
2782 XMapRaised(display, win->id);
2786 stack_floater(fs_win, ws->r);
2787 XMapRaised(display, fs_win->id);
2792 vertical_config(struct workspace *ws, int id)
2794 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2797 case SWM_ARG_ID_STACKRESET:
2798 case SWM_ARG_ID_STACKINIT:
2799 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2800 ws->l_state.vertical_mwin = 1;
2801 ws->l_state.vertical_stacks = 1;
2803 case SWM_ARG_ID_MASTERSHRINK:
2804 if (ws->l_state.vertical_msize > 1)
2805 ws->l_state.vertical_msize--;
2807 case SWM_ARG_ID_MASTERGROW:
2808 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2809 ws->l_state.vertical_msize++;
2811 case SWM_ARG_ID_MASTERADD:
2812 ws->l_state.vertical_mwin++;
2814 case SWM_ARG_ID_MASTERDEL:
2815 if (ws->l_state.vertical_mwin > 0)
2816 ws->l_state.vertical_mwin--;
2818 case SWM_ARG_ID_STACKINC:
2819 ws->l_state.vertical_stacks++;
2821 case SWM_ARG_ID_STACKDEC:
2822 if (ws->l_state.vertical_stacks > 1)
2823 ws->l_state.vertical_stacks--;
2831 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2833 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2835 stack_master(ws, g, 0, 0);
2839 horizontal_config(struct workspace *ws, int id)
2841 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2844 case SWM_ARG_ID_STACKRESET:
2845 case SWM_ARG_ID_STACKINIT:
2846 ws->l_state.horizontal_mwin = 1;
2847 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2848 ws->l_state.horizontal_stacks = 1;
2850 case SWM_ARG_ID_MASTERSHRINK:
2851 if (ws->l_state.horizontal_msize > 1)
2852 ws->l_state.horizontal_msize--;
2854 case SWM_ARG_ID_MASTERGROW:
2855 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2856 ws->l_state.horizontal_msize++;
2858 case SWM_ARG_ID_MASTERADD:
2859 ws->l_state.horizontal_mwin++;
2861 case SWM_ARG_ID_MASTERDEL:
2862 if (ws->l_state.horizontal_mwin > 0)
2863 ws->l_state.horizontal_mwin--;
2865 case SWM_ARG_ID_STACKINC:
2866 ws->l_state.horizontal_stacks++;
2868 case SWM_ARG_ID_STACKDEC:
2869 if (ws->l_state.horizontal_stacks > 1)
2870 ws->l_state.horizontal_stacks--;
2878 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2880 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
2882 stack_master(ws, g, 1, 0);
2885 /* fullscreen view */
2887 max_stack(struct workspace *ws, struct swm_geometry *g)
2890 struct swm_geometry gg = *g;
2891 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2895 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2900 winno = count_win(ws, 0);
2901 if (winno == 0 && count_win(ws, 1) == 0)
2904 TAILQ_FOREACH(win, &ws->winlist, entry) {
2905 if (win->transient) {
2907 parent = find_window(win->transient);
2911 if (win->floating && win->floatmaxed == 0 ) {
2913 * retain geometry for retrieval on exit from
2916 store_float_geom(win, ws->r);
2917 win->floatmaxed = 1;
2920 /* only reconfigure if necessary */
2921 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2923 bzero(&wc, sizeof wc);
2924 win->g.x = wc.x = gg.x;
2925 win->g.y = wc.y = gg.y;
2927 wc.border_width = border_width;
2928 win->g.w = wc.width = gg.w;
2929 win->g.h = wc.height = gg.h;
2931 wc.border_width = 0;
2932 win->g.w = wc.width = gg.w + 2 * border_width;
2933 win->g.h = wc.height = gg.h + 2 * border_width;
2935 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2936 XConfigureWindow(display, win->id, mask, &wc);
2938 /* unmap only if we don't have multi screen */
2939 if (win != ws->focus)
2940 if (!(ScreenCount(display) > 1 || outputs > 1))
2944 /* put the last transient on top */
2947 XMapRaised(display, parent->id);
2948 stack_floater(wintrans, ws->r);
2949 focus_magic(wintrans);
2954 send_to_ws(struct swm_region *r, union arg *args)
2956 int wsid = args->id;
2957 struct ws_win *win = NULL, *parent;
2958 struct workspace *ws, *nws;
2959 Atom ws_idx_atom = 0;
2960 unsigned char ws_idx_str[SWM_PROPLEN];
2969 if (win->ws->idx == wsid)
2972 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2975 nws = &win->s->ws[wsid];
2977 a.id = SWM_ARG_ID_FOCUSPREV;
2979 if (win->transient) {
2980 parent = find_window(win->transient);
2982 unmap_window(parent);
2983 TAILQ_REMOVE(&ws->winlist, parent, entry);
2984 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
2989 TAILQ_REMOVE(&ws->winlist, win, entry);
2990 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2993 /* Try to update the window's workspace property */
2994 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2996 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2997 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2999 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3000 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3007 raise_toggle(struct swm_region *r, union arg *args)
3009 if (r && r->ws == NULL)
3012 r->ws->always_raise = !r->ws->always_raise;
3014 /* bring floaters back to top */
3015 if (r->ws->always_raise == 0)
3020 iconify(struct swm_region *r, union arg *args)
3024 if (r->ws->focus == NULL)
3026 unmap_window(r->ws->focus);
3027 update_iconic(r->ws->focus, 1);
3029 if (focus_mode == SWM_FOCUS_DEFAULT)
3030 drain_enter_notify();
3031 r->ws->focus = NULL;
3032 a.id = SWM_ARG_ID_FOCUSCUR;
3037 get_win_name(Display *dpy, Window win, Atom wname, Atom stype,
3038 unsigned long *slen)
3041 unsigned long nitems, nbytes, nextra;
3042 unsigned char *prop = NULL;
3045 status = XGetWindowProperty(dpy, win, wname, 0L, 0L, False, stype,
3046 &rettype, &retfmt, &nitems, &nbytes, &prop);
3047 if (status != Success)
3051 status = XGetWindowProperty(dpy, win, wname, 0L, nbytes, False,
3052 stype, &rettype, &retfmt, &nitems, &nextra, &prop);
3053 if (status != Success) {
3057 if (rettype != stype) {
3066 uniconify(struct swm_region *r, union arg *args)
3074 DNPRINTF(SWM_D_MISC, "uniconify\n");
3076 if (r && r->ws == NULL)
3079 /* make sure we have anything to uniconify */
3080 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3081 if (win->ws == NULL)
3082 continue; /* should never happen */
3083 if (win->iconic == 0)
3092 spawn_select(r, args, "uniconify", &searchpid);
3094 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3097 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3098 if (win->ws == NULL)
3099 continue; /* should never happen */
3100 if (win->iconic == 0)
3103 name = get_win_name(display, win->id, a_wmname, a_string,
3107 fprintf(lfile, "%s.%lu\n", name, win->id);
3114 #define MAX_RESP_LEN 1024
3117 search_do_resp(void)
3121 char *name, *resp, *s;
3124 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3129 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3130 fprintf(stderr, "search: calloc\n");
3134 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3136 fprintf(stderr, "search: read error: %s\n", strerror(errno));
3139 resp[rbytes] = '\0';
3142 DNPRINTF(SWM_D_MISC, "search_do_resp: resp %s\n", resp);
3143 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3144 if (win->iconic == 0)
3146 name = get_win_name(display, win->id, a_wmname, a_string, &len);
3149 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3154 if (strncmp(s, resp, len) == 0) {
3155 /* XXX this should be a callback to generalize */
3156 update_iconic(win, 0);
3167 wkill(struct swm_region *r, union arg *args)
3169 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
3171 if (r->ws->focus == NULL)
3174 if (args->id == SWM_ARG_ID_KILLWINDOW)
3175 XKillClient(display, r->ws->focus->id);
3177 if (r->ws->focus->can_delete)
3178 client_msg(r->ws->focus, adelete);
3183 floating_toggle_win(struct ws_win *win)
3185 struct swm_region *r;
3195 /* reject floating toggles in max stack mode */
3196 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3199 if (win->floating) {
3200 if (!win->floatmaxed) {
3201 /* retain position for refloat */
3202 store_float_geom(win, r);
3206 if (win->g_floatvalid) {
3207 /* refloat at last floating relative position */
3208 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
3209 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
3210 win->g.w = win->g_float.w;
3211 win->g.h = win->g_float.h;
3216 ewmh_update_actions(win);
3222 floating_toggle(struct swm_region *r, union arg *args)
3224 struct ws_win *win = r->ws->focus;
3230 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3231 _NET_WM_STATE_TOGGLE);
3234 if (focus_mode == SWM_FOCUS_DEFAULT)
3235 drain_enter_notify();
3237 if (win == win->ws->focus) {
3238 a.id = SWM_ARG_ID_FOCUSCUR;
3239 focus(win->ws->r, &a);
3244 resize_window(struct ws_win *win, int center)
3248 struct swm_region *r;
3250 r = root_to_region(win->wa.root);
3251 bzero(&wc, sizeof wc);
3252 mask = CWBorderWidth | CWWidth | CWHeight;
3253 wc.border_width = border_width;
3254 wc.width = win->g.w;
3255 wc.height = win->g.h;
3256 if (center == SWM_ARG_ID_CENTER) {
3257 wc.x = (WIDTH(r) - win->g.w) / 2 - border_width;
3258 wc.y = (HEIGHT(r) - win->g.h) / 2 - border_width;
3262 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
3263 win->id, wc.x, wc.y, wc.width, wc.height);
3265 XConfigureWindow(display, win->id, mask, &wc);
3269 resize(struct ws_win *win, union arg *args)
3273 struct swm_region *r = win->ws->r;
3277 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
3278 win->id, win->floating, win->transient);
3280 if (!(win->transient != 0 || win->floating != 0))
3283 /* reject resizes in max mode for floaters (transient ok) */
3284 if (win->floatmaxed)
3288 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3292 if (focus_mode == SWM_FOCUS_DEFAULT)
3293 drain_enter_notify();
3295 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3296 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3299 /* place pointer at bottom left corner or nearest point inside r */
3300 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
3301 relx = win->g.w - 1;
3303 relx = r->g.x + r->g.w - win->g.x - 1;
3305 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
3306 rely = win->g.h - 1;
3308 rely = r->g.y + r->g.h - win->g.y - 1;
3310 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
3312 XMaskEvent(display, MOUSEMASK | ExposureMask |
3313 SubstructureRedirectMask, &ev);
3315 case ConfigureRequest:
3318 handler[ev.type](&ev);
3321 /* do not allow resize outside of region */
3322 if ( ev.xmotion.x_root < r->g.x ||
3323 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3324 ev.xmotion.y_root < r->g.y ||
3325 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3328 if (ev.xmotion.x <= 1)
3330 if (ev.xmotion.y <= 1)
3332 win->g.w = ev.xmotion.x + 1;
3333 win->g.h = ev.xmotion.y + 1;
3335 /* not free, don't sync more than 120 times / second */
3336 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3337 time = ev.xmotion.time;
3338 XSync(display, False);
3339 resize_window(win, args->id);
3343 } while (ev.type != ButtonRelease);
3345 XSync(display, False);
3346 resize_window(win, args->id);
3348 store_float_geom(win,r);
3350 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
3352 XUngrabPointer(display, CurrentTime);
3355 drain_enter_notify();
3359 move_window(struct ws_win *win)
3363 struct swm_region *r;
3365 r = root_to_region(win->wa.root);
3366 bzero(&wc, sizeof wc);
3370 wc.border_width = border_width;
3372 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
3373 win->id, wc.x, wc.y, wc.width, wc.height);
3375 XConfigureWindow(display, win->id, mask, &wc);
3379 move(struct ws_win *win, union arg *args)
3383 struct swm_region *r = win->ws->r;
3385 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
3386 win->id, win->floating, win->transient);
3388 /* in max_stack mode should only move transients */
3389 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
3393 if (win->floating == 0 && !win->transient) {
3394 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3397 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3402 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3403 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3405 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3407 XMaskEvent(display, MOUSEMASK | ExposureMask |
3408 SubstructureRedirectMask, &ev);
3410 case ConfigureRequest:
3413 handler[ev.type](&ev);
3416 /* don't allow to move window origin out of region */
3417 if ( ev.xmotion.x_root < r->g.x ||
3418 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3419 ev.xmotion.y_root < r->g.y ||
3420 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3423 win->g.x = ev.xmotion.x_root - border_width;
3424 win->g.y = ev.xmotion.y_root - border_width;
3426 /* not free, don't sync more than 120 times / second */
3427 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3428 time = ev.xmotion.time;
3429 XSync(display, False);
3434 } while (ev.type != ButtonRelease);
3436 XSync(display, False);
3439 store_float_geom(win,r);
3440 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3441 XUngrabPointer(display, CurrentTime);
3444 drain_enter_notify();
3447 /* user/key callable function IDs */
3505 kf_dumpwins, /* MUST BE LAST */
3509 /* key definitions */
3511 dummykeyfunc(struct swm_region *r, union arg *args)
3516 legacyfunc(struct swm_region *r, union arg *args)
3521 char name[SWM_FUNCNAME_LEN];
3522 void (*func)(struct swm_region *r, union arg *);
3524 } keyfuncs[kf_invalid + 1] = {
3525 /* name function argument */
3526 { "cycle_layout", cycle_layout, {0} },
3527 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
3528 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
3529 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
3530 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
3531 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
3532 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
3533 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
3534 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
3535 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
3536 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
3537 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
3538 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
3539 { "spawn_term", spawnterm, {.argv = spawn_term} },
3540 { "spawn_menu", legacyfunc, {0} },
3541 { "quit", quit, {0} },
3542 { "restart", restart, {0} },
3543 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
3544 { "ws_1", switchws, {.id = 0} },
3545 { "ws_2", switchws, {.id = 1} },
3546 { "ws_3", switchws, {.id = 2} },
3547 { "ws_4", switchws, {.id = 3} },
3548 { "ws_5", switchws, {.id = 4} },
3549 { "ws_6", switchws, {.id = 5} },
3550 { "ws_7", switchws, {.id = 6} },
3551 { "ws_8", switchws, {.id = 7} },
3552 { "ws_9", switchws, {.id = 8} },
3553 { "ws_10", switchws, {.id = 9} },
3554 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
3555 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
3556 { "ws_prior", priorws, {0} },
3557 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
3558 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
3559 { "mvws_1", send_to_ws, {.id = 0} },
3560 { "mvws_2", send_to_ws, {.id = 1} },
3561 { "mvws_3", send_to_ws, {.id = 2} },
3562 { "mvws_4", send_to_ws, {.id = 3} },
3563 { "mvws_5", send_to_ws, {.id = 4} },
3564 { "mvws_6", send_to_ws, {.id = 5} },
3565 { "mvws_7", send_to_ws, {.id = 6} },
3566 { "mvws_8", send_to_ws, {.id = 7} },
3567 { "mvws_9", send_to_ws, {.id = 8} },
3568 { "mvws_10", send_to_ws, {.id = 9} },
3569 { "bar_toggle", bar_toggle, {0} },
3570 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
3571 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
3572 { "screenshot_all", legacyfunc, {0} },
3573 { "screenshot_wind", legacyfunc, {0} },
3574 { "float_toggle", floating_toggle,{0} },
3575 { "version", version, {0} },
3576 { "spawn_lock", legacyfunc, {0} },
3577 { "spawn_initscr", legacyfunc, {0} },
3578 { "spawn_custom", dummykeyfunc, {0} },
3579 { "iconify", iconify, {0} },
3580 { "uniconify", uniconify, {0} },
3581 { "raise_toggle", raise_toggle, {0} },
3582 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
3583 { "invalid key func", NULL, {0} },
3588 enum keyfuncid funcid;
3591 int keys_size = 0, keys_length = 0;
3592 struct key *keys = NULL;
3595 enum { client_click, root_click };
3597 unsigned int action;
3599 unsigned int button;
3600 void (*func)(struct ws_win *, union arg *);
3603 /* action key mouse button func args */
3604 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
3605 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
3606 { client_click, MODKEY, Button1, move, {0} },
3610 update_modkey(unsigned int mod)
3615 for (i = 0; i < keys_length; i++)
3616 if (keys[i].mod & ShiftMask)
3617 keys[i].mod = mod | ShiftMask;
3621 for (i = 0; i < LENGTH(buttons); i++)
3622 if (buttons[i].mask & ShiftMask)
3623 buttons[i].mask = mod | ShiftMask;
3625 buttons[i].mask = mod;
3635 int spawns_size = 0, spawns_length = 0;
3636 struct spawn_prog *spawns = NULL;
3639 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
3642 struct spawn_prog *prog = NULL;
3644 char *ap, **real_args;
3646 DNPRINTF(SWM_D_SPAWN, "spawn_expand %s\n", spawn_name);
3649 for (i = 0; i < spawns_length; i++) {
3650 if (!strcasecmp(spawn_name, spawns[i].name))
3654 fprintf(stderr, "spawn_custom: program %s not found\n",
3659 /* make room for expanded args */
3660 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
3661 err(1, "spawn_custom: calloc real_args");
3663 /* expand spawn_args into real_args */
3664 for (i = 0; i < prog->argc; i++) {
3666 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
3667 if (!strcasecmp(ap, "$bar_border")) {
3669 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
3671 err(1, "spawn_custom border color");
3672 } else if (!strcasecmp(ap, "$bar_color")) {
3674 strdup(r->s->c[SWM_S_COLOR_BAR].name))
3676 err(1, "spawn_custom bar color");
3677 } else if (!strcasecmp(ap, "$bar_font")) {
3678 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
3680 err(1, "spawn_custom bar fonts");
3681 } else if (!strcasecmp(ap, "$bar_font_color")) {
3683 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
3685 err(1, "spawn_custom color font");
3686 } else if (!strcasecmp(ap, "$color_focus")) {
3688 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
3690 err(1, "spawn_custom color focus");
3691 } else if (!strcasecmp(ap, "$color_unfocus")) {
3693 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
3695 err(1, "spawn_custom color unfocus");
3697 /* no match --> copy as is */
3698 if ((real_args[i] = strdup(ap)) == NULL)
3699 err(1, "spawn_custom strdup(ap)");
3701 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
3706 if ((swm_debug & SWM_D_SPAWN) != 0) {
3707 fprintf(stderr, "spawn_custom: result = ");
3708 for (i = 0; i < prog->argc; i++)
3709 fprintf(stderr, "\"%s\" ", real_args[i]);
3710 fprintf(stderr, "\n");
3713 *ret_args = real_args;
3714 return (prog->argc);
3718 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
3724 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3728 spawn(r->ws->idx, &a, 1);
3730 for (i = 0; i < spawn_argc; i++)
3736 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
3742 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3746 if (pipe(select_list_pipe) == -1)
3747 err(1, "pipe error");
3748 if (pipe(select_resp_pipe) == -1)
3749 err(1, "pipe error");
3751 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
3752 err(1, "could not disable SIGPIPE");
3753 switch (*pid = fork()) {
3755 err(1, "cannot fork");
3758 if (dup2(select_list_pipe[0], 0) == -1)
3760 if (dup2(select_resp_pipe[1], 1) == -1)
3762 close(select_list_pipe[1]);
3763 close(select_resp_pipe[0]);
3764 spawn(r->ws->idx, &a, 0);
3766 default: /* parent */
3767 close(select_list_pipe[0]);
3768 close(select_resp_pipe[1]);
3772 for (i = 0; i < spawn_argc; i++)
3778 setspawn(struct spawn_prog *prog)
3782 if (prog == NULL || prog->name == NULL)
3786 for (i = 0; i < spawns_length; i++) {
3787 if (!strcmp(spawns[i].name, prog->name)) {
3789 if (prog->argv == NULL) {
3791 DNPRINTF(SWM_D_SPAWN,
3792 "setspawn: delete #%d %s\n",
3794 free(spawns[i].name);
3795 for (j = 0; j < spawns[i].argc; j++)
3796 free(spawns[i].argv[j]);
3797 free(spawns[i].argv);
3798 j = spawns_length - 1;
3800 spawns[i] = spawns[j];
3805 DNPRINTF(SWM_D_SPAWN,
3806 "setspawn: replace #%d %s\n",
3808 free(spawns[i].name);
3809 for (j = 0; j < spawns[i].argc; j++)
3810 free(spawns[i].argv[j]);
3811 free(spawns[i].argv);
3814 /* found case handled */
3820 if (prog->argv == NULL) {
3822 "error: setspawn: cannot find program %s", prog->name);
3827 /* not found: add */
3828 if (spawns_size == 0 || spawns == NULL) {
3830 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
3831 spawns = malloc((size_t)spawns_size *
3832 sizeof(struct spawn_prog));
3833 if (spawns == NULL) {
3834 fprintf(stderr, "setspawn: malloc failed\n");
3838 } else if (spawns_length == spawns_size) {
3840 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
3841 spawns = realloc(spawns, (size_t)spawns_size *
3842 sizeof(struct spawn_prog));
3843 if (spawns == NULL) {
3844 fprintf(stderr, "setspawn: realloc failed\n");
3850 if (spawns_length < spawns_size) {
3851 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
3852 spawns_length, prog->name);
3853 i = spawns_length++;
3856 fprintf(stderr, "spawns array problem?\n");
3857 if (spawns == NULL) {
3858 fprintf(stderr, "spawns array is NULL!\n");
3866 setconfspawn(char *selector, char *value, int flags)
3868 struct spawn_prog *prog;
3869 char *vp, *cp, *word;
3871 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
3872 if ((prog = calloc(1, sizeof *prog)) == NULL)
3873 err(1, "setconfspawn: calloc prog");
3874 prog->name = strdup(selector);
3875 if (prog->name == NULL)
3876 err(1, "setconfspawn prog->name");
3877 if ((cp = vp = strdup(value)) == NULL)
3878 err(1, "setconfspawn: strdup(value) ");
3879 while ((word = strsep(&cp, " \t")) != NULL) {
3880 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
3882 cp += (long)strspn(cp, " \t");
3883 if (strlen(word) > 0) {
3885 if ((prog->argv = realloc(prog->argv,
3886 prog->argc * sizeof(char *))) == NULL)
3887 err(1, "setconfspawn: realloc");
3888 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
3889 err(1, "setconfspawn: strdup");
3896 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
3903 setconfspawn("term", "xterm", 0);
3904 setconfspawn("screenshot_all", "screenshot.sh full", 0);
3905 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
3906 setconfspawn("lock", "xlock", 0);
3907 setconfspawn("initscr", "initscreen.sh", 0);
3908 setconfspawn("menu", "dmenu_run"
3911 " -nf $bar_font_color"
3913 " -sf $bar_color", 0);
3914 setconfspawn("uniconify", "dmenu"
3918 " -nf $bar_font_color"
3920 " -sf $bar_color", 0);
3924 #define SWM_MODNAME_SIZE 32
3925 #define SWM_KEY_WS "\n+ \t"
3927 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
3931 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
3932 if (mod == NULL || ks == NULL) {
3933 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
3936 if (keystr == NULL || strlen(keystr) == 0) {
3937 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
3943 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
3944 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
3946 cp += (long)strspn(cp, SWM_KEY_WS);
3947 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
3949 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
3951 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
3953 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
3955 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
3957 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
3959 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
3960 *mod |= ControlMask;
3962 *ks = XStringToKeysym(name);
3963 XConvertCase(*ks, ks, &uks);
3964 if (ks == NoSymbol) {
3966 "parsekeys: invalid key %s\n",
3972 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
3977 strdupsafe(char *str)
3982 return (strdup(str));
3986 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
3990 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
3991 keyfuncs[kfid].name, spawn_name);
3993 for (i = 0; i < keys_length; i++) {
3994 if (keys[i].mod == mod && keys[i].keysym == ks) {
3995 if (kfid == kf_invalid) {
3998 "setkeybinding: delete #%d %s\n",
3999 i, keyfuncs[keys[i].funcid].name);
4000 free(keys[i].spawn_name);
4001 j = keys_length - 1;
4005 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4008 /* found: replace */
4010 "setkeybinding: replace #%d %s %s\n",
4011 i, keyfuncs[keys[i].funcid].name,
4013 free(keys[i].spawn_name);
4015 keys[i].keysym = ks;
4016 keys[i].funcid = kfid;
4017 keys[i].spawn_name = strdupsafe(spawn_name);
4018 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4023 if (kfid == kf_invalid) {
4025 "error: setkeybinding: cannot find mod/key combination");
4026 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4029 /* not found: add */
4030 if (keys_size == 0 || keys == NULL) {
4032 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
4033 keys = malloc((size_t)keys_size * sizeof(struct key));
4035 fprintf(stderr, "malloc failed\n");
4039 } else if (keys_length == keys_size) {
4041 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
4042 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
4044 fprintf(stderr, "realloc failed\n");
4049 if (keys_length < keys_size) {
4051 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
4052 j, keyfuncs[kfid].name, spawn_name);
4054 keys[j].keysym = ks;
4055 keys[j].funcid = kfid;
4056 keys[j].spawn_name = strdupsafe(spawn_name);
4058 fprintf(stderr, "keys array problem?\n");
4060 fprintf(stderr, "keys array problem\n");
4064 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4068 setconfbinding(char *selector, char *value, int flags)
4070 enum keyfuncid kfid;
4074 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
4075 if (selector == NULL) {
4076 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
4077 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4079 setkeybinding(mod, ks, kfid, NULL);
4084 /* search by key function name */
4085 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
4086 if (strncasecmp(selector, keyfuncs[kfid].name,
4087 SWM_FUNCNAME_LEN) == 0) {
4088 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4090 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4091 setkeybinding(mod, ks, kfid, NULL);
4097 /* search by custom spawn name */
4098 for (i = 0; i < spawns_length; i++) {
4099 if (strcasecmp(selector, spawns[i].name) == 0) {
4100 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4102 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4103 setkeybinding(mod, ks, kf_spawn_custom,
4110 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
4117 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
4118 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
4119 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
4120 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
4121 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
4122 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
4123 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
4124 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
4125 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
4126 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
4127 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
4128 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
4129 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
4130 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
4131 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
4132 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
4133 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
4134 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
4135 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
4136 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
4137 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
4138 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
4139 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
4140 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
4141 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
4142 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
4143 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
4144 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
4145 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
4146 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
4147 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
4148 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
4149 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
4150 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
4151 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
4152 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
4153 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
4154 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
4155 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
4156 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
4157 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
4158 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
4159 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
4160 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
4161 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
4162 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
4163 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
4164 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
4165 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
4166 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
4167 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
4168 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
4169 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
4170 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
4171 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
4172 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
4173 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
4175 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
4180 updatenumlockmask(void)
4183 XModifierKeymap *modmap;
4185 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
4187 modmap = XGetModifierMapping(display);
4188 for (i = 0; i < 8; i++)
4189 for (j = 0; j < modmap->max_keypermod; j++)
4190 if (modmap->modifiermap[i * modmap->max_keypermod + j]
4191 == XKeysymToKeycode(display, XK_Num_Lock))
4192 numlockmask = (1 << i);
4194 XFreeModifiermap(modmap);
4200 unsigned int i, j, k;
4202 unsigned int modifiers[] =
4203 { 0, LockMask, numlockmask, numlockmask | LockMask };
4205 DNPRINTF(SWM_D_MISC, "grabkeys\n");
4206 updatenumlockmask();
4208 for (k = 0; k < ScreenCount(display); k++) {
4209 if (TAILQ_EMPTY(&screens[k].rl))
4211 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
4212 for (i = 0; i < keys_length; i++) {
4213 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
4214 for (j = 0; j < LENGTH(modifiers); j++)
4215 XGrabKey(display, code,
4216 keys[i].mod | modifiers[j],
4217 screens[k].root, True,
4218 GrabModeAsync, GrabModeAsync);
4224 grabbuttons(struct ws_win *win, int focused)
4227 unsigned int modifiers[] =
4228 { 0, LockMask, numlockmask, numlockmask|LockMask };
4230 updatenumlockmask();
4231 XUngrabButton(display, AnyButton, AnyModifier, win->id);
4233 for (i = 0; i < LENGTH(buttons); i++)
4234 if (buttons[i].action == client_click)
4235 for (j = 0; j < LENGTH(modifiers); j++)
4236 XGrabButton(display, buttons[i].button,
4237 buttons[i].mask | modifiers[j],
4238 win->id, False, BUTTONMASK,
4239 GrabModeAsync, GrabModeSync, None,
4242 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
4243 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
4246 const char *quirkname[] = {
4247 "NONE", /* config string for "no value" */
4256 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
4257 #define SWM_Q_WS "\n|+ \t"
4259 parsequirks(char *qstr, unsigned long *quirk)
4269 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
4271 cp += (long)strspn(cp, SWM_Q_WS);
4272 for (i = 0; i < LENGTH(quirkname); i++) {
4273 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
4274 DNPRINTF(SWM_D_QUIRK,
4275 "parsequirks: %s\n", name);
4280 *quirk |= 1 << (i-1);
4284 if (i >= LENGTH(quirkname)) {
4285 DNPRINTF(SWM_D_QUIRK,
4286 "parsequirks: invalid quirk [%s]\n", name);
4294 setquirk(const char *class, const char *name, const int quirk)
4299 for (i = 0; i < quirks_length; i++) {
4300 if (!strcmp(quirks[i].class, class) &&
4301 !strcmp(quirks[i].name, name)) {
4304 DNPRINTF(SWM_D_QUIRK,
4305 "setquirk: delete #%d %s:%s\n",
4306 i, quirks[i].class, quirks[i].name);
4307 free(quirks[i].class);
4308 free(quirks[i].name);
4309 j = quirks_length - 1;
4311 quirks[i] = quirks[j];
4315 /* found: replace */
4316 DNPRINTF(SWM_D_QUIRK,
4317 "setquirk: replace #%d %s:%s\n",
4318 i, quirks[i].class, quirks[i].name);
4319 free(quirks[i].class);
4320 free(quirks[i].name);
4321 quirks[i].class = strdup(class);
4322 quirks[i].name = strdup(name);
4323 quirks[i].quirk = quirk;
4330 "error: setquirk: cannot find class/name combination");
4333 /* not found: add */
4334 if (quirks_size == 0 || quirks == NULL) {
4336 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
4337 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
4338 if (quirks == NULL) {
4339 fprintf(stderr, "setquirk: malloc failed\n");
4343 } else if (quirks_length == quirks_size) {
4345 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
4346 quirks = realloc(quirks,
4347 (size_t)quirks_size * sizeof(struct quirk));
4348 if (quirks == NULL) {
4349 fprintf(stderr, "setquirk: realloc failed\n");
4354 if (quirks_length < quirks_size) {
4355 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
4356 j = quirks_length++;
4357 quirks[j].class = strdup(class);
4358 quirks[j].name = strdup(name);
4359 quirks[j].quirk = quirk;
4361 fprintf(stderr, "quirks array problem?\n");
4362 if (quirks == NULL) {
4363 fprintf(stderr, "quirks array problem!\n");
4370 setconfquirk(char *selector, char *value, int flags)
4372 char *cp, *class, *name;
4374 unsigned long quirks;
4375 if (selector == NULL)
4377 if ((cp = strchr(selector, ':')) == NULL)
4382 if ((retval = parsequirks(value, &quirks)) == 0)
4383 setquirk(class, name, quirks);
4390 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
4391 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
4392 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
4393 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
4394 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4395 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
4396 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4397 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4398 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4399 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4400 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
4401 setquirk("pcb", "pcb", SWM_Q_FLOAT);
4402 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
4405 /* conf file stuff */
4406 #define SWM_CONF_FILE "scrotwm.conf"
4408 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH,
4409 SWM_S_STACK_ENABLED, SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT,
4410 SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED,
4411 SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
4412 SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED,
4413 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH,
4414 SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM,
4415 SWM_S_SS_APP, SWM_S_DIALOG_RATIO, SWM_S_BAR_AT_BOTTOM
4419 setconfvalue(char *selector, char *value, int flags)
4422 case SWM_S_BAR_DELAY:
4423 bar_delay = atoi(value);
4425 case SWM_S_BAR_ENABLED:
4426 bar_enabled = atoi(value);
4428 case SWM_S_BAR_BORDER_WIDTH:
4429 bar_border_width = atoi(value);
4431 case SWM_S_BAR_AT_BOTTOM:
4432 bar_at_bottom = atoi(value);
4434 case SWM_S_STACK_ENABLED:
4435 stack_enabled = atoi(value);
4437 case SWM_S_CLOCK_ENABLED:
4438 clock_enabled = atoi(value);
4440 case SWM_S_CLOCK_FORMAT:
4441 #ifndef SWM_DENY_CLOCK_FORMAT
4443 if ((clock_format = strdup(value)) == NULL)
4444 err(1, "setconfvalue: clock_format");
4447 case SWM_S_CYCLE_EMPTY:
4448 cycle_empty = atoi(value);
4450 case SWM_S_CYCLE_VISIBLE:
4451 cycle_visible = atoi(value);
4453 case SWM_S_SS_ENABLED:
4454 ss_enabled = atoi(value);
4456 case SWM_S_TERM_WIDTH:
4457 term_width = atoi(value);
4459 case SWM_S_TITLE_CLASS_ENABLED:
4460 title_class_enabled = atoi(value);
4462 case SWM_S_WINDOW_NAME_ENABLED:
4463 window_name_enabled = atoi(value);
4465 case SWM_S_TITLE_NAME_ENABLED:
4466 title_name_enabled = atoi(value);
4468 case SWM_S_FOCUS_MODE:
4469 if (!strcmp(value, "default"))
4470 focus_mode = SWM_FOCUS_DEFAULT;
4471 else if (!strcmp(value, "follow_cursor"))
4472 focus_mode = SWM_FOCUS_FOLLOW;
4473 else if (!strcmp(value, "synergy"))
4474 focus_mode = SWM_FOCUS_SYNERGY;
4476 err(1, "focus_mode");
4478 case SWM_S_DISABLE_BORDER:
4479 disable_border = atoi(value);
4481 case SWM_S_BORDER_WIDTH:
4482 border_width = atoi(value);
4484 case SWM_S_BAR_FONT:
4486 if ((bar_fonts[0] = strdup(value)) == NULL)
4487 err(1, "setconfvalue: bar_font");
4489 case SWM_S_BAR_ACTION:
4491 if ((bar_argv[0] = strdup(value)) == NULL)
4492 err(1, "setconfvalue: bar_action");
4494 case SWM_S_SPAWN_TERM:
4495 free(spawn_term[0]);
4496 if ((spawn_term[0] = strdup(value)) == NULL)
4497 err(1, "setconfvalue: spawn_term");
4501 case SWM_S_DIALOG_RATIO:
4502 dialog_ratio = atof(value);
4503 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
4513 setconfmodkey(char *selector, char *value, int flags)
4515 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
4516 update_modkey(Mod1Mask);
4517 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
4518 update_modkey(Mod2Mask);
4519 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
4520 update_modkey(Mod3Mask);
4521 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
4522 update_modkey(Mod4Mask);
4529 setconfcolor(char *selector, char *value, int flags)
4531 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
4536 setconfregion(char *selector, char *value, int flags)
4538 custom_region(value);
4543 setautorun(char *selector, char *value, int flags)
4553 if (getenv("SWM_STARTED"))
4557 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
4558 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'\n");
4560 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
4561 errx(1, "autorun: invalid workspace %d\n", ws_id + 1);
4564 * This is a little intricate
4566 * If the pid already exists we simply reuse it because it means it was
4567 * used before AND not claimed by manage_window. We get away with
4568 * altering it in the parent after INSERT because this can not be a race
4571 while ((ap = strsep(&sp, " \t")) != NULL) {
4574 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
4576 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
4577 err(1, "setautorun: realloc");
4578 a.argv[argc - 1] = ap;
4581 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
4582 err(1, "setautorun: realloc");
4583 a.argv[argc] = NULL;
4585 if ((pid = fork()) == 0) {
4586 spawn(ws_id, &a, 1);
4595 p = calloc(1, sizeof *p);
4598 TAILQ_INSERT_TAIL(&pidlist, p, entry);
4608 setlayout(char *selector, char *value, int flags)
4610 int ws_id, st, i, x, mg, ma, si, raise;
4612 struct workspace *ws;
4614 if (getenv("SWM_STARTED"))
4618 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
4619 &ws_id, &mg, &ma, &si, &raise, s) != 6)
4620 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
4621 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
4624 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
4625 errx(1, "layout: invalid workspace %d\n", ws_id + 1);
4627 if (!strcasecmp(s, "vertical"))
4629 else if (!strcasecmp(s, "horizontal"))
4631 else if (!strcasecmp(s, "fullscreen"))
4634 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
4635 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
4638 for (i = 0; i < ScreenCount(display); i++) {
4639 ws = (struct workspace *)&screens[i].ws;
4640 ws[ws_id].cur_layout = &layouts[st];
4642 ws[ws_id].always_raise = raise;
4643 if (st == SWM_MAX_STACK)
4647 for (x = 0; x < abs(mg); x++) {
4648 ws[ws_id].cur_layout->l_config(&ws[ws_id],
4649 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
4650 SWM_ARG_ID_MASTERSHRINK);
4654 for (x = 0; x < abs(ma); x++) {
4655 ws[ws_id].cur_layout->l_config(&ws[ws_id],
4656 ma >= 0 ? SWM_ARG_ID_MASTERADD :
4657 SWM_ARG_ID_MASTERDEL);
4661 for (x = 0; x < abs(si); x++) {
4662 ws[ws_id].cur_layout->l_config(&ws[ws_id],
4663 si >= 0 ? SWM_ARG_ID_STACKINC :
4664 SWM_ARG_ID_STACKDEC);
4672 /* config options */
4673 struct config_option {
4675 int (*func)(char*, char*, int);
4678 struct config_option configopt[] = {
4679 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
4680 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
4681 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
4682 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
4683 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
4684 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
4685 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
4686 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
4687 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
4688 { "bind", setconfbinding, 0 },
4689 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
4690 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
4691 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
4692 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
4693 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
4694 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
4695 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
4696 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
4697 { "modkey", setconfmodkey, 0 },
4698 { "program", setconfspawn, 0 },
4699 { "quirk", setconfquirk, 0 },
4700 { "region", setconfregion, 0 },
4701 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
4702 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
4703 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
4704 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
4705 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
4706 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
4707 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
4708 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
4709 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
4710 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
4711 { "autorun", setautorun, 0 },
4712 { "layout", setlayout, 0 },
4717 conf_load(char *filename)
4720 char *line, *cp, *optsub, *optval;
4721 size_t linelen, lineno = 0;
4722 int wordlen, i, optind;
4723 struct config_option *opt;
4725 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
4727 if (filename == NULL) {
4728 fprintf(stderr, "conf_load: no filename\n");
4731 if ((config = fopen(filename, "r")) == NULL) {
4732 warn("conf_load: fopen");
4736 while (!feof(config)) {
4737 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
4740 err(1, "%s", filename);
4745 cp += strspn(cp, " \t\n"); /* eat whitespace */
4746 if (cp[0] == '\0') {
4751 /* get config option */
4752 wordlen = strcspn(cp, "=[ \t\n");
4754 warnx("%s: line %zd: no option found",
4759 for (i = 0; i < LENGTH(configopt); i++) {
4760 opt = &configopt[i];
4761 if (!strncasecmp(cp, opt->optname, wordlen) &&
4762 strlen(opt->optname) == wordlen) {
4768 warnx("%s: line %zd: unknown option %.*s",
4769 filename, lineno, wordlen, cp);
4773 cp += strspn(cp, " \t\n"); /* eat whitespace */
4774 /* get [selector] if any */
4778 wordlen = strcspn(cp, "]");
4781 warnx("%s: line %zd: syntax error",
4785 asprintf(&optsub, "%.*s", wordlen, cp);
4788 cp += strspn(cp, "] \t\n"); /* eat trailing */
4790 cp += strspn(cp, "= \t\n"); /* eat trailing */
4792 optval = strdup(cp);
4793 /* call function to deal with it all */
4794 if (configopt[optind].func(optsub, optval,
4795 configopt[optind].funcflags) != 0) {
4796 fprintf(stderr, "%s line %zd: %s\n",
4797 filename, lineno, line);
4798 errx(1, "%s: line %zd: invalid data for %s",
4799 filename, lineno, configopt[optind].optname);
4807 DNPRINTF(SWM_D_CONF, "conf_load end\n");
4813 set_child_transient(struct ws_win *win, Window *trans)
4815 struct ws_win *parent, *w;
4816 XWMHints *wmh = NULL;
4817 struct swm_region *r;
4818 struct workspace *ws;
4820 parent = find_window(win->transient);
4822 parent->child_trans = win;
4824 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
4825 " for %lu trans %lu\n", win->id, win->transient);
4827 if (win->hints == NULL) {
4828 fprintf(stderr, "no hints for %lu\n", win->id);
4832 r = root_to_region(win->wa.root);
4834 /* parent doen't exist in our window list */
4835 TAILQ_FOREACH(w, &ws->winlist, entry) {
4839 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
4840 fprintf(stderr, "can't get hints for %lu\n",
4845 if (win->hints->window_group != wmh->window_group)
4848 w->child_trans = win;
4849 win->transient = w->id;
4851 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
4852 "transient to %lu\n", win->transient);
4862 window_get_pid(Window win)
4864 Atom actual_type_return;
4865 int actual_format_return = 0;
4866 unsigned long nitems_return = 0;
4867 unsigned long bytes_after_return = 0;
4871 unsigned char *prop = NULL;
4873 if (XGetWindowProperty(display, win,
4874 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
4875 XA_CARDINAL, &actual_type_return, &actual_format_return,
4876 &nitems_return, &bytes_after_return,
4877 (unsigned char**)(void*)&pid) != Success)
4879 if (actual_type_return != XA_CARDINAL)
4890 if (XGetWindowProperty(display, win,
4891 XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
4892 XA_STRING, &actual_type_return, &actual_format_return,
4893 &nitems_return, &bytes_after_return, &prop) != Success)
4895 if (actual_type_return != XA_STRING)
4900 ret = strtonum(prop, 0, UINT_MAX, &errstr);
4901 /* ignore error because strtonum returns 0 anyway */
4908 manage_window(Window id)
4911 struct workspace *ws;
4912 struct ws_win *win, *ww;
4913 int format, i, ws_idx, n, border_me = 0;
4914 unsigned long nitems, bytes;
4915 Atom ws_idx_atom = 0, type;
4916 Atom *prot = NULL, *pp;
4917 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
4918 struct swm_region *r;
4924 if ((win = find_window(id)) != NULL)
4925 return (win); /* already being managed */
4927 /* see if we are on the unmanaged list */
4928 if ((win = find_unmanaged_window(id)) != NULL) {
4929 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
4931 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
4932 if (win->transient) {
4933 set_child_transient(win, &trans);
4934 } if (trans && (ww = find_window(trans)))
4935 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
4937 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
4938 ewmh_update_actions(win);
4942 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
4943 errx(1, "calloc: failed to allocate memory for new window");
4947 /* see if we need to override the workspace */
4948 p = find_pid(window_get_pid(id));
4950 /* Get all the window data in one shot */
4951 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4953 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
4954 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
4956 XGetWindowAttributes(display, id, &win->wa);
4957 XGetWMNormalHints(display, id, &win->sh, &mask);
4958 win->hints = XGetWMHints(display, id);
4959 XGetTransientForHint(display, id, &trans);
4961 win->transient = trans;
4962 set_child_transient(win, &trans);
4963 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
4964 win->id, win->transient);
4967 /* get supported protocols */
4968 if (XGetWMProtocols(display, id, &prot, &n)) {
4969 for (i = 0, pp = prot; i < n; i++, pp++) {
4970 if (*pp == takefocus)
4971 win->take_focus = 1;
4973 win->can_delete = 1;
4979 win->iconic = get_iconic(win);
4982 * Figure out where to put the window. If it was previously assigned to
4983 * a workspace (either by spawn() or manually moving), and isn't
4984 * transient, * put it in the same workspace
4986 r = root_to_region(win->wa.root);
4988 ws = &r->s->ws[p->ws];
4989 TAILQ_REMOVE(&pidlist, p, entry);
4992 } else if (prop && win->transient == 0) {
4993 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
4994 ws_idx = strtonum(prop, 0, 9, &errstr);
4996 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
4999 ws = &r->s->ws[ws_idx];
5002 /* this should launch transients in the same ws as parent */
5004 if ((ww = find_window(trans)) != NULL)
5011 "fix this bug mcbride\n");
5016 /* set up the window layout */
5019 win->s = r->s; /* this never changes */
5020 if (trans && (ww = find_window(trans)))
5021 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
5023 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
5025 win->g.w = win->wa.width;
5026 win->g.h = win->wa.height;
5027 win->g.x = win->wa.x;
5028 win->g.y = win->wa.y;
5029 win->g_floatvalid = 0;
5030 win->floatmaxed = 0;
5031 win->ewmh_flags = 0;
5033 /* Set window properties so we can remember this after reincarnation */
5034 if (ws_idx_atom && prop == NULL &&
5035 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
5036 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
5038 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
5039 PropModeReplace, ws_idx_str, SWM_PROPLEN);
5044 ewmh_autoquirk(win);
5046 if (XGetClassHint(display, win->id, &win->ch)) {
5047 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
5048 win->ch.res_class, win->ch.res_name);
5050 /* java is retarded so treat it special */
5051 if (strstr(win->ch.res_name, "sun-awt")) {
5056 for (i = 0; i < quirks_length; i++){
5057 if (!strcmp(win->ch.res_class, quirks[i].class) &&
5058 !strcmp(win->ch.res_name, quirks[i].name)) {
5059 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
5060 win->ch.res_class, win->ch.res_name);
5061 if (quirks[i].quirk & SWM_Q_FLOAT) {
5065 win->quirks = quirks[i].quirk;
5070 /* alter window position if quirky */
5071 if (win->quirks & SWM_Q_ANYWHERE) {
5072 win->manual = 1; /* don't center the quirky windows */
5073 bzero(&wc, sizeof wc);
5075 if (bar_enabled && win->g.y < bar_height) {
5076 win->g.y = wc.y = bar_height;
5079 if (win->g.w + win->g.x > WIDTH(r)) {
5080 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
5086 /* Reset font sizes (the bruteforce way; no default keybinding). */
5087 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
5088 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5089 fake_keypress(win, XK_KP_Subtract, ShiftMask);
5090 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5091 fake_keypress(win, XK_KP_Add, ShiftMask);
5094 ewmh_get_win_state(win);
5095 ewmh_update_actions(win);
5096 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
5100 bzero(&wc, sizeof wc);
5101 wc.border_width = border_width;
5102 mask = CWBorderWidth;
5103 XConfigureWindow(display, win->id, mask, &wc);
5106 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
5107 PropertyChangeMask | StructureNotifyMask);
5109 /* floaters need to be mapped if they are in the current workspace */
5110 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
5111 XMapRaised(display, win->id);
5117 free_window(struct ws_win *win)
5119 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
5124 /* needed for restart wm */
5125 set_win_state(win, WithdrawnState);
5127 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5129 if (win->ch.res_class)
5130 XFree(win->ch.res_class);
5131 if (win->ch.res_name)
5132 XFree(win->ch.res_name);
5137 memset(win, 0xff, sizeof *win); /* XXX kill later */
5143 unmanage_window(struct ws_win *win)
5145 struct ws_win *parent;
5150 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
5152 if (win->transient) {
5153 parent = find_window(win->transient);
5155 parent->child_trans = NULL;
5158 /* focus on root just in case */
5159 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
5163 TAILQ_REMOVE(&win->ws->winlist, win, entry);
5164 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
5170 focus_magic(struct ws_win *win)
5172 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu\n", WINID(win));
5177 if (win->child_trans) {
5178 /* win = parent & has a transient so focus on that */
5180 focus_win(win->child_trans);
5181 if (win->child_trans->take_focus)
5182 client_msg(win, takefocus);
5184 /* make sure transient hasn't dissapeared */
5185 if (validate_win(win->child_trans) == 0) {
5186 focus_win(win->child_trans);
5187 if (win->child_trans->take_focus)
5188 client_msg(win->child_trans, takefocus);
5190 win->child_trans = NULL;
5192 if (win->take_focus)
5193 client_msg(win, takefocus);
5199 if (win->take_focus)
5200 client_msg(win, takefocus);
5207 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
5215 XKeyEvent *ev = &e->xkey;
5217 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
5219 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
5220 for (i = 0; i < keys_length; i++)
5221 if (keysym == keys[i].keysym
5222 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
5223 && keyfuncs[keys[i].funcid].func) {
5224 if (keys[i].funcid == kf_spawn_custom)
5226 root_to_region(ev->root),
5227 &(keyfuncs[keys[i].funcid].args),
5231 keyfuncs[keys[i].funcid].func(
5232 root_to_region(ev->root),
5233 &(keyfuncs[keys[i].funcid].args)
5239 buttonpress(XEvent *e)
5243 XButtonPressedEvent *ev = &e->xbutton;
5245 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
5247 action = root_click;
5248 if ((win = find_window(ev->window)) == NULL)
5252 action = client_click;
5254 for (i = 0; i < LENGTH(buttons); i++)
5255 if (action == buttons[i].action && buttons[i].func &&
5256 buttons[i].button == ev->button &&
5257 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
5258 buttons[i].func(win, &buttons[i].args);
5262 configurerequest(XEvent *e)
5264 XConfigureRequestEvent *ev = &e->xconfigurerequest;
5269 if ((win = find_window(ev->window)) == NULL)
5270 if ((win = find_unmanaged_window(ev->window)) == NULL)
5274 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
5276 bzero(&wc, sizeof wc);
5279 wc.width = ev->width;
5280 wc.height = ev->height;
5281 wc.border_width = ev->border_width;
5282 wc.sibling = ev->above;
5283 wc.stack_mode = ev->detail;
5284 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
5286 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
5288 config_win(win, ev);
5293 configurenotify(XEvent *e)
5298 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
5299 e->xconfigure.window);
5301 win = find_window(e->xconfigure.window);
5303 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5307 if (focus_mode == SWM_FOCUS_DEFAULT)
5308 drain_enter_notify();
5313 destroynotify(XEvent *e)
5316 XDestroyWindowEvent *ev = &e->xdestroywindow;
5318 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
5320 if ((win = find_window(ev->window)) == NULL) {
5321 if ((win = find_unmanaged_window(ev->window)) == NULL)
5327 /* make sure we focus on something */
5330 unmanage_window(win);
5332 if (focus_mode == SWM_FOCUS_DEFAULT)
5333 drain_enter_notify();
5338 enternotify(XEvent *e)
5340 XCrossingEvent *ev = &e->xcrossing;
5345 Window focus_return;
5346 int revert_to_return;
5348 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
5349 "%lu subwindow %lu same_screen %d focus %d state %d\n",
5350 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
5351 ev->same_screen, ev->focus, ev->state);
5353 switch (focus_mode) {
5354 case SWM_FOCUS_DEFAULT:
5356 case SWM_FOCUS_FOLLOW:
5358 case SWM_FOCUS_SYNERGY:
5361 * all these checks need to be in this order because the
5362 * XCheckTypedWindowEvent relies on weeding out the previous events
5364 * making this code an option would enable a follow mouse for focus
5369 * state is set when we are switching workspaces and focus is set when
5370 * the window or a subwindow already has focus (occurs during restart).
5372 * Only honor the focus flag if last_focus_event is not FocusOut,
5373 * this allows scrotwm to continue to control focus when another
5374 * program is also playing with it.
5376 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
5377 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
5382 * happens when a window is created or destroyed and the border
5383 * crosses the mouse pointer and when switching ws
5385 * we need the subwindow test to see if we came from root in order
5386 * to give focus to floaters
5388 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
5389 ev->subwindow == 0) {
5390 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
5394 /* this window already has focus */
5395 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
5396 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
5400 /* this window is being deleted or moved to another ws */
5401 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
5403 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
5404 XPutBackEvent(display, &cne);
5408 if ((win = find_window(ev->window)) == NULL) {
5409 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5414 * In fullstack kill all enters unless they come from a different ws
5415 * (i.e. another region) or focus has been grabbed externally.
5417 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
5418 last_focus_event != FocusOut) {
5419 XGetInputFocus(display, &focus_return, &revert_to_return);
5420 if ((w = find_window(focus_return)) == NULL ||
5422 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
5430 if ((win = find_window(ev->window)) == NULL) {
5431 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5436 * if we have more enternotifies let them handle it in due time
5438 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
5439 DNPRINTF(SWM_D_EVENT,
5440 "ignoring enternotify: got more enternotify\n");
5441 XPutBackEvent(display, &cne);
5448 /* lets us use one switch statement for arbitrary mode/detail combinations */
5449 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
5452 focusevent(XEvent *e)
5456 u_int32_t mode_detail;
5457 XFocusChangeEvent *ev = &e->xfocus;
5459 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
5460 ev->type == FocusIn ? "entering" : "leaving",
5461 ev->window, ev->mode, ev->detail);
5463 if (last_focus_event == ev->type) {
5464 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
5468 last_focus_event = ev->type;
5469 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
5471 switch (mode_detail) {
5472 /* synergy client focus operations */
5473 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
5474 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
5476 /* synergy server focus operations */
5477 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
5479 /* Entering applications like rdesktop that mangle the pointer */
5480 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
5482 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
5483 XSetWindowBorder(display, win->id,
5484 win->ws->r->s->c[ev->type == FocusIn ?
5485 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
5488 fprintf(stderr, "ignoring focusevent\n");
5489 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
5496 mapnotify(XEvent *e)
5499 XMapEvent *ev = &e->xmap;
5501 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
5503 win = manage_window(ev->window);
5505 set_win_state(win, NormalState);
5509 mappingnotify(XEvent *e)
5511 XMappingEvent *ev = &e->xmapping;
5513 XRefreshKeyboardMapping(ev);
5514 if (ev->request == MappingKeyboard)
5519 maprequest(XEvent *e)
5522 struct swm_region *r;
5523 XWindowAttributes wa;
5524 XMapRequestEvent *ev = &e->xmaprequest;
5526 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
5527 e->xmaprequest.window);
5529 if (!XGetWindowAttributes(display, ev->window, &wa))
5531 if (wa.override_redirect)
5534 win = manage_window(e->xmaprequest.window);
5536 return; /* can't happen */
5540 /* make new win focused */
5541 r = root_to_region(win->wa.root);
5542 if (win->ws == r->ws)
5547 propertynotify(XEvent *e)
5550 XPropertyEvent *ev = &e->xproperty;
5552 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
5555 win = find_window(ev->window);
5559 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
5560 update_iconic(win, 0);
5561 XMapRaised(display, win->id);
5568 case XA_WM_NORMAL_HINTS:
5571 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5572 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
5573 if (win->sh.flags & PMinSize) {
5574 win->g.w = win->sh.min_width;
5575 win->g.h = win->sh.min_height;
5576 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
5578 XMoveResizeWindow(display, win->id,
5579 win->g.x, win->g.y, win->g.w, win->g.h);
5581 if (window_name_enabled)
5590 unmapnotify(XEvent *e)
5594 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
5596 /* determine if we need to help unmanage this window */
5597 win = find_window(e->xunmap.window);
5601 if (getstate(e->xunmap.window) == NormalState) {
5602 unmanage_window(win);
5605 /* giant hack for apps that don't destroy transient windows */
5606 /* eat a bunch of events to prevent remanaging the window */
5608 while (XCheckWindowEvent(display, e->xunmap.window,
5609 EnterWindowMask, &cne))
5611 while (XCheckWindowEvent(display, e->xunmap.window,
5612 StructureNotifyMask, &cne))
5614 while (XCheckWindowEvent(display, e->xunmap.window,
5615 SubstructureNotifyMask, &cne))
5617 /* resend unmap because we ated it */
5618 XUnmapWindow(display, e->xunmap.window);
5621 if (focus_mode == SWM_FOCUS_DEFAULT)
5622 drain_enter_notify();
5626 visibilitynotify(XEvent *e)
5629 struct swm_region *r;
5631 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
5632 e->xvisibility.window);
5633 if (e->xvisibility.state == VisibilityUnobscured)
5634 for (i = 0; i < ScreenCount(display); i++)
5635 TAILQ_FOREACH(r, &screens[i].rl, entry)
5636 if (e->xvisibility.window == r->bar_window)
5641 clientmessage(XEvent *e)
5643 XClientMessageEvent *ev;
5648 win = find_window(ev->window);
5652 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx type: %ld \n",
5653 ev->window, ev->message_type);
5655 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
5656 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW \n");
5659 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
5660 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW \n");
5661 if (win->can_delete)
5662 client_msg(win, adelete);
5664 XKillClient(display, win->id);
5666 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
5667 DNPRINTF(SWM_D_EVENT,
5668 "clientmessage: _NET_MOVERESIZE_WINDOW \n");
5669 if (win->floating) {
5670 if (ev->data.l[0] & (1<<8)) /* x */
5671 win->g.x = ev->data.l[1];
5672 if (ev->data.l[0] & (1<<9)) /* y */
5673 win->g.y = ev->data.l[2];
5674 if (ev->data.l[0] & (1<<10)) /* width */
5675 win->g.w = ev->data.l[3];
5676 if (ev->data.l[0] & (1<<11)) /* height */
5677 win->g.h = ev->data.l[4];
5680 /* TODO: Change stack sizes */
5682 config_win(win, NULL);
5684 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
5685 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE \n");
5686 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
5688 ewmh_update_win_state(win, ev->data.l[2],
5696 xerror_start(Display *d, XErrorEvent *ee)
5703 xerror(Display *d, XErrorEvent *ee)
5705 /* fprintf(stderr, "error: %p %p\n", display, ee); */
5713 xerrorxlib = XSetErrorHandler(xerror_start);
5715 /* this causes an error if some other window manager is running */
5716 XSelectInput(display, DefaultRootWindow(display),
5717 SubstructureRedirectMask);
5718 XSync(display, False);
5722 XSetErrorHandler(xerror);
5723 XSync(display, False);
5728 new_region(struct swm_screen *s, int x, int y, int w, int h)
5730 struct swm_region *r, *n;
5731 struct workspace *ws = NULL;
5734 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
5735 s->idx, w, h, x, y);
5737 /* remove any conflicting regions */
5738 n = TAILQ_FIRST(&s->rl);
5741 n = TAILQ_NEXT(r, entry);
5742 if (X(r) < (x + w) &&
5743 (X(r) + WIDTH(r)) > x &&
5745 (Y(r) + HEIGHT(r)) > y) {
5746 if (r->ws->r != NULL)
5747 r->ws->old_r = r->ws->r;
5749 XDestroyWindow(display, r->bar_window);
5750 TAILQ_REMOVE(&s->rl, r, entry);
5751 TAILQ_INSERT_TAIL(&s->orl, r, entry);
5755 /* search old regions for one to reuse */
5757 /* size + location match */
5758 TAILQ_FOREACH(r, &s->orl, entry)
5759 if (X(r) == x && Y(r) == y &&
5760 HEIGHT(r) == h && WIDTH(r) == w)
5764 TAILQ_FOREACH(r, &s->orl, entry)
5765 if (HEIGHT(r) == h && WIDTH(r) == w)
5769 TAILQ_REMOVE(&s->orl, r, entry);
5770 /* try to use old region's workspace */
5771 if (r->ws->r == NULL)
5774 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
5775 errx(1, "calloc: failed to allocate memory for screen");
5777 /* if we don't have a workspace already, find one */
5779 for (i = 0; i < SWM_WS_MAX; i++)
5780 if (s->ws[i].r == NULL) {
5787 errx(1, "no free workspaces\n");
5798 TAILQ_INSERT_TAIL(&s->rl, r, entry);
5804 #ifdef SWM_XRR_HAS_CRTC
5806 XRRScreenResources *sr;
5809 #endif /* SWM_XRR_HAS_CRTC */
5810 struct swm_region *r;
5813 if (i >= ScreenCount(display))
5814 errx(1, "invalid screen");
5816 /* remove any old regions */
5817 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
5818 r->ws->old_r = r->ws->r = NULL;
5819 XDestroyWindow(display, r->bar_window);
5820 TAILQ_REMOVE(&screens[i].rl, r, entry);
5821 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
5825 /* map virtual screens onto physical screens */
5826 #ifdef SWM_XRR_HAS_CRTC
5827 if (xrandr_support) {
5828 sr = XRRGetScreenResources(display, screens[i].root);
5830 new_region(&screens[i], 0, 0,
5831 DisplayWidth(display, i),
5832 DisplayHeight(display, i));
5836 for (c = 0, ci = NULL; c < ncrtc; c++) {
5837 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
5838 if (ci->noutput == 0)
5841 if (ci != NULL && ci->mode == None)
5842 new_region(&screens[i], 0, 0,
5843 DisplayWidth(display, i),
5844 DisplayHeight(display, i));
5846 new_region(&screens[i],
5847 ci->x, ci->y, ci->width, ci->height);
5850 XRRFreeCrtcInfo(ci);
5851 XRRFreeScreenResources(sr);
5853 #endif /* SWM_XRR_HAS_CRTC */
5855 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
5856 DisplayHeight(display, i));
5861 screenchange(XEvent *e) {
5862 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
5863 struct swm_region *r;
5866 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
5868 if (!XRRUpdateConfiguration(e))
5871 /* silly event doesn't include the screen index */
5872 for (i = 0; i < ScreenCount(display); i++)
5873 if (screens[i].root == xe->root)
5875 if (i >= ScreenCount(display))
5876 errx(1, "screenchange: screen not found\n");
5878 /* brute force for now, just re-enumerate the regions */
5881 /* add bars to all regions */
5882 for (i = 0; i < ScreenCount(display); i++)
5883 TAILQ_FOREACH(r, &screens[i].rl, entry)
5886 if (focus_mode == SWM_FOCUS_DEFAULT)
5887 drain_enter_notify();
5893 Window d1, d2, *wins = NULL;
5894 XWindowAttributes wa;
5899 for (i = 0; i < ScreenCount(display); i++) {
5900 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
5903 /* attach windows to a region */
5904 /* normal windows */
5905 for (j = 0; j < no; j++) {
5906 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5907 wa.override_redirect ||
5908 XGetTransientForHint(display, wins[j], &d1))
5911 state = getstate(wins[j]);
5912 manage = state == IconicState;
5913 if (wa.map_state == IsViewable || manage)
5914 manage_window(wins[j]);
5916 /* transient windows */
5917 for (j = 0; j < no; j++) {
5918 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5919 wa.override_redirect)
5922 state = getstate(wins[j]);
5923 manage = state == IconicState;
5924 if (XGetTransientForHint(display, wins[j], &d1) &&
5926 manage_window(wins[j]);
5939 int errorbase, major, minor;
5940 struct workspace *ws;
5942 if ((screens = calloc(ScreenCount(display),
5943 sizeof(struct swm_screen))) == NULL)
5944 errx(1, "calloc: screens");
5946 /* initial Xrandr setup */
5947 xrandr_support = XRRQueryExtension(display,
5948 &xrandr_eventbase, &errorbase);
5950 if (XRRQueryVersion(display, &major, &minor) && major < 1)
5953 /* map physical screens */
5954 for (i = 0; i < ScreenCount(display); i++) {
5955 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
5957 TAILQ_INIT(&screens[i].rl);
5958 TAILQ_INIT(&screens[i].orl);
5959 screens[i].root = RootWindow(display, i);
5961 /* set default colors */
5962 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
5963 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
5964 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
5965 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
5966 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
5968 /* set default cursor */
5969 XDefineCursor(display, screens[i].root,
5970 XCreateFontCursor(display, XC_left_ptr));
5972 /* init all workspaces */
5973 /* XXX these should be dynamically allocated too */
5974 for (j = 0; j < SWM_WS_MAX; j++) {
5975 ws = &screens[i].ws[j];
5980 TAILQ_INIT(&ws->winlist);
5981 TAILQ_INIT(&ws->unmanagedlist);
5983 for (k = 0; layouts[k].l_stack != NULL; k++)
5984 if (layouts[k].l_config != NULL)
5985 layouts[k].l_config(ws,
5986 SWM_ARG_ID_STACKINIT);
5987 ws->cur_layout = &layouts[0];
5993 XRRSelectInput(display, screens[i].root,
5994 RRScreenChangeNotifyMask);
6001 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
6003 err(1, "setup_globals: strdup");
6004 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
6006 err(1, "setup_globals: strdup");
6007 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
6009 err(1, "setup_globals: strdup");
6010 if ((spawn_term[0] = strdup("xterm")) == NULL)
6011 err(1, "setup_globals: strdup");
6012 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
6020 Atom netwmcheck, netwmname, utf8_string;
6023 /* work around sun jdk bugs, code from wmname */
6024 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
6025 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
6026 utf8_string = XInternAtom(display, "UTF8_STRING", False);
6027 for (i = 0; i < ScreenCount(display); i++) {
6028 root = screens[i].root;
6029 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
6030 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
6031 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
6033 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
6034 PropModeReplace, (unsigned char *)&win,1);
6035 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
6036 PropModeReplace, (unsigned char *)&win,1);
6037 XChangeProperty(display, win, netwmname, utf8_string, 8,
6038 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
6043 main(int argc, char *argv[])
6046 struct swm_region *r, *rr;
6047 struct ws_win *winfocus = NULL;
6050 char conf[PATH_MAX], *cfile = NULL;
6055 struct sigaction sact;
6058 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
6059 SWM_VERSION, cvstag);
6060 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
6061 warnx("no locale support");
6063 if (!(display = XOpenDisplay(0)))
6064 errx(1, "can not open display");
6067 errx(1, "other wm running");
6069 /* handle some signals */
6070 bzero(&sact, sizeof(sact));
6071 sigemptyset(&sact.sa_mask);
6073 sact.sa_handler = sighdlr;
6074 sigaction(SIGINT, &sact, NULL);
6075 sigaction(SIGQUIT, &sact, NULL);
6076 sigaction(SIGTERM, &sact, NULL);
6077 sigaction(SIGHUP, &sact, NULL);
6079 sact.sa_handler = sighdlr;
6080 sact.sa_flags = SA_NOCLDSTOP;
6081 sigaction(SIGCHLD, &sact, NULL);
6083 astate = XInternAtom(display, "WM_STATE", False);
6084 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
6085 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
6086 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
6087 a_wmname = XInternAtom(display, "WM_NAME", False);
6088 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
6089 a_string = XInternAtom(display, "STRING", False);
6090 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
6092 /* look for local and global conf file */
6093 pwd = getpwuid(getuid());
6095 errx(1, "invalid user %d", getuid());
6104 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
6105 if (stat(conf, &sb) != -1) {
6106 if (S_ISREG(sb.st_mode))
6109 /* try global conf file */
6110 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
6111 if (!stat(conf, &sb))
6112 if (S_ISREG(sb.st_mode))
6119 /* set some values to work around bad programs */
6122 /* grab existing windows (before we build the bars) */
6125 if (getenv("SWM_STARTED") == NULL)
6126 setenv("SWM_STARTED", "YES", 1);
6128 /* setup all bars */
6129 for (i = 0; i < ScreenCount(display); i++)
6130 TAILQ_FOREACH(r, &screens[i].rl, entry) {
6131 if (winfocus == NULL)
6132 winfocus = TAILQ_FIRST(&r->ws->winlist);
6140 if (focus_mode == SWM_FOCUS_DEFAULT)
6141 drain_enter_notify();
6143 xfd = ConnectionNumber(display);
6145 while (XPending(display)) {
6146 XNextEvent(display, &e);
6149 if (e.type < LASTEvent) {
6151 if (handler[e.type])
6152 handler[e.type](&e);
6154 DNPRINTF(SWM_D_EVENT,
6155 "win: %lu unknown event: %d\n",
6156 e.xany.window, e.type);
6158 switch (e.type - xrandr_eventbase) {
6159 case RRScreenChangeNotify:
6163 DNPRINTF(SWM_D_EVENT,
6164 "win: %lu unknown xrandr event: "
6165 "%d\n", e.xany.window, e.type);
6171 /* if we are being restarted go focus on first window */
6173 rr = winfocus->ws->r;
6175 /* not a visible window */
6179 /* move pointer to first screen if multi screen */
6180 if (ScreenCount(display) > 1 || outputs > 1)
6181 XWarpPointer(display, None, rr->s[0].root,
6182 0, 0, 0, 0, rr->g.x,
6183 rr->g.y + (bar_enabled ? bar_height : 0));
6185 a.id = SWM_ARG_ID_FOCUSCUR;
6195 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
6197 DNPRINTF(SWM_D_MISC, "select failed");
6198 if (restart_wm == 1)
6199 restart(NULL, NULL);
6200 if (search_resp == 1)
6212 XFreeGC(display, bar_gc);
6213 XCloseDisplay(display);