2 * Copyright (c) 2009-2015 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2015 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 * Copyright (c) 2012-2015 David Hill <dhill@mindcry.org>
12 * Copyright (c) 2014-2015 Yuri D'Elia <yuri.delia@eurac.edu>
14 * Permission to use, copy, modify, and distribute this software for any
15 * purpose with or without fee is hereby granted, provided that the above
16 * copyright notice and this permission notice appear in all copies.
18 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
19 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
21 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
23 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
24 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 * Much code and ideas taken from dwm under the following license:
28 * MIT/X Consortium License
30 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
31 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
32 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
33 * 2007 Premysl Hruby <dfenze at gmail dot com>
34 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
35 * 2007 Christof Musik <christof at sendfax dot de>
36 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
37 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
38 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
40 * Permission is hereby granted, free of charge, to any person obtaining a
41 * copy of this software and associated documentation files (the "Software"),
42 * to deal in the Software without restriction, including without limitation
43 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
44 * and/or sell copies of the Software, and to permit persons to whom the
45 * Software is furnished to do so, subject to the following conditions:
47 * The above copyright notice and this permission notice shall be included in
48 * all copies or substantial portions of the Software.
50 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
51 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
52 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
53 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
54 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
55 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
56 * DEALINGS IN THE SOFTWARE.
60 #include <sys/types.h>
64 #include <sys/queue.h>
65 #include <sys/param.h>
66 #include <sys/select.h>
67 #if defined(__linux__)
69 #elif defined(__OpenBSD__)
71 #elif defined(__FreeBSD__)
95 #include <X11/cursorfont.h>
96 #include <X11/extensions/Xrandr.h>
97 #include <X11/Xcursor/Xcursor.h>
98 #include <X11/Xft/Xft.h>
99 #include <X11/Xlib-xcb.h>
100 #include <xcb/xcb_atom.h>
101 #include <xcb/xcb_aux.h>
102 #include <xcb/xcb_event.h>
103 #include <xcb/xcb_icccm.h>
104 #include <xcb/xcb_keysyms.h>
105 #include <xcb/xtest.h>
106 #include <xcb/randr.h>
114 #ifdef SPECTRWM_BUILDSTR
115 static const char *buildstr
= SPECTRWM_BUILDSTR
;
117 static const char *buildstr
= SPECTRWM_VERSION
;
120 #if !defined(__CYGWIN__) /* cygwin chokes on randr stuff */
122 # error RandR versions less than 1.0 are not supported
125 # if RANDR_MAJOR >= 1
126 # if RANDR_MINOR >= 2
127 # define SWM_XRR_HAS_CRTC
130 #endif /* __CYGWIN__ */
132 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
133 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
134 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
135 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
136 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
137 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
138 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
139 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
140 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
141 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
142 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
143 #define xcb_icccm_get_wm_class xcb_get_wm_class
144 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
145 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
146 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
147 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
148 #define xcb_icccm_wm_hints_get_urgency xcb_wm_hints_get_urgency
149 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
150 #define xcb_icccm_get_wm_name xcb_get_wm_name
151 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
152 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
153 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
154 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
155 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
156 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
157 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
158 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
159 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
160 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
163 /*#define SWM_DEBUG*/
165 #define DPRINTF(x...) do { \
167 fprintf(stderr, x); \
169 #define DNPRINTF(n,x...) do { \
170 if (swm_debug & n) { \
171 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
172 fprintf(stderr, x); \
175 #define SWM_D_MISC 0x0001
176 #define SWM_D_EVENT 0x0002
177 #define SWM_D_WS 0x0004
178 #define SWM_D_FOCUS 0x0008
179 #define SWM_D_MOVE 0x0010
180 #define SWM_D_STACK 0x0020
181 #define SWM_D_MOUSE 0x0040
182 #define SWM_D_PROP 0x0080
183 #define SWM_D_CLASS 0x0100
184 #define SWM_D_KEY 0x0200
185 #define SWM_D_QUIRK 0x0400
186 #define SWM_D_SPAWN 0x0800
187 #define SWM_D_EVENTQ 0x1000
188 #define SWM_D_CONF 0x2000
189 #define SWM_D_BAR 0x4000
190 #define SWM_D_INIT 0x8000
192 uint32_t swm_debug
= 0
211 #define DPRINTF(x...)
212 #define DNPRINTF(n,x...)
215 #define SWM_EWMH_ACTION_COUNT_MAX (8)
216 #define EWMH_F_FULLSCREEN (0x001)
217 #define EWMH_F_ABOVE (0x002)
218 #define EWMH_F_HIDDEN (0x004)
219 #define EWMH_F_MAXIMIZED_VERT (0x008)
220 #define EWMH_F_MAXIMIZED_HORZ (0x010)
221 #define EWMH_F_SKIP_PAGER (0x020)
222 #define EWMH_F_SKIP_TASKBAR (0x040)
223 #define SWM_F_MANUAL (0x080)
225 #define EWMH_F_MAXIMIZED (EWMH_F_MAXIMIZED_VERT | EWMH_F_MAXIMIZED_HORZ)
227 /* convert 8-bit to 16-bit */
228 #define RGB_8_TO_16(col) (((col) << 8) + (col))
230 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
231 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
232 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
233 xrc.blue = RGB_8_TO_16((px) & 0xff); \
236 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
237 #define MODKEY XCB_MOD_MASK_1
238 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
239 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
240 XCB_EVENT_MASK_BUTTON_RELEASE)
241 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
242 #define SWM_PROPLEN (16)
243 #define SWM_FUNCNAME_LEN (32)
244 #define SWM_KEYS_LEN (255)
245 #define SWM_QUIRK_LEN (64)
246 #define X(r) ((r)->g.x)
247 #define Y(r) ((r)->g.y)
248 #define WIDTH(r) ((r)->g.w)
249 #define HEIGHT(r) ((r)->g.h)
250 #define BORDER(w) ((w)->bordered ? border_width : 0)
251 #define MAX_X(r) ((r)->g.x + (r)->g.w)
252 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
253 #define SH_MIN(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
254 #define SH_MIN_W(w) ((w)->sh.min_width)
255 #define SH_MIN_H(w) ((w)->sh.min_height)
256 #define SH_MAX(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
257 #define SH_MAX_W(w) ((w)->sh.max_width)
258 #define SH_MAX_H(w) ((w)->sh.max_height)
259 #define SH_INC(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)
260 #define SH_INC_W(w) ((w)->sh.width_inc)
261 #define SH_INC_H(w) ((w)->sh.height_inc)
262 #define SWM_MAX_FONT_STEPS (3)
263 #define WINID(w) ((w) ? (w)->id : XCB_WINDOW_NONE)
264 #define WS_FOCUSED(ws) ((ws)->r && (ws)->r->s->r_focus == (ws)->r)
265 #define YESNO(x) ((x) ? "yes" : "no")
266 #define ICONIC(w) ((w)->ewmh_flags & EWMH_F_HIDDEN)
267 #define ABOVE(w) ((w)->ewmh_flags & EWMH_F_ABOVE)
268 #define FULLSCREEN(w) ((w)->ewmh_flags & EWMH_F_FULLSCREEN)
269 #define MAXIMIZED_VERT(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_VERT)
270 #define MAXIMIZED_HORZ(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_HORZ)
271 #define MAXIMIZED(w) (MAXIMIZED_VERT(w) || MAXIMIZED_HORZ(w))
272 #define MANUAL(w) ((w)->ewmh_flags & SWM_F_MANUAL)
273 #define TRANS(w) ((w)->transient != XCB_WINDOW_NONE)
274 #define FLOATING(w) (ABOVE(w) || TRANS(w) || FULLSCREEN(w) || \
277 /* Constrain Window flags */
278 #define SWM_CW_RESIZABLE (0x01)
279 #define SWM_CW_SOFTBOUNDARY (0x02)
280 #define SWM_CW_HARDBOUNDARY (0x04)
281 #define SWM_CW_RIGHT (0x10)
282 #define SWM_CW_LEFT (0x20)
283 #define SWM_CW_BOTTOM (0x40)
284 #define SWM_CW_TOP (0x80)
285 #define SWM_CW_ALLSIDES (0xf0)
287 #define SWM_FOCUS_DEFAULT (0)
288 #define SWM_FOCUS_FOLLOW (1)
289 #define SWM_FOCUS_MANUAL (2)
291 #define SWM_CK_NONE (0)
292 #define SWM_CK_ALL (0xf)
293 #define SWM_CK_FOCUS (0x1)
294 #define SWM_CK_POINTER (0x2)
295 #define SWM_CK_FALLBACK (0x4)
296 #define SWM_CK_REGION (0x8)
298 #define SWM_G_ALL (0xf)
299 #define SWM_G_SIZE (0x1)
300 #define SWM_G_POS (0x2)
302 #define SWM_CONF_DEFAULT (0)
303 #define SWM_CONF_KEYMAPPING (1)
306 #define SWM_LIB "/usr/local/lib/libswmhack.so"
313 xcb_atom_t a_net_wm_check
;
314 xcb_atom_t a_net_supported
;
315 xcb_atom_t a_takefocus
;
316 xcb_atom_t a_utf8_string
;
318 volatile sig_atomic_t running
= 1;
319 volatile sig_atomic_t restart_wm
= 0;
320 xcb_timestamp_t last_event_time
= 0;
324 unsigned int numlockmask
= 0;
327 xcb_connection_t
*conn
;
328 xcb_key_symbols_t
*syms
;
330 int boundary_width
= 50;
331 bool cycle_empty
= false;
332 bool cycle_visible
= false;
334 int font_adjusted
= 0;
335 unsigned int mod_key
= MODKEY
;
336 bool warp_focus
= false;
337 bool warp_pointer
= false;
338 bool workspace_clamp
= false;
341 struct swm_region
*search_r
;
342 int select_list_pipe
[2];
343 int select_resp_pipe
[2];
345 volatile sig_atomic_t search_resp
;
346 int search_resp_action
;
348 struct search_window
{
349 TAILQ_ENTRY(search_window
) entry
;
353 xcb_window_t indicator
;
355 TAILQ_HEAD(search_winlist
, search_window
);
356 struct search_winlist search_wl
;
361 SWM_SEARCH_UNICONIFY
,
362 SWM_SEARCH_NAME_WORKSPACE
,
363 SWM_SEARCH_SEARCH_WORKSPACE
,
364 SWM_SEARCH_SEARCH_WINDOW
367 #define SWM_STACK_TOP (0)
368 #define SWM_STACK_BOTTOM (1)
369 #define SWM_STACK_ABOVE (2)
370 #define SWM_STACK_BELOW (3)
373 double dialog_ratio
= 0.6;
375 #define SWM_BAR_MAX (256)
376 #define SWM_BAR_JUSTIFY_LEFT (0)
377 #define SWM_BAR_JUSTIFY_CENTER (1)
378 #define SWM_BAR_JUSTIFY_RIGHT (2)
379 #define SWM_BAR_OFFSET (4)
380 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
381 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
382 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
383 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
384 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
386 #ifdef X_HAVE_UTF8_STRING
387 #define DRAWSTRING(x...) Xutf8DrawString(x)
389 #define DRAWSTRING(x...) XmbDrawString(x)
392 char *bar_argv
[] = { NULL
, NULL
};
394 char bar_ext
[SWM_BAR_MAX
];
395 char bar_ext_buf
[SWM_BAR_MAX
];
396 char bar_vertext
[SWM_BAR_MAX
];
397 bool bar_version
= false;
398 bool bar_enabled
= true;
399 int bar_border_width
= 1;
400 bool bar_at_bottom
= false;
401 bool bar_extra
= false;
403 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
404 char *bar_format
= NULL
;
405 bool stack_enabled
= true;
406 bool clock_enabled
= true;
407 bool iconic_enabled
= false;
408 bool maximize_hide_bar
= false;
409 bool urgent_enabled
= false;
410 bool urgent_collapse
= false;
411 char *clock_format
= NULL
;
412 bool window_class_enabled
= false;
413 bool window_instance_enabled
= false;
414 bool window_name_enabled
= false;
415 int focus_mode
= SWM_FOCUS_DEFAULT
;
416 int focus_close
= SWM_STACK_BELOW
;
417 bool focus_close_wrap
= true;
418 int focus_default
= SWM_STACK_TOP
;
419 int spawn_position
= SWM_STACK_TOP
;
420 bool disable_border
= false;
421 int border_width
= 1;
422 int region_padding
= 0;
424 bool java_workaround
= true;
425 bool verbose_layout
= false;
431 XFontSetExtents
*bar_fs_extents
;
433 bool bar_font_legacy
= true;
435 XftColor bar_font_color
;
436 XftColor search_font_color
;
438 char *startup_exception
;
439 unsigned int nr_exceptions
= 0;
441 /* layout manager data */
442 struct swm_geometry
{
455 struct swm_geometry g
;
458 /* virtual "screens" */
460 TAILQ_ENTRY(swm_region
) entry
;
462 struct swm_geometry g
;
463 struct workspace
*ws
; /* current workspace on this region */
464 struct workspace
*ws_prior
; /* prior workspace on this region */
465 struct swm_screen
*s
; /* screen idx */
468 TAILQ_HEAD(swm_region_list
, swm_region
);
471 TAILQ_ENTRY(ws_win
) entry
;
472 TAILQ_ENTRY(ws_win
) stack_entry
;
474 xcb_window_t transient
;
475 struct ws_win
*focus_child
; /* focus on child transient */
476 struct swm_geometry g
; /* current geometry */
477 struct swm_geometry g_float
; /* region coordinates */
478 bool g_floatvalid
; /* g_float geometry validity */
482 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
489 struct workspace
*ws
; /* always valid */
490 struct swm_screen
*s
; /* always valid, never changes */
492 xcb_icccm_get_wm_class_reply_t ch
;
493 xcb_icccm_wm_hints_t hints
;
495 TAILQ_HEAD(ws_win_list
, ws_win
);
496 TAILQ_HEAD(ws_win_stack
, ws_win
);
500 TAILQ_ENTRY(pid_e
) entry
;
504 TAILQ_HEAD(pid_list
, pid_e
);
505 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
507 /* layout handlers */
509 void vertical_config(struct workspace
*, int);
510 void vertical_stack(struct workspace
*, struct swm_geometry
*);
511 void horizontal_config(struct workspace
*, int);
512 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
513 void max_stack(struct workspace
*, struct swm_geometry
*);
514 void plain_stacker(struct workspace
*);
515 void fancy_stacker(struct workspace
*);
518 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
519 void (*l_config
)(struct workspace
*, int);
521 #define SWM_L_FOCUSPREV (1<<0)
522 #define SWM_L_MAPONFOCUS (1<<1)
523 void (*l_string
)(struct workspace
*);
525 /* stack, configure */
526 { vertical_stack
, vertical_config
, 0, plain_stacker
},
527 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
529 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
530 { NULL
, NULL
, 0, NULL
},
533 /* position of max_stack mode in the layouts array, index into layouts! */
534 #define SWM_V_STACK (0)
535 #define SWM_H_STACK (1)
536 #define SWM_MAX_STACK (2)
538 #define SWM_H_SLICE (32)
539 #define SWM_V_SLICE (32)
541 /* define work spaces */
543 int idx
; /* workspace index */
544 char *name
; /* workspace name */
545 bool always_raise
; /* raise windows on focus */
546 bool bar_enabled
; /* bar visibility */
547 struct layout
*cur_layout
; /* current layout handlers */
548 struct ws_win
*focus
; /* may be NULL */
549 struct ws_win
*focus_prev
; /* may be NULL */
550 struct ws_win
*focus_pending
; /* may be NULL */
551 struct swm_region
*r
; /* may be NULL */
552 struct swm_region
*old_r
; /* may be NULL */
553 struct ws_win_list winlist
; /* list of windows in ws */
554 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
555 struct ws_win_stack stack
; /* stacking order */
556 int state
; /* mapping state */
557 char stacker
[10]; /* display stacker and layout */
561 int horizontal_msize
;
563 int horizontal_stacks
;
564 bool horizontal_flip
;
574 SWM_WS_STATE_MAPPING
,
580 SWM_S_COLOR_BAR_BORDER
,
581 SWM_S_COLOR_BAR_BORDER_UNFOCUS
,
582 SWM_S_COLOR_BAR_FONT
,
584 SWM_S_COLOR_FOCUS_MAXIMIZED
,
586 SWM_S_COLOR_UNFOCUS_MAXIMIZED
,
590 /* physical screen mapping */
591 #define SWM_WS_MAX (22) /* hard limit */
592 int workspace_limit
= 10; /* soft limit */
595 int idx
; /* screen index */
596 struct swm_region_list rl
; /* list of regions on this screen */
597 struct swm_region_list orl
; /* list of old regions */
599 struct workspace ws
[SWM_WS_MAX
];
600 struct swm_region
*r_focus
;
607 } c
[SWM_S_COLOR_MAX
];
609 xcb_gcontext_t bar_gc
;
612 struct swm_screen
*screens
;
614 /* args to functions */
617 #define SWM_ARG_ID_FOCUSNEXT (0)
618 #define SWM_ARG_ID_FOCUSPREV (1)
619 #define SWM_ARG_ID_FOCUSMAIN (2)
620 #define SWM_ARG_ID_FOCUSURGENT (3)
621 #define SWM_ARG_ID_SWAPNEXT (10)
622 #define SWM_ARG_ID_SWAPPREV (11)
623 #define SWM_ARG_ID_SWAPMAIN (12)
624 #define SWM_ARG_ID_MOVELAST (13)
625 #define SWM_ARG_ID_MASTERSHRINK (20)
626 #define SWM_ARG_ID_MASTERGROW (21)
627 #define SWM_ARG_ID_MASTERADD (22)
628 #define SWM_ARG_ID_MASTERDEL (23)
629 #define SWM_ARG_ID_FLIPLAYOUT (24)
630 #define SWM_ARG_ID_STACKRESET (30)
631 #define SWM_ARG_ID_STACKINIT (31)
632 #define SWM_ARG_ID_STACKBALANCE (32)
633 #define SWM_ARG_ID_CYCLEWS_UP (40)
634 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
635 #define SWM_ARG_ID_CYCLERG_UP (42)
636 #define SWM_ARG_ID_CYCLERG_DOWN (43)
637 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
638 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
639 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
640 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
641 #define SWM_ARG_ID_STACKINC (50)
642 #define SWM_ARG_ID_STACKDEC (51)
643 #define SWM_ARG_ID_SS_ALL (60)
644 #define SWM_ARG_ID_SS_WINDOW (61)
645 #define SWM_ARG_ID_DONTCENTER (70)
646 #define SWM_ARG_ID_CENTER (71)
647 #define SWM_ARG_ID_KILLWINDOW (80)
648 #define SWM_ARG_ID_DELETEWINDOW (81)
649 #define SWM_ARG_ID_WIDTHGROW (90)
650 #define SWM_ARG_ID_WIDTHSHRINK (91)
651 #define SWM_ARG_ID_HEIGHTGROW (92)
652 #define SWM_ARG_ID_HEIGHTSHRINK (93)
653 #define SWM_ARG_ID_MOVEUP (100)
654 #define SWM_ARG_ID_MOVEDOWN (101)
655 #define SWM_ARG_ID_MOVELEFT (102)
656 #define SWM_ARG_ID_MOVERIGHT (103)
657 #define SWM_ARG_ID_RAISE (105)
658 #define SWM_ARG_ID_LOWER (106)
659 #define SWM_ARG_ID_BAR_TOGGLE (110)
660 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
661 #define SWM_ARG_ID_CYCLERG_MOVE_UP (112)
662 #define SWM_ARG_ID_CYCLERG_MOVE_DOWN (113)
668 TAILQ_ENTRY(quirk
) entry
;
669 char *class; /* WM_CLASS:class */
670 char *instance
; /* WM_CLASS:instance */
671 char *name
; /* WM_NAME */
673 regex_t regex_instance
;
676 int ws
; /* Initial workspace. */
677 #define SWM_Q_FLOAT (1<<0) /* float this window */
678 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
679 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
680 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
681 #define SWM_Q_FULLSCREEN (1<<4) /* remove border when fullscreen */
682 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
683 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
684 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
685 #define SWM_Q_OBEYAPPFOCUSREQ (1<<8) /* Focus when applications ask. */
686 #define SWM_Q_IGNOREPID (1<<9) /* Ignore PID when determining ws. */
687 #define SWM_Q_IGNORESPAWNWS (1<<10) /* Ignore _SWM_WS when managing win. */
688 #define SWM_Q_NOFOCUSCYCLE (1<<11) /* Remove from normal focus cycle. */
689 #define SWM_Q_MINIMALBORDER (1<<12) /* Remove border when floating/unfocused */
691 TAILQ_HEAD(quirk_list
, quirk
);
692 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
695 * Supported EWMH hints should be added to
696 * both the enum and the ewmh array
702 _NET_CURRENT_DESKTOP
,
703 _NET_DESKTOP_GEOMETRY
,
705 _NET_DESKTOP_VIEWPORT
,
706 _NET_MOVERESIZE_WINDOW
,
707 _NET_NUMBER_OF_DESKTOPS
,
709 _NET_WM_ACTION_ABOVE
,
710 _NET_WM_ACTION_CLOSE
,
711 _NET_WM_ACTION_FULLSCREEN
,
713 _NET_WM_ACTION_RESIZE
,
714 _NET_WM_ALLOWED_ACTIONS
,
716 _NET_WM_FULL_PLACEMENT
,
720 _NET_WM_STATE_FULLSCREEN
,
721 _NET_WM_STATE_HIDDEN
,
722 _NET_WM_STATE_MAXIMIZED_VERT
,
723 _NET_WM_STATE_MAXIMIZED_HORZ
,
724 _NET_WM_STATE_SKIP_PAGER
,
725 _NET_WM_STATE_SKIP_TASKBAR
,
727 _NET_WM_WINDOW_TYPE_DIALOG
,
728 _NET_WM_WINDOW_TYPE_DOCK
,
729 _NET_WM_WINDOW_TYPE_NORMAL
,
730 _NET_WM_WINDOW_TYPE_SPLASH
,
731 _NET_WM_WINDOW_TYPE_TOOLBAR
,
732 _NET_WM_WINDOW_TYPE_UTILITY
,
733 _SWM_WM_STATE_MANUAL
,
740 } ewmh
[SWM_EWMH_HINT_MAX
] = {
741 /* must be in same order as in the enum */
742 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
743 {"_NET_CLIENT_LIST", XCB_ATOM_NONE
},
744 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
745 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE
},
746 {"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE
},
747 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE
},
748 {"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE
},
749 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
750 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE
},
751 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE
},
752 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
753 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
754 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
755 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
756 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
757 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
758 {"_NET_WM_DESKTOP", XCB_ATOM_NONE
},
759 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE
},
760 {"_NET_WM_NAME", XCB_ATOM_NONE
},
761 {"_NET_WM_STATE", XCB_ATOM_NONE
},
762 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
763 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
764 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
765 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
766 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
767 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
768 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
769 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
770 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
771 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
772 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
773 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
774 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
775 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
776 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
779 /* EWMH source type */
781 EWMH_SOURCE_TYPE_NONE
= 0,
782 EWMH_SOURCE_TYPE_NORMAL
= 1,
783 EWMH_SOURCE_TYPE_OTHER
= 2,
790 XC_BOTTOM_LEFT_CORNER
,
791 XC_BOTTOM_RIGHT_CORNER
,
799 char *name
; /* Name used by Xcursor .*/
800 uint8_t cf_char
; /* cursorfont index. */
802 } cursors
[XC_MAX
] = {
803 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
804 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
805 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
806 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
807 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
808 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
809 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
812 #define SWM_SPAWN_OPTIONAL 0x1
816 TAILQ_ENTRY(spawn_prog
) entry
;
822 TAILQ_HEAD(spawn_list
, spawn_prog
);
823 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
825 /* user/key callable function IDs */
944 KF_DUMPWINS
, /* MUST BE LAST */
952 enum keyfuncid funcid
;
955 RB_HEAD(key_tree
, key
);
957 /* function prototypes */
958 void adjust_font(struct ws_win
*);
959 char *argsep(char **);
960 void bar_cleanup(struct swm_region
*);
961 void bar_extra_setup(void);
962 void bar_extra_stop(void);
963 int bar_extra_update(void);
964 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
965 void bar_fmt_expand(char *, size_t);
967 void bar_print(struct swm_region
*, const char *);
968 void bar_print_legacy(struct swm_region
*, const char *);
969 void bar_replace(char *, char *, struct swm_region
*, size_t);
970 void bar_replace_pad(char *, int *, size_t);
971 char *bar_replace_seq(char *, char *, struct swm_region
*, size_t *, size_t);
972 void bar_setup(struct swm_region
*);
973 void bar_toggle(struct swm_region
*, union arg
*);
974 void bar_urgent(char *, size_t);
975 void bar_window_class(char *, size_t, struct swm_region
*);
976 void bar_window_class_instance(char *, size_t, struct swm_region
*);
977 void bar_window_float(char *, size_t, struct swm_region
*);
978 void bar_window_instance(char *, size_t, struct swm_region
*);
979 void bar_window_name(char *, size_t, struct swm_region
*);
980 void bar_window_state(char *, size_t, struct swm_region
*);
981 void bar_workspace_name(char *, size_t, struct swm_region
*);
982 void buttonpress(xcb_button_press_event_t
*);
983 void center_pointer(struct swm_region
*);
984 void check_conn(void);
985 void clear_keys(void);
986 int clear_maximized(struct workspace
*);
987 void clear_quirks(void);
988 void clear_spawns(void);
989 void clientmessage(xcb_client_message_event_t
*);
990 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
991 int conf_load(const char *, int);
992 void configurenotify(xcb_configure_notify_event_t
*);
993 void configurerequest(xcb_configure_request_event_t
*);
994 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
995 void constrain_window(struct ws_win
*, struct swm_geometry
*, int *);
996 int count_win(struct workspace
*, bool);
997 void cursors_cleanup(void);
998 void cursors_load(void);
999 void custom_region(const char *);
1000 void cyclerg(struct swm_region
*, union arg
*);
1001 void cyclews(struct swm_region
*, union arg
*);
1002 void cycle_layout(struct swm_region
*, union arg
*);
1003 void destroynotify(xcb_destroy_notify_event_t
*);
1004 void dumpwins(struct swm_region
*, union arg
*);
1005 int enable_wm(void);
1006 void enternotify(xcb_enter_notify_event_t
*);
1007 void event_drain(uint8_t);
1008 void event_error(xcb_generic_error_t
*);
1009 void event_handle(xcb_generic_event_t
*);
1010 void ewmh_apply_flags(struct ws_win
*, uint32_t);
1011 void ewmh_autoquirk(struct ws_win
*);
1012 void ewmh_get_desktop_names(void);
1013 void ewmh_get_wm_state(struct ws_win
*);
1014 void ewmh_update_actions(struct ws_win
*);
1015 void ewmh_update_client_list(void);
1016 void ewmh_update_current_desktop(void);
1017 void ewmh_update_desktop_names(void);
1018 void ewmh_update_desktops(void);
1019 void ewmh_change_wm_state(struct ws_win
*, xcb_atom_t
, long);
1020 void ewmh_update_wm_state(struct ws_win
*);
1021 char *expand_tilde(const char *);
1022 void expose(xcb_expose_event_t
*);
1023 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
1024 struct pid_e
*find_pid(pid_t
);
1025 struct ws_win
*find_unmanaged_window(xcb_window_t
);
1026 struct ws_win
*find_window(xcb_window_t
);
1027 void floating_toggle(struct swm_region
*, union arg
*);
1028 void focus(struct swm_region
*, union arg
*);
1030 void focusin(xcb_focus_in_event_t
*);
1031 void focusout(xcb_focus_out_event_t
*);
1033 void focus_flush(void);
1034 void focus_region(struct swm_region
*);
1035 void focusrg(struct swm_region
*, union arg
*);
1036 void focus_win(struct ws_win
*);
1037 void fontset_init(void);
1038 void free_window(struct ws_win
*);
1039 xcb_atom_t
get_atom_from_string(const char *);
1041 char *get_atom_name(xcb_atom_t
);
1043 struct ws_win
*get_focus_magic(struct ws_win
*);
1044 struct ws_win
*get_focus_prev(struct ws_win
*);
1046 char *get_notify_detail_label(uint8_t);
1047 char *get_notify_mode_label(uint8_t);
1049 struct ws_win
*get_pointer_win(xcb_window_t
);
1050 struct ws_win
*get_region_focus(struct swm_region
*);
1051 int get_region_index(struct swm_region
*);
1052 xcb_screen_t
*get_screen(int);
1053 int get_screen_count(void);
1055 char *get_source_type_label(uint32_t);
1056 char *get_stack_mode_name(uint8_t);
1058 int32_t get_swm_ws(xcb_window_t
);
1059 bool get_urgent(struct ws_win
*);
1060 char *get_win_name(xcb_window_t
);
1061 uint8_t get_win_state(xcb_window_t
);
1062 void get_wm_protocols(struct ws_win
*);
1063 int get_ws_idx(struct ws_win
*);
1064 void grabbuttons(struct ws_win
*);
1065 void grabkeys(void);
1066 void grab_windows(void);
1067 void iconify(struct swm_region
*, union arg
*);
1068 bool isxlfd(char *);
1069 void keypress(xcb_key_press_event_t
*);
1070 int key_cmp(struct key
*, struct key
*);
1071 void key_insert(unsigned int, KeySym
, enum keyfuncid
, const char *);
1072 struct key
*key_lookup(unsigned int, KeySym
);
1073 void key_remove(struct key
*);
1074 void key_replace(struct key
*, unsigned int, KeySym
, enum keyfuncid
,
1076 void kill_bar_extra_atexit(void);
1077 void kill_refs(struct ws_win
*);
1079 void leavenotify(xcb_leave_notify_event_t
*);
1081 void load_float_geom(struct ws_win
*);
1082 void lower_window(struct ws_win
*);
1083 struct ws_win
*manage_window(xcb_window_t
, int, bool);
1084 void map_window(struct ws_win
*);
1085 void mapnotify(xcb_map_notify_event_t
*);
1086 void mappingnotify(xcb_mapping_notify_event_t
*);
1087 void maprequest(xcb_map_request_event_t
*);
1088 void maximize_toggle(struct swm_region
*, union arg
*);
1089 void motionnotify(xcb_motion_notify_event_t
*);
1090 void move(struct ws_win
*, union arg
*);
1091 void move_step(struct swm_region
*, union arg
*);
1092 uint32_t name_to_pixel(int, const char *);
1093 void name_workspace(struct swm_region
*, union arg
*);
1094 void new_region(struct swm_screen
*, int, int, int, int);
1095 int parsekeys(const char *, unsigned int, unsigned int *, KeySym
*);
1096 int parsequirks(const char *, uint32_t *, int *);
1097 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1098 void pressbutton(struct swm_region
*, union arg
*);
1099 void priorws(struct swm_region
*, union arg
*);
1101 void print_win_geom(xcb_window_t
);
1103 void propertynotify(xcb_property_notify_event_t
*);
1104 void quirk_free(struct quirk
*);
1105 void quirk_insert(const char *, const char *, const char *, uint32_t, int);
1106 void quirk_remove(struct quirk
*);
1107 void quirk_replace(struct quirk
*, const char *, const char *, const char *,
1109 void quit(struct swm_region
*, union arg
*);
1110 void raise_toggle(struct swm_region
*, union arg
*);
1111 void raise_window(struct ws_win
*);
1112 void region_containment(struct ws_win
*, struct swm_region
*, int);
1113 struct swm_region
*region_under(struct swm_screen
*, int, int);
1114 void regionize(struct ws_win
*, int, int);
1115 void resize(struct ws_win
*, union arg
*);
1116 void resize_step(struct swm_region
*, union arg
*);
1117 void restart(struct swm_region
*, union arg
*);
1118 struct swm_region
*root_to_region(xcb_window_t
, int);
1119 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1120 void scan_randr(int);
1121 void search_do_resp(void);
1122 void search_resp_name_workspace(const char *, size_t);
1123 void search_resp_search_window(const char *);
1124 void search_resp_search_workspace(const char *);
1125 void search_resp_uniconify(const char *, size_t);
1126 void search_win(struct swm_region
*, union arg
*);
1127 void search_win_cleanup(void);
1128 void search_workspace(struct swm_region
*, union arg
*);
1129 void send_to_rg(struct swm_region
*, union arg
*);
1130 void send_to_ws(struct swm_region
*, union arg
*);
1131 void set_region(struct swm_region
*);
1132 int setautorun(const char *, const char *, int);
1133 int setconfbinding(const char *, const char *, int);
1134 int setconfcolor(const char *, const char *, int);
1135 int setconfmodkey(const char *, const char *, int);
1136 int setconfquirk(const char *, const char *, int);
1137 int setconfregion(const char *, const char *, int);
1138 int setconfspawn(const char *, const char *, int);
1139 int setconfvalue(const char *, const char *, int);
1140 void setkeybinding(unsigned int, KeySym
, enum keyfuncid
, const char *);
1141 int setkeymapping(const char *, const char *, int);
1142 int setlayout(const char *, const char *, int);
1143 void setquirk(const char *, const char *, const char *, uint32_t, int);
1144 void setscreencolor(const char *, int, int);
1145 void setspawn(const char *, const char *, int);
1146 void setup_ewmh(void);
1147 void setup_globals(void);
1148 void setup_keys(void);
1149 void setup_quirks(void);
1150 void setup_screens(void);
1151 void setup_spawn(void);
1152 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1153 void set_win_state(struct ws_win
*, uint8_t);
1154 void shutdown_cleanup(void);
1156 void socket_setnonblock(int);
1157 void sort_windows(struct ws_win_list
*);
1158 void spawn(int, union arg
*, bool);
1159 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1160 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1161 void spawn_insert(const char *, const char *, int);
1162 struct spawn_prog
*spawn_find(const char *);
1163 void spawn_remove(struct spawn_prog
*);
1164 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1165 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1166 void stack_config(struct swm_region
*, union arg
*);
1167 void stack_master(struct workspace
*, struct swm_geometry
*, int, bool);
1168 void store_float_geom(struct ws_win
*);
1169 char *strdupsafe(const char *);
1170 void swapwin(struct swm_region
*, union arg
*);
1171 void switchws(struct swm_region
*, union arg
*);
1172 void teardown_ewmh(void);
1173 void unescape_selector(char *);
1174 void unfocus_win(struct ws_win
*);
1175 void uniconify(struct swm_region
*, union arg
*);
1176 void unmanage_window(struct ws_win
*);
1177 void unmapnotify(xcb_unmap_notify_event_t
*);
1178 void unmap_all(void);
1179 void unmap_window(struct ws_win
*);
1180 void updatenumlockmask(void);
1181 void update_floater(struct ws_win
*);
1182 void update_modkey(unsigned int);
1183 void update_win_stacking(struct ws_win
*);
1184 void update_window(struct ws_win
*);
1185 void update_window_color(struct ws_win
*);
1186 void update_wm_state(struct ws_win
*win
);
1187 void validate_spawns(void);
1188 int validate_win(struct ws_win
*);
1189 int validate_ws(struct workspace
*);
1190 void version(struct swm_region
*, union arg
*);
1191 void win_to_ws(struct ws_win
*, int, bool);
1192 pid_t
window_get_pid(xcb_window_t
);
1193 void wkill(struct swm_region
*, union arg
*);
1194 void update_ws_stack(struct workspace
*);
1195 void xft_init(struct swm_region
*);
1196 void _add_startup_exception(const char *, va_list);
1197 void add_startup_exception(const char *, ...);
1199 RB_PROTOTYPE(key_tree
, key
, entry
, key_cmp
);
1200 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
1201 struct key_tree keys
;
1206 xcb_font_t cf
= XCB_NONE
;
1209 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1210 /* try to load Xcursor first. */
1211 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1214 /* fallback to cursorfont. */
1215 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1216 if (cf
== XCB_NONE
) {
1217 cf
= xcb_generate_id(conn
);
1218 xcb_open_font(conn
, cf
, strlen("cursor"),
1222 cursors
[i
].cid
= xcb_generate_id(conn
);
1223 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1224 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1225 0xffff, 0xffff, 0xffff);
1231 xcb_close_font(conn
, cf
);
1235 cursors_cleanup(void)
1238 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1239 xcb_free_cursor(conn
, cursors
[i
].cid
);
1243 expand_tilde(const char *s
)
1245 struct passwd
*ppwd
;
1253 errx(1, "expand_tilde: NULL string.");
1256 result
= strdup(sc
);
1262 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1263 errx(1, "expand_tilde: sysconf");
1265 if ((user
= calloc(1, max
+ 1)) == NULL
)
1266 errx(1, "expand_tilde: calloc");
1268 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1273 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1277 result
= strdup(sc
);
1279 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1283 errx(1, "expand_tilde: failed to allocate memory.");
1289 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1291 unsigned int tmpr
, tmpg
, tmpb
;
1293 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1296 *rr
= RGB_8_TO_16(tmpr
);
1297 *gg
= RGB_8_TO_16(tmpg
);
1298 *bb
= RGB_8_TO_16(tmpb
);
1304 get_screen(int screen
)
1306 const xcb_setup_t
*r
;
1307 xcb_screen_iterator_t iter
;
1309 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1310 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1314 iter
= xcb_setup_roots_iterator(r
);
1315 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1323 get_screen_count(void)
1325 const xcb_setup_t
*r
;
1327 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1328 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1332 return xcb_setup_roots_length(r
);
1336 get_region_index(struct swm_region
*r
)
1338 struct swm_region
*rr
;
1344 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1359 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1360 event_drain(XCB_ENTER_NOTIFY
);
1366 get_atom_from_string(const char *str
)
1368 xcb_intern_atom_cookie_t c
;
1369 xcb_intern_atom_reply_t
*r
;
1372 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1373 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1381 return (XCB_ATOM_NONE
);
1385 get_wm_protocols(struct ws_win
*win
) {
1387 xcb_icccm_get_wm_protocols_reply_t wpr
;
1389 if (xcb_icccm_get_wm_protocols_reply(conn
,
1390 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
1392 for (i
= 0; i
< (int)wpr
.atoms_len
; i
++) {
1393 if (wpr
.atoms
[i
] == a_takefocus
)
1394 win
->take_focus
= true;
1395 if (wpr
.atoms
[i
] == a_delete
)
1396 win
->can_delete
= true;
1398 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
1405 xcb_window_t root
, win
;
1406 int i
, j
, num_screens
;
1408 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1409 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1411 num_screens
= get_screen_count();
1412 for (i
= 0; i
< num_screens
; i
++) {
1413 root
= screens
[i
].root
;
1415 /* Set up _NET_SUPPORTING_WM_CHECK. */
1416 win
= xcb_generate_id(conn
);
1417 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
1418 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1419 XCB_COPY_FROM_PARENT
, 0, NULL
);
1421 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
1422 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1423 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1424 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1427 * Impersonate LG3D non-reparenting WM, written by Sun, to
1428 * workaround a Java GUI rendering issue.
1430 if (java_workaround
)
1431 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1432 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1433 8, strlen("LG3D"), "LG3D");
1435 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1436 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1437 8, strlen("spectrwm"), "spectrwm");
1439 /* Report supported atoms */
1440 xcb_delete_property(conn
, root
, a_net_supported
);
1441 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1442 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, root
,
1443 a_net_supported
, XCB_ATOM_ATOM
, 32, 1,
1448 ewmh_update_desktops();
1449 ewmh_get_desktop_names();
1457 xcb_get_property_cookie_t pc
;
1458 xcb_get_property_reply_t
*pr
;
1460 num_screens
= get_screen_count();
1462 for (i
= 0; i
< num_screens
; i
++) {
1463 /* Get the support check window and destroy it */
1464 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, a_net_wm_check
,
1465 XCB_ATOM_WINDOW
, 0, 1);
1466 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1469 if (pr
->format
== a_net_wm_check
) {
1470 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1472 xcb_destroy_window(conn
, id
);
1473 xcb_delete_property(conn
, screens
[i
].root
,
1475 xcb_delete_property(conn
, screens
[i
].root
,
1483 ewmh_autoquirk(struct ws_win
*win
)
1485 xcb_get_property_reply_t
*r
;
1486 xcb_get_property_cookie_t c
;
1490 c
= xcb_get_property(conn
, 0, win
->id
,
1491 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1492 r
= xcb_get_property_reply(conn
, c
, NULL
);
1496 type
= xcb_get_property_value(r
);
1497 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1499 for (i
= 0; i
< n
; i
++) {
1500 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1502 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1503 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1504 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1505 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1508 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1509 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1510 win
->quirks
= SWM_Q_FLOAT
;
1518 ewmh_update_actions(struct ws_win
*win
)
1520 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1526 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1529 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1530 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1531 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1534 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1535 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1538 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1539 #define _NET_WM_STATE_ADD 1 /* add/set property */
1540 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1543 ewmh_change_wm_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1550 name
= get_atom_name(state
);
1551 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: win %#x, state: %s, "
1552 "action: %ld\n", WINID(win
), name
, action
);
1558 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1559 flag
= EWMH_F_FULLSCREEN
;
1560 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1561 flag
= EWMH_F_ABOVE
;
1562 else if (state
== ewmh
[_NET_WM_STATE_HIDDEN
].atom
)
1563 flag
= EWMH_F_HIDDEN
;
1564 else if (state
== ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
||
1565 state
== ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
)
1566 flag
= EWMH_F_MAXIMIZED
;
1567 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1568 flag
= SWM_F_MANUAL
;
1569 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1570 flag
= EWMH_F_SKIP_PAGER
;
1571 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1572 flag
= EWMH_F_SKIP_TASKBAR
;
1574 /* Disallow unfloating transients. */
1575 if (TRANS(win
) && flag
== EWMH_F_ABOVE
)
1578 new_flags
= win
->ewmh_flags
;
1581 case _NET_WM_STATE_REMOVE
:
1584 case _NET_WM_STATE_ADD
:
1587 case _NET_WM_STATE_TOGGLE
:
1592 ewmh_apply_flags(win
, new_flags
);
1595 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: done.\n");
1599 ewmh_apply_flags(struct ws_win
*win
, uint32_t pending
)
1601 struct workspace
*ws
;
1604 changed
= win
->ewmh_flags
^ pending
;
1608 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: pending: %d\n", pending
);
1610 win
->ewmh_flags
= pending
;
1613 if (changed
& EWMH_F_HIDDEN
) {
1615 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1616 ws
->focus_pending
= get_focus_prev(win
);
1621 /* Reload floating geometry in case region changed. */
1623 load_float_geom(win
);
1625 /* The window is no longer iconic, prepare focus. */
1626 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1627 ws
->focus_pending
= get_focus_magic(win
);
1632 if (changed
& EWMH_F_ABOVE
) {
1633 if (ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1635 load_float_geom(win
);
1636 else if (!MAXIMIZED(win
))
1637 store_float_geom(win
);
1639 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1640 changed
&= ~EWMH_F_MAXIMIZED
;
1644 win
->ewmh_flags
^= EWMH_F_ABOVE
& pending
;
1648 if (changed
& EWMH_F_MAXIMIZED
) {
1649 /* VERT and/or HORZ changed. */
1651 if (!MAXIMIZED(win
))
1652 load_float_geom(win
);
1654 store_float_geom(win
);
1657 if (MAXIMIZED(win
)) {
1658 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
1659 ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1663 ws
->focus_pending
= win
;
1667 update_window_color(win
);
1671 if (changed
& EWMH_F_FULLSCREEN
) {
1672 if (FULLSCREEN(win
)) {
1673 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
1677 ws
->focus_pending
= win
;
1680 load_float_geom(win
);
1683 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1687 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: done.\n");
1691 ewmh_update_wm_state(struct ws_win
*win
) {
1692 xcb_atom_t vals
[SWM_EWMH_ACTION_COUNT_MAX
];
1696 vals
[n
++] = ewmh
[_NET_WM_STATE_HIDDEN
].atom
;
1697 if (FULLSCREEN(win
))
1698 vals
[n
++] = ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
;
1699 if (MAXIMIZED_VERT(win
))
1700 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
;
1701 if (MAXIMIZED_HORZ(win
))
1702 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
;
1703 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1704 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
;
1705 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1706 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
;
1707 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1708 vals
[n
++] = ewmh
[_NET_WM_STATE_ABOVE
].atom
;
1709 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1710 vals
[n
++] = ewmh
[_SWM_WM_STATE_MANUAL
].atom
;
1713 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1714 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, n
, vals
);
1716 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1720 ewmh_get_wm_state(struct ws_win
*win
)
1723 xcb_get_property_cookie_t c
;
1724 xcb_get_property_reply_t
*r
;
1730 win
->ewmh_flags
= 0;
1732 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1733 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1734 r
= xcb_get_property_reply(conn
, c
, NULL
);
1738 states
= xcb_get_property_value(r
);
1739 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1741 for (i
= 0; i
< n
; i
++)
1742 ewmh_change_wm_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1750 dumpwins(struct swm_region
*r
, union arg
*args
)
1754 xcb_get_window_attributes_cookie_t c
;
1755 xcb_get_window_attributes_reply_t
*wa
;
1757 /* suppress unused warning since var is needed */
1760 if (r
->ws
== NULL
) {
1761 DPRINTF("dumpwins: invalid workspace\n");
1765 DPRINTF("=== managed window list ws %02d ===\n", r
->ws
->idx
);
1766 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
) {
1767 state
= get_win_state(w
->id
);
1768 c
= xcb_get_window_attributes(conn
, w
->id
);
1769 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1771 DPRINTF("win %#x, map_state: %d, state: %u, "
1772 "transient: %#x\n", w
->id
, wa
->map_state
,
1773 state
, w
->transient
);
1776 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1780 DPRINTF("=== stacking order (top down) === \n");
1781 TAILQ_FOREACH(w
, &r
->ws
->stack
, stack_entry
) {
1782 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
1783 "iconic: %s\n", w
->id
, YESNO(FULLSCREEN(w
)),
1784 YESNO(MAXIMIZED(w
)), YESNO(ABOVE(w
)), YESNO(ICONIC(w
)));
1787 DPRINTF("===== unmanaged window list =====\n");
1788 TAILQ_FOREACH(w
, &r
->ws
->unmanagedlist
, entry
) {
1789 state
= get_win_state(w
->id
);
1790 c
= xcb_get_window_attributes(conn
, w
->id
);
1791 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1793 DPRINTF("win %#x, map_state: %d, state: %u, "
1794 "transient: %#x\n", w
->id
, wa
->map_state
,
1795 state
, w
->transient
);
1798 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1802 DPRINTF("=================================\n");
1806 dumpwins(struct swm_region
*r
, union arg
*s
)
1811 #endif /* SWM_DEBUG */
1816 int saved_errno
, status
;
1819 saved_errno
= errno
;
1823 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1828 if (errno
!= ECHILD
)
1829 warn("sighdlr: waitpid");
1830 #endif /* SWM_DEBUG */
1833 if (pid
== searchpid
)
1837 if (WIFEXITED(status
)) {
1838 if (WEXITSTATUS(status
) != 0)
1839 warnx("sighdlr: child exit status: %d",
1840 WEXITSTATUS(status
));
1842 warnx("sighdlr: child is terminated "
1844 #endif /* SWM_DEBUG */
1858 errno
= saved_errno
;
1864 struct pid_e
*p
= NULL
;
1866 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1871 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1880 name_to_pixel(int sidx
, const char *colorname
)
1882 uint32_t result
= 0;
1883 char cname
[32] = "#";
1884 xcb_screen_t
*screen
;
1885 xcb_colormap_t cmap
;
1886 xcb_alloc_color_reply_t
*cr
;
1887 xcb_alloc_named_color_reply_t
*nr
;
1888 uint16_t rr
, gg
, bb
;
1890 screen
= get_screen(sidx
);
1891 cmap
= screen
->default_colormap
;
1893 /* color is in format rgb://rr/gg/bb */
1894 if (strncmp(colorname
, "rgb:", 4) == 0) {
1895 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1896 warnx("could not parse rgb %s", colorname
);
1898 cr
= xcb_alloc_color_reply(conn
,
1899 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1905 warnx("color '%s' not found", colorname
);
1908 nr
= xcb_alloc_named_color_reply(conn
,
1909 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1912 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1913 nr
= xcb_alloc_named_color_reply(conn
,
1914 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1921 warnx("color '%s' not found", colorname
);
1928 setscreencolor(const char *val
, int i
, int c
)
1930 if (i
< 0 || i
>= get_screen_count())
1933 screens
[i
].c
[c
].pixel
= name_to_pixel(i
, val
);
1934 free(screens
[i
].c
[c
].name
);
1935 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1940 fancy_stacker(struct workspace
*ws
)
1942 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1943 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1944 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1945 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1946 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1947 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1948 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1949 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1950 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1954 plain_stacker(struct workspace
*ws
)
1956 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1957 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1958 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1959 sizeof ws
->stacker
);
1960 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1961 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1962 sizeof ws
->stacker
);
1966 custom_region(const char *val
)
1968 unsigned int x
, y
, w
, h
;
1969 int sidx
, num_screens
;
1970 xcb_screen_t
*screen
;
1972 num_screens
= get_screen_count();
1973 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1974 errx(1, "invalid custom region, "
1975 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1976 if (sidx
< 1 || sidx
> num_screens
)
1977 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1981 if ((screen
= get_screen(sidx
)) == NULL
)
1982 errx(1, "ERROR: can't get screen %d.", sidx
);
1985 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1987 if (x
> screen
->width_in_pixels
||
1988 y
> screen
->height_in_pixels
||
1989 w
+ x
> screen
->width_in_pixels
||
1990 h
+ y
> screen
->height_in_pixels
) {
1991 warnx("ignoring region %ux%u+%u+%u - not within screen "
1992 "boundaries (%ux%u)", w
, h
, x
, y
,
1993 screen
->width_in_pixels
, screen
->height_in_pixels
);
1997 new_region(&screens
[sidx
], x
, y
, w
, h
);
2001 socket_setnonblock(int fd
)
2005 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
2006 err(1, "fcntl F_GETFL");
2007 flags
|= O_NONBLOCK
;
2008 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
2009 err(1, "fcntl F_SETFL");
2013 bar_print_legacy(struct swm_region
*r
, const char *s
)
2015 xcb_rectangle_t rect
;
2020 XRectangle ibox
, lbox
;
2024 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
2026 switch (bar_justify
) {
2027 case SWM_BAR_JUSTIFY_LEFT
:
2030 case SWM_BAR_JUSTIFY_CENTER
:
2031 x
= (WIDTH(r
) - lbox
.width
) / 2;
2033 case SWM_BAR_JUSTIFY_RIGHT
:
2034 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
2038 if (x
< SWM_BAR_OFFSET
)
2043 rect
.width
= WIDTH(r
->bar
);
2044 rect
.height
= HEIGHT(r
->bar
);
2046 /* clear back buffer */
2047 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2048 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2049 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2051 /* draw back buffer */
2052 gcvd
.graphics_exposures
= 0;
2053 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
2054 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2055 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
2056 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
2058 XFreeGC(display
, draw
);
2061 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2062 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2066 bar_print(struct swm_region
*r
, const char *s
)
2069 xcb_rectangle_t rect
;
2077 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
2079 switch (bar_justify
) {
2080 case SWM_BAR_JUSTIFY_LEFT
:
2083 case SWM_BAR_JUSTIFY_CENTER
:
2084 x
= (WIDTH(r
) - info
.width
) / 2;
2086 case SWM_BAR_JUSTIFY_RIGHT
:
2087 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
2091 if (x
< SWM_BAR_OFFSET
)
2096 rect
.width
= WIDTH(r
->bar
);
2097 rect
.height
= HEIGHT(r
->bar
);
2099 /* clear back buffer */
2100 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2101 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2102 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2104 /* draw back buffer */
2105 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
2106 DefaultVisual(display
, r
->s
->idx
),
2107 DefaultColormap(display
, r
->s
->idx
));
2109 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
2110 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
2113 XftDrawDestroy(draw
);
2116 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2117 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2121 bar_extra_stop(void)
2125 bzero(bar_pipe
, sizeof bar_pipe
);
2128 kill(bar_pid
, SIGTERM
);
2131 strlcpy(bar_ext
, "", sizeof bar_ext
);
2136 bar_window_class(char *s
, size_t sz
, struct swm_region
*r
)
2138 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2140 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
2141 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
2145 bar_window_instance(char *s
, size_t sz
, struct swm_region
*r
)
2147 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2149 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
2150 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
2154 bar_window_class_instance(char *s
, size_t sz
, struct swm_region
*r
)
2156 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2159 bar_window_class(s
, sz
, r
);
2160 strlcat(s
, ":", sz
);
2161 bar_window_instance(s
, sz
, r
);
2165 bar_window_state(char *s
, size_t sz
, struct swm_region
*r
)
2167 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2169 if (MAXIMIZED(r
->ws
->focus
))
2170 strlcat(s
, "(m)", sz
);
2171 else if (ABOVE(r
->ws
->focus
))
2172 strlcat(s
, "(f)", sz
);
2176 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
2180 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2183 title
= get_win_name(r
->ws
->focus
->id
);
2184 strlcat(s
, title
, sz
);
2189 get_urgent(struct ws_win
*win
)
2191 xcb_icccm_wm_hints_t hints
;
2192 xcb_get_property_cookie_t c
;
2193 bool urgent
= false;
2196 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2197 if (xcb_icccm_get_wm_hints_reply(conn
, c
, &hints
, NULL
))
2198 urgent
= xcb_icccm_wm_hints_get_urgency(&hints
);
2205 bar_urgent(char *s
, size_t sz
)
2208 int i
, j
, num_screens
;
2209 bool urgent
[SWM_WS_MAX
];
2212 for (i
= 0; i
< workspace_limit
; i
++)
2215 num_screens
= get_screen_count();
2216 for (i
= 0; i
< num_screens
; i
++)
2217 for (j
= 0; j
< workspace_limit
; j
++)
2218 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2219 if (get_urgent(win
))
2222 for (i
= 0; i
< workspace_limit
; i
++) {
2224 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2226 } else if (!urgent_collapse
) {
2227 strlcat(s
, "- ", sz
);
2230 if(urgent_collapse
&& s
[0])
2231 s
[strlen(s
) - 1] = 0;
2235 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2237 if (r
== NULL
|| r
->ws
== NULL
)
2239 if (r
->ws
->name
!= NULL
)
2240 strlcat(s
, r
->ws
->name
, sz
);
2243 /* build the default bar format according to the defined enabled options */
2245 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2249 /* if format provided, just copy the buffers */
2250 if (bar_format
!= NULL
) {
2251 strlcpy(fmtnew
, fmtexp
, sz
);
2255 /* reset the output buffer */
2258 strlcat(fmtnew
, "+N:+I ", sz
);
2260 strlcat(fmtnew
, "+S", sz
);
2261 strlcat(fmtnew
, " ", sz
);
2263 /* only show the workspace name if there's actually one */
2264 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2265 strlcat(fmtnew
, "<+D>", sz
);
2267 /* If enabled, only show the iconic count if there are iconic wins. */
2268 if (iconic_enabled
&& r
!= NULL
&& r
->ws
!= NULL
)
2269 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2271 strlcat(fmtnew
, "{+M}", sz
);
2275 strlcat(fmtnew
, "+3<", sz
);
2277 if (clock_enabled
) {
2278 strlcat(fmtnew
, fmtexp
, sz
);
2279 strlcat(fmtnew
, "+4<", sz
);
2282 /* bar_urgent already adds the space before the last asterisk */
2284 strlcat(fmtnew
, "* +U*+4<", sz
);
2286 if (window_class_enabled
) {
2287 strlcat(fmtnew
, "+C", sz
);
2288 if (!window_instance_enabled
)
2289 strlcat(fmtnew
, "+4<", sz
);
2292 /* checks needed by the colon and floating strlcat(3) calls below */
2293 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2294 if (window_instance_enabled
) {
2295 if (window_class_enabled
)
2296 strlcat(fmtnew
, ":", sz
);
2297 strlcat(fmtnew
, "+T+4<", sz
);
2299 if (window_name_enabled
) {
2300 if (ABOVE(r
->ws
->focus
) || MAXIMIZED(r
->ws
->focus
))
2301 strlcat(fmtnew
, "+F ", sz
);
2302 strlcat(fmtnew
, "+64W ", sz
);
2306 /* finally add the action script output and the version */
2307 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2311 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2313 /* special case; no limit given, pad one space, instead */
2314 if (*limit
== (int)sz
- 1)
2316 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2319 /* replaces the bar format character sequences (like in tmux(1)) */
2321 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2326 char tmp
[SWM_BAR_MAX
];
2327 int limit
, size
, count
;
2330 /* reset strlcat(3) buffer */
2333 /* get number, if any */
2336 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2337 limit
= sizeof tmp
- 1;
2338 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2339 limit
= sizeof tmp
- 1;
2341 /* there is nothing to replace (ie EOL) */
2348 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2351 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2354 bar_window_class(tmp
, sizeof tmp
, r
);
2357 bar_workspace_name(tmp
, sizeof tmp
, r
);
2360 bar_window_state(tmp
, sizeof tmp
, r
);
2363 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2367 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2371 snprintf(tmp
, sizeof tmp
, "%d", count
);
2374 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2377 bar_window_class_instance(tmp
, sizeof tmp
, r
);
2380 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2383 bar_window_instance(tmp
, sizeof tmp
, r
);
2386 bar_urgent(tmp
, sizeof tmp
);
2389 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2392 bar_window_name(tmp
, sizeof tmp
, r
);
2395 /* unknown character sequence; copy as-is */
2396 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2402 if ((int)len
< limit
)
2404 while (limit
-- > 0) {
2405 if (*offrep
>= sz
- 1)
2407 fmtrep
[(*offrep
)++] = *ptr
++;
2415 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2420 while (*fmt
!= '\0') {
2422 /* skip ordinary characters */
2425 fmtrep
[off
++] = *fmt
++;
2429 /* character sequence found; replace it */
2430 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2439 bar_fmt_expand(char *fmtexp
, size_t sz
)
2446 /* start by grabbing the current time and date */
2448 localtime_r(&tmt
, &tm
);
2450 /* figure out what to expand */
2451 if (bar_format
!= NULL
)
2453 else if (bar_format
== NULL
&& clock_enabled
)
2455 /* if nothing to expand bail out */
2461 /* copy as-is, just in case the format shouldn't be expanded below */
2462 strlcpy(fmtexp
, fmt
, sz
);
2463 /* finally pass the string through strftime(3) */
2464 #ifndef SWM_DENY_CLOCK_FORMAT
2465 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2466 warnx("format too long");
2471 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2475 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2476 char fmtrep
[SWM_BAR_MAX
];
2478 struct swm_region
*r
;
2480 /* expand the format by first passing it through strftime(3) */
2481 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2483 num_screens
= get_screen_count();
2484 for (i
= 0; i
< num_screens
; i
++) {
2485 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2489 if (bar_enabled
&& r
->ws
->bar_enabled
)
2490 xcb_map_window(conn
, r
->bar
->id
);
2492 xcb_unmap_window(conn
, r
->bar
->id
);
2496 if (startup_exception
)
2497 snprintf(fmtrep
, sizeof fmtrep
, "total "
2498 "exceptions: %d, first exception: %s",
2502 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2503 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2505 if (bar_font_legacy
)
2506 bar_print_legacy(r
, fmtrep
);
2508 bar_print(r
, fmtrep
);
2514 * Reads external script output; call when stdin is readable.
2515 * Returns 1 if bar_ext was updated; otherwise 0.
2518 bar_extra_update(void)
2521 char b
[SWM_BAR_MAX
];
2522 bool changed
= false;
2527 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2530 if (b
[len
- 1] == '\n') {
2531 /* Remove newline. */
2534 /* "Clear" bar_ext. */
2537 /* Flush buffered output. */
2538 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2539 bar_ext_buf
[0] = '\0';
2541 /* Append new output to bar. */
2542 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2546 /* Buffer output. */
2547 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2552 if (errno
!= EAGAIN
) {
2553 warn("bar_action failed");
2562 bar_toggle(struct swm_region
*r
, union arg
*args
)
2564 struct swm_region
*tmpr
;
2567 /* suppress unused warnings since vars are needed */
2571 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2574 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2575 /* Only change if master switch is enabled. */
2577 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2579 bar_enabled
= r
->ws
->bar_enabled
= true;
2581 case SWM_ARG_ID_BAR_TOGGLE
:
2582 bar_enabled
= !bar_enabled
;
2586 /* update bars as necessary */
2587 num_screens
= get_screen_count();
2588 for (i
= 0; i
< num_screens
; i
++)
2589 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2591 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2592 xcb_map_window(conn
, tmpr
->bar
->id
);
2594 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2599 /* must be after stack */
2606 bar_extra_setup(void)
2608 /* do this here because the conf file is in memory */
2609 if (!bar_extra
&& bar_argv
[0]) {
2610 /* launch external status app */
2612 if (pipe(bar_pipe
) == -1)
2613 err(1, "pipe error");
2614 socket_setnonblock(bar_pipe
[0]);
2615 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2617 /* Set stdin to read from the pipe. */
2618 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2621 /* Set stdout to write to the pipe. */
2622 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2625 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2626 err(1, "could not disable SIGPIPE");
2627 switch (bar_pid
= fork()) {
2629 err(1, "cannot fork");
2633 execvp(bar_argv
[0], bar_argv
);
2634 err(1, "%s external app failed", bar_argv
[0]);
2636 default: /* parent */
2641 atexit(kill_bar_extra_atexit
);
2646 kill_bar_extra_atexit(void)
2649 kill(bar_pid
, SIGTERM
);
2657 while ((s
= index(s
, '-'))) {
2662 return (count
== 14);
2668 char *default_string
;
2669 char **missing_charsets
;
2670 int num_missing_charsets
= 0;
2674 XFreeFontSet(display
, bar_fs
);
2678 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n", bar_fonts
);
2680 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2681 &num_missing_charsets
, &default_string
);
2683 if (num_missing_charsets
> 0) {
2684 warnx("Unable to load charset(s):");
2686 for (i
= 0; i
< num_missing_charsets
; ++i
)
2687 warnx("%s", missing_charsets
[i
]);
2689 XFreeStringList(missing_charsets
);
2691 if (strcmp(default_string
, ""))
2692 warnx("Glyphs from those sets will be replaced "
2693 "by '%s'.", default_string
);
2695 warnx("Glyphs from those sets won't be drawn.");
2699 errx(1, "Error creating font set structure.");
2701 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2703 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2704 2 * bar_border_width
;
2711 xft_init(struct swm_region
*r
)
2713 char *font
, *d
, *search
;
2716 if (bar_font
== NULL
) {
2717 if ((d
= strdup(bar_fonts
)) == NULL
)
2718 errx(1, "insufficient memory.");
2720 while ((font
= strsep(&search
, ",")) != NULL
) {
2724 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2727 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2730 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2734 if (bar_font
== NULL
) {
2735 warnx("unable to load font %s", font
);
2738 DNPRINTF(SWM_D_INIT
, "successfully opened "
2746 if (bar_font
== NULL
)
2747 errx(1, "unable to open a font");
2749 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2751 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2752 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2753 warn("Xft error: unable to allocate color.");
2755 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR
].pixel
, color
);
2757 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2758 DefaultColormap(display
, r
->s
->idx
), &color
, &search_font_color
))
2759 warn("Xft error: unable to allocate color.");
2761 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2768 bar_setup(struct swm_region
*r
)
2770 xcb_screen_t
*screen
;
2773 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2776 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2777 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2782 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2783 err(1, "bar_setup: calloc: failed to allocate memory.");
2785 if (bar_font_legacy
)
2791 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2792 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2793 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2795 /* Assume region is unfocused when we create the bar. */
2796 r
->bar
->id
= xcb_generate_id(conn
);
2797 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2798 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
2799 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
2800 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
2802 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2803 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2804 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
2805 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
2806 | XCB_CW_EVENT_MASK
, wa
);
2808 /* Stack bar window above region window to start. */
2810 wa
[1] = XCB_STACK_MODE_ABOVE
;
2812 xcb_configure_window(conn
, r
->bar
->id
, XCB_CONFIG_WINDOW_SIBLING
|
2813 XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
2815 r
->bar
->buffer
= xcb_generate_id(conn
);
2816 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
2817 WIDTH(r
->bar
), HEIGHT(r
->bar
));
2820 xcb_randr_select_input(conn
, r
->bar
->id
,
2821 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
2824 xcb_map_window(conn
, r
->bar
->id
);
2826 DNPRINTF(SWM_D_BAR
, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
2827 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
2834 bar_cleanup(struct swm_region
*r
)
2838 xcb_destroy_window(conn
, r
->bar
->id
);
2839 xcb_free_pixmap(conn
, r
->bar
->buffer
);
2845 set_win_state(struct ws_win
*win
, uint8_t state
)
2847 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
2849 DNPRINTF(SWM_D_EVENT
, "set_win_state: win %#x, state: %u\n",
2855 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
2856 a_state
, 32, 2, data
);
2860 get_win_state(xcb_window_t w
)
2862 xcb_get_property_reply_t
*r
;
2863 xcb_get_property_cookie_t c
;
2864 uint32_t result
= 0;
2866 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
2867 r
= xcb_get_property_reply(conn
, c
, NULL
);
2869 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
2870 result
= *((uint32_t *)xcb_get_property_value(r
));
2874 DNPRINTF(SWM_D_MISC
, "get_win_state property: win %#x state %u\n", w
,
2880 version(struct swm_region
*r
, union arg
*args
)
2882 /* suppress unused warnings since vars are needed */
2886 bar_version
= !bar_version
;
2888 snprintf(bar_vertext
, sizeof bar_vertext
,
2889 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2891 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2898 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
2900 xcb_client_message_event_t ev
;
2908 name
= get_atom_name(a
);
2909 DNPRINTF(SWM_D_EVENT
, "client_msg: win %#x, atom: %s(%u), "
2911 win
->id
, name
, a
, t
);
2915 bzero(&ev
, sizeof ev
);
2916 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2917 ev
.window
= win
->id
;
2920 ev
.data
.data32
[0] = a
;
2921 ev
.data
.data32
[1] = t
;
2923 xcb_send_event(conn
, 0, win
->id
,
2924 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2927 /* synthetic response to a ConfigureRequest when not making a change */
2929 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2931 xcb_configure_notify_event_t ce
;
2936 /* send notification of unchanged state. */
2937 bzero(&ce
, sizeof(ce
));
2938 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2941 ce
.width
= WIDTH(win
);
2942 ce
.height
= HEIGHT(win
);
2943 ce
.override_redirect
= 0;
2948 ce
.window
= win
->id
;
2949 ce
.border_width
= BORDER(win
);
2950 ce
.above_sibling
= XCB_WINDOW_NONE
;
2953 ce
.event
= ev
->window
;
2954 ce
.window
= ev
->window
;
2956 /* make response appear more WM_SIZE_HINTS-compliant */
2957 if (win
->sh
.flags
) {
2958 DNPRINTF(SWM_D_MISC
, "config_win: hints: win %#x,"
2959 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2960 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2961 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2962 SH_INC_W(win
), SH_INC_H(win
));
2967 /* the hint may be set... to 0! */
2968 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2969 ce
.width
= SH_MIN_W(win
);
2970 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2971 ce
.height
= SH_MIN_H(win
);
2976 /* may also be advertized as 0 */
2977 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2978 ce
.width
= SH_MAX_W(win
);
2979 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2980 ce
.height
= SH_MAX_H(win
);
2983 /* resize increment. */
2985 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2986 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2988 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2989 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2993 /* adjust x and y for requested border_width. */
2994 ce
.x
+= BORDER(win
) - ev
->border_width
;
2995 ce
.y
+= BORDER(win
) - ev
->border_width
;
2996 ce
.border_width
= ev
->border_width
;
2997 ce
.above_sibling
= ev
->sibling
;
3000 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
3001 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
3002 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
3004 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
3009 count_win(struct workspace
*ws
, bool count_transient
)
3014 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3015 if (!count_transient
&& FLOATING(win
))
3021 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
3027 quit(struct swm_region
*r
, union arg
*args
)
3029 /* suppress unused warnings since vars are needed */
3033 DNPRINTF(SWM_D_MISC
, "quit\n");
3038 lower_window(struct ws_win
*win
)
3040 struct ws_win
*target
= NULL
;
3041 struct workspace
*ws
;
3048 DNPRINTF(SWM_D_EVENT
, "lower_window: win %#x\n", win
->id
);
3050 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3051 if (target
== win
|| ICONIC(target
))
3053 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3056 if (win
->transient
== target
->transient
)
3058 if (win
->transient
== target
->id
)
3061 if (FULLSCREEN(target
))
3063 if (FULLSCREEN(win
))
3065 if (MAXIMIZED(target
))
3069 if (ABOVE(target
) || TRANS(target
))
3071 if (ABOVE(win
) || TRANS(win
))
3075 /* Change stack position. */
3076 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3078 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3080 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3082 update_win_stacking(win
);
3085 if (swm_debug
& SWM_D_STACK
) {
3086 DPRINTF("=== stacking order (top down) === \n");
3087 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3088 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3089 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3090 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3091 YESNO(ICONIC(target
)));
3095 DNPRINTF(SWM_D_EVENT
, "lower_window: done\n");
3099 raise_window(struct ws_win
*win
)
3101 struct ws_win
*target
= NULL
;
3102 struct workspace
*ws
;
3108 DNPRINTF(SWM_D_EVENT
, "raise_window: win %#x\n", win
->id
);
3110 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3111 if (target
== win
|| ICONIC(target
))
3113 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3115 if (TRANS(win
) && (win
->transient
== target
->transient
||
3116 win
->transient
== target
->id
))
3118 if (FULLSCREEN(win
))
3120 if (FULLSCREEN(target
))
3124 if (MAXIMIZED(target
))
3126 if (ABOVE(win
) || TRANS(win
) ||
3127 (win
->ws
->focus
== win
&& ws
->always_raise
))
3129 if (!ABOVE(target
) && !TRANS(target
))
3133 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3135 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3137 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3139 update_win_stacking(win
);
3142 if (swm_debug
& SWM_D_STACK
) {
3143 DPRINTF("=== stacking order (top down) === \n");
3144 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3145 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3146 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3147 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3148 YESNO(ICONIC(target
)));
3152 DNPRINTF(SWM_D_EVENT
, "raise_window: done\n");
3156 update_win_stacking(struct ws_win
*win
)
3158 struct ws_win
*sibling
;
3159 struct swm_region
*r
;
3162 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
3165 sibling
= TAILQ_NEXT(win
, stack_entry
);
3166 if (sibling
!= NULL
&& (FLOATING(win
) == FLOATING(sibling
) ||
3167 (win
->ws
->always_raise
&& win
->ws
->focus
== win
)))
3168 val
[0] = sibling
->id
;
3169 else if (FLOATING(win
) || (win
->ws
->always_raise
&&
3170 win
->ws
->focus
== win
))
3171 val
[0] = r
->bar
->id
;
3175 DNPRINTF(SWM_D_EVENT
, "update_win_stacking: %#x, sibling %#x\n",
3178 val
[1] = XCB_STACK_MODE_ABOVE
;
3180 xcb_configure_window(conn
, win
->id
, XCB_CONFIG_WINDOW_SIBLING
|
3181 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
3185 map_window(struct ws_win
*win
)
3190 DNPRINTF(SWM_D_EVENT
, "map_window: win %#x, mapped: %s\n",
3191 win
->id
, YESNO(win
->mapped
));
3196 xcb_map_window(conn
, win
->id
);
3197 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
3202 unmap_window(struct ws_win
*win
)
3207 DNPRINTF(SWM_D_EVENT
, "unmap_window: win %#x, mapped: %s\n", win
->id
,
3208 YESNO(win
->mapped
));
3213 xcb_unmap_window(conn
, win
->id
);
3214 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
3215 win
->mapped
= false;
3222 int i
, j
, num_screens
;
3224 num_screens
= get_screen_count();
3225 for (i
= 0; i
< num_screens
; i
++)
3226 for (j
= 0; j
< workspace_limit
; j
++)
3227 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3232 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
3234 xcb_key_press_event_t event
;
3235 xcb_keycode_t
*keycode
;
3240 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
3242 DNPRINTF(SWM_D_MISC
, "fake_keypress: win %#x, keycode %u\n",
3245 bzero(&event
, sizeof(event
));
3246 event
.event
= win
->id
;
3247 event
.root
= win
->s
->root
;
3248 event
.child
= XCB_WINDOW_NONE
;
3249 event
.time
= XCB_CURRENT_TIME
;
3250 event
.event_x
= X(win
);
3251 event
.event_y
= Y(win
);
3254 event
.same_screen
= 1;
3255 event
.detail
= *keycode
;
3256 event
.state
= modifiers
;
3258 event
.response_type
= XCB_KEY_PRESS
;
3259 xcb_send_event(conn
, 1, win
->id
,
3260 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
3262 event
.response_type
= XCB_KEY_RELEASE
;
3263 xcb_send_event(conn
, 1, win
->id
,
3264 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
3270 restart(struct swm_region
*r
, union arg
*args
)
3272 /* suppress unused warning since var is needed */
3276 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
3280 execvp(start_argv
[0], start_argv
);
3281 warn("execvp failed");
3286 get_pointer_win(xcb_window_t root
)
3288 struct ws_win
*win
= NULL
;
3289 xcb_query_pointer_reply_t
*r
;
3291 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: %#x.\n", root
);
3293 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
3295 win
= find_window(r
->child
);
3297 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: %#x.\n",
3300 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
3309 center_pointer(struct swm_region
*r
)
3313 if (!warp_pointer
|| r
== NULL
)
3318 DNPRINTF(SWM_D_EVENT
, "center_pointer: win %#x.\n", WINID(win
));
3320 if (win
&& win
->mapped
)
3321 xcb_warp_pointer(conn
, XCB_NONE
, win
->id
, 0, 0, 0, 0,
3322 WIDTH(win
) / 2, HEIGHT(win
) / 2);
3324 xcb_warp_pointer(conn
, XCB_NONE
, r
->id
, 0, 0, 0, 0,
3325 WIDTH(r
) / 2, HEIGHT(r
) / 2);
3329 root_to_region(xcb_window_t root
, int check
)
3332 struct swm_region
*r
= NULL
;
3334 xcb_query_pointer_reply_t
*qpr
;
3335 xcb_get_input_focus_reply_t
*gifr
;
3337 DNPRINTF(SWM_D_MISC
, "root_to_region: win %#x\n", root
);
3339 num_screens
= get_screen_count();
3340 for (i
= 0; i
< num_screens
; i
++)
3341 if (screens
[i
].root
== root
)
3344 if (check
& SWM_CK_REGION
)
3345 r
= screens
[i
].r_focus
;
3347 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
3348 /* Try to find an actively focused window */
3349 gifr
= xcb_get_input_focus_reply(conn
,
3350 xcb_get_input_focus(conn
), NULL
);
3352 cfw
= find_window(gifr
->focus
);
3353 if (cfw
&& cfw
->ws
->r
)
3360 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
3361 /* No region with an active focus; try to use pointer. */
3362 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
3363 screens
[i
].root
), NULL
);
3365 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
3366 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
3367 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3368 if (X(r
) <= qpr
->root_x
&&
3369 qpr
->root_x
< MAX_X(r
) &&
3370 Y(r
) <= qpr
->root_y
&&
3371 qpr
->root_y
< MAX_Y(r
))
3378 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
3379 r
= TAILQ_FIRST(&screens
[i
].rl
);
3381 DNPRINTF(SWM_D_MISC
, "root_to_region: idx: %d\n", get_region_index(r
));
3387 find_unmanaged_window(xcb_window_t id
)
3390 int i
, j
, num_screens
;
3392 num_screens
= get_screen_count();
3393 for (i
= 0; i
< num_screens
; i
++)
3394 for (j
= 0; j
< workspace_limit
; j
++)
3395 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
3403 find_window(xcb_window_t id
)
3406 int i
, j
, num_screens
;
3407 xcb_query_tree_reply_t
*r
;
3409 num_screens
= get_screen_count();
3410 for (i
= 0; i
< num_screens
; i
++)
3411 for (j
= 0; j
< workspace_limit
; j
++)
3412 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3416 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3420 /* if we were looking for the parent return that window instead */
3421 if (r
->parent
== 0 || r
->root
== r
->parent
) {
3426 /* look for parent */
3427 for (i
= 0; i
< num_screens
; i
++)
3428 for (j
= 0; j
< workspace_limit
; j
++)
3429 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3430 if (r
->parent
== win
->id
) {
3440 spawn(int ws_idx
, union arg
*args
, bool close_fd
)
3445 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3447 close(xcb_get_file_descriptor(conn
));
3449 setenv("LD_PRELOAD", SWM_LIB
, 1);
3451 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3452 warn("spawn: asprintf SWM_WS");
3455 setenv("_SWM_WS", ret
, 1);
3459 if (asprintf(&ret
, "%d", getpid()) == -1) {
3460 warn("spawn: asprintf _SWM_PID");
3463 setenv("_SWM_PID", ret
, 1);
3467 if (setsid() == -1) {
3468 warn("spawn: setsid");
3474 * close stdin and stdout to prevent interaction between apps
3475 * and the baraction script
3476 * leave stderr open to record errors
3478 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3479 warn("spawn: open");
3482 dup2(fd
, STDIN_FILENO
);
3483 dup2(fd
, STDOUT_FILENO
);
3488 execvp(args
->argv
[0], args
->argv
);
3490 warn("spawn: execvp");
3495 kill_refs(struct ws_win
*win
)
3497 int i
, x
, num_screens
;
3498 struct swm_region
*r
;
3499 struct workspace
*ws
;
3504 num_screens
= get_screen_count();
3505 for (i
= 0; i
< num_screens
; i
++)
3506 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3507 for (x
= 0; x
< workspace_limit
; x
++) {
3509 if (win
== ws
->focus
)
3511 if (win
== ws
->focus_prev
)
3512 ws
->focus_prev
= NULL
;
3517 validate_win(struct ws_win
*testwin
)
3520 struct workspace
*ws
;
3521 struct swm_region
*r
;
3522 int i
, x
, num_screens
;
3524 if (testwin
== NULL
)
3527 num_screens
= get_screen_count();
3528 for (i
= 0; i
< num_screens
; i
++)
3529 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3530 for (x
= 0; x
< workspace_limit
; x
++) {
3532 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3540 validate_ws(struct workspace
*testws
)
3542 struct swm_region
*r
;
3543 struct workspace
*ws
;
3544 int i
, x
, num_screens
;
3546 /* validate all ws */
3547 num_screens
= get_screen_count();
3548 for (i
= 0; i
< num_screens
; i
++)
3549 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3550 for (x
= 0; x
< workspace_limit
; x
++) {
3559 unfocus_win(struct ws_win
*win
)
3561 xcb_window_t none
= XCB_WINDOW_NONE
;
3563 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: win %#x\n", WINID(win
));
3568 if (win
->ws
== NULL
) {
3569 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3573 if (validate_ws(win
->ws
)) {
3574 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3578 if (win
->ws
->r
== NULL
) {
3579 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3583 if (validate_win(win
)) {
3584 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3589 if (win
->ws
->focus
== win
) {
3590 win
->ws
->focus
= NULL
;
3591 win
->ws
->focus_prev
= win
;
3594 if (validate_win(win
->ws
->focus
)) {
3595 kill_refs(win
->ws
->focus
);
3596 win
->ws
->focus
= NULL
;
3599 if (validate_win(win
->ws
->focus_prev
)) {
3600 kill_refs(win
->ws
->focus_prev
);
3601 win
->ws
->focus_prev
= NULL
;
3604 update_window_color(win
);
3606 /* Raise window to "top unfocused position." */
3607 if (win
->ws
->always_raise
)
3610 /* Update border width */
3611 if (win
->bordered
&& (win
->quirks
& SWM_Q_MINIMALBORDER
) &&
3614 X(win
) += border_width
;
3615 Y(win
) += border_width
;
3619 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3620 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3622 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3626 focus_win(struct ws_win
*win
)
3628 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
, *tmpw
;
3629 struct workspace
*ws
;
3630 xcb_get_input_focus_reply_t
*gifr
;
3632 DNPRINTF(SWM_D_FOCUS
, "focus_win: win %#x\n", WINID(win
));
3634 if (win
== NULL
|| win
->ws
== NULL
|| !win
->mapped
)
3639 if (validate_ws(ws
))
3642 if (validate_win(win
)) {
3647 gifr
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3649 cfw
= find_window(gifr
->focus
);
3650 if (cfw
!= NULL
&& cfw
!= win
) {
3651 if (cfw
->ws
!= ws
&& cfw
->ws
->r
!= NULL
) {
3652 /* Change border to unfocused color. */
3653 xcb_change_window_attributes(conn
, cfw
->id
,
3654 XCB_CW_BORDER_PIXEL
,
3655 &cfw
->s
->c
[(MAXIMIZED(cfw
) ?
3656 SWM_S_COLOR_UNFOCUS_MAXIMIZED
:
3657 SWM_S_COLOR_UNFOCUS
)].pixel
);
3665 if (ws
->focus
!= win
) {
3666 if (ws
->focus
&& ws
->focus
!= cfw
)
3667 unfocus_win(ws
->focus
);
3671 /* If this window directs focus to a child window, then clear. */
3672 if (win
->focus_child
)
3673 win
->focus_child
= NULL
;
3675 /* If transient, adjust parent's focus child for focus_magic. */
3677 parent
= find_window(win
->transient
);
3678 if (parent
&& parent
->focus_child
!= win
)
3679 parent
->focus_child
= win
;
3682 if (cfw
!= win
&& ws
->r
!= NULL
) {
3683 /* Set input focus if no input hint, or indicated by hint. */
3684 if (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
3685 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
3687 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3688 win
->id
, last_event_time
);
3690 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3691 ws
->r
->id
, XCB_CURRENT_TIME
);
3693 /* Tell app it can adjust focus to a specific window. */
3694 if (win
->take_focus
) {
3695 /* java is special; always tell parent */
3696 if (TRANS(win
) && win
->java
)
3697 client_msg(parent
, a_takefocus
,
3700 client_msg(win
, a_takefocus
, last_event_time
);
3703 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
3705 /* If a parent exists, map it first. */
3707 raise_window(parent
);
3710 /* Map siblings next. */
3711 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
,
3713 if (w
!= win
&& !ICONIC(w
) &&
3714 w
->transient
== parent
->id
) {
3720 /* Map focused window. */
3724 /* Stack any children of focus window. */
3725 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
3726 if (w
->transient
== win
->id
&& !ICONIC(w
)) {
3730 } else if (tile_gap
< 0 && !ABOVE(win
)) {
3732 * Windows overlap in the layout.
3733 * Raise focused win above all tiled wins.
3741 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3742 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3747 /* Update window border even if workspace is hidden. */
3748 update_window_color(win
);
3750 /* Update border width */
3751 if (!win
->bordered
&& WS_FOCUSED(win
->ws
) &&
3752 (win
->quirks
& SWM_Q_MINIMALBORDER
) && FLOATING(win
)) {
3754 X(win
) -= border_width
;
3755 Y(win
) -= border_width
;
3763 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
3766 /* If a child window should have focus instead, return it. */
3768 get_focus_magic(struct ws_win
*win
)
3770 struct ws_win
*parent
= NULL
;
3771 struct ws_win
*child
= NULL
;
3773 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: win %#x\n", WINID(win
));
3778 parent
= find_window(win
->transient
);
3780 /* If parent prefers focus elsewhere, then try to do so. */
3781 if (parent
&& (child
= parent
->focus_child
)) {
3782 if (validate_win(child
) == 0 && child
->mapped
)
3785 parent
->focus_child
= NULL
;
3789 /* If this window prefers focus elsewhere, then try to do so. */
3790 if ((child
= win
->focus_child
)) {
3791 if (validate_win(child
) == 0 && child
->mapped
)
3794 win
->focus_child
= NULL
;
3801 event_drain(uint8_t rt
)
3803 xcb_generic_event_t
*evt
;
3805 /* ensure all pending requests have been processed before filtering. */
3807 while ((evt
= xcb_poll_for_event(conn
))) {
3808 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
3816 set_region(struct swm_region
*r
)
3818 struct swm_region
*rf
;
3825 /* Unfocus old region bar. */
3830 xcb_change_window_attributes(conn
, rf
->bar
->id
,
3831 XCB_CW_BORDER_PIXEL
,
3832 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
3835 if (rf
!= NULL
&& rf
!= r
&& (X(rf
) != X(r
) || Y(rf
) != Y(r
) ||
3836 WIDTH(rf
) != WIDTH(r
) || HEIGHT(rf
) != HEIGHT(r
))) {
3837 /* Set _NET_DESKTOP_GEOMETRY. */
3839 vals
[1] = HEIGHT(r
);
3840 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, r
->s
->root
,
3841 ewmh
[_NET_DESKTOP_GEOMETRY
].atom
, XCB_ATOM_CARDINAL
, 32, 2,
3845 /* Set region bar border to focus_color. */
3846 xcb_change_window_attributes(conn
, r
->bar
->id
,
3847 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
3851 ewmh_update_current_desktop();
3855 focus_region(struct swm_region
*r
)
3858 struct swm_region
*old_r
;
3863 old_r
= r
->s
->r_focus
;
3866 nfw
= get_region_focus(r
);
3870 /* New region is empty; need to manually unfocus win. */
3872 unfocus_win(old_r
->ws
->focus
);
3874 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
3877 /* Clear bar since empty. */
3883 switchws(struct swm_region
*r
, union arg
*args
)
3885 struct swm_region
*this_r
, *other_r
;
3887 struct workspace
*new_ws
, *old_ws
;
3888 xcb_window_t none
= XCB_WINDOW_NONE
;
3889 int wsid
= args
->id
;
3890 bool unmap_old
= false;
3895 if (wsid
>= workspace_limit
)
3899 old_ws
= this_r
->ws
;
3900 new_ws
= &this_r
->s
->ws
[wsid
];
3902 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3903 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
3905 if (new_ws
== NULL
|| old_ws
== NULL
)
3907 if (new_ws
== old_ws
)
3910 other_r
= new_ws
->r
;
3911 if (other_r
&& workspace_clamp
) {
3912 DNPRINTF(SWM_D_WS
, "switchws: ws clamped.\n");
3915 DNPRINTF(SWM_D_WS
, "switchws: warping focus to region "
3916 "with ws %d.\n", wsid
);
3917 focus_region(other_r
);
3918 center_pointer(other_r
);
3924 if ((win
= old_ws
->focus
) != NULL
) {
3925 update_window_color(win
);
3927 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3928 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3933 /* the other ws is visible in another region, exchange them */
3934 other_r
->ws_prior
= new_ws
;
3935 other_r
->ws
= old_ws
;
3936 old_ws
->r
= other_r
;
3938 /* the other workspace is hidden, hide this one */
3943 this_r
->ws_prior
= old_ws
;
3944 this_r
->ws
= new_ws
;
3947 /* Set focus_pending before stacking, if needed. */
3948 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
3949 validate_win(new_ws
->focus_pending
))) {
3950 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
3951 new_ws
->focus
= new_ws
->focus_prev
;
3952 new_ws
->focus_prev
= NULL
;
3955 new_ws
->state
= SWM_WS_STATE_MAPPING
;
3958 /* unmap old windows */
3960 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
3962 old_ws
->state
= SWM_WS_STATE_HIDDEN
;
3965 /* if workspaces were swapped, then don't wait to set focus */
3966 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
3967 if (new_ws
->focus_pending
) {
3968 focus_win(new_ws
->focus_pending
);
3969 new_ws
->focus_pending
= NULL
;
3973 /* Clear bar and set focus on region input win if new ws is empty. */
3974 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
3975 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
3980 ewmh_update_current_desktop();
3984 new_ws
->state
= SWM_WS_STATE_MAPPED
;
3986 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
3990 cyclews(struct swm_region
*r
, union arg
*args
)
3993 struct swm_screen
*s
= r
->s
;
3994 bool cycle_all
= false, mv
= false;
3996 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3997 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4003 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
4006 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
4009 case SWM_ARG_ID_CYCLEWS_UP
:
4010 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
4012 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
4015 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
4018 case SWM_ARG_ID_CYCLEWS_DOWN
:
4019 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
4026 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
4028 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
4035 } while (a
.id
!= r
->ws
->idx
);
4037 DNPRINTF(SWM_D_FOCUS
, "cyclews: done\n");
4041 priorws(struct swm_region
*r
, union arg
*args
)
4047 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4048 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4050 if (r
->ws_prior
== NULL
)
4053 a
.id
= r
->ws_prior
->idx
;
4055 DNPRINTF(SWM_D_FOCUS
, "priorws: done\n");
4059 focusrg(struct swm_region
*r
, union arg
*args
)
4061 int ridx
= args
->id
, i
, num_screens
;
4062 struct swm_region
*rr
= NULL
;
4064 num_screens
= get_screen_count();
4065 /* do nothing if we don't have more than one screen */
4066 if (!(num_screens
> 1 || outputs
> 1))
4069 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
4071 rr
= TAILQ_FIRST(&r
->s
->rl
);
4072 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
4073 rr
= TAILQ_NEXT(rr
, entry
);
4081 DNPRINTF(SWM_D_FOCUS
, "focusrg: done\n");
4085 cyclerg(struct swm_region
*r
, union arg
*args
)
4088 struct swm_region
*rr
= NULL
;
4091 num_screens
= get_screen_count();
4092 /* do nothing if we don't have more than one screen */
4093 if (!(num_screens
> 1 || outputs
> 1))
4097 DNPRINTF(SWM_D_FOCUS
, "cyclerg: id: %d, region: %d\n", args
->id
, i
);
4100 case SWM_ARG_ID_CYCLERG_UP
:
4101 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4102 rr
= TAILQ_NEXT(r
, entry
);
4104 rr
= TAILQ_FIRST(&screens
[i
].rl
);
4106 case SWM_ARG_ID_CYCLERG_DOWN
:
4107 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4108 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
4110 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
4119 case SWM_ARG_ID_CYCLERG_UP
:
4120 case SWM_ARG_ID_CYCLERG_DOWN
:
4125 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4126 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4134 DNPRINTF(SWM_D_FOCUS
, "cyclerg: done\n");
4137 /* Sorts transients after parent. */
4139 sort_windows(struct ws_win_list
*wl
)
4141 struct ws_win
*win
, *parent
, *nxt
;
4146 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
4147 nxt
= TAILQ_NEXT(win
, entry
);
4149 parent
= find_window(win
->transient
);
4150 if (parent
== NULL
) {
4151 warnx("not possible bug");
4154 TAILQ_REMOVE(wl
, win
, entry
);
4155 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
4161 swapwin(struct swm_region
*r
, union arg
*args
)
4163 struct ws_win
*target
, *source
;
4164 struct ws_win
*cur_focus
;
4165 struct ws_win_list
*wl
;
4167 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4168 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4170 cur_focus
= r
->ws
->focus
;
4171 if (cur_focus
== NULL
|| FULLSCREEN(cur_focus
))
4174 /* Adjust stacking in floating layer. */
4175 if (ABOVE(cur_focus
)) {
4177 case SWM_ARG_ID_SWAPPREV
:
4178 target
= TAILQ_PREV(cur_focus
, ws_win_stack
,
4180 if (target
!= NULL
&& FLOATING(target
)) {
4181 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4183 TAILQ_INSERT_BEFORE(target
, cur_focus
,
4185 update_win_stacking(cur_focus
);
4189 case SWM_ARG_ID_SWAPNEXT
:
4190 target
= TAILQ_NEXT(cur_focus
, stack_entry
);
4191 if (target
!= NULL
&& FLOATING(target
)) {
4192 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4194 TAILQ_INSERT_AFTER(&cur_focus
->ws
->stack
,
4195 target
, cur_focus
, stack_entry
);
4196 update_win_stacking(cur_focus
);
4204 if (r
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4207 clear_maximized(r
->ws
);
4210 wl
= &source
->ws
->winlist
;
4213 case SWM_ARG_ID_SWAPPREV
:
4215 source
= find_window(source
->transient
);
4216 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
4217 if (target
&& target
->transient
)
4218 target
= find_window(target
->transient
);
4219 TAILQ_REMOVE(wl
, source
, entry
);
4221 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4223 TAILQ_INSERT_BEFORE(target
, source
, entry
);
4225 case SWM_ARG_ID_SWAPNEXT
:
4226 target
= TAILQ_NEXT(source
, entry
);
4227 /* move the parent and let the sort handle the move */
4229 source
= find_window(source
->transient
);
4230 TAILQ_REMOVE(wl
, source
, entry
);
4232 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4234 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
4236 case SWM_ARG_ID_SWAPMAIN
:
4237 target
= TAILQ_FIRST(wl
);
4238 if (target
== source
) {
4239 if (source
->ws
->focus_prev
!= NULL
&&
4240 source
->ws
->focus_prev
!= target
)
4241 source
= source
->ws
->focus_prev
;
4245 if (target
== NULL
|| source
== NULL
)
4247 source
->ws
->focus_prev
= target
;
4248 TAILQ_REMOVE(wl
, target
, entry
);
4249 TAILQ_INSERT_BEFORE(source
, target
, entry
);
4250 TAILQ_REMOVE(wl
, source
, entry
);
4251 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4253 case SWM_ARG_ID_MOVELAST
:
4254 TAILQ_REMOVE(wl
, source
, entry
);
4255 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4258 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
4263 ewmh_update_client_list();
4269 DNPRINTF(SWM_D_MOVE
, "swapwin: done\n");
4273 get_focus_prev(struct ws_win
*win
)
4275 struct ws_win
*winfocus
= NULL
;
4276 struct ws_win
*cur_focus
= NULL
;
4277 struct ws_win_list
*wl
= NULL
;
4278 struct workspace
*ws
= NULL
;
4280 if (!(win
&& win
->ws
))
4285 cur_focus
= ws
->focus
;
4287 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: win %#x, cur_focus: %#x, "
4288 "focus_prev: %#x\n", WINID(win
), WINID(cur_focus
),
4289 WINID(ws
->focus_prev
));
4291 /* pickle, just focus on whatever */
4292 if (cur_focus
== NULL
) {
4293 /* use prev_focus if valid */
4294 if (ws
->focus_prev
&& find_window(ws
->focus_prev
->id
))
4295 winfocus
= ws
->focus_prev
;
4299 /* if transient focus on parent */
4300 if (TRANS(cur_focus
)) {
4301 winfocus
= find_window(cur_focus
->transient
);
4305 /* if in max_stack try harder */
4306 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
4307 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
4308 if (cur_focus
!= ws
->focus_prev
)
4309 winfocus
= ws
->focus_prev
;
4311 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
4316 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
4318 if (winfocus
== NULL
|| winfocus
== win
) {
4319 switch (focus_close
) {
4320 case SWM_STACK_BOTTOM
:
4321 TAILQ_FOREACH(winfocus
, wl
, entry
)
4322 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4326 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4327 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4330 case SWM_STACK_ABOVE
:
4331 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
4332 while (winfocus
&& ICONIC(winfocus
))
4333 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4335 if (winfocus
== NULL
) {
4336 if (focus_close_wrap
) {
4337 TAILQ_FOREACH(winfocus
, wl
, entry
)
4338 if (!ICONIC(winfocus
) &&
4339 winfocus
!= cur_focus
)
4342 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4344 if (!ICONIC(winfocus
) &&
4345 winfocus
!= cur_focus
)
4350 case SWM_STACK_BELOW
:
4351 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
4352 while (winfocus
&& ICONIC(winfocus
))
4353 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
4356 if (winfocus
== NULL
) {
4357 if (focus_close_wrap
) {
4358 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4360 if (!ICONIC(winfocus
) &&
4361 winfocus
!= cur_focus
)
4364 TAILQ_FOREACH(winfocus
, wl
, entry
)
4365 if (!ICONIC(winfocus
) &&
4366 winfocus
!= cur_focus
)
4374 if (winfocus
== NULL
||
4375 (winfocus
&& (ICONIC(winfocus
) || winfocus
== cur_focus
))) {
4376 if (focus_default
== SWM_STACK_TOP
) {
4377 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4378 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4381 TAILQ_FOREACH(winfocus
, wl
, entry
)
4382 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4389 return get_focus_magic(winfocus
);
4393 get_region_focus(struct swm_region
*r
)
4395 struct ws_win
*winfocus
= NULL
;
4400 if (r
->ws
->focus
&& !ICONIC(r
->ws
->focus
))
4401 winfocus
= r
->ws
->focus
;
4402 else if (r
->ws
->focus_prev
&& !ICONIC(r
->ws
->focus_prev
))
4403 winfocus
= r
->ws
->focus_prev
;
4405 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
4406 if (!ICONIC(winfocus
))
4409 return get_focus_magic(winfocus
);
4413 focus(struct swm_region
*r
, union arg
*args
)
4415 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
4416 struct ws_win_list
*wl
= NULL
;
4417 struct workspace
*ws
= NULL
;
4424 cur_focus
= r
->ws
->focus
;
4428 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d, cur_focus: %#x\n", args
->id
,
4431 /* Make sure an uniconified window has focus, if one exists. */
4432 if (cur_focus
== NULL
) {
4433 cur_focus
= TAILQ_FIRST(wl
);
4434 while (cur_focus
!= NULL
&& ICONIC(cur_focus
))
4435 cur_focus
= TAILQ_NEXT(cur_focus
, entry
);
4437 DNPRINTF(SWM_D_FOCUS
, "focus: new cur_focus: %#x\n",
4442 case SWM_ARG_ID_FOCUSPREV
:
4443 if (cur_focus
== NULL
)
4446 winfocus
= cur_focus
;
4448 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
4449 if (winfocus
== NULL
)
4450 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
4451 if (winfocus
== cur_focus
)
4453 } while (winfocus
&& (ICONIC(winfocus
) ||
4454 winfocus
->id
== cur_focus
->transient
||
4455 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4456 winfocus
->transient
== cur_focus
->transient
) ||
4457 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4459 case SWM_ARG_ID_FOCUSNEXT
:
4460 if (cur_focus
== NULL
)
4463 winfocus
= cur_focus
;
4465 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4466 if (winfocus
== NULL
)
4467 winfocus
= TAILQ_FIRST(wl
);
4468 if (winfocus
== cur_focus
)
4470 } while (winfocus
&& (ICONIC(winfocus
) ||
4471 winfocus
->id
== cur_focus
->transient
||
4472 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4473 winfocus
->transient
== cur_focus
->transient
) ||
4474 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4476 case SWM_ARG_ID_FOCUSMAIN
:
4477 if (cur_focus
== NULL
)
4480 winfocus
= TAILQ_FIRST(wl
);
4481 if (winfocus
== cur_focus
)
4482 winfocus
= cur_focus
->ws
->focus_prev
;
4484 case SWM_ARG_ID_FOCUSURGENT
:
4485 /* Search forward for the next urgent window. */
4489 for (i
= 0; i
<= workspace_limit
; ++i
) {
4491 head
= TAILQ_FIRST(&r
->s
->ws
[(ws
->idx
+ i
) %
4492 workspace_limit
].winlist
);
4495 if (head
== cur_focus
) {
4497 winfocus
= cur_focus
;
4500 } else if (get_urgent(head
)) {
4505 head
= TAILQ_NEXT(head
, entry
);
4512 /* Switch ws if new focus is on a different ws. */
4513 if (winfocus
&& winfocus
->ws
!= ws
) {
4514 a
.id
= winfocus
->ws
->idx
;
4522 if (clear_maximized(ws
) > 0)
4525 focus_win(get_focus_magic(winfocus
));
4530 DNPRINTF(SWM_D_FOCUS
, "focus: done\n");
4534 cycle_layout(struct swm_region
*r
, union arg
*args
)
4536 struct workspace
*ws
= r
->ws
;
4538 /* suppress unused warning since var is needed */
4541 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
4544 if (ws
->cur_layout
->l_stack
== NULL
)
4545 ws
->cur_layout
= &layouts
[0];
4547 clear_maximized(ws
);
4552 focus_win(get_region_focus(r
));
4559 stack_config(struct swm_region
*r
, union arg
*args
)
4561 struct workspace
*ws
= r
->ws
;
4563 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
4566 if (clear_maximized(ws
) > 0)
4569 if (ws
->cur_layout
->l_config
!= NULL
)
4570 ws
->cur_layout
->l_config(ws
, args
->id
);
4572 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
4582 struct swm_geometry g
;
4583 struct swm_region
*r
, *r_prev
= NULL
;
4590 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
4592 num_screens
= get_screen_count();
4593 for (i
= 0; i
< num_screens
; i
++) {
4597 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
4598 /* Adjust stack area for region bar and padding. */
4600 g
.x
+= region_padding
;
4601 g
.y
+= region_padding
;
4602 g
.w
-= 2 * border_width
+ 2 * region_padding
;
4603 g
.h
-= 2 * border_width
+ 2 * region_padding
;
4604 if (bar_enabled
&& r
->ws
->bar_enabled
) {
4610 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d (screen: "
4611 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4612 r
->ws
->idx
, i
, j
++, g
.x
, g
.y
, g
.w
, g
.h
);
4615 /* Stack bar/input relative to prev. region. */
4616 val
[1] = XCB_STACK_MODE_ABOVE
;
4618 val
[0] = r_prev
->id
;
4619 DNPRINTF(SWM_D_STACK
, "stack: region input %#x "
4620 "relative to %#x.\n", r
->id
, val
[0]);
4621 xcb_configure_window(conn
, r
->id
,
4622 XCB_CONFIG_WINDOW_SIBLING
|
4623 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4625 val
[0] = r_prev
->bar
->id
;
4626 DNPRINTF(SWM_D_STACK
, "stack: region bar %#x "
4627 "relative to %#x.\n", r
->bar
->id
, val
[0]);
4628 xcb_configure_window(conn
, r
->bar
->id
,
4629 XCB_CONFIG_WINDOW_SIBLING
|
4630 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4633 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
4634 r
->ws
->cur_layout
->l_string(r
->ws
);
4635 /* save r so we can track region changes */
4643 DNPRINTF(SWM_D_STACK
, "stack: end\n");
4647 store_float_geom(struct ws_win
*win
)
4649 if (win
== NULL
|| win
->ws
->r
== NULL
)
4652 /* retain window geom and region geom */
4653 win
->g_float
= win
->g
;
4654 win
->g_float
.x
-= X(win
->ws
->r
);
4655 win
->g_float
.y
-= Y(win
->ws
->r
);
4656 win
->g_floatvalid
= true;
4657 DNPRINTF(SWM_D_MISC
, "store_float_geom: win %#x, g: (%d,%d)"
4658 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
4659 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
4660 win
->g_float
.w
, win
->g_float
.h
);
4664 load_float_geom(struct ws_win
*win
)
4666 if (win
== NULL
|| win
->ws
->r
== NULL
)
4669 if (win
->g_floatvalid
) {
4670 win
->g
= win
->g_float
;
4671 X(win
) += X(win
->ws
->r
);
4672 Y(win
) += Y(win
->ws
->r
);
4673 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g: (%d,%d)"
4674 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
4677 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g_float "
4678 "is not set.\n", win
->id
);
4683 update_floater(struct ws_win
*win
)
4685 struct workspace
*ws
;
4686 struct swm_region
*r
;
4693 if ((r
= ws
->r
) == NULL
)
4696 DNPRINTF(SWM_D_MISC
, "update_floater: win %#x\n", win
->id
);
4698 win
->bordered
= true;
4700 if (FULLSCREEN(win
)) {
4701 /* _NET_WM_FULLSCREEN: fullscreen without border. */
4702 if (!win
->g_floatvalid
)
4703 store_float_geom(win
);
4706 win
->bordered
= false;
4707 } else if (MAXIMIZED(win
)) {
4708 /* Maximize: like a single stacked window. */
4709 if (!win
->g_floatvalid
)
4710 store_float_geom(win
);
4714 if (bar_enabled
&& ws
->bar_enabled
&& !maximize_hide_bar
) {
4716 Y(win
) += bar_height
;
4717 HEIGHT(win
) -= bar_height
;
4718 } else if (disable_border
) {
4719 win
->bordered
= false;
4722 if (win
->bordered
) {
4723 HEIGHT(win
) -= 2 * border_width
;
4724 WIDTH(win
) -= 2 * border_width
;
4727 /* Normal floating window. */
4728 /* Update geometry if new region. */
4730 load_float_geom(win
);
4732 if (((win
->quirks
& SWM_Q_FULLSCREEN
) &&
4733 WIDTH(win
) >= WIDTH(r
) && HEIGHT(win
) >= HEIGHT(r
)) ||
4734 ((!WS_FOCUSED(win
->ws
) || win
->ws
->focus
!= win
) &&
4735 (win
->quirks
& SWM_Q_MINIMALBORDER
))) {
4737 win
->bordered
= false;
4738 } else if (!MANUAL(win
)) {
4739 if (TRANS(win
) && (win
->quirks
& SWM_Q_TRANSSZ
)) {
4740 /* Adjust size on TRANSSZ quirk. */
4741 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
4742 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
4745 if (!(win
->quirks
& SWM_Q_ANYWHERE
)) {
4747 * Floaters and transients are auto-centred
4748 * unless manually moved, resized or ANYWHERE
4751 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 -
4753 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 -
4755 store_float_geom(win
);
4760 /* Ensure at least 1 pixel of the window is in the region. */
4761 region_containment(win
, r
, SWM_CW_ALLSIDES
);
4767 * Send keystrokes to terminal to decrease/increase the font size as the
4768 * window size changes.
4771 adjust_font(struct ws_win
*win
)
4773 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
4774 ABOVE(win
) || TRANS(win
))
4777 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
4778 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
4779 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
4780 win
->font_size_boundary
[win
->font_steps
] =
4781 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
4784 win
->last_inc
= win
->sh
.width_inc
;
4785 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
4786 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
4787 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
4790 win
->last_inc
= win
->sh
.width_inc
;
4791 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
4795 #define SWAPXY(g) do { \
4797 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4798 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4801 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, bool flip
)
4803 struct swm_geometry win_g
, r_g
= *g
;
4805 int i
, j
, s
, stacks
;
4806 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
4807 int hrh
, extra
= 0, h_slice
, last_h
= 0;
4808 int split
, colno
, winno
, mwin
, msize
, mscale
;
4809 int remain
, missing
, v_slice
;
4810 bool bordered
= true, reconfigure
= false;
4812 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
4813 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
4815 /* Prepare tiling variables, if needed. */
4816 if ((winno
= count_win(ws
, false)) > 0) {
4817 /* Find first tiled window. */
4818 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
4819 if (!FLOATING(win
) && !ICONIC(win
))
4822 /* Take into account size hints of first tiled window. */
4824 w_inc
= win
->sh
.width_inc
;
4825 w_base
= win
->sh
.base_width
;
4826 mwin
= ws
->l_state
.horizontal_mwin
;
4827 mscale
= ws
->l_state
.horizontal_msize
;
4828 stacks
= ws
->l_state
.horizontal_stacks
;
4831 w_inc
= win
->sh
.height_inc
;
4832 w_base
= win
->sh
.base_height
;
4833 mwin
= ws
->l_state
.vertical_mwin
;
4834 mscale
= ws
->l_state
.vertical_msize
;
4835 stacks
= ws
->l_state
.vertical_stacks
;
4839 if (stacks
> winno
- mwin
)
4840 stacks
= winno
- mwin
;
4844 h_slice
= r_g
.h
/ SWM_H_SLICE
;
4845 if (mwin
&& winno
> mwin
) {
4846 v_slice
= r_g
.w
/ SWM_V_SLICE
;
4850 win_g
.w
= v_slice
* mscale
;
4852 if (w_inc
> 1 && w_inc
< v_slice
) {
4853 /* Adjust for requested size increment. */
4854 remain
= (win_g
.w
- w_base
) % w_inc
;
4860 win_g
.x
+= r_g
.w
- msize
;
4863 colno
= split
= winno
/ stacks
;
4864 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
4865 2 * border_width
) / stacks
);
4867 hrh
= r_g
.h
/ colno
;
4868 extra
= r_g
.h
- (colno
* hrh
);
4869 win_g
.h
= hrh
- 2 * border_width
;
4870 i
= j
= 0, s
= stacks
;
4873 /* Update window geometry. */
4874 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
4878 if (FLOATING(win
)) {
4879 update_floater(win
);
4884 if (split
&& i
== split
) {
4885 colno
= (winno
- mwin
) / stacks
;
4886 if (s
<= (winno
- mwin
) % stacks
)
4889 hrh
= r_g
.h
/ colno
;
4890 extra
= r_g
.h
- (colno
* hrh
);
4893 win_g
.x
+= win_g
.w
+ 2 * border_width
+
4896 win_g
.w
= (r_g
.w
- msize
-
4897 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
4899 win_g
.w
+= (r_g
.w
- msize
-
4900 (stacks
* (2 * border_width
+ tile_gap
))) %
4904 win_g
.x
-= win_g
.w
+ 2 * border_width
+
4910 win_g
.h
= hrh
- 2 * border_width
- tile_gap
;
4913 h_inc
= win
->sh
.width_inc
;
4914 h_base
= win
->sh
.base_width
;
4916 h_inc
= win
->sh
.height_inc
;
4917 h_base
= win
->sh
.base_height
;
4920 if (j
== colno
- 1) {
4921 win_g
.h
= hrh
+ extra
;
4922 } else if (h_inc
> 1 && h_inc
< h_slice
) {
4923 /* adjust for window's requested size increment */
4924 remain
= (win_g
.h
- h_base
) % h_inc
;
4925 missing
= h_inc
- remain
;
4927 if (missing
<= extra
|| j
== 0) {
4939 win_g
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
4941 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
) &&
4944 win_g
.w
+= 2 * border_width
;
4945 win_g
.h
+= 2 * border_width
;
4951 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
4952 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
4956 WIDTH(win
) = win_g
.h
;
4957 HEIGHT(win
) = win_g
.w
;
4960 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
4961 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
4965 WIDTH(win
) = win_g
.w
;
4966 HEIGHT(win
) = win_g
.h
;
4970 if (bordered
!= win
->bordered
) {
4972 win
->bordered
= bordered
;
4985 /* Stack all windows from bottom up. */
4986 TAILQ_FOREACH_REVERSE(win
, &ws
->stack
, ws_win_stack
, stack_entry
)
4988 update_win_stacking(win
);
4990 /* Map all windows from top down. */
4991 TAILQ_FOREACH(win
, &ws
->stack
, stack_entry
)
4995 DNPRINTF(SWM_D_STACK
, "stack_master: done\n");
4999 vertical_config(struct workspace
*ws
, int id
)
5001 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
5005 case SWM_ARG_ID_STACKRESET
:
5006 case SWM_ARG_ID_STACKINIT
:
5007 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
5008 ws
->l_state
.vertical_mwin
= 1;
5009 ws
->l_state
.vertical_stacks
= 1;
5011 case SWM_ARG_ID_MASTERSHRINK
:
5012 if (ws
->l_state
.vertical_msize
> 1)
5013 ws
->l_state
.vertical_msize
--;
5015 case SWM_ARG_ID_MASTERGROW
:
5016 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
5017 ws
->l_state
.vertical_msize
++;
5019 case SWM_ARG_ID_MASTERADD
:
5020 ws
->l_state
.vertical_mwin
++;
5022 case SWM_ARG_ID_MASTERDEL
:
5023 if (ws
->l_state
.vertical_mwin
> 0)
5024 ws
->l_state
.vertical_mwin
--;
5026 case SWM_ARG_ID_STACKBALANCE
:
5027 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ (ws
->l_state
.vertical_stacks
+ 1);
5029 case SWM_ARG_ID_STACKINC
:
5030 ws
->l_state
.vertical_stacks
++;
5032 case SWM_ARG_ID_STACKDEC
:
5033 if (ws
->l_state
.vertical_stacks
> 1)
5034 ws
->l_state
.vertical_stacks
--;
5036 case SWM_ARG_ID_FLIPLAYOUT
:
5037 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
5045 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5047 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
5049 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
5053 horizontal_config(struct workspace
*ws
, int id
)
5055 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
5058 case SWM_ARG_ID_STACKRESET
:
5059 case SWM_ARG_ID_STACKINIT
:
5060 ws
->l_state
.horizontal_mwin
= 1;
5061 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
5062 ws
->l_state
.horizontal_stacks
= 1;
5064 case SWM_ARG_ID_MASTERSHRINK
:
5065 if (ws
->l_state
.horizontal_msize
> 1)
5066 ws
->l_state
.horizontal_msize
--;
5068 case SWM_ARG_ID_MASTERGROW
:
5069 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
5070 ws
->l_state
.horizontal_msize
++;
5072 case SWM_ARG_ID_MASTERADD
:
5073 ws
->l_state
.horizontal_mwin
++;
5075 case SWM_ARG_ID_MASTERDEL
:
5076 if (ws
->l_state
.horizontal_mwin
> 0)
5077 ws
->l_state
.horizontal_mwin
--;
5079 case SWM_ARG_ID_STACKBALANCE
:
5080 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ (ws
->l_state
.horizontal_stacks
+ 1);
5082 case SWM_ARG_ID_STACKINC
:
5083 ws
->l_state
.horizontal_stacks
++;
5085 case SWM_ARG_ID_STACKDEC
:
5086 if (ws
->l_state
.horizontal_stacks
> 1)
5087 ws
->l_state
.horizontal_stacks
--;
5089 case SWM_ARG_ID_FLIPLAYOUT
:
5090 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
5098 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5100 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
5102 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
5105 /* fullscreen view */
5107 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5109 struct swm_geometry gg
= *g
;
5110 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
, *tmpw
;
5113 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
5118 winno
= count_win(ws
, false);
5119 if (winno
== 0 && count_win(ws
, true) == 0)
5122 /* Figure out which top level window should be visible. */
5123 if (ws
->focus_pending
)
5124 win
= ws
->focus_pending
;
5127 else if (ws
->focus_prev
)
5128 win
= ws
->focus_prev
;
5130 win
= TAILQ_FIRST(&ws
->winlist
);
5132 DNPRINTF(SWM_D_STACK
, "max_stack: focus_pending: %#x, focus: %#x, "
5133 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws
->focus_pending
),
5134 WINID(ws
->focus
), WINID(ws
->focus_prev
),
5135 WINID(TAILQ_FIRST(&ws
->winlist
)), win
->id
);
5137 /* Update window geometry. */
5138 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5147 /* Set maximized flag for all maxed windows. */
5148 if (!MAXIMIZED(w
)) {
5149 /* Preserve floating geometry. */
5151 store_float_geom(w
);
5153 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_MAXIMIZED
);
5154 ewmh_update_wm_state(w
);
5157 /* Only reconfigure if necessary. */
5158 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
5159 HEIGHT(w
) != gg
.h
) {
5162 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
)) {
5163 w
->bordered
= false;
5164 WIDTH(w
) += 2 * border_width
;
5165 HEIGHT(w
) += 2 * border_width
;
5174 /* If transient, stack parent and its children. */
5175 if (TRANS(win
) && (parent
= find_window(win
->transient
))) {
5176 raise_window(parent
);
5178 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5179 if (w
->transient
== parent
->id
)
5183 /* Make sure focus window is on top. */
5186 /* Stack any children of focus window. */
5187 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5188 if (w
->transient
== win
->id
)
5191 /* Map all windows. */
5192 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
5198 send_to_rg(struct swm_region
*r
, union arg
*args
)
5200 int ridx
= args
->id
, i
, num_screens
;
5201 struct swm_region
*rr
= NULL
;
5204 num_screens
= get_screen_count();
5205 /* do nothing if we don't have more than one screen */
5206 if (!(num_screens
> 1 || outputs
> 1))
5209 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
5211 rr
= TAILQ_FIRST(&r
->s
->rl
);
5212 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
5213 rr
= TAILQ_NEXT(rr
, entry
);
5224 region_under(struct swm_screen
*s
, int x
, int y
)
5226 struct swm_region
*r
= NULL
;
5228 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
5229 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
5230 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
5231 WIDTH(r
), HEIGHT(r
), x
, y
);
5232 if (X(r
) <= x
&& x
< MAX_X(r
))
5233 if (Y(r
) <= y
&& y
< MAX_Y(r
))
5240 /* Transfer focused window to target workspace and focus. */
5242 send_to_ws(struct swm_region
*r
, union arg
*args
)
5244 int wsid
= args
->id
;
5245 struct ws_win
*win
= NULL
;
5247 if (r
&& r
->ws
&& r
->ws
->focus
)
5252 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win %#x, ws %d\n", win
->id
, wsid
);
5254 if (wsid
< 0 || wsid
>= workspace_limit
)
5257 if (win
->ws
->idx
== wsid
)
5260 win_to_ws(win
, wsid
, true);
5262 /* Set new focus on target ws. */
5263 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5264 win
->ws
->focus_prev
= win
->ws
->focus
;
5265 win
->ws
->focus
= win
;
5266 win
->ws
->focus_pending
= NULL
;
5268 if (win
->ws
->focus_prev
)
5269 update_window_color(win
->ws
->focus_prev
);
5272 DNPRINTF(SWM_D_STACK
, "send_to_ws: focus_pending: %#x, focus: %#x, "
5273 "focus_prev: %#x, first: %#x, win: %#x\n",
5274 WINID(r
->ws
->focus_pending
), WINID(r
->ws
->focus
),
5275 WINID(r
->ws
->focus_prev
), WINID(TAILQ_FIRST(&r
->ws
->winlist
)),
5278 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5279 ewmh_update_wm_state(win
);
5281 /* Restack and set new focus on current ws. */
5283 load_float_geom(win
);
5287 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5288 if (r
->ws
->focus
!= NULL
) {
5289 focus_win(r
->ws
->focus
);
5291 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
5302 win_to_ws(struct ws_win
*win
, int wsid
, bool unfocus
)
5304 struct ws_win
*parent
;
5305 struct workspace
*ws
, *nws
, *pws
;
5307 if (wsid
< 0 || wsid
>= workspace_limit
)
5310 if (win
->ws
->idx
== wsid
)
5314 nws
= &win
->s
->ws
[wsid
];
5316 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win %#x, ws %d -> %d\n", win
->id
,
5319 /* Cleanup focus on source ws. */
5320 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
5321 (ws
->focus
== win
|| ws
->focus_pending
== win
))
5322 ws
->focus_pending
= get_focus_prev(win
);
5324 /* Move the parent if this is a transient window. */
5326 parent
= find_window(win
->transient
);
5329 /* Set new focus in parent's ws if needed. */
5330 if (pws
->focus
== parent
) {
5331 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5332 pws
->focus_pending
=
5333 get_focus_prev(parent
);
5335 unfocus_win(parent
);
5337 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5338 pws
->focus
= pws
->focus_pending
;
5339 pws
->focus_pending
= NULL
;
5343 /* Don't unmap parent if new ws is visible */
5345 unmap_window(parent
);
5348 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
5349 TAILQ_REMOVE(&ws
->stack
, parent
, stack_entry
);
5350 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
5351 TAILQ_INSERT_TAIL(&nws
->stack
, parent
, stack_entry
);
5354 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5355 "_NET_WM_DESKTOP: %d\n", wsid
);
5356 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5357 parent
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
5358 XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5365 if (ws
->focus_prev
== win
)
5366 ws
->focus_prev
= NULL
;
5368 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& ws
->focus_pending
!= NULL
) {
5369 ws
->focus
= ws
->focus_pending
;
5370 ws
->focus_pending
= NULL
;
5373 /* Don't unmap if new ws is visible */
5378 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
5379 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
5380 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
5381 TAILQ_INSERT_TAIL(&nws
->stack
, win
, stack_entry
);
5384 /* Update the window's workspace property: _NET_WM_DESKTOP */
5385 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5386 "_NET_WM_DESKTOP: %d\n", wsid
);
5387 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
5388 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5390 ewmh_update_client_list();
5392 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
5396 pressbutton(struct swm_region
*r
, union arg
*args
)
5398 /* suppress unused warning since var is needed */
5401 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
5402 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5403 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
5404 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5408 raise_toggle(struct swm_region
*r
, union arg
*args
)
5410 /* Suppress warning. */
5413 if (r
== NULL
|| r
->ws
== NULL
)
5416 if (r
->ws
->focus
&& MAXIMIZED(r
->ws
->focus
))
5419 r
->ws
->always_raise
= !r
->ws
->always_raise
;
5421 /* Update focused win stacking order based on new always_raise value. */
5422 raise_window(r
->ws
->focus
);
5428 iconify(struct swm_region
*r
, union arg
*args
)
5432 /* Suppress warning. */
5435 if ((w
= r
->ws
->focus
) == NULL
)
5438 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_HIDDEN
);
5439 ewmh_update_wm_state(w
);
5447 get_win_name(xcb_window_t win
)
5450 xcb_get_property_cookie_t c
;
5451 xcb_get_property_reply_t
*r
;
5453 /* First try _NET_WM_NAME for UTF-8. */
5454 c
= xcb_get_property(conn
, 0, win
, ewmh
[_NET_WM_NAME
].atom
,
5455 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5456 r
= xcb_get_property_reply(conn
, c
, NULL
);
5457 if (r
&& r
->type
== XCB_NONE
) {
5459 /* Use WM_NAME instead; no UTF-8. */
5460 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
5461 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5462 r
= xcb_get_property_reply(conn
, c
, NULL
);
5465 if (r
&& r
->type
!= XCB_NONE
&& r
->length
> 0)
5466 name
= strndup(xcb_get_property_value(r
),
5467 xcb_get_property_value_length(r
));
5472 err(1, "get_win_name: failed to allocate memory.");
5480 uniconify(struct swm_region
*r
, union arg
*args
)
5487 DNPRINTF(SWM_D_MISC
, "uniconify\n");
5489 if (r
== NULL
|| r
->ws
== NULL
)
5492 /* make sure we have anything to uniconify */
5493 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5494 if (win
->ws
== NULL
)
5495 continue; /* should never happen */
5501 DNPRINTF(SWM_D_MISC
, "uniconify: count: %d\n", count
);
5507 search_resp_action
= SWM_SEARCH_UNICONIFY
;
5509 spawn_select(r
, args
, "search", &searchpid
);
5511 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5514 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5515 if (win
->ws
== NULL
)
5516 continue; /* should never happen */
5520 name
= get_win_name(win
->id
);
5521 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
5529 name_workspace(struct swm_region
*r
, union arg
*args
)
5533 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
5539 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
5541 spawn_select(r
, args
, "name_workspace", &searchpid
);
5543 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5546 fprintf(lfile
, "%s", "");
5551 search_workspace(struct swm_region
*r
, union arg
*args
)
5554 struct workspace
*ws
;
5557 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
5563 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
5565 spawn_select(r
, args
, "search", &searchpid
);
5567 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5570 for (i
= 0; i
< workspace_limit
; i
++) {
5574 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
5575 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
5582 search_win_cleanup(void)
5584 struct search_window
*sw
= NULL
;
5586 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
5587 xcb_destroy_window(conn
, sw
->indicator
);
5588 TAILQ_REMOVE(&search_wl
, sw
, entry
);
5594 search_win(struct swm_region
*r
, union arg
*args
)
5596 struct ws_win
*win
= NULL
;
5597 struct search_window
*sw
= NULL
;
5600 xcb_screen_t
*screen
;
5601 int i
, width
, height
;
5609 XRectangle l_ibox
, l_lbox
;
5611 DNPRINTF(SWM_D_MISC
, "search_win\n");
5614 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
5616 spawn_select(r
, args
, "search", &searchpid
);
5618 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5621 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
5622 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
5624 TAILQ_INIT(&search_wl
);
5627 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5631 sw
= calloc(1, sizeof(struct search_window
));
5633 warn("search_win: calloc");
5635 search_win_cleanup();
5641 snprintf(s
, sizeof s
, "%d", i
);
5644 w
= xcb_generate_id(conn
);
5645 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
5646 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
5647 wa
[2] = screen
->default_colormap
;
5649 if (bar_font_legacy
) {
5650 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
5651 width
= l_lbox
.width
+ 4;
5652 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
5654 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
5656 width
= info
.width
+ 4;
5657 height
= bar_font
->height
+ 4;
5660 xcb_create_window(conn
, screen
->root_depth
, w
, win
->id
, 0, 0,
5661 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
5662 screen
->root_visual
, XCB_CW_BACK_PIXEL
|
5663 XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
, wa
);
5665 xcb_map_window(conn
, w
);
5668 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
5670 if (bar_font_legacy
) {
5671 l_gcv
.graphics_exposures
= 0;
5672 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
5674 XSetForeground(display
, l_draw
,
5675 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
5677 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
5678 (bar_fs_extents
->max_logical_extent
.height
-
5679 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
5681 XFreeGC(display
, l_draw
);
5684 draw
= XftDrawCreate(display
, w
,
5685 DefaultVisual(display
, r
->s
->idx
),
5686 DefaultColormap(display
, r
->s
->idx
));
5688 XftDrawStringUtf8(draw
, &search_font_color
, bar_font
, 2,
5689 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
5690 bar_font
->descent
, (FcChar8
*)s
, len
);
5692 XftDrawDestroy(draw
);
5695 DNPRINTF(SWM_D_MISC
, "search_win: mapped win %#x\n", w
);
5697 fprintf(lfile
, "%d\n", i
);
5707 search_resp_uniconify(const char *resp
, size_t len
)
5713 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
5715 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
5718 name
= get_win_name(win
->id
);
5719 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
5724 if (strncmp(s
, resp
, len
) == 0) {
5725 /* XXX this should be a callback to generalize */
5726 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_HIDDEN
);
5727 ewmh_update_wm_state(win
);
5737 search_resp_name_workspace(const char *resp
, size_t len
)
5739 struct workspace
*ws
;
5741 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
5743 if (search_r
->ws
== NULL
)
5748 free(search_r
->ws
->name
);
5749 search_r
->ws
->name
= NULL
;
5753 ws
->name
= strdup(resp
);
5754 if (ws
->name
== NULL
) {
5755 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
5756 "strdup: %s", strerror(errno
));
5760 ewmh_update_desktop_names();
5761 ewmh_get_desktop_names();
5766 ewmh_update_desktop_names(void)
5768 char *name_list
= NULL
, *p
;
5769 int num_screens
, i
, j
, len
= 0, tot
= 0;
5771 num_screens
= get_screen_count();
5772 for (i
= 0; i
< num_screens
; ++i
) {
5773 for (j
= 0; j
< workspace_limit
; ++j
) {
5774 if (screens
[i
].ws
[j
].name
!= NULL
)
5775 len
+= strlen(screens
[i
].ws
[j
].name
);
5779 if((name_list
= calloc(len
, sizeof(char))) == NULL
)
5780 err(1, "update_desktop_names: calloc: failed to "
5781 "allocate memory.");
5784 for (j
= 0; j
< workspace_limit
; ++j
) {
5785 if (screens
[i
].ws
[j
].name
!= NULL
) {
5786 len
= strlen(screens
[i
].ws
[j
].name
);
5787 memcpy(p
, screens
[i
].ws
[j
].name
, len
);
5796 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5797 screens
[i
].root
, ewmh
[_NET_DESKTOP_NAMES
].atom
,
5798 a_utf8_string
, 8, tot
, name_list
);
5808 ewmh_get_desktop_names(void)
5811 xcb_get_property_cookie_t c
;
5812 xcb_get_property_reply_t
*r
;
5813 int num_screens
, i
, j
, n
, k
;
5815 num_screens
= get_screen_count();
5816 for (i
= 0; i
< num_screens
; ++i
) {
5817 for (j
= 0; j
< workspace_limit
; ++j
) {
5818 free(screens
[i
].ws
[j
].name
);
5819 screens
[i
].ws
[j
].name
= NULL
;
5822 c
= xcb_get_property(conn
, 0, screens
[i
].root
,
5823 ewmh
[_NET_DESKTOP_NAMES
].atom
,
5824 a_utf8_string
, 0, UINT32_MAX
);
5825 r
= xcb_get_property_reply(conn
, c
, NULL
);
5829 names
= xcb_get_property_value(r
);
5830 n
= xcb_get_property_value_length(r
);
5832 for (j
= 0, k
= 0; j
< n
; ++j
) {
5833 if (*(names
+ j
) != '\0') {
5834 screens
[i
].ws
[k
].name
= strdup(names
+ j
);
5835 j
+= strlen(names
+ j
);
5844 ewmh_update_client_list(void)
5847 int num_screens
, i
, j
, k
= 0, count
= 0;
5850 num_screens
= get_screen_count();
5851 for (i
= 0; i
< num_screens
; ++i
) {
5852 for (j
= 0; j
< workspace_limit
; ++j
)
5853 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
5856 DNPRINTF(SWM_D_PROP
, "ewmh_update_client_list: win count: %d\n",
5862 wins
= calloc(count
, sizeof(xcb_window_t
));
5864 err(1, "ewmh_update_client_list: calloc: failed to "
5865 "allocate memory.");
5867 for (j
= 0, k
= 0; j
< workspace_limit
; ++j
)
5868 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
5869 wins
[k
++] = win
->id
;
5871 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5872 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
5873 XCB_ATOM_WINDOW
, 32, count
, wins
);
5880 ewmh_update_current_desktop(void)
5884 num_screens
= get_screen_count();
5885 for (i
= 0; i
< num_screens
; ++i
) {
5886 if (screens
[i
].r_focus
)
5887 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5888 screens
[i
].root
, ewmh
[_NET_CURRENT_DESKTOP
].atom
,
5889 XCB_ATOM_CARDINAL
, 32, 1,
5890 &screens
[i
].r_focus
->ws
->idx
);
5895 ewmh_update_desktops(void)
5897 int num_screens
, i
, j
;
5900 vals
= calloc(workspace_limit
* 2, sizeof(uint32_t));
5902 err(1, "ewmh_update_desktops: calloc: failed to allocate "
5905 num_screens
= get_screen_count();
5906 for (i
= 0; i
< num_screens
; i
++) {
5907 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5908 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
5909 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
5911 for (j
= 0; j
< workspace_limit
; ++j
) {
5912 if (screens
[i
].ws
[j
].r
!= NULL
) {
5913 vals
[j
* 2] = X(screens
[i
].ws
[j
].r
);
5914 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].r
);
5915 } else if (screens
[i
].ws
[j
].old_r
!= NULL
) {
5916 vals
[j
* 2] = X(screens
[i
].ws
[j
].old_r
);
5917 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].old_r
);
5919 vals
[j
* 2] = vals
[j
* 2 + 1] = 0;
5923 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5924 screens
[i
].root
, ewmh
[_NET_DESKTOP_VIEWPORT
].atom
,
5925 XCB_ATOM_CARDINAL
, 32, workspace_limit
* 2, vals
);
5932 search_resp_search_workspace(const char *resp
)
5939 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
5943 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
5950 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
5952 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
5959 switchws(search_r
, &a
);
5963 search_resp_search_window(const char *resp
)
5968 struct search_window
*sw
;
5970 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
5974 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
5979 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
5981 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
5988 TAILQ_FOREACH(sw
, &search_wl
, entry
)
5989 if (idx
== sw
->idx
) {
5995 #define MAX_RESP_LEN 1024
5998 search_do_resp(void)
6004 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
6009 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
6010 warn("search: calloc");
6014 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
6016 warn("search: read error");
6019 resp
[rbytes
] = '\0';
6022 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
6023 * newline, so work around that by sanitizing the resp now.
6025 resp
[strcspn(resp
, "\n")] = '\0';
6028 switch (search_resp_action
) {
6029 case SWM_SEARCH_UNICONIFY
:
6030 search_resp_uniconify(resp
, len
);
6032 case SWM_SEARCH_NAME_WORKSPACE
:
6033 search_resp_name_workspace(resp
, len
);
6035 case SWM_SEARCH_SEARCH_WORKSPACE
:
6036 search_resp_search_workspace(resp
);
6038 case SWM_SEARCH_SEARCH_WINDOW
:
6039 search_resp_search_window(resp
);
6044 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
6045 search_win_cleanup();
6047 search_resp_action
= SWM_SEARCH_NONE
;
6048 close(select_resp_pipe
[0]);
6055 wkill(struct swm_region
*r
, union arg
*args
)
6057 DNPRINTF(SWM_D_MISC
, "wkill: win %#x, id: %d\n", WINID(r
->ws
->focus
),
6060 if (r
->ws
->focus
== NULL
)
6063 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
6064 xcb_kill_client(conn
, r
->ws
->focus
->id
);
6066 if (r
->ws
->focus
->can_delete
)
6067 client_msg(r
->ws
->focus
, a_delete
, 0);
6073 clear_maximized(struct workspace
*ws
)
6078 /* Clear any maximized win(s) on ws, from bottom up. */
6079 TAILQ_FOREACH_REVERSE(w
, &ws
->stack
, ws_win_stack
, stack_entry
)
6081 ewmh_apply_flags(w
, w
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
6082 ewmh_update_wm_state(w
);
6090 maximize_toggle(struct swm_region
*r
, union arg
*args
)
6092 struct ws_win
*w
= r
->ws
->focus
;
6094 /* suppress unused warning since var is needed */
6100 DNPRINTF(SWM_D_MISC
, "maximize_toggle: win %#x\n", w
->id
);
6105 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6108 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_MAXIMIZED
);
6109 ewmh_update_wm_state(w
);
6113 if (w
== w
->ws
->focus
)
6118 DNPRINTF(SWM_D_MISC
, "maximize_toggle: done\n");
6122 floating_toggle(struct swm_region
*r
, union arg
*args
)
6124 struct ws_win
*w
= r
->ws
->focus
;
6126 /* suppress unused warning since var is needed */
6132 DNPRINTF(SWM_D_MISC
, "floating_toggle: win %#x\n", w
->id
);
6134 if (FULLSCREEN(w
) || TRANS(w
))
6137 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6140 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_ABOVE
);
6141 ewmh_update_wm_state(w
);
6145 if (w
== w
->ws
->focus
)
6150 DNPRINTF(SWM_D_MISC
, "floating_toggle: done\n");
6154 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
6156 struct swm_geometry g
= r
->g
;
6157 int rt
, lt
, tp
, bm
, bw
;
6159 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
6162 * Perpendicular distance of each side of the window to the respective
6163 * side of the region boundary. Positive values indicate the side of
6164 * the window has passed beyond the region boundary.
6166 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) + BORDER(win
) - MAX_X(r
) : bw
;
6167 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) + BORDER(win
) : bw
;
6168 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) + BORDER(win
) - MAX_Y(r
) : bw
;
6169 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) + BORDER(win
) : bw
;
6171 DNPRINTF(SWM_D_MISC
, "region_containment: win %#x, rt: %d, lt: %d, "
6172 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
6173 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
6174 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
6177 * Disable containment if any of the flagged sides went beyond the
6178 * containment boundary, or if containment is disabled.
6180 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
6181 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
6182 /* Make sure window has at least 1 pixel in the region */
6183 g
.x
+= 1 - WIDTH(win
);
6184 g
.y
+= 1 - HEIGHT(win
);
6185 g
.w
+= 2 * WIDTH(win
) - 2;
6186 g
.h
+= 2 * HEIGHT(win
) - 2;
6189 constrain_window(win
, &g
, &opts
);
6192 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6194 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
6196 DNPRINTF(SWM_D_MISC
, "constrain_window: win %#x, (x,y) w x h: "
6197 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6198 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
6199 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
6200 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
6201 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
6202 YESNO(*opts
& SWM_CW_RESIZABLE
));
6204 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) + BORDER(win
) > b
->x
+ b
->w
) {
6205 if (*opts
& SWM_CW_RESIZABLE
)
6206 WIDTH(win
) = b
->x
+ b
->w
- X(win
) - BORDER(win
);
6208 X(win
) = b
->x
+ b
->w
- WIDTH(win
) - BORDER(win
);
6211 if ((*opts
& SWM_CW_LEFT
) && X(win
) + BORDER(win
) < b
->x
) {
6212 if (*opts
& SWM_CW_RESIZABLE
)
6213 WIDTH(win
) -= b
->x
- X(win
) - BORDER(win
);
6215 X(win
) = b
->x
- BORDER(win
);
6218 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) + BORDER(win
) > b
->y
+ b
->h
) {
6219 if (*opts
& SWM_CW_RESIZABLE
)
6220 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
) - BORDER(win
);
6222 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
) - BORDER(win
);
6225 if ((*opts
& SWM_CW_TOP
) && Y(win
) + BORDER(win
) < b
->y
) {
6226 if (*opts
& SWM_CW_RESIZABLE
)
6227 HEIGHT(win
) -= b
->y
- Y(win
) - BORDER(win
);
6229 Y(win
) = b
->y
- BORDER(win
);
6232 if (*opts
& SWM_CW_RESIZABLE
) {
6235 if (HEIGHT(win
) < 1)
6241 update_window_color(struct ws_win
*win
)
6245 if (WS_FOCUSED(win
->ws
) && win
->ws
->focus
== win
)
6246 pixel
= MAXIMIZED(win
) ?
6247 &win
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].pixel
:
6248 &win
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6250 pixel
= MAXIMIZED(win
) ?
6251 &win
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].pixel
:
6252 &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6254 xcb_change_window_attributes(conn
, win
->id
,
6255 XCB_CW_BORDER_PIXEL
, pixel
);
6259 update_window(struct ws_win
*win
)
6264 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
6265 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
6266 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6270 wc
[3] = HEIGHT(win
);
6271 wc
[4] = BORDER(win
);
6273 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x, (x,y) w x h: "
6274 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
6275 wc
[3], YESNO(win
->bordered
));
6277 xcb_configure_window(conn
, win
->id
, mask
, wc
);
6280 #define SWM_RESIZE_STEPS (50)
6283 resize(struct ws_win
*win
, union arg
*args
)
6285 xcb_timestamp_t timestamp
= 0;
6286 struct swm_region
*r
= NULL
;
6287 struct swm_geometry g
;
6289 int top
= 0, left
= 0;
6291 xcb_cursor_t cursor
;
6292 xcb_query_pointer_reply_t
*xpr
= NULL
;
6293 xcb_generic_event_t
*evt
;
6294 xcb_motion_notify_event_t
*mne
;
6301 if (FULLSCREEN(win
))
6304 /* In max_stack mode, should only resize transients. */
6305 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6308 DNPRINTF(SWM_D_EVENT
, "resize: win %#x, floating: %s, "
6309 "transient: %#x\n", win
->id
, YESNO(ABOVE(win
)),
6313 store_float_geom(win
);
6314 else if (!(TRANS(win
) || ABOVE(win
)))
6317 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6319 ewmh_update_wm_state(win
);
6325 /* It's possible for win to have been freed during focus_flush(). */
6326 if (validate_win(win
)) {
6327 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6332 case SWM_ARG_ID_WIDTHSHRINK
:
6333 WIDTH(win
) -= SWM_RESIZE_STEPS
;
6336 case SWM_ARG_ID_WIDTHGROW
:
6337 WIDTH(win
) += SWM_RESIZE_STEPS
;
6340 case SWM_ARG_ID_HEIGHTSHRINK
:
6341 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
6344 case SWM_ARG_ID_HEIGHTGROW
:
6345 HEIGHT(win
) += SWM_RESIZE_STEPS
;
6352 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6353 SWM_CW_HARDBOUNDARY
);
6355 store_float_geom(win
);
6359 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6360 SWM_CW_SOFTBOUNDARY
);
6363 /* get cursor offset from window root */
6364 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6371 if (xpr
->win_x
< WIDTH(win
) / 2)
6374 if (xpr
->win_y
< HEIGHT(win
) / 2)
6377 if (args
->id
== SWM_ARG_ID_CENTER
)
6378 cursor
= cursors
[XC_SIZING
].cid
;
6380 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
6381 XC_TOP_RIGHT_CORNER
].cid
;
6383 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
6384 XC_BOTTOM_RIGHT_CORNER
].cid
;
6386 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6387 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
6392 while (resizing
&& (evt
= xcb_wait_for_event(conn
))) {
6393 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6394 case XCB_BUTTON_RELEASE
:
6395 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
6398 case XCB_MOTION_NOTIFY
:
6399 mne
= (xcb_motion_notify_event_t
*)evt
;
6400 /* cursor offset/delta from start of the operation */
6401 dx
= mne
->root_x
- xpr
->root_x
;
6402 dy
= mne
->root_y
- xpr
->root_y
;
6408 if (args
->id
== SWM_ARG_ID_CENTER
) {
6409 if (g
.h
/ 2 + dy
< 1)
6413 HEIGHT(win
) = g
.h
+ 2 * dy
;
6421 HEIGHT(win
) = g
.h
+ dy
;
6428 if (args
->id
== SWM_ARG_ID_CENTER
) {
6429 if (g
.w
/ 2 + dx
< 1)
6433 WIDTH(win
) = g
.w
+ 2 * dx
;
6441 WIDTH(win
) = g
.w
+ dx
;
6444 /* not free, don't sync more than 120 times / second */
6445 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6446 timestamp
= mne
->time
;
6447 regionize(win
, mne
->root_x
, mne
->root_y
);
6448 region_containment(win
, r
, SWM_CW_ALLSIDES
|
6449 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
6450 SWM_CW_SOFTBOUNDARY
);
6457 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6458 ((xcb_key_press_event_t
*)evt
)->time
);
6464 /* It's possible for win to have been freed above. */
6465 if (validate_win(win
)) {
6466 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6474 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6475 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
6479 store_float_geom(win
);
6481 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6483 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
6487 resize_step(struct swm_region
*r
, union arg
*args
)
6489 struct ws_win
*win
= NULL
;
6491 if (r
&& r
->ws
&& r
->ws
->focus
)
6501 /* Try to set window region based on supplied coordinates or window center. */
6503 regionize(struct ws_win
*win
, int x
, int y
)
6505 struct swm_region
*r
= NULL
;
6507 r
= region_under(win
->s
, x
, y
);
6509 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
6510 Y(win
) + HEIGHT(win
) / 2);
6512 if (r
!= NULL
&& r
!= win
->ws
->r
) {
6513 if (clear_maximized(r
->ws
) > 0)
6516 win_to_ws(win
, r
->ws
->idx
, false);
6518 /* Set focus on new ws. */
6519 unfocus_win(r
->ws
->focus
);
6527 #define SWM_MOVE_STEPS (50)
6530 move(struct ws_win
*win
, union arg
*args
)
6532 struct swm_region
*r
;
6533 xcb_timestamp_t timestamp
= 0;
6535 xcb_query_pointer_reply_t
*qpr
= NULL
;
6536 xcb_generic_event_t
*evt
;
6537 xcb_motion_notify_event_t
*mne
;
6538 bool moving
, restack
= false;
6543 if ((r
= win
->ws
->r
) == NULL
)
6546 if (FULLSCREEN(win
))
6549 DNPRINTF(SWM_D_EVENT
, "move: win %#x, floating: %s, transient: "
6550 "%#x\n", win
->id
, YESNO(ABOVE(win
)), win
->transient
);
6552 /* in max_stack mode should only move transients */
6553 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6556 if (!(ABOVE(win
) || TRANS(win
)) || MAXIMIZED(win
)) {
6557 store_float_geom(win
);
6561 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6563 ewmh_update_wm_state(win
);
6570 /* It's possible for win to have been freed during focus_flush(). */
6571 if (validate_win(win
)) {
6572 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
6578 case SWM_ARG_ID_MOVELEFT
:
6579 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
6582 case SWM_ARG_ID_MOVERIGHT
:
6583 X(win
) += (SWM_MOVE_STEPS
- border_width
);
6586 case SWM_ARG_ID_MOVEUP
:
6587 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
6590 case SWM_ARG_ID_MOVEDOWN
:
6591 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
6598 regionize(win
, -1, -1);
6599 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
6601 store_float_geom(win
);
6605 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6606 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
6607 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
6609 /* get cursor offset from window root */
6610 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6613 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6617 regionize(win
, qpr
->root_x
, qpr
->root_y
);
6618 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
6619 SWM_CW_SOFTBOUNDARY
);
6623 while (moving
&& (evt
= xcb_wait_for_event(conn
))) {
6624 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6625 case XCB_BUTTON_RELEASE
:
6626 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
6629 case XCB_MOTION_NOTIFY
:
6630 mne
= (xcb_motion_notify_event_t
*)evt
;
6631 DNPRINTF(SWM_D_EVENT
, "motion: root: %#x\n", mne
->root
);
6632 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
6633 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
6635 /* not free, don't sync more than 120 times / second */
6636 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6637 timestamp
= mne
->time
;
6638 regionize(win
, mne
->root_x
, mne
->root_y
);
6639 region_containment(win
, win
->ws
->r
,
6640 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
6647 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6648 ((xcb_key_press_event_t
*)evt
)->time
);
6654 /* It's possible for win to have been freed above. */
6655 if (validate_win(win
)) {
6656 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
6664 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
6665 SWM_CW_SOFTBOUNDARY
);
6669 store_float_geom(win
);
6671 /* New region set to fullscreen layout. */
6672 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
]) {
6679 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6680 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
6684 move_step(struct swm_region
*r
, union arg
*args
)
6686 struct ws_win
*win
= NULL
;
6688 if (r
&& r
->ws
&& r
->ws
->focus
)
6693 if (!TRANS(win
) && !ABOVE(win
))
6701 /* key definitions */
6703 char name
[SWM_FUNCNAME_LEN
];
6704 void (*func
)(struct swm_region
*r
, union arg
*);
6706 } keyfuncs
[KF_INVALID
+ 1] = {
6707 /* name function argument */
6708 { "bar_toggle", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
6709 { "bar_toggle_ws", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
6710 { "button2", pressbutton
, {2} },
6711 { "cycle_layout", cycle_layout
, {0} },
6712 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
6713 { "float_toggle", floating_toggle
,{0} },
6714 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
6715 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
6716 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
6717 { "focus_urgent", focus
, {.id
= SWM_ARG_ID_FOCUSURGENT
} },
6718 { "maximize_toggle", maximize_toggle
,{0} },
6719 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
6720 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
6721 { "iconify", iconify
, {0} },
6722 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
6723 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
6724 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
6725 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
6726 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
6727 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
6728 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
6729 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
6730 { "mvrg_1", send_to_rg
, {.id
= 0} },
6731 { "mvrg_2", send_to_rg
, {.id
= 1} },
6732 { "mvrg_3", send_to_rg
, {.id
= 2} },
6733 { "mvrg_4", send_to_rg
, {.id
= 3} },
6734 { "mvrg_5", send_to_rg
, {.id
= 4} },
6735 { "mvrg_6", send_to_rg
, {.id
= 5} },
6736 { "mvrg_7", send_to_rg
, {.id
= 6} },
6737 { "mvrg_8", send_to_rg
, {.id
= 7} },
6738 { "mvrg_9", send_to_rg
, {.id
= 8} },
6739 { "mvws_1", send_to_ws
, {.id
= 0} },
6740 { "mvws_2", send_to_ws
, {.id
= 1} },
6741 { "mvws_3", send_to_ws
, {.id
= 2} },
6742 { "mvws_4", send_to_ws
, {.id
= 3} },
6743 { "mvws_5", send_to_ws
, {.id
= 4} },
6744 { "mvws_6", send_to_ws
, {.id
= 5} },
6745 { "mvws_7", send_to_ws
, {.id
= 6} },
6746 { "mvws_8", send_to_ws
, {.id
= 7} },
6747 { "mvws_9", send_to_ws
, {.id
= 8} },
6748 { "mvws_10", send_to_ws
, {.id
= 9} },
6749 { "mvws_11", send_to_ws
, {.id
= 10} },
6750 { "mvws_12", send_to_ws
, {.id
= 11} },
6751 { "mvws_13", send_to_ws
, {.id
= 12} },
6752 { "mvws_14", send_to_ws
, {.id
= 13} },
6753 { "mvws_15", send_to_ws
, {.id
= 14} },
6754 { "mvws_16", send_to_ws
, {.id
= 15} },
6755 { "mvws_17", send_to_ws
, {.id
= 16} },
6756 { "mvws_18", send_to_ws
, {.id
= 17} },
6757 { "mvws_19", send_to_ws
, {.id
= 18} },
6758 { "mvws_20", send_to_ws
, {.id
= 19} },
6759 { "mvws_21", send_to_ws
, {.id
= 20} },
6760 { "mvws_22", send_to_ws
, {.id
= 21} },
6761 { "name_workspace", name_workspace
, {0} },
6762 { "quit", quit
, {0} },
6763 { "raise_toggle", raise_toggle
, {0} },
6764 { "restart", restart
, {0} },
6765 { "rg_1", focusrg
, {.id
= 0} },
6766 { "rg_2", focusrg
, {.id
= 1} },
6767 { "rg_3", focusrg
, {.id
= 2} },
6768 { "rg_4", focusrg
, {.id
= 3} },
6769 { "rg_5", focusrg
, {.id
= 4} },
6770 { "rg_6", focusrg
, {.id
= 5} },
6771 { "rg_7", focusrg
, {.id
= 6} },
6772 { "rg_8", focusrg
, {.id
= 7} },
6773 { "rg_9", focusrg
, {.id
= 8} },
6774 { "rg_move_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_MOVE_UP
} },
6775 { "rg_move_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_MOVE_DOWN
} },
6776 { "rg_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
6777 { "rg_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
6778 { "screen_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
6779 { "screen_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
6780 { "search_win", search_win
, {0} },
6781 { "search_workspace", search_workspace
, {0} },
6782 { "spawn_custom", NULL
, {0} },
6783 { "stack_balance", stack_config
, {.id
= SWM_ARG_ID_STACKBALANCE
} },
6784 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
6785 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
6786 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
6787 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
6788 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
6789 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
6790 { "uniconify", uniconify
, {0} },
6791 { "version", version
, {0} },
6792 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
6793 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
6794 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
6795 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
6796 { "ws_1", switchws
, {.id
= 0} },
6797 { "ws_2", switchws
, {.id
= 1} },
6798 { "ws_3", switchws
, {.id
= 2} },
6799 { "ws_4", switchws
, {.id
= 3} },
6800 { "ws_5", switchws
, {.id
= 4} },
6801 { "ws_6", switchws
, {.id
= 5} },
6802 { "ws_7", switchws
, {.id
= 6} },
6803 { "ws_8", switchws
, {.id
= 7} },
6804 { "ws_9", switchws
, {.id
= 8} },
6805 { "ws_10", switchws
, {.id
= 9} },
6806 { "ws_11", switchws
, {.id
= 10} },
6807 { "ws_12", switchws
, {.id
= 11} },
6808 { "ws_13", switchws
, {.id
= 12} },
6809 { "ws_14", switchws
, {.id
= 13} },
6810 { "ws_15", switchws
, {.id
= 14} },
6811 { "ws_16", switchws
, {.id
= 15} },
6812 { "ws_17", switchws
, {.id
= 16} },
6813 { "ws_18", switchws
, {.id
= 17} },
6814 { "ws_19", switchws
, {.id
= 18} },
6815 { "ws_20", switchws
, {.id
= 19} },
6816 { "ws_21", switchws
, {.id
= 20} },
6817 { "ws_22", switchws
, {.id
= 21} },
6818 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
6819 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
6820 { "ws_next_move", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
6821 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
6822 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
6823 { "ws_prev_move", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
6824 { "ws_prior", priorws
, {0} },
6825 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
6826 { "invalid key func", NULL
, {0} },
6830 key_cmp(struct key
*kp1
, struct key
*kp2
)
6832 if (kp1
->keysym
< kp2
->keysym
)
6834 if (kp1
->keysym
> kp2
->keysym
)
6837 if (kp1
->mod
< kp2
->mod
)
6839 if (kp1
->mod
> kp2
->mod
)
6846 enum { client_click
, root_click
};
6848 unsigned int action
;
6850 unsigned int button
;
6851 void (*func
)(struct ws_win
*, union arg
*);
6854 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6855 /* action key mouse button func args */
6856 { client_click
, MODKEY
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
6857 { client_click
, MODKEY_SHIFT
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
6858 { client_click
, MODKEY
, XCB_BUTTON_INDEX_1
, move
, {0} },
6863 update_modkey(unsigned int mod
)
6869 RB_FOREACH(kp
, key_tree
, &keys
)
6870 if (kp
->mod
& XCB_MOD_MASK_SHIFT
)
6871 kp
->mod
= mod
| XCB_MOD_MASK_SHIFT
;
6875 for (i
= 0; i
< LENGTH(buttons
); i
++)
6876 if (buttons
[i
].mask
& XCB_MOD_MASK_SHIFT
)
6877 buttons
[i
].mask
= mod
| XCB_MOD_MASK_SHIFT
;
6879 buttons
[i
].mask
= mod
;
6883 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
6886 struct spawn_prog
*prog
= NULL
;
6888 char *ap
, **real_args
;
6890 /* suppress unused warning since var is needed */
6893 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
6896 TAILQ_FOREACH(prog
, &spawns
, entry
) {
6897 if (strcasecmp(spawn_name
, prog
->name
) == 0)
6901 warnx("spawn_custom: program %s not found", spawn_name
);
6905 /* make room for expanded args */
6906 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
6907 err(1, "spawn_custom: calloc real_args");
6909 /* expand spawn_args into real_args */
6910 for (i
= c
= 0; i
< prog
->argc
; i
++) {
6912 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
6913 if (strcasecmp(ap
, "$bar_border") == 0) {
6915 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
6917 err(1, "spawn_custom border color");
6918 } else if (strcasecmp(ap
, "$bar_color") == 0) {
6920 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
6922 err(1, "spawn_custom bar color");
6923 } else if (strcasecmp(ap
, "$bar_font") == 0) {
6924 if ((real_args
[c
] = strdup(bar_fonts
))
6926 err(1, "spawn_custom bar fonts");
6927 } else if (strcasecmp(ap
, "$bar_font_color") == 0) {
6929 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
6931 err(1, "spawn_custom color font");
6932 } else if (strcasecmp(ap
, "$color_focus") == 0) {
6934 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
6936 err(1, "spawn_custom color focus");
6937 } else if (strcasecmp(ap
, "$color_focus_maximized") == 0) {
6939 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].name
))
6941 err(1, "spawn_custom color focus maximized");
6942 } else if (strcasecmp(ap
, "$color_unfocus") == 0) {
6944 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
6946 err(1, "spawn_custom color unfocus");
6947 } else if (strcasecmp(ap
, "$color_unfocus_maximized") == 0) {
6949 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].name
))
6951 err(1, "spawn_custom color unfocus maximized");
6952 } else if (strcasecmp(ap
, "$region_index") == 0) {
6953 if (asprintf(&real_args
[c
], "%d",
6954 get_region_index(r
) + 1) < 1)
6955 err(1, "spawn_custom region index");
6956 } else if (strcasecmp(ap
, "$workspace_index") == 0) {
6957 if (asprintf(&real_args
[c
], "%d", r
->ws
->idx
+ 1) < 1)
6958 err(1, "spawn_custom workspace index");
6959 } else if (strcasecmp(ap
, "$dmenu_bottom") == 0) {
6962 if ((real_args
[c
] = strdup("-b")) == NULL
)
6963 err(1, "spawn_custom workspace index");
6965 /* no match --> copy as is */
6966 if ((real_args
[c
] = strdup(ap
)) == NULL
)
6967 err(1, "spawn_custom strdup(ap)");
6969 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
6975 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
6976 for (i
= 0; i
< c
; ++i
)
6977 DPRINTF("\"%s\" ", real_args
[i
]);
6980 *ret_args
= real_args
;
6985 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
6991 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
6995 spawn(r
->ws
->idx
, &a
, true);
6997 for (i
= 0; i
< spawn_argc
; i
++)
7003 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7010 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7014 if (pipe(select_list_pipe
) == -1)
7015 err(1, "pipe error");
7016 if (pipe(select_resp_pipe
) == -1)
7017 err(1, "pipe error");
7019 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
7020 err(1, "could not disable SIGPIPE");
7021 switch (*pid
= fork()) {
7023 err(1, "cannot fork");
7026 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
7028 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
7030 close(select_list_pipe
[1]);
7031 close(select_resp_pipe
[0]);
7032 spawn(r
->ws
->idx
, &a
, false);
7034 default: /* parent */
7035 close(select_list_pipe
[0]);
7036 close(select_resp_pipe
[1]);
7040 for (i
= 0; i
< spawn_argc
; i
++)
7045 /* Argument tokenizer. */
7048 char *arg
, *cp
, *next
;
7049 bool single_quoted
= false, double_quoted
= false;
7054 /* Eat and move characters until end of argument is found. */
7055 for (arg
= next
= cp
= *sp
; *cp
!= '\0'; ++cp
) {
7056 if (!double_quoted
&& *cp
== '\'') {
7057 /* Eat single-quote. */
7058 single_quoted
= !single_quoted
;
7059 } else if (!single_quoted
&& *cp
== '"') {
7060 /* Eat double-quote. */
7061 double_quoted
= !double_quoted
;
7062 } else if (!single_quoted
&& *cp
== '\\' && *(cp
+ 1) == '"') {
7063 /* Eat backslash; copy escaped character to arg. */
7065 } else if (!single_quoted
&& !double_quoted
&& *cp
== '\\' &&
7066 (*(cp
+ 1) == '\'' || *(cp
+ 1) == ' ')) {
7067 /* Eat backslash; move escaped character. */
7069 } else if (!single_quoted
&& !double_quoted
&&
7070 (*cp
== ' ' || *cp
== '\t')) {
7071 /* Terminate argument. */
7073 /* Point sp to beginning of next argument. */
7077 /* Move regular character. */
7082 /* Terminate argument if end of string. */
7092 spawn_insert(const char *name
, const char *args
, int flags
)
7094 struct spawn_prog
*sp
;
7095 char *arg
, *cp
, *ptr
;
7097 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s[%s]\n", name
, args
);
7099 if (args
== NULL
|| *args
== '\0')
7102 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
7103 err(1, "spawn_insert: calloc");
7104 if ((sp
->name
= strdup(name
)) == NULL
)
7105 err(1, "spawn_insert: strdup");
7107 /* Convert the arguments to an argument list. */
7108 if ((ptr
= cp
= strdup(args
)) == NULL
)
7109 err(1, "spawn_insert: strdup");
7110 while ((arg
= argsep(&ptr
)) != NULL
) {
7111 /* Null argument; skip it. */
7116 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
7117 sizeof *sp
->argv
)) == NULL
)
7118 err(1, "spawn_insert: realloc");
7119 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
7120 err(1, "spawn_insert: strdup");
7126 DNPRINTF(SWM_D_SPAWN
, "arg %d: [%s]\n", sp
->argc
, sp
->argv
[sp
->argc
-1]);
7127 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
7128 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
7132 spawn_remove(struct spawn_prog
*sp
)
7136 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
7138 TAILQ_REMOVE(&spawns
, sp
, entry
);
7139 for (i
= 0; i
< sp
->argc
; i
++)
7145 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
7151 struct spawn_prog
*sp
;
7153 while ((sp
= TAILQ_FIRST(&spawns
)) != NULL
) {
7159 spawn_find(const char *name
)
7161 struct spawn_prog
*sp
;
7163 TAILQ_FOREACH(sp
, &spawns
, entry
)
7164 if (strcasecmp(sp
->name
, name
) == 0)
7171 setspawn(const char *name
, const char *args
, int flags
)
7173 struct spawn_prog
*sp
;
7175 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
7180 /* Remove any old spawn under the same name. */
7181 if ((sp
= spawn_find(name
)) != NULL
)
7185 spawn_insert(name
, args
, flags
);
7187 warnx("error: setspawn: cannot find program: %s", name
);
7189 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
7193 setconfspawn(const char *selector
, const char *value
, int flags
)
7197 if (selector
== NULL
|| strlen(selector
) == 0)
7200 args
= expand_tilde(value
);
7202 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
7204 setspawn(selector
, args
, flags
);
7207 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
7212 validate_spawns(void)
7214 struct spawn_prog
*sp
;
7215 char which
[PATH_MAX
];
7220 RB_FOREACH(kp
, key_tree
, &keys
) {
7221 if (kp
->funcid
!= KF_SPAWN_CUSTOM
)
7225 sp
= spawn_find(kp
->spawn_name
);
7226 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
7229 /* verify we have the goods */
7230 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
7231 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
7233 for (i
= strlen("which "); i
< strlen(which
); i
++)
7234 if (which
[i
] == ' ') {
7238 if (system(which
) != 0)
7239 add_startup_exception("could not find %s",
7240 &which
[strlen("which ")]);
7247 setconfspawn("lock", "xlock", 0);
7249 setconfspawn("term", "xterm", 0);
7250 setconfspawn("spawn_term", "xterm", 0);
7252 setconfspawn("menu", "dmenu_run"
7256 " -nf $bar_font_color"
7258 " -sf $bar_color", 0);
7260 setconfspawn("search", "dmenu"
7265 " -nf $bar_font_color"
7267 " -sf $bar_color", 0);
7269 setconfspawn("name_workspace", "dmenu"
7274 " -nf $bar_font_color"
7276 " -sf $bar_color", 0);
7278 /* These are not verified for existence, even with a binding set. */
7279 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
7280 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
7281 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
7285 #define SWM_MODNAME_SIZE 32
7286 #define SWM_KEY_WS "\n+ \t"
7288 parsekeys(const char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
7290 char *str
, *cp
, *name
;
7293 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
7294 if (mod
== NULL
|| ks
== NULL
) {
7295 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
7298 if (keystr
== NULL
|| strlen(keystr
) == 0) {
7299 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
7303 if ((cp
= str
= strdup(keystr
)) == NULL
)
7304 err(1, "parsekeys: strdup");
7306 *ks
= XCB_NO_SYMBOL
;
7308 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
7309 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
7311 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
7312 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
7314 else if (strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
) == 0)
7315 *mod
|= XCB_MOD_MASK_1
;
7316 else if (strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
) == 0)
7317 *mod
+= XCB_MOD_MASK_2
;
7318 else if (strncmp(name
, "Mod3", SWM_MODNAME_SIZE
) == 0)
7319 *mod
|= XCB_MOD_MASK_3
;
7320 else if (strncmp(name
, "Mod4", SWM_MODNAME_SIZE
) == 0)
7321 *mod
|= XCB_MOD_MASK_4
;
7322 else if (strncmp(name
, "Mod5", SWM_MODNAME_SIZE
) == 0)
7323 *mod
|= XCB_MOD_MASK_5
;
7324 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
7325 *mod
|= XCB_MOD_MASK_SHIFT
;
7326 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
7327 *mod
|= XCB_MOD_MASK_CONTROL
;
7329 *ks
= XStringToKeysym(name
);
7330 XConvertCase(*ks
, ks
, &uks
);
7331 if (ks
== XCB_NO_SYMBOL
) {
7333 "parsekeys: invalid key %s\n",
7342 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
7347 strdupsafe(const char *str
)
7352 return (strdup(str
));
7356 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
7357 const char *spawn_name
)
7361 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
7362 keyfuncs
[kfid
].name
, spawn_name
);
7364 if ((kp
= malloc(sizeof *kp
)) == NULL
)
7365 err(1, "key_insert: malloc");
7370 kp
->spawn_name
= strdupsafe(spawn_name
);
7371 RB_INSERT(key_tree
, &keys
, kp
);
7373 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
7377 key_lookup(unsigned int mod
, KeySym ks
)
7384 return (RB_FIND(key_tree
, &keys
, &kp
));
7388 key_remove(struct key
*kp
)
7390 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
7392 RB_REMOVE(key_tree
, &keys
, kp
);
7393 free(kp
->spawn_name
);
7396 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
7400 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
7401 const char *spawn_name
)
7403 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
7407 key_insert(mod
, ks
, kfid
, spawn_name
);
7409 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
7413 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
7414 const char *spawn_name
)
7418 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
7419 keyfuncs
[kfid
].name
, spawn_name
);
7421 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
7422 if (kfid
== KF_INVALID
)
7425 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
7426 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
7429 if (kfid
== KF_INVALID
) {
7430 warnx("bind: Key combination already unbound.");
7431 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
7435 key_insert(mod
, ks
, kfid
, spawn_name
);
7436 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
7440 setconfbinding(const char *selector
, const char *value
, int flags
)
7442 enum keyfuncid kfid
;
7445 struct spawn_prog
*sp
;
7447 /* suppress unused warning since var is needed */
7450 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter selector: [%s], "
7451 "value: [%s]\n", selector
, value
);
7452 if (selector
== NULL
|| strlen(selector
) == 0) {
7453 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
7454 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7456 setkeybinding(mod
, ks
, kfid
, NULL
);
7461 /* search by key function name */
7462 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
7463 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
7464 SWM_FUNCNAME_LEN
) == 0) {
7465 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
7466 "keyfunc\n", selector
);
7467 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7468 setkeybinding(mod
, ks
, kfid
, NULL
);
7474 /* search by custom spawn name */
7475 if ((sp
= spawn_find(selector
)) != NULL
) {
7476 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
7477 "spawn\n", selector
);
7478 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7479 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
7485 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
7492 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
7493 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
7494 setkeybinding(MODKEY_SHIFT
, XK_b
, KF_BAR_TOGGLE_WS
,NULL
);
7495 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
7496 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
7497 setkeybinding(MODKEY_SHIFT
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
7498 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
7499 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
7500 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
7501 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
7502 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
7503 setkeybinding(MODKEY_SHIFT
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
7504 setkeybinding(MODKEY
, XK_u
, KF_FOCUS_URGENT
,NULL
);
7505 setkeybinding(MODKEY
, XK_e
, KF_MAXIMIZE_TOGGLE
,NULL
);
7506 setkeybinding(MODKEY_SHIFT
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
7507 setkeybinding(MODKEY_SHIFT
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
7508 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
7509 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
7510 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
7511 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
7512 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
7513 setkeybinding(MODKEY_SHIFT
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
7514 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
7515 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
7516 setkeybinding(MODKEY_SHIFT
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
7517 setkeybinding(MODKEY_SHIFT
, XK_KP_End
, KF_MVRG_1
, NULL
);
7518 setkeybinding(MODKEY_SHIFT
, XK_KP_Down
, KF_MVRG_2
, NULL
);
7519 setkeybinding(MODKEY_SHIFT
, XK_KP_Next
, KF_MVRG_3
, NULL
);
7520 setkeybinding(MODKEY_SHIFT
, XK_KP_Left
, KF_MVRG_4
, NULL
);
7521 setkeybinding(MODKEY_SHIFT
, XK_KP_Begin
, KF_MVRG_5
, NULL
);
7522 setkeybinding(MODKEY_SHIFT
, XK_KP_Right
, KF_MVRG_6
, NULL
);
7523 setkeybinding(MODKEY_SHIFT
, XK_KP_Home
, KF_MVRG_7
, NULL
);
7524 setkeybinding(MODKEY_SHIFT
, XK_KP_Up
, KF_MVRG_8
, NULL
);
7525 setkeybinding(MODKEY_SHIFT
, XK_KP_Prior
, KF_MVRG_9
, NULL
);
7526 setkeybinding(MODKEY_SHIFT
, XK_1
, KF_MVWS_1
, NULL
);
7527 setkeybinding(MODKEY_SHIFT
, XK_2
, KF_MVWS_2
, NULL
);
7528 setkeybinding(MODKEY_SHIFT
, XK_3
, KF_MVWS_3
, NULL
);
7529 setkeybinding(MODKEY_SHIFT
, XK_4
, KF_MVWS_4
, NULL
);
7530 setkeybinding(MODKEY_SHIFT
, XK_5
, KF_MVWS_5
, NULL
);
7531 setkeybinding(MODKEY_SHIFT
, XK_6
, KF_MVWS_6
, NULL
);
7532 setkeybinding(MODKEY_SHIFT
, XK_7
, KF_MVWS_7
, NULL
);
7533 setkeybinding(MODKEY_SHIFT
, XK_8
, KF_MVWS_8
, NULL
);
7534 setkeybinding(MODKEY_SHIFT
, XK_9
, KF_MVWS_9
, NULL
);
7535 setkeybinding(MODKEY_SHIFT
, XK_0
, KF_MVWS_10
, NULL
);
7536 setkeybinding(MODKEY_SHIFT
, XK_F1
, KF_MVWS_11
, NULL
);
7537 setkeybinding(MODKEY_SHIFT
, XK_F2
, KF_MVWS_12
, NULL
);
7538 setkeybinding(MODKEY_SHIFT
, XK_F3
, KF_MVWS_13
, NULL
);
7539 setkeybinding(MODKEY_SHIFT
, XK_F4
, KF_MVWS_14
, NULL
);
7540 setkeybinding(MODKEY_SHIFT
, XK_F5
, KF_MVWS_15
, NULL
);
7541 setkeybinding(MODKEY_SHIFT
, XK_F6
, KF_MVWS_16
, NULL
);
7542 setkeybinding(MODKEY_SHIFT
, XK_F7
, KF_MVWS_17
, NULL
);
7543 setkeybinding(MODKEY_SHIFT
, XK_F8
, KF_MVWS_18
, NULL
);
7544 setkeybinding(MODKEY_SHIFT
, XK_F9
, KF_MVWS_19
, NULL
);
7545 setkeybinding(MODKEY_SHIFT
, XK_F10
, KF_MVWS_20
, NULL
);
7546 setkeybinding(MODKEY_SHIFT
, XK_F11
, KF_MVWS_21
, NULL
);
7547 setkeybinding(MODKEY_SHIFT
, XK_F12
, KF_MVWS_22
, NULL
);
7548 setkeybinding(MODKEY_SHIFT
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
7549 setkeybinding(MODKEY_SHIFT
, XK_q
, KF_QUIT
, NULL
);
7550 setkeybinding(MODKEY_SHIFT
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
7551 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
7552 setkeybinding(MODKEY
, XK_KP_End
, KF_RG_1
, NULL
);
7553 setkeybinding(MODKEY
, XK_KP_Down
, KF_RG_2
, NULL
);
7554 setkeybinding(MODKEY
, XK_KP_Next
, KF_RG_3
, NULL
);
7555 setkeybinding(MODKEY
, XK_KP_Left
, KF_RG_4
, NULL
);
7556 setkeybinding(MODKEY
, XK_KP_Begin
, KF_RG_5
, NULL
);
7557 setkeybinding(MODKEY
, XK_KP_Right
, KF_RG_6
, NULL
);
7558 setkeybinding(MODKEY
, XK_KP_Home
, KF_RG_7
, NULL
);
7559 setkeybinding(MODKEY
, XK_KP_Up
, KF_RG_8
, NULL
);
7560 setkeybinding(MODKEY
, XK_KP_Prior
, KF_RG_9
, NULL
);
7561 setkeybinding(MODKEY_SHIFT
, XK_Right
, KF_RG_NEXT
, NULL
);
7562 setkeybinding(MODKEY_SHIFT
, XK_Left
, KF_RG_PREV
, NULL
);
7563 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
7564 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
7565 setkeybinding(MODKEY_SHIFT
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
7566 setkeybinding(MODKEY_SHIFT
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
7567 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
7568 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
7569 setkeybinding(MODKEY_SHIFT
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
7570 setkeybinding(MODKEY_SHIFT
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
7571 setkeybinding(MODKEY_SHIFT
, XK_comma
, KF_STACK_INC
, NULL
);
7572 setkeybinding(MODKEY_SHIFT
, XK_period
, KF_STACK_DEC
, NULL
);
7573 setkeybinding(MODKEY_SHIFT
, XK_space
, KF_STACK_RESET
, NULL
);
7574 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
7575 setkeybinding(MODKEY_SHIFT
, XK_j
, KF_SWAP_NEXT
, NULL
);
7576 setkeybinding(MODKEY_SHIFT
, XK_k
, KF_SWAP_PREV
, NULL
);
7577 setkeybinding(MODKEY_SHIFT
, XK_w
, KF_UNICONIFY
, NULL
);
7578 setkeybinding(MODKEY_SHIFT
, XK_v
, KF_VERSION
, NULL
);
7579 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
7580 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
7581 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
7582 setkeybinding(MODKEY_SHIFT
, XK_x
, KF_WIND_KILL
, NULL
);
7583 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
7584 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
7585 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
7586 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
7587 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
7588 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
7589 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
7590 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
7591 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
7592 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
7593 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
7594 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
7595 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
7596 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
7597 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
7598 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
7599 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
7600 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
7601 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
7602 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
7603 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
7604 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
7605 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
7606 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
7607 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
7608 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
7609 setkeybinding(MODKEY_SHIFT
, XK_Up
, KF_WS_NEXT_MOVE
,NULL
);
7610 setkeybinding(MODKEY_SHIFT
, XK_Down
, KF_WS_PREV_MOVE
,NULL
);
7611 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
7613 setkeybinding(MODKEY_SHIFT
, XK_d
, KF_DUMPWINS
, NULL
);
7623 while (RB_EMPTY(&keys
) == 0) {
7624 kp
= RB_ROOT(&keys
);
7630 setkeymapping(const char *selector
, const char *value
, int flags
)
7632 char *keymapping_file
;
7634 /* suppress unused warnings since vars are needed */
7638 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
7640 keymapping_file
= expand_tilde(value
);
7643 /* load new key bindings; if it fails, revert to default bindings */
7644 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
7649 free(keymapping_file
);
7651 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
7656 updatenumlockmask(void)
7659 xcb_get_modifier_mapping_reply_t
*modmap_r
;
7660 xcb_keycode_t
*modmap
, kc
, *keycode
;
7664 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
7665 xcb_get_modifier_mapping(conn
),
7668 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
7669 for (i
= 0; i
< 8; i
++) {
7670 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
7671 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
7673 keycode
= xcb_key_symbols_get_keycode(syms
,
7677 numlockmask
= (1 << i
);
7684 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
7691 int num_screens
, k
, j
;
7692 unsigned int modifiers
[4];
7693 xcb_keycode_t
*code
;
7695 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
7696 updatenumlockmask();
7699 modifiers
[1] = numlockmask
;
7700 modifiers
[2] = XCB_MOD_MASK_LOCK
;
7701 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
7703 num_screens
= get_screen_count();
7704 for (k
= 0; k
< num_screens
; k
++) {
7705 if (TAILQ_EMPTY(&screens
[k
].rl
))
7707 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
7709 RB_FOREACH(kp
, key_tree
, &keys
) {
7710 /* Skip unused ws binds. */
7711 if ((int)kp
->funcid
> KF_WS_1
+ workspace_limit
- 1 &&
7712 kp
->funcid
<= KF_WS_22
)
7715 /* Skip unused mvws binds. */
7716 if ((int)kp
->funcid
> KF_MVWS_1
+ workspace_limit
- 1 &&
7717 kp
->funcid
<= KF_MVWS_22
)
7720 if ((code
= xcb_key_symbols_get_keycode(syms
,
7722 for (j
= 0; j
< LENGTH(modifiers
); j
++)
7723 xcb_grab_key(conn
, 1,
7725 kp
->mod
| modifiers
[j
],
7726 *code
, XCB_GRAB_MODE_SYNC
,
7727 XCB_GRAB_MODE_SYNC
);
7735 grabbuttons(struct ws_win
*win
)
7737 unsigned int modifiers
[4];
7740 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: win %#x\n", win
->id
);
7741 updatenumlockmask();
7744 modifiers
[1] = numlockmask
;
7745 modifiers
[2] = XCB_MOD_MASK_LOCK
;
7746 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
7748 for (i
= 0; i
< LENGTH(buttons
); i
++)
7749 if (buttons
[i
].action
== client_click
)
7750 for (j
= 0; j
< LENGTH(modifiers
); ++j
)
7751 xcb_grab_button(conn
, 0, win
->id
, BUTTONMASK
,
7752 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
,
7753 XCB_WINDOW_NONE
, XCB_CURSOR_NONE
,
7754 buttons
[i
].button
, buttons
[i
].mask
|
7758 const char *quirkname
[] = {
7759 "NONE", /* config string for "no value" */
7767 "FOCUSONMAP_SINGLE",
7775 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
7776 #define SWM_Q_DELIM "\n|+ \t"
7778 parsequirks(const char *qstr
, uint32_t *quirk
, int *ws
)
7780 char *str
, *cp
, *name
;
7783 if (quirk
== NULL
|| qstr
== NULL
)
7786 if ((str
= strdup(qstr
)) == NULL
)
7787 err(1, "parsequirks: strdup");
7791 while ((name
= strsep(&cp
, SWM_Q_DELIM
)) != NULL
) {
7793 cp
+= (long)strspn(cp
, SWM_Q_DELIM
);
7795 if (sscanf(name
, "WS[%d]", ws
) == 1) {
7801 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
7802 if (strncasecmp(name
, quirkname
[i
],
7803 SWM_QUIRK_LEN
) == 0) {
7804 DNPRINTF(SWM_D_QUIRK
,
7805 "parsequirks: %s\n", name
);
7811 *quirk
|= 1 << (i
-1);
7815 if (i
>= LENGTH(quirkname
)) {
7816 DNPRINTF(SWM_D_QUIRK
,
7817 "parsequirks: invalid quirk [%s]\n", name
);
7828 quirk_insert(const char *class, const char *instance
, const char *name
,
7829 uint32_t quirk
, int ws
)
7833 bool failed
= false;
7835 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: class: %s, instance: %s, name: %s,"
7836 " value: %u, ws: %d\n", class, instance
, name
, quirk
, ws
);
7838 if ((qp
= malloc(sizeof *qp
)) == NULL
)
7839 err(1, "quirk_insert: malloc");
7841 if ((qp
->class = strdup(class)) == NULL
)
7842 err(1, "quirk_insert: strdup");
7843 if ((qp
->instance
= strdup(instance
)) == NULL
)
7844 err(1, "quirk_insert: strdup");
7845 if ((qp
->name
= strdup(name
)) == NULL
)
7846 err(1, "quirk_insert: strdup");
7848 if (asprintf(&str
, "^%s$", class) == -1)
7849 err(1, "quirk_insert: asprintf");
7850 if (regcomp(&qp
->regex_class
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7851 add_startup_exception("regex failed to compile quirk 'class' "
7852 "field: %s", class);
7855 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7858 if (asprintf(&str
, "^%s$", instance
) == -1)
7859 err(1, "quirk_insert: asprintf");
7860 if (regcomp(&qp
->regex_instance
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7861 add_startup_exception("regex failed to compile quirk 'instance'"
7862 " field: %s", instance
);
7865 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7868 if (asprintf(&str
, "^%s$", name
) == -1)
7869 err(1, "quirk_insert: asprintf");
7870 if (regcomp(&qp
->regex_name
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7871 add_startup_exception("regex failed to compile quirk 'name' "
7875 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7879 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: regex error; skipping.\n");
7884 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
7886 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
7890 quirk_remove(struct quirk
*qp
)
7892 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%u]\n", qp
->class,
7893 qp
->name
, qp
->quirk
);
7895 TAILQ_REMOVE(&quirks
, qp
, entry
);
7898 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
7902 quirk_free(struct quirk
*qp
)
7904 regfree(&qp
->regex_class
);
7905 regfree(&qp
->regex_instance
);
7906 regfree(&qp
->regex_name
);
7918 while ((qp
= TAILQ_FIRST(&quirks
)) != NULL
) {
7924 quirk_replace(struct quirk
*qp
, const char *class, const char *instance
,
7925 const char *name
, uint32_t quirk
, int ws
)
7927 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp
->class,
7928 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
7931 quirk_insert(class, instance
, name
, quirk
, ws
);
7933 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
7937 setquirk(const char *class, const char *instance
, const char *name
,
7938 uint32_t quirk
, int ws
)
7942 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
7943 instance
, name
, quirk
, ws
);
7945 /* Remove/replace existing quirk. */
7946 TAILQ_FOREACH(qp
, &quirks
, entry
) {
7947 if (strcmp(qp
->class, class) == 0 &&
7948 strcmp(qp
->instance
, instance
) == 0 &&
7949 strcmp(qp
->name
, name
) == 0) {
7950 if (quirk
== 0 && ws
== -1)
7953 quirk_replace(qp
, class, instance
, name
, quirk
,
7955 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
7960 /* Only insert if quirk is not NONE or forced ws is set. */
7961 if (quirk
|| ws
!= -1)
7962 quirk_insert(class, instance
, name
, quirk
, ws
);
7964 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
7967 /* Eat '\' in str used to escape square brackets and colon. */
7969 unescape_selector(char *str
)
7973 for (cp
= str
; *str
!= '\0'; ++str
, ++cp
) {
7974 if (*str
== '\\' && (*(str
+ 1) == ':' || *(str
+ 1) == ']' ||
7984 setconfquirk(const char *selector
, const char *value
, int flags
)
7986 char *str
, *cp
, *class;
7987 char *instance
= NULL
, *name
= NULL
;
7988 int retval
, count
= 0, ws
= -1;
7991 /* suppress unused warning since var is needed */
7994 if (selector
== NULL
|| strlen(selector
) == 0)
7997 if ((str
= strdup(selector
)) == NULL
)
7998 err(1, "setconfquirk: strdup");
8000 /* Find non-escaped colon. */
8007 for (++cp
; *cp
!= '\0'; ++cp
) {
8008 if (*cp
== ':' && *(cp
- 1) != '\\') {
8014 unescape_selector(class);
8016 instance
= class + strlen(class) + 1;
8017 unescape_selector(instance
);
8023 name
= instance
+ strlen(instance
) + 1;
8024 unescape_selector(name
);
8029 DNPRINTF(SWM_D_CONF
, "setconfquirk: class: %s, instance: %s, "
8030 "name: %s\n", class, instance
, name
);
8032 if ((retval
= parsequirks(value
, &qrks
, &ws
)) == 0)
8033 setquirk(class, instance
, name
, qrks
, ws
);
8042 setquirk("MPlayer", "xv", ".*",
8043 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
, -1);
8044 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
8046 setquirk("Firefox-bin", "firefox-bin", ".*",
8048 setquirk("Firefox", "Dialog", ".*",
8050 setquirk("Gimp", "gimp", ".*",
8051 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8052 setquirk("XTerm", "xterm", ".*",
8053 SWM_Q_XTERM_FONTADJ
, -1);
8054 setquirk("xine", "Xine Window", ".*",
8055 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8056 setquirk("Xitk", "Xitk Combo", ".*",
8057 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8058 setquirk("xine", "xine Panel", ".*",
8059 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8060 setquirk("Xitk", "Xine Window", ".*",
8061 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8062 setquirk("xine", "xine Video Fullscreen Window", ".*",
8063 SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
, -1);
8064 setquirk("pcb", "pcb", ".*",
8066 setquirk("SDL_App", "SDL_App", ".*",
8067 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
, -1);
8070 /* conf file stuff */
8071 #define SWM_CONF_FILE "spectrwm.conf"
8072 #define SWM_CONF_FILE_OLD "scrotwm.conf"
8076 SWM_S_BAR_AT_BOTTOM
,
8077 SWM_S_BAR_BORDER_WIDTH
,
8080 SWM_S_BAR_ENABLED_WS
,
8085 SWM_S_BOUNDARY_WIDTH
,
8086 SWM_S_CLOCK_ENABLED
,
8089 SWM_S_CYCLE_VISIBLE
,
8091 SWM_S_DISABLE_BORDER
,
8093 SWM_S_FOCUS_CLOSE_WRAP
,
8094 SWM_S_FOCUS_DEFAULT
,
8096 SWM_S_ICONIC_ENABLED
,
8097 SWM_S_JAVA_WORKAROUND
,
8098 SWM_S_MAXIMIZE_HIDE_BAR
,
8099 SWM_S_REGION_PADDING
,
8104 SWM_S_STACK_ENABLED
,
8107 SWM_S_URGENT_COLLAPSE
,
8108 SWM_S_URGENT_ENABLED
,
8109 SWM_S_VERBOSE_LAYOUT
,
8112 SWM_S_WINDOW_CLASS_ENABLED
,
8113 SWM_S_WINDOW_INSTANCE_ENABLED
,
8114 SWM_S_WINDOW_NAME_ENABLED
,
8115 SWM_S_WORKSPACE_CLAMP
,
8116 SWM_S_WORKSPACE_LIMIT
,
8117 SWM_S_WORKSPACE_NAME
,
8121 setconfvalue(const char *selector
, const char *value
, int flags
)
8123 struct workspace
*ws
;
8124 int i
, ws_id
, num_screens
;
8125 char *b
, *str
, s
[1024];
8128 case SWM_S_BAR_ACTION
:
8130 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
8131 err(1, "setconfvalue: bar_action");
8133 case SWM_S_BAR_AT_BOTTOM
:
8134 bar_at_bottom
= (atoi(value
) != 0);
8136 case SWM_S_BAR_BORDER_WIDTH
:
8137 bar_border_width
= atoi(value
);
8138 if (bar_border_width
< 0)
8139 bar_border_width
= 0;
8141 case SWM_S_BAR_DELAY
:
8142 /* No longer needed; leave to not break old conf files. */
8144 case SWM_S_BAR_ENABLED
:
8145 bar_enabled
= (atoi(value
) != 0);
8147 case SWM_S_BAR_ENABLED_WS
:
8148 ws_id
= atoi(selector
) - 1;
8149 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8150 errx(1, "setconfvalue: bar_enabled_ws: invalid "
8151 "workspace %d.", ws_id
+ 1);
8153 num_screens
= get_screen_count();
8154 for (i
= 0; i
< num_screens
; i
++) {
8155 ws
= (struct workspace
*)&screens
[i
].ws
;
8156 ws
[ws_id
].bar_enabled
= (atoi(value
) != 0);
8159 case SWM_S_BAR_FONT
:
8161 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
8162 err(1, "setconfvalue: asprintf: failed to allocate "
8163 "memory for bar_fonts.");
8166 /* If already in xft mode, then we are done. */
8167 if (!bar_font_legacy
)
8170 if ((str
= strdup(value
)) == NULL
)
8171 err(1, "setconfvalue: strdup");
8173 /* If there are any non-XLFD entries, switch to Xft mode. */
8174 while ((b
= strsep(&str
, ",")) != NULL
) {
8178 bar_font_legacy
= false;
8185 case SWM_S_BAR_FORMAT
:
8187 if ((bar_format
= strdup(value
)) == NULL
)
8188 err(1, "setconfvalue: bar_format");
8190 case SWM_S_BAR_JUSTIFY
:
8191 if (strcmp(value
, "left") == 0)
8192 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
8193 else if (strcmp(value
, "center") == 0)
8194 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
8195 else if (strcmp(value
, "right") == 0)
8196 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
8198 errx(1, "invalid bar_justify");
8200 case SWM_S_BORDER_WIDTH
:
8201 border_width
= atoi(value
);
8202 if (border_width
< 0)
8205 case SWM_S_BOUNDARY_WIDTH
:
8206 boundary_width
= atoi(value
);
8207 if (boundary_width
< 0)
8210 case SWM_S_CLOCK_ENABLED
:
8211 clock_enabled
= (atoi(value
) != 0);
8213 case SWM_S_CLOCK_FORMAT
:
8214 #ifndef SWM_DENY_CLOCK_FORMAT
8216 if ((clock_format
= strdup(value
)) == NULL
)
8217 err(1, "setconfvalue: clock_format");
8220 case SWM_S_CYCLE_EMPTY
:
8221 cycle_empty
= (atoi(value
) != 0);
8223 case SWM_S_CYCLE_VISIBLE
:
8224 cycle_visible
= (atoi(value
) != 0);
8226 case SWM_S_DIALOG_RATIO
:
8227 dialog_ratio
= atof(value
);
8228 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
8231 case SWM_S_DISABLE_BORDER
:
8232 disable_border
= (atoi(value
) != 0);
8234 case SWM_S_FOCUS_CLOSE
:
8235 if (strcmp(value
, "first") == 0)
8236 focus_close
= SWM_STACK_BOTTOM
;
8237 else if (strcmp(value
, "last") == 0)
8238 focus_close
= SWM_STACK_TOP
;
8239 else if (strcmp(value
, "next") == 0)
8240 focus_close
= SWM_STACK_ABOVE
;
8241 else if (strcmp(value
, "previous") == 0)
8242 focus_close
= SWM_STACK_BELOW
;
8244 errx(1, "focus_close");
8246 case SWM_S_FOCUS_CLOSE_WRAP
:
8247 focus_close_wrap
= (atoi(value
) != 0);
8249 case SWM_S_FOCUS_DEFAULT
:
8250 if (strcmp(value
, "last") == 0)
8251 focus_default
= SWM_STACK_TOP
;
8252 else if (strcmp(value
, "first") == 0)
8253 focus_default
= SWM_STACK_BOTTOM
;
8255 errx(1, "focus_default");
8257 case SWM_S_FOCUS_MODE
:
8258 if (strcmp(value
, "default") == 0)
8259 focus_mode
= SWM_FOCUS_DEFAULT
;
8260 else if (strcmp(value
, "follow") == 0 ||
8261 strcmp(value
, "follow_cursor") == 0)
8262 focus_mode
= SWM_FOCUS_FOLLOW
;
8263 else if (strcmp(value
, "manual") == 0)
8264 focus_mode
= SWM_FOCUS_MANUAL
;
8266 errx(1, "focus_mode");
8268 case SWM_S_ICONIC_ENABLED
:
8269 iconic_enabled
= (atoi(value
) != 0);
8271 case SWM_S_JAVA_WORKAROUND
:
8272 java_workaround
= (atoi(value
) != 0);
8274 case SWM_S_MAXIMIZE_HIDE_BAR
:
8275 maximize_hide_bar
= atoi(value
);
8277 case SWM_S_REGION_PADDING
:
8278 region_padding
= atoi(value
);
8279 if (region_padding
< 0)
8282 case SWM_S_SPAWN_ORDER
:
8283 if (strcmp(value
, "first") == 0)
8284 spawn_position
= SWM_STACK_BOTTOM
;
8285 else if (strcmp(value
, "last") == 0)
8286 spawn_position
= SWM_STACK_TOP
;
8287 else if (strcmp(value
, "next") == 0)
8288 spawn_position
= SWM_STACK_ABOVE
;
8289 else if (strcmp(value
, "previous") == 0)
8290 spawn_position
= SWM_STACK_BELOW
;
8292 errx(1, "spawn_position");
8294 case SWM_S_SPAWN_TERM
:
8295 setconfspawn("term", value
, 0);
8296 setconfspawn("spawn_term", value
, 0);
8299 /* No longer needed; leave to not break old conf files. */
8301 case SWM_S_SS_ENABLED
:
8302 /* No longer needed; leave to not break old conf files. */
8304 case SWM_S_STACK_ENABLED
:
8305 stack_enabled
= (atoi(value
) != 0);
8307 case SWM_S_TERM_WIDTH
:
8308 term_width
= atoi(value
);
8312 case SWM_S_TILE_GAP
:
8313 tile_gap
= atoi(value
);
8315 case SWM_S_URGENT_COLLAPSE
:
8316 urgent_collapse
= (atoi(value
) != 0);
8318 case SWM_S_URGENT_ENABLED
:
8319 urgent_enabled
= (atoi(value
) != 0);
8321 case SWM_S_VERBOSE_LAYOUT
:
8322 verbose_layout
= (atoi(value
) != 0);
8323 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
8325 layouts
[i
].l_string
= fancy_stacker
;
8327 layouts
[i
].l_string
= plain_stacker
;
8330 case SWM_S_WARP_FOCUS
:
8331 warp_focus
= (atoi(value
) != 0);
8333 case SWM_S_WARP_POINTER
:
8334 warp_pointer
= (atoi(value
) != 0);
8336 case SWM_S_WINDOW_CLASS_ENABLED
:
8337 window_class_enabled
= (atoi(value
) != 0);
8339 case SWM_S_WINDOW_INSTANCE_ENABLED
:
8340 window_instance_enabled
= (atoi(value
) != 0);
8342 case SWM_S_WINDOW_NAME_ENABLED
:
8343 window_name_enabled
= (atoi(value
) != 0);
8345 case SWM_S_WORKSPACE_CLAMP
:
8346 workspace_clamp
= (atoi(value
) != 0);
8348 case SWM_S_WORKSPACE_LIMIT
:
8349 workspace_limit
= atoi(value
);
8350 if (workspace_limit
> SWM_WS_MAX
)
8351 workspace_limit
= SWM_WS_MAX
;
8352 else if (workspace_limit
< 1)
8353 workspace_limit
= 1;
8355 ewmh_update_desktops();
8357 case SWM_S_WORKSPACE_NAME
:
8358 if (getenv("SWM_STARTED") != NULL
)
8362 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
8363 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
8365 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8366 errx(1, "setconfvalue: workspace_name: invalid "
8367 "workspace %d.", ws_id
+ 1);
8369 num_screens
= get_screen_count();
8370 for (i
= 0; i
< num_screens
; ++i
) {
8371 ws
= (struct workspace
*)&screens
[i
].ws
;
8373 if (strlen(s
) > 0) {
8374 free(ws
[ws_id
].name
);
8375 if ((ws
[ws_id
].name
= strdup(s
)) == NULL
)
8376 err(1, "setconfvalue: workspace_name.");
8378 ewmh_update_desktop_names();
8379 ewmh_get_desktop_names();
8390 setconfmodkey(const char *selector
, const char *value
, int flags
)
8392 /* suppress unused warnings since vars are needed */
8396 if (strncasecmp(value
, "Mod1", strlen("Mod1")) == 0)
8397 update_modkey(XCB_MOD_MASK_1
);
8398 else if (strncasecmp(value
, "Mod2", strlen("Mod2")) == 0)
8399 update_modkey(XCB_MOD_MASK_2
);
8400 else if (strncasecmp(value
, "Mod3", strlen("Mod3")) == 0)
8401 update_modkey(XCB_MOD_MASK_3
);
8402 else if (strncasecmp(value
, "Mod4", strlen("Mod4")) == 0)
8403 update_modkey(XCB_MOD_MASK_4
);
8404 else if (strncasecmp(value
, "Mod5", strlen("Mod5")) == 0)
8405 update_modkey(XCB_MOD_MASK_5
);
8412 setconfcolor(const char *selector
, const char *value
, int flags
)
8414 int first
, last
, i
= 0, num_screens
;
8416 num_screens
= get_screen_count();
8418 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
8419 if (selector
== NULL
|| strlen(selector
) == 0 ||
8420 (last
= atoi(selector
) - 1) < 0) {
8422 last
= num_screens
- 1;
8427 if (last
>= num_screens
) {
8428 add_startup_exception("invalid screen index: %d out of bounds "
8429 "(maximum %d)", last
+ 1, num_screens
);
8433 for (i
= first
; i
<= last
; ++i
) {
8434 setscreencolor(value
, i
, flags
);
8437 * When setting focus/unfocus colors, we need to also
8438 * set maximize colors to match if they haven't been customized.
8440 if (flags
== SWM_S_COLOR_FOCUS
&&
8441 !screens
[i
].c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].manual
)
8442 setscreencolor(value
, i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
8443 else if (flags
== SWM_S_COLOR_UNFOCUS
&&
8444 !screens
[i
].c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].manual
)
8445 setscreencolor(value
, i
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
8447 screens
[i
].c
[flags
].manual
= 1;
8454 setconfregion(const char *selector
, const char *value
, int flags
)
8456 /* suppress unused warnings since vars are needed */
8460 custom_region(value
);
8465 setautorun(const char *selector
, const char *value
, int flags
)
8475 /* suppress unused warnings since vars are needed */
8479 if (getenv("SWM_STARTED"))
8483 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
8484 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
8486 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8487 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
8489 sp
= expand_tilde((char *)&s
);
8492 * This is a little intricate
8494 * If the pid already exists we simply reuse it because it means it was
8495 * used before AND not claimed by manage_window. We get away with
8496 * altering it in the parent after INSERT because this can not be a race
8499 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
8502 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
8504 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
8505 err(1, "setautorun: realloc");
8506 a
.argv
[argc
- 1] = ap
;
8510 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
8511 err(1, "setautorun: realloc");
8512 a
.argv
[argc
] = NULL
;
8514 if ((pid
= fork()) == 0) {
8515 spawn(ws_id
, &a
, true);
8524 p
= calloc(1, sizeof *p
);
8527 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
8537 setlayout(const char *selector
, const char *value
, int flags
)
8539 struct workspace
*ws
;
8540 int ws_id
, i
, x
, mg
, ma
, si
, ar
;
8541 int st
= SWM_V_STACK
, num_screens
;
8545 /* suppress unused warnings since vars are needed */
8549 if (getenv("SWM_STARTED"))
8553 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
8554 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
8555 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8556 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8559 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8560 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
8562 if (strcasecmp(s
, "vertical") == 0)
8564 else if (strcasecmp(s
, "vertical_flip") == 0) {
8567 } else if (strcasecmp(s
, "horizontal") == 0)
8569 else if (strcasecmp(s
, "horizontal_flip") == 0) {
8572 } else if (strcasecmp(s
, "fullscreen") == 0)
8575 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8576 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8579 num_screens
= get_screen_count();
8580 for (i
= 0; i
< num_screens
; i
++) {
8581 ws
= (struct workspace
*)&screens
[i
].ws
;
8582 ws
[ws_id
].cur_layout
= &layouts
[st
];
8584 ws
[ws_id
].always_raise
= (ar
!= 0);
8585 if (st
== SWM_MAX_STACK
)
8589 for (x
= 0; x
< abs(mg
); x
++) {
8590 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8591 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
8592 SWM_ARG_ID_MASTERSHRINK
);
8595 for (x
= 0; x
< abs(ma
); x
++) {
8596 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8597 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
8598 SWM_ARG_ID_MASTERDEL
);
8601 for (x
= 0; x
< abs(si
); x
++) {
8602 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8603 si
>= 0 ? SWM_ARG_ID_STACKINC
:
8604 SWM_ARG_ID_STACKDEC
);
8608 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8609 SWM_ARG_ID_FLIPLAYOUT
);
8616 /* config options */
8617 struct config_option
{
8619 int (*func
)(const char*, const char*, int);
8622 struct config_option configopt
[] = {
8623 { "autorun", setautorun
, 0 },
8624 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
8625 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
8626 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
8627 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
8628 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
8629 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
8630 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
8631 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
8632 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
8633 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
8634 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
8635 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
8636 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
8637 { "bind", setconfbinding
, 0 },
8638 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
8639 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
8640 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
8641 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
8642 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
8643 { "color_focus_maximized", setconfcolor
, SWM_S_COLOR_FOCUS_MAXIMIZED
},
8644 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
8645 { "color_unfocus_maximized", setconfcolor
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
},
8646 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
8647 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
8648 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
8649 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
8650 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
8651 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
8652 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
8653 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
8654 { "iconic_enabled", setconfvalue
, SWM_S_ICONIC_ENABLED
},
8655 { "java_workaround", setconfvalue
, SWM_S_JAVA_WORKAROUND
},
8656 { "keyboard_mapping", setkeymapping
, 0 },
8657 { "layout", setlayout
, 0 },
8658 { "maximize_hide_bar", setconfvalue
, SWM_S_MAXIMIZE_HIDE_BAR
},
8659 { "modkey", setconfmodkey
, 0 },
8660 { "program", setconfspawn
, 0 },
8661 { "quirk", setconfquirk
, 0 },
8662 { "region", setconfregion
, 0 },
8663 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
8664 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
8665 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
8666 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
8667 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
8668 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
8669 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
8670 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
8671 { "title_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
}, /* For backwards compat. */
8672 { "title_name_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
}, /* For backwards compat. */
8673 { "urgent_collapse", setconfvalue
, SWM_S_URGENT_COLLAPSE
},
8674 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
8675 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
8676 { "warp_focus", setconfvalue
, SWM_S_WARP_FOCUS
},
8677 { "warp_pointer", setconfvalue
, SWM_S_WARP_POINTER
},
8678 { "window_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
},
8679 { "window_instance_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
},
8680 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
8681 { "workspace_clamp", setconfvalue
, SWM_S_WORKSPACE_CLAMP
},
8682 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
8683 { "name", setconfvalue
, SWM_S_WORKSPACE_NAME
},
8687 _add_startup_exception(const char *fmt
, va_list ap
)
8689 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
8690 warn("%s: asprintf", __func__
);
8694 add_startup_exception(const char *fmt
, ...)
8700 if (startup_exception
)
8703 /* force bar to be enabled due to exception */
8707 _add_startup_exception(fmt
, ap
);
8712 conf_load(const char *filename
, int keymapping
)
8715 char *line
= NULL
, *cp
, *ce
, *optsub
, *optval
= NULL
;
8716 size_t linelen
, lineno
= 0;
8717 int wordlen
, i
, optidx
, count
;
8718 struct config_option
*opt
= NULL
;
8720 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
8722 if (filename
== NULL
) {
8723 warnx("conf_load: no filename");
8727 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
8729 if ((config
= fopen(filename
, "r")) == NULL
) {
8730 warn("conf_load: fopen: %s", filename
);
8734 while (!feof(config
)) {
8738 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
8739 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
8741 err(1, "%s", filename
);
8746 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
8747 if (cp
[0] == '\0') {
8751 /* get config option */
8752 wordlen
= strcspn(cp
, "=[ \t\n");
8754 add_startup_exception("%s: line %zd: no option found",
8759 for (i
= 0; i
< LENGTH(configopt
); i
++) {
8760 opt
= &configopt
[i
];
8761 if (strncasecmp(cp
, opt
->optname
, wordlen
) == 0 &&
8762 (int)strlen(opt
->optname
) == wordlen
) {
8768 add_startup_exception("%s: line %zd: unknown option "
8769 "%.*s", filename
, lineno
, wordlen
, cp
);
8772 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
8773 add_startup_exception("%s: line %zd: invalid option "
8774 "%.*s", filename
, lineno
, wordlen
, cp
);
8778 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
8780 /* get [selector] if any */
8785 /* Get length of selector. */
8786 for (ce
= ++cp
; *ce
!= '\0'; ++ce
) {
8787 /* Find matching (not escaped) bracket. */
8788 if (*ce
== ']' && *(ce
- 1) != '\\') {
8795 add_startup_exception("%s: line %zd: syntax "
8796 "error: unterminated selector", filename
,
8801 /* ce points at ']'; terminate optsub. */
8806 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
8809 if (strlen(optval
) == 0) {
8810 add_startup_exception("%s: line %zd: must supply value "
8811 "to %s", filename
, lineno
,
8812 configopt
[optidx
].optname
);
8815 /* call function to deal with it all */
8816 if (configopt
[optidx
].func(optsub
, optval
,
8817 configopt
[optidx
].funcflags
) != 0) {
8818 add_startup_exception("%s: line %zd: invalid data for "
8819 "%s", filename
, lineno
, configopt
[optidx
].optname
);
8827 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
8833 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
8835 struct ws_win
*parent
, *w
;
8836 struct swm_region
*r
;
8837 struct workspace
*ws
;
8838 xcb_icccm_wm_hints_t wmh
;
8840 parent
= find_window(win
->transient
);
8842 parent
->focus_child
= win
;
8844 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
8845 " for %#x trans %#x\n", win
->id
, win
->transient
);
8847 r
= root_to_region(win
->s
->root
, SWM_CK_ALL
);
8849 /* parent doen't exist in our window list */
8850 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
8851 if (xcb_icccm_get_wm_hints_reply(conn
,
8852 xcb_icccm_get_wm_hints(conn
, w
->id
),
8854 warnx("can't get hints for %#x", w
->id
);
8858 if (win
->hints
.window_group
!= wmh
.window_group
)
8861 w
->focus_child
= win
;
8862 win
->transient
= w
->id
;
8864 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
8865 "transient to %#x\n", win
->transient
);
8872 window_get_pid(xcb_window_t win
)
8877 xcb_get_property_cookie_t pc
;
8878 xcb_get_property_reply_t
*pr
;
8880 apid
= get_atom_from_string("_NET_WM_PID");
8881 if (apid
== XCB_ATOM_NONE
)
8884 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
8885 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
8888 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
8893 if (pr
->type
== apid
&& pr
->format
== 32)
8894 ret
= *((pid_t
*)xcb_get_property_value(pr
));
8900 apid
= get_atom_from_string("_SWM_PID");
8901 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
8903 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
8906 if (pr
->type
!= apid
) {
8911 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
8918 get_swm_ws(xcb_window_t id
)
8924 xcb_get_property_reply_t
*gpr
;
8926 gpr
= xcb_get_property_reply(conn
,
8927 xcb_get_property(conn
, 0, id
, a_swm_ws
,
8928 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
8933 proplen
= xcb_get_property_value_length(gpr
);
8935 prop
= malloc(proplen
+ 1);
8938 xcb_get_property_value(gpr
),
8940 prop
[proplen
] = '\0';
8947 DNPRINTF(SWM_D_PROP
, "get_swm_ws: _SWM_WS: %s\n", prop
);
8948 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
8950 DNPRINTF(SWM_D_PROP
, "get_swm_ws: win #%s: %s",
8960 get_ws_idx(struct ws_win
*win
)
8962 xcb_get_property_reply_t
*gpr
;
8968 gpr
= xcb_get_property_reply(conn
,
8969 xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
8970 XCB_ATOM_CARDINAL
, 0, 1),
8973 if (gpr
->type
== XCB_ATOM_CARDINAL
&& gpr
->format
== 32)
8974 ws_idx
= *((int *)xcb_get_property_value(gpr
));
8978 if (ws_idx
== -1 && !(win
->quirks
& SWM_Q_IGNORESPAWNWS
))
8979 ws_idx
= get_swm_ws(win
->id
);
8981 if (ws_idx
> workspace_limit
- 1 || ws_idx
< -1)
8984 DNPRINTF(SWM_D_PROP
, "get_ws_idx: win %#x, ws_idx: %d\n", win
->id
,
8991 manage_window(xcb_window_t id
, int spawn_pos
, bool mapped
)
8993 struct ws_win
*win
, *ww
;
8994 struct swm_region
*r
;
8997 xcb_get_geometry_reply_t
*gr
;
8998 xcb_window_t trans
= XCB_WINDOW_NONE
;
8999 uint32_t i
, wa
[2], new_flags
;
9000 int ws_idx
, force_ws
= -1;
9001 char *class, *instance
, *name
;
9003 if ((win
= find_window(id
)) != NULL
) {
9004 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x already "
9005 "managed; skipping.)\n", id
);
9006 return (win
); /* Already managed. */
9009 /* See if window is on the unmanaged list. */
9010 if ((win
= find_unmanaged_window(id
)) != NULL
) {
9011 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x found on "
9012 "unmanaged list.\n", id
);
9013 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
9016 set_child_transient(win
, &trans
);
9020 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is new.\n", id
);
9023 /* Try to get initial window geometry. */
9024 gr
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, id
), NULL
);
9026 DNPRINTF(SWM_D_MISC
, "manage_window: get geometry failed.\n");
9030 /* Create and initialize ws_win object. */
9031 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
9032 err(1, "manage_window: calloc: failed to allocate memory for "
9037 /* Figureout which region the window belongs to. */
9038 r
= root_to_region(gr
->root
, SWM_CK_ALL
);
9040 /* Ignore window border if there is one. */
9041 WIDTH(win
) = gr
->width
;
9042 HEIGHT(win
) = gr
->height
;
9043 X(win
) = gr
->x
+ gr
->border_width
- border_width
;
9044 Y(win
) = gr
->y
+ gr
->border_width
- border_width
;
9045 win
->bordered
= true;
9046 win
->mapped
= mapped
;
9047 win
->s
= r
->s
; /* this never changes */
9051 /* Select which X events to monitor and set border pixel color. */
9052 wa
[0] = win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
9053 wa
[1] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
9054 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
9056 wa
[1] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
9059 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
|
9060 XCB_CW_EVENT_MASK
, wa
);
9062 /* Get WM_SIZE_HINTS. */
9063 xcb_icccm_get_wm_normal_hints_reply(conn
,
9064 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
9068 xcb_icccm_get_wm_hints_reply(conn
,
9069 xcb_icccm_get_wm_hints(conn
, win
->id
),
9072 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
9073 xcb_icccm_get_wm_transient_for_reply(conn
,
9074 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
9077 win
->transient
= trans
;
9078 set_child_transient(win
, &win
->transient
);
9081 /* Get WM_PROTOCOLS. */
9082 get_wm_protocols(win
);
9084 /* Set initial quirks based on EWMH. */
9085 ewmh_autoquirk(win
);
9087 /* Determine initial quirks. */
9088 xcb_icccm_get_wm_class_reply(conn
,
9089 xcb_icccm_get_wm_class(conn
, win
->id
),
9092 class = win
->ch
.class_name
? win
->ch
.class_name
: "";
9093 instance
= win
->ch
.instance_name
? win
->ch
.instance_name
: "";
9094 name
= get_win_name(win
->id
);
9096 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, instance: %s, "
9097 "name: %s\n", class, instance
, name
);
9099 /* java is retarded so treat it special */
9100 if (strstr(instance
, "sun-awt")) {
9101 DNPRINTF(SWM_D_CLASS
, "manage_window: java window detected.\n");
9105 TAILQ_FOREACH(qp
, &quirks
, entry
) {
9106 if (regexec(&qp
->regex_class
, class, 0, NULL
, 0) == 0 &&
9107 regexec(&qp
->regex_instance
, instance
, 0, NULL
, 0) == 0 &&
9108 regexec(&qp
->regex_name
, name
, 0, NULL
, 0) == 0) {
9109 DNPRINTF(SWM_D_CLASS
, "manage_window: matched "
9110 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp
->class,
9111 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
9112 win
->quirks
= qp
->quirk
;
9113 if (qp
->ws
>= 0 && qp
->ws
< workspace_limit
)
9120 /* Reset font sizes (the bruteforce way; no default keybinding). */
9121 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
9122 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9123 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
9124 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9125 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
9128 /* Figure out which workspace the window belongs to. */
9129 if (!(win
->quirks
& SWM_Q_IGNOREPID
) &&
9130 (p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
9131 win
->ws
= &r
->s
->ws
[p
->ws
];
9132 TAILQ_REMOVE(&pidlist
, p
, entry
);
9135 } else if ((ws_idx
= get_ws_idx(win
)) != -1 &&
9137 /* _SWM_WS is set; use that. */
9138 win
->ws
= &r
->s
->ws
[ws_idx
];
9139 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
9140 /* Launch transients in the same ws as parent. */
9147 win
->ws
= &r
->s
->ws
[force_ws
];
9149 /* Set the _NET_WM_DESKTOP atom. */
9150 DNPRINTF(SWM_D_PROP
, "manage_window: set _NET_WM_DESKTOP: %d\n",
9152 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
9153 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &win
->ws
->idx
);
9155 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
9156 xcb_delete_property(conn
, win
->id
, a_swm_ws
);
9158 /* WS must already be set for this to work. */
9159 store_float_geom(win
);
9161 /* Make sure window is positioned inside its region, if its active. */
9163 region_containment(win
, r
, SWM_CW_ALLSIDES
|
9164 SWM_CW_HARDBOUNDARY
);
9169 /* Figure out where to insert the window in the workspace list. */
9170 if (trans
&& (ww
= find_window(trans
)))
9171 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
9172 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_ABOVE
)
9173 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
9175 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_BELOW
)
9176 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
9177 else switch (spawn_pos
) {
9180 case SWM_STACK_ABOVE
:
9181 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
9183 case SWM_STACK_BOTTOM
:
9184 case SWM_STACK_BELOW
:
9185 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
9188 ewmh_update_client_list();
9190 TAILQ_INSERT_HEAD(&win
->ws
->stack
, win
, stack_entry
);
9193 /* Get/apply initial _NET_WM_STATE */
9194 ewmh_get_wm_state(win
);
9197 new_flags
= win
->ewmh_flags
;
9199 if (win
->quirks
& SWM_Q_FLOAT
)
9200 new_flags
|= EWMH_F_ABOVE
;
9202 if (win
->quirks
& SWM_Q_ANYWHERE
)
9203 new_flags
|= SWM_F_MANUAL
;
9205 ewmh_apply_flags(win
, new_flags
);
9206 ewmh_update_wm_state(win
);
9208 /* Set initial _NET_WM_ALLOWED_ACTIONS */
9209 ewmh_update_actions(win
);
9213 DNPRINTF(SWM_D_MISC
, "manage_window: done. win %#x, (x,y) w x h: "
9214 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win
->id
,
9215 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
9216 YESNO(ICONIC(win
)), win
->transient
);
9222 free_window(struct ws_win
*win
)
9224 DNPRINTF(SWM_D_MISC
, "free_window: win %#x\n", win
->id
);
9229 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
9231 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
9236 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
9239 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
9243 unmanage_window(struct ws_win
*win
)
9245 struct ws_win
*parent
;
9250 DNPRINTF(SWM_D_MISC
, "unmanage_window: win %#x\n", win
->id
);
9253 parent
= find_window(win
->transient
);
9255 parent
->focus_child
= NULL
;
9258 TAILQ_REMOVE(&win
->ws
->stack
, win
, stack_entry
);
9259 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
9260 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
9262 ewmh_update_client_list();
9266 expose(xcb_expose_event_t
*e
)
9269 struct swm_region
*r
;
9271 DNPRINTF(SWM_D_EVENT
, "expose: win %#x\n", e
->window
);
9273 num_screens
= get_screen_count();
9274 for (i
= 0; i
< num_screens
; i
++)
9275 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9276 if (e
->window
== WINID(r
->bar
))
9284 focusin(xcb_focus_in_event_t
*e
)
9286 DNPRINTF(SWM_D_EVENT
, "focusin: win %#x, mode: %s(%u), "
9287 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
9288 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
9292 focusout(xcb_focus_out_event_t
*e
)
9294 DNPRINTF(SWM_D_EVENT
, "focusout: win %#x, mode: %s(%u), "
9295 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
9296 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
9301 keypress(xcb_key_press_event_t
*e
)
9303 xcb_keysym_t keysym
;
9306 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
9308 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
9309 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9310 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
9311 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
9312 e
->child
, e
->state
, YESNO(e
->same_screen
));
9314 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
9317 last_event_time
= e
->time
;
9319 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
9320 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
),
9321 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
9322 else if (keyfuncs
[kp
->funcid
].func
)
9323 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
, SWM_CK_ALL
),
9324 &(keyfuncs
[kp
->funcid
].args
));
9327 /* Unfreeze grab events. */
9328 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
, e
->time
);
9331 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
9335 buttonpress(xcb_button_press_event_t
*e
)
9337 struct ws_win
*win
= NULL
;
9338 struct swm_region
*r
, *old_r
;
9340 bool handled
= false;
9342 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): %#x (%d,%d), "
9343 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9344 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
9345 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9346 e
->state
, YESNO(e
->same_screen
));
9348 if (e
->event
== e
->root
) {
9349 if (e
->child
!= 0) {
9350 win
= find_window(e
->child
);
9351 /* Pass ButtonPress to window if it isn't managed. */
9355 /* Focus on empty region */
9356 /* If no windows on region if its empty. */
9357 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
9359 DNPRINTF(SWM_D_EVENT
, "buttonpress: "
9360 "NULL region; ignoring.\n");
9364 if (TAILQ_EMPTY(&r
->ws
->winlist
)) {
9365 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
9366 if (old_r
&& old_r
!= r
)
9367 unfocus_win(old_r
->ws
->focus
);
9369 xcb_set_input_focus(conn
,
9370 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
9372 /* Clear bar since empty. */
9380 win
= find_window(e
->event
);
9386 last_event_time
= e
->time
;
9388 focus_win(get_focus_magic(win
));
9390 for (i
= 0; i
< LENGTH(buttons
); i
++)
9391 if (client_click
== buttons
[i
].action
&& buttons
[i
].func
&&
9392 buttons
[i
].button
== e
->detail
&&
9393 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
)) {
9394 buttons
[i
].func(win
, &buttons
[i
].args
);
9400 DNPRINTF(SWM_D_EVENT
, "buttonpress: passing to window.\n");
9401 /* Replay event to event window */
9402 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
9404 DNPRINTF(SWM_D_EVENT
, "buttonpress: handled.\n");
9405 /* Unfreeze grab events. */
9406 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
9414 print_win_geom(xcb_window_t w
)
9416 xcb_get_geometry_reply_t
*wa
;
9418 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
9420 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: %#x\n",
9425 DNPRINTF(SWM_D_MISC
, "print_win_geom: win %#x, root: %#x, "
9426 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
9427 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
9436 get_stack_mode_name(uint8_t mode
)
9441 case XCB_STACK_MODE_ABOVE
:
9444 case XCB_STACK_MODE_BELOW
:
9447 case XCB_STACK_MODE_TOP_IF
:
9450 case XCB_STACK_MODE_BOTTOM_IF
:
9453 case XCB_STACK_MODE_OPPOSITE
:
9465 configurerequest(xcb_configure_request_event_t
*e
)
9468 struct swm_region
*r
= NULL
;
9470 uint32_t wc
[7] = {0};
9474 if ((win
= find_window(e
->window
)) == NULL
)
9475 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
9479 if (swm_debug
& SWM_D_EVENT
) {
9480 print_win_geom(e
->window
);
9482 DNPRINTF(SWM_D_EVENT
, "configurerequest: win %#x, "
9483 "parent: %#x, new: %s, value_mask: %u { ", e
->window
,
9484 e
->parent
, YESNO(new), e
->value_mask
);
9485 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
9486 DPRINTF("X: %d ", e
->x
);
9487 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
9488 DPRINTF("Y: %d ", e
->y
);
9489 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
9490 DPRINTF("W: %u ", e
->width
);
9491 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
9492 DPRINTF("H: %u ", e
->height
);
9493 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
9494 DPRINTF("Border: %u ", e
->border_width
);
9495 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
9496 DPRINTF("Sibling: %#x ", e
->sibling
);
9497 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
9498 DPRINTF("StackMode: %s(%u) ",
9499 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
9505 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
9506 mask
|= XCB_CONFIG_WINDOW_X
;
9509 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
9510 mask
|= XCB_CONFIG_WINDOW_Y
;
9513 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
9514 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
9517 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
9518 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
9519 wc
[i
++] = e
->height
;
9521 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
9522 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
9523 wc
[i
++] = e
->border_width
;
9525 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
9526 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
9527 wc
[i
++] = e
->sibling
;
9529 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
9530 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
9531 wc
[i
++] = e
->stack_mode
;
9535 xcb_configure_window(conn
, e
->window
, mask
, wc
);
9538 } else if ((!MANUAL(win
) || win
->quirks
& SWM_Q_ANYWHERE
) &&
9539 !FULLSCREEN(win
) && !MAXIMIZED(win
)) {
9542 else if (win
->ws
->old_r
)
9545 /* windows are centered unless ANYWHERE quirk is set. */
9546 if (win
->quirks
& SWM_Q_ANYWHERE
) {
9547 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
9548 win
->g_float
.x
= e
->x
;
9550 win
->g_float
.x
-= X(r
);
9553 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
9554 win
->g_float
.y
= e
->y
;
9556 win
->g_float
.y
-= Y(r
);
9560 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
9561 win
->g_float
.w
= e
->width
;
9563 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
9564 win
->g_float
.h
= e
->height
;
9566 win
->g_floatvalid
= true;
9568 if (!MAXIMIZED(win
) && !FULLSCREEN(win
) &&
9569 (TRANS(win
) || (ABOVE(win
) &&
9570 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]))) {
9571 WIDTH(win
) = win
->g_float
.w
;
9572 HEIGHT(win
) = win
->g_float
.h
;
9575 update_floater(win
);
9590 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
9594 configurenotify(xcb_configure_notify_event_t
*e
)
9598 DNPRINTF(SWM_D_EVENT
, "configurenotify: win %#x, event win: %#x, "
9599 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
9600 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
9601 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
9602 YESNO(e
->override_redirect
));
9604 win
= find_window(e
->window
);
9606 xcb_icccm_get_wm_normal_hints_reply(conn
,
9607 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
9610 if (font_adjusted
) {
9618 destroynotify(xcb_destroy_notify_event_t
*e
)
9622 DNPRINTF(SWM_D_EVENT
, "destroynotify: win %#x\n", e
->window
);
9624 if ((win
= find_window(e
->window
)) == NULL
) {
9625 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
9631 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
9632 /* If we were focused, make sure we focus on something else. */
9633 if (win
== win
->ws
->focus
)
9634 win
->ws
->focus_pending
= get_focus_prev(win
);
9637 unmanage_window(win
);
9640 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
9641 if (win
->ws
->focus_pending
) {
9642 focus_win(win
->ws
->focus_pending
);
9643 win
->ws
->focus_pending
= NULL
;
9644 } else if (win
== win
->ws
->focus
) {
9645 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
9646 win
->ws
->r
->id
, XCB_CURRENT_TIME
);
9657 get_notify_detail_label(uint8_t detail
)
9662 case XCB_NOTIFY_DETAIL_ANCESTOR
:
9665 case XCB_NOTIFY_DETAIL_VIRTUAL
:
9668 case XCB_NOTIFY_DETAIL_INFERIOR
:
9671 case XCB_NOTIFY_DETAIL_NONLINEAR
:
9672 label
= "Nonlinear";
9674 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
9675 label
= "NonlinearVirtual";
9677 case XCB_NOTIFY_DETAIL_POINTER
:
9680 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
9681 label
= "PointerRoot";
9683 case XCB_NOTIFY_DETAIL_NONE
:
9694 get_notify_mode_label(uint8_t mode
)
9699 case XCB_NOTIFY_MODE_NORMAL
:
9702 case XCB_NOTIFY_MODE_GRAB
:
9705 case XCB_NOTIFY_MODE_UNGRAB
:
9708 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
9709 label
= "WhileGrabbed";
9720 enternotify(xcb_enter_notify_event_t
*e
)
9723 struct swm_region
*r
;
9725 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): %#x "
9726 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9727 "child: %#x, same_screen_focus: %s, state: %d\n",
9728 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9729 get_notify_mode_label(e
->mode
), e
->mode
,
9730 get_notify_detail_label(e
->detail
), e
->detail
,
9731 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9732 YESNO(e
->same_screen_focus
), e
->state
);
9734 if (focus_mode
== SWM_FOCUS_MANUAL
&&
9735 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
9736 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
9740 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
9741 e
->mode
== XCB_NOTIFY_MODE_UNGRAB
) {
9742 DNPRINTF(SWM_D_EVENT
, "enternotify: ungrab; ignoring.\n");
9746 last_event_time
= e
->time
;
9748 if ((win
= find_window(e
->event
)) == NULL
) {
9749 if (e
->event
== e
->root
) {
9750 /* If no windows on pointer region, then focus root. */
9751 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
9753 DNPRINTF(SWM_D_EVENT
, "enternotify: "
9754 "NULL region; ignoring.\n");
9760 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
9765 if (e
->mode
== XCB_NOTIFY_MODE_NORMAL
&&
9766 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
9767 DNPRINTF(SWM_D_EVENT
, "enternotify: entering from "
9768 "inferior; ignoring\n");
9772 focus_win(get_focus_magic(win
));
9780 leavenotify(xcb_leave_notify_event_t
*e
)
9782 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): %#x "
9783 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9784 "child: %#x, same_screen_focus: %s, state: %d\n",
9785 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9786 get_notify_mode_label(e
->mode
), e
->mode
,
9787 get_notify_detail_label(e
->detail
), e
->detail
,
9788 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9789 YESNO(e
->same_screen_focus
), e
->state
);
9794 mapnotify(xcb_map_notify_event_t
*e
)
9796 struct ws_win
*win
, *parent
= NULL
;
9797 struct workspace
*ws
;
9799 DNPRINTF(SWM_D_EVENT
, "mapnotify: win %#x\n", e
->window
);
9801 if ((win
= manage_window(e
->window
, spawn_position
, true)) == NULL
)
9805 /* Need to know if win was mapped due to ws switch. */
9806 if (ws
->state
== SWM_WS_STATE_MAPPED
) {
9807 if (ws
->focus_pending
&& TRANS(ws
->focus_pending
))
9808 parent
= find_window(win
->transient
);
9810 /* If window's parent is maximized, don't clear it. */
9811 if ((parent
== NULL
) || !MAXIMIZED(parent
))
9812 if (clear_maximized(ws
) > 0)
9817 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
9819 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
9820 if (ws
->focus_pending
== win
) {
9822 ws
->focus_pending
= NULL
;
9823 center_pointer(win
->ws
->r
);
9832 mappingnotify(xcb_mapping_notify_event_t
*e
)
9835 int i
, j
, num_screens
;
9837 xcb_refresh_keyboard_mapping(syms
, e
);
9839 if (e
->request
== XCB_MAPPING_KEYBOARD
) {
9842 /* Regrab buttons on all managed windows. */
9843 num_screens
= get_screen_count();
9844 for (i
= 0; i
< num_screens
; i
++)
9845 for (j
= 0; j
< workspace_limit
; j
++)
9846 TAILQ_FOREACH(w
, &screens
[i
].ws
[j
].winlist
,
9853 maprequest(xcb_map_request_event_t
*e
)
9855 struct ws_win
*win
, *w
= NULL
;
9856 xcb_get_window_attributes_reply_t
*war
;
9858 DNPRINTF(SWM_D_EVENT
, "maprequest: win %#x\n",
9861 war
= xcb_get_window_attributes_reply(conn
,
9862 xcb_get_window_attributes(conn
, e
->window
),
9865 DNPRINTF(SWM_D_EVENT
, "maprequest: window lost.\n");
9869 if (war
->override_redirect
) {
9870 DNPRINTF(SWM_D_EVENT
, "maprequest: override_redirect; "
9875 win
= manage_window(e
->window
, spawn_position
,
9876 (war
->map_state
== XCB_MAP_STATE_VIEWABLE
));
9880 /* The new window should get focus; prepare. */
9881 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
9882 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
) &&
9883 (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
9884 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
9885 win
->hints
.input
))) {
9886 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
9887 /* See if other wins of same type are already mapped. */
9888 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
9889 if (w
== win
|| !w
->mapped
)
9892 if (w
->ch
.class_name
&&
9893 win
->ch
.class_name
&&
9894 strcmp(w
->ch
.class_name
,
9895 win
->ch
.class_name
) == 0 &&
9896 w
->ch
.instance_name
&&
9897 win
->ch
.instance_name
&&
9898 strcmp(w
->ch
.instance_name
,
9899 win
->ch
.instance_name
) == 0)
9905 win
->ws
->focus_pending
= get_focus_magic(win
);
9908 /* All windows need to be mapped if they are in the current workspace.*/
9912 /* Ignore EnterNotify to handle the mapnotify without interference. */
9913 if (focus_mode
== SWM_FOCUS_DEFAULT
)
9914 event_drain(XCB_ENTER_NOTIFY
);
9917 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
9921 motionnotify(xcb_motion_notify_event_t
*e
)
9923 struct swm_region
*r
= NULL
;
9926 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): %#x "
9927 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9928 "child: %#x, same_screen_focus: %s, state: %d\n",
9929 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9930 get_notify_detail_label(e
->detail
), e
->detail
,
9931 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9932 YESNO(e
->same_screen
), e
->state
);
9934 last_event_time
= e
->time
;
9936 if (focus_mode
== SWM_FOCUS_MANUAL
)
9939 num_screens
= get_screen_count();
9940 for (i
= 0; i
< num_screens
; i
++)
9941 if (screens
[i
].root
== e
->root
)
9944 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9945 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
9946 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
9954 get_atom_name(xcb_atom_t atom
)
9957 #ifdef SWM_DEBUG_ATOM_NAMES
9959 * This should be disabled during most debugging since
9960 * xcb_get_* causes an xcb_flush.
9963 xcb_get_atom_name_reply_t
*r
;
9965 r
= xcb_get_atom_name_reply(conn
,
9966 xcb_get_atom_name(conn
, atom
),
9969 len
= xcb_get_atom_name_name_length(r
);
9971 name
= malloc(len
+ 1);
9973 memcpy(name
, xcb_get_atom_name_name(r
), len
);
9987 propertynotify(xcb_property_notify_event_t
*e
)
9990 struct workspace
*ws
;
9994 name
= get_atom_name(e
->atom
);
9995 DNPRINTF(SWM_D_EVENT
, "propertynotify: win %#x, atom: %s(%u), "
9996 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
10000 win
= find_window(e
->window
);
10006 last_event_time
= e
->time
;
10008 if (e
->atom
== a_state
) {
10009 /* State just changed, make sure it gets focused if mapped. */
10010 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
10011 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
10013 ws
->focus_pending
== win
) {
10014 focus_win(ws
->focus_pending
);
10015 ws
->focus_pending
= NULL
;
10019 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
10020 e
->atom
== XCB_ATOM_WM_NAME
) {
10022 } else if (e
->atom
== a_prot
) {
10023 get_wm_protocols(win
);
10030 unmapnotify(xcb_unmap_notify_event_t
*e
)
10032 struct ws_win
*win
;
10033 struct workspace
*ws
;
10035 DNPRINTF(SWM_D_EVENT
, "unmapnotify: win %#x\n", e
->window
);
10037 /* If we aren't managing the window, then ignore. */
10038 win
= find_window(e
->window
);
10039 if (win
== NULL
|| win
->id
!= e
->window
)
10042 /* Do nothing if already withdrawn. */
10043 if (!win
->mapped
&& !ICONIC(win
))
10047 win
->mapped
= false;
10049 /* If win was focused, make sure to focus on something else. */
10050 if (win
== ws
->focus
) {
10051 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10052 ws
->focus_pending
= get_focus_prev(win
);
10053 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
10054 "focus_pending: %#x\n",
10055 WINID(ws
->focus_pending
));
10063 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
10066 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
10067 unmanage_window(win
);
10073 /* Update focus if ws is active. */
10074 if (WS_FOCUSED(ws
)) {
10075 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
10076 focus_win(get_pointer_win(ws
->r
->s
->root
));
10077 } else if (ws
->focus_pending
) {
10078 focus_win(ws
->focus_pending
);
10079 ws
->focus_pending
= NULL
;
10080 } else if (ws
->focus
== NULL
) {
10081 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
10082 ws
->r
->id
, XCB_CURRENT_TIME
);
10086 center_pointer(ws
->r
);
10092 get_source_type_label(uint32_t type
)
10097 case EWMH_SOURCE_TYPE_NONE
:
10100 case EWMH_SOURCE_TYPE_NORMAL
:
10103 case EWMH_SOURCE_TYPE_OTHER
:
10115 clientmessage(xcb_client_message_event_t
*e
)
10117 struct ws_win
*win
;
10118 struct swm_region
*r
= NULL
;
10121 int num_screens
, i
;
10122 xcb_map_request_event_t mre
;
10126 name
= get_atom_name(e
->type
);
10127 DNPRINTF(SWM_D_EVENT
, "clientmessage: win %#x, atom: %s(%u)\n",
10128 e
->window
, name
, e
->type
);
10132 if (e
->type
== ewmh
[_NET_CURRENT_DESKTOP
].atom
) {
10133 num_screens
= get_screen_count();
10134 for (i
= 0; i
< num_screens
; i
++)
10135 if (screens
[i
].root
== e
->window
) {
10136 r
= screens
[i
].r_focus
;
10140 if (r
&& e
->data
.data32
[0] < (uint32_t)workspace_limit
) {
10141 a
.id
= e
->data
.data32
[0];
10149 win
= find_window(e
->window
);
10151 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
10152 /* Manage the window with maprequest. */
10153 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
10154 "unmanaged window.\n");
10155 mre
.window
= e
->window
;
10161 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
10162 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW, "
10163 "source_type: %s(%d)\n",
10164 get_source_type_label(e
->data
.data32
[0]),
10165 e
->data
.data32
[0]);
10168 * Allow focus changes that are a result of direct user
10169 * action and from applications that use the old EWMH spec.
10171 if (e
->data
.data32
[0] != EWMH_SOURCE_TYPE_NORMAL
||
10172 win
->quirks
& SWM_Q_OBEYAPPFOCUSREQ
) {
10173 if (WS_FOCUSED(win
->ws
))
10176 win
->ws
->focus_pending
= win
;
10178 } else if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
10179 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
10180 if (win
->can_delete
)
10181 client_msg(win
, a_delete
, 0);
10183 xcb_kill_client(conn
, win
->id
);
10184 } else if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
10185 DNPRINTF(SWM_D_EVENT
,
10186 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
10188 if (e
->data
.data32
[0] & (1<<8)) /* x */
10189 X(win
) = e
->data
.data32
[1];
10190 if (e
->data
.data32
[0] & (1<<9)) /* y */
10191 Y(win
) = e
->data
.data32
[2];
10192 if (e
->data
.data32
[0] & (1<<10)) /* width */
10193 WIDTH(win
) = e
->data
.data32
[3];
10194 if (e
->data
.data32
[0] & (1<<11)) /* height */
10195 HEIGHT(win
) = e
->data
.data32
[4];
10197 update_window(win
);
10199 /* Notify no change was made. */
10200 config_win(win
, NULL
);
10201 /* TODO: Change stack sizes */
10203 } else if (e
->type
== ewmh
[_NET_RESTACK_WINDOW
].atom
) {
10204 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_RESTACK_WINDOW\n");
10205 val
[0] = e
->data
.data32
[1]; /* Sibling window. */
10206 val
[1] = e
->data
.data32
[2]; /* Stack mode detail. */
10208 xcb_configure_window(conn
, win
->id
, XCB_CONFIG_WINDOW_SIBLING
|
10209 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
10210 } else if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
10211 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
10212 ewmh_change_wm_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
10213 if (e
->data
.data32
[2])
10214 ewmh_change_wm_state(win
, e
->data
.data32
[2],
10215 e
->data
.data32
[0]);
10217 ewmh_update_wm_state(win
);
10219 } else if (e
->type
== ewmh
[_NET_WM_DESKTOP
].atom
) {
10220 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_DESKTOP\n");
10223 win_to_ws(win
, e
->data
.data32
[0], true);
10225 /* Restack if either the source or destination ws is mapped. */
10226 if (r
!= NULL
|| win
->ws
->r
!= NULL
) {
10228 load_float_geom(win
);
10240 int errcode
= xcb_connection_has_error(conn
);
10241 #ifdef XCB_CONN_ERROR
10244 case XCB_CONN_ERROR
:
10245 s
= "Socket error, pipe error or other stream error.";
10247 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
10248 s
= "Extension not supported.";
10250 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
10251 s
= "Insufficient memory.";
10253 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
10254 s
= "Request length was exceeded.";
10256 case XCB_CONN_CLOSED_PARSE_ERR
:
10257 s
= "Error parsing display string.";
10260 s
= "Unknown error.";
10263 errx(errcode
, "X CONNECTION ERROR: %s", s
);
10266 errx(errcode
, "X CONNECTION ERROR");
10273 int num_screens
, i
;
10274 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
10275 XCB_EVENT_MASK_ENTER_WINDOW
;
10277 xcb_void_cookie_t wac
;
10278 xcb_generic_error_t
*error
;
10280 /* this causes an error if some other window manager is running */
10281 num_screens
= get_screen_count();
10282 for (i
= 0; i
< num_screens
; i
++) {
10283 if ((sc
= get_screen(i
)) == NULL
)
10284 errx(1, "ERROR: can't get screen %d.", i
);
10285 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: %#x\n",
10287 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
10288 XCB_CW_EVENT_MASK
, &val
);
10289 if ((error
= xcb_request_check(conn
, wac
))) {
10290 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
10291 error
->error_code
);
10296 /* click to focus on empty region */
10297 xcb_grab_button(conn
, 1, sc
->root
, BUTTONMASK
,
10298 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
10299 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_1
, XCB_BUTTON_MASK_ANY
);
10306 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
10308 struct swm_region
*r
= NULL
, *n
;
10309 struct workspace
*ws
= NULL
;
10313 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
10314 s
->idx
, w
, h
, x
, y
);
10316 /* remove any conflicting regions */
10317 n
= TAILQ_FIRST(&s
->rl
);
10320 n
= TAILQ_NEXT(r
, entry
);
10321 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
10322 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
10323 if (r
->ws
->r
!= NULL
)
10324 r
->ws
->old_r
= r
->ws
->r
;
10327 xcb_destroy_window(conn
, r
->id
);
10328 TAILQ_REMOVE(&s
->rl
, r
, entry
);
10329 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
10333 /* search old regions for one to reuse */
10335 /* size + location match */
10336 TAILQ_FOREACH(r
, &s
->orl
, entry
)
10337 if (X(r
) == x
&& Y(r
) == y
&&
10338 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
10342 TAILQ_FOREACH(r
, &s
->orl
, entry
)
10343 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
10347 TAILQ_REMOVE(&s
->orl
, r
, entry
);
10348 /* try to use old region's workspace */
10349 if (r
->ws
->r
== NULL
)
10352 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
10353 err(1, "new_region: calloc: failed to allocate memory "
10356 /* if we don't have a workspace already, find one */
10358 for (i
= 0; i
< workspace_limit
; i
++)
10359 if (s
->ws
[i
].r
== NULL
) {
10366 errx(1, "new_region: no free workspaces");
10368 if (ws
->state
== SWM_WS_STATE_HIDDEN
)
10369 ws
->state
= SWM_WS_STATE_MAPPING
;
10377 r
->ws_prior
= NULL
;
10380 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
10382 /* Invisible region window to detect pointer events on empty regions. */
10383 r
->id
= xcb_generate_id(conn
);
10384 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
10385 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
10387 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
10388 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
10389 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
10391 /* Make sure region input is at the bottom. */
10392 wa
[0] = XCB_STACK_MODE_BELOW
;
10393 xcb_configure_window(conn
, r
->id
, XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
10395 xcb_map_window(conn
, r
->id
);
10399 scan_randr(int idx
)
10401 #ifdef SWM_XRR_HAS_CRTC
10404 #endif /* SWM_XRR_HAS_CRTC */
10405 struct swm_region
*r
;
10406 struct ws_win
*win
;
10408 xcb_randr_get_screen_resources_current_cookie_t src
;
10409 xcb_randr_get_screen_resources_current_reply_t
*srr
;
10410 xcb_randr_get_crtc_info_cookie_t cic
;
10411 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
10412 xcb_randr_crtc_t
*crtc
;
10413 xcb_screen_t
*screen
;
10415 DNPRINTF(SWM_D_MISC
, "scan_randr: screen: %d\n", idx
);
10417 if ((screen
= get_screen(idx
)) == NULL
)
10418 errx(1, "ERROR: can't get screen %d.", idx
);
10420 num_screens
= get_screen_count();
10421 if (idx
>= num_screens
)
10422 errx(1, "scan_randr: invalid screen");
10424 /* remove any old regions */
10425 while ((r
= TAILQ_FIRST(&screens
[idx
].rl
)) != NULL
) {
10426 r
->ws
->old_r
= r
->ws
->r
= NULL
;
10428 xcb_destroy_window(conn
, r
->id
);
10429 TAILQ_REMOVE(&screens
[idx
].rl
, r
, entry
);
10430 TAILQ_INSERT_TAIL(&screens
[idx
].orl
, r
, entry
);
10434 /* map virtual screens onto physical screens */
10435 #ifdef SWM_XRR_HAS_CRTC
10436 if (randr_support
) {
10437 src
= xcb_randr_get_screen_resources_current(conn
,
10438 screens
[idx
].root
);
10439 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
10442 new_region(&screens
[idx
], 0, 0,
10443 screen
->width_in_pixels
,
10444 screen
->height_in_pixels
);
10447 ncrtc
= srr
->num_crtcs
;
10449 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
10450 for (c
= 0; c
< ncrtc
; c
++) {
10451 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
10453 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
10456 if (cir
->num_outputs
== 0) {
10461 if (cir
->mode
== 0)
10462 new_region(&screens
[idx
], 0, 0,
10463 screen
->width_in_pixels
,
10464 screen
->height_in_pixels
);
10466 new_region(&screens
[idx
],
10467 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
10472 #endif /* SWM_XRR_HAS_CRTC */
10474 /* If detection failed, create a single region that spans the screen. */
10475 if (TAILQ_EMPTY(&screens
[idx
].rl
))
10476 new_region(&screens
[idx
], 0, 0, screen
->width_in_pixels
,
10477 screen
->height_in_pixels
);
10480 /* Cleanup unused previously visible workspaces. */
10481 TAILQ_FOREACH(r
, &screens
[idx
].orl
, entry
) {
10482 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
10484 r
->ws
->state
= SWM_WS_STATE_HIDDEN
;
10486 /* The screen shouldn't focus on an unused region. */
10487 if (screens
[idx
].r_focus
== r
)
10488 screens
[idx
].r_focus
= NULL
;
10491 DNPRINTF(SWM_D_MISC
, "scan_randr: done.\n");
10495 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
10497 struct swm_region
*r
;
10498 int i
, num_screens
;
10500 DNPRINTF(SWM_D_EVENT
, "screenchange: root: %#x\n", e
->root
);
10502 num_screens
= get_screen_count();
10503 /* silly event doesn't include the screen index */
10504 for (i
= 0; i
< num_screens
; i
++)
10505 if (screens
[i
].root
== e
->root
)
10507 if (i
>= num_screens
)
10508 errx(1, "screenchange: screen not found");
10510 /* brute force for now, just re-enumerate the regions */
10514 print_win_geom(e
->root
);
10516 /* add bars to all regions */
10517 for (i
= 0; i
< num_screens
; i
++) {
10518 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10524 /* Make sure a region has focus on each screen. */
10525 for (i
= 0; i
< num_screens
; i
++) {
10526 if (screens
[i
].r_focus
== NULL
) {
10527 r
= TAILQ_FIRST(&screens
[i
].rl
);
10536 /* Update workspace state on all regions. */
10537 for (i
= 0; i
< num_screens
; i
++)
10538 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10539 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
10545 struct swm_region
*r
= NULL
;
10546 xcb_window_t
*wins
= NULL
, trans
, *cwins
= NULL
;
10547 int i
, j
, k
, n
, no
, num_screens
;
10549 bool manage
, mapped
;
10551 xcb_query_tree_cookie_t qtc
;
10552 xcb_query_tree_reply_t
*qtr
;
10553 xcb_get_window_attributes_cookie_t gac
;
10554 xcb_get_window_attributes_reply_t
*gar
;
10555 xcb_get_property_cookie_t pc
;
10556 xcb_get_property_reply_t
*pr
;
10558 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
10559 num_screens
= get_screen_count();
10560 for (i
= 0; i
< num_screens
; i
++) {
10561 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
10562 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
10565 wins
= xcb_query_tree_children(qtr
);
10566 no
= xcb_query_tree_children_length(qtr
);
10568 /* Try to sort windows according to _NET_CLIENT_LIST. */
10569 pr
= xcb_get_property_reply(conn
, xcb_get_property(conn
, 0,
10570 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
10571 XCB_ATOM_WINDOW
, 0, UINT32_MAX
), NULL
);
10573 cwins
= xcb_get_property_value(pr
);
10574 n
= xcb_get_property_value_length(pr
) /
10575 sizeof(xcb_atom_t
);
10577 for (j
= 0; j
< n
; ++j
) {
10578 for (k
= j
; k
< no
; ++k
) {
10579 if (wins
[k
] == cwins
[j
]) {
10580 /* Swap wins j and k. */
10582 wins
[j
] = cwins
[j
];
10590 /* attach windows to a region */
10591 /* normal windows */
10592 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top level windows.\n");
10593 for (j
= 0; j
< no
; j
++) {
10594 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
10595 if (r
->id
== wins
[j
]) {
10596 DNPRINTF(SWM_D_INIT
, "grab_windows: "
10597 "skip %#x; region input window.\n",
10600 } else if (r
->bar
->id
== wins
[j
]) {
10601 DNPRINTF(SWM_D_INIT
, "grab_windows: "
10602 "skip %#x; region bar.\n",
10611 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
10612 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
10614 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10615 "doesn't exist.\n", wins
[j
]);
10619 if (gar
->override_redirect
) {
10620 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10621 "override_redirect set.\n", wins
[j
]);
10626 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
10627 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
10629 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10630 "is transient for %#x.\n", wins
[j
], trans
);
10635 state
= get_win_state(wins
[j
]);
10636 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
10637 mapped
= gar
->map_state
== XCB_MAP_STATE_VIEWABLE
;
10638 if (mapped
|| manage
)
10639 manage_window(wins
[j
], SWM_STACK_TOP
, mapped
);
10642 /* transient windows */
10643 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
10644 for (j
= 0; j
< no
; j
++) {
10645 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
10646 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
10648 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10649 "doesn't exist.\n", wins
[j
]);
10653 if (gar
->override_redirect
) {
10654 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10655 "override_redirect set.\n", wins
[j
]);
10660 state
= get_win_state(wins
[j
]);
10661 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
10662 mapped
= gar
->map_state
== XCB_MAP_STATE_VIEWABLE
;
10663 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
10664 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
10665 &trans
, NULL
) && manage
)
10666 manage_window(wins
[j
], SWM_STACK_TOP
, mapped
);
10671 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
10675 setup_screens(void)
10677 int i
, j
, k
, num_screens
;
10678 struct workspace
*ws
;
10679 uint32_t gcv
[1], wa
[1];
10680 const xcb_query_extension_reply_t
*qep
;
10681 xcb_screen_t
*screen
;
10682 xcb_randr_query_version_cookie_t c
;
10683 xcb_randr_query_version_reply_t
*r
;
10685 num_screens
= get_screen_count();
10686 if ((screens
= calloc(num_screens
,
10687 sizeof(struct swm_screen
))) == NULL
)
10688 err(1, "setup_screens: calloc: failed to allocate memory for "
10691 /* Initial RandR setup. */
10692 randr_support
= false;
10693 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
10694 if (qep
->present
) {
10695 c
= xcb_randr_query_version(conn
, 1, 1);
10696 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
10698 if (r
->major_version
>= 1) {
10699 randr_support
= true;
10700 randr_eventbase
= qep
->first_event
;
10706 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
10708 /* map physical screens */
10709 for (i
= 0; i
< num_screens
; i
++) {
10710 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
10711 screens
[i
].idx
= i
;
10712 screens
[i
].r_focus
= NULL
;
10714 TAILQ_INIT(&screens
[i
].rl
);
10715 TAILQ_INIT(&screens
[i
].orl
);
10716 if ((screen
= get_screen(i
)) == NULL
)
10717 errx(1, "ERROR: can't get screen %d.", i
);
10718 screens
[i
].root
= screen
->root
;
10720 /* set default colors */
10721 setscreencolor("red", i
, SWM_S_COLOR_FOCUS
);
10722 setscreencolor("rgb:88/88/88", i
, SWM_S_COLOR_UNFOCUS
);
10723 setscreencolor("rgb:00/80/80", i
, SWM_S_COLOR_BAR_BORDER
);
10724 setscreencolor("rgb:00/40/40", i
,
10725 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
10726 setscreencolor("black", i
, SWM_S_COLOR_BAR
);
10727 setscreencolor("rgb:a0/a0/a0", i
, SWM_S_COLOR_BAR_FONT
);
10728 setscreencolor("red", i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
10729 setscreencolor("rgb:88/88/88", i
,
10730 SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
10732 /* create graphics context on screen */
10733 screens
[i
].bar_gc
= xcb_generate_id(conn
);
10735 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
10736 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
10738 /* set default cursor */
10739 xcb_change_window_attributes(conn
, screens
[i
].root
,
10740 XCB_CW_CURSOR
, wa
);
10742 /* init all workspaces */
10743 /* XXX these should be dynamically allocated too */
10744 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
10745 ws
= &screens
[i
].ws
[j
];
10748 ws
->bar_enabled
= true;
10750 ws
->focus_prev
= NULL
;
10751 ws
->focus_pending
= NULL
;
10754 ws
->state
= SWM_WS_STATE_HIDDEN
;
10755 TAILQ_INIT(&ws
->stack
);
10756 TAILQ_INIT(&ws
->winlist
);
10757 TAILQ_INIT(&ws
->unmanagedlist
);
10759 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
10760 if (layouts
[k
].l_config
!= NULL
)
10761 layouts
[k
].l_config(ws
,
10762 SWM_ARG_ID_STACKINIT
);
10763 ws
->cur_layout
= &layouts
[0];
10764 ws
->cur_layout
->l_string(ws
);
10770 xcb_randr_select_input(conn
, screens
[i
].root
,
10771 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
10776 setup_globals(void)
10778 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
10779 err(1, "setup_globals: strdup: failed to allocate memory.");
10781 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
10782 err(1, "setup_globals: strdup: failed to allocate memory.");
10784 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
10785 errx(1, "unable to allocate key symbols");
10787 a_state
= get_atom_from_string("WM_STATE");
10788 a_prot
= get_atom_from_string("WM_PROTOCOLS");
10789 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
10790 a_net_supported
= get_atom_from_string("_NET_SUPPORTED");
10791 a_net_wm_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
10792 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
10793 a_utf8_string
= get_atom_from_string("UTF8_STRING");
10794 a_swm_ws
= get_atom_from_string("_SWM_WS");
10798 shutdown_cleanup(void)
10800 int i
, num_screens
;
10802 /* disable alarm because the following code may not be interrupted */
10804 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
10805 err(1, "can't disable alarm");
10818 num_screens
= get_screen_count();
10819 for (i
= 0; i
< num_screens
; ++i
) {
10822 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
10823 screens
[i
].root
, XCB_CURRENT_TIME
);
10825 if (screens
[i
].bar_gc
!= XCB_NONE
)
10826 xcb_free_gc(conn
, screens
[i
].bar_gc
);
10827 if (!bar_font_legacy
) {
10828 XftColorFree(display
, DefaultVisual(display
, i
),
10829 DefaultColormap(display
, i
), &bar_font_color
);
10830 XftColorFree(display
, DefaultVisual(display
, i
),
10831 DefaultColormap(display
, i
), &search_font_color
);
10834 for (j
= 0; j
< SWM_S_COLOR_MAX
; ++j
) {
10835 free(screens
[i
].c
[j
].name
);
10838 for (j
= 0; j
< SWM_WS_MAX
; ++j
) {
10839 free(screens
[i
].ws
[j
].name
);
10845 free(clock_format
);
10847 if (bar_font_legacy
)
10848 XFreeFontSet(display
, bar_fs
);
10850 XftFontClose(display
, bar_font
);
10853 xcb_key_symbols_free(syms
);
10855 xcb_disconnect(conn
);
10859 event_error(xcb_generic_error_t
*e
)
10863 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
10864 "resource_id: %u, minor_code: %u\n",
10865 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
10866 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
10867 e
->sequence
, e
->resource_id
, e
->minor_code
);
10871 event_handle(xcb_generic_event_t
*evt
)
10873 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
10875 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
10876 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
10877 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
10880 #define EVENT(type, callback) case type: callback((void *)evt); return
10881 EVENT(0, event_error
);
10882 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
10883 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
10884 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
10885 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
10886 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
10887 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
10888 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
10889 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
10890 /*EVENT(XCB_CREATE_NOTIFY, );*/
10891 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
10892 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
10893 EVENT(XCB_EXPOSE
, expose
);
10895 EVENT(XCB_FOCUS_IN
, focusin
);
10896 EVENT(XCB_FOCUS_OUT
, focusout
);
10898 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
10899 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
10900 EVENT(XCB_KEY_PRESS
, keypress
);
10901 /*EVENT(XCB_KEY_RELEASE, keypress);*/
10902 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
10904 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
10906 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
10907 EVENT(XCB_MAP_REQUEST
, maprequest
);
10908 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
10909 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
10910 /*EVENT(XCB_NO_EXPOSURE, );*/
10911 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
10912 /*EVENT(XCB_REPARENT_NOTIFY, );*/
10913 /*EVENT(XCB_RESIZE_REQUEST, );*/
10914 /*EVENT(XCB_SELECTION_CLEAR, );*/
10915 /*EVENT(XCB_SELECTION_NOTIFY, );*/
10916 /*EVENT(XCB_SELECTION_REQUEST, );*/
10917 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
10918 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
10921 if (type
- randr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
10922 screenchange((void *)evt
);
10926 main(int argc
, char *argv
[])
10928 struct swm_region
*r
;
10929 char conf
[PATH_MAX
], *cfile
= NULL
;
10931 int xfd
, i
, num_screens
;
10932 struct sigaction sact
;
10933 xcb_generic_event_t
*evt
;
10935 struct pollfd pfd
[2];
10936 bool stdin_ready
= false, startup
= true;
10938 /* suppress unused warning since var is needed */
10942 time_started
= time(NULL
);
10946 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
10947 if (setlocale(LC_CTYPE
, "") == NULL
|| setlocale(LC_TIME
, "") == NULL
)
10948 warnx("no locale support");
10950 /* handle some signals */
10951 bzero(&sact
, sizeof(sact
));
10952 sigemptyset(&sact
.sa_mask
);
10954 sact
.sa_handler
= sighdlr
;
10955 sigaction(SIGINT
, &sact
, NULL
);
10956 sigaction(SIGQUIT
, &sact
, NULL
);
10957 sigaction(SIGTERM
, &sact
, NULL
);
10958 sigaction(SIGHUP
, &sact
, NULL
);
10960 sact
.sa_handler
= sighdlr
;
10961 sact
.sa_flags
= SA_NOCLDSTOP
;
10962 sigaction(SIGCHLD
, &sact
, NULL
);
10964 if ((display
= XOpenDisplay(0)) == NULL
)
10965 errx(1, "can not open display");
10967 conn
= XGetXCBConnection(display
);
10968 if (xcb_connection_has_error(conn
))
10969 errx(1, "can not get XCB connection");
10971 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
10973 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
10974 xfd
= xcb_get_file_descriptor(conn
);
10976 /* look for local and global conf file */
10977 pwd
= getpwuid(getuid());
10979 errx(1, "invalid user: %d", getuid());
10981 xcb_grab_server(conn
);
10982 xcb_aux_sync(conn
);
10984 /* flush all events */
10985 while ((evt
= xcb_poll_for_event(conn
))) {
10986 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
10992 errx(1, "another window manager is currently running");
10994 /* Load Xcursors and/or cursorfont glyph cursors. */
10997 xcb_aux_sync(conn
);
11007 for (i
= 0; ; i
++) {
11012 snprintf(conf
, sizeof conf
, "%s/.%s",
11013 pwd
->pw_dir
, SWM_CONF_FILE
);
11017 snprintf(conf
, sizeof conf
, "/etc/%s",
11022 snprintf(conf
, sizeof conf
, "%s/.%s",
11023 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
11026 /* global compat */
11027 snprintf(conf
, sizeof conf
, "/etc/%s",
11028 SWM_CONF_FILE_OLD
);
11034 if (strlen(conf
) && stat(conf
, &sb
) != -1)
11035 if (S_ISREG(sb
.st_mode
)) {
11042 /* load conf (if any) */
11044 conf_load(cfile
, SWM_CONF_DEFAULT
);
11048 if (getenv("SWM_STARTED") == NULL
)
11049 setenv("SWM_STARTED", "YES", 1);
11051 /* setup all bars */
11052 num_screens
= get_screen_count();
11053 for (i
= 0; i
< num_screens
; i
++)
11054 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11057 /* Manage existing windows. */
11064 xcb_ungrab_server(conn
);
11067 /* Update state of each newly mapped workspace. */
11068 for (i
= 0; i
< num_screens
; i
++)
11069 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11070 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
11072 memset(&pfd
, 0, sizeof(pfd
));
11074 pfd
[0].events
= POLLIN
;
11075 pfd
[1].fd
= STDIN_FILENO
;
11076 pfd
[1].events
= POLLIN
;
11079 while ((evt
= xcb_poll_for_event(conn
))) {
11086 /* If just (re)started, set default focus if needed. */
11090 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
11091 r
= TAILQ_FIRST(&screens
[0].rl
);
11100 num_readable
= poll(pfd
, bar_extra
? 2 : 1, 1000);
11101 if (num_readable
== -1) {
11102 DNPRINTF(SWM_D_MISC
, "poll failed: %s", strerror(errno
));
11103 } else if (num_readable
> 0 && bar_extra
&& pfd
[1].revents
& POLLIN
) {
11104 stdin_ready
= true;
11108 restart(NULL
, NULL
);
11117 stdin_ready
= false;
11118 bar_extra_update();
11125 shutdown_cleanup();