2 * Copyright (c) 2009-2012 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-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>
8 * Copyright (c) 2011-2012 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
12 * Permission to use, copy, modify, and distribute this software for any
13 * purpose with or without fee is hereby granted, provided that the above
14 * copyright notice and this permission notice appear in all copies.
16 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
17 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
19 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 * Much code and ideas taken from dwm under the following license:
26 * MIT/X Consortium License
28 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
29 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
30 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
31 * 2007 Premysl Hruby <dfenze at gmail dot com>
32 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
33 * 2007 Christof Musik <christof at sendfax dot de>
34 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
35 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
36 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
38 * Permission is hereby granted, free of charge, to any person obtaining a
39 * copy of this software and associated documentation files (the "Software"),
40 * to deal in the Software without restriction, including without limitation
41 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
42 * and/or sell copies of the Software, and to permit persons to whom the
43 * Software is furnished to do so, subject to the following conditions:
45 * The above copyright notice and this permission notice shall be included in
46 * all copies or substantial portions of the Software.
48 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
49 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
50 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
51 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
52 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
53 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
54 * DEALINGS IN THE SOFTWARE.
72 #include <sys/types.h>
76 #include <sys/queue.h>
77 #include <sys/param.h>
78 #include <sys/select.h>
79 #if defined(__linux__)
81 #elif defined(__OpenBSD__)
83 #elif defined(__FreeBSD__)
89 #include <X11/cursorfont.h>
90 #include <X11/keysym.h>
91 #include <X11/XKBlib.h>
92 #include <X11/Xatom.h>
94 #include <X11/Xproto.h>
95 #include <X11/Xutil.h>
96 #include <X11/extensions/Xrandr.h>
97 #include <X11/extensions/XTest.h>
105 #ifdef SPECTRWM_BUILDSTR
106 static const char *buildstr = SPECTRWM_BUILDSTR;
108 static const char *buildstr = SPECTRWM_VERSION;
112 # error XRandR versions less than 1.0 are not supported
117 #define SWM_XRR_HAS_CRTC
121 /*#define SWM_DEBUG*/
123 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
124 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
125 #define SWM_D_MISC 0x0001
126 #define SWM_D_EVENT 0x0002
127 #define SWM_D_WS 0x0004
128 #define SWM_D_FOCUS 0x0008
129 #define SWM_D_MOVE 0x0010
130 #define SWM_D_STACK 0x0020
131 #define SWM_D_MOUSE 0x0040
132 #define SWM_D_PROP 0x0080
133 #define SWM_D_CLASS 0x0100
134 #define SWM_D_KEY 0x0200
135 #define SWM_D_QUIRK 0x0400
136 #define SWM_D_SPAWN 0x0800
137 #define SWM_D_EVENTQ 0x1000
138 #define SWM_D_CONF 0x2000
139 #define SWM_D_BAR 0x4000
141 u_int32_t swm_debug = 0
159 #define DPRINTF(x...)
160 #define DNPRINTF(n,x...)
163 #define LENGTH(x) (sizeof x / sizeof x[0])
164 #define MODKEY Mod1Mask
165 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
166 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
167 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
168 #define SWM_PROPLEN (16)
169 #define SWM_FUNCNAME_LEN (32)
170 #define SWM_KEYS_LEN (255)
171 #define SWM_QUIRK_LEN (64)
172 #define X(r) (r)->g.x
173 #define Y(r) (r)->g.y
174 #define WIDTH(r) (r)->g.w
175 #define HEIGHT(r) (r)->g.h
176 #define SH_MIN(w) (w)->sh_mask & PMinSize
177 #define SH_MIN_W(w) (w)->sh.min_width
178 #define SH_MIN_H(w) (w)->sh.min_height
179 #define SH_MAX(w) (w)->sh_mask & PMaxSize
180 #define SH_MAX_W(w) (w)->sh.max_width
181 #define SH_MAX_H(w) (w)->sh.max_height
182 #define SH_INC(w) (w)->sh_mask & PResizeInc
183 #define SH_INC_W(w) (w)->sh.width_inc
184 #define SH_INC_H(w) (w)->sh.height_inc
185 #define SWM_MAX_FONT_STEPS (3)
186 #define WINID(w) ((w) ? (w)->id : 0)
187 #define YESNO(x) ((x) ? "yes" : "no")
189 #define SWM_FOCUS_DEFAULT (0)
190 #define SWM_FOCUS_SYNERGY (1)
191 #define SWM_FOCUS_FOLLOW (2)
193 #define SWM_CONF_DEFAULT (0)
194 #define SWM_CONF_KEYMAPPING (1)
197 #define SWM_LIB "/usr/local/lib/libswmhack.so"
210 volatile sig_atomic_t running = 1;
211 volatile sig_atomic_t restart_wm = 0;
213 int last_focus_event = FocusOut;
214 int (*xerrorxlib)(Display *, XErrorEvent *);
218 int xrandr_eventbase;
219 unsigned int numlockmask = 0;
223 int cycle_visible = 0;
225 int font_adjusted = 0;
226 unsigned int mod_key = MODKEY;
229 struct swm_region *search_r;
230 int select_list_pipe[2];
231 int select_resp_pipe[2];
233 volatile sig_atomic_t search_resp;
234 int search_resp_action;
236 struct search_window {
237 TAILQ_ENTRY(search_window) entry;
243 TAILQ_HEAD(search_winlist, search_window);
244 struct search_winlist search_wl;
249 SWM_SEARCH_UNICONIFY,
250 SWM_SEARCH_NAME_WORKSPACE,
251 SWM_SEARCH_SEARCH_WORKSPACE,
252 SWM_SEARCH_SEARCH_WINDOW
255 #define SWM_STACK_TOP (0)
256 #define SWM_STACK_BOTTOM (1)
257 #define SWM_STACK_ABOVE (2)
258 #define SWM_STACK_BELOW (3)
261 double dialog_ratio = 0.6;
263 #define SWM_BAR_MAX (256)
264 #define SWM_BAR_JUSTIFY_LEFT (0)
265 #define SWM_BAR_JUSTIFY_CENTER (1)
266 #define SWM_BAR_JUSTIFY_RIGHT (2)
267 #define SWM_BAR_OFFSET (4)
268 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*," \
269 "-*-profont-*-*-*-*-*-*-*-*-*-*-*-*," \
270 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*," \
271 "-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*," \
272 "-*-*-*-r-*--*-*-*-*-*-*-*-*"
274 #ifdef X_HAVE_UTF8_STRING
275 #define DRAWSTRING(x...) Xutf8DrawString(x)
277 #define DRAWSTRING(x...) XmbDrawString(x)
280 char *bar_argv[] = { NULL, NULL };
282 unsigned char bar_ext[SWM_BAR_MAX];
283 char bar_vertext[SWM_BAR_MAX];
285 sig_atomic_t bar_alarm = 0;
288 int bar_border_width = 1;
289 int bar_at_bottom = 0;
291 int bar_extra_running = 0;
294 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
295 char *bar_format = NULL;
296 int stack_enabled = 1;
297 int clock_enabled = 1;
298 int urgent_enabled = 0;
299 char *clock_format = NULL;
300 int title_name_enabled = 0;
301 int title_class_enabled = 0;
302 int window_name_enabled = 0;
303 int focus_mode = SWM_FOCUS_DEFAULT;
304 int focus_close = SWM_STACK_BELOW;
305 int focus_close_wrap = 1;
306 int focus_default = SWM_STACK_TOP;
307 int spawn_position = SWM_STACK_TOP;
308 int disable_border = 0;
309 int border_width = 1;
310 int verbose_layout = 0;
313 XFontSetExtents *bar_fs_extents;
315 char *spawn_term[] = { NULL, NULL }; /* XXX fully dynamic */
318 #define SWM_MENU_FN (2)
319 #define SWM_MENU_NB (4)
320 #define SWM_MENU_NF (6)
321 #define SWM_MENU_SB (8)
322 #define SWM_MENU_SF (10)
324 /* layout manager data */
325 struct swm_geometry {
335 /* virtual "screens" */
337 TAILQ_ENTRY(swm_region) entry;
338 struct swm_geometry g;
339 struct workspace *ws; /* current workspace on this region */
340 struct workspace *ws_prior; /* prior workspace on this region */
341 struct swm_screen *s; /* screen idx */
344 TAILQ_HEAD(swm_region_list, swm_region);
347 TAILQ_ENTRY(ws_win) entry;
350 struct ws_win *child_trans; /* transient child window */
351 struct swm_geometry g; /* current geometry */
352 struct swm_geometry g_float; /* geometry when floating */
353 struct swm_geometry rg_float; /* region geom when floating */
354 int g_floatvalid; /* g_float geometry validity */
355 int floatmaxed; /* whether maxed by max_stack */
359 unsigned int ewmh_flags;
360 int font_size_boundary[SWM_MAX_FONT_STEPS];
366 unsigned long quirks;
367 struct workspace *ws; /* always valid */
368 struct swm_screen *s; /* always valid, never changes */
369 XWindowAttributes wa;
375 TAILQ_HEAD(ws_win_list, ws_win);
379 TAILQ_ENTRY(pid_e) entry;
383 TAILQ_HEAD(pid_list, pid_e);
384 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
386 /* layout handlers */
388 void vertical_config(struct workspace *, int);
389 void vertical_stack(struct workspace *, struct swm_geometry *);
390 void horizontal_config(struct workspace *, int);
391 void horizontal_stack(struct workspace *, struct swm_geometry *);
392 void max_stack(struct workspace *, struct swm_geometry *);
393 void plain_stacker(struct workspace *);
394 void fancy_stacker(struct workspace *);
396 struct ws_win *find_window(Window);
398 void grabbuttons(struct ws_win *, int);
399 void new_region(struct swm_screen *, int, int, int, int);
400 void unmanage_window(struct ws_win *);
401 long getstate(Window);
403 int conf_load(char *, int);
406 void (*l_stack)(struct workspace *, struct swm_geometry *);
407 void (*l_config)(struct workspace *, int);
409 #define SWM_L_FOCUSPREV (1<<0)
410 #define SWM_L_MAPONFOCUS (1<<1)
411 void (*l_string)(struct workspace *);
413 /* stack, configure */
414 { vertical_stack, vertical_config, 0, plain_stacker },
415 { horizontal_stack, horizontal_config, 0, plain_stacker },
417 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
418 { NULL, NULL, 0, NULL },
421 /* position of max_stack mode in the layouts array, index into layouts! */
422 #define SWM_V_STACK (0)
423 #define SWM_H_STACK (1)
424 #define SWM_MAX_STACK (2)
426 #define SWM_H_SLICE (32)
427 #define SWM_V_SLICE (32)
429 /* define work spaces */
431 int idx; /* workspace index */
432 char *name; /* workspace name */
433 int always_raise; /* raise windows on focus */
434 struct layout *cur_layout; /* current layout handlers */
435 struct ws_win *focus; /* may be NULL */
436 struct ws_win *focus_prev; /* may be NULL */
437 struct swm_region *r; /* may be NULL */
438 struct swm_region *old_r; /* may be NULL */
439 struct ws_win_list winlist; /* list of windows in ws */
440 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
441 char stacker[10]; /* display stacker and layout */
445 int horizontal_msize;
447 int horizontal_stacks;
456 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
457 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
459 /* physical screen mapping */
460 #define SWM_WS_MAX (22) /* hard limit */
461 int workspace_limit = 10; /* soft limit */
464 int idx; /* screen index */
465 struct swm_region_list rl; /* list of regions on this screen */
466 struct swm_region_list orl; /* list of old regions */
468 struct workspace ws[SWM_WS_MAX];
474 } c[SWM_S_COLOR_MAX];
478 struct swm_screen *screens;
480 /* args to functions */
483 #define SWM_ARG_ID_FOCUSNEXT (0)
484 #define SWM_ARG_ID_FOCUSPREV (1)
485 #define SWM_ARG_ID_FOCUSMAIN (2)
486 #define SWM_ARG_ID_FOCUSCUR (4)
487 #define SWM_ARG_ID_SWAPNEXT (10)
488 #define SWM_ARG_ID_SWAPPREV (11)
489 #define SWM_ARG_ID_SWAPMAIN (12)
490 #define SWM_ARG_ID_MOVELAST (13)
491 #define SWM_ARG_ID_MASTERSHRINK (20)
492 #define SWM_ARG_ID_MASTERGROW (21)
493 #define SWM_ARG_ID_MASTERADD (22)
494 #define SWM_ARG_ID_MASTERDEL (23)
495 #define SWM_ARG_ID_FLIPLAYOUT (24)
496 #define SWM_ARG_ID_STACKRESET (30)
497 #define SWM_ARG_ID_STACKINIT (31)
498 #define SWM_ARG_ID_CYCLEWS_UP (40)
499 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
500 #define SWM_ARG_ID_CYCLESC_UP (42)
501 #define SWM_ARG_ID_CYCLESC_DOWN (43)
502 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
503 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
504 #define SWM_ARG_ID_STACKINC (50)
505 #define SWM_ARG_ID_STACKDEC (51)
506 #define SWM_ARG_ID_SS_ALL (60)
507 #define SWM_ARG_ID_SS_WINDOW (61)
508 #define SWM_ARG_ID_DONTCENTER (70)
509 #define SWM_ARG_ID_CENTER (71)
510 #define SWM_ARG_ID_KILLWINDOW (80)
511 #define SWM_ARG_ID_DELETEWINDOW (81)
512 #define SWM_ARG_ID_WIDTHGROW (90)
513 #define SWM_ARG_ID_WIDTHSHRINK (91)
514 #define SWM_ARG_ID_HEIGHTGROW (92)
515 #define SWM_ARG_ID_HEIGHTSHRINK (93)
516 #define SWM_ARG_ID_MOVEUP (100)
517 #define SWM_ARG_ID_MOVEDOWN (101)
518 #define SWM_ARG_ID_MOVELEFT (102)
519 #define SWM_ARG_ID_MOVERIGHT (103)
523 void focus(struct swm_region *, union arg *);
524 void focus_magic(struct ws_win *);
528 TAILQ_ENTRY(quirk) entry;
532 #define SWM_Q_FLOAT (1<<0) /* float this window */
533 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
534 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
535 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
536 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
537 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
539 TAILQ_HEAD(quirk_list, quirk);
540 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
543 * Supported EWMH hints should be added to
544 * both the enum and the ewmh array
546 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
547 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
548 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
549 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
550 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
551 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
552 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
553 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
554 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
555 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
561 } ewmh[SWM_EWMH_HINT_MAX] = {
562 /* must be in same order as in the enum */
563 {"_NET_ACTIVE_WINDOW", None},
564 {"_NET_MOVERESIZE_WINDOW", None},
565 {"_NET_CLOSE_WINDOW", None},
566 {"_NET_WM_WINDOW_TYPE", None},
567 {"_NET_WM_WINDOW_TYPE_DOCK", None},
568 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
569 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
570 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
571 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
572 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
573 {"_NET_WM_STATE", None},
574 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
575 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
576 {"_NET_WM_STATE_SKIP_TASKBAR", None},
577 {"_NET_WM_STATE_SKIP_PAGER", None},
578 {"_NET_WM_STATE_HIDDEN", None},
579 {"_NET_WM_STATE_ABOVE", None},
580 {"_SWM_WM_STATE_MANUAL", None},
581 {"_NET_WM_STATE_FULLSCREEN", None},
582 {"_NET_WM_ALLOWED_ACTIONS", None},
583 {"_NET_WM_ACTION_MOVE", None},
584 {"_NET_WM_ACTION_RESIZE", None},
585 {"_NET_WM_ACTION_FULLSCREEN", None},
586 {"_NET_WM_ACTION_CLOSE", None},
589 void store_float_geom(struct ws_win *, struct swm_region *);
590 int floating_toggle_win(struct ws_win *);
591 void spawn_select(struct swm_region *, union arg *, char *, int *);
592 unsigned char *get_win_name(Window);
595 get_property(Window id, Atom atom, long count, Atom type, unsigned long *nitems,
596 unsigned long *nbytes, unsigned char **data)
599 unsigned long *nbytes_ret, *nitems_ret;
600 unsigned long nbytes_tmp, nitems_tmp;
603 nbytes_ret = nbytes != NULL ? nbytes : &nbytes_tmp;
604 nitems_ret = nitems != NULL ? nitems : &nitems_tmp;
606 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
607 &real, &format, nitems_ret, nbytes_ret, data);
609 if (status != Success)
618 update_iconic(struct ws_win *win, int newv)
625 iprop = XInternAtom(display, "_SWM_ICONIC", False);
629 XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
630 PropModeReplace, (unsigned char *)&v, 1);
632 XDeleteProperty(display, win->id, iprop);
636 get_iconic(struct ws_win *win)
641 unsigned long nitems, extra;
642 unsigned char *prop = NULL;
644 iprop = XInternAtom(display, "_SWM_ICONIC", False);
647 status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
648 False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
649 if (status != Success)
651 if (rettype != XA_INTEGER || retfmt != 32)
655 v = *((int32_t *)prop);
669 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
671 for (i = 0; i < LENGTH(ewmh); i++)
672 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
674 for (i = 0; i < ScreenCount(display); i++) {
675 /* Support check window will be created by workaround(). */
677 /* Report supported atoms */
678 XDeleteProperty(display, screens[i].root, sup_list);
679 for (j = 0; j < LENGTH(ewmh); j++)
680 XChangeProperty(display, screens[i].root,
681 sup_list, XA_ATOM, 32,
682 PropModeAppend, (unsigned char *)&ewmh[j].atom, 1);
690 unsigned char *data = NULL;
692 Atom sup_check, sup_list;
695 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
696 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
698 for (i = 0; i < ScreenCount(display); i++) {
699 /* Get the support check window and destroy it */
700 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
705 XDestroyWindow(display, id);
706 XDeleteProperty(display, screens[i].root, sup_check);
707 XDeleteProperty(display, screens[i].root, sup_list);
715 ewmh_autoquirk(struct ws_win *win)
718 unsigned long *data = NULL, n;
721 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
722 XA_ATOM, &n, NULL, (void *)&data);
729 for (i = 0; i < n; i++) {
731 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
733 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
734 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
735 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
737 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
740 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
741 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
743 win->quirks = SWM_Q_FLOAT;
751 #define SWM_EWMH_ACTION_COUNT_MAX (6)
752 #define EWMH_F_FULLSCREEN (1<<0)
753 #define EWMH_F_ABOVE (1<<1)
754 #define EWMH_F_HIDDEN (1<<2)
755 #define EWMH_F_SKIP_PAGER (1<<3)
756 #define EWMH_F_SKIP_TASKBAR (1<<4)
757 #define SWM_F_MANUAL (1<<5)
760 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
762 struct swm_geometry rg;
770 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%lx, "
771 "fullscreen %s\n", win->id, YESNO(fs));
776 store_float_geom(win, win->ws->r);
780 if (win->g_floatvalid) {
781 /* refloat at last floating relative position */
782 X(win) = win->g_float.x - win->rg_float.x + rg.x;
783 Y(win) = win->g_float.y - win->rg_float.y + rg.y;
784 WIDTH(win) = win->g_float.w;
785 HEIGHT(win) = win->g_float.h;
793 ewmh_update_actions(struct ws_win *win)
795 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
801 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
804 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
805 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
808 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
809 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
812 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
813 #define _NET_WM_STATE_ADD 1 /* add/set property */
814 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
817 ewmh_update_win_state(struct ws_win *win, long state, long action)
819 unsigned int mask = 0;
820 unsigned int changed = 0;
821 unsigned int orig_flags;
826 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
827 mask = EWMH_F_FULLSCREEN;
828 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
830 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
832 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
833 mask = EWMH_F_SKIP_PAGER;
834 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
835 mask = EWMH_F_SKIP_TASKBAR;
838 orig_flags = win->ewmh_flags;
841 case _NET_WM_STATE_REMOVE:
842 win->ewmh_flags &= ~mask;
844 case _NET_WM_STATE_ADD:
845 win->ewmh_flags |= mask;
847 case _NET_WM_STATE_TOGGLE:
848 win->ewmh_flags ^= mask;
852 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
854 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
856 if (!floating_toggle_win(win))
857 win->ewmh_flags = orig_flags; /* revert */
858 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
860 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
861 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
863 if (!ewmh_set_win_fullscreen(win,
864 win->ewmh_flags & EWMH_F_FULLSCREEN))
865 win->ewmh_flags = orig_flags; /* revert */
867 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
869 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
870 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
871 XA_ATOM, 32, PropModeAppend,
872 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
873 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
874 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
875 XA_ATOM, 32, PropModeAppend,
876 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
877 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
878 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
879 XA_ATOM, 32, PropModeAppend,
880 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
881 if (win->ewmh_flags & EWMH_F_ABOVE)
882 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
883 XA_ATOM, 32, PropModeAppend,
884 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
885 if (win->ewmh_flags & SWM_F_MANUAL)
886 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
887 XA_ATOM, 32, PropModeAppend,
888 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
892 ewmh_get_win_state(struct ws_win *win)
903 win->ewmh_flags |= EWMH_F_ABOVE;
905 win->ewmh_flags |= SWM_F_MANUAL;
907 success = get_property(win->id, ewmh[_NET_WM_STATE].atom,
908 (~0L), XA_ATOM, &n, NULL, (void *)&states);
913 for (i = 0; i < n; i++)
914 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
922 geteventname(XEvent *e)
934 name = "ButtonPress";
937 name = "ButtonRelease";
940 name = "MotionNotify";
943 name = "EnterNotify";
946 name = "LeaveNotify";
955 name = "KeymapNotify";
961 name = "GraphicsExpose";
966 case VisibilityNotify:
967 name = "VisibilityNotify";
970 name = "CreateNotify";
973 name = "DestroyNotify";
976 name = "UnmapNotify";
985 name = "ReparentNotify";
987 case ConfigureNotify:
988 name = "ConfigureNotify";
990 case ConfigureRequest:
991 name = "ConfigureRequest";
994 name = "GravityNotify";
997 name = "ResizeRequest";
999 case CirculateNotify:
1000 name = "CirculateNotify";
1002 case CirculateRequest:
1003 name = "CirculateRequest";
1005 case PropertyNotify:
1006 name = "PropertyNotify";
1008 case SelectionClear:
1009 name = "SelectionClear";
1011 case SelectionRequest:
1012 name = "SelectionRequest";
1014 case SelectionNotify:
1015 name = "SelectionNotify";
1017 case ColormapNotify:
1018 name = "ColormapNotify";
1021 name = "ClientMessage";
1024 name = "MappingNotify";
1034 xrandr_geteventname(XEvent *e)
1038 switch(e->type - xrandr_eventbase) {
1039 case RRScreenChangeNotify:
1040 name = "RRScreenChangeNotify";
1050 dumpwins(struct swm_region *r, union arg *args)
1054 XWindowAttributes wa;
1056 if (r->ws == NULL) {
1057 warnx("dumpwins: invalid workspace");
1061 warnx("=== managed window list ws %02d ===", r->ws->idx);
1063 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1064 state = getstate(win->id);
1065 if (!XGetWindowAttributes(display, win->id, &wa))
1066 warnx("window: 0x%lx, failed XGetWindowAttributes",
1068 warnx("window: 0x%lx, map_state: %d, state: %d, "
1069 "transient: 0x%lx", win->id, wa.map_state, state,
1073 warnx("===== unmanaged window list =====");
1074 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1075 state = getstate(win->id);
1076 if (!XGetWindowAttributes(display, win->id, &wa))
1077 warnx("window: 0x%lx, failed XGetWindowAttributes",
1079 warnx("window: 0x%lx, map_state: %d, state: %d, "
1080 "transient: 0x%lx", win->id, wa.map_state, state,
1084 warnx("=================================");
1088 dumpwins(struct swm_region *r, union arg *args)
1091 #endif /* SWM_DEBUG */
1093 void expose(XEvent *);
1094 void keypress(XEvent *);
1095 void buttonpress(XEvent *);
1096 void configurerequest(XEvent *);
1097 void configurenotify(XEvent *);
1098 void destroynotify(XEvent *);
1099 void enternotify(XEvent *);
1100 void focusevent(XEvent *);
1101 void mapnotify(XEvent *);
1102 void mappingnotify(XEvent *);
1103 void maprequest(XEvent *);
1104 void propertynotify(XEvent *);
1105 void unmapnotify(XEvent *);
1106 void visibilitynotify(XEvent *);
1107 void clientmessage(XEvent *);
1109 void (*handler[LASTEvent])(XEvent *) = {
1111 [KeyPress] = keypress,
1112 [ButtonPress] = buttonpress,
1113 [ConfigureRequest] = configurerequest,
1114 [ConfigureNotify] = configurenotify,
1115 [DestroyNotify] = destroynotify,
1116 [EnterNotify] = enternotify,
1117 [FocusIn] = focusevent,
1118 [FocusOut] = focusevent,
1119 [MapNotify] = mapnotify,
1120 [MappingNotify] = mappingnotify,
1121 [MapRequest] = maprequest,
1122 [PropertyNotify] = propertynotify,
1123 [UnmapNotify] = unmapnotify,
1124 [VisibilityNotify] = visibilitynotify,
1125 [ClientMessage] = clientmessage,
1131 int saved_errno, status;
1134 saved_errno = errno;
1138 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1143 if (errno != ECHILD)
1144 warn("sighdlr: waitpid");
1145 #endif /* SWM_DEBUG */
1148 if (pid == searchpid)
1152 if (WIFEXITED(status)) {
1153 if (WEXITSTATUS(status) != 0)
1154 warnx("sighdlr: child exit status: %d",
1155 WEXITSTATUS(status));
1157 warnx("sighdlr: child is terminated "
1159 #endif /* SWM_DEBUG */
1173 errno = saved_errno;
1179 struct pid_e *p = NULL;
1181 DNPRINTF(SWM_D_MISC, "find_pid: %lu\n", pid);
1186 TAILQ_FOREACH(p, &pidlist, entry) {
1195 name_to_color(char *colorname)
1199 XColor screen_def, exact_def;
1200 unsigned long result = 0;
1201 char cname[32] = "#";
1203 cmap = DefaultColormap(display, screens[0].idx);
1204 status = XAllocNamedColor(display, cmap, colorname,
1205 &screen_def, &exact_def);
1207 strlcat(cname, colorname + 2, sizeof cname - 1);
1208 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1212 result = screen_def.pixel;
1214 warnx("color '%s' not found", colorname);
1220 setscreencolor(char *val, int i, int c)
1222 if (i > 0 && i <= ScreenCount(display)) {
1223 screens[i - 1].c[c].color = name_to_color(val);
1224 free(screens[i - 1].c[c].name);
1225 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1227 } else if (i == -1) {
1228 for (i = 0; i < ScreenCount(display); i++) {
1229 screens[i].c[c].color = name_to_color(val);
1230 free(screens[i].c[c].name);
1231 if ((screens[i].c[c].name = strdup(val)) == NULL)
1235 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1236 i, ScreenCount(display));
1240 fancy_stacker(struct workspace *ws)
1242 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1243 if (ws->cur_layout->l_stack == vertical_stack)
1244 snprintf(ws->stacker, sizeof ws->stacker,
1245 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1246 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1247 if (ws->cur_layout->l_stack == horizontal_stack)
1248 snprintf(ws->stacker, sizeof ws->stacker,
1249 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1250 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1254 plain_stacker(struct workspace *ws)
1256 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1257 if (ws->cur_layout->l_stack == vertical_stack)
1258 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1259 sizeof ws->stacker);
1260 if (ws->cur_layout->l_stack == horizontal_stack)
1261 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1262 sizeof ws->stacker);
1266 custom_region(char *val)
1268 unsigned int sidx, x, y, w, h;
1270 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1271 errx(1, "invalid custom region, "
1272 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1273 if (sidx < 1 || sidx > ScreenCount(display))
1274 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1275 sidx, ScreenCount(display));
1279 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1281 if (x > DisplayWidth(display, sidx) ||
1282 y > DisplayHeight(display, sidx) ||
1283 w + x > DisplayWidth(display, sidx) ||
1284 h + y > DisplayHeight(display, sidx)) {
1285 warnx("ignoring region %ux%u+%u+%u - not within screen "
1286 "boundaries (%ux%u)", w, h, x, y,
1287 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1291 new_region(&screens[sidx], x, y, w, h);
1295 socket_setnonblock(int fd)
1299 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1300 err(1, "fcntl F_GETFL");
1301 flags |= O_NONBLOCK;
1302 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1303 err(1, "fcntl F_SETFL");
1307 bar_print(struct swm_region *r, const char *s)
1311 XRectangle ibox, lbox;
1313 XClearWindow(display, r->bar_window);
1316 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1318 switch (bar_justify) {
1319 case SWM_BAR_JUSTIFY_LEFT:
1322 case SWM_BAR_JUSTIFY_CENTER:
1323 x = (WIDTH(r) - lbox.width) / 2;
1325 case SWM_BAR_JUSTIFY_RIGHT:
1326 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1330 if (x < SWM_BAR_OFFSET)
1333 DRAWSTRING(display, r->bar_window, bar_fs, r->s->bar_gc,
1334 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1339 bar_extra_stop(void)
1343 bzero(bar_pipe, sizeof bar_pipe);
1346 kill(bar_pid, SIGTERM);
1349 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1354 bar_class_name(char *s, size_t sz, struct swm_region *r)
1356 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1358 if (r->ws->focus->ch.res_class != NULL)
1359 strlcat(s, r->ws->focus->ch.res_class, sz);
1363 bar_title_name(char *s, size_t sz, struct swm_region *r)
1365 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1367 if (r->ws->focus->ch.res_name != NULL)
1368 strlcat(s, r->ws->focus->ch.res_name, sz);
1372 bar_class_title_name(char *s, size_t sz, struct swm_region *r)
1374 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1377 bar_class_name(s, sz, r);
1378 strlcat(s, ":", sz);
1379 bar_title_name(s, sz, r);
1383 bar_window_float(char *s, size_t sz, struct swm_region *r)
1385 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
1387 if (r->ws->focus->floating)
1388 strlcat(s, "(f)", sz);
1392 bar_window_name(char *s, size_t sz, struct swm_region *r)
1394 unsigned char *title;
1396 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1398 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1401 strlcat(s, (char *)title, sz);
1405 int urgent[SWM_WS_MAX];
1407 bar_urgent(char *s, size_t sz)
1409 XWMHints *wmh = NULL;
1414 for (i = 0; i < workspace_limit; i++)
1417 for (i = 0; i < ScreenCount(display); i++)
1418 for (j = 0; j < workspace_limit; j++)
1419 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1420 wmh = XGetWMHints(display, win->id);
1424 if (wmh->flags & XUrgencyHint)
1429 for (i = 0; i < workspace_limit; i++) {
1431 snprintf(b, sizeof b, "%d ", i + 1);
1433 snprintf(b, sizeof b, "- ");
1439 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1441 if (r == NULL || r->ws == NULL)
1443 if (r->ws->name != NULL)
1444 strlcat(s, r->ws->name, sz);
1447 /* build the default bar format according to the defined enabled options */
1449 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1451 /* if format provided, just copy the buffers */
1452 if (bar_format != NULL) {
1453 strlcpy(fmtnew, fmtexp, sz);
1457 /* reset the output buffer */
1460 strlcat(fmtnew, "+N:+I ", sz);
1462 strlcat(fmtnew, "+S", sz);
1463 strlcat(fmtnew, " ", sz);
1465 /* only show the workspace name if there's actually one */
1466 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1467 strlcat(fmtnew, "<+D>", sz);
1468 strlcat(fmtnew, " ", sz);
1470 if (clock_enabled) {
1471 strlcat(fmtnew, fmtexp, sz);
1472 strlcat(fmtnew, " ", sz);
1475 /* bar_urgent already adds the space before the last asterisk */
1477 strlcat(fmtnew, "* +U* ", sz);
1479 if (title_class_enabled) {
1480 strlcat(fmtnew, "+C", sz);
1481 if (title_name_enabled == 0)
1482 strlcat(fmtnew, " ", sz);
1485 /* checks needed by the colon and floating strlcat(3) calls below */
1486 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
1487 if (title_name_enabled) {
1488 if (title_class_enabled)
1489 strlcat(fmtnew, ":", sz);
1490 strlcat(fmtnew, "+T ", sz);
1492 if (window_name_enabled) {
1493 if (r->ws->focus->floating)
1494 strlcat(fmtnew, "+F ", sz);
1495 strlcat(fmtnew, "+64W ", sz);
1499 /* finally add the action script output and the version */
1500 strlcat(fmtnew, " +A +V", sz);
1503 /* replaces the bar format character sequences (like in tmux(1)) */
1505 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1509 char num[8], tmp[SWM_BAR_MAX];
1511 size_t len, numoff = 0;
1513 /* reset strlcat(3) buffer */
1516 /* get number, if any */
1518 while (*fmt != '\0' && isdigit((unsigned char) *fmt)) {
1519 if (numoff >= sizeof num - 1)
1521 num[numoff++] = *fmt++;
1525 if ((limit = strtonum(num, 1, sizeof tmp - 1, NULL)) == 0)
1526 limit = sizeof tmp - 1;
1528 /* if number is too big, skip to the first non-digit */
1529 if (numoff >= sizeof num - 1) {
1530 while (*fmt != '\0' && isdigit((unsigned char) *fmt))
1533 /* there is nothing to replace (ie EOL) */
1539 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1542 bar_class_name(tmp, sizeof tmp, r);
1545 bar_workspace_name(tmp, sizeof tmp, r);
1548 bar_window_float(tmp, sizeof tmp, r);
1551 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1554 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1557 bar_class_title_name(tmp, sizeof tmp, r);
1560 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1563 bar_title_name(tmp, sizeof tmp, r);
1566 bar_urgent(tmp, sizeof tmp);
1569 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1572 bar_window_name(tmp, sizeof tmp, r);
1575 /* unknown character sequence; copy as-is */
1576 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1584 while (limit-- > 0) {
1585 if (*offrep >= sz - 1)
1587 fmtrep[(*offrep)++] = *ptr++;
1595 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1600 while (*fmt != '\0') {
1602 /* skip ordinary characters */
1605 fmtrep[off++] = *fmt++;
1609 /* character sequence found; replace it */
1610 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1619 bar_fmt_expand(char *fmtexp, size_t sz)
1626 /* start by grabbing the current time and date */
1628 localtime_r(&tmt, &tm);
1630 /* figure out what to expand */
1631 if (bar_format != NULL)
1633 else if (bar_format == NULL && clock_enabled)
1635 /* if nothing to expand bail out */
1641 /* copy as-is, just in case the format shouldn't be expanded below */
1642 strlcpy(fmtexp, fmt, sz);
1643 /* finally pass the string through strftime(3) */
1644 #ifndef SWM_DENY_CLOCK_FORMAT
1645 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1646 warnx("format too long");
1654 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1655 char fmtrep[SWM_BAR_MAX];
1657 struct swm_region *r;
1659 /* expand the format by first passing it through strftime(3) */
1660 bar_fmt_expand(fmtexp, sizeof fmtexp);
1662 for (i = 0; i < ScreenCount(display); i++) {
1663 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1664 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1665 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1666 bar_print(r, fmtrep);
1677 if (bar_enabled == 0)
1679 if (bar_extra && bar_extra_running) {
1680 /* ignore short reads; it'll correct itself */
1681 while ((b = fgetln(stdin, &len)) != NULL)
1682 if (b && b[len - 1] == '\n') {
1684 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1686 if (b == NULL && errno != EAGAIN) {
1687 warn("bar_update: bar_extra failed");
1691 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1704 bar_toggle(struct swm_region *r, union arg *args)
1706 struct swm_region *tmpr;
1707 int i, sc = ScreenCount(display);
1709 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1712 for (i = 0; i < sc; i++)
1713 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1714 XUnmapWindow(display, tmpr->bar_window);
1716 for (i = 0; i < sc; i++)
1717 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1718 XMapRaised(display, tmpr->bar_window);
1720 bar_enabled = !bar_enabled;
1723 /* must be after stack */
1730 XSetWindowAttributes wa;
1731 struct swm_region *r;
1734 /* do this here because the conf file is in memory */
1735 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1736 /* launch external status app */
1737 bar_extra_running = 1;
1738 if (pipe(bar_pipe) == -1)
1739 err(1, "pipe error");
1740 socket_setnonblock(bar_pipe[0]);
1741 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1742 if (dup2(bar_pipe[0], 0) == -1)
1744 if (dup2(bar_pipe[1], 1) == -1)
1746 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1747 err(1, "could not disable SIGPIPE");
1748 switch (bar_pid = fork()) {
1750 err(1, "cannot fork");
1754 execvp(bar_argv[0], bar_argv);
1755 err(1, "%s external app failed", bar_argv[0]);
1757 default: /* parent */
1763 bzero(&wa, sizeof wa);
1764 for (i = 0; i < ScreenCount(display); i++)
1765 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1767 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1768 wa.background_pixel =
1769 screens[i].c[SWM_S_COLOR_BAR].color;
1770 XChangeWindowAttributes(display, r->bar_window,
1771 CWBackPixel | CWBorderPixel, &wa);
1777 bar_setup(struct swm_region *r)
1779 char *default_string;
1780 char **missing_charsets;
1781 int num_missing_charsets = 0;
1785 XFreeFontSet(display, bar_fs);
1790 DNPRINTF(SWM_D_BAR, "bar_setup: loading bar_fonts: %s\n", bar_fonts);
1792 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
1793 &num_missing_charsets, &default_string);
1795 if (num_missing_charsets > 0) {
1796 warnx("Unable to load charset(s):");
1798 for (i = 0; i < num_missing_charsets; ++i)
1799 warnx("%s", missing_charsets[i]);
1801 XFreeStringList(missing_charsets);
1803 if (strcmp(default_string, ""))
1804 warnx("Glyphs from those sets will be replaced "
1805 "by '%s'.", default_string);
1807 warnx("Glyphs from those sets won't be drawn.");
1811 errx(1, "Error creating font set structure.");
1813 bar_fs_extents = XExtentsOfFontSet(bar_fs);
1815 bar_height = bar_fs_extents->max_logical_extent.height +
1816 2 * bar_border_width;
1822 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1824 r->bar_window = XCreateSimpleWindow(display,
1825 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width,
1826 bar_height - 2 * bar_border_width,
1827 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1828 r->s->c[SWM_S_COLOR_BAR].color);
1829 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1831 XMapRaised(display, r->bar_window);
1832 DNPRINTF(SWM_D_BAR, "bar_setup: bar_window: 0x%lx\n", r->bar_window);
1834 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1835 err(1, "could not install bar_signal");
1840 drain_enter_notify(void)
1845 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1848 DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained: %d\n", i);
1852 set_win_state(struct ws_win *win, long state)
1854 long data[] = {state, None};
1856 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%lx\n", win->id);
1861 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1862 (unsigned char *)data, 2);
1869 unsigned char *p = NULL;
1872 if (!get_property(w, astate, 2L, astate, &n, NULL, &p))
1875 result = *((long *)p);
1881 version(struct swm_region *r, union arg *args)
1883 bar_version = !bar_version;
1885 snprintf(bar_vertext, sizeof bar_vertext,
1886 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
1888 strlcpy(bar_vertext, "", sizeof bar_vertext);
1893 client_msg(struct ws_win *win, Atom a)
1895 XClientMessageEvent cm;
1900 bzero(&cm, sizeof cm);
1901 cm.type = ClientMessage;
1902 cm.window = win->id;
1903 cm.message_type = aprot;
1906 cm.data.l[1] = CurrentTime;
1907 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1910 /* synthetic response to a ConfigureRequest when not making a change */
1912 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1919 /* send notification of unchanged state. */
1920 ce.type = ConfigureNotify;
1923 ce.width = WIDTH(win);
1924 ce.height = HEIGHT(win);
1925 ce.override_redirect = False;
1929 ce.display = display;
1931 ce.window = win->id;
1932 ce.border_width = border_width;
1936 ce.display = ev->display;
1937 ce.event = ev->window;
1938 ce.window = ev->window;
1940 /* make response appear more WM_SIZE_HINTS-compliant */
1942 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%lx,"
1943 " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
1944 "%d x %d\n", win->id, win->sh_mask, SH_MIN_W(win),
1945 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
1946 SH_INC_W(win), SH_INC_H(win));
1950 /* the hint may be set... to 0! */
1951 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
1952 ce.width = SH_MIN_W(win);
1953 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
1954 ce.height = SH_MIN_H(win);
1959 /* may also be advertized as 0 */
1960 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
1961 ce.width = SH_MAX_W(win);
1962 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
1963 ce.height = SH_MAX_H(win);
1966 /* resize increment. */
1968 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
1969 ce.width -= (ce.width - SH_MIN_W(win)) %
1971 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
1972 ce.height -= (ce.height - SH_MIN_H(win)) %
1976 /* adjust x and y for requested border_width. */
1977 ce.x += border_width - ev->border_width;
1978 ce.y += border_width - ev->border_width;
1979 ce.border_width = ev->border_width;
1980 ce.above = ev->above;
1983 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%lx, (x,y) w x h: "
1984 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
1985 ce.y, ce.width, ce.height, ce.border_width);
1987 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1991 count_win(struct workspace *ws, int count_transient)
1996 TAILQ_FOREACH(win, &ws->winlist, entry) {
1997 if (count_transient == 0 && win->floating)
1999 if (count_transient == 0 && win->transient)
2005 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2011 quit(struct swm_region *r, union arg *args)
2013 DNPRINTF(SWM_D_MISC, "quit\n");
2018 unmap_window(struct ws_win *win)
2023 /* don't unmap again */
2024 if (getstate(win->id) == IconicState)
2027 set_win_state(win, IconicState);
2029 XUnmapWindow(display, win->id);
2030 XSetWindowBorder(display, win->id,
2031 win->s->c[SWM_S_COLOR_UNFOCUS].color);
2040 for (i = 0; i < ScreenCount(display); i++)
2041 for (j = 0; j < workspace_limit; j++)
2042 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2047 fake_keypress(struct ws_win *win, int keysym, int modifiers)
2054 event.display = display; /* Ignored, but what the hell */
2055 event.window = win->id;
2056 event.root = win->s->root;
2057 event.subwindow = None;
2058 event.time = CurrentTime;
2063 event.same_screen = True;
2064 event.keycode = XKeysymToKeycode(display, keysym);
2065 event.state = modifiers;
2067 event.type = KeyPress;
2068 XSendEvent(event.display, event.window, True,
2069 KeyPressMask, (XEvent *)&event);
2071 event.type = KeyRelease;
2072 XSendEvent(event.display, event.window, True,
2073 KeyPressMask, (XEvent *)&event);
2078 restart(struct swm_region *r, union arg *args)
2080 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2082 /* disable alarm because the following code may not be interrupted */
2084 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
2085 err(1, "can't disable alarm");
2090 XCloseDisplay(display);
2091 execvp(start_argv[0], start_argv);
2092 warn("execvp failed");
2097 root_to_region(Window root)
2099 struct swm_region *r = NULL;
2101 int i, x, y, wx, wy;
2104 for (i = 0; i < ScreenCount(display); i++)
2105 if (screens[i].root == root)
2108 if (XQueryPointer(display, screens[i].root,
2109 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
2110 /* choose a region based on pointer location */
2111 TAILQ_FOREACH(r, &screens[i].rl, entry)
2112 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
2113 y >= Y(r) && y <= Y(r) + HEIGHT(r))
2118 r = TAILQ_FIRST(&screens[i].rl);
2124 find_unmanaged_window(Window id)
2129 for (i = 0; i < ScreenCount(display); i++)
2130 for (j = 0; j < workspace_limit; j++)
2131 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2139 find_window(Window id)
2142 Window wrr, wpr, *wcr = NULL;
2146 for (i = 0; i < ScreenCount(display); i++)
2147 for (j = 0; j < workspace_limit; j++)
2148 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2152 /* if we were looking for the parent return that window instead */
2153 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
2158 /* ignore not found and root */
2159 if (wpr == 0 || wrr == wpr)
2162 /* look for parent */
2163 for (i = 0; i < ScreenCount(display); i++)
2164 for (j = 0; j < workspace_limit; j++)
2165 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2173 spawn(int ws_idx, union arg *args, int close_fd)
2178 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2181 close(ConnectionNumber(display));
2183 setenv("LD_PRELOAD", SWM_LIB, 1);
2185 if (asprintf(&ret, "%d", ws_idx) == -1) {
2186 warn("spawn: asprintf SWM_WS");
2189 setenv("_SWM_WS", ret, 1);
2193 if (asprintf(&ret, "%d", getpid()) == -1) {
2194 warn("spawn: asprintf _SWM_PID");
2197 setenv("_SWM_PID", ret, 1);
2201 if (setsid() == -1) {
2202 warn("spawn: setsid");
2208 * close stdin and stdout to prevent interaction between apps
2209 * and the baraction script
2210 * leave stderr open to record errors
2212 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2213 warn("spawn: open");
2216 dup2(fd, STDIN_FILENO);
2217 dup2(fd, STDOUT_FILENO);
2222 execvp(args->argv[0], args->argv);
2224 warn("spawn: execvp");
2229 spawnterm(struct swm_region *r, union arg *args)
2231 DNPRINTF(SWM_D_MISC, "spawnterm\n");
2235 setenv("_SWM_XTERM_FONTADJ", "", 1);
2236 spawn(r->ws->idx, args, 1);
2241 kill_refs(struct ws_win *win)
2244 struct swm_region *r;
2245 struct workspace *ws;
2250 for (i = 0; i < ScreenCount(display); i++)
2251 TAILQ_FOREACH(r, &screens[i].rl, entry)
2252 for (x = 0; x < workspace_limit; x++) {
2254 if (win == ws->focus)
2256 if (win == ws->focus_prev)
2257 ws->focus_prev = NULL;
2262 validate_win(struct ws_win *testwin)
2265 struct workspace *ws;
2266 struct swm_region *r;
2269 if (testwin == NULL)
2272 for (i = 0; i < ScreenCount(display); i++)
2273 TAILQ_FOREACH(r, &screens[i].rl, entry)
2274 for (x = 0; x < workspace_limit; x++) {
2276 TAILQ_FOREACH(win, &ws->winlist, entry)
2284 validate_ws(struct workspace *testws)
2286 struct swm_region *r;
2287 struct workspace *ws;
2290 /* validate all ws */
2291 for (i = 0; i < ScreenCount(display); i++)
2292 TAILQ_FOREACH(r, &screens[i].rl, entry)
2293 for (x = 0; x < workspace_limit; x++) {
2302 unfocus_win(struct ws_win *win)
2307 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%lx\n", WINID(win));
2311 if (win->ws == NULL)
2314 if (validate_ws(win->ws))
2315 return; /* XXX this gets hit with thunderbird, needs fixing */
2317 if (win->ws->r == NULL)
2320 if (validate_win(win)) {
2325 if (win->ws->focus == win) {
2326 win->ws->focus = NULL;
2327 win->ws->focus_prev = win;
2330 if (validate_win(win->ws->focus)) {
2331 kill_refs(win->ws->focus);
2332 win->ws->focus = NULL;
2334 if (validate_win(win->ws->focus_prev)) {
2335 kill_refs(win->ws->focus_prev);
2336 win->ws->focus_prev = NULL;
2339 /* drain all previous unfocus events */
2340 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
2343 grabbuttons(win, 0);
2344 XSetWindowBorder(display, win->id,
2345 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2347 XChangeProperty(display, win->s->root,
2348 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2349 PropModeReplace, (unsigned char *)&none, 1);
2358 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2360 for (i = 0; i < ScreenCount(display); i++)
2361 for (j = 0; j < workspace_limit; j++)
2362 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2367 focus_win(struct ws_win *win)
2372 struct ws_win *cfw = NULL;
2375 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%lx\n", WINID(win));
2379 if (win->ws == NULL)
2382 if (validate_ws(win->ws))
2383 return; /* XXX this gets hit with thunderbird, needs fixing */
2385 if (validate_win(win)) {
2390 if (validate_win(win)) {
2395 XGetInputFocus(display, &cur_focus, &rr);
2396 if ((cfw = find_window(cur_focus)) != NULL)
2399 /* use larger hammer since the window was killed somehow */
2400 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2401 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2402 XSetWindowBorder(display, cfw->id,
2403 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2406 win->ws->focus = win;
2408 if (win->ws->r != NULL) {
2409 /* drain all previous focus events */
2410 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
2414 XSetInputFocus(display, win->id,
2415 RevertToParent, CurrentTime);
2416 grabbuttons(win, 1);
2417 XSetWindowBorder(display, win->id,
2418 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2419 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2420 win->ws->always_raise)
2421 XMapRaised(display, win->id);
2423 XChangeProperty(display, win->s->root,
2424 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2425 PropModeReplace, (unsigned char *)&win->id, 1);
2432 switchws(struct swm_region *r, union arg *args)
2434 int wsid = args->id, unmap_old = 0;
2435 struct swm_region *this_r, *other_r;
2437 struct workspace *new_ws, *old_ws;
2443 if (wsid >= workspace_limit)
2447 old_ws = this_r->ws;
2448 new_ws = &this_r->s->ws[wsid];
2450 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2451 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2453 if (new_ws == NULL || old_ws == NULL)
2455 if (new_ws == old_ws)
2458 other_r = new_ws->r;
2459 if (other_r == NULL) {
2460 /* the other workspace is hidden, hide this one */
2464 /* the other ws is visible in another region, exchange them */
2465 other_r->ws_prior = new_ws;
2466 other_r->ws = old_ws;
2467 old_ws->r = other_r;
2469 this_r->ws_prior = old_ws;
2470 this_r->ws = new_ws;
2473 /* this is needed so that we can click on a window after a restart */
2477 a.id = SWM_ARG_ID_FOCUSCUR;
2478 focus(new_ws->r, &a);
2480 /* unmap old windows */
2482 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2485 if (focus_mode == SWM_FOCUS_DEFAULT)
2486 drain_enter_notify();
2490 cyclews(struct swm_region *r, union arg *args)
2493 struct swm_screen *s = r->s;
2496 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2497 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2502 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2505 case SWM_ARG_ID_CYCLEWS_UP:
2506 if (a.id < workspace_limit - 1)
2511 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2514 case SWM_ARG_ID_CYCLEWS_DOWN:
2518 a.id = workspace_limit - 1;
2525 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2527 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2531 } while (a.id != r->ws->idx);
2535 priorws(struct swm_region *r, union arg *args)
2539 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2540 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2542 if (r->ws_prior == NULL)
2545 a.id = r->ws_prior->idx;
2550 cyclescr(struct swm_region *r, union arg *args)
2552 struct swm_region *rr = NULL;
2556 /* do nothing if we don't have more than one screen */
2557 if (!(ScreenCount(display) > 1 || outputs > 1))
2562 case SWM_ARG_ID_CYCLESC_UP:
2563 rr = TAILQ_NEXT(r, entry);
2565 rr = TAILQ_FIRST(&screens[i].rl);
2567 case SWM_ARG_ID_CYCLESC_DOWN:
2568 rr = TAILQ_PREV(r, swm_region_list, entry);
2570 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2578 /* move mouse to region */
2580 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2581 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2583 a.id = SWM_ARG_ID_FOCUSCUR;
2586 if (rr->ws->focus) {
2587 /* move to focus window */
2588 x = X(rr->ws->focus) + 1;
2589 y = Y(rr->ws->focus) + 1;
2590 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2595 sort_windows(struct ws_win_list *wl)
2597 struct ws_win *win, *parent, *nxt;
2602 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2603 nxt = TAILQ_NEXT(win, entry);
2604 if (win->transient) {
2605 parent = find_window(win->transient);
2606 if (parent == NULL) {
2607 warnx("not possible bug");
2610 TAILQ_REMOVE(wl, win, entry);
2611 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2618 swapwin(struct swm_region *r, union arg *args)
2620 struct ws_win *target, *source;
2621 struct ws_win *cur_focus;
2622 struct ws_win_list *wl;
2625 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2626 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2628 cur_focus = r->ws->focus;
2629 if (cur_focus == NULL)
2633 wl = &source->ws->winlist;
2636 case SWM_ARG_ID_SWAPPREV:
2637 if (source->transient)
2638 source = find_window(source->transient);
2639 target = TAILQ_PREV(source, ws_win_list, entry);
2640 if (target && target->transient)
2641 target = find_window(target->transient);
2642 TAILQ_REMOVE(wl, source, entry);
2644 TAILQ_INSERT_TAIL(wl, source, entry);
2646 TAILQ_INSERT_BEFORE(target, source, entry);
2648 case SWM_ARG_ID_SWAPNEXT:
2649 target = TAILQ_NEXT(source, entry);
2650 /* move the parent and let the sort handle the move */
2651 if (source->transient)
2652 source = find_window(source->transient);
2653 TAILQ_REMOVE(wl, source, entry);
2655 TAILQ_INSERT_HEAD(wl, source, entry);
2657 TAILQ_INSERT_AFTER(wl, target, source, entry);
2659 case SWM_ARG_ID_SWAPMAIN:
2660 target = TAILQ_FIRST(wl);
2661 if (target == source) {
2662 if (source->ws->focus_prev != NULL &&
2663 source->ws->focus_prev != target)
2664 source = source->ws->focus_prev;
2668 if (target == NULL || source == NULL)
2670 source->ws->focus_prev = target;
2671 TAILQ_REMOVE(wl, target, entry);
2672 TAILQ_INSERT_BEFORE(source, target, entry);
2673 TAILQ_REMOVE(wl, source, entry);
2674 TAILQ_INSERT_HEAD(wl, source, entry);
2676 case SWM_ARG_ID_MOVELAST:
2677 TAILQ_REMOVE(wl, source, entry);
2678 TAILQ_INSERT_TAIL(wl, source, entry);
2681 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2691 focus_prev(struct ws_win *win)
2693 struct ws_win *winfocus = NULL;
2694 struct ws_win *cur_focus = NULL;
2695 struct ws_win_list *wl = NULL;
2696 struct workspace *ws = NULL;
2698 if (!(win && win->ws))
2703 cur_focus = ws->focus;
2705 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%lx, cur_focus: 0x%lx\n",
2706 WINID(win), WINID(cur_focus));
2708 /* pickle, just focus on whatever */
2709 if (cur_focus == NULL) {
2710 /* use prev_focus if valid */
2711 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2712 find_window(WINID(ws->focus_prev)))
2713 winfocus = ws->focus_prev;
2717 /* if transient focus on parent */
2718 if (cur_focus->transient) {
2719 winfocus = find_window(cur_focus->transient);
2723 /* if in max_stack try harder */
2724 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2725 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2726 if (cur_focus != ws->focus_prev)
2727 winfocus = ws->focus_prev;
2729 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2734 DNPRINTF(SWM_D_FOCUS, "focus_prev: focus_close: %d\n", focus_close);
2736 if (winfocus == NULL || winfocus == win) {
2737 switch (focus_close) {
2738 case SWM_STACK_BOTTOM:
2739 winfocus = TAILQ_FIRST(wl);
2742 winfocus = TAILQ_LAST(wl, ws_win_list);
2744 case SWM_STACK_ABOVE:
2745 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
2746 if (focus_close_wrap)
2747 winfocus = TAILQ_FIRST(wl);
2749 winfocus = TAILQ_PREV(cur_focus,
2750 ws_win_list, entry);
2753 case SWM_STACK_BELOW:
2754 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
2756 if (focus_close_wrap)
2757 winfocus = TAILQ_LAST(wl, ws_win_list);
2759 winfocus = TAILQ_NEXT(cur_focus, entry);
2765 if (winfocus == NULL) {
2766 if (focus_default == SWM_STACK_TOP)
2767 winfocus = TAILQ_LAST(wl, ws_win_list);
2769 winfocus = TAILQ_FIRST(wl);
2773 focus_magic(winfocus);
2777 focus(struct swm_region *r, union arg *args)
2779 struct ws_win *winfocus = NULL, *head;
2780 struct ws_win *cur_focus = NULL;
2781 struct ws_win_list *wl = NULL;
2782 struct workspace *ws = NULL;
2788 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
2790 /* treat FOCUS_CUR special */
2791 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2792 if (r->ws->focus && r->ws->focus->iconic == 0)
2793 winfocus = r->ws->focus;
2794 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2795 winfocus = r->ws->focus_prev;
2797 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2798 if (winfocus->iconic == 0)
2801 focus_magic(winfocus);
2805 if ((cur_focus = r->ws->focus) == NULL)
2809 if (TAILQ_EMPTY(wl))
2811 /* make sure there is at least one uniconified window */
2813 TAILQ_FOREACH(winfocus, wl, entry)
2814 if (winfocus->iconic == 0) {
2822 case SWM_ARG_ID_FOCUSPREV:
2823 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2825 head = TAILQ_LAST(wl, ws_win_list);
2827 if (WINID(winfocus) == cur_focus->transient) {
2828 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2830 head = TAILQ_LAST(wl, ws_win_list);
2835 if (winfocus && winfocus->iconic) {
2836 while (winfocus != cur_focus) {
2837 if (winfocus == NULL)
2838 winfocus = TAILQ_LAST(wl, ws_win_list);
2839 if (winfocus->iconic == 0)
2841 winfocus = TAILQ_PREV(winfocus, ws_win_list,
2847 case SWM_ARG_ID_FOCUSNEXT:
2848 head = TAILQ_NEXT(cur_focus, entry);
2850 head = TAILQ_FIRST(wl);
2854 if (winfocus && winfocus->iconic) {
2855 while (winfocus != cur_focus) {
2856 if (winfocus == NULL)
2857 winfocus = TAILQ_FIRST(wl);
2858 if (winfocus->iconic == 0)
2860 winfocus = TAILQ_NEXT(winfocus, entry);
2865 case SWM_ARG_ID_FOCUSMAIN:
2866 winfocus = TAILQ_FIRST(wl);
2867 if (winfocus == cur_focus)
2868 winfocus = cur_focus->ws->focus_prev;
2875 focus_magic(winfocus);
2879 cycle_layout(struct swm_region *r, union arg *args)
2881 struct workspace *ws = r->ws;
2884 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2887 if (ws->cur_layout->l_stack == NULL)
2888 ws->cur_layout = &layouts[0];
2891 if (focus_mode == SWM_FOCUS_DEFAULT)
2892 drain_enter_notify();
2893 a.id = SWM_ARG_ID_FOCUSCUR;
2898 stack_config(struct swm_region *r, union arg *args)
2900 struct workspace *ws = r->ws;
2902 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
2905 if (ws->cur_layout->l_config != NULL)
2906 ws->cur_layout->l_config(ws, args->id);
2908 if (args->id != SWM_ARG_ID_STACKINIT)
2915 struct swm_geometry g;
2916 struct swm_region *r;
2922 DNPRINTF(SWM_D_STACK, "stack: begin\n");
2924 for (i = 0; i < ScreenCount(display); i++) {
2928 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2929 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
2930 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
2932 /* start with screen geometry, adjust for bar */
2934 g.w -= 2 * border_width;
2935 g.h -= 2 * border_width;
2941 r->ws->cur_layout->l_stack(r->ws, &g);
2942 r->ws->cur_layout->l_string(r->ws);
2943 /* save r so we can track region changes */
2950 if (focus_mode == SWM_FOCUS_DEFAULT)
2951 drain_enter_notify();
2953 DNPRINTF(SWM_D_STACK, "stack: end\n");
2957 store_float_geom(struct ws_win *win, struct swm_region *r)
2959 /* retain window geom and region geom */
2960 win->g_float = win->g;
2961 win->rg_float = r->g;
2962 win->g_floatvalid = 1;
2966 stack_floater(struct ws_win *win, struct swm_region *r)
2974 bzero(&wc, sizeof wc);
2975 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2978 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2979 * geom on ws switches or return from max mode
2981 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2982 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2984 * use stored g and rg to set relative position and size
2985 * as in old region or before max stack mode
2987 X(win) = win->g_float.x - win->rg_float.x + X(r);
2988 Y(win) = win->g_float.y - win->rg_float.y + Y(r);
2989 WIDTH(win) = win->g_float.w;
2990 HEIGHT(win) = win->g_float.h;
2991 win->g_floatvalid = 0;
2994 win->floatmaxed = 0;
2996 if ((win->quirks & SWM_Q_FULLSCREEN) && (WIDTH(win) >= WIDTH(r)) &&
2997 (HEIGHT(win) >= HEIGHT(r)))
2998 wc.border_width = 0;
3000 wc.border_width = border_width;
3001 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
3002 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
3003 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
3008 * floaters and transients are auto-centred unless moved
3011 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - wc.border_width;
3012 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - wc.border_width;
3015 /* win can be outside r if new r smaller than old r */
3016 /* Ensure top left corner inside r (move probs otherwise) */
3017 if (X(win) < X(r) - wc.border_width)
3018 X(win) = X(r) - wc.border_width;
3019 if (X(win) > X(r) + WIDTH(r) - 1)
3020 X(win) = (WIDTH(win) > WIDTH(r)) ? X(r) :
3021 (X(r) + WIDTH(r) - WIDTH(win) - 2 * wc.border_width);
3022 if (Y(win) < Y(r) - wc.border_width)
3023 Y(win) = Y(r) - wc.border_width;
3024 if (Y(win) > Y(r) + HEIGHT(r) - 1)
3025 Y(win) = (HEIGHT(win) > HEIGHT(r)) ? Y(r) :
3026 (Y(r) + HEIGHT(r) - HEIGHT(win) - 2 * wc.border_width);
3030 wc.width = WIDTH(win);
3031 wc.height = HEIGHT(win);
3034 * Retain floater and transient geometry for correct positioning
3035 * when ws changes region
3037 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
3038 store_float_geom(win, r);
3040 DNPRINTF(SWM_D_MISC, "stack_floater: window: %lu, (x,y) w x h: (%d,%d) "
3041 "%d x %d\n", win->id, wc.x, wc.y, wc.width, wc.height);
3043 XConfigureWindow(display, win->id, mask, &wc);
3047 * Send keystrokes to terminal to decrease/increase the font size as the
3048 * window size changes.
3051 adjust_font(struct ws_win *win)
3053 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3054 win->floating || win->transient)
3057 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3058 WIDTH(win) / win->sh.width_inc < term_width &&
3059 win->font_steps < SWM_MAX_FONT_STEPS) {
3060 win->font_size_boundary[win->font_steps] =
3061 (win->sh.width_inc * term_width) + win->sh.base_width;
3064 win->last_inc = win->sh.width_inc;
3065 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3066 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3067 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3070 win->last_inc = win->sh.width_inc;
3071 fake_keypress(win, XK_KP_Add, ShiftMask);
3075 #define SWAPXY(g) do { \
3077 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3078 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3081 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3084 XWindowAttributes wa;
3085 struct swm_geometry win_g, r_g = *g;
3086 struct ws_win *win, *fs_win = 0;
3087 int i, j, s, stacks;
3088 int w_inc = 1, h_inc, w_base = 1, h_base;
3089 int hrh, extra = 0, h_slice, last_h = 0;
3090 int split, colno, winno, mwin, msize, mscale;
3091 int remain, missing, v_slice, reconfigure;
3094 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3095 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3097 winno = count_win(ws, 0);
3098 if (winno == 0 && count_win(ws, 1) == 0)
3101 TAILQ_FOREACH(win, &ws->winlist, entry)
3102 if (win->transient == 0 && win->floating == 0
3103 && win->iconic == 0)
3110 w_inc = win->sh.width_inc;
3111 w_base = win->sh.base_width;
3112 mwin = ws->l_state.horizontal_mwin;
3113 mscale = ws->l_state.horizontal_msize;
3114 stacks = ws->l_state.horizontal_stacks;
3117 w_inc = win->sh.height_inc;
3118 w_base = win->sh.base_height;
3119 mwin = ws->l_state.vertical_mwin;
3120 mscale = ws->l_state.vertical_msize;
3121 stacks = ws->l_state.vertical_stacks;
3125 if (stacks > winno - mwin)
3126 stacks = winno - mwin;
3130 h_slice = r_g.h / SWM_H_SLICE;
3131 if (mwin && winno > mwin) {
3132 v_slice = r_g.w / SWM_V_SLICE;
3136 win_g.w = v_slice * mscale;
3138 if (w_inc > 1 && w_inc < v_slice) {
3139 /* adjust for window's requested size increment */
3140 remain = (win_g.w - w_base) % w_inc;
3146 win_g.x += r_g.w - msize;
3149 colno = split = winno / stacks;
3150 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3151 2 * border_width) / stacks);
3153 hrh = r_g.h / colno;
3154 extra = r_g.h - (colno * hrh);
3155 win_g.h = hrh - 2 * border_width;
3157 /* stack all the tiled windows */
3158 i = j = 0, s = stacks;
3159 TAILQ_FOREACH(win, &ws->winlist, entry) {
3160 if (win->transient != 0 || win->floating != 0)
3162 if (win->iconic != 0)
3165 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3170 if (split && i == split) {
3171 colno = (winno - mwin) / stacks;
3172 if (s <= (winno - mwin) % stacks)
3174 split = split + colno;
3175 hrh = (r_g.h / colno);
3176 extra = r_g.h - (colno * hrh);
3180 win_g.x += win_g.w + 2 * border_width;
3181 win_g.w = (r_g.w - msize -
3182 (stacks * 2 * border_width)) / stacks;
3184 win_g.w += (r_g.w - msize -
3185 (stacks * 2 * border_width)) % stacks;
3189 win_g.h = hrh - 2 * border_width;
3191 h_inc = win->sh.width_inc;
3192 h_base = win->sh.base_width;
3194 h_inc = win->sh.height_inc;
3195 h_base = win->sh.base_height;
3197 if (j == colno - 1) {
3198 win_g.h = hrh + extra;
3199 } else if (h_inc > 1 && h_inc < h_slice) {
3200 /* adjust for window's requested size increment */
3201 remain = (win_g.h - h_base) % h_inc;
3202 missing = h_inc - remain;
3204 if (missing <= extra || j == 0) {
3216 win_g.y += last_h + 2 * border_width;
3218 bzero(&wc, sizeof wc);
3219 if (disable_border && bar_enabled == 0 && winno == 1){
3220 wc.border_width = 0;
3221 win_g.w += 2 * border_width;
3222 win_g.h += 2 * border_width;
3224 wc.border_width = border_width;
3227 if (X(win) != win_g.y || Y(win) != win_g.x ||
3228 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3230 X(win) = wc.x = win_g.y;
3231 Y(win) = wc.y = win_g.x;
3232 WIDTH(win) = wc.width = win_g.h;
3233 HEIGHT(win) = wc.height = win_g.w;
3236 if (X(win) != win_g.x || Y(win) != win_g.y ||
3237 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3239 X(win) = wc.x = win_g.x;
3240 Y(win) = wc.y = win_g.y;
3241 WIDTH(win) = wc.width = win_g.w;
3242 HEIGHT(win) = wc.height = win_g.h;
3247 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3248 XConfigureWindow(display, win->id, mask, &wc);
3251 if (XGetWindowAttributes(display, win->id, &wa))
3252 if (wa.map_state == IsUnmapped)
3253 XMapRaised(display, win->id);
3261 /* now, stack all the floaters and transients */
3262 TAILQ_FOREACH(win, &ws->winlist, entry) {
3263 if (win->transient == 0 && win->floating == 0)
3265 if (win->iconic == 1)
3267 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3272 stack_floater(win, ws->r);
3273 XMapRaised(display, win->id);
3277 stack_floater(fs_win, ws->r);
3278 XMapRaised(display, fs_win->id);
3283 vertical_config(struct workspace *ws, int id)
3285 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3289 case SWM_ARG_ID_STACKRESET:
3290 case SWM_ARG_ID_STACKINIT:
3291 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3292 ws->l_state.vertical_mwin = 1;
3293 ws->l_state.vertical_stacks = 1;
3295 case SWM_ARG_ID_MASTERSHRINK:
3296 if (ws->l_state.vertical_msize > 1)
3297 ws->l_state.vertical_msize--;
3299 case SWM_ARG_ID_MASTERGROW:
3300 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3301 ws->l_state.vertical_msize++;
3303 case SWM_ARG_ID_MASTERADD:
3304 ws->l_state.vertical_mwin++;
3306 case SWM_ARG_ID_MASTERDEL:
3307 if (ws->l_state.vertical_mwin > 0)
3308 ws->l_state.vertical_mwin--;
3310 case SWM_ARG_ID_STACKINC:
3311 ws->l_state.vertical_stacks++;
3313 case SWM_ARG_ID_STACKDEC:
3314 if (ws->l_state.vertical_stacks > 1)
3315 ws->l_state.vertical_stacks--;
3317 case SWM_ARG_ID_FLIPLAYOUT:
3318 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3326 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3328 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3330 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3334 horizontal_config(struct workspace *ws, int id)
3336 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3339 case SWM_ARG_ID_STACKRESET:
3340 case SWM_ARG_ID_STACKINIT:
3341 ws->l_state.horizontal_mwin = 1;
3342 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3343 ws->l_state.horizontal_stacks = 1;
3345 case SWM_ARG_ID_MASTERSHRINK:
3346 if (ws->l_state.horizontal_msize > 1)
3347 ws->l_state.horizontal_msize--;
3349 case SWM_ARG_ID_MASTERGROW:
3350 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3351 ws->l_state.horizontal_msize++;
3353 case SWM_ARG_ID_MASTERADD:
3354 ws->l_state.horizontal_mwin++;
3356 case SWM_ARG_ID_MASTERDEL:
3357 if (ws->l_state.horizontal_mwin > 0)
3358 ws->l_state.horizontal_mwin--;
3360 case SWM_ARG_ID_STACKINC:
3361 ws->l_state.horizontal_stacks++;
3363 case SWM_ARG_ID_STACKDEC:
3364 if (ws->l_state.horizontal_stacks > 1)
3365 ws->l_state.horizontal_stacks--;
3367 case SWM_ARG_ID_FLIPLAYOUT:
3368 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3376 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3378 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3380 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3383 /* fullscreen view */
3385 max_stack(struct workspace *ws, struct swm_geometry *g)
3388 struct swm_geometry gg = *g;
3389 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3393 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3398 winno = count_win(ws, 0);
3399 if (winno == 0 && count_win(ws, 1) == 0)
3402 TAILQ_FOREACH(win, &ws->winlist, entry) {
3403 if (win->transient) {
3405 parent = find_window(win->transient);
3409 if (win->floating && win->floatmaxed == 0 ) {
3411 * retain geometry for retrieval on exit from
3414 store_float_geom(win, ws->r);
3415 win->floatmaxed = 1;
3418 /* only reconfigure if necessary */
3419 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3420 HEIGHT(win) != gg.h) {
3421 bzero(&wc, sizeof wc);
3422 X(win) = wc.x = gg.x;
3423 Y(win) = wc.y = gg.y;
3425 wc.border_width = border_width;
3426 WIDTH(win) = wc.width = gg.w;
3427 HEIGHT(win) = wc.height = gg.h;
3429 wc.border_width = 0;
3430 WIDTH(win) = wc.width = gg.w + 2 * border_width;
3431 HEIGHT(win) = wc.height = gg.h +
3434 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3435 XConfigureWindow(display, win->id, mask, &wc);
3437 /* unmap only if we don't have multi screen */
3438 if (win != ws->focus)
3439 if (!(ScreenCount(display) > 1 || outputs > 1))
3443 /* put the last transient on top */
3446 XMapRaised(display, parent->id);
3447 stack_floater(wintrans, ws->r);
3448 focus_magic(wintrans);
3453 send_to_ws(struct swm_region *r, union arg *args)
3455 int wsid = args->id;
3456 struct ws_win *win = NULL, *parent;
3457 struct workspace *ws, *nws;
3458 Atom ws_idx_atom = 0;
3459 unsigned char ws_idx_str[SWM_PROPLEN];
3462 if (wsid >= workspace_limit)
3465 if (r && r->ws && r->ws->focus)
3471 if (win->ws->idx == wsid)
3474 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%lx\n", win->id);
3477 nws = &win->s->ws[wsid];
3479 a.id = SWM_ARG_ID_FOCUSPREV;
3481 if (win->transient) {
3482 parent = find_window(win->transient);
3484 unmap_window(parent);
3485 TAILQ_REMOVE(&ws->winlist, parent, entry);
3486 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3491 TAILQ_REMOVE(&ws->winlist, win, entry);
3492 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3493 if (TAILQ_EMPTY(&ws->winlist))
3494 r->ws->focus = NULL;
3497 /* Try to update the window's workspace property */
3498 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3500 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3502 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3504 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3505 PropModeReplace, ws_idx_str, strlen((char *)ws_idx_str));
3513 pressbutton(struct swm_region *r, union arg *args)
3515 XTestFakeButtonEvent(display, args->id, True, CurrentTime);
3516 XTestFakeButtonEvent(display, args->id, False, CurrentTime);
3520 raise_toggle(struct swm_region *r, union arg *args)
3522 if (r == NULL || r->ws == NULL)
3525 r->ws->always_raise = !r->ws->always_raise;
3527 /* bring floaters back to top */
3528 if (r->ws->always_raise == 0)
3533 iconify(struct swm_region *r, union arg *args)
3537 if (r->ws->focus == NULL)
3539 unmap_window(r->ws->focus);
3540 update_iconic(r->ws->focus, 1);
3542 if (focus_mode == SWM_FOCUS_DEFAULT)
3543 drain_enter_notify();
3544 r->ws->focus = NULL;
3545 a.id = SWM_ARG_ID_FOCUSCUR;
3550 get_win_name(Window win)
3552 unsigned char *prop = NULL;
3553 unsigned long nbytes, nitems;
3555 /* try _NET_WM_NAME first */
3556 if (get_property(win, a_netwmname, 0L, a_utf8_string, NULL, &nbytes,
3559 if (get_property(win, a_netwmname, nbytes, a_utf8_string,
3560 &nitems, NULL, &prop))
3564 /* fallback to WM_NAME */
3565 if (!get_property(win, a_wmname, 0L, a_string, NULL, &nbytes, &prop))
3568 if (get_property(win, a_wmname, nbytes, a_string, &nitems, NULL, &prop))
3575 uniconify(struct swm_region *r, union arg *args)
3579 unsigned char *name;
3582 DNPRINTF(SWM_D_MISC, "uniconify\n");
3584 if (r == NULL || r->ws == NULL)
3587 /* make sure we have anything to uniconify */
3588 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3589 if (win->ws == NULL)
3590 continue; /* should never happen */
3591 if (win->iconic == 0)
3599 search_resp_action = SWM_SEARCH_UNICONIFY;
3601 spawn_select(r, args, "search", &searchpid);
3603 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3606 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3607 if (win->ws == NULL)
3608 continue; /* should never happen */
3609 if (win->iconic == 0)
3612 name = get_win_name(win->id);
3615 fprintf(lfile, "%s.%lu\n", name, win->id);
3623 name_workspace(struct swm_region *r, union arg *args)
3627 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3633 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3635 spawn_select(r, args, "name_workspace", &searchpid);
3637 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3640 fprintf(lfile, "%s", "");
3645 search_workspace(struct swm_region *r, union arg *args)
3648 struct workspace *ws;
3651 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3657 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3659 spawn_select(r, args, "search", &searchpid);
3661 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3664 for (i = 0; i < workspace_limit; i++) {
3668 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3669 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3676 search_win_cleanup(void)
3678 struct search_window *sw = NULL;
3680 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3681 XDestroyWindow(display, sw->indicator);
3682 XFreeGC(display, sw->gc);
3683 TAILQ_REMOVE(&search_wl, sw, entry);
3689 search_win(struct swm_region *r, union arg *args)
3691 struct ws_win *win = NULL;
3692 struct search_window *sw = NULL;
3699 XRectangle ibox, lbox;
3701 DNPRINTF(SWM_D_MISC, "search_win\n");
3704 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3706 spawn_select(r, args, "search", &searchpid);
3708 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3711 TAILQ_INIT(&search_wl);
3714 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3715 if (win->iconic == 1)
3718 sw = calloc(1, sizeof(struct search_window));
3720 warn("search_win: calloc");
3722 search_win_cleanup();
3728 snprintf(s, sizeof s, "%d", i);
3731 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
3733 w = XCreateSimpleWindow(display,
3734 win->id, 0, 0,lbox.width + 4,
3735 bar_fs_extents->max_logical_extent.height, 1,
3736 r->s->c[SWM_S_COLOR_UNFOCUS].color,
3737 r->s->c[SWM_S_COLOR_FOCUS].color);
3740 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3742 sw->gc = XCreateGC(display, w, 0, &gcv);
3743 XMapRaised(display, w);
3744 XSetForeground(display, sw->gc, r->s->c[SWM_S_COLOR_BAR].color);
3746 DRAWSTRING(display, w, bar_fs, sw->gc, 2,
3747 (bar_fs_extents->max_logical_extent.height -
3748 lbox.height) / 2 - lbox.y, s, len);
3750 fprintf(lfile, "%d\n", i);
3758 search_resp_uniconify(char *resp, unsigned long len)
3760 unsigned char *name;
3764 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
3766 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3767 if (win->iconic == 0)
3769 name = get_win_name(win->id);
3772 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3777 if (strncmp(s, resp, len) == 0) {
3778 /* XXX this should be a callback to generalize */
3779 update_iconic(win, 0);
3788 search_resp_name_workspace(char *resp, unsigned long len)
3790 struct workspace *ws;
3792 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
3794 if (search_r->ws == NULL)
3799 free(search_r->ws->name);
3800 search_r->ws->name = NULL;
3804 ws->name = strdup(resp);
3805 if (ws->name == NULL) {
3806 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
3807 "strdup: %s", strerror(errno));
3814 search_resp_search_workspace(char *resp, unsigned long len)
3821 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
3825 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
3832 ws_idx = strtonum(q, 1, workspace_limit, &errstr);
3834 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
3841 switchws(search_r, &a);
3845 search_resp_search_window(char *resp, unsigned long len)
3850 struct search_window *sw;
3852 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
3856 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
3861 idx = strtonum(s, 1, INT_MAX, &errstr);
3863 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
3870 TAILQ_FOREACH(sw, &search_wl, entry)
3871 if (idx == sw->idx) {
3877 #define MAX_RESP_LEN 1024
3880 search_do_resp(void)
3886 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3891 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3892 warn("search: calloc");
3896 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3898 warn("search: read error");
3901 resp[rbytes] = '\0';
3904 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3905 * newline, so work around that by sanitizing the resp now.
3907 resp[strcspn(resp, "\n")] = '\0';
3910 switch (search_resp_action) {
3911 case SWM_SEARCH_UNICONIFY:
3912 search_resp_uniconify(resp, len);
3914 case SWM_SEARCH_NAME_WORKSPACE:
3915 search_resp_name_workspace(resp, len);
3917 case SWM_SEARCH_SEARCH_WORKSPACE:
3918 search_resp_search_workspace(resp, len);
3920 case SWM_SEARCH_SEARCH_WINDOW:
3921 search_resp_search_window(resp, len);
3926 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
3927 search_win_cleanup();
3929 search_resp_action = SWM_SEARCH_NONE;
3930 close(select_resp_pipe[0]);
3935 wkill(struct swm_region *r, union arg *args)
3937 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
3939 if (r->ws->focus == NULL)
3942 if (args->id == SWM_ARG_ID_KILLWINDOW)
3943 XKillClient(display, r->ws->focus->id);
3945 if (r->ws->focus->can_delete)
3946 client_msg(r->ws->focus, adelete);
3951 floating_toggle_win(struct ws_win *win)
3953 struct swm_region *r;
3963 /* reject floating toggles in max stack mode */
3964 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3967 if (win->floating) {
3968 if (!win->floatmaxed) {
3969 /* retain position for refloat */
3970 store_float_geom(win, r);
3974 if (win->g_floatvalid) {
3975 /* refloat at last floating relative position */
3976 X(win) = win->g_float.x - win->rg_float.x + X(r);
3977 Y(win) = win->g_float.y - win->rg_float.y + Y(r);
3978 WIDTH(win) = win->g_float.w;
3979 HEIGHT(win) = win->g_float.h;
3984 ewmh_update_actions(win);
3990 floating_toggle(struct swm_region *r, union arg *args)
3992 struct ws_win *win = r->ws->focus;
3998 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3999 _NET_WM_STATE_TOGGLE);
4002 if (focus_mode == SWM_FOCUS_DEFAULT)
4003 drain_enter_notify();
4005 if (win == win->ws->focus) {
4006 a.id = SWM_ARG_ID_FOCUSCUR;
4007 focus(win->ws->r, &a);
4012 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
4014 if (X(win) + WIDTH(win) > X(r) + WIDTH(r) - border_width) {
4016 WIDTH(win) = X(r) + WIDTH(r) - X(win) - border_width;
4018 X(win) = X(r) + WIDTH(r) - WIDTH(win) - border_width;
4021 if (X(win) < X(r) - border_width) {
4023 WIDTH(win) -= X(r) - X(win) - border_width;
4025 X(win) = X(r) - border_width;
4028 if (Y(win) + HEIGHT(win) > Y(r) + HEIGHT(r) - border_width) {
4030 HEIGHT(win) = Y(r) + HEIGHT(r) - Y(win) - border_width;
4032 Y(win) = Y(r) + HEIGHT(r) - HEIGHT(win) - border_width;
4035 if (Y(win) < Y(r) - border_width) {
4037 HEIGHT(win) -= Y(r) - Y(win) - border_width;
4039 Y(win) = Y(r) - border_width;
4044 if (HEIGHT(win) < 1)
4049 update_window(struct ws_win *win)
4054 bzero(&wc, sizeof wc);
4055 mask = CWBorderWidth | CWWidth | CWHeight | CWX | CWY;
4056 wc.border_width = border_width;
4059 wc.width = WIDTH(win);
4060 wc.height = HEIGHT(win);
4062 DNPRINTF(SWM_D_MISC, "update_window: window: 0x%lx, (x,y) w x h: "
4063 "(%d,%d) %d x %d\n", win->id, wc.x, wc.y, wc.width, wc.height);
4065 XConfigureWindow(display, win->id, mask, &wc);
4068 #define SWM_RESIZE_STEPS (50)
4071 resize(struct ws_win *win, union arg *args)
4075 struct swm_region *r = NULL;
4076 int resize_step = 0;
4080 struct swm_geometry g;
4081 int top = 0, left = 0;
4084 unsigned int shape; /* cursor style */
4090 DNPRINTF(SWM_D_MOUSE, "resize: window: 0x%lx, floating: %s, "
4091 "transient: 0x%lx\n", win->id, YESNO(win->floating),
4094 if (!(win->transient != 0 || win->floating != 0))
4097 /* reject resizes in max mode for floaters (transient ok) */
4098 if (win->floatmaxed)
4102 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4108 case SWM_ARG_ID_WIDTHSHRINK:
4109 WIDTH(win) -= SWM_RESIZE_STEPS;
4112 case SWM_ARG_ID_WIDTHGROW:
4113 WIDTH(win) += SWM_RESIZE_STEPS;
4116 case SWM_ARG_ID_HEIGHTSHRINK:
4117 HEIGHT(win) -= SWM_RESIZE_STEPS;
4120 case SWM_ARG_ID_HEIGHTGROW:
4121 HEIGHT(win) += SWM_RESIZE_STEPS;
4128 constrain_window(win, r, 1);
4130 store_float_geom(win,r);
4134 if (focus_mode == SWM_FOCUS_DEFAULT)
4135 drain_enter_notify();
4137 /* get cursor offset from window root */
4138 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
4143 if (wx < WIDTH(win) / 2)
4146 if (wy < HEIGHT(win) / 2)
4149 if (args->id == SWM_ARG_ID_CENTER)
4152 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4154 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4156 cursor = XCreateFontCursor(display, shape);
4158 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
4159 GrabModeAsync, None, cursor, CurrentTime) != GrabSuccess) {
4160 XFreeCursor(display, cursor);
4165 XMaskEvent(display, MOUSEMASK | ExposureMask |
4166 SubstructureRedirectMask, &ev);
4168 case ConfigureRequest:
4171 handler[ev.type](&ev);
4174 /* cursor offset/delta from start of the operation */
4175 dx = ev.xmotion.x_root - x;
4176 dy = ev.xmotion.y_root - y;
4182 if (args->id == SWM_ARG_ID_CENTER) {
4183 if (g.h / 2 + dy < 1)
4187 HEIGHT(win) = g.h + 2 * dy;
4195 HEIGHT(win) = g.h + dy;
4202 if (args->id == SWM_ARG_ID_CENTER) {
4203 if (g.w / 2 + dx < 1)
4207 WIDTH(win) = g.w + 2 * dx;
4215 WIDTH(win) = g.w + dx;
4218 constrain_window(win, r, 1);
4220 /* not free, don't sync more than 120 times / second */
4221 if ((ev.xmotion.time - time) > (1000 / 120) ) {
4222 time = ev.xmotion.time;
4223 XSync(display, False);
4228 } while (ev.type != ButtonRelease);
4230 XSync(display, False);
4233 store_float_geom(win,r);
4235 XUngrabPointer(display, CurrentTime);
4236 XFreeCursor(display, cursor);
4239 drain_enter_notify();
4243 resize_step(struct swm_region *r, union arg *args)
4245 struct ws_win *win = NULL;
4247 if (r && r->ws && r->ws->focus)
4255 #define SWM_MOVE_STEPS (50)
4258 move(struct ws_win *win, union arg *args)
4263 struct swm_region *r = NULL;
4273 DNPRINTF(SWM_D_MOUSE, "move: window: 0x%lx, floating: %s, transient: "
4274 "0x%lx\n", win->id, YESNO(win->floating), win->transient);
4276 /* in max_stack mode should only move transients */
4277 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4281 if (win->floating == 0 && !win->transient) {
4282 store_float_geom(win,r);
4283 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4286 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4293 case SWM_ARG_ID_MOVELEFT:
4294 X(win) -= (SWM_MOVE_STEPS - border_width);
4297 case SWM_ARG_ID_MOVERIGHT:
4298 X(win) += (SWM_MOVE_STEPS - border_width);
4301 case SWM_ARG_ID_MOVEUP:
4302 Y(win) -= (SWM_MOVE_STEPS - border_width);
4305 case SWM_ARG_ID_MOVEDOWN:
4306 Y(win) += (SWM_MOVE_STEPS - border_width);
4313 constrain_window(win, r, 0);
4315 store_float_geom(win, r);
4319 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
4320 GrabModeAsync, None, XCreateFontCursor(display, XC_fleur),
4321 CurrentTime) != GrabSuccess)
4324 /* get cursor offset from window root */
4325 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
4329 XMaskEvent(display, MOUSEMASK | ExposureMask |
4330 SubstructureRedirectMask, &ev);
4332 case ConfigureRequest:
4335 handler[ev.type](&ev);
4338 X(win) = ev.xmotion.x_root - wx - border_width;
4339 Y(win) = ev.xmotion.y_root - wy - border_width;
4341 constrain_window(win, r, 0);
4343 /* not free, don't sync more than 120 times / second */
4344 if ((ev.xmotion.time - time) > (1000 / 120) ) {
4345 time = ev.xmotion.time;
4346 XSync(display, False);
4351 } while (ev.type != ButtonRelease);
4353 XSync(display, False);
4356 store_float_geom(win,r);
4357 XUngrabPointer(display, CurrentTime);
4360 drain_enter_notify();
4364 move_step(struct swm_region *r, union arg *args)
4366 struct ws_win *win = NULL;
4368 if (r && r->ws && r->ws->focus)
4373 if (!(win->transient != 0 || win->floating != 0))
4380 /* user/key callable function IDs */
4475 kf_search_workspace,
4477 kf_dumpwins, /* MUST BE LAST */
4481 /* key definitions */
4483 dummykeyfunc(struct swm_region *r, union arg *args)
4488 legacyfunc(struct swm_region *r, union arg *args)
4493 char name[SWM_FUNCNAME_LEN];
4494 void (*func)(struct swm_region *r, union arg *);
4496 } keyfuncs[kf_invalid + 1] = {
4497 /* name function argument */
4498 { "cycle_layout", cycle_layout, {0} },
4499 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
4500 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4501 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4502 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4503 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4504 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4505 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4506 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4507 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4508 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4509 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4510 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4511 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4512 { "spawn_term", spawnterm, {.argv = spawn_term} },
4513 { "spawn_menu", legacyfunc, {0} },
4514 { "quit", quit, {0} },
4515 { "restart", restart, {0} },
4516 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4517 { "ws_1", switchws, {.id = 0} },
4518 { "ws_2", switchws, {.id = 1} },
4519 { "ws_3", switchws, {.id = 2} },
4520 { "ws_4", switchws, {.id = 3} },
4521 { "ws_5", switchws, {.id = 4} },
4522 { "ws_6", switchws, {.id = 5} },
4523 { "ws_7", switchws, {.id = 6} },
4524 { "ws_8", switchws, {.id = 7} },
4525 { "ws_9", switchws, {.id = 8} },
4526 { "ws_10", switchws, {.id = 9} },
4527 { "ws_11", switchws, {.id = 10} },
4528 { "ws_12", switchws, {.id = 11} },
4529 { "ws_13", switchws, {.id = 12} },
4530 { "ws_14", switchws, {.id = 13} },
4531 { "ws_15", switchws, {.id = 14} },
4532 { "ws_16", switchws, {.id = 15} },
4533 { "ws_17", switchws, {.id = 16} },
4534 { "ws_18", switchws, {.id = 17} },
4535 { "ws_19", switchws, {.id = 18} },
4536 { "ws_20", switchws, {.id = 19} },
4537 { "ws_21", switchws, {.id = 20} },
4538 { "ws_22", switchws, {.id = 21} },
4539 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4540 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4541 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4542 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4543 { "ws_prior", priorws, {0} },
4544 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4545 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4546 { "mvws_1", send_to_ws, {.id = 0} },
4547 { "mvws_2", send_to_ws, {.id = 1} },
4548 { "mvws_3", send_to_ws, {.id = 2} },
4549 { "mvws_4", send_to_ws, {.id = 3} },
4550 { "mvws_5", send_to_ws, {.id = 4} },
4551 { "mvws_6", send_to_ws, {.id = 5} },
4552 { "mvws_7", send_to_ws, {.id = 6} },
4553 { "mvws_8", send_to_ws, {.id = 7} },
4554 { "mvws_9", send_to_ws, {.id = 8} },
4555 { "mvws_10", send_to_ws, {.id = 9} },
4556 { "mvws_11", send_to_ws, {.id = 10} },
4557 { "mvws_12", send_to_ws, {.id = 11} },
4558 { "mvws_13", send_to_ws, {.id = 12} },
4559 { "mvws_14", send_to_ws, {.id = 13} },
4560 { "mvws_15", send_to_ws, {.id = 14} },
4561 { "mvws_16", send_to_ws, {.id = 15} },
4562 { "mvws_17", send_to_ws, {.id = 16} },
4563 { "mvws_18", send_to_ws, {.id = 17} },
4564 { "mvws_19", send_to_ws, {.id = 18} },
4565 { "mvws_20", send_to_ws, {.id = 19} },
4566 { "mvws_21", send_to_ws, {.id = 20} },
4567 { "mvws_22", send_to_ws, {.id = 21} },
4568 { "bar_toggle", bar_toggle, {0} },
4569 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4570 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4571 { "screenshot_all", legacyfunc, {0} },
4572 { "screenshot_wind", legacyfunc, {0} },
4573 { "float_toggle", floating_toggle,{0} },
4574 { "version", version, {0} },
4575 { "spawn_lock", legacyfunc, {0} },
4576 { "spawn_initscr", legacyfunc, {0} },
4577 { "spawn_custom", dummykeyfunc, {0} },
4578 { "iconify", iconify, {0} },
4579 { "uniconify", uniconify, {0} },
4580 { "raise_toggle", raise_toggle, {0} },
4581 { "button2", pressbutton, {2} },
4582 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4583 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4584 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4585 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4586 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4587 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4588 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4589 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4590 { "name_workspace", name_workspace, {0} },
4591 { "search_workspace", search_workspace, {0} },
4592 { "search_win", search_win, {0} },
4593 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4594 { "invalid key func", NULL, {0} },
4597 RB_ENTRY(key) entry;
4600 enum keyfuncid funcid;
4603 RB_HEAD(key_tree, key);
4606 key_cmp(struct key *kp1, struct key *kp2)
4608 if (kp1->keysym < kp2->keysym)
4610 if (kp1->keysym > kp2->keysym)
4613 if (kp1->mod < kp2->mod)
4615 if (kp1->mod > kp2->mod)
4621 RB_GENERATE(key_tree, key, entry, key_cmp);
4622 struct key_tree keys;
4625 enum { client_click, root_click };
4627 unsigned int action;
4629 unsigned int button;
4630 void (*func)(struct ws_win *, union arg *);
4633 /* action key mouse button func args */
4634 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4635 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4636 { client_click, MODKEY, Button1, move, {0} },
4640 update_modkey(unsigned int mod)
4646 RB_FOREACH(kp, key_tree, &keys)
4647 if (kp->mod & ShiftMask)
4648 kp->mod = mod | ShiftMask;
4652 for (i = 0; i < LENGTH(buttons); i++)
4653 if (buttons[i].mask & ShiftMask)
4654 buttons[i].mask = mod | ShiftMask;
4656 buttons[i].mask = mod;
4661 TAILQ_ENTRY(spawn_prog) entry;
4666 TAILQ_HEAD(spawn_list, spawn_prog);
4667 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
4670 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4673 struct spawn_prog *prog = NULL;
4675 char *ap, **real_args;
4677 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4680 TAILQ_FOREACH(prog, &spawns, entry) {
4681 if (!strcasecmp(spawn_name, prog->name))
4685 warnx("spawn_custom: program %s not found", spawn_name);
4689 /* make room for expanded args */
4690 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4691 err(1, "spawn_custom: calloc real_args");
4693 /* expand spawn_args into real_args */
4694 for (i = 0; i < prog->argc; i++) {
4696 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4697 if (!strcasecmp(ap, "$bar_border")) {
4699 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4701 err(1, "spawn_custom border color");
4702 } else if (!strcasecmp(ap, "$bar_color")) {
4704 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4706 err(1, "spawn_custom bar color");
4707 } else if (!strcasecmp(ap, "$bar_font")) {
4708 if ((real_args[i] = strdup(bar_fonts))
4710 err(1, "spawn_custom bar fonts");
4711 } else if (!strcasecmp(ap, "$bar_font_color")) {
4713 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4715 err(1, "spawn_custom color font");
4716 } else if (!strcasecmp(ap, "$color_focus")) {
4718 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4720 err(1, "spawn_custom color focus");
4721 } else if (!strcasecmp(ap, "$color_unfocus")) {
4723 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4725 err(1, "spawn_custom color unfocus");
4727 /* no match --> copy as is */
4728 if ((real_args[i] = strdup(ap)) == NULL)
4729 err(1, "spawn_custom strdup(ap)");
4731 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
4736 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
4737 for (i = 0; i < prog->argc; i++)
4738 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
4739 DNPRINTF(SWM_D_SPAWN, "\n");
4741 *ret_args = real_args;
4742 return (prog->argc);
4746 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4752 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4756 spawn(r->ws->idx, &a, 1);
4758 for (i = 0; i < spawn_argc; i++)
4764 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
4770 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4774 if (pipe(select_list_pipe) == -1)
4775 err(1, "pipe error");
4776 if (pipe(select_resp_pipe) == -1)
4777 err(1, "pipe error");
4779 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4780 err(1, "could not disable SIGPIPE");
4781 switch (*pid = fork()) {
4783 err(1, "cannot fork");
4786 if (dup2(select_list_pipe[0], 0) == -1)
4788 if (dup2(select_resp_pipe[1], 1) == -1)
4790 close(select_list_pipe[1]);
4791 close(select_resp_pipe[0]);
4792 spawn(r->ws->idx, &a, 0);
4794 default: /* parent */
4795 close(select_list_pipe[0]);
4796 close(select_resp_pipe[1]);
4800 for (i = 0; i < spawn_argc; i++)
4806 spawn_insert(char *name, char *args)
4808 char *arg, *cp, *ptr;
4809 struct spawn_prog *sp;
4811 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
4813 if ((sp = calloc(1, sizeof *sp)) == NULL)
4814 err(1, "spawn_insert: malloc");
4815 if ((sp->name = strdup(name)) == NULL)
4816 err(1, "spawn_insert: strdup");
4818 /* convert the arguments to an argument list */
4819 if ((ptr = cp = strdup(args)) == NULL)
4820 err(1, "spawn_insert: strdup");
4821 while ((arg = strsep(&ptr, " \t")) != NULL) {
4822 /* empty field; skip it */
4827 if ((sp->argv = realloc(sp->argv, sp->argc *
4828 sizeof *sp->argv)) == NULL)
4829 err(1, "spawn_insert: realloc");
4830 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
4831 err(1, "spawn_insert: strdup");
4835 TAILQ_INSERT_TAIL(&spawns, sp, entry);
4836 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
4840 spawn_remove(struct spawn_prog *sp)
4844 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
4846 TAILQ_REMOVE(&spawns, sp, entry);
4847 for (i = 0; i < sp->argc; i++)
4853 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
4857 spawn_replace(struct spawn_prog *sp, char *name, char *args)
4859 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
4862 spawn_insert(name, args);
4864 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
4868 setspawn(char *name, char *args)
4870 struct spawn_prog *sp;
4872 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
4877 TAILQ_FOREACH(sp, &spawns, entry) {
4878 if (!strcmp(sp->name, name)) {
4882 spawn_replace(sp, name, args);
4883 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
4887 if (*args == '\0') {
4888 warnx("error: setspawn: cannot find program: %s", name);
4892 spawn_insert(name, args);
4893 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
4897 setconfspawn(char *selector, char *value, int flags)
4899 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
4901 setspawn(selector, value);
4903 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
4910 setconfspawn("term", "xterm", 0);
4911 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4912 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4913 setconfspawn("lock", "xlock", 0);
4914 setconfspawn("initscr", "initscreen.sh", 0);
4915 setconfspawn("menu", "dmenu_run"
4918 " -nf $bar_font_color"
4920 " -sf $bar_color", 0);
4921 setconfspawn("search", "dmenu"
4925 " -nf $bar_font_color"
4927 " -sf $bar_color", 0);
4928 setconfspawn("name_workspace", "dmenu"
4932 " -nf $bar_font_color"
4934 " -sf $bar_color", 0);
4938 #define SWM_MODNAME_SIZE 32
4939 #define SWM_KEY_WS "\n+ \t"
4941 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
4945 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
4946 if (mod == NULL || ks == NULL) {
4947 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
4950 if (keystr == NULL || strlen(keystr) == 0) {
4951 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
4957 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
4958 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
4960 cp += (long)strspn(cp, SWM_KEY_WS);
4961 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
4963 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
4965 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
4967 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
4969 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
4971 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
4973 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
4974 *mod |= ControlMask;
4976 *ks = XStringToKeysym(name);
4977 XConvertCase(*ks, ks, &uks);
4978 if (ks == NoSymbol) {
4980 "parsekeys: invalid key %s\n",
4986 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
4991 strdupsafe(char *str)
4996 return (strdup(str));
5000 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
5004 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
5005 keyfuncs[kfid].name, spawn_name);
5007 if ((kp = malloc(sizeof *kp)) == NULL)
5008 err(1, "key_insert: malloc");
5013 kp->spawn_name = strdupsafe(spawn_name);
5014 RB_INSERT(key_tree, &keys, kp);
5016 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
5020 key_lookup(unsigned int mod, KeySym ks)
5027 return (RB_FIND(key_tree, &keys, &kp));
5031 key_remove(struct key *kp)
5033 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5035 RB_REMOVE(key_tree, &keys, kp);
5036 free(kp->spawn_name);
5039 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5043 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5046 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5050 key_insert(mod, ks, kfid, spawn_name);
5052 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5056 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5061 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5062 keyfuncs[kfid].name, spawn_name);
5064 if ((kp = key_lookup(mod, ks)) != NULL) {
5065 if (kfid == kf_invalid)
5068 key_replace(kp, mod, ks, kfid, spawn_name);
5069 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5072 if (kfid == kf_invalid) {
5073 warnx("error: setkeybinding: cannot find mod/key combination");
5074 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5078 key_insert(mod, ks, kfid, spawn_name);
5079 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5083 setconfbinding(char *selector, char *value, int flags)
5085 enum keyfuncid kfid;
5088 struct spawn_prog *sp;
5089 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5090 if (selector == NULL) {
5091 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5092 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5094 setkeybinding(mod, ks, kfid, NULL);
5099 /* search by key function name */
5100 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
5101 if (strncasecmp(selector, keyfuncs[kfid].name,
5102 SWM_FUNCNAME_LEN) == 0) {
5103 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5105 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5106 setkeybinding(mod, ks, kfid, NULL);
5112 /* search by custom spawn name */
5113 TAILQ_FOREACH(sp, &spawns, entry) {
5114 if (strcasecmp(selector, sp->name) == 0) {
5115 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5117 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5118 setkeybinding(mod, ks, kf_spawn_custom,
5125 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5132 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
5133 setkeybinding(MODKEY|ShiftMask, XK_backslash, kf_flip_layout, NULL);
5134 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
5135 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
5136 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
5137 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
5138 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
5139 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
5140 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
5141 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
5142 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
5143 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
5144 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
5145 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
5146 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
5147 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
5148 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
5149 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
5150 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
5151 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
5152 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
5153 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
5154 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
5155 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
5156 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
5157 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
5158 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
5159 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
5160 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
5161 setkeybinding(MODKEY, XK_F1, kf_ws_11, NULL);
5162 setkeybinding(MODKEY, XK_F2, kf_ws_12, NULL);
5163 setkeybinding(MODKEY, XK_F3, kf_ws_13, NULL);
5164 setkeybinding(MODKEY, XK_F4, kf_ws_14, NULL);
5165 setkeybinding(MODKEY, XK_F5, kf_ws_15, NULL);
5166 setkeybinding(MODKEY, XK_F6, kf_ws_16, NULL);
5167 setkeybinding(MODKEY, XK_F7, kf_ws_17, NULL);
5168 setkeybinding(MODKEY, XK_F8, kf_ws_18, NULL);
5169 setkeybinding(MODKEY, XK_F9, kf_ws_19, NULL);
5170 setkeybinding(MODKEY, XK_F10, kf_ws_20, NULL);
5171 setkeybinding(MODKEY, XK_F11, kf_ws_21, NULL);
5172 setkeybinding(MODKEY, XK_F12, kf_ws_22, NULL);
5173 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
5174 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
5175 setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
5176 setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
5177 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
5178 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
5179 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
5180 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
5181 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
5182 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
5183 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
5184 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
5185 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
5186 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
5187 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
5188 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
5189 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
5190 setkeybinding(MODKEY|ShiftMask, XK_F1, kf_mvws_11, NULL);
5191 setkeybinding(MODKEY|ShiftMask, XK_F2, kf_mvws_12, NULL);
5192 setkeybinding(MODKEY|ShiftMask, XK_F3, kf_mvws_13, NULL);
5193 setkeybinding(MODKEY|ShiftMask, XK_F4, kf_mvws_14, NULL);
5194 setkeybinding(MODKEY|ShiftMask, XK_F5, kf_mvws_15, NULL);
5195 setkeybinding(MODKEY|ShiftMask, XK_F6, kf_mvws_16, NULL);
5196 setkeybinding(MODKEY|ShiftMask, XK_F7, kf_mvws_17, NULL);
5197 setkeybinding(MODKEY|ShiftMask, XK_F8, kf_mvws_18, NULL);
5198 setkeybinding(MODKEY|ShiftMask, XK_F9, kf_mvws_19, NULL);
5199 setkeybinding(MODKEY|ShiftMask, XK_F10, kf_mvws_20, NULL);
5200 setkeybinding(MODKEY|ShiftMask, XK_F11, kf_mvws_21, NULL);
5201 setkeybinding(MODKEY|ShiftMask, XK_F12, kf_mvws_22, NULL);
5202 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
5203 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
5204 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
5205 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
5206 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
5207 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
5208 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
5209 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
5210 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
5211 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
5212 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
5213 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
5214 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
5215 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
5216 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
5217 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
5218 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
5219 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
5220 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
5221 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
5222 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
5223 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
5224 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
5225 setkeybinding(MODKEY|ShiftMask, XK_slash, kf_name_workspace,NULL);
5226 setkeybinding(MODKEY, XK_slash, kf_search_workspace,NULL);
5227 setkeybinding(MODKEY, XK_f, kf_search_win, NULL);
5229 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
5238 while (RB_EMPTY(&keys) == 0) {
5239 kp = RB_ROOT(&keys);
5245 setkeymapping(char *selector, char *value, int flags)
5247 char keymapping_file[PATH_MAX];
5248 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5249 if (value[0] == '~')
5250 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5251 pwd->pw_dir, &value[1]);
5253 strlcpy(keymapping_file, value, sizeof keymapping_file);
5255 /* load new key bindings; if it fails, revert to default bindings */
5256 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5260 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5265 updatenumlockmask(void)
5268 XModifierKeymap *modmap;
5270 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
5272 modmap = XGetModifierMapping(display);
5273 for (i = 0; i < 8; i++)
5274 for (j = 0; j < modmap->max_keypermod; j++)
5275 if (modmap->modifiermap[i * modmap->max_keypermod + j]
5276 == XKeysymToKeycode(display, XK_Num_Lock))
5277 numlockmask = (1 << i);
5279 XFreeModifiermap(modmap);
5287 unsigned int modifiers[] =
5288 { 0, LockMask, numlockmask, numlockmask | LockMask };
5291 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5292 updatenumlockmask();
5294 for (k = 0; k < ScreenCount(display); k++) {
5295 if (TAILQ_EMPTY(&screens[k].rl))
5297 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
5298 RB_FOREACH(kp, key_tree, &keys) {
5299 if ((code = XKeysymToKeycode(display, kp->keysym)))
5300 for (j = 0; j < LENGTH(modifiers); j++)
5301 XGrabKey(display, code,
5302 kp->mod | modifiers[j],
5303 screens[k].root, True,
5304 GrabModeAsync, GrabModeAsync);
5310 grabbuttons(struct ws_win *win, int focused)
5313 unsigned int modifiers[] =
5314 { 0, LockMask, numlockmask, numlockmask|LockMask };
5316 updatenumlockmask();
5317 XUngrabButton(display, AnyButton, AnyModifier, win->id);
5319 for (i = 0; i < LENGTH(buttons); i++)
5320 if (buttons[i].action == client_click)
5321 for (j = 0; j < LENGTH(modifiers); j++)
5322 XGrabButton(display, buttons[i].button,
5323 buttons[i].mask | modifiers[j],
5324 win->id, False, BUTTONMASK,
5325 GrabModeAsync, GrabModeSync, None,
5328 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
5329 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
5332 const char *quirkname[] = {
5333 "NONE", /* config string for "no value" */
5342 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5343 #define SWM_Q_WS "\n|+ \t"
5345 parsequirks(char *qstr, unsigned long *quirk)
5355 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5357 cp += (long)strspn(cp, SWM_Q_WS);
5358 for (i = 0; i < LENGTH(quirkname); i++) {
5359 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5360 DNPRINTF(SWM_D_QUIRK,
5361 "parsequirks: %s\n", name);
5366 *quirk |= 1 << (i-1);
5370 if (i >= LENGTH(quirkname)) {
5371 DNPRINTF(SWM_D_QUIRK,
5372 "parsequirks: invalid quirk [%s]\n", name);
5380 quirk_insert(const char *class, const char *name, unsigned long quirk)
5384 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5387 if ((qp = malloc(sizeof *qp)) == NULL)
5388 err(1, "quirk_insert: malloc");
5389 if ((qp->class = strdup(class)) == NULL)
5390 err(1, "quirk_insert: strdup");
5391 if ((qp->name = strdup(name)) == NULL)
5392 err(1, "quirk_insert: strdup");
5395 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5397 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5401 quirk_remove(struct quirk *qp)
5403 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5404 qp->name, qp->quirk);
5406 TAILQ_REMOVE(&quirks, qp, entry);
5411 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5415 quirk_replace(struct quirk *qp, const char *class, const char *name,
5416 unsigned long quirk)
5418 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5419 qp->name, qp->quirk);
5422 quirk_insert(class, name, quirk);
5424 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5428 setquirk(const char *class, const char *name, unsigned long quirk)
5432 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5435 TAILQ_FOREACH(qp, &quirks, entry) {
5436 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5440 quirk_replace(qp, class, name, quirk);
5441 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5446 warnx("error: setquirk: cannot find class/name combination");
5450 quirk_insert(class, name, quirk);
5451 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5455 setconfquirk(char *selector, char *value, int flags)
5457 char *cp, *class, *name;
5459 unsigned long quirks;
5460 if (selector == NULL)
5462 if ((cp = strchr(selector, ':')) == NULL)
5467 if ((retval = parsequirks(value, &quirks)) == 0)
5468 setquirk(class, name, quirks);
5475 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5476 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5477 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5478 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5479 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5480 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5481 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5482 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5483 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5484 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5485 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5486 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5487 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5490 /* conf file stuff */
5491 #define SWM_CONF_FILE "spectrwm.conf"
5492 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5494 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH,
5495 SWM_S_STACK_ENABLED, SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT,
5496 SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_WORKSPACE_LIMIT,
5497 SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
5498 SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED,
5499 SWM_S_URGENT_ENABLED, SWM_S_FOCUS_MODE, SWM_S_FOCUS_CLOSE,
5500 SWM_S_FOCUS_CLOSE_WRAP, SWM_S_FOCUS_DEFAULT, SWM_S_SPAWN_ORDER,
5501 SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH, SWM_S_BAR_FONT,
5502 SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM, SWM_S_SS_APP, SWM_S_DIALOG_RATIO,
5503 SWM_S_BAR_AT_BOTTOM, SWM_S_VERBOSE_LAYOUT, SWM_S_BAR_JUSTIFY,
5508 setconfvalue(char *selector, char *value, int flags)
5514 case SWM_S_BAR_DELAY:
5515 bar_delay = atoi(value);
5517 case SWM_S_BAR_ENABLED:
5518 bar_enabled = atoi(value);
5520 case SWM_S_BAR_BORDER_WIDTH:
5521 bar_border_width = atoi(value);
5523 case SWM_S_BAR_AT_BOTTOM:
5524 bar_at_bottom = atoi(value);
5526 case SWM_S_BAR_JUSTIFY:
5527 if (!strcmp(value, "left"))
5528 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5529 else if (!strcmp(value, "center"))
5530 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5531 else if (!strcmp(value, "right"))
5532 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5534 errx(1, "invalid bar_justify");
5536 case SWM_S_BAR_FORMAT:
5538 if ((bar_format = strdup(value)) == NULL)
5539 err(1, "setconfvalue: bar_format");
5541 case SWM_S_STACK_ENABLED:
5542 stack_enabled = atoi(value);
5544 case SWM_S_CLOCK_ENABLED:
5545 clock_enabled = atoi(value);
5547 case SWM_S_CLOCK_FORMAT:
5548 #ifndef SWM_DENY_CLOCK_FORMAT
5550 if ((clock_format = strdup(value)) == NULL)
5551 err(1, "setconfvalue: clock_format");
5554 case SWM_S_CYCLE_EMPTY:
5555 cycle_empty = atoi(value);
5557 case SWM_S_CYCLE_VISIBLE:
5558 cycle_visible = atoi(value);
5560 case SWM_S_WORKSPACE_LIMIT:
5561 workspace_limit = atoi(value);
5562 if (workspace_limit > SWM_WS_MAX)
5563 workspace_limit = SWM_WS_MAX;
5564 else if (workspace_limit < 1)
5565 workspace_limit = 1;
5567 case SWM_S_SS_ENABLED:
5568 ss_enabled = atoi(value);
5570 case SWM_S_TERM_WIDTH:
5571 term_width = atoi(value);
5573 case SWM_S_TITLE_CLASS_ENABLED:
5574 title_class_enabled = atoi(value);
5576 case SWM_S_WINDOW_NAME_ENABLED:
5577 window_name_enabled = atoi(value);
5579 case SWM_S_TITLE_NAME_ENABLED:
5580 title_name_enabled = atoi(value);
5582 case SWM_S_URGENT_ENABLED:
5583 urgent_enabled = atoi(value);
5585 case SWM_S_FOCUS_MODE:
5586 if (!strcmp(value, "default"))
5587 focus_mode = SWM_FOCUS_DEFAULT;
5588 else if (!strcmp(value, "follow_cursor"))
5589 focus_mode = SWM_FOCUS_FOLLOW;
5590 else if (!strcmp(value, "synergy"))
5591 focus_mode = SWM_FOCUS_SYNERGY;
5593 errx(1, "focus_mode");
5595 case SWM_S_FOCUS_CLOSE:
5596 if (!strcmp(value, "first"))
5597 focus_close = SWM_STACK_BOTTOM;
5598 else if (!strcmp(value, "last"))
5599 focus_close = SWM_STACK_TOP;
5600 else if (!strcmp(value, "next"))
5601 focus_close = SWM_STACK_ABOVE;
5602 else if (!strcmp(value, "previous"))
5603 focus_close = SWM_STACK_BELOW;
5605 errx(1, "focus_close");
5607 case SWM_S_FOCUS_CLOSE_WRAP:
5608 focus_close_wrap = atoi(value);
5610 case SWM_S_FOCUS_DEFAULT:
5611 if (!strcmp(value, "last"))
5612 focus_default = SWM_STACK_TOP;
5613 else if (!strcmp(value, "first"))
5614 focus_default = SWM_STACK_BOTTOM;
5616 errx(1, "focus_default");
5618 case SWM_S_SPAWN_ORDER:
5619 if (!strcmp(value, "first"))
5620 spawn_position = SWM_STACK_BOTTOM;
5621 else if (!strcmp(value, "last"))
5622 spawn_position = SWM_STACK_TOP;
5623 else if (!strcmp(value, "next"))
5624 spawn_position = SWM_STACK_ABOVE;
5625 else if (!strcmp(value, "previous"))
5626 spawn_position = SWM_STACK_BELOW;
5628 errx(1, "spawn_position");
5630 case SWM_S_DISABLE_BORDER:
5631 disable_border = atoi(value);
5633 case SWM_S_BORDER_WIDTH:
5634 border_width = atoi(value);
5636 case SWM_S_BAR_FONT:
5638 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
5639 err(1, "setconfvalue: asprintf: failed to allocate "
5640 "memory for bar_fonts.");
5644 case SWM_S_BAR_ACTION:
5646 if ((bar_argv[0] = strdup(value)) == NULL)
5647 err(1, "setconfvalue: bar_action");
5649 case SWM_S_SPAWN_TERM:
5650 free(spawn_term[0]);
5651 if ((spawn_term[0] = strdup(value)) == NULL)
5652 err(1, "setconfvalue: spawn_term");
5656 case SWM_S_DIALOG_RATIO:
5657 dialog_ratio = atof(value);
5658 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5661 case SWM_S_VERBOSE_LAYOUT:
5662 verbose_layout = atoi(value);
5663 for (i = 0; layouts[i].l_stack != NULL; i++) {
5665 layouts[i].l_string = fancy_stacker;
5667 layouts[i].l_string = plain_stacker;
5677 setconfmodkey(char *selector, char *value, int flags)
5679 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5680 update_modkey(Mod1Mask);
5681 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5682 update_modkey(Mod2Mask);
5683 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5684 update_modkey(Mod3Mask);
5685 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5686 update_modkey(Mod4Mask);
5693 setconfcolor(char *selector, char *value, int flags)
5695 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5700 setconfregion(char *selector, char *value, int flags)
5702 custom_region(value);
5707 setautorun(char *selector, char *value, int flags)
5717 if (getenv("SWM_STARTED"))
5721 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
5722 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5724 if (ws_id < 0 || ws_id >= workspace_limit)
5725 errx(1, "autorun: invalid workspace %d", ws_id + 1);
5728 * This is a little intricate
5730 * If the pid already exists we simply reuse it because it means it was
5731 * used before AND not claimed by manage_window. We get away with
5732 * altering it in the parent after INSERT because this can not be a race
5735 while ((ap = strsep(&sp, " \t")) != NULL) {
5738 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
5740 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
5741 err(1, "setautorun: realloc");
5742 a.argv[argc - 1] = ap;
5745 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
5746 err(1, "setautorun: realloc");
5747 a.argv[argc] = NULL;
5749 if ((pid = fork()) == 0) {
5750 spawn(ws_id, &a, 1);
5759 p = calloc(1, sizeof *p);
5762 TAILQ_INSERT_TAIL(&pidlist, p, entry);
5772 setlayout(char *selector, char *value, int flags)
5774 int ws_id, i, x, mg, ma, si, raise;
5775 int st = SWM_V_STACK;
5777 struct workspace *ws;
5779 if (getenv("SWM_STARTED"))
5783 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
5784 &ws_id, &mg, &ma, &si, &raise, s) != 6)
5785 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5786 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5789 if (ws_id < 0 || ws_id >= workspace_limit)
5790 errx(1, "layout: invalid workspace %d", ws_id + 1);
5792 if (!strcasecmp(s, "vertical"))
5794 else if (!strcasecmp(s, "horizontal"))
5796 else if (!strcasecmp(s, "fullscreen"))
5799 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5800 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5803 for (i = 0; i < ScreenCount(display); i++) {
5804 ws = (struct workspace *)&screens[i].ws;
5805 ws[ws_id].cur_layout = &layouts[st];
5807 ws[ws_id].always_raise = raise;
5808 if (st == SWM_MAX_STACK)
5812 for (x = 0; x < abs(mg); x++) {
5813 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5814 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
5815 SWM_ARG_ID_MASTERSHRINK);
5819 for (x = 0; x < abs(ma); x++) {
5820 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5821 ma >= 0 ? SWM_ARG_ID_MASTERADD :
5822 SWM_ARG_ID_MASTERDEL);
5826 for (x = 0; x < abs(si); x++) {
5827 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5828 si >= 0 ? SWM_ARG_ID_STACKINC :
5829 SWM_ARG_ID_STACKDEC);
5837 /* config options */
5838 struct config_option {
5840 int (*func)(char*, char*, int);
5843 struct config_option configopt[] = {
5844 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
5845 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
5846 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
5847 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
5848 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
5849 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
5850 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
5851 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
5852 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
5853 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
5854 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
5855 { "keyboard_mapping", setkeymapping, 0 },
5856 { "bind", setconfbinding, 0 },
5857 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
5858 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
5859 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
5860 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
5861 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
5862 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
5863 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
5864 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
5865 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
5866 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
5867 { "modkey", setconfmodkey, 0 },
5868 { "program", setconfspawn, 0 },
5869 { "quirk", setconfquirk, 0 },
5870 { "region", setconfregion, 0 },
5871 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
5872 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
5873 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
5874 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
5875 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
5876 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
5877 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
5878 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
5879 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
5880 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
5881 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
5882 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
5883 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
5884 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
5885 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
5886 { "autorun", setautorun, 0 },
5887 { "layout", setlayout, 0 },
5892 conf_load(char *filename, int keymapping)
5895 char *line, *cp, *optsub, *optval;
5896 size_t linelen, lineno = 0;
5897 int wordlen, i, optind;
5898 struct config_option *opt;
5900 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
5902 if (filename == NULL) {
5903 warnx("conf_load: no filename");
5906 if ((config = fopen(filename, "r")) == NULL) {
5907 warn("conf_load: fopen: %s", filename);
5911 while (!feof(config)) {
5912 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
5915 err(1, "%s", filename);
5920 cp += strspn(cp, " \t\n"); /* eat whitespace */
5921 if (cp[0] == '\0') {
5926 /* get config option */
5927 wordlen = strcspn(cp, "=[ \t\n");
5929 warnx("%s: line %zd: no option found",
5934 for (i = 0; i < LENGTH(configopt); i++) {
5935 opt = &configopt[i];
5936 if (!strncasecmp(cp, opt->optname, wordlen) &&
5937 strlen(opt->optname) == wordlen) {
5943 warnx("%s: line %zd: unknown option %.*s",
5944 filename, lineno, wordlen, cp);
5947 if (keymapping && strcmp(opt->optname, "bind")) {
5948 warnx("%s: line %zd: invalid option %.*s",
5949 filename, lineno, wordlen, cp);
5953 cp += strspn(cp, " \t\n"); /* eat whitespace */
5954 /* get [selector] if any */
5958 wordlen = strcspn(cp, "]");
5961 warnx("%s: line %zd: syntax error",
5966 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
5968 warnx("%s: line %zd: unable to allocate"
5969 "memory for selector", filename,
5975 cp += strspn(cp, "] \t\n"); /* eat trailing */
5977 cp += strspn(cp, "= \t\n"); /* eat trailing */
5979 optval = strdup(cp);
5980 /* call function to deal with it all */
5981 if (configopt[optind].func(optsub, optval,
5982 configopt[optind].funcflags) != 0)
5983 errx(1, "%s: line %zd: invalid data for %s",
5984 filename, lineno, configopt[optind].optname);
5991 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
5998 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
6004 set_child_transient(struct ws_win *win, Window *trans)
6006 struct ws_win *parent, *w;
6007 XWMHints *wmh = NULL;
6008 struct swm_region *r;
6009 struct workspace *ws;
6011 parent = find_window(win->transient);
6013 parent->child_trans = win;
6015 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
6016 " for 0x%lx trans 0x%lx\n", win->id, win->transient);
6018 if (win->hints == NULL) {
6019 warnx("no hints for 0x%lx", win->id);
6023 r = root_to_region(win->wa.root);
6025 /* parent doen't exist in our window list */
6026 TAILQ_FOREACH(w, &ws->winlist, entry) {
6030 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
6031 warnx("can't get hints for 0x%lx", w->id);
6035 if (win->hints->window_group != wmh->window_group)
6038 w->child_trans = win;
6039 win->transient = w->id;
6041 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
6042 "transient to 0x%lx\n", win->transient);
6052 window_get_pid(Window win)
6054 Atom actual_type_return;
6055 int actual_format_return = 0;
6056 unsigned long nitems_return = 0;
6057 unsigned long bytes_after_return = 0;
6061 unsigned char *prop = NULL;
6063 if (XGetWindowProperty(display, win,
6064 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
6065 XA_CARDINAL, &actual_type_return, &actual_format_return,
6066 &nitems_return, &bytes_after_return,
6067 (unsigned char**)(void*)&pid) != Success)
6069 if (actual_type_return != XA_CARDINAL)
6080 if (XGetWindowProperty(display, win,
6081 XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
6082 XA_STRING, &actual_type_return, &actual_format_return,
6083 &nitems_return, &bytes_after_return, &prop) != Success)
6085 if (actual_type_return != XA_STRING)
6090 ret = strtonum((const char *)prop, 0, UINT_MAX, &errstr);
6091 /* ignore error because strtonum returns 0 anyway */
6098 manage_window(Window id)
6101 struct workspace *ws;
6102 struct ws_win *win, *ww;
6103 int format, i, ws_idx, n, border_me = 0;
6104 unsigned long nitems, bytes;
6105 Atom ws_idx_atom = 0, type;
6106 Atom *prot = NULL, *pp;
6107 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
6108 struct swm_region *r;
6115 if ((win = find_window(id)) != NULL)
6116 return (win); /* already being managed */
6118 /* see if we are on the unmanaged list */
6119 if ((win = find_unmanaged_window(id)) != NULL) {
6120 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
6121 "window: 0x%lx\n", win->id);
6122 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6124 set_child_transient(win, &trans);
6126 if (trans && (ww = find_window(trans)))
6127 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
6128 else if ((ww = win->ws->focus) &&
6129 spawn_position == SWM_STACK_ABOVE)
6130 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win, entry);
6131 else if (ww && spawn_position == SWM_STACK_BELOW)
6132 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win, entry);
6133 else switch (spawn_position) {
6136 case SWM_STACK_ABOVE:
6137 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6139 case SWM_STACK_BOTTOM:
6140 case SWM_STACK_BELOW:
6141 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6144 ewmh_update_actions(win);
6148 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6149 err(1, "manage_window: calloc: failed to allocate memory for "
6154 /* see if we need to override the workspace */
6155 p = find_pid(window_get_pid(id));
6157 /* Get all the window data in one shot */
6158 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
6160 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
6161 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
6163 XGetWindowAttributes(display, id, &win->wa);
6164 XGetWMNormalHints(display, id, &win->sh, &win->sh_mask);
6165 win->hints = XGetWMHints(display, id);
6166 XGetTransientForHint(display, id, &trans);
6168 win->transient = trans;
6169 set_child_transient(win, &trans);
6170 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%lx, "
6171 "transient: 0x%lx\n", win->id, win->transient);
6174 /* get supported protocols */
6175 if (XGetWMProtocols(display, id, &prot, &n)) {
6176 for (i = 0, pp = prot; i < n; i++, pp++) {
6177 if (*pp == takefocus)
6178 win->take_focus = 1;
6180 win->can_delete = 1;
6186 win->iconic = get_iconic(win);
6189 * Figure out where to put the window. If it was previously assigned to
6190 * a workspace (either by spawn() or manually moving), and isn't
6191 * transient, * put it in the same workspace
6193 r = root_to_region(win->wa.root);
6195 ws = &r->s->ws[p->ws];
6196 TAILQ_REMOVE(&pidlist, p, entry);
6199 } else if (prop && win->transient == 0) {
6200 DNPRINTF(SWM_D_PROP, "manage_window: get _SWM_WS: %s\n", prop);
6201 ws_idx = strtonum((const char *)prop, 0, workspace_limit - 1,
6204 DNPRINTF(SWM_D_EVENT, "manage_window: window: #%s: %s",
6207 ws = &r->s->ws[ws_idx];
6210 /* this should launch transients in the same ws as parent */
6212 if ((ww = find_window(trans)) != NULL)
6218 warnx("manage_window: fix this "
6224 /* set up the window layout */
6227 win->s = r->s; /* this never changes */
6228 if (trans && (ww = find_window(trans)))
6229 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
6230 else if (spawn_position == SWM_STACK_ABOVE && win->ws->focus)
6231 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win, entry);
6233 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
6235 WIDTH(win) = win->wa.width;
6236 HEIGHT(win) = win->wa.height;
6239 win->g_floatvalid = 0;
6240 win->floatmaxed = 0;
6241 win->ewmh_flags = 0;
6243 /* Set window properties so we can remember this after reincarnation */
6244 if (ws_idx_atom && prop == NULL &&
6245 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
6247 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
6249 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
6250 PropModeReplace, ws_idx_str, strlen((char *)ws_idx_str));
6255 ewmh_autoquirk(win);
6257 if (XGetClassHint(display, win->id, &win->ch)) {
6258 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
6259 win->ch.res_class, win->ch.res_name);
6261 /* java is retarded so treat it special */
6262 if (strstr(win->ch.res_name, "sun-awt")) {
6267 TAILQ_FOREACH(qp, &quirks, entry) {
6268 if (!strcmp(win->ch.res_class, qp->class) &&
6269 !strcmp(win->ch.res_name, qp->name)) {
6270 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
6271 "class: %s, name: %s\n", win->ch.res_class,
6273 if (qp->quirk & SWM_Q_FLOAT) {
6277 win->quirks = qp->quirk;
6282 /* alter window position if quirky */
6283 if (win->quirks & SWM_Q_ANYWHERE) {
6284 win->manual = 1; /* don't center the quirky windows */
6285 bzero(&wc, sizeof wc);
6287 if (bar_enabled && Y(win) < bar_height) {
6288 Y(win) = wc.y = bar_height;
6291 if (WIDTH(win) + X(win) > WIDTH(r)) {
6292 X(win) = wc.x = WIDTH(r) - WIDTH(win) - 2;
6298 /* Reset font sizes (the bruteforce way; no default keybinding). */
6299 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6300 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6301 fake_keypress(win, XK_KP_Subtract, ShiftMask);
6302 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6303 fake_keypress(win, XK_KP_Add, ShiftMask);
6306 ewmh_get_win_state(win);
6307 ewmh_update_actions(win);
6308 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
6312 bzero(&wc, sizeof wc);
6313 wc.border_width = border_width;
6314 mask |= CWBorderWidth;
6315 XConfigureWindow(display, win->id, mask, &wc);
6318 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
6319 PropertyChangeMask | StructureNotifyMask);
6321 /* floaters need to be mapped if they are in the current workspace */
6322 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
6323 XMapRaised(display, win->id);
6329 free_window(struct ws_win *win)
6331 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%lx\n", win->id);
6336 /* needed for restart wm */
6337 set_win_state(win, WithdrawnState);
6339 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6341 if (win->ch.res_class)
6342 XFree(win->ch.res_class);
6343 if (win->ch.res_name)
6344 XFree(win->ch.res_name);
6349 memset(win, 0xff, sizeof *win); /* XXX kill later */
6355 unmanage_window(struct ws_win *win)
6357 struct ws_win *parent;
6362 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%lx\n", win->id);
6364 if (win->transient) {
6365 parent = find_window(win->transient);
6367 parent->child_trans = NULL;
6370 /* focus on root just in case */
6371 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
6380 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6381 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6385 focus_magic(struct ws_win *win)
6387 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%lx\n", WINID(win));
6390 /* if there are no windows clear the status-bar */
6395 if (win->child_trans) {
6396 /* win = parent & has a transient so focus on that */
6398 focus_win(win->child_trans);
6399 if (win->child_trans->take_focus)
6400 client_msg(win, takefocus);
6402 /* make sure transient hasn't disappeared */
6403 if (validate_win(win->child_trans) == 0) {
6404 focus_win(win->child_trans);
6405 if (win->child_trans->take_focus)
6406 client_msg(win->child_trans, takefocus);
6408 win->child_trans = NULL;
6410 if (win->take_focus)
6411 client_msg(win, takefocus);
6417 if (win->take_focus)
6418 client_msg(win, takefocus);
6425 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%lx\n", e->xexpose.window);
6432 XKeyEvent *ev = &e->xkey;
6434 struct swm_region *r;
6436 keysym = XkbKeycodeToKeysym(display, (KeyCode)ev->keycode, 0, 0);
6437 if ((kp = key_lookup(CLEANMASK(ev->state), keysym)) == NULL)
6439 if (keyfuncs[kp->funcid].func == NULL)
6442 r = root_to_region(ev->root);
6443 if (kp->funcid == kf_spawn_custom)
6444 spawn_custom(r, &(keyfuncs[kp->funcid].args), kp->spawn_name);
6446 keyfuncs[kp->funcid].func(r, &(keyfuncs[kp->funcid].args));
6450 buttonpress(XEvent *e)
6454 XButtonPressedEvent *ev = &e->xbutton;
6456 if ((win = find_window(ev->window)) == NULL)
6460 action = client_click;
6462 for (i = 0; i < LENGTH(buttons); i++)
6463 if (action == buttons[i].action && buttons[i].func &&
6464 buttons[i].button == ev->button &&
6465 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
6466 buttons[i].func(win, &buttons[i].args);
6470 configurerequest(XEvent *e)
6472 XConfigureRequestEvent *ev = &e->xconfigurerequest;
6477 if ((win = find_window(ev->window)) == NULL)
6478 if ((win = find_unmanaged_window(ev->window)) == NULL)
6481 DNPRINTF(SWM_D_EVENT, "configurerequest: window: 0x%lx, new: %s\n",
6482 ev->window, YESNO(new));
6485 bzero(&wc, sizeof wc);
6488 wc.width = ev->width;
6489 wc.height = ev->height;
6490 wc.border_width = ev->border_width;
6491 wc.sibling = ev->above;
6492 wc.stack_mode = ev->detail;
6493 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
6495 config_win(win, ev);
6499 configurenotify(XEvent *e)
6503 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%lx\n",
6504 e->xconfigure.window);
6506 win = find_window(e->xconfigure.window);
6508 XGetWMNormalHints(display, win->id, &win->sh, &win->sh_mask);
6512 if (focus_mode == SWM_FOCUS_DEFAULT)
6513 drain_enter_notify();
6518 destroynotify(XEvent *e)
6521 XDestroyWindowEvent *ev = &e->xdestroywindow;
6523 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%lx\n", ev->window);
6525 if ((win = find_window(ev->window)) == NULL) {
6526 if ((win = find_unmanaged_window(ev->window)) == NULL)
6532 /* make sure we focus on something */
6535 unmanage_window(win);
6537 if (focus_mode == SWM_FOCUS_DEFAULT)
6538 drain_enter_notify();
6543 enternotify(XEvent *e)
6545 XCrossingEvent *ev = &e->xcrossing;
6550 Window focus_return;
6551 int revert_to_return;
6553 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%lx, mode: %d, detail: "
6554 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6555 "state: %d\n", ev->window, ev->mode, ev->detail, ev->root,
6556 ev->subwindow, YESNO(ev->same_screen), YESNO(ev->focus), ev->state);
6558 if (ev->mode != NotifyNormal) {
6559 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
6564 switch (focus_mode) {
6565 case SWM_FOCUS_DEFAULT:
6567 case SWM_FOCUS_FOLLOW:
6569 case SWM_FOCUS_SYNERGY:
6572 * all these checks need to be in this order because the
6573 * XCheckTypedWindowEvent relies on weeding out the previous events
6575 * making this code an option would enable a follow mouse for focus
6580 * state is set when we are switching workspaces and focus is set when
6581 * the window or a subwindow already has focus (occurs during restart).
6583 * Only honor the focus flag if last_focus_event is not FocusOut,
6584 * this allows spectrwm to continue to control focus when another
6585 * program is also playing with it.
6587 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
6588 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
6593 * happens when a window is created or destroyed and the border
6594 * crosses the mouse pointer and when switching ws
6596 * we need the subwindow test to see if we came from root in order
6597 * to give focus to floaters
6599 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
6600 ev->subwindow == 0) {
6601 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
6605 /* this window already has focus */
6606 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
6607 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
6611 /* this window is being deleted or moved to another ws */
6612 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
6614 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
6615 XPutBackEvent(display, &cne);
6619 if ((win = find_window(ev->window)) == NULL) {
6620 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6625 * In fullstack kill all enters unless they come from a different ws
6626 * (i.e. another region) or focus has been grabbed externally.
6628 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
6629 last_focus_event != FocusOut) {
6630 XGetInputFocus(display, &focus_return, &revert_to_return);
6631 if ((w = find_window(focus_return)) == NULL ||
6633 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
6641 if ((win = find_window(ev->window)) == NULL) {
6642 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
6647 * if we have more enternotifies let them handle it in due time
6649 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
6650 DNPRINTF(SWM_D_EVENT,
6651 "ignoring enternotify: got more enternotify\n");
6652 XPutBackEvent(display, &cne);
6659 /* lets us use one switch statement for arbitrary mode/detail combinations */
6660 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6663 focusevent(XEvent *e)
6667 u_int32_t mode_detail;
6668 XFocusChangeEvent *ev = &e->xfocus;
6670 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: 0x%lx mode: %d "
6671 "detail: %d\n", ev->type == FocusIn ? "entering" : "leaving",
6672 ev->window, ev->mode, ev->detail);
6674 if (last_focus_event == ev->type) {
6675 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
6679 last_focus_event = ev->type;
6680 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
6682 switch (mode_detail) {
6683 /* synergy client focus operations */
6684 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
6685 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
6687 /* synergy server focus operations */
6688 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
6690 /* Entering applications like rdesktop that mangle the pointer */
6691 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
6693 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
6694 XSetWindowBorder(display, win->id,
6695 win->ws->r->s->c[ev->type == FocusIn ?
6696 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
6699 warnx("ignoring focusevent");
6700 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
6707 mapnotify(XEvent *e)
6710 XMapEvent *ev = &e->xmap;
6712 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%lx\n", ev->window);
6714 win = manage_window(ev->window);
6716 set_win_state(win, NormalState);
6719 * focus_win can only set input focus on a mapped window.
6720 * make sure the window really has focus since it is just being mapped.
6722 if (win->ws->focus == win)
6727 mappingnotify(XEvent *e)
6729 XMappingEvent *ev = &e->xmapping;
6731 XRefreshKeyboardMapping(ev);
6732 if (ev->request == MappingKeyboard)
6737 maprequest(XEvent *e)
6740 struct swm_region *r;
6741 XWindowAttributes wa;
6742 XMapRequestEvent *ev = &e->xmaprequest;
6744 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%lx\n",
6745 e->xmaprequest.window);
6747 if (!XGetWindowAttributes(display, ev->window, &wa))
6749 if (wa.override_redirect)
6752 win = manage_window(e->xmaprequest.window);
6754 return; /* can't happen */
6758 /* make new win focused */
6759 r = root_to_region(win->wa.root);
6760 if (win->ws == r->ws)
6765 propertynotify(XEvent *e)
6768 XPropertyEvent *ev = &e->xproperty;
6771 name = XGetAtomName(display, ev->atom);
6772 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%lx, atom: %s\n",
6777 win = find_window(ev->window);
6781 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
6782 update_iconic(win, 0);
6783 XMapRaised(display, win->id);
6791 case XA_WM_NORMAL_HINTS:
6793 XGetWMNormalHints(display, win->id, &win->sh, &mask);
6794 warnx("normal hints: flag 0x%x", win->sh.flags);
6795 if (win->sh.flags & PMinSize) {
6796 WIDTH(win) = win->sh.min_width;
6797 HEIGHT(win) = win->sh.min_height;
6798 warnx("min %d %d", WIDTH(win), HEIGHT(win));
6800 XMoveResizeWindow(display, win->id,
6801 X(win), Y(win), WIDTH(win), HEIGHT(win));
6813 unmapnotify(XEvent *e)
6817 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%lx\n", e->xunmap.window);
6819 /* determine if we need to help unmanage this window */
6820 win = find_window(e->xunmap.window);
6824 if (getstate(e->xunmap.window) == NormalState) {
6825 unmanage_window(win);
6828 /* giant hack for apps that don't destroy transient windows */
6829 /* eat a bunch of events to prevent remanaging the window */
6831 while (XCheckWindowEvent(display, e->xunmap.window,
6832 EnterWindowMask, &cne))
6834 while (XCheckWindowEvent(display, e->xunmap.window,
6835 StructureNotifyMask, &cne))
6837 while (XCheckWindowEvent(display, e->xunmap.window,
6838 SubstructureNotifyMask, &cne))
6840 /* resend unmap because we ated it */
6841 XUnmapWindow(display, e->xunmap.window);
6844 if (focus_mode == SWM_FOCUS_DEFAULT)
6845 drain_enter_notify();
6849 visibilitynotify(XEvent *e)
6852 struct swm_region *r;
6854 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%lx\n",
6855 e->xvisibility.window);
6856 if (e->xvisibility.state == VisibilityUnobscured)
6857 for (i = 0; i < ScreenCount(display); i++)
6858 TAILQ_FOREACH(r, &screens[i].rl, entry)
6859 if (e->xvisibility.window == r->bar_window)
6864 clientmessage(XEvent *e)
6866 XClientMessageEvent *ev;
6871 win = find_window(ev->window);
6875 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx, type: %ld\n",
6876 ev->window, ev->message_type);
6878 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
6879 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
6882 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
6883 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
6884 if (win->can_delete)
6885 client_msg(win, adelete);
6887 XKillClient(display, win->id);
6889 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
6890 DNPRINTF(SWM_D_EVENT,
6891 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
6892 if (win->floating) {
6893 if (ev->data.l[0] & (1<<8)) /* x */
6894 X(win) = ev->data.l[1];
6895 if (ev->data.l[0] & (1<<9)) /* y */
6896 Y(win) = ev->data.l[2];
6897 if (ev->data.l[0] & (1<<10)) /* width */
6898 WIDTH(win) = ev->data.l[3];
6899 if (ev->data.l[0] & (1<<11)) /* height */
6900 HEIGHT(win) = ev->data.l[4];
6905 /* TODO: Change stack sizes */
6906 /* notify no change was made. */
6907 config_win(win, NULL);
6910 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
6911 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
6912 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
6914 ewmh_update_win_state(win, ev->data.l[2],
6922 xerror_start(Display *d, XErrorEvent *ee)
6929 xerror(Display *d, XErrorEvent *ee)
6931 /* warnx("error: %p %p", display, ee); */
6939 xerrorxlib = XSetErrorHandler(xerror_start);
6941 /* this causes an error if some other window manager is running */
6942 XSelectInput(display, DefaultRootWindow(display),
6943 SubstructureRedirectMask);
6944 XSync(display, False);
6948 XSetErrorHandler(xerror);
6949 XSync(display, False);
6954 new_region(struct swm_screen *s, int x, int y, int w, int h)
6956 struct swm_region *r, *n;
6957 struct workspace *ws = NULL;
6960 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
6961 s->idx, w, h, x, y);
6963 /* remove any conflicting regions */
6964 n = TAILQ_FIRST(&s->rl);
6967 n = TAILQ_NEXT(r, entry);
6968 if (X(r) < (x + w) &&
6969 (X(r) + WIDTH(r)) > x &&
6971 (Y(r) + HEIGHT(r)) > y) {
6972 if (r->ws->r != NULL)
6973 r->ws->old_r = r->ws->r;
6975 XDestroyWindow(display, r->bar_window);
6976 TAILQ_REMOVE(&s->rl, r, entry);
6977 TAILQ_INSERT_TAIL(&s->orl, r, entry);
6981 /* search old regions for one to reuse */
6983 /* size + location match */
6984 TAILQ_FOREACH(r, &s->orl, entry)
6985 if (X(r) == x && Y(r) == y &&
6986 HEIGHT(r) == h && WIDTH(r) == w)
6990 TAILQ_FOREACH(r, &s->orl, entry)
6991 if (HEIGHT(r) == h && WIDTH(r) == w)
6995 TAILQ_REMOVE(&s->orl, r, entry);
6996 /* try to use old region's workspace */
6997 if (r->ws->r == NULL)
7000 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
7001 err(1, "new_region: calloc: failed to allocate memory "
7004 /* if we don't have a workspace already, find one */
7006 for (i = 0; i < workspace_limit; i++)
7007 if (s->ws[i].r == NULL) {
7014 errx(1, "new_region: no free workspaces");
7025 TAILQ_INSERT_TAIL(&s->rl, r, entry);
7031 #ifdef SWM_XRR_HAS_CRTC
7033 XRRScreenResources *sr;
7036 #endif /* SWM_XRR_HAS_CRTC */
7037 struct swm_region *r;
7040 if (i >= ScreenCount(display))
7041 errx(1, "scan_xrandr: invalid screen");
7043 /* remove any old regions */
7044 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
7045 r->ws->old_r = r->ws->r = NULL;
7046 XDestroyWindow(display, r->bar_window);
7047 TAILQ_REMOVE(&screens[i].rl, r, entry);
7048 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
7052 /* map virtual screens onto physical screens */
7053 #ifdef SWM_XRR_HAS_CRTC
7054 if (xrandr_support) {
7055 sr = XRRGetScreenResources(display, screens[i].root);
7057 new_region(&screens[i], 0, 0,
7058 DisplayWidth(display, i),
7059 DisplayHeight(display, i));
7063 for (c = 0, ci = NULL; c < ncrtc; c++) {
7064 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
7065 if (ci->noutput == 0)
7068 if (ci != NULL && ci->mode == None)
7069 new_region(&screens[i], 0, 0,
7070 DisplayWidth(display, i),
7071 DisplayHeight(display, i));
7073 new_region(&screens[i],
7074 ci->x, ci->y, ci->width, ci->height);
7077 XRRFreeCrtcInfo(ci);
7078 XRRFreeScreenResources(sr);
7080 #endif /* SWM_XRR_HAS_CRTC */
7082 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
7083 DisplayHeight(display, i));
7088 screenchange(XEvent *e) {
7089 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
7090 struct swm_region *r;
7093 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%lx\n", xe->root);
7095 if (!XRRUpdateConfiguration(e))
7098 /* silly event doesn't include the screen index */
7099 for (i = 0; i < ScreenCount(display); i++)
7100 if (screens[i].root == xe->root)
7102 if (i >= ScreenCount(display))
7103 errx(1, "screenchange: screen not found");
7105 /* brute force for now, just re-enumerate the regions */
7108 /* add bars to all regions */
7109 for (i = 0; i < ScreenCount(display); i++)
7110 TAILQ_FOREACH(r, &screens[i].rl, entry)
7113 if (focus_mode == SWM_FOCUS_DEFAULT)
7114 drain_enter_notify();
7120 Window d1, d2, *wins = NULL;
7121 XWindowAttributes wa;
7126 for (i = 0; i < ScreenCount(display); i++) {
7127 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
7130 /* attach windows to a region */
7131 /* normal windows */
7132 for (j = 0; j < no; j++) {
7133 if (!XGetWindowAttributes(display, wins[j], &wa) ||
7134 wa.override_redirect ||
7135 XGetTransientForHint(display, wins[j], &d1))
7138 state = getstate(wins[j]);
7139 manage = state == IconicState;
7140 if (wa.map_state == IsViewable || manage)
7141 manage_window(wins[j]);
7143 /* transient windows */
7144 for (j = 0; j < no; j++) {
7145 if (!XGetWindowAttributes(display, wins[j], &wa) ||
7146 wa.override_redirect)
7149 state = getstate(wins[j]);
7150 manage = state == IconicState;
7151 if (XGetTransientForHint(display, wins[j], &d1) &&
7153 manage_window(wins[j]);
7166 int errorbase, major, minor;
7167 struct workspace *ws;
7170 if ((screens = calloc(ScreenCount(display),
7171 sizeof(struct swm_screen))) == NULL)
7172 err(1, "setup_screens: calloc: failed to allocate memory for "
7175 /* initial Xrandr setup */
7176 xrandr_support = XRRQueryExtension(display,
7177 &xrandr_eventbase, &errorbase);
7179 if (XRRQueryVersion(display, &major, &minor) && major < 1)
7182 /* map physical screens */
7183 for (i = 0; i < ScreenCount(display); i++) {
7184 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
7186 TAILQ_INIT(&screens[i].rl);
7187 TAILQ_INIT(&screens[i].orl);
7188 screens[i].root = RootWindow(display, i);
7190 /* set default colors */
7191 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
7192 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
7193 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
7194 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
7195 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
7197 /* create graphics context on screen with default font color */
7198 screens[i].bar_gc = XCreateGC(display, screens[i].root, 0,
7201 XSetForeground(display, screens[i].bar_gc,
7202 screens[i].c[SWM_S_COLOR_BAR_FONT].color);
7204 /* set default cursor */
7205 XDefineCursor(display, screens[i].root,
7206 XCreateFontCursor(display, XC_left_ptr));
7208 /* init all workspaces */
7209 /* XXX these should be dynamically allocated too */
7210 for (j = 0; j < SWM_WS_MAX; j++) {
7211 ws = &screens[i].ws[j];
7217 TAILQ_INIT(&ws->winlist);
7218 TAILQ_INIT(&ws->unmanagedlist);
7220 for (k = 0; layouts[k].l_stack != NULL; k++)
7221 if (layouts[k].l_config != NULL)
7222 layouts[k].l_config(ws,
7223 SWM_ARG_ID_STACKINIT);
7224 ws->cur_layout = &layouts[0];
7225 ws->cur_layout->l_string(ws);
7231 XRRSelectInput(display, screens[i].root,
7232 RRScreenChangeNotifyMask);
7239 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
7240 err(1, "setup_globals: strdup: failed to allocate memory.");
7242 if ((spawn_term[0] = strdup("xterm")) == NULL)
7243 err(1, "setup_globals: strdup: failed to allocate memory.");
7245 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
7246 err(1, "setup_globals: strdup: failed to allocate memory.");
7253 Atom netwmcheck, netwmname, utf8_string;
7256 /* work around sun jdk bugs, code from wmname */
7257 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
7258 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
7259 utf8_string = XInternAtom(display, "UTF8_STRING", False);
7260 for (i = 0; i < ScreenCount(display); i++) {
7261 root = screens[i].root;
7262 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
7263 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
7264 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
7266 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
7267 PropModeReplace, (unsigned char *)&win, 1);
7268 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
7269 PropModeReplace, (unsigned char *)&win, 1);
7270 XChangeProperty(display, win, netwmname, utf8_string, 8,
7271 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
7276 main(int argc, char *argv[])
7278 struct swm_region *r, *rr;
7279 struct ws_win *winfocus = NULL;
7282 char conf[PATH_MAX], *cfile = NULL;
7287 struct sigaction sact;
7290 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
7291 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
7293 warnx("no locale support");
7295 if (!X_HAVE_UTF8_STRING)
7296 warnx("no UTF-8 support");
7298 if (!(display = XOpenDisplay(0)))
7299 errx(1, "can not open display");
7302 errx(1, "other wm running");
7304 /* handle some signals */
7305 bzero(&sact, sizeof(sact));
7306 sigemptyset(&sact.sa_mask);
7308 sact.sa_handler = sighdlr;
7309 sigaction(SIGINT, &sact, NULL);
7310 sigaction(SIGQUIT, &sact, NULL);
7311 sigaction(SIGTERM, &sact, NULL);
7312 sigaction(SIGHUP, &sact, NULL);
7314 sact.sa_handler = sighdlr;
7315 sact.sa_flags = SA_NOCLDSTOP;
7316 sigaction(SIGCHLD, &sact, NULL);
7318 astate = XInternAtom(display, "WM_STATE", False);
7319 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
7320 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
7321 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
7322 a_wmname = XInternAtom(display, "WM_NAME", False);
7323 a_netwmname = XInternAtom(display, "_NET_WM_NAME", False);
7324 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
7325 a_string = XInternAtom(display, "STRING", False);
7326 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
7328 /* look for local and global conf file */
7329 pwd = getpwuid(getuid());
7331 errx(1, "invalid user: %d", getuid());
7340 for (i = 0; ; i++) {
7345 snprintf(conf, sizeof conf, "%s/.%s",
7346 pwd->pw_dir, SWM_CONF_FILE);
7350 snprintf(conf, sizeof conf, "/etc/%s",
7355 snprintf(conf, sizeof conf, "%s/.%s",
7356 pwd->pw_dir, SWM_CONF_FILE_OLD);
7360 snprintf(conf, sizeof conf, "/etc/%s",
7367 if (strlen(conf) && stat(conf, &sb) != -1)
7368 if (S_ISREG(sb.st_mode)) {
7375 /* load conf (if any) and refresh font color in bar graphics contexts */
7376 if (cfile && conf_load(cfile, SWM_CONF_DEFAULT) == 0)
7377 for (i = 0; i < ScreenCount(display); ++i)
7378 XSetForeground(display, screens[i].bar_gc,
7379 screens[i].c[SWM_S_COLOR_BAR_FONT].color);
7382 /* set some values to work around bad programs */
7384 /* grab existing windows (before we build the bars) */
7387 if (getenv("SWM_STARTED") == NULL)
7388 setenv("SWM_STARTED", "YES", 1);
7390 /* setup all bars */
7391 for (i = 0; i < ScreenCount(display); i++)
7392 TAILQ_FOREACH(r, &screens[i].rl, entry) {
7393 if (winfocus == NULL)
7394 winfocus = TAILQ_FIRST(&r->ws->winlist);
7402 if (focus_mode == SWM_FOCUS_DEFAULT)
7403 drain_enter_notify();
7405 xfd = ConnectionNumber(display);
7407 while (XPending(display)) {
7408 XNextEvent(display, &e);
7411 if (e.type < LASTEvent) {
7412 DNPRINTF(SWM_D_EVENTQ ,"XEvent: handled: %s, "
7413 "window: 0x%lx, type: %s (%d), %d remaining"
7414 "\n", YESNO(handler[e.type]),
7415 e.xany.window, geteventname(&e),
7416 e.type, QLength(display));
7418 if (handler[e.type])
7419 handler[e.type](&e);
7421 DNPRINTF(SWM_D_EVENTQ, "XRandr Event: window: "
7422 "0x%lx, type: %s (%d)\n", e.xany.window,
7423 xrandr_geteventname(&e), e.type);
7425 switch (e.type - xrandr_eventbase) {
7426 case RRScreenChangeNotify:
7435 /* if we are being restarted go focus on first window */
7437 rr = winfocus->ws->r;
7439 /* not a visible window */
7443 /* move pointer to first screen if multi screen */
7444 if (ScreenCount(display) > 1 || outputs > 1)
7445 XWarpPointer(display, None, rr->s[0].root,
7447 Y(rr) + (bar_enabled ? bar_height : 0));
7449 a.id = SWM_ARG_ID_FOCUSCUR;
7459 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
7461 DNPRINTF(SWM_D_MISC, "select failed");
7462 if (restart_wm == 1)
7463 restart(NULL, NULL);
7464 if (search_resp == 1)
7477 for (i = 0; i < ScreenCount(display); ++i)
7478 if (screens[i].bar_gc != NULL)
7479 XFreeGC(display, screens[i].bar_gc);
7481 XFreeFontSet(display, bar_fs);
7482 XCloseDisplay(display);