2 * Copyright (c) 2009-2015 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-2015 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-2015 David Hill <dhill@mindcry.org>
12 * Copyright (c) 2014-2015 Yuri D'Elia <yuri.delia@eurac.edu>
14 * Permission to use, copy, modify, and distribute this software for any
15 * purpose with or without fee is hereby granted, provided that the above
16 * copyright notice and this permission notice appear in all copies.
18 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
19 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
21 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
23 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
24 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 * Much code and ideas taken from dwm under the following license:
28 * MIT/X Consortium License
30 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
31 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
32 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
33 * 2007 Premysl Hruby <dfenze at gmail dot com>
34 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
35 * 2007 Christof Musik <christof at sendfax dot de>
36 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
37 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
38 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
40 * Permission is hereby granted, free of charge, to any person obtaining a
41 * copy of this software and associated documentation files (the "Software"),
42 * to deal in the Software without restriction, including without limitation
43 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
44 * and/or sell copies of the Software, and to permit persons to whom the
45 * Software is furnished to do so, subject to the following conditions:
47 * The above copyright notice and this permission notice shall be included in
48 * all copies or substantial portions of the Software.
50 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
51 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
52 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
53 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
54 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
55 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
56 * DEALINGS IN THE SOFTWARE.
60 #include <sys/types.h>
67 #include <sys/queue.h>
69 #include <sys/param.h>
70 #include <sys/select.h>
71 #if defined(__linux__)
73 #elif defined(__OpenBSD__)
75 #elif defined(__FreeBSD__)
99 #include <X11/cursorfont.h>
100 #include <X11/extensions/Xrandr.h>
101 #include <X11/Xcursor/Xcursor.h>
102 #include <X11/Xft/Xft.h>
103 #include <X11/Xlib-xcb.h>
104 #include <xcb/xcb_atom.h>
105 #include <xcb/xcb_aux.h>
106 #include <xcb/xcb_event.h>
107 #include <xcb/xcb_icccm.h>
108 #include <xcb/xcb_keysyms.h>
109 #include <xcb/xtest.h>
110 #include <xcb/randr.h>
118 #ifdef SPECTRWM_BUILDSTR
119 static const char *buildstr
= SPECTRWM_BUILDSTR
;
121 static const char *buildstr
= SPECTRWM_VERSION
;
124 #if !defined(__CYGWIN__) /* cygwin chokes on randr stuff */
126 # error RandR versions less than 1.0 are not supported
129 # if RANDR_MAJOR >= 1
130 # if RANDR_MINOR >= 2
131 # define SWM_XRR_HAS_CRTC
134 #endif /* __CYGWIN__ */
136 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
137 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
138 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
139 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
140 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
141 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
142 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
143 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
144 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
145 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
146 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
147 #define xcb_icccm_get_wm_class xcb_get_wm_class
148 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
149 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
150 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
151 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
152 #define xcb_icccm_wm_hints_get_urgency xcb_wm_hints_get_urgency
153 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
154 #define xcb_icccm_get_wm_name xcb_get_wm_name
155 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
156 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
157 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
158 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
159 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
160 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
161 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
162 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
163 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
164 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
167 /*#define SWM_DEBUG*/
169 #define DPRINTF(x...) do { \
171 fprintf(stderr, x); \
173 #define DNPRINTF(n,x...) do { \
174 if (swm_debug & n) { \
175 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
176 fprintf(stderr, x); \
179 #define SWM_D_MISC 0x0001
180 #define SWM_D_EVENT 0x0002
181 #define SWM_D_WS 0x0004
182 #define SWM_D_FOCUS 0x0008
183 #define SWM_D_MOVE 0x0010
184 #define SWM_D_STACK 0x0020
185 #define SWM_D_MOUSE 0x0040
186 #define SWM_D_PROP 0x0080
187 #define SWM_D_CLASS 0x0100
188 #define SWM_D_KEY 0x0200
189 #define SWM_D_QUIRK 0x0400
190 #define SWM_D_SPAWN 0x0800
191 #define SWM_D_EVENTQ 0x1000
192 #define SWM_D_CONF 0x2000
193 #define SWM_D_BAR 0x4000
194 #define SWM_D_INIT 0x8000
196 uint32_t swm_debug
= 0
215 #define DPRINTF(x...)
216 #define DNPRINTF(n,x...)
219 #define SWM_EWMH_ACTION_COUNT_MAX (8)
220 #define EWMH_F_FULLSCREEN (0x001)
221 #define EWMH_F_ABOVE (0x002)
222 #define EWMH_F_HIDDEN (0x004)
223 #define EWMH_F_MAXIMIZED_VERT (0x008)
224 #define EWMH_F_MAXIMIZED_HORZ (0x010)
225 #define EWMH_F_SKIP_PAGER (0x020)
226 #define EWMH_F_SKIP_TASKBAR (0x040)
227 #define SWM_F_MANUAL (0x080)
229 #define EWMH_F_MAXIMIZED (EWMH_F_MAXIMIZED_VERT | EWMH_F_MAXIMIZED_HORZ)
231 /* convert 8-bit to 16-bit */
232 #define RGB_8_TO_16(col) (((col) << 8) + (col))
234 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
235 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
236 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
237 xrc.blue = RGB_8_TO_16((px) & 0xff); \
240 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
241 #define MODKEY XCB_MOD_MASK_1
242 #define ANYMOD XCB_MOD_MASK_ANY
243 #define CLEANMASK(mask) ((mask) & (XCB_KEY_BUT_MASK_SHIFT | \
244 XCB_KEY_BUT_MASK_CONTROL | XCB_KEY_BUT_MASK_MOD_1 | \
245 XCB_KEY_BUT_MASK_MOD_2 | XCB_KEY_BUT_MASK_MOD_3 | \
246 XCB_KEY_BUT_MASK_MOD_4 | XCB_KEY_BUT_MASK_MOD_5) & ~(numlockmask))
247 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
248 XCB_EVENT_MASK_BUTTON_RELEASE)
249 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
250 #define SWM_PROPLEN (16)
251 #define SWM_FUNCNAME_LEN (32)
252 #define SWM_KEYS_LEN (255)
253 #define SWM_QUIRK_LEN (64)
254 #define X(r) ((r)->g.x)
255 #define Y(r) ((r)->g.y)
256 #define WIDTH(r) ((r)->g.w)
257 #define HEIGHT(r) ((r)->g.h)
258 #define BORDER(w) ((w)->bordered ? border_width : 0)
259 #define MAX_X(r) ((r)->g.x + (r)->g.w)
260 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
261 #define SH_MIN(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
262 #define SH_MIN_W(w) ((w)->sh.min_width)
263 #define SH_MIN_H(w) ((w)->sh.min_height)
264 #define SH_MAX(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
265 #define SH_MAX_W(w) ((w)->sh.max_width)
266 #define SH_MAX_H(w) ((w)->sh.max_height)
267 #define SH_INC(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)
268 #define SH_INC_W(w) ((w)->sh.width_inc)
269 #define SH_INC_H(w) ((w)->sh.height_inc)
270 #define SWM_MAX_FONT_STEPS (3)
271 #define WINID(w) ((w) ? (w)->id : XCB_WINDOW_NONE)
272 #define WS_FOCUSED(ws) ((ws)->r && (ws)->r->s->r_focus == (ws)->r)
273 #define YESNO(x) ((x) ? "yes" : "no")
274 #define ICONIC(w) ((w)->ewmh_flags & EWMH_F_HIDDEN)
275 #define ABOVE(w) ((w)->ewmh_flags & EWMH_F_ABOVE)
276 #define FULLSCREEN(w) ((w)->ewmh_flags & EWMH_F_FULLSCREEN)
277 #define MAXIMIZED_VERT(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_VERT)
278 #define MAXIMIZED_HORZ(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_HORZ)
279 #define MAXIMIZED(w) (MAXIMIZED_VERT(w) || MAXIMIZED_HORZ(w))
280 #define MANUAL(w) ((w)->ewmh_flags & SWM_F_MANUAL)
281 #define TRANS(w) ((w)->transient != XCB_WINDOW_NONE)
282 #define FLOATING(w) (ABOVE(w) || TRANS(w) || FULLSCREEN(w) || \
285 /* Constrain Window flags */
286 #define SWM_CW_RESIZABLE (0x01)
287 #define SWM_CW_SOFTBOUNDARY (0x02)
288 #define SWM_CW_HARDBOUNDARY (0x04)
289 #define SWM_CW_RIGHT (0x10)
290 #define SWM_CW_LEFT (0x20)
291 #define SWM_CW_BOTTOM (0x40)
292 #define SWM_CW_TOP (0x80)
293 #define SWM_CW_ALLSIDES (0xf0)
295 #define SWM_FOCUS_DEFAULT (0)
296 #define SWM_FOCUS_FOLLOW (1)
297 #define SWM_FOCUS_MANUAL (2)
299 #define SWM_CK_NONE (0)
300 #define SWM_CK_ALL (0xf)
301 #define SWM_CK_FOCUS (0x1)
302 #define SWM_CK_POINTER (0x2)
303 #define SWM_CK_FALLBACK (0x4)
304 #define SWM_CK_REGION (0x8)
306 #define SWM_G_ALL (0xf)
307 #define SWM_G_SIZE (0x1)
308 #define SWM_G_POS (0x2)
310 #define SWM_CONF_DEFAULT (0)
311 #define SWM_CONF_KEYMAPPING (1)
314 #define SWM_LIB "/usr/local/lib/libswmhack.so"
321 xcb_atom_t a_net_wm_check
;
322 xcb_atom_t a_net_supported
;
323 xcb_atom_t a_takefocus
;
324 xcb_atom_t a_utf8_string
;
326 volatile sig_atomic_t running
= 1;
327 volatile sig_atomic_t restart_wm
= 0;
328 xcb_timestamp_t last_event_time
= 0;
332 unsigned int numlockmask
= 0;
335 xcb_connection_t
*conn
;
336 xcb_key_symbols_t
*syms
;
338 int boundary_width
= 50;
339 bool cycle_empty
= false;
340 bool cycle_visible
= false;
342 int font_adjusted
= 0;
343 uint16_t mod_key
= MODKEY
;
344 bool warp_focus
= false;
345 bool warp_pointer
= false;
346 bool workspace_clamp
= false;
349 struct swm_region
*search_r
;
350 int select_list_pipe
[2];
351 int select_resp_pipe
[2];
353 volatile sig_atomic_t search_resp
;
354 int search_resp_action
;
356 struct search_window
{
357 TAILQ_ENTRY(search_window
) entry
;
361 xcb_window_t indicator
;
363 TAILQ_HEAD(search_winlist
, search_window
);
364 struct search_winlist search_wl
;
369 SWM_SEARCH_UNICONIFY
,
370 SWM_SEARCH_NAME_WORKSPACE
,
371 SWM_SEARCH_SEARCH_WORKSPACE
,
372 SWM_SEARCH_SEARCH_WINDOW
375 #define SWM_STACK_TOP (0)
376 #define SWM_STACK_BOTTOM (1)
377 #define SWM_STACK_ABOVE (2)
378 #define SWM_STACK_BELOW (3)
381 double dialog_ratio
= 0.6;
383 #define SWM_BAR_MAX (256)
384 #define SWM_BAR_JUSTIFY_LEFT (0)
385 #define SWM_BAR_JUSTIFY_CENTER (1)
386 #define SWM_BAR_JUSTIFY_RIGHT (2)
387 #define SWM_BAR_OFFSET (4)
388 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
389 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
390 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
391 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
392 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
394 #ifdef X_HAVE_UTF8_STRING
395 #define DRAWSTRING(x...) Xutf8DrawString(x)
397 #define DRAWSTRING(x...) XmbDrawString(x)
400 char *bar_argv
[] = { NULL
, NULL
};
402 char bar_ext
[SWM_BAR_MAX
];
403 char bar_ext_buf
[SWM_BAR_MAX
];
404 char bar_vertext
[SWM_BAR_MAX
];
405 bool bar_version
= false;
406 bool bar_enabled
= true;
407 int bar_border_width
= 1;
408 bool bar_at_bottom
= false;
409 bool bar_extra
= false;
411 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
412 char *bar_format
= NULL
;
413 bool stack_enabled
= true;
414 bool clock_enabled
= true;
415 bool iconic_enabled
= false;
416 bool maximize_hide_bar
= false;
417 bool urgent_enabled
= false;
418 bool urgent_collapse
= false;
419 char *clock_format
= NULL
;
420 bool window_class_enabled
= false;
421 bool window_instance_enabled
= false;
422 bool window_name_enabled
= false;
423 int focus_mode
= SWM_FOCUS_DEFAULT
;
424 int focus_close
= SWM_STACK_BELOW
;
425 bool focus_close_wrap
= true;
426 int focus_default
= SWM_STACK_TOP
;
427 int spawn_position
= SWM_STACK_TOP
;
428 bool disable_border
= false;
429 int border_width
= 1;
430 int region_padding
= 0;
432 bool java_workaround
= true;
433 bool verbose_layout
= false;
438 XFontSet bar_fs
= NULL
;
439 XFontSetExtents
*bar_fs_extents
;
440 XftFont
*bar_font
= NULL
;
441 bool bar_font_legacy
= true;
442 char *bar_fonts
= NULL
;
443 XftColor bar_font_color
;
444 XftColor search_font_color
;
445 char *startup_exception
= NULL
;
446 unsigned int nr_exceptions
= 0;
448 /* layout manager data */
449 struct swm_geometry
{
462 struct swm_geometry g
;
465 /* virtual "screens" */
467 TAILQ_ENTRY(swm_region
) entry
;
469 struct swm_geometry g
;
470 struct workspace
*ws
; /* current workspace on this region */
471 struct workspace
*ws_prior
; /* prior workspace on this region */
472 struct swm_screen
*s
; /* screen idx */
475 TAILQ_HEAD(swm_region_list
, swm_region
);
478 TAILQ_ENTRY(ws_win
) entry
;
479 TAILQ_ENTRY(ws_win
) stack_entry
;
481 xcb_window_t transient
;
482 struct ws_win
*focus_child
; /* focus on child transient */
483 struct swm_geometry g
; /* current geometry */
484 struct swm_geometry g_float
; /* region coordinates */
485 bool g_floatvalid
; /* g_float geometry validity */
489 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
496 struct workspace
*ws
; /* always valid */
497 struct swm_screen
*s
; /* always valid, never changes */
499 xcb_icccm_get_wm_class_reply_t ch
;
500 xcb_icccm_wm_hints_t hints
;
502 TAILQ_HEAD(ws_win_list
, ws_win
);
503 TAILQ_HEAD(ws_win_stack
, ws_win
);
507 TAILQ_ENTRY(pid_e
) entry
;
511 TAILQ_HEAD(pid_list
, pid_e
);
512 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
514 /* layout handlers */
516 void vertical_config(struct workspace
*, int);
517 void vertical_stack(struct workspace
*, struct swm_geometry
*);
518 void horizontal_config(struct workspace
*, int);
519 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
520 void max_stack(struct workspace
*, struct swm_geometry
*);
521 void plain_stacker(struct workspace
*);
522 void fancy_stacker(struct workspace
*);
525 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
526 void (*l_config
)(struct workspace
*, int);
528 #define SWM_L_FOCUSPREV (1<<0)
529 #define SWM_L_MAPONFOCUS (1<<1)
530 void (*l_string
)(struct workspace
*);
532 /* stack, configure */
533 { vertical_stack
, vertical_config
, 0, plain_stacker
},
534 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
536 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
537 { NULL
, NULL
, 0, NULL
},
540 /* position of max_stack mode in the layouts array, index into layouts! */
541 #define SWM_V_STACK (0)
542 #define SWM_H_STACK (1)
543 #define SWM_MAX_STACK (2)
545 #define SWM_H_SLICE (32)
546 #define SWM_V_SLICE (32)
548 /* define work spaces */
550 int idx
; /* workspace index */
551 char *name
; /* workspace name */
552 bool always_raise
; /* raise windows on focus */
553 bool bar_enabled
; /* bar visibility */
554 struct layout
*cur_layout
; /* current layout handlers */
555 struct ws_win
*focus
; /* may be NULL */
556 struct ws_win
*focus_prev
; /* may be NULL */
557 struct ws_win
*focus_pending
; /* may be NULL */
558 struct swm_region
*r
; /* may be NULL */
559 struct swm_region
*old_r
; /* may be NULL */
560 struct ws_win_list winlist
; /* list of windows in ws */
561 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
562 struct ws_win_stack stack
; /* stacking order */
563 int state
; /* mapping state */
564 char stacker
[10]; /* display stacker and layout */
568 int horizontal_msize
;
570 int horizontal_stacks
;
571 bool horizontal_flip
;
581 SWM_WS_STATE_MAPPING
,
587 SWM_S_COLOR_BAR_BORDER
,
588 SWM_S_COLOR_BAR_BORDER_UNFOCUS
,
589 SWM_S_COLOR_BAR_FONT
,
591 SWM_S_COLOR_FOCUS_MAXIMIZED
,
593 SWM_S_COLOR_UNFOCUS_MAXIMIZED
,
597 /* physical screen mapping */
598 #define SWM_WS_MAX (22) /* hard limit */
599 int workspace_limit
= 10; /* soft limit */
602 int idx
; /* screen index */
603 struct swm_region_list rl
; /* list of regions on this screen */
604 struct swm_region_list orl
; /* list of old regions */
606 struct workspace ws
[SWM_WS_MAX
];
607 struct swm_region
*r_focus
;
614 } c
[SWM_S_COLOR_MAX
];
616 xcb_gcontext_t bar_gc
;
619 struct swm_screen
*screens
;
621 /* args to functions */
624 #define SWM_ARG_ID_FOCUSNEXT (0)
625 #define SWM_ARG_ID_FOCUSPREV (1)
626 #define SWM_ARG_ID_FOCUSMAIN (2)
627 #define SWM_ARG_ID_FOCUSURGENT (3)
628 #define SWM_ARG_ID_SWAPNEXT (10)
629 #define SWM_ARG_ID_SWAPPREV (11)
630 #define SWM_ARG_ID_SWAPMAIN (12)
631 #define SWM_ARG_ID_MOVELAST (13)
632 #define SWM_ARG_ID_MASTERSHRINK (20)
633 #define SWM_ARG_ID_MASTERGROW (21)
634 #define SWM_ARG_ID_MASTERADD (22)
635 #define SWM_ARG_ID_MASTERDEL (23)
636 #define SWM_ARG_ID_FLIPLAYOUT (24)
637 #define SWM_ARG_ID_STACKRESET (30)
638 #define SWM_ARG_ID_STACKINIT (31)
639 #define SWM_ARG_ID_STACKBALANCE (32)
640 #define SWM_ARG_ID_CYCLEWS_UP (40)
641 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
642 #define SWM_ARG_ID_CYCLERG_UP (42)
643 #define SWM_ARG_ID_CYCLERG_DOWN (43)
644 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
645 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
646 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
647 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
648 #define SWM_ARG_ID_STACKINC (50)
649 #define SWM_ARG_ID_STACKDEC (51)
650 #define SWM_ARG_ID_SS_ALL (60)
651 #define SWM_ARG_ID_SS_WINDOW (61)
652 #define SWM_ARG_ID_DONTCENTER (70)
653 #define SWM_ARG_ID_CENTER (71)
654 #define SWM_ARG_ID_KILLWINDOW (80)
655 #define SWM_ARG_ID_DELETEWINDOW (81)
656 #define SWM_ARG_ID_WIDTHGROW (90)
657 #define SWM_ARG_ID_WIDTHSHRINK (91)
658 #define SWM_ARG_ID_HEIGHTGROW (92)
659 #define SWM_ARG_ID_HEIGHTSHRINK (93)
660 #define SWM_ARG_ID_MOVEUP (100)
661 #define SWM_ARG_ID_MOVEDOWN (101)
662 #define SWM_ARG_ID_MOVELEFT (102)
663 #define SWM_ARG_ID_MOVERIGHT (103)
664 #define SWM_ARG_ID_RAISE (105)
665 #define SWM_ARG_ID_LOWER (106)
666 #define SWM_ARG_ID_BAR_TOGGLE (110)
667 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
668 #define SWM_ARG_ID_CYCLERG_MOVE_UP (112)
669 #define SWM_ARG_ID_CYCLERG_MOVE_DOWN (113)
675 TAILQ_ENTRY(quirk
) entry
;
676 char *class; /* WM_CLASS:class */
677 char *instance
; /* WM_CLASS:instance */
678 char *name
; /* WM_NAME */
680 regex_t regex_instance
;
683 int ws
; /* Initial workspace. */
684 #define SWM_Q_FLOAT (1<<0) /* Float this window. */
685 #define SWM_Q_TRANSSZ (1<<1) /* Transient window size too small. */
686 #define SWM_Q_ANYWHERE (1<<2) /* Don't position this window */
687 #define SWM_Q_XTERM_FONTADJ (1<<3) /* Adjust xterm fonts when resizing. */
688 #define SWM_Q_FULLSCREEN (1<<4) /* Remove border when fullscreen. */
689 #define SWM_Q_FOCUSPREV (1<<5) /* Focus on caller. */
690 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
691 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
692 #define SWM_Q_OBEYAPPFOCUSREQ (1<<8) /* Focus when applications ask. */
693 #define SWM_Q_IGNOREPID (1<<9) /* Ignore PID when determining ws. */
694 #define SWM_Q_IGNORESPAWNWS (1<<10) /* Ignore _SWM_WS when managing win. */
695 #define SWM_Q_NOFOCUSCYCLE (1<<11) /* Remove from normal focus cycle. */
696 #define SWM_Q_MINIMALBORDER (1<<12) /* No border when floating/unfocused. */
698 TAILQ_HEAD(quirk_list
, quirk
);
699 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
702 * Supported EWMH hints should be added to
703 * both the enum and the ewmh array
709 _NET_CURRENT_DESKTOP
,
710 _NET_DESKTOP_GEOMETRY
,
712 _NET_DESKTOP_VIEWPORT
,
713 _NET_MOVERESIZE_WINDOW
,
714 _NET_NUMBER_OF_DESKTOPS
,
716 _NET_WM_ACTION_ABOVE
,
717 _NET_WM_ACTION_CLOSE
,
718 _NET_WM_ACTION_FULLSCREEN
,
720 _NET_WM_ACTION_RESIZE
,
721 _NET_WM_ALLOWED_ACTIONS
,
723 _NET_WM_FULL_PLACEMENT
,
727 _NET_WM_STATE_FULLSCREEN
,
728 _NET_WM_STATE_HIDDEN
,
729 _NET_WM_STATE_MAXIMIZED_VERT
,
730 _NET_WM_STATE_MAXIMIZED_HORZ
,
731 _NET_WM_STATE_SKIP_PAGER
,
732 _NET_WM_STATE_SKIP_TASKBAR
,
734 _NET_WM_WINDOW_TYPE_DIALOG
,
735 _NET_WM_WINDOW_TYPE_DOCK
,
736 _NET_WM_WINDOW_TYPE_NORMAL
,
737 _NET_WM_WINDOW_TYPE_SPLASH
,
738 _NET_WM_WINDOW_TYPE_TOOLBAR
,
739 _NET_WM_WINDOW_TYPE_UTILITY
,
740 _SWM_WM_STATE_MANUAL
,
747 } ewmh
[SWM_EWMH_HINT_MAX
] = {
748 /* must be in same order as in the enum */
749 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
750 {"_NET_CLIENT_LIST", XCB_ATOM_NONE
},
751 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
752 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE
},
753 {"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE
},
754 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE
},
755 {"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE
},
756 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
757 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE
},
758 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE
},
759 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
760 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
761 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
762 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
763 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
764 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
765 {"_NET_WM_DESKTOP", XCB_ATOM_NONE
},
766 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE
},
767 {"_NET_WM_NAME", XCB_ATOM_NONE
},
768 {"_NET_WM_STATE", XCB_ATOM_NONE
},
769 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
770 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
771 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
772 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
773 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
774 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
775 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
776 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
777 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
778 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
779 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
780 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
781 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
782 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
783 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
786 /* EWMH source type */
788 EWMH_SOURCE_TYPE_NONE
= 0,
789 EWMH_SOURCE_TYPE_NORMAL
= 1,
790 EWMH_SOURCE_TYPE_OTHER
= 2,
797 XC_BOTTOM_LEFT_CORNER
,
798 XC_BOTTOM_RIGHT_CORNER
,
806 char *name
; /* Name used by Xcursor .*/
807 uint8_t cf_char
; /* cursorfont index. */
809 } cursors
[XC_MAX
] = {
810 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
811 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
812 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
813 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
814 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
815 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
816 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
819 #define SWM_SPAWN_OPTIONAL 0x1
823 TAILQ_ENTRY(spawn_prog
) entry
;
829 TAILQ_HEAD(spawn_list
, spawn_prog
);
830 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
836 /* User callable function IDs. */
959 FN_DUMPWINS
, /* MUST BE LAST */
969 BINDING_F_REPLAY
= 0x1,
973 RB_ENTRY(binding
) entry
;
974 uint16_t mod
; /* Modifier Mask. */
975 enum binding_type type
; /* Key or Button. */
976 uint32_t value
; /* KeySym or Button Index. */
977 enum actionid action
; /* Action Identifier. */
981 RB_HEAD(binding_tree
, binding
);
983 /* function prototypes */
984 void adjust_font(struct ws_win
*);
985 char *argsep(char **);
986 void bar_cleanup(struct swm_region
*);
987 void bar_extra_setup(void);
988 void bar_extra_stop(void);
989 int bar_extra_update(void);
990 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
991 void bar_fmt_expand(char *, size_t);
993 void bar_print(struct swm_region
*, const char *);
994 void bar_print_legacy(struct swm_region
*, const char *);
995 void bar_replace(char *, char *, struct swm_region
*, size_t);
996 void bar_replace_pad(char *, int *, size_t);
997 char *bar_replace_seq(char *, char *, struct swm_region
*, size_t *, size_t);
998 void bar_setup(struct swm_region
*);
999 void bar_toggle(struct binding
*, struct swm_region
*, union arg
*);
1000 void bar_urgent(char *, size_t);
1001 void bar_window_class(char *, size_t, struct swm_region
*);
1002 void bar_window_class_instance(char *, size_t, struct swm_region
*);
1003 void bar_window_float(char *, size_t, struct swm_region
*);
1004 void bar_window_instance(char *, size_t, struct swm_region
*);
1005 void bar_window_name(char *, size_t, struct swm_region
*);
1006 void bar_window_state(char *, size_t, struct swm_region
*);
1007 void bar_workspace_name(char *, size_t, struct swm_region
*);
1008 int binding_cmp(struct binding
*, struct binding
*);
1009 void binding_insert(uint16_t, enum binding_type
, uint32_t, enum actionid
,
1010 uint32_t, const char *);
1011 struct binding
*binding_lookup(uint16_t, enum binding_type
, uint32_t);
1012 void binding_remove(struct binding
*);
1013 void buttonpress(xcb_button_press_event_t
*);
1014 void buttonrelease(xcb_button_release_event_t
*);
1015 void center_pointer(struct swm_region
*);
1016 void check_conn(void);
1017 void clear_bindings(void);
1018 void clear_keybindings(void);
1019 int clear_maximized(struct workspace
*);
1020 void clear_quirks(void);
1021 void clear_spawns(void);
1022 void clientmessage(xcb_client_message_event_t
*);
1023 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
1024 int conf_load(const char *, int);
1025 void configurenotify(xcb_configure_notify_event_t
*);
1026 void configurerequest(xcb_configure_request_event_t
*);
1027 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
1028 void constrain_window(struct ws_win
*, struct swm_geometry
*, int *);
1029 int count_win(struct workspace
*, bool);
1030 void cursors_cleanup(void);
1031 void cursors_load(void);
1032 void custom_region(const char *);
1033 void cycle_layout(struct binding
*, struct swm_region
*, union arg
*);
1034 void cyclerg(struct binding
*, struct swm_region
*, union arg
*);
1035 void cyclews(struct binding
*, struct swm_region
*, union arg
*);
1036 void destroynotify(xcb_destroy_notify_event_t
*);
1037 void dumpwins(struct binding
*, struct swm_region
*, union arg
*);
1038 int enable_wm(void);
1039 void enternotify(xcb_enter_notify_event_t
*);
1040 void event_drain(uint8_t);
1041 void event_error(xcb_generic_error_t
*);
1042 void event_handle(xcb_generic_event_t
*);
1043 void ewmh_apply_flags(struct ws_win
*, uint32_t);
1044 void ewmh_autoquirk(struct ws_win
*);
1045 void ewmh_get_desktop_names(void);
1046 void ewmh_get_wm_state(struct ws_win
*);
1047 void ewmh_update_actions(struct ws_win
*);
1048 void ewmh_update_client_list(void);
1049 void ewmh_update_current_desktop(void);
1050 void ewmh_update_desktop_names(void);
1051 void ewmh_update_desktops(void);
1052 void ewmh_change_wm_state(struct ws_win
*, xcb_atom_t
, long);
1053 void ewmh_update_wm_state(struct ws_win
*);
1054 char *expand_tilde(const char *);
1055 void expose(xcb_expose_event_t
*);
1056 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
1057 struct pid_e
*find_pid(pid_t
);
1058 struct ws_win
*find_unmanaged_window(xcb_window_t
);
1059 struct ws_win
*find_window(xcb_window_t
);
1060 void floating_toggle(struct binding
*, struct swm_region
*, union arg
*);
1061 void focus(struct binding
*, struct swm_region
*, union arg
*);
1062 void focus_flush(void);
1063 void focus_pointer(struct binding
*, struct swm_region
*, union arg
*);
1064 void focus_region(struct swm_region
*);
1065 void focus_win(struct ws_win
*);
1067 void focusin(xcb_focus_in_event_t
*);
1068 void focusout(xcb_focus_out_event_t
*);
1070 void focusrg(struct binding
*, struct swm_region
*, union arg
*);
1071 void fontset_init(void);
1072 void free_window(struct ws_win
*);
1073 xcb_atom_t
get_atom_from_string(const char *);
1075 char *get_atom_name(xcb_atom_t
);
1077 struct ws_win
*get_focus_magic(struct ws_win
*);
1078 struct ws_win
*get_focus_prev(struct ws_win
*);
1079 xcb_generic_event_t
*get_next_event(bool);
1081 char *get_notify_detail_label(uint8_t);
1082 char *get_notify_mode_label(uint8_t);
1084 struct ws_win
*get_pointer_win(xcb_window_t
);
1085 struct ws_win
*get_region_focus(struct swm_region
*);
1086 int get_region_index(struct swm_region
*);
1087 xcb_screen_t
*get_screen(int);
1088 int get_screen_count(void);
1090 char *get_source_type_label(uint32_t);
1091 char *get_stack_mode_name(uint8_t);
1092 char *get_state_mask_label(uint16_t);
1094 int32_t get_swm_ws(xcb_window_t
);
1095 bool get_urgent(struct ws_win
*);
1096 char *get_win_name(xcb_window_t
);
1097 uint8_t get_win_state(xcb_window_t
);
1098 void get_wm_protocols(struct ws_win
*);
1099 int get_ws_idx(struct ws_win
*);
1100 void grab_windows(void);
1101 void grabbuttons(void);
1102 void grabkeys(void);
1103 void iconify(struct binding
*, struct swm_region
*, union arg
*);
1104 bool isxlfd(char *);
1105 bool keybindreleased(struct binding
*, xcb_key_release_event_t
*);
1106 void keypress(xcb_key_press_event_t
*);
1107 void keyrelease(xcb_key_release_event_t
*);
1108 bool keyrepeating(xcb_key_release_event_t
*);
1109 void kill_bar_extra_atexit(void);
1110 void kill_refs(struct ws_win
*);
1112 void leavenotify(xcb_leave_notify_event_t
*);
1114 void load_float_geom(struct ws_win
*);
1115 void lower_window(struct ws_win
*);
1116 struct ws_win
*manage_window(xcb_window_t
, int, bool);
1117 void map_window(struct ws_win
*);
1118 void mapnotify(xcb_map_notify_event_t
*);
1119 void mappingnotify(xcb_mapping_notify_event_t
*);
1120 void maprequest(xcb_map_request_event_t
*);
1121 void maximize_toggle(struct binding
*, struct swm_region
*, union arg
*);
1122 void motionnotify(xcb_motion_notify_event_t
*);
1123 void move(struct binding
*, struct swm_region
*, union arg
*);
1124 void move_win(struct ws_win
*, struct binding
*, int);
1125 uint32_t name_to_pixel(int, const char *);
1126 void name_workspace(struct binding
*, struct swm_region
*, union arg
*);
1127 void new_region(struct swm_screen
*, int, int, int, int);
1128 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1129 int parsebinding(const char *, uint16_t *, enum binding_type
*, uint32_t *,
1131 int parsequirks(const char *, uint32_t *, int *);
1132 void pressbutton(struct binding
*, struct swm_region
*, union arg
*);
1133 void priorws(struct binding
*, struct swm_region
*, union arg
*);
1135 void print_win_geom(xcb_window_t
);
1136 void print_win_input_model(struct ws_win
*);
1138 void propertynotify(xcb_property_notify_event_t
*);
1139 void put_back_event(xcb_generic_event_t
*);
1140 void quirk_free(struct quirk
*);
1141 void quirk_insert(const char *, const char *, const char *, uint32_t, int);
1142 void quirk_remove(struct quirk
*);
1143 void quirk_replace(struct quirk
*, const char *, const char *, const char *,
1145 void quit(struct binding
*, struct swm_region
*, union arg
*);
1146 void raise_toggle(struct binding
*, struct swm_region
*, union arg
*);
1147 void raise_window(struct ws_win
*);
1148 void region_containment(struct ws_win
*, struct swm_region
*, int);
1149 struct swm_region
*region_under(struct swm_screen
*, int, int);
1150 void regionize(struct ws_win
*, int, int);
1151 void resize(struct binding
*, struct swm_region
*, union arg
*);
1152 void resize_win(struct ws_win
*, struct binding
*, int);
1153 void restart(struct binding
*, struct swm_region
*, union arg
*);
1154 struct swm_region
*root_to_region(xcb_window_t
, int);
1155 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1156 void scan_randr(int);
1157 void search_do_resp(void);
1158 void search_resp_name_workspace(const char *, size_t);
1159 void search_resp_search_window(const char *);
1160 void search_resp_search_workspace(const char *);
1161 void search_resp_uniconify(const char *, size_t);
1162 void search_win(struct binding
*, struct swm_region
*, union arg
*);
1163 void search_win_cleanup(void);
1164 void search_workspace(struct binding
*, struct swm_region
*, union arg
*);
1165 void send_to_rg(struct binding
*, struct swm_region
*, union arg
*);
1166 void send_to_ws(struct binding
*, struct swm_region
*, union arg
*);
1167 void set_region(struct swm_region
*);
1168 int setautorun(const char *, const char *, int);
1169 void setbinding(uint16_t, enum binding_type
, uint32_t, enum actionid
,
1170 uint32_t, const char *);
1171 int setconfbinding(const char *, const char *, int);
1172 int setconfcolor(const char *, const char *, int);
1173 int setconfmodkey(const char *, const char *, int);
1174 int setconfquirk(const char *, const char *, int);
1175 int setconfregion(const char *, const char *, int);
1176 int setconfspawn(const char *, const char *, int);
1177 int setconfvalue(const char *, const char *, int);
1178 int setkeymapping(const char *, const char *, int);
1179 int setlayout(const char *, const char *, int);
1180 void setquirk(const char *, const char *, const char *, uint32_t, int);
1181 void setscreencolor(const char *, int, int);
1182 void setspawn(const char *, const char *, int);
1183 void setup_btnbindings(void);
1184 void setup_ewmh(void);
1185 void setup_globals(void);
1186 void setup_keybindings(void);
1187 void setup_quirks(void);
1188 void setup_screens(void);
1189 void setup_spawn(void);
1190 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1191 void set_win_state(struct ws_win
*, uint8_t);
1192 void shutdown_cleanup(void);
1194 void socket_setnonblock(int);
1195 void sort_windows(struct ws_win_list
*);
1196 void spawn(int, union arg
*, bool);
1197 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1198 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1199 void spawn_insert(const char *, const char *, int);
1200 struct spawn_prog
*spawn_find(const char *);
1201 void spawn_remove(struct spawn_prog
*);
1202 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1203 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1204 void stack_config(struct binding
*, struct swm_region
*, union arg
*);
1205 void stack_master(struct workspace
*, struct swm_geometry
*, int, bool);
1206 void store_float_geom(struct ws_win
*);
1207 char *strdupsafe(const char *);
1208 void swapwin(struct binding
*, struct swm_region
*, union arg
*);
1209 void switchws(struct binding
*, struct swm_region
*, union arg
*);
1210 void teardown_ewmh(void);
1211 void unescape_selector(char *);
1212 void unfocus_win(struct ws_win
*);
1213 void uniconify(struct binding
*, struct swm_region
*, union arg
*);
1214 void unmanage_window(struct ws_win
*);
1215 void unmap_all(void);
1216 void unmap_window(struct ws_win
*);
1217 void unmapnotify(xcb_unmap_notify_event_t
*);
1218 void update_floater(struct ws_win
*);
1219 void update_modkey(uint16_t);
1220 void update_win_stacking(struct ws_win
*);
1221 void update_window(struct ws_win
*);
1222 void update_window_color(struct ws_win
*);
1223 void update_wm_state(struct ws_win
*win
);
1224 void updatenumlockmask(void);
1225 void validate_spawns(void);
1226 int validate_win(struct ws_win
*);
1227 int validate_ws(struct workspace
*);
1228 void version(struct binding
*, struct swm_region
*, union arg
*);
1229 void win_to_ws(struct ws_win
*, int, bool);
1230 pid_t
window_get_pid(xcb_window_t
);
1231 void wkill(struct binding
*, struct swm_region
*, union arg
*);
1232 void update_ws_stack(struct workspace
*);
1233 void xft_init(struct swm_region
*);
1234 void _add_startup_exception(const char *, va_list);
1235 void add_startup_exception(const char *, ...);
1237 RB_PROTOTYPE(binding_tree
, binding
, entry
, binding_cmp
);
1238 RB_GENERATE(binding_tree
, binding
, entry
, binding_cmp
);
1239 struct binding_tree bindings
;
1244 xcb_font_t cf
= XCB_NONE
;
1247 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1248 /* try to load Xcursor first. */
1249 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1252 /* fallback to cursorfont. */
1253 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1254 if (cf
== XCB_NONE
) {
1255 cf
= xcb_generate_id(conn
);
1256 xcb_open_font(conn
, cf
, strlen("cursor"),
1260 cursors
[i
].cid
= xcb_generate_id(conn
);
1261 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1262 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1263 0xffff, 0xffff, 0xffff);
1269 xcb_close_font(conn
, cf
);
1273 cursors_cleanup(void)
1276 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1277 xcb_free_cursor(conn
, cursors
[i
].cid
);
1281 expand_tilde(const char *s
)
1283 struct passwd
*ppwd
;
1291 errx(1, "expand_tilde: NULL string.");
1294 result
= strdup(sc
);
1300 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1301 errx(1, "expand_tilde: sysconf");
1303 if ((user
= calloc(1, max
+ 1)) == NULL
)
1304 errx(1, "expand_tilde: calloc");
1306 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1311 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1315 result
= strdup(sc
);
1317 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1321 errx(1, "expand_tilde: failed to allocate memory.");
1327 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1329 unsigned int tmpr
, tmpg
, tmpb
;
1331 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1334 *rr
= RGB_8_TO_16(tmpr
);
1335 *gg
= RGB_8_TO_16(tmpg
);
1336 *bb
= RGB_8_TO_16(tmpb
);
1342 get_screen(int screen
)
1344 const xcb_setup_t
*r
;
1345 xcb_screen_iterator_t iter
;
1347 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1348 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1352 iter
= xcb_setup_roots_iterator(r
);
1353 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1361 get_screen_count(void)
1363 const xcb_setup_t
*r
;
1365 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1366 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1370 return xcb_setup_roots_length(r
);
1374 get_region_index(struct swm_region
*r
)
1376 struct swm_region
*rr
;
1382 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1397 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1398 event_drain(XCB_ENTER_NOTIFY
);
1404 get_atom_from_string(const char *str
)
1406 xcb_intern_atom_cookie_t c
;
1407 xcb_intern_atom_reply_t
*r
;
1410 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1411 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1419 return (XCB_ATOM_NONE
);
1423 get_wm_protocols(struct ws_win
*win
) {
1425 xcb_icccm_get_wm_protocols_reply_t wpr
;
1427 if (xcb_icccm_get_wm_protocols_reply(conn
,
1428 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
1430 for (i
= 0; i
< (int)wpr
.atoms_len
; i
++) {
1431 if (wpr
.atoms
[i
] == a_takefocus
)
1432 win
->take_focus
= true;
1433 if (wpr
.atoms
[i
] == a_delete
)
1434 win
->can_delete
= true;
1436 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
1443 xcb_window_t root
, win
;
1444 int i
, j
, num_screens
;
1446 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1447 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1449 num_screens
= get_screen_count();
1450 for (i
= 0; i
< num_screens
; i
++) {
1451 root
= screens
[i
].root
;
1453 /* Set up _NET_SUPPORTING_WM_CHECK. */
1454 win
= xcb_generate_id(conn
);
1455 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
1456 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1457 XCB_COPY_FROM_PARENT
, 0, NULL
);
1459 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
1460 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1461 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1462 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1465 * Impersonate LG3D non-reparenting WM, written by Sun, to
1466 * workaround a Java GUI rendering issue.
1468 if (java_workaround
)
1469 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1470 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1471 8, strlen("LG3D"), "LG3D");
1473 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1474 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1475 8, strlen("spectrwm"), "spectrwm");
1477 /* Report supported atoms */
1478 xcb_delete_property(conn
, root
, a_net_supported
);
1479 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1480 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, root
,
1481 a_net_supported
, XCB_ATOM_ATOM
, 32, 1,
1486 ewmh_update_desktops();
1487 ewmh_get_desktop_names();
1495 xcb_get_property_cookie_t pc
;
1496 xcb_get_property_reply_t
*pr
;
1498 num_screens
= get_screen_count();
1500 for (i
= 0; i
< num_screens
; i
++) {
1501 /* Get the support check window and destroy it */
1502 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, a_net_wm_check
,
1503 XCB_ATOM_WINDOW
, 0, 1);
1504 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1507 if (pr
->format
== a_net_wm_check
) {
1508 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1510 xcb_destroy_window(conn
, id
);
1511 xcb_delete_property(conn
, screens
[i
].root
,
1513 xcb_delete_property(conn
, screens
[i
].root
,
1521 ewmh_autoquirk(struct ws_win
*win
)
1523 xcb_get_property_reply_t
*r
;
1524 xcb_get_property_cookie_t c
;
1528 c
= xcb_get_property(conn
, 0, win
->id
,
1529 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1530 r
= xcb_get_property_reply(conn
, c
, NULL
);
1534 type
= xcb_get_property_value(r
);
1535 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1537 for (i
= 0; i
< n
; i
++) {
1538 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1540 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1541 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1542 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1543 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1546 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1547 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1548 win
->quirks
= SWM_Q_FLOAT
;
1556 ewmh_update_actions(struct ws_win
*win
)
1558 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1564 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1567 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1568 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1569 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1572 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1573 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1576 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1577 #define _NET_WM_STATE_ADD 1 /* add/set property */
1578 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1581 ewmh_change_wm_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1588 name
= get_atom_name(state
);
1589 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: win %#x, state: %s, "
1590 "action: %ld\n", WINID(win
), name
, action
);
1596 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1597 flag
= EWMH_F_FULLSCREEN
;
1598 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1599 flag
= EWMH_F_ABOVE
;
1600 else if (state
== ewmh
[_NET_WM_STATE_HIDDEN
].atom
)
1601 flag
= EWMH_F_HIDDEN
;
1602 else if (state
== ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
||
1603 state
== ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
)
1604 flag
= EWMH_F_MAXIMIZED
;
1605 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1606 flag
= SWM_F_MANUAL
;
1607 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1608 flag
= EWMH_F_SKIP_PAGER
;
1609 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1610 flag
= EWMH_F_SKIP_TASKBAR
;
1612 /* Disallow unfloating transients. */
1613 if (TRANS(win
) && flag
== EWMH_F_ABOVE
)
1616 new_flags
= win
->ewmh_flags
;
1619 case _NET_WM_STATE_REMOVE
:
1622 case _NET_WM_STATE_ADD
:
1625 case _NET_WM_STATE_TOGGLE
:
1630 ewmh_apply_flags(win
, new_flags
);
1633 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: done.\n");
1637 ewmh_apply_flags(struct ws_win
*win
, uint32_t pending
)
1639 struct workspace
*ws
;
1642 changed
= win
->ewmh_flags
^ pending
;
1646 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: pending: %d\n", pending
);
1648 win
->ewmh_flags
= pending
;
1651 if (changed
& EWMH_F_HIDDEN
) {
1653 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1654 ws
->focus_pending
= get_focus_prev(win
);
1659 /* Reload floating geometry in case region changed. */
1661 load_float_geom(win
);
1663 /* The window is no longer iconic, prepare focus. */
1664 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1665 ws
->focus_pending
= get_focus_magic(win
);
1670 if (changed
& EWMH_F_ABOVE
) {
1671 if (ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1673 load_float_geom(win
);
1674 else if (!MAXIMIZED(win
))
1675 store_float_geom(win
);
1677 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1678 changed
&= ~EWMH_F_MAXIMIZED
;
1682 win
->ewmh_flags
^= EWMH_F_ABOVE
& pending
;
1686 if (changed
& EWMH_F_MAXIMIZED
) {
1687 /* VERT and/or HORZ changed. */
1689 if (!MAXIMIZED(win
))
1690 load_float_geom(win
);
1692 store_float_geom(win
);
1695 if (MAXIMIZED(win
)) {
1696 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
1697 ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1701 ws
->focus_pending
= win
;
1705 update_window_color(win
);
1709 if (changed
& EWMH_F_FULLSCREEN
) {
1710 if (FULLSCREEN(win
)) {
1711 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
1715 ws
->focus_pending
= win
;
1718 load_float_geom(win
);
1721 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1725 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: done.\n");
1729 ewmh_update_wm_state(struct ws_win
*win
) {
1730 xcb_atom_t vals
[SWM_EWMH_ACTION_COUNT_MAX
];
1734 vals
[n
++] = ewmh
[_NET_WM_STATE_HIDDEN
].atom
;
1735 if (FULLSCREEN(win
))
1736 vals
[n
++] = ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
;
1737 if (MAXIMIZED_VERT(win
))
1738 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
;
1739 if (MAXIMIZED_HORZ(win
))
1740 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
;
1741 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1742 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
;
1743 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1744 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
;
1745 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1746 vals
[n
++] = ewmh
[_NET_WM_STATE_ABOVE
].atom
;
1747 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1748 vals
[n
++] = ewmh
[_SWM_WM_STATE_MANUAL
].atom
;
1751 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1752 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, n
, vals
);
1754 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1758 ewmh_get_wm_state(struct ws_win
*win
)
1761 xcb_get_property_cookie_t c
;
1762 xcb_get_property_reply_t
*r
;
1768 win
->ewmh_flags
= 0;
1770 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1771 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1772 r
= xcb_get_property_reply(conn
, c
, NULL
);
1776 states
= xcb_get_property_value(r
);
1777 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1779 for (i
= 0; i
< n
; i
++)
1780 ewmh_change_wm_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1788 dumpwins(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
1792 xcb_get_window_attributes_cookie_t c
;
1793 xcb_get_window_attributes_reply_t
*wa
;
1795 /* suppress unused warning since var is needed */
1799 if (r
->ws
== NULL
) {
1800 DPRINTF("dumpwins: invalid workspace\n");
1804 DPRINTF("=== managed window list ws %02d ===\n", r
->ws
->idx
);
1805 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
) {
1806 state
= get_win_state(w
->id
);
1807 c
= xcb_get_window_attributes(conn
, w
->id
);
1808 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1810 DPRINTF("win %#x, map_state: %d, state: %u, "
1811 "transient: %#x\n", w
->id
, wa
->map_state
,
1812 state
, w
->transient
);
1815 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1819 DPRINTF("=== stacking order (top down) === \n");
1820 TAILQ_FOREACH(w
, &r
->ws
->stack
, stack_entry
) {
1821 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
1822 "iconic: %s\n", w
->id
, YESNO(FULLSCREEN(w
)),
1823 YESNO(MAXIMIZED(w
)), YESNO(ABOVE(w
)), YESNO(ICONIC(w
)));
1826 DPRINTF("===== unmanaged window list =====\n");
1827 TAILQ_FOREACH(w
, &r
->ws
->unmanagedlist
, entry
) {
1828 state
= get_win_state(w
->id
);
1829 c
= xcb_get_window_attributes(conn
, w
->id
);
1830 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1832 DPRINTF("win %#x, map_state: %d, state: %u, "
1833 "transient: %#x\n", w
->id
, wa
->map_state
,
1834 state
, w
->transient
);
1837 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1841 DPRINTF("=================================\n");
1845 dumpwins(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
1851 #endif /* SWM_DEBUG */
1856 int saved_errno
, status
;
1859 saved_errno
= errno
;
1863 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1868 if (errno
!= ECHILD
)
1869 warn("sighdlr: waitpid");
1870 #endif /* SWM_DEBUG */
1873 if (pid
== searchpid
)
1877 if (WIFEXITED(status
)) {
1878 if (WEXITSTATUS(status
) != 0)
1879 warnx("sighdlr: child exit status: %d",
1880 WEXITSTATUS(status
));
1882 warnx("sighdlr: child is terminated "
1884 #endif /* SWM_DEBUG */
1898 errno
= saved_errno
;
1904 struct pid_e
*p
= NULL
;
1906 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1911 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1920 name_to_pixel(int sidx
, const char *colorname
)
1922 uint32_t result
= 0;
1923 char cname
[32] = "#";
1924 xcb_screen_t
*screen
;
1925 xcb_colormap_t cmap
;
1926 xcb_alloc_color_reply_t
*cr
;
1927 xcb_alloc_named_color_reply_t
*nr
;
1928 uint16_t rr
, gg
, bb
;
1930 screen
= get_screen(sidx
);
1931 cmap
= screen
->default_colormap
;
1933 /* color is in format rgb://rr/gg/bb */
1934 if (strncmp(colorname
, "rgb:", 4) == 0) {
1935 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1936 warnx("could not parse rgb %s", colorname
);
1938 cr
= xcb_alloc_color_reply(conn
,
1939 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1945 warnx("color '%s' not found", colorname
);
1948 nr
= xcb_alloc_named_color_reply(conn
,
1949 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1952 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1953 nr
= xcb_alloc_named_color_reply(conn
,
1954 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1961 warnx("color '%s' not found", colorname
);
1968 setscreencolor(const char *val
, int i
, int c
)
1970 if (i
< 0 || i
>= get_screen_count())
1973 screens
[i
].c
[c
].pixel
= name_to_pixel(i
, val
);
1974 free(screens
[i
].c
[c
].name
);
1975 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1980 fancy_stacker(struct workspace
*ws
)
1982 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1983 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1984 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1985 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1986 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1987 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1988 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1989 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1990 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1994 plain_stacker(struct workspace
*ws
)
1996 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1997 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1998 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1999 sizeof ws
->stacker
);
2000 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2001 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
2002 sizeof ws
->stacker
);
2006 custom_region(const char *val
)
2008 unsigned int x
, y
, w
, h
;
2009 int sidx
, num_screens
;
2010 xcb_screen_t
*screen
;
2012 DNPRINTF(SWM_D_CONF
, "custom_region: %s\n", val
);
2014 num_screens
= get_screen_count();
2015 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
2016 errx(1, "invalid custom region, "
2017 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
2018 if (sidx
< 1 || sidx
> num_screens
)
2019 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
2023 if ((screen
= get_screen(sidx
)) == NULL
)
2024 errx(1, "ERROR: can't get screen %d.", sidx
);
2027 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
2029 if (x
> screen
->width_in_pixels
||
2030 y
> screen
->height_in_pixels
||
2031 w
+ x
> screen
->width_in_pixels
||
2032 h
+ y
> screen
->height_in_pixels
) {
2033 warnx("ignoring region %ux%u+%u+%u - not within screen "
2034 "boundaries (%ux%u)", w
, h
, x
, y
,
2035 screen
->width_in_pixels
, screen
->height_in_pixels
);
2039 new_region(&screens
[sidx
], x
, y
, w
, h
);
2043 socket_setnonblock(int fd
)
2047 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
2048 err(1, "fcntl F_GETFL");
2049 flags
|= O_NONBLOCK
;
2050 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
2051 err(1, "fcntl F_SETFL");
2055 bar_print_legacy(struct swm_region
*r
, const char *s
)
2057 xcb_rectangle_t rect
;
2062 XRectangle ibox
, lbox
;
2066 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
2068 switch (bar_justify
) {
2069 case SWM_BAR_JUSTIFY_LEFT
:
2072 case SWM_BAR_JUSTIFY_CENTER
:
2073 x
= (WIDTH(r
) - lbox
.width
) / 2;
2075 case SWM_BAR_JUSTIFY_RIGHT
:
2076 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
2080 if (x
< SWM_BAR_OFFSET
)
2085 rect
.width
= WIDTH(r
->bar
);
2086 rect
.height
= HEIGHT(r
->bar
);
2088 /* clear back buffer */
2089 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2090 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2091 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2093 /* draw back buffer */
2094 gcvd
.graphics_exposures
= 0;
2095 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
2096 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2097 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
2098 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
2100 XFreeGC(display
, draw
);
2103 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2104 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2108 bar_print(struct swm_region
*r
, const char *s
)
2111 xcb_rectangle_t rect
;
2119 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
2121 switch (bar_justify
) {
2122 case SWM_BAR_JUSTIFY_LEFT
:
2125 case SWM_BAR_JUSTIFY_CENTER
:
2126 x
= (WIDTH(r
) - info
.width
) / 2;
2128 case SWM_BAR_JUSTIFY_RIGHT
:
2129 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
2133 if (x
< SWM_BAR_OFFSET
)
2138 rect
.width
= WIDTH(r
->bar
);
2139 rect
.height
= HEIGHT(r
->bar
);
2141 /* clear back buffer */
2142 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2143 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2144 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2146 /* draw back buffer */
2147 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
2148 DefaultVisual(display
, r
->s
->idx
),
2149 DefaultColormap(display
, r
->s
->idx
));
2151 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
2152 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
2155 XftDrawDestroy(draw
);
2158 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2159 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2163 bar_extra_stop(void)
2167 bzero(bar_pipe
, sizeof bar_pipe
);
2170 kill(bar_pid
, SIGTERM
);
2173 strlcpy(bar_ext
, "", sizeof bar_ext
);
2178 bar_window_class(char *s
, size_t sz
, struct swm_region
*r
)
2180 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2182 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
2183 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
2187 bar_window_instance(char *s
, size_t sz
, struct swm_region
*r
)
2189 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2191 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
2192 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
2196 bar_window_class_instance(char *s
, size_t sz
, struct swm_region
*r
)
2198 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2201 bar_window_class(s
, sz
, r
);
2202 strlcat(s
, ":", sz
);
2203 bar_window_instance(s
, sz
, r
);
2207 bar_window_state(char *s
, size_t sz
, struct swm_region
*r
)
2209 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2211 if (MAXIMIZED(r
->ws
->focus
))
2212 strlcat(s
, "(m)", sz
);
2213 else if (ABOVE(r
->ws
->focus
))
2214 strlcat(s
, "(f)", sz
);
2218 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
2222 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2225 title
= get_win_name(r
->ws
->focus
->id
);
2226 strlcat(s
, title
, sz
);
2231 get_urgent(struct ws_win
*win
)
2233 xcb_icccm_wm_hints_t hints
;
2234 xcb_get_property_cookie_t c
;
2235 bool urgent
= false;
2238 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2239 if (xcb_icccm_get_wm_hints_reply(conn
, c
, &hints
, NULL
))
2240 urgent
= xcb_icccm_wm_hints_get_urgency(&hints
);
2247 bar_urgent(char *s
, size_t sz
)
2250 int i
, j
, num_screens
;
2251 bool urgent
[SWM_WS_MAX
];
2254 for (i
= 0; i
< workspace_limit
; i
++)
2257 num_screens
= get_screen_count();
2258 for (i
= 0; i
< num_screens
; i
++)
2259 for (j
= 0; j
< workspace_limit
; j
++)
2260 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2261 if (get_urgent(win
))
2264 for (i
= 0; i
< workspace_limit
; i
++) {
2266 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2268 } else if (!urgent_collapse
) {
2269 strlcat(s
, "- ", sz
);
2272 if(urgent_collapse
&& s
[0])
2273 s
[strlen(s
) - 1] = 0;
2277 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2279 if (r
== NULL
|| r
->ws
== NULL
)
2281 if (r
->ws
->name
!= NULL
)
2282 strlcat(s
, r
->ws
->name
, sz
);
2285 /* build the default bar format according to the defined enabled options */
2287 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2291 /* if format provided, just copy the buffers */
2292 if (bar_format
!= NULL
) {
2293 strlcpy(fmtnew
, fmtexp
, sz
);
2297 /* reset the output buffer */
2300 strlcat(fmtnew
, "+N:+I ", sz
);
2302 strlcat(fmtnew
, "+S", sz
);
2303 strlcat(fmtnew
, " ", sz
);
2305 /* only show the workspace name if there's actually one */
2306 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2307 strlcat(fmtnew
, "<+D>", sz
);
2309 /* If enabled, only show the iconic count if there are iconic wins. */
2310 if (iconic_enabled
&& r
!= NULL
&& r
->ws
!= NULL
)
2311 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2313 strlcat(fmtnew
, "{+M}", sz
);
2317 strlcat(fmtnew
, "+3<", sz
);
2319 if (clock_enabled
) {
2320 strlcat(fmtnew
, fmtexp
, sz
);
2321 strlcat(fmtnew
, "+4<", sz
);
2324 /* bar_urgent already adds the space before the last asterisk */
2326 strlcat(fmtnew
, (urgent_collapse
? "*+U*+4<" : "* +U*+4<"), sz
);
2328 if (window_class_enabled
) {
2329 strlcat(fmtnew
, "+C", sz
);
2330 if (!window_instance_enabled
)
2331 strlcat(fmtnew
, "+4<", sz
);
2334 /* checks needed by the colon and floating strlcat(3) calls below */
2335 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2336 if (window_instance_enabled
) {
2337 if (window_class_enabled
)
2338 strlcat(fmtnew
, ":", sz
);
2339 strlcat(fmtnew
, "+T+4<", sz
);
2341 if (window_name_enabled
) {
2342 if (ABOVE(r
->ws
->focus
) || MAXIMIZED(r
->ws
->focus
))
2343 strlcat(fmtnew
, "+F ", sz
);
2344 strlcat(fmtnew
, "+64W ", sz
);
2348 /* finally add the action script output and the version */
2349 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2353 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2355 /* special case; no limit given, pad one space, instead */
2356 if (*limit
== (int)sz
- 1)
2358 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2361 /* replaces the bar format character sequences (like in tmux(1)) */
2363 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2368 char tmp
[SWM_BAR_MAX
];
2369 int limit
, size
, count
;
2372 /* reset strlcat(3) buffer */
2375 /* get number, if any */
2378 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2379 limit
= sizeof tmp
- 1;
2380 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2381 limit
= sizeof tmp
- 1;
2383 /* there is nothing to replace (ie EOL) */
2390 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2393 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2396 bar_window_class(tmp
, sizeof tmp
, r
);
2399 bar_workspace_name(tmp
, sizeof tmp
, r
);
2402 bar_window_state(tmp
, sizeof tmp
, r
);
2405 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2409 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2413 snprintf(tmp
, sizeof tmp
, "%d", count
);
2416 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2419 bar_window_class_instance(tmp
, sizeof tmp
, r
);
2422 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2425 bar_window_instance(tmp
, sizeof tmp
, r
);
2428 bar_urgent(tmp
, sizeof tmp
);
2431 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2434 bar_window_name(tmp
, sizeof tmp
, r
);
2437 /* unknown character sequence; copy as-is */
2438 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2444 if ((int)len
< limit
)
2446 while (limit
-- > 0) {
2447 if (*offrep
>= sz
- 1)
2449 fmtrep
[(*offrep
)++] = *ptr
++;
2457 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2462 while (*fmt
!= '\0') {
2464 /* skip ordinary characters */
2467 fmtrep
[off
++] = *fmt
++;
2471 /* character sequence found; replace it */
2472 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2481 bar_fmt_expand(char *fmtexp
, size_t sz
)
2488 /* start by grabbing the current time and date */
2490 localtime_r(&tmt
, &tm
);
2492 /* figure out what to expand */
2493 if (bar_format
!= NULL
)
2495 else if (bar_format
== NULL
&& clock_enabled
)
2497 /* if nothing to expand bail out */
2503 /* copy as-is, just in case the format shouldn't be expanded below */
2504 strlcpy(fmtexp
, fmt
, sz
);
2505 /* finally pass the string through strftime(3) */
2506 #ifndef SWM_DENY_CLOCK_FORMAT
2507 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2508 warnx("format too long");
2513 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2517 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2518 char fmtrep
[SWM_BAR_MAX
];
2520 struct swm_region
*r
;
2522 /* expand the format by first passing it through strftime(3) */
2523 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2525 num_screens
= get_screen_count();
2526 for (i
= 0; i
< num_screens
; i
++) {
2527 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2531 if (bar_enabled
&& r
->ws
->bar_enabled
)
2532 xcb_map_window(conn
, r
->bar
->id
);
2534 xcb_unmap_window(conn
, r
->bar
->id
);
2538 if (startup_exception
)
2539 snprintf(fmtrep
, sizeof fmtrep
, "total "
2540 "exceptions: %d, first exception: %s",
2544 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2545 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2547 if (bar_font_legacy
)
2548 bar_print_legacy(r
, fmtrep
);
2550 bar_print(r
, fmtrep
);
2556 * Reads external script output; call when stdin is readable.
2557 * Returns 1 if bar_ext was updated; otherwise 0.
2560 bar_extra_update(void)
2563 char b
[SWM_BAR_MAX
];
2564 bool changed
= false;
2569 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2572 if (b
[len
- 1] == '\n') {
2573 /* Remove newline. */
2576 /* "Clear" bar_ext. */
2579 /* Flush buffered output. */
2580 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2581 bar_ext_buf
[0] = '\0';
2583 /* Append new output to bar. */
2584 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2588 /* Buffer output. */
2589 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2594 if (errno
!= EAGAIN
) {
2595 warn("bar_action failed");
2604 bar_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
2606 struct swm_region
*tmpr
;
2609 /* suppress unused warnings since vars are needed */
2614 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2617 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2618 /* Only change if master switch is enabled. */
2620 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2622 bar_enabled
= r
->ws
->bar_enabled
= true;
2624 case SWM_ARG_ID_BAR_TOGGLE
:
2625 bar_enabled
= !bar_enabled
;
2629 /* update bars as necessary */
2630 num_screens
= get_screen_count();
2631 for (i
= 0; i
< num_screens
; i
++)
2632 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2634 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2635 xcb_map_window(conn
, tmpr
->bar
->id
);
2637 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2642 /* must be after stack */
2649 bar_extra_setup(void)
2651 /* do this here because the conf file is in memory */
2652 if (!bar_extra
&& bar_argv
[0]) {
2653 /* launch external status app */
2655 if (pipe(bar_pipe
) == -1)
2656 err(1, "pipe error");
2657 socket_setnonblock(bar_pipe
[0]);
2658 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2660 /* Set stdin to read from the pipe. */
2661 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2664 /* Set stdout to write to the pipe. */
2665 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2668 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2669 err(1, "could not disable SIGPIPE");
2670 switch (bar_pid
= fork()) {
2672 err(1, "cannot fork");
2676 execvp(bar_argv
[0], bar_argv
);
2677 err(1, "%s external app failed", bar_argv
[0]);
2679 default: /* parent */
2684 atexit(kill_bar_extra_atexit
);
2689 kill_bar_extra_atexit(void)
2692 kill(bar_pid
, SIGTERM
);
2700 while ((s
= index(s
, '-'))) {
2705 return (count
== 14);
2711 char *default_string
;
2712 char **missing_charsets
;
2713 int num_missing_charsets
= 0;
2717 XFreeFontSet(display
, bar_fs
);
2721 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n", bar_fonts
);
2723 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2724 &num_missing_charsets
, &default_string
);
2726 if (num_missing_charsets
> 0) {
2727 warnx("Unable to load charset(s):");
2729 for (i
= 0; i
< num_missing_charsets
; ++i
)
2730 warnx("%s", missing_charsets
[i
]);
2732 XFreeStringList(missing_charsets
);
2734 if (strcmp(default_string
, ""))
2735 warnx("Glyphs from those sets will be replaced "
2736 "by '%s'.", default_string
);
2738 warnx("Glyphs from those sets won't be drawn.");
2742 errx(1, "Error creating font set structure.");
2744 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2746 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2747 2 * bar_border_width
;
2754 xft_init(struct swm_region
*r
)
2756 char *font
, *str
, *search
;
2759 if (bar_font
== NULL
) {
2760 if ((search
= str
= strdup(bar_fonts
)) == NULL
)
2761 errx(1, "insufficient memory.");
2763 while ((font
= strsep(&search
, ",")) != NULL
) {
2767 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2770 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2773 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2777 if (bar_font
== NULL
) {
2778 warnx("unable to load font %s", font
);
2781 DNPRINTF(SWM_D_INIT
, "successfully opened "
2789 if (bar_font
== NULL
)
2790 errx(1, "unable to open a font");
2792 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2794 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2795 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2796 warn("Xft error: unable to allocate color.");
2798 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR
].pixel
, color
);
2800 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2801 DefaultColormap(display
, r
->s
->idx
), &color
, &search_font_color
))
2802 warn("Xft error: unable to allocate color.");
2804 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2811 bar_setup(struct swm_region
*r
)
2813 xcb_screen_t
*screen
;
2816 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2819 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2820 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2825 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2826 err(1, "bar_setup: calloc: failed to allocate memory.");
2828 if (bar_font_legacy
)
2834 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2835 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2836 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2838 /* Assume region is unfocused when we create the bar. */
2839 r
->bar
->id
= xcb_generate_id(conn
);
2840 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2841 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
2842 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
2843 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
2845 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2846 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2847 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
2848 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
2849 | XCB_CW_EVENT_MASK
, wa
);
2851 /* Stack bar window above region window to start. */
2853 wa
[1] = XCB_STACK_MODE_ABOVE
;
2855 xcb_configure_window(conn
, r
->bar
->id
, XCB_CONFIG_WINDOW_SIBLING
|
2856 XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
2858 r
->bar
->buffer
= xcb_generate_id(conn
);
2859 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
2860 WIDTH(r
->bar
), HEIGHT(r
->bar
));
2863 xcb_randr_select_input(conn
, r
->bar
->id
,
2864 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
2867 xcb_map_window(conn
, r
->bar
->id
);
2869 DNPRINTF(SWM_D_BAR
, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
2870 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
2877 bar_cleanup(struct swm_region
*r
)
2881 xcb_destroy_window(conn
, r
->bar
->id
);
2882 xcb_free_pixmap(conn
, r
->bar
->buffer
);
2888 set_win_state(struct ws_win
*win
, uint8_t state
)
2890 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
2892 DNPRINTF(SWM_D_EVENT
, "set_win_state: win %#x, state: %u\n",
2898 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
2899 a_state
, 32, 2, data
);
2903 get_win_state(xcb_window_t w
)
2905 xcb_get_property_reply_t
*r
;
2906 xcb_get_property_cookie_t c
;
2907 uint32_t result
= 0;
2909 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
2910 r
= xcb_get_property_reply(conn
, c
, NULL
);
2912 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
2913 result
= *((uint32_t *)xcb_get_property_value(r
));
2917 DNPRINTF(SWM_D_MISC
, "get_win_state property: win %#x state %u\n", w
,
2923 version(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
2925 /* suppress unused warnings since vars are needed */
2930 bar_version
= !bar_version
;
2932 snprintf(bar_vertext
, sizeof bar_vertext
,
2933 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2935 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2942 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
2944 xcb_client_message_event_t ev
;
2952 name
= get_atom_name(a
);
2953 DNPRINTF(SWM_D_EVENT
, "client_msg: win %#x, atom: %s(%u), "
2955 win
->id
, name
, a
, t
);
2959 bzero(&ev
, sizeof ev
);
2960 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2961 ev
.window
= win
->id
;
2964 ev
.data
.data32
[0] = a
;
2965 ev
.data
.data32
[1] = t
;
2967 xcb_send_event(conn
, 0, win
->id
,
2968 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2971 /* synthetic response to a ConfigureRequest when not making a change */
2973 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2975 xcb_configure_notify_event_t ce
;
2980 /* send notification of unchanged state. */
2981 bzero(&ce
, sizeof(ce
));
2982 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2985 ce
.width
= WIDTH(win
);
2986 ce
.height
= HEIGHT(win
);
2987 ce
.override_redirect
= 0;
2992 ce
.window
= win
->id
;
2993 ce
.border_width
= BORDER(win
);
2994 ce
.above_sibling
= XCB_WINDOW_NONE
;
2997 ce
.event
= ev
->window
;
2998 ce
.window
= ev
->window
;
3000 /* make response appear more WM_SIZE_HINTS-compliant */
3001 if (win
->sh
.flags
) {
3002 DNPRINTF(SWM_D_MISC
, "config_win: hints: win %#x,"
3003 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
3004 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
3005 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
3006 SH_INC_W(win
), SH_INC_H(win
));
3011 /* the hint may be set... to 0! */
3012 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
3013 ce
.width
= SH_MIN_W(win
);
3014 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
3015 ce
.height
= SH_MIN_H(win
);
3020 /* may also be advertized as 0 */
3021 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
3022 ce
.width
= SH_MAX_W(win
);
3023 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
3024 ce
.height
= SH_MAX_H(win
);
3027 /* resize increment. */
3029 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
3030 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
3032 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
3033 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
3037 /* adjust x and y for requested border_width. */
3038 ce
.x
+= BORDER(win
) - ev
->border_width
;
3039 ce
.y
+= BORDER(win
) - ev
->border_width
;
3040 ce
.border_width
= ev
->border_width
;
3041 ce
.above_sibling
= ev
->sibling
;
3044 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
3045 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
3046 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
3048 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
3053 count_win(struct workspace
*ws
, bool count_transient
)
3058 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3059 if (!count_transient
&& FLOATING(win
))
3065 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
3071 quit(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3073 /* suppress unused warnings since vars are needed */
3078 DNPRINTF(SWM_D_MISC
, "quit\n");
3083 lower_window(struct ws_win
*win
)
3085 struct ws_win
*target
= NULL
;
3086 struct workspace
*ws
;
3088 DNPRINTF(SWM_D_EVENT
, "lower_window: win %#x\n", WINID(win
));
3095 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3096 if (target
== win
|| ICONIC(target
))
3098 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3101 if (win
->transient
== target
->transient
)
3103 if (win
->transient
== target
->id
)
3106 if (FULLSCREEN(target
))
3108 if (FULLSCREEN(win
))
3110 if (MAXIMIZED(target
))
3114 if (ABOVE(target
) || TRANS(target
))
3116 if (ABOVE(win
) || TRANS(win
))
3120 /* Change stack position. */
3121 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3123 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3125 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3127 update_win_stacking(win
);
3130 if (swm_debug
& SWM_D_STACK
) {
3131 DPRINTF("=== stacking order (top down) === \n");
3132 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3133 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3134 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3135 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3136 YESNO(ICONIC(target
)));
3140 DNPRINTF(SWM_D_EVENT
, "lower_window: done\n");
3144 raise_window(struct ws_win
*win
)
3146 struct ws_win
*target
= NULL
;
3147 struct workspace
*ws
;
3149 DNPRINTF(SWM_D_EVENT
, "raise_window: win %#x\n", WINID(win
));
3156 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3157 if (target
== win
|| ICONIC(target
))
3159 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3161 if (TRANS(win
) && (win
->transient
== target
->transient
||
3162 win
->transient
== target
->id
))
3164 if (FULLSCREEN(win
))
3166 if (FULLSCREEN(target
))
3170 if (MAXIMIZED(target
))
3172 if (ABOVE(win
) || TRANS(win
) ||
3173 (win
->ws
->focus
== win
&& ws
->always_raise
))
3175 if (!ABOVE(target
) && !TRANS(target
))
3179 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3181 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3183 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3185 update_win_stacking(win
);
3188 if (swm_debug
& SWM_D_STACK
) {
3189 DPRINTF("=== stacking order (top down) === \n");
3190 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3191 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3192 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3193 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3194 YESNO(ICONIC(target
)));
3198 DNPRINTF(SWM_D_EVENT
, "raise_window: done\n");
3202 update_win_stacking(struct ws_win
*win
)
3204 struct ws_win
*sibling
;
3205 struct swm_region
*r
;
3208 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
3211 sibling
= TAILQ_NEXT(win
, stack_entry
);
3212 if (sibling
!= NULL
&& (FLOATING(win
) == FLOATING(sibling
) ||
3213 (win
->ws
->always_raise
&& win
->ws
->focus
== win
)))
3214 val
[0] = sibling
->id
;
3215 else if (FLOATING(win
) || (win
->ws
->always_raise
&&
3216 win
->ws
->focus
== win
))
3217 val
[0] = r
->bar
->id
;
3221 DNPRINTF(SWM_D_EVENT
, "update_win_stacking: %#x, sibling %#x\n",
3224 val
[1] = XCB_STACK_MODE_ABOVE
;
3226 xcb_configure_window(conn
, win
->id
, XCB_CONFIG_WINDOW_SIBLING
|
3227 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
3231 map_window(struct ws_win
*win
)
3236 DNPRINTF(SWM_D_EVENT
, "map_window: win %#x, mapped: %s\n",
3237 win
->id
, YESNO(win
->mapped
));
3242 xcb_map_window(conn
, win
->id
);
3243 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
3248 unmap_window(struct ws_win
*win
)
3253 DNPRINTF(SWM_D_EVENT
, "unmap_window: win %#x, mapped: %s\n", win
->id
,
3254 YESNO(win
->mapped
));
3259 xcb_unmap_window(conn
, win
->id
);
3260 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
3261 win
->mapped
= false;
3268 int i
, j
, num_screens
;
3270 num_screens
= get_screen_count();
3271 for (i
= 0; i
< num_screens
; i
++)
3272 for (j
= 0; j
< workspace_limit
; j
++)
3273 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3278 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
3280 xcb_key_press_event_t event
;
3281 xcb_keycode_t
*keycode
;
3286 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
3288 DNPRINTF(SWM_D_MISC
, "fake_keypress: win %#x, keycode %u\n",
3291 bzero(&event
, sizeof(event
));
3292 event
.event
= win
->id
;
3293 event
.root
= win
->s
->root
;
3294 event
.child
= XCB_WINDOW_NONE
;
3295 event
.time
= XCB_CURRENT_TIME
;
3296 event
.event_x
= X(win
);
3297 event
.event_y
= Y(win
);
3300 event
.same_screen
= 1;
3301 event
.detail
= *keycode
;
3302 event
.state
= modifiers
;
3304 event
.response_type
= XCB_KEY_PRESS
;
3305 xcb_send_event(conn
, 1, win
->id
,
3306 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
3308 event
.response_type
= XCB_KEY_RELEASE
;
3309 xcb_send_event(conn
, 1, win
->id
,
3310 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
3316 restart(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3318 /* suppress unused warning since var is needed */
3323 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
3327 execvp(start_argv
[0], start_argv
);
3328 warn("execvp failed");
3329 quit(NULL
, NULL
, NULL
);
3333 get_pointer_win(xcb_window_t root
)
3335 struct ws_win
*win
= NULL
;
3336 xcb_query_pointer_reply_t
*r
;
3338 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: %#x.\n", root
);
3340 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
3342 win
= find_window(r
->child
);
3344 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: %#x.\n",
3347 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
3356 center_pointer(struct swm_region
*r
)
3360 if (!warp_pointer
|| r
== NULL
)
3365 DNPRINTF(SWM_D_EVENT
, "center_pointer: win %#x.\n", WINID(win
));
3367 if (win
&& win
->mapped
)
3368 xcb_warp_pointer(conn
, XCB_NONE
, win
->id
, 0, 0, 0, 0,
3369 WIDTH(win
) / 2, HEIGHT(win
) / 2);
3371 xcb_warp_pointer(conn
, XCB_NONE
, r
->id
, 0, 0, 0, 0,
3372 WIDTH(r
) / 2, HEIGHT(r
) / 2);
3376 root_to_region(xcb_window_t root
, int check
)
3379 struct swm_region
*r
= NULL
;
3381 xcb_query_pointer_reply_t
*qpr
;
3382 xcb_get_input_focus_reply_t
*gifr
;
3384 DNPRINTF(SWM_D_MISC
, "root_to_region: win %#x\n", root
);
3386 num_screens
= get_screen_count();
3387 for (i
= 0; i
< num_screens
; i
++)
3388 if (screens
[i
].root
== root
)
3391 if (check
& SWM_CK_REGION
)
3392 r
= screens
[i
].r_focus
;
3394 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
3395 /* Try to find an actively focused window */
3396 gifr
= xcb_get_input_focus_reply(conn
,
3397 xcb_get_input_focus(conn
), NULL
);
3399 cfw
= find_window(gifr
->focus
);
3400 if (cfw
&& cfw
->ws
->r
)
3407 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
3408 /* No region with an active focus; try to use pointer. */
3409 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
3410 screens
[i
].root
), NULL
);
3412 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
3413 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
3414 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3415 if (X(r
) <= qpr
->root_x
&&
3416 qpr
->root_x
< MAX_X(r
) &&
3417 Y(r
) <= qpr
->root_y
&&
3418 qpr
->root_y
< MAX_Y(r
))
3425 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
3426 r
= TAILQ_FIRST(&screens
[i
].rl
);
3428 DNPRINTF(SWM_D_MISC
, "root_to_region: idx: %d\n", get_region_index(r
));
3434 find_unmanaged_window(xcb_window_t id
)
3437 int i
, j
, num_screens
;
3439 num_screens
= get_screen_count();
3440 for (i
= 0; i
< num_screens
; i
++)
3441 for (j
= 0; j
< workspace_limit
; j
++)
3442 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
3450 find_window(xcb_window_t id
)
3453 int i
, j
, num_screens
;
3454 xcb_query_tree_reply_t
*r
;
3456 num_screens
= get_screen_count();
3457 for (i
= 0; i
< num_screens
; i
++)
3458 for (j
= 0; j
< workspace_limit
; j
++)
3459 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3463 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3467 /* if we were looking for the parent return that window instead */
3468 if (r
->parent
== 0 || r
->root
== r
->parent
) {
3473 /* look for parent */
3474 for (i
= 0; i
< num_screens
; i
++)
3475 for (j
= 0; j
< workspace_limit
; j
++)
3476 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3477 if (r
->parent
== win
->id
) {
3487 spawn(int ws_idx
, union arg
*args
, bool close_fd
)
3492 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3494 close(xcb_get_file_descriptor(conn
));
3496 setenv("LD_PRELOAD", SWM_LIB
, 1);
3498 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3499 warn("spawn: asprintf SWM_WS");
3502 setenv("_SWM_WS", ret
, 1);
3506 if (asprintf(&ret
, "%d", getpid()) == -1) {
3507 warn("spawn: asprintf _SWM_PID");
3510 setenv("_SWM_PID", ret
, 1);
3514 if (setsid() == -1) {
3515 warn("spawn: setsid");
3521 * close stdin and stdout to prevent interaction between apps
3522 * and the baraction script
3523 * leave stderr open to record errors
3525 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3526 warn("spawn: open");
3529 dup2(fd
, STDIN_FILENO
);
3530 dup2(fd
, STDOUT_FILENO
);
3535 execvp(args
->argv
[0], args
->argv
);
3537 warn("spawn: execvp");
3542 kill_refs(struct ws_win
*win
)
3544 struct workspace
*ws
;
3546 int i
, j
, num_screens
;
3551 num_screens
= get_screen_count();
3552 for (i
= 0; i
< num_screens
; i
++) {
3553 for (j
= 0; j
< workspace_limit
; j
++) {
3554 ws
= &screens
[i
].ws
[j
];
3556 if (win
== ws
->focus
)
3558 if (win
== ws
->focus_prev
)
3559 ws
->focus_prev
= NULL
;
3562 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3563 if (win
== w
->focus_child
)
3564 w
->focus_child
= NULL
;
3570 validate_win(struct ws_win
*testwin
)
3573 struct workspace
*ws
;
3574 struct swm_region
*r
;
3575 int i
, x
, num_screens
;
3577 if (testwin
== NULL
)
3580 num_screens
= get_screen_count();
3581 for (i
= 0; i
< num_screens
; i
++)
3582 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3583 for (x
= 0; x
< workspace_limit
; x
++) {
3585 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3593 validate_ws(struct workspace
*testws
)
3595 struct swm_region
*r
;
3596 struct workspace
*ws
;
3597 int i
, x
, num_screens
;
3599 /* validate all ws */
3600 num_screens
= get_screen_count();
3601 for (i
= 0; i
< num_screens
; i
++)
3602 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3603 for (x
= 0; x
< workspace_limit
; x
++) {
3612 unfocus_win(struct ws_win
*win
)
3614 xcb_window_t none
= XCB_WINDOW_NONE
;
3616 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: win %#x\n", WINID(win
));
3621 if (win
->ws
== NULL
) {
3622 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3626 if (validate_ws(win
->ws
)) {
3627 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3631 if (win
->ws
->r
== NULL
) {
3632 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3636 if (validate_win(win
)) {
3637 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3642 if (win
->ws
->focus
== win
) {
3643 win
->ws
->focus
= NULL
;
3644 win
->ws
->focus_prev
= win
;
3647 if (validate_win(win
->ws
->focus
)) {
3648 kill_refs(win
->ws
->focus
);
3649 win
->ws
->focus
= NULL
;
3652 if (validate_win(win
->ws
->focus_prev
)) {
3653 kill_refs(win
->ws
->focus_prev
);
3654 win
->ws
->focus_prev
= NULL
;
3657 update_window_color(win
);
3659 /* Raise window to "top unfocused position." */
3660 if (win
->ws
->always_raise
)
3663 /* Update border width */
3664 if (win
->bordered
&& (win
->quirks
& SWM_Q_MINIMALBORDER
) &&
3667 X(win
) += border_width
;
3668 Y(win
) += border_width
;
3672 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3673 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3675 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3679 focus_win(struct ws_win
*win
)
3681 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
, *tmpw
;
3682 struct workspace
*ws
;
3683 xcb_get_input_focus_reply_t
*gifr
;
3685 DNPRINTF(SWM_D_FOCUS
, "focus_win: win %#x\n", WINID(win
));
3687 if (win
== NULL
|| win
->ws
== NULL
|| !win
->mapped
)
3692 if (validate_ws(ws
))
3695 if (validate_win(win
)) {
3700 gifr
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3702 cfw
= find_window(gifr
->focus
);
3703 if (cfw
!= NULL
&& cfw
!= win
) {
3704 if (cfw
->ws
!= ws
&& cfw
->ws
->r
!= NULL
) {
3705 /* Change border to unfocused color. */
3706 xcb_change_window_attributes(conn
, cfw
->id
,
3707 XCB_CW_BORDER_PIXEL
,
3708 &cfw
->s
->c
[(MAXIMIZED(cfw
) ?
3709 SWM_S_COLOR_UNFOCUS_MAXIMIZED
:
3710 SWM_S_COLOR_UNFOCUS
)].pixel
);
3712 /* Update border width */
3713 if (cfw
->bordered
&&
3714 (cfw
->quirks
& SWM_Q_MINIMALBORDER
) &&
3717 X(cfw
) += border_width
;
3718 Y(cfw
) += border_width
;
3728 if (ws
->focus
!= win
) {
3729 if (ws
->focus
&& ws
->focus
!= cfw
)
3730 unfocus_win(ws
->focus
);
3734 /* Clear focus child redirect. */
3735 win
->focus_child
= NULL
;
3737 /* If transient, adjust parent's focus child for focus_magic. */
3739 parent
= find_window(win
->transient
);
3740 if (parent
&& parent
->focus_child
!= win
)
3741 parent
->focus_child
= win
;
3744 if (cfw
!= win
&& ws
->r
!= NULL
) {
3745 /* Set input focus if no input hint, or indicated by hint. */
3746 if (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
3748 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3749 win
->id
, last_event_time
);
3751 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3752 ws
->r
->id
, XCB_CURRENT_TIME
);
3754 /* Tell app it can adjust focus to a specific window. */
3755 if (win
->take_focus
) {
3756 /* java is special; always tell parent */
3757 if (TRANS(win
) && win
->java
)
3758 client_msg(parent
, a_takefocus
,
3761 client_msg(win
, a_takefocus
, last_event_time
);
3764 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
3766 /* If a parent exists, map it first. */
3768 raise_window(parent
);
3771 /* Map siblings next. */
3772 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
,
3774 if (w
!= win
&& !ICONIC(w
) &&
3775 w
->transient
== parent
->id
) {
3781 /* Map focused window. */
3785 /* Stack any children of focus window. */
3786 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
3787 if (w
->transient
== win
->id
&& !ICONIC(w
)) {
3791 } else if (tile_gap
< 0 && !ABOVE(win
)) {
3793 * Windows overlap in the layout.
3794 * Raise focused win above all tiled wins.
3802 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3803 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3808 /* Update window border even if workspace is hidden. */
3809 update_window_color(win
);
3811 /* Update border width */
3812 if (!win
->bordered
&& WS_FOCUSED(win
->ws
) &&
3813 (win
->quirks
& SWM_Q_MINIMALBORDER
) && FLOATING(win
)) {
3815 X(win
) -= border_width
;
3816 Y(win
) -= border_width
;
3824 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
3827 /* If a child window should have focus instead, return it. */
3829 get_focus_magic(struct ws_win
*win
)
3831 struct ws_win
*parent
= NULL
;
3832 struct ws_win
*child
= NULL
;
3834 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: win %#x\n", WINID(win
));
3839 parent
= find_window(win
->transient
);
3841 /* If parent prefers focus elsewhere, then try to do so. */
3842 if (parent
&& (child
= parent
->focus_child
)) {
3843 if (validate_win(child
) == 0 && child
->mapped
)
3846 parent
->focus_child
= NULL
;
3850 /* If this window prefers focus elsewhere, then try to do so. */
3851 if ((child
= win
->focus_child
)) {
3852 if (validate_win(child
) == 0 && child
->mapped
)
3855 win
->focus_child
= NULL
;
3862 event_drain(uint8_t rt
)
3864 xcb_generic_event_t
*evt
;
3866 /* ensure all pending requests have been processed before filtering. */
3868 while ((evt
= get_next_event(false))) {
3869 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
3877 set_region(struct swm_region
*r
)
3879 struct swm_region
*rf
;
3886 /* Unfocus old region bar. */
3891 xcb_change_window_attributes(conn
, rf
->bar
->id
,
3892 XCB_CW_BORDER_PIXEL
,
3893 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
3896 if (rf
!= NULL
&& rf
!= r
&& (X(rf
) != X(r
) || Y(rf
) != Y(r
) ||
3897 WIDTH(rf
) != WIDTH(r
) || HEIGHT(rf
) != HEIGHT(r
))) {
3898 /* Set _NET_DESKTOP_GEOMETRY. */
3900 vals
[1] = HEIGHT(r
);
3901 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, r
->s
->root
,
3902 ewmh
[_NET_DESKTOP_GEOMETRY
].atom
, XCB_ATOM_CARDINAL
, 32, 2,
3906 /* Set region bar border to focus_color. */
3907 xcb_change_window_attributes(conn
, r
->bar
->id
,
3908 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
3912 ewmh_update_current_desktop();
3916 focus_region(struct swm_region
*r
)
3919 struct swm_region
*old_r
;
3924 old_r
= r
->s
->r_focus
;
3927 nfw
= get_region_focus(r
);
3931 /* New region is empty; need to manually unfocus win. */
3933 unfocus_win(old_r
->ws
->focus
);
3935 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
3938 /* Clear bar since empty. */
3944 switchws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3946 struct swm_region
*this_r
, *other_r
;
3948 struct workspace
*new_ws
, *old_ws
;
3949 xcb_window_t none
= XCB_WINDOW_NONE
;
3950 int wsid
= args
->id
;
3951 bool unmap_old
= false;
3958 if (wsid
>= workspace_limit
)
3962 old_ws
= this_r
->ws
;
3963 new_ws
= &this_r
->s
->ws
[wsid
];
3965 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3966 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
3968 if (new_ws
== NULL
|| old_ws
== NULL
)
3970 if (new_ws
== old_ws
)
3973 other_r
= new_ws
->r
;
3974 if (other_r
&& workspace_clamp
) {
3975 DNPRINTF(SWM_D_WS
, "switchws: ws clamped.\n");
3978 DNPRINTF(SWM_D_WS
, "switchws: warping focus to region "
3979 "with ws %d.\n", wsid
);
3980 focus_region(other_r
);
3981 center_pointer(other_r
);
3987 if ((win
= old_ws
->focus
) != NULL
) {
3988 update_window_color(win
);
3990 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3991 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3996 /* the other ws is visible in another region, exchange them */
3997 other_r
->ws_prior
= new_ws
;
3998 other_r
->ws
= old_ws
;
3999 old_ws
->r
= other_r
;
4001 /* the other workspace is hidden, hide this one */
4006 this_r
->ws_prior
= old_ws
;
4007 this_r
->ws
= new_ws
;
4010 /* Set focus_pending before stacking, if needed. */
4011 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
4012 validate_win(new_ws
->focus_pending
))) {
4013 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
4014 new_ws
->focus
= new_ws
->focus_prev
;
4015 new_ws
->focus_prev
= NULL
;
4018 new_ws
->state
= SWM_WS_STATE_MAPPING
;
4021 /* unmap old windows */
4023 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
4025 old_ws
->state
= SWM_WS_STATE_HIDDEN
;
4028 /* if workspaces were swapped, then don't wait to set focus */
4029 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
4030 if (new_ws
->focus_pending
) {
4031 focus_win(new_ws
->focus_pending
);
4032 new_ws
->focus_pending
= NULL
;
4036 /* Clear bar and set focus on region input win if new ws is empty. */
4037 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
4038 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
4043 ewmh_update_current_desktop();
4047 new_ws
->state
= SWM_WS_STATE_MAPPED
;
4049 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
4053 cyclews(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4056 struct swm_screen
*s
= r
->s
;
4057 bool cycle_all
= false, mv
= false;
4059 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4060 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4066 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
4069 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
4072 case SWM_ARG_ID_CYCLEWS_UP
:
4073 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
4075 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
4078 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
4081 case SWM_ARG_ID_CYCLEWS_DOWN
:
4082 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
4089 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
4091 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
4095 send_to_ws(bp
, r
, &a
);
4097 switchws(bp
, r
, &a
);
4098 } while (a
.id
!= r
->ws
->idx
);
4100 DNPRINTF(SWM_D_FOCUS
, "cyclews: done\n");
4104 priorws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4110 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4111 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4113 if (r
->ws_prior
== NULL
)
4116 a
.id
= r
->ws_prior
->idx
;
4117 switchws(bp
, r
, &a
);
4118 DNPRINTF(SWM_D_FOCUS
, "priorws: done\n");
4122 focusrg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4124 int ridx
= args
->id
, i
, num_screens
;
4125 struct swm_region
*rr
= NULL
;
4129 num_screens
= get_screen_count();
4130 /* do nothing if we don't have more than one screen */
4131 if (!(num_screens
> 1 || outputs
> 1))
4134 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
4136 rr
= TAILQ_FIRST(&r
->s
->rl
);
4137 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
4138 rr
= TAILQ_NEXT(rr
, entry
);
4146 DNPRINTF(SWM_D_FOCUS
, "focusrg: done\n");
4150 cyclerg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4153 struct swm_region
*rr
= NULL
;
4156 num_screens
= get_screen_count();
4157 /* do nothing if we don't have more than one screen */
4158 if (!(num_screens
> 1 || outputs
> 1))
4162 DNPRINTF(SWM_D_FOCUS
, "cyclerg: id: %d, region: %d\n", args
->id
, i
);
4165 case SWM_ARG_ID_CYCLERG_UP
:
4166 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4167 rr
= TAILQ_NEXT(r
, entry
);
4169 rr
= TAILQ_FIRST(&screens
[i
].rl
);
4171 case SWM_ARG_ID_CYCLERG_DOWN
:
4172 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4173 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
4175 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
4184 case SWM_ARG_ID_CYCLERG_UP
:
4185 case SWM_ARG_ID_CYCLERG_DOWN
:
4190 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4191 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4193 switchws(bp
, r
, &a
);
4199 DNPRINTF(SWM_D_FOCUS
, "cyclerg: done\n");
4202 /* Sorts transients after parent. */
4204 sort_windows(struct ws_win_list
*wl
)
4206 struct ws_win
*win
, *parent
, *nxt
;
4211 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
4212 nxt
= TAILQ_NEXT(win
, entry
);
4214 parent
= find_window(win
->transient
);
4215 if (parent
== NULL
) {
4216 warnx("not possible bug");
4219 TAILQ_REMOVE(wl
, win
, entry
);
4220 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
4226 swapwin(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4228 struct ws_win
*target
, *source
;
4229 struct ws_win
*cur_focus
;
4230 struct ws_win_list
*wl
;
4234 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4235 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4237 cur_focus
= r
->ws
->focus
;
4238 if (cur_focus
== NULL
|| FULLSCREEN(cur_focus
))
4241 /* Adjust stacking in floating layer. */
4242 if (ABOVE(cur_focus
)) {
4244 case SWM_ARG_ID_SWAPPREV
:
4245 target
= TAILQ_PREV(cur_focus
, ws_win_stack
,
4247 if (target
!= NULL
&& FLOATING(target
)) {
4248 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4250 TAILQ_INSERT_BEFORE(target
, cur_focus
,
4252 update_win_stacking(cur_focus
);
4256 case SWM_ARG_ID_SWAPNEXT
:
4257 target
= TAILQ_NEXT(cur_focus
, stack_entry
);
4258 if (target
!= NULL
&& FLOATING(target
)) {
4259 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4261 TAILQ_INSERT_AFTER(&cur_focus
->ws
->stack
,
4262 target
, cur_focus
, stack_entry
);
4263 update_win_stacking(cur_focus
);
4271 if (r
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4274 clear_maximized(r
->ws
);
4277 wl
= &source
->ws
->winlist
;
4280 case SWM_ARG_ID_SWAPPREV
:
4282 source
= find_window(source
->transient
);
4283 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
4284 if (target
&& target
->transient
)
4285 target
= find_window(target
->transient
);
4286 TAILQ_REMOVE(wl
, source
, entry
);
4288 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4290 TAILQ_INSERT_BEFORE(target
, source
, entry
);
4292 case SWM_ARG_ID_SWAPNEXT
:
4293 target
= TAILQ_NEXT(source
, entry
);
4294 /* move the parent and let the sort handle the move */
4296 source
= find_window(source
->transient
);
4297 TAILQ_REMOVE(wl
, source
, entry
);
4299 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4301 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
4303 case SWM_ARG_ID_SWAPMAIN
:
4304 target
= TAILQ_FIRST(wl
);
4305 if (target
== source
) {
4306 if (source
->ws
->focus_prev
!= NULL
&&
4307 source
->ws
->focus_prev
!= target
)
4308 source
= source
->ws
->focus_prev
;
4312 if (target
== NULL
|| source
== NULL
)
4314 source
->ws
->focus_prev
= target
;
4315 TAILQ_REMOVE(wl
, target
, entry
);
4316 TAILQ_INSERT_BEFORE(source
, target
, entry
);
4317 TAILQ_REMOVE(wl
, source
, entry
);
4318 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4320 case SWM_ARG_ID_MOVELAST
:
4321 TAILQ_REMOVE(wl
, source
, entry
);
4322 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4325 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
4330 ewmh_update_client_list();
4336 DNPRINTF(SWM_D_MOVE
, "swapwin: done\n");
4340 get_focus_prev(struct ws_win
*win
)
4342 struct ws_win
*winfocus
= NULL
;
4343 struct ws_win
*cur_focus
= NULL
;
4344 struct ws_win_list
*wl
= NULL
;
4345 struct workspace
*ws
= NULL
;
4347 if (!(win
&& win
->ws
))
4352 cur_focus
= ws
->focus
;
4354 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: win %#x, cur_focus: %#x, "
4355 "focus_prev: %#x\n", WINID(win
), WINID(cur_focus
),
4356 WINID(ws
->focus_prev
));
4358 /* pickle, just focus on whatever */
4359 if (cur_focus
== NULL
) {
4360 /* use prev_focus if valid */
4361 if (ws
->focus_prev
&& find_window(ws
->focus_prev
->id
))
4362 winfocus
= ws
->focus_prev
;
4366 /* if transient focus on parent */
4367 if (TRANS(cur_focus
)) {
4368 winfocus
= find_window(cur_focus
->transient
);
4372 /* if in max_stack try harder */
4373 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
4374 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
4375 if (cur_focus
!= ws
->focus_prev
)
4376 winfocus
= ws
->focus_prev
;
4378 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
4383 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
4385 if (winfocus
== NULL
|| winfocus
== win
) {
4386 switch (focus_close
) {
4387 case SWM_STACK_BOTTOM
:
4388 TAILQ_FOREACH(winfocus
, wl
, entry
)
4389 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4393 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4394 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4397 case SWM_STACK_ABOVE
:
4398 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
4399 while (winfocus
&& ICONIC(winfocus
))
4400 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4402 if (winfocus
== NULL
) {
4403 if (focus_close_wrap
) {
4404 TAILQ_FOREACH(winfocus
, wl
, entry
)
4405 if (!ICONIC(winfocus
) &&
4406 winfocus
!= cur_focus
)
4409 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4411 if (!ICONIC(winfocus
) &&
4412 winfocus
!= cur_focus
)
4417 case SWM_STACK_BELOW
:
4418 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
4419 while (winfocus
&& ICONIC(winfocus
))
4420 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
4423 if (winfocus
== NULL
) {
4424 if (focus_close_wrap
) {
4425 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4427 if (!ICONIC(winfocus
) &&
4428 winfocus
!= cur_focus
)
4431 TAILQ_FOREACH(winfocus
, wl
, entry
)
4432 if (!ICONIC(winfocus
) &&
4433 winfocus
!= cur_focus
)
4441 if (winfocus
== NULL
||
4442 (winfocus
&& (ICONIC(winfocus
) || winfocus
== cur_focus
))) {
4443 if (focus_default
== SWM_STACK_TOP
) {
4444 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4445 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4448 TAILQ_FOREACH(winfocus
, wl
, entry
)
4449 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4456 return get_focus_magic(winfocus
);
4460 get_region_focus(struct swm_region
*r
)
4462 struct ws_win
*winfocus
= NULL
;
4467 if (r
->ws
->focus
&& !ICONIC(r
->ws
->focus
))
4468 winfocus
= r
->ws
->focus
;
4469 else if (r
->ws
->focus_prev
&& !ICONIC(r
->ws
->focus_prev
))
4470 winfocus
= r
->ws
->focus_prev
;
4472 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
4473 if (!ICONIC(winfocus
))
4476 return get_focus_magic(winfocus
);
4480 focus(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4482 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
4483 struct ws_win_list
*wl
= NULL
;
4484 struct workspace
*ws
= NULL
;
4491 cur_focus
= r
->ws
->focus
;
4495 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d, cur_focus: %#x\n", args
->id
,
4498 /* Make sure an uniconified window has focus, if one exists. */
4499 if (cur_focus
== NULL
) {
4500 cur_focus
= TAILQ_FIRST(wl
);
4501 while (cur_focus
!= NULL
&& ICONIC(cur_focus
))
4502 cur_focus
= TAILQ_NEXT(cur_focus
, entry
);
4504 DNPRINTF(SWM_D_FOCUS
, "focus: new cur_focus: %#x\n",
4509 case SWM_ARG_ID_FOCUSPREV
:
4510 if (cur_focus
== NULL
)
4513 winfocus
= cur_focus
;
4515 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
4516 if (winfocus
== NULL
)
4517 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
4518 if (winfocus
== cur_focus
)
4520 } while (winfocus
&& (ICONIC(winfocus
) ||
4521 winfocus
->id
== cur_focus
->transient
||
4522 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4523 winfocus
->transient
== cur_focus
->transient
) ||
4524 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4526 case SWM_ARG_ID_FOCUSNEXT
:
4527 if (cur_focus
== NULL
)
4530 winfocus
= cur_focus
;
4532 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4533 if (winfocus
== NULL
)
4534 winfocus
= TAILQ_FIRST(wl
);
4535 if (winfocus
== cur_focus
)
4537 } while (winfocus
&& (ICONIC(winfocus
) ||
4538 winfocus
->id
== cur_focus
->transient
||
4539 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4540 winfocus
->transient
== cur_focus
->transient
) ||
4541 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4543 case SWM_ARG_ID_FOCUSMAIN
:
4544 if (cur_focus
== NULL
)
4547 winfocus
= TAILQ_FIRST(wl
);
4548 if (winfocus
== cur_focus
)
4549 winfocus
= cur_focus
->ws
->focus_prev
;
4551 case SWM_ARG_ID_FOCUSURGENT
:
4552 /* Search forward for the next urgent window. */
4556 for (i
= 0; i
<= workspace_limit
; ++i
) {
4558 head
= TAILQ_FIRST(&r
->s
->ws
[(ws
->idx
+ i
) %
4559 workspace_limit
].winlist
);
4562 if (head
== cur_focus
) {
4564 winfocus
= cur_focus
;
4567 } else if (get_urgent(head
)) {
4572 head
= TAILQ_NEXT(head
, entry
);
4579 /* Switch ws if new focus is on a different ws. */
4580 if (winfocus
&& winfocus
->ws
!= ws
) {
4581 a
.id
= winfocus
->ws
->idx
;
4582 switchws(bp
, r
, &a
);
4589 if (clear_maximized(ws
) > 0)
4592 focus_win(get_focus_magic(winfocus
));
4597 DNPRINTF(SWM_D_FOCUS
, "focus: done\n");
4601 focus_pointer(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4605 /* Not needed for buttons since this is already done in buttonpress. */
4606 if (bp
->type
== KEYBIND
) {
4607 focus_win(get_pointer_win(r
->s
->root
));
4613 cycle_layout(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4615 struct workspace
*ws
= r
->ws
;
4617 /* suppress unused warning since var is needed */
4621 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
4624 if (ws
->cur_layout
->l_stack
== NULL
)
4625 ws
->cur_layout
= &layouts
[0];
4627 clear_maximized(ws
);
4632 focus_win(get_region_focus(r
));
4639 stack_config(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4641 struct workspace
*ws
= r
->ws
;
4645 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
4648 if (clear_maximized(ws
) > 0)
4651 if (ws
->cur_layout
->l_config
!= NULL
)
4652 ws
->cur_layout
->l_config(ws
, args
->id
);
4654 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
4664 struct swm_geometry g
;
4665 struct swm_region
*r
, *r_prev
= NULL
;
4672 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
4674 num_screens
= get_screen_count();
4675 for (i
= 0; i
< num_screens
; i
++) {
4679 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
4680 /* Adjust stack area for region bar and padding. */
4682 g
.x
+= region_padding
;
4683 g
.y
+= region_padding
;
4684 g
.w
-= 2 * border_width
+ 2 * region_padding
;
4685 g
.h
-= 2 * border_width
+ 2 * region_padding
;
4686 if (bar_enabled
&& r
->ws
->bar_enabled
) {
4692 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d (screen: "
4693 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4694 r
->ws
->idx
, i
, j
++, g
.x
, g
.y
, g
.w
, g
.h
);
4697 /* Stack bar/input relative to prev. region. */
4698 val
[1] = XCB_STACK_MODE_ABOVE
;
4700 val
[0] = r_prev
->id
;
4701 DNPRINTF(SWM_D_STACK
, "stack: region input %#x "
4702 "relative to %#x.\n", r
->id
, val
[0]);
4703 xcb_configure_window(conn
, r
->id
,
4704 XCB_CONFIG_WINDOW_SIBLING
|
4705 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4707 val
[0] = r_prev
->bar
->id
;
4708 DNPRINTF(SWM_D_STACK
, "stack: region bar %#x "
4709 "relative to %#x.\n", r
->bar
->id
, val
[0]);
4710 xcb_configure_window(conn
, r
->bar
->id
,
4711 XCB_CONFIG_WINDOW_SIBLING
|
4712 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4715 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
4716 r
->ws
->cur_layout
->l_string(r
->ws
);
4717 /* save r so we can track region changes */
4725 DNPRINTF(SWM_D_STACK
, "stack: end\n");
4729 store_float_geom(struct ws_win
*win
)
4731 if (win
== NULL
|| win
->ws
->r
== NULL
)
4734 /* retain window geom and region geom */
4735 win
->g_float
= win
->g
;
4736 win
->g_float
.x
-= X(win
->ws
->r
);
4737 win
->g_float
.y
-= Y(win
->ws
->r
);
4738 win
->g_floatvalid
= true;
4739 DNPRINTF(SWM_D_MISC
, "store_float_geom: win %#x, g: (%d,%d)"
4740 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
4741 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
4742 win
->g_float
.w
, win
->g_float
.h
);
4746 load_float_geom(struct ws_win
*win
)
4748 if (win
== NULL
|| win
->ws
->r
== NULL
)
4751 if (win
->g_floatvalid
) {
4752 win
->g
= win
->g_float
;
4753 X(win
) += X(win
->ws
->r
);
4754 Y(win
) += Y(win
->ws
->r
);
4755 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g: (%d,%d)"
4756 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
4759 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g_float "
4760 "is not set.\n", win
->id
);
4765 update_floater(struct ws_win
*win
)
4767 struct workspace
*ws
;
4768 struct swm_region
*r
;
4770 DNPRINTF(SWM_D_MISC
, "update_floater: win %#x\n", WINID(win
));
4777 if ((r
= ws
->r
) == NULL
)
4780 win
->bordered
= true;
4782 if (FULLSCREEN(win
)) {
4783 /* _NET_WM_FULLSCREEN: fullscreen without border. */
4784 if (!win
->g_floatvalid
)
4785 store_float_geom(win
);
4788 win
->bordered
= false;
4789 } else if (MAXIMIZED(win
)) {
4790 /* Maximize: like a single stacked window. */
4791 if (!win
->g_floatvalid
)
4792 store_float_geom(win
);
4796 if (bar_enabled
&& ws
->bar_enabled
&& !maximize_hide_bar
) {
4798 Y(win
) += bar_height
;
4799 HEIGHT(win
) -= bar_height
;
4800 } else if (disable_border
) {
4801 win
->bordered
= false;
4804 if (win
->bordered
) {
4805 HEIGHT(win
) -= 2 * border_width
;
4806 WIDTH(win
) -= 2 * border_width
;
4809 /* Normal floating window. */
4810 /* Update geometry if new region. */
4812 load_float_geom(win
);
4814 if (((win
->quirks
& SWM_Q_FULLSCREEN
) &&
4815 WIDTH(win
) >= WIDTH(r
) && HEIGHT(win
) >= HEIGHT(r
)) ||
4816 ((!WS_FOCUSED(win
->ws
) || win
->ws
->focus
!= win
) &&
4817 (win
->quirks
& SWM_Q_MINIMALBORDER
))) {
4819 win
->bordered
= false;
4820 } else if (!MANUAL(win
)) {
4821 if (TRANS(win
) && (win
->quirks
& SWM_Q_TRANSSZ
)) {
4822 /* Adjust size on TRANSSZ quirk. */
4823 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
4824 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
4827 if (!(win
->quirks
& SWM_Q_ANYWHERE
)) {
4829 * Floaters and transients are auto-centred
4830 * unless manually moved, resized or ANYWHERE
4833 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 -
4835 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 -
4837 store_float_geom(win
);
4842 /* Ensure at least 1 pixel of the window is in the region. */
4843 region_containment(win
, r
, SWM_CW_ALLSIDES
);
4849 * Send keystrokes to terminal to decrease/increase the font size as the
4850 * window size changes.
4853 adjust_font(struct ws_win
*win
)
4855 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
4856 ABOVE(win
) || TRANS(win
))
4859 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
4860 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
4861 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
4862 win
->font_size_boundary
[win
->font_steps
] =
4863 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
4866 win
->last_inc
= win
->sh
.width_inc
;
4867 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
4868 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
4869 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
4872 win
->last_inc
= win
->sh
.width_inc
;
4873 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
4877 #define SWAPXY(g) do { \
4879 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4880 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4883 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, bool flip
)
4885 struct swm_geometry win_g
, r_g
= *g
;
4887 int i
= 0, j
= 0, s
= 0, stacks
= 0;
4888 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
4889 int hrh
= 0, extra
= 0, h_slice
= 0, last_h
= 0;
4890 int split
= 0, colno
= 0;
4891 int winno
, mwin
= 0, msize
= 0;
4892 int remain
, missing
, v_slice
, mscale
;
4893 bool bordered
= true, reconfigure
= false;
4895 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
4896 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
4898 /* Prepare tiling variables, if needed. */
4899 if ((winno
= count_win(ws
, false)) > 0) {
4900 /* Find first tiled window. */
4901 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
4902 if (!FLOATING(win
) && !ICONIC(win
))
4905 /* Take into account size hints of first tiled window. */
4907 w_inc
= win
->sh
.width_inc
;
4908 w_base
= win
->sh
.base_width
;
4909 mwin
= ws
->l_state
.horizontal_mwin
;
4910 mscale
= ws
->l_state
.horizontal_msize
;
4911 stacks
= ws
->l_state
.horizontal_stacks
;
4914 w_inc
= win
->sh
.height_inc
;
4915 w_base
= win
->sh
.base_height
;
4916 mwin
= ws
->l_state
.vertical_mwin
;
4917 mscale
= ws
->l_state
.vertical_msize
;
4918 stacks
= ws
->l_state
.vertical_stacks
;
4922 if (stacks
> winno
- mwin
)
4923 stacks
= winno
- mwin
;
4927 h_slice
= r_g
.h
/ SWM_H_SLICE
;
4928 if (mwin
&& winno
> mwin
) {
4929 v_slice
= r_g
.w
/ SWM_V_SLICE
;
4933 win_g
.w
= v_slice
* mscale
;
4935 if (w_inc
> 1 && w_inc
< v_slice
) {
4936 /* Adjust for requested size increment. */
4937 remain
= (win_g
.w
- w_base
) % w_inc
;
4943 win_g
.x
+= r_g
.w
- msize
;
4946 colno
= split
= winno
/ stacks
;
4947 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
4948 2 * border_width
) / stacks
);
4950 hrh
= r_g
.h
/ colno
;
4951 extra
= r_g
.h
- (colno
* hrh
);
4952 win_g
.h
= hrh
- 2 * border_width
;
4953 i
= j
= 0, s
= stacks
;
4956 /* Update window geometry. */
4957 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
4961 if (FLOATING(win
)) {
4962 update_floater(win
);
4967 if (split
&& i
== split
) {
4968 colno
= (winno
- mwin
) / stacks
;
4969 if (s
<= (winno
- mwin
) % stacks
)
4972 hrh
= r_g
.h
/ colno
;
4973 extra
= r_g
.h
- (colno
* hrh
);
4976 win_g
.x
+= win_g
.w
+ 2 * border_width
+
4979 win_g
.w
= (r_g
.w
- msize
-
4980 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
4982 win_g
.w
+= (r_g
.w
- msize
-
4983 (stacks
* (2 * border_width
+ tile_gap
))) %
4987 win_g
.x
-= win_g
.w
+ 2 * border_width
+
4993 win_g
.h
= hrh
- 2 * border_width
- tile_gap
;
4996 h_inc
= win
->sh
.width_inc
;
4997 h_base
= win
->sh
.base_width
;
4999 h_inc
= win
->sh
.height_inc
;
5000 h_base
= win
->sh
.base_height
;
5003 if (j
== colno
- 1) {
5004 win_g
.h
= hrh
+ extra
;
5005 } else if (h_inc
> 1 && h_inc
< h_slice
) {
5006 /* adjust for window's requested size increment */
5007 remain
= (win_g
.h
- h_base
) % h_inc
;
5008 missing
= h_inc
- remain
;
5010 if (missing
<= extra
|| j
== 0) {
5022 win_g
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
5024 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
) &&
5027 win_g
.w
+= 2 * border_width
;
5028 win_g
.h
+= 2 * border_width
;
5034 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
5035 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
5039 WIDTH(win
) = win_g
.h
;
5040 HEIGHT(win
) = win_g
.w
;
5043 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
5044 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
5048 WIDTH(win
) = win_g
.w
;
5049 HEIGHT(win
) = win_g
.h
;
5053 if (bordered
!= win
->bordered
) {
5055 win
->bordered
= bordered
;
5068 /* Stack all windows from bottom up. */
5069 TAILQ_FOREACH_REVERSE(win
, &ws
->stack
, ws_win_stack
, stack_entry
)
5071 update_win_stacking(win
);
5073 /* Map all windows from top down. */
5074 TAILQ_FOREACH(win
, &ws
->stack
, stack_entry
)
5078 DNPRINTF(SWM_D_STACK
, "stack_master: done\n");
5082 vertical_config(struct workspace
*ws
, int id
)
5084 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
5088 case SWM_ARG_ID_STACKRESET
:
5089 case SWM_ARG_ID_STACKINIT
:
5090 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
5091 ws
->l_state
.vertical_mwin
= 1;
5092 ws
->l_state
.vertical_stacks
= 1;
5094 case SWM_ARG_ID_MASTERSHRINK
:
5095 if (ws
->l_state
.vertical_msize
> 1)
5096 ws
->l_state
.vertical_msize
--;
5098 case SWM_ARG_ID_MASTERGROW
:
5099 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
5100 ws
->l_state
.vertical_msize
++;
5102 case SWM_ARG_ID_MASTERADD
:
5103 ws
->l_state
.vertical_mwin
++;
5105 case SWM_ARG_ID_MASTERDEL
:
5106 if (ws
->l_state
.vertical_mwin
> 0)
5107 ws
->l_state
.vertical_mwin
--;
5109 case SWM_ARG_ID_STACKBALANCE
:
5110 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ (ws
->l_state
.vertical_stacks
+ 1);
5112 case SWM_ARG_ID_STACKINC
:
5113 ws
->l_state
.vertical_stacks
++;
5115 case SWM_ARG_ID_STACKDEC
:
5116 if (ws
->l_state
.vertical_stacks
> 1)
5117 ws
->l_state
.vertical_stacks
--;
5119 case SWM_ARG_ID_FLIPLAYOUT
:
5120 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
5128 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5130 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
5132 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
5136 horizontal_config(struct workspace
*ws
, int id
)
5138 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
5141 case SWM_ARG_ID_STACKRESET
:
5142 case SWM_ARG_ID_STACKINIT
:
5143 ws
->l_state
.horizontal_mwin
= 1;
5144 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
5145 ws
->l_state
.horizontal_stacks
= 1;
5147 case SWM_ARG_ID_MASTERSHRINK
:
5148 if (ws
->l_state
.horizontal_msize
> 1)
5149 ws
->l_state
.horizontal_msize
--;
5151 case SWM_ARG_ID_MASTERGROW
:
5152 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
5153 ws
->l_state
.horizontal_msize
++;
5155 case SWM_ARG_ID_MASTERADD
:
5156 ws
->l_state
.horizontal_mwin
++;
5158 case SWM_ARG_ID_MASTERDEL
:
5159 if (ws
->l_state
.horizontal_mwin
> 0)
5160 ws
->l_state
.horizontal_mwin
--;
5162 case SWM_ARG_ID_STACKBALANCE
:
5163 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ (ws
->l_state
.horizontal_stacks
+ 1);
5165 case SWM_ARG_ID_STACKINC
:
5166 ws
->l_state
.horizontal_stacks
++;
5168 case SWM_ARG_ID_STACKDEC
:
5169 if (ws
->l_state
.horizontal_stacks
> 1)
5170 ws
->l_state
.horizontal_stacks
--;
5172 case SWM_ARG_ID_FLIPLAYOUT
:
5173 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
5181 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5183 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
5185 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
5188 /* fullscreen view */
5190 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5192 struct swm_geometry gg
= *g
;
5193 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
, *tmpw
;
5196 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
5201 winno
= count_win(ws
, false);
5202 if (winno
== 0 && count_win(ws
, true) == 0)
5205 /* Figure out which top level window should be visible. */
5206 if (ws
->focus_pending
)
5207 win
= ws
->focus_pending
;
5210 else if (ws
->focus_prev
)
5211 win
= ws
->focus_prev
;
5213 win
= TAILQ_FIRST(&ws
->winlist
);
5215 DNPRINTF(SWM_D_STACK
, "max_stack: focus_pending: %#x, focus: %#x, "
5216 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws
->focus_pending
),
5217 WINID(ws
->focus
), WINID(ws
->focus_prev
),
5218 WINID(TAILQ_FIRST(&ws
->winlist
)), win
->id
);
5220 /* Update window geometry. */
5221 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5230 /* Set maximized flag for all maxed windows. */
5231 if (!MAXIMIZED(w
)) {
5232 /* Preserve floating geometry. */
5234 store_float_geom(w
);
5236 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_MAXIMIZED
);
5237 ewmh_update_wm_state(w
);
5240 /* Only reconfigure if necessary. */
5241 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
5242 HEIGHT(w
) != gg
.h
) {
5245 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
)) {
5246 w
->bordered
= false;
5247 WIDTH(w
) += 2 * border_width
;
5248 HEIGHT(w
) += 2 * border_width
;
5257 /* If transient, stack parent and its children. */
5258 if (TRANS(win
) && (parent
= find_window(win
->transient
))) {
5259 raise_window(parent
);
5261 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5262 if (w
->transient
== parent
->id
)
5266 /* Make sure focus window is on top. */
5269 /* Stack any children of focus window. */
5270 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5271 if (w
->transient
== win
->id
)
5274 /* Map all windows. */
5275 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
5281 send_to_rg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5283 int ridx
= args
->id
, i
, num_screens
;
5284 struct swm_region
*rr
= NULL
;
5287 num_screens
= get_screen_count();
5288 /* do nothing if we don't have more than one screen */
5289 if (!(num_screens
> 1 || outputs
> 1))
5292 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
5294 rr
= TAILQ_FIRST(&r
->s
->rl
);
5295 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
5296 rr
= TAILQ_NEXT(rr
, entry
);
5303 send_to_ws(bp
, r
, &a
);
5307 region_under(struct swm_screen
*s
, int x
, int y
)
5309 struct swm_region
*r
= NULL
;
5311 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
5312 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
5313 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
5314 WIDTH(r
), HEIGHT(r
), x
, y
);
5315 if (X(r
) <= x
&& x
< MAX_X(r
))
5316 if (Y(r
) <= y
&& y
< MAX_Y(r
))
5323 /* Transfer focused window to target workspace and focus. */
5325 send_to_ws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5327 int wsid
= args
->id
;
5328 struct ws_win
*win
= NULL
;
5332 if (r
&& r
->ws
&& r
->ws
->focus
)
5337 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win %#x, ws %d\n", win
->id
, wsid
);
5339 if (wsid
< 0 || wsid
>= workspace_limit
)
5342 if (win
->ws
->idx
== wsid
)
5345 win_to_ws(win
, wsid
, true);
5347 /* Set new focus on target ws. */
5348 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5349 win
->ws
->focus_prev
= win
->ws
->focus
;
5350 win
->ws
->focus
= win
;
5351 win
->ws
->focus_pending
= NULL
;
5353 if (win
->ws
->focus_prev
)
5354 update_window_color(win
->ws
->focus_prev
);
5357 DNPRINTF(SWM_D_STACK
, "send_to_ws: focus_pending: %#x, focus: %#x, "
5358 "focus_prev: %#x, first: %#x, win: %#x\n",
5359 WINID(r
->ws
->focus_pending
), WINID(r
->ws
->focus
),
5360 WINID(r
->ws
->focus_prev
), WINID(TAILQ_FIRST(&r
->ws
->winlist
)),
5363 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5364 ewmh_update_wm_state(win
);
5366 /* Restack and set new focus on current ws. */
5368 load_float_geom(win
);
5372 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5373 if (r
->ws
->focus
!= NULL
) {
5374 focus_win(r
->ws
->focus
);
5376 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
5387 win_to_ws(struct ws_win
*win
, int wsid
, bool unfocus
)
5389 struct ws_win
*parent
;
5390 struct workspace
*ws
, *nws
, *pws
;
5392 if (wsid
< 0 || wsid
>= workspace_limit
)
5395 if (win
->ws
->idx
== wsid
)
5399 nws
= &win
->s
->ws
[wsid
];
5401 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win %#x, ws %d -> %d\n", win
->id
,
5404 /* Cleanup focus on source ws. */
5405 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
5406 (ws
->focus
== win
|| ws
->focus_pending
== win
))
5407 ws
->focus_pending
= get_focus_prev(win
);
5409 /* Move the parent if this is a transient window. */
5411 parent
= find_window(win
->transient
);
5414 /* Set new focus in parent's ws if needed. */
5415 if (pws
->focus
== parent
) {
5416 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5417 pws
->focus_pending
=
5418 get_focus_prev(parent
);
5420 unfocus_win(parent
);
5422 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5423 pws
->focus
= pws
->focus_pending
;
5424 pws
->focus_pending
= NULL
;
5428 /* Don't unmap parent if new ws is visible */
5430 unmap_window(parent
);
5433 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
5434 TAILQ_REMOVE(&ws
->stack
, parent
, stack_entry
);
5435 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
5436 TAILQ_INSERT_TAIL(&nws
->stack
, parent
, stack_entry
);
5439 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5440 "_NET_WM_DESKTOP: %d\n", wsid
);
5441 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5442 parent
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
5443 XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5450 if (ws
->focus_prev
== win
)
5451 ws
->focus_prev
= NULL
;
5453 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& ws
->focus_pending
!= NULL
) {
5454 ws
->focus
= ws
->focus_pending
;
5455 ws
->focus_pending
= NULL
;
5458 /* Don't unmap if new ws is visible */
5463 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
5464 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
5465 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
5466 TAILQ_INSERT_TAIL(&nws
->stack
, win
, stack_entry
);
5469 /* Update the window's workspace property: _NET_WM_DESKTOP */
5470 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5471 "_NET_WM_DESKTOP: %d\n", wsid
);
5472 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
5473 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5475 ewmh_update_client_list();
5477 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
5481 pressbutton(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5483 /* suppress unused warning since var is needed */
5487 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
5488 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5489 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
5490 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5494 raise_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5496 /* Suppress warning. */
5500 if (r
== NULL
|| r
->ws
== NULL
)
5503 if (r
->ws
->focus
&& MAXIMIZED(r
->ws
->focus
))
5506 r
->ws
->always_raise
= !r
->ws
->always_raise
;
5508 /* Update focused win stacking order based on new always_raise value. */
5509 raise_window(r
->ws
->focus
);
5515 iconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5519 /* Suppress warning. */
5523 if ((w
= r
->ws
->focus
) == NULL
)
5526 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_HIDDEN
);
5527 ewmh_update_wm_state(w
);
5535 get_win_name(xcb_window_t win
)
5538 xcb_get_property_cookie_t c
;
5539 xcb_get_property_reply_t
*r
;
5541 /* First try _NET_WM_NAME for UTF-8. */
5542 c
= xcb_get_property(conn
, 0, win
, ewmh
[_NET_WM_NAME
].atom
,
5543 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5544 r
= xcb_get_property_reply(conn
, c
, NULL
);
5545 if (r
&& r
->type
== XCB_NONE
) {
5547 /* Use WM_NAME instead; no UTF-8. */
5548 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
5549 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5550 r
= xcb_get_property_reply(conn
, c
, NULL
);
5553 if (r
&& r
->type
!= XCB_NONE
&& r
->length
> 0)
5554 name
= strndup(xcb_get_property_value(r
),
5555 xcb_get_property_value_length(r
));
5560 err(1, "get_win_name: failed to allocate memory.");
5568 uniconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5577 DNPRINTF(SWM_D_MISC
, "uniconify\n");
5579 if (r
== NULL
|| r
->ws
== NULL
)
5582 /* make sure we have anything to uniconify */
5583 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5584 if (win
->ws
== NULL
)
5585 continue; /* should never happen */
5591 DNPRINTF(SWM_D_MISC
, "uniconify: count: %d\n", count
);
5597 search_resp_action
= SWM_SEARCH_UNICONIFY
;
5599 spawn_select(r
, args
, "search", &searchpid
);
5601 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5604 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5605 if (win
->ws
== NULL
)
5606 continue; /* should never happen */
5610 name
= get_win_name(win
->id
);
5611 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
5619 name_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5625 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
5631 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
5633 spawn_select(r
, args
, "name_workspace", &searchpid
);
5635 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5638 fprintf(lfile
, "%s", "");
5643 search_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5646 struct workspace
*ws
;
5651 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
5657 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
5659 spawn_select(r
, args
, "search", &searchpid
);
5661 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5664 for (i
= 0; i
< workspace_limit
; i
++) {
5668 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
5669 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
5676 search_win_cleanup(void)
5678 struct search_window
*sw
= NULL
;
5680 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
5681 xcb_destroy_window(conn
, sw
->indicator
);
5682 TAILQ_REMOVE(&search_wl
, sw
, entry
);
5688 search_win(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5690 struct ws_win
*win
= NULL
;
5691 struct search_window
*sw
= NULL
;
5694 xcb_screen_t
*screen
;
5695 int i
, width
, height
;
5703 XRectangle l_ibox
, l_lbox
;
5707 DNPRINTF(SWM_D_MISC
, "search_win\n");
5710 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
5712 spawn_select(r
, args
, "search", &searchpid
);
5714 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5717 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
5718 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
5720 TAILQ_INIT(&search_wl
);
5723 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5727 sw
= calloc(1, sizeof(struct search_window
));
5729 warn("search_win: calloc");
5731 search_win_cleanup();
5737 snprintf(s
, sizeof s
, "%d", i
);
5740 w
= xcb_generate_id(conn
);
5741 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
5742 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
5743 wa
[2] = screen
->default_colormap
;
5745 if (bar_font_legacy
) {
5746 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
5747 width
= l_lbox
.width
+ 4;
5748 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
5750 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
5752 width
= info
.width
+ 4;
5753 height
= bar_font
->height
+ 4;
5756 xcb_create_window(conn
, screen
->root_depth
, w
, win
->id
, 0, 0,
5757 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
5758 screen
->root_visual
, XCB_CW_BACK_PIXEL
|
5759 XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
, wa
);
5761 xcb_map_window(conn
, w
);
5764 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
5766 if (bar_font_legacy
) {
5767 l_gcv
.graphics_exposures
= 0;
5768 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
5770 XSetForeground(display
, l_draw
,
5771 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
5773 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
5774 (bar_fs_extents
->max_logical_extent
.height
-
5775 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
5777 XFreeGC(display
, l_draw
);
5780 draw
= XftDrawCreate(display
, w
,
5781 DefaultVisual(display
, r
->s
->idx
),
5782 DefaultColormap(display
, r
->s
->idx
));
5784 XftDrawStringUtf8(draw
, &search_font_color
, bar_font
, 2,
5785 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
5786 bar_font
->descent
, (FcChar8
*)s
, len
);
5788 XftDrawDestroy(draw
);
5791 DNPRINTF(SWM_D_MISC
, "search_win: mapped win %#x\n", w
);
5793 fprintf(lfile
, "%d\n", i
);
5803 search_resp_uniconify(const char *resp
, size_t len
)
5809 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
5811 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
5814 name
= get_win_name(win
->id
);
5815 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
5820 if (strncmp(s
, resp
, len
) == 0) {
5821 /* XXX this should be a callback to generalize */
5822 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_HIDDEN
);
5823 ewmh_update_wm_state(win
);
5833 search_resp_name_workspace(const char *resp
, size_t len
)
5835 struct workspace
*ws
;
5837 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
5839 if (search_r
->ws
== NULL
)
5844 free(search_r
->ws
->name
);
5845 search_r
->ws
->name
= NULL
;
5849 ws
->name
= strdup(resp
);
5850 if (ws
->name
== NULL
) {
5851 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
5852 "strdup: %s", strerror(errno
));
5856 ewmh_update_desktop_names();
5857 ewmh_get_desktop_names();
5862 ewmh_update_desktop_names(void)
5864 char *name_list
= NULL
, *p
;
5865 int num_screens
, i
, j
, len
= 0, tot
= 0;
5867 num_screens
= get_screen_count();
5868 for (i
= 0; i
< num_screens
; ++i
) {
5869 for (j
= 0; j
< workspace_limit
; ++j
) {
5870 if (screens
[i
].ws
[j
].name
!= NULL
)
5871 len
+= strlen(screens
[i
].ws
[j
].name
);
5875 if((name_list
= calloc(len
, sizeof(char))) == NULL
)
5876 err(1, "update_desktop_names: calloc: failed to "
5877 "allocate memory.");
5880 for (j
= 0; j
< workspace_limit
; ++j
) {
5881 if (screens
[i
].ws
[j
].name
!= NULL
) {
5882 len
= strlen(screens
[i
].ws
[j
].name
);
5883 memcpy(p
, screens
[i
].ws
[j
].name
, len
);
5892 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5893 screens
[i
].root
, ewmh
[_NET_DESKTOP_NAMES
].atom
,
5894 a_utf8_string
, 8, tot
, name_list
);
5904 ewmh_get_desktop_names(void)
5907 xcb_get_property_cookie_t c
;
5908 xcb_get_property_reply_t
*r
;
5909 int num_screens
, i
, j
, n
, k
;
5911 num_screens
= get_screen_count();
5912 for (i
= 0; i
< num_screens
; ++i
) {
5913 for (j
= 0; j
< workspace_limit
; ++j
) {
5914 free(screens
[i
].ws
[j
].name
);
5915 screens
[i
].ws
[j
].name
= NULL
;
5918 c
= xcb_get_property(conn
, 0, screens
[i
].root
,
5919 ewmh
[_NET_DESKTOP_NAMES
].atom
,
5920 a_utf8_string
, 0, UINT32_MAX
);
5921 r
= xcb_get_property_reply(conn
, c
, NULL
);
5925 names
= xcb_get_property_value(r
);
5926 n
= xcb_get_property_value_length(r
);
5928 for (j
= 0, k
= 0; j
< n
; ++j
) {
5929 if (*(names
+ j
) != '\0') {
5930 screens
[i
].ws
[k
].name
= strdup(names
+ j
);
5931 j
+= strlen(names
+ j
);
5940 ewmh_update_client_list(void)
5943 int num_screens
, i
, j
, k
= 0, count
= 0;
5946 num_screens
= get_screen_count();
5947 for (i
= 0; i
< num_screens
; ++i
) {
5948 for (j
= 0; j
< workspace_limit
; ++j
)
5949 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
5952 DNPRINTF(SWM_D_PROP
, "ewmh_update_client_list: win count: %d\n",
5958 wins
= calloc(count
, sizeof(xcb_window_t
));
5960 err(1, "ewmh_update_client_list: calloc: failed to "
5961 "allocate memory.");
5963 for (j
= 0, k
= 0; j
< workspace_limit
; ++j
)
5964 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
5965 wins
[k
++] = win
->id
;
5967 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5968 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
5969 XCB_ATOM_WINDOW
, 32, count
, wins
);
5976 ewmh_update_current_desktop(void)
5980 num_screens
= get_screen_count();
5981 for (i
= 0; i
< num_screens
; ++i
) {
5982 if (screens
[i
].r_focus
)
5983 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5984 screens
[i
].root
, ewmh
[_NET_CURRENT_DESKTOP
].atom
,
5985 XCB_ATOM_CARDINAL
, 32, 1,
5986 &screens
[i
].r_focus
->ws
->idx
);
5991 ewmh_update_desktops(void)
5993 int num_screens
, i
, j
;
5996 vals
= calloc(workspace_limit
* 2, sizeof(uint32_t));
5998 err(1, "ewmh_update_desktops: calloc: failed to allocate "
6001 num_screens
= get_screen_count();
6002 for (i
= 0; i
< num_screens
; i
++) {
6003 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6004 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
6005 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
6007 for (j
= 0; j
< workspace_limit
; ++j
) {
6008 if (screens
[i
].ws
[j
].r
!= NULL
) {
6009 vals
[j
* 2] = X(screens
[i
].ws
[j
].r
);
6010 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].r
);
6011 } else if (screens
[i
].ws
[j
].old_r
!= NULL
) {
6012 vals
[j
* 2] = X(screens
[i
].ws
[j
].old_r
);
6013 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].old_r
);
6015 vals
[j
* 2] = vals
[j
* 2 + 1] = 0;
6019 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6020 screens
[i
].root
, ewmh
[_NET_DESKTOP_VIEWPORT
].atom
,
6021 XCB_ATOM_CARDINAL
, 32, workspace_limit
* 2, vals
);
6028 search_resp_search_workspace(const char *resp
)
6035 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
6039 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
6046 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
6048 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
6055 switchws(NULL
, search_r
, &a
);
6059 search_resp_search_window(const char *resp
)
6064 struct search_window
*sw
;
6066 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
6070 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
6075 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
6077 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
6084 TAILQ_FOREACH(sw
, &search_wl
, entry
)
6085 if (idx
== sw
->idx
) {
6091 #define MAX_RESP_LEN 1024
6094 search_do_resp(void)
6100 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
6105 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
6106 warn("search: calloc");
6110 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
6112 warn("search: read error");
6115 resp
[rbytes
] = '\0';
6118 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
6119 * newline, so work around that by sanitizing the resp now.
6121 resp
[strcspn(resp
, "\n")] = '\0';
6124 switch (search_resp_action
) {
6125 case SWM_SEARCH_UNICONIFY
:
6126 search_resp_uniconify(resp
, len
);
6128 case SWM_SEARCH_NAME_WORKSPACE
:
6129 search_resp_name_workspace(resp
, len
);
6131 case SWM_SEARCH_SEARCH_WORKSPACE
:
6132 search_resp_search_workspace(resp
);
6134 case SWM_SEARCH_SEARCH_WINDOW
:
6135 search_resp_search_window(resp
);
6140 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
6141 search_win_cleanup();
6143 search_resp_action
= SWM_SEARCH_NONE
;
6144 close(select_resp_pipe
[0]);
6151 wkill(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6155 DNPRINTF(SWM_D_MISC
, "wkill: win %#x, id: %d\n", WINID(r
->ws
->focus
),
6158 if (r
->ws
->focus
== NULL
)
6161 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
6162 xcb_kill_client(conn
, r
->ws
->focus
->id
);
6164 if (r
->ws
->focus
->can_delete
)
6165 client_msg(r
->ws
->focus
, a_delete
, 0);
6171 clear_maximized(struct workspace
*ws
)
6176 /* Clear any maximized win(s) on ws, from bottom up. */
6177 TAILQ_FOREACH_REVERSE(w
, &ws
->stack
, ws_win_stack
, stack_entry
)
6179 ewmh_apply_flags(w
, w
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
6180 ewmh_update_wm_state(w
);
6188 maximize_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6190 struct ws_win
*w
= r
->ws
->focus
;
6192 /* suppress unused warning since var is needed */
6199 DNPRINTF(SWM_D_MISC
, "maximize_toggle: win %#x\n", w
->id
);
6204 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6207 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_MAXIMIZED
);
6208 ewmh_update_wm_state(w
);
6212 if (w
== w
->ws
->focus
)
6217 DNPRINTF(SWM_D_MISC
, "maximize_toggle: done\n");
6221 floating_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6223 struct ws_win
*w
= r
->ws
->focus
;
6225 /* suppress unused warning since var is needed */
6232 DNPRINTF(SWM_D_MISC
, "floating_toggle: win %#x\n", w
->id
);
6234 if (FULLSCREEN(w
) || TRANS(w
))
6237 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6240 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_ABOVE
);
6241 ewmh_update_wm_state(w
);
6245 if (w
== w
->ws
->focus
)
6250 DNPRINTF(SWM_D_MISC
, "floating_toggle: done\n");
6254 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
6256 struct swm_geometry g
= r
->g
;
6257 int rt
, lt
, tp
, bm
, bw
;
6259 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
6262 * Perpendicular distance of each side of the window to the respective
6263 * side of the region boundary. Positive values indicate the side of
6264 * the window has passed beyond the region boundary.
6266 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) + BORDER(win
) - MAX_X(r
) : bw
;
6267 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) + BORDER(win
) : bw
;
6268 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) + BORDER(win
) - MAX_Y(r
) : bw
;
6269 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) + BORDER(win
) : bw
;
6271 DNPRINTF(SWM_D_MISC
, "region_containment: win %#x, rt: %d, lt: %d, "
6272 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
6273 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
6274 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
6277 * Disable containment if any of the flagged sides went beyond the
6278 * containment boundary, or if containment is disabled.
6280 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
6281 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
6282 /* Make sure window has at least 1 pixel in the region */
6283 g
.x
+= 1 - WIDTH(win
);
6284 g
.y
+= 1 - HEIGHT(win
);
6285 g
.w
+= 2 * WIDTH(win
) - 2;
6286 g
.h
+= 2 * HEIGHT(win
) - 2;
6289 constrain_window(win
, &g
, &opts
);
6292 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6294 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
6296 DNPRINTF(SWM_D_MISC
, "constrain_window: win %#x, (x,y) w x h: "
6297 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6298 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
6299 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
6300 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
6301 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
6302 YESNO(*opts
& SWM_CW_RESIZABLE
));
6304 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) + BORDER(win
) > b
->x
+ b
->w
) {
6305 if (*opts
& SWM_CW_RESIZABLE
)
6306 WIDTH(win
) = b
->x
+ b
->w
- X(win
) - BORDER(win
);
6308 X(win
) = b
->x
+ b
->w
- WIDTH(win
) - BORDER(win
);
6311 if ((*opts
& SWM_CW_LEFT
) && X(win
) + BORDER(win
) < b
->x
) {
6312 if (*opts
& SWM_CW_RESIZABLE
)
6313 WIDTH(win
) -= b
->x
- X(win
) - BORDER(win
);
6315 X(win
) = b
->x
- BORDER(win
);
6318 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) + BORDER(win
) > b
->y
+ b
->h
) {
6319 if (*opts
& SWM_CW_RESIZABLE
)
6320 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
) - BORDER(win
);
6322 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
) - BORDER(win
);
6325 if ((*opts
& SWM_CW_TOP
) && Y(win
) + BORDER(win
) < b
->y
) {
6326 if (*opts
& SWM_CW_RESIZABLE
)
6327 HEIGHT(win
) -= b
->y
- Y(win
) - BORDER(win
);
6329 Y(win
) = b
->y
- BORDER(win
);
6332 if (*opts
& SWM_CW_RESIZABLE
) {
6335 if (HEIGHT(win
) < 1)
6341 update_window_color(struct ws_win
*win
)
6345 if (WS_FOCUSED(win
->ws
) && win
->ws
->focus
== win
)
6346 pixel
= MAXIMIZED(win
) ?
6347 &win
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].pixel
:
6348 &win
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6350 pixel
= MAXIMIZED(win
) ?
6351 &win
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].pixel
:
6352 &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6354 xcb_change_window_attributes(conn
, win
->id
,
6355 XCB_CW_BORDER_PIXEL
, pixel
);
6359 update_window(struct ws_win
*win
)
6364 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
6365 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
6366 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6370 wc
[3] = HEIGHT(win
);
6371 wc
[4] = BORDER(win
);
6373 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x, (x,y) w x h: "
6374 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
6375 wc
[3], YESNO(win
->bordered
));
6377 xcb_configure_window(conn
, win
->id
, mask
, wc
);
6381 SIMPLEQ_ENTRY(event
) entry
;
6382 xcb_generic_event_t
*ev
;
6384 SIMPLEQ_HEAD(event_queue
, event
) events
= SIMPLEQ_HEAD_INITIALIZER(events
);
6386 xcb_generic_event_t
*
6387 get_next_event(bool wait
)
6390 xcb_generic_event_t
*evt
;
6392 /* Try queue first. */
6393 if ((ep
= SIMPLEQ_FIRST(&events
))) {
6395 SIMPLEQ_REMOVE_HEAD(&events
, entry
);
6398 evt
= xcb_wait_for_event(conn
);
6400 evt
= xcb_poll_for_event(conn
);
6406 put_back_event(xcb_generic_event_t
*evt
)
6409 if ((ep
= malloc(sizeof (struct event
))) == NULL
)
6410 err(1, "put_back_event: failed to allocate memory.");
6412 SIMPLEQ_INSERT_HEAD(&events
, ep
, entry
);
6415 /* Peeks at next event to detect auto-repeat. */
6417 keyrepeating(xcb_key_release_event_t
*kre
)
6419 xcb_generic_event_t
*evt
;
6421 /* Ensure repeating keypress is finished processing. */
6424 if ((evt
= get_next_event(false))) {
6425 put_back_event(evt
);
6427 if (XCB_EVENT_RESPONSE_TYPE(evt
) == XCB_KEY_PRESS
&&
6428 kre
->sequence
== evt
->sequence
&&
6429 kre
->detail
== ((xcb_key_press_event_t
*)evt
)->detail
)
6437 keybindreleased(struct binding
*bp
, xcb_key_release_event_t
*kre
)
6439 if (bp
->type
== KEYBIND
&& !keyrepeating(kre
) &&
6440 bp
->value
== xcb_key_press_lookup_keysym(syms
, kre
, 0))
6446 #define SWM_RESIZE_STEPS (50)
6449 resize_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
6451 xcb_timestamp_t timestamp
= 0;
6452 struct swm_region
*r
= NULL
;
6453 struct swm_geometry g
;
6454 int top
= 0, left
= 0;
6456 xcb_cursor_t cursor
;
6457 xcb_query_pointer_reply_t
*xpr
= NULL
;
6458 xcb_generic_event_t
*evt
;
6459 xcb_motion_notify_event_t
*mne
;
6460 bool resizing
, step
= false;
6466 if (FULLSCREEN(win
))
6469 /* In max_stack mode, should only resize transients. */
6470 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6473 DNPRINTF(SWM_D_EVENT
, "resize: win %#x, floating: %s, "
6474 "transient: %#x\n", win
->id
, YESNO(ABOVE(win
)),
6478 store_float_geom(win
);
6479 else if (!(TRANS(win
) || ABOVE(win
)))
6482 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6484 ewmh_update_wm_state(win
);
6490 /* It's possible for win to have been freed during focus_flush(). */
6491 if (validate_win(win
)) {
6492 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6497 case SWM_ARG_ID_WIDTHSHRINK
:
6498 WIDTH(win
) -= SWM_RESIZE_STEPS
;
6501 case SWM_ARG_ID_WIDTHGROW
:
6502 WIDTH(win
) += SWM_RESIZE_STEPS
;
6505 case SWM_ARG_ID_HEIGHTSHRINK
:
6506 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
6509 case SWM_ARG_ID_HEIGHTGROW
:
6510 HEIGHT(win
) += SWM_RESIZE_STEPS
;
6517 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6518 SWM_CW_HARDBOUNDARY
);
6520 store_float_geom(win
);
6524 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6525 SWM_CW_SOFTBOUNDARY
);
6528 /* get cursor offset from window root */
6529 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6536 if (xpr
->win_x
< WIDTH(win
) / 2)
6539 if (xpr
->win_y
< HEIGHT(win
) / 2)
6542 if (opt
== SWM_ARG_ID_CENTER
)
6543 cursor
= cursors
[XC_SIZING
].cid
;
6545 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
6546 XC_TOP_RIGHT_CORNER
].cid
;
6548 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
6549 XC_BOTTOM_RIGHT_CORNER
].cid
;
6551 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6552 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
6555 /* Release keyboard freeze if called via keybind. */
6556 if (bp
->type
== KEYBIND
)
6557 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6562 while (resizing
&& (evt
= get_next_event(true))) {
6563 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6564 case XCB_BUTTON_RELEASE
:
6565 if (bp
->type
== BTNBIND
&& bp
->value
==
6566 ((xcb_button_release_event_t
*)evt
)->detail
)
6569 case XCB_KEY_RELEASE
:
6570 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
6573 case XCB_MOTION_NOTIFY
:
6574 mne
= (xcb_motion_notify_event_t
*)evt
;
6575 DNPRINTF(SWM_D_EVENT
, "resize: MOTION_NOTIFY: "
6576 "root: %#x\n", mne
->root
);
6578 /* cursor offset/delta from start of the operation */
6579 dx
= mne
->root_x
- xpr
->root_x
;
6580 dy
= mne
->root_y
- xpr
->root_y
;
6586 if (opt
== SWM_ARG_ID_CENTER
) {
6587 if (g
.h
/ 2 + dy
< 1)
6591 HEIGHT(win
) = g
.h
+ 2 * dy
;
6599 HEIGHT(win
) = g
.h
+ dy
;
6606 if (opt
== SWM_ARG_ID_CENTER
) {
6607 if (g
.w
/ 2 + dx
< 1)
6611 WIDTH(win
) = g
.w
+ 2 * dx
;
6619 WIDTH(win
) = g
.w
+ dx
;
6622 /* not free, don't sync more than 120 times / second */
6623 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6624 timestamp
= mne
->time
;
6625 regionize(win
, mne
->root_x
, mne
->root_y
);
6626 region_containment(win
, r
, SWM_CW_ALLSIDES
|
6627 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
6628 SWM_CW_SOFTBOUNDARY
);
6633 case XCB_BUTTON_PRESS
:
6635 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_PRESS ignored\n");
6636 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
6637 ((xcb_button_press_event_t
*)evt
)->time
);
6642 DNPRINTF(SWM_D_EVENT
, "resize: KEY_PRESS ignored\n");
6643 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6644 ((xcb_key_press_event_t
*)evt
)->time
);
6650 /* It's possible for win to have been freed above. */
6651 if (validate_win(win
)) {
6652 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6660 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6661 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
6665 store_float_geom(win
);
6667 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6669 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
6673 resize(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6675 struct ws_win
*win
= NULL
;
6676 xcb_query_pointer_reply_t
*qpr
= NULL
;
6681 if (args
->id
!= SWM_ARG_ID_DONTCENTER
&&
6682 args
->id
!= SWM_ARG_ID_CENTER
) {
6683 /* {height,width}_{grow,shrink} use the focus window. */
6687 /* resize uses window under pointer. */
6688 qpr
= xcb_query_pointer_reply(conn
,
6689 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
6691 win
= find_window(qpr
->child
);
6697 resize_win(win
, bp
, args
->id
);
6699 if (args
->id
&& bp
->type
== KEYBIND
)
6705 /* Try to set window region based on supplied coordinates or window center. */
6707 regionize(struct ws_win
*win
, int x
, int y
)
6709 struct swm_region
*r
= NULL
;
6711 r
= region_under(win
->s
, x
, y
);
6713 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
6714 Y(win
) + HEIGHT(win
) / 2);
6716 if (r
!= NULL
&& r
!= win
->ws
->r
) {
6717 if (clear_maximized(r
->ws
) > 0)
6720 win_to_ws(win
, r
->ws
->idx
, false);
6722 /* Set focus on new ws. */
6723 unfocus_win(r
->ws
->focus
);
6731 #define SWM_MOVE_STEPS (50)
6734 move_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
6736 struct swm_region
*r
;
6737 xcb_timestamp_t timestamp
= 0;
6738 xcb_query_pointer_reply_t
*qpr
= NULL
;
6739 xcb_generic_event_t
*evt
;
6740 xcb_motion_notify_event_t
*mne
;
6741 bool moving
, restack
= false, step
= false;
6746 if ((r
= win
->ws
->r
) == NULL
)
6749 if (FULLSCREEN(win
))
6752 DNPRINTF(SWM_D_EVENT
, "move: win %#x, floating: %s, transient: "
6753 "%#x\n", win
->id
, YESNO(ABOVE(win
)), win
->transient
);
6755 /* in max_stack mode should only move transients */
6756 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6759 if (!(ABOVE(win
) || TRANS(win
)) || MAXIMIZED(win
)) {
6760 store_float_geom(win
);
6764 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6766 ewmh_update_wm_state(win
);
6773 /* It's possible for win to have been freed during focus_flush(). */
6774 if (validate_win(win
)) {
6775 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
6780 case SWM_ARG_ID_MOVELEFT
:
6781 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
6784 case SWM_ARG_ID_MOVERIGHT
:
6785 X(win
) += (SWM_MOVE_STEPS
- border_width
);
6788 case SWM_ARG_ID_MOVEUP
:
6789 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
6792 case SWM_ARG_ID_MOVEDOWN
:
6793 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
6800 regionize(win
, -1, -1);
6801 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
6803 store_float_geom(win
);
6807 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6808 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
6809 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
6811 /* get cursor offset from window root */
6812 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6815 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6819 /* Release keyboard freeze if called via keybind. */
6820 if (bp
->type
== KEYBIND
)
6821 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6824 regionize(win
, qpr
->root_x
, qpr
->root_y
);
6825 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
6826 SWM_CW_SOFTBOUNDARY
);
6830 while (moving
&& (evt
= get_next_event(true))) {
6831 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6832 case XCB_BUTTON_RELEASE
:
6833 if (bp
->type
== BTNBIND
&& bp
->value
==
6834 ((xcb_button_release_event_t
*)evt
)->detail
)
6837 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
6838 ((xcb_button_release_event_t
*)evt
)->time
);
6841 case XCB_KEY_RELEASE
:
6842 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
6845 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6846 ((xcb_key_release_event_t
*)evt
)->time
);
6849 case XCB_MOTION_NOTIFY
:
6850 mne
= (xcb_motion_notify_event_t
*)evt
;
6851 DNPRINTF(SWM_D_EVENT
, "move: MOTION_NOTIFY: "
6852 "root: %#x\n", mne
->root
);
6853 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
6854 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
6856 /* not free, don't sync more than 120 times / second */
6857 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6858 timestamp
= mne
->time
;
6859 regionize(win
, mne
->root_x
, mne
->root_y
);
6860 region_containment(win
, win
->ws
->r
,
6861 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
6866 case XCB_BUTTON_PRESS
:
6867 /* Thaw and ignore. */
6868 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
6869 ((xcb_button_press_event_t
*)evt
)->time
);
6874 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6875 ((xcb_key_press_event_t
*)evt
)->time
);
6881 /* It's possible for win to have been freed. */
6882 if (validate_win(win
)) {
6883 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
6891 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
6892 SWM_CW_SOFTBOUNDARY
);
6896 store_float_geom(win
);
6898 /* New region set to fullscreen layout. */
6899 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
]) {
6906 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6907 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
6911 move(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6913 struct ws_win
*win
= NULL
;
6914 xcb_query_pointer_reply_t
*qpr
= NULL
;
6920 /* move_* uses focus window. */
6924 /* move uses window under pointer. */
6925 qpr
= xcb_query_pointer_reply(conn
,
6926 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
6928 win
= find_window(qpr
->child
);
6934 move_win(win
, bp
, args
->id
);
6936 if (args
->id
&& bp
->type
== KEYBIND
)
6942 /* action definitions */
6944 char name
[SWM_FUNCNAME_LEN
];
6945 void (*func
)(struct binding
*, struct swm_region
*,
6949 } actions
[FN_INVALID
+ 1] = {
6950 /* name function argument */
6951 { "bar_toggle", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
6952 { "bar_toggle_ws", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
6953 { "button2", pressbutton
, 0, {.id
= 2} },
6954 { "cycle_layout", cycle_layout
, 0, {0} },
6955 { "flip_layout", stack_config
, 0, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
6956 { "float_toggle", floating_toggle
,0, {0} },
6957 { "focus", focus_pointer
, 0, {0} },
6958 { "focus_main", focus
, 0, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
6959 { "focus_next", focus
, 0, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
6960 { "focus_prev", focus
, 0, {.id
= SWM_ARG_ID_FOCUSPREV
} },
6961 { "focus_urgent", focus
, 0, {.id
= SWM_ARG_ID_FOCUSURGENT
} },
6962 { "maximize_toggle", maximize_toggle
,0, {0} },
6963 { "height_grow", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
6964 { "height_shrink", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
6965 { "iconify", iconify
, 0, {0} },
6966 { "master_shrink", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
6967 { "master_grow", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERGROW
} },
6968 { "master_add", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERADD
} },
6969 { "master_del", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERDEL
} },
6970 { "move", move
, FN_F_NOREPLAY
, {0} },
6971 { "move_down", move
, 0, {.id
= SWM_ARG_ID_MOVEDOWN
} },
6972 { "move_left", move
, 0, {.id
= SWM_ARG_ID_MOVELEFT
} },
6973 { "move_right", move
, 0, {.id
= SWM_ARG_ID_MOVERIGHT
} },
6974 { "move_up", move
, 0, {.id
= SWM_ARG_ID_MOVEUP
} },
6975 { "mvrg_1", send_to_rg
, 0, {.id
= 0} },
6976 { "mvrg_2", send_to_rg
, 0, {.id
= 1} },
6977 { "mvrg_3", send_to_rg
, 0, {.id
= 2} },
6978 { "mvrg_4", send_to_rg
, 0, {.id
= 3} },
6979 { "mvrg_5", send_to_rg
, 0, {.id
= 4} },
6980 { "mvrg_6", send_to_rg
, 0, {.id
= 5} },
6981 { "mvrg_7", send_to_rg
, 0, {.id
= 6} },
6982 { "mvrg_8", send_to_rg
, 0, {.id
= 7} },
6983 { "mvrg_9", send_to_rg
, 0, {.id
= 8} },
6984 { "mvws_1", send_to_ws
, 0, {.id
= 0} },
6985 { "mvws_2", send_to_ws
, 0, {.id
= 1} },
6986 { "mvws_3", send_to_ws
, 0, {.id
= 2} },
6987 { "mvws_4", send_to_ws
, 0, {.id
= 3} },
6988 { "mvws_5", send_to_ws
, 0, {.id
= 4} },
6989 { "mvws_6", send_to_ws
, 0, {.id
= 5} },
6990 { "mvws_7", send_to_ws
, 0, {.id
= 6} },
6991 { "mvws_8", send_to_ws
, 0, {.id
= 7} },
6992 { "mvws_9", send_to_ws
, 0, {.id
= 8} },
6993 { "mvws_10", send_to_ws
, 0, {.id
= 9} },
6994 { "mvws_11", send_to_ws
, 0, {.id
= 10} },
6995 { "mvws_12", send_to_ws
, 0, {.id
= 11} },
6996 { "mvws_13", send_to_ws
, 0, {.id
= 12} },
6997 { "mvws_14", send_to_ws
, 0, {.id
= 13} },
6998 { "mvws_15", send_to_ws
, 0, {.id
= 14} },
6999 { "mvws_16", send_to_ws
, 0, {.id
= 15} },
7000 { "mvws_17", send_to_ws
, 0, {.id
= 16} },
7001 { "mvws_18", send_to_ws
, 0, {.id
= 17} },
7002 { "mvws_19", send_to_ws
, 0, {.id
= 18} },
7003 { "mvws_20", send_to_ws
, 0, {.id
= 19} },
7004 { "mvws_21", send_to_ws
, 0, {.id
= 20} },
7005 { "mvws_22", send_to_ws
, 0, {.id
= 21} },
7006 { "name_workspace", name_workspace
, 0, {0} },
7007 { "quit", quit
, 0, {0} },
7008 { "raise_toggle", raise_toggle
, 0, {0} },
7009 { "resize", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_DONTCENTER
} },
7010 { "resize_centered", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_CENTER
} },
7011 { "restart", restart
, 0, {0} },
7012 { "rg_1", focusrg
, 0, {.id
= 0} },
7013 { "rg_2", focusrg
, 0, {.id
= 1} },
7014 { "rg_3", focusrg
, 0, {.id
= 2} },
7015 { "rg_4", focusrg
, 0, {.id
= 3} },
7016 { "rg_5", focusrg
, 0, {.id
= 4} },
7017 { "rg_6", focusrg
, 0, {.id
= 5} },
7018 { "rg_7", focusrg
, 0, {.id
= 6} },
7019 { "rg_8", focusrg
, 0, {.id
= 7} },
7020 { "rg_9", focusrg
, 0, {.id
= 8} },
7021 { "rg_move_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_UP
} },
7022 { "rg_move_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_DOWN
} },
7023 { "rg_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7024 { "rg_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7025 { "screen_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7026 { "screen_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7027 { "search_win", search_win
, 0, {0} },
7028 { "search_workspace", search_workspace
, 0, {0} },
7029 { "spawn_custom", NULL
, 0, {0} },
7030 { "stack_balance", stack_config
, 0, {.id
= SWM_ARG_ID_STACKBALANCE
} },
7031 { "stack_inc", stack_config
, 0, {.id
= SWM_ARG_ID_STACKINC
} },
7032 { "stack_dec", stack_config
, 0, {.id
= SWM_ARG_ID_STACKDEC
} },
7033 { "stack_reset", stack_config
, 0, {.id
= SWM_ARG_ID_STACKRESET
} },
7034 { "swap_main", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPMAIN
} },
7035 { "swap_next", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPNEXT
} },
7036 { "swap_prev", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPPREV
} },
7037 { "uniconify", uniconify
, 0, {0} },
7038 { "version", version
, 0, {0} },
7039 { "width_grow", resize
, 0, {.id
= SWM_ARG_ID_WIDTHGROW
} },
7040 { "width_shrink", resize
, 0, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
7041 { "wind_del", wkill
, 0, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
7042 { "wind_kill", wkill
, 0, {.id
= SWM_ARG_ID_KILLWINDOW
} },
7043 { "ws_1", switchws
, 0, {.id
= 0} },
7044 { "ws_2", switchws
, 0, {.id
= 1} },
7045 { "ws_3", switchws
, 0, {.id
= 2} },
7046 { "ws_4", switchws
, 0, {.id
= 3} },
7047 { "ws_5", switchws
, 0, {.id
= 4} },
7048 { "ws_6", switchws
, 0, {.id
= 5} },
7049 { "ws_7", switchws
, 0, {.id
= 6} },
7050 { "ws_8", switchws
, 0, {.id
= 7} },
7051 { "ws_9", switchws
, 0, {.id
= 8} },
7052 { "ws_10", switchws
, 0, {.id
= 9} },
7053 { "ws_11", switchws
, 0, {.id
= 10} },
7054 { "ws_12", switchws
, 0, {.id
= 11} },
7055 { "ws_13", switchws
, 0, {.id
= 12} },
7056 { "ws_14", switchws
, 0, {.id
= 13} },
7057 { "ws_15", switchws
, 0, {.id
= 14} },
7058 { "ws_16", switchws
, 0, {.id
= 15} },
7059 { "ws_17", switchws
, 0, {.id
= 16} },
7060 { "ws_18", switchws
, 0, {.id
= 17} },
7061 { "ws_19", switchws
, 0, {.id
= 18} },
7062 { "ws_20", switchws
, 0, {.id
= 19} },
7063 { "ws_21", switchws
, 0, {.id
= 20} },
7064 { "ws_22", switchws
, 0, {.id
= 21} },
7065 { "ws_next", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
7066 { "ws_next_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
7067 { "ws_next_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
7068 { "ws_prev", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
7069 { "ws_prev_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
7070 { "ws_prev_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
7071 { "ws_prior", priorws
, 0, {0} },
7072 { "dumpwins", dumpwins
, 0, {0} }, /* MUST BE LAST */
7073 { "invalid action", NULL
, 0, {0} },
7077 update_modkey(uint16_t mod
)
7081 /* Replace all instances of the old mod key. */
7082 RB_FOREACH(bp
, binding_tree
, &bindings
)
7083 if (bp
->mod
& mod_key
)
7084 bp
->mod
= (bp
->mod
& ~mod_key
) | mod
;
7089 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7092 struct spawn_prog
*prog
= NULL
;
7094 char *ap
, **real_args
;
7096 /* suppress unused warning since var is needed */
7099 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
7102 TAILQ_FOREACH(prog
, &spawns
, entry
) {
7103 if (strcasecmp(spawn_name
, prog
->name
) == 0)
7107 warnx("spawn_custom: program %s not found", spawn_name
);
7111 /* make room for expanded args */
7112 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
7113 err(1, "spawn_custom: calloc real_args");
7115 /* expand spawn_args into real_args */
7116 for (i
= c
= 0; i
< prog
->argc
; i
++) {
7118 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
7119 if (strcasecmp(ap
, "$bar_border") == 0) {
7121 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
7123 err(1, "spawn_custom border color");
7124 } else if (strcasecmp(ap
, "$bar_color") == 0) {
7126 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
7128 err(1, "spawn_custom bar color");
7129 } else if (strcasecmp(ap
, "$bar_font") == 0) {
7130 if ((real_args
[c
] = strdup(bar_fonts
))
7132 err(1, "spawn_custom bar fonts");
7133 } else if (strcasecmp(ap
, "$bar_font_color") == 0) {
7135 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
7137 err(1, "spawn_custom color font");
7138 } else if (strcasecmp(ap
, "$color_focus") == 0) {
7140 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
7142 err(1, "spawn_custom color focus");
7143 } else if (strcasecmp(ap
, "$color_focus_maximized") == 0) {
7145 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].name
))
7147 err(1, "spawn_custom color focus maximized");
7148 } else if (strcasecmp(ap
, "$color_unfocus") == 0) {
7150 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
7152 err(1, "spawn_custom color unfocus");
7153 } else if (strcasecmp(ap
, "$color_unfocus_maximized") == 0) {
7155 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].name
))
7157 err(1, "spawn_custom color unfocus maximized");
7158 } else if (strcasecmp(ap
, "$region_index") == 0) {
7159 if (asprintf(&real_args
[c
], "%d",
7160 get_region_index(r
) + 1) < 1)
7161 err(1, "spawn_custom region index");
7162 } else if (strcasecmp(ap
, "$workspace_index") == 0) {
7163 if (asprintf(&real_args
[c
], "%d", r
->ws
->idx
+ 1) < 1)
7164 err(1, "spawn_custom workspace index");
7165 } else if (strcasecmp(ap
, "$dmenu_bottom") == 0) {
7168 if ((real_args
[c
] = strdup("-b")) == NULL
)
7169 err(1, "spawn_custom workspace index");
7171 /* no match --> copy as is */
7172 if ((real_args
[c
] = strdup(ap
)) == NULL
)
7173 err(1, "spawn_custom strdup(ap)");
7175 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
7181 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
7182 for (i
= 0; i
< c
; ++i
)
7183 DPRINTF("\"%s\" ", real_args
[i
]);
7186 *ret_args
= real_args
;
7191 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
7197 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7201 spawn(r
->ws
->idx
, &a
, true);
7203 for (i
= 0; i
< spawn_argc
; i
++)
7209 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7216 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7220 if (pipe(select_list_pipe
) == -1)
7221 err(1, "pipe error");
7222 if (pipe(select_resp_pipe
) == -1)
7223 err(1, "pipe error");
7225 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
7226 err(1, "could not disable SIGPIPE");
7227 switch (*pid
= fork()) {
7229 err(1, "cannot fork");
7232 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
7234 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
7236 close(select_list_pipe
[1]);
7237 close(select_resp_pipe
[0]);
7238 spawn(r
->ws
->idx
, &a
, false);
7240 default: /* parent */
7241 close(select_list_pipe
[0]);
7242 close(select_resp_pipe
[1]);
7246 for (i
= 0; i
< spawn_argc
; i
++)
7251 /* Argument tokenizer. */
7254 char *arg
, *cp
, *next
;
7255 bool single_quoted
= false, double_quoted
= false;
7260 /* Eat and move characters until end of argument is found. */
7261 for (arg
= next
= cp
= *sp
; *cp
!= '\0'; ++cp
) {
7262 if (!double_quoted
&& *cp
== '\'') {
7263 /* Eat single-quote. */
7264 single_quoted
= !single_quoted
;
7265 } else if (!single_quoted
&& *cp
== '"') {
7266 /* Eat double-quote. */
7267 double_quoted
= !double_quoted
;
7268 } else if (!single_quoted
&& *cp
== '\\' && *(cp
+ 1) == '"') {
7269 /* Eat backslash; copy escaped character to arg. */
7271 } else if (!single_quoted
&& !double_quoted
&& *cp
== '\\' &&
7272 (*(cp
+ 1) == '\'' || *(cp
+ 1) == ' ')) {
7273 /* Eat backslash; move escaped character. */
7275 } else if (!single_quoted
&& !double_quoted
&&
7276 (*cp
== ' ' || *cp
== '\t')) {
7277 /* Terminate argument. */
7279 /* Point sp to beginning of next argument. */
7283 /* Move regular character. */
7288 /* Terminate argument if end of string. */
7298 spawn_insert(const char *name
, const char *args
, int flags
)
7300 struct spawn_prog
*sp
;
7301 char *arg
, *cp
, *ptr
;
7303 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s[%s]\n", name
, args
);
7305 if (args
== NULL
|| *args
== '\0')
7308 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
7309 err(1, "spawn_insert: calloc");
7310 if ((sp
->name
= strdup(name
)) == NULL
)
7311 err(1, "spawn_insert: strdup");
7313 /* Convert the arguments to an argument list. */
7314 if ((ptr
= cp
= strdup(args
)) == NULL
)
7315 err(1, "spawn_insert: strdup");
7316 while ((arg
= argsep(&ptr
)) != NULL
) {
7317 /* Null argument; skip it. */
7322 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
7323 sizeof *sp
->argv
)) == NULL
)
7324 err(1, "spawn_insert: realloc");
7325 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
7326 err(1, "spawn_insert: strdup");
7332 DNPRINTF(SWM_D_SPAWN
, "arg %d: [%s]\n", sp
->argc
, sp
->argv
[sp
->argc
-1]);
7333 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
7334 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
7338 spawn_remove(struct spawn_prog
*sp
)
7342 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
7344 TAILQ_REMOVE(&spawns
, sp
, entry
);
7345 for (i
= 0; i
< sp
->argc
; i
++)
7351 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
7357 struct spawn_prog
*sp
;
7359 while ((sp
= TAILQ_FIRST(&spawns
)) != NULL
) {
7365 spawn_find(const char *name
)
7367 struct spawn_prog
*sp
;
7369 TAILQ_FOREACH(sp
, &spawns
, entry
)
7370 if (strcasecmp(sp
->name
, name
) == 0)
7377 setspawn(const char *name
, const char *args
, int flags
)
7379 struct spawn_prog
*sp
;
7381 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
7386 /* Remove any old spawn under the same name. */
7387 if ((sp
= spawn_find(name
)) != NULL
)
7391 spawn_insert(name
, args
, flags
);
7393 warnx("error: setspawn: cannot find program: %s", name
);
7395 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
7399 setconfspawn(const char *selector
, const char *value
, int flags
)
7403 if (selector
== NULL
|| strlen(selector
) == 0)
7406 args
= expand_tilde(value
);
7408 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
7410 setspawn(selector
, args
, flags
);
7413 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
7418 validate_spawns(void)
7421 struct spawn_prog
*sp
;
7422 char which
[PATH_MAX
];
7425 RB_FOREACH(bp
, binding_tree
, &bindings
) {
7426 if (bp
->action
!= FN_SPAWN_CUSTOM
)
7430 sp
= spawn_find(bp
->spawn_name
);
7431 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
7434 /* verify we have the goods */
7435 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
7436 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
7438 for (i
= strlen("which "); i
< strlen(which
); i
++)
7439 if (which
[i
] == ' ') {
7443 if (system(which
) != 0)
7444 add_startup_exception("could not find %s",
7445 &which
[strlen("which ")]);
7452 setconfspawn("lock", "xlock", 0);
7454 setconfspawn("term", "xterm", 0);
7455 setconfspawn("spawn_term", "xterm", 0);
7457 setconfspawn("menu", "dmenu_run"
7461 " -nf $bar_font_color"
7463 " -sf $bar_color", 0);
7465 setconfspawn("search", "dmenu"
7470 " -nf $bar_font_color"
7472 " -sf $bar_color", 0);
7474 setconfspawn("name_workspace", "dmenu"
7479 " -nf $bar_font_color"
7481 " -sf $bar_color", 0);
7483 /* These are not verified for existence, even with a binding set. */
7484 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
7485 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
7486 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
7490 #define SWM_MODNAME_SIZE 32
7491 #define SWM_KEY_WS "\n+ \t"
7493 parsebinding(const char *bindstr
, uint16_t *mod
, enum binding_type
*type
,
7494 uint32_t *val
, uint32_t *flags
)
7496 char *str
, *cp
, *name
;
7497 KeySym ks
, lks
, uks
;
7499 DNPRINTF(SWM_D_KEY
, "parsebinding: enter [%s]\n", bindstr
);
7500 if (mod
== NULL
|| val
== NULL
) {
7501 DNPRINTF(SWM_D_KEY
, "parsebinding: no mod or key vars\n");
7504 if (bindstr
== NULL
|| strlen(bindstr
) == 0) {
7505 DNPRINTF(SWM_D_KEY
, "parsebinding: no bindstr\n");
7509 if ((cp
= str
= strdup(bindstr
)) == NULL
)
7510 err(1, "parsebinding: strdup");
7512 *val
= XCB_NO_SYMBOL
;
7516 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
7517 DNPRINTF(SWM_D_KEY
, "parsebinding: entry [%s]\n", name
);
7519 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
7520 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
7522 else if (strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
) == 0)
7523 *mod
|= XCB_MOD_MASK_1
;
7524 else if (strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
) == 0)
7525 *mod
|= XCB_MOD_MASK_2
;
7526 else if (strncmp(name
, "Mod3", SWM_MODNAME_SIZE
) == 0)
7527 *mod
|= XCB_MOD_MASK_3
;
7528 else if (strncmp(name
, "Mod4", SWM_MODNAME_SIZE
) == 0)
7529 *mod
|= XCB_MOD_MASK_4
;
7530 else if (strncmp(name
, "Mod5", SWM_MODNAME_SIZE
) == 0)
7531 *mod
|= XCB_MOD_MASK_5
;
7532 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
7533 *mod
|= XCB_MOD_MASK_SHIFT
;
7534 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
7535 *mod
|= XCB_MOD_MASK_CONTROL
;
7536 else if (strncasecmp(name
, "ANYMOD", SWM_MODNAME_SIZE
) == 0)
7537 *mod
|= XCB_MOD_MASK_ANY
;
7538 else if (strncasecmp(name
, "REPLAY", SWM_MODNAME_SIZE
) == 0)
7539 *flags
|= BINDING_F_REPLAY
;
7540 else if (sscanf(name
, "Button%u", val
) == 1) {
7541 DNPRINTF(SWM_D_KEY
, "parsebinding: button %u\n", *val
);
7543 if (*val
> 255 || *val
== 0) {
7545 "parsebinding: invalid button %u\n", *val
);
7549 /* TODO: do this without Xlib. */
7550 ks
= XStringToKeysym(name
);
7551 if (ks
== NoSymbol
) {
7553 "parsebinding: invalid key %s\n", name
);
7558 XConvertCase(ks
, &lks
, &uks
);
7559 *val
= (uint32_t)lks
;
7563 /* If ANYMOD was specified, ignore the rest. */
7564 if (*mod
& XCB_MOD_MASK_ANY
)
7565 *mod
= XCB_MOD_MASK_ANY
;
7568 DNPRINTF(SWM_D_KEY
, "parsebinding: leave ok\n");
7573 strdupsafe(const char *str
)
7578 return (strdup(str
));
7582 binding_cmp(struct binding
*bp1
, struct binding
*bp2
)
7584 if (bp1
->type
< bp2
->type
)
7586 if (bp1
->type
> bp2
->type
)
7589 if (bp1
->value
< bp2
->value
)
7591 if (bp1
->value
> bp2
->value
)
7594 if (bp1
->mod
< bp2
->mod
)
7596 if (bp1
->mod
> bp2
->mod
)
7603 binding_insert(uint16_t mod
, enum binding_type type
, uint32_t val
,
7604 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
7608 DNPRINTF(SWM_D_KEY
, "binding_insert: mod: %u, type: %d, val: %u, "
7609 "action: %s(%d), spawn_name: %s\n", mod
, type
, val
,
7610 actions
[aid
].name
, aid
, spawn_name
);
7612 if ((bp
= malloc(sizeof *bp
)) == NULL
)
7613 err(1, "binding_insert: malloc");
7620 bp
->spawn_name
= strdupsafe(spawn_name
);
7621 RB_INSERT(binding_tree
, &bindings
, bp
);
7623 DNPRINTF(SWM_D_KEY
, "binding_insert: leave\n");
7627 binding_lookup(uint16_t mod
, enum binding_type type
, uint32_t val
)
7635 return (RB_FIND(binding_tree
, &bindings
, &bp
));
7639 binding_remove(struct binding
*bp
)
7641 DNPRINTF(SWM_D_KEY
, "binding_remove: mod: %u, type: %d, val: %u, "
7642 "action: %s(%d), spawn_name: %s\n", bp
->mod
, bp
->type
, bp
->value
,
7643 actions
[bp
->action
].name
, bp
->action
, bp
->spawn_name
);
7645 RB_REMOVE(binding_tree
, &bindings
, bp
);
7646 free(bp
->spawn_name
);
7649 DNPRINTF(SWM_D_KEY
, "binding_remove: leave\n");
7653 setbinding(uint16_t mod
, enum binding_type type
, uint32_t val
,
7654 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
7658 DNPRINTF(SWM_D_KEY
, "setbinding: enter %s [%s]\n",
7659 actions
[aid
].name
, spawn_name
);
7661 /* Unbind any existing. Loop is to handle MOD_MASK_ANY. */
7662 while ((bp
= binding_lookup(mod
, type
, val
)))
7665 if (aid
!= FN_INVALID
)
7666 binding_insert(mod
, type
, val
, aid
, flags
, spawn_name
);
7668 DNPRINTF(SWM_D_KEY
, "setbinding: leave\n");
7672 setconfbinding(const char *selector
, const char *value
, int flags
)
7674 struct spawn_prog
*sp
;
7675 uint32_t keybtn
, opts
;
7678 enum binding_type type
;
7680 /* suppress unused warning since var is needed */
7683 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter selector: [%s], "
7684 "value: [%s]\n", selector
, value
);
7685 if (selector
== NULL
|| strlen(selector
) == 0) {
7686 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
7687 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
7688 setbinding(mod
, type
, keybtn
, FN_INVALID
, opts
, NULL
);
7693 /* search by key function name */
7694 for (aid
= 0; aid
< FN_INVALID
; aid
++) {
7695 if (strncasecmp(selector
, actions
[aid
].name
,
7696 SWM_FUNCNAME_LEN
) == 0) {
7697 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
7698 "action\n", selector
);
7699 if (parsebinding(value
, &mod
, &type
, &keybtn
,
7701 setbinding(mod
, type
, keybtn
, aid
, opts
, NULL
);
7707 /* search by custom spawn name */
7708 if ((sp
= spawn_find(selector
)) != NULL
) {
7709 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
7710 "spawn\n", selector
);
7711 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
7712 setbinding(mod
, type
, keybtn
, FN_SPAWN_CUSTOM
, opts
,
7718 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
7722 #define MODSHIFT MODKEY | XCB_MOD_MASK_SHIFT
7724 setup_keybindings(void)
7726 #define BINDKEY(m, k, a) setbinding(m, KEYBIND, k, a, 0, NULL)
7727 #define BINDKEYSPAWN(m, k, s) setbinding(m, KEYBIND, k, FN_SPAWN_CUSTOM, 0, s)
7728 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
7729 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
7730 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
7731 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
7732 BINDKEY(MODKEY
, XK_v
, FN_BUTTON2
);
7733 BINDKEY(MODKEY
, XK_space
, FN_CYCLE_LAYOUT
);
7734 BINDKEY(MODSHIFT
, XK_backslash
, FN_FLIP_LAYOUT
);
7735 BINDKEY(MODKEY
, XK_t
, FN_FLOAT_TOGGLE
);
7736 BINDKEY(MODKEY
, XK_m
, FN_FOCUS_MAIN
);
7737 BINDKEY(MODKEY
, XK_j
, FN_FOCUS_NEXT
);
7738 BINDKEY(MODKEY
, XK_Tab
, FN_FOCUS_NEXT
);
7739 BINDKEY(MODKEY
, XK_k
, FN_FOCUS_PREV
);
7740 BINDKEY(MODSHIFT
, XK_Tab
, FN_FOCUS_PREV
);
7741 BINDKEY(MODKEY
, XK_u
, FN_FOCUS_URGENT
);
7742 BINDKEY(MODKEY
, XK_e
, FN_MAXIMIZE_TOGGLE
);
7743 BINDKEY(MODSHIFT
, XK_equal
, FN_HEIGHT_GROW
);
7744 BINDKEY(MODSHIFT
, XK_minus
, FN_HEIGHT_SHRINK
);
7745 BINDKEY(MODKEY
, XK_w
, FN_ICONIFY
);
7746 BINDKEY(MODKEY
, XK_h
, FN_MASTER_SHRINK
);
7747 BINDKEY(MODKEY
, XK_l
, FN_MASTER_GROW
);
7748 BINDKEY(MODKEY
, XK_comma
, FN_MASTER_ADD
);
7749 BINDKEY(MODKEY
, XK_period
, FN_MASTER_DEL
);
7750 BINDKEY(MODSHIFT
, XK_bracketright
, FN_MOVE_DOWN
);
7751 BINDKEY(MODKEY
, XK_bracketleft
, FN_MOVE_LEFT
);
7752 BINDKEY(MODKEY
, XK_bracketright
, FN_MOVE_RIGHT
);
7753 BINDKEY(MODSHIFT
, XK_bracketleft
, FN_MOVE_UP
);
7754 BINDKEY(MODSHIFT
, XK_KP_End
, FN_MVRG_1
);
7755 BINDKEY(MODSHIFT
, XK_KP_Down
, FN_MVRG_2
);
7756 BINDKEY(MODSHIFT
, XK_KP_Next
, FN_MVRG_3
);
7757 BINDKEY(MODSHIFT
, XK_KP_Left
, FN_MVRG_4
);
7758 BINDKEY(MODSHIFT
, XK_KP_Begin
, FN_MVRG_5
);
7759 BINDKEY(MODSHIFT
, XK_KP_Right
, FN_MVRG_6
);
7760 BINDKEY(MODSHIFT
, XK_KP_Home
, FN_MVRG_7
);
7761 BINDKEY(MODSHIFT
, XK_KP_Up
, FN_MVRG_8
);
7762 BINDKEY(MODSHIFT
, XK_KP_Prior
, FN_MVRG_9
);
7763 BINDKEY(MODSHIFT
, XK_1
, FN_MVWS_1
);
7764 BINDKEY(MODSHIFT
, XK_2
, FN_MVWS_2
);
7765 BINDKEY(MODSHIFT
, XK_3
, FN_MVWS_3
);
7766 BINDKEY(MODSHIFT
, XK_4
, FN_MVWS_4
);
7767 BINDKEY(MODSHIFT
, XK_5
, FN_MVWS_5
);
7768 BINDKEY(MODSHIFT
, XK_6
, FN_MVWS_6
);
7769 BINDKEY(MODSHIFT
, XK_7
, FN_MVWS_7
);
7770 BINDKEY(MODSHIFT
, XK_8
, FN_MVWS_8
);
7771 BINDKEY(MODSHIFT
, XK_9
, FN_MVWS_9
);
7772 BINDKEY(MODSHIFT
, XK_0
, FN_MVWS_10
);
7773 BINDKEY(MODSHIFT
, XK_F1
, FN_MVWS_11
);
7774 BINDKEY(MODSHIFT
, XK_F2
, FN_MVWS_12
);
7775 BINDKEY(MODSHIFT
, XK_F3
, FN_MVWS_13
);
7776 BINDKEY(MODSHIFT
, XK_F4
, FN_MVWS_14
);
7777 BINDKEY(MODSHIFT
, XK_F5
, FN_MVWS_15
);
7778 BINDKEY(MODSHIFT
, XK_F6
, FN_MVWS_16
);
7779 BINDKEY(MODSHIFT
, XK_F7
, FN_MVWS_17
);
7780 BINDKEY(MODSHIFT
, XK_F8
, FN_MVWS_18
);
7781 BINDKEY(MODSHIFT
, XK_F9
, FN_MVWS_19
);
7782 BINDKEY(MODSHIFT
, XK_F10
, FN_MVWS_20
);
7783 BINDKEY(MODSHIFT
, XK_F11
, FN_MVWS_21
);
7784 BINDKEY(MODSHIFT
, XK_F12
, FN_MVWS_22
);
7785 BINDKEY(MODSHIFT
, XK_slash
, FN_NAME_WORKSPACE
);
7786 BINDKEY(MODSHIFT
, XK_q
, FN_QUIT
);
7787 BINDKEY(MODSHIFT
, XK_r
, FN_RAISE_TOGGLE
);
7788 BINDKEY(MODKEY
, XK_q
, FN_RESTART
);
7789 BINDKEY(MODKEY
, XK_KP_End
, FN_RG_1
);
7790 BINDKEY(MODKEY
, XK_KP_Down
, FN_RG_2
);
7791 BINDKEY(MODKEY
, XK_KP_Next
, FN_RG_3
);
7792 BINDKEY(MODKEY
, XK_KP_Left
, FN_RG_4
);
7793 BINDKEY(MODKEY
, XK_KP_Begin
, FN_RG_5
);
7794 BINDKEY(MODKEY
, XK_KP_Right
, FN_RG_6
);
7795 BINDKEY(MODKEY
, XK_KP_Home
, FN_RG_7
);
7796 BINDKEY(MODKEY
, XK_KP_Up
, FN_RG_8
);
7797 BINDKEY(MODKEY
, XK_KP_Prior
, FN_RG_9
);
7798 BINDKEY(MODSHIFT
, XK_Right
, FN_RG_NEXT
);
7799 BINDKEY(MODSHIFT
, XK_Left
, FN_RG_PREV
);
7800 BINDKEY(MODKEY
, XK_f
, FN_SEARCH_WIN
);
7801 BINDKEY(MODKEY
, XK_slash
, FN_SEARCH_WORKSPACE
);
7802 BINDKEYSPAWN(MODSHIFT
, XK_i
, "initscr");
7803 BINDKEYSPAWN(MODSHIFT
, XK_Delete
, "lock");
7804 BINDKEYSPAWN(MODKEY
, XK_p
, "menu");
7805 BINDKEYSPAWN(MODKEY
, XK_s
, "screenshot_all");
7806 BINDKEYSPAWN(MODSHIFT
, XK_s
, "screenshot_wind");
7807 BINDKEYSPAWN(MODSHIFT
, XK_Return
, "term");
7808 BINDKEY(MODSHIFT
, XK_comma
, FN_STACK_INC
);
7809 BINDKEY(MODSHIFT
, XK_period
, FN_STACK_DEC
);
7810 BINDKEY(MODSHIFT
, XK_space
, FN_STACK_RESET
);
7811 BINDKEY(MODKEY
, XK_Return
, FN_SWAP_MAIN
);
7812 BINDKEY(MODSHIFT
, XK_j
, FN_SWAP_NEXT
);
7813 BINDKEY(MODSHIFT
, XK_k
, FN_SWAP_PREV
);
7814 BINDKEY(MODSHIFT
, XK_w
, FN_UNICONIFY
);
7815 BINDKEY(MODSHIFT
, XK_v
, FN_VERSION
);
7816 BINDKEY(MODKEY
, XK_equal
, FN_WIDTH_GROW
);
7817 BINDKEY(MODKEY
, XK_minus
, FN_WIDTH_SHRINK
);
7818 BINDKEY(MODKEY
, XK_x
, FN_WIND_DEL
);
7819 BINDKEY(MODSHIFT
, XK_x
, FN_WIND_KILL
);
7820 BINDKEY(MODKEY
, XK_1
, FN_WS_1
);
7821 BINDKEY(MODKEY
, XK_2
, FN_WS_2
);
7822 BINDKEY(MODKEY
, XK_3
, FN_WS_3
);
7823 BINDKEY(MODKEY
, XK_4
, FN_WS_4
);
7824 BINDKEY(MODKEY
, XK_5
, FN_WS_5
);
7825 BINDKEY(MODKEY
, XK_6
, FN_WS_6
);
7826 BINDKEY(MODKEY
, XK_7
, FN_WS_7
);
7827 BINDKEY(MODKEY
, XK_8
, FN_WS_8
);
7828 BINDKEY(MODKEY
, XK_9
, FN_WS_9
);
7829 BINDKEY(MODKEY
, XK_0
, FN_WS_10
);
7830 BINDKEY(MODKEY
, XK_F1
, FN_WS_11
);
7831 BINDKEY(MODKEY
, XK_F2
, FN_WS_12
);
7832 BINDKEY(MODKEY
, XK_F3
, FN_WS_13
);
7833 BINDKEY(MODKEY
, XK_F4
, FN_WS_14
);
7834 BINDKEY(MODKEY
, XK_F5
, FN_WS_15
);
7835 BINDKEY(MODKEY
, XK_F6
, FN_WS_16
);
7836 BINDKEY(MODKEY
, XK_F7
, FN_WS_17
);
7837 BINDKEY(MODKEY
, XK_F8
, FN_WS_18
);
7838 BINDKEY(MODKEY
, XK_F9
, FN_WS_19
);
7839 BINDKEY(MODKEY
, XK_F10
, FN_WS_20
);
7840 BINDKEY(MODKEY
, XK_F11
, FN_WS_21
);
7841 BINDKEY(MODKEY
, XK_F12
, FN_WS_22
);
7842 BINDKEY(MODKEY
, XK_Right
, FN_WS_NEXT
);
7843 BINDKEY(MODKEY
, XK_Left
, FN_WS_PREV
);
7844 BINDKEY(MODKEY
, XK_Up
, FN_WS_NEXT_ALL
);
7845 BINDKEY(MODKEY
, XK_Down
, FN_WS_PREV_ALL
);
7846 BINDKEY(MODSHIFT
, XK_Up
, FN_WS_NEXT_MOVE
);
7847 BINDKEY(MODSHIFT
, XK_Down
, FN_WS_PREV_MOVE
);
7848 BINDKEY(MODKEY
, XK_a
, FN_WS_PRIOR
);
7850 BINDKEY(MODSHIFT
, XK_d
, FN_DUMPWINS
);
7857 setup_btnbindings(void)
7859 setbinding(ANYMOD
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_FOCUS
,
7860 BINDING_F_REPLAY
, NULL
);
7861 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE
, 0, NULL
);
7862 setbinding(MODSHIFT
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE_CENTERED
, 0,
7864 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_MOVE
, 0, NULL
);
7869 clear_bindings(void)
7873 while ((bp
= RB_ROOT(&bindings
)))
7878 clear_keybindings(void)
7880 struct binding
*bp
, *bptmp
;
7882 RB_FOREACH_SAFE(bp
, binding_tree
, &bindings
, bptmp
) {
7883 if (bp
->type
!= KEYBIND
)
7890 setkeymapping(const char *selector
, const char *value
, int flags
)
7892 char *keymapping_file
;
7894 /* suppress unused warnings since vars are needed */
7898 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
7900 keymapping_file
= expand_tilde(value
);
7902 clear_keybindings();
7903 /* load new key bindings; if it fails, revert to default bindings */
7904 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
7905 clear_keybindings();
7906 setup_keybindings();
7909 free(keymapping_file
);
7911 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
7916 updatenumlockmask(void)
7919 xcb_get_modifier_mapping_reply_t
*modmap_r
;
7920 xcb_keycode_t
*modmap
, kc
, *keycode
;
7924 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
7925 xcb_get_modifier_mapping(conn
),
7928 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
7929 for (i
= 0; i
< 8; i
++) {
7930 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
7931 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
7933 keycode
= xcb_key_symbols_get_keycode(syms
,
7937 numlockmask
= (1 << i
);
7944 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
7951 int num_screens
, k
, j
;
7952 uint16_t modifiers
[4];
7953 xcb_keycode_t
*code
;
7955 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
7956 updatenumlockmask();
7959 modifiers
[1] = numlockmask
;
7960 modifiers
[2] = XCB_MOD_MASK_LOCK
;
7961 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
7963 num_screens
= get_screen_count();
7964 for (k
= 0; k
< num_screens
; k
++) {
7965 if (TAILQ_EMPTY(&screens
[k
].rl
))
7967 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
7969 RB_FOREACH(bp
, binding_tree
, &bindings
) {
7970 if (bp
->type
!= KEYBIND
)
7973 /* If there is a catch-all, only bind that. */
7974 if ((binding_lookup(ANYMOD
, KEYBIND
, bp
->value
)) &&
7978 /* Skip unused ws binds. */
7979 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
7980 bp
->action
<= FN_WS_22
)
7983 /* Skip unused mvws binds. */
7984 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
7985 bp
->action
<= FN_MVWS_22
)
7988 if ((code
= xcb_key_symbols_get_keycode(syms
,
7989 bp
->value
)) == NULL
)
7992 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
7993 /* All modifiers are grabbed in one pass. */
7994 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
7995 "key: %u, modifiers: %d\n", bp
->value
,
7997 xcb_grab_key(conn
, 1, screens
[k
].root
,
7998 bp
->mod
, *code
, XCB_GRAB_MODE_ASYNC
,
7999 XCB_GRAB_MODE_SYNC
);
8001 /* Need to grab each modifier permutation. */
8002 for (j
= 0; j
< LENGTH(modifiers
); j
++) {
8003 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8004 "key: %u, modifiers: %d\n",
8005 bp
->value
, bp
->mod
| modifiers
[j
]);
8006 xcb_grab_key(conn
, 1,
8008 bp
->mod
| modifiers
[j
],
8009 *code
, XCB_GRAB_MODE_ASYNC
,
8010 XCB_GRAB_MODE_SYNC
);
8022 int num_screens
, i
, k
;
8023 uint16_t modifiers
[4];
8025 DNPRINTF(SWM_D_MOUSE
, "grabbuttons\n");
8026 updatenumlockmask();
8029 modifiers
[1] = numlockmask
;
8030 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8031 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8033 num_screens
= get_screen_count();
8034 for (k
= 0; k
< num_screens
; k
++) {
8035 if (TAILQ_EMPTY(&screens
[k
].rl
))
8037 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, screens
[k
].root
,
8039 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8040 if (bp
->type
!= BTNBIND
)
8043 /* If there is a catch-all, only bind that. */
8044 if ((binding_lookup(ANYMOD
, BTNBIND
, bp
->value
)) &&
8048 /* Skip unused ws binds. */
8049 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8050 bp
->action
<= FN_WS_22
)
8053 /* Skip unused mvws binds. */
8054 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8055 bp
->action
<= FN_MVWS_22
)
8058 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8059 /* All modifiers are grabbed in one pass. */
8060 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: grab, "
8061 "button: %u, modifiers: %d\n", bp
->value
,
8063 xcb_grab_button(conn
, 0, screens
[k
].root
,
8064 BUTTONMASK
, XCB_GRAB_MODE_SYNC
,
8065 XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
8066 XCB_CURSOR_NONE
, bp
->value
, bp
->mod
);
8068 /* Need to grab each modifier permutation. */
8069 for (i
= 0; i
< LENGTH(modifiers
); ++i
) {
8070 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: "
8071 "grab, button: %u, modifiers: %u\n",
8072 bp
->value
, bp
->mod
| modifiers
[i
]);
8073 xcb_grab_button(conn
, 0,
8074 screens
[k
].root
, BUTTONMASK
,
8076 XCB_GRAB_MODE_ASYNC
,
8078 XCB_CURSOR_NONE
, bp
->value
,
8079 bp
->mod
| modifiers
[i
]);
8086 const char *quirkname
[] = {
8087 "NONE", /* config string for "no value" */
8095 "FOCUSONMAP_SINGLE",
8103 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
8104 #define SWM_Q_DELIM "\n|+ \t"
8106 parsequirks(const char *qstr
, uint32_t *quirk
, int *ws
)
8108 char *str
, *cp
, *name
;
8111 if (quirk
== NULL
|| qstr
== NULL
)
8114 if ((cp
= str
= strdup(qstr
)) == NULL
)
8115 err(1, "parsequirks: strdup");
8118 while ((name
= strsep(&cp
, SWM_Q_DELIM
)) != NULL
) {
8120 cp
+= (long)strspn(cp
, SWM_Q_DELIM
);
8122 if (sscanf(name
, "WS[%d]", ws
) == 1) {
8128 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
8129 if (strncasecmp(name
, quirkname
[i
],
8130 SWM_QUIRK_LEN
) == 0) {
8131 DNPRINTF(SWM_D_QUIRK
,
8132 "parsequirks: %s\n", name
);
8138 *quirk
|= 1 << (i
-1);
8142 if (i
>= LENGTH(quirkname
)) {
8143 DNPRINTF(SWM_D_QUIRK
,
8144 "parsequirks: invalid quirk [%s]\n", name
);
8155 quirk_insert(const char *class, const char *instance
, const char *name
,
8156 uint32_t quirk
, int ws
)
8160 bool failed
= false;
8162 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: class: %s, instance: %s, name: %s,"
8163 " value: %u, ws: %d\n", class, instance
, name
, quirk
, ws
);
8165 if ((qp
= malloc(sizeof *qp
)) == NULL
)
8166 err(1, "quirk_insert: malloc");
8168 if ((qp
->class = strdup(class)) == NULL
)
8169 err(1, "quirk_insert: strdup");
8170 if ((qp
->instance
= strdup(instance
)) == NULL
)
8171 err(1, "quirk_insert: strdup");
8172 if ((qp
->name
= strdup(name
)) == NULL
)
8173 err(1, "quirk_insert: strdup");
8175 if (asprintf(&str
, "^%s$", class) == -1)
8176 err(1, "quirk_insert: asprintf");
8177 if (regcomp(&qp
->regex_class
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8178 add_startup_exception("regex failed to compile quirk 'class' "
8179 "field: %s", class);
8182 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8185 if (asprintf(&str
, "^%s$", instance
) == -1)
8186 err(1, "quirk_insert: asprintf");
8187 if (regcomp(&qp
->regex_instance
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8188 add_startup_exception("regex failed to compile quirk 'instance'"
8189 " field: %s", instance
);
8192 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8195 if (asprintf(&str
, "^%s$", name
) == -1)
8196 err(1, "quirk_insert: asprintf");
8197 if (regcomp(&qp
->regex_name
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8198 add_startup_exception("regex failed to compile quirk 'name' "
8202 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8206 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: regex error; skipping.\n");
8211 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
8213 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
8217 quirk_remove(struct quirk
*qp
)
8219 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%u]\n", qp
->class,
8220 qp
->name
, qp
->quirk
);
8222 TAILQ_REMOVE(&quirks
, qp
, entry
);
8225 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
8229 quirk_free(struct quirk
*qp
)
8231 regfree(&qp
->regex_class
);
8232 regfree(&qp
->regex_instance
);
8233 regfree(&qp
->regex_name
);
8245 while ((qp
= TAILQ_FIRST(&quirks
)) != NULL
) {
8251 quirk_replace(struct quirk
*qp
, const char *class, const char *instance
,
8252 const char *name
, uint32_t quirk
, int ws
)
8254 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp
->class,
8255 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
8258 quirk_insert(class, instance
, name
, quirk
, ws
);
8260 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
8264 setquirk(const char *class, const char *instance
, const char *name
,
8265 uint32_t quirk
, int ws
)
8269 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
8270 instance
, name
, quirk
, ws
);
8272 /* Remove/replace existing quirk. */
8273 TAILQ_FOREACH(qp
, &quirks
, entry
) {
8274 if (strcmp(qp
->class, class) == 0 &&
8275 strcmp(qp
->instance
, instance
) == 0 &&
8276 strcmp(qp
->name
, name
) == 0) {
8277 if (quirk
== 0 && ws
== -1)
8280 quirk_replace(qp
, class, instance
, name
, quirk
,
8282 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8287 /* Only insert if quirk is not NONE or forced ws is set. */
8288 if (quirk
|| ws
!= -1)
8289 quirk_insert(class, instance
, name
, quirk
, ws
);
8291 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8294 /* Eat '\' in str used to escape square brackets and colon. */
8296 unescape_selector(char *str
)
8300 for (cp
= str
; *str
!= '\0'; ++str
, ++cp
) {
8301 if (*str
== '\\' && (*(str
+ 1) == ':' || *(str
+ 1) == ']' ||
8311 setconfquirk(const char *selector
, const char *value
, int flags
)
8313 char *str
, *cp
, *class;
8314 char *instance
= NULL
, *name
= NULL
;
8315 int retval
, count
= 0, ws
= -1;
8318 /* suppress unused warning since var is needed */
8321 if (selector
== NULL
|| strlen(selector
) == 0)
8324 if ((str
= strdup(selector
)) == NULL
)
8325 err(1, "setconfquirk: strdup");
8327 /* Find non-escaped colon. */
8334 for (++cp
; *cp
!= '\0'; ++cp
) {
8335 if (*cp
== ':' && *(cp
- 1) != '\\') {
8341 unescape_selector(class);
8343 instance
= class + strlen(class) + 1;
8344 unescape_selector(instance
);
8350 name
= instance
+ strlen(instance
) + 1;
8351 unescape_selector(name
);
8356 DNPRINTF(SWM_D_CONF
, "setconfquirk: class: %s, instance: %s, "
8357 "name: %s\n", class, instance
, name
);
8359 if ((retval
= parsequirks(value
, &qrks
, &ws
)) == 0)
8360 setquirk(class, instance
, name
, qrks
, ws
);
8369 setquirk("MPlayer", "xv", ".*",
8370 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
, -1);
8371 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
8373 setquirk("Firefox-bin", "firefox-bin", ".*",
8375 setquirk("Firefox", "Dialog", ".*",
8377 setquirk("Gimp", "gimp", ".*",
8378 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8379 setquirk("XTerm", "xterm", ".*",
8380 SWM_Q_XTERM_FONTADJ
, -1);
8381 setquirk("xine", "Xine Window", ".*",
8382 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8383 setquirk("Xitk", "Xitk Combo", ".*",
8384 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8385 setquirk("xine", "xine Panel", ".*",
8386 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8387 setquirk("Xitk", "Xine Window", ".*",
8388 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8389 setquirk("xine", "xine Video Fullscreen Window", ".*",
8390 SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
, -1);
8391 setquirk("pcb", "pcb", ".*",
8393 setquirk("SDL_App", "SDL_App", ".*",
8394 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
, -1);
8397 /* conf file stuff */
8398 #define SWM_CONF_FILE "spectrwm.conf"
8399 #define SWM_CONF_FILE_OLD "scrotwm.conf"
8403 SWM_S_BAR_AT_BOTTOM
,
8404 SWM_S_BAR_BORDER_WIDTH
,
8407 SWM_S_BAR_ENABLED_WS
,
8412 SWM_S_BOUNDARY_WIDTH
,
8413 SWM_S_CLOCK_ENABLED
,
8416 SWM_S_CYCLE_VISIBLE
,
8418 SWM_S_DISABLE_BORDER
,
8420 SWM_S_FOCUS_CLOSE_WRAP
,
8421 SWM_S_FOCUS_DEFAULT
,
8423 SWM_S_ICONIC_ENABLED
,
8424 SWM_S_JAVA_WORKAROUND
,
8425 SWM_S_MAXIMIZE_HIDE_BAR
,
8426 SWM_S_REGION_PADDING
,
8431 SWM_S_STACK_ENABLED
,
8434 SWM_S_URGENT_COLLAPSE
,
8435 SWM_S_URGENT_ENABLED
,
8436 SWM_S_VERBOSE_LAYOUT
,
8439 SWM_S_WINDOW_CLASS_ENABLED
,
8440 SWM_S_WINDOW_INSTANCE_ENABLED
,
8441 SWM_S_WINDOW_NAME_ENABLED
,
8442 SWM_S_WORKSPACE_CLAMP
,
8443 SWM_S_WORKSPACE_LIMIT
,
8444 SWM_S_WORKSPACE_NAME
,
8448 setconfvalue(const char *selector
, const char *value
, int flags
)
8450 struct workspace
*ws
;
8451 int i
, ws_id
, num_screens
;
8452 char *b
, *str
, *sp
, s
[1024];
8455 case SWM_S_BAR_ACTION
:
8457 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
8458 err(1, "setconfvalue: bar_action");
8460 case SWM_S_BAR_AT_BOTTOM
:
8461 bar_at_bottom
= (atoi(value
) != 0);
8463 case SWM_S_BAR_BORDER_WIDTH
:
8464 bar_border_width
= atoi(value
);
8465 if (bar_border_width
< 0)
8466 bar_border_width
= 0;
8468 case SWM_S_BAR_DELAY
:
8469 /* No longer needed; leave to not break old conf files. */
8471 case SWM_S_BAR_ENABLED
:
8472 bar_enabled
= (atoi(value
) != 0);
8474 case SWM_S_BAR_ENABLED_WS
:
8475 ws_id
= atoi(selector
) - 1;
8476 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8477 errx(1, "setconfvalue: bar_enabled_ws: invalid "
8478 "workspace %d.", ws_id
+ 1);
8480 num_screens
= get_screen_count();
8481 for (i
= 0; i
< num_screens
; i
++) {
8482 ws
= (struct workspace
*)&screens
[i
].ws
;
8483 ws
[ws_id
].bar_enabled
= (atoi(value
) != 0);
8486 case SWM_S_BAR_FONT
:
8488 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
8489 err(1, "setconfvalue: asprintf: failed to allocate "
8490 "memory for bar_fonts.");
8493 /* If already in xft mode, then we are done. */
8494 if (!bar_font_legacy
)
8497 if ((sp
= str
= strdup(value
)) == NULL
)
8498 err(1, "setconfvalue: strdup");
8500 /* If there are any non-XLFD entries, switch to Xft mode. */
8501 while ((b
= strsep(&sp
, ",")) != NULL
) {
8505 bar_font_legacy
= false;
8512 case SWM_S_BAR_FORMAT
:
8514 if ((bar_format
= strdup(value
)) == NULL
)
8515 err(1, "setconfvalue: bar_format");
8517 case SWM_S_BAR_JUSTIFY
:
8518 if (strcmp(value
, "left") == 0)
8519 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
8520 else if (strcmp(value
, "center") == 0)
8521 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
8522 else if (strcmp(value
, "right") == 0)
8523 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
8525 errx(1, "invalid bar_justify");
8527 case SWM_S_BORDER_WIDTH
:
8528 border_width
= atoi(value
);
8529 if (border_width
< 0)
8532 case SWM_S_BOUNDARY_WIDTH
:
8533 boundary_width
= atoi(value
);
8534 if (boundary_width
< 0)
8537 case SWM_S_CLOCK_ENABLED
:
8538 clock_enabled
= (atoi(value
) != 0);
8540 case SWM_S_CLOCK_FORMAT
:
8541 #ifndef SWM_DENY_CLOCK_FORMAT
8543 if ((clock_format
= strdup(value
)) == NULL
)
8544 err(1, "setconfvalue: clock_format");
8547 case SWM_S_CYCLE_EMPTY
:
8548 cycle_empty
= (atoi(value
) != 0);
8550 case SWM_S_CYCLE_VISIBLE
:
8551 cycle_visible
= (atoi(value
) != 0);
8553 case SWM_S_DIALOG_RATIO
:
8554 dialog_ratio
= atof(value
);
8555 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
8558 case SWM_S_DISABLE_BORDER
:
8559 disable_border
= (atoi(value
) != 0);
8561 case SWM_S_FOCUS_CLOSE
:
8562 if (strcmp(value
, "first") == 0)
8563 focus_close
= SWM_STACK_BOTTOM
;
8564 else if (strcmp(value
, "last") == 0)
8565 focus_close
= SWM_STACK_TOP
;
8566 else if (strcmp(value
, "next") == 0)
8567 focus_close
= SWM_STACK_ABOVE
;
8568 else if (strcmp(value
, "previous") == 0)
8569 focus_close
= SWM_STACK_BELOW
;
8571 errx(1, "focus_close");
8573 case SWM_S_FOCUS_CLOSE_WRAP
:
8574 focus_close_wrap
= (atoi(value
) != 0);
8576 case SWM_S_FOCUS_DEFAULT
:
8577 if (strcmp(value
, "last") == 0)
8578 focus_default
= SWM_STACK_TOP
;
8579 else if (strcmp(value
, "first") == 0)
8580 focus_default
= SWM_STACK_BOTTOM
;
8582 errx(1, "focus_default");
8584 case SWM_S_FOCUS_MODE
:
8585 if (strcmp(value
, "default") == 0)
8586 focus_mode
= SWM_FOCUS_DEFAULT
;
8587 else if (strcmp(value
, "follow") == 0 ||
8588 strcmp(value
, "follow_cursor") == 0)
8589 focus_mode
= SWM_FOCUS_FOLLOW
;
8590 else if (strcmp(value
, "manual") == 0)
8591 focus_mode
= SWM_FOCUS_MANUAL
;
8593 errx(1, "focus_mode");
8595 case SWM_S_ICONIC_ENABLED
:
8596 iconic_enabled
= (atoi(value
) != 0);
8598 case SWM_S_JAVA_WORKAROUND
:
8599 java_workaround
= (atoi(value
) != 0);
8601 case SWM_S_MAXIMIZE_HIDE_BAR
:
8602 maximize_hide_bar
= atoi(value
);
8604 case SWM_S_REGION_PADDING
:
8605 region_padding
= atoi(value
);
8606 if (region_padding
< 0)
8609 case SWM_S_SPAWN_ORDER
:
8610 if (strcmp(value
, "first") == 0)
8611 spawn_position
= SWM_STACK_BOTTOM
;
8612 else if (strcmp(value
, "last") == 0)
8613 spawn_position
= SWM_STACK_TOP
;
8614 else if (strcmp(value
, "next") == 0)
8615 spawn_position
= SWM_STACK_ABOVE
;
8616 else if (strcmp(value
, "previous") == 0)
8617 spawn_position
= SWM_STACK_BELOW
;
8619 errx(1, "spawn_position");
8621 case SWM_S_SPAWN_TERM
:
8622 setconfspawn("term", value
, 0);
8623 setconfspawn("spawn_term", value
, 0);
8626 /* No longer needed; leave to not break old conf files. */
8628 case SWM_S_SS_ENABLED
:
8629 /* No longer needed; leave to not break old conf files. */
8631 case SWM_S_STACK_ENABLED
:
8632 stack_enabled
= (atoi(value
) != 0);
8634 case SWM_S_TERM_WIDTH
:
8635 term_width
= atoi(value
);
8639 case SWM_S_TILE_GAP
:
8640 tile_gap
= atoi(value
);
8642 case SWM_S_URGENT_COLLAPSE
:
8643 urgent_collapse
= (atoi(value
) != 0);
8645 case SWM_S_URGENT_ENABLED
:
8646 urgent_enabled
= (atoi(value
) != 0);
8648 case SWM_S_VERBOSE_LAYOUT
:
8649 verbose_layout
= (atoi(value
) != 0);
8650 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
8652 layouts
[i
].l_string
= fancy_stacker
;
8654 layouts
[i
].l_string
= plain_stacker
;
8657 case SWM_S_WARP_FOCUS
:
8658 warp_focus
= (atoi(value
) != 0);
8660 case SWM_S_WARP_POINTER
:
8661 warp_pointer
= (atoi(value
) != 0);
8663 case SWM_S_WINDOW_CLASS_ENABLED
:
8664 window_class_enabled
= (atoi(value
) != 0);
8666 case SWM_S_WINDOW_INSTANCE_ENABLED
:
8667 window_instance_enabled
= (atoi(value
) != 0);
8669 case SWM_S_WINDOW_NAME_ENABLED
:
8670 window_name_enabled
= (atoi(value
) != 0);
8672 case SWM_S_WORKSPACE_CLAMP
:
8673 workspace_clamp
= (atoi(value
) != 0);
8675 case SWM_S_WORKSPACE_LIMIT
:
8676 workspace_limit
= atoi(value
);
8677 if (workspace_limit
> SWM_WS_MAX
)
8678 workspace_limit
= SWM_WS_MAX
;
8679 else if (workspace_limit
< 1)
8680 workspace_limit
= 1;
8682 ewmh_update_desktops();
8684 case SWM_S_WORKSPACE_NAME
:
8685 if (getenv("SWM_STARTED") != NULL
)
8689 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
8690 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
8692 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8693 errx(1, "setconfvalue: workspace_name: invalid "
8694 "workspace %d.", ws_id
+ 1);
8696 num_screens
= get_screen_count();
8697 for (i
= 0; i
< num_screens
; ++i
) {
8698 ws
= (struct workspace
*)&screens
[i
].ws
;
8700 if (strlen(s
) > 0) {
8701 free(ws
[ws_id
].name
);
8702 if ((ws
[ws_id
].name
= strdup(s
)) == NULL
)
8703 err(1, "setconfvalue: workspace_name.");
8705 ewmh_update_desktop_names();
8706 ewmh_get_desktop_names();
8717 setconfmodkey(const char *selector
, const char *value
, int flags
)
8719 /* suppress unused warnings since vars are needed */
8723 if (strncasecmp(value
, "Mod1", strlen("Mod1")) == 0)
8724 update_modkey(XCB_MOD_MASK_1
);
8725 else if (strncasecmp(value
, "Mod2", strlen("Mod2")) == 0)
8726 update_modkey(XCB_MOD_MASK_2
);
8727 else if (strncasecmp(value
, "Mod3", strlen("Mod3")) == 0)
8728 update_modkey(XCB_MOD_MASK_3
);
8729 else if (strncasecmp(value
, "Mod4", strlen("Mod4")) == 0)
8730 update_modkey(XCB_MOD_MASK_4
);
8731 else if (strncasecmp(value
, "Mod5", strlen("Mod5")) == 0)
8732 update_modkey(XCB_MOD_MASK_5
);
8739 setconfcolor(const char *selector
, const char *value
, int flags
)
8741 int first
, last
, i
= 0, num_screens
;
8743 num_screens
= get_screen_count();
8745 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
8746 if (selector
== NULL
|| strlen(selector
) == 0 ||
8747 (last
= atoi(selector
) - 1) < 0) {
8749 last
= num_screens
- 1;
8754 if (last
>= num_screens
) {
8755 add_startup_exception("invalid screen index: %d out of bounds "
8756 "(maximum %d)", last
+ 1, num_screens
);
8760 for (i
= first
; i
<= last
; ++i
) {
8761 setscreencolor(value
, i
, flags
);
8764 * When setting focus/unfocus colors, we need to also
8765 * set maximize colors to match if they haven't been customized.
8767 if (flags
== SWM_S_COLOR_FOCUS
&&
8768 !screens
[i
].c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].manual
)
8769 setscreencolor(value
, i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
8770 else if (flags
== SWM_S_COLOR_UNFOCUS
&&
8771 !screens
[i
].c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].manual
)
8772 setscreencolor(value
, i
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
8774 screens
[i
].c
[flags
].manual
= 1;
8781 setconfregion(const char *selector
, const char *value
, int flags
)
8783 /* suppress unused warnings since vars are needed */
8787 custom_region(value
);
8792 setautorun(const char *selector
, const char *value
, int flags
)
8796 char *ap
, *sp
, *str
;
8802 /* suppress unused warnings since vars are needed */
8806 if (getenv("SWM_STARTED"))
8810 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
8811 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
8813 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8814 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
8816 sp
= str
= expand_tilde((char *)&s
);
8819 * This is a little intricate
8821 * If the pid already exists we simply reuse it because it means it was
8822 * used before AND not claimed by manage_window. We get away with
8823 * altering it in the parent after INSERT because this can not be a race
8826 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
8829 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
8831 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
8832 err(1, "setautorun: realloc");
8833 a
.argv
[argc
- 1] = ap
;
8836 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
8837 err(1, "setautorun: realloc");
8838 a
.argv
[argc
] = NULL
;
8840 if ((pid
= fork()) == 0) {
8841 spawn(ws_id
, &a
, true);
8851 p
= calloc(1, sizeof *p
);
8854 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
8864 setlayout(const char *selector
, const char *value
, int flags
)
8866 struct workspace
*ws
;
8867 int ws_id
, i
, x
, mg
, ma
, si
, ar
;
8868 int st
= SWM_V_STACK
, num_screens
;
8872 /* suppress unused warnings since vars are needed */
8876 if (getenv("SWM_STARTED"))
8880 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
8881 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
8882 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8883 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8886 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8887 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
8889 if (strcasecmp(s
, "vertical") == 0)
8891 else if (strcasecmp(s
, "vertical_flip") == 0) {
8894 } else if (strcasecmp(s
, "horizontal") == 0)
8896 else if (strcasecmp(s
, "horizontal_flip") == 0) {
8899 } else if (strcasecmp(s
, "fullscreen") == 0)
8902 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8903 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8906 num_screens
= get_screen_count();
8907 for (i
= 0; i
< num_screens
; i
++) {
8908 ws
= (struct workspace
*)&screens
[i
].ws
;
8909 ws
[ws_id
].cur_layout
= &layouts
[st
];
8911 ws
[ws_id
].always_raise
= (ar
!= 0);
8912 if (st
== SWM_MAX_STACK
)
8916 for (x
= 0; x
< abs(mg
); x
++) {
8917 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8918 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
8919 SWM_ARG_ID_MASTERSHRINK
);
8922 for (x
= 0; x
< abs(ma
); x
++) {
8923 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8924 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
8925 SWM_ARG_ID_MASTERDEL
);
8928 for (x
= 0; x
< abs(si
); x
++) {
8929 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8930 si
>= 0 ? SWM_ARG_ID_STACKINC
:
8931 SWM_ARG_ID_STACKDEC
);
8935 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8936 SWM_ARG_ID_FLIPLAYOUT
);
8943 /* config options */
8944 struct config_option
{
8946 int (*func
)(const char*, const char*, int);
8949 struct config_option configopt
[] = {
8950 { "autorun", setautorun
, 0 },
8951 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
8952 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
8953 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
8954 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
8955 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
8956 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
8957 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
8958 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
8959 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
8960 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
8961 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
8962 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
8963 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
8964 { "bind", setconfbinding
, 0 },
8965 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
8966 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
8967 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
8968 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
8969 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
8970 { "color_focus_maximized", setconfcolor
, SWM_S_COLOR_FOCUS_MAXIMIZED
},
8971 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
8972 { "color_unfocus_maximized", setconfcolor
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
},
8973 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
8974 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
8975 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
8976 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
8977 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
8978 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
8979 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
8980 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
8981 { "iconic_enabled", setconfvalue
, SWM_S_ICONIC_ENABLED
},
8982 { "java_workaround", setconfvalue
, SWM_S_JAVA_WORKAROUND
},
8983 { "keyboard_mapping", setkeymapping
, 0 },
8984 { "layout", setlayout
, 0 },
8985 { "maximize_hide_bar", setconfvalue
, SWM_S_MAXIMIZE_HIDE_BAR
},
8986 { "modkey", setconfmodkey
, 0 },
8987 { "program", setconfspawn
, 0 },
8988 { "quirk", setconfquirk
, 0 },
8989 { "region", setconfregion
, 0 },
8990 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
8991 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
8992 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
8993 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
8994 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
8995 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
8996 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
8997 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
8998 { "title_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
}, /* For backwards compat. */
8999 { "title_name_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
}, /* For backwards compat. */
9000 { "urgent_collapse", setconfvalue
, SWM_S_URGENT_COLLAPSE
},
9001 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
9002 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
9003 { "warp_focus", setconfvalue
, SWM_S_WARP_FOCUS
},
9004 { "warp_pointer", setconfvalue
, SWM_S_WARP_POINTER
},
9005 { "window_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
},
9006 { "window_instance_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
},
9007 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
9008 { "workspace_clamp", setconfvalue
, SWM_S_WORKSPACE_CLAMP
},
9009 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
9010 { "name", setconfvalue
, SWM_S_WORKSPACE_NAME
},
9014 _add_startup_exception(const char *fmt
, va_list ap
)
9016 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
9017 warn("%s: asprintf", __func__
);
9021 add_startup_exception(const char *fmt
, ...)
9027 if (startup_exception
)
9030 /* force bar to be enabled due to exception */
9034 _add_startup_exception(fmt
, ap
);
9039 conf_load(const char *filename
, int keymapping
)
9042 char *line
= NULL
, *cp
, *ce
, *optsub
, *optval
= NULL
;
9043 size_t linelen
, lineno
= 0;
9044 int wordlen
, i
, optidx
, count
;
9045 struct config_option
*opt
= NULL
;
9047 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
9049 if (filename
== NULL
) {
9050 warnx("conf_load: no filename");
9054 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
9056 if ((config
= fopen(filename
, "r")) == NULL
) {
9057 warn("conf_load: fopen: %s", filename
);
9061 while (!feof(config
)) {
9065 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
9066 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
9068 err(1, "%s", filename
);
9073 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9074 if (cp
[0] == '\0') {
9078 /* get config option */
9079 wordlen
= strcspn(cp
, "=[ \t\n");
9081 add_startup_exception("%s: line %zd: no option found",
9086 for (i
= 0; i
< LENGTH(configopt
); i
++) {
9087 opt
= &configopt
[i
];
9088 if (strncasecmp(cp
, opt
->optname
, wordlen
) == 0 &&
9089 (int)strlen(opt
->optname
) == wordlen
) {
9095 add_startup_exception("%s: line %zd: unknown option "
9096 "%.*s", filename
, lineno
, wordlen
, cp
);
9099 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
9100 add_startup_exception("%s: line %zd: invalid option "
9101 "%.*s", filename
, lineno
, wordlen
, cp
);
9105 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9107 /* get [selector] if any */
9112 /* Get length of selector. */
9113 for (ce
= ++cp
; *ce
!= '\0'; ++ce
) {
9114 /* Find matching (not escaped) bracket. */
9115 if (*ce
== ']' && *(ce
- 1) != '\\') {
9122 add_startup_exception("%s: line %zd: syntax "
9123 "error: unterminated selector", filename
,
9128 /* ce points at ']'; terminate optsub. */
9133 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
9136 if (strlen(optval
) == 0) {
9137 add_startup_exception("%s: line %zd: must supply value "
9138 "to %s", filename
, lineno
,
9139 configopt
[optidx
].optname
);
9142 /* call function to deal with it all */
9143 if (configopt
[optidx
].func(optsub
, optval
,
9144 configopt
[optidx
].funcflags
) != 0) {
9145 add_startup_exception("%s: line %zd: invalid data for "
9146 "%s", filename
, lineno
, configopt
[optidx
].optname
);
9155 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
9161 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
9163 struct ws_win
*parent
, *w
;
9164 struct swm_region
*r
;
9165 struct workspace
*ws
;
9166 xcb_icccm_wm_hints_t wmh
;
9168 parent
= find_window(win
->transient
);
9170 parent
->focus_child
= win
;
9172 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
9173 " for %#x trans %#x\n", win
->id
, win
->transient
);
9175 r
= root_to_region(win
->s
->root
, SWM_CK_ALL
);
9177 /* parent doen't exist in our window list */
9178 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
9179 if (xcb_icccm_get_wm_hints_reply(conn
,
9180 xcb_icccm_get_wm_hints(conn
, w
->id
),
9182 warnx("can't get hints for %#x", w
->id
);
9186 if (win
->hints
.window_group
!= wmh
.window_group
)
9189 w
->focus_child
= win
;
9190 win
->transient
= w
->id
;
9192 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
9193 "transient to %#x\n", win
->transient
);
9200 window_get_pid(xcb_window_t win
)
9205 xcb_get_property_cookie_t pc
;
9206 xcb_get_property_reply_t
*pr
;
9208 apid
= get_atom_from_string("_NET_WM_PID");
9209 if (apid
== XCB_ATOM_NONE
)
9212 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
9213 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9216 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
9221 if (pr
->type
== apid
&& pr
->format
== 32)
9222 ret
= *((pid_t
*)xcb_get_property_value(pr
));
9228 apid
= get_atom_from_string("_SWM_PID");
9229 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
9231 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9234 if (pr
->type
!= apid
) {
9239 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
9246 get_swm_ws(xcb_window_t id
)
9252 xcb_get_property_reply_t
*gpr
;
9254 gpr
= xcb_get_property_reply(conn
,
9255 xcb_get_property(conn
, 0, id
, a_swm_ws
,
9256 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
9261 proplen
= xcb_get_property_value_length(gpr
);
9263 prop
= malloc(proplen
+ 1);
9266 xcb_get_property_value(gpr
),
9268 prop
[proplen
] = '\0';
9275 DNPRINTF(SWM_D_PROP
, "get_swm_ws: _SWM_WS: %s\n", prop
);
9276 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
9278 DNPRINTF(SWM_D_PROP
, "get_swm_ws: win #%s: %s",
9288 get_ws_idx(struct ws_win
*win
)
9290 xcb_get_property_reply_t
*gpr
;
9296 gpr
= xcb_get_property_reply(conn
,
9297 xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
9298 XCB_ATOM_CARDINAL
, 0, 1),
9301 if (gpr
->type
== XCB_ATOM_CARDINAL
&& gpr
->format
== 32)
9302 ws_idx
= *((int *)xcb_get_property_value(gpr
));
9306 if (ws_idx
== -1 && !(win
->quirks
& SWM_Q_IGNORESPAWNWS
))
9307 ws_idx
= get_swm_ws(win
->id
);
9309 if (ws_idx
> workspace_limit
- 1 || ws_idx
< -1)
9312 DNPRINTF(SWM_D_PROP
, "get_ws_idx: win %#x, ws_idx: %d\n", win
->id
,
9319 manage_window(xcb_window_t id
, int spawn_pos
, bool mapped
)
9321 struct ws_win
*win
, *ww
;
9322 struct swm_region
*r
;
9325 xcb_get_geometry_reply_t
*gr
;
9326 xcb_window_t trans
= XCB_WINDOW_NONE
;
9327 uint32_t i
, wa
[2], new_flags
;
9328 int ws_idx
, force_ws
= -1;
9329 char *class, *instance
, *name
;
9331 if ((win
= find_window(id
)) != NULL
) {
9332 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x already "
9333 "managed; skipping.)\n", id
);
9334 return (win
); /* Already managed. */
9337 /* See if window is on the unmanaged list. */
9338 if ((win
= find_unmanaged_window(id
)) != NULL
) {
9339 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x found on "
9340 "unmanaged list.\n", id
);
9341 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
9344 set_child_transient(win
, &trans
);
9348 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is new.\n", id
);
9351 /* Try to get initial window geometry. */
9352 gr
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, id
), NULL
);
9354 DNPRINTF(SWM_D_MISC
, "manage_window: get geometry failed.\n");
9358 /* Create and initialize ws_win object. */
9359 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
9360 err(1, "manage_window: calloc: failed to allocate memory for "
9365 /* Figureout which region the window belongs to. */
9366 r
= root_to_region(gr
->root
, SWM_CK_ALL
);
9368 /* Ignore window border if there is one. */
9369 WIDTH(win
) = gr
->width
;
9370 HEIGHT(win
) = gr
->height
;
9371 X(win
) = gr
->x
+ gr
->border_width
- border_width
;
9372 Y(win
) = gr
->y
+ gr
->border_width
- border_width
;
9373 win
->bordered
= true;
9374 win
->mapped
= mapped
;
9375 win
->s
= r
->s
; /* this never changes */
9379 /* Select which X events to monitor and set border pixel color. */
9380 wa
[0] = win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
9381 wa
[1] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
9382 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
9384 wa
[1] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
9387 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
|
9388 XCB_CW_EVENT_MASK
, wa
);
9390 /* Get WM_SIZE_HINTS. */
9391 xcb_icccm_get_wm_normal_hints_reply(conn
,
9392 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
9396 xcb_icccm_get_wm_hints_reply(conn
,
9397 xcb_icccm_get_wm_hints(conn
, win
->id
),
9400 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
9401 xcb_icccm_get_wm_transient_for_reply(conn
,
9402 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
9405 win
->transient
= trans
;
9406 set_child_transient(win
, &win
->transient
);
9409 /* Get WM_PROTOCOLS. */
9410 get_wm_protocols(win
);
9413 /* Must be after getting WM_HINTS and WM_PROTOCOLS. */
9414 print_win_input_model(win
);
9417 /* Set initial quirks based on EWMH. */
9418 ewmh_autoquirk(win
);
9420 /* Determine initial quirks. */
9421 xcb_icccm_get_wm_class_reply(conn
,
9422 xcb_icccm_get_wm_class(conn
, win
->id
),
9425 class = win
->ch
.class_name
? win
->ch
.class_name
: "";
9426 instance
= win
->ch
.instance_name
? win
->ch
.instance_name
: "";
9427 name
= get_win_name(win
->id
);
9429 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, instance: %s, "
9430 "name: %s\n", class, instance
, name
);
9432 /* java is retarded so treat it special */
9433 if (strstr(instance
, "sun-awt")) {
9434 DNPRINTF(SWM_D_CLASS
, "manage_window: java window detected.\n");
9438 TAILQ_FOREACH(qp
, &quirks
, entry
) {
9439 if (regexec(&qp
->regex_class
, class, 0, NULL
, 0) == 0 &&
9440 regexec(&qp
->regex_instance
, instance
, 0, NULL
, 0) == 0 &&
9441 regexec(&qp
->regex_name
, name
, 0, NULL
, 0) == 0) {
9442 DNPRINTF(SWM_D_CLASS
, "manage_window: matched "
9443 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp
->class,
9444 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
9445 win
->quirks
= qp
->quirk
;
9446 if (qp
->ws
>= 0 && qp
->ws
< workspace_limit
)
9453 /* Reset font sizes (the bruteforce way; no default keybinding). */
9454 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
9455 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9456 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
9457 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9458 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
9461 /* Figure out which workspace the window belongs to. */
9462 if (!(win
->quirks
& SWM_Q_IGNOREPID
) &&
9463 (p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
9464 win
->ws
= &r
->s
->ws
[p
->ws
];
9465 TAILQ_REMOVE(&pidlist
, p
, entry
);
9468 } else if ((ws_idx
= get_ws_idx(win
)) != -1 &&
9470 /* _SWM_WS is set; use that. */
9471 win
->ws
= &r
->s
->ws
[ws_idx
];
9472 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
9473 /* Launch transients in the same ws as parent. */
9480 win
->ws
= &r
->s
->ws
[force_ws
];
9482 /* Set the _NET_WM_DESKTOP atom. */
9483 DNPRINTF(SWM_D_PROP
, "manage_window: set _NET_WM_DESKTOP: %d\n",
9485 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
9486 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &win
->ws
->idx
);
9488 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
9489 xcb_delete_property(conn
, win
->id
, a_swm_ws
);
9491 /* WS must already be set for this to work. */
9492 store_float_geom(win
);
9494 /* Make sure window is positioned inside its region, if its active. */
9496 region_containment(win
, r
, SWM_CW_ALLSIDES
|
9497 SWM_CW_HARDBOUNDARY
);
9502 /* Figure out where to insert the window in the workspace list. */
9503 if (trans
&& (ww
= find_window(trans
)))
9504 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
9505 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_ABOVE
)
9506 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
9508 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_BELOW
)
9509 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
9510 else switch (spawn_pos
) {
9513 case SWM_STACK_ABOVE
:
9514 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
9516 case SWM_STACK_BOTTOM
:
9517 case SWM_STACK_BELOW
:
9518 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
9521 ewmh_update_client_list();
9523 TAILQ_INSERT_HEAD(&win
->ws
->stack
, win
, stack_entry
);
9526 /* Get/apply initial _NET_WM_STATE */
9527 ewmh_get_wm_state(win
);
9530 new_flags
= win
->ewmh_flags
;
9532 if (win
->quirks
& SWM_Q_FLOAT
)
9533 new_flags
|= EWMH_F_ABOVE
;
9535 if (win
->quirks
& SWM_Q_ANYWHERE
)
9536 new_flags
|= SWM_F_MANUAL
;
9538 ewmh_apply_flags(win
, new_flags
);
9539 ewmh_update_wm_state(win
);
9541 /* Set initial _NET_WM_ALLOWED_ACTIONS */
9542 ewmh_update_actions(win
);
9544 DNPRINTF(SWM_D_MISC
, "manage_window: done. win %#x, (x,y) w x h: "
9545 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win
->id
,
9546 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
9547 YESNO(ICONIC(win
)), win
->transient
);
9553 free_window(struct ws_win
*win
)
9555 DNPRINTF(SWM_D_MISC
, "free_window: win %#x\n", WINID(win
));
9560 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
9563 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
9566 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
9570 unmanage_window(struct ws_win
*win
)
9572 struct ws_win
*parent
;
9574 DNPRINTF(SWM_D_MISC
, "unmanage_window: win %#x\n", WINID(win
));
9580 parent
= find_window(win
->transient
);
9582 parent
->focus_child
= NULL
;
9585 TAILQ_REMOVE(&win
->ws
->stack
, win
, stack_entry
);
9586 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
9587 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
9589 ewmh_update_client_list();
9593 expose(xcb_expose_event_t
*e
)
9596 struct swm_region
*r
;
9598 DNPRINTF(SWM_D_EVENT
, "expose: win %#x\n", e
->window
);
9600 num_screens
= get_screen_count();
9601 for (i
= 0; i
< num_screens
; i
++)
9602 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9603 if (e
->window
== WINID(r
->bar
))
9611 focusin(xcb_focus_in_event_t
*e
)
9613 DNPRINTF(SWM_D_EVENT
, "focusin: win %#x, mode: %s(%u), "
9614 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
9615 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
9619 focusout(xcb_focus_out_event_t
*e
)
9621 DNPRINTF(SWM_D_EVENT
, "focusout: win %#x, mode: %s(%u), "
9622 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
9623 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
9628 keypress(xcb_key_press_event_t
*e
)
9632 xcb_keysym_t keysym
;
9635 last_event_time
= e
->time
;
9637 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
9639 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
9640 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9641 "state: %u, cleaned: %u, same_screen: %s\n", keysym
, e
->event
,
9642 e
->event_x
, e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
,
9643 e
->root_y
, e
->child
, e
->state
, CLEANMASK(e
->state
),
9644 YESNO(e
->same_screen
));
9646 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
9648 /* Look for catch-all. */
9649 if ((bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
)) == NULL
)
9653 replay
= bp
->flags
& BINDING_F_REPLAY
;
9655 if ((ap
= &actions
[bp
->action
]) == NULL
)
9658 if (bp
->action
== FN_SPAWN_CUSTOM
)
9659 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
9662 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
9664 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
9668 DNPRINTF(SWM_D_EVENT
, "keypress: replaying.\n");
9669 /* Pass keypress to event window and unfreeze keyboard queue. */
9670 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
9672 /* Release freeze. */
9673 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
9677 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
9681 keyrelease(xcb_key_release_event_t
*e
)
9685 xcb_keysym_t keysym
;
9687 last_event_time
= e
->time
;
9689 DNPRINTF(SWM_D_EVENT
, "keyrelease: keysym: %u, win (x,y): %#x (%d,%d), "
9690 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9691 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
9692 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
9693 e
->child
, e
->state
, YESNO(e
->same_screen
));
9695 keysym
= xcb_key_release_lookup_keysym(syms
, e
, 0);
9697 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
9699 /* Look for catch-all. */
9700 bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
);
9702 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
9703 bp
->flags
& BINDING_F_REPLAY
) {
9704 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
9705 DNPRINTF(SWM_D_EVENT
, "keyrelease: replaying.\n");
9707 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
9712 DNPRINTF(SWM_D_EVENT
, "keyrelease: done.\n");
9716 buttonpress(xcb_button_press_event_t
*e
)
9718 struct ws_win
*win
= NULL
;
9719 struct swm_region
*r
, *old_r
;
9724 last_event_time
= e
->time
;
9726 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): %#x (%d,%d), "
9727 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9728 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
9729 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9730 e
->state
, YESNO(e
->same_screen
));
9732 if (e
->event
== e
->root
) {
9734 win
= find_window(e
->child
);
9736 /* Focus on empty region */
9737 /* If no windows on region if its empty. */
9738 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
9739 if (r
&& TAILQ_EMPTY(&r
->ws
->winlist
)) {
9740 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
9741 if (old_r
&& old_r
!= r
)
9742 unfocus_win(old_r
->ws
->focus
);
9744 xcb_set_input_focus(conn
,
9745 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
9747 /* Clear bar since empty. */
9750 /* No need to replay event. */
9755 win
= find_window(e
->event
);
9759 focus_win(get_focus_magic(win
));
9761 /* Handle any bound action. */
9762 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
9764 /* Look for catch-all. */
9765 if ((bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
)) == NULL
)
9770 replay
= bp
->flags
& BINDING_F_REPLAY
;
9772 if ((ap
= &actions
[bp
->action
]) == NULL
)
9775 if (bp
->action
== FN_SPAWN_CUSTOM
)
9776 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
9779 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
9781 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
9785 DNPRINTF(SWM_D_EVENT
, "buttonpress: replaying.\n");
9786 /* Replay event to event window */
9787 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
9789 /* Unfreeze grab events. */
9790 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
9797 buttonrelease(xcb_button_release_event_t
*e
)
9802 last_event_time
= e
->time
;
9804 DNPRINTF(SWM_D_EVENT
, "buttonrelease: win (x,y): %#x (%d,%d), "
9805 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9806 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
9807 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9808 e
->state
, YESNO(e
->same_screen
));
9810 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
9812 /* Look for catch-all. */
9813 bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
);
9815 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
9816 bp
->flags
& BINDING_F_REPLAY
) {
9817 DNPRINTF(SWM_D_EVENT
, "buttonrelease: replaying.\n");
9818 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
9820 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
9828 print_win_input_model(struct ws_win
*win
)
9832 * Input Model Input Field WM_TAKE_FOCUS
9833 * No Input False Absent
9834 * Passive True Absent
9835 * Locally Active True Present
9836 * Globally Active False Present
9839 if (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) || win
->hints
.input
)
9840 inputmodel
= (win
->take_focus
) ? "Locally Active" : "Passive";
9842 inputmodel
= (win
->take_focus
) ? "Globally Active" : "No Input";
9844 DNPRINTF(SWM_D_FOCUS
, "print_win_input_model: win %#x, model: %s\n",
9845 win
->id
, inputmodel
);
9849 print_win_geom(xcb_window_t w
)
9851 xcb_get_geometry_reply_t
*wa
;
9853 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
9855 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: %#x\n",
9860 DNPRINTF(SWM_D_MISC
, "print_win_geom: win %#x, root: %#x, "
9861 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
9862 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
9871 get_stack_mode_name(uint8_t mode
)
9876 case XCB_STACK_MODE_ABOVE
:
9879 case XCB_STACK_MODE_BELOW
:
9882 case XCB_STACK_MODE_TOP_IF
:
9885 case XCB_STACK_MODE_BOTTOM_IF
:
9888 case XCB_STACK_MODE_OPPOSITE
:
9900 configurerequest(xcb_configure_request_event_t
*e
)
9903 struct swm_region
*r
= NULL
;
9905 uint32_t wc
[7] = {0};
9909 if ((win
= find_window(e
->window
)) == NULL
)
9910 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
9914 if (swm_debug
& SWM_D_EVENT
) {
9915 print_win_geom(e
->window
);
9917 DNPRINTF(SWM_D_EVENT
, "configurerequest: win %#x, "
9918 "parent: %#x, new: %s, value_mask: %u { ", e
->window
,
9919 e
->parent
, YESNO(new), e
->value_mask
);
9920 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
9921 DPRINTF("X: %d ", e
->x
);
9922 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
9923 DPRINTF("Y: %d ", e
->y
);
9924 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
9925 DPRINTF("W: %u ", e
->width
);
9926 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
9927 DPRINTF("H: %u ", e
->height
);
9928 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
9929 DPRINTF("Border: %u ", e
->border_width
);
9930 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
9931 DPRINTF("Sibling: %#x ", e
->sibling
);
9932 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
9933 DPRINTF("StackMode: %s(%u) ",
9934 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
9940 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
9941 mask
|= XCB_CONFIG_WINDOW_X
;
9944 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
9945 mask
|= XCB_CONFIG_WINDOW_Y
;
9948 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
9949 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
9952 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
9953 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
9954 wc
[i
++] = e
->height
;
9956 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
9957 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
9958 wc
[i
++] = e
->border_width
;
9960 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
9961 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
9962 wc
[i
++] = e
->sibling
;
9964 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
9965 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
9966 wc
[i
++] = e
->stack_mode
;
9970 xcb_configure_window(conn
, e
->window
, mask
, wc
);
9973 } else if ((!MANUAL(win
) || win
->quirks
& SWM_Q_ANYWHERE
) &&
9974 !FULLSCREEN(win
) && !MAXIMIZED(win
)) {
9977 else if (win
->ws
->old_r
)
9980 /* windows are centered unless ANYWHERE quirk is set. */
9981 if (win
->quirks
& SWM_Q_ANYWHERE
) {
9982 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
9983 win
->g_float
.x
= e
->x
;
9985 win
->g_float
.x
-= X(r
);
9988 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
9989 win
->g_float
.y
= e
->y
;
9991 win
->g_float
.y
-= Y(r
);
9995 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
9996 win
->g_float
.w
= e
->width
;
9998 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
9999 win
->g_float
.h
= e
->height
;
10001 win
->g_floatvalid
= true;
10003 if (!MAXIMIZED(win
) && !FULLSCREEN(win
) &&
10004 (TRANS(win
) || (ABOVE(win
) &&
10005 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]))) {
10006 WIDTH(win
) = win
->g_float
.w
;
10007 HEIGHT(win
) = win
->g_float
.h
;
10010 update_floater(win
);
10013 config_win(win
, e
);
10017 config_win(win
, e
);
10021 config_win(win
, e
);
10025 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
10029 configurenotify(xcb_configure_notify_event_t
*e
)
10031 struct ws_win
*win
;
10033 DNPRINTF(SWM_D_EVENT
, "configurenotify: win %#x, event win: %#x, "
10034 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
10035 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
10036 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
10037 YESNO(e
->override_redirect
));
10039 win
= find_window(e
->window
);
10041 xcb_icccm_get_wm_normal_hints_reply(conn
,
10042 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
10045 if (font_adjusted
) {
10053 destroynotify(xcb_destroy_notify_event_t
*e
)
10055 struct ws_win
*win
;
10057 DNPRINTF(SWM_D_EVENT
, "destroynotify: win %#x\n", e
->window
);
10059 if ((win
= find_window(e
->window
))) {
10060 /* Managed window cleanup. */
10061 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10062 /* If focused, focus on something else. */
10063 if (win
== win
->ws
->focus
)
10064 win
->ws
->focus_pending
= get_focus_prev(win
);
10067 unmanage_window(win
);
10070 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
10071 if (win
->ws
->focus_pending
) {
10072 focus_win(win
->ws
->focus_pending
);
10073 win
->ws
->focus_pending
= NULL
;
10074 } else if (win
== win
->ws
->focus
) {
10075 xcb_set_input_focus(conn
,
10076 XCB_INPUT_FOCUS_PARENT
, win
->ws
->r
->id
,
10084 win
= find_unmanaged_window(e
->window
);
10088 /* unmanage_window() puts win into unmanaged list. */
10089 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10093 DNPRINTF(SWM_D_EVENT
, "destroynotify: done.\n");
10098 get_notify_detail_label(uint8_t detail
)
10103 case XCB_NOTIFY_DETAIL_ANCESTOR
:
10104 label
= "Ancestor";
10106 case XCB_NOTIFY_DETAIL_VIRTUAL
:
10109 case XCB_NOTIFY_DETAIL_INFERIOR
:
10110 label
= "Inferior";
10112 case XCB_NOTIFY_DETAIL_NONLINEAR
:
10113 label
= "Nonlinear";
10115 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
10116 label
= "NonlinearVirtual";
10118 case XCB_NOTIFY_DETAIL_POINTER
:
10121 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
10122 label
= "PointerRoot";
10124 case XCB_NOTIFY_DETAIL_NONE
:
10135 get_notify_mode_label(uint8_t mode
)
10140 case XCB_NOTIFY_MODE_NORMAL
:
10143 case XCB_NOTIFY_MODE_GRAB
:
10146 case XCB_NOTIFY_MODE_UNGRAB
:
10149 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
10150 label
= "WhileGrabbed";
10160 get_state_mask_label(uint16_t state
)
10165 case XCB_KEY_BUT_MASK_SHIFT
:
10166 label
= "ShiftMask";
10168 case XCB_KEY_BUT_MASK_LOCK
:
10169 label
= "LockMask";
10171 case XCB_KEY_BUT_MASK_CONTROL
:
10172 label
= "ControlMask";
10174 case XCB_KEY_BUT_MASK_MOD_1
:
10175 label
= "Mod1Mask";
10177 case XCB_KEY_BUT_MASK_MOD_2
:
10178 label
= "Mod2Mask";
10180 case XCB_KEY_BUT_MASK_MOD_3
:
10181 label
= "Mod3Mask";
10183 case XCB_KEY_BUT_MASK_MOD_4
:
10184 label
= "Mod4Mask";
10186 case XCB_KEY_BUT_MASK_MOD_5
:
10187 label
= "Mod5Mask";
10189 case XCB_KEY_BUT_MASK_BUTTON_1
:
10190 label
= "Button1Mask";
10192 case XCB_KEY_BUT_MASK_BUTTON_2
:
10193 label
= "Button2Mask";
10195 case XCB_KEY_BUT_MASK_BUTTON_3
:
10196 label
= "Button3Mask";
10198 case XCB_KEY_BUT_MASK_BUTTON_4
:
10199 label
= "Button4Mask";
10201 case XCB_KEY_BUT_MASK_BUTTON_5
:
10202 label
= "Button5Mask";
10216 enternotify(xcb_enter_notify_event_t
*e
)
10218 struct ws_win
*win
;
10219 struct swm_region
*r
;
10221 last_event_time
= e
->time
;
10223 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): %#x "
10224 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10225 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10226 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10227 get_notify_mode_label(e
->mode
), e
->mode
,
10228 get_notify_detail_label(e
->detail
), e
->detail
,
10229 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10230 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10233 if (focus_mode
== SWM_FOCUS_MANUAL
&&
10234 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10235 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
10239 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10240 e
->mode
== XCB_NOTIFY_MODE_UNGRAB
&&
10241 e
->detail
!= XCB_NOTIFY_DETAIL_ANCESTOR
) {
10242 DNPRINTF(SWM_D_EVENT
, "enternotify: ungrab; ignoring.\n");
10246 if ((win
= find_window(e
->event
)) == NULL
) {
10247 if (e
->event
== e
->root
) {
10248 /* If no windows on pointer region, then focus root. */
10249 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10251 DNPRINTF(SWM_D_EVENT
, "enternotify: "
10252 "NULL region; ignoring.\n");
10258 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
10263 if (e
->mode
== XCB_NOTIFY_MODE_NORMAL
&&
10264 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10265 DNPRINTF(SWM_D_EVENT
, "enternotify: entering from "
10266 "inferior; ignoring\n");
10270 focus_win(get_focus_magic(win
));
10278 leavenotify(xcb_leave_notify_event_t
*e
)
10280 last_event_time
= e
->time
;
10282 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): %#x "
10283 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10284 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10285 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10286 get_notify_mode_label(e
->mode
), e
->mode
,
10287 get_notify_detail_label(e
->detail
), e
->detail
,
10288 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10289 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10295 mapnotify(xcb_map_notify_event_t
*e
)
10297 struct ws_win
*win
, *parent
= NULL
;
10298 struct workspace
*ws
;
10300 DNPRINTF(SWM_D_EVENT
, "mapnotify: win %#x\n", e
->window
);
10302 if ((win
= manage_window(e
->window
, spawn_position
, true)) == NULL
)
10306 /* Need to know if win was mapped due to ws switch. */
10307 if (ws
->state
== SWM_WS_STATE_MAPPED
) {
10308 if (ws
->focus_pending
&& TRANS(ws
->focus_pending
))
10309 parent
= find_window(win
->transient
);
10311 /* If window's parent is maximized, don't clear it. */
10312 if ((parent
== NULL
) || !MAXIMIZED(parent
))
10313 if (clear_maximized(ws
) > 0)
10317 win
->mapped
= true;
10318 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
10320 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
10321 if (ws
->focus_pending
== win
) {
10323 ws
->focus_pending
= NULL
;
10324 center_pointer(win
->ws
->r
);
10333 mappingnotify(xcb_mapping_notify_event_t
*e
)
10335 if (e
->request
!= XCB_MAPPING_POINTER
) {
10336 xcb_refresh_keyboard_mapping(syms
, e
);
10344 maprequest(xcb_map_request_event_t
*e
)
10346 struct ws_win
*win
, *w
= NULL
;
10347 xcb_get_window_attributes_reply_t
*war
;
10349 DNPRINTF(SWM_D_EVENT
, "maprequest: win %#x\n",
10352 war
= xcb_get_window_attributes_reply(conn
,
10353 xcb_get_window_attributes(conn
, e
->window
),
10356 DNPRINTF(SWM_D_EVENT
, "maprequest: window lost.\n");
10360 if (war
->override_redirect
) {
10361 DNPRINTF(SWM_D_EVENT
, "maprequest: override_redirect; "
10366 win
= manage_window(e
->window
, spawn_position
,
10367 (war
->map_state
== XCB_MAP_STATE_VIEWABLE
));
10371 /* The new window should get focus; prepare. */
10372 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10373 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
) &&
10374 (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
10375 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
10376 win
->hints
.input
))) {
10377 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
10378 /* See if other wins of same type are already mapped. */
10379 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
10380 if (w
== win
|| !w
->mapped
)
10383 if (w
->ch
.class_name
&&
10384 win
->ch
.class_name
&&
10385 strcmp(w
->ch
.class_name
,
10386 win
->ch
.class_name
) == 0 &&
10387 w
->ch
.instance_name
&&
10388 win
->ch
.instance_name
&&
10389 strcmp(w
->ch
.instance_name
,
10390 win
->ch
.instance_name
) == 0)
10396 win
->ws
->focus_pending
= get_focus_magic(win
);
10399 /* All windows need to be mapped if they are in the current workspace.*/
10403 /* Ignore EnterNotify to handle the mapnotify without interference. */
10404 if (focus_mode
== SWM_FOCUS_DEFAULT
)
10405 event_drain(XCB_ENTER_NOTIFY
);
10408 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
10412 motionnotify(xcb_motion_notify_event_t
*e
)
10414 struct swm_region
*r
= NULL
;
10415 int i
, num_screens
;
10417 last_event_time
= e
->time
;
10419 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): %#x "
10420 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10421 "child: %#x, same_screen_focus: %s, state: %d\n",
10422 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10423 get_notify_detail_label(e
->detail
), e
->detail
,
10424 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10425 YESNO(e
->same_screen
), e
->state
);
10427 if (focus_mode
== SWM_FOCUS_MANUAL
)
10430 num_screens
= get_screen_count();
10431 for (i
= 0; i
< num_screens
; i
++)
10432 if (screens
[i
].root
== e
->root
)
10435 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10436 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
10437 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
10445 get_atom_name(xcb_atom_t atom
)
10448 #ifdef SWM_DEBUG_ATOM_NAMES
10450 * This should be disabled during most debugging since
10451 * xcb_get_* causes an xcb_flush.
10454 xcb_get_atom_name_reply_t
*r
;
10456 r
= xcb_get_atom_name_reply(conn
,
10457 xcb_get_atom_name(conn
, atom
),
10460 len
= xcb_get_atom_name_name_length(r
);
10462 name
= malloc(len
+ 1);
10464 memcpy(name
, xcb_get_atom_name_name(r
), len
);
10478 propertynotify(xcb_property_notify_event_t
*e
)
10480 struct ws_win
*win
;
10481 struct workspace
*ws
;
10485 name
= get_atom_name(e
->atom
);
10486 DNPRINTF(SWM_D_EVENT
, "propertynotify: win %#x, atom: %s(%u), "
10487 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
10491 last_event_time
= e
->time
;
10493 win
= find_window(e
->window
);
10499 if (e
->atom
== a_state
) {
10500 /* State just changed, make sure it gets focused if mapped. */
10501 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
10502 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
10504 ws
->focus_pending
== win
) {
10505 focus_win(ws
->focus_pending
);
10506 ws
->focus_pending
= NULL
;
10510 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
10511 e
->atom
== XCB_ATOM_WM_NAME
) {
10513 } else if (e
->atom
== a_prot
) {
10514 get_wm_protocols(win
);
10521 unmapnotify(xcb_unmap_notify_event_t
*e
)
10523 struct ws_win
*win
;
10524 struct workspace
*ws
;
10526 DNPRINTF(SWM_D_EVENT
, "unmapnotify: win %#x\n", e
->window
);
10528 /* If we aren't managing the window, then ignore. */
10529 win
= find_window(e
->window
);
10530 if (win
== NULL
|| win
->id
!= e
->window
)
10533 /* Do nothing if already withdrawn. */
10534 if (!win
->mapped
&& !ICONIC(win
))
10538 win
->mapped
= false;
10540 /* If win was focused, make sure to focus on something else. */
10541 if (win
== ws
->focus
) {
10542 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10543 ws
->focus_pending
= get_focus_prev(win
);
10544 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
10545 "focus_pending: %#x\n",
10546 WINID(ws
->focus_pending
));
10554 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
10557 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
10558 unmanage_window(win
);
10564 /* Update focus if ws is active. */
10565 if (WS_FOCUSED(ws
)) {
10566 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
10567 focus_win(get_pointer_win(ws
->r
->s
->root
));
10568 } else if (ws
->focus_pending
) {
10569 focus_win(ws
->focus_pending
);
10570 ws
->focus_pending
= NULL
;
10571 } else if (ws
->focus
== NULL
) {
10572 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
10573 ws
->r
->id
, XCB_CURRENT_TIME
);
10577 center_pointer(ws
->r
);
10583 get_source_type_label(uint32_t type
)
10588 case EWMH_SOURCE_TYPE_NONE
:
10591 case EWMH_SOURCE_TYPE_NORMAL
:
10594 case EWMH_SOURCE_TYPE_OTHER
:
10606 clientmessage(xcb_client_message_event_t
*e
)
10608 struct ws_win
*win
;
10609 struct swm_region
*r
= NULL
;
10612 int num_screens
, i
;
10613 xcb_map_request_event_t mre
;
10617 name
= get_atom_name(e
->type
);
10618 DNPRINTF(SWM_D_EVENT
, "clientmessage: win %#x, atom: %s(%u)\n",
10619 e
->window
, name
, e
->type
);
10623 if (e
->type
== ewmh
[_NET_CURRENT_DESKTOP
].atom
) {
10624 num_screens
= get_screen_count();
10625 for (i
= 0; i
< num_screens
; i
++)
10626 if (screens
[i
].root
== e
->window
) {
10627 r
= screens
[i
].r_focus
;
10631 if (r
&& e
->data
.data32
[0] < (uint32_t)workspace_limit
) {
10632 a
.id
= e
->data
.data32
[0];
10633 switchws(NULL
, r
, &a
);
10640 win
= find_window(e
->window
);
10642 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
10643 /* Manage the window with maprequest. */
10644 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
10645 "unmanaged window.\n");
10646 mre
.window
= e
->window
;
10652 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
10653 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW, "
10654 "source_type: %s(%d)\n",
10655 get_source_type_label(e
->data
.data32
[0]),
10656 e
->data
.data32
[0]);
10659 * Allow focus changes that are a result of direct user
10660 * action and from applications that use the old EWMH spec.
10662 if (e
->data
.data32
[0] != EWMH_SOURCE_TYPE_NORMAL
||
10663 win
->quirks
& SWM_Q_OBEYAPPFOCUSREQ
) {
10664 if (WS_FOCUSED(win
->ws
))
10667 win
->ws
->focus_pending
= win
;
10669 } else if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
10670 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
10671 if (win
->can_delete
)
10672 client_msg(win
, a_delete
, 0);
10674 xcb_kill_client(conn
, win
->id
);
10675 } else if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
10676 DNPRINTF(SWM_D_EVENT
,
10677 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
10679 if (e
->data
.data32
[0] & (1<<8)) /* x */
10680 X(win
) = e
->data
.data32
[1];
10681 if (e
->data
.data32
[0] & (1<<9)) /* y */
10682 Y(win
) = e
->data
.data32
[2];
10683 if (e
->data
.data32
[0] & (1<<10)) /* width */
10684 WIDTH(win
) = e
->data
.data32
[3];
10685 if (e
->data
.data32
[0] & (1<<11)) /* height */
10686 HEIGHT(win
) = e
->data
.data32
[4];
10688 update_window(win
);
10690 /* Notify no change was made. */
10691 config_win(win
, NULL
);
10692 /* TODO: Change stack sizes */
10694 } else if (e
->type
== ewmh
[_NET_RESTACK_WINDOW
].atom
) {
10695 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_RESTACK_WINDOW\n");
10696 val
[0] = e
->data
.data32
[1]; /* Sibling window. */
10697 val
[1] = e
->data
.data32
[2]; /* Stack mode detail. */
10699 xcb_configure_window(conn
, win
->id
, XCB_CONFIG_WINDOW_SIBLING
|
10700 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
10701 } else if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
10702 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
10703 ewmh_change_wm_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
10704 if (e
->data
.data32
[2])
10705 ewmh_change_wm_state(win
, e
->data
.data32
[2],
10706 e
->data
.data32
[0]);
10708 ewmh_update_wm_state(win
);
10710 } else if (e
->type
== ewmh
[_NET_WM_DESKTOP
].atom
) {
10711 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_DESKTOP\n");
10714 win_to_ws(win
, e
->data
.data32
[0], true);
10716 /* Restack if either the source or destination ws is mapped. */
10717 if (r
!= NULL
|| win
->ws
->r
!= NULL
) {
10719 load_float_geom(win
);
10731 int errcode
= xcb_connection_has_error(conn
);
10732 #ifdef XCB_CONN_ERROR
10735 case XCB_CONN_ERROR
:
10736 s
= "Socket error, pipe error or other stream error.";
10738 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
10739 s
= "Extension not supported.";
10741 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
10742 s
= "Insufficient memory.";
10744 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
10745 s
= "Request length was exceeded.";
10747 case XCB_CONN_CLOSED_PARSE_ERR
:
10748 s
= "Error parsing display string.";
10751 s
= "Unknown error.";
10754 errx(errcode
, "X CONNECTION ERROR: %s", s
);
10757 errx(errcode
, "X CONNECTION ERROR");
10764 int num_screens
, i
;
10765 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
10766 XCB_EVENT_MASK_ENTER_WINDOW
;
10768 xcb_void_cookie_t wac
;
10769 xcb_generic_error_t
*error
;
10771 /* this causes an error if some other window manager is running */
10772 num_screens
= get_screen_count();
10773 for (i
= 0; i
< num_screens
; i
++) {
10774 if ((sc
= get_screen(i
)) == NULL
)
10775 errx(1, "ERROR: can't get screen %d.", i
);
10776 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: %#x\n",
10778 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
10779 XCB_CW_EVENT_MASK
, &val
);
10780 if ((error
= xcb_request_check(conn
, wac
))) {
10781 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
10782 error
->error_code
);
10792 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
10794 struct swm_region
*r
= NULL
, *n
;
10795 struct workspace
*ws
= NULL
;
10799 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
10800 s
->idx
, w
, h
, x
, y
);
10802 /* remove any conflicting regions */
10803 n
= TAILQ_FIRST(&s
->rl
);
10806 n
= TAILQ_NEXT(r
, entry
);
10807 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
10808 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
10809 if (r
->ws
->r
!= NULL
)
10810 r
->ws
->old_r
= r
->ws
->r
;
10813 xcb_destroy_window(conn
, r
->id
);
10814 TAILQ_REMOVE(&s
->rl
, r
, entry
);
10815 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
10819 /* search old regions for one to reuse */
10821 /* size + location match */
10822 TAILQ_FOREACH(r
, &s
->orl
, entry
)
10823 if (X(r
) == x
&& Y(r
) == y
&&
10824 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
10828 TAILQ_FOREACH(r
, &s
->orl
, entry
)
10829 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
10833 TAILQ_REMOVE(&s
->orl
, r
, entry
);
10834 /* try to use old region's workspace */
10835 if (r
->ws
->r
== NULL
)
10838 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
10839 err(1, "new_region: calloc: failed to allocate memory "
10842 /* if we don't have a workspace already, find one */
10844 for (i
= 0; i
< workspace_limit
; i
++)
10845 if (s
->ws
[i
].r
== NULL
) {
10852 errx(1, "new_region: no free workspaces");
10854 if (ws
->state
== SWM_WS_STATE_HIDDEN
)
10855 ws
->state
= SWM_WS_STATE_MAPPING
;
10863 r
->ws_prior
= NULL
;
10866 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
10868 /* Invisible region window to detect pointer events on empty regions. */
10869 r
->id
= xcb_generate_id(conn
);
10870 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
10871 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
10873 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
10874 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
10875 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
10877 /* Make sure region input is at the bottom. */
10878 wa
[0] = XCB_STACK_MODE_BELOW
;
10879 xcb_configure_window(conn
, r
->id
, XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
10881 xcb_map_window(conn
, r
->id
);
10885 scan_randr(int idx
)
10887 #ifdef SWM_XRR_HAS_CRTC
10890 #endif /* SWM_XRR_HAS_CRTC */
10891 struct swm_region
*r
;
10892 struct ws_win
*win
;
10894 xcb_randr_get_screen_resources_current_cookie_t src
;
10895 xcb_randr_get_screen_resources_current_reply_t
*srr
;
10896 xcb_randr_get_crtc_info_cookie_t cic
;
10897 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
10898 xcb_randr_crtc_t
*crtc
;
10899 xcb_screen_t
*screen
;
10901 DNPRINTF(SWM_D_MISC
, "scan_randr: screen: %d\n", idx
);
10903 if ((screen
= get_screen(idx
)) == NULL
)
10904 errx(1, "ERROR: can't get screen %d.", idx
);
10906 num_screens
= get_screen_count();
10907 if (idx
>= num_screens
)
10908 errx(1, "scan_randr: invalid screen");
10910 /* remove any old regions */
10911 while ((r
= TAILQ_FIRST(&screens
[idx
].rl
)) != NULL
) {
10912 r
->ws
->old_r
= r
->ws
->r
= NULL
;
10914 xcb_destroy_window(conn
, r
->id
);
10915 TAILQ_REMOVE(&screens
[idx
].rl
, r
, entry
);
10916 TAILQ_INSERT_TAIL(&screens
[idx
].orl
, r
, entry
);
10920 /* map virtual screens onto physical screens */
10921 #ifdef SWM_XRR_HAS_CRTC
10922 if (randr_support
) {
10923 src
= xcb_randr_get_screen_resources_current(conn
,
10924 screens
[idx
].root
);
10925 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
10928 new_region(&screens
[idx
], 0, 0,
10929 screen
->width_in_pixels
,
10930 screen
->height_in_pixels
);
10933 ncrtc
= srr
->num_crtcs
;
10935 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
10936 for (c
= 0; c
< ncrtc
; c
++) {
10937 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
10939 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
10942 if (cir
->num_outputs
== 0) {
10947 if (cir
->mode
== 0)
10948 new_region(&screens
[idx
], 0, 0,
10949 screen
->width_in_pixels
,
10950 screen
->height_in_pixels
);
10952 new_region(&screens
[idx
],
10953 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
10958 #endif /* SWM_XRR_HAS_CRTC */
10960 /* If detection failed, create a single region that spans the screen. */
10961 if (TAILQ_EMPTY(&screens
[idx
].rl
))
10962 new_region(&screens
[idx
], 0, 0, screen
->width_in_pixels
,
10963 screen
->height_in_pixels
);
10966 /* Cleanup unused previously visible workspaces. */
10967 TAILQ_FOREACH(r
, &screens
[idx
].orl
, entry
) {
10968 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
10970 r
->ws
->state
= SWM_WS_STATE_HIDDEN
;
10972 /* The screen shouldn't focus on an unused region. */
10973 if (screens
[idx
].r_focus
== r
)
10974 screens
[idx
].r_focus
= NULL
;
10977 DNPRINTF(SWM_D_MISC
, "scan_randr: done.\n");
10981 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
10983 struct swm_region
*r
;
10984 int i
, num_screens
;
10986 DNPRINTF(SWM_D_EVENT
, "screenchange: root: %#x\n", e
->root
);
10988 num_screens
= get_screen_count();
10989 /* silly event doesn't include the screen index */
10990 for (i
= 0; i
< num_screens
; i
++)
10991 if (screens
[i
].root
== e
->root
)
10993 if (i
>= num_screens
)
10994 errx(1, "screenchange: screen not found");
10996 /* brute force for now, just re-enumerate the regions */
11000 print_win_geom(e
->root
);
11002 /* add bars to all regions */
11003 for (i
= 0; i
< num_screens
; i
++) {
11004 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11010 /* Make sure a region has focus on each screen. */
11011 for (i
= 0; i
< num_screens
; i
++) {
11012 if (screens
[i
].r_focus
== NULL
) {
11013 r
= TAILQ_FIRST(&screens
[i
].rl
);
11022 /* Update workspace state on all regions. */
11023 for (i
= 0; i
< num_screens
; i
++)
11024 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11025 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
11031 struct swm_region
*r
= NULL
;
11032 xcb_window_t
*wins
= NULL
, trans
, *cwins
= NULL
;
11033 int i
, j
, k
, n
, no
, num_screens
;
11035 bool manage
, mapped
;
11037 xcb_query_tree_cookie_t qtc
;
11038 xcb_query_tree_reply_t
*qtr
;
11039 xcb_get_window_attributes_cookie_t gac
;
11040 xcb_get_window_attributes_reply_t
*gar
;
11041 xcb_get_property_cookie_t pc
;
11042 xcb_get_property_reply_t
*pr
;
11044 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
11045 num_screens
= get_screen_count();
11046 for (i
= 0; i
< num_screens
; i
++) {
11047 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
11048 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
11051 wins
= xcb_query_tree_children(qtr
);
11052 no
= xcb_query_tree_children_length(qtr
);
11054 /* Try to sort windows according to _NET_CLIENT_LIST. */
11055 pr
= xcb_get_property_reply(conn
, xcb_get_property(conn
, 0,
11056 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
11057 XCB_ATOM_WINDOW
, 0, UINT32_MAX
), NULL
);
11059 cwins
= xcb_get_property_value(pr
);
11060 n
= xcb_get_property_value_length(pr
) /
11061 sizeof(xcb_atom_t
);
11063 for (j
= 0; j
< n
; ++j
) {
11064 for (k
= j
; k
< no
; ++k
) {
11065 if (wins
[k
] == cwins
[j
]) {
11066 /* Swap wins j and k. */
11068 wins
[j
] = cwins
[j
];
11076 /* attach windows to a region */
11077 /* normal windows */
11078 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top level windows.\n");
11079 for (j
= 0; j
< no
; j
++) {
11080 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11081 if (r
->id
== wins
[j
]) {
11082 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11083 "skip %#x; region input window.\n",
11086 } else if (r
->bar
->id
== wins
[j
]) {
11087 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11088 "skip %#x; region bar.\n",
11097 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
11098 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
11100 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
11101 "doesn't exist.\n", wins
[j
]);
11105 if (gar
->override_redirect
) {
11106 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
11107 "override_redirect set.\n", wins
[j
]);
11112 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11113 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11115 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
11116 "is transient for %#x.\n", wins
[j
], trans
);
11121 state
= get_win_state(wins
[j
]);
11122 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
11123 mapped
= gar
->map_state
== XCB_MAP_STATE_VIEWABLE
;
11124 if (mapped
|| manage
)
11125 manage_window(wins
[j
], SWM_STACK_TOP
, mapped
);
11128 /* transient windows */
11129 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
11130 for (j
= 0; j
< no
; j
++) {
11131 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
11132 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
11134 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
11135 "doesn't exist.\n", wins
[j
]);
11139 if (gar
->override_redirect
) {
11140 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
11141 "override_redirect set.\n", wins
[j
]);
11146 state
= get_win_state(wins
[j
]);
11147 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
11148 mapped
= gar
->map_state
== XCB_MAP_STATE_VIEWABLE
;
11149 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11150 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11151 &trans
, NULL
) && manage
)
11152 manage_window(wins
[j
], SWM_STACK_TOP
, mapped
);
11157 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
11161 setup_screens(void)
11163 int i
, j
, k
, num_screens
;
11164 struct workspace
*ws
;
11165 uint32_t gcv
[1], wa
[1];
11166 const xcb_query_extension_reply_t
*qep
;
11167 xcb_screen_t
*screen
;
11168 xcb_randr_query_version_cookie_t c
;
11169 xcb_randr_query_version_reply_t
*r
;
11171 num_screens
= get_screen_count();
11172 if ((screens
= calloc(num_screens
, sizeof(struct swm_screen
))) == NULL
)
11173 err(1, "setup_screens: calloc: failed to allocate memory for "
11176 /* Initial RandR setup. */
11177 randr_support
= false;
11178 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
11179 if (qep
->present
) {
11180 c
= xcb_randr_query_version(conn
, 1, 1);
11181 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
11183 if (r
->major_version
>= 1) {
11184 randr_support
= true;
11185 randr_eventbase
= qep
->first_event
;
11191 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
11193 /* map physical screens */
11194 for (i
= 0; i
< num_screens
; i
++) {
11195 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
11196 screens
[i
].idx
= i
;
11197 screens
[i
].r_focus
= NULL
;
11199 TAILQ_INIT(&screens
[i
].rl
);
11200 TAILQ_INIT(&screens
[i
].orl
);
11201 if ((screen
= get_screen(i
)) == NULL
)
11202 errx(1, "ERROR: can't get screen %d.", i
);
11203 screens
[i
].root
= screen
->root
;
11205 /* set default colors */
11206 setscreencolor("red", i
, SWM_S_COLOR_FOCUS
);
11207 setscreencolor("rgb:88/88/88", i
, SWM_S_COLOR_UNFOCUS
);
11208 setscreencolor("rgb:00/80/80", i
, SWM_S_COLOR_BAR_BORDER
);
11209 setscreencolor("rgb:00/40/40", i
,
11210 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
11211 setscreencolor("black", i
, SWM_S_COLOR_BAR
);
11212 setscreencolor("rgb:a0/a0/a0", i
, SWM_S_COLOR_BAR_FONT
);
11213 setscreencolor("red", i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
11214 setscreencolor("rgb:88/88/88", i
,
11215 SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
11217 /* create graphics context on screen */
11218 screens
[i
].bar_gc
= xcb_generate_id(conn
);
11220 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
11221 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
11223 /* set default cursor */
11224 xcb_change_window_attributes(conn
, screens
[i
].root
,
11225 XCB_CW_CURSOR
, wa
);
11227 /* init all workspaces */
11228 /* XXX these should be dynamically allocated too */
11229 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
11230 ws
= &screens
[i
].ws
[j
];
11233 ws
->bar_enabled
= true;
11235 ws
->focus_prev
= NULL
;
11236 ws
->focus_pending
= NULL
;
11239 ws
->state
= SWM_WS_STATE_HIDDEN
;
11240 TAILQ_INIT(&ws
->stack
);
11241 TAILQ_INIT(&ws
->winlist
);
11242 TAILQ_INIT(&ws
->unmanagedlist
);
11244 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
11245 if (layouts
[k
].l_config
!= NULL
)
11246 layouts
[k
].l_config(ws
,
11247 SWM_ARG_ID_STACKINIT
);
11248 ws
->cur_layout
= &layouts
[0];
11249 ws
->cur_layout
->l_string(ws
);
11255 xcb_randr_select_input(conn
, screens
[i
].root
,
11256 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
11261 setup_globals(void)
11263 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
11264 err(1, "setup_globals: strdup: failed to allocate memory.");
11266 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
11267 err(1, "setup_globals: strdup: failed to allocate memory.");
11269 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
11270 errx(1, "unable to allocate key symbols");
11272 a_state
= get_atom_from_string("WM_STATE");
11273 a_prot
= get_atom_from_string("WM_PROTOCOLS");
11274 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
11275 a_net_supported
= get_atom_from_string("_NET_SUPPORTED");
11276 a_net_wm_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
11277 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
11278 a_utf8_string
= get_atom_from_string("UTF8_STRING");
11279 a_swm_ws
= get_atom_from_string("_SWM_WS");
11283 shutdown_cleanup(void)
11285 struct swm_region
*r
;
11287 struct workspace
*ws
;
11288 int i
, num_screens
;
11290 /* disable alarm because the following code may not be interrupted */
11292 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
11293 err(1, "can't disable alarm");
11306 num_screens
= get_screen_count();
11307 for (i
= 0; i
< num_screens
; ++i
) {
11310 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11311 screens
[i
].root
, XCB_CURRENT_TIME
);
11313 if (screens
[i
].bar_gc
!= XCB_NONE
)
11314 xcb_free_gc(conn
, screens
[i
].bar_gc
);
11315 if (!bar_font_legacy
) {
11316 XftColorFree(display
, DefaultVisual(display
, i
),
11317 DefaultColormap(display
, i
), &bar_font_color
);
11318 XftColorFree(display
, DefaultVisual(display
, i
),
11319 DefaultColormap(display
, i
), &search_font_color
);
11322 for (j
= 0; j
< SWM_S_COLOR_MAX
; ++j
) {
11323 free(screens
[i
].c
[j
].name
);
11326 /* Free window memory. */
11327 for (j
= 0; j
< SWM_WS_MAX
; ++j
) {
11328 ws
= &screens
[i
].ws
[j
];
11331 while ((w
= TAILQ_FIRST(&ws
->winlist
)) != NULL
) {
11332 TAILQ_REMOVE(&ws
->winlist
, w
, entry
);
11336 while ((w
= TAILQ_FIRST(&ws
->unmanagedlist
)) != NULL
) {
11337 TAILQ_REMOVE(&ws
->unmanagedlist
, w
, entry
);
11342 /* Free region memory. */
11343 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
11344 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
11349 while ((r
= TAILQ_FIRST(&screens
[i
].orl
)) != NULL
) {
11350 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
11359 free(clock_format
);
11360 free(startup_exception
);
11363 XFreeFontSet(display
, bar_fs
);
11365 XftFontClose(display
, bar_font
);
11367 xcb_key_symbols_free(syms
);
11369 xcb_disconnect(conn
);
11373 event_error(xcb_generic_error_t
*e
)
11377 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
11378 "resource_id: %u, minor_code: %u\n",
11379 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
11380 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
11381 e
->sequence
, e
->resource_id
, e
->minor_code
);
11385 event_handle(xcb_generic_event_t
*evt
)
11387 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
11389 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
11390 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
11391 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
11394 #define EVENT(type, callback) case type: callback((void *)evt); return
11395 EVENT(0, event_error
);
11396 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
11397 EVENT(XCB_BUTTON_RELEASE
, buttonrelease
);
11398 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
11399 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
11400 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
11401 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
11402 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
11403 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
11404 /*EVENT(XCB_CREATE_NOTIFY, );*/
11405 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
11406 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
11407 EVENT(XCB_EXPOSE
, expose
);
11409 EVENT(XCB_FOCUS_IN
, focusin
);
11410 EVENT(XCB_FOCUS_OUT
, focusout
);
11412 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
11413 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
11414 EVENT(XCB_KEY_PRESS
, keypress
);
11415 EVENT(XCB_KEY_RELEASE
, keyrelease
);
11416 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
11418 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
11420 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
11421 EVENT(XCB_MAP_REQUEST
, maprequest
);
11422 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
11423 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
11424 /*EVENT(XCB_NO_EXPOSURE, );*/
11425 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
11426 /*EVENT(XCB_REPARENT_NOTIFY, );*/
11427 /*EVENT(XCB_RESIZE_REQUEST, );*/
11428 /*EVENT(XCB_SELECTION_CLEAR, );*/
11429 /*EVENT(XCB_SELECTION_NOTIFY, );*/
11430 /*EVENT(XCB_SELECTION_REQUEST, );*/
11431 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
11432 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
11435 if (type
- randr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
11436 screenchange((void *)evt
);
11440 main(int argc
, char *argv
[])
11442 struct pollfd pfd
[2];
11443 struct sigaction sact
;
11445 struct passwd
*pwd
;
11446 struct swm_region
*r
;
11447 xcb_generic_event_t
*evt
;
11448 int xfd
, i
, num_screens
, num_readable
;
11449 char conf
[PATH_MAX
], *cfile
= NULL
;
11450 bool stdin_ready
= false, startup
= true;
11452 /* suppress unused warning since var is needed */
11456 time_started
= time(NULL
);
11460 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
11461 if (setlocale(LC_CTYPE
, "") == NULL
|| setlocale(LC_TIME
, "") == NULL
)
11462 warnx("no locale support");
11464 /* handle some signals */
11465 bzero(&sact
, sizeof(sact
));
11466 sigemptyset(&sact
.sa_mask
);
11468 sact
.sa_handler
= sighdlr
;
11469 sigaction(SIGINT
, &sact
, NULL
);
11470 sigaction(SIGQUIT
, &sact
, NULL
);
11471 sigaction(SIGTERM
, &sact
, NULL
);
11472 sigaction(SIGHUP
, &sact
, NULL
);
11474 sact
.sa_handler
= sighdlr
;
11475 sact
.sa_flags
= SA_NOCLDSTOP
;
11476 sigaction(SIGCHLD
, &sact
, NULL
);
11478 if ((display
= XOpenDisplay(0)) == NULL
)
11479 errx(1, "can not open display");
11481 conn
= XGetXCBConnection(display
);
11482 if (xcb_connection_has_error(conn
))
11483 errx(1, "can not get XCB connection");
11485 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
11487 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
11488 xfd
= xcb_get_file_descriptor(conn
);
11490 /* look for local and global conf file */
11491 pwd
= getpwuid(getuid());
11493 errx(1, "invalid user: %d", getuid());
11495 xcb_grab_server(conn
);
11496 xcb_aux_sync(conn
);
11498 /* flush all events */
11499 while ((evt
= get_next_event(false))) {
11500 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
11506 errx(1, "another window manager is currently running");
11508 /* Load Xcursors and/or cursorfont glyph cursors. */
11511 xcb_aux_sync(conn
);
11516 setup_keybindings();
11517 setup_btnbindings();
11522 for (i
= 0; ; i
++) {
11527 snprintf(conf
, sizeof conf
, "%s/.%s",
11528 pwd
->pw_dir
, SWM_CONF_FILE
);
11532 snprintf(conf
, sizeof conf
, "/etc/%s",
11537 snprintf(conf
, sizeof conf
, "%s/.%s",
11538 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
11541 /* global compat */
11542 snprintf(conf
, sizeof conf
, "/etc/%s",
11543 SWM_CONF_FILE_OLD
);
11549 if (strlen(conf
) && stat(conf
, &sb
) != -1)
11550 if (S_ISREG(sb
.st_mode
)) {
11557 /* load conf (if any) */
11559 conf_load(cfile
, SWM_CONF_DEFAULT
);
11563 if (getenv("SWM_STARTED") == NULL
)
11564 setenv("SWM_STARTED", "YES", 1);
11566 /* setup all bars */
11567 num_screens
= get_screen_count();
11568 for (i
= 0; i
< num_screens
; i
++)
11569 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11572 /* Manage existing windows. */
11580 xcb_ungrab_server(conn
);
11583 /* Update state of each newly mapped workspace. */
11584 for (i
= 0; i
< num_screens
; i
++)
11585 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11586 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
11588 memset(&pfd
, 0, sizeof(pfd
));
11590 pfd
[0].events
= POLLIN
;
11591 pfd
[1].fd
= STDIN_FILENO
;
11592 pfd
[1].events
= POLLIN
;
11595 while ((evt
= get_next_event(false))) {
11602 /* If just (re)started, set default focus if needed. */
11606 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
11607 r
= TAILQ_FIRST(&screens
[0].rl
);
11619 num_readable
= poll(pfd
, bar_extra
? 2 : 1, 1000);
11620 if (num_readable
== -1) {
11621 DNPRINTF(SWM_D_MISC
, "poll failed: %s",
11623 } else if (num_readable
> 0 && bar_extra
&&
11624 pfd
[1].revents
& POLLIN
) {
11625 stdin_ready
= true;
11629 restart(NULL
, NULL
, NULL
);
11635 stdin_ready
= false;
11636 bar_extra_update();
11643 shutdown_cleanup();