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 struct layout *cur_layout; /* current layout handlers */
347 struct ws_win *focus; /* may be NULL */
348 struct ws_win *focus_prev; /* may be NULL */
349 struct swm_region *r; /* may be NULL */
350 struct swm_region *old_r; /* may be NULL */
351 struct ws_win_list winlist; /* list of windows in ws */
352 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
356 int horizontal_msize;
358 int horizontal_stacks;
365 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
366 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
368 /* physical screen mapping */
369 #define SWM_WS_MAX (10)
371 int idx; /* screen index */
372 struct swm_region_list rl; /* list of regions on this screen */
373 struct swm_region_list orl; /* list of old regions */
375 struct workspace ws[SWM_WS_MAX];
381 } c[SWM_S_COLOR_MAX];
383 struct swm_screen *screens;
386 /* args to functions */
389 #define SWM_ARG_ID_FOCUSNEXT (0)
390 #define SWM_ARG_ID_FOCUSPREV (1)
391 #define SWM_ARG_ID_FOCUSMAIN (2)
392 #define SWM_ARG_ID_FOCUSCUR (4)
393 #define SWM_ARG_ID_SWAPNEXT (10)
394 #define SWM_ARG_ID_SWAPPREV (11)
395 #define SWM_ARG_ID_SWAPMAIN (12)
396 #define SWM_ARG_ID_MOVELAST (13)
397 #define SWM_ARG_ID_MASTERSHRINK (20)
398 #define SWM_ARG_ID_MASTERGROW (21)
399 #define SWM_ARG_ID_MASTERADD (22)
400 #define SWM_ARG_ID_MASTERDEL (23)
401 #define SWM_ARG_ID_STACKRESET (30)
402 #define SWM_ARG_ID_STACKINIT (31)
403 #define SWM_ARG_ID_CYCLEWS_UP (40)
404 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
405 #define SWM_ARG_ID_CYCLESC_UP (42)
406 #define SWM_ARG_ID_CYCLESC_DOWN (43)
407 #define SWM_ARG_ID_STACKINC (50)
408 #define SWM_ARG_ID_STACKDEC (51)
409 #define SWM_ARG_ID_SS_ALL (60)
410 #define SWM_ARG_ID_SS_WINDOW (61)
411 #define SWM_ARG_ID_DONTCENTER (70)
412 #define SWM_ARG_ID_CENTER (71)
413 #define SWM_ARG_ID_KILLWINDOW (80)
414 #define SWM_ARG_ID_DELETEWINDOW (81)
418 void focus(struct swm_region *, union arg *);
419 void focus_magic(struct ws_win *);
426 #define SWM_Q_FLOAT (1<<0) /* float this window */
427 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
428 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
429 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
430 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
431 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
433 int quirks_size = 0, quirks_length = 0;
434 struct quirk *quirks = NULL;
437 * Supported EWMH hints should be added to
438 * both the enum and the ewmh array
440 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
441 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
442 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
443 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
444 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
445 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
446 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
447 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
448 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
449 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
455 } ewmh[SWM_EWMH_HINT_MAX] = {
456 /* must be in same order as in the enum */
457 {"_NET_ACTIVE_WINDOW", None},
458 {"_NET_MOVERESIZE_WINDOW", None},
459 {"_NET_CLOSE_WINDOW", None},
460 {"_NET_WM_WINDOW_TYPE", None},
461 {"_NET_WM_WINDOW_TYPE_DOCK", None},
462 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
463 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
464 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
465 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
466 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
467 {"_NET_WM_STATE", None},
468 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
469 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
470 {"_NET_WM_STATE_SKIP_TASKBAR", None},
471 {"_NET_WM_STATE_SKIP_PAGER", None},
472 {"_NET_WM_STATE_HIDDEN", None},
473 {"_NET_WM_STATE_ABOVE", None},
474 {"_SWM_WM_STATE_MANUAL", None},
475 {"_NET_WM_STATE_FULLSCREEN", None},
476 {"_NET_WM_ALLOWED_ACTIONS", None},
477 {"_NET_WM_ACTION_MOVE", None},
478 {"_NET_WM_ACTION_RESIZE", None},
479 {"_NET_WM_ACTION_FULLSCREEN", None},
480 {"_NET_WM_ACTION_CLOSE", None},
483 void store_float_geom(struct ws_win *win, struct swm_region *r);
484 int floating_toggle_win(struct ws_win *win);
485 void spawn_select(struct swm_region *, union arg *, char *, int *);
488 get_property(Window id, Atom atom, long count, Atom type,
489 unsigned long *n, unsigned char **data)
492 unsigned long tmp, extra;
493 unsigned long *nitems;
496 nitems = n != NULL ? n : &tmp;
497 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
498 &real, &format, nitems, &extra, data);
500 if (status != Success)
509 update_iconic(struct ws_win *win, int newv)
516 iprop = XInternAtom(display, "_SWM_ICONIC", False);
520 XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
521 PropModeReplace, (unsigned char *)&v, 1);
523 XDeleteProperty(display, win->id, iprop);
527 get_iconic(struct ws_win *win)
532 unsigned long nitems, extra;
533 unsigned char *prop = NULL;
535 iprop = XInternAtom(display, "_SWM_ICONIC", False);
538 status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
539 False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
540 if (status != Success)
542 if (rettype != XA_INTEGER || retfmt != 32)
546 v = *((int32_t *)prop);
560 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
562 for (i = 0; i < LENGTH(ewmh); i++)
563 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
565 for (i = 0; i < ScreenCount(display); i++) {
566 /* Support check window will be created by workaround(). */
568 /* Report supported atoms */
569 XDeleteProperty(display, screens[i].root, sup_list);
570 for (j = 0; j < LENGTH(ewmh); j++)
571 XChangeProperty(display, screens[i].root,
572 sup_list, XA_ATOM, 32,
573 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
581 unsigned char *data = NULL;
583 Atom sup_check, sup_list;
586 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
587 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
589 for (i = 0; i < ScreenCount(display); i++) {
590 /* Get the support check window and destroy it */
591 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
596 XDestroyWindow(display, id);
597 XDeleteProperty(display, screens[i].root, sup_check);
598 XDeleteProperty(display, screens[i].root, sup_list);
606 ewmh_autoquirk(struct ws_win *win)
609 unsigned long *data = NULL;
613 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
614 XA_ATOM, &n, (unsigned char **)&data);
621 for (i = 0; i < n; i++) {
623 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
625 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
626 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
627 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
629 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
632 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
633 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
635 win->quirks = SWM_Q_FLOAT;
643 #define SWM_EWMH_ACTION_COUNT_MAX (6)
644 #define EWMH_F_FULLSCREEN (1<<0)
645 #define EWMH_F_ABOVE (1<<1)
646 #define EWMH_F_HIDDEN (1<<2)
647 #define EWMH_F_SKIP_PAGER (1<<3)
648 #define EWMH_F_SKIP_TASKBAR (1<<4)
649 #define SWM_F_MANUAL (1<<5)
652 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
654 struct swm_geometry rg;
662 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
668 store_float_geom(win, win->ws->r);
675 if (win->g_floatvalid) {
676 /* refloat at last floating relative position */
677 win->g.x = win->g_float.x - win->rg_float.x + rg.x;
678 win->g.y = win->g_float.y - win->rg_float.y + rg.y;
679 win->g.w = win->g_float.w;
680 win->g.h = win->g_float.h;
688 ewmh_update_actions(struct ws_win *win)
690 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
696 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
699 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
700 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
703 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
704 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
707 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
708 #define _NET_WM_STATE_ADD 1 /* add/set property */
709 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
712 ewmh_update_win_state(struct ws_win *win, long state, long action)
714 unsigned int mask = 0;
715 unsigned int changed = 0;
716 unsigned int orig_flags;
721 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
722 mask = EWMH_F_FULLSCREEN;
723 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
725 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
727 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
728 mask = EWMH_F_SKIP_PAGER;
729 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
730 mask = EWMH_F_SKIP_TASKBAR;
733 orig_flags = win->ewmh_flags;
736 case _NET_WM_STATE_REMOVE:
737 win->ewmh_flags &= ~mask;
739 case _NET_WM_STATE_ADD:
740 win->ewmh_flags |= mask;
742 case _NET_WM_STATE_TOGGLE:
743 win->ewmh_flags ^= mask;
747 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
749 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
751 if (!floating_toggle_win(win))
752 win->ewmh_flags = orig_flags; /* revert */
753 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
755 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
756 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
758 if (!ewmh_set_win_fullscreen(win,
759 win->ewmh_flags & EWMH_F_FULLSCREEN))
760 win->ewmh_flags = orig_flags; /* revert */
762 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
764 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
765 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
766 XA_ATOM, 32, PropModeAppend,
767 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
768 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
769 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
770 XA_ATOM, 32, PropModeAppend,
771 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
772 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
773 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
774 XA_ATOM, 32, PropModeAppend,
775 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
776 if (win->ewmh_flags & EWMH_F_ABOVE)
777 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
778 XA_ATOM, 32, PropModeAppend,
779 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
780 if (win->ewmh_flags & SWM_F_MANUAL)
781 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
782 XA_ATOM, 32, PropModeAppend,
783 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
787 ewmh_get_win_state(struct ws_win *win)
798 win->ewmh_flags |= EWMH_F_ABOVE;
800 win->ewmh_flags |= SWM_F_MANUAL;
802 success = get_property(win->id, ewmh[_NET_WM_STATE].atom,
803 (~0L), XA_ATOM, &n, (unsigned char **)&states);
808 for (i = 0; i < n; i++)
809 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
829 name = "ButtonPress";
832 name = "ButtonRelease";
835 name = "MotionNotify";
838 name = "EnterNotify";
841 name = "LeaveNotify";
850 name = "KeymapNotify";
856 name = "GraphicsExpose";
861 case VisibilityNotify:
862 name = "VisibilityNotify";
865 name = "CreateNotify";
868 name = "DestroyNotify";
871 name = "UnmapNotify";
880 name = "ReparentNotify";
882 case ConfigureNotify:
883 name = "ConfigureNotify";
885 case ConfigureRequest:
886 name = "ConfigureRequest";
889 name = "GravityNotify";
892 name = "ResizeRequest";
894 case CirculateNotify:
895 name = "CirculateNotify";
897 case CirculateRequest:
898 name = "CirculateRequest";
901 name = "PropertyNotify";
904 name = "SelectionClear";
906 case SelectionRequest:
907 name = "SelectionRequest";
909 case SelectionNotify:
910 name = "SelectionNotify";
913 name = "ColormapNotify";
916 name = "ClientMessage";
919 name = "MappingNotify";
924 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
926 e->xany.window, name, e->type, QLength(display));
928 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
930 e->xany.window, e->type, QLength(display));
934 dumpwins(struct swm_region *r, union arg *args)
938 XWindowAttributes wa;
941 fprintf(stderr, "invalid workspace\n");
945 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
947 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
948 state = getstate(win->id);
949 if (!XGetWindowAttributes(display, win->id, &wa))
950 fprintf(stderr, "window: %lu failed "
951 "XGetWindowAttributes\n", win->id);
952 fprintf(stderr, "window: %lu map_state: %d state: %d "
954 win->id, wa.map_state, state, win->transient);
957 fprintf(stderr, "===== unmanaged window list =====\n");
958 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
959 state = getstate(win->id);
960 if (!XGetWindowAttributes(display, win->id, &wa))
961 fprintf(stderr, "window: %lu failed "
962 "XGetWindowAttributes\n", win->id);
963 fprintf(stderr, "window: %lu map_state: %d state: %d "
965 win->id, wa.map_state, state, win->transient);
968 fprintf(stderr, "=================================\n");
973 dumpwins(struct swm_region *r, union arg *args)
976 #endif /* SWM_DEBUG */
978 void expose(XEvent *);
979 void keypress(XEvent *);
980 void buttonpress(XEvent *);
981 void configurerequest(XEvent *);
982 void configurenotify(XEvent *);
983 void destroynotify(XEvent *);
984 void enternotify(XEvent *);
985 void focusevent(XEvent *);
986 void mapnotify(XEvent *);
987 void mappingnotify(XEvent *);
988 void maprequest(XEvent *);
989 void propertynotify(XEvent *);
990 void unmapnotify(XEvent *);
991 void visibilitynotify(XEvent *);
992 void clientmessage(XEvent *);
994 void (*handler[LASTEvent])(XEvent *) = {
996 [KeyPress] = keypress,
997 [ButtonPress] = buttonpress,
998 [ConfigureRequest] = configurerequest,
999 [ConfigureNotify] = configurenotify,
1000 [DestroyNotify] = destroynotify,
1001 [EnterNotify] = enternotify,
1002 [FocusIn] = focusevent,
1003 [FocusOut] = focusevent,
1004 [MapNotify] = mapnotify,
1005 [MappingNotify] = mappingnotify,
1006 [MapRequest] = maprequest,
1007 [PropertyNotify] = propertynotify,
1008 [UnmapNotify] = unmapnotify,
1009 [VisibilityNotify] = visibilitynotify,
1010 [ClientMessage] = clientmessage,
1016 int saved_errno, status;
1019 saved_errno = errno;
1023 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1028 if (errno != ECHILD)
1029 warn("sighdlr: waitpid");
1030 #endif /* SWM_DEBUG */
1033 if (pid == searchpid)
1037 if (WIFEXITED(status)) {
1038 if (WEXITSTATUS(status) != 0)
1039 warnx("sighdlr: child exit status: %d",
1040 WEXITSTATUS(status));
1042 warnx("sighdlr: child is terminated "
1044 #endif /* SWM_DEBUG */
1058 errno = saved_errno;
1064 struct pid_e *p = NULL;
1066 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1071 TAILQ_FOREACH(p, &pidlist, entry) {
1080 name_to_color(char *colorname)
1084 XColor screen_def, exact_def;
1085 unsigned long result = 0;
1086 char cname[32] = "#";
1088 cmap = DefaultColormap(display, screens[0].idx);
1089 status = XAllocNamedColor(display, cmap, colorname,
1090 &screen_def, &exact_def);
1092 strlcat(cname, colorname + 2, sizeof cname - 1);
1093 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1097 result = screen_def.pixel;
1099 fprintf(stderr, "color '%s' not found.\n", colorname);
1105 setscreencolor(char *val, int i, int c)
1107 if (i > 0 && i <= ScreenCount(display)) {
1108 screens[i - 1].c[c].color = name_to_color(val);
1109 free(screens[i - 1].c[c].name);
1110 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1112 } else if (i == -1) {
1113 for (i = 0; i < ScreenCount(display); i++) {
1114 screens[i].c[c].color = name_to_color(val);
1115 free(screens[i].c[c].name);
1116 if ((screens[i].c[c].name = strdup(val)) == NULL)
1120 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1121 i, ScreenCount(display));
1125 custom_region(char *val)
1127 unsigned int sidx, x, y, w, h;
1129 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1130 errx(1, "invalid custom region, "
1131 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
1132 if (sidx < 1 || sidx > ScreenCount(display))
1133 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1134 sidx, ScreenCount(display));
1138 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
1140 if (x < 0 || x > DisplayWidth(display, sidx) ||
1141 y < 0 || y > DisplayHeight(display, sidx) ||
1142 w + x > DisplayWidth(display, sidx) ||
1143 h + y > DisplayHeight(display, sidx)) {
1144 fprintf(stderr, "ignoring region %ux%u+%u+%u "
1145 "- not within screen boundaries "
1146 "(%ux%u)\n", w, h, x, y,
1147 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1151 new_region(&screens[sidx], x, y, w, h);
1155 socket_setnonblock(int fd)
1159 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1160 err(1, "fcntl F_GETFL");
1161 flags |= O_NONBLOCK;
1162 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1163 err(1, "fcntl F_SETFL");
1167 bar_print(struct swm_region *r, char *s)
1169 XClearWindow(display, r->bar_window);
1170 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
1171 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
1176 bar_extra_stop(void)
1180 bzero(bar_pipe, sizeof bar_pipe);
1183 kill(bar_pid, SIGTERM);
1186 strlcpy(bar_ext, "", sizeof bar_ext);
1191 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1193 int do_class, do_name;
1195 XClassHint *xch = NULL;
1197 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
1198 cur_focus != NULL) {
1199 if ((xch = XAllocClassHint()) == NULL)
1201 status = XGetClassHint(display, cur_focus->id, xch);
1202 if (status == BadWindow || status == BadAlloc)
1204 do_class = (title_class_enabled && xch->res_class != NULL);
1205 do_name = (title_name_enabled && xch->res_name != NULL);
1207 strlcat(s, xch->res_class, sz);
1208 if (do_class && do_name)
1209 strlcat(s, ":", sz);
1211 strlcat(s, xch->res_name, sz);
1212 strlcat(s, " ", sz);
1220 bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1224 if (window_name_enabled && cur_focus != NULL) {
1225 XFetchName(display, cur_focus->id, &title);
1227 if (cur_focus->floating)
1228 strlcat(s, "(f) ", sz);
1229 strlcat(s, title, sz);
1230 strlcat(s, " ", sz);
1241 struct swm_region *r;
1244 char s[SWM_BAR_MAX];
1245 char cn[SWM_BAR_MAX];
1246 char loc[SWM_BAR_MAX];
1250 if (bar_enabled == 0)
1252 if (bar_extra && bar_extra_running) {
1253 /* ignore short reads; it'll correct itself */
1254 while ((b = fgetln(stdin, &len)) != NULL)
1255 if (b && b[len - 1] == '\n') {
1257 strlcpy(bar_ext, b, sizeof bar_ext);
1259 if (b == NULL && errno != EAGAIN) {
1260 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
1261 errno, strerror(errno));
1265 strlcpy(bar_ext, "", sizeof bar_ext);
1267 if (clock_enabled == 0)
1268 strlcpy(s, "", sizeof s);
1271 localtime_r(&tmt, &tm);
1272 strftime(s, sizeof s, clock_format, &tm);
1273 strlcat(s, " ", sizeof s);
1276 for (i = 0; i < ScreenCount(display); i++) {
1278 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1279 strlcpy(cn, "", sizeof cn);
1281 bar_class_name(cn, sizeof cn, r->ws->focus);
1282 bar_window_name(cn, sizeof cn, r->ws->focus);
1286 stack = r->ws->cur_layout->name;
1288 snprintf(loc, sizeof loc, "%d:%d %s %s%s %s %s",
1289 x++, r->ws->idx + 1, stack, s, cn, bar_ext,
1304 bar_toggle(struct swm_region *r, union arg *args)
1306 struct swm_region *tmpr;
1307 int i, sc = ScreenCount(display);
1309 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
1312 for (i = 0; i < sc; i++)
1313 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1314 XUnmapWindow(display, tmpr->bar_window);
1316 for (i = 0; i < sc; i++)
1317 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1318 XMapRaised(display, tmpr->bar_window);
1320 bar_enabled = !bar_enabled;
1323 /* must be after stack */
1330 XSetWindowAttributes wa;
1331 struct swm_region *r;
1334 /* do this here because the conf file is in memory */
1335 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1336 /* launch external status app */
1337 bar_extra_running = 1;
1338 if (pipe(bar_pipe) == -1)
1339 err(1, "pipe error");
1340 socket_setnonblock(bar_pipe[0]);
1341 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1342 if (dup2(bar_pipe[0], 0) == -1)
1344 if (dup2(bar_pipe[1], 1) == -1)
1346 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1347 err(1, "could not disable SIGPIPE");
1348 switch (bar_pid = fork()) {
1350 err(1, "cannot fork");
1354 execvp(bar_argv[0], bar_argv);
1355 err(1, "%s external app failed", bar_argv[0]);
1357 default: /* parent */
1363 bzero(&wa, sizeof wa);
1364 for (i = 0; i < ScreenCount(display); i++)
1365 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1367 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1368 wa.background_pixel =
1369 screens[i].c[SWM_S_COLOR_BAR].color;
1370 XChangeWindowAttributes(display, r->bar_window,
1371 CWBackPixel | CWBorderPixel, &wa);
1377 bar_setup(struct swm_region *r)
1382 XFreeFont(display, bar_fs);
1386 for (i = 0; bar_fonts[i] != NULL; i++) {
1387 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
1393 if (bar_fonts[i] == NULL)
1394 errx(1, "couldn't load font");
1396 errx(1, "couldn't create font structure");
1398 bar_height = bar_fs->ascent + bar_fs->descent + 1 +
1399 2 * bar_border_width;
1401 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1403 r->bar_window = XCreateSimpleWindow(display,
1404 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width,
1405 bar_height - 2 * bar_border_width,
1406 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1407 r->s->c[SWM_S_COLOR_BAR].color);
1408 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
1409 XSetFont(display, bar_gc, bar_fs->fid);
1410 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1412 XMapRaised(display, r->bar_window);
1413 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
1415 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1416 err(1, "could not install bar_signal");
1421 drain_enter_notify(void)
1426 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1429 DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained %d\n", i);
1433 set_win_state(struct ws_win *win, long state)
1435 long data[] = {state, None};
1437 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1442 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1443 (unsigned char *)data, 2);
1450 unsigned char *p = NULL;
1453 if (!get_property(w, astate, 2L, astate, &n, &p))
1456 result = *((long *)p);
1462 version(struct swm_region *r, union arg *args)
1464 bar_version = !bar_version;
1466 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
1467 SWM_VERSION, cvstag);
1469 strlcpy(bar_vertext, "", sizeof bar_vertext);
1474 client_msg(struct ws_win *win, Atom a)
1476 XClientMessageEvent cm;
1481 bzero(&cm, sizeof cm);
1482 cm.type = ClientMessage;
1483 cm.window = win->id;
1484 cm.message_type = aprot;
1487 cm.data.l[1] = CurrentTime;
1488 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1492 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1500 DNPRINTF(SWM_D_MISC,
1501 "config_win: win %lu x %d y %d w %d h %d\n",
1502 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1504 ce.type = ConfigureNotify;
1505 ce.display = display;
1507 ce.window = win->id;
1510 ce.width = win->g.w;
1511 ce.height = win->g.h;
1512 ce.border_width = border_width;
1514 ce.override_redirect = False;
1516 DNPRINTF(SWM_D_MISC,
1517 "config_win: ev win %lu x %d y %d w %d h %d\n",
1518 ev->window, ev->x, ev->y, ev->width, ev->height);
1519 ce.type = ConfigureNotify;
1520 ce.display = ev->display;
1521 ce.event = ev->window;
1522 ce.window = ev->window;
1525 ce.width = ev->width;
1526 ce.height = ev->height;
1527 ce.border_width = ev->border_width;
1528 ce.above = ev->above;
1529 ce.override_redirect = False;
1532 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1536 count_win(struct workspace *ws, int count_transient)
1541 TAILQ_FOREACH(win, &ws->winlist, entry) {
1542 if (count_transient == 0 && win->floating)
1544 if (count_transient == 0 && win->transient)
1550 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1556 quit(struct swm_region *r, union arg *args)
1558 DNPRINTF(SWM_D_MISC, "quit\n");
1563 unmap_window(struct ws_win *win)
1568 /* don't unmap again */
1569 if (getstate(win->id) == IconicState)
1572 set_win_state(win, IconicState);
1574 XUnmapWindow(display, win->id);
1575 XSetWindowBorder(display, win->id,
1576 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1585 for (i = 0; i < ScreenCount(display); i++)
1586 for (j = 0; j < SWM_WS_MAX; j++)
1587 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1592 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1599 event.display = display; /* Ignored, but what the hell */
1600 event.window = win->id;
1601 event.root = win->s->root;
1602 event.subwindow = None;
1603 event.time = CurrentTime;
1608 event.same_screen = True;
1609 event.keycode = XKeysymToKeycode(display, keysym);
1610 event.state = modifiers;
1612 event.type = KeyPress;
1613 XSendEvent(event.display, event.window, True,
1614 KeyPressMask, (XEvent *)&event);
1616 event.type = KeyRelease;
1617 XSendEvent(event.display, event.window, True,
1618 KeyPressMask, (XEvent *)&event);
1623 restart(struct swm_region *r, union arg *args)
1625 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1627 /* disable alarm because the following code may not be interrupted */
1629 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1630 errx(1, "can't disable alarm");
1635 XCloseDisplay(display);
1636 execvp(start_argv[0], start_argv);
1637 fprintf(stderr, "execvp failed\n");
1643 root_to_region(Window root)
1645 struct swm_region *r = NULL;
1647 int i, x, y, wx, wy;
1650 for (i = 0; i < ScreenCount(display); i++)
1651 if (screens[i].root == root)
1654 if (XQueryPointer(display, screens[i].root,
1655 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1656 /* choose a region based on pointer location */
1657 TAILQ_FOREACH(r, &screens[i].rl, entry)
1658 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1659 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1664 r = TAILQ_FIRST(&screens[i].rl);
1670 find_unmanaged_window(Window id)
1675 for (i = 0; i < ScreenCount(display); i++)
1676 for (j = 0; j < SWM_WS_MAX; j++)
1677 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1685 find_window(Window id)
1688 Window wrr, wpr, *wcr = NULL;
1692 for (i = 0; i < ScreenCount(display); i++)
1693 for (j = 0; j < SWM_WS_MAX; j++)
1694 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1698 /* if we were looking for the parent return that window instead */
1699 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1704 /* ignore not found and root */
1705 if (wpr == 0 || wrr == wpr)
1708 /* look for parent */
1709 for (i = 0; i < ScreenCount(display); i++)
1710 for (j = 0; j < SWM_WS_MAX; j++)
1711 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1719 spawn(int ws_idx, union arg *args, int close_fd)
1724 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1727 close(ConnectionNumber(display));
1729 setenv("LD_PRELOAD", SWM_LIB, 1);
1731 if (asprintf(&ret, "%d", ws_idx) == -1) {
1735 setenv("_SWM_WS", ret, 1);
1739 if (asprintf(&ret, "%d", getpid()) == -1) {
1743 setenv("_SWM_PID", ret, 1);
1747 if (setsid() == -1) {
1754 * close stdin and stdout to prevent interaction between apps
1755 * and the baraction script
1756 * leave stderr open to record errors
1758 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
1762 dup2(fd, STDIN_FILENO);
1763 dup2(fd, STDOUT_FILENO);
1768 execvp(args->argv[0], args->argv);
1775 spawnterm(struct swm_region *r, union arg *args)
1777 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1781 setenv("_SWM_XTERM_FONTADJ", "", 1);
1782 spawn(r->ws->idx, args, 1);
1787 kill_refs(struct ws_win *win)
1790 struct swm_region *r;
1791 struct workspace *ws;
1796 for (i = 0; i < ScreenCount(display); i++)
1797 TAILQ_FOREACH(r, &screens[i].rl, entry)
1798 for (x = 0; x < SWM_WS_MAX; x++) {
1800 if (win == ws->focus)
1802 if (win == ws->focus_prev)
1803 ws->focus_prev = NULL;
1808 validate_win(struct ws_win *testwin)
1811 struct workspace *ws;
1812 struct swm_region *r;
1813 int i, x, foundit = 0;
1815 if (testwin == NULL)
1818 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1819 TAILQ_FOREACH(r, &screens[i].rl, entry)
1820 for (x = 0; x < SWM_WS_MAX; x++) {
1822 TAILQ_FOREACH(win, &ws->winlist, entry)
1830 validate_ws(struct workspace *testws)
1832 struct swm_region *r;
1833 struct workspace *ws;
1836 /* validate all ws */
1837 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1838 TAILQ_FOREACH(r, &screens[i].rl, entry)
1839 for (x = 0; x < SWM_WS_MAX; x++) {
1848 unfocus_win(struct ws_win *win)
1853 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1857 if (win->ws == NULL)
1860 if (validate_ws(win->ws))
1861 return; /* XXX this gets hit with thunderbird, needs fixing */
1863 if (win->ws->r == NULL)
1866 if (validate_win(win)) {
1871 if (win->ws->focus == win) {
1872 win->ws->focus = NULL;
1873 win->ws->focus_prev = win;
1876 if (validate_win(win->ws->focus)) {
1877 kill_refs(win->ws->focus);
1878 win->ws->focus = NULL;
1880 if (validate_win(win->ws->focus_prev)) {
1881 kill_refs(win->ws->focus_prev);
1882 win->ws->focus_prev = NULL;
1885 /* drain all previous unfocus events */
1886 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
1889 grabbuttons(win, 0);
1890 XSetWindowBorder(display, win->id,
1891 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1893 XChangeProperty(display, win->s->root,
1894 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1895 PropModeReplace, (unsigned char *)&none,1);
1904 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
1906 for (i = 0; i < ScreenCount(display); i++)
1907 for (j = 0; j < SWM_WS_MAX; j++)
1908 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1913 focus_win(struct ws_win *win)
1918 struct ws_win *cfw = NULL;
1921 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1925 if (win->ws == NULL)
1928 if (validate_ws(win->ws))
1929 return; /* XXX this gets hit with thunderbird, needs fixing */
1931 if (validate_win(win)) {
1936 if (validate_win(win)) {
1941 XGetInputFocus(display, &cur_focus, &rr);
1942 if ((cfw = find_window(cur_focus)) != NULL)
1945 /* use larger hammer since the window was killed somehow */
1946 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
1947 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
1948 XSetWindowBorder(display, cfw->id,
1949 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1952 win->ws->focus = win;
1954 if (win->ws->r != NULL) {
1955 /* drain all previous focus events */
1956 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
1960 XSetInputFocus(display, win->id,
1961 RevertToParent, CurrentTime);
1962 XMapRaised(display, win->id);
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 XMapRaised(display, win->id);
1969 XChangeProperty(display, win->s->root,
1970 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1971 PropModeReplace, (unsigned char *)&win->id,1);
1974 if (window_name_enabled)
1979 switchws(struct swm_region *r, union arg *args)
1981 int wsid = args->id, unmap_old = 0;
1982 struct swm_region *this_r, *other_r;
1984 struct workspace *new_ws, *old_ws;
1991 old_ws = this_r->ws;
1992 new_ws = &this_r->s->ws[wsid];
1994 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1995 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1998 if (new_ws == NULL || old_ws == NULL)
2000 if (new_ws == old_ws)
2003 other_r = new_ws->r;
2004 if (other_r == NULL) {
2005 /* the other workspace is hidden, hide this one */
2009 /* the other ws is visible in another region, exchange them */
2010 other_r->ws_prior = new_ws;
2011 other_r->ws = old_ws;
2012 old_ws->r = other_r;
2014 this_r->ws_prior = old_ws;
2015 this_r->ws = new_ws;
2018 /* this is needed so that we can click on a window after a restart */
2022 a.id = SWM_ARG_ID_FOCUSCUR;
2023 focus(new_ws->r, &a);
2027 /* unmap old windows */
2029 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2032 if (focus_mode == SWM_FOCUS_DEFAULT)
2033 drain_enter_notify();
2037 cyclews(struct swm_region *r, union arg *args)
2040 struct swm_screen *s = r->s;
2042 DNPRINTF(SWM_D_WS, "cyclews id %d "
2043 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2044 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2049 case SWM_ARG_ID_CYCLEWS_UP:
2050 if (a.id < SWM_WS_MAX - 1)
2055 case SWM_ARG_ID_CYCLEWS_DOWN:
2059 a.id = SWM_WS_MAX - 1;
2065 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
2067 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2071 } while (a.id != r->ws->idx);
2075 priorws(struct swm_region *r, union arg *args)
2079 DNPRINTF(SWM_D_WS, "priorws id %d "
2080 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2081 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2083 if (r->ws_prior == NULL)
2086 a.id = r->ws_prior->idx;
2091 cyclescr(struct swm_region *r, union arg *args)
2093 struct swm_region *rr = NULL;
2097 /* do nothing if we don't have more than one screen */
2098 if (!(ScreenCount(display) > 1 || outputs > 1))
2103 case SWM_ARG_ID_CYCLESC_UP:
2104 rr = TAILQ_NEXT(r, entry);
2106 rr = TAILQ_FIRST(&screens[i].rl);
2108 case SWM_ARG_ID_CYCLESC_DOWN:
2109 rr = TAILQ_PREV(r, swm_region_list, entry);
2111 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2119 /* move mouse to region */
2121 y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
2122 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2124 a.id = SWM_ARG_ID_FOCUSCUR;
2127 if (rr->ws->focus) {
2128 /* move to focus window */
2129 x = rr->ws->focus->g.x + 1;
2130 y = rr->ws->focus->g.y + 1;
2131 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2136 sort_windows(struct ws_win_list *wl)
2138 struct ws_win *win, *parent, *nxt;
2143 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2144 nxt = TAILQ_NEXT(win, entry);
2145 if (win->transient) {
2146 parent = find_window(win->transient);
2147 if (parent == NULL) {
2148 fprintf(stderr, "not possible bug\n");
2151 TAILQ_REMOVE(wl, win, entry);
2152 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2159 swapwin(struct swm_region *r, union arg *args)
2161 struct ws_win *target, *source;
2162 struct ws_win *cur_focus;
2163 struct ws_win_list *wl;
2166 DNPRINTF(SWM_D_WS, "swapwin id %d "
2167 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
2168 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2170 cur_focus = r->ws->focus;
2171 if (cur_focus == NULL)
2175 wl = &source->ws->winlist;
2178 case SWM_ARG_ID_SWAPPREV:
2179 if (source->transient)
2180 source = find_window(source->transient);
2181 target = TAILQ_PREV(source, ws_win_list, entry);
2182 if (target && target->transient)
2183 target = find_window(target->transient);
2184 TAILQ_REMOVE(wl, source, entry);
2186 TAILQ_INSERT_TAIL(wl, source, entry);
2188 TAILQ_INSERT_BEFORE(target, source, entry);
2190 case SWM_ARG_ID_SWAPNEXT:
2191 target = TAILQ_NEXT(source, entry);
2192 /* move the parent and let the sort handle the move */
2193 if (source->transient)
2194 source = find_window(source->transient);
2195 TAILQ_REMOVE(wl, source, entry);
2197 TAILQ_INSERT_HEAD(wl, source, entry);
2199 TAILQ_INSERT_AFTER(wl, target, source, entry);
2201 case SWM_ARG_ID_SWAPMAIN:
2202 target = TAILQ_FIRST(wl);
2203 if (target == source) {
2204 if (source->ws->focus_prev != NULL &&
2205 source->ws->focus_prev != target)
2207 source = source->ws->focus_prev;
2211 if (target == NULL || source == NULL)
2213 source->ws->focus_prev = target;
2214 TAILQ_REMOVE(wl, target, entry);
2215 TAILQ_INSERT_BEFORE(source, target, entry);
2216 TAILQ_REMOVE(wl, source, entry);
2217 TAILQ_INSERT_HEAD(wl, source, entry);
2219 case SWM_ARG_ID_MOVELAST:
2220 TAILQ_REMOVE(wl, source, entry);
2221 TAILQ_INSERT_TAIL(wl, source, entry);
2224 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
2234 focus_prev(struct ws_win *win)
2236 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2237 struct ws_win *cur_focus = NULL;
2238 struct ws_win_list *wl = NULL;
2239 struct workspace *ws = NULL;
2241 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
2243 if (!(win && win->ws))
2248 cur_focus = ws->focus;
2249 winlostfocus = cur_focus;
2251 /* pickle, just focus on whatever */
2252 if (cur_focus == NULL) {
2253 /* use prev_focus if valid */
2254 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2255 find_window(WINID(ws->focus_prev)))
2256 winfocus = ws->focus_prev;
2257 if (winfocus == NULL)
2258 winfocus = TAILQ_FIRST(wl);
2262 /* if transient focus on parent */
2263 if (cur_focus->transient) {
2264 winfocus = find_window(cur_focus->transient);
2268 /* if in max_stack try harder */
2269 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2270 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2271 if (cur_focus != ws->focus_prev)
2272 winfocus = ws->focus_prev;
2273 else if (cur_focus != ws->focus)
2274 winfocus = ws->focus;
2276 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2281 if (cur_focus == win)
2282 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2283 if (winfocus == NULL)
2284 winfocus = TAILQ_LAST(wl, ws_win_list);
2285 if (winfocus == NULL || winfocus == win)
2286 winfocus = TAILQ_NEXT(cur_focus, entry);
2288 if (winfocus == winlostfocus || winfocus == NULL)
2291 focus_magic(winfocus);
2295 focus(struct swm_region *r, union arg *args)
2297 struct ws_win *winfocus = NULL, *winlostfocus = NULL, *head;
2298 struct ws_win *cur_focus = NULL;
2299 struct ws_win_list *wl = NULL;
2300 struct workspace *ws = NULL;
2305 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
2307 /* treat FOCUS_CUR special */
2308 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2309 if (r->ws->focus && r->ws->focus->iconic == 0)
2310 winfocus = r->ws->focus;
2311 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2312 winfocus = r->ws->focus_prev;
2314 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2315 if (winfocus->iconic == 0)
2318 focus_magic(winfocus);
2322 if ((cur_focus = r->ws->focus) == NULL)
2327 winlostfocus = cur_focus;
2330 case SWM_ARG_ID_FOCUSPREV:
2331 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2333 head = TAILQ_LAST(wl, ws_win_list);
2335 if (WINID(winfocus) == cur_focus->transient) {
2336 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2338 head = TAILQ_LAST(wl, ws_win_list);
2343 if (winfocus && winfocus->iconic) {
2344 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
2345 if (winfocus->iconic == 0)
2350 case SWM_ARG_ID_FOCUSNEXT:
2351 head = TAILQ_NEXT(cur_focus, entry);
2353 head = TAILQ_FIRST(wl);
2357 if (winfocus && winfocus->iconic) {
2358 TAILQ_FOREACH(winfocus, wl, entry)
2359 if (winfocus->iconic == 0)
2364 case SWM_ARG_ID_FOCUSMAIN:
2365 winfocus = TAILQ_FIRST(wl);
2366 if (winfocus == cur_focus)
2367 winfocus = cur_focus->ws->focus_prev;
2373 if (winfocus == winlostfocus || winfocus == NULL)
2376 focus_magic(winfocus);
2380 cycle_layout(struct swm_region *r, union arg *args)
2382 struct workspace *ws = r->ws;
2383 struct ws_win *winfocus;
2386 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2388 winfocus = ws->focus;
2391 if (ws->cur_layout->l_stack == NULL)
2392 ws->cur_layout = &layouts[0];
2395 if (focus_mode == SWM_FOCUS_DEFAULT)
2396 drain_enter_notify();
2397 a.id = SWM_ARG_ID_FOCUSCUR;
2403 stack_config(struct swm_region *r, union arg *args)
2405 struct workspace *ws = r->ws;
2407 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
2410 if (ws->cur_layout->l_config != NULL)
2411 ws->cur_layout->l_config(ws, args->id);
2413 if (args->id != SWM_ARG_ID_STACKINIT)
2419 struct swm_geometry g;
2420 struct swm_region *r;
2423 DNPRINTF(SWM_D_STACK, "stack\n");
2425 for (i = 0; i < ScreenCount(display); i++) {
2427 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2428 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
2429 "(screen %d, region %d)\n", r->ws->idx, i, j++);
2431 /* start with screen geometry, adjust for bar */
2433 g.w -= 2 * border_width;
2434 g.h -= 2 * border_width;
2440 r->ws->cur_layout->l_stack(r->ws, &g);
2441 /* save r so we can track region changes */
2448 if (focus_mode == SWM_FOCUS_DEFAULT)
2449 drain_enter_notify();
2453 store_float_geom(struct ws_win *win, struct swm_region *r)
2455 /* retain window geom and region geom */
2456 win->g_float.x = win->g.x;
2457 win->g_float.y = win->g.y;
2458 win->g_float.w = win->g.w;
2459 win->g_float.h = win->g.h;
2460 win->rg_float.x = r->g.x;
2461 win->rg_float.y = r->g.y;
2462 win->rg_float.w = r->g.w;
2463 win->rg_float.h = r->g.h;
2464 win->g_floatvalid = 1;
2468 stack_floater(struct ws_win *win, struct swm_region *r)
2476 bzero(&wc, sizeof wc);
2477 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2480 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2481 * geom on ws switches or return from max mode
2484 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2485 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2487 * use stored g and rg to set relative position and size
2488 * as in old region or before max stack mode
2490 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2491 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2492 win->g.w = win->g_float.w;
2493 win->g.h = win->g_float.h;
2494 win->g_floatvalid = 0;
2497 win->floatmaxed = 0;
2499 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
2500 (win->g.h >= HEIGHT(r)))
2501 wc.border_width = 0;
2503 wc.border_width = border_width;
2504 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2505 win->g.w = (double)WIDTH(r) * dialog_ratio;
2506 win->g.h = (double)HEIGHT(r) * dialog_ratio;
2511 * floaters and transients are auto-centred unless moved
2514 win->g.x = r->g.x + (WIDTH(r) - win->g.w) /
2515 2 - wc.border_width;
2516 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) /
2517 2 - wc.border_width;
2520 /* win can be outside r if new r smaller than old r */
2521 /* Ensure top left corner inside r (move probs otherwise) */
2522 if (win->g.x < r->g.x - wc.border_width)
2523 win->g.x = r->g.x - wc.border_width;
2524 if (win->g.x > r->g.x + r->g.w - 1)
2525 win->g.x = (win->g.w > r->g.w) ? r->g.x :
2526 (r->g.x + r->g.w - win->g.w - 2 * wc.border_width);
2527 if (win->g.y < r->g.y - wc.border_width)
2528 win->g.y = r->g.y - wc.border_width;
2529 if (win->g.y > r->g.y + r->g.h - 1)
2530 win->g.y = (win->g.h > r->g.h) ? r->g.y :
2531 (r->g.y + r->g.h - win->g.h - 2 * wc.border_width);
2535 wc.width = win->g.w;
2536 wc.height = win->g.h;
2539 * Retain floater and transient geometry for correct positioning
2540 * when ws changes region
2542 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2543 store_float_geom(win, r);
2545 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
2546 win->id, wc.x, wc.y, wc.width, wc.height);
2548 XConfigureWindow(display, win->id, mask, &wc);
2552 * Send keystrokes to terminal to decrease/increase the font size as the
2553 * window size changes.
2556 adjust_font(struct ws_win *win)
2558 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2559 win->floating || win->transient)
2562 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2563 win->g.w / win->sh.width_inc < term_width &&
2564 win->font_steps < SWM_MAX_FONT_STEPS) {
2565 win->font_size_boundary[win->font_steps] =
2566 (win->sh.width_inc * term_width) + win->sh.base_width;
2569 win->last_inc = win->sh.width_inc;
2570 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2571 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2572 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
2575 win->last_inc = win->sh.width_inc;
2576 fake_keypress(win, XK_KP_Add, ShiftMask);
2580 #define SWAPXY(g) do { \
2582 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2583 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2586 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2589 XWindowAttributes wa;
2590 struct swm_geometry win_g, r_g = *g;
2591 struct ws_win *win, *fs_win = 0;
2592 int i, j, s, stacks;
2593 int w_inc = 1, h_inc, w_base = 1, h_base;
2594 int hrh, extra = 0, h_slice, last_h = 0;
2595 int split, colno, winno, mwin, msize, mscale;
2596 int remain, missing, v_slice, reconfigure;
2599 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2600 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2602 winno = count_win(ws, 0);
2603 if (winno == 0 && count_win(ws, 1) == 0)
2606 TAILQ_FOREACH(win, &ws->winlist, entry)
2607 if (win->transient == 0 && win->floating == 0
2608 && win->iconic == 0)
2615 w_inc = win->sh.width_inc;
2616 w_base = win->sh.base_width;
2617 mwin = ws->l_state.horizontal_mwin;
2618 mscale = ws->l_state.horizontal_msize;
2619 stacks = ws->l_state.horizontal_stacks;
2622 w_inc = win->sh.height_inc;
2623 w_base = win->sh.base_height;
2624 mwin = ws->l_state.vertical_mwin;
2625 mscale = ws->l_state.vertical_msize;
2626 stacks = ws->l_state.vertical_stacks;
2630 if (stacks > winno - mwin)
2631 stacks = winno - mwin;
2635 h_slice = r_g.h / SWM_H_SLICE;
2636 if (mwin && winno > mwin) {
2637 v_slice = r_g.w / SWM_V_SLICE;
2641 win_g.w = v_slice * mscale;
2643 if (w_inc > 1 && w_inc < v_slice) {
2644 /* adjust for window's requested size increment */
2645 remain = (win_g.w - w_base) % w_inc;
2646 missing = w_inc - remain;
2653 win_g.x += r_g.w - msize;
2656 colno = split = winno / stacks;
2657 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
2658 2 * border_width) / stacks);
2660 hrh = r_g.h / colno;
2661 extra = r_g.h - (colno * hrh);
2662 win_g.h = hrh - 2 * border_width;
2664 /* stack all the tiled windows */
2665 i = j = 0, s = stacks;
2666 TAILQ_FOREACH(win, &ws->winlist, entry) {
2667 if (win->transient != 0 || win->floating != 0)
2669 if (win->iconic != 0)
2672 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2677 if (split && i == split) {
2678 colno = (winno - mwin) / stacks;
2679 if (s <= (winno - mwin) % stacks)
2681 split = split + colno;
2682 hrh = (r_g.h / colno);
2683 extra = r_g.h - (colno * hrh);
2687 win_g.x += win_g.w + 2 * border_width;
2688 win_g.w = (r_g.w - msize -
2689 (stacks * 2 * border_width)) / stacks;
2691 win_g.w += (r_g.w - msize -
2692 (stacks * 2 * border_width)) % stacks;
2696 win_g.h = hrh - 2 * border_width;
2698 h_inc = win->sh.width_inc;
2699 h_base = win->sh.base_width;
2701 h_inc = win->sh.height_inc;
2702 h_base = win->sh.base_height;
2704 if (j == colno - 1) {
2705 win_g.h = hrh + extra;
2706 } else if (h_inc > 1 && h_inc < h_slice) {
2707 /* adjust for window's requested size increment */
2708 remain = (win_g.h - h_base) % h_inc;
2709 missing = h_inc - remain;
2711 if (missing <= extra || j == 0) {
2723 win_g.y += last_h + 2 * border_width;
2725 bzero(&wc, sizeof wc);
2726 if (disable_border && bar_enabled == 0 && winno == 1){
2727 wc.border_width = 0;
2728 win_g.w += 2 * border_width;
2729 win_g.h += 2 * border_width;
2731 wc.border_width = border_width;
2734 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2735 win->g.w != win_g.h || win->g.h != win_g.w) {
2737 win->g.x = wc.x = win_g.y;
2738 win->g.y = wc.y = win_g.x;
2739 win->g.w = wc.width = win_g.h;
2740 win->g.h = wc.height = win_g.w;
2743 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2744 win->g.w != win_g.w || win->g.h != win_g.h) {
2746 win->g.x = wc.x = win_g.x;
2747 win->g.y = wc.y = win_g.y;
2748 win->g.w = wc.width = win_g.w;
2749 win->g.h = wc.height = win_g.h;
2754 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2755 XConfigureWindow(display, win->id, mask, &wc);
2758 if (XGetWindowAttributes(display, win->id, &wa))
2759 if (wa.map_state == IsUnmapped)
2760 XMapRaised(display, win->id);
2768 /* now, stack all the floaters and transients */
2769 TAILQ_FOREACH(win, &ws->winlist, entry) {
2770 if (win->transient == 0 && win->floating == 0)
2772 if (win->iconic == 1)
2774 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2779 stack_floater(win, ws->r);
2780 XMapRaised(display, win->id);
2784 stack_floater(fs_win, ws->r);
2785 XMapRaised(display, fs_win->id);
2790 vertical_config(struct workspace *ws, int id)
2792 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2795 case SWM_ARG_ID_STACKRESET:
2796 case SWM_ARG_ID_STACKINIT:
2797 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2798 ws->l_state.vertical_mwin = 1;
2799 ws->l_state.vertical_stacks = 1;
2801 case SWM_ARG_ID_MASTERSHRINK:
2802 if (ws->l_state.vertical_msize > 1)
2803 ws->l_state.vertical_msize--;
2805 case SWM_ARG_ID_MASTERGROW:
2806 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2807 ws->l_state.vertical_msize++;
2809 case SWM_ARG_ID_MASTERADD:
2810 ws->l_state.vertical_mwin++;
2812 case SWM_ARG_ID_MASTERDEL:
2813 if (ws->l_state.vertical_mwin > 0)
2814 ws->l_state.vertical_mwin--;
2816 case SWM_ARG_ID_STACKINC:
2817 ws->l_state.vertical_stacks++;
2819 case SWM_ARG_ID_STACKDEC:
2820 if (ws->l_state.vertical_stacks > 1)
2821 ws->l_state.vertical_stacks--;
2829 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2831 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2833 stack_master(ws, g, 0, 0);
2837 horizontal_config(struct workspace *ws, int id)
2839 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2842 case SWM_ARG_ID_STACKRESET:
2843 case SWM_ARG_ID_STACKINIT:
2844 ws->l_state.horizontal_mwin = 1;
2845 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2846 ws->l_state.horizontal_stacks = 1;
2848 case SWM_ARG_ID_MASTERSHRINK:
2849 if (ws->l_state.horizontal_msize > 1)
2850 ws->l_state.horizontal_msize--;
2852 case SWM_ARG_ID_MASTERGROW:
2853 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2854 ws->l_state.horizontal_msize++;
2856 case SWM_ARG_ID_MASTERADD:
2857 ws->l_state.horizontal_mwin++;
2859 case SWM_ARG_ID_MASTERDEL:
2860 if (ws->l_state.horizontal_mwin > 0)
2861 ws->l_state.horizontal_mwin--;
2863 case SWM_ARG_ID_STACKINC:
2864 ws->l_state.horizontal_stacks++;
2866 case SWM_ARG_ID_STACKDEC:
2867 if (ws->l_state.horizontal_stacks > 1)
2868 ws->l_state.horizontal_stacks--;
2876 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2878 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
2880 stack_master(ws, g, 1, 0);
2883 /* fullscreen view */
2885 max_stack(struct workspace *ws, struct swm_geometry *g)
2888 struct swm_geometry gg = *g;
2889 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2893 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2898 winno = count_win(ws, 0);
2899 if (winno == 0 && count_win(ws, 1) == 0)
2902 TAILQ_FOREACH(win, &ws->winlist, entry) {
2903 if (win->transient) {
2905 parent = find_window(win->transient);
2909 if (win->floating && win->floatmaxed == 0 ) {
2911 * retain geometry for retrieval on exit from
2914 store_float_geom(win, ws->r);
2915 win->floatmaxed = 1;
2918 /* only reconfigure if necessary */
2919 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2921 bzero(&wc, sizeof wc);
2922 win->g.x = wc.x = gg.x;
2923 win->g.y = wc.y = gg.y;
2925 wc.border_width = border_width;
2926 win->g.w = wc.width = gg.w;
2927 win->g.h = wc.height = gg.h;
2929 wc.border_width = 0;
2930 win->g.w = wc.width = gg.w + 2 * border_width;
2931 win->g.h = wc.height = gg.h + 2 * border_width;
2933 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2934 XConfigureWindow(display, win->id, mask, &wc);
2936 /* unmap only if we don't have multi screen */
2937 if (win != ws->focus)
2938 if (!(ScreenCount(display) > 1 || outputs > 1))
2942 /* put the last transient on top */
2945 XMapRaised(display, parent->id);
2946 stack_floater(wintrans, ws->r);
2947 focus_magic(wintrans);
2952 send_to_ws(struct swm_region *r, union arg *args)
2954 int wsid = args->id;
2955 struct ws_win *win = NULL, *parent;
2956 struct workspace *ws, *nws;
2957 Atom ws_idx_atom = 0;
2958 unsigned char ws_idx_str[SWM_PROPLEN];
2967 if (win->ws->idx == wsid)
2970 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2973 nws = &win->s->ws[wsid];
2975 a.id = SWM_ARG_ID_FOCUSPREV;
2977 if (win->transient) {
2978 parent = find_window(win->transient);
2980 unmap_window(parent);
2981 TAILQ_REMOVE(&ws->winlist, parent, entry);
2982 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
2987 TAILQ_REMOVE(&ws->winlist, win, entry);
2988 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2991 /* Try to update the window's workspace property */
2992 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2994 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2995 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2997 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2998 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3005 iconify(struct swm_region *r, union arg *args)
3009 if (r->ws->focus == NULL)
3011 unmap_window(r->ws->focus);
3012 update_iconic(r->ws->focus, 1);
3014 if (focus_mode == SWM_FOCUS_DEFAULT)
3015 drain_enter_notify();
3016 r->ws->focus = NULL;
3017 a.id = SWM_ARG_ID_FOCUSCUR;
3022 get_win_name(Display *dpy, Window win, Atom wname, Atom stype,
3023 unsigned long *slen)
3026 unsigned long nitems, nbytes, nextra;
3027 unsigned char *prop = NULL;
3030 status = XGetWindowProperty(dpy, win, wname, 0L, 0L, False, stype,
3031 &rettype, &retfmt, &nitems, &nbytes, &prop);
3032 if (status != Success)
3036 status = XGetWindowProperty(dpy, win, wname, 0L, nbytes, False,
3037 stype, &rettype, &retfmt, &nitems, &nextra, &prop);
3038 if (status != Success) {
3042 if (rettype != stype) {
3051 uniconify(struct swm_region *r, union arg *args)
3059 DNPRINTF(SWM_D_MISC, "uniconify\n");
3061 if (r && r->ws == NULL)
3064 /* make sure we have anything to uniconify */
3065 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3066 if (win->ws == NULL)
3067 continue; /* should never happen */
3068 if (win->iconic == 0)
3077 spawn_select(r, args, "uniconify", &searchpid);
3079 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3082 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3083 if (win->ws == NULL)
3084 continue; /* should never happen */
3085 if (win->iconic == 0)
3088 name = get_win_name(display, win->id, a_wmname, a_string,
3092 fprintf(lfile, "%s.%lu\n", name, win->id);
3099 #define MAX_RESP_LEN 1024
3102 search_do_resp(void)
3106 char *name, *resp, *s;
3109 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3114 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3115 fprintf(stderr, "search: calloc\n");
3119 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3121 fprintf(stderr, "search: read error: %s\n", strerror(errno));
3124 resp[rbytes] = '\0';
3127 DNPRINTF(SWM_D_MISC, "search_do_resp: resp %s\n", resp);
3128 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3129 if (win->iconic == 0)
3131 name = get_win_name(display, win->id, a_wmname, a_string, &len);
3134 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3139 if (strncmp(s, resp, len) == 0) {
3140 /* XXX this should be a callback to generalize */
3141 update_iconic(win, 0);
3152 wkill(struct swm_region *r, union arg *args)
3154 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
3156 if (r->ws->focus == NULL)
3159 if (args->id == SWM_ARG_ID_KILLWINDOW)
3160 XKillClient(display, r->ws->focus->id);
3162 if (r->ws->focus->can_delete)
3163 client_msg(r->ws->focus, adelete);
3168 floating_toggle_win(struct ws_win *win)
3170 struct swm_region *r;
3180 /* reject floating toggles in max stack mode */
3181 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3184 if (win->floating) {
3185 if (!win->floatmaxed) {
3186 /* retain position for refloat */
3187 store_float_geom(win, r);
3191 if (win->g_floatvalid) {
3192 /* refloat at last floating relative position */
3193 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
3194 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
3195 win->g.w = win->g_float.w;
3196 win->g.h = win->g_float.h;
3201 ewmh_update_actions(win);
3207 floating_toggle(struct swm_region *r, union arg *args)
3209 struct ws_win *win = r->ws->focus;
3215 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3216 _NET_WM_STATE_TOGGLE);
3219 if (focus_mode == SWM_FOCUS_DEFAULT)
3220 drain_enter_notify();
3222 if (win == win->ws->focus) {
3223 a.id = SWM_ARG_ID_FOCUSCUR;
3224 focus(win->ws->r, &a);
3229 resize_window(struct ws_win *win, int center)
3233 struct swm_region *r;
3235 r = root_to_region(win->wa.root);
3236 bzero(&wc, sizeof wc);
3237 mask = CWBorderWidth | CWWidth | CWHeight;
3238 wc.border_width = border_width;
3239 wc.width = win->g.w;
3240 wc.height = win->g.h;
3241 if (center == SWM_ARG_ID_CENTER) {
3242 wc.x = (WIDTH(r) - win->g.w) / 2 - border_width;
3243 wc.y = (HEIGHT(r) - win->g.h) / 2 - border_width;
3247 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
3248 win->id, wc.x, wc.y, wc.width, wc.height);
3250 XConfigureWindow(display, win->id, mask, &wc);
3254 resize(struct ws_win *win, union arg *args)
3258 struct swm_region *r = win->ws->r;
3262 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
3263 win->id, win->floating, win->transient);
3265 if (!(win->transient != 0 || win->floating != 0))
3268 /* reject resizes in max mode for floaters (transient ok) */
3269 if (win->floatmaxed)
3273 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3277 if (focus_mode == SWM_FOCUS_DEFAULT)
3278 drain_enter_notify();
3280 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3281 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3284 /* place pointer at bottom left corner or nearest point inside r */
3285 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
3286 relx = win->g.w - 1;
3288 relx = r->g.x + r->g.w - win->g.x - 1;
3290 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
3291 rely = win->g.h - 1;
3293 rely = r->g.y + r->g.h - win->g.y - 1;
3295 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
3297 XMaskEvent(display, MOUSEMASK | ExposureMask |
3298 SubstructureRedirectMask, &ev);
3300 case ConfigureRequest:
3303 handler[ev.type](&ev);
3306 /* do not allow resize outside of region */
3307 if ( ev.xmotion.x_root < r->g.x ||
3308 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3309 ev.xmotion.y_root < r->g.y ||
3310 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3313 if (ev.xmotion.x <= 1)
3315 if (ev.xmotion.y <= 1)
3317 win->g.w = ev.xmotion.x + 1;
3318 win->g.h = ev.xmotion.y + 1;
3320 /* not free, don't sync more than 120 times / second */
3321 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3322 time = ev.xmotion.time;
3323 XSync(display, False);
3324 resize_window(win, args->id);
3328 } while (ev.type != ButtonRelease);
3330 XSync(display, False);
3331 resize_window(win, args->id);
3333 store_float_geom(win,r);
3335 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
3337 XUngrabPointer(display, CurrentTime);
3340 drain_enter_notify();
3344 move_window(struct ws_win *win)
3348 struct swm_region *r;
3350 r = root_to_region(win->wa.root);
3351 bzero(&wc, sizeof wc);
3355 wc.border_width = border_width;
3357 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
3358 win->id, wc.x, wc.y, wc.width, wc.height);
3360 XConfigureWindow(display, win->id, mask, &wc);
3364 move(struct ws_win *win, union arg *args)
3368 struct swm_region *r = win->ws->r;
3370 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
3371 win->id, win->floating, win->transient);
3373 /* in max_stack mode should only move transients */
3374 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
3378 if (win->floating == 0 && !win->transient) {
3379 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3382 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3387 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3388 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3390 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3392 XMaskEvent(display, MOUSEMASK | ExposureMask |
3393 SubstructureRedirectMask, &ev);
3395 case ConfigureRequest:
3398 handler[ev.type](&ev);
3401 /* don't allow to move window origin out of region */
3402 if ( ev.xmotion.x_root < r->g.x ||
3403 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3404 ev.xmotion.y_root < r->g.y ||
3405 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3408 win->g.x = ev.xmotion.x_root - border_width;
3409 win->g.y = ev.xmotion.y_root - border_width;
3411 /* not free, don't sync more than 120 times / second */
3412 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3413 time = ev.xmotion.time;
3414 XSync(display, False);
3419 } while (ev.type != ButtonRelease);
3421 XSync(display, False);
3424 store_float_geom(win,r);
3425 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3426 XUngrabPointer(display, CurrentTime);
3429 drain_enter_notify();
3432 /* user/key callable function IDs */
3489 kf_dumpwins, /* MUST BE LAST */
3493 /* key definitions */
3495 dummykeyfunc(struct swm_region *r, union arg *args)
3500 legacyfunc(struct swm_region *r, union arg *args)
3505 char name[SWM_FUNCNAME_LEN];
3506 void (*func)(struct swm_region *r, union arg *);
3508 } keyfuncs[kf_invalid + 1] = {
3509 /* name function argument */
3510 { "cycle_layout", cycle_layout, {0} },
3511 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
3512 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
3513 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
3514 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
3515 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
3516 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
3517 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
3518 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
3519 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
3520 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
3521 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
3522 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
3523 { "spawn_term", spawnterm, {.argv = spawn_term} },
3524 { "spawn_menu", legacyfunc, {0} },
3525 { "quit", quit, {0} },
3526 { "restart", restart, {0} },
3527 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
3528 { "ws_1", switchws, {.id = 0} },
3529 { "ws_2", switchws, {.id = 1} },
3530 { "ws_3", switchws, {.id = 2} },
3531 { "ws_4", switchws, {.id = 3} },
3532 { "ws_5", switchws, {.id = 4} },
3533 { "ws_6", switchws, {.id = 5} },
3534 { "ws_7", switchws, {.id = 6} },
3535 { "ws_8", switchws, {.id = 7} },
3536 { "ws_9", switchws, {.id = 8} },
3537 { "ws_10", switchws, {.id = 9} },
3538 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
3539 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
3540 { "ws_prior", priorws, {0} },
3541 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
3542 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
3543 { "mvws_1", send_to_ws, {.id = 0} },
3544 { "mvws_2", send_to_ws, {.id = 1} },
3545 { "mvws_3", send_to_ws, {.id = 2} },
3546 { "mvws_4", send_to_ws, {.id = 3} },
3547 { "mvws_5", send_to_ws, {.id = 4} },
3548 { "mvws_6", send_to_ws, {.id = 5} },
3549 { "mvws_7", send_to_ws, {.id = 6} },
3550 { "mvws_8", send_to_ws, {.id = 7} },
3551 { "mvws_9", send_to_ws, {.id = 8} },
3552 { "mvws_10", send_to_ws, {.id = 9} },
3553 { "bar_toggle", bar_toggle, {0} },
3554 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
3555 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
3556 { "screenshot_all", legacyfunc, {0} },
3557 { "screenshot_wind", legacyfunc, {0} },
3558 { "float_toggle", floating_toggle,{0} },
3559 { "version", version, {0} },
3560 { "spawn_lock", legacyfunc, {0} },
3561 { "spawn_initscr", legacyfunc, {0} },
3562 { "spawn_custom", dummykeyfunc, {0} },
3563 { "iconify", iconify, {0} },
3564 { "uniconify", uniconify, {0} },
3565 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
3566 { "invalid key func", NULL, {0} },
3571 enum keyfuncid funcid;
3574 int keys_size = 0, keys_length = 0;
3575 struct key *keys = NULL;
3578 enum { client_click, root_click };
3580 unsigned int action;
3582 unsigned int button;
3583 void (*func)(struct ws_win *, union arg *);
3586 /* action key mouse button func args */
3587 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
3588 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
3589 { client_click, MODKEY, Button1, move, {0} },
3593 update_modkey(unsigned int mod)
3598 for (i = 0; i < keys_length; i++)
3599 if (keys[i].mod & ShiftMask)
3600 keys[i].mod = mod | ShiftMask;
3604 for (i = 0; i < LENGTH(buttons); i++)
3605 if (buttons[i].mask & ShiftMask)
3606 buttons[i].mask = mod | ShiftMask;
3608 buttons[i].mask = mod;
3618 int spawns_size = 0, spawns_length = 0;
3619 struct spawn_prog *spawns = NULL;
3622 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
3625 struct spawn_prog *prog = NULL;
3627 char *ap, **real_args;
3629 DNPRINTF(SWM_D_SPAWN, "spawn_expand %s\n", spawn_name);
3632 for (i = 0; i < spawns_length; i++) {
3633 if (!strcasecmp(spawn_name, spawns[i].name))
3637 fprintf(stderr, "spawn_custom: program %s not found\n",
3642 /* make room for expanded args */
3643 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
3644 err(1, "spawn_custom: calloc real_args");
3646 /* expand spawn_args into real_args */
3647 for (i = 0; i < prog->argc; i++) {
3649 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
3650 if (!strcasecmp(ap, "$bar_border")) {
3652 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
3654 err(1, "spawn_custom border color");
3655 } else if (!strcasecmp(ap, "$bar_color")) {
3657 strdup(r->s->c[SWM_S_COLOR_BAR].name))
3659 err(1, "spawn_custom bar color");
3660 } else if (!strcasecmp(ap, "$bar_font")) {
3661 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
3663 err(1, "spawn_custom bar fonts");
3664 } else if (!strcasecmp(ap, "$bar_font_color")) {
3666 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
3668 err(1, "spawn_custom color font");
3669 } else if (!strcasecmp(ap, "$color_focus")) {
3671 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
3673 err(1, "spawn_custom color focus");
3674 } else if (!strcasecmp(ap, "$color_unfocus")) {
3676 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
3678 err(1, "spawn_custom color unfocus");
3680 /* no match --> copy as is */
3681 if ((real_args[i] = strdup(ap)) == NULL)
3682 err(1, "spawn_custom strdup(ap)");
3684 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
3689 if ((swm_debug & SWM_D_SPAWN) != 0) {
3690 fprintf(stderr, "spawn_custom: result = ");
3691 for (i = 0; i < prog->argc; i++)
3692 fprintf(stderr, "\"%s\" ", real_args[i]);
3693 fprintf(stderr, "\n");
3696 *ret_args = real_args;
3697 return (prog->argc);
3701 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
3707 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3711 spawn(r->ws->idx, &a, 1);
3713 for (i = 0; i < spawn_argc; i++)
3719 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
3725 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3729 if (pipe(select_list_pipe) == -1)
3730 err(1, "pipe error");
3731 if (pipe(select_resp_pipe) == -1)
3732 err(1, "pipe error");
3734 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
3735 err(1, "could not disable SIGPIPE");
3736 switch (*pid = fork()) {
3738 err(1, "cannot fork");
3741 if (dup2(select_list_pipe[0], 0) == -1)
3743 if (dup2(select_resp_pipe[1], 1) == -1)
3745 close(select_list_pipe[1]);
3746 close(select_resp_pipe[0]);
3747 spawn(r->ws->idx, &a, 0);
3749 default: /* parent */
3750 close(select_list_pipe[0]);
3751 close(select_resp_pipe[1]);
3755 for (i = 0; i < spawn_argc; i++)
3761 setspawn(struct spawn_prog *prog)
3765 if (prog == NULL || prog->name == NULL)
3769 for (i = 0; i < spawns_length; i++) {
3770 if (!strcmp(spawns[i].name, prog->name)) {
3772 if (prog->argv == NULL) {
3774 DNPRINTF(SWM_D_SPAWN,
3775 "setspawn: delete #%d %s\n",
3777 free(spawns[i].name);
3778 for (j = 0; j < spawns[i].argc; j++)
3779 free(spawns[i].argv[j]);
3780 free(spawns[i].argv);
3781 j = spawns_length - 1;
3783 spawns[i] = spawns[j];
3788 DNPRINTF(SWM_D_SPAWN,
3789 "setspawn: replace #%d %s\n",
3791 free(spawns[i].name);
3792 for (j = 0; j < spawns[i].argc; j++)
3793 free(spawns[i].argv[j]);
3794 free(spawns[i].argv);
3797 /* found case handled */
3803 if (prog->argv == NULL) {
3805 "error: setspawn: cannot find program %s", prog->name);
3810 /* not found: add */
3811 if (spawns_size == 0 || spawns == NULL) {
3813 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
3814 spawns = malloc((size_t)spawns_size *
3815 sizeof(struct spawn_prog));
3816 if (spawns == NULL) {
3817 fprintf(stderr, "setspawn: malloc failed\n");
3821 } else if (spawns_length == spawns_size) {
3823 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
3824 spawns = realloc(spawns, (size_t)spawns_size *
3825 sizeof(struct spawn_prog));
3826 if (spawns == NULL) {
3827 fprintf(stderr, "setspawn: realloc failed\n");
3833 if (spawns_length < spawns_size) {
3834 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
3835 spawns_length, prog->name);
3836 i = spawns_length++;
3839 fprintf(stderr, "spawns array problem?\n");
3840 if (spawns == NULL) {
3841 fprintf(stderr, "spawns array is NULL!\n");
3849 setconfspawn(char *selector, char *value, int flags)
3851 struct spawn_prog *prog;
3852 char *vp, *cp, *word;
3854 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
3855 if ((prog = calloc(1, sizeof *prog)) == NULL)
3856 err(1, "setconfspawn: calloc prog");
3857 prog->name = strdup(selector);
3858 if (prog->name == NULL)
3859 err(1, "setconfspawn prog->name");
3860 if ((cp = vp = strdup(value)) == NULL)
3861 err(1, "setconfspawn: strdup(value) ");
3862 while ((word = strsep(&cp, " \t")) != NULL) {
3863 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
3865 cp += (long)strspn(cp, " \t");
3866 if (strlen(word) > 0) {
3868 if ((prog->argv = realloc(prog->argv,
3869 prog->argc * sizeof(char *))) == NULL)
3870 err(1, "setconfspawn: realloc");
3871 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
3872 err(1, "setconfspawn: strdup");
3879 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
3886 setconfspawn("term", "xterm", 0);
3887 setconfspawn("screenshot_all", "screenshot.sh full", 0);
3888 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
3889 setconfspawn("lock", "xlock", 0);
3890 setconfspawn("initscr", "initscreen.sh", 0);
3891 setconfspawn("menu", "dmenu_run"
3894 " -nf $bar_font_color"
3896 " -sf $bar_color", 0);
3897 setconfspawn("uniconify", "dmenu"
3901 " -nf $bar_font_color"
3903 " -sf $bar_color", 0);
3907 #define SWM_MODNAME_SIZE 32
3908 #define SWM_KEY_WS "\n+ \t"
3910 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
3914 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
3915 if (mod == NULL || ks == NULL) {
3916 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
3919 if (keystr == NULL || strlen(keystr) == 0) {
3920 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
3926 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
3927 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
3929 cp += (long)strspn(cp, SWM_KEY_WS);
3930 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
3932 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
3934 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
3936 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
3938 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
3940 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
3942 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
3943 *mod |= ControlMask;
3945 *ks = XStringToKeysym(name);
3946 XConvertCase(*ks, ks, &uks);
3947 if (ks == NoSymbol) {
3949 "parsekeys: invalid key %s\n",
3955 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
3960 strdupsafe(char *str)
3965 return (strdup(str));
3969 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
3973 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
3974 keyfuncs[kfid].name, spawn_name);
3976 for (i = 0; i < keys_length; i++) {
3977 if (keys[i].mod == mod && keys[i].keysym == ks) {
3978 if (kfid == kf_invalid) {
3981 "setkeybinding: delete #%d %s\n",
3982 i, keyfuncs[keys[i].funcid].name);
3983 free(keys[i].spawn_name);
3984 j = keys_length - 1;
3988 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3991 /* found: replace */
3993 "setkeybinding: replace #%d %s %s\n",
3994 i, keyfuncs[keys[i].funcid].name,
3996 free(keys[i].spawn_name);
3998 keys[i].keysym = ks;
3999 keys[i].funcid = kfid;
4000 keys[i].spawn_name = strdupsafe(spawn_name);
4001 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4006 if (kfid == kf_invalid) {
4008 "error: setkeybinding: cannot find mod/key combination");
4009 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4012 /* not found: add */
4013 if (keys_size == 0 || keys == NULL) {
4015 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
4016 keys = malloc((size_t)keys_size * sizeof(struct key));
4018 fprintf(stderr, "malloc failed\n");
4022 } else if (keys_length == keys_size) {
4024 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
4025 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
4027 fprintf(stderr, "realloc failed\n");
4032 if (keys_length < keys_size) {
4034 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
4035 j, keyfuncs[kfid].name, spawn_name);
4037 keys[j].keysym = ks;
4038 keys[j].funcid = kfid;
4039 keys[j].spawn_name = strdupsafe(spawn_name);
4041 fprintf(stderr, "keys array problem?\n");
4043 fprintf(stderr, "keys array problem\n");
4047 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4051 setconfbinding(char *selector, char *value, int flags)
4053 enum keyfuncid kfid;
4057 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
4058 if (selector == NULL) {
4059 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
4060 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4062 setkeybinding(mod, ks, kfid, NULL);
4067 /* search by key function name */
4068 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
4069 if (strncasecmp(selector, keyfuncs[kfid].name,
4070 SWM_FUNCNAME_LEN) == 0) {
4071 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4073 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4074 setkeybinding(mod, ks, kfid, NULL);
4080 /* search by custom spawn name */
4081 for (i = 0; i < spawns_length; i++) {
4082 if (strcasecmp(selector, spawns[i].name) == 0) {
4083 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4085 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4086 setkeybinding(mod, ks, kf_spawn_custom,
4093 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
4100 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
4101 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
4102 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
4103 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
4104 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
4105 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
4106 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
4107 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
4108 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
4109 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
4110 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
4111 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
4112 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
4113 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
4114 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
4115 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
4116 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
4117 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
4118 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
4119 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
4120 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
4121 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
4122 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
4123 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
4124 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
4125 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
4126 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
4127 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
4128 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
4129 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
4130 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
4131 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
4132 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
4133 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
4134 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
4135 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
4136 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
4137 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
4138 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
4139 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
4140 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
4141 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
4142 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
4143 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
4144 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
4145 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
4146 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
4147 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
4148 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
4149 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
4150 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
4151 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
4152 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
4153 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
4154 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
4155 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
4157 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
4162 updatenumlockmask(void)
4165 XModifierKeymap *modmap;
4167 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
4169 modmap = XGetModifierMapping(display);
4170 for (i = 0; i < 8; i++)
4171 for (j = 0; j < modmap->max_keypermod; j++)
4172 if (modmap->modifiermap[i * modmap->max_keypermod + j]
4173 == XKeysymToKeycode(display, XK_Num_Lock))
4174 numlockmask = (1 << i);
4176 XFreeModifiermap(modmap);
4182 unsigned int i, j, k;
4184 unsigned int modifiers[] =
4185 { 0, LockMask, numlockmask, numlockmask | LockMask };
4187 DNPRINTF(SWM_D_MISC, "grabkeys\n");
4188 updatenumlockmask();
4190 for (k = 0; k < ScreenCount(display); k++) {
4191 if (TAILQ_EMPTY(&screens[k].rl))
4193 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
4194 for (i = 0; i < keys_length; i++) {
4195 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
4196 for (j = 0; j < LENGTH(modifiers); j++)
4197 XGrabKey(display, code,
4198 keys[i].mod | modifiers[j],
4199 screens[k].root, True,
4200 GrabModeAsync, GrabModeAsync);
4206 grabbuttons(struct ws_win *win, int focused)
4209 unsigned int modifiers[] =
4210 { 0, LockMask, numlockmask, numlockmask|LockMask };
4212 updatenumlockmask();
4213 XUngrabButton(display, AnyButton, AnyModifier, win->id);
4215 for (i = 0; i < LENGTH(buttons); i++)
4216 if (buttons[i].action == client_click)
4217 for (j = 0; j < LENGTH(modifiers); j++)
4218 XGrabButton(display, buttons[i].button,
4219 buttons[i].mask | modifiers[j],
4220 win->id, False, BUTTONMASK,
4221 GrabModeAsync, GrabModeSync, None,
4224 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
4225 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
4228 const char *quirkname[] = {
4229 "NONE", /* config string for "no value" */
4238 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
4239 #define SWM_Q_WS "\n|+ \t"
4241 parsequirks(char *qstr, unsigned long *quirk)
4251 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
4253 cp += (long)strspn(cp, SWM_Q_WS);
4254 for (i = 0; i < LENGTH(quirkname); i++) {
4255 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
4256 DNPRINTF(SWM_D_QUIRK,
4257 "parsequirks: %s\n", name);
4262 *quirk |= 1 << (i-1);
4266 if (i >= LENGTH(quirkname)) {
4267 DNPRINTF(SWM_D_QUIRK,
4268 "parsequirks: invalid quirk [%s]\n", name);
4276 setquirk(const char *class, const char *name, const int quirk)
4281 for (i = 0; i < quirks_length; i++) {
4282 if (!strcmp(quirks[i].class, class) &&
4283 !strcmp(quirks[i].name, name)) {
4286 DNPRINTF(SWM_D_QUIRK,
4287 "setquirk: delete #%d %s:%s\n",
4288 i, quirks[i].class, quirks[i].name);
4289 free(quirks[i].class);
4290 free(quirks[i].name);
4291 j = quirks_length - 1;
4293 quirks[i] = quirks[j];
4297 /* found: replace */
4298 DNPRINTF(SWM_D_QUIRK,
4299 "setquirk: replace #%d %s:%s\n",
4300 i, quirks[i].class, quirks[i].name);
4301 free(quirks[i].class);
4302 free(quirks[i].name);
4303 quirks[i].class = strdup(class);
4304 quirks[i].name = strdup(name);
4305 quirks[i].quirk = quirk;
4312 "error: setquirk: cannot find class/name combination");
4315 /* not found: add */
4316 if (quirks_size == 0 || quirks == NULL) {
4318 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
4319 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
4320 if (quirks == NULL) {
4321 fprintf(stderr, "setquirk: malloc failed\n");
4325 } else if (quirks_length == quirks_size) {
4327 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
4328 quirks = realloc(quirks,
4329 (size_t)quirks_size * sizeof(struct quirk));
4330 if (quirks == NULL) {
4331 fprintf(stderr, "setquirk: realloc failed\n");
4336 if (quirks_length < quirks_size) {
4337 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
4338 j = quirks_length++;
4339 quirks[j].class = strdup(class);
4340 quirks[j].name = strdup(name);
4341 quirks[j].quirk = quirk;
4343 fprintf(stderr, "quirks array problem?\n");
4344 if (quirks == NULL) {
4345 fprintf(stderr, "quirks array problem!\n");
4352 setconfquirk(char *selector, char *value, int flags)
4354 char *cp, *class, *name;
4356 unsigned long quirks;
4357 if (selector == NULL)
4359 if ((cp = strchr(selector, ':')) == NULL)
4364 if ((retval = parsequirks(value, &quirks)) == 0)
4365 setquirk(class, name, quirks);
4372 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
4373 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
4374 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
4375 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
4376 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4377 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
4378 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4379 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4380 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4381 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4382 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
4383 setquirk("pcb", "pcb", SWM_Q_FLOAT);
4384 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
4387 /* conf file stuff */
4388 #define SWM_CONF_FILE "scrotwm.conf"
4390 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH,
4391 SWM_S_STACK_ENABLED, SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT,
4392 SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED,
4393 SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
4394 SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED,
4395 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH,
4396 SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM,
4397 SWM_S_SS_APP, SWM_S_DIALOG_RATIO, SWM_S_BAR_AT_BOTTOM
4401 setconfvalue(char *selector, char *value, int flags)
4404 case SWM_S_BAR_DELAY:
4405 bar_delay = atoi(value);
4407 case SWM_S_BAR_ENABLED:
4408 bar_enabled = atoi(value);
4410 case SWM_S_BAR_BORDER_WIDTH:
4411 bar_border_width = atoi(value);
4413 case SWM_S_BAR_AT_BOTTOM:
4414 bar_at_bottom = atoi(value);
4416 case SWM_S_STACK_ENABLED:
4417 stack_enabled = atoi(value);
4419 case SWM_S_CLOCK_ENABLED:
4420 clock_enabled = atoi(value);
4422 case SWM_S_CLOCK_FORMAT:
4423 #ifndef SWM_DENY_CLOCK_FORMAT
4425 if ((clock_format = strdup(value)) == NULL)
4426 err(1, "setconfvalue: clock_format");
4429 case SWM_S_CYCLE_EMPTY:
4430 cycle_empty = atoi(value);
4432 case SWM_S_CYCLE_VISIBLE:
4433 cycle_visible = atoi(value);
4435 case SWM_S_SS_ENABLED:
4436 ss_enabled = atoi(value);
4438 case SWM_S_TERM_WIDTH:
4439 term_width = atoi(value);
4441 case SWM_S_TITLE_CLASS_ENABLED:
4442 title_class_enabled = atoi(value);
4444 case SWM_S_WINDOW_NAME_ENABLED:
4445 window_name_enabled = atoi(value);
4447 case SWM_S_TITLE_NAME_ENABLED:
4448 title_name_enabled = atoi(value);
4450 case SWM_S_FOCUS_MODE:
4451 if (!strcmp(value, "default"))
4452 focus_mode = SWM_FOCUS_DEFAULT;
4453 else if (!strcmp(value, "follow_cursor"))
4454 focus_mode = SWM_FOCUS_FOLLOW;
4455 else if (!strcmp(value, "synergy"))
4456 focus_mode = SWM_FOCUS_SYNERGY;
4458 err(1, "focus_mode");
4460 case SWM_S_DISABLE_BORDER:
4461 disable_border = atoi(value);
4463 case SWM_S_BORDER_WIDTH:
4464 border_width = atoi(value);
4466 case SWM_S_BAR_FONT:
4468 if ((bar_fonts[0] = strdup(value)) == NULL)
4469 err(1, "setconfvalue: bar_font");
4471 case SWM_S_BAR_ACTION:
4473 if ((bar_argv[0] = strdup(value)) == NULL)
4474 err(1, "setconfvalue: bar_action");
4476 case SWM_S_SPAWN_TERM:
4477 free(spawn_term[0]);
4478 if ((spawn_term[0] = strdup(value)) == NULL)
4479 err(1, "setconfvalue: spawn_term");
4483 case SWM_S_DIALOG_RATIO:
4484 dialog_ratio = atof(value);
4485 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
4495 setconfmodkey(char *selector, char *value, int flags)
4497 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
4498 update_modkey(Mod1Mask);
4499 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
4500 update_modkey(Mod2Mask);
4501 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
4502 update_modkey(Mod3Mask);
4503 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
4504 update_modkey(Mod4Mask);
4511 setconfcolor(char *selector, char *value, int flags)
4513 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
4518 setconfregion(char *selector, char *value, int flags)
4520 custom_region(value);
4525 setautorun(char *selector, char *value, int flags)
4535 if (getenv("SWM_STARTED"))
4539 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
4540 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'\n");
4542 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
4543 errx(1, "autorun: invalid workspace %d\n", ws_id + 1);
4546 * This is a little intricate
4548 * If the pid already exists we simply reuse it because it means it was
4549 * used before AND not claimed by manage_window. We get away with
4550 * altering it in the parent after INSERT because this can not be a race
4553 while ((ap = strsep(&sp, " \t")) != NULL) {
4556 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
4558 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
4559 err(1, "setautorun: realloc");
4560 a.argv[argc - 1] = ap;
4563 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
4564 err(1, "setautorun: realloc");
4565 a.argv[argc] = NULL;
4567 if ((pid = fork()) == 0) {
4568 spawn(ws_id, &a, 1);
4577 p = calloc(1, sizeof *p);
4580 TAILQ_INSERT_TAIL(&pidlist, p, entry);
4590 setlayout(char *selector, char *value, int flags)
4594 struct workspace *ws;
4596 if (getenv("SWM_STARTED"))
4600 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
4601 errx(1, "invalid layout entry, should be 'ws[<idx>]:<type>'\n");
4603 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
4604 errx(1, "layout: invalid workspace %d\n", ws_id + 1);
4606 if (!strcasecmp(s, "vertical"))
4608 else if (!strcasecmp(s, "horizontal"))
4610 else if (!strcasecmp(s, "fullscreen"))
4613 errx(1, "invalid layout entry, should be 'ws[<idx>]:<type>'\n");
4615 for (i = 0; i < ScreenCount(display); i++) {
4616 ws = (struct workspace *)&screens[i].ws;
4617 ws[ws_id].cur_layout = &layouts[st];
4623 /* config options */
4624 struct config_option {
4626 int (*func)(char*, char*, int);
4629 struct config_option configopt[] = {
4630 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
4631 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
4632 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
4633 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
4634 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
4635 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
4636 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
4637 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
4638 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
4639 { "bind", setconfbinding, 0 },
4640 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
4641 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
4642 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
4643 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
4644 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
4645 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
4646 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
4647 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
4648 { "modkey", setconfmodkey, 0 },
4649 { "program", setconfspawn, 0 },
4650 { "quirk", setconfquirk, 0 },
4651 { "region", setconfregion, 0 },
4652 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
4653 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
4654 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
4655 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
4656 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
4657 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
4658 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
4659 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
4660 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
4661 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
4662 { "autorun", setautorun, 0 },
4663 { "layout", setlayout, 0 },
4668 conf_load(char *filename)
4671 char *line, *cp, *optsub, *optval;
4672 size_t linelen, lineno = 0;
4673 int wordlen, i, optind;
4674 struct config_option *opt;
4676 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
4678 if (filename == NULL) {
4679 fprintf(stderr, "conf_load: no filename\n");
4682 if ((config = fopen(filename, "r")) == NULL) {
4683 warn("conf_load: fopen");
4687 while (!feof(config)) {
4688 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
4691 err(1, "%s", filename);
4696 cp += strspn(cp, " \t\n"); /* eat whitespace */
4697 if (cp[0] == '\0') {
4702 /* get config option */
4703 wordlen = strcspn(cp, "=[ \t\n");
4705 warnx("%s: line %zd: no option found",
4710 for (i = 0; i < LENGTH(configopt); i++) {
4711 opt = &configopt[i];
4712 if (!strncasecmp(cp, opt->optname, wordlen) &&
4713 strlen(opt->optname) == wordlen) {
4719 warnx("%s: line %zd: unknown option %.*s",
4720 filename, lineno, wordlen, cp);
4724 cp += strspn(cp, " \t\n"); /* eat whitespace */
4725 /* get [selector] if any */
4729 wordlen = strcspn(cp, "]");
4732 warnx("%s: line %zd: syntax error",
4736 asprintf(&optsub, "%.*s", wordlen, cp);
4739 cp += strspn(cp, "] \t\n"); /* eat trailing */
4741 cp += strspn(cp, "= \t\n"); /* eat trailing */
4743 optval = strdup(cp);
4744 /* call function to deal with it all */
4745 if (configopt[optind].func(optsub, optval,
4746 configopt[optind].funcflags) != 0) {
4747 fprintf(stderr, "%s line %zd: %s\n",
4748 filename, lineno, line);
4749 errx(1, "%s: line %zd: invalid data for %s",
4750 filename, lineno, configopt[optind].optname);
4758 DNPRINTF(SWM_D_CONF, "conf_load end\n");
4764 set_child_transient(struct ws_win *win, Window *trans)
4766 struct ws_win *parent, *w;
4767 XWMHints *wmh = NULL;
4768 struct swm_region *r;
4769 struct workspace *ws;
4771 parent = find_window(win->transient);
4773 parent->child_trans = win;
4775 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
4776 " for %lu trans %lu\n", win->id, win->transient);
4778 if (win->hints == NULL) {
4779 fprintf(stderr, "no hints for %lu\n", win->id);
4783 r = root_to_region(win->wa.root);
4785 /* parent doen't exist in our window list */
4786 TAILQ_FOREACH(w, &ws->winlist, entry) {
4790 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
4791 fprintf(stderr, "can't get hints for %lu\n",
4796 if (win->hints->window_group != wmh->window_group)
4799 w->child_trans = win;
4800 win->transient = w->id;
4802 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
4803 "transient to %lu\n", win->transient);
4813 window_get_pid(Window win)
4815 Atom actual_type_return;
4816 int actual_format_return = 0;
4817 unsigned long nitems_return = 0;
4818 unsigned long bytes_after_return = 0;
4822 unsigned char *prop = NULL;
4824 if (XGetWindowProperty(display, win,
4825 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
4826 XA_CARDINAL, &actual_type_return, &actual_format_return,
4827 &nitems_return, &bytes_after_return,
4828 (unsigned char**)(void*)&pid) != Success)
4830 if (actual_type_return != XA_CARDINAL)
4841 if (XGetWindowProperty(display, win,
4842 XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
4843 XA_STRING, &actual_type_return, &actual_format_return,
4844 &nitems_return, &bytes_after_return, &prop) != Success)
4846 if (actual_type_return != XA_STRING)
4851 ret = strtonum(prop, 0, UINT_MAX, &errstr);
4852 /* ignore error because strtonum returns 0 anyway */
4859 manage_window(Window id)
4862 struct workspace *ws;
4863 struct ws_win *win, *ww;
4864 int format, i, ws_idx, n, border_me = 0;
4865 unsigned long nitems, bytes;
4866 Atom ws_idx_atom = 0, type;
4867 Atom *prot = NULL, *pp;
4868 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
4869 struct swm_region *r;
4875 if ((win = find_window(id)) != NULL)
4876 return (win); /* already being managed */
4878 /* see if we are on the unmanaged list */
4879 if ((win = find_unmanaged_window(id)) != NULL) {
4880 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
4882 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
4883 if (win->transient) {
4884 set_child_transient(win, &trans);
4885 } if (trans && (ww = find_window(trans)))
4886 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
4888 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
4889 ewmh_update_actions(win);
4893 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
4894 errx(1, "calloc: failed to allocate memory for new window");
4898 /* see if we need to override the workspace */
4899 p = find_pid(window_get_pid(id));
4901 /* Get all the window data in one shot */
4902 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4904 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
4905 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
4907 XGetWindowAttributes(display, id, &win->wa);
4908 XGetWMNormalHints(display, id, &win->sh, &mask);
4909 win->hints = XGetWMHints(display, id);
4910 XGetTransientForHint(display, id, &trans);
4912 win->transient = trans;
4913 set_child_transient(win, &trans);
4914 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
4915 win->id, win->transient);
4918 /* get supported protocols */
4919 if (XGetWMProtocols(display, id, &prot, &n)) {
4920 for (i = 0, pp = prot; i < n; i++, pp++) {
4921 if (*pp == takefocus)
4922 win->take_focus = 1;
4924 win->can_delete = 1;
4930 win->iconic = get_iconic(win);
4933 * Figure out where to put the window. If it was previously assigned to
4934 * a workspace (either by spawn() or manually moving), and isn't
4935 * transient, * put it in the same workspace
4937 r = root_to_region(win->wa.root);
4939 ws = &r->s->ws[p->ws];
4940 TAILQ_REMOVE(&pidlist, p, entry);
4943 } else if (prop && win->transient == 0) {
4944 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
4945 ws_idx = strtonum(prop, 0, 9, &errstr);
4947 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
4950 ws = &r->s->ws[ws_idx];
4953 /* this should launch transients in the same ws as parent */
4955 if ((ww = find_window(trans)) != NULL)
4962 "fix this bug mcbride\n");
4967 /* set up the window layout */
4970 win->s = r->s; /* this never changes */
4971 if (trans && (ww = find_window(trans)))
4972 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
4974 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
4976 win->g.w = win->wa.width;
4977 win->g.h = win->wa.height;
4978 win->g.x = win->wa.x;
4979 win->g.y = win->wa.y;
4980 win->g_floatvalid = 0;
4981 win->floatmaxed = 0;
4982 win->ewmh_flags = 0;
4984 /* Set window properties so we can remember this after reincarnation */
4985 if (ws_idx_atom && prop == NULL &&
4986 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
4987 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
4989 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
4990 PropModeReplace, ws_idx_str, SWM_PROPLEN);
4995 ewmh_autoquirk(win);
4997 if (XGetClassHint(display, win->id, &win->ch)) {
4998 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
4999 win->ch.res_class, win->ch.res_name);
5001 /* java is retarded so treat it special */
5002 if (strstr(win->ch.res_name, "sun-awt")) {
5007 for (i = 0; i < quirks_length; i++){
5008 if (!strcmp(win->ch.res_class, quirks[i].class) &&
5009 !strcmp(win->ch.res_name, quirks[i].name)) {
5010 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
5011 win->ch.res_class, win->ch.res_name);
5012 if (quirks[i].quirk & SWM_Q_FLOAT) {
5016 win->quirks = quirks[i].quirk;
5021 /* alter window position if quirky */
5022 if (win->quirks & SWM_Q_ANYWHERE) {
5023 win->manual = 1; /* don't center the quirky windows */
5024 bzero(&wc, sizeof wc);
5026 if (bar_enabled && win->g.y < bar_height) {
5027 win->g.y = wc.y = bar_height;
5030 if (win->g.w + win->g.x > WIDTH(r)) {
5031 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
5037 /* Reset font sizes (the bruteforce way; no default keybinding). */
5038 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
5039 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5040 fake_keypress(win, XK_KP_Subtract, ShiftMask);
5041 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5042 fake_keypress(win, XK_KP_Add, ShiftMask);
5045 ewmh_get_win_state(win);
5046 ewmh_update_actions(win);
5047 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
5051 bzero(&wc, sizeof wc);
5052 wc.border_width = border_width;
5053 mask = CWBorderWidth;
5054 XConfigureWindow(display, win->id, mask, &wc);
5057 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
5058 PropertyChangeMask | StructureNotifyMask);
5060 /* floaters need to be mapped if they are in the current workspace */
5061 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
5062 XMapRaised(display, win->id);
5068 free_window(struct ws_win *win)
5070 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
5075 /* needed for restart wm */
5076 set_win_state(win, WithdrawnState);
5078 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5080 if (win->ch.res_class)
5081 XFree(win->ch.res_class);
5082 if (win->ch.res_name)
5083 XFree(win->ch.res_name);
5088 memset(win, 0xff, sizeof *win); /* XXX kill later */
5094 unmanage_window(struct ws_win *win)
5096 struct ws_win *parent;
5101 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
5103 if (win->transient) {
5104 parent = find_window(win->transient);
5106 parent->child_trans = NULL;
5109 /* focus on root just in case */
5110 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
5114 TAILQ_REMOVE(&win->ws->winlist, win, entry);
5115 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
5121 focus_magic(struct ws_win *win)
5123 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu\n", WINID(win));
5128 if (win->child_trans) {
5129 /* win = parent & has a transient so focus on that */
5131 focus_win(win->child_trans);
5132 if (win->child_trans->take_focus)
5133 client_msg(win, takefocus);
5135 /* make sure transient hasn't dissapeared */
5136 if (validate_win(win->child_trans) == 0) {
5137 focus_win(win->child_trans);
5138 if (win->child_trans->take_focus)
5139 client_msg(win->child_trans, takefocus);
5141 win->child_trans = NULL;
5143 if (win->take_focus)
5144 client_msg(win, takefocus);
5150 if (win->take_focus)
5151 client_msg(win, takefocus);
5158 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
5166 XKeyEvent *ev = &e->xkey;
5168 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
5170 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
5171 for (i = 0; i < keys_length; i++)
5172 if (keysym == keys[i].keysym
5173 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
5174 && keyfuncs[keys[i].funcid].func) {
5175 if (keys[i].funcid == kf_spawn_custom)
5177 root_to_region(ev->root),
5178 &(keyfuncs[keys[i].funcid].args),
5182 keyfuncs[keys[i].funcid].func(
5183 root_to_region(ev->root),
5184 &(keyfuncs[keys[i].funcid].args)
5190 buttonpress(XEvent *e)
5194 XButtonPressedEvent *ev = &e->xbutton;
5196 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
5198 action = root_click;
5199 if ((win = find_window(ev->window)) == NULL)
5203 action = client_click;
5205 for (i = 0; i < LENGTH(buttons); i++)
5206 if (action == buttons[i].action && buttons[i].func &&
5207 buttons[i].button == ev->button &&
5208 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
5209 buttons[i].func(win, &buttons[i].args);
5213 configurerequest(XEvent *e)
5215 XConfigureRequestEvent *ev = &e->xconfigurerequest;
5220 if ((win = find_window(ev->window)) == NULL)
5221 if ((win = find_unmanaged_window(ev->window)) == NULL)
5225 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
5227 bzero(&wc, sizeof wc);
5230 wc.width = ev->width;
5231 wc.height = ev->height;
5232 wc.border_width = ev->border_width;
5233 wc.sibling = ev->above;
5234 wc.stack_mode = ev->detail;
5235 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
5237 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
5239 config_win(win, ev);
5244 configurenotify(XEvent *e)
5249 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
5250 e->xconfigure.window);
5252 win = find_window(e->xconfigure.window);
5254 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5258 if (focus_mode == SWM_FOCUS_DEFAULT)
5259 drain_enter_notify();
5264 destroynotify(XEvent *e)
5267 XDestroyWindowEvent *ev = &e->xdestroywindow;
5269 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
5271 if ((win = find_window(ev->window)) == NULL) {
5272 if ((win = find_unmanaged_window(ev->window)) == NULL)
5278 /* make sure we focus on something */
5281 unmanage_window(win);
5283 if (focus_mode == SWM_FOCUS_DEFAULT)
5284 drain_enter_notify();
5289 enternotify(XEvent *e)
5291 XCrossingEvent *ev = &e->xcrossing;
5296 Window focus_return;
5297 int revert_to_return;
5299 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
5300 "%lu subwindow %lu same_screen %d focus %d state %d\n",
5301 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
5302 ev->same_screen, ev->focus, ev->state);
5304 switch (focus_mode) {
5305 case SWM_FOCUS_DEFAULT:
5307 case SWM_FOCUS_FOLLOW:
5309 case SWM_FOCUS_SYNERGY:
5312 * all these checks need to be in this order because the
5313 * XCheckTypedWindowEvent relies on weeding out the previous events
5315 * making this code an option would enable a follow mouse for focus
5320 * state is set when we are switching workspaces and focus is set when
5321 * the window or a subwindow already has focus (occurs during restart).
5323 * Only honor the focus flag if last_focus_event is not FocusOut,
5324 * this allows scrotwm to continue to control focus when another
5325 * program is also playing with it.
5327 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
5328 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
5333 * happens when a window is created or destroyed and the border
5334 * crosses the mouse pointer and when switching ws
5336 * we need the subwindow test to see if we came from root in order
5337 * to give focus to floaters
5339 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
5340 ev->subwindow == 0) {
5341 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
5345 /* this window already has focus */
5346 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
5347 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
5351 /* this window is being deleted or moved to another ws */
5352 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
5354 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
5355 XPutBackEvent(display, &cne);
5359 if ((win = find_window(ev->window)) == NULL) {
5360 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5365 * In fullstack kill all enters unless they come from a different ws
5366 * (i.e. another region) or focus has been grabbed externally.
5368 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
5369 last_focus_event != FocusOut) {
5370 XGetInputFocus(display, &focus_return, &revert_to_return);
5371 if ((w = find_window(focus_return)) == NULL ||
5373 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
5381 if ((win = find_window(ev->window)) == NULL) {
5382 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5387 * if we have more enternotifies let them handle it in due time
5389 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
5390 DNPRINTF(SWM_D_EVENT,
5391 "ignoring enternotify: got more enternotify\n");
5392 XPutBackEvent(display, &cne);
5399 /* lets us use one switch statement for arbitrary mode/detail combinations */
5400 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
5403 focusevent(XEvent *e)
5407 u_int32_t mode_detail;
5408 XFocusChangeEvent *ev = &e->xfocus;
5410 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
5411 ev->type == FocusIn ? "entering" : "leaving",
5412 ev->window, ev->mode, ev->detail);
5414 if (last_focus_event == ev->type) {
5415 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
5419 last_focus_event = ev->type;
5420 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
5422 switch (mode_detail) {
5423 /* synergy client focus operations */
5424 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
5425 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
5427 /* synergy server focus operations */
5428 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
5430 /* Entering applications like rdesktop that mangle the pointer */
5431 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
5433 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
5434 XSetWindowBorder(display, win->id,
5435 win->ws->r->s->c[ev->type == FocusIn ?
5436 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
5439 fprintf(stderr, "ignoring focusevent\n");
5440 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
5447 mapnotify(XEvent *e)
5450 XMapEvent *ev = &e->xmap;
5452 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
5454 win = manage_window(ev->window);
5456 set_win_state(win, NormalState);
5460 mappingnotify(XEvent *e)
5462 XMappingEvent *ev = &e->xmapping;
5464 XRefreshKeyboardMapping(ev);
5465 if (ev->request == MappingKeyboard)
5470 maprequest(XEvent *e)
5473 struct swm_region *r;
5474 XWindowAttributes wa;
5475 XMapRequestEvent *ev = &e->xmaprequest;
5477 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
5478 e->xmaprequest.window);
5480 if (!XGetWindowAttributes(display, ev->window, &wa))
5482 if (wa.override_redirect)
5485 win = manage_window(e->xmaprequest.window);
5487 return; /* can't happen */
5491 /* make new win focused */
5492 r = root_to_region(win->wa.root);
5493 if (win->ws == r->ws)
5498 propertynotify(XEvent *e)
5501 XPropertyEvent *ev = &e->xproperty;
5503 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
5506 win = find_window(ev->window);
5510 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
5511 update_iconic(win, 0);
5512 XMapRaised(display, win->id);
5519 case XA_WM_NORMAL_HINTS:
5522 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5523 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
5524 if (win->sh.flags & PMinSize) {
5525 win->g.w = win->sh.min_width;
5526 win->g.h = win->sh.min_height;
5527 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
5529 XMoveResizeWindow(display, win->id,
5530 win->g.x, win->g.y, win->g.w, win->g.h);
5532 if (window_name_enabled)
5541 unmapnotify(XEvent *e)
5545 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
5547 /* determine if we need to help unmanage this window */
5548 win = find_window(e->xunmap.window);
5552 if (getstate(e->xunmap.window) == NormalState) {
5553 unmanage_window(win);
5556 /* giant hack for apps that don't destroy transient windows */
5557 /* eat a bunch of events to prevent remanaging the window */
5559 while (XCheckWindowEvent(display, e->xunmap.window,
5560 EnterWindowMask, &cne))
5562 while (XCheckWindowEvent(display, e->xunmap.window,
5563 StructureNotifyMask, &cne))
5565 while (XCheckWindowEvent(display, e->xunmap.window,
5566 SubstructureNotifyMask, &cne))
5568 /* resend unmap because we ated it */
5569 XUnmapWindow(display, e->xunmap.window);
5572 if (focus_mode == SWM_FOCUS_DEFAULT)
5573 drain_enter_notify();
5577 visibilitynotify(XEvent *e)
5580 struct swm_region *r;
5582 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
5583 e->xvisibility.window);
5584 if (e->xvisibility.state == VisibilityUnobscured)
5585 for (i = 0; i < ScreenCount(display); i++)
5586 TAILQ_FOREACH(r, &screens[i].rl, entry)
5587 if (e->xvisibility.window == r->bar_window)
5592 clientmessage(XEvent *e)
5594 XClientMessageEvent *ev;
5599 win = find_window(ev->window);
5603 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx type: %ld \n",
5604 ev->window, ev->message_type);
5606 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
5607 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW \n");
5610 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
5611 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW \n");
5612 if (win->can_delete)
5613 client_msg(win, adelete);
5615 XKillClient(display, win->id);
5617 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
5618 DNPRINTF(SWM_D_EVENT,
5619 "clientmessage: _NET_MOVERESIZE_WINDOW \n");
5620 if (win->floating) {
5621 if (ev->data.l[0] & (1<<8)) /* x */
5622 win->g.x = ev->data.l[1];
5623 if (ev->data.l[0] & (1<<9)) /* y */
5624 win->g.y = ev->data.l[2];
5625 if (ev->data.l[0] & (1<<10)) /* width */
5626 win->g.w = ev->data.l[3];
5627 if (ev->data.l[0] & (1<<11)) /* height */
5628 win->g.h = ev->data.l[4];
5631 /* TODO: Change stack sizes */
5633 config_win(win, NULL);
5635 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
5636 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE \n");
5637 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
5639 ewmh_update_win_state(win, ev->data.l[2],
5647 xerror_start(Display *d, XErrorEvent *ee)
5654 xerror(Display *d, XErrorEvent *ee)
5656 /* fprintf(stderr, "error: %p %p\n", display, ee); */
5664 xerrorxlib = XSetErrorHandler(xerror_start);
5666 /* this causes an error if some other window manager is running */
5667 XSelectInput(display, DefaultRootWindow(display),
5668 SubstructureRedirectMask);
5669 XSync(display, False);
5673 XSetErrorHandler(xerror);
5674 XSync(display, False);
5679 new_region(struct swm_screen *s, int x, int y, int w, int h)
5681 struct swm_region *r, *n;
5682 struct workspace *ws = NULL;
5685 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
5686 s->idx, w, h, x, y);
5688 /* remove any conflicting regions */
5689 n = TAILQ_FIRST(&s->rl);
5692 n = TAILQ_NEXT(r, entry);
5693 if (X(r) < (x + w) &&
5694 (X(r) + WIDTH(r)) > x &&
5696 (Y(r) + HEIGHT(r)) > y) {
5697 if (r->ws->r != NULL)
5698 r->ws->old_r = r->ws->r;
5700 XDestroyWindow(display, r->bar_window);
5701 TAILQ_REMOVE(&s->rl, r, entry);
5702 TAILQ_INSERT_TAIL(&s->orl, r, entry);
5706 /* search old regions for one to reuse */
5708 /* size + location match */
5709 TAILQ_FOREACH(r, &s->orl, entry)
5710 if (X(r) == x && Y(r) == y &&
5711 HEIGHT(r) == h && WIDTH(r) == w)
5715 TAILQ_FOREACH(r, &s->orl, entry)
5716 if (HEIGHT(r) == h && WIDTH(r) == w)
5720 TAILQ_REMOVE(&s->orl, r, entry);
5721 /* try to use old region's workspace */
5722 if (r->ws->r == NULL)
5725 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
5726 errx(1, "calloc: failed to allocate memory for screen");
5728 /* if we don't have a workspace already, find one */
5730 for (i = 0; i < SWM_WS_MAX; i++)
5731 if (s->ws[i].r == NULL) {
5738 errx(1, "no free workspaces\n");
5749 TAILQ_INSERT_TAIL(&s->rl, r, entry);
5755 #ifdef SWM_XRR_HAS_CRTC
5757 XRRScreenResources *sr;
5760 #endif /* SWM_XRR_HAS_CRTC */
5761 struct swm_region *r;
5764 if (i >= ScreenCount(display))
5765 errx(1, "invalid screen");
5767 /* remove any old regions */
5768 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
5769 r->ws->old_r = r->ws->r = NULL;
5770 XDestroyWindow(display, r->bar_window);
5771 TAILQ_REMOVE(&screens[i].rl, r, entry);
5772 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
5776 /* map virtual screens onto physical screens */
5777 #ifdef SWM_XRR_HAS_CRTC
5778 if (xrandr_support) {
5779 sr = XRRGetScreenResources(display, screens[i].root);
5781 new_region(&screens[i], 0, 0,
5782 DisplayWidth(display, i),
5783 DisplayHeight(display, i));
5787 for (c = 0, ci = NULL; c < ncrtc; c++) {
5788 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
5789 if (ci->noutput == 0)
5792 if (ci != NULL && ci->mode == None)
5793 new_region(&screens[i], 0, 0,
5794 DisplayWidth(display, i),
5795 DisplayHeight(display, i));
5797 new_region(&screens[i],
5798 ci->x, ci->y, ci->width, ci->height);
5801 XRRFreeCrtcInfo(ci);
5802 XRRFreeScreenResources(sr);
5804 #endif /* SWM_XRR_HAS_CRTC */
5806 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
5807 DisplayHeight(display, i));
5812 screenchange(XEvent *e) {
5813 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
5814 struct swm_region *r;
5817 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
5819 if (!XRRUpdateConfiguration(e))
5822 /* silly event doesn't include the screen index */
5823 for (i = 0; i < ScreenCount(display); i++)
5824 if (screens[i].root == xe->root)
5826 if (i >= ScreenCount(display))
5827 errx(1, "screenchange: screen not found\n");
5829 /* brute force for now, just re-enumerate the regions */
5832 /* add bars to all regions */
5833 for (i = 0; i < ScreenCount(display); i++)
5834 TAILQ_FOREACH(r, &screens[i].rl, entry)
5837 if (focus_mode == SWM_FOCUS_DEFAULT)
5838 drain_enter_notify();
5844 Window d1, d2, *wins = NULL;
5845 XWindowAttributes wa;
5850 for (i = 0; i < ScreenCount(display); i++) {
5851 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
5854 /* attach windows to a region */
5855 /* normal windows */
5856 for (j = 0; j < no; j++) {
5857 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5858 wa.override_redirect ||
5859 XGetTransientForHint(display, wins[j], &d1))
5862 state = getstate(wins[j]);
5863 manage = state == IconicState;
5864 if (wa.map_state == IsViewable || manage)
5865 manage_window(wins[j]);
5867 /* transient windows */
5868 for (j = 0; j < no; j++) {
5869 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5870 wa.override_redirect)
5873 state = getstate(wins[j]);
5874 manage = state == IconicState;
5875 if (XGetTransientForHint(display, wins[j], &d1) &&
5877 manage_window(wins[j]);
5890 int errorbase, major, minor;
5891 struct workspace *ws;
5893 if ((screens = calloc(ScreenCount(display),
5894 sizeof(struct swm_screen))) == NULL)
5895 errx(1, "calloc: screens");
5897 /* initial Xrandr setup */
5898 xrandr_support = XRRQueryExtension(display,
5899 &xrandr_eventbase, &errorbase);
5901 if (XRRQueryVersion(display, &major, &minor) && major < 1)
5904 /* map physical screens */
5905 for (i = 0; i < ScreenCount(display); i++) {
5906 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
5908 TAILQ_INIT(&screens[i].rl);
5909 TAILQ_INIT(&screens[i].orl);
5910 screens[i].root = RootWindow(display, i);
5912 /* set default colors */
5913 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
5914 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
5915 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
5916 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
5917 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
5919 /* set default cursor */
5920 XDefineCursor(display, screens[i].root,
5921 XCreateFontCursor(display, XC_left_ptr));
5923 /* init all workspaces */
5924 /* XXX these should be dynamically allocated too */
5925 for (j = 0; j < SWM_WS_MAX; j++) {
5926 ws = &screens[i].ws[j];
5931 TAILQ_INIT(&ws->winlist);
5932 TAILQ_INIT(&ws->unmanagedlist);
5934 for (k = 0; layouts[k].l_stack != NULL; k++)
5935 if (layouts[k].l_config != NULL)
5936 layouts[k].l_config(ws,
5937 SWM_ARG_ID_STACKINIT);
5938 ws->cur_layout = &layouts[0];
5944 XRRSelectInput(display, screens[i].root,
5945 RRScreenChangeNotifyMask);
5952 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
5954 err(1, "setup_globals: strdup");
5955 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
5957 err(1, "setup_globals: strdup");
5958 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
5960 err(1, "setup_globals: strdup");
5961 if ((spawn_term[0] = strdup("xterm")) == NULL)
5962 err(1, "setup_globals: strdup");
5963 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
5971 Atom netwmcheck, netwmname, utf8_string;
5974 /* work around sun jdk bugs, code from wmname */
5975 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
5976 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
5977 utf8_string = XInternAtom(display, "UTF8_STRING", False);
5978 for (i = 0; i < ScreenCount(display); i++) {
5979 root = screens[i].root;
5980 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
5981 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
5982 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
5984 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
5985 PropModeReplace, (unsigned char *)&win,1);
5986 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
5987 PropModeReplace, (unsigned char *)&win,1);
5988 XChangeProperty(display, win, netwmname, utf8_string, 8,
5989 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
5994 main(int argc, char *argv[])
5997 struct swm_region *r, *rr;
5998 struct ws_win *winfocus = NULL;
6001 char conf[PATH_MAX], *cfile = NULL;
6006 struct sigaction sact;
6009 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
6010 SWM_VERSION, cvstag);
6011 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
6012 warnx("no locale support");
6014 if (!(display = XOpenDisplay(0)))
6015 errx(1, "can not open display");
6018 errx(1, "other wm running");
6020 /* handle some signals */
6021 bzero(&sact, sizeof(sact));
6022 sigemptyset(&sact.sa_mask);
6024 sact.sa_handler = sighdlr;
6025 sigaction(SIGINT, &sact, NULL);
6026 sigaction(SIGQUIT, &sact, NULL);
6027 sigaction(SIGTERM, &sact, NULL);
6028 sigaction(SIGHUP, &sact, NULL);
6030 sact.sa_handler = sighdlr;
6031 sact.sa_flags = SA_NOCLDSTOP;
6032 sigaction(SIGCHLD, &sact, NULL);
6034 astate = XInternAtom(display, "WM_STATE", False);
6035 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
6036 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
6037 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
6038 a_wmname = XInternAtom(display, "WM_NAME", False);
6039 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
6040 a_string = XInternAtom(display, "STRING", False);
6041 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
6043 /* look for local and global conf file */
6044 pwd = getpwuid(getuid());
6046 errx(1, "invalid user %d", getuid());
6055 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
6056 if (stat(conf, &sb) != -1) {
6057 if (S_ISREG(sb.st_mode))
6060 /* try global conf file */
6061 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
6062 if (!stat(conf, &sb))
6063 if (S_ISREG(sb.st_mode))
6070 /* set some values to work around bad programs */
6073 /* grab existing windows (before we build the bars) */
6076 if (getenv("SWM_STARTED") == NULL)
6077 setenv("SWM_STARTED", "YES", 1);
6079 /* setup all bars */
6080 for (i = 0; i < ScreenCount(display); i++)
6081 TAILQ_FOREACH(r, &screens[i].rl, entry) {
6082 if (winfocus == NULL)
6083 winfocus = TAILQ_FIRST(&r->ws->winlist);
6091 if (focus_mode == SWM_FOCUS_DEFAULT)
6092 drain_enter_notify();
6094 xfd = ConnectionNumber(display);
6096 while (XPending(display)) {
6097 XNextEvent(display, &e);
6100 if (e.type < LASTEvent) {
6102 if (handler[e.type])
6103 handler[e.type](&e);
6105 DNPRINTF(SWM_D_EVENT,
6106 "win: %lu unknown event: %d\n",
6107 e.xany.window, e.type);
6109 switch (e.type - xrandr_eventbase) {
6110 case RRScreenChangeNotify:
6114 DNPRINTF(SWM_D_EVENT,
6115 "win: %lu unknown xrandr event: "
6116 "%d\n", e.xany.window, e.type);
6122 /* if we are being restarted go focus on first window */
6124 rr = winfocus->ws->r;
6126 /* not a visible window */
6130 /* move pointer to first screen if multi screen */
6131 if (ScreenCount(display) > 1 || outputs > 1)
6132 XWarpPointer(display, None, rr->s[0].root,
6133 0, 0, 0, 0, rr->g.x,
6134 rr->g.y + (bar_enabled ? bar_height : 0));
6136 a.id = SWM_ARG_ID_FOCUSCUR;
6146 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
6148 DNPRINTF(SWM_D_MISC, "select failed");
6149 if (restart_wm == 1)
6150 restart(NULL, NULL);
6151 if (search_resp == 1)
6163 XFreeGC(display, bar_gc);
6164 XCloseDisplay(display);