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-2016 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 MAX_X(r) ((r)->g.x + (r)->g.w)
259 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
260 #define SH_MIN(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
261 #define SH_MIN_W(w) ((w)->sh.min_width)
262 #define SH_MIN_H(w) ((w)->sh.min_height)
263 #define SH_MAX(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
264 #define SH_MAX_W(w) ((w)->sh.max_width)
265 #define SH_MAX_H(w) ((w)->sh.max_height)
266 #define SH_INC(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)
267 #define SH_INC_W(w) ((w)->sh.width_inc)
268 #define SH_INC_H(w) ((w)->sh.height_inc)
269 #define SWM_MAX_FONT_STEPS (3)
270 #define WINID(w) ((w) ? (w)->id : XCB_WINDOW_NONE)
271 #define ACCEPTS_FOCUS(w) (!((w)->hints.flags & XCB_ICCCM_WM_HINT_INPUT) \
272 || ((w)->hints.input))
273 #define WS_FOCUSED(ws) ((ws)->r && (ws)->r->s->r_focus == (ws)->r)
274 #define YESNO(x) ((x) ? "yes" : "no")
275 #define ICONIC(w) ((w)->ewmh_flags & EWMH_F_HIDDEN)
276 #define ABOVE(w) ((w)->ewmh_flags & EWMH_F_ABOVE)
277 #define FULLSCREEN(w) ((w)->ewmh_flags & EWMH_F_FULLSCREEN)
278 #define MAXIMIZED_VERT(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_VERT)
279 #define MAXIMIZED_HORZ(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_HORZ)
280 #define MAXIMIZED(w) (MAXIMIZED_VERT(w) || MAXIMIZED_HORZ(w))
281 #define MANUAL(w) ((w)->ewmh_flags & SWM_F_MANUAL)
282 #define TRANS(w) ((w)->transient != XCB_WINDOW_NONE)
283 #define FLOATING(w) (ABOVE(w) || TRANS(w) || FULLSCREEN(w) || \
286 /* Constrain Window flags */
287 #define SWM_CW_RESIZABLE (0x01)
288 #define SWM_CW_SOFTBOUNDARY (0x02)
289 #define SWM_CW_HARDBOUNDARY (0x04)
290 #define SWM_CW_RIGHT (0x10)
291 #define SWM_CW_LEFT (0x20)
292 #define SWM_CW_BOTTOM (0x40)
293 #define SWM_CW_TOP (0x80)
294 #define SWM_CW_ALLSIDES (0xf0)
296 #define SWM_FOCUS_DEFAULT (0)
297 #define SWM_FOCUS_FOLLOW (1)
298 #define SWM_FOCUS_MANUAL (2)
300 #define SWM_CK_NONE (0)
301 #define SWM_CK_ALL (0xf)
302 #define SWM_CK_FOCUS (0x1)
303 #define SWM_CK_POINTER (0x2)
304 #define SWM_CK_FALLBACK (0x4)
305 #define SWM_CK_REGION (0x8)
307 #define SWM_G_ALL (0xf)
308 #define SWM_G_SIZE (0x1)
309 #define SWM_G_POS (0x2)
311 #define SWM_CONF_DEFAULT (0)
312 #define SWM_CONF_KEYMAPPING (1)
315 #define SWM_LIB "/usr/local/lib/libswmhack.so"
322 xcb_atom_t a_net_frame_extents
;
323 xcb_atom_t a_net_wm_check
;
324 xcb_atom_t a_net_supported
;
325 xcb_atom_t a_takefocus
;
326 xcb_atom_t a_utf8_string
;
328 volatile sig_atomic_t running
= 1;
329 volatile sig_atomic_t restart_wm
= 0;
330 xcb_timestamp_t last_event_time
= 0;
334 unsigned int numlockmask
= 0;
337 xcb_connection_t
*conn
;
338 xcb_key_symbols_t
*syms
;
340 int boundary_width
= 50;
341 bool cycle_empty
= false;
342 bool cycle_visible
= false;
344 int font_adjusted
= 0;
345 uint16_t mod_key
= MODKEY
;
346 bool warp_focus
= false;
347 bool warp_pointer
= false;
348 bool workspace_clamp
= false;
351 struct swm_region
*search_r
;
352 int select_list_pipe
[2];
353 int select_resp_pipe
[2];
355 volatile sig_atomic_t search_resp
;
356 int search_resp_action
;
358 struct search_window
{
359 TAILQ_ENTRY(search_window
) entry
;
363 xcb_window_t indicator
;
365 TAILQ_HEAD(search_winlist
, search_window
);
366 struct search_winlist search_wl
;
371 SWM_SEARCH_UNICONIFY
,
372 SWM_SEARCH_NAME_WORKSPACE
,
373 SWM_SEARCH_SEARCH_WORKSPACE
,
374 SWM_SEARCH_SEARCH_WINDOW
377 #define SWM_STACK_TOP (0)
378 #define SWM_STACK_BOTTOM (1)
379 #define SWM_STACK_ABOVE (2)
380 #define SWM_STACK_BELOW (3)
383 double dialog_ratio
= 0.6;
385 #define SWM_BAR_MAX (256)
386 #define SWM_BAR_JUSTIFY_LEFT (0)
387 #define SWM_BAR_JUSTIFY_CENTER (1)
388 #define SWM_BAR_JUSTIFY_RIGHT (2)
389 #define SWM_BAR_OFFSET (4)
390 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
391 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
392 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
393 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
394 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
396 #ifdef X_HAVE_UTF8_STRING
397 #define DRAWSTRING(x...) Xutf8DrawString(x)
399 #define DRAWSTRING(x...) XmbDrawString(x)
402 char *bar_argv
[] = { NULL
, NULL
};
404 char bar_ext
[SWM_BAR_MAX
];
405 char bar_ext_buf
[SWM_BAR_MAX
];
406 char bar_vertext
[SWM_BAR_MAX
];
407 bool bar_version
= false;
408 bool bar_enabled
= true;
409 int bar_border_width
= 1;
410 bool bar_at_bottom
= false;
411 bool bar_extra
= false;
413 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
414 char *bar_format
= NULL
;
415 bool stack_enabled
= true;
416 bool clock_enabled
= true;
417 bool iconic_enabled
= false;
418 bool maximize_hide_bar
= false;
419 bool urgent_enabled
= false;
420 bool urgent_collapse
= false;
421 char *clock_format
= NULL
;
422 bool window_class_enabled
= false;
423 bool window_instance_enabled
= false;
424 bool window_name_enabled
= false;
425 int focus_mode
= SWM_FOCUS_DEFAULT
;
426 int focus_close
= SWM_STACK_BELOW
;
427 bool focus_close_wrap
= true;
428 int focus_default
= SWM_STACK_TOP
;
429 int spawn_position
= SWM_STACK_TOP
;
430 bool disable_border
= false;
431 int border_width
= 1;
432 int region_padding
= 0;
434 bool java_workaround
= true;
435 bool verbose_layout
= false;
441 XFontSet bar_fs
= NULL
;
442 XFontSetExtents
*bar_fs_extents
;
443 XftFont
*bar_font
= NULL
;
444 bool bar_font_legacy
= true;
445 char *bar_fonts
= NULL
;
446 XftColor bar_font_color
;
447 XftColor search_font_color
;
448 char *startup_exception
= NULL
;
449 unsigned int nr_exceptions
= 0;
451 /* layout manager data */
452 struct swm_geometry
{
465 struct swm_geometry g
;
466 struct swm_region
*r
; /* Associated region. */
469 /* virtual "screens" */
471 TAILQ_ENTRY(swm_region
) entry
;
473 struct swm_geometry g
;
474 struct workspace
*ws
; /* current workspace on this region */
475 struct workspace
*ws_prior
; /* prior workspace on this region */
476 struct swm_screen
*s
; /* screen idx */
479 TAILQ_HEAD(swm_region_list
, swm_region
);
482 SWM_WIN_STATE_REPARENTING
,
483 SWM_WIN_STATE_REPARENTED
,
484 SWM_WIN_STATE_UNPARENTING
,
485 SWM_WIN_STATE_UNPARENTED
,
489 TAILQ_ENTRY(ws_win
) entry
;
490 TAILQ_ENTRY(ws_win
) stack_entry
;
493 xcb_window_t transient
;
494 struct ws_win
*focus_child
; /* focus on child transient */
495 struct swm_geometry g
; /* current geometry */
496 struct swm_geometry g_prev
; /* prev configured geometry */
497 struct swm_geometry g_float
; /* region coordinates */
498 bool g_floatvalid
; /* g_float geometry validity */
503 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
510 struct workspace
*ws
; /* always valid */
511 struct swm_screen
*s
; /* always valid, never changes */
513 xcb_icccm_get_wm_class_reply_t ch
;
514 xcb_icccm_wm_hints_t hints
;
519 TAILQ_HEAD(ws_win_list
, ws_win
);
520 TAILQ_HEAD(ws_win_stack
, ws_win
);
524 TAILQ_ENTRY(pid_e
) entry
;
528 TAILQ_HEAD(pid_list
, pid_e
);
529 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
531 /* layout handlers */
532 void stack(struct swm_region
*);
533 void vertical_config(struct workspace
*, int);
534 void vertical_stack(struct workspace
*, struct swm_geometry
*);
535 void horizontal_config(struct workspace
*, int);
536 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
537 void max_stack(struct workspace
*, struct swm_geometry
*);
538 void plain_stacker(struct workspace
*);
539 void fancy_stacker(struct workspace
*);
542 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
543 void (*l_config
)(struct workspace
*, int);
545 #define SWM_L_FOCUSPREV (1<<0)
546 #define SWM_L_MAPONFOCUS (1<<1)
547 void (*l_string
)(struct workspace
*);
549 /* stack, configure */
550 { vertical_stack
, vertical_config
, 0, plain_stacker
},
551 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
553 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
554 { NULL
, NULL
, 0, NULL
},
557 /* position of max_stack mode in the layouts array, index into layouts! */
558 #define SWM_V_STACK (0)
559 #define SWM_H_STACK (1)
560 #define SWM_MAX_STACK (2)
562 #define SWM_H_SLICE (32)
563 #define SWM_V_SLICE (32)
565 /* define work spaces */
567 int idx
; /* workspace index */
568 char *name
; /* workspace name */
569 bool always_raise
; /* raise windows on focus */
570 bool bar_enabled
; /* bar visibility */
571 struct layout
*cur_layout
; /* current layout handlers */
572 struct ws_win
*focus
; /* may be NULL */
573 struct ws_win
*focus_prev
; /* may be NULL */
574 struct ws_win
*focus_pending
; /* may be NULL */
575 struct swm_region
*r
; /* may be NULL */
576 struct swm_region
*old_r
; /* may be NULL */
577 struct ws_win_list winlist
; /* list of windows in ws */
578 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
579 struct ws_win_stack stack
; /* stacking order */
580 int state
; /* mapping state */
581 char stacker
[10]; /* display stacker and layout */
585 int horizontal_msize
;
587 int horizontal_stacks
;
588 bool horizontal_flip
;
598 SWM_WS_STATE_MAPPING
,
604 SWM_S_COLOR_BAR_BORDER
,
605 SWM_S_COLOR_BAR_BORDER_UNFOCUS
,
606 SWM_S_COLOR_BAR_FONT
,
608 SWM_S_COLOR_FOCUS_MAXIMIZED
,
610 SWM_S_COLOR_UNFOCUS_MAXIMIZED
,
614 /* physical screen mapping */
615 #define SWM_WS_MAX (22) /* hard limit */
616 int workspace_limit
= 10; /* soft limit */
619 int idx
; /* screen index */
620 struct swm_region_list rl
; /* list of regions on this screen */
621 struct swm_region_list orl
; /* list of old regions */
623 struct workspace ws
[SWM_WS_MAX
];
624 struct swm_region
*r_focus
;
631 } c
[SWM_S_COLOR_MAX
];
633 xcb_gcontext_t bar_gc
;
636 struct swm_screen
*screens
;
638 /* args to functions */
641 #define SWM_ARG_ID_FOCUSNEXT (0)
642 #define SWM_ARG_ID_FOCUSPREV (1)
643 #define SWM_ARG_ID_FOCUSMAIN (2)
644 #define SWM_ARG_ID_FOCUSURGENT (3)
645 #define SWM_ARG_ID_SWAPNEXT (10)
646 #define SWM_ARG_ID_SWAPPREV (11)
647 #define SWM_ARG_ID_SWAPMAIN (12)
648 #define SWM_ARG_ID_MOVELAST (13)
649 #define SWM_ARG_ID_MASTERSHRINK (20)
650 #define SWM_ARG_ID_MASTERGROW (21)
651 #define SWM_ARG_ID_MASTERADD (22)
652 #define SWM_ARG_ID_MASTERDEL (23)
653 #define SWM_ARG_ID_FLIPLAYOUT (24)
654 #define SWM_ARG_ID_STACKRESET (30)
655 #define SWM_ARG_ID_STACKINIT (31)
656 #define SWM_ARG_ID_STACKBALANCE (32)
657 #define SWM_ARG_ID_CYCLEWS_UP (40)
658 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
659 #define SWM_ARG_ID_CYCLERG_UP (42)
660 #define SWM_ARG_ID_CYCLERG_DOWN (43)
661 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
662 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
663 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
664 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
665 #define SWM_ARG_ID_STACKINC (50)
666 #define SWM_ARG_ID_STACKDEC (51)
667 #define SWM_ARG_ID_SS_ALL (60)
668 #define SWM_ARG_ID_SS_WINDOW (61)
669 #define SWM_ARG_ID_DONTCENTER (70)
670 #define SWM_ARG_ID_CENTER (71)
671 #define SWM_ARG_ID_KILLWINDOW (80)
672 #define SWM_ARG_ID_DELETEWINDOW (81)
673 #define SWM_ARG_ID_WIDTHGROW (90)
674 #define SWM_ARG_ID_WIDTHSHRINK (91)
675 #define SWM_ARG_ID_HEIGHTGROW (92)
676 #define SWM_ARG_ID_HEIGHTSHRINK (93)
677 #define SWM_ARG_ID_MOVEUP (100)
678 #define SWM_ARG_ID_MOVEDOWN (101)
679 #define SWM_ARG_ID_MOVELEFT (102)
680 #define SWM_ARG_ID_MOVERIGHT (103)
681 #define SWM_ARG_ID_RAISE (105)
682 #define SWM_ARG_ID_LOWER (106)
683 #define SWM_ARG_ID_BAR_TOGGLE (110)
684 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
685 #define SWM_ARG_ID_CYCLERG_MOVE_UP (112)
686 #define SWM_ARG_ID_CYCLERG_MOVE_DOWN (113)
692 TAILQ_ENTRY(quirk
) entry
;
693 char *class; /* WM_CLASS:class */
694 char *instance
; /* WM_CLASS:instance */
695 char *name
; /* WM_NAME */
697 regex_t regex_instance
;
700 int ws
; /* Initial workspace. */
701 #define SWM_Q_FLOAT (1<<0) /* Float this window. */
702 #define SWM_Q_TRANSSZ (1<<1) /* Transient window size too small. */
703 #define SWM_Q_ANYWHERE (1<<2) /* Don't position this window */
704 #define SWM_Q_XTERM_FONTADJ (1<<3) /* Adjust xterm fonts when resizing. */
705 #define SWM_Q_FULLSCREEN (1<<4) /* Remove border when fullscreen. */
706 #define SWM_Q_FOCUSPREV (1<<5) /* Focus on caller. */
707 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
708 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
709 #define SWM_Q_OBEYAPPFOCUSREQ (1<<8) /* Focus when applications ask. */
710 #define SWM_Q_IGNOREPID (1<<9) /* Ignore PID when determining ws. */
711 #define SWM_Q_IGNORESPAWNWS (1<<10) /* Ignore _SWM_WS when managing win. */
712 #define SWM_Q_NOFOCUSCYCLE (1<<11) /* Remove from normal focus cycle. */
713 #define SWM_Q_MINIMALBORDER (1<<12) /* No border when floating/unfocused. */
715 TAILQ_HEAD(quirk_list
, quirk
);
716 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
719 * Supported EWMH hints should be added to
720 * both the enum and the ewmh array
726 _NET_CURRENT_DESKTOP
,
727 _NET_DESKTOP_GEOMETRY
,
729 _NET_DESKTOP_VIEWPORT
,
730 _NET_MOVERESIZE_WINDOW
,
731 _NET_NUMBER_OF_DESKTOPS
,
732 _NET_REQUEST_FRAME_EXTENTS
,
734 _NET_WM_ACTION_ABOVE
,
735 _NET_WM_ACTION_CLOSE
,
736 _NET_WM_ACTION_FULLSCREEN
,
738 _NET_WM_ACTION_RESIZE
,
739 _NET_WM_ALLOWED_ACTIONS
,
741 _NET_WM_FULL_PLACEMENT
,
745 _NET_WM_STATE_FULLSCREEN
,
746 _NET_WM_STATE_HIDDEN
,
747 _NET_WM_STATE_MAXIMIZED_VERT
,
748 _NET_WM_STATE_MAXIMIZED_HORZ
,
749 _NET_WM_STATE_SKIP_PAGER
,
750 _NET_WM_STATE_SKIP_TASKBAR
,
752 _NET_WM_WINDOW_TYPE_DIALOG
,
753 _NET_WM_WINDOW_TYPE_DOCK
,
754 _NET_WM_WINDOW_TYPE_NORMAL
,
755 _NET_WM_WINDOW_TYPE_SPLASH
,
756 _NET_WM_WINDOW_TYPE_TOOLBAR
,
757 _NET_WM_WINDOW_TYPE_UTILITY
,
758 _SWM_WM_STATE_MANUAL
,
765 } ewmh
[SWM_EWMH_HINT_MAX
] = {
766 /* must be in same order as in the enum */
767 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
768 {"_NET_CLIENT_LIST", XCB_ATOM_NONE
},
769 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
770 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE
},
771 {"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE
},
772 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE
},
773 {"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE
},
774 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
775 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE
},
776 {"_NET_REQUEST_FRAME_EXTENTS", XCB_ATOM_NONE
},
777 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE
},
778 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
779 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
780 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
781 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
782 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
783 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
784 {"_NET_WM_DESKTOP", XCB_ATOM_NONE
},
785 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE
},
786 {"_NET_WM_NAME", XCB_ATOM_NONE
},
787 {"_NET_WM_STATE", XCB_ATOM_NONE
},
788 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
789 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
790 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
791 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
792 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
793 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
794 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
795 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
796 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
797 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
798 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
799 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
800 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
801 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
802 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
805 /* EWMH source type */
807 EWMH_SOURCE_TYPE_NONE
= 0,
808 EWMH_SOURCE_TYPE_NORMAL
= 1,
809 EWMH_SOURCE_TYPE_OTHER
= 2,
816 XC_BOTTOM_LEFT_CORNER
,
817 XC_BOTTOM_RIGHT_CORNER
,
825 char *name
; /* Name used by Xcursor .*/
826 uint8_t cf_char
; /* cursorfont index. */
828 } cursors
[XC_MAX
] = {
829 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
830 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
831 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
832 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
833 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
834 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
835 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
838 #define SWM_SPAWN_OPTIONAL 0x1
842 TAILQ_ENTRY(spawn_prog
) entry
;
848 TAILQ_HEAD(spawn_list
, spawn_prog
);
849 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
855 /* User callable function IDs. */
978 /* SWM_DEBUG actions MUST be here: */
991 BINDING_F_REPLAY
= 0x1,
995 RB_ENTRY(binding
) entry
;
996 uint16_t mod
; /* Modifier Mask. */
997 enum binding_type type
; /* Key or Button. */
998 uint32_t value
; /* KeySym or Button Index. */
999 enum actionid action
; /* Action Identifier. */
1003 RB_HEAD(binding_tree
, binding
);
1005 /* function prototypes */
1006 void adjust_font(struct ws_win
*);
1007 char *argsep(char **);
1008 void bar_cleanup(struct swm_region
*);
1009 void bar_extra_setup(void);
1010 void bar_extra_stop(void);
1011 int bar_extra_update(void);
1012 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
1013 void bar_fmt_expand(char *, size_t);
1014 void bar_draw(struct swm_bar
*);
1015 void bar_print(struct swm_region
*, const char *);
1016 void bar_print_legacy(struct swm_region
*, const char *);
1017 void bar_replace(char *, char *, struct swm_region
*, size_t);
1018 void bar_replace_pad(char *, int *, size_t);
1019 char *bar_replace_seq(char *, char *, struct swm_region
*, size_t *, size_t);
1020 void bar_setup(struct swm_region
*);
1021 void bar_toggle(struct binding
*, struct swm_region
*, union arg
*);
1022 void bar_urgent(char *, size_t);
1023 void bar_window_class(char *, size_t, struct swm_region
*);
1024 void bar_window_class_instance(char *, size_t, struct swm_region
*);
1025 void bar_window_float(char *, size_t, struct swm_region
*);
1026 void bar_window_instance(char *, size_t, struct swm_region
*);
1027 void bar_window_name(char *, size_t, struct swm_region
*);
1028 void bar_window_state(char *, size_t, struct swm_region
*);
1029 void bar_workspace_name(char *, size_t, struct swm_region
*);
1030 int binding_cmp(struct binding
*, struct binding
*);
1031 void binding_insert(uint16_t, enum binding_type
, uint32_t, enum actionid
,
1032 uint32_t, const char *);
1033 struct binding
*binding_lookup(uint16_t, enum binding_type
, uint32_t);
1034 void binding_remove(struct binding
*);
1035 void buttonpress(xcb_button_press_event_t
*);
1036 void buttonrelease(xcb_button_release_event_t
*);
1037 void center_pointer(struct swm_region
*);
1038 void check_conn(void);
1039 void clear_bindings(void);
1040 void clear_keybindings(void);
1041 int clear_maximized(struct workspace
*);
1042 void clear_quirks(void);
1043 void clear_spawns(void);
1044 void clientmessage(xcb_client_message_event_t
*);
1045 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
1046 int conf_load(const char *, int);
1047 void configurenotify(xcb_configure_notify_event_t
*);
1048 void configurerequest(xcb_configure_request_event_t
*);
1049 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
1050 void constrain_window(struct ws_win
*, struct swm_geometry
*, int *);
1051 int count_win(struct workspace
*, bool);
1052 void cursors_cleanup(void);
1053 void cursors_load(void);
1054 void custom_region(const char *);
1055 void cycle_layout(struct binding
*, struct swm_region
*, union arg
*);
1056 void cyclerg(struct binding
*, struct swm_region
*, union arg
*);
1057 void cyclews(struct binding
*, struct swm_region
*, union arg
*);
1059 void debug_refresh(struct ws_win
*);
1061 void debug_toggle(struct binding
*, struct swm_region
*, union arg
*);
1062 void destroynotify(xcb_destroy_notify_event_t
*);
1063 void dumpwins(struct binding
*, struct swm_region
*, union arg
*);
1064 int enable_wm(void);
1065 void enternotify(xcb_enter_notify_event_t
*);
1066 void event_drain(uint8_t);
1067 void event_error(xcb_generic_error_t
*);
1068 void event_handle(xcb_generic_event_t
*);
1069 void ewmh_apply_flags(struct ws_win
*, uint32_t);
1070 void ewmh_autoquirk(struct ws_win
*);
1071 void ewmh_get_desktop_names(void);
1072 void ewmh_get_wm_state(struct ws_win
*);
1073 void ewmh_update_actions(struct ws_win
*);
1074 void ewmh_update_client_list(void);
1075 void ewmh_update_current_desktop(void);
1076 void ewmh_update_desktop_names(void);
1077 void ewmh_update_desktops(void);
1078 void ewmh_change_wm_state(struct ws_win
*, xcb_atom_t
, long);
1079 void ewmh_update_wm_state(struct ws_win
*);
1080 char *expand_tilde(const char *);
1081 void expose(xcb_expose_event_t
*);
1082 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
1083 struct swm_bar
*find_bar(xcb_window_t
);
1084 struct ws_win
*find_frame_window(xcb_window_t
);
1085 struct pid_e
*find_pid(pid_t
);
1086 struct swm_region
*find_region(xcb_window_t
);
1087 struct ws_win
*find_unmanaged_window(xcb_window_t
);
1088 struct ws_win
*find_window(xcb_window_t
);
1089 void floating_toggle(struct binding
*, struct swm_region
*, union arg
*);
1090 void focus(struct binding
*, struct swm_region
*, union arg
*);
1091 void focus_flush(void);
1092 void focus_pointer(struct binding
*, struct swm_region
*, union arg
*);
1093 void focus_region(struct swm_region
*);
1094 void focus_win(struct ws_win
*);
1095 void focusin(xcb_focus_in_event_t
*);
1097 void focusout(xcb_focus_out_event_t
*);
1099 void focusrg(struct binding
*, struct swm_region
*, union arg
*);
1100 void fontset_init(void);
1101 void free_window(struct ws_win
*);
1102 xcb_atom_t
get_atom_from_string(const char *);
1104 char *get_atom_name(xcb_atom_t
);
1106 struct ws_win
*get_focus_magic(struct ws_win
*);
1107 struct ws_win
*get_focus_prev(struct ws_win
*);
1108 xcb_generic_event_t
*get_next_event(bool);
1110 char *get_notify_detail_label(uint8_t);
1111 char *get_notify_mode_label(uint8_t);
1113 struct ws_win
*get_pointer_win(xcb_window_t
);
1114 struct ws_win
*get_region_focus(struct swm_region
*);
1115 int get_region_index(struct swm_region
*);
1116 xcb_screen_t
*get_screen(int);
1117 int get_screen_count(void);
1119 char *get_source_type_label(uint32_t);
1120 char *get_stack_mode_name(uint8_t);
1121 char *get_state_mask_label(uint16_t);
1123 int32_t get_swm_ws(xcb_window_t
);
1124 bool get_urgent(struct ws_win
*);
1126 char *get_win_input_model(struct ws_win
*);
1128 char *get_win_name(xcb_window_t
);
1129 uint8_t get_win_state(xcb_window_t
);
1130 void get_wm_protocols(struct ws_win
*);
1131 int get_ws_idx(struct ws_win
*);
1132 void grab_windows(void);
1133 void grabbuttons(void);
1134 void grabkeys(void);
1135 void iconify(struct binding
*, struct swm_region
*, union arg
*);
1136 bool isxlfd(char *);
1137 bool keybindreleased(struct binding
*, xcb_key_release_event_t
*);
1138 void keypress(xcb_key_press_event_t
*);
1139 void keyrelease(xcb_key_release_event_t
*);
1140 bool keyrepeating(xcb_key_release_event_t
*);
1141 void kill_bar_extra_atexit(void);
1142 void kill_refs(struct ws_win
*);
1144 void leavenotify(xcb_leave_notify_event_t
*);
1146 void load_float_geom(struct ws_win
*);
1147 void lower_window(struct ws_win
*);
1148 struct ws_win
*manage_window(xcb_window_t
, int, bool);
1149 void map_window(struct ws_win
*);
1150 void mapnotify(xcb_map_notify_event_t
*);
1151 void mappingnotify(xcb_mapping_notify_event_t
*);
1152 void maprequest(xcb_map_request_event_t
*);
1153 void maximize_toggle(struct binding
*, struct swm_region
*, union arg
*);
1154 void motionnotify(xcb_motion_notify_event_t
*);
1155 void move(struct binding
*, struct swm_region
*, union arg
*);
1156 void move_win(struct ws_win
*, struct binding
*, int);
1157 uint32_t name_to_pixel(int, const char *);
1158 void name_workspace(struct binding
*, struct swm_region
*, union arg
*);
1159 void new_region(struct swm_screen
*, int, int, int, int);
1160 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1161 int parsebinding(const char *, uint16_t *, enum binding_type
*, uint32_t *,
1163 int parsequirks(const char *, uint32_t *, int *);
1164 void pressbutton(struct binding
*, struct swm_region
*, union arg
*);
1165 void priorws(struct binding
*, struct swm_region
*, union arg
*);
1167 void print_win_geom(xcb_window_t
);
1169 void propertynotify(xcb_property_notify_event_t
*);
1170 void put_back_event(xcb_generic_event_t
*);
1171 void quirk_free(struct quirk
*);
1172 void quirk_insert(const char *, const char *, const char *, uint32_t, int);
1173 void quirk_remove(struct quirk
*);
1174 void quirk_replace(struct quirk
*, const char *, const char *, const char *,
1176 void quit(struct binding
*, struct swm_region
*, union arg
*);
1177 void raise_toggle(struct binding
*, struct swm_region
*, union arg
*);
1178 void raise_window(struct ws_win
*);
1179 void region_containment(struct ws_win
*, struct swm_region
*, int);
1180 struct swm_region
*region_under(struct swm_screen
*, int, int);
1181 void regionize(struct ws_win
*, int, int);
1182 void reparent_window(struct ws_win
*);
1183 void reparentnotify(xcb_reparent_notify_event_t
*);
1184 void resize(struct binding
*, struct swm_region
*, union arg
*);
1185 void resize_win(struct ws_win
*, struct binding
*, int);
1186 void restart(struct binding
*, struct swm_region
*, union arg
*);
1187 struct swm_region
*root_to_region(xcb_window_t
, int);
1188 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1189 void scan_randr(int);
1190 void search_do_resp(void);
1191 void search_resp_name_workspace(const char *, size_t);
1192 void search_resp_search_window(const char *);
1193 void search_resp_search_workspace(const char *);
1194 void search_resp_uniconify(const char *, size_t);
1195 void search_win(struct binding
*, struct swm_region
*, union arg
*);
1196 void search_win_cleanup(void);
1197 void search_workspace(struct binding
*, struct swm_region
*, union arg
*);
1198 void send_to_rg(struct binding
*, struct swm_region
*, union arg
*);
1199 void send_to_ws(struct binding
*, struct swm_region
*, union arg
*);
1200 void set_region(struct swm_region
*);
1201 int setautorun(const char *, const char *, int);
1202 void setbinding(uint16_t, enum binding_type
, uint32_t, enum actionid
,
1203 uint32_t, const char *);
1204 int setconfbinding(const char *, const char *, int);
1205 int setconfcolor(const char *, const char *, int);
1206 int setconfmodkey(const char *, const char *, int);
1207 int setconfquirk(const char *, const char *, int);
1208 int setconfregion(const char *, const char *, int);
1209 int setconfspawn(const char *, const char *, int);
1210 int setconfvalue(const char *, const char *, int);
1211 int setkeymapping(const char *, const char *, int);
1212 int setlayout(const char *, const char *, int);
1213 void setquirk(const char *, const char *, const char *, uint32_t, int);
1214 void setscreencolor(const char *, int, int);
1215 void setspawn(const char *, const char *, int);
1216 void setup_btnbindings(void);
1217 void setup_ewmh(void);
1218 void setup_globals(void);
1219 void setup_keybindings(void);
1220 void setup_quirks(void);
1221 void setup_screens(void);
1222 void setup_spawn(void);
1223 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1224 void set_win_state(struct ws_win
*, uint8_t);
1225 void shutdown_cleanup(void);
1227 void socket_setnonblock(int);
1228 void sort_windows(struct ws_win_list
*);
1229 void spawn(int, union arg
*, bool);
1230 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1231 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1232 void spawn_insert(const char *, const char *, int);
1233 struct spawn_prog
*spawn_find(const char *);
1234 void spawn_remove(struct spawn_prog
*);
1235 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1236 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1237 void stack_config(struct binding
*, struct swm_region
*, union arg
*);
1238 void stack_master(struct workspace
*, struct swm_geometry
*, int, bool);
1239 void store_float_geom(struct ws_win
*);
1240 char *strdupsafe(const char *);
1241 void swapwin(struct binding
*, struct swm_region
*, union arg
*);
1242 void switchws(struct binding
*, struct swm_region
*, union arg
*);
1243 void teardown_ewmh(void);
1244 void unescape_selector(char *);
1245 void unfocus_win(struct ws_win
*);
1246 void uniconify(struct binding
*, struct swm_region
*, union arg
*);
1247 void unmanage_window(struct ws_win
*);
1248 void unmap_all(void);
1249 void unmap_window(struct ws_win
*);
1250 void unmapnotify(xcb_unmap_notify_event_t
*);
1251 void unparent_window(struct ws_win
*);
1252 void update_floater(struct ws_win
*);
1253 void update_modkey(uint16_t);
1254 void update_win_stacking(struct ws_win
*);
1255 void update_window(struct ws_win
*);
1256 void draw_frame(struct ws_win
*);
1257 void update_wm_state(struct ws_win
*win
);
1258 void updatenumlockmask(void);
1259 void validate_spawns(void);
1260 int validate_win(struct ws_win
*);
1261 int validate_ws(struct workspace
*);
1262 void version(struct binding
*, struct swm_region
*, union arg
*);
1263 void win_to_ws(struct ws_win
*, int, bool);
1264 pid_t
window_get_pid(xcb_window_t
);
1265 void wkill(struct binding
*, struct swm_region
*, union arg
*);
1266 void update_ws_stack(struct workspace
*);
1267 void xft_init(struct swm_region
*);
1268 void _add_startup_exception(const char *, va_list);
1269 void add_startup_exception(const char *, ...);
1271 RB_PROTOTYPE(binding_tree
, binding
, entry
, binding_cmp
);
1272 RB_GENERATE(binding_tree
, binding
, entry
, binding_cmp
);
1273 struct binding_tree bindings
;
1278 xcb_font_t cf
= XCB_NONE
;
1281 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1282 /* try to load Xcursor first. */
1283 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1286 /* fallback to cursorfont. */
1287 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1288 if (cf
== XCB_NONE
) {
1289 cf
= xcb_generate_id(conn
);
1290 xcb_open_font(conn
, cf
, strlen("cursor"),
1294 cursors
[i
].cid
= xcb_generate_id(conn
);
1295 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1296 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1297 0xffff, 0xffff, 0xffff);
1303 xcb_close_font(conn
, cf
);
1307 cursors_cleanup(void)
1310 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1311 xcb_free_cursor(conn
, cursors
[i
].cid
);
1315 expand_tilde(const char *s
)
1317 struct passwd
*ppwd
;
1325 errx(1, "expand_tilde: NULL string.");
1328 result
= strdup(sc
);
1334 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1335 errx(1, "expand_tilde: sysconf");
1337 if ((user
= calloc(1, max
+ 1)) == NULL
)
1338 errx(1, "expand_tilde: calloc");
1340 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1345 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1349 result
= strdup(sc
);
1351 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1355 errx(1, "expand_tilde: failed to allocate memory.");
1361 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1363 unsigned int tmpr
, tmpg
, tmpb
;
1365 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1368 *rr
= RGB_8_TO_16(tmpr
);
1369 *gg
= RGB_8_TO_16(tmpg
);
1370 *bb
= RGB_8_TO_16(tmpb
);
1376 get_screen(int screen
)
1378 const xcb_setup_t
*r
;
1379 xcb_screen_iterator_t iter
;
1381 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1382 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1386 iter
= xcb_setup_roots_iterator(r
);
1387 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1395 get_screen_count(void)
1397 const xcb_setup_t
*r
;
1399 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1400 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1404 return xcb_setup_roots_length(r
);
1408 get_region_index(struct swm_region
*r
)
1410 struct swm_region
*rr
;
1416 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1431 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1432 event_drain(XCB_ENTER_NOTIFY
);
1438 get_atom_from_string(const char *str
)
1440 xcb_intern_atom_cookie_t c
;
1441 xcb_intern_atom_reply_t
*r
;
1444 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1445 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1453 return (XCB_ATOM_NONE
);
1457 get_wm_protocols(struct ws_win
*win
) {
1459 xcb_icccm_get_wm_protocols_reply_t wpr
;
1461 if (xcb_icccm_get_wm_protocols_reply(conn
,
1462 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
1464 for (i
= 0; i
< (int)wpr
.atoms_len
; i
++) {
1465 if (wpr
.atoms
[i
] == a_takefocus
)
1466 win
->take_focus
= true;
1467 if (wpr
.atoms
[i
] == a_delete
)
1468 win
->can_delete
= true;
1470 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
1477 xcb_window_t root
, win
;
1478 int i
, j
, num_screens
;
1480 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1481 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1483 num_screens
= get_screen_count();
1484 for (i
= 0; i
< num_screens
; i
++) {
1485 root
= screens
[i
].root
;
1487 /* Set up _NET_SUPPORTING_WM_CHECK. */
1488 win
= xcb_generate_id(conn
);
1489 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
1490 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1491 XCB_COPY_FROM_PARENT
, 0, NULL
);
1493 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
1494 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1495 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1496 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1498 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1499 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1500 8, strlen("spectrwm"), "spectrwm");
1502 /* Report supported atoms */
1503 xcb_delete_property(conn
, root
, a_net_supported
);
1504 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1505 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, root
,
1506 a_net_supported
, XCB_ATOM_ATOM
, 32, 1,
1511 ewmh_update_desktops();
1512 ewmh_get_desktop_names();
1520 xcb_get_property_cookie_t pc
;
1521 xcb_get_property_reply_t
*pr
;
1523 num_screens
= get_screen_count();
1525 for (i
= 0; i
< num_screens
; i
++) {
1526 /* Get the support check window and destroy it */
1527 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, a_net_wm_check
,
1528 XCB_ATOM_WINDOW
, 0, 1);
1529 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1532 if (pr
->format
== a_net_wm_check
) {
1533 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1535 xcb_destroy_window(conn
, id
);
1536 xcb_delete_property(conn
, screens
[i
].root
,
1538 xcb_delete_property(conn
, screens
[i
].root
,
1546 ewmh_autoquirk(struct ws_win
*win
)
1548 xcb_get_property_reply_t
*r
;
1549 xcb_get_property_cookie_t c
;
1553 c
= xcb_get_property(conn
, 0, win
->id
,
1554 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1555 r
= xcb_get_property_reply(conn
, c
, NULL
);
1559 type
= xcb_get_property_value(r
);
1560 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1562 for (i
= 0; i
< n
; i
++) {
1563 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1565 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1566 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1567 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1568 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1571 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1572 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1573 win
->quirks
= SWM_Q_FLOAT
;
1581 ewmh_update_actions(struct ws_win
*win
)
1583 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1589 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1592 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1593 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1594 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1597 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1598 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1601 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1602 #define _NET_WM_STATE_ADD 1 /* add/set property */
1603 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1606 ewmh_change_wm_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1613 name
= get_atom_name(state
);
1614 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: win %#x, state: %s, "
1615 "action: %ld\n", WINID(win
), name
, action
);
1621 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1622 flag
= EWMH_F_FULLSCREEN
;
1623 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1624 flag
= EWMH_F_ABOVE
;
1625 else if (state
== ewmh
[_NET_WM_STATE_HIDDEN
].atom
)
1626 flag
= EWMH_F_HIDDEN
;
1627 else if (state
== ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
||
1628 state
== ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
)
1629 flag
= EWMH_F_MAXIMIZED
;
1630 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1631 flag
= SWM_F_MANUAL
;
1632 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1633 flag
= EWMH_F_SKIP_PAGER
;
1634 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1635 flag
= EWMH_F_SKIP_TASKBAR
;
1637 /* Disallow unfloating transients. */
1638 if (TRANS(win
) && flag
== EWMH_F_ABOVE
)
1641 new_flags
= win
->ewmh_flags
;
1644 case _NET_WM_STATE_REMOVE
:
1647 case _NET_WM_STATE_ADD
:
1650 case _NET_WM_STATE_TOGGLE
:
1655 ewmh_apply_flags(win
, new_flags
);
1658 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: done.\n");
1662 ewmh_apply_flags(struct ws_win
*win
, uint32_t pending
)
1664 struct workspace
*ws
;
1667 changed
= win
->ewmh_flags
^ pending
;
1671 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: pending: %d\n", pending
);
1673 win
->ewmh_flags
= pending
;
1676 if (changed
& EWMH_F_HIDDEN
) {
1678 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1679 ws
->focus_pending
= get_focus_prev(win
);
1684 /* Reload floating geometry in case region changed. */
1686 load_float_geom(win
);
1688 /* The window is no longer iconic, prepare focus. */
1689 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1690 ws
->focus_pending
= get_focus_magic(win
);
1695 if (changed
& EWMH_F_ABOVE
) {
1696 if (ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1698 load_float_geom(win
);
1699 else if (!MAXIMIZED(win
))
1700 store_float_geom(win
);
1702 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1703 changed
&= ~EWMH_F_MAXIMIZED
;
1707 win
->ewmh_flags
^= EWMH_F_ABOVE
& pending
;
1711 if (changed
& EWMH_F_MAXIMIZED
) {
1712 /* VERT and/or HORZ changed. */
1714 if (!MAXIMIZED(win
))
1715 load_float_geom(win
);
1717 store_float_geom(win
);
1720 if (MAXIMIZED(win
)) {
1721 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
1722 ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1726 ws
->focus_pending
= win
;
1734 if (changed
& EWMH_F_FULLSCREEN
) {
1735 if (FULLSCREEN(win
)) {
1736 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
1740 ws
->focus_pending
= win
;
1743 load_float_geom(win
);
1746 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1750 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: done.\n");
1754 ewmh_update_wm_state(struct ws_win
*win
) {
1755 xcb_atom_t vals
[SWM_EWMH_ACTION_COUNT_MAX
];
1759 vals
[n
++] = ewmh
[_NET_WM_STATE_HIDDEN
].atom
;
1760 if (FULLSCREEN(win
))
1761 vals
[n
++] = ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
;
1762 if (MAXIMIZED_VERT(win
))
1763 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
;
1764 if (MAXIMIZED_HORZ(win
))
1765 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
;
1766 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1767 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
;
1768 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1769 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
;
1770 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1771 vals
[n
++] = ewmh
[_NET_WM_STATE_ABOVE
].atom
;
1772 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1773 vals
[n
++] = ewmh
[_SWM_WM_STATE_MANUAL
].atom
;
1776 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1777 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, n
, vals
);
1779 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1783 ewmh_get_wm_state(struct ws_win
*win
)
1786 xcb_get_property_cookie_t c
;
1787 xcb_get_property_reply_t
*r
;
1793 win
->ewmh_flags
= 0;
1795 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1796 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1797 r
= xcb_get_property_reply(conn
, c
, NULL
);
1801 states
= xcb_get_property_value(r
);
1802 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1804 for (i
= 0; i
< n
; i
++)
1805 ewmh_change_wm_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1813 dumpwins(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
1817 xcb_get_window_attributes_cookie_t c
;
1818 xcb_get_window_attributes_reply_t
*wa
;
1820 /* suppress unused warning since var is needed */
1824 if (r
->ws
== NULL
) {
1825 DPRINTF("dumpwins: invalid workspace\n");
1829 DPRINTF("=== managed window list ws %02d ===\n", r
->ws
->idx
);
1830 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
) {
1831 state
= get_win_state(w
->id
);
1832 c
= xcb_get_window_attributes(conn
, w
->id
);
1833 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1835 DPRINTF("win %#x (%#x), map_state: %d, state: %u, "
1836 "transient: %#x\n", w
->frame
, w
->id
, wa
->map_state
,
1837 state
, w
->transient
);
1840 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1844 DPRINTF("=== stacking order (top down) === \n");
1845 TAILQ_FOREACH(w
, &r
->ws
->stack
, stack_entry
) {
1846 DPRINTF("win %#x (%#x), fs: %s, maximized: %s, above: %s, "
1847 "iconic: %s\n", w
->frame
, w
->id
, YESNO(FULLSCREEN(w
)),
1848 YESNO(MAXIMIZED(w
)), YESNO(ABOVE(w
)), YESNO(ICONIC(w
)));
1851 DPRINTF("===== unmanaged window list =====\n");
1852 TAILQ_FOREACH(w
, &r
->ws
->unmanagedlist
, entry
) {
1853 state
= get_win_state(w
->id
);
1854 c
= xcb_get_window_attributes(conn
, w
->id
);
1855 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1857 DPRINTF("win %#x, map_state: %d, state: %u, "
1858 "transient: %#x\n", w
->id
, wa
->map_state
,
1859 state
, w
->transient
);
1862 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1866 DPRINTF("=================================\n");
1870 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
1873 int num_screens
, i
, j
;
1875 /* Suppress warnings. */
1880 DNPRINTF(SWM_D_MISC
, "debug_toggle\n");
1882 debug_enabled
= !debug_enabled
;
1884 num_screens
= get_screen_count();
1885 for (i
= 0; i
< num_screens
; i
++)
1886 for (j
= 0; j
< workspace_limit
; j
++)
1887 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1894 debug_refresh(struct ws_win
*win
)
1901 XRectangle l_ibox
, l_lbox
;
1902 xcb_rectangle_t rect
;
1904 uint32_t wc
[4], mask
, width
, height
, gcv
[1];
1907 xcb_screen_t
*screen
;
1909 if (debug_enabled
) {
1910 /* Create debug window if it doesn't exist. */
1911 if (win
->debug
== XCB_WINDOW_NONE
) {
1912 if ((screen
= get_screen(win
->s
->idx
)) == NULL
)
1913 errx(1, "ERROR: can't get screen %d.",
1916 win
->debug
= xcb_generate_id(conn
);
1917 wc
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1918 wc
[1] = win
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
1919 wc
[2] = screen
->default_colormap
;
1921 xcb_create_window(conn
, screen
->root_depth
, win
->debug
,
1922 win
->frame
, 0, 0, 10, 10, 1,
1923 XCB_WINDOW_CLASS_INPUT_OUTPUT
, screen
->root_visual
,
1924 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
|
1925 XCB_CW_COLORMAP
, wc
);
1927 xcb_map_window(conn
, win
->debug
);
1930 /* Determine workspace window list index. */
1932 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
1938 /* Determine stacking index (top down). */
1940 TAILQ_FOREACH(w
, &win
->ws
->stack
, stack_entry
) {
1946 if (asprintf(&s
, "%#x f:%#x wl:%d s:%d im:%s", win
->id
,
1947 win
->frame
, widx
, sidx
, get_win_input_model(win
)) == -1)
1952 /* Update window to an appropriate dimension. */
1953 if (bar_font_legacy
) {
1954 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
1955 width
= l_lbox
.width
+ 4;
1956 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
1958 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
1960 width
= info
.width
+ 4;
1961 height
= bar_font
->height
+ 4;
1964 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
1965 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
;
1967 wc
[0] = wc
[1] = border_width
;
1974 xcb_configure_window(conn
, win
->debug
, mask
, wc
);
1976 /* Draw a filled rectangle to 'clear' window. */
1980 rect
.height
= height
;
1982 gcv
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1983 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1984 xcb_poly_fill_rectangle(conn
, win
->debug
, win
->s
->bar_gc
, 1,
1988 if (bar_font_legacy
) {
1989 l_gcv
.graphics_exposures
= 0;
1990 l_draw
= XCreateGC(display
, win
->debug
, 0, &l_gcv
);
1992 XSetForeground(display
, l_draw
,
1993 win
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
1995 DRAWSTRING(display
, win
->debug
, bar_fs
, l_draw
, 2,
1996 (bar_fs_extents
->max_logical_extent
.height
-
1997 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
1999 XFreeGC(display
, l_draw
);
2001 draw
= XftDrawCreate(display
, win
->debug
,
2002 DefaultVisual(display
, win
->s
->idx
),
2003 DefaultColormap(display
, win
->s
->idx
));
2005 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
2006 (bar_height
+ bar_font
->height
) / 2 -
2007 bar_font
->descent
, (FcChar8
*)s
, len
);
2009 XftDrawDestroy(draw
);
2013 } else if (win
->debug
!= XCB_WINDOW_NONE
) {
2014 xcb_destroy_window(conn
, win
->debug
);
2015 win
->debug
= XCB_WINDOW_NONE
;
2020 dumpwins(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
2028 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
2034 #endif /* SWM_DEBUG */
2039 int saved_errno
, status
;
2042 saved_errno
= errno
;
2046 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
2051 if (errno
!= ECHILD
)
2052 warn("sighdlr: waitpid");
2053 #endif /* SWM_DEBUG */
2056 if (pid
== searchpid
)
2060 if (WIFEXITED(status
)) {
2061 if (WEXITSTATUS(status
) != 0)
2062 warnx("sighdlr: child exit status: %d",
2063 WEXITSTATUS(status
));
2065 warnx("sighdlr: child is terminated "
2067 #endif /* SWM_DEBUG */
2081 errno
= saved_errno
;
2087 struct pid_e
*p
= NULL
;
2089 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
2094 TAILQ_FOREACH(p
, &pidlist
, entry
) {
2103 name_to_pixel(int sidx
, const char *colorname
)
2105 uint32_t result
= 0;
2106 char cname
[32] = "#";
2107 xcb_screen_t
*screen
;
2108 xcb_colormap_t cmap
;
2109 xcb_alloc_color_reply_t
*cr
;
2110 xcb_alloc_named_color_reply_t
*nr
;
2111 uint16_t rr
, gg
, bb
;
2113 screen
= get_screen(sidx
);
2114 cmap
= screen
->default_colormap
;
2116 /* color is in format rgb://rr/gg/bb */
2117 if (strncmp(colorname
, "rgb:", 4) == 0) {
2118 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
2119 warnx("could not parse rgb %s", colorname
);
2121 cr
= xcb_alloc_color_reply(conn
,
2122 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
2128 warnx("color '%s' not found", colorname
);
2131 nr
= xcb_alloc_named_color_reply(conn
,
2132 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
2135 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
2136 nr
= xcb_alloc_named_color_reply(conn
,
2137 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
2144 warnx("color '%s' not found", colorname
);
2151 setscreencolor(const char *val
, int i
, int c
)
2153 if (i
< 0 || i
>= get_screen_count())
2156 screens
[i
].c
[c
].pixel
= name_to_pixel(i
, val
);
2157 free(screens
[i
].c
[c
].name
);
2158 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
2163 fancy_stacker(struct workspace
*ws
)
2165 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2166 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2167 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2168 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
2169 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
2170 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2171 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2172 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
2173 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
2177 plain_stacker(struct workspace
*ws
)
2179 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2180 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2181 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
2182 sizeof ws
->stacker
);
2183 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2184 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
2185 sizeof ws
->stacker
);
2189 custom_region(const char *val
)
2191 unsigned int x
, y
, w
, h
;
2192 int sidx
, num_screens
;
2193 xcb_screen_t
*screen
;
2195 DNPRINTF(SWM_D_CONF
, "custom_region: %s\n", val
);
2197 num_screens
= get_screen_count();
2198 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
2199 errx(1, "invalid custom region, "
2200 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
2201 if (sidx
< 1 || sidx
> num_screens
)
2202 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
2206 if ((screen
= get_screen(sidx
)) == NULL
)
2207 errx(1, "ERROR: can't get screen %d.", sidx
);
2210 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
2212 if (x
> screen
->width_in_pixels
||
2213 y
> screen
->height_in_pixels
||
2214 w
+ x
> screen
->width_in_pixels
||
2215 h
+ y
> screen
->height_in_pixels
) {
2216 warnx("ignoring region %ux%u+%u+%u - not within screen "
2217 "boundaries (%ux%u)", w
, h
, x
, y
,
2218 screen
->width_in_pixels
, screen
->height_in_pixels
);
2222 new_region(&screens
[sidx
], x
, y
, w
, h
);
2226 socket_setnonblock(int fd
)
2230 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
2231 err(1, "fcntl F_GETFL");
2232 flags
|= O_NONBLOCK
;
2233 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
2234 err(1, "fcntl F_SETFL");
2238 bar_print_legacy(struct swm_region
*r
, const char *s
)
2240 xcb_rectangle_t rect
;
2245 XRectangle ibox
, lbox
;
2249 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
2251 switch (bar_justify
) {
2252 case SWM_BAR_JUSTIFY_LEFT
:
2255 case SWM_BAR_JUSTIFY_CENTER
:
2256 x
= (WIDTH(r
) - lbox
.width
) / 2;
2258 case SWM_BAR_JUSTIFY_RIGHT
:
2259 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
2263 if (x
< SWM_BAR_OFFSET
)
2268 rect
.width
= WIDTH(r
->bar
);
2269 rect
.height
= HEIGHT(r
->bar
);
2271 /* clear back buffer */
2272 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2273 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2274 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2276 /* draw back buffer */
2277 gcvd
.graphics_exposures
= 0;
2278 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
2279 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2280 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
2281 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
2283 XFreeGC(display
, draw
);
2286 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2287 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2291 bar_print(struct swm_region
*r
, const char *s
)
2294 xcb_rectangle_t rect
;
2302 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
2304 switch (bar_justify
) {
2305 case SWM_BAR_JUSTIFY_LEFT
:
2308 case SWM_BAR_JUSTIFY_CENTER
:
2309 x
= (WIDTH(r
) - info
.width
) / 2;
2311 case SWM_BAR_JUSTIFY_RIGHT
:
2312 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
2316 if (x
< SWM_BAR_OFFSET
)
2321 rect
.width
= WIDTH(r
->bar
);
2322 rect
.height
= HEIGHT(r
->bar
);
2324 /* clear back buffer */
2325 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2326 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2327 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2329 /* draw back buffer */
2330 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
2331 DefaultVisual(display
, r
->s
->idx
),
2332 DefaultColormap(display
, r
->s
->idx
));
2334 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
2335 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
2338 XftDrawDestroy(draw
);
2341 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2342 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2346 bar_extra_stop(void)
2350 bzero(bar_pipe
, sizeof bar_pipe
);
2353 kill(bar_pid
, SIGTERM
);
2356 strlcpy(bar_ext
, "", sizeof bar_ext
);
2361 bar_window_class(char *s
, size_t sz
, struct swm_region
*r
)
2363 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2365 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
2366 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
2370 bar_window_instance(char *s
, size_t sz
, struct swm_region
*r
)
2372 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2374 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
2375 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
2379 bar_window_class_instance(char *s
, size_t sz
, struct swm_region
*r
)
2381 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2384 bar_window_class(s
, sz
, r
);
2385 strlcat(s
, ":", sz
);
2386 bar_window_instance(s
, sz
, r
);
2390 bar_window_state(char *s
, size_t sz
, struct swm_region
*r
)
2392 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2394 if (MAXIMIZED(r
->ws
->focus
))
2395 strlcat(s
, "(m)", sz
);
2396 else if (ABOVE(r
->ws
->focus
))
2397 strlcat(s
, "(f)", sz
);
2401 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
2405 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2408 title
= get_win_name(r
->ws
->focus
->id
);
2409 strlcat(s
, title
, sz
);
2414 get_urgent(struct ws_win
*win
)
2416 xcb_icccm_wm_hints_t hints
;
2417 xcb_get_property_cookie_t c
;
2418 bool urgent
= false;
2421 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2422 if (xcb_icccm_get_wm_hints_reply(conn
, c
, &hints
, NULL
))
2423 urgent
= xcb_icccm_wm_hints_get_urgency(&hints
);
2430 bar_urgent(char *s
, size_t sz
)
2433 int i
, j
, num_screens
;
2434 bool urgent
[SWM_WS_MAX
];
2437 for (i
= 0; i
< workspace_limit
; i
++)
2440 num_screens
= get_screen_count();
2441 for (i
= 0; i
< num_screens
; i
++)
2442 for (j
= 0; j
< workspace_limit
; j
++)
2443 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2444 if (get_urgent(win
))
2447 for (i
= 0; i
< workspace_limit
; i
++) {
2449 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2451 } else if (!urgent_collapse
) {
2452 strlcat(s
, "- ", sz
);
2455 if(urgent_collapse
&& s
[0])
2456 s
[strlen(s
) - 1] = 0;
2460 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2462 if (r
== NULL
|| r
->ws
== NULL
)
2464 if (r
->ws
->name
!= NULL
)
2465 strlcat(s
, r
->ws
->name
, sz
);
2468 /* build the default bar format according to the defined enabled options */
2470 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2474 /* if format provided, just copy the buffers */
2475 if (bar_format
!= NULL
) {
2476 strlcpy(fmtnew
, fmtexp
, sz
);
2480 /* reset the output buffer */
2483 strlcat(fmtnew
, "+N:+I ", sz
);
2485 strlcat(fmtnew
, "+S", sz
);
2486 strlcat(fmtnew
, " ", sz
);
2488 /* only show the workspace name if there's actually one */
2489 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2490 strlcat(fmtnew
, "<+D>", sz
);
2492 /* If enabled, only show the iconic count if there are iconic wins. */
2493 if (iconic_enabled
&& r
!= NULL
&& r
->ws
!= NULL
)
2494 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2496 strlcat(fmtnew
, "{+M}", sz
);
2500 strlcat(fmtnew
, "+3<", sz
);
2502 if (clock_enabled
) {
2503 strlcat(fmtnew
, fmtexp
, sz
);
2504 strlcat(fmtnew
, "+4<", sz
);
2507 /* bar_urgent already adds the space before the last asterisk */
2509 strlcat(fmtnew
, (urgent_collapse
? "*+U*+4<" : "* +U*+4<"), sz
);
2511 if (window_class_enabled
) {
2512 strlcat(fmtnew
, "+C", sz
);
2513 if (!window_instance_enabled
)
2514 strlcat(fmtnew
, "+4<", sz
);
2517 /* checks needed by the colon and floating strlcat(3) calls below */
2518 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2519 if (window_instance_enabled
) {
2520 if (window_class_enabled
)
2521 strlcat(fmtnew
, ":", sz
);
2522 strlcat(fmtnew
, "+T+4<", sz
);
2524 if (window_name_enabled
) {
2525 if (ABOVE(r
->ws
->focus
) || MAXIMIZED(r
->ws
->focus
))
2526 strlcat(fmtnew
, "+F ", sz
);
2527 strlcat(fmtnew
, "+64W ", sz
);
2531 /* finally add the action script output and the version */
2532 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2536 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2538 /* special case; no limit given, pad one space, instead */
2539 if (*limit
== (int)sz
- 1)
2541 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2544 /* replaces the bar format character sequences (like in tmux(1)) */
2546 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2551 char tmp
[SWM_BAR_MAX
];
2552 int limit
, size
, count
;
2555 /* reset strlcat(3) buffer */
2558 /* get number, if any */
2561 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2562 limit
= sizeof tmp
- 1;
2563 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2564 limit
= sizeof tmp
- 1;
2566 /* there is nothing to replace (ie EOL) */
2573 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2576 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2579 bar_window_class(tmp
, sizeof tmp
, r
);
2582 bar_workspace_name(tmp
, sizeof tmp
, r
);
2585 bar_window_state(tmp
, sizeof tmp
, r
);
2588 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2592 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2596 snprintf(tmp
, sizeof tmp
, "%d", count
);
2599 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2602 bar_window_class_instance(tmp
, sizeof tmp
, r
);
2605 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2608 bar_window_instance(tmp
, sizeof tmp
, r
);
2611 bar_urgent(tmp
, sizeof tmp
);
2614 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2617 bar_window_name(tmp
, sizeof tmp
, r
);
2620 /* unknown character sequence; copy as-is */
2621 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2627 if ((int)len
< limit
)
2629 while (limit
-- > 0) {
2630 if (*offrep
>= sz
- 1)
2632 fmtrep
[(*offrep
)++] = *ptr
++;
2640 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2645 while (*fmt
!= '\0') {
2647 /* skip ordinary characters */
2650 fmtrep
[off
++] = *fmt
++;
2654 /* character sequence found; replace it */
2655 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2664 bar_fmt_expand(char *fmtexp
, size_t sz
)
2671 /* start by grabbing the current time and date */
2673 localtime_r(&tmt
, &tm
);
2675 /* figure out what to expand */
2676 if (bar_format
!= NULL
)
2678 else if (bar_format
== NULL
&& clock_enabled
)
2680 /* if nothing to expand bail out */
2686 /* copy as-is, just in case the format shouldn't be expanded below */
2687 strlcpy(fmtexp
, fmt
, sz
);
2688 /* finally pass the string through strftime(3) */
2689 #ifndef SWM_DENY_CLOCK_FORMAT
2690 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2691 warnx("format too long");
2696 /* Redraws a region bar; need to follow with xcb_flush() or focus_flush(). */
2698 bar_draw(struct swm_bar
*bar
)
2700 struct swm_region
*r
;
2701 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2702 char fmtrep
[SWM_BAR_MAX
];
2704 /* expand the format by first passing it through strftime(3) */
2705 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2712 if (bar_enabled
&& r
->ws
->bar_enabled
)
2713 xcb_map_window(conn
, bar
->id
);
2715 xcb_unmap_window(conn
, bar
->id
);
2719 if (startup_exception
)
2720 snprintf(fmtrep
, sizeof fmtrep
, "total "
2721 "exceptions: %d, first exception: %s",
2725 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2726 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2729 if (bar_font_legacy
)
2730 bar_print_legacy(r
, fmtrep
);
2732 bar_print(r
, fmtrep
);
2736 * Reads external script output; call when stdin is readable.
2737 * Returns 1 if bar_ext was updated; otherwise 0.
2740 bar_extra_update(void)
2743 char b
[SWM_BAR_MAX
];
2744 bool changed
= false;
2749 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2752 if (b
[len
- 1] == '\n') {
2753 /* Remove newline. */
2756 /* "Clear" bar_ext. */
2759 /* Flush buffered output. */
2760 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2761 bar_ext_buf
[0] = '\0';
2763 /* Append new output to bar. */
2764 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2768 /* Buffer output. */
2769 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2774 if (errno
!= EAGAIN
) {
2775 warn("bar_action failed");
2784 bar_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
2786 struct swm_region
*tmpr
;
2789 /* suppress unused warnings since vars are needed */
2794 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2797 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2798 /* Only change if master switch is enabled. */
2800 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2802 bar_enabled
= r
->ws
->bar_enabled
= true;
2804 case SWM_ARG_ID_BAR_TOGGLE
:
2805 bar_enabled
= !bar_enabled
;
2809 /* update bars as necessary */
2810 num_screens
= get_screen_count();
2811 for (i
= 0; i
< num_screens
; i
++)
2812 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2814 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2815 xcb_map_window(conn
, tmpr
->bar
->id
);
2817 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2820 /* Restack all regions and redraw bar. */
2821 num_screens
= get_screen_count();
2822 for (i
= 0; i
< num_screens
; i
++)
2823 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
2825 bar_draw(tmpr
->bar
);
2832 bar_extra_setup(void)
2834 /* do this here because the conf file is in memory */
2835 if (!bar_extra
&& bar_argv
[0]) {
2836 /* launch external status app */
2838 if (pipe(bar_pipe
) == -1)
2839 err(1, "pipe error");
2840 socket_setnonblock(bar_pipe
[0]);
2841 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2843 /* Set stdin to read from the pipe. */
2844 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2847 /* Set stdout to write to the pipe. */
2848 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2851 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2852 err(1, "could not disable SIGPIPE");
2853 switch (bar_pid
= fork()) {
2855 err(1, "cannot fork");
2859 execvp(bar_argv
[0], bar_argv
);
2860 err(1, "%s external app failed", bar_argv
[0]);
2862 default: /* parent */
2867 atexit(kill_bar_extra_atexit
);
2872 kill_bar_extra_atexit(void)
2875 kill(bar_pid
, SIGTERM
);
2883 while ((s
= index(s
, '-'))) {
2888 return (count
== 14);
2894 char *default_string
;
2895 char **missing_charsets
;
2896 int num_missing_charsets
= 0;
2900 XFreeFontSet(display
, bar_fs
);
2904 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n",
2907 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2908 &num_missing_charsets
, &default_string
);
2910 if (num_missing_charsets
> 0) {
2911 warnx("Unable to load charset(s):");
2913 for (i
= 0; i
< num_missing_charsets
; ++i
)
2914 warnx("%s", missing_charsets
[i
]);
2916 XFreeStringList(missing_charsets
);
2918 if (strcmp(default_string
, ""))
2919 warnx("Glyphs from those sets will be replaced "
2920 "by '%s'.", default_string
);
2922 warnx("Glyphs from those sets won't be drawn.");
2926 errx(1, "Error creating font set structure.");
2928 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2930 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2931 2 * bar_border_width
;
2938 xft_init(struct swm_region
*r
)
2940 char *font
, *str
, *search
;
2943 if (bar_font
== NULL
) {
2944 if ((search
= str
= strdup(bar_fonts
)) == NULL
)
2945 errx(1, "insufficient memory.");
2947 while ((font
= strsep(&search
, ",")) != NULL
) {
2951 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2954 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2957 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2961 if (bar_font
== NULL
) {
2962 warnx("unable to load font %s", font
);
2965 DNPRINTF(SWM_D_INIT
, "successfully opened "
2973 if (bar_font
== NULL
)
2974 errx(1, "unable to open a font");
2976 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2978 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2979 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2980 warn("Xft error: unable to allocate color.");
2982 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR
].pixel
, color
);
2984 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2985 DefaultColormap(display
, r
->s
->idx
), &color
, &search_font_color
))
2986 warn("Xft error: unable to allocate color.");
2988 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2995 bar_setup(struct swm_region
*r
)
2997 xcb_screen_t
*screen
;
3000 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
3003 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
3004 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
3009 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
3010 err(1, "bar_setup: calloc: failed to allocate memory.");
3012 if (bar_font_legacy
)
3019 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
3020 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
3021 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
3023 /* Assume region is unfocused when we create the bar. */
3024 r
->bar
->id
= xcb_generate_id(conn
);
3025 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
3026 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
3027 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
3028 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
3030 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
3031 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
3032 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
3033 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
3034 | XCB_CW_EVENT_MASK
, wa
);
3036 /* Stack bar window above region window to start. */
3038 wa
[1] = XCB_STACK_MODE_ABOVE
;
3040 xcb_configure_window(conn
, r
->bar
->id
, XCB_CONFIG_WINDOW_SIBLING
|
3041 XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
3043 r
->bar
->buffer
= xcb_generate_id(conn
);
3044 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
3045 WIDTH(r
->bar
), HEIGHT(r
->bar
));
3048 xcb_randr_select_input(conn
, r
->bar
->id
,
3049 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
3052 xcb_map_window(conn
, r
->bar
->id
);
3054 DNPRINTF(SWM_D_BAR
, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
3055 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
3062 bar_cleanup(struct swm_region
*r
)
3066 xcb_destroy_window(conn
, r
->bar
->id
);
3067 xcb_free_pixmap(conn
, r
->bar
->buffer
);
3073 set_win_state(struct ws_win
*win
, uint8_t state
)
3075 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
3077 DNPRINTF(SWM_D_EVENT
, "set_win_state: win %#x, state: %u\n",
3083 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
3084 a_state
, 32, 2, data
);
3088 get_win_state(xcb_window_t w
)
3090 xcb_get_property_reply_t
*r
;
3091 xcb_get_property_cookie_t c
;
3092 uint32_t result
= 0;
3094 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
3095 r
= xcb_get_property_reply(conn
, c
, NULL
);
3097 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
3098 result
= *((uint32_t *)xcb_get_property_value(r
));
3102 DNPRINTF(SWM_D_MISC
, "get_win_state property: win %#x state %u\n", w
,
3108 version(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3110 struct swm_region
*tmpr
;
3113 /* suppress unused warnings since vars are needed */
3118 bar_version
= !bar_version
;
3120 snprintf(bar_vertext
, sizeof bar_vertext
,
3121 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
3123 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
3125 num_screens
= get_screen_count();
3126 for (i
= 0; i
< num_screens
; i
++) {
3127 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
3128 bar_draw(tmpr
->bar
);
3135 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
3137 xcb_client_message_event_t ev
;
3145 name
= get_atom_name(a
);
3146 DNPRINTF(SWM_D_EVENT
, "client_msg: win %#x, atom: %s(%u), "
3148 win
->id
, name
, a
, t
);
3152 bzero(&ev
, sizeof ev
);
3153 ev
.response_type
= XCB_CLIENT_MESSAGE
;
3154 ev
.window
= win
->id
;
3157 ev
.data
.data32
[0] = a
;
3158 ev
.data
.data32
[1] = t
;
3160 xcb_send_event(conn
, 0, win
->id
,
3161 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
3164 /* synthetic response to a ConfigureRequest when not making a change */
3166 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
3168 xcb_configure_notify_event_t ce
;
3173 /* send notification of unchanged state. */
3174 bzero(&ce
, sizeof(ce
));
3175 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
3178 ce
.width
= WIDTH(win
);
3179 ce
.height
= HEIGHT(win
);
3180 ce
.border_width
= 0;
3181 ce
.override_redirect
= 0;
3186 ce
.window
= win
->id
;
3187 ce
.above_sibling
= XCB_WINDOW_NONE
;
3190 ce
.event
= ev
->window
;
3191 ce
.window
= ev
->window
;
3193 /* make response appear more WM_SIZE_HINTS-compliant */
3194 if (win
->sh
.flags
) {
3195 DNPRINTF(SWM_D_MISC
, "config_win: hints: win %#x,"
3196 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
3197 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
3198 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
3199 SH_INC_W(win
), SH_INC_H(win
));
3204 /* the hint may be set... to 0! */
3205 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
3206 ce
.width
= SH_MIN_W(win
);
3207 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
3208 ce
.height
= SH_MIN_H(win
);
3213 /* may also be advertized as 0 */
3214 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
3215 ce
.width
= SH_MAX_W(win
);
3216 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
3217 ce
.height
= SH_MAX_H(win
);
3220 /* resize increment. */
3222 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
3223 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
3225 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
3226 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
3230 /* adjust x and y for requested border_width. */
3231 ce
.x
+= ev
->border_width
;
3232 ce
.y
+= ev
->border_width
;
3234 ce
.above_sibling
= ev
->sibling
;
3237 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
3238 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
3239 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
3241 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
3246 count_win(struct workspace
*ws
, bool count_transient
)
3251 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3252 if (!count_transient
&& FLOATING(win
))
3258 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
3264 quit(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3266 /* suppress unused warnings since vars are needed */
3271 DNPRINTF(SWM_D_MISC
, "quit\n");
3276 lower_window(struct ws_win
*win
)
3278 struct ws_win
*target
= NULL
;
3279 struct workspace
*ws
;
3281 DNPRINTF(SWM_D_EVENT
, "lower_window: win %#x\n", WINID(win
));
3288 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3289 if (target
== win
|| ICONIC(target
))
3291 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3294 if (win
->transient
== target
->transient
)
3296 if (win
->transient
== target
->id
)
3299 if (FULLSCREEN(target
))
3301 if (FULLSCREEN(win
))
3303 if (MAXIMIZED(target
))
3307 if (ABOVE(target
) || TRANS(target
))
3309 if (ABOVE(win
) || TRANS(win
))
3313 /* Change stack position. */
3314 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3316 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3318 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3320 update_win_stacking(win
);
3323 if (swm_debug
& SWM_D_STACK
) {
3324 DPRINTF("=== stacking order (top down) === \n");
3325 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3326 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3327 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3328 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3329 YESNO(ICONIC(target
)));
3333 DNPRINTF(SWM_D_EVENT
, "lower_window: done\n");
3337 raise_window(struct ws_win
*win
)
3339 struct ws_win
*target
= NULL
;
3340 struct workspace
*ws
;
3342 DNPRINTF(SWM_D_EVENT
, "raise_window: win %#x\n", WINID(win
));
3349 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3350 if (target
== win
|| ICONIC(target
))
3352 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3354 if (TRANS(win
) && (win
->transient
== target
->transient
||
3355 win
->transient
== target
->id
))
3357 if (FULLSCREEN(win
))
3359 if (FULLSCREEN(target
))
3363 if (MAXIMIZED(target
))
3365 if (ABOVE(win
) || TRANS(win
) ||
3366 (win
->ws
->focus
== win
&& ws
->always_raise
))
3368 if (!ABOVE(target
) && !TRANS(target
))
3372 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3374 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3376 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3378 update_win_stacking(win
);
3381 if (swm_debug
& SWM_D_STACK
) {
3382 DPRINTF("=== stacking order (top down) === \n");
3383 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3384 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3385 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3386 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3387 YESNO(ICONIC(target
)));
3391 DNPRINTF(SWM_D_EVENT
, "raise_window: done\n");
3395 update_win_stacking(struct ws_win
*win
)
3397 struct ws_win
*sibling
;
3401 struct swm_region
*r
;
3404 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
3407 if (win
->frame
== XCB_WINDOW_NONE
) {
3408 DNPRINTF(SWM_D_EVENT
, "update_window_stacking: win %#x not "
3409 "reparented.\n", win
->id
);
3413 sibling
= TAILQ_NEXT(win
, stack_entry
);
3414 if (sibling
!= NULL
&& (FLOATING(win
) == FLOATING(sibling
) ||
3415 (win
->ws
->always_raise
&& win
->ws
->focus
== win
)))
3416 val
[0] = sibling
->frame
;
3417 else if (FLOATING(win
) || (win
->ws
->always_raise
&&
3418 win
->ws
->focus
== win
))
3419 val
[0] = r
->bar
->id
;
3423 DNPRINTF(SWM_D_EVENT
, "update_win_stacking: win %#x (%#x), "
3424 "sibling %#x\n", win
->frame
, win
->id
, val
[0]);
3426 val
[1] = XCB_STACK_MODE_ABOVE
;
3428 xcb_configure_window(conn
, win
->frame
, XCB_CONFIG_WINDOW_SIBLING
|
3429 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
3432 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
)
3438 map_window(struct ws_win
*win
)
3443 DNPRINTF(SWM_D_EVENT
, "map_window: win %#x, mapped: %s\n",
3444 win
->id
, YESNO(win
->mapped
));
3449 xcb_map_window(conn
, win
->frame
);
3450 xcb_map_window(conn
, win
->id
);
3451 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
3456 unmap_window(struct ws_win
*win
)
3461 DNPRINTF(SWM_D_EVENT
, "unmap_window: win %#x, mapped: %s\n", win
->id
,
3462 YESNO(win
->mapped
));
3467 xcb_unmap_window(conn
, win
->id
);
3468 xcb_unmap_window(conn
, win
->frame
);
3469 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
3470 win
->mapped
= false;
3477 int i
, j
, num_screens
;
3479 num_screens
= get_screen_count();
3480 for (i
= 0; i
< num_screens
; i
++)
3481 for (j
= 0; j
< workspace_limit
; j
++)
3482 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3487 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
3489 xcb_key_press_event_t event
;
3490 xcb_keycode_t
*keycode
;
3495 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
3497 DNPRINTF(SWM_D_MISC
, "fake_keypress: win %#x, keycode %u\n",
3500 bzero(&event
, sizeof(event
));
3501 event
.event
= win
->id
;
3502 event
.root
= win
->s
->root
;
3503 event
.child
= XCB_WINDOW_NONE
;
3504 event
.time
= XCB_CURRENT_TIME
;
3505 event
.event_x
= X(win
);
3506 event
.event_y
= Y(win
);
3509 event
.same_screen
= 1;
3510 event
.detail
= *keycode
;
3511 event
.state
= modifiers
;
3513 event
.response_type
= XCB_KEY_PRESS
;
3514 xcb_send_event(conn
, 1, win
->id
,
3515 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
3517 event
.response_type
= XCB_KEY_RELEASE
;
3518 xcb_send_event(conn
, 1, win
->id
,
3519 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
3525 restart(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3527 /* suppress unused warning since var is needed */
3532 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
3536 execvp(start_argv
[0], start_argv
);
3537 warn("execvp failed");
3538 quit(NULL
, NULL
, NULL
);
3542 get_pointer_win(xcb_window_t root
)
3544 struct ws_win
*win
= NULL
;
3545 xcb_query_pointer_reply_t
*r
;
3547 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: %#x.\n", root
);
3549 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
3551 win
= find_window(r
->child
);
3553 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: %#x.\n",
3556 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
3565 center_pointer(struct swm_region
*r
)
3569 if (!warp_pointer
|| r
== NULL
)
3574 DNPRINTF(SWM_D_EVENT
, "center_pointer: win %#x.\n", WINID(win
));
3576 if (win
&& win
->mapped
)
3577 xcb_warp_pointer(conn
, XCB_NONE
, win
->frame
, 0, 0, 0, 0,
3578 WIDTH(win
) / 2, HEIGHT(win
) / 2);
3580 xcb_warp_pointer(conn
, XCB_NONE
, r
->id
, 0, 0, 0, 0,
3581 WIDTH(r
) / 2, HEIGHT(r
) / 2);
3585 root_to_region(xcb_window_t root
, int check
)
3588 struct swm_region
*r
= NULL
;
3590 xcb_query_pointer_reply_t
*qpr
;
3591 xcb_get_input_focus_reply_t
*gifr
;
3593 DNPRINTF(SWM_D_MISC
, "root_to_region: win %#x\n", root
);
3595 num_screens
= get_screen_count();
3596 for (i
= 0; i
< num_screens
; i
++)
3597 if (screens
[i
].root
== root
)
3600 if (check
& SWM_CK_REGION
)
3601 r
= screens
[i
].r_focus
;
3603 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
3604 /* Try to find an actively focused window */
3605 gifr
= xcb_get_input_focus_reply(conn
,
3606 xcb_get_input_focus(conn
), NULL
);
3608 cfw
= find_window(gifr
->focus
);
3609 if (cfw
&& cfw
->ws
->r
)
3616 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
3617 /* No region with an active focus; try to use pointer. */
3618 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
3619 screens
[i
].root
), NULL
);
3621 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
3622 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
3623 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3624 if (X(r
) <= qpr
->root_x
&&
3625 qpr
->root_x
< MAX_X(r
) &&
3626 Y(r
) <= qpr
->root_y
&&
3627 qpr
->root_y
< MAX_Y(r
))
3634 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
3635 r
= TAILQ_FIRST(&screens
[i
].rl
);
3637 DNPRINTF(SWM_D_MISC
, "root_to_region: idx: %d\n", get_region_index(r
));
3643 find_region(xcb_window_t id
)
3645 struct swm_region
*r
;
3648 num_screens
= get_screen_count();
3649 for (i
= 0; i
< num_screens
; i
++)
3650 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3658 find_bar(xcb_window_t id
)
3660 struct swm_region
*r
;
3663 num_screens
= get_screen_count();
3664 for (i
= 0; i
< num_screens
; i
++)
3665 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3666 if (r
->bar
&& r
->bar
->id
== id
)
3673 find_frame_window(xcb_window_t id
) {
3674 struct swm_region
*r
;
3678 num_screens
= get_screen_count();
3679 for (i
= 0; i
< num_screens
; i
++)
3680 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3681 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
3689 find_window(xcb_window_t id
)
3691 struct ws_win
*win
= NULL
;
3692 int i
, j
, num_screens
;
3693 xcb_query_tree_reply_t
*qtr
;
3695 DNPRINTF(SWM_D_MISC
, "find_window: id: %#x\n", id
);
3697 num_screens
= get_screen_count();
3698 for (i
= 0; i
< num_screens
; i
++)
3699 for (j
= 0; j
< workspace_limit
; j
++)
3700 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3701 if (id
== win
->id
|| id
== win
->frame
)
3705 /* If window isn't top-level, try to find managed ancestor. */
3706 qtr
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3708 if (qtr
->parent
!= XCB_WINDOW_NONE
&& qtr
->parent
!= qtr
->root
)
3709 win
= find_window(qtr
->parent
);
3713 DNPRINTF(SWM_D_MISC
, "find_window: found child %#x "
3714 "of %#x.\n", win
->id
, qtr
->parent
);
3724 find_unmanaged_window(xcb_window_t id
)
3727 int i
, j
, num_screens
;
3729 num_screens
= get_screen_count();
3730 for (i
= 0; i
< num_screens
; i
++)
3731 for (j
= 0; j
< workspace_limit
; j
++)
3732 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
3740 spawn(int ws_idx
, union arg
*args
, bool close_fd
)
3745 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3747 close(xcb_get_file_descriptor(conn
));
3749 setenv("LD_PRELOAD", SWM_LIB
, 1);
3751 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3752 warn("spawn: asprintf SWM_WS");
3755 setenv("_SWM_WS", ret
, 1);
3759 if (asprintf(&ret
, "%d", getpid()) == -1) {
3760 warn("spawn: asprintf _SWM_PID");
3763 setenv("_SWM_PID", ret
, 1);
3767 if (setsid() == -1) {
3768 warn("spawn: setsid");
3774 * close stdin and stdout to prevent interaction between apps
3775 * and the baraction script
3776 * leave stderr open to record errors
3778 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3779 warn("spawn: open");
3782 dup2(fd
, STDIN_FILENO
);
3783 dup2(fd
, STDOUT_FILENO
);
3788 execvp(args
->argv
[0], args
->argv
);
3790 warn("spawn: execvp");
3795 kill_refs(struct ws_win
*win
)
3797 struct workspace
*ws
;
3799 int i
, j
, num_screens
;
3804 num_screens
= get_screen_count();
3805 for (i
= 0; i
< num_screens
; i
++) {
3806 for (j
= 0; j
< workspace_limit
; j
++) {
3807 ws
= &screens
[i
].ws
[j
];
3809 if (win
== ws
->focus
)
3811 if (win
== ws
->focus_prev
)
3812 ws
->focus_prev
= NULL
;
3813 if (win
== ws
->focus_pending
)
3814 ws
->focus_pending
= NULL
;
3817 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3818 if (win
== w
->focus_child
)
3819 w
->focus_child
= NULL
;
3825 validate_win(struct ws_win
*testwin
)
3828 struct workspace
*ws
;
3829 struct swm_region
*r
;
3830 int i
, x
, num_screens
;
3832 if (testwin
== NULL
)
3835 num_screens
= get_screen_count();
3836 for (i
= 0; i
< num_screens
; i
++)
3837 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3838 for (x
= 0; x
< workspace_limit
; x
++) {
3840 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3848 validate_ws(struct workspace
*testws
)
3850 struct swm_region
*r
;
3851 struct workspace
*ws
;
3852 int i
, x
, num_screens
;
3854 /* validate all ws */
3855 num_screens
= get_screen_count();
3856 for (i
= 0; i
< num_screens
; i
++)
3857 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3858 for (x
= 0; x
< workspace_limit
; x
++) {
3867 unfocus_win(struct ws_win
*win
)
3869 xcb_window_t none
= XCB_WINDOW_NONE
;
3871 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: win %#x\n", WINID(win
));
3876 if (win
->ws
== NULL
) {
3877 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3881 if (validate_ws(win
->ws
)) {
3882 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3886 if (win
->ws
->r
== NULL
) {
3887 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3891 if (validate_win(win
)) {
3892 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3897 if (win
->ws
->focus
== win
) {
3898 win
->ws
->focus
= NULL
;
3899 win
->ws
->focus_prev
= win
;
3902 if (validate_win(win
->ws
->focus
)) {
3903 kill_refs(win
->ws
->focus
);
3904 win
->ws
->focus
= NULL
;
3907 if (validate_win(win
->ws
->focus_prev
)) {
3908 kill_refs(win
->ws
->focus_prev
);
3909 win
->ws
->focus_prev
= NULL
;
3914 /* Raise window to "top unfocused position." */
3915 if (win
->ws
->always_raise
)
3918 /* Update border width */
3919 if (win
->bordered
&& (win
->quirks
& SWM_Q_MINIMALBORDER
) &&
3922 X(win
) += border_width
;
3923 Y(win
) += border_width
;
3927 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3928 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3930 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3934 focus_win(struct ws_win
*win
)
3936 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
, *tmpw
;
3937 struct workspace
*ws
;
3938 xcb_get_input_focus_reply_t
*gifr
= NULL
;
3939 xcb_get_window_attributes_reply_t
*war
= NULL
;
3941 DNPRINTF(SWM_D_FOCUS
, "focus_win: win %#x\n", WINID(win
));
3943 if (win
== NULL
|| win
->ws
== NULL
|| !win
->mapped
)
3948 if (validate_ws(ws
))
3951 if (validate_win(win
)) {
3956 gifr
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3958 DNPRINTF(SWM_D_FOCUS
, "focus_win: cur focus: %#x\n",
3961 cfw
= find_window(gifr
->focus
);
3964 if (cfw
->ws
!= ws
&& cfw
->ws
->r
!= NULL
&&
3965 cfw
->frame
!= XCB_WINDOW_NONE
) {
3972 war
= xcb_get_window_attributes_reply(conn
,
3973 xcb_get_window_attributes(conn
, gifr
->focus
), NULL
);
3974 if (war
&& war
->override_redirect
&& ws
->focus
== win
) {
3975 DNPRINTF(SWM_D_FOCUS
, "focus_win: skip refocus "
3976 "from override_redirect.\n");
3982 if (ws
->focus
!= win
) {
3983 if (ws
->focus
&& ws
->focus
!= cfw
)
3984 unfocus_win(ws
->focus
);
3988 /* Clear focus child redirect. */
3989 win
->focus_child
= NULL
;
3991 /* If transient, adjust parent's focus child for focus_magic. */
3993 parent
= find_window(win
->transient
);
3994 if (parent
&& parent
->focus_child
!= win
)
3995 parent
->focus_child
= win
;
3998 /* Update window border even if workspace is hidden. */
4002 DNPRINTF(SWM_D_FOCUS
, "focus_win: already focused.\n");
4007 /* Set input focus if no input hint, or indicated by hint. */
4008 if (ACCEPTS_FOCUS(win
)) {
4009 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
4010 " revert-to: parent, time: %#x\n", win
->id
,
4012 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
4013 win
->id
, last_event_time
);
4014 } else if (!win
->take_focus
) {
4015 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
4016 " revert-to: parent, time: 0\n", ws
->r
->id
);
4017 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
4018 ws
->r
->id
, XCB_CURRENT_TIME
);
4021 /* Tell app it can adjust focus to a specific window. */
4022 if (win
->take_focus
) {
4023 /* java is special; always tell parent */
4024 if (TRANS(win
) && win
->java
)
4025 client_msg(parent
, a_takefocus
,
4028 client_msg(win
, a_takefocus
, last_event_time
);
4031 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
4033 /* If a parent exists, map it first. */
4035 raise_window(parent
);
4038 /* Map siblings next. */
4039 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
,
4041 if (w
!= win
&& !ICONIC(w
) &&
4042 w
->transient
== parent
->id
) {
4048 /* Map focused window. */
4052 /* Stack any children of focus window. */
4053 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
4054 if (w
->transient
== win
->id
&& !ICONIC(w
)) {
4058 } else if (tile_gap
< 0 && !ABOVE(win
)) {
4060 * Windows overlap in the layout.
4061 * Raise focused win above all tiled wins.
4069 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4070 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4073 bar_draw(ws
->r
->bar
);
4079 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
4082 /* If a child window should have focus instead, return it. */
4084 get_focus_magic(struct ws_win
*win
)
4086 struct ws_win
*parent
= NULL
;
4087 struct ws_win
*child
= NULL
;
4089 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: win %#x\n", WINID(win
));
4094 parent
= find_window(win
->transient
);
4096 /* If parent prefers focus elsewhere, then try to do so. */
4097 if (parent
&& (child
= parent
->focus_child
)) {
4098 if (validate_win(child
) == 0 && child
->mapped
)
4101 parent
->focus_child
= NULL
;
4105 /* If this window prefers focus elsewhere, then try to do so. */
4106 if ((child
= win
->focus_child
)) {
4107 if (validate_win(child
) == 0 && child
->mapped
)
4110 win
->focus_child
= NULL
;
4117 event_drain(uint8_t rt
)
4119 xcb_generic_event_t
*evt
;
4121 /* ensure all pending requests have been processed before filtering. */
4123 while ((evt
= get_next_event(false))) {
4124 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
4132 set_region(struct swm_region
*r
)
4134 struct swm_region
*rf
;
4141 /* Unfocus old region bar. */
4146 xcb_change_window_attributes(conn
, rf
->bar
->id
,
4147 XCB_CW_BORDER_PIXEL
,
4148 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
4151 if (rf
!= NULL
&& rf
!= r
&& (X(rf
) != X(r
) || Y(rf
) != Y(r
) ||
4152 WIDTH(rf
) != WIDTH(r
) || HEIGHT(rf
) != HEIGHT(r
))) {
4153 /* Set _NET_DESKTOP_GEOMETRY. */
4155 vals
[1] = HEIGHT(r
);
4156 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, r
->s
->root
,
4157 ewmh
[_NET_DESKTOP_GEOMETRY
].atom
, XCB_ATOM_CARDINAL
, 32, 2,
4161 /* Set region bar border to focus_color. */
4162 xcb_change_window_attributes(conn
, r
->bar
->id
,
4163 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
4167 ewmh_update_current_desktop();
4171 focus_region(struct swm_region
*r
)
4174 struct swm_region
*old_r
;
4179 old_r
= r
->s
->r_focus
;
4182 nfw
= get_region_focus(r
);
4186 /* New region is empty; need to manually unfocus win. */
4188 unfocus_win(old_r
->ws
->focus
);
4189 /* Clear bar since empty. */
4190 bar_draw(old_r
->bar
);
4193 DNPRINTF(SWM_D_FOCUS
, "focus_region: set_input_focus: %#x, "
4194 "revert-to: parent, time: 0\n", r
->id
);
4195 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4202 switchws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4204 struct swm_region
*this_r
, *other_r
;
4206 struct workspace
*new_ws
, *old_ws
;
4207 xcb_window_t none
= XCB_WINDOW_NONE
;
4208 int wsid
= args
->id
;
4209 bool unmap_old
= false;
4216 if (wsid
>= workspace_limit
)
4220 old_ws
= this_r
->ws
;
4221 new_ws
= &this_r
->s
->ws
[wsid
];
4223 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
4224 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
4226 if (new_ws
== NULL
|| old_ws
== NULL
)
4228 if (new_ws
== old_ws
)
4231 other_r
= new_ws
->r
;
4232 if (other_r
&& workspace_clamp
) {
4233 DNPRINTF(SWM_D_WS
, "switchws: ws clamped.\n");
4236 DNPRINTF(SWM_D_WS
, "switchws: warping focus to region "
4237 "with ws %d.\n", wsid
);
4238 focus_region(other_r
);
4239 center_pointer(other_r
);
4245 if ((win
= old_ws
->focus
) != NULL
) {
4248 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4249 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4254 /* the other ws is visible in another region, exchange them */
4255 other_r
->ws_prior
= new_ws
;
4256 other_r
->ws
= old_ws
;
4257 old_ws
->r
= other_r
;
4259 /* the other workspace is hidden, hide this one */
4264 this_r
->ws_prior
= old_ws
;
4265 this_r
->ws
= new_ws
;
4268 /* Set focus_pending before stacking, if needed. */
4269 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
4270 validate_win(new_ws
->focus_pending
))) {
4271 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
4272 new_ws
->focus
= new_ws
->focus_prev
;
4273 new_ws
->focus_prev
= NULL
;
4276 new_ws
->state
= SWM_WS_STATE_MAPPING
;
4281 /* unmap old windows */
4283 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
4285 old_ws
->state
= SWM_WS_STATE_HIDDEN
;
4288 /* if workspaces were swapped, then don't wait to set focus */
4289 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
4290 if (new_ws
->focus_pending
) {
4291 focus_win(new_ws
->focus_pending
);
4292 new_ws
->focus_pending
= NULL
;
4296 /* Clear bar and set focus on region input win if new ws is empty. */
4297 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
4298 DNPRINTF(SWM_D_FOCUS
, "switchws: set_input_focus: %#x, "
4299 "revert-to: parent, time: 0\n", r
->id
);
4300 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4305 ewmh_update_current_desktop();
4309 new_ws
->state
= SWM_WS_STATE_MAPPED
;
4311 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
4315 cyclews(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4318 struct swm_screen
*s
= r
->s
;
4319 bool cycle_all
= false, mv
= false;
4321 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4322 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4328 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
4331 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
4334 case SWM_ARG_ID_CYCLEWS_UP
:
4335 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
4337 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
4340 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
4343 case SWM_ARG_ID_CYCLEWS_DOWN
:
4344 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
4351 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
4353 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
4357 send_to_ws(bp
, r
, &a
);
4359 switchws(bp
, r
, &a
);
4360 } while (a
.id
!= r
->ws
->idx
);
4362 DNPRINTF(SWM_D_FOCUS
, "cyclews: done\n");
4366 priorws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4372 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4373 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4375 if (r
->ws_prior
== NULL
)
4378 a
.id
= r
->ws_prior
->idx
;
4379 switchws(bp
, r
, &a
);
4380 DNPRINTF(SWM_D_FOCUS
, "priorws: done\n");
4384 focusrg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4386 int ridx
= args
->id
, i
, num_screens
;
4387 struct swm_region
*rr
= NULL
;
4391 num_screens
= get_screen_count();
4392 /* do nothing if we don't have more than one screen */
4393 if (!(num_screens
> 1 || outputs
> 1))
4396 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
4398 rr
= TAILQ_FIRST(&r
->s
->rl
);
4399 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
4400 rr
= TAILQ_NEXT(rr
, entry
);
4408 DNPRINTF(SWM_D_FOCUS
, "focusrg: done\n");
4412 cyclerg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4415 struct swm_region
*rr
= NULL
;
4418 num_screens
= get_screen_count();
4419 /* do nothing if we don't have more than one screen */
4420 if (!(num_screens
> 1 || outputs
> 1))
4424 DNPRINTF(SWM_D_FOCUS
, "cyclerg: id: %d, region: %d\n", args
->id
, i
);
4427 case SWM_ARG_ID_CYCLERG_UP
:
4428 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4429 rr
= TAILQ_NEXT(r
, entry
);
4431 rr
= TAILQ_FIRST(&screens
[i
].rl
);
4433 case SWM_ARG_ID_CYCLERG_DOWN
:
4434 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4435 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
4437 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
4446 case SWM_ARG_ID_CYCLERG_UP
:
4447 case SWM_ARG_ID_CYCLERG_DOWN
:
4452 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4453 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4455 switchws(bp
, r
, &a
);
4461 DNPRINTF(SWM_D_FOCUS
, "cyclerg: done\n");
4464 /* Sorts transients after parent. */
4466 sort_windows(struct ws_win_list
*wl
)
4468 struct ws_win
*win
, *parent
, *nxt
;
4473 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
4474 nxt
= TAILQ_NEXT(win
, entry
);
4476 parent
= find_window(win
->transient
);
4477 if (parent
== NULL
) {
4478 warnx("not possible bug");
4481 TAILQ_REMOVE(wl
, win
, entry
);
4482 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
4488 swapwin(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4490 struct ws_win
*target
, *source
;
4491 struct ws_win
*cur_focus
;
4492 struct ws_win_list
*wl
;
4496 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4497 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4499 cur_focus
= r
->ws
->focus
;
4500 if (cur_focus
== NULL
|| FULLSCREEN(cur_focus
))
4503 /* Adjust stacking in floating layer. */
4504 if (ABOVE(cur_focus
)) {
4506 case SWM_ARG_ID_SWAPPREV
:
4507 target
= TAILQ_PREV(cur_focus
, ws_win_stack
,
4509 if (target
!= NULL
&& FLOATING(target
)) {
4510 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4512 TAILQ_INSERT_BEFORE(target
, cur_focus
,
4514 update_win_stacking(cur_focus
);
4518 case SWM_ARG_ID_SWAPNEXT
:
4519 target
= TAILQ_NEXT(cur_focus
, stack_entry
);
4520 if (target
!= NULL
&& FLOATING(target
)) {
4521 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4523 TAILQ_INSERT_AFTER(&cur_focus
->ws
->stack
,
4524 target
, cur_focus
, stack_entry
);
4525 update_win_stacking(cur_focus
);
4533 if (r
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4536 clear_maximized(r
->ws
);
4539 wl
= &source
->ws
->winlist
;
4542 case SWM_ARG_ID_SWAPPREV
:
4544 source
= find_window(source
->transient
);
4545 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
4546 if (target
&& target
->transient
)
4547 target
= find_window(target
->transient
);
4548 TAILQ_REMOVE(wl
, source
, entry
);
4550 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4552 TAILQ_INSERT_BEFORE(target
, source
, entry
);
4554 case SWM_ARG_ID_SWAPNEXT
:
4555 target
= TAILQ_NEXT(source
, entry
);
4556 /* move the parent and let the sort handle the move */
4558 source
= find_window(source
->transient
);
4559 TAILQ_REMOVE(wl
, source
, entry
);
4561 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4563 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
4565 case SWM_ARG_ID_SWAPMAIN
:
4566 target
= TAILQ_FIRST(wl
);
4567 if (target
== source
) {
4568 if (source
->ws
->focus_prev
!= NULL
&&
4569 source
->ws
->focus_prev
!= target
)
4570 source
= source
->ws
->focus_prev
;
4574 if (target
== NULL
|| source
== NULL
)
4576 source
->ws
->focus_prev
= target
;
4577 TAILQ_REMOVE(wl
, target
, entry
);
4578 TAILQ_INSERT_BEFORE(source
, target
, entry
);
4579 TAILQ_REMOVE(wl
, source
, entry
);
4580 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4582 case SWM_ARG_ID_MOVELAST
:
4583 TAILQ_REMOVE(wl
, source
, entry
);
4584 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4587 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
4592 ewmh_update_client_list();
4598 DNPRINTF(SWM_D_MOVE
, "swapwin: done\n");
4602 get_focus_prev(struct ws_win
*win
)
4604 struct ws_win
*winfocus
= NULL
;
4605 struct ws_win
*cur_focus
= NULL
;
4606 struct ws_win_list
*wl
= NULL
;
4607 struct workspace
*ws
= NULL
;
4609 if (!(win
&& win
->ws
))
4614 cur_focus
= ws
->focus
;
4616 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: win %#x, cur_focus: %#x, "
4617 "focus_prev: %#x\n", WINID(win
), WINID(cur_focus
),
4618 WINID(ws
->focus_prev
));
4620 /* pickle, just focus on whatever */
4621 if (cur_focus
== NULL
) {
4622 /* use prev_focus if valid */
4623 if (ws
->focus_prev
&& find_window(ws
->focus_prev
->id
))
4624 winfocus
= ws
->focus_prev
;
4628 /* if transient focus on parent */
4629 if (TRANS(cur_focus
)) {
4630 winfocus
= find_window(cur_focus
->transient
);
4634 /* if in max_stack try harder */
4635 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
4636 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
4637 if (cur_focus
!= ws
->focus_prev
)
4638 winfocus
= ws
->focus_prev
;
4640 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
4645 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
4647 if (winfocus
== NULL
|| winfocus
== win
) {
4648 switch (focus_close
) {
4649 case SWM_STACK_BOTTOM
:
4650 TAILQ_FOREACH(winfocus
, wl
, entry
)
4651 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4655 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4656 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4659 case SWM_STACK_ABOVE
:
4660 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
4661 while (winfocus
&& ICONIC(winfocus
))
4662 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4664 if (winfocus
== NULL
) {
4665 if (focus_close_wrap
) {
4666 TAILQ_FOREACH(winfocus
, wl
, entry
)
4667 if (!ICONIC(winfocus
) &&
4668 winfocus
!= cur_focus
)
4671 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4673 if (!ICONIC(winfocus
) &&
4674 winfocus
!= cur_focus
)
4679 case SWM_STACK_BELOW
:
4680 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
4681 while (winfocus
&& ICONIC(winfocus
))
4682 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
4685 if (winfocus
== NULL
) {
4686 if (focus_close_wrap
) {
4687 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4689 if (!ICONIC(winfocus
) &&
4690 winfocus
!= cur_focus
)
4693 TAILQ_FOREACH(winfocus
, wl
, entry
)
4694 if (!ICONIC(winfocus
) &&
4695 winfocus
!= cur_focus
)
4703 if (winfocus
== NULL
||
4704 (winfocus
&& (ICONIC(winfocus
) || winfocus
== cur_focus
))) {
4705 if (focus_default
== SWM_STACK_TOP
) {
4706 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4707 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4710 TAILQ_FOREACH(winfocus
, wl
, entry
)
4711 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4718 return get_focus_magic(winfocus
);
4722 get_region_focus(struct swm_region
*r
)
4724 struct ws_win
*winfocus
= NULL
;
4729 if (r
->ws
->focus
&& !ICONIC(r
->ws
->focus
))
4730 winfocus
= r
->ws
->focus
;
4731 else if (r
->ws
->focus_prev
&& !ICONIC(r
->ws
->focus_prev
))
4732 winfocus
= r
->ws
->focus_prev
;
4734 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
4735 if (!ICONIC(winfocus
))
4738 return get_focus_magic(winfocus
);
4742 focus(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4744 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
4745 struct ws_win_list
*wl
= NULL
;
4746 struct workspace
*ws
= NULL
;
4753 cur_focus
= r
->ws
->focus
;
4757 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d, cur_focus: %#x\n", args
->id
,
4760 /* Make sure an uniconified window has focus, if one exists. */
4761 if (cur_focus
== NULL
) {
4762 cur_focus
= TAILQ_FIRST(wl
);
4763 while (cur_focus
!= NULL
&& ICONIC(cur_focus
))
4764 cur_focus
= TAILQ_NEXT(cur_focus
, entry
);
4766 DNPRINTF(SWM_D_FOCUS
, "focus: new cur_focus: %#x\n",
4771 case SWM_ARG_ID_FOCUSPREV
:
4772 if (cur_focus
== NULL
)
4775 winfocus
= cur_focus
;
4777 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
4778 if (winfocus
== NULL
)
4779 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
4780 if (winfocus
== cur_focus
)
4782 } while (winfocus
&& (ICONIC(winfocus
) ||
4783 winfocus
->id
== cur_focus
->transient
||
4784 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4785 winfocus
->transient
== cur_focus
->transient
) ||
4786 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4788 case SWM_ARG_ID_FOCUSNEXT
:
4789 if (cur_focus
== NULL
)
4792 winfocus
= cur_focus
;
4794 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4795 if (winfocus
== NULL
)
4796 winfocus
= TAILQ_FIRST(wl
);
4797 if (winfocus
== cur_focus
)
4799 } while (winfocus
&& (ICONIC(winfocus
) ||
4800 winfocus
->id
== cur_focus
->transient
||
4801 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4802 winfocus
->transient
== cur_focus
->transient
) ||
4803 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4805 case SWM_ARG_ID_FOCUSMAIN
:
4806 if (cur_focus
== NULL
)
4809 winfocus
= TAILQ_FIRST(wl
);
4810 if (winfocus
== cur_focus
)
4811 winfocus
= cur_focus
->ws
->focus_prev
;
4813 case SWM_ARG_ID_FOCUSURGENT
:
4814 /* Search forward for the next urgent window. */
4818 for (i
= 0; i
<= workspace_limit
; ++i
) {
4820 head
= TAILQ_FIRST(&r
->s
->ws
[(ws
->idx
+ i
) %
4821 workspace_limit
].winlist
);
4824 if (head
== cur_focus
) {
4826 winfocus
= cur_focus
;
4829 } else if (get_urgent(head
)) {
4834 head
= TAILQ_NEXT(head
, entry
);
4841 /* Switch ws if new focus is on a different ws. */
4842 if (winfocus
&& winfocus
->ws
!= ws
) {
4843 a
.id
= winfocus
->ws
->idx
;
4844 switchws(bp
, r
, &a
);
4851 if (clear_maximized(ws
) > 0)
4854 focus_win(get_focus_magic(winfocus
));
4859 DNPRINTF(SWM_D_FOCUS
, "focus: done\n");
4863 focus_pointer(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4867 /* Not needed for buttons since this is already done in buttonpress. */
4868 if (bp
->type
== KEYBIND
) {
4869 focus_win(get_pointer_win(r
->s
->root
));
4875 cycle_layout(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4877 struct workspace
*ws
= r
->ws
;
4879 /* suppress unused warning since var is needed */
4883 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
4886 if (ws
->cur_layout
->l_stack
== NULL
)
4887 ws
->cur_layout
= &layouts
[0];
4889 clear_maximized(ws
);
4894 focus_win(get_region_focus(r
));
4901 stack_config(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4903 struct workspace
*ws
= r
->ws
;
4907 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
4910 if (clear_maximized(ws
) > 0)
4913 if (ws
->cur_layout
->l_config
!= NULL
)
4914 ws
->cur_layout
->l_config(ws
, args
->id
);
4916 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
4925 stack(struct swm_region
*r
) {
4926 struct swm_geometry g
;
4927 struct swm_region
*r_prev
;
4933 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
4935 /* Adjust stack area for region bar and padding. */
4937 g
.x
+= region_padding
;
4938 g
.y
+= region_padding
;
4939 g
.w
-= 2 * border_width
+ 2 * region_padding
;
4940 g
.h
-= 2 * border_width
+ 2 * region_padding
;
4941 if (bar_enabled
&& r
->ws
->bar_enabled
) {
4947 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d (screen: "
4948 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4949 r
->ws
->idx
, r
->s
->idx
, get_region_index(r
), g
.x
, g
.y
, g
.w
, g
.h
);
4951 r_prev
= TAILQ_PREV(r
, swm_region_list
, entry
);
4953 /* Stack bar/input relative to prev. region. */
4954 val
[1] = XCB_STACK_MODE_ABOVE
;
4956 val
[0] = r_prev
->id
;
4957 DNPRINTF(SWM_D_STACK
, "stack: region input %#x "
4958 "relative to %#x.\n", r
->id
, val
[0]);
4959 xcb_configure_window(conn
, r
->id
,
4960 XCB_CONFIG_WINDOW_SIBLING
|
4961 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4963 val
[0] = r_prev
->bar
->id
;
4964 DNPRINTF(SWM_D_STACK
, "stack: region bar %#x "
4965 "relative to %#x.\n", r
->bar
->id
, val
[0]);
4966 xcb_configure_window(conn
, r
->bar
->id
,
4967 XCB_CONFIG_WINDOW_SIBLING
|
4968 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4971 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
4972 r
->ws
->cur_layout
->l_string(r
->ws
);
4973 /* save r so we can track region changes */
4979 DNPRINTF(SWM_D_STACK
, "stack: end\n");
4983 store_float_geom(struct ws_win
*win
)
4985 if (win
== NULL
|| win
->ws
->r
== NULL
)
4988 /* retain window geom and region geom */
4989 win
->g_float
= win
->g
;
4990 win
->g_float
.x
-= X(win
->ws
->r
);
4991 win
->g_float
.y
-= Y(win
->ws
->r
);
4992 win
->g_floatvalid
= true;
4993 DNPRINTF(SWM_D_MISC
, "store_float_geom: win %#x, g: (%d,%d)"
4994 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
4995 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
4996 win
->g_float
.w
, win
->g_float
.h
);
5000 load_float_geom(struct ws_win
*win
)
5002 if (win
== NULL
|| win
->ws
->r
== NULL
)
5005 if (win
->g_floatvalid
) {
5006 win
->g
= win
->g_float
;
5007 X(win
) += X(win
->ws
->r
);
5008 Y(win
) += Y(win
->ws
->r
);
5009 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g: (%d,%d)"
5010 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
5013 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g_float "
5014 "is not set.\n", win
->id
);
5019 update_floater(struct ws_win
*win
)
5021 struct workspace
*ws
;
5022 struct swm_region
*r
;
5024 DNPRINTF(SWM_D_MISC
, "update_floater: win %#x\n", WINID(win
));
5031 if ((r
= ws
->r
) == NULL
)
5034 win
->bordered
= true;
5036 if (FULLSCREEN(win
)) {
5037 /* _NET_WM_FULLSCREEN: fullscreen without border. */
5038 if (!win
->g_floatvalid
)
5039 store_float_geom(win
);
5042 win
->bordered
= false;
5043 } else if (MAXIMIZED(win
)) {
5044 /* Maximize: like a single stacked window. */
5045 if (!win
->g_floatvalid
)
5046 store_float_geom(win
);
5050 if (bar_enabled
&& ws
->bar_enabled
&& !maximize_hide_bar
) {
5052 Y(win
) += bar_height
;
5053 HEIGHT(win
) -= bar_height
;
5054 } else if (disable_border
) {
5055 win
->bordered
= false;
5058 if (win
->bordered
) {
5059 /* Window geometry excludes frame. */
5060 X(win
) += border_width
;
5061 Y(win
) += border_width
;
5062 HEIGHT(win
) -= 2 * border_width
;
5063 WIDTH(win
) -= 2 * border_width
;
5066 /* Normal floating window. */
5067 /* Update geometry if new region. */
5069 load_float_geom(win
);
5071 if (((win
->quirks
& SWM_Q_FULLSCREEN
) &&
5072 WIDTH(win
) >= WIDTH(r
) && HEIGHT(win
) >= HEIGHT(r
)) ||
5073 ((!WS_FOCUSED(win
->ws
) || win
->ws
->focus
!= win
) &&
5074 (win
->quirks
& SWM_Q_MINIMALBORDER
))) {
5076 win
->bordered
= false;
5077 } else if (!MANUAL(win
)) {
5078 if (TRANS(win
) && (win
->quirks
& SWM_Q_TRANSSZ
)) {
5079 /* Adjust size on TRANSSZ quirk. */
5080 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
5081 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
5084 if (!(win
->quirks
& SWM_Q_ANYWHERE
)) {
5086 * Floaters and transients are auto-centred
5087 * unless manually moved, resized or ANYWHERE
5090 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2;
5091 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2;
5092 store_float_geom(win
);
5097 /* Ensure at least 1 pixel of the window is in the region. */
5098 region_containment(win
, r
, SWM_CW_ALLSIDES
);
5104 * Send keystrokes to terminal to decrease/increase the font size as the
5105 * window size changes.
5108 adjust_font(struct ws_win
*win
)
5110 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
5111 ABOVE(win
) || TRANS(win
))
5114 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
5115 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
5116 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
5117 win
->font_size_boundary
[win
->font_steps
] =
5118 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
5121 win
->last_inc
= win
->sh
.width_inc
;
5122 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
5123 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
5124 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
5127 win
->last_inc
= win
->sh
.width_inc
;
5128 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
5132 #define SWAPXY(g) do { \
5134 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
5135 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
5138 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, bool flip
)
5140 struct swm_geometry cell
, r_g
= *g
;
5142 int i
= 0, j
= 0, s
= 0, stacks
= 0;
5143 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
5144 int hrh
= 0, extra
= 0, h_slice
= 0, last_h
= 0;
5145 int split
= 0, colno
= 0;
5146 int winno
, mwin
= 0, msize
= 0;
5147 int remain
, missing
, v_slice
, mscale
;
5148 bool bordered
= true, reconfigure
= false;
5151 * cell: geometry for window, including frame.
5152 * mwin: # of windows in master area.
5153 * mscale: size increment of master area.
5154 * stacks: # of stack columns
5157 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
5158 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
5160 memset(&cell
, 0, sizeof(cell
));
5162 /* Prepare tiling variables, if needed. */
5163 if ((winno
= count_win(ws
, false)) > 0) {
5164 /* Find first tiled window. */
5165 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
5166 if (!FLOATING(win
) && !ICONIC(win
))
5169 /* Take into account size hints of first tiled window. */
5171 w_inc
= win
->sh
.width_inc
;
5172 w_base
= win
->sh
.base_width
;
5173 mwin
= ws
->l_state
.horizontal_mwin
;
5174 mscale
= ws
->l_state
.horizontal_msize
;
5175 stacks
= ws
->l_state
.horizontal_stacks
;
5178 w_inc
= win
->sh
.height_inc
;
5179 w_base
= win
->sh
.base_height
;
5180 mwin
= ws
->l_state
.vertical_mwin
;
5181 mscale
= ws
->l_state
.vertical_msize
;
5182 stacks
= ws
->l_state
.vertical_stacks
;
5186 cell
.x
+= border_width
;
5187 cell
.y
+= border_width
;
5189 if (stacks
> winno
- mwin
)
5190 stacks
= winno
- mwin
;
5194 h_slice
= r_g
.h
/ SWM_H_SLICE
;
5195 if (mwin
&& winno
> mwin
) {
5196 v_slice
= r_g
.w
/ SWM_V_SLICE
;
5200 cell
.w
= v_slice
* mscale
;
5202 if (w_inc
> 1 && w_inc
< v_slice
) {
5203 /* Adjust for requested size increment. */
5204 remain
= (cell
.w
- w_base
) % w_inc
;
5210 cell
.x
+= r_g
.w
- msize
;
5213 colno
= split
= winno
/ stacks
;
5214 cell
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
5215 2 * border_width
) / stacks
);
5218 hrh
= r_g
.h
/ colno
;
5219 extra
= r_g
.h
- (colno
* hrh
);
5220 cell
.h
= hrh
- 2 * border_width
;
5221 i
= j
= 0, s
= stacks
;
5224 /* Update window geometry. */
5225 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
5229 if (FLOATING(win
)) {
5230 update_floater(win
);
5235 if (split
&& i
== split
) {
5236 colno
= (winno
- mwin
) / stacks
;
5237 if (s
<= (winno
- mwin
) % stacks
)
5240 hrh
= r_g
.h
/ colno
;
5241 extra
= r_g
.h
- (colno
* hrh
);
5244 cell
.x
+= cell
.w
+ 2 * border_width
+
5247 cell
.w
= (r_g
.w
- msize
-
5248 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
5250 cell
.w
+= (r_g
.w
- msize
-
5251 (stacks
* (2 * border_width
+ tile_gap
))) %
5255 cell
.x
-= cell
.w
+ 2 * border_width
+
5261 cell
.h
= hrh
- 2 * border_width
- tile_gap
;
5264 h_inc
= win
->sh
.width_inc
;
5265 h_base
= win
->sh
.base_width
;
5267 h_inc
= win
->sh
.height_inc
;
5268 h_base
= win
->sh
.base_height
;
5271 if (j
== colno
- 1) {
5272 cell
.h
= hrh
+ extra
;
5273 } else if (h_inc
> 1 && h_inc
< h_slice
) {
5274 /* adjust for window's requested size increment */
5275 remain
= (cell
.h
- h_base
) % h_inc
;
5276 missing
= h_inc
- remain
;
5278 if (missing
<= extra
|| j
== 0) {
5288 cell
.y
= r_g
.y
+ border_width
;
5290 cell
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
5292 /* Window coordinates exclude frame. */
5294 if (winno
> 1 || !disable_border
||
5295 (bar_enabled
&& ws
->bar_enabled
)) {
5302 if (X(win
) != cell
.y
|| Y(win
) != cell
.x
||
5303 WIDTH(win
) != cell
.h
|| HEIGHT(win
) != cell
.w
) {
5307 WIDTH(win
) = cell
.h
;
5308 HEIGHT(win
) = cell
.w
;
5311 if (X(win
) != cell
.x
|| Y(win
) != cell
.y
||
5312 WIDTH(win
) != cell
.w
|| HEIGHT(win
) != cell
.h
) {
5316 WIDTH(win
) = cell
.w
;
5317 HEIGHT(win
) = cell
.h
;
5322 X(win
) -= border_width
;
5323 Y(win
) -= border_width
;
5324 WIDTH(win
) += 2 * border_width
;
5325 HEIGHT(win
) += 2 * border_width
;
5328 if (bordered
!= win
->bordered
) {
5330 win
->bordered
= bordered
;
5343 /* Stack all windows from bottom up. */
5344 TAILQ_FOREACH_REVERSE(win
, &ws
->stack
, ws_win_stack
, stack_entry
)
5346 update_win_stacking(win
);
5348 /* Map all windows from top down. */
5349 TAILQ_FOREACH(win
, &ws
->stack
, stack_entry
)
5353 DNPRINTF(SWM_D_STACK
, "stack_master: done\n");
5357 vertical_config(struct workspace
*ws
, int id
)
5359 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
5363 case SWM_ARG_ID_STACKRESET
:
5364 case SWM_ARG_ID_STACKINIT
:
5365 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
5366 ws
->l_state
.vertical_mwin
= 1;
5367 ws
->l_state
.vertical_stacks
= 1;
5369 case SWM_ARG_ID_MASTERSHRINK
:
5370 if (ws
->l_state
.vertical_msize
> 1)
5371 ws
->l_state
.vertical_msize
--;
5373 case SWM_ARG_ID_MASTERGROW
:
5374 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
5375 ws
->l_state
.vertical_msize
++;
5377 case SWM_ARG_ID_MASTERADD
:
5378 ws
->l_state
.vertical_mwin
++;
5380 case SWM_ARG_ID_MASTERDEL
:
5381 if (ws
->l_state
.vertical_mwin
> 0)
5382 ws
->l_state
.vertical_mwin
--;
5384 case SWM_ARG_ID_STACKBALANCE
:
5385 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ (ws
->l_state
.vertical_stacks
+ 1);
5387 case SWM_ARG_ID_STACKINC
:
5388 ws
->l_state
.vertical_stacks
++;
5390 case SWM_ARG_ID_STACKDEC
:
5391 if (ws
->l_state
.vertical_stacks
> 1)
5392 ws
->l_state
.vertical_stacks
--;
5394 case SWM_ARG_ID_FLIPLAYOUT
:
5395 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
5403 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5405 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
5407 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
5411 horizontal_config(struct workspace
*ws
, int id
)
5413 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
5416 case SWM_ARG_ID_STACKRESET
:
5417 case SWM_ARG_ID_STACKINIT
:
5418 ws
->l_state
.horizontal_mwin
= 1;
5419 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
5420 ws
->l_state
.horizontal_stacks
= 1;
5422 case SWM_ARG_ID_MASTERSHRINK
:
5423 if (ws
->l_state
.horizontal_msize
> 1)
5424 ws
->l_state
.horizontal_msize
--;
5426 case SWM_ARG_ID_MASTERGROW
:
5427 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
5428 ws
->l_state
.horizontal_msize
++;
5430 case SWM_ARG_ID_MASTERADD
:
5431 ws
->l_state
.horizontal_mwin
++;
5433 case SWM_ARG_ID_MASTERDEL
:
5434 if (ws
->l_state
.horizontal_mwin
> 0)
5435 ws
->l_state
.horizontal_mwin
--;
5437 case SWM_ARG_ID_STACKBALANCE
:
5438 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ (ws
->l_state
.horizontal_stacks
+ 1);
5440 case SWM_ARG_ID_STACKINC
:
5441 ws
->l_state
.horizontal_stacks
++;
5443 case SWM_ARG_ID_STACKDEC
:
5444 if (ws
->l_state
.horizontal_stacks
> 1)
5445 ws
->l_state
.horizontal_stacks
--;
5447 case SWM_ARG_ID_FLIPLAYOUT
:
5448 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
5456 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5458 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
5460 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
5463 /* fullscreen view */
5465 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5467 struct swm_geometry gg
= *g
;
5468 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
, *tmpw
;
5471 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
5476 winno
= count_win(ws
, false);
5477 if (winno
== 0 && count_win(ws
, true) == 0)
5480 /* Figure out which top level window should be visible. */
5481 if (ws
->focus_pending
)
5482 win
= ws
->focus_pending
;
5485 else if (ws
->focus_prev
)
5486 win
= ws
->focus_prev
;
5488 win
= TAILQ_FIRST(&ws
->winlist
);
5490 DNPRINTF(SWM_D_STACK
, "max_stack: focus_pending: %#x, focus: %#x, "
5491 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws
->focus_pending
),
5492 WINID(ws
->focus
), WINID(ws
->focus_prev
),
5493 WINID(TAILQ_FIRST(&ws
->winlist
)), win
->id
);
5495 /* Update window geometry. */
5496 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5505 /* Set maximized flag for all maxed windows. */
5506 if (!MAXIMIZED(w
)) {
5507 /* Preserve floating geometry. */
5509 store_float_geom(w
);
5511 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_MAXIMIZED
);
5512 ewmh_update_wm_state(w
);
5515 /* Only reconfigure if necessary. */
5516 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
5517 HEIGHT(w
) != gg
.h
) {
5520 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
)) {
5521 w
->bordered
= false;
5522 WIDTH(w
) += 2 * border_width
;
5523 HEIGHT(w
) += 2 * border_width
;
5526 X(w
) += border_width
;
5527 Y(w
) += border_width
;
5534 /* If transient, stack parent and its children. */
5535 if (TRANS(win
) && (parent
= find_window(win
->transient
))) {
5536 raise_window(parent
);
5538 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5539 if (w
->transient
== parent
->id
)
5543 /* Make sure focus window is on top. */
5546 /* Stack any children of focus window. */
5547 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5548 if (w
->transient
== win
->id
)
5551 /* Map all windows. */
5552 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
5558 send_to_rg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5560 int ridx
= args
->id
, i
, num_screens
;
5561 struct swm_region
*rr
= NULL
;
5564 num_screens
= get_screen_count();
5565 /* do nothing if we don't have more than one screen */
5566 if (!(num_screens
> 1 || outputs
> 1))
5569 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
5571 rr
= TAILQ_FIRST(&r
->s
->rl
);
5572 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
5573 rr
= TAILQ_NEXT(rr
, entry
);
5580 send_to_ws(bp
, r
, &a
);
5584 region_under(struct swm_screen
*s
, int x
, int y
)
5586 struct swm_region
*r
= NULL
;
5588 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
5589 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
5590 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
5591 WIDTH(r
), HEIGHT(r
), x
, y
);
5592 if (X(r
) <= x
&& x
< MAX_X(r
))
5593 if (Y(r
) <= y
&& y
< MAX_Y(r
))
5600 /* Transfer focused window to target workspace and focus. */
5602 send_to_ws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5604 int wsid
= args
->id
;
5605 struct ws_win
*win
= NULL
;
5609 if (r
&& r
->ws
&& r
->ws
->focus
)
5614 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win %#x, ws %d\n", win
->id
, wsid
);
5616 if (wsid
< 0 || wsid
>= workspace_limit
)
5619 if (win
->ws
->idx
== wsid
)
5622 win_to_ws(win
, wsid
, true);
5624 /* Set new focus on target ws. */
5625 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5626 win
->ws
->focus_prev
= win
->ws
->focus
;
5627 win
->ws
->focus
= win
;
5628 win
->ws
->focus_pending
= NULL
;
5630 if (win
->ws
->focus_prev
)
5631 draw_frame(win
->ws
->focus_prev
);
5634 DNPRINTF(SWM_D_STACK
, "send_to_ws: focus_pending: %#x, focus: %#x, "
5635 "focus_prev: %#x, first: %#x, win: %#x\n",
5636 WINID(r
->ws
->focus_pending
), WINID(r
->ws
->focus
),
5637 WINID(r
->ws
->focus_prev
), WINID(TAILQ_FIRST(&r
->ws
->winlist
)),
5640 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5641 ewmh_update_wm_state(win
);
5643 /* Restack focused region. */
5646 /* If destination ws has a region, restack. */
5649 load_float_geom(win
);
5654 /* Set new focus on current ws. */
5655 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5656 if (r
->ws
->focus
!= NULL
) {
5657 focus_win(r
->ws
->focus
);
5659 DNPRINTF(SWM_D_FOCUS
, "send_to_ws: set_input_focus: "
5660 "%#x, revert-to: parent, time: 0\n", r
->id
);
5661 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
5662 r
->id
, XCB_CURRENT_TIME
);
5672 win_to_ws(struct ws_win
*win
, int wsid
, bool unfocus
)
5674 struct ws_win
*parent
;
5675 struct workspace
*ws
, *nws
, *pws
;
5677 if (wsid
< 0 || wsid
>= workspace_limit
)
5680 if (win
->ws
->idx
== wsid
)
5684 nws
= &win
->s
->ws
[wsid
];
5686 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win %#x, ws %d -> %d\n", win
->id
,
5689 /* Cleanup focus on source ws. */
5690 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
5691 (ws
->focus
== win
|| ws
->focus_pending
== win
))
5692 ws
->focus_pending
= get_focus_prev(win
);
5694 /* Move the parent if this is a transient window. */
5696 parent
= find_window(win
->transient
);
5699 /* Set new focus in parent's ws if needed. */
5700 if (pws
->focus
== parent
) {
5701 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5702 pws
->focus_pending
=
5703 get_focus_prev(parent
);
5705 unfocus_win(parent
);
5707 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5708 pws
->focus
= pws
->focus_pending
;
5709 pws
->focus_pending
= NULL
;
5713 /* Don't unmap parent if new ws is visible */
5715 unmap_window(parent
);
5718 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
5719 TAILQ_REMOVE(&ws
->stack
, parent
, stack_entry
);
5720 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
5721 TAILQ_INSERT_TAIL(&nws
->stack
, parent
, stack_entry
);
5724 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5725 "_NET_WM_DESKTOP: %d\n", wsid
);
5726 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5727 parent
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
5728 XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5735 if (ws
->focus_prev
== win
)
5736 ws
->focus_prev
= NULL
;
5738 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& ws
->focus_pending
!= NULL
) {
5739 ws
->focus
= ws
->focus_pending
;
5740 ws
->focus_pending
= NULL
;
5743 /* Don't unmap if new ws is visible */
5748 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
5749 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
5750 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
5751 TAILQ_INSERT_TAIL(&nws
->stack
, win
, stack_entry
);
5754 /* Update the window's workspace property: _NET_WM_DESKTOP */
5755 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5756 "_NET_WM_DESKTOP: %d\n", wsid
);
5757 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
5758 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5760 ewmh_update_client_list();
5762 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
5766 pressbutton(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5768 /* suppress unused warning since var is needed */
5772 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
5773 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5774 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
5775 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5779 raise_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5781 /* Suppress warning. */
5785 if (r
== NULL
|| r
->ws
== NULL
)
5788 if (r
->ws
->focus
&& MAXIMIZED(r
->ws
->focus
))
5791 r
->ws
->always_raise
= !r
->ws
->always_raise
;
5793 /* Update focused win stacking order based on new always_raise value. */
5794 raise_window(r
->ws
->focus
);
5800 iconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5804 /* Suppress warning. */
5808 if ((w
= r
->ws
->focus
) == NULL
)
5811 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_HIDDEN
);
5812 ewmh_update_wm_state(w
);
5820 get_win_name(xcb_window_t win
)
5823 xcb_get_property_cookie_t c
;
5824 xcb_get_property_reply_t
*r
;
5826 /* First try _NET_WM_NAME for UTF-8. */
5827 c
= xcb_get_property(conn
, 0, win
, ewmh
[_NET_WM_NAME
].atom
,
5828 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5829 r
= xcb_get_property_reply(conn
, c
, NULL
);
5830 if (r
&& r
->type
== XCB_NONE
) {
5832 /* Use WM_NAME instead; no UTF-8. */
5833 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
5834 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5835 r
= xcb_get_property_reply(conn
, c
, NULL
);
5838 if (r
&& r
->type
!= XCB_NONE
&& r
->length
> 0)
5839 name
= strndup(xcb_get_property_value(r
),
5840 xcb_get_property_value_length(r
));
5845 err(1, "get_win_name: failed to allocate memory.");
5853 uniconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5862 DNPRINTF(SWM_D_MISC
, "uniconify\n");
5864 if (r
== NULL
|| r
->ws
== NULL
)
5867 /* make sure we have anything to uniconify */
5868 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5869 if (win
->ws
== NULL
)
5870 continue; /* should never happen */
5876 DNPRINTF(SWM_D_MISC
, "uniconify: count: %d\n", count
);
5882 search_resp_action
= SWM_SEARCH_UNICONIFY
;
5884 spawn_select(r
, args
, "search", &searchpid
);
5886 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5889 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5890 if (win
->ws
== NULL
)
5891 continue; /* should never happen */
5895 name
= get_win_name(win
->id
);
5896 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
5904 name_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5910 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
5916 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
5918 spawn_select(r
, args
, "name_workspace", &searchpid
);
5920 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5923 fprintf(lfile
, "%s", "");
5928 search_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5931 struct workspace
*ws
;
5936 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
5942 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
5944 spawn_select(r
, args
, "search", &searchpid
);
5946 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5949 for (i
= 0; i
< workspace_limit
; i
++) {
5953 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
5954 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
5961 search_win_cleanup(void)
5963 struct search_window
*sw
= NULL
;
5965 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
5966 xcb_destroy_window(conn
, sw
->indicator
);
5967 TAILQ_REMOVE(&search_wl
, sw
, entry
);
5973 search_win(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5975 struct ws_win
*win
= NULL
;
5976 struct search_window
*sw
= NULL
;
5979 xcb_screen_t
*screen
;
5980 int i
, width
, height
;
5988 XRectangle l_ibox
, l_lbox
;
5992 DNPRINTF(SWM_D_MISC
, "search_win\n");
5995 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
5997 spawn_select(r
, args
, "search", &searchpid
);
5999 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
6002 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
6003 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
6005 TAILQ_INIT(&search_wl
);
6008 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
6012 sw
= calloc(1, sizeof(struct search_window
));
6014 warn("search_win: calloc");
6016 search_win_cleanup();
6022 snprintf(s
, sizeof s
, "%d", i
);
6025 w
= xcb_generate_id(conn
);
6026 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6027 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6028 wa
[2] = screen
->default_colormap
;
6030 if (bar_font_legacy
) {
6031 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
6032 width
= l_lbox
.width
+ 4;
6033 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
6035 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
6037 width
= info
.width
+ 4;
6038 height
= bar_font
->height
+ 4;
6041 xcb_create_window(conn
, screen
->root_depth
, w
, win
->frame
, 0, 0,
6042 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
6043 screen
->root_visual
, XCB_CW_BACK_PIXEL
|
6044 XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
, wa
);
6046 xcb_map_window(conn
, w
);
6049 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
6051 if (bar_font_legacy
) {
6052 l_gcv
.graphics_exposures
= 0;
6053 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
6055 XSetForeground(display
, l_draw
,
6056 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
6058 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
6059 (bar_fs_extents
->max_logical_extent
.height
-
6060 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
6062 XFreeGC(display
, l_draw
);
6065 draw
= XftDrawCreate(display
, w
,
6066 DefaultVisual(display
, r
->s
->idx
),
6067 DefaultColormap(display
, r
->s
->idx
));
6069 XftDrawStringUtf8(draw
, &search_font_color
, bar_font
, 2,
6070 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
6071 bar_font
->descent
, (FcChar8
*)s
, len
);
6073 XftDrawDestroy(draw
);
6076 DNPRINTF(SWM_D_MISC
, "search_win: mapped win %#x\n", w
);
6078 fprintf(lfile
, "%d\n", i
);
6088 search_resp_uniconify(const char *resp
, size_t len
)
6094 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
6096 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
6099 name
= get_win_name(win
->id
);
6100 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
6105 if (strncmp(s
, resp
, len
) == 0) {
6106 /* XXX this should be a callback to generalize */
6107 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_HIDDEN
);
6108 ewmh_update_wm_state(win
);
6118 search_resp_name_workspace(const char *resp
, size_t len
)
6120 struct workspace
*ws
;
6122 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
6124 if (search_r
->ws
== NULL
)
6129 free(search_r
->ws
->name
);
6130 search_r
->ws
->name
= NULL
;
6134 ws
->name
= strdup(resp
);
6135 if (ws
->name
== NULL
) {
6136 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
6137 "strdup: %s", strerror(errno
));
6141 ewmh_update_desktop_names();
6142 ewmh_get_desktop_names();
6147 ewmh_update_desktop_names(void)
6149 char *name_list
= NULL
, *p
;
6150 int num_screens
, i
, j
, len
= 0, tot
= 0;
6152 num_screens
= get_screen_count();
6153 for (i
= 0; i
< num_screens
; ++i
) {
6154 for (j
= 0; j
< workspace_limit
; ++j
) {
6155 if (screens
[i
].ws
[j
].name
!= NULL
)
6156 len
+= strlen(screens
[i
].ws
[j
].name
);
6160 if((name_list
= calloc(len
, sizeof(char))) == NULL
)
6161 err(1, "update_desktop_names: calloc: failed to "
6162 "allocate memory.");
6165 for (j
= 0; j
< workspace_limit
; ++j
) {
6166 if (screens
[i
].ws
[j
].name
!= NULL
) {
6167 len
= strlen(screens
[i
].ws
[j
].name
);
6168 memcpy(p
, screens
[i
].ws
[j
].name
, len
);
6177 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6178 screens
[i
].root
, ewmh
[_NET_DESKTOP_NAMES
].atom
,
6179 a_utf8_string
, 8, tot
, name_list
);
6189 ewmh_get_desktop_names(void)
6192 xcb_get_property_cookie_t c
;
6193 xcb_get_property_reply_t
*r
;
6194 int num_screens
, i
, j
, n
, k
;
6196 num_screens
= get_screen_count();
6197 for (i
= 0; i
< num_screens
; ++i
) {
6198 for (j
= 0; j
< workspace_limit
; ++j
) {
6199 free(screens
[i
].ws
[j
].name
);
6200 screens
[i
].ws
[j
].name
= NULL
;
6203 c
= xcb_get_property(conn
, 0, screens
[i
].root
,
6204 ewmh
[_NET_DESKTOP_NAMES
].atom
,
6205 a_utf8_string
, 0, UINT32_MAX
);
6206 r
= xcb_get_property_reply(conn
, c
, NULL
);
6210 names
= xcb_get_property_value(r
);
6211 n
= xcb_get_property_value_length(r
);
6213 for (j
= 0, k
= 0; j
< n
; ++j
) {
6214 if (*(names
+ j
) != '\0') {
6215 screens
[i
].ws
[k
].name
= strdup(names
+ j
);
6216 j
+= strlen(names
+ j
);
6225 ewmh_update_client_list(void)
6228 int num_screens
, i
, j
, k
= 0, count
= 0;
6231 num_screens
= get_screen_count();
6232 for (i
= 0; i
< num_screens
; ++i
) {
6233 for (j
= 0; j
< workspace_limit
; ++j
)
6234 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6237 DNPRINTF(SWM_D_PROP
, "ewmh_update_client_list: win count: %d\n",
6243 wins
= calloc(count
, sizeof(xcb_window_t
));
6245 err(1, "ewmh_update_client_list: calloc: failed to "
6246 "allocate memory.");
6248 for (j
= 0, k
= 0; j
< workspace_limit
; ++j
)
6249 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6250 wins
[k
++] = win
->id
;
6252 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6253 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
6254 XCB_ATOM_WINDOW
, 32, count
, wins
);
6261 ewmh_update_current_desktop(void)
6265 num_screens
= get_screen_count();
6266 for (i
= 0; i
< num_screens
; ++i
) {
6267 if (screens
[i
].r_focus
)
6268 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6269 screens
[i
].root
, ewmh
[_NET_CURRENT_DESKTOP
].atom
,
6270 XCB_ATOM_CARDINAL
, 32, 1,
6271 &screens
[i
].r_focus
->ws
->idx
);
6276 ewmh_update_desktops(void)
6278 int num_screens
, i
, j
;
6281 vals
= calloc(workspace_limit
* 2, sizeof(uint32_t));
6283 err(1, "ewmh_update_desktops: calloc: failed to allocate "
6286 num_screens
= get_screen_count();
6287 for (i
= 0; i
< num_screens
; i
++) {
6288 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6289 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
6290 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
6292 for (j
= 0; j
< workspace_limit
; ++j
) {
6293 if (screens
[i
].ws
[j
].r
!= NULL
) {
6294 vals
[j
* 2] = X(screens
[i
].ws
[j
].r
);
6295 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].r
);
6296 } else if (screens
[i
].ws
[j
].old_r
!= NULL
) {
6297 vals
[j
* 2] = X(screens
[i
].ws
[j
].old_r
);
6298 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].old_r
);
6300 vals
[j
* 2] = vals
[j
* 2 + 1] = 0;
6304 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6305 screens
[i
].root
, ewmh
[_NET_DESKTOP_VIEWPORT
].atom
,
6306 XCB_ATOM_CARDINAL
, 32, workspace_limit
* 2, vals
);
6313 search_resp_search_workspace(const char *resp
)
6320 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
6324 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
6331 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
6333 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
6340 switchws(NULL
, search_r
, &a
);
6344 search_resp_search_window(const char *resp
)
6349 struct search_window
*sw
;
6351 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
6355 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
6360 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
6362 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
6369 TAILQ_FOREACH(sw
, &search_wl
, entry
)
6370 if (idx
== sw
->idx
) {
6376 #define MAX_RESP_LEN 1024
6379 search_do_resp(void)
6385 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
6390 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
6391 warn("search: calloc");
6395 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
6397 warn("search: read error");
6400 resp
[rbytes
] = '\0';
6403 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
6404 * newline, so work around that by sanitizing the resp now.
6406 resp
[strcspn(resp
, "\n")] = '\0';
6409 switch (search_resp_action
) {
6410 case SWM_SEARCH_UNICONIFY
:
6411 search_resp_uniconify(resp
, len
);
6413 case SWM_SEARCH_NAME_WORKSPACE
:
6414 search_resp_name_workspace(resp
, len
);
6416 case SWM_SEARCH_SEARCH_WORKSPACE
:
6417 search_resp_search_workspace(resp
);
6419 case SWM_SEARCH_SEARCH_WINDOW
:
6420 search_resp_search_window(resp
);
6425 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
6426 search_win_cleanup();
6428 search_resp_action
= SWM_SEARCH_NONE
;
6429 close(select_resp_pipe
[0]);
6436 wkill(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6440 DNPRINTF(SWM_D_MISC
, "wkill: win %#x, id: %d\n", WINID(r
->ws
->focus
),
6443 if (r
->ws
->focus
== NULL
)
6446 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
6447 xcb_kill_client(conn
, r
->ws
->focus
->id
);
6449 if (r
->ws
->focus
->can_delete
)
6450 client_msg(r
->ws
->focus
, a_delete
, 0);
6456 clear_maximized(struct workspace
*ws
)
6461 /* Clear any maximized win(s) on ws, from bottom up. */
6462 TAILQ_FOREACH_REVERSE(w
, &ws
->stack
, ws_win_stack
, stack_entry
)
6464 ewmh_apply_flags(w
, w
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
6465 ewmh_update_wm_state(w
);
6473 maximize_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6475 struct ws_win
*w
= r
->ws
->focus
;
6477 /* suppress unused warning since var is needed */
6484 DNPRINTF(SWM_D_MISC
, "maximize_toggle: win %#x\n", w
->id
);
6489 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6492 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_MAXIMIZED
);
6493 ewmh_update_wm_state(w
);
6497 if (w
== w
->ws
->focus
)
6502 DNPRINTF(SWM_D_MISC
, "maximize_toggle: done\n");
6506 floating_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6508 struct ws_win
*w
= r
->ws
->focus
;
6510 /* suppress unused warning since var is needed */
6517 DNPRINTF(SWM_D_MISC
, "floating_toggle: win %#x\n", w
->id
);
6519 if (FULLSCREEN(w
) || TRANS(w
))
6522 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6525 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_ABOVE
);
6526 ewmh_update_wm_state(w
);
6530 if (w
== w
->ws
->focus
)
6535 DNPRINTF(SWM_D_MISC
, "floating_toggle: done\n");
6539 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
6541 struct swm_geometry g
= r
->g
;
6542 int rt
, lt
, tp
, bm
, bw
;
6544 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
6547 * Perpendicular distance of each side of the window to the respective
6548 * side of the region boundary. Positive values indicate the side of
6549 * the window has passed beyond the region boundary.
6551 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) - MAX_X(r
) : bw
;
6552 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) : bw
;
6553 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) - MAX_Y(r
) : bw
;
6554 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) : bw
;
6556 DNPRINTF(SWM_D_MISC
, "region_containment: win %#x, rt: %d, lt: %d, "
6557 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
6558 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
6559 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
6562 * Disable containment if any of the flagged sides went beyond the
6563 * containment boundary, or if containment is disabled.
6565 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
6566 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
6567 /* Make sure window has at least 1 pixel in the region */
6568 g
.x
+= 1 - WIDTH(win
);
6569 g
.y
+= 1 - HEIGHT(win
);
6570 g
.w
+= 2 * WIDTH(win
) - 2;
6571 g
.h
+= 2 * HEIGHT(win
) - 2;
6574 constrain_window(win
, &g
, &opts
);
6577 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6579 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
6581 DNPRINTF(SWM_D_MISC
, "constrain_window: win %#x, (x,y) w x h: "
6582 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6583 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
6584 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
6585 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
6586 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
6587 YESNO(*opts
& SWM_CW_RESIZABLE
));
6589 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) > b
->x
+ b
->w
) {
6590 if (*opts
& SWM_CW_RESIZABLE
)
6591 WIDTH(win
) = b
->x
+ b
->w
- X(win
);
6593 X(win
) = b
->x
+ b
->w
- WIDTH(win
);
6596 if ((*opts
& SWM_CW_LEFT
) && X(win
) < b
->x
) {
6597 if (*opts
& SWM_CW_RESIZABLE
)
6598 WIDTH(win
) -= b
->x
- X(win
);
6603 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) > b
->y
+ b
->h
) {
6604 if (*opts
& SWM_CW_RESIZABLE
)
6605 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
);
6607 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
);
6610 if ((*opts
& SWM_CW_TOP
) && Y(win
) < b
->y
) {
6611 if (*opts
& SWM_CW_RESIZABLE
)
6612 HEIGHT(win
) -= b
->y
- Y(win
);
6617 if (*opts
& SWM_CW_RESIZABLE
) {
6620 if (HEIGHT(win
) < 1)
6626 draw_frame(struct ws_win
*win
)
6628 xcb_rectangle_t rect
[4];
6632 if (win
->frame
== XCB_WINDOW_NONE
) {
6633 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x not "
6634 "reparented.\n", win
->id
);
6638 if (!win
->bordered
) {
6639 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x frame "
6640 "disabled\n", win
->id
);
6643 if (WS_FOCUSED(win
->ws
) && win
->ws
->focus
== win
)
6644 pixel
= MAXIMIZED(win
) ?
6645 &win
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].pixel
:
6646 &win
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6648 pixel
= MAXIMIZED(win
) ?
6649 &win
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].pixel
:
6650 &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6652 /* Top (with corners) */
6655 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6656 rect
[n
].height
= border_width
;
6657 /* Left (without corners) */
6659 rect
[n
].y
= border_width
;
6660 rect
[n
].width
= border_width
;
6661 rect
[n
].height
= HEIGHT(win
);
6662 /* Right (without corners) */
6663 rect
[++n
].x
= border_width
+ WIDTH(win
);
6664 rect
[n
].y
= border_width
;
6665 rect
[n
].width
= border_width
;
6666 rect
[n
].height
= HEIGHT(win
);
6667 /* Bottom (with corners)*/
6669 rect
[n
].y
= border_width
+ HEIGHT(win
);
6670 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6671 rect
[n
].height
= border_width
;
6673 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, pixel
);
6674 xcb_poly_fill_rectangle(conn
, win
->frame
, win
->s
->bar_gc
, 4, rect
);
6678 update_window(struct ws_win
*win
)
6683 if (win
->frame
== XCB_WINDOW_NONE
) {
6684 DNPRINTF(SWM_D_EVENT
, "update_window: skip win %#x; "
6685 "not reparented.\n", win
->id
);
6689 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
6690 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
6691 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6693 /* Reconfigure frame. */
6694 if (win
->bordered
) {
6695 wc
[0] = X(win
) - border_width
;
6696 wc
[1] = Y(win
) - border_width
;
6697 wc
[2] = WIDTH(win
) + 2 * border_width
;
6698 wc
[3] = HEIGHT(win
) + 2 * border_width
;
6703 wc
[3] = HEIGHT(win
);
6708 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x frame %#x, (x,y) w x h: "
6709 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, win
->frame
, wc
[0],
6710 wc
[1], wc
[2], wc
[3], YESNO(win
->bordered
));
6712 xcb_configure_window(conn
, win
->frame
, mask
, wc
);
6714 /* Reconfigure client window. */
6715 wc
[0] = wc
[1] = win
->bordered
? border_width
: 0;
6717 wc
[3] = HEIGHT(win
);
6719 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x, (x,y) w x h: "
6720 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
6721 wc
[3], YESNO(win
->bordered
));
6722 xcb_configure_window(conn
, win
->id
, mask
, wc
);
6724 /* ICCCM 4.2.3 Synthetic ConfigureNotify when moved and not resized. */
6725 if ((X(win
) != win
->g_prev
.x
|| Y(win
) != win
->g_prev
.y
) &&
6726 (win
->java
|| (WIDTH(win
) == win
->g_prev
.w
&&
6727 HEIGHT(win
) == win
->g_prev
.h
))) {
6728 /* Java has special needs when moved together with a resize. */
6729 config_win(win
, NULL
);
6732 win
->g_prev
= win
->g
;
6736 SIMPLEQ_ENTRY(event
) entry
;
6737 xcb_generic_event_t
*ev
;
6739 SIMPLEQ_HEAD(event_queue
, event
) events
= SIMPLEQ_HEAD_INITIALIZER(events
);
6741 xcb_generic_event_t
*
6742 get_next_event(bool dowait
)
6745 xcb_generic_event_t
*evt
;
6747 /* Try queue first. */
6748 if ((ep
= SIMPLEQ_FIRST(&events
))) {
6750 SIMPLEQ_REMOVE_HEAD(&events
, entry
);
6753 evt
= xcb_wait_for_event(conn
);
6755 evt
= xcb_poll_for_event(conn
);
6761 put_back_event(xcb_generic_event_t
*evt
)
6764 if ((ep
= malloc(sizeof (struct event
))) == NULL
)
6765 err(1, "put_back_event: failed to allocate memory.");
6767 SIMPLEQ_INSERT_HEAD(&events
, ep
, entry
);
6770 /* Peeks at next event to detect auto-repeat. */
6772 keyrepeating(xcb_key_release_event_t
*kre
)
6774 xcb_generic_event_t
*evt
;
6776 /* Ensure repeating keypress is finished processing. */
6779 if ((evt
= get_next_event(false))) {
6780 put_back_event(evt
);
6782 if (XCB_EVENT_RESPONSE_TYPE(evt
) == XCB_KEY_PRESS
&&
6783 kre
->sequence
== evt
->sequence
&&
6784 kre
->detail
== ((xcb_key_press_event_t
*)evt
)->detail
)
6792 keybindreleased(struct binding
*bp
, xcb_key_release_event_t
*kre
)
6794 if (bp
->type
== KEYBIND
&& !keyrepeating(kre
) &&
6795 bp
->value
== xcb_key_press_lookup_keysym(syms
, kre
, 0))
6801 #define SWM_RESIZE_STEPS (50)
6804 resize_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
6806 xcb_timestamp_t timestamp
= 0;
6807 struct swm_region
*r
= NULL
;
6808 struct swm_geometry g
;
6809 int top
= 0, left
= 0;
6811 xcb_cursor_t cursor
;
6812 xcb_query_pointer_reply_t
*xpr
= NULL
;
6813 xcb_generic_event_t
*evt
;
6814 xcb_motion_notify_event_t
*mne
;
6815 bool resizing
, step
= false;
6821 if (FULLSCREEN(win
))
6824 /* In max_stack mode, should only resize transients. */
6825 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6828 DNPRINTF(SWM_D_EVENT
, "resize: win %#x, floating: %s, "
6829 "transient: %#x\n", win
->id
, YESNO(ABOVE(win
)),
6833 store_float_geom(win
);
6834 else if (!(TRANS(win
) || ABOVE(win
)))
6837 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6839 ewmh_update_wm_state(win
);
6845 /* It's possible for win to have been freed during focus_flush(). */
6846 if (validate_win(win
)) {
6847 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6852 case SWM_ARG_ID_WIDTHSHRINK
:
6853 WIDTH(win
) -= SWM_RESIZE_STEPS
;
6856 case SWM_ARG_ID_WIDTHGROW
:
6857 WIDTH(win
) += SWM_RESIZE_STEPS
;
6860 case SWM_ARG_ID_HEIGHTSHRINK
:
6861 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
6864 case SWM_ARG_ID_HEIGHTGROW
:
6865 HEIGHT(win
) += SWM_RESIZE_STEPS
;
6872 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6873 SWM_CW_HARDBOUNDARY
);
6875 store_float_geom(win
);
6879 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6880 SWM_CW_SOFTBOUNDARY
);
6883 /* get cursor offset from window root */
6884 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6891 if (xpr
->win_x
< WIDTH(win
) / 2)
6894 if (xpr
->win_y
< HEIGHT(win
) / 2)
6897 if (opt
== SWM_ARG_ID_CENTER
)
6898 cursor
= cursors
[XC_SIZING
].cid
;
6900 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
6901 XC_TOP_RIGHT_CORNER
].cid
;
6903 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
6904 XC_BOTTOM_RIGHT_CORNER
].cid
;
6906 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6907 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
6910 /* Release keyboard freeze if called via keybind. */
6911 if (bp
->type
== KEYBIND
)
6912 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6917 while (resizing
&& (evt
= get_next_event(true))) {
6918 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6919 case XCB_BUTTON_RELEASE
:
6920 if (bp
->type
== BTNBIND
&& bp
->value
==
6921 ((xcb_button_release_event_t
*)evt
)->detail
)
6924 case XCB_KEY_RELEASE
:
6925 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
6928 case XCB_MOTION_NOTIFY
:
6929 mne
= (xcb_motion_notify_event_t
*)evt
;
6930 DNPRINTF(SWM_D_EVENT
, "resize: MOTION_NOTIFY: "
6931 "root: %#x\n", mne
->root
);
6933 /* cursor offset/delta from start of the operation */
6934 dx
= mne
->root_x
- xpr
->root_x
;
6935 dy
= mne
->root_y
- xpr
->root_y
;
6941 if (opt
== SWM_ARG_ID_CENTER
) {
6942 if (g
.h
/ 2 + dy
< 1)
6946 HEIGHT(win
) = g
.h
+ 2 * dy
;
6954 HEIGHT(win
) = g
.h
+ dy
;
6961 if (opt
== SWM_ARG_ID_CENTER
) {
6962 if (g
.w
/ 2 + dx
< 1)
6966 WIDTH(win
) = g
.w
+ 2 * dx
;
6974 WIDTH(win
) = g
.w
+ dx
;
6977 /* not free, don't sync more than 120 times / second */
6978 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6979 timestamp
= mne
->time
;
6980 regionize(win
, mne
->root_x
, mne
->root_y
);
6981 region_containment(win
, r
, SWM_CW_ALLSIDES
|
6982 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
6983 SWM_CW_SOFTBOUNDARY
);
6988 case XCB_BUTTON_PRESS
:
6990 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_PRESS ignored\n");
6991 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
6992 ((xcb_button_press_event_t
*)evt
)->time
);
6997 DNPRINTF(SWM_D_EVENT
, "resize: KEY_PRESS ignored\n");
6998 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6999 ((xcb_key_press_event_t
*)evt
)->time
);
7005 /* It's possible for win to have been freed above. */
7006 if (validate_win(win
)) {
7007 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
7015 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
7016 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
7020 store_float_geom(win
);
7022 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7024 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
7028 resize(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
7030 struct ws_win
*win
= NULL
;
7031 xcb_query_pointer_reply_t
*qpr
= NULL
;
7036 if (args
->id
!= SWM_ARG_ID_DONTCENTER
&&
7037 args
->id
!= SWM_ARG_ID_CENTER
) {
7038 /* {height,width}_{grow,shrink} use the focus window. */
7042 /* resize uses window under pointer. */
7043 qpr
= xcb_query_pointer_reply(conn
,
7044 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7046 win
= find_window(qpr
->child
);
7052 resize_win(win
, bp
, args
->id
);
7054 if (args
->id
&& bp
->type
== KEYBIND
)
7060 /* Try to set window region based on supplied coordinates or window center. */
7062 regionize(struct ws_win
*win
, int x
, int y
)
7064 struct swm_region
*r
= NULL
;
7066 r
= region_under(win
->s
, x
, y
);
7068 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
7069 Y(win
) + HEIGHT(win
) / 2);
7071 if (r
&& r
!= win
->ws
->r
) {
7072 clear_maximized(r
->ws
);
7074 win_to_ws(win
, r
->ws
->idx
, false);
7076 /* Stack old and new region. */
7080 /* Set focus on new ws. */
7081 unfocus_win(r
->ws
->focus
);
7089 #define SWM_MOVE_STEPS (50)
7092 move_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
7094 struct swm_region
*r
;
7095 xcb_timestamp_t timestamp
= 0;
7096 xcb_query_pointer_reply_t
*qpr
= NULL
;
7097 xcb_generic_event_t
*evt
;
7098 xcb_motion_notify_event_t
*mne
;
7099 bool moving
, restack
= false, step
= false;
7104 if ((r
= win
->ws
->r
) == NULL
)
7107 if (FULLSCREEN(win
))
7110 DNPRINTF(SWM_D_EVENT
, "move: win %#x, floating: %s, transient: "
7111 "%#x\n", win
->id
, YESNO(ABOVE(win
)), win
->transient
);
7113 /* in max_stack mode should only move transients */
7114 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
7117 if (!(ABOVE(win
) || TRANS(win
)) || MAXIMIZED(win
)) {
7118 store_float_geom(win
);
7122 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
7124 ewmh_update_wm_state(win
);
7131 /* It's possible for win to have been freed during focus_flush(). */
7132 if (validate_win(win
)) {
7133 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7138 case SWM_ARG_ID_MOVELEFT
:
7139 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
7142 case SWM_ARG_ID_MOVERIGHT
:
7143 X(win
) += (SWM_MOVE_STEPS
- border_width
);
7146 case SWM_ARG_ID_MOVEUP
:
7147 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
7150 case SWM_ARG_ID_MOVEDOWN
:
7151 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
7158 regionize(win
, -1, -1);
7159 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
7161 store_float_geom(win
);
7165 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
7166 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
7167 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
7169 /* get cursor offset from window root */
7170 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
7173 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7177 /* Release keyboard freeze if called via keybind. */
7178 if (bp
->type
== KEYBIND
)
7179 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7182 regionize(win
, qpr
->root_x
, qpr
->root_y
);
7183 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7184 SWM_CW_SOFTBOUNDARY
);
7188 while (moving
&& (evt
= get_next_event(true))) {
7189 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
7190 case XCB_BUTTON_RELEASE
:
7191 if (bp
->type
== BTNBIND
&& bp
->value
==
7192 ((xcb_button_release_event_t
*)evt
)->detail
)
7195 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7196 ((xcb_button_release_event_t
*)evt
)->time
);
7199 case XCB_KEY_RELEASE
:
7200 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
7203 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7204 ((xcb_key_release_event_t
*)evt
)->time
);
7207 case XCB_MOTION_NOTIFY
:
7208 mne
= (xcb_motion_notify_event_t
*)evt
;
7209 DNPRINTF(SWM_D_EVENT
, "move: MOTION_NOTIFY: "
7210 "root: %#x\n", mne
->root
);
7211 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
7212 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
7214 /* not free, don't sync more than 120 times / second */
7215 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
7216 timestamp
= mne
->time
;
7217 regionize(win
, mne
->root_x
, mne
->root_y
);
7218 region_containment(win
, win
->ws
->r
,
7219 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
7224 case XCB_BUTTON_PRESS
:
7225 /* Thaw and ignore. */
7226 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7227 ((xcb_button_press_event_t
*)evt
)->time
);
7232 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7233 ((xcb_key_press_event_t
*)evt
)->time
);
7239 /* It's possible for win to have been freed. */
7240 if (validate_win(win
)) {
7241 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7249 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7250 SWM_CW_SOFTBOUNDARY
);
7254 store_float_geom(win
);
7256 /* New region set to fullscreen layout. */
7257 if (win
->ws
->r
&& win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
]) {
7264 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7265 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
7269 move(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
7271 struct ws_win
*win
= NULL
;
7272 xcb_query_pointer_reply_t
*qpr
= NULL
;
7278 /* move_* uses focus window. */
7282 /* Disallow move_ on tiled. */
7283 if (win
&& !(TRANS(win
) || ABOVE(win
)))
7286 /* move uses window under pointer. */
7287 qpr
= xcb_query_pointer_reply(conn
,
7288 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7290 win
= find_window(qpr
->child
);
7296 move_win(win
, bp
, args
->id
);
7298 if (args
->id
&& bp
->type
== KEYBIND
)
7304 /* action definitions */
7306 char name
[SWM_FUNCNAME_LEN
];
7307 void (*func
)(struct binding
*, struct swm_region
*,
7311 } actions
[FN_INVALID
+ 2] = {
7312 /* name function argument */
7313 { "bar_toggle", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
7314 { "bar_toggle_ws", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
7315 { "button2", pressbutton
, 0, {.id
= 2} },
7316 { "cycle_layout", cycle_layout
, 0, {0} },
7317 { "flip_layout", stack_config
, 0, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
7318 { "float_toggle", floating_toggle
,0, {0} },
7319 { "focus", focus_pointer
, 0, {0} },
7320 { "focus_main", focus
, 0, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
7321 { "focus_next", focus
, 0, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
7322 { "focus_prev", focus
, 0, {.id
= SWM_ARG_ID_FOCUSPREV
} },
7323 { "focus_urgent", focus
, 0, {.id
= SWM_ARG_ID_FOCUSURGENT
} },
7324 { "maximize_toggle", maximize_toggle
,0, {0} },
7325 { "height_grow", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
7326 { "height_shrink", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
7327 { "iconify", iconify
, 0, {0} },
7328 { "master_shrink", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
7329 { "master_grow", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERGROW
} },
7330 { "master_add", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERADD
} },
7331 { "master_del", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERDEL
} },
7332 { "move", move
, FN_F_NOREPLAY
, {0} },
7333 { "move_down", move
, 0, {.id
= SWM_ARG_ID_MOVEDOWN
} },
7334 { "move_left", move
, 0, {.id
= SWM_ARG_ID_MOVELEFT
} },
7335 { "move_right", move
, 0, {.id
= SWM_ARG_ID_MOVERIGHT
} },
7336 { "move_up", move
, 0, {.id
= SWM_ARG_ID_MOVEUP
} },
7337 { "mvrg_1", send_to_rg
, 0, {.id
= 0} },
7338 { "mvrg_2", send_to_rg
, 0, {.id
= 1} },
7339 { "mvrg_3", send_to_rg
, 0, {.id
= 2} },
7340 { "mvrg_4", send_to_rg
, 0, {.id
= 3} },
7341 { "mvrg_5", send_to_rg
, 0, {.id
= 4} },
7342 { "mvrg_6", send_to_rg
, 0, {.id
= 5} },
7343 { "mvrg_7", send_to_rg
, 0, {.id
= 6} },
7344 { "mvrg_8", send_to_rg
, 0, {.id
= 7} },
7345 { "mvrg_9", send_to_rg
, 0, {.id
= 8} },
7346 { "mvws_1", send_to_ws
, 0, {.id
= 0} },
7347 { "mvws_2", send_to_ws
, 0, {.id
= 1} },
7348 { "mvws_3", send_to_ws
, 0, {.id
= 2} },
7349 { "mvws_4", send_to_ws
, 0, {.id
= 3} },
7350 { "mvws_5", send_to_ws
, 0, {.id
= 4} },
7351 { "mvws_6", send_to_ws
, 0, {.id
= 5} },
7352 { "mvws_7", send_to_ws
, 0, {.id
= 6} },
7353 { "mvws_8", send_to_ws
, 0, {.id
= 7} },
7354 { "mvws_9", send_to_ws
, 0, {.id
= 8} },
7355 { "mvws_10", send_to_ws
, 0, {.id
= 9} },
7356 { "mvws_11", send_to_ws
, 0, {.id
= 10} },
7357 { "mvws_12", send_to_ws
, 0, {.id
= 11} },
7358 { "mvws_13", send_to_ws
, 0, {.id
= 12} },
7359 { "mvws_14", send_to_ws
, 0, {.id
= 13} },
7360 { "mvws_15", send_to_ws
, 0, {.id
= 14} },
7361 { "mvws_16", send_to_ws
, 0, {.id
= 15} },
7362 { "mvws_17", send_to_ws
, 0, {.id
= 16} },
7363 { "mvws_18", send_to_ws
, 0, {.id
= 17} },
7364 { "mvws_19", send_to_ws
, 0, {.id
= 18} },
7365 { "mvws_20", send_to_ws
, 0, {.id
= 19} },
7366 { "mvws_21", send_to_ws
, 0, {.id
= 20} },
7367 { "mvws_22", send_to_ws
, 0, {.id
= 21} },
7368 { "name_workspace", name_workspace
, 0, {0} },
7369 { "quit", quit
, 0, {0} },
7370 { "raise_toggle", raise_toggle
, 0, {0} },
7371 { "resize", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_DONTCENTER
} },
7372 { "resize_centered", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_CENTER
} },
7373 { "restart", restart
, 0, {0} },
7374 { "rg_1", focusrg
, 0, {.id
= 0} },
7375 { "rg_2", focusrg
, 0, {.id
= 1} },
7376 { "rg_3", focusrg
, 0, {.id
= 2} },
7377 { "rg_4", focusrg
, 0, {.id
= 3} },
7378 { "rg_5", focusrg
, 0, {.id
= 4} },
7379 { "rg_6", focusrg
, 0, {.id
= 5} },
7380 { "rg_7", focusrg
, 0, {.id
= 6} },
7381 { "rg_8", focusrg
, 0, {.id
= 7} },
7382 { "rg_9", focusrg
, 0, {.id
= 8} },
7383 { "rg_move_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_UP
} },
7384 { "rg_move_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_DOWN
} },
7385 { "rg_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7386 { "rg_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7387 { "screen_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7388 { "screen_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7389 { "search_win", search_win
, 0, {0} },
7390 { "search_workspace", search_workspace
, 0, {0} },
7391 { "spawn_custom", NULL
, 0, {0} },
7392 { "stack_balance", stack_config
, 0, {.id
= SWM_ARG_ID_STACKBALANCE
} },
7393 { "stack_inc", stack_config
, 0, {.id
= SWM_ARG_ID_STACKINC
} },
7394 { "stack_dec", stack_config
, 0, {.id
= SWM_ARG_ID_STACKDEC
} },
7395 { "stack_reset", stack_config
, 0, {.id
= SWM_ARG_ID_STACKRESET
} },
7396 { "swap_main", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPMAIN
} },
7397 { "swap_next", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPNEXT
} },
7398 { "swap_prev", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPPREV
} },
7399 { "uniconify", uniconify
, 0, {0} },
7400 { "version", version
, 0, {0} },
7401 { "width_grow", resize
, 0, {.id
= SWM_ARG_ID_WIDTHGROW
} },
7402 { "width_shrink", resize
, 0, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
7403 { "wind_del", wkill
, 0, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
7404 { "wind_kill", wkill
, 0, {.id
= SWM_ARG_ID_KILLWINDOW
} },
7405 { "ws_1", switchws
, 0, {.id
= 0} },
7406 { "ws_2", switchws
, 0, {.id
= 1} },
7407 { "ws_3", switchws
, 0, {.id
= 2} },
7408 { "ws_4", switchws
, 0, {.id
= 3} },
7409 { "ws_5", switchws
, 0, {.id
= 4} },
7410 { "ws_6", switchws
, 0, {.id
= 5} },
7411 { "ws_7", switchws
, 0, {.id
= 6} },
7412 { "ws_8", switchws
, 0, {.id
= 7} },
7413 { "ws_9", switchws
, 0, {.id
= 8} },
7414 { "ws_10", switchws
, 0, {.id
= 9} },
7415 { "ws_11", switchws
, 0, {.id
= 10} },
7416 { "ws_12", switchws
, 0, {.id
= 11} },
7417 { "ws_13", switchws
, 0, {.id
= 12} },
7418 { "ws_14", switchws
, 0, {.id
= 13} },
7419 { "ws_15", switchws
, 0, {.id
= 14} },
7420 { "ws_16", switchws
, 0, {.id
= 15} },
7421 { "ws_17", switchws
, 0, {.id
= 16} },
7422 { "ws_18", switchws
, 0, {.id
= 17} },
7423 { "ws_19", switchws
, 0, {.id
= 18} },
7424 { "ws_20", switchws
, 0, {.id
= 19} },
7425 { "ws_21", switchws
, 0, {.id
= 20} },
7426 { "ws_22", switchws
, 0, {.id
= 21} },
7427 { "ws_next", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
7428 { "ws_next_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
7429 { "ws_next_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
7430 { "ws_prev", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
7431 { "ws_prev_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
7432 { "ws_prev_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
7433 { "ws_prior", priorws
, 0, {0} },
7434 /* SWM_DEBUG actions MUST be here: */
7435 { "debug_toggle", debug_toggle
, 0, {0} },
7436 { "dumpwins", dumpwins
, 0, {0} },
7438 { "invalid action", NULL
, 0, {0} },
7442 update_modkey(uint16_t mod
)
7446 /* Replace all instances of the old mod key. */
7447 RB_FOREACH(bp
, binding_tree
, &bindings
)
7448 if (bp
->mod
& mod_key
)
7449 bp
->mod
= (bp
->mod
& ~mod_key
) | mod
;
7454 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7457 struct spawn_prog
*prog
= NULL
;
7459 char *ap
, **real_args
;
7461 /* suppress unused warning since var is needed */
7464 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
7467 TAILQ_FOREACH(prog
, &spawns
, entry
) {
7468 if (strcasecmp(spawn_name
, prog
->name
) == 0)
7472 warnx("spawn_custom: program %s not found", spawn_name
);
7476 /* make room for expanded args */
7477 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
7478 err(1, "spawn_custom: calloc real_args");
7480 /* expand spawn_args into real_args */
7481 for (i
= c
= 0; i
< prog
->argc
; i
++) {
7483 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
7484 if (strcasecmp(ap
, "$bar_border") == 0) {
7486 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
7488 err(1, "spawn_custom border color");
7489 } else if (strcasecmp(ap
, "$bar_color") == 0) {
7491 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
7493 err(1, "spawn_custom bar color");
7494 } else if (strcasecmp(ap
, "$bar_font") == 0) {
7495 if ((real_args
[c
] = strdup(bar_fonts
))
7497 err(1, "spawn_custom bar fonts");
7498 } else if (strcasecmp(ap
, "$bar_font_color") == 0) {
7500 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
7502 err(1, "spawn_custom color font");
7503 } else if (strcasecmp(ap
, "$color_focus") == 0) {
7505 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
7507 err(1, "spawn_custom color focus");
7508 } else if (strcasecmp(ap
, "$color_focus_maximized") == 0) {
7510 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].name
))
7512 err(1, "spawn_custom color focus maximized");
7513 } else if (strcasecmp(ap
, "$color_unfocus") == 0) {
7515 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
7517 err(1, "spawn_custom color unfocus");
7518 } else if (strcasecmp(ap
, "$color_unfocus_maximized") == 0) {
7520 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].name
))
7522 err(1, "spawn_custom color unfocus maximized");
7523 } else if (strcasecmp(ap
, "$region_index") == 0) {
7524 if (asprintf(&real_args
[c
], "%d",
7525 get_region_index(r
) + 1) < 1)
7526 err(1, "spawn_custom region index");
7527 } else if (strcasecmp(ap
, "$workspace_index") == 0) {
7528 if (asprintf(&real_args
[c
], "%d", r
->ws
->idx
+ 1) < 1)
7529 err(1, "spawn_custom workspace index");
7530 } else if (strcasecmp(ap
, "$dmenu_bottom") == 0) {
7533 if ((real_args
[c
] = strdup("-b")) == NULL
)
7534 err(1, "spawn_custom workspace index");
7536 /* no match --> copy as is */
7537 if ((real_args
[c
] = strdup(ap
)) == NULL
)
7538 err(1, "spawn_custom strdup(ap)");
7540 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
7546 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
7547 for (i
= 0; i
< c
; ++i
)
7548 DPRINTF("\"%s\" ", real_args
[i
]);
7551 *ret_args
= real_args
;
7556 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
7562 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7566 spawn(r
->ws
->idx
, &a
, true);
7568 for (i
= 0; i
< spawn_argc
; i
++)
7574 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7581 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7585 if (pipe(select_list_pipe
) == -1)
7586 err(1, "pipe error");
7587 if (pipe(select_resp_pipe
) == -1)
7588 err(1, "pipe error");
7590 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
7591 err(1, "could not disable SIGPIPE");
7592 switch (*pid
= fork()) {
7594 err(1, "cannot fork");
7597 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
7599 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
7601 close(select_list_pipe
[1]);
7602 close(select_resp_pipe
[0]);
7603 spawn(r
->ws
->idx
, &a
, false);
7605 default: /* parent */
7606 close(select_list_pipe
[0]);
7607 close(select_resp_pipe
[1]);
7611 for (i
= 0; i
< spawn_argc
; i
++)
7616 /* Argument tokenizer. */
7619 char *arg
, *cp
, *next
;
7620 bool single_quoted
= false, double_quoted
= false;
7625 /* Eat and move characters until end of argument is found. */
7626 for (arg
= next
= cp
= *sp
; *cp
!= '\0'; ++cp
) {
7627 if (!double_quoted
&& *cp
== '\'') {
7628 /* Eat single-quote. */
7629 single_quoted
= !single_quoted
;
7630 } else if (!single_quoted
&& *cp
== '"') {
7631 /* Eat double-quote. */
7632 double_quoted
= !double_quoted
;
7633 } else if (!single_quoted
&& *cp
== '\\' && *(cp
+ 1) == '"') {
7634 /* Eat backslash; copy escaped character to arg. */
7636 } else if (!single_quoted
&& !double_quoted
&& *cp
== '\\' &&
7637 (*(cp
+ 1) == '\'' || *(cp
+ 1) == ' ')) {
7638 /* Eat backslash; move escaped character. */
7640 } else if (!single_quoted
&& !double_quoted
&&
7641 (*cp
== ' ' || *cp
== '\t')) {
7642 /* Terminate argument. */
7644 /* Point sp to beginning of next argument. */
7648 /* Move regular character. */
7653 /* Terminate argument if end of string. */
7663 spawn_insert(const char *name
, const char *args
, int flags
)
7665 struct spawn_prog
*sp
;
7666 char *arg
, *cp
, *ptr
;
7668 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s[%s]\n", name
, args
);
7670 if (args
== NULL
|| *args
== '\0')
7673 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
7674 err(1, "spawn_insert: calloc");
7675 if ((sp
->name
= strdup(name
)) == NULL
)
7676 err(1, "spawn_insert: strdup");
7678 /* Convert the arguments to an argument list. */
7679 if ((ptr
= cp
= strdup(args
)) == NULL
)
7680 err(1, "spawn_insert: strdup");
7681 while ((arg
= argsep(&ptr
)) != NULL
) {
7682 /* Null argument; skip it. */
7687 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
7688 sizeof *sp
->argv
)) == NULL
)
7689 err(1, "spawn_insert: realloc");
7690 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
7691 err(1, "spawn_insert: strdup");
7697 DNPRINTF(SWM_D_SPAWN
, "arg %d: [%s]\n", sp
->argc
, sp
->argv
[sp
->argc
-1]);
7698 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
7699 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
7703 spawn_remove(struct spawn_prog
*sp
)
7707 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
7709 TAILQ_REMOVE(&spawns
, sp
, entry
);
7710 for (i
= 0; i
< sp
->argc
; i
++)
7716 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
7722 struct spawn_prog
*sp
;
7724 while ((sp
= TAILQ_FIRST(&spawns
)) != NULL
) {
7730 spawn_find(const char *name
)
7732 struct spawn_prog
*sp
;
7734 TAILQ_FOREACH(sp
, &spawns
, entry
)
7735 if (strcasecmp(sp
->name
, name
) == 0)
7742 setspawn(const char *name
, const char *args
, int flags
)
7744 struct spawn_prog
*sp
;
7746 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
7751 /* Remove any old spawn under the same name. */
7752 if ((sp
= spawn_find(name
)) != NULL
)
7756 spawn_insert(name
, args
, flags
);
7758 warnx("error: setspawn: cannot find program: %s", name
);
7760 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
7764 setconfspawn(const char *selector
, const char *value
, int flags
)
7768 if (selector
== NULL
|| strlen(selector
) == 0)
7771 args
= expand_tilde(value
);
7773 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
7775 setspawn(selector
, args
, flags
);
7778 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
7783 validate_spawns(void)
7786 struct spawn_prog
*sp
;
7787 char which
[PATH_MAX
];
7790 RB_FOREACH(bp
, binding_tree
, &bindings
) {
7791 if (bp
->action
!= FN_SPAWN_CUSTOM
)
7795 sp
= spawn_find(bp
->spawn_name
);
7796 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
7799 /* verify we have the goods */
7800 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
7801 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
7803 for (i
= strlen("which "); i
< strlen(which
); i
++)
7804 if (which
[i
] == ' ') {
7808 if (system(which
) != 0)
7809 add_startup_exception("could not find %s",
7810 &which
[strlen("which ")]);
7817 setconfspawn("lock", "xlock", 0);
7819 setconfspawn("term", "xterm", 0);
7820 setconfspawn("spawn_term", "xterm", 0);
7822 setconfspawn("menu", "dmenu_run"
7826 " -nf $bar_font_color"
7828 " -sf $bar_color", 0);
7830 setconfspawn("search", "dmenu"
7835 " -nf $bar_font_color"
7837 " -sf $bar_color", 0);
7839 setconfspawn("name_workspace", "dmenu"
7844 " -nf $bar_font_color"
7846 " -sf $bar_color", 0);
7848 /* These are not verified for existence, even with a binding set. */
7849 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
7850 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
7851 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
7855 #define SWM_MODNAME_SIZE 32
7856 #define SWM_KEY_WS "\n+ \t"
7858 parsebinding(const char *bindstr
, uint16_t *mod
, enum binding_type
*type
,
7859 uint32_t *val
, uint32_t *flags
)
7861 char *str
, *cp
, *name
;
7862 KeySym ks
, lks
, uks
;
7864 DNPRINTF(SWM_D_KEY
, "parsebinding: enter [%s]\n", bindstr
);
7865 if (mod
== NULL
|| val
== NULL
) {
7866 DNPRINTF(SWM_D_KEY
, "parsebinding: no mod or key vars\n");
7869 if (bindstr
== NULL
|| strlen(bindstr
) == 0) {
7870 DNPRINTF(SWM_D_KEY
, "parsebinding: no bindstr\n");
7874 if ((cp
= str
= strdup(bindstr
)) == NULL
)
7875 err(1, "parsebinding: strdup");
7877 *val
= XCB_NO_SYMBOL
;
7881 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
7882 DNPRINTF(SWM_D_KEY
, "parsebinding: entry [%s]\n", name
);
7884 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
7885 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
7887 else if (strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
) == 0)
7888 *mod
|= XCB_MOD_MASK_1
;
7889 else if (strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
) == 0)
7890 *mod
|= XCB_MOD_MASK_2
;
7891 else if (strncmp(name
, "Mod3", SWM_MODNAME_SIZE
) == 0)
7892 *mod
|= XCB_MOD_MASK_3
;
7893 else if (strncmp(name
, "Mod4", SWM_MODNAME_SIZE
) == 0)
7894 *mod
|= XCB_MOD_MASK_4
;
7895 else if (strncmp(name
, "Mod5", SWM_MODNAME_SIZE
) == 0)
7896 *mod
|= XCB_MOD_MASK_5
;
7897 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
7898 *mod
|= XCB_MOD_MASK_SHIFT
;
7899 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
7900 *mod
|= XCB_MOD_MASK_CONTROL
;
7901 else if (strncasecmp(name
, "ANYMOD", SWM_MODNAME_SIZE
) == 0)
7902 *mod
|= XCB_MOD_MASK_ANY
;
7903 else if (strncasecmp(name
, "REPLAY", SWM_MODNAME_SIZE
) == 0)
7904 *flags
|= BINDING_F_REPLAY
;
7905 else if (sscanf(name
, "Button%u", val
) == 1) {
7906 DNPRINTF(SWM_D_KEY
, "parsebinding: button %u\n", *val
);
7908 if (*val
> 255 || *val
== 0) {
7910 "parsebinding: invalid button %u\n", *val
);
7914 /* TODO: do this without Xlib. */
7915 ks
= XStringToKeysym(name
);
7916 if (ks
== NoSymbol
) {
7918 "parsebinding: invalid key %s\n", name
);
7923 XConvertCase(ks
, &lks
, &uks
);
7924 *val
= (uint32_t)lks
;
7928 /* If ANYMOD was specified, ignore the rest. */
7929 if (*mod
& XCB_MOD_MASK_ANY
)
7930 *mod
= XCB_MOD_MASK_ANY
;
7933 DNPRINTF(SWM_D_KEY
, "parsebinding: leave ok\n");
7938 strdupsafe(const char *str
)
7943 return (strdup(str
));
7947 binding_cmp(struct binding
*bp1
, struct binding
*bp2
)
7949 if (bp1
->type
< bp2
->type
)
7951 if (bp1
->type
> bp2
->type
)
7954 if (bp1
->value
< bp2
->value
)
7956 if (bp1
->value
> bp2
->value
)
7959 if (bp1
->mod
< bp2
->mod
)
7961 if (bp1
->mod
> bp2
->mod
)
7968 binding_insert(uint16_t mod
, enum binding_type type
, uint32_t val
,
7969 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
7973 DNPRINTF(SWM_D_KEY
, "binding_insert: mod: %u, type: %d, val: %u, "
7974 "action: %s(%d), spawn_name: %s\n", mod
, type
, val
,
7975 actions
[aid
].name
, aid
, spawn_name
);
7977 if ((bp
= malloc(sizeof *bp
)) == NULL
)
7978 err(1, "binding_insert: malloc");
7985 bp
->spawn_name
= strdupsafe(spawn_name
);
7986 RB_INSERT(binding_tree
, &bindings
, bp
);
7988 DNPRINTF(SWM_D_KEY
, "binding_insert: leave\n");
7992 binding_lookup(uint16_t mod
, enum binding_type type
, uint32_t val
)
8000 return (RB_FIND(binding_tree
, &bindings
, &bp
));
8004 binding_remove(struct binding
*bp
)
8006 DNPRINTF(SWM_D_KEY
, "binding_remove: mod: %u, type: %d, val: %u, "
8007 "action: %s(%d), spawn_name: %s\n", bp
->mod
, bp
->type
, bp
->value
,
8008 actions
[bp
->action
].name
, bp
->action
, bp
->spawn_name
);
8010 RB_REMOVE(binding_tree
, &bindings
, bp
);
8011 free(bp
->spawn_name
);
8014 DNPRINTF(SWM_D_KEY
, "binding_remove: leave\n");
8018 setbinding(uint16_t mod
, enum binding_type type
, uint32_t val
,
8019 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
8023 DNPRINTF(SWM_D_KEY
, "setbinding: enter %s [%s]\n",
8024 actions
[aid
].name
, spawn_name
);
8026 /* Unbind any existing. Loop is to handle MOD_MASK_ANY. */
8027 while ((bp
= binding_lookup(mod
, type
, val
)))
8030 if (aid
!= FN_INVALID
)
8031 binding_insert(mod
, type
, val
, aid
, flags
, spawn_name
);
8033 DNPRINTF(SWM_D_KEY
, "setbinding: leave\n");
8037 setconfbinding(const char *selector
, const char *value
, int flags
)
8039 struct spawn_prog
*sp
;
8040 uint32_t keybtn
, opts
;
8043 enum binding_type type
;
8045 /* suppress unused warning since var is needed */
8048 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter selector: [%s], "
8049 "value: [%s]\n", selector
, value
);
8050 if (selector
== NULL
|| strlen(selector
) == 0) {
8051 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
8052 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8053 setbinding(mod
, type
, keybtn
, FN_INVALID
, opts
, NULL
);
8058 /* search by key function name */
8059 for (aid
= 0; aid
< FN_INVALID
; aid
++) {
8060 if (strncasecmp(selector
, actions
[aid
].name
,
8061 SWM_FUNCNAME_LEN
) == 0) {
8062 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8063 "action\n", selector
);
8064 if (parsebinding(value
, &mod
, &type
, &keybtn
,
8066 setbinding(mod
, type
, keybtn
, aid
, opts
, NULL
);
8072 /* search by custom spawn name */
8073 if ((sp
= spawn_find(selector
)) != NULL
) {
8074 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8075 "spawn\n", selector
);
8076 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8077 setbinding(mod
, type
, keybtn
, FN_SPAWN_CUSTOM
, opts
,
8083 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
8087 #define MODSHIFT MODKEY | XCB_MOD_MASK_SHIFT
8089 setup_keybindings(void)
8091 #define BINDKEY(m, k, a) setbinding(m, KEYBIND, k, a, 0, NULL)
8092 #define BINDKEYSPAWN(m, k, s) setbinding(m, KEYBIND, k, FN_SPAWN_CUSTOM, 0, s)
8093 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8094 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8095 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8096 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8097 BINDKEY(MODKEY
, XK_v
, FN_BUTTON2
);
8098 BINDKEY(MODKEY
, XK_space
, FN_CYCLE_LAYOUT
);
8099 BINDKEY(MODSHIFT
, XK_backslash
, FN_FLIP_LAYOUT
);
8100 BINDKEY(MODKEY
, XK_t
, FN_FLOAT_TOGGLE
);
8101 BINDKEY(MODKEY
, XK_m
, FN_FOCUS_MAIN
);
8102 BINDKEY(MODKEY
, XK_j
, FN_FOCUS_NEXT
);
8103 BINDKEY(MODKEY
, XK_Tab
, FN_FOCUS_NEXT
);
8104 BINDKEY(MODKEY
, XK_k
, FN_FOCUS_PREV
);
8105 BINDKEY(MODSHIFT
, XK_Tab
, FN_FOCUS_PREV
);
8106 BINDKEY(MODKEY
, XK_u
, FN_FOCUS_URGENT
);
8107 BINDKEY(MODKEY
, XK_e
, FN_MAXIMIZE_TOGGLE
);
8108 BINDKEY(MODSHIFT
, XK_equal
, FN_HEIGHT_GROW
);
8109 BINDKEY(MODSHIFT
, XK_minus
, FN_HEIGHT_SHRINK
);
8110 BINDKEY(MODKEY
, XK_w
, FN_ICONIFY
);
8111 BINDKEY(MODKEY
, XK_h
, FN_MASTER_SHRINK
);
8112 BINDKEY(MODKEY
, XK_l
, FN_MASTER_GROW
);
8113 BINDKEY(MODKEY
, XK_comma
, FN_MASTER_ADD
);
8114 BINDKEY(MODKEY
, XK_period
, FN_MASTER_DEL
);
8115 BINDKEY(MODSHIFT
, XK_bracketright
, FN_MOVE_DOWN
);
8116 BINDKEY(MODKEY
, XK_bracketleft
, FN_MOVE_LEFT
);
8117 BINDKEY(MODKEY
, XK_bracketright
, FN_MOVE_RIGHT
);
8118 BINDKEY(MODSHIFT
, XK_bracketleft
, FN_MOVE_UP
);
8119 BINDKEY(MODSHIFT
, XK_KP_End
, FN_MVRG_1
);
8120 BINDKEY(MODSHIFT
, XK_KP_Down
, FN_MVRG_2
);
8121 BINDKEY(MODSHIFT
, XK_KP_Next
, FN_MVRG_3
);
8122 BINDKEY(MODSHIFT
, XK_KP_Left
, FN_MVRG_4
);
8123 BINDKEY(MODSHIFT
, XK_KP_Begin
, FN_MVRG_5
);
8124 BINDKEY(MODSHIFT
, XK_KP_Right
, FN_MVRG_6
);
8125 BINDKEY(MODSHIFT
, XK_KP_Home
, FN_MVRG_7
);
8126 BINDKEY(MODSHIFT
, XK_KP_Up
, FN_MVRG_8
);
8127 BINDKEY(MODSHIFT
, XK_KP_Prior
, FN_MVRG_9
);
8128 BINDKEY(MODSHIFT
, XK_1
, FN_MVWS_1
);
8129 BINDKEY(MODSHIFT
, XK_2
, FN_MVWS_2
);
8130 BINDKEY(MODSHIFT
, XK_3
, FN_MVWS_3
);
8131 BINDKEY(MODSHIFT
, XK_4
, FN_MVWS_4
);
8132 BINDKEY(MODSHIFT
, XK_5
, FN_MVWS_5
);
8133 BINDKEY(MODSHIFT
, XK_6
, FN_MVWS_6
);
8134 BINDKEY(MODSHIFT
, XK_7
, FN_MVWS_7
);
8135 BINDKEY(MODSHIFT
, XK_8
, FN_MVWS_8
);
8136 BINDKEY(MODSHIFT
, XK_9
, FN_MVWS_9
);
8137 BINDKEY(MODSHIFT
, XK_0
, FN_MVWS_10
);
8138 BINDKEY(MODSHIFT
, XK_F1
, FN_MVWS_11
);
8139 BINDKEY(MODSHIFT
, XK_F2
, FN_MVWS_12
);
8140 BINDKEY(MODSHIFT
, XK_F3
, FN_MVWS_13
);
8141 BINDKEY(MODSHIFT
, XK_F4
, FN_MVWS_14
);
8142 BINDKEY(MODSHIFT
, XK_F5
, FN_MVWS_15
);
8143 BINDKEY(MODSHIFT
, XK_F6
, FN_MVWS_16
);
8144 BINDKEY(MODSHIFT
, XK_F7
, FN_MVWS_17
);
8145 BINDKEY(MODSHIFT
, XK_F8
, FN_MVWS_18
);
8146 BINDKEY(MODSHIFT
, XK_F9
, FN_MVWS_19
);
8147 BINDKEY(MODSHIFT
, XK_F10
, FN_MVWS_20
);
8148 BINDKEY(MODSHIFT
, XK_F11
, FN_MVWS_21
);
8149 BINDKEY(MODSHIFT
, XK_F12
, FN_MVWS_22
);
8150 BINDKEY(MODSHIFT
, XK_slash
, FN_NAME_WORKSPACE
);
8151 BINDKEY(MODSHIFT
, XK_q
, FN_QUIT
);
8152 BINDKEY(MODSHIFT
, XK_r
, FN_RAISE_TOGGLE
);
8153 BINDKEY(MODKEY
, XK_q
, FN_RESTART
);
8154 BINDKEY(MODKEY
, XK_KP_End
, FN_RG_1
);
8155 BINDKEY(MODKEY
, XK_KP_Down
, FN_RG_2
);
8156 BINDKEY(MODKEY
, XK_KP_Next
, FN_RG_3
);
8157 BINDKEY(MODKEY
, XK_KP_Left
, FN_RG_4
);
8158 BINDKEY(MODKEY
, XK_KP_Begin
, FN_RG_5
);
8159 BINDKEY(MODKEY
, XK_KP_Right
, FN_RG_6
);
8160 BINDKEY(MODKEY
, XK_KP_Home
, FN_RG_7
);
8161 BINDKEY(MODKEY
, XK_KP_Up
, FN_RG_8
);
8162 BINDKEY(MODKEY
, XK_KP_Prior
, FN_RG_9
);
8163 BINDKEY(MODSHIFT
, XK_Right
, FN_RG_NEXT
);
8164 BINDKEY(MODSHIFT
, XK_Left
, FN_RG_PREV
);
8165 BINDKEY(MODKEY
, XK_f
, FN_SEARCH_WIN
);
8166 BINDKEY(MODKEY
, XK_slash
, FN_SEARCH_WORKSPACE
);
8167 BINDKEYSPAWN(MODSHIFT
, XK_i
, "initscr");
8168 BINDKEYSPAWN(MODSHIFT
, XK_Delete
, "lock");
8169 BINDKEYSPAWN(MODKEY
, XK_p
, "menu");
8170 BINDKEYSPAWN(MODKEY
, XK_s
, "screenshot_all");
8171 BINDKEYSPAWN(MODSHIFT
, XK_s
, "screenshot_wind");
8172 BINDKEYSPAWN(MODSHIFT
, XK_Return
, "term");
8173 BINDKEY(MODSHIFT
, XK_comma
, FN_STACK_INC
);
8174 BINDKEY(MODSHIFT
, XK_period
, FN_STACK_DEC
);
8175 BINDKEY(MODSHIFT
, XK_space
, FN_STACK_RESET
);
8176 BINDKEY(MODKEY
, XK_Return
, FN_SWAP_MAIN
);
8177 BINDKEY(MODSHIFT
, XK_j
, FN_SWAP_NEXT
);
8178 BINDKEY(MODSHIFT
, XK_k
, FN_SWAP_PREV
);
8179 BINDKEY(MODSHIFT
, XK_w
, FN_UNICONIFY
);
8180 BINDKEY(MODSHIFT
, XK_v
, FN_VERSION
);
8181 BINDKEY(MODKEY
, XK_equal
, FN_WIDTH_GROW
);
8182 BINDKEY(MODKEY
, XK_minus
, FN_WIDTH_SHRINK
);
8183 BINDKEY(MODKEY
, XK_x
, FN_WIND_DEL
);
8184 BINDKEY(MODSHIFT
, XK_x
, FN_WIND_KILL
);
8185 BINDKEY(MODKEY
, XK_1
, FN_WS_1
);
8186 BINDKEY(MODKEY
, XK_2
, FN_WS_2
);
8187 BINDKEY(MODKEY
, XK_3
, FN_WS_3
);
8188 BINDKEY(MODKEY
, XK_4
, FN_WS_4
);
8189 BINDKEY(MODKEY
, XK_5
, FN_WS_5
);
8190 BINDKEY(MODKEY
, XK_6
, FN_WS_6
);
8191 BINDKEY(MODKEY
, XK_7
, FN_WS_7
);
8192 BINDKEY(MODKEY
, XK_8
, FN_WS_8
);
8193 BINDKEY(MODKEY
, XK_9
, FN_WS_9
);
8194 BINDKEY(MODKEY
, XK_0
, FN_WS_10
);
8195 BINDKEY(MODKEY
, XK_F1
, FN_WS_11
);
8196 BINDKEY(MODKEY
, XK_F2
, FN_WS_12
);
8197 BINDKEY(MODKEY
, XK_F3
, FN_WS_13
);
8198 BINDKEY(MODKEY
, XK_F4
, FN_WS_14
);
8199 BINDKEY(MODKEY
, XK_F5
, FN_WS_15
);
8200 BINDKEY(MODKEY
, XK_F6
, FN_WS_16
);
8201 BINDKEY(MODKEY
, XK_F7
, FN_WS_17
);
8202 BINDKEY(MODKEY
, XK_F8
, FN_WS_18
);
8203 BINDKEY(MODKEY
, XK_F9
, FN_WS_19
);
8204 BINDKEY(MODKEY
, XK_F10
, FN_WS_20
);
8205 BINDKEY(MODKEY
, XK_F11
, FN_WS_21
);
8206 BINDKEY(MODKEY
, XK_F12
, FN_WS_22
);
8207 BINDKEY(MODKEY
, XK_Right
, FN_WS_NEXT
);
8208 BINDKEY(MODKEY
, XK_Left
, FN_WS_PREV
);
8209 BINDKEY(MODKEY
, XK_Up
, FN_WS_NEXT_ALL
);
8210 BINDKEY(MODKEY
, XK_Down
, FN_WS_PREV_ALL
);
8211 BINDKEY(MODSHIFT
, XK_Up
, FN_WS_NEXT_MOVE
);
8212 BINDKEY(MODSHIFT
, XK_Down
, FN_WS_PREV_MOVE
);
8213 BINDKEY(MODKEY
, XK_a
, FN_WS_PRIOR
);
8215 BINDKEY(MODKEY
, XK_d
, FN_DEBUG_TOGGLE
);
8216 BINDKEY(MODSHIFT
, XK_d
, FN_DUMPWINS
);
8223 setup_btnbindings(void)
8225 setbinding(ANYMOD
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_FOCUS
,
8226 BINDING_F_REPLAY
, NULL
);
8227 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE
, 0, NULL
);
8228 setbinding(MODSHIFT
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE_CENTERED
, 0,
8230 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_MOVE
, 0, NULL
);
8235 clear_bindings(void)
8239 while ((bp
= RB_ROOT(&bindings
)))
8244 clear_keybindings(void)
8246 struct binding
*bp
, *bptmp
;
8248 RB_FOREACH_SAFE(bp
, binding_tree
, &bindings
, bptmp
) {
8249 if (bp
->type
!= KEYBIND
)
8256 setkeymapping(const char *selector
, const char *value
, int flags
)
8258 char *keymapping_file
;
8260 /* suppress unused warnings since vars are needed */
8264 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
8266 keymapping_file
= expand_tilde(value
);
8268 clear_keybindings();
8269 /* load new key bindings; if it fails, revert to default bindings */
8270 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
8271 clear_keybindings();
8272 setup_keybindings();
8275 free(keymapping_file
);
8277 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
8282 updatenumlockmask(void)
8285 xcb_get_modifier_mapping_reply_t
*modmap_r
;
8286 xcb_keycode_t
*modmap
, kc
, *keycode
;
8290 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
8291 xcb_get_modifier_mapping(conn
),
8294 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
8295 for (i
= 0; i
< 8; i
++) {
8296 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
8297 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
8299 keycode
= xcb_key_symbols_get_keycode(syms
,
8303 numlockmask
= (1 << i
);
8310 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
8317 int num_screens
, k
, j
;
8318 uint16_t modifiers
[4];
8319 xcb_keycode_t
*code
;
8321 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
8322 updatenumlockmask();
8325 modifiers
[1] = numlockmask
;
8326 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8327 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8329 num_screens
= get_screen_count();
8330 for (k
= 0; k
< num_screens
; k
++) {
8331 if (TAILQ_EMPTY(&screens
[k
].rl
))
8333 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
8335 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8336 if (bp
->type
!= KEYBIND
)
8339 /* If there is a catch-all, only bind that. */
8340 if ((binding_lookup(ANYMOD
, KEYBIND
, bp
->value
)) &&
8344 /* Skip unused ws binds. */
8345 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8346 bp
->action
<= FN_WS_22
)
8349 /* Skip unused mvws binds. */
8350 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8351 bp
->action
<= FN_MVWS_22
)
8354 if ((code
= xcb_key_symbols_get_keycode(syms
,
8355 bp
->value
)) == NULL
)
8358 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8359 /* All modifiers are grabbed in one pass. */
8360 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8361 "key: %u, modifiers: %d\n", bp
->value
,
8363 xcb_grab_key(conn
, 1, screens
[k
].root
,
8364 bp
->mod
, *code
, XCB_GRAB_MODE_ASYNC
,
8365 XCB_GRAB_MODE_SYNC
);
8367 /* Need to grab each modifier permutation. */
8368 for (j
= 0; j
< LENGTH(modifiers
); j
++) {
8369 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8370 "key: %u, modifiers: %d\n",
8371 bp
->value
, bp
->mod
| modifiers
[j
]);
8372 xcb_grab_key(conn
, 1,
8374 bp
->mod
| modifiers
[j
],
8375 *code
, XCB_GRAB_MODE_ASYNC
,
8376 XCB_GRAB_MODE_SYNC
);
8388 int num_screens
, i
, k
;
8389 uint16_t modifiers
[4];
8391 DNPRINTF(SWM_D_MOUSE
, "grabbuttons\n");
8392 updatenumlockmask();
8395 modifiers
[1] = numlockmask
;
8396 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8397 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8399 num_screens
= get_screen_count();
8400 for (k
= 0; k
< num_screens
; k
++) {
8401 if (TAILQ_EMPTY(&screens
[k
].rl
))
8403 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, screens
[k
].root
,
8405 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8406 if (bp
->type
!= BTNBIND
)
8409 /* If there is a catch-all, only bind that. */
8410 if ((binding_lookup(ANYMOD
, BTNBIND
, bp
->value
)) &&
8414 /* Skip unused ws binds. */
8415 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8416 bp
->action
<= FN_WS_22
)
8419 /* Skip unused mvws binds. */
8420 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8421 bp
->action
<= FN_MVWS_22
)
8424 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8425 /* All modifiers are grabbed in one pass. */
8426 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: grab, "
8427 "button: %u, modifiers: %d\n", bp
->value
,
8429 xcb_grab_button(conn
, 0, screens
[k
].root
,
8430 BUTTONMASK
, XCB_GRAB_MODE_SYNC
,
8431 XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
8432 XCB_CURSOR_NONE
, bp
->value
, bp
->mod
);
8434 /* Need to grab each modifier permutation. */
8435 for (i
= 0; i
< LENGTH(modifiers
); ++i
) {
8436 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: "
8437 "grab, button: %u, modifiers: %u\n",
8438 bp
->value
, bp
->mod
| modifiers
[i
]);
8439 xcb_grab_button(conn
, 0,
8440 screens
[k
].root
, BUTTONMASK
,
8442 XCB_GRAB_MODE_ASYNC
,
8444 XCB_CURSOR_NONE
, bp
->value
,
8445 bp
->mod
| modifiers
[i
]);
8452 const char *quirkname
[] = {
8453 "NONE", /* config string for "no value" */
8461 "FOCUSONMAP_SINGLE",
8469 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
8470 #define SWM_Q_DELIM "\n|+ \t"
8472 parsequirks(const char *qstr
, uint32_t *quirk
, int *ws
)
8474 char *str
, *cp
, *name
;
8477 if (quirk
== NULL
|| qstr
== NULL
)
8480 if ((cp
= str
= strdup(qstr
)) == NULL
)
8481 err(1, "parsequirks: strdup");
8484 while ((name
= strsep(&cp
, SWM_Q_DELIM
)) != NULL
) {
8486 cp
+= (long)strspn(cp
, SWM_Q_DELIM
);
8488 if (sscanf(name
, "WS[%d]", ws
) == 1) {
8494 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
8495 if (strncasecmp(name
, quirkname
[i
],
8496 SWM_QUIRK_LEN
) == 0) {
8497 DNPRINTF(SWM_D_QUIRK
,
8498 "parsequirks: %s\n", name
);
8504 *quirk
|= 1 << (i
-1);
8508 if (i
>= LENGTH(quirkname
)) {
8509 DNPRINTF(SWM_D_QUIRK
,
8510 "parsequirks: invalid quirk [%s]\n", name
);
8521 quirk_insert(const char *class, const char *instance
, const char *name
,
8522 uint32_t quirk
, int ws
)
8526 bool failed
= false;
8528 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: class: %s, instance: %s, name: %s,"
8529 " value: %u, ws: %d\n", class, instance
, name
, quirk
, ws
);
8531 if ((qp
= malloc(sizeof *qp
)) == NULL
)
8532 err(1, "quirk_insert: malloc");
8534 if ((qp
->class = strdup(class)) == NULL
)
8535 err(1, "quirk_insert: strdup");
8536 if ((qp
->instance
= strdup(instance
)) == NULL
)
8537 err(1, "quirk_insert: strdup");
8538 if ((qp
->name
= strdup(name
)) == NULL
)
8539 err(1, "quirk_insert: strdup");
8541 if (asprintf(&str
, "^%s$", class) == -1)
8542 err(1, "quirk_insert: asprintf");
8543 if (regcomp(&qp
->regex_class
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8544 add_startup_exception("regex failed to compile quirk 'class' "
8545 "field: %s", class);
8548 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8551 if (asprintf(&str
, "^%s$", instance
) == -1)
8552 err(1, "quirk_insert: asprintf");
8553 if (regcomp(&qp
->regex_instance
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8554 add_startup_exception("regex failed to compile quirk 'instance'"
8555 " field: %s", instance
);
8558 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8561 if (asprintf(&str
, "^%s$", name
) == -1)
8562 err(1, "quirk_insert: asprintf");
8563 if (regcomp(&qp
->regex_name
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8564 add_startup_exception("regex failed to compile quirk 'name' "
8568 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8572 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: regex error; skipping.\n");
8577 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
8579 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
8583 quirk_remove(struct quirk
*qp
)
8585 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%u]\n", qp
->class,
8586 qp
->name
, qp
->quirk
);
8588 TAILQ_REMOVE(&quirks
, qp
, entry
);
8591 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
8595 quirk_free(struct quirk
*qp
)
8597 regfree(&qp
->regex_class
);
8598 regfree(&qp
->regex_instance
);
8599 regfree(&qp
->regex_name
);
8611 while ((qp
= TAILQ_FIRST(&quirks
)) != NULL
) {
8617 quirk_replace(struct quirk
*qp
, const char *class, const char *instance
,
8618 const char *name
, uint32_t quirk
, int ws
)
8620 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp
->class,
8621 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
8624 quirk_insert(class, instance
, name
, quirk
, ws
);
8626 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
8630 setquirk(const char *class, const char *instance
, const char *name
,
8631 uint32_t quirk
, int ws
)
8635 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
8636 instance
, name
, quirk
, ws
);
8638 /* Remove/replace existing quirk. */
8639 TAILQ_FOREACH(qp
, &quirks
, entry
) {
8640 if (strcmp(qp
->class, class) == 0 &&
8641 strcmp(qp
->instance
, instance
) == 0 &&
8642 strcmp(qp
->name
, name
) == 0) {
8643 if (quirk
== 0 && ws
== -1)
8646 quirk_replace(qp
, class, instance
, name
, quirk
,
8648 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8653 /* Only insert if quirk is not NONE or forced ws is set. */
8654 if (quirk
|| ws
!= -1)
8655 quirk_insert(class, instance
, name
, quirk
, ws
);
8657 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8660 /* Eat '\' in str used to escape square brackets and colon. */
8662 unescape_selector(char *str
)
8666 for (cp
= str
; *str
!= '\0'; ++str
, ++cp
) {
8667 if (*str
== '\\' && (*(str
+ 1) == ':' || *(str
+ 1) == ']' ||
8677 setconfquirk(const char *selector
, const char *value
, int flags
)
8679 char *str
, *cp
, *class;
8680 char *instance
= NULL
, *name
= NULL
;
8681 int retval
, count
= 0, ws
= -1;
8684 /* suppress unused warning since var is needed */
8687 if (selector
== NULL
|| strlen(selector
) == 0)
8690 if ((str
= strdup(selector
)) == NULL
)
8691 err(1, "setconfquirk: strdup");
8693 /* Find non-escaped colon. */
8700 for (++cp
; *cp
!= '\0'; ++cp
) {
8701 if (*cp
== ':' && *(cp
- 1) != '\\') {
8707 unescape_selector(class);
8709 instance
= class + strlen(class) + 1;
8710 unescape_selector(instance
);
8716 name
= instance
+ strlen(instance
) + 1;
8717 unescape_selector(name
);
8722 DNPRINTF(SWM_D_CONF
, "setconfquirk: class: %s, instance: %s, "
8723 "name: %s\n", class, instance
, name
);
8725 if ((retval
= parsequirks(value
, &qrks
, &ws
)) == 0)
8726 setquirk(class, instance
, name
, qrks
, ws
);
8735 setquirk("MPlayer", "xv", ".*",
8736 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
, -1);
8737 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
8739 setquirk("Firefox-bin", "firefox-bin", ".*",
8741 setquirk("Firefox", "Dialog", ".*",
8743 setquirk("Gimp", "gimp", ".*",
8744 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8745 setquirk("XTerm", "xterm", ".*",
8746 SWM_Q_XTERM_FONTADJ
, -1);
8747 setquirk("xine", "Xine Window", ".*",
8748 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8749 setquirk("Xitk", "Xitk Combo", ".*",
8750 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8751 setquirk("xine", "xine Panel", ".*",
8752 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8753 setquirk("Xitk", "Xine Window", ".*",
8754 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8755 setquirk("xine", "xine Video Fullscreen Window", ".*",
8756 SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
, -1);
8757 setquirk("pcb", "pcb", ".*",
8759 setquirk("SDL_App", "SDL_App", ".*",
8760 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
, -1);
8763 /* conf file stuff */
8764 #define SWM_CONF_FILE "spectrwm.conf"
8765 #define SWM_CONF_FILE_OLD "scrotwm.conf"
8769 SWM_S_BAR_AT_BOTTOM
,
8770 SWM_S_BAR_BORDER_WIDTH
,
8773 SWM_S_BAR_ENABLED_WS
,
8778 SWM_S_BOUNDARY_WIDTH
,
8779 SWM_S_CLOCK_ENABLED
,
8782 SWM_S_CYCLE_VISIBLE
,
8784 SWM_S_DISABLE_BORDER
,
8786 SWM_S_FOCUS_CLOSE_WRAP
,
8787 SWM_S_FOCUS_DEFAULT
,
8789 SWM_S_ICONIC_ENABLED
,
8790 SWM_S_JAVA_WORKAROUND
,
8791 SWM_S_MAXIMIZE_HIDE_BAR
,
8792 SWM_S_REGION_PADDING
,
8797 SWM_S_STACK_ENABLED
,
8800 SWM_S_URGENT_COLLAPSE
,
8801 SWM_S_URGENT_ENABLED
,
8802 SWM_S_VERBOSE_LAYOUT
,
8805 SWM_S_WINDOW_CLASS_ENABLED
,
8806 SWM_S_WINDOW_INSTANCE_ENABLED
,
8807 SWM_S_WINDOW_NAME_ENABLED
,
8808 SWM_S_WORKSPACE_CLAMP
,
8809 SWM_S_WORKSPACE_LIMIT
,
8810 SWM_S_WORKSPACE_NAME
,
8814 setconfvalue(const char *selector
, const char *value
, int flags
)
8816 struct workspace
*ws
;
8817 int i
, ws_id
, num_screens
;
8818 char *b
, *str
, *sp
, s
[1024];
8821 case SWM_S_BAR_ACTION
:
8823 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
8824 err(1, "setconfvalue: bar_action");
8826 case SWM_S_BAR_AT_BOTTOM
:
8827 bar_at_bottom
= (atoi(value
) != 0);
8829 case SWM_S_BAR_BORDER_WIDTH
:
8830 bar_border_width
= atoi(value
);
8831 if (bar_border_width
< 0)
8832 bar_border_width
= 0;
8834 case SWM_S_BAR_DELAY
:
8835 /* No longer needed; leave to not break old conf files. */
8837 case SWM_S_BAR_ENABLED
:
8838 bar_enabled
= (atoi(value
) != 0);
8840 case SWM_S_BAR_ENABLED_WS
:
8841 ws_id
= atoi(selector
) - 1;
8842 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8843 errx(1, "setconfvalue: bar_enabled_ws: invalid "
8844 "workspace %d.", ws_id
+ 1);
8846 num_screens
= get_screen_count();
8847 for (i
= 0; i
< num_screens
; i
++) {
8848 ws
= (struct workspace
*)&screens
[i
].ws
;
8849 ws
[ws_id
].bar_enabled
= (atoi(value
) != 0);
8852 case SWM_S_BAR_FONT
:
8854 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
8855 err(1, "setconfvalue: asprintf: failed to allocate "
8856 "memory for bar_fonts.");
8859 /* If already in xft mode, then we are done. */
8860 if (!bar_font_legacy
)
8863 if ((sp
= str
= strdup(value
)) == NULL
)
8864 err(1, "setconfvalue: strdup");
8866 /* If there are any non-XLFD entries, switch to Xft mode. */
8867 while ((b
= strsep(&sp
, ",")) != NULL
) {
8871 bar_font_legacy
= false;
8878 case SWM_S_BAR_FORMAT
:
8880 if ((bar_format
= strdup(value
)) == NULL
)
8881 err(1, "setconfvalue: bar_format");
8883 case SWM_S_BAR_JUSTIFY
:
8884 if (strcmp(value
, "left") == 0)
8885 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
8886 else if (strcmp(value
, "center") == 0)
8887 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
8888 else if (strcmp(value
, "right") == 0)
8889 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
8891 errx(1, "invalid bar_justify");
8893 case SWM_S_BORDER_WIDTH
:
8894 border_width
= atoi(value
);
8895 if (border_width
< 0)
8898 case SWM_S_BOUNDARY_WIDTH
:
8899 boundary_width
= atoi(value
);
8900 if (boundary_width
< 0)
8903 case SWM_S_CLOCK_ENABLED
:
8904 clock_enabled
= (atoi(value
) != 0);
8906 case SWM_S_CLOCK_FORMAT
:
8907 #ifndef SWM_DENY_CLOCK_FORMAT
8909 if ((clock_format
= strdup(value
)) == NULL
)
8910 err(1, "setconfvalue: clock_format");
8913 case SWM_S_CYCLE_EMPTY
:
8914 cycle_empty
= (atoi(value
) != 0);
8916 case SWM_S_CYCLE_VISIBLE
:
8917 cycle_visible
= (atoi(value
) != 0);
8919 case SWM_S_DIALOG_RATIO
:
8920 dialog_ratio
= atof(value
);
8921 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
8924 case SWM_S_DISABLE_BORDER
:
8925 disable_border
= (atoi(value
) != 0);
8927 case SWM_S_FOCUS_CLOSE
:
8928 if (strcmp(value
, "first") == 0)
8929 focus_close
= SWM_STACK_BOTTOM
;
8930 else if (strcmp(value
, "last") == 0)
8931 focus_close
= SWM_STACK_TOP
;
8932 else if (strcmp(value
, "next") == 0)
8933 focus_close
= SWM_STACK_ABOVE
;
8934 else if (strcmp(value
, "previous") == 0)
8935 focus_close
= SWM_STACK_BELOW
;
8937 errx(1, "focus_close");
8939 case SWM_S_FOCUS_CLOSE_WRAP
:
8940 focus_close_wrap
= (atoi(value
) != 0);
8942 case SWM_S_FOCUS_DEFAULT
:
8943 if (strcmp(value
, "last") == 0)
8944 focus_default
= SWM_STACK_TOP
;
8945 else if (strcmp(value
, "first") == 0)
8946 focus_default
= SWM_STACK_BOTTOM
;
8948 errx(1, "focus_default");
8950 case SWM_S_FOCUS_MODE
:
8951 if (strcmp(value
, "default") == 0)
8952 focus_mode
= SWM_FOCUS_DEFAULT
;
8953 else if (strcmp(value
, "follow") == 0 ||
8954 strcmp(value
, "follow_cursor") == 0)
8955 focus_mode
= SWM_FOCUS_FOLLOW
;
8956 else if (strcmp(value
, "manual") == 0)
8957 focus_mode
= SWM_FOCUS_MANUAL
;
8959 errx(1, "focus_mode");
8961 case SWM_S_ICONIC_ENABLED
:
8962 iconic_enabled
= (atoi(value
) != 0);
8964 case SWM_S_JAVA_WORKAROUND
:
8965 java_workaround
= (atoi(value
) != 0);
8967 case SWM_S_MAXIMIZE_HIDE_BAR
:
8968 maximize_hide_bar
= atoi(value
);
8970 case SWM_S_REGION_PADDING
:
8971 region_padding
= atoi(value
);
8972 if (region_padding
< 0)
8975 case SWM_S_SPAWN_ORDER
:
8976 if (strcmp(value
, "first") == 0)
8977 spawn_position
= SWM_STACK_BOTTOM
;
8978 else if (strcmp(value
, "last") == 0)
8979 spawn_position
= SWM_STACK_TOP
;
8980 else if (strcmp(value
, "next") == 0)
8981 spawn_position
= SWM_STACK_ABOVE
;
8982 else if (strcmp(value
, "previous") == 0)
8983 spawn_position
= SWM_STACK_BELOW
;
8985 errx(1, "spawn_position");
8987 case SWM_S_SPAWN_TERM
:
8988 setconfspawn("term", value
, 0);
8989 setconfspawn("spawn_term", value
, 0);
8992 /* No longer needed; leave to not break old conf files. */
8994 case SWM_S_SS_ENABLED
:
8995 /* No longer needed; leave to not break old conf files. */
8997 case SWM_S_STACK_ENABLED
:
8998 stack_enabled
= (atoi(value
) != 0);
9000 case SWM_S_TERM_WIDTH
:
9001 term_width
= atoi(value
);
9005 case SWM_S_TILE_GAP
:
9006 tile_gap
= atoi(value
);
9008 case SWM_S_URGENT_COLLAPSE
:
9009 urgent_collapse
= (atoi(value
) != 0);
9011 case SWM_S_URGENT_ENABLED
:
9012 urgent_enabled
= (atoi(value
) != 0);
9014 case SWM_S_VERBOSE_LAYOUT
:
9015 verbose_layout
= (atoi(value
) != 0);
9016 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
9018 layouts
[i
].l_string
= fancy_stacker
;
9020 layouts
[i
].l_string
= plain_stacker
;
9023 case SWM_S_WARP_FOCUS
:
9024 warp_focus
= (atoi(value
) != 0);
9026 case SWM_S_WARP_POINTER
:
9027 warp_pointer
= (atoi(value
) != 0);
9029 case SWM_S_WINDOW_CLASS_ENABLED
:
9030 window_class_enabled
= (atoi(value
) != 0);
9032 case SWM_S_WINDOW_INSTANCE_ENABLED
:
9033 window_instance_enabled
= (atoi(value
) != 0);
9035 case SWM_S_WINDOW_NAME_ENABLED
:
9036 window_name_enabled
= (atoi(value
) != 0);
9038 case SWM_S_WORKSPACE_CLAMP
:
9039 workspace_clamp
= (atoi(value
) != 0);
9041 case SWM_S_WORKSPACE_LIMIT
:
9042 workspace_limit
= atoi(value
);
9043 if (workspace_limit
> SWM_WS_MAX
)
9044 workspace_limit
= SWM_WS_MAX
;
9045 else if (workspace_limit
< 1)
9046 workspace_limit
= 1;
9048 ewmh_update_desktops();
9050 case SWM_S_WORKSPACE_NAME
:
9051 if (getenv("SWM_STARTED") != NULL
)
9055 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9056 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
9058 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9059 errx(1, "setconfvalue: workspace_name: invalid "
9060 "workspace %d.", ws_id
+ 1);
9062 num_screens
= get_screen_count();
9063 for (i
= 0; i
< num_screens
; ++i
) {
9064 ws
= (struct workspace
*)&screens
[i
].ws
;
9066 if (strlen(s
) > 0) {
9067 free(ws
[ws_id
].name
);
9068 if ((ws
[ws_id
].name
= strdup(s
)) == NULL
)
9069 err(1, "setconfvalue: workspace_name.");
9071 ewmh_update_desktop_names();
9072 ewmh_get_desktop_names();
9083 setconfmodkey(const char *selector
, const char *value
, int flags
)
9085 /* suppress unused warnings since vars are needed */
9089 if (strncasecmp(value
, "Mod1", strlen("Mod1")) == 0)
9090 update_modkey(XCB_MOD_MASK_1
);
9091 else if (strncasecmp(value
, "Mod2", strlen("Mod2")) == 0)
9092 update_modkey(XCB_MOD_MASK_2
);
9093 else if (strncasecmp(value
, "Mod3", strlen("Mod3")) == 0)
9094 update_modkey(XCB_MOD_MASK_3
);
9095 else if (strncasecmp(value
, "Mod4", strlen("Mod4")) == 0)
9096 update_modkey(XCB_MOD_MASK_4
);
9097 else if (strncasecmp(value
, "Mod5", strlen("Mod5")) == 0)
9098 update_modkey(XCB_MOD_MASK_5
);
9105 setconfcolor(const char *selector
, const char *value
, int flags
)
9107 int first
, last
, i
= 0, num_screens
;
9109 num_screens
= get_screen_count();
9111 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
9112 if (selector
== NULL
|| strlen(selector
) == 0 ||
9113 (last
= atoi(selector
) - 1) < 0) {
9115 last
= num_screens
- 1;
9120 if (last
>= num_screens
) {
9121 add_startup_exception("invalid screen index: %d out of bounds "
9122 "(maximum %d)", last
+ 1, num_screens
);
9126 for (i
= first
; i
<= last
; ++i
) {
9127 setscreencolor(value
, i
, flags
);
9130 * When setting focus/unfocus colors, we need to also
9131 * set maximize colors to match if they haven't been customized.
9133 if (flags
== SWM_S_COLOR_FOCUS
&&
9134 !screens
[i
].c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].manual
)
9135 setscreencolor(value
, i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
9136 else if (flags
== SWM_S_COLOR_UNFOCUS
&&
9137 !screens
[i
].c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].manual
)
9138 setscreencolor(value
, i
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
9140 screens
[i
].c
[flags
].manual
= 1;
9147 setconfregion(const char *selector
, const char *value
, int flags
)
9149 /* suppress unused warnings since vars are needed */
9153 custom_region(value
);
9158 setautorun(const char *selector
, const char *value
, int flags
)
9162 char *ap
, *sp
, *str
;
9168 /* suppress unused warnings since vars are needed */
9172 if (getenv("SWM_STARTED"))
9176 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9177 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
9179 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9180 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
9182 sp
= str
= expand_tilde((char *)&s
);
9185 * This is a little intricate
9187 * If the pid already exists we simply reuse it because it means it was
9188 * used before AND not claimed by manage_window. We get away with
9189 * altering it in the parent after INSERT because this can not be a race
9192 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
9195 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
9197 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
9198 err(1, "setautorun: realloc");
9199 a
.argv
[argc
- 1] = ap
;
9202 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
9203 err(1, "setautorun: realloc");
9204 a
.argv
[argc
] = NULL
;
9206 if ((pid
= fork()) == 0) {
9207 spawn(ws_id
, &a
, true);
9217 p
= calloc(1, sizeof *p
);
9220 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
9230 setlayout(const char *selector
, const char *value
, int flags
)
9232 struct workspace
*ws
;
9233 int ws_id
, i
, x
, mg
, ma
, si
, ar
;
9234 int st
= SWM_V_STACK
, num_screens
;
9238 /* suppress unused warnings since vars are needed */
9242 if (getenv("SWM_STARTED"))
9246 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
9247 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
9248 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9249 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9252 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9253 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
9255 if (strcasecmp(s
, "vertical") == 0)
9257 else if (strcasecmp(s
, "vertical_flip") == 0) {
9260 } else if (strcasecmp(s
, "horizontal") == 0)
9262 else if (strcasecmp(s
, "horizontal_flip") == 0) {
9265 } else if (strcasecmp(s
, "fullscreen") == 0)
9268 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9269 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9272 num_screens
= get_screen_count();
9273 for (i
= 0; i
< num_screens
; i
++) {
9274 ws
= (struct workspace
*)&screens
[i
].ws
;
9275 ws
[ws_id
].cur_layout
= &layouts
[st
];
9277 ws
[ws_id
].always_raise
= (ar
!= 0);
9278 if (st
== SWM_MAX_STACK
)
9282 for (x
= 0; x
< abs(mg
); x
++) {
9283 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9284 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
9285 SWM_ARG_ID_MASTERSHRINK
);
9288 for (x
= 0; x
< abs(ma
); x
++) {
9289 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9290 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
9291 SWM_ARG_ID_MASTERDEL
);
9294 for (x
= 0; x
< abs(si
); x
++) {
9295 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9296 si
>= 0 ? SWM_ARG_ID_STACKINC
:
9297 SWM_ARG_ID_STACKDEC
);
9301 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9302 SWM_ARG_ID_FLIPLAYOUT
);
9309 /* config options */
9310 struct config_option
{
9312 int (*func
)(const char*, const char*, int);
9315 struct config_option configopt
[] = {
9316 { "autorun", setautorun
, 0 },
9317 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
9318 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
9319 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
9320 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
9321 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
9322 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
9323 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
9324 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
9325 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
9326 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
9327 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
9328 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
9329 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
9330 { "bind", setconfbinding
, 0 },
9331 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
9332 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
9333 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
9334 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
9335 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
9336 { "color_focus_maximized", setconfcolor
, SWM_S_COLOR_FOCUS_MAXIMIZED
},
9337 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
9338 { "color_unfocus_maximized", setconfcolor
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
},
9339 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
9340 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
9341 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
9342 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
9343 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
9344 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
9345 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
9346 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
9347 { "iconic_enabled", setconfvalue
, SWM_S_ICONIC_ENABLED
},
9348 { "java_workaround", setconfvalue
, SWM_S_JAVA_WORKAROUND
},
9349 { "keyboard_mapping", setkeymapping
, 0 },
9350 { "layout", setlayout
, 0 },
9351 { "maximize_hide_bar", setconfvalue
, SWM_S_MAXIMIZE_HIDE_BAR
},
9352 { "modkey", setconfmodkey
, 0 },
9353 { "program", setconfspawn
, 0 },
9354 { "quirk", setconfquirk
, 0 },
9355 { "region", setconfregion
, 0 },
9356 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
9357 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
9358 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
9359 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
9360 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
9361 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
9362 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
9363 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
9364 { "title_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
}, /* For backwards compat. */
9365 { "title_name_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
}, /* For backwards compat. */
9366 { "urgent_collapse", setconfvalue
, SWM_S_URGENT_COLLAPSE
},
9367 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
9368 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
9369 { "warp_focus", setconfvalue
, SWM_S_WARP_FOCUS
},
9370 { "warp_pointer", setconfvalue
, SWM_S_WARP_POINTER
},
9371 { "window_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
},
9372 { "window_instance_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
},
9373 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
9374 { "workspace_clamp", setconfvalue
, SWM_S_WORKSPACE_CLAMP
},
9375 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
9376 { "name", setconfvalue
, SWM_S_WORKSPACE_NAME
},
9380 _add_startup_exception(const char *fmt
, va_list ap
)
9382 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
9383 warn("%s: asprintf", __func__
);
9387 add_startup_exception(const char *fmt
, ...)
9393 if (startup_exception
)
9396 /* force bar to be enabled due to exception */
9400 _add_startup_exception(fmt
, ap
);
9405 conf_load(const char *filename
, int keymapping
)
9408 char *line
= NULL
, *cp
, *ce
, *optsub
, *optval
= NULL
;
9409 size_t linelen
, lineno
= 0;
9410 int wordlen
, i
, optidx
, count
;
9411 struct config_option
*opt
= NULL
;
9413 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
9415 if (filename
== NULL
) {
9416 warnx("conf_load: no filename");
9420 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
9422 if ((config
= fopen(filename
, "r")) == NULL
) {
9423 warn("conf_load: fopen: %s", filename
);
9427 while (!feof(config
)) {
9431 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
9432 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
9434 err(1, "%s", filename
);
9439 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9440 if (cp
[0] == '\0') {
9444 /* get config option */
9445 wordlen
= strcspn(cp
, "=[ \t\n");
9447 add_startup_exception("%s: line %zd: no option found",
9452 for (i
= 0; i
< LENGTH(configopt
); i
++) {
9453 opt
= &configopt
[i
];
9454 if (strncasecmp(cp
, opt
->optname
, wordlen
) == 0 &&
9455 (int)strlen(opt
->optname
) == wordlen
) {
9461 add_startup_exception("%s: line %zd: unknown option "
9462 "%.*s", filename
, lineno
, wordlen
, cp
);
9465 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
9466 add_startup_exception("%s: line %zd: invalid option "
9467 "%.*s", filename
, lineno
, wordlen
, cp
);
9471 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9473 /* get [selector] if any */
9478 /* Get length of selector. */
9479 for (ce
= ++cp
; *ce
!= '\0'; ++ce
) {
9480 /* Find matching (not escaped) bracket. */
9481 if (*ce
== ']' && *(ce
- 1) != '\\') {
9488 add_startup_exception("%s: line %zd: syntax "
9489 "error: unterminated selector", filename
,
9494 /* ce points at ']'; terminate optsub. */
9499 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
9502 if (strlen(optval
) == 0) {
9503 add_startup_exception("%s: line %zd: must supply value "
9504 "to %s", filename
, lineno
,
9505 configopt
[optidx
].optname
);
9508 /* call function to deal with it all */
9509 if (configopt
[optidx
].func(optsub
, optval
,
9510 configopt
[optidx
].funcflags
) != 0) {
9511 add_startup_exception("%s: line %zd: invalid data for "
9512 "%s", filename
, lineno
, configopt
[optidx
].optname
);
9521 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
9527 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
9529 struct ws_win
*parent
, *w
;
9530 struct swm_region
*r
;
9531 struct workspace
*ws
;
9532 xcb_icccm_wm_hints_t wmh
;
9534 parent
= find_window(win
->transient
);
9536 parent
->focus_child
= win
;
9538 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
9539 " for %#x trans %#x\n", win
->id
, win
->transient
);
9541 r
= root_to_region(win
->s
->root
, SWM_CK_ALL
);
9543 /* parent doen't exist in our window list */
9544 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
9545 if (xcb_icccm_get_wm_hints_reply(conn
,
9546 xcb_icccm_get_wm_hints(conn
, w
->id
),
9548 warnx("can't get hints for %#x", w
->id
);
9552 if (win
->hints
.window_group
!= wmh
.window_group
)
9555 w
->focus_child
= win
;
9556 win
->transient
= w
->id
;
9558 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
9559 "transient to %#x\n", win
->transient
);
9566 window_get_pid(xcb_window_t win
)
9571 xcb_get_property_cookie_t pc
;
9572 xcb_get_property_reply_t
*pr
;
9574 apid
= get_atom_from_string("_NET_WM_PID");
9575 if (apid
== XCB_ATOM_NONE
)
9578 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
9579 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9582 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
9587 if (pr
->type
== apid
&& pr
->format
== 32)
9588 ret
= *((pid_t
*)xcb_get_property_value(pr
));
9594 apid
= get_atom_from_string("_SWM_PID");
9595 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
9597 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9600 if (pr
->type
!= apid
) {
9605 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
9612 get_swm_ws(xcb_window_t id
)
9618 xcb_get_property_reply_t
*gpr
;
9620 gpr
= xcb_get_property_reply(conn
,
9621 xcb_get_property(conn
, 0, id
, a_swm_ws
,
9622 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
9627 proplen
= xcb_get_property_value_length(gpr
);
9629 prop
= malloc(proplen
+ 1);
9632 xcb_get_property_value(gpr
),
9634 prop
[proplen
] = '\0';
9641 DNPRINTF(SWM_D_PROP
, "get_swm_ws: _SWM_WS: %s\n", prop
);
9642 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
9644 DNPRINTF(SWM_D_PROP
, "get_swm_ws: win #%s: %s",
9654 get_ws_idx(struct ws_win
*win
)
9656 xcb_get_property_reply_t
*gpr
;
9662 gpr
= xcb_get_property_reply(conn
,
9663 xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
9664 XCB_ATOM_CARDINAL
, 0, 1),
9667 if (gpr
->type
== XCB_ATOM_CARDINAL
&& gpr
->format
== 32)
9668 ws_idx
= *((int *)xcb_get_property_value(gpr
));
9672 if (ws_idx
== -1 && !(win
->quirks
& SWM_Q_IGNORESPAWNWS
))
9673 ws_idx
= get_swm_ws(win
->id
);
9675 if (ws_idx
> workspace_limit
- 1 || ws_idx
< -1)
9678 DNPRINTF(SWM_D_PROP
, "get_ws_idx: win %#x, ws_idx: %d\n", win
->id
,
9685 reparent_window(struct ws_win
*win
)
9687 xcb_void_cookie_t c
;
9688 xcb_generic_error_t
*error
;
9691 win
->frame
= xcb_generate_id(conn
);
9693 DNPRINTF(SWM_D_MISC
, "reparent_window: win %#x, frame: %#x\n",
9694 win
->id
, win
->frame
);
9697 XCB_EVENT_MASK_ENTER_WINDOW
|
9698 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
9699 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
9700 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
|
9701 XCB_EVENT_MASK_EXPOSURE
;
9703 wa
[0] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
9706 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
->frame
, win
->s
->root
,
9707 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), 0,
9708 XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
9709 XCB_CW_EVENT_MASK
, wa
);
9711 win
->state
= SWM_WIN_STATE_REPARENTING
;
9712 c
= xcb_reparent_window_checked(conn
, win
->id
, win
->frame
, 0, 0);
9713 if ((error
= xcb_request_check(conn
, c
))) {
9714 DNPRINTF(SWM_D_MISC
, "reparent_window: error:\n");
9719 xcb_destroy_window(conn
, win
->frame
);
9720 win
->frame
= XCB_WINDOW_NONE
;
9722 xcb_change_save_set(conn
, XCB_SET_MODE_INSERT
, win
->id
);
9724 DNPRINTF(SWM_D_MISC
, "reparent_window: done.\n");
9728 unparent_window(struct ws_win
*win
)
9730 xcb_change_save_set(conn
, XCB_SET_MODE_DELETE
, win
->id
);
9731 xcb_reparent_window(conn
, win
->id
, win
->s
->root
, X(win
), Y(win
));
9732 xcb_destroy_window(conn
, win
->frame
);
9733 win
->frame
= XCB_WINDOW_NONE
;
9734 win
->state
= SWM_WIN_STATE_UNPARENTING
;
9738 manage_window(xcb_window_t id
, int spawn_pos
, bool mapping
)
9740 struct ws_win
*win
= NULL
, *ww
;
9741 struct swm_region
*r
;
9744 xcb_get_geometry_reply_t
*gr
;
9745 xcb_get_window_attributes_reply_t
*war
= NULL
;
9746 xcb_window_t trans
= XCB_WINDOW_NONE
;
9747 uint32_t i
, wa
[1], new_flags
;
9748 int ws_idx
, force_ws
= -1;
9749 char *class, *instance
, *name
;
9752 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region bar; "
9757 if (find_region(id
)) {
9758 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region window; "
9763 if ((win
= find_window(id
)) != NULL
) {
9764 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x already "
9765 "managed; skipping.)\n", id
);
9766 return (win
); /* Already managed. */
9769 /* See if window is on the unmanaged list. */
9770 if ((win
= find_unmanaged_window(id
)) != NULL
) {
9771 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x found on "
9772 "unmanaged list.\n", id
);
9773 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
9776 set_child_transient(win
, &trans
);
9780 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is new.\n", id
);
9783 war
= xcb_get_window_attributes_reply(conn
,
9784 xcb_get_window_attributes(conn
, id
), NULL
);
9786 DNPRINTF(SWM_D_EVENT
, "manage_window: window lost.\n");
9790 if (war
->override_redirect
) {
9791 DNPRINTF(SWM_D_EVENT
, "manage_window: override_redirect; "
9796 if (!mapping
&& war
->map_state
== XCB_MAP_STATE_UNMAPPED
&&
9797 get_win_state(id
) == XCB_ICCCM_WM_STATE_WITHDRAWN
) {
9798 DNPRINTF(SWM_D_EVENT
, "manage_window: window withdrawn; "
9803 /* Try to get initial window geometry. */
9804 gr
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, id
), NULL
);
9806 DNPRINTF(SWM_D_MISC
, "manage_window: get geometry failed.\n");
9810 /* Create and initialize ws_win object. */
9811 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
9812 err(1, "manage_window: calloc: failed to allocate memory for "
9817 /* Figureout which region the window belongs to. */
9818 r
= root_to_region(gr
->root
, SWM_CK_ALL
);
9820 /* Ignore window border if there is one. */
9821 WIDTH(win
) = gr
->width
;
9822 HEIGHT(win
) = gr
->height
;
9823 X(win
) = gr
->x
+ gr
->border_width
;
9824 Y(win
) = gr
->y
+ gr
->border_width
;
9825 win
->bordered
= false;
9826 win
->mapped
= (war
->map_state
!= XCB_MAP_STATE_UNMAPPED
);
9827 win
->s
= r
->s
; /* this never changes */
9831 /* Select which X events to monitor and set border pixel color. */
9832 wa
[0] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
9833 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
9835 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_EVENT_MASK
, wa
);
9837 /* Get WM_SIZE_HINTS. */
9838 xcb_icccm_get_wm_normal_hints_reply(conn
,
9839 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
9843 xcb_icccm_get_wm_hints_reply(conn
,
9844 xcb_icccm_get_wm_hints(conn
, win
->id
),
9847 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
9848 xcb_icccm_get_wm_transient_for_reply(conn
,
9849 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
9852 win
->transient
= trans
;
9853 set_child_transient(win
, &win
->transient
);
9856 /* Get WM_PROTOCOLS. */
9857 get_wm_protocols(win
);
9860 /* Must be after getting WM_HINTS and WM_PROTOCOLS. */
9861 DNPRINTF(SWM_D_FOCUS
, "manage_window: input_model: %s\n",
9862 get_win_input_model(win
));
9865 /* Set initial quirks based on EWMH. */
9866 ewmh_autoquirk(win
);
9868 /* Determine initial quirks. */
9869 xcb_icccm_get_wm_class_reply(conn
,
9870 xcb_icccm_get_wm_class(conn
, win
->id
),
9873 class = win
->ch
.class_name
? win
->ch
.class_name
: "";
9874 instance
= win
->ch
.instance_name
? win
->ch
.instance_name
: "";
9875 name
= get_win_name(win
->id
);
9877 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, instance: %s, "
9878 "name: %s\n", class, instance
, name
);
9880 /* java is retarded so treat it special */
9881 if (strstr(instance
, "sun-awt")) {
9882 DNPRINTF(SWM_D_CLASS
, "manage_window: java window detected.\n");
9886 TAILQ_FOREACH(qp
, &quirks
, entry
) {
9887 if (regexec(&qp
->regex_class
, class, 0, NULL
, 0) == 0 &&
9888 regexec(&qp
->regex_instance
, instance
, 0, NULL
, 0) == 0 &&
9889 regexec(&qp
->regex_name
, name
, 0, NULL
, 0) == 0) {
9890 DNPRINTF(SWM_D_CLASS
, "manage_window: matched "
9891 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp
->class,
9892 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
9893 win
->quirks
= qp
->quirk
;
9894 if (qp
->ws
>= 0 && qp
->ws
< workspace_limit
)
9901 /* Reset font sizes (the bruteforce way; no default keybinding). */
9902 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
9903 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9904 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
9905 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9906 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
9909 /* Figure out which workspace the window belongs to. */
9910 if (!(win
->quirks
& SWM_Q_IGNOREPID
) &&
9911 (p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
9912 win
->ws
= &r
->s
->ws
[p
->ws
];
9913 TAILQ_REMOVE(&pidlist
, p
, entry
);
9916 } else if ((ws_idx
= get_ws_idx(win
)) != -1 &&
9918 /* _SWM_WS is set; use that. */
9919 win
->ws
= &r
->s
->ws
[ws_idx
];
9920 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
9921 /* Launch transients in the same ws as parent. */
9928 win
->ws
= &r
->s
->ws
[force_ws
];
9930 /* Set the _NET_WM_DESKTOP atom. */
9931 DNPRINTF(SWM_D_PROP
, "manage_window: set _NET_WM_DESKTOP: %d\n",
9933 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
9934 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &win
->ws
->idx
);
9936 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
9937 xcb_delete_property(conn
, win
->id
, a_swm_ws
);
9939 /* WS must already be set for this to work. */
9940 store_float_geom(win
);
9942 /* Make sure window is positioned inside its region, if its active. */
9944 region_containment(win
, r
, SWM_CW_ALLSIDES
|
9945 SWM_CW_HARDBOUNDARY
);
9950 /* Figure out where to insert the window in the workspace list. */
9951 if (trans
&& (ww
= find_window(trans
)))
9952 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
9953 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_ABOVE
)
9954 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
9956 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_BELOW
)
9957 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
9958 else switch (spawn_pos
) {
9961 case SWM_STACK_ABOVE
:
9962 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
9964 case SWM_STACK_BOTTOM
:
9965 case SWM_STACK_BELOW
:
9966 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
9969 ewmh_update_client_list();
9971 TAILQ_INSERT_HEAD(&win
->ws
->stack
, win
, stack_entry
);
9974 /* Get/apply initial _NET_WM_STATE */
9975 ewmh_get_wm_state(win
);
9978 new_flags
= win
->ewmh_flags
;
9980 if (win
->quirks
& SWM_Q_FLOAT
)
9981 new_flags
|= EWMH_F_ABOVE
;
9983 if (win
->quirks
& SWM_Q_ANYWHERE
)
9984 new_flags
|= SWM_F_MANUAL
;
9986 ewmh_apply_flags(win
, new_flags
);
9987 ewmh_update_wm_state(win
);
9989 /* Set initial _NET_WM_ALLOWED_ACTIONS */
9990 ewmh_update_actions(win
);
9992 reparent_window(win
);
9994 DNPRINTF(SWM_D_MISC
, "manage_window: done. win %#x, (x,y) w x h: "
9995 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win
->id
,
9996 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
9997 YESNO(ICONIC(win
)), win
->transient
);
10004 free_window(struct ws_win
*win
)
10006 DNPRINTF(SWM_D_MISC
, "free_window: win %#x\n", WINID(win
));
10011 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
10014 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
10017 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
10021 unmanage_window(struct ws_win
*win
)
10023 DNPRINTF(SWM_D_MISC
, "unmanage_window: win %#x\n", WINID(win
));
10029 unparent_window(win
);
10031 TAILQ_REMOVE(&win
->ws
->stack
, win
, stack_entry
);
10032 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
10033 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
10035 ewmh_update_client_list();
10039 expose(xcb_expose_event_t
*e
)
10044 struct workspace
*ws
;
10047 DNPRINTF(SWM_D_EVENT
, "expose: win %#x, count: %d\n", e
->window
,
10053 if ((b
= find_bar(e
->window
))) {
10056 } else if ((w
= find_window(e
->window
)) && w
->frame
== e
->window
) {
10060 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
10066 DNPRINTF(SWM_D_EVENT
, "expose: done\n");
10070 focusin(xcb_focus_in_event_t
*e
)
10072 struct ws_win
*win
;
10074 DNPRINTF(SWM_D_EVENT
, "focusin: win %#x, mode: %s(%u), "
10075 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10076 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10077 if ((win
= find_window(e
->event
)) && win
!= win
->ws
->focus
&&
10078 win
!= win
->ws
->focus_pending
&&
10079 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10080 win
->ws
->focus_prev
= win
->ws
->focus
;
10081 win
->ws
->focus
= win
;
10082 win
->ws
->focus_pending
= NULL
;
10084 if (win
->ws
->focus_prev
)
10085 draw_frame(win
->ws
->focus_prev
);
10093 focusout(xcb_focus_out_event_t
*e
)
10095 DNPRINTF(SWM_D_EVENT
, "focusout: win %#x, mode: %s(%u), "
10096 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10097 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10102 keypress(xcb_key_press_event_t
*e
)
10105 struct binding
*bp
;
10106 xcb_keysym_t keysym
;
10107 bool replay
= true;
10109 last_event_time
= e
->time
;
10111 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
10113 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
10114 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10115 "state: %u, cleaned: %u, same_screen: %s\n", keysym
, e
->event
,
10116 e
->event_x
, e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
,
10117 e
->root_y
, e
->child
, e
->state
, CLEANMASK(e
->state
),
10118 YESNO(e
->same_screen
));
10120 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10122 /* Look for catch-all. */
10123 if ((bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
)) == NULL
)
10127 replay
= bp
->flags
& BINDING_F_REPLAY
;
10129 if ((ap
= &actions
[bp
->action
]) == NULL
)
10132 if (bp
->action
== FN_SPAWN_CUSTOM
)
10133 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10136 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10138 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10142 DNPRINTF(SWM_D_EVENT
, "keypress: replaying.\n");
10143 /* Pass keypress to event window and unfreeze keyboard queue. */
10144 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10146 /* Release freeze. */
10147 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10151 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
10155 keyrelease(xcb_key_release_event_t
*e
)
10158 struct binding
*bp
;
10159 xcb_keysym_t keysym
;
10161 last_event_time
= e
->time
;
10163 keysym
= xcb_key_release_lookup_keysym(syms
, e
, 0);
10165 DNPRINTF(SWM_D_EVENT
, "keyrelease: keysym: %u, win (x,y): %#x (%d,%d), "
10166 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10167 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
10168 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
10169 e
->child
, e
->state
, YESNO(e
->same_screen
));
10171 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10173 /* Look for catch-all. */
10174 bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
);
10176 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10177 bp
->flags
& BINDING_F_REPLAY
) {
10178 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10179 DNPRINTF(SWM_D_EVENT
, "keyrelease: replaying.\n");
10181 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10182 DNPRINTF(SWM_D_EVENT
, "keyrelease: unfreezing.\n");
10187 DNPRINTF(SWM_D_EVENT
, "keyrelease: done.\n");
10191 buttonpress(xcb_button_press_event_t
*e
)
10193 struct ws_win
*win
= NULL
, *newf
;
10194 struct swm_region
*r
, *old_r
;
10196 struct binding
*bp
;
10197 bool replay
= true;
10199 last_event_time
= e
->time
;
10201 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): %#x (%d,%d), "
10202 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10203 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10204 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10205 e
->state
, YESNO(e
->same_screen
));
10207 if (e
->event
== e
->root
) {
10209 win
= find_window(e
->child
);
10211 /* Focus on empty region */
10212 /* If no windows on region if its empty. */
10213 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10214 if (r
&& TAILQ_EMPTY(&r
->ws
->winlist
)) {
10215 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
10216 if (old_r
&& old_r
!= r
)
10217 unfocus_win(old_r
->ws
->focus
);
10219 DNPRINTF(SWM_D_FOCUS
, "buttonpress: "
10220 "set_input_focus: %#x, revert-to: parent, "
10221 "time: %#x\n", e
->root
, e
->time
);
10222 xcb_set_input_focus(conn
,
10223 XCB_INPUT_FOCUS_POINTER_ROOT
,
10226 /* Clear bar since empty. */
10229 /* No need to replay event. */
10234 win
= find_window(e
->event
);
10238 newf
= get_focus_magic(win
);
10239 if (win
->ws
->focus
== newf
&& newf
!= win
) {
10240 if (win
->focus_child
== win
)
10241 win
->focus_child
= NULL
;
10247 /* Handle any bound action. */
10248 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10250 /* Look for catch-all. */
10251 if ((bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
)) == NULL
)
10256 replay
= bp
->flags
& BINDING_F_REPLAY
;
10258 if ((ap
= &actions
[bp
->action
]) == NULL
)
10261 if (bp
->action
== FN_SPAWN_CUSTOM
)
10262 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10265 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10267 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10271 DNPRINTF(SWM_D_EVENT
, "buttonpress: replaying.\n");
10272 /* Replay event to event window */
10273 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10275 /* Unfreeze grab events. */
10276 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10283 buttonrelease(xcb_button_release_event_t
*e
)
10286 struct binding
*bp
;
10288 last_event_time
= e
->time
;
10290 DNPRINTF(SWM_D_EVENT
, "buttonrelease: win (x,y): %#x (%d,%d), "
10291 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10292 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10293 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10294 e
->state
, YESNO(e
->same_screen
));
10296 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10298 /* Look for catch-all. */
10299 bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
);
10301 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10302 bp
->flags
& BINDING_F_REPLAY
) {
10303 DNPRINTF(SWM_D_EVENT
, "buttonrelease: replaying.\n");
10304 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10306 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10314 get_win_input_model(struct ws_win
*win
)
10318 * Input Model Input Field WM_TAKE_FOCUS
10319 * No Input False Absent
10320 * Passive True Absent
10321 * Locally Active True Present
10322 * Globally Active False Present
10325 if (ACCEPTS_FOCUS(win
))
10326 inputmodel
= (win
->take_focus
) ? "Locally Active" : "Passive";
10328 inputmodel
= (win
->take_focus
) ? "Globally Active" : "No Input";
10334 print_win_geom(xcb_window_t w
)
10336 xcb_get_geometry_reply_t
*wa
;
10338 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
10340 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: %#x\n",
10345 DNPRINTF(SWM_D_MISC
, "print_win_geom: win %#x, root: %#x, "
10346 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
10347 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
10356 get_stack_mode_name(uint8_t mode
)
10361 case XCB_STACK_MODE_ABOVE
:
10364 case XCB_STACK_MODE_BELOW
:
10367 case XCB_STACK_MODE_TOP_IF
:
10370 case XCB_STACK_MODE_BOTTOM_IF
:
10373 case XCB_STACK_MODE_OPPOSITE
:
10385 configurerequest(xcb_configure_request_event_t
*e
)
10387 struct ws_win
*win
;
10388 struct swm_region
*r
= NULL
;
10390 uint32_t wc
[7] = {0};
10394 if ((win
= find_window(e
->window
)) == NULL
)
10395 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10399 if (swm_debug
& SWM_D_EVENT
) {
10400 print_win_geom(e
->window
);
10402 DNPRINTF(SWM_D_EVENT
, "configurerequest: win %#x, "
10403 "parent: %#x, new: %s, value_mask: %u { ", e
->window
,
10404 e
->parent
, YESNO(new), e
->value_mask
);
10405 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
10406 DPRINTF("X: %d ", e
->x
);
10407 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
10408 DPRINTF("Y: %d ", e
->y
);
10409 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10410 DPRINTF("W: %u ", e
->width
);
10411 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10412 DPRINTF("H: %u ", e
->height
);
10413 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
10414 DPRINTF("Border: %u ", e
->border_width
);
10415 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
10416 DPRINTF("Sibling: %#x ", e
->sibling
);
10417 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
10418 DPRINTF("StackMode: %s(%u) ",
10419 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
10425 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10426 mask
|= XCB_CONFIG_WINDOW_X
;
10429 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10430 mask
|= XCB_CONFIG_WINDOW_Y
;
10433 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
10434 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
10435 wc
[i
++] = e
->width
;
10437 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
10438 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
10439 wc
[i
++] = e
->height
;
10441 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
10442 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
10443 wc
[i
++] = e
->border_width
;
10445 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
10446 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
10447 wc
[i
++] = e
->sibling
;
10449 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
10450 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
10451 wc
[i
++] = e
->stack_mode
;
10455 xcb_configure_window(conn
, e
->window
, mask
, wc
);
10458 } else if ((!MANUAL(win
) || win
->quirks
& SWM_Q_ANYWHERE
) &&
10459 !FULLSCREEN(win
) && !MAXIMIZED(win
)) {
10462 else if (win
->ws
->old_r
)
10463 r
= win
->ws
->old_r
;
10465 /* windows are centered unless ANYWHERE quirk is set. */
10466 if (win
->quirks
& SWM_Q_ANYWHERE
) {
10467 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10468 win
->g_float
.x
= e
->x
;
10470 win
->g_float
.x
-= X(r
);
10473 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10474 win
->g_float
.y
= e
->y
;
10476 win
->g_float
.y
-= Y(r
);
10480 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10481 win
->g_float
.w
= e
->width
;
10483 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10484 win
->g_float
.h
= e
->height
;
10486 win
->g_floatvalid
= true;
10488 if (!MAXIMIZED(win
) && !FULLSCREEN(win
) &&
10489 (TRANS(win
) || (ABOVE(win
) &&
10490 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]))) {
10491 WIDTH(win
) = win
->g_float
.w
;
10492 HEIGHT(win
) = win
->g_float
.h
;
10495 update_floater(win
);
10498 config_win(win
, e
);
10502 config_win(win
, e
);
10506 config_win(win
, e
);
10510 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
10514 configurenotify(xcb_configure_notify_event_t
*e
)
10516 struct ws_win
*win
;
10518 DNPRINTF(SWM_D_EVENT
, "configurenotify: win %#x, event win: %#x, "
10519 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
10520 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
10521 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
10522 YESNO(e
->override_redirect
));
10524 win
= find_window(e
->window
);
10527 if (font_adjusted
&& win
->ws
->r
) {
10535 destroynotify(xcb_destroy_notify_event_t
*e
)
10537 struct ws_win
*win
;
10538 struct workspace
*ws
;
10540 DNPRINTF(SWM_D_EVENT
, "destroynotify: win %#x\n", e
->window
);
10542 if ((win
= find_window(e
->window
)) == NULL
) {
10543 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10545 /* Window is on unmanaged list. */
10546 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10553 if (win
->frame
== e
->window
) {
10554 DNPRINTF(SWM_D_EVENT
, "destroynotify: frame for win %#x\n",
10556 win
->frame
= XCB_WINDOW_NONE
;
10560 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10561 /* If we were focused, make sure we focus on something else. */
10562 if (win
== ws
->focus
) {
10563 ws
->focus_pending
= get_focus_prev(win
);
10564 if (ws
->focus_pending
== win
)
10565 ws
->focus_pending
= NULL
;
10569 unmanage_window(win
);
10570 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10574 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
10575 if (ws
->focus_pending
) {
10576 focus_win(ws
->focus_pending
);
10577 ws
->focus_pending
= NULL
;
10578 } else if (ws
->focus
== NULL
) {
10579 DNPRINTF(SWM_D_FOCUS
, "destroynotify: set_input_focus: "
10580 "%#x, revert-to: parent, time: 0\n", ws
->r
->id
);
10581 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
10582 ws
->r
->id
, XCB_CURRENT_TIME
);
10589 DNPRINTF(SWM_D_EVENT
, "destroynotify: done.\n");
10594 get_notify_detail_label(uint8_t detail
)
10599 case XCB_NOTIFY_DETAIL_ANCESTOR
:
10600 label
= "Ancestor";
10602 case XCB_NOTIFY_DETAIL_VIRTUAL
:
10605 case XCB_NOTIFY_DETAIL_INFERIOR
:
10606 label
= "Inferior";
10608 case XCB_NOTIFY_DETAIL_NONLINEAR
:
10609 label
= "Nonlinear";
10611 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
10612 label
= "NonlinearVirtual";
10614 case XCB_NOTIFY_DETAIL_POINTER
:
10617 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
10618 label
= "PointerRoot";
10620 case XCB_NOTIFY_DETAIL_NONE
:
10631 get_notify_mode_label(uint8_t mode
)
10636 case XCB_NOTIFY_MODE_NORMAL
:
10639 case XCB_NOTIFY_MODE_GRAB
:
10642 case XCB_NOTIFY_MODE_UNGRAB
:
10645 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
10646 label
= "WhileGrabbed";
10656 get_state_mask_label(uint16_t state
)
10661 case XCB_KEY_BUT_MASK_SHIFT
:
10662 label
= "ShiftMask";
10664 case XCB_KEY_BUT_MASK_LOCK
:
10665 label
= "LockMask";
10667 case XCB_KEY_BUT_MASK_CONTROL
:
10668 label
= "ControlMask";
10670 case XCB_KEY_BUT_MASK_MOD_1
:
10671 label
= "Mod1Mask";
10673 case XCB_KEY_BUT_MASK_MOD_2
:
10674 label
= "Mod2Mask";
10676 case XCB_KEY_BUT_MASK_MOD_3
:
10677 label
= "Mod3Mask";
10679 case XCB_KEY_BUT_MASK_MOD_4
:
10680 label
= "Mod4Mask";
10682 case XCB_KEY_BUT_MASK_MOD_5
:
10683 label
= "Mod5Mask";
10685 case XCB_KEY_BUT_MASK_BUTTON_1
:
10686 label
= "Button1Mask";
10688 case XCB_KEY_BUT_MASK_BUTTON_2
:
10689 label
= "Button2Mask";
10691 case XCB_KEY_BUT_MASK_BUTTON_3
:
10692 label
= "Button3Mask";
10694 case XCB_KEY_BUT_MASK_BUTTON_4
:
10695 label
= "Button4Mask";
10697 case XCB_KEY_BUT_MASK_BUTTON_5
:
10698 label
= "Button5Mask";
10712 enternotify(xcb_enter_notify_event_t
*e
)
10714 struct ws_win
*win
;
10715 struct swm_region
*r
;
10717 last_event_time
= e
->time
;
10719 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): %#x "
10720 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10721 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10722 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10723 get_notify_mode_label(e
->mode
), e
->mode
,
10724 get_notify_detail_label(e
->detail
), e
->detail
,
10725 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10726 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10729 if (e
->event
== e
->root
&& e
->child
== XCB_WINDOW_NONE
&&
10730 e
->mode
== XCB_NOTIFY_MODE_GRAB
&&
10731 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10732 DNPRINTF(SWM_D_EVENT
, "enternotify: grab inferior; ignoring.\n");
10736 if (focus_mode
== SWM_FOCUS_MANUAL
&&
10737 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10738 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
10742 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10743 e
->mode
== XCB_NOTIFY_MODE_UNGRAB
&&
10744 e
->detail
!= XCB_NOTIFY_DETAIL_ANCESTOR
) {
10745 DNPRINTF(SWM_D_EVENT
, "enternotify: ungrab; ignoring.\n");
10749 if ((win
= find_window(e
->event
)) == NULL
) {
10750 if (e
->event
== e
->root
) {
10751 /* If no windows on pointer region, then focus root. */
10752 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10754 DNPRINTF(SWM_D_EVENT
, "enternotify: "
10755 "NULL region; ignoring.\n");
10761 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
10766 if (e
->mode
== XCB_NOTIFY_MODE_NORMAL
&&
10767 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10768 DNPRINTF(SWM_D_EVENT
, "enternotify: entering from "
10769 "inferior; ignoring\n");
10773 focus_win(get_focus_magic(win
));
10776 DNPRINTF(SWM_D_EVENT
, "enternotify: done\n");
10783 leavenotify(xcb_leave_notify_event_t
*e
)
10785 last_event_time
= e
->time
;
10787 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): %#x "
10788 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10789 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10790 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10791 get_notify_mode_label(e
->mode
), e
->mode
,
10792 get_notify_detail_label(e
->detail
), e
->detail
,
10793 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10794 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10800 mapnotify(xcb_map_notify_event_t
*e
)
10802 struct ws_win
*win
, *parent
= NULL
;
10803 struct workspace
*ws
;
10805 DNPRINTF(SWM_D_EVENT
, "mapnotify: win %#x\n", e
->window
);
10807 if ((win
= manage_window(e
->window
, spawn_position
, false)) == NULL
)
10811 if (win
->state
== SWM_WIN_STATE_REPARENTING
)
10814 if (ws
->r
== NULL
) {
10819 /* Need to know if win was mapped due to ws switch. */
10820 if (ws
->state
== SWM_WS_STATE_MAPPED
) {
10821 if (ws
->focus_pending
&& TRANS(ws
->focus_pending
))
10822 parent
= find_window(win
->transient
);
10824 /* If window's parent is maximized, don't clear it. */
10825 if ((parent
== NULL
) || !MAXIMIZED(parent
))
10826 if (clear_maximized(ws
) > 0)
10830 win
->mapped
= true;
10831 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
10833 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
10834 if (ws
->focus_pending
== win
) {
10836 ws
->focus_pending
= NULL
;
10837 center_pointer(ws
->r
);
10843 DNPRINTF(SWM_D_EVENT
, "mapnotify: done\n");
10849 mappingnotify(xcb_mapping_notify_event_t
*e
)
10851 if (e
->request
!= XCB_MAPPING_POINTER
) {
10852 xcb_refresh_keyboard_mapping(syms
, e
);
10860 maprequest(xcb_map_request_event_t
*e
)
10862 struct ws_win
*win
, *w
= NULL
;
10864 DNPRINTF(SWM_D_EVENT
, "maprequest: win %#x\n",
10867 win
= manage_window(e
->window
, spawn_position
, true);
10871 /* The new window should get focus; prepare. */
10872 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10873 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
) && ACCEPTS_FOCUS(win
)) {
10874 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
10875 /* See if other wins of same type are already mapped. */
10876 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
10877 if (w
== win
|| !w
->mapped
)
10880 if (w
->ch
.class_name
&&
10881 win
->ch
.class_name
&&
10882 strcmp(w
->ch
.class_name
,
10883 win
->ch
.class_name
) == 0 &&
10884 w
->ch
.instance_name
&&
10885 win
->ch
.instance_name
&&
10886 strcmp(w
->ch
.instance_name
,
10887 win
->ch
.instance_name
) == 0)
10893 win
->ws
->focus_pending
= get_focus_magic(win
);
10896 /* All windows need to be mapped if they are in the current workspace.*/
10899 /* Ignore EnterNotify to handle the mapnotify without interference. */
10900 if (focus_mode
== SWM_FOCUS_DEFAULT
)
10901 event_drain(XCB_ENTER_NOTIFY
);
10903 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
10907 motionnotify(xcb_motion_notify_event_t
*e
)
10909 struct swm_region
*r
= NULL
;
10910 int i
, num_screens
;
10912 last_event_time
= e
->time
;
10914 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): %#x "
10915 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10916 "child: %#x, same_screen_focus: %s, state: %d\n",
10917 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10918 get_notify_detail_label(e
->detail
), e
->detail
,
10919 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10920 YESNO(e
->same_screen
), e
->state
);
10922 if (focus_mode
== SWM_FOCUS_MANUAL
)
10925 num_screens
= get_screen_count();
10926 for (i
= 0; i
< num_screens
; i
++)
10927 if (screens
[i
].root
== e
->root
)
10930 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10931 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
10932 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
10940 get_atom_name(xcb_atom_t atom
)
10943 #ifdef SWM_DEBUG_ATOM_NAMES
10945 * This should be disabled during most debugging since
10946 * xcb_get_* causes an xcb_flush.
10949 xcb_get_atom_name_reply_t
*r
;
10951 r
= xcb_get_atom_name_reply(conn
,
10952 xcb_get_atom_name(conn
, atom
),
10955 len
= xcb_get_atom_name_name_length(r
);
10957 name
= malloc(len
+ 1);
10959 memcpy(name
, xcb_get_atom_name_name(r
), len
);
10973 propertynotify(xcb_property_notify_event_t
*e
)
10975 struct ws_win
*win
;
10976 struct workspace
*ws
;
10980 name
= get_atom_name(e
->atom
);
10981 DNPRINTF(SWM_D_EVENT
, "propertynotify: win %#x, atom: %s(%u), "
10982 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
10986 last_event_time
= e
->time
;
10988 win
= find_window(e
->window
);
10994 if (e
->atom
== a_state
) {
10995 /* State just changed, make sure it gets focused if mapped. */
10996 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
10997 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
10999 win
->state
== SWM_WIN_STATE_REPARENTED
&&
11000 ws
->focus_pending
== win
) {
11001 focus_win(ws
->focus_pending
);
11002 ws
->focus_pending
= NULL
;
11006 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
11007 e
->atom
== XCB_ATOM_WM_NAME
) {
11009 bar_draw(ws
->r
->bar
);
11010 } else if (e
->atom
== a_prot
) {
11011 get_wm_protocols(win
);
11012 } else if (e
->atom
== XCB_ATOM_WM_NORMAL_HINTS
) {
11013 xcb_icccm_get_wm_normal_hints_reply(conn
,
11014 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
11022 reparentnotify(xcb_reparent_notify_event_t
*e
)
11024 struct ws_win
*win
;
11026 DNPRINTF(SWM_D_EVENT
, "reparentnotify: event: %#x, win %#x, "
11027 "parent: %#x, (x,y): (%u,%u), override_redirect: %u\n",
11028 e
->event
, e
->window
, e
->parent
, e
->x
, e
->y
, e
->override_redirect
);
11030 win
= find_window(e
->window
);
11032 if (win
->state
== SWM_WIN_STATE_REPARENTING
) {
11033 win
->state
= SWM_WIN_STATE_REPARENTED
;
11040 update_window(win
);
11041 update_win_stacking(win
);
11042 } else if (win
->state
== SWM_WIN_STATE_UNPARENTING
) {
11043 win
->state
= SWM_WIN_STATE_UNPARENTED
;
11049 unmapnotify(xcb_unmap_notify_event_t
*e
)
11051 struct ws_win
*win
;
11052 struct workspace
*ws
;
11054 DNPRINTF(SWM_D_EVENT
, "unmapnotify: win %#x\n", e
->window
);
11056 /* If we aren't managing the window, then ignore. */
11057 win
= find_window(e
->window
);
11058 if (win
== NULL
|| win
->id
!= e
->window
) {
11059 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore unmanaged.\n");
11063 /* Do nothing if already withdrawn. */
11064 if (!win
->mapped
&& !ICONIC(win
)) {
11065 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore withdrawn.\n");
11070 win
->mapped
= false;
11072 /* Ignore if reparenting-related. */
11073 if (win
->state
!= SWM_WIN_STATE_REPARENTED
) {
11074 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore not reparented.\n");
11078 /* If win was focused, make sure to focus on something else. */
11079 if (win
== ws
->focus
) {
11080 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
11081 ws
->focus_pending
= get_focus_prev(win
);
11082 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
11083 "focus_pending: %#x\n",
11084 WINID(ws
->focus_pending
));
11092 DNPRINTF(SWM_D_EVENT
, "unmapnotify: iconify.\n");
11093 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
11096 DNPRINTF(SWM_D_EVENT
, "unmapnotify: withdraw.\n");
11097 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
11098 unmanage_window(win
);
11103 /* Update focus if ws is active. */
11104 if (WS_FOCUSED(ws
)) {
11105 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
11106 focus_win(get_pointer_win(ws
->r
->s
->root
));
11107 } else if (ws
->focus_pending
) {
11108 focus_win(ws
->focus_pending
);
11109 ws
->focus_pending
= NULL
;
11110 } else if (ws
->focus
== NULL
) {
11111 DNPRINTF(SWM_D_FOCUS
, "unmapnotify: set_input_focus: "
11112 "%#x, revert-to: parent, time: 0\n",
11114 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11115 ws
->r
->id
, XCB_CURRENT_TIME
);
11119 center_pointer(ws
->r
);
11121 DNPRINTF(SWM_D_EVENT
, "unmapnotify: done.\n");
11126 get_source_type_label(uint32_t type
)
11131 case EWMH_SOURCE_TYPE_NONE
:
11134 case EWMH_SOURCE_TYPE_NORMAL
:
11137 case EWMH_SOURCE_TYPE_OTHER
:
11149 clientmessage(xcb_client_message_event_t
*e
)
11151 struct ws_win
*win
;
11152 struct swm_region
*r
= NULL
;
11155 int num_screens
, i
;
11156 xcb_map_request_event_t mre
;
11160 name
= get_atom_name(e
->type
);
11161 DNPRINTF(SWM_D_EVENT
, "clientmessage: win %#x, atom: %s(%u)\n",
11162 e
->window
, name
, e
->type
);
11166 if (e
->type
== ewmh
[_NET_CURRENT_DESKTOP
].atom
) {
11167 num_screens
= get_screen_count();
11168 for (i
= 0; i
< num_screens
; i
++)
11169 if (screens
[i
].root
== e
->window
) {
11170 r
= screens
[i
].r_focus
;
11174 if (r
&& e
->data
.data32
[0] < (uint32_t)workspace_limit
) {
11175 a
.id
= e
->data
.data32
[0];
11176 switchws(NULL
, r
, &a
);
11181 } else if (e
->type
== ewmh
[_NET_REQUEST_FRAME_EXTENTS
].atom
) {
11182 DNPRINTF(SWM_D_EVENT
,
11183 "clientmessage: set _NET_FRAME_EXTENTS on window.\n");
11184 vals
[0] = vals
[1] = vals
[2] = vals
[3] = border_width
;
11185 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, e
->window
,
11186 a_net_frame_extents
, XCB_ATOM_CARDINAL
, 32, 4, vals
);
11191 win
= find_window(e
->window
);
11193 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11194 /* Manage the window with maprequest. */
11195 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
11196 "unmanaged window.\n");
11197 mre
.window
= e
->window
;
11203 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11204 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW, "
11205 "source_type: %s(%d)\n",
11206 get_source_type_label(e
->data
.data32
[0]),
11207 e
->data
.data32
[0]);
11210 * Allow focus changes that are a result of direct user
11211 * action and from applications that use the old EWMH spec.
11213 if (e
->data
.data32
[0] != EWMH_SOURCE_TYPE_NORMAL
||
11214 win
->quirks
& SWM_Q_OBEYAPPFOCUSREQ
) {
11215 if (WS_FOCUSED(win
->ws
))
11218 win
->ws
->focus_pending
= win
;
11220 } else if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
11221 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
11222 if (win
->can_delete
)
11223 client_msg(win
, a_delete
, 0);
11225 xcb_kill_client(conn
, win
->id
);
11226 } else if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
11227 DNPRINTF(SWM_D_EVENT
,
11228 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
11230 if (e
->data
.data32
[0] & (1<<8)) /* x */
11231 X(win
) = e
->data
.data32
[1];
11232 if (e
->data
.data32
[0] & (1<<9)) /* y */
11233 Y(win
) = e
->data
.data32
[2];
11234 if (e
->data
.data32
[0] & (1<<10)) /* width */
11235 WIDTH(win
) = e
->data
.data32
[3];
11236 if (e
->data
.data32
[0] & (1<<11)) /* height */
11237 HEIGHT(win
) = e
->data
.data32
[4];
11239 update_window(win
);
11241 /* Notify no change was made. */
11242 config_win(win
, NULL
);
11243 /* TODO: Change stack sizes */
11245 } else if (e
->type
== ewmh
[_NET_RESTACK_WINDOW
].atom
) {
11246 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_RESTACK_WINDOW\n");
11247 vals
[0] = e
->data
.data32
[1]; /* Sibling window. */
11248 vals
[1] = e
->data
.data32
[2]; /* Stack mode detail. */
11250 if (win
->frame
!= XCB_WINDOW_NONE
)
11251 xcb_configure_window(conn
, win
->frame
,
11252 XCB_CONFIG_WINDOW_SIBLING
|
11253 XCB_CONFIG_WINDOW_STACK_MODE
, vals
);
11254 } else if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
11255 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
11256 ewmh_change_wm_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
11257 if (e
->data
.data32
[2])
11258 ewmh_change_wm_state(win
, e
->data
.data32
[2],
11259 e
->data
.data32
[0]);
11261 ewmh_update_wm_state(win
);
11263 } else if (e
->type
== ewmh
[_NET_WM_DESKTOP
].atom
) {
11264 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_DESKTOP\n");
11267 win_to_ws(win
, e
->data
.data32
[0], true);
11269 /* Stack source and destination ws, if mapped. */
11270 if (r
!= win
->ws
->r
) {
11276 load_float_geom(win
);
11289 int errcode
= xcb_connection_has_error(conn
);
11290 #ifdef XCB_CONN_ERROR
11293 case XCB_CONN_ERROR
:
11294 s
= "Socket error, pipe error or other stream error.";
11296 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
11297 s
= "Extension not supported.";
11299 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
11300 s
= "Insufficient memory.";
11302 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
11303 s
= "Request length was exceeded.";
11305 case XCB_CONN_CLOSED_PARSE_ERR
:
11306 s
= "Error parsing display string.";
11309 s
= "Unknown error.";
11312 errx(errcode
, "X CONNECTION ERROR: %s", s
);
11315 errx(errcode
, "X CONNECTION ERROR");
11322 int num_screens
, i
;
11323 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
11324 XCB_EVENT_MASK_ENTER_WINDOW
|
11325 XCB_EVENT_MASK_OWNER_GRAB_BUTTON
|
11326 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
11327 XCB_EVENT_MASK_ENTER_WINDOW
|
11328 XCB_EVENT_MASK_LEAVE_WINDOW
|
11329 XCB_EVENT_MASK_BUTTON_PRESS
|
11330 XCB_EVENT_MASK_BUTTON_RELEASE
|
11331 XCB_EVENT_MASK_KEY_PRESS
|
11332 XCB_EVENT_MASK_KEY_RELEASE
|
11333 XCB_EVENT_MASK_PROPERTY_CHANGE
;
11335 xcb_void_cookie_t wac
;
11336 xcb_generic_error_t
*error
;
11338 /* this causes an error if some other window manager is running */
11339 num_screens
= get_screen_count();
11340 for (i
= 0; i
< num_screens
; i
++) {
11341 if ((sc
= get_screen(i
)) == NULL
)
11342 errx(1, "ERROR: can't get screen %d.", i
);
11343 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: %#x\n",
11345 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
11346 XCB_CW_EVENT_MASK
, &val
);
11347 if ((error
= xcb_request_check(conn
, wac
))) {
11348 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
11349 error
->error_code
);
11359 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
11361 struct swm_region
*r
= NULL
, *n
;
11362 struct workspace
*ws
= NULL
;
11366 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
11367 s
->idx
, w
, h
, x
, y
);
11369 /* remove any conflicting regions */
11370 n
= TAILQ_FIRST(&s
->rl
);
11373 n
= TAILQ_NEXT(r
, entry
);
11374 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
11375 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
11376 if (r
->ws
->r
!= NULL
)
11377 r
->ws
->old_r
= r
->ws
->r
;
11380 xcb_destroy_window(conn
, r
->id
);
11381 r
->id
= XCB_WINDOW_NONE
;
11382 TAILQ_REMOVE(&s
->rl
, r
, entry
);
11383 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
11387 /* search old regions for one to reuse */
11389 /* size + location match */
11390 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11391 if (X(r
) == x
&& Y(r
) == y
&&
11392 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11396 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11397 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11401 TAILQ_REMOVE(&s
->orl
, r
, entry
);
11402 /* try to use old region's workspace */
11403 if (r
->ws
->r
== NULL
)
11406 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
11407 err(1, "new_region: calloc: failed to allocate memory "
11410 /* if we don't have a workspace already, find one */
11412 for (i
= 0; i
< workspace_limit
; i
++)
11413 if (s
->ws
[i
].r
== NULL
) {
11420 errx(1, "new_region: no free workspaces");
11422 if (ws
->state
== SWM_WS_STATE_HIDDEN
)
11423 ws
->state
= SWM_WS_STATE_MAPPING
;
11432 r
->ws_prior
= NULL
;
11435 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
11437 /* Invisible region window to detect pointer events on empty regions. */
11438 r
->id
= xcb_generate_id(conn
);
11439 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
11440 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
11442 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
11443 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
11444 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
11446 /* Make sure region input is at the bottom. */
11447 wa
[0] = XCB_STACK_MODE_BELOW
;
11448 xcb_configure_window(conn
, r
->id
, XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
11450 xcb_map_window(conn
, r
->id
);
11454 scan_randr(int idx
)
11456 #ifdef SWM_XRR_HAS_CRTC
11459 #endif /* SWM_XRR_HAS_CRTC */
11460 struct swm_region
*r
;
11461 struct ws_win
*win
;
11463 xcb_randr_get_screen_resources_current_cookie_t src
;
11464 xcb_randr_get_screen_resources_current_reply_t
*srr
;
11465 xcb_randr_get_crtc_info_cookie_t cic
;
11466 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
11467 xcb_randr_crtc_t
*crtc
;
11468 xcb_screen_t
*screen
;
11470 DNPRINTF(SWM_D_MISC
, "scan_randr: screen: %d\n", idx
);
11472 if ((screen
= get_screen(idx
)) == NULL
)
11473 errx(1, "ERROR: can't get screen %d.", idx
);
11475 num_screens
= get_screen_count();
11476 if (idx
>= num_screens
)
11477 errx(1, "scan_randr: invalid screen");
11479 /* remove any old regions */
11480 while ((r
= TAILQ_FIRST(&screens
[idx
].rl
)) != NULL
) {
11481 r
->ws
->old_r
= r
->ws
->r
= NULL
;
11483 xcb_destroy_window(conn
, r
->id
);
11484 r
->id
= XCB_WINDOW_NONE
;
11485 TAILQ_REMOVE(&screens
[idx
].rl
, r
, entry
);
11486 TAILQ_INSERT_TAIL(&screens
[idx
].orl
, r
, entry
);
11490 /* map virtual screens onto physical screens */
11491 #ifdef SWM_XRR_HAS_CRTC
11492 if (randr_support
) {
11493 src
= xcb_randr_get_screen_resources_current(conn
,
11494 screens
[idx
].root
);
11495 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
11498 new_region(&screens
[idx
], 0, 0,
11499 screen
->width_in_pixels
,
11500 screen
->height_in_pixels
);
11503 ncrtc
= srr
->num_crtcs
;
11505 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
11506 for (c
= 0; c
< ncrtc
; c
++) {
11507 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
11509 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
11512 if (cir
->num_outputs
== 0) {
11517 if (cir
->mode
== 0)
11518 new_region(&screens
[idx
], 0, 0,
11519 screen
->width_in_pixels
,
11520 screen
->height_in_pixels
);
11522 new_region(&screens
[idx
],
11523 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
11528 #endif /* SWM_XRR_HAS_CRTC */
11530 /* If detection failed, create a single region that spans the screen. */
11531 if (TAILQ_EMPTY(&screens
[idx
].rl
))
11532 new_region(&screens
[idx
], 0, 0, screen
->width_in_pixels
,
11533 screen
->height_in_pixels
);
11536 /* Cleanup unused previously visible workspaces. */
11537 TAILQ_FOREACH(r
, &screens
[idx
].orl
, entry
) {
11538 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
11540 r
->ws
->state
= SWM_WS_STATE_HIDDEN
;
11542 /* The screen shouldn't focus on an unused region. */
11543 if (screens
[idx
].r_focus
== r
)
11544 screens
[idx
].r_focus
= NULL
;
11547 DNPRINTF(SWM_D_MISC
, "scan_randr: done.\n");
11551 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
11553 struct swm_region
*r
;
11554 int i
, num_screens
;
11556 DNPRINTF(SWM_D_EVENT
, "screenchange: root: %#x\n", e
->root
);
11558 num_screens
= get_screen_count();
11559 /* silly event doesn't include the screen index */
11560 for (i
= 0; i
< num_screens
; i
++)
11561 if (screens
[i
].root
== e
->root
)
11563 if (i
>= num_screens
)
11564 errx(1, "screenchange: screen not found");
11566 /* brute force for now, just re-enumerate the regions */
11570 print_win_geom(e
->root
);
11572 /* add bars to all regions */
11573 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11576 /* Stack all regions. */
11577 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11580 /* Make sure a region has focus. */
11581 if (screens
[i
].r_focus
== NULL
) {
11582 r
= TAILQ_FIRST(&screens
[i
].rl
);
11589 /* Update workspace state and bar on all regions. */
11590 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11591 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
11599 struct swm_region
*r
= NULL
;
11600 xcb_query_tree_cookie_t qtc
;
11601 xcb_query_tree_reply_t
*qtr
;
11602 xcb_get_property_cookie_t pc
;
11603 xcb_get_property_reply_t
*pr
;
11604 xcb_window_t
*wins
= NULL
, trans
, *cwins
= NULL
;
11605 int i
, j
, k
, n
, no
, num_screens
;
11607 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
11608 num_screens
= get_screen_count();
11609 for (i
= 0; i
< num_screens
; i
++) {
11610 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
11611 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
11614 wins
= xcb_query_tree_children(qtr
);
11615 no
= xcb_query_tree_children_length(qtr
);
11617 /* Try to sort windows according to _NET_CLIENT_LIST. */
11618 pr
= xcb_get_property_reply(conn
, xcb_get_property(conn
, 0,
11619 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
11620 XCB_ATOM_WINDOW
, 0, UINT32_MAX
), NULL
);
11622 cwins
= xcb_get_property_value(pr
);
11623 n
= xcb_get_property_value_length(pr
) /
11624 sizeof(xcb_atom_t
);
11626 for (j
= 0; j
< n
; ++j
) {
11627 for (k
= j
; k
< no
; ++k
) {
11628 if (wins
[k
] == cwins
[j
]) {
11629 /* Swap wins j and k. */
11631 wins
[j
] = cwins
[j
];
11639 /* Manage top-level windows first, then transients. */
11640 /* TODO: allow transients to be managed before leader. */
11641 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top-level windows.\n");
11642 for (j
= 0; j
< no
; j
++) {
11643 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11644 if (r
->id
== wins
[j
]) {
11645 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11646 "skip %#x; region input window.\n",
11649 } else if (r
->bar
->id
== wins
[j
]) {
11650 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11651 "skip %#x; region bar.\n",
11660 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11661 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11663 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
11664 "is transient for %#x.\n", wins
[j
], trans
);
11668 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11671 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
11672 for (j
= 0; j
< no
; j
++) {
11673 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11674 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11676 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11680 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
11684 setup_screens(void)
11686 int i
, j
, k
, num_screens
;
11687 struct workspace
*ws
;
11688 uint32_t gcv
[1], wa
[1];
11689 const xcb_query_extension_reply_t
*qep
;
11690 xcb_screen_t
*screen
;
11691 xcb_randr_query_version_cookie_t c
;
11692 xcb_randr_query_version_reply_t
*r
;
11694 num_screens
= get_screen_count();
11695 if ((screens
= calloc(num_screens
, sizeof(struct swm_screen
))) == NULL
)
11696 err(1, "setup_screens: calloc: failed to allocate memory for "
11699 /* Initial RandR setup. */
11700 randr_support
= false;
11701 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
11702 if (qep
->present
) {
11703 c
= xcb_randr_query_version(conn
, 1, 1);
11704 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
11706 if (r
->major_version
>= 1) {
11707 randr_support
= true;
11708 randr_eventbase
= qep
->first_event
;
11714 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
11716 /* map physical screens */
11717 for (i
= 0; i
< num_screens
; i
++) {
11718 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
11719 screens
[i
].idx
= i
;
11720 screens
[i
].r_focus
= NULL
;
11722 TAILQ_INIT(&screens
[i
].rl
);
11723 TAILQ_INIT(&screens
[i
].orl
);
11724 if ((screen
= get_screen(i
)) == NULL
)
11725 errx(1, "ERROR: can't get screen %d.", i
);
11726 screens
[i
].root
= screen
->root
;
11728 /* set default colors */
11729 setscreencolor("red", i
, SWM_S_COLOR_FOCUS
);
11730 setscreencolor("rgb:88/88/88", i
, SWM_S_COLOR_UNFOCUS
);
11731 setscreencolor("rgb:00/80/80", i
, SWM_S_COLOR_BAR_BORDER
);
11732 setscreencolor("rgb:00/40/40", i
,
11733 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
11734 setscreencolor("black", i
, SWM_S_COLOR_BAR
);
11735 setscreencolor("rgb:a0/a0/a0", i
, SWM_S_COLOR_BAR_FONT
);
11736 setscreencolor("red", i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
11737 setscreencolor("rgb:88/88/88", i
,
11738 SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
11740 /* create graphics context on screen */
11741 screens
[i
].bar_gc
= xcb_generate_id(conn
);
11743 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
11744 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
11746 /* set default cursor */
11747 xcb_change_window_attributes(conn
, screens
[i
].root
,
11748 XCB_CW_CURSOR
, wa
);
11750 /* init all workspaces */
11751 /* XXX these should be dynamically allocated too */
11752 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
11753 ws
= &screens
[i
].ws
[j
];
11756 ws
->bar_enabled
= true;
11758 ws
->focus_prev
= NULL
;
11759 ws
->focus_pending
= NULL
;
11762 ws
->state
= SWM_WS_STATE_HIDDEN
;
11763 TAILQ_INIT(&ws
->stack
);
11764 TAILQ_INIT(&ws
->winlist
);
11765 TAILQ_INIT(&ws
->unmanagedlist
);
11767 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
11768 if (layouts
[k
].l_config
!= NULL
)
11769 layouts
[k
].l_config(ws
,
11770 SWM_ARG_ID_STACKINIT
);
11771 ws
->cur_layout
= &layouts
[0];
11772 ws
->cur_layout
->l_string(ws
);
11778 xcb_randr_select_input(conn
, screens
[i
].root
,
11779 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
11784 setup_globals(void)
11786 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
11787 err(1, "setup_globals: strdup: failed to allocate memory.");
11789 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
11790 err(1, "setup_globals: strdup: failed to allocate memory.");
11792 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
11793 errx(1, "unable to allocate key symbols");
11795 a_state
= get_atom_from_string("WM_STATE");
11796 a_prot
= get_atom_from_string("WM_PROTOCOLS");
11797 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
11798 a_net_frame_extents
= get_atom_from_string("_NET_FRAME_EXTENTS");
11799 a_net_supported
= get_atom_from_string("_NET_SUPPORTED");
11800 a_net_wm_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
11801 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
11802 a_utf8_string
= get_atom_from_string("UTF8_STRING");
11803 a_swm_ws
= get_atom_from_string("_SWM_WS");
11807 shutdown_cleanup(void)
11809 struct swm_region
*r
;
11811 struct workspace
*ws
;
11812 int i
, num_screens
;
11814 /* disable alarm because the following code may not be interrupted */
11816 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
11817 err(1, "can't disable alarm");
11830 num_screens
= get_screen_count();
11831 for (i
= 0; i
< num_screens
; ++i
) {
11834 DNPRINTF(SWM_D_FOCUS
, "shutdown_cleanup: set_input_focus: "
11835 "%#x, revert-to: root, time: 0\n", screens
[i
].root
);
11836 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11837 screens
[i
].root
, XCB_CURRENT_TIME
);
11839 if (screens
[i
].bar_gc
!= XCB_NONE
)
11840 xcb_free_gc(conn
, screens
[i
].bar_gc
);
11841 if (!bar_font_legacy
) {
11842 XftColorFree(display
, DefaultVisual(display
, i
),
11843 DefaultColormap(display
, i
), &bar_font_color
);
11844 XftColorFree(display
, DefaultVisual(display
, i
),
11845 DefaultColormap(display
, i
), &search_font_color
);
11848 for (j
= 0; j
< SWM_S_COLOR_MAX
; ++j
) {
11849 free(screens
[i
].c
[j
].name
);
11852 /* Free window memory. */
11853 for (j
= 0; j
< SWM_WS_MAX
; ++j
) {
11854 ws
= &screens
[i
].ws
[j
];
11857 while ((w
= TAILQ_FIRST(&ws
->winlist
)) != NULL
) {
11858 TAILQ_REMOVE(&ws
->winlist
, w
, entry
);
11862 while ((w
= TAILQ_FIRST(&ws
->unmanagedlist
)) != NULL
) {
11863 TAILQ_REMOVE(&ws
->unmanagedlist
, w
, entry
);
11868 /* Free region memory. */
11869 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
11870 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
11875 while ((r
= TAILQ_FIRST(&screens
[i
].orl
)) != NULL
) {
11876 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
11885 free(clock_format
);
11886 free(startup_exception
);
11889 XFreeFontSet(display
, bar_fs
);
11891 XftFontClose(display
, bar_font
);
11893 xcb_key_symbols_free(syms
);
11895 xcb_aux_sync(conn
);
11896 xcb_disconnect(conn
);
11900 event_error(xcb_generic_error_t
*e
)
11904 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
11905 "resource_id: %u, minor_code: %u\n",
11906 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
11907 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
11908 e
->sequence
, e
->resource_id
, e
->minor_code
);
11912 event_handle(xcb_generic_event_t
*evt
)
11914 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
11916 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
11917 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
11918 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
11921 #define EVENT(type, callback) case type: callback((void *)evt); return
11922 EVENT(0, event_error
);
11923 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
11924 EVENT(XCB_BUTTON_RELEASE
, buttonrelease
);
11925 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
11926 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
11927 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
11928 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
11929 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
11930 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
11931 /*EVENT(XCB_CREATE_NOTIFY, );*/
11932 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
11933 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
11934 EVENT(XCB_EXPOSE
, expose
);
11935 EVENT(XCB_FOCUS_IN
, focusin
);
11937 EVENT(XCB_FOCUS_OUT
, focusout
);
11939 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
11940 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
11941 EVENT(XCB_KEY_PRESS
, keypress
);
11942 EVENT(XCB_KEY_RELEASE
, keyrelease
);
11943 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
11945 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
11947 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
11948 EVENT(XCB_MAP_REQUEST
, maprequest
);
11949 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
11950 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
11951 /*EVENT(XCB_NO_EXPOSURE, );*/
11952 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
11953 EVENT(XCB_REPARENT_NOTIFY
, reparentnotify
);
11954 /*EVENT(XCB_RESIZE_REQUEST, );*/
11955 /*EVENT(XCB_SELECTION_CLEAR, );*/
11956 /*EVENT(XCB_SELECTION_NOTIFY, );*/
11957 /*EVENT(XCB_SELECTION_REQUEST, );*/
11958 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
11959 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
11962 if (type
- randr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
11963 screenchange((void *)evt
);
11967 main(int argc
, char *argv
[])
11969 struct pollfd pfd
[2];
11970 struct sigaction sact
;
11972 struct passwd
*pwd
;
11973 struct swm_region
*r
;
11974 xcb_generic_event_t
*evt
;
11975 int xfd
, i
, num_screens
, num_readable
;
11976 char conf
[PATH_MAX
], *cfile
= NULL
;
11977 bool stdin_ready
= false, startup
= true;
11979 /* suppress unused warning since var is needed */
11983 time_started
= time(NULL
);
11987 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
11988 if (setlocale(LC_CTYPE
, "") == NULL
|| setlocale(LC_TIME
, "") == NULL
)
11989 warnx("no locale support");
11991 /* handle some signals */
11992 bzero(&sact
, sizeof(sact
));
11993 sigemptyset(&sact
.sa_mask
);
11995 sact
.sa_handler
= sighdlr
;
11996 sigaction(SIGINT
, &sact
, NULL
);
11997 sigaction(SIGQUIT
, &sact
, NULL
);
11998 sigaction(SIGTERM
, &sact
, NULL
);
11999 sigaction(SIGHUP
, &sact
, NULL
);
12001 sact
.sa_handler
= sighdlr
;
12002 sact
.sa_flags
= SA_NOCLDSTOP
;
12003 sigaction(SIGCHLD
, &sact
, NULL
);
12005 if ((display
= XOpenDisplay(0)) == NULL
)
12006 errx(1, "can not open display");
12008 conn
= XGetXCBConnection(display
);
12009 if (xcb_connection_has_error(conn
))
12010 errx(1, "can not get XCB connection");
12012 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
12014 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
12015 xfd
= xcb_get_file_descriptor(conn
);
12017 /* look for local and global conf file */
12018 pwd
= getpwuid(getuid());
12020 errx(1, "invalid user: %d", getuid());
12022 xcb_grab_server(conn
);
12023 xcb_aux_sync(conn
);
12025 /* flush all events */
12026 while ((evt
= get_next_event(false))) {
12027 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
12033 errx(1, "another window manager is currently running");
12035 /* Load Xcursors and/or cursorfont glyph cursors. */
12038 xcb_aux_sync(conn
);
12043 setup_keybindings();
12044 setup_btnbindings();
12049 for (i
= 0; ; i
++) {
12054 snprintf(conf
, sizeof conf
, "%s/.%s",
12055 pwd
->pw_dir
, SWM_CONF_FILE
);
12059 snprintf(conf
, sizeof conf
, "/etc/%s",
12064 snprintf(conf
, sizeof conf
, "%s/.%s",
12065 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
12068 /* global compat */
12069 snprintf(conf
, sizeof conf
, "/etc/%s",
12070 SWM_CONF_FILE_OLD
);
12076 if (strlen(conf
) && stat(conf
, &sb
) != -1)
12077 if (S_ISREG(sb
.st_mode
)) {
12084 /* load conf (if any) */
12086 conf_load(cfile
, SWM_CONF_DEFAULT
);
12090 if (getenv("SWM_STARTED") == NULL
)
12091 setenv("SWM_STARTED", "YES", 1);
12093 /* setup all bars */
12094 num_screens
= get_screen_count();
12095 for (i
= 0; i
< num_screens
; i
++)
12096 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12099 /* Manage existing windows. */
12105 /* Stack all regions to trigger mapping. */
12106 for (i
= 0; i
< num_screens
; i
++)
12107 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12110 xcb_ungrab_server(conn
);
12113 /* Update state and bar of each newly mapped workspace. */
12114 for (i
= 0; i
< num_screens
; i
++)
12115 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
12116 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
12120 memset(&pfd
, 0, sizeof(pfd
));
12122 pfd
[0].events
= POLLIN
;
12123 pfd
[1].fd
= STDIN_FILENO
;
12124 pfd
[1].events
= POLLIN
;
12127 while ((evt
= get_next_event(false))) {
12134 /* If just (re)started, set default focus if needed. */
12138 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
12139 r
= TAILQ_FIRST(&screens
[0].rl
);
12151 num_readable
= poll(pfd
, bar_extra
? 2 : 1, 1000);
12152 if (num_readable
== -1) {
12153 DNPRINTF(SWM_D_MISC
, "poll failed: %s",
12155 } else if (num_readable
> 0 && bar_extra
&&
12156 pfd
[1].revents
& POLLIN
) {
12157 stdin_ready
= true;
12161 restart(NULL
, NULL
, NULL
);
12167 stdin_ready
= false;
12168 bar_extra_update();
12171 /* Need to ensure the bar(s) are always updated. */
12172 for (i
= 0; i
< num_screens
; i
++)
12173 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12179 shutdown_cleanup();