3 * Copyright (c) 2009-2010-2011 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009-2010-2011 Ryan McBride <mcbride@countersiege.com>
5 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
6 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
7 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
8 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Much code and ideas taken from dwm under the following license:
24 * MIT/X Consortium License
26 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
27 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
28 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
29 * 2007 Premysl Hruby <dfenze at gmail dot com>
30 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
31 * 2007 Christof Musik <christof at sendfax dot de>
32 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
33 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
34 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
36 * Permission is hereby granted, free of charge, to any person obtaining a
37 * copy of this software and associated documentation files (the "Software"),
38 * to deal in the Software without restriction, including without limitation
39 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
40 * and/or sell copies of the Software, and to permit persons to whom the
41 * Software is furnished to do so, subject to the following conditions:
43 * The above copyright notice and this permission notice shall be included in
44 * all copies or substantial portions of the Software.
46 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
49 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
50 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
51 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
52 * DEALINGS IN THE SOFTWARE.
55 static const char *cvstag =
58 #define SWM_VERSION "0.9.34"
75 #include <sys/types.h>
79 #include <sys/queue.h>
80 #include <sys/param.h>
81 #include <sys/select.h>
83 #include <X11/cursorfont.h>
84 #include <X11/keysym.h>
85 #include <X11/Xatom.h>
87 #include <X11/Xproto.h>
88 #include <X11/Xutil.h>
89 #include <X11/extensions/Xrandr.h>
90 #include <X11/extensions/XTest.h>
97 # error XRandR versions less than 1.0 are not supported
102 #define SWM_XRR_HAS_CRTC
106 /*#define SWM_DEBUG*/
108 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
109 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
110 #define SWM_D_MISC 0x0001
111 #define SWM_D_EVENT 0x0002
112 #define SWM_D_WS 0x0004
113 #define SWM_D_FOCUS 0x0008
114 #define SWM_D_MOVE 0x0010
115 #define SWM_D_STACK 0x0020
116 #define SWM_D_MOUSE 0x0040
117 #define SWM_D_PROP 0x0080
118 #define SWM_D_CLASS 0x0100
119 #define SWM_D_KEY 0x0200
120 #define SWM_D_QUIRK 0x0400
121 #define SWM_D_SPAWN 0x0800
122 #define SWM_D_EVENTQ 0x1000
123 #define SWM_D_CONF 0x2000
125 u_int32_t swm_debug = 0
142 #define DPRINTF(x...)
143 #define DNPRINTF(n,x...)
146 #define LENGTH(x) (sizeof x / sizeof x[0])
147 #define MODKEY Mod1Mask
148 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
149 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
150 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
151 #define SWM_PROPLEN (16)
152 #define SWM_FUNCNAME_LEN (32)
153 #define SWM_KEYS_LEN (255)
154 #define SWM_QUIRK_LEN (64)
155 #define X(r) (r)->g.x
156 #define Y(r) (r)->g.y
157 #define WIDTH(r) (r)->g.w
158 #define HEIGHT(r) (r)->g.h
159 #define SWM_MAX_FONT_STEPS (3)
160 #define WINID(w) (w ? w->id : 0)
162 #define SWM_FOCUS_DEFAULT (0)
163 #define SWM_FOCUS_SYNERGY (1)
164 #define SWM_FOCUS_FOLLOW (2)
167 #define SWM_LIB "/usr/local/lib/libswmhack.so"
179 volatile sig_atomic_t running = 1;
180 volatile sig_atomic_t restart_wm = 0;
182 int last_focus_event = FocusOut;
183 int (*xerrorxlib)(Display *, XErrorEvent *);
187 int xrandr_eventbase;
188 unsigned int numlockmask = 0;
192 int cycle_visible = 0;
194 int font_adjusted = 0;
195 unsigned int mod_key = MODKEY;
198 struct swm_region *search_r;
199 int select_list_pipe[2];
200 int select_resp_pipe[2];
202 volatile sig_atomic_t search_resp;
205 double dialog_ratio = .6;
207 #define SWM_BAR_MAX (256)
208 char *bar_argv[] = { NULL, NULL };
210 char bar_ext[SWM_BAR_MAX];
211 char bar_vertext[SWM_BAR_MAX];
213 sig_atomic_t bar_alarm = 0;
216 int bar_border_width = 1;
217 int bar_at_bottom = 0;
219 int bar_extra_running = 0;
222 int stack_enabled = 1;
223 int clock_enabled = 1;
224 int urgent_enabled = 0;
225 char *clock_format = NULL;
226 int title_name_enabled = 0;
227 int title_class_enabled = 0;
228 int window_name_enabled = 0;
229 int focus_mode = SWM_FOCUS_DEFAULT;
230 int disable_border = 0;
231 int border_width = 1;
232 int verbose_layout = 0;
238 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
239 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
241 #define SWM_MENU_FN (2)
242 #define SWM_MENU_NB (4)
243 #define SWM_MENU_NF (6)
244 #define SWM_MENU_SB (8)
245 #define SWM_MENU_SF (10)
247 /* layout manager data */
248 struct swm_geometry {
258 /* virtual "screens" */
260 TAILQ_ENTRY(swm_region) entry;
261 struct swm_geometry g;
262 struct workspace *ws; /* current workspace on this region */
263 struct workspace *ws_prior; /* prior workspace on this region */
264 struct swm_screen *s; /* screen idx */
267 TAILQ_HEAD(swm_region_list, swm_region);
270 TAILQ_ENTRY(ws_win) entry;
273 struct ws_win *child_trans; /* transient child window */
274 struct swm_geometry g; /* current geometry */
275 struct swm_geometry g_float; /* geometry when floating */
276 struct swm_geometry rg_float; /* region geom when floating */
277 int g_floatvalid; /* flag: geometry in g_float is valid */
278 int floatmaxed; /* flag: floater was maxed in max_stack */
282 unsigned int ewmh_flags;
283 int font_size_boundary[SWM_MAX_FONT_STEPS];
289 unsigned long quirks;
290 struct workspace *ws; /* always valid */
291 struct swm_screen *s; /* always valid, never changes */
292 XWindowAttributes wa;
297 TAILQ_HEAD(ws_win_list, ws_win);
301 TAILQ_ENTRY(pid_e) entry;
305 TAILQ_HEAD(pid_list, pid_e);
306 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
308 /* layout handlers */
310 void vertical_config(struct workspace *, int);
311 void vertical_stack(struct workspace *, struct swm_geometry *);
312 void horizontal_config(struct workspace *, int);
313 void horizontal_stack(struct workspace *, struct swm_geometry *);
314 void max_stack(struct workspace *, struct swm_geometry *);
315 void plain_stacker(struct workspace *);
316 void fancy_stacker(struct workspace *);
318 struct ws_win *find_window(Window);
320 void grabbuttons(struct ws_win *, int);
321 void new_region(struct swm_screen *, int, int, int, int);
322 void unmanage_window(struct ws_win *);
323 long getstate(Window);
326 void (*l_stack)(struct workspace *, struct swm_geometry *);
327 void (*l_config)(struct workspace *, int);
329 #define SWM_L_FOCUSPREV (1<<0)
330 #define SWM_L_MAPONFOCUS (1<<1)
331 void (*l_string)(struct workspace *);
333 /* stack, configure */
334 { vertical_stack, vertical_config, 0, plain_stacker },
335 { horizontal_stack, horizontal_config, 0, plain_stacker },
337 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
338 { NULL, NULL, 0, NULL },
341 /* position of max_stack mode in the layouts array, index into layouts! */
342 #define SWM_V_STACK (0)
343 #define SWM_H_STACK (1)
344 #define SWM_MAX_STACK (2)
346 #define SWM_H_SLICE (32)
347 #define SWM_V_SLICE (32)
349 /* define work spaces */
351 int idx; /* workspace index */
352 int always_raise; /* raise windows on focus */
353 struct layout *cur_layout; /* current layout handlers */
354 struct ws_win *focus; /* may be NULL */
355 struct ws_win *focus_prev; /* may be NULL */
356 struct swm_region *r; /* may be NULL */
357 struct swm_region *old_r; /* may be NULL */
358 struct ws_win_list winlist; /* list of windows in ws */
359 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
360 char stacker[10]; /* display stacker and layout */
364 int horizontal_msize;
366 int horizontal_stacks;
373 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
374 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
376 /* physical screen mapping */
377 #define SWM_WS_MAX (10)
379 int idx; /* screen index */
380 struct swm_region_list rl; /* list of regions on this screen */
381 struct swm_region_list orl; /* list of old regions */
383 struct workspace ws[SWM_WS_MAX];
389 } c[SWM_S_COLOR_MAX];
391 struct swm_screen *screens;
394 /* args to functions */
397 #define SWM_ARG_ID_FOCUSNEXT (0)
398 #define SWM_ARG_ID_FOCUSPREV (1)
399 #define SWM_ARG_ID_FOCUSMAIN (2)
400 #define SWM_ARG_ID_FOCUSCUR (4)
401 #define SWM_ARG_ID_SWAPNEXT (10)
402 #define SWM_ARG_ID_SWAPPREV (11)
403 #define SWM_ARG_ID_SWAPMAIN (12)
404 #define SWM_ARG_ID_MOVELAST (13)
405 #define SWM_ARG_ID_MASTERSHRINK (20)
406 #define SWM_ARG_ID_MASTERGROW (21)
407 #define SWM_ARG_ID_MASTERADD (22)
408 #define SWM_ARG_ID_MASTERDEL (23)
409 #define SWM_ARG_ID_STACKRESET (30)
410 #define SWM_ARG_ID_STACKINIT (31)
411 #define SWM_ARG_ID_CYCLEWS_UP (40)
412 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
413 #define SWM_ARG_ID_CYCLESC_UP (42)
414 #define SWM_ARG_ID_CYCLESC_DOWN (43)
415 #define SWM_ARG_ID_STACKINC (50)
416 #define SWM_ARG_ID_STACKDEC (51)
417 #define SWM_ARG_ID_SS_ALL (60)
418 #define SWM_ARG_ID_SS_WINDOW (61)
419 #define SWM_ARG_ID_DONTCENTER (70)
420 #define SWM_ARG_ID_CENTER (71)
421 #define SWM_ARG_ID_KILLWINDOW (80)
422 #define SWM_ARG_ID_DELETEWINDOW (81)
426 void focus(struct swm_region *, union arg *);
427 void focus_magic(struct ws_win *);
434 #define SWM_Q_FLOAT (1<<0) /* float this window */
435 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
436 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
437 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
438 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
439 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
441 int quirks_size = 0, quirks_length = 0;
442 struct quirk *quirks = NULL;
445 * Supported EWMH hints should be added to
446 * both the enum and the ewmh array
448 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
449 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
450 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
451 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
452 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
453 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
454 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
455 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
456 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
457 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
463 } ewmh[SWM_EWMH_HINT_MAX] = {
464 /* must be in same order as in the enum */
465 {"_NET_ACTIVE_WINDOW", None},
466 {"_NET_MOVERESIZE_WINDOW", None},
467 {"_NET_CLOSE_WINDOW", None},
468 {"_NET_WM_WINDOW_TYPE", None},
469 {"_NET_WM_WINDOW_TYPE_DOCK", None},
470 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
471 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
472 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
473 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
474 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
475 {"_NET_WM_STATE", None},
476 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
477 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
478 {"_NET_WM_STATE_SKIP_TASKBAR", None},
479 {"_NET_WM_STATE_SKIP_PAGER", None},
480 {"_NET_WM_STATE_HIDDEN", None},
481 {"_NET_WM_STATE_ABOVE", None},
482 {"_SWM_WM_STATE_MANUAL", None},
483 {"_NET_WM_STATE_FULLSCREEN", None},
484 {"_NET_WM_ALLOWED_ACTIONS", None},
485 {"_NET_WM_ACTION_MOVE", None},
486 {"_NET_WM_ACTION_RESIZE", None},
487 {"_NET_WM_ACTION_FULLSCREEN", None},
488 {"_NET_WM_ACTION_CLOSE", None},
491 void store_float_geom(struct ws_win *win, struct swm_region *r);
492 int floating_toggle_win(struct ws_win *win);
493 void spawn_select(struct swm_region *, union arg *, char *, int *);
496 get_property(Window id, Atom atom, long count, Atom type,
497 unsigned long *n, unsigned char **data)
500 unsigned long tmp, extra;
501 unsigned long *nitems;
504 nitems = n != NULL ? n : &tmp;
505 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
506 &real, &format, nitems, &extra, data);
508 if (status != Success)
517 update_iconic(struct ws_win *win, int newv)
524 iprop = XInternAtom(display, "_SWM_ICONIC", False);
528 XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
529 PropModeReplace, (unsigned char *)&v, 1);
531 XDeleteProperty(display, win->id, iprop);
535 get_iconic(struct ws_win *win)
540 unsigned long nitems, extra;
541 unsigned char *prop = NULL;
543 iprop = XInternAtom(display, "_SWM_ICONIC", False);
546 status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
547 False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
548 if (status != Success)
550 if (rettype != XA_INTEGER || retfmt != 32)
554 v = *((int32_t *)prop);
568 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
570 for (i = 0; i < LENGTH(ewmh); i++)
571 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
573 for (i = 0; i < ScreenCount(display); i++) {
574 /* Support check window will be created by workaround(). */
576 /* Report supported atoms */
577 XDeleteProperty(display, screens[i].root, sup_list);
578 for (j = 0; j < LENGTH(ewmh); j++)
579 XChangeProperty(display, screens[i].root,
580 sup_list, XA_ATOM, 32,
581 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
589 unsigned char *data = NULL;
591 Atom sup_check, sup_list;
594 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
595 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
597 for (i = 0; i < ScreenCount(display); i++) {
598 /* Get the support check window and destroy it */
599 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
604 XDestroyWindow(display, id);
605 XDeleteProperty(display, screens[i].root, sup_check);
606 XDeleteProperty(display, screens[i].root, sup_list);
614 ewmh_autoquirk(struct ws_win *win)
617 unsigned long *data = NULL, n;
620 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
621 XA_ATOM, &n, (void *)&data);
628 for (i = 0; i < n; i++) {
630 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
632 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
633 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
634 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
636 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
639 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
640 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
642 win->quirks = SWM_Q_FLOAT;
650 #define SWM_EWMH_ACTION_COUNT_MAX (6)
651 #define EWMH_F_FULLSCREEN (1<<0)
652 #define EWMH_F_ABOVE (1<<1)
653 #define EWMH_F_HIDDEN (1<<2)
654 #define EWMH_F_SKIP_PAGER (1<<3)
655 #define EWMH_F_SKIP_TASKBAR (1<<4)
656 #define SWM_F_MANUAL (1<<5)
659 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
661 struct swm_geometry rg;
669 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
675 store_float_geom(win, win->ws->r);
682 if (win->g_floatvalid) {
683 /* refloat at last floating relative position */
684 win->g.x = win->g_float.x - win->rg_float.x + rg.x;
685 win->g.y = win->g_float.y - win->rg_float.y + rg.y;
686 win->g.w = win->g_float.w;
687 win->g.h = win->g_float.h;
695 ewmh_update_actions(struct ws_win *win)
697 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
703 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
706 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
707 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
710 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
711 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
714 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
715 #define _NET_WM_STATE_ADD 1 /* add/set property */
716 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
719 ewmh_update_win_state(struct ws_win *win, long state, long action)
721 unsigned int mask = 0;
722 unsigned int changed = 0;
723 unsigned int orig_flags;
728 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
729 mask = EWMH_F_FULLSCREEN;
730 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
732 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
734 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
735 mask = EWMH_F_SKIP_PAGER;
736 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
737 mask = EWMH_F_SKIP_TASKBAR;
740 orig_flags = win->ewmh_flags;
743 case _NET_WM_STATE_REMOVE:
744 win->ewmh_flags &= ~mask;
746 case _NET_WM_STATE_ADD:
747 win->ewmh_flags |= mask;
749 case _NET_WM_STATE_TOGGLE:
750 win->ewmh_flags ^= mask;
754 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
756 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
758 if (!floating_toggle_win(win))
759 win->ewmh_flags = orig_flags; /* revert */
760 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
762 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
763 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
765 if (!ewmh_set_win_fullscreen(win,
766 win->ewmh_flags & EWMH_F_FULLSCREEN))
767 win->ewmh_flags = orig_flags; /* revert */
769 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
771 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
772 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
773 XA_ATOM, 32, PropModeAppend,
774 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
775 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
776 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
777 XA_ATOM, 32, PropModeAppend,
778 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
779 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
780 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
781 XA_ATOM, 32, PropModeAppend,
782 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
783 if (win->ewmh_flags & EWMH_F_ABOVE)
784 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
785 XA_ATOM, 32, PropModeAppend,
786 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
787 if (win->ewmh_flags & SWM_F_MANUAL)
788 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
789 XA_ATOM, 32, PropModeAppend,
790 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
794 ewmh_get_win_state(struct ws_win *win)
805 win->ewmh_flags |= EWMH_F_ABOVE;
807 win->ewmh_flags |= SWM_F_MANUAL;
809 success = get_property(win->id, ewmh[_NET_WM_STATE].atom,
810 (~0L), XA_ATOM, &n, (void *)&states);
815 for (i = 0; i < n; i++)
816 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
836 name = "ButtonPress";
839 name = "ButtonRelease";
842 name = "MotionNotify";
845 name = "EnterNotify";
848 name = "LeaveNotify";
857 name = "KeymapNotify";
863 name = "GraphicsExpose";
868 case VisibilityNotify:
869 name = "VisibilityNotify";
872 name = "CreateNotify";
875 name = "DestroyNotify";
878 name = "UnmapNotify";
887 name = "ReparentNotify";
889 case ConfigureNotify:
890 name = "ConfigureNotify";
892 case ConfigureRequest:
893 name = "ConfigureRequest";
896 name = "GravityNotify";
899 name = "ResizeRequest";
901 case CirculateNotify:
902 name = "CirculateNotify";
904 case CirculateRequest:
905 name = "CirculateRequest";
908 name = "PropertyNotify";
911 name = "SelectionClear";
913 case SelectionRequest:
914 name = "SelectionRequest";
916 case SelectionNotify:
917 name = "SelectionNotify";
920 name = "ColormapNotify";
923 name = "ClientMessage";
926 name = "MappingNotify";
931 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
933 e->xany.window, name, e->type, QLength(display));
935 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
937 e->xany.window, e->type, QLength(display));
941 dumpwins(struct swm_region *r, union arg *args)
945 XWindowAttributes wa;
948 fprintf(stderr, "invalid workspace\n");
952 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
954 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
955 state = getstate(win->id);
956 if (!XGetWindowAttributes(display, win->id, &wa))
957 fprintf(stderr, "window: %lu failed "
958 "XGetWindowAttributes\n", win->id);
959 fprintf(stderr, "window: %lu map_state: %d state: %d "
961 win->id, wa.map_state, state, win->transient);
964 fprintf(stderr, "===== unmanaged window list =====\n");
965 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
966 state = getstate(win->id);
967 if (!XGetWindowAttributes(display, win->id, &wa))
968 fprintf(stderr, "window: %lu failed "
969 "XGetWindowAttributes\n", win->id);
970 fprintf(stderr, "window: %lu map_state: %d state: %d "
972 win->id, wa.map_state, state, win->transient);
975 fprintf(stderr, "=================================\n");
980 dumpwins(struct swm_region *r, union arg *args)
983 #endif /* SWM_DEBUG */
985 void expose(XEvent *);
986 void keypress(XEvent *);
987 void buttonpress(XEvent *);
988 void configurerequest(XEvent *);
989 void configurenotify(XEvent *);
990 void destroynotify(XEvent *);
991 void enternotify(XEvent *);
992 void focusevent(XEvent *);
993 void mapnotify(XEvent *);
994 void mappingnotify(XEvent *);
995 void maprequest(XEvent *);
996 void propertynotify(XEvent *);
997 void unmapnotify(XEvent *);
998 void visibilitynotify(XEvent *);
999 void clientmessage(XEvent *);
1001 void (*handler[LASTEvent])(XEvent *) = {
1003 [KeyPress] = keypress,
1004 [ButtonPress] = buttonpress,
1005 [ConfigureRequest] = configurerequest,
1006 [ConfigureNotify] = configurenotify,
1007 [DestroyNotify] = destroynotify,
1008 [EnterNotify] = enternotify,
1009 [FocusIn] = focusevent,
1010 [FocusOut] = focusevent,
1011 [MapNotify] = mapnotify,
1012 [MappingNotify] = mappingnotify,
1013 [MapRequest] = maprequest,
1014 [PropertyNotify] = propertynotify,
1015 [UnmapNotify] = unmapnotify,
1016 [VisibilityNotify] = visibilitynotify,
1017 [ClientMessage] = clientmessage,
1023 int saved_errno, status;
1026 saved_errno = errno;
1030 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1035 if (errno != ECHILD)
1036 warn("sighdlr: waitpid");
1037 #endif /* SWM_DEBUG */
1040 if (pid == searchpid)
1044 if (WIFEXITED(status)) {
1045 if (WEXITSTATUS(status) != 0)
1046 warnx("sighdlr: child exit status: %d",
1047 WEXITSTATUS(status));
1049 warnx("sighdlr: child is terminated "
1051 #endif /* SWM_DEBUG */
1065 errno = saved_errno;
1071 struct pid_e *p = NULL;
1073 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1078 TAILQ_FOREACH(p, &pidlist, entry) {
1087 name_to_color(char *colorname)
1091 XColor screen_def, exact_def;
1092 unsigned long result = 0;
1093 char cname[32] = "#";
1095 cmap = DefaultColormap(display, screens[0].idx);
1096 status = XAllocNamedColor(display, cmap, colorname,
1097 &screen_def, &exact_def);
1099 strlcat(cname, colorname + 2, sizeof cname - 1);
1100 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1104 result = screen_def.pixel;
1106 fprintf(stderr, "color '%s' not found.\n", colorname);
1112 setscreencolor(char *val, int i, int c)
1114 if (i > 0 && i <= ScreenCount(display)) {
1115 screens[i - 1].c[c].color = name_to_color(val);
1116 free(screens[i - 1].c[c].name);
1117 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1119 } else if (i == -1) {
1120 for (i = 0; i < ScreenCount(display); i++) {
1121 screens[i].c[c].color = name_to_color(val);
1122 free(screens[i].c[c].name);
1123 if ((screens[i].c[c].name = strdup(val)) == NULL)
1127 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1128 i, ScreenCount(display));
1132 fancy_stacker(struct workspace *ws)
1134 strcpy(ws->stacker, "[ ]");
1135 if (ws->cur_layout->l_stack == vertical_stack)
1136 snprintf(ws->stacker, sizeof ws->stacker, "[%d|%d]",
1137 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1138 if (ws->cur_layout->l_stack == horizontal_stack)
1139 snprintf(ws->stacker, sizeof ws->stacker, "[%d-%d]",
1140 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1144 plain_stacker(struct workspace *ws)
1146 strcpy(ws->stacker, "[ ]");
1147 if (ws->cur_layout->l_stack == vertical_stack)
1148 strcpy(ws->stacker, "[|]");
1149 if (ws->cur_layout->l_stack == horizontal_stack)
1150 strcpy(ws->stacker, "[-]");
1154 custom_region(char *val)
1156 unsigned int sidx, x, y, w, h;
1158 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1159 errx(1, "invalid custom region, "
1160 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
1161 if (sidx < 1 || sidx > ScreenCount(display))
1162 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1163 sidx, ScreenCount(display));
1167 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
1169 if (x > DisplayWidth(display, sidx) ||
1170 y > DisplayHeight(display, sidx) ||
1171 w + x > DisplayWidth(display, sidx) ||
1172 h + y > DisplayHeight(display, sidx)) {
1173 fprintf(stderr, "ignoring region %ux%u+%u+%u "
1174 "- not within screen boundaries "
1175 "(%ux%u)\n", w, h, x, y,
1176 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1180 new_region(&screens[sidx], x, y, w, h);
1184 socket_setnonblock(int fd)
1188 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1189 err(1, "fcntl F_GETFL");
1190 flags |= O_NONBLOCK;
1191 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1192 err(1, "fcntl F_SETFL");
1196 bar_print(struct swm_region *r, char *s)
1198 XClearWindow(display, r->bar_window);
1199 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
1200 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
1205 bar_extra_stop(void)
1209 bzero(bar_pipe, sizeof bar_pipe);
1212 kill(bar_pid, SIGTERM);
1215 strlcpy(bar_ext, "", sizeof bar_ext);
1220 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1222 int do_class, do_name;
1224 XClassHint *xch = NULL;
1226 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
1227 cur_focus != NULL) {
1228 if ((xch = XAllocClassHint()) == NULL)
1230 status = XGetClassHint(display, cur_focus->id, xch);
1231 if (status == BadWindow || status == BadAlloc)
1233 do_class = (title_class_enabled && xch->res_class != NULL);
1234 do_name = (title_name_enabled && xch->res_name != NULL);
1236 strlcat(s, xch->res_class, sz);
1237 if (do_class && do_name)
1238 strlcat(s, ":", sz);
1240 strlcat(s, xch->res_name, sz);
1241 strlcat(s, " ", sz);
1249 bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1253 if (window_name_enabled && cur_focus != NULL) {
1254 XFetchName(display, cur_focus->id, &title);
1256 if (cur_focus->floating)
1257 strlcat(s, "(f) ", sz);
1258 strlcat(s, title, sz);
1259 strlcat(s, " ", sz);
1265 int urgent[SWM_WS_MAX];
1267 bar_urgent(char *s, ssize_t sz)
1269 XWMHints *wmh = NULL;
1274 if (urgent_enabled == 0)
1277 for (i = 0; i < SWM_WS_MAX; i++)
1280 for (i = 0; i < ScreenCount(display); i++)
1281 for (j = 0; j < SWM_WS_MAX; j++)
1282 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1283 wmh = XGetWMHints(display, win->id);
1287 if (wmh->flags & XUrgencyHint)
1292 strlcat(s, "* ", sz);
1293 for (i = 0; i < SWM_WS_MAX; i++) {
1295 snprintf(b, sizeof b, "%d ", i + 1);
1297 snprintf(b, sizeof b, "- ");
1300 strlcat(s, "* ", sz);
1308 struct swm_region *r;
1311 char s[SWM_BAR_MAX];
1312 char cn[SWM_BAR_MAX];
1313 char loc[SWM_BAR_MAX];
1316 if (bar_enabled == 0)
1318 if (bar_extra && bar_extra_running) {
1319 /* ignore short reads; it'll correct itself */
1320 while ((b = fgetln(stdin, &len)) != NULL)
1321 if (b && b[len - 1] == '\n') {
1323 strlcpy(bar_ext, b, sizeof bar_ext);
1325 if (b == NULL && errno != EAGAIN) {
1326 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
1327 errno, strerror(errno));
1331 strlcpy(bar_ext, "", sizeof bar_ext);
1333 if (clock_enabled == 0)
1334 strlcpy(s, "", sizeof s);
1337 localtime_r(&tmt, &tm);
1338 strftime(s, sizeof s, clock_format, &tm);
1339 strlcat(s, " ", sizeof s);
1342 for (i = 0; i < ScreenCount(display); i++) {
1344 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1345 strlcpy(cn, "", sizeof cn);
1347 bar_urgent(cn, sizeof cn);
1348 bar_class_name(cn, sizeof cn, r->ws->focus);
1349 bar_window_name(cn, sizeof cn, r->ws->focus);
1352 snprintf(loc, sizeof loc, "%d:%d %s %s%s %s %s",
1353 x++, r->ws->idx + 1, r->ws->stacker, s, cn, bar_ext,
1368 bar_toggle(struct swm_region *r, union arg *args)
1370 struct swm_region *tmpr;
1371 int i, sc = ScreenCount(display);
1373 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
1376 for (i = 0; i < sc; i++)
1377 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1378 XUnmapWindow(display, tmpr->bar_window);
1380 for (i = 0; i < sc; i++)
1381 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1382 XMapRaised(display, tmpr->bar_window);
1384 bar_enabled = !bar_enabled;
1387 /* must be after stack */
1394 XSetWindowAttributes wa;
1395 struct swm_region *r;
1398 /* do this here because the conf file is in memory */
1399 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1400 /* launch external status app */
1401 bar_extra_running = 1;
1402 if (pipe(bar_pipe) == -1)
1403 err(1, "pipe error");
1404 socket_setnonblock(bar_pipe[0]);
1405 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1406 if (dup2(bar_pipe[0], 0) == -1)
1408 if (dup2(bar_pipe[1], 1) == -1)
1410 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1411 err(1, "could not disable SIGPIPE");
1412 switch (bar_pid = fork()) {
1414 err(1, "cannot fork");
1418 execvp(bar_argv[0], bar_argv);
1419 err(1, "%s external app failed", bar_argv[0]);
1421 default: /* parent */
1427 bzero(&wa, sizeof wa);
1428 for (i = 0; i < ScreenCount(display); i++)
1429 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1431 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1432 wa.background_pixel =
1433 screens[i].c[SWM_S_COLOR_BAR].color;
1434 XChangeWindowAttributes(display, r->bar_window,
1435 CWBackPixel | CWBorderPixel, &wa);
1441 bar_setup(struct swm_region *r)
1446 XFreeFont(display, bar_fs);
1450 for (i = 0; bar_fonts[i] != NULL; i++) {
1451 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
1457 if (bar_fonts[i] == NULL)
1458 errx(1, "couldn't load font");
1460 errx(1, "couldn't create font structure");
1462 bar_height = bar_fs->ascent + bar_fs->descent + 1 +
1463 2 * bar_border_width;
1465 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1467 r->bar_window = XCreateSimpleWindow(display,
1468 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width,
1469 bar_height - 2 * bar_border_width,
1470 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1471 r->s->c[SWM_S_COLOR_BAR].color);
1472 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
1473 XSetFont(display, bar_gc, bar_fs->fid);
1474 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1476 XMapRaised(display, r->bar_window);
1477 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
1479 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1480 err(1, "could not install bar_signal");
1485 drain_enter_notify(void)
1490 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1493 DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained %d\n", i);
1497 set_win_state(struct ws_win *win, long state)
1499 long data[] = {state, None};
1501 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1506 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1507 (unsigned char *)data, 2);
1514 unsigned char *p = NULL;
1517 if (!get_property(w, astate, 2L, astate, &n, &p))
1520 result = *((long *)p);
1526 version(struct swm_region *r, union arg *args)
1528 bar_version = !bar_version;
1530 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
1531 SWM_VERSION, cvstag);
1533 strlcpy(bar_vertext, "", sizeof bar_vertext);
1538 client_msg(struct ws_win *win, Atom a)
1540 XClientMessageEvent cm;
1545 bzero(&cm, sizeof cm);
1546 cm.type = ClientMessage;
1547 cm.window = win->id;
1548 cm.message_type = aprot;
1551 cm.data.l[1] = CurrentTime;
1552 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1556 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1564 DNPRINTF(SWM_D_MISC,
1565 "config_win: win %lu x %d y %d w %d h %d\n",
1566 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1568 ce.type = ConfigureNotify;
1569 ce.display = display;
1571 ce.window = win->id;
1574 ce.width = win->g.w;
1575 ce.height = win->g.h;
1576 ce.border_width = border_width;
1578 ce.override_redirect = False;
1580 DNPRINTF(SWM_D_MISC,
1581 "config_win: ev win %lu x %d y %d w %d h %d\n",
1582 ev->window, ev->x, ev->y, ev->width, ev->height);
1583 ce.type = ConfigureNotify;
1584 ce.display = ev->display;
1585 ce.event = ev->window;
1586 ce.window = ev->window;
1589 ce.width = ev->width;
1590 ce.height = ev->height;
1591 ce.border_width = ev->border_width;
1592 ce.above = ev->above;
1593 ce.override_redirect = False;
1596 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1600 count_win(struct workspace *ws, int count_transient)
1605 TAILQ_FOREACH(win, &ws->winlist, entry) {
1606 if (count_transient == 0 && win->floating)
1608 if (count_transient == 0 && win->transient)
1614 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1620 quit(struct swm_region *r, union arg *args)
1622 DNPRINTF(SWM_D_MISC, "quit\n");
1627 unmap_window(struct ws_win *win)
1632 /* don't unmap again */
1633 if (getstate(win->id) == IconicState)
1636 set_win_state(win, IconicState);
1638 XUnmapWindow(display, win->id);
1639 XSetWindowBorder(display, win->id,
1640 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1649 for (i = 0; i < ScreenCount(display); i++)
1650 for (j = 0; j < SWM_WS_MAX; j++)
1651 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1656 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1663 event.display = display; /* Ignored, but what the hell */
1664 event.window = win->id;
1665 event.root = win->s->root;
1666 event.subwindow = None;
1667 event.time = CurrentTime;
1672 event.same_screen = True;
1673 event.keycode = XKeysymToKeycode(display, keysym);
1674 event.state = modifiers;
1676 event.type = KeyPress;
1677 XSendEvent(event.display, event.window, True,
1678 KeyPressMask, (XEvent *)&event);
1680 event.type = KeyRelease;
1681 XSendEvent(event.display, event.window, True,
1682 KeyPressMask, (XEvent *)&event);
1687 restart(struct swm_region *r, union arg *args)
1689 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1691 /* disable alarm because the following code may not be interrupted */
1693 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1694 errx(1, "can't disable alarm");
1699 XCloseDisplay(display);
1700 execvp(start_argv[0], start_argv);
1701 fprintf(stderr, "execvp failed\n");
1707 root_to_region(Window root)
1709 struct swm_region *r = NULL;
1711 int i, x, y, wx, wy;
1714 for (i = 0; i < ScreenCount(display); i++)
1715 if (screens[i].root == root)
1718 if (XQueryPointer(display, screens[i].root,
1719 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1720 /* choose a region based on pointer location */
1721 TAILQ_FOREACH(r, &screens[i].rl, entry)
1722 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1723 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1728 r = TAILQ_FIRST(&screens[i].rl);
1734 find_unmanaged_window(Window id)
1739 for (i = 0; i < ScreenCount(display); i++)
1740 for (j = 0; j < SWM_WS_MAX; j++)
1741 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1749 find_window(Window id)
1752 Window wrr, wpr, *wcr = NULL;
1756 for (i = 0; i < ScreenCount(display); i++)
1757 for (j = 0; j < SWM_WS_MAX; j++)
1758 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1762 /* if we were looking for the parent return that window instead */
1763 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1768 /* ignore not found and root */
1769 if (wpr == 0 || wrr == wpr)
1772 /* look for parent */
1773 for (i = 0; i < ScreenCount(display); i++)
1774 for (j = 0; j < SWM_WS_MAX; j++)
1775 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1783 spawn(int ws_idx, union arg *args, int close_fd)
1788 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1791 close(ConnectionNumber(display));
1793 setenv("LD_PRELOAD", SWM_LIB, 1);
1795 if (asprintf(&ret, "%d", ws_idx) == -1) {
1799 setenv("_SWM_WS", ret, 1);
1803 if (asprintf(&ret, "%d", getpid()) == -1) {
1807 setenv("_SWM_PID", ret, 1);
1811 if (setsid() == -1) {
1818 * close stdin and stdout to prevent interaction between apps
1819 * and the baraction script
1820 * leave stderr open to record errors
1822 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
1826 dup2(fd, STDIN_FILENO);
1827 dup2(fd, STDOUT_FILENO);
1832 execvp(args->argv[0], args->argv);
1839 spawnterm(struct swm_region *r, union arg *args)
1841 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1845 setenv("_SWM_XTERM_FONTADJ", "", 1);
1846 spawn(r->ws->idx, args, 1);
1851 kill_refs(struct ws_win *win)
1854 struct swm_region *r;
1855 struct workspace *ws;
1860 for (i = 0; i < ScreenCount(display); i++)
1861 TAILQ_FOREACH(r, &screens[i].rl, entry)
1862 for (x = 0; x < SWM_WS_MAX; x++) {
1864 if (win == ws->focus)
1866 if (win == ws->focus_prev)
1867 ws->focus_prev = NULL;
1872 validate_win(struct ws_win *testwin)
1875 struct workspace *ws;
1876 struct swm_region *r;
1877 int i, x, foundit = 0;
1879 if (testwin == NULL)
1882 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1883 TAILQ_FOREACH(r, &screens[i].rl, entry)
1884 for (x = 0; x < SWM_WS_MAX; x++) {
1886 TAILQ_FOREACH(win, &ws->winlist, entry)
1894 validate_ws(struct workspace *testws)
1896 struct swm_region *r;
1897 struct workspace *ws;
1900 /* validate all ws */
1901 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1902 TAILQ_FOREACH(r, &screens[i].rl, entry)
1903 for (x = 0; x < SWM_WS_MAX; x++) {
1912 unfocus_win(struct ws_win *win)
1917 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1921 if (win->ws == NULL)
1924 if (validate_ws(win->ws))
1925 return; /* XXX this gets hit with thunderbird, needs fixing */
1927 if (win->ws->r == NULL)
1930 if (validate_win(win)) {
1935 if (win->ws->focus == win) {
1936 win->ws->focus = NULL;
1937 win->ws->focus_prev = win;
1940 if (validate_win(win->ws->focus)) {
1941 kill_refs(win->ws->focus);
1942 win->ws->focus = NULL;
1944 if (validate_win(win->ws->focus_prev)) {
1945 kill_refs(win->ws->focus_prev);
1946 win->ws->focus_prev = NULL;
1949 /* drain all previous unfocus events */
1950 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
1953 grabbuttons(win, 0);
1954 XSetWindowBorder(display, win->id,
1955 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1957 XChangeProperty(display, win->s->root,
1958 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1959 PropModeReplace, (unsigned char *)&none,1);
1968 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
1970 for (i = 0; i < ScreenCount(display); i++)
1971 for (j = 0; j < SWM_WS_MAX; j++)
1972 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1977 focus_win(struct ws_win *win)
1982 struct ws_win *cfw = NULL;
1985 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1989 if (win->ws == NULL)
1992 if (validate_ws(win->ws))
1993 return; /* XXX this gets hit with thunderbird, needs fixing */
1995 if (validate_win(win)) {
2000 if (validate_win(win)) {
2005 XGetInputFocus(display, &cur_focus, &rr);
2006 if ((cfw = find_window(cur_focus)) != NULL)
2009 /* use larger hammer since the window was killed somehow */
2010 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2011 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2012 XSetWindowBorder(display, cfw->id,
2013 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2016 win->ws->focus = win;
2018 if (win->ws->r != NULL) {
2019 /* drain all previous focus events */
2020 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
2024 XSetInputFocus(display, win->id,
2025 RevertToParent, CurrentTime);
2026 grabbuttons(win, 1);
2027 XSetWindowBorder(display, win->id,
2028 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2029 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2030 win->ws->always_raise)
2031 XMapRaised(display, win->id);
2033 XChangeProperty(display, win->s->root,
2034 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2035 PropModeReplace, (unsigned char *)&win->id,1);
2038 if (window_name_enabled)
2043 switchws(struct swm_region *r, union arg *args)
2045 int wsid = args->id, unmap_old = 0;
2046 struct swm_region *this_r, *other_r;
2048 struct workspace *new_ws, *old_ws;
2055 old_ws = this_r->ws;
2056 new_ws = &this_r->s->ws[wsid];
2058 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
2059 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
2062 if (new_ws == NULL || old_ws == NULL)
2064 if (new_ws == old_ws)
2067 other_r = new_ws->r;
2068 if (other_r == NULL) {
2069 /* the other workspace is hidden, hide this one */
2073 /* the other ws is visible in another region, exchange them */
2074 other_r->ws_prior = new_ws;
2075 other_r->ws = old_ws;
2076 old_ws->r = other_r;
2078 this_r->ws_prior = old_ws;
2079 this_r->ws = new_ws;
2082 /* this is needed so that we can click on a window after a restart */
2086 a.id = SWM_ARG_ID_FOCUSCUR;
2087 focus(new_ws->r, &a);
2091 /* unmap old windows */
2093 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2096 if (focus_mode == SWM_FOCUS_DEFAULT)
2097 drain_enter_notify();
2101 cyclews(struct swm_region *r, union arg *args)
2104 struct swm_screen *s = r->s;
2106 DNPRINTF(SWM_D_WS, "cyclews id %d "
2107 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2108 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2113 case SWM_ARG_ID_CYCLEWS_UP:
2114 if (a.id < SWM_WS_MAX - 1)
2119 case SWM_ARG_ID_CYCLEWS_DOWN:
2123 a.id = SWM_WS_MAX - 1;
2129 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
2131 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2135 } while (a.id != r->ws->idx);
2139 priorws(struct swm_region *r, union arg *args)
2143 DNPRINTF(SWM_D_WS, "priorws id %d "
2144 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2145 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2147 if (r->ws_prior == NULL)
2150 a.id = r->ws_prior->idx;
2155 cyclescr(struct swm_region *r, union arg *args)
2157 struct swm_region *rr = NULL;
2161 /* do nothing if we don't have more than one screen */
2162 if (!(ScreenCount(display) > 1 || outputs > 1))
2167 case SWM_ARG_ID_CYCLESC_UP:
2168 rr = TAILQ_NEXT(r, entry);
2170 rr = TAILQ_FIRST(&screens[i].rl);
2172 case SWM_ARG_ID_CYCLESC_DOWN:
2173 rr = TAILQ_PREV(r, swm_region_list, entry);
2175 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2183 /* move mouse to region */
2185 y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
2186 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2188 a.id = SWM_ARG_ID_FOCUSCUR;
2191 if (rr->ws->focus) {
2192 /* move to focus window */
2193 x = rr->ws->focus->g.x + 1;
2194 y = rr->ws->focus->g.y + 1;
2195 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2200 sort_windows(struct ws_win_list *wl)
2202 struct ws_win *win, *parent, *nxt;
2207 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2208 nxt = TAILQ_NEXT(win, entry);
2209 if (win->transient) {
2210 parent = find_window(win->transient);
2211 if (parent == NULL) {
2212 fprintf(stderr, "not possible bug\n");
2215 TAILQ_REMOVE(wl, win, entry);
2216 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2223 swapwin(struct swm_region *r, union arg *args)
2225 struct ws_win *target, *source;
2226 struct ws_win *cur_focus;
2227 struct ws_win_list *wl;
2230 DNPRINTF(SWM_D_WS, "swapwin id %d "
2231 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
2232 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2234 cur_focus = r->ws->focus;
2235 if (cur_focus == NULL)
2239 wl = &source->ws->winlist;
2242 case SWM_ARG_ID_SWAPPREV:
2243 if (source->transient)
2244 source = find_window(source->transient);
2245 target = TAILQ_PREV(source, ws_win_list, entry);
2246 if (target && target->transient)
2247 target = find_window(target->transient);
2248 TAILQ_REMOVE(wl, source, entry);
2250 TAILQ_INSERT_TAIL(wl, source, entry);
2252 TAILQ_INSERT_BEFORE(target, source, entry);
2254 case SWM_ARG_ID_SWAPNEXT:
2255 target = TAILQ_NEXT(source, entry);
2256 /* move the parent and let the sort handle the move */
2257 if (source->transient)
2258 source = find_window(source->transient);
2259 TAILQ_REMOVE(wl, source, entry);
2261 TAILQ_INSERT_HEAD(wl, source, entry);
2263 TAILQ_INSERT_AFTER(wl, target, source, entry);
2265 case SWM_ARG_ID_SWAPMAIN:
2266 target = TAILQ_FIRST(wl);
2267 if (target == source) {
2268 if (source->ws->focus_prev != NULL &&
2269 source->ws->focus_prev != target)
2271 source = source->ws->focus_prev;
2275 if (target == NULL || source == NULL)
2277 source->ws->focus_prev = target;
2278 TAILQ_REMOVE(wl, target, entry);
2279 TAILQ_INSERT_BEFORE(source, target, entry);
2280 TAILQ_REMOVE(wl, source, entry);
2281 TAILQ_INSERT_HEAD(wl, source, entry);
2283 case SWM_ARG_ID_MOVELAST:
2284 TAILQ_REMOVE(wl, source, entry);
2285 TAILQ_INSERT_TAIL(wl, source, entry);
2288 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
2298 focus_prev(struct ws_win *win)
2300 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2301 struct ws_win *cur_focus = NULL;
2302 struct ws_win_list *wl = NULL;
2303 struct workspace *ws = NULL;
2305 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
2307 if (!(win && win->ws))
2312 cur_focus = ws->focus;
2313 winlostfocus = cur_focus;
2315 /* pickle, just focus on whatever */
2316 if (cur_focus == NULL) {
2317 /* use prev_focus if valid */
2318 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2319 find_window(WINID(ws->focus_prev)))
2320 winfocus = ws->focus_prev;
2321 if (winfocus == NULL)
2322 winfocus = TAILQ_FIRST(wl);
2326 /* if transient focus on parent */
2327 if (cur_focus->transient) {
2328 winfocus = find_window(cur_focus->transient);
2332 /* if in max_stack try harder */
2333 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2334 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2335 if (cur_focus != ws->focus_prev)
2336 winfocus = ws->focus_prev;
2337 else if (cur_focus != ws->focus)
2338 winfocus = ws->focus;
2340 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2345 if (cur_focus == win)
2346 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2347 if (winfocus == NULL)
2348 winfocus = TAILQ_LAST(wl, ws_win_list);
2349 if (winfocus == NULL || winfocus == win)
2350 winfocus = TAILQ_NEXT(cur_focus, entry);
2352 if (winfocus == winlostfocus || winfocus == NULL)
2355 focus_magic(winfocus);
2359 focus(struct swm_region *r, union arg *args)
2361 struct ws_win *winfocus = NULL, *winlostfocus = NULL, *head;
2362 struct ws_win *cur_focus = NULL;
2363 struct ws_win_list *wl = NULL;
2364 struct workspace *ws = NULL;
2369 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
2371 /* treat FOCUS_CUR special */
2372 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2373 if (r->ws->focus && r->ws->focus->iconic == 0)
2374 winfocus = r->ws->focus;
2375 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2376 winfocus = r->ws->focus_prev;
2378 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2379 if (winfocus->iconic == 0)
2382 focus_magic(winfocus);
2386 if ((cur_focus = r->ws->focus) == NULL)
2391 winlostfocus = cur_focus;
2394 case SWM_ARG_ID_FOCUSPREV:
2395 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2397 head = TAILQ_LAST(wl, ws_win_list);
2399 if (WINID(winfocus) == cur_focus->transient) {
2400 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2402 head = TAILQ_LAST(wl, ws_win_list);
2407 if (winfocus && winfocus->iconic) {
2408 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
2409 if (winfocus->iconic == 0)
2414 case SWM_ARG_ID_FOCUSNEXT:
2415 head = TAILQ_NEXT(cur_focus, entry);
2417 head = TAILQ_FIRST(wl);
2421 if (winfocus && winfocus->iconic) {
2422 TAILQ_FOREACH(winfocus, wl, entry)
2423 if (winfocus->iconic == 0)
2428 case SWM_ARG_ID_FOCUSMAIN:
2429 winfocus = TAILQ_FIRST(wl);
2430 if (winfocus == cur_focus)
2431 winfocus = cur_focus->ws->focus_prev;
2437 if (winfocus == winlostfocus || winfocus == NULL)
2440 focus_magic(winfocus);
2444 cycle_layout(struct swm_region *r, union arg *args)
2446 struct workspace *ws = r->ws;
2447 struct ws_win *winfocus;
2450 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2452 winfocus = ws->focus;
2455 if (ws->cur_layout->l_stack == NULL)
2456 ws->cur_layout = &layouts[0];
2459 if (focus_mode == SWM_FOCUS_DEFAULT)
2460 drain_enter_notify();
2461 a.id = SWM_ARG_ID_FOCUSCUR;
2467 stack_config(struct swm_region *r, union arg *args)
2469 struct workspace *ws = r->ws;
2471 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
2474 if (ws->cur_layout->l_config != NULL)
2475 ws->cur_layout->l_config(ws, args->id);
2477 if (args->id != SWM_ARG_ID_STACKINIT)
2484 struct swm_geometry g;
2485 struct swm_region *r;
2488 DNPRINTF(SWM_D_STACK, "stack\n");
2490 for (i = 0; i < ScreenCount(display); i++) {
2492 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2493 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
2494 "(screen %d, region %d)\n", r->ws->idx, i, j++);
2496 /* start with screen geometry, adjust for bar */
2498 g.w -= 2 * border_width;
2499 g.h -= 2 * border_width;
2505 r->ws->cur_layout->l_stack(r->ws, &g);
2506 r->ws->cur_layout->l_string(r->ws);
2507 /* save r so we can track region changes */
2514 if (focus_mode == SWM_FOCUS_DEFAULT)
2515 drain_enter_notify();
2519 store_float_geom(struct ws_win *win, struct swm_region *r)
2521 /* retain window geom and region geom */
2522 win->g_float.x = win->g.x;
2523 win->g_float.y = win->g.y;
2524 win->g_float.w = win->g.w;
2525 win->g_float.h = win->g.h;
2526 win->rg_float.x = r->g.x;
2527 win->rg_float.y = r->g.y;
2528 win->rg_float.w = r->g.w;
2529 win->rg_float.h = r->g.h;
2530 win->g_floatvalid = 1;
2534 stack_floater(struct ws_win *win, struct swm_region *r)
2542 bzero(&wc, sizeof wc);
2543 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2546 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2547 * geom on ws switches or return from max mode
2550 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2551 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2553 * use stored g and rg to set relative position and size
2554 * as in old region or before max stack mode
2556 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2557 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2558 win->g.w = win->g_float.w;
2559 win->g.h = win->g_float.h;
2560 win->g_floatvalid = 0;
2563 win->floatmaxed = 0;
2565 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
2566 (win->g.h >= HEIGHT(r)))
2567 wc.border_width = 0;
2569 wc.border_width = border_width;
2570 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2571 win->g.w = (double)WIDTH(r) * dialog_ratio;
2572 win->g.h = (double)HEIGHT(r) * dialog_ratio;
2577 * floaters and transients are auto-centred unless moved
2580 win->g.x = r->g.x + (WIDTH(r) - win->g.w) /
2581 2 - wc.border_width;
2582 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) /
2583 2 - wc.border_width;
2586 /* win can be outside r if new r smaller than old r */
2587 /* Ensure top left corner inside r (move probs otherwise) */
2588 if (win->g.x < r->g.x - wc.border_width)
2589 win->g.x = r->g.x - wc.border_width;
2590 if (win->g.x > r->g.x + r->g.w - 1)
2591 win->g.x = (win->g.w > r->g.w) ? r->g.x :
2592 (r->g.x + r->g.w - win->g.w - 2 * wc.border_width);
2593 if (win->g.y < r->g.y - wc.border_width)
2594 win->g.y = r->g.y - wc.border_width;
2595 if (win->g.y > r->g.y + r->g.h - 1)
2596 win->g.y = (win->g.h > r->g.h) ? r->g.y :
2597 (r->g.y + r->g.h - win->g.h - 2 * wc.border_width);
2601 wc.width = win->g.w;
2602 wc.height = win->g.h;
2605 * Retain floater and transient geometry for correct positioning
2606 * when ws changes region
2608 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2609 store_float_geom(win, r);
2611 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
2612 win->id, wc.x, wc.y, wc.width, wc.height);
2614 XConfigureWindow(display, win->id, mask, &wc);
2618 * Send keystrokes to terminal to decrease/increase the font size as the
2619 * window size changes.
2622 adjust_font(struct ws_win *win)
2624 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2625 win->floating || win->transient)
2628 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2629 win->g.w / win->sh.width_inc < term_width &&
2630 win->font_steps < SWM_MAX_FONT_STEPS) {
2631 win->font_size_boundary[win->font_steps] =
2632 (win->sh.width_inc * term_width) + win->sh.base_width;
2635 win->last_inc = win->sh.width_inc;
2636 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2637 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2638 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
2641 win->last_inc = win->sh.width_inc;
2642 fake_keypress(win, XK_KP_Add, ShiftMask);
2646 #define SWAPXY(g) do { \
2648 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2649 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2652 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2655 XWindowAttributes wa;
2656 struct swm_geometry win_g, r_g = *g;
2657 struct ws_win *win, *fs_win = 0;
2658 int i, j, s, stacks;
2659 int w_inc = 1, h_inc, w_base = 1, h_base;
2660 int hrh, extra = 0, h_slice, last_h = 0;
2661 int split, colno, winno, mwin, msize, mscale;
2662 int remain, missing, v_slice, reconfigure;
2665 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2666 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2668 winno = count_win(ws, 0);
2669 if (winno == 0 && count_win(ws, 1) == 0)
2672 TAILQ_FOREACH(win, &ws->winlist, entry)
2673 if (win->transient == 0 && win->floating == 0
2674 && win->iconic == 0)
2681 w_inc = win->sh.width_inc;
2682 w_base = win->sh.base_width;
2683 mwin = ws->l_state.horizontal_mwin;
2684 mscale = ws->l_state.horizontal_msize;
2685 stacks = ws->l_state.horizontal_stacks;
2688 w_inc = win->sh.height_inc;
2689 w_base = win->sh.base_height;
2690 mwin = ws->l_state.vertical_mwin;
2691 mscale = ws->l_state.vertical_msize;
2692 stacks = ws->l_state.vertical_stacks;
2696 if (stacks > winno - mwin)
2697 stacks = winno - mwin;
2701 h_slice = r_g.h / SWM_H_SLICE;
2702 if (mwin && winno > mwin) {
2703 v_slice = r_g.w / SWM_V_SLICE;
2707 win_g.w = v_slice * mscale;
2709 if (w_inc > 1 && w_inc < v_slice) {
2710 /* adjust for window's requested size increment */
2711 remain = (win_g.w - w_base) % w_inc;
2712 missing = w_inc - remain;
2719 win_g.x += r_g.w - msize;
2722 colno = split = winno / stacks;
2723 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
2724 2 * border_width) / stacks);
2726 hrh = r_g.h / colno;
2727 extra = r_g.h - (colno * hrh);
2728 win_g.h = hrh - 2 * border_width;
2730 /* stack all the tiled windows */
2731 i = j = 0, s = stacks;
2732 TAILQ_FOREACH(win, &ws->winlist, entry) {
2733 if (win->transient != 0 || win->floating != 0)
2735 if (win->iconic != 0)
2738 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2743 if (split && i == split) {
2744 colno = (winno - mwin) / stacks;
2745 if (s <= (winno - mwin) % stacks)
2747 split = split + colno;
2748 hrh = (r_g.h / colno);
2749 extra = r_g.h - (colno * hrh);
2753 win_g.x += win_g.w + 2 * border_width;
2754 win_g.w = (r_g.w - msize -
2755 (stacks * 2 * border_width)) / stacks;
2757 win_g.w += (r_g.w - msize -
2758 (stacks * 2 * border_width)) % stacks;
2762 win_g.h = hrh - 2 * border_width;
2764 h_inc = win->sh.width_inc;
2765 h_base = win->sh.base_width;
2767 h_inc = win->sh.height_inc;
2768 h_base = win->sh.base_height;
2770 if (j == colno - 1) {
2771 win_g.h = hrh + extra;
2772 } else if (h_inc > 1 && h_inc < h_slice) {
2773 /* adjust for window's requested size increment */
2774 remain = (win_g.h - h_base) % h_inc;
2775 missing = h_inc - remain;
2777 if (missing <= extra || j == 0) {
2789 win_g.y += last_h + 2 * border_width;
2791 bzero(&wc, sizeof wc);
2792 if (disable_border && bar_enabled == 0 && winno == 1){
2793 wc.border_width = 0;
2794 win_g.w += 2 * border_width;
2795 win_g.h += 2 * border_width;
2797 wc.border_width = border_width;
2800 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2801 win->g.w != win_g.h || win->g.h != win_g.w) {
2803 win->g.x = wc.x = win_g.y;
2804 win->g.y = wc.y = win_g.x;
2805 win->g.w = wc.width = win_g.h;
2806 win->g.h = wc.height = win_g.w;
2809 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2810 win->g.w != win_g.w || win->g.h != win_g.h) {
2812 win->g.x = wc.x = win_g.x;
2813 win->g.y = wc.y = win_g.y;
2814 win->g.w = wc.width = win_g.w;
2815 win->g.h = wc.height = win_g.h;
2820 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2821 XConfigureWindow(display, win->id, mask, &wc);
2824 if (XGetWindowAttributes(display, win->id, &wa))
2825 if (wa.map_state == IsUnmapped)
2826 XMapRaised(display, win->id);
2834 /* now, stack all the floaters and transients */
2835 TAILQ_FOREACH(win, &ws->winlist, entry) {
2836 if (win->transient == 0 && win->floating == 0)
2838 if (win->iconic == 1)
2840 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2845 stack_floater(win, ws->r);
2846 XMapRaised(display, win->id);
2850 stack_floater(fs_win, ws->r);
2851 XMapRaised(display, fs_win->id);
2856 vertical_config(struct workspace *ws, int id)
2858 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2861 case SWM_ARG_ID_STACKRESET:
2862 case SWM_ARG_ID_STACKINIT:
2863 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2864 ws->l_state.vertical_mwin = 1;
2865 ws->l_state.vertical_stacks = 1;
2867 case SWM_ARG_ID_MASTERSHRINK:
2868 if (ws->l_state.vertical_msize > 1)
2869 ws->l_state.vertical_msize--;
2871 case SWM_ARG_ID_MASTERGROW:
2872 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2873 ws->l_state.vertical_msize++;
2875 case SWM_ARG_ID_MASTERADD:
2876 ws->l_state.vertical_mwin++;
2878 case SWM_ARG_ID_MASTERDEL:
2879 if (ws->l_state.vertical_mwin > 0)
2880 ws->l_state.vertical_mwin--;
2882 case SWM_ARG_ID_STACKINC:
2883 ws->l_state.vertical_stacks++;
2885 case SWM_ARG_ID_STACKDEC:
2886 if (ws->l_state.vertical_stacks > 1)
2887 ws->l_state.vertical_stacks--;
2895 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2897 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2899 stack_master(ws, g, 0, 0);
2903 horizontal_config(struct workspace *ws, int id)
2905 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2908 case SWM_ARG_ID_STACKRESET:
2909 case SWM_ARG_ID_STACKINIT:
2910 ws->l_state.horizontal_mwin = 1;
2911 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2912 ws->l_state.horizontal_stacks = 1;
2914 case SWM_ARG_ID_MASTERSHRINK:
2915 if (ws->l_state.horizontal_msize > 1)
2916 ws->l_state.horizontal_msize--;
2918 case SWM_ARG_ID_MASTERGROW:
2919 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2920 ws->l_state.horizontal_msize++;
2922 case SWM_ARG_ID_MASTERADD:
2923 ws->l_state.horizontal_mwin++;
2925 case SWM_ARG_ID_MASTERDEL:
2926 if (ws->l_state.horizontal_mwin > 0)
2927 ws->l_state.horizontal_mwin--;
2929 case SWM_ARG_ID_STACKINC:
2930 ws->l_state.horizontal_stacks++;
2932 case SWM_ARG_ID_STACKDEC:
2933 if (ws->l_state.horizontal_stacks > 1)
2934 ws->l_state.horizontal_stacks--;
2942 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2944 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
2946 stack_master(ws, g, 1, 0);
2949 /* fullscreen view */
2951 max_stack(struct workspace *ws, struct swm_geometry *g)
2954 struct swm_geometry gg = *g;
2955 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2959 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2964 winno = count_win(ws, 0);
2965 if (winno == 0 && count_win(ws, 1) == 0)
2968 TAILQ_FOREACH(win, &ws->winlist, entry) {
2969 if (win->transient) {
2971 parent = find_window(win->transient);
2975 if (win->floating && win->floatmaxed == 0 ) {
2977 * retain geometry for retrieval on exit from
2980 store_float_geom(win, ws->r);
2981 win->floatmaxed = 1;
2984 /* only reconfigure if necessary */
2985 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2987 bzero(&wc, sizeof wc);
2988 win->g.x = wc.x = gg.x;
2989 win->g.y = wc.y = gg.y;
2991 wc.border_width = border_width;
2992 win->g.w = wc.width = gg.w;
2993 win->g.h = wc.height = gg.h;
2995 wc.border_width = 0;
2996 win->g.w = wc.width = gg.w + 2 * border_width;
2997 win->g.h = wc.height = gg.h + 2 * border_width;
2999 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3000 XConfigureWindow(display, win->id, mask, &wc);
3002 /* unmap only if we don't have multi screen */
3003 if (win != ws->focus)
3004 if (!(ScreenCount(display) > 1 || outputs > 1))
3008 /* put the last transient on top */
3011 XMapRaised(display, parent->id);
3012 stack_floater(wintrans, ws->r);
3013 focus_magic(wintrans);
3018 send_to_ws(struct swm_region *r, union arg *args)
3020 int wsid = args->id;
3021 struct ws_win *win = NULL, *parent;
3022 struct workspace *ws, *nws;
3023 Atom ws_idx_atom = 0;
3024 unsigned char ws_idx_str[SWM_PROPLEN];
3033 if (win->ws->idx == wsid)
3036 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
3039 nws = &win->s->ws[wsid];
3041 a.id = SWM_ARG_ID_FOCUSPREV;
3043 if (win->transient) {
3044 parent = find_window(win->transient);
3046 unmap_window(parent);
3047 TAILQ_REMOVE(&ws->winlist, parent, entry);
3048 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3053 TAILQ_REMOVE(&ws->winlist, win, entry);
3054 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3057 /* Try to update the window's workspace property */
3058 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3060 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3062 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3064 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3065 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3072 pressbutton(struct swm_region *r, union arg *args)
3074 XTestFakeButtonEvent(display, args->id, True, CurrentTime);
3075 XTestFakeButtonEvent(display, args->id, False, CurrentTime);
3079 raise_toggle(struct swm_region *r, union arg *args)
3081 if (r && r->ws == NULL)
3084 r->ws->always_raise = !r->ws->always_raise;
3086 /* bring floaters back to top */
3087 if (r->ws->always_raise == 0)
3092 iconify(struct swm_region *r, union arg *args)
3096 if (r->ws->focus == NULL)
3098 unmap_window(r->ws->focus);
3099 update_iconic(r->ws->focus, 1);
3101 if (focus_mode == SWM_FOCUS_DEFAULT)
3102 drain_enter_notify();
3103 r->ws->focus = NULL;
3104 a.id = SWM_ARG_ID_FOCUSCUR;
3109 get_win_name(Display *dpy, Window win, Atom wname, Atom stype,
3110 unsigned long *slen)
3113 unsigned long nitems, nbytes, nextra;
3114 unsigned char *prop = NULL;
3117 status = XGetWindowProperty(dpy, win, wname, 0L, 0L, False, stype,
3118 &rettype, &retfmt, &nitems, &nbytes, &prop);
3119 if (status != Success)
3123 status = XGetWindowProperty(dpy, win, wname, 0L, nbytes, False,
3124 stype, &rettype, &retfmt, &nitems, &nextra, &prop);
3125 if (status != Success) {
3129 if (rettype != stype) {
3138 uniconify(struct swm_region *r, union arg *args)
3142 unsigned char *name;
3146 DNPRINTF(SWM_D_MISC, "uniconify\n");
3148 if (r && r->ws == NULL)
3151 /* make sure we have anything to uniconify */
3152 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3153 if (win->ws == NULL)
3154 continue; /* should never happen */
3155 if (win->iconic == 0)
3164 spawn_select(r, args, "uniconify", &searchpid);
3166 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3169 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3170 if (win->ws == NULL)
3171 continue; /* should never happen */
3172 if (win->iconic == 0)
3175 name = get_win_name(display, win->id, a_wmname, a_string,
3179 fprintf(lfile, "%s.%lu\n", name, win->id);
3186 #define MAX_RESP_LEN 1024
3189 search_do_resp(void)
3193 unsigned char *name;
3197 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3202 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3203 fprintf(stderr, "search: calloc\n");
3207 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3209 fprintf(stderr, "search: read error: %s\n", strerror(errno));
3212 resp[rbytes] = '\0';
3215 DNPRINTF(SWM_D_MISC, "search_do_resp: resp %s\n", resp);
3216 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3217 if (win->iconic == 0)
3219 name = get_win_name(display, win->id, a_wmname, a_string, &len);
3222 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3227 if (strncmp(s, resp, len) == 0) {
3228 /* XXX this should be a callback to generalize */
3229 update_iconic(win, 0);
3240 wkill(struct swm_region *r, union arg *args)
3242 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
3244 if (r->ws->focus == NULL)
3247 if (args->id == SWM_ARG_ID_KILLWINDOW)
3248 XKillClient(display, r->ws->focus->id);
3250 if (r->ws->focus->can_delete)
3251 client_msg(r->ws->focus, adelete);
3256 floating_toggle_win(struct ws_win *win)
3258 struct swm_region *r;
3268 /* reject floating toggles in max stack mode */
3269 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3272 if (win->floating) {
3273 if (!win->floatmaxed) {
3274 /* retain position for refloat */
3275 store_float_geom(win, r);
3279 if (win->g_floatvalid) {
3280 /* refloat at last floating relative position */
3281 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
3282 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
3283 win->g.w = win->g_float.w;
3284 win->g.h = win->g_float.h;
3289 ewmh_update_actions(win);
3295 floating_toggle(struct swm_region *r, union arg *args)
3297 struct ws_win *win = r->ws->focus;
3303 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3304 _NET_WM_STATE_TOGGLE);
3307 if (focus_mode == SWM_FOCUS_DEFAULT)
3308 drain_enter_notify();
3310 if (win == win->ws->focus) {
3311 a.id = SWM_ARG_ID_FOCUSCUR;
3312 focus(win->ws->r, &a);
3317 resize_window(struct ws_win *win, int center)
3321 struct swm_region *r;
3323 r = root_to_region(win->wa.root);
3324 bzero(&wc, sizeof wc);
3325 mask = CWBorderWidth | CWWidth | CWHeight;
3326 wc.border_width = border_width;
3327 wc.width = win->g.w;
3328 wc.height = win->g.h;
3329 if (center == SWM_ARG_ID_CENTER) {
3330 wc.x = (WIDTH(r) - win->g.w) / 2 - border_width;
3331 wc.y = (HEIGHT(r) - win->g.h) / 2 - border_width;
3335 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
3336 win->id, wc.x, wc.y, wc.width, wc.height);
3338 XConfigureWindow(display, win->id, mask, &wc);
3342 resize(struct ws_win *win, union arg *args)
3346 struct swm_region *r = win->ws->r;
3350 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
3351 win->id, win->floating, win->transient);
3353 if (!(win->transient != 0 || win->floating != 0))
3356 /* reject resizes in max mode for floaters (transient ok) */
3357 if (win->floatmaxed)
3361 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3365 if (focus_mode == SWM_FOCUS_DEFAULT)
3366 drain_enter_notify();
3368 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3369 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3372 /* place pointer at bottom left corner or nearest point inside r */
3373 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
3374 relx = win->g.w - 1;
3376 relx = r->g.x + r->g.w - win->g.x - 1;
3378 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
3379 rely = win->g.h - 1;
3381 rely = r->g.y + r->g.h - win->g.y - 1;
3383 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
3385 XMaskEvent(display, MOUSEMASK | ExposureMask |
3386 SubstructureRedirectMask, &ev);
3388 case ConfigureRequest:
3391 handler[ev.type](&ev);
3394 /* do not allow resize outside of region */
3395 if ( ev.xmotion.x_root < r->g.x ||
3396 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3397 ev.xmotion.y_root < r->g.y ||
3398 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3401 if (ev.xmotion.x <= 1)
3403 if (ev.xmotion.y <= 1)
3405 win->g.w = ev.xmotion.x + 1;
3406 win->g.h = ev.xmotion.y + 1;
3408 /* not free, don't sync more than 120 times / second */
3409 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3410 time = ev.xmotion.time;
3411 XSync(display, False);
3412 resize_window(win, args->id);
3416 } while (ev.type != ButtonRelease);
3418 XSync(display, False);
3419 resize_window(win, args->id);
3421 store_float_geom(win,r);
3423 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
3425 XUngrabPointer(display, CurrentTime);
3428 drain_enter_notify();
3432 move_window(struct ws_win *win)
3436 struct swm_region *r;
3438 r = root_to_region(win->wa.root);
3439 bzero(&wc, sizeof wc);
3443 wc.border_width = border_width;
3445 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
3446 win->id, wc.x, wc.y, wc.width, wc.height);
3448 XConfigureWindow(display, win->id, mask, &wc);
3452 move(struct ws_win *win, union arg *args)
3456 struct swm_region *r = win->ws->r;
3458 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
3459 win->id, win->floating, win->transient);
3461 /* in max_stack mode should only move transients */
3462 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
3466 if (win->floating == 0 && !win->transient) {
3467 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3470 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3475 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3476 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3478 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3480 XMaskEvent(display, MOUSEMASK | ExposureMask |
3481 SubstructureRedirectMask, &ev);
3483 case ConfigureRequest:
3486 handler[ev.type](&ev);
3489 /* don't allow to move window origin out of region */
3490 if ( ev.xmotion.x_root < r->g.x ||
3491 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3492 ev.xmotion.y_root < r->g.y ||
3493 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3496 win->g.x = ev.xmotion.x_root - border_width;
3497 win->g.y = ev.xmotion.y_root - border_width;
3499 /* not free, don't sync more than 120 times / second */
3500 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3501 time = ev.xmotion.time;
3502 XSync(display, False);
3507 } while (ev.type != ButtonRelease);
3509 XSync(display, False);
3512 store_float_geom(win,r);
3513 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3514 XUngrabPointer(display, CurrentTime);
3517 drain_enter_notify();
3520 /* user/key callable function IDs */
3579 kf_dumpwins, /* MUST BE LAST */
3583 /* key definitions */
3585 dummykeyfunc(struct swm_region *r, union arg *args)
3590 legacyfunc(struct swm_region *r, union arg *args)
3595 char name[SWM_FUNCNAME_LEN];
3596 void (*func)(struct swm_region *r, union arg *);
3598 } keyfuncs[kf_invalid + 1] = {
3599 /* name function argument */
3600 { "cycle_layout", cycle_layout, {0} },
3601 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
3602 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
3603 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
3604 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
3605 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
3606 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
3607 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
3608 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
3609 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
3610 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
3611 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
3612 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
3613 { "spawn_term", spawnterm, {.argv = spawn_term} },
3614 { "spawn_menu", legacyfunc, {0} },
3615 { "quit", quit, {0} },
3616 { "restart", restart, {0} },
3617 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
3618 { "ws_1", switchws, {.id = 0} },
3619 { "ws_2", switchws, {.id = 1} },
3620 { "ws_3", switchws, {.id = 2} },
3621 { "ws_4", switchws, {.id = 3} },
3622 { "ws_5", switchws, {.id = 4} },
3623 { "ws_6", switchws, {.id = 5} },
3624 { "ws_7", switchws, {.id = 6} },
3625 { "ws_8", switchws, {.id = 7} },
3626 { "ws_9", switchws, {.id = 8} },
3627 { "ws_10", switchws, {.id = 9} },
3628 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
3629 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
3630 { "ws_prior", priorws, {0} },
3631 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
3632 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
3633 { "mvws_1", send_to_ws, {.id = 0} },
3634 { "mvws_2", send_to_ws, {.id = 1} },
3635 { "mvws_3", send_to_ws, {.id = 2} },
3636 { "mvws_4", send_to_ws, {.id = 3} },
3637 { "mvws_5", send_to_ws, {.id = 4} },
3638 { "mvws_6", send_to_ws, {.id = 5} },
3639 { "mvws_7", send_to_ws, {.id = 6} },
3640 { "mvws_8", send_to_ws, {.id = 7} },
3641 { "mvws_9", send_to_ws, {.id = 8} },
3642 { "mvws_10", send_to_ws, {.id = 9} },
3643 { "bar_toggle", bar_toggle, {0} },
3644 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
3645 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
3646 { "screenshot_all", legacyfunc, {0} },
3647 { "screenshot_wind", legacyfunc, {0} },
3648 { "float_toggle", floating_toggle,{0} },
3649 { "version", version, {0} },
3650 { "spawn_lock", legacyfunc, {0} },
3651 { "spawn_initscr", legacyfunc, {0} },
3652 { "spawn_custom", dummykeyfunc, {0} },
3653 { "iconify", iconify, {0} },
3654 { "uniconify", uniconify, {0} },
3655 { "raise_toggle", raise_toggle, {0} },
3656 { "button2", pressbutton, {2} },
3657 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
3658 { "invalid key func", NULL, {0} },
3663 enum keyfuncid funcid;
3666 int keys_size = 0, keys_length = 0;
3667 struct key *keys = NULL;
3670 enum { client_click, root_click };
3672 unsigned int action;
3674 unsigned int button;
3675 void (*func)(struct ws_win *, union arg *);
3678 /* action key mouse button func args */
3679 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
3680 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
3681 { client_click, MODKEY, Button1, move, {0} },
3685 update_modkey(unsigned int mod)
3690 for (i = 0; i < keys_length; i++)
3691 if (keys[i].mod & ShiftMask)
3692 keys[i].mod = mod | ShiftMask;
3696 for (i = 0; i < LENGTH(buttons); i++)
3697 if (buttons[i].mask & ShiftMask)
3698 buttons[i].mask = mod | ShiftMask;
3700 buttons[i].mask = mod;
3710 int spawns_size = 0, spawns_length = 0;
3711 struct spawn_prog *spawns = NULL;
3714 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
3717 struct spawn_prog *prog = NULL;
3719 char *ap, **real_args;
3721 DNPRINTF(SWM_D_SPAWN, "spawn_expand %s\n", spawn_name);
3724 for (i = 0; i < spawns_length; i++) {
3725 if (!strcasecmp(spawn_name, spawns[i].name))
3729 fprintf(stderr, "spawn_custom: program %s not found\n",
3734 /* make room for expanded args */
3735 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
3736 err(1, "spawn_custom: calloc real_args");
3738 /* expand spawn_args into real_args */
3739 for (i = 0; i < prog->argc; i++) {
3741 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
3742 if (!strcasecmp(ap, "$bar_border")) {
3744 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
3746 err(1, "spawn_custom border color");
3747 } else if (!strcasecmp(ap, "$bar_color")) {
3749 strdup(r->s->c[SWM_S_COLOR_BAR].name))
3751 err(1, "spawn_custom bar color");
3752 } else if (!strcasecmp(ap, "$bar_font")) {
3753 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
3755 err(1, "spawn_custom bar fonts");
3756 } else if (!strcasecmp(ap, "$bar_font_color")) {
3758 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
3760 err(1, "spawn_custom color font");
3761 } else if (!strcasecmp(ap, "$color_focus")) {
3763 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
3765 err(1, "spawn_custom color focus");
3766 } else if (!strcasecmp(ap, "$color_unfocus")) {
3768 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
3770 err(1, "spawn_custom color unfocus");
3772 /* no match --> copy as is */
3773 if ((real_args[i] = strdup(ap)) == NULL)
3774 err(1, "spawn_custom strdup(ap)");
3776 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
3781 if ((swm_debug & SWM_D_SPAWN) != 0) {
3782 fprintf(stderr, "spawn_custom: result = ");
3783 for (i = 0; i < prog->argc; i++)
3784 fprintf(stderr, "\"%s\" ", real_args[i]);
3785 fprintf(stderr, "\n");
3788 *ret_args = real_args;
3789 return (prog->argc);
3793 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
3799 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3803 spawn(r->ws->idx, &a, 1);
3805 for (i = 0; i < spawn_argc; i++)
3811 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
3817 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3821 if (pipe(select_list_pipe) == -1)
3822 err(1, "pipe error");
3823 if (pipe(select_resp_pipe) == -1)
3824 err(1, "pipe error");
3826 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
3827 err(1, "could not disable SIGPIPE");
3828 switch (*pid = fork()) {
3830 err(1, "cannot fork");
3833 if (dup2(select_list_pipe[0], 0) == -1)
3835 if (dup2(select_resp_pipe[1], 1) == -1)
3837 close(select_list_pipe[1]);
3838 close(select_resp_pipe[0]);
3839 spawn(r->ws->idx, &a, 0);
3841 default: /* parent */
3842 close(select_list_pipe[0]);
3843 close(select_resp_pipe[1]);
3847 for (i = 0; i < spawn_argc; i++)
3853 setspawn(struct spawn_prog *prog)
3857 if (prog == NULL || prog->name == NULL)
3861 for (i = 0; i < spawns_length; i++) {
3862 if (!strcmp(spawns[i].name, prog->name)) {
3864 if (prog->argv == NULL) {
3866 DNPRINTF(SWM_D_SPAWN,
3867 "setspawn: delete #%d %s\n",
3869 free(spawns[i].name);
3870 for (j = 0; j < spawns[i].argc; j++)
3871 free(spawns[i].argv[j]);
3872 free(spawns[i].argv);
3873 j = spawns_length - 1;
3875 spawns[i] = spawns[j];
3880 DNPRINTF(SWM_D_SPAWN,
3881 "setspawn: replace #%d %s\n",
3883 free(spawns[i].name);
3884 for (j = 0; j < spawns[i].argc; j++)
3885 free(spawns[i].argv[j]);
3886 free(spawns[i].argv);
3889 /* found case handled */
3895 if (prog->argv == NULL) {
3897 "error: setspawn: cannot find program %s", prog->name);
3902 /* not found: add */
3903 if (spawns_size == 0 || spawns == NULL) {
3905 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
3906 spawns = malloc((size_t)spawns_size *
3907 sizeof(struct spawn_prog));
3908 if (spawns == NULL) {
3909 fprintf(stderr, "setspawn: malloc failed\n");
3913 } else if (spawns_length == spawns_size) {
3915 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
3916 spawns = realloc(spawns, (size_t)spawns_size *
3917 sizeof(struct spawn_prog));
3918 if (spawns == NULL) {
3919 fprintf(stderr, "setspawn: realloc failed\n");
3925 if (spawns_length < spawns_size) {
3926 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
3927 spawns_length, prog->name);
3928 i = spawns_length++;
3931 fprintf(stderr, "spawns array problem?\n");
3932 if (spawns == NULL) {
3933 fprintf(stderr, "spawns array is NULL!\n");
3941 setconfspawn(char *selector, char *value, int flags)
3943 struct spawn_prog *prog;
3944 char *vp, *cp, *word;
3946 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
3947 if ((prog = calloc(1, sizeof *prog)) == NULL)
3948 err(1, "setconfspawn: calloc prog");
3949 prog->name = strdup(selector);
3950 if (prog->name == NULL)
3951 err(1, "setconfspawn prog->name");
3952 if ((cp = vp = strdup(value)) == NULL)
3953 err(1, "setconfspawn: strdup(value) ");
3954 while ((word = strsep(&cp, " \t")) != NULL) {
3955 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
3957 cp += (long)strspn(cp, " \t");
3958 if (strlen(word) > 0) {
3960 if ((prog->argv = realloc(prog->argv,
3961 prog->argc * sizeof(char *))) == NULL)
3962 err(1, "setconfspawn: realloc");
3963 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
3964 err(1, "setconfspawn: strdup");
3971 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
3978 setconfspawn("term", "xterm", 0);
3979 setconfspawn("screenshot_all", "screenshot.sh full", 0);
3980 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
3981 setconfspawn("lock", "xlock", 0);
3982 setconfspawn("initscr", "initscreen.sh", 0);
3983 setconfspawn("menu", "dmenu_run"
3986 " -nf $bar_font_color"
3988 " -sf $bar_color", 0);
3989 setconfspawn("uniconify", "dmenu"
3993 " -nf $bar_font_color"
3995 " -sf $bar_color", 0);
3999 #define SWM_MODNAME_SIZE 32
4000 #define SWM_KEY_WS "\n+ \t"
4002 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
4006 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
4007 if (mod == NULL || ks == NULL) {
4008 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
4011 if (keystr == NULL || strlen(keystr) == 0) {
4012 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
4018 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
4019 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
4021 cp += (long)strspn(cp, SWM_KEY_WS);
4022 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
4024 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
4026 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
4028 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
4030 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
4032 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
4034 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
4035 *mod |= ControlMask;
4037 *ks = XStringToKeysym(name);
4038 XConvertCase(*ks, ks, &uks);
4039 if (ks == NoSymbol) {
4041 "parsekeys: invalid key %s\n",
4047 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
4052 strdupsafe(char *str)
4057 return (strdup(str));
4061 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
4065 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
4066 keyfuncs[kfid].name, spawn_name);
4068 for (i = 0; i < keys_length; i++) {
4069 if (keys[i].mod == mod && keys[i].keysym == ks) {
4070 if (kfid == kf_invalid) {
4073 "setkeybinding: delete #%d %s\n",
4074 i, keyfuncs[keys[i].funcid].name);
4075 free(keys[i].spawn_name);
4076 j = keys_length - 1;
4080 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4083 /* found: replace */
4085 "setkeybinding: replace #%d %s %s\n",
4086 i, keyfuncs[keys[i].funcid].name,
4088 free(keys[i].spawn_name);
4090 keys[i].keysym = ks;
4091 keys[i].funcid = kfid;
4092 keys[i].spawn_name = strdupsafe(spawn_name);
4093 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4098 if (kfid == kf_invalid) {
4100 "error: setkeybinding: cannot find mod/key combination");
4101 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4104 /* not found: add */
4105 if (keys_size == 0 || keys == NULL) {
4107 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
4108 keys = malloc((size_t)keys_size * sizeof(struct key));
4110 fprintf(stderr, "malloc failed\n");
4114 } else if (keys_length == keys_size) {
4116 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
4117 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
4119 fprintf(stderr, "realloc failed\n");
4124 if (keys_length < keys_size) {
4126 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
4127 j, keyfuncs[kfid].name, spawn_name);
4129 keys[j].keysym = ks;
4130 keys[j].funcid = kfid;
4131 keys[j].spawn_name = strdupsafe(spawn_name);
4133 fprintf(stderr, "keys array problem?\n");
4135 fprintf(stderr, "keys array problem\n");
4139 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4143 setconfbinding(char *selector, char *value, int flags)
4145 enum keyfuncid kfid;
4149 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
4150 if (selector == NULL) {
4151 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
4152 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4154 setkeybinding(mod, ks, kfid, NULL);
4159 /* search by key function name */
4160 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
4161 if (strncasecmp(selector, keyfuncs[kfid].name,
4162 SWM_FUNCNAME_LEN) == 0) {
4163 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4165 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4166 setkeybinding(mod, ks, kfid, NULL);
4172 /* search by custom spawn name */
4173 for (i = 0; i < spawns_length; i++) {
4174 if (strcasecmp(selector, spawns[i].name) == 0) {
4175 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4177 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4178 setkeybinding(mod, ks, kf_spawn_custom,
4185 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
4192 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
4193 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
4194 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
4195 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
4196 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
4197 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
4198 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
4199 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
4200 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
4201 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
4202 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
4203 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
4204 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
4205 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
4206 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
4207 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
4208 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
4209 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
4210 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
4211 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
4212 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
4213 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
4214 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
4215 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
4216 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
4217 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
4218 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
4219 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
4220 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
4221 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
4222 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
4223 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
4224 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
4225 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
4226 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
4227 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
4228 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
4229 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
4230 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
4231 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
4232 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
4233 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
4234 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
4235 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
4236 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
4237 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
4238 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
4239 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
4240 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
4241 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
4242 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
4243 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
4244 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
4245 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
4246 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
4247 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
4248 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
4249 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
4251 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
4256 updatenumlockmask(void)
4259 XModifierKeymap *modmap;
4261 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
4263 modmap = XGetModifierMapping(display);
4264 for (i = 0; i < 8; i++)
4265 for (j = 0; j < modmap->max_keypermod; j++)
4266 if (modmap->modifiermap[i * modmap->max_keypermod + j]
4267 == XKeysymToKeycode(display, XK_Num_Lock))
4268 numlockmask = (1 << i);
4270 XFreeModifiermap(modmap);
4276 unsigned int i, j, k;
4278 unsigned int modifiers[] =
4279 { 0, LockMask, numlockmask, numlockmask | LockMask };
4281 DNPRINTF(SWM_D_MISC, "grabkeys\n");
4282 updatenumlockmask();
4284 for (k = 0; k < ScreenCount(display); k++) {
4285 if (TAILQ_EMPTY(&screens[k].rl))
4287 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
4288 for (i = 0; i < keys_length; i++) {
4289 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
4290 for (j = 0; j < LENGTH(modifiers); j++)
4291 XGrabKey(display, code,
4292 keys[i].mod | modifiers[j],
4293 screens[k].root, True,
4294 GrabModeAsync, GrabModeAsync);
4300 grabbuttons(struct ws_win *win, int focused)
4303 unsigned int modifiers[] =
4304 { 0, LockMask, numlockmask, numlockmask|LockMask };
4306 updatenumlockmask();
4307 XUngrabButton(display, AnyButton, AnyModifier, win->id);
4309 for (i = 0; i < LENGTH(buttons); i++)
4310 if (buttons[i].action == client_click)
4311 for (j = 0; j < LENGTH(modifiers); j++)
4312 XGrabButton(display, buttons[i].button,
4313 buttons[i].mask | modifiers[j],
4314 win->id, False, BUTTONMASK,
4315 GrabModeAsync, GrabModeSync, None,
4318 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
4319 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
4322 const char *quirkname[] = {
4323 "NONE", /* config string for "no value" */
4332 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
4333 #define SWM_Q_WS "\n|+ \t"
4335 parsequirks(char *qstr, unsigned long *quirk)
4345 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
4347 cp += (long)strspn(cp, SWM_Q_WS);
4348 for (i = 0; i < LENGTH(quirkname); i++) {
4349 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
4350 DNPRINTF(SWM_D_QUIRK,
4351 "parsequirks: %s\n", name);
4356 *quirk |= 1 << (i-1);
4360 if (i >= LENGTH(quirkname)) {
4361 DNPRINTF(SWM_D_QUIRK,
4362 "parsequirks: invalid quirk [%s]\n", name);
4370 setquirk(const char *class, const char *name, const int quirk)
4375 for (i = 0; i < quirks_length; i++) {
4376 if (!strcmp(quirks[i].class, class) &&
4377 !strcmp(quirks[i].name, name)) {
4380 DNPRINTF(SWM_D_QUIRK,
4381 "setquirk: delete #%d %s:%s\n",
4382 i, quirks[i].class, quirks[i].name);
4383 free(quirks[i].class);
4384 free(quirks[i].name);
4385 j = quirks_length - 1;
4387 quirks[i] = quirks[j];
4391 /* found: replace */
4392 DNPRINTF(SWM_D_QUIRK,
4393 "setquirk: replace #%d %s:%s\n",
4394 i, quirks[i].class, quirks[i].name);
4395 free(quirks[i].class);
4396 free(quirks[i].name);
4397 quirks[i].class = strdup(class);
4398 quirks[i].name = strdup(name);
4399 quirks[i].quirk = quirk;
4406 "error: setquirk: cannot find class/name combination");
4409 /* not found: add */
4410 if (quirks_size == 0 || quirks == NULL) {
4412 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
4413 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
4414 if (quirks == NULL) {
4415 fprintf(stderr, "setquirk: malloc failed\n");
4419 } else if (quirks_length == quirks_size) {
4421 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
4422 quirks = realloc(quirks,
4423 (size_t)quirks_size * sizeof(struct quirk));
4424 if (quirks == NULL) {
4425 fprintf(stderr, "setquirk: realloc failed\n");
4430 if (quirks_length < quirks_size) {
4431 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
4432 j = quirks_length++;
4433 quirks[j].class = strdup(class);
4434 quirks[j].name = strdup(name);
4435 quirks[j].quirk = quirk;
4437 fprintf(stderr, "quirks array problem?\n");
4438 if (quirks == NULL) {
4439 fprintf(stderr, "quirks array problem!\n");
4446 setconfquirk(char *selector, char *value, int flags)
4448 char *cp, *class, *name;
4450 unsigned long quirks;
4451 if (selector == NULL)
4453 if ((cp = strchr(selector, ':')) == NULL)
4458 if ((retval = parsequirks(value, &quirks)) == 0)
4459 setquirk(class, name, quirks);
4466 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
4467 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
4468 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
4469 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
4470 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4471 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
4472 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4473 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4474 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4475 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4476 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
4477 setquirk("pcb", "pcb", SWM_Q_FLOAT);
4478 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
4481 /* conf file stuff */
4482 #define SWM_CONF_FILE "scrotwm.conf"
4484 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH,
4485 SWM_S_STACK_ENABLED, SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT,
4486 SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED,
4487 SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
4488 SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED, SWM_S_URGENT_ENABLED,
4489 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH,
4490 SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM,
4491 SWM_S_SS_APP, SWM_S_DIALOG_RATIO, SWM_S_BAR_AT_BOTTOM,
4492 SWM_S_VERBOSE_LAYOUT
4496 setconfvalue(char *selector, char *value, int flags)
4500 case SWM_S_BAR_DELAY:
4501 bar_delay = atoi(value);
4503 case SWM_S_BAR_ENABLED:
4504 bar_enabled = atoi(value);
4506 case SWM_S_BAR_BORDER_WIDTH:
4507 bar_border_width = atoi(value);
4509 case SWM_S_BAR_AT_BOTTOM:
4510 bar_at_bottom = atoi(value);
4512 case SWM_S_STACK_ENABLED:
4513 stack_enabled = atoi(value);
4515 case SWM_S_CLOCK_ENABLED:
4516 clock_enabled = atoi(value);
4518 case SWM_S_CLOCK_FORMAT:
4519 #ifndef SWM_DENY_CLOCK_FORMAT
4521 if ((clock_format = strdup(value)) == NULL)
4522 err(1, "setconfvalue: clock_format");
4525 case SWM_S_CYCLE_EMPTY:
4526 cycle_empty = atoi(value);
4528 case SWM_S_CYCLE_VISIBLE:
4529 cycle_visible = atoi(value);
4531 case SWM_S_SS_ENABLED:
4532 ss_enabled = atoi(value);
4534 case SWM_S_TERM_WIDTH:
4535 term_width = atoi(value);
4537 case SWM_S_TITLE_CLASS_ENABLED:
4538 title_class_enabled = atoi(value);
4540 case SWM_S_WINDOW_NAME_ENABLED:
4541 window_name_enabled = atoi(value);
4543 case SWM_S_TITLE_NAME_ENABLED:
4544 title_name_enabled = atoi(value);
4546 case SWM_S_URGENT_ENABLED:
4547 urgent_enabled = atoi(value);
4549 case SWM_S_FOCUS_MODE:
4550 if (!strcmp(value, "default"))
4551 focus_mode = SWM_FOCUS_DEFAULT;
4552 else if (!strcmp(value, "follow_cursor"))
4553 focus_mode = SWM_FOCUS_FOLLOW;
4554 else if (!strcmp(value, "synergy"))
4555 focus_mode = SWM_FOCUS_SYNERGY;
4557 err(1, "focus_mode");
4559 case SWM_S_DISABLE_BORDER:
4560 disable_border = atoi(value);
4562 case SWM_S_BORDER_WIDTH:
4563 border_width = atoi(value);
4565 case SWM_S_BAR_FONT:
4567 if ((bar_fonts[0] = strdup(value)) == NULL)
4568 err(1, "setconfvalue: bar_font");
4570 case SWM_S_BAR_ACTION:
4572 if ((bar_argv[0] = strdup(value)) == NULL)
4573 err(1, "setconfvalue: bar_action");
4575 case SWM_S_SPAWN_TERM:
4576 free(spawn_term[0]);
4577 if ((spawn_term[0] = strdup(value)) == NULL)
4578 err(1, "setconfvalue: spawn_term");
4582 case SWM_S_DIALOG_RATIO:
4583 dialog_ratio = atof(value);
4584 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
4587 case SWM_S_VERBOSE_LAYOUT:
4588 verbose_layout = atoi(value);
4589 for (i = 0; layouts[i].l_stack != NULL; i++) {
4591 layouts[i].l_string = fancy_stacker;
4593 layouts[i].l_string = plain_stacker;
4603 setconfmodkey(char *selector, char *value, int flags)
4605 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
4606 update_modkey(Mod1Mask);
4607 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
4608 update_modkey(Mod2Mask);
4609 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
4610 update_modkey(Mod3Mask);
4611 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
4612 update_modkey(Mod4Mask);
4619 setconfcolor(char *selector, char *value, int flags)
4621 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
4626 setconfregion(char *selector, char *value, int flags)
4628 custom_region(value);
4633 setautorun(char *selector, char *value, int flags)
4643 if (getenv("SWM_STARTED"))
4647 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
4648 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'\n");
4650 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
4651 errx(1, "autorun: invalid workspace %d\n", ws_id + 1);
4654 * This is a little intricate
4656 * If the pid already exists we simply reuse it because it means it was
4657 * used before AND not claimed by manage_window. We get away with
4658 * altering it in the parent after INSERT because this can not be a race
4661 while ((ap = strsep(&sp, " \t")) != NULL) {
4664 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
4666 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
4667 err(1, "setautorun: realloc");
4668 a.argv[argc - 1] = ap;
4671 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
4672 err(1, "setautorun: realloc");
4673 a.argv[argc] = NULL;
4675 if ((pid = fork()) == 0) {
4676 spawn(ws_id, &a, 1);
4685 p = calloc(1, sizeof *p);
4688 TAILQ_INSERT_TAIL(&pidlist, p, entry);
4698 setlayout(char *selector, char *value, int flags)
4700 int ws_id, st, i, x, mg, ma, si, raise;
4702 struct workspace *ws;
4704 if (getenv("SWM_STARTED"))
4708 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
4709 &ws_id, &mg, &ma, &si, &raise, s) != 6)
4710 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
4711 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
4714 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
4715 errx(1, "layout: invalid workspace %d\n", ws_id + 1);
4717 if (!strcasecmp(s, "vertical"))
4719 else if (!strcasecmp(s, "horizontal"))
4721 else if (!strcasecmp(s, "fullscreen"))
4724 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
4725 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
4728 for (i = 0; i < ScreenCount(display); i++) {
4729 ws = (struct workspace *)&screens[i].ws;
4730 ws[ws_id].cur_layout = &layouts[st];
4732 ws[ws_id].always_raise = raise;
4733 if (st == SWM_MAX_STACK)
4737 for (x = 0; x < abs(mg); x++) {
4738 ws[ws_id].cur_layout->l_config(&ws[ws_id],
4739 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
4740 SWM_ARG_ID_MASTERSHRINK);
4744 for (x = 0; x < abs(ma); x++) {
4745 ws[ws_id].cur_layout->l_config(&ws[ws_id],
4746 ma >= 0 ? SWM_ARG_ID_MASTERADD :
4747 SWM_ARG_ID_MASTERDEL);
4751 for (x = 0; x < abs(si); x++) {
4752 ws[ws_id].cur_layout->l_config(&ws[ws_id],
4753 si >= 0 ? SWM_ARG_ID_STACKINC :
4754 SWM_ARG_ID_STACKDEC);
4762 /* config options */
4763 struct config_option {
4765 int (*func)(char*, char*, int);
4768 struct config_option configopt[] = {
4769 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
4770 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
4771 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
4772 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
4773 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
4774 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
4775 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
4776 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
4777 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
4778 { "bind", setconfbinding, 0 },
4779 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
4780 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
4781 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
4782 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
4783 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
4784 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
4785 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
4786 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
4787 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
4788 { "modkey", setconfmodkey, 0 },
4789 { "program", setconfspawn, 0 },
4790 { "quirk", setconfquirk, 0 },
4791 { "region", setconfregion, 0 },
4792 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
4793 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
4794 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
4795 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
4796 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
4797 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
4798 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
4799 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
4800 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
4801 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
4802 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
4803 { "autorun", setautorun, 0 },
4804 { "layout", setlayout, 0 },
4809 conf_load(char *filename)
4812 char *line, *cp, *optsub, *optval;
4813 size_t linelen, lineno = 0;
4814 int wordlen, i, optind;
4815 struct config_option *opt;
4817 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
4819 if (filename == NULL) {
4820 fprintf(stderr, "conf_load: no filename\n");
4823 if ((config = fopen(filename, "r")) == NULL) {
4824 warn("conf_load: fopen");
4828 while (!feof(config)) {
4829 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
4832 err(1, "%s", filename);
4837 cp += strspn(cp, " \t\n"); /* eat whitespace */
4838 if (cp[0] == '\0') {
4843 /* get config option */
4844 wordlen = strcspn(cp, "=[ \t\n");
4846 warnx("%s: line %zd: no option found",
4851 for (i = 0; i < LENGTH(configopt); i++) {
4852 opt = &configopt[i];
4853 if (!strncasecmp(cp, opt->optname, wordlen) &&
4854 strlen(opt->optname) == wordlen) {
4860 warnx("%s: line %zd: unknown option %.*s",
4861 filename, lineno, wordlen, cp);
4865 cp += strspn(cp, " \t\n"); /* eat whitespace */
4866 /* get [selector] if any */
4870 wordlen = strcspn(cp, "]");
4873 warnx("%s: line %zd: syntax error",
4877 asprintf(&optsub, "%.*s", wordlen, cp);
4880 cp += strspn(cp, "] \t\n"); /* eat trailing */
4882 cp += strspn(cp, "= \t\n"); /* eat trailing */
4884 optval = strdup(cp);
4885 /* call function to deal with it all */
4886 if (configopt[optind].func(optsub, optval,
4887 configopt[optind].funcflags) != 0) {
4888 fprintf(stderr, "%s line %zd: %s\n",
4889 filename, lineno, line);
4890 errx(1, "%s: line %zd: invalid data for %s",
4891 filename, lineno, configopt[optind].optname);
4899 DNPRINTF(SWM_D_CONF, "conf_load end\n");
4905 set_child_transient(struct ws_win *win, Window *trans)
4907 struct ws_win *parent, *w;
4908 XWMHints *wmh = NULL;
4909 struct swm_region *r;
4910 struct workspace *ws;
4912 parent = find_window(win->transient);
4914 parent->child_trans = win;
4916 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
4917 " for %lu trans %lu\n", win->id, win->transient);
4919 if (win->hints == NULL) {
4920 fprintf(stderr, "no hints for %lu\n", win->id);
4924 r = root_to_region(win->wa.root);
4926 /* parent doen't exist in our window list */
4927 TAILQ_FOREACH(w, &ws->winlist, entry) {
4931 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
4932 fprintf(stderr, "can't get hints for %lu\n",
4937 if (win->hints->window_group != wmh->window_group)
4940 w->child_trans = win;
4941 win->transient = w->id;
4943 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
4944 "transient to %lu\n", win->transient);
4954 window_get_pid(Window win)
4956 Atom actual_type_return;
4957 int actual_format_return = 0;
4958 unsigned long nitems_return = 0;
4959 unsigned long bytes_after_return = 0;
4963 unsigned char *prop = NULL;
4965 if (XGetWindowProperty(display, win,
4966 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
4967 XA_CARDINAL, &actual_type_return, &actual_format_return,
4968 &nitems_return, &bytes_after_return,
4969 (unsigned char**)(void*)&pid) != Success)
4971 if (actual_type_return != XA_CARDINAL)
4982 if (XGetWindowProperty(display, win,
4983 XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
4984 XA_STRING, &actual_type_return, &actual_format_return,
4985 &nitems_return, &bytes_after_return, &prop) != Success)
4987 if (actual_type_return != XA_STRING)
4992 ret = strtonum((const char *)prop, 0, UINT_MAX, &errstr);
4993 /* ignore error because strtonum returns 0 anyway */
5000 manage_window(Window id)
5003 struct workspace *ws;
5004 struct ws_win *win, *ww;
5005 int format, i, ws_idx, n, border_me = 0;
5006 unsigned long nitems, bytes;
5007 Atom ws_idx_atom = 0, type;
5008 Atom *prot = NULL, *pp;
5009 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
5010 struct swm_region *r;
5016 if ((win = find_window(id)) != NULL)
5017 return (win); /* already being managed */
5019 /* see if we are on the unmanaged list */
5020 if ((win = find_unmanaged_window(id)) != NULL) {
5021 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
5023 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5024 if (win->transient) {
5025 set_child_transient(win, &trans);
5026 } if (trans && (ww = find_window(trans)))
5027 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
5029 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
5030 ewmh_update_actions(win);
5034 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
5035 errx(1, "calloc: failed to allocate memory for new window");
5039 /* see if we need to override the workspace */
5040 p = find_pid(window_get_pid(id));
5042 /* Get all the window data in one shot */
5043 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
5045 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
5046 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
5048 XGetWindowAttributes(display, id, &win->wa);
5049 XGetWMNormalHints(display, id, &win->sh, &mask);
5050 win->hints = XGetWMHints(display, id);
5051 XGetTransientForHint(display, id, &trans);
5053 win->transient = trans;
5054 set_child_transient(win, &trans);
5055 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
5056 win->id, win->transient);
5059 /* get supported protocols */
5060 if (XGetWMProtocols(display, id, &prot, &n)) {
5061 for (i = 0, pp = prot; i < n; i++, pp++) {
5062 if (*pp == takefocus)
5063 win->take_focus = 1;
5065 win->can_delete = 1;
5071 win->iconic = get_iconic(win);
5074 * Figure out where to put the window. If it was previously assigned to
5075 * a workspace (either by spawn() or manually moving), and isn't
5076 * transient, * put it in the same workspace
5078 r = root_to_region(win->wa.root);
5080 ws = &r->s->ws[p->ws];
5081 TAILQ_REMOVE(&pidlist, p, entry);
5084 } else if (prop && win->transient == 0) {
5085 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
5086 ws_idx = strtonum((const char *)prop, 0, 9, &errstr);
5088 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
5091 ws = &r->s->ws[ws_idx];
5094 /* this should launch transients in the same ws as parent */
5096 if ((ww = find_window(trans)) != NULL)
5103 "fix this bug mcbride\n");
5108 /* set up the window layout */
5111 win->s = r->s; /* this never changes */
5112 if (trans && (ww = find_window(trans)))
5113 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
5115 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
5117 win->g.w = win->wa.width;
5118 win->g.h = win->wa.height;
5119 win->g.x = win->wa.x;
5120 win->g.y = win->wa.y;
5121 win->g_floatvalid = 0;
5122 win->floatmaxed = 0;
5123 win->ewmh_flags = 0;
5125 /* Set window properties so we can remember this after reincarnation */
5126 if (ws_idx_atom && prop == NULL &&
5127 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
5129 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
5131 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
5132 PropModeReplace, ws_idx_str, SWM_PROPLEN);
5137 ewmh_autoquirk(win);
5139 if (XGetClassHint(display, win->id, &win->ch)) {
5140 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
5141 win->ch.res_class, win->ch.res_name);
5143 /* java is retarded so treat it special */
5144 if (strstr(win->ch.res_name, "sun-awt")) {
5149 for (i = 0; i < quirks_length; i++){
5150 if (!strcmp(win->ch.res_class, quirks[i].class) &&
5151 !strcmp(win->ch.res_name, quirks[i].name)) {
5152 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
5153 win->ch.res_class, win->ch.res_name);
5154 if (quirks[i].quirk & SWM_Q_FLOAT) {
5158 win->quirks = quirks[i].quirk;
5163 /* alter window position if quirky */
5164 if (win->quirks & SWM_Q_ANYWHERE) {
5165 win->manual = 1; /* don't center the quirky windows */
5166 bzero(&wc, sizeof wc);
5168 if (bar_enabled && win->g.y < bar_height) {
5169 win->g.y = wc.y = bar_height;
5172 if (win->g.w + win->g.x > WIDTH(r)) {
5173 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
5179 /* Reset font sizes (the bruteforce way; no default keybinding). */
5180 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
5181 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5182 fake_keypress(win, XK_KP_Subtract, ShiftMask);
5183 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5184 fake_keypress(win, XK_KP_Add, ShiftMask);
5187 ewmh_get_win_state(win);
5188 ewmh_update_actions(win);
5189 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
5193 bzero(&wc, sizeof wc);
5194 wc.border_width = border_width;
5195 mask = CWBorderWidth;
5196 XConfigureWindow(display, win->id, mask, &wc);
5199 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
5200 PropertyChangeMask | StructureNotifyMask);
5202 /* floaters need to be mapped if they are in the current workspace */
5203 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
5204 XMapRaised(display, win->id);
5210 free_window(struct ws_win *win)
5212 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
5217 /* needed for restart wm */
5218 set_win_state(win, WithdrawnState);
5220 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5222 if (win->ch.res_class)
5223 XFree(win->ch.res_class);
5224 if (win->ch.res_name)
5225 XFree(win->ch.res_name);
5230 memset(win, 0xff, sizeof *win); /* XXX kill later */
5236 unmanage_window(struct ws_win *win)
5238 struct ws_win *parent;
5243 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
5245 if (win->transient) {
5246 parent = find_window(win->transient);
5248 parent->child_trans = NULL;
5251 /* focus on root just in case */
5252 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
5256 TAILQ_REMOVE(&win->ws->winlist, win, entry);
5257 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
5263 focus_magic(struct ws_win *win)
5265 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu\n", WINID(win));
5270 if (win->child_trans) {
5271 /* win = parent & has a transient so focus on that */
5273 focus_win(win->child_trans);
5274 if (win->child_trans->take_focus)
5275 client_msg(win, takefocus);
5277 /* make sure transient hasn't dissapeared */
5278 if (validate_win(win->child_trans) == 0) {
5279 focus_win(win->child_trans);
5280 if (win->child_trans->take_focus)
5281 client_msg(win->child_trans, takefocus);
5283 win->child_trans = NULL;
5285 if (win->take_focus)
5286 client_msg(win, takefocus);
5292 if (win->take_focus)
5293 client_msg(win, takefocus);
5300 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
5308 XKeyEvent *ev = &e->xkey;
5310 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
5312 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
5313 for (i = 0; i < keys_length; i++)
5314 if (keysym == keys[i].keysym
5315 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
5316 && keyfuncs[keys[i].funcid].func) {
5317 if (keys[i].funcid == kf_spawn_custom)
5319 root_to_region(ev->root),
5320 &(keyfuncs[keys[i].funcid].args),
5324 keyfuncs[keys[i].funcid].func(
5325 root_to_region(ev->root),
5326 &(keyfuncs[keys[i].funcid].args)
5332 buttonpress(XEvent *e)
5336 XButtonPressedEvent *ev = &e->xbutton;
5338 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
5340 action = root_click;
5341 if ((win = find_window(ev->window)) == NULL)
5345 action = client_click;
5347 for (i = 0; i < LENGTH(buttons); i++)
5348 if (action == buttons[i].action && buttons[i].func &&
5349 buttons[i].button == ev->button &&
5350 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
5351 buttons[i].func(win, &buttons[i].args);
5355 configurerequest(XEvent *e)
5357 XConfigureRequestEvent *ev = &e->xconfigurerequest;
5362 if ((win = find_window(ev->window)) == NULL)
5363 if ((win = find_unmanaged_window(ev->window)) == NULL)
5367 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
5369 bzero(&wc, sizeof wc);
5372 wc.width = ev->width;
5373 wc.height = ev->height;
5374 wc.border_width = ev->border_width;
5375 wc.sibling = ev->above;
5376 wc.stack_mode = ev->detail;
5377 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
5379 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
5381 config_win(win, ev);
5386 configurenotify(XEvent *e)
5391 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
5392 e->xconfigure.window);
5394 win = find_window(e->xconfigure.window);
5396 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5400 if (focus_mode == SWM_FOCUS_DEFAULT)
5401 drain_enter_notify();
5406 destroynotify(XEvent *e)
5409 XDestroyWindowEvent *ev = &e->xdestroywindow;
5411 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
5413 if ((win = find_window(ev->window)) == NULL) {
5414 if ((win = find_unmanaged_window(ev->window)) == NULL)
5420 /* make sure we focus on something */
5423 unmanage_window(win);
5425 if (focus_mode == SWM_FOCUS_DEFAULT)
5426 drain_enter_notify();
5431 enternotify(XEvent *e)
5433 XCrossingEvent *ev = &e->xcrossing;
5438 Window focus_return;
5439 int revert_to_return;
5441 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
5442 "%lu subwindow %lu same_screen %d focus %d state %d\n",
5443 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
5444 ev->same_screen, ev->focus, ev->state);
5446 switch (focus_mode) {
5447 case SWM_FOCUS_DEFAULT:
5449 case SWM_FOCUS_FOLLOW:
5451 case SWM_FOCUS_SYNERGY:
5454 * all these checks need to be in this order because the
5455 * XCheckTypedWindowEvent relies on weeding out the previous events
5457 * making this code an option would enable a follow mouse for focus
5462 * state is set when we are switching workspaces and focus is set when
5463 * the window or a subwindow already has focus (occurs during restart).
5465 * Only honor the focus flag if last_focus_event is not FocusOut,
5466 * this allows scrotwm to continue to control focus when another
5467 * program is also playing with it.
5469 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
5470 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
5475 * happens when a window is created or destroyed and the border
5476 * crosses the mouse pointer and when switching ws
5478 * we need the subwindow test to see if we came from root in order
5479 * to give focus to floaters
5481 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
5482 ev->subwindow == 0) {
5483 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
5487 /* this window already has focus */
5488 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
5489 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
5493 /* this window is being deleted or moved to another ws */
5494 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
5496 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
5497 XPutBackEvent(display, &cne);
5501 if ((win = find_window(ev->window)) == NULL) {
5502 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5507 * In fullstack kill all enters unless they come from a different ws
5508 * (i.e. another region) or focus has been grabbed externally.
5510 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
5511 last_focus_event != FocusOut) {
5512 XGetInputFocus(display, &focus_return, &revert_to_return);
5513 if ((w = find_window(focus_return)) == NULL ||
5515 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
5523 if ((win = find_window(ev->window)) == NULL) {
5524 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5529 * if we have more enternotifies let them handle it in due time
5531 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
5532 DNPRINTF(SWM_D_EVENT,
5533 "ignoring enternotify: got more enternotify\n");
5534 XPutBackEvent(display, &cne);
5541 /* lets us use one switch statement for arbitrary mode/detail combinations */
5542 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
5545 focusevent(XEvent *e)
5549 u_int32_t mode_detail;
5550 XFocusChangeEvent *ev = &e->xfocus;
5552 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
5553 ev->type == FocusIn ? "entering" : "leaving",
5554 ev->window, ev->mode, ev->detail);
5556 if (last_focus_event == ev->type) {
5557 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
5561 last_focus_event = ev->type;
5562 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
5564 switch (mode_detail) {
5565 /* synergy client focus operations */
5566 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
5567 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
5569 /* synergy server focus operations */
5570 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
5572 /* Entering applications like rdesktop that mangle the pointer */
5573 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
5575 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
5576 XSetWindowBorder(display, win->id,
5577 win->ws->r->s->c[ev->type == FocusIn ?
5578 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
5581 fprintf(stderr, "ignoring focusevent\n");
5582 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
5589 mapnotify(XEvent *e)
5592 XMapEvent *ev = &e->xmap;
5594 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
5596 win = manage_window(ev->window);
5598 set_win_state(win, NormalState);
5602 mappingnotify(XEvent *e)
5604 XMappingEvent *ev = &e->xmapping;
5606 XRefreshKeyboardMapping(ev);
5607 if (ev->request == MappingKeyboard)
5612 maprequest(XEvent *e)
5615 struct swm_region *r;
5616 XWindowAttributes wa;
5617 XMapRequestEvent *ev = &e->xmaprequest;
5619 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
5620 e->xmaprequest.window);
5622 if (!XGetWindowAttributes(display, ev->window, &wa))
5624 if (wa.override_redirect)
5627 win = manage_window(e->xmaprequest.window);
5629 return; /* can't happen */
5633 /* make new win focused */
5634 r = root_to_region(win->wa.root);
5635 if (win->ws == r->ws)
5640 propertynotify(XEvent *e)
5643 XPropertyEvent *ev = &e->xproperty;
5645 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
5648 win = find_window(ev->window);
5652 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
5653 update_iconic(win, 0);
5654 XMapRaised(display, win->id);
5661 case XA_WM_NORMAL_HINTS:
5664 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5665 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
5666 if (win->sh.flags & PMinSize) {
5667 win->g.w = win->sh.min_width;
5668 win->g.h = win->sh.min_height;
5669 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
5671 XMoveResizeWindow(display, win->id,
5672 win->g.x, win->g.y, win->g.w, win->g.h);
5674 if (window_name_enabled)
5683 unmapnotify(XEvent *e)
5687 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
5689 /* determine if we need to help unmanage this window */
5690 win = find_window(e->xunmap.window);
5694 if (getstate(e->xunmap.window) == NormalState) {
5695 unmanage_window(win);
5698 /* giant hack for apps that don't destroy transient windows */
5699 /* eat a bunch of events to prevent remanaging the window */
5701 while (XCheckWindowEvent(display, e->xunmap.window,
5702 EnterWindowMask, &cne))
5704 while (XCheckWindowEvent(display, e->xunmap.window,
5705 StructureNotifyMask, &cne))
5707 while (XCheckWindowEvent(display, e->xunmap.window,
5708 SubstructureNotifyMask, &cne))
5710 /* resend unmap because we ated it */
5711 XUnmapWindow(display, e->xunmap.window);
5714 if (focus_mode == SWM_FOCUS_DEFAULT)
5715 drain_enter_notify();
5719 visibilitynotify(XEvent *e)
5722 struct swm_region *r;
5724 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
5725 e->xvisibility.window);
5726 if (e->xvisibility.state == VisibilityUnobscured)
5727 for (i = 0; i < ScreenCount(display); i++)
5728 TAILQ_FOREACH(r, &screens[i].rl, entry)
5729 if (e->xvisibility.window == r->bar_window)
5734 clientmessage(XEvent *e)
5736 XClientMessageEvent *ev;
5741 win = find_window(ev->window);
5745 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx type: %ld \n",
5746 ev->window, ev->message_type);
5748 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
5749 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW \n");
5752 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
5753 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW \n");
5754 if (win->can_delete)
5755 client_msg(win, adelete);
5757 XKillClient(display, win->id);
5759 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
5760 DNPRINTF(SWM_D_EVENT,
5761 "clientmessage: _NET_MOVERESIZE_WINDOW \n");
5762 if (win->floating) {
5763 if (ev->data.l[0] & (1<<8)) /* x */
5764 win->g.x = ev->data.l[1];
5765 if (ev->data.l[0] & (1<<9)) /* y */
5766 win->g.y = ev->data.l[2];
5767 if (ev->data.l[0] & (1<<10)) /* width */
5768 win->g.w = ev->data.l[3];
5769 if (ev->data.l[0] & (1<<11)) /* height */
5770 win->g.h = ev->data.l[4];
5773 /* TODO: Change stack sizes */
5775 config_win(win, NULL);
5777 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
5778 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE \n");
5779 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
5781 ewmh_update_win_state(win, ev->data.l[2],
5789 xerror_start(Display *d, XErrorEvent *ee)
5796 xerror(Display *d, XErrorEvent *ee)
5798 /* fprintf(stderr, "error: %p %p\n", display, ee); */
5806 xerrorxlib = XSetErrorHandler(xerror_start);
5808 /* this causes an error if some other window manager is running */
5809 XSelectInput(display, DefaultRootWindow(display),
5810 SubstructureRedirectMask);
5811 XSync(display, False);
5815 XSetErrorHandler(xerror);
5816 XSync(display, False);
5821 new_region(struct swm_screen *s, int x, int y, int w, int h)
5823 struct swm_region *r, *n;
5824 struct workspace *ws = NULL;
5827 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
5828 s->idx, w, h, x, y);
5830 /* remove any conflicting regions */
5831 n = TAILQ_FIRST(&s->rl);
5834 n = TAILQ_NEXT(r, entry);
5835 if (X(r) < (x + w) &&
5836 (X(r) + WIDTH(r)) > x &&
5838 (Y(r) + HEIGHT(r)) > y) {
5839 if (r->ws->r != NULL)
5840 r->ws->old_r = r->ws->r;
5842 XDestroyWindow(display, r->bar_window);
5843 TAILQ_REMOVE(&s->rl, r, entry);
5844 TAILQ_INSERT_TAIL(&s->orl, r, entry);
5848 /* search old regions for one to reuse */
5850 /* size + location match */
5851 TAILQ_FOREACH(r, &s->orl, entry)
5852 if (X(r) == x && Y(r) == y &&
5853 HEIGHT(r) == h && WIDTH(r) == w)
5857 TAILQ_FOREACH(r, &s->orl, entry)
5858 if (HEIGHT(r) == h && WIDTH(r) == w)
5862 TAILQ_REMOVE(&s->orl, r, entry);
5863 /* try to use old region's workspace */
5864 if (r->ws->r == NULL)
5867 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
5868 errx(1, "calloc: failed to allocate memory for screen");
5870 /* if we don't have a workspace already, find one */
5872 for (i = 0; i < SWM_WS_MAX; i++)
5873 if (s->ws[i].r == NULL) {
5880 errx(1, "no free workspaces\n");
5891 TAILQ_INSERT_TAIL(&s->rl, r, entry);
5897 #ifdef SWM_XRR_HAS_CRTC
5899 XRRScreenResources *sr;
5902 #endif /* SWM_XRR_HAS_CRTC */
5903 struct swm_region *r;
5906 if (i >= ScreenCount(display))
5907 errx(1, "invalid screen");
5909 /* remove any old regions */
5910 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
5911 r->ws->old_r = r->ws->r = NULL;
5912 XDestroyWindow(display, r->bar_window);
5913 TAILQ_REMOVE(&screens[i].rl, r, entry);
5914 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
5918 /* map virtual screens onto physical screens */
5919 #ifdef SWM_XRR_HAS_CRTC
5920 if (xrandr_support) {
5921 sr = XRRGetScreenResources(display, screens[i].root);
5923 new_region(&screens[i], 0, 0,
5924 DisplayWidth(display, i),
5925 DisplayHeight(display, i));
5929 for (c = 0, ci = NULL; c < ncrtc; c++) {
5930 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
5931 if (ci->noutput == 0)
5934 if (ci != NULL && ci->mode == None)
5935 new_region(&screens[i], 0, 0,
5936 DisplayWidth(display, i),
5937 DisplayHeight(display, i));
5939 new_region(&screens[i],
5940 ci->x, ci->y, ci->width, ci->height);
5943 XRRFreeCrtcInfo(ci);
5944 XRRFreeScreenResources(sr);
5946 #endif /* SWM_XRR_HAS_CRTC */
5948 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
5949 DisplayHeight(display, i));
5954 screenchange(XEvent *e) {
5955 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
5956 struct swm_region *r;
5959 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
5961 if (!XRRUpdateConfiguration(e))
5964 /* silly event doesn't include the screen index */
5965 for (i = 0; i < ScreenCount(display); i++)
5966 if (screens[i].root == xe->root)
5968 if (i >= ScreenCount(display))
5969 errx(1, "screenchange: screen not found\n");
5971 /* brute force for now, just re-enumerate the regions */
5974 /* add bars to all regions */
5975 for (i = 0; i < ScreenCount(display); i++)
5976 TAILQ_FOREACH(r, &screens[i].rl, entry)
5979 if (focus_mode == SWM_FOCUS_DEFAULT)
5980 drain_enter_notify();
5986 Window d1, d2, *wins = NULL;
5987 XWindowAttributes wa;
5992 for (i = 0; i < ScreenCount(display); i++) {
5993 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
5996 /* attach windows to a region */
5997 /* normal windows */
5998 for (j = 0; j < no; j++) {
5999 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6000 wa.override_redirect ||
6001 XGetTransientForHint(display, wins[j], &d1))
6004 state = getstate(wins[j]);
6005 manage = state == IconicState;
6006 if (wa.map_state == IsViewable || manage)
6007 manage_window(wins[j]);
6009 /* transient windows */
6010 for (j = 0; j < no; j++) {
6011 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6012 wa.override_redirect)
6015 state = getstate(wins[j]);
6016 manage = state == IconicState;
6017 if (XGetTransientForHint(display, wins[j], &d1) &&
6019 manage_window(wins[j]);
6032 int errorbase, major, minor;
6033 struct workspace *ws;
6035 if ((screens = calloc(ScreenCount(display),
6036 sizeof(struct swm_screen))) == NULL)
6037 errx(1, "calloc: screens");
6039 /* initial Xrandr setup */
6040 xrandr_support = XRRQueryExtension(display,
6041 &xrandr_eventbase, &errorbase);
6043 if (XRRQueryVersion(display, &major, &minor) && major < 1)
6046 /* map physical screens */
6047 for (i = 0; i < ScreenCount(display); i++) {
6048 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
6050 TAILQ_INIT(&screens[i].rl);
6051 TAILQ_INIT(&screens[i].orl);
6052 screens[i].root = RootWindow(display, i);
6054 /* set default colors */
6055 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
6056 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
6057 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
6058 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
6059 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
6061 /* set default cursor */
6062 XDefineCursor(display, screens[i].root,
6063 XCreateFontCursor(display, XC_left_ptr));
6065 /* init all workspaces */
6066 /* XXX these should be dynamically allocated too */
6067 for (j = 0; j < SWM_WS_MAX; j++) {
6068 ws = &screens[i].ws[j];
6073 TAILQ_INIT(&ws->winlist);
6074 TAILQ_INIT(&ws->unmanagedlist);
6076 for (k = 0; layouts[k].l_stack != NULL; k++)
6077 if (layouts[k].l_config != NULL)
6078 layouts[k].l_config(ws,
6079 SWM_ARG_ID_STACKINIT);
6080 ws->cur_layout = &layouts[0];
6081 ws->cur_layout->l_string(ws);
6087 XRRSelectInput(display, screens[i].root,
6088 RRScreenChangeNotifyMask);
6095 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
6097 err(1, "setup_globals: strdup");
6098 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
6100 err(1, "setup_globals: strdup");
6101 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
6103 err(1, "setup_globals: strdup");
6104 if ((spawn_term[0] = strdup("xterm")) == NULL)
6105 err(1, "setup_globals: strdup");
6106 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
6114 Atom netwmcheck, netwmname, utf8_string;
6117 /* work around sun jdk bugs, code from wmname */
6118 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
6119 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
6120 utf8_string = XInternAtom(display, "UTF8_STRING", False);
6121 for (i = 0; i < ScreenCount(display); i++) {
6122 root = screens[i].root;
6123 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
6124 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
6125 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
6127 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
6128 PropModeReplace, (unsigned char *)&win,1);
6129 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
6130 PropModeReplace, (unsigned char *)&win,1);
6131 XChangeProperty(display, win, netwmname, utf8_string, 8,
6132 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
6137 main(int argc, char *argv[])
6140 struct swm_region *r, *rr;
6141 struct ws_win *winfocus = NULL;
6144 char conf[PATH_MAX], *cfile = NULL;
6149 struct sigaction sact;
6152 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
6153 SWM_VERSION, cvstag);
6154 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
6155 warnx("no locale support");
6157 if (!(display = XOpenDisplay(0)))
6158 errx(1, "can not open display");
6161 errx(1, "other wm running");
6163 /* handle some signals */
6164 bzero(&sact, sizeof(sact));
6165 sigemptyset(&sact.sa_mask);
6167 sact.sa_handler = sighdlr;
6168 sigaction(SIGINT, &sact, NULL);
6169 sigaction(SIGQUIT, &sact, NULL);
6170 sigaction(SIGTERM, &sact, NULL);
6171 sigaction(SIGHUP, &sact, NULL);
6173 sact.sa_handler = sighdlr;
6174 sact.sa_flags = SA_NOCLDSTOP;
6175 sigaction(SIGCHLD, &sact, NULL);
6177 astate = XInternAtom(display, "WM_STATE", False);
6178 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
6179 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
6180 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
6181 a_wmname = XInternAtom(display, "WM_NAME", False);
6182 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
6183 a_string = XInternAtom(display, "STRING", False);
6184 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
6186 /* look for local and global conf file */
6187 pwd = getpwuid(getuid());
6189 errx(1, "invalid user %d", getuid());
6198 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
6199 if (stat(conf, &sb) != -1) {
6200 if (S_ISREG(sb.st_mode))
6203 /* try global conf file */
6204 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
6205 if (!stat(conf, &sb))
6206 if (S_ISREG(sb.st_mode))
6213 /* set some values to work around bad programs */
6215 /* grab existing windows (before we build the bars) */
6218 if (getenv("SWM_STARTED") == NULL)
6219 setenv("SWM_STARTED", "YES", 1);
6221 /* setup all bars */
6222 for (i = 0; i < ScreenCount(display); i++)
6223 TAILQ_FOREACH(r, &screens[i].rl, entry) {
6224 if (winfocus == NULL)
6225 winfocus = TAILQ_FIRST(&r->ws->winlist);
6233 if (focus_mode == SWM_FOCUS_DEFAULT)
6234 drain_enter_notify();
6236 xfd = ConnectionNumber(display);
6238 while (XPending(display)) {
6239 XNextEvent(display, &e);
6242 if (e.type < LASTEvent) {
6244 if (handler[e.type])
6245 handler[e.type](&e);
6247 DNPRINTF(SWM_D_EVENT,
6248 "win: %lu unknown event: %d\n",
6249 e.xany.window, e.type);
6251 switch (e.type - xrandr_eventbase) {
6252 case RRScreenChangeNotify:
6256 DNPRINTF(SWM_D_EVENT,
6257 "win: %lu unknown xrandr event: "
6258 "%d\n", e.xany.window, e.type);
6264 /* if we are being restarted go focus on first window */
6266 rr = winfocus->ws->r;
6268 /* not a visible window */
6272 /* move pointer to first screen if multi screen */
6273 if (ScreenCount(display) > 1 || outputs > 1)
6274 XWarpPointer(display, None, rr->s[0].root,
6275 0, 0, 0, 0, rr->g.x,
6276 rr->g.y + (bar_enabled ? bar_height : 0));
6278 a.id = SWM_ARG_ID_FOCUSCUR;
6288 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
6290 DNPRINTF(SWM_D_MISC, "select failed");
6291 if (restart_wm == 1)
6292 restart(NULL, NULL);
6293 if (search_resp == 1)
6305 XFreeGC(display, bar_gc);
6306 XCloseDisplay(display);