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.33"
75 #include <sys/types.h>
79 #include <sys/queue.h>
80 #include <sys/param.h>
81 #include <sys/select.h>
83 #include <X11/cursorfont.h>
84 #include <X11/keysym.h>
85 #include <X11/Xatom.h>
87 #include <X11/Xproto.h>
88 #include <X11/Xutil.h>
89 #include <X11/extensions/Xrandr.h>
96 # error XRandR versions less than 1.0 are not supported
101 #define SWM_XRR_HAS_CRTC
105 /*#define SWM_DEBUG*/
107 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
108 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
109 #define SWM_D_MISC 0x0001
110 #define SWM_D_EVENT 0x0002
111 #define SWM_D_WS 0x0004
112 #define SWM_D_FOCUS 0x0008
113 #define SWM_D_MOVE 0x0010
114 #define SWM_D_STACK 0x0020
115 #define SWM_D_MOUSE 0x0040
116 #define SWM_D_PROP 0x0080
117 #define SWM_D_CLASS 0x0100
118 #define SWM_D_KEY 0x0200
119 #define SWM_D_QUIRK 0x0400
120 #define SWM_D_SPAWN 0x0800
121 #define SWM_D_EVENTQ 0x1000
122 #define SWM_D_CONF 0x2000
124 u_int32_t swm_debug = 0
141 #define DPRINTF(x...)
142 #define DNPRINTF(n,x...)
145 #define LENGTH(x) (sizeof x / sizeof x[0])
146 #define MODKEY Mod1Mask
147 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
148 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
149 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
150 #define SWM_PROPLEN (16)
151 #define SWM_FUNCNAME_LEN (32)
152 #define SWM_KEYS_LEN (255)
153 #define SWM_QUIRK_LEN (64)
154 #define X(r) (r)->g.x
155 #define Y(r) (r)->g.y
156 #define WIDTH(r) (r)->g.w
157 #define HEIGHT(r) (r)->g.h
158 #define SWM_MAX_FONT_STEPS (3)
159 #define WINID(w) (w ? w->id : 0)
161 #define SWM_FOCUS_DEFAULT (0)
162 #define SWM_FOCUS_SYNERGY (1)
163 #define SWM_FOCUS_FOLLOW (2)
166 #define SWM_LIB "/usr/local/lib/libswmhack.so"
178 volatile sig_atomic_t running = 1;
179 volatile sig_atomic_t restart_wm = 0;
181 int last_focus_event = FocusOut;
182 int (*xerrorxlib)(Display *, XErrorEvent *);
186 int xrandr_eventbase;
187 unsigned int numlockmask = 0;
191 int cycle_visible = 0;
193 int font_adjusted = 0;
194 unsigned int mod_key = MODKEY;
197 struct swm_region *search_r;
198 int select_list_pipe[2];
199 int select_resp_pipe[2];
201 volatile sig_atomic_t search_resp;
204 double dialog_ratio = .6;
206 #define SWM_BAR_MAX (256)
207 char *bar_argv[] = { NULL, NULL };
209 char bar_ext[SWM_BAR_MAX];
210 char bar_vertext[SWM_BAR_MAX];
212 sig_atomic_t bar_alarm = 0;
215 int bar_border_width = 1;
216 int bar_at_bottom = 0;
218 int bar_extra_running = 0;
221 int stack_enabled = 1;
222 int clock_enabled = 1;
223 int urgent_enabled = 0;
224 char *clock_format = NULL;
225 int title_name_enabled = 0;
226 int title_class_enabled = 0;
227 int window_name_enabled = 0;
228 int focus_mode = SWM_FOCUS_DEFAULT;
229 int disable_border = 0;
230 int border_width = 1;
231 int verbose_layout = 0;
237 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
238 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
240 #define SWM_MENU_FN (2)
241 #define SWM_MENU_NB (4)
242 #define SWM_MENU_NF (6)
243 #define SWM_MENU_SB (8)
244 #define SWM_MENU_SF (10)
246 /* layout manager data */
247 struct swm_geometry {
257 /* virtual "screens" */
259 TAILQ_ENTRY(swm_region) entry;
260 struct swm_geometry g;
261 struct workspace *ws; /* current workspace on this region */
262 struct workspace *ws_prior; /* prior workspace on this region */
263 struct swm_screen *s; /* screen idx */
266 TAILQ_HEAD(swm_region_list, swm_region);
269 TAILQ_ENTRY(ws_win) entry;
272 struct ws_win *child_trans; /* transient child window */
273 struct swm_geometry g; /* current geometry */
274 struct swm_geometry g_float; /* geometry when floating */
275 struct swm_geometry rg_float; /* region geom when floating */
276 int g_floatvalid; /* flag: geometry in g_float is valid */
277 int floatmaxed; /* flag: floater was maxed in max_stack */
281 unsigned int ewmh_flags;
282 int font_size_boundary[SWM_MAX_FONT_STEPS];
288 unsigned long quirks;
289 struct workspace *ws; /* always valid */
290 struct swm_screen *s; /* always valid, never changes */
291 XWindowAttributes wa;
296 TAILQ_HEAD(ws_win_list, ws_win);
300 TAILQ_ENTRY(pid_e) entry;
304 TAILQ_HEAD(pid_list, pid_e);
305 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
307 /* layout handlers */
309 void vertical_config(struct workspace *, int);
310 void vertical_stack(struct workspace *, struct swm_geometry *);
311 void horizontal_config(struct workspace *, int);
312 void horizontal_stack(struct workspace *, struct swm_geometry *);
313 void max_stack(struct workspace *, struct swm_geometry *);
314 void plain_stacker(struct workspace *);
315 void fancy_stacker(struct workspace *);
317 struct ws_win *find_window(Window);
319 void grabbuttons(struct ws_win *, int);
320 void new_region(struct swm_screen *, int, int, int, int);
321 void unmanage_window(struct ws_win *);
322 long getstate(Window);
325 void (*l_stack)(struct workspace *, struct swm_geometry *);
326 void (*l_config)(struct workspace *, int);
328 #define SWM_L_FOCUSPREV (1<<0)
329 #define SWM_L_MAPONFOCUS (1<<1)
330 void (*l_string)(struct workspace *);
332 /* stack, configure */
333 { vertical_stack, vertical_config, 0, plain_stacker },
334 { horizontal_stack, horizontal_config, 0, plain_stacker },
336 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
337 { NULL, NULL, 0, NULL },
340 /* position of max_stack mode in the layouts array, index into layouts! */
341 #define SWM_V_STACK (0)
342 #define SWM_H_STACK (1)
343 #define SWM_MAX_STACK (2)
345 #define SWM_H_SLICE (32)
346 #define SWM_V_SLICE (32)
348 /* define work spaces */
350 int idx; /* workspace index */
351 int always_raise; /* raise windows on focus */
352 struct layout *cur_layout; /* current layout handlers */
353 struct ws_win *focus; /* may be NULL */
354 struct ws_win *focus_prev; /* may be NULL */
355 struct swm_region *r; /* may be NULL */
356 struct swm_region *old_r; /* may be NULL */
357 struct ws_win_list winlist; /* list of windows in ws */
358 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
359 char stacker[10]; /* display stacker and layout */
363 int horizontal_msize;
365 int horizontal_stacks;
372 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
373 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
375 /* physical screen mapping */
376 #define SWM_WS_MAX (10)
378 int idx; /* screen index */
379 struct swm_region_list rl; /* list of regions on this screen */
380 struct swm_region_list orl; /* list of old regions */
382 struct workspace ws[SWM_WS_MAX];
388 } c[SWM_S_COLOR_MAX];
390 struct swm_screen *screens;
393 /* args to functions */
396 #define SWM_ARG_ID_FOCUSNEXT (0)
397 #define SWM_ARG_ID_FOCUSPREV (1)
398 #define SWM_ARG_ID_FOCUSMAIN (2)
399 #define SWM_ARG_ID_FOCUSCUR (4)
400 #define SWM_ARG_ID_SWAPNEXT (10)
401 #define SWM_ARG_ID_SWAPPREV (11)
402 #define SWM_ARG_ID_SWAPMAIN (12)
403 #define SWM_ARG_ID_MOVELAST (13)
404 #define SWM_ARG_ID_MASTERSHRINK (20)
405 #define SWM_ARG_ID_MASTERGROW (21)
406 #define SWM_ARG_ID_MASTERADD (22)
407 #define SWM_ARG_ID_MASTERDEL (23)
408 #define SWM_ARG_ID_STACKRESET (30)
409 #define SWM_ARG_ID_STACKINIT (31)
410 #define SWM_ARG_ID_CYCLEWS_UP (40)
411 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
412 #define SWM_ARG_ID_CYCLESC_UP (42)
413 #define SWM_ARG_ID_CYCLESC_DOWN (43)
414 #define SWM_ARG_ID_STACKINC (50)
415 #define SWM_ARG_ID_STACKDEC (51)
416 #define SWM_ARG_ID_SS_ALL (60)
417 #define SWM_ARG_ID_SS_WINDOW (61)
418 #define SWM_ARG_ID_DONTCENTER (70)
419 #define SWM_ARG_ID_CENTER (71)
420 #define SWM_ARG_ID_KILLWINDOW (80)
421 #define SWM_ARG_ID_DELETEWINDOW (81)
425 void focus(struct swm_region *, union arg *);
426 void focus_magic(struct ws_win *);
433 #define SWM_Q_FLOAT (1<<0) /* float this window */
434 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
435 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
436 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
437 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
438 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
440 int quirks_size = 0, quirks_length = 0;
441 struct quirk *quirks = NULL;
444 * Supported EWMH hints should be added to
445 * both the enum and the ewmh array
447 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
448 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
449 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
450 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
451 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
452 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
453 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
454 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
455 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
456 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
462 } ewmh[SWM_EWMH_HINT_MAX] = {
463 /* must be in same order as in the enum */
464 {"_NET_ACTIVE_WINDOW", None},
465 {"_NET_MOVERESIZE_WINDOW", None},
466 {"_NET_CLOSE_WINDOW", None},
467 {"_NET_WM_WINDOW_TYPE", None},
468 {"_NET_WM_WINDOW_TYPE_DOCK", None},
469 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
470 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
471 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
472 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
473 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
474 {"_NET_WM_STATE", None},
475 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
476 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
477 {"_NET_WM_STATE_SKIP_TASKBAR", None},
478 {"_NET_WM_STATE_SKIP_PAGER", None},
479 {"_NET_WM_STATE_HIDDEN", None},
480 {"_NET_WM_STATE_ABOVE", None},
481 {"_SWM_WM_STATE_MANUAL", None},
482 {"_NET_WM_STATE_FULLSCREEN", None},
483 {"_NET_WM_ALLOWED_ACTIONS", None},
484 {"_NET_WM_ACTION_MOVE", None},
485 {"_NET_WM_ACTION_RESIZE", None},
486 {"_NET_WM_ACTION_FULLSCREEN", None},
487 {"_NET_WM_ACTION_CLOSE", None},
490 void store_float_geom(struct ws_win *win, struct swm_region *r);
491 int floating_toggle_win(struct ws_win *win);
492 void spawn_select(struct swm_region *, union arg *, char *, int *);
495 get_property(Window id, Atom atom, long count, Atom type,
496 unsigned long *n, unsigned char **data)
499 unsigned long tmp, extra;
500 unsigned long *nitems;
503 nitems = n != NULL ? n : &tmp;
504 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
505 &real, &format, nitems, &extra, data);
507 if (status != Success)
516 update_iconic(struct ws_win *win, int newv)
523 iprop = XInternAtom(display, "_SWM_ICONIC", False);
527 XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
528 PropModeReplace, (unsigned char *)&v, 1);
530 XDeleteProperty(display, win->id, iprop);
534 get_iconic(struct ws_win *win)
539 unsigned long nitems, extra;
540 unsigned char *prop = NULL;
542 iprop = XInternAtom(display, "_SWM_ICONIC", False);
545 status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
546 False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
547 if (status != Success)
549 if (rettype != XA_INTEGER || retfmt != 32)
553 v = *((int32_t *)prop);
567 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
569 for (i = 0; i < LENGTH(ewmh); i++)
570 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
572 for (i = 0; i < ScreenCount(display); i++) {
573 /* Support check window will be created by workaround(). */
575 /* Report supported atoms */
576 XDeleteProperty(display, screens[i].root, sup_list);
577 for (j = 0; j < LENGTH(ewmh); j++)
578 XChangeProperty(display, screens[i].root,
579 sup_list, XA_ATOM, 32,
580 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
588 unsigned char *data = NULL;
590 Atom sup_check, sup_list;
593 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
594 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
596 for (i = 0; i < ScreenCount(display); i++) {
597 /* Get the support check window and destroy it */
598 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
603 XDestroyWindow(display, id);
604 XDeleteProperty(display, screens[i].root, sup_check);
605 XDeleteProperty(display, screens[i].root, sup_list);
613 ewmh_autoquirk(struct ws_win *win)
616 unsigned long *data = NULL;
620 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
621 XA_ATOM, &n, (unsigned char **)&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, (unsigned char **)&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 < 0 || x > DisplayWidth(display, sidx) ||
1170 y < 0 || 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);
1266 bar_urgent(char *s, ssize_t sz)
1268 XWMHints *wmh = NULL;
1270 int i, j, got_some = 0;
1273 if (urgent_enabled == 0)
1277 for (i = 0; i < ScreenCount(display); i++)
1278 for (j = 0; j < SWM_WS_MAX; j++)
1279 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1280 wmh = XGetWMHints(display, win->id);
1284 if (wmh->flags & XUrgencyHint) {
1285 snprintf(b, sizeof b, "%d ", j + 1);
1286 strlcat(a, b, sizeof a);
1294 strlcat(s, " ", sz);
1296 strlcat(s, " ", sz);
1304 struct swm_region *r;
1307 char s[SWM_BAR_MAX];
1308 char cn[SWM_BAR_MAX];
1309 char loc[SWM_BAR_MAX];
1312 if (bar_enabled == 0)
1314 if (bar_extra && bar_extra_running) {
1315 /* ignore short reads; it'll correct itself */
1316 while ((b = fgetln(stdin, &len)) != NULL)
1317 if (b && b[len - 1] == '\n') {
1319 strlcpy(bar_ext, b, sizeof bar_ext);
1321 if (b == NULL && errno != EAGAIN) {
1322 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
1323 errno, strerror(errno));
1327 strlcpy(bar_ext, "", sizeof bar_ext);
1329 if (clock_enabled == 0)
1330 strlcpy(s, "", sizeof s);
1333 localtime_r(&tmt, &tm);
1334 strftime(s, sizeof s, clock_format, &tm);
1335 strlcat(s, " ", sizeof s);
1338 for (i = 0; i < ScreenCount(display); i++) {
1340 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1341 strlcpy(cn, "", sizeof cn);
1343 bar_urgent(cn, sizeof cn);
1344 bar_class_name(cn, sizeof cn, r->ws->focus);
1345 bar_window_name(cn, sizeof cn, r->ws->focus);
1348 snprintf(loc, sizeof loc, "%d:%d %s %s%s %s %s",
1349 x++, r->ws->idx + 1, r->ws->stacker, s, cn, bar_ext,
1364 bar_toggle(struct swm_region *r, union arg *args)
1366 struct swm_region *tmpr;
1367 int i, sc = ScreenCount(display);
1369 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
1372 for (i = 0; i < sc; i++)
1373 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1374 XUnmapWindow(display, tmpr->bar_window);
1376 for (i = 0; i < sc; i++)
1377 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1378 XMapRaised(display, tmpr->bar_window);
1380 bar_enabled = !bar_enabled;
1383 /* must be after stack */
1390 XSetWindowAttributes wa;
1391 struct swm_region *r;
1394 /* do this here because the conf file is in memory */
1395 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1396 /* launch external status app */
1397 bar_extra_running = 1;
1398 if (pipe(bar_pipe) == -1)
1399 err(1, "pipe error");
1400 socket_setnonblock(bar_pipe[0]);
1401 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1402 if (dup2(bar_pipe[0], 0) == -1)
1404 if (dup2(bar_pipe[1], 1) == -1)
1406 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1407 err(1, "could not disable SIGPIPE");
1408 switch (bar_pid = fork()) {
1410 err(1, "cannot fork");
1414 execvp(bar_argv[0], bar_argv);
1415 err(1, "%s external app failed", bar_argv[0]);
1417 default: /* parent */
1423 bzero(&wa, sizeof wa);
1424 for (i = 0; i < ScreenCount(display); i++)
1425 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1427 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1428 wa.background_pixel =
1429 screens[i].c[SWM_S_COLOR_BAR].color;
1430 XChangeWindowAttributes(display, r->bar_window,
1431 CWBackPixel | CWBorderPixel, &wa);
1437 bar_setup(struct swm_region *r)
1442 XFreeFont(display, bar_fs);
1446 for (i = 0; bar_fonts[i] != NULL; i++) {
1447 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
1453 if (bar_fonts[i] == NULL)
1454 errx(1, "couldn't load font");
1456 errx(1, "couldn't create font structure");
1458 bar_height = bar_fs->ascent + bar_fs->descent + 1 +
1459 2 * bar_border_width;
1461 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1463 r->bar_window = XCreateSimpleWindow(display,
1464 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width,
1465 bar_height - 2 * bar_border_width,
1466 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1467 r->s->c[SWM_S_COLOR_BAR].color);
1468 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
1469 XSetFont(display, bar_gc, bar_fs->fid);
1470 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1472 XMapRaised(display, r->bar_window);
1473 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
1475 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1476 err(1, "could not install bar_signal");
1481 drain_enter_notify(void)
1486 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1489 DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained %d\n", i);
1493 set_win_state(struct ws_win *win, long state)
1495 long data[] = {state, None};
1497 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1502 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1503 (unsigned char *)data, 2);
1510 unsigned char *p = NULL;
1513 if (!get_property(w, astate, 2L, astate, &n, &p))
1516 result = *((long *)p);
1522 version(struct swm_region *r, union arg *args)
1524 bar_version = !bar_version;
1526 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
1527 SWM_VERSION, cvstag);
1529 strlcpy(bar_vertext, "", sizeof bar_vertext);
1534 client_msg(struct ws_win *win, Atom a)
1536 XClientMessageEvent cm;
1541 bzero(&cm, sizeof cm);
1542 cm.type = ClientMessage;
1543 cm.window = win->id;
1544 cm.message_type = aprot;
1547 cm.data.l[1] = CurrentTime;
1548 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1552 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1560 DNPRINTF(SWM_D_MISC,
1561 "config_win: win %lu x %d y %d w %d h %d\n",
1562 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1564 ce.type = ConfigureNotify;
1565 ce.display = display;
1567 ce.window = win->id;
1570 ce.width = win->g.w;
1571 ce.height = win->g.h;
1572 ce.border_width = border_width;
1574 ce.override_redirect = False;
1576 DNPRINTF(SWM_D_MISC,
1577 "config_win: ev win %lu x %d y %d w %d h %d\n",
1578 ev->window, ev->x, ev->y, ev->width, ev->height);
1579 ce.type = ConfigureNotify;
1580 ce.display = ev->display;
1581 ce.event = ev->window;
1582 ce.window = ev->window;
1585 ce.width = ev->width;
1586 ce.height = ev->height;
1587 ce.border_width = ev->border_width;
1588 ce.above = ev->above;
1589 ce.override_redirect = False;
1592 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1596 count_win(struct workspace *ws, int count_transient)
1601 TAILQ_FOREACH(win, &ws->winlist, entry) {
1602 if (count_transient == 0 && win->floating)
1604 if (count_transient == 0 && win->transient)
1610 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1616 quit(struct swm_region *r, union arg *args)
1618 DNPRINTF(SWM_D_MISC, "quit\n");
1623 unmap_window(struct ws_win *win)
1628 /* don't unmap again */
1629 if (getstate(win->id) == IconicState)
1632 set_win_state(win, IconicState);
1634 XUnmapWindow(display, win->id);
1635 XSetWindowBorder(display, win->id,
1636 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1645 for (i = 0; i < ScreenCount(display); i++)
1646 for (j = 0; j < SWM_WS_MAX; j++)
1647 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1652 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1659 event.display = display; /* Ignored, but what the hell */
1660 event.window = win->id;
1661 event.root = win->s->root;
1662 event.subwindow = None;
1663 event.time = CurrentTime;
1668 event.same_screen = True;
1669 event.keycode = XKeysymToKeycode(display, keysym);
1670 event.state = modifiers;
1672 event.type = KeyPress;
1673 XSendEvent(event.display, event.window, True,
1674 KeyPressMask, (XEvent *)&event);
1676 event.type = KeyRelease;
1677 XSendEvent(event.display, event.window, True,
1678 KeyPressMask, (XEvent *)&event);
1683 restart(struct swm_region *r, union arg *args)
1685 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1687 /* disable alarm because the following code may not be interrupted */
1689 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1690 errx(1, "can't disable alarm");
1695 XCloseDisplay(display);
1696 execvp(start_argv[0], start_argv);
1697 fprintf(stderr, "execvp failed\n");
1703 root_to_region(Window root)
1705 struct swm_region *r = NULL;
1707 int i, x, y, wx, wy;
1710 for (i = 0; i < ScreenCount(display); i++)
1711 if (screens[i].root == root)
1714 if (XQueryPointer(display, screens[i].root,
1715 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1716 /* choose a region based on pointer location */
1717 TAILQ_FOREACH(r, &screens[i].rl, entry)
1718 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1719 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1724 r = TAILQ_FIRST(&screens[i].rl);
1730 find_unmanaged_window(Window id)
1735 for (i = 0; i < ScreenCount(display); i++)
1736 for (j = 0; j < SWM_WS_MAX; j++)
1737 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1745 find_window(Window id)
1748 Window wrr, wpr, *wcr = NULL;
1752 for (i = 0; i < ScreenCount(display); i++)
1753 for (j = 0; j < SWM_WS_MAX; j++)
1754 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1758 /* if we were looking for the parent return that window instead */
1759 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1764 /* ignore not found and root */
1765 if (wpr == 0 || wrr == wpr)
1768 /* look for parent */
1769 for (i = 0; i < ScreenCount(display); i++)
1770 for (j = 0; j < SWM_WS_MAX; j++)
1771 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1779 spawn(int ws_idx, union arg *args, int close_fd)
1784 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1787 close(ConnectionNumber(display));
1789 setenv("LD_PRELOAD", SWM_LIB, 1);
1791 if (asprintf(&ret, "%d", ws_idx) == -1) {
1795 setenv("_SWM_WS", ret, 1);
1799 if (asprintf(&ret, "%d", getpid()) == -1) {
1803 setenv("_SWM_PID", ret, 1);
1807 if (setsid() == -1) {
1814 * close stdin and stdout to prevent interaction between apps
1815 * and the baraction script
1816 * leave stderr open to record errors
1818 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
1822 dup2(fd, STDIN_FILENO);
1823 dup2(fd, STDOUT_FILENO);
1828 execvp(args->argv[0], args->argv);
1835 spawnterm(struct swm_region *r, union arg *args)
1837 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1841 setenv("_SWM_XTERM_FONTADJ", "", 1);
1842 spawn(r->ws->idx, args, 1);
1847 kill_refs(struct ws_win *win)
1850 struct swm_region *r;
1851 struct workspace *ws;
1856 for (i = 0; i < ScreenCount(display); i++)
1857 TAILQ_FOREACH(r, &screens[i].rl, entry)
1858 for (x = 0; x < SWM_WS_MAX; x++) {
1860 if (win == ws->focus)
1862 if (win == ws->focus_prev)
1863 ws->focus_prev = NULL;
1868 validate_win(struct ws_win *testwin)
1871 struct workspace *ws;
1872 struct swm_region *r;
1873 int i, x, foundit = 0;
1875 if (testwin == NULL)
1878 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1879 TAILQ_FOREACH(r, &screens[i].rl, entry)
1880 for (x = 0; x < SWM_WS_MAX; x++) {
1882 TAILQ_FOREACH(win, &ws->winlist, entry)
1890 validate_ws(struct workspace *testws)
1892 struct swm_region *r;
1893 struct workspace *ws;
1896 /* validate all ws */
1897 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1898 TAILQ_FOREACH(r, &screens[i].rl, entry)
1899 for (x = 0; x < SWM_WS_MAX; x++) {
1908 unfocus_win(struct ws_win *win)
1913 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1917 if (win->ws == NULL)
1920 if (validate_ws(win->ws))
1921 return; /* XXX this gets hit with thunderbird, needs fixing */
1923 if (win->ws->r == NULL)
1926 if (validate_win(win)) {
1931 if (win->ws->focus == win) {
1932 win->ws->focus = NULL;
1933 win->ws->focus_prev = win;
1936 if (validate_win(win->ws->focus)) {
1937 kill_refs(win->ws->focus);
1938 win->ws->focus = NULL;
1940 if (validate_win(win->ws->focus_prev)) {
1941 kill_refs(win->ws->focus_prev);
1942 win->ws->focus_prev = NULL;
1945 /* drain all previous unfocus events */
1946 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
1949 grabbuttons(win, 0);
1950 XSetWindowBorder(display, win->id,
1951 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1953 XChangeProperty(display, win->s->root,
1954 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1955 PropModeReplace, (unsigned char *)&none,1);
1964 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
1966 for (i = 0; i < ScreenCount(display); i++)
1967 for (j = 0; j < SWM_WS_MAX; j++)
1968 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1973 focus_win(struct ws_win *win)
1978 struct ws_win *cfw = NULL;
1981 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1985 if (win->ws == NULL)
1988 if (validate_ws(win->ws))
1989 return; /* XXX this gets hit with thunderbird, needs fixing */
1991 if (validate_win(win)) {
1996 if (validate_win(win)) {
2001 XGetInputFocus(display, &cur_focus, &rr);
2002 if ((cfw = find_window(cur_focus)) != NULL)
2005 /* use larger hammer since the window was killed somehow */
2006 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2007 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2008 XSetWindowBorder(display, cfw->id,
2009 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2012 win->ws->focus = win;
2014 if (win->ws->r != NULL) {
2015 /* drain all previous focus events */
2016 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
2020 XSetInputFocus(display, win->id,
2021 RevertToParent, CurrentTime);
2022 grabbuttons(win, 1);
2023 XSetWindowBorder(display, win->id,
2024 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2025 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2026 win->ws->always_raise)
2027 XMapRaised(display, win->id);
2029 XChangeProperty(display, win->s->root,
2030 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2031 PropModeReplace, (unsigned char *)&win->id,1);
2034 if (window_name_enabled)
2039 switchws(struct swm_region *r, union arg *args)
2041 int wsid = args->id, unmap_old = 0;
2042 struct swm_region *this_r, *other_r;
2044 struct workspace *new_ws, *old_ws;
2051 old_ws = this_r->ws;
2052 new_ws = &this_r->s->ws[wsid];
2054 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
2055 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
2058 if (new_ws == NULL || old_ws == NULL)
2060 if (new_ws == old_ws)
2063 other_r = new_ws->r;
2064 if (other_r == NULL) {
2065 /* the other workspace is hidden, hide this one */
2069 /* the other ws is visible in another region, exchange them */
2070 other_r->ws_prior = new_ws;
2071 other_r->ws = old_ws;
2072 old_ws->r = other_r;
2074 this_r->ws_prior = old_ws;
2075 this_r->ws = new_ws;
2078 /* this is needed so that we can click on a window after a restart */
2082 a.id = SWM_ARG_ID_FOCUSCUR;
2083 focus(new_ws->r, &a);
2087 /* unmap old windows */
2089 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2092 if (focus_mode == SWM_FOCUS_DEFAULT)
2093 drain_enter_notify();
2097 cyclews(struct swm_region *r, union arg *args)
2100 struct swm_screen *s = r->s;
2102 DNPRINTF(SWM_D_WS, "cyclews id %d "
2103 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2104 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2109 case SWM_ARG_ID_CYCLEWS_UP:
2110 if (a.id < SWM_WS_MAX - 1)
2115 case SWM_ARG_ID_CYCLEWS_DOWN:
2119 a.id = SWM_WS_MAX - 1;
2125 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
2127 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2131 } while (a.id != r->ws->idx);
2135 priorws(struct swm_region *r, union arg *args)
2139 DNPRINTF(SWM_D_WS, "priorws id %d "
2140 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2141 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2143 if (r->ws_prior == NULL)
2146 a.id = r->ws_prior->idx;
2151 cyclescr(struct swm_region *r, union arg *args)
2153 struct swm_region *rr = NULL;
2157 /* do nothing if we don't have more than one screen */
2158 if (!(ScreenCount(display) > 1 || outputs > 1))
2163 case SWM_ARG_ID_CYCLESC_UP:
2164 rr = TAILQ_NEXT(r, entry);
2166 rr = TAILQ_FIRST(&screens[i].rl);
2168 case SWM_ARG_ID_CYCLESC_DOWN:
2169 rr = TAILQ_PREV(r, swm_region_list, entry);
2171 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2179 /* move mouse to region */
2181 y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
2182 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2184 a.id = SWM_ARG_ID_FOCUSCUR;
2187 if (rr->ws->focus) {
2188 /* move to focus window */
2189 x = rr->ws->focus->g.x + 1;
2190 y = rr->ws->focus->g.y + 1;
2191 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2196 sort_windows(struct ws_win_list *wl)
2198 struct ws_win *win, *parent, *nxt;
2203 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2204 nxt = TAILQ_NEXT(win, entry);
2205 if (win->transient) {
2206 parent = find_window(win->transient);
2207 if (parent == NULL) {
2208 fprintf(stderr, "not possible bug\n");
2211 TAILQ_REMOVE(wl, win, entry);
2212 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2219 swapwin(struct swm_region *r, union arg *args)
2221 struct ws_win *target, *source;
2222 struct ws_win *cur_focus;
2223 struct ws_win_list *wl;
2226 DNPRINTF(SWM_D_WS, "swapwin id %d "
2227 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
2228 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2230 cur_focus = r->ws->focus;
2231 if (cur_focus == NULL)
2235 wl = &source->ws->winlist;
2238 case SWM_ARG_ID_SWAPPREV:
2239 if (source->transient)
2240 source = find_window(source->transient);
2241 target = TAILQ_PREV(source, ws_win_list, entry);
2242 if (target && target->transient)
2243 target = find_window(target->transient);
2244 TAILQ_REMOVE(wl, source, entry);
2246 TAILQ_INSERT_TAIL(wl, source, entry);
2248 TAILQ_INSERT_BEFORE(target, source, entry);
2250 case SWM_ARG_ID_SWAPNEXT:
2251 target = TAILQ_NEXT(source, entry);
2252 /* move the parent and let the sort handle the move */
2253 if (source->transient)
2254 source = find_window(source->transient);
2255 TAILQ_REMOVE(wl, source, entry);
2257 TAILQ_INSERT_HEAD(wl, source, entry);
2259 TAILQ_INSERT_AFTER(wl, target, source, entry);
2261 case SWM_ARG_ID_SWAPMAIN:
2262 target = TAILQ_FIRST(wl);
2263 if (target == source) {
2264 if (source->ws->focus_prev != NULL &&
2265 source->ws->focus_prev != target)
2267 source = source->ws->focus_prev;
2271 if (target == NULL || source == NULL)
2273 source->ws->focus_prev = target;
2274 TAILQ_REMOVE(wl, target, entry);
2275 TAILQ_INSERT_BEFORE(source, target, entry);
2276 TAILQ_REMOVE(wl, source, entry);
2277 TAILQ_INSERT_HEAD(wl, source, entry);
2279 case SWM_ARG_ID_MOVELAST:
2280 TAILQ_REMOVE(wl, source, entry);
2281 TAILQ_INSERT_TAIL(wl, source, entry);
2284 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
2294 focus_prev(struct ws_win *win)
2296 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2297 struct ws_win *cur_focus = NULL;
2298 struct ws_win_list *wl = NULL;
2299 struct workspace *ws = NULL;
2301 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
2303 if (!(win && win->ws))
2308 cur_focus = ws->focus;
2309 winlostfocus = cur_focus;
2311 /* pickle, just focus on whatever */
2312 if (cur_focus == NULL) {
2313 /* use prev_focus if valid */
2314 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2315 find_window(WINID(ws->focus_prev)))
2316 winfocus = ws->focus_prev;
2317 if (winfocus == NULL)
2318 winfocus = TAILQ_FIRST(wl);
2322 /* if transient focus on parent */
2323 if (cur_focus->transient) {
2324 winfocus = find_window(cur_focus->transient);
2328 /* if in max_stack try harder */
2329 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2330 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2331 if (cur_focus != ws->focus_prev)
2332 winfocus = ws->focus_prev;
2333 else if (cur_focus != ws->focus)
2334 winfocus = ws->focus;
2336 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2341 if (cur_focus == win)
2342 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2343 if (winfocus == NULL)
2344 winfocus = TAILQ_LAST(wl, ws_win_list);
2345 if (winfocus == NULL || winfocus == win)
2346 winfocus = TAILQ_NEXT(cur_focus, entry);
2348 if (winfocus == winlostfocus || winfocus == NULL)
2351 focus_magic(winfocus);
2355 focus(struct swm_region *r, union arg *args)
2357 struct ws_win *winfocus = NULL, *winlostfocus = NULL, *head;
2358 struct ws_win *cur_focus = NULL;
2359 struct ws_win_list *wl = NULL;
2360 struct workspace *ws = NULL;
2365 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
2367 /* treat FOCUS_CUR special */
2368 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2369 if (r->ws->focus && r->ws->focus->iconic == 0)
2370 winfocus = r->ws->focus;
2371 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2372 winfocus = r->ws->focus_prev;
2374 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2375 if (winfocus->iconic == 0)
2378 focus_magic(winfocus);
2382 if ((cur_focus = r->ws->focus) == NULL)
2387 winlostfocus = cur_focus;
2390 case SWM_ARG_ID_FOCUSPREV:
2391 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2393 head = TAILQ_LAST(wl, ws_win_list);
2395 if (WINID(winfocus) == cur_focus->transient) {
2396 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2398 head = TAILQ_LAST(wl, ws_win_list);
2403 if (winfocus && winfocus->iconic) {
2404 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
2405 if (winfocus->iconic == 0)
2410 case SWM_ARG_ID_FOCUSNEXT:
2411 head = TAILQ_NEXT(cur_focus, entry);
2413 head = TAILQ_FIRST(wl);
2417 if (winfocus && winfocus->iconic) {
2418 TAILQ_FOREACH(winfocus, wl, entry)
2419 if (winfocus->iconic == 0)
2424 case SWM_ARG_ID_FOCUSMAIN:
2425 winfocus = TAILQ_FIRST(wl);
2426 if (winfocus == cur_focus)
2427 winfocus = cur_focus->ws->focus_prev;
2433 if (winfocus == winlostfocus || winfocus == NULL)
2436 focus_magic(winfocus);
2440 cycle_layout(struct swm_region *r, union arg *args)
2442 struct workspace *ws = r->ws;
2443 struct ws_win *winfocus;
2446 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2448 winfocus = ws->focus;
2451 if (ws->cur_layout->l_stack == NULL)
2452 ws->cur_layout = &layouts[0];
2455 if (focus_mode == SWM_FOCUS_DEFAULT)
2456 drain_enter_notify();
2457 a.id = SWM_ARG_ID_FOCUSCUR;
2463 stack_config(struct swm_region *r, union arg *args)
2465 struct workspace *ws = r->ws;
2467 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
2470 if (ws->cur_layout->l_config != NULL)
2471 ws->cur_layout->l_config(ws, args->id);
2473 if (args->id != SWM_ARG_ID_STACKINIT)
2480 struct swm_geometry g;
2481 struct swm_region *r;
2484 DNPRINTF(SWM_D_STACK, "stack\n");
2486 for (i = 0; i < ScreenCount(display); i++) {
2488 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2489 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
2490 "(screen %d, region %d)\n", r->ws->idx, i, j++);
2492 /* start with screen geometry, adjust for bar */
2494 g.w -= 2 * border_width;
2495 g.h -= 2 * border_width;
2501 r->ws->cur_layout->l_stack(r->ws, &g);
2502 r->ws->cur_layout->l_string(r->ws);
2503 /* save r so we can track region changes */
2510 if (focus_mode == SWM_FOCUS_DEFAULT)
2511 drain_enter_notify();
2515 store_float_geom(struct ws_win *win, struct swm_region *r)
2517 /* retain window geom and region geom */
2518 win->g_float.x = win->g.x;
2519 win->g_float.y = win->g.y;
2520 win->g_float.w = win->g.w;
2521 win->g_float.h = win->g.h;
2522 win->rg_float.x = r->g.x;
2523 win->rg_float.y = r->g.y;
2524 win->rg_float.w = r->g.w;
2525 win->rg_float.h = r->g.h;
2526 win->g_floatvalid = 1;
2530 stack_floater(struct ws_win *win, struct swm_region *r)
2538 bzero(&wc, sizeof wc);
2539 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2542 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2543 * geom on ws switches or return from max mode
2546 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2547 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2549 * use stored g and rg to set relative position and size
2550 * as in old region or before max stack mode
2552 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2553 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2554 win->g.w = win->g_float.w;
2555 win->g.h = win->g_float.h;
2556 win->g_floatvalid = 0;
2559 win->floatmaxed = 0;
2561 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
2562 (win->g.h >= HEIGHT(r)))
2563 wc.border_width = 0;
2565 wc.border_width = border_width;
2566 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2567 win->g.w = (double)WIDTH(r) * dialog_ratio;
2568 win->g.h = (double)HEIGHT(r) * dialog_ratio;
2573 * floaters and transients are auto-centred unless moved
2576 win->g.x = r->g.x + (WIDTH(r) - win->g.w) /
2577 2 - wc.border_width;
2578 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) /
2579 2 - wc.border_width;
2582 /* win can be outside r if new r smaller than old r */
2583 /* Ensure top left corner inside r (move probs otherwise) */
2584 if (win->g.x < r->g.x - wc.border_width)
2585 win->g.x = r->g.x - wc.border_width;
2586 if (win->g.x > r->g.x + r->g.w - 1)
2587 win->g.x = (win->g.w > r->g.w) ? r->g.x :
2588 (r->g.x + r->g.w - win->g.w - 2 * wc.border_width);
2589 if (win->g.y < r->g.y - wc.border_width)
2590 win->g.y = r->g.y - wc.border_width;
2591 if (win->g.y > r->g.y + r->g.h - 1)
2592 win->g.y = (win->g.h > r->g.h) ? r->g.y :
2593 (r->g.y + r->g.h - win->g.h - 2 * wc.border_width);
2597 wc.width = win->g.w;
2598 wc.height = win->g.h;
2601 * Retain floater and transient geometry for correct positioning
2602 * when ws changes region
2604 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2605 store_float_geom(win, r);
2607 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
2608 win->id, wc.x, wc.y, wc.width, wc.height);
2610 XConfigureWindow(display, win->id, mask, &wc);
2614 * Send keystrokes to terminal to decrease/increase the font size as the
2615 * window size changes.
2618 adjust_font(struct ws_win *win)
2620 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2621 win->floating || win->transient)
2624 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2625 win->g.w / win->sh.width_inc < term_width &&
2626 win->font_steps < SWM_MAX_FONT_STEPS) {
2627 win->font_size_boundary[win->font_steps] =
2628 (win->sh.width_inc * term_width) + win->sh.base_width;
2631 win->last_inc = win->sh.width_inc;
2632 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2633 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2634 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
2637 win->last_inc = win->sh.width_inc;
2638 fake_keypress(win, XK_KP_Add, ShiftMask);
2642 #define SWAPXY(g) do { \
2644 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2645 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2648 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2651 XWindowAttributes wa;
2652 struct swm_geometry win_g, r_g = *g;
2653 struct ws_win *win, *fs_win = 0;
2654 int i, j, s, stacks;
2655 int w_inc = 1, h_inc, w_base = 1, h_base;
2656 int hrh, extra = 0, h_slice, last_h = 0;
2657 int split, colno, winno, mwin, msize, mscale;
2658 int remain, missing, v_slice, reconfigure;
2661 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2662 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2664 winno = count_win(ws, 0);
2665 if (winno == 0 && count_win(ws, 1) == 0)
2668 TAILQ_FOREACH(win, &ws->winlist, entry)
2669 if (win->transient == 0 && win->floating == 0
2670 && win->iconic == 0)
2677 w_inc = win->sh.width_inc;
2678 w_base = win->sh.base_width;
2679 mwin = ws->l_state.horizontal_mwin;
2680 mscale = ws->l_state.horizontal_msize;
2681 stacks = ws->l_state.horizontal_stacks;
2684 w_inc = win->sh.height_inc;
2685 w_base = win->sh.base_height;
2686 mwin = ws->l_state.vertical_mwin;
2687 mscale = ws->l_state.vertical_msize;
2688 stacks = ws->l_state.vertical_stacks;
2692 if (stacks > winno - mwin)
2693 stacks = winno - mwin;
2697 h_slice = r_g.h / SWM_H_SLICE;
2698 if (mwin && winno > mwin) {
2699 v_slice = r_g.w / SWM_V_SLICE;
2703 win_g.w = v_slice * mscale;
2705 if (w_inc > 1 && w_inc < v_slice) {
2706 /* adjust for window's requested size increment */
2707 remain = (win_g.w - w_base) % w_inc;
2708 missing = w_inc - remain;
2715 win_g.x += r_g.w - msize;
2718 colno = split = winno / stacks;
2719 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
2720 2 * border_width) / stacks);
2722 hrh = r_g.h / colno;
2723 extra = r_g.h - (colno * hrh);
2724 win_g.h = hrh - 2 * border_width;
2726 /* stack all the tiled windows */
2727 i = j = 0, s = stacks;
2728 TAILQ_FOREACH(win, &ws->winlist, entry) {
2729 if (win->transient != 0 || win->floating != 0)
2731 if (win->iconic != 0)
2734 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2739 if (split && i == split) {
2740 colno = (winno - mwin) / stacks;
2741 if (s <= (winno - mwin) % stacks)
2743 split = split + colno;
2744 hrh = (r_g.h / colno);
2745 extra = r_g.h - (colno * hrh);
2749 win_g.x += win_g.w + 2 * border_width;
2750 win_g.w = (r_g.w - msize -
2751 (stacks * 2 * border_width)) / stacks;
2753 win_g.w += (r_g.w - msize -
2754 (stacks * 2 * border_width)) % stacks;
2758 win_g.h = hrh - 2 * border_width;
2760 h_inc = win->sh.width_inc;
2761 h_base = win->sh.base_width;
2763 h_inc = win->sh.height_inc;
2764 h_base = win->sh.base_height;
2766 if (j == colno - 1) {
2767 win_g.h = hrh + extra;
2768 } else if (h_inc > 1 && h_inc < h_slice) {
2769 /* adjust for window's requested size increment */
2770 remain = (win_g.h - h_base) % h_inc;
2771 missing = h_inc - remain;
2773 if (missing <= extra || j == 0) {
2785 win_g.y += last_h + 2 * border_width;
2787 bzero(&wc, sizeof wc);
2788 if (disable_border && bar_enabled == 0 && winno == 1){
2789 wc.border_width = 0;
2790 win_g.w += 2 * border_width;
2791 win_g.h += 2 * border_width;
2793 wc.border_width = border_width;
2796 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2797 win->g.w != win_g.h || win->g.h != win_g.w) {
2799 win->g.x = wc.x = win_g.y;
2800 win->g.y = wc.y = win_g.x;
2801 win->g.w = wc.width = win_g.h;
2802 win->g.h = wc.height = win_g.w;
2805 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2806 win->g.w != win_g.w || win->g.h != win_g.h) {
2808 win->g.x = wc.x = win_g.x;
2809 win->g.y = wc.y = win_g.y;
2810 win->g.w = wc.width = win_g.w;
2811 win->g.h = wc.height = win_g.h;
2816 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2817 XConfigureWindow(display, win->id, mask, &wc);
2820 if (XGetWindowAttributes(display, win->id, &wa))
2821 if (wa.map_state == IsUnmapped)
2822 XMapRaised(display, win->id);
2830 /* now, stack all the floaters and transients */
2831 TAILQ_FOREACH(win, &ws->winlist, entry) {
2832 if (win->transient == 0 && win->floating == 0)
2834 if (win->iconic == 1)
2836 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2841 stack_floater(win, ws->r);
2842 XMapRaised(display, win->id);
2846 stack_floater(fs_win, ws->r);
2847 XMapRaised(display, fs_win->id);
2852 vertical_config(struct workspace *ws, int id)
2854 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2857 case SWM_ARG_ID_STACKRESET:
2858 case SWM_ARG_ID_STACKINIT:
2859 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2860 ws->l_state.vertical_mwin = 1;
2861 ws->l_state.vertical_stacks = 1;
2863 case SWM_ARG_ID_MASTERSHRINK:
2864 if (ws->l_state.vertical_msize > 1)
2865 ws->l_state.vertical_msize--;
2867 case SWM_ARG_ID_MASTERGROW:
2868 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2869 ws->l_state.vertical_msize++;
2871 case SWM_ARG_ID_MASTERADD:
2872 ws->l_state.vertical_mwin++;
2874 case SWM_ARG_ID_MASTERDEL:
2875 if (ws->l_state.vertical_mwin > 0)
2876 ws->l_state.vertical_mwin--;
2878 case SWM_ARG_ID_STACKINC:
2879 ws->l_state.vertical_stacks++;
2881 case SWM_ARG_ID_STACKDEC:
2882 if (ws->l_state.vertical_stacks > 1)
2883 ws->l_state.vertical_stacks--;
2891 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2893 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2895 stack_master(ws, g, 0, 0);
2899 horizontal_config(struct workspace *ws, int id)
2901 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2904 case SWM_ARG_ID_STACKRESET:
2905 case SWM_ARG_ID_STACKINIT:
2906 ws->l_state.horizontal_mwin = 1;
2907 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2908 ws->l_state.horizontal_stacks = 1;
2910 case SWM_ARG_ID_MASTERSHRINK:
2911 if (ws->l_state.horizontal_msize > 1)
2912 ws->l_state.horizontal_msize--;
2914 case SWM_ARG_ID_MASTERGROW:
2915 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2916 ws->l_state.horizontal_msize++;
2918 case SWM_ARG_ID_MASTERADD:
2919 ws->l_state.horizontal_mwin++;
2921 case SWM_ARG_ID_MASTERDEL:
2922 if (ws->l_state.horizontal_mwin > 0)
2923 ws->l_state.horizontal_mwin--;
2925 case SWM_ARG_ID_STACKINC:
2926 ws->l_state.horizontal_stacks++;
2928 case SWM_ARG_ID_STACKDEC:
2929 if (ws->l_state.horizontal_stacks > 1)
2930 ws->l_state.horizontal_stacks--;
2938 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2940 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
2942 stack_master(ws, g, 1, 0);
2945 /* fullscreen view */
2947 max_stack(struct workspace *ws, struct swm_geometry *g)
2950 struct swm_geometry gg = *g;
2951 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2955 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2960 winno = count_win(ws, 0);
2961 if (winno == 0 && count_win(ws, 1) == 0)
2964 TAILQ_FOREACH(win, &ws->winlist, entry) {
2965 if (win->transient) {
2967 parent = find_window(win->transient);
2971 if (win->floating && win->floatmaxed == 0 ) {
2973 * retain geometry for retrieval on exit from
2976 store_float_geom(win, ws->r);
2977 win->floatmaxed = 1;
2980 /* only reconfigure if necessary */
2981 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2983 bzero(&wc, sizeof wc);
2984 win->g.x = wc.x = gg.x;
2985 win->g.y = wc.y = gg.y;
2987 wc.border_width = border_width;
2988 win->g.w = wc.width = gg.w;
2989 win->g.h = wc.height = gg.h;
2991 wc.border_width = 0;
2992 win->g.w = wc.width = gg.w + 2 * border_width;
2993 win->g.h = wc.height = gg.h + 2 * border_width;
2995 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2996 XConfigureWindow(display, win->id, mask, &wc);
2998 /* unmap only if we don't have multi screen */
2999 if (win != ws->focus)
3000 if (!(ScreenCount(display) > 1 || outputs > 1))
3004 /* put the last transient on top */
3007 XMapRaised(display, parent->id);
3008 stack_floater(wintrans, ws->r);
3009 focus_magic(wintrans);
3014 send_to_ws(struct swm_region *r, union arg *args)
3016 int wsid = args->id;
3017 struct ws_win *win = NULL, *parent;
3018 struct workspace *ws, *nws;
3019 Atom ws_idx_atom = 0;
3020 unsigned char ws_idx_str[SWM_PROPLEN];
3029 if (win->ws->idx == wsid)
3032 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
3035 nws = &win->s->ws[wsid];
3037 a.id = SWM_ARG_ID_FOCUSPREV;
3039 if (win->transient) {
3040 parent = find_window(win->transient);
3042 unmap_window(parent);
3043 TAILQ_REMOVE(&ws->winlist, parent, entry);
3044 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3049 TAILQ_REMOVE(&ws->winlist, win, entry);
3050 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3053 /* Try to update the window's workspace property */
3054 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3056 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
3057 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3059 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3060 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3067 raise_toggle(struct swm_region *r, union arg *args)
3069 if (r && r->ws == NULL)
3072 r->ws->always_raise = !r->ws->always_raise;
3074 /* bring floaters back to top */
3075 if (r->ws->always_raise == 0)
3080 iconify(struct swm_region *r, union arg *args)
3084 if (r->ws->focus == NULL)
3086 unmap_window(r->ws->focus);
3087 update_iconic(r->ws->focus, 1);
3089 if (focus_mode == SWM_FOCUS_DEFAULT)
3090 drain_enter_notify();
3091 r->ws->focus = NULL;
3092 a.id = SWM_ARG_ID_FOCUSCUR;
3097 get_win_name(Display *dpy, Window win, Atom wname, Atom stype,
3098 unsigned long *slen)
3101 unsigned long nitems, nbytes, nextra;
3102 unsigned char *prop = NULL;
3105 status = XGetWindowProperty(dpy, win, wname, 0L, 0L, False, stype,
3106 &rettype, &retfmt, &nitems, &nbytes, &prop);
3107 if (status != Success)
3111 status = XGetWindowProperty(dpy, win, wname, 0L, nbytes, False,
3112 stype, &rettype, &retfmt, &nitems, &nextra, &prop);
3113 if (status != Success) {
3117 if (rettype != stype) {
3126 uniconify(struct swm_region *r, union arg *args)
3134 DNPRINTF(SWM_D_MISC, "uniconify\n");
3136 if (r && r->ws == NULL)
3139 /* make sure we have anything to uniconify */
3140 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3141 if (win->ws == NULL)
3142 continue; /* should never happen */
3143 if (win->iconic == 0)
3152 spawn_select(r, args, "uniconify", &searchpid);
3154 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3157 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3158 if (win->ws == NULL)
3159 continue; /* should never happen */
3160 if (win->iconic == 0)
3163 name = get_win_name(display, win->id, a_wmname, a_string,
3167 fprintf(lfile, "%s.%lu\n", name, win->id);
3174 #define MAX_RESP_LEN 1024
3177 search_do_resp(void)
3181 char *name, *resp, *s;
3184 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3189 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3190 fprintf(stderr, "search: calloc\n");
3194 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3196 fprintf(stderr, "search: read error: %s\n", strerror(errno));
3199 resp[rbytes] = '\0';
3202 DNPRINTF(SWM_D_MISC, "search_do_resp: resp %s\n", resp);
3203 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3204 if (win->iconic == 0)
3206 name = get_win_name(display, win->id, a_wmname, a_string, &len);
3209 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3214 if (strncmp(s, resp, len) == 0) {
3215 /* XXX this should be a callback to generalize */
3216 update_iconic(win, 0);
3227 wkill(struct swm_region *r, union arg *args)
3229 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
3231 if (r->ws->focus == NULL)
3234 if (args->id == SWM_ARG_ID_KILLWINDOW)
3235 XKillClient(display, r->ws->focus->id);
3237 if (r->ws->focus->can_delete)
3238 client_msg(r->ws->focus, adelete);
3243 floating_toggle_win(struct ws_win *win)
3245 struct swm_region *r;
3255 /* reject floating toggles in max stack mode */
3256 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3259 if (win->floating) {
3260 if (!win->floatmaxed) {
3261 /* retain position for refloat */
3262 store_float_geom(win, r);
3266 if (win->g_floatvalid) {
3267 /* refloat at last floating relative position */
3268 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
3269 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
3270 win->g.w = win->g_float.w;
3271 win->g.h = win->g_float.h;
3276 ewmh_update_actions(win);
3282 floating_toggle(struct swm_region *r, union arg *args)
3284 struct ws_win *win = r->ws->focus;
3290 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3291 _NET_WM_STATE_TOGGLE);
3294 if (focus_mode == SWM_FOCUS_DEFAULT)
3295 drain_enter_notify();
3297 if (win == win->ws->focus) {
3298 a.id = SWM_ARG_ID_FOCUSCUR;
3299 focus(win->ws->r, &a);
3304 resize_window(struct ws_win *win, int center)
3308 struct swm_region *r;
3310 r = root_to_region(win->wa.root);
3311 bzero(&wc, sizeof wc);
3312 mask = CWBorderWidth | CWWidth | CWHeight;
3313 wc.border_width = border_width;
3314 wc.width = win->g.w;
3315 wc.height = win->g.h;
3316 if (center == SWM_ARG_ID_CENTER) {
3317 wc.x = (WIDTH(r) - win->g.w) / 2 - border_width;
3318 wc.y = (HEIGHT(r) - win->g.h) / 2 - border_width;
3322 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
3323 win->id, wc.x, wc.y, wc.width, wc.height);
3325 XConfigureWindow(display, win->id, mask, &wc);
3329 resize(struct ws_win *win, union arg *args)
3333 struct swm_region *r = win->ws->r;
3337 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
3338 win->id, win->floating, win->transient);
3340 if (!(win->transient != 0 || win->floating != 0))
3343 /* reject resizes in max mode for floaters (transient ok) */
3344 if (win->floatmaxed)
3348 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3352 if (focus_mode == SWM_FOCUS_DEFAULT)
3353 drain_enter_notify();
3355 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3356 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3359 /* place pointer at bottom left corner or nearest point inside r */
3360 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
3361 relx = win->g.w - 1;
3363 relx = r->g.x + r->g.w - win->g.x - 1;
3365 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
3366 rely = win->g.h - 1;
3368 rely = r->g.y + r->g.h - win->g.y - 1;
3370 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
3372 XMaskEvent(display, MOUSEMASK | ExposureMask |
3373 SubstructureRedirectMask, &ev);
3375 case ConfigureRequest:
3378 handler[ev.type](&ev);
3381 /* do not allow resize outside of region */
3382 if ( ev.xmotion.x_root < r->g.x ||
3383 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3384 ev.xmotion.y_root < r->g.y ||
3385 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3388 if (ev.xmotion.x <= 1)
3390 if (ev.xmotion.y <= 1)
3392 win->g.w = ev.xmotion.x + 1;
3393 win->g.h = ev.xmotion.y + 1;
3395 /* not free, don't sync more than 120 times / second */
3396 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3397 time = ev.xmotion.time;
3398 XSync(display, False);
3399 resize_window(win, args->id);
3403 } while (ev.type != ButtonRelease);
3405 XSync(display, False);
3406 resize_window(win, args->id);
3408 store_float_geom(win,r);
3410 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
3412 XUngrabPointer(display, CurrentTime);
3415 drain_enter_notify();
3419 move_window(struct ws_win *win)
3423 struct swm_region *r;
3425 r = root_to_region(win->wa.root);
3426 bzero(&wc, sizeof wc);
3430 wc.border_width = border_width;
3432 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
3433 win->id, wc.x, wc.y, wc.width, wc.height);
3435 XConfigureWindow(display, win->id, mask, &wc);
3439 move(struct ws_win *win, union arg *args)
3443 struct swm_region *r = win->ws->r;
3445 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
3446 win->id, win->floating, win->transient);
3448 /* in max_stack mode should only move transients */
3449 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
3453 if (win->floating == 0 && !win->transient) {
3454 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3457 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3462 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3463 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3465 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3467 XMaskEvent(display, MOUSEMASK | ExposureMask |
3468 SubstructureRedirectMask, &ev);
3470 case ConfigureRequest:
3473 handler[ev.type](&ev);
3476 /* don't allow to move window origin out of region */
3477 if ( ev.xmotion.x_root < r->g.x ||
3478 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3479 ev.xmotion.y_root < r->g.y ||
3480 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3483 win->g.x = ev.xmotion.x_root - border_width;
3484 win->g.y = ev.xmotion.y_root - border_width;
3486 /* not free, don't sync more than 120 times / second */
3487 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3488 time = ev.xmotion.time;
3489 XSync(display, False);
3494 } while (ev.type != ButtonRelease);
3496 XSync(display, False);
3499 store_float_geom(win,r);
3500 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3501 XUngrabPointer(display, CurrentTime);
3504 drain_enter_notify();
3507 /* user/key callable function IDs */
3565 kf_dumpwins, /* MUST BE LAST */
3569 /* key definitions */
3571 dummykeyfunc(struct swm_region *r, union arg *args)
3576 legacyfunc(struct swm_region *r, union arg *args)
3581 char name[SWM_FUNCNAME_LEN];
3582 void (*func)(struct swm_region *r, union arg *);
3584 } keyfuncs[kf_invalid + 1] = {
3585 /* name function argument */
3586 { "cycle_layout", cycle_layout, {0} },
3587 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
3588 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
3589 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
3590 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
3591 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
3592 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
3593 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
3594 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
3595 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
3596 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
3597 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
3598 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
3599 { "spawn_term", spawnterm, {.argv = spawn_term} },
3600 { "spawn_menu", legacyfunc, {0} },
3601 { "quit", quit, {0} },
3602 { "restart", restart, {0} },
3603 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
3604 { "ws_1", switchws, {.id = 0} },
3605 { "ws_2", switchws, {.id = 1} },
3606 { "ws_3", switchws, {.id = 2} },
3607 { "ws_4", switchws, {.id = 3} },
3608 { "ws_5", switchws, {.id = 4} },
3609 { "ws_6", switchws, {.id = 5} },
3610 { "ws_7", switchws, {.id = 6} },
3611 { "ws_8", switchws, {.id = 7} },
3612 { "ws_9", switchws, {.id = 8} },
3613 { "ws_10", switchws, {.id = 9} },
3614 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
3615 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
3616 { "ws_prior", priorws, {0} },
3617 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
3618 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
3619 { "mvws_1", send_to_ws, {.id = 0} },
3620 { "mvws_2", send_to_ws, {.id = 1} },
3621 { "mvws_3", send_to_ws, {.id = 2} },
3622 { "mvws_4", send_to_ws, {.id = 3} },
3623 { "mvws_5", send_to_ws, {.id = 4} },
3624 { "mvws_6", send_to_ws, {.id = 5} },
3625 { "mvws_7", send_to_ws, {.id = 6} },
3626 { "mvws_8", send_to_ws, {.id = 7} },
3627 { "mvws_9", send_to_ws, {.id = 8} },
3628 { "mvws_10", send_to_ws, {.id = 9} },
3629 { "bar_toggle", bar_toggle, {0} },
3630 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
3631 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
3632 { "screenshot_all", legacyfunc, {0} },
3633 { "screenshot_wind", legacyfunc, {0} },
3634 { "float_toggle", floating_toggle,{0} },
3635 { "version", version, {0} },
3636 { "spawn_lock", legacyfunc, {0} },
3637 { "spawn_initscr", legacyfunc, {0} },
3638 { "spawn_custom", dummykeyfunc, {0} },
3639 { "iconify", iconify, {0} },
3640 { "uniconify", uniconify, {0} },
3641 { "raise_toggle", raise_toggle, {0} },
3642 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
3643 { "invalid key func", NULL, {0} },
3648 enum keyfuncid funcid;
3651 int keys_size = 0, keys_length = 0;
3652 struct key *keys = NULL;
3655 enum { client_click, root_click };
3657 unsigned int action;
3659 unsigned int button;
3660 void (*func)(struct ws_win *, union arg *);
3663 /* action key mouse button func args */
3664 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
3665 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
3666 { client_click, MODKEY, Button1, move, {0} },
3670 update_modkey(unsigned int mod)
3675 for (i = 0; i < keys_length; i++)
3676 if (keys[i].mod & ShiftMask)
3677 keys[i].mod = mod | ShiftMask;
3681 for (i = 0; i < LENGTH(buttons); i++)
3682 if (buttons[i].mask & ShiftMask)
3683 buttons[i].mask = mod | ShiftMask;
3685 buttons[i].mask = mod;
3695 int spawns_size = 0, spawns_length = 0;
3696 struct spawn_prog *spawns = NULL;
3699 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
3702 struct spawn_prog *prog = NULL;
3704 char *ap, **real_args;
3706 DNPRINTF(SWM_D_SPAWN, "spawn_expand %s\n", spawn_name);
3709 for (i = 0; i < spawns_length; i++) {
3710 if (!strcasecmp(spawn_name, spawns[i].name))
3714 fprintf(stderr, "spawn_custom: program %s not found\n",
3719 /* make room for expanded args */
3720 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
3721 err(1, "spawn_custom: calloc real_args");
3723 /* expand spawn_args into real_args */
3724 for (i = 0; i < prog->argc; i++) {
3726 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
3727 if (!strcasecmp(ap, "$bar_border")) {
3729 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
3731 err(1, "spawn_custom border color");
3732 } else if (!strcasecmp(ap, "$bar_color")) {
3734 strdup(r->s->c[SWM_S_COLOR_BAR].name))
3736 err(1, "spawn_custom bar color");
3737 } else if (!strcasecmp(ap, "$bar_font")) {
3738 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
3740 err(1, "spawn_custom bar fonts");
3741 } else if (!strcasecmp(ap, "$bar_font_color")) {
3743 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
3745 err(1, "spawn_custom color font");
3746 } else if (!strcasecmp(ap, "$color_focus")) {
3748 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
3750 err(1, "spawn_custom color focus");
3751 } else if (!strcasecmp(ap, "$color_unfocus")) {
3753 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
3755 err(1, "spawn_custom color unfocus");
3757 /* no match --> copy as is */
3758 if ((real_args[i] = strdup(ap)) == NULL)
3759 err(1, "spawn_custom strdup(ap)");
3761 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
3766 if ((swm_debug & SWM_D_SPAWN) != 0) {
3767 fprintf(stderr, "spawn_custom: result = ");
3768 for (i = 0; i < prog->argc; i++)
3769 fprintf(stderr, "\"%s\" ", real_args[i]);
3770 fprintf(stderr, "\n");
3773 *ret_args = real_args;
3774 return (prog->argc);
3778 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
3784 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3788 spawn(r->ws->idx, &a, 1);
3790 for (i = 0; i < spawn_argc; i++)
3796 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
3802 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3806 if (pipe(select_list_pipe) == -1)
3807 err(1, "pipe error");
3808 if (pipe(select_resp_pipe) == -1)
3809 err(1, "pipe error");
3811 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
3812 err(1, "could not disable SIGPIPE");
3813 switch (*pid = fork()) {
3815 err(1, "cannot fork");
3818 if (dup2(select_list_pipe[0], 0) == -1)
3820 if (dup2(select_resp_pipe[1], 1) == -1)
3822 close(select_list_pipe[1]);
3823 close(select_resp_pipe[0]);
3824 spawn(r->ws->idx, &a, 0);
3826 default: /* parent */
3827 close(select_list_pipe[0]);
3828 close(select_resp_pipe[1]);
3832 for (i = 0; i < spawn_argc; i++)
3838 setspawn(struct spawn_prog *prog)
3842 if (prog == NULL || prog->name == NULL)
3846 for (i = 0; i < spawns_length; i++) {
3847 if (!strcmp(spawns[i].name, prog->name)) {
3849 if (prog->argv == NULL) {
3851 DNPRINTF(SWM_D_SPAWN,
3852 "setspawn: delete #%d %s\n",
3854 free(spawns[i].name);
3855 for (j = 0; j < spawns[i].argc; j++)
3856 free(spawns[i].argv[j]);
3857 free(spawns[i].argv);
3858 j = spawns_length - 1;
3860 spawns[i] = spawns[j];
3865 DNPRINTF(SWM_D_SPAWN,
3866 "setspawn: replace #%d %s\n",
3868 free(spawns[i].name);
3869 for (j = 0; j < spawns[i].argc; j++)
3870 free(spawns[i].argv[j]);
3871 free(spawns[i].argv);
3874 /* found case handled */
3880 if (prog->argv == NULL) {
3882 "error: setspawn: cannot find program %s", prog->name);
3887 /* not found: add */
3888 if (spawns_size == 0 || spawns == NULL) {
3890 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
3891 spawns = malloc((size_t)spawns_size *
3892 sizeof(struct spawn_prog));
3893 if (spawns == NULL) {
3894 fprintf(stderr, "setspawn: malloc failed\n");
3898 } else if (spawns_length == spawns_size) {
3900 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
3901 spawns = realloc(spawns, (size_t)spawns_size *
3902 sizeof(struct spawn_prog));
3903 if (spawns == NULL) {
3904 fprintf(stderr, "setspawn: realloc failed\n");
3910 if (spawns_length < spawns_size) {
3911 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
3912 spawns_length, prog->name);
3913 i = spawns_length++;
3916 fprintf(stderr, "spawns array problem?\n");
3917 if (spawns == NULL) {
3918 fprintf(stderr, "spawns array is NULL!\n");
3926 setconfspawn(char *selector, char *value, int flags)
3928 struct spawn_prog *prog;
3929 char *vp, *cp, *word;
3931 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
3932 if ((prog = calloc(1, sizeof *prog)) == NULL)
3933 err(1, "setconfspawn: calloc prog");
3934 prog->name = strdup(selector);
3935 if (prog->name == NULL)
3936 err(1, "setconfspawn prog->name");
3937 if ((cp = vp = strdup(value)) == NULL)
3938 err(1, "setconfspawn: strdup(value) ");
3939 while ((word = strsep(&cp, " \t")) != NULL) {
3940 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
3942 cp += (long)strspn(cp, " \t");
3943 if (strlen(word) > 0) {
3945 if ((prog->argv = realloc(prog->argv,
3946 prog->argc * sizeof(char *))) == NULL)
3947 err(1, "setconfspawn: realloc");
3948 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
3949 err(1, "setconfspawn: strdup");
3956 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
3963 setconfspawn("term", "xterm", 0);
3964 setconfspawn("screenshot_all", "screenshot.sh full", 0);
3965 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
3966 setconfspawn("lock", "xlock", 0);
3967 setconfspawn("initscr", "initscreen.sh", 0);
3968 setconfspawn("menu", "dmenu_run"
3971 " -nf $bar_font_color"
3973 " -sf $bar_color", 0);
3974 setconfspawn("uniconify", "dmenu"
3978 " -nf $bar_font_color"
3980 " -sf $bar_color", 0);
3984 #define SWM_MODNAME_SIZE 32
3985 #define SWM_KEY_WS "\n+ \t"
3987 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
3991 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
3992 if (mod == NULL || ks == NULL) {
3993 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
3996 if (keystr == NULL || strlen(keystr) == 0) {
3997 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
4003 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
4004 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
4006 cp += (long)strspn(cp, SWM_KEY_WS);
4007 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
4009 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
4011 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
4013 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
4015 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
4017 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
4019 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
4020 *mod |= ControlMask;
4022 *ks = XStringToKeysym(name);
4023 XConvertCase(*ks, ks, &uks);
4024 if (ks == NoSymbol) {
4026 "parsekeys: invalid key %s\n",
4032 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
4037 strdupsafe(char *str)
4042 return (strdup(str));
4046 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
4050 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
4051 keyfuncs[kfid].name, spawn_name);
4053 for (i = 0; i < keys_length; i++) {
4054 if (keys[i].mod == mod && keys[i].keysym == ks) {
4055 if (kfid == kf_invalid) {
4058 "setkeybinding: delete #%d %s\n",
4059 i, keyfuncs[keys[i].funcid].name);
4060 free(keys[i].spawn_name);
4061 j = keys_length - 1;
4065 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4068 /* found: replace */
4070 "setkeybinding: replace #%d %s %s\n",
4071 i, keyfuncs[keys[i].funcid].name,
4073 free(keys[i].spawn_name);
4075 keys[i].keysym = ks;
4076 keys[i].funcid = kfid;
4077 keys[i].spawn_name = strdupsafe(spawn_name);
4078 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4083 if (kfid == kf_invalid) {
4085 "error: setkeybinding: cannot find mod/key combination");
4086 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4089 /* not found: add */
4090 if (keys_size == 0 || keys == NULL) {
4092 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
4093 keys = malloc((size_t)keys_size * sizeof(struct key));
4095 fprintf(stderr, "malloc failed\n");
4099 } else if (keys_length == keys_size) {
4101 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
4102 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
4104 fprintf(stderr, "realloc failed\n");
4109 if (keys_length < keys_size) {
4111 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
4112 j, keyfuncs[kfid].name, spawn_name);
4114 keys[j].keysym = ks;
4115 keys[j].funcid = kfid;
4116 keys[j].spawn_name = strdupsafe(spawn_name);
4118 fprintf(stderr, "keys array problem?\n");
4120 fprintf(stderr, "keys array problem\n");
4124 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4128 setconfbinding(char *selector, char *value, int flags)
4130 enum keyfuncid kfid;
4134 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
4135 if (selector == NULL) {
4136 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
4137 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4139 setkeybinding(mod, ks, kfid, NULL);
4144 /* search by key function name */
4145 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
4146 if (strncasecmp(selector, keyfuncs[kfid].name,
4147 SWM_FUNCNAME_LEN) == 0) {
4148 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4150 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4151 setkeybinding(mod, ks, kfid, NULL);
4157 /* search by custom spawn name */
4158 for (i = 0; i < spawns_length; i++) {
4159 if (strcasecmp(selector, spawns[i].name) == 0) {
4160 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4162 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4163 setkeybinding(mod, ks, kf_spawn_custom,
4170 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
4177 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
4178 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
4179 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
4180 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
4181 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
4182 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
4183 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
4184 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
4185 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
4186 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
4187 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
4188 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
4189 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
4190 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
4191 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
4192 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
4193 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
4194 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
4195 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
4196 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
4197 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
4198 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
4199 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
4200 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
4201 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
4202 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
4203 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
4204 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
4205 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
4206 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
4207 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
4208 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
4209 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
4210 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
4211 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
4212 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
4213 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
4214 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
4215 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
4216 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
4217 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
4218 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
4219 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
4220 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
4221 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
4222 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
4223 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
4224 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
4225 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
4226 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
4227 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
4228 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
4229 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
4230 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
4231 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
4232 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
4233 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
4235 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
4240 updatenumlockmask(void)
4243 XModifierKeymap *modmap;
4245 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
4247 modmap = XGetModifierMapping(display);
4248 for (i = 0; i < 8; i++)
4249 for (j = 0; j < modmap->max_keypermod; j++)
4250 if (modmap->modifiermap[i * modmap->max_keypermod + j]
4251 == XKeysymToKeycode(display, XK_Num_Lock))
4252 numlockmask = (1 << i);
4254 XFreeModifiermap(modmap);
4260 unsigned int i, j, k;
4262 unsigned int modifiers[] =
4263 { 0, LockMask, numlockmask, numlockmask | LockMask };
4265 DNPRINTF(SWM_D_MISC, "grabkeys\n");
4266 updatenumlockmask();
4268 for (k = 0; k < ScreenCount(display); k++) {
4269 if (TAILQ_EMPTY(&screens[k].rl))
4271 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
4272 for (i = 0; i < keys_length; i++) {
4273 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
4274 for (j = 0; j < LENGTH(modifiers); j++)
4275 XGrabKey(display, code,
4276 keys[i].mod | modifiers[j],
4277 screens[k].root, True,
4278 GrabModeAsync, GrabModeAsync);
4284 grabbuttons(struct ws_win *win, int focused)
4287 unsigned int modifiers[] =
4288 { 0, LockMask, numlockmask, numlockmask|LockMask };
4290 updatenumlockmask();
4291 XUngrabButton(display, AnyButton, AnyModifier, win->id);
4293 for (i = 0; i < LENGTH(buttons); i++)
4294 if (buttons[i].action == client_click)
4295 for (j = 0; j < LENGTH(modifiers); j++)
4296 XGrabButton(display, buttons[i].button,
4297 buttons[i].mask | modifiers[j],
4298 win->id, False, BUTTONMASK,
4299 GrabModeAsync, GrabModeSync, None,
4302 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
4303 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
4306 const char *quirkname[] = {
4307 "NONE", /* config string for "no value" */
4316 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
4317 #define SWM_Q_WS "\n|+ \t"
4319 parsequirks(char *qstr, unsigned long *quirk)
4329 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
4331 cp += (long)strspn(cp, SWM_Q_WS);
4332 for (i = 0; i < LENGTH(quirkname); i++) {
4333 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
4334 DNPRINTF(SWM_D_QUIRK,
4335 "parsequirks: %s\n", name);
4340 *quirk |= 1 << (i-1);
4344 if (i >= LENGTH(quirkname)) {
4345 DNPRINTF(SWM_D_QUIRK,
4346 "parsequirks: invalid quirk [%s]\n", name);
4354 setquirk(const char *class, const char *name, const int quirk)
4359 for (i = 0; i < quirks_length; i++) {
4360 if (!strcmp(quirks[i].class, class) &&
4361 !strcmp(quirks[i].name, name)) {
4364 DNPRINTF(SWM_D_QUIRK,
4365 "setquirk: delete #%d %s:%s\n",
4366 i, quirks[i].class, quirks[i].name);
4367 free(quirks[i].class);
4368 free(quirks[i].name);
4369 j = quirks_length - 1;
4371 quirks[i] = quirks[j];
4375 /* found: replace */
4376 DNPRINTF(SWM_D_QUIRK,
4377 "setquirk: replace #%d %s:%s\n",
4378 i, quirks[i].class, quirks[i].name);
4379 free(quirks[i].class);
4380 free(quirks[i].name);
4381 quirks[i].class = strdup(class);
4382 quirks[i].name = strdup(name);
4383 quirks[i].quirk = quirk;
4390 "error: setquirk: cannot find class/name combination");
4393 /* not found: add */
4394 if (quirks_size == 0 || quirks == NULL) {
4396 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
4397 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
4398 if (quirks == NULL) {
4399 fprintf(stderr, "setquirk: malloc failed\n");
4403 } else if (quirks_length == quirks_size) {
4405 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
4406 quirks = realloc(quirks,
4407 (size_t)quirks_size * sizeof(struct quirk));
4408 if (quirks == NULL) {
4409 fprintf(stderr, "setquirk: realloc failed\n");
4414 if (quirks_length < quirks_size) {
4415 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
4416 j = quirks_length++;
4417 quirks[j].class = strdup(class);
4418 quirks[j].name = strdup(name);
4419 quirks[j].quirk = quirk;
4421 fprintf(stderr, "quirks array problem?\n");
4422 if (quirks == NULL) {
4423 fprintf(stderr, "quirks array problem!\n");
4430 setconfquirk(char *selector, char *value, int flags)
4432 char *cp, *class, *name;
4434 unsigned long quirks;
4435 if (selector == NULL)
4437 if ((cp = strchr(selector, ':')) == NULL)
4442 if ((retval = parsequirks(value, &quirks)) == 0)
4443 setquirk(class, name, quirks);
4450 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
4451 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
4452 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
4453 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
4454 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4455 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
4456 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4457 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4458 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4459 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4460 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
4461 setquirk("pcb", "pcb", SWM_Q_FLOAT);
4462 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
4465 /* conf file stuff */
4466 #define SWM_CONF_FILE "scrotwm.conf"
4468 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH,
4469 SWM_S_STACK_ENABLED, SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT,
4470 SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED,
4471 SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
4472 SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED, SWM_S_URGENT_ENABLED,
4473 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH,
4474 SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM,
4475 SWM_S_SS_APP, SWM_S_DIALOG_RATIO, SWM_S_BAR_AT_BOTTOM,
4476 SWM_S_VERBOSE_LAYOUT
4480 setconfvalue(char *selector, char *value, int flags)
4484 case SWM_S_BAR_DELAY:
4485 bar_delay = atoi(value);
4487 case SWM_S_BAR_ENABLED:
4488 bar_enabled = atoi(value);
4490 case SWM_S_BAR_BORDER_WIDTH:
4491 bar_border_width = atoi(value);
4493 case SWM_S_BAR_AT_BOTTOM:
4494 bar_at_bottom = atoi(value);
4496 case SWM_S_STACK_ENABLED:
4497 stack_enabled = atoi(value);
4499 case SWM_S_CLOCK_ENABLED:
4500 clock_enabled = atoi(value);
4502 case SWM_S_CLOCK_FORMAT:
4503 #ifndef SWM_DENY_CLOCK_FORMAT
4505 if ((clock_format = strdup(value)) == NULL)
4506 err(1, "setconfvalue: clock_format");
4509 case SWM_S_CYCLE_EMPTY:
4510 cycle_empty = atoi(value);
4512 case SWM_S_CYCLE_VISIBLE:
4513 cycle_visible = atoi(value);
4515 case SWM_S_SS_ENABLED:
4516 ss_enabled = atoi(value);
4518 case SWM_S_TERM_WIDTH:
4519 term_width = atoi(value);
4521 case SWM_S_TITLE_CLASS_ENABLED:
4522 title_class_enabled = atoi(value);
4524 case SWM_S_WINDOW_NAME_ENABLED:
4525 window_name_enabled = atoi(value);
4527 case SWM_S_TITLE_NAME_ENABLED:
4528 title_name_enabled = atoi(value);
4530 case SWM_S_URGENT_ENABLED:
4531 urgent_enabled = atoi(value);
4533 case SWM_S_FOCUS_MODE:
4534 if (!strcmp(value, "default"))
4535 focus_mode = SWM_FOCUS_DEFAULT;
4536 else if (!strcmp(value, "follow_cursor"))
4537 focus_mode = SWM_FOCUS_FOLLOW;
4538 else if (!strcmp(value, "synergy"))
4539 focus_mode = SWM_FOCUS_SYNERGY;
4541 err(1, "focus_mode");
4543 case SWM_S_DISABLE_BORDER:
4544 disable_border = atoi(value);
4546 case SWM_S_BORDER_WIDTH:
4547 border_width = atoi(value);
4549 case SWM_S_BAR_FONT:
4551 if ((bar_fonts[0] = strdup(value)) == NULL)
4552 err(1, "setconfvalue: bar_font");
4554 case SWM_S_BAR_ACTION:
4556 if ((bar_argv[0] = strdup(value)) == NULL)
4557 err(1, "setconfvalue: bar_action");
4559 case SWM_S_SPAWN_TERM:
4560 free(spawn_term[0]);
4561 if ((spawn_term[0] = strdup(value)) == NULL)
4562 err(1, "setconfvalue: spawn_term");
4566 case SWM_S_DIALOG_RATIO:
4567 dialog_ratio = atof(value);
4568 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
4571 case SWM_S_VERBOSE_LAYOUT:
4572 verbose_layout = atoi(value);
4573 for (i = 0; layouts[i].l_stack != NULL; i++) {
4575 layouts[i].l_string = fancy_stacker;
4577 layouts[i].l_string = plain_stacker;
4587 setconfmodkey(char *selector, char *value, int flags)
4589 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
4590 update_modkey(Mod1Mask);
4591 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
4592 update_modkey(Mod2Mask);
4593 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
4594 update_modkey(Mod3Mask);
4595 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
4596 update_modkey(Mod4Mask);
4603 setconfcolor(char *selector, char *value, int flags)
4605 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
4610 setconfregion(char *selector, char *value, int flags)
4612 custom_region(value);
4617 setautorun(char *selector, char *value, int flags)
4627 if (getenv("SWM_STARTED"))
4631 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
4632 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'\n");
4634 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
4635 errx(1, "autorun: invalid workspace %d\n", ws_id + 1);
4638 * This is a little intricate
4640 * If the pid already exists we simply reuse it because it means it was
4641 * used before AND not claimed by manage_window. We get away with
4642 * altering it in the parent after INSERT because this can not be a race
4645 while ((ap = strsep(&sp, " \t")) != NULL) {
4648 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
4650 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
4651 err(1, "setautorun: realloc");
4652 a.argv[argc - 1] = ap;
4655 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
4656 err(1, "setautorun: realloc");
4657 a.argv[argc] = NULL;
4659 if ((pid = fork()) == 0) {
4660 spawn(ws_id, &a, 1);
4669 p = calloc(1, sizeof *p);
4672 TAILQ_INSERT_TAIL(&pidlist, p, entry);
4682 setlayout(char *selector, char *value, int flags)
4684 int ws_id, st, i, x, mg, ma, si, raise;
4686 struct workspace *ws;
4688 if (getenv("SWM_STARTED"))
4692 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
4693 &ws_id, &mg, &ma, &si, &raise, s) != 6)
4694 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
4695 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
4698 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
4699 errx(1, "layout: invalid workspace %d\n", ws_id + 1);
4701 if (!strcasecmp(s, "vertical"))
4703 else if (!strcasecmp(s, "horizontal"))
4705 else if (!strcasecmp(s, "fullscreen"))
4708 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
4709 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
4712 for (i = 0; i < ScreenCount(display); i++) {
4713 ws = (struct workspace *)&screens[i].ws;
4714 ws[ws_id].cur_layout = &layouts[st];
4716 ws[ws_id].always_raise = raise;
4717 if (st == SWM_MAX_STACK)
4721 for (x = 0; x < abs(mg); x++) {
4722 ws[ws_id].cur_layout->l_config(&ws[ws_id],
4723 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
4724 SWM_ARG_ID_MASTERSHRINK);
4728 for (x = 0; x < abs(ma); x++) {
4729 ws[ws_id].cur_layout->l_config(&ws[ws_id],
4730 ma >= 0 ? SWM_ARG_ID_MASTERADD :
4731 SWM_ARG_ID_MASTERDEL);
4735 for (x = 0; x < abs(si); x++) {
4736 ws[ws_id].cur_layout->l_config(&ws[ws_id],
4737 si >= 0 ? SWM_ARG_ID_STACKINC :
4738 SWM_ARG_ID_STACKDEC);
4746 /* config options */
4747 struct config_option {
4749 int (*func)(char*, char*, int);
4752 struct config_option configopt[] = {
4753 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
4754 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
4755 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
4756 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
4757 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
4758 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
4759 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
4760 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
4761 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
4762 { "bind", setconfbinding, 0 },
4763 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
4764 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
4765 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
4766 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
4767 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
4768 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
4769 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
4770 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
4771 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
4772 { "modkey", setconfmodkey, 0 },
4773 { "program", setconfspawn, 0 },
4774 { "quirk", setconfquirk, 0 },
4775 { "region", setconfregion, 0 },
4776 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
4777 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
4778 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
4779 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
4780 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
4781 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
4782 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
4783 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
4784 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
4785 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
4786 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
4787 { "autorun", setautorun, 0 },
4788 { "layout", setlayout, 0 },
4793 conf_load(char *filename)
4796 char *line, *cp, *optsub, *optval;
4797 size_t linelen, lineno = 0;
4798 int wordlen, i, optind;
4799 struct config_option *opt;
4801 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
4803 if (filename == NULL) {
4804 fprintf(stderr, "conf_load: no filename\n");
4807 if ((config = fopen(filename, "r")) == NULL) {
4808 warn("conf_load: fopen");
4812 while (!feof(config)) {
4813 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
4816 err(1, "%s", filename);
4821 cp += strspn(cp, " \t\n"); /* eat whitespace */
4822 if (cp[0] == '\0') {
4827 /* get config option */
4828 wordlen = strcspn(cp, "=[ \t\n");
4830 warnx("%s: line %zd: no option found",
4835 for (i = 0; i < LENGTH(configopt); i++) {
4836 opt = &configopt[i];
4837 if (!strncasecmp(cp, opt->optname, wordlen) &&
4838 strlen(opt->optname) == wordlen) {
4844 warnx("%s: line %zd: unknown option %.*s",
4845 filename, lineno, wordlen, cp);
4849 cp += strspn(cp, " \t\n"); /* eat whitespace */
4850 /* get [selector] if any */
4854 wordlen = strcspn(cp, "]");
4857 warnx("%s: line %zd: syntax error",
4861 asprintf(&optsub, "%.*s", wordlen, cp);
4864 cp += strspn(cp, "] \t\n"); /* eat trailing */
4866 cp += strspn(cp, "= \t\n"); /* eat trailing */
4868 optval = strdup(cp);
4869 /* call function to deal with it all */
4870 if (configopt[optind].func(optsub, optval,
4871 configopt[optind].funcflags) != 0) {
4872 fprintf(stderr, "%s line %zd: %s\n",
4873 filename, lineno, line);
4874 errx(1, "%s: line %zd: invalid data for %s",
4875 filename, lineno, configopt[optind].optname);
4883 DNPRINTF(SWM_D_CONF, "conf_load end\n");
4889 set_child_transient(struct ws_win *win, Window *trans)
4891 struct ws_win *parent, *w;
4892 XWMHints *wmh = NULL;
4893 struct swm_region *r;
4894 struct workspace *ws;
4896 parent = find_window(win->transient);
4898 parent->child_trans = win;
4900 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
4901 " for %lu trans %lu\n", win->id, win->transient);
4903 if (win->hints == NULL) {
4904 fprintf(stderr, "no hints for %lu\n", win->id);
4908 r = root_to_region(win->wa.root);
4910 /* parent doen't exist in our window list */
4911 TAILQ_FOREACH(w, &ws->winlist, entry) {
4915 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
4916 fprintf(stderr, "can't get hints for %lu\n",
4921 if (win->hints->window_group != wmh->window_group)
4924 w->child_trans = win;
4925 win->transient = w->id;
4927 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
4928 "transient to %lu\n", win->transient);
4938 window_get_pid(Window win)
4940 Atom actual_type_return;
4941 int actual_format_return = 0;
4942 unsigned long nitems_return = 0;
4943 unsigned long bytes_after_return = 0;
4947 unsigned char *prop = NULL;
4949 if (XGetWindowProperty(display, win,
4950 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
4951 XA_CARDINAL, &actual_type_return, &actual_format_return,
4952 &nitems_return, &bytes_after_return,
4953 (unsigned char**)(void*)&pid) != Success)
4955 if (actual_type_return != XA_CARDINAL)
4966 if (XGetWindowProperty(display, win,
4967 XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
4968 XA_STRING, &actual_type_return, &actual_format_return,
4969 &nitems_return, &bytes_after_return, &prop) != Success)
4971 if (actual_type_return != XA_STRING)
4976 ret = strtonum(prop, 0, UINT_MAX, &errstr);
4977 /* ignore error because strtonum returns 0 anyway */
4984 manage_window(Window id)
4987 struct workspace *ws;
4988 struct ws_win *win, *ww;
4989 int format, i, ws_idx, n, border_me = 0;
4990 unsigned long nitems, bytes;
4991 Atom ws_idx_atom = 0, type;
4992 Atom *prot = NULL, *pp;
4993 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
4994 struct swm_region *r;
5000 if ((win = find_window(id)) != NULL)
5001 return (win); /* already being managed */
5003 /* see if we are on the unmanaged list */
5004 if ((win = find_unmanaged_window(id)) != NULL) {
5005 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
5007 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5008 if (win->transient) {
5009 set_child_transient(win, &trans);
5010 } if (trans && (ww = find_window(trans)))
5011 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
5013 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
5014 ewmh_update_actions(win);
5018 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
5019 errx(1, "calloc: failed to allocate memory for new window");
5023 /* see if we need to override the workspace */
5024 p = find_pid(window_get_pid(id));
5026 /* Get all the window data in one shot */
5027 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
5029 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
5030 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
5032 XGetWindowAttributes(display, id, &win->wa);
5033 XGetWMNormalHints(display, id, &win->sh, &mask);
5034 win->hints = XGetWMHints(display, id);
5035 XGetTransientForHint(display, id, &trans);
5037 win->transient = trans;
5038 set_child_transient(win, &trans);
5039 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
5040 win->id, win->transient);
5043 /* get supported protocols */
5044 if (XGetWMProtocols(display, id, &prot, &n)) {
5045 for (i = 0, pp = prot; i < n; i++, pp++) {
5046 if (*pp == takefocus)
5047 win->take_focus = 1;
5049 win->can_delete = 1;
5055 win->iconic = get_iconic(win);
5058 * Figure out where to put the window. If it was previously assigned to
5059 * a workspace (either by spawn() or manually moving), and isn't
5060 * transient, * put it in the same workspace
5062 r = root_to_region(win->wa.root);
5064 ws = &r->s->ws[p->ws];
5065 TAILQ_REMOVE(&pidlist, p, entry);
5068 } else if (prop && win->transient == 0) {
5069 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
5070 ws_idx = strtonum(prop, 0, 9, &errstr);
5072 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
5075 ws = &r->s->ws[ws_idx];
5078 /* this should launch transients in the same ws as parent */
5080 if ((ww = find_window(trans)) != NULL)
5087 "fix this bug mcbride\n");
5092 /* set up the window layout */
5095 win->s = r->s; /* this never changes */
5096 if (trans && (ww = find_window(trans)))
5097 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
5099 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
5101 win->g.w = win->wa.width;
5102 win->g.h = win->wa.height;
5103 win->g.x = win->wa.x;
5104 win->g.y = win->wa.y;
5105 win->g_floatvalid = 0;
5106 win->floatmaxed = 0;
5107 win->ewmh_flags = 0;
5109 /* Set window properties so we can remember this after reincarnation */
5110 if (ws_idx_atom && prop == NULL &&
5111 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
5112 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
5114 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
5115 PropModeReplace, ws_idx_str, SWM_PROPLEN);
5120 ewmh_autoquirk(win);
5122 if (XGetClassHint(display, win->id, &win->ch)) {
5123 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
5124 win->ch.res_class, win->ch.res_name);
5126 /* java is retarded so treat it special */
5127 if (strstr(win->ch.res_name, "sun-awt")) {
5132 for (i = 0; i < quirks_length; i++){
5133 if (!strcmp(win->ch.res_class, quirks[i].class) &&
5134 !strcmp(win->ch.res_name, quirks[i].name)) {
5135 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
5136 win->ch.res_class, win->ch.res_name);
5137 if (quirks[i].quirk & SWM_Q_FLOAT) {
5141 win->quirks = quirks[i].quirk;
5146 /* alter window position if quirky */
5147 if (win->quirks & SWM_Q_ANYWHERE) {
5148 win->manual = 1; /* don't center the quirky windows */
5149 bzero(&wc, sizeof wc);
5151 if (bar_enabled && win->g.y < bar_height) {
5152 win->g.y = wc.y = bar_height;
5155 if (win->g.w + win->g.x > WIDTH(r)) {
5156 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
5162 /* Reset font sizes (the bruteforce way; no default keybinding). */
5163 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
5164 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5165 fake_keypress(win, XK_KP_Subtract, ShiftMask);
5166 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5167 fake_keypress(win, XK_KP_Add, ShiftMask);
5170 ewmh_get_win_state(win);
5171 ewmh_update_actions(win);
5172 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
5176 bzero(&wc, sizeof wc);
5177 wc.border_width = border_width;
5178 mask = CWBorderWidth;
5179 XConfigureWindow(display, win->id, mask, &wc);
5182 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
5183 PropertyChangeMask | StructureNotifyMask);
5185 /* floaters need to be mapped if they are in the current workspace */
5186 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
5187 XMapRaised(display, win->id);
5193 free_window(struct ws_win *win)
5195 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
5200 /* needed for restart wm */
5201 set_win_state(win, WithdrawnState);
5203 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5205 if (win->ch.res_class)
5206 XFree(win->ch.res_class);
5207 if (win->ch.res_name)
5208 XFree(win->ch.res_name);
5213 memset(win, 0xff, sizeof *win); /* XXX kill later */
5219 unmanage_window(struct ws_win *win)
5221 struct ws_win *parent;
5226 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
5228 if (win->transient) {
5229 parent = find_window(win->transient);
5231 parent->child_trans = NULL;
5234 /* focus on root just in case */
5235 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
5239 TAILQ_REMOVE(&win->ws->winlist, win, entry);
5240 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
5246 focus_magic(struct ws_win *win)
5248 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu\n", WINID(win));
5253 if (win->child_trans) {
5254 /* win = parent & has a transient so focus on that */
5256 focus_win(win->child_trans);
5257 if (win->child_trans->take_focus)
5258 client_msg(win, takefocus);
5260 /* make sure transient hasn't dissapeared */
5261 if (validate_win(win->child_trans) == 0) {
5262 focus_win(win->child_trans);
5263 if (win->child_trans->take_focus)
5264 client_msg(win->child_trans, takefocus);
5266 win->child_trans = NULL;
5268 if (win->take_focus)
5269 client_msg(win, takefocus);
5275 if (win->take_focus)
5276 client_msg(win, takefocus);
5283 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
5291 XKeyEvent *ev = &e->xkey;
5293 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
5295 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
5296 for (i = 0; i < keys_length; i++)
5297 if (keysym == keys[i].keysym
5298 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
5299 && keyfuncs[keys[i].funcid].func) {
5300 if (keys[i].funcid == kf_spawn_custom)
5302 root_to_region(ev->root),
5303 &(keyfuncs[keys[i].funcid].args),
5307 keyfuncs[keys[i].funcid].func(
5308 root_to_region(ev->root),
5309 &(keyfuncs[keys[i].funcid].args)
5315 buttonpress(XEvent *e)
5319 XButtonPressedEvent *ev = &e->xbutton;
5321 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
5323 action = root_click;
5324 if ((win = find_window(ev->window)) == NULL)
5328 action = client_click;
5330 for (i = 0; i < LENGTH(buttons); i++)
5331 if (action == buttons[i].action && buttons[i].func &&
5332 buttons[i].button == ev->button &&
5333 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
5334 buttons[i].func(win, &buttons[i].args);
5338 configurerequest(XEvent *e)
5340 XConfigureRequestEvent *ev = &e->xconfigurerequest;
5345 if ((win = find_window(ev->window)) == NULL)
5346 if ((win = find_unmanaged_window(ev->window)) == NULL)
5350 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
5352 bzero(&wc, sizeof wc);
5355 wc.width = ev->width;
5356 wc.height = ev->height;
5357 wc.border_width = ev->border_width;
5358 wc.sibling = ev->above;
5359 wc.stack_mode = ev->detail;
5360 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
5362 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
5364 config_win(win, ev);
5369 configurenotify(XEvent *e)
5374 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
5375 e->xconfigure.window);
5377 win = find_window(e->xconfigure.window);
5379 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5383 if (focus_mode == SWM_FOCUS_DEFAULT)
5384 drain_enter_notify();
5389 destroynotify(XEvent *e)
5392 XDestroyWindowEvent *ev = &e->xdestroywindow;
5394 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
5396 if ((win = find_window(ev->window)) == NULL) {
5397 if ((win = find_unmanaged_window(ev->window)) == NULL)
5403 /* make sure we focus on something */
5406 unmanage_window(win);
5408 if (focus_mode == SWM_FOCUS_DEFAULT)
5409 drain_enter_notify();
5414 enternotify(XEvent *e)
5416 XCrossingEvent *ev = &e->xcrossing;
5421 Window focus_return;
5422 int revert_to_return;
5424 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
5425 "%lu subwindow %lu same_screen %d focus %d state %d\n",
5426 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
5427 ev->same_screen, ev->focus, ev->state);
5429 switch (focus_mode) {
5430 case SWM_FOCUS_DEFAULT:
5432 case SWM_FOCUS_FOLLOW:
5434 case SWM_FOCUS_SYNERGY:
5437 * all these checks need to be in this order because the
5438 * XCheckTypedWindowEvent relies on weeding out the previous events
5440 * making this code an option would enable a follow mouse for focus
5445 * state is set when we are switching workspaces and focus is set when
5446 * the window or a subwindow already has focus (occurs during restart).
5448 * Only honor the focus flag if last_focus_event is not FocusOut,
5449 * this allows scrotwm to continue to control focus when another
5450 * program is also playing with it.
5452 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
5453 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
5458 * happens when a window is created or destroyed and the border
5459 * crosses the mouse pointer and when switching ws
5461 * we need the subwindow test to see if we came from root in order
5462 * to give focus to floaters
5464 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
5465 ev->subwindow == 0) {
5466 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
5470 /* this window already has focus */
5471 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
5472 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
5476 /* this window is being deleted or moved to another ws */
5477 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
5479 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
5480 XPutBackEvent(display, &cne);
5484 if ((win = find_window(ev->window)) == NULL) {
5485 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5490 * In fullstack kill all enters unless they come from a different ws
5491 * (i.e. another region) or focus has been grabbed externally.
5493 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
5494 last_focus_event != FocusOut) {
5495 XGetInputFocus(display, &focus_return, &revert_to_return);
5496 if ((w = find_window(focus_return)) == NULL ||
5498 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
5506 if ((win = find_window(ev->window)) == NULL) {
5507 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5512 * if we have more enternotifies let them handle it in due time
5514 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
5515 DNPRINTF(SWM_D_EVENT,
5516 "ignoring enternotify: got more enternotify\n");
5517 XPutBackEvent(display, &cne);
5524 /* lets us use one switch statement for arbitrary mode/detail combinations */
5525 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
5528 focusevent(XEvent *e)
5532 u_int32_t mode_detail;
5533 XFocusChangeEvent *ev = &e->xfocus;
5535 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
5536 ev->type == FocusIn ? "entering" : "leaving",
5537 ev->window, ev->mode, ev->detail);
5539 if (last_focus_event == ev->type) {
5540 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
5544 last_focus_event = ev->type;
5545 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
5547 switch (mode_detail) {
5548 /* synergy client focus operations */
5549 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
5550 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
5552 /* synergy server focus operations */
5553 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
5555 /* Entering applications like rdesktop that mangle the pointer */
5556 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
5558 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
5559 XSetWindowBorder(display, win->id,
5560 win->ws->r->s->c[ev->type == FocusIn ?
5561 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
5564 fprintf(stderr, "ignoring focusevent\n");
5565 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
5572 mapnotify(XEvent *e)
5575 XMapEvent *ev = &e->xmap;
5577 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
5579 win = manage_window(ev->window);
5581 set_win_state(win, NormalState);
5585 mappingnotify(XEvent *e)
5587 XMappingEvent *ev = &e->xmapping;
5589 XRefreshKeyboardMapping(ev);
5590 if (ev->request == MappingKeyboard)
5595 maprequest(XEvent *e)
5598 struct swm_region *r;
5599 XWindowAttributes wa;
5600 XMapRequestEvent *ev = &e->xmaprequest;
5602 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
5603 e->xmaprequest.window);
5605 if (!XGetWindowAttributes(display, ev->window, &wa))
5607 if (wa.override_redirect)
5610 win = manage_window(e->xmaprequest.window);
5612 return; /* can't happen */
5616 /* make new win focused */
5617 r = root_to_region(win->wa.root);
5618 if (win->ws == r->ws)
5623 propertynotify(XEvent *e)
5626 XPropertyEvent *ev = &e->xproperty;
5628 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
5631 win = find_window(ev->window);
5635 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
5636 update_iconic(win, 0);
5637 XMapRaised(display, win->id);
5644 case XA_WM_NORMAL_HINTS:
5647 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5648 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
5649 if (win->sh.flags & PMinSize) {
5650 win->g.w = win->sh.min_width;
5651 win->g.h = win->sh.min_height;
5652 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
5654 XMoveResizeWindow(display, win->id,
5655 win->g.x, win->g.y, win->g.w, win->g.h);
5657 if (window_name_enabled)
5666 unmapnotify(XEvent *e)
5670 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
5672 /* determine if we need to help unmanage this window */
5673 win = find_window(e->xunmap.window);
5677 if (getstate(e->xunmap.window) == NormalState) {
5678 unmanage_window(win);
5681 /* giant hack for apps that don't destroy transient windows */
5682 /* eat a bunch of events to prevent remanaging the window */
5684 while (XCheckWindowEvent(display, e->xunmap.window,
5685 EnterWindowMask, &cne))
5687 while (XCheckWindowEvent(display, e->xunmap.window,
5688 StructureNotifyMask, &cne))
5690 while (XCheckWindowEvent(display, e->xunmap.window,
5691 SubstructureNotifyMask, &cne))
5693 /* resend unmap because we ated it */
5694 XUnmapWindow(display, e->xunmap.window);
5697 if (focus_mode == SWM_FOCUS_DEFAULT)
5698 drain_enter_notify();
5702 visibilitynotify(XEvent *e)
5705 struct swm_region *r;
5707 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
5708 e->xvisibility.window);
5709 if (e->xvisibility.state == VisibilityUnobscured)
5710 for (i = 0; i < ScreenCount(display); i++)
5711 TAILQ_FOREACH(r, &screens[i].rl, entry)
5712 if (e->xvisibility.window == r->bar_window)
5717 clientmessage(XEvent *e)
5719 XClientMessageEvent *ev;
5724 win = find_window(ev->window);
5728 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx type: %ld \n",
5729 ev->window, ev->message_type);
5731 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
5732 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW \n");
5735 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
5736 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW \n");
5737 if (win->can_delete)
5738 client_msg(win, adelete);
5740 XKillClient(display, win->id);
5742 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
5743 DNPRINTF(SWM_D_EVENT,
5744 "clientmessage: _NET_MOVERESIZE_WINDOW \n");
5745 if (win->floating) {
5746 if (ev->data.l[0] & (1<<8)) /* x */
5747 win->g.x = ev->data.l[1];
5748 if (ev->data.l[0] & (1<<9)) /* y */
5749 win->g.y = ev->data.l[2];
5750 if (ev->data.l[0] & (1<<10)) /* width */
5751 win->g.w = ev->data.l[3];
5752 if (ev->data.l[0] & (1<<11)) /* height */
5753 win->g.h = ev->data.l[4];
5756 /* TODO: Change stack sizes */
5758 config_win(win, NULL);
5760 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
5761 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE \n");
5762 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
5764 ewmh_update_win_state(win, ev->data.l[2],
5772 xerror_start(Display *d, XErrorEvent *ee)
5779 xerror(Display *d, XErrorEvent *ee)
5781 /* fprintf(stderr, "error: %p %p\n", display, ee); */
5789 xerrorxlib = XSetErrorHandler(xerror_start);
5791 /* this causes an error if some other window manager is running */
5792 XSelectInput(display, DefaultRootWindow(display),
5793 SubstructureRedirectMask);
5794 XSync(display, False);
5798 XSetErrorHandler(xerror);
5799 XSync(display, False);
5804 new_region(struct swm_screen *s, int x, int y, int w, int h)
5806 struct swm_region *r, *n;
5807 struct workspace *ws = NULL;
5810 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
5811 s->idx, w, h, x, y);
5813 /* remove any conflicting regions */
5814 n = TAILQ_FIRST(&s->rl);
5817 n = TAILQ_NEXT(r, entry);
5818 if (X(r) < (x + w) &&
5819 (X(r) + WIDTH(r)) > x &&
5821 (Y(r) + HEIGHT(r)) > y) {
5822 if (r->ws->r != NULL)
5823 r->ws->old_r = r->ws->r;
5825 XDestroyWindow(display, r->bar_window);
5826 TAILQ_REMOVE(&s->rl, r, entry);
5827 TAILQ_INSERT_TAIL(&s->orl, r, entry);
5831 /* search old regions for one to reuse */
5833 /* size + location match */
5834 TAILQ_FOREACH(r, &s->orl, entry)
5835 if (X(r) == x && Y(r) == y &&
5836 HEIGHT(r) == h && WIDTH(r) == w)
5840 TAILQ_FOREACH(r, &s->orl, entry)
5841 if (HEIGHT(r) == h && WIDTH(r) == w)
5845 TAILQ_REMOVE(&s->orl, r, entry);
5846 /* try to use old region's workspace */
5847 if (r->ws->r == NULL)
5850 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
5851 errx(1, "calloc: failed to allocate memory for screen");
5853 /* if we don't have a workspace already, find one */
5855 for (i = 0; i < SWM_WS_MAX; i++)
5856 if (s->ws[i].r == NULL) {
5863 errx(1, "no free workspaces\n");
5874 TAILQ_INSERT_TAIL(&s->rl, r, entry);
5880 #ifdef SWM_XRR_HAS_CRTC
5882 XRRScreenResources *sr;
5885 #endif /* SWM_XRR_HAS_CRTC */
5886 struct swm_region *r;
5889 if (i >= ScreenCount(display))
5890 errx(1, "invalid screen");
5892 /* remove any old regions */
5893 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
5894 r->ws->old_r = r->ws->r = NULL;
5895 XDestroyWindow(display, r->bar_window);
5896 TAILQ_REMOVE(&screens[i].rl, r, entry);
5897 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
5901 /* map virtual screens onto physical screens */
5902 #ifdef SWM_XRR_HAS_CRTC
5903 if (xrandr_support) {
5904 sr = XRRGetScreenResources(display, screens[i].root);
5906 new_region(&screens[i], 0, 0,
5907 DisplayWidth(display, i),
5908 DisplayHeight(display, i));
5912 for (c = 0, ci = NULL; c < ncrtc; c++) {
5913 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
5914 if (ci->noutput == 0)
5917 if (ci != NULL && ci->mode == None)
5918 new_region(&screens[i], 0, 0,
5919 DisplayWidth(display, i),
5920 DisplayHeight(display, i));
5922 new_region(&screens[i],
5923 ci->x, ci->y, ci->width, ci->height);
5926 XRRFreeCrtcInfo(ci);
5927 XRRFreeScreenResources(sr);
5929 #endif /* SWM_XRR_HAS_CRTC */
5931 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
5932 DisplayHeight(display, i));
5937 screenchange(XEvent *e) {
5938 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
5939 struct swm_region *r;
5942 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
5944 if (!XRRUpdateConfiguration(e))
5947 /* silly event doesn't include the screen index */
5948 for (i = 0; i < ScreenCount(display); i++)
5949 if (screens[i].root == xe->root)
5951 if (i >= ScreenCount(display))
5952 errx(1, "screenchange: screen not found\n");
5954 /* brute force for now, just re-enumerate the regions */
5957 /* add bars to all regions */
5958 for (i = 0; i < ScreenCount(display); i++)
5959 TAILQ_FOREACH(r, &screens[i].rl, entry)
5962 if (focus_mode == SWM_FOCUS_DEFAULT)
5963 drain_enter_notify();
5969 Window d1, d2, *wins = NULL;
5970 XWindowAttributes wa;
5975 for (i = 0; i < ScreenCount(display); i++) {
5976 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
5979 /* attach windows to a region */
5980 /* normal windows */
5981 for (j = 0; j < no; j++) {
5982 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5983 wa.override_redirect ||
5984 XGetTransientForHint(display, wins[j], &d1))
5987 state = getstate(wins[j]);
5988 manage = state == IconicState;
5989 if (wa.map_state == IsViewable || manage)
5990 manage_window(wins[j]);
5992 /* transient windows */
5993 for (j = 0; j < no; j++) {
5994 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5995 wa.override_redirect)
5998 state = getstate(wins[j]);
5999 manage = state == IconicState;
6000 if (XGetTransientForHint(display, wins[j], &d1) &&
6002 manage_window(wins[j]);
6015 int errorbase, major, minor;
6016 struct workspace *ws;
6018 if ((screens = calloc(ScreenCount(display),
6019 sizeof(struct swm_screen))) == NULL)
6020 errx(1, "calloc: screens");
6022 /* initial Xrandr setup */
6023 xrandr_support = XRRQueryExtension(display,
6024 &xrandr_eventbase, &errorbase);
6026 if (XRRQueryVersion(display, &major, &minor) && major < 1)
6029 /* map physical screens */
6030 for (i = 0; i < ScreenCount(display); i++) {
6031 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
6033 TAILQ_INIT(&screens[i].rl);
6034 TAILQ_INIT(&screens[i].orl);
6035 screens[i].root = RootWindow(display, i);
6037 /* set default colors */
6038 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
6039 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
6040 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
6041 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
6042 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
6044 /* set default cursor */
6045 XDefineCursor(display, screens[i].root,
6046 XCreateFontCursor(display, XC_left_ptr));
6048 /* init all workspaces */
6049 /* XXX these should be dynamically allocated too */
6050 for (j = 0; j < SWM_WS_MAX; j++) {
6051 ws = &screens[i].ws[j];
6056 TAILQ_INIT(&ws->winlist);
6057 TAILQ_INIT(&ws->unmanagedlist);
6059 for (k = 0; layouts[k].l_stack != NULL; k++)
6060 if (layouts[k].l_config != NULL)
6061 layouts[k].l_config(ws,
6062 SWM_ARG_ID_STACKINIT);
6063 ws->cur_layout = &layouts[0];
6064 ws->cur_layout->l_string(ws);
6070 XRRSelectInput(display, screens[i].root,
6071 RRScreenChangeNotifyMask);
6078 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
6080 err(1, "setup_globals: strdup");
6081 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
6083 err(1, "setup_globals: strdup");
6084 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
6086 err(1, "setup_globals: strdup");
6087 if ((spawn_term[0] = strdup("xterm")) == NULL)
6088 err(1, "setup_globals: strdup");
6089 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
6097 Atom netwmcheck, netwmname, utf8_string;
6100 /* work around sun jdk bugs, code from wmname */
6101 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
6102 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
6103 utf8_string = XInternAtom(display, "UTF8_STRING", False);
6104 for (i = 0; i < ScreenCount(display); i++) {
6105 root = screens[i].root;
6106 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
6107 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
6108 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
6110 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
6111 PropModeReplace, (unsigned char *)&win,1);
6112 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
6113 PropModeReplace, (unsigned char *)&win,1);
6114 XChangeProperty(display, win, netwmname, utf8_string, 8,
6115 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
6120 main(int argc, char *argv[])
6123 struct swm_region *r, *rr;
6124 struct ws_win *winfocus = NULL;
6127 char conf[PATH_MAX], *cfile = NULL;
6132 struct sigaction sact;
6135 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
6136 SWM_VERSION, cvstag);
6137 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
6138 warnx("no locale support");
6140 if (!(display = XOpenDisplay(0)))
6141 errx(1, "can not open display");
6144 errx(1, "other wm running");
6146 /* handle some signals */
6147 bzero(&sact, sizeof(sact));
6148 sigemptyset(&sact.sa_mask);
6150 sact.sa_handler = sighdlr;
6151 sigaction(SIGINT, &sact, NULL);
6152 sigaction(SIGQUIT, &sact, NULL);
6153 sigaction(SIGTERM, &sact, NULL);
6154 sigaction(SIGHUP, &sact, NULL);
6156 sact.sa_handler = sighdlr;
6157 sact.sa_flags = SA_NOCLDSTOP;
6158 sigaction(SIGCHLD, &sact, NULL);
6160 astate = XInternAtom(display, "WM_STATE", False);
6161 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
6162 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
6163 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
6164 a_wmname = XInternAtom(display, "WM_NAME", False);
6165 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
6166 a_string = XInternAtom(display, "STRING", False);
6167 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
6169 /* look for local and global conf file */
6170 pwd = getpwuid(getuid());
6172 errx(1, "invalid user %d", getuid());
6181 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
6182 if (stat(conf, &sb) != -1) {
6183 if (S_ISREG(sb.st_mode))
6186 /* try global conf file */
6187 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
6188 if (!stat(conf, &sb))
6189 if (S_ISREG(sb.st_mode))
6196 /* set some values to work around bad programs */
6199 /* grab existing windows (before we build the bars) */
6202 if (getenv("SWM_STARTED") == NULL)
6203 setenv("SWM_STARTED", "YES", 1);
6205 /* setup all bars */
6206 for (i = 0; i < ScreenCount(display); i++)
6207 TAILQ_FOREACH(r, &screens[i].rl, entry) {
6208 if (winfocus == NULL)
6209 winfocus = TAILQ_FIRST(&r->ws->winlist);
6217 if (focus_mode == SWM_FOCUS_DEFAULT)
6218 drain_enter_notify();
6220 xfd = ConnectionNumber(display);
6222 while (XPending(display)) {
6223 XNextEvent(display, &e);
6226 if (e.type < LASTEvent) {
6228 if (handler[e.type])
6229 handler[e.type](&e);
6231 DNPRINTF(SWM_D_EVENT,
6232 "win: %lu unknown event: %d\n",
6233 e.xany.window, e.type);
6235 switch (e.type - xrandr_eventbase) {
6236 case RRScreenChangeNotify:
6240 DNPRINTF(SWM_D_EVENT,
6241 "win: %lu unknown xrandr event: "
6242 "%d\n", e.xany.window, e.type);
6248 /* if we are being restarted go focus on first window */
6250 rr = winfocus->ws->r;
6252 /* not a visible window */
6256 /* move pointer to first screen if multi screen */
6257 if (ScreenCount(display) > 1 || outputs > 1)
6258 XWarpPointer(display, None, rr->s[0].root,
6259 0, 0, 0, 0, rr->g.x,
6260 rr->g.y + (bar_enabled ? bar_height : 0));
6262 a.id = SWM_ARG_ID_FOCUSCUR;
6272 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
6274 DNPRINTF(SWM_D_MISC, "select failed");
6275 if (restart_wm == 1)
6276 restart(NULL, NULL);
6277 if (search_resp == 1)
6289 XFreeGC(display, bar_gc);
6290 XCloseDisplay(display);