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-2013 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>
11 * Copyright (c) 2012-2013 David Hill <dhill@mindcry.org>
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Much code and ideas taken from dwm under the following license:
27 * MIT/X Consortium License
29 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
30 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
31 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
32 * 2007 Premysl Hruby <dfenze at gmail dot com>
33 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
34 * 2007 Christof Musik <christof at sendfax dot de>
35 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
36 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
37 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
46 * The above copyright notice and this permission notice shall be included in
47 * all copies or substantial portions of the Software.
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
52 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
54 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
55 * DEALINGS IN THE SOFTWARE.
59 #include <sys/types.h>
63 #include <sys/queue.h>
64 #include <sys/param.h>
65 #include <sys/select.h>
66 #if defined(__linux__)
68 #elif defined(__OpenBSD__)
70 #elif defined(__FreeBSD__)
92 #include <X11/cursorfont.h>
93 #include <X11/extensions/Xrandr.h>
94 #include <X11/Xcursor/Xcursor.h>
95 #include <X11/Xft/Xft.h>
96 #include <X11/Xlib-xcb.h>
97 #include <xcb/xcb_atom.h>
98 #include <xcb/xcb_aux.h>
99 #include <xcb/xcb_event.h>
100 #include <xcb/xcb_icccm.h>
101 #include <xcb/xcb_keysyms.h>
102 #include <xcb/xtest.h>
103 #include <xcb/randr.h>
111 #ifdef SPECTRWM_BUILDSTR
112 static const char *buildstr = SPECTRWM_BUILDSTR;
114 static const char *buildstr = SPECTRWM_VERSION;
117 #if !defined(__CYGWIN__) /* cygwin chokes on xrandr stuff */
119 # error XRandR versions less than 1.0 are not supported
122 # if RANDR_MAJOR >= 1
123 # if RANDR_MINOR >= 2
124 # define SWM_XRR_HAS_CRTC
127 #endif /* __CYGWIN__ */
129 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
130 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
131 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
132 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
133 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
134 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
135 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
136 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
137 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
138 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
139 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
140 #define xcb_icccm_get_wm_class xcb_get_wm_class
141 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
142 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
143 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
144 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
145 #define xcb_icccm_wm_hints_get_urgency xcb_wm_hints_get_urgency
146 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
147 #define xcb_icccm_get_wm_name xcb_get_wm_name
148 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
149 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
150 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
151 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
152 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
153 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
154 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
155 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
156 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
157 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
160 /*#define SWM_DEBUG*/
162 #define DPRINTF(x...) do { \
164 fprintf(stderr, x); \
166 #define DNPRINTF(n,x...) do { \
167 if (swm_debug & n) { \
168 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
169 fprintf(stderr, x); \
172 #define SWM_D_MISC 0x0001
173 #define SWM_D_EVENT 0x0002
174 #define SWM_D_WS 0x0004
175 #define SWM_D_FOCUS 0x0008
176 #define SWM_D_MOVE 0x0010
177 #define SWM_D_STACK 0x0020
178 #define SWM_D_MOUSE 0x0040
179 #define SWM_D_PROP 0x0080
180 #define SWM_D_CLASS 0x0100
181 #define SWM_D_KEY 0x0200
182 #define SWM_D_QUIRK 0x0400
183 #define SWM_D_SPAWN 0x0800
184 #define SWM_D_EVENTQ 0x1000
185 #define SWM_D_CONF 0x2000
186 #define SWM_D_BAR 0x4000
187 #define SWM_D_INIT 0x8000
189 u_int32_t swm_debug = 0
208 #define DPRINTF(x...)
209 #define DNPRINTF(n,x...)
212 /* convert 8-bit to 16-bit */
213 #define RGB_8_TO_16(col) ((col) << 8) + (col)
215 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
216 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
217 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
218 xrc.blue = RGB_8_TO_16((px) & 0xff); \
221 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
222 #define MODKEY XCB_MOD_MASK_1
223 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
224 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
225 XCB_EVENT_MASK_BUTTON_RELEASE)
226 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
227 #define SWM_PROPLEN (16)
228 #define SWM_FUNCNAME_LEN (32)
229 #define SWM_KEYS_LEN (255)
230 #define SWM_QUIRK_LEN (64)
231 #define X(r) (r)->g.x
232 #define Y(r) (r)->g.y
233 #define WIDTH(r) (r)->g.w
234 #define HEIGHT(r) (r)->g.h
235 #define BORDER(w) ((w)->bordered ? border_width : 0)
236 #define MAX_X(r) ((r)->g.x + (r)->g.w)
237 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
238 #define SH_MIN(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE
239 #define SH_MIN_W(w) (w)->sh.min_width
240 #define SH_MIN_H(w) (w)->sh.min_height
241 #define SH_MAX(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE
242 #define SH_MAX_W(w) (w)->sh.max_width
243 #define SH_MAX_H(w) (w)->sh.max_height
244 #define SH_INC(w) (w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC
245 #define SH_INC_W(w) (w)->sh.width_inc
246 #define SH_INC_H(w) (w)->sh.height_inc
247 #define SWM_MAX_FONT_STEPS (3)
248 #define WINID(w) ((w) ? (w)->id : XCB_WINDOW_NONE)
249 #define WS_FOCUSED(ws) ((ws)->r && (ws)->r->s->r_focus == (ws)->r)
250 #define YESNO(x) ((x) ? "yes" : "no")
252 /* Constrain Window flags */
253 #define SWM_CW_RESIZABLE (0x01)
254 #define SWM_CW_SOFTBOUNDARY (0x02)
255 #define SWM_CW_HARDBOUNDARY (0x04)
256 #define SWM_CW_RIGHT (0x10)
257 #define SWM_CW_LEFT (0x20)
258 #define SWM_CW_BOTTOM (0x40)
259 #define SWM_CW_TOP (0x80)
260 #define SWM_CW_ALLSIDES (0xf0)
262 #define SWM_FOCUS_DEFAULT (0)
263 #define SWM_FOCUS_FOLLOW (1)
264 #define SWM_FOCUS_MANUAL (2)
266 #define SWM_CK_NONE 0
267 #define SWM_CK_ALL 0xf
268 #define SWM_CK_FOCUS 0x1
269 #define SWM_CK_POINTER 0x2
270 #define SWM_CK_FALLBACK 0x4
271 #define SWM_CK_REGION 0x8
273 #define SWM_CONF_DEFAULT (0)
274 #define SWM_CONF_KEYMAPPING (1)
277 #define SWM_LIB "/usr/local/lib/libswmhack.so"
284 xcb_atom_t a_takefocus;
285 xcb_atom_t a_utf8_string;
286 xcb_atom_t a_swm_iconic;
288 volatile sig_atomic_t running = 1;
289 volatile sig_atomic_t restart_wm = 0;
290 xcb_timestamp_t last_event_time = 0;
294 int xrandr_eventbase;
295 unsigned int numlockmask = 0;
298 xcb_connection_t *conn;
299 xcb_key_symbols_t *syms;
301 int boundary_width = 50;
303 int cycle_visible = 0;
305 int font_adjusted = 0;
306 unsigned int mod_key = MODKEY;
309 struct swm_region *search_r;
310 int select_list_pipe[2];
311 int select_resp_pipe[2];
313 volatile sig_atomic_t search_resp;
314 int search_resp_action;
316 struct search_window {
317 TAILQ_ENTRY(search_window) entry;
321 xcb_window_t indicator;
323 TAILQ_HEAD(search_winlist, search_window);
324 struct search_winlist search_wl;
329 SWM_SEARCH_UNICONIFY,
330 SWM_SEARCH_NAME_WORKSPACE,
331 SWM_SEARCH_SEARCH_WORKSPACE,
332 SWM_SEARCH_SEARCH_WINDOW
335 #define SWM_STACK_TOP (0)
336 #define SWM_STACK_BOTTOM (1)
337 #define SWM_STACK_ABOVE (2)
338 #define SWM_STACK_BELOW (3)
341 double dialog_ratio = 0.6;
343 #define SWM_BAR_MAX (256)
344 #define SWM_BAR_JUSTIFY_LEFT (0)
345 #define SWM_BAR_JUSTIFY_CENTER (1)
346 #define SWM_BAR_JUSTIFY_RIGHT (2)
347 #define SWM_BAR_OFFSET (4)
348 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
349 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
350 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
351 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
352 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
354 #ifdef X_HAVE_UTF8_STRING
355 #define DRAWSTRING(x...) Xutf8DrawString(x)
357 #define DRAWSTRING(x...) XmbDrawString(x)
360 char *bar_argv[] = { NULL, NULL };
362 char bar_ext[SWM_BAR_MAX];
363 char bar_ext_buf[SWM_BAR_MAX];
364 char bar_vertext[SWM_BAR_MAX];
367 int bar_border_width = 1;
368 int bar_at_bottom = 0;
372 int bar_justify = SWM_BAR_JUSTIFY_LEFT;
373 char *bar_format = NULL;
374 int stack_enabled = 1;
375 int clock_enabled = 1;
376 int iconic_enabled = 0;
377 int urgent_enabled = 0;
378 char *clock_format = NULL;
379 int window_class_enabled = 0;
380 int window_instance_enabled = 0;
381 int window_name_enabled = 0;
382 int focus_mode = SWM_FOCUS_DEFAULT;
383 int focus_close = SWM_STACK_BELOW;
384 int focus_close_wrap = 1;
385 int focus_default = SWM_STACK_TOP;
386 int spawn_position = SWM_STACK_TOP;
387 int disable_border = 0;
388 int border_width = 1;
389 int region_padding = 0;
391 int verbose_layout = 0;
395 XFontSetExtents *bar_fs_extents;
397 int bar_font_legacy = 1;
399 XftColor bar_font_color;
401 char *startup_exception;
402 unsigned int nr_exceptions = 0;
404 /* layout manager data */
405 struct swm_geometry {
418 struct swm_geometry g;
421 /* virtual "screens" */
423 TAILQ_ENTRY(swm_region) entry;
425 struct swm_geometry g;
426 struct workspace *ws; /* current workspace on this region */
427 struct workspace *ws_prior; /* prior workspace on this region */
428 struct swm_screen *s; /* screen idx */
431 TAILQ_HEAD(swm_region_list, swm_region);
434 TAILQ_ENTRY(ws_win) entry;
436 xcb_window_t transient;
437 struct ws_win *focus_child; /* focus on child transient */
438 struct swm_geometry g; /* current geometry */
439 struct swm_geometry g_float; /* region coordinates */
440 int g_floatvalid; /* g_float geometry validity */
447 unsigned int ewmh_flags;
448 int font_size_boundary[SWM_MAX_FONT_STEPS];
454 unsigned long quirks;
455 struct workspace *ws; /* always valid */
456 struct swm_screen *s; /* always valid, never changes */
458 xcb_icccm_get_wm_class_reply_t ch;
459 xcb_icccm_wm_hints_t hints;
461 TAILQ_HEAD(ws_win_list, ws_win);
465 TAILQ_ENTRY(pid_e) entry;
469 TAILQ_HEAD(pid_list, pid_e);
470 struct pid_list pidlist = TAILQ_HEAD_INITIALIZER(pidlist);
472 /* layout handlers */
474 void vertical_config(struct workspace *, int);
475 void vertical_stack(struct workspace *, struct swm_geometry *);
476 void horizontal_config(struct workspace *, int);
477 void horizontal_stack(struct workspace *, struct swm_geometry *);
478 void max_stack(struct workspace *, struct swm_geometry *);
479 void plain_stacker(struct workspace *);
480 void fancy_stacker(struct workspace *);
483 void (*l_stack)(struct workspace *, struct swm_geometry *);
484 void (*l_config)(struct workspace *, int);
486 #define SWM_L_FOCUSPREV (1<<0)
487 #define SWM_L_MAPONFOCUS (1<<1)
488 void (*l_string)(struct workspace *);
490 /* stack, configure */
491 { vertical_stack, vertical_config, 0, plain_stacker },
492 { horizontal_stack, horizontal_config, 0, plain_stacker },
494 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, plain_stacker },
495 { NULL, NULL, 0, NULL },
498 /* position of max_stack mode in the layouts array, index into layouts! */
499 #define SWM_V_STACK (0)
500 #define SWM_H_STACK (1)
501 #define SWM_MAX_STACK (2)
503 #define SWM_H_SLICE (32)
504 #define SWM_V_SLICE (32)
506 /* define work spaces */
508 int idx; /* workspace index */
509 char *name; /* workspace name */
510 int always_raise; /* raise windows on focus */
511 int bar_enabled; /* bar visibility */
512 struct layout *cur_layout; /* current layout handlers */
513 struct ws_win *focus; /* may be NULL */
514 struct ws_win *focus_prev; /* may be NULL */
515 struct ws_win *focus_pending; /* may be NULL */
516 struct swm_region *r; /* may be NULL */
517 struct swm_region *old_r; /* may be NULL */
518 struct ws_win_list winlist; /* list of windows in ws */
519 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
521 char stacker[10]; /* display stacker and layout */
525 int horizontal_msize;
527 int horizontal_stacks;
538 SWM_WS_STATE_MAPPING,
544 SWM_S_COLOR_BAR_BORDER,
545 SWM_S_COLOR_BAR_BORDER_UNFOCUS,
546 SWM_S_COLOR_BAR_FONT,
552 /* physical screen mapping */
553 #define SWM_WS_MAX (22) /* hard limit */
554 int workspace_limit = 10; /* soft limit */
557 int idx; /* screen index */
558 struct swm_region_list rl; /* list of regions on this screen */
559 struct swm_region_list orl; /* list of old regions */
561 struct workspace ws[SWM_WS_MAX];
562 struct swm_region *r_focus;
568 } c[SWM_S_COLOR_MAX];
570 xcb_gcontext_t bar_gc;
573 struct swm_screen *screens;
575 /* args to functions */
578 #define SWM_ARG_ID_FOCUSNEXT (0)
579 #define SWM_ARG_ID_FOCUSPREV (1)
580 #define SWM_ARG_ID_FOCUSMAIN (2)
581 #define SWM_ARG_ID_FOCUSURGENT (3)
582 #define SWM_ARG_ID_SWAPNEXT (10)
583 #define SWM_ARG_ID_SWAPPREV (11)
584 #define SWM_ARG_ID_SWAPMAIN (12)
585 #define SWM_ARG_ID_MOVELAST (13)
586 #define SWM_ARG_ID_MASTERSHRINK (20)
587 #define SWM_ARG_ID_MASTERGROW (21)
588 #define SWM_ARG_ID_MASTERADD (22)
589 #define SWM_ARG_ID_MASTERDEL (23)
590 #define SWM_ARG_ID_FLIPLAYOUT (24)
591 #define SWM_ARG_ID_STACKRESET (30)
592 #define SWM_ARG_ID_STACKINIT (31)
593 #define SWM_ARG_ID_CYCLEWS_UP (40)
594 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
595 #define SWM_ARG_ID_CYCLERG_UP (42)
596 #define SWM_ARG_ID_CYCLERG_DOWN (43)
597 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
598 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
599 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
600 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
601 #define SWM_ARG_ID_STACKINC (50)
602 #define SWM_ARG_ID_STACKDEC (51)
603 #define SWM_ARG_ID_SS_ALL (60)
604 #define SWM_ARG_ID_SS_WINDOW (61)
605 #define SWM_ARG_ID_DONTCENTER (70)
606 #define SWM_ARG_ID_CENTER (71)
607 #define SWM_ARG_ID_KILLWINDOW (80)
608 #define SWM_ARG_ID_DELETEWINDOW (81)
609 #define SWM_ARG_ID_WIDTHGROW (90)
610 #define SWM_ARG_ID_WIDTHSHRINK (91)
611 #define SWM_ARG_ID_HEIGHTGROW (92)
612 #define SWM_ARG_ID_HEIGHTSHRINK (93)
613 #define SWM_ARG_ID_MOVEUP (100)
614 #define SWM_ARG_ID_MOVEDOWN (101)
615 #define SWM_ARG_ID_MOVELEFT (102)
616 #define SWM_ARG_ID_MOVERIGHT (103)
617 #define SWM_ARG_ID_BAR_TOGGLE (110)
618 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
624 TAILQ_ENTRY(quirk) entry;
625 char *class; /* WM_CLASS:class */
626 char *instance; /* WM_CLASS:instance */
627 char *name; /* WM_NAME */
629 regex_t regex_instance;
632 #define SWM_Q_FLOAT (1<<0) /* float this window */
633 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
634 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
635 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
636 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
637 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
638 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
639 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
641 TAILQ_HEAD(quirk_list, quirk);
642 struct quirk_list quirks = TAILQ_HEAD_INITIALIZER(quirks);
645 * Supported EWMH hints should be added to
646 * both the enum and the ewmh array
651 _NET_MOVERESIZE_WINDOW,
652 _NET_WM_ACTION_ABOVE,
653 _NET_WM_ACTION_CLOSE,
654 _NET_WM_ACTION_FULLSCREEN,
656 _NET_WM_ACTION_RESIZE,
657 _NET_WM_ALLOWED_ACTIONS,
661 _NET_WM_STATE_FULLSCREEN,
662 _NET_WM_STATE_HIDDEN,
663 _NET_WM_STATE_MAXIMIZED_HORZ,
664 _NET_WM_STATE_MAXIMIZED_VERT,
665 _NET_WM_STATE_SKIP_PAGER,
666 _NET_WM_STATE_SKIP_TASKBAR,
668 _NET_WM_WINDOW_TYPE_DIALOG,
669 _NET_WM_WINDOW_TYPE_DOCK,
670 _NET_WM_WINDOW_TYPE_NORMAL,
671 _NET_WM_WINDOW_TYPE_SPLASH,
672 _NET_WM_WINDOW_TYPE_TOOLBAR,
673 _NET_WM_WINDOW_TYPE_UTILITY,
674 _SWM_WM_STATE_MANUAL,
681 } ewmh[SWM_EWMH_HINT_MAX] = {
682 /* must be in same order as in the enum */
683 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE},
684 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE},
685 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE},
686 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE},
687 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE},
688 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE},
689 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE},
690 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE},
691 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE},
692 {"_NET_WM_NAME", XCB_ATOM_NONE},
693 {"_NET_WM_STATE", XCB_ATOM_NONE},
694 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE},
695 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE},
696 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE},
697 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE},
698 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE},
699 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE},
700 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE},
701 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE},
702 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE},
703 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE},
704 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE},
705 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE},
706 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE},
707 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE},
708 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE},
715 XC_BOTTOM_LEFT_CORNER,
716 XC_BOTTOM_RIGHT_CORNER,
724 char *name; /* Name used by Xcursor .*/
725 uint8_t cf_char; /* cursorfont index. */
727 } cursors[XC_MAX] = {
728 {"fleur", XC_fleur, XCB_CURSOR_NONE},
729 {"left_ptr", XC_left_ptr, XCB_CURSOR_NONE},
730 {"bottom_left_corner", XC_bottom_left_corner, XCB_CURSOR_NONE},
731 {"bottom_right_corner", XC_bottom_right_corner, XCB_CURSOR_NONE},
732 {"sizing", XC_sizing, XCB_CURSOR_NONE},
733 {"top_left_corner", XC_top_left_corner, XCB_CURSOR_NONE},
734 {"top_right_corner", XC_top_right_corner, XCB_CURSOR_NONE},
737 #define SWM_SPAWN_OPTIONAL 0x1
741 TAILQ_ENTRY(spawn_prog) entry;
747 TAILQ_HEAD(spawn_list, spawn_prog);
748 struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
750 /* user/key callable function IDs */
866 KF_DUMPWINS, /* MUST BE LAST */
874 enum keyfuncid funcid;
877 RB_HEAD(key_tree, key);
879 /* function prototypes */
880 void adjust_font(struct ws_win *);
881 char *argsep(char **);
882 void bar_cleanup(struct swm_region *);
883 void bar_extra_setup(void);
884 void bar_extra_stop(void);
885 int bar_extra_update(void);
886 void bar_fmt(const char *, char *, struct swm_region *, size_t);
887 void bar_fmt_expand(char *, size_t);
889 void bar_print(struct swm_region *, const char *);
890 void bar_print_legacy(struct swm_region *, const char *);
891 void bar_replace(char *, char *, struct swm_region *, size_t);
892 void bar_replace_pad(char *, int *, size_t);
893 char *bar_replace_seq(char *, char *, struct swm_region *, size_t *, size_t);
894 void bar_setup(struct swm_region *);
895 void bar_toggle(struct swm_region *, union arg *);
896 void bar_urgent(char *, size_t);
897 void bar_window_class(char *, size_t, struct swm_region *);
898 void bar_window_class_instance(char *, size_t, struct swm_region *);
899 void bar_window_float(char *, size_t, struct swm_region *);
900 void bar_window_instance(char *, size_t, struct swm_region *);
901 void bar_window_name(char *, size_t, struct swm_region *);
902 void bar_window_state(char *, size_t, struct swm_region *);
903 void bar_workspace_name(char *, size_t, struct swm_region *);
904 void buttonpress(xcb_button_press_event_t *);
905 void check_conn(void);
906 void clear_keys(void);
907 int clear_maximized(struct workspace *);
908 void clientmessage(xcb_client_message_event_t *);
909 void client_msg(struct ws_win *, xcb_atom_t, xcb_timestamp_t);
910 int conf_load(const char *, int);
911 void configurenotify(xcb_configure_notify_event_t *);
912 void configurerequest(xcb_configure_request_event_t *);
913 void config_win(struct ws_win *, xcb_configure_request_event_t *);
914 void constrain_window(struct ws_win *, struct swm_geometry *, int *);
915 int count_win(struct workspace *, int);
916 void cursors_cleanup(void);
917 void cursors_load(void);
918 void custom_region(const char *);
919 void cyclerg(struct swm_region *, union arg *);
920 void cyclews(struct swm_region *, union arg *);
921 void cycle_layout(struct swm_region *, union arg *);
922 void destroynotify(xcb_destroy_notify_event_t *);
923 void dumpwins(struct swm_region *, union arg *);
925 void enternotify(xcb_enter_notify_event_t *);
926 void event_drain(uint8_t);
927 void event_error(xcb_generic_error_t *);
928 void event_handle(xcb_generic_event_t *);
929 void ewmh_autoquirk(struct ws_win *);
930 void ewmh_get_win_state(struct ws_win *);
931 int ewmh_set_win_fullscreen(struct ws_win *, int);
932 void ewmh_update_actions(struct ws_win *);
933 void ewmh_update_win_state(struct ws_win *, xcb_atom_t, long);
934 char *expand_tilde(const char *);
935 void expose(xcb_expose_event_t *);
936 void fake_keypress(struct ws_win *, xcb_keysym_t, uint16_t);
937 struct pid_e *find_pid(pid_t);
938 struct ws_win *find_unmanaged_window(xcb_window_t);
939 struct ws_win *find_window(xcb_window_t);
940 void floating_toggle(struct swm_region *, union arg *);
941 int floating_toggle_win(struct ws_win *);
942 void focus(struct swm_region *, union arg *);
944 void focusin(xcb_focus_in_event_t *);
945 void focusout(xcb_focus_out_event_t *);
947 void focus_flush(void);
948 void focus_region(struct swm_region *);
949 void focusrg(struct swm_region *, union arg *);
950 void focus_win(struct ws_win *);
951 void fontset_init(void);
952 void free_window(struct ws_win *);
953 xcb_atom_t get_atom_from_string(const char *);
955 char *get_atom_name(xcb_atom_t);
957 struct ws_win *get_focus_magic(struct ws_win *);
958 struct ws_win *get_focus_prev(struct ws_win *);
960 char *get_notify_detail_label(uint8_t);
961 char *get_notify_mode_label(uint8_t);
963 struct ws_win *get_pointer_win(xcb_window_t);
964 struct ws_win *get_region_focus(struct swm_region *);
965 int get_region_index(struct swm_region *);
966 xcb_screen_t *get_screen(int);
967 xcb_window_t get_sibling(struct ws_win *, int);
968 int get_screen_count(void);
970 char *get_stack_mode_name(uint8_t);
972 int32_t get_swm_iconic(struct ws_win *);
973 char *get_win_name(xcb_window_t);
974 void get_wm_protocols(struct ws_win *);
975 int get_ws_idx(xcb_window_t);
976 uint32_t getstate(xcb_window_t);
977 void grabbuttons(struct ws_win *);
979 void grab_windows(void);
980 void iconify(struct swm_region *, union arg *);
982 void keypress(xcb_key_press_event_t *);
983 int key_cmp(struct key *, struct key *);
984 void key_insert(unsigned int, KeySym, enum keyfuncid, const char *);
985 struct key *key_lookup(unsigned int, KeySym);
986 void key_remove(struct key *);
987 void key_replace(struct key *, unsigned int, KeySym, enum keyfuncid,
989 void kill_bar_extra_atexit(void);
990 void kill_refs(struct ws_win *);
992 void leavenotify(xcb_leave_notify_event_t *);
994 void load_float_geom(struct ws_win *);
995 struct ws_win *manage_window(xcb_window_t, uint16_t);
996 void map_window(struct ws_win *);
997 void mapnotify(xcb_map_notify_event_t *);
998 void mappingnotify(xcb_mapping_notify_event_t *);
999 void maprequest(xcb_map_request_event_t *);
1000 void maximize_toggle(struct swm_region *, union arg *);
1001 void motionnotify(xcb_motion_notify_event_t *);
1002 void move(struct ws_win *, union arg *);
1003 void move_step(struct swm_region *, union arg *);
1004 uint32_t name_to_pixel(int, const char *);
1005 void name_workspace(struct swm_region *, union arg *);
1006 void new_region(struct swm_screen *, int, int, int, int);
1007 int parsekeys(const char *, unsigned int, unsigned int *, KeySym *);
1008 int parsequirks(const char *, unsigned long *);
1009 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1010 void pressbutton(struct swm_region *, union arg *);
1011 void priorws(struct swm_region *, union arg *);
1013 void print_win_geom(xcb_window_t);
1015 void propertynotify(xcb_property_notify_event_t *);
1016 void quirk_free(struct quirk *);
1017 void quirk_insert(const char *, const char *, const char *,unsigned long);
1018 void quirk_remove(struct quirk *);
1019 void quirk_replace(struct quirk *, const char *, const char *, const char *,
1021 void quit(struct swm_region *, union arg *);
1022 void raise_toggle(struct swm_region *, union arg *);
1023 void region_containment(struct ws_win *, struct swm_region *, int);
1024 struct swm_region *region_under(struct swm_screen *, int, int);
1025 void regionize(struct ws_win *, int, int);
1026 void resize(struct ws_win *, union arg *);
1027 void resize_step(struct swm_region *, union arg *);
1028 void restart(struct swm_region *, union arg *);
1029 struct swm_region *root_to_region(xcb_window_t, int);
1030 void screenchange(xcb_randr_screen_change_notify_event_t *);
1031 void scan_xrandr(int);
1032 void search_do_resp(void);
1033 void search_resp_name_workspace(const char *, size_t);
1034 void search_resp_search_window(const char *);
1035 void search_resp_search_workspace(const char *);
1036 void search_resp_uniconify(const char *, size_t);
1037 void search_win(struct swm_region *, union arg *);
1038 void search_win_cleanup(void);
1039 void search_workspace(struct swm_region *, union arg *);
1040 void send_to_rg(struct swm_region *, union arg *);
1041 void send_to_ws(struct swm_region *, union arg *);
1042 void set_region(struct swm_region *);
1043 int setautorun(const char *, const char *, int);
1044 int setconfbinding(const char *, const char *, int);
1045 int setconfcolor(const char *, const char *, int);
1046 int setconfmodkey(const char *, const char *, int);
1047 int setconfquirk(const char *, const char *, int);
1048 int setconfregion(const char *, const char *, int);
1049 int setconfspawn(const char *, const char *, int);
1050 int setconfvalue(const char *, const char *, int);
1051 void setkeybinding(unsigned int, KeySym, enum keyfuncid, const char *);
1052 int setkeymapping(const char *, const char *, int);
1053 int setlayout(const char *, const char *, int);
1054 void setquirk(const char *, const char *, const char *,unsigned long);
1055 void setscreencolor(const char *, int, int);
1056 void setspawn(const char *, const char *, int);
1057 void setup_ewmh(void);
1058 void setup_globals(void);
1059 void setup_keys(void);
1060 void setup_quirks(void);
1061 void setup_screens(void);
1062 void setup_spawn(void);
1063 void set_child_transient(struct ws_win *, xcb_window_t *);
1064 void set_swm_iconic(struct ws_win *, int);
1065 void set_win_state(struct ws_win *, uint16_t);
1066 void shutdown_cleanup(void);
1068 void socket_setnonblock(int);
1069 void sort_windows(struct ws_win_list *);
1070 void spawn(int, union arg *, int);
1071 void spawn_custom(struct swm_region *, union arg *, const char *);
1072 int spawn_expand(struct swm_region *, union arg *, const char *, char ***);
1073 void spawn_insert(const char *, const char *, int);
1074 struct spawn_prog *spawn_find(const char *);
1075 void spawn_remove(struct spawn_prog *);
1076 void spawn_replace(struct spawn_prog *, const char *, const char *, int);
1077 void spawn_select(struct swm_region *, union arg *, const char *, int *);
1078 void stack_config(struct swm_region *, union arg *);
1079 void stack_floater(struct ws_win *);
1080 void stack_master(struct workspace *, struct swm_geometry *, int, int);
1081 void reorder_window(struct ws_win *, uint32_t);
1082 void store_float_geom(struct ws_win *);
1083 char *strdupsafe(const char *);
1084 void swapwin(struct swm_region *, union arg *);
1085 void switchws(struct swm_region *, union arg *);
1086 void teardown_ewmh(void);
1087 void unescape_selector(char *);
1088 void unfocus_win(struct ws_win *);
1089 void uniconify(struct swm_region *, union arg *);
1090 void unmanage_window(struct ws_win *);
1091 void unmapnotify(xcb_unmap_notify_event_t *);
1092 void unmap_all(void);
1093 void unmap_window(struct ws_win *);
1094 void updatenumlockmask(void);
1095 void update_modkey(unsigned int);
1096 void update_window(struct ws_win *);
1097 void validate_spawns(void);
1098 int validate_win(struct ws_win *);
1099 int validate_ws(struct workspace *);
1100 void version(struct swm_region *, union arg *);
1101 void win_to_ws(struct ws_win *, int, int);
1102 pid_t window_get_pid(xcb_window_t);
1103 void wkill(struct swm_region *, union arg *);
1104 void workaround(void);
1105 void xft_init(struct swm_region *);
1106 void _add_startup_exception(const char *, va_list);
1107 void add_startup_exception(const char *, ...);
1109 RB_PROTOTYPE(key_tree, key, entry, key_cmp);
1110 RB_GENERATE(key_tree, key, entry, key_cmp);
1111 struct key_tree keys;
1116 xcb_font_t cf = XCB_NONE;
1119 for (i = 0; i < LENGTH(cursors); ++i) {
1120 /* try to load Xcursor first. */
1121 cursors[i].cid = XcursorLibraryLoadCursor(display,
1124 /* fallback to cursorfont. */
1125 if (cursors[i].cid == XCB_CURSOR_NONE) {
1126 if (cf == XCB_NONE) {
1127 cf = xcb_generate_id(conn);
1128 xcb_open_font(conn, cf, strlen("cursor"),
1132 cursors[i].cid = xcb_generate_id(conn);
1133 xcb_create_glyph_cursor(conn, cursors[i].cid, cf, cf,
1134 cursors[i].cf_char, cursors[i].cf_char + 1, 0, 0, 0,
1135 0xffff, 0xffff, 0xffff);
1141 xcb_close_font(conn, cf);
1145 cursors_cleanup(void)
1148 for (i = 0; i < LENGTH(cursors); ++i)
1149 xcb_free_cursor(conn, cursors[i].cid);
1153 expand_tilde(const char *s)
1155 struct passwd *ppwd;
1163 errx(1, "expand_tilde: NULL string.");
1166 result = strdup(sc);
1172 if ((max = sysconf(_SC_LOGIN_NAME_MAX)) == -1)
1173 errx(1, "expand_tilde: sysconf");
1175 if ((user = calloc(1, max + 1)) == NULL)
1176 errx(1, "expand_tilde: calloc");
1178 for (i = 0; s[i] != '/' && s[i] != '\0'; ++i)
1183 ppwd = strlen(user) == 0 ? getpwuid(getuid()) : getpwnam(user);
1187 result = strdup(sc);
1189 if (asprintf(&result, "%s%s", ppwd->pw_dir, s) == -1)
1193 errx(1, "expand_tilde: failed to allocate memory.");
1199 parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
1201 unsigned int tmpr, tmpg, tmpb;
1203 if (sscanf(rgb, "rgb:%x/%x/%x", &tmpr, &tmpg, &tmpb) != 3)
1206 *rr = RGB_8_TO_16(tmpr);
1207 *gg = RGB_8_TO_16(tmpg);
1208 *bb = RGB_8_TO_16(tmpb);
1214 get_screen(int screen)
1216 const xcb_setup_t *r;
1217 xcb_screen_iterator_t iter;
1219 if ((r = xcb_get_setup(conn)) == NULL) {
1220 DNPRINTF(SWM_D_MISC, "get_screen: xcb_get_setup\n");
1224 iter = xcb_setup_roots_iterator(r);
1225 for (; iter.rem; --screen, xcb_screen_next(&iter))
1233 get_screen_count(void)
1235 const xcb_setup_t *r;
1237 if ((r = xcb_get_setup(conn)) == NULL) {
1238 DNPRINTF(SWM_D_MISC, "get_screen_count: xcb_get_setup\n");
1242 return xcb_setup_roots_length(r);
1246 get_region_index(struct swm_region *r)
1248 struct swm_region *rr;
1254 TAILQ_FOREACH(rr, &r->s->rl, entry) {
1269 if (focus_mode == SWM_FOCUS_DEFAULT)
1270 event_drain(XCB_ENTER_NOTIFY);
1276 get_atom_from_string(const char *str)
1278 xcb_intern_atom_cookie_t c;
1279 xcb_intern_atom_reply_t *r;
1282 c = xcb_intern_atom(conn, 0, strlen(str), str);
1283 r = xcb_intern_atom_reply(conn, c, NULL);
1291 return (XCB_ATOM_NONE);
1295 get_wm_protocols(struct ws_win *win) {
1297 xcb_icccm_get_wm_protocols_reply_t wpr;
1299 if (xcb_icccm_get_wm_protocols_reply(conn,
1300 xcb_icccm_get_wm_protocols(conn, win->id, a_prot),
1302 for (i = 0; i < (int)wpr.atoms_len; i++) {
1303 if (wpr.atoms[i] == a_takefocus)
1304 win->take_focus = 1;
1305 if (wpr.atoms[i] == a_delete)
1306 win->can_delete = 1;
1308 xcb_icccm_get_wm_protocols_reply_wipe(&wpr);
1313 set_swm_iconic(struct ws_win *win, int newv)
1320 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1321 a_swm_iconic, XCB_ATOM_INTEGER, 32, 1, &v);
1323 xcb_delete_property(conn, win->id, a_swm_iconic);
1327 get_swm_iconic(struct ws_win *win)
1330 xcb_get_property_reply_t *pr = NULL;
1332 pr = xcb_get_property_reply(conn,
1333 xcb_get_property(conn, 0, win->id, a_swm_iconic,
1334 XCB_ATOM_INTEGER, 0, 1), NULL);
1337 if (pr->type != XCB_ATOM_INTEGER || pr->format != 32)
1339 v = *((int32_t *)xcb_get_property_value(pr));
1349 xcb_atom_t sup_list;
1350 int i, j, num_screens;
1352 sup_list = get_atom_from_string("_NET_SUPPORTED");
1354 for (i = 0; i < LENGTH(ewmh); i++)
1355 ewmh[i].atom = get_atom_from_string(ewmh[i].name);
1357 num_screens = get_screen_count();
1358 for (i = 0; i < num_screens; i++) {
1359 /* Support check window will be created by workaround(). */
1361 /* Report supported atoms */
1362 xcb_delete_property(conn, screens[i].root, sup_list);
1363 for (j = 0; j < LENGTH(ewmh); j++)
1364 xcb_change_property(conn, XCB_PROP_MODE_APPEND,
1365 screens[i].root, sup_list, XCB_ATOM_ATOM, 32, 1,
1374 xcb_atom_t sup_check, sup_list;
1376 xcb_get_property_cookie_t pc;
1377 xcb_get_property_reply_t *pr;
1379 sup_check = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
1380 sup_list = get_atom_from_string("_NET_SUPPORTED");
1381 num_screens = get_screen_count();
1383 for (i = 0; i < num_screens; i++) {
1384 /* Get the support check window and destroy it */
1385 pc = xcb_get_property(conn, 0, screens[i].root, sup_check,
1386 XCB_ATOM_WINDOW, 0, 1);
1387 pr = xcb_get_property_reply(conn, pc, NULL);
1390 if (pr->format == sup_check) {
1391 id = *((xcb_window_t *)xcb_get_property_value(pr));
1393 xcb_destroy_window(conn, id);
1394 xcb_delete_property(conn, screens[i].root, sup_check);
1395 xcb_delete_property(conn, screens[i].root, sup_list);
1402 ewmh_autoquirk(struct ws_win *win)
1406 xcb_get_property_cookie_t c;
1407 xcb_get_property_reply_t *r;
1409 c = xcb_get_property(conn, 0, win->id,
1410 ewmh[_NET_WM_WINDOW_TYPE].atom, XCB_ATOM_ATOM, 0, UINT32_MAX);
1411 r = xcb_get_property_reply(conn, c, NULL);
1415 n = xcb_get_property_value_length(r);
1416 type = xcb_get_property_value(r);
1418 for (i = 0; i < n; i++) {
1419 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
1421 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
1422 type[i] == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
1423 type[i] == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
1425 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
1428 if (type[i] == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
1429 type[i] == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
1431 win->quirks = SWM_Q_FLOAT;
1438 #define SWM_EWMH_ACTION_COUNT_MAX (6)
1439 #define EWMH_F_FULLSCREEN (1<<0)
1440 #define EWMH_F_ABOVE (1<<1)
1441 #define EWMH_F_HIDDEN (1<<2)
1442 #define EWMH_F_SKIP_PAGER (1<<3)
1443 #define EWMH_F_SKIP_TASKBAR (1<<4)
1444 #define SWM_F_MANUAL (1<<5)
1447 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
1449 if (win->ws->r == NULL)
1455 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: window: 0x%x, "
1456 "fullscreen %s\n", win->id, YESNO(fs));
1459 if (!win->g_floatvalid)
1460 store_float_geom(win);
1462 win->g = win->ws->r->g;
1465 load_float_geom(win);
1472 ewmh_update_actions(struct ws_win *win)
1474 xcb_atom_t actions[SWM_EWMH_ACTION_COUNT_MAX];
1480 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
1482 if (win->floating) {
1483 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
1484 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
1485 actions[n++] = ewmh[_NET_WM_ACTION_ABOVE].atom;
1488 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
1489 ewmh[_NET_WM_ALLOWED_ACTIONS].atom, XCB_ATOM_ATOM, 32, 1, actions);
1492 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1493 #define _NET_WM_STATE_ADD 1 /* add/set property */
1494 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1497 ewmh_update_win_state(struct ws_win *win, xcb_atom_t state, long action)
1499 unsigned int mask = 0;
1500 unsigned int changed = 0;
1501 unsigned int orig_flags;
1510 name = get_atom_name(state);
1511 DNPRINTF(SWM_D_PROP, "ewmh_update_win_state: window: 0x%x, state: %s, "
1512 "action: %ld\n", win->id, name, action);
1516 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
1517 mask = EWMH_F_FULLSCREEN;
1518 else if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
1519 mask = EWMH_F_ABOVE;
1520 else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
1521 mask = SWM_F_MANUAL;
1522 else if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
1523 mask = EWMH_F_SKIP_PAGER;
1524 else if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
1525 mask = EWMH_F_SKIP_TASKBAR;
1527 orig_flags = win->ewmh_flags;
1530 case _NET_WM_STATE_REMOVE:
1531 win->ewmh_flags &= ~mask;
1533 case _NET_WM_STATE_ADD:
1534 win->ewmh_flags |= mask;
1536 case _NET_WM_STATE_TOGGLE:
1537 win->ewmh_flags ^= mask;
1541 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
1543 if (state == ewmh[_NET_WM_STATE_ABOVE].atom) {
1544 if (changed && !floating_toggle_win(win))
1545 win->ewmh_flags = orig_flags; /* revert */
1546 } else if (state == ewmh[_SWM_WM_STATE_MANUAL].atom) {
1548 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
1549 } else if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom) {
1550 if (changed && !ewmh_set_win_fullscreen(win,
1551 win->ewmh_flags & EWMH_F_FULLSCREEN))
1552 win->ewmh_flags = orig_flags; /* revert */
1555 xcb_delete_property(conn, win->id, ewmh[_NET_WM_STATE].atom);
1557 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
1558 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1559 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1560 &ewmh[_NET_WM_STATE_FULLSCREEN].atom);
1561 else if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
1562 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1563 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1564 &ewmh[_NET_WM_STATE_SKIP_PAGER].atom);
1565 else if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
1566 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1567 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1568 &ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom);
1569 else if (win->ewmh_flags & EWMH_F_ABOVE)
1570 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1571 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1572 &ewmh[_NET_WM_STATE_ABOVE].atom);
1573 else if (win->ewmh_flags & SWM_F_MANUAL)
1574 xcb_change_property(conn, XCB_PROP_MODE_APPEND, win->id,
1575 ewmh[_NET_WM_STATE].atom, XCB_ATOM_ATOM, 32, 1,
1576 &ewmh[_SWM_WM_STATE_MANUAL].atom);
1580 ewmh_get_win_state(struct ws_win *win)
1583 xcb_get_property_cookie_t c;
1584 xcb_get_property_reply_t *r;
1590 win->ewmh_flags = 0;
1592 win->ewmh_flags |= EWMH_F_ABOVE;
1594 win->ewmh_flags |= SWM_F_MANUAL;
1596 c = xcb_get_property(conn, 0, win->id, ewmh[_NET_WM_STATE].atom,
1597 XCB_ATOM_ATOM, 0, UINT32_MAX);
1598 r = xcb_get_property_reply(conn, c, NULL);
1602 states = xcb_get_property_value(r);
1603 n = xcb_get_property_value_length(r);
1605 for (i = 0; i < n; i++)
1606 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
1614 dumpwins(struct swm_region *r, union arg *args)
1618 xcb_get_window_attributes_cookie_t c;
1619 xcb_get_window_attributes_reply_t *wa;
1621 /* suppress unused warning since var is needed */
1624 if (r->ws == NULL) {
1625 warnx("dumpwins: invalid workspace");
1629 warnx("=== managed window list ws %02d ===", r->ws->idx);
1630 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
1631 state = getstate(win->id);
1632 c = xcb_get_window_attributes(conn, win->id);
1633 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1635 warnx("window: 0x%x, map_state: %d, state: %u, "
1636 "transient: 0x%x", win->id, wa->map_state,
1637 state, win->transient);
1640 warnx("window: 0x%x, failed xcb_get_window_attributes",
1644 warnx("===== unmanaged window list =====");
1645 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
1646 state = getstate(win->id);
1647 c = xcb_get_window_attributes(conn, win->id);
1648 wa = xcb_get_window_attributes_reply(conn, c, NULL);
1650 warnx("window: 0x%x, map_state: %d, state: %u, "
1651 "transient: 0x%x", win->id, wa->map_state,
1652 state, win->transient);
1655 warnx("window: 0x%x, failed xcb_get_window_attributes",
1659 warnx("=================================");
1663 dumpwins(struct swm_region *r, union arg *s)
1668 #endif /* SWM_DEBUG */
1673 int saved_errno, status;
1676 saved_errno = errno;
1680 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1685 if (errno != ECHILD)
1686 warn("sighdlr: waitpid");
1687 #endif /* SWM_DEBUG */
1690 if (pid == searchpid)
1694 if (WIFEXITED(status)) {
1695 if (WEXITSTATUS(status) != 0)
1696 warnx("sighdlr: child exit status: %d",
1697 WEXITSTATUS(status));
1699 warnx("sighdlr: child is terminated "
1701 #endif /* SWM_DEBUG */
1715 errno = saved_errno;
1721 struct pid_e *p = NULL;
1723 DNPRINTF(SWM_D_MISC, "find_pid: %d\n", pid);
1728 TAILQ_FOREACH(p, &pidlist, entry) {
1737 name_to_pixel(int sidx, const char *colorname)
1739 uint32_t result = 0;
1740 char cname[32] = "#";
1741 xcb_screen_t *screen;
1742 xcb_colormap_t cmap;
1743 xcb_alloc_color_reply_t *cr;
1744 xcb_alloc_named_color_reply_t *nr;
1745 uint16_t rr, gg, bb;
1747 screen = get_screen(sidx);
1748 cmap = screen->default_colormap;
1750 /* color is in format rgb://rr/gg/bb */
1751 if (strncmp(colorname, "rgb:", 4) == 0) {
1752 if (parse_rgb(colorname, &rr, &gg, &bb) == -1)
1753 warnx("could not parse rgb %s", colorname);
1755 cr = xcb_alloc_color_reply(conn,
1756 xcb_alloc_color(conn, cmap, rr, gg, bb),
1762 warnx("color '%s' not found", colorname);
1765 nr = xcb_alloc_named_color_reply(conn,
1766 xcb_alloc_named_color(conn, cmap, strlen(colorname),
1769 strlcat(cname, colorname + 2, sizeof cname - 1);
1770 nr = xcb_alloc_named_color_reply(conn,
1771 xcb_alloc_named_color(conn, cmap, strlen(cname),
1778 warnx("color '%s' not found", colorname);
1785 setscreencolor(const char *val, int i, int c)
1789 num_screens = get_screen_count();
1790 if (i > 0 && i <= num_screens) {
1791 screens[i - 1].c[c].pixel = name_to_pixel(i - 1, val);
1792 free(screens[i - 1].c[c].name);
1793 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1795 } else if (i == -1) {
1796 for (i = 0; i < num_screens; i++) {
1797 screens[i].c[c].pixel = name_to_pixel(0, val);
1798 free(screens[i].c[c].name);
1799 if ((screens[i].c[c].name = strdup(val)) == NULL)
1803 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1808 fancy_stacker(struct workspace *ws)
1810 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1811 if (ws->cur_layout->l_stack == vertical_stack)
1812 snprintf(ws->stacker, sizeof ws->stacker,
1813 ws->l_state.vertical_flip ? "[%d>%d]" : "[%d|%d]",
1814 ws->l_state.vertical_mwin, ws->l_state.vertical_stacks);
1815 if (ws->cur_layout->l_stack == horizontal_stack)
1816 snprintf(ws->stacker, sizeof ws->stacker,
1817 ws->l_state.horizontal_flip ? "[%dv%d]" : "[%d-%d]",
1818 ws->l_state.horizontal_mwin, ws->l_state.horizontal_stacks);
1822 plain_stacker(struct workspace *ws)
1824 strlcpy(ws->stacker, "[ ]", sizeof ws->stacker);
1825 if (ws->cur_layout->l_stack == vertical_stack)
1826 strlcpy(ws->stacker, ws->l_state.vertical_flip ? "[>]" : "[|]",
1827 sizeof ws->stacker);
1828 if (ws->cur_layout->l_stack == horizontal_stack)
1829 strlcpy(ws->stacker, ws->l_state.horizontal_flip ? "[v]" : "[-]",
1830 sizeof ws->stacker);
1834 custom_region(const char *val)
1836 unsigned int x, y, w, h;
1837 int sidx, num_screens;
1838 xcb_screen_t *screen;
1840 num_screens = get_screen_count();
1841 if (sscanf(val, "screen[%d]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1842 errx(1, "invalid custom region, "
1843 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1844 if (sidx < 1 || sidx > num_screens)
1845 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1849 if ((screen = get_screen(sidx)) == NULL)
1850 errx(1, "ERROR: can't get screen %d.", sidx);
1853 errx(1, "region %ux%u+%u+%u too small", w, h, x, y);
1855 if (x > screen->width_in_pixels ||
1856 y > screen->height_in_pixels ||
1857 w + x > screen->width_in_pixels ||
1858 h + y > screen->height_in_pixels) {
1859 warnx("ignoring region %ux%u+%u+%u - not within screen "
1860 "boundaries (%ux%u)", w, h, x, y,
1861 screen->width_in_pixels, screen->height_in_pixels);
1865 new_region(&screens[sidx], x, y, w, h);
1869 socket_setnonblock(int fd)
1873 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1874 err(1, "fcntl F_GETFL");
1875 flags |= O_NONBLOCK;
1876 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1877 err(1, "fcntl F_SETFL");
1881 bar_print_legacy(struct swm_region *r, const char *s)
1883 xcb_rectangle_t rect;
1888 XRectangle ibox, lbox;
1892 XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
1894 switch (bar_justify) {
1895 case SWM_BAR_JUSTIFY_LEFT:
1898 case SWM_BAR_JUSTIFY_CENTER:
1899 x = (WIDTH(r) - lbox.width) / 2;
1901 case SWM_BAR_JUSTIFY_RIGHT:
1902 x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
1906 if (x < SWM_BAR_OFFSET)
1911 rect.width = WIDTH(r->bar);
1912 rect.height = HEIGHT(r->bar);
1914 /* clear back buffer */
1915 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1916 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1917 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
1919 /* draw back buffer */
1920 gcvd.graphics_exposures = 0;
1921 draw = XCreateGC(display, r->bar->buffer, GCGraphicsExposures, &gcvd);
1922 XSetForeground(display, draw, r->s->c[SWM_S_COLOR_BAR_FONT].pixel);
1923 DRAWSTRING(display, r->bar->buffer, bar_fs, draw,
1924 x, (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
1926 XFreeGC(display, draw);
1929 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1930 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1934 bar_print(struct swm_region *r, const char *s)
1937 xcb_rectangle_t rect;
1945 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len, &info);
1947 switch (bar_justify) {
1948 case SWM_BAR_JUSTIFY_LEFT:
1951 case SWM_BAR_JUSTIFY_CENTER:
1952 x = (WIDTH(r) - info.width) / 2;
1954 case SWM_BAR_JUSTIFY_RIGHT:
1955 x = WIDTH(r) - info.width - SWM_BAR_OFFSET;
1959 if (x < SWM_BAR_OFFSET)
1964 rect.width = WIDTH(r->bar);
1965 rect.height = HEIGHT(r->bar);
1967 /* clear back buffer */
1968 gcv[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
1969 xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
1970 xcb_poly_fill_rectangle(conn, r->bar->buffer, r->s->bar_gc, 1, &rect);
1972 /* draw back buffer */
1973 draw = XftDrawCreate(display, r->bar->buffer,
1974 DefaultVisual(display, r->s->idx),
1975 DefaultColormap(display, r->s->idx));
1977 XftDrawStringUtf8(draw, &bar_font_color, bar_font, x,
1978 (HEIGHT(r->bar) + bar_font->height) / 2 - bar_font->descent,
1981 XftDrawDestroy(draw);
1984 xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
1985 0, 0, WIDTH(r->bar), HEIGHT(r->bar));
1989 bar_extra_stop(void)
1993 bzero(bar_pipe, sizeof bar_pipe);
1996 kill(bar_pid, SIGTERM);
1999 strlcpy(bar_ext, "", sizeof bar_ext);
2004 bar_window_class(char *s, size_t sz, struct swm_region *r)
2006 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2008 if (r->ws->focus->ch.class_name != NULL)
2009 strlcat(s, r->ws->focus->ch.class_name, sz);
2013 bar_window_instance(char *s, size_t sz, struct swm_region *r)
2015 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2017 if (r->ws->focus->ch.instance_name != NULL)
2018 strlcat(s, r->ws->focus->ch.instance_name, sz);
2022 bar_window_class_instance(char *s, size_t sz, struct swm_region *r)
2024 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2027 bar_window_class(s, sz, r);
2028 strlcat(s, ":", sz);
2029 bar_window_instance(s, sz, r);
2033 bar_window_state(char *s, size_t sz, struct swm_region *r)
2035 if (r == NULL || r ->ws == NULL || r->ws->focus == NULL)
2037 if (r->ws->focus->maximized)
2038 strlcat(s, "(m)", sz);
2039 else if (r->ws->focus->floating)
2040 strlcat(s, "(f)", sz);
2044 bar_window_name(char *s, size_t sz, struct swm_region *r)
2048 if (r == NULL || r->ws == NULL || r->ws->focus == NULL)
2051 title = get_win_name(r->ws->focus->id);
2052 strlcat(s, title, sz);
2057 bar_urgent(char *s, size_t sz)
2060 int i, j, num_screens;
2061 int urgent[SWM_WS_MAX];
2063 xcb_get_property_cookie_t c;
2064 xcb_icccm_wm_hints_t hints;
2066 for (i = 0; i < workspace_limit; i++)
2069 num_screens = get_screen_count();
2070 for (i = 0; i < num_screens; i++)
2071 for (j = 0; j < workspace_limit; j++)
2072 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
2073 c = xcb_icccm_get_wm_hints(conn, win->id);
2074 if (xcb_icccm_get_wm_hints_reply(conn, c,
2077 if (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY)
2081 for (i = 0; i < workspace_limit; i++) {
2083 snprintf(b, sizeof b, "%d ", i + 1);
2085 snprintf(b, sizeof b, "- ");
2091 bar_workspace_name(char *s, size_t sz, struct swm_region *r)
2093 if (r == NULL || r->ws == NULL)
2095 if (r->ws->name != NULL)
2096 strlcat(s, r->ws->name, sz);
2099 /* build the default bar format according to the defined enabled options */
2101 bar_fmt(const char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz)
2105 /* if format provided, just copy the buffers */
2106 if (bar_format != NULL) {
2107 strlcpy(fmtnew, fmtexp, sz);
2111 /* reset the output buffer */
2114 strlcat(fmtnew, "+N:+I ", sz);
2116 strlcat(fmtnew, "+S", sz);
2117 strlcat(fmtnew, " ", sz);
2119 /* only show the workspace name if there's actually one */
2120 if (r != NULL && r->ws != NULL && r->ws->name != NULL)
2121 strlcat(fmtnew, "<+D>", sz);
2123 /* If enabled, only show the iconic count if there are iconic wins. */
2124 if (iconic_enabled && r != NULL && r->ws != NULL)
2125 TAILQ_FOREACH(w, &r->ws->winlist, entry)
2127 strlcat(fmtnew, "{+M}", sz);
2131 strlcat(fmtnew, "+3<", sz);
2133 if (clock_enabled) {
2134 strlcat(fmtnew, fmtexp, sz);
2135 strlcat(fmtnew, "+4<", sz);
2138 /* bar_urgent already adds the space before the last asterisk */
2140 strlcat(fmtnew, "* +U*+4<", sz);
2142 if (window_class_enabled) {
2143 strlcat(fmtnew, "+C", sz);
2144 if (!window_instance_enabled)
2145 strlcat(fmtnew, "+4<", sz);
2148 /* checks needed by the colon and floating strlcat(3) calls below */
2149 if (r != NULL && r->ws != NULL && r->ws->focus != NULL) {
2150 if (window_instance_enabled) {
2151 if (window_class_enabled)
2152 strlcat(fmtnew, ":", sz);
2153 strlcat(fmtnew, "+T+4<", sz);
2155 if (window_name_enabled) {
2156 if (r->ws->focus->floating || r->ws->focus->maximized)
2157 strlcat(fmtnew, "+F ", sz);
2158 strlcat(fmtnew, "+64W ", sz);
2162 /* finally add the action script output and the version */
2163 strlcat(fmtnew, "+4<+A+4<+V", sz);
2167 bar_replace_pad(char *tmp, int *limit, size_t sz)
2169 /* special case; no limit given, pad one space, instead */
2170 if (*limit == (int)sz - 1)
2172 snprintf(tmp, sz, "%*s", *limit, " ");
2175 /* replaces the bar format character sequences (like in tmux(1)) */
2177 bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep,
2182 char tmp[SWM_BAR_MAX];
2183 int limit, size, count;
2186 /* reset strlcat(3) buffer */
2189 /* get number, if any */
2192 if (sscanf(fmt, "%d%n", &limit, &size) != 1)
2193 limit = sizeof tmp - 1;
2194 if (limit <= 0 || limit >= (int)sizeof tmp)
2195 limit = sizeof tmp - 1;
2197 /* there is nothing to replace (ie EOL) */
2204 bar_replace_pad(tmp, &limit, sizeof tmp);
2207 snprintf(tmp, sizeof tmp, "%s", bar_ext);
2210 bar_window_class(tmp, sizeof tmp, r);
2213 bar_workspace_name(tmp, sizeof tmp, r);
2216 bar_window_state(tmp, sizeof tmp, r);
2219 snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1);
2223 TAILQ_FOREACH(w, &r->ws->winlist, entry)
2227 snprintf(tmp, sizeof tmp, "%d", count);
2230 snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1);
2233 bar_window_class_instance(tmp, sizeof tmp, r);
2236 snprintf(tmp, sizeof tmp, "%s", r->ws->stacker);
2239 bar_window_instance(tmp, sizeof tmp, r);
2242 bar_urgent(tmp, sizeof tmp);
2245 snprintf(tmp, sizeof tmp, "%s", bar_vertext);
2248 bar_window_name(tmp, sizeof tmp, r);
2251 /* unknown character sequence; copy as-is */
2252 snprintf(tmp, sizeof tmp, "+%c", *fmt);
2258 if ((int)len < limit)
2260 while (limit-- > 0) {
2261 if (*offrep >= sz - 1)
2263 fmtrep[(*offrep)++] = *ptr++;
2271 bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz)
2276 while (*fmt != '\0') {
2278 /* skip ordinary characters */
2281 fmtrep[off++] = *fmt++;
2285 /* character sequence found; replace it */
2286 fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz);
2295 bar_fmt_expand(char *fmtexp, size_t sz)
2302 /* start by grabbing the current time and date */
2304 localtime_r(&tmt, &tm);
2306 /* figure out what to expand */
2307 if (bar_format != NULL)
2309 else if (bar_format == NULL && clock_enabled)
2311 /* if nothing to expand bail out */
2317 /* copy as-is, just in case the format shouldn't be expanded below */
2318 strlcpy(fmtexp, fmt, sz);
2319 /* finally pass the string through strftime(3) */
2320 #ifndef SWM_DENY_CLOCK_FORMAT
2321 if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0)
2322 warnx("format too long");
2327 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2331 char fmtexp[SWM_BAR_MAX], fmtnew[SWM_BAR_MAX];
2332 char fmtrep[SWM_BAR_MAX];
2334 struct swm_region *r;
2336 /* expand the format by first passing it through strftime(3) */
2337 bar_fmt_expand(fmtexp, sizeof fmtexp);
2339 num_screens = get_screen_count();
2340 for (i = 0; i < num_screens; i++) {
2341 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2345 if (bar_enabled && r->ws->bar_enabled)
2346 xcb_map_window(conn, r->bar->id);
2348 xcb_unmap_window(conn, r->bar->id);
2352 if (startup_exception)
2353 snprintf(fmtrep, sizeof fmtrep, "total "
2354 "exceptions: %d, first exception: %s",
2358 bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew);
2359 bar_replace(fmtnew, fmtrep, r, sizeof fmtrep);
2361 if (bar_font_legacy)
2362 bar_print_legacy(r, fmtrep);
2364 bar_print(r, fmtrep);
2370 * Reads external script output; call when stdin is readable.
2371 * Returns 1 if bar_ext was updated; otherwise 0.
2374 bar_extra_update(void)
2377 char b[SWM_BAR_MAX];
2383 while (fgets(b, sizeof(b), stdin) != NULL) {
2386 if (b[len - 1] == '\n') {
2387 /* Remove newline. */
2390 /* "Clear" bar_ext. */
2393 /* Flush buffered output. */
2394 strlcpy(bar_ext, bar_ext_buf, sizeof(bar_ext));
2395 bar_ext_buf[0] = '\0';
2397 /* Append new output to bar. */
2398 strlcat(bar_ext, b, sizeof(bar_ext));
2402 /* Buffer output. */
2403 strlcat(bar_ext_buf, b, sizeof(bar_ext_buf));
2408 if (errno != EAGAIN) {
2409 warn("bar_action failed");
2418 bar_toggle(struct swm_region *r, union arg *args)
2420 struct swm_region *tmpr;
2423 /* suppress unused warnings since vars are needed */
2427 DNPRINTF(SWM_D_BAR, "bar_toggle\n");
2430 case SWM_ARG_ID_BAR_TOGGLE_WS:
2431 /* Only change if master switch is enabled. */
2433 r->ws->bar_enabled = !r->ws->bar_enabled;
2435 bar_enabled = r->ws->bar_enabled = 1;
2437 case SWM_ARG_ID_BAR_TOGGLE:
2438 bar_enabled = !bar_enabled;
2442 /* update bars as necessary */
2443 num_screens = get_screen_count();
2444 for (i = 0; i < num_screens; i++)
2445 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
2447 if (bar_enabled && tmpr->ws->bar_enabled)
2448 xcb_map_window(conn, tmpr->bar->id);
2450 xcb_unmap_window(conn, tmpr->bar->id);
2455 /* must be after stack */
2462 bar_extra_setup(void)
2464 /* do this here because the conf file is in memory */
2465 if (!bar_extra && bar_argv[0]) {
2466 /* launch external status app */
2468 if (pipe(bar_pipe) == -1)
2469 err(1, "pipe error");
2470 socket_setnonblock(bar_pipe[0]);
2471 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
2473 /* Set stdin to read from the pipe. */
2474 if (dup2(bar_pipe[0], STDIN_FILENO) == -1)
2477 /* Set stdout to write to the pipe. */
2478 if (dup2(bar_pipe[1], STDOUT_FILENO) == -1)
2481 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
2482 err(1, "could not disable SIGPIPE");
2483 switch (bar_pid = fork()) {
2485 err(1, "cannot fork");
2489 execvp(bar_argv[0], bar_argv);
2490 err(1, "%s external app failed", bar_argv[0]);
2492 default: /* parent */
2497 atexit(kill_bar_extra_atexit);
2502 kill_bar_extra_atexit(void)
2505 kill(bar_pid, SIGTERM);
2513 while ((s = index(s, '-'))) {
2518 return (count == 14);
2524 char *default_string;
2525 char **missing_charsets;
2526 int num_missing_charsets = 0;
2530 XFreeFontSet(display, bar_fs);
2534 DNPRINTF(SWM_D_INIT, "fontset_init: loading bar_fonts: %s\n", bar_fonts);
2536 bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
2537 &num_missing_charsets, &default_string);
2539 if (num_missing_charsets > 0) {
2540 warnx("Unable to load charset(s):");
2542 for (i = 0; i < num_missing_charsets; ++i)
2543 warnx("%s", missing_charsets[i]);
2545 XFreeStringList(missing_charsets);
2547 if (strcmp(default_string, ""))
2548 warnx("Glyphs from those sets will be replaced "
2549 "by '%s'.", default_string);
2551 warnx("Glyphs from those sets won't be drawn.");
2555 errx(1, "Error creating font set structure.");
2557 bar_fs_extents = XExtentsOfFontSet(bar_fs);
2559 bar_height = bar_fs_extents->max_logical_extent.height +
2560 2 * bar_border_width;
2567 xft_init(struct swm_region *r)
2569 char *font, *d, *search;
2572 if (bar_font == NULL) {
2573 if ((d = strdup(bar_fonts)) == NULL)
2574 errx(1, "insufficient memory.");
2576 while ((font = strsep(&search, ",")) != NULL) {
2580 DNPRINTF(SWM_D_INIT, "xft_init: try font %s\n", font);
2583 bar_font = XftFontOpenXlfd(display, r->s->idx,
2586 bar_font = XftFontOpenName(display, r->s->idx,
2590 if (bar_font == NULL) {
2591 warnx("unable to load font %s", font);
2594 DNPRINTF(SWM_D_INIT, "successfully opened "
2602 if (bar_font == NULL)
2603 errx(1, "unable to open a font");
2605 PIXEL_TO_XRENDERCOLOR(r->s->c[SWM_S_COLOR_BAR_FONT].pixel, color);
2607 if (!XftColorAllocValue(display, DefaultVisual(display, r->s->idx),
2608 DefaultColormap(display, r->s->idx), &color, &bar_font_color))
2609 warn("Xft error: unable to allocate color.");
2611 bar_height = bar_font->height + 2 * bar_border_width;
2618 bar_setup(struct swm_region *r)
2620 xcb_screen_t *screen;
2623 DNPRINTF(SWM_D_BAR, "bar_setup: screen %d.\n",
2626 if ((screen = get_screen(r->s->idx)) == NULL)
2627 errx(1, "ERROR: can't get screen %d.", r->s->idx);
2632 if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
2633 err(1, "bar_setup: calloc: failed to allocate memory.");
2635 if (bar_font_legacy)
2641 Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
2642 WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
2643 HEIGHT(r->bar) = bar_height - 2 * bar_border_width;
2645 /* Assume region is unfocused when we create the bar. */
2646 r->bar->id = xcb_generate_id(conn);
2647 wa[0] = r->s->c[SWM_S_COLOR_BAR].pixel;
2648 wa[1] = r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel;
2649 wa[2] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_POINTER_MOTION |
2650 XCB_EVENT_MASK_POINTER_MOTION_HINT;
2652 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->bar->id, r->s->root,
2653 X(r->bar), Y(r->bar), WIDTH(r->bar), HEIGHT(r->bar),
2654 bar_border_width, XCB_WINDOW_CLASS_INPUT_OUTPUT,
2655 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
2656 | XCB_CW_EVENT_MASK, wa);
2658 r->bar->buffer = xcb_generate_id(conn);
2659 xcb_create_pixmap(conn, screen->root_depth, r->bar->buffer, r->bar->id,
2660 WIDTH(r->bar), HEIGHT(r->bar));
2663 xcb_randr_select_input(conn, r->bar->id,
2664 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
2667 xcb_map_window(conn, r->bar->id);
2669 DNPRINTF(SWM_D_BAR, "bar_setup: window: 0x%x, (x,y) w x h: (%d,%d) "
2670 "%d x %d\n", WINID(r->bar), X(r->bar), Y(r->bar), WIDTH(r->bar),
2677 bar_cleanup(struct swm_region *r)
2681 xcb_destroy_window(conn, r->bar->id);
2682 xcb_free_pixmap(conn, r->bar->buffer);
2688 set_win_state(struct ws_win *win, uint16_t state)
2690 uint16_t data[2] = { state, XCB_ATOM_NONE };
2692 DNPRINTF(SWM_D_EVENT, "set_win_state: window: 0x%x, state: %u\n",
2698 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id, a_state,
2699 a_state, 32, 2, data);
2703 getstate(xcb_window_t w)
2705 uint32_t result = 0;
2706 xcb_get_property_cookie_t c;
2707 xcb_get_property_reply_t *r;
2709 c = xcb_get_property(conn, 0, w, a_state, a_state, 0L, 2L);
2710 r = xcb_get_property_reply(conn, c, NULL);
2712 if (r->type == a_state && r->format == 32 && r->length == 2)
2713 result = *((uint32_t *)xcb_get_property_value(r));
2717 DNPRINTF(SWM_D_MISC, "getstate property: win 0x%x state %u\n", w,
2723 version(struct swm_region *r, union arg *args)
2725 /* suppress unused warnings since vars are needed */
2729 bar_version = !bar_version;
2731 snprintf(bar_vertext, sizeof bar_vertext,
2732 "Version: %s Build: %s", SPECTRWM_VERSION, buildstr);
2734 strlcpy(bar_vertext, "", sizeof bar_vertext);
2741 client_msg(struct ws_win *win, xcb_atom_t a, xcb_timestamp_t t)
2743 xcb_client_message_event_t ev;
2751 name = get_atom_name(a);
2752 DNPRINTF(SWM_D_EVENT, "client_msg: window: 0x%x, atom: %s(%u), "
2754 win->id, name, a, t);
2758 bzero(&ev, sizeof ev);
2759 ev.response_type = XCB_CLIENT_MESSAGE;
2760 ev.window = win->id;
2763 ev.data.data32[0] = a;
2764 ev.data.data32[1] = t;
2766 xcb_send_event(conn, 0, win->id,
2767 XCB_EVENT_MASK_NO_EVENT, (const char *)&ev);
2770 /* synthetic response to a ConfigureRequest when not making a change */
2772 config_win(struct ws_win *win, xcb_configure_request_event_t *ev)
2774 xcb_configure_notify_event_t ce;
2779 /* send notification of unchanged state. */
2780 bzero(&ce, sizeof(ce));
2781 ce.response_type = XCB_CONFIGURE_NOTIFY;
2784 ce.width = WIDTH(win);
2785 ce.height = HEIGHT(win);
2786 ce.override_redirect = 0;
2791 ce.window = win->id;
2792 ce.border_width = BORDER(win);
2793 ce.above_sibling = XCB_WINDOW_NONE;
2796 ce.event = ev->window;
2797 ce.window = ev->window;
2799 /* make response appear more WM_SIZE_HINTS-compliant */
2800 if (win->sh.flags) {
2801 DNPRINTF(SWM_D_MISC, "config_win: hints: window: 0x%x,"
2802 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2803 "%d x %d\n", win->id, win->sh.flags, SH_MIN_W(win),
2804 SH_MIN_H(win), SH_MAX_W(win), SH_MAX_H(win),
2805 SH_INC_W(win), SH_INC_H(win));
2810 /* the hint may be set... to 0! */
2811 if (SH_MIN_W(win) > 0 && ce.width < SH_MIN_W(win))
2812 ce.width = SH_MIN_W(win);
2813 if (SH_MIN_H(win) > 0 && ce.height < SH_MIN_H(win))
2814 ce.height = SH_MIN_H(win);
2819 /* may also be advertized as 0 */
2820 if (SH_MAX_W(win) > 0 && ce.width > SH_MAX_W(win))
2821 ce.width = SH_MAX_W(win);
2822 if (SH_MAX_H(win) > 0 && ce.height > SH_MAX_H(win))
2823 ce.height = SH_MAX_H(win);
2826 /* resize increment. */
2828 if (SH_INC_W(win) > 1 && ce.width > SH_INC_W(win))
2829 ce.width -= (ce.width - SH_MIN_W(win)) %
2831 if (SH_INC_H(win) > 1 && ce.height > SH_INC_H(win))
2832 ce.height -= (ce.height - SH_MIN_H(win)) %
2836 /* adjust x and y for requested border_width. */
2837 ce.x += BORDER(win) - ev->border_width;
2838 ce.y += BORDER(win) - ev->border_width;
2839 ce.border_width = ev->border_width;
2840 ce.above_sibling = ev->sibling;
2843 DNPRINTF(SWM_D_MISC, "config_win: ewmh: %s, window: 0x%x, (x,y) w x h: "
2844 "(%d,%d) %d x %d, border: %d\n", YESNO(ev == NULL), win->id, ce.x,
2845 ce.y, ce.width, ce.height, ce.border_width);
2847 xcb_send_event(conn, 0, win->id, XCB_EVENT_MASK_STRUCTURE_NOTIFY,
2852 count_win(struct workspace *ws, int count_transient)
2857 TAILQ_FOREACH(win, &ws->winlist, entry) {
2858 if (!count_transient && win->floating)
2860 if (!count_transient && win->transient)
2866 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
2872 quit(struct swm_region *r, union arg *args)
2874 /* suppress unused warnings since vars are needed */
2878 DNPRINTF(SWM_D_MISC, "quit\n");
2883 reorder_window(struct ws_win *win, uint32_t mode)
2885 struct ws_win *w = win;
2886 struct swm_region *r;
2887 struct workspace *ws;
2890 if (win == NULL || (r = win->ws->r) == NULL)
2894 DNPRINTF(SWM_D_EVENT, "reorder_window: win 0x%x, mode: %d\n",
2899 /* Position above top-most sibling. */
2900 TAILQ_FOREACH_REVERSE(w, &ws->winlist, ws_win_list, entry) {
2901 if (w == win || w->iconic)
2903 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) !=
2904 (w->ewmh_flags & EWMH_F_FULLSCREEN))
2906 if (!win->maximized && w->maximized)
2908 if (win->maximized) {
2913 if (win->floating == w->floating)
2917 case SWM_STACK_ABOVE:
2918 /* Stack above win directly prior. */
2919 while ((w = TAILQ_PREV(w, ws_win_list, entry)) != NULL) {
2920 if (w == win || w->iconic)
2922 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) !=
2923 (w->ewmh_flags & EWMH_F_FULLSCREEN))
2925 if (win->maximized != w->maximized)
2927 if (win->floating == w->floating)
2931 case SWM_STACK_BELOW:
2932 /* Stack above win directly prior. */
2933 while ((w = TAILQ_NEXT(w, entry)) != NULL) {
2934 if (w == win || w->iconic)
2936 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) !=
2937 (w->ewmh_flags & EWMH_F_FULLSCREEN))
2939 if (win->maximized != w->maximized)
2941 if (win->floating == w->floating)
2945 case SWM_STACK_BOTTOM:
2946 /* Position above top-most sibling. */
2947 TAILQ_FOREACH(w, &ws->winlist, entry) {
2948 if (w == win || w->iconic)
2950 if ((win->ewmh_flags & EWMH_F_FULLSCREEN) !=
2951 (w->ewmh_flags & EWMH_F_FULLSCREEN))
2953 if (win->maximized != w->maximized)
2955 if (win->floating == w->floating)
2962 if (win->floating || (win->ewmh_flags & EWMH_F_FULLSCREEN)
2964 val[0] = r->bar->id;
2970 DNPRINTF(SWM_D_EVENT, "reorder_window: sibling: 0x%x\n", val[0]);
2972 val[1] = XCB_STACK_MODE_ABOVE;
2974 xcb_configure_window(conn, win->id, XCB_CONFIG_WINDOW_STACK_MODE |
2975 XCB_CONFIG_WINDOW_SIBLING, val);
2979 map_window(struct ws_win *win)
2984 DNPRINTF(SWM_D_EVENT, "map_window: win 0x%x, mapped: %s\n",
2985 win->id, YESNO(win->mapped));
2990 xcb_map_window(conn, win->id);
2991 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
2996 unmap_window(struct ws_win *win)
3001 DNPRINTF(SWM_D_EVENT, "unmap_window: win 0x%x, mapped: %s\n", win->id,
3002 YESNO(win->mapped));
3007 xcb_unmap_window(conn, win->id);
3008 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
3016 int i, j, num_screens;
3018 num_screens = get_screen_count();
3019 for (i = 0; i < num_screens; i++)
3020 for (j = 0; j < workspace_limit; j++)
3021 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3026 fake_keypress(struct ws_win *win, xcb_keysym_t keysym, uint16_t modifiers)
3028 xcb_key_press_event_t event;
3029 xcb_keycode_t *keycode;
3034 keycode = xcb_key_symbols_get_keycode(syms, keysym);
3036 DNPRINTF(SWM_D_MISC, "fake_keypress: win 0x%x keycode %u\n",
3039 bzero(&event, sizeof(event));
3040 event.event = win->id;
3041 event.root = win->s->root;
3042 event.child = XCB_WINDOW_NONE;
3043 event.time = XCB_CURRENT_TIME;
3044 event.event_x = X(win);
3045 event.event_y = Y(win);
3048 event.same_screen = 1;
3049 event.detail = *keycode;
3050 event.state = modifiers;
3052 event.response_type = XCB_KEY_PRESS;
3053 xcb_send_event(conn, 1, win->id,
3054 XCB_EVENT_MASK_KEY_PRESS, (const char *)&event);
3056 event.response_type = XCB_KEY_RELEASE;
3057 xcb_send_event(conn, 1, win->id,
3058 XCB_EVENT_MASK_KEY_RELEASE, (const char *)&event);
3064 restart(struct swm_region *r, union arg *args)
3066 /* suppress unused warning since var is needed */
3070 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
3074 execvp(start_argv[0], start_argv);
3075 warn("execvp failed");
3080 get_pointer_win(xcb_window_t root)
3082 struct ws_win *win = NULL;
3083 xcb_query_pointer_reply_t *r;
3085 DNPRINTF(SWM_D_EVENT, "get_pointer_win: root: 0x%x.\n", root);
3087 r = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
3089 win = find_window(r->child);
3091 DNPRINTF(SWM_D_EVENT, "get_pointer_win: 0x%x.\n",
3094 DNPRINTF(SWM_D_EVENT, "get_pointer_win: none.\n");
3103 root_to_region(xcb_window_t root, int check)
3106 struct swm_region *r = NULL;
3108 xcb_query_pointer_reply_t *qpr;
3109 xcb_get_input_focus_reply_t *gifr;
3111 DNPRINTF(SWM_D_MISC, "root_to_region: window: 0x%x\n", root);
3113 num_screens = get_screen_count();
3114 for (i = 0; i < num_screens; i++)
3115 if (screens[i].root == root)
3118 if (check & SWM_CK_REGION)
3119 r = screens[i].r_focus;
3121 if (r == NULL && check & SWM_CK_FOCUS) {
3122 /* Try to find an actively focused window */
3123 gifr = xcb_get_input_focus_reply(conn,
3124 xcb_get_input_focus(conn), NULL);
3126 cfw = find_window(gifr->focus);
3127 if (cfw && cfw->ws->r)
3134 if (r == NULL && check & SWM_CK_POINTER) {
3135 /* No region with an active focus; try to use pointer. */
3136 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn,
3137 screens[i].root), NULL);
3139 DNPRINTF(SWM_D_MISC, "root_to_region: pointer: "
3140 "(%d,%d)\n", qpr->root_x, qpr->root_y);
3141 TAILQ_FOREACH(r, &screens[i].rl, entry)
3142 if (X(r) <= qpr->root_x &&
3143 qpr->root_x < MAX_X(r) &&
3144 Y(r) <= qpr->root_y &&
3145 qpr->root_y < MAX_Y(r))
3152 if (r == NULL && check & SWM_CK_FALLBACK)
3153 r = TAILQ_FIRST(&screens[i].rl);
3155 DNPRINTF(SWM_D_MISC, "root_to_region: idx: %d\n", get_region_index(r));
3161 find_unmanaged_window(xcb_window_t id)
3164 int i, j, num_screens;
3166 num_screens = get_screen_count();
3167 for (i = 0; i < num_screens; i++)
3168 for (j = 0; j < workspace_limit; j++)
3169 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
3177 find_window(xcb_window_t id)
3180 int i, j, num_screens;
3181 xcb_query_tree_reply_t *r;
3183 num_screens = get_screen_count();
3184 for (i = 0; i < num_screens; i++)
3185 for (j = 0; j < workspace_limit; j++)
3186 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3190 r = xcb_query_tree_reply(conn, xcb_query_tree(conn, id), NULL);
3194 /* if we were looking for the parent return that window instead */
3195 if (r->parent == 0 || r->root == r->parent) {
3200 /* look for parent */
3201 for (i = 0; i < num_screens; i++)
3202 for (j = 0; j < workspace_limit; j++)
3203 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
3204 if (r->parent == win->id) {
3214 spawn(int ws_idx, union arg *args, int close_fd)
3219 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
3221 close(xcb_get_file_descriptor(conn));
3223 setenv("LD_PRELOAD", SWM_LIB, 1);
3225 if (asprintf(&ret, "%d", ws_idx) == -1) {
3226 warn("spawn: asprintf SWM_WS");
3229 setenv("_SWM_WS", ret, 1);
3233 if (asprintf(&ret, "%d", getpid()) == -1) {
3234 warn("spawn: asprintf _SWM_PID");
3237 setenv("_SWM_PID", ret, 1);
3241 if (setsid() == -1) {
3242 warn("spawn: setsid");
3248 * close stdin and stdout to prevent interaction between apps
3249 * and the baraction script
3250 * leave stderr open to record errors
3252 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
3253 warn("spawn: open");
3256 dup2(fd, STDIN_FILENO);
3257 dup2(fd, STDOUT_FILENO);
3262 execvp(args->argv[0], args->argv);
3264 warn("spawn: execvp");
3269 kill_refs(struct ws_win *win)
3271 int i, x, num_screens;
3272 struct swm_region *r;
3273 struct workspace *ws;
3278 num_screens = get_screen_count();
3279 for (i = 0; i < num_screens; i++)
3280 TAILQ_FOREACH(r, &screens[i].rl, entry)
3281 for (x = 0; x < workspace_limit; x++) {
3283 if (win == ws->focus)
3285 if (win == ws->focus_prev)
3286 ws->focus_prev = NULL;
3291 validate_win(struct ws_win *testwin)
3294 struct workspace *ws;
3295 struct swm_region *r;
3296 int i, x, num_screens;
3298 if (testwin == NULL)
3301 num_screens = get_screen_count();
3302 for (i = 0; i < num_screens; i++)
3303 TAILQ_FOREACH(r, &screens[i].rl, entry)
3304 for (x = 0; x < workspace_limit; x++) {
3306 TAILQ_FOREACH(win, &ws->winlist, entry)
3314 validate_ws(struct workspace *testws)
3316 struct swm_region *r;
3317 struct workspace *ws;
3318 int i, x, num_screens;
3320 /* validate all ws */
3321 num_screens = get_screen_count();
3322 for (i = 0; i < num_screens; i++)
3323 TAILQ_FOREACH(r, &screens[i].rl, entry)
3324 for (x = 0; x < workspace_limit; x++) {
3333 unfocus_win(struct ws_win *win)
3335 xcb_window_t none = XCB_WINDOW_NONE;
3337 DNPRINTF(SWM_D_FOCUS, "unfocus_win: window: 0x%x\n", WINID(win));
3341 if (win->ws == NULL) {
3342 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL ws.\n");
3346 if (validate_ws(win->ws)) {
3347 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid ws.\n");
3351 if (win->ws->r == NULL) {
3352 DNPRINTF(SWM_D_FOCUS, "unfocus_win: NULL region.\n");
3356 if (validate_win(win)) {
3357 DNPRINTF(SWM_D_FOCUS, "unfocus_win: invalid win.\n");
3362 if (win->ws->focus == win) {
3363 if (tile_gap < 0 && !win->floating && !win->maximized)
3364 reorder_window(win, SWM_STACK_ABOVE);
3365 win->ws->focus = NULL;
3366 win->ws->focus_prev = win;
3369 if (validate_win(win->ws->focus)) {
3370 kill_refs(win->ws->focus);
3371 win->ws->focus = NULL;
3374 if (validate_win(win->ws->focus_prev)) {
3375 kill_refs(win->ws->focus_prev);
3376 win->ws->focus_prev = NULL;
3379 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3380 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
3382 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3383 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
3385 DNPRINTF(SWM_D_FOCUS, "unfocus_win: done.\n");
3389 focus_win(struct ws_win *win)
3391 struct ws_win *cfw = NULL, *parent = NULL, *w;
3392 struct workspace *ws;
3393 xcb_get_input_focus_reply_t *r;
3395 DNPRINTF(SWM_D_FOCUS, "focus_win: window: 0x%x\n", WINID(win));
3400 if (win->ws == NULL)
3408 if (validate_ws(ws))
3411 if (validate_win(win)) {
3416 r = xcb_get_input_focus_reply(conn, xcb_get_input_focus(conn), NULL);
3418 cfw = find_window(r->focus);
3424 if (ws->focus != win) {
3425 if (ws->focus && ws->focus != cfw)
3426 unfocus_win(ws->focus);
3430 /* If this window directs focus to a child window, then clear. */
3431 if (win->focus_child)
3432 win->focus_child = NULL;
3434 /* If transient, adjust parent's focus child for focus_magic. */
3435 if (win->transient) {
3436 parent = find_window(win->transient);
3437 if (parent && parent->focus_child != win)
3438 parent->focus_child = win;
3441 if (cfw != win && ws->r != NULL) {
3442 /* Set input focus if no input hint, or indicated by hint. */
3443 if (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
3444 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
3446 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3447 win->id, last_event_time);
3449 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
3450 ws->r->id, XCB_CURRENT_TIME);
3452 /* Tell app it can adjust focus to a specific window. */
3453 if (win->take_focus) {
3454 /* java is special; always tell parent */
3455 if (win->transient && win->java)
3456 client_msg(parent, a_takefocus,
3459 client_msg(win, a_takefocus, last_event_time);
3462 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3463 &ws->r->s->c[SWM_S_COLOR_FOCUS].pixel);
3465 if (ws->cur_layout->flags & SWM_L_MAPONFOCUS ||
3467 /* If a parent exists, map it first. */
3469 reorder_window(parent, SWM_STACK_TOP);
3472 /* Map siblings next. */
3473 TAILQ_FOREACH(w, &ws->winlist, entry)
3474 if (w != win && !w->iconic &&
3475 w->transient == parent->id) {
3476 reorder_window(w, SWM_STACK_ABOVE);
3481 /* Map focused window. */
3482 reorder_window(win, SWM_STACK_TOP);
3485 /* Finally, map children of focus window. */
3486 TAILQ_FOREACH(w, &ws->winlist, entry)
3487 if (w->transient == win->id && !w->iconic) {
3488 reorder_window(w, SWM_STACK_ABOVE);
3491 } else if (tile_gap < 0 && !win->floating) {
3493 * Windows overlap in the layout.
3494 * Raise focused win above all tiled wins.
3496 reorder_window(win, SWM_STACK_TOP);
3502 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3503 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3510 DNPRINTF(SWM_D_FOCUS, "focus_win: done.\n");
3513 /* If a child window should have focus instead, return it. */
3515 get_focus_magic(struct ws_win *win)
3517 struct ws_win *parent = NULL;
3518 struct ws_win *child = NULL;
3520 DNPRINTF(SWM_D_FOCUS, "get_focus_magic: window: 0x%x\n", WINID(win));
3524 if (win->transient) {
3525 parent = find_window(win->transient);
3527 /* If parent prefers focus elsewhere, then try to do so. */
3528 if (parent && (child = parent->focus_child)) {
3529 if (validate_win(child) == 0 && child->mapped)
3532 parent->focus_child = NULL;
3536 /* If this window prefers focus elsewhere, then try to do so. */
3537 if ((child = win->focus_child)) {
3538 if (validate_win(child) == 0 && child->mapped)
3541 win->focus_child = NULL;
3548 event_drain(uint8_t rt)
3550 xcb_generic_event_t *evt;
3552 /* ensure all pending requests have been processed before filtering. */
3554 while ((evt = xcb_poll_for_event(conn))) {
3555 if (XCB_EVENT_RESPONSE_TYPE(evt) != rt)
3563 set_region(struct swm_region *r)
3565 struct swm_region *rf;
3571 /* Unfocus old region bar. */
3576 xcb_change_window_attributes(conn, rf->bar->id,
3577 XCB_CW_BORDER_PIXEL,
3578 &r->s->c[SWM_S_COLOR_BAR_BORDER_UNFOCUS].pixel);
3581 /* Set region bar border to focus_color. */
3582 xcb_change_window_attributes(conn, r->bar->id,
3583 XCB_CW_BORDER_PIXEL, &r->s->c[SWM_S_COLOR_BAR_BORDER].pixel);
3589 focus_region(struct swm_region *r)
3592 struct swm_region *old_r;
3597 old_r = r->s->r_focus;
3600 nfw = get_region_focus(r);
3604 /* New region is empty; need to manually unfocus win. */
3606 unfocus_win(old_r->ws->focus);
3608 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
3611 /* Clear bar since empty. */
3617 switchws(struct swm_region *r, union arg *args)
3619 struct swm_region *this_r, *other_r;
3621 struct workspace *new_ws, *old_ws;
3622 xcb_window_t none = XCB_WINDOW_NONE;
3623 int wsid = args->id, unmap_old = 0;
3628 if (wsid >= workspace_limit)
3632 old_ws = this_r->ws;
3633 new_ws = &this_r->s->ws[wsid];
3635 DNPRINTF(SWM_D_WS, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3636 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), old_ws->idx, wsid);
3638 if (new_ws == NULL || old_ws == NULL)
3640 if (new_ws == old_ws)
3643 if ((win = old_ws->focus) != NULL) {
3644 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL,
3645 &win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].pixel);
3647 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->s->root,
3648 ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1,
3652 other_r = new_ws->r;
3653 if (other_r == NULL) {
3654 /* the other workspace is hidden, hide this one */
3658 /* the other ws is visible in another region, exchange them */
3659 other_r->ws_prior = new_ws;
3660 other_r->ws = old_ws;
3661 old_ws->r = other_r;
3663 this_r->ws_prior = old_ws;
3664 this_r->ws = new_ws;
3667 /* Set focus_pending before stacking, if needed. */
3668 if (focus_mode != SWM_FOCUS_FOLLOW && (!new_ws->focus_pending ||
3669 validate_win(new_ws->focus_pending))) {
3670 new_ws->focus_pending = get_region_focus(new_ws->r);
3671 new_ws->focus = new_ws->focus_prev;
3672 new_ws->focus_prev = NULL;
3675 new_ws->state = SWM_WS_STATE_MAPPING;
3678 /* unmap old windows */
3680 TAILQ_FOREACH(win, &old_ws->winlist, entry)
3682 old_ws->state = SWM_WS_STATE_HIDDEN;
3685 /* if workspaces were swapped, then don't wait to set focus */
3686 if (old_ws->r && focus_mode != SWM_FOCUS_FOLLOW) {
3687 if (new_ws->focus_pending) {
3688 focus_win(new_ws->focus_pending);
3689 new_ws->focus_pending = NULL;
3693 /* Clear bar and set focus on region input win if new ws is empty. */
3694 if (new_ws->focus_pending == NULL && new_ws->focus == NULL) {
3695 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT, r->id,
3701 new_ws->state = SWM_WS_STATE_MAPPED;
3703 DNPRINTF(SWM_D_WS, "switchws: done.\n");
3707 cyclews(struct swm_region *r, union arg *args)
3710 struct swm_screen *s = r->s;
3714 DNPRINTF(SWM_D_WS, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3715 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3721 case SWM_ARG_ID_CYCLEWS_MOVE_UP:
3724 case SWM_ARG_ID_CYCLEWS_UP_ALL:
3727 case SWM_ARG_ID_CYCLEWS_UP:
3728 a.id = (a.id < workspace_limit - 1) ? a.id + 1 : 0;
3730 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN:
3733 case SWM_ARG_ID_CYCLEWS_DOWN_ALL:
3736 case SWM_ARG_ID_CYCLEWS_DOWN:
3737 a.id = (a.id > 0) ? a.id - 1 : workspace_limit - 1;
3744 (!cycle_empty && TAILQ_EMPTY(&s->ws[a.id].winlist)))
3746 if (!cycle_visible && s->ws[a.id].r != NULL)
3753 } while (a.id != r->ws->idx);
3757 priorws(struct swm_region *r, union arg *args)
3763 DNPRINTF(SWM_D_WS, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3764 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3766 if (r->ws_prior == NULL)
3769 a.id = r->ws_prior->idx;
3774 focusrg(struct swm_region *r, union arg *args)
3776 int ridx = args->id, i, num_screens;
3777 struct swm_region *rr = NULL;
3779 num_screens = get_screen_count();
3780 /* do nothing if we don't have more than one screen */
3781 if (!(num_screens > 1 || outputs > 1))
3784 DNPRINTF(SWM_D_FOCUS, "focusrg: id: %d\n", ridx);
3786 rr = TAILQ_FIRST(&r->s->rl);
3787 for (i = 0; i < ridx && rr != NULL; ++i)
3788 rr = TAILQ_NEXT(rr, entry);
3798 cyclerg(struct swm_region *r, union arg *args)
3800 struct swm_region *rr = NULL;
3803 num_screens = get_screen_count();
3804 /* do nothing if we don't have more than one screen */
3805 if (!(num_screens > 1 || outputs > 1))
3810 case SWM_ARG_ID_CYCLERG_UP:
3811 rr = TAILQ_NEXT(r, entry);
3813 rr = TAILQ_FIRST(&screens[i].rl);
3815 case SWM_ARG_ID_CYCLERG_DOWN:
3816 rr = TAILQ_PREV(r, swm_region_list, entry);
3818 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
3831 sort_windows(struct ws_win_list *wl)
3833 struct ws_win *win, *parent, *nxt;
3838 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
3839 nxt = TAILQ_NEXT(win, entry);
3840 if (win->transient) {
3841 parent = find_window(win->transient);
3842 if (parent == NULL) {
3843 warnx("not possible bug");
3846 TAILQ_REMOVE(wl, win, entry);
3847 TAILQ_INSERT_AFTER(wl, parent, win, entry);
3854 swapwin(struct swm_region *r, union arg *args)
3856 struct ws_win *target, *source;
3857 struct ws_win *cur_focus;
3858 struct ws_win_list *wl;
3860 DNPRINTF(SWM_D_WS, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3861 args->id, r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
3863 cur_focus = r->ws->focus;
3864 if (cur_focus == NULL)
3867 clear_maximized(r->ws);
3870 wl = &source->ws->winlist;
3873 case SWM_ARG_ID_SWAPPREV:
3874 if (source->transient)
3875 source = find_window(source->transient);
3876 target = TAILQ_PREV(source, ws_win_list, entry);
3877 if (target && target->transient)
3878 target = find_window(target->transient);
3879 TAILQ_REMOVE(wl, source, entry);
3881 TAILQ_INSERT_TAIL(wl, source, entry);
3883 TAILQ_INSERT_BEFORE(target, source, entry);
3885 case SWM_ARG_ID_SWAPNEXT:
3886 target = TAILQ_NEXT(source, entry);
3887 /* move the parent and let the sort handle the move */
3888 if (source->transient)
3889 source = find_window(source->transient);
3890 TAILQ_REMOVE(wl, source, entry);
3892 TAILQ_INSERT_HEAD(wl, source, entry);
3894 TAILQ_INSERT_AFTER(wl, target, source, entry);
3896 case SWM_ARG_ID_SWAPMAIN:
3897 target = TAILQ_FIRST(wl);
3898 if (target == source) {
3899 if (source->ws->focus_prev != NULL &&
3900 source->ws->focus_prev != target)
3901 source = source->ws->focus_prev;
3905 if (target == NULL || source == NULL)
3907 source->ws->focus_prev = target;
3908 TAILQ_REMOVE(wl, target, entry);
3909 TAILQ_INSERT_BEFORE(source, target, entry);
3910 TAILQ_REMOVE(wl, source, entry);
3911 TAILQ_INSERT_HEAD(wl, source, entry);
3913 case SWM_ARG_ID_MOVELAST:
3914 TAILQ_REMOVE(wl, source, entry);
3915 TAILQ_INSERT_TAIL(wl, source, entry);
3918 DNPRINTF(SWM_D_MOVE, "swapwin: invalid id: %d\n", args->id);
3930 get_focus_prev(struct ws_win *win)
3932 struct ws_win *winfocus = NULL;
3933 struct ws_win *cur_focus = NULL;
3934 struct ws_win_list *wl = NULL;
3935 struct workspace *ws = NULL;
3937 if (!(win && win->ws))
3942 cur_focus = ws->focus;
3944 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: window: 0x%x, cur_focus: 0x%x, "
3945 "focus_prev: 0x%x\n", WINID(win), WINID(cur_focus),
3946 WINID(ws->focus_prev));
3948 /* pickle, just focus on whatever */
3949 if (cur_focus == NULL) {
3950 /* use prev_focus if valid */
3951 if (ws->focus_prev && find_window(ws->focus_prev->id))
3952 winfocus = ws->focus_prev;
3956 /* if transient focus on parent */
3957 if (cur_focus->transient) {
3958 winfocus = find_window(cur_focus->transient);
3962 /* if in max_stack try harder */
3963 if ((win->quirks & SWM_Q_FOCUSPREV) ||
3964 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
3965 if (cur_focus != ws->focus_prev)
3966 winfocus = ws->focus_prev;
3968 winfocus = TAILQ_PREV(win, ws_win_list, entry);
3973 DNPRINTF(SWM_D_FOCUS, "get_focus_prev: focus_close: %d\n", focus_close);
3975 if (winfocus == NULL || winfocus == win) {
3976 switch (focus_close) {
3977 case SWM_STACK_BOTTOM:
3978 TAILQ_FOREACH(winfocus, wl, entry)
3979 if (!winfocus->iconic && winfocus != cur_focus)
3983 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
3984 if (!winfocus->iconic && winfocus != cur_focus)
3987 case SWM_STACK_ABOVE:
3988 winfocus = TAILQ_NEXT(cur_focus, entry);
3989 while (winfocus && winfocus->iconic)
3990 winfocus = TAILQ_NEXT(winfocus, entry);
3992 if (winfocus == NULL) {
3993 if (focus_close_wrap) {
3994 TAILQ_FOREACH(winfocus, wl, entry)
3995 if (!winfocus->iconic &&
3996 winfocus != cur_focus)
3999 TAILQ_FOREACH_REVERSE(winfocus, wl,
4001 if (!winfocus->iconic &&
4002 winfocus != cur_focus)
4007 case SWM_STACK_BELOW:
4008 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
4009 while (winfocus && winfocus->iconic)
4010 winfocus = TAILQ_PREV(winfocus, ws_win_list,
4013 if (winfocus == NULL) {
4014 if (focus_close_wrap) {
4015 TAILQ_FOREACH_REVERSE(winfocus, wl,
4017 if (!winfocus->iconic &&
4018 winfocus != cur_focus)
4021 TAILQ_FOREACH(winfocus, wl, entry)
4022 if (!winfocus->iconic &&
4023 winfocus != cur_focus)
4031 if (winfocus == NULL ||
4032 (winfocus && (winfocus->iconic || winfocus == cur_focus))) {
4033 if (focus_default == SWM_STACK_TOP) {
4034 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
4035 if (!winfocus->iconic && winfocus != cur_focus)
4038 TAILQ_FOREACH(winfocus, wl, entry)
4039 if (!winfocus->iconic && winfocus != cur_focus)
4046 return get_focus_magic(winfocus);
4050 get_region_focus(struct swm_region *r)
4052 struct ws_win *winfocus = NULL;
4057 if (r->ws->focus && !r->ws->focus->iconic)
4058 winfocus = r->ws->focus;
4059 else if (r->ws->focus_prev && !r->ws->focus_prev->iconic)
4060 winfocus = r->ws->focus_prev;
4062 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
4063 if (!winfocus->iconic)
4066 return get_focus_magic(winfocus);
4070 focus(struct swm_region *r, union arg *args)
4072 struct ws_win *head, *cur_focus = NULL, *winfocus = NULL;
4073 struct ws_win_list *wl = NULL;
4074 struct workspace *ws = NULL;
4077 xcb_icccm_wm_hints_t hints;
4082 DNPRINTF(SWM_D_FOCUS, "focus: id: %d\n", args->id);
4084 cur_focus = r->ws->focus;
4088 /* Make sure an uniconified window has focus, if one exists. */
4089 if (cur_focus == NULL) {
4090 cur_focus = TAILQ_FIRST(wl);
4091 while (cur_focus != NULL && cur_focus->iconic)
4092 cur_focus = TAILQ_NEXT(cur_focus, entry);
4096 case SWM_ARG_ID_FOCUSPREV:
4097 if (cur_focus == NULL)
4100 winfocus = cur_focus;
4102 winfocus = TAILQ_PREV(winfocus, ws_win_list, entry);
4103 if (winfocus == NULL)
4104 winfocus = TAILQ_LAST(wl, ws_win_list);
4105 if (winfocus == cur_focus)
4107 } while (winfocus != NULL &&
4108 (winfocus->iconic || winfocus->id == cur_focus->transient));
4110 case SWM_ARG_ID_FOCUSNEXT:
4111 if (cur_focus == NULL)
4114 winfocus = cur_focus;
4116 winfocus = TAILQ_NEXT(winfocus, entry);
4117 if (winfocus == NULL)
4118 winfocus = TAILQ_FIRST(wl);
4119 if (winfocus == cur_focus)
4121 } while (winfocus != NULL &&
4122 (winfocus->iconic || winfocus->id == cur_focus->transient));
4124 case SWM_ARG_ID_FOCUSMAIN:
4125 if (cur_focus == NULL)
4128 winfocus = TAILQ_FIRST(wl);
4129 if (winfocus == cur_focus)
4130 winfocus = cur_focus->ws->focus_prev;
4132 case SWM_ARG_ID_FOCUSURGENT:
4133 /* Search forward for the next urgent window. */
4137 for (i = 0; i <= workspace_limit; ++i) {
4139 head = TAILQ_FIRST(&r->s->ws[(ws->idx + i) %
4140 workspace_limit].winlist);
4142 while (head != NULL &&
4143 (head = TAILQ_NEXT(head, entry)) != NULL) {
4144 if (head == cur_focus) {
4145 winfocus = cur_focus;
4148 if (xcb_icccm_get_wm_hints_reply(conn,
4149 xcb_icccm_get_wm_hints(conn, head->id),
4150 &hints, NULL) != 0 &&
4151 xcb_icccm_wm_hints_get_urgency(&hints)) {
4157 if (winfocus != NULL)
4161 /* Switch ws if new focus is on a different ws. */
4162 if (winfocus != NULL && winfocus->ws != ws) {
4163 a.id = winfocus->ws->idx;
4171 if (clear_maximized(ws) > 0)
4174 focus_win(get_focus_magic(winfocus));
4178 DNPRINTF(SWM_D_FOCUS, "focus: done\n");
4182 cycle_layout(struct swm_region *r, union arg *args)
4184 struct workspace *ws = r->ws;
4186 /* suppress unused warning since var is needed */
4189 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
4192 if (ws->cur_layout->l_stack == NULL)
4193 ws->cur_layout = &layouts[0];
4195 clear_maximized(ws);
4200 focus_win(get_region_focus(r));
4206 stack_config(struct swm_region *r, union arg *args)
4208 struct workspace *ws = r->ws;
4210 DNPRINTF(SWM_D_STACK, "stack_config: id: %d workspace: %d\n",
4213 if (clear_maximized(ws) > 0)
4216 if (ws->cur_layout->l_config != NULL)
4217 ws->cur_layout->l_config(ws, args->id);
4219 if (args->id != SWM_ARG_ID_STACKINIT)
4228 struct swm_geometry g;
4229 struct swm_region *r;
4235 DNPRINTF(SWM_D_STACK, "stack: begin\n");
4237 num_screens = get_screen_count();
4238 for (i = 0; i < num_screens; i++) {
4242 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4243 /* Adjust stack area for region bar and padding. */
4245 g.x += region_padding;
4246 g.y += region_padding;
4247 g.w -= 2 * border_width + 2 * region_padding;
4248 g.h -= 2 * border_width + 2 * region_padding;
4249 if (bar_enabled && r->ws->bar_enabled) {
4255 DNPRINTF(SWM_D_STACK, "stack: workspace: %d (screen: "
4256 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4257 r->ws->idx, i, j++, g.x, g.y, g.w, g.h);
4259 r->ws->cur_layout->l_stack(r->ws, &g);
4260 r->ws->cur_layout->l_string(r->ws);
4261 /* save r so we can track region changes */
4268 DNPRINTF(SWM_D_STACK, "stack: end\n");
4272 store_float_geom(struct ws_win *win)
4274 if (win == NULL || win->ws->r == NULL)
4277 /* retain window geom and region geom */
4278 win->g_float = win->g;
4279 win->g_float.x -= X(win->ws->r);
4280 win->g_float.y -= Y(win->ws->r);
4281 win->g_floatvalid = 1;
4282 DNPRINTF(SWM_D_MISC, "store_float_geom: window: 0x%x, g: (%d,%d)"
4283 " %d x %d, g_float: (%d,%d) %d x %d\n", win->id, X(win), Y(win),
4284 WIDTH(win), HEIGHT(win), win->g_float.x, win->g_float.y,
4285 win->g_float.w, win->g_float.h);
4289 load_float_geom(struct ws_win *win)
4291 if (win == NULL || win->ws->r == NULL)
4294 if (win->g_floatvalid) {
4295 win->g = win->g_float;
4296 X(win) += X(win->ws->r);
4297 Y(win) += Y(win->ws->r);
4298 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g: (%d,%d)"
4299 "%d x %d\n", win->id, X(win), Y(win), WIDTH(win),
4302 DNPRINTF(SWM_D_MISC, "load_float_geom: window: 0x%x, g_float "
4303 "is not set.\n", win->id);
4308 stack_floater(struct ws_win *win)
4310 struct workspace *ws;
4311 struct swm_region *r;
4318 if ((r = ws->r) == NULL)
4321 DNPRINTF(SWM_D_MISC, "stack_floater: window: 0x%x\n", win->id);
4323 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4324 /* _NET_WM_FULLSCREEN: fullscreen without border. */
4325 if (!win->g_floatvalid)
4326 store_float_geom(win);
4330 } else if (win->maximized) {
4331 /* Maximize: like a single stacked window. */
4332 if (!win->g_floatvalid)
4333 store_float_geom(win);
4337 if (bar_enabled && ws->bar_enabled) {
4340 Y(win) += bar_height;
4341 HEIGHT(win) -= bar_height;
4342 } else if (disable_border) {
4348 if (win->bordered) {
4349 HEIGHT(win) -= 2 * border_width;
4350 WIDTH(win) -= 2 * border_width;
4353 /* Normal floating window. */
4354 /* Update geometry if new region. */
4356 load_float_geom(win);
4358 if ((win->quirks & SWM_Q_FULLSCREEN) &&
4359 WIDTH(win) >= WIDTH(r) && HEIGHT(win) >= HEIGHT(r)) {
4360 /* Remove border for FULLSCREEN quirk. */
4362 } else if (!win->manual) {
4363 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
4364 /* Adjust size on TRANSSZ quirk. */
4365 WIDTH(win) = (double)WIDTH(r) * dialog_ratio;
4366 HEIGHT(win) = (double)HEIGHT(r) * dialog_ratio;
4369 if (!(win->quirks & SWM_Q_ANYWHERE)) {
4371 * Floaters and transients are auto-centred
4372 * unless manually moved, resized or ANYWHERE
4375 X(win) = X(r) + (WIDTH(r) - WIDTH(win)) / 2 -
4377 Y(win) = Y(r) + (HEIGHT(r) - HEIGHT(win)) / 2 -
4379 store_float_geom(win);
4384 /* Ensure at least 1 pixel of the window is in the region. */
4385 region_containment(win, r, SWM_CW_ALLSIDES);
4391 * Send keystrokes to terminal to decrease/increase the font size as the
4392 * window size changes.
4395 adjust_font(struct ws_win *win)
4397 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
4398 win->floating || win->transient)
4401 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
4402 WIDTH(win) / win->sh.width_inc < term_width &&
4403 win->font_steps < SWM_MAX_FONT_STEPS) {
4404 win->font_size_boundary[win->font_steps] =
4405 (win->sh.width_inc * term_width) + win->sh.base_width;
4408 win->last_inc = win->sh.width_inc;
4409 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
4410 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
4411 WIDTH(win) > win->font_size_boundary[win->font_steps - 1]) {
4414 win->last_inc = win->sh.width_inc;
4415 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
4419 #define SWAPXY(g) do { \
4421 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4422 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4425 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
4427 struct swm_geometry win_g, r_g = *g;
4428 struct ws_win *win, *fs_win = NULL, *max_win = NULL;
4429 int i, j, s, stacks;
4430 int w_inc = 1, h_inc, w_base = 1, h_base;
4431 int hrh, extra = 0, h_slice, last_h = 0;
4432 int split, colno, winno, mwin, msize, mscale;
4433 int remain, missing, v_slice, reconfigure = 0;
4436 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d, rot: %s, "
4437 "flip: %s\n", ws->idx, YESNO(rot), YESNO(flip));
4439 winno = count_win(ws, 0);
4440 if (winno == 0 && count_win(ws, 1) == 0)
4443 /* Find first tiled window. */
4444 TAILQ_FOREACH(win, &ws->winlist, entry)
4445 if (!win->transient && !win->floating && !win->iconic)
4452 w_inc = win->sh.width_inc;
4453 w_base = win->sh.base_width;
4454 mwin = ws->l_state.horizontal_mwin;
4455 mscale = ws->l_state.horizontal_msize;
4456 stacks = ws->l_state.horizontal_stacks;
4459 w_inc = win->sh.height_inc;
4460 w_base = win->sh.base_height;
4461 mwin = ws->l_state.vertical_mwin;
4462 mscale = ws->l_state.vertical_msize;
4463 stacks = ws->l_state.vertical_stacks;
4467 if (stacks > winno - mwin)
4468 stacks = winno - mwin;
4472 h_slice = r_g.h / SWM_H_SLICE;
4473 if (mwin && winno > mwin) {
4474 v_slice = r_g.w / SWM_V_SLICE;
4478 win_g.w = v_slice * mscale;
4480 if (w_inc > 1 && w_inc < v_slice) {
4481 /* adjust for window's requested size increment */
4482 remain = (win_g.w - w_base) % w_inc;
4488 win_g.x += r_g.w - msize;
4491 colno = split = winno / stacks;
4492 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
4493 2 * border_width) / stacks);
4495 hrh = r_g.h / colno;
4496 extra = r_g.h - (colno * hrh);
4497 win_g.h = hrh - 2 * border_width;
4499 /* stack all the tiled windows */
4500 i = j = 0, s = stacks;
4501 TAILQ_FOREACH(win, &ws->winlist, entry) {
4502 if (win->transient || win->floating || win->iconic)
4505 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4510 if (win->maximized) {
4515 if (split && i == split) {
4516 colno = (winno - mwin) / stacks;
4517 if (s <= (winno - mwin) % stacks)
4520 hrh = r_g.h / colno;
4521 extra = r_g.h - (colno * hrh);
4524 win_g.x += win_g.w + 2 * border_width +
4527 win_g.w = (r_g.w - msize -
4528 (stacks * (2 * border_width + tile_gap))) / stacks;
4530 win_g.w += (r_g.w - msize -
4531 (stacks * (2 * border_width + tile_gap))) %
4535 win_g.x -= win_g.w + 2 * border_width +
4541 win_g.h = hrh - 2 * border_width - tile_gap;
4544 h_inc = win->sh.width_inc;
4545 h_base = win->sh.base_width;
4547 h_inc = win->sh.height_inc;
4548 h_base = win->sh.base_height;
4551 if (j == colno - 1) {
4552 win_g.h = hrh + extra;
4553 } else if (h_inc > 1 && h_inc < h_slice) {
4554 /* adjust for window's requested size increment */
4555 remain = (win_g.h - h_base) % h_inc;
4556 missing = h_inc - remain;
4558 if (missing <= extra || j == 0) {
4570 win_g.y += last_h + 2 * border_width + tile_gap;
4572 if (disable_border && !(bar_enabled && ws->bar_enabled) &&
4575 win_g.w += 2 * border_width;
4576 win_g.h += 2 * border_width;
4582 if (X(win) != win_g.y || Y(win) != win_g.x ||
4583 WIDTH(win) != win_g.h || HEIGHT(win) != win_g.w) {
4587 WIDTH(win) = win_g.h;
4588 HEIGHT(win) = win_g.w;
4591 if (X(win) != win_g.x || Y(win) != win_g.y ||
4592 WIDTH(win) != win_g.w || HEIGHT(win) != win_g.h) {
4596 WIDTH(win) = win_g.w;
4597 HEIGHT(win) = win_g.h;
4601 if (bordered != win->bordered) {
4603 win->bordered = bordered;
4611 reorder_window(win, SWM_STACK_ABOVE);
4619 /* Map/raise focused tiled window to top if windows could overlap. */
4620 if (tile_gap < 0 && ws->focus != NULL && !ws->focus->floating &&
4621 !ws->focus->maximized) {
4622 reorder_window(ws->focus, SWM_STACK_TOP);
4623 map_window(ws->focus);
4627 /* now, stack all the floaters and transients */
4628 TAILQ_FOREACH(win, &ws->winlist, entry) {
4629 if ((!win->transient && !win->floating) || win->iconic)
4632 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
4637 if (win->maximized) {
4643 reorder_window(win, SWM_STACK_ABOVE);
4647 /* Make sure fs_win is stacked on top. */
4649 stack_floater(fs_win);
4650 reorder_window(fs_win, SWM_STACK_TOP);
4652 } else if (max_win) {
4653 stack_floater(max_win);
4654 reorder_window(max_win, SWM_STACK_TOP);
4655 map_window(max_win);
4658 DNPRINTF(SWM_D_STACK, "stack_master: max_win: 0x%x\n", WINID(max_win));
4662 vertical_config(struct workspace *ws, int id)
4664 DNPRINTF(SWM_D_STACK, "vertical_config: id: %d, workspace: %d\n",
4668 case SWM_ARG_ID_STACKRESET:
4669 case SWM_ARG_ID_STACKINIT:
4670 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
4671 ws->l_state.vertical_mwin = 1;
4672 ws->l_state.vertical_stacks = 1;
4674 case SWM_ARG_ID_MASTERSHRINK:
4675 if (ws->l_state.vertical_msize > 1)
4676 ws->l_state.vertical_msize--;
4678 case SWM_ARG_ID_MASTERGROW:
4679 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
4680 ws->l_state.vertical_msize++;
4682 case SWM_ARG_ID_MASTERADD:
4683 ws->l_state.vertical_mwin++;
4685 case SWM_ARG_ID_MASTERDEL:
4686 if (ws->l_state.vertical_mwin > 0)
4687 ws->l_state.vertical_mwin--;
4689 case SWM_ARG_ID_STACKINC:
4690 ws->l_state.vertical_stacks++;
4692 case SWM_ARG_ID_STACKDEC:
4693 if (ws->l_state.vertical_stacks > 1)
4694 ws->l_state.vertical_stacks--;
4696 case SWM_ARG_ID_FLIPLAYOUT:
4697 ws->l_state.vertical_flip = !ws->l_state.vertical_flip;
4705 vertical_stack(struct workspace *ws, struct swm_geometry *g)
4707 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
4709 stack_master(ws, g, 0, ws->l_state.vertical_flip);
4713 horizontal_config(struct workspace *ws, int id)
4715 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
4718 case SWM_ARG_ID_STACKRESET:
4719 case SWM_ARG_ID_STACKINIT:
4720 ws->l_state.horizontal_mwin = 1;
4721 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
4722 ws->l_state.horizontal_stacks = 1;
4724 case SWM_ARG_ID_MASTERSHRINK:
4725 if (ws->l_state.horizontal_msize > 1)
4726 ws->l_state.horizontal_msize--;
4728 case SWM_ARG_ID_MASTERGROW:
4729 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
4730 ws->l_state.horizontal_msize++;
4732 case SWM_ARG_ID_MASTERADD:
4733 ws->l_state.horizontal_mwin++;
4735 case SWM_ARG_ID_MASTERDEL:
4736 if (ws->l_state.horizontal_mwin > 0)
4737 ws->l_state.horizontal_mwin--;
4739 case SWM_ARG_ID_STACKINC:
4740 ws->l_state.horizontal_stacks++;
4742 case SWM_ARG_ID_STACKDEC:
4743 if (ws->l_state.horizontal_stacks > 1)
4744 ws->l_state.horizontal_stacks--;
4746 case SWM_ARG_ID_FLIPLAYOUT:
4747 ws->l_state.horizontal_flip = !ws->l_state.horizontal_flip;
4755 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
4757 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
4759 stack_master(ws, g, 1, ws->l_state.horizontal_flip);
4762 /* fullscreen view */
4764 max_stack(struct workspace *ws, struct swm_geometry *g)
4766 struct swm_geometry gg = *g;
4767 struct ws_win *w, *win = NULL, *parent = NULL;
4770 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
4775 winno = count_win(ws, 0);
4776 if (winno == 0 && count_win(ws, 1) == 0)
4779 /* Figure out which top level window should be visible. */
4780 if (ws->focus_pending)
4781 win = ws->focus_pending;
4784 else if (ws->focus_prev)
4785 win = ws->focus_prev;
4787 win = TAILQ_FIRST(&ws->winlist);
4790 parent = find_window(win->transient);
4792 DNPRINTF(SWM_D_STACK, "max_stack: win: 0x%x\n", win->id);
4794 /* maximize all top level windows */
4795 TAILQ_FOREACH(w, &ws->winlist, entry) {
4796 if (w->transient || w->iconic)
4799 if (!w->mapped && w != win) {
4800 reorder_window(w, SWM_STACK_TOP);
4804 if (w->floating && !w->maximized) {
4806 * retain geometry for retrieval on exit from
4809 store_float_geom(w);
4813 /* only reconfigure if necessary */
4814 if (X(w) != gg.x || Y(w) != gg.y || WIDTH(w) != gg.w ||
4815 HEIGHT(w) != gg.h) {
4817 if (bar_enabled && ws->bar_enabled){
4821 WIDTH(w) += 2 * border_width;
4822 HEIGHT(w) += 2 * border_width;
4829 /* If a parent exists, map/raise it first. */
4831 reorder_window(parent, SWM_STACK_TOP);
4834 /* Map siblings next. */
4835 TAILQ_FOREACH(w, &ws->winlist, entry)
4836 if (w != win && !w->iconic &&
4837 w->transient == parent->id) {
4839 reorder_window(w, SWM_STACK_ABOVE);
4844 /* Map/raise focused window. */
4845 reorder_window(win, SWM_STACK_TOP);
4848 /* Finally, map/raise children of focus window. */
4849 TAILQ_FOREACH(w, &ws->winlist, entry)
4850 if (w->transient == win->id && !w->iconic) {
4852 reorder_window(w, SWM_STACK_ABOVE);
4858 send_to_rg(struct swm_region *r, union arg *args)
4860 int ridx = args->id, i, num_screens;
4861 struct swm_region *rr = NULL;
4864 num_screens = get_screen_count();
4865 /* do nothing if we don't have more than one screen */
4866 if (!(num_screens > 1 || outputs > 1))
4869 DNPRINTF(SWM_D_FOCUS, "send_to_rg: id: %d\n", ridx);
4871 rr = TAILQ_FIRST(&r->s->rl);
4872 for (i = 0; i < ridx && rr != NULL; ++i)
4873 rr = TAILQ_NEXT(rr, entry);
4884 region_under(struct swm_screen *s, int x, int y)
4886 struct swm_region *r = NULL;
4888 TAILQ_FOREACH(r, &s->rl, entry) {
4889 DNPRINTF(SWM_D_MISC, "region_under: ws: %d, region g: (%d,%d) "
4890 "%d x %d, coords: (%d,%d)\n", r->ws->idx, X(r), Y(r),
4891 WIDTH(r), HEIGHT(r), x, y);
4892 if (X(r) <= x && x < MAX_X(r))
4893 if (Y(r) <= y && y < MAX_Y(r))
4901 send_to_ws(struct swm_region *r, union arg *args)
4903 int wsid = args->id;
4904 struct ws_win *win = NULL;
4906 if (r && r->ws && r->ws->focus)
4911 DNPRINTF(SWM_D_MOVE, "send_to_ws: win 0x%x, ws %d\n", win->id, wsid);
4913 if (wsid >= workspace_limit)
4916 if (win->ws->idx == wsid)
4919 win_to_ws(win, wsid, 1);
4923 /* Restack and set new focus. */
4926 if (focus_mode != SWM_FOCUS_FOLLOW) {
4927 if (r->ws->focus_pending) {
4928 focus_win(r->ws->focus_pending);
4929 r->ws->focus_pending = NULL;
4931 xcb_set_input_focus(conn,
4932 XCB_INPUT_FOCUS_PARENT, r->id,
4941 win_to_ws(struct ws_win *win, int wsid, int unfocus)
4943 struct ws_win *parent;
4944 struct workspace *ws, *nws, *pws;
4945 char ws_idx_str[SWM_PROPLEN];
4947 if (wsid >= workspace_limit)
4950 if (win->ws->idx == wsid)
4954 nws = &win->s->ws[wsid];
4956 DNPRINTF(SWM_D_MOVE, "win_to_ws: win 0x%x, ws %d -> %d\n", win->id,
4959 /* Update the window's workspace property: _SWM_WS */
4960 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
4961 if (focus_mode != SWM_FOCUS_FOLLOW)
4962 ws->focus_pending = get_focus_prev(win);
4964 /* Move the parent if this is a transient window. */
4965 if (win->transient) {
4966 parent = find_window(win->transient);
4969 /* Set new focus in parent's ws if needed. */
4970 if (pws->focus == parent) {
4971 if (focus_mode != SWM_FOCUS_FOLLOW)
4972 pws->focus_pending =
4973 get_focus_prev(parent);
4975 unfocus_win(parent);
4977 if (focus_mode != SWM_FOCUS_FOLLOW) {
4978 pws->focus = pws->focus_pending;
4979 pws->focus_pending = NULL;
4983 /* Don't unmap parent if new ws is visible */
4985 unmap_window(parent);
4988 TAILQ_REMOVE(&ws->winlist, parent, entry);
4989 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
4992 DNPRINTF(SWM_D_PROP, "win_to_ws: set "
4993 "property: _SWM_WS: %s\n", ws_idx_str);
4994 xcb_change_property(conn, XCB_PROP_MODE_REPLACE,
4995 parent->id, a_swm_ws, XCB_ATOM_STRING, 8,
4996 strlen(ws_idx_str), ws_idx_str);
5003 /* Don't unmap if new ws is visible */
5008 TAILQ_REMOVE(&ws->winlist, win, entry);
5009 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
5012 /* Set focus on new ws. */
5013 unfocus_win(nws->focus);
5016 DNPRINTF(SWM_D_PROP, "win_to_ws: set property: _SWM_WS: %s\n",
5018 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
5019 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
5023 DNPRINTF(SWM_D_MOVE, "win_to_ws: done.\n");
5027 pressbutton(struct swm_region *r, union arg *args)
5029 /* suppress unused warning since var is needed */
5032 xcb_test_fake_input(conn, XCB_BUTTON_PRESS, args->id,
5033 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
5034 xcb_test_fake_input(conn, XCB_BUTTON_RELEASE, args->id,
5035 XCB_CURRENT_TIME, XCB_WINDOW_NONE, 0, 0, 0);
5039 raise_toggle(struct swm_region *r, union arg *args)
5041 /* suppress unused warning since var is needed */
5044 if (r == NULL || r->ws == NULL)
5047 if (r->ws->focus && r->ws->focus->maximized)
5050 r->ws->always_raise = !r->ws->always_raise;
5052 /* bring floaters back to top */
5053 if (!r->ws->always_raise)
5060 iconify(struct swm_region *r, union arg *args)
5062 /* suppress unused warning since var is needed */
5065 if (r->ws->focus == NULL)
5068 set_swm_iconic(r->ws->focus, 1);
5074 get_win_name(xcb_window_t win)
5077 xcb_get_property_cookie_t c;
5078 xcb_get_property_reply_t *r;
5080 /* First try _NET_WM_NAME for UTF-8. */
5081 c = xcb_get_property(conn, 0, win, ewmh[_NET_WM_NAME].atom,
5082 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
5083 r = xcb_get_property_reply(conn, c, NULL);
5084 if (r && r->type == XCB_NONE) {
5086 /* Use WM_NAME instead; no UTF-8. */
5087 c = xcb_get_property(conn, 0, win, XCB_ATOM_WM_NAME,
5088 XCB_GET_PROPERTY_TYPE_ANY, 0, UINT_MAX);
5089 r = xcb_get_property_reply(conn, c, NULL);
5092 if (r && r->type != XCB_NONE && r->length > 0)
5093 name = strndup(xcb_get_property_value(r),
5094 xcb_get_property_value_length(r));
5099 err(1, "get_win_name: failed to allocate memory.");
5107 uniconify(struct swm_region *r, union arg *args)
5114 DNPRINTF(SWM_D_MISC, "uniconify\n");
5116 if (r == NULL || r->ws == NULL)
5119 /* make sure we have anything to uniconify */
5120 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5121 if (win->ws == NULL)
5122 continue; /* should never happen */
5131 search_resp_action = SWM_SEARCH_UNICONIFY;
5133 spawn_select(r, args, "search", &searchpid);
5135 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5138 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5139 if (win->ws == NULL)
5140 continue; /* should never happen */
5144 name = get_win_name(win->id);
5145 fprintf(lfile, "%s.%u\n", name, win->id);
5153 name_workspace(struct swm_region *r, union arg *args)
5157 DNPRINTF(SWM_D_MISC, "name_workspace\n");
5163 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
5165 spawn_select(r, args, "name_workspace", &searchpid);
5167 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5170 fprintf(lfile, "%s", "");
5175 search_workspace(struct swm_region *r, union arg *args)
5178 struct workspace *ws;
5181 DNPRINTF(SWM_D_MISC, "search_workspace\n");
5187 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
5189 spawn_select(r, args, "search", &searchpid);
5191 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5194 for (i = 0; i < workspace_limit; i++) {
5198 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
5199 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
5206 search_win_cleanup(void)
5208 struct search_window *sw = NULL;
5210 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
5211 xcb_destroy_window(conn, sw->indicator);
5212 TAILQ_REMOVE(&search_wl, sw, entry);
5218 search_win(struct swm_region *r, union arg *args)
5220 struct ws_win *win = NULL;
5221 struct search_window *sw = NULL;
5224 int i, width, height;
5232 XRectangle l_ibox, l_lbox;
5234 DNPRINTF(SWM_D_MISC, "search_win\n");
5237 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
5239 spawn_select(r, args, "search", &searchpid);
5241 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
5244 TAILQ_INIT(&search_wl);
5247 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
5251 sw = calloc(1, sizeof(struct search_window));
5253 warn("search_win: calloc");
5255 search_win_cleanup();
5261 snprintf(s, sizeof s, "%d", i);
5264 w = xcb_generate_id(conn);
5265 wa[0] = r->s->c[SWM_S_COLOR_FOCUS].pixel;
5266 wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].pixel;
5268 if (bar_font_legacy) {
5269 XmbTextExtents(bar_fs, s, len, &l_ibox, &l_lbox);
5270 width = l_lbox.width + 4;
5271 height = bar_fs_extents->max_logical_extent.height + 4;
5273 XftTextExtentsUtf8(display, bar_font, (FcChar8 *)s, len,
5275 width = info.width + 4;
5276 height = bar_font->height + 4;
5279 xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
5280 width, height, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
5281 XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL |
5282 XCB_CW_BORDER_PIXEL, wa);
5284 xcb_map_window(conn, w);
5287 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
5289 if (bar_font_legacy) {
5290 l_gcv.graphics_exposures = 0;
5291 l_draw = XCreateGC(display, w, 0, &l_gcv);
5293 XSetForeground(display, l_draw,
5294 r->s->c[SWM_S_COLOR_BAR].pixel);
5296 DRAWSTRING(display, w, bar_fs, l_draw, 2,
5297 (bar_fs_extents->max_logical_extent.height -
5298 l_lbox.height) / 2 - l_lbox.y, s, len);
5300 XFreeGC(display, l_draw);
5303 draw = XftDrawCreate(display, w,
5304 DefaultVisual(display, r->s->idx),
5305 DefaultColormap(display, r->s->idx));
5307 XftDrawStringUtf8(draw, &bar_font_color, bar_font, 2,
5308 (HEIGHT(r->bar) + bar_font->height) / 2 -
5309 bar_font->descent, (FcChar8 *)s, len);
5311 XftDrawDestroy(draw);
5314 DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
5316 fprintf(lfile, "%d\n", i);
5326 search_resp_uniconify(const char *resp, size_t len)
5332 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp: %s\n", resp);
5334 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
5337 name = get_win_name(win->id);
5338 if (asprintf(&s, "%s.%u", name, win->id) == -1) {
5343 if (strncmp(s, resp, len) == 0) {
5344 /* XXX this should be a callback to generalize */
5345 set_swm_iconic(win, 0);
5354 search_resp_name_workspace(const char *resp, size_t len)
5356 struct workspace *ws;
5358 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp: %s\n", resp);
5360 if (search_r->ws == NULL)
5365 free(search_r->ws->name);
5366 search_r->ws->name = NULL;
5370 ws->name = strdup(resp);
5371 if (ws->name == NULL) {
5372 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: "
5373 "strdup: %s", strerror(errno));
5380 search_resp_search_workspace(const char *resp)
5387 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp: %s\n", resp);
5391 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
5398 ws_idx = (int)strtonum(q, 1, workspace_limit, &errstr);
5400 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
5407 switchws(search_r, &a);
5411 search_resp_search_window(const char *resp)
5416 struct search_window *sw;
5418 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp: %s\n", resp);
5422 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
5427 idx = (int)strtonum(s, 1, INT_MAX, &errstr);
5429 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
5436 TAILQ_FOREACH(sw, &search_wl, entry)
5437 if (idx == sw->idx) {
5443 #define MAX_RESP_LEN 1024
5446 search_do_resp(void)
5452 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
5457 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
5458 warn("search: calloc");
5462 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
5464 warn("search: read error");
5467 resp[rbytes] = '\0';
5470 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5471 * newline, so work around that by sanitizing the resp now.
5473 resp[strcspn(resp, "\n")] = '\0';
5476 switch (search_resp_action) {
5477 case SWM_SEARCH_UNICONIFY:
5478 search_resp_uniconify(resp, len);
5480 case SWM_SEARCH_NAME_WORKSPACE:
5481 search_resp_name_workspace(resp, len);
5483 case SWM_SEARCH_SEARCH_WORKSPACE:
5484 search_resp_search_workspace(resp);
5486 case SWM_SEARCH_SEARCH_WINDOW:
5487 search_resp_search_window(resp);
5492 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
5493 search_win_cleanup();
5495 search_resp_action = SWM_SEARCH_NONE;
5496 close(select_resp_pipe[0]);
5503 wkill(struct swm_region *r, union arg *args)
5505 DNPRINTF(SWM_D_MISC, "wkill: win %#x, id: %d\n", WINID(r->ws->focus),
5508 if (r->ws->focus == NULL)
5511 if (args->id == SWM_ARG_ID_KILLWINDOW)
5512 xcb_kill_client(conn, r->ws->focus->id);
5514 if (r->ws->focus->can_delete)
5515 client_msg(r->ws->focus, a_delete, 0);
5521 clear_maximized(struct workspace *ws)
5526 /* Clear any maximized win(s) on ws. */
5527 TAILQ_FOREACH(w, &ws->winlist, entry)
5539 maximize_toggle(struct swm_region *r, union arg *args)
5541 struct ws_win *win = r->ws->focus;
5543 /* suppress unused warning since var is needed */
5549 DNPRINTF(SWM_D_MISC, "maximize_toggle: win %#x\n", win->id);
5551 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5554 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
5557 if (win->floating) {
5559 load_float_geom(win);
5561 store_float_geom(win);
5564 win->maximized = !win->maximized;
5568 if (win == win->ws->focus)
5572 DNPRINTF(SWM_D_MISC, "maximize_toggle: done\n");
5576 floating_toggle_win(struct ws_win *win)
5581 /* reject floating toggles in max stack mode */
5582 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
5585 if (win->ws->r == NULL)
5588 if (win->floating) {
5589 if (!win->maximized)
5590 store_float_geom(win);
5592 load_float_geom(win);
5595 win->floating = !win->floating;
5598 ewmh_update_actions(win);
5604 floating_toggle(struct swm_region *r, union arg *args)
5606 struct ws_win *win = r->ws->focus;
5608 /* suppress unused warning since var is needed */
5614 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5617 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
5618 _NET_WM_STATE_TOGGLE);
5622 if (win == win->ws->focus)
5629 region_containment(struct ws_win *win, struct swm_region *r, int opts)
5631 struct swm_geometry g = r->g;
5632 int rt, lt, tp, bm, bw;
5634 bw = (opts & SWM_CW_SOFTBOUNDARY) ? boundary_width : 0;
5637 * Perpendicular distance of each side of the window to the respective
5638 * side of the region boundary. Positive values indicate the side of
5639 * the window has passed beyond the region boundary.
5641 rt = opts & SWM_CW_RIGHT ? MAX_X(win) + BORDER(win) - MAX_X(r) : bw;
5642 lt = opts & SWM_CW_LEFT ? X(r) - X(win) + BORDER(win) : bw;
5643 bm = opts & SWM_CW_BOTTOM ? MAX_Y(win) + BORDER(win) - MAX_Y(r) : bw;
5644 tp = opts & SWM_CW_TOP ? Y(r) - Y(win) + BORDER(win) : bw;
5646 DNPRINTF(SWM_D_MISC, "region_containment: win 0x%x, rt: %d, lt: %d, "
5647 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win->id, rt,
5648 lt, bm, tp, YESNO(opts & SWM_CW_SOFTBOUNDARY),
5649 YESNO(opts & SWM_CW_HARDBOUNDARY));
5652 * Disable containment if any of the flagged sides went beyond the
5653 * containment boundary, or if containment is disabled.
5655 if (!(opts & SWM_CW_HARDBOUNDARY || opts & SWM_CW_SOFTBOUNDARY) ||
5656 (bw != 0 && ((rt > bw) || (lt > bw) || (bm > bw) || (tp > bw)))) {
5657 /* Make sure window has at least 1 pixel in the region */
5658 g.x += 1 - WIDTH(win);
5659 g.y += 1 - HEIGHT(win);
5660 g.w += 2 * WIDTH(win) - 2;
5661 g.h += 2 * HEIGHT(win) - 2;
5664 constrain_window(win, &g, &opts);
5667 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
5669 constrain_window(struct ws_win *win, struct swm_geometry *b, int *opts)
5671 DNPRINTF(SWM_D_MISC, "constrain_window: window: 0x%x, (x,y) w x h: "
5672 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
5673 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win->id, X(win),
5674 Y(win), WIDTH(win), HEIGHT(win), b->x, b->y, b->w, b->h,
5675 YESNO(*opts & SWM_CW_RIGHT), YESNO(*opts & SWM_CW_LEFT),
5676 YESNO(*opts & SWM_CW_BOTTOM), YESNO(*opts & SWM_CW_TOP),
5677 YESNO(*opts & SWM_CW_RESIZABLE));
5679 if ((*opts & SWM_CW_RIGHT) && MAX_X(win) + BORDER(win) > b->x + b->w) {
5680 if (*opts & SWM_CW_RESIZABLE)
5681 WIDTH(win) = b->x + b->w - X(win) - BORDER(win);
5683 X(win) = b->x + b->w - WIDTH(win) - BORDER(win);
5686 if ((*opts & SWM_CW_LEFT) && X(win) + BORDER(win) < b->x) {
5687 if (*opts & SWM_CW_RESIZABLE)
5688 WIDTH(win) -= b->x - X(win) - BORDER(win);
5690 X(win) = b->x - BORDER(win);
5693 if ((*opts & SWM_CW_BOTTOM) && MAX_Y(win) + BORDER(win) > b->y + b->h) {
5694 if (*opts & SWM_CW_RESIZABLE)
5695 HEIGHT(win) = b->y + b->h - Y(win) - BORDER(win);
5697 Y(win) = b->y + b->h - HEIGHT(win) - BORDER(win);
5700 if ((*opts & SWM_CW_TOP) && Y(win) + BORDER(win) < b->y) {
5701 if (*opts & SWM_CW_RESIZABLE)
5702 HEIGHT(win) -= b->y - Y(win) - BORDER(win);
5704 Y(win) = b->y - BORDER(win);
5707 if (*opts & SWM_CW_RESIZABLE) {
5710 if (HEIGHT(win) < 1)
5716 update_window(struct ws_win *win)
5721 mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
5722 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT |
5723 XCB_CONFIG_WINDOW_BORDER_WIDTH;
5727 wc[3] = HEIGHT(win);
5728 wc[4] = BORDER(win);
5730 DNPRINTF(SWM_D_EVENT, "update_window: window: 0x%x, (x,y) w x h: "
5731 "(%d,%d) %d x %d, bordered: %s\n", win->id, wc[0], wc[1], wc[2],
5732 wc[3], YESNO(win->bordered));
5734 xcb_configure_window(conn, win->id, mask, wc);
5737 #define SWM_RESIZE_STEPS (50)
5740 resize(struct ws_win *win, union arg *args)
5742 xcb_timestamp_t timestamp = 0;
5743 struct swm_region *r = NULL;
5745 struct swm_geometry g;
5746 int top = 0, left = 0, resizing;
5748 xcb_cursor_t cursor;
5749 xcb_query_pointer_reply_t *xpr = NULL;
5750 xcb_generic_event_t *evt;
5751 xcb_motion_notify_event_t *mne;
5757 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5760 DNPRINTF(SWM_D_EVENT, "resize: window: 0x%x, floating: %s, "
5761 "transient: 0x%x\n", win->id, YESNO(win->floating),
5764 if (!win->transient && !win->floating)
5767 /* In max_stack mode, should only resize transients. */
5768 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
5775 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
5783 /* It's possible for win to have been freed during focus_flush(). */
5784 if (validate_win(win)) {
5785 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
5790 case SWM_ARG_ID_WIDTHSHRINK:
5791 WIDTH(win) -= SWM_RESIZE_STEPS;
5794 case SWM_ARG_ID_WIDTHGROW:
5795 WIDTH(win) += SWM_RESIZE_STEPS;
5798 case SWM_ARG_ID_HEIGHTSHRINK:
5799 HEIGHT(win) -= SWM_RESIZE_STEPS;
5802 case SWM_ARG_ID_HEIGHTGROW:
5803 HEIGHT(win) += SWM_RESIZE_STEPS;
5810 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
5811 SWM_CW_HARDBOUNDARY);
5813 store_float_geom(win);
5817 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
5818 SWM_CW_SOFTBOUNDARY);
5821 /* get cursor offset from window root */
5822 xpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
5829 if (xpr->win_x < WIDTH(win) / 2)
5832 if (xpr->win_y < HEIGHT(win) / 2)
5835 if (args->id == SWM_ARG_ID_CENTER)
5836 cursor = cursors[XC_SIZING].cid;
5838 cursor = cursors[left ? XC_TOP_LEFT_CORNER :
5839 XC_TOP_RIGHT_CORNER].cid;
5841 cursor = cursors[left ? XC_BOTTOM_LEFT_CORNER :
5842 XC_BOTTOM_RIGHT_CORNER].cid;
5844 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
5845 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE, cursor,
5850 while (resizing && (evt = xcb_wait_for_event(conn))) {
5851 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
5852 case XCB_BUTTON_RELEASE:
5853 DNPRINTF(SWM_D_EVENT, "resize: BUTTON_RELEASE\n");
5856 case XCB_MOTION_NOTIFY:
5857 mne = (xcb_motion_notify_event_t *)evt;
5858 /* cursor offset/delta from start of the operation */
5859 dx = mne->root_x - xpr->root_x;
5860 dy = mne->root_y - xpr->root_y;
5866 if (args->id == SWM_ARG_ID_CENTER) {
5867 if (g.h / 2 + dy < 1)
5871 HEIGHT(win) = g.h + 2 * dy;
5879 HEIGHT(win) = g.h + dy;
5886 if (args->id == SWM_ARG_ID_CENTER) {
5887 if (g.w / 2 + dx < 1)
5891 WIDTH(win) = g.w + 2 * dx;
5899 WIDTH(win) = g.w + dx;
5902 /* not free, don't sync more than 120 times / second */
5903 if ((mne->time - timestamp) > (1000 / 120) ) {
5904 timestamp = mne->time;
5905 regionize(win, mne->root_x, mne->root_y);
5906 region_containment(win, r, SWM_CW_ALLSIDES |
5907 SWM_CW_RESIZABLE | SWM_CW_HARDBOUNDARY |
5908 SWM_CW_SOFTBOUNDARY);
5916 /* It's possible for win to have been freed above. */
5917 if (validate_win(win)) {
5918 DNPRINTF(SWM_D_EVENT, "resize: invalid win.\n");
5926 region_containment(win, r, SWM_CW_ALLSIDES | SWM_CW_RESIZABLE |
5927 SWM_CW_HARDBOUNDARY | SWM_CW_SOFTBOUNDARY);
5931 store_float_geom(win);
5933 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
5935 DNPRINTF(SWM_D_EVENT, "resize: done.\n");
5939 resize_step(struct swm_region *r, union arg *args)
5941 struct ws_win *win = NULL;
5943 if (r && r->ws && r->ws->focus)
5952 /* Try to set window region based on supplied coordinates or window center. */
5954 regionize(struct ws_win *win, int x, int y)
5956 struct swm_region *r = NULL;
5958 r = region_under(win->s, x, y);
5960 r = region_under(win->s, X(win) + WIDTH(win) / 2,
5961 Y(win) + HEIGHT(win) / 2);
5963 if (r && r != win->ws->r) {
5964 win_to_ws(win, r->ws->idx, 0);
5969 #define SWM_MOVE_STEPS (50)
5972 move(struct ws_win *win, union arg *args)
5974 xcb_timestamp_t timestamp = 0;
5975 int move_stp = 0, moving;
5976 xcb_query_pointer_reply_t *qpr = NULL;
5977 xcb_generic_event_t *evt;
5978 xcb_motion_notify_event_t *mne;
5983 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
5986 DNPRINTF(SWM_D_EVENT, "move: window: 0x%x, floating: %s, transient: "
5987 "0x%x\n", win->id, YESNO(win->floating), win->transient);
5989 /* in max_stack mode should only move transients */
5990 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
5997 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
6001 /* When a stacked win is moved, float it and restack. */
6002 if (!win->floating && !win->transient) {
6003 store_float_geom(win);
6004 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
6011 /* It's possible for win to have been freed during focus_flush(). */
6012 if (validate_win(win)) {
6013 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
6019 case SWM_ARG_ID_MOVELEFT:
6020 X(win) -= (SWM_MOVE_STEPS - border_width);
6023 case SWM_ARG_ID_MOVERIGHT:
6024 X(win) += (SWM_MOVE_STEPS - border_width);
6027 case SWM_ARG_ID_MOVEUP:
6028 Y(win) -= (SWM_MOVE_STEPS - border_width);
6031 case SWM_ARG_ID_MOVEDOWN:
6032 Y(win) += (SWM_MOVE_STEPS - border_width);
6039 regionize(win, -1, -1);
6040 region_containment(win, win->ws->r, SWM_CW_ALLSIDES);
6042 store_float_geom(win);
6046 xcb_grab_pointer(conn, 0, win->id, MOUSEMASK,
6047 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
6048 XCB_WINDOW_NONE, cursors[XC_FLEUR].cid, XCB_CURRENT_TIME);
6050 /* get cursor offset from window root */
6051 qpr = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, win->id),
6054 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
6058 regionize(win, qpr->root_x, qpr->root_y);
6059 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
6060 SWM_CW_SOFTBOUNDARY);
6064 while (moving && (evt = xcb_wait_for_event(conn))) {
6065 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
6066 case XCB_BUTTON_RELEASE:
6067 DNPRINTF(SWM_D_EVENT, "move: BUTTON_RELEASE\n");
6070 case XCB_MOTION_NOTIFY:
6071 mne = (xcb_motion_notify_event_t *)evt;
6072 DNPRINTF(SWM_D_EVENT, "motion: root: 0x%x\n", mne->root);
6073 X(win) = mne->root_x - qpr->win_x - border_width;
6074 Y(win) = mne->root_y - qpr->win_y - border_width;
6076 /* not free, don't sync more than 120 times / second */
6077 if ((mne->time - timestamp) > (1000 / 120) ) {
6078 timestamp = mne->time;
6079 regionize(win, mne->root_x, mne->root_y);
6080 region_containment(win, win->ws->r,
6081 SWM_CW_ALLSIDES | SWM_CW_SOFTBOUNDARY);
6089 /* It's possible for win to have been freed above. */
6090 if (validate_win(win)) {
6091 DNPRINTF(SWM_D_EVENT, "move: invalid win.\n");
6099 region_containment(win, win->ws->r, SWM_CW_ALLSIDES |
6100 SWM_CW_SOFTBOUNDARY);
6104 store_float_geom(win);
6107 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
6108 DNPRINTF(SWM_D_EVENT, "move: done.\n");
6112 move_step(struct swm_region *r, union arg *args)
6114 struct ws_win *win = NULL;
6116 if (r && r->ws && r->ws->focus)
6121 if (!win->transient && !win->floating)
6128 /* key definitions */
6130 char name[SWM_FUNCNAME_LEN];
6131 void (*func)(struct swm_region *r, union arg *);
6133 } keyfuncs[KF_INVALID + 1] = {
6134 /* name function argument */
6135 { "bar_toggle", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE} },
6136 { "bar_toggle_ws", bar_toggle, {.id = SWM_ARG_ID_BAR_TOGGLE_WS} },
6137 { "button2", pressbutton, {2} },
6138 { "cycle_layout", cycle_layout, {0} },
6139 { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
6140 { "float_toggle", floating_toggle,{0} },
6141 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
6142 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
6143 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
6144 { "focus_urgent", focus, {.id = SWM_ARG_ID_FOCUSURGENT} },
6145 { "maximize_toggle", maximize_toggle,{0} },
6146 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
6147 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
6148 { "iconify", iconify, {0} },
6149 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
6150 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
6151 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
6152 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
6153 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
6154 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
6155 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
6156 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
6157 { "mvrg_1", send_to_rg, {.id = 0} },
6158 { "mvrg_2", send_to_rg, {.id = 1} },
6159 { "mvrg_3", send_to_rg, {.id = 2} },
6160 { "mvrg_4", send_to_rg, {.id = 3} },
6161 { "mvrg_5", send_to_rg, {.id = 4} },
6162 { "mvrg_6", send_to_rg, {.id = 5} },
6163 { "mvrg_7", send_to_rg, {.id = 6} },
6164 { "mvrg_8", send_to_rg, {.id = 7} },
6165 { "mvrg_9", send_to_rg, {.id = 8} },
6166 { "mvws_1", send_to_ws, {.id = 0} },
6167 { "mvws_2", send_to_ws, {.id = 1} },
6168 { "mvws_3", send_to_ws, {.id = 2} },
6169 { "mvws_4", send_to_ws, {.id = 3} },
6170 { "mvws_5", send_to_ws, {.id = 4} },
6171 { "mvws_6", send_to_ws, {.id = 5} },
6172 { "mvws_7", send_to_ws, {.id = 6} },
6173 { "mvws_8", send_to_ws, {.id = 7} },
6174 { "mvws_9", send_to_ws, {.id = 8} },
6175 { "mvws_10", send_to_ws, {.id = 9} },
6176 { "mvws_11", send_to_ws, {.id = 10} },
6177 { "mvws_12", send_to_ws, {.id = 11} },
6178 { "mvws_13", send_to_ws, {.id = 12} },
6179 { "mvws_14", send_to_ws, {.id = 13} },
6180 { "mvws_15", send_to_ws, {.id = 14} },
6181 { "mvws_16", send_to_ws, {.id = 15} },
6182 { "mvws_17", send_to_ws, {.id = 16} },
6183 { "mvws_18", send_to_ws, {.id = 17} },
6184 { "mvws_19", send_to_ws, {.id = 18} },
6185 { "mvws_20", send_to_ws, {.id = 19} },
6186 { "mvws_21", send_to_ws, {.id = 20} },
6187 { "mvws_22", send_to_ws, {.id = 21} },
6188 { "name_workspace", name_workspace, {0} },
6189 { "quit", quit, {0} },
6190 { "raise_toggle", raise_toggle, {0} },
6191 { "restart", restart, {0} },
6192 { "rg_1", focusrg, {.id = 0} },
6193 { "rg_2", focusrg, {.id = 1} },
6194 { "rg_3", focusrg, {.id = 2} },
6195 { "rg_4", focusrg, {.id = 3} },
6196 { "rg_5", focusrg, {.id = 4} },
6197 { "rg_6", focusrg, {.id = 5} },
6198 { "rg_7", focusrg, {.id = 6} },
6199 { "rg_8", focusrg, {.id = 7} },
6200 { "rg_9", focusrg, {.id = 8} },
6201 { "rg_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
6202 { "rg_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
6203 { "screen_next", cyclerg, {.id = SWM_ARG_ID_CYCLERG_UP} },
6204 { "screen_prev", cyclerg, {.id = SWM_ARG_ID_CYCLERG_DOWN} },
6205 { "search_win", search_win, {0} },
6206 { "search_workspace", search_workspace, {0} },
6207 { "spawn_custom", NULL, {0} },
6208 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
6209 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
6210 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
6211 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
6212 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
6213 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
6214 { "uniconify", uniconify, {0} },
6215 { "version", version, {0} },
6216 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
6217 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
6218 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
6219 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
6220 { "ws_1", switchws, {.id = 0} },
6221 { "ws_2", switchws, {.id = 1} },
6222 { "ws_3", switchws, {.id = 2} },
6223 { "ws_4", switchws, {.id = 3} },
6224 { "ws_5", switchws, {.id = 4} },
6225 { "ws_6", switchws, {.id = 5} },
6226 { "ws_7", switchws, {.id = 6} },
6227 { "ws_8", switchws, {.id = 7} },
6228 { "ws_9", switchws, {.id = 8} },
6229 { "ws_10", switchws, {.id = 9} },
6230 { "ws_11", switchws, {.id = 10} },
6231 { "ws_12", switchws, {.id = 11} },
6232 { "ws_13", switchws, {.id = 12} },
6233 { "ws_14", switchws, {.id = 13} },
6234 { "ws_15", switchws, {.id = 14} },
6235 { "ws_16", switchws, {.id = 15} },
6236 { "ws_17", switchws, {.id = 16} },
6237 { "ws_18", switchws, {.id = 17} },
6238 { "ws_19", switchws, {.id = 18} },
6239 { "ws_20", switchws, {.id = 19} },
6240 { "ws_21", switchws, {.id = 20} },
6241 { "ws_22", switchws, {.id = 21} },
6242 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
6243 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
6244 { "ws_next_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_UP} },
6245 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
6246 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
6247 { "ws_prev_move", cyclews, {.id = SWM_ARG_ID_CYCLEWS_MOVE_DOWN} },
6248 { "ws_prior", priorws, {0} },
6249 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
6250 { "invalid key func", NULL, {0} },
6254 key_cmp(struct key *kp1, struct key *kp2)
6256 if (kp1->keysym < kp2->keysym)
6258 if (kp1->keysym > kp2->keysym)
6261 if (kp1->mod < kp2->mod)
6263 if (kp1->mod > kp2->mod)
6270 enum { client_click, root_click };
6272 unsigned int action;
6274 unsigned int button;
6275 void (*func)(struct ws_win *, union arg *);
6278 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6279 /* action key mouse button func args */
6280 { client_click, MODKEY, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
6281 { client_click, MODKEY_SHIFT, XCB_BUTTON_INDEX_3, resize, {.id = SWM_ARG_ID_CENTER} },
6282 { client_click, MODKEY, XCB_BUTTON_INDEX_1, move, {0} },
6287 update_modkey(unsigned int mod)
6293 RB_FOREACH(kp, key_tree, &keys)
6294 if (kp->mod & XCB_MOD_MASK_SHIFT)
6295 kp->mod = mod | XCB_MOD_MASK_SHIFT;
6299 for (i = 0; i < LENGTH(buttons); i++)
6300 if (buttons[i].mask & XCB_MOD_MASK_SHIFT)
6301 buttons[i].mask = mod | XCB_MOD_MASK_SHIFT;
6303 buttons[i].mask = mod;
6307 spawn_expand(struct swm_region *r, union arg *args, const char *spawn_name,
6310 struct spawn_prog *prog = NULL;
6312 char *ap, **real_args;
6314 /* suppress unused warning since var is needed */
6317 DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
6320 TAILQ_FOREACH(prog, &spawns, entry) {
6321 if (strcasecmp(spawn_name, prog->name) == 0)
6325 warnx("spawn_custom: program %s not found", spawn_name);
6329 /* make room for expanded args */
6330 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
6331 err(1, "spawn_custom: calloc real_args");
6333 /* expand spawn_args into real_args */
6334 for (i = c = 0; i < prog->argc; i++) {
6336 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
6337 if (strcasecmp(ap, "$bar_border") == 0) {
6339 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
6341 err(1, "spawn_custom border color");
6342 } else if (strcasecmp(ap, "$bar_color") == 0) {
6344 strdup(r->s->c[SWM_S_COLOR_BAR].name))
6346 err(1, "spawn_custom bar color");
6347 } else if (strcasecmp(ap, "$bar_font") == 0) {
6348 if ((real_args[c] = strdup(bar_fonts))
6350 err(1, "spawn_custom bar fonts");
6351 } else if (strcasecmp(ap, "$bar_font_color") == 0) {
6353 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
6355 err(1, "spawn_custom color font");
6356 } else if (strcasecmp(ap, "$color_focus") == 0) {
6358 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
6360 err(1, "spawn_custom color focus");
6361 } else if (strcasecmp(ap, "$color_unfocus") == 0) {
6363 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
6365 err(1, "spawn_custom color unfocus");
6366 } else if (strcasecmp(ap, "$region_index") == 0) {
6367 if (asprintf(&real_args[c], "%d",
6368 get_region_index(r) + 1) < 1)
6369 err(1, "spawn_custom region index");
6370 } else if (strcasecmp(ap, "$workspace_index") == 0) {
6371 if (asprintf(&real_args[c], "%d", r->ws->idx + 1) < 1)
6372 err(1, "spawn_custom workspace index");
6373 } else if (strcasecmp(ap, "$dmenu_bottom") == 0) {
6376 if ((real_args[c] = strdup("-b")) == NULL)
6377 err(1, "spawn_custom workspace index");
6379 /* no match --> copy as is */
6380 if ((real_args[c] = strdup(ap)) == NULL)
6381 err(1, "spawn_custom strdup(ap)");
6383 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
6389 DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
6390 for (i = 0; i < c; ++i)
6391 DPRINTF("\"%s\" ", real_args[i]);
6394 *ret_args = real_args;
6399 spawn_custom(struct swm_region *r, union arg *args, const char *spawn_name)
6405 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
6409 spawn(r->ws->idx, &a, 1);
6411 for (i = 0; i < spawn_argc; i++)
6417 spawn_select(struct swm_region *r, union arg *args, const char *spawn_name,
6424 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
6428 if (pipe(select_list_pipe) == -1)
6429 err(1, "pipe error");
6430 if (pipe(select_resp_pipe) == -1)
6431 err(1, "pipe error");
6433 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
6434 err(1, "could not disable SIGPIPE");
6435 switch (*pid = fork()) {
6437 err(1, "cannot fork");
6440 if (dup2(select_list_pipe[0], STDIN_FILENO) == -1)
6442 if (dup2(select_resp_pipe[1], STDOUT_FILENO) == -1)
6444 close(select_list_pipe[1]);
6445 close(select_resp_pipe[0]);
6446 spawn(r->ws->idx, &a, 0);
6448 default: /* parent */
6449 close(select_list_pipe[0]);
6450 close(select_resp_pipe[1]);
6454 for (i = 0; i < spawn_argc; i++)
6459 /* Argument tokenizer. */
6462 int single_quoted = 0, double_quoted = 0;
6463 char *arg, *cp, *next;
6468 /* Eat and move characters until end of argument is found. */
6469 for (arg = next = cp = *sp; *cp != '\0'; ++cp) {
6470 if (!double_quoted && *cp == '\'') {
6471 /* Eat single-quote. */
6472 single_quoted = !single_quoted;
6473 } else if (!single_quoted && *cp == '"') {
6474 /* Eat double-quote. */
6475 double_quoted = !double_quoted;
6476 } else if (!single_quoted && *cp == '\\' && *(cp + 1) == '"') {
6477 /* Eat backslash; copy escaped character to arg. */
6479 } else if (!single_quoted && !double_quoted && *cp == '\\' &&
6480 (*(cp + 1) == '\'' || *(cp + 1) == ' ')) {
6481 /* Eat backslash; move escaped character. */
6483 } else if (!single_quoted && !double_quoted &&
6484 (*cp == ' ' || *cp == '\t')) {
6485 /* Terminate argument. */
6487 /* Point sp to beginning of next argument. */
6491 /* Move regular character. */
6496 /* Terminate argument if end of string. */
6506 spawn_insert(const char *name, const char *args, int flags)
6508 struct spawn_prog *sp;
6509 char *arg, *dup, *ptr;
6511 DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s[%s]\n", name, args);
6513 if (args == NULL || *args == '\0')
6516 if ((sp = calloc(1, sizeof *sp)) == NULL)
6517 err(1, "spawn_insert: calloc");
6518 if ((sp->name = strdup(name)) == NULL)
6519 err(1, "spawn_insert: strdup");
6521 /* Convert the arguments to an argument list. */
6522 if ((ptr = dup = strdup(args)) == NULL)
6523 err(1, "spawn_insert: strdup");
6524 while ((arg = argsep(&ptr)) != NULL) {
6525 /* Null argument; skip it. */
6530 if ((sp->argv = realloc(sp->argv, sp->argc *
6531 sizeof *sp->argv)) == NULL)
6532 err(1, "spawn_insert: realloc");
6533 if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
6534 err(1, "spawn_insert: strdup");
6540 DNPRINTF(SWM_D_SPAWN, "arg %d: [%s]\n", sp->argc, sp->argv[sp->argc-1]);
6541 TAILQ_INSERT_TAIL(&spawns, sp, entry);
6542 DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
6546 spawn_remove(struct spawn_prog *sp)
6550 DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
6552 TAILQ_REMOVE(&spawns, sp, entry);
6553 for (i = 0; i < sp->argc; i++)
6559 DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
6563 spawn_find(const char *name)
6565 struct spawn_prog *sp;
6567 TAILQ_FOREACH(sp, &spawns, entry)
6568 if (strcasecmp(sp->name, name) == 0)
6575 setspawn(const char *name, const char *args, int flags)
6577 struct spawn_prog *sp;
6579 DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
6584 /* Remove any old spawn under the same name. */
6585 if ((sp = spawn_find(name)) != NULL)
6589 spawn_insert(name, args, flags);
6591 warnx("error: setspawn: cannot find program: %s", name);
6593 DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
6597 setconfspawn(const char *selector, const char *value, int flags)
6601 if (selector == NULL || strlen(selector) == 0)
6604 args = expand_tilde(value);
6606 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, args);
6608 setspawn(selector, args, flags);
6611 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done.\n");
6616 validate_spawns(void)
6618 struct spawn_prog *sp;
6619 char which[PATH_MAX];
6624 RB_FOREACH(kp, key_tree, &keys) {
6625 if (kp->funcid != KF_SPAWN_CUSTOM)
6629 sp = spawn_find(kp->spawn_name);
6630 if (sp == NULL || sp->flags & SWM_SPAWN_OPTIONAL)
6633 /* verify we have the goods */
6634 snprintf(which, sizeof which, "which %s", sp->argv[0]);
6635 DNPRINTF(SWM_D_CONF, "validate_spawns: which %s\n",
6637 for (i = strlen("which "); i < strlen(which); i++)
6638 if (which[i] == ' ') {
6642 if (system(which) != 0)
6643 add_startup_exception("could not find %s",
6644 &which[strlen("which ")]);
6651 setconfspawn("lock", "xlock", 0);
6653 setconfspawn("term", "xterm", 0);
6654 setconfspawn("spawn_term", "xterm", 0);
6656 setconfspawn("menu", "dmenu_run"
6660 " -nf $bar_font_color"
6662 " -sf $bar_color", 0);
6664 setconfspawn("search", "dmenu"
6669 " -nf $bar_font_color"
6671 " -sf $bar_color", 0);
6673 setconfspawn("name_workspace", "dmenu"
6678 " -nf $bar_font_color"
6680 " -sf $bar_color", 0);
6682 /* These are not verified for existence, even with a binding set. */
6683 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL);
6684 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL);
6685 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL);
6689 #define SWM_MODNAME_SIZE 32
6690 #define SWM_KEY_WS "\n+ \t"
6692 parsekeys(const char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
6694 char *str, *cp, *name;
6697 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
6698 if (mod == NULL || ks == NULL) {
6699 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
6702 if (keystr == NULL || strlen(keystr) == 0) {
6703 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
6707 if ((cp = str = strdup(keystr)) == NULL)
6708 err(1, "parsekeys: strdup");
6710 *ks = XCB_NO_SYMBOL;
6712 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
6713 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
6715 cp += (long)strspn(cp, SWM_KEY_WS);
6716 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
6718 else if (strncasecmp(name, "Mod1", SWM_MODNAME_SIZE) == 0)
6719 *mod |= XCB_MOD_MASK_1;
6720 else if (strncasecmp(name, "Mod2", SWM_MODNAME_SIZE) == 0)
6721 *mod += XCB_MOD_MASK_2;
6722 else if (strncmp(name, "Mod3", SWM_MODNAME_SIZE) == 0)
6723 *mod |= XCB_MOD_MASK_3;
6724 else if (strncmp(name, "Mod4", SWM_MODNAME_SIZE) == 0)
6725 *mod |= XCB_MOD_MASK_4;
6726 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
6727 *mod |= XCB_MOD_MASK_SHIFT;
6728 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
6729 *mod |= XCB_MOD_MASK_CONTROL;
6731 *ks = XStringToKeysym(name);
6732 XConvertCase(*ks, ks, &uks);
6733 if (ks == XCB_NO_SYMBOL) {
6735 "parsekeys: invalid key %s\n",
6744 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
6749 strdupsafe(const char *str)
6754 return (strdup(str));
6758 key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid,
6759 const char *spawn_name)
6763 DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
6764 keyfuncs[kfid].name, spawn_name);
6766 if ((kp = malloc(sizeof *kp)) == NULL)
6767 err(1, "key_insert: malloc");
6772 kp->spawn_name = strdupsafe(spawn_name);
6773 RB_INSERT(key_tree, &keys, kp);
6775 DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
6779 key_lookup(unsigned int mod, KeySym ks)
6786 return (RB_FIND(key_tree, &keys, &kp));
6790 key_remove(struct key *kp)
6792 DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
6794 RB_REMOVE(key_tree, &keys, kp);
6795 free(kp->spawn_name);
6798 DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
6802 key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
6803 const char *spawn_name)
6805 DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
6809 key_insert(mod, ks, kfid, spawn_name);
6811 DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
6815 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
6816 const char *spawn_name)
6820 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
6821 keyfuncs[kfid].name, spawn_name);
6823 if ((kp = key_lookup(mod, ks)) != NULL) {
6824 if (kfid == KF_INVALID)
6827 key_replace(kp, mod, ks, kfid, spawn_name);
6828 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6831 if (kfid == KF_INVALID) {
6832 warnx("bind: Key combination already unbound.");
6833 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6837 key_insert(mod, ks, kfid, spawn_name);
6838 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
6842 setconfbinding(const char *selector, const char *value, int flags)
6844 enum keyfuncid kfid;
6847 struct spawn_prog *sp;
6849 /* suppress unused warning since var is needed */
6852 DNPRINTF(SWM_D_KEY, "setconfbinding: enter selector: [%s], "
6853 "value: [%s]\n", selector, value);
6854 if (selector == NULL || strlen(selector) == 0) {
6855 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
6856 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6858 setkeybinding(mod, ks, kfid, NULL);
6863 /* search by key function name */
6864 for (kfid = 0; kfid < KF_INVALID; (kfid)++) {
6865 if (strncasecmp(selector, keyfuncs[kfid].name,
6866 SWM_FUNCNAME_LEN) == 0) {
6867 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
6868 "keyfunc\n", selector);
6869 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6870 setkeybinding(mod, ks, kfid, NULL);
6876 /* search by custom spawn name */
6877 if ((sp = spawn_find(selector)) != NULL) {
6878 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match "
6879 "spawn\n", selector);
6880 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
6881 setkeybinding(mod, ks, KF_SPAWN_CUSTOM,
6887 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
6894 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6895 setkeybinding(MODKEY, XK_b, KF_BAR_TOGGLE, NULL);
6896 setkeybinding(MODKEY_SHIFT, XK_b, KF_BAR_TOGGLE_WS,NULL);
6897 setkeybinding(MODKEY, XK_v, KF_BUTTON2, NULL);
6898 setkeybinding(MODKEY, XK_space, KF_CYCLE_LAYOUT,NULL);
6899 setkeybinding(MODKEY_SHIFT, XK_backslash, KF_FLIP_LAYOUT, NULL);
6900 setkeybinding(MODKEY, XK_t, KF_FLOAT_TOGGLE,NULL);
6901 setkeybinding(MODKEY, XK_m, KF_FOCUS_MAIN, NULL);
6902 setkeybinding(MODKEY, XK_j, KF_FOCUS_NEXT, NULL);
6903 setkeybinding(MODKEY, XK_Tab, KF_FOCUS_NEXT, NULL);
6904 setkeybinding(MODKEY, XK_k, KF_FOCUS_PREV, NULL);
6905 setkeybinding(MODKEY_SHIFT, XK_Tab, KF_FOCUS_PREV, NULL);
6906 setkeybinding(MODKEY, XK_u, KF_FOCUS_URGENT,NULL);
6907 setkeybinding(MODKEY, XK_e, KF_MAXIMIZE_TOGGLE,NULL);
6908 setkeybinding(MODKEY_SHIFT, XK_equal, KF_HEIGHT_GROW,NULL);
6909 setkeybinding(MODKEY_SHIFT, XK_minus, KF_HEIGHT_SHRINK,NULL);
6910 setkeybinding(MODKEY, XK_w, KF_ICONIFY, NULL);
6911 setkeybinding(MODKEY, XK_h, KF_MASTER_SHRINK, NULL);
6912 setkeybinding(MODKEY, XK_l, KF_MASTER_GROW, NULL);
6913 setkeybinding(MODKEY, XK_comma, KF_MASTER_ADD, NULL);
6914 setkeybinding(MODKEY, XK_period, KF_MASTER_DEL, NULL);
6915 setkeybinding(MODKEY_SHIFT, XK_bracketright,KF_MOVE_DOWN,NULL);
6916 setkeybinding(MODKEY, XK_bracketleft, KF_MOVE_LEFT,NULL);
6917 setkeybinding(MODKEY, XK_bracketright,KF_MOVE_RIGHT,NULL);
6918 setkeybinding(MODKEY_SHIFT, XK_bracketleft, KF_MOVE_UP, NULL);
6919 setkeybinding(MODKEY_SHIFT, XK_KP_End, KF_MVRG_1, NULL);
6920 setkeybinding(MODKEY_SHIFT, XK_KP_Down, KF_MVRG_2, NULL);
6921 setkeybinding(MODKEY_SHIFT, XK_KP_Next, KF_MVRG_3, NULL);
6922 setkeybinding(MODKEY_SHIFT, XK_KP_Left, KF_MVRG_4, NULL);
6923 setkeybinding(MODKEY_SHIFT, XK_KP_Begin, KF_MVRG_5, NULL);
6924 setkeybinding(MODKEY_SHIFT, XK_KP_Right, KF_MVRG_6, NULL);
6925 setkeybinding(MODKEY_SHIFT, XK_KP_Home, KF_MVRG_7, NULL);
6926 setkeybinding(MODKEY_SHIFT, XK_KP_Up, KF_MVRG_8, NULL);
6927 setkeybinding(MODKEY_SHIFT, XK_KP_Prior, KF_MVRG_9, NULL);
6928 setkeybinding(MODKEY_SHIFT, XK_1, KF_MVWS_1, NULL);
6929 setkeybinding(MODKEY_SHIFT, XK_2, KF_MVWS_2, NULL);
6930 setkeybinding(MODKEY_SHIFT, XK_3, KF_MVWS_3, NULL);
6931 setkeybinding(MODKEY_SHIFT, XK_4, KF_MVWS_4, NULL);
6932 setkeybinding(MODKEY_SHIFT, XK_5, KF_MVWS_5, NULL);
6933 setkeybinding(MODKEY_SHIFT, XK_6, KF_MVWS_6, NULL);
6934 setkeybinding(MODKEY_SHIFT, XK_7, KF_MVWS_7, NULL);
6935 setkeybinding(MODKEY_SHIFT, XK_8, KF_MVWS_8, NULL);
6936 setkeybinding(MODKEY_SHIFT, XK_9, KF_MVWS_9, NULL);
6937 setkeybinding(MODKEY_SHIFT, XK_0, KF_MVWS_10, NULL);
6938 setkeybinding(MODKEY_SHIFT, XK_F1, KF_MVWS_11, NULL);
6939 setkeybinding(MODKEY_SHIFT, XK_F2, KF_MVWS_12, NULL);
6940 setkeybinding(MODKEY_SHIFT, XK_F3, KF_MVWS_13, NULL);
6941 setkeybinding(MODKEY_SHIFT, XK_F4, KF_MVWS_14, NULL);
6942 setkeybinding(MODKEY_SHIFT, XK_F5, KF_MVWS_15, NULL);
6943 setkeybinding(MODKEY_SHIFT, XK_F6, KF_MVWS_16, NULL);
6944 setkeybinding(MODKEY_SHIFT, XK_F7, KF_MVWS_17, NULL);
6945 setkeybinding(MODKEY_SHIFT, XK_F8, KF_MVWS_18, NULL);
6946 setkeybinding(MODKEY_SHIFT, XK_F9, KF_MVWS_19, NULL);
6947 setkeybinding(MODKEY_SHIFT, XK_F10, KF_MVWS_20, NULL);
6948 setkeybinding(MODKEY_SHIFT, XK_F11, KF_MVWS_21, NULL);
6949 setkeybinding(MODKEY_SHIFT, XK_F12, KF_MVWS_22, NULL);
6950 setkeybinding(MODKEY_SHIFT, XK_slash, KF_NAME_WORKSPACE,NULL);
6951 setkeybinding(MODKEY_SHIFT, XK_q, KF_QUIT, NULL);
6952 setkeybinding(MODKEY_SHIFT, XK_r, KF_RAISE_TOGGLE,NULL);
6953 setkeybinding(MODKEY, XK_q, KF_RESTART, NULL);
6954 setkeybinding(MODKEY, XK_KP_End, KF_RG_1, NULL);
6955 setkeybinding(MODKEY, XK_KP_Down, KF_RG_2, NULL);
6956 setkeybinding(MODKEY, XK_KP_Next, KF_RG_3, NULL);
6957 setkeybinding(MODKEY, XK_KP_Left, KF_RG_4, NULL);
6958 setkeybinding(MODKEY, XK_KP_Begin, KF_RG_5, NULL);
6959 setkeybinding(MODKEY, XK_KP_Right, KF_RG_6, NULL);
6960 setkeybinding(MODKEY, XK_KP_Home, KF_RG_7, NULL);
6961 setkeybinding(MODKEY, XK_KP_Up, KF_RG_8, NULL);
6962 setkeybinding(MODKEY, XK_KP_Prior, KF_RG_9, NULL);
6963 setkeybinding(MODKEY_SHIFT, XK_Right, KF_RG_NEXT, NULL);
6964 setkeybinding(MODKEY_SHIFT, XK_Left, KF_RG_PREV, NULL);
6965 setkeybinding(MODKEY, XK_f, KF_SEARCH_WIN, NULL);
6966 setkeybinding(MODKEY, XK_slash, KF_SEARCH_WORKSPACE,NULL);
6967 setkeybinding(MODKEY_SHIFT, XK_i, KF_SPAWN_CUSTOM,"initscr");
6968 setkeybinding(MODKEY_SHIFT, XK_Delete, KF_SPAWN_CUSTOM,"lock");
6969 setkeybinding(MODKEY, XK_p, KF_SPAWN_CUSTOM,"menu");
6970 setkeybinding(MODKEY, XK_s, KF_SPAWN_CUSTOM,"screenshot_all");
6971 setkeybinding(MODKEY_SHIFT, XK_s, KF_SPAWN_CUSTOM,"screenshot_wind");
6972 setkeybinding(MODKEY_SHIFT, XK_Return, KF_SPAWN_CUSTOM,"term");
6973 setkeybinding(MODKEY_SHIFT, XK_comma, KF_STACK_INC, NULL);
6974 setkeybinding(MODKEY_SHIFT, XK_period, KF_STACK_DEC, NULL);
6975 setkeybinding(MODKEY_SHIFT, XK_space, KF_STACK_RESET, NULL);
6976 setkeybinding(MODKEY, XK_Return, KF_SWAP_MAIN, NULL);
6977 setkeybinding(MODKEY_SHIFT, XK_j, KF_SWAP_NEXT, NULL);
6978 setkeybinding(MODKEY_SHIFT, XK_k, KF_SWAP_PREV, NULL);
6979 setkeybinding(MODKEY_SHIFT, XK_w, KF_UNICONIFY, NULL);
6980 setkeybinding(MODKEY_SHIFT, XK_v, KF_VERSION, NULL);
6981 setkeybinding(MODKEY, XK_equal, KF_WIDTH_GROW, NULL);
6982 setkeybinding(MODKEY, XK_minus, KF_WIDTH_SHRINK,NULL);
6983 setkeybinding(MODKEY, XK_x, KF_WIND_DEL, NULL);
6984 setkeybinding(MODKEY_SHIFT, XK_x, KF_WIND_KILL, NULL);
6985 setkeybinding(MODKEY, XK_1, KF_WS_1, NULL);
6986 setkeybinding(MODKEY, XK_2, KF_WS_2, NULL);
6987 setkeybinding(MODKEY, XK_3, KF_WS_3, NULL);
6988 setkeybinding(MODKEY, XK_4, KF_WS_4, NULL);
6989 setkeybinding(MODKEY, XK_5, KF_WS_5, NULL);
6990 setkeybinding(MODKEY, XK_6, KF_WS_6, NULL);
6991 setkeybinding(MODKEY, XK_7, KF_WS_7, NULL);
6992 setkeybinding(MODKEY, XK_8, KF_WS_8, NULL);
6993 setkeybinding(MODKEY, XK_9, KF_WS_9, NULL);
6994 setkeybinding(MODKEY, XK_0, KF_WS_10, NULL);
6995 setkeybinding(MODKEY, XK_F1, KF_WS_11, NULL);
6996 setkeybinding(MODKEY, XK_F2, KF_WS_12, NULL);
6997 setkeybinding(MODKEY, XK_F3, KF_WS_13, NULL);
6998 setkeybinding(MODKEY, XK_F4, KF_WS_14, NULL);
6999 setkeybinding(MODKEY, XK_F5, KF_WS_15, NULL);
7000 setkeybinding(MODKEY, XK_F6, KF_WS_16, NULL);
7001 setkeybinding(MODKEY, XK_F7, KF_WS_17, NULL);
7002 setkeybinding(MODKEY, XK_F8, KF_WS_18, NULL);
7003 setkeybinding(MODKEY, XK_F9, KF_WS_19, NULL);
7004 setkeybinding(MODKEY, XK_F10, KF_WS_20, NULL);
7005 setkeybinding(MODKEY, XK_F11, KF_WS_21, NULL);
7006 setkeybinding(MODKEY, XK_F12, KF_WS_22, NULL);
7007 setkeybinding(MODKEY, XK_Right, KF_WS_NEXT, NULL);
7008 setkeybinding(MODKEY, XK_Left, KF_WS_PREV, NULL);
7009 setkeybinding(MODKEY, XK_Up, KF_WS_NEXT_ALL, NULL);
7010 setkeybinding(MODKEY, XK_Down, KF_WS_PREV_ALL, NULL);
7011 setkeybinding(MODKEY_SHIFT, XK_Up, KF_WS_NEXT_MOVE,NULL);
7012 setkeybinding(MODKEY_SHIFT, XK_Down, KF_WS_PREV_MOVE,NULL);
7013 setkeybinding(MODKEY, XK_a, KF_WS_PRIOR, NULL);
7015 setkeybinding(MODKEY_SHIFT, XK_d, KF_DUMPWINS, NULL);
7025 while (RB_EMPTY(&keys) == 0) {
7026 kp = RB_ROOT(&keys);
7032 setkeymapping(const char *selector, const char *value, int flags)
7034 char *keymapping_file;
7036 /* suppress unused warnings since vars are needed */
7040 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
7042 keymapping_file = expand_tilde(value);
7045 /* load new key bindings; if it fails, revert to default bindings */
7046 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
7051 free(keymapping_file);
7053 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
7058 updatenumlockmask(void)
7061 xcb_get_modifier_mapping_reply_t *modmap_r;
7062 xcb_keycode_t *modmap, kc, *keycode;
7066 modmap_r = xcb_get_modifier_mapping_reply(conn,
7067 xcb_get_modifier_mapping(conn),
7070 modmap = xcb_get_modifier_mapping_keycodes(modmap_r);
7071 for (i = 0; i < 8; i++) {
7072 for (j = 0; j < modmap_r->keycodes_per_modifier; j++) {
7073 kc = modmap[i * modmap_r->keycodes_per_modifier
7075 keycode = xcb_key_symbols_get_keycode(syms,
7079 numlockmask = (1 << i);
7086 DNPRINTF(SWM_D_MISC, "updatenumlockmask: %d\n", numlockmask);
7093 int num_screens, k, j;
7094 unsigned int modifiers[4];
7095 xcb_keycode_t *code;
7097 DNPRINTF(SWM_D_MISC, "grabkeys\n");
7098 updatenumlockmask();
7101 modifiers[1] = numlockmask;
7102 modifiers[2] = XCB_MOD_MASK_LOCK;
7103 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
7105 num_screens = get_screen_count();
7106 for (k = 0; k < num_screens; k++) {
7107 if (TAILQ_EMPTY(&screens[k].rl))
7109 xcb_ungrab_key(conn, XCB_GRAB_ANY, screens[k].root,
7111 RB_FOREACH(kp, key_tree, &keys) {
7112 /* Skip unused ws binds. */
7113 if ((int)kp->funcid > KF_WS_1 + workspace_limit - 1 &&
7114 kp->funcid <= KF_WS_22)
7117 /* Skip unused mvws binds. */
7118 if ((int)kp->funcid > KF_MVWS_1 + workspace_limit - 1 &&
7119 kp->funcid <= KF_MVWS_22)
7122 if ((code = xcb_key_symbols_get_keycode(syms,
7124 for (j = 0; j < LENGTH(modifiers); j++)
7125 xcb_grab_key(conn, 1,
7127 kp->mod | modifiers[j],
7128 *code, XCB_GRAB_MODE_SYNC,
7129 XCB_GRAB_MODE_SYNC);
7137 grabbuttons(struct ws_win *win)
7139 unsigned int modifiers[4];
7142 DNPRINTF(SWM_D_MOUSE, "grabbuttons: win 0x%x\n", win->id);
7143 updatenumlockmask();
7146 modifiers[1] = numlockmask;
7147 modifiers[2] = XCB_MOD_MASK_LOCK;
7148 modifiers[3] = numlockmask | XCB_MOD_MASK_LOCK;
7150 for (i = 0; i < LENGTH(buttons); i++)
7151 if (buttons[i].action == client_click)
7152 for (j = 0; j < LENGTH(modifiers); ++j)
7153 xcb_grab_button(conn, 0, win->id, BUTTONMASK,
7154 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC,
7155 XCB_WINDOW_NONE, XCB_CURSOR_NONE,
7156 buttons[i].button, buttons[i].mask |
7160 const char *quirkname[] = {
7161 "NONE", /* config string for "no value" */
7169 "FOCUSONMAP_SINGLE",
7172 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
7173 #define SWM_Q_WS "\n|+ \t"
7175 parsequirks(const char *qstr, unsigned long *quirk)
7177 char *str, *cp, *name;
7180 if (quirk == NULL || qstr == NULL)
7183 if ((str = strdup(qstr)) == NULL)
7184 err(1, "parsequirks: strdup");
7188 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
7190 cp += (long)strspn(cp, SWM_Q_WS);
7191 for (i = 0; i < LENGTH(quirkname); i++) {
7192 if (strncasecmp(name, quirkname[i],
7193 SWM_QUIRK_LEN) == 0) {
7194 DNPRINTF(SWM_D_QUIRK,
7195 "parsequirks: %s\n", name);
7201 *quirk |= 1 << (i-1);
7205 if (i >= LENGTH(quirkname)) {
7206 DNPRINTF(SWM_D_QUIRK,
7207 "parsequirks: invalid quirk [%s]\n", name);
7218 quirk_insert(const char *class, const char *instance, const char *name,
7219 unsigned long quirk)
7225 DNPRINTF(SWM_D_QUIRK, "quirk_insert: class: %s, instance: %s, name: %s,"
7226 " value: %lu\n", class, instance, name, quirk);
7228 if ((qp = malloc(sizeof *qp)) == NULL)
7229 err(1, "quirk_insert: malloc");
7231 if ((qp->class = strdup(class)) == NULL)
7232 err(1, "quirk_insert: strdup");
7233 if ((qp->instance = strdup(instance)) == NULL)
7234 err(1, "quirk_insert: strdup");
7235 if ((qp->name = strdup(name)) == NULL)
7236 err(1, "quirk_insert: strdup");
7238 if (asprintf(&str, "^%s$", class) == -1)
7239 err(1, "quirk_insert: asprintf");
7240 if (regcomp(&qp->regex_class, str, REG_EXTENDED | REG_NOSUB)) {
7241 add_startup_exception("regex failed to compile quirk 'class' "
7242 "field: %s", class);
7245 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
7248 if (asprintf(&str, "^%s$", instance) == -1)
7249 err(1, "quirk_insert: asprintf");
7250 if (regcomp(&qp->regex_instance, str, REG_EXTENDED | REG_NOSUB)) {
7251 add_startup_exception("regex failed to compile quirk 'instance'"
7252 " field: %s", instance);
7255 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
7258 if (asprintf(&str, "^%s$", name) == -1)
7259 err(1, "quirk_insert: asprintf");
7260 if (regcomp(&qp->regex_name, str, REG_EXTENDED | REG_NOSUB)) {
7261 add_startup_exception("regex failed to compile quirk 'name' "
7265 DNPRINTF(SWM_D_QUIRK, "quirk_insert: compiled: %s\n", str);
7269 DNPRINTF(SWM_D_QUIRK, "quirk_insert: regex error; skipping.\n");
7273 TAILQ_INSERT_TAIL(&quirks, qp, entry);
7275 DNPRINTF(SWM_D_QUIRK, "quirk_insert: leave\n");
7279 quirk_remove(struct quirk *qp)
7281 DNPRINTF(SWM_D_QUIRK, "quirk_remove: %s:%s [%lu]\n", qp->class,
7282 qp->name, qp->quirk);
7284 TAILQ_REMOVE(&quirks, qp, entry);
7287 DNPRINTF(SWM_D_QUIRK, "quirk_remove: leave\n");
7291 quirk_free(struct quirk *qp)
7293 regfree(&qp->regex_class);
7294 regfree(&qp->regex_instance);
7295 regfree(&qp->regex_name);
7303 quirk_replace(struct quirk *qp, const char *class, const char *instance,
7304 const char *name, unsigned long quirk)
7306 DNPRINTF(SWM_D_QUIRK, "quirk_replace: %s:%s:%s [%lu]\n", qp->class,
7307 qp->instance, qp->name, qp->quirk);
7310 quirk_insert(class, instance, name, quirk);
7312 DNPRINTF(SWM_D_QUIRK, "quirk_replace: leave\n");
7316 setquirk(const char *class, const char *instance, const char *name,
7317 unsigned long quirk)
7321 DNPRINTF(SWM_D_QUIRK, "setquirk: enter %s:%s:%s [%lu]\n", class,
7322 instance, name, quirk);
7324 /* Remove/replace existing quirk. */
7325 TAILQ_FOREACH(qp, &quirks, entry) {
7326 if (strcmp(qp->class, class) == 0 &&
7327 strcmp(qp->instance, instance) == 0 &&
7328 strcmp(qp->name, name) == 0) {
7332 quirk_replace(qp, class, instance, name, quirk);
7333 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
7338 /* Only insert if quirk is not NONE. */
7340 quirk_insert(class, instance, name, quirk);
7342 DNPRINTF(SWM_D_QUIRK, "setquirk: leave\n");
7345 /* Eat '\' in str used to escape square brackets and colon. */
7347 unescape_selector(char *str)
7351 for (cp = str; *str != '\0'; ++str, ++cp) {
7352 if (*str == '\\' && (*(str + 1) == ':' || *(str + 1) == ']' ||
7362 setconfquirk(const char *selector, const char *value, int flags)
7364 char *str, *cp, *class;
7365 char *instance = NULL, *name = NULL;
7366 int retval, count = 0;
7369 /* suppress unused warning since var is needed */
7372 if (selector == NULL || strlen(selector) == 0)
7375 if ((str = strdup(selector)) == NULL)
7376 err(1, "setconfquirk: strdup");
7378 /* Find non-escaped colon. */
7385 for (++cp; *cp != '\0'; ++cp) {
7386 if (*cp == ':' && *(cp - 1) != '\\') {
7392 unescape_selector(class);
7394 instance = class + strlen(class) + 1;
7395 unescape_selector(instance);
7401 name = instance + strlen(instance) + 1;
7402 unescape_selector(name);
7407 DNPRINTF(SWM_D_CONF, "setconfquirk: class: %s, instance: %s, "
7408 "name: %s\n", class, instance, name);
7410 if ((retval = parsequirks(value, &qrks)) == 0)
7411 setquirk(class, instance, name, qrks);
7420 setquirk("MPlayer", "xv", ".*", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
7421 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*", SWM_Q_FLOAT);
7422 setquirk("Firefox-bin", "firefox-bin", ".*", SWM_Q_TRANSSZ);
7423 setquirk("Firefox", "Dialog", ".*", SWM_Q_FLOAT);
7424 setquirk("Gimp", "gimp", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7425 setquirk("XTerm", "xterm", ".*", SWM_Q_XTERM_FONTADJ);
7426 setquirk("xine", "Xine Window", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7427 setquirk("Xitk", "Xitk Combo", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7428 setquirk("xine", "xine Panel", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7429 setquirk("Xitk", "Xine Window", ".*", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
7430 setquirk("xine", "xine Video Fullscreen Window", ".*", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
7431 setquirk("pcb", "pcb", ".*", SWM_Q_FLOAT);
7432 setquirk("SDL_App", "SDL_App", ".*", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
7435 /* conf file stuff */
7436 #define SWM_CONF_FILE "spectrwm.conf"
7437 #define SWM_CONF_FILE_OLD "scrotwm.conf"
7441 SWM_S_BAR_AT_BOTTOM,
7442 SWM_S_BAR_BORDER_WIDTH,
7445 SWM_S_BAR_ENABLED_WS,
7450 SWM_S_BOUNDARY_WIDTH,
7451 SWM_S_CLOCK_ENABLED,
7454 SWM_S_CYCLE_VISIBLE,
7456 SWM_S_DISABLE_BORDER,
7458 SWM_S_FOCUS_CLOSE_WRAP,
7459 SWM_S_FOCUS_DEFAULT,
7461 SWM_S_ICONIC_ENABLED,
7462 SWM_S_REGION_PADDING,
7467 SWM_S_STACK_ENABLED,
7470 SWM_S_URGENT_ENABLED,
7471 SWM_S_VERBOSE_LAYOUT,
7472 SWM_S_WINDOW_CLASS_ENABLED,
7473 SWM_S_WINDOW_INSTANCE_ENABLED,
7474 SWM_S_WINDOW_NAME_ENABLED,
7475 SWM_S_WORKSPACE_LIMIT
7479 setconfvalue(const char *selector, const char *value, int flags)
7481 struct workspace *ws;
7482 int i, ws_id, num_screens;
7486 case SWM_S_BAR_ACTION:
7488 if ((bar_argv[0] = expand_tilde(value)) == NULL)
7489 err(1, "setconfvalue: bar_action");
7491 case SWM_S_BAR_AT_BOTTOM:
7492 bar_at_bottom = atoi(value);
7494 case SWM_S_BAR_BORDER_WIDTH:
7495 bar_border_width = atoi(value);
7496 if (bar_border_width < 0)
7497 bar_border_width = 0;
7499 case SWM_S_BAR_DELAY:
7500 /* No longer needed; leave to not break old conf files. */
7502 case SWM_S_BAR_ENABLED:
7503 bar_enabled = atoi(value);
7505 case SWM_S_BAR_ENABLED_WS:
7506 ws_id = atoi(selector) - 1;
7507 if (ws_id < 0 || ws_id >= workspace_limit)
7508 errx(1, "setconfvalue: bar_enabled_ws: invalid "
7509 "workspace %d.", ws_id + 1);
7511 num_screens = get_screen_count();
7512 for (i = 0; i < num_screens; i++) {
7513 ws = (struct workspace *)&screens[i].ws;
7514 ws[ws_id].bar_enabled = atoi(value);
7517 case SWM_S_BAR_FONT:
7519 if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
7520 err(1, "setconfvalue: asprintf: failed to allocate "
7521 "memory for bar_fonts.");
7524 /* If already in xft mode, then we are done. */
7525 if (!bar_font_legacy)
7528 if ((str = strdup(value)) == NULL)
7529 err(1, "setconfvalue: strdup");
7531 /* If there are any non-XLFD entries, switch to Xft mode. */
7532 while ((b = strsep(&str, ",")) != NULL) {
7536 bar_font_legacy = 0;
7543 case SWM_S_BAR_FORMAT:
7545 if ((bar_format = strdup(value)) == NULL)
7546 err(1, "setconfvalue: bar_format");
7548 case SWM_S_BAR_JUSTIFY:
7549 if (strcmp(value, "left") == 0)
7550 bar_justify = SWM_BAR_JUSTIFY_LEFT;
7551 else if (strcmp(value, "center") == 0)
7552 bar_justify = SWM_BAR_JUSTIFY_CENTER;
7553 else if (strcmp(value, "right") == 0)
7554 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
7556 errx(1, "invalid bar_justify");
7558 case SWM_S_BORDER_WIDTH:
7559 border_width = atoi(value);
7560 if (border_width < 0)
7563 case SWM_S_BOUNDARY_WIDTH:
7564 boundary_width = atoi(value);
7565 if (boundary_width < 0)
7568 case SWM_S_CLOCK_ENABLED:
7569 clock_enabled = atoi(value);
7571 case SWM_S_CLOCK_FORMAT:
7572 #ifndef SWM_DENY_CLOCK_FORMAT
7574 if ((clock_format = strdup(value)) == NULL)
7575 err(1, "setconfvalue: clock_format");
7578 case SWM_S_CYCLE_EMPTY:
7579 cycle_empty = atoi(value);
7581 case SWM_S_CYCLE_VISIBLE:
7582 cycle_visible = atoi(value);
7584 case SWM_S_DIALOG_RATIO:
7585 dialog_ratio = atof(value);
7586 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
7589 case SWM_S_DISABLE_BORDER:
7590 disable_border = atoi(value);
7592 case SWM_S_FOCUS_CLOSE:
7593 if (strcmp(value, "first") == 0)
7594 focus_close = SWM_STACK_BOTTOM;
7595 else if (strcmp(value, "last") == 0)
7596 focus_close = SWM_STACK_TOP;
7597 else if (strcmp(value, "next") == 0)
7598 focus_close = SWM_STACK_ABOVE;
7599 else if (strcmp(value, "previous") == 0)
7600 focus_close = SWM_STACK_BELOW;
7602 errx(1, "focus_close");
7604 case SWM_S_FOCUS_CLOSE_WRAP:
7605 focus_close_wrap = atoi(value);
7607 case SWM_S_FOCUS_DEFAULT:
7608 if (strcmp(value, "last") == 0)
7609 focus_default = SWM_STACK_TOP;
7610 else if (strcmp(value, "first") == 0)
7611 focus_default = SWM_STACK_BOTTOM;
7613 errx(1, "focus_default");
7615 case SWM_S_FOCUS_MODE:
7616 if (strcmp(value, "default") == 0)
7617 focus_mode = SWM_FOCUS_DEFAULT;
7618 else if (strcmp(value, "follow") == 0 ||
7619 strcmp(value, "follow_cursor") == 0)
7620 focus_mode = SWM_FOCUS_FOLLOW;
7621 else if (strcmp(value, "manual") == 0)
7622 focus_mode = SWM_FOCUS_MANUAL;
7624 errx(1, "focus_mode");
7626 case SWM_S_ICONIC_ENABLED:
7627 iconic_enabled = atoi(value);
7629 case SWM_S_REGION_PADDING:
7630 region_padding = atoi(value);
7631 if (region_padding < 0)
7634 case SWM_S_SPAWN_ORDER:
7635 if (strcmp(value, "first") == 0)
7636 spawn_position = SWM_STACK_BOTTOM;
7637 else if (strcmp(value, "last") == 0)
7638 spawn_position = SWM_STACK_TOP;
7639 else if (strcmp(value, "next") == 0)
7640 spawn_position = SWM_STACK_ABOVE;
7641 else if (strcmp(value, "previous") == 0)
7642 spawn_position = SWM_STACK_BELOW;
7644 errx(1, "spawn_position");
7646 case SWM_S_SPAWN_TERM:
7647 setconfspawn("term", value, 0);
7648 setconfspawn("spawn_term", value, 0);
7651 /* No longer needed; leave to not break old conf files. */
7653 case SWM_S_SS_ENABLED:
7654 /* No longer needed; leave to not break old conf files. */
7656 case SWM_S_STACK_ENABLED:
7657 stack_enabled = atoi(value);
7659 case SWM_S_TERM_WIDTH:
7660 term_width = atoi(value);
7664 case SWM_S_TILE_GAP:
7665 tile_gap = atoi(value);
7667 case SWM_S_URGENT_ENABLED:
7668 urgent_enabled = atoi(value);
7670 case SWM_S_VERBOSE_LAYOUT:
7671 verbose_layout = atoi(value);
7672 for (i = 0; layouts[i].l_stack != NULL; i++) {
7674 layouts[i].l_string = fancy_stacker;
7676 layouts[i].l_string = plain_stacker;
7679 case SWM_S_WINDOW_CLASS_ENABLED:
7680 window_class_enabled = atoi(value);
7682 case SWM_S_WINDOW_INSTANCE_ENABLED:
7683 window_instance_enabled = atoi(value);
7685 case SWM_S_WINDOW_NAME_ENABLED:
7686 window_name_enabled = atoi(value);
7688 case SWM_S_WORKSPACE_LIMIT:
7689 workspace_limit = atoi(value);
7690 if (workspace_limit > SWM_WS_MAX)
7691 workspace_limit = SWM_WS_MAX;
7692 else if (workspace_limit < 1)
7693 workspace_limit = 1;
7702 setconfmodkey(const char *selector, const char *value, int flags)
7704 /* suppress unused warnings since vars are needed */
7708 if (strncasecmp(value, "Mod1", strlen("Mod1")) == 0)
7709 update_modkey(XCB_MOD_MASK_1);
7710 else if (strncasecmp(value, "Mod2", strlen("Mod2")) == 0)
7711 update_modkey(XCB_MOD_MASK_2);
7712 else if (strncasecmp(value, "Mod3", strlen("Mod3")) == 0)
7713 update_modkey(XCB_MOD_MASK_3);
7714 else if (strncasecmp(value, "Mod4", strlen("Mod4")) == 0)
7715 update_modkey(XCB_MOD_MASK_4);
7722 setconfcolor(const char *selector, const char *value, int flags)
7724 setscreencolor(value,
7725 (selector == NULL || strlen(selector) == 0) ? -1 : atoi(selector),
7731 setconfregion(const char *selector, const char *value, int flags)
7733 /* suppress unused warnings since vars are needed */
7737 custom_region(value);
7742 setautorun(const char *selector, const char *value, int flags)
7752 /* suppress unused warnings since vars are needed */
7756 if (getenv("SWM_STARTED"))
7760 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
7761 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
7763 if (ws_id < 0 || ws_id >= workspace_limit)
7764 errx(1, "autorun: invalid workspace %d", ws_id + 1);
7766 sp = expand_tilde((char *)&s);
7769 * This is a little intricate
7771 * If the pid already exists we simply reuse it because it means it was
7772 * used before AND not claimed by manage_window. We get away with
7773 * altering it in the parent after INSERT because this can not be a race
7776 while ((ap = strsep(&sp, " \t")) != NULL) {
7779 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
7781 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
7782 err(1, "setautorun: realloc");
7783 a.argv[argc - 1] = ap;
7787 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
7788 err(1, "setautorun: realloc");
7789 a.argv[argc] = NULL;
7791 if ((pid = fork()) == 0) {
7792 spawn(ws_id, &a, 1);
7801 p = calloc(1, sizeof *p);
7804 TAILQ_INSERT_TAIL(&pidlist, p, entry);
7814 setlayout(const char *selector, const char *value, int flags)
7816 int ws_id, i, x, mg, ma, si, ar, f = 0;
7817 int st = SWM_V_STACK, num_screens;
7819 struct workspace *ws;
7821 /* suppress unused warnings since vars are needed */
7825 if (getenv("SWM_STARTED"))
7829 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
7830 &ws_id, &mg, &ma, &si, &ar, s) != 6)
7831 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7832 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7835 if (ws_id < 0 || ws_id >= workspace_limit)
7836 errx(1, "layout: invalid workspace %d", ws_id + 1);
7838 if (strcasecmp(s, "vertical") == 0)
7840 else if (strcasecmp(s, "vertical_flip") == 0) {
7843 } else if (strcasecmp(s, "horizontal") == 0)
7845 else if (strcasecmp(s, "horizontal_flip") == 0) {
7848 } else if (strcasecmp(s, "fullscreen") == 0)
7851 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7852 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7855 num_screens = get_screen_count();
7856 for (i = 0; i < num_screens; i++) {
7857 ws = (struct workspace *)&screens[i].ws;
7858 ws[ws_id].cur_layout = &layouts[st];
7860 ws[ws_id].always_raise = ar;
7861 if (st == SWM_MAX_STACK)
7865 for (x = 0; x < abs(mg); x++) {
7866 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7867 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
7868 SWM_ARG_ID_MASTERSHRINK);
7872 for (x = 0; x < abs(ma); x++) {
7873 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7874 ma >= 0 ? SWM_ARG_ID_MASTERADD :
7875 SWM_ARG_ID_MASTERDEL);
7879 for (x = 0; x < abs(si); x++) {
7880 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7881 si >= 0 ? SWM_ARG_ID_STACKINC :
7882 SWM_ARG_ID_STACKDEC);
7887 ws[ws_id].cur_layout->l_config(&ws[ws_id],
7888 SWM_ARG_ID_FLIPLAYOUT);
7898 /* config options */
7899 struct config_option {
7901 int (*func)(const char*, const char*, int);
7904 struct config_option configopt[] = {
7905 { "autorun", setautorun, 0 },
7906 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
7907 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
7908 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
7909 { "bar_border_unfocus", setconfcolor, SWM_S_COLOR_BAR_BORDER_UNFOCUS },
7910 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
7911 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
7912 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
7913 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
7914 { "bar_enabled_ws", setconfvalue, SWM_S_BAR_ENABLED_WS },
7915 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
7916 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
7917 { "bar_format", setconfvalue, SWM_S_BAR_FORMAT },
7918 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
7919 { "bind", setconfbinding, 0 },
7920 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
7921 { "boundary_width", setconfvalue, SWM_S_BOUNDARY_WIDTH },
7922 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
7923 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
7924 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
7925 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
7926 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
7927 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
7928 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
7929 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
7930 { "focus_close", setconfvalue, SWM_S_FOCUS_CLOSE },
7931 { "focus_close_wrap", setconfvalue, SWM_S_FOCUS_CLOSE_WRAP },
7932 { "focus_default", setconfvalue, SWM_S_FOCUS_DEFAULT },
7933 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
7934 { "iconic_enabled", setconfvalue, SWM_S_ICONIC_ENABLED },
7935 { "keyboard_mapping", setkeymapping, 0 },
7936 { "layout", setlayout, 0 },
7937 { "modkey", setconfmodkey, 0 },
7938 { "program", setconfspawn, 0 },
7939 { "quirk", setconfquirk, 0 },
7940 { "region", setconfregion, 0 },
7941 { "region_padding", setconfvalue, SWM_S_REGION_PADDING },
7942 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
7943 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
7944 { "spawn_position", setconfvalue, SWM_S_SPAWN_ORDER },
7945 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
7946 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
7947 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
7948 { "tile_gap", setconfvalue, SWM_S_TILE_GAP },
7949 { "title_class_enabled", setconfvalue, SWM_S_WINDOW_CLASS_ENABLED }, /* For backwards compat. */
7950 { "title_name_enabled", setconfvalue, SWM_S_WINDOW_INSTANCE_ENABLED }, /* For backwards compat. */
7951 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
7952 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
7953 { "window_class_enabled", setconfvalue, SWM_S_WINDOW_CLASS_ENABLED },
7954 { "window_instance_enabled", setconfvalue, SWM_S_WINDOW_INSTANCE_ENABLED },
7955 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
7956 { "workspace_limit", setconfvalue, SWM_S_WORKSPACE_LIMIT },
7960 _add_startup_exception(const char *fmt, va_list ap)
7962 if (vasprintf(&startup_exception, fmt, ap) == -1)
7963 warn("%s: asprintf", __func__);
7967 add_startup_exception(const char *fmt, ...)
7973 if (startup_exception)
7976 /* force bar to be enabled due to exception */
7980 _add_startup_exception(fmt, ap);
7985 conf_load(const char *filename, int keymapping)
7988 char *line = NULL, *cp, *ce, *optsub, *optval = NULL;
7989 size_t linelen, lineno = 0;
7990 int wordlen, i, optidx, count;
7991 struct config_option *opt = NULL;
7993 DNPRINTF(SWM_D_CONF, "conf_load: begin\n");
7995 if (filename == NULL) {
7996 warnx("conf_load: no filename");
8000 DNPRINTF(SWM_D_CONF, "conf_load: open %s\n", filename);
8002 if ((config = fopen(filename, "r")) == NULL) {
8003 warn("conf_load: fopen: %s", filename);
8007 while (!feof(config)) {
8011 if ((line = fparseln(config, &linelen, &lineno, NULL,
8012 FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT)) == NULL) {
8014 err(1, "%s", filename);
8019 cp += strspn(cp, " \t\n"); /* eat whitespace */
8020 if (cp[0] == '\0') {
8024 /* get config option */
8025 wordlen = strcspn(cp, "=[ \t\n");
8027 add_startup_exception("%s: line %zd: no option found",
8032 for (i = 0; i < LENGTH(configopt); i++) {
8033 opt = &configopt[i];
8034 if (strncasecmp(cp, opt->optname, wordlen) == 0 &&
8035 (int)strlen(opt->optname) == wordlen) {
8041 add_startup_exception("%s: line %zd: unknown option "
8042 "%.*s", filename, lineno, wordlen, cp);
8045 if (keymapping && opt && strcmp(opt->optname, "bind")) {
8046 add_startup_exception("%s: line %zd: invalid option "
8047 "%.*s", filename, lineno, wordlen, cp);
8051 cp += strspn(cp, " \t\n"); /* eat whitespace */
8053 /* get [selector] if any */
8058 /* Get length of selector. */
8059 for (ce = ++cp; *ce != '\0'; ++ce) {
8060 /* Find matching (not escaped) bracket. */
8061 if (*ce == ']' && *(ce - 1) != '\\') {
8068 add_startup_exception("%s: line %zd: syntax "
8069 "error: unterminated selector", filename,
8074 /* ce points at ']'; terminate optsub. */
8079 cp += strspn(cp, "= \t\n"); /* eat trailing */
8082 if (strlen(optval) == 0) {
8083 add_startup_exception("%s: line %zd: must supply value "
8084 "to %s", filename, lineno,
8085 configopt[optidx].optname);
8088 /* call function to deal with it all */
8089 if (configopt[optidx].func(optsub, optval,
8090 configopt[optidx].funcflags) != 0) {
8091 add_startup_exception("%s: line %zd: invalid data for "
8092 "%s", filename, lineno, configopt[optidx].optname);
8100 DNPRINTF(SWM_D_CONF, "conf_load: end\n");
8106 set_child_transient(struct ws_win *win, xcb_window_t *trans)
8108 struct ws_win *parent, *w;
8109 struct swm_region *r;
8110 struct workspace *ws;
8111 xcb_icccm_wm_hints_t wmh;
8113 parent = find_window(win->transient);
8115 parent->focus_child = win;
8117 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
8118 " for 0x%x trans 0x%x\n", win->id, win->transient);
8120 r = root_to_region(win->s->root, SWM_CK_ALL);
8122 /* parent doen't exist in our window list */
8123 TAILQ_FOREACH(w, &ws->winlist, entry) {
8124 if (xcb_icccm_get_wm_hints_reply(conn,
8125 xcb_icccm_get_wm_hints(conn, w->id),
8127 warnx("can't get hints for 0x%x", w->id);
8131 if (win->hints.window_group != wmh.window_group)
8134 w->focus_child = win;
8135 win->transient = w->id;
8137 DNPRINTF(SWM_D_MISC, "set_child_transient: adjusting "
8138 "transient to 0x%x\n", win->transient);
8145 window_get_pid(xcb_window_t win)
8150 xcb_get_property_cookie_t pc;
8151 xcb_get_property_reply_t *pr;
8153 apid = get_atom_from_string("_NET_WM_PID");
8154 if (apid == XCB_ATOM_NONE)
8157 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_CARDINAL, 0, 1);
8158 pr = xcb_get_property_reply(conn, pc, NULL);
8161 if (pr->type != XCB_ATOM_CARDINAL) {
8166 if (pr->type == apid && pr->format == 32)
8167 ret = *((pid_t *)xcb_get_property_value(pr));
8173 apid = get_atom_from_string("_SWM_PID");
8174 pc = xcb_get_property(conn, 0, win, apid, XCB_ATOM_STRING,
8176 pr = xcb_get_property_reply(conn, pc, NULL);
8179 if (pr->type != apid) {
8184 ret = (pid_t)strtonum(xcb_get_property_value(pr), 0, INT_MAX, &errstr);
8191 get_ws_idx(xcb_window_t id)
8197 xcb_get_property_reply_t *gpr;
8199 gpr = xcb_get_property_reply(conn,
8200 xcb_get_property(conn, 0, id, a_swm_ws,
8201 XCB_ATOM_STRING, 0, SWM_PROPLEN),
8206 proplen = xcb_get_property_value_length(gpr);
8208 prop = malloc(proplen + 1);
8211 xcb_get_property_value(gpr),
8213 prop[proplen] = '\0';
8220 DNPRINTF(SWM_D_PROP, "get_ws_idx: _SWM_WS: %s\n", prop);
8221 ws_idx = (int)strtonum(prop, 0, workspace_limit - 1, &errstr);
8223 DNPRINTF(SWM_D_PROP, "get_ws_idx: window: #%s: %s",
8233 manage_window(xcb_window_t id, uint16_t mapped)
8235 xcb_window_t trans = XCB_WINDOW_NONE;
8236 struct ws_win *win, *ww;
8238 char ws_idx_str[SWM_PROPLEN];
8239 char *class, *instance, *name;
8240 struct swm_region *r;
8244 xcb_get_geometry_reply_t *gr;
8246 if ((win = find_window(id)) != NULL) {
8247 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x already "
8248 "managed; skipping.)\n", id);
8249 return (win); /* Already managed. */
8252 /* See if window is on the unmanaged list. */
8253 if ((win = find_unmanaged_window(id)) != NULL) {
8254 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x found on "
8255 "unmanaged list.\n", id);
8256 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
8259 set_child_transient(win, &trans);
8263 DNPRINTF(SWM_D_MISC, "manage_window: win 0x%x is new.\n", id);
8266 /* Try to get initial window geometry. */
8267 gr = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, id), NULL);
8269 DNPRINTF(SWM_D_MISC, "manage_window: get geometry failed.\n");
8273 /* Create and initialize ws_win object. */
8274 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
8275 err(1, "manage_window: calloc: failed to allocate memory for "
8280 /* Figureout which region the window belongs to. */
8281 r = root_to_region(gr->root, SWM_CK_ALL);
8283 /* Ignore window border if there is one. */
8284 WIDTH(win) = gr->width;
8285 HEIGHT(win) = gr->height;
8286 X(win) = gr->x + gr->border_width - border_width;
8287 Y(win) = gr->y + gr->border_width - border_width;
8289 win->mapped = mapped;
8291 win->ewmh_flags = 0;
8292 win->s = r->s; /* this never changes */
8296 /* Select which X events to monitor and set border pixel color. */
8297 wa[0] = win->s->c[SWM_S_COLOR_UNFOCUS].pixel;
8298 wa[1] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_PROPERTY_CHANGE |
8299 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
8301 wa[1] |= XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_FOCUS_CHANGE;
8304 xcb_change_window_attributes(conn, win->id, XCB_CW_BORDER_PIXEL |
8305 XCB_CW_EVENT_MASK, wa);
8307 /* Get WM_SIZE_HINTS. */
8308 xcb_icccm_get_wm_normal_hints_reply(conn,
8309 xcb_icccm_get_wm_normal_hints(conn, win->id),
8313 xcb_icccm_get_wm_hints_reply(conn,
8314 xcb_icccm_get_wm_hints(conn, win->id),
8317 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
8318 xcb_icccm_get_wm_transient_for_reply(conn,
8319 xcb_icccm_get_wm_transient_for(conn, win->id),
8322 win->transient = trans;
8323 set_child_transient(win, &win->transient);
8326 /* Get WM_PROTOCOLS. */
8327 get_wm_protocols(win);
8329 win->iconic = get_swm_iconic(win);
8331 /* Figure out which workspace the window belongs to. */
8332 if ((p = find_pid(window_get_pid(win->id))) != NULL) {
8333 win->ws = &r->s->ws[p->ws];
8334 TAILQ_REMOVE(&pidlist, p, entry);
8337 } else if ((ws_idx = get_ws_idx(win->id)) != -1 &&
8339 /* _SWM_WS is set; use that. */
8340 win->ws = &r->s->ws[ws_idx];
8341 } else if (trans && (ww = find_window(trans)) != NULL) {
8342 /* Launch transients in the same ws as parent. */
8348 /* Set the _SWM_WS atom so we can remember this after reincarnation. */
8349 if (snprintf(ws_idx_str, SWM_PROPLEN, "%d", win->ws->idx) <
8351 DNPRINTF(SWM_D_PROP, "manage_window: set _SWM_WS: %s\n",
8353 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win->id,
8354 a_swm_ws, XCB_ATOM_STRING, 8, strlen(ws_idx_str),
8358 /* WS must already be set for this to work. */
8359 store_float_geom(win);
8362 ewmh_autoquirk(win);
8364 /* Determine initial quirks. */
8365 xcb_icccm_get_wm_class_reply(conn,
8366 xcb_icccm_get_wm_class(conn, win->id),
8369 class = win->ch.class_name ? win->ch.class_name : "";
8370 instance = win->ch.instance_name ? win->ch.instance_name : "";
8371 name = get_win_name(win->id);
8373 DNPRINTF(SWM_D_CLASS, "manage_window: class: %s, instance: %s, "
8374 "name: %s\n", class, instance, name);
8376 /* java is retarded so treat it special */
8377 if (win->ch.instance_name && strstr(win->ch.instance_name, "sun-awt")) {
8378 DNPRINTF(SWM_D_CLASS, "manage_window: java window detected.\n");
8382 TAILQ_FOREACH(qp, &quirks, entry) {
8383 if (regexec(&qp->regex_class, class, 0, NULL, 0) == 0 &&
8384 regexec(&qp->regex_instance, instance, 0, NULL, 0) == 0 &&
8385 regexec(&qp->regex_name, name, 0, NULL, 0) == 0) {
8386 DNPRINTF(SWM_D_CLASS, "manage_window: matched "
8387 "quirk: %s:%s:%s mask: %#lx\n", qp->class,
8388 qp->instance, qp->name, qp->quirk);
8389 if (qp->quirk & SWM_Q_FLOAT)
8391 win->quirks = qp->quirk;
8397 /* Alter window position if quirky */
8398 if (win->quirks & SWM_Q_ANYWHERE)
8401 /* Reset font sizes (the bruteforce way; no default keybinding). */
8402 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
8403 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
8404 fake_keypress(win, XK_KP_Subtract, XCB_MOD_MASK_SHIFT);
8405 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
8406 fake_keypress(win, XK_KP_Add, XCB_MOD_MASK_SHIFT);
8409 /* Make sure window is positioned inside its region, if its active. */
8411 region_containment(win, r, SWM_CW_ALLSIDES |
8412 SWM_CW_HARDBOUNDARY);
8417 /* Figure out where to stack the window in the workspace. */
8418 if (trans && (ww = find_window(trans)))
8419 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
8420 else if (win->ws->focus && spawn_position == SWM_STACK_ABOVE)
8421 TAILQ_INSERT_AFTER(&win->ws->winlist, win->ws->focus, win,
8423 else if (win->ws->focus && spawn_position == SWM_STACK_BELOW)
8424 TAILQ_INSERT_BEFORE(win->ws->focus, win, entry);
8425 else switch (spawn_position) {
8428 case SWM_STACK_ABOVE:
8429 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
8431 case SWM_STACK_BOTTOM:
8432 case SWM_STACK_BELOW:
8433 TAILQ_INSERT_HEAD(&win->ws->winlist, win, entry);
8436 /* Get initial _NET_WM_STATE */
8437 ewmh_get_win_state(win);
8438 /* Set initial _NET_WM_ALLOWED_ACTIONS */
8439 ewmh_update_actions(win);
8443 DNPRINTF(SWM_D_MISC, "manage_window: done. window: 0x%x, (x,y) w x h: "
8444 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: 0x%x\n", win->id,
8445 X(win), Y(win), WIDTH(win), HEIGHT(win), win->ws->idx,
8446 YESNO(win->iconic), win->transient);
8452 free_window(struct ws_win *win)
8454 DNPRINTF(SWM_D_MISC, "free_window: window: 0x%x\n", win->id);
8459 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
8461 xcb_icccm_get_wm_class_reply_wipe(&win->ch);
8466 memset(win, 0xff, sizeof *win); /* XXX kill later */
8469 DNPRINTF(SWM_D_MISC, "free_window: done.\n");
8473 unmanage_window(struct ws_win *win)
8475 struct ws_win *parent;
8480 DNPRINTF(SWM_D_MISC, "unmanage_window: window: 0x%x\n", win->id);
8482 if (win->transient) {
8483 parent = find_window(win->transient);
8485 parent->focus_child = NULL;
8488 TAILQ_REMOVE(&win->ws->winlist, win, entry);
8489 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
8493 expose(xcb_expose_event_t *e)
8496 struct swm_region *r;
8498 DNPRINTF(SWM_D_EVENT, "expose: window: 0x%x\n", e->window);
8500 num_screens = get_screen_count();
8501 for (i = 0; i < num_screens; i++)
8502 TAILQ_FOREACH(r, &screens[i].rl, entry)
8503 if (e->window == WINID(r->bar))
8511 focusin(xcb_focus_in_event_t *e)
8513 DNPRINTF(SWM_D_EVENT, "focusin: window: 0x%x, mode: %s(%u), "
8514 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
8515 e->mode, get_notify_detail_label(e->detail), e->detail);
8519 focusout(xcb_focus_out_event_t *e)
8521 DNPRINTF(SWM_D_EVENT, "focusout: window: 0x%x, mode: %s(%u), "
8522 "detail: %s(%u)\n", e->event, get_notify_mode_label(e->mode),
8523 e->mode, get_notify_detail_label(e->detail), e->detail);
8528 keypress(xcb_key_press_event_t *e)
8530 xcb_keysym_t keysym;
8533 keysym = xcb_key_press_lookup_keysym(syms, e, 0);
8535 DNPRINTF(SWM_D_EVENT, "keypress: keysym: %u, win (x,y): 0x%x (%d,%d), "
8536 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
8537 "state: %u, same_screen: %s\n", keysym, e->event, e->event_x,
8538 e->event_y, e->detail, e->time, e->root, e->root_x, e->root_y,
8539 e->child, e->state, YESNO(e->same_screen));
8541 if ((kp = key_lookup(CLEANMASK(e->state), keysym)) == NULL)
8544 last_event_time = e->time;
8546 if (kp->funcid == KF_SPAWN_CUSTOM)
8547 spawn_custom(root_to_region(e->root, SWM_CK_ALL),
8548 &(keyfuncs[kp->funcid].args), kp->spawn_name);
8549 else if (keyfuncs[kp->funcid].func)
8550 keyfuncs[kp->funcid].func(root_to_region(e->root, SWM_CK_ALL),
8551 &(keyfuncs[kp->funcid].args));
8554 /* Unfreeze grab events. */
8555 xcb_allow_events(conn, XCB_ALLOW_ASYNC_KEYBOARD, e->time);
8558 DNPRINTF(SWM_D_EVENT, "keypress: done.\n");
8562 buttonpress(xcb_button_press_event_t *e)
8564 struct ws_win *win = NULL;
8565 struct swm_region *r, *old_r;
8569 DNPRINTF(SWM_D_EVENT, "buttonpress: win (x,y): 0x%x (%d,%d), "
8570 "detail: %u, time: %u, root (x,y): 0x%x (%d,%d), child: 0x%x, "
8571 "state: %u, same_screen: %s\n", e->event, e->event_x, e->event_y,
8572 e->detail, e->time, e->root, e->root_x, e->root_y, e->child,
8573 e->state, YESNO(e->same_screen));
8575 if (e->event == e->root) {
8576 if (e->child != 0) {
8577 win = find_window(e->child);
8578 /* Pass ButtonPress to window if it isn't managed. */
8582 /* Focus on empty region */
8583 /* If no windows on region if its empty. */
8584 r = root_to_region(e->root, SWM_CK_POINTER);
8586 DNPRINTF(SWM_D_EVENT, "buttonpress: "
8587 "NULL region; ignoring.\n");
8591 if (TAILQ_EMPTY(&r->ws->winlist)) {
8592 old_r = root_to_region(e->root, SWM_CK_FOCUS);
8593 if (old_r && old_r != r)
8594 unfocus_win(old_r->ws->focus);
8596 xcb_set_input_focus(conn,
8597 XCB_INPUT_FOCUS_PARENT, e->root, e->time);
8599 /* Clear bar since empty. */
8607 win = find_window(e->event);
8613 last_event_time = e->time;
8615 focus_win(get_focus_magic(win));
8617 for (i = 0; i < LENGTH(buttons); i++)
8618 if (client_click == buttons[i].action && buttons[i].func &&
8619 buttons[i].button == e->detail &&
8620 CLEANMASK(buttons[i].mask) == CLEANMASK(e->state)) {
8621 buttons[i].func(win, &buttons[i].args);
8627 DNPRINTF(SWM_D_EVENT, "buttonpress: passing to window.\n");
8628 /* Replay event to event window */
8629 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, e->time);
8631 DNPRINTF(SWM_D_EVENT, "buttonpress: handled.\n");
8632 /* Unfreeze grab events. */
8633 xcb_allow_events(conn, XCB_ALLOW_SYNC_POINTER, e->time);
8641 print_win_geom(xcb_window_t w)
8643 xcb_get_geometry_reply_t *wa;
8645 wa = xcb_get_geometry_reply(conn, xcb_get_geometry(conn, w), NULL);
8647 DNPRINTF(SWM_D_MISC, "print_win_geom: window not found: 0x%x\n",
8652 DNPRINTF(SWM_D_MISC, "print_win_geom: window: 0x%x, root: 0x%x, "
8653 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
8654 w, wa->root, wa->depth, wa->x, wa->y, wa->width, wa->height,
8663 get_stack_mode_name(uint8_t mode)
8668 case XCB_STACK_MODE_ABOVE:
8671 case XCB_STACK_MODE_BELOW:
8674 case XCB_STACK_MODE_TOP_IF:
8677 case XCB_STACK_MODE_BOTTOM_IF:
8680 case XCB_STACK_MODE_OPPOSITE:
8692 configurerequest(xcb_configure_request_event_t *e)
8695 struct swm_region *r = NULL;
8698 uint32_t wc[7] = {0};
8700 if ((win = find_window(e->window)) == NULL)
8701 if ((win = find_unmanaged_window(e->window)) == NULL)
8705 if (swm_debug & SWM_D_EVENT) {
8706 print_win_geom(e->window);
8708 DNPRINTF(SWM_D_EVENT, "configurerequest: window: 0x%x, "
8709 "parent: 0x%x, new: %s, value_mask: %u { ", e->window,
8710 e->parent, YESNO(new), e->value_mask);
8711 if (e->value_mask & XCB_CONFIG_WINDOW_X)
8712 DPRINTF("X: %d ", e->x);
8713 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
8714 DPRINTF("Y: %d ", e->y);
8715 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
8716 DPRINTF("W: %u ", e->width);
8717 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
8718 DPRINTF("H: %u ", e->height);
8719 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH)
8720 DPRINTF("Border: %u ", e->border_width);
8721 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING)
8722 DPRINTF("Sibling: 0x%x ", e->sibling);
8723 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
8724 DPRINTF("StackMode: %s(%u) ",
8725 get_stack_mode_name(e->stack_mode), e->stack_mode);
8731 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
8732 mask |= XCB_CONFIG_WINDOW_X;
8735 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
8736 mask |= XCB_CONFIG_WINDOW_Y;
8739 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
8740 mask |= XCB_CONFIG_WINDOW_WIDTH;
8743 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
8744 mask |= XCB_CONFIG_WINDOW_HEIGHT;
8745 wc[i++] = e->height;
8747 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
8748 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
8749 wc[i++] = e->border_width;
8751 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
8752 mask |= XCB_CONFIG_WINDOW_SIBLING;
8753 wc[i++] = e->sibling;
8755 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
8756 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
8757 wc[i++] = e->stack_mode;
8761 xcb_configure_window(conn, e->window, mask, wc);
8764 } else if ((!win->manual || win->quirks & SWM_Q_ANYWHERE) &&
8765 !(win->ewmh_flags & EWMH_F_FULLSCREEN) && !win->maximized) {
8768 else if (win->ws->old_r)
8771 /* windows are centered unless ANYWHERE quirk is set. */
8772 if (win->quirks & SWM_Q_ANYWHERE) {
8773 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
8774 win->g_float.x = e->x;
8776 win->g_float.x -= X(r);
8779 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
8780 win->g_float.y = e->y;
8782 win->g_float.y -= Y(r);
8786 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
8787 win->g_float.w = e->width;
8789 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
8790 win->g_float.h = e->height;
8792 win->g_floatvalid = 1;
8794 if (win->floating && r && (win->transient ||
8795 win->ws->cur_layout != &layouts[SWM_MAX_STACK])
8796 && !win->maximized) {
8797 WIDTH(win) = win->g_float.w;
8798 HEIGHT(win) = win->g_float.h;
8813 DNPRINTF(SWM_D_EVENT, "configurerequest: done.\n");
8817 configurenotify(xcb_configure_notify_event_t *e)
8821 DNPRINTF(SWM_D_EVENT, "configurenotify: win 0x%x, event win: 0x%x, "
8822 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: 0x%x, "
8823 "override_redirect: %s\n", e->window, e->event, e->x, e->y,
8824 e->width, e->height, e->border_width, e->above_sibling,
8825 YESNO(e->override_redirect));
8827 win = find_window(e->window);
8829 xcb_icccm_get_wm_normal_hints_reply(conn,
8830 xcb_icccm_get_wm_normal_hints(conn, win->id),
8833 if (font_adjusted) {
8841 destroynotify(xcb_destroy_notify_event_t *e)
8845 DNPRINTF(SWM_D_EVENT, "destroynotify: window: 0x%x\n", e->window);
8847 if ((win = find_window(e->window)) == NULL) {
8848 if ((win = find_unmanaged_window(e->window)) == NULL)
8854 if (focus_mode != SWM_FOCUS_FOLLOW) {
8855 /* If we were focused, make sure we focus on something else. */
8856 if (win == win->ws->focus)
8857 win->ws->focus_pending = get_focus_prev(win);
8860 unmanage_window(win);
8863 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
8864 if (win->ws->focus_pending) {
8865 focus_win(win->ws->focus_pending);
8866 win->ws->focus_pending = NULL;
8867 } else if (win == win->ws->focus) {
8868 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
8869 win->ws->r->id, XCB_CURRENT_TIME);
8880 get_notify_detail_label(uint8_t detail)
8885 case XCB_NOTIFY_DETAIL_ANCESTOR:
8888 case XCB_NOTIFY_DETAIL_VIRTUAL:
8891 case XCB_NOTIFY_DETAIL_INFERIOR:
8894 case XCB_NOTIFY_DETAIL_NONLINEAR:
8895 label = "Nonlinear";
8897 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
8898 label = "NonlinearVirtual";
8900 case XCB_NOTIFY_DETAIL_POINTER:
8903 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
8904 label = "PointerRoot";
8906 case XCB_NOTIFY_DETAIL_NONE:
8917 get_notify_mode_label(uint8_t mode)
8922 case XCB_NOTIFY_MODE_NORMAL:
8925 case XCB_NOTIFY_MODE_GRAB:
8928 case XCB_NOTIFY_MODE_UNGRAB:
8931 case XCB_NOTIFY_MODE_WHILE_GRABBED:
8932 label = "WhileGrabbed";
8943 enternotify(xcb_enter_notify_event_t *e)
8946 struct swm_region *r;
8948 DNPRINTF(SWM_D_FOCUS, "enternotify: time: %u, win (x,y): 0x%x "
8949 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8950 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8951 e->time, e->event, e->event_x, e->event_y,
8952 get_notify_mode_label(e->mode), e->mode,
8953 get_notify_detail_label(e->detail), e->detail,
8954 e->root, e->root_x, e->root_y, e->child,
8955 YESNO(e->same_screen_focus), e->state);
8957 if (focus_mode == SWM_FOCUS_MANUAL &&
8958 e->mode == XCB_NOTIFY_MODE_NORMAL) {
8959 DNPRINTF(SWM_D_EVENT, "enternotify: manual focus; ignoring.\n");
8963 last_event_time = e->time;
8965 if ((win = find_window(e->event)) == NULL) {
8966 if (e->event == e->root) {
8967 /* If no windows on pointer region, then focus root. */
8968 r = root_to_region(e->root, SWM_CK_POINTER);
8970 DNPRINTF(SWM_D_EVENT, "enternotify: "
8971 "NULL region; ignoring.\n");
8977 DNPRINTF(SWM_D_EVENT, "enternotify: window is NULL; "
8982 focus_win(get_focus_magic(win));
8990 leavenotify(xcb_leave_notify_event_t *e)
8992 DNPRINTF(SWM_D_FOCUS, "leavenotify: time: %u, win (x,y): 0x%x "
8993 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
8994 "child: 0x%x, same_screen_focus: %s, state: %d\n",
8995 e->time, e->event, e->event_x, e->event_y,
8996 get_notify_mode_label(e->mode), e->mode,
8997 get_notify_detail_label(e->detail), e->detail,
8998 e->root, e->root_x, e->root_y, e->child,
8999 YESNO(e->same_screen_focus), e->state);
9004 mapnotify(xcb_map_notify_event_t *e)
9006 struct ws_win *win, *parent = NULL;
9007 struct workspace *ws;
9009 DNPRINTF(SWM_D_EVENT, "mapnotify: window: 0x%x\n", e->window);
9011 if ((win = manage_window(e->window, 1)) == NULL)
9015 /* Need to know if win was mapped due to ws switch. */
9016 if (ws->state == SWM_WS_STATE_MAPPED) {
9017 if (ws->focus_pending && ws->focus_pending->transient)
9018 parent = find_window(win->transient);
9020 /* If window's parent is maximized, don't clear it. */
9021 if ((parent == NULL) || !parent->maximized)
9022 if (clear_maximized(ws) > 0)
9027 set_win_state(win, XCB_ICCCM_WM_STATE_NORMAL);
9029 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(win->ws)) {
9030 if (ws->focus_pending == win) {
9032 ws->focus_pending = NULL;
9041 mappingnotify(xcb_mapping_notify_event_t *e)
9044 int i, j, num_screens;
9046 xcb_refresh_keyboard_mapping(syms, e);
9048 if (e->request == XCB_MAPPING_KEYBOARD) {
9051 /* Regrab buttons on all managed windows. */
9052 num_screens = get_screen_count();
9053 for (i = 0; i < num_screens; i++)
9054 for (j = 0; j < workspace_limit; j++)
9055 TAILQ_FOREACH(w, &screens[i].ws[j].winlist,
9062 maprequest(xcb_map_request_event_t *e)
9064 struct ws_win *win, *w = NULL;
9065 xcb_get_window_attributes_reply_t *war;
9067 DNPRINTF(SWM_D_EVENT, "maprequest: win 0x%x\n",
9070 war = xcb_get_window_attributes_reply(conn,
9071 xcb_get_window_attributes(conn, e->window),
9074 DNPRINTF(SWM_D_EVENT, "maprequest: window lost.\n");
9078 if (war->override_redirect) {
9079 DNPRINTF(SWM_D_EVENT, "maprequest: override_redirect; "
9084 win = manage_window(e->window,
9085 (war->map_state == XCB_MAP_STATE_VIEWABLE));
9089 /* The new window should get focus; prepare. */
9090 if (focus_mode != SWM_FOCUS_FOLLOW &&
9091 !(win->quirks & SWM_Q_NOFOCUSONMAP) &&
9092 (!(win->hints.flags & XCB_ICCCM_WM_HINT_INPUT) ||
9093 (win->hints.flags & XCB_ICCCM_WM_HINT_INPUT &&
9094 win->hints.input))) {
9095 if (win->quirks & SWM_Q_FOCUSONMAP_SINGLE) {
9096 /* See if other wins of same type are already mapped. */
9097 TAILQ_FOREACH(w, &win->ws->winlist, entry) {
9098 if (w == win || !w->mapped)
9101 if (w->ch.class_name &&
9102 win->ch.class_name &&
9103 strcmp(w->ch.class_name,
9104 win->ch.class_name) == 0 &&
9105 w->ch.instance_name &&
9106 win->ch.instance_name &&
9107 strcmp(w->ch.instance_name,
9108 win->ch.instance_name) == 0)
9114 win->ws->focus_pending = get_focus_magic(win);
9117 /* All windows need to be mapped if they are in the current workspace.*/
9121 /* Ignore EnterNotify to handle the mapnotify without interference. */
9122 if (focus_mode == SWM_FOCUS_DEFAULT)
9123 event_drain(XCB_ENTER_NOTIFY);
9126 DNPRINTF(SWM_D_EVENT, "maprequest: done.\n");
9130 motionnotify(xcb_motion_notify_event_t *e)
9132 struct swm_region *r;
9135 DNPRINTF(SWM_D_FOCUS, "motionnotify: time: %u, win (x,y): 0x%x "
9136 "(%d,%d), detail: %s(%d), root (x,y): 0x%x (%d,%d), "
9137 "child: 0x%x, same_screen_focus: %s, state: %d\n",
9138 e->time, e->event, e->event_x, e->event_y,
9139 get_notify_detail_label(e->detail), e->detail,
9140 e->root, e->root_x, e->root_y, e->child,
9141 YESNO(e->same_screen), e->state);
9143 last_event_time = e->time;
9145 if (focus_mode == SWM_FOCUS_MANUAL)
9148 num_screens = get_screen_count();
9149 for (i = 0; i < num_screens; i++)
9150 if (screens[i].root == e->root)
9153 TAILQ_FOREACH(r, &screens[i].rl, entry)
9154 if (X(r) <= e->root_x && e->root_x < MAX_X(r) &&
9155 Y(r) <= e->root_y && e->root_y < MAX_Y(r))
9163 get_atom_name(xcb_atom_t atom)
9166 #ifdef SWM_DEBUG_ATOM_NAMES
9168 * This should be disabled during most debugging since
9169 * xcb_get_* causes an xcb_flush.
9172 xcb_get_atom_name_reply_t *r;
9174 r = xcb_get_atom_name_reply(conn,
9175 xcb_get_atom_name(conn, atom),
9178 len = xcb_get_atom_name_name_length(r);
9180 name = malloc(len + 1);
9182 memcpy(name, xcb_get_atom_name_name(r), len);
9196 propertynotify(xcb_property_notify_event_t *e)
9199 struct workspace *ws;
9203 name = get_atom_name(e->atom);
9204 DNPRINTF(SWM_D_EVENT, "propertynotify: window: 0x%x, atom: %s(%u), "
9205 "time: %#x, state: %u\n", e->window, name, e->atom, e->time,
9209 win = find_window(e->window);
9215 last_event_time = e->time;
9217 if (e->atom == a_swm_iconic) {
9218 if (e->state == XCB_PROPERTY_NEW_VALUE) {
9219 if (focus_mode != SWM_FOCUS_FOLLOW)
9220 ws->focus_pending = get_focus_prev(win);
9228 if (focus_mode != SWM_FOCUS_FOLLOW &&
9230 if (ws->focus_pending) {
9231 focus_win(ws->focus_pending);
9232 ws->focus_pending = NULL;
9234 xcb_set_input_focus(conn,
9235 XCB_INPUT_FOCUS_PARENT,
9243 } else if (e->state == XCB_PROPERTY_DELETE) {
9244 /* Reload floating geometry in case region changed. */
9246 load_float_geom(win);
9248 /* The window is no longer iconic, restack ws. */
9249 if (focus_mode != SWM_FOCUS_FOLLOW)
9250 ws->focus_pending = get_focus_magic(win);
9254 /* Flush EnterNotify for mapnotify, if needed. */
9257 } else if (e->atom == a_state) {
9258 /* State just changed, make sure it gets focused if mapped. */
9259 if (e->state == XCB_PROPERTY_NEW_VALUE) {
9260 if (focus_mode != SWM_FOCUS_FOLLOW && WS_FOCUSED(ws)) {
9262 ws->focus_pending == win) {
9263 focus_win(ws->focus_pending);
9264 ws->focus_pending = NULL;
9268 } else if (e->atom == XCB_ATOM_WM_CLASS ||
9269 e->atom == XCB_ATOM_WM_NAME) {
9271 } else if (e->atom == a_prot) {
9272 get_wm_protocols(win);
9279 unmapnotify(xcb_unmap_notify_event_t *e)
9282 struct workspace *ws;
9284 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: 0x%x\n", e->window);
9286 /* If we aren't managing the window, then ignore. */
9287 win = find_window(e->window);
9288 if (win == NULL || win->id != e->window)
9293 if (getstate(e->window) != XCB_ICCCM_WM_STATE_ICONIC)
9294 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
9297 /* window unmapped itself */
9298 /* do unmap/unfocus/restack and unmanage */
9301 /* If win was focused, make sure to focus on something else. */
9302 if (win == ws->focus) {
9303 if (focus_mode != SWM_FOCUS_FOLLOW) {
9304 ws->focus_pending = get_focus_prev(win);
9305 DNPRINTF(SWM_D_EVENT, "unmapnotify: "
9306 "focus_pending: 0x%x\n",
9307 WINID(ws->focus_pending));
9313 unmanage_window(win);
9318 if (WS_FOCUSED(ws)) {
9319 if (focus_mode == SWM_FOCUS_FOLLOW) {
9320 focus_win(get_pointer_win(ws->r->s->root));
9321 } else if (ws->focus_pending) {
9322 focus_win(ws->focus_pending);
9323 ws->focus_pending = NULL;
9324 } else if (ws->focus == NULL) {
9325 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_PARENT,
9326 ws->r->id, XCB_CURRENT_TIME);
9331 if (getstate(e->window) == XCB_ICCCM_WM_STATE_NORMAL)
9332 set_win_state(win, XCB_ICCCM_WM_STATE_ICONIC);
9338 clientmessage(xcb_client_message_event_t *e)
9341 xcb_map_request_event_t mre;
9345 name = get_atom_name(e->type);
9346 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%x, atom: %s(%u)\n",
9347 e->window, name, e->type);
9350 win = find_window(e->window);
9353 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
9354 /* Manage the window with maprequest. */
9355 DNPRINTF(SWM_D_EVENT, "clientmessage: request focus on "
9356 "unmanaged window.\n");
9357 mre.window = e->window;
9363 if (e->type == ewmh[_NET_ACTIVE_WINDOW].atom) {
9364 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW\n");
9365 if (WS_FOCUSED(win->ws))
9368 win->ws->focus_pending = win;
9370 if (e->type == ewmh[_NET_CLOSE_WINDOW].atom) {
9371 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW\n");
9372 if (win->can_delete)
9373 client_msg(win, a_delete, 0);
9375 xcb_kill_client(conn, win->id);
9377 if (e->type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
9378 DNPRINTF(SWM_D_EVENT,
9379 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
9380 if (win->floating) {
9381 if (e->data.data32[0] & (1<<8)) /* x */
9382 X(win) = e->data.data32[1];
9383 if (e->data.data32[0] & (1<<9)) /* y */
9384 Y(win) = e->data.data32[2];
9385 if (e->data.data32[0] & (1<<10)) /* width */
9386 WIDTH(win) = e->data.data32[3];
9387 if (e->data.data32[0] & (1<<11)) /* height */
9388 HEIGHT(win) = e->data.data32[4];
9393 /* TODO: Change stack sizes */
9394 /* notify no change was made. */
9395 config_win(win, NULL);
9398 if (e->type == ewmh[_NET_WM_STATE].atom) {
9399 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE\n");
9400 ewmh_update_win_state(win, e->data.data32[1], e->data.data32[0]);
9401 if (e->data.data32[2])
9402 ewmh_update_win_state(win, e->data.data32[2],
9414 int errcode = xcb_connection_has_error(conn);
9415 #ifdef XCB_CONN_ERROR
9418 case XCB_CONN_ERROR:
9419 s = "Socket error, pipe error or other stream error.";
9421 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
9422 s = "Extension not supported.";
9424 case XCB_CONN_CLOSED_MEM_INSUFFICIENT:
9425 s = "Insufficient memory.";
9427 case XCB_CONN_CLOSED_REQ_LEN_EXCEED:
9428 s = "Request length was exceeded.";
9430 case XCB_CONN_CLOSED_PARSE_ERR:
9431 s = "Error parsing display string.";
9434 s = "Unknown error.";
9437 errx(errcode, "X CONNECTION ERROR: %s", s);
9440 errx(errcode, "X CONNECTION ERROR");
9448 const uint32_t val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
9449 XCB_EVENT_MASK_ENTER_WINDOW;
9451 xcb_void_cookie_t wac;
9452 xcb_generic_error_t *error;
9454 /* this causes an error if some other window manager is running */
9455 num_screens = get_screen_count();
9456 for (i = 0; i < num_screens; i++) {
9457 if ((sc = get_screen(i)) == NULL)
9458 errx(1, "ERROR: can't get screen %d.", i);
9459 DNPRINTF(SWM_D_INIT, "enable_wm: screen %d, root: 0x%x\n",
9461 wac = xcb_change_window_attributes_checked(conn, sc->root,
9462 XCB_CW_EVENT_MASK, &val);
9463 if ((error = xcb_request_check(conn, wac))) {
9464 DNPRINTF(SWM_D_INIT, "enable_wm: error_code: %u\n",
9470 /* click to focus on empty region */
9471 xcb_grab_button(conn, 1, sc->root, BUTTONMASK,
9472 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
9473 XCB_CURSOR_NONE, XCB_BUTTON_INDEX_1, XCB_BUTTON_MASK_ANY);
9480 new_region(struct swm_screen *s, int x, int y, int w, int h)
9482 struct swm_region *r, *n;
9483 struct workspace *ws = NULL;
9487 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
9488 s->idx, w, h, x, y);
9490 /* remove any conflicting regions */
9491 n = TAILQ_FIRST(&s->rl);
9494 n = TAILQ_NEXT(r, entry);
9495 if (X(r) < (x + w) && (X(r) + WIDTH(r)) > x &&
9496 Y(r) < (y + h) && (Y(r) + HEIGHT(r)) > y) {
9497 if (r->ws->r != NULL)
9498 r->ws->old_r = r->ws->r;
9501 xcb_destroy_window(conn, r->id);
9502 TAILQ_REMOVE(&s->rl, r, entry);
9503 TAILQ_INSERT_TAIL(&s->orl, r, entry);
9507 /* search old regions for one to reuse */
9509 /* size + location match */
9510 TAILQ_FOREACH(r, &s->orl, entry)
9511 if (X(r) == x && Y(r) == y &&
9512 HEIGHT(r) == h && WIDTH(r) == w)
9516 TAILQ_FOREACH(r, &s->orl, entry)
9517 if (HEIGHT(r) == h && WIDTH(r) == w)
9521 TAILQ_REMOVE(&s->orl, r, entry);
9522 /* try to use old region's workspace */
9523 if (r->ws->r == NULL)
9526 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
9527 err(1, "new_region: calloc: failed to allocate memory "
9530 /* if we don't have a workspace already, find one */
9532 for (i = 0; i < workspace_limit; i++)
9533 if (s->ws[i].r == NULL) {
9540 errx(1, "new_region: no free workspaces");
9542 if (ws->state == SWM_WS_STATE_HIDDEN)
9543 ws->state = SWM_WS_STATE_MAPPING;
9554 TAILQ_INSERT_TAIL(&s->rl, r, entry);
9556 /* Invisible region window to detect pointer events on empty regions. */
9557 r->id = xcb_generate_id(conn);
9558 wa[0] = XCB_EVENT_MASK_POINTER_MOTION |
9559 XCB_EVENT_MASK_POINTER_MOTION_HINT;
9561 xcb_create_window(conn, XCB_COPY_FROM_PARENT, r->id, r->s->root,
9562 X(r), Y(r), WIDTH(r), HEIGHT(r), 0, XCB_WINDOW_CLASS_INPUT_ONLY,
9563 XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, wa);
9565 xcb_map_window(conn, r->id);
9571 #ifdef SWM_XRR_HAS_CRTC
9574 #endif /* SWM_XRR_HAS_CRTC */
9575 struct swm_region *r;
9578 xcb_randr_get_screen_resources_current_cookie_t src;
9579 xcb_randr_get_screen_resources_current_reply_t *srr;
9580 xcb_randr_get_crtc_info_cookie_t cic;
9581 xcb_randr_get_crtc_info_reply_t *cir = NULL;
9582 xcb_randr_crtc_t *crtc;
9583 xcb_screen_t *screen;
9585 DNPRINTF(SWM_D_MISC, "scan_xrandr: screen: %d\n", i);
9587 if ((screen = get_screen(i)) == NULL)
9588 errx(1, "ERROR: can't get screen %d.", i);
9590 num_screens = get_screen_count();
9591 if (i >= num_screens)
9592 errx(1, "scan_xrandr: invalid screen");
9594 /* remove any old regions */
9595 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
9596 r->ws->old_r = r->ws->r = NULL;
9598 xcb_destroy_window(conn, r->id);
9599 TAILQ_REMOVE(&screens[i].rl, r, entry);
9600 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
9604 /* map virtual screens onto physical screens */
9605 #ifdef SWM_XRR_HAS_CRTC
9606 if (xrandr_support) {
9607 src = xcb_randr_get_screen_resources_current(conn,
9609 srr = xcb_randr_get_screen_resources_current_reply(conn, src,
9612 new_region(&screens[i], 0, 0,
9613 screen->width_in_pixels,
9614 screen->height_in_pixels);
9617 ncrtc = srr->num_crtcs;
9619 crtc = xcb_randr_get_screen_resources_current_crtcs(srr);
9620 for (c = 0; c < ncrtc; c++) {
9621 cic = xcb_randr_get_crtc_info(conn, crtc[c],
9623 cir = xcb_randr_get_crtc_info_reply(conn, cic, NULL);
9626 if (cir->num_outputs == 0) {
9632 new_region(&screens[i], 0, 0,
9633 screen->width_in_pixels,
9634 screen->height_in_pixels);
9636 new_region(&screens[i],
9637 cir->x, cir->y, cir->width, cir->height);
9642 #endif /* SWM_XRR_HAS_CRTC */
9644 /* If detection failed, create a single region that spans the screen. */
9645 if (TAILQ_EMPTY(&screens[i].rl))
9646 new_region(&screens[i], 0, 0, screen->width_in_pixels,
9647 screen->height_in_pixels);
9650 /* Cleanup unused previously visible workspaces. */
9651 TAILQ_FOREACH(r, &screens[i].orl, entry) {
9652 TAILQ_FOREACH(win, &r->ws->winlist, entry)
9654 r->ws->state = SWM_WS_STATE_HIDDEN;
9656 /* The screen shouldn't focus on an unused region. */
9657 if (screens[i].r_focus == r)
9658 screens[i].r_focus = NULL;
9661 DNPRINTF(SWM_D_MISC, "scan_xrandr: done.\n");
9665 screenchange(xcb_randr_screen_change_notify_event_t *e)
9667 struct swm_region *r;
9670 DNPRINTF(SWM_D_EVENT, "screenchange: root: 0x%x\n", e->root);
9672 num_screens = get_screen_count();
9673 /* silly event doesn't include the screen index */
9674 for (i = 0; i < num_screens; i++)
9675 if (screens[i].root == e->root)
9677 if (i >= num_screens)
9678 errx(1, "screenchange: screen not found");
9680 /* brute force for now, just re-enumerate the regions */
9684 print_win_geom(e->root);
9686 /* add bars to all regions */
9687 for (i = 0; i < num_screens; i++) {
9688 TAILQ_FOREACH(r, &screens[i].rl, entry)
9694 /* Make sure a region has focus on each screen. */
9695 for (i = 0; i < num_screens; i++) {
9696 if (screens[i].r_focus == NULL) {
9697 r = TAILQ_FIRST(&screens[i].rl);
9706 /* Update workspace state on all regions. */
9707 for (i = 0; i < num_screens; i++)
9708 TAILQ_FOREACH(r, &screens[i].rl, entry)
9709 r->ws->state = SWM_WS_STATE_MAPPED;
9715 struct swm_region *r = NULL;
9716 xcb_window_t *wins = NULL, trans;
9718 int i, j, num_screens;
9719 uint16_t state, manage, mapped;
9721 xcb_query_tree_cookie_t qtc;
9722 xcb_query_tree_reply_t *qtr;
9723 xcb_get_window_attributes_cookie_t gac;
9724 xcb_get_window_attributes_reply_t *gar;
9725 xcb_get_property_cookie_t pc;
9727 DNPRINTF(SWM_D_INIT, "grab_windows: begin\n");
9728 num_screens = get_screen_count();
9729 for (i = 0; i < num_screens; i++) {
9730 qtc = xcb_query_tree(conn, screens[i].root);
9731 qtr = xcb_query_tree_reply(conn, qtc, NULL);
9734 wins = xcb_query_tree_children(qtr);
9735 no = xcb_query_tree_children_length(qtr);
9736 /* attach windows to a region */
9737 /* normal windows */
9738 DNPRINTF(SWM_D_INIT, "grab_windows: grab top level windows.\n");
9739 for (j = 0; j < no; j++) {
9740 TAILQ_FOREACH(r, &screens[i].rl, entry) {
9741 if (r->id == wins[j]) {
9742 DNPRINTF(SWM_D_INIT, "grab_windows: "
9743 "skip %#x; region input window.\n",
9752 gac = xcb_get_window_attributes(conn, wins[j]);
9753 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
9755 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9756 "doesn't exist.\n", wins[j]);
9760 if (gar->override_redirect) {
9761 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9762 "override_redirect set.\n", wins[j]);
9767 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
9768 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
9770 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9771 "is transient for %#x.\n", wins[j], trans);
9776 state = getstate(wins[j]);
9777 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
9778 mapped = gar->map_state != XCB_MAP_STATE_UNMAPPED;
9779 if (mapped || manage)
9780 manage_window(wins[j], mapped);
9783 /* transient windows */
9784 DNPRINTF(SWM_D_INIT, "grab_windows: grab transient windows.\n");
9785 for (j = 0; j < no; j++) {
9786 gac = xcb_get_window_attributes(conn, wins[j]);
9787 gar = xcb_get_window_attributes_reply(conn, gac, NULL);
9789 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9790 "doesn't exist.\n", wins[j]);
9794 if (gar->override_redirect) {
9795 DNPRINTF(SWM_D_INIT, "grab_windows: skip %#x; "
9796 "override_redirect set.\n", wins[j]);
9801 state = getstate(wins[j]);
9802 manage = state != XCB_ICCCM_WM_STATE_WITHDRAWN;
9803 mapped = gar->map_state != XCB_MAP_STATE_UNMAPPED;
9804 pc = xcb_icccm_get_wm_transient_for(conn, wins[j]);
9805 if (xcb_icccm_get_wm_transient_for_reply(conn, pc,
9806 &trans, NULL) && manage)
9807 manage_window(wins[j], mapped);
9812 DNPRINTF(SWM_D_INIT, "grab_windows: done.\n");
9818 int i, j, k, num_screens;
9819 struct workspace *ws;
9820 uint32_t gcv[1], wa[1];
9821 const xcb_query_extension_reply_t *qep;
9822 xcb_screen_t *screen;
9823 xcb_randr_query_version_cookie_t c;
9824 xcb_randr_query_version_reply_t *r;
9826 num_screens = get_screen_count();
9827 if ((screens = calloc(num_screens,
9828 sizeof(struct swm_screen))) == NULL)
9829 err(1, "setup_screens: calloc: failed to allocate memory for "
9832 /* initial Xrandr setup */
9834 qep = xcb_get_extension_data(conn, &xcb_randr_id);
9836 c = xcb_randr_query_version(conn, 1, 1);
9837 r = xcb_randr_query_version_reply(conn, c, NULL);
9839 if (r->major_version >= 1) {
9841 xrandr_eventbase = qep->first_event;
9847 wa[0] = cursors[XC_LEFT_PTR].cid;
9849 /* map physical screens */
9850 for (i = 0; i < num_screens; i++) {
9851 DNPRINTF(SWM_D_WS, "setup_screens: init screen: %d\n", i);
9853 screens[i].r_focus = NULL;
9855 TAILQ_INIT(&screens[i].rl);
9856 TAILQ_INIT(&screens[i].orl);
9857 if ((screen = get_screen(i)) == NULL)
9858 errx(1, "ERROR: can't get screen %d.", i);
9859 screens[i].root = screen->root;
9861 /* set default colors */
9862 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
9863 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
9864 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
9865 setscreencolor("rgb:00/40/40", i + 1,
9866 SWM_S_COLOR_BAR_BORDER_UNFOCUS);
9867 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
9868 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
9870 /* create graphics context on screen */
9871 screens[i].bar_gc = xcb_generate_id(conn);
9873 xcb_create_gc(conn, screens[i].bar_gc, screens[i].root,
9874 XCB_GC_GRAPHICS_EXPOSURES, gcv);
9876 /* set default cursor */
9877 xcb_change_window_attributes(conn, screens[i].root,
9880 /* init all workspaces */
9881 /* XXX these should be dynamically allocated too */
9882 for (j = 0; j < SWM_WS_MAX; j++) {
9883 ws = &screens[i].ws[j];
9886 ws->bar_enabled = 1;
9888 ws->focus_prev = NULL;
9889 ws->focus_pending = NULL;
9892 ws->state = SWM_WS_STATE_HIDDEN;
9893 TAILQ_INIT(&ws->winlist);
9894 TAILQ_INIT(&ws->unmanagedlist);
9896 for (k = 0; layouts[k].l_stack != NULL; k++)
9897 if (layouts[k].l_config != NULL)
9898 layouts[k].l_config(ws,
9899 SWM_ARG_ID_STACKINIT);
9900 ws->cur_layout = &layouts[0];
9901 ws->cur_layout->l_string(ws);
9907 xcb_randr_select_input(conn, screens[i].root,
9908 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
9915 if ((bar_fonts = strdup(SWM_BAR_FONTS)) == NULL)
9916 err(1, "setup_globals: strdup: failed to allocate memory.");
9918 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
9919 err(1, "setup_globals: strdup: failed to allocate memory.");
9921 if ((syms = xcb_key_symbols_alloc(conn)) == NULL)
9922 errx(1, "unable to allocate key symbols");
9924 a_state = get_atom_from_string("WM_STATE");
9925 a_prot = get_atom_from_string("WM_PROTOCOLS");
9926 a_delete = get_atom_from_string("WM_DELETE_WINDOW");
9927 a_takefocus = get_atom_from_string("WM_TAKE_FOCUS");
9928 a_utf8_string = get_atom_from_string("UTF8_STRING");
9929 a_swm_iconic = get_atom_from_string("_SWM_ICONIC");
9930 a_swm_ws = get_atom_from_string("_SWM_WS");
9937 xcb_atom_t netwmcheck;
9938 xcb_window_t root, win;
9940 /* work around sun jdk bugs, code from wmname */
9941 netwmcheck = get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
9943 num_screens = get_screen_count();
9944 for (i = 0; i < num_screens; i++) {
9945 root = screens[i].root;
9947 win = xcb_generate_id(conn);
9948 xcb_create_window(conn, XCB_COPY_FROM_PARENT, win, root,
9949 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
9950 XCB_COPY_FROM_PARENT, 0, NULL);
9952 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
9953 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
9954 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
9955 netwmcheck, XCB_ATOM_WINDOW, 32, 1, &win);
9956 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, win,
9957 ewmh[_NET_WM_NAME].atom, a_utf8_string, 8, strlen("LG3D"),
9963 shutdown_cleanup(void)
9967 /* disable alarm because the following code may not be interrupted */
9969 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
9970 err(1, "can't disable alarm");
9979 num_screens = get_screen_count();
9980 for (i = 0; i < num_screens; ++i) {
9981 if (screens[i].bar_gc != XCB_NONE)
9982 xcb_free_gc(conn, screens[i].bar_gc);
9983 if (!bar_font_legacy)
9984 XftColorFree(display, DefaultVisual(display, i),
9985 DefaultColormap(display, i), &bar_font_color);
9988 if (bar_font_legacy)
9989 XFreeFontSet(display, bar_fs);
9991 XftFontClose(display, bar_font);
9994 xcb_key_symbols_free(syms);
9996 xcb_disconnect(conn);
10000 event_error(xcb_generic_error_t *e)
10004 DNPRINTF(SWM_D_EVENT, "event_error: %s(%u) from %s(%u), sequence: %u, "
10005 "resource_id: %u, minor_code: %u\n",
10006 xcb_event_get_error_label(e->error_code), e->error_code,
10007 xcb_event_get_request_label(e->major_code), e->major_code,
10008 e->sequence, e->resource_id, e->minor_code);
10012 event_handle(xcb_generic_event_t *evt)
10014 uint8_t type = XCB_EVENT_RESPONSE_TYPE(evt);
10016 DNPRINTF(SWM_D_EVENT, "XCB Event: %s(%d), seq %u\n",
10017 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt)),
10018 XCB_EVENT_RESPONSE_TYPE(evt), evt->sequence);
10021 #define EVENT(type, callback) case type: callback((void *)evt); return
10022 EVENT(0, event_error);
10023 EVENT(XCB_BUTTON_PRESS, buttonpress);
10024 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
10025 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
10026 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
10027 EVENT(XCB_CLIENT_MESSAGE, clientmessage);
10028 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
10029 EVENT(XCB_CONFIGURE_NOTIFY, configurenotify);
10030 EVENT(XCB_CONFIGURE_REQUEST, configurerequest);
10031 /*EVENT(XCB_CREATE_NOTIFY, );*/
10032 EVENT(XCB_DESTROY_NOTIFY, destroynotify);
10033 EVENT(XCB_ENTER_NOTIFY, enternotify);
10034 EVENT(XCB_EXPOSE, expose);
10036 EVENT(XCB_FOCUS_IN, focusin);
10037 EVENT(XCB_FOCUS_OUT, focusout);
10039 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
10040 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
10041 EVENT(XCB_KEY_PRESS, keypress);
10042 /*EVENT(XCB_KEY_RELEASE, keypress);*/
10043 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
10045 EVENT(XCB_LEAVE_NOTIFY, leavenotify);
10047 EVENT(XCB_MAP_NOTIFY, mapnotify);
10048 EVENT(XCB_MAP_REQUEST, maprequest);
10049 EVENT(XCB_MAPPING_NOTIFY, mappingnotify);
10050 EVENT(XCB_MOTION_NOTIFY, motionnotify);
10051 /*EVENT(XCB_NO_EXPOSURE, );*/
10052 EVENT(XCB_PROPERTY_NOTIFY, propertynotify);
10053 /*EVENT(XCB_REPARENT_NOTIFY, );*/
10054 /*EVENT(XCB_RESIZE_REQUEST, );*/
10055 /*EVENT(XCB_SELECTION_CLEAR, );*/
10056 /*EVENT(XCB_SELECTION_NOTIFY, );*/
10057 /*EVENT(XCB_SELECTION_REQUEST, );*/
10058 EVENT(XCB_UNMAP_NOTIFY, unmapnotify);
10059 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
10062 if (type - xrandr_eventbase == XCB_RANDR_SCREEN_CHANGE_NOTIFY)
10063 screenchange((void *)evt);
10067 main(int argc, char *argv[])
10069 struct swm_region *r;
10070 char conf[PATH_MAX], *cfile = NULL;
10072 int xfd, i, num_screens, startup = 1;
10073 struct sigaction sact;
10074 xcb_generic_event_t *evt;
10078 int stdin_ready = 0;
10081 /* suppress unused warning since var is needed */
10084 time_started = time(NULL);
10087 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION, buildstr);
10088 if (setlocale(LC_CTYPE, "") == NULL || setlocale(LC_TIME, "") == NULL)
10089 warnx("no locale support");
10091 /* handle some signals */
10092 bzero(&sact, sizeof(sact));
10093 sigemptyset(&sact.sa_mask);
10095 sact.sa_handler = sighdlr;
10096 sigaction(SIGINT, &sact, NULL);
10097 sigaction(SIGQUIT, &sact, NULL);
10098 sigaction(SIGTERM, &sact, NULL);
10099 sigaction(SIGHUP, &sact, NULL);
10101 sact.sa_handler = sighdlr;
10102 sact.sa_flags = SA_NOCLDSTOP;
10103 sigaction(SIGCHLD, &sact, NULL);
10105 if ((display = XOpenDisplay(0)) == NULL)
10106 errx(1, "can not open display");
10108 conn = XGetXCBConnection(display);
10109 if (xcb_connection_has_error(conn))
10110 errx(1, "can not get XCB connection");
10112 XSetEventQueueOwner(display, XCBOwnsEventQueue);
10114 xcb_prefetch_extension_data(conn, &xcb_randr_id);
10115 xfd = xcb_get_file_descriptor(conn);
10117 /* look for local and global conf file */
10118 pwd = getpwuid(getuid());
10120 errx(1, "invalid user: %d", getuid());
10122 xcb_grab_server(conn);
10123 xcb_aux_sync(conn);
10125 /* flush all events */
10126 while ((evt = xcb_poll_for_event(conn))) {
10127 if (XCB_EVENT_RESPONSE_TYPE(evt) == 0)
10133 errx(1, "another window manager is currently running");
10135 /* Load Xcursors and/or cursorfont glyph cursors. */
10138 xcb_aux_sync(conn);
10147 for (i = 0; ; i++) {
10152 snprintf(conf, sizeof conf, "%s/.%s",
10153 pwd->pw_dir, SWM_CONF_FILE);
10157 snprintf(conf, sizeof conf, "/etc/%s",
10162 snprintf(conf, sizeof conf, "%s/.%s",
10163 pwd->pw_dir, SWM_CONF_FILE_OLD);
10166 /* global compat */
10167 snprintf(conf, sizeof conf, "/etc/%s",
10168 SWM_CONF_FILE_OLD);
10174 if (strlen(conf) && stat(conf, &sb) != -1)
10175 if (S_ISREG(sb.st_mode)) {
10182 /* load conf (if any) */
10184 conf_load(cfile, SWM_CONF_DEFAULT);
10189 /* set some values to work around bad programs */
10191 /* grab existing windows (before we build the bars) */
10194 if (getenv("SWM_STARTED") == NULL)
10195 setenv("SWM_STARTED", "YES", 1);
10197 /* setup all bars */
10198 num_screens = get_screen_count();
10199 for (i = 0; i < num_screens; i++)
10200 TAILQ_FOREACH(r, &screens[i].rl, entry)
10207 xcb_ungrab_server(conn);
10210 rd_max = xfd > STDIN_FILENO ? xfd : STDIN_FILENO;
10213 while ((evt = xcb_poll_for_event(conn))) {
10220 /* If just (re)started, set default focus if needed. */
10224 if (focus_mode != SWM_FOCUS_FOLLOW) {
10225 r = TAILQ_FIRST(&screens[0].rl);
10237 FD_SET(STDIN_FILENO, &rd);
10242 num_readable = select(rd_max + 1, &rd, NULL, NULL, &tv);
10243 if (num_readable == -1 && errno != EINTR) {
10244 DNPRINTF(SWM_D_MISC, "select failed");
10245 } else if (num_readable > 0 && FD_ISSET(STDIN_FILENO, &rd)) {
10250 restart(NULL, NULL);
10260 bar_extra_update();
10267 shutdown_cleanup();