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, 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_window_name(char *s, size_t sz, struct swm_region *r)
1374 unsigned char *title;
1376 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
1378 if ((title = get_win_name(r->ws->focus->id)) == NULL)
1381 if (r->ws->focus->floating)
1382 strlcat(s, "(f) ", sz);
1383 strlcat(s, (char *)title, sz);
1388 int urgent[SWM_WS_MAX];
1390 bar_urgent(char *s, size_t sz)
1392 XWMHints *wmh = NULL;
1397 for (i = 0; i < workspace_limit; i++)
1400 for (i = 0; i < ScreenCount(display); i++)
1401 for (j = 0; j < workspace_limit; j++)
1402 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
1403 wmh = XGetWMHints(display, win->id);
1407 if (wmh->flags & XUrgencyHint)
1412 for (i = 0; i < workspace_limit; i++) {
1414 snprintf(b, sizeof b, "%d ", i + 1);
1416 snprintf(b, sizeof b, "- ");
1422 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
1424 if (r == NULL || r->ws == NULL)
1426 if (r->ws->name != NULL)
1427 strlcat(s, r->ws->name, sz);
1430 /* build the default bar format according to the defined enabled options */
1432 bar_fmt(char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
1434 /* if format provided, just copy the buffers */
1435 if (bar_format != NULL) {
1436 strlcpy(fmtnew, fmtexp, sz);
1440 /* reset the output buffer */
1443 strlcat(fmtnew, "+N:+I ", sz);
1445 strlcat(fmtnew, "+S", sz);
1446 strlcat(fmtnew, " ", sz);
1448 /* only show the workspace name if there's actually one */
1449 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
1450 strlcat(fmtnew, "<+D>", sz);
1451 strlcat(fmtnew, " ", sz);
1453 if (clock_enabled) {
1454 strlcat(fmtnew, fmtexp, sz);
1455 strlcat(fmtnew, " ", sz);
1458 /* bar_urgent already adds the space before the last asterisk */
1460 strlcat(fmtnew, "* +U* ", sz);
1462 if (title_class_enabled)
1463 strlcat(fmtnew, "+C", sz);
1464 if (title_name_enabled) {
1465 /* add a colon if showing the class and something is focused */
1466 if (title_class_enabled && r != NULL && r->ws != NULL &&
1467 r->ws->focus != NULL)
1468 strlcat(fmtnew, ":", sz);
1469 strlcat(fmtnew, "+T ", sz);
1472 if (window_name_enabled)
1473 strlcat(fmtnew, "+64W ", sz);
1475 /* finally add the action script output and the version */
1476 strlcat(fmtnew, " +A +V", sz);
1479 /* replaces the bar format character sequences (like in tmux(1)) */
1481 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
1485 char num[8], tmp[SWM_BAR_MAX];
1487 size_t len, numoff = 0;
1489 /* reset strlcat(3) buffer */
1492 /* get number, if any */
1494 while (*fmt != '\0' && isdigit((unsigned char) *fmt)) {
1495 if (numoff >= sizeof num - 1)
1497 num[numoff++] = *fmt++;
1501 if ((limit = strtonum(num, 1, sizeof tmp - 1, NULL)) == 0)
1502 limit = sizeof tmp - 1;
1504 /* if number is too big, skip to the first non-digit */
1505 if (numoff >= sizeof num - 1) {
1506 while (*fmt != '\0' && isdigit((unsigned char) *fmt))
1509 /* there is nothing to replace (ie EOL) */
1515 snprintf(tmp, sizeof tmp, "%s", bar_ext);
1518 bar_class_name(tmp, sizeof tmp, r);
1521 bar_workspace_name(tmp, sizeof tmp, r);
1524 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
1527 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
1530 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
1533 bar_title_name(tmp, sizeof tmp, r);
1536 bar_urgent(tmp, sizeof tmp);
1539 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
1542 bar_window_name(tmp, sizeof tmp, r);
1545 /* unknown character sequence; copy as-is */
1546 snprintf(tmp, sizeof tmp, "+%c", *fmt);
1554 while (limit-- > 0) {
1555 if (*offrep >= sz - 1)
1557 fmtrep[(*offrep)++] = *ptr++;
1565 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
1570 while (*fmt != '\0') {
1572 /* skip ordinary characters */
1575 fmtrep[off++] = *fmt++;
1579 /* character sequence found; replace it */
1580 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
1589 bar_fmt_expand(char *fmtexp, size_t sz)
1596 /* start by grabbing the current time and date */
1598 localtime_r(&tmt, &tm);
1600 /* figure out what to expand */
1601 if (bar_format != NULL)
1603 else if (bar_format == NULL && clock_enabled)
1605 /* if nothing to expand bail out */
1611 /* copy as-is, just in case the format shouldn't be expanded below */
1612 strlcpy(fmtexp, fmt, sz);
1613 /* finally pass the string through strftime(3) */
1614 #ifndef SWM_DENY_CLOCK_FORMAT
1615 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
1616 warnx("format too long");
1624 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
1625 char fmtrep[SWM_BAR_MAX];
1627 struct swm_region *r;
1629 /* expand the format by first passing it through strftime(3) */
1630 bar_fmt_expand(fmtexp, sizeof fmtexp);
1632 for (i = 0; i < ScreenCount(display); i++) {
1633 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1634 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
1635 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
1636 bar_print(r, fmtrep);
1647 if (bar_enabled == 0)
1649 if (bar_extra && bar_extra_running) {
1650 /* ignore short reads; it'll correct itself */
1651 while ((b = fgetln(stdin, &len)) != NULL)
1652 if (b && b[len - 1] == '\n') {
1654 strlcpy((char *)bar_ext, b, sizeof bar_ext);
1656 if (b == NULL && errno != EAGAIN) {
1657 warn("bar_update: bar_extra failed");
1661 strlcpy((char *)bar_ext, "", sizeof bar_ext);
1674 bar_toggle(struct swm_region *r, union arg *args)
1676 struct swm_region *tmpr;
1677 int i, sc = ScreenCount(display);
1679 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
1682 for (i = 0; i < sc; i++)
1683 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1684 XUnmapWindow(display, tmpr->bar_window);
1686 for (i = 0; i < sc; i++)
1687 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1688 XMapRaised(display, tmpr->bar_window);
1690 bar_enabled = !bar_enabled;
1693 /* must be after stack */
1700 XSetWindowAttributes wa;
1701 struct swm_region *r;
1704 /* do this here because the conf file is in memory */
1705 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1706 /* launch external status app */
1707 bar_extra_running = 1;
1708 if (pipe(bar_pipe) == -1)
1709 err(1, "pipe error");
1710 socket_setnonblock(bar_pipe[0]);
1711 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1712 if (dup2(bar_pipe[0], 0) == -1)
1714 if (dup2(bar_pipe[1], 1) == -1)
1716 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1717 err(1, "could not disable SIGPIPE");
1718 switch (bar_pid = fork()) {
1720 err(1, "cannot fork");
1724 execvp(bar_argv[0], bar_argv);
1725 err(1, "%s external app failed", bar_argv[0]);
1727 default: /* parent */
1733 bzero(&wa, sizeof wa);
1734 for (i = 0; i < ScreenCount(display); i++)
1735 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1737 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1738 wa.background_pixel =
1739 screens[i].c[SWM_S_COLOR_BAR].color;
1740 XChangeWindowAttributes(display, r->bar_window,
1741 CWBackPixel | CWBorderPixel, &wa);
1747 bar_setup(struct swm_region *r)
1749 char *default_string;
1750 char **missing_charsets;
1751 int num_missing_charsets = 0;
1755 XFreeFontSet(display, bar_fs);
1760 DNPRINTF(SWM_D_BAR, "bar_setup: loading bar_fonts: %s\n", bar_fonts);
1762 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
1763 &num_missing_charsets, &default_string);
1765 if (num_missing_charsets > 0) {
1766 warnx("Unable to load charset(s):");
1768 for (i = 0; i < num_missing_charsets; ++i)
1769 warnx("%s", missing_charsets[i]);
1771 XFreeStringList(missing_charsets);
1773 if (strcmp(default_string, ""))
1774 warnx("Glyphs from those sets will be replaced "
1775 "by '%s'.", default_string);
1777 warnx("Glyphs from those sets won't be drawn.");
1781 errx(1, "Error creating font set structure.");
1783 bar_fs_extents = XExtentsOfFontSet(bar_fs);
1785 bar_height = bar_fs_extents->max_logical_extent.height +
1786 2 * bar_border_width;
1792 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1794 r->bar_window = XCreateSimpleWindow(display,
1795 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width,
1796 bar_height - 2 * bar_border_width,
1797 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1798 r->s->c[SWM_S_COLOR_BAR].color);
1799 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1801 XMapRaised(display, r->bar_window);
1802 DNPRINTF(SWM_D_BAR, "bar_setup: bar_window: 0x%lx\n", r->bar_window);
1804 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1805 err(1, "could not install bar_signal");
1810 drain_enter_notify(void)
1815 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1818 DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained: %d\n", i);
1822 set_win_state(struct ws_win *win, long state)
1824 long data[] = {state, None};
1826 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%lx\n", win->id);
1831 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1832 (unsigned char *)data, 2);
1839 unsigned char *p = NULL;
1842 if (!get_property(w, astate, 2L, astate, &n, NULL, &p))
1845 result = *((long *)p);
1851 version(struct swm_region *r, union arg *args)
1853 bar_version = !bar_version;
1855 snprintf(bar_vertext, sizeof bar_vertext,
1856 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
1858 strlcpy(bar_vertext, "", sizeof bar_vertext);
1863 client_msg(struct ws_win *win, Atom a)
1865 XClientMessageEvent cm;
1870 bzero(&cm, sizeof cm);
1871 cm.type = ClientMessage;
1872 cm.window = win->id;
1873 cm.message_type = aprot;
1876 cm.data.l[1] = CurrentTime;
1877 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1880 /* synthetic response to a ConfigureRequest when not making a change */
1882 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1889 /* send notification of unchanged state. */
1890 ce.type = ConfigureNotify;
1893 ce.width = WIDTH(win);
1894 ce.height = HEIGHT(win);
1895 ce.override_redirect = False;
1899 ce.display = display;
1901 ce.window = win->id;
1902 ce.border_width = border_width;
1906 ce.display = ev->display;
1907 ce.event = ev->window;
1908 ce.window = ev->window;
1910 /* make response appear more WM_SIZE_HINTS-compliant */
1912 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%lx,"
1913 " sh_mask: %ld, min: %d x %d, max: %d x %d, inc: "
1914 "%d x %d\n", win->id, win->sh_mask, SH_MIN_W(win),
1915 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
1916 SH_INC_W(win), SH_INC_H(win));
1920 /* the hint may be set... to 0! */
1921 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
1922 ce.width = SH_MIN_W(win);
1923 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
1924 ce.height = SH_MIN_H(win);
1929 /* may also be advertized as 0 */
1930 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
1931 ce.width = SH_MAX_W(win);
1932 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
1933 ce.height = SH_MAX_H(win);
1936 /* resize increment. */
1938 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
1939 ce.width -= (ce.width - SH_MIN_W(win)) %
1941 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
1942 ce.height -= (ce.height - SH_MIN_H(win)) %
1946 /* adjust x and y for requested border_width. */
1947 ce.x += border_width - ev->border_width;
1948 ce.y += border_width - ev->border_width;
1949 ce.border_width = ev->border_width;
1950 ce.above = ev->above;
1953 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%lx, (x,y) w x h: "
1954 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
1955 ce.y, ce.width, ce.height, ce.border_width);
1957 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1961 count_win(struct workspace *ws, int count_transient)
1966 TAILQ_FOREACH(win, &ws->winlist, entry) {
1967 if (count_transient == 0 && win->floating)
1969 if (count_transient == 0 && win->transient)
1975 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1981 quit(struct swm_region *r, union arg *args)
1983 DNPRINTF(SWM_D_MISC, "quit\n");
1988 unmap_window(struct ws_win *win)
1993 /* don't unmap again */
1994 if (getstate(win->id) == IconicState)
1997 set_win_state(win, IconicState);
1999 XUnmapWindow(display, win->id);
2000 XSetWindowBorder(display, win->id,
2001 win->s->c[SWM_S_COLOR_UNFOCUS].color);
2010 for (i = 0; i < ScreenCount(display); i++)
2011 for (j = 0; j < workspace_limit; j++)
2012 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2017 fake_keypress(struct ws_win *win, int keysym, int modifiers)
2024 event.display = display; /* Ignored, but what the hell */
2025 event.window = win->id;
2026 event.root = win->s->root;
2027 event.subwindow = None;
2028 event.time = CurrentTime;
2033 event.same_screen = True;
2034 event.keycode = XKeysymToKeycode(display, keysym);
2035 event.state = modifiers;
2037 event.type = KeyPress;
2038 XSendEvent(event.display, event.window, True,
2039 KeyPressMask, (XEvent *)&event);
2041 event.type = KeyRelease;
2042 XSendEvent(event.display, event.window, True,
2043 KeyPressMask, (XEvent *)&event);
2048 restart(struct swm_region *r, union arg *args)
2050 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
2052 /* disable alarm because the following code may not be interrupted */
2054 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
2055 err(1, "can't disable alarm");
2060 XCloseDisplay(display);
2061 execvp(start_argv[0], start_argv);
2062 warn("execvp failed");
2067 root_to_region(Window root)
2069 struct swm_region *r = NULL;
2071 int i, x, y, wx, wy;
2074 for (i = 0; i < ScreenCount(display); i++)
2075 if (screens[i].root == root)
2078 if (XQueryPointer(display, screens[i].root,
2079 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
2080 /* choose a region based on pointer location */
2081 TAILQ_FOREACH(r, &screens[i].rl, entry)
2082 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
2083 y >= Y(r) && y <= Y(r) + HEIGHT(r))
2088 r = TAILQ_FIRST(&screens[i].rl);
2094 find_unmanaged_window(Window id)
2099 for (i = 0; i < ScreenCount(display); i++)
2100 for (j = 0; j < workspace_limit; j++)
2101 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
2109 find_window(Window id)
2112 Window wrr, wpr, *wcr = NULL;
2116 for (i = 0; i < ScreenCount(display); i++)
2117 for (j = 0; j < workspace_limit; j++)
2118 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2122 /* if we were looking for the parent return that window instead */
2123 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
2128 /* ignore not found and root */
2129 if (wpr == 0 || wrr == wpr)
2132 /* look for parent */
2133 for (i = 0; i < ScreenCount(display); i++)
2134 for (j = 0; j < workspace_limit; j++)
2135 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2143 spawn(int ws_idx, union arg *args, int close_fd)
2148 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
2151 close(ConnectionNumber(display));
2153 setenv("LD_PRELOAD", SWM_LIB, 1);
2155 if (asprintf(&ret, "%d", ws_idx) == -1) {
2156 warn("spawn: asprintf SWM_WS");
2159 setenv("_SWM_WS", ret, 1);
2163 if (asprintf(&ret, "%d", getpid()) == -1) {
2164 warn("spawn: asprintf _SWM_PID");
2167 setenv("_SWM_PID", ret, 1);
2171 if (setsid() == -1) {
2172 warn("spawn: setsid");
2178 * close stdin and stdout to prevent interaction between apps
2179 * and the baraction script
2180 * leave stderr open to record errors
2182 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
2183 warn("spawn: open");
2186 dup2(fd, STDIN_FILENO);
2187 dup2(fd, STDOUT_FILENO);
2192 execvp(args->argv[0], args->argv);
2194 warn("spawn: execvp");
2199 spawnterm(struct swm_region *r, union arg *args)
2201 DNPRINTF(SWM_D_MISC, "spawnterm\n");
2205 setenv("_SWM_XTERM_FONTADJ", "", 1);
2206 spawn(r->ws->idx, args, 1);
2211 kill_refs(struct ws_win *win)
2214 struct swm_region *r;
2215 struct workspace *ws;
2220 for (i = 0; i < ScreenCount(display); i++)
2221 TAILQ_FOREACH(r, &screens[i].rl, entry)
2222 for (x = 0; x < workspace_limit; x++) {
2224 if (win == ws->focus)
2226 if (win == ws->focus_prev)
2227 ws->focus_prev = NULL;
2232 validate_win(struct ws_win *testwin)
2235 struct workspace *ws;
2236 struct swm_region *r;
2239 if (testwin == NULL)
2242 for (i = 0; i < ScreenCount(display); i++)
2243 TAILQ_FOREACH(r, &screens[i].rl, entry)
2244 for (x = 0; x < workspace_limit; x++) {
2246 TAILQ_FOREACH(win, &ws->winlist, entry)
2254 validate_ws(struct workspace *testws)
2256 struct swm_region *r;
2257 struct workspace *ws;
2260 /* validate all ws */
2261 for (i = 0; i < ScreenCount(display); i++)
2262 TAILQ_FOREACH(r, &screens[i].rl, entry)
2263 for (x = 0; x < workspace_limit; x++) {
2272 unfocus_win(struct ws_win *win)
2277 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%lx\n", WINID(win));
2281 if (win->ws == NULL)
2284 if (validate_ws(win->ws))
2285 return; /* XXX this gets hit with thunderbird, needs fixing */
2287 if (win->ws->r == NULL)
2290 if (validate_win(win)) {
2295 if (win->ws->focus == win) {
2296 win->ws->focus = NULL;
2297 win->ws->focus_prev = win;
2300 if (validate_win(win->ws->focus)) {
2301 kill_refs(win->ws->focus);
2302 win->ws->focus = NULL;
2304 if (validate_win(win->ws->focus_prev)) {
2305 kill_refs(win->ws->focus_prev);
2306 win->ws->focus_prev = NULL;
2309 /* drain all previous unfocus events */
2310 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
2313 grabbuttons(win, 0);
2314 XSetWindowBorder(display, win->id,
2315 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2317 XChangeProperty(display, win->s->root,
2318 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2319 PropModeReplace, (unsigned char *)&none, 1);
2328 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
2330 for (i = 0; i < ScreenCount(display); i++)
2331 for (j = 0; j < workspace_limit; j++)
2332 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
2337 focus_win(struct ws_win *win)
2342 struct ws_win *cfw = NULL;
2345 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%lx\n", WINID(win));
2349 if (win->ws == NULL)
2352 if (validate_ws(win->ws))
2353 return; /* XXX this gets hit with thunderbird, needs fixing */
2355 if (validate_win(win)) {
2360 if (validate_win(win)) {
2365 XGetInputFocus(display, &cur_focus, &rr);
2366 if ((cfw = find_window(cur_focus)) != NULL)
2369 /* use larger hammer since the window was killed somehow */
2370 TAILQ_FOREACH(cfw, &win->ws->winlist, entry)
2371 if (cfw->ws && cfw->ws->r && cfw->ws->r->s)
2372 XSetWindowBorder(display, cfw->id,
2373 cfw->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
2376 win->ws->focus = win;
2378 if (win->ws->r != NULL) {
2379 /* drain all previous focus events */
2380 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
2384 XSetInputFocus(display, win->id,
2385 RevertToParent, CurrentTime);
2386 grabbuttons(win, 1);
2387 XSetWindowBorder(display, win->id,
2388 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
2389 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
2390 win->ws->always_raise)
2391 XMapRaised(display, win->id);
2393 XChangeProperty(display, win->s->root,
2394 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
2395 PropModeReplace, (unsigned char *)&win->id, 1);
2402 switchws(struct swm_region *r, union arg *args)
2404 int wsid = args->id, unmap_old = 0;
2405 struct swm_region *this_r, *other_r;
2407 struct workspace *new_ws, *old_ws;
2413 if (wsid >= workspace_limit)
2417 old_ws = this_r->ws;
2418 new_ws = &this_r->s->ws[wsid];
2420 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
2421 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
2423 if (new_ws == NULL || old_ws == NULL)
2425 if (new_ws == old_ws)
2428 other_r = new_ws->r;
2429 if (other_r == NULL) {
2430 /* the other workspace is hidden, hide this one */
2434 /* the other ws is visible in another region, exchange them */
2435 other_r->ws_prior = new_ws;
2436 other_r->ws = old_ws;
2437 old_ws->r = other_r;
2439 this_r->ws_prior = old_ws;
2440 this_r->ws = new_ws;
2443 /* this is needed so that we can click on a window after a restart */
2447 a.id = SWM_ARG_ID_FOCUSCUR;
2448 focus(new_ws->r, &a);
2450 /* unmap old windows */
2452 TAILQ_FOREACH(win, &old_ws->winlist, entry)
2455 if (focus_mode == SWM_FOCUS_DEFAULT)
2456 drain_enter_notify();
2460 cyclews(struct swm_region *r, union arg *args)
2463 struct swm_screen *s = r->s;
2466 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2467 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2472 case SWM_ARG_ID_CYCLEWS_UP_ALL:
2475 case SWM_ARG_ID_CYCLEWS_UP:
2476 if (a.id < workspace_limit - 1)
2481 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
2484 case SWM_ARG_ID_CYCLEWS_DOWN:
2488 a.id = workspace_limit - 1;
2495 (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist)))
2497 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2501 } while (a.id != r->ws->idx);
2505 priorws(struct swm_region *r, union arg *args)
2509 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2510 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2512 if (r->ws_prior == NULL)
2515 a.id = r->ws_prior->idx;
2520 cyclescr(struct swm_region *r, union arg *args)
2522 struct swm_region *rr = NULL;
2526 /* do nothing if we don't have more than one screen */
2527 if (!(ScreenCount(display) > 1 || outputs > 1))
2532 case SWM_ARG_ID_CYCLESC_UP:
2533 rr = TAILQ_NEXT(r, entry);
2535 rr = TAILQ_FIRST(&screens[i].rl);
2537 case SWM_ARG_ID_CYCLESC_DOWN:
2538 rr = TAILQ_PREV(r, swm_region_list, entry);
2540 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2548 /* move mouse to region */
2550 y = Y(rr) + 1 + (bar_enabled ? bar_height : 0);
2551 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2553 a.id = SWM_ARG_ID_FOCUSCUR;
2556 if (rr->ws->focus) {
2557 /* move to focus window */
2558 x = X(rr->ws->focus) + 1;
2559 y = Y(rr->ws->focus) + 1;
2560 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2565 sort_windows(struct ws_win_list *wl)
2567 struct ws_win *win, *parent, *nxt;
2572 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2573 nxt = TAILQ_NEXT(win, entry);
2574 if (win->transient) {
2575 parent = find_window(win->transient);
2576 if (parent == NULL) {
2577 warnx("not possible bug");
2580 TAILQ_REMOVE(wl, win, entry);
2581 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2588 swapwin(struct swm_region *r, union arg *args)
2590 struct ws_win *target, *source;
2591 struct ws_win *cur_focus;
2592 struct ws_win_list *wl;
2595 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
2596 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2598 cur_focus = r->ws->focus;
2599 if (cur_focus == NULL)
2603 wl = &source->ws->winlist;
2606 case SWM_ARG_ID_SWAPPREV:
2607 if (source->transient)
2608 source = find_window(source->transient);
2609 target = TAILQ_PREV(source, ws_win_list, entry);
2610 if (target && target->transient)
2611 target = find_window(target->transient);
2612 TAILQ_REMOVE(wl, source, entry);
2614 TAILQ_INSERT_TAIL(wl, source, entry);
2616 TAILQ_INSERT_BEFORE(target, source, entry);
2618 case SWM_ARG_ID_SWAPNEXT:
2619 target = TAILQ_NEXT(source, entry);
2620 /* move the parent and let the sort handle the move */
2621 if (source->transient)
2622 source = find_window(source->transient);
2623 TAILQ_REMOVE(wl, source, entry);
2625 TAILQ_INSERT_HEAD(wl, source, entry);
2627 TAILQ_INSERT_AFTER(wl, target, source, entry);
2629 case SWM_ARG_ID_SWAPMAIN:
2630 target = TAILQ_FIRST(wl);
2631 if (target == source) {
2632 if (source->ws->focus_prev != NULL &&
2633 source->ws->focus_prev != target)
2634 source = source->ws->focus_prev;
2638 if (target == NULL || source == NULL)
2640 source->ws->focus_prev = target;
2641 TAILQ_REMOVE(wl, target, entry);
2642 TAILQ_INSERT_BEFORE(source, target, entry);
2643 TAILQ_REMOVE(wl, source, entry);
2644 TAILQ_INSERT_HEAD(wl, source, entry);
2646 case SWM_ARG_ID_MOVELAST:
2647 TAILQ_REMOVE(wl, source, entry);
2648 TAILQ_INSERT_TAIL(wl, source, entry);
2651 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
2661 focus_prev(struct ws_win *win)
2663 struct ws_win *winfocus = NULL;
2664 struct ws_win *cur_focus = NULL;
2665 struct ws_win_list *wl = NULL;
2666 struct workspace *ws = NULL;
2668 if (!(win && win->ws))
2673 cur_focus = ws->focus;
2675 DNPRINTF(SWM_D_FOCUS, "focus_prev: window: 0x%lx, cur_focus: 0x%lx\n",
2676 WINID(win), WINID(cur_focus));
2678 /* pickle, just focus on whatever */
2679 if (cur_focus == NULL) {
2680 /* use prev_focus if valid */
2681 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2682 find_window(WINID(ws->focus_prev)))
2683 winfocus = ws->focus_prev;
2687 /* if transient focus on parent */
2688 if (cur_focus->transient) {
2689 winfocus = find_window(cur_focus->transient);
2693 /* if in max_stack try harder */
2694 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2695 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2696 if (cur_focus != ws->focus_prev)
2697 winfocus = ws->focus_prev;
2699 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2704 DNPRINTF(SWM_D_FOCUS, "focus_prev: focus_close: %d\n", focus_close);
2706 if (winfocus == NULL || winfocus == win) {
2707 switch (focus_close) {
2708 case SWM_STACK_BOTTOM:
2709 winfocus = TAILQ_FIRST(wl);
2712 winfocus = TAILQ_LAST(wl, ws_win_list);
2714 case SWM_STACK_ABOVE:
2715 if ((winfocus = TAILQ_NEXT(cur_focus, entry)) == NULL) {
2716 if (focus_close_wrap)
2717 winfocus = TAILQ_FIRST(wl);
2719 winfocus = TAILQ_PREV(cur_focus,
2720 ws_win_list, entry);
2723 case SWM_STACK_BELOW:
2724 if ((winfocus = TAILQ_PREV(cur_focus, ws_win_list,
2726 if (focus_close_wrap)
2727 winfocus = TAILQ_LAST(wl, ws_win_list);
2729 winfocus = TAILQ_NEXT(cur_focus, entry);
2735 if (winfocus == NULL) {
2736 if (focus_default == SWM_STACK_TOP)
2737 winfocus = TAILQ_LAST(wl, ws_win_list);
2739 winfocus = TAILQ_FIRST(wl);
2743 focus_magic(winfocus);
2747 focus(struct swm_region *r, union arg *args)
2749 struct ws_win *winfocus = NULL, *head;
2750 struct ws_win *cur_focus = NULL;
2751 struct ws_win_list *wl = NULL;
2752 struct workspace *ws = NULL;
2758 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
2760 /* treat FOCUS_CUR special */
2761 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2762 if (r->ws->focus && r->ws->focus->iconic == 0)
2763 winfocus = r->ws->focus;
2764 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2765 winfocus = r->ws->focus_prev;
2767 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2768 if (winfocus->iconic == 0)
2771 focus_magic(winfocus);
2775 if ((cur_focus = r->ws->focus) == NULL)
2779 if (TAILQ_EMPTY(wl))
2781 /* make sure there is at least one uniconified window */
2783 TAILQ_FOREACH(winfocus, wl, entry)
2784 if (winfocus->iconic == 0) {
2792 case SWM_ARG_ID_FOCUSPREV:
2793 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2795 head = TAILQ_LAST(wl, ws_win_list);
2797 if (WINID(winfocus) == cur_focus->transient) {
2798 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2800 head = TAILQ_LAST(wl, ws_win_list);
2805 if (winfocus && winfocus->iconic) {
2806 while (winfocus != cur_focus) {
2807 if (winfocus == NULL)
2808 winfocus = TAILQ_LAST(wl, ws_win_list);
2809 if (winfocus->iconic == 0)
2811 winfocus = TAILQ_PREV(winfocus, ws_win_list,
2817 case SWM_ARG_ID_FOCUSNEXT:
2818 head = TAILQ_NEXT(cur_focus, entry);
2820 head = TAILQ_FIRST(wl);
2824 if (winfocus && winfocus->iconic) {
2825 while (winfocus != cur_focus) {
2826 if (winfocus == NULL)
2827 winfocus = TAILQ_FIRST(wl);
2828 if (winfocus->iconic == 0)
2830 winfocus = TAILQ_NEXT(winfocus, entry);
2835 case SWM_ARG_ID_FOCUSMAIN:
2836 winfocus = TAILQ_FIRST(wl);
2837 if (winfocus == cur_focus)
2838 winfocus = cur_focus->ws->focus_prev;
2845 focus_magic(winfocus);
2849 cycle_layout(struct swm_region *r, union arg *args)
2851 struct workspace *ws = r->ws;
2854 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2857 if (ws->cur_layout->l_stack == NULL)
2858 ws->cur_layout = &layouts[0];
2861 if (focus_mode == SWM_FOCUS_DEFAULT)
2862 drain_enter_notify();
2863 a.id = SWM_ARG_ID_FOCUSCUR;
2868 stack_config(struct swm_region *r, union arg *args)
2870 struct workspace *ws = r->ws;
2872 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
2875 if (ws->cur_layout->l_config != NULL)
2876 ws->cur_layout->l_config(ws, args->id);
2878 if (args->id != SWM_ARG_ID_STACKINIT)
2885 struct swm_geometry g;
2886 struct swm_region *r;
2892 DNPRINTF(SWM_D_STACK, "stack: begin\n");
2894 for (i = 0; i < ScreenCount(display); i++) {
2898 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2899 DNPRINTF(SWM_D_STACK, "stack: workspace: %d "
2900 "(screen: %d, region: %d)\n", r->ws->idx, i, j++);
2902 /* start with screen geometry, adjust for bar */
2904 g.w -= 2 * border_width;
2905 g.h -= 2 * border_width;
2911 r->ws->cur_layout->l_stack(r->ws, &g);
2912 r->ws->cur_layout->l_string(r->ws);
2913 /* save r so we can track region changes */
2920 if (focus_mode == SWM_FOCUS_DEFAULT)
2921 drain_enter_notify();
2923 DNPRINTF(SWM_D_STACK, "stack: end\n");
2927 store_float_geom(struct ws_win *win, struct swm_region *r)
2929 /* retain window geom and region geom */
2930 win->g_float = win->g;
2931 win->rg_float = r->g;
2932 win->g_floatvalid = 1;
2936 stack_floater(struct ws_win *win, struct swm_region *r)
2944 bzero(&wc, sizeof wc);
2945 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2948 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2949 * geom on ws switches or return from max mode
2951 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2952 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2954 * use stored g and rg to set relative position and size
2955 * as in old region or before max stack mode
2957 X(win) = win->g_float.x - win->rg_float.x + X(r);
2958 Y(win) = win->g_float.y - win->rg_float.y + Y(r);
2959 WIDTH(win) = win->g_float.w;
2960 HEIGHT(win) = win->g_float.h;
2961 win->g_floatvalid = 0;
2964 win->floatmaxed = 0;
2966 if ((win->quirks & SWM_Q_FULLSCREEN) && (WIDTH(win) >= WIDTH(r)) &&
2967 (HEIGHT(win) >= HEIGHT(r)))
2968 wc.border_width = 0;
2970 wc.border_width = border_width;
2971 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2972 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
2973 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
2978 * floaters and transients are auto-centred unless moved
2981 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 - wc.border_width;
2982 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 - wc.border_width;
2985 /* win can be outside r if new r smaller than old r */
2986 /* Ensure top left corner inside r (move probs otherwise) */
2987 if (X(win) < X(r) - wc.border_width)
2988 X(win) = X(r) - wc.border_width;
2989 if (X(win) > X(r) + WIDTH(r) - 1)
2990 X(win) = (WIDTH(win) > WIDTH(r)) ? X(r) :
2991 (X(r) + WIDTH(r) - WIDTH(win) - 2 * wc.border_width);
2992 if (Y(win) < Y(r) - wc.border_width)
2993 Y(win) = Y(r) - wc.border_width;
2994 if (Y(win) > Y(r) + HEIGHT(r) - 1)
2995 Y(win) = (HEIGHT(win) > HEIGHT(r)) ? Y(r) :
2996 (Y(r) + HEIGHT(r) - HEIGHT(win) - 2 * wc.border_width);
3000 wc.width = WIDTH(win);
3001 wc.height = HEIGHT(win);
3004 * Retain floater and transient geometry for correct positioning
3005 * when ws changes region
3007 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
3008 store_float_geom(win, r);
3010 DNPRINTF(SWM_D_MISC, "stack_floater: window: %lu, (x,y) w x h: (%d,%d) "
3011 "%d x %d\n", win->id, wc.x, wc.y, wc.width, wc.height);
3013 XConfigureWindow(display, win->id, mask, &wc);
3017 * Send keystrokes to terminal to decrease/increase the font size as the
3018 * window size changes.
3021 adjust_font(struct ws_win *win)
3023 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
3024 win->floating || win->transient)
3027 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
3028 WIDTH(win) / win->sh.width_inc < term_width &&
3029 win->font_steps < SWM_MAX_FONT_STEPS) {
3030 win->font_size_boundary[win->font_steps] =
3031 (win->sh.width_inc * term_width) + win->sh.base_width;
3034 win->last_inc = win->sh.width_inc;
3035 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3036 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
3037 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
3040 win->last_inc = win->sh.width_inc;
3041 fake_keypress(win, XK_KP_Add, ShiftMask);
3045 #define SWAPXY(g) do { \
3047 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
3048 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
3051 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
3054 XWindowAttributes wa;
3055 struct swm_geometry win_g, r_g = *g;
3056 struct ws_win *win, *fs_win = 0;
3057 int i, j, s, stacks;
3058 int w_inc = 1, h_inc, w_base = 1, h_base;
3059 int hrh, extra = 0, h_slice, last_h = 0;
3060 int split, colno, winno, mwin, msize, mscale;
3061 int remain, missing, v_slice, reconfigure;
3064 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
3065 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
3067 winno = count_win(ws, 0);
3068 if (winno == 0 && count_win(ws, 1) == 0)
3071 TAILQ_FOREACH(win, &ws->winlist, entry)
3072 if (win->transient == 0 && win->floating == 0
3073 && win->iconic == 0)
3080 w_inc = win->sh.width_inc;
3081 w_base = win->sh.base_width;
3082 mwin = ws->l_state.horizontal_mwin;
3083 mscale = ws->l_state.horizontal_msize;
3084 stacks = ws->l_state.horizontal_stacks;
3087 w_inc = win->sh.height_inc;
3088 w_base = win->sh.base_height;
3089 mwin = ws->l_state.vertical_mwin;
3090 mscale = ws->l_state.vertical_msize;
3091 stacks = ws->l_state.vertical_stacks;
3095 if (stacks > winno - mwin)
3096 stacks = winno - mwin;
3100 h_slice = r_g.h / SWM_H_SLICE;
3101 if (mwin && winno > mwin) {
3102 v_slice = r_g.w / SWM_V_SLICE;
3106 win_g.w = v_slice * mscale;
3108 if (w_inc > 1 && w_inc < v_slice) {
3109 /* adjust for window's requested size increment */
3110 remain = (win_g.w - w_base) % w_inc;
3116 win_g.x += r_g.w - msize;
3119 colno = split = winno / stacks;
3120 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
3121 2 * border_width) / stacks);
3123 hrh = r_g.h / colno;
3124 extra = r_g.h - (colno * hrh);
3125 win_g.h = hrh - 2 * border_width;
3127 /* stack all the tiled windows */
3128 i = j = 0, s = stacks;
3129 TAILQ_FOREACH(win, &ws->winlist, entry) {
3130 if (win->transient != 0 || win->floating != 0)
3132 if (win->iconic != 0)
3135 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3140 if (split && i == split) {
3141 colno = (winno - mwin) / stacks;
3142 if (s <= (winno - mwin) % stacks)
3144 split = split + colno;
3145 hrh = (r_g.h / colno);
3146 extra = r_g.h - (colno * hrh);
3150 win_g.x += win_g.w + 2 * border_width;
3151 win_g.w = (r_g.w - msize -
3152 (stacks * 2 * border_width)) / stacks;
3154 win_g.w += (r_g.w - msize -
3155 (stacks * 2 * border_width)) % stacks;
3159 win_g.h = hrh - 2 * border_width;
3161 h_inc = win->sh.width_inc;
3162 h_base = win->sh.base_width;
3164 h_inc = win->sh.height_inc;
3165 h_base = win->sh.base_height;
3167 if (j == colno - 1) {
3168 win_g.h = hrh + extra;
3169 } else if (h_inc > 1 && h_inc < h_slice) {
3170 /* adjust for window's requested size increment */
3171 remain = (win_g.h - h_base) % h_inc;
3172 missing = h_inc - remain;
3174 if (missing <= extra || j == 0) {
3186 win_g.y += last_h + 2 * border_width;
3188 bzero(&wc, sizeof wc);
3189 if (disable_border && bar_enabled == 0 && winno == 1){
3190 wc.border_width = 0;
3191 win_g.w += 2 * border_width;
3192 win_g.h += 2 * border_width;
3194 wc.border_width = border_width;
3197 if (X(win) != win_g.y || Y(win) != win_g.x ||
3198 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
3200 X(win) = wc.x = win_g.y;
3201 Y(win) = wc.y = win_g.x;
3202 WIDTH(win) = wc.width = win_g.h;
3203 HEIGHT(win) = wc.height = win_g.w;
3206 if (X(win) != win_g.x || Y(win) != win_g.y ||
3207 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
3209 X(win) = wc.x = win_g.x;
3210 Y(win) = wc.y = win_g.y;
3211 WIDTH(win) = wc.width = win_g.w;
3212 HEIGHT(win) = wc.height = win_g.h;
3217 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3218 XConfigureWindow(display, win->id, mask, &wc);
3221 if (XGetWindowAttributes(display, win->id, &wa))
3222 if (wa.map_state == IsUnmapped)
3223 XMapRaised(display, win->id);
3231 /* now, stack all the floaters and transients */
3232 TAILQ_FOREACH(win, &ws->winlist, entry) {
3233 if (win->transient == 0 && win->floating == 0)
3235 if (win->iconic == 1)
3237 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
3242 stack_floater(win, ws->r);
3243 XMapRaised(display, win->id);
3247 stack_floater(fs_win, ws->r);
3248 XMapRaised(display, fs_win->id);
3253 vertical_config(struct workspace *ws, int id)
3255 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
3259 case SWM_ARG_ID_STACKRESET:
3260 case SWM_ARG_ID_STACKINIT:
3261 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
3262 ws->l_state.vertical_mwin = 1;
3263 ws->l_state.vertical_stacks = 1;
3265 case SWM_ARG_ID_MASTERSHRINK:
3266 if (ws->l_state.vertical_msize > 1)
3267 ws->l_state.vertical_msize--;
3269 case SWM_ARG_ID_MASTERGROW:
3270 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
3271 ws->l_state.vertical_msize++;
3273 case SWM_ARG_ID_MASTERADD:
3274 ws->l_state.vertical_mwin++;
3276 case SWM_ARG_ID_MASTERDEL:
3277 if (ws->l_state.vertical_mwin > 0)
3278 ws->l_state.vertical_mwin--;
3280 case SWM_ARG_ID_STACKINC:
3281 ws->l_state.vertical_stacks++;
3283 case SWM_ARG_ID_STACKDEC:
3284 if (ws->l_state.vertical_stacks > 1)
3285 ws->l_state.vertical_stacks--;
3287 case SWM_ARG_ID_FLIPLAYOUT:
3288 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
3296 vertical_stack(struct workspace *ws, struct swm_geometry *g)
3298 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
3300 stack_master(ws, g, 0, ws->l_state.vertical_flip);
3304 horizontal_config(struct workspace *ws, int id)
3306 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3309 case SWM_ARG_ID_STACKRESET:
3310 case SWM_ARG_ID_STACKINIT:
3311 ws->l_state.horizontal_mwin = 1;
3312 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3313 ws->l_state.horizontal_stacks = 1;
3315 case SWM_ARG_ID_MASTERSHRINK:
3316 if (ws->l_state.horizontal_msize > 1)
3317 ws->l_state.horizontal_msize--;
3319 case SWM_ARG_ID_MASTERGROW:
3320 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3321 ws->l_state.horizontal_msize++;
3323 case SWM_ARG_ID_MASTERADD:
3324 ws->l_state.horizontal_mwin++;
3326 case SWM_ARG_ID_MASTERDEL:
3327 if (ws->l_state.horizontal_mwin > 0)
3328 ws->l_state.horizontal_mwin--;
3330 case SWM_ARG_ID_STACKINC:
3331 ws->l_state.horizontal_stacks++;
3333 case SWM_ARG_ID_STACKDEC:
3334 if (ws->l_state.horizontal_stacks > 1)
3335 ws->l_state.horizontal_stacks--;
3337 case SWM_ARG_ID_FLIPLAYOUT:
3338 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
3346 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3348 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3350 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
3353 /* fullscreen view */
3355 max_stack(struct workspace *ws, struct swm_geometry *g)
3358 struct swm_geometry gg = *g;
3359 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3363 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3368 winno = count_win(ws, 0);
3369 if (winno == 0 && count_win(ws, 1) == 0)
3372 TAILQ_FOREACH(win, &ws->winlist, entry) {
3373 if (win->transient) {
3375 parent = find_window(win->transient);
3379 if (win->floating && win->floatmaxed == 0 ) {
3381 * retain geometry for retrieval on exit from
3384 store_float_geom(win, ws->r);
3385 win->floatmaxed = 1;
3388 /* only reconfigure if necessary */
3389 if (X(win) != gg.x || Y(win) != gg.y || WIDTH(win) != gg.w ||
3390 HEIGHT(win) != gg.h) {
3391 bzero(&wc, sizeof wc);
3392 X(win) = wc.x = gg.x;
3393 Y(win) = wc.y = gg.y;
3395 wc.border_width = border_width;
3396 WIDTH(win) = wc.width = gg.w;
3397 HEIGHT(win) = wc.height = gg.h;
3399 wc.border_width = 0;
3400 WIDTH(win) = wc.width = gg.w + 2 * border_width;
3401 HEIGHT(win) = wc.height = gg.h +
3404 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3405 XConfigureWindow(display, win->id, mask, &wc);
3407 /* unmap only if we don't have multi screen */
3408 if (win != ws->focus)
3409 if (!(ScreenCount(display) > 1 || outputs > 1))
3413 /* put the last transient on top */
3416 XMapRaised(display, parent->id);
3417 stack_floater(wintrans, ws->r);
3418 focus_magic(wintrans);
3423 send_to_ws(struct swm_region *r, union arg *args)
3425 int wsid = args->id;
3426 struct ws_win *win = NULL, *parent;
3427 struct workspace *ws, *nws;
3428 Atom ws_idx_atom = 0;
3429 unsigned char ws_idx_str[SWM_PROPLEN];
3432 if (wsid >= workspace_limit)
3435 if (r && r->ws && r->ws->focus)
3441 if (win->ws->idx == wsid)
3444 DNPRINTF(SWM_D_MOVE, "send_to_ws: window: 0x%lx\n", win->id);
3447 nws = &win->s->ws[wsid];
3449 a.id = SWM_ARG_ID_FOCUSPREV;
3451 if (win->transient) {
3452 parent = find_window(win->transient);
3454 unmap_window(parent);
3455 TAILQ_REMOVE(&ws->winlist, parent, entry);
3456 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3461 TAILQ_REMOVE(&ws->winlist, win, entry);
3462 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3463 if (TAILQ_EMPTY(&ws->winlist))
3464 r->ws->focus = NULL;
3467 /* Try to update the window's workspace property */
3468 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3470 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3472 DNPRINTF(SWM_D_PROP, "send_to_ws: set property: _SWM_WS: %s\n",
3474 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3475 PropModeReplace, ws_idx_str, strlen((char *)ws_idx_str));
3483 pressbutton(struct swm_region *r, union arg *args)
3485 XTestFakeButtonEvent(display, args->id, True, CurrentTime);
3486 XTestFakeButtonEvent(display, args->id, False, CurrentTime);
3490 raise_toggle(struct swm_region *r, union arg *args)
3492 if (r == NULL || r->ws == NULL)
3495 r->ws->always_raise = !r->ws->always_raise;
3497 /* bring floaters back to top */
3498 if (r->ws->always_raise == 0)
3503 iconify(struct swm_region *r, union arg *args)
3507 if (r->ws->focus == NULL)
3509 unmap_window(r->ws->focus);
3510 update_iconic(r->ws->focus, 1);
3512 if (focus_mode == SWM_FOCUS_DEFAULT)
3513 drain_enter_notify();
3514 r->ws->focus = NULL;
3515 a.id = SWM_ARG_ID_FOCUSCUR;
3520 get_win_name(Window win)
3522 unsigned char *prop = NULL;
3523 unsigned long nbytes, nitems;
3525 /* try _NET_WM_NAME first */
3526 if (get_property(win, a_netwmname, 0L, a_utf8_string, NULL, &nbytes,
3529 if (get_property(win, a_netwmname, nbytes, a_utf8_string,
3530 &nitems, NULL, &prop))
3534 /* fallback to WM_NAME */
3535 if (!get_property(win, a_wmname, 0L, a_string, NULL, &nbytes, &prop))
3538 if (get_property(win, a_wmname, nbytes, a_string, &nitems, NULL, &prop))
3545 uniconify(struct swm_region *r, union arg *args)
3549 unsigned char *name;
3552 DNPRINTF(SWM_D_MISC, "uniconify\n");
3554 if (r == NULL || r->ws == NULL)
3557 /* make sure we have anything to uniconify */
3558 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3559 if (win->ws == NULL)
3560 continue; /* should never happen */
3561 if (win->iconic == 0)
3569 search_resp_action = SWM_SEARCH_UNICONIFY;
3571 spawn_select(r, args, "search", &searchpid);
3573 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3576 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3577 if (win->ws == NULL)
3578 continue; /* should never happen */
3579 if (win->iconic == 0)
3582 name = get_win_name(win->id);
3585 fprintf(lfile, "%s.%lu\n", name, win->id);
3593 name_workspace(struct swm_region *r, union arg *args)
3597 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3603 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3605 spawn_select(r, args, "name_workspace", &searchpid);
3607 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3610 fprintf(lfile, "%s", "");
3615 search_workspace(struct swm_region *r, union arg *args)
3618 struct workspace *ws;
3621 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3627 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3629 spawn_select(r, args, "search", &searchpid);
3631 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3634 for (i = 0; i < workspace_limit; i++) {
3638 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3639 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3646 search_win_cleanup(void)
3648 struct search_window *sw = NULL;
3650 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3651 XDestroyWindow(display, sw->indicator);
3652 XFreeGC(display, sw->gc);
3653 TAILQ_REMOVE(&search_wl, sw, entry);
3659 search_win(struct swm_region *r, union arg *args)
3661 struct ws_win *win = NULL;
3662 struct search_window *sw = NULL;
3669 XRectangle ibox, lbox;
3671 DNPRINTF(SWM_D_MISC, "search_win\n");
3674 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3676 spawn_select(r, args, "search", &searchpid);
3678 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3681 TAILQ_INIT(&search_wl);
3684 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3685 if (win->iconic == 1)
3688 sw = calloc(1, sizeof(struct search_window));
3690 warn("search_win: calloc");
3692 search_win_cleanup();
3698 snprintf(s, sizeof s, "%d", i);
3701 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
3703 w = XCreateSimpleWindow(display,
3704 win->id, 0, 0,lbox.width + 4,
3705 bar_fs_extents->max_logical_extent.height, 1,
3706 r->s->c[SWM_S_COLOR_UNFOCUS].color,
3707 r->s->c[SWM_S_COLOR_FOCUS].color);
3710 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3712 sw->gc = XCreateGC(display, w, 0, &gcv);
3713 XMapRaised(display, w);
3714 XSetForeground(display, sw->gc, r->s->c[SWM_S_COLOR_BAR].color);
3716 DRAWSTRING(display, w, bar_fs, sw->gc, 2,
3717 (bar_fs_extents->max_logical_extent.height -
3718 lbox.height) / 2 - lbox.y, s, len);
3720 fprintf(lfile, "%d\n", i);
3728 search_resp_uniconify(char *resp, unsigned long len)
3730 unsigned char *name;
3734 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
3736 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3737 if (win->iconic == 0)
3739 name = get_win_name(win->id);
3742 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3747 if (strncmp(s, resp, len) == 0) {
3748 /* XXX this should be a callback to generalize */
3749 update_iconic(win, 0);
3758 search_resp_name_workspace(char *resp, unsigned long len)
3760 struct workspace *ws;
3762 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
3764 if (search_r->ws == NULL)
3769 free(search_r->ws->name);
3770 search_r->ws->name = NULL;
3774 ws->name = strdup(resp);
3775 if (ws->name == NULL) {
3776 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
3777 "strdup: %s", strerror(errno));
3784 search_resp_search_workspace(char *resp, unsigned long len)
3791 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
3795 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
3802 ws_idx = strtonum(q, 1, workspace_limit, &errstr);
3804 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
3811 switchws(search_r, &a);
3815 search_resp_search_window(char *resp, unsigned long len)
3820 struct search_window *sw;
3822 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
3826 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
3831 idx = strtonum(s, 1, INT_MAX, &errstr);
3833 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
3840 TAILQ_FOREACH(sw, &search_wl, entry)
3841 if (idx == sw->idx) {
3847 #define MAX_RESP_LEN 1024
3850 search_do_resp(void)
3856 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3861 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3862 warn("search: calloc");
3866 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3868 warn("search: read error");
3871 resp[rbytes] = '\0';
3874 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3875 * newline, so work around that by sanitizing the resp now.
3877 resp[strcspn(resp, "\n")] = '\0';
3880 switch (search_resp_action) {
3881 case SWM_SEARCH_UNICONIFY:
3882 search_resp_uniconify(resp, len);
3884 case SWM_SEARCH_NAME_WORKSPACE:
3885 search_resp_name_workspace(resp, len);
3887 case SWM_SEARCH_SEARCH_WORKSPACE:
3888 search_resp_search_workspace(resp, len);
3890 case SWM_SEARCH_SEARCH_WINDOW:
3891 search_resp_search_window(resp, len);
3896 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
3897 search_win_cleanup();
3899 search_resp_action = SWM_SEARCH_NONE;
3900 close(select_resp_pipe[0]);
3905 wkill(struct swm_region *r, union arg *args)
3907 DNPRINTF(SWM_D_MISC, "wkill: id: %d\n", args->id);
3909 if (r->ws->focus == NULL)
3912 if (args->id == SWM_ARG_ID_KILLWINDOW)
3913 XKillClient(display, r->ws->focus->id);
3915 if (r->ws->focus->can_delete)
3916 client_msg(r->ws->focus, adelete);
3921 floating_toggle_win(struct ws_win *win)
3923 struct swm_region *r;
3933 /* reject floating toggles in max stack mode */
3934 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3937 if (win->floating) {
3938 if (!win->floatmaxed) {
3939 /* retain position for refloat */
3940 store_float_geom(win, r);
3944 if (win->g_floatvalid) {
3945 /* refloat at last floating relative position */
3946 X(win) = win->g_float.x - win->rg_float.x + X(r);
3947 Y(win) = win->g_float.y - win->rg_float.y + Y(r);
3948 WIDTH(win) = win->g_float.w;
3949 HEIGHT(win) = win->g_float.h;
3954 ewmh_update_actions(win);
3960 floating_toggle(struct swm_region *r, union arg *args)
3962 struct ws_win *win = r->ws->focus;
3968 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3969 _NET_WM_STATE_TOGGLE);
3972 if (focus_mode == SWM_FOCUS_DEFAULT)
3973 drain_enter_notify();
3975 if (win == win->ws->focus) {
3976 a.id = SWM_ARG_ID_FOCUSCUR;
3977 focus(win->ws->r, &a);
3982 constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
3984 if (X(win) + WIDTH(win) > X(r) + WIDTH(r) - border_width) {
3986 WIDTH(win) = X(r) + WIDTH(r) - X(win) - border_width;
3988 X(win) = X(r) + WIDTH(r) - WIDTH(win) - border_width;
3991 if (X(win) < X(r) - border_width) {
3993 WIDTH(win) -= X(r) - X(win) - border_width;
3995 X(win) = X(r) - border_width;
3998 if (Y(win) + HEIGHT(win) > Y(r) + HEIGHT(r) - border_width) {
4000 HEIGHT(win) = Y(r) + HEIGHT(r) - Y(win) - border_width;
4002 Y(win) = Y(r) + HEIGHT(r) - HEIGHT(win) - border_width;
4005 if (Y(win) < Y(r) - border_width) {
4007 HEIGHT(win) -= Y(r) - Y(win) - border_width;
4009 Y(win) = Y(r) - border_width;
4014 if (HEIGHT(win) < 1)
4019 update_window(struct ws_win *win)
4024 bzero(&wc, sizeof wc);
4025 mask = CWBorderWidth | CWWidth | CWHeight | CWX | CWY;
4026 wc.border_width = border_width;
4029 wc.width = WIDTH(win);
4030 wc.height = HEIGHT(win);
4032 DNPRINTF(SWM_D_MISC, "update_window: window: 0x%lx, (x,y) w x h: "
4033 "(%d,%d) %d x %d\n", win->id, wc.x, wc.y, wc.width, wc.height);
4035 XConfigureWindow(display, win->id, mask, &wc);
4038 #define SWM_RESIZE_STEPS (50)
4041 resize(struct ws_win *win, union arg *args)
4045 struct swm_region *r = NULL;
4046 int resize_step = 0;
4050 struct swm_geometry g;
4051 int top = 0, left = 0;
4054 unsigned int shape; /* cursor style */
4060 DNPRINTF(SWM_D_MOUSE, "resize: window: 0x%lx, floating: %s, "
4061 "transient: 0x%lx\n", win->id, YESNO(win->floating),
4064 if (!(win->transient != 0 || win->floating != 0))
4067 /* reject resizes in max mode for floaters (transient ok) */
4068 if (win->floatmaxed)
4072 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4078 case SWM_ARG_ID_WIDTHSHRINK:
4079 WIDTH(win) -= SWM_RESIZE_STEPS;
4082 case SWM_ARG_ID_WIDTHGROW:
4083 WIDTH(win) += SWM_RESIZE_STEPS;
4086 case SWM_ARG_ID_HEIGHTSHRINK:
4087 HEIGHT(win) -= SWM_RESIZE_STEPS;
4090 case SWM_ARG_ID_HEIGHTGROW:
4091 HEIGHT(win) += SWM_RESIZE_STEPS;
4098 constrain_window(win, r, 1);
4100 store_float_geom(win,r);
4104 if (focus_mode == SWM_FOCUS_DEFAULT)
4105 drain_enter_notify();
4107 /* get cursor offset from window root */
4108 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
4113 if (wx < WIDTH(win) / 2)
4116 if (wy < HEIGHT(win) / 2)
4119 if (args->id == SWM_ARG_ID_CENTER)
4122 shape = (left) ? XC_top_left_corner : XC_top_right_corner;
4124 shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
4126 cursor = XCreateFontCursor(display, shape);
4128 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
4129 GrabModeAsync, None, cursor, CurrentTime) != GrabSuccess) {
4130 XFreeCursor(display, cursor);
4135 XMaskEvent(display, MOUSEMASK | ExposureMask |
4136 SubstructureRedirectMask, &ev);
4138 case ConfigureRequest:
4141 handler[ev.type](&ev);
4144 /* cursor offset/delta from start of the operation */
4145 dx = ev.xmotion.x_root - x;
4146 dy = ev.xmotion.y_root - y;
4152 if (args->id == SWM_ARG_ID_CENTER) {
4153 if (g.h / 2 + dy < 1)
4157 HEIGHT(win) = g.h + 2 * dy;
4165 HEIGHT(win) = g.h + dy;
4172 if (args->id == SWM_ARG_ID_CENTER) {
4173 if (g.w / 2 + dx < 1)
4177 WIDTH(win) = g.w + 2 * dx;
4185 WIDTH(win) = g.w + dx;
4188 constrain_window(win, r, 1);
4190 /* not free, don't sync more than 120 times / second */
4191 if ((ev.xmotion.time - time) > (1000 / 120) ) {
4192 time = ev.xmotion.time;
4193 XSync(display, False);
4198 } while (ev.type != ButtonRelease);
4200 XSync(display, False);
4203 store_float_geom(win,r);
4205 XUngrabPointer(display, CurrentTime);
4206 XFreeCursor(display, cursor);
4209 drain_enter_notify();
4213 resize_step(struct swm_region *r, union arg *args)
4215 struct ws_win *win = NULL;
4217 if (r && r->ws && r->ws->focus)
4225 #define SWM_MOVE_STEPS (50)
4228 move(struct ws_win *win, union arg *args)
4233 struct swm_region *r = NULL;
4243 DNPRINTF(SWM_D_MOUSE, "move: window: 0x%lx, floating: %s, transient: "
4244 "0x%lx\n", win->id, YESNO(win->floating), win->transient);
4246 /* in max_stack mode should only move transients */
4247 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
4251 if (win->floating == 0 && !win->transient) {
4252 store_float_geom(win,r);
4253 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
4256 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
4263 case SWM_ARG_ID_MOVELEFT:
4264 X(win) -= (SWM_MOVE_STEPS - border_width);
4267 case SWM_ARG_ID_MOVERIGHT:
4268 X(win) += (SWM_MOVE_STEPS - border_width);
4271 case SWM_ARG_ID_MOVEUP:
4272 Y(win) -= (SWM_MOVE_STEPS - border_width);
4275 case SWM_ARG_ID_MOVEDOWN:
4276 Y(win) += (SWM_MOVE_STEPS - border_width);
4283 constrain_window(win, r, 0);
4285 store_float_geom(win, r);
4289 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
4290 GrabModeAsync, None, XCreateFontCursor(display, XC_fleur),
4291 CurrentTime) != GrabSuccess)
4294 /* get cursor offset from window root */
4295 if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
4299 XMaskEvent(display, MOUSEMASK | ExposureMask |
4300 SubstructureRedirectMask, &ev);
4302 case ConfigureRequest:
4305 handler[ev.type](&ev);
4308 X(win) = ev.xmotion.x_root - wx - border_width;
4309 Y(win) = ev.xmotion.y_root - wy - border_width;
4311 constrain_window(win, r, 0);
4313 /* not free, don't sync more than 120 times / second */
4314 if ((ev.xmotion.time - time) > (1000 / 120) ) {
4315 time = ev.xmotion.time;
4316 XSync(display, False);
4321 } while (ev.type != ButtonRelease);
4323 XSync(display, False);
4326 store_float_geom(win,r);
4327 XUngrabPointer(display, CurrentTime);
4330 drain_enter_notify();
4334 move_step(struct swm_region *r, union arg *args)
4336 struct ws_win *win = NULL;
4338 if (r && r->ws && r->ws->focus)
4343 if (!(win->transient != 0 || win->floating != 0))
4350 /* user/key callable function IDs */
4445 kf_search_workspace,
4447 kf_dumpwins, /* MUST BE LAST */
4451 /* key definitions */
4453 dummykeyfunc(struct swm_region *r, union arg *args)
4458 legacyfunc(struct swm_region *r, union arg *args)
4463 char name[SWM_FUNCNAME_LEN];
4464 void (*func)(struct swm_region *r, union arg *);
4466 } keyfuncs[kf_invalid + 1] = {
4467 /* name function argument */
4468 { "cycle_layout", cycle_layout, {0} },
4469 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
4470 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4471 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4472 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4473 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4474 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4475 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4476 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4477 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4478 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4479 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4480 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4481 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4482 { "spawn_term", spawnterm, {.argv = spawn_term} },
4483 { "spawn_menu", legacyfunc, {0} },
4484 { "quit", quit, {0} },
4485 { "restart", restart, {0} },
4486 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4487 { "ws_1", switchws, {.id = 0} },
4488 { "ws_2", switchws, {.id = 1} },
4489 { "ws_3", switchws, {.id = 2} },
4490 { "ws_4", switchws, {.id = 3} },
4491 { "ws_5", switchws, {.id = 4} },
4492 { "ws_6", switchws, {.id = 5} },
4493 { "ws_7", switchws, {.id = 6} },
4494 { "ws_8", switchws, {.id = 7} },
4495 { "ws_9", switchws, {.id = 8} },
4496 { "ws_10", switchws, {.id = 9} },
4497 { "ws_11", switchws, {.id = 10} },
4498 { "ws_12", switchws, {.id = 11} },
4499 { "ws_13", switchws, {.id = 12} },
4500 { "ws_14", switchws, {.id = 13} },
4501 { "ws_15", switchws, {.id = 14} },
4502 { "ws_16", switchws, {.id = 15} },
4503 { "ws_17", switchws, {.id = 16} },
4504 { "ws_18", switchws, {.id = 17} },
4505 { "ws_19", switchws, {.id = 18} },
4506 { "ws_20", switchws, {.id = 19} },
4507 { "ws_21", switchws, {.id = 20} },
4508 { "ws_22", switchws, {.id = 21} },
4509 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4510 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4511 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4512 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4513 { "ws_prior", priorws, {0} },
4514 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4515 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4516 { "mvws_1", send_to_ws, {.id = 0} },
4517 { "mvws_2", send_to_ws, {.id = 1} },
4518 { "mvws_3", send_to_ws, {.id = 2} },
4519 { "mvws_4", send_to_ws, {.id = 3} },
4520 { "mvws_5", send_to_ws, {.id = 4} },
4521 { "mvws_6", send_to_ws, {.id = 5} },
4522 { "mvws_7", send_to_ws, {.id = 6} },
4523 { "mvws_8", send_to_ws, {.id = 7} },
4524 { "mvws_9", send_to_ws, {.id = 8} },
4525 { "mvws_10", send_to_ws, {.id = 9} },
4526 { "mvws_11", send_to_ws, {.id = 10} },
4527 { "mvws_12", send_to_ws, {.id = 11} },
4528 { "mvws_13", send_to_ws, {.id = 12} },
4529 { "mvws_14", send_to_ws, {.id = 13} },
4530 { "mvws_15", send_to_ws, {.id = 14} },
4531 { "mvws_16", send_to_ws, {.id = 15} },
4532 { "mvws_17", send_to_ws, {.id = 16} },
4533 { "mvws_18", send_to_ws, {.id = 17} },
4534 { "mvws_19", send_to_ws, {.id = 18} },
4535 { "mvws_20", send_to_ws, {.id = 19} },
4536 { "mvws_21", send_to_ws, {.id = 20} },
4537 { "mvws_22", send_to_ws, {.id = 21} },
4538 { "bar_toggle", bar_toggle, {0} },
4539 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4540 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4541 { "screenshot_all", legacyfunc, {0} },
4542 { "screenshot_wind", legacyfunc, {0} },
4543 { "float_toggle", floating_toggle,{0} },
4544 { "version", version, {0} },
4545 { "spawn_lock", legacyfunc, {0} },
4546 { "spawn_initscr", legacyfunc, {0} },
4547 { "spawn_custom", dummykeyfunc, {0} },
4548 { "iconify", iconify, {0} },
4549 { "uniconify", uniconify, {0} },
4550 { "raise_toggle", raise_toggle, {0} },
4551 { "button2", pressbutton, {2} },
4552 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4553 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4554 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4555 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4556 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4557 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4558 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4559 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4560 { "name_workspace", name_workspace, {0} },
4561 { "search_workspace", search_workspace, {0} },
4562 { "search_win", search_win, {0} },
4563 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4564 { "invalid key func", NULL, {0} },
4567 RB_ENTRY(key) entry;
4570 enum keyfuncid funcid;
4573 RB_HEAD(key_tree, key);
4576 key_cmp(struct key *kp1, struct key *kp2)
4578 if (kp1->keysym < kp2->keysym)
4580 if (kp1->keysym > kp2->keysym)
4583 if (kp1->mod < kp2->mod)
4585 if (kp1->mod > kp2->mod)
4591 RB_GENERATE_STATIC(key_tree, key, entry, key_cmp);
4592 struct key_tree keys;
4595 enum { client_click, root_click };
4597 unsigned int action;
4599 unsigned int button;
4600 void (*func)(struct ws_win *, union arg *);
4603 /* action key mouse button func args */
4604 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4605 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4606 { client_click, MODKEY, Button1, move, {0} },
4610 update_modkey(unsigned int mod)
4616 RB_FOREACH(kp, key_tree, &keys)
4617 if (kp->mod & ShiftMask)
4618 kp->mod = mod | ShiftMask;
4622 for (i = 0; i < LENGTH(buttons); i++)
4623 if (buttons[i].mask & ShiftMask)
4624 buttons[i].mask = mod | ShiftMask;
4626 buttons[i].mask = mod;
4631 TAILQ_ENTRY(spawn_prog) entry;
4636 TAILQ_HEAD(spawn_list, spawn_prog);
4637 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
4640 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4643 struct spawn_prog *prog = NULL;
4645 char *ap, **real_args;
4647 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
4650 TAILQ_FOREACH(prog, &spawns, entry) {
4651 if (!strcasecmp(spawn_name, prog->name))
4655 warnx("spawn_custom: program %s not found", spawn_name);
4659 /* make room for expanded args */
4660 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4661 err(1, "spawn_custom: calloc real_args");
4663 /* expand spawn_args into real_args */
4664 for (i = 0; i < prog->argc; i++) {
4666 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
4667 if (!strcasecmp(ap, "$bar_border")) {
4669 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4671 err(1, "spawn_custom border color");
4672 } else if (!strcasecmp(ap, "$bar_color")) {
4674 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4676 err(1, "spawn_custom bar color");
4677 } else if (!strcasecmp(ap, "$bar_font")) {
4678 if ((real_args[i] = strdup(bar_fonts))
4680 err(1, "spawn_custom bar fonts");
4681 } else if (!strcasecmp(ap, "$bar_font_color")) {
4683 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4685 err(1, "spawn_custom color font");
4686 } else if (!strcasecmp(ap, "$color_focus")) {
4688 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4690 err(1, "spawn_custom color focus");
4691 } else if (!strcasecmp(ap, "$color_unfocus")) {
4693 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4695 err(1, "spawn_custom color unfocus");
4697 /* no match --> copy as is */
4698 if ((real_args[i] = strdup(ap)) == NULL)
4699 err(1, "spawn_custom strdup(ap)");
4701 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
4706 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
4707 for (i = 0; i < prog->argc; i++)
4708 DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
4709 DNPRINTF(SWM_D_SPAWN, "\n");
4711 *ret_args = real_args;
4712 return (prog->argc);
4716 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4722 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4726 spawn(r->ws->idx, &a, 1);
4728 for (i = 0; i < spawn_argc; i++)
4734 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
4740 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4744 if (pipe(select_list_pipe) == -1)
4745 err(1, "pipe error");
4746 if (pipe(select_resp_pipe) == -1)
4747 err(1, "pipe error");
4749 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4750 err(1, "could not disable SIGPIPE");
4751 switch (*pid = fork()) {
4753 err(1, "cannot fork");
4756 if (dup2(select_list_pipe[0], 0) == -1)
4758 if (dup2(select_resp_pipe[1], 1) == -1)
4760 close(select_list_pipe[1]);
4761 close(select_resp_pipe[0]);
4762 spawn(r->ws->idx, &a, 0);
4764 default: /* parent */
4765 close(select_list_pipe[0]);
4766 close(select_resp_pipe[1]);
4770 for (i = 0; i < spawn_argc; i++)
4776 spawn_insert(char *name, char *args)
4778 char *arg, *cp, *ptr;
4779 struct spawn_prog *sp;
4781 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
4783 if ((sp = calloc(1, sizeof *sp)) == NULL)
4784 err(1, "spawn_insert: malloc");
4785 if ((sp->name = strdup(name)) == NULL)
4786 err(1, "spawn_insert: strdup");
4788 /* convert the arguments to an argument list */
4789 if ((ptr = cp = strdup(args)) == NULL)
4790 err(1, "spawn_insert: strdup");
4791 while ((arg = strsep(&ptr, " \t")) != NULL) {
4792 /* empty field; skip it */
4797 if ((sp->argv = realloc(sp->argv, sp->argc *
4798 sizeof *sp->argv)) == NULL)
4799 err(1, "spawn_insert: realloc");
4800 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
4801 err(1, "spawn_insert: strdup");
4805 TAILQ_INSERT_TAIL(&spawns, sp, entry);
4806 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
4810 spawn_remove(struct spawn_prog *sp)
4814 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
4816 TAILQ_REMOVE(&spawns, sp, entry);
4817 for (i = 0; i < sp->argc; i++)
4823 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
4827 spawn_replace(struct spawn_prog *sp, char *name, char *args)
4829 DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
4832 spawn_insert(name, args);
4834 DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
4838 setspawn(char *name, char *args)
4840 struct spawn_prog *sp;
4842 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
4847 TAILQ_FOREACH(sp, &spawns, entry) {
4848 if (!strcmp(sp->name, name)) {
4852 spawn_replace(sp, name, args);
4853 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
4857 if (*args == '\0') {
4858 warnx("error: setspawn: cannot find program: %s", name);
4862 spawn_insert(name, args);
4863 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
4867 setconfspawn(char *selector, char *value, int flags)
4869 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
4871 setspawn(selector, value);
4873 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
4880 setconfspawn("term", "xterm", 0);
4881 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4882 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4883 setconfspawn("lock", "xlock", 0);
4884 setconfspawn("initscr", "initscreen.sh", 0);
4885 setconfspawn("menu", "dmenu_run"
4888 " -nf $bar_font_color"
4890 " -sf $bar_color", 0);
4891 setconfspawn("search", "dmenu"
4895 " -nf $bar_font_color"
4897 " -sf $bar_color", 0);
4898 setconfspawn("name_workspace", "dmenu"
4902 " -nf $bar_font_color"
4904 " -sf $bar_color", 0);
4908 #define SWM_MODNAME_SIZE 32
4909 #define SWM_KEY_WS "\n+ \t"
4911 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
4915 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
4916 if (mod == NULL || ks == NULL) {
4917 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
4920 if (keystr == NULL || strlen(keystr) == 0) {
4921 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
4927 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
4928 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
4930 cp += (long)strspn(cp, SWM_KEY_WS);
4931 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
4933 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
4935 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
4937 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
4939 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
4941 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
4943 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
4944 *mod |= ControlMask;
4946 *ks = XStringToKeysym(name);
4947 XConvertCase(*ks, ks, &uks);
4948 if (ks == NoSymbol) {
4950 "parsekeys: invalid key %s\n",
4956 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
4961 strdupsafe(char *str)
4966 return (strdup(str));
4970 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
4974 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
4975 keyfuncs[kfid].name, spawn_name);
4977 if ((kp = malloc(sizeof *kp)) == NULL)
4978 err(1, "key_insert: malloc");
4983 kp->spawn_name = strdupsafe(spawn_name);
4984 RB_INSERT(key_tree, &keys, kp);
4986 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
4990 key_lookup(unsigned int mod, KeySym ks)
4997 return (RB_FIND(key_tree, &keys, &kp));
5001 key_remove(struct key *kp)
5003 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
5005 RB_REMOVE(key_tree, &keys, kp);
5006 free(kp->spawn_name);
5009 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
5013 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
5016 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
5020 key_insert(mod, ks, kfid, spawn_name);
5022 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
5026 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
5031 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
5032 keyfuncs[kfid].name, spawn_name);
5034 if ((kp = key_lookup(mod, ks)) != NULL) {
5035 if (kfid == kf_invalid)
5038 key_replace(kp, mod, ks, kfid, spawn_name);
5039 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5042 if (kfid == kf_invalid) {
5043 warnx("error: setkeybinding: cannot find mod/key combination");
5044 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5048 key_insert(mod, ks, kfid, spawn_name);
5049 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
5053 setconfbinding(char *selector, char *value, int flags)
5055 enum keyfuncid kfid;
5058 struct spawn_prog *sp;
5059 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
5060 if (selector == NULL) {
5061 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
5062 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5064 setkeybinding(mod, ks, kfid, NULL);
5069 /* search by key function name */
5070 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
5071 if (strncasecmp(selector, keyfuncs[kfid].name,
5072 SWM_FUNCNAME_LEN) == 0) {
5073 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5075 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5076 setkeybinding(mod, ks, kfid, NULL);
5082 /* search by custom spawn name */
5083 TAILQ_FOREACH(sp, &spawns, entry) {
5084 if (strcasecmp(selector, sp->name) == 0) {
5085 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
5087 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
5088 setkeybinding(mod, ks, kf_spawn_custom,
5095 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
5102 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
5103 setkeybinding(MODKEY|ShiftMask, XK_backslash, kf_flip_layout, NULL);
5104 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
5105 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
5106 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
5107 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
5108 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
5109 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
5110 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
5111 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
5112 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
5113 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
5114 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
5115 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
5116 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
5117 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
5118 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
5119 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
5120 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
5121 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
5122 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
5123 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
5124 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
5125 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
5126 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
5127 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
5128 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
5129 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
5130 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
5131 setkeybinding(MODKEY, XK_F1, kf_ws_11, NULL);
5132 setkeybinding(MODKEY, XK_F2, kf_ws_12, NULL);
5133 setkeybinding(MODKEY, XK_F3, kf_ws_13, NULL);
5134 setkeybinding(MODKEY, XK_F4, kf_ws_14, NULL);
5135 setkeybinding(MODKEY, XK_F5, kf_ws_15, NULL);
5136 setkeybinding(MODKEY, XK_F6, kf_ws_16, NULL);
5137 setkeybinding(MODKEY, XK_F7, kf_ws_17, NULL);
5138 setkeybinding(MODKEY, XK_F8, kf_ws_18, NULL);
5139 setkeybinding(MODKEY, XK_F9, kf_ws_19, NULL);
5140 setkeybinding(MODKEY, XK_F10, kf_ws_20, NULL);
5141 setkeybinding(MODKEY, XK_F11, kf_ws_21, NULL);
5142 setkeybinding(MODKEY, XK_F12, kf_ws_22, NULL);
5143 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
5144 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
5145 setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
5146 setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
5147 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
5148 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
5149 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
5150 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
5151 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
5152 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
5153 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
5154 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
5155 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
5156 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
5157 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
5158 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
5159 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
5160 setkeybinding(MODKEY|ShiftMask, XK_F1, kf_mvws_11, NULL);
5161 setkeybinding(MODKEY|ShiftMask, XK_F2, kf_mvws_12, NULL);
5162 setkeybinding(MODKEY|ShiftMask, XK_F3, kf_mvws_13, NULL);
5163 setkeybinding(MODKEY|ShiftMask, XK_F4, kf_mvws_14, NULL);
5164 setkeybinding(MODKEY|ShiftMask, XK_F5, kf_mvws_15, NULL);
5165 setkeybinding(MODKEY|ShiftMask, XK_F6, kf_mvws_16, NULL);
5166 setkeybinding(MODKEY|ShiftMask, XK_F7, kf_mvws_17, NULL);
5167 setkeybinding(MODKEY|ShiftMask, XK_F8, kf_mvws_18, NULL);
5168 setkeybinding(MODKEY|ShiftMask, XK_F9, kf_mvws_19, NULL);
5169 setkeybinding(MODKEY|ShiftMask, XK_F10, kf_mvws_20, NULL);
5170 setkeybinding(MODKEY|ShiftMask, XK_F11, kf_mvws_21, NULL);
5171 setkeybinding(MODKEY|ShiftMask, XK_F12, kf_mvws_22, NULL);
5172 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
5173 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
5174 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
5175 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
5176 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
5177 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
5178 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
5179 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
5180 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
5181 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
5182 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
5183 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
5184 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
5185 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
5186 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
5187 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
5188 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
5189 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
5190 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
5191 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
5192 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
5193 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
5194 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
5195 setkeybinding(MODKEY|ShiftMask, XK_slash, kf_name_workspace,NULL);
5196 setkeybinding(MODKEY, XK_slash, kf_search_workspace,NULL);
5197 setkeybinding(MODKEY, XK_f, kf_search_win, NULL);
5199 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
5208 while (RB_EMPTY(&keys) == 0) {
5209 kp = RB_ROOT(&keys);
5215 setkeymapping(char *selector, char *value, int flags)
5217 char keymapping_file[PATH_MAX];
5218 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
5219 if (value[0] == '~')
5220 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
5221 pwd->pw_dir, &value[1]);
5223 strlcpy(keymapping_file, value, sizeof keymapping_file);
5225 /* load new key bindings; if it fails, revert to default bindings */
5226 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
5230 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
5235 updatenumlockmask(void)
5238 XModifierKeymap *modmap;
5240 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
5242 modmap = XGetModifierMapping(display);
5243 for (i = 0; i < 8; i++)
5244 for (j = 0; j < modmap->max_keypermod; j++)
5245 if (modmap->modifiermap[i * modmap->max_keypermod + j]
5246 == XKeysymToKeycode(display, XK_Num_Lock))
5247 numlockmask = (1 << i);
5249 XFreeModifiermap(modmap);
5257 unsigned int modifiers[] =
5258 { 0, LockMask, numlockmask, numlockmask | LockMask };
5261 DNPRINTF(SWM_D_MISC, "grabkeys\n");
5262 updatenumlockmask();
5264 for (k = 0; k < ScreenCount(display); k++) {
5265 if (TAILQ_EMPTY(&screens[k].rl))
5267 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
5268 RB_FOREACH(kp, key_tree, &keys) {
5269 if ((code = XKeysymToKeycode(display, kp->keysym)))
5270 for (j = 0; j < LENGTH(modifiers); j++)
5271 XGrabKey(display, code,
5272 kp->mod | modifiers[j],
5273 screens[k].root, True,
5274 GrabModeAsync, GrabModeAsync);
5280 grabbuttons(struct ws_win *win, int focused)
5283 unsigned int modifiers[] =
5284 { 0, LockMask, numlockmask, numlockmask|LockMask };
5286 updatenumlockmask();
5287 XUngrabButton(display, AnyButton, AnyModifier, win->id);
5289 for (i = 0; i < LENGTH(buttons); i++)
5290 if (buttons[i].action == client_click)
5291 for (j = 0; j < LENGTH(modifiers); j++)
5292 XGrabButton(display, buttons[i].button,
5293 buttons[i].mask | modifiers[j],
5294 win->id, False, BUTTONMASK,
5295 GrabModeAsync, GrabModeSync, None,
5298 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
5299 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
5302 const char *quirkname[] = {
5303 "NONE", /* config string for "no value" */
5312 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
5313 #define SWM_Q_WS "\n|+ \t"
5315 parsequirks(char *qstr, unsigned long *quirk)
5325 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
5327 cp += (long)strspn(cp, SWM_Q_WS);
5328 for (i = 0; i < LENGTH(quirkname); i++) {
5329 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
5330 DNPRINTF(SWM_D_QUIRK,
5331 "parsequirks: %s\n", name);
5336 *quirk |= 1 << (i-1);
5340 if (i >= LENGTH(quirkname)) {
5341 DNPRINTF(SWM_D_QUIRK,
5342 "parsequirks: invalid quirk [%s]\n", name);
5350 quirk_insert(const char *class, const char *name, unsigned long quirk)
5354 DNPRINTF(SWM_D_QUIRK, "quirk_insert: %s:%s [%lu]\n", class, name,
5357 if ((qp = malloc(sizeof *qp)) == NULL)
5358 err(1, "quirk_insert: malloc");
5359 if ((qp->class = strdup(class)) == NULL)
5360 err(1, "quirk_insert: strdup");
5361 if ((qp->name = strdup(name)) == NULL)
5362 err(1, "quirk_insert: strdup");
5365 TAILQ_INSERT_TAIL(&quirks, qp, entry);
5367 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
5371 quirk_remove(struct quirk *qp)
5373 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
5374 qp->name, qp->quirk);
5376 TAILQ_REMOVE(&quirks, qp, entry);
5381 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
5385 quirk_replace(struct quirk *qp, const char *class, const char *name,
5386 unsigned long quirk)
5388 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s [%lu]\n", qp->class,
5389 qp->name, qp->quirk);
5392 quirk_insert(class, name, quirk);
5394 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
5398 setquirk(const char *class, const char *name, unsigned long quirk)
5402 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s [%lu]\n", class, name,
5405 TAILQ_FOREACH(qp, &quirks, entry) {
5406 if (!strcmp(qp->class, class) && !strcmp(qp->name, name)) {
5410 quirk_replace(qp, class, name, quirk);
5411 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5416 warnx("error: setquirk: cannot find class/name combination");
5420 quirk_insert(class, name, quirk);
5421 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
5425 setconfquirk(char *selector, char *value, int flags)
5427 char *cp, *class, *name;
5429 unsigned long quirks;
5430 if (selector == NULL)
5432 if ((cp = strchr(selector, ':')) == NULL)
5437 if ((retval = parsequirks(value, &quirks)) == 0)
5438 setquirk(class, name, quirks);
5445 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
5446 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
5447 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
5448 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
5449 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5450 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
5451 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5452 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5453 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5454 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5455 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5456 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5457 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5460 /* conf file stuff */
5461 #define SWM_CONF_FILE "spectrwm.conf"
5462 #define SWM_CONF_FILE_OLD "scrotwm.conf"
5464 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH,
5465 SWM_S_STACK_ENABLED, SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT,
5466 SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_WORKSPACE_LIMIT,
5467 SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
5468 SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED,
5469 SWM_S_URGENT_ENABLED, SWM_S_FOCUS_MODE, SWM_S_FOCUS_CLOSE,
5470 SWM_S_FOCUS_CLOSE_WRAP, SWM_S_FOCUS_DEFAULT, SWM_S_SPAWN_ORDER,
5471 SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH, SWM_S_BAR_FONT,
5472 SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM, SWM_S_SS_APP, SWM_S_DIALOG_RATIO,
5473 SWM_S_BAR_AT_BOTTOM, SWM_S_VERBOSE_LAYOUT, SWM_S_BAR_JUSTIFY,
5478 setconfvalue(char *selector, char *value, int flags)
5484 case SWM_S_BAR_DELAY:
5485 bar_delay = atoi(value);
5487 case SWM_S_BAR_ENABLED:
5488 bar_enabled = atoi(value);
5490 case SWM_S_BAR_BORDER_WIDTH:
5491 bar_border_width = atoi(value);
5493 case SWM_S_BAR_AT_BOTTOM:
5494 bar_at_bottom = atoi(value);
5496 case SWM_S_BAR_JUSTIFY:
5497 if (!strcmp(value, "left"))
5498 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5499 else if (!strcmp(value, "center"))
5500 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5501 else if (!strcmp(value, "right"))
5502 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5504 errx(1, "invalid bar_justify");
5506 case SWM_S_BAR_FORMAT:
5508 if ((bar_format = strdup(value)) == NULL)
5509 err(1, "setconfvalue: bar_format");
5511 case SWM_S_STACK_ENABLED:
5512 stack_enabled = atoi(value);
5514 case SWM_S_CLOCK_ENABLED:
5515 clock_enabled = atoi(value);
5517 case SWM_S_CLOCK_FORMAT:
5518 #ifndef SWM_DENY_CLOCK_FORMAT
5520 if ((clock_format = strdup(value)) == NULL)
5521 err(1, "setconfvalue: clock_format");
5524 case SWM_S_CYCLE_EMPTY:
5525 cycle_empty = atoi(value);
5527 case SWM_S_CYCLE_VISIBLE:
5528 cycle_visible = atoi(value);
5530 case SWM_S_WORKSPACE_LIMIT:
5531 workspace_limit = atoi(value);
5532 if (workspace_limit > SWM_WS_MAX)
5533 workspace_limit = SWM_WS_MAX;
5534 else if (workspace_limit < 1)
5535 workspace_limit = 1;
5537 case SWM_S_SS_ENABLED:
5538 ss_enabled = atoi(value);
5540 case SWM_S_TERM_WIDTH:
5541 term_width = atoi(value);
5543 case SWM_S_TITLE_CLASS_ENABLED:
5544 title_class_enabled = atoi(value);
5546 case SWM_S_WINDOW_NAME_ENABLED:
5547 window_name_enabled = atoi(value);
5549 case SWM_S_TITLE_NAME_ENABLED:
5550 title_name_enabled = atoi(value);
5552 case SWM_S_URGENT_ENABLED:
5553 urgent_enabled = atoi(value);
5555 case SWM_S_FOCUS_MODE:
5556 if (!strcmp(value, "default"))
5557 focus_mode = SWM_FOCUS_DEFAULT;
5558 else if (!strcmp(value, "follow_cursor"))
5559 focus_mode = SWM_FOCUS_FOLLOW;
5560 else if (!strcmp(value, "synergy"))
5561 focus_mode = SWM_FOCUS_SYNERGY;
5563 errx(1, "focus_mode");
5565 case SWM_S_FOCUS_CLOSE:
5566 if (!strcmp(value, "first"))
5567 focus_close = SWM_STACK_BOTTOM;
5568 else if (!strcmp(value, "last"))
5569 focus_close = SWM_STACK_TOP;
5570 else if (!strcmp(value, "next"))
5571 focus_close = SWM_STACK_ABOVE;
5572 else if (!strcmp(value, "previous"))
5573 focus_close = SWM_STACK_BELOW;
5575 errx(1, "focus_close");
5577 case SWM_S_FOCUS_CLOSE_WRAP:
5578 focus_close_wrap = atoi(value);
5580 case SWM_S_FOCUS_DEFAULT:
5581 if (!strcmp(value, "last"))
5582 focus_default = SWM_STACK_TOP;
5583 else if (!strcmp(value, "first"))
5584 focus_default = SWM_STACK_BOTTOM;
5586 errx(1, "focus_default");
5588 case SWM_S_SPAWN_ORDER:
5589 if (!strcmp(value, "first"))
5590 spawn_position = SWM_STACK_BOTTOM;
5591 else if (!strcmp(value, "last"))
5592 spawn_position = SWM_STACK_TOP;
5593 else if (!strcmp(value, "next"))
5594 spawn_position = SWM_STACK_ABOVE;
5595 else if (!strcmp(value, "previous"))
5596 spawn_position = SWM_STACK_BELOW;
5598 errx(1, "spawn_position");
5600 case SWM_S_DISABLE_BORDER:
5601 disable_border = atoi(value);
5603 case SWM_S_BORDER_WIDTH:
5604 border_width = atoi(value);
5606 case SWM_S_BAR_FONT:
5608 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
5609 err(1, "setconfvalue: asprintf: failed to allocate "
5610 "memory for bar_fonts.");
5614 case SWM_S_BAR_ACTION:
5616 if ((bar_argv[0] = strdup(value)) == NULL)
5617 err(1, "setconfvalue: bar_action");
5619 case SWM_S_SPAWN_TERM:
5620 free(spawn_term[0]);
5621 if ((spawn_term[0] = strdup(value)) == NULL)
5622 err(1, "setconfvalue: spawn_term");
5626 case SWM_S_DIALOG_RATIO:
5627 dialog_ratio = atof(value);
5628 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5631 case SWM_S_VERBOSE_LAYOUT:
5632 verbose_layout = atoi(value);
5633 for (i = 0; layouts[i].l_stack != NULL; i++) {
5635 layouts[i].l_string = fancy_stacker;
5637 layouts[i].l_string = plain_stacker;
5647 setconfmodkey(char *selector, char *value, int flags)
5649 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5650 update_modkey(Mod1Mask);
5651 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5652 update_modkey(Mod2Mask);
5653 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5654 update_modkey(Mod3Mask);
5655 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5656 update_modkey(Mod4Mask);
5663 setconfcolor(char *selector, char *value, int flags)
5665 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5670 setconfregion(char *selector, char *value, int flags)
5672 custom_region(value);
5677 setautorun(char *selector, char *value, int flags)
5687 if (getenv("SWM_STARTED"))
5691 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
5692 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5694 if (ws_id < 0 || ws_id >= workspace_limit)
5695 errx(1, "autorun: invalid workspace %d", ws_id + 1);
5698 * This is a little intricate
5700 * If the pid already exists we simply reuse it because it means it was
5701 * used before AND not claimed by manage_window. We get away with
5702 * altering it in the parent after INSERT because this can not be a race
5705 while ((ap = strsep(&sp, " \t")) != NULL) {
5708 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
5710 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
5711 err(1, "setautorun: realloc");
5712 a.argv[argc - 1] = ap;
5715 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
5716 err(1, "setautorun: realloc");
5717 a.argv[argc] = NULL;
5719 if ((pid = fork()) == 0) {
5720 spawn(ws_id, &a, 1);
5729 p = calloc(1, sizeof *p);
5732 TAILQ_INSERT_TAIL(&pidlist, p, entry);
5742 setlayout(char *selector, char *value, int flags)
5744 int ws_id, i, x, mg, ma, si, raise;
5745 int st = SWM_V_STACK;
5747 struct workspace *ws;
5749 if (getenv("SWM_STARTED"))
5753 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
5754 &ws_id, &mg, &ma, &si, &raise, s) != 6)
5755 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5756 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5759 if (ws_id < 0 || ws_id >= workspace_limit)
5760 errx(1, "layout: invalid workspace %d", ws_id + 1);
5762 if (!strcasecmp(s, "vertical"))
5764 else if (!strcasecmp(s, "horizontal"))
5766 else if (!strcasecmp(s, "fullscreen"))
5769 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5770 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5773 for (i = 0; i < ScreenCount(display); i++) {
5774 ws = (struct workspace *)&screens[i].ws;
5775 ws[ws_id].cur_layout = &layouts[st];
5777 ws[ws_id].always_raise = raise;
5778 if (st == SWM_MAX_STACK)
5782 for (x = 0; x < abs(mg); x++) {
5783 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5784 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
5785 SWM_ARG_ID_MASTERSHRINK);
5789 for (x = 0; x < abs(ma); x++) {
5790 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5791 ma >= 0 ? SWM_ARG_ID_MASTERADD :
5792 SWM_ARG_ID_MASTERDEL);
5796 for (x = 0; x < abs(si); x++) {
5797 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5798 si >= 0 ? SWM_ARG_ID_STACKINC :
5799 SWM_ARG_ID_STACKDEC);
5807 /* config options */
5808 struct config_option {
5810 int (*func)(char*, char*, int);
5813 struct config_option configopt[] = {
5814 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
5815 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
5816 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
5817 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
5818 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
5819 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
5820 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
5821 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
5822 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
5823 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
5824 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
5825 { "keyboard_mapping", setkeymapping, 0 },
5826 { "bind", setconfbinding, 0 },
5827 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
5828 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
5829 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
5830 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
5831 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
5832 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
5833 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
5834 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
5835 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
5836 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
5837 { "modkey", setconfmodkey, 0 },
5838 { "program", setconfspawn, 0 },
5839 { "quirk", setconfquirk, 0 },
5840 { "region", setconfregion, 0 },
5841 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
5842 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
5843 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
5844 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
5845 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
5846 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
5847 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
5848 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
5849 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
5850 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
5851 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
5852 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
5853 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
5854 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
5855 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
5856 { "autorun", setautorun, 0 },
5857 { "layout", setlayout, 0 },
5862 conf_load(char *filename, int keymapping)
5865 char *line, *cp, *optsub, *optval;
5866 size_t linelen, lineno = 0;
5867 int wordlen, i, optind;
5868 struct config_option *opt;
5870 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
5872 if (filename == NULL) {
5873 warnx("conf_load: no filename");
5876 if ((config = fopen(filename, "r")) == NULL) {
5877 warn("conf_load: fopen: %s", filename);
5881 while (!feof(config)) {
5882 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
5885 err(1, "%s", filename);
5890 cp += strspn(cp, " \t\n"); /* eat whitespace */
5891 if (cp[0] == '\0') {
5896 /* get config option */
5897 wordlen = strcspn(cp, "=[ \t\n");
5899 warnx("%s: line %zd: no option found",
5904 for (i = 0; i < LENGTH(configopt); i++) {
5905 opt = &configopt[i];
5906 if (!strncasecmp(cp, opt->optname, wordlen) &&
5907 strlen(opt->optname) == wordlen) {
5913 warnx("%s: line %zd: unknown option %.*s",
5914 filename, lineno, wordlen, cp);
5917 if (keymapping && strcmp(opt->optname, "bind")) {
5918 warnx("%s: line %zd: invalid option %.*s",
5919 filename, lineno, wordlen, cp);
5923 cp += strspn(cp, " \t\n"); /* eat whitespace */
5924 /* get [selector] if any */
5928 wordlen = strcspn(cp, "]");
5931 warnx("%s: line %zd: syntax error",
5936 if (asprintf(&optsub, "%.*s", wordlen, cp) ==
5938 warnx("%s: line %zd: unable to allocate"
5939 "memory for selector", filename,
5945 cp += strspn(cp, "] \t\n"); /* eat trailing */
5947 cp += strspn(cp, "= \t\n"); /* eat trailing */
5949 optval = strdup(cp);
5950 /* call function to deal with it all */
5951 if (configopt[optind].func(optsub, optval,
5952 configopt[optind].funcflags) != 0)
5953 errx(1, "%s: line %zd: invalid data for %s",
5954 filename, lineno, configopt[optind].optname);
5961 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
5968 DNPRINTF(SWM_D_CONF, "conf_load: end with error.\n");
5974 set_child_transient(struct ws_win *win, Window *trans)
5976 struct ws_win *parent, *w;
5977 XWMHints *wmh = NULL;
5978 struct swm_region *r;
5979 struct workspace *ws;
5981 parent = find_window(win->transient);
5983 parent->child_trans = win;
5985 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
5986 " for 0x%lx trans 0x%lx\n", win->id, win->transient);
5988 if (win->hints == NULL) {
5989 warnx("no hints for 0x%lx", win->id);
5993 r = root_to_region(win->wa.root);
5995 /* parent doen't exist in our window list */
5996 TAILQ_FOREACH(w, &ws->winlist, entry) {
6000 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
6001 warnx("can't get hints for 0x%lx", w->id);
6005 if (win->hints->window_group != wmh->window_group)
6008 w->child_trans = win;
6009 win->transient = w->id;
6011 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
6012 "transient to 0x%lx\n", win->transient);
6022 window_get_pid(Window win)
6024 Atom actual_type_return;
6025 int actual_format_return = 0;
6026 unsigned long nitems_return = 0;
6027 unsigned long bytes_after_return = 0;
6031 unsigned char *prop = NULL;
6033 if (XGetWindowProperty(display, win,
6034 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
6035 XA_CARDINAL, &actual_type_return, &actual_format_return,
6036 &nitems_return, &bytes_after_return,
6037 (unsigned char**)(void*)&pid) != Success)
6039 if (actual_type_return != XA_CARDINAL)
6050 if (XGetWindowProperty(display, win,
6051 XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
6052 XA_STRING, &actual_type_return, &actual_format_return,
6053 &nitems_return, &bytes_after_return, &prop) != Success)
6055 if (actual_type_return != XA_STRING)
6060 ret = strtonum((const char *)prop, 0, UINT_MAX, &errstr);
6061 /* ignore error because strtonum returns 0 anyway */
6068 manage_window(Window id)
6071 struct workspace *ws;
6072 struct ws_win *win, *ww;
6073 int format, i, ws_idx, n, border_me = 0;
6074 unsigned long nitems, bytes;
6075 Atom ws_idx_atom = 0, type;
6076 Atom *prot = NULL, *pp;
6077 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
6078 struct swm_region *r;
6085 if ((win = find_window(id)) != NULL)
6086 return (win); /* already being managed */
6088 /* see if we are on the unmanaged list */
6089 if ((win = find_unmanaged_window(id)) != NULL) {
6090 DNPRINTF(SWM_D_MISC, "manage_window: previously unmanaged "
6091 "window: 0x%lx\n", win->id);
6092 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6094 set_child_transient(win, &trans);
6096 if (trans && (ww = find_window(trans)))
6097 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
6098 else if ((ww = win->ws->focus) &&
6099 spawn_position == SWM_STACK_ABOVE)
6100 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win, entry);
6101 else if (ww && spawn_position == SWM_STACK_BELOW)
6102 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win, entry);
6103 else switch (spawn_position) {
6106 case SWM_STACK_ABOVE:
6107 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
6109 case SWM_STACK_BOTTOM:
6110 case SWM_STACK_BELOW:
6111 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
6114 ewmh_update_actions(win);
6118 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
6119 err(1, "manage_window: calloc: failed to allocate memory for "
6124 /* see if we need to override the workspace */
6125 p = find_pid(window_get_pid(id));
6127 /* Get all the window data in one shot */
6128 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
6130 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
6131 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
6133 XGetWindowAttributes(display, id, &win->wa);
6134 XGetWMNormalHints(display, id, &win->sh, &win->sh_mask);
6135 win->hints = XGetWMHints(display, id);
6136 XGetTransientForHint(display, id, &trans);
6138 win->transient = trans;
6139 set_child_transient(win, &trans);
6140 DNPRINTF(SWM_D_MISC, "manage_window: window: 0x%lx, "
6141 "transient: 0x%lx\n", win->id, win->transient);
6144 /* get supported protocols */
6145 if (XGetWMProtocols(display, id, &prot, &n)) {
6146 for (i = 0, pp = prot; i < n; i++, pp++) {
6147 if (*pp == takefocus)
6148 win->take_focus = 1;
6150 win->can_delete = 1;
6156 win->iconic = get_iconic(win);
6159 * Figure out where to put the window. If it was previously assigned to
6160 * a workspace (either by spawn() or manually moving), and isn't
6161 * transient, * put it in the same workspace
6163 r = root_to_region(win->wa.root);
6165 ws = &r->s->ws[p->ws];
6166 TAILQ_REMOVE(&pidlist, p, entry);
6169 } else if (prop && win->transient == 0) {
6170 DNPRINTF(SWM_D_PROP, "manage_window: get _SWM_WS: %s\n", prop);
6171 ws_idx = strtonum((const char *)prop, 0, workspace_limit - 1,
6174 DNPRINTF(SWM_D_EVENT, "manage_window: window: #%s: %s",
6177 ws = &r->s->ws[ws_idx];
6180 /* this should launch transients in the same ws as parent */
6182 if ((ww = find_window(trans)) != NULL)
6188 warnx("manage_window: fix this "
6194 /* set up the window layout */
6197 win->s = r->s; /* this never changes */
6198 if (trans && (ww = find_window(trans)))
6199 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
6200 else if (spawn_position == SWM_STACK_ABOVE && win->ws->focus)
6201 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win, entry);
6203 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
6205 WIDTH(win) = win->wa.width;
6206 HEIGHT(win) = win->wa.height;
6209 win->g_floatvalid = 0;
6210 win->floatmaxed = 0;
6211 win->ewmh_flags = 0;
6213 /* Set window properties so we can remember this after reincarnation */
6214 if (ws_idx_atom && prop == NULL &&
6215 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
6217 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
6219 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
6220 PropModeReplace, ws_idx_str, strlen((char *)ws_idx_str));
6225 ewmh_autoquirk(win);
6227 if (XGetClassHint(display, win->id, &win->ch)) {
6228 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, name: %s\n",
6229 win->ch.res_class, win->ch.res_name);
6231 /* java is retarded so treat it special */
6232 if (strstr(win->ch.res_name, "sun-awt")) {
6237 TAILQ_FOREACH(qp, &quirks, entry) {
6238 if (!strcmp(win->ch.res_class, qp->class) &&
6239 !strcmp(win->ch.res_name, qp->name)) {
6240 DNPRINTF(SWM_D_CLASS, "manage_window: found: "
6241 "class: %s, name: %s\n", win->ch.res_class,
6243 if (qp->quirk & SWM_Q_FLOAT) {
6247 win->quirks = qp->quirk;
6252 /* alter window position if quirky */
6253 if (win->quirks & SWM_Q_ANYWHERE) {
6254 win->manual = 1; /* don't center the quirky windows */
6255 bzero(&wc, sizeof wc);
6257 if (bar_enabled && Y(win) < bar_height) {
6258 Y(win) = wc.y = bar_height;
6261 if (WIDTH(win) + X(win) > WIDTH(r)) {
6262 X(win) = wc.x = WIDTH(r) - WIDTH(win) - 2;
6268 /* Reset font sizes (the bruteforce way; no default keybinding). */
6269 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
6270 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6271 fake_keypress(win, XK_KP_Subtract, ShiftMask);
6272 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
6273 fake_keypress(win, XK_KP_Add, ShiftMask);
6276 ewmh_get_win_state(win);
6277 ewmh_update_actions(win);
6278 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
6282 bzero(&wc, sizeof wc);
6283 wc.border_width = border_width;
6284 mask |= CWBorderWidth;
6285 XConfigureWindow(display, win->id, mask, &wc);
6288 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
6289 PropertyChangeMask | StructureNotifyMask);
6291 /* floaters need to be mapped if they are in the current workspace */
6292 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
6293 XMapRaised(display, win->id);
6299 free_window(struct ws_win *win)
6301 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%lx\n", win->id);
6306 /* needed for restart wm */
6307 set_win_state(win, WithdrawnState);
6309 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
6311 if (win->ch.res_class)
6312 XFree(win->ch.res_class);
6313 if (win->ch.res_name)
6314 XFree(win->ch.res_name);
6319 memset(win, 0xff, sizeof *win); /* XXX kill later */
6325 unmanage_window(struct ws_win *win)
6327 struct ws_win *parent;
6332 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%lx\n", win->id);
6334 if (win->transient) {
6335 parent = find_window(win->transient);
6337 parent->child_trans = NULL;
6340 /* focus on root just in case */
6341 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
6350 TAILQ_REMOVE(&win->ws->winlist, win, entry);
6351 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
6355 focus_magic(struct ws_win *win)
6357 DNPRINTF(SWM_D_FOCUS, "focus_magic: window: 0x%lx\n", WINID(win));
6360 /* if there are no windows clear the status-bar */
6365 if (win->child_trans) {
6366 /* win = parent & has a transient so focus on that */
6368 focus_win(win->child_trans);
6369 if (win->child_trans->take_focus)
6370 client_msg(win, takefocus);
6372 /* make sure transient hasn't disappeared */
6373 if (validate_win(win->child_trans) == 0) {
6374 focus_win(win->child_trans);
6375 if (win->child_trans->take_focus)
6376 client_msg(win->child_trans, takefocus);
6378 win->child_trans = NULL;
6380 if (win->take_focus)
6381 client_msg(win, takefocus);
6387 if (win->take_focus)
6388 client_msg(win, takefocus);
6395 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%lx\n", e->xexpose.window);
6402 XKeyEvent *ev = &e->xkey;
6404 struct swm_region *r;
6406 keysym = XkbKeycodeToKeysym(display, (KeyCode)ev->keycode, 0, 0);
6407 if ((kp = key_lookup(CLEANMASK(ev->state), keysym)) == NULL)
6409 if (keyfuncs[kp->funcid].func == NULL)
6412 r = root_to_region(ev->root);
6413 if (kp->funcid == kf_spawn_custom)
6414 spawn_custom(r, &(keyfuncs[kp->funcid].args), kp->spawn_name);
6416 keyfuncs[kp->funcid].func(r, &(keyfuncs[kp->funcid].args));
6420 buttonpress(XEvent *e)
6424 XButtonPressedEvent *ev = &e->xbutton;
6426 if ((win = find_window(ev->window)) == NULL)
6430 action = client_click;
6432 for (i = 0; i < LENGTH(buttons); i++)
6433 if (action == buttons[i].action && buttons[i].func &&
6434 buttons[i].button == ev->button &&
6435 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
6436 buttons[i].func(win, &buttons[i].args);
6440 configurerequest(XEvent *e)
6442 XConfigureRequestEvent *ev = &e->xconfigurerequest;
6447 if ((win = find_window(ev->window)) == NULL)
6448 if ((win = find_unmanaged_window(ev->window)) == NULL)
6451 DNPRINTF(SWM_D_EVENT, "configurerequest: window: 0x%lx, new: %s\n",
6452 ev->window, YESNO(new));
6455 bzero(&wc, sizeof wc);
6458 wc.width = ev->width;
6459 wc.height = ev->height;
6460 wc.border_width = ev->border_width;
6461 wc.sibling = ev->above;
6462 wc.stack_mode = ev->detail;
6463 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
6465 config_win(win, ev);
6469 configurenotify(XEvent *e)
6473 DNPRINTF(SWM_D_EVENT, "configurenotify: window: 0x%lx\n",
6474 e->xconfigure.window);
6476 win = find_window(e->xconfigure.window);
6478 XGetWMNormalHints(display, win->id, &win->sh, &win->sh_mask);
6482 if (focus_mode == SWM_FOCUS_DEFAULT)
6483 drain_enter_notify();
6488 destroynotify(XEvent *e)
6491 XDestroyWindowEvent *ev = &e->xdestroywindow;
6493 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%lx\n", ev->window);
6495 if ((win = find_window(ev->window)) == NULL) {
6496 if ((win = find_unmanaged_window(ev->window)) == NULL)
6502 /* make sure we focus on something */
6505 unmanage_window(win);
6507 if (focus_mode == SWM_FOCUS_DEFAULT)
6508 drain_enter_notify();
6513 enternotify(XEvent *e)
6515 XCrossingEvent *ev = &e->xcrossing;
6520 Window focus_return;
6521 int revert_to_return;
6523 DNPRINTF(SWM_D_FOCUS, "enternotify: window: 0x%lx, mode: %d, detail: "
6524 "%d, root: 0x%lx, subwindow: 0x%lx, same_screen: %s, focus: %s, "
6525 "state: %d\n", ev->window, ev->mode, ev->detail, ev->root,
6526 ev->subwindow, YESNO(ev->same_screen), YESNO(ev->focus), ev->state);
6528 if (ev->mode != NotifyNormal) {
6529 DNPRINTF(SWM_D_EVENT, "skip enternotify: generated by "
6534 switch (focus_mode) {
6535 case SWM_FOCUS_DEFAULT:
6537 case SWM_FOCUS_FOLLOW:
6539 case SWM_FOCUS_SYNERGY:
6542 * all these checks need to be in this order because the
6543 * XCheckTypedWindowEvent relies on weeding out the previous events
6545 * making this code an option would enable a follow mouse for focus
6550 * state is set when we are switching workspaces and focus is set when
6551 * the window or a subwindow already has focus (occurs during restart).
6553 * Only honor the focus flag if last_focus_event is not FocusOut,
6554 * this allows spectrwm to continue to control focus when another
6555 * program is also playing with it.
6557 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
6558 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
6563 * happens when a window is created or destroyed and the border
6564 * crosses the mouse pointer and when switching ws
6566 * we need the subwindow test to see if we came from root in order
6567 * to give focus to floaters
6569 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
6570 ev->subwindow == 0) {
6571 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
6575 /* this window already has focus */
6576 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
6577 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
6581 /* this window is being deleted or moved to another ws */
6582 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
6584 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
6585 XPutBackEvent(display, &cne);
6589 if ((win = find_window(ev->window)) == NULL) {
6590 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6595 * In fullstack kill all enters unless they come from a different ws
6596 * (i.e. another region) or focus has been grabbed externally.
6598 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
6599 last_focus_event != FocusOut) {
6600 XGetInputFocus(display, &focus_return, &revert_to_return);
6601 if ((w = find_window(focus_return)) == NULL ||
6603 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
6611 if ((win = find_window(ev->window)) == NULL) {
6612 DNPRINTF(SWM_D_EVENT, "skip enternotify: window is NULL\n");
6617 * if we have more enternotifies let them handle it in due time
6619 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
6620 DNPRINTF(SWM_D_EVENT,
6621 "ignoring enternotify: got more enternotify\n");
6622 XPutBackEvent(display, &cne);
6629 /* lets us use one switch statement for arbitrary mode/detail combinations */
6630 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6633 focusevent(XEvent *e)
6637 u_int32_t mode_detail;
6638 XFocusChangeEvent *ev = &e->xfocus;
6640 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: 0x%lx mode: %d "
6641 "detail: %d\n", ev->type == FocusIn ? "entering" : "leaving",
6642 ev->window, ev->mode, ev->detail);
6644 if (last_focus_event == ev->type) {
6645 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
6649 last_focus_event = ev->type;
6650 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
6652 switch (mode_detail) {
6653 /* synergy client focus operations */
6654 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
6655 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
6657 /* synergy server focus operations */
6658 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
6660 /* Entering applications like rdesktop that mangle the pointer */
6661 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
6663 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
6664 XSetWindowBorder(display, win->id,
6665 win->ws->r->s->c[ev->type == FocusIn ?
6666 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
6669 warnx("ignoring focusevent");
6670 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
6677 mapnotify(XEvent *e)
6680 XMapEvent *ev = &e->xmap;
6682 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%lx\n", ev->window);
6684 win = manage_window(ev->window);
6686 set_win_state(win, NormalState);
6689 * focus_win can only set input focus on a mapped window.
6690 * make sure the window really has focus since it is just being mapped.
6692 if (win->ws->focus == win)
6697 mappingnotify(XEvent *e)
6699 XMappingEvent *ev = &e->xmapping;
6701 XRefreshKeyboardMapping(ev);
6702 if (ev->request == MappingKeyboard)
6707 maprequest(XEvent *e)
6710 struct swm_region *r;
6711 XWindowAttributes wa;
6712 XMapRequestEvent *ev = &e->xmaprequest;
6714 DNPRINTF(SWM_D_EVENT, "maprequest: window: 0x%lx\n",
6715 e->xmaprequest.window);
6717 if (!XGetWindowAttributes(display, ev->window, &wa))
6719 if (wa.override_redirect)
6722 win = manage_window(e->xmaprequest.window);
6724 return; /* can't happen */
6728 /* make new win focused */
6729 r = root_to_region(win->wa.root);
6730 if (win->ws == r->ws)
6735 propertynotify(XEvent *e)
6738 XPropertyEvent *ev = &e->xproperty;
6741 name = XGetAtomName(display, ev->atom);
6742 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%lx, atom: %s\n",
6747 win = find_window(ev->window);
6751 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
6752 update_iconic(win, 0);
6753 XMapRaised(display, win->id);
6761 case XA_WM_NORMAL_HINTS:
6763 XGetWMNormalHints(display, win->id, &win->sh, &mask);
6764 warnx("normal hints: flag 0x%x", win->sh.flags);
6765 if (win->sh.flags & PMinSize) {
6766 WIDTH(win) = win->sh.min_width;
6767 HEIGHT(win) = win->sh.min_height;
6768 warnx("min %d %d", WIDTH(win), HEIGHT(win));
6770 XMoveResizeWindow(display, win->id,
6771 X(win), Y(win), WIDTH(win), HEIGHT(win));
6783 unmapnotify(XEvent *e)
6787 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%lx\n", e->xunmap.window);
6789 /* determine if we need to help unmanage this window */
6790 win = find_window(e->xunmap.window);
6794 if (getstate(e->xunmap.window) == NormalState) {
6795 unmanage_window(win);
6798 /* giant hack for apps that don't destroy transient windows */
6799 /* eat a bunch of events to prevent remanaging the window */
6801 while (XCheckWindowEvent(display, e->xunmap.window,
6802 EnterWindowMask, &cne))
6804 while (XCheckWindowEvent(display, e->xunmap.window,
6805 StructureNotifyMask, &cne))
6807 while (XCheckWindowEvent(display, e->xunmap.window,
6808 SubstructureNotifyMask, &cne))
6810 /* resend unmap because we ated it */
6811 XUnmapWindow(display, e->xunmap.window);
6814 if (focus_mode == SWM_FOCUS_DEFAULT)
6815 drain_enter_notify();
6819 visibilitynotify(XEvent *e)
6822 struct swm_region *r;
6824 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: 0x%lx\n",
6825 e->xvisibility.window);
6826 if (e->xvisibility.state == VisibilityUnobscured)
6827 for (i = 0; i < ScreenCount(display); i++)
6828 TAILQ_FOREACH(r, &screens[i].rl, entry)
6829 if (e->xvisibility.window == r->bar_window)
6834 clientmessage(XEvent *e)
6836 XClientMessageEvent *ev;
6841 win = find_window(ev->window);
6845 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx, type: %ld\n",
6846 ev->window, ev->message_type);
6848 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
6849 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
6852 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
6853 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
6854 if (win->can_delete)
6855 client_msg(win, adelete);
6857 XKillClient(display, win->id);
6859 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
6860 DNPRINTF(SWM_D_EVENT,
6861 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
6862 if (win->floating) {
6863 if (ev->data.l[0] & (1<<8)) /* x */
6864 X(win) = ev->data.l[1];
6865 if (ev->data.l[0] & (1<<9)) /* y */
6866 Y(win) = ev->data.l[2];
6867 if (ev->data.l[0] & (1<<10)) /* width */
6868 WIDTH(win) = ev->data.l[3];
6869 if (ev->data.l[0] & (1<<11)) /* height */
6870 HEIGHT(win) = ev->data.l[4];
6875 /* TODO: Change stack sizes */
6876 /* notify no change was made. */
6877 config_win(win, NULL);
6880 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
6881 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
6882 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
6884 ewmh_update_win_state(win, ev->data.l[2],
6892 xerror_start(Display *d, XErrorEvent *ee)
6899 xerror(Display *d, XErrorEvent *ee)
6901 /* warnx("error: %p %p", display, ee); */
6909 xerrorxlib = XSetErrorHandler(xerror_start);
6911 /* this causes an error if some other window manager is running */
6912 XSelectInput(display, DefaultRootWindow(display),
6913 SubstructureRedirectMask);
6914 XSync(display, False);
6918 XSetErrorHandler(xerror);
6919 XSync(display, False);
6924 new_region(struct swm_screen *s, int x, int y, int w, int h)
6926 struct swm_region *r, *n;
6927 struct workspace *ws = NULL;
6930 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
6931 s->idx, w, h, x, y);
6933 /* remove any conflicting regions */
6934 n = TAILQ_FIRST(&s->rl);
6937 n = TAILQ_NEXT(r, entry);
6938 if (X(r) < (x + w) &&
6939 (X(r) + WIDTH(r)) > x &&
6941 (Y(r) + HEIGHT(r)) > y) {
6942 if (r->ws->r != NULL)
6943 r->ws->old_r = r->ws->r;
6945 XDestroyWindow(display, r->bar_window);
6946 TAILQ_REMOVE(&s->rl, r, entry);
6947 TAILQ_INSERT_TAIL(&s->orl, r, entry);
6951 /* search old regions for one to reuse */
6953 /* size + location match */
6954 TAILQ_FOREACH(r, &s->orl, entry)
6955 if (X(r) == x && Y(r) == y &&
6956 HEIGHT(r) == h && WIDTH(r) == w)
6960 TAILQ_FOREACH(r, &s->orl, entry)
6961 if (HEIGHT(r) == h && WIDTH(r) == w)
6965 TAILQ_REMOVE(&s->orl, r, entry);
6966 /* try to use old region's workspace */
6967 if (r->ws->r == NULL)
6970 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
6971 err(1, "new_region: calloc: failed to allocate memory "
6974 /* if we don't have a workspace already, find one */
6976 for (i = 0; i < workspace_limit; i++)
6977 if (s->ws[i].r == NULL) {
6984 errx(1, "new_region: no free workspaces");
6995 TAILQ_INSERT_TAIL(&s->rl, r, entry);
7001 #ifdef SWM_XRR_HAS_CRTC
7003 XRRScreenResources *sr;
7006 #endif /* SWM_XRR_HAS_CRTC */
7007 struct swm_region *r;
7010 if (i >= ScreenCount(display))
7011 errx(1, "scan_xrandr: invalid screen");
7013 /* remove any old regions */
7014 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
7015 r->ws->old_r = r->ws->r = NULL;
7016 XDestroyWindow(display, r->bar_window);
7017 TAILQ_REMOVE(&screens[i].rl, r, entry);
7018 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
7022 /* map virtual screens onto physical screens */
7023 #ifdef SWM_XRR_HAS_CRTC
7024 if (xrandr_support) {
7025 sr = XRRGetScreenResources(display, screens[i].root);
7027 new_region(&screens[i], 0, 0,
7028 DisplayWidth(display, i),
7029 DisplayHeight(display, i));
7033 for (c = 0, ci = NULL; c < ncrtc; c++) {
7034 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
7035 if (ci->noutput == 0)
7038 if (ci != NULL && ci->mode == None)
7039 new_region(&screens[i], 0, 0,
7040 DisplayWidth(display, i),
7041 DisplayHeight(display, i));
7043 new_region(&screens[i],
7044 ci->x, ci->y, ci->width, ci->height);
7047 XRRFreeCrtcInfo(ci);
7048 XRRFreeScreenResources(sr);
7050 #endif /* SWM_XRR_HAS_CRTC */
7052 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
7053 DisplayHeight(display, i));
7058 screenchange(XEvent *e) {
7059 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
7060 struct swm_region *r;
7063 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%lx\n", xe->root);
7065 if (!XRRUpdateConfiguration(e))
7068 /* silly event doesn't include the screen index */
7069 for (i = 0; i < ScreenCount(display); i++)
7070 if (screens[i].root == xe->root)
7072 if (i >= ScreenCount(display))
7073 errx(1, "screenchange: screen not found");
7075 /* brute force for now, just re-enumerate the regions */
7078 /* add bars to all regions */
7079 for (i = 0; i < ScreenCount(display); i++)
7080 TAILQ_FOREACH(r, &screens[i].rl, entry)
7083 if (focus_mode == SWM_FOCUS_DEFAULT)
7084 drain_enter_notify();
7090 Window d1, d2, *wins = NULL;
7091 XWindowAttributes wa;
7096 for (i = 0; i < ScreenCount(display); i++) {
7097 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
7100 /* attach windows to a region */
7101 /* normal windows */
7102 for (j = 0; j < no; j++) {
7103 if (!XGetWindowAttributes(display, wins[j], &wa) ||
7104 wa.override_redirect ||
7105 XGetTransientForHint(display, wins[j], &d1))
7108 state = getstate(wins[j]);
7109 manage = state == IconicState;
7110 if (wa.map_state == IsViewable || manage)
7111 manage_window(wins[j]);
7113 /* transient windows */
7114 for (j = 0; j < no; j++) {
7115 if (!XGetWindowAttributes(display, wins[j], &wa) ||
7116 wa.override_redirect)
7119 state = getstate(wins[j]);
7120 manage = state == IconicState;
7121 if (XGetTransientForHint(display, wins[j], &d1) &&
7123 manage_window(wins[j]);
7136 int errorbase, major, minor;
7137 struct workspace *ws;
7140 if ((screens = calloc(ScreenCount(display),
7141 sizeof(struct swm_screen))) == NULL)
7142 err(1, "setup_screens: calloc: failed to allocate memory for "
7145 /* initial Xrandr setup */
7146 xrandr_support = XRRQueryExtension(display,
7147 &xrandr_eventbase, &errorbase);
7149 if (XRRQueryVersion(display, &major, &minor) && major < 1)
7152 /* map physical screens */
7153 for (i = 0; i < ScreenCount(display); i++) {
7154 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
7156 TAILQ_INIT(&screens[i].rl);
7157 TAILQ_INIT(&screens[i].orl);
7158 screens[i].root = RootWindow(display, i);
7160 /* set default colors */
7161 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
7162 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
7163 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
7164 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
7165 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
7167 /* create graphics context on screen with default font color */
7168 screens[i].bar_gc = XCreateGC(display, screens[i].root, 0,
7171 XSetForeground(display, screens[i].bar_gc,
7172 screens[i].c[SWM_S_COLOR_BAR_FONT].color);
7174 /* set default cursor */
7175 XDefineCursor(display, screens[i].root,
7176 XCreateFontCursor(display, XC_left_ptr));
7178 /* init all workspaces */
7179 /* XXX these should be dynamically allocated too */
7180 for (j = 0; j < SWM_WS_MAX; j++) {
7181 ws = &screens[i].ws[j];
7187 TAILQ_INIT(&ws->winlist);
7188 TAILQ_INIT(&ws->unmanagedlist);
7190 for (k = 0; layouts[k].l_stack != NULL; k++)
7191 if (layouts[k].l_config != NULL)
7192 layouts[k].l_config(ws,
7193 SWM_ARG_ID_STACKINIT);
7194 ws->cur_layout = &layouts[0];
7195 ws->cur_layout->l_string(ws);
7201 XRRSelectInput(display, screens[i].root,
7202 RRScreenChangeNotifyMask);
7209 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
7210 err(1, "setup_globals: strdup: failed to allocate memory.");
7212 if ((spawn_term[0] = strdup("xterm")) == NULL)
7213 err(1, "setup_globals: strdup: failed to allocate memory.");
7215 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
7216 err(1, "setup_globals: strdup: failed to allocate memory.");
7223 Atom netwmcheck, netwmname, utf8_string;
7226 /* work around sun jdk bugs, code from wmname */
7227 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
7228 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
7229 utf8_string = XInternAtom(display, "UTF8_STRING", False);
7230 for (i = 0; i < ScreenCount(display); i++) {
7231 root = screens[i].root;
7232 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
7233 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
7234 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
7236 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
7237 PropModeReplace, (unsigned char *)&win, 1);
7238 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
7239 PropModeReplace, (unsigned char *)&win, 1);
7240 XChangeProperty(display, win, netwmname, utf8_string, 8,
7241 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
7246 main(int argc, char *argv[])
7248 struct swm_region *r, *rr;
7249 struct ws_win *winfocus = NULL;
7252 char conf[PATH_MAX], *cfile = NULL;
7257 struct sigaction sact;
7260 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
7261 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
7263 warnx("no locale support");
7265 if (!X_HAVE_UTF8_STRING)
7266 warnx("no UTF-8 support");
7268 if (!(display = XOpenDisplay(0)))
7269 errx(1, "can not open display");
7272 errx(1, "other wm running");
7274 /* handle some signals */
7275 bzero(&sact, sizeof(sact));
7276 sigemptyset(&sact.sa_mask);
7278 sact.sa_handler = sighdlr;
7279 sigaction(SIGINT, &sact, NULL);
7280 sigaction(SIGQUIT, &sact, NULL);
7281 sigaction(SIGTERM, &sact, NULL);
7282 sigaction(SIGHUP, &sact, NULL);
7284 sact.sa_handler = sighdlr;
7285 sact.sa_flags = SA_NOCLDSTOP;
7286 sigaction(SIGCHLD, &sact, NULL);
7288 astate = XInternAtom(display, "WM_STATE", False);
7289 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
7290 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
7291 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
7292 a_wmname = XInternAtom(display, "WM_NAME", False);
7293 a_netwmname = XInternAtom(display, "_NET_WM_NAME", False);
7294 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
7295 a_string = XInternAtom(display, "STRING", False);
7296 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
7298 /* look for local and global conf file */
7299 pwd = getpwuid(getuid());
7301 errx(1, "invalid user: %d", getuid());
7310 for (i = 0; ; i++) {
7315 snprintf(conf, sizeof conf, "%s/.%s",
7316 pwd->pw_dir, SWM_CONF_FILE);
7320 snprintf(conf, sizeof conf, "/etc/%s",
7325 snprintf(conf, sizeof conf, "%s/.%s",
7326 pwd->pw_dir, SWM_CONF_FILE_OLD);
7330 snprintf(conf, sizeof conf, "/etc/%s",
7337 if (strlen(conf) && stat(conf, &sb) != -1)
7338 if (S_ISREG(sb.st_mode)) {
7345 /* load conf (if any) and refresh font color in bar graphics contexts */
7346 if (cfile && conf_load(cfile, SWM_CONF_DEFAULT) == 0)
7347 for (i = 0; i < ScreenCount(display); ++i)
7348 XSetForeground(display, screens[i].bar_gc,
7349 screens[i].c[SWM_S_COLOR_BAR_FONT].color);
7352 /* set some values to work around bad programs */
7354 /* grab existing windows (before we build the bars) */
7357 if (getenv("SWM_STARTED") == NULL)
7358 setenv("SWM_STARTED", "YES", 1);
7360 /* setup all bars */
7361 for (i = 0; i < ScreenCount(display); i++)
7362 TAILQ_FOREACH(r, &screens[i].rl, entry) {
7363 if (winfocus == NULL)
7364 winfocus = TAILQ_FIRST(&r->ws->winlist);
7372 if (focus_mode == SWM_FOCUS_DEFAULT)
7373 drain_enter_notify();
7375 xfd = ConnectionNumber(display);
7377 while (XPending(display)) {
7378 XNextEvent(display, &e);
7381 if (e.type < LASTEvent) {
7382 DNPRINTF(SWM_D_EVENTQ ,"XEvent: handled: %s, "
7383 "window: 0x%lx, type: %s (%d), %d remaining"
7384 "\n", YESNO(handler[e.type]),
7385 e.xany.window, geteventname(&e),
7386 e.type, QLength(display));
7388 if (handler[e.type])
7389 handler[e.type](&e);
7391 DNPRINTF(SWM_D_EVENTQ, "XRandr Event: window: "
7392 "0x%lx, type: %s (%d)\n", e.xany.window,
7393 xrandr_geteventname(&e), e.type);
7395 switch (e.type - xrandr_eventbase) {
7396 case RRScreenChangeNotify:
7405 /* if we are being restarted go focus on first window */
7407 rr = winfocus->ws->r;
7409 /* not a visible window */
7413 /* move pointer to first screen if multi screen */
7414 if (ScreenCount(display) > 1 || outputs > 1)
7415 XWarpPointer(display, None, rr->s[0].root,
7417 Y(rr) + (bar_enabled ? bar_height : 0));
7419 a.id = SWM_ARG_ID_FOCUSCUR;
7429 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
7431 DNPRINTF(SWM_D_MISC, "select failed");
7432 if (restart_wm == 1)
7433 restart(NULL, NULL);
7434 if (search_resp == 1)
7447 for (i = 0; i < ScreenCount(display); ++i)
7448 if (screens[i].bar_gc != NULL)
7449 XFreeGC(display, screens[i].bar_gc);
7451 XFreeFontSet(display, bar_fs);
7452 XCloseDisplay(display);