3 * Copyright (c) 2009-2010-2011 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009-2010-2011 Ryan McBride <mcbride@countersiege.com>
5 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
6 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Much code and ideas taken from dwm under the following license:
23 * MIT/X Consortium License
25 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
26 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
27 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
28 * 2007 Premysl Hruby <dfenze at gmail dot com>
29 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
30 * 2007 Christof Musik <christof at sendfax dot de>
31 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
32 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
33 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
35 * Permission is hereby granted, free of charge, to any person obtaining a
36 * copy of this software and associated documentation files (the "Software"),
37 * to deal in the Software without restriction, including without limitation
38 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
39 * and/or sell copies of the Software, and to permit persons to whom the
40 * Software is furnished to do so, subject to the following conditions:
42 * The above copyright notice and this permission notice shall be included in
43 * all copies or substantial portions of the Software.
45 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
46 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
47 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
48 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
49 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
50 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
51 * DEALINGS IN THE SOFTWARE.
54 static const char *cvstag =
57 #define SWM_VERSION "0.9.32"
74 #include <sys/types.h>
78 #include <sys/queue.h>
79 #include <sys/param.h>
80 #include <sys/select.h>
82 #include <X11/cursorfont.h>
83 #include <X11/keysym.h>
84 #include <X11/Xatom.h>
86 #include <X11/Xproto.h>
87 #include <X11/Xutil.h>
88 #include <X11/extensions/Xrandr.h>
95 # error XRandR versions less than 1.0 are not supported
100 #define SWM_XRR_HAS_CRTC
104 /*#define SWM_DEBUG*/
106 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
107 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
108 #define SWM_D_MISC 0x0001
109 #define SWM_D_EVENT 0x0002
110 #define SWM_D_WS 0x0004
111 #define SWM_D_FOCUS 0x0008
112 #define SWM_D_MOVE 0x0010
113 #define SWM_D_STACK 0x0020
114 #define SWM_D_MOUSE 0x0040
115 #define SWM_D_PROP 0x0080
116 #define SWM_D_CLASS 0x0100
117 #define SWM_D_KEY 0x0200
118 #define SWM_D_QUIRK 0x0400
119 #define SWM_D_SPAWN 0x0800
120 #define SWM_D_EVENTQ 0x1000
121 #define SWM_D_CONF 0x2000
123 u_int32_t swm_debug = 0
140 #define DPRINTF(x...)
141 #define DNPRINTF(n,x...)
144 #define LENGTH(x) (sizeof x / sizeof x[0])
145 #define MODKEY Mod1Mask
146 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
147 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
148 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
149 #define SWM_PROPLEN (16)
150 #define SWM_FUNCNAME_LEN (32)
151 #define SWM_KEYS_LEN (255)
152 #define SWM_QUIRK_LEN (64)
153 #define X(r) (r)->g.x
154 #define Y(r) (r)->g.y
155 #define WIDTH(r) (r)->g.w
156 #define HEIGHT(r) (r)->g.h
157 #define SWM_MAX_FONT_STEPS (3)
158 #define WINID(w) (w ? w->id : 0)
160 #define SWM_FOCUS_DEFAULT (0)
161 #define SWM_FOCUS_SYNERGY (1)
162 #define SWM_FOCUS_FOLLOW (2)
165 #define SWM_LIB "/usr/local/lib/libswmhack.so"
177 volatile sig_atomic_t running = 1;
178 volatile sig_atomic_t restart_wm = 0;
180 int last_focus_event = FocusOut;
181 int (*xerrorxlib)(Display *, XErrorEvent *);
185 int xrandr_eventbase;
186 unsigned int numlockmask = 0;
190 int cycle_visible = 0;
192 int font_adjusted = 0;
193 unsigned int mod_key = MODKEY;
196 struct swm_region *search_r;
197 int select_list_pipe[2];
198 int select_resp_pipe[2];
200 volatile sig_atomic_t search_resp;
203 double dialog_ratio = .6;
205 #define SWM_BAR_MAX (256)
206 char *bar_argv[] = { NULL, NULL };
208 char bar_ext[SWM_BAR_MAX];
209 char bar_vertext[SWM_BAR_MAX];
211 sig_atomic_t bar_alarm = 0;
214 int bar_border_width = 1;
215 int bar_at_bottom = 0;
217 int bar_extra_running = 0;
220 int stack_enabled = 1;
221 int clock_enabled = 1;
222 char *clock_format = NULL;
223 int title_name_enabled = 0;
224 int title_class_enabled = 0;
225 int window_name_enabled = 0;
226 int focus_mode = SWM_FOCUS_DEFAULT;
227 int disable_border = 0;
228 int border_width = 1;
234 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
235 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
237 #define SWM_MENU_FN (2)
238 #define SWM_MENU_NB (4)
239 #define SWM_MENU_NF (6)
240 #define SWM_MENU_SB (8)
241 #define SWM_MENU_SF (10)
243 /* layout manager data */
244 struct swm_geometry {
254 /* virtual "screens" */
256 TAILQ_ENTRY(swm_region) entry;
257 struct swm_geometry g;
258 struct workspace *ws; /* current workspace on this region */
259 struct workspace *ws_prior; /* prior workspace on this region */
260 struct swm_screen *s; /* screen idx */
263 TAILQ_HEAD(swm_region_list, swm_region);
266 TAILQ_ENTRY(ws_win) entry;
269 struct ws_win *child_trans; /* transient child window */
270 struct swm_geometry g; /* current geometry */
271 struct swm_geometry g_float; /* geometry when floating */
272 struct swm_geometry rg_float; /* region geom when floating */
273 int g_floatvalid; /* flag: geometry in g_float is valid */
274 int floatmaxed; /* flag: floater was maxed in max_stack */
278 unsigned int ewmh_flags;
279 int font_size_boundary[SWM_MAX_FONT_STEPS];
285 unsigned long quirks;
286 struct workspace *ws; /* always valid */
287 struct swm_screen *s; /* always valid, never changes */
288 XWindowAttributes wa;
293 TAILQ_HEAD(ws_win_list, ws_win);
297 TAILQ_ENTRY(pid_e) entry;
301 TAILQ_HEAD(pid_list, pid_e);
302 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
304 /* layout handlers */
306 void vertical_config(struct workspace *, int);
307 void vertical_stack(struct workspace *, struct swm_geometry *);
308 void horizontal_config(struct workspace *, int);
309 void horizontal_stack(struct workspace *, struct swm_geometry *);
310 void max_stack(struct workspace *, struct swm_geometry *);
312 struct ws_win *find_window(Window);
314 void grabbuttons(struct ws_win *, int);
315 void new_region(struct swm_screen *, int, int, int, int);
316 void unmanage_window(struct ws_win *);
317 long getstate(Window);
320 void (*l_stack)(struct workspace *, struct swm_geometry *);
321 void (*l_config)(struct workspace *, int);
323 #define SWM_L_FOCUSPREV (1<<0)
324 #define SWM_L_MAPONFOCUS (1<<1)
327 /* stack, configure */
328 { vertical_stack, vertical_config, 0, "[|]" },
329 { horizontal_stack, horizontal_config, 0, "[-]" },
331 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, "[ ]" },
332 { NULL, NULL, 0, NULL },
335 /* position of max_stack mode in the layouts array, index into layouts! */
336 #define SWM_V_STACK (0)
337 #define SWM_H_STACK (1)
338 #define SWM_MAX_STACK (2)
340 #define SWM_H_SLICE (32)
341 #define SWM_V_SLICE (32)
343 /* define work spaces */
345 int idx; /* workspace index */
346 int always_raise; /* raise windows on focus */
347 struct layout *cur_layout; /* current layout handlers */
348 struct ws_win *focus; /* may be NULL */
349 struct ws_win *focus_prev; /* may be NULL */
350 struct swm_region *r; /* may be NULL */
351 struct swm_region *old_r; /* may be NULL */
352 struct ws_win_list winlist; /* list of windows in ws */
353 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
357 int horizontal_msize;
359 int horizontal_stacks;
366 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
367 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
369 /* physical screen mapping */
370 #define SWM_WS_MAX (10)
372 int idx; /* screen index */
373 struct swm_region_list rl; /* list of regions on this screen */
374 struct swm_region_list orl; /* list of old regions */
376 struct workspace ws[SWM_WS_MAX];
382 } c[SWM_S_COLOR_MAX];
384 struct swm_screen *screens;
387 /* args to functions */
390 #define SWM_ARG_ID_FOCUSNEXT (0)
391 #define SWM_ARG_ID_FOCUSPREV (1)
392 #define SWM_ARG_ID_FOCUSMAIN (2)
393 #define SWM_ARG_ID_FOCUSCUR (4)
394 #define SWM_ARG_ID_SWAPNEXT (10)
395 #define SWM_ARG_ID_SWAPPREV (11)
396 #define SWM_ARG_ID_SWAPMAIN (12)
397 #define SWM_ARG_ID_MOVELAST (13)
398 #define SWM_ARG_ID_MASTERSHRINK (20)
399 #define SWM_ARG_ID_MASTERGROW (21)
400 #define SWM_ARG_ID_MASTERADD (22)
401 #define SWM_ARG_ID_MASTERDEL (23)
402 #define SWM_ARG_ID_STACKRESET (30)
403 #define SWM_ARG_ID_STACKINIT (31)
404 #define SWM_ARG_ID_CYCLEWS_UP (40)
405 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
406 #define SWM_ARG_ID_CYCLESC_UP (42)
407 #define SWM_ARG_ID_CYCLESC_DOWN (43)
408 #define SWM_ARG_ID_STACKINC (50)
409 #define SWM_ARG_ID_STACKDEC (51)
410 #define SWM_ARG_ID_SS_ALL (60)
411 #define SWM_ARG_ID_SS_WINDOW (61)
412 #define SWM_ARG_ID_DONTCENTER (70)
413 #define SWM_ARG_ID_CENTER (71)
414 #define SWM_ARG_ID_KILLWINDOW (80)
415 #define SWM_ARG_ID_DELETEWINDOW (81)
419 void focus(struct swm_region *, union arg *);
420 void focus_magic(struct ws_win *);
427 #define SWM_Q_FLOAT (1<<0) /* float this window */
428 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
429 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
430 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
431 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
432 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
434 int quirks_size = 0, quirks_length = 0;
435 struct quirk *quirks = NULL;
438 * Supported EWMH hints should be added to
439 * both the enum and the ewmh array
441 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
442 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
443 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
444 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
445 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
446 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
447 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
448 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
449 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
450 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
456 } ewmh[SWM_EWMH_HINT_MAX] = {
457 /* must be in same order as in the enum */
458 {"_NET_ACTIVE_WINDOW", None},
459 {"_NET_MOVERESIZE_WINDOW", None},
460 {"_NET_CLOSE_WINDOW", None},
461 {"_NET_WM_WINDOW_TYPE", None},
462 {"_NET_WM_WINDOW_TYPE_DOCK", None},
463 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
464 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
465 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
466 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
467 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
468 {"_NET_WM_STATE", None},
469 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
470 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
471 {"_NET_WM_STATE_SKIP_TASKBAR", None},
472 {"_NET_WM_STATE_SKIP_PAGER", None},
473 {"_NET_WM_STATE_HIDDEN", None},
474 {"_NET_WM_STATE_ABOVE", None},
475 {"_SWM_WM_STATE_MANUAL", None},
476 {"_NET_WM_STATE_FULLSCREEN", None},
477 {"_NET_WM_ALLOWED_ACTIONS", None},
478 {"_NET_WM_ACTION_MOVE", None},
479 {"_NET_WM_ACTION_RESIZE", None},
480 {"_NET_WM_ACTION_FULLSCREEN", None},
481 {"_NET_WM_ACTION_CLOSE", None},
484 void store_float_geom(struct ws_win *win, struct swm_region *r);
485 int floating_toggle_win(struct ws_win *win);
486 void spawn_select(struct swm_region *, union arg *, char *, int *);
489 get_property(Window id, Atom atom, long count, Atom type,
490 unsigned long *n, unsigned char **data)
493 unsigned long tmp, extra;
494 unsigned long *nitems;
497 nitems = n != NULL ? n : &tmp;
498 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
499 &real, &format, nitems, &extra, data);
501 if (status != Success)
510 update_iconic(struct ws_win *win, int newv)
517 iprop = XInternAtom(display, "_SWM_ICONIC", False);
521 XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
522 PropModeReplace, (unsigned char *)&v, 1);
524 XDeleteProperty(display, win->id, iprop);
528 get_iconic(struct ws_win *win)
533 unsigned long nitems, extra;
534 unsigned char *prop = NULL;
536 iprop = XInternAtom(display, "_SWM_ICONIC", False);
539 status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
540 False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
541 if (status != Success)
543 if (rettype != XA_INTEGER || retfmt != 32)
547 v = *((int32_t *)prop);
561 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
563 for (i = 0; i < LENGTH(ewmh); i++)
564 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
566 for (i = 0; i < ScreenCount(display); i++) {
567 /* Support check window will be created by workaround(). */
569 /* Report supported atoms */
570 XDeleteProperty(display, screens[i].root, sup_list);
571 for (j = 0; j < LENGTH(ewmh); j++)
572 XChangeProperty(display, screens[i].root,
573 sup_list, XA_ATOM, 32,
574 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
582 unsigned char *data = NULL;
584 Atom sup_check, sup_list;
587 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
588 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
590 for (i = 0; i < ScreenCount(display); i++) {
591 /* Get the support check window and destroy it */
592 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
597 XDestroyWindow(display, id);
598 XDeleteProperty(display, screens[i].root, sup_check);
599 XDeleteProperty(display, screens[i].root, sup_list);
607 ewmh_autoquirk(struct ws_win *win)
610 unsigned long *data = NULL;
614 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
615 XA_ATOM, &n, (unsigned char **)&data);
622 for (i = 0; i < n; i++) {
624 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
626 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
627 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
628 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
630 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
633 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
634 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
636 win->quirks = SWM_Q_FLOAT;
644 #define SWM_EWMH_ACTION_COUNT_MAX (6)
645 #define EWMH_F_FULLSCREEN (1<<0)
646 #define EWMH_F_ABOVE (1<<1)
647 #define EWMH_F_HIDDEN (1<<2)
648 #define EWMH_F_SKIP_PAGER (1<<3)
649 #define EWMH_F_SKIP_TASKBAR (1<<4)
650 #define SWM_F_MANUAL (1<<5)
653 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
655 struct swm_geometry rg;
663 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
669 store_float_geom(win, win->ws->r);
676 if (win->g_floatvalid) {
677 /* refloat at last floating relative position */
678 win->g.x = win->g_float.x - win->rg_float.x + rg.x;
679 win->g.y = win->g_float.y - win->rg_float.y + rg.y;
680 win->g.w = win->g_float.w;
681 win->g.h = win->g_float.h;
689 ewmh_update_actions(struct ws_win *win)
691 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
697 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
700 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
701 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
704 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
705 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
708 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
709 #define _NET_WM_STATE_ADD 1 /* add/set property */
710 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
713 ewmh_update_win_state(struct ws_win *win, long state, long action)
715 unsigned int mask = 0;
716 unsigned int changed = 0;
717 unsigned int orig_flags;
722 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
723 mask = EWMH_F_FULLSCREEN;
724 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
726 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
728 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
729 mask = EWMH_F_SKIP_PAGER;
730 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
731 mask = EWMH_F_SKIP_TASKBAR;
734 orig_flags = win->ewmh_flags;
737 case _NET_WM_STATE_REMOVE:
738 win->ewmh_flags &= ~mask;
740 case _NET_WM_STATE_ADD:
741 win->ewmh_flags |= mask;
743 case _NET_WM_STATE_TOGGLE:
744 win->ewmh_flags ^= mask;
748 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
750 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
752 if (!floating_toggle_win(win))
753 win->ewmh_flags = orig_flags; /* revert */
754 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
756 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
757 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
759 if (!ewmh_set_win_fullscreen(win,
760 win->ewmh_flags & EWMH_F_FULLSCREEN))
761 win->ewmh_flags = orig_flags; /* revert */
763 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
765 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
766 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
767 XA_ATOM, 32, PropModeAppend,
768 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
769 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
770 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
771 XA_ATOM, 32, PropModeAppend,
772 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
773 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
774 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
775 XA_ATOM, 32, PropModeAppend,
776 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
777 if (win->ewmh_flags & EWMH_F_ABOVE)
778 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
779 XA_ATOM, 32, PropModeAppend,
780 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
781 if (win->ewmh_flags & SWM_F_MANUAL)
782 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
783 XA_ATOM, 32, PropModeAppend,
784 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
788 ewmh_get_win_state(struct ws_win *win)
799 win->ewmh_flags |= EWMH_F_ABOVE;
801 win->ewmh_flags |= SWM_F_MANUAL;
803 success = get_property(win->id, ewmh[_NET_WM_STATE].atom,
804 (~0L), XA_ATOM, &n, (unsigned char **)&states);
809 for (i = 0; i < n; i++)
810 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
830 name = "ButtonPress";
833 name = "ButtonRelease";
836 name = "MotionNotify";
839 name = "EnterNotify";
842 name = "LeaveNotify";
851 name = "KeymapNotify";
857 name = "GraphicsExpose";
862 case VisibilityNotify:
863 name = "VisibilityNotify";
866 name = "CreateNotify";
869 name = "DestroyNotify";
872 name = "UnmapNotify";
881 name = "ReparentNotify";
883 case ConfigureNotify:
884 name = "ConfigureNotify";
886 case ConfigureRequest:
887 name = "ConfigureRequest";
890 name = "GravityNotify";
893 name = "ResizeRequest";
895 case CirculateNotify:
896 name = "CirculateNotify";
898 case CirculateRequest:
899 name = "CirculateRequest";
902 name = "PropertyNotify";
905 name = "SelectionClear";
907 case SelectionRequest:
908 name = "SelectionRequest";
910 case SelectionNotify:
911 name = "SelectionNotify";
914 name = "ColormapNotify";
917 name = "ClientMessage";
920 name = "MappingNotify";
925 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
927 e->xany.window, name, e->type, QLength(display));
929 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
931 e->xany.window, e->type, QLength(display));
935 dumpwins(struct swm_region *r, union arg *args)
939 XWindowAttributes wa;
942 fprintf(stderr, "invalid workspace\n");
946 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
948 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
949 state = getstate(win->id);
950 if (!XGetWindowAttributes(display, win->id, &wa))
951 fprintf(stderr, "window: %lu failed "
952 "XGetWindowAttributes\n", win->id);
953 fprintf(stderr, "window: %lu map_state: %d state: %d "
955 win->id, wa.map_state, state, win->transient);
958 fprintf(stderr, "===== unmanaged window list =====\n");
959 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
960 state = getstate(win->id);
961 if (!XGetWindowAttributes(display, win->id, &wa))
962 fprintf(stderr, "window: %lu failed "
963 "XGetWindowAttributes\n", win->id);
964 fprintf(stderr, "window: %lu map_state: %d state: %d "
966 win->id, wa.map_state, state, win->transient);
969 fprintf(stderr, "=================================\n");
974 dumpwins(struct swm_region *r, union arg *args)
977 #endif /* SWM_DEBUG */
979 void expose(XEvent *);
980 void keypress(XEvent *);
981 void buttonpress(XEvent *);
982 void configurerequest(XEvent *);
983 void configurenotify(XEvent *);
984 void destroynotify(XEvent *);
985 void enternotify(XEvent *);
986 void focusevent(XEvent *);
987 void mapnotify(XEvent *);
988 void mappingnotify(XEvent *);
989 void maprequest(XEvent *);
990 void propertynotify(XEvent *);
991 void unmapnotify(XEvent *);
992 void visibilitynotify(XEvent *);
993 void clientmessage(XEvent *);
995 void (*handler[LASTEvent])(XEvent *) = {
997 [KeyPress] = keypress,
998 [ButtonPress] = buttonpress,
999 [ConfigureRequest] = configurerequest,
1000 [ConfigureNotify] = configurenotify,
1001 [DestroyNotify] = destroynotify,
1002 [EnterNotify] = enternotify,
1003 [FocusIn] = focusevent,
1004 [FocusOut] = focusevent,
1005 [MapNotify] = mapnotify,
1006 [MappingNotify] = mappingnotify,
1007 [MapRequest] = maprequest,
1008 [PropertyNotify] = propertynotify,
1009 [UnmapNotify] = unmapnotify,
1010 [VisibilityNotify] = visibilitynotify,
1011 [ClientMessage] = clientmessage,
1017 int saved_errno, status;
1020 saved_errno = errno;
1024 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1029 if (errno != ECHILD)
1030 warn("sighdlr: waitpid");
1031 #endif /* SWM_DEBUG */
1034 if (pid == searchpid)
1038 if (WIFEXITED(status)) {
1039 if (WEXITSTATUS(status) != 0)
1040 warnx("sighdlr: child exit status: %d",
1041 WEXITSTATUS(status));
1043 warnx("sighdlr: child is terminated "
1045 #endif /* SWM_DEBUG */
1059 errno = saved_errno;
1065 struct pid_e *p = NULL;
1067 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1072 TAILQ_FOREACH(p, &pidlist, entry) {
1081 name_to_color(char *colorname)
1085 XColor screen_def, exact_def;
1086 unsigned long result = 0;
1087 char cname[32] = "#";
1089 cmap = DefaultColormap(display, screens[0].idx);
1090 status = XAllocNamedColor(display, cmap, colorname,
1091 &screen_def, &exact_def);
1093 strlcat(cname, colorname + 2, sizeof cname - 1);
1094 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1098 result = screen_def.pixel;
1100 fprintf(stderr, "color '%s' not found.\n", colorname);
1106 setscreencolor(char *val, int i, int c)
1108 if (i > 0 && i <= ScreenCount(display)) {
1109 screens[i - 1].c[c].color = name_to_color(val);
1110 free(screens[i - 1].c[c].name);
1111 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1113 } else if (i == -1) {
1114 for (i = 0; i < ScreenCount(display); i++) {
1115 screens[i].c[c].color = name_to_color(val);
1116 free(screens[i].c[c].name);
1117 if ((screens[i].c[c].name = strdup(val)) == NULL)
1121 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1122 i, ScreenCount(display));
1126 custom_region(char *val)
1128 unsigned int sidx, x, y, w, h;
1130 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1131 errx(1, "invalid custom region, "
1132 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
1133 if (sidx < 1 || sidx > ScreenCount(display))
1134 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1135 sidx, ScreenCount(display));
1139 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
1141 if (x < 0 || x > DisplayWidth(display, sidx) ||
1142 y < 0 || y > DisplayHeight(display, sidx) ||
1143 w + x > DisplayWidth(display, sidx) ||
1144 h + y > DisplayHeight(display, sidx)) {
1145 fprintf(stderr, "ignoring region %ux%u+%u+%u "
1146 "- not within screen boundaries "
1147 "(%ux%u)\n", w, h, x, y,
1148 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1152 new_region(&screens[sidx], x, y, w, h);
1156 socket_setnonblock(int fd)
1160 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1161 err(1, "fcntl F_GETFL");
1162 flags |= O_NONBLOCK;
1163 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1164 err(1, "fcntl F_SETFL");
1168 bar_print(struct swm_region *r, char *s)
1170 XClearWindow(display, r->bar_window);
1171 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
1172 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
1177 bar_extra_stop(void)
1181 bzero(bar_pipe, sizeof bar_pipe);
1184 kill(bar_pid, SIGTERM);
1187 strlcpy(bar_ext, "", sizeof bar_ext);
1192 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1194 int do_class, do_name;
1196 XClassHint *xch = NULL;
1198 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
1199 cur_focus != NULL) {
1200 if ((xch = XAllocClassHint()) == NULL)
1202 status = XGetClassHint(display, cur_focus->id, xch);
1203 if (status == BadWindow || status == BadAlloc)
1205 do_class = (title_class_enabled && xch->res_class != NULL);
1206 do_name = (title_name_enabled && xch->res_name != NULL);
1208 strlcat(s, xch->res_class, sz);
1209 if (do_class && do_name)
1210 strlcat(s, ":", sz);
1212 strlcat(s, xch->res_name, sz);
1213 strlcat(s, " ", sz);
1221 bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1225 if (window_name_enabled && cur_focus != NULL) {
1226 XFetchName(display, cur_focus->id, &title);
1228 if (cur_focus->floating)
1229 strlcat(s, "(f) ", sz);
1230 strlcat(s, title, sz);
1231 strlcat(s, " ", sz);
1242 struct swm_region *r;
1245 char s[SWM_BAR_MAX];
1246 char cn[SWM_BAR_MAX];
1247 char loc[SWM_BAR_MAX];
1251 if (bar_enabled == 0)
1253 if (bar_extra && bar_extra_running) {
1254 /* ignore short reads; it'll correct itself */
1255 while ((b = fgetln(stdin, &len)) != NULL)
1256 if (b && b[len - 1] == '\n') {
1258 strlcpy(bar_ext, b, sizeof bar_ext);
1260 if (b == NULL && errno != EAGAIN) {
1261 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
1262 errno, strerror(errno));
1266 strlcpy(bar_ext, "", sizeof bar_ext);
1268 if (clock_enabled == 0)
1269 strlcpy(s, "", sizeof s);
1272 localtime_r(&tmt, &tm);
1273 strftime(s, sizeof s, clock_format, &tm);
1274 strlcat(s, " ", sizeof s);
1277 for (i = 0; i < ScreenCount(display); i++) {
1279 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1280 strlcpy(cn, "", sizeof cn);
1282 bar_class_name(cn, sizeof cn, r->ws->focus);
1283 bar_window_name(cn, sizeof cn, r->ws->focus);
1287 stack = r->ws->cur_layout->name;
1289 snprintf(loc, sizeof loc, "%d:%d %s %s%s %s %s",
1290 x++, r->ws->idx + 1, stack, s, cn, bar_ext,
1305 bar_toggle(struct swm_region *r, union arg *args)
1307 struct swm_region *tmpr;
1308 int i, sc = ScreenCount(display);
1310 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
1313 for (i = 0; i < sc; i++)
1314 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1315 XUnmapWindow(display, tmpr->bar_window);
1317 for (i = 0; i < sc; i++)
1318 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1319 XMapRaised(display, tmpr->bar_window);
1321 bar_enabled = !bar_enabled;
1324 /* must be after stack */
1331 XSetWindowAttributes wa;
1332 struct swm_region *r;
1335 /* do this here because the conf file is in memory */
1336 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1337 /* launch external status app */
1338 bar_extra_running = 1;
1339 if (pipe(bar_pipe) == -1)
1340 err(1, "pipe error");
1341 socket_setnonblock(bar_pipe[0]);
1342 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1343 if (dup2(bar_pipe[0], 0) == -1)
1345 if (dup2(bar_pipe[1], 1) == -1)
1347 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1348 err(1, "could not disable SIGPIPE");
1349 switch (bar_pid = fork()) {
1351 err(1, "cannot fork");
1355 execvp(bar_argv[0], bar_argv);
1356 err(1, "%s external app failed", bar_argv[0]);
1358 default: /* parent */
1364 bzero(&wa, sizeof wa);
1365 for (i = 0; i < ScreenCount(display); i++)
1366 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1368 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1369 wa.background_pixel =
1370 screens[i].c[SWM_S_COLOR_BAR].color;
1371 XChangeWindowAttributes(display, r->bar_window,
1372 CWBackPixel | CWBorderPixel, &wa);
1378 bar_setup(struct swm_region *r)
1383 XFreeFont(display, bar_fs);
1387 for (i = 0; bar_fonts[i] != NULL; i++) {
1388 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
1394 if (bar_fonts[i] == NULL)
1395 errx(1, "couldn't load font");
1397 errx(1, "couldn't create font structure");
1399 bar_height = bar_fs->ascent + bar_fs->descent + 1 +
1400 2 * bar_border_width;
1402 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1404 r->bar_window = XCreateSimpleWindow(display,
1405 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width,
1406 bar_height - 2 * bar_border_width,
1407 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1408 r->s->c[SWM_S_COLOR_BAR].color);
1409 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
1410 XSetFont(display, bar_gc, bar_fs->fid);
1411 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1413 XMapRaised(display, r->bar_window);
1414 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
1416 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1417 err(1, "could not install bar_signal");
1422 drain_enter_notify(void)
1427 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1430 DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained %d\n", i);
1434 set_win_state(struct ws_win *win, long state)
1436 long data[] = {state, None};
1438 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1443 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1444 (unsigned char *)data, 2);
1451 unsigned char *p = NULL;
1454 if (!get_property(w, astate, 2L, astate, &n, &p))
1457 result = *((long *)p);
1463 version(struct swm_region *r, union arg *args)
1465 bar_version = !bar_version;
1467 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
1468 SWM_VERSION, cvstag);
1470 strlcpy(bar_vertext, "", sizeof bar_vertext);
1475 client_msg(struct ws_win *win, Atom a)
1477 XClientMessageEvent cm;
1482 bzero(&cm, sizeof cm);
1483 cm.type = ClientMessage;
1484 cm.window = win->id;
1485 cm.message_type = aprot;
1488 cm.data.l[1] = CurrentTime;
1489 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1493 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1501 DNPRINTF(SWM_D_MISC,
1502 "config_win: win %lu x %d y %d w %d h %d\n",
1503 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1505 ce.type = ConfigureNotify;
1506 ce.display = display;
1508 ce.window = win->id;
1511 ce.width = win->g.w;
1512 ce.height = win->g.h;
1513 ce.border_width = border_width;
1515 ce.override_redirect = False;
1517 DNPRINTF(SWM_D_MISC,
1518 "config_win: ev win %lu x %d y %d w %d h %d\n",
1519 ev->window, ev->x, ev->y, ev->width, ev->height);
1520 ce.type = ConfigureNotify;
1521 ce.display = ev->display;
1522 ce.event = ev->window;
1523 ce.window = ev->window;
1526 ce.width = ev->width;
1527 ce.height = ev->height;
1528 ce.border_width = ev->border_width;
1529 ce.above = ev->above;
1530 ce.override_redirect = False;
1533 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1537 count_win(struct workspace *ws, int count_transient)
1542 TAILQ_FOREACH(win, &ws->winlist, entry) {
1543 if (count_transient == 0 && win->floating)
1545 if (count_transient == 0 && win->transient)
1551 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1557 quit(struct swm_region *r, union arg *args)
1559 DNPRINTF(SWM_D_MISC, "quit\n");
1564 unmap_window(struct ws_win *win)
1569 /* don't unmap again */
1570 if (getstate(win->id) == IconicState)
1573 set_win_state(win, IconicState);
1575 XUnmapWindow(display, win->id);
1576 XSetWindowBorder(display, win->id,
1577 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1586 for (i = 0; i < ScreenCount(display); i++)
1587 for (j = 0; j < SWM_WS_MAX; j++)
1588 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1593 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1600 event.display = display; /* Ignored, but what the hell */
1601 event.window = win->id;
1602 event.root = win->s->root;
1603 event.subwindow = None;
1604 event.time = CurrentTime;
1609 event.same_screen = True;
1610 event.keycode = XKeysymToKeycode(display, keysym);
1611 event.state = modifiers;
1613 event.type = KeyPress;
1614 XSendEvent(event.display, event.window, True,
1615 KeyPressMask, (XEvent *)&event);
1617 event.type = KeyRelease;
1618 XSendEvent(event.display, event.window, True,
1619 KeyPressMask, (XEvent *)&event);
1624 restart(struct swm_region *r, union arg *args)
1626 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1628 /* disable alarm because the following code may not be interrupted */
1630 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1631 errx(1, "can't disable alarm");
1636 XCloseDisplay(display);
1637 execvp(start_argv[0], start_argv);
1638 fprintf(stderr, "execvp failed\n");
1644 root_to_region(Window root)
1646 struct swm_region *r = NULL;
1648 int i, x, y, wx, wy;
1651 for (i = 0; i < ScreenCount(display); i++)
1652 if (screens[i].root == root)
1655 if (XQueryPointer(display, screens[i].root,
1656 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1657 /* choose a region based on pointer location */
1658 TAILQ_FOREACH(r, &screens[i].rl, entry)
1659 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1660 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1665 r = TAILQ_FIRST(&screens[i].rl);
1671 find_unmanaged_window(Window id)
1676 for (i = 0; i < ScreenCount(display); i++)
1677 for (j = 0; j < SWM_WS_MAX; j++)
1678 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1686 find_window(Window id)
1689 Window wrr, wpr, *wcr = NULL;
1693 for (i = 0; i < ScreenCount(display); i++)
1694 for (j = 0; j < SWM_WS_MAX; j++)
1695 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1699 /* if we were looking for the parent return that window instead */
1700 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1705 /* ignore not found and root */
1706 if (wpr == 0 || wrr == wpr)
1709 /* look for parent */
1710 for (i = 0; i < ScreenCount(display); i++)
1711 for (j = 0; j < SWM_WS_MAX; j++)
1712 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1720 spawn(int ws_idx, union arg *args, int close_fd)
1725 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1728 close(ConnectionNumber(display));
1730 setenv("LD_PRELOAD", SWM_LIB, 1);
1732 if (asprintf(&ret, "%d", ws_idx) == -1) {
1736 setenv("_SWM_WS", ret, 1);
1740 if (asprintf(&ret, "%d", getpid()) == -1) {
1744 setenv("_SWM_PID", ret, 1);
1748 if (setsid() == -1) {
1755 * close stdin and stdout to prevent interaction between apps
1756 * and the baraction script
1757 * leave stderr open to record errors
1759 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
1763 dup2(fd, STDIN_FILENO);
1764 dup2(fd, STDOUT_FILENO);
1769 execvp(args->argv[0], args->argv);
1776 spawnterm(struct swm_region *r, union arg *args)
1778 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1782 setenv("_SWM_XTERM_FONTADJ", "", 1);
1783 spawn(r->ws->idx, args, 1);
1788 kill_refs(struct ws_win *win)
1791 struct swm_region *r;
1792 struct workspace *ws;
1797 for (i = 0; i < ScreenCount(display); i++)
1798 TAILQ_FOREACH(r, &screens[i].rl, entry)
1799 for (x = 0; x < SWM_WS_MAX; x++) {
1801 if (win == ws->focus)
1803 if (win == ws->focus_prev)
1804 ws->focus_prev = NULL;
1809 validate_win(struct ws_win *testwin)
1812 struct workspace *ws;
1813 struct swm_region *r;
1814 int i, x, foundit = 0;
1816 if (testwin == NULL)
1819 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1820 TAILQ_FOREACH(r, &screens[i].rl, entry)
1821 for (x = 0; x < SWM_WS_MAX; x++) {
1823 TAILQ_FOREACH(win, &ws->winlist, entry)
1831 validate_ws(struct workspace *testws)
1833 struct swm_region *r;
1834 struct workspace *ws;
1837 /* validate all ws */
1838 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1839 TAILQ_FOREACH(r, &screens[i].rl, entry)
1840 for (x = 0; x < SWM_WS_MAX; x++) {
1849 unfocus_win(struct ws_win *win)
1854 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1858 if (win->ws == NULL)
1861 if (validate_ws(win->ws))
1862 return; /* XXX this gets hit with thunderbird, needs fixing */
1864 if (win->ws->r == NULL)
1867 if (validate_win(win)) {
1872 if (win->ws->focus == win) {
1873 win->ws->focus = NULL;
1874 win->ws->focus_prev = win;
1877 if (validate_win(win->ws->focus)) {
1878 kill_refs(win->ws->focus);
1879 win->ws->focus = NULL;
1881 if (validate_win(win->ws->focus_prev)) {
1882 kill_refs(win->ws->focus_prev);
1883 win->ws->focus_prev = NULL;
1886 /* drain all previous unfocus events */
1887 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
1890 grabbuttons(win, 0);
1891 XSetWindowBorder(display, win->id,
1892 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1894 XChangeProperty(display, win->s->root,
1895 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1896 PropModeReplace, (unsigned char *)&none,1);
1905 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
1907 for (i = 0; i < ScreenCount(display); i++)
1908 for (j = 0; j < SWM_WS_MAX; j++)
1909 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1914 focus_win(struct ws_win *win)
1919 struct ws_win *cfw = NULL;
1922 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1926 if (win->ws == NULL)
1929 if (validate_ws(win->ws))
1930 return; /* XXX this gets hit with thunderbird, needs fixing */
1932 if (validate_win(win)) {
1937 if (validate_win(win)) {
1942 XGetInputFocus(display, &cur_focus, &rr);
1943 if ((cfw = find_window(cur_focus)) != NULL)
1946 /* use larger hammer since the window was killed somehow */
1947 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
1948 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
1949 XSetWindowBorder(display, cfw->id,
1950 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1953 win->ws->focus = win;
1955 if (win->ws->r != NULL) {
1956 /* drain all previous focus events */
1957 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
1961 XSetInputFocus(display, win->id,
1962 RevertToParent, CurrentTime);
1963 grabbuttons(win, 1);
1964 XSetWindowBorder(display, win->id,
1965 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1966 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
1967 win->ws->always_raise)
1968 XMapRaised(display, win->id);
1970 XChangeProperty(display, win->s->root,
1971 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1972 PropModeReplace, (unsigned char *)&win->id,1);
1975 if (window_name_enabled)
1980 switchws(struct swm_region *r, union arg *args)
1982 int wsid = args->id, unmap_old = 0;
1983 struct swm_region *this_r, *other_r;
1985 struct workspace *new_ws, *old_ws;
1992 old_ws = this_r->ws;
1993 new_ws = &this_r->s->ws[wsid];
1995 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1996 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1999 if (new_ws == NULL || old_ws == NULL)
2001 if (new_ws == old_ws)
2004 other_r = new_ws->r;
2005 if (other_r == NULL) {
2006 /* the other workspace is hidden, hide this one */
2010 /* the other ws is visible in another region, exchange them */
2011 other_r->ws_prior = new_ws;
2012 other_r->ws = old_ws;
2013 old_ws->r = other_r;
2015 this_r->ws_prior = old_ws;
2016 this_r->ws = new_ws;
2019 /* this is needed so that we can click on a window after a restart */
2023 a.id = SWM_ARG_ID_FOCUSCUR;
2024 focus(new_ws->r, &a);
2028 /* unmap old windows */
2030 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2033 if (focus_mode == SWM_FOCUS_DEFAULT)
2034 drain_enter_notify();
2038 cyclews(struct swm_region *r, union arg *args)
2041 struct swm_screen *s = r->s;
2043 DNPRINTF(SWM_D_WS, "cyclews id %d "
2044 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2045 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2050 case SWM_ARG_ID_CYCLEWS_UP:
2051 if (a.id < SWM_WS_MAX - 1)
2056 case SWM_ARG_ID_CYCLEWS_DOWN:
2060 a.id = SWM_WS_MAX - 1;
2066 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
2068 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2072 } while (a.id != r->ws->idx);
2076 priorws(struct swm_region *r, union arg *args)
2080 DNPRINTF(SWM_D_WS, "priorws id %d "
2081 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2082 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2084 if (r->ws_prior == NULL)
2087 a.id = r->ws_prior->idx;
2092 cyclescr(struct swm_region *r, union arg *args)
2094 struct swm_region *rr = NULL;
2098 /* do nothing if we don't have more than one screen */
2099 if (!(ScreenCount(display) > 1 || outputs > 1))
2104 case SWM_ARG_ID_CYCLESC_UP:
2105 rr = TAILQ_NEXT(r, entry);
2107 rr = TAILQ_FIRST(&screens[i].rl);
2109 case SWM_ARG_ID_CYCLESC_DOWN:
2110 rr = TAILQ_PREV(r, swm_region_list, entry);
2112 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2120 /* move mouse to region */
2122 y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
2123 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2125 a.id = SWM_ARG_ID_FOCUSCUR;
2128 if (rr->ws->focus) {
2129 /* move to focus window */
2130 x = rr->ws->focus->g.x + 1;
2131 y = rr->ws->focus->g.y + 1;
2132 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2137 sort_windows(struct ws_win_list *wl)
2139 struct ws_win *win, *parent, *nxt;
2144 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2145 nxt = TAILQ_NEXT(win, entry);
2146 if (win->transient) {
2147 parent = find_window(win->transient);
2148 if (parent == NULL) {
2149 fprintf(stderr, "not possible bug\n");
2152 TAILQ_REMOVE(wl, win, entry);
2153 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2160 swapwin(struct swm_region *r, union arg *args)
2162 struct ws_win *target, *source;
2163 struct ws_win *cur_focus;
2164 struct ws_win_list *wl;
2167 DNPRINTF(SWM_D_WS, "swapwin id %d "
2168 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
2169 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2171 cur_focus = r->ws->focus;
2172 if (cur_focus == NULL)
2176 wl = &source->ws->winlist;
2179 case SWM_ARG_ID_SWAPPREV:
2180 if (source->transient)
2181 source = find_window(source->transient);
2182 target = TAILQ_PREV(source, ws_win_list, entry);
2183 if (target && target->transient)
2184 target = find_window(target->transient);
2185 TAILQ_REMOVE(wl, source, entry);
2187 TAILQ_INSERT_TAIL(wl, source, entry);
2189 TAILQ_INSERT_BEFORE(target, source, entry);
2191 case SWM_ARG_ID_SWAPNEXT:
2192 target = TAILQ_NEXT(source, entry);
2193 /* move the parent and let the sort handle the move */
2194 if (source->transient)
2195 source = find_window(source->transient);
2196 TAILQ_REMOVE(wl, source, entry);
2198 TAILQ_INSERT_HEAD(wl, source, entry);
2200 TAILQ_INSERT_AFTER(wl, target, source, entry);
2202 case SWM_ARG_ID_SWAPMAIN:
2203 target = TAILQ_FIRST(wl);
2204 if (target == source) {
2205 if (source->ws->focus_prev != NULL &&
2206 source->ws->focus_prev != target)
2208 source = source->ws->focus_prev;
2212 if (target == NULL || source == NULL)
2214 source->ws->focus_prev = target;
2215 TAILQ_REMOVE(wl, target, entry);
2216 TAILQ_INSERT_BEFORE(source, target, entry);
2217 TAILQ_REMOVE(wl, source, entry);
2218 TAILQ_INSERT_HEAD(wl, source, entry);
2220 case SWM_ARG_ID_MOVELAST:
2221 TAILQ_REMOVE(wl, source, entry);
2222 TAILQ_INSERT_TAIL(wl, source, entry);
2225 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
2235 focus_prev(struct ws_win *win)
2237 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2238 struct ws_win *cur_focus = NULL;
2239 struct ws_win_list *wl = NULL;
2240 struct workspace *ws = NULL;
2242 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
2244 if (!(win && win->ws))
2249 cur_focus = ws->focus;
2250 winlostfocus = cur_focus;
2252 /* pickle, just focus on whatever */
2253 if (cur_focus == NULL) {
2254 /* use prev_focus if valid */
2255 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2256 find_window(WINID(ws->focus_prev)))
2257 winfocus = ws->focus_prev;
2258 if (winfocus == NULL)
2259 winfocus = TAILQ_FIRST(wl);
2263 /* if transient focus on parent */
2264 if (cur_focus->transient) {
2265 winfocus = find_window(cur_focus->transient);
2269 /* if in max_stack try harder */
2270 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2271 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2272 if (cur_focus != ws->focus_prev)
2273 winfocus = ws->focus_prev;
2274 else if (cur_focus != ws->focus)
2275 winfocus = ws->focus;
2277 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2282 if (cur_focus == win)
2283 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2284 if (winfocus == NULL)
2285 winfocus = TAILQ_LAST(wl, ws_win_list);
2286 if (winfocus == NULL || winfocus == win)
2287 winfocus = TAILQ_NEXT(cur_focus, entry);
2289 if (winfocus == winlostfocus || winfocus == NULL)
2292 focus_magic(winfocus);
2296 focus(struct swm_region *r, union arg *args)
2298 struct ws_win *winfocus = NULL, *winlostfocus = NULL, *head;
2299 struct ws_win *cur_focus = NULL;
2300 struct ws_win_list *wl = NULL;
2301 struct workspace *ws = NULL;
2306 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
2308 /* treat FOCUS_CUR special */
2309 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2310 if (r->ws->focus && r->ws->focus->iconic == 0)
2311 winfocus = r->ws->focus;
2312 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2313 winfocus = r->ws->focus_prev;
2315 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2316 if (winfocus->iconic == 0)
2319 focus_magic(winfocus);
2323 if ((cur_focus = r->ws->focus) == NULL)
2328 winlostfocus = cur_focus;
2331 case SWM_ARG_ID_FOCUSPREV:
2332 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2334 head = TAILQ_LAST(wl, ws_win_list);
2336 if (WINID(winfocus) == cur_focus->transient) {
2337 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2339 head = TAILQ_LAST(wl, ws_win_list);
2344 if (winfocus && winfocus->iconic) {
2345 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
2346 if (winfocus->iconic == 0)
2351 case SWM_ARG_ID_FOCUSNEXT:
2352 head = TAILQ_NEXT(cur_focus, entry);
2354 head = TAILQ_FIRST(wl);
2358 if (winfocus && winfocus->iconic) {
2359 TAILQ_FOREACH(winfocus, wl, entry)
2360 if (winfocus->iconic == 0)
2365 case SWM_ARG_ID_FOCUSMAIN:
2366 winfocus = TAILQ_FIRST(wl);
2367 if (winfocus == cur_focus)
2368 winfocus = cur_focus->ws->focus_prev;
2374 if (winfocus == winlostfocus || winfocus == NULL)
2377 focus_magic(winfocus);
2381 cycle_layout(struct swm_region *r, union arg *args)
2383 struct workspace *ws = r->ws;
2384 struct ws_win *winfocus;
2387 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2389 winfocus = ws->focus;
2392 if (ws->cur_layout->l_stack == NULL)
2393 ws->cur_layout = &layouts[0];
2396 if (focus_mode == SWM_FOCUS_DEFAULT)
2397 drain_enter_notify();
2398 a.id = SWM_ARG_ID_FOCUSCUR;
2404 stack_config(struct swm_region *r, union arg *args)
2406 struct workspace *ws = r->ws;
2408 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
2411 if (ws->cur_layout->l_config != NULL)
2412 ws->cur_layout->l_config(ws, args->id);
2414 if (args->id != SWM_ARG_ID_STACKINIT)
2420 struct swm_geometry g;
2421 struct swm_region *r;
2424 DNPRINTF(SWM_D_STACK, "stack\n");
2426 for (i = 0; i < ScreenCount(display); i++) {
2428 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2429 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
2430 "(screen %d, region %d)\n", r->ws->idx, i, j++);
2432 /* start with screen geometry, adjust for bar */
2434 g.w -= 2 * border_width;
2435 g.h -= 2 * border_width;
2441 r->ws->cur_layout->l_stack(r->ws, &g);
2442 /* save r so we can track region changes */
2449 if (focus_mode == SWM_FOCUS_DEFAULT)
2450 drain_enter_notify();
2454 store_float_geom(struct ws_win *win, struct swm_region *r)
2456 /* retain window geom and region geom */
2457 win->g_float.x = win->g.x;
2458 win->g_float.y = win->g.y;
2459 win->g_float.w = win->g.w;
2460 win->g_float.h = win->g.h;
2461 win->rg_float.x = r->g.x;
2462 win->rg_float.y = r->g.y;
2463 win->rg_float.w = r->g.w;
2464 win->rg_float.h = r->g.h;
2465 win->g_floatvalid = 1;
2469 stack_floater(struct ws_win *win, struct swm_region *r)
2477 bzero(&wc, sizeof wc);
2478 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2481 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2482 * geom on ws switches or return from max mode
2485 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2486 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2488 * use stored g and rg to set relative position and size
2489 * as in old region or before max stack mode
2491 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2492 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2493 win->g.w = win->g_float.w;
2494 win->g.h = win->g_float.h;
2495 win->g_floatvalid = 0;
2498 win->floatmaxed = 0;
2500 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
2501 (win->g.h >= HEIGHT(r)))
2502 wc.border_width = 0;
2504 wc.border_width = border_width;
2505 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2506 win->g.w = (double)WIDTH(r) * dialog_ratio;
2507 win->g.h = (double)HEIGHT(r) * dialog_ratio;
2512 * floaters and transients are auto-centred unless moved
2515 win->g.x = r->g.x + (WIDTH(r) - win->g.w) /
2516 2 - wc.border_width;
2517 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) /
2518 2 - wc.border_width;
2521 /* win can be outside r if new r smaller than old r */
2522 /* Ensure top left corner inside r (move probs otherwise) */
2523 if (win->g.x < r->g.x - wc.border_width)
2524 win->g.x = r->g.x - wc.border_width;
2525 if (win->g.x > r->g.x + r->g.w - 1)
2526 win->g.x = (win->g.w > r->g.w) ? r->g.x :
2527 (r->g.x + r->g.w - win->g.w - 2 * wc.border_width);
2528 if (win->g.y < r->g.y - wc.border_width)
2529 win->g.y = r->g.y - wc.border_width;
2530 if (win->g.y > r->g.y + r->g.h - 1)
2531 win->g.y = (win->g.h > r->g.h) ? r->g.y :
2532 (r->g.y + r->g.h - win->g.h - 2 * wc.border_width);
2536 wc.width = win->g.w;
2537 wc.height = win->g.h;
2540 * Retain floater and transient geometry for correct positioning
2541 * when ws changes region
2543 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2544 store_float_geom(win, r);
2546 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
2547 win->id, wc.x, wc.y, wc.width, wc.height);
2549 XConfigureWindow(display, win->id, mask, &wc);
2553 * Send keystrokes to terminal to decrease/increase the font size as the
2554 * window size changes.
2557 adjust_font(struct ws_win *win)
2559 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2560 win->floating || win->transient)
2563 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2564 win->g.w / win->sh.width_inc < term_width &&
2565 win->font_steps < SWM_MAX_FONT_STEPS) {
2566 win->font_size_boundary[win->font_steps] =
2567 (win->sh.width_inc * term_width) + win->sh.base_width;
2570 win->last_inc = win->sh.width_inc;
2571 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2572 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2573 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
2576 win->last_inc = win->sh.width_inc;
2577 fake_keypress(win, XK_KP_Add, ShiftMask);
2581 #define SWAPXY(g) do { \
2583 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2584 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2587 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2590 XWindowAttributes wa;
2591 struct swm_geometry win_g, r_g = *g;
2592 struct ws_win *win, *fs_win = 0;
2593 int i, j, s, stacks;
2594 int w_inc = 1, h_inc, w_base = 1, h_base;
2595 int hrh, extra = 0, h_slice, last_h = 0;
2596 int split, colno, winno, mwin, msize, mscale;
2597 int remain, missing, v_slice, reconfigure;
2600 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2601 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2603 winno = count_win(ws, 0);
2604 if (winno == 0 && count_win(ws, 1) == 0)
2607 TAILQ_FOREACH(win, &ws->winlist, entry)
2608 if (win->transient == 0 && win->floating == 0
2609 && win->iconic == 0)
2616 w_inc = win->sh.width_inc;
2617 w_base = win->sh.base_width;
2618 mwin = ws->l_state.horizontal_mwin;
2619 mscale = ws->l_state.horizontal_msize;
2620 stacks = ws->l_state.horizontal_stacks;
2623 w_inc = win->sh.height_inc;
2624 w_base = win->sh.base_height;
2625 mwin = ws->l_state.vertical_mwin;
2626 mscale = ws->l_state.vertical_msize;
2627 stacks = ws->l_state.vertical_stacks;
2631 if (stacks > winno - mwin)
2632 stacks = winno - mwin;
2636 h_slice = r_g.h / SWM_H_SLICE;
2637 if (mwin && winno > mwin) {
2638 v_slice = r_g.w / SWM_V_SLICE;
2642 win_g.w = v_slice * mscale;
2644 if (w_inc > 1 && w_inc < v_slice) {
2645 /* adjust for window's requested size increment */
2646 remain = (win_g.w - w_base) % w_inc;
2647 missing = w_inc - remain;
2654 win_g.x += r_g.w - msize;
2657 colno = split = winno / stacks;
2658 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
2659 2 * border_width) / stacks);
2661 hrh = r_g.h / colno;
2662 extra = r_g.h - (colno * hrh);
2663 win_g.h = hrh - 2 * border_width;
2665 /* stack all the tiled windows */
2666 i = j = 0, s = stacks;
2667 TAILQ_FOREACH(win, &ws->winlist, entry) {
2668 if (win->transient != 0 || win->floating != 0)
2670 if (win->iconic != 0)
2673 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2678 if (split && i == split) {
2679 colno = (winno - mwin) / stacks;
2680 if (s <= (winno - mwin) % stacks)
2682 split = split + colno;
2683 hrh = (r_g.h / colno);
2684 extra = r_g.h - (colno * hrh);
2688 win_g.x += win_g.w + 2 * border_width;
2689 win_g.w = (r_g.w - msize -
2690 (stacks * 2 * border_width)) / stacks;
2692 win_g.w += (r_g.w - msize -
2693 (stacks * 2 * border_width)) % stacks;
2697 win_g.h = hrh - 2 * border_width;
2699 h_inc = win->sh.width_inc;
2700 h_base = win->sh.base_width;
2702 h_inc = win->sh.height_inc;
2703 h_base = win->sh.base_height;
2705 if (j == colno - 1) {
2706 win_g.h = hrh + extra;
2707 } else if (h_inc > 1 && h_inc < h_slice) {
2708 /* adjust for window's requested size increment */
2709 remain = (win_g.h - h_base) % h_inc;
2710 missing = h_inc - remain;
2712 if (missing <= extra || j == 0) {
2724 win_g.y += last_h + 2 * border_width;
2726 bzero(&wc, sizeof wc);
2727 if (disable_border && bar_enabled == 0 && winno == 1){
2728 wc.border_width = 0;
2729 win_g.w += 2 * border_width;
2730 win_g.h += 2 * border_width;
2732 wc.border_width = border_width;
2735 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2736 win->g.w != win_g.h || win->g.h != win_g.w) {
2738 win->g.x = wc.x = win_g.y;
2739 win->g.y = wc.y = win_g.x;
2740 win->g.w = wc.width = win_g.h;
2741 win->g.h = wc.height = win_g.w;
2744 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2745 win->g.w != win_g.w || win->g.h != win_g.h) {
2747 win->g.x = wc.x = win_g.x;
2748 win->g.y = wc.y = win_g.y;
2749 win->g.w = wc.width = win_g.w;
2750 win->g.h = wc.height = win_g.h;
2755 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2756 XConfigureWindow(display, win->id, mask, &wc);
2759 if (XGetWindowAttributes(display, win->id, &wa))
2760 if (wa.map_state == IsUnmapped)
2761 XMapRaised(display, win->id);
2769 /* now, stack all the floaters and transients */
2770 TAILQ_FOREACH(win, &ws->winlist, entry) {
2771 if (win->transient == 0 && win->floating == 0)
2773 if (win->iconic == 1)
2775 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2780 stack_floater(win, ws->r);
2781 XMapRaised(display, win->id);
2785 stack_floater(fs_win, ws->r);
2786 XMapRaised(display, fs_win->id);
2791 vertical_config(struct workspace *ws, int id)
2793 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2796 case SWM_ARG_ID_STACKRESET:
2797 case SWM_ARG_ID_STACKINIT:
2798 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2799 ws->l_state.vertical_mwin = 1;
2800 ws->l_state.vertical_stacks = 1;
2802 case SWM_ARG_ID_MASTERSHRINK:
2803 if (ws->l_state.vertical_msize > 1)
2804 ws->l_state.vertical_msize--;
2806 case SWM_ARG_ID_MASTERGROW:
2807 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2808 ws->l_state.vertical_msize++;
2810 case SWM_ARG_ID_MASTERADD:
2811 ws->l_state.vertical_mwin++;
2813 case SWM_ARG_ID_MASTERDEL:
2814 if (ws->l_state.vertical_mwin > 0)
2815 ws->l_state.vertical_mwin--;
2817 case SWM_ARG_ID_STACKINC:
2818 ws->l_state.vertical_stacks++;
2820 case SWM_ARG_ID_STACKDEC:
2821 if (ws->l_state.vertical_stacks > 1)
2822 ws->l_state.vertical_stacks--;
2830 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2832 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2834 stack_master(ws, g, 0, 0);
2838 horizontal_config(struct workspace *ws, int id)
2840 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2843 case SWM_ARG_ID_STACKRESET:
2844 case SWM_ARG_ID_STACKINIT:
2845 ws->l_state.horizontal_mwin = 1;
2846 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2847 ws->l_state.horizontal_stacks = 1;
2849 case SWM_ARG_ID_MASTERSHRINK:
2850 if (ws->l_state.horizontal_msize > 1)
2851 ws->l_state.horizontal_msize--;
2853 case SWM_ARG_ID_MASTERGROW:
2854 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2855 ws->l_state.horizontal_msize++;
2857 case SWM_ARG_ID_MASTERADD:
2858 ws->l_state.horizontal_mwin++;
2860 case SWM_ARG_ID_MASTERDEL:
2861 if (ws->l_state.horizontal_mwin > 0)
2862 ws->l_state.horizontal_mwin--;
2864 case SWM_ARG_ID_STACKINC:
2865 ws->l_state.horizontal_stacks++;
2867 case SWM_ARG_ID_STACKDEC:
2868 if (ws->l_state.horizontal_stacks > 1)
2869 ws->l_state.horizontal_stacks--;
2877 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2879 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
2881 stack_master(ws, g, 1, 0);
2884 /* fullscreen view */
2886 max_stack(struct workspace *ws, struct swm_geometry *g)
2889 struct swm_geometry gg = *g;
2890 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2894 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2899 winno = count_win(ws, 0);
2900 if (winno == 0 && count_win(ws, 1) == 0)
2903 TAILQ_FOREACH(win, &ws->winlist, entry) {
2904 if (win->transient) {
2906 parent = find_window(win->transient);
2910 if (win->floating && win->floatmaxed == 0 ) {
2912 * retain geometry for retrieval on exit from
2915 store_float_geom(win, ws->r);
2916 win->floatmaxed = 1;
2919 /* only reconfigure if necessary */
2920 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2922 bzero(&wc, sizeof wc);
2923 win->g.x = wc.x = gg.x;
2924 win->g.y = wc.y = gg.y;
2926 wc.border_width = border_width;
2927 win->g.w = wc.width = gg.w;
2928 win->g.h = wc.height = gg.h;
2930 wc.border_width = 0;
2931 win->g.w = wc.width = gg.w + 2 * border_width;
2932 win->g.h = wc.height = gg.h + 2 * border_width;
2934 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2935 XConfigureWindow(display, win->id, mask, &wc);
2937 /* unmap only if we don't have multi screen */
2938 if (win != ws->focus)
2939 if (!(ScreenCount(display) > 1 || outputs > 1))
2943 /* put the last transient on top */
2946 XMapRaised(display, parent->id);
2947 stack_floater(wintrans, ws->r);
2948 focus_magic(wintrans);
2953 send_to_ws(struct swm_region *r, union arg *args)
2955 int wsid = args->id;
2956 struct ws_win *win = NULL, *parent;
2957 struct workspace *ws, *nws;
2958 Atom ws_idx_atom = 0;
2959 unsigned char ws_idx_str[SWM_PROPLEN];
2968 if (win->ws->idx == wsid)
2971 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2974 nws = &win->s->ws[wsid];
2976 a.id = SWM_ARG_ID_FOCUSPREV;
2978 if (win->transient) {
2979 parent = find_window(win->transient);
2981 unmap_window(parent);
2982 TAILQ_REMOVE(&ws->winlist, parent, entry);
2983 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
2988 TAILQ_REMOVE(&ws->winlist, win, entry);
2989 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2992 /* Try to update the window's workspace property */
2993 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2995 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2996 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2998 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2999 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3006 raise_toggle(struct swm_region *r, union arg *args)
3008 if (r && r->ws == NULL)
3011 r->ws->always_raise = !r->ws->always_raise;
3013 /* bring floaters back to top */
3014 if (r->ws->always_raise == 0)
3019 iconify(struct swm_region *r, union arg *args)
3023 if (r->ws->focus == NULL)
3025 unmap_window(r->ws->focus);
3026 update_iconic(r->ws->focus, 1);
3028 if (focus_mode == SWM_FOCUS_DEFAULT)
3029 drain_enter_notify();
3030 r->ws->focus = NULL;
3031 a.id = SWM_ARG_ID_FOCUSCUR;
3036 get_win_name(Display *dpy, Window win, Atom wname, Atom stype,
3037 unsigned long *slen)
3040 unsigned long nitems, nbytes, nextra;
3041 unsigned char *prop = NULL;
3044 status = XGetWindowProperty(dpy, win, wname, 0L, 0L, False, stype,
3045 &rettype, &retfmt, &nitems, &nbytes, &prop);
3046 if (status != Success)
3050 status = XGetWindowProperty(dpy, win, wname, 0L, nbytes, False,
3051 stype, &rettype, &retfmt, &nitems, &nextra, &prop);
3052 if (status != Success) {
3056 if (rettype != stype) {
3065 uniconify(struct swm_region *r, union arg *args)
3073 DNPRINTF(SWM_D_MISC, "uniconify\n");
3075 if (r && r->ws == NULL)
3078 /* make sure we have anything to uniconify */
3079 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3080 if (win->ws == NULL)
3081 continue; /* should never happen */
3082 if (win->iconic == 0)
3091 spawn_select(r, args, "uniconify", &searchpid);
3093 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3096 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3097 if (win->ws == NULL)
3098 continue; /* should never happen */
3099 if (win->iconic == 0)
3102 name = get_win_name(display, win->id, a_wmname, a_string,
3106 fprintf(lfile, "%s.%lu\n", name, win->id);
3113 #define MAX_RESP_LEN 1024
3116 search_do_resp(void)
3120 char *name, *resp, *s;
3123 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3128 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3129 fprintf(stderr, "search: calloc\n");
3133 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3135 fprintf(stderr, "search: read error: %s\n", strerror(errno));
3138 resp[rbytes] = '\0';
3141 DNPRINTF(SWM_D_MISC, "search_do_resp: resp %s\n", resp);
3142 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3143 if (win->iconic == 0)
3145 name = get_win_name(display, win->id, a_wmname, a_string, &len);
3148 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3153 if (strncmp(s, resp, len) == 0) {
3154 /* XXX this should be a callback to generalize */
3155 update_iconic(win, 0);
3166 wkill(struct swm_region *r, union arg *args)
3168 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
3170 if (r->ws->focus == NULL)
3173 if (args->id == SWM_ARG_ID_KILLWINDOW)
3174 XKillClient(display, r->ws->focus->id);
3176 if (r->ws->focus->can_delete)
3177 client_msg(r->ws->focus, adelete);
3182 floating_toggle_win(struct ws_win *win)
3184 struct swm_region *r;
3194 /* reject floating toggles in max stack mode */
3195 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3198 if (win->floating) {
3199 if (!win->floatmaxed) {
3200 /* retain position for refloat */
3201 store_float_geom(win, r);
3205 if (win->g_floatvalid) {
3206 /* refloat at last floating relative position */
3207 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
3208 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
3209 win->g.w = win->g_float.w;
3210 win->g.h = win->g_float.h;
3215 ewmh_update_actions(win);
3221 floating_toggle(struct swm_region *r, union arg *args)
3223 struct ws_win *win = r->ws->focus;
3229 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3230 _NET_WM_STATE_TOGGLE);
3233 if (focus_mode == SWM_FOCUS_DEFAULT)
3234 drain_enter_notify();
3236 if (win == win->ws->focus) {
3237 a.id = SWM_ARG_ID_FOCUSCUR;
3238 focus(win->ws->r, &a);
3243 resize_window(struct ws_win *win, int center)
3247 struct swm_region *r;
3249 r = root_to_region(win->wa.root);
3250 bzero(&wc, sizeof wc);
3251 mask = CWBorderWidth | CWWidth | CWHeight;
3252 wc.border_width = border_width;
3253 wc.width = win->g.w;
3254 wc.height = win->g.h;
3255 if (center == SWM_ARG_ID_CENTER) {
3256 wc.x = (WIDTH(r) - win->g.w) / 2 - border_width;
3257 wc.y = (HEIGHT(r) - win->g.h) / 2 - border_width;
3261 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
3262 win->id, wc.x, wc.y, wc.width, wc.height);
3264 XConfigureWindow(display, win->id, mask, &wc);
3268 resize(struct ws_win *win, union arg *args)
3272 struct swm_region *r = win->ws->r;
3276 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
3277 win->id, win->floating, win->transient);
3279 if (!(win->transient != 0 || win->floating != 0))
3282 /* reject resizes in max mode for floaters (transient ok) */
3283 if (win->floatmaxed)
3287 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3291 if (focus_mode == SWM_FOCUS_DEFAULT)
3292 drain_enter_notify();
3294 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3295 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3298 /* place pointer at bottom left corner or nearest point inside r */
3299 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
3300 relx = win->g.w - 1;
3302 relx = r->g.x + r->g.w - win->g.x - 1;
3304 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
3305 rely = win->g.h - 1;
3307 rely = r->g.y + r->g.h - win->g.y - 1;
3309 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
3311 XMaskEvent(display, MOUSEMASK | ExposureMask |
3312 SubstructureRedirectMask, &ev);
3314 case ConfigureRequest:
3317 handler[ev.type](&ev);
3320 /* do not allow resize outside of region */
3321 if ( ev.xmotion.x_root < r->g.x ||
3322 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3323 ev.xmotion.y_root < r->g.y ||
3324 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3327 if (ev.xmotion.x <= 1)
3329 if (ev.xmotion.y <= 1)
3331 win->g.w = ev.xmotion.x + 1;
3332 win->g.h = ev.xmotion.y + 1;
3334 /* not free, don't sync more than 120 times / second */
3335 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3336 time = ev.xmotion.time;
3337 XSync(display, False);
3338 resize_window(win, args->id);
3342 } while (ev.type != ButtonRelease);
3344 XSync(display, False);
3345 resize_window(win, args->id);
3347 store_float_geom(win,r);
3349 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
3351 XUngrabPointer(display, CurrentTime);
3354 drain_enter_notify();
3358 move_window(struct ws_win *win)
3362 struct swm_region *r;
3364 r = root_to_region(win->wa.root);
3365 bzero(&wc, sizeof wc);
3369 wc.border_width = border_width;
3371 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
3372 win->id, wc.x, wc.y, wc.width, wc.height);
3374 XConfigureWindow(display, win->id, mask, &wc);
3378 move(struct ws_win *win, union arg *args)
3382 struct swm_region *r = win->ws->r;
3384 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
3385 win->id, win->floating, win->transient);
3387 /* in max_stack mode should only move transients */
3388 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
3392 if (win->floating == 0 && !win->transient) {
3393 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3396 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3401 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3402 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3404 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3406 XMaskEvent(display, MOUSEMASK | ExposureMask |
3407 SubstructureRedirectMask, &ev);
3409 case ConfigureRequest:
3412 handler[ev.type](&ev);
3415 /* don't allow to move window origin out of region */
3416 if ( ev.xmotion.x_root < r->g.x ||
3417 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3418 ev.xmotion.y_root < r->g.y ||
3419 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3422 win->g.x = ev.xmotion.x_root - border_width;
3423 win->g.y = ev.xmotion.y_root - border_width;
3425 /* not free, don't sync more than 120 times / second */
3426 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3427 time = ev.xmotion.time;
3428 XSync(display, False);
3433 } while (ev.type != ButtonRelease);
3435 XSync(display, False);
3438 store_float_geom(win,r);
3439 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3440 XUngrabPointer(display, CurrentTime);
3443 drain_enter_notify();
3446 /* user/key callable function IDs */
3504 kf_dumpwins, /* MUST BE LAST */
3508 /* key definitions */
3510 dummykeyfunc(struct swm_region *r, union arg *args)
3515 legacyfunc(struct swm_region *r, union arg *args)
3520 char name[SWM_FUNCNAME_LEN];
3521 void (*func)(struct swm_region *r, union arg *);
3523 } keyfuncs[kf_invalid + 1] = {
3524 /* name function argument */
3525 { "cycle_layout", cycle_layout, {0} },
3526 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
3527 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
3528 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
3529 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
3530 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
3531 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
3532 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
3533 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
3534 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
3535 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
3536 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
3537 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
3538 { "spawn_term", spawnterm, {.argv = spawn_term} },
3539 { "spawn_menu", legacyfunc, {0} },
3540 { "quit", quit, {0} },
3541 { "restart", restart, {0} },
3542 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
3543 { "ws_1", switchws, {.id = 0} },
3544 { "ws_2", switchws, {.id = 1} },
3545 { "ws_3", switchws, {.id = 2} },
3546 { "ws_4", switchws, {.id = 3} },
3547 { "ws_5", switchws, {.id = 4} },
3548 { "ws_6", switchws, {.id = 5} },
3549 { "ws_7", switchws, {.id = 6} },
3550 { "ws_8", switchws, {.id = 7} },
3551 { "ws_9", switchws, {.id = 8} },
3552 { "ws_10", switchws, {.id = 9} },
3553 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
3554 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
3555 { "ws_prior", priorws, {0} },
3556 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
3557 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
3558 { "mvws_1", send_to_ws, {.id = 0} },
3559 { "mvws_2", send_to_ws, {.id = 1} },
3560 { "mvws_3", send_to_ws, {.id = 2} },
3561 { "mvws_4", send_to_ws, {.id = 3} },
3562 { "mvws_5", send_to_ws, {.id = 4} },
3563 { "mvws_6", send_to_ws, {.id = 5} },
3564 { "mvws_7", send_to_ws, {.id = 6} },
3565 { "mvws_8", send_to_ws, {.id = 7} },
3566 { "mvws_9", send_to_ws, {.id = 8} },
3567 { "mvws_10", send_to_ws, {.id = 9} },
3568 { "bar_toggle", bar_toggle, {0} },
3569 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
3570 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
3571 { "screenshot_all", legacyfunc, {0} },
3572 { "screenshot_wind", legacyfunc, {0} },
3573 { "float_toggle", floating_toggle,{0} },
3574 { "version", version, {0} },
3575 { "spawn_lock", legacyfunc, {0} },
3576 { "spawn_initscr", legacyfunc, {0} },
3577 { "spawn_custom", dummykeyfunc, {0} },
3578 { "iconify", iconify, {0} },
3579 { "uniconify", uniconify, {0} },
3580 { "raise_toggle", raise_toggle, {0} },
3581 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
3582 { "invalid key func", NULL, {0} },
3587 enum keyfuncid funcid;
3590 int keys_size = 0, keys_length = 0;
3591 struct key *keys = NULL;
3594 enum { client_click, root_click };
3596 unsigned int action;
3598 unsigned int button;
3599 void (*func)(struct ws_win *, union arg *);
3602 /* action key mouse button func args */
3603 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
3604 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
3605 { client_click, MODKEY, Button1, move, {0} },
3609 update_modkey(unsigned int mod)
3614 for (i = 0; i < keys_length; i++)
3615 if (keys[i].mod & ShiftMask)
3616 keys[i].mod = mod | ShiftMask;
3620 for (i = 0; i < LENGTH(buttons); i++)
3621 if (buttons[i].mask & ShiftMask)
3622 buttons[i].mask = mod | ShiftMask;
3624 buttons[i].mask = mod;
3634 int spawns_size = 0, spawns_length = 0;
3635 struct spawn_prog *spawns = NULL;
3638 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
3641 struct spawn_prog *prog = NULL;
3643 char *ap, **real_args;
3645 DNPRINTF(SWM_D_SPAWN, "spawn_expand %s\n", spawn_name);
3648 for (i = 0; i < spawns_length; i++) {
3649 if (!strcasecmp(spawn_name, spawns[i].name))
3653 fprintf(stderr, "spawn_custom: program %s not found\n",
3658 /* make room for expanded args */
3659 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
3660 err(1, "spawn_custom: calloc real_args");
3662 /* expand spawn_args into real_args */
3663 for (i = 0; i < prog->argc; i++) {
3665 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
3666 if (!strcasecmp(ap, "$bar_border")) {
3668 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
3670 err(1, "spawn_custom border color");
3671 } else if (!strcasecmp(ap, "$bar_color")) {
3673 strdup(r->s->c[SWM_S_COLOR_BAR].name))
3675 err(1, "spawn_custom bar color");
3676 } else if (!strcasecmp(ap, "$bar_font")) {
3677 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
3679 err(1, "spawn_custom bar fonts");
3680 } else if (!strcasecmp(ap, "$bar_font_color")) {
3682 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
3684 err(1, "spawn_custom color font");
3685 } else if (!strcasecmp(ap, "$color_focus")) {
3687 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
3689 err(1, "spawn_custom color focus");
3690 } else if (!strcasecmp(ap, "$color_unfocus")) {
3692 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
3694 err(1, "spawn_custom color unfocus");
3696 /* no match --> copy as is */
3697 if ((real_args[i] = strdup(ap)) == NULL)
3698 err(1, "spawn_custom strdup(ap)");
3700 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
3705 if ((swm_debug & SWM_D_SPAWN) != 0) {
3706 fprintf(stderr, "spawn_custom: result = ");
3707 for (i = 0; i < prog->argc; i++)
3708 fprintf(stderr, "\"%s\" ", real_args[i]);
3709 fprintf(stderr, "\n");
3712 *ret_args = real_args;
3713 return (prog->argc);
3717 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
3723 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3727 spawn(r->ws->idx, &a, 1);
3729 for (i = 0; i < spawn_argc; i++)
3735 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
3741 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3745 if (pipe(select_list_pipe) == -1)
3746 err(1, "pipe error");
3747 if (pipe(select_resp_pipe) == -1)
3748 err(1, "pipe error");
3750 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
3751 err(1, "could not disable SIGPIPE");
3752 switch (*pid = fork()) {
3754 err(1, "cannot fork");
3757 if (dup2(select_list_pipe[0], 0) == -1)
3759 if (dup2(select_resp_pipe[1], 1) == -1)
3761 close(select_list_pipe[1]);
3762 close(select_resp_pipe[0]);
3763 spawn(r->ws->idx, &a, 0);
3765 default: /* parent */
3766 close(select_list_pipe[0]);
3767 close(select_resp_pipe[1]);
3771 for (i = 0; i < spawn_argc; i++)
3777 setspawn(struct spawn_prog *prog)
3781 if (prog == NULL || prog->name == NULL)
3785 for (i = 0; i < spawns_length; i++) {
3786 if (!strcmp(spawns[i].name, prog->name)) {
3788 if (prog->argv == NULL) {
3790 DNPRINTF(SWM_D_SPAWN,
3791 "setspawn: delete #%d %s\n",
3793 free(spawns[i].name);
3794 for (j = 0; j < spawns[i].argc; j++)
3795 free(spawns[i].argv[j]);
3796 free(spawns[i].argv);
3797 j = spawns_length - 1;
3799 spawns[i] = spawns[j];
3804 DNPRINTF(SWM_D_SPAWN,
3805 "setspawn: replace #%d %s\n",
3807 free(spawns[i].name);
3808 for (j = 0; j < spawns[i].argc; j++)
3809 free(spawns[i].argv[j]);
3810 free(spawns[i].argv);
3813 /* found case handled */
3819 if (prog->argv == NULL) {
3821 "error: setspawn: cannot find program %s", prog->name);
3826 /* not found: add */
3827 if (spawns_size == 0 || spawns == NULL) {
3829 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
3830 spawns = malloc((size_t)spawns_size *
3831 sizeof(struct spawn_prog));
3832 if (spawns == NULL) {
3833 fprintf(stderr, "setspawn: malloc failed\n");
3837 } else if (spawns_length == spawns_size) {
3839 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
3840 spawns = realloc(spawns, (size_t)spawns_size *
3841 sizeof(struct spawn_prog));
3842 if (spawns == NULL) {
3843 fprintf(stderr, "setspawn: realloc failed\n");
3849 if (spawns_length < spawns_size) {
3850 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
3851 spawns_length, prog->name);
3852 i = spawns_length++;
3855 fprintf(stderr, "spawns array problem?\n");
3856 if (spawns == NULL) {
3857 fprintf(stderr, "spawns array is NULL!\n");
3865 setconfspawn(char *selector, char *value, int flags)
3867 struct spawn_prog *prog;
3868 char *vp, *cp, *word;
3870 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
3871 if ((prog = calloc(1, sizeof *prog)) == NULL)
3872 err(1, "setconfspawn: calloc prog");
3873 prog->name = strdup(selector);
3874 if (prog->name == NULL)
3875 err(1, "setconfspawn prog->name");
3876 if ((cp = vp = strdup(value)) == NULL)
3877 err(1, "setconfspawn: strdup(value) ");
3878 while ((word = strsep(&cp, " \t")) != NULL) {
3879 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
3881 cp += (long)strspn(cp, " \t");
3882 if (strlen(word) > 0) {
3884 if ((prog->argv = realloc(prog->argv,
3885 prog->argc * sizeof(char *))) == NULL)
3886 err(1, "setconfspawn: realloc");
3887 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
3888 err(1, "setconfspawn: strdup");
3895 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
3902 setconfspawn("term", "xterm", 0);
3903 setconfspawn("screenshot_all", "screenshot.sh full", 0);
3904 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
3905 setconfspawn("lock", "xlock", 0);
3906 setconfspawn("initscr", "initscreen.sh", 0);
3907 setconfspawn("menu", "dmenu_run"
3910 " -nf $bar_font_color"
3912 " -sf $bar_color", 0);
3913 setconfspawn("uniconify", "dmenu"
3917 " -nf $bar_font_color"
3919 " -sf $bar_color", 0);
3923 #define SWM_MODNAME_SIZE 32
3924 #define SWM_KEY_WS "\n+ \t"
3926 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
3930 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
3931 if (mod == NULL || ks == NULL) {
3932 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
3935 if (keystr == NULL || strlen(keystr) == 0) {
3936 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
3942 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
3943 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
3945 cp += (long)strspn(cp, SWM_KEY_WS);
3946 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
3948 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
3950 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
3952 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
3954 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
3956 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
3958 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
3959 *mod |= ControlMask;
3961 *ks = XStringToKeysym(name);
3962 XConvertCase(*ks, ks, &uks);
3963 if (ks == NoSymbol) {
3965 "parsekeys: invalid key %s\n",
3971 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
3976 strdupsafe(char *str)
3981 return (strdup(str));
3985 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
3989 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
3990 keyfuncs[kfid].name, spawn_name);
3992 for (i = 0; i < keys_length; i++) {
3993 if (keys[i].mod == mod && keys[i].keysym == ks) {
3994 if (kfid == kf_invalid) {
3997 "setkeybinding: delete #%d %s\n",
3998 i, keyfuncs[keys[i].funcid].name);
3999 free(keys[i].spawn_name);
4000 j = keys_length - 1;
4004 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4007 /* found: replace */
4009 "setkeybinding: replace #%d %s %s\n",
4010 i, keyfuncs[keys[i].funcid].name,
4012 free(keys[i].spawn_name);
4014 keys[i].keysym = ks;
4015 keys[i].funcid = kfid;
4016 keys[i].spawn_name = strdupsafe(spawn_name);
4017 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4022 if (kfid == kf_invalid) {
4024 "error: setkeybinding: cannot find mod/key combination");
4025 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4028 /* not found: add */
4029 if (keys_size == 0 || keys == NULL) {
4031 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
4032 keys = malloc((size_t)keys_size * sizeof(struct key));
4034 fprintf(stderr, "malloc failed\n");
4038 } else if (keys_length == keys_size) {
4040 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
4041 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
4043 fprintf(stderr, "realloc failed\n");
4048 if (keys_length < keys_size) {
4050 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
4051 j, keyfuncs[kfid].name, spawn_name);
4053 keys[j].keysym = ks;
4054 keys[j].funcid = kfid;
4055 keys[j].spawn_name = strdupsafe(spawn_name);
4057 fprintf(stderr, "keys array problem?\n");
4059 fprintf(stderr, "keys array problem\n");
4063 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4067 setconfbinding(char *selector, char *value, int flags)
4069 enum keyfuncid kfid;
4073 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
4074 if (selector == NULL) {
4075 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
4076 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4078 setkeybinding(mod, ks, kfid, NULL);
4083 /* search by key function name */
4084 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
4085 if (strncasecmp(selector, keyfuncs[kfid].name,
4086 SWM_FUNCNAME_LEN) == 0) {
4087 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4089 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4090 setkeybinding(mod, ks, kfid, NULL);
4096 /* search by custom spawn name */
4097 for (i = 0; i < spawns_length; i++) {
4098 if (strcasecmp(selector, spawns[i].name) == 0) {
4099 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4101 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4102 setkeybinding(mod, ks, kf_spawn_custom,
4109 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
4116 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
4117 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
4118 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
4119 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
4120 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
4121 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
4122 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
4123 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
4124 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
4125 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
4126 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
4127 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
4128 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
4129 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
4130 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
4131 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
4132 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
4133 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
4134 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
4135 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
4136 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
4137 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
4138 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
4139 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
4140 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
4141 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
4142 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
4143 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
4144 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
4145 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
4146 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
4147 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
4148 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
4149 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
4150 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
4151 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
4152 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
4153 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
4154 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
4155 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
4156 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
4157 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
4158 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
4159 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
4160 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
4161 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
4162 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
4163 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
4164 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
4165 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
4166 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
4167 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
4168 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
4169 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
4170 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
4171 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
4172 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
4174 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
4179 updatenumlockmask(void)
4182 XModifierKeymap *modmap;
4184 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
4186 modmap = XGetModifierMapping(display);
4187 for (i = 0; i < 8; i++)
4188 for (j = 0; j < modmap->max_keypermod; j++)
4189 if (modmap->modifiermap[i * modmap->max_keypermod + j]
4190 == XKeysymToKeycode(display, XK_Num_Lock))
4191 numlockmask = (1 << i);
4193 XFreeModifiermap(modmap);
4199 unsigned int i, j, k;
4201 unsigned int modifiers[] =
4202 { 0, LockMask, numlockmask, numlockmask | LockMask };
4204 DNPRINTF(SWM_D_MISC, "grabkeys\n");
4205 updatenumlockmask();
4207 for (k = 0; k < ScreenCount(display); k++) {
4208 if (TAILQ_EMPTY(&screens[k].rl))
4210 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
4211 for (i = 0; i < keys_length; i++) {
4212 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
4213 for (j = 0; j < LENGTH(modifiers); j++)
4214 XGrabKey(display, code,
4215 keys[i].mod | modifiers[j],
4216 screens[k].root, True,
4217 GrabModeAsync, GrabModeAsync);
4223 grabbuttons(struct ws_win *win, int focused)
4226 unsigned int modifiers[] =
4227 { 0, LockMask, numlockmask, numlockmask|LockMask };
4229 updatenumlockmask();
4230 XUngrabButton(display, AnyButton, AnyModifier, win->id);
4232 for (i = 0; i < LENGTH(buttons); i++)
4233 if (buttons[i].action == client_click)
4234 for (j = 0; j < LENGTH(modifiers); j++)
4235 XGrabButton(display, buttons[i].button,
4236 buttons[i].mask | modifiers[j],
4237 win->id, False, BUTTONMASK,
4238 GrabModeAsync, GrabModeSync, None,
4241 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
4242 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
4245 const char *quirkname[] = {
4246 "NONE", /* config string for "no value" */
4255 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
4256 #define SWM_Q_WS "\n|+ \t"
4258 parsequirks(char *qstr, unsigned long *quirk)
4268 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
4270 cp += (long)strspn(cp, SWM_Q_WS);
4271 for (i = 0; i < LENGTH(quirkname); i++) {
4272 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
4273 DNPRINTF(SWM_D_QUIRK,
4274 "parsequirks: %s\n", name);
4279 *quirk |= 1 << (i-1);
4283 if (i >= LENGTH(quirkname)) {
4284 DNPRINTF(SWM_D_QUIRK,
4285 "parsequirks: invalid quirk [%s]\n", name);
4293 setquirk(const char *class, const char *name, const int quirk)
4298 for (i = 0; i < quirks_length; i++) {
4299 if (!strcmp(quirks[i].class, class) &&
4300 !strcmp(quirks[i].name, name)) {
4303 DNPRINTF(SWM_D_QUIRK,
4304 "setquirk: delete #%d %s:%s\n",
4305 i, quirks[i].class, quirks[i].name);
4306 free(quirks[i].class);
4307 free(quirks[i].name);
4308 j = quirks_length - 1;
4310 quirks[i] = quirks[j];
4314 /* found: replace */
4315 DNPRINTF(SWM_D_QUIRK,
4316 "setquirk: replace #%d %s:%s\n",
4317 i, quirks[i].class, quirks[i].name);
4318 free(quirks[i].class);
4319 free(quirks[i].name);
4320 quirks[i].class = strdup(class);
4321 quirks[i].name = strdup(name);
4322 quirks[i].quirk = quirk;
4329 "error: setquirk: cannot find class/name combination");
4332 /* not found: add */
4333 if (quirks_size == 0 || quirks == NULL) {
4335 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
4336 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
4337 if (quirks == NULL) {
4338 fprintf(stderr, "setquirk: malloc failed\n");
4342 } else if (quirks_length == quirks_size) {
4344 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
4345 quirks = realloc(quirks,
4346 (size_t)quirks_size * sizeof(struct quirk));
4347 if (quirks == NULL) {
4348 fprintf(stderr, "setquirk: realloc failed\n");
4353 if (quirks_length < quirks_size) {
4354 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
4355 j = quirks_length++;
4356 quirks[j].class = strdup(class);
4357 quirks[j].name = strdup(name);
4358 quirks[j].quirk = quirk;
4360 fprintf(stderr, "quirks array problem?\n");
4361 if (quirks == NULL) {
4362 fprintf(stderr, "quirks array problem!\n");
4369 setconfquirk(char *selector, char *value, int flags)
4371 char *cp, *class, *name;
4373 unsigned long quirks;
4374 if (selector == NULL)
4376 if ((cp = strchr(selector, ':')) == NULL)
4381 if ((retval = parsequirks(value, &quirks)) == 0)
4382 setquirk(class, name, quirks);
4389 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
4390 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
4391 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
4392 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
4393 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4394 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
4395 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4396 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4397 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4398 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4399 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
4400 setquirk("pcb", "pcb", SWM_Q_FLOAT);
4401 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
4404 /* conf file stuff */
4405 #define SWM_CONF_FILE "scrotwm.conf"
4407 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH,
4408 SWM_S_STACK_ENABLED, SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT,
4409 SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED,
4410 SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
4411 SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED,
4412 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH,
4413 SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM,
4414 SWM_S_SS_APP, SWM_S_DIALOG_RATIO, SWM_S_BAR_AT_BOTTOM
4418 setconfvalue(char *selector, char *value, int flags)
4421 case SWM_S_BAR_DELAY:
4422 bar_delay = atoi(value);
4424 case SWM_S_BAR_ENABLED:
4425 bar_enabled = atoi(value);
4427 case SWM_S_BAR_BORDER_WIDTH:
4428 bar_border_width = atoi(value);
4430 case SWM_S_BAR_AT_BOTTOM:
4431 bar_at_bottom = atoi(value);
4433 case SWM_S_STACK_ENABLED:
4434 stack_enabled = atoi(value);
4436 case SWM_S_CLOCK_ENABLED:
4437 clock_enabled = atoi(value);
4439 case SWM_S_CLOCK_FORMAT:
4440 #ifndef SWM_DENY_CLOCK_FORMAT
4442 if ((clock_format = strdup(value)) == NULL)
4443 err(1, "setconfvalue: clock_format");
4446 case SWM_S_CYCLE_EMPTY:
4447 cycle_empty = atoi(value);
4449 case SWM_S_CYCLE_VISIBLE:
4450 cycle_visible = atoi(value);
4452 case SWM_S_SS_ENABLED:
4453 ss_enabled = atoi(value);
4455 case SWM_S_TERM_WIDTH:
4456 term_width = atoi(value);
4458 case SWM_S_TITLE_CLASS_ENABLED:
4459 title_class_enabled = atoi(value);
4461 case SWM_S_WINDOW_NAME_ENABLED:
4462 window_name_enabled = atoi(value);
4464 case SWM_S_TITLE_NAME_ENABLED:
4465 title_name_enabled = atoi(value);
4467 case SWM_S_FOCUS_MODE:
4468 if (!strcmp(value, "default"))
4469 focus_mode = SWM_FOCUS_DEFAULT;
4470 else if (!strcmp(value, "follow_cursor"))
4471 focus_mode = SWM_FOCUS_FOLLOW;
4472 else if (!strcmp(value, "synergy"))
4473 focus_mode = SWM_FOCUS_SYNERGY;
4475 err(1, "focus_mode");
4477 case SWM_S_DISABLE_BORDER:
4478 disable_border = atoi(value);
4480 case SWM_S_BORDER_WIDTH:
4481 border_width = atoi(value);
4483 case SWM_S_BAR_FONT:
4485 if ((bar_fonts[0] = strdup(value)) == NULL)
4486 err(1, "setconfvalue: bar_font");
4488 case SWM_S_BAR_ACTION:
4490 if ((bar_argv[0] = strdup(value)) == NULL)
4491 err(1, "setconfvalue: bar_action");
4493 case SWM_S_SPAWN_TERM:
4494 free(spawn_term[0]);
4495 if ((spawn_term[0] = strdup(value)) == NULL)
4496 err(1, "setconfvalue: spawn_term");
4500 case SWM_S_DIALOG_RATIO:
4501 dialog_ratio = atof(value);
4502 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
4512 setconfmodkey(char *selector, char *value, int flags)
4514 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
4515 update_modkey(Mod1Mask);
4516 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
4517 update_modkey(Mod2Mask);
4518 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
4519 update_modkey(Mod3Mask);
4520 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
4521 update_modkey(Mod4Mask);
4528 setconfcolor(char *selector, char *value, int flags)
4530 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
4535 setconfregion(char *selector, char *value, int flags)
4537 custom_region(value);
4542 setautorun(char *selector, char *value, int flags)
4552 if (getenv("SWM_STARTED"))
4556 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
4557 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'\n");
4559 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
4560 errx(1, "autorun: invalid workspace %d\n", ws_id + 1);
4563 * This is a little intricate
4565 * If the pid already exists we simply reuse it because it means it was
4566 * used before AND not claimed by manage_window. We get away with
4567 * altering it in the parent after INSERT because this can not be a race
4570 while ((ap = strsep(&sp, " \t")) != NULL) {
4573 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
4575 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
4576 err(1, "setautorun: realloc");
4577 a.argv[argc - 1] = ap;
4580 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
4581 err(1, "setautorun: realloc");
4582 a.argv[argc] = NULL;
4584 if ((pid = fork()) == 0) {
4585 spawn(ws_id, &a, 1);
4594 p = calloc(1, sizeof *p);
4597 TAILQ_INSERT_TAIL(&pidlist, p, entry);
4607 setlayout(char *selector, char *value, int flags)
4609 int ws_id, st, i, x, mg, ma, si, raise;
4611 struct workspace *ws;
4613 if (getenv("SWM_STARTED"))
4617 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
4618 &ws_id, &mg, &ma, &si, &raise, s) != 6)
4619 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
4620 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
4623 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
4624 errx(1, "layout: invalid workspace %d\n", ws_id + 1);
4626 if (!strcasecmp(s, "vertical"))
4628 else if (!strcasecmp(s, "horizontal"))
4630 else if (!strcasecmp(s, "fullscreen"))
4633 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
4634 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
4637 for (i = 0; i < ScreenCount(display); i++) {
4638 ws = (struct workspace *)&screens[i].ws;
4639 ws[ws_id].cur_layout = &layouts[st];
4641 ws[ws_id].always_raise = raise;
4642 if (st == SWM_MAX_STACK)
4646 for (x = 0; x < abs(mg); x++) {
4647 ws[ws_id].cur_layout->l_config(&ws[ws_id],
4648 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
4649 SWM_ARG_ID_MASTERSHRINK);
4653 for (x = 0; x < abs(ma); x++) {
4654 ws[ws_id].cur_layout->l_config(&ws[ws_id],
4655 ma >= 0 ? SWM_ARG_ID_MASTERADD :
4656 SWM_ARG_ID_MASTERDEL);
4660 for (x = 0; x < abs(si); x++) {
4661 ws[ws_id].cur_layout->l_config(&ws[ws_id],
4662 si >= 0 ? SWM_ARG_ID_STACKINC :
4663 SWM_ARG_ID_STACKDEC);
4671 /* config options */
4672 struct config_option {
4674 int (*func)(char*, char*, int);
4677 struct config_option configopt[] = {
4678 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
4679 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
4680 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
4681 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
4682 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
4683 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
4684 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
4685 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
4686 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
4687 { "bind", setconfbinding, 0 },
4688 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
4689 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
4690 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
4691 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
4692 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
4693 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
4694 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
4695 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
4696 { "modkey", setconfmodkey, 0 },
4697 { "program", setconfspawn, 0 },
4698 { "quirk", setconfquirk, 0 },
4699 { "region", setconfregion, 0 },
4700 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
4701 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
4702 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
4703 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
4704 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
4705 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
4706 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
4707 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
4708 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
4709 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
4710 { "autorun", setautorun, 0 },
4711 { "layout", setlayout, 0 },
4716 conf_load(char *filename)
4719 char *line, *cp, *optsub, *optval;
4720 size_t linelen, lineno = 0;
4721 int wordlen, i, optind;
4722 struct config_option *opt;
4724 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
4726 if (filename == NULL) {
4727 fprintf(stderr, "conf_load: no filename\n");
4730 if ((config = fopen(filename, "r")) == NULL) {
4731 warn("conf_load: fopen");
4735 while (!feof(config)) {
4736 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
4739 err(1, "%s", filename);
4744 cp += strspn(cp, " \t\n"); /* eat whitespace */
4745 if (cp[0] == '\0') {
4750 /* get config option */
4751 wordlen = strcspn(cp, "=[ \t\n");
4753 warnx("%s: line %zd: no option found",
4758 for (i = 0; i < LENGTH(configopt); i++) {
4759 opt = &configopt[i];
4760 if (!strncasecmp(cp, opt->optname, wordlen) &&
4761 strlen(opt->optname) == wordlen) {
4767 warnx("%s: line %zd: unknown option %.*s",
4768 filename, lineno, wordlen, cp);
4772 cp += strspn(cp, " \t\n"); /* eat whitespace */
4773 /* get [selector] if any */
4777 wordlen = strcspn(cp, "]");
4780 warnx("%s: line %zd: syntax error",
4784 asprintf(&optsub, "%.*s", wordlen, cp);
4787 cp += strspn(cp, "] \t\n"); /* eat trailing */
4789 cp += strspn(cp, "= \t\n"); /* eat trailing */
4791 optval = strdup(cp);
4792 /* call function to deal with it all */
4793 if (configopt[optind].func(optsub, optval,
4794 configopt[optind].funcflags) != 0) {
4795 fprintf(stderr, "%s line %zd: %s\n",
4796 filename, lineno, line);
4797 errx(1, "%s: line %zd: invalid data for %s",
4798 filename, lineno, configopt[optind].optname);
4806 DNPRINTF(SWM_D_CONF, "conf_load end\n");
4812 set_child_transient(struct ws_win *win, Window *trans)
4814 struct ws_win *parent, *w;
4815 XWMHints *wmh = NULL;
4816 struct swm_region *r;
4817 struct workspace *ws;
4819 parent = find_window(win->transient);
4821 parent->child_trans = win;
4823 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
4824 " for %lu trans %lu\n", win->id, win->transient);
4826 if (win->hints == NULL) {
4827 fprintf(stderr, "no hints for %lu\n", win->id);
4831 r = root_to_region(win->wa.root);
4833 /* parent doen't exist in our window list */
4834 TAILQ_FOREACH(w, &ws->winlist, entry) {
4838 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
4839 fprintf(stderr, "can't get hints for %lu\n",
4844 if (win->hints->window_group != wmh->window_group)
4847 w->child_trans = win;
4848 win->transient = w->id;
4850 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
4851 "transient to %lu\n", win->transient);
4861 window_get_pid(Window win)
4863 Atom actual_type_return;
4864 int actual_format_return = 0;
4865 unsigned long nitems_return = 0;
4866 unsigned long bytes_after_return = 0;
4870 unsigned char *prop = NULL;
4872 if (XGetWindowProperty(display, win,
4873 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
4874 XA_CARDINAL, &actual_type_return, &actual_format_return,
4875 &nitems_return, &bytes_after_return,
4876 (unsigned char**)(void*)&pid) != Success)
4878 if (actual_type_return != XA_CARDINAL)
4889 if (XGetWindowProperty(display, win,
4890 XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
4891 XA_STRING, &actual_type_return, &actual_format_return,
4892 &nitems_return, &bytes_after_return, &prop) != Success)
4894 if (actual_type_return != XA_STRING)
4899 ret = strtonum(prop, 0, UINT_MAX, &errstr);
4900 /* ignore error because strtonum returns 0 anyway */
4907 manage_window(Window id)
4910 struct workspace *ws;
4911 struct ws_win *win, *ww;
4912 int format, i, ws_idx, n, border_me = 0;
4913 unsigned long nitems, bytes;
4914 Atom ws_idx_atom = 0, type;
4915 Atom *prot = NULL, *pp;
4916 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
4917 struct swm_region *r;
4923 if ((win = find_window(id)) != NULL)
4924 return (win); /* already being managed */
4926 /* see if we are on the unmanaged list */
4927 if ((win = find_unmanaged_window(id)) != NULL) {
4928 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
4930 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
4931 if (win->transient) {
4932 set_child_transient(win, &trans);
4933 } if (trans && (ww = find_window(trans)))
4934 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
4936 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
4937 ewmh_update_actions(win);
4941 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
4942 errx(1, "calloc: failed to allocate memory for new window");
4946 /* see if we need to override the workspace */
4947 p = find_pid(window_get_pid(id));
4949 /* Get all the window data in one shot */
4950 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4952 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
4953 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
4955 XGetWindowAttributes(display, id, &win->wa);
4956 XGetWMNormalHints(display, id, &win->sh, &mask);
4957 win->hints = XGetWMHints(display, id);
4958 XGetTransientForHint(display, id, &trans);
4960 win->transient = trans;
4961 set_child_transient(win, &trans);
4962 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
4963 win->id, win->transient);
4966 /* get supported protocols */
4967 if (XGetWMProtocols(display, id, &prot, &n)) {
4968 for (i = 0, pp = prot; i < n; i++, pp++) {
4969 if (*pp == takefocus)
4970 win->take_focus = 1;
4972 win->can_delete = 1;
4978 win->iconic = get_iconic(win);
4981 * Figure out where to put the window. If it was previously assigned to
4982 * a workspace (either by spawn() or manually moving), and isn't
4983 * transient, * put it in the same workspace
4985 r = root_to_region(win->wa.root);
4987 ws = &r->s->ws[p->ws];
4988 TAILQ_REMOVE(&pidlist, p, entry);
4991 } else if (prop && win->transient == 0) {
4992 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
4993 ws_idx = strtonum(prop, 0, 9, &errstr);
4995 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
4998 ws = &r->s->ws[ws_idx];
5001 /* this should launch transients in the same ws as parent */
5003 if ((ww = find_window(trans)) != NULL)
5010 "fix this bug mcbride\n");
5015 /* set up the window layout */
5018 win->s = r->s; /* this never changes */
5019 if (trans && (ww = find_window(trans)))
5020 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
5022 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
5024 win->g.w = win->wa.width;
5025 win->g.h = win->wa.height;
5026 win->g.x = win->wa.x;
5027 win->g.y = win->wa.y;
5028 win->g_floatvalid = 0;
5029 win->floatmaxed = 0;
5030 win->ewmh_flags = 0;
5032 /* Set window properties so we can remember this after reincarnation */
5033 if (ws_idx_atom && prop == NULL &&
5034 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
5035 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
5037 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
5038 PropModeReplace, ws_idx_str, SWM_PROPLEN);
5043 ewmh_autoquirk(win);
5045 if (XGetClassHint(display, win->id, &win->ch)) {
5046 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
5047 win->ch.res_class, win->ch.res_name);
5049 /* java is retarded so treat it special */
5050 if (strstr(win->ch.res_name, "sun-awt")) {
5055 for (i = 0; i < quirks_length; i++){
5056 if (!strcmp(win->ch.res_class, quirks[i].class) &&
5057 !strcmp(win->ch.res_name, quirks[i].name)) {
5058 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
5059 win->ch.res_class, win->ch.res_name);
5060 if (quirks[i].quirk & SWM_Q_FLOAT) {
5064 win->quirks = quirks[i].quirk;
5069 /* alter window position if quirky */
5070 if (win->quirks & SWM_Q_ANYWHERE) {
5071 win->manual = 1; /* don't center the quirky windows */
5072 bzero(&wc, sizeof wc);
5074 if (bar_enabled && win->g.y < bar_height) {
5075 win->g.y = wc.y = bar_height;
5078 if (win->g.w + win->g.x > WIDTH(r)) {
5079 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
5085 /* Reset font sizes (the bruteforce way; no default keybinding). */
5086 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
5087 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5088 fake_keypress(win, XK_KP_Subtract, ShiftMask);
5089 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5090 fake_keypress(win, XK_KP_Add, ShiftMask);
5093 ewmh_get_win_state(win);
5094 ewmh_update_actions(win);
5095 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
5099 bzero(&wc, sizeof wc);
5100 wc.border_width = border_width;
5101 mask = CWBorderWidth;
5102 XConfigureWindow(display, win->id, mask, &wc);
5105 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
5106 PropertyChangeMask | StructureNotifyMask);
5108 /* floaters need to be mapped if they are in the current workspace */
5109 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
5110 XMapRaised(display, win->id);
5116 free_window(struct ws_win *win)
5118 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
5123 /* needed for restart wm */
5124 set_win_state(win, WithdrawnState);
5126 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5128 if (win->ch.res_class)
5129 XFree(win->ch.res_class);
5130 if (win->ch.res_name)
5131 XFree(win->ch.res_name);
5136 memset(win, 0xff, sizeof *win); /* XXX kill later */
5142 unmanage_window(struct ws_win *win)
5144 struct ws_win *parent;
5149 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
5151 if (win->transient) {
5152 parent = find_window(win->transient);
5154 parent->child_trans = NULL;
5157 /* focus on root just in case */
5158 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
5162 TAILQ_REMOVE(&win->ws->winlist, win, entry);
5163 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
5169 focus_magic(struct ws_win *win)
5171 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu\n", WINID(win));
5176 if (win->child_trans) {
5177 /* win = parent & has a transient so focus on that */
5179 focus_win(win->child_trans);
5180 if (win->child_trans->take_focus)
5181 client_msg(win, takefocus);
5183 /* make sure transient hasn't dissapeared */
5184 if (validate_win(win->child_trans) == 0) {
5185 focus_win(win->child_trans);
5186 if (win->child_trans->take_focus)
5187 client_msg(win->child_trans, takefocus);
5189 win->child_trans = NULL;
5191 if (win->take_focus)
5192 client_msg(win, takefocus);
5198 if (win->take_focus)
5199 client_msg(win, takefocus);
5206 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
5214 XKeyEvent *ev = &e->xkey;
5216 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
5218 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
5219 for (i = 0; i < keys_length; i++)
5220 if (keysym == keys[i].keysym
5221 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
5222 && keyfuncs[keys[i].funcid].func) {
5223 if (keys[i].funcid == kf_spawn_custom)
5225 root_to_region(ev->root),
5226 &(keyfuncs[keys[i].funcid].args),
5230 keyfuncs[keys[i].funcid].func(
5231 root_to_region(ev->root),
5232 &(keyfuncs[keys[i].funcid].args)
5238 buttonpress(XEvent *e)
5242 XButtonPressedEvent *ev = &e->xbutton;
5244 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
5246 action = root_click;
5247 if ((win = find_window(ev->window)) == NULL)
5251 action = client_click;
5253 for (i = 0; i < LENGTH(buttons); i++)
5254 if (action == buttons[i].action && buttons[i].func &&
5255 buttons[i].button == ev->button &&
5256 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
5257 buttons[i].func(win, &buttons[i].args);
5261 configurerequest(XEvent *e)
5263 XConfigureRequestEvent *ev = &e->xconfigurerequest;
5268 if ((win = find_window(ev->window)) == NULL)
5269 if ((win = find_unmanaged_window(ev->window)) == NULL)
5273 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
5275 bzero(&wc, sizeof wc);
5278 wc.width = ev->width;
5279 wc.height = ev->height;
5280 wc.border_width = ev->border_width;
5281 wc.sibling = ev->above;
5282 wc.stack_mode = ev->detail;
5283 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
5285 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
5287 config_win(win, ev);
5292 configurenotify(XEvent *e)
5297 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
5298 e->xconfigure.window);
5300 win = find_window(e->xconfigure.window);
5302 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5306 if (focus_mode == SWM_FOCUS_DEFAULT)
5307 drain_enter_notify();
5312 destroynotify(XEvent *e)
5315 XDestroyWindowEvent *ev = &e->xdestroywindow;
5317 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
5319 if ((win = find_window(ev->window)) == NULL) {
5320 if ((win = find_unmanaged_window(ev->window)) == NULL)
5326 /* make sure we focus on something */
5329 unmanage_window(win);
5331 if (focus_mode == SWM_FOCUS_DEFAULT)
5332 drain_enter_notify();
5337 enternotify(XEvent *e)
5339 XCrossingEvent *ev = &e->xcrossing;
5344 Window focus_return;
5345 int revert_to_return;
5347 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
5348 "%lu subwindow %lu same_screen %d focus %d state %d\n",
5349 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
5350 ev->same_screen, ev->focus, ev->state);
5352 switch (focus_mode) {
5353 case SWM_FOCUS_DEFAULT:
5355 case SWM_FOCUS_FOLLOW:
5357 case SWM_FOCUS_SYNERGY:
5360 * all these checks need to be in this order because the
5361 * XCheckTypedWindowEvent relies on weeding out the previous events
5363 * making this code an option would enable a follow mouse for focus
5368 * state is set when we are switching workspaces and focus is set when
5369 * the window or a subwindow already has focus (occurs during restart).
5371 * Only honor the focus flag if last_focus_event is not FocusOut,
5372 * this allows scrotwm to continue to control focus when another
5373 * program is also playing with it.
5375 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
5376 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
5381 * happens when a window is created or destroyed and the border
5382 * crosses the mouse pointer and when switching ws
5384 * we need the subwindow test to see if we came from root in order
5385 * to give focus to floaters
5387 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
5388 ev->subwindow == 0) {
5389 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
5393 /* this window already has focus */
5394 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
5395 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
5399 /* this window is being deleted or moved to another ws */
5400 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
5402 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
5403 XPutBackEvent(display, &cne);
5407 if ((win = find_window(ev->window)) == NULL) {
5408 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5413 * In fullstack kill all enters unless they come from a different ws
5414 * (i.e. another region) or focus has been grabbed externally.
5416 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
5417 last_focus_event != FocusOut) {
5418 XGetInputFocus(display, &focus_return, &revert_to_return);
5419 if ((w = find_window(focus_return)) == NULL ||
5421 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
5429 if ((win = find_window(ev->window)) == NULL) {
5430 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5435 * if we have more enternotifies let them handle it in due time
5437 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
5438 DNPRINTF(SWM_D_EVENT,
5439 "ignoring enternotify: got more enternotify\n");
5440 XPutBackEvent(display, &cne);
5447 /* lets us use one switch statement for arbitrary mode/detail combinations */
5448 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
5451 focusevent(XEvent *e)
5455 u_int32_t mode_detail;
5456 XFocusChangeEvent *ev = &e->xfocus;
5458 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
5459 ev->type == FocusIn ? "entering" : "leaving",
5460 ev->window, ev->mode, ev->detail);
5462 if (last_focus_event == ev->type) {
5463 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
5467 last_focus_event = ev->type;
5468 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
5470 switch (mode_detail) {
5471 /* synergy client focus operations */
5472 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
5473 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
5475 /* synergy server focus operations */
5476 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
5478 /* Entering applications like rdesktop that mangle the pointer */
5479 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
5481 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
5482 XSetWindowBorder(display, win->id,
5483 win->ws->r->s->c[ev->type == FocusIn ?
5484 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
5487 fprintf(stderr, "ignoring focusevent\n");
5488 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
5495 mapnotify(XEvent *e)
5498 XMapEvent *ev = &e->xmap;
5500 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
5502 win = manage_window(ev->window);
5504 set_win_state(win, NormalState);
5508 mappingnotify(XEvent *e)
5510 XMappingEvent *ev = &e->xmapping;
5512 XRefreshKeyboardMapping(ev);
5513 if (ev->request == MappingKeyboard)
5518 maprequest(XEvent *e)
5521 struct swm_region *r;
5522 XWindowAttributes wa;
5523 XMapRequestEvent *ev = &e->xmaprequest;
5525 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
5526 e->xmaprequest.window);
5528 if (!XGetWindowAttributes(display, ev->window, &wa))
5530 if (wa.override_redirect)
5533 win = manage_window(e->xmaprequest.window);
5535 return; /* can't happen */
5539 /* make new win focused */
5540 r = root_to_region(win->wa.root);
5541 if (win->ws == r->ws)
5546 propertynotify(XEvent *e)
5549 XPropertyEvent *ev = &e->xproperty;
5551 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
5554 win = find_window(ev->window);
5558 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
5559 update_iconic(win, 0);
5560 XMapRaised(display, win->id);
5567 case XA_WM_NORMAL_HINTS:
5570 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5571 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
5572 if (win->sh.flags & PMinSize) {
5573 win->g.w = win->sh.min_width;
5574 win->g.h = win->sh.min_height;
5575 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
5577 XMoveResizeWindow(display, win->id,
5578 win->g.x, win->g.y, win->g.w, win->g.h);
5580 if (window_name_enabled)
5589 unmapnotify(XEvent *e)
5593 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
5595 /* determine if we need to help unmanage this window */
5596 win = find_window(e->xunmap.window);
5600 if (getstate(e->xunmap.window) == NormalState) {
5601 unmanage_window(win);
5604 /* giant hack for apps that don't destroy transient windows */
5605 /* eat a bunch of events to prevent remanaging the window */
5607 while (XCheckWindowEvent(display, e->xunmap.window,
5608 EnterWindowMask, &cne))
5610 while (XCheckWindowEvent(display, e->xunmap.window,
5611 StructureNotifyMask, &cne))
5613 while (XCheckWindowEvent(display, e->xunmap.window,
5614 SubstructureNotifyMask, &cne))
5616 /* resend unmap because we ated it */
5617 XUnmapWindow(display, e->xunmap.window);
5620 if (focus_mode == SWM_FOCUS_DEFAULT)
5621 drain_enter_notify();
5625 visibilitynotify(XEvent *e)
5628 struct swm_region *r;
5630 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
5631 e->xvisibility.window);
5632 if (e->xvisibility.state == VisibilityUnobscured)
5633 for (i = 0; i < ScreenCount(display); i++)
5634 TAILQ_FOREACH(r, &screens[i].rl, entry)
5635 if (e->xvisibility.window == r->bar_window)
5640 clientmessage(XEvent *e)
5642 XClientMessageEvent *ev;
5647 win = find_window(ev->window);
5651 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx type: %ld \n",
5652 ev->window, ev->message_type);
5654 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
5655 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW \n");
5658 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
5659 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW \n");
5660 if (win->can_delete)
5661 client_msg(win, adelete);
5663 XKillClient(display, win->id);
5665 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
5666 DNPRINTF(SWM_D_EVENT,
5667 "clientmessage: _NET_MOVERESIZE_WINDOW \n");
5668 if (win->floating) {
5669 if (ev->data.l[0] & (1<<8)) /* x */
5670 win->g.x = ev->data.l[1];
5671 if (ev->data.l[0] & (1<<9)) /* y */
5672 win->g.y = ev->data.l[2];
5673 if (ev->data.l[0] & (1<<10)) /* width */
5674 win->g.w = ev->data.l[3];
5675 if (ev->data.l[0] & (1<<11)) /* height */
5676 win->g.h = ev->data.l[4];
5679 /* TODO: Change stack sizes */
5681 config_win(win, NULL);
5683 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
5684 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE \n");
5685 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
5687 ewmh_update_win_state(win, ev->data.l[2],
5695 xerror_start(Display *d, XErrorEvent *ee)
5702 xerror(Display *d, XErrorEvent *ee)
5704 /* fprintf(stderr, "error: %p %p\n", display, ee); */
5712 xerrorxlib = XSetErrorHandler(xerror_start);
5714 /* this causes an error if some other window manager is running */
5715 XSelectInput(display, DefaultRootWindow(display),
5716 SubstructureRedirectMask);
5717 XSync(display, False);
5721 XSetErrorHandler(xerror);
5722 XSync(display, False);
5727 new_region(struct swm_screen *s, int x, int y, int w, int h)
5729 struct swm_region *r, *n;
5730 struct workspace *ws = NULL;
5733 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
5734 s->idx, w, h, x, y);
5736 /* remove any conflicting regions */
5737 n = TAILQ_FIRST(&s->rl);
5740 n = TAILQ_NEXT(r, entry);
5741 if (X(r) < (x + w) &&
5742 (X(r) + WIDTH(r)) > x &&
5744 (Y(r) + HEIGHT(r)) > y) {
5745 if (r->ws->r != NULL)
5746 r->ws->old_r = r->ws->r;
5748 XDestroyWindow(display, r->bar_window);
5749 TAILQ_REMOVE(&s->rl, r, entry);
5750 TAILQ_INSERT_TAIL(&s->orl, r, entry);
5754 /* search old regions for one to reuse */
5756 /* size + location match */
5757 TAILQ_FOREACH(r, &s->orl, entry)
5758 if (X(r) == x && Y(r) == y &&
5759 HEIGHT(r) == h && WIDTH(r) == w)
5763 TAILQ_FOREACH(r, &s->orl, entry)
5764 if (HEIGHT(r) == h && WIDTH(r) == w)
5768 TAILQ_REMOVE(&s->orl, r, entry);
5769 /* try to use old region's workspace */
5770 if (r->ws->r == NULL)
5773 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
5774 errx(1, "calloc: failed to allocate memory for screen");
5776 /* if we don't have a workspace already, find one */
5778 for (i = 0; i < SWM_WS_MAX; i++)
5779 if (s->ws[i].r == NULL) {
5786 errx(1, "no free workspaces\n");
5797 TAILQ_INSERT_TAIL(&s->rl, r, entry);
5803 #ifdef SWM_XRR_HAS_CRTC
5805 XRRScreenResources *sr;
5808 #endif /* SWM_XRR_HAS_CRTC */
5809 struct swm_region *r;
5812 if (i >= ScreenCount(display))
5813 errx(1, "invalid screen");
5815 /* remove any old regions */
5816 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
5817 r->ws->old_r = r->ws->r = NULL;
5818 XDestroyWindow(display, r->bar_window);
5819 TAILQ_REMOVE(&screens[i].rl, r, entry);
5820 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
5824 /* map virtual screens onto physical screens */
5825 #ifdef SWM_XRR_HAS_CRTC
5826 if (xrandr_support) {
5827 sr = XRRGetScreenResources(display, screens[i].root);
5829 new_region(&screens[i], 0, 0,
5830 DisplayWidth(display, i),
5831 DisplayHeight(display, i));
5835 for (c = 0, ci = NULL; c < ncrtc; c++) {
5836 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
5837 if (ci->noutput == 0)
5840 if (ci != NULL && ci->mode == None)
5841 new_region(&screens[i], 0, 0,
5842 DisplayWidth(display, i),
5843 DisplayHeight(display, i));
5845 new_region(&screens[i],
5846 ci->x, ci->y, ci->width, ci->height);
5849 XRRFreeCrtcInfo(ci);
5850 XRRFreeScreenResources(sr);
5852 #endif /* SWM_XRR_HAS_CRTC */
5854 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
5855 DisplayHeight(display, i));
5860 screenchange(XEvent *e) {
5861 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
5862 struct swm_region *r;
5865 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
5867 if (!XRRUpdateConfiguration(e))
5870 /* silly event doesn't include the screen index */
5871 for (i = 0; i < ScreenCount(display); i++)
5872 if (screens[i].root == xe->root)
5874 if (i >= ScreenCount(display))
5875 errx(1, "screenchange: screen not found\n");
5877 /* brute force for now, just re-enumerate the regions */
5880 /* add bars to all regions */
5881 for (i = 0; i < ScreenCount(display); i++)
5882 TAILQ_FOREACH(r, &screens[i].rl, entry)
5885 if (focus_mode == SWM_FOCUS_DEFAULT)
5886 drain_enter_notify();
5892 Window d1, d2, *wins = NULL;
5893 XWindowAttributes wa;
5898 for (i = 0; i < ScreenCount(display); i++) {
5899 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
5902 /* attach windows to a region */
5903 /* normal windows */
5904 for (j = 0; j < no; j++) {
5905 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5906 wa.override_redirect ||
5907 XGetTransientForHint(display, wins[j], &d1))
5910 state = getstate(wins[j]);
5911 manage = state == IconicState;
5912 if (wa.map_state == IsViewable || manage)
5913 manage_window(wins[j]);
5915 /* transient windows */
5916 for (j = 0; j < no; j++) {
5917 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5918 wa.override_redirect)
5921 state = getstate(wins[j]);
5922 manage = state == IconicState;
5923 if (XGetTransientForHint(display, wins[j], &d1) &&
5925 manage_window(wins[j]);
5938 int errorbase, major, minor;
5939 struct workspace *ws;
5941 if ((screens = calloc(ScreenCount(display),
5942 sizeof(struct swm_screen))) == NULL)
5943 errx(1, "calloc: screens");
5945 /* initial Xrandr setup */
5946 xrandr_support = XRRQueryExtension(display,
5947 &xrandr_eventbase, &errorbase);
5949 if (XRRQueryVersion(display, &major, &minor) && major < 1)
5952 /* map physical screens */
5953 for (i = 0; i < ScreenCount(display); i++) {
5954 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
5956 TAILQ_INIT(&screens[i].rl);
5957 TAILQ_INIT(&screens[i].orl);
5958 screens[i].root = RootWindow(display, i);
5960 /* set default colors */
5961 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
5962 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
5963 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
5964 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
5965 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
5967 /* set default cursor */
5968 XDefineCursor(display, screens[i].root,
5969 XCreateFontCursor(display, XC_left_ptr));
5971 /* init all workspaces */
5972 /* XXX these should be dynamically allocated too */
5973 for (j = 0; j < SWM_WS_MAX; j++) {
5974 ws = &screens[i].ws[j];
5979 TAILQ_INIT(&ws->winlist);
5980 TAILQ_INIT(&ws->unmanagedlist);
5982 for (k = 0; layouts[k].l_stack != NULL; k++)
5983 if (layouts[k].l_config != NULL)
5984 layouts[k].l_config(ws,
5985 SWM_ARG_ID_STACKINIT);
5986 ws->cur_layout = &layouts[0];
5992 XRRSelectInput(display, screens[i].root,
5993 RRScreenChangeNotifyMask);
6000 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
6002 err(1, "setup_globals: strdup");
6003 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
6005 err(1, "setup_globals: strdup");
6006 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
6008 err(1, "setup_globals: strdup");
6009 if ((spawn_term[0] = strdup("xterm")) == NULL)
6010 err(1, "setup_globals: strdup");
6011 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
6019 Atom netwmcheck, netwmname, utf8_string;
6022 /* work around sun jdk bugs, code from wmname */
6023 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
6024 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
6025 utf8_string = XInternAtom(display, "UTF8_STRING", False);
6026 for (i = 0; i < ScreenCount(display); i++) {
6027 root = screens[i].root;
6028 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
6029 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
6030 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
6032 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
6033 PropModeReplace, (unsigned char *)&win,1);
6034 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
6035 PropModeReplace, (unsigned char *)&win,1);
6036 XChangeProperty(display, win, netwmname, utf8_string, 8,
6037 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
6042 main(int argc, char *argv[])
6045 struct swm_region *r, *rr;
6046 struct ws_win *winfocus = NULL;
6049 char conf[PATH_MAX], *cfile = NULL;
6054 struct sigaction sact;
6057 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
6058 SWM_VERSION, cvstag);
6059 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
6060 warnx("no locale support");
6062 if (!(display = XOpenDisplay(0)))
6063 errx(1, "can not open display");
6066 errx(1, "other wm running");
6068 /* handle some signals */
6069 bzero(&sact, sizeof(sact));
6070 sigemptyset(&sact.sa_mask);
6072 sact.sa_handler = sighdlr;
6073 sigaction(SIGINT, &sact, NULL);
6074 sigaction(SIGQUIT, &sact, NULL);
6075 sigaction(SIGTERM, &sact, NULL);
6076 sigaction(SIGHUP, &sact, NULL);
6078 sact.sa_handler = sighdlr;
6079 sact.sa_flags = SA_NOCLDSTOP;
6080 sigaction(SIGCHLD, &sact, NULL);
6082 astate = XInternAtom(display, "WM_STATE", False);
6083 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
6084 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
6085 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
6086 a_wmname = XInternAtom(display, "WM_NAME", False);
6087 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
6088 a_string = XInternAtom(display, "STRING", False);
6089 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
6091 /* look for local and global conf file */
6092 pwd = getpwuid(getuid());
6094 errx(1, "invalid user %d", getuid());
6103 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
6104 if (stat(conf, &sb) != -1) {
6105 if (S_ISREG(sb.st_mode))
6108 /* try global conf file */
6109 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
6110 if (!stat(conf, &sb))
6111 if (S_ISREG(sb.st_mode))
6118 /* set some values to work around bad programs */
6121 /* grab existing windows (before we build the bars) */
6124 if (getenv("SWM_STARTED") == NULL)
6125 setenv("SWM_STARTED", "YES", 1);
6127 /* setup all bars */
6128 for (i = 0; i < ScreenCount(display); i++)
6129 TAILQ_FOREACH(r, &screens[i].rl, entry) {
6130 if (winfocus == NULL)
6131 winfocus = TAILQ_FIRST(&r->ws->winlist);
6139 if (focus_mode == SWM_FOCUS_DEFAULT)
6140 drain_enter_notify();
6142 xfd = ConnectionNumber(display);
6144 while (XPending(display)) {
6145 XNextEvent(display, &e);
6148 if (e.type < LASTEvent) {
6150 if (handler[e.type])
6151 handler[e.type](&e);
6153 DNPRINTF(SWM_D_EVENT,
6154 "win: %lu unknown event: %d\n",
6155 e.xany.window, e.type);
6157 switch (e.type - xrandr_eventbase) {
6158 case RRScreenChangeNotify:
6162 DNPRINTF(SWM_D_EVENT,
6163 "win: %lu unknown xrandr event: "
6164 "%d\n", e.xany.window, e.type);
6170 /* if we are being restarted go focus on first window */
6172 rr = winfocus->ws->r;
6174 /* not a visible window */
6178 /* move pointer to first screen if multi screen */
6179 if (ScreenCount(display) > 1 || outputs > 1)
6180 XWarpPointer(display, None, rr->s[0].root,
6181 0, 0, 0, 0, rr->g.x,
6182 rr->g.y + (bar_enabled ? bar_height : 0));
6184 a.id = SWM_ARG_ID_FOCUSCUR;
6194 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
6196 DNPRINTF(SWM_D_MISC, "select failed");
6197 if (restart_wm == 1)
6198 restart(NULL, NULL);
6199 if (search_resp == 1)
6211 XFreeGC(display, bar_gc);
6212 XCloseDisplay(display);