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 WS_FOCUSED(ws) ((ws)->r && (ws)->r->s->r_focus == (ws)->r)
272 #define YESNO(x) ((x) ? "yes" : "no")
273 #define ICONIC(w) ((w)->ewmh_flags & EWMH_F_HIDDEN)
274 #define ABOVE(w) ((w)->ewmh_flags & EWMH_F_ABOVE)
275 #define FULLSCREEN(w) ((w)->ewmh_flags & EWMH_F_FULLSCREEN)
276 #define MAXIMIZED_VERT(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_VERT)
277 #define MAXIMIZED_HORZ(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_HORZ)
278 #define MAXIMIZED(w) (MAXIMIZED_VERT(w) || MAXIMIZED_HORZ(w))
279 #define MANUAL(w) ((w)->ewmh_flags & SWM_F_MANUAL)
280 #define TRANS(w) ((w)->transient != XCB_WINDOW_NONE)
281 #define FLOATING(w) (ABOVE(w) || TRANS(w) || FULLSCREEN(w) || \
284 /* Constrain Window flags */
285 #define SWM_CW_RESIZABLE (0x01)
286 #define SWM_CW_SOFTBOUNDARY (0x02)
287 #define SWM_CW_HARDBOUNDARY (0x04)
288 #define SWM_CW_RIGHT (0x10)
289 #define SWM_CW_LEFT (0x20)
290 #define SWM_CW_BOTTOM (0x40)
291 #define SWM_CW_TOP (0x80)
292 #define SWM_CW_ALLSIDES (0xf0)
294 #define SWM_FOCUS_DEFAULT (0)
295 #define SWM_FOCUS_FOLLOW (1)
296 #define SWM_FOCUS_MANUAL (2)
298 #define SWM_CK_NONE (0)
299 #define SWM_CK_ALL (0xf)
300 #define SWM_CK_FOCUS (0x1)
301 #define SWM_CK_POINTER (0x2)
302 #define SWM_CK_FALLBACK (0x4)
303 #define SWM_CK_REGION (0x8)
305 #define SWM_G_ALL (0xf)
306 #define SWM_G_SIZE (0x1)
307 #define SWM_G_POS (0x2)
309 #define SWM_CONF_DEFAULT (0)
310 #define SWM_CONF_KEYMAPPING (1)
313 #define SWM_LIB "/usr/local/lib/libswmhack.so"
320 xcb_atom_t a_net_frame_extents
;
321 xcb_atom_t a_net_wm_check
;
322 xcb_atom_t a_net_supported
;
323 xcb_atom_t a_takefocus
;
324 xcb_atom_t a_utf8_string
;
326 volatile sig_atomic_t running
= 1;
327 volatile sig_atomic_t restart_wm
= 0;
328 xcb_timestamp_t last_event_time
= 0;
332 unsigned int numlockmask
= 0;
335 xcb_connection_t
*conn
;
336 xcb_key_symbols_t
*syms
;
338 int boundary_width
= 50;
339 bool cycle_empty
= false;
340 bool cycle_visible
= false;
342 int font_adjusted
= 0;
343 uint16_t mod_key
= MODKEY
;
344 bool warp_focus
= false;
345 bool warp_pointer
= false;
346 bool workspace_clamp
= false;
349 struct swm_region
*search_r
;
350 int select_list_pipe
[2];
351 int select_resp_pipe
[2];
353 volatile sig_atomic_t search_resp
;
354 int search_resp_action
;
356 struct search_window
{
357 TAILQ_ENTRY(search_window
) entry
;
361 xcb_window_t indicator
;
363 TAILQ_HEAD(search_winlist
, search_window
);
364 struct search_winlist search_wl
;
369 SWM_SEARCH_UNICONIFY
,
370 SWM_SEARCH_NAME_WORKSPACE
,
371 SWM_SEARCH_SEARCH_WORKSPACE
,
372 SWM_SEARCH_SEARCH_WINDOW
375 #define SWM_STACK_TOP (0)
376 #define SWM_STACK_BOTTOM (1)
377 #define SWM_STACK_ABOVE (2)
378 #define SWM_STACK_BELOW (3)
381 double dialog_ratio
= 0.6;
383 #define SWM_BAR_MAX (256)
384 #define SWM_BAR_JUSTIFY_LEFT (0)
385 #define SWM_BAR_JUSTIFY_CENTER (1)
386 #define SWM_BAR_JUSTIFY_RIGHT (2)
387 #define SWM_BAR_OFFSET (4)
388 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
389 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
390 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
391 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
392 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
394 #ifdef X_HAVE_UTF8_STRING
395 #define DRAWSTRING(x...) Xutf8DrawString(x)
397 #define DRAWSTRING(x...) XmbDrawString(x)
400 char *bar_argv
[] = { NULL
, NULL
};
402 char bar_ext
[SWM_BAR_MAX
];
403 char bar_ext_buf
[SWM_BAR_MAX
];
404 char bar_vertext
[SWM_BAR_MAX
];
405 bool bar_version
= false;
406 bool bar_enabled
= true;
407 int bar_border_width
= 1;
408 bool bar_at_bottom
= false;
409 bool bar_extra
= false;
411 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
412 char *bar_format
= NULL
;
413 bool stack_enabled
= true;
414 bool clock_enabled
= true;
415 bool iconic_enabled
= false;
416 bool maximize_hide_bar
= false;
417 bool urgent_enabled
= false;
418 bool urgent_collapse
= false;
419 char *clock_format
= NULL
;
420 bool window_class_enabled
= false;
421 bool window_instance_enabled
= false;
422 bool window_name_enabled
= false;
423 int focus_mode
= SWM_FOCUS_DEFAULT
;
424 int focus_close
= SWM_STACK_BELOW
;
425 bool focus_close_wrap
= true;
426 int focus_default
= SWM_STACK_TOP
;
427 int spawn_position
= SWM_STACK_TOP
;
428 bool disable_border
= false;
429 int border_width
= 1;
430 int region_padding
= 0;
432 bool java_workaround
= true;
433 bool verbose_layout
= false;
439 XFontSet bar_fs
= NULL
;
440 XFontSetExtents
*bar_fs_extents
;
441 XftFont
*bar_font
= NULL
;
442 bool bar_font_legacy
= true;
443 char *bar_fonts
= NULL
;
444 XftColor bar_font_color
;
445 XftColor search_font_color
;
446 char *startup_exception
= NULL
;
447 unsigned int nr_exceptions
= 0;
449 /* layout manager data */
450 struct swm_geometry
{
463 struct swm_geometry g
;
464 struct swm_region
*r
; /* Associated region. */
467 /* virtual "screens" */
469 TAILQ_ENTRY(swm_region
) entry
;
471 struct swm_geometry g
;
472 struct workspace
*ws
; /* current workspace on this region */
473 struct workspace
*ws_prior
; /* prior workspace on this region */
474 struct swm_screen
*s
; /* screen idx */
477 TAILQ_HEAD(swm_region_list
, swm_region
);
480 SWM_WIN_STATE_REPARENTING
,
481 SWM_WIN_STATE_REPARENTED
,
482 SWM_WIN_STATE_UNPARENTING
,
483 SWM_WIN_STATE_UNPARENTED
,
487 TAILQ_ENTRY(ws_win
) entry
;
488 TAILQ_ENTRY(ws_win
) stack_entry
;
491 xcb_window_t transient
;
492 struct ws_win
*focus_child
; /* focus on child transient */
493 struct swm_geometry g
; /* current geometry */
494 struct swm_geometry g_float
; /* region coordinates */
495 bool g_floatvalid
; /* g_float geometry validity */
500 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
507 struct workspace
*ws
; /* always valid */
508 struct swm_screen
*s
; /* always valid, never changes */
510 xcb_icccm_get_wm_class_reply_t ch
;
511 xcb_icccm_wm_hints_t hints
;
516 TAILQ_HEAD(ws_win_list
, ws_win
);
517 TAILQ_HEAD(ws_win_stack
, ws_win
);
521 TAILQ_ENTRY(pid_e
) entry
;
525 TAILQ_HEAD(pid_list
, pid_e
);
526 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
528 /* layout handlers */
529 void stack(struct swm_region
*);
530 void vertical_config(struct workspace
*, int);
531 void vertical_stack(struct workspace
*, struct swm_geometry
*);
532 void horizontal_config(struct workspace
*, int);
533 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
534 void max_stack(struct workspace
*, struct swm_geometry
*);
535 void plain_stacker(struct workspace
*);
536 void fancy_stacker(struct workspace
*);
539 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
540 void (*l_config
)(struct workspace
*, int);
542 #define SWM_L_FOCUSPREV (1<<0)
543 #define SWM_L_MAPONFOCUS (1<<1)
544 void (*l_string
)(struct workspace
*);
546 /* stack, configure */
547 { vertical_stack
, vertical_config
, 0, plain_stacker
},
548 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
550 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
551 { NULL
, NULL
, 0, NULL
},
554 /* position of max_stack mode in the layouts array, index into layouts! */
555 #define SWM_V_STACK (0)
556 #define SWM_H_STACK (1)
557 #define SWM_MAX_STACK (2)
559 #define SWM_H_SLICE (32)
560 #define SWM_V_SLICE (32)
562 /* define work spaces */
564 int idx
; /* workspace index */
565 char *name
; /* workspace name */
566 bool always_raise
; /* raise windows on focus */
567 bool bar_enabled
; /* bar visibility */
568 struct layout
*cur_layout
; /* current layout handlers */
569 struct ws_win
*focus
; /* may be NULL */
570 struct ws_win
*focus_prev
; /* may be NULL */
571 struct ws_win
*focus_pending
; /* may be NULL */
572 struct swm_region
*r
; /* may be NULL */
573 struct swm_region
*old_r
; /* may be NULL */
574 struct ws_win_list winlist
; /* list of windows in ws */
575 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
576 struct ws_win_stack stack
; /* stacking order */
577 int state
; /* mapping state */
578 char stacker
[10]; /* display stacker and layout */
582 int horizontal_msize
;
584 int horizontal_stacks
;
585 bool horizontal_flip
;
595 SWM_WS_STATE_MAPPING
,
601 SWM_S_COLOR_BAR_BORDER
,
602 SWM_S_COLOR_BAR_BORDER_UNFOCUS
,
603 SWM_S_COLOR_BAR_FONT
,
605 SWM_S_COLOR_FOCUS_MAXIMIZED
,
607 SWM_S_COLOR_UNFOCUS_MAXIMIZED
,
611 /* physical screen mapping */
612 #define SWM_WS_MAX (22) /* hard limit */
613 int workspace_limit
= 10; /* soft limit */
616 int idx
; /* screen index */
617 struct swm_region_list rl
; /* list of regions on this screen */
618 struct swm_region_list orl
; /* list of old regions */
620 struct workspace ws
[SWM_WS_MAX
];
621 struct swm_region
*r_focus
;
628 } c
[SWM_S_COLOR_MAX
];
630 xcb_gcontext_t bar_gc
;
633 struct swm_screen
*screens
;
635 /* args to functions */
638 #define SWM_ARG_ID_FOCUSNEXT (0)
639 #define SWM_ARG_ID_FOCUSPREV (1)
640 #define SWM_ARG_ID_FOCUSMAIN (2)
641 #define SWM_ARG_ID_FOCUSURGENT (3)
642 #define SWM_ARG_ID_SWAPNEXT (10)
643 #define SWM_ARG_ID_SWAPPREV (11)
644 #define SWM_ARG_ID_SWAPMAIN (12)
645 #define SWM_ARG_ID_MOVELAST (13)
646 #define SWM_ARG_ID_MASTERSHRINK (20)
647 #define SWM_ARG_ID_MASTERGROW (21)
648 #define SWM_ARG_ID_MASTERADD (22)
649 #define SWM_ARG_ID_MASTERDEL (23)
650 #define SWM_ARG_ID_FLIPLAYOUT (24)
651 #define SWM_ARG_ID_STACKRESET (30)
652 #define SWM_ARG_ID_STACKINIT (31)
653 #define SWM_ARG_ID_STACKBALANCE (32)
654 #define SWM_ARG_ID_CYCLEWS_UP (40)
655 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
656 #define SWM_ARG_ID_CYCLERG_UP (42)
657 #define SWM_ARG_ID_CYCLERG_DOWN (43)
658 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
659 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
660 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
661 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
662 #define SWM_ARG_ID_STACKINC (50)
663 #define SWM_ARG_ID_STACKDEC (51)
664 #define SWM_ARG_ID_SS_ALL (60)
665 #define SWM_ARG_ID_SS_WINDOW (61)
666 #define SWM_ARG_ID_DONTCENTER (70)
667 #define SWM_ARG_ID_CENTER (71)
668 #define SWM_ARG_ID_KILLWINDOW (80)
669 #define SWM_ARG_ID_DELETEWINDOW (81)
670 #define SWM_ARG_ID_WIDTHGROW (90)
671 #define SWM_ARG_ID_WIDTHSHRINK (91)
672 #define SWM_ARG_ID_HEIGHTGROW (92)
673 #define SWM_ARG_ID_HEIGHTSHRINK (93)
674 #define SWM_ARG_ID_MOVEUP (100)
675 #define SWM_ARG_ID_MOVEDOWN (101)
676 #define SWM_ARG_ID_MOVELEFT (102)
677 #define SWM_ARG_ID_MOVERIGHT (103)
678 #define SWM_ARG_ID_RAISE (105)
679 #define SWM_ARG_ID_LOWER (106)
680 #define SWM_ARG_ID_BAR_TOGGLE (110)
681 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
682 #define SWM_ARG_ID_CYCLERG_MOVE_UP (112)
683 #define SWM_ARG_ID_CYCLERG_MOVE_DOWN (113)
689 TAILQ_ENTRY(quirk
) entry
;
690 char *class; /* WM_CLASS:class */
691 char *instance
; /* WM_CLASS:instance */
692 char *name
; /* WM_NAME */
694 regex_t regex_instance
;
697 int ws
; /* Initial workspace. */
698 #define SWM_Q_FLOAT (1<<0) /* Float this window. */
699 #define SWM_Q_TRANSSZ (1<<1) /* Transient window size too small. */
700 #define SWM_Q_ANYWHERE (1<<2) /* Don't position this window */
701 #define SWM_Q_XTERM_FONTADJ (1<<3) /* Adjust xterm fonts when resizing. */
702 #define SWM_Q_FULLSCREEN (1<<4) /* Remove border when fullscreen. */
703 #define SWM_Q_FOCUSPREV (1<<5) /* Focus on caller. */
704 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
705 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
706 #define SWM_Q_OBEYAPPFOCUSREQ (1<<8) /* Focus when applications ask. */
707 #define SWM_Q_IGNOREPID (1<<9) /* Ignore PID when determining ws. */
708 #define SWM_Q_IGNORESPAWNWS (1<<10) /* Ignore _SWM_WS when managing win. */
709 #define SWM_Q_NOFOCUSCYCLE (1<<11) /* Remove from normal focus cycle. */
710 #define SWM_Q_MINIMALBORDER (1<<12) /* No border when floating/unfocused. */
712 TAILQ_HEAD(quirk_list
, quirk
);
713 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
716 * Supported EWMH hints should be added to
717 * both the enum and the ewmh array
723 _NET_CURRENT_DESKTOP
,
724 _NET_DESKTOP_GEOMETRY
,
726 _NET_DESKTOP_VIEWPORT
,
727 _NET_MOVERESIZE_WINDOW
,
728 _NET_NUMBER_OF_DESKTOPS
,
729 _NET_REQUEST_FRAME_EXTENTS
,
731 _NET_WM_ACTION_ABOVE
,
732 _NET_WM_ACTION_CLOSE
,
733 _NET_WM_ACTION_FULLSCREEN
,
735 _NET_WM_ACTION_RESIZE
,
736 _NET_WM_ALLOWED_ACTIONS
,
738 _NET_WM_FULL_PLACEMENT
,
742 _NET_WM_STATE_FULLSCREEN
,
743 _NET_WM_STATE_HIDDEN
,
744 _NET_WM_STATE_MAXIMIZED_VERT
,
745 _NET_WM_STATE_MAXIMIZED_HORZ
,
746 _NET_WM_STATE_SKIP_PAGER
,
747 _NET_WM_STATE_SKIP_TASKBAR
,
749 _NET_WM_WINDOW_TYPE_DIALOG
,
750 _NET_WM_WINDOW_TYPE_DOCK
,
751 _NET_WM_WINDOW_TYPE_NORMAL
,
752 _NET_WM_WINDOW_TYPE_SPLASH
,
753 _NET_WM_WINDOW_TYPE_TOOLBAR
,
754 _NET_WM_WINDOW_TYPE_UTILITY
,
755 _SWM_WM_STATE_MANUAL
,
762 } ewmh
[SWM_EWMH_HINT_MAX
] = {
763 /* must be in same order as in the enum */
764 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
765 {"_NET_CLIENT_LIST", XCB_ATOM_NONE
},
766 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
767 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE
},
768 {"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE
},
769 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE
},
770 {"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE
},
771 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
772 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE
},
773 {"_NET_REQUEST_FRAME_EXTENTS", XCB_ATOM_NONE
},
774 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE
},
775 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
776 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
777 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
778 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
779 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
780 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
781 {"_NET_WM_DESKTOP", XCB_ATOM_NONE
},
782 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE
},
783 {"_NET_WM_NAME", XCB_ATOM_NONE
},
784 {"_NET_WM_STATE", XCB_ATOM_NONE
},
785 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
786 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
787 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
788 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
789 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
790 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
791 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
792 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
793 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
794 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
795 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
796 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
797 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
798 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
799 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
802 /* EWMH source type */
804 EWMH_SOURCE_TYPE_NONE
= 0,
805 EWMH_SOURCE_TYPE_NORMAL
= 1,
806 EWMH_SOURCE_TYPE_OTHER
= 2,
813 XC_BOTTOM_LEFT_CORNER
,
814 XC_BOTTOM_RIGHT_CORNER
,
822 char *name
; /* Name used by Xcursor .*/
823 uint8_t cf_char
; /* cursorfont index. */
825 } cursors
[XC_MAX
] = {
826 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
827 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
828 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
829 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
830 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
831 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
832 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
835 #define SWM_SPAWN_OPTIONAL 0x1
839 TAILQ_ENTRY(spawn_prog
) entry
;
845 TAILQ_HEAD(spawn_list
, spawn_prog
);
846 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
852 /* User callable function IDs. */
975 /* SWM_DEBUG actions MUST be here: */
988 BINDING_F_REPLAY
= 0x1,
992 RB_ENTRY(binding
) entry
;
993 uint16_t mod
; /* Modifier Mask. */
994 enum binding_type type
; /* Key or Button. */
995 uint32_t value
; /* KeySym or Button Index. */
996 enum actionid action
; /* Action Identifier. */
1000 RB_HEAD(binding_tree
, binding
);
1002 /* function prototypes */
1003 void adjust_font(struct ws_win
*);
1004 char *argsep(char **);
1005 void bar_cleanup(struct swm_region
*);
1006 void bar_extra_setup(void);
1007 void bar_extra_stop(void);
1008 int bar_extra_update(void);
1009 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
1010 void bar_fmt_expand(char *, size_t);
1011 void bar_draw(struct swm_bar
*);
1012 void bar_print(struct swm_region
*, const char *);
1013 void bar_print_legacy(struct swm_region
*, const char *);
1014 void bar_replace(char *, char *, struct swm_region
*, size_t);
1015 void bar_replace_pad(char *, int *, size_t);
1016 char *bar_replace_seq(char *, char *, struct swm_region
*, size_t *, size_t);
1017 void bar_setup(struct swm_region
*);
1018 void bar_toggle(struct binding
*, struct swm_region
*, union arg
*);
1019 void bar_urgent(char *, size_t);
1020 void bar_window_class(char *, size_t, struct swm_region
*);
1021 void bar_window_class_instance(char *, size_t, struct swm_region
*);
1022 void bar_window_float(char *, size_t, struct swm_region
*);
1023 void bar_window_instance(char *, size_t, struct swm_region
*);
1024 void bar_window_name(char *, size_t, struct swm_region
*);
1025 void bar_window_state(char *, size_t, struct swm_region
*);
1026 void bar_workspace_name(char *, size_t, struct swm_region
*);
1027 int binding_cmp(struct binding
*, struct binding
*);
1028 void binding_insert(uint16_t, enum binding_type
, uint32_t, enum actionid
,
1029 uint32_t, const char *);
1030 struct binding
*binding_lookup(uint16_t, enum binding_type
, uint32_t);
1031 void binding_remove(struct binding
*);
1032 void buttonpress(xcb_button_press_event_t
*);
1033 void buttonrelease(xcb_button_release_event_t
*);
1034 void center_pointer(struct swm_region
*);
1035 void check_conn(void);
1036 void clear_bindings(void);
1037 void clear_keybindings(void);
1038 int clear_maximized(struct workspace
*);
1039 void clear_quirks(void);
1040 void clear_spawns(void);
1041 void clientmessage(xcb_client_message_event_t
*);
1042 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
1043 int conf_load(const char *, int);
1044 void configurenotify(xcb_configure_notify_event_t
*);
1045 void configurerequest(xcb_configure_request_event_t
*);
1046 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
1047 void constrain_window(struct ws_win
*, struct swm_geometry
*, int *);
1048 int count_win(struct workspace
*, bool);
1049 void cursors_cleanup(void);
1050 void cursors_load(void);
1051 void custom_region(const char *);
1052 void cycle_layout(struct binding
*, struct swm_region
*, union arg
*);
1053 void cyclerg(struct binding
*, struct swm_region
*, union arg
*);
1054 void cyclews(struct binding
*, struct swm_region
*, union arg
*);
1056 void debug_refresh(struct ws_win
*);
1058 void debug_toggle(struct binding
*, struct swm_region
*, union arg
*);
1059 void destroynotify(xcb_destroy_notify_event_t
*);
1060 void dumpwins(struct binding
*, struct swm_region
*, union arg
*);
1061 int enable_wm(void);
1062 void enternotify(xcb_enter_notify_event_t
*);
1063 void event_drain(uint8_t);
1064 void event_error(xcb_generic_error_t
*);
1065 void event_handle(xcb_generic_event_t
*);
1066 void ewmh_apply_flags(struct ws_win
*, uint32_t);
1067 void ewmh_autoquirk(struct ws_win
*);
1068 void ewmh_get_desktop_names(void);
1069 void ewmh_get_wm_state(struct ws_win
*);
1070 void ewmh_update_actions(struct ws_win
*);
1071 void ewmh_update_client_list(void);
1072 void ewmh_update_current_desktop(void);
1073 void ewmh_update_desktop_names(void);
1074 void ewmh_update_desktops(void);
1075 void ewmh_change_wm_state(struct ws_win
*, xcb_atom_t
, long);
1076 void ewmh_update_wm_state(struct ws_win
*);
1077 char *expand_tilde(const char *);
1078 void expose(xcb_expose_event_t
*);
1079 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
1080 struct swm_bar
*find_bar(xcb_window_t
);
1081 struct ws_win
*find_frame_window(xcb_window_t
);
1082 struct pid_e
*find_pid(pid_t
);
1083 struct swm_region
*find_region(xcb_window_t
);
1084 struct ws_win
*find_unmanaged_window(xcb_window_t
);
1085 struct ws_win
*find_window(xcb_window_t
);
1086 void floating_toggle(struct binding
*, struct swm_region
*, union arg
*);
1087 void focus(struct binding
*, struct swm_region
*, union arg
*);
1088 void focus_flush(void);
1089 void focus_pointer(struct binding
*, struct swm_region
*, union arg
*);
1090 void focus_region(struct swm_region
*);
1091 void focus_win(struct ws_win
*);
1093 void focusin(xcb_focus_in_event_t
*);
1094 void focusout(xcb_focus_out_event_t
*);
1096 void focusrg(struct binding
*, struct swm_region
*, union arg
*);
1097 void fontset_init(void);
1098 void free_window(struct ws_win
*);
1099 xcb_atom_t
get_atom_from_string(const char *);
1101 char *get_atom_name(xcb_atom_t
);
1103 struct ws_win
*get_focus_magic(struct ws_win
*);
1104 struct ws_win
*get_focus_prev(struct ws_win
*);
1105 xcb_generic_event_t
*get_next_event(bool);
1107 char *get_notify_detail_label(uint8_t);
1108 char *get_notify_mode_label(uint8_t);
1110 struct ws_win
*get_pointer_win(xcb_window_t
);
1111 struct ws_win
*get_region_focus(struct swm_region
*);
1112 int get_region_index(struct swm_region
*);
1113 xcb_screen_t
*get_screen(int);
1114 int get_screen_count(void);
1116 char *get_source_type_label(uint32_t);
1117 char *get_stack_mode_name(uint8_t);
1118 char *get_state_mask_label(uint16_t);
1120 int32_t get_swm_ws(xcb_window_t
);
1121 bool get_urgent(struct ws_win
*);
1122 char *get_win_name(xcb_window_t
);
1123 uint8_t get_win_state(xcb_window_t
);
1124 void get_wm_protocols(struct ws_win
*);
1125 int get_ws_idx(struct ws_win
*);
1126 void grab_windows(void);
1127 void grabbuttons(void);
1128 void grabkeys(void);
1129 void iconify(struct binding
*, struct swm_region
*, union arg
*);
1130 bool isxlfd(char *);
1131 bool keybindreleased(struct binding
*, xcb_key_release_event_t
*);
1132 void keypress(xcb_key_press_event_t
*);
1133 void keyrelease(xcb_key_release_event_t
*);
1134 bool keyrepeating(xcb_key_release_event_t
*);
1135 void kill_bar_extra_atexit(void);
1136 void kill_refs(struct ws_win
*);
1138 void leavenotify(xcb_leave_notify_event_t
*);
1140 void load_float_geom(struct ws_win
*);
1141 void lower_window(struct ws_win
*);
1142 struct ws_win
*manage_window(xcb_window_t
, int, bool);
1143 void map_window(struct ws_win
*);
1144 void mapnotify(xcb_map_notify_event_t
*);
1145 void mappingnotify(xcb_mapping_notify_event_t
*);
1146 void maprequest(xcb_map_request_event_t
*);
1147 void maximize_toggle(struct binding
*, struct swm_region
*, union arg
*);
1148 void motionnotify(xcb_motion_notify_event_t
*);
1149 void move(struct binding
*, struct swm_region
*, union arg
*);
1150 void move_win(struct ws_win
*, struct binding
*, int);
1151 uint32_t name_to_pixel(int, const char *);
1152 void name_workspace(struct binding
*, struct swm_region
*, union arg
*);
1153 void new_region(struct swm_screen
*, int, int, int, int);
1154 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1155 int parsebinding(const char *, uint16_t *, enum binding_type
*, uint32_t *,
1157 int parsequirks(const char *, uint32_t *, int *);
1158 void pressbutton(struct binding
*, struct swm_region
*, union arg
*);
1159 void priorws(struct binding
*, struct swm_region
*, union arg
*);
1161 void print_win_geom(xcb_window_t
);
1162 void print_win_input_model(struct ws_win
*);
1164 void propertynotify(xcb_property_notify_event_t
*);
1165 void put_back_event(xcb_generic_event_t
*);
1166 void quirk_free(struct quirk
*);
1167 void quirk_insert(const char *, const char *, const char *, uint32_t, int);
1168 void quirk_remove(struct quirk
*);
1169 void quirk_replace(struct quirk
*, const char *, const char *, const char *,
1171 void quit(struct binding
*, struct swm_region
*, union arg
*);
1172 void raise_toggle(struct binding
*, struct swm_region
*, union arg
*);
1173 void raise_window(struct ws_win
*);
1174 void region_containment(struct ws_win
*, struct swm_region
*, int);
1175 struct swm_region
*region_under(struct swm_screen
*, int, int);
1176 void regionize(struct ws_win
*, int, int);
1177 void reparent_window(struct ws_win
*);
1178 void reparentnotify(xcb_reparent_notify_event_t
*);
1179 void resize(struct binding
*, struct swm_region
*, union arg
*);
1180 void resize_win(struct ws_win
*, struct binding
*, int);
1181 void restart(struct binding
*, struct swm_region
*, union arg
*);
1182 struct swm_region
*root_to_region(xcb_window_t
, int);
1183 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1184 void scan_randr(int);
1185 void search_do_resp(void);
1186 void search_resp_name_workspace(const char *, size_t);
1187 void search_resp_search_window(const char *);
1188 void search_resp_search_workspace(const char *);
1189 void search_resp_uniconify(const char *, size_t);
1190 void search_win(struct binding
*, struct swm_region
*, union arg
*);
1191 void search_win_cleanup(void);
1192 void search_workspace(struct binding
*, struct swm_region
*, union arg
*);
1193 void send_to_rg(struct binding
*, struct swm_region
*, union arg
*);
1194 void send_to_ws(struct binding
*, struct swm_region
*, union arg
*);
1195 void set_region(struct swm_region
*);
1196 int setautorun(const char *, const char *, int);
1197 void setbinding(uint16_t, enum binding_type
, uint32_t, enum actionid
,
1198 uint32_t, const char *);
1199 int setconfbinding(const char *, const char *, int);
1200 int setconfcolor(const char *, const char *, int);
1201 int setconfmodkey(const char *, const char *, int);
1202 int setconfquirk(const char *, const char *, int);
1203 int setconfregion(const char *, const char *, int);
1204 int setconfspawn(const char *, const char *, int);
1205 int setconfvalue(const char *, const char *, int);
1206 int setkeymapping(const char *, const char *, int);
1207 int setlayout(const char *, const char *, int);
1208 void setquirk(const char *, const char *, const char *, uint32_t, int);
1209 void setscreencolor(const char *, int, int);
1210 void setspawn(const char *, const char *, int);
1211 void setup_btnbindings(void);
1212 void setup_ewmh(void);
1213 void setup_globals(void);
1214 void setup_keybindings(void);
1215 void setup_quirks(void);
1216 void setup_screens(void);
1217 void setup_spawn(void);
1218 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1219 void set_win_state(struct ws_win
*, uint8_t);
1220 void shutdown_cleanup(void);
1222 void socket_setnonblock(int);
1223 void sort_windows(struct ws_win_list
*);
1224 void spawn(int, union arg
*, bool);
1225 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1226 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1227 void spawn_insert(const char *, const char *, int);
1228 struct spawn_prog
*spawn_find(const char *);
1229 void spawn_remove(struct spawn_prog
*);
1230 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1231 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1232 void stack_config(struct binding
*, struct swm_region
*, union arg
*);
1233 void stack_master(struct workspace
*, struct swm_geometry
*, int, bool);
1234 void store_float_geom(struct ws_win
*);
1235 char *strdupsafe(const char *);
1236 void swapwin(struct binding
*, struct swm_region
*, union arg
*);
1237 void switchws(struct binding
*, struct swm_region
*, union arg
*);
1238 void teardown_ewmh(void);
1239 void unescape_selector(char *);
1240 void unfocus_win(struct ws_win
*);
1241 void uniconify(struct binding
*, struct swm_region
*, union arg
*);
1242 void unmanage_window(struct ws_win
*);
1243 void unmap_all(void);
1244 void unmap_window(struct ws_win
*);
1245 void unmapnotify(xcb_unmap_notify_event_t
*);
1246 void unparent_window(struct ws_win
*);
1247 void update_floater(struct ws_win
*);
1248 void update_modkey(uint16_t);
1249 void update_win_stacking(struct ws_win
*);
1250 void update_window(struct ws_win
*);
1251 void draw_frame(struct ws_win
*);
1252 void update_wm_state(struct ws_win
*win
);
1253 void updatenumlockmask(void);
1254 void validate_spawns(void);
1255 int validate_win(struct ws_win
*);
1256 int validate_ws(struct workspace
*);
1257 void version(struct binding
*, struct swm_region
*, union arg
*);
1258 void win_to_ws(struct ws_win
*, int, bool);
1259 pid_t
window_get_pid(xcb_window_t
);
1260 void wkill(struct binding
*, struct swm_region
*, union arg
*);
1261 void update_ws_stack(struct workspace
*);
1262 void xft_init(struct swm_region
*);
1263 void _add_startup_exception(const char *, va_list);
1264 void add_startup_exception(const char *, ...);
1266 RB_PROTOTYPE(binding_tree
, binding
, entry
, binding_cmp
);
1267 RB_GENERATE(binding_tree
, binding
, entry
, binding_cmp
);
1268 struct binding_tree bindings
;
1273 xcb_font_t cf
= XCB_NONE
;
1276 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1277 /* try to load Xcursor first. */
1278 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1281 /* fallback to cursorfont. */
1282 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1283 if (cf
== XCB_NONE
) {
1284 cf
= xcb_generate_id(conn
);
1285 xcb_open_font(conn
, cf
, strlen("cursor"),
1289 cursors
[i
].cid
= xcb_generate_id(conn
);
1290 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1291 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1292 0xffff, 0xffff, 0xffff);
1298 xcb_close_font(conn
, cf
);
1302 cursors_cleanup(void)
1305 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1306 xcb_free_cursor(conn
, cursors
[i
].cid
);
1310 expand_tilde(const char *s
)
1312 struct passwd
*ppwd
;
1320 errx(1, "expand_tilde: NULL string.");
1323 result
= strdup(sc
);
1329 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1330 errx(1, "expand_tilde: sysconf");
1332 if ((user
= calloc(1, max
+ 1)) == NULL
)
1333 errx(1, "expand_tilde: calloc");
1335 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1340 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1344 result
= strdup(sc
);
1346 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1350 errx(1, "expand_tilde: failed to allocate memory.");
1356 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1358 unsigned int tmpr
, tmpg
, tmpb
;
1360 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1363 *rr
= RGB_8_TO_16(tmpr
);
1364 *gg
= RGB_8_TO_16(tmpg
);
1365 *bb
= RGB_8_TO_16(tmpb
);
1371 get_screen(int screen
)
1373 const xcb_setup_t
*r
;
1374 xcb_screen_iterator_t iter
;
1376 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1377 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1381 iter
= xcb_setup_roots_iterator(r
);
1382 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1390 get_screen_count(void)
1392 const xcb_setup_t
*r
;
1394 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1395 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1399 return xcb_setup_roots_length(r
);
1403 get_region_index(struct swm_region
*r
)
1405 struct swm_region
*rr
;
1411 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1426 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1427 event_drain(XCB_ENTER_NOTIFY
);
1433 get_atom_from_string(const char *str
)
1435 xcb_intern_atom_cookie_t c
;
1436 xcb_intern_atom_reply_t
*r
;
1439 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1440 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1448 return (XCB_ATOM_NONE
);
1452 get_wm_protocols(struct ws_win
*win
) {
1454 xcb_icccm_get_wm_protocols_reply_t wpr
;
1456 if (xcb_icccm_get_wm_protocols_reply(conn
,
1457 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
1459 for (i
= 0; i
< (int)wpr
.atoms_len
; i
++) {
1460 if (wpr
.atoms
[i
] == a_takefocus
)
1461 win
->take_focus
= true;
1462 if (wpr
.atoms
[i
] == a_delete
)
1463 win
->can_delete
= true;
1465 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
1472 xcb_window_t root
, win
;
1473 int i
, j
, num_screens
;
1475 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1476 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1478 num_screens
= get_screen_count();
1479 for (i
= 0; i
< num_screens
; i
++) {
1480 root
= screens
[i
].root
;
1482 /* Set up _NET_SUPPORTING_WM_CHECK. */
1483 win
= xcb_generate_id(conn
);
1484 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
1485 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1486 XCB_COPY_FROM_PARENT
, 0, NULL
);
1488 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
1489 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1490 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1491 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1493 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1494 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1495 8, strlen("spectrwm"), "spectrwm");
1497 /* Report supported atoms */
1498 xcb_delete_property(conn
, root
, a_net_supported
);
1499 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1500 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, root
,
1501 a_net_supported
, XCB_ATOM_ATOM
, 32, 1,
1506 ewmh_update_desktops();
1507 ewmh_get_desktop_names();
1515 xcb_get_property_cookie_t pc
;
1516 xcb_get_property_reply_t
*pr
;
1518 num_screens
= get_screen_count();
1520 for (i
= 0; i
< num_screens
; i
++) {
1521 /* Get the support check window and destroy it */
1522 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, a_net_wm_check
,
1523 XCB_ATOM_WINDOW
, 0, 1);
1524 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1527 if (pr
->format
== a_net_wm_check
) {
1528 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1530 xcb_destroy_window(conn
, id
);
1531 xcb_delete_property(conn
, screens
[i
].root
,
1533 xcb_delete_property(conn
, screens
[i
].root
,
1541 ewmh_autoquirk(struct ws_win
*win
)
1543 xcb_get_property_reply_t
*r
;
1544 xcb_get_property_cookie_t c
;
1548 c
= xcb_get_property(conn
, 0, win
->id
,
1549 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1550 r
= xcb_get_property_reply(conn
, c
, NULL
);
1554 type
= xcb_get_property_value(r
);
1555 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1557 for (i
= 0; i
< n
; i
++) {
1558 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1560 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1561 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1562 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1563 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1566 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1567 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1568 win
->quirks
= SWM_Q_FLOAT
;
1576 ewmh_update_actions(struct ws_win
*win
)
1578 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1584 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1587 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1588 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1589 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1592 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1593 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1596 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1597 #define _NET_WM_STATE_ADD 1 /* add/set property */
1598 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1601 ewmh_change_wm_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1608 name
= get_atom_name(state
);
1609 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: win %#x, state: %s, "
1610 "action: %ld\n", WINID(win
), name
, action
);
1616 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1617 flag
= EWMH_F_FULLSCREEN
;
1618 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1619 flag
= EWMH_F_ABOVE
;
1620 else if (state
== ewmh
[_NET_WM_STATE_HIDDEN
].atom
)
1621 flag
= EWMH_F_HIDDEN
;
1622 else if (state
== ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
||
1623 state
== ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
)
1624 flag
= EWMH_F_MAXIMIZED
;
1625 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1626 flag
= SWM_F_MANUAL
;
1627 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1628 flag
= EWMH_F_SKIP_PAGER
;
1629 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1630 flag
= EWMH_F_SKIP_TASKBAR
;
1632 /* Disallow unfloating transients. */
1633 if (TRANS(win
) && flag
== EWMH_F_ABOVE
)
1636 new_flags
= win
->ewmh_flags
;
1639 case _NET_WM_STATE_REMOVE
:
1642 case _NET_WM_STATE_ADD
:
1645 case _NET_WM_STATE_TOGGLE
:
1650 ewmh_apply_flags(win
, new_flags
);
1653 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: done.\n");
1657 ewmh_apply_flags(struct ws_win
*win
, uint32_t pending
)
1659 struct workspace
*ws
;
1662 changed
= win
->ewmh_flags
^ pending
;
1666 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: pending: %d\n", pending
);
1668 win
->ewmh_flags
= pending
;
1671 if (changed
& EWMH_F_HIDDEN
) {
1673 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1674 ws
->focus_pending
= get_focus_prev(win
);
1679 /* Reload floating geometry in case region changed. */
1681 load_float_geom(win
);
1683 /* The window is no longer iconic, prepare focus. */
1684 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1685 ws
->focus_pending
= get_focus_magic(win
);
1690 if (changed
& EWMH_F_ABOVE
) {
1691 if (ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1693 load_float_geom(win
);
1694 else if (!MAXIMIZED(win
))
1695 store_float_geom(win
);
1697 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1698 changed
&= ~EWMH_F_MAXIMIZED
;
1702 win
->ewmh_flags
^= EWMH_F_ABOVE
& pending
;
1706 if (changed
& EWMH_F_MAXIMIZED
) {
1707 /* VERT and/or HORZ changed. */
1709 if (!MAXIMIZED(win
))
1710 load_float_geom(win
);
1712 store_float_geom(win
);
1715 if (MAXIMIZED(win
)) {
1716 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
1717 ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1721 ws
->focus_pending
= win
;
1729 if (changed
& EWMH_F_FULLSCREEN
) {
1730 if (FULLSCREEN(win
)) {
1731 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
1735 ws
->focus_pending
= win
;
1738 load_float_geom(win
);
1741 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1745 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: done.\n");
1749 ewmh_update_wm_state(struct ws_win
*win
) {
1750 xcb_atom_t vals
[SWM_EWMH_ACTION_COUNT_MAX
];
1754 vals
[n
++] = ewmh
[_NET_WM_STATE_HIDDEN
].atom
;
1755 if (FULLSCREEN(win
))
1756 vals
[n
++] = ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
;
1757 if (MAXIMIZED_VERT(win
))
1758 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
;
1759 if (MAXIMIZED_HORZ(win
))
1760 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
;
1761 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1762 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
;
1763 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1764 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
;
1765 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1766 vals
[n
++] = ewmh
[_NET_WM_STATE_ABOVE
].atom
;
1767 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1768 vals
[n
++] = ewmh
[_SWM_WM_STATE_MANUAL
].atom
;
1771 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1772 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, n
, vals
);
1774 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1778 ewmh_get_wm_state(struct ws_win
*win
)
1781 xcb_get_property_cookie_t c
;
1782 xcb_get_property_reply_t
*r
;
1788 win
->ewmh_flags
= 0;
1790 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1791 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1792 r
= xcb_get_property_reply(conn
, c
, NULL
);
1796 states
= xcb_get_property_value(r
);
1797 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1799 for (i
= 0; i
< n
; i
++)
1800 ewmh_change_wm_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1808 dumpwins(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
1812 xcb_get_window_attributes_cookie_t c
;
1813 xcb_get_window_attributes_reply_t
*wa
;
1815 /* suppress unused warning since var is needed */
1819 if (r
->ws
== NULL
) {
1820 DPRINTF("dumpwins: invalid workspace\n");
1824 DPRINTF("=== managed window list ws %02d ===\n", r
->ws
->idx
);
1825 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
) {
1826 state
= get_win_state(w
->id
);
1827 c
= xcb_get_window_attributes(conn
, w
->id
);
1828 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1830 DPRINTF("win %#x (%#x), map_state: %d, state: %u, "
1831 "transient: %#x\n", w
->frame
, w
->id
, wa
->map_state
,
1832 state
, w
->transient
);
1835 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1839 DPRINTF("=== stacking order (top down) === \n");
1840 TAILQ_FOREACH(w
, &r
->ws
->stack
, stack_entry
) {
1841 DPRINTF("win %#x (%#x), fs: %s, maximized: %s, above: %s, "
1842 "iconic: %s\n", w
->frame
, w
->id
, YESNO(FULLSCREEN(w
)),
1843 YESNO(MAXIMIZED(w
)), YESNO(ABOVE(w
)), YESNO(ICONIC(w
)));
1846 DPRINTF("===== unmanaged window list =====\n");
1847 TAILQ_FOREACH(w
, &r
->ws
->unmanagedlist
, entry
) {
1848 state
= get_win_state(w
->id
);
1849 c
= xcb_get_window_attributes(conn
, w
->id
);
1850 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1852 DPRINTF("win %#x, map_state: %d, state: %u, "
1853 "transient: %#x\n", w
->id
, wa
->map_state
,
1854 state
, w
->transient
);
1857 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1861 DPRINTF("=================================\n");
1865 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
1868 int num_screens
, i
, j
;
1870 /* Suppress warnings. */
1875 DNPRINTF(SWM_D_MISC
, "debug_toggle\n");
1877 debug_enabled
= !debug_enabled
;
1879 num_screens
= get_screen_count();
1880 for (i
= 0; i
< num_screens
; i
++)
1881 for (j
= 0; j
< workspace_limit
; j
++)
1882 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1889 debug_refresh(struct ws_win
*win
)
1896 XRectangle l_ibox
, l_lbox
;
1897 xcb_rectangle_t rect
;
1899 uint32_t wc
[4], mask
, width
, height
, gcv
[1];
1902 xcb_screen_t
*screen
;
1904 if (debug_enabled
) {
1905 /* Create debug window if it doesn't exist. */
1906 if (win
->debug
== XCB_WINDOW_NONE
) {
1907 if ((screen
= get_screen(win
->s
->idx
)) == NULL
)
1908 errx(1, "ERROR: can't get screen %d.",
1911 win
->debug
= xcb_generate_id(conn
);
1912 wc
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1913 wc
[1] = win
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
1914 wc
[2] = screen
->default_colormap
;
1916 xcb_create_window(conn
, screen
->root_depth
, win
->debug
,
1917 win
->frame
, 0, 0, 10, 10, 1,
1918 XCB_WINDOW_CLASS_INPUT_OUTPUT
, screen
->root_visual
,
1919 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
|
1920 XCB_CW_COLORMAP
, wc
);
1922 xcb_map_window(conn
, win
->debug
);
1925 /* Determine workspace window list index. */
1927 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
1933 /* Determine stacking index (top down). */
1935 TAILQ_FOREACH(w
, &win
->ws
->stack
, stack_entry
) {
1941 if (asprintf(&s
, "%#x wl:%d s:%d", win
->id
, widx
, sidx
) == -1)
1946 /* Update window to an appropriate dimension. */
1947 if (bar_font_legacy
) {
1948 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
1949 width
= l_lbox
.width
+ 4;
1950 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
1952 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
1954 width
= info
.width
+ 4;
1955 height
= bar_font
->height
+ 4;
1958 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
1959 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
;
1961 wc
[0] = wc
[1] = border_width
;
1968 xcb_configure_window(conn
, win
->debug
, mask
, wc
);
1970 /* Draw a filled rectangle to 'clear' window. */
1974 rect
.height
= height
;
1976 gcv
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1977 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1978 xcb_poly_fill_rectangle(conn
, win
->debug
, win
->s
->bar_gc
, 1,
1982 if (bar_font_legacy
) {
1983 l_gcv
.graphics_exposures
= 0;
1984 l_draw
= XCreateGC(display
, win
->debug
, 0, &l_gcv
);
1986 XSetForeground(display
, l_draw
,
1987 win
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
1989 DRAWSTRING(display
, win
->debug
, bar_fs
, l_draw
, 2,
1990 (bar_fs_extents
->max_logical_extent
.height
-
1991 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
1993 XFreeGC(display
, l_draw
);
1995 draw
= XftDrawCreate(display
, win
->debug
,
1996 DefaultVisual(display
, win
->s
->idx
),
1997 DefaultColormap(display
, win
->s
->idx
));
1999 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
2000 (bar_height
+ bar_font
->height
) / 2 -
2001 bar_font
->descent
, (FcChar8
*)s
, len
);
2003 XftDrawDestroy(draw
);
2007 } else if (win
->debug
!= XCB_WINDOW_NONE
) {
2008 xcb_destroy_window(conn
, win
->debug
);
2009 win
->debug
= XCB_WINDOW_NONE
;
2014 dumpwins(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
2022 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
2028 #endif /* SWM_DEBUG */
2033 int saved_errno
, status
;
2036 saved_errno
= errno
;
2040 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
2045 if (errno
!= ECHILD
)
2046 warn("sighdlr: waitpid");
2047 #endif /* SWM_DEBUG */
2050 if (pid
== searchpid
)
2054 if (WIFEXITED(status
)) {
2055 if (WEXITSTATUS(status
) != 0)
2056 warnx("sighdlr: child exit status: %d",
2057 WEXITSTATUS(status
));
2059 warnx("sighdlr: child is terminated "
2061 #endif /* SWM_DEBUG */
2075 errno
= saved_errno
;
2081 struct pid_e
*p
= NULL
;
2083 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
2088 TAILQ_FOREACH(p
, &pidlist
, entry
) {
2097 name_to_pixel(int sidx
, const char *colorname
)
2099 uint32_t result
= 0;
2100 char cname
[32] = "#";
2101 xcb_screen_t
*screen
;
2102 xcb_colormap_t cmap
;
2103 xcb_alloc_color_reply_t
*cr
;
2104 xcb_alloc_named_color_reply_t
*nr
;
2105 uint16_t rr
, gg
, bb
;
2107 screen
= get_screen(sidx
);
2108 cmap
= screen
->default_colormap
;
2110 /* color is in format rgb://rr/gg/bb */
2111 if (strncmp(colorname
, "rgb:", 4) == 0) {
2112 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
2113 warnx("could not parse rgb %s", colorname
);
2115 cr
= xcb_alloc_color_reply(conn
,
2116 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
2122 warnx("color '%s' not found", colorname
);
2125 nr
= xcb_alloc_named_color_reply(conn
,
2126 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
2129 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
2130 nr
= xcb_alloc_named_color_reply(conn
,
2131 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
2138 warnx("color '%s' not found", colorname
);
2145 setscreencolor(const char *val
, int i
, int c
)
2147 if (i
< 0 || i
>= get_screen_count())
2150 screens
[i
].c
[c
].pixel
= name_to_pixel(i
, val
);
2151 free(screens
[i
].c
[c
].name
);
2152 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
2157 fancy_stacker(struct workspace
*ws
)
2159 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2160 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2161 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2162 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
2163 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
2164 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2165 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2166 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
2167 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
2171 plain_stacker(struct workspace
*ws
)
2173 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2174 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2175 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
2176 sizeof ws
->stacker
);
2177 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2178 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
2179 sizeof ws
->stacker
);
2183 custom_region(const char *val
)
2185 unsigned int x
, y
, w
, h
;
2186 int sidx
, num_screens
;
2187 xcb_screen_t
*screen
;
2189 DNPRINTF(SWM_D_CONF
, "custom_region: %s\n", val
);
2191 num_screens
= get_screen_count();
2192 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
2193 errx(1, "invalid custom region, "
2194 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
2195 if (sidx
< 1 || sidx
> num_screens
)
2196 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
2200 if ((screen
= get_screen(sidx
)) == NULL
)
2201 errx(1, "ERROR: can't get screen %d.", sidx
);
2204 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
2206 if (x
> screen
->width_in_pixels
||
2207 y
> screen
->height_in_pixels
||
2208 w
+ x
> screen
->width_in_pixels
||
2209 h
+ y
> screen
->height_in_pixels
) {
2210 warnx("ignoring region %ux%u+%u+%u - not within screen "
2211 "boundaries (%ux%u)", w
, h
, x
, y
,
2212 screen
->width_in_pixels
, screen
->height_in_pixels
);
2216 new_region(&screens
[sidx
], x
, y
, w
, h
);
2220 socket_setnonblock(int fd
)
2224 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
2225 err(1, "fcntl F_GETFL");
2226 flags
|= O_NONBLOCK
;
2227 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
2228 err(1, "fcntl F_SETFL");
2232 bar_print_legacy(struct swm_region
*r
, const char *s
)
2234 xcb_rectangle_t rect
;
2239 XRectangle ibox
, lbox
;
2243 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
2245 switch (bar_justify
) {
2246 case SWM_BAR_JUSTIFY_LEFT
:
2249 case SWM_BAR_JUSTIFY_CENTER
:
2250 x
= (WIDTH(r
) - lbox
.width
) / 2;
2252 case SWM_BAR_JUSTIFY_RIGHT
:
2253 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
2257 if (x
< SWM_BAR_OFFSET
)
2262 rect
.width
= WIDTH(r
->bar
);
2263 rect
.height
= HEIGHT(r
->bar
);
2265 /* clear back buffer */
2266 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2267 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2268 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2270 /* draw back buffer */
2271 gcvd
.graphics_exposures
= 0;
2272 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
2273 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2274 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
2275 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
2277 XFreeGC(display
, draw
);
2280 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2281 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2285 bar_print(struct swm_region
*r
, const char *s
)
2288 xcb_rectangle_t rect
;
2296 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
2298 switch (bar_justify
) {
2299 case SWM_BAR_JUSTIFY_LEFT
:
2302 case SWM_BAR_JUSTIFY_CENTER
:
2303 x
= (WIDTH(r
) - info
.width
) / 2;
2305 case SWM_BAR_JUSTIFY_RIGHT
:
2306 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
2310 if (x
< SWM_BAR_OFFSET
)
2315 rect
.width
= WIDTH(r
->bar
);
2316 rect
.height
= HEIGHT(r
->bar
);
2318 /* clear back buffer */
2319 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2320 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2321 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2323 /* draw back buffer */
2324 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
2325 DefaultVisual(display
, r
->s
->idx
),
2326 DefaultColormap(display
, r
->s
->idx
));
2328 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
2329 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
2332 XftDrawDestroy(draw
);
2335 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2336 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2340 bar_extra_stop(void)
2344 bzero(bar_pipe
, sizeof bar_pipe
);
2347 kill(bar_pid
, SIGTERM
);
2350 strlcpy(bar_ext
, "", sizeof bar_ext
);
2355 bar_window_class(char *s
, size_t sz
, struct swm_region
*r
)
2357 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2359 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
2360 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
2364 bar_window_instance(char *s
, size_t sz
, struct swm_region
*r
)
2366 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2368 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
2369 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
2373 bar_window_class_instance(char *s
, size_t sz
, struct swm_region
*r
)
2375 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2378 bar_window_class(s
, sz
, r
);
2379 strlcat(s
, ":", sz
);
2380 bar_window_instance(s
, sz
, r
);
2384 bar_window_state(char *s
, size_t sz
, struct swm_region
*r
)
2386 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2388 if (MAXIMIZED(r
->ws
->focus
))
2389 strlcat(s
, "(m)", sz
);
2390 else if (ABOVE(r
->ws
->focus
))
2391 strlcat(s
, "(f)", sz
);
2395 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
2399 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2402 title
= get_win_name(r
->ws
->focus
->id
);
2403 strlcat(s
, title
, sz
);
2408 get_urgent(struct ws_win
*win
)
2410 xcb_icccm_wm_hints_t hints
;
2411 xcb_get_property_cookie_t c
;
2412 bool urgent
= false;
2415 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2416 if (xcb_icccm_get_wm_hints_reply(conn
, c
, &hints
, NULL
))
2417 urgent
= xcb_icccm_wm_hints_get_urgency(&hints
);
2424 bar_urgent(char *s
, size_t sz
)
2427 int i
, j
, num_screens
;
2428 bool urgent
[SWM_WS_MAX
];
2431 for (i
= 0; i
< workspace_limit
; i
++)
2434 num_screens
= get_screen_count();
2435 for (i
= 0; i
< num_screens
; i
++)
2436 for (j
= 0; j
< workspace_limit
; j
++)
2437 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2438 if (get_urgent(win
))
2441 for (i
= 0; i
< workspace_limit
; i
++) {
2443 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2445 } else if (!urgent_collapse
) {
2446 strlcat(s
, "- ", sz
);
2449 if(urgent_collapse
&& s
[0])
2450 s
[strlen(s
) - 1] = 0;
2454 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2456 if (r
== NULL
|| r
->ws
== NULL
)
2458 if (r
->ws
->name
!= NULL
)
2459 strlcat(s
, r
->ws
->name
, sz
);
2462 /* build the default bar format according to the defined enabled options */
2464 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2468 /* if format provided, just copy the buffers */
2469 if (bar_format
!= NULL
) {
2470 strlcpy(fmtnew
, fmtexp
, sz
);
2474 /* reset the output buffer */
2477 strlcat(fmtnew
, "+N:+I ", sz
);
2479 strlcat(fmtnew
, "+S", sz
);
2480 strlcat(fmtnew
, " ", sz
);
2482 /* only show the workspace name if there's actually one */
2483 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2484 strlcat(fmtnew
, "<+D>", sz
);
2486 /* If enabled, only show the iconic count if there are iconic wins. */
2487 if (iconic_enabled
&& r
!= NULL
&& r
->ws
!= NULL
)
2488 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2490 strlcat(fmtnew
, "{+M}", sz
);
2494 strlcat(fmtnew
, "+3<", sz
);
2496 if (clock_enabled
) {
2497 strlcat(fmtnew
, fmtexp
, sz
);
2498 strlcat(fmtnew
, "+4<", sz
);
2501 /* bar_urgent already adds the space before the last asterisk */
2503 strlcat(fmtnew
, (urgent_collapse
? "*+U*+4<" : "* +U*+4<"), sz
);
2505 if (window_class_enabled
) {
2506 strlcat(fmtnew
, "+C", sz
);
2507 if (!window_instance_enabled
)
2508 strlcat(fmtnew
, "+4<", sz
);
2511 /* checks needed by the colon and floating strlcat(3) calls below */
2512 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2513 if (window_instance_enabled
) {
2514 if (window_class_enabled
)
2515 strlcat(fmtnew
, ":", sz
);
2516 strlcat(fmtnew
, "+T+4<", sz
);
2518 if (window_name_enabled
) {
2519 if (ABOVE(r
->ws
->focus
) || MAXIMIZED(r
->ws
->focus
))
2520 strlcat(fmtnew
, "+F ", sz
);
2521 strlcat(fmtnew
, "+64W ", sz
);
2525 /* finally add the action script output and the version */
2526 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2530 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2532 /* special case; no limit given, pad one space, instead */
2533 if (*limit
== (int)sz
- 1)
2535 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2538 /* replaces the bar format character sequences (like in tmux(1)) */
2540 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2545 char tmp
[SWM_BAR_MAX
];
2546 int limit
, size
, count
;
2549 /* reset strlcat(3) buffer */
2552 /* get number, if any */
2555 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2556 limit
= sizeof tmp
- 1;
2557 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2558 limit
= sizeof tmp
- 1;
2560 /* there is nothing to replace (ie EOL) */
2567 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2570 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2573 bar_window_class(tmp
, sizeof tmp
, r
);
2576 bar_workspace_name(tmp
, sizeof tmp
, r
);
2579 bar_window_state(tmp
, sizeof tmp
, r
);
2582 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2586 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2590 snprintf(tmp
, sizeof tmp
, "%d", count
);
2593 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2596 bar_window_class_instance(tmp
, sizeof tmp
, r
);
2599 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2602 bar_window_instance(tmp
, sizeof tmp
, r
);
2605 bar_urgent(tmp
, sizeof tmp
);
2608 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2611 bar_window_name(tmp
, sizeof tmp
, r
);
2614 /* unknown character sequence; copy as-is */
2615 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2621 if ((int)len
< limit
)
2623 while (limit
-- > 0) {
2624 if (*offrep
>= sz
- 1)
2626 fmtrep
[(*offrep
)++] = *ptr
++;
2634 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2639 while (*fmt
!= '\0') {
2641 /* skip ordinary characters */
2644 fmtrep
[off
++] = *fmt
++;
2648 /* character sequence found; replace it */
2649 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2658 bar_fmt_expand(char *fmtexp
, size_t sz
)
2665 /* start by grabbing the current time and date */
2667 localtime_r(&tmt
, &tm
);
2669 /* figure out what to expand */
2670 if (bar_format
!= NULL
)
2672 else if (bar_format
== NULL
&& clock_enabled
)
2674 /* if nothing to expand bail out */
2680 /* copy as-is, just in case the format shouldn't be expanded below */
2681 strlcpy(fmtexp
, fmt
, sz
);
2682 /* finally pass the string through strftime(3) */
2683 #ifndef SWM_DENY_CLOCK_FORMAT
2684 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2685 warnx("format too long");
2690 /* Redraws a region bar; need to follow with xcb_flush() or focus_flush(). */
2692 bar_draw(struct swm_bar
*bar
)
2694 struct swm_region
*r
;
2695 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2696 char fmtrep
[SWM_BAR_MAX
];
2698 /* expand the format by first passing it through strftime(3) */
2699 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2706 if (bar_enabled
&& r
->ws
->bar_enabled
)
2707 xcb_map_window(conn
, bar
->id
);
2709 xcb_unmap_window(conn
, bar
->id
);
2713 if (startup_exception
)
2714 snprintf(fmtrep
, sizeof fmtrep
, "total "
2715 "exceptions: %d, first exception: %s",
2719 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2720 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2723 if (bar_font_legacy
)
2724 bar_print_legacy(r
, fmtrep
);
2726 bar_print(r
, fmtrep
);
2730 * Reads external script output; call when stdin is readable.
2731 * Returns 1 if bar_ext was updated; otherwise 0.
2734 bar_extra_update(void)
2737 char b
[SWM_BAR_MAX
];
2738 bool changed
= false;
2743 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2746 if (b
[len
- 1] == '\n') {
2747 /* Remove newline. */
2750 /* "Clear" bar_ext. */
2753 /* Flush buffered output. */
2754 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2755 bar_ext_buf
[0] = '\0';
2757 /* Append new output to bar. */
2758 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2762 /* Buffer output. */
2763 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2768 if (errno
!= EAGAIN
) {
2769 warn("bar_action failed");
2778 bar_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
2780 struct swm_region
*tmpr
;
2783 /* suppress unused warnings since vars are needed */
2788 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2791 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2792 /* Only change if master switch is enabled. */
2794 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2796 bar_enabled
= r
->ws
->bar_enabled
= true;
2798 case SWM_ARG_ID_BAR_TOGGLE
:
2799 bar_enabled
= !bar_enabled
;
2803 /* update bars as necessary */
2804 num_screens
= get_screen_count();
2805 for (i
= 0; i
< num_screens
; i
++)
2806 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2808 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2809 xcb_map_window(conn
, tmpr
->bar
->id
);
2811 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2814 /* Restack all regions and redraw bar. */
2815 num_screens
= get_screen_count();
2816 for (i
= 0; i
< num_screens
; i
++)
2817 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
2819 bar_draw(tmpr
->bar
);
2826 bar_extra_setup(void)
2828 /* do this here because the conf file is in memory */
2829 if (!bar_extra
&& bar_argv
[0]) {
2830 /* launch external status app */
2832 if (pipe(bar_pipe
) == -1)
2833 err(1, "pipe error");
2834 socket_setnonblock(bar_pipe
[0]);
2835 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2837 /* Set stdin to read from the pipe. */
2838 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2841 /* Set stdout to write to the pipe. */
2842 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2845 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2846 err(1, "could not disable SIGPIPE");
2847 switch (bar_pid
= fork()) {
2849 err(1, "cannot fork");
2853 execvp(bar_argv
[0], bar_argv
);
2854 err(1, "%s external app failed", bar_argv
[0]);
2856 default: /* parent */
2861 atexit(kill_bar_extra_atexit
);
2866 kill_bar_extra_atexit(void)
2869 kill(bar_pid
, SIGTERM
);
2877 while ((s
= index(s
, '-'))) {
2882 return (count
== 14);
2888 char *default_string
;
2889 char **missing_charsets
;
2890 int num_missing_charsets
= 0;
2894 XFreeFontSet(display
, bar_fs
);
2898 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n",
2901 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2902 &num_missing_charsets
, &default_string
);
2904 if (num_missing_charsets
> 0) {
2905 warnx("Unable to load charset(s):");
2907 for (i
= 0; i
< num_missing_charsets
; ++i
)
2908 warnx("%s", missing_charsets
[i
]);
2910 XFreeStringList(missing_charsets
);
2912 if (strcmp(default_string
, ""))
2913 warnx("Glyphs from those sets will be replaced "
2914 "by '%s'.", default_string
);
2916 warnx("Glyphs from those sets won't be drawn.");
2920 errx(1, "Error creating font set structure.");
2922 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2924 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2925 2 * bar_border_width
;
2932 xft_init(struct swm_region
*r
)
2934 char *font
, *str
, *search
;
2937 if (bar_font
== NULL
) {
2938 if ((search
= str
= strdup(bar_fonts
)) == NULL
)
2939 errx(1, "insufficient memory.");
2941 while ((font
= strsep(&search
, ",")) != NULL
) {
2945 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2948 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2951 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2955 if (bar_font
== NULL
) {
2956 warnx("unable to load font %s", font
);
2959 DNPRINTF(SWM_D_INIT
, "successfully opened "
2967 if (bar_font
== NULL
)
2968 errx(1, "unable to open a font");
2970 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2972 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2973 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2974 warn("Xft error: unable to allocate color.");
2976 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR
].pixel
, color
);
2978 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2979 DefaultColormap(display
, r
->s
->idx
), &color
, &search_font_color
))
2980 warn("Xft error: unable to allocate color.");
2982 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2989 bar_setup(struct swm_region
*r
)
2991 xcb_screen_t
*screen
;
2994 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2997 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2998 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
3003 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
3004 err(1, "bar_setup: calloc: failed to allocate memory.");
3006 if (bar_font_legacy
)
3013 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
3014 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
3015 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
3017 /* Assume region is unfocused when we create the bar. */
3018 r
->bar
->id
= xcb_generate_id(conn
);
3019 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
3020 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
3021 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
3022 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
3024 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
3025 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
3026 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
3027 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
3028 | XCB_CW_EVENT_MASK
, wa
);
3030 /* Stack bar window above region window to start. */
3032 wa
[1] = XCB_STACK_MODE_ABOVE
;
3034 xcb_configure_window(conn
, r
->bar
->id
, XCB_CONFIG_WINDOW_SIBLING
|
3035 XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
3037 r
->bar
->buffer
= xcb_generate_id(conn
);
3038 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
3039 WIDTH(r
->bar
), HEIGHT(r
->bar
));
3042 xcb_randr_select_input(conn
, r
->bar
->id
,
3043 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
3046 xcb_map_window(conn
, r
->bar
->id
);
3048 DNPRINTF(SWM_D_BAR
, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
3049 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
3056 bar_cleanup(struct swm_region
*r
)
3060 xcb_destroy_window(conn
, r
->bar
->id
);
3061 xcb_free_pixmap(conn
, r
->bar
->buffer
);
3067 set_win_state(struct ws_win
*win
, uint8_t state
)
3069 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
3071 DNPRINTF(SWM_D_EVENT
, "set_win_state: win %#x, state: %u\n",
3077 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
3078 a_state
, 32, 2, data
);
3082 get_win_state(xcb_window_t w
)
3084 xcb_get_property_reply_t
*r
;
3085 xcb_get_property_cookie_t c
;
3086 uint32_t result
= 0;
3088 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
3089 r
= xcb_get_property_reply(conn
, c
, NULL
);
3091 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
3092 result
= *((uint32_t *)xcb_get_property_value(r
));
3096 DNPRINTF(SWM_D_MISC
, "get_win_state property: win %#x state %u\n", w
,
3102 version(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3104 struct swm_region
*tmpr
;
3107 /* suppress unused warnings since vars are needed */
3112 bar_version
= !bar_version
;
3114 snprintf(bar_vertext
, sizeof bar_vertext
,
3115 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
3117 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
3119 num_screens
= get_screen_count();
3120 for (i
= 0; i
< num_screens
; i
++) {
3121 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
3122 bar_draw(tmpr
->bar
);
3129 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
3131 xcb_client_message_event_t ev
;
3139 name
= get_atom_name(a
);
3140 DNPRINTF(SWM_D_EVENT
, "client_msg: win %#x, atom: %s(%u), "
3142 win
->id
, name
, a
, t
);
3146 bzero(&ev
, sizeof ev
);
3147 ev
.response_type
= XCB_CLIENT_MESSAGE
;
3148 ev
.window
= win
->id
;
3151 ev
.data
.data32
[0] = a
;
3152 ev
.data
.data32
[1] = t
;
3154 xcb_send_event(conn
, 0, win
->id
,
3155 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
3158 /* synthetic response to a ConfigureRequest when not making a change */
3160 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
3162 xcb_configure_notify_event_t ce
;
3167 /* send notification of unchanged state. */
3168 bzero(&ce
, sizeof(ce
));
3169 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
3172 ce
.width
= WIDTH(win
);
3173 ce
.height
= HEIGHT(win
);
3174 ce
.border_width
= 0;
3175 ce
.override_redirect
= 0;
3180 ce
.window
= win
->id
;
3181 ce
.above_sibling
= XCB_WINDOW_NONE
;
3184 ce
.event
= ev
->window
;
3185 ce
.window
= ev
->window
;
3187 /* make response appear more WM_SIZE_HINTS-compliant */
3188 if (win
->sh
.flags
) {
3189 DNPRINTF(SWM_D_MISC
, "config_win: hints: win %#x,"
3190 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
3191 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
3192 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
3193 SH_INC_W(win
), SH_INC_H(win
));
3198 /* the hint may be set... to 0! */
3199 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
3200 ce
.width
= SH_MIN_W(win
);
3201 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
3202 ce
.height
= SH_MIN_H(win
);
3207 /* may also be advertized as 0 */
3208 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
3209 ce
.width
= SH_MAX_W(win
);
3210 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
3211 ce
.height
= SH_MAX_H(win
);
3214 /* resize increment. */
3216 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
3217 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
3219 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
3220 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
3224 /* adjust x and y for requested border_width. */
3225 ce
.x
+= ev
->border_width
;
3226 ce
.y
+= ev
->border_width
;
3228 ce
.above_sibling
= ev
->sibling
;
3231 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
3232 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
3233 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
3235 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
3240 count_win(struct workspace
*ws
, bool count_transient
)
3245 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3246 if (!count_transient
&& FLOATING(win
))
3252 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
3258 quit(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3260 /* suppress unused warnings since vars are needed */
3265 DNPRINTF(SWM_D_MISC
, "quit\n");
3270 lower_window(struct ws_win
*win
)
3272 struct ws_win
*target
= NULL
;
3273 struct workspace
*ws
;
3275 DNPRINTF(SWM_D_EVENT
, "lower_window: win %#x\n", WINID(win
));
3282 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3283 if (target
== win
|| ICONIC(target
))
3285 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3288 if (win
->transient
== target
->transient
)
3290 if (win
->transient
== target
->id
)
3293 if (FULLSCREEN(target
))
3295 if (FULLSCREEN(win
))
3297 if (MAXIMIZED(target
))
3301 if (ABOVE(target
) || TRANS(target
))
3303 if (ABOVE(win
) || TRANS(win
))
3307 /* Change stack position. */
3308 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3310 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3312 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3314 update_win_stacking(win
);
3317 if (swm_debug
& SWM_D_STACK
) {
3318 DPRINTF("=== stacking order (top down) === \n");
3319 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3320 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3321 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3322 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3323 YESNO(ICONIC(target
)));
3327 DNPRINTF(SWM_D_EVENT
, "lower_window: done\n");
3331 raise_window(struct ws_win
*win
)
3333 struct ws_win
*target
= NULL
;
3334 struct workspace
*ws
;
3336 DNPRINTF(SWM_D_EVENT
, "raise_window: win %#x\n", WINID(win
));
3343 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3344 if (target
== win
|| ICONIC(target
))
3346 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3348 if (TRANS(win
) && (win
->transient
== target
->transient
||
3349 win
->transient
== target
->id
))
3351 if (FULLSCREEN(win
))
3353 if (FULLSCREEN(target
))
3357 if (MAXIMIZED(target
))
3359 if (ABOVE(win
) || TRANS(win
) ||
3360 (win
->ws
->focus
== win
&& ws
->always_raise
))
3362 if (!ABOVE(target
) && !TRANS(target
))
3366 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3368 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3370 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3372 update_win_stacking(win
);
3375 if (swm_debug
& SWM_D_STACK
) {
3376 DPRINTF("=== stacking order (top down) === \n");
3377 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3378 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3379 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3380 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3381 YESNO(ICONIC(target
)));
3385 DNPRINTF(SWM_D_EVENT
, "raise_window: done\n");
3389 update_win_stacking(struct ws_win
*win
)
3391 struct ws_win
*sibling
;
3395 struct swm_region
*r
;
3398 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
3401 if (win
->frame
== XCB_WINDOW_NONE
) {
3402 DNPRINTF(SWM_D_EVENT
, "update_window_stacking: win %#x not "
3403 "reparented.\n", win
->id
);
3407 sibling
= TAILQ_NEXT(win
, stack_entry
);
3408 if (sibling
!= NULL
&& (FLOATING(win
) == FLOATING(sibling
) ||
3409 (win
->ws
->always_raise
&& win
->ws
->focus
== win
)))
3410 val
[0] = sibling
->frame
;
3411 else if (FLOATING(win
) || (win
->ws
->always_raise
&&
3412 win
->ws
->focus
== win
))
3413 val
[0] = r
->bar
->id
;
3417 DNPRINTF(SWM_D_EVENT
, "update_win_stacking: win %#x (%#x), "
3418 "sibling %#x\n", win
->frame
, win
->id
, val
[0]);
3420 val
[1] = XCB_STACK_MODE_ABOVE
;
3422 xcb_configure_window(conn
, win
->frame
, XCB_CONFIG_WINDOW_SIBLING
|
3423 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
3426 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
)
3432 map_window(struct ws_win
*win
)
3437 DNPRINTF(SWM_D_EVENT
, "map_window: win %#x, mapped: %s\n",
3438 win
->id
, YESNO(win
->mapped
));
3443 xcb_map_window(conn
, win
->frame
);
3444 xcb_map_window(conn
, win
->id
);
3445 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
3450 unmap_window(struct ws_win
*win
)
3455 DNPRINTF(SWM_D_EVENT
, "unmap_window: win %#x, mapped: %s\n", win
->id
,
3456 YESNO(win
->mapped
));
3461 xcb_unmap_window(conn
, win
->id
);
3462 xcb_unmap_window(conn
, win
->frame
);
3463 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
3464 win
->mapped
= false;
3471 int i
, j
, num_screens
;
3473 num_screens
= get_screen_count();
3474 for (i
= 0; i
< num_screens
; i
++)
3475 for (j
= 0; j
< workspace_limit
; j
++)
3476 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3481 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
3483 xcb_key_press_event_t event
;
3484 xcb_keycode_t
*keycode
;
3489 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
3491 DNPRINTF(SWM_D_MISC
, "fake_keypress: win %#x, keycode %u\n",
3494 bzero(&event
, sizeof(event
));
3495 event
.event
= win
->id
;
3496 event
.root
= win
->s
->root
;
3497 event
.child
= XCB_WINDOW_NONE
;
3498 event
.time
= XCB_CURRENT_TIME
;
3499 event
.event_x
= X(win
);
3500 event
.event_y
= Y(win
);
3503 event
.same_screen
= 1;
3504 event
.detail
= *keycode
;
3505 event
.state
= modifiers
;
3507 event
.response_type
= XCB_KEY_PRESS
;
3508 xcb_send_event(conn
, 1, win
->id
,
3509 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
3511 event
.response_type
= XCB_KEY_RELEASE
;
3512 xcb_send_event(conn
, 1, win
->id
,
3513 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
3519 restart(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3521 /* suppress unused warning since var is needed */
3526 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
3530 execvp(start_argv
[0], start_argv
);
3531 warn("execvp failed");
3532 quit(NULL
, NULL
, NULL
);
3536 get_pointer_win(xcb_window_t root
)
3538 struct ws_win
*win
= NULL
;
3539 xcb_query_pointer_reply_t
*r
;
3541 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: %#x.\n", root
);
3543 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
3545 win
= find_window(r
->child
);
3547 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: %#x.\n",
3550 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
3559 center_pointer(struct swm_region
*r
)
3563 if (!warp_pointer
|| r
== NULL
)
3568 DNPRINTF(SWM_D_EVENT
, "center_pointer: win %#x.\n", WINID(win
));
3570 if (win
&& win
->mapped
)
3571 xcb_warp_pointer(conn
, XCB_NONE
, win
->frame
, 0, 0, 0, 0,
3572 WIDTH(win
) / 2, HEIGHT(win
) / 2);
3574 xcb_warp_pointer(conn
, XCB_NONE
, r
->id
, 0, 0, 0, 0,
3575 WIDTH(r
) / 2, HEIGHT(r
) / 2);
3579 root_to_region(xcb_window_t root
, int check
)
3582 struct swm_region
*r
= NULL
;
3584 xcb_query_pointer_reply_t
*qpr
;
3585 xcb_get_input_focus_reply_t
*gifr
;
3587 DNPRINTF(SWM_D_MISC
, "root_to_region: win %#x\n", root
);
3589 num_screens
= get_screen_count();
3590 for (i
= 0; i
< num_screens
; i
++)
3591 if (screens
[i
].root
== root
)
3594 if (check
& SWM_CK_REGION
)
3595 r
= screens
[i
].r_focus
;
3597 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
3598 /* Try to find an actively focused window */
3599 gifr
= xcb_get_input_focus_reply(conn
,
3600 xcb_get_input_focus(conn
), NULL
);
3602 cfw
= find_window(gifr
->focus
);
3603 if (cfw
&& cfw
->ws
->r
)
3610 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
3611 /* No region with an active focus; try to use pointer. */
3612 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
3613 screens
[i
].root
), NULL
);
3615 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
3616 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
3617 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3618 if (X(r
) <= qpr
->root_x
&&
3619 qpr
->root_x
< MAX_X(r
) &&
3620 Y(r
) <= qpr
->root_y
&&
3621 qpr
->root_y
< MAX_Y(r
))
3628 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
3629 r
= TAILQ_FIRST(&screens
[i
].rl
);
3631 DNPRINTF(SWM_D_MISC
, "root_to_region: idx: %d\n", get_region_index(r
));
3637 find_region(xcb_window_t id
)
3639 struct swm_region
*r
;
3642 num_screens
= get_screen_count();
3643 for (i
= 0; i
< num_screens
; i
++)
3644 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3652 find_bar(xcb_window_t id
)
3654 struct swm_region
*r
;
3657 num_screens
= get_screen_count();
3658 for (i
= 0; i
< num_screens
; i
++)
3659 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3660 if (r
->bar
&& r
->bar
->id
== id
)
3667 find_frame_window(xcb_window_t id
) {
3668 struct swm_region
*r
;
3672 num_screens
= get_screen_count();
3673 for (i
= 0; i
< num_screens
; i
++)
3674 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3675 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
3683 find_window(xcb_window_t id
)
3686 int i
, j
, num_screens
;
3687 xcb_query_tree_reply_t
*qtr
;
3689 num_screens
= get_screen_count();
3690 for (i
= 0; i
< num_screens
; i
++)
3691 for (j
= 0; j
< workspace_limit
; j
++)
3692 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3693 if (id
== win
->id
|| id
== win
->frame
)
3698 /* If window isn't top-level, try to find managed ancestor. */
3699 qtr
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3701 if (qtr
->parent
!= XCB_WINDOW_NONE
&& qtr
->parent
!= qtr
->root
)
3702 win
= find_window(qtr
->parent
);
3706 DNPRINTF(SWM_D_MISC
, "find_window: found child %#x "
3707 "of %#x.\n", win
->id
, qtr
->parent
);
3717 find_unmanaged_window(xcb_window_t id
)
3720 int i
, j
, num_screens
;
3722 num_screens
= get_screen_count();
3723 for (i
= 0; i
< num_screens
; i
++)
3724 for (j
= 0; j
< workspace_limit
; j
++)
3725 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
3733 spawn(int ws_idx
, union arg
*args
, bool close_fd
)
3738 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3740 close(xcb_get_file_descriptor(conn
));
3742 setenv("LD_PRELOAD", SWM_LIB
, 1);
3744 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3745 warn("spawn: asprintf SWM_WS");
3748 setenv("_SWM_WS", ret
, 1);
3752 if (asprintf(&ret
, "%d", getpid()) == -1) {
3753 warn("spawn: asprintf _SWM_PID");
3756 setenv("_SWM_PID", ret
, 1);
3760 if (setsid() == -1) {
3761 warn("spawn: setsid");
3767 * close stdin and stdout to prevent interaction between apps
3768 * and the baraction script
3769 * leave stderr open to record errors
3771 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3772 warn("spawn: open");
3775 dup2(fd
, STDIN_FILENO
);
3776 dup2(fd
, STDOUT_FILENO
);
3781 execvp(args
->argv
[0], args
->argv
);
3783 warn("spawn: execvp");
3788 kill_refs(struct ws_win
*win
)
3790 struct workspace
*ws
;
3792 int i
, j
, num_screens
;
3797 num_screens
= get_screen_count();
3798 for (i
= 0; i
< num_screens
; i
++) {
3799 for (j
= 0; j
< workspace_limit
; j
++) {
3800 ws
= &screens
[i
].ws
[j
];
3802 if (win
== ws
->focus
)
3804 if (win
== ws
->focus_prev
)
3805 ws
->focus_prev
= NULL
;
3806 if (win
== ws
->focus_pending
)
3807 ws
->focus_pending
= NULL
;
3810 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3811 if (win
== w
->focus_child
)
3812 w
->focus_child
= NULL
;
3818 validate_win(struct ws_win
*testwin
)
3821 struct workspace
*ws
;
3822 struct swm_region
*r
;
3823 int i
, x
, num_screens
;
3825 if (testwin
== NULL
)
3828 num_screens
= get_screen_count();
3829 for (i
= 0; i
< num_screens
; i
++)
3830 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3831 for (x
= 0; x
< workspace_limit
; x
++) {
3833 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3841 validate_ws(struct workspace
*testws
)
3843 struct swm_region
*r
;
3844 struct workspace
*ws
;
3845 int i
, x
, num_screens
;
3847 /* validate all ws */
3848 num_screens
= get_screen_count();
3849 for (i
= 0; i
< num_screens
; i
++)
3850 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3851 for (x
= 0; x
< workspace_limit
; x
++) {
3860 unfocus_win(struct ws_win
*win
)
3862 xcb_window_t none
= XCB_WINDOW_NONE
;
3864 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: win %#x\n", WINID(win
));
3869 if (win
->ws
== NULL
) {
3870 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3874 if (validate_ws(win
->ws
)) {
3875 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3879 if (win
->ws
->r
== NULL
) {
3880 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3884 if (validate_win(win
)) {
3885 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3890 if (win
->ws
->focus
== win
) {
3891 win
->ws
->focus
= NULL
;
3892 win
->ws
->focus_prev
= win
;
3895 if (validate_win(win
->ws
->focus
)) {
3896 kill_refs(win
->ws
->focus
);
3897 win
->ws
->focus
= NULL
;
3900 if (validate_win(win
->ws
->focus_prev
)) {
3901 kill_refs(win
->ws
->focus_prev
);
3902 win
->ws
->focus_prev
= NULL
;
3907 /* Raise window to "top unfocused position." */
3908 if (win
->ws
->always_raise
)
3911 /* Update border width */
3912 if (win
->bordered
&& (win
->quirks
& SWM_Q_MINIMALBORDER
) &&
3915 X(win
) += border_width
;
3916 Y(win
) += border_width
;
3920 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3921 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3923 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3927 focus_win(struct ws_win
*win
)
3929 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
, *tmpw
;
3930 struct workspace
*ws
;
3931 xcb_get_input_focus_reply_t
*gifr
;
3933 DNPRINTF(SWM_D_FOCUS
, "focus_win: win %#x\n", WINID(win
));
3935 if (win
== NULL
|| win
->ws
== NULL
|| !win
->mapped
)
3940 if (validate_ws(ws
))
3943 if (validate_win(win
)) {
3948 gifr
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3950 DNPRINTF(SWM_D_FOCUS
, "focus_win: cur focus: %#x\n",
3953 cfw
= find_window(gifr
->focus
);
3954 if (cfw
!= NULL
&& cfw
!= win
) {
3955 if (cfw
->ws
!= ws
&& cfw
->ws
->r
!= NULL
&&
3956 cfw
->frame
!= XCB_WINDOW_NONE
) {
3965 if (ws
->focus
!= win
) {
3966 if (ws
->focus
&& ws
->focus
!= cfw
)
3967 unfocus_win(ws
->focus
);
3971 /* Clear focus child redirect. */
3972 win
->focus_child
= NULL
;
3974 /* If transient, adjust parent's focus child for focus_magic. */
3976 parent
= find_window(win
->transient
);
3977 if (parent
&& parent
->focus_child
!= win
)
3978 parent
->focus_child
= win
;
3981 /* Update window border even if workspace is hidden. */
3985 DNPRINTF(SWM_D_FOCUS
, "focus_win: already focused.\n");
3990 /* Set input focus if no input hint, or indicated by hint. */
3991 if (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
3993 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
3994 " revert-to: parent, time: %#x\n", win
->id
,
3996 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
3997 win
->id
, last_event_time
);
3998 } else if (!win
->take_focus
) {
3999 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
4000 " revert-to: parent, time: 0\n", ws
->r
->id
);
4001 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
4002 ws
->r
->id
, XCB_CURRENT_TIME
);
4005 /* Tell app it can adjust focus to a specific window. */
4006 if (win
->take_focus
) {
4007 /* java is special; always tell parent */
4008 if (TRANS(win
) && win
->java
)
4009 client_msg(parent
, a_takefocus
,
4012 client_msg(win
, a_takefocus
, last_event_time
);
4015 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
4017 /* If a parent exists, map it first. */
4019 raise_window(parent
);
4022 /* Map siblings next. */
4023 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
,
4025 if (w
!= win
&& !ICONIC(w
) &&
4026 w
->transient
== parent
->id
) {
4032 /* Map focused window. */
4036 /* Stack any children of focus window. */
4037 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
4038 if (w
->transient
== win
->id
&& !ICONIC(w
)) {
4042 } else if (tile_gap
< 0 && !ABOVE(win
)) {
4044 * Windows overlap in the layout.
4045 * Raise focused win above all tiled wins.
4053 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4054 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4057 bar_draw(ws
->r
->bar
);
4061 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
4064 /* If a child window should have focus instead, return it. */
4066 get_focus_magic(struct ws_win
*win
)
4068 struct ws_win
*parent
= NULL
;
4069 struct ws_win
*child
= NULL
;
4071 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: win %#x\n", WINID(win
));
4076 parent
= find_window(win
->transient
);
4078 /* If parent prefers focus elsewhere, then try to do so. */
4079 if (parent
&& (child
= parent
->focus_child
)) {
4080 if (validate_win(child
) == 0 && child
->mapped
)
4083 parent
->focus_child
= NULL
;
4087 /* If this window prefers focus elsewhere, then try to do so. */
4088 if ((child
= win
->focus_child
)) {
4089 if (validate_win(child
) == 0 && child
->mapped
)
4092 win
->focus_child
= NULL
;
4099 event_drain(uint8_t rt
)
4101 xcb_generic_event_t
*evt
;
4103 /* ensure all pending requests have been processed before filtering. */
4105 while ((evt
= get_next_event(false))) {
4106 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
4114 set_region(struct swm_region
*r
)
4116 struct swm_region
*rf
;
4123 /* Unfocus old region bar. */
4128 xcb_change_window_attributes(conn
, rf
->bar
->id
,
4129 XCB_CW_BORDER_PIXEL
,
4130 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
4133 if (rf
!= NULL
&& rf
!= r
&& (X(rf
) != X(r
) || Y(rf
) != Y(r
) ||
4134 WIDTH(rf
) != WIDTH(r
) || HEIGHT(rf
) != HEIGHT(r
))) {
4135 /* Set _NET_DESKTOP_GEOMETRY. */
4137 vals
[1] = HEIGHT(r
);
4138 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, r
->s
->root
,
4139 ewmh
[_NET_DESKTOP_GEOMETRY
].atom
, XCB_ATOM_CARDINAL
, 32, 2,
4143 /* Set region bar border to focus_color. */
4144 xcb_change_window_attributes(conn
, r
->bar
->id
,
4145 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
4149 ewmh_update_current_desktop();
4153 focus_region(struct swm_region
*r
)
4156 struct swm_region
*old_r
;
4161 old_r
= r
->s
->r_focus
;
4164 nfw
= get_region_focus(r
);
4168 /* New region is empty; need to manually unfocus win. */
4170 unfocus_win(old_r
->ws
->focus
);
4171 /* Clear bar since empty. */
4172 bar_draw(old_r
->bar
);
4175 DNPRINTF(SWM_D_FOCUS
, "focus_region: set_input_focus: %#x, "
4176 "revert-to: parent, time: 0\n", r
->id
);
4177 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4184 switchws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4186 struct swm_region
*this_r
, *other_r
;
4188 struct workspace
*new_ws
, *old_ws
;
4189 xcb_window_t none
= XCB_WINDOW_NONE
;
4190 int wsid
= args
->id
;
4191 bool unmap_old
= false;
4198 if (wsid
>= workspace_limit
)
4202 old_ws
= this_r
->ws
;
4203 new_ws
= &this_r
->s
->ws
[wsid
];
4205 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
4206 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
4208 if (new_ws
== NULL
|| old_ws
== NULL
)
4210 if (new_ws
== old_ws
)
4213 other_r
= new_ws
->r
;
4214 if (other_r
&& workspace_clamp
) {
4215 DNPRINTF(SWM_D_WS
, "switchws: ws clamped.\n");
4218 DNPRINTF(SWM_D_WS
, "switchws: warping focus to region "
4219 "with ws %d.\n", wsid
);
4220 focus_region(other_r
);
4221 center_pointer(other_r
);
4227 if ((win
= old_ws
->focus
) != NULL
) {
4230 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4231 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4236 /* the other ws is visible in another region, exchange them */
4237 other_r
->ws_prior
= new_ws
;
4238 other_r
->ws
= old_ws
;
4239 old_ws
->r
= other_r
;
4241 /* the other workspace is hidden, hide this one */
4246 this_r
->ws_prior
= old_ws
;
4247 this_r
->ws
= new_ws
;
4250 /* Set focus_pending before stacking, if needed. */
4251 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
4252 validate_win(new_ws
->focus_pending
))) {
4253 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
4254 new_ws
->focus
= new_ws
->focus_prev
;
4255 new_ws
->focus_prev
= NULL
;
4258 new_ws
->state
= SWM_WS_STATE_MAPPING
;
4263 /* unmap old windows */
4265 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
4267 old_ws
->state
= SWM_WS_STATE_HIDDEN
;
4270 /* if workspaces were swapped, then don't wait to set focus */
4271 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
4272 if (new_ws
->focus_pending
) {
4273 focus_win(new_ws
->focus_pending
);
4274 new_ws
->focus_pending
= NULL
;
4278 /* Clear bar and set focus on region input win if new ws is empty. */
4279 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
4280 DNPRINTF(SWM_D_FOCUS
, "switchws: set_input_focus: %#x, "
4281 "revert-to: parent, time: 0\n", r
->id
);
4282 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4287 ewmh_update_current_desktop();
4291 new_ws
->state
= SWM_WS_STATE_MAPPED
;
4293 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
4297 cyclews(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4300 struct swm_screen
*s
= r
->s
;
4301 bool cycle_all
= false, mv
= false;
4303 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4304 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4310 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
4313 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
4316 case SWM_ARG_ID_CYCLEWS_UP
:
4317 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
4319 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
4322 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
4325 case SWM_ARG_ID_CYCLEWS_DOWN
:
4326 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
4333 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
4335 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
4339 send_to_ws(bp
, r
, &a
);
4341 switchws(bp
, r
, &a
);
4342 } while (a
.id
!= r
->ws
->idx
);
4344 DNPRINTF(SWM_D_FOCUS
, "cyclews: done\n");
4348 priorws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4354 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4355 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4357 if (r
->ws_prior
== NULL
)
4360 a
.id
= r
->ws_prior
->idx
;
4361 switchws(bp
, r
, &a
);
4362 DNPRINTF(SWM_D_FOCUS
, "priorws: done\n");
4366 focusrg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4368 int ridx
= args
->id
, i
, num_screens
;
4369 struct swm_region
*rr
= NULL
;
4373 num_screens
= get_screen_count();
4374 /* do nothing if we don't have more than one screen */
4375 if (!(num_screens
> 1 || outputs
> 1))
4378 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
4380 rr
= TAILQ_FIRST(&r
->s
->rl
);
4381 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
4382 rr
= TAILQ_NEXT(rr
, entry
);
4390 DNPRINTF(SWM_D_FOCUS
, "focusrg: done\n");
4394 cyclerg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4397 struct swm_region
*rr
= NULL
;
4400 num_screens
= get_screen_count();
4401 /* do nothing if we don't have more than one screen */
4402 if (!(num_screens
> 1 || outputs
> 1))
4406 DNPRINTF(SWM_D_FOCUS
, "cyclerg: id: %d, region: %d\n", args
->id
, i
);
4409 case SWM_ARG_ID_CYCLERG_UP
:
4410 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4411 rr
= TAILQ_NEXT(r
, entry
);
4413 rr
= TAILQ_FIRST(&screens
[i
].rl
);
4415 case SWM_ARG_ID_CYCLERG_DOWN
:
4416 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4417 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
4419 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
4428 case SWM_ARG_ID_CYCLERG_UP
:
4429 case SWM_ARG_ID_CYCLERG_DOWN
:
4434 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4435 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4437 switchws(bp
, r
, &a
);
4443 DNPRINTF(SWM_D_FOCUS
, "cyclerg: done\n");
4446 /* Sorts transients after parent. */
4448 sort_windows(struct ws_win_list
*wl
)
4450 struct ws_win
*win
, *parent
, *nxt
;
4455 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
4456 nxt
= TAILQ_NEXT(win
, entry
);
4458 parent
= find_window(win
->transient
);
4459 if (parent
== NULL
) {
4460 warnx("not possible bug");
4463 TAILQ_REMOVE(wl
, win
, entry
);
4464 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
4470 swapwin(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4472 struct ws_win
*target
, *source
;
4473 struct ws_win
*cur_focus
;
4474 struct ws_win_list
*wl
;
4478 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4479 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4481 cur_focus
= r
->ws
->focus
;
4482 if (cur_focus
== NULL
|| FULLSCREEN(cur_focus
))
4485 /* Adjust stacking in floating layer. */
4486 if (ABOVE(cur_focus
)) {
4488 case SWM_ARG_ID_SWAPPREV
:
4489 target
= TAILQ_PREV(cur_focus
, ws_win_stack
,
4491 if (target
!= NULL
&& FLOATING(target
)) {
4492 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4494 TAILQ_INSERT_BEFORE(target
, cur_focus
,
4496 update_win_stacking(cur_focus
);
4500 case SWM_ARG_ID_SWAPNEXT
:
4501 target
= TAILQ_NEXT(cur_focus
, stack_entry
);
4502 if (target
!= NULL
&& FLOATING(target
)) {
4503 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4505 TAILQ_INSERT_AFTER(&cur_focus
->ws
->stack
,
4506 target
, cur_focus
, stack_entry
);
4507 update_win_stacking(cur_focus
);
4515 if (r
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4518 clear_maximized(r
->ws
);
4521 wl
= &source
->ws
->winlist
;
4524 case SWM_ARG_ID_SWAPPREV
:
4526 source
= find_window(source
->transient
);
4527 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
4528 if (target
&& target
->transient
)
4529 target
= find_window(target
->transient
);
4530 TAILQ_REMOVE(wl
, source
, entry
);
4532 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4534 TAILQ_INSERT_BEFORE(target
, source
, entry
);
4536 case SWM_ARG_ID_SWAPNEXT
:
4537 target
= TAILQ_NEXT(source
, entry
);
4538 /* move the parent and let the sort handle the move */
4540 source
= find_window(source
->transient
);
4541 TAILQ_REMOVE(wl
, source
, entry
);
4543 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4545 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
4547 case SWM_ARG_ID_SWAPMAIN
:
4548 target
= TAILQ_FIRST(wl
);
4549 if (target
== source
) {
4550 if (source
->ws
->focus_prev
!= NULL
&&
4551 source
->ws
->focus_prev
!= target
)
4552 source
= source
->ws
->focus_prev
;
4556 if (target
== NULL
|| source
== NULL
)
4558 source
->ws
->focus_prev
= target
;
4559 TAILQ_REMOVE(wl
, target
, entry
);
4560 TAILQ_INSERT_BEFORE(source
, target
, entry
);
4561 TAILQ_REMOVE(wl
, source
, entry
);
4562 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4564 case SWM_ARG_ID_MOVELAST
:
4565 TAILQ_REMOVE(wl
, source
, entry
);
4566 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4569 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
4574 ewmh_update_client_list();
4580 DNPRINTF(SWM_D_MOVE
, "swapwin: done\n");
4584 get_focus_prev(struct ws_win
*win
)
4586 struct ws_win
*winfocus
= NULL
;
4587 struct ws_win
*cur_focus
= NULL
;
4588 struct ws_win_list
*wl
= NULL
;
4589 struct workspace
*ws
= NULL
;
4591 if (!(win
&& win
->ws
))
4596 cur_focus
= ws
->focus
;
4598 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: win %#x, cur_focus: %#x, "
4599 "focus_prev: %#x\n", WINID(win
), WINID(cur_focus
),
4600 WINID(ws
->focus_prev
));
4602 /* pickle, just focus on whatever */
4603 if (cur_focus
== NULL
) {
4604 /* use prev_focus if valid */
4605 if (ws
->focus_prev
&& find_window(ws
->focus_prev
->id
))
4606 winfocus
= ws
->focus_prev
;
4610 /* if transient focus on parent */
4611 if (TRANS(cur_focus
)) {
4612 winfocus
= find_window(cur_focus
->transient
);
4616 /* if in max_stack try harder */
4617 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
4618 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
4619 if (cur_focus
!= ws
->focus_prev
)
4620 winfocus
= ws
->focus_prev
;
4622 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
4627 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
4629 if (winfocus
== NULL
|| winfocus
== win
) {
4630 switch (focus_close
) {
4631 case SWM_STACK_BOTTOM
:
4632 TAILQ_FOREACH(winfocus
, wl
, entry
)
4633 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4637 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4638 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4641 case SWM_STACK_ABOVE
:
4642 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
4643 while (winfocus
&& ICONIC(winfocus
))
4644 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4646 if (winfocus
== NULL
) {
4647 if (focus_close_wrap
) {
4648 TAILQ_FOREACH(winfocus
, wl
, entry
)
4649 if (!ICONIC(winfocus
) &&
4650 winfocus
!= cur_focus
)
4653 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4655 if (!ICONIC(winfocus
) &&
4656 winfocus
!= cur_focus
)
4661 case SWM_STACK_BELOW
:
4662 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
4663 while (winfocus
&& ICONIC(winfocus
))
4664 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
4667 if (winfocus
== NULL
) {
4668 if (focus_close_wrap
) {
4669 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4671 if (!ICONIC(winfocus
) &&
4672 winfocus
!= cur_focus
)
4675 TAILQ_FOREACH(winfocus
, wl
, entry
)
4676 if (!ICONIC(winfocus
) &&
4677 winfocus
!= cur_focus
)
4685 if (winfocus
== NULL
||
4686 (winfocus
&& (ICONIC(winfocus
) || winfocus
== cur_focus
))) {
4687 if (focus_default
== SWM_STACK_TOP
) {
4688 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4689 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4692 TAILQ_FOREACH(winfocus
, wl
, entry
)
4693 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4700 return get_focus_magic(winfocus
);
4704 get_region_focus(struct swm_region
*r
)
4706 struct ws_win
*winfocus
= NULL
;
4711 if (r
->ws
->focus
&& !ICONIC(r
->ws
->focus
))
4712 winfocus
= r
->ws
->focus
;
4713 else if (r
->ws
->focus_prev
&& !ICONIC(r
->ws
->focus_prev
))
4714 winfocus
= r
->ws
->focus_prev
;
4716 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
4717 if (!ICONIC(winfocus
))
4720 return get_focus_magic(winfocus
);
4724 focus(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4726 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
4727 struct ws_win_list
*wl
= NULL
;
4728 struct workspace
*ws
= NULL
;
4735 cur_focus
= r
->ws
->focus
;
4739 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d, cur_focus: %#x\n", args
->id
,
4742 /* Make sure an uniconified window has focus, if one exists. */
4743 if (cur_focus
== NULL
) {
4744 cur_focus
= TAILQ_FIRST(wl
);
4745 while (cur_focus
!= NULL
&& ICONIC(cur_focus
))
4746 cur_focus
= TAILQ_NEXT(cur_focus
, entry
);
4748 DNPRINTF(SWM_D_FOCUS
, "focus: new cur_focus: %#x\n",
4753 case SWM_ARG_ID_FOCUSPREV
:
4754 if (cur_focus
== NULL
)
4757 winfocus
= cur_focus
;
4759 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
4760 if (winfocus
== NULL
)
4761 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
4762 if (winfocus
== cur_focus
)
4764 } while (winfocus
&& (ICONIC(winfocus
) ||
4765 winfocus
->id
== cur_focus
->transient
||
4766 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4767 winfocus
->transient
== cur_focus
->transient
) ||
4768 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4770 case SWM_ARG_ID_FOCUSNEXT
:
4771 if (cur_focus
== NULL
)
4774 winfocus
= cur_focus
;
4776 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4777 if (winfocus
== NULL
)
4778 winfocus
= TAILQ_FIRST(wl
);
4779 if (winfocus
== cur_focus
)
4781 } while (winfocus
&& (ICONIC(winfocus
) ||
4782 winfocus
->id
== cur_focus
->transient
||
4783 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4784 winfocus
->transient
== cur_focus
->transient
) ||
4785 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4787 case SWM_ARG_ID_FOCUSMAIN
:
4788 if (cur_focus
== NULL
)
4791 winfocus
= TAILQ_FIRST(wl
);
4792 if (winfocus
== cur_focus
)
4793 winfocus
= cur_focus
->ws
->focus_prev
;
4795 case SWM_ARG_ID_FOCUSURGENT
:
4796 /* Search forward for the next urgent window. */
4800 for (i
= 0; i
<= workspace_limit
; ++i
) {
4802 head
= TAILQ_FIRST(&r
->s
->ws
[(ws
->idx
+ i
) %
4803 workspace_limit
].winlist
);
4806 if (head
== cur_focus
) {
4808 winfocus
= cur_focus
;
4811 } else if (get_urgent(head
)) {
4816 head
= TAILQ_NEXT(head
, entry
);
4823 /* Switch ws if new focus is on a different ws. */
4824 if (winfocus
&& winfocus
->ws
!= ws
) {
4825 a
.id
= winfocus
->ws
->idx
;
4826 switchws(bp
, r
, &a
);
4833 if (clear_maximized(ws
) > 0)
4836 focus_win(get_focus_magic(winfocus
));
4841 DNPRINTF(SWM_D_FOCUS
, "focus: done\n");
4845 focus_pointer(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4849 /* Not needed for buttons since this is already done in buttonpress. */
4850 if (bp
->type
== KEYBIND
) {
4851 focus_win(get_pointer_win(r
->s
->root
));
4857 cycle_layout(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4859 struct workspace
*ws
= r
->ws
;
4861 /* suppress unused warning since var is needed */
4865 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
4868 if (ws
->cur_layout
->l_stack
== NULL
)
4869 ws
->cur_layout
= &layouts
[0];
4871 clear_maximized(ws
);
4876 focus_win(get_region_focus(r
));
4883 stack_config(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4885 struct workspace
*ws
= r
->ws
;
4889 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
4892 if (clear_maximized(ws
) > 0)
4895 if (ws
->cur_layout
->l_config
!= NULL
)
4896 ws
->cur_layout
->l_config(ws
, args
->id
);
4898 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
4907 stack(struct swm_region
*r
) {
4908 struct swm_geometry g
;
4909 struct swm_region
*r_prev
;
4915 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
4917 /* Adjust stack area for region bar and padding. */
4919 g
.x
+= region_padding
;
4920 g
.y
+= region_padding
;
4921 g
.w
-= 2 * border_width
+ 2 * region_padding
;
4922 g
.h
-= 2 * border_width
+ 2 * region_padding
;
4923 if (bar_enabled
&& r
->ws
->bar_enabled
) {
4929 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d (screen: "
4930 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4931 r
->ws
->idx
, r
->s
->idx
, get_region_index(r
), g
.x
, g
.y
, g
.w
, g
.h
);
4933 r_prev
= TAILQ_PREV(r
, swm_region_list
, entry
);
4935 /* Stack bar/input relative to prev. region. */
4936 val
[1] = XCB_STACK_MODE_ABOVE
;
4938 val
[0] = r_prev
->id
;
4939 DNPRINTF(SWM_D_STACK
, "stack: region input %#x "
4940 "relative to %#x.\n", r
->id
, val
[0]);
4941 xcb_configure_window(conn
, r
->id
,
4942 XCB_CONFIG_WINDOW_SIBLING
|
4943 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4945 val
[0] = r_prev
->bar
->id
;
4946 DNPRINTF(SWM_D_STACK
, "stack: region bar %#x "
4947 "relative to %#x.\n", r
->bar
->id
, val
[0]);
4948 xcb_configure_window(conn
, r
->bar
->id
,
4949 XCB_CONFIG_WINDOW_SIBLING
|
4950 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4953 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
4954 r
->ws
->cur_layout
->l_string(r
->ws
);
4955 /* save r so we can track region changes */
4961 DNPRINTF(SWM_D_STACK
, "stack: end\n");
4965 store_float_geom(struct ws_win
*win
)
4967 if (win
== NULL
|| win
->ws
->r
== NULL
)
4970 /* retain window geom and region geom */
4971 win
->g_float
= win
->g
;
4972 win
->g_float
.x
-= X(win
->ws
->r
);
4973 win
->g_float
.y
-= Y(win
->ws
->r
);
4974 win
->g_floatvalid
= true;
4975 DNPRINTF(SWM_D_MISC
, "store_float_geom: win %#x, g: (%d,%d)"
4976 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
4977 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
4978 win
->g_float
.w
, win
->g_float
.h
);
4982 load_float_geom(struct ws_win
*win
)
4984 if (win
== NULL
|| win
->ws
->r
== NULL
)
4987 if (win
->g_floatvalid
) {
4988 win
->g
= win
->g_float
;
4989 X(win
) += X(win
->ws
->r
);
4990 Y(win
) += Y(win
->ws
->r
);
4991 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g: (%d,%d)"
4992 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
4995 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g_float "
4996 "is not set.\n", win
->id
);
5001 update_floater(struct ws_win
*win
)
5003 struct workspace
*ws
;
5004 struct swm_region
*r
;
5006 DNPRINTF(SWM_D_MISC
, "update_floater: win %#x\n", WINID(win
));
5013 if ((r
= ws
->r
) == NULL
)
5016 win
->bordered
= true;
5018 if (FULLSCREEN(win
)) {
5019 /* _NET_WM_FULLSCREEN: fullscreen without border. */
5020 if (!win
->g_floatvalid
)
5021 store_float_geom(win
);
5024 win
->bordered
= false;
5025 } else if (MAXIMIZED(win
)) {
5026 /* Maximize: like a single stacked window. */
5027 if (!win
->g_floatvalid
)
5028 store_float_geom(win
);
5032 if (bar_enabled
&& ws
->bar_enabled
&& !maximize_hide_bar
) {
5034 Y(win
) += bar_height
;
5035 HEIGHT(win
) -= bar_height
;
5036 } else if (disable_border
) {
5037 win
->bordered
= false;
5040 if (win
->bordered
) {
5041 /* Window geometry excludes frame. */
5042 X(win
) += border_width
;
5043 Y(win
) += border_width
;
5044 HEIGHT(win
) -= 2 * border_width
;
5045 WIDTH(win
) -= 2 * border_width
;
5048 /* Normal floating window. */
5049 /* Update geometry if new region. */
5051 load_float_geom(win
);
5053 if (((win
->quirks
& SWM_Q_FULLSCREEN
) &&
5054 WIDTH(win
) >= WIDTH(r
) && HEIGHT(win
) >= HEIGHT(r
)) ||
5055 ((!WS_FOCUSED(win
->ws
) || win
->ws
->focus
!= win
) &&
5056 (win
->quirks
& SWM_Q_MINIMALBORDER
))) {
5058 win
->bordered
= false;
5059 } else if (!MANUAL(win
)) {
5060 if (TRANS(win
) && (win
->quirks
& SWM_Q_TRANSSZ
)) {
5061 /* Adjust size on TRANSSZ quirk. */
5062 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
5063 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
5066 if (!(win
->quirks
& SWM_Q_ANYWHERE
)) {
5068 * Floaters and transients are auto-centred
5069 * unless manually moved, resized or ANYWHERE
5072 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2;
5073 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2;
5074 store_float_geom(win
);
5079 /* Ensure at least 1 pixel of the window is in the region. */
5080 region_containment(win
, r
, SWM_CW_ALLSIDES
);
5086 * Send keystrokes to terminal to decrease/increase the font size as the
5087 * window size changes.
5090 adjust_font(struct ws_win
*win
)
5092 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
5093 ABOVE(win
) || TRANS(win
))
5096 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
5097 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
5098 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
5099 win
->font_size_boundary
[win
->font_steps
] =
5100 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
5103 win
->last_inc
= win
->sh
.width_inc
;
5104 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
5105 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
5106 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
5109 win
->last_inc
= win
->sh
.width_inc
;
5110 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
5114 #define SWAPXY(g) do { \
5116 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
5117 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
5120 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, bool flip
)
5122 struct swm_geometry cell
, r_g
= *g
;
5124 int i
= 0, j
= 0, s
= 0, stacks
= 0;
5125 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
5126 int hrh
= 0, extra
= 0, h_slice
= 0, last_h
= 0;
5127 int split
= 0, colno
= 0;
5128 int winno
, mwin
= 0, msize
= 0;
5129 int remain
, missing
, v_slice
, mscale
;
5130 bool bordered
= true, reconfigure
= false;
5133 * cell: geometry for window, including frame.
5134 * mwin: # of windows in master area.
5135 * mscale: size increment of master area.
5136 * stacks: # of stack columns
5139 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
5140 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
5142 /* Prepare tiling variables, if needed. */
5143 if ((winno
= count_win(ws
, false)) > 0) {
5144 /* Find first tiled window. */
5145 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
5146 if (!FLOATING(win
) && !ICONIC(win
))
5149 /* Take into account size hints of first tiled window. */
5151 w_inc
= win
->sh
.width_inc
;
5152 w_base
= win
->sh
.base_width
;
5153 mwin
= ws
->l_state
.horizontal_mwin
;
5154 mscale
= ws
->l_state
.horizontal_msize
;
5155 stacks
= ws
->l_state
.horizontal_stacks
;
5158 w_inc
= win
->sh
.height_inc
;
5159 w_base
= win
->sh
.base_height
;
5160 mwin
= ws
->l_state
.vertical_mwin
;
5161 mscale
= ws
->l_state
.vertical_msize
;
5162 stacks
= ws
->l_state
.vertical_stacks
;
5166 cell
.x
+= border_width
;
5167 cell
.y
+= border_width
;
5169 if (stacks
> winno
- mwin
)
5170 stacks
= winno
- mwin
;
5174 h_slice
= r_g
.h
/ SWM_H_SLICE
;
5175 if (mwin
&& winno
> mwin
) {
5176 v_slice
= r_g
.w
/ SWM_V_SLICE
;
5180 cell
.w
= v_slice
* mscale
;
5182 if (w_inc
> 1 && w_inc
< v_slice
) {
5183 /* Adjust for requested size increment. */
5184 remain
= (cell
.w
- w_base
) % w_inc
;
5190 cell
.x
+= r_g
.w
- msize
;
5193 colno
= split
= winno
/ stacks
;
5194 cell
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
5195 2 * border_width
) / stacks
);
5198 hrh
= r_g
.h
/ colno
;
5199 extra
= r_g
.h
- (colno
* hrh
);
5200 cell
.h
= hrh
- 2 * border_width
;
5201 i
= j
= 0, s
= stacks
;
5204 /* Update window geometry. */
5205 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
5209 if (FLOATING(win
)) {
5210 update_floater(win
);
5215 if (split
&& i
== split
) {
5216 colno
= (winno
- mwin
) / stacks
;
5217 if (s
<= (winno
- mwin
) % stacks
)
5220 hrh
= r_g
.h
/ colno
;
5221 extra
= r_g
.h
- (colno
* hrh
);
5224 cell
.x
+= cell
.w
+ 2 * border_width
+
5227 cell
.w
= (r_g
.w
- msize
-
5228 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
5230 cell
.w
+= (r_g
.w
- msize
-
5231 (stacks
* (2 * border_width
+ tile_gap
))) %
5235 cell
.x
-= cell
.w
+ 2 * border_width
+
5241 cell
.h
= hrh
- 2 * border_width
- tile_gap
;
5244 h_inc
= win
->sh
.width_inc
;
5245 h_base
= win
->sh
.base_width
;
5247 h_inc
= win
->sh
.height_inc
;
5248 h_base
= win
->sh
.base_height
;
5251 if (j
== colno
- 1) {
5252 cell
.h
= hrh
+ extra
;
5253 } else if (h_inc
> 1 && h_inc
< h_slice
) {
5254 /* adjust for window's requested size increment */
5255 remain
= (cell
.h
- h_base
) % h_inc
;
5256 missing
= h_inc
- remain
;
5258 if (missing
<= extra
|| j
== 0) {
5268 cell
.y
= r_g
.y
+ border_width
;
5270 cell
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
5272 /* Window coordinates exclude frame. */
5274 if (winno
> 1 || !disable_border
||
5275 (bar_enabled
&& ws
->bar_enabled
)) {
5282 if (X(win
) != cell
.y
|| Y(win
) != cell
.x
||
5283 WIDTH(win
) != cell
.h
|| HEIGHT(win
) != cell
.w
) {
5287 WIDTH(win
) = cell
.h
;
5288 HEIGHT(win
) = cell
.w
;
5291 if (X(win
) != cell
.x
|| Y(win
) != cell
.y
||
5292 WIDTH(win
) != cell
.w
|| HEIGHT(win
) != cell
.h
) {
5296 WIDTH(win
) = cell
.w
;
5297 HEIGHT(win
) = cell
.h
;
5302 X(win
) -= border_width
;
5303 Y(win
) -= border_width
;
5304 WIDTH(win
) += 2 * border_width
;
5305 HEIGHT(win
) += 2 * border_width
;
5308 if (bordered
!= win
->bordered
) {
5310 win
->bordered
= bordered
;
5323 /* Stack all windows from bottom up. */
5324 TAILQ_FOREACH_REVERSE(win
, &ws
->stack
, ws_win_stack
, stack_entry
)
5326 update_win_stacking(win
);
5328 /* Map all windows from top down. */
5329 TAILQ_FOREACH(win
, &ws
->stack
, stack_entry
)
5333 DNPRINTF(SWM_D_STACK
, "stack_master: done\n");
5337 vertical_config(struct workspace
*ws
, int id
)
5339 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
5343 case SWM_ARG_ID_STACKRESET
:
5344 case SWM_ARG_ID_STACKINIT
:
5345 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
5346 ws
->l_state
.vertical_mwin
= 1;
5347 ws
->l_state
.vertical_stacks
= 1;
5349 case SWM_ARG_ID_MASTERSHRINK
:
5350 if (ws
->l_state
.vertical_msize
> 1)
5351 ws
->l_state
.vertical_msize
--;
5353 case SWM_ARG_ID_MASTERGROW
:
5354 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
5355 ws
->l_state
.vertical_msize
++;
5357 case SWM_ARG_ID_MASTERADD
:
5358 ws
->l_state
.vertical_mwin
++;
5360 case SWM_ARG_ID_MASTERDEL
:
5361 if (ws
->l_state
.vertical_mwin
> 0)
5362 ws
->l_state
.vertical_mwin
--;
5364 case SWM_ARG_ID_STACKBALANCE
:
5365 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ (ws
->l_state
.vertical_stacks
+ 1);
5367 case SWM_ARG_ID_STACKINC
:
5368 ws
->l_state
.vertical_stacks
++;
5370 case SWM_ARG_ID_STACKDEC
:
5371 if (ws
->l_state
.vertical_stacks
> 1)
5372 ws
->l_state
.vertical_stacks
--;
5374 case SWM_ARG_ID_FLIPLAYOUT
:
5375 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
5383 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5385 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
5387 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
5391 horizontal_config(struct workspace
*ws
, int id
)
5393 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
5396 case SWM_ARG_ID_STACKRESET
:
5397 case SWM_ARG_ID_STACKINIT
:
5398 ws
->l_state
.horizontal_mwin
= 1;
5399 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
5400 ws
->l_state
.horizontal_stacks
= 1;
5402 case SWM_ARG_ID_MASTERSHRINK
:
5403 if (ws
->l_state
.horizontal_msize
> 1)
5404 ws
->l_state
.horizontal_msize
--;
5406 case SWM_ARG_ID_MASTERGROW
:
5407 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
5408 ws
->l_state
.horizontal_msize
++;
5410 case SWM_ARG_ID_MASTERADD
:
5411 ws
->l_state
.horizontal_mwin
++;
5413 case SWM_ARG_ID_MASTERDEL
:
5414 if (ws
->l_state
.horizontal_mwin
> 0)
5415 ws
->l_state
.horizontal_mwin
--;
5417 case SWM_ARG_ID_STACKBALANCE
:
5418 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ (ws
->l_state
.horizontal_stacks
+ 1);
5420 case SWM_ARG_ID_STACKINC
:
5421 ws
->l_state
.horizontal_stacks
++;
5423 case SWM_ARG_ID_STACKDEC
:
5424 if (ws
->l_state
.horizontal_stacks
> 1)
5425 ws
->l_state
.horizontal_stacks
--;
5427 case SWM_ARG_ID_FLIPLAYOUT
:
5428 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
5436 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5438 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
5440 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
5443 /* fullscreen view */
5445 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5447 struct swm_geometry gg
= *g
;
5448 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
, *tmpw
;
5451 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
5456 winno
= count_win(ws
, false);
5457 if (winno
== 0 && count_win(ws
, true) == 0)
5460 /* Figure out which top level window should be visible. */
5461 if (ws
->focus_pending
)
5462 win
= ws
->focus_pending
;
5465 else if (ws
->focus_prev
)
5466 win
= ws
->focus_prev
;
5468 win
= TAILQ_FIRST(&ws
->winlist
);
5470 DNPRINTF(SWM_D_STACK
, "max_stack: focus_pending: %#x, focus: %#x, "
5471 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws
->focus_pending
),
5472 WINID(ws
->focus
), WINID(ws
->focus_prev
),
5473 WINID(TAILQ_FIRST(&ws
->winlist
)), win
->id
);
5475 /* Update window geometry. */
5476 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5485 /* Set maximized flag for all maxed windows. */
5486 if (!MAXIMIZED(w
)) {
5487 /* Preserve floating geometry. */
5489 store_float_geom(w
);
5491 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_MAXIMIZED
);
5492 ewmh_update_wm_state(w
);
5495 /* Only reconfigure if necessary. */
5496 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
5497 HEIGHT(w
) != gg
.h
) {
5500 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
)) {
5501 w
->bordered
= false;
5502 WIDTH(w
) += 2 * border_width
;
5503 HEIGHT(w
) += 2 * border_width
;
5506 X(w
) += border_width
;
5507 Y(w
) += border_width
;
5514 /* If transient, stack parent and its children. */
5515 if (TRANS(win
) && (parent
= find_window(win
->transient
))) {
5516 raise_window(parent
);
5518 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5519 if (w
->transient
== parent
->id
)
5523 /* Make sure focus window is on top. */
5526 /* Stack any children of focus window. */
5527 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5528 if (w
->transient
== win
->id
)
5531 /* Map all windows. */
5532 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
5538 send_to_rg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5540 int ridx
= args
->id
, i
, num_screens
;
5541 struct swm_region
*rr
= NULL
;
5544 num_screens
= get_screen_count();
5545 /* do nothing if we don't have more than one screen */
5546 if (!(num_screens
> 1 || outputs
> 1))
5549 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
5551 rr
= TAILQ_FIRST(&r
->s
->rl
);
5552 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
5553 rr
= TAILQ_NEXT(rr
, entry
);
5560 send_to_ws(bp
, r
, &a
);
5564 region_under(struct swm_screen
*s
, int x
, int y
)
5566 struct swm_region
*r
= NULL
;
5568 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
5569 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
5570 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
5571 WIDTH(r
), HEIGHT(r
), x
, y
);
5572 if (X(r
) <= x
&& x
< MAX_X(r
))
5573 if (Y(r
) <= y
&& y
< MAX_Y(r
))
5580 /* Transfer focused window to target workspace and focus. */
5582 send_to_ws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5584 int wsid
= args
->id
;
5585 struct ws_win
*win
= NULL
;
5589 if (r
&& r
->ws
&& r
->ws
->focus
)
5594 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win %#x, ws %d\n", win
->id
, wsid
);
5596 if (wsid
< 0 || wsid
>= workspace_limit
)
5599 if (win
->ws
->idx
== wsid
)
5602 win_to_ws(win
, wsid
, true);
5604 /* Set new focus on target ws. */
5605 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5606 win
->ws
->focus_prev
= win
->ws
->focus
;
5607 win
->ws
->focus
= win
;
5608 win
->ws
->focus_pending
= NULL
;
5610 if (win
->ws
->focus_prev
)
5611 draw_frame(win
->ws
->focus_prev
);
5614 DNPRINTF(SWM_D_STACK
, "send_to_ws: focus_pending: %#x, focus: %#x, "
5615 "focus_prev: %#x, first: %#x, win: %#x\n",
5616 WINID(r
->ws
->focus_pending
), WINID(r
->ws
->focus
),
5617 WINID(r
->ws
->focus_prev
), WINID(TAILQ_FIRST(&r
->ws
->winlist
)),
5620 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5621 ewmh_update_wm_state(win
);
5623 /* Restack focused region. */
5626 /* If destination ws has a region, restack. */
5629 load_float_geom(win
);
5634 /* Set new focus on current ws. */
5635 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5636 if (r
->ws
->focus
!= NULL
) {
5637 focus_win(r
->ws
->focus
);
5639 DNPRINTF(SWM_D_FOCUS
, "send_to_ws: set_input_focus: "
5640 "%#x, revert-to: parent, time: 0\n", r
->id
);
5641 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
5642 r
->id
, XCB_CURRENT_TIME
);
5652 win_to_ws(struct ws_win
*win
, int wsid
, bool unfocus
)
5654 struct ws_win
*parent
;
5655 struct workspace
*ws
, *nws
, *pws
;
5657 if (wsid
< 0 || wsid
>= workspace_limit
)
5660 if (win
->ws
->idx
== wsid
)
5664 nws
= &win
->s
->ws
[wsid
];
5666 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win %#x, ws %d -> %d\n", win
->id
,
5669 /* Cleanup focus on source ws. */
5670 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
5671 (ws
->focus
== win
|| ws
->focus_pending
== win
))
5672 ws
->focus_pending
= get_focus_prev(win
);
5674 /* Move the parent if this is a transient window. */
5676 parent
= find_window(win
->transient
);
5679 /* Set new focus in parent's ws if needed. */
5680 if (pws
->focus
== parent
) {
5681 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5682 pws
->focus_pending
=
5683 get_focus_prev(parent
);
5685 unfocus_win(parent
);
5687 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5688 pws
->focus
= pws
->focus_pending
;
5689 pws
->focus_pending
= NULL
;
5693 /* Don't unmap parent if new ws is visible */
5695 unmap_window(parent
);
5698 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
5699 TAILQ_REMOVE(&ws
->stack
, parent
, stack_entry
);
5700 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
5701 TAILQ_INSERT_TAIL(&nws
->stack
, parent
, stack_entry
);
5704 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5705 "_NET_WM_DESKTOP: %d\n", wsid
);
5706 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5707 parent
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
5708 XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5715 if (ws
->focus_prev
== win
)
5716 ws
->focus_prev
= NULL
;
5718 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& ws
->focus_pending
!= NULL
) {
5719 ws
->focus
= ws
->focus_pending
;
5720 ws
->focus_pending
= NULL
;
5723 /* Don't unmap if new ws is visible */
5728 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
5729 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
5730 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
5731 TAILQ_INSERT_TAIL(&nws
->stack
, win
, stack_entry
);
5734 /* Update the window's workspace property: _NET_WM_DESKTOP */
5735 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5736 "_NET_WM_DESKTOP: %d\n", wsid
);
5737 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
5738 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5740 ewmh_update_client_list();
5742 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
5746 pressbutton(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5748 /* suppress unused warning since var is needed */
5752 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
5753 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5754 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
5755 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5759 raise_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5761 /* Suppress warning. */
5765 if (r
== NULL
|| r
->ws
== NULL
)
5768 if (r
->ws
->focus
&& MAXIMIZED(r
->ws
->focus
))
5771 r
->ws
->always_raise
= !r
->ws
->always_raise
;
5773 /* Update focused win stacking order based on new always_raise value. */
5774 raise_window(r
->ws
->focus
);
5780 iconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5784 /* Suppress warning. */
5788 if ((w
= r
->ws
->focus
) == NULL
)
5791 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_HIDDEN
);
5792 ewmh_update_wm_state(w
);
5800 get_win_name(xcb_window_t win
)
5803 xcb_get_property_cookie_t c
;
5804 xcb_get_property_reply_t
*r
;
5806 /* First try _NET_WM_NAME for UTF-8. */
5807 c
= xcb_get_property(conn
, 0, win
, ewmh
[_NET_WM_NAME
].atom
,
5808 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5809 r
= xcb_get_property_reply(conn
, c
, NULL
);
5810 if (r
&& r
->type
== XCB_NONE
) {
5812 /* Use WM_NAME instead; no UTF-8. */
5813 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
5814 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5815 r
= xcb_get_property_reply(conn
, c
, NULL
);
5818 if (r
&& r
->type
!= XCB_NONE
&& r
->length
> 0)
5819 name
= strndup(xcb_get_property_value(r
),
5820 xcb_get_property_value_length(r
));
5825 err(1, "get_win_name: failed to allocate memory.");
5833 uniconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5842 DNPRINTF(SWM_D_MISC
, "uniconify\n");
5844 if (r
== NULL
|| r
->ws
== NULL
)
5847 /* make sure we have anything to uniconify */
5848 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5849 if (win
->ws
== NULL
)
5850 continue; /* should never happen */
5856 DNPRINTF(SWM_D_MISC
, "uniconify: count: %d\n", count
);
5862 search_resp_action
= SWM_SEARCH_UNICONIFY
;
5864 spawn_select(r
, args
, "search", &searchpid
);
5866 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5869 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5870 if (win
->ws
== NULL
)
5871 continue; /* should never happen */
5875 name
= get_win_name(win
->id
);
5876 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
5884 name_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5890 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
5896 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
5898 spawn_select(r
, args
, "name_workspace", &searchpid
);
5900 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5903 fprintf(lfile
, "%s", "");
5908 search_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5911 struct workspace
*ws
;
5916 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
5922 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
5924 spawn_select(r
, args
, "search", &searchpid
);
5926 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5929 for (i
= 0; i
< workspace_limit
; i
++) {
5933 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
5934 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
5941 search_win_cleanup(void)
5943 struct search_window
*sw
= NULL
;
5945 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
5946 xcb_destroy_window(conn
, sw
->indicator
);
5947 TAILQ_REMOVE(&search_wl
, sw
, entry
);
5953 search_win(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5955 struct ws_win
*win
= NULL
;
5956 struct search_window
*sw
= NULL
;
5959 xcb_screen_t
*screen
;
5960 int i
, width
, height
;
5968 XRectangle l_ibox
, l_lbox
;
5972 DNPRINTF(SWM_D_MISC
, "search_win\n");
5975 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
5977 spawn_select(r
, args
, "search", &searchpid
);
5979 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5982 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
5983 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
5985 TAILQ_INIT(&search_wl
);
5988 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5992 sw
= calloc(1, sizeof(struct search_window
));
5994 warn("search_win: calloc");
5996 search_win_cleanup();
6002 snprintf(s
, sizeof s
, "%d", i
);
6005 w
= xcb_generate_id(conn
);
6006 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6007 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6008 wa
[2] = screen
->default_colormap
;
6010 if (bar_font_legacy
) {
6011 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
6012 width
= l_lbox
.width
+ 4;
6013 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
6015 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
6017 width
= info
.width
+ 4;
6018 height
= bar_font
->height
+ 4;
6021 xcb_create_window(conn
, screen
->root_depth
, w
, win
->frame
, 0, 0,
6022 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
6023 screen
->root_visual
, XCB_CW_BACK_PIXEL
|
6024 XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
, wa
);
6026 xcb_map_window(conn
, w
);
6029 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
6031 if (bar_font_legacy
) {
6032 l_gcv
.graphics_exposures
= 0;
6033 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
6035 XSetForeground(display
, l_draw
,
6036 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
6038 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
6039 (bar_fs_extents
->max_logical_extent
.height
-
6040 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
6042 XFreeGC(display
, l_draw
);
6045 draw
= XftDrawCreate(display
, w
,
6046 DefaultVisual(display
, r
->s
->idx
),
6047 DefaultColormap(display
, r
->s
->idx
));
6049 XftDrawStringUtf8(draw
, &search_font_color
, bar_font
, 2,
6050 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
6051 bar_font
->descent
, (FcChar8
*)s
, len
);
6053 XftDrawDestroy(draw
);
6056 DNPRINTF(SWM_D_MISC
, "search_win: mapped win %#x\n", w
);
6058 fprintf(lfile
, "%d\n", i
);
6068 search_resp_uniconify(const char *resp
, size_t len
)
6074 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
6076 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
6079 name
= get_win_name(win
->id
);
6080 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
6085 if (strncmp(s
, resp
, len
) == 0) {
6086 /* XXX this should be a callback to generalize */
6087 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_HIDDEN
);
6088 ewmh_update_wm_state(win
);
6098 search_resp_name_workspace(const char *resp
, size_t len
)
6100 struct workspace
*ws
;
6102 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
6104 if (search_r
->ws
== NULL
)
6109 free(search_r
->ws
->name
);
6110 search_r
->ws
->name
= NULL
;
6114 ws
->name
= strdup(resp
);
6115 if (ws
->name
== NULL
) {
6116 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
6117 "strdup: %s", strerror(errno
));
6121 ewmh_update_desktop_names();
6122 ewmh_get_desktop_names();
6127 ewmh_update_desktop_names(void)
6129 char *name_list
= NULL
, *p
;
6130 int num_screens
, i
, j
, len
= 0, tot
= 0;
6132 num_screens
= get_screen_count();
6133 for (i
= 0; i
< num_screens
; ++i
) {
6134 for (j
= 0; j
< workspace_limit
; ++j
) {
6135 if (screens
[i
].ws
[j
].name
!= NULL
)
6136 len
+= strlen(screens
[i
].ws
[j
].name
);
6140 if((name_list
= calloc(len
, sizeof(char))) == NULL
)
6141 err(1, "update_desktop_names: calloc: failed to "
6142 "allocate memory.");
6145 for (j
= 0; j
< workspace_limit
; ++j
) {
6146 if (screens
[i
].ws
[j
].name
!= NULL
) {
6147 len
= strlen(screens
[i
].ws
[j
].name
);
6148 memcpy(p
, screens
[i
].ws
[j
].name
, len
);
6157 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6158 screens
[i
].root
, ewmh
[_NET_DESKTOP_NAMES
].atom
,
6159 a_utf8_string
, 8, tot
, name_list
);
6169 ewmh_get_desktop_names(void)
6172 xcb_get_property_cookie_t c
;
6173 xcb_get_property_reply_t
*r
;
6174 int num_screens
, i
, j
, n
, k
;
6176 num_screens
= get_screen_count();
6177 for (i
= 0; i
< num_screens
; ++i
) {
6178 for (j
= 0; j
< workspace_limit
; ++j
) {
6179 free(screens
[i
].ws
[j
].name
);
6180 screens
[i
].ws
[j
].name
= NULL
;
6183 c
= xcb_get_property(conn
, 0, screens
[i
].root
,
6184 ewmh
[_NET_DESKTOP_NAMES
].atom
,
6185 a_utf8_string
, 0, UINT32_MAX
);
6186 r
= xcb_get_property_reply(conn
, c
, NULL
);
6190 names
= xcb_get_property_value(r
);
6191 n
= xcb_get_property_value_length(r
);
6193 for (j
= 0, k
= 0; j
< n
; ++j
) {
6194 if (*(names
+ j
) != '\0') {
6195 screens
[i
].ws
[k
].name
= strdup(names
+ j
);
6196 j
+= strlen(names
+ j
);
6205 ewmh_update_client_list(void)
6208 int num_screens
, i
, j
, k
= 0, count
= 0;
6211 num_screens
= get_screen_count();
6212 for (i
= 0; i
< num_screens
; ++i
) {
6213 for (j
= 0; j
< workspace_limit
; ++j
)
6214 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6217 DNPRINTF(SWM_D_PROP
, "ewmh_update_client_list: win count: %d\n",
6223 wins
= calloc(count
, sizeof(xcb_window_t
));
6225 err(1, "ewmh_update_client_list: calloc: failed to "
6226 "allocate memory.");
6228 for (j
= 0, k
= 0; j
< workspace_limit
; ++j
)
6229 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6230 wins
[k
++] = win
->id
;
6232 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6233 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
6234 XCB_ATOM_WINDOW
, 32, count
, wins
);
6241 ewmh_update_current_desktop(void)
6245 num_screens
= get_screen_count();
6246 for (i
= 0; i
< num_screens
; ++i
) {
6247 if (screens
[i
].r_focus
)
6248 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6249 screens
[i
].root
, ewmh
[_NET_CURRENT_DESKTOP
].atom
,
6250 XCB_ATOM_CARDINAL
, 32, 1,
6251 &screens
[i
].r_focus
->ws
->idx
);
6256 ewmh_update_desktops(void)
6258 int num_screens
, i
, j
;
6261 vals
= calloc(workspace_limit
* 2, sizeof(uint32_t));
6263 err(1, "ewmh_update_desktops: calloc: failed to allocate "
6266 num_screens
= get_screen_count();
6267 for (i
= 0; i
< num_screens
; i
++) {
6268 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6269 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
6270 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
6272 for (j
= 0; j
< workspace_limit
; ++j
) {
6273 if (screens
[i
].ws
[j
].r
!= NULL
) {
6274 vals
[j
* 2] = X(screens
[i
].ws
[j
].r
);
6275 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].r
);
6276 } else if (screens
[i
].ws
[j
].old_r
!= NULL
) {
6277 vals
[j
* 2] = X(screens
[i
].ws
[j
].old_r
);
6278 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].old_r
);
6280 vals
[j
* 2] = vals
[j
* 2 + 1] = 0;
6284 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6285 screens
[i
].root
, ewmh
[_NET_DESKTOP_VIEWPORT
].atom
,
6286 XCB_ATOM_CARDINAL
, 32, workspace_limit
* 2, vals
);
6293 search_resp_search_workspace(const char *resp
)
6300 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
6304 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
6311 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
6313 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
6320 switchws(NULL
, search_r
, &a
);
6324 search_resp_search_window(const char *resp
)
6329 struct search_window
*sw
;
6331 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
6335 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
6340 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
6342 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
6349 TAILQ_FOREACH(sw
, &search_wl
, entry
)
6350 if (idx
== sw
->idx
) {
6356 #define MAX_RESP_LEN 1024
6359 search_do_resp(void)
6365 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
6370 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
6371 warn("search: calloc");
6375 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
6377 warn("search: read error");
6380 resp
[rbytes
] = '\0';
6383 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
6384 * newline, so work around that by sanitizing the resp now.
6386 resp
[strcspn(resp
, "\n")] = '\0';
6389 switch (search_resp_action
) {
6390 case SWM_SEARCH_UNICONIFY
:
6391 search_resp_uniconify(resp
, len
);
6393 case SWM_SEARCH_NAME_WORKSPACE
:
6394 search_resp_name_workspace(resp
, len
);
6396 case SWM_SEARCH_SEARCH_WORKSPACE
:
6397 search_resp_search_workspace(resp
);
6399 case SWM_SEARCH_SEARCH_WINDOW
:
6400 search_resp_search_window(resp
);
6405 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
6406 search_win_cleanup();
6408 search_resp_action
= SWM_SEARCH_NONE
;
6409 close(select_resp_pipe
[0]);
6416 wkill(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6420 DNPRINTF(SWM_D_MISC
, "wkill: win %#x, id: %d\n", WINID(r
->ws
->focus
),
6423 if (r
->ws
->focus
== NULL
)
6426 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
6427 xcb_kill_client(conn
, r
->ws
->focus
->id
);
6429 if (r
->ws
->focus
->can_delete
)
6430 client_msg(r
->ws
->focus
, a_delete
, 0);
6436 clear_maximized(struct workspace
*ws
)
6441 /* Clear any maximized win(s) on ws, from bottom up. */
6442 TAILQ_FOREACH_REVERSE(w
, &ws
->stack
, ws_win_stack
, stack_entry
)
6444 ewmh_apply_flags(w
, w
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
6445 ewmh_update_wm_state(w
);
6453 maximize_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6455 struct ws_win
*w
= r
->ws
->focus
;
6457 /* suppress unused warning since var is needed */
6464 DNPRINTF(SWM_D_MISC
, "maximize_toggle: win %#x\n", w
->id
);
6469 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6472 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_MAXIMIZED
);
6473 ewmh_update_wm_state(w
);
6477 if (w
== w
->ws
->focus
)
6482 DNPRINTF(SWM_D_MISC
, "maximize_toggle: done\n");
6486 floating_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6488 struct ws_win
*w
= r
->ws
->focus
;
6490 /* suppress unused warning since var is needed */
6497 DNPRINTF(SWM_D_MISC
, "floating_toggle: win %#x\n", w
->id
);
6499 if (FULLSCREEN(w
) || TRANS(w
))
6502 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6505 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_ABOVE
);
6506 ewmh_update_wm_state(w
);
6510 if (w
== w
->ws
->focus
)
6515 DNPRINTF(SWM_D_MISC
, "floating_toggle: done\n");
6519 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
6521 struct swm_geometry g
= r
->g
;
6522 int rt
, lt
, tp
, bm
, bw
;
6524 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
6527 * Perpendicular distance of each side of the window to the respective
6528 * side of the region boundary. Positive values indicate the side of
6529 * the window has passed beyond the region boundary.
6531 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) - MAX_X(r
) : bw
;
6532 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) : bw
;
6533 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) - MAX_Y(r
) : bw
;
6534 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) : bw
;
6536 DNPRINTF(SWM_D_MISC
, "region_containment: win %#x, rt: %d, lt: %d, "
6537 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
6538 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
6539 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
6542 * Disable containment if any of the flagged sides went beyond the
6543 * containment boundary, or if containment is disabled.
6545 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
6546 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
6547 /* Make sure window has at least 1 pixel in the region */
6548 g
.x
+= 1 - WIDTH(win
);
6549 g
.y
+= 1 - HEIGHT(win
);
6550 g
.w
+= 2 * WIDTH(win
) - 2;
6551 g
.h
+= 2 * HEIGHT(win
) - 2;
6554 constrain_window(win
, &g
, &opts
);
6557 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6559 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
6561 DNPRINTF(SWM_D_MISC
, "constrain_window: win %#x, (x,y) w x h: "
6562 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6563 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
6564 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
6565 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
6566 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
6567 YESNO(*opts
& SWM_CW_RESIZABLE
));
6569 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) > b
->x
+ b
->w
) {
6570 if (*opts
& SWM_CW_RESIZABLE
)
6571 WIDTH(win
) = b
->x
+ b
->w
- X(win
);
6573 X(win
) = b
->x
+ b
->w
- WIDTH(win
);
6576 if ((*opts
& SWM_CW_LEFT
) && X(win
) < b
->x
) {
6577 if (*opts
& SWM_CW_RESIZABLE
)
6578 WIDTH(win
) -= b
->x
- X(win
);
6583 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) > b
->y
+ b
->h
) {
6584 if (*opts
& SWM_CW_RESIZABLE
)
6585 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
);
6587 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
);
6590 if ((*opts
& SWM_CW_TOP
) && Y(win
) < b
->y
) {
6591 if (*opts
& SWM_CW_RESIZABLE
)
6592 HEIGHT(win
) -= b
->y
- Y(win
);
6597 if (*opts
& SWM_CW_RESIZABLE
) {
6600 if (HEIGHT(win
) < 1)
6606 draw_frame(struct ws_win
*win
)
6608 xcb_rectangle_t rect
[4];
6612 if (win
->frame
== XCB_WINDOW_NONE
) {
6613 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x not "
6614 "reparented.\n", win
->id
);
6618 if (!win
->bordered
) {
6619 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x frame "
6620 "disabled\n", win
->id
);
6623 if (WS_FOCUSED(win
->ws
) && win
->ws
->focus
== win
)
6624 pixel
= MAXIMIZED(win
) ?
6625 &win
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].pixel
:
6626 &win
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6628 pixel
= MAXIMIZED(win
) ?
6629 &win
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].pixel
:
6630 &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6632 /* Top (with corners) */
6635 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6636 rect
[n
].height
= border_width
;
6637 /* Left (without corners) */
6639 rect
[n
].y
= border_width
;
6640 rect
[n
].width
= border_width
;
6641 rect
[n
].height
= HEIGHT(win
);
6642 /* Right (without corners) */
6643 rect
[++n
].x
= border_width
+ WIDTH(win
);
6644 rect
[n
].y
= border_width
;
6645 rect
[n
].width
= border_width
;
6646 rect
[n
].height
= HEIGHT(win
);
6647 /* Bottom (with corners)*/
6649 rect
[n
].y
= border_width
+ HEIGHT(win
);
6650 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6651 rect
[n
].height
= border_width
;
6653 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, pixel
);
6654 xcb_poly_fill_rectangle(conn
, win
->frame
, win
->s
->bar_gc
, 4, rect
);
6658 update_window(struct ws_win
*win
)
6663 if (win
->frame
== XCB_WINDOW_NONE
) {
6664 DNPRINTF(SWM_D_EVENT
, "update_window: skip win %#x; "
6665 "not reparented.\n", win
->id
);
6669 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
6670 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
6671 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6673 /* Reconfigure frame. */
6674 if (win
->bordered
) {
6675 wc
[0] = X(win
) - border_width
;
6676 wc
[1] = Y(win
) - border_width
;
6677 wc
[2] = WIDTH(win
) + 2 * border_width
;
6678 wc
[3] = HEIGHT(win
) + 2 * border_width
;
6683 wc
[3] = HEIGHT(win
);
6688 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x frame %#x, (x,y) w x h: "
6689 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, win
->frame
, wc
[0],
6690 wc
[1], wc
[2], wc
[3], YESNO(win
->bordered
));
6692 xcb_configure_window(conn
, win
->frame
, mask
, wc
);
6694 /* Reconfigure client window. */
6695 wc
[0] = wc
[1] = win
->bordered
? border_width
: 0;
6697 wc
[3] = HEIGHT(win
);
6699 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x, (x,y) w x h: "
6700 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
6701 wc
[3], YESNO(win
->bordered
));
6702 xcb_configure_window(conn
, win
->id
, mask
, wc
);
6703 config_win(win
, NULL
);
6707 SIMPLEQ_ENTRY(event
) entry
;
6708 xcb_generic_event_t
*ev
;
6710 SIMPLEQ_HEAD(event_queue
, event
) events
= SIMPLEQ_HEAD_INITIALIZER(events
);
6712 xcb_generic_event_t
*
6713 get_next_event(bool wait
)
6716 xcb_generic_event_t
*evt
;
6718 /* Try queue first. */
6719 if ((ep
= SIMPLEQ_FIRST(&events
))) {
6721 SIMPLEQ_REMOVE_HEAD(&events
, entry
);
6724 evt
= xcb_wait_for_event(conn
);
6726 evt
= xcb_poll_for_event(conn
);
6732 put_back_event(xcb_generic_event_t
*evt
)
6735 if ((ep
= malloc(sizeof (struct event
))) == NULL
)
6736 err(1, "put_back_event: failed to allocate memory.");
6738 SIMPLEQ_INSERT_HEAD(&events
, ep
, entry
);
6741 /* Peeks at next event to detect auto-repeat. */
6743 keyrepeating(xcb_key_release_event_t
*kre
)
6745 xcb_generic_event_t
*evt
;
6747 /* Ensure repeating keypress is finished processing. */
6750 if ((evt
= get_next_event(false))) {
6751 put_back_event(evt
);
6753 if (XCB_EVENT_RESPONSE_TYPE(evt
) == XCB_KEY_PRESS
&&
6754 kre
->sequence
== evt
->sequence
&&
6755 kre
->detail
== ((xcb_key_press_event_t
*)evt
)->detail
)
6763 keybindreleased(struct binding
*bp
, xcb_key_release_event_t
*kre
)
6765 if (bp
->type
== KEYBIND
&& !keyrepeating(kre
) &&
6766 bp
->value
== xcb_key_press_lookup_keysym(syms
, kre
, 0))
6772 #define SWM_RESIZE_STEPS (50)
6775 resize_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
6777 xcb_timestamp_t timestamp
= 0;
6778 struct swm_region
*r
= NULL
;
6779 struct swm_geometry g
;
6780 int top
= 0, left
= 0;
6782 xcb_cursor_t cursor
;
6783 xcb_query_pointer_reply_t
*xpr
= NULL
;
6784 xcb_generic_event_t
*evt
;
6785 xcb_motion_notify_event_t
*mne
;
6786 bool resizing
, step
= false;
6792 if (FULLSCREEN(win
))
6795 /* In max_stack mode, should only resize transients. */
6796 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6799 DNPRINTF(SWM_D_EVENT
, "resize: win %#x, floating: %s, "
6800 "transient: %#x\n", win
->id
, YESNO(ABOVE(win
)),
6804 store_float_geom(win
);
6805 else if (!(TRANS(win
) || ABOVE(win
)))
6808 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6810 ewmh_update_wm_state(win
);
6816 /* It's possible for win to have been freed during focus_flush(). */
6817 if (validate_win(win
)) {
6818 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6823 case SWM_ARG_ID_WIDTHSHRINK
:
6824 WIDTH(win
) -= SWM_RESIZE_STEPS
;
6827 case SWM_ARG_ID_WIDTHGROW
:
6828 WIDTH(win
) += SWM_RESIZE_STEPS
;
6831 case SWM_ARG_ID_HEIGHTSHRINK
:
6832 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
6835 case SWM_ARG_ID_HEIGHTGROW
:
6836 HEIGHT(win
) += SWM_RESIZE_STEPS
;
6843 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6844 SWM_CW_HARDBOUNDARY
);
6846 store_float_geom(win
);
6850 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6851 SWM_CW_SOFTBOUNDARY
);
6854 /* get cursor offset from window root */
6855 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6862 if (xpr
->win_x
< WIDTH(win
) / 2)
6865 if (xpr
->win_y
< HEIGHT(win
) / 2)
6868 if (opt
== SWM_ARG_ID_CENTER
)
6869 cursor
= cursors
[XC_SIZING
].cid
;
6871 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
6872 XC_TOP_RIGHT_CORNER
].cid
;
6874 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
6875 XC_BOTTOM_RIGHT_CORNER
].cid
;
6877 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6878 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
6881 /* Release keyboard freeze if called via keybind. */
6882 if (bp
->type
== KEYBIND
)
6883 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6888 while (resizing
&& (evt
= get_next_event(true))) {
6889 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6890 case XCB_BUTTON_RELEASE
:
6891 if (bp
->type
== BTNBIND
&& bp
->value
==
6892 ((xcb_button_release_event_t
*)evt
)->detail
)
6895 case XCB_KEY_RELEASE
:
6896 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
6899 case XCB_MOTION_NOTIFY
:
6900 mne
= (xcb_motion_notify_event_t
*)evt
;
6901 DNPRINTF(SWM_D_EVENT
, "resize: MOTION_NOTIFY: "
6902 "root: %#x\n", mne
->root
);
6904 /* cursor offset/delta from start of the operation */
6905 dx
= mne
->root_x
- xpr
->root_x
;
6906 dy
= mne
->root_y
- xpr
->root_y
;
6912 if (opt
== SWM_ARG_ID_CENTER
) {
6913 if (g
.h
/ 2 + dy
< 1)
6917 HEIGHT(win
) = g
.h
+ 2 * dy
;
6925 HEIGHT(win
) = g
.h
+ dy
;
6932 if (opt
== SWM_ARG_ID_CENTER
) {
6933 if (g
.w
/ 2 + dx
< 1)
6937 WIDTH(win
) = g
.w
+ 2 * dx
;
6945 WIDTH(win
) = g
.w
+ dx
;
6948 /* not free, don't sync more than 120 times / second */
6949 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6950 timestamp
= mne
->time
;
6951 regionize(win
, mne
->root_x
, mne
->root_y
);
6952 region_containment(win
, r
, SWM_CW_ALLSIDES
|
6953 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
6954 SWM_CW_SOFTBOUNDARY
);
6959 case XCB_BUTTON_PRESS
:
6961 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_PRESS ignored\n");
6962 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
6963 ((xcb_button_press_event_t
*)evt
)->time
);
6968 DNPRINTF(SWM_D_EVENT
, "resize: KEY_PRESS ignored\n");
6969 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6970 ((xcb_key_press_event_t
*)evt
)->time
);
6976 /* It's possible for win to have been freed above. */
6977 if (validate_win(win
)) {
6978 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6986 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6987 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
6991 store_float_geom(win
);
6993 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6995 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
6999 resize(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
7001 struct ws_win
*win
= NULL
;
7002 xcb_query_pointer_reply_t
*qpr
= NULL
;
7007 if (args
->id
!= SWM_ARG_ID_DONTCENTER
&&
7008 args
->id
!= SWM_ARG_ID_CENTER
) {
7009 /* {height,width}_{grow,shrink} use the focus window. */
7013 /* resize uses window under pointer. */
7014 qpr
= xcb_query_pointer_reply(conn
,
7015 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7017 win
= find_window(qpr
->child
);
7023 resize_win(win
, bp
, args
->id
);
7025 if (args
->id
&& bp
->type
== KEYBIND
)
7031 /* Try to set window region based on supplied coordinates or window center. */
7033 regionize(struct ws_win
*win
, int x
, int y
)
7035 struct swm_region
*r
= NULL
;
7037 r
= region_under(win
->s
, x
, y
);
7039 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
7040 Y(win
) + HEIGHT(win
) / 2);
7042 if (r
&& r
!= win
->ws
->r
) {
7043 clear_maximized(r
->ws
);
7045 win_to_ws(win
, r
->ws
->idx
, false);
7047 /* Stack old and new region. */
7051 /* Set focus on new ws. */
7052 unfocus_win(r
->ws
->focus
);
7060 #define SWM_MOVE_STEPS (50)
7063 move_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
7065 struct swm_region
*r
;
7066 xcb_timestamp_t timestamp
= 0;
7067 xcb_query_pointer_reply_t
*qpr
= NULL
;
7068 xcb_generic_event_t
*evt
;
7069 xcb_motion_notify_event_t
*mne
;
7070 bool moving
, restack
= false, step
= false;
7075 if ((r
= win
->ws
->r
) == NULL
)
7078 if (FULLSCREEN(win
))
7081 DNPRINTF(SWM_D_EVENT
, "move: win %#x, floating: %s, transient: "
7082 "%#x\n", win
->id
, YESNO(ABOVE(win
)), win
->transient
);
7084 /* in max_stack mode should only move transients */
7085 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
7088 if (!(ABOVE(win
) || TRANS(win
)) || MAXIMIZED(win
)) {
7089 store_float_geom(win
);
7093 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
7095 ewmh_update_wm_state(win
);
7102 /* It's possible for win to have been freed during focus_flush(). */
7103 if (validate_win(win
)) {
7104 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7109 case SWM_ARG_ID_MOVELEFT
:
7110 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
7113 case SWM_ARG_ID_MOVERIGHT
:
7114 X(win
) += (SWM_MOVE_STEPS
- border_width
);
7117 case SWM_ARG_ID_MOVEUP
:
7118 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
7121 case SWM_ARG_ID_MOVEDOWN
:
7122 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
7129 regionize(win
, -1, -1);
7130 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
7132 store_float_geom(win
);
7136 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
7137 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
7138 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
7140 /* get cursor offset from window root */
7141 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
7144 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7148 /* Release keyboard freeze if called via keybind. */
7149 if (bp
->type
== KEYBIND
)
7150 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7153 regionize(win
, qpr
->root_x
, qpr
->root_y
);
7154 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7155 SWM_CW_SOFTBOUNDARY
);
7159 while (moving
&& (evt
= get_next_event(true))) {
7160 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
7161 case XCB_BUTTON_RELEASE
:
7162 if (bp
->type
== BTNBIND
&& bp
->value
==
7163 ((xcb_button_release_event_t
*)evt
)->detail
)
7166 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7167 ((xcb_button_release_event_t
*)evt
)->time
);
7170 case XCB_KEY_RELEASE
:
7171 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
7174 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7175 ((xcb_key_release_event_t
*)evt
)->time
);
7178 case XCB_MOTION_NOTIFY
:
7179 mne
= (xcb_motion_notify_event_t
*)evt
;
7180 DNPRINTF(SWM_D_EVENT
, "move: MOTION_NOTIFY: "
7181 "root: %#x\n", mne
->root
);
7182 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
7183 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
7185 /* not free, don't sync more than 120 times / second */
7186 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
7187 timestamp
= mne
->time
;
7188 regionize(win
, mne
->root_x
, mne
->root_y
);
7189 region_containment(win
, win
->ws
->r
,
7190 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
7195 case XCB_BUTTON_PRESS
:
7196 /* Thaw and ignore. */
7197 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7198 ((xcb_button_press_event_t
*)evt
)->time
);
7203 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7204 ((xcb_key_press_event_t
*)evt
)->time
);
7210 /* It's possible for win to have been freed. */
7211 if (validate_win(win
)) {
7212 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7220 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7221 SWM_CW_SOFTBOUNDARY
);
7225 store_float_geom(win
);
7227 /* New region set to fullscreen layout. */
7228 if (win
->ws
->r
&& win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
]) {
7235 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7236 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
7240 move(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
7242 struct ws_win
*win
= NULL
;
7243 xcb_query_pointer_reply_t
*qpr
= NULL
;
7249 /* move_* uses focus window. */
7253 /* Disallow move_ on tiled. */
7254 if (win
&& !(TRANS(win
) || ABOVE(win
)))
7257 /* move uses window under pointer. */
7258 qpr
= xcb_query_pointer_reply(conn
,
7259 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7261 win
= find_window(qpr
->child
);
7267 move_win(win
, bp
, args
->id
);
7269 if (args
->id
&& bp
->type
== KEYBIND
)
7275 /* action definitions */
7277 char name
[SWM_FUNCNAME_LEN
];
7278 void (*func
)(struct binding
*, struct swm_region
*,
7282 } actions
[FN_INVALID
+ 2] = {
7283 /* name function argument */
7284 { "bar_toggle", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
7285 { "bar_toggle_ws", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
7286 { "button2", pressbutton
, 0, {.id
= 2} },
7287 { "cycle_layout", cycle_layout
, 0, {0} },
7288 { "flip_layout", stack_config
, 0, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
7289 { "float_toggle", floating_toggle
,0, {0} },
7290 { "focus", focus_pointer
, 0, {0} },
7291 { "focus_main", focus
, 0, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
7292 { "focus_next", focus
, 0, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
7293 { "focus_prev", focus
, 0, {.id
= SWM_ARG_ID_FOCUSPREV
} },
7294 { "focus_urgent", focus
, 0, {.id
= SWM_ARG_ID_FOCUSURGENT
} },
7295 { "maximize_toggle", maximize_toggle
,0, {0} },
7296 { "height_grow", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
7297 { "height_shrink", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
7298 { "iconify", iconify
, 0, {0} },
7299 { "master_shrink", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
7300 { "master_grow", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERGROW
} },
7301 { "master_add", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERADD
} },
7302 { "master_del", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERDEL
} },
7303 { "move", move
, FN_F_NOREPLAY
, {0} },
7304 { "move_down", move
, 0, {.id
= SWM_ARG_ID_MOVEDOWN
} },
7305 { "move_left", move
, 0, {.id
= SWM_ARG_ID_MOVELEFT
} },
7306 { "move_right", move
, 0, {.id
= SWM_ARG_ID_MOVERIGHT
} },
7307 { "move_up", move
, 0, {.id
= SWM_ARG_ID_MOVEUP
} },
7308 { "mvrg_1", send_to_rg
, 0, {.id
= 0} },
7309 { "mvrg_2", send_to_rg
, 0, {.id
= 1} },
7310 { "mvrg_3", send_to_rg
, 0, {.id
= 2} },
7311 { "mvrg_4", send_to_rg
, 0, {.id
= 3} },
7312 { "mvrg_5", send_to_rg
, 0, {.id
= 4} },
7313 { "mvrg_6", send_to_rg
, 0, {.id
= 5} },
7314 { "mvrg_7", send_to_rg
, 0, {.id
= 6} },
7315 { "mvrg_8", send_to_rg
, 0, {.id
= 7} },
7316 { "mvrg_9", send_to_rg
, 0, {.id
= 8} },
7317 { "mvws_1", send_to_ws
, 0, {.id
= 0} },
7318 { "mvws_2", send_to_ws
, 0, {.id
= 1} },
7319 { "mvws_3", send_to_ws
, 0, {.id
= 2} },
7320 { "mvws_4", send_to_ws
, 0, {.id
= 3} },
7321 { "mvws_5", send_to_ws
, 0, {.id
= 4} },
7322 { "mvws_6", send_to_ws
, 0, {.id
= 5} },
7323 { "mvws_7", send_to_ws
, 0, {.id
= 6} },
7324 { "mvws_8", send_to_ws
, 0, {.id
= 7} },
7325 { "mvws_9", send_to_ws
, 0, {.id
= 8} },
7326 { "mvws_10", send_to_ws
, 0, {.id
= 9} },
7327 { "mvws_11", send_to_ws
, 0, {.id
= 10} },
7328 { "mvws_12", send_to_ws
, 0, {.id
= 11} },
7329 { "mvws_13", send_to_ws
, 0, {.id
= 12} },
7330 { "mvws_14", send_to_ws
, 0, {.id
= 13} },
7331 { "mvws_15", send_to_ws
, 0, {.id
= 14} },
7332 { "mvws_16", send_to_ws
, 0, {.id
= 15} },
7333 { "mvws_17", send_to_ws
, 0, {.id
= 16} },
7334 { "mvws_18", send_to_ws
, 0, {.id
= 17} },
7335 { "mvws_19", send_to_ws
, 0, {.id
= 18} },
7336 { "mvws_20", send_to_ws
, 0, {.id
= 19} },
7337 { "mvws_21", send_to_ws
, 0, {.id
= 20} },
7338 { "mvws_22", send_to_ws
, 0, {.id
= 21} },
7339 { "name_workspace", name_workspace
, 0, {0} },
7340 { "quit", quit
, 0, {0} },
7341 { "raise_toggle", raise_toggle
, 0, {0} },
7342 { "resize", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_DONTCENTER
} },
7343 { "resize_centered", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_CENTER
} },
7344 { "restart", restart
, 0, {0} },
7345 { "rg_1", focusrg
, 0, {.id
= 0} },
7346 { "rg_2", focusrg
, 0, {.id
= 1} },
7347 { "rg_3", focusrg
, 0, {.id
= 2} },
7348 { "rg_4", focusrg
, 0, {.id
= 3} },
7349 { "rg_5", focusrg
, 0, {.id
= 4} },
7350 { "rg_6", focusrg
, 0, {.id
= 5} },
7351 { "rg_7", focusrg
, 0, {.id
= 6} },
7352 { "rg_8", focusrg
, 0, {.id
= 7} },
7353 { "rg_9", focusrg
, 0, {.id
= 8} },
7354 { "rg_move_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_UP
} },
7355 { "rg_move_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_DOWN
} },
7356 { "rg_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7357 { "rg_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7358 { "screen_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7359 { "screen_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7360 { "search_win", search_win
, 0, {0} },
7361 { "search_workspace", search_workspace
, 0, {0} },
7362 { "spawn_custom", NULL
, 0, {0} },
7363 { "stack_balance", stack_config
, 0, {.id
= SWM_ARG_ID_STACKBALANCE
} },
7364 { "stack_inc", stack_config
, 0, {.id
= SWM_ARG_ID_STACKINC
} },
7365 { "stack_dec", stack_config
, 0, {.id
= SWM_ARG_ID_STACKDEC
} },
7366 { "stack_reset", stack_config
, 0, {.id
= SWM_ARG_ID_STACKRESET
} },
7367 { "swap_main", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPMAIN
} },
7368 { "swap_next", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPNEXT
} },
7369 { "swap_prev", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPPREV
} },
7370 { "uniconify", uniconify
, 0, {0} },
7371 { "version", version
, 0, {0} },
7372 { "width_grow", resize
, 0, {.id
= SWM_ARG_ID_WIDTHGROW
} },
7373 { "width_shrink", resize
, 0, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
7374 { "wind_del", wkill
, 0, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
7375 { "wind_kill", wkill
, 0, {.id
= SWM_ARG_ID_KILLWINDOW
} },
7376 { "ws_1", switchws
, 0, {.id
= 0} },
7377 { "ws_2", switchws
, 0, {.id
= 1} },
7378 { "ws_3", switchws
, 0, {.id
= 2} },
7379 { "ws_4", switchws
, 0, {.id
= 3} },
7380 { "ws_5", switchws
, 0, {.id
= 4} },
7381 { "ws_6", switchws
, 0, {.id
= 5} },
7382 { "ws_7", switchws
, 0, {.id
= 6} },
7383 { "ws_8", switchws
, 0, {.id
= 7} },
7384 { "ws_9", switchws
, 0, {.id
= 8} },
7385 { "ws_10", switchws
, 0, {.id
= 9} },
7386 { "ws_11", switchws
, 0, {.id
= 10} },
7387 { "ws_12", switchws
, 0, {.id
= 11} },
7388 { "ws_13", switchws
, 0, {.id
= 12} },
7389 { "ws_14", switchws
, 0, {.id
= 13} },
7390 { "ws_15", switchws
, 0, {.id
= 14} },
7391 { "ws_16", switchws
, 0, {.id
= 15} },
7392 { "ws_17", switchws
, 0, {.id
= 16} },
7393 { "ws_18", switchws
, 0, {.id
= 17} },
7394 { "ws_19", switchws
, 0, {.id
= 18} },
7395 { "ws_20", switchws
, 0, {.id
= 19} },
7396 { "ws_21", switchws
, 0, {.id
= 20} },
7397 { "ws_22", switchws
, 0, {.id
= 21} },
7398 { "ws_next", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
7399 { "ws_next_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
7400 { "ws_next_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
7401 { "ws_prev", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
7402 { "ws_prev_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
7403 { "ws_prev_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
7404 { "ws_prior", priorws
, 0, {0} },
7405 /* SWM_DEBUG actions MUST be here: */
7406 { "debug_toggle", debug_toggle
, 0, {0} },
7407 { "dumpwins", dumpwins
, 0, {0} },
7409 { "invalid action", NULL
, 0, {0} },
7413 update_modkey(uint16_t mod
)
7417 /* Replace all instances of the old mod key. */
7418 RB_FOREACH(bp
, binding_tree
, &bindings
)
7419 if (bp
->mod
& mod_key
)
7420 bp
->mod
= (bp
->mod
& ~mod_key
) | mod
;
7425 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7428 struct spawn_prog
*prog
= NULL
;
7430 char *ap
, **real_args
;
7432 /* suppress unused warning since var is needed */
7435 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
7438 TAILQ_FOREACH(prog
, &spawns
, entry
) {
7439 if (strcasecmp(spawn_name
, prog
->name
) == 0)
7443 warnx("spawn_custom: program %s not found", spawn_name
);
7447 /* make room for expanded args */
7448 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
7449 err(1, "spawn_custom: calloc real_args");
7451 /* expand spawn_args into real_args */
7452 for (i
= c
= 0; i
< prog
->argc
; i
++) {
7454 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
7455 if (strcasecmp(ap
, "$bar_border") == 0) {
7457 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
7459 err(1, "spawn_custom border color");
7460 } else if (strcasecmp(ap
, "$bar_color") == 0) {
7462 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
7464 err(1, "spawn_custom bar color");
7465 } else if (strcasecmp(ap
, "$bar_font") == 0) {
7466 if ((real_args
[c
] = strdup(bar_fonts
))
7468 err(1, "spawn_custom bar fonts");
7469 } else if (strcasecmp(ap
, "$bar_font_color") == 0) {
7471 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
7473 err(1, "spawn_custom color font");
7474 } else if (strcasecmp(ap
, "$color_focus") == 0) {
7476 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
7478 err(1, "spawn_custom color focus");
7479 } else if (strcasecmp(ap
, "$color_focus_maximized") == 0) {
7481 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].name
))
7483 err(1, "spawn_custom color focus maximized");
7484 } else if (strcasecmp(ap
, "$color_unfocus") == 0) {
7486 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
7488 err(1, "spawn_custom color unfocus");
7489 } else if (strcasecmp(ap
, "$color_unfocus_maximized") == 0) {
7491 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].name
))
7493 err(1, "spawn_custom color unfocus maximized");
7494 } else if (strcasecmp(ap
, "$region_index") == 0) {
7495 if (asprintf(&real_args
[c
], "%d",
7496 get_region_index(r
) + 1) < 1)
7497 err(1, "spawn_custom region index");
7498 } else if (strcasecmp(ap
, "$workspace_index") == 0) {
7499 if (asprintf(&real_args
[c
], "%d", r
->ws
->idx
+ 1) < 1)
7500 err(1, "spawn_custom workspace index");
7501 } else if (strcasecmp(ap
, "$dmenu_bottom") == 0) {
7504 if ((real_args
[c
] = strdup("-b")) == NULL
)
7505 err(1, "spawn_custom workspace index");
7507 /* no match --> copy as is */
7508 if ((real_args
[c
] = strdup(ap
)) == NULL
)
7509 err(1, "spawn_custom strdup(ap)");
7511 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
7517 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
7518 for (i
= 0; i
< c
; ++i
)
7519 DPRINTF("\"%s\" ", real_args
[i
]);
7522 *ret_args
= real_args
;
7527 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
7533 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7537 spawn(r
->ws
->idx
, &a
, true);
7539 for (i
= 0; i
< spawn_argc
; i
++)
7545 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7552 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7556 if (pipe(select_list_pipe
) == -1)
7557 err(1, "pipe error");
7558 if (pipe(select_resp_pipe
) == -1)
7559 err(1, "pipe error");
7561 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
7562 err(1, "could not disable SIGPIPE");
7563 switch (*pid
= fork()) {
7565 err(1, "cannot fork");
7568 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
7570 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
7572 close(select_list_pipe
[1]);
7573 close(select_resp_pipe
[0]);
7574 spawn(r
->ws
->idx
, &a
, false);
7576 default: /* parent */
7577 close(select_list_pipe
[0]);
7578 close(select_resp_pipe
[1]);
7582 for (i
= 0; i
< spawn_argc
; i
++)
7587 /* Argument tokenizer. */
7590 char *arg
, *cp
, *next
;
7591 bool single_quoted
= false, double_quoted
= false;
7596 /* Eat and move characters until end of argument is found. */
7597 for (arg
= next
= cp
= *sp
; *cp
!= '\0'; ++cp
) {
7598 if (!double_quoted
&& *cp
== '\'') {
7599 /* Eat single-quote. */
7600 single_quoted
= !single_quoted
;
7601 } else if (!single_quoted
&& *cp
== '"') {
7602 /* Eat double-quote. */
7603 double_quoted
= !double_quoted
;
7604 } else if (!single_quoted
&& *cp
== '\\' && *(cp
+ 1) == '"') {
7605 /* Eat backslash; copy escaped character to arg. */
7607 } else if (!single_quoted
&& !double_quoted
&& *cp
== '\\' &&
7608 (*(cp
+ 1) == '\'' || *(cp
+ 1) == ' ')) {
7609 /* Eat backslash; move escaped character. */
7611 } else if (!single_quoted
&& !double_quoted
&&
7612 (*cp
== ' ' || *cp
== '\t')) {
7613 /* Terminate argument. */
7615 /* Point sp to beginning of next argument. */
7619 /* Move regular character. */
7624 /* Terminate argument if end of string. */
7634 spawn_insert(const char *name
, const char *args
, int flags
)
7636 struct spawn_prog
*sp
;
7637 char *arg
, *cp
, *ptr
;
7639 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s[%s]\n", name
, args
);
7641 if (args
== NULL
|| *args
== '\0')
7644 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
7645 err(1, "spawn_insert: calloc");
7646 if ((sp
->name
= strdup(name
)) == NULL
)
7647 err(1, "spawn_insert: strdup");
7649 /* Convert the arguments to an argument list. */
7650 if ((ptr
= cp
= strdup(args
)) == NULL
)
7651 err(1, "spawn_insert: strdup");
7652 while ((arg
= argsep(&ptr
)) != NULL
) {
7653 /* Null argument; skip it. */
7658 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
7659 sizeof *sp
->argv
)) == NULL
)
7660 err(1, "spawn_insert: realloc");
7661 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
7662 err(1, "spawn_insert: strdup");
7668 DNPRINTF(SWM_D_SPAWN
, "arg %d: [%s]\n", sp
->argc
, sp
->argv
[sp
->argc
-1]);
7669 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
7670 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
7674 spawn_remove(struct spawn_prog
*sp
)
7678 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
7680 TAILQ_REMOVE(&spawns
, sp
, entry
);
7681 for (i
= 0; i
< sp
->argc
; i
++)
7687 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
7693 struct spawn_prog
*sp
;
7695 while ((sp
= TAILQ_FIRST(&spawns
)) != NULL
) {
7701 spawn_find(const char *name
)
7703 struct spawn_prog
*sp
;
7705 TAILQ_FOREACH(sp
, &spawns
, entry
)
7706 if (strcasecmp(sp
->name
, name
) == 0)
7713 setspawn(const char *name
, const char *args
, int flags
)
7715 struct spawn_prog
*sp
;
7717 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
7722 /* Remove any old spawn under the same name. */
7723 if ((sp
= spawn_find(name
)) != NULL
)
7727 spawn_insert(name
, args
, flags
);
7729 warnx("error: setspawn: cannot find program: %s", name
);
7731 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
7735 setconfspawn(const char *selector
, const char *value
, int flags
)
7739 if (selector
== NULL
|| strlen(selector
) == 0)
7742 args
= expand_tilde(value
);
7744 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
7746 setspawn(selector
, args
, flags
);
7749 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
7754 validate_spawns(void)
7757 struct spawn_prog
*sp
;
7758 char which
[PATH_MAX
];
7761 RB_FOREACH(bp
, binding_tree
, &bindings
) {
7762 if (bp
->action
!= FN_SPAWN_CUSTOM
)
7766 sp
= spawn_find(bp
->spawn_name
);
7767 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
7770 /* verify we have the goods */
7771 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
7772 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
7774 for (i
= strlen("which "); i
< strlen(which
); i
++)
7775 if (which
[i
] == ' ') {
7779 if (system(which
) != 0)
7780 add_startup_exception("could not find %s",
7781 &which
[strlen("which ")]);
7788 setconfspawn("lock", "xlock", 0);
7790 setconfspawn("term", "xterm", 0);
7791 setconfspawn("spawn_term", "xterm", 0);
7793 setconfspawn("menu", "dmenu_run"
7797 " -nf $bar_font_color"
7799 " -sf $bar_color", 0);
7801 setconfspawn("search", "dmenu"
7806 " -nf $bar_font_color"
7808 " -sf $bar_color", 0);
7810 setconfspawn("name_workspace", "dmenu"
7815 " -nf $bar_font_color"
7817 " -sf $bar_color", 0);
7819 /* These are not verified for existence, even with a binding set. */
7820 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
7821 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
7822 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
7826 #define SWM_MODNAME_SIZE 32
7827 #define SWM_KEY_WS "\n+ \t"
7829 parsebinding(const char *bindstr
, uint16_t *mod
, enum binding_type
*type
,
7830 uint32_t *val
, uint32_t *flags
)
7832 char *str
, *cp
, *name
;
7833 KeySym ks
, lks
, uks
;
7835 DNPRINTF(SWM_D_KEY
, "parsebinding: enter [%s]\n", bindstr
);
7836 if (mod
== NULL
|| val
== NULL
) {
7837 DNPRINTF(SWM_D_KEY
, "parsebinding: no mod or key vars\n");
7840 if (bindstr
== NULL
|| strlen(bindstr
) == 0) {
7841 DNPRINTF(SWM_D_KEY
, "parsebinding: no bindstr\n");
7845 if ((cp
= str
= strdup(bindstr
)) == NULL
)
7846 err(1, "parsebinding: strdup");
7848 *val
= XCB_NO_SYMBOL
;
7852 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
7853 DNPRINTF(SWM_D_KEY
, "parsebinding: entry [%s]\n", name
);
7855 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
7856 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
7858 else if (strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
) == 0)
7859 *mod
|= XCB_MOD_MASK_1
;
7860 else if (strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
) == 0)
7861 *mod
|= XCB_MOD_MASK_2
;
7862 else if (strncmp(name
, "Mod3", SWM_MODNAME_SIZE
) == 0)
7863 *mod
|= XCB_MOD_MASK_3
;
7864 else if (strncmp(name
, "Mod4", SWM_MODNAME_SIZE
) == 0)
7865 *mod
|= XCB_MOD_MASK_4
;
7866 else if (strncmp(name
, "Mod5", SWM_MODNAME_SIZE
) == 0)
7867 *mod
|= XCB_MOD_MASK_5
;
7868 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
7869 *mod
|= XCB_MOD_MASK_SHIFT
;
7870 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
7871 *mod
|= XCB_MOD_MASK_CONTROL
;
7872 else if (strncasecmp(name
, "ANYMOD", SWM_MODNAME_SIZE
) == 0)
7873 *mod
|= XCB_MOD_MASK_ANY
;
7874 else if (strncasecmp(name
, "REPLAY", SWM_MODNAME_SIZE
) == 0)
7875 *flags
|= BINDING_F_REPLAY
;
7876 else if (sscanf(name
, "Button%u", val
) == 1) {
7877 DNPRINTF(SWM_D_KEY
, "parsebinding: button %u\n", *val
);
7879 if (*val
> 255 || *val
== 0) {
7881 "parsebinding: invalid button %u\n", *val
);
7885 /* TODO: do this without Xlib. */
7886 ks
= XStringToKeysym(name
);
7887 if (ks
== NoSymbol
) {
7889 "parsebinding: invalid key %s\n", name
);
7894 XConvertCase(ks
, &lks
, &uks
);
7895 *val
= (uint32_t)lks
;
7899 /* If ANYMOD was specified, ignore the rest. */
7900 if (*mod
& XCB_MOD_MASK_ANY
)
7901 *mod
= XCB_MOD_MASK_ANY
;
7904 DNPRINTF(SWM_D_KEY
, "parsebinding: leave ok\n");
7909 strdupsafe(const char *str
)
7914 return (strdup(str
));
7918 binding_cmp(struct binding
*bp1
, struct binding
*bp2
)
7920 if (bp1
->type
< bp2
->type
)
7922 if (bp1
->type
> bp2
->type
)
7925 if (bp1
->value
< bp2
->value
)
7927 if (bp1
->value
> bp2
->value
)
7930 if (bp1
->mod
< bp2
->mod
)
7932 if (bp1
->mod
> bp2
->mod
)
7939 binding_insert(uint16_t mod
, enum binding_type type
, uint32_t val
,
7940 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
7944 DNPRINTF(SWM_D_KEY
, "binding_insert: mod: %u, type: %d, val: %u, "
7945 "action: %s(%d), spawn_name: %s\n", mod
, type
, val
,
7946 actions
[aid
].name
, aid
, spawn_name
);
7948 if ((bp
= malloc(sizeof *bp
)) == NULL
)
7949 err(1, "binding_insert: malloc");
7956 bp
->spawn_name
= strdupsafe(spawn_name
);
7957 RB_INSERT(binding_tree
, &bindings
, bp
);
7959 DNPRINTF(SWM_D_KEY
, "binding_insert: leave\n");
7963 binding_lookup(uint16_t mod
, enum binding_type type
, uint32_t val
)
7971 return (RB_FIND(binding_tree
, &bindings
, &bp
));
7975 binding_remove(struct binding
*bp
)
7977 DNPRINTF(SWM_D_KEY
, "binding_remove: mod: %u, type: %d, val: %u, "
7978 "action: %s(%d), spawn_name: %s\n", bp
->mod
, bp
->type
, bp
->value
,
7979 actions
[bp
->action
].name
, bp
->action
, bp
->spawn_name
);
7981 RB_REMOVE(binding_tree
, &bindings
, bp
);
7982 free(bp
->spawn_name
);
7985 DNPRINTF(SWM_D_KEY
, "binding_remove: leave\n");
7989 setbinding(uint16_t mod
, enum binding_type type
, uint32_t val
,
7990 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
7994 DNPRINTF(SWM_D_KEY
, "setbinding: enter %s [%s]\n",
7995 actions
[aid
].name
, spawn_name
);
7997 /* Unbind any existing. Loop is to handle MOD_MASK_ANY. */
7998 while ((bp
= binding_lookup(mod
, type
, val
)))
8001 if (aid
!= FN_INVALID
)
8002 binding_insert(mod
, type
, val
, aid
, flags
, spawn_name
);
8004 DNPRINTF(SWM_D_KEY
, "setbinding: leave\n");
8008 setconfbinding(const char *selector
, const char *value
, int flags
)
8010 struct spawn_prog
*sp
;
8011 uint32_t keybtn
, opts
;
8014 enum binding_type type
;
8016 /* suppress unused warning since var is needed */
8019 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter selector: [%s], "
8020 "value: [%s]\n", selector
, value
);
8021 if (selector
== NULL
|| strlen(selector
) == 0) {
8022 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
8023 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8024 setbinding(mod
, type
, keybtn
, FN_INVALID
, opts
, NULL
);
8029 /* search by key function name */
8030 for (aid
= 0; aid
< FN_INVALID
; aid
++) {
8031 if (strncasecmp(selector
, actions
[aid
].name
,
8032 SWM_FUNCNAME_LEN
) == 0) {
8033 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8034 "action\n", selector
);
8035 if (parsebinding(value
, &mod
, &type
, &keybtn
,
8037 setbinding(mod
, type
, keybtn
, aid
, opts
, NULL
);
8043 /* search by custom spawn name */
8044 if ((sp
= spawn_find(selector
)) != NULL
) {
8045 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8046 "spawn\n", selector
);
8047 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8048 setbinding(mod
, type
, keybtn
, FN_SPAWN_CUSTOM
, opts
,
8054 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
8058 #define MODSHIFT MODKEY | XCB_MOD_MASK_SHIFT
8060 setup_keybindings(void)
8062 #define BINDKEY(m, k, a) setbinding(m, KEYBIND, k, a, 0, NULL)
8063 #define BINDKEYSPAWN(m, k, s) setbinding(m, KEYBIND, k, FN_SPAWN_CUSTOM, 0, s)
8064 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8065 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8066 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8067 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8068 BINDKEY(MODKEY
, XK_v
, FN_BUTTON2
);
8069 BINDKEY(MODKEY
, XK_space
, FN_CYCLE_LAYOUT
);
8070 BINDKEY(MODSHIFT
, XK_backslash
, FN_FLIP_LAYOUT
);
8071 BINDKEY(MODKEY
, XK_t
, FN_FLOAT_TOGGLE
);
8072 BINDKEY(MODKEY
, XK_m
, FN_FOCUS_MAIN
);
8073 BINDKEY(MODKEY
, XK_j
, FN_FOCUS_NEXT
);
8074 BINDKEY(MODKEY
, XK_Tab
, FN_FOCUS_NEXT
);
8075 BINDKEY(MODKEY
, XK_k
, FN_FOCUS_PREV
);
8076 BINDKEY(MODSHIFT
, XK_Tab
, FN_FOCUS_PREV
);
8077 BINDKEY(MODKEY
, XK_u
, FN_FOCUS_URGENT
);
8078 BINDKEY(MODKEY
, XK_e
, FN_MAXIMIZE_TOGGLE
);
8079 BINDKEY(MODSHIFT
, XK_equal
, FN_HEIGHT_GROW
);
8080 BINDKEY(MODSHIFT
, XK_minus
, FN_HEIGHT_SHRINK
);
8081 BINDKEY(MODKEY
, XK_w
, FN_ICONIFY
);
8082 BINDKEY(MODKEY
, XK_h
, FN_MASTER_SHRINK
);
8083 BINDKEY(MODKEY
, XK_l
, FN_MASTER_GROW
);
8084 BINDKEY(MODKEY
, XK_comma
, FN_MASTER_ADD
);
8085 BINDKEY(MODKEY
, XK_period
, FN_MASTER_DEL
);
8086 BINDKEY(MODSHIFT
, XK_bracketright
, FN_MOVE_DOWN
);
8087 BINDKEY(MODKEY
, XK_bracketleft
, FN_MOVE_LEFT
);
8088 BINDKEY(MODKEY
, XK_bracketright
, FN_MOVE_RIGHT
);
8089 BINDKEY(MODSHIFT
, XK_bracketleft
, FN_MOVE_UP
);
8090 BINDKEY(MODSHIFT
, XK_KP_End
, FN_MVRG_1
);
8091 BINDKEY(MODSHIFT
, XK_KP_Down
, FN_MVRG_2
);
8092 BINDKEY(MODSHIFT
, XK_KP_Next
, FN_MVRG_3
);
8093 BINDKEY(MODSHIFT
, XK_KP_Left
, FN_MVRG_4
);
8094 BINDKEY(MODSHIFT
, XK_KP_Begin
, FN_MVRG_5
);
8095 BINDKEY(MODSHIFT
, XK_KP_Right
, FN_MVRG_6
);
8096 BINDKEY(MODSHIFT
, XK_KP_Home
, FN_MVRG_7
);
8097 BINDKEY(MODSHIFT
, XK_KP_Up
, FN_MVRG_8
);
8098 BINDKEY(MODSHIFT
, XK_KP_Prior
, FN_MVRG_9
);
8099 BINDKEY(MODSHIFT
, XK_1
, FN_MVWS_1
);
8100 BINDKEY(MODSHIFT
, XK_2
, FN_MVWS_2
);
8101 BINDKEY(MODSHIFT
, XK_3
, FN_MVWS_3
);
8102 BINDKEY(MODSHIFT
, XK_4
, FN_MVWS_4
);
8103 BINDKEY(MODSHIFT
, XK_5
, FN_MVWS_5
);
8104 BINDKEY(MODSHIFT
, XK_6
, FN_MVWS_6
);
8105 BINDKEY(MODSHIFT
, XK_7
, FN_MVWS_7
);
8106 BINDKEY(MODSHIFT
, XK_8
, FN_MVWS_8
);
8107 BINDKEY(MODSHIFT
, XK_9
, FN_MVWS_9
);
8108 BINDKEY(MODSHIFT
, XK_0
, FN_MVWS_10
);
8109 BINDKEY(MODSHIFT
, XK_F1
, FN_MVWS_11
);
8110 BINDKEY(MODSHIFT
, XK_F2
, FN_MVWS_12
);
8111 BINDKEY(MODSHIFT
, XK_F3
, FN_MVWS_13
);
8112 BINDKEY(MODSHIFT
, XK_F4
, FN_MVWS_14
);
8113 BINDKEY(MODSHIFT
, XK_F5
, FN_MVWS_15
);
8114 BINDKEY(MODSHIFT
, XK_F6
, FN_MVWS_16
);
8115 BINDKEY(MODSHIFT
, XK_F7
, FN_MVWS_17
);
8116 BINDKEY(MODSHIFT
, XK_F8
, FN_MVWS_18
);
8117 BINDKEY(MODSHIFT
, XK_F9
, FN_MVWS_19
);
8118 BINDKEY(MODSHIFT
, XK_F10
, FN_MVWS_20
);
8119 BINDKEY(MODSHIFT
, XK_F11
, FN_MVWS_21
);
8120 BINDKEY(MODSHIFT
, XK_F12
, FN_MVWS_22
);
8121 BINDKEY(MODSHIFT
, XK_slash
, FN_NAME_WORKSPACE
);
8122 BINDKEY(MODSHIFT
, XK_q
, FN_QUIT
);
8123 BINDKEY(MODSHIFT
, XK_r
, FN_RAISE_TOGGLE
);
8124 BINDKEY(MODKEY
, XK_q
, FN_RESTART
);
8125 BINDKEY(MODKEY
, XK_KP_End
, FN_RG_1
);
8126 BINDKEY(MODKEY
, XK_KP_Down
, FN_RG_2
);
8127 BINDKEY(MODKEY
, XK_KP_Next
, FN_RG_3
);
8128 BINDKEY(MODKEY
, XK_KP_Left
, FN_RG_4
);
8129 BINDKEY(MODKEY
, XK_KP_Begin
, FN_RG_5
);
8130 BINDKEY(MODKEY
, XK_KP_Right
, FN_RG_6
);
8131 BINDKEY(MODKEY
, XK_KP_Home
, FN_RG_7
);
8132 BINDKEY(MODKEY
, XK_KP_Up
, FN_RG_8
);
8133 BINDKEY(MODKEY
, XK_KP_Prior
, FN_RG_9
);
8134 BINDKEY(MODSHIFT
, XK_Right
, FN_RG_NEXT
);
8135 BINDKEY(MODSHIFT
, XK_Left
, FN_RG_PREV
);
8136 BINDKEY(MODKEY
, XK_f
, FN_SEARCH_WIN
);
8137 BINDKEY(MODKEY
, XK_slash
, FN_SEARCH_WORKSPACE
);
8138 BINDKEYSPAWN(MODSHIFT
, XK_i
, "initscr");
8139 BINDKEYSPAWN(MODSHIFT
, XK_Delete
, "lock");
8140 BINDKEYSPAWN(MODKEY
, XK_p
, "menu");
8141 BINDKEYSPAWN(MODKEY
, XK_s
, "screenshot_all");
8142 BINDKEYSPAWN(MODSHIFT
, XK_s
, "screenshot_wind");
8143 BINDKEYSPAWN(MODSHIFT
, XK_Return
, "term");
8144 BINDKEY(MODSHIFT
, XK_comma
, FN_STACK_INC
);
8145 BINDKEY(MODSHIFT
, XK_period
, FN_STACK_DEC
);
8146 BINDKEY(MODSHIFT
, XK_space
, FN_STACK_RESET
);
8147 BINDKEY(MODKEY
, XK_Return
, FN_SWAP_MAIN
);
8148 BINDKEY(MODSHIFT
, XK_j
, FN_SWAP_NEXT
);
8149 BINDKEY(MODSHIFT
, XK_k
, FN_SWAP_PREV
);
8150 BINDKEY(MODSHIFT
, XK_w
, FN_UNICONIFY
);
8151 BINDKEY(MODSHIFT
, XK_v
, FN_VERSION
);
8152 BINDKEY(MODKEY
, XK_equal
, FN_WIDTH_GROW
);
8153 BINDKEY(MODKEY
, XK_minus
, FN_WIDTH_SHRINK
);
8154 BINDKEY(MODKEY
, XK_x
, FN_WIND_DEL
);
8155 BINDKEY(MODSHIFT
, XK_x
, FN_WIND_KILL
);
8156 BINDKEY(MODKEY
, XK_1
, FN_WS_1
);
8157 BINDKEY(MODKEY
, XK_2
, FN_WS_2
);
8158 BINDKEY(MODKEY
, XK_3
, FN_WS_3
);
8159 BINDKEY(MODKEY
, XK_4
, FN_WS_4
);
8160 BINDKEY(MODKEY
, XK_5
, FN_WS_5
);
8161 BINDKEY(MODKEY
, XK_6
, FN_WS_6
);
8162 BINDKEY(MODKEY
, XK_7
, FN_WS_7
);
8163 BINDKEY(MODKEY
, XK_8
, FN_WS_8
);
8164 BINDKEY(MODKEY
, XK_9
, FN_WS_9
);
8165 BINDKEY(MODKEY
, XK_0
, FN_WS_10
);
8166 BINDKEY(MODKEY
, XK_F1
, FN_WS_11
);
8167 BINDKEY(MODKEY
, XK_F2
, FN_WS_12
);
8168 BINDKEY(MODKEY
, XK_F3
, FN_WS_13
);
8169 BINDKEY(MODKEY
, XK_F4
, FN_WS_14
);
8170 BINDKEY(MODKEY
, XK_F5
, FN_WS_15
);
8171 BINDKEY(MODKEY
, XK_F6
, FN_WS_16
);
8172 BINDKEY(MODKEY
, XK_F7
, FN_WS_17
);
8173 BINDKEY(MODKEY
, XK_F8
, FN_WS_18
);
8174 BINDKEY(MODKEY
, XK_F9
, FN_WS_19
);
8175 BINDKEY(MODKEY
, XK_F10
, FN_WS_20
);
8176 BINDKEY(MODKEY
, XK_F11
, FN_WS_21
);
8177 BINDKEY(MODKEY
, XK_F12
, FN_WS_22
);
8178 BINDKEY(MODKEY
, XK_Right
, FN_WS_NEXT
);
8179 BINDKEY(MODKEY
, XK_Left
, FN_WS_PREV
);
8180 BINDKEY(MODKEY
, XK_Up
, FN_WS_NEXT_ALL
);
8181 BINDKEY(MODKEY
, XK_Down
, FN_WS_PREV_ALL
);
8182 BINDKEY(MODSHIFT
, XK_Up
, FN_WS_NEXT_MOVE
);
8183 BINDKEY(MODSHIFT
, XK_Down
, FN_WS_PREV_MOVE
);
8184 BINDKEY(MODKEY
, XK_a
, FN_WS_PRIOR
);
8186 BINDKEY(MODKEY
, XK_d
, FN_DEBUG_TOGGLE
);
8187 BINDKEY(MODSHIFT
, XK_d
, FN_DUMPWINS
);
8194 setup_btnbindings(void)
8196 setbinding(ANYMOD
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_FOCUS
,
8197 BINDING_F_REPLAY
, NULL
);
8198 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE
, 0, NULL
);
8199 setbinding(MODSHIFT
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE_CENTERED
, 0,
8201 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_MOVE
, 0, NULL
);
8206 clear_bindings(void)
8210 while ((bp
= RB_ROOT(&bindings
)))
8215 clear_keybindings(void)
8217 struct binding
*bp
, *bptmp
;
8219 RB_FOREACH_SAFE(bp
, binding_tree
, &bindings
, bptmp
) {
8220 if (bp
->type
!= KEYBIND
)
8227 setkeymapping(const char *selector
, const char *value
, int flags
)
8229 char *keymapping_file
;
8231 /* suppress unused warnings since vars are needed */
8235 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
8237 keymapping_file
= expand_tilde(value
);
8239 clear_keybindings();
8240 /* load new key bindings; if it fails, revert to default bindings */
8241 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
8242 clear_keybindings();
8243 setup_keybindings();
8246 free(keymapping_file
);
8248 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
8253 updatenumlockmask(void)
8256 xcb_get_modifier_mapping_reply_t
*modmap_r
;
8257 xcb_keycode_t
*modmap
, kc
, *keycode
;
8261 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
8262 xcb_get_modifier_mapping(conn
),
8265 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
8266 for (i
= 0; i
< 8; i
++) {
8267 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
8268 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
8270 keycode
= xcb_key_symbols_get_keycode(syms
,
8274 numlockmask
= (1 << i
);
8281 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
8288 int num_screens
, k
, j
;
8289 uint16_t modifiers
[4];
8290 xcb_keycode_t
*code
;
8292 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
8293 updatenumlockmask();
8296 modifiers
[1] = numlockmask
;
8297 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8298 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8300 num_screens
= get_screen_count();
8301 for (k
= 0; k
< num_screens
; k
++) {
8302 if (TAILQ_EMPTY(&screens
[k
].rl
))
8304 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
8306 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8307 if (bp
->type
!= KEYBIND
)
8310 /* If there is a catch-all, only bind that. */
8311 if ((binding_lookup(ANYMOD
, KEYBIND
, bp
->value
)) &&
8315 /* Skip unused ws binds. */
8316 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8317 bp
->action
<= FN_WS_22
)
8320 /* Skip unused mvws binds. */
8321 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8322 bp
->action
<= FN_MVWS_22
)
8325 if ((code
= xcb_key_symbols_get_keycode(syms
,
8326 bp
->value
)) == NULL
)
8329 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8330 /* All modifiers are grabbed in one pass. */
8331 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8332 "key: %u, modifiers: %d\n", bp
->value
,
8334 xcb_grab_key(conn
, 1, screens
[k
].root
,
8335 bp
->mod
, *code
, XCB_GRAB_MODE_ASYNC
,
8336 XCB_GRAB_MODE_SYNC
);
8338 /* Need to grab each modifier permutation. */
8339 for (j
= 0; j
< LENGTH(modifiers
); j
++) {
8340 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8341 "key: %u, modifiers: %d\n",
8342 bp
->value
, bp
->mod
| modifiers
[j
]);
8343 xcb_grab_key(conn
, 1,
8345 bp
->mod
| modifiers
[j
],
8346 *code
, XCB_GRAB_MODE_ASYNC
,
8347 XCB_GRAB_MODE_SYNC
);
8359 int num_screens
, i
, k
;
8360 uint16_t modifiers
[4];
8362 DNPRINTF(SWM_D_MOUSE
, "grabbuttons\n");
8363 updatenumlockmask();
8366 modifiers
[1] = numlockmask
;
8367 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8368 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8370 num_screens
= get_screen_count();
8371 for (k
= 0; k
< num_screens
; k
++) {
8372 if (TAILQ_EMPTY(&screens
[k
].rl
))
8374 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, screens
[k
].root
,
8376 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8377 if (bp
->type
!= BTNBIND
)
8380 /* If there is a catch-all, only bind that. */
8381 if ((binding_lookup(ANYMOD
, BTNBIND
, bp
->value
)) &&
8385 /* Skip unused ws binds. */
8386 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8387 bp
->action
<= FN_WS_22
)
8390 /* Skip unused mvws binds. */
8391 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8392 bp
->action
<= FN_MVWS_22
)
8395 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8396 /* All modifiers are grabbed in one pass. */
8397 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: grab, "
8398 "button: %u, modifiers: %d\n", bp
->value
,
8400 xcb_grab_button(conn
, 0, screens
[k
].root
,
8401 BUTTONMASK
, XCB_GRAB_MODE_SYNC
,
8402 XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
8403 XCB_CURSOR_NONE
, bp
->value
, bp
->mod
);
8405 /* Need to grab each modifier permutation. */
8406 for (i
= 0; i
< LENGTH(modifiers
); ++i
) {
8407 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: "
8408 "grab, button: %u, modifiers: %u\n",
8409 bp
->value
, bp
->mod
| modifiers
[i
]);
8410 xcb_grab_button(conn
, 0,
8411 screens
[k
].root
, BUTTONMASK
,
8413 XCB_GRAB_MODE_ASYNC
,
8415 XCB_CURSOR_NONE
, bp
->value
,
8416 bp
->mod
| modifiers
[i
]);
8423 const char *quirkname
[] = {
8424 "NONE", /* config string for "no value" */
8432 "FOCUSONMAP_SINGLE",
8440 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
8441 #define SWM_Q_DELIM "\n|+ \t"
8443 parsequirks(const char *qstr
, uint32_t *quirk
, int *ws
)
8445 char *str
, *cp
, *name
;
8448 if (quirk
== NULL
|| qstr
== NULL
)
8451 if ((cp
= str
= strdup(qstr
)) == NULL
)
8452 err(1, "parsequirks: strdup");
8455 while ((name
= strsep(&cp
, SWM_Q_DELIM
)) != NULL
) {
8457 cp
+= (long)strspn(cp
, SWM_Q_DELIM
);
8459 if (sscanf(name
, "WS[%d]", ws
) == 1) {
8465 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
8466 if (strncasecmp(name
, quirkname
[i
],
8467 SWM_QUIRK_LEN
) == 0) {
8468 DNPRINTF(SWM_D_QUIRK
,
8469 "parsequirks: %s\n", name
);
8475 *quirk
|= 1 << (i
-1);
8479 if (i
>= LENGTH(quirkname
)) {
8480 DNPRINTF(SWM_D_QUIRK
,
8481 "parsequirks: invalid quirk [%s]\n", name
);
8492 quirk_insert(const char *class, const char *instance
, const char *name
,
8493 uint32_t quirk
, int ws
)
8497 bool failed
= false;
8499 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: class: %s, instance: %s, name: %s,"
8500 " value: %u, ws: %d\n", class, instance
, name
, quirk
, ws
);
8502 if ((qp
= malloc(sizeof *qp
)) == NULL
)
8503 err(1, "quirk_insert: malloc");
8505 if ((qp
->class = strdup(class)) == NULL
)
8506 err(1, "quirk_insert: strdup");
8507 if ((qp
->instance
= strdup(instance
)) == NULL
)
8508 err(1, "quirk_insert: strdup");
8509 if ((qp
->name
= strdup(name
)) == NULL
)
8510 err(1, "quirk_insert: strdup");
8512 if (asprintf(&str
, "^%s$", class) == -1)
8513 err(1, "quirk_insert: asprintf");
8514 if (regcomp(&qp
->regex_class
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8515 add_startup_exception("regex failed to compile quirk 'class' "
8516 "field: %s", class);
8519 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8522 if (asprintf(&str
, "^%s$", instance
) == -1)
8523 err(1, "quirk_insert: asprintf");
8524 if (regcomp(&qp
->regex_instance
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8525 add_startup_exception("regex failed to compile quirk 'instance'"
8526 " field: %s", instance
);
8529 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8532 if (asprintf(&str
, "^%s$", name
) == -1)
8533 err(1, "quirk_insert: asprintf");
8534 if (regcomp(&qp
->regex_name
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8535 add_startup_exception("regex failed to compile quirk 'name' "
8539 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8543 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: regex error; skipping.\n");
8548 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
8550 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
8554 quirk_remove(struct quirk
*qp
)
8556 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%u]\n", qp
->class,
8557 qp
->name
, qp
->quirk
);
8559 TAILQ_REMOVE(&quirks
, qp
, entry
);
8562 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
8566 quirk_free(struct quirk
*qp
)
8568 regfree(&qp
->regex_class
);
8569 regfree(&qp
->regex_instance
);
8570 regfree(&qp
->regex_name
);
8582 while ((qp
= TAILQ_FIRST(&quirks
)) != NULL
) {
8588 quirk_replace(struct quirk
*qp
, const char *class, const char *instance
,
8589 const char *name
, uint32_t quirk
, int ws
)
8591 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp
->class,
8592 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
8595 quirk_insert(class, instance
, name
, quirk
, ws
);
8597 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
8601 setquirk(const char *class, const char *instance
, const char *name
,
8602 uint32_t quirk
, int ws
)
8606 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
8607 instance
, name
, quirk
, ws
);
8609 /* Remove/replace existing quirk. */
8610 TAILQ_FOREACH(qp
, &quirks
, entry
) {
8611 if (strcmp(qp
->class, class) == 0 &&
8612 strcmp(qp
->instance
, instance
) == 0 &&
8613 strcmp(qp
->name
, name
) == 0) {
8614 if (quirk
== 0 && ws
== -1)
8617 quirk_replace(qp
, class, instance
, name
, quirk
,
8619 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8624 /* Only insert if quirk is not NONE or forced ws is set. */
8625 if (quirk
|| ws
!= -1)
8626 quirk_insert(class, instance
, name
, quirk
, ws
);
8628 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8631 /* Eat '\' in str used to escape square brackets and colon. */
8633 unescape_selector(char *str
)
8637 for (cp
= str
; *str
!= '\0'; ++str
, ++cp
) {
8638 if (*str
== '\\' && (*(str
+ 1) == ':' || *(str
+ 1) == ']' ||
8648 setconfquirk(const char *selector
, const char *value
, int flags
)
8650 char *str
, *cp
, *class;
8651 char *instance
= NULL
, *name
= NULL
;
8652 int retval
, count
= 0, ws
= -1;
8655 /* suppress unused warning since var is needed */
8658 if (selector
== NULL
|| strlen(selector
) == 0)
8661 if ((str
= strdup(selector
)) == NULL
)
8662 err(1, "setconfquirk: strdup");
8664 /* Find non-escaped colon. */
8671 for (++cp
; *cp
!= '\0'; ++cp
) {
8672 if (*cp
== ':' && *(cp
- 1) != '\\') {
8678 unescape_selector(class);
8680 instance
= class + strlen(class) + 1;
8681 unescape_selector(instance
);
8687 name
= instance
+ strlen(instance
) + 1;
8688 unescape_selector(name
);
8693 DNPRINTF(SWM_D_CONF
, "setconfquirk: class: %s, instance: %s, "
8694 "name: %s\n", class, instance
, name
);
8696 if ((retval
= parsequirks(value
, &qrks
, &ws
)) == 0)
8697 setquirk(class, instance
, name
, qrks
, ws
);
8706 setquirk("MPlayer", "xv", ".*",
8707 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
, -1);
8708 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
8710 setquirk("Firefox-bin", "firefox-bin", ".*",
8712 setquirk("Firefox", "Dialog", ".*",
8714 setquirk("Gimp", "gimp", ".*",
8715 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8716 setquirk("XTerm", "xterm", ".*",
8717 SWM_Q_XTERM_FONTADJ
, -1);
8718 setquirk("xine", "Xine Window", ".*",
8719 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8720 setquirk("Xitk", "Xitk Combo", ".*",
8721 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8722 setquirk("xine", "xine Panel", ".*",
8723 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8724 setquirk("Xitk", "Xine Window", ".*",
8725 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8726 setquirk("xine", "xine Video Fullscreen Window", ".*",
8727 SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
, -1);
8728 setquirk("pcb", "pcb", ".*",
8730 setquirk("SDL_App", "SDL_App", ".*",
8731 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
, -1);
8734 /* conf file stuff */
8735 #define SWM_CONF_FILE "spectrwm.conf"
8736 #define SWM_CONF_FILE_OLD "scrotwm.conf"
8740 SWM_S_BAR_AT_BOTTOM
,
8741 SWM_S_BAR_BORDER_WIDTH
,
8744 SWM_S_BAR_ENABLED_WS
,
8749 SWM_S_BOUNDARY_WIDTH
,
8750 SWM_S_CLOCK_ENABLED
,
8753 SWM_S_CYCLE_VISIBLE
,
8755 SWM_S_DISABLE_BORDER
,
8757 SWM_S_FOCUS_CLOSE_WRAP
,
8758 SWM_S_FOCUS_DEFAULT
,
8760 SWM_S_ICONIC_ENABLED
,
8761 SWM_S_JAVA_WORKAROUND
,
8762 SWM_S_MAXIMIZE_HIDE_BAR
,
8763 SWM_S_REGION_PADDING
,
8768 SWM_S_STACK_ENABLED
,
8771 SWM_S_URGENT_COLLAPSE
,
8772 SWM_S_URGENT_ENABLED
,
8773 SWM_S_VERBOSE_LAYOUT
,
8776 SWM_S_WINDOW_CLASS_ENABLED
,
8777 SWM_S_WINDOW_INSTANCE_ENABLED
,
8778 SWM_S_WINDOW_NAME_ENABLED
,
8779 SWM_S_WORKSPACE_CLAMP
,
8780 SWM_S_WORKSPACE_LIMIT
,
8781 SWM_S_WORKSPACE_NAME
,
8785 setconfvalue(const char *selector
, const char *value
, int flags
)
8787 struct workspace
*ws
;
8788 int i
, ws_id
, num_screens
;
8789 char *b
, *str
, *sp
, s
[1024];
8792 case SWM_S_BAR_ACTION
:
8794 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
8795 err(1, "setconfvalue: bar_action");
8797 case SWM_S_BAR_AT_BOTTOM
:
8798 bar_at_bottom
= (atoi(value
) != 0);
8800 case SWM_S_BAR_BORDER_WIDTH
:
8801 bar_border_width
= atoi(value
);
8802 if (bar_border_width
< 0)
8803 bar_border_width
= 0;
8805 case SWM_S_BAR_DELAY
:
8806 /* No longer needed; leave to not break old conf files. */
8808 case SWM_S_BAR_ENABLED
:
8809 bar_enabled
= (atoi(value
) != 0);
8811 case SWM_S_BAR_ENABLED_WS
:
8812 ws_id
= atoi(selector
) - 1;
8813 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8814 errx(1, "setconfvalue: bar_enabled_ws: invalid "
8815 "workspace %d.", ws_id
+ 1);
8817 num_screens
= get_screen_count();
8818 for (i
= 0; i
< num_screens
; i
++) {
8819 ws
= (struct workspace
*)&screens
[i
].ws
;
8820 ws
[ws_id
].bar_enabled
= (atoi(value
) != 0);
8823 case SWM_S_BAR_FONT
:
8825 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
8826 err(1, "setconfvalue: asprintf: failed to allocate "
8827 "memory for bar_fonts.");
8830 /* If already in xft mode, then we are done. */
8831 if (!bar_font_legacy
)
8834 if ((sp
= str
= strdup(value
)) == NULL
)
8835 err(1, "setconfvalue: strdup");
8837 /* If there are any non-XLFD entries, switch to Xft mode. */
8838 while ((b
= strsep(&sp
, ",")) != NULL
) {
8842 bar_font_legacy
= false;
8849 case SWM_S_BAR_FORMAT
:
8851 if ((bar_format
= strdup(value
)) == NULL
)
8852 err(1, "setconfvalue: bar_format");
8854 case SWM_S_BAR_JUSTIFY
:
8855 if (strcmp(value
, "left") == 0)
8856 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
8857 else if (strcmp(value
, "center") == 0)
8858 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
8859 else if (strcmp(value
, "right") == 0)
8860 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
8862 errx(1, "invalid bar_justify");
8864 case SWM_S_BORDER_WIDTH
:
8865 border_width
= atoi(value
);
8866 if (border_width
< 0)
8869 case SWM_S_BOUNDARY_WIDTH
:
8870 boundary_width
= atoi(value
);
8871 if (boundary_width
< 0)
8874 case SWM_S_CLOCK_ENABLED
:
8875 clock_enabled
= (atoi(value
) != 0);
8877 case SWM_S_CLOCK_FORMAT
:
8878 #ifndef SWM_DENY_CLOCK_FORMAT
8880 if ((clock_format
= strdup(value
)) == NULL
)
8881 err(1, "setconfvalue: clock_format");
8884 case SWM_S_CYCLE_EMPTY
:
8885 cycle_empty
= (atoi(value
) != 0);
8887 case SWM_S_CYCLE_VISIBLE
:
8888 cycle_visible
= (atoi(value
) != 0);
8890 case SWM_S_DIALOG_RATIO
:
8891 dialog_ratio
= atof(value
);
8892 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
8895 case SWM_S_DISABLE_BORDER
:
8896 disable_border
= (atoi(value
) != 0);
8898 case SWM_S_FOCUS_CLOSE
:
8899 if (strcmp(value
, "first") == 0)
8900 focus_close
= SWM_STACK_BOTTOM
;
8901 else if (strcmp(value
, "last") == 0)
8902 focus_close
= SWM_STACK_TOP
;
8903 else if (strcmp(value
, "next") == 0)
8904 focus_close
= SWM_STACK_ABOVE
;
8905 else if (strcmp(value
, "previous") == 0)
8906 focus_close
= SWM_STACK_BELOW
;
8908 errx(1, "focus_close");
8910 case SWM_S_FOCUS_CLOSE_WRAP
:
8911 focus_close_wrap
= (atoi(value
) != 0);
8913 case SWM_S_FOCUS_DEFAULT
:
8914 if (strcmp(value
, "last") == 0)
8915 focus_default
= SWM_STACK_TOP
;
8916 else if (strcmp(value
, "first") == 0)
8917 focus_default
= SWM_STACK_BOTTOM
;
8919 errx(1, "focus_default");
8921 case SWM_S_FOCUS_MODE
:
8922 if (strcmp(value
, "default") == 0)
8923 focus_mode
= SWM_FOCUS_DEFAULT
;
8924 else if (strcmp(value
, "follow") == 0 ||
8925 strcmp(value
, "follow_cursor") == 0)
8926 focus_mode
= SWM_FOCUS_FOLLOW
;
8927 else if (strcmp(value
, "manual") == 0)
8928 focus_mode
= SWM_FOCUS_MANUAL
;
8930 errx(1, "focus_mode");
8932 case SWM_S_ICONIC_ENABLED
:
8933 iconic_enabled
= (atoi(value
) != 0);
8935 case SWM_S_JAVA_WORKAROUND
:
8936 java_workaround
= (atoi(value
) != 0);
8938 case SWM_S_MAXIMIZE_HIDE_BAR
:
8939 maximize_hide_bar
= atoi(value
);
8941 case SWM_S_REGION_PADDING
:
8942 region_padding
= atoi(value
);
8943 if (region_padding
< 0)
8946 case SWM_S_SPAWN_ORDER
:
8947 if (strcmp(value
, "first") == 0)
8948 spawn_position
= SWM_STACK_BOTTOM
;
8949 else if (strcmp(value
, "last") == 0)
8950 spawn_position
= SWM_STACK_TOP
;
8951 else if (strcmp(value
, "next") == 0)
8952 spawn_position
= SWM_STACK_ABOVE
;
8953 else if (strcmp(value
, "previous") == 0)
8954 spawn_position
= SWM_STACK_BELOW
;
8956 errx(1, "spawn_position");
8958 case SWM_S_SPAWN_TERM
:
8959 setconfspawn("term", value
, 0);
8960 setconfspawn("spawn_term", value
, 0);
8963 /* No longer needed; leave to not break old conf files. */
8965 case SWM_S_SS_ENABLED
:
8966 /* No longer needed; leave to not break old conf files. */
8968 case SWM_S_STACK_ENABLED
:
8969 stack_enabled
= (atoi(value
) != 0);
8971 case SWM_S_TERM_WIDTH
:
8972 term_width
= atoi(value
);
8976 case SWM_S_TILE_GAP
:
8977 tile_gap
= atoi(value
);
8979 case SWM_S_URGENT_COLLAPSE
:
8980 urgent_collapse
= (atoi(value
) != 0);
8982 case SWM_S_URGENT_ENABLED
:
8983 urgent_enabled
= (atoi(value
) != 0);
8985 case SWM_S_VERBOSE_LAYOUT
:
8986 verbose_layout
= (atoi(value
) != 0);
8987 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
8989 layouts
[i
].l_string
= fancy_stacker
;
8991 layouts
[i
].l_string
= plain_stacker
;
8994 case SWM_S_WARP_FOCUS
:
8995 warp_focus
= (atoi(value
) != 0);
8997 case SWM_S_WARP_POINTER
:
8998 warp_pointer
= (atoi(value
) != 0);
9000 case SWM_S_WINDOW_CLASS_ENABLED
:
9001 window_class_enabled
= (atoi(value
) != 0);
9003 case SWM_S_WINDOW_INSTANCE_ENABLED
:
9004 window_instance_enabled
= (atoi(value
) != 0);
9006 case SWM_S_WINDOW_NAME_ENABLED
:
9007 window_name_enabled
= (atoi(value
) != 0);
9009 case SWM_S_WORKSPACE_CLAMP
:
9010 workspace_clamp
= (atoi(value
) != 0);
9012 case SWM_S_WORKSPACE_LIMIT
:
9013 workspace_limit
= atoi(value
);
9014 if (workspace_limit
> SWM_WS_MAX
)
9015 workspace_limit
= SWM_WS_MAX
;
9016 else if (workspace_limit
< 1)
9017 workspace_limit
= 1;
9019 ewmh_update_desktops();
9021 case SWM_S_WORKSPACE_NAME
:
9022 if (getenv("SWM_STARTED") != NULL
)
9026 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9027 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
9029 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9030 errx(1, "setconfvalue: workspace_name: invalid "
9031 "workspace %d.", ws_id
+ 1);
9033 num_screens
= get_screen_count();
9034 for (i
= 0; i
< num_screens
; ++i
) {
9035 ws
= (struct workspace
*)&screens
[i
].ws
;
9037 if (strlen(s
) > 0) {
9038 free(ws
[ws_id
].name
);
9039 if ((ws
[ws_id
].name
= strdup(s
)) == NULL
)
9040 err(1, "setconfvalue: workspace_name.");
9042 ewmh_update_desktop_names();
9043 ewmh_get_desktop_names();
9054 setconfmodkey(const char *selector
, const char *value
, int flags
)
9056 /* suppress unused warnings since vars are needed */
9060 if (strncasecmp(value
, "Mod1", strlen("Mod1")) == 0)
9061 update_modkey(XCB_MOD_MASK_1
);
9062 else if (strncasecmp(value
, "Mod2", strlen("Mod2")) == 0)
9063 update_modkey(XCB_MOD_MASK_2
);
9064 else if (strncasecmp(value
, "Mod3", strlen("Mod3")) == 0)
9065 update_modkey(XCB_MOD_MASK_3
);
9066 else if (strncasecmp(value
, "Mod4", strlen("Mod4")) == 0)
9067 update_modkey(XCB_MOD_MASK_4
);
9068 else if (strncasecmp(value
, "Mod5", strlen("Mod5")) == 0)
9069 update_modkey(XCB_MOD_MASK_5
);
9076 setconfcolor(const char *selector
, const char *value
, int flags
)
9078 int first
, last
, i
= 0, num_screens
;
9080 num_screens
= get_screen_count();
9082 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
9083 if (selector
== NULL
|| strlen(selector
) == 0 ||
9084 (last
= atoi(selector
) - 1) < 0) {
9086 last
= num_screens
- 1;
9091 if (last
>= num_screens
) {
9092 add_startup_exception("invalid screen index: %d out of bounds "
9093 "(maximum %d)", last
+ 1, num_screens
);
9097 for (i
= first
; i
<= last
; ++i
) {
9098 setscreencolor(value
, i
, flags
);
9101 * When setting focus/unfocus colors, we need to also
9102 * set maximize colors to match if they haven't been customized.
9104 if (flags
== SWM_S_COLOR_FOCUS
&&
9105 !screens
[i
].c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].manual
)
9106 setscreencolor(value
, i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
9107 else if (flags
== SWM_S_COLOR_UNFOCUS
&&
9108 !screens
[i
].c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].manual
)
9109 setscreencolor(value
, i
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
9111 screens
[i
].c
[flags
].manual
= 1;
9118 setconfregion(const char *selector
, const char *value
, int flags
)
9120 /* suppress unused warnings since vars are needed */
9124 custom_region(value
);
9129 setautorun(const char *selector
, const char *value
, int flags
)
9133 char *ap
, *sp
, *str
;
9139 /* suppress unused warnings since vars are needed */
9143 if (getenv("SWM_STARTED"))
9147 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9148 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
9150 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9151 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
9153 sp
= str
= expand_tilde((char *)&s
);
9156 * This is a little intricate
9158 * If the pid already exists we simply reuse it because it means it was
9159 * used before AND not claimed by manage_window. We get away with
9160 * altering it in the parent after INSERT because this can not be a race
9163 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
9166 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
9168 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
9169 err(1, "setautorun: realloc");
9170 a
.argv
[argc
- 1] = ap
;
9173 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
9174 err(1, "setautorun: realloc");
9175 a
.argv
[argc
] = NULL
;
9177 if ((pid
= fork()) == 0) {
9178 spawn(ws_id
, &a
, true);
9188 p
= calloc(1, sizeof *p
);
9191 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
9201 setlayout(const char *selector
, const char *value
, int flags
)
9203 struct workspace
*ws
;
9204 int ws_id
, i
, x
, mg
, ma
, si
, ar
;
9205 int st
= SWM_V_STACK
, num_screens
;
9209 /* suppress unused warnings since vars are needed */
9213 if (getenv("SWM_STARTED"))
9217 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
9218 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
9219 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9220 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9223 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9224 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
9226 if (strcasecmp(s
, "vertical") == 0)
9228 else if (strcasecmp(s
, "vertical_flip") == 0) {
9231 } else if (strcasecmp(s
, "horizontal") == 0)
9233 else if (strcasecmp(s
, "horizontal_flip") == 0) {
9236 } else if (strcasecmp(s
, "fullscreen") == 0)
9239 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9240 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9243 num_screens
= get_screen_count();
9244 for (i
= 0; i
< num_screens
; i
++) {
9245 ws
= (struct workspace
*)&screens
[i
].ws
;
9246 ws
[ws_id
].cur_layout
= &layouts
[st
];
9248 ws
[ws_id
].always_raise
= (ar
!= 0);
9249 if (st
== SWM_MAX_STACK
)
9253 for (x
= 0; x
< abs(mg
); x
++) {
9254 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9255 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
9256 SWM_ARG_ID_MASTERSHRINK
);
9259 for (x
= 0; x
< abs(ma
); x
++) {
9260 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9261 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
9262 SWM_ARG_ID_MASTERDEL
);
9265 for (x
= 0; x
< abs(si
); x
++) {
9266 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9267 si
>= 0 ? SWM_ARG_ID_STACKINC
:
9268 SWM_ARG_ID_STACKDEC
);
9272 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9273 SWM_ARG_ID_FLIPLAYOUT
);
9280 /* config options */
9281 struct config_option
{
9283 int (*func
)(const char*, const char*, int);
9286 struct config_option configopt
[] = {
9287 { "autorun", setautorun
, 0 },
9288 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
9289 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
9290 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
9291 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
9292 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
9293 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
9294 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
9295 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
9296 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
9297 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
9298 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
9299 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
9300 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
9301 { "bind", setconfbinding
, 0 },
9302 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
9303 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
9304 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
9305 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
9306 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
9307 { "color_focus_maximized", setconfcolor
, SWM_S_COLOR_FOCUS_MAXIMIZED
},
9308 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
9309 { "color_unfocus_maximized", setconfcolor
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
},
9310 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
9311 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
9312 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
9313 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
9314 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
9315 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
9316 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
9317 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
9318 { "iconic_enabled", setconfvalue
, SWM_S_ICONIC_ENABLED
},
9319 { "java_workaround", setconfvalue
, SWM_S_JAVA_WORKAROUND
},
9320 { "keyboard_mapping", setkeymapping
, 0 },
9321 { "layout", setlayout
, 0 },
9322 { "maximize_hide_bar", setconfvalue
, SWM_S_MAXIMIZE_HIDE_BAR
},
9323 { "modkey", setconfmodkey
, 0 },
9324 { "program", setconfspawn
, 0 },
9325 { "quirk", setconfquirk
, 0 },
9326 { "region", setconfregion
, 0 },
9327 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
9328 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
9329 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
9330 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
9331 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
9332 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
9333 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
9334 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
9335 { "title_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
}, /* For backwards compat. */
9336 { "title_name_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
}, /* For backwards compat. */
9337 { "urgent_collapse", setconfvalue
, SWM_S_URGENT_COLLAPSE
},
9338 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
9339 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
9340 { "warp_focus", setconfvalue
, SWM_S_WARP_FOCUS
},
9341 { "warp_pointer", setconfvalue
, SWM_S_WARP_POINTER
},
9342 { "window_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
},
9343 { "window_instance_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
},
9344 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
9345 { "workspace_clamp", setconfvalue
, SWM_S_WORKSPACE_CLAMP
},
9346 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
9347 { "name", setconfvalue
, SWM_S_WORKSPACE_NAME
},
9351 _add_startup_exception(const char *fmt
, va_list ap
)
9353 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
9354 warn("%s: asprintf", __func__
);
9358 add_startup_exception(const char *fmt
, ...)
9364 if (startup_exception
)
9367 /* force bar to be enabled due to exception */
9371 _add_startup_exception(fmt
, ap
);
9376 conf_load(const char *filename
, int keymapping
)
9379 char *line
= NULL
, *cp
, *ce
, *optsub
, *optval
= NULL
;
9380 size_t linelen
, lineno
= 0;
9381 int wordlen
, i
, optidx
, count
;
9382 struct config_option
*opt
= NULL
;
9384 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
9386 if (filename
== NULL
) {
9387 warnx("conf_load: no filename");
9391 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
9393 if ((config
= fopen(filename
, "r")) == NULL
) {
9394 warn("conf_load: fopen: %s", filename
);
9398 while (!feof(config
)) {
9402 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
9403 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
9405 err(1, "%s", filename
);
9410 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9411 if (cp
[0] == '\0') {
9415 /* get config option */
9416 wordlen
= strcspn(cp
, "=[ \t\n");
9418 add_startup_exception("%s: line %zd: no option found",
9423 for (i
= 0; i
< LENGTH(configopt
); i
++) {
9424 opt
= &configopt
[i
];
9425 if (strncasecmp(cp
, opt
->optname
, wordlen
) == 0 &&
9426 (int)strlen(opt
->optname
) == wordlen
) {
9432 add_startup_exception("%s: line %zd: unknown option "
9433 "%.*s", filename
, lineno
, wordlen
, cp
);
9436 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
9437 add_startup_exception("%s: line %zd: invalid option "
9438 "%.*s", filename
, lineno
, wordlen
, cp
);
9442 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9444 /* get [selector] if any */
9449 /* Get length of selector. */
9450 for (ce
= ++cp
; *ce
!= '\0'; ++ce
) {
9451 /* Find matching (not escaped) bracket. */
9452 if (*ce
== ']' && *(ce
- 1) != '\\') {
9459 add_startup_exception("%s: line %zd: syntax "
9460 "error: unterminated selector", filename
,
9465 /* ce points at ']'; terminate optsub. */
9470 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
9473 if (strlen(optval
) == 0) {
9474 add_startup_exception("%s: line %zd: must supply value "
9475 "to %s", filename
, lineno
,
9476 configopt
[optidx
].optname
);
9479 /* call function to deal with it all */
9480 if (configopt
[optidx
].func(optsub
, optval
,
9481 configopt
[optidx
].funcflags
) != 0) {
9482 add_startup_exception("%s: line %zd: invalid data for "
9483 "%s", filename
, lineno
, configopt
[optidx
].optname
);
9492 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
9498 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
9500 struct ws_win
*parent
, *w
;
9501 struct swm_region
*r
;
9502 struct workspace
*ws
;
9503 xcb_icccm_wm_hints_t wmh
;
9505 parent
= find_window(win
->transient
);
9507 parent
->focus_child
= win
;
9509 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
9510 " for %#x trans %#x\n", win
->id
, win
->transient
);
9512 r
= root_to_region(win
->s
->root
, SWM_CK_ALL
);
9514 /* parent doen't exist in our window list */
9515 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
9516 if (xcb_icccm_get_wm_hints_reply(conn
,
9517 xcb_icccm_get_wm_hints(conn
, w
->id
),
9519 warnx("can't get hints for %#x", w
->id
);
9523 if (win
->hints
.window_group
!= wmh
.window_group
)
9526 w
->focus_child
= win
;
9527 win
->transient
= w
->id
;
9529 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
9530 "transient to %#x\n", win
->transient
);
9537 window_get_pid(xcb_window_t win
)
9542 xcb_get_property_cookie_t pc
;
9543 xcb_get_property_reply_t
*pr
;
9545 apid
= get_atom_from_string("_NET_WM_PID");
9546 if (apid
== XCB_ATOM_NONE
)
9549 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
9550 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9553 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
9558 if (pr
->type
== apid
&& pr
->format
== 32)
9559 ret
= *((pid_t
*)xcb_get_property_value(pr
));
9565 apid
= get_atom_from_string("_SWM_PID");
9566 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
9568 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9571 if (pr
->type
!= apid
) {
9576 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
9583 get_swm_ws(xcb_window_t id
)
9589 xcb_get_property_reply_t
*gpr
;
9591 gpr
= xcb_get_property_reply(conn
,
9592 xcb_get_property(conn
, 0, id
, a_swm_ws
,
9593 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
9598 proplen
= xcb_get_property_value_length(gpr
);
9600 prop
= malloc(proplen
+ 1);
9603 xcb_get_property_value(gpr
),
9605 prop
[proplen
] = '\0';
9612 DNPRINTF(SWM_D_PROP
, "get_swm_ws: _SWM_WS: %s\n", prop
);
9613 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
9615 DNPRINTF(SWM_D_PROP
, "get_swm_ws: win #%s: %s",
9625 get_ws_idx(struct ws_win
*win
)
9627 xcb_get_property_reply_t
*gpr
;
9633 gpr
= xcb_get_property_reply(conn
,
9634 xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
9635 XCB_ATOM_CARDINAL
, 0, 1),
9638 if (gpr
->type
== XCB_ATOM_CARDINAL
&& gpr
->format
== 32)
9639 ws_idx
= *((int *)xcb_get_property_value(gpr
));
9643 if (ws_idx
== -1 && !(win
->quirks
& SWM_Q_IGNORESPAWNWS
))
9644 ws_idx
= get_swm_ws(win
->id
);
9646 if (ws_idx
> workspace_limit
- 1 || ws_idx
< -1)
9649 DNPRINTF(SWM_D_PROP
, "get_ws_idx: win %#x, ws_idx: %d\n", win
->id
,
9656 reparent_window(struct ws_win
*win
)
9658 xcb_void_cookie_t c
;
9659 xcb_generic_error_t
*error
;
9662 win
->frame
= xcb_generate_id(conn
);
9664 DNPRINTF(SWM_D_MISC
, "reparent_window: win %#x, frame: %#x\n",
9665 win
->id
, win
->frame
);
9668 XCB_EVENT_MASK_ENTER_WINDOW
|
9669 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
9670 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
9671 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
|
9672 XCB_EVENT_MASK_EXPOSURE
;
9674 wa
[0] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
9677 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
->frame
, win
->s
->root
,
9678 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), 0,
9679 XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
9680 XCB_CW_EVENT_MASK
, wa
);
9682 win
->state
= SWM_WIN_STATE_REPARENTING
;
9683 c
= xcb_reparent_window_checked(conn
, win
->id
, win
->frame
, 0, 0);
9684 if ((error
= xcb_request_check(conn
, c
))) {
9685 DNPRINTF(SWM_D_MISC
, "reparent_window: error:\n");
9690 xcb_destroy_window(conn
, win
->frame
);
9691 win
->frame
= XCB_WINDOW_NONE
;
9693 xcb_change_save_set(conn
, XCB_SET_MODE_INSERT
, win
->id
);
9695 DNPRINTF(SWM_D_MISC
, "reparent_window: done.\n");
9699 unparent_window(struct ws_win
*win
)
9701 xcb_change_save_set(conn
, XCB_SET_MODE_DELETE
, win
->id
);
9702 xcb_reparent_window(conn
, win
->id
, win
->s
->root
, X(win
), Y(win
));
9703 xcb_destroy_window(conn
, win
->frame
);
9704 win
->frame
= XCB_WINDOW_NONE
;
9705 win
->state
= SWM_WIN_STATE_UNPARENTING
;
9709 manage_window(xcb_window_t id
, int spawn_pos
, bool mapping
)
9711 struct ws_win
*win
, *ww
;
9712 struct swm_region
*r
;
9715 xcb_get_geometry_reply_t
*gr
;
9716 xcb_get_window_attributes_reply_t
*war
= NULL
;
9717 xcb_window_t trans
= XCB_WINDOW_NONE
;
9718 uint32_t i
, wa
[1], new_flags
;
9719 int ws_idx
, force_ws
= -1;
9720 char *class, *instance
, *name
;
9723 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region bar; "
9728 if (find_region(id
)) {
9729 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region window; "
9734 if ((win
= find_window(id
)) != NULL
) {
9735 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x already "
9736 "managed; skipping.)\n", id
);
9737 return (win
); /* Already managed. */
9740 /* See if window is on the unmanaged list. */
9741 if ((win
= find_unmanaged_window(id
)) != NULL
) {
9742 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x found on "
9743 "unmanaged list.\n", id
);
9744 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
9747 set_child_transient(win
, &trans
);
9751 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is new.\n", id
);
9754 war
= xcb_get_window_attributes_reply(conn
,
9755 xcb_get_window_attributes(conn
, id
), NULL
);
9757 DNPRINTF(SWM_D_EVENT
, "manage_window: window lost.\n");
9761 if (war
->override_redirect
) {
9762 DNPRINTF(SWM_D_EVENT
, "manage_window: override_redirect; "
9767 if (!mapping
&& war
->map_state
== XCB_MAP_STATE_UNMAPPED
&&
9768 get_win_state(id
) == XCB_ICCCM_WM_STATE_WITHDRAWN
) {
9769 DNPRINTF(SWM_D_EVENT
, "manage_window: window withdrawn; "
9774 /* Try to get initial window geometry. */
9775 gr
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, id
), NULL
);
9777 DNPRINTF(SWM_D_MISC
, "manage_window: get geometry failed.\n");
9781 /* Create and initialize ws_win object. */
9782 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
9783 err(1, "manage_window: calloc: failed to allocate memory for "
9788 /* Figureout which region the window belongs to. */
9789 r
= root_to_region(gr
->root
, SWM_CK_ALL
);
9791 /* Ignore window border if there is one. */
9792 WIDTH(win
) = gr
->width
;
9793 HEIGHT(win
) = gr
->height
;
9794 X(win
) = gr
->x
+ gr
->border_width
;
9795 Y(win
) = gr
->y
+ gr
->border_width
;
9796 win
->bordered
= false;
9797 win
->mapped
= (war
->map_state
!= XCB_MAP_STATE_UNMAPPED
);
9798 win
->s
= r
->s
; /* this never changes */
9802 /* Select which X events to monitor and set border pixel color. */
9803 wa
[0] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
9804 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
9806 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_EVENT_MASK
, wa
);
9808 /* Get WM_SIZE_HINTS. */
9809 xcb_icccm_get_wm_normal_hints_reply(conn
,
9810 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
9814 xcb_icccm_get_wm_hints_reply(conn
,
9815 xcb_icccm_get_wm_hints(conn
, win
->id
),
9818 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
9819 xcb_icccm_get_wm_transient_for_reply(conn
,
9820 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
9823 win
->transient
= trans
;
9824 set_child_transient(win
, &win
->transient
);
9827 /* Get WM_PROTOCOLS. */
9828 get_wm_protocols(win
);
9831 /* Must be after getting WM_HINTS and WM_PROTOCOLS. */
9832 print_win_input_model(win
);
9835 /* Set initial quirks based on EWMH. */
9836 ewmh_autoquirk(win
);
9838 /* Determine initial quirks. */
9839 xcb_icccm_get_wm_class_reply(conn
,
9840 xcb_icccm_get_wm_class(conn
, win
->id
),
9843 class = win
->ch
.class_name
? win
->ch
.class_name
: "";
9844 instance
= win
->ch
.instance_name
? win
->ch
.instance_name
: "";
9845 name
= get_win_name(win
->id
);
9847 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, instance: %s, "
9848 "name: %s\n", class, instance
, name
);
9850 /* java is retarded so treat it special */
9851 if (strstr(instance
, "sun-awt")) {
9852 DNPRINTF(SWM_D_CLASS
, "manage_window: java window detected.\n");
9856 TAILQ_FOREACH(qp
, &quirks
, entry
) {
9857 if (regexec(&qp
->regex_class
, class, 0, NULL
, 0) == 0 &&
9858 regexec(&qp
->regex_instance
, instance
, 0, NULL
, 0) == 0 &&
9859 regexec(&qp
->regex_name
, name
, 0, NULL
, 0) == 0) {
9860 DNPRINTF(SWM_D_CLASS
, "manage_window: matched "
9861 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp
->class,
9862 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
9863 win
->quirks
= qp
->quirk
;
9864 if (qp
->ws
>= 0 && qp
->ws
< workspace_limit
)
9871 /* Reset font sizes (the bruteforce way; no default keybinding). */
9872 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
9873 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9874 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
9875 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9876 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
9879 /* Figure out which workspace the window belongs to. */
9880 if (!(win
->quirks
& SWM_Q_IGNOREPID
) &&
9881 (p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
9882 win
->ws
= &r
->s
->ws
[p
->ws
];
9883 TAILQ_REMOVE(&pidlist
, p
, entry
);
9886 } else if ((ws_idx
= get_ws_idx(win
)) != -1 &&
9888 /* _SWM_WS is set; use that. */
9889 win
->ws
= &r
->s
->ws
[ws_idx
];
9890 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
9891 /* Launch transients in the same ws as parent. */
9898 win
->ws
= &r
->s
->ws
[force_ws
];
9900 /* Set the _NET_WM_DESKTOP atom. */
9901 DNPRINTF(SWM_D_PROP
, "manage_window: set _NET_WM_DESKTOP: %d\n",
9903 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
9904 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &win
->ws
->idx
);
9906 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
9907 xcb_delete_property(conn
, win
->id
, a_swm_ws
);
9909 /* WS must already be set for this to work. */
9910 store_float_geom(win
);
9912 /* Make sure window is positioned inside its region, if its active. */
9914 region_containment(win
, r
, SWM_CW_ALLSIDES
|
9915 SWM_CW_HARDBOUNDARY
);
9920 /* Figure out where to insert the window in the workspace list. */
9921 if (trans
&& (ww
= find_window(trans
)))
9922 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
9923 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_ABOVE
)
9924 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
9926 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_BELOW
)
9927 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
9928 else switch (spawn_pos
) {
9931 case SWM_STACK_ABOVE
:
9932 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
9934 case SWM_STACK_BOTTOM
:
9935 case SWM_STACK_BELOW
:
9936 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
9939 ewmh_update_client_list();
9941 TAILQ_INSERT_HEAD(&win
->ws
->stack
, win
, stack_entry
);
9944 /* Get/apply initial _NET_WM_STATE */
9945 ewmh_get_wm_state(win
);
9948 new_flags
= win
->ewmh_flags
;
9950 if (win
->quirks
& SWM_Q_FLOAT
)
9951 new_flags
|= EWMH_F_ABOVE
;
9953 if (win
->quirks
& SWM_Q_ANYWHERE
)
9954 new_flags
|= SWM_F_MANUAL
;
9956 ewmh_apply_flags(win
, new_flags
);
9957 ewmh_update_wm_state(win
);
9959 /* Set initial _NET_WM_ALLOWED_ACTIONS */
9960 ewmh_update_actions(win
);
9962 reparent_window(win
);
9964 DNPRINTF(SWM_D_MISC
, "manage_window: done. win %#x, (x,y) w x h: "
9965 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win
->id
,
9966 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
9967 YESNO(ICONIC(win
)), win
->transient
);
9974 free_window(struct ws_win
*win
)
9976 DNPRINTF(SWM_D_MISC
, "free_window: win %#x\n", WINID(win
));
9981 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
9984 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
9987 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
9991 unmanage_window(struct ws_win
*win
)
9993 DNPRINTF(SWM_D_MISC
, "unmanage_window: win %#x\n", WINID(win
));
9999 unparent_window(win
);
10001 TAILQ_REMOVE(&win
->ws
->stack
, win
, stack_entry
);
10002 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
10003 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
10005 ewmh_update_client_list();
10009 expose(xcb_expose_event_t
*e
)
10014 struct workspace
*ws
;
10017 DNPRINTF(SWM_D_EVENT
, "expose: win %#x, count: %d\n", e
->window
,
10023 if ((b
= find_bar(e
->window
))) {
10026 } else if ((w
= find_window(e
->window
)) && w
->frame
== e
->window
) {
10030 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
10036 DNPRINTF(SWM_D_EVENT
, "expose: done\n");
10041 focusin(xcb_focus_in_event_t
*e
)
10043 DNPRINTF(SWM_D_EVENT
, "focusin: win %#x, mode: %s(%u), "
10044 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10045 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10049 focusout(xcb_focus_out_event_t
*e
)
10051 DNPRINTF(SWM_D_EVENT
, "focusout: win %#x, mode: %s(%u), "
10052 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10053 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10058 keypress(xcb_key_press_event_t
*e
)
10061 struct binding
*bp
;
10062 xcb_keysym_t keysym
;
10063 bool replay
= true;
10065 last_event_time
= e
->time
;
10067 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
10069 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
10070 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10071 "state: %u, cleaned: %u, same_screen: %s\n", keysym
, e
->event
,
10072 e
->event_x
, e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
,
10073 e
->root_y
, e
->child
, e
->state
, CLEANMASK(e
->state
),
10074 YESNO(e
->same_screen
));
10076 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10078 /* Look for catch-all. */
10079 if ((bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
)) == NULL
)
10083 replay
= bp
->flags
& BINDING_F_REPLAY
;
10085 if ((ap
= &actions
[bp
->action
]) == NULL
)
10088 if (bp
->action
== FN_SPAWN_CUSTOM
)
10089 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10092 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10094 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10098 DNPRINTF(SWM_D_EVENT
, "keypress: replaying.\n");
10099 /* Pass keypress to event window and unfreeze keyboard queue. */
10100 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10102 /* Release freeze. */
10103 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10107 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
10111 keyrelease(xcb_key_release_event_t
*e
)
10114 struct binding
*bp
;
10115 xcb_keysym_t keysym
;
10117 last_event_time
= e
->time
;
10119 keysym
= xcb_key_release_lookup_keysym(syms
, e
, 0);
10121 DNPRINTF(SWM_D_EVENT
, "keyrelease: keysym: %u, win (x,y): %#x (%d,%d), "
10122 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10123 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
10124 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
10125 e
->child
, e
->state
, YESNO(e
->same_screen
));
10127 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10129 /* Look for catch-all. */
10130 bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
);
10132 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10133 bp
->flags
& BINDING_F_REPLAY
) {
10134 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10135 DNPRINTF(SWM_D_EVENT
, "keyrelease: replaying.\n");
10137 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10138 DNPRINTF(SWM_D_EVENT
, "keyrelease: unfreezing.\n");
10143 DNPRINTF(SWM_D_EVENT
, "keyrelease: done.\n");
10147 buttonpress(xcb_button_press_event_t
*e
)
10149 struct ws_win
*win
= NULL
;
10150 struct swm_region
*r
, *old_r
;
10152 struct binding
*bp
;
10153 bool replay
= true;
10155 last_event_time
= e
->time
;
10157 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): %#x (%d,%d), "
10158 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10159 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10160 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10161 e
->state
, YESNO(e
->same_screen
));
10163 if (e
->event
== e
->root
) {
10165 win
= find_window(e
->child
);
10167 /* Focus on empty region */
10168 /* If no windows on region if its empty. */
10169 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10170 if (r
&& TAILQ_EMPTY(&r
->ws
->winlist
)) {
10171 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
10172 if (old_r
&& old_r
!= r
)
10173 unfocus_win(old_r
->ws
->focus
);
10175 DNPRINTF(SWM_D_FOCUS
, "buttonpress: "
10176 "set_input_focus: %#x, revert-to: parent, "
10177 "time: %#x\n", e
->root
, e
->time
);
10178 xcb_set_input_focus(conn
,
10179 XCB_INPUT_FOCUS_POINTER_ROOT
,
10182 /* Clear bar since empty. */
10185 /* No need to replay event. */
10190 win
= find_window(e
->event
);
10194 focus_win(get_focus_magic(win
));
10196 /* Handle any bound action. */
10197 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10199 /* Look for catch-all. */
10200 if ((bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
)) == NULL
)
10205 replay
= bp
->flags
& BINDING_F_REPLAY
;
10207 if ((ap
= &actions
[bp
->action
]) == NULL
)
10210 if (bp
->action
== FN_SPAWN_CUSTOM
)
10211 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10214 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10216 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10220 DNPRINTF(SWM_D_EVENT
, "buttonpress: replaying.\n");
10221 /* Replay event to event window */
10222 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10224 /* Unfreeze grab events. */
10225 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10232 buttonrelease(xcb_button_release_event_t
*e
)
10235 struct binding
*bp
;
10237 last_event_time
= e
->time
;
10239 DNPRINTF(SWM_D_EVENT
, "buttonrelease: win (x,y): %#x (%d,%d), "
10240 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10241 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10242 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10243 e
->state
, YESNO(e
->same_screen
));
10245 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10247 /* Look for catch-all. */
10248 bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
);
10250 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10251 bp
->flags
& BINDING_F_REPLAY
) {
10252 DNPRINTF(SWM_D_EVENT
, "buttonrelease: replaying.\n");
10253 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10255 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10263 print_win_input_model(struct ws_win
*win
)
10267 * Input Model Input Field WM_TAKE_FOCUS
10268 * No Input False Absent
10269 * Passive True Absent
10270 * Locally Active True Present
10271 * Globally Active False Present
10274 if (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) || win
->hints
.input
)
10275 inputmodel
= (win
->take_focus
) ? "Locally Active" : "Passive";
10277 inputmodel
= (win
->take_focus
) ? "Globally Active" : "No Input";
10279 DNPRINTF(SWM_D_FOCUS
, "print_win_input_model: win %#x, model: %s\n",
10280 win
->id
, inputmodel
);
10284 print_win_geom(xcb_window_t w
)
10286 xcb_get_geometry_reply_t
*wa
;
10288 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
10290 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: %#x\n",
10295 DNPRINTF(SWM_D_MISC
, "print_win_geom: win %#x, root: %#x, "
10296 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
10297 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
10306 get_stack_mode_name(uint8_t mode
)
10311 case XCB_STACK_MODE_ABOVE
:
10314 case XCB_STACK_MODE_BELOW
:
10317 case XCB_STACK_MODE_TOP_IF
:
10320 case XCB_STACK_MODE_BOTTOM_IF
:
10323 case XCB_STACK_MODE_OPPOSITE
:
10335 configurerequest(xcb_configure_request_event_t
*e
)
10337 struct ws_win
*win
;
10338 struct swm_region
*r
= NULL
;
10340 uint32_t wc
[7] = {0};
10344 if ((win
= find_window(e
->window
)) == NULL
)
10345 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10349 if (swm_debug
& SWM_D_EVENT
) {
10350 print_win_geom(e
->window
);
10352 DNPRINTF(SWM_D_EVENT
, "configurerequest: win %#x, "
10353 "parent: %#x, new: %s, value_mask: %u { ", e
->window
,
10354 e
->parent
, YESNO(new), e
->value_mask
);
10355 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
10356 DPRINTF("X: %d ", e
->x
);
10357 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
10358 DPRINTF("Y: %d ", e
->y
);
10359 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10360 DPRINTF("W: %u ", e
->width
);
10361 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10362 DPRINTF("H: %u ", e
->height
);
10363 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
10364 DPRINTF("Border: %u ", e
->border_width
);
10365 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
10366 DPRINTF("Sibling: %#x ", e
->sibling
);
10367 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
10368 DPRINTF("StackMode: %s(%u) ",
10369 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
10375 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10376 mask
|= XCB_CONFIG_WINDOW_X
;
10379 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10380 mask
|= XCB_CONFIG_WINDOW_Y
;
10383 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
10384 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
10385 wc
[i
++] = e
->width
;
10387 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
10388 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
10389 wc
[i
++] = e
->height
;
10391 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
10392 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
10393 wc
[i
++] = e
->border_width
;
10395 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
10396 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
10397 wc
[i
++] = e
->sibling
;
10399 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
10400 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
10401 wc
[i
++] = e
->stack_mode
;
10405 xcb_configure_window(conn
, e
->window
, mask
, wc
);
10408 } else if ((!MANUAL(win
) || win
->quirks
& SWM_Q_ANYWHERE
) &&
10409 !FULLSCREEN(win
) && !MAXIMIZED(win
)) {
10412 else if (win
->ws
->old_r
)
10413 r
= win
->ws
->old_r
;
10415 /* windows are centered unless ANYWHERE quirk is set. */
10416 if (win
->quirks
& SWM_Q_ANYWHERE
) {
10417 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10418 win
->g_float
.x
= e
->x
;
10420 win
->g_float
.x
-= X(r
);
10423 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10424 win
->g_float
.y
= e
->y
;
10426 win
->g_float
.y
-= Y(r
);
10430 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10431 win
->g_float
.w
= e
->width
;
10433 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10434 win
->g_float
.h
= e
->height
;
10436 win
->g_floatvalid
= true;
10438 if (!MAXIMIZED(win
) && !FULLSCREEN(win
) &&
10439 (TRANS(win
) || (ABOVE(win
) &&
10440 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]))) {
10441 WIDTH(win
) = win
->g_float
.w
;
10442 HEIGHT(win
) = win
->g_float
.h
;
10445 update_floater(win
);
10448 config_win(win
, e
);
10452 config_win(win
, e
);
10456 config_win(win
, e
);
10460 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
10464 configurenotify(xcb_configure_notify_event_t
*e
)
10466 struct ws_win
*win
;
10468 DNPRINTF(SWM_D_EVENT
, "configurenotify: win %#x, event win: %#x, "
10469 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
10470 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
10471 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
10472 YESNO(e
->override_redirect
));
10474 win
= find_window(e
->window
);
10477 if (font_adjusted
&& win
->ws
->r
) {
10485 destroynotify(xcb_destroy_notify_event_t
*e
)
10487 struct ws_win
*win
;
10488 struct workspace
*ws
;
10490 DNPRINTF(SWM_D_EVENT
, "destroynotify: win %#x\n", e
->window
);
10492 if ((win
= find_window(e
->window
)) == NULL
) {
10493 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10495 /* Window is on unmanaged list. */
10496 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10503 if (win
->frame
== e
->window
) {
10504 DNPRINTF(SWM_D_EVENT
, "destroynotify: frame for win %#x\n",
10506 win
->frame
= XCB_WINDOW_NONE
;
10510 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10511 /* If we were focused, make sure we focus on something else. */
10512 if (win
== ws
->focus
) {
10513 ws
->focus_pending
= get_focus_prev(win
);
10514 if (ws
->focus_pending
== win
)
10515 ws
->focus_pending
= NULL
;
10519 unmanage_window(win
);
10520 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10524 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
10525 if (ws
->focus_pending
) {
10526 focus_win(ws
->focus_pending
);
10527 ws
->focus_pending
= NULL
;
10528 } else if (ws
->focus
== NULL
) {
10529 DNPRINTF(SWM_D_FOCUS
, "destroynotify: set_input_focus: "
10530 "%#x, revert-to: parent, time: 0\n", ws
->r
->id
);
10531 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
10532 ws
->r
->id
, XCB_CURRENT_TIME
);
10539 DNPRINTF(SWM_D_EVENT
, "destroynotify: done.\n");
10544 get_notify_detail_label(uint8_t detail
)
10549 case XCB_NOTIFY_DETAIL_ANCESTOR
:
10550 label
= "Ancestor";
10552 case XCB_NOTIFY_DETAIL_VIRTUAL
:
10555 case XCB_NOTIFY_DETAIL_INFERIOR
:
10556 label
= "Inferior";
10558 case XCB_NOTIFY_DETAIL_NONLINEAR
:
10559 label
= "Nonlinear";
10561 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
10562 label
= "NonlinearVirtual";
10564 case XCB_NOTIFY_DETAIL_POINTER
:
10567 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
10568 label
= "PointerRoot";
10570 case XCB_NOTIFY_DETAIL_NONE
:
10581 get_notify_mode_label(uint8_t mode
)
10586 case XCB_NOTIFY_MODE_NORMAL
:
10589 case XCB_NOTIFY_MODE_GRAB
:
10592 case XCB_NOTIFY_MODE_UNGRAB
:
10595 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
10596 label
= "WhileGrabbed";
10606 get_state_mask_label(uint16_t state
)
10611 case XCB_KEY_BUT_MASK_SHIFT
:
10612 label
= "ShiftMask";
10614 case XCB_KEY_BUT_MASK_LOCK
:
10615 label
= "LockMask";
10617 case XCB_KEY_BUT_MASK_CONTROL
:
10618 label
= "ControlMask";
10620 case XCB_KEY_BUT_MASK_MOD_1
:
10621 label
= "Mod1Mask";
10623 case XCB_KEY_BUT_MASK_MOD_2
:
10624 label
= "Mod2Mask";
10626 case XCB_KEY_BUT_MASK_MOD_3
:
10627 label
= "Mod3Mask";
10629 case XCB_KEY_BUT_MASK_MOD_4
:
10630 label
= "Mod4Mask";
10632 case XCB_KEY_BUT_MASK_MOD_5
:
10633 label
= "Mod5Mask";
10635 case XCB_KEY_BUT_MASK_BUTTON_1
:
10636 label
= "Button1Mask";
10638 case XCB_KEY_BUT_MASK_BUTTON_2
:
10639 label
= "Button2Mask";
10641 case XCB_KEY_BUT_MASK_BUTTON_3
:
10642 label
= "Button3Mask";
10644 case XCB_KEY_BUT_MASK_BUTTON_4
:
10645 label
= "Button4Mask";
10647 case XCB_KEY_BUT_MASK_BUTTON_5
:
10648 label
= "Button5Mask";
10662 enternotify(xcb_enter_notify_event_t
*e
)
10664 struct ws_win
*win
;
10665 struct swm_region
*r
;
10667 last_event_time
= e
->time
;
10669 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): %#x "
10670 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10671 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10672 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10673 get_notify_mode_label(e
->mode
), e
->mode
,
10674 get_notify_detail_label(e
->detail
), e
->detail
,
10675 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10676 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10679 if (e
->event
== e
->root
&& e
->child
== XCB_WINDOW_NONE
&&
10680 e
->mode
== XCB_NOTIFY_MODE_GRAB
&&
10681 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10682 DNPRINTF(SWM_D_EVENT
, "enternotify: grab inferior; ignoring.\n");
10686 if (focus_mode
== SWM_FOCUS_MANUAL
&&
10687 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10688 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
10692 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10693 e
->mode
== XCB_NOTIFY_MODE_UNGRAB
&&
10694 e
->detail
!= XCB_NOTIFY_DETAIL_ANCESTOR
) {
10695 DNPRINTF(SWM_D_EVENT
, "enternotify: ungrab; ignoring.\n");
10699 if ((win
= find_window(e
->event
)) == NULL
) {
10700 if (e
->event
== e
->root
) {
10701 /* If no windows on pointer region, then focus root. */
10702 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10704 DNPRINTF(SWM_D_EVENT
, "enternotify: "
10705 "NULL region; ignoring.\n");
10711 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
10716 if (e
->mode
== XCB_NOTIFY_MODE_NORMAL
&&
10717 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10718 DNPRINTF(SWM_D_EVENT
, "enternotify: entering from "
10719 "inferior; ignoring\n");
10723 focus_win(get_focus_magic(win
));
10726 DNPRINTF(SWM_D_EVENT
, "enternotify: done\n");
10733 leavenotify(xcb_leave_notify_event_t
*e
)
10735 last_event_time
= e
->time
;
10737 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): %#x "
10738 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10739 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10740 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10741 get_notify_mode_label(e
->mode
), e
->mode
,
10742 get_notify_detail_label(e
->detail
), e
->detail
,
10743 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10744 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10750 mapnotify(xcb_map_notify_event_t
*e
)
10752 struct ws_win
*win
, *parent
= NULL
;
10753 struct workspace
*ws
;
10755 DNPRINTF(SWM_D_EVENT
, "mapnotify: win %#x\n", e
->window
);
10757 if ((win
= manage_window(e
->window
, spawn_position
, false)) == NULL
)
10761 if (win
->state
== SWM_WIN_STATE_REPARENTING
)
10764 if (ws
->r
== NULL
) {
10769 /* Need to know if win was mapped due to ws switch. */
10770 if (ws
->state
== SWM_WS_STATE_MAPPED
) {
10771 if (ws
->focus_pending
&& TRANS(ws
->focus_pending
))
10772 parent
= find_window(win
->transient
);
10774 /* If window's parent is maximized, don't clear it. */
10775 if ((parent
== NULL
) || !MAXIMIZED(parent
))
10776 if (clear_maximized(ws
) > 0)
10780 win
->mapped
= true;
10781 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
10783 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
10784 if (ws
->focus_pending
== win
) {
10786 ws
->focus_pending
= NULL
;
10787 center_pointer(ws
->r
);
10793 DNPRINTF(SWM_D_EVENT
, "mapnotify: done\n");
10799 mappingnotify(xcb_mapping_notify_event_t
*e
)
10801 if (e
->request
!= XCB_MAPPING_POINTER
) {
10802 xcb_refresh_keyboard_mapping(syms
, e
);
10810 maprequest(xcb_map_request_event_t
*e
)
10812 struct ws_win
*win
, *w
= NULL
;
10813 xcb_get_window_attributes_reply_t
*war
;
10815 DNPRINTF(SWM_D_EVENT
, "maprequest: win %#x\n",
10818 war
= xcb_get_window_attributes_reply(conn
,
10819 xcb_get_window_attributes(conn
, e
->window
),
10822 DNPRINTF(SWM_D_EVENT
, "maprequest: window lost.\n");
10826 if (war
->override_redirect
) {
10827 DNPRINTF(SWM_D_EVENT
, "maprequest: override_redirect; "
10832 win
= manage_window(e
->window
, spawn_position
, true);
10836 /* The new window should get focus; prepare. */
10837 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10838 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
) &&
10839 (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
10840 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
10841 win
->hints
.input
))) {
10842 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
10843 /* See if other wins of same type are already mapped. */
10844 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
10845 if (w
== win
|| !w
->mapped
)
10848 if (w
->ch
.class_name
&&
10849 win
->ch
.class_name
&&
10850 strcmp(w
->ch
.class_name
,
10851 win
->ch
.class_name
) == 0 &&
10852 w
->ch
.instance_name
&&
10853 win
->ch
.instance_name
&&
10854 strcmp(w
->ch
.instance_name
,
10855 win
->ch
.instance_name
) == 0)
10861 win
->ws
->focus_pending
= get_focus_magic(win
);
10864 /* All windows need to be mapped if they are in the current workspace.*/
10867 /* Ignore EnterNotify to handle the mapnotify without interference. */
10868 if (focus_mode
== SWM_FOCUS_DEFAULT
)
10869 event_drain(XCB_ENTER_NOTIFY
);
10872 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
10876 motionnotify(xcb_motion_notify_event_t
*e
)
10878 struct swm_region
*r
= NULL
;
10879 int i
, num_screens
;
10881 last_event_time
= e
->time
;
10883 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): %#x "
10884 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10885 "child: %#x, same_screen_focus: %s, state: %d\n",
10886 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10887 get_notify_detail_label(e
->detail
), e
->detail
,
10888 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10889 YESNO(e
->same_screen
), e
->state
);
10891 if (focus_mode
== SWM_FOCUS_MANUAL
)
10894 num_screens
= get_screen_count();
10895 for (i
= 0; i
< num_screens
; i
++)
10896 if (screens
[i
].root
== e
->root
)
10899 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10900 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
10901 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
10909 get_atom_name(xcb_atom_t atom
)
10912 #ifdef SWM_DEBUG_ATOM_NAMES
10914 * This should be disabled during most debugging since
10915 * xcb_get_* causes an xcb_flush.
10918 xcb_get_atom_name_reply_t
*r
;
10920 r
= xcb_get_atom_name_reply(conn
,
10921 xcb_get_atom_name(conn
, atom
),
10924 len
= xcb_get_atom_name_name_length(r
);
10926 name
= malloc(len
+ 1);
10928 memcpy(name
, xcb_get_atom_name_name(r
), len
);
10942 propertynotify(xcb_property_notify_event_t
*e
)
10944 struct ws_win
*win
;
10945 struct workspace
*ws
;
10949 name
= get_atom_name(e
->atom
);
10950 DNPRINTF(SWM_D_EVENT
, "propertynotify: win %#x, atom: %s(%u), "
10951 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
10955 last_event_time
= e
->time
;
10957 win
= find_window(e
->window
);
10963 if (e
->atom
== a_state
) {
10964 /* State just changed, make sure it gets focused if mapped. */
10965 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
10966 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
10968 win
->state
== SWM_WIN_STATE_REPARENTED
&&
10969 ws
->focus_pending
== win
) {
10970 focus_win(ws
->focus_pending
);
10971 ws
->focus_pending
= NULL
;
10975 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
10976 e
->atom
== XCB_ATOM_WM_NAME
) {
10978 bar_draw(ws
->r
->bar
);
10979 } else if (e
->atom
== a_prot
) {
10980 get_wm_protocols(win
);
10981 } else if (e
->atom
== XCB_ATOM_WM_NORMAL_HINTS
) {
10982 xcb_icccm_get_wm_normal_hints_reply(conn
,
10983 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
10991 reparentnotify(xcb_reparent_notify_event_t
*e
)
10993 struct ws_win
*win
;
10995 DNPRINTF(SWM_D_EVENT
, "reparentnotify: event: %#x, win %#x, "
10996 "parent: %#x, (x,y): (%u,%u), override_redirect: %u\n",
10997 e
->event
, e
->window
, e
->parent
, e
->x
, e
->y
, e
->override_redirect
);
10999 win
= find_window(e
->window
);
11001 if (win
->state
== SWM_WIN_STATE_REPARENTING
) {
11002 win
->state
= SWM_WIN_STATE_REPARENTED
;
11009 update_window(win
);
11010 update_win_stacking(win
);
11011 } else if (win
->state
== SWM_WIN_STATE_UNPARENTING
) {
11012 win
->state
= SWM_WIN_STATE_UNPARENTED
;
11018 unmapnotify(xcb_unmap_notify_event_t
*e
)
11020 struct ws_win
*win
;
11021 struct workspace
*ws
;
11023 DNPRINTF(SWM_D_EVENT
, "unmapnotify: win %#x\n", e
->window
);
11025 /* If we aren't managing the window, then ignore. */
11026 win
= find_window(e
->window
);
11027 if (win
== NULL
|| win
->id
!= e
->window
) {
11028 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore unmanaged.\n");
11032 /* Do nothing if already withdrawn. */
11033 if (!win
->mapped
&& !ICONIC(win
)) {
11034 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore withdrawn.\n");
11039 win
->mapped
= false;
11041 /* Ignore if reparenting-related. */
11042 if (win
->state
!= SWM_WIN_STATE_REPARENTED
) {
11043 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore not reparented.\n");
11047 /* If win was focused, make sure to focus on something else. */
11048 if (win
== ws
->focus
) {
11049 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
11050 ws
->focus_pending
= get_focus_prev(win
);
11051 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
11052 "focus_pending: %#x\n",
11053 WINID(ws
->focus_pending
));
11061 DNPRINTF(SWM_D_EVENT
, "unmapnotify: iconify.\n");
11062 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
11065 DNPRINTF(SWM_D_EVENT
, "unmapnotify: withdraw.\n");
11066 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
11067 unmanage_window(win
);
11072 /* Update focus if ws is active. */
11073 if (WS_FOCUSED(ws
)) {
11074 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
11075 focus_win(get_pointer_win(ws
->r
->s
->root
));
11076 } else if (ws
->focus_pending
) {
11077 focus_win(ws
->focus_pending
);
11078 ws
->focus_pending
= NULL
;
11079 } else if (ws
->focus
== NULL
) {
11080 DNPRINTF(SWM_D_FOCUS
, "unmapnotify: set_input_focus: "
11081 "%#x, revert-to: parent, time: 0\n",
11083 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11084 ws
->r
->id
, XCB_CURRENT_TIME
);
11088 center_pointer(ws
->r
);
11090 DNPRINTF(SWM_D_EVENT
, "unmapnotify: done.\n");
11095 get_source_type_label(uint32_t type
)
11100 case EWMH_SOURCE_TYPE_NONE
:
11103 case EWMH_SOURCE_TYPE_NORMAL
:
11106 case EWMH_SOURCE_TYPE_OTHER
:
11118 clientmessage(xcb_client_message_event_t
*e
)
11120 struct ws_win
*win
;
11121 struct swm_region
*r
= NULL
;
11124 int num_screens
, i
;
11125 xcb_map_request_event_t mre
;
11129 name
= get_atom_name(e
->type
);
11130 DNPRINTF(SWM_D_EVENT
, "clientmessage: win %#x, atom: %s(%u)\n",
11131 e
->window
, name
, e
->type
);
11135 if (e
->type
== ewmh
[_NET_CURRENT_DESKTOP
].atom
) {
11136 num_screens
= get_screen_count();
11137 for (i
= 0; i
< num_screens
; i
++)
11138 if (screens
[i
].root
== e
->window
) {
11139 r
= screens
[i
].r_focus
;
11143 if (r
&& e
->data
.data32
[0] < (uint32_t)workspace_limit
) {
11144 a
.id
= e
->data
.data32
[0];
11145 switchws(NULL
, r
, &a
);
11150 } else if (e
->type
== ewmh
[_NET_REQUEST_FRAME_EXTENTS
].atom
) {
11151 DNPRINTF(SWM_D_EVENT
,
11152 "clientmessage: set _NET_FRAME_EXTENTS on window.\n");
11153 vals
[0] = vals
[1] = vals
[2] = vals
[3] = border_width
;
11154 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, e
->window
,
11155 a_net_frame_extents
, XCB_ATOM_CARDINAL
, 32, 4, vals
);
11160 win
= find_window(e
->window
);
11162 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11163 /* Manage the window with maprequest. */
11164 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
11165 "unmanaged window.\n");
11166 mre
.window
= e
->window
;
11172 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11173 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW, "
11174 "source_type: %s(%d)\n",
11175 get_source_type_label(e
->data
.data32
[0]),
11176 e
->data
.data32
[0]);
11179 * Allow focus changes that are a result of direct user
11180 * action and from applications that use the old EWMH spec.
11182 if (e
->data
.data32
[0] != EWMH_SOURCE_TYPE_NORMAL
||
11183 win
->quirks
& SWM_Q_OBEYAPPFOCUSREQ
) {
11184 if (WS_FOCUSED(win
->ws
))
11187 win
->ws
->focus_pending
= win
;
11189 } else if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
11190 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
11191 if (win
->can_delete
)
11192 client_msg(win
, a_delete
, 0);
11194 xcb_kill_client(conn
, win
->id
);
11195 } else if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
11196 DNPRINTF(SWM_D_EVENT
,
11197 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
11199 if (e
->data
.data32
[0] & (1<<8)) /* x */
11200 X(win
) = e
->data
.data32
[1];
11201 if (e
->data
.data32
[0] & (1<<9)) /* y */
11202 Y(win
) = e
->data
.data32
[2];
11203 if (e
->data
.data32
[0] & (1<<10)) /* width */
11204 WIDTH(win
) = e
->data
.data32
[3];
11205 if (e
->data
.data32
[0] & (1<<11)) /* height */
11206 HEIGHT(win
) = e
->data
.data32
[4];
11208 update_window(win
);
11210 /* Notify no change was made. */
11211 config_win(win
, NULL
);
11212 /* TODO: Change stack sizes */
11214 } else if (e
->type
== ewmh
[_NET_RESTACK_WINDOW
].atom
) {
11215 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_RESTACK_WINDOW\n");
11216 vals
[0] = e
->data
.data32
[1]; /* Sibling window. */
11217 vals
[1] = e
->data
.data32
[2]; /* Stack mode detail. */
11219 if (win
->frame
!= XCB_WINDOW_NONE
)
11220 xcb_configure_window(conn
, win
->frame
,
11221 XCB_CONFIG_WINDOW_SIBLING
|
11222 XCB_CONFIG_WINDOW_STACK_MODE
, vals
);
11223 } else if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
11224 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
11225 ewmh_change_wm_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
11226 if (e
->data
.data32
[2])
11227 ewmh_change_wm_state(win
, e
->data
.data32
[2],
11228 e
->data
.data32
[0]);
11230 ewmh_update_wm_state(win
);
11232 } else if (e
->type
== ewmh
[_NET_WM_DESKTOP
].atom
) {
11233 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_DESKTOP\n");
11236 win_to_ws(win
, e
->data
.data32
[0], true);
11238 /* Stack source and destination ws, if mapped. */
11239 if (r
!= win
->ws
->r
) {
11245 load_float_geom(win
);
11258 int errcode
= xcb_connection_has_error(conn
);
11259 #ifdef XCB_CONN_ERROR
11262 case XCB_CONN_ERROR
:
11263 s
= "Socket error, pipe error or other stream error.";
11265 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
11266 s
= "Extension not supported.";
11268 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
11269 s
= "Insufficient memory.";
11271 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
11272 s
= "Request length was exceeded.";
11274 case XCB_CONN_CLOSED_PARSE_ERR
:
11275 s
= "Error parsing display string.";
11278 s
= "Unknown error.";
11281 errx(errcode
, "X CONNECTION ERROR: %s", s
);
11284 errx(errcode
, "X CONNECTION ERROR");
11291 int num_screens
, i
;
11292 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
11293 XCB_EVENT_MASK_ENTER_WINDOW
|
11294 XCB_EVENT_MASK_OWNER_GRAB_BUTTON
|
11295 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
11296 XCB_EVENT_MASK_ENTER_WINDOW
|
11297 XCB_EVENT_MASK_LEAVE_WINDOW
|
11298 XCB_EVENT_MASK_BUTTON_PRESS
|
11299 XCB_EVENT_MASK_BUTTON_RELEASE
|
11300 XCB_EVENT_MASK_KEY_PRESS
|
11301 XCB_EVENT_MASK_KEY_RELEASE
|
11302 XCB_EVENT_MASK_PROPERTY_CHANGE
;
11304 xcb_void_cookie_t wac
;
11305 xcb_generic_error_t
*error
;
11307 /* this causes an error if some other window manager is running */
11308 num_screens
= get_screen_count();
11309 for (i
= 0; i
< num_screens
; i
++) {
11310 if ((sc
= get_screen(i
)) == NULL
)
11311 errx(1, "ERROR: can't get screen %d.", i
);
11312 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: %#x\n",
11314 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
11315 XCB_CW_EVENT_MASK
, &val
);
11316 if ((error
= xcb_request_check(conn
, wac
))) {
11317 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
11318 error
->error_code
);
11328 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
11330 struct swm_region
*r
= NULL
, *n
;
11331 struct workspace
*ws
= NULL
;
11335 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
11336 s
->idx
, w
, h
, x
, y
);
11338 /* remove any conflicting regions */
11339 n
= TAILQ_FIRST(&s
->rl
);
11342 n
= TAILQ_NEXT(r
, entry
);
11343 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
11344 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
11345 if (r
->ws
->r
!= NULL
)
11346 r
->ws
->old_r
= r
->ws
->r
;
11349 xcb_destroy_window(conn
, r
->id
);
11350 r
->id
= XCB_WINDOW_NONE
;
11351 TAILQ_REMOVE(&s
->rl
, r
, entry
);
11352 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
11356 /* search old regions for one to reuse */
11358 /* size + location match */
11359 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11360 if (X(r
) == x
&& Y(r
) == y
&&
11361 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11365 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11366 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11370 TAILQ_REMOVE(&s
->orl
, r
, entry
);
11371 /* try to use old region's workspace */
11372 if (r
->ws
->r
== NULL
)
11375 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
11376 err(1, "new_region: calloc: failed to allocate memory "
11379 /* if we don't have a workspace already, find one */
11381 for (i
= 0; i
< workspace_limit
; i
++)
11382 if (s
->ws
[i
].r
== NULL
) {
11389 errx(1, "new_region: no free workspaces");
11391 if (ws
->state
== SWM_WS_STATE_HIDDEN
)
11392 ws
->state
= SWM_WS_STATE_MAPPING
;
11401 r
->ws_prior
= NULL
;
11404 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
11406 /* Invisible region window to detect pointer events on empty regions. */
11407 r
->id
= xcb_generate_id(conn
);
11408 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
11409 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
11411 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
11412 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
11413 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
11415 /* Make sure region input is at the bottom. */
11416 wa
[0] = XCB_STACK_MODE_BELOW
;
11417 xcb_configure_window(conn
, r
->id
, XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
11419 xcb_map_window(conn
, r
->id
);
11423 scan_randr(int idx
)
11425 #ifdef SWM_XRR_HAS_CRTC
11428 #endif /* SWM_XRR_HAS_CRTC */
11429 struct swm_region
*r
;
11430 struct ws_win
*win
;
11432 xcb_randr_get_screen_resources_current_cookie_t src
;
11433 xcb_randr_get_screen_resources_current_reply_t
*srr
;
11434 xcb_randr_get_crtc_info_cookie_t cic
;
11435 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
11436 xcb_randr_crtc_t
*crtc
;
11437 xcb_screen_t
*screen
;
11439 DNPRINTF(SWM_D_MISC
, "scan_randr: screen: %d\n", idx
);
11441 if ((screen
= get_screen(idx
)) == NULL
)
11442 errx(1, "ERROR: can't get screen %d.", idx
);
11444 num_screens
= get_screen_count();
11445 if (idx
>= num_screens
)
11446 errx(1, "scan_randr: invalid screen");
11448 /* remove any old regions */
11449 while ((r
= TAILQ_FIRST(&screens
[idx
].rl
)) != NULL
) {
11450 r
->ws
->old_r
= r
->ws
->r
= NULL
;
11452 xcb_destroy_window(conn
, r
->id
);
11453 r
->id
= XCB_WINDOW_NONE
;
11454 TAILQ_REMOVE(&screens
[idx
].rl
, r
, entry
);
11455 TAILQ_INSERT_TAIL(&screens
[idx
].orl
, r
, entry
);
11459 /* map virtual screens onto physical screens */
11460 #ifdef SWM_XRR_HAS_CRTC
11461 if (randr_support
) {
11462 src
= xcb_randr_get_screen_resources_current(conn
,
11463 screens
[idx
].root
);
11464 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
11467 new_region(&screens
[idx
], 0, 0,
11468 screen
->width_in_pixels
,
11469 screen
->height_in_pixels
);
11472 ncrtc
= srr
->num_crtcs
;
11474 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
11475 for (c
= 0; c
< ncrtc
; c
++) {
11476 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
11478 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
11481 if (cir
->num_outputs
== 0) {
11486 if (cir
->mode
== 0)
11487 new_region(&screens
[idx
], 0, 0,
11488 screen
->width_in_pixels
,
11489 screen
->height_in_pixels
);
11491 new_region(&screens
[idx
],
11492 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
11497 #endif /* SWM_XRR_HAS_CRTC */
11499 /* If detection failed, create a single region that spans the screen. */
11500 if (TAILQ_EMPTY(&screens
[idx
].rl
))
11501 new_region(&screens
[idx
], 0, 0, screen
->width_in_pixels
,
11502 screen
->height_in_pixels
);
11505 /* Cleanup unused previously visible workspaces. */
11506 TAILQ_FOREACH(r
, &screens
[idx
].orl
, entry
) {
11507 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
11509 r
->ws
->state
= SWM_WS_STATE_HIDDEN
;
11511 /* The screen shouldn't focus on an unused region. */
11512 if (screens
[idx
].r_focus
== r
)
11513 screens
[idx
].r_focus
= NULL
;
11516 DNPRINTF(SWM_D_MISC
, "scan_randr: done.\n");
11520 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
11522 struct swm_region
*r
;
11523 int i
, num_screens
;
11525 DNPRINTF(SWM_D_EVENT
, "screenchange: root: %#x\n", e
->root
);
11527 num_screens
= get_screen_count();
11528 /* silly event doesn't include the screen index */
11529 for (i
= 0; i
< num_screens
; i
++)
11530 if (screens
[i
].root
== e
->root
)
11532 if (i
>= num_screens
)
11533 errx(1, "screenchange: screen not found");
11535 /* brute force for now, just re-enumerate the regions */
11539 print_win_geom(e
->root
);
11541 /* add bars to all regions */
11542 for (i
= 0; i
< num_screens
; i
++) {
11543 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11547 /* Stack all regions. */
11548 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11551 /* Make sure a region has focus on each screen. */
11552 for (i
= 0; i
< num_screens
; i
++) {
11553 if (screens
[i
].r_focus
== NULL
) {
11554 r
= TAILQ_FIRST(&screens
[i
].rl
);
11562 /* Update workspace state and bar on all regions. */
11563 for (i
= 0; i
< num_screens
; i
++)
11564 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11565 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
11573 struct swm_region
*r
= NULL
;
11574 xcb_query_tree_cookie_t qtc
;
11575 xcb_query_tree_reply_t
*qtr
;
11576 xcb_get_property_cookie_t pc
;
11577 xcb_get_property_reply_t
*pr
;
11578 xcb_window_t
*wins
= NULL
, trans
, *cwins
= NULL
;
11579 int i
, j
, k
, n
, no
, num_screens
;
11581 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
11582 num_screens
= get_screen_count();
11583 for (i
= 0; i
< num_screens
; i
++) {
11584 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
11585 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
11588 wins
= xcb_query_tree_children(qtr
);
11589 no
= xcb_query_tree_children_length(qtr
);
11591 /* Try to sort windows according to _NET_CLIENT_LIST. */
11592 pr
= xcb_get_property_reply(conn
, xcb_get_property(conn
, 0,
11593 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
11594 XCB_ATOM_WINDOW
, 0, UINT32_MAX
), NULL
);
11596 cwins
= xcb_get_property_value(pr
);
11597 n
= xcb_get_property_value_length(pr
) /
11598 sizeof(xcb_atom_t
);
11600 for (j
= 0; j
< n
; ++j
) {
11601 for (k
= j
; k
< no
; ++k
) {
11602 if (wins
[k
] == cwins
[j
]) {
11603 /* Swap wins j and k. */
11605 wins
[j
] = cwins
[j
];
11613 /* Manage top-level windows first, then transients. */
11614 /* TODO: allow transients to be managed before leader. */
11615 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top-level windows.\n");
11616 for (j
= 0; j
< no
; j
++) {
11617 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11618 if (r
->id
== wins
[j
]) {
11619 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11620 "skip %#x; region input window.\n",
11623 } else if (r
->bar
->id
== wins
[j
]) {
11624 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11625 "skip %#x; region bar.\n",
11634 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11635 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11637 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
11638 "is transient for %#x.\n", wins
[j
], trans
);
11642 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11645 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
11646 for (j
= 0; j
< no
; j
++) {
11647 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11648 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11650 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11654 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
11658 setup_screens(void)
11660 int i
, j
, k
, num_screens
;
11661 struct workspace
*ws
;
11662 uint32_t gcv
[1], wa
[1];
11663 const xcb_query_extension_reply_t
*qep
;
11664 xcb_screen_t
*screen
;
11665 xcb_randr_query_version_cookie_t c
;
11666 xcb_randr_query_version_reply_t
*r
;
11668 num_screens
= get_screen_count();
11669 if ((screens
= calloc(num_screens
, sizeof(struct swm_screen
))) == NULL
)
11670 err(1, "setup_screens: calloc: failed to allocate memory for "
11673 /* Initial RandR setup. */
11674 randr_support
= false;
11675 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
11676 if (qep
->present
) {
11677 c
= xcb_randr_query_version(conn
, 1, 1);
11678 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
11680 if (r
->major_version
>= 1) {
11681 randr_support
= true;
11682 randr_eventbase
= qep
->first_event
;
11688 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
11690 /* map physical screens */
11691 for (i
= 0; i
< num_screens
; i
++) {
11692 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
11693 screens
[i
].idx
= i
;
11694 screens
[i
].r_focus
= NULL
;
11696 TAILQ_INIT(&screens
[i
].rl
);
11697 TAILQ_INIT(&screens
[i
].orl
);
11698 if ((screen
= get_screen(i
)) == NULL
)
11699 errx(1, "ERROR: can't get screen %d.", i
);
11700 screens
[i
].root
= screen
->root
;
11702 /* set default colors */
11703 setscreencolor("red", i
, SWM_S_COLOR_FOCUS
);
11704 setscreencolor("rgb:88/88/88", i
, SWM_S_COLOR_UNFOCUS
);
11705 setscreencolor("rgb:00/80/80", i
, SWM_S_COLOR_BAR_BORDER
);
11706 setscreencolor("rgb:00/40/40", i
,
11707 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
11708 setscreencolor("black", i
, SWM_S_COLOR_BAR
);
11709 setscreencolor("rgb:a0/a0/a0", i
, SWM_S_COLOR_BAR_FONT
);
11710 setscreencolor("red", i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
11711 setscreencolor("rgb:88/88/88", i
,
11712 SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
11714 /* create graphics context on screen */
11715 screens
[i
].bar_gc
= xcb_generate_id(conn
);
11717 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
11718 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
11720 /* set default cursor */
11721 xcb_change_window_attributes(conn
, screens
[i
].root
,
11722 XCB_CW_CURSOR
, wa
);
11724 /* init all workspaces */
11725 /* XXX these should be dynamically allocated too */
11726 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
11727 ws
= &screens
[i
].ws
[j
];
11730 ws
->bar_enabled
= true;
11732 ws
->focus_prev
= NULL
;
11733 ws
->focus_pending
= NULL
;
11736 ws
->state
= SWM_WS_STATE_HIDDEN
;
11737 TAILQ_INIT(&ws
->stack
);
11738 TAILQ_INIT(&ws
->winlist
);
11739 TAILQ_INIT(&ws
->unmanagedlist
);
11741 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
11742 if (layouts
[k
].l_config
!= NULL
)
11743 layouts
[k
].l_config(ws
,
11744 SWM_ARG_ID_STACKINIT
);
11745 ws
->cur_layout
= &layouts
[0];
11746 ws
->cur_layout
->l_string(ws
);
11752 xcb_randr_select_input(conn
, screens
[i
].root
,
11753 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
11758 setup_globals(void)
11760 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
11761 err(1, "setup_globals: strdup: failed to allocate memory.");
11763 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
11764 err(1, "setup_globals: strdup: failed to allocate memory.");
11766 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
11767 errx(1, "unable to allocate key symbols");
11769 a_state
= get_atom_from_string("WM_STATE");
11770 a_prot
= get_atom_from_string("WM_PROTOCOLS");
11771 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
11772 a_net_frame_extents
= get_atom_from_string("_NET_FRAME_EXTENTS");
11773 a_net_supported
= get_atom_from_string("_NET_SUPPORTED");
11774 a_net_wm_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
11775 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
11776 a_utf8_string
= get_atom_from_string("UTF8_STRING");
11777 a_swm_ws
= get_atom_from_string("_SWM_WS");
11781 shutdown_cleanup(void)
11783 struct swm_region
*r
;
11785 struct workspace
*ws
;
11786 int i
, num_screens
;
11788 /* disable alarm because the following code may not be interrupted */
11790 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
11791 err(1, "can't disable alarm");
11804 num_screens
= get_screen_count();
11805 for (i
= 0; i
< num_screens
; ++i
) {
11808 DNPRINTF(SWM_D_FOCUS
, "shutdown_cleanup: set_input_focus: "
11809 "%#x, revert-to: root, time: 0\n", screens
[i
].root
);
11810 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11811 screens
[i
].root
, XCB_CURRENT_TIME
);
11813 if (screens
[i
].bar_gc
!= XCB_NONE
)
11814 xcb_free_gc(conn
, screens
[i
].bar_gc
);
11815 if (!bar_font_legacy
) {
11816 XftColorFree(display
, DefaultVisual(display
, i
),
11817 DefaultColormap(display
, i
), &bar_font_color
);
11818 XftColorFree(display
, DefaultVisual(display
, i
),
11819 DefaultColormap(display
, i
), &search_font_color
);
11822 for (j
= 0; j
< SWM_S_COLOR_MAX
; ++j
) {
11823 free(screens
[i
].c
[j
].name
);
11826 /* Free window memory. */
11827 for (j
= 0; j
< SWM_WS_MAX
; ++j
) {
11828 ws
= &screens
[i
].ws
[j
];
11831 while ((w
= TAILQ_FIRST(&ws
->winlist
)) != NULL
) {
11832 TAILQ_REMOVE(&ws
->winlist
, w
, entry
);
11836 while ((w
= TAILQ_FIRST(&ws
->unmanagedlist
)) != NULL
) {
11837 TAILQ_REMOVE(&ws
->unmanagedlist
, w
, entry
);
11842 /* Free region memory. */
11843 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
11844 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
11849 while ((r
= TAILQ_FIRST(&screens
[i
].orl
)) != NULL
) {
11850 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
11859 free(clock_format
);
11860 free(startup_exception
);
11863 XFreeFontSet(display
, bar_fs
);
11865 XftFontClose(display
, bar_font
);
11867 xcb_key_symbols_free(syms
);
11869 xcb_aux_sync(conn
);
11870 xcb_disconnect(conn
);
11874 event_error(xcb_generic_error_t
*e
)
11878 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
11879 "resource_id: %u, minor_code: %u\n",
11880 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
11881 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
11882 e
->sequence
, e
->resource_id
, e
->minor_code
);
11886 event_handle(xcb_generic_event_t
*evt
)
11888 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
11890 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
11891 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
11892 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
11895 #define EVENT(type, callback) case type: callback((void *)evt); return
11896 EVENT(0, event_error
);
11897 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
11898 EVENT(XCB_BUTTON_RELEASE
, buttonrelease
);
11899 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
11900 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
11901 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
11902 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
11903 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
11904 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
11905 /*EVENT(XCB_CREATE_NOTIFY, );*/
11906 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
11907 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
11908 EVENT(XCB_EXPOSE
, expose
);
11910 EVENT(XCB_FOCUS_IN
, focusin
);
11911 EVENT(XCB_FOCUS_OUT
, focusout
);
11913 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
11914 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
11915 EVENT(XCB_KEY_PRESS
, keypress
);
11916 EVENT(XCB_KEY_RELEASE
, keyrelease
);
11917 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
11919 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
11921 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
11922 EVENT(XCB_MAP_REQUEST
, maprequest
);
11923 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
11924 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
11925 /*EVENT(XCB_NO_EXPOSURE, );*/
11926 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
11927 EVENT(XCB_REPARENT_NOTIFY
, reparentnotify
);
11928 /*EVENT(XCB_RESIZE_REQUEST, );*/
11929 /*EVENT(XCB_SELECTION_CLEAR, );*/
11930 /*EVENT(XCB_SELECTION_NOTIFY, );*/
11931 /*EVENT(XCB_SELECTION_REQUEST, );*/
11932 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
11933 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
11936 if (type
- randr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
11937 screenchange((void *)evt
);
11941 main(int argc
, char *argv
[])
11943 struct pollfd pfd
[2];
11944 struct sigaction sact
;
11946 struct passwd
*pwd
;
11947 struct swm_region
*r
;
11948 xcb_generic_event_t
*evt
;
11949 int xfd
, i
, num_screens
, num_readable
;
11950 char conf
[PATH_MAX
], *cfile
= NULL
;
11951 bool stdin_ready
= false, startup
= true;
11953 /* suppress unused warning since var is needed */
11957 time_started
= time(NULL
);
11961 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
11962 if (setlocale(LC_CTYPE
, "") == NULL
|| setlocale(LC_TIME
, "") == NULL
)
11963 warnx("no locale support");
11965 /* handle some signals */
11966 bzero(&sact
, sizeof(sact
));
11967 sigemptyset(&sact
.sa_mask
);
11969 sact
.sa_handler
= sighdlr
;
11970 sigaction(SIGINT
, &sact
, NULL
);
11971 sigaction(SIGQUIT
, &sact
, NULL
);
11972 sigaction(SIGTERM
, &sact
, NULL
);
11973 sigaction(SIGHUP
, &sact
, NULL
);
11975 sact
.sa_handler
= sighdlr
;
11976 sact
.sa_flags
= SA_NOCLDSTOP
;
11977 sigaction(SIGCHLD
, &sact
, NULL
);
11979 if ((display
= XOpenDisplay(0)) == NULL
)
11980 errx(1, "can not open display");
11982 conn
= XGetXCBConnection(display
);
11983 if (xcb_connection_has_error(conn
))
11984 errx(1, "can not get XCB connection");
11986 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
11988 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
11989 xfd
= xcb_get_file_descriptor(conn
);
11991 /* look for local and global conf file */
11992 pwd
= getpwuid(getuid());
11994 errx(1, "invalid user: %d", getuid());
11996 xcb_grab_server(conn
);
11997 xcb_aux_sync(conn
);
11999 /* flush all events */
12000 while ((evt
= get_next_event(false))) {
12001 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
12007 errx(1, "another window manager is currently running");
12009 /* Load Xcursors and/or cursorfont glyph cursors. */
12012 xcb_aux_sync(conn
);
12017 setup_keybindings();
12018 setup_btnbindings();
12023 for (i
= 0; ; i
++) {
12028 snprintf(conf
, sizeof conf
, "%s/.%s",
12029 pwd
->pw_dir
, SWM_CONF_FILE
);
12033 snprintf(conf
, sizeof conf
, "/etc/%s",
12038 snprintf(conf
, sizeof conf
, "%s/.%s",
12039 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
12042 /* global compat */
12043 snprintf(conf
, sizeof conf
, "/etc/%s",
12044 SWM_CONF_FILE_OLD
);
12050 if (strlen(conf
) && stat(conf
, &sb
) != -1)
12051 if (S_ISREG(sb
.st_mode
)) {
12058 /* load conf (if any) */
12060 conf_load(cfile
, SWM_CONF_DEFAULT
);
12064 if (getenv("SWM_STARTED") == NULL
)
12065 setenv("SWM_STARTED", "YES", 1);
12067 /* setup all bars */
12068 num_screens
= get_screen_count();
12069 for (i
= 0; i
< num_screens
; i
++)
12070 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12073 /* Manage existing windows. */
12079 /* Stack all regions to trigger mapping. */
12080 for (i
= 0; i
< num_screens
; i
++)
12081 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12084 xcb_ungrab_server(conn
);
12087 /* Update state and bar of each newly mapped workspace. */
12088 for (i
= 0; i
< num_screens
; i
++)
12089 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
12090 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
12094 memset(&pfd
, 0, sizeof(pfd
));
12096 pfd
[0].events
= POLLIN
;
12097 pfd
[1].fd
= STDIN_FILENO
;
12098 pfd
[1].events
= POLLIN
;
12101 while ((evt
= get_next_event(false))) {
12108 /* If just (re)started, set default focus if needed. */
12112 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
12113 r
= TAILQ_FIRST(&screens
[0].rl
);
12125 num_readable
= poll(pfd
, bar_extra
? 2 : 1, 1000);
12126 if (num_readable
== -1) {
12127 DNPRINTF(SWM_D_MISC
, "poll failed: %s",
12129 } else if (num_readable
> 0 && bar_extra
&&
12130 pfd
[1].revents
& POLLIN
) {
12131 stdin_ready
= true;
12135 restart(NULL
, NULL
, NULL
);
12141 stdin_ready
= false;
12142 bar_extra_update();
12145 /* Need to ensure the bar(s) are always updated. */
12146 for (i
= 0; i
< num_screens
; i
++)
12147 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12153 shutdown_cleanup();