2 * Copyright (c) 2009-2010-2011 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2010-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Much code and ideas taken from dwm under the following license:
23 * MIT/X Consortium License
25 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
26 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
27 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
28 * 2007 Premysl Hruby <dfenze at gmail dot com>
29 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
30 * 2007 Christof Musik <christof at sendfax dot de>
31 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
32 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
33 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
35 * Permission is hereby granted, free of charge, to any person obtaining a
36 * copy of this software and associated documentation files (the "Software"),
37 * to deal in the Software without restriction, including without limitation
38 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
39 * and/or sell copies of the Software, and to permit persons to whom the
40 * Software is furnished to do so, subject to the following conditions:
42 * The above copyright notice and this permission notice shall be included in
43 * all copies or substantial portions of the Software.
45 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
46 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
47 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
48 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
49 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
50 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
51 * DEALINGS IN THE SOFTWARE.
69 #include <sys/types.h>
73 #include <sys/queue.h>
74 #include <sys/param.h>
75 #include <sys/select.h>
77 #include <X11/cursorfont.h>
78 #include <X11/keysym.h>
79 #include <X11/Xatom.h>
81 #include <X11/Xproto.h>
82 #include <X11/Xutil.h>
83 #include <X11/extensions/Xrandr.h>
84 #include <X11/extensions/XTest.h>
92 #ifdef SCROTWM_BUILDSTR
93 static const char *buildstr = SCROTWM_BUILDSTR;
95 static const char *buildstr = SCROTWM_VERSION;
99 # error XRandR versions less than 1.0 are not supported
104 #define SWM_XRR_HAS_CRTC
108 /*#define SWM_DEBUG*/
110 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
111 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
112 #define SWM_D_MISC 0x0001
113 #define SWM_D_EVENT 0x0002
114 #define SWM_D_WS 0x0004
115 #define SWM_D_FOCUS 0x0008
116 #define SWM_D_MOVE 0x0010
117 #define SWM_D_STACK 0x0020
118 #define SWM_D_MOUSE 0x0040
119 #define SWM_D_PROP 0x0080
120 #define SWM_D_CLASS 0x0100
121 #define SWM_D_KEY 0x0200
122 #define SWM_D_QUIRK 0x0400
123 #define SWM_D_SPAWN 0x0800
124 #define SWM_D_EVENTQ 0x1000
125 #define SWM_D_CONF 0x2000
127 u_int32_t swm_debug = 0
144 #define DPRINTF(x...)
145 #define DNPRINTF(n,x...)
148 #define LENGTH(x) (sizeof x / sizeof x[0])
149 #define MODKEY Mod1Mask
150 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
151 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
152 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
153 #define SWM_PROPLEN (16)
154 #define SWM_FUNCNAME_LEN (32)
155 #define SWM_KEYS_LEN (255)
156 #define SWM_QUIRK_LEN (64)
157 #define X(r) (r)->g.x
158 #define Y(r) (r)->g.y
159 #define WIDTH(r) (r)->g.w
160 #define HEIGHT(r) (r)->g.h
161 #define SWM_MAX_FONT_STEPS (3)
162 #define WINID(w) (w ? w->id : 0)
164 #define SWM_FOCUS_DEFAULT (0)
165 #define SWM_FOCUS_SYNERGY (1)
166 #define SWM_FOCUS_FOLLOW (2)
168 #define SWM_CONF_DEFAULT (0)
169 #define SWM_CONF_KEYMAPPING (1)
172 #define SWM_LIB "/usr/local/lib/libswmhack.so"
184 volatile sig_atomic_t running = 1;
185 volatile sig_atomic_t restart_wm = 0;
187 int last_focus_event = FocusOut;
188 int (*xerrorxlib)(Display *, XErrorEvent *);
192 int xrandr_eventbase;
193 unsigned int numlockmask = 0;
197 int cycle_visible = 0;
199 int font_adjusted = 0;
200 unsigned int mod_key = MODKEY;
203 struct swm_region *search_r;
204 int select_list_pipe[2];
205 int select_resp_pipe[2];
207 volatile sig_atomic_t search_resp;
210 double dialog_ratio = .6;
212 #define SWM_BAR_MAX (256)
213 char *bar_argv[] = { NULL, NULL };
215 char bar_ext[SWM_BAR_MAX];
216 char bar_vertext[SWM_BAR_MAX];
218 sig_atomic_t bar_alarm = 0;
221 int bar_border_width = 1;
222 int bar_at_bottom = 0;
224 int bar_extra_running = 0;
227 int stack_enabled = 1;
228 int clock_enabled = 1;
229 int urgent_enabled = 0;
230 char *clock_format = NULL;
231 int title_name_enabled = 0;
232 int title_class_enabled = 0;
233 int window_name_enabled = 0;
234 int focus_mode = SWM_FOCUS_DEFAULT;
235 int disable_border = 0;
236 int border_width = 1;
237 int verbose_layout = 0;
243 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
244 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
247 #define SWM_MENU_FN (2)
248 #define SWM_MENU_NB (4)
249 #define SWM_MENU_NF (6)
250 #define SWM_MENU_SB (8)
251 #define SWM_MENU_SF (10)
253 /* layout manager data */
254 struct swm_geometry {
264 /* virtual "screens" */
266 TAILQ_ENTRY(swm_region) entry;
267 struct swm_geometry g;
268 struct workspace *ws; /* current workspace on this region */
269 struct workspace *ws_prior; /* prior workspace on this region */
270 struct swm_screen *s; /* screen idx */
273 TAILQ_HEAD(swm_region_list, swm_region);
276 TAILQ_ENTRY(ws_win) entry;
279 struct ws_win *child_trans; /* transient child window */
280 struct swm_geometry g; /* current geometry */
281 struct swm_geometry g_float; /* geometry when floating */
282 struct swm_geometry rg_float; /* region geom when floating */
283 int g_floatvalid; /* flag: geometry in g_float is valid */
284 int floatmaxed; /* flag: floater was maxed in max_stack */
288 unsigned int ewmh_flags;
289 int font_size_boundary[SWM_MAX_FONT_STEPS];
295 unsigned long quirks;
296 struct workspace *ws; /* always valid */
297 struct swm_screen *s; /* always valid, never changes */
298 XWindowAttributes wa;
303 TAILQ_HEAD(ws_win_list, ws_win);
307 TAILQ_ENTRY(pid_e) entry;
311 TAILQ_HEAD(pid_list, pid_e);
312 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
314 /* layout handlers */
316 void vertical_config(struct workspace *, int);
317 void vertical_stack(struct workspace *, struct swm_geometry *);
318 void horizontal_config(struct workspace *, int);
319 void horizontal_stack(struct workspace *, struct swm_geometry *);
320 void max_stack(struct workspace *, struct swm_geometry *);
321 void plain_stacker(struct workspace *);
322 void fancy_stacker(struct workspace *);
324 struct ws_win *find_window(Window);
326 void grabbuttons(struct ws_win *, int);
327 void new_region(struct swm_screen *, int, int, int, int);
328 void unmanage_window(struct ws_win *);
329 long getstate(Window);
331 int conf_load(char *, int);
334 void (*l_stack)(struct workspace *, struct swm_geometry *);
335 void (*l_config)(struct workspace *, int);
337 #define SWM_L_FOCUSPREV (1<<0)
338 #define SWM_L_MAPONFOCUS (1<<1)
339 void (*l_string)(struct workspace *);
341 /* stack, configure */
342 { vertical_stack, vertical_config, 0, plain_stacker },
343 { horizontal_stack, horizontal_config, 0, plain_stacker },
345 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
346 { NULL, NULL, 0, NULL },
349 /* position of max_stack mode in the layouts array, index into layouts! */
350 #define SWM_V_STACK (0)
351 #define SWM_H_STACK (1)
352 #define SWM_MAX_STACK (2)
354 #define SWM_H_SLICE (32)
355 #define SWM_V_SLICE (32)
357 /* define work spaces */
359 int idx; /* workspace index */
360 int always_raise; /* raise windows on focus */
361 struct layout *cur_layout; /* current layout handlers */
362 struct ws_win *focus; /* may be NULL */
363 struct ws_win *focus_prev; /* may be NULL */
364 struct swm_region *r; /* may be NULL */
365 struct swm_region *old_r; /* may be NULL */
366 struct ws_win_list winlist; /* list of windows in ws */
367 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
368 char stacker[10]; /* display stacker and layout */
372 int horizontal_msize;
374 int horizontal_stacks;
381 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
382 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
384 /* physical screen mapping */
385 #define SWM_WS_MAX (10)
387 int idx; /* screen index */
388 struct swm_region_list rl; /* list of regions on this screen */
389 struct swm_region_list orl; /* list of old regions */
391 struct workspace ws[SWM_WS_MAX];
397 } c[SWM_S_COLOR_MAX];
399 struct swm_screen *screens;
402 /* args to functions */
405 #define SWM_ARG_ID_FOCUSNEXT (0)
406 #define SWM_ARG_ID_FOCUSPREV (1)
407 #define SWM_ARG_ID_FOCUSMAIN (2)
408 #define SWM_ARG_ID_FOCUSCUR (4)
409 #define SWM_ARG_ID_SWAPNEXT (10)
410 #define SWM_ARG_ID_SWAPPREV (11)
411 #define SWM_ARG_ID_SWAPMAIN (12)
412 #define SWM_ARG_ID_MOVELAST (13)
413 #define SWM_ARG_ID_MASTERSHRINK (20)
414 #define SWM_ARG_ID_MASTERGROW (21)
415 #define SWM_ARG_ID_MASTERADD (22)
416 #define SWM_ARG_ID_MASTERDEL (23)
417 #define SWM_ARG_ID_STACKRESET (30)
418 #define SWM_ARG_ID_STACKINIT (31)
419 #define SWM_ARG_ID_CYCLEWS_UP (40)
420 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
421 #define SWM_ARG_ID_CYCLESC_UP (42)
422 #define SWM_ARG_ID_CYCLESC_DOWN (43)
423 #define SWM_ARG_ID_STACKINC (50)
424 #define SWM_ARG_ID_STACKDEC (51)
425 #define SWM_ARG_ID_SS_ALL (60)
426 #define SWM_ARG_ID_SS_WINDOW (61)
427 #define SWM_ARG_ID_DONTCENTER (70)
428 #define SWM_ARG_ID_CENTER (71)
429 #define SWM_ARG_ID_KILLWINDOW (80)
430 #define SWM_ARG_ID_DELETEWINDOW (81)
431 #define SWM_ARG_ID_WIDTHGROW (90)
432 #define SWM_ARG_ID_WIDTHSHRINK (91)
433 #define SWM_ARG_ID_HEIGHTGROW (92)
434 #define SWM_ARG_ID_HEIGHTSHRINK (93)
435 #define SWM_ARG_ID_MOVEUP (100)
436 #define SWM_ARG_ID_MOVEDOWN (101)
437 #define SWM_ARG_ID_MOVELEFT (102)
438 #define SWM_ARG_ID_MOVERIGHT (103)
442 void focus(struct swm_region *, union arg *);
443 void focus_magic(struct ws_win *);
450 #define SWM_Q_FLOAT (1<<0) /* float this window */
451 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
452 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
453 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
454 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
455 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
457 int quirks_size = 0, quirks_length = 0;
458 struct quirk *quirks = NULL;
461 * Supported EWMH hints should be added to
462 * both the enum and the ewmh array
464 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
465 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
466 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
467 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
468 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
469 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
470 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
471 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
472 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
473 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
479 } ewmh[SWM_EWMH_HINT_MAX] = {
480 /* must be in same order as in the enum */
481 {"_NET_ACTIVE_WINDOW", None},
482 {"_NET_MOVERESIZE_WINDOW", None},
483 {"_NET_CLOSE_WINDOW", None},
484 {"_NET_WM_WINDOW_TYPE", None},
485 {"_NET_WM_WINDOW_TYPE_DOCK", None},
486 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
487 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
488 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
489 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
490 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
491 {"_NET_WM_STATE", None},
492 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
493 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
494 {"_NET_WM_STATE_SKIP_TASKBAR", None},
495 {"_NET_WM_STATE_SKIP_PAGER", None},
496 {"_NET_WM_STATE_HIDDEN", None},
497 {"_NET_WM_STATE_ABOVE", None},
498 {"_SWM_WM_STATE_MANUAL", None},
499 {"_NET_WM_STATE_FULLSCREEN", None},
500 {"_NET_WM_ALLOWED_ACTIONS", None},
501 {"_NET_WM_ACTION_MOVE", None},
502 {"_NET_WM_ACTION_RESIZE", None},
503 {"_NET_WM_ACTION_FULLSCREEN", None},
504 {"_NET_WM_ACTION_CLOSE", None},
507 void store_float_geom(struct ws_win *win, struct swm_region *r);
508 int floating_toggle_win(struct ws_win *win);
509 void spawn_select(struct swm_region *, union arg *, char *, int *);
512 get_property(Window id, Atom atom, long count, Atom type,
513 unsigned long *n, unsigned char **data)
516 unsigned long tmp, extra;
517 unsigned long *nitems;
520 nitems = n != NULL ? n : &tmp;
521 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
522 &real, &format, nitems, &extra, data);
524 if (status != Success)
533 update_iconic(struct ws_win *win, int newv)
540 iprop = XInternAtom(display, "_SWM_ICONIC", False);
544 XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
545 PropModeReplace, (unsigned char *)&v, 1);
547 XDeleteProperty(display, win->id, iprop);
551 get_iconic(struct ws_win *win)
556 unsigned long nitems, extra;
557 unsigned char *prop = NULL;
559 iprop = XInternAtom(display, "_SWM_ICONIC", False);
562 status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
563 False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
564 if (status != Success)
566 if (rettype != XA_INTEGER || retfmt != 32)
570 v = *((int32_t *)prop);
584 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
586 for (i = 0; i < LENGTH(ewmh); i++)
587 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
589 for (i = 0; i < ScreenCount(display); i++) {
590 /* Support check window will be created by workaround(). */
592 /* Report supported atoms */
593 XDeleteProperty(display, screens[i].root, sup_list);
594 for (j = 0; j < LENGTH(ewmh); j++)
595 XChangeProperty(display, screens[i].root,
596 sup_list, XA_ATOM, 32,
597 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
605 unsigned char *data = NULL;
607 Atom sup_check, sup_list;
610 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
611 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
613 for (i = 0; i < ScreenCount(display); i++) {
614 /* Get the support check window and destroy it */
615 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
620 XDestroyWindow(display, id);
621 XDeleteProperty(display, screens[i].root, sup_check);
622 XDeleteProperty(display, screens[i].root, sup_list);
630 ewmh_autoquirk(struct ws_win *win)
633 unsigned long *data = NULL, n;
636 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
637 XA_ATOM, &n, (void *)&data);
644 for (i = 0; i < n; i++) {
646 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
648 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
649 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
650 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
652 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
655 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
656 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
658 win->quirks = SWM_Q_FLOAT;
666 #define SWM_EWMH_ACTION_COUNT_MAX (6)
667 #define EWMH_F_FULLSCREEN (1<<0)
668 #define EWMH_F_ABOVE (1<<1)
669 #define EWMH_F_HIDDEN (1<<2)
670 #define EWMH_F_SKIP_PAGER (1<<3)
671 #define EWMH_F_SKIP_TASKBAR (1<<4)
672 #define SWM_F_MANUAL (1<<5)
675 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
677 struct swm_geometry rg;
685 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
691 store_float_geom(win, win->ws->r);
698 if (win->g_floatvalid) {
699 /* refloat at last floating relative position */
700 win->g.x = win->g_float.x - win->rg_float.x + rg.x;
701 win->g.y = win->g_float.y - win->rg_float.y + rg.y;
702 win->g.w = win->g_float.w;
703 win->g.h = win->g_float.h;
711 ewmh_update_actions(struct ws_win *win)
713 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
719 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
722 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
723 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
726 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
727 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
730 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
731 #define _NET_WM_STATE_ADD 1 /* add/set property */
732 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
735 ewmh_update_win_state(struct ws_win *win, long state, long action)
737 unsigned int mask = 0;
738 unsigned int changed = 0;
739 unsigned int orig_flags;
744 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
745 mask = EWMH_F_FULLSCREEN;
746 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
748 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
750 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
751 mask = EWMH_F_SKIP_PAGER;
752 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
753 mask = EWMH_F_SKIP_TASKBAR;
756 orig_flags = win->ewmh_flags;
759 case _NET_WM_STATE_REMOVE:
760 win->ewmh_flags &= ~mask;
762 case _NET_WM_STATE_ADD:
763 win->ewmh_flags |= mask;
765 case _NET_WM_STATE_TOGGLE:
766 win->ewmh_flags ^= mask;
770 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
772 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
774 if (!floating_toggle_win(win))
775 win->ewmh_flags = orig_flags; /* revert */
776 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
778 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
779 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
781 if (!ewmh_set_win_fullscreen(win,
782 win->ewmh_flags & EWMH_F_FULLSCREEN))
783 win->ewmh_flags = orig_flags; /* revert */
785 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
787 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
788 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
789 XA_ATOM, 32, PropModeAppend,
790 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
791 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
792 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
793 XA_ATOM, 32, PropModeAppend,
794 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
795 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
796 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
797 XA_ATOM, 32, PropModeAppend,
798 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
799 if (win->ewmh_flags & EWMH_F_ABOVE)
800 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
801 XA_ATOM, 32, PropModeAppend,
802 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
803 if (win->ewmh_flags & SWM_F_MANUAL)
804 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
805 XA_ATOM, 32, PropModeAppend,
806 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
810 ewmh_get_win_state(struct ws_win *win)
821 win->ewmh_flags |= EWMH_F_ABOVE;
823 win->ewmh_flags |= SWM_F_MANUAL;
825 success = get_property(win->id, ewmh[_NET_WM_STATE].atom,
826 (~0L), XA_ATOM, &n, (void *)&states);
831 for (i = 0; i < n; i++)
832 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
852 name = "ButtonPress";
855 name = "ButtonRelease";
858 name = "MotionNotify";
861 name = "EnterNotify";
864 name = "LeaveNotify";
873 name = "KeymapNotify";
879 name = "GraphicsExpose";
884 case VisibilityNotify:
885 name = "VisibilityNotify";
888 name = "CreateNotify";
891 name = "DestroyNotify";
894 name = "UnmapNotify";
903 name = "ReparentNotify";
905 case ConfigureNotify:
906 name = "ConfigureNotify";
908 case ConfigureRequest:
909 name = "ConfigureRequest";
912 name = "GravityNotify";
915 name = "ResizeRequest";
917 case CirculateNotify:
918 name = "CirculateNotify";
920 case CirculateRequest:
921 name = "CirculateRequest";
924 name = "PropertyNotify";
927 name = "SelectionClear";
929 case SelectionRequest:
930 name = "SelectionRequest";
932 case SelectionNotify:
933 name = "SelectionNotify";
936 name = "ColormapNotify";
939 name = "ClientMessage";
942 name = "MappingNotify";
947 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
949 e->xany.window, name, e->type, QLength(display));
951 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
953 e->xany.window, e->type, QLength(display));
957 dumpwins(struct swm_region *r, union arg *args)
961 XWindowAttributes wa;
964 fprintf(stderr, "invalid workspace\n");
968 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
970 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
971 state = getstate(win->id);
972 if (!XGetWindowAttributes(display, win->id, &wa))
973 fprintf(stderr, "window: %lu failed "
974 "XGetWindowAttributes\n", win->id);
975 fprintf(stderr, "window: %lu map_state: %d state: %d "
977 win->id, wa.map_state, state, win->transient);
980 fprintf(stderr, "===== unmanaged window list =====\n");
981 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
982 state = getstate(win->id);
983 if (!XGetWindowAttributes(display, win->id, &wa))
984 fprintf(stderr, "window: %lu failed "
985 "XGetWindowAttributes\n", win->id);
986 fprintf(stderr, "window: %lu map_state: %d state: %d "
988 win->id, wa.map_state, state, win->transient);
991 fprintf(stderr, "=================================\n");
996 dumpwins(struct swm_region *r, union arg *args)
999 #endif /* SWM_DEBUG */
1001 void expose(XEvent *);
1002 void keypress(XEvent *);
1003 void buttonpress(XEvent *);
1004 void configurerequest(XEvent *);
1005 void configurenotify(XEvent *);
1006 void destroynotify(XEvent *);
1007 void enternotify(XEvent *);
1008 void focusevent(XEvent *);
1009 void mapnotify(XEvent *);
1010 void mappingnotify(XEvent *);
1011 void maprequest(XEvent *);
1012 void propertynotify(XEvent *);
1013 void unmapnotify(XEvent *);
1014 void visibilitynotify(XEvent *);
1015 void clientmessage(XEvent *);
1017 void (*handler[LASTEvent])(XEvent *) = {
1019 [KeyPress] = keypress,
1020 [ButtonPress] = buttonpress,
1021 [ConfigureRequest] = configurerequest,
1022 [ConfigureNotify] = configurenotify,
1023 [DestroyNotify] = destroynotify,
1024 [EnterNotify] = enternotify,
1025 [FocusIn] = focusevent,
1026 [FocusOut] = focusevent,
1027 [MapNotify] = mapnotify,
1028 [MappingNotify] = mappingnotify,
1029 [MapRequest] = maprequest,
1030 [PropertyNotify] = propertynotify,
1031 [UnmapNotify] = unmapnotify,
1032 [VisibilityNotify] = visibilitynotify,
1033 [ClientMessage] = clientmessage,
1039 int saved_errno, status;
1042 saved_errno = errno;
1046 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1051 if (errno != ECHILD)
1052 warn("sighdlr: waitpid");
1053 #endif /* SWM_DEBUG */
1056 if (pid == searchpid)
1060 if (WIFEXITED(status)) {
1061 if (WEXITSTATUS(status) != 0)
1062 warnx("sighdlr: child exit status: %d",
1063 WEXITSTATUS(status));
1065 warnx("sighdlr: child is terminated "
1067 #endif /* SWM_DEBUG */
1081 errno = saved_errno;
1087 struct pid_e *p = NULL;
1089 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1094 TAILQ_FOREACH(p, &pidlist, entry) {
1103 name_to_color(char *colorname)
1107 XColor screen_def, exact_def;
1108 unsigned long result = 0;
1109 char cname[32] = "#";
1111 cmap = DefaultColormap(display, screens[0].idx);
1112 status = XAllocNamedColor(display, cmap, colorname,
1113 &screen_def, &exact_def);
1115 strlcat(cname, colorname + 2, sizeof cname - 1);
1116 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1120 result = screen_def.pixel;
1122 fprintf(stderr, "color '%s' not found.\n", colorname);
1128 setscreencolor(char *val, int i, int c)
1130 if (i > 0 && i <= ScreenCount(display)) {
1131 screens[i - 1].c[c].color = name_to_color(val);
1132 free(screens[i - 1].c[c].name);
1133 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1135 } else if (i == -1) {
1136 for (i = 0; i < ScreenCount(display); i++) {
1137 screens[i].c[c].color = name_to_color(val);
1138 free(screens[i].c[c].name);
1139 if ((screens[i].c[c].name = strdup(val)) == NULL)
1143 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1144 i, ScreenCount(display));
1148 fancy_stacker(struct workspace *ws)
1150 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1151 if (ws->cur_layout->l_stack == vertical_stack)
1152 snprintf(ws->stacker, sizeof ws->stacker, "[%d|%d]",
1153 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1154 if (ws->cur_layout->l_stack == horizontal_stack)
1155 snprintf(ws->stacker, sizeof ws->stacker, "[%d-%d]",
1156 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1160 plain_stacker(struct workspace *ws)
1162 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1163 if (ws->cur_layout->l_stack == vertical_stack)
1164 strlcpy(ws->stacker, "[|]", sizeof ws->stacker);
1165 if (ws->cur_layout->l_stack == horizontal_stack)
1166 strlcpy(ws->stacker, "[-]", sizeof ws->stacker);
1170 custom_region(char *val)
1172 unsigned int sidx, x, y, w, h;
1174 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1175 errx(1, "invalid custom region, "
1176 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
1177 if (sidx < 1 || sidx > ScreenCount(display))
1178 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1179 sidx, ScreenCount(display));
1183 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
1185 if (x > DisplayWidth(display, sidx) ||
1186 y > DisplayHeight(display, sidx) ||
1187 w + x > DisplayWidth(display, sidx) ||
1188 h + y > DisplayHeight(display, sidx)) {
1189 fprintf(stderr, "ignoring region %ux%u+%u+%u "
1190 "- not within screen boundaries "
1191 "(%ux%u)\n", w, h, x, y,
1192 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1196 new_region(&screens[sidx], x, y, w, h);
1200 socket_setnonblock(int fd)
1204 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1205 err(1, "fcntl F_GETFL");
1206 flags |= O_NONBLOCK;
1207 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1208 err(1, "fcntl F_SETFL");
1212 bar_print(struct swm_region *r, char *s)
1214 XClearWindow(display, r->bar_window);
1215 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
1216 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
1221 bar_extra_stop(void)
1225 bzero(bar_pipe, sizeof bar_pipe);
1228 kill(bar_pid, SIGTERM);
1231 strlcpy(bar_ext, "", sizeof bar_ext);
1236 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1238 int do_class, do_name;
1240 XClassHint *xch = NULL;
1242 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
1243 cur_focus != NULL) {
1244 if ((xch = XAllocClassHint()) == NULL)
1246 status = XGetClassHint(display, cur_focus->id, xch);
1247 if (status == BadWindow || status == BadAlloc)
1249 do_class = (title_class_enabled && xch->res_class != NULL);
1250 do_name = (title_name_enabled && xch->res_name != NULL);
1252 strlcat(s, xch->res_class, sz);
1253 if (do_class && do_name)
1254 strlcat(s, ":", sz);
1256 strlcat(s, xch->res_name, sz);
1257 strlcat(s, " ", sz);
1265 bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1269 if (window_name_enabled && cur_focus != NULL) {
1270 XFetchName(display, cur_focus->id, &title);
1272 if (cur_focus->floating)
1273 strlcat(s, "(f) ", sz);
1274 strlcat(s, title, sz);
1275 strlcat(s, " ", sz);
1281 int urgent[SWM_WS_MAX];
1283 bar_urgent(char *s, ssize_t sz)
1285 XWMHints *wmh = NULL;
1290 if (urgent_enabled == 0)
1293 for (i = 0; i < SWM_WS_MAX; i++)
1296 for (i = 0; i < ScreenCount(display); i++)
1297 for (j = 0; j < SWM_WS_MAX; j++)
1298 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1299 wmh = XGetWMHints(display, win->id);
1303 if (wmh->flags & XUrgencyHint)
1308 strlcat(s, "* ", sz);
1309 for (i = 0; i < SWM_WS_MAX; i++) {
1311 snprintf(b, sizeof b, "%d ", i + 1);
1313 snprintf(b, sizeof b, "- ");
1316 strlcat(s, "* ", sz);
1324 struct swm_region *r;
1327 char s[SWM_BAR_MAX];
1328 char cn[SWM_BAR_MAX];
1329 char loc[SWM_BAR_MAX];
1330 char *b, *stack = "";
1332 if (bar_enabled == 0)
1334 if (bar_extra && bar_extra_running) {
1335 /* ignore short reads; it'll correct itself */
1336 while ((b = fgetln(stdin, &len)) != NULL)
1337 if (b && b[len - 1] == '\n') {
1339 strlcpy(bar_ext, b, sizeof bar_ext);
1341 if (b == NULL && errno != EAGAIN) {
1342 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
1343 errno, strerror(errno));
1347 strlcpy(bar_ext, "", sizeof bar_ext);
1349 if (clock_enabled == 0)
1350 strlcpy(s, "", sizeof s);
1353 localtime_r(&tmt, &tm);
1354 strftime(s, sizeof s, clock_format, &tm);
1355 strlcat(s, " ", sizeof s);
1358 for (i = 0; i < ScreenCount(display); i++) {
1360 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1361 strlcpy(cn, "", sizeof cn);
1363 bar_urgent(cn, sizeof cn);
1364 bar_class_name(cn, sizeof cn, r->ws->focus);
1365 bar_window_name(cn, sizeof cn, r->ws->focus);
1368 stack = r->ws->stacker;
1370 snprintf(loc, sizeof loc, "%d:%d %s %s%s %s %s",
1371 x++, r->ws->idx + 1, stack, s, cn, bar_ext, bar_vertext);
1385 bar_toggle(struct swm_region *r, union arg *args)
1387 struct swm_region *tmpr;
1388 int i, sc = ScreenCount(display);
1390 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
1393 for (i = 0; i < sc; i++)
1394 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1395 XUnmapWindow(display, tmpr->bar_window);
1397 for (i = 0; i < sc; i++)
1398 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1399 XMapRaised(display, tmpr->bar_window);
1401 bar_enabled = !bar_enabled;
1404 /* must be after stack */
1411 XSetWindowAttributes wa;
1412 struct swm_region *r;
1415 /* do this here because the conf file is in memory */
1416 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1417 /* launch external status app */
1418 bar_extra_running = 1;
1419 if (pipe(bar_pipe) == -1)
1420 err(1, "pipe error");
1421 socket_setnonblock(bar_pipe[0]);
1422 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1423 if (dup2(bar_pipe[0], 0) == -1)
1425 if (dup2(bar_pipe[1], 1) == -1)
1427 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1428 err(1, "could not disable SIGPIPE");
1429 switch (bar_pid = fork()) {
1431 err(1, "cannot fork");
1435 execvp(bar_argv[0], bar_argv);
1436 err(1, "%s external app failed", bar_argv[0]);
1438 default: /* parent */
1444 bzero(&wa, sizeof wa);
1445 for (i = 0; i < ScreenCount(display); i++)
1446 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1448 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1449 wa.background_pixel =
1450 screens[i].c[SWM_S_COLOR_BAR].color;
1451 XChangeWindowAttributes(display, r->bar_window,
1452 CWBackPixel | CWBorderPixel, &wa);
1458 bar_setup(struct swm_region *r)
1463 XFreeFont(display, bar_fs);
1467 for (i = 0; bar_fonts[i] != NULL; i++) {
1468 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
1474 if (bar_fonts[i] == NULL)
1475 errx(1, "couldn't load font");
1477 errx(1, "couldn't create font structure");
1479 bar_height = bar_fs->ascent + bar_fs->descent + 1 +
1480 2 * bar_border_width;
1482 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1484 r->bar_window = XCreateSimpleWindow(display,
1485 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width,
1486 bar_height - 2 * bar_border_width,
1487 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1488 r->s->c[SWM_S_COLOR_BAR].color);
1489 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
1490 XSetFont(display, bar_gc, bar_fs->fid);
1491 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1493 XMapRaised(display, r->bar_window);
1494 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
1496 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1497 err(1, "could not install bar_signal");
1502 drain_enter_notify(void)
1507 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1510 DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained %d\n", i);
1514 set_win_state(struct ws_win *win, long state)
1516 long data[] = {state, None};
1518 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1523 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1524 (unsigned char *)data, 2);
1531 unsigned char *p = NULL;
1534 if (!get_property(w, astate, 2L, astate, &n, &p))
1537 result = *((long *)p);
1543 version(struct swm_region *r, union arg *args)
1545 bar_version = !bar_version;
1547 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s Build: %s",
1548 SCROTWM_VERSION, buildstr);
1550 strlcpy(bar_vertext, "", sizeof bar_vertext);
1555 client_msg(struct ws_win *win, Atom a)
1557 XClientMessageEvent cm;
1562 bzero(&cm, sizeof cm);
1563 cm.type = ClientMessage;
1564 cm.window = win->id;
1565 cm.message_type = aprot;
1568 cm.data.l[1] = CurrentTime;
1569 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1573 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1581 DNPRINTF(SWM_D_MISC,
1582 "config_win: win %lu x %d y %d w %d h %d\n",
1583 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1585 ce.type = ConfigureNotify;
1586 ce.display = display;
1588 ce.window = win->id;
1591 ce.width = win->g.w;
1592 ce.height = win->g.h;
1593 ce.border_width = border_width;
1595 ce.override_redirect = False;
1597 DNPRINTF(SWM_D_MISC,
1598 "config_win: ev win %lu x %d y %d w %d h %d\n",
1599 ev->window, ev->x, ev->y, ev->width, ev->height);
1600 ce.type = ConfigureNotify;
1601 ce.display = ev->display;
1602 ce.event = ev->window;
1603 ce.window = ev->window;
1606 ce.width = ev->width;
1607 ce.height = ev->height;
1608 ce.border_width = ev->border_width;
1609 ce.above = ev->above;
1610 ce.override_redirect = False;
1613 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1617 count_win(struct workspace *ws, int count_transient)
1622 TAILQ_FOREACH(win, &ws->winlist, entry) {
1623 if (count_transient == 0 && win->floating)
1625 if (count_transient == 0 && win->transient)
1631 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1637 quit(struct swm_region *r, union arg *args)
1639 DNPRINTF(SWM_D_MISC, "quit\n");
1644 unmap_window(struct ws_win *win)
1649 /* don't unmap again */
1650 if (getstate(win->id) == IconicState)
1653 set_win_state(win, IconicState);
1655 XUnmapWindow(display, win->id);
1656 XSetWindowBorder(display, win->id,
1657 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1666 for (i = 0; i < ScreenCount(display); i++)
1667 for (j = 0; j < SWM_WS_MAX; j++)
1668 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1673 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1680 event.display = display; /* Ignored, but what the hell */
1681 event.window = win->id;
1682 event.root = win->s->root;
1683 event.subwindow = None;
1684 event.time = CurrentTime;
1689 event.same_screen = True;
1690 event.keycode = XKeysymToKeycode(display, keysym);
1691 event.state = modifiers;
1693 event.type = KeyPress;
1694 XSendEvent(event.display, event.window, True,
1695 KeyPressMask, (XEvent *)&event);
1697 event.type = KeyRelease;
1698 XSendEvent(event.display, event.window, True,
1699 KeyPressMask, (XEvent *)&event);
1704 restart(struct swm_region *r, union arg *args)
1706 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1708 /* disable alarm because the following code may not be interrupted */
1710 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1711 errx(1, "can't disable alarm");
1716 XCloseDisplay(display);
1717 execvp(start_argv[0], start_argv);
1718 fprintf(stderr, "execvp failed\n");
1724 root_to_region(Window root)
1726 struct swm_region *r = NULL;
1728 int i, x, y, wx, wy;
1731 for (i = 0; i < ScreenCount(display); i++)
1732 if (screens[i].root == root)
1735 if (XQueryPointer(display, screens[i].root,
1736 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1737 /* choose a region based on pointer location */
1738 TAILQ_FOREACH(r, &screens[i].rl, entry)
1739 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1740 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1745 r = TAILQ_FIRST(&screens[i].rl);
1751 find_unmanaged_window(Window id)
1756 for (i = 0; i < ScreenCount(display); i++)
1757 for (j = 0; j < SWM_WS_MAX; j++)
1758 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1766 find_window(Window id)
1769 Window wrr, wpr, *wcr = NULL;
1773 for (i = 0; i < ScreenCount(display); i++)
1774 for (j = 0; j < SWM_WS_MAX; j++)
1775 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1779 /* if we were looking for the parent return that window instead */
1780 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1785 /* ignore not found and root */
1786 if (wpr == 0 || wrr == wpr)
1789 /* look for parent */
1790 for (i = 0; i < ScreenCount(display); i++)
1791 for (j = 0; j < SWM_WS_MAX; j++)
1792 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1800 spawn(int ws_idx, union arg *args, int close_fd)
1805 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1808 close(ConnectionNumber(display));
1810 setenv("LD_PRELOAD", SWM_LIB, 1);
1812 if (asprintf(&ret, "%d", ws_idx) == -1) {
1816 setenv("_SWM_WS", ret, 1);
1820 if (asprintf(&ret, "%d", getpid()) == -1) {
1824 setenv("_SWM_PID", ret, 1);
1828 if (setsid() == -1) {
1835 * close stdin and stdout to prevent interaction between apps
1836 * and the baraction script
1837 * leave stderr open to record errors
1839 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
1843 dup2(fd, STDIN_FILENO);
1844 dup2(fd, STDOUT_FILENO);
1849 execvp(args->argv[0], args->argv);
1856 spawnterm(struct swm_region *r, union arg *args)
1858 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1862 setenv("_SWM_XTERM_FONTADJ", "", 1);
1863 spawn(r->ws->idx, args, 1);
1868 kill_refs(struct ws_win *win)
1871 struct swm_region *r;
1872 struct workspace *ws;
1877 for (i = 0; i < ScreenCount(display); i++)
1878 TAILQ_FOREACH(r, &screens[i].rl, entry)
1879 for (x = 0; x < SWM_WS_MAX; x++) {
1881 if (win == ws->focus)
1883 if (win == ws->focus_prev)
1884 ws->focus_prev = NULL;
1889 validate_win(struct ws_win *testwin)
1892 struct workspace *ws;
1893 struct swm_region *r;
1894 int i, x, foundit = 0;
1896 if (testwin == NULL)
1899 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1900 TAILQ_FOREACH(r, &screens[i].rl, entry)
1901 for (x = 0; x < SWM_WS_MAX; x++) {
1903 TAILQ_FOREACH(win, &ws->winlist, entry)
1911 validate_ws(struct workspace *testws)
1913 struct swm_region *r;
1914 struct workspace *ws;
1917 /* validate all ws */
1918 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1919 TAILQ_FOREACH(r, &screens[i].rl, entry)
1920 for (x = 0; x < SWM_WS_MAX; x++) {
1929 unfocus_win(struct ws_win *win)
1934 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1938 if (win->ws == NULL)
1941 if (validate_ws(win->ws))
1942 return; /* XXX this gets hit with thunderbird, needs fixing */
1944 if (win->ws->r == NULL)
1947 if (validate_win(win)) {
1952 if (win->ws->focus == win) {
1953 win->ws->focus = NULL;
1954 win->ws->focus_prev = win;
1957 if (validate_win(win->ws->focus)) {
1958 kill_refs(win->ws->focus);
1959 win->ws->focus = NULL;
1961 if (validate_win(win->ws->focus_prev)) {
1962 kill_refs(win->ws->focus_prev);
1963 win->ws->focus_prev = NULL;
1966 /* drain all previous unfocus events */
1967 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
1970 grabbuttons(win, 0);
1971 XSetWindowBorder(display, win->id,
1972 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1974 XChangeProperty(display, win->s->root,
1975 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1976 PropModeReplace, (unsigned char *)&none,1);
1985 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
1987 for (i = 0; i < ScreenCount(display); i++)
1988 for (j = 0; j < SWM_WS_MAX; j++)
1989 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1994 focus_win(struct ws_win *win)
1999 struct ws_win *cfw = NULL;
2002 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
2006 if (win->ws == NULL)
2009 if (validate_ws(win->ws))
2010 return; /* XXX this gets hit with thunderbird, needs fixing */
2012 if (validate_win(win)) {
2017 if (validate_win(win)) {
2022 XGetInputFocus(display, &cur_focus, &rr);
2023 if ((cfw = find_window(cur_focus)) != NULL)
2026 /* use larger hammer since the window was killed somehow */
2027 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2028 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2029 XSetWindowBorder(display, cfw->id,
2030 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2033 win->ws->focus = win;
2035 if (win->ws->r != NULL) {
2036 /* drain all previous focus events */
2037 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
2041 XSetInputFocus(display, win->id,
2042 RevertToParent, CurrentTime);
2043 grabbuttons(win, 1);
2044 XSetWindowBorder(display, win->id,
2045 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2046 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2047 win->ws->always_raise)
2048 XMapRaised(display, win->id);
2050 XChangeProperty(display, win->s->root,
2051 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2052 PropModeReplace, (unsigned char *)&win->id,1);
2055 if (window_name_enabled)
2060 switchws(struct swm_region *r, union arg *args)
2062 int wsid = args->id, unmap_old = 0;
2063 struct swm_region *this_r, *other_r;
2065 struct workspace *new_ws, *old_ws;
2072 old_ws = this_r->ws;
2073 new_ws = &this_r->s->ws[wsid];
2075 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
2076 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
2079 if (new_ws == NULL || old_ws == NULL)
2081 if (new_ws == old_ws)
2084 other_r = new_ws->r;
2085 if (other_r == NULL) {
2086 /* the other workspace is hidden, hide this one */
2090 /* the other ws is visible in another region, exchange them */
2091 other_r->ws_prior = new_ws;
2092 other_r->ws = old_ws;
2093 old_ws->r = other_r;
2095 this_r->ws_prior = old_ws;
2096 this_r->ws = new_ws;
2099 /* this is needed so that we can click on a window after a restart */
2103 a.id = SWM_ARG_ID_FOCUSCUR;
2104 focus(new_ws->r, &a);
2108 /* unmap old windows */
2110 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2113 if (focus_mode == SWM_FOCUS_DEFAULT)
2114 drain_enter_notify();
2118 cyclews(struct swm_region *r, union arg *args)
2121 struct swm_screen *s = r->s;
2123 DNPRINTF(SWM_D_WS, "cyclews id %d "
2124 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2125 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2130 case SWM_ARG_ID_CYCLEWS_UP:
2131 if (a.id < SWM_WS_MAX - 1)
2136 case SWM_ARG_ID_CYCLEWS_DOWN:
2140 a.id = SWM_WS_MAX - 1;
2146 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
2148 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2152 } while (a.id != r->ws->idx);
2156 priorws(struct swm_region *r, union arg *args)
2160 DNPRINTF(SWM_D_WS, "priorws id %d "
2161 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2162 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2164 if (r->ws_prior == NULL)
2167 a.id = r->ws_prior->idx;
2172 cyclescr(struct swm_region *r, union arg *args)
2174 struct swm_region *rr = NULL;
2178 /* do nothing if we don't have more than one screen */
2179 if (!(ScreenCount(display) > 1 || outputs > 1))
2184 case SWM_ARG_ID_CYCLESC_UP:
2185 rr = TAILQ_NEXT(r, entry);
2187 rr = TAILQ_FIRST(&screens[i].rl);
2189 case SWM_ARG_ID_CYCLESC_DOWN:
2190 rr = TAILQ_PREV(r, swm_region_list, entry);
2192 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2200 /* move mouse to region */
2202 y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
2203 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2205 a.id = SWM_ARG_ID_FOCUSCUR;
2208 if (rr->ws->focus) {
2209 /* move to focus window */
2210 x = rr->ws->focus->g.x + 1;
2211 y = rr->ws->focus->g.y + 1;
2212 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2217 sort_windows(struct ws_win_list *wl)
2219 struct ws_win *win, *parent, *nxt;
2224 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2225 nxt = TAILQ_NEXT(win, entry);
2226 if (win->transient) {
2227 parent = find_window(win->transient);
2228 if (parent == NULL) {
2229 fprintf(stderr, "not possible bug\n");
2232 TAILQ_REMOVE(wl, win, entry);
2233 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2240 swapwin(struct swm_region *r, union arg *args)
2242 struct ws_win *target, *source;
2243 struct ws_win *cur_focus;
2244 struct ws_win_list *wl;
2247 DNPRINTF(SWM_D_WS, "swapwin id %d "
2248 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
2249 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2251 cur_focus = r->ws->focus;
2252 if (cur_focus == NULL)
2256 wl = &source->ws->winlist;
2259 case SWM_ARG_ID_SWAPPREV:
2260 if (source->transient)
2261 source = find_window(source->transient);
2262 target = TAILQ_PREV(source, ws_win_list, entry);
2263 if (target && target->transient)
2264 target = find_window(target->transient);
2265 TAILQ_REMOVE(wl, source, entry);
2267 TAILQ_INSERT_TAIL(wl, source, entry);
2269 TAILQ_INSERT_BEFORE(target, source, entry);
2271 case SWM_ARG_ID_SWAPNEXT:
2272 target = TAILQ_NEXT(source, entry);
2273 /* move the parent and let the sort handle the move */
2274 if (source->transient)
2275 source = find_window(source->transient);
2276 TAILQ_REMOVE(wl, source, entry);
2278 TAILQ_INSERT_HEAD(wl, source, entry);
2280 TAILQ_INSERT_AFTER(wl, target, source, entry);
2282 case SWM_ARG_ID_SWAPMAIN:
2283 target = TAILQ_FIRST(wl);
2284 if (target == source) {
2285 if (source->ws->focus_prev != NULL &&
2286 source->ws->focus_prev != target)
2288 source = source->ws->focus_prev;
2292 if (target == NULL || source == NULL)
2294 source->ws->focus_prev = target;
2295 TAILQ_REMOVE(wl, target, entry);
2296 TAILQ_INSERT_BEFORE(source, target, entry);
2297 TAILQ_REMOVE(wl, source, entry);
2298 TAILQ_INSERT_HEAD(wl, source, entry);
2300 case SWM_ARG_ID_MOVELAST:
2301 TAILQ_REMOVE(wl, source, entry);
2302 TAILQ_INSERT_TAIL(wl, source, entry);
2305 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
2315 focus_prev(struct ws_win *win)
2317 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2318 struct ws_win *cur_focus = NULL;
2319 struct ws_win_list *wl = NULL;
2320 struct workspace *ws = NULL;
2322 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
2324 if (!(win && win->ws))
2329 cur_focus = ws->focus;
2330 winlostfocus = cur_focus;
2332 /* pickle, just focus on whatever */
2333 if (cur_focus == NULL) {
2334 /* use prev_focus if valid */
2335 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2336 find_window(WINID(ws->focus_prev)))
2337 winfocus = ws->focus_prev;
2338 if (winfocus == NULL)
2339 winfocus = TAILQ_FIRST(wl);
2343 /* if transient focus on parent */
2344 if (cur_focus->transient) {
2345 winfocus = find_window(cur_focus->transient);
2349 /* if in max_stack try harder */
2350 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2351 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2352 if (cur_focus != ws->focus_prev)
2353 winfocus = ws->focus_prev;
2354 else if (cur_focus != ws->focus)
2355 winfocus = ws->focus;
2357 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2362 if (cur_focus == win)
2363 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2364 if (winfocus == NULL)
2365 winfocus = TAILQ_LAST(wl, ws_win_list);
2366 if (winfocus == NULL || winfocus == win)
2367 winfocus = TAILQ_NEXT(cur_focus, entry);
2369 if (winfocus == winlostfocus || winfocus == NULL)
2372 focus_magic(winfocus);
2376 focus(struct swm_region *r, union arg *args)
2378 struct ws_win *winfocus = NULL, *winlostfocus = NULL, *head;
2379 struct ws_win *cur_focus = NULL;
2380 struct ws_win_list *wl = NULL;
2381 struct workspace *ws = NULL;
2386 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
2388 /* treat FOCUS_CUR special */
2389 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2390 if (r->ws->focus && r->ws->focus->iconic == 0)
2391 winfocus = r->ws->focus;
2392 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2393 winfocus = r->ws->focus_prev;
2395 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2396 if (winfocus->iconic == 0)
2399 focus_magic(winfocus);
2403 if ((cur_focus = r->ws->focus) == NULL)
2408 winlostfocus = cur_focus;
2411 case SWM_ARG_ID_FOCUSPREV:
2412 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2414 head = TAILQ_LAST(wl, ws_win_list);
2416 if (WINID(winfocus) == cur_focus->transient) {
2417 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2419 head = TAILQ_LAST(wl, ws_win_list);
2424 if (winfocus && winfocus->iconic) {
2425 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
2426 if (winfocus->iconic == 0)
2431 case SWM_ARG_ID_FOCUSNEXT:
2432 head = TAILQ_NEXT(cur_focus, entry);
2434 head = TAILQ_FIRST(wl);
2438 if (winfocus && winfocus->iconic) {
2439 TAILQ_FOREACH(winfocus, wl, entry)
2440 if (winfocus->iconic == 0)
2445 case SWM_ARG_ID_FOCUSMAIN:
2446 winfocus = TAILQ_FIRST(wl);
2447 if (winfocus == cur_focus)
2448 winfocus = cur_focus->ws->focus_prev;
2454 if (winfocus == winlostfocus || winfocus == NULL)
2457 focus_magic(winfocus);
2461 cycle_layout(struct swm_region *r, union arg *args)
2463 struct workspace *ws = r->ws;
2464 struct ws_win *winfocus;
2467 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2469 winfocus = ws->focus;
2472 if (ws->cur_layout->l_stack == NULL)
2473 ws->cur_layout = &layouts[0];
2476 if (focus_mode == SWM_FOCUS_DEFAULT)
2477 drain_enter_notify();
2478 a.id = SWM_ARG_ID_FOCUSCUR;
2484 stack_config(struct swm_region *r, union arg *args)
2486 struct workspace *ws = r->ws;
2488 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
2491 if (ws->cur_layout->l_config != NULL)
2492 ws->cur_layout->l_config(ws, args->id);
2494 if (args->id != SWM_ARG_ID_STACKINIT)
2501 struct swm_geometry g;
2502 struct swm_region *r;
2505 DNPRINTF(SWM_D_STACK, "stack\n");
2507 for (i = 0; i < ScreenCount(display); i++) {
2509 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2510 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
2511 "(screen %d, region %d)\n", r->ws->idx, i, j++);
2513 /* start with screen geometry, adjust for bar */
2515 g.w -= 2 * border_width;
2516 g.h -= 2 * border_width;
2522 r->ws->cur_layout->l_stack(r->ws, &g);
2523 r->ws->cur_layout->l_string(r->ws);
2524 /* save r so we can track region changes */
2531 if (focus_mode == SWM_FOCUS_DEFAULT)
2532 drain_enter_notify();
2536 store_float_geom(struct ws_win *win, struct swm_region *r)
2538 /* retain window geom and region geom */
2539 win->g_float.x = win->g.x;
2540 win->g_float.y = win->g.y;
2541 win->g_float.w = win->g.w;
2542 win->g_float.h = win->g.h;
2543 win->rg_float.x = r->g.x;
2544 win->rg_float.y = r->g.y;
2545 win->rg_float.w = r->g.w;
2546 win->rg_float.h = r->g.h;
2547 win->g_floatvalid = 1;
2551 stack_floater(struct ws_win *win, struct swm_region *r)
2559 bzero(&wc, sizeof wc);
2560 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2563 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2564 * geom on ws switches or return from max mode
2567 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2568 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2570 * use stored g and rg to set relative position and size
2571 * as in old region or before max stack mode
2573 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2574 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2575 win->g.w = win->g_float.w;
2576 win->g.h = win->g_float.h;
2577 win->g_floatvalid = 0;
2580 win->floatmaxed = 0;
2582 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
2583 (win->g.h >= HEIGHT(r)))
2584 wc.border_width = 0;
2586 wc.border_width = border_width;
2587 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2588 win->g.w = (double)WIDTH(r) * dialog_ratio;
2589 win->g.h = (double)HEIGHT(r) * dialog_ratio;
2594 * floaters and transients are auto-centred unless moved
2597 win->g.x = r->g.x + (WIDTH(r) - win->g.w) /
2598 2 - wc.border_width;
2599 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) /
2600 2 - wc.border_width;
2603 /* win can be outside r if new r smaller than old r */
2604 /* Ensure top left corner inside r (move probs otherwise) */
2605 if (win->g.x < r->g.x - wc.border_width)
2606 win->g.x = r->g.x - wc.border_width;
2607 if (win->g.x > r->g.x + r->g.w - 1)
2608 win->g.x = (win->g.w > r->g.w) ? r->g.x :
2609 (r->g.x + r->g.w - win->g.w - 2 * wc.border_width);
2610 if (win->g.y < r->g.y - wc.border_width)
2611 win->g.y = r->g.y - wc.border_width;
2612 if (win->g.y > r->g.y + r->g.h - 1)
2613 win->g.y = (win->g.h > r->g.h) ? r->g.y :
2614 (r->g.y + r->g.h - win->g.h - 2 * wc.border_width);
2618 wc.width = win->g.w;
2619 wc.height = win->g.h;
2622 * Retain floater and transient geometry for correct positioning
2623 * when ws changes region
2625 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2626 store_float_geom(win, r);
2628 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
2629 win->id, wc.x, wc.y, wc.width, wc.height);
2631 XConfigureWindow(display, win->id, mask, &wc);
2635 * Send keystrokes to terminal to decrease/increase the font size as the
2636 * window size changes.
2639 adjust_font(struct ws_win *win)
2641 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2642 win->floating || win->transient)
2645 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2646 win->g.w / win->sh.width_inc < term_width &&
2647 win->font_steps < SWM_MAX_FONT_STEPS) {
2648 win->font_size_boundary[win->font_steps] =
2649 (win->sh.width_inc * term_width) + win->sh.base_width;
2652 win->last_inc = win->sh.width_inc;
2653 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2654 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2655 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
2658 win->last_inc = win->sh.width_inc;
2659 fake_keypress(win, XK_KP_Add, ShiftMask);
2663 #define SWAPXY(g) do { \
2665 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2666 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2669 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2672 XWindowAttributes wa;
2673 struct swm_geometry win_g, r_g = *g;
2674 struct ws_win *win, *fs_win = 0;
2675 int i, j, s, stacks;
2676 int w_inc = 1, h_inc, w_base = 1, h_base;
2677 int hrh, extra = 0, h_slice, last_h = 0;
2678 int split, colno, winno, mwin, msize, mscale;
2679 int remain, missing, v_slice, reconfigure;
2682 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2683 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2685 winno = count_win(ws, 0);
2686 if (winno == 0 && count_win(ws, 1) == 0)
2689 TAILQ_FOREACH(win, &ws->winlist, entry)
2690 if (win->transient == 0 && win->floating == 0
2691 && win->iconic == 0)
2698 w_inc = win->sh.width_inc;
2699 w_base = win->sh.base_width;
2700 mwin = ws->l_state.horizontal_mwin;
2701 mscale = ws->l_state.horizontal_msize;
2702 stacks = ws->l_state.horizontal_stacks;
2705 w_inc = win->sh.height_inc;
2706 w_base = win->sh.base_height;
2707 mwin = ws->l_state.vertical_mwin;
2708 mscale = ws->l_state.vertical_msize;
2709 stacks = ws->l_state.vertical_stacks;
2713 if (stacks > winno - mwin)
2714 stacks = winno - mwin;
2718 h_slice = r_g.h / SWM_H_SLICE;
2719 if (mwin && winno > mwin) {
2720 v_slice = r_g.w / SWM_V_SLICE;
2724 win_g.w = v_slice * mscale;
2726 if (w_inc > 1 && w_inc < v_slice) {
2727 /* adjust for window's requested size increment */
2728 remain = (win_g.w - w_base) % w_inc;
2729 missing = w_inc - remain;
2736 win_g.x += r_g.w - msize;
2739 colno = split = winno / stacks;
2740 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
2741 2 * border_width) / stacks);
2743 hrh = r_g.h / colno;
2744 extra = r_g.h - (colno * hrh);
2745 win_g.h = hrh - 2 * border_width;
2747 /* stack all the tiled windows */
2748 i = j = 0, s = stacks;
2749 TAILQ_FOREACH(win, &ws->winlist, entry) {
2750 if (win->transient != 0 || win->floating != 0)
2752 if (win->iconic != 0)
2755 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2760 if (split && i == split) {
2761 colno = (winno - mwin) / stacks;
2762 if (s <= (winno - mwin) % stacks)
2764 split = split + colno;
2765 hrh = (r_g.h / colno);
2766 extra = r_g.h - (colno * hrh);
2770 win_g.x += win_g.w + 2 * border_width;
2771 win_g.w = (r_g.w - msize -
2772 (stacks * 2 * border_width)) / stacks;
2774 win_g.w += (r_g.w - msize -
2775 (stacks * 2 * border_width)) % stacks;
2779 win_g.h = hrh - 2 * border_width;
2781 h_inc = win->sh.width_inc;
2782 h_base = win->sh.base_width;
2784 h_inc = win->sh.height_inc;
2785 h_base = win->sh.base_height;
2787 if (j == colno - 1) {
2788 win_g.h = hrh + extra;
2789 } else if (h_inc > 1 && h_inc < h_slice) {
2790 /* adjust for window's requested size increment */
2791 remain = (win_g.h - h_base) % h_inc;
2792 missing = h_inc - remain;
2794 if (missing <= extra || j == 0) {
2806 win_g.y += last_h + 2 * border_width;
2808 bzero(&wc, sizeof wc);
2809 if (disable_border && bar_enabled == 0 && winno == 1){
2810 wc.border_width = 0;
2811 win_g.w += 2 * border_width;
2812 win_g.h += 2 * border_width;
2814 wc.border_width = border_width;
2817 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2818 win->g.w != win_g.h || win->g.h != win_g.w) {
2820 win->g.x = wc.x = win_g.y;
2821 win->g.y = wc.y = win_g.x;
2822 win->g.w = wc.width = win_g.h;
2823 win->g.h = wc.height = win_g.w;
2826 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2827 win->g.w != win_g.w || win->g.h != win_g.h) {
2829 win->g.x = wc.x = win_g.x;
2830 win->g.y = wc.y = win_g.y;
2831 win->g.w = wc.width = win_g.w;
2832 win->g.h = wc.height = win_g.h;
2837 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2838 XConfigureWindow(display, win->id, mask, &wc);
2841 if (XGetWindowAttributes(display, win->id, &wa))
2842 if (wa.map_state == IsUnmapped)
2843 XMapRaised(display, win->id);
2851 /* now, stack all the floaters and transients */
2852 TAILQ_FOREACH(win, &ws->winlist, entry) {
2853 if (win->transient == 0 && win->floating == 0)
2855 if (win->iconic == 1)
2857 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2862 stack_floater(win, ws->r);
2863 XMapRaised(display, win->id);
2867 stack_floater(fs_win, ws->r);
2868 XMapRaised(display, fs_win->id);
2873 vertical_config(struct workspace *ws, int id)
2875 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2878 case SWM_ARG_ID_STACKRESET:
2879 case SWM_ARG_ID_STACKINIT:
2880 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2881 ws->l_state.vertical_mwin = 1;
2882 ws->l_state.vertical_stacks = 1;
2884 case SWM_ARG_ID_MASTERSHRINK:
2885 if (ws->l_state.vertical_msize > 1)
2886 ws->l_state.vertical_msize--;
2888 case SWM_ARG_ID_MASTERGROW:
2889 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2890 ws->l_state.vertical_msize++;
2892 case SWM_ARG_ID_MASTERADD:
2893 ws->l_state.vertical_mwin++;
2895 case SWM_ARG_ID_MASTERDEL:
2896 if (ws->l_state.vertical_mwin > 0)
2897 ws->l_state.vertical_mwin--;
2899 case SWM_ARG_ID_STACKINC:
2900 ws->l_state.vertical_stacks++;
2902 case SWM_ARG_ID_STACKDEC:
2903 if (ws->l_state.vertical_stacks > 1)
2904 ws->l_state.vertical_stacks--;
2912 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2914 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2916 stack_master(ws, g, 0, 0);
2920 horizontal_config(struct workspace *ws, int id)
2922 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2925 case SWM_ARG_ID_STACKRESET:
2926 case SWM_ARG_ID_STACKINIT:
2927 ws->l_state.horizontal_mwin = 1;
2928 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2929 ws->l_state.horizontal_stacks = 1;
2931 case SWM_ARG_ID_MASTERSHRINK:
2932 if (ws->l_state.horizontal_msize > 1)
2933 ws->l_state.horizontal_msize--;
2935 case SWM_ARG_ID_MASTERGROW:
2936 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2937 ws->l_state.horizontal_msize++;
2939 case SWM_ARG_ID_MASTERADD:
2940 ws->l_state.horizontal_mwin++;
2942 case SWM_ARG_ID_MASTERDEL:
2943 if (ws->l_state.horizontal_mwin > 0)
2944 ws->l_state.horizontal_mwin--;
2946 case SWM_ARG_ID_STACKINC:
2947 ws->l_state.horizontal_stacks++;
2949 case SWM_ARG_ID_STACKDEC:
2950 if (ws->l_state.horizontal_stacks > 1)
2951 ws->l_state.horizontal_stacks--;
2959 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2961 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
2963 stack_master(ws, g, 1, 0);
2966 /* fullscreen view */
2968 max_stack(struct workspace *ws, struct swm_geometry *g)
2971 struct swm_geometry gg = *g;
2972 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2976 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2981 winno = count_win(ws, 0);
2982 if (winno == 0 && count_win(ws, 1) == 0)
2985 TAILQ_FOREACH(win, &ws->winlist, entry) {
2986 if (win->transient) {
2988 parent = find_window(win->transient);
2992 if (win->floating && win->floatmaxed == 0 ) {
2994 * retain geometry for retrieval on exit from
2997 store_float_geom(win, ws->r);
2998 win->floatmaxed = 1;
3001 /* only reconfigure if necessary */
3002 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
3004 bzero(&wc, sizeof wc);
3005 win->g.x = wc.x = gg.x;
3006 win->g.y = wc.y = gg.y;
3008 wc.border_width = border_width;
3009 win->g.w = wc.width = gg.w;
3010 win->g.h = wc.height = gg.h;
3012 wc.border_width = 0;
3013 win->g.w = wc.width = gg.w + 2 * border_width;
3014 win->g.h = wc.height = gg.h + 2 * border_width;
3016 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3017 XConfigureWindow(display, win->id, mask, &wc);
3019 /* unmap only if we don't have multi screen */
3020 if (win != ws->focus)
3021 if (!(ScreenCount(display) > 1 || outputs > 1))
3025 /* put the last transient on top */
3028 XMapRaised(display, parent->id);
3029 stack_floater(wintrans, ws->r);
3030 focus_magic(wintrans);
3035 send_to_ws(struct swm_region *r, union arg *args)
3037 int wsid = args->id;
3038 struct ws_win *win = NULL, *parent;
3039 struct workspace *ws, *nws;
3040 Atom ws_idx_atom = 0;
3041 unsigned char ws_idx_str[SWM_PROPLEN];
3050 if (win->ws->idx == wsid)
3053 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
3056 nws = &win->s->ws[wsid];
3058 a.id = SWM_ARG_ID_FOCUSPREV;
3060 if (win->transient) {
3061 parent = find_window(win->transient);
3063 unmap_window(parent);
3064 TAILQ_REMOVE(&ws->winlist, parent, entry);
3065 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3070 TAILQ_REMOVE(&ws->winlist, win, entry);
3071 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3074 /* Try to update the window's workspace property */
3075 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3077 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3079 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3081 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3082 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3089 pressbutton(struct swm_region *r, union arg *args)
3091 XTestFakeButtonEvent(display, args->id, True, CurrentTime);
3092 XTestFakeButtonEvent(display, args->id, False, CurrentTime);
3096 raise_toggle(struct swm_region *r, union arg *args)
3098 if (r && r->ws == NULL)
3101 r->ws->always_raise = !r->ws->always_raise;
3103 /* bring floaters back to top */
3104 if (r->ws->always_raise == 0)
3109 iconify(struct swm_region *r, union arg *args)
3113 if (r->ws->focus == NULL)
3115 unmap_window(r->ws->focus);
3116 update_iconic(r->ws->focus, 1);
3118 if (focus_mode == SWM_FOCUS_DEFAULT)
3119 drain_enter_notify();
3120 r->ws->focus = NULL;
3121 a.id = SWM_ARG_ID_FOCUSCUR;
3126 get_win_name(Display *dpy, Window win, Atom wname, Atom stype,
3127 unsigned long *slen)
3130 unsigned long nitems, nbytes, nextra;
3131 unsigned char *prop = NULL;
3134 status = XGetWindowProperty(dpy, win, wname, 0L, 0L, False, stype,
3135 &rettype, &retfmt, &nitems, &nbytes, &prop);
3136 if (status != Success)
3140 status = XGetWindowProperty(dpy, win, wname, 0L, nbytes, False,
3141 stype, &rettype, &retfmt, &nitems, &nextra, &prop);
3142 if (status != Success) {
3146 if (rettype != stype) {
3155 uniconify(struct swm_region *r, union arg *args)
3159 unsigned char *name;
3163 DNPRINTF(SWM_D_MISC, "uniconify\n");
3165 if (r && r->ws == NULL)
3168 /* make sure we have anything to uniconify */
3169 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3170 if (win->ws == NULL)
3171 continue; /* should never happen */
3172 if (win->iconic == 0)
3181 spawn_select(r, args, "uniconify", &searchpid);
3183 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3186 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3187 if (win->ws == NULL)
3188 continue; /* should never happen */
3189 if (win->iconic == 0)
3192 name = get_win_name(display, win->id, a_wmname, a_string,
3196 fprintf(lfile, "%s.%lu\n", name, win->id);
3203 #define MAX_RESP_LEN 1024
3206 search_do_resp(void)
3210 unsigned char *name;
3214 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3219 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3220 fprintf(stderr, "search: calloc\n");
3224 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3226 fprintf(stderr, "search: read error: %s\n", strerror(errno));
3229 resp[rbytes] = '\0';
3232 DNPRINTF(SWM_D_MISC, "search_do_resp: resp %s\n", resp);
3233 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3234 if (win->iconic == 0)
3236 name = get_win_name(display, win->id, a_wmname, a_string, &len);
3239 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3244 if (strncmp(s, resp, len) == 0) {
3245 /* XXX this should be a callback to generalize */
3246 update_iconic(win, 0);
3257 wkill(struct swm_region *r, union arg *args)
3259 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
3261 if (r->ws->focus == NULL)
3264 if (args->id == SWM_ARG_ID_KILLWINDOW)
3265 XKillClient(display, r->ws->focus->id);
3267 if (r->ws->focus->can_delete)
3268 client_msg(r->ws->focus, adelete);
3273 floating_toggle_win(struct ws_win *win)
3275 struct swm_region *r;
3285 /* reject floating toggles in max stack mode */
3286 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3289 if (win->floating) {
3290 if (!win->floatmaxed) {
3291 /* retain position for refloat */
3292 store_float_geom(win, r);
3296 if (win->g_floatvalid) {
3297 /* refloat at last floating relative position */
3298 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
3299 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
3300 win->g.w = win->g_float.w;
3301 win->g.h = win->g_float.h;
3306 ewmh_update_actions(win);
3312 floating_toggle(struct swm_region *r, union arg *args)
3314 struct ws_win *win = r->ws->focus;
3320 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3321 _NET_WM_STATE_TOGGLE);
3324 if (focus_mode == SWM_FOCUS_DEFAULT)
3325 drain_enter_notify();
3327 if (win == win->ws->focus) {
3328 a.id = SWM_ARG_ID_FOCUSCUR;
3329 focus(win->ws->r, &a);
3334 resize_window(struct ws_win *win, int center)
3338 struct swm_region *r;
3340 r = root_to_region(win->wa.root);
3341 bzero(&wc, sizeof wc);
3342 mask = CWBorderWidth | CWWidth | CWHeight;
3343 wc.border_width = border_width;
3344 wc.width = win->g.w;
3345 wc.height = win->g.h;
3346 if (center == SWM_ARG_ID_CENTER) {
3347 wc.x = (WIDTH(r) - win->g.w) / 2 - border_width;
3348 wc.y = (HEIGHT(r) - win->g.h) / 2 - border_width;
3352 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
3353 win->id, wc.x, wc.y, wc.width, wc.height);
3355 XConfigureWindow(display, win->id, mask, &wc);
3358 #define SWM_RESIZE_STEPS (50)
3361 resize(struct ws_win *win, union arg *args)
3365 struct swm_region *r = NULL;
3367 int resize_step = 0;
3373 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
3374 win->id, win->floating, win->transient);
3376 if (!(win->transient != 0 || win->floating != 0))
3379 /* reject resizes in max mode for floaters (transient ok) */
3380 if (win->floatmaxed)
3384 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3390 case SWM_ARG_ID_WIDTHSHRINK:
3391 win->g.w -= SWM_RESIZE_STEPS;
3394 case SWM_ARG_ID_WIDTHGROW:
3395 win->g.w += SWM_RESIZE_STEPS;
3398 case SWM_ARG_ID_HEIGHTSHRINK:
3399 win->g.h -= SWM_RESIZE_STEPS;
3402 case SWM_ARG_ID_HEIGHTGROW:
3403 win->g.h += SWM_RESIZE_STEPS;
3410 resize_window(win, 0);
3411 store_float_geom(win,r);
3415 if (focus_mode == SWM_FOCUS_DEFAULT)
3416 drain_enter_notify();
3418 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3419 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3422 /* place pointer at bottom left corner or nearest point inside r */
3423 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
3424 relx = win->g.w - 1;
3426 relx = r->g.x + r->g.w - win->g.x - 1;
3428 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
3429 rely = win->g.h - 1;
3431 rely = r->g.y + r->g.h - win->g.y - 1;
3433 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
3435 XMaskEvent(display, MOUSEMASK | ExposureMask |
3436 SubstructureRedirectMask, &ev);
3438 case ConfigureRequest:
3441 handler[ev.type](&ev);
3444 /* do not allow resize outside of region */
3445 if ( ev.xmotion.x_root < r->g.x ||
3446 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3447 ev.xmotion.y_root < r->g.y ||
3448 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3451 if (ev.xmotion.x <= 1)
3453 if (ev.xmotion.y <= 1)
3455 win->g.w = ev.xmotion.x + 1;
3456 win->g.h = ev.xmotion.y + 1;
3458 /* not free, don't sync more than 120 times / second */
3459 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3460 time = ev.xmotion.time;
3461 XSync(display, False);
3462 resize_window(win, args->id);
3466 } while (ev.type != ButtonRelease);
3468 XSync(display, False);
3469 resize_window(win, args->id);
3471 store_float_geom(win,r);
3473 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
3475 XUngrabPointer(display, CurrentTime);
3478 drain_enter_notify();
3482 resize_step(struct swm_region *r, union arg *args)
3484 struct ws_win *win = NULL;
3486 if (r && r->ws && r->ws->focus)
3496 move_window(struct ws_win *win)
3500 struct swm_region *r;
3502 r = root_to_region(win->wa.root);
3503 bzero(&wc, sizeof wc);
3507 wc.border_width = border_width;
3509 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
3510 win->id, wc.x, wc.y, wc.width, wc.height);
3512 XConfigureWindow(display, win->id, mask, &wc);
3515 #define SWM_MOVE_STEPS (50)
3518 move(struct ws_win *win, union arg *args)
3523 struct swm_region *r = NULL;
3529 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
3530 win->id, win->floating, win->transient);
3532 /* in max_stack mode should only move transients */
3533 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
3537 if (win->floating == 0 && !win->transient) {
3538 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3541 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3548 case SWM_ARG_ID_MOVELEFT:
3549 win->g.x -= (SWM_MOVE_STEPS - border_width);
3552 case SWM_ARG_ID_MOVERIGHT:
3553 win->g.x += (SWM_MOVE_STEPS - border_width);
3556 case SWM_ARG_ID_MOVEUP:
3557 win->g.y -= (SWM_MOVE_STEPS - border_width);
3560 case SWM_ARG_ID_MOVEDOWN:
3561 win->g.y += (SWM_MOVE_STEPS - border_width);
3569 store_float_geom(win,r);
3574 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3575 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3577 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3579 XMaskEvent(display, MOUSEMASK | ExposureMask |
3580 SubstructureRedirectMask, &ev);
3582 case ConfigureRequest:
3585 handler[ev.type](&ev);
3588 /* don't allow to move window origin out of region */
3589 if ( ev.xmotion.x_root < r->g.x ||
3590 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3591 ev.xmotion.y_root < r->g.y ||
3592 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3595 win->g.x = ev.xmotion.x_root - border_width;
3596 win->g.y = ev.xmotion.y_root - border_width;
3598 /* not free, don't sync more than 120 times / second */
3599 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3600 time = ev.xmotion.time;
3601 XSync(display, False);
3606 } while (ev.type != ButtonRelease);
3608 XSync(display, False);
3611 store_float_geom(win,r);
3612 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3613 XUngrabPointer(display, CurrentTime);
3616 drain_enter_notify();
3620 move_step(struct swm_region *r, union arg *args)
3622 struct ws_win *win = NULL;
3624 if (r && r->ws && r->ws->focus)
3629 if (!(win->transient != 0 || win->floating != 0))
3636 /* user/key callable function IDs */
3703 kf_dumpwins, /* MUST BE LAST */
3707 /* key definitions */
3709 dummykeyfunc(struct swm_region *r, union arg *args)
3714 legacyfunc(struct swm_region *r, union arg *args)
3719 char name[SWM_FUNCNAME_LEN];
3720 void (*func)(struct swm_region *r, union arg *);
3722 } keyfuncs[kf_invalid + 1] = {
3723 /* name function argument */
3724 { "cycle_layout", cycle_layout, {0} },
3725 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
3726 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
3727 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
3728 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
3729 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
3730 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
3731 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
3732 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
3733 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
3734 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
3735 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
3736 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
3737 { "spawn_term", spawnterm, {.argv = spawn_term} },
3738 { "spawn_menu", legacyfunc, {0} },
3739 { "quit", quit, {0} },
3740 { "restart", restart, {0} },
3741 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
3742 { "ws_1", switchws, {.id = 0} },
3743 { "ws_2", switchws, {.id = 1} },
3744 { "ws_3", switchws, {.id = 2} },
3745 { "ws_4", switchws, {.id = 3} },
3746 { "ws_5", switchws, {.id = 4} },
3747 { "ws_6", switchws, {.id = 5} },
3748 { "ws_7", switchws, {.id = 6} },
3749 { "ws_8", switchws, {.id = 7} },
3750 { "ws_9", switchws, {.id = 8} },
3751 { "ws_10", switchws, {.id = 9} },
3752 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
3753 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
3754 { "ws_prior", priorws, {0} },
3755 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
3756 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
3757 { "mvws_1", send_to_ws, {.id = 0} },
3758 { "mvws_2", send_to_ws, {.id = 1} },
3759 { "mvws_3", send_to_ws, {.id = 2} },
3760 { "mvws_4", send_to_ws, {.id = 3} },
3761 { "mvws_5", send_to_ws, {.id = 4} },
3762 { "mvws_6", send_to_ws, {.id = 5} },
3763 { "mvws_7", send_to_ws, {.id = 6} },
3764 { "mvws_8", send_to_ws, {.id = 7} },
3765 { "mvws_9", send_to_ws, {.id = 8} },
3766 { "mvws_10", send_to_ws, {.id = 9} },
3767 { "bar_toggle", bar_toggle, {0} },
3768 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
3769 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
3770 { "screenshot_all", legacyfunc, {0} },
3771 { "screenshot_wind", legacyfunc, {0} },
3772 { "float_toggle", floating_toggle,{0} },
3773 { "version", version, {0} },
3774 { "spawn_lock", legacyfunc, {0} },
3775 { "spawn_initscr", legacyfunc, {0} },
3776 { "spawn_custom", dummykeyfunc, {0} },
3777 { "iconify", iconify, {0} },
3778 { "uniconify", uniconify, {0} },
3779 { "raise_toggle", raise_toggle, {0} },
3780 { "button2", pressbutton, {2} },
3781 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
3782 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
3783 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
3784 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
3785 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
3786 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
3787 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
3788 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
3789 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
3790 { "invalid key func", NULL, {0} },
3795 enum keyfuncid funcid;
3798 int keys_size = 0, keys_length = 0;
3799 struct key *keys = NULL;
3802 enum { client_click, root_click };
3804 unsigned int action;
3806 unsigned int button;
3807 void (*func)(struct ws_win *, union arg *);
3810 /* action key mouse button func args */
3811 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
3812 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
3813 { client_click, MODKEY, Button1, move, {0} },
3817 update_modkey(unsigned int mod)
3822 for (i = 0; i < keys_length; i++)
3823 if (keys[i].mod & ShiftMask)
3824 keys[i].mod = mod | ShiftMask;
3828 for (i = 0; i < LENGTH(buttons); i++)
3829 if (buttons[i].mask & ShiftMask)
3830 buttons[i].mask = mod | ShiftMask;
3832 buttons[i].mask = mod;
3842 int spawns_size = 0, spawns_length = 0;
3843 struct spawn_prog *spawns = NULL;
3846 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
3849 struct spawn_prog *prog = NULL;
3851 char *ap, **real_args;
3853 DNPRINTF(SWM_D_SPAWN, "spawn_expand %s\n", spawn_name);
3856 for (i = 0; i < spawns_length; i++) {
3857 if (!strcasecmp(spawn_name, spawns[i].name))
3861 fprintf(stderr, "spawn_custom: program %s not found\n",
3866 /* make room for expanded args */
3867 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
3868 err(1, "spawn_custom: calloc real_args");
3870 /* expand spawn_args into real_args */
3871 for (i = 0; i < prog->argc; i++) {
3873 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
3874 if (!strcasecmp(ap, "$bar_border")) {
3876 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
3878 err(1, "spawn_custom border color");
3879 } else if (!strcasecmp(ap, "$bar_color")) {
3881 strdup(r->s->c[SWM_S_COLOR_BAR].name))
3883 err(1, "spawn_custom bar color");
3884 } else if (!strcasecmp(ap, "$bar_font")) {
3885 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
3887 err(1, "spawn_custom bar fonts");
3888 } else if (!strcasecmp(ap, "$bar_font_color")) {
3890 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
3892 err(1, "spawn_custom color font");
3893 } else if (!strcasecmp(ap, "$color_focus")) {
3895 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
3897 err(1, "spawn_custom color focus");
3898 } else if (!strcasecmp(ap, "$color_unfocus")) {
3900 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
3902 err(1, "spawn_custom color unfocus");
3904 /* no match --> copy as is */
3905 if ((real_args[i] = strdup(ap)) == NULL)
3906 err(1, "spawn_custom strdup(ap)");
3908 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
3913 if ((swm_debug & SWM_D_SPAWN) != 0) {
3914 fprintf(stderr, "spawn_custom: result = ");
3915 for (i = 0; i < prog->argc; i++)
3916 fprintf(stderr, "\"%s\" ", real_args[i]);
3917 fprintf(stderr, "\n");
3920 *ret_args = real_args;
3921 return (prog->argc);
3925 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
3931 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3935 spawn(r->ws->idx, &a, 1);
3937 for (i = 0; i < spawn_argc; i++)
3943 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
3949 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3953 if (pipe(select_list_pipe) == -1)
3954 err(1, "pipe error");
3955 if (pipe(select_resp_pipe) == -1)
3956 err(1, "pipe error");
3958 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
3959 err(1, "could not disable SIGPIPE");
3960 switch (*pid = fork()) {
3962 err(1, "cannot fork");
3965 if (dup2(select_list_pipe[0], 0) == -1)
3967 if (dup2(select_resp_pipe[1], 1) == -1)
3969 close(select_list_pipe[1]);
3970 close(select_resp_pipe[0]);
3971 spawn(r->ws->idx, &a, 0);
3973 default: /* parent */
3974 close(select_list_pipe[0]);
3975 close(select_resp_pipe[1]);
3979 for (i = 0; i < spawn_argc; i++)
3985 setspawn(struct spawn_prog *prog)
3989 if (prog == NULL || prog->name == NULL)
3993 for (i = 0; i < spawns_length; i++) {
3994 if (!strcmp(spawns[i].name, prog->name)) {
3996 if (prog->argv == NULL) {
3998 DNPRINTF(SWM_D_SPAWN,
3999 "setspawn: delete #%d %s\n",
4001 free(spawns[i].name);
4002 for (j = 0; j < spawns[i].argc; j++)
4003 free(spawns[i].argv[j]);
4004 free(spawns[i].argv);
4005 j = spawns_length - 1;
4007 spawns[i] = spawns[j];
4012 DNPRINTF(SWM_D_SPAWN,
4013 "setspawn: replace #%d %s\n",
4015 free(spawns[i].name);
4016 for (j = 0; j < spawns[i].argc; j++)
4017 free(spawns[i].argv[j]);
4018 free(spawns[i].argv);
4021 /* found case handled */
4027 if (prog->argv == NULL) {
4029 "error: setspawn: cannot find program %s", prog->name);
4034 /* not found: add */
4035 if (spawns_size == 0 || spawns == NULL) {
4037 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
4038 spawns = malloc((size_t)spawns_size *
4039 sizeof(struct spawn_prog));
4040 if (spawns == NULL) {
4041 fprintf(stderr, "setspawn: malloc failed\n");
4045 } else if (spawns_length == spawns_size) {
4047 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
4048 spawns = realloc(spawns, (size_t)spawns_size *
4049 sizeof(struct spawn_prog));
4050 if (spawns == NULL) {
4051 fprintf(stderr, "setspawn: realloc failed\n");
4057 if (spawns_length < spawns_size) {
4058 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
4059 spawns_length, prog->name);
4060 i = spawns_length++;
4063 fprintf(stderr, "spawns array problem?\n");
4064 if (spawns == NULL) {
4065 fprintf(stderr, "spawns array is NULL!\n");
4073 setconfspawn(char *selector, char *value, int flags)
4075 struct spawn_prog *prog;
4076 char *vp, *cp, *word;
4078 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
4079 if ((prog = calloc(1, sizeof *prog)) == NULL)
4080 err(1, "setconfspawn: calloc prog");
4081 prog->name = strdup(selector);
4082 if (prog->name == NULL)
4083 err(1, "setconfspawn prog->name");
4084 if ((cp = vp = strdup(value)) == NULL)
4085 err(1, "setconfspawn: strdup(value) ");
4086 while ((word = strsep(&cp, " \t")) != NULL) {
4087 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
4089 cp += (long)strspn(cp, " \t");
4090 if (strlen(word) > 0) {
4092 if ((prog->argv = realloc(prog->argv,
4093 prog->argc * sizeof(char *))) == NULL)
4094 err(1, "setconfspawn: realloc");
4095 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
4096 err(1, "setconfspawn: strdup");
4103 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
4110 setconfspawn("term", "xterm", 0);
4111 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4112 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4113 setconfspawn("lock", "xlock", 0);
4114 setconfspawn("initscr", "initscreen.sh", 0);
4115 setconfspawn("menu", "dmenu_run"
4118 " -nf $bar_font_color"
4120 " -sf $bar_color", 0);
4121 setconfspawn("uniconify", "dmenu"
4125 " -nf $bar_font_color"
4127 " -sf $bar_color", 0);
4131 #define SWM_MODNAME_SIZE 32
4132 #define SWM_KEY_WS "\n+ \t"
4134 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
4138 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
4139 if (mod == NULL || ks == NULL) {
4140 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
4143 if (keystr == NULL || strlen(keystr) == 0) {
4144 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
4150 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
4151 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
4153 cp += (long)strspn(cp, SWM_KEY_WS);
4154 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
4156 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
4158 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
4160 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
4162 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
4164 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
4166 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
4167 *mod |= ControlMask;
4169 *ks = XStringToKeysym(name);
4170 XConvertCase(*ks, ks, &uks);
4171 if (ks == NoSymbol) {
4173 "parsekeys: invalid key %s\n",
4179 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
4184 strdupsafe(char *str)
4189 return (strdup(str));
4193 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
4197 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
4198 keyfuncs[kfid].name, spawn_name);
4200 for (i = 0; i < keys_length; i++) {
4201 if (keys[i].mod == mod && keys[i].keysym == ks) {
4202 if (kfid == kf_invalid) {
4205 "setkeybinding: delete #%d %s\n",
4206 i, keyfuncs[keys[i].funcid].name);
4207 free(keys[i].spawn_name);
4208 j = keys_length - 1;
4212 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4215 /* found: replace */
4217 "setkeybinding: replace #%d %s %s\n",
4218 i, keyfuncs[keys[i].funcid].name,
4220 free(keys[i].spawn_name);
4222 keys[i].keysym = ks;
4223 keys[i].funcid = kfid;
4224 keys[i].spawn_name = strdupsafe(spawn_name);
4225 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4230 if (kfid == kf_invalid) {
4232 "error: setkeybinding: cannot find mod/key combination");
4233 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4236 /* not found: add */
4237 if (keys_size == 0 || keys == NULL) {
4239 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
4240 keys = malloc((size_t)keys_size * sizeof(struct key));
4242 fprintf(stderr, "malloc failed\n");
4246 } else if (keys_length == keys_size) {
4248 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
4249 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
4251 fprintf(stderr, "realloc failed\n");
4256 if (keys_length < keys_size) {
4258 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
4259 j, keyfuncs[kfid].name, spawn_name);
4261 keys[j].keysym = ks;
4262 keys[j].funcid = kfid;
4263 keys[j].spawn_name = strdupsafe(spawn_name);
4265 fprintf(stderr, "keys array problem?\n");
4267 fprintf(stderr, "keys array problem\n");
4271 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4275 setconfbinding(char *selector, char *value, int flags)
4277 enum keyfuncid kfid;
4281 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
4282 if (selector == NULL) {
4283 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
4284 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4286 setkeybinding(mod, ks, kfid, NULL);
4291 /* search by key function name */
4292 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
4293 if (strncasecmp(selector, keyfuncs[kfid].name,
4294 SWM_FUNCNAME_LEN) == 0) {
4295 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4297 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4298 setkeybinding(mod, ks, kfid, NULL);
4304 /* search by custom spawn name */
4305 for (i = 0; i < spawns_length; i++) {
4306 if (strcasecmp(selector, spawns[i].name) == 0) {
4307 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4309 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4310 setkeybinding(mod, ks, kf_spawn_custom,
4317 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
4324 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
4325 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
4326 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
4327 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
4328 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
4329 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
4330 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
4331 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
4332 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
4333 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
4334 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
4335 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
4336 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
4337 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
4338 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
4339 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
4340 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
4341 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
4342 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
4343 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
4344 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
4345 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
4346 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
4347 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
4348 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
4349 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
4350 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
4351 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
4352 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
4353 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
4354 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
4355 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
4356 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
4357 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
4358 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
4359 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
4360 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
4361 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
4362 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
4363 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
4364 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
4365 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
4366 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
4367 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
4368 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
4369 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
4370 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
4371 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
4372 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
4373 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
4374 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
4375 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
4376 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
4377 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
4378 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
4379 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
4380 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
4381 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
4382 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
4383 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
4384 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
4385 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
4386 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
4387 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
4388 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
4389 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
4391 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
4400 /* clear all key bindings, if any */
4401 for (i = 0; i < keys_length; i++)
4402 free(keys[i].spawn_name);
4407 setkeymapping(char *selector, char *value, int flags)
4409 char keymapping_file[PATH_MAX];
4410 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
4411 if (value[0] == '~')
4412 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
4413 pwd->pw_dir, &value[1]);
4415 strlcpy(keymapping_file, value, sizeof keymapping_file);
4417 /* load new key bindings; if it fails, revert to default bindings */
4418 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
4422 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
4427 updatenumlockmask(void)
4430 XModifierKeymap *modmap;
4432 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
4434 modmap = XGetModifierMapping(display);
4435 for (i = 0; i < 8; i++)
4436 for (j = 0; j < modmap->max_keypermod; j++)
4437 if (modmap->modifiermap[i * modmap->max_keypermod + j]
4438 == XKeysymToKeycode(display, XK_Num_Lock))
4439 numlockmask = (1 << i);
4441 XFreeModifiermap(modmap);
4447 unsigned int i, j, k;
4449 unsigned int modifiers[] =
4450 { 0, LockMask, numlockmask, numlockmask | LockMask };
4452 DNPRINTF(SWM_D_MISC, "grabkeys\n");
4453 updatenumlockmask();
4455 for (k = 0; k < ScreenCount(display); k++) {
4456 if (TAILQ_EMPTY(&screens[k].rl))
4458 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
4459 for (i = 0; i < keys_length; i++) {
4460 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
4461 for (j = 0; j < LENGTH(modifiers); j++)
4462 XGrabKey(display, code,
4463 keys[i].mod | modifiers[j],
4464 screens[k].root, True,
4465 GrabModeAsync, GrabModeAsync);
4471 grabbuttons(struct ws_win *win, int focused)
4474 unsigned int modifiers[] =
4475 { 0, LockMask, numlockmask, numlockmask|LockMask };
4477 updatenumlockmask();
4478 XUngrabButton(display, AnyButton, AnyModifier, win->id);
4480 for (i = 0; i < LENGTH(buttons); i++)
4481 if (buttons[i].action == client_click)
4482 for (j = 0; j < LENGTH(modifiers); j++)
4483 XGrabButton(display, buttons[i].button,
4484 buttons[i].mask | modifiers[j],
4485 win->id, False, BUTTONMASK,
4486 GrabModeAsync, GrabModeSync, None,
4489 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
4490 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
4493 const char *quirkname[] = {
4494 "NONE", /* config string for "no value" */
4503 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
4504 #define SWM_Q_WS "\n|+ \t"
4506 parsequirks(char *qstr, unsigned long *quirk)
4516 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
4518 cp += (long)strspn(cp, SWM_Q_WS);
4519 for (i = 0; i < LENGTH(quirkname); i++) {
4520 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
4521 DNPRINTF(SWM_D_QUIRK,
4522 "parsequirks: %s\n", name);
4527 *quirk |= 1 << (i-1);
4531 if (i >= LENGTH(quirkname)) {
4532 DNPRINTF(SWM_D_QUIRK,
4533 "parsequirks: invalid quirk [%s]\n", name);
4541 setquirk(const char *class, const char *name, const int quirk)
4546 for (i = 0; i < quirks_length; i++) {
4547 if (!strcmp(quirks[i].class, class) &&
4548 !strcmp(quirks[i].name, name)) {
4551 DNPRINTF(SWM_D_QUIRK,
4552 "setquirk: delete #%d %s:%s\n",
4553 i, quirks[i].class, quirks[i].name);
4554 free(quirks[i].class);
4555 free(quirks[i].name);
4556 j = quirks_length - 1;
4558 quirks[i] = quirks[j];
4562 /* found: replace */
4563 DNPRINTF(SWM_D_QUIRK,
4564 "setquirk: replace #%d %s:%s\n",
4565 i, quirks[i].class, quirks[i].name);
4566 free(quirks[i].class);
4567 free(quirks[i].name);
4568 quirks[i].class = strdup(class);
4569 quirks[i].name = strdup(name);
4570 quirks[i].quirk = quirk;
4577 "error: setquirk: cannot find class/name combination");
4580 /* not found: add */
4581 if (quirks_size == 0 || quirks == NULL) {
4583 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
4584 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
4585 if (quirks == NULL) {
4586 fprintf(stderr, "setquirk: malloc failed\n");
4590 } else if (quirks_length == quirks_size) {
4592 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
4593 quirks = realloc(quirks,
4594 (size_t)quirks_size * sizeof(struct quirk));
4595 if (quirks == NULL) {
4596 fprintf(stderr, "setquirk: realloc failed\n");
4601 if (quirks_length < quirks_size) {
4602 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
4603 j = quirks_length++;
4604 quirks[j].class = strdup(class);
4605 quirks[j].name = strdup(name);
4606 quirks[j].quirk = quirk;
4608 fprintf(stderr, "quirks array problem?\n");
4609 if (quirks == NULL) {
4610 fprintf(stderr, "quirks array problem!\n");
4617 setconfquirk(char *selector, char *value, int flags)
4619 char *cp, *class, *name;
4621 unsigned long quirks;
4622 if (selector == NULL)
4624 if ((cp = strchr(selector, ':')) == NULL)
4629 if ((retval = parsequirks(value, &quirks)) == 0)
4630 setquirk(class, name, quirks);
4637 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
4638 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
4639 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
4640 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
4641 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4642 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
4643 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4644 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4645 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4646 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4647 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
4648 setquirk("pcb", "pcb", SWM_Q_FLOAT);
4649 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
4652 /* conf file stuff */
4653 #define SWM_CONF_FILE "scrotwm.conf"
4655 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH,
4656 SWM_S_STACK_ENABLED, SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT,
4657 SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED,
4658 SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
4659 SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED, SWM_S_URGENT_ENABLED,
4660 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH,
4661 SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM,
4662 SWM_S_SS_APP, SWM_S_DIALOG_RATIO, SWM_S_BAR_AT_BOTTOM,
4663 SWM_S_VERBOSE_LAYOUT
4667 setconfvalue(char *selector, char *value, int flags)
4672 case SWM_S_BAR_DELAY:
4673 bar_delay = atoi(value);
4675 case SWM_S_BAR_ENABLED:
4676 bar_enabled = atoi(value);
4678 case SWM_S_BAR_BORDER_WIDTH:
4679 bar_border_width = atoi(value);
4681 case SWM_S_BAR_AT_BOTTOM:
4682 bar_at_bottom = atoi(value);
4684 case SWM_S_STACK_ENABLED:
4685 stack_enabled = atoi(value);
4687 case SWM_S_CLOCK_ENABLED:
4688 clock_enabled = atoi(value);
4690 case SWM_S_CLOCK_FORMAT:
4691 #ifndef SWM_DENY_CLOCK_FORMAT
4693 if ((clock_format = strdup(value)) == NULL)
4694 err(1, "setconfvalue: clock_format");
4697 case SWM_S_CYCLE_EMPTY:
4698 cycle_empty = atoi(value);
4700 case SWM_S_CYCLE_VISIBLE:
4701 cycle_visible = atoi(value);
4703 case SWM_S_SS_ENABLED:
4704 ss_enabled = atoi(value);
4706 case SWM_S_TERM_WIDTH:
4707 term_width = atoi(value);
4709 case SWM_S_TITLE_CLASS_ENABLED:
4710 title_class_enabled = atoi(value);
4712 case SWM_S_WINDOW_NAME_ENABLED:
4713 window_name_enabled = atoi(value);
4715 case SWM_S_TITLE_NAME_ENABLED:
4716 title_name_enabled = atoi(value);
4718 case SWM_S_URGENT_ENABLED:
4719 urgent_enabled = atoi(value);
4721 case SWM_S_FOCUS_MODE:
4722 if (!strcmp(value, "default"))
4723 focus_mode = SWM_FOCUS_DEFAULT;
4724 else if (!strcmp(value, "follow_cursor"))
4725 focus_mode = SWM_FOCUS_FOLLOW;
4726 else if (!strcmp(value, "synergy"))
4727 focus_mode = SWM_FOCUS_SYNERGY;
4729 err(1, "focus_mode");
4731 case SWM_S_DISABLE_BORDER:
4732 disable_border = atoi(value);
4734 case SWM_S_BORDER_WIDTH:
4735 border_width = atoi(value);
4737 case SWM_S_BAR_FONT:
4739 if ((bar_fonts[0] = strdup(value)) == NULL)
4740 err(1, "setconfvalue: bar_font");
4742 case SWM_S_BAR_ACTION:
4744 if ((bar_argv[0] = strdup(value)) == NULL)
4745 err(1, "setconfvalue: bar_action");
4747 case SWM_S_SPAWN_TERM:
4748 free(spawn_term[0]);
4749 if ((spawn_term[0] = strdup(value)) == NULL)
4750 err(1, "setconfvalue: spawn_term");
4754 case SWM_S_DIALOG_RATIO:
4755 dialog_ratio = atof(value);
4756 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
4759 case SWM_S_VERBOSE_LAYOUT:
4760 verbose_layout = atoi(value);
4761 for (i = 0; layouts[i].l_stack != NULL; i++) {
4763 layouts[i].l_string = fancy_stacker;
4765 layouts[i].l_string = plain_stacker;
4775 setconfmodkey(char *selector, char *value, int flags)
4777 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
4778 update_modkey(Mod1Mask);
4779 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
4780 update_modkey(Mod2Mask);
4781 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
4782 update_modkey(Mod3Mask);
4783 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
4784 update_modkey(Mod4Mask);
4791 setconfcolor(char *selector, char *value, int flags)
4793 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
4798 setconfregion(char *selector, char *value, int flags)
4800 custom_region(value);
4805 setautorun(char *selector, char *value, int flags)
4815 if (getenv("SWM_STARTED"))
4819 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
4820 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'\n");
4822 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
4823 errx(1, "autorun: invalid workspace %d\n", ws_id + 1);
4826 * This is a little intricate
4828 * If the pid already exists we simply reuse it because it means it was
4829 * used before AND not claimed by manage_window. We get away with
4830 * altering it in the parent after INSERT because this can not be a race
4833 while ((ap = strsep(&sp, " \t")) != NULL) {
4836 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
4838 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
4839 err(1, "setautorun: realloc");
4840 a.argv[argc - 1] = ap;
4843 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
4844 err(1, "setautorun: realloc");
4845 a.argv[argc] = NULL;
4847 if ((pid = fork()) == 0) {
4848 spawn(ws_id, &a, 1);
4857 p = calloc(1, sizeof *p);
4860 TAILQ_INSERT_TAIL(&pidlist, p, entry);
4870 setlayout(char *selector, char *value, int flags)
4872 int ws_id, i, x, mg, ma, si, raise;
4873 int st = SWM_V_STACK;
4875 struct workspace *ws;
4877 if (getenv("SWM_STARTED"))
4881 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
4882 &ws_id, &mg, &ma, &si, &raise, s) != 6)
4883 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
4884 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
4887 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
4888 errx(1, "layout: invalid workspace %d\n", ws_id + 1);
4890 if (!strcasecmp(s, "vertical"))
4892 else if (!strcasecmp(s, "horizontal"))
4894 else if (!strcasecmp(s, "fullscreen"))
4897 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
4898 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
4901 for (i = 0; i < ScreenCount(display); i++) {
4902 ws = (struct workspace *)&screens[i].ws;
4903 ws[ws_id].cur_layout = &layouts[st];
4905 ws[ws_id].always_raise = raise;
4906 if (st == SWM_MAX_STACK)
4910 for (x = 0; x < abs(mg); x++) {
4911 ws[ws_id].cur_layout->l_config(&ws[ws_id],
4912 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
4913 SWM_ARG_ID_MASTERSHRINK);
4917 for (x = 0; x < abs(ma); x++) {
4918 ws[ws_id].cur_layout->l_config(&ws[ws_id],
4919 ma >= 0 ? SWM_ARG_ID_MASTERADD :
4920 SWM_ARG_ID_MASTERDEL);
4924 for (x = 0; x < abs(si); x++) {
4925 ws[ws_id].cur_layout->l_config(&ws[ws_id],
4926 si >= 0 ? SWM_ARG_ID_STACKINC :
4927 SWM_ARG_ID_STACKDEC);
4935 /* config options */
4936 struct config_option {
4938 int (*func)(char*, char*, int);
4941 struct config_option configopt[] = {
4942 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
4943 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
4944 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
4945 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
4946 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
4947 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
4948 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
4949 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
4950 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
4951 { "keyboard_mapping", setkeymapping, 0 },
4952 { "bind", setconfbinding, 0 },
4953 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
4954 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
4955 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
4956 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
4957 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
4958 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
4959 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
4960 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
4961 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
4962 { "modkey", setconfmodkey, 0 },
4963 { "program", setconfspawn, 0 },
4964 { "quirk", setconfquirk, 0 },
4965 { "region", setconfregion, 0 },
4966 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
4967 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
4968 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
4969 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
4970 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
4971 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
4972 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
4973 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
4974 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
4975 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
4976 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
4977 { "autorun", setautorun, 0 },
4978 { "layout", setlayout, 0 },
4983 conf_load(char *filename, int keymapping)
4986 char *line, *cp, *optsub, *optval;
4987 size_t linelen, lineno = 0;
4988 int wordlen, i, optind;
4989 struct config_option *opt;
4991 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
4993 if (filename == NULL) {
4994 fprintf(stderr, "conf_load: no filename\n");
4997 if ((config = fopen(filename, "r")) == NULL) {
4998 warn("conf_load: fopen: %s", filename);
5002 while (!feof(config)) {
5003 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
5006 err(1, "%s", filename);
5011 cp += strspn(cp, " \t\n"); /* eat whitespace */
5012 if (cp[0] == '\0') {
5017 /* get config option */
5018 wordlen = strcspn(cp, "=[ \t\n");
5020 warnx("%s: line %zd: no option found",
5025 for (i = 0; i < LENGTH(configopt); i++) {
5026 opt = &configopt[i];
5027 if (!strncasecmp(cp, opt->optname, wordlen) &&
5028 strlen(opt->optname) == wordlen) {
5034 warnx("%s: line %zd: unknown option %.*s",
5035 filename, lineno, wordlen, cp);
5038 if (keymapping && strcmp(opt->optname, "bind")) {
5039 warnx("%s: line %zd: invalid option %.*s",
5040 filename, lineno, wordlen, cp);
5044 cp += strspn(cp, " \t\n"); /* eat whitespace */
5045 /* get [selector] if any */
5049 wordlen = strcspn(cp, "]");
5052 warnx("%s: line %zd: syntax error",
5056 asprintf(&optsub, "%.*s", wordlen, cp);
5059 cp += strspn(cp, "] \t\n"); /* eat trailing */
5061 cp += strspn(cp, "= \t\n"); /* eat trailing */
5063 optval = strdup(cp);
5064 /* call function to deal with it all */
5065 if (configopt[optind].func(optsub, optval,
5066 configopt[optind].funcflags) != 0) {
5067 fprintf(stderr, "%s line %zd: %s\n",
5068 filename, lineno, line);
5069 errx(1, "%s: line %zd: invalid data for %s",
5070 filename, lineno, configopt[optind].optname);
5078 DNPRINTF(SWM_D_CONF, "conf_load end\n");
5084 set_child_transient(struct ws_win *win, Window *trans)
5086 struct ws_win *parent, *w;
5087 XWMHints *wmh = NULL;
5088 struct swm_region *r;
5089 struct workspace *ws;
5091 parent = find_window(win->transient);
5093 parent->child_trans = win;
5095 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
5096 " for %lu trans %lu\n", win->id, win->transient);
5098 if (win->hints == NULL) {
5099 fprintf(stderr, "no hints for %lu\n", win->id);
5103 r = root_to_region(win->wa.root);
5105 /* parent doen't exist in our window list */
5106 TAILQ_FOREACH(w, &ws->winlist, entry) {
5110 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
5111 fprintf(stderr, "can't get hints for %lu\n",
5116 if (win->hints->window_group != wmh->window_group)
5119 w->child_trans = win;
5120 win->transient = w->id;
5122 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
5123 "transient to %lu\n", win->transient);
5133 window_get_pid(Window win)
5135 Atom actual_type_return;
5136 int actual_format_return = 0;
5137 unsigned long nitems_return = 0;
5138 unsigned long bytes_after_return = 0;
5142 unsigned char *prop = NULL;
5144 if (XGetWindowProperty(display, win,
5145 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
5146 XA_CARDINAL, &actual_type_return, &actual_format_return,
5147 &nitems_return, &bytes_after_return,
5148 (unsigned char**)(void*)&pid) != Success)
5150 if (actual_type_return != XA_CARDINAL)
5161 if (XGetWindowProperty(display, win,
5162 XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
5163 XA_STRING, &actual_type_return, &actual_format_return,
5164 &nitems_return, &bytes_after_return, &prop) != Success)
5166 if (actual_type_return != XA_STRING)
5171 ret = strtonum((const char *)prop, 0, UINT_MAX, &errstr);
5172 /* ignore error because strtonum returns 0 anyway */
5179 manage_window(Window id)
5182 struct workspace *ws;
5183 struct ws_win *win, *ww;
5184 int format, i, ws_idx, n, border_me = 0;
5185 unsigned long nitems, bytes;
5186 Atom ws_idx_atom = 0, type;
5187 Atom *prot = NULL, *pp;
5188 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
5189 struct swm_region *r;
5195 if ((win = find_window(id)) != NULL)
5196 return (win); /* already being managed */
5198 /* see if we are on the unmanaged list */
5199 if ((win = find_unmanaged_window(id)) != NULL) {
5200 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
5202 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5203 if (win->transient) {
5204 set_child_transient(win, &trans);
5205 } if (trans && (ww = find_window(trans)))
5206 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
5208 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
5209 ewmh_update_actions(win);
5213 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
5214 errx(1, "calloc: failed to allocate memory for new window");
5218 /* see if we need to override the workspace */
5219 p = find_pid(window_get_pid(id));
5221 /* Get all the window data in one shot */
5222 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
5224 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
5225 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
5227 XGetWindowAttributes(display, id, &win->wa);
5228 XGetWMNormalHints(display, id, &win->sh, &mask);
5229 win->hints = XGetWMHints(display, id);
5230 XGetTransientForHint(display, id, &trans);
5232 win->transient = trans;
5233 set_child_transient(win, &trans);
5234 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
5235 win->id, win->transient);
5238 /* get supported protocols */
5239 if (XGetWMProtocols(display, id, &prot, &n)) {
5240 for (i = 0, pp = prot; i < n; i++, pp++) {
5241 if (*pp == takefocus)
5242 win->take_focus = 1;
5244 win->can_delete = 1;
5250 win->iconic = get_iconic(win);
5253 * Figure out where to put the window. If it was previously assigned to
5254 * a workspace (either by spawn() or manually moving), and isn't
5255 * transient, * put it in the same workspace
5257 r = root_to_region(win->wa.root);
5259 ws = &r->s->ws[p->ws];
5260 TAILQ_REMOVE(&pidlist, p, entry);
5263 } else if (prop && win->transient == 0) {
5264 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
5265 ws_idx = strtonum((const char *)prop, 0, 9, &errstr);
5267 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
5270 ws = &r->s->ws[ws_idx];
5273 /* this should launch transients in the same ws as parent */
5275 if ((ww = find_window(trans)) != NULL)
5282 "fix this bug mcbride\n");
5287 /* set up the window layout */
5290 win->s = r->s; /* this never changes */
5291 if (trans && (ww = find_window(trans)))
5292 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
5294 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
5296 win->g.w = win->wa.width;
5297 win->g.h = win->wa.height;
5298 win->g.x = win->wa.x;
5299 win->g.y = win->wa.y;
5300 win->g_floatvalid = 0;
5301 win->floatmaxed = 0;
5302 win->ewmh_flags = 0;
5304 /* Set window properties so we can remember this after reincarnation */
5305 if (ws_idx_atom && prop == NULL &&
5306 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
5308 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
5310 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
5311 PropModeReplace, ws_idx_str, SWM_PROPLEN);
5316 ewmh_autoquirk(win);
5318 if (XGetClassHint(display, win->id, &win->ch)) {
5319 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
5320 win->ch.res_class, win->ch.res_name);
5322 /* java is retarded so treat it special */
5323 if (strstr(win->ch.res_name, "sun-awt")) {
5328 for (i = 0; i < quirks_length; i++){
5329 if (!strcmp(win->ch.res_class, quirks[i].class) &&
5330 !strcmp(win->ch.res_name, quirks[i].name)) {
5331 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
5332 win->ch.res_class, win->ch.res_name);
5333 if (quirks[i].quirk & SWM_Q_FLOAT) {
5337 win->quirks = quirks[i].quirk;
5342 /* alter window position if quirky */
5343 if (win->quirks & SWM_Q_ANYWHERE) {
5344 win->manual = 1; /* don't center the quirky windows */
5345 bzero(&wc, sizeof wc);
5347 if (bar_enabled && win->g.y < bar_height) {
5348 win->g.y = wc.y = bar_height;
5351 if (win->g.w + win->g.x > WIDTH(r)) {
5352 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
5358 /* Reset font sizes (the bruteforce way; no default keybinding). */
5359 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
5360 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5361 fake_keypress(win, XK_KP_Subtract, ShiftMask);
5362 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5363 fake_keypress(win, XK_KP_Add, ShiftMask);
5366 ewmh_get_win_state(win);
5367 ewmh_update_actions(win);
5368 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
5372 bzero(&wc, sizeof wc);
5373 wc.border_width = border_width;
5374 mask = CWBorderWidth;
5375 XConfigureWindow(display, win->id, mask, &wc);
5378 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
5379 PropertyChangeMask | StructureNotifyMask);
5381 /* floaters need to be mapped if they are in the current workspace */
5382 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
5383 XMapRaised(display, win->id);
5389 free_window(struct ws_win *win)
5391 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
5396 /* needed for restart wm */
5397 set_win_state(win, WithdrawnState);
5399 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5401 if (win->ch.res_class)
5402 XFree(win->ch.res_class);
5403 if (win->ch.res_name)
5404 XFree(win->ch.res_name);
5409 memset(win, 0xff, sizeof *win); /* XXX kill later */
5415 unmanage_window(struct ws_win *win)
5417 struct ws_win *parent;
5422 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
5424 if (win->transient) {
5425 parent = find_window(win->transient);
5427 parent->child_trans = NULL;
5430 /* focus on root just in case */
5431 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
5435 TAILQ_REMOVE(&win->ws->winlist, win, entry);
5436 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
5442 focus_magic(struct ws_win *win)
5444 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu\n", WINID(win));
5449 if (win->child_trans) {
5450 /* win = parent & has a transient so focus on that */
5452 focus_win(win->child_trans);
5453 if (win->child_trans->take_focus)
5454 client_msg(win, takefocus);
5456 /* make sure transient hasn't dissapeared */
5457 if (validate_win(win->child_trans) == 0) {
5458 focus_win(win->child_trans);
5459 if (win->child_trans->take_focus)
5460 client_msg(win->child_trans, takefocus);
5462 win->child_trans = NULL;
5464 if (win->take_focus)
5465 client_msg(win, takefocus);
5471 if (win->take_focus)
5472 client_msg(win, takefocus);
5479 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
5487 XKeyEvent *ev = &e->xkey;
5489 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
5491 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
5492 for (i = 0; i < keys_length; i++)
5493 if (keysym == keys[i].keysym
5494 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
5495 && keyfuncs[keys[i].funcid].func) {
5496 if (keys[i].funcid == kf_spawn_custom)
5498 root_to_region(ev->root),
5499 &(keyfuncs[keys[i].funcid].args),
5503 keyfuncs[keys[i].funcid].func(
5504 root_to_region(ev->root),
5505 &(keyfuncs[keys[i].funcid].args)
5511 buttonpress(XEvent *e)
5515 XButtonPressedEvent *ev = &e->xbutton;
5517 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
5519 if ((win = find_window(ev->window)) == NULL)
5523 action = client_click;
5525 for (i = 0; i < LENGTH(buttons); i++)
5526 if (action == buttons[i].action && buttons[i].func &&
5527 buttons[i].button == ev->button &&
5528 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
5529 buttons[i].func(win, &buttons[i].args);
5533 configurerequest(XEvent *e)
5535 XConfigureRequestEvent *ev = &e->xconfigurerequest;
5540 if ((win = find_window(ev->window)) == NULL)
5541 if ((win = find_unmanaged_window(ev->window)) == NULL)
5545 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
5547 bzero(&wc, sizeof wc);
5550 wc.width = ev->width;
5551 wc.height = ev->height;
5552 wc.border_width = ev->border_width;
5553 wc.sibling = ev->above;
5554 wc.stack_mode = ev->detail;
5555 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
5557 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
5559 config_win(win, ev);
5564 configurenotify(XEvent *e)
5569 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
5570 e->xconfigure.window);
5572 win = find_window(e->xconfigure.window);
5574 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5578 if (focus_mode == SWM_FOCUS_DEFAULT)
5579 drain_enter_notify();
5584 destroynotify(XEvent *e)
5587 XDestroyWindowEvent *ev = &e->xdestroywindow;
5589 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
5591 if ((win = find_window(ev->window)) == NULL) {
5592 if ((win = find_unmanaged_window(ev->window)) == NULL)
5598 /* make sure we focus on something */
5601 unmanage_window(win);
5603 if (focus_mode == SWM_FOCUS_DEFAULT)
5604 drain_enter_notify();
5609 enternotify(XEvent *e)
5611 XCrossingEvent *ev = &e->xcrossing;
5616 Window focus_return;
5617 int revert_to_return;
5619 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
5620 "%lu subwindow %lu same_screen %d focus %d state %d\n",
5621 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
5622 ev->same_screen, ev->focus, ev->state);
5624 switch (focus_mode) {
5625 case SWM_FOCUS_DEFAULT:
5627 case SWM_FOCUS_FOLLOW:
5629 case SWM_FOCUS_SYNERGY:
5632 * all these checks need to be in this order because the
5633 * XCheckTypedWindowEvent relies on weeding out the previous events
5635 * making this code an option would enable a follow mouse for focus
5640 * state is set when we are switching workspaces and focus is set when
5641 * the window or a subwindow already has focus (occurs during restart).
5643 * Only honor the focus flag if last_focus_event is not FocusOut,
5644 * this allows scrotwm to continue to control focus when another
5645 * program is also playing with it.
5647 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
5648 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
5653 * happens when a window is created or destroyed and the border
5654 * crosses the mouse pointer and when switching ws
5656 * we need the subwindow test to see if we came from root in order
5657 * to give focus to floaters
5659 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
5660 ev->subwindow == 0) {
5661 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
5665 /* this window already has focus */
5666 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
5667 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
5671 /* this window is being deleted or moved to another ws */
5672 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
5674 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
5675 XPutBackEvent(display, &cne);
5679 if ((win = find_window(ev->window)) == NULL) {
5680 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5685 * In fullstack kill all enters unless they come from a different ws
5686 * (i.e. another region) or focus has been grabbed externally.
5688 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
5689 last_focus_event != FocusOut) {
5690 XGetInputFocus(display, &focus_return, &revert_to_return);
5691 if ((w = find_window(focus_return)) == NULL ||
5693 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
5701 if ((win = find_window(ev->window)) == NULL) {
5702 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5707 * if we have more enternotifies let them handle it in due time
5709 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
5710 DNPRINTF(SWM_D_EVENT,
5711 "ignoring enternotify: got more enternotify\n");
5712 XPutBackEvent(display, &cne);
5719 /* lets us use one switch statement for arbitrary mode/detail combinations */
5720 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
5723 focusevent(XEvent *e)
5727 u_int32_t mode_detail;
5728 XFocusChangeEvent *ev = &e->xfocus;
5730 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
5731 ev->type == FocusIn ? "entering" : "leaving",
5732 ev->window, ev->mode, ev->detail);
5734 if (last_focus_event == ev->type) {
5735 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
5739 last_focus_event = ev->type;
5740 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
5742 switch (mode_detail) {
5743 /* synergy client focus operations */
5744 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
5745 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
5747 /* synergy server focus operations */
5748 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
5750 /* Entering applications like rdesktop that mangle the pointer */
5751 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
5753 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
5754 XSetWindowBorder(display, win->id,
5755 win->ws->r->s->c[ev->type == FocusIn ?
5756 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
5759 fprintf(stderr, "ignoring focusevent\n");
5760 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
5767 mapnotify(XEvent *e)
5770 XMapEvent *ev = &e->xmap;
5772 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
5774 win = manage_window(ev->window);
5776 set_win_state(win, NormalState);
5780 mappingnotify(XEvent *e)
5782 XMappingEvent *ev = &e->xmapping;
5784 XRefreshKeyboardMapping(ev);
5785 if (ev->request == MappingKeyboard)
5790 maprequest(XEvent *e)
5793 struct swm_region *r;
5794 XWindowAttributes wa;
5795 XMapRequestEvent *ev = &e->xmaprequest;
5797 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
5798 e->xmaprequest.window);
5800 if (!XGetWindowAttributes(display, ev->window, &wa))
5802 if (wa.override_redirect)
5805 win = manage_window(e->xmaprequest.window);
5807 return; /* can't happen */
5811 /* make new win focused */
5812 r = root_to_region(win->wa.root);
5813 if (win->ws == r->ws)
5818 propertynotify(XEvent *e)
5821 XPropertyEvent *ev = &e->xproperty;
5823 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
5826 win = find_window(ev->window);
5830 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
5831 update_iconic(win, 0);
5832 XMapRaised(display, win->id);
5839 case XA_WM_NORMAL_HINTS:
5842 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5843 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
5844 if (win->sh.flags & PMinSize) {
5845 win->g.w = win->sh.min_width;
5846 win->g.h = win->sh.min_height;
5847 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
5849 XMoveResizeWindow(display, win->id,
5850 win->g.x, win->g.y, win->g.w, win->g.h);
5852 if (window_name_enabled)
5861 unmapnotify(XEvent *e)
5865 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
5867 /* determine if we need to help unmanage this window */
5868 win = find_window(e->xunmap.window);
5872 if (getstate(e->xunmap.window) == NormalState) {
5873 unmanage_window(win);
5876 /* giant hack for apps that don't destroy transient windows */
5877 /* eat a bunch of events to prevent remanaging the window */
5879 while (XCheckWindowEvent(display, e->xunmap.window,
5880 EnterWindowMask, &cne))
5882 while (XCheckWindowEvent(display, e->xunmap.window,
5883 StructureNotifyMask, &cne))
5885 while (XCheckWindowEvent(display, e->xunmap.window,
5886 SubstructureNotifyMask, &cne))
5888 /* resend unmap because we ated it */
5889 XUnmapWindow(display, e->xunmap.window);
5892 if (focus_mode == SWM_FOCUS_DEFAULT)
5893 drain_enter_notify();
5897 visibilitynotify(XEvent *e)
5900 struct swm_region *r;
5902 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
5903 e->xvisibility.window);
5904 if (e->xvisibility.state == VisibilityUnobscured)
5905 for (i = 0; i < ScreenCount(display); i++)
5906 TAILQ_FOREACH(r, &screens[i].rl, entry)
5907 if (e->xvisibility.window == r->bar_window)
5912 clientmessage(XEvent *e)
5914 XClientMessageEvent *ev;
5919 win = find_window(ev->window);
5923 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx type: %ld \n",
5924 ev->window, ev->message_type);
5926 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
5927 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW \n");
5930 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
5931 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW \n");
5932 if (win->can_delete)
5933 client_msg(win, adelete);
5935 XKillClient(display, win->id);
5937 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
5938 DNPRINTF(SWM_D_EVENT,
5939 "clientmessage: _NET_MOVERESIZE_WINDOW \n");
5940 if (win->floating) {
5941 if (ev->data.l[0] & (1<<8)) /* x */
5942 win->g.x = ev->data.l[1];
5943 if (ev->data.l[0] & (1<<9)) /* y */
5944 win->g.y = ev->data.l[2];
5945 if (ev->data.l[0] & (1<<10)) /* width */
5946 win->g.w = ev->data.l[3];
5947 if (ev->data.l[0] & (1<<11)) /* height */
5948 win->g.h = ev->data.l[4];
5951 /* TODO: Change stack sizes */
5953 config_win(win, NULL);
5955 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
5956 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE \n");
5957 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
5959 ewmh_update_win_state(win, ev->data.l[2],
5967 xerror_start(Display *d, XErrorEvent *ee)
5974 xerror(Display *d, XErrorEvent *ee)
5976 /* fprintf(stderr, "error: %p %p\n", display, ee); */
5984 xerrorxlib = XSetErrorHandler(xerror_start);
5986 /* this causes an error if some other window manager is running */
5987 XSelectInput(display, DefaultRootWindow(display),
5988 SubstructureRedirectMask);
5989 XSync(display, False);
5993 XSetErrorHandler(xerror);
5994 XSync(display, False);
5999 new_region(struct swm_screen *s, int x, int y, int w, int h)
6001 struct swm_region *r, *n;
6002 struct workspace *ws = NULL;
6005 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
6006 s->idx, w, h, x, y);
6008 /* remove any conflicting regions */
6009 n = TAILQ_FIRST(&s->rl);
6012 n = TAILQ_NEXT(r, entry);
6013 if (X(r) < (x + w) &&
6014 (X(r) + WIDTH(r)) > x &&
6016 (Y(r) + HEIGHT(r)) > y) {
6017 if (r->ws->r != NULL)
6018 r->ws->old_r = r->ws->r;
6020 XDestroyWindow(display, r->bar_window);
6021 TAILQ_REMOVE(&s->rl, r, entry);
6022 TAILQ_INSERT_TAIL(&s->orl, r, entry);
6026 /* search old regions for one to reuse */
6028 /* size + location match */
6029 TAILQ_FOREACH(r, &s->orl, entry)
6030 if (X(r) == x && Y(r) == y &&
6031 HEIGHT(r) == h && WIDTH(r) == w)
6035 TAILQ_FOREACH(r, &s->orl, entry)
6036 if (HEIGHT(r) == h && WIDTH(r) == w)
6040 TAILQ_REMOVE(&s->orl, r, entry);
6041 /* try to use old region's workspace */
6042 if (r->ws->r == NULL)
6045 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
6046 errx(1, "calloc: failed to allocate memory for screen");
6048 /* if we don't have a workspace already, find one */
6050 for (i = 0; i < SWM_WS_MAX; i++)
6051 if (s->ws[i].r == NULL) {
6058 errx(1, "no free workspaces\n");
6069 TAILQ_INSERT_TAIL(&s->rl, r, entry);
6075 #ifdef SWM_XRR_HAS_CRTC
6077 XRRScreenResources *sr;
6080 #endif /* SWM_XRR_HAS_CRTC */
6081 struct swm_region *r;
6084 if (i >= ScreenCount(display))
6085 errx(1, "invalid screen");
6087 /* remove any old regions */
6088 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
6089 r->ws->old_r = r->ws->r = NULL;
6090 XDestroyWindow(display, r->bar_window);
6091 TAILQ_REMOVE(&screens[i].rl, r, entry);
6092 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
6096 /* map virtual screens onto physical screens */
6097 #ifdef SWM_XRR_HAS_CRTC
6098 if (xrandr_support) {
6099 sr = XRRGetScreenResources(display, screens[i].root);
6101 new_region(&screens[i], 0, 0,
6102 DisplayWidth(display, i),
6103 DisplayHeight(display, i));
6107 for (c = 0, ci = NULL; c < ncrtc; c++) {
6108 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
6109 if (ci->noutput == 0)
6112 if (ci != NULL && ci->mode == None)
6113 new_region(&screens[i], 0, 0,
6114 DisplayWidth(display, i),
6115 DisplayHeight(display, i));
6117 new_region(&screens[i],
6118 ci->x, ci->y, ci->width, ci->height);
6121 XRRFreeCrtcInfo(ci);
6122 XRRFreeScreenResources(sr);
6124 #endif /* SWM_XRR_HAS_CRTC */
6126 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
6127 DisplayHeight(display, i));
6132 screenchange(XEvent *e) {
6133 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
6134 struct swm_region *r;
6137 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
6139 if (!XRRUpdateConfiguration(e))
6142 /* silly event doesn't include the screen index */
6143 for (i = 0; i < ScreenCount(display); i++)
6144 if (screens[i].root == xe->root)
6146 if (i >= ScreenCount(display))
6147 errx(1, "screenchange: screen not found\n");
6149 /* brute force for now, just re-enumerate the regions */
6152 /* add bars to all regions */
6153 for (i = 0; i < ScreenCount(display); i++)
6154 TAILQ_FOREACH(r, &screens[i].rl, entry)
6157 if (focus_mode == SWM_FOCUS_DEFAULT)
6158 drain_enter_notify();
6164 Window d1, d2, *wins = NULL;
6165 XWindowAttributes wa;
6170 for (i = 0; i < ScreenCount(display); i++) {
6171 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
6174 /* attach windows to a region */
6175 /* normal windows */
6176 for (j = 0; j < no; j++) {
6177 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6178 wa.override_redirect ||
6179 XGetTransientForHint(display, wins[j], &d1))
6182 state = getstate(wins[j]);
6183 manage = state == IconicState;
6184 if (wa.map_state == IsViewable || manage)
6185 manage_window(wins[j]);
6187 /* transient windows */
6188 for (j = 0; j < no; j++) {
6189 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6190 wa.override_redirect)
6193 state = getstate(wins[j]);
6194 manage = state == IconicState;
6195 if (XGetTransientForHint(display, wins[j], &d1) &&
6197 manage_window(wins[j]);
6210 int errorbase, major, minor;
6211 struct workspace *ws;
6213 if ((screens = calloc(ScreenCount(display),
6214 sizeof(struct swm_screen))) == NULL)
6215 errx(1, "calloc: screens");
6217 /* initial Xrandr setup */
6218 xrandr_support = XRRQueryExtension(display,
6219 &xrandr_eventbase, &errorbase);
6221 if (XRRQueryVersion(display, &major, &minor) && major < 1)
6224 /* map physical screens */
6225 for (i = 0; i < ScreenCount(display); i++) {
6226 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
6228 TAILQ_INIT(&screens[i].rl);
6229 TAILQ_INIT(&screens[i].orl);
6230 screens[i].root = RootWindow(display, i);
6232 /* set default colors */
6233 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
6234 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
6235 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
6236 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
6237 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
6239 /* set default cursor */
6240 XDefineCursor(display, screens[i].root,
6241 XCreateFontCursor(display, XC_left_ptr));
6243 /* init all workspaces */
6244 /* XXX these should be dynamically allocated too */
6245 for (j = 0; j < SWM_WS_MAX; j++) {
6246 ws = &screens[i].ws[j];
6251 TAILQ_INIT(&ws->winlist);
6252 TAILQ_INIT(&ws->unmanagedlist);
6254 for (k = 0; layouts[k].l_stack != NULL; k++)
6255 if (layouts[k].l_config != NULL)
6256 layouts[k].l_config(ws,
6257 SWM_ARG_ID_STACKINIT);
6258 ws->cur_layout = &layouts[0];
6259 ws->cur_layout->l_string(ws);
6265 XRRSelectInput(display, screens[i].root,
6266 RRScreenChangeNotifyMask);
6273 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
6275 err(1, "setup_globals: strdup");
6276 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
6278 err(1, "setup_globals: strdup");
6279 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
6281 err(1, "setup_globals: strdup");
6282 if ((spawn_term[0] = strdup("xterm")) == NULL)
6283 err(1, "setup_globals: strdup");
6284 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
6292 Atom netwmcheck, netwmname, utf8_string;
6295 /* work around sun jdk bugs, code from wmname */
6296 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
6297 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
6298 utf8_string = XInternAtom(display, "UTF8_STRING", False);
6299 for (i = 0; i < ScreenCount(display); i++) {
6300 root = screens[i].root;
6301 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
6302 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
6303 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
6305 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
6306 PropModeReplace, (unsigned char *)&win,1);
6307 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
6308 PropModeReplace, (unsigned char *)&win,1);
6309 XChangeProperty(display, win, netwmname, utf8_string, 8,
6310 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
6315 main(int argc, char *argv[])
6317 struct swm_region *r, *rr;
6318 struct ws_win *winfocus = NULL;
6321 char conf[PATH_MAX], *cfile = NULL;
6326 struct sigaction sact;
6329 fprintf(stderr, "Welcome to scrotwm V%s Build: %s\n",
6330 SCROTWM_VERSION, buildstr);
6331 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
6332 warnx("no locale support");
6334 if (!(display = XOpenDisplay(0)))
6335 errx(1, "can not open display");
6338 errx(1, "other wm running");
6340 /* handle some signals */
6341 bzero(&sact, sizeof(sact));
6342 sigemptyset(&sact.sa_mask);
6344 sact.sa_handler = sighdlr;
6345 sigaction(SIGINT, &sact, NULL);
6346 sigaction(SIGQUIT, &sact, NULL);
6347 sigaction(SIGTERM, &sact, NULL);
6348 sigaction(SIGHUP, &sact, NULL);
6350 sact.sa_handler = sighdlr;
6351 sact.sa_flags = SA_NOCLDSTOP;
6352 sigaction(SIGCHLD, &sact, NULL);
6354 astate = XInternAtom(display, "WM_STATE", False);
6355 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
6356 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
6357 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
6358 a_wmname = XInternAtom(display, "WM_NAME", False);
6359 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
6360 a_string = XInternAtom(display, "STRING", False);
6361 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
6363 /* look for local and global conf file */
6364 pwd = getpwuid(getuid());
6366 errx(1, "invalid user %d", getuid());
6375 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
6376 if (stat(conf, &sb) != -1) {
6377 if (S_ISREG(sb.st_mode))
6380 /* try global conf file */
6381 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
6382 if (!stat(conf, &sb))
6383 if (S_ISREG(sb.st_mode))
6387 conf_load(cfile, SWM_CONF_DEFAULT);
6390 /* set some values to work around bad programs */
6392 /* grab existing windows (before we build the bars) */
6395 if (getenv("SWM_STARTED") == NULL)
6396 setenv("SWM_STARTED", "YES", 1);
6398 /* setup all bars */
6399 for (i = 0; i < ScreenCount(display); i++)
6400 TAILQ_FOREACH(r, &screens[i].rl, entry) {
6401 if (winfocus == NULL)
6402 winfocus = TAILQ_FIRST(&r->ws->winlist);
6410 if (focus_mode == SWM_FOCUS_DEFAULT)
6411 drain_enter_notify();
6413 xfd = ConnectionNumber(display);
6415 while (XPending(display)) {
6416 XNextEvent(display, &e);
6419 if (e.type < LASTEvent) {
6421 if (handler[e.type])
6422 handler[e.type](&e);
6424 DNPRINTF(SWM_D_EVENT,
6425 "win: %lu unknown event: %d\n",
6426 e.xany.window, e.type);
6428 switch (e.type - xrandr_eventbase) {
6429 case RRScreenChangeNotify:
6433 DNPRINTF(SWM_D_EVENT,
6434 "win: %lu unknown xrandr event: "
6435 "%d\n", e.xany.window, e.type);
6441 /* if we are being restarted go focus on first window */
6443 rr = winfocus->ws->r;
6445 /* not a visible window */
6449 /* move pointer to first screen if multi screen */
6450 if (ScreenCount(display) > 1 || outputs > 1)
6451 XWarpPointer(display, None, rr->s[0].root,
6452 0, 0, 0, 0, rr->g.x,
6453 rr->g.y + (bar_enabled ? bar_height : 0));
6455 a.id = SWM_ARG_ID_FOCUSCUR;
6465 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
6467 DNPRINTF(SWM_D_MISC, "select failed");
6468 if (restart_wm == 1)
6469 restart(NULL, NULL);
6470 if (search_resp == 1)
6482 XFreeGC(display, bar_gc);
6483 XCloseDisplay(display);