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;
430 XFontSet bar_fs
= NULL
;
431 XFontSetExtents
*bar_fs_extents
;
432 XftFont
*bar_font
= NULL
;
433 bool bar_font_legacy
= true;
434 char *bar_fonts
= NULL
;
435 XftColor bar_font_color
;
436 XftColor search_font_color
;
437 char *startup_exception
= NULL
;
438 unsigned int nr_exceptions
= 0;
440 /* layout manager data */
441 struct swm_geometry
{
454 struct swm_geometry g
;
457 /* virtual "screens" */
459 TAILQ_ENTRY(swm_region
) entry
;
461 struct swm_geometry g
;
462 struct workspace
*ws
; /* current workspace on this region */
463 struct workspace
*ws_prior
; /* prior workspace on this region */
464 struct swm_screen
*s
; /* screen idx */
467 TAILQ_HEAD(swm_region_list
, swm_region
);
470 TAILQ_ENTRY(ws_win
) entry
;
471 TAILQ_ENTRY(ws_win
) stack_entry
;
473 xcb_window_t transient
;
474 struct ws_win
*focus_child
; /* focus on child transient */
475 struct swm_geometry g
; /* current geometry */
476 struct swm_geometry g_float
; /* region coordinates */
477 bool g_floatvalid
; /* g_float geometry validity */
481 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
488 struct workspace
*ws
; /* always valid */
489 struct swm_screen
*s
; /* always valid, never changes */
491 xcb_icccm_get_wm_class_reply_t ch
;
492 xcb_icccm_wm_hints_t hints
;
494 TAILQ_HEAD(ws_win_list
, ws_win
);
495 TAILQ_HEAD(ws_win_stack
, ws_win
);
499 TAILQ_ENTRY(pid_e
) entry
;
503 TAILQ_HEAD(pid_list
, pid_e
);
504 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
506 /* layout handlers */
508 void vertical_config(struct workspace
*, int);
509 void vertical_stack(struct workspace
*, struct swm_geometry
*);
510 void horizontal_config(struct workspace
*, int);
511 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
512 void max_stack(struct workspace
*, struct swm_geometry
*);
513 void plain_stacker(struct workspace
*);
514 void fancy_stacker(struct workspace
*);
517 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
518 void (*l_config
)(struct workspace
*, int);
520 #define SWM_L_FOCUSPREV (1<<0)
521 #define SWM_L_MAPONFOCUS (1<<1)
522 void (*l_string
)(struct workspace
*);
524 /* stack, configure */
525 { vertical_stack
, vertical_config
, 0, plain_stacker
},
526 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
528 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
529 { NULL
, NULL
, 0, NULL
},
532 /* position of max_stack mode in the layouts array, index into layouts! */
533 #define SWM_V_STACK (0)
534 #define SWM_H_STACK (1)
535 #define SWM_MAX_STACK (2)
537 #define SWM_H_SLICE (32)
538 #define SWM_V_SLICE (32)
540 /* define work spaces */
542 int idx
; /* workspace index */
543 char *name
; /* workspace name */
544 bool always_raise
; /* raise windows on focus */
545 bool bar_enabled
; /* bar visibility */
546 struct layout
*cur_layout
; /* current layout handlers */
547 struct ws_win
*focus
; /* may be NULL */
548 struct ws_win
*focus_prev
; /* may be NULL */
549 struct ws_win
*focus_pending
; /* may be NULL */
550 struct swm_region
*r
; /* may be NULL */
551 struct swm_region
*old_r
; /* may be NULL */
552 struct ws_win_list winlist
; /* list of windows in ws */
553 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
554 struct ws_win_stack stack
; /* stacking order */
555 int state
; /* mapping state */
556 char stacker
[10]; /* display stacker and layout */
560 int horizontal_msize
;
562 int horizontal_stacks
;
563 bool horizontal_flip
;
573 SWM_WS_STATE_MAPPING
,
579 SWM_S_COLOR_BAR_BORDER
,
580 SWM_S_COLOR_BAR_BORDER_UNFOCUS
,
581 SWM_S_COLOR_BAR_FONT
,
583 SWM_S_COLOR_FOCUS_MAXIMIZED
,
585 SWM_S_COLOR_UNFOCUS_MAXIMIZED
,
589 /* physical screen mapping */
590 #define SWM_WS_MAX (22) /* hard limit */
591 int workspace_limit
= 10; /* soft limit */
594 int idx
; /* screen index */
595 struct swm_region_list rl
; /* list of regions on this screen */
596 struct swm_region_list orl
; /* list of old regions */
598 struct workspace ws
[SWM_WS_MAX
];
599 struct swm_region
*r_focus
;
606 } c
[SWM_S_COLOR_MAX
];
608 xcb_gcontext_t bar_gc
;
611 struct swm_screen
*screens
;
613 /* args to functions */
616 #define SWM_ARG_ID_FOCUSNEXT (0)
617 #define SWM_ARG_ID_FOCUSPREV (1)
618 #define SWM_ARG_ID_FOCUSMAIN (2)
619 #define SWM_ARG_ID_FOCUSURGENT (3)
620 #define SWM_ARG_ID_SWAPNEXT (10)
621 #define SWM_ARG_ID_SWAPPREV (11)
622 #define SWM_ARG_ID_SWAPMAIN (12)
623 #define SWM_ARG_ID_MOVELAST (13)
624 #define SWM_ARG_ID_MASTERSHRINK (20)
625 #define SWM_ARG_ID_MASTERGROW (21)
626 #define SWM_ARG_ID_MASTERADD (22)
627 #define SWM_ARG_ID_MASTERDEL (23)
628 #define SWM_ARG_ID_FLIPLAYOUT (24)
629 #define SWM_ARG_ID_STACKRESET (30)
630 #define SWM_ARG_ID_STACKINIT (31)
631 #define SWM_ARG_ID_STACKBALANCE (32)
632 #define SWM_ARG_ID_CYCLEWS_UP (40)
633 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
634 #define SWM_ARG_ID_CYCLERG_UP (42)
635 #define SWM_ARG_ID_CYCLERG_DOWN (43)
636 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
637 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
638 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
639 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
640 #define SWM_ARG_ID_STACKINC (50)
641 #define SWM_ARG_ID_STACKDEC (51)
642 #define SWM_ARG_ID_SS_ALL (60)
643 #define SWM_ARG_ID_SS_WINDOW (61)
644 #define SWM_ARG_ID_DONTCENTER (70)
645 #define SWM_ARG_ID_CENTER (71)
646 #define SWM_ARG_ID_KILLWINDOW (80)
647 #define SWM_ARG_ID_DELETEWINDOW (81)
648 #define SWM_ARG_ID_WIDTHGROW (90)
649 #define SWM_ARG_ID_WIDTHSHRINK (91)
650 #define SWM_ARG_ID_HEIGHTGROW (92)
651 #define SWM_ARG_ID_HEIGHTSHRINK (93)
652 #define SWM_ARG_ID_MOVEUP (100)
653 #define SWM_ARG_ID_MOVEDOWN (101)
654 #define SWM_ARG_ID_MOVELEFT (102)
655 #define SWM_ARG_ID_MOVERIGHT (103)
656 #define SWM_ARG_ID_RAISE (105)
657 #define SWM_ARG_ID_LOWER (106)
658 #define SWM_ARG_ID_BAR_TOGGLE (110)
659 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
660 #define SWM_ARG_ID_CYCLERG_MOVE_UP (112)
661 #define SWM_ARG_ID_CYCLERG_MOVE_DOWN (113)
667 TAILQ_ENTRY(quirk
) entry
;
668 char *class; /* WM_CLASS:class */
669 char *instance
; /* WM_CLASS:instance */
670 char *name
; /* WM_NAME */
672 regex_t regex_instance
;
675 int ws
; /* Initial workspace. */
676 #define SWM_Q_FLOAT (1<<0) /* Float this window. */
677 #define SWM_Q_TRANSSZ (1<<1) /* Transient window size too small. */
678 #define SWM_Q_ANYWHERE (1<<2) /* Don't position this window */
679 #define SWM_Q_XTERM_FONTADJ (1<<3) /* Adjust xterm fonts when resizing. */
680 #define SWM_Q_FULLSCREEN (1<<4) /* Remove border when fullscreen. */
681 #define SWM_Q_FOCUSPREV (1<<5) /* Focus on caller. */
682 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
683 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
684 #define SWM_Q_OBEYAPPFOCUSREQ (1<<8) /* Focus when applications ask. */
685 #define SWM_Q_IGNOREPID (1<<9) /* Ignore PID when determining ws. */
686 #define SWM_Q_IGNORESPAWNWS (1<<10) /* Ignore _SWM_WS when managing win. */
687 #define SWM_Q_NOFOCUSCYCLE (1<<11) /* Remove from normal focus cycle. */
688 #define SWM_Q_MINIMALBORDER (1<<12) /* No border when floating/unfocused. */
690 TAILQ_HEAD(quirk_list
, quirk
);
691 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
694 * Supported EWMH hints should be added to
695 * both the enum and the ewmh array
701 _NET_CURRENT_DESKTOP
,
702 _NET_DESKTOP_GEOMETRY
,
704 _NET_DESKTOP_VIEWPORT
,
705 _NET_MOVERESIZE_WINDOW
,
706 _NET_NUMBER_OF_DESKTOPS
,
708 _NET_WM_ACTION_ABOVE
,
709 _NET_WM_ACTION_CLOSE
,
710 _NET_WM_ACTION_FULLSCREEN
,
712 _NET_WM_ACTION_RESIZE
,
713 _NET_WM_ALLOWED_ACTIONS
,
715 _NET_WM_FULL_PLACEMENT
,
719 _NET_WM_STATE_FULLSCREEN
,
720 _NET_WM_STATE_HIDDEN
,
721 _NET_WM_STATE_MAXIMIZED_VERT
,
722 _NET_WM_STATE_MAXIMIZED_HORZ
,
723 _NET_WM_STATE_SKIP_PAGER
,
724 _NET_WM_STATE_SKIP_TASKBAR
,
726 _NET_WM_WINDOW_TYPE_DIALOG
,
727 _NET_WM_WINDOW_TYPE_DOCK
,
728 _NET_WM_WINDOW_TYPE_NORMAL
,
729 _NET_WM_WINDOW_TYPE_SPLASH
,
730 _NET_WM_WINDOW_TYPE_TOOLBAR
,
731 _NET_WM_WINDOW_TYPE_UTILITY
,
732 _SWM_WM_STATE_MANUAL
,
739 } ewmh
[SWM_EWMH_HINT_MAX
] = {
740 /* must be in same order as in the enum */
741 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
742 {"_NET_CLIENT_LIST", XCB_ATOM_NONE
},
743 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
744 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE
},
745 {"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE
},
746 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE
},
747 {"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE
},
748 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
749 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE
},
750 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE
},
751 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
752 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
753 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
754 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
755 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
756 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
757 {"_NET_WM_DESKTOP", XCB_ATOM_NONE
},
758 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE
},
759 {"_NET_WM_NAME", XCB_ATOM_NONE
},
760 {"_NET_WM_STATE", XCB_ATOM_NONE
},
761 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
762 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
763 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
764 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
765 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
766 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
767 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
768 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
769 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
770 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
771 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
772 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
773 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
774 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
775 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
778 /* EWMH source type */
780 EWMH_SOURCE_TYPE_NONE
= 0,
781 EWMH_SOURCE_TYPE_NORMAL
= 1,
782 EWMH_SOURCE_TYPE_OTHER
= 2,
789 XC_BOTTOM_LEFT_CORNER
,
790 XC_BOTTOM_RIGHT_CORNER
,
798 char *name
; /* Name used by Xcursor .*/
799 uint8_t cf_char
; /* cursorfont index. */
801 } cursors
[XC_MAX
] = {
802 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
803 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
804 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
805 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
806 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
807 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
808 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
811 #define SWM_SPAWN_OPTIONAL 0x1
815 TAILQ_ENTRY(spawn_prog
) entry
;
821 TAILQ_HEAD(spawn_list
, spawn_prog
);
822 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
824 /* user/key callable function IDs */
943 KF_DUMPWINS
, /* MUST BE LAST */
951 enum keyfuncid funcid
;
954 RB_HEAD(key_tree
, key
);
956 /* function prototypes */
957 void adjust_font(struct ws_win
*);
958 char *argsep(char **);
959 void bar_cleanup(struct swm_region
*);
960 void bar_extra_setup(void);
961 void bar_extra_stop(void);
962 int bar_extra_update(void);
963 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
964 void bar_fmt_expand(char *, size_t);
966 void bar_print(struct swm_region
*, const char *);
967 void bar_print_legacy(struct swm_region
*, const char *);
968 void bar_replace(char *, char *, struct swm_region
*, size_t);
969 void bar_replace_pad(char *, int *, size_t);
970 char *bar_replace_seq(char *, char *, struct swm_region
*, size_t *, size_t);
971 void bar_setup(struct swm_region
*);
972 void bar_toggle(struct swm_region
*, union arg
*);
973 void bar_urgent(char *, size_t);
974 void bar_window_class(char *, size_t, struct swm_region
*);
975 void bar_window_class_instance(char *, size_t, struct swm_region
*);
976 void bar_window_float(char *, size_t, struct swm_region
*);
977 void bar_window_instance(char *, size_t, struct swm_region
*);
978 void bar_window_name(char *, size_t, struct swm_region
*);
979 void bar_window_state(char *, size_t, struct swm_region
*);
980 void bar_workspace_name(char *, size_t, struct swm_region
*);
981 void buttonpress(xcb_button_press_event_t
*);
982 void center_pointer(struct swm_region
*);
983 void check_conn(void);
984 void clear_keys(void);
985 int clear_maximized(struct workspace
*);
986 void clear_quirks(void);
987 void clear_spawns(void);
988 void clientmessage(xcb_client_message_event_t
*);
989 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
990 int conf_load(const char *, int);
991 void configurenotify(xcb_configure_notify_event_t
*);
992 void configurerequest(xcb_configure_request_event_t
*);
993 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
994 void constrain_window(struct ws_win
*, struct swm_geometry
*, int *);
995 int count_win(struct workspace
*, bool);
996 void cursors_cleanup(void);
997 void cursors_load(void);
998 void custom_region(const char *);
999 void cyclerg(struct swm_region
*, union arg
*);
1000 void cyclews(struct swm_region
*, union arg
*);
1001 void cycle_layout(struct swm_region
*, union arg
*);
1002 void destroynotify(xcb_destroy_notify_event_t
*);
1003 void dumpwins(struct swm_region
*, union arg
*);
1004 int enable_wm(void);
1005 void enternotify(xcb_enter_notify_event_t
*);
1006 void event_drain(uint8_t);
1007 void event_error(xcb_generic_error_t
*);
1008 void event_handle(xcb_generic_event_t
*);
1009 void ewmh_apply_flags(struct ws_win
*, uint32_t);
1010 void ewmh_autoquirk(struct ws_win
*);
1011 void ewmh_get_desktop_names(void);
1012 void ewmh_get_wm_state(struct ws_win
*);
1013 void ewmh_update_actions(struct ws_win
*);
1014 void ewmh_update_client_list(void);
1015 void ewmh_update_current_desktop(void);
1016 void ewmh_update_desktop_names(void);
1017 void ewmh_update_desktops(void);
1018 void ewmh_change_wm_state(struct ws_win
*, xcb_atom_t
, long);
1019 void ewmh_update_wm_state(struct ws_win
*);
1020 char *expand_tilde(const char *);
1021 void expose(xcb_expose_event_t
*);
1022 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
1023 struct pid_e
*find_pid(pid_t
);
1024 struct ws_win
*find_unmanaged_window(xcb_window_t
);
1025 struct ws_win
*find_window(xcb_window_t
);
1026 void floating_toggle(struct swm_region
*, union arg
*);
1027 void focus(struct swm_region
*, union arg
*);
1029 void focusin(xcb_focus_in_event_t
*);
1030 void focusout(xcb_focus_out_event_t
*);
1032 void focus_flush(void);
1033 void focus_region(struct swm_region
*);
1034 void focusrg(struct swm_region
*, union arg
*);
1035 void focus_win(struct ws_win
*);
1036 void fontset_init(void);
1037 void free_window(struct ws_win
*);
1038 xcb_atom_t
get_atom_from_string(const char *);
1040 char *get_atom_name(xcb_atom_t
);
1042 struct ws_win
*get_focus_magic(struct ws_win
*);
1043 struct ws_win
*get_focus_prev(struct ws_win
*);
1045 char *get_notify_detail_label(uint8_t);
1046 char *get_notify_mode_label(uint8_t);
1048 struct ws_win
*get_pointer_win(xcb_window_t
);
1049 struct ws_win
*get_region_focus(struct swm_region
*);
1050 int get_region_index(struct swm_region
*);
1051 xcb_screen_t
*get_screen(int);
1052 int get_screen_count(void);
1054 char *get_source_type_label(uint32_t);
1055 char *get_stack_mode_name(uint8_t);
1057 int32_t get_swm_ws(xcb_window_t
);
1058 bool get_urgent(struct ws_win
*);
1059 char *get_win_name(xcb_window_t
);
1060 uint8_t get_win_state(xcb_window_t
);
1061 void get_wm_protocols(struct ws_win
*);
1062 int get_ws_idx(struct ws_win
*);
1063 void grabbuttons(struct ws_win
*);
1064 void grabkeys(void);
1065 void grab_windows(void);
1066 void iconify(struct swm_region
*, union arg
*);
1067 bool isxlfd(char *);
1068 void keypress(xcb_key_press_event_t
*);
1069 int key_cmp(struct key
*, struct key
*);
1070 void key_insert(unsigned int, KeySym
, enum keyfuncid
, const char *);
1071 struct key
*key_lookup(unsigned int, KeySym
);
1072 void key_remove(struct key
*);
1073 void key_replace(struct key
*, unsigned int, KeySym
, enum keyfuncid
,
1075 void kill_bar_extra_atexit(void);
1076 void kill_refs(struct ws_win
*);
1078 void leavenotify(xcb_leave_notify_event_t
*);
1080 void load_float_geom(struct ws_win
*);
1081 void lower_window(struct ws_win
*);
1082 struct ws_win
*manage_window(xcb_window_t
, int, bool);
1083 void map_window(struct ws_win
*);
1084 void mapnotify(xcb_map_notify_event_t
*);
1085 void mappingnotify(xcb_mapping_notify_event_t
*);
1086 void maprequest(xcb_map_request_event_t
*);
1087 void maximize_toggle(struct swm_region
*, union arg
*);
1088 void motionnotify(xcb_motion_notify_event_t
*);
1089 void move(struct ws_win
*, union arg
*);
1090 void move_step(struct swm_region
*, union arg
*);
1091 uint32_t name_to_pixel(int, const char *);
1092 void name_workspace(struct swm_region
*, union arg
*);
1093 void new_region(struct swm_screen
*, int, int, int, int);
1094 int parsekeys(const char *, unsigned int, unsigned int *, KeySym
*);
1095 int parsequirks(const char *, uint32_t *, int *);
1096 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1097 void pressbutton(struct swm_region
*, union arg
*);
1098 void priorws(struct swm_region
*, union arg
*);
1100 void print_win_geom(xcb_window_t
);
1102 void propertynotify(xcb_property_notify_event_t
*);
1103 void quirk_free(struct quirk
*);
1104 void quirk_insert(const char *, const char *, const char *, uint32_t, int);
1105 void quirk_remove(struct quirk
*);
1106 void quirk_replace(struct quirk
*, const char *, const char *, const char *,
1108 void quit(struct swm_region
*, union arg
*);
1109 void raise_toggle(struct swm_region
*, union arg
*);
1110 void raise_window(struct ws_win
*);
1111 void region_containment(struct ws_win
*, struct swm_region
*, int);
1112 struct swm_region
*region_under(struct swm_screen
*, int, int);
1113 void regionize(struct ws_win
*, int, int);
1114 void resize(struct ws_win
*, union arg
*);
1115 void resize_step(struct swm_region
*, union arg
*);
1116 void restart(struct swm_region
*, union arg
*);
1117 struct swm_region
*root_to_region(xcb_window_t
, int);
1118 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1119 void scan_randr(int);
1120 void search_do_resp(void);
1121 void search_resp_name_workspace(const char *, size_t);
1122 void search_resp_search_window(const char *);
1123 void search_resp_search_workspace(const char *);
1124 void search_resp_uniconify(const char *, size_t);
1125 void search_win(struct swm_region
*, union arg
*);
1126 void search_win_cleanup(void);
1127 void search_workspace(struct swm_region
*, union arg
*);
1128 void send_to_rg(struct swm_region
*, union arg
*);
1129 void send_to_ws(struct swm_region
*, union arg
*);
1130 void set_region(struct swm_region
*);
1131 int setautorun(const char *, const char *, int);
1132 int setconfbinding(const char *, const char *, int);
1133 int setconfcolor(const char *, const char *, int);
1134 int setconfmodkey(const char *, const char *, int);
1135 int setconfquirk(const char *, const char *, int);
1136 int setconfregion(const char *, const char *, int);
1137 int setconfspawn(const char *, const char *, int);
1138 int setconfvalue(const char *, const char *, int);
1139 void setkeybinding(unsigned int, KeySym
, enum keyfuncid
, const char *);
1140 int setkeymapping(const char *, const char *, int);
1141 int setlayout(const char *, const char *, int);
1142 void setquirk(const char *, const char *, const char *, uint32_t, int);
1143 void setscreencolor(const char *, int, int);
1144 void setspawn(const char *, const char *, int);
1145 void setup_ewmh(void);
1146 void setup_globals(void);
1147 void setup_keys(void);
1148 void setup_quirks(void);
1149 void setup_screens(void);
1150 void setup_spawn(void);
1151 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1152 void set_win_state(struct ws_win
*, uint8_t);
1153 void shutdown_cleanup(void);
1155 void socket_setnonblock(int);
1156 void sort_windows(struct ws_win_list
*);
1157 void spawn(int, union arg
*, bool);
1158 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1159 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1160 void spawn_insert(const char *, const char *, int);
1161 struct spawn_prog
*spawn_find(const char *);
1162 void spawn_remove(struct spawn_prog
*);
1163 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1164 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1165 void stack_config(struct swm_region
*, union arg
*);
1166 void stack_master(struct workspace
*, struct swm_geometry
*, int, bool);
1167 void store_float_geom(struct ws_win
*);
1168 char *strdupsafe(const char *);
1169 void swapwin(struct swm_region
*, union arg
*);
1170 void switchws(struct swm_region
*, union arg
*);
1171 void teardown_ewmh(void);
1172 void unescape_selector(char *);
1173 void unfocus_win(struct ws_win
*);
1174 void uniconify(struct swm_region
*, union arg
*);
1175 void unmanage_window(struct ws_win
*);
1176 void unmapnotify(xcb_unmap_notify_event_t
*);
1177 void unmap_all(void);
1178 void unmap_window(struct ws_win
*);
1179 void updatenumlockmask(void);
1180 void update_floater(struct ws_win
*);
1181 void update_modkey(unsigned int);
1182 void update_win_stacking(struct ws_win
*);
1183 void update_window(struct ws_win
*);
1184 void update_window_color(struct ws_win
*);
1185 void update_wm_state(struct ws_win
*win
);
1186 void validate_spawns(void);
1187 int validate_win(struct ws_win
*);
1188 int validate_ws(struct workspace
*);
1189 void version(struct swm_region
*, union arg
*);
1190 void win_to_ws(struct ws_win
*, int, bool);
1191 pid_t
window_get_pid(xcb_window_t
);
1192 void wkill(struct swm_region
*, union arg
*);
1193 void update_ws_stack(struct workspace
*);
1194 void xft_init(struct swm_region
*);
1195 void _add_startup_exception(const char *, va_list);
1196 void add_startup_exception(const char *, ...);
1198 RB_PROTOTYPE(key_tree
, key
, entry
, key_cmp
);
1199 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
1200 struct key_tree keys
;
1205 xcb_font_t cf
= XCB_NONE
;
1208 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1209 /* try to load Xcursor first. */
1210 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1213 /* fallback to cursorfont. */
1214 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1215 if (cf
== XCB_NONE
) {
1216 cf
= xcb_generate_id(conn
);
1217 xcb_open_font(conn
, cf
, strlen("cursor"),
1221 cursors
[i
].cid
= xcb_generate_id(conn
);
1222 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1223 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1224 0xffff, 0xffff, 0xffff);
1230 xcb_close_font(conn
, cf
);
1234 cursors_cleanup(void)
1237 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1238 xcb_free_cursor(conn
, cursors
[i
].cid
);
1242 expand_tilde(const char *s
)
1244 struct passwd
*ppwd
;
1252 errx(1, "expand_tilde: NULL string.");
1255 result
= strdup(sc
);
1261 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1262 errx(1, "expand_tilde: sysconf");
1264 if ((user
= calloc(1, max
+ 1)) == NULL
)
1265 errx(1, "expand_tilde: calloc");
1267 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1272 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1276 result
= strdup(sc
);
1278 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1282 errx(1, "expand_tilde: failed to allocate memory.");
1288 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1290 unsigned int tmpr
, tmpg
, tmpb
;
1292 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1295 *rr
= RGB_8_TO_16(tmpr
);
1296 *gg
= RGB_8_TO_16(tmpg
);
1297 *bb
= RGB_8_TO_16(tmpb
);
1303 get_screen(int screen
)
1305 const xcb_setup_t
*r
;
1306 xcb_screen_iterator_t iter
;
1308 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1309 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1313 iter
= xcb_setup_roots_iterator(r
);
1314 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1322 get_screen_count(void)
1324 const xcb_setup_t
*r
;
1326 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1327 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1331 return xcb_setup_roots_length(r
);
1335 get_region_index(struct swm_region
*r
)
1337 struct swm_region
*rr
;
1343 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1358 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1359 event_drain(XCB_ENTER_NOTIFY
);
1365 get_atom_from_string(const char *str
)
1367 xcb_intern_atom_cookie_t c
;
1368 xcb_intern_atom_reply_t
*r
;
1371 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1372 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1380 return (XCB_ATOM_NONE
);
1384 get_wm_protocols(struct ws_win
*win
) {
1386 xcb_icccm_get_wm_protocols_reply_t wpr
;
1388 if (xcb_icccm_get_wm_protocols_reply(conn
,
1389 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
1391 for (i
= 0; i
< (int)wpr
.atoms_len
; i
++) {
1392 if (wpr
.atoms
[i
] == a_takefocus
)
1393 win
->take_focus
= true;
1394 if (wpr
.atoms
[i
] == a_delete
)
1395 win
->can_delete
= true;
1397 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
1404 xcb_window_t root
, win
;
1405 int i
, j
, num_screens
;
1407 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1408 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1410 num_screens
= get_screen_count();
1411 for (i
= 0; i
< num_screens
; i
++) {
1412 root
= screens
[i
].root
;
1414 /* Set up _NET_SUPPORTING_WM_CHECK. */
1415 win
= xcb_generate_id(conn
);
1416 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
1417 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1418 XCB_COPY_FROM_PARENT
, 0, NULL
);
1420 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
1421 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1422 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1423 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1426 * Impersonate LG3D non-reparenting WM, written by Sun, to
1427 * workaround a Java GUI rendering issue.
1429 if (java_workaround
)
1430 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1431 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1432 8, strlen("LG3D"), "LG3D");
1434 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1435 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1436 8, strlen("spectrwm"), "spectrwm");
1438 /* Report supported atoms */
1439 xcb_delete_property(conn
, root
, a_net_supported
);
1440 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1441 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, root
,
1442 a_net_supported
, XCB_ATOM_ATOM
, 32, 1,
1447 ewmh_update_desktops();
1448 ewmh_get_desktop_names();
1456 xcb_get_property_cookie_t pc
;
1457 xcb_get_property_reply_t
*pr
;
1459 num_screens
= get_screen_count();
1461 for (i
= 0; i
< num_screens
; i
++) {
1462 /* Get the support check window and destroy it */
1463 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, a_net_wm_check
,
1464 XCB_ATOM_WINDOW
, 0, 1);
1465 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1468 if (pr
->format
== a_net_wm_check
) {
1469 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1471 xcb_destroy_window(conn
, id
);
1472 xcb_delete_property(conn
, screens
[i
].root
,
1474 xcb_delete_property(conn
, screens
[i
].root
,
1482 ewmh_autoquirk(struct ws_win
*win
)
1484 xcb_get_property_reply_t
*r
;
1485 xcb_get_property_cookie_t c
;
1489 c
= xcb_get_property(conn
, 0, win
->id
,
1490 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1491 r
= xcb_get_property_reply(conn
, c
, NULL
);
1495 type
= xcb_get_property_value(r
);
1496 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1498 for (i
= 0; i
< n
; i
++) {
1499 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1501 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1502 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1503 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1504 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1507 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1508 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1509 win
->quirks
= SWM_Q_FLOAT
;
1517 ewmh_update_actions(struct ws_win
*win
)
1519 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1525 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1528 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1529 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1530 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1533 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1534 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1537 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1538 #define _NET_WM_STATE_ADD 1 /* add/set property */
1539 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1542 ewmh_change_wm_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1549 name
= get_atom_name(state
);
1550 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: win %#x, state: %s, "
1551 "action: %ld\n", WINID(win
), name
, action
);
1557 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1558 flag
= EWMH_F_FULLSCREEN
;
1559 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1560 flag
= EWMH_F_ABOVE
;
1561 else if (state
== ewmh
[_NET_WM_STATE_HIDDEN
].atom
)
1562 flag
= EWMH_F_HIDDEN
;
1563 else if (state
== ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
||
1564 state
== ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
)
1565 flag
= EWMH_F_MAXIMIZED
;
1566 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1567 flag
= SWM_F_MANUAL
;
1568 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1569 flag
= EWMH_F_SKIP_PAGER
;
1570 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1571 flag
= EWMH_F_SKIP_TASKBAR
;
1573 /* Disallow unfloating transients. */
1574 if (TRANS(win
) && flag
== EWMH_F_ABOVE
)
1577 new_flags
= win
->ewmh_flags
;
1580 case _NET_WM_STATE_REMOVE
:
1583 case _NET_WM_STATE_ADD
:
1586 case _NET_WM_STATE_TOGGLE
:
1591 ewmh_apply_flags(win
, new_flags
);
1594 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: done.\n");
1598 ewmh_apply_flags(struct ws_win
*win
, uint32_t pending
)
1600 struct workspace
*ws
;
1603 changed
= win
->ewmh_flags
^ pending
;
1607 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: pending: %d\n", pending
);
1609 win
->ewmh_flags
= pending
;
1612 if (changed
& EWMH_F_HIDDEN
) {
1614 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1615 ws
->focus_pending
= get_focus_prev(win
);
1620 /* Reload floating geometry in case region changed. */
1622 load_float_geom(win
);
1624 /* The window is no longer iconic, prepare focus. */
1625 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1626 ws
->focus_pending
= get_focus_magic(win
);
1631 if (changed
& EWMH_F_ABOVE
) {
1632 if (ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1634 load_float_geom(win
);
1635 else if (!MAXIMIZED(win
))
1636 store_float_geom(win
);
1638 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1639 changed
&= ~EWMH_F_MAXIMIZED
;
1643 win
->ewmh_flags
^= EWMH_F_ABOVE
& pending
;
1647 if (changed
& EWMH_F_MAXIMIZED
) {
1648 /* VERT and/or HORZ changed. */
1650 if (!MAXIMIZED(win
))
1651 load_float_geom(win
);
1653 store_float_geom(win
);
1656 if (MAXIMIZED(win
)) {
1657 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
1658 ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1662 ws
->focus_pending
= win
;
1666 update_window_color(win
);
1670 if (changed
& EWMH_F_FULLSCREEN
) {
1671 if (FULLSCREEN(win
)) {
1672 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
1676 ws
->focus_pending
= win
;
1679 load_float_geom(win
);
1682 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1686 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: done.\n");
1690 ewmh_update_wm_state(struct ws_win
*win
) {
1691 xcb_atom_t vals
[SWM_EWMH_ACTION_COUNT_MAX
];
1695 vals
[n
++] = ewmh
[_NET_WM_STATE_HIDDEN
].atom
;
1696 if (FULLSCREEN(win
))
1697 vals
[n
++] = ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
;
1698 if (MAXIMIZED_VERT(win
))
1699 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
;
1700 if (MAXIMIZED_HORZ(win
))
1701 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
;
1702 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1703 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
;
1704 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1705 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
;
1706 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1707 vals
[n
++] = ewmh
[_NET_WM_STATE_ABOVE
].atom
;
1708 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1709 vals
[n
++] = ewmh
[_SWM_WM_STATE_MANUAL
].atom
;
1712 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1713 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, n
, vals
);
1715 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1719 ewmh_get_wm_state(struct ws_win
*win
)
1722 xcb_get_property_cookie_t c
;
1723 xcb_get_property_reply_t
*r
;
1729 win
->ewmh_flags
= 0;
1731 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1732 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1733 r
= xcb_get_property_reply(conn
, c
, NULL
);
1737 states
= xcb_get_property_value(r
);
1738 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1740 for (i
= 0; i
< n
; i
++)
1741 ewmh_change_wm_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1749 dumpwins(struct swm_region
*r
, union arg
*args
)
1753 xcb_get_window_attributes_cookie_t c
;
1754 xcb_get_window_attributes_reply_t
*wa
;
1756 /* suppress unused warning since var is needed */
1759 if (r
->ws
== NULL
) {
1760 DPRINTF("dumpwins: invalid workspace\n");
1764 DPRINTF("=== managed window list ws %02d ===\n", r
->ws
->idx
);
1765 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
) {
1766 state
= get_win_state(w
->id
);
1767 c
= xcb_get_window_attributes(conn
, w
->id
);
1768 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1770 DPRINTF("win %#x, map_state: %d, state: %u, "
1771 "transient: %#x\n", w
->id
, wa
->map_state
,
1772 state
, w
->transient
);
1775 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1779 DPRINTF("=== stacking order (top down) === \n");
1780 TAILQ_FOREACH(w
, &r
->ws
->stack
, stack_entry
) {
1781 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
1782 "iconic: %s\n", w
->id
, YESNO(FULLSCREEN(w
)),
1783 YESNO(MAXIMIZED(w
)), YESNO(ABOVE(w
)), YESNO(ICONIC(w
)));
1786 DPRINTF("===== unmanaged window list =====\n");
1787 TAILQ_FOREACH(w
, &r
->ws
->unmanagedlist
, entry
) {
1788 state
= get_win_state(w
->id
);
1789 c
= xcb_get_window_attributes(conn
, w
->id
);
1790 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1792 DPRINTF("win %#x, map_state: %d, state: %u, "
1793 "transient: %#x\n", w
->id
, wa
->map_state
,
1794 state
, w
->transient
);
1797 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1801 DPRINTF("=================================\n");
1805 dumpwins(struct swm_region
*r
, union arg
*s
)
1810 #endif /* SWM_DEBUG */
1815 int saved_errno
, status
;
1818 saved_errno
= errno
;
1822 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1827 if (errno
!= ECHILD
)
1828 warn("sighdlr: waitpid");
1829 #endif /* SWM_DEBUG */
1832 if (pid
== searchpid
)
1836 if (WIFEXITED(status
)) {
1837 if (WEXITSTATUS(status
) != 0)
1838 warnx("sighdlr: child exit status: %d",
1839 WEXITSTATUS(status
));
1841 warnx("sighdlr: child is terminated "
1843 #endif /* SWM_DEBUG */
1857 errno
= saved_errno
;
1863 struct pid_e
*p
= NULL
;
1865 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1870 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1879 name_to_pixel(int sidx
, const char *colorname
)
1881 uint32_t result
= 0;
1882 char cname
[32] = "#";
1883 xcb_screen_t
*screen
;
1884 xcb_colormap_t cmap
;
1885 xcb_alloc_color_reply_t
*cr
;
1886 xcb_alloc_named_color_reply_t
*nr
;
1887 uint16_t rr
, gg
, bb
;
1889 screen
= get_screen(sidx
);
1890 cmap
= screen
->default_colormap
;
1892 /* color is in format rgb://rr/gg/bb */
1893 if (strncmp(colorname
, "rgb:", 4) == 0) {
1894 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1895 warnx("could not parse rgb %s", colorname
);
1897 cr
= xcb_alloc_color_reply(conn
,
1898 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1904 warnx("color '%s' not found", colorname
);
1907 nr
= xcb_alloc_named_color_reply(conn
,
1908 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1911 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1912 nr
= xcb_alloc_named_color_reply(conn
,
1913 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1920 warnx("color '%s' not found", colorname
);
1927 setscreencolor(const char *val
, int i
, int c
)
1929 if (i
< 0 || i
>= get_screen_count())
1932 screens
[i
].c
[c
].pixel
= name_to_pixel(i
, val
);
1933 free(screens
[i
].c
[c
].name
);
1934 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1939 fancy_stacker(struct workspace
*ws
)
1941 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1942 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1943 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1944 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1945 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1946 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1947 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1948 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1949 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1953 plain_stacker(struct workspace
*ws
)
1955 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1956 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1957 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1958 sizeof ws
->stacker
);
1959 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1960 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1961 sizeof ws
->stacker
);
1965 custom_region(const char *val
)
1967 unsigned int x
, y
, w
, h
;
1968 int sidx
, num_screens
;
1969 xcb_screen_t
*screen
;
1971 num_screens
= get_screen_count();
1972 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1973 errx(1, "invalid custom region, "
1974 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1975 if (sidx
< 1 || sidx
> num_screens
)
1976 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1980 if ((screen
= get_screen(sidx
)) == NULL
)
1981 errx(1, "ERROR: can't get screen %d.", sidx
);
1984 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1986 if (x
> screen
->width_in_pixels
||
1987 y
> screen
->height_in_pixels
||
1988 w
+ x
> screen
->width_in_pixels
||
1989 h
+ y
> screen
->height_in_pixels
) {
1990 warnx("ignoring region %ux%u+%u+%u - not within screen "
1991 "boundaries (%ux%u)", w
, h
, x
, y
,
1992 screen
->width_in_pixels
, screen
->height_in_pixels
);
1996 new_region(&screens
[sidx
], x
, y
, w
, h
);
2000 socket_setnonblock(int fd
)
2004 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
2005 err(1, "fcntl F_GETFL");
2006 flags
|= O_NONBLOCK
;
2007 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
2008 err(1, "fcntl F_SETFL");
2012 bar_print_legacy(struct swm_region
*r
, const char *s
)
2014 xcb_rectangle_t rect
;
2019 XRectangle ibox
, lbox
;
2023 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
2025 switch (bar_justify
) {
2026 case SWM_BAR_JUSTIFY_LEFT
:
2029 case SWM_BAR_JUSTIFY_CENTER
:
2030 x
= (WIDTH(r
) - lbox
.width
) / 2;
2032 case SWM_BAR_JUSTIFY_RIGHT
:
2033 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
2037 if (x
< SWM_BAR_OFFSET
)
2042 rect
.width
= WIDTH(r
->bar
);
2043 rect
.height
= HEIGHT(r
->bar
);
2045 /* clear back buffer */
2046 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2047 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2048 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2050 /* draw back buffer */
2051 gcvd
.graphics_exposures
= 0;
2052 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
2053 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2054 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
2055 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
2057 XFreeGC(display
, draw
);
2060 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2061 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2065 bar_print(struct swm_region
*r
, const char *s
)
2068 xcb_rectangle_t rect
;
2076 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
2078 switch (bar_justify
) {
2079 case SWM_BAR_JUSTIFY_LEFT
:
2082 case SWM_BAR_JUSTIFY_CENTER
:
2083 x
= (WIDTH(r
) - info
.width
) / 2;
2085 case SWM_BAR_JUSTIFY_RIGHT
:
2086 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
2090 if (x
< SWM_BAR_OFFSET
)
2095 rect
.width
= WIDTH(r
->bar
);
2096 rect
.height
= HEIGHT(r
->bar
);
2098 /* clear back buffer */
2099 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2100 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2101 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2103 /* draw back buffer */
2104 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
2105 DefaultVisual(display
, r
->s
->idx
),
2106 DefaultColormap(display
, r
->s
->idx
));
2108 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
2109 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
2112 XftDrawDestroy(draw
);
2115 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2116 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2120 bar_extra_stop(void)
2124 bzero(bar_pipe
, sizeof bar_pipe
);
2127 kill(bar_pid
, SIGTERM
);
2130 strlcpy(bar_ext
, "", sizeof bar_ext
);
2135 bar_window_class(char *s
, size_t sz
, struct swm_region
*r
)
2137 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2139 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
2140 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
2144 bar_window_instance(char *s
, size_t sz
, struct swm_region
*r
)
2146 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2148 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
2149 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
2153 bar_window_class_instance(char *s
, size_t sz
, struct swm_region
*r
)
2155 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2158 bar_window_class(s
, sz
, r
);
2159 strlcat(s
, ":", sz
);
2160 bar_window_instance(s
, sz
, r
);
2164 bar_window_state(char *s
, size_t sz
, struct swm_region
*r
)
2166 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2168 if (MAXIMIZED(r
->ws
->focus
))
2169 strlcat(s
, "(m)", sz
);
2170 else if (ABOVE(r
->ws
->focus
))
2171 strlcat(s
, "(f)", sz
);
2175 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
2179 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2182 title
= get_win_name(r
->ws
->focus
->id
);
2183 strlcat(s
, title
, sz
);
2188 get_urgent(struct ws_win
*win
)
2190 xcb_icccm_wm_hints_t hints
;
2191 xcb_get_property_cookie_t c
;
2192 bool urgent
= false;
2195 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2196 if (xcb_icccm_get_wm_hints_reply(conn
, c
, &hints
, NULL
))
2197 urgent
= xcb_icccm_wm_hints_get_urgency(&hints
);
2204 bar_urgent(char *s
, size_t sz
)
2207 int i
, j
, num_screens
;
2208 bool urgent
[SWM_WS_MAX
];
2211 for (i
= 0; i
< workspace_limit
; i
++)
2214 num_screens
= get_screen_count();
2215 for (i
= 0; i
< num_screens
; i
++)
2216 for (j
= 0; j
< workspace_limit
; j
++)
2217 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2218 if (get_urgent(win
))
2221 for (i
= 0; i
< workspace_limit
; i
++) {
2223 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2225 } else if (!urgent_collapse
) {
2226 strlcat(s
, "- ", sz
);
2229 if(urgent_collapse
&& s
[0])
2230 s
[strlen(s
) - 1] = 0;
2234 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2236 if (r
== NULL
|| r
->ws
== NULL
)
2238 if (r
->ws
->name
!= NULL
)
2239 strlcat(s
, r
->ws
->name
, sz
);
2242 /* build the default bar format according to the defined enabled options */
2244 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2248 /* if format provided, just copy the buffers */
2249 if (bar_format
!= NULL
) {
2250 strlcpy(fmtnew
, fmtexp
, sz
);
2254 /* reset the output buffer */
2257 strlcat(fmtnew
, "+N:+I ", sz
);
2259 strlcat(fmtnew
, "+S", sz
);
2260 strlcat(fmtnew
, " ", sz
);
2262 /* only show the workspace name if there's actually one */
2263 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2264 strlcat(fmtnew
, "<+D>", sz
);
2266 /* If enabled, only show the iconic count if there are iconic wins. */
2267 if (iconic_enabled
&& r
!= NULL
&& r
->ws
!= NULL
)
2268 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2270 strlcat(fmtnew
, "{+M}", sz
);
2274 strlcat(fmtnew
, "+3<", sz
);
2276 if (clock_enabled
) {
2277 strlcat(fmtnew
, fmtexp
, sz
);
2278 strlcat(fmtnew
, "+4<", sz
);
2281 /* bar_urgent already adds the space before the last asterisk */
2283 strlcat(fmtnew
, "* +U*+4<", sz
);
2285 if (window_class_enabled
) {
2286 strlcat(fmtnew
, "+C", sz
);
2287 if (!window_instance_enabled
)
2288 strlcat(fmtnew
, "+4<", sz
);
2291 /* checks needed by the colon and floating strlcat(3) calls below */
2292 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2293 if (window_instance_enabled
) {
2294 if (window_class_enabled
)
2295 strlcat(fmtnew
, ":", sz
);
2296 strlcat(fmtnew
, "+T+4<", sz
);
2298 if (window_name_enabled
) {
2299 if (ABOVE(r
->ws
->focus
) || MAXIMIZED(r
->ws
->focus
))
2300 strlcat(fmtnew
, "+F ", sz
);
2301 strlcat(fmtnew
, "+64W ", sz
);
2305 /* finally add the action script output and the version */
2306 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2310 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2312 /* special case; no limit given, pad one space, instead */
2313 if (*limit
== (int)sz
- 1)
2315 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2318 /* replaces the bar format character sequences (like in tmux(1)) */
2320 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2325 char tmp
[SWM_BAR_MAX
];
2326 int limit
, size
, count
;
2329 /* reset strlcat(3) buffer */
2332 /* get number, if any */
2335 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2336 limit
= sizeof tmp
- 1;
2337 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2338 limit
= sizeof tmp
- 1;
2340 /* there is nothing to replace (ie EOL) */
2347 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2350 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2353 bar_window_class(tmp
, sizeof tmp
, r
);
2356 bar_workspace_name(tmp
, sizeof tmp
, r
);
2359 bar_window_state(tmp
, sizeof tmp
, r
);
2362 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2366 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2370 snprintf(tmp
, sizeof tmp
, "%d", count
);
2373 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2376 bar_window_class_instance(tmp
, sizeof tmp
, r
);
2379 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2382 bar_window_instance(tmp
, sizeof tmp
, r
);
2385 bar_urgent(tmp
, sizeof tmp
);
2388 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2391 bar_window_name(tmp
, sizeof tmp
, r
);
2394 /* unknown character sequence; copy as-is */
2395 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2401 if ((int)len
< limit
)
2403 while (limit
-- > 0) {
2404 if (*offrep
>= sz
- 1)
2406 fmtrep
[(*offrep
)++] = *ptr
++;
2414 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2419 while (*fmt
!= '\0') {
2421 /* skip ordinary characters */
2424 fmtrep
[off
++] = *fmt
++;
2428 /* character sequence found; replace it */
2429 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2438 bar_fmt_expand(char *fmtexp
, size_t sz
)
2445 /* start by grabbing the current time and date */
2447 localtime_r(&tmt
, &tm
);
2449 /* figure out what to expand */
2450 if (bar_format
!= NULL
)
2452 else if (bar_format
== NULL
&& clock_enabled
)
2454 /* if nothing to expand bail out */
2460 /* copy as-is, just in case the format shouldn't be expanded below */
2461 strlcpy(fmtexp
, fmt
, sz
);
2462 /* finally pass the string through strftime(3) */
2463 #ifndef SWM_DENY_CLOCK_FORMAT
2464 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2465 warnx("format too long");
2470 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2474 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2475 char fmtrep
[SWM_BAR_MAX
];
2477 struct swm_region
*r
;
2479 /* expand the format by first passing it through strftime(3) */
2480 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2482 num_screens
= get_screen_count();
2483 for (i
= 0; i
< num_screens
; i
++) {
2484 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2488 if (bar_enabled
&& r
->ws
->bar_enabled
)
2489 xcb_map_window(conn
, r
->bar
->id
);
2491 xcb_unmap_window(conn
, r
->bar
->id
);
2495 if (startup_exception
)
2496 snprintf(fmtrep
, sizeof fmtrep
, "total "
2497 "exceptions: %d, first exception: %s",
2501 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2502 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2504 if (bar_font_legacy
)
2505 bar_print_legacy(r
, fmtrep
);
2507 bar_print(r
, fmtrep
);
2513 * Reads external script output; call when stdin is readable.
2514 * Returns 1 if bar_ext was updated; otherwise 0.
2517 bar_extra_update(void)
2520 char b
[SWM_BAR_MAX
];
2521 bool changed
= false;
2526 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2529 if (b
[len
- 1] == '\n') {
2530 /* Remove newline. */
2533 /* "Clear" bar_ext. */
2536 /* Flush buffered output. */
2537 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2538 bar_ext_buf
[0] = '\0';
2540 /* Append new output to bar. */
2541 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2545 /* Buffer output. */
2546 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2551 if (errno
!= EAGAIN
) {
2552 warn("bar_action failed");
2561 bar_toggle(struct swm_region
*r
, union arg
*args
)
2563 struct swm_region
*tmpr
;
2566 /* suppress unused warnings since vars are needed */
2570 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2573 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2574 /* Only change if master switch is enabled. */
2576 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2578 bar_enabled
= r
->ws
->bar_enabled
= true;
2580 case SWM_ARG_ID_BAR_TOGGLE
:
2581 bar_enabled
= !bar_enabled
;
2585 /* update bars as necessary */
2586 num_screens
= get_screen_count();
2587 for (i
= 0; i
< num_screens
; i
++)
2588 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2590 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2591 xcb_map_window(conn
, tmpr
->bar
->id
);
2593 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2598 /* must be after stack */
2605 bar_extra_setup(void)
2607 /* do this here because the conf file is in memory */
2608 if (!bar_extra
&& bar_argv
[0]) {
2609 /* launch external status app */
2611 if (pipe(bar_pipe
) == -1)
2612 err(1, "pipe error");
2613 socket_setnonblock(bar_pipe
[0]);
2614 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2616 /* Set stdin to read from the pipe. */
2617 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2620 /* Set stdout to write to the pipe. */
2621 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2624 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2625 err(1, "could not disable SIGPIPE");
2626 switch (bar_pid
= fork()) {
2628 err(1, "cannot fork");
2632 execvp(bar_argv
[0], bar_argv
);
2633 err(1, "%s external app failed", bar_argv
[0]);
2635 default: /* parent */
2640 atexit(kill_bar_extra_atexit
);
2645 kill_bar_extra_atexit(void)
2648 kill(bar_pid
, SIGTERM
);
2656 while ((s
= index(s
, '-'))) {
2661 return (count
== 14);
2667 char *default_string
;
2668 char **missing_charsets
;
2669 int num_missing_charsets
= 0;
2673 XFreeFontSet(display
, bar_fs
);
2677 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n", bar_fonts
);
2679 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2680 &num_missing_charsets
, &default_string
);
2682 if (num_missing_charsets
> 0) {
2683 warnx("Unable to load charset(s):");
2685 for (i
= 0; i
< num_missing_charsets
; ++i
)
2686 warnx("%s", missing_charsets
[i
]);
2688 XFreeStringList(missing_charsets
);
2690 if (strcmp(default_string
, ""))
2691 warnx("Glyphs from those sets will be replaced "
2692 "by '%s'.", default_string
);
2694 warnx("Glyphs from those sets won't be drawn.");
2698 errx(1, "Error creating font set structure.");
2700 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2702 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2703 2 * bar_border_width
;
2710 xft_init(struct swm_region
*r
)
2712 char *font
, *str
, *search
;
2715 if (bar_font
== NULL
) {
2716 if ((search
= str
= strdup(bar_fonts
)) == NULL
)
2717 errx(1, "insufficient memory.");
2719 while ((font
= strsep(&search
, ",")) != NULL
) {
2723 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2726 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2729 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2733 if (bar_font
== NULL
) {
2734 warnx("unable to load font %s", font
);
2737 DNPRINTF(SWM_D_INIT
, "successfully opened "
2745 if (bar_font
== NULL
)
2746 errx(1, "unable to open a font");
2748 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2750 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2751 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2752 warn("Xft error: unable to allocate color.");
2754 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR
].pixel
, color
);
2756 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2757 DefaultColormap(display
, r
->s
->idx
), &color
, &search_font_color
))
2758 warn("Xft error: unable to allocate color.");
2760 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2767 bar_setup(struct swm_region
*r
)
2769 xcb_screen_t
*screen
;
2772 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2775 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2776 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2781 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2782 err(1, "bar_setup: calloc: failed to allocate memory.");
2784 if (bar_font_legacy
)
2790 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2791 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2792 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2794 /* Assume region is unfocused when we create the bar. */
2795 r
->bar
->id
= xcb_generate_id(conn
);
2796 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2797 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
2798 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
2799 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
2801 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2802 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2803 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
2804 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
2805 | XCB_CW_EVENT_MASK
, wa
);
2807 /* Stack bar window above region window to start. */
2809 wa
[1] = XCB_STACK_MODE_ABOVE
;
2811 xcb_configure_window(conn
, r
->bar
->id
, XCB_CONFIG_WINDOW_SIBLING
|
2812 XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
2814 r
->bar
->buffer
= xcb_generate_id(conn
);
2815 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
2816 WIDTH(r
->bar
), HEIGHT(r
->bar
));
2819 xcb_randr_select_input(conn
, r
->bar
->id
,
2820 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
2823 xcb_map_window(conn
, r
->bar
->id
);
2825 DNPRINTF(SWM_D_BAR
, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
2826 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
2833 bar_cleanup(struct swm_region
*r
)
2837 xcb_destroy_window(conn
, r
->bar
->id
);
2838 xcb_free_pixmap(conn
, r
->bar
->buffer
);
2844 set_win_state(struct ws_win
*win
, uint8_t state
)
2846 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
2848 DNPRINTF(SWM_D_EVENT
, "set_win_state: win %#x, state: %u\n",
2854 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
2855 a_state
, 32, 2, data
);
2859 get_win_state(xcb_window_t w
)
2861 xcb_get_property_reply_t
*r
;
2862 xcb_get_property_cookie_t c
;
2863 uint32_t result
= 0;
2865 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
2866 r
= xcb_get_property_reply(conn
, c
, NULL
);
2868 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
2869 result
= *((uint32_t *)xcb_get_property_value(r
));
2873 DNPRINTF(SWM_D_MISC
, "get_win_state property: win %#x state %u\n", w
,
2879 version(struct swm_region
*r
, union arg
*args
)
2881 /* suppress unused warnings since vars are needed */
2885 bar_version
= !bar_version
;
2887 snprintf(bar_vertext
, sizeof bar_vertext
,
2888 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2890 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2897 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
2899 xcb_client_message_event_t ev
;
2907 name
= get_atom_name(a
);
2908 DNPRINTF(SWM_D_EVENT
, "client_msg: win %#x, atom: %s(%u), "
2910 win
->id
, name
, a
, t
);
2914 bzero(&ev
, sizeof ev
);
2915 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2916 ev
.window
= win
->id
;
2919 ev
.data
.data32
[0] = a
;
2920 ev
.data
.data32
[1] = t
;
2922 xcb_send_event(conn
, 0, win
->id
,
2923 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2926 /* synthetic response to a ConfigureRequest when not making a change */
2928 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2930 xcb_configure_notify_event_t ce
;
2935 /* send notification of unchanged state. */
2936 bzero(&ce
, sizeof(ce
));
2937 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2940 ce
.width
= WIDTH(win
);
2941 ce
.height
= HEIGHT(win
);
2942 ce
.override_redirect
= 0;
2947 ce
.window
= win
->id
;
2948 ce
.border_width
= BORDER(win
);
2949 ce
.above_sibling
= XCB_WINDOW_NONE
;
2952 ce
.event
= ev
->window
;
2953 ce
.window
= ev
->window
;
2955 /* make response appear more WM_SIZE_HINTS-compliant */
2956 if (win
->sh
.flags
) {
2957 DNPRINTF(SWM_D_MISC
, "config_win: hints: win %#x,"
2958 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2959 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2960 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2961 SH_INC_W(win
), SH_INC_H(win
));
2966 /* the hint may be set... to 0! */
2967 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2968 ce
.width
= SH_MIN_W(win
);
2969 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2970 ce
.height
= SH_MIN_H(win
);
2975 /* may also be advertized as 0 */
2976 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2977 ce
.width
= SH_MAX_W(win
);
2978 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2979 ce
.height
= SH_MAX_H(win
);
2982 /* resize increment. */
2984 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2985 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2987 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2988 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2992 /* adjust x and y for requested border_width. */
2993 ce
.x
+= BORDER(win
) - ev
->border_width
;
2994 ce
.y
+= BORDER(win
) - ev
->border_width
;
2995 ce
.border_width
= ev
->border_width
;
2996 ce
.above_sibling
= ev
->sibling
;
2999 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
3000 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
3001 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
3003 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
3008 count_win(struct workspace
*ws
, bool count_transient
)
3013 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3014 if (!count_transient
&& FLOATING(win
))
3020 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
3026 quit(struct swm_region
*r
, union arg
*args
)
3028 /* suppress unused warnings since vars are needed */
3032 DNPRINTF(SWM_D_MISC
, "quit\n");
3037 lower_window(struct ws_win
*win
)
3039 struct ws_win
*target
= NULL
;
3040 struct workspace
*ws
;
3042 DNPRINTF(SWM_D_EVENT
, "lower_window: win %#x\n", WINID(win
));
3049 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3050 if (target
== win
|| ICONIC(target
))
3052 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3055 if (win
->transient
== target
->transient
)
3057 if (win
->transient
== target
->id
)
3060 if (FULLSCREEN(target
))
3062 if (FULLSCREEN(win
))
3064 if (MAXIMIZED(target
))
3068 if (ABOVE(target
) || TRANS(target
))
3070 if (ABOVE(win
) || TRANS(win
))
3074 /* Change stack position. */
3075 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3077 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3079 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3081 update_win_stacking(win
);
3084 if (swm_debug
& SWM_D_STACK
) {
3085 DPRINTF("=== stacking order (top down) === \n");
3086 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3087 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3088 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3089 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3090 YESNO(ICONIC(target
)));
3094 DNPRINTF(SWM_D_EVENT
, "lower_window: done\n");
3098 raise_window(struct ws_win
*win
)
3100 struct ws_win
*target
= NULL
;
3101 struct workspace
*ws
;
3103 DNPRINTF(SWM_D_EVENT
, "raise_window: win %#x\n", WINID(win
));
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 struct workspace
*ws
;
3499 int i
, j
, num_screens
;
3504 num_screens
= get_screen_count();
3505 for (i
= 0; i
< num_screens
; i
++) {
3506 for (j
= 0; j
< workspace_limit
; j
++) {
3507 ws
= &screens
[i
].ws
[j
];
3509 if (win
== ws
->focus
)
3511 if (win
== ws
->focus_prev
)
3512 ws
->focus_prev
= NULL
;
3515 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3516 if (win
== w
->focus_child
)
3517 w
->focus_child
= NULL
;
3523 validate_win(struct ws_win
*testwin
)
3526 struct workspace
*ws
;
3527 struct swm_region
*r
;
3528 int i
, x
, num_screens
;
3530 if (testwin
== NULL
)
3533 num_screens
= get_screen_count();
3534 for (i
= 0; i
< num_screens
; i
++)
3535 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3536 for (x
= 0; x
< workspace_limit
; x
++) {
3538 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3546 validate_ws(struct workspace
*testws
)
3548 struct swm_region
*r
;
3549 struct workspace
*ws
;
3550 int i
, x
, num_screens
;
3552 /* validate all ws */
3553 num_screens
= get_screen_count();
3554 for (i
= 0; i
< num_screens
; i
++)
3555 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3556 for (x
= 0; x
< workspace_limit
; x
++) {
3565 unfocus_win(struct ws_win
*win
)
3567 xcb_window_t none
= XCB_WINDOW_NONE
;
3569 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: win %#x\n", WINID(win
));
3574 if (win
->ws
== NULL
) {
3575 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3579 if (validate_ws(win
->ws
)) {
3580 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3584 if (win
->ws
->r
== NULL
) {
3585 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3589 if (validate_win(win
)) {
3590 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3595 if (win
->ws
->focus
== win
) {
3596 win
->ws
->focus
= NULL
;
3597 win
->ws
->focus_prev
= win
;
3600 if (validate_win(win
->ws
->focus
)) {
3601 kill_refs(win
->ws
->focus
);
3602 win
->ws
->focus
= NULL
;
3605 if (validate_win(win
->ws
->focus_prev
)) {
3606 kill_refs(win
->ws
->focus_prev
);
3607 win
->ws
->focus_prev
= NULL
;
3610 update_window_color(win
);
3612 /* Raise window to "top unfocused position." */
3613 if (win
->ws
->always_raise
)
3616 /* Update border width */
3617 if (win
->bordered
&& (win
->quirks
& SWM_Q_MINIMALBORDER
) &&
3620 X(win
) += border_width
;
3621 Y(win
) += border_width
;
3625 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3626 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3628 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3632 focus_win(struct ws_win
*win
)
3634 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
, *tmpw
;
3635 struct workspace
*ws
;
3636 xcb_get_input_focus_reply_t
*gifr
;
3638 DNPRINTF(SWM_D_FOCUS
, "focus_win: win %#x\n", WINID(win
));
3640 if (win
== NULL
|| win
->ws
== NULL
|| !win
->mapped
)
3645 if (validate_ws(ws
))
3648 if (validate_win(win
)) {
3653 gifr
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3655 cfw
= find_window(gifr
->focus
);
3656 if (cfw
!= NULL
&& cfw
!= win
) {
3657 if (cfw
->ws
!= ws
&& cfw
->ws
->r
!= NULL
) {
3658 /* Change border to unfocused color. */
3659 xcb_change_window_attributes(conn
, cfw
->id
,
3660 XCB_CW_BORDER_PIXEL
,
3661 &cfw
->s
->c
[(MAXIMIZED(cfw
) ?
3662 SWM_S_COLOR_UNFOCUS_MAXIMIZED
:
3663 SWM_S_COLOR_UNFOCUS
)].pixel
);
3665 /* Update border width */
3666 if (cfw
->bordered
&&
3667 (cfw
->quirks
& SWM_Q_MINIMALBORDER
) &&
3670 X(cfw
) += border_width
;
3671 Y(cfw
) += border_width
;
3681 if (ws
->focus
!= win
) {
3682 if (ws
->focus
&& ws
->focus
!= cfw
)
3683 unfocus_win(ws
->focus
);
3687 /* Clear focus child redirect. */
3688 win
->focus_child
= NULL
;
3690 /* If transient, adjust parent's focus child for focus_magic. */
3692 parent
= find_window(win
->transient
);
3693 if (parent
&& parent
->focus_child
!= win
)
3694 parent
->focus_child
= win
;
3697 if (cfw
!= win
&& ws
->r
!= NULL
) {
3698 /* Set input focus if no input hint, or indicated by hint. */
3699 if (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
3700 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
3702 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3703 win
->id
, last_event_time
);
3705 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3706 ws
->r
->id
, XCB_CURRENT_TIME
);
3708 /* Tell app it can adjust focus to a specific window. */
3709 if (win
->take_focus
) {
3710 /* java is special; always tell parent */
3711 if (TRANS(win
) && win
->java
)
3712 client_msg(parent
, a_takefocus
,
3715 client_msg(win
, a_takefocus
, last_event_time
);
3718 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
3720 /* If a parent exists, map it first. */
3722 raise_window(parent
);
3725 /* Map siblings next. */
3726 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
,
3728 if (w
!= win
&& !ICONIC(w
) &&
3729 w
->transient
== parent
->id
) {
3735 /* Map focused window. */
3739 /* Stack any children of focus window. */
3740 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
3741 if (w
->transient
== win
->id
&& !ICONIC(w
)) {
3745 } else if (tile_gap
< 0 && !ABOVE(win
)) {
3747 * Windows overlap in the layout.
3748 * Raise focused win above all tiled wins.
3756 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3757 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3762 /* Update window border even if workspace is hidden. */
3763 update_window_color(win
);
3765 /* Update border width */
3766 if (!win
->bordered
&& WS_FOCUSED(win
->ws
) &&
3767 (win
->quirks
& SWM_Q_MINIMALBORDER
) && FLOATING(win
)) {
3769 X(win
) -= border_width
;
3770 Y(win
) -= border_width
;
3778 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
3781 /* If a child window should have focus instead, return it. */
3783 get_focus_magic(struct ws_win
*win
)
3785 struct ws_win
*parent
= NULL
;
3786 struct ws_win
*child
= NULL
;
3788 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: win %#x\n", WINID(win
));
3793 parent
= find_window(win
->transient
);
3795 /* If parent prefers focus elsewhere, then try to do so. */
3796 if (parent
&& (child
= parent
->focus_child
)) {
3797 if (validate_win(child
) == 0 && child
->mapped
)
3800 parent
->focus_child
= NULL
;
3804 /* If this window prefers focus elsewhere, then try to do so. */
3805 if ((child
= win
->focus_child
)) {
3806 if (validate_win(child
) == 0 && child
->mapped
)
3809 win
->focus_child
= NULL
;
3816 event_drain(uint8_t rt
)
3818 xcb_generic_event_t
*evt
;
3820 /* ensure all pending requests have been processed before filtering. */
3822 while ((evt
= xcb_poll_for_event(conn
))) {
3823 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
3831 set_region(struct swm_region
*r
)
3833 struct swm_region
*rf
;
3840 /* Unfocus old region bar. */
3845 xcb_change_window_attributes(conn
, rf
->bar
->id
,
3846 XCB_CW_BORDER_PIXEL
,
3847 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
3850 if (rf
!= NULL
&& rf
!= r
&& (X(rf
) != X(r
) || Y(rf
) != Y(r
) ||
3851 WIDTH(rf
) != WIDTH(r
) || HEIGHT(rf
) != HEIGHT(r
))) {
3852 /* Set _NET_DESKTOP_GEOMETRY. */
3854 vals
[1] = HEIGHT(r
);
3855 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, r
->s
->root
,
3856 ewmh
[_NET_DESKTOP_GEOMETRY
].atom
, XCB_ATOM_CARDINAL
, 32, 2,
3860 /* Set region bar border to focus_color. */
3861 xcb_change_window_attributes(conn
, r
->bar
->id
,
3862 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
3866 ewmh_update_current_desktop();
3870 focus_region(struct swm_region
*r
)
3873 struct swm_region
*old_r
;
3878 old_r
= r
->s
->r_focus
;
3881 nfw
= get_region_focus(r
);
3885 /* New region is empty; need to manually unfocus win. */
3887 unfocus_win(old_r
->ws
->focus
);
3889 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
3892 /* Clear bar since empty. */
3898 switchws(struct swm_region
*r
, union arg
*args
)
3900 struct swm_region
*this_r
, *other_r
;
3902 struct workspace
*new_ws
, *old_ws
;
3903 xcb_window_t none
= XCB_WINDOW_NONE
;
3904 int wsid
= args
->id
;
3905 bool unmap_old
= false;
3910 if (wsid
>= workspace_limit
)
3914 old_ws
= this_r
->ws
;
3915 new_ws
= &this_r
->s
->ws
[wsid
];
3917 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3918 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
3920 if (new_ws
== NULL
|| old_ws
== NULL
)
3922 if (new_ws
== old_ws
)
3925 other_r
= new_ws
->r
;
3926 if (other_r
&& workspace_clamp
) {
3927 DNPRINTF(SWM_D_WS
, "switchws: ws clamped.\n");
3930 DNPRINTF(SWM_D_WS
, "switchws: warping focus to region "
3931 "with ws %d.\n", wsid
);
3932 focus_region(other_r
);
3933 center_pointer(other_r
);
3939 if ((win
= old_ws
->focus
) != NULL
) {
3940 update_window_color(win
);
3942 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3943 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3948 /* the other ws is visible in another region, exchange them */
3949 other_r
->ws_prior
= new_ws
;
3950 other_r
->ws
= old_ws
;
3951 old_ws
->r
= other_r
;
3953 /* the other workspace is hidden, hide this one */
3958 this_r
->ws_prior
= old_ws
;
3959 this_r
->ws
= new_ws
;
3962 /* Set focus_pending before stacking, if needed. */
3963 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
3964 validate_win(new_ws
->focus_pending
))) {
3965 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
3966 new_ws
->focus
= new_ws
->focus_prev
;
3967 new_ws
->focus_prev
= NULL
;
3970 new_ws
->state
= SWM_WS_STATE_MAPPING
;
3973 /* unmap old windows */
3975 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
3977 old_ws
->state
= SWM_WS_STATE_HIDDEN
;
3980 /* if workspaces were swapped, then don't wait to set focus */
3981 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
3982 if (new_ws
->focus_pending
) {
3983 focus_win(new_ws
->focus_pending
);
3984 new_ws
->focus_pending
= NULL
;
3988 /* Clear bar and set focus on region input win if new ws is empty. */
3989 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
3990 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
3995 ewmh_update_current_desktop();
3999 new_ws
->state
= SWM_WS_STATE_MAPPED
;
4001 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
4005 cyclews(struct swm_region
*r
, union arg
*args
)
4008 struct swm_screen
*s
= r
->s
;
4009 bool cycle_all
= false, mv
= false;
4011 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4012 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4018 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
4021 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
4024 case SWM_ARG_ID_CYCLEWS_UP
:
4025 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
4027 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
4030 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
4033 case SWM_ARG_ID_CYCLEWS_DOWN
:
4034 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
4041 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
4043 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
4050 } while (a
.id
!= r
->ws
->idx
);
4052 DNPRINTF(SWM_D_FOCUS
, "cyclews: done\n");
4056 priorws(struct swm_region
*r
, union arg
*args
)
4062 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4063 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4065 if (r
->ws_prior
== NULL
)
4068 a
.id
= r
->ws_prior
->idx
;
4070 DNPRINTF(SWM_D_FOCUS
, "priorws: done\n");
4074 focusrg(struct swm_region
*r
, union arg
*args
)
4076 int ridx
= args
->id
, i
, num_screens
;
4077 struct swm_region
*rr
= NULL
;
4079 num_screens
= get_screen_count();
4080 /* do nothing if we don't have more than one screen */
4081 if (!(num_screens
> 1 || outputs
> 1))
4084 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
4086 rr
= TAILQ_FIRST(&r
->s
->rl
);
4087 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
4088 rr
= TAILQ_NEXT(rr
, entry
);
4096 DNPRINTF(SWM_D_FOCUS
, "focusrg: done\n");
4100 cyclerg(struct swm_region
*r
, union arg
*args
)
4103 struct swm_region
*rr
= NULL
;
4106 num_screens
= get_screen_count();
4107 /* do nothing if we don't have more than one screen */
4108 if (!(num_screens
> 1 || outputs
> 1))
4112 DNPRINTF(SWM_D_FOCUS
, "cyclerg: id: %d, region: %d\n", args
->id
, i
);
4115 case SWM_ARG_ID_CYCLERG_UP
:
4116 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4117 rr
= TAILQ_NEXT(r
, entry
);
4119 rr
= TAILQ_FIRST(&screens
[i
].rl
);
4121 case SWM_ARG_ID_CYCLERG_DOWN
:
4122 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4123 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
4125 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
4134 case SWM_ARG_ID_CYCLERG_UP
:
4135 case SWM_ARG_ID_CYCLERG_DOWN
:
4140 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4141 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4149 DNPRINTF(SWM_D_FOCUS
, "cyclerg: done\n");
4152 /* Sorts transients after parent. */
4154 sort_windows(struct ws_win_list
*wl
)
4156 struct ws_win
*win
, *parent
, *nxt
;
4161 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
4162 nxt
= TAILQ_NEXT(win
, entry
);
4164 parent
= find_window(win
->transient
);
4165 if (parent
== NULL
) {
4166 warnx("not possible bug");
4169 TAILQ_REMOVE(wl
, win
, entry
);
4170 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
4176 swapwin(struct swm_region
*r
, union arg
*args
)
4178 struct ws_win
*target
, *source
;
4179 struct ws_win
*cur_focus
;
4180 struct ws_win_list
*wl
;
4182 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4183 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4185 cur_focus
= r
->ws
->focus
;
4186 if (cur_focus
== NULL
|| FULLSCREEN(cur_focus
))
4189 /* Adjust stacking in floating layer. */
4190 if (ABOVE(cur_focus
)) {
4192 case SWM_ARG_ID_SWAPPREV
:
4193 target
= TAILQ_PREV(cur_focus
, ws_win_stack
,
4195 if (target
!= NULL
&& FLOATING(target
)) {
4196 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4198 TAILQ_INSERT_BEFORE(target
, cur_focus
,
4200 update_win_stacking(cur_focus
);
4204 case SWM_ARG_ID_SWAPNEXT
:
4205 target
= TAILQ_NEXT(cur_focus
, stack_entry
);
4206 if (target
!= NULL
&& FLOATING(target
)) {
4207 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4209 TAILQ_INSERT_AFTER(&cur_focus
->ws
->stack
,
4210 target
, cur_focus
, stack_entry
);
4211 update_win_stacking(cur_focus
);
4219 if (r
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4222 clear_maximized(r
->ws
);
4225 wl
= &source
->ws
->winlist
;
4228 case SWM_ARG_ID_SWAPPREV
:
4230 source
= find_window(source
->transient
);
4231 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
4232 if (target
&& target
->transient
)
4233 target
= find_window(target
->transient
);
4234 TAILQ_REMOVE(wl
, source
, entry
);
4236 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4238 TAILQ_INSERT_BEFORE(target
, source
, entry
);
4240 case SWM_ARG_ID_SWAPNEXT
:
4241 target
= TAILQ_NEXT(source
, entry
);
4242 /* move the parent and let the sort handle the move */
4244 source
= find_window(source
->transient
);
4245 TAILQ_REMOVE(wl
, source
, entry
);
4247 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4249 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
4251 case SWM_ARG_ID_SWAPMAIN
:
4252 target
= TAILQ_FIRST(wl
);
4253 if (target
== source
) {
4254 if (source
->ws
->focus_prev
!= NULL
&&
4255 source
->ws
->focus_prev
!= target
)
4256 source
= source
->ws
->focus_prev
;
4260 if (target
== NULL
|| source
== NULL
)
4262 source
->ws
->focus_prev
= target
;
4263 TAILQ_REMOVE(wl
, target
, entry
);
4264 TAILQ_INSERT_BEFORE(source
, target
, entry
);
4265 TAILQ_REMOVE(wl
, source
, entry
);
4266 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4268 case SWM_ARG_ID_MOVELAST
:
4269 TAILQ_REMOVE(wl
, source
, entry
);
4270 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4273 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
4278 ewmh_update_client_list();
4284 DNPRINTF(SWM_D_MOVE
, "swapwin: done\n");
4288 get_focus_prev(struct ws_win
*win
)
4290 struct ws_win
*winfocus
= NULL
;
4291 struct ws_win
*cur_focus
= NULL
;
4292 struct ws_win_list
*wl
= NULL
;
4293 struct workspace
*ws
= NULL
;
4295 if (!(win
&& win
->ws
))
4300 cur_focus
= ws
->focus
;
4302 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: win %#x, cur_focus: %#x, "
4303 "focus_prev: %#x\n", WINID(win
), WINID(cur_focus
),
4304 WINID(ws
->focus_prev
));
4306 /* pickle, just focus on whatever */
4307 if (cur_focus
== NULL
) {
4308 /* use prev_focus if valid */
4309 if (ws
->focus_prev
&& find_window(ws
->focus_prev
->id
))
4310 winfocus
= ws
->focus_prev
;
4314 /* if transient focus on parent */
4315 if (TRANS(cur_focus
)) {
4316 winfocus
= find_window(cur_focus
->transient
);
4320 /* if in max_stack try harder */
4321 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
4322 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
4323 if (cur_focus
!= ws
->focus_prev
)
4324 winfocus
= ws
->focus_prev
;
4326 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
4331 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
4333 if (winfocus
== NULL
|| winfocus
== win
) {
4334 switch (focus_close
) {
4335 case SWM_STACK_BOTTOM
:
4336 TAILQ_FOREACH(winfocus
, wl
, entry
)
4337 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4341 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4342 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4345 case SWM_STACK_ABOVE
:
4346 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
4347 while (winfocus
&& ICONIC(winfocus
))
4348 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4350 if (winfocus
== NULL
) {
4351 if (focus_close_wrap
) {
4352 TAILQ_FOREACH(winfocus
, wl
, entry
)
4353 if (!ICONIC(winfocus
) &&
4354 winfocus
!= cur_focus
)
4357 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4359 if (!ICONIC(winfocus
) &&
4360 winfocus
!= cur_focus
)
4365 case SWM_STACK_BELOW
:
4366 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
4367 while (winfocus
&& ICONIC(winfocus
))
4368 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
4371 if (winfocus
== NULL
) {
4372 if (focus_close_wrap
) {
4373 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4375 if (!ICONIC(winfocus
) &&
4376 winfocus
!= cur_focus
)
4379 TAILQ_FOREACH(winfocus
, wl
, entry
)
4380 if (!ICONIC(winfocus
) &&
4381 winfocus
!= cur_focus
)
4389 if (winfocus
== NULL
||
4390 (winfocus
&& (ICONIC(winfocus
) || winfocus
== cur_focus
))) {
4391 if (focus_default
== SWM_STACK_TOP
) {
4392 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4393 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4396 TAILQ_FOREACH(winfocus
, wl
, entry
)
4397 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4404 return get_focus_magic(winfocus
);
4408 get_region_focus(struct swm_region
*r
)
4410 struct ws_win
*winfocus
= NULL
;
4415 if (r
->ws
->focus
&& !ICONIC(r
->ws
->focus
))
4416 winfocus
= r
->ws
->focus
;
4417 else if (r
->ws
->focus_prev
&& !ICONIC(r
->ws
->focus_prev
))
4418 winfocus
= r
->ws
->focus_prev
;
4420 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
4421 if (!ICONIC(winfocus
))
4424 return get_focus_magic(winfocus
);
4428 focus(struct swm_region
*r
, union arg
*args
)
4430 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
4431 struct ws_win_list
*wl
= NULL
;
4432 struct workspace
*ws
= NULL
;
4439 cur_focus
= r
->ws
->focus
;
4443 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d, cur_focus: %#x\n", args
->id
,
4446 /* Make sure an uniconified window has focus, if one exists. */
4447 if (cur_focus
== NULL
) {
4448 cur_focus
= TAILQ_FIRST(wl
);
4449 while (cur_focus
!= NULL
&& ICONIC(cur_focus
))
4450 cur_focus
= TAILQ_NEXT(cur_focus
, entry
);
4452 DNPRINTF(SWM_D_FOCUS
, "focus: new cur_focus: %#x\n",
4457 case SWM_ARG_ID_FOCUSPREV
:
4458 if (cur_focus
== NULL
)
4461 winfocus
= cur_focus
;
4463 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
4464 if (winfocus
== NULL
)
4465 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
4466 if (winfocus
== cur_focus
)
4468 } while (winfocus
&& (ICONIC(winfocus
) ||
4469 winfocus
->id
== cur_focus
->transient
||
4470 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4471 winfocus
->transient
== cur_focus
->transient
) ||
4472 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4474 case SWM_ARG_ID_FOCUSNEXT
:
4475 if (cur_focus
== NULL
)
4478 winfocus
= cur_focus
;
4480 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4481 if (winfocus
== NULL
)
4482 winfocus
= TAILQ_FIRST(wl
);
4483 if (winfocus
== cur_focus
)
4485 } while (winfocus
&& (ICONIC(winfocus
) ||
4486 winfocus
->id
== cur_focus
->transient
||
4487 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4488 winfocus
->transient
== cur_focus
->transient
) ||
4489 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4491 case SWM_ARG_ID_FOCUSMAIN
:
4492 if (cur_focus
== NULL
)
4495 winfocus
= TAILQ_FIRST(wl
);
4496 if (winfocus
== cur_focus
)
4497 winfocus
= cur_focus
->ws
->focus_prev
;
4499 case SWM_ARG_ID_FOCUSURGENT
:
4500 /* Search forward for the next urgent window. */
4504 for (i
= 0; i
<= workspace_limit
; ++i
) {
4506 head
= TAILQ_FIRST(&r
->s
->ws
[(ws
->idx
+ i
) %
4507 workspace_limit
].winlist
);
4510 if (head
== cur_focus
) {
4512 winfocus
= cur_focus
;
4515 } else if (get_urgent(head
)) {
4520 head
= TAILQ_NEXT(head
, entry
);
4527 /* Switch ws if new focus is on a different ws. */
4528 if (winfocus
&& winfocus
->ws
!= ws
) {
4529 a
.id
= winfocus
->ws
->idx
;
4537 if (clear_maximized(ws
) > 0)
4540 focus_win(get_focus_magic(winfocus
));
4545 DNPRINTF(SWM_D_FOCUS
, "focus: done\n");
4549 cycle_layout(struct swm_region
*r
, union arg
*args
)
4551 struct workspace
*ws
= r
->ws
;
4553 /* suppress unused warning since var is needed */
4556 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
4559 if (ws
->cur_layout
->l_stack
== NULL
)
4560 ws
->cur_layout
= &layouts
[0];
4562 clear_maximized(ws
);
4567 focus_win(get_region_focus(r
));
4574 stack_config(struct swm_region
*r
, union arg
*args
)
4576 struct workspace
*ws
= r
->ws
;
4578 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
4581 if (clear_maximized(ws
) > 0)
4584 if (ws
->cur_layout
->l_config
!= NULL
)
4585 ws
->cur_layout
->l_config(ws
, args
->id
);
4587 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
4597 struct swm_geometry g
;
4598 struct swm_region
*r
, *r_prev
= NULL
;
4605 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
4607 num_screens
= get_screen_count();
4608 for (i
= 0; i
< num_screens
; i
++) {
4612 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
4613 /* Adjust stack area for region bar and padding. */
4615 g
.x
+= region_padding
;
4616 g
.y
+= region_padding
;
4617 g
.w
-= 2 * border_width
+ 2 * region_padding
;
4618 g
.h
-= 2 * border_width
+ 2 * region_padding
;
4619 if (bar_enabled
&& r
->ws
->bar_enabled
) {
4625 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d (screen: "
4626 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4627 r
->ws
->idx
, i
, j
++, g
.x
, g
.y
, g
.w
, g
.h
);
4630 /* Stack bar/input relative to prev. region. */
4631 val
[1] = XCB_STACK_MODE_ABOVE
;
4633 val
[0] = r_prev
->id
;
4634 DNPRINTF(SWM_D_STACK
, "stack: region input %#x "
4635 "relative to %#x.\n", r
->id
, val
[0]);
4636 xcb_configure_window(conn
, r
->id
,
4637 XCB_CONFIG_WINDOW_SIBLING
|
4638 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4640 val
[0] = r_prev
->bar
->id
;
4641 DNPRINTF(SWM_D_STACK
, "stack: region bar %#x "
4642 "relative to %#x.\n", r
->bar
->id
, val
[0]);
4643 xcb_configure_window(conn
, r
->bar
->id
,
4644 XCB_CONFIG_WINDOW_SIBLING
|
4645 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4648 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
4649 r
->ws
->cur_layout
->l_string(r
->ws
);
4650 /* save r so we can track region changes */
4658 DNPRINTF(SWM_D_STACK
, "stack: end\n");
4662 store_float_geom(struct ws_win
*win
)
4664 if (win
== NULL
|| win
->ws
->r
== NULL
)
4667 /* retain window geom and region geom */
4668 win
->g_float
= win
->g
;
4669 win
->g_float
.x
-= X(win
->ws
->r
);
4670 win
->g_float
.y
-= Y(win
->ws
->r
);
4671 win
->g_floatvalid
= true;
4672 DNPRINTF(SWM_D_MISC
, "store_float_geom: win %#x, g: (%d,%d)"
4673 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
4674 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
4675 win
->g_float
.w
, win
->g_float
.h
);
4679 load_float_geom(struct ws_win
*win
)
4681 if (win
== NULL
|| win
->ws
->r
== NULL
)
4684 if (win
->g_floatvalid
) {
4685 win
->g
= win
->g_float
;
4686 X(win
) += X(win
->ws
->r
);
4687 Y(win
) += Y(win
->ws
->r
);
4688 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g: (%d,%d)"
4689 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
4692 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g_float "
4693 "is not set.\n", win
->id
);
4698 update_floater(struct ws_win
*win
)
4700 struct workspace
*ws
;
4701 struct swm_region
*r
;
4703 DNPRINTF(SWM_D_MISC
, "update_floater: win %#x\n", WINID(win
));
4710 if ((r
= ws
->r
) == NULL
)
4713 win
->bordered
= true;
4715 if (FULLSCREEN(win
)) {
4716 /* _NET_WM_FULLSCREEN: fullscreen without border. */
4717 if (!win
->g_floatvalid
)
4718 store_float_geom(win
);
4721 win
->bordered
= false;
4722 } else if (MAXIMIZED(win
)) {
4723 /* Maximize: like a single stacked window. */
4724 if (!win
->g_floatvalid
)
4725 store_float_geom(win
);
4729 if (bar_enabled
&& ws
->bar_enabled
&& !maximize_hide_bar
) {
4731 Y(win
) += bar_height
;
4732 HEIGHT(win
) -= bar_height
;
4733 } else if (disable_border
) {
4734 win
->bordered
= false;
4737 if (win
->bordered
) {
4738 HEIGHT(win
) -= 2 * border_width
;
4739 WIDTH(win
) -= 2 * border_width
;
4742 /* Normal floating window. */
4743 /* Update geometry if new region. */
4745 load_float_geom(win
);
4747 if (((win
->quirks
& SWM_Q_FULLSCREEN
) &&
4748 WIDTH(win
) >= WIDTH(r
) && HEIGHT(win
) >= HEIGHT(r
)) ||
4749 ((!WS_FOCUSED(win
->ws
) || win
->ws
->focus
!= win
) &&
4750 (win
->quirks
& SWM_Q_MINIMALBORDER
))) {
4752 win
->bordered
= false;
4753 } else if (!MANUAL(win
)) {
4754 if (TRANS(win
) && (win
->quirks
& SWM_Q_TRANSSZ
)) {
4755 /* Adjust size on TRANSSZ quirk. */
4756 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
4757 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
4760 if (!(win
->quirks
& SWM_Q_ANYWHERE
)) {
4762 * Floaters and transients are auto-centred
4763 * unless manually moved, resized or ANYWHERE
4766 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 -
4768 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 -
4770 store_float_geom(win
);
4775 /* Ensure at least 1 pixel of the window is in the region. */
4776 region_containment(win
, r
, SWM_CW_ALLSIDES
);
4782 * Send keystrokes to terminal to decrease/increase the font size as the
4783 * window size changes.
4786 adjust_font(struct ws_win
*win
)
4788 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
4789 ABOVE(win
) || TRANS(win
))
4792 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
4793 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
4794 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
4795 win
->font_size_boundary
[win
->font_steps
] =
4796 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
4799 win
->last_inc
= win
->sh
.width_inc
;
4800 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
4801 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
4802 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
4805 win
->last_inc
= win
->sh
.width_inc
;
4806 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
4810 #define SWAPXY(g) do { \
4812 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4813 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4816 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, bool flip
)
4818 struct swm_geometry win_g
, r_g
= *g
;
4820 int i
, j
, s
, stacks
;
4821 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
4822 int hrh
, extra
= 0, h_slice
, last_h
= 0;
4823 int split
, colno
, winno
, mwin
, msize
, mscale
;
4824 int remain
, missing
, v_slice
;
4825 bool bordered
= true, reconfigure
= false;
4827 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
4828 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
4830 /* Prepare tiling variables, if needed. */
4831 if ((winno
= count_win(ws
, false)) > 0) {
4832 /* Find first tiled window. */
4833 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
4834 if (!FLOATING(win
) && !ICONIC(win
))
4837 /* Take into account size hints of first tiled window. */
4839 w_inc
= win
->sh
.width_inc
;
4840 w_base
= win
->sh
.base_width
;
4841 mwin
= ws
->l_state
.horizontal_mwin
;
4842 mscale
= ws
->l_state
.horizontal_msize
;
4843 stacks
= ws
->l_state
.horizontal_stacks
;
4846 w_inc
= win
->sh
.height_inc
;
4847 w_base
= win
->sh
.base_height
;
4848 mwin
= ws
->l_state
.vertical_mwin
;
4849 mscale
= ws
->l_state
.vertical_msize
;
4850 stacks
= ws
->l_state
.vertical_stacks
;
4854 if (stacks
> winno
- mwin
)
4855 stacks
= winno
- mwin
;
4859 h_slice
= r_g
.h
/ SWM_H_SLICE
;
4860 if (mwin
&& winno
> mwin
) {
4861 v_slice
= r_g
.w
/ SWM_V_SLICE
;
4865 win_g
.w
= v_slice
* mscale
;
4867 if (w_inc
> 1 && w_inc
< v_slice
) {
4868 /* Adjust for requested size increment. */
4869 remain
= (win_g
.w
- w_base
) % w_inc
;
4875 win_g
.x
+= r_g
.w
- msize
;
4878 colno
= split
= winno
/ stacks
;
4879 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
4880 2 * border_width
) / stacks
);
4882 hrh
= r_g
.h
/ colno
;
4883 extra
= r_g
.h
- (colno
* hrh
);
4884 win_g
.h
= hrh
- 2 * border_width
;
4885 i
= j
= 0, s
= stacks
;
4888 /* Update window geometry. */
4889 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
4893 if (FLOATING(win
)) {
4894 update_floater(win
);
4899 if (split
&& i
== split
) {
4900 colno
= (winno
- mwin
) / stacks
;
4901 if (s
<= (winno
- mwin
) % stacks
)
4904 hrh
= r_g
.h
/ colno
;
4905 extra
= r_g
.h
- (colno
* hrh
);
4908 win_g
.x
+= win_g
.w
+ 2 * border_width
+
4911 win_g
.w
= (r_g
.w
- msize
-
4912 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
4914 win_g
.w
+= (r_g
.w
- msize
-
4915 (stacks
* (2 * border_width
+ tile_gap
))) %
4919 win_g
.x
-= win_g
.w
+ 2 * border_width
+
4925 win_g
.h
= hrh
- 2 * border_width
- tile_gap
;
4928 h_inc
= win
->sh
.width_inc
;
4929 h_base
= win
->sh
.base_width
;
4931 h_inc
= win
->sh
.height_inc
;
4932 h_base
= win
->sh
.base_height
;
4935 if (j
== colno
- 1) {
4936 win_g
.h
= hrh
+ extra
;
4937 } else if (h_inc
> 1 && h_inc
< h_slice
) {
4938 /* adjust for window's requested size increment */
4939 remain
= (win_g
.h
- h_base
) % h_inc
;
4940 missing
= h_inc
- remain
;
4942 if (missing
<= extra
|| j
== 0) {
4954 win_g
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
4956 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
) &&
4959 win_g
.w
+= 2 * border_width
;
4960 win_g
.h
+= 2 * border_width
;
4966 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
4967 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
4971 WIDTH(win
) = win_g
.h
;
4972 HEIGHT(win
) = win_g
.w
;
4975 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
4976 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
4980 WIDTH(win
) = win_g
.w
;
4981 HEIGHT(win
) = win_g
.h
;
4985 if (bordered
!= win
->bordered
) {
4987 win
->bordered
= bordered
;
5000 /* Stack all windows from bottom up. */
5001 TAILQ_FOREACH_REVERSE(win
, &ws
->stack
, ws_win_stack
, stack_entry
)
5003 update_win_stacking(win
);
5005 /* Map all windows from top down. */
5006 TAILQ_FOREACH(win
, &ws
->stack
, stack_entry
)
5010 DNPRINTF(SWM_D_STACK
, "stack_master: done\n");
5014 vertical_config(struct workspace
*ws
, int id
)
5016 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
5020 case SWM_ARG_ID_STACKRESET
:
5021 case SWM_ARG_ID_STACKINIT
:
5022 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
5023 ws
->l_state
.vertical_mwin
= 1;
5024 ws
->l_state
.vertical_stacks
= 1;
5026 case SWM_ARG_ID_MASTERSHRINK
:
5027 if (ws
->l_state
.vertical_msize
> 1)
5028 ws
->l_state
.vertical_msize
--;
5030 case SWM_ARG_ID_MASTERGROW
:
5031 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
5032 ws
->l_state
.vertical_msize
++;
5034 case SWM_ARG_ID_MASTERADD
:
5035 ws
->l_state
.vertical_mwin
++;
5037 case SWM_ARG_ID_MASTERDEL
:
5038 if (ws
->l_state
.vertical_mwin
> 0)
5039 ws
->l_state
.vertical_mwin
--;
5041 case SWM_ARG_ID_STACKBALANCE
:
5042 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ (ws
->l_state
.vertical_stacks
+ 1);
5044 case SWM_ARG_ID_STACKINC
:
5045 ws
->l_state
.vertical_stacks
++;
5047 case SWM_ARG_ID_STACKDEC
:
5048 if (ws
->l_state
.vertical_stacks
> 1)
5049 ws
->l_state
.vertical_stacks
--;
5051 case SWM_ARG_ID_FLIPLAYOUT
:
5052 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
5060 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5062 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
5064 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
5068 horizontal_config(struct workspace
*ws
, int id
)
5070 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
5073 case SWM_ARG_ID_STACKRESET
:
5074 case SWM_ARG_ID_STACKINIT
:
5075 ws
->l_state
.horizontal_mwin
= 1;
5076 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
5077 ws
->l_state
.horizontal_stacks
= 1;
5079 case SWM_ARG_ID_MASTERSHRINK
:
5080 if (ws
->l_state
.horizontal_msize
> 1)
5081 ws
->l_state
.horizontal_msize
--;
5083 case SWM_ARG_ID_MASTERGROW
:
5084 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
5085 ws
->l_state
.horizontal_msize
++;
5087 case SWM_ARG_ID_MASTERADD
:
5088 ws
->l_state
.horizontal_mwin
++;
5090 case SWM_ARG_ID_MASTERDEL
:
5091 if (ws
->l_state
.horizontal_mwin
> 0)
5092 ws
->l_state
.horizontal_mwin
--;
5094 case SWM_ARG_ID_STACKBALANCE
:
5095 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ (ws
->l_state
.horizontal_stacks
+ 1);
5097 case SWM_ARG_ID_STACKINC
:
5098 ws
->l_state
.horizontal_stacks
++;
5100 case SWM_ARG_ID_STACKDEC
:
5101 if (ws
->l_state
.horizontal_stacks
> 1)
5102 ws
->l_state
.horizontal_stacks
--;
5104 case SWM_ARG_ID_FLIPLAYOUT
:
5105 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
5113 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5115 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
5117 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
5120 /* fullscreen view */
5122 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5124 struct swm_geometry gg
= *g
;
5125 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
, *tmpw
;
5128 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
5133 winno
= count_win(ws
, false);
5134 if (winno
== 0 && count_win(ws
, true) == 0)
5137 /* Figure out which top level window should be visible. */
5138 if (ws
->focus_pending
)
5139 win
= ws
->focus_pending
;
5142 else if (ws
->focus_prev
)
5143 win
= ws
->focus_prev
;
5145 win
= TAILQ_FIRST(&ws
->winlist
);
5147 DNPRINTF(SWM_D_STACK
, "max_stack: focus_pending: %#x, focus: %#x, "
5148 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws
->focus_pending
),
5149 WINID(ws
->focus
), WINID(ws
->focus_prev
),
5150 WINID(TAILQ_FIRST(&ws
->winlist
)), win
->id
);
5152 /* Update window geometry. */
5153 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5162 /* Set maximized flag for all maxed windows. */
5163 if (!MAXIMIZED(w
)) {
5164 /* Preserve floating geometry. */
5166 store_float_geom(w
);
5168 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_MAXIMIZED
);
5169 ewmh_update_wm_state(w
);
5172 /* Only reconfigure if necessary. */
5173 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
5174 HEIGHT(w
) != gg
.h
) {
5177 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
)) {
5178 w
->bordered
= false;
5179 WIDTH(w
) += 2 * border_width
;
5180 HEIGHT(w
) += 2 * border_width
;
5189 /* If transient, stack parent and its children. */
5190 if (TRANS(win
) && (parent
= find_window(win
->transient
))) {
5191 raise_window(parent
);
5193 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5194 if (w
->transient
== parent
->id
)
5198 /* Make sure focus window is on top. */
5201 /* Stack any children of focus window. */
5202 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5203 if (w
->transient
== win
->id
)
5206 /* Map all windows. */
5207 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
5213 send_to_rg(struct swm_region
*r
, union arg
*args
)
5215 int ridx
= args
->id
, i
, num_screens
;
5216 struct swm_region
*rr
= NULL
;
5219 num_screens
= get_screen_count();
5220 /* do nothing if we don't have more than one screen */
5221 if (!(num_screens
> 1 || outputs
> 1))
5224 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
5226 rr
= TAILQ_FIRST(&r
->s
->rl
);
5227 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
5228 rr
= TAILQ_NEXT(rr
, entry
);
5239 region_under(struct swm_screen
*s
, int x
, int y
)
5241 struct swm_region
*r
= NULL
;
5243 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
5244 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
5245 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
5246 WIDTH(r
), HEIGHT(r
), x
, y
);
5247 if (X(r
) <= x
&& x
< MAX_X(r
))
5248 if (Y(r
) <= y
&& y
< MAX_Y(r
))
5255 /* Transfer focused window to target workspace and focus. */
5257 send_to_ws(struct swm_region
*r
, union arg
*args
)
5259 int wsid
= args
->id
;
5260 struct ws_win
*win
= NULL
;
5262 if (r
&& r
->ws
&& r
->ws
->focus
)
5267 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win %#x, ws %d\n", win
->id
, wsid
);
5269 if (wsid
< 0 || wsid
>= workspace_limit
)
5272 if (win
->ws
->idx
== wsid
)
5275 win_to_ws(win
, wsid
, true);
5277 /* Set new focus on target ws. */
5278 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5279 win
->ws
->focus_prev
= win
->ws
->focus
;
5280 win
->ws
->focus
= win
;
5281 win
->ws
->focus_pending
= NULL
;
5283 if (win
->ws
->focus_prev
)
5284 update_window_color(win
->ws
->focus_prev
);
5287 DNPRINTF(SWM_D_STACK
, "send_to_ws: focus_pending: %#x, focus: %#x, "
5288 "focus_prev: %#x, first: %#x, win: %#x\n",
5289 WINID(r
->ws
->focus_pending
), WINID(r
->ws
->focus
),
5290 WINID(r
->ws
->focus_prev
), WINID(TAILQ_FIRST(&r
->ws
->winlist
)),
5293 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5294 ewmh_update_wm_state(win
);
5296 /* Restack and set new focus on current ws. */
5298 load_float_geom(win
);
5302 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5303 if (r
->ws
->focus
!= NULL
) {
5304 focus_win(r
->ws
->focus
);
5306 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
5317 win_to_ws(struct ws_win
*win
, int wsid
, bool unfocus
)
5319 struct ws_win
*parent
;
5320 struct workspace
*ws
, *nws
, *pws
;
5322 if (wsid
< 0 || wsid
>= workspace_limit
)
5325 if (win
->ws
->idx
== wsid
)
5329 nws
= &win
->s
->ws
[wsid
];
5331 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win %#x, ws %d -> %d\n", win
->id
,
5334 /* Cleanup focus on source ws. */
5335 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
5336 (ws
->focus
== win
|| ws
->focus_pending
== win
))
5337 ws
->focus_pending
= get_focus_prev(win
);
5339 /* Move the parent if this is a transient window. */
5341 parent
= find_window(win
->transient
);
5344 /* Set new focus in parent's ws if needed. */
5345 if (pws
->focus
== parent
) {
5346 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5347 pws
->focus_pending
=
5348 get_focus_prev(parent
);
5350 unfocus_win(parent
);
5352 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5353 pws
->focus
= pws
->focus_pending
;
5354 pws
->focus_pending
= NULL
;
5358 /* Don't unmap parent if new ws is visible */
5360 unmap_window(parent
);
5363 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
5364 TAILQ_REMOVE(&ws
->stack
, parent
, stack_entry
);
5365 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
5366 TAILQ_INSERT_TAIL(&nws
->stack
, parent
, stack_entry
);
5369 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5370 "_NET_WM_DESKTOP: %d\n", wsid
);
5371 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5372 parent
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
5373 XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5380 if (ws
->focus_prev
== win
)
5381 ws
->focus_prev
= NULL
;
5383 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& ws
->focus_pending
!= NULL
) {
5384 ws
->focus
= ws
->focus_pending
;
5385 ws
->focus_pending
= NULL
;
5388 /* Don't unmap if new ws is visible */
5393 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
5394 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
5395 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
5396 TAILQ_INSERT_TAIL(&nws
->stack
, win
, stack_entry
);
5399 /* Update the window's workspace property: _NET_WM_DESKTOP */
5400 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5401 "_NET_WM_DESKTOP: %d\n", wsid
);
5402 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
5403 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5405 ewmh_update_client_list();
5407 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
5411 pressbutton(struct swm_region
*r
, union arg
*args
)
5413 /* suppress unused warning since var is needed */
5416 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
5417 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5418 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
5419 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5423 raise_toggle(struct swm_region
*r
, union arg
*args
)
5425 /* Suppress warning. */
5428 if (r
== NULL
|| r
->ws
== NULL
)
5431 if (r
->ws
->focus
&& MAXIMIZED(r
->ws
->focus
))
5434 r
->ws
->always_raise
= !r
->ws
->always_raise
;
5436 /* Update focused win stacking order based on new always_raise value. */
5437 raise_window(r
->ws
->focus
);
5443 iconify(struct swm_region
*r
, union arg
*args
)
5447 /* Suppress warning. */
5450 if ((w
= r
->ws
->focus
) == NULL
)
5453 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_HIDDEN
);
5454 ewmh_update_wm_state(w
);
5462 get_win_name(xcb_window_t win
)
5465 xcb_get_property_cookie_t c
;
5466 xcb_get_property_reply_t
*r
;
5468 /* First try _NET_WM_NAME for UTF-8. */
5469 c
= xcb_get_property(conn
, 0, win
, ewmh
[_NET_WM_NAME
].atom
,
5470 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5471 r
= xcb_get_property_reply(conn
, c
, NULL
);
5472 if (r
&& r
->type
== XCB_NONE
) {
5474 /* Use WM_NAME instead; no UTF-8. */
5475 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
5476 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5477 r
= xcb_get_property_reply(conn
, c
, NULL
);
5480 if (r
&& r
->type
!= XCB_NONE
&& r
->length
> 0)
5481 name
= strndup(xcb_get_property_value(r
),
5482 xcb_get_property_value_length(r
));
5487 err(1, "get_win_name: failed to allocate memory.");
5495 uniconify(struct swm_region
*r
, union arg
*args
)
5502 DNPRINTF(SWM_D_MISC
, "uniconify\n");
5504 if (r
== NULL
|| r
->ws
== NULL
)
5507 /* make sure we have anything to uniconify */
5508 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5509 if (win
->ws
== NULL
)
5510 continue; /* should never happen */
5516 DNPRINTF(SWM_D_MISC
, "uniconify: count: %d\n", count
);
5522 search_resp_action
= SWM_SEARCH_UNICONIFY
;
5524 spawn_select(r
, args
, "search", &searchpid
);
5526 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5529 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5530 if (win
->ws
== NULL
)
5531 continue; /* should never happen */
5535 name
= get_win_name(win
->id
);
5536 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
5544 name_workspace(struct swm_region
*r
, union arg
*args
)
5548 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
5554 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
5556 spawn_select(r
, args
, "name_workspace", &searchpid
);
5558 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5561 fprintf(lfile
, "%s", "");
5566 search_workspace(struct swm_region
*r
, union arg
*args
)
5569 struct workspace
*ws
;
5572 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
5578 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
5580 spawn_select(r
, args
, "search", &searchpid
);
5582 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5585 for (i
= 0; i
< workspace_limit
; i
++) {
5589 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
5590 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
5597 search_win_cleanup(void)
5599 struct search_window
*sw
= NULL
;
5601 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
5602 xcb_destroy_window(conn
, sw
->indicator
);
5603 TAILQ_REMOVE(&search_wl
, sw
, entry
);
5609 search_win(struct swm_region
*r
, union arg
*args
)
5611 struct ws_win
*win
= NULL
;
5612 struct search_window
*sw
= NULL
;
5615 xcb_screen_t
*screen
;
5616 int i
, width
, height
;
5624 XRectangle l_ibox
, l_lbox
;
5626 DNPRINTF(SWM_D_MISC
, "search_win\n");
5629 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
5631 spawn_select(r
, args
, "search", &searchpid
);
5633 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5636 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
5637 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
5639 TAILQ_INIT(&search_wl
);
5642 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5646 sw
= calloc(1, sizeof(struct search_window
));
5648 warn("search_win: calloc");
5650 search_win_cleanup();
5656 snprintf(s
, sizeof s
, "%d", i
);
5659 w
= xcb_generate_id(conn
);
5660 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
5661 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
5662 wa
[2] = screen
->default_colormap
;
5664 if (bar_font_legacy
) {
5665 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
5666 width
= l_lbox
.width
+ 4;
5667 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
5669 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
5671 width
= info
.width
+ 4;
5672 height
= bar_font
->height
+ 4;
5675 xcb_create_window(conn
, screen
->root_depth
, w
, win
->id
, 0, 0,
5676 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
5677 screen
->root_visual
, XCB_CW_BACK_PIXEL
|
5678 XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
, wa
);
5680 xcb_map_window(conn
, w
);
5683 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
5685 if (bar_font_legacy
) {
5686 l_gcv
.graphics_exposures
= 0;
5687 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
5689 XSetForeground(display
, l_draw
,
5690 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
5692 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
5693 (bar_fs_extents
->max_logical_extent
.height
-
5694 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
5696 XFreeGC(display
, l_draw
);
5699 draw
= XftDrawCreate(display
, w
,
5700 DefaultVisual(display
, r
->s
->idx
),
5701 DefaultColormap(display
, r
->s
->idx
));
5703 XftDrawStringUtf8(draw
, &search_font_color
, bar_font
, 2,
5704 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
5705 bar_font
->descent
, (FcChar8
*)s
, len
);
5707 XftDrawDestroy(draw
);
5710 DNPRINTF(SWM_D_MISC
, "search_win: mapped win %#x\n", w
);
5712 fprintf(lfile
, "%d\n", i
);
5722 search_resp_uniconify(const char *resp
, size_t len
)
5728 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
5730 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
5733 name
= get_win_name(win
->id
);
5734 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
5739 if (strncmp(s
, resp
, len
) == 0) {
5740 /* XXX this should be a callback to generalize */
5741 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_HIDDEN
);
5742 ewmh_update_wm_state(win
);
5752 search_resp_name_workspace(const char *resp
, size_t len
)
5754 struct workspace
*ws
;
5756 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
5758 if (search_r
->ws
== NULL
)
5763 free(search_r
->ws
->name
);
5764 search_r
->ws
->name
= NULL
;
5768 ws
->name
= strdup(resp
);
5769 if (ws
->name
== NULL
) {
5770 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
5771 "strdup: %s", strerror(errno
));
5775 ewmh_update_desktop_names();
5776 ewmh_get_desktop_names();
5781 ewmh_update_desktop_names(void)
5783 char *name_list
= NULL
, *p
;
5784 int num_screens
, i
, j
, len
= 0, tot
= 0;
5786 num_screens
= get_screen_count();
5787 for (i
= 0; i
< num_screens
; ++i
) {
5788 for (j
= 0; j
< workspace_limit
; ++j
) {
5789 if (screens
[i
].ws
[j
].name
!= NULL
)
5790 len
+= strlen(screens
[i
].ws
[j
].name
);
5794 if((name_list
= calloc(len
, sizeof(char))) == NULL
)
5795 err(1, "update_desktop_names: calloc: failed to "
5796 "allocate memory.");
5799 for (j
= 0; j
< workspace_limit
; ++j
) {
5800 if (screens
[i
].ws
[j
].name
!= NULL
) {
5801 len
= strlen(screens
[i
].ws
[j
].name
);
5802 memcpy(p
, screens
[i
].ws
[j
].name
, len
);
5811 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5812 screens
[i
].root
, ewmh
[_NET_DESKTOP_NAMES
].atom
,
5813 a_utf8_string
, 8, tot
, name_list
);
5823 ewmh_get_desktop_names(void)
5826 xcb_get_property_cookie_t c
;
5827 xcb_get_property_reply_t
*r
;
5828 int num_screens
, i
, j
, n
, k
;
5830 num_screens
= get_screen_count();
5831 for (i
= 0; i
< num_screens
; ++i
) {
5832 for (j
= 0; j
< workspace_limit
; ++j
) {
5833 free(screens
[i
].ws
[j
].name
);
5834 screens
[i
].ws
[j
].name
= NULL
;
5837 c
= xcb_get_property(conn
, 0, screens
[i
].root
,
5838 ewmh
[_NET_DESKTOP_NAMES
].atom
,
5839 a_utf8_string
, 0, UINT32_MAX
);
5840 r
= xcb_get_property_reply(conn
, c
, NULL
);
5844 names
= xcb_get_property_value(r
);
5845 n
= xcb_get_property_value_length(r
);
5847 for (j
= 0, k
= 0; j
< n
; ++j
) {
5848 if (*(names
+ j
) != '\0') {
5849 screens
[i
].ws
[k
].name
= strdup(names
+ j
);
5850 j
+= strlen(names
+ j
);
5859 ewmh_update_client_list(void)
5862 int num_screens
, i
, j
, k
= 0, count
= 0;
5865 num_screens
= get_screen_count();
5866 for (i
= 0; i
< num_screens
; ++i
) {
5867 for (j
= 0; j
< workspace_limit
; ++j
)
5868 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
5871 DNPRINTF(SWM_D_PROP
, "ewmh_update_client_list: win count: %d\n",
5877 wins
= calloc(count
, sizeof(xcb_window_t
));
5879 err(1, "ewmh_update_client_list: calloc: failed to "
5880 "allocate memory.");
5882 for (j
= 0, k
= 0; j
< workspace_limit
; ++j
)
5883 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
5884 wins
[k
++] = win
->id
;
5886 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5887 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
5888 XCB_ATOM_WINDOW
, 32, count
, wins
);
5895 ewmh_update_current_desktop(void)
5899 num_screens
= get_screen_count();
5900 for (i
= 0; i
< num_screens
; ++i
) {
5901 if (screens
[i
].r_focus
)
5902 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5903 screens
[i
].root
, ewmh
[_NET_CURRENT_DESKTOP
].atom
,
5904 XCB_ATOM_CARDINAL
, 32, 1,
5905 &screens
[i
].r_focus
->ws
->idx
);
5910 ewmh_update_desktops(void)
5912 int num_screens
, i
, j
;
5915 vals
= calloc(workspace_limit
* 2, sizeof(uint32_t));
5917 err(1, "ewmh_update_desktops: calloc: failed to allocate "
5920 num_screens
= get_screen_count();
5921 for (i
= 0; i
< num_screens
; i
++) {
5922 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5923 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
5924 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
5926 for (j
= 0; j
< workspace_limit
; ++j
) {
5927 if (screens
[i
].ws
[j
].r
!= NULL
) {
5928 vals
[j
* 2] = X(screens
[i
].ws
[j
].r
);
5929 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].r
);
5930 } else if (screens
[i
].ws
[j
].old_r
!= NULL
) {
5931 vals
[j
* 2] = X(screens
[i
].ws
[j
].old_r
);
5932 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].old_r
);
5934 vals
[j
* 2] = vals
[j
* 2 + 1] = 0;
5938 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5939 screens
[i
].root
, ewmh
[_NET_DESKTOP_VIEWPORT
].atom
,
5940 XCB_ATOM_CARDINAL
, 32, workspace_limit
* 2, vals
);
5947 search_resp_search_workspace(const char *resp
)
5954 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
5958 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
5965 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
5967 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
5974 switchws(search_r
, &a
);
5978 search_resp_search_window(const char *resp
)
5983 struct search_window
*sw
;
5985 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
5989 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
5994 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
5996 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
6003 TAILQ_FOREACH(sw
, &search_wl
, entry
)
6004 if (idx
== sw
->idx
) {
6010 #define MAX_RESP_LEN 1024
6013 search_do_resp(void)
6019 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
6024 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
6025 warn("search: calloc");
6029 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
6031 warn("search: read error");
6034 resp
[rbytes
] = '\0';
6037 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
6038 * newline, so work around that by sanitizing the resp now.
6040 resp
[strcspn(resp
, "\n")] = '\0';
6043 switch (search_resp_action
) {
6044 case SWM_SEARCH_UNICONIFY
:
6045 search_resp_uniconify(resp
, len
);
6047 case SWM_SEARCH_NAME_WORKSPACE
:
6048 search_resp_name_workspace(resp
, len
);
6050 case SWM_SEARCH_SEARCH_WORKSPACE
:
6051 search_resp_search_workspace(resp
);
6053 case SWM_SEARCH_SEARCH_WINDOW
:
6054 search_resp_search_window(resp
);
6059 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
6060 search_win_cleanup();
6062 search_resp_action
= SWM_SEARCH_NONE
;
6063 close(select_resp_pipe
[0]);
6070 wkill(struct swm_region
*r
, union arg
*args
)
6072 DNPRINTF(SWM_D_MISC
, "wkill: win %#x, id: %d\n", WINID(r
->ws
->focus
),
6075 if (r
->ws
->focus
== NULL
)
6078 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
6079 xcb_kill_client(conn
, r
->ws
->focus
->id
);
6081 if (r
->ws
->focus
->can_delete
)
6082 client_msg(r
->ws
->focus
, a_delete
, 0);
6088 clear_maximized(struct workspace
*ws
)
6093 /* Clear any maximized win(s) on ws, from bottom up. */
6094 TAILQ_FOREACH_REVERSE(w
, &ws
->stack
, ws_win_stack
, stack_entry
)
6096 ewmh_apply_flags(w
, w
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
6097 ewmh_update_wm_state(w
);
6105 maximize_toggle(struct swm_region
*r
, union arg
*args
)
6107 struct ws_win
*w
= r
->ws
->focus
;
6109 /* suppress unused warning since var is needed */
6115 DNPRINTF(SWM_D_MISC
, "maximize_toggle: win %#x\n", w
->id
);
6120 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6123 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_MAXIMIZED
);
6124 ewmh_update_wm_state(w
);
6128 if (w
== w
->ws
->focus
)
6133 DNPRINTF(SWM_D_MISC
, "maximize_toggle: done\n");
6137 floating_toggle(struct swm_region
*r
, union arg
*args
)
6139 struct ws_win
*w
= r
->ws
->focus
;
6141 /* suppress unused warning since var is needed */
6147 DNPRINTF(SWM_D_MISC
, "floating_toggle: win %#x\n", w
->id
);
6149 if (FULLSCREEN(w
) || TRANS(w
))
6152 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6155 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_ABOVE
);
6156 ewmh_update_wm_state(w
);
6160 if (w
== w
->ws
->focus
)
6165 DNPRINTF(SWM_D_MISC
, "floating_toggle: done\n");
6169 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
6171 struct swm_geometry g
= r
->g
;
6172 int rt
, lt
, tp
, bm
, bw
;
6174 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
6177 * Perpendicular distance of each side of the window to the respective
6178 * side of the region boundary. Positive values indicate the side of
6179 * the window has passed beyond the region boundary.
6181 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) + BORDER(win
) - MAX_X(r
) : bw
;
6182 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) + BORDER(win
) : bw
;
6183 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) + BORDER(win
) - MAX_Y(r
) : bw
;
6184 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) + BORDER(win
) : bw
;
6186 DNPRINTF(SWM_D_MISC
, "region_containment: win %#x, rt: %d, lt: %d, "
6187 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
6188 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
6189 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
6192 * Disable containment if any of the flagged sides went beyond the
6193 * containment boundary, or if containment is disabled.
6195 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
6196 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
6197 /* Make sure window has at least 1 pixel in the region */
6198 g
.x
+= 1 - WIDTH(win
);
6199 g
.y
+= 1 - HEIGHT(win
);
6200 g
.w
+= 2 * WIDTH(win
) - 2;
6201 g
.h
+= 2 * HEIGHT(win
) - 2;
6204 constrain_window(win
, &g
, &opts
);
6207 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6209 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
6211 DNPRINTF(SWM_D_MISC
, "constrain_window: win %#x, (x,y) w x h: "
6212 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6213 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
6214 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
6215 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
6216 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
6217 YESNO(*opts
& SWM_CW_RESIZABLE
));
6219 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) + BORDER(win
) > b
->x
+ b
->w
) {
6220 if (*opts
& SWM_CW_RESIZABLE
)
6221 WIDTH(win
) = b
->x
+ b
->w
- X(win
) - BORDER(win
);
6223 X(win
) = b
->x
+ b
->w
- WIDTH(win
) - BORDER(win
);
6226 if ((*opts
& SWM_CW_LEFT
) && X(win
) + BORDER(win
) < b
->x
) {
6227 if (*opts
& SWM_CW_RESIZABLE
)
6228 WIDTH(win
) -= b
->x
- X(win
) - BORDER(win
);
6230 X(win
) = b
->x
- BORDER(win
);
6233 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) + BORDER(win
) > b
->y
+ b
->h
) {
6234 if (*opts
& SWM_CW_RESIZABLE
)
6235 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
) - BORDER(win
);
6237 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
) - BORDER(win
);
6240 if ((*opts
& SWM_CW_TOP
) && Y(win
) + BORDER(win
) < b
->y
) {
6241 if (*opts
& SWM_CW_RESIZABLE
)
6242 HEIGHT(win
) -= b
->y
- Y(win
) - BORDER(win
);
6244 Y(win
) = b
->y
- BORDER(win
);
6247 if (*opts
& SWM_CW_RESIZABLE
) {
6250 if (HEIGHT(win
) < 1)
6256 update_window_color(struct ws_win
*win
)
6260 if (WS_FOCUSED(win
->ws
) && win
->ws
->focus
== win
)
6261 pixel
= MAXIMIZED(win
) ?
6262 &win
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].pixel
:
6263 &win
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6265 pixel
= MAXIMIZED(win
) ?
6266 &win
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].pixel
:
6267 &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6269 xcb_change_window_attributes(conn
, win
->id
,
6270 XCB_CW_BORDER_PIXEL
, pixel
);
6274 update_window(struct ws_win
*win
)
6279 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
6280 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
6281 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6285 wc
[3] = HEIGHT(win
);
6286 wc
[4] = BORDER(win
);
6288 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x, (x,y) w x h: "
6289 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
6290 wc
[3], YESNO(win
->bordered
));
6292 xcb_configure_window(conn
, win
->id
, mask
, wc
);
6295 #define SWM_RESIZE_STEPS (50)
6298 resize(struct ws_win
*win
, union arg
*args
)
6300 xcb_timestamp_t timestamp
= 0;
6301 struct swm_region
*r
= NULL
;
6302 struct swm_geometry g
;
6304 int top
= 0, left
= 0;
6306 xcb_cursor_t cursor
;
6307 xcb_query_pointer_reply_t
*xpr
= NULL
;
6308 xcb_generic_event_t
*evt
;
6309 xcb_motion_notify_event_t
*mne
;
6316 if (FULLSCREEN(win
))
6319 /* In max_stack mode, should only resize transients. */
6320 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6323 DNPRINTF(SWM_D_EVENT
, "resize: win %#x, floating: %s, "
6324 "transient: %#x\n", win
->id
, YESNO(ABOVE(win
)),
6328 store_float_geom(win
);
6329 else if (!(TRANS(win
) || ABOVE(win
)))
6332 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6334 ewmh_update_wm_state(win
);
6340 /* It's possible for win to have been freed during focus_flush(). */
6341 if (validate_win(win
)) {
6342 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6347 case SWM_ARG_ID_WIDTHSHRINK
:
6348 WIDTH(win
) -= SWM_RESIZE_STEPS
;
6351 case SWM_ARG_ID_WIDTHGROW
:
6352 WIDTH(win
) += SWM_RESIZE_STEPS
;
6355 case SWM_ARG_ID_HEIGHTSHRINK
:
6356 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
6359 case SWM_ARG_ID_HEIGHTGROW
:
6360 HEIGHT(win
) += SWM_RESIZE_STEPS
;
6367 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6368 SWM_CW_HARDBOUNDARY
);
6370 store_float_geom(win
);
6374 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6375 SWM_CW_SOFTBOUNDARY
);
6378 /* get cursor offset from window root */
6379 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6386 if (xpr
->win_x
< WIDTH(win
) / 2)
6389 if (xpr
->win_y
< HEIGHT(win
) / 2)
6392 if (args
->id
== SWM_ARG_ID_CENTER
)
6393 cursor
= cursors
[XC_SIZING
].cid
;
6395 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
6396 XC_TOP_RIGHT_CORNER
].cid
;
6398 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
6399 XC_BOTTOM_RIGHT_CORNER
].cid
;
6401 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6402 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
6407 while (resizing
&& (evt
= xcb_wait_for_event(conn
))) {
6408 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6409 case XCB_BUTTON_RELEASE
:
6410 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
6413 case XCB_MOTION_NOTIFY
:
6414 mne
= (xcb_motion_notify_event_t
*)evt
;
6415 /* cursor offset/delta from start of the operation */
6416 dx
= mne
->root_x
- xpr
->root_x
;
6417 dy
= mne
->root_y
- xpr
->root_y
;
6423 if (args
->id
== SWM_ARG_ID_CENTER
) {
6424 if (g
.h
/ 2 + dy
< 1)
6428 HEIGHT(win
) = g
.h
+ 2 * dy
;
6436 HEIGHT(win
) = g
.h
+ dy
;
6443 if (args
->id
== SWM_ARG_ID_CENTER
) {
6444 if (g
.w
/ 2 + dx
< 1)
6448 WIDTH(win
) = g
.w
+ 2 * dx
;
6456 WIDTH(win
) = g
.w
+ dx
;
6459 /* not free, don't sync more than 120 times / second */
6460 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6461 timestamp
= mne
->time
;
6462 regionize(win
, mne
->root_x
, mne
->root_y
);
6463 region_containment(win
, r
, SWM_CW_ALLSIDES
|
6464 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
6465 SWM_CW_SOFTBOUNDARY
);
6472 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6473 ((xcb_key_press_event_t
*)evt
)->time
);
6479 /* It's possible for win to have been freed above. */
6480 if (validate_win(win
)) {
6481 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6489 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6490 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
6494 store_float_geom(win
);
6496 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6498 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
6502 resize_step(struct swm_region
*r
, union arg
*args
)
6504 struct ws_win
*win
= NULL
;
6506 if (r
&& r
->ws
&& r
->ws
->focus
)
6516 /* Try to set window region based on supplied coordinates or window center. */
6518 regionize(struct ws_win
*win
, int x
, int y
)
6520 struct swm_region
*r
= NULL
;
6522 r
= region_under(win
->s
, x
, y
);
6524 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
6525 Y(win
) + HEIGHT(win
) / 2);
6527 if (r
!= NULL
&& r
!= win
->ws
->r
) {
6528 if (clear_maximized(r
->ws
) > 0)
6531 win_to_ws(win
, r
->ws
->idx
, false);
6533 /* Set focus on new ws. */
6534 unfocus_win(r
->ws
->focus
);
6542 #define SWM_MOVE_STEPS (50)
6545 move(struct ws_win
*win
, union arg
*args
)
6547 struct swm_region
*r
;
6548 xcb_timestamp_t timestamp
= 0;
6550 xcb_query_pointer_reply_t
*qpr
= NULL
;
6551 xcb_generic_event_t
*evt
;
6552 xcb_motion_notify_event_t
*mne
;
6553 bool moving
, restack
= false;
6558 if ((r
= win
->ws
->r
) == NULL
)
6561 if (FULLSCREEN(win
))
6564 DNPRINTF(SWM_D_EVENT
, "move: win %#x, floating: %s, transient: "
6565 "%#x\n", win
->id
, YESNO(ABOVE(win
)), win
->transient
);
6567 /* in max_stack mode should only move transients */
6568 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6571 if (!(ABOVE(win
) || TRANS(win
)) || MAXIMIZED(win
)) {
6572 store_float_geom(win
);
6576 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6578 ewmh_update_wm_state(win
);
6585 /* It's possible for win to have been freed during focus_flush(). */
6586 if (validate_win(win
)) {
6587 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
6593 case SWM_ARG_ID_MOVELEFT
:
6594 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
6597 case SWM_ARG_ID_MOVERIGHT
:
6598 X(win
) += (SWM_MOVE_STEPS
- border_width
);
6601 case SWM_ARG_ID_MOVEUP
:
6602 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
6605 case SWM_ARG_ID_MOVEDOWN
:
6606 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
6613 regionize(win
, -1, -1);
6614 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
6616 store_float_geom(win
);
6620 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6621 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
6622 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
6624 /* get cursor offset from window root */
6625 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6628 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6632 regionize(win
, qpr
->root_x
, qpr
->root_y
);
6633 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
6634 SWM_CW_SOFTBOUNDARY
);
6638 while (moving
&& (evt
= xcb_wait_for_event(conn
))) {
6639 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6640 case XCB_BUTTON_RELEASE
:
6641 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
6644 case XCB_MOTION_NOTIFY
:
6645 mne
= (xcb_motion_notify_event_t
*)evt
;
6646 DNPRINTF(SWM_D_EVENT
, "motion: root: %#x\n", mne
->root
);
6647 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
6648 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
6650 /* not free, don't sync more than 120 times / second */
6651 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6652 timestamp
= mne
->time
;
6653 regionize(win
, mne
->root_x
, mne
->root_y
);
6654 region_containment(win
, win
->ws
->r
,
6655 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
6662 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6663 ((xcb_key_press_event_t
*)evt
)->time
);
6669 /* It's possible for win to have been freed above. */
6670 if (validate_win(win
)) {
6671 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
6679 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
6680 SWM_CW_SOFTBOUNDARY
);
6684 store_float_geom(win
);
6686 /* New region set to fullscreen layout. */
6687 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
]) {
6694 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6695 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
6699 move_step(struct swm_region
*r
, union arg
*args
)
6701 struct ws_win
*win
= NULL
;
6703 if (r
&& r
->ws
&& r
->ws
->focus
)
6708 if (!TRANS(win
) && !ABOVE(win
))
6716 /* key definitions */
6718 char name
[SWM_FUNCNAME_LEN
];
6719 void (*func
)(struct swm_region
*r
, union arg
*);
6721 } keyfuncs
[KF_INVALID
+ 1] = {
6722 /* name function argument */
6723 { "bar_toggle", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
6724 { "bar_toggle_ws", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
6725 { "button2", pressbutton
, {2} },
6726 { "cycle_layout", cycle_layout
, {0} },
6727 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
6728 { "float_toggle", floating_toggle
,{0} },
6729 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
6730 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
6731 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
6732 { "focus_urgent", focus
, {.id
= SWM_ARG_ID_FOCUSURGENT
} },
6733 { "maximize_toggle", maximize_toggle
,{0} },
6734 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
6735 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
6736 { "iconify", iconify
, {0} },
6737 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
6738 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
6739 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
6740 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
6741 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
6742 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
6743 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
6744 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
6745 { "mvrg_1", send_to_rg
, {.id
= 0} },
6746 { "mvrg_2", send_to_rg
, {.id
= 1} },
6747 { "mvrg_3", send_to_rg
, {.id
= 2} },
6748 { "mvrg_4", send_to_rg
, {.id
= 3} },
6749 { "mvrg_5", send_to_rg
, {.id
= 4} },
6750 { "mvrg_6", send_to_rg
, {.id
= 5} },
6751 { "mvrg_7", send_to_rg
, {.id
= 6} },
6752 { "mvrg_8", send_to_rg
, {.id
= 7} },
6753 { "mvrg_9", send_to_rg
, {.id
= 8} },
6754 { "mvws_1", send_to_ws
, {.id
= 0} },
6755 { "mvws_2", send_to_ws
, {.id
= 1} },
6756 { "mvws_3", send_to_ws
, {.id
= 2} },
6757 { "mvws_4", send_to_ws
, {.id
= 3} },
6758 { "mvws_5", send_to_ws
, {.id
= 4} },
6759 { "mvws_6", send_to_ws
, {.id
= 5} },
6760 { "mvws_7", send_to_ws
, {.id
= 6} },
6761 { "mvws_8", send_to_ws
, {.id
= 7} },
6762 { "mvws_9", send_to_ws
, {.id
= 8} },
6763 { "mvws_10", send_to_ws
, {.id
= 9} },
6764 { "mvws_11", send_to_ws
, {.id
= 10} },
6765 { "mvws_12", send_to_ws
, {.id
= 11} },
6766 { "mvws_13", send_to_ws
, {.id
= 12} },
6767 { "mvws_14", send_to_ws
, {.id
= 13} },
6768 { "mvws_15", send_to_ws
, {.id
= 14} },
6769 { "mvws_16", send_to_ws
, {.id
= 15} },
6770 { "mvws_17", send_to_ws
, {.id
= 16} },
6771 { "mvws_18", send_to_ws
, {.id
= 17} },
6772 { "mvws_19", send_to_ws
, {.id
= 18} },
6773 { "mvws_20", send_to_ws
, {.id
= 19} },
6774 { "mvws_21", send_to_ws
, {.id
= 20} },
6775 { "mvws_22", send_to_ws
, {.id
= 21} },
6776 { "name_workspace", name_workspace
, {0} },
6777 { "quit", quit
, {0} },
6778 { "raise_toggle", raise_toggle
, {0} },
6779 { "restart", restart
, {0} },
6780 { "rg_1", focusrg
, {.id
= 0} },
6781 { "rg_2", focusrg
, {.id
= 1} },
6782 { "rg_3", focusrg
, {.id
= 2} },
6783 { "rg_4", focusrg
, {.id
= 3} },
6784 { "rg_5", focusrg
, {.id
= 4} },
6785 { "rg_6", focusrg
, {.id
= 5} },
6786 { "rg_7", focusrg
, {.id
= 6} },
6787 { "rg_8", focusrg
, {.id
= 7} },
6788 { "rg_9", focusrg
, {.id
= 8} },
6789 { "rg_move_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_MOVE_UP
} },
6790 { "rg_move_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_MOVE_DOWN
} },
6791 { "rg_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
6792 { "rg_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
6793 { "screen_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
6794 { "screen_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
6795 { "search_win", search_win
, {0} },
6796 { "search_workspace", search_workspace
, {0} },
6797 { "spawn_custom", NULL
, {0} },
6798 { "stack_balance", stack_config
, {.id
= SWM_ARG_ID_STACKBALANCE
} },
6799 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
6800 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
6801 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
6802 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
6803 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
6804 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
6805 { "uniconify", uniconify
, {0} },
6806 { "version", version
, {0} },
6807 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
6808 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
6809 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
6810 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
6811 { "ws_1", switchws
, {.id
= 0} },
6812 { "ws_2", switchws
, {.id
= 1} },
6813 { "ws_3", switchws
, {.id
= 2} },
6814 { "ws_4", switchws
, {.id
= 3} },
6815 { "ws_5", switchws
, {.id
= 4} },
6816 { "ws_6", switchws
, {.id
= 5} },
6817 { "ws_7", switchws
, {.id
= 6} },
6818 { "ws_8", switchws
, {.id
= 7} },
6819 { "ws_9", switchws
, {.id
= 8} },
6820 { "ws_10", switchws
, {.id
= 9} },
6821 { "ws_11", switchws
, {.id
= 10} },
6822 { "ws_12", switchws
, {.id
= 11} },
6823 { "ws_13", switchws
, {.id
= 12} },
6824 { "ws_14", switchws
, {.id
= 13} },
6825 { "ws_15", switchws
, {.id
= 14} },
6826 { "ws_16", switchws
, {.id
= 15} },
6827 { "ws_17", switchws
, {.id
= 16} },
6828 { "ws_18", switchws
, {.id
= 17} },
6829 { "ws_19", switchws
, {.id
= 18} },
6830 { "ws_20", switchws
, {.id
= 19} },
6831 { "ws_21", switchws
, {.id
= 20} },
6832 { "ws_22", switchws
, {.id
= 21} },
6833 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
6834 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
6835 { "ws_next_move", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
6836 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
6837 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
6838 { "ws_prev_move", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
6839 { "ws_prior", priorws
, {0} },
6840 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
6841 { "invalid key func", NULL
, {0} },
6845 key_cmp(struct key
*kp1
, struct key
*kp2
)
6847 if (kp1
->keysym
< kp2
->keysym
)
6849 if (kp1
->keysym
> kp2
->keysym
)
6852 if (kp1
->mod
< kp2
->mod
)
6854 if (kp1
->mod
> kp2
->mod
)
6861 enum { client_click
, root_click
};
6863 unsigned int action
;
6865 unsigned int button
;
6866 void (*func
)(struct ws_win
*, union arg
*);
6869 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6870 /* action key mouse button func args */
6871 { client_click
, MODKEY
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
6872 { client_click
, MODKEY_SHIFT
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
6873 { client_click
, MODKEY
, XCB_BUTTON_INDEX_1
, move
, {0} },
6878 update_modkey(unsigned int mod
)
6884 RB_FOREACH(kp
, key_tree
, &keys
)
6885 if (kp
->mod
& XCB_MOD_MASK_SHIFT
)
6886 kp
->mod
= mod
| XCB_MOD_MASK_SHIFT
;
6890 for (i
= 0; i
< LENGTH(buttons
); i
++)
6891 if (buttons
[i
].mask
& XCB_MOD_MASK_SHIFT
)
6892 buttons
[i
].mask
= mod
| XCB_MOD_MASK_SHIFT
;
6894 buttons
[i
].mask
= mod
;
6898 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
6901 struct spawn_prog
*prog
= NULL
;
6903 char *ap
, **real_args
;
6905 /* suppress unused warning since var is needed */
6908 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
6911 TAILQ_FOREACH(prog
, &spawns
, entry
) {
6912 if (strcasecmp(spawn_name
, prog
->name
) == 0)
6916 warnx("spawn_custom: program %s not found", spawn_name
);
6920 /* make room for expanded args */
6921 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
6922 err(1, "spawn_custom: calloc real_args");
6924 /* expand spawn_args into real_args */
6925 for (i
= c
= 0; i
< prog
->argc
; i
++) {
6927 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
6928 if (strcasecmp(ap
, "$bar_border") == 0) {
6930 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
6932 err(1, "spawn_custom border color");
6933 } else if (strcasecmp(ap
, "$bar_color") == 0) {
6935 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
6937 err(1, "spawn_custom bar color");
6938 } else if (strcasecmp(ap
, "$bar_font") == 0) {
6939 if ((real_args
[c
] = strdup(bar_fonts
))
6941 err(1, "spawn_custom bar fonts");
6942 } else if (strcasecmp(ap
, "$bar_font_color") == 0) {
6944 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
6946 err(1, "spawn_custom color font");
6947 } else if (strcasecmp(ap
, "$color_focus") == 0) {
6949 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
6951 err(1, "spawn_custom color focus");
6952 } else if (strcasecmp(ap
, "$color_focus_maximized") == 0) {
6954 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].name
))
6956 err(1, "spawn_custom color focus maximized");
6957 } else if (strcasecmp(ap
, "$color_unfocus") == 0) {
6959 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
6961 err(1, "spawn_custom color unfocus");
6962 } else if (strcasecmp(ap
, "$color_unfocus_maximized") == 0) {
6964 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].name
))
6966 err(1, "spawn_custom color unfocus maximized");
6967 } else if (strcasecmp(ap
, "$region_index") == 0) {
6968 if (asprintf(&real_args
[c
], "%d",
6969 get_region_index(r
) + 1) < 1)
6970 err(1, "spawn_custom region index");
6971 } else if (strcasecmp(ap
, "$workspace_index") == 0) {
6972 if (asprintf(&real_args
[c
], "%d", r
->ws
->idx
+ 1) < 1)
6973 err(1, "spawn_custom workspace index");
6974 } else if (strcasecmp(ap
, "$dmenu_bottom") == 0) {
6977 if ((real_args
[c
] = strdup("-b")) == NULL
)
6978 err(1, "spawn_custom workspace index");
6980 /* no match --> copy as is */
6981 if ((real_args
[c
] = strdup(ap
)) == NULL
)
6982 err(1, "spawn_custom strdup(ap)");
6984 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
6990 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
6991 for (i
= 0; i
< c
; ++i
)
6992 DPRINTF("\"%s\" ", real_args
[i
]);
6995 *ret_args
= real_args
;
7000 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
7006 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7010 spawn(r
->ws
->idx
, &a
, true);
7012 for (i
= 0; i
< spawn_argc
; i
++)
7018 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7025 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7029 if (pipe(select_list_pipe
) == -1)
7030 err(1, "pipe error");
7031 if (pipe(select_resp_pipe
) == -1)
7032 err(1, "pipe error");
7034 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
7035 err(1, "could not disable SIGPIPE");
7036 switch (*pid
= fork()) {
7038 err(1, "cannot fork");
7041 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
7043 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
7045 close(select_list_pipe
[1]);
7046 close(select_resp_pipe
[0]);
7047 spawn(r
->ws
->idx
, &a
, false);
7049 default: /* parent */
7050 close(select_list_pipe
[0]);
7051 close(select_resp_pipe
[1]);
7055 for (i
= 0; i
< spawn_argc
; i
++)
7060 /* Argument tokenizer. */
7063 char *arg
, *cp
, *next
;
7064 bool single_quoted
= false, double_quoted
= false;
7069 /* Eat and move characters until end of argument is found. */
7070 for (arg
= next
= cp
= *sp
; *cp
!= '\0'; ++cp
) {
7071 if (!double_quoted
&& *cp
== '\'') {
7072 /* Eat single-quote. */
7073 single_quoted
= !single_quoted
;
7074 } else if (!single_quoted
&& *cp
== '"') {
7075 /* Eat double-quote. */
7076 double_quoted
= !double_quoted
;
7077 } else if (!single_quoted
&& *cp
== '\\' && *(cp
+ 1) == '"') {
7078 /* Eat backslash; copy escaped character to arg. */
7080 } else if (!single_quoted
&& !double_quoted
&& *cp
== '\\' &&
7081 (*(cp
+ 1) == '\'' || *(cp
+ 1) == ' ')) {
7082 /* Eat backslash; move escaped character. */
7084 } else if (!single_quoted
&& !double_quoted
&&
7085 (*cp
== ' ' || *cp
== '\t')) {
7086 /* Terminate argument. */
7088 /* Point sp to beginning of next argument. */
7092 /* Move regular character. */
7097 /* Terminate argument if end of string. */
7107 spawn_insert(const char *name
, const char *args
, int flags
)
7109 struct spawn_prog
*sp
;
7110 char *arg
, *cp
, *ptr
;
7112 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s[%s]\n", name
, args
);
7114 if (args
== NULL
|| *args
== '\0')
7117 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
7118 err(1, "spawn_insert: calloc");
7119 if ((sp
->name
= strdup(name
)) == NULL
)
7120 err(1, "spawn_insert: strdup");
7122 /* Convert the arguments to an argument list. */
7123 if ((ptr
= cp
= strdup(args
)) == NULL
)
7124 err(1, "spawn_insert: strdup");
7125 while ((arg
= argsep(&ptr
)) != NULL
) {
7126 /* Null argument; skip it. */
7131 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
7132 sizeof *sp
->argv
)) == NULL
)
7133 err(1, "spawn_insert: realloc");
7134 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
7135 err(1, "spawn_insert: strdup");
7141 DNPRINTF(SWM_D_SPAWN
, "arg %d: [%s]\n", sp
->argc
, sp
->argv
[sp
->argc
-1]);
7142 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
7143 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
7147 spawn_remove(struct spawn_prog
*sp
)
7151 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
7153 TAILQ_REMOVE(&spawns
, sp
, entry
);
7154 for (i
= 0; i
< sp
->argc
; i
++)
7160 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
7166 struct spawn_prog
*sp
;
7168 while ((sp
= TAILQ_FIRST(&spawns
)) != NULL
) {
7174 spawn_find(const char *name
)
7176 struct spawn_prog
*sp
;
7178 TAILQ_FOREACH(sp
, &spawns
, entry
)
7179 if (strcasecmp(sp
->name
, name
) == 0)
7186 setspawn(const char *name
, const char *args
, int flags
)
7188 struct spawn_prog
*sp
;
7190 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
7195 /* Remove any old spawn under the same name. */
7196 if ((sp
= spawn_find(name
)) != NULL
)
7200 spawn_insert(name
, args
, flags
);
7202 warnx("error: setspawn: cannot find program: %s", name
);
7204 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
7208 setconfspawn(const char *selector
, const char *value
, int flags
)
7212 if (selector
== NULL
|| strlen(selector
) == 0)
7215 args
= expand_tilde(value
);
7217 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
7219 setspawn(selector
, args
, flags
);
7222 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
7227 validate_spawns(void)
7229 struct spawn_prog
*sp
;
7230 char which
[PATH_MAX
];
7235 RB_FOREACH(kp
, key_tree
, &keys
) {
7236 if (kp
->funcid
!= KF_SPAWN_CUSTOM
)
7240 sp
= spawn_find(kp
->spawn_name
);
7241 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
7244 /* verify we have the goods */
7245 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
7246 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
7248 for (i
= strlen("which "); i
< strlen(which
); i
++)
7249 if (which
[i
] == ' ') {
7253 if (system(which
) != 0)
7254 add_startup_exception("could not find %s",
7255 &which
[strlen("which ")]);
7262 setconfspawn("lock", "xlock", 0);
7264 setconfspawn("term", "xterm", 0);
7265 setconfspawn("spawn_term", "xterm", 0);
7267 setconfspawn("menu", "dmenu_run"
7271 " -nf $bar_font_color"
7273 " -sf $bar_color", 0);
7275 setconfspawn("search", "dmenu"
7280 " -nf $bar_font_color"
7282 " -sf $bar_color", 0);
7284 setconfspawn("name_workspace", "dmenu"
7289 " -nf $bar_font_color"
7291 " -sf $bar_color", 0);
7293 /* These are not verified for existence, even with a binding set. */
7294 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
7295 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
7296 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
7300 #define SWM_MODNAME_SIZE 32
7301 #define SWM_KEY_WS "\n+ \t"
7303 parsekeys(const char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
7305 char *str
, *cp
, *name
;
7308 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
7309 if (mod
== NULL
|| ks
== NULL
) {
7310 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
7313 if (keystr
== NULL
|| strlen(keystr
) == 0) {
7314 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
7318 if ((cp
= str
= strdup(keystr
)) == NULL
)
7319 err(1, "parsekeys: strdup");
7321 *ks
= XCB_NO_SYMBOL
;
7323 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
7324 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
7326 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
7327 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
7329 else if (strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
) == 0)
7330 *mod
|= XCB_MOD_MASK_1
;
7331 else if (strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
) == 0)
7332 *mod
+= XCB_MOD_MASK_2
;
7333 else if (strncmp(name
, "Mod3", SWM_MODNAME_SIZE
) == 0)
7334 *mod
|= XCB_MOD_MASK_3
;
7335 else if (strncmp(name
, "Mod4", SWM_MODNAME_SIZE
) == 0)
7336 *mod
|= XCB_MOD_MASK_4
;
7337 else if (strncmp(name
, "Mod5", SWM_MODNAME_SIZE
) == 0)
7338 *mod
|= XCB_MOD_MASK_5
;
7339 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
7340 *mod
|= XCB_MOD_MASK_SHIFT
;
7341 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
7342 *mod
|= XCB_MOD_MASK_CONTROL
;
7344 *ks
= XStringToKeysym(name
);
7345 XConvertCase(*ks
, ks
, &uks
);
7346 if (ks
== XCB_NO_SYMBOL
) {
7348 "parsekeys: invalid key %s\n",
7357 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
7362 strdupsafe(const char *str
)
7367 return (strdup(str
));
7371 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
7372 const char *spawn_name
)
7376 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
7377 keyfuncs
[kfid
].name
, spawn_name
);
7379 if ((kp
= malloc(sizeof *kp
)) == NULL
)
7380 err(1, "key_insert: malloc");
7385 kp
->spawn_name
= strdupsafe(spawn_name
);
7386 RB_INSERT(key_tree
, &keys
, kp
);
7388 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
7392 key_lookup(unsigned int mod
, KeySym ks
)
7399 return (RB_FIND(key_tree
, &keys
, &kp
));
7403 key_remove(struct key
*kp
)
7405 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
7407 RB_REMOVE(key_tree
, &keys
, kp
);
7408 free(kp
->spawn_name
);
7411 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
7415 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
7416 const char *spawn_name
)
7418 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
7422 key_insert(mod
, ks
, kfid
, spawn_name
);
7424 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
7428 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
7429 const char *spawn_name
)
7433 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
7434 keyfuncs
[kfid
].name
, spawn_name
);
7436 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
7437 if (kfid
== KF_INVALID
)
7440 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
7441 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
7444 if (kfid
== KF_INVALID
) {
7445 warnx("bind: Key combination already unbound.");
7446 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
7450 key_insert(mod
, ks
, kfid
, spawn_name
);
7451 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
7455 setconfbinding(const char *selector
, const char *value
, int flags
)
7457 enum keyfuncid kfid
;
7460 struct spawn_prog
*sp
;
7462 /* suppress unused warning since var is needed */
7465 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter selector: [%s], "
7466 "value: [%s]\n", selector
, value
);
7467 if (selector
== NULL
|| strlen(selector
) == 0) {
7468 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
7469 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7471 setkeybinding(mod
, ks
, kfid
, NULL
);
7476 /* search by key function name */
7477 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
7478 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
7479 SWM_FUNCNAME_LEN
) == 0) {
7480 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
7481 "keyfunc\n", selector
);
7482 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7483 setkeybinding(mod
, ks
, kfid
, NULL
);
7489 /* search by custom spawn name */
7490 if ((sp
= spawn_find(selector
)) != NULL
) {
7491 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
7492 "spawn\n", selector
);
7493 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7494 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
7500 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
7507 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
7508 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
7509 setkeybinding(MODKEY_SHIFT
, XK_b
, KF_BAR_TOGGLE_WS
,NULL
);
7510 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
7511 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
7512 setkeybinding(MODKEY_SHIFT
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
7513 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
7514 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
7515 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
7516 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
7517 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
7518 setkeybinding(MODKEY_SHIFT
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
7519 setkeybinding(MODKEY
, XK_u
, KF_FOCUS_URGENT
,NULL
);
7520 setkeybinding(MODKEY
, XK_e
, KF_MAXIMIZE_TOGGLE
,NULL
);
7521 setkeybinding(MODKEY_SHIFT
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
7522 setkeybinding(MODKEY_SHIFT
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
7523 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
7524 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
7525 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
7526 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
7527 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
7528 setkeybinding(MODKEY_SHIFT
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
7529 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
7530 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
7531 setkeybinding(MODKEY_SHIFT
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
7532 setkeybinding(MODKEY_SHIFT
, XK_KP_End
, KF_MVRG_1
, NULL
);
7533 setkeybinding(MODKEY_SHIFT
, XK_KP_Down
, KF_MVRG_2
, NULL
);
7534 setkeybinding(MODKEY_SHIFT
, XK_KP_Next
, KF_MVRG_3
, NULL
);
7535 setkeybinding(MODKEY_SHIFT
, XK_KP_Left
, KF_MVRG_4
, NULL
);
7536 setkeybinding(MODKEY_SHIFT
, XK_KP_Begin
, KF_MVRG_5
, NULL
);
7537 setkeybinding(MODKEY_SHIFT
, XK_KP_Right
, KF_MVRG_6
, NULL
);
7538 setkeybinding(MODKEY_SHIFT
, XK_KP_Home
, KF_MVRG_7
, NULL
);
7539 setkeybinding(MODKEY_SHIFT
, XK_KP_Up
, KF_MVRG_8
, NULL
);
7540 setkeybinding(MODKEY_SHIFT
, XK_KP_Prior
, KF_MVRG_9
, NULL
);
7541 setkeybinding(MODKEY_SHIFT
, XK_1
, KF_MVWS_1
, NULL
);
7542 setkeybinding(MODKEY_SHIFT
, XK_2
, KF_MVWS_2
, NULL
);
7543 setkeybinding(MODKEY_SHIFT
, XK_3
, KF_MVWS_3
, NULL
);
7544 setkeybinding(MODKEY_SHIFT
, XK_4
, KF_MVWS_4
, NULL
);
7545 setkeybinding(MODKEY_SHIFT
, XK_5
, KF_MVWS_5
, NULL
);
7546 setkeybinding(MODKEY_SHIFT
, XK_6
, KF_MVWS_6
, NULL
);
7547 setkeybinding(MODKEY_SHIFT
, XK_7
, KF_MVWS_7
, NULL
);
7548 setkeybinding(MODKEY_SHIFT
, XK_8
, KF_MVWS_8
, NULL
);
7549 setkeybinding(MODKEY_SHIFT
, XK_9
, KF_MVWS_9
, NULL
);
7550 setkeybinding(MODKEY_SHIFT
, XK_0
, KF_MVWS_10
, NULL
);
7551 setkeybinding(MODKEY_SHIFT
, XK_F1
, KF_MVWS_11
, NULL
);
7552 setkeybinding(MODKEY_SHIFT
, XK_F2
, KF_MVWS_12
, NULL
);
7553 setkeybinding(MODKEY_SHIFT
, XK_F3
, KF_MVWS_13
, NULL
);
7554 setkeybinding(MODKEY_SHIFT
, XK_F4
, KF_MVWS_14
, NULL
);
7555 setkeybinding(MODKEY_SHIFT
, XK_F5
, KF_MVWS_15
, NULL
);
7556 setkeybinding(MODKEY_SHIFT
, XK_F6
, KF_MVWS_16
, NULL
);
7557 setkeybinding(MODKEY_SHIFT
, XK_F7
, KF_MVWS_17
, NULL
);
7558 setkeybinding(MODKEY_SHIFT
, XK_F8
, KF_MVWS_18
, NULL
);
7559 setkeybinding(MODKEY_SHIFT
, XK_F9
, KF_MVWS_19
, NULL
);
7560 setkeybinding(MODKEY_SHIFT
, XK_F10
, KF_MVWS_20
, NULL
);
7561 setkeybinding(MODKEY_SHIFT
, XK_F11
, KF_MVWS_21
, NULL
);
7562 setkeybinding(MODKEY_SHIFT
, XK_F12
, KF_MVWS_22
, NULL
);
7563 setkeybinding(MODKEY_SHIFT
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
7564 setkeybinding(MODKEY_SHIFT
, XK_q
, KF_QUIT
, NULL
);
7565 setkeybinding(MODKEY_SHIFT
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
7566 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
7567 setkeybinding(MODKEY
, XK_KP_End
, KF_RG_1
, NULL
);
7568 setkeybinding(MODKEY
, XK_KP_Down
, KF_RG_2
, NULL
);
7569 setkeybinding(MODKEY
, XK_KP_Next
, KF_RG_3
, NULL
);
7570 setkeybinding(MODKEY
, XK_KP_Left
, KF_RG_4
, NULL
);
7571 setkeybinding(MODKEY
, XK_KP_Begin
, KF_RG_5
, NULL
);
7572 setkeybinding(MODKEY
, XK_KP_Right
, KF_RG_6
, NULL
);
7573 setkeybinding(MODKEY
, XK_KP_Home
, KF_RG_7
, NULL
);
7574 setkeybinding(MODKEY
, XK_KP_Up
, KF_RG_8
, NULL
);
7575 setkeybinding(MODKEY
, XK_KP_Prior
, KF_RG_9
, NULL
);
7576 setkeybinding(MODKEY_SHIFT
, XK_Right
, KF_RG_NEXT
, NULL
);
7577 setkeybinding(MODKEY_SHIFT
, XK_Left
, KF_RG_PREV
, NULL
);
7578 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
7579 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
7580 setkeybinding(MODKEY_SHIFT
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
7581 setkeybinding(MODKEY_SHIFT
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
7582 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
7583 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
7584 setkeybinding(MODKEY_SHIFT
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
7585 setkeybinding(MODKEY_SHIFT
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
7586 setkeybinding(MODKEY_SHIFT
, XK_comma
, KF_STACK_INC
, NULL
);
7587 setkeybinding(MODKEY_SHIFT
, XK_period
, KF_STACK_DEC
, NULL
);
7588 setkeybinding(MODKEY_SHIFT
, XK_space
, KF_STACK_RESET
, NULL
);
7589 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
7590 setkeybinding(MODKEY_SHIFT
, XK_j
, KF_SWAP_NEXT
, NULL
);
7591 setkeybinding(MODKEY_SHIFT
, XK_k
, KF_SWAP_PREV
, NULL
);
7592 setkeybinding(MODKEY_SHIFT
, XK_w
, KF_UNICONIFY
, NULL
);
7593 setkeybinding(MODKEY_SHIFT
, XK_v
, KF_VERSION
, NULL
);
7594 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
7595 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
7596 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
7597 setkeybinding(MODKEY_SHIFT
, XK_x
, KF_WIND_KILL
, NULL
);
7598 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
7599 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
7600 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
7601 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
7602 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
7603 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
7604 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
7605 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
7606 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
7607 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
7608 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
7609 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
7610 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
7611 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
7612 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
7613 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
7614 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
7615 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
7616 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
7617 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
7618 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
7619 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
7620 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
7621 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
7622 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
7623 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
7624 setkeybinding(MODKEY_SHIFT
, XK_Up
, KF_WS_NEXT_MOVE
,NULL
);
7625 setkeybinding(MODKEY_SHIFT
, XK_Down
, KF_WS_PREV_MOVE
,NULL
);
7626 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
7628 setkeybinding(MODKEY_SHIFT
, XK_d
, KF_DUMPWINS
, NULL
);
7638 while (RB_EMPTY(&keys
) == 0) {
7639 kp
= RB_ROOT(&keys
);
7645 setkeymapping(const char *selector
, const char *value
, int flags
)
7647 char *keymapping_file
;
7649 /* suppress unused warnings since vars are needed */
7653 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
7655 keymapping_file
= expand_tilde(value
);
7658 /* load new key bindings; if it fails, revert to default bindings */
7659 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
7664 free(keymapping_file
);
7666 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
7671 updatenumlockmask(void)
7674 xcb_get_modifier_mapping_reply_t
*modmap_r
;
7675 xcb_keycode_t
*modmap
, kc
, *keycode
;
7679 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
7680 xcb_get_modifier_mapping(conn
),
7683 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
7684 for (i
= 0; i
< 8; i
++) {
7685 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
7686 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
7688 keycode
= xcb_key_symbols_get_keycode(syms
,
7692 numlockmask
= (1 << i
);
7699 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
7706 int num_screens
, k
, j
;
7707 unsigned int modifiers
[4];
7708 xcb_keycode_t
*code
;
7710 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
7711 updatenumlockmask();
7714 modifiers
[1] = numlockmask
;
7715 modifiers
[2] = XCB_MOD_MASK_LOCK
;
7716 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
7718 num_screens
= get_screen_count();
7719 for (k
= 0; k
< num_screens
; k
++) {
7720 if (TAILQ_EMPTY(&screens
[k
].rl
))
7722 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
7724 RB_FOREACH(kp
, key_tree
, &keys
) {
7725 /* Skip unused ws binds. */
7726 if ((int)kp
->funcid
> KF_WS_1
+ workspace_limit
- 1 &&
7727 kp
->funcid
<= KF_WS_22
)
7730 /* Skip unused mvws binds. */
7731 if ((int)kp
->funcid
> KF_MVWS_1
+ workspace_limit
- 1 &&
7732 kp
->funcid
<= KF_MVWS_22
)
7735 if ((code
= xcb_key_symbols_get_keycode(syms
,
7737 for (j
= 0; j
< LENGTH(modifiers
); j
++)
7738 xcb_grab_key(conn
, 1,
7740 kp
->mod
| modifiers
[j
],
7741 *code
, XCB_GRAB_MODE_SYNC
,
7742 XCB_GRAB_MODE_SYNC
);
7750 grabbuttons(struct ws_win
*win
)
7752 unsigned int modifiers
[4];
7755 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: win %#x\n", win
->id
);
7756 updatenumlockmask();
7759 modifiers
[1] = numlockmask
;
7760 modifiers
[2] = XCB_MOD_MASK_LOCK
;
7761 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
7763 for (i
= 0; i
< LENGTH(buttons
); i
++)
7764 if (buttons
[i
].action
== client_click
)
7765 for (j
= 0; j
< LENGTH(modifiers
); ++j
)
7766 xcb_grab_button(conn
, 0, win
->id
, BUTTONMASK
,
7767 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
,
7768 XCB_WINDOW_NONE
, XCB_CURSOR_NONE
,
7769 buttons
[i
].button
, buttons
[i
].mask
|
7773 const char *quirkname
[] = {
7774 "NONE", /* config string for "no value" */
7782 "FOCUSONMAP_SINGLE",
7790 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
7791 #define SWM_Q_DELIM "\n|+ \t"
7793 parsequirks(const char *qstr
, uint32_t *quirk
, int *ws
)
7795 char *str
, *cp
, *name
;
7798 if (quirk
== NULL
|| qstr
== NULL
)
7801 if ((cp
= str
= strdup(qstr
)) == NULL
)
7802 err(1, "parsequirks: strdup");
7805 while ((name
= strsep(&cp
, SWM_Q_DELIM
)) != NULL
) {
7807 cp
+= (long)strspn(cp
, SWM_Q_DELIM
);
7809 if (sscanf(name
, "WS[%d]", ws
) == 1) {
7815 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
7816 if (strncasecmp(name
, quirkname
[i
],
7817 SWM_QUIRK_LEN
) == 0) {
7818 DNPRINTF(SWM_D_QUIRK
,
7819 "parsequirks: %s\n", name
);
7825 *quirk
|= 1 << (i
-1);
7829 if (i
>= LENGTH(quirkname
)) {
7830 DNPRINTF(SWM_D_QUIRK
,
7831 "parsequirks: invalid quirk [%s]\n", name
);
7842 quirk_insert(const char *class, const char *instance
, const char *name
,
7843 uint32_t quirk
, int ws
)
7847 bool failed
= false;
7849 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: class: %s, instance: %s, name: %s,"
7850 " value: %u, ws: %d\n", class, instance
, name
, quirk
, ws
);
7852 if ((qp
= malloc(sizeof *qp
)) == NULL
)
7853 err(1, "quirk_insert: malloc");
7855 if ((qp
->class = strdup(class)) == NULL
)
7856 err(1, "quirk_insert: strdup");
7857 if ((qp
->instance
= strdup(instance
)) == NULL
)
7858 err(1, "quirk_insert: strdup");
7859 if ((qp
->name
= strdup(name
)) == NULL
)
7860 err(1, "quirk_insert: strdup");
7862 if (asprintf(&str
, "^%s$", class) == -1)
7863 err(1, "quirk_insert: asprintf");
7864 if (regcomp(&qp
->regex_class
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7865 add_startup_exception("regex failed to compile quirk 'class' "
7866 "field: %s", class);
7869 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7872 if (asprintf(&str
, "^%s$", instance
) == -1)
7873 err(1, "quirk_insert: asprintf");
7874 if (regcomp(&qp
->regex_instance
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7875 add_startup_exception("regex failed to compile quirk 'instance'"
7876 " field: %s", instance
);
7879 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7882 if (asprintf(&str
, "^%s$", name
) == -1)
7883 err(1, "quirk_insert: asprintf");
7884 if (regcomp(&qp
->regex_name
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7885 add_startup_exception("regex failed to compile quirk 'name' "
7889 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7893 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: regex error; skipping.\n");
7898 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
7900 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
7904 quirk_remove(struct quirk
*qp
)
7906 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%u]\n", qp
->class,
7907 qp
->name
, qp
->quirk
);
7909 TAILQ_REMOVE(&quirks
, qp
, entry
);
7912 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
7916 quirk_free(struct quirk
*qp
)
7918 regfree(&qp
->regex_class
);
7919 regfree(&qp
->regex_instance
);
7920 regfree(&qp
->regex_name
);
7932 while ((qp
= TAILQ_FIRST(&quirks
)) != NULL
) {
7938 quirk_replace(struct quirk
*qp
, const char *class, const char *instance
,
7939 const char *name
, uint32_t quirk
, int ws
)
7941 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp
->class,
7942 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
7945 quirk_insert(class, instance
, name
, quirk
, ws
);
7947 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
7951 setquirk(const char *class, const char *instance
, const char *name
,
7952 uint32_t quirk
, int ws
)
7956 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
7957 instance
, name
, quirk
, ws
);
7959 /* Remove/replace existing quirk. */
7960 TAILQ_FOREACH(qp
, &quirks
, entry
) {
7961 if (strcmp(qp
->class, class) == 0 &&
7962 strcmp(qp
->instance
, instance
) == 0 &&
7963 strcmp(qp
->name
, name
) == 0) {
7964 if (quirk
== 0 && ws
== -1)
7967 quirk_replace(qp
, class, instance
, name
, quirk
,
7969 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
7974 /* Only insert if quirk is not NONE or forced ws is set. */
7975 if (quirk
|| ws
!= -1)
7976 quirk_insert(class, instance
, name
, quirk
, ws
);
7978 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
7981 /* Eat '\' in str used to escape square brackets and colon. */
7983 unescape_selector(char *str
)
7987 for (cp
= str
; *str
!= '\0'; ++str
, ++cp
) {
7988 if (*str
== '\\' && (*(str
+ 1) == ':' || *(str
+ 1) == ']' ||
7998 setconfquirk(const char *selector
, const char *value
, int flags
)
8000 char *str
, *cp
, *class;
8001 char *instance
= NULL
, *name
= NULL
;
8002 int retval
, count
= 0, ws
= -1;
8005 /* suppress unused warning since var is needed */
8008 if (selector
== NULL
|| strlen(selector
) == 0)
8011 if ((str
= strdup(selector
)) == NULL
)
8012 err(1, "setconfquirk: strdup");
8014 /* Find non-escaped colon. */
8021 for (++cp
; *cp
!= '\0'; ++cp
) {
8022 if (*cp
== ':' && *(cp
- 1) != '\\') {
8028 unescape_selector(class);
8030 instance
= class + strlen(class) + 1;
8031 unescape_selector(instance
);
8037 name
= instance
+ strlen(instance
) + 1;
8038 unescape_selector(name
);
8043 DNPRINTF(SWM_D_CONF
, "setconfquirk: class: %s, instance: %s, "
8044 "name: %s\n", class, instance
, name
);
8046 if ((retval
= parsequirks(value
, &qrks
, &ws
)) == 0)
8047 setquirk(class, instance
, name
, qrks
, ws
);
8056 setquirk("MPlayer", "xv", ".*",
8057 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
, -1);
8058 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
8060 setquirk("Firefox-bin", "firefox-bin", ".*",
8062 setquirk("Firefox", "Dialog", ".*",
8064 setquirk("Gimp", "gimp", ".*",
8065 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8066 setquirk("XTerm", "xterm", ".*",
8067 SWM_Q_XTERM_FONTADJ
, -1);
8068 setquirk("xine", "Xine Window", ".*",
8069 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8070 setquirk("Xitk", "Xitk Combo", ".*",
8071 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8072 setquirk("xine", "xine Panel", ".*",
8073 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8074 setquirk("Xitk", "Xine Window", ".*",
8075 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8076 setquirk("xine", "xine Video Fullscreen Window", ".*",
8077 SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
, -1);
8078 setquirk("pcb", "pcb", ".*",
8080 setquirk("SDL_App", "SDL_App", ".*",
8081 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
, -1);
8084 /* conf file stuff */
8085 #define SWM_CONF_FILE "spectrwm.conf"
8086 #define SWM_CONF_FILE_OLD "scrotwm.conf"
8090 SWM_S_BAR_AT_BOTTOM
,
8091 SWM_S_BAR_BORDER_WIDTH
,
8094 SWM_S_BAR_ENABLED_WS
,
8099 SWM_S_BOUNDARY_WIDTH
,
8100 SWM_S_CLOCK_ENABLED
,
8103 SWM_S_CYCLE_VISIBLE
,
8105 SWM_S_DISABLE_BORDER
,
8107 SWM_S_FOCUS_CLOSE_WRAP
,
8108 SWM_S_FOCUS_DEFAULT
,
8110 SWM_S_ICONIC_ENABLED
,
8111 SWM_S_JAVA_WORKAROUND
,
8112 SWM_S_MAXIMIZE_HIDE_BAR
,
8113 SWM_S_REGION_PADDING
,
8118 SWM_S_STACK_ENABLED
,
8121 SWM_S_URGENT_COLLAPSE
,
8122 SWM_S_URGENT_ENABLED
,
8123 SWM_S_VERBOSE_LAYOUT
,
8126 SWM_S_WINDOW_CLASS_ENABLED
,
8127 SWM_S_WINDOW_INSTANCE_ENABLED
,
8128 SWM_S_WINDOW_NAME_ENABLED
,
8129 SWM_S_WORKSPACE_CLAMP
,
8130 SWM_S_WORKSPACE_LIMIT
,
8131 SWM_S_WORKSPACE_NAME
,
8135 setconfvalue(const char *selector
, const char *value
, int flags
)
8137 struct workspace
*ws
;
8138 int i
, ws_id
, num_screens
;
8139 char *b
, *str
, *sp
, s
[1024];
8142 case SWM_S_BAR_ACTION
:
8144 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
8145 err(1, "setconfvalue: bar_action");
8147 case SWM_S_BAR_AT_BOTTOM
:
8148 bar_at_bottom
= (atoi(value
) != 0);
8150 case SWM_S_BAR_BORDER_WIDTH
:
8151 bar_border_width
= atoi(value
);
8152 if (bar_border_width
< 0)
8153 bar_border_width
= 0;
8155 case SWM_S_BAR_DELAY
:
8156 /* No longer needed; leave to not break old conf files. */
8158 case SWM_S_BAR_ENABLED
:
8159 bar_enabled
= (atoi(value
) != 0);
8161 case SWM_S_BAR_ENABLED_WS
:
8162 ws_id
= atoi(selector
) - 1;
8163 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8164 errx(1, "setconfvalue: bar_enabled_ws: invalid "
8165 "workspace %d.", ws_id
+ 1);
8167 num_screens
= get_screen_count();
8168 for (i
= 0; i
< num_screens
; i
++) {
8169 ws
= (struct workspace
*)&screens
[i
].ws
;
8170 ws
[ws_id
].bar_enabled
= (atoi(value
) != 0);
8173 case SWM_S_BAR_FONT
:
8175 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
8176 err(1, "setconfvalue: asprintf: failed to allocate "
8177 "memory for bar_fonts.");
8180 /* If already in xft mode, then we are done. */
8181 if (!bar_font_legacy
)
8184 if ((sp
= str
= strdup(value
)) == NULL
)
8185 err(1, "setconfvalue: strdup");
8187 /* If there are any non-XLFD entries, switch to Xft mode. */
8188 while ((b
= strsep(&sp
, ",")) != NULL
) {
8192 bar_font_legacy
= false;
8199 case SWM_S_BAR_FORMAT
:
8201 if ((bar_format
= strdup(value
)) == NULL
)
8202 err(1, "setconfvalue: bar_format");
8204 case SWM_S_BAR_JUSTIFY
:
8205 if (strcmp(value
, "left") == 0)
8206 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
8207 else if (strcmp(value
, "center") == 0)
8208 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
8209 else if (strcmp(value
, "right") == 0)
8210 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
8212 errx(1, "invalid bar_justify");
8214 case SWM_S_BORDER_WIDTH
:
8215 border_width
= atoi(value
);
8216 if (border_width
< 0)
8219 case SWM_S_BOUNDARY_WIDTH
:
8220 boundary_width
= atoi(value
);
8221 if (boundary_width
< 0)
8224 case SWM_S_CLOCK_ENABLED
:
8225 clock_enabled
= (atoi(value
) != 0);
8227 case SWM_S_CLOCK_FORMAT
:
8228 #ifndef SWM_DENY_CLOCK_FORMAT
8230 if ((clock_format
= strdup(value
)) == NULL
)
8231 err(1, "setconfvalue: clock_format");
8234 case SWM_S_CYCLE_EMPTY
:
8235 cycle_empty
= (atoi(value
) != 0);
8237 case SWM_S_CYCLE_VISIBLE
:
8238 cycle_visible
= (atoi(value
) != 0);
8240 case SWM_S_DIALOG_RATIO
:
8241 dialog_ratio
= atof(value
);
8242 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
8245 case SWM_S_DISABLE_BORDER
:
8246 disable_border
= (atoi(value
) != 0);
8248 case SWM_S_FOCUS_CLOSE
:
8249 if (strcmp(value
, "first") == 0)
8250 focus_close
= SWM_STACK_BOTTOM
;
8251 else if (strcmp(value
, "last") == 0)
8252 focus_close
= SWM_STACK_TOP
;
8253 else if (strcmp(value
, "next") == 0)
8254 focus_close
= SWM_STACK_ABOVE
;
8255 else if (strcmp(value
, "previous") == 0)
8256 focus_close
= SWM_STACK_BELOW
;
8258 errx(1, "focus_close");
8260 case SWM_S_FOCUS_CLOSE_WRAP
:
8261 focus_close_wrap
= (atoi(value
) != 0);
8263 case SWM_S_FOCUS_DEFAULT
:
8264 if (strcmp(value
, "last") == 0)
8265 focus_default
= SWM_STACK_TOP
;
8266 else if (strcmp(value
, "first") == 0)
8267 focus_default
= SWM_STACK_BOTTOM
;
8269 errx(1, "focus_default");
8271 case SWM_S_FOCUS_MODE
:
8272 if (strcmp(value
, "default") == 0)
8273 focus_mode
= SWM_FOCUS_DEFAULT
;
8274 else if (strcmp(value
, "follow") == 0 ||
8275 strcmp(value
, "follow_cursor") == 0)
8276 focus_mode
= SWM_FOCUS_FOLLOW
;
8277 else if (strcmp(value
, "manual") == 0)
8278 focus_mode
= SWM_FOCUS_MANUAL
;
8280 errx(1, "focus_mode");
8282 case SWM_S_ICONIC_ENABLED
:
8283 iconic_enabled
= (atoi(value
) != 0);
8285 case SWM_S_JAVA_WORKAROUND
:
8286 java_workaround
= (atoi(value
) != 0);
8288 case SWM_S_MAXIMIZE_HIDE_BAR
:
8289 maximize_hide_bar
= atoi(value
);
8291 case SWM_S_REGION_PADDING
:
8292 region_padding
= atoi(value
);
8293 if (region_padding
< 0)
8296 case SWM_S_SPAWN_ORDER
:
8297 if (strcmp(value
, "first") == 0)
8298 spawn_position
= SWM_STACK_BOTTOM
;
8299 else if (strcmp(value
, "last") == 0)
8300 spawn_position
= SWM_STACK_TOP
;
8301 else if (strcmp(value
, "next") == 0)
8302 spawn_position
= SWM_STACK_ABOVE
;
8303 else if (strcmp(value
, "previous") == 0)
8304 spawn_position
= SWM_STACK_BELOW
;
8306 errx(1, "spawn_position");
8308 case SWM_S_SPAWN_TERM
:
8309 setconfspawn("term", value
, 0);
8310 setconfspawn("spawn_term", value
, 0);
8313 /* No longer needed; leave to not break old conf files. */
8315 case SWM_S_SS_ENABLED
:
8316 /* No longer needed; leave to not break old conf files. */
8318 case SWM_S_STACK_ENABLED
:
8319 stack_enabled
= (atoi(value
) != 0);
8321 case SWM_S_TERM_WIDTH
:
8322 term_width
= atoi(value
);
8326 case SWM_S_TILE_GAP
:
8327 tile_gap
= atoi(value
);
8329 case SWM_S_URGENT_COLLAPSE
:
8330 urgent_collapse
= (atoi(value
) != 0);
8332 case SWM_S_URGENT_ENABLED
:
8333 urgent_enabled
= (atoi(value
) != 0);
8335 case SWM_S_VERBOSE_LAYOUT
:
8336 verbose_layout
= (atoi(value
) != 0);
8337 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
8339 layouts
[i
].l_string
= fancy_stacker
;
8341 layouts
[i
].l_string
= plain_stacker
;
8344 case SWM_S_WARP_FOCUS
:
8345 warp_focus
= (atoi(value
) != 0);
8347 case SWM_S_WARP_POINTER
:
8348 warp_pointer
= (atoi(value
) != 0);
8350 case SWM_S_WINDOW_CLASS_ENABLED
:
8351 window_class_enabled
= (atoi(value
) != 0);
8353 case SWM_S_WINDOW_INSTANCE_ENABLED
:
8354 window_instance_enabled
= (atoi(value
) != 0);
8356 case SWM_S_WINDOW_NAME_ENABLED
:
8357 window_name_enabled
= (atoi(value
) != 0);
8359 case SWM_S_WORKSPACE_CLAMP
:
8360 workspace_clamp
= (atoi(value
) != 0);
8362 case SWM_S_WORKSPACE_LIMIT
:
8363 workspace_limit
= atoi(value
);
8364 if (workspace_limit
> SWM_WS_MAX
)
8365 workspace_limit
= SWM_WS_MAX
;
8366 else if (workspace_limit
< 1)
8367 workspace_limit
= 1;
8369 ewmh_update_desktops();
8371 case SWM_S_WORKSPACE_NAME
:
8372 if (getenv("SWM_STARTED") != NULL
)
8376 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
8377 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
8379 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8380 errx(1, "setconfvalue: workspace_name: invalid "
8381 "workspace %d.", ws_id
+ 1);
8383 num_screens
= get_screen_count();
8384 for (i
= 0; i
< num_screens
; ++i
) {
8385 ws
= (struct workspace
*)&screens
[i
].ws
;
8387 if (strlen(s
) > 0) {
8388 free(ws
[ws_id
].name
);
8389 if ((ws
[ws_id
].name
= strdup(s
)) == NULL
)
8390 err(1, "setconfvalue: workspace_name.");
8392 ewmh_update_desktop_names();
8393 ewmh_get_desktop_names();
8404 setconfmodkey(const char *selector
, const char *value
, int flags
)
8406 /* suppress unused warnings since vars are needed */
8410 if (strncasecmp(value
, "Mod1", strlen("Mod1")) == 0)
8411 update_modkey(XCB_MOD_MASK_1
);
8412 else if (strncasecmp(value
, "Mod2", strlen("Mod2")) == 0)
8413 update_modkey(XCB_MOD_MASK_2
);
8414 else if (strncasecmp(value
, "Mod3", strlen("Mod3")) == 0)
8415 update_modkey(XCB_MOD_MASK_3
);
8416 else if (strncasecmp(value
, "Mod4", strlen("Mod4")) == 0)
8417 update_modkey(XCB_MOD_MASK_4
);
8418 else if (strncasecmp(value
, "Mod5", strlen("Mod5")) == 0)
8419 update_modkey(XCB_MOD_MASK_5
);
8426 setconfcolor(const char *selector
, const char *value
, int flags
)
8428 int first
, last
, i
= 0, num_screens
;
8430 num_screens
= get_screen_count();
8432 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
8433 if (selector
== NULL
|| strlen(selector
) == 0 ||
8434 (last
= atoi(selector
) - 1) < 0) {
8436 last
= num_screens
- 1;
8441 if (last
>= num_screens
) {
8442 add_startup_exception("invalid screen index: %d out of bounds "
8443 "(maximum %d)", last
+ 1, num_screens
);
8447 for (i
= first
; i
<= last
; ++i
) {
8448 setscreencolor(value
, i
, flags
);
8451 * When setting focus/unfocus colors, we need to also
8452 * set maximize colors to match if they haven't been customized.
8454 if (flags
== SWM_S_COLOR_FOCUS
&&
8455 !screens
[i
].c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].manual
)
8456 setscreencolor(value
, i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
8457 else if (flags
== SWM_S_COLOR_UNFOCUS
&&
8458 !screens
[i
].c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].manual
)
8459 setscreencolor(value
, i
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
8461 screens
[i
].c
[flags
].manual
= 1;
8468 setconfregion(const char *selector
, const char *value
, int flags
)
8470 /* suppress unused warnings since vars are needed */
8474 custom_region(value
);
8479 setautorun(const char *selector
, const char *value
, int flags
)
8483 char *ap
, *sp
, *str
;
8489 /* suppress unused warnings since vars are needed */
8493 if (getenv("SWM_STARTED"))
8497 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
8498 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
8500 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8501 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
8503 sp
= str
= expand_tilde((char *)&s
);
8506 * This is a little intricate
8508 * If the pid already exists we simply reuse it because it means it was
8509 * used before AND not claimed by manage_window. We get away with
8510 * altering it in the parent after INSERT because this can not be a race
8513 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
8516 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
8518 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
8519 err(1, "setautorun: realloc");
8520 a
.argv
[argc
- 1] = ap
;
8524 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
8525 err(1, "setautorun: realloc");
8526 a
.argv
[argc
] = NULL
;
8528 if ((pid
= fork()) == 0) {
8529 spawn(ws_id
, &a
, true);
8538 p
= calloc(1, sizeof *p
);
8541 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
8551 setlayout(const char *selector
, const char *value
, int flags
)
8553 struct workspace
*ws
;
8554 int ws_id
, i
, x
, mg
, ma
, si
, ar
;
8555 int st
= SWM_V_STACK
, num_screens
;
8559 /* suppress unused warnings since vars are needed */
8563 if (getenv("SWM_STARTED"))
8567 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
8568 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
8569 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8570 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8573 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8574 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
8576 if (strcasecmp(s
, "vertical") == 0)
8578 else if (strcasecmp(s
, "vertical_flip") == 0) {
8581 } else if (strcasecmp(s
, "horizontal") == 0)
8583 else if (strcasecmp(s
, "horizontal_flip") == 0) {
8586 } else if (strcasecmp(s
, "fullscreen") == 0)
8589 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8590 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8593 num_screens
= get_screen_count();
8594 for (i
= 0; i
< num_screens
; i
++) {
8595 ws
= (struct workspace
*)&screens
[i
].ws
;
8596 ws
[ws_id
].cur_layout
= &layouts
[st
];
8598 ws
[ws_id
].always_raise
= (ar
!= 0);
8599 if (st
== SWM_MAX_STACK
)
8603 for (x
= 0; x
< abs(mg
); x
++) {
8604 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8605 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
8606 SWM_ARG_ID_MASTERSHRINK
);
8609 for (x
= 0; x
< abs(ma
); x
++) {
8610 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8611 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
8612 SWM_ARG_ID_MASTERDEL
);
8615 for (x
= 0; x
< abs(si
); x
++) {
8616 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8617 si
>= 0 ? SWM_ARG_ID_STACKINC
:
8618 SWM_ARG_ID_STACKDEC
);
8622 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8623 SWM_ARG_ID_FLIPLAYOUT
);
8630 /* config options */
8631 struct config_option
{
8633 int (*func
)(const char*, const char*, int);
8636 struct config_option configopt
[] = {
8637 { "autorun", setautorun
, 0 },
8638 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
8639 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
8640 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
8641 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
8642 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
8643 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
8644 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
8645 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
8646 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
8647 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
8648 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
8649 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
8650 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
8651 { "bind", setconfbinding
, 0 },
8652 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
8653 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
8654 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
8655 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
8656 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
8657 { "color_focus_maximized", setconfcolor
, SWM_S_COLOR_FOCUS_MAXIMIZED
},
8658 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
8659 { "color_unfocus_maximized", setconfcolor
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
},
8660 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
8661 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
8662 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
8663 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
8664 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
8665 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
8666 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
8667 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
8668 { "iconic_enabled", setconfvalue
, SWM_S_ICONIC_ENABLED
},
8669 { "java_workaround", setconfvalue
, SWM_S_JAVA_WORKAROUND
},
8670 { "keyboard_mapping", setkeymapping
, 0 },
8671 { "layout", setlayout
, 0 },
8672 { "maximize_hide_bar", setconfvalue
, SWM_S_MAXIMIZE_HIDE_BAR
},
8673 { "modkey", setconfmodkey
, 0 },
8674 { "program", setconfspawn
, 0 },
8675 { "quirk", setconfquirk
, 0 },
8676 { "region", setconfregion
, 0 },
8677 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
8678 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
8679 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
8680 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
8681 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
8682 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
8683 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
8684 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
8685 { "title_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
}, /* For backwards compat. */
8686 { "title_name_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
}, /* For backwards compat. */
8687 { "urgent_collapse", setconfvalue
, SWM_S_URGENT_COLLAPSE
},
8688 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
8689 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
8690 { "warp_focus", setconfvalue
, SWM_S_WARP_FOCUS
},
8691 { "warp_pointer", setconfvalue
, SWM_S_WARP_POINTER
},
8692 { "window_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
},
8693 { "window_instance_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
},
8694 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
8695 { "workspace_clamp", setconfvalue
, SWM_S_WORKSPACE_CLAMP
},
8696 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
8697 { "name", setconfvalue
, SWM_S_WORKSPACE_NAME
},
8701 _add_startup_exception(const char *fmt
, va_list ap
)
8703 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
8704 warn("%s: asprintf", __func__
);
8708 add_startup_exception(const char *fmt
, ...)
8714 if (startup_exception
)
8717 /* force bar to be enabled due to exception */
8721 _add_startup_exception(fmt
, ap
);
8726 conf_load(const char *filename
, int keymapping
)
8729 char *line
= NULL
, *cp
, *ce
, *optsub
, *optval
= NULL
;
8730 size_t linelen
, lineno
= 0;
8731 int wordlen
, i
, optidx
, count
;
8732 struct config_option
*opt
= NULL
;
8734 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
8736 if (filename
== NULL
) {
8737 warnx("conf_load: no filename");
8741 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
8743 if ((config
= fopen(filename
, "r")) == NULL
) {
8744 warn("conf_load: fopen: %s", filename
);
8748 while (!feof(config
)) {
8752 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
8753 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
8755 err(1, "%s", filename
);
8760 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
8761 if (cp
[0] == '\0') {
8765 /* get config option */
8766 wordlen
= strcspn(cp
, "=[ \t\n");
8768 add_startup_exception("%s: line %zd: no option found",
8773 for (i
= 0; i
< LENGTH(configopt
); i
++) {
8774 opt
= &configopt
[i
];
8775 if (strncasecmp(cp
, opt
->optname
, wordlen
) == 0 &&
8776 (int)strlen(opt
->optname
) == wordlen
) {
8782 add_startup_exception("%s: line %zd: unknown option "
8783 "%.*s", filename
, lineno
, wordlen
, cp
);
8786 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
8787 add_startup_exception("%s: line %zd: invalid option "
8788 "%.*s", filename
, lineno
, wordlen
, cp
);
8792 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
8794 /* get [selector] if any */
8799 /* Get length of selector. */
8800 for (ce
= ++cp
; *ce
!= '\0'; ++ce
) {
8801 /* Find matching (not escaped) bracket. */
8802 if (*ce
== ']' && *(ce
- 1) != '\\') {
8809 add_startup_exception("%s: line %zd: syntax "
8810 "error: unterminated selector", filename
,
8815 /* ce points at ']'; terminate optsub. */
8820 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
8823 if (strlen(optval
) == 0) {
8824 add_startup_exception("%s: line %zd: must supply value "
8825 "to %s", filename
, lineno
,
8826 configopt
[optidx
].optname
);
8829 /* call function to deal with it all */
8830 if (configopt
[optidx
].func(optsub
, optval
,
8831 configopt
[optidx
].funcflags
) != 0) {
8832 add_startup_exception("%s: line %zd: invalid data for "
8833 "%s", filename
, lineno
, configopt
[optidx
].optname
);
8841 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
8847 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
8849 struct ws_win
*parent
, *w
;
8850 struct swm_region
*r
;
8851 struct workspace
*ws
;
8852 xcb_icccm_wm_hints_t wmh
;
8854 parent
= find_window(win
->transient
);
8856 parent
->focus_child
= win
;
8858 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
8859 " for %#x trans %#x\n", win
->id
, win
->transient
);
8861 r
= root_to_region(win
->s
->root
, SWM_CK_ALL
);
8863 /* parent doen't exist in our window list */
8864 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
8865 if (xcb_icccm_get_wm_hints_reply(conn
,
8866 xcb_icccm_get_wm_hints(conn
, w
->id
),
8868 warnx("can't get hints for %#x", w
->id
);
8872 if (win
->hints
.window_group
!= wmh
.window_group
)
8875 w
->focus_child
= win
;
8876 win
->transient
= w
->id
;
8878 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
8879 "transient to %#x\n", win
->transient
);
8886 window_get_pid(xcb_window_t win
)
8891 xcb_get_property_cookie_t pc
;
8892 xcb_get_property_reply_t
*pr
;
8894 apid
= get_atom_from_string("_NET_WM_PID");
8895 if (apid
== XCB_ATOM_NONE
)
8898 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
8899 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
8902 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
8907 if (pr
->type
== apid
&& pr
->format
== 32)
8908 ret
= *((pid_t
*)xcb_get_property_value(pr
));
8914 apid
= get_atom_from_string("_SWM_PID");
8915 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
8917 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
8920 if (pr
->type
!= apid
) {
8925 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
8932 get_swm_ws(xcb_window_t id
)
8938 xcb_get_property_reply_t
*gpr
;
8940 gpr
= xcb_get_property_reply(conn
,
8941 xcb_get_property(conn
, 0, id
, a_swm_ws
,
8942 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
8947 proplen
= xcb_get_property_value_length(gpr
);
8949 prop
= malloc(proplen
+ 1);
8952 xcb_get_property_value(gpr
),
8954 prop
[proplen
] = '\0';
8961 DNPRINTF(SWM_D_PROP
, "get_swm_ws: _SWM_WS: %s\n", prop
);
8962 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
8964 DNPRINTF(SWM_D_PROP
, "get_swm_ws: win #%s: %s",
8974 get_ws_idx(struct ws_win
*win
)
8976 xcb_get_property_reply_t
*gpr
;
8982 gpr
= xcb_get_property_reply(conn
,
8983 xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
8984 XCB_ATOM_CARDINAL
, 0, 1),
8987 if (gpr
->type
== XCB_ATOM_CARDINAL
&& gpr
->format
== 32)
8988 ws_idx
= *((int *)xcb_get_property_value(gpr
));
8992 if (ws_idx
== -1 && !(win
->quirks
& SWM_Q_IGNORESPAWNWS
))
8993 ws_idx
= get_swm_ws(win
->id
);
8995 if (ws_idx
> workspace_limit
- 1 || ws_idx
< -1)
8998 DNPRINTF(SWM_D_PROP
, "get_ws_idx: win %#x, ws_idx: %d\n", win
->id
,
9005 manage_window(xcb_window_t id
, int spawn_pos
, bool mapped
)
9007 struct ws_win
*win
, *ww
;
9008 struct swm_region
*r
;
9011 xcb_get_geometry_reply_t
*gr
;
9012 xcb_window_t trans
= XCB_WINDOW_NONE
;
9013 uint32_t i
, wa
[2], new_flags
;
9014 int ws_idx
, force_ws
= -1;
9015 char *class, *instance
, *name
;
9017 if ((win
= find_window(id
)) != NULL
) {
9018 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x already "
9019 "managed; skipping.)\n", id
);
9020 return (win
); /* Already managed. */
9023 /* See if window is on the unmanaged list. */
9024 if ((win
= find_unmanaged_window(id
)) != NULL
) {
9025 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x found on "
9026 "unmanaged list.\n", id
);
9027 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
9030 set_child_transient(win
, &trans
);
9034 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is new.\n", id
);
9037 /* Try to get initial window geometry. */
9038 gr
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, id
), NULL
);
9040 DNPRINTF(SWM_D_MISC
, "manage_window: get geometry failed.\n");
9044 /* Create and initialize ws_win object. */
9045 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
9046 err(1, "manage_window: calloc: failed to allocate memory for "
9051 /* Figureout which region the window belongs to. */
9052 r
= root_to_region(gr
->root
, SWM_CK_ALL
);
9054 /* Ignore window border if there is one. */
9055 WIDTH(win
) = gr
->width
;
9056 HEIGHT(win
) = gr
->height
;
9057 X(win
) = gr
->x
+ gr
->border_width
- border_width
;
9058 Y(win
) = gr
->y
+ gr
->border_width
- border_width
;
9059 win
->bordered
= true;
9060 win
->mapped
= mapped
;
9061 win
->s
= r
->s
; /* this never changes */
9065 /* Select which X events to monitor and set border pixel color. */
9066 wa
[0] = win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
9067 wa
[1] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
9068 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
9070 wa
[1] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
9073 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
|
9074 XCB_CW_EVENT_MASK
, wa
);
9076 /* Get WM_SIZE_HINTS. */
9077 xcb_icccm_get_wm_normal_hints_reply(conn
,
9078 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
9082 xcb_icccm_get_wm_hints_reply(conn
,
9083 xcb_icccm_get_wm_hints(conn
, win
->id
),
9086 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
9087 xcb_icccm_get_wm_transient_for_reply(conn
,
9088 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
9091 win
->transient
= trans
;
9092 set_child_transient(win
, &win
->transient
);
9095 /* Get WM_PROTOCOLS. */
9096 get_wm_protocols(win
);
9098 /* Set initial quirks based on EWMH. */
9099 ewmh_autoquirk(win
);
9101 /* Determine initial quirks. */
9102 xcb_icccm_get_wm_class_reply(conn
,
9103 xcb_icccm_get_wm_class(conn
, win
->id
),
9106 class = win
->ch
.class_name
? win
->ch
.class_name
: "";
9107 instance
= win
->ch
.instance_name
? win
->ch
.instance_name
: "";
9108 name
= get_win_name(win
->id
);
9110 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, instance: %s, "
9111 "name: %s\n", class, instance
, name
);
9113 /* java is retarded so treat it special */
9114 if (strstr(instance
, "sun-awt")) {
9115 DNPRINTF(SWM_D_CLASS
, "manage_window: java window detected.\n");
9119 TAILQ_FOREACH(qp
, &quirks
, entry
) {
9120 if (regexec(&qp
->regex_class
, class, 0, NULL
, 0) == 0 &&
9121 regexec(&qp
->regex_instance
, instance
, 0, NULL
, 0) == 0 &&
9122 regexec(&qp
->regex_name
, name
, 0, NULL
, 0) == 0) {
9123 DNPRINTF(SWM_D_CLASS
, "manage_window: matched "
9124 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp
->class,
9125 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
9126 win
->quirks
= qp
->quirk
;
9127 if (qp
->ws
>= 0 && qp
->ws
< workspace_limit
)
9134 /* Reset font sizes (the bruteforce way; no default keybinding). */
9135 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
9136 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9137 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
9138 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9139 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
9142 /* Figure out which workspace the window belongs to. */
9143 if (!(win
->quirks
& SWM_Q_IGNOREPID
) &&
9144 (p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
9145 win
->ws
= &r
->s
->ws
[p
->ws
];
9146 TAILQ_REMOVE(&pidlist
, p
, entry
);
9149 } else if ((ws_idx
= get_ws_idx(win
)) != -1 &&
9151 /* _SWM_WS is set; use that. */
9152 win
->ws
= &r
->s
->ws
[ws_idx
];
9153 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
9154 /* Launch transients in the same ws as parent. */
9161 win
->ws
= &r
->s
->ws
[force_ws
];
9163 /* Set the _NET_WM_DESKTOP atom. */
9164 DNPRINTF(SWM_D_PROP
, "manage_window: set _NET_WM_DESKTOP: %d\n",
9166 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
9167 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &win
->ws
->idx
);
9169 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
9170 xcb_delete_property(conn
, win
->id
, a_swm_ws
);
9172 /* WS must already be set for this to work. */
9173 store_float_geom(win
);
9175 /* Make sure window is positioned inside its region, if its active. */
9177 region_containment(win
, r
, SWM_CW_ALLSIDES
|
9178 SWM_CW_HARDBOUNDARY
);
9183 /* Figure out where to insert the window in the workspace list. */
9184 if (trans
&& (ww
= find_window(trans
)))
9185 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
9186 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_ABOVE
)
9187 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
9189 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_BELOW
)
9190 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
9191 else switch (spawn_pos
) {
9194 case SWM_STACK_ABOVE
:
9195 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
9197 case SWM_STACK_BOTTOM
:
9198 case SWM_STACK_BELOW
:
9199 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
9202 ewmh_update_client_list();
9204 TAILQ_INSERT_HEAD(&win
->ws
->stack
, win
, stack_entry
);
9207 /* Get/apply initial _NET_WM_STATE */
9208 ewmh_get_wm_state(win
);
9211 new_flags
= win
->ewmh_flags
;
9213 if (win
->quirks
& SWM_Q_FLOAT
)
9214 new_flags
|= EWMH_F_ABOVE
;
9216 if (win
->quirks
& SWM_Q_ANYWHERE
)
9217 new_flags
|= SWM_F_MANUAL
;
9219 ewmh_apply_flags(win
, new_flags
);
9220 ewmh_update_wm_state(win
);
9222 /* Set initial _NET_WM_ALLOWED_ACTIONS */
9223 ewmh_update_actions(win
);
9227 DNPRINTF(SWM_D_MISC
, "manage_window: done. win %#x, (x,y) w x h: "
9228 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win
->id
,
9229 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
9230 YESNO(ICONIC(win
)), win
->transient
);
9236 free_window(struct ws_win
*win
)
9238 DNPRINTF(SWM_D_MISC
, "free_window: win %#x\n", WINID(win
));
9243 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
9246 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
9249 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
9253 unmanage_window(struct ws_win
*win
)
9255 struct ws_win
*parent
;
9257 DNPRINTF(SWM_D_MISC
, "unmanage_window: win %#x\n", WINID(win
));
9263 parent
= find_window(win
->transient
);
9265 parent
->focus_child
= NULL
;
9268 TAILQ_REMOVE(&win
->ws
->stack
, win
, stack_entry
);
9269 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
9270 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
9272 ewmh_update_client_list();
9276 expose(xcb_expose_event_t
*e
)
9279 struct swm_region
*r
;
9281 DNPRINTF(SWM_D_EVENT
, "expose: win %#x\n", e
->window
);
9283 num_screens
= get_screen_count();
9284 for (i
= 0; i
< num_screens
; i
++)
9285 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9286 if (e
->window
== WINID(r
->bar
))
9294 focusin(xcb_focus_in_event_t
*e
)
9296 DNPRINTF(SWM_D_EVENT
, "focusin: win %#x, mode: %s(%u), "
9297 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
9298 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
9302 focusout(xcb_focus_out_event_t
*e
)
9304 DNPRINTF(SWM_D_EVENT
, "focusout: win %#x, mode: %s(%u), "
9305 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
9306 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
9311 keypress(xcb_key_press_event_t
*e
)
9313 xcb_keysym_t keysym
;
9316 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
9318 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
9319 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9320 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
9321 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
9322 e
->child
, e
->state
, YESNO(e
->same_screen
));
9324 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
9327 last_event_time
= e
->time
;
9329 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
9330 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
),
9331 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
9332 else if (keyfuncs
[kp
->funcid
].func
)
9333 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
, SWM_CK_ALL
),
9334 &(keyfuncs
[kp
->funcid
].args
));
9337 /* Unfreeze grab events. */
9338 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
, e
->time
);
9341 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
9345 buttonpress(xcb_button_press_event_t
*e
)
9347 struct ws_win
*win
= NULL
;
9348 struct swm_region
*r
, *old_r
;
9350 bool handled
= false;
9352 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): %#x (%d,%d), "
9353 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
9354 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
9355 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9356 e
->state
, YESNO(e
->same_screen
));
9358 if (e
->event
== e
->root
) {
9359 if (e
->child
!= 0) {
9360 win
= find_window(e
->child
);
9361 /* Pass ButtonPress to window if it isn't managed. */
9365 /* Focus on empty region */
9366 /* If no windows on region if its empty. */
9367 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
9369 DNPRINTF(SWM_D_EVENT
, "buttonpress: "
9370 "NULL region; ignoring.\n");
9374 if (TAILQ_EMPTY(&r
->ws
->winlist
)) {
9375 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
9376 if (old_r
&& old_r
!= r
)
9377 unfocus_win(old_r
->ws
->focus
);
9379 xcb_set_input_focus(conn
,
9380 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
9382 /* Clear bar since empty. */
9390 win
= find_window(e
->event
);
9396 last_event_time
= e
->time
;
9398 focus_win(get_focus_magic(win
));
9400 for (i
= 0; i
< LENGTH(buttons
); i
++)
9401 if (client_click
== buttons
[i
].action
&& buttons
[i
].func
&&
9402 buttons
[i
].button
== e
->detail
&&
9403 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
)) {
9404 buttons
[i
].func(win
, &buttons
[i
].args
);
9410 DNPRINTF(SWM_D_EVENT
, "buttonpress: passing to window.\n");
9411 /* Replay event to event window */
9412 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
9414 DNPRINTF(SWM_D_EVENT
, "buttonpress: handled.\n");
9415 /* Unfreeze grab events. */
9416 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
9424 print_win_geom(xcb_window_t w
)
9426 xcb_get_geometry_reply_t
*wa
;
9428 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
9430 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: %#x\n",
9435 DNPRINTF(SWM_D_MISC
, "print_win_geom: win %#x, root: %#x, "
9436 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
9437 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
9446 get_stack_mode_name(uint8_t mode
)
9451 case XCB_STACK_MODE_ABOVE
:
9454 case XCB_STACK_MODE_BELOW
:
9457 case XCB_STACK_MODE_TOP_IF
:
9460 case XCB_STACK_MODE_BOTTOM_IF
:
9463 case XCB_STACK_MODE_OPPOSITE
:
9475 configurerequest(xcb_configure_request_event_t
*e
)
9478 struct swm_region
*r
= NULL
;
9480 uint32_t wc
[7] = {0};
9484 if ((win
= find_window(e
->window
)) == NULL
)
9485 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
9489 if (swm_debug
& SWM_D_EVENT
) {
9490 print_win_geom(e
->window
);
9492 DNPRINTF(SWM_D_EVENT
, "configurerequest: win %#x, "
9493 "parent: %#x, new: %s, value_mask: %u { ", e
->window
,
9494 e
->parent
, YESNO(new), e
->value_mask
);
9495 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
9496 DPRINTF("X: %d ", e
->x
);
9497 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
9498 DPRINTF("Y: %d ", e
->y
);
9499 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
9500 DPRINTF("W: %u ", e
->width
);
9501 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
9502 DPRINTF("H: %u ", e
->height
);
9503 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
9504 DPRINTF("Border: %u ", e
->border_width
);
9505 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
9506 DPRINTF("Sibling: %#x ", e
->sibling
);
9507 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
9508 DPRINTF("StackMode: %s(%u) ",
9509 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
9515 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
9516 mask
|= XCB_CONFIG_WINDOW_X
;
9519 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
9520 mask
|= XCB_CONFIG_WINDOW_Y
;
9523 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
9524 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
9527 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
9528 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
9529 wc
[i
++] = e
->height
;
9531 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
9532 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
9533 wc
[i
++] = e
->border_width
;
9535 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
9536 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
9537 wc
[i
++] = e
->sibling
;
9539 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
9540 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
9541 wc
[i
++] = e
->stack_mode
;
9545 xcb_configure_window(conn
, e
->window
, mask
, wc
);
9548 } else if ((!MANUAL(win
) || win
->quirks
& SWM_Q_ANYWHERE
) &&
9549 !FULLSCREEN(win
) && !MAXIMIZED(win
)) {
9552 else if (win
->ws
->old_r
)
9555 /* windows are centered unless ANYWHERE quirk is set. */
9556 if (win
->quirks
& SWM_Q_ANYWHERE
) {
9557 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
9558 win
->g_float
.x
= e
->x
;
9560 win
->g_float
.x
-= X(r
);
9563 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
9564 win
->g_float
.y
= e
->y
;
9566 win
->g_float
.y
-= Y(r
);
9570 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
9571 win
->g_float
.w
= e
->width
;
9573 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
9574 win
->g_float
.h
= e
->height
;
9576 win
->g_floatvalid
= true;
9578 if (!MAXIMIZED(win
) && !FULLSCREEN(win
) &&
9579 (TRANS(win
) || (ABOVE(win
) &&
9580 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]))) {
9581 WIDTH(win
) = win
->g_float
.w
;
9582 HEIGHT(win
) = win
->g_float
.h
;
9585 update_floater(win
);
9600 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
9604 configurenotify(xcb_configure_notify_event_t
*e
)
9608 DNPRINTF(SWM_D_EVENT
, "configurenotify: win %#x, event win: %#x, "
9609 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
9610 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
9611 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
9612 YESNO(e
->override_redirect
));
9614 win
= find_window(e
->window
);
9616 xcb_icccm_get_wm_normal_hints_reply(conn
,
9617 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
9620 if (font_adjusted
) {
9628 destroynotify(xcb_destroy_notify_event_t
*e
)
9632 DNPRINTF(SWM_D_EVENT
, "destroynotify: win %#x\n", e
->window
);
9634 if ((win
= find_window(e
->window
))) {
9635 /* Managed window cleanup. */
9636 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
9637 /* If focused, focus on something else. */
9638 if (win
== win
->ws
->focus
)
9639 win
->ws
->focus_pending
= get_focus_prev(win
);
9642 unmanage_window(win
);
9645 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
9646 if (win
->ws
->focus_pending
) {
9647 focus_win(win
->ws
->focus_pending
);
9648 win
->ws
->focus_pending
= NULL
;
9649 } else if (win
== win
->ws
->focus
) {
9650 xcb_set_input_focus(conn
,
9651 XCB_INPUT_FOCUS_PARENT
, win
->ws
->r
->id
,
9659 win
= find_unmanaged_window(e
->window
);
9663 /* unmanage_window() puts win into unmanaged list. */
9664 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
9668 DNPRINTF(SWM_D_EVENT
, "destroynotify: done.\n");
9673 get_notify_detail_label(uint8_t detail
)
9678 case XCB_NOTIFY_DETAIL_ANCESTOR
:
9681 case XCB_NOTIFY_DETAIL_VIRTUAL
:
9684 case XCB_NOTIFY_DETAIL_INFERIOR
:
9687 case XCB_NOTIFY_DETAIL_NONLINEAR
:
9688 label
= "Nonlinear";
9690 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
9691 label
= "NonlinearVirtual";
9693 case XCB_NOTIFY_DETAIL_POINTER
:
9696 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
9697 label
= "PointerRoot";
9699 case XCB_NOTIFY_DETAIL_NONE
:
9710 get_notify_mode_label(uint8_t mode
)
9715 case XCB_NOTIFY_MODE_NORMAL
:
9718 case XCB_NOTIFY_MODE_GRAB
:
9721 case XCB_NOTIFY_MODE_UNGRAB
:
9724 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
9725 label
= "WhileGrabbed";
9736 enternotify(xcb_enter_notify_event_t
*e
)
9739 struct swm_region
*r
;
9741 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): %#x "
9742 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9743 "child: %#x, same_screen_focus: %s, state: %d\n",
9744 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9745 get_notify_mode_label(e
->mode
), e
->mode
,
9746 get_notify_detail_label(e
->detail
), e
->detail
,
9747 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9748 YESNO(e
->same_screen_focus
), e
->state
);
9750 if (focus_mode
== SWM_FOCUS_MANUAL
&&
9751 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
9752 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
9756 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
9757 e
->mode
== XCB_NOTIFY_MODE_UNGRAB
) {
9758 DNPRINTF(SWM_D_EVENT
, "enternotify: ungrab; ignoring.\n");
9762 last_event_time
= e
->time
;
9764 if ((win
= find_window(e
->event
)) == NULL
) {
9765 if (e
->event
== e
->root
) {
9766 /* If no windows on pointer region, then focus root. */
9767 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
9769 DNPRINTF(SWM_D_EVENT
, "enternotify: "
9770 "NULL region; ignoring.\n");
9776 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
9781 if (e
->mode
== XCB_NOTIFY_MODE_NORMAL
&&
9782 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
9783 DNPRINTF(SWM_D_EVENT
, "enternotify: entering from "
9784 "inferior; ignoring\n");
9788 focus_win(get_focus_magic(win
));
9796 leavenotify(xcb_leave_notify_event_t
*e
)
9798 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): %#x "
9799 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9800 "child: %#x, same_screen_focus: %s, state: %d\n",
9801 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9802 get_notify_mode_label(e
->mode
), e
->mode
,
9803 get_notify_detail_label(e
->detail
), e
->detail
,
9804 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9805 YESNO(e
->same_screen_focus
), e
->state
);
9810 mapnotify(xcb_map_notify_event_t
*e
)
9812 struct ws_win
*win
, *parent
= NULL
;
9813 struct workspace
*ws
;
9815 DNPRINTF(SWM_D_EVENT
, "mapnotify: win %#x\n", e
->window
);
9817 if ((win
= manage_window(e
->window
, spawn_position
, true)) == NULL
)
9821 /* Need to know if win was mapped due to ws switch. */
9822 if (ws
->state
== SWM_WS_STATE_MAPPED
) {
9823 if (ws
->focus_pending
&& TRANS(ws
->focus_pending
))
9824 parent
= find_window(win
->transient
);
9826 /* If window's parent is maximized, don't clear it. */
9827 if ((parent
== NULL
) || !MAXIMIZED(parent
))
9828 if (clear_maximized(ws
) > 0)
9833 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
9835 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
9836 if (ws
->focus_pending
== win
) {
9838 ws
->focus_pending
= NULL
;
9839 center_pointer(win
->ws
->r
);
9848 mappingnotify(xcb_mapping_notify_event_t
*e
)
9851 int i
, j
, num_screens
;
9853 xcb_refresh_keyboard_mapping(syms
, e
);
9855 if (e
->request
== XCB_MAPPING_KEYBOARD
) {
9858 /* Regrab buttons on all managed windows. */
9859 num_screens
= get_screen_count();
9860 for (i
= 0; i
< num_screens
; i
++)
9861 for (j
= 0; j
< workspace_limit
; j
++)
9862 TAILQ_FOREACH(w
, &screens
[i
].ws
[j
].winlist
,
9869 maprequest(xcb_map_request_event_t
*e
)
9871 struct ws_win
*win
, *w
= NULL
;
9872 xcb_get_window_attributes_reply_t
*war
;
9874 DNPRINTF(SWM_D_EVENT
, "maprequest: win %#x\n",
9877 war
= xcb_get_window_attributes_reply(conn
,
9878 xcb_get_window_attributes(conn
, e
->window
),
9881 DNPRINTF(SWM_D_EVENT
, "maprequest: window lost.\n");
9885 if (war
->override_redirect
) {
9886 DNPRINTF(SWM_D_EVENT
, "maprequest: override_redirect; "
9891 win
= manage_window(e
->window
, spawn_position
,
9892 (war
->map_state
== XCB_MAP_STATE_VIEWABLE
));
9896 /* The new window should get focus; prepare. */
9897 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
9898 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
) &&
9899 (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
9900 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
9901 win
->hints
.input
))) {
9902 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
9903 /* See if other wins of same type are already mapped. */
9904 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
9905 if (w
== win
|| !w
->mapped
)
9908 if (w
->ch
.class_name
&&
9909 win
->ch
.class_name
&&
9910 strcmp(w
->ch
.class_name
,
9911 win
->ch
.class_name
) == 0 &&
9912 w
->ch
.instance_name
&&
9913 win
->ch
.instance_name
&&
9914 strcmp(w
->ch
.instance_name
,
9915 win
->ch
.instance_name
) == 0)
9921 win
->ws
->focus_pending
= get_focus_magic(win
);
9924 /* All windows need to be mapped if they are in the current workspace.*/
9928 /* Ignore EnterNotify to handle the mapnotify without interference. */
9929 if (focus_mode
== SWM_FOCUS_DEFAULT
)
9930 event_drain(XCB_ENTER_NOTIFY
);
9933 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
9937 motionnotify(xcb_motion_notify_event_t
*e
)
9939 struct swm_region
*r
= NULL
;
9942 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): %#x "
9943 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9944 "child: %#x, same_screen_focus: %s, state: %d\n",
9945 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9946 get_notify_detail_label(e
->detail
), e
->detail
,
9947 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9948 YESNO(e
->same_screen
), e
->state
);
9950 last_event_time
= e
->time
;
9952 if (focus_mode
== SWM_FOCUS_MANUAL
)
9955 num_screens
= get_screen_count();
9956 for (i
= 0; i
< num_screens
; i
++)
9957 if (screens
[i
].root
== e
->root
)
9960 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9961 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
9962 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
9970 get_atom_name(xcb_atom_t atom
)
9973 #ifdef SWM_DEBUG_ATOM_NAMES
9975 * This should be disabled during most debugging since
9976 * xcb_get_* causes an xcb_flush.
9979 xcb_get_atom_name_reply_t
*r
;
9981 r
= xcb_get_atom_name_reply(conn
,
9982 xcb_get_atom_name(conn
, atom
),
9985 len
= xcb_get_atom_name_name_length(r
);
9987 name
= malloc(len
+ 1);
9989 memcpy(name
, xcb_get_atom_name_name(r
), len
);
10003 propertynotify(xcb_property_notify_event_t
*e
)
10005 struct ws_win
*win
;
10006 struct workspace
*ws
;
10010 name
= get_atom_name(e
->atom
);
10011 DNPRINTF(SWM_D_EVENT
, "propertynotify: win %#x, atom: %s(%u), "
10012 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
10016 win
= find_window(e
->window
);
10022 last_event_time
= e
->time
;
10024 if (e
->atom
== a_state
) {
10025 /* State just changed, make sure it gets focused if mapped. */
10026 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
10027 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
10029 ws
->focus_pending
== win
) {
10030 focus_win(ws
->focus_pending
);
10031 ws
->focus_pending
= NULL
;
10035 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
10036 e
->atom
== XCB_ATOM_WM_NAME
) {
10038 } else if (e
->atom
== a_prot
) {
10039 get_wm_protocols(win
);
10046 unmapnotify(xcb_unmap_notify_event_t
*e
)
10048 struct ws_win
*win
;
10049 struct workspace
*ws
;
10051 DNPRINTF(SWM_D_EVENT
, "unmapnotify: win %#x\n", e
->window
);
10053 /* If we aren't managing the window, then ignore. */
10054 win
= find_window(e
->window
);
10055 if (win
== NULL
|| win
->id
!= e
->window
)
10058 /* Do nothing if already withdrawn. */
10059 if (!win
->mapped
&& !ICONIC(win
))
10063 win
->mapped
= false;
10065 /* If win was focused, make sure to focus on something else. */
10066 if (win
== ws
->focus
) {
10067 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10068 ws
->focus_pending
= get_focus_prev(win
);
10069 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
10070 "focus_pending: %#x\n",
10071 WINID(ws
->focus_pending
));
10079 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
10082 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
10083 unmanage_window(win
);
10089 /* Update focus if ws is active. */
10090 if (WS_FOCUSED(ws
)) {
10091 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
10092 focus_win(get_pointer_win(ws
->r
->s
->root
));
10093 } else if (ws
->focus_pending
) {
10094 focus_win(ws
->focus_pending
);
10095 ws
->focus_pending
= NULL
;
10096 } else if (ws
->focus
== NULL
) {
10097 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
10098 ws
->r
->id
, XCB_CURRENT_TIME
);
10102 center_pointer(ws
->r
);
10108 get_source_type_label(uint32_t type
)
10113 case EWMH_SOURCE_TYPE_NONE
:
10116 case EWMH_SOURCE_TYPE_NORMAL
:
10119 case EWMH_SOURCE_TYPE_OTHER
:
10131 clientmessage(xcb_client_message_event_t
*e
)
10133 struct ws_win
*win
;
10134 struct swm_region
*r
= NULL
;
10137 int num_screens
, i
;
10138 xcb_map_request_event_t mre
;
10142 name
= get_atom_name(e
->type
);
10143 DNPRINTF(SWM_D_EVENT
, "clientmessage: win %#x, atom: %s(%u)\n",
10144 e
->window
, name
, e
->type
);
10148 if (e
->type
== ewmh
[_NET_CURRENT_DESKTOP
].atom
) {
10149 num_screens
= get_screen_count();
10150 for (i
= 0; i
< num_screens
; i
++)
10151 if (screens
[i
].root
== e
->window
) {
10152 r
= screens
[i
].r_focus
;
10156 if (r
&& e
->data
.data32
[0] < (uint32_t)workspace_limit
) {
10157 a
.id
= e
->data
.data32
[0];
10165 win
= find_window(e
->window
);
10167 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
10168 /* Manage the window with maprequest. */
10169 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
10170 "unmanaged window.\n");
10171 mre
.window
= e
->window
;
10177 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
10178 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW, "
10179 "source_type: %s(%d)\n",
10180 get_source_type_label(e
->data
.data32
[0]),
10181 e
->data
.data32
[0]);
10184 * Allow focus changes that are a result of direct user
10185 * action and from applications that use the old EWMH spec.
10187 if (e
->data
.data32
[0] != EWMH_SOURCE_TYPE_NORMAL
||
10188 win
->quirks
& SWM_Q_OBEYAPPFOCUSREQ
) {
10189 if (WS_FOCUSED(win
->ws
))
10192 win
->ws
->focus_pending
= win
;
10194 } else if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
10195 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
10196 if (win
->can_delete
)
10197 client_msg(win
, a_delete
, 0);
10199 xcb_kill_client(conn
, win
->id
);
10200 } else if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
10201 DNPRINTF(SWM_D_EVENT
,
10202 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
10204 if (e
->data
.data32
[0] & (1<<8)) /* x */
10205 X(win
) = e
->data
.data32
[1];
10206 if (e
->data
.data32
[0] & (1<<9)) /* y */
10207 Y(win
) = e
->data
.data32
[2];
10208 if (e
->data
.data32
[0] & (1<<10)) /* width */
10209 WIDTH(win
) = e
->data
.data32
[3];
10210 if (e
->data
.data32
[0] & (1<<11)) /* height */
10211 HEIGHT(win
) = e
->data
.data32
[4];
10213 update_window(win
);
10215 /* Notify no change was made. */
10216 config_win(win
, NULL
);
10217 /* TODO: Change stack sizes */
10219 } else if (e
->type
== ewmh
[_NET_RESTACK_WINDOW
].atom
) {
10220 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_RESTACK_WINDOW\n");
10221 val
[0] = e
->data
.data32
[1]; /* Sibling window. */
10222 val
[1] = e
->data
.data32
[2]; /* Stack mode detail. */
10224 xcb_configure_window(conn
, win
->id
, XCB_CONFIG_WINDOW_SIBLING
|
10225 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
10226 } else if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
10227 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
10228 ewmh_change_wm_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
10229 if (e
->data
.data32
[2])
10230 ewmh_change_wm_state(win
, e
->data
.data32
[2],
10231 e
->data
.data32
[0]);
10233 ewmh_update_wm_state(win
);
10235 } else if (e
->type
== ewmh
[_NET_WM_DESKTOP
].atom
) {
10236 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_DESKTOP\n");
10239 win_to_ws(win
, e
->data
.data32
[0], true);
10241 /* Restack if either the source or destination ws is mapped. */
10242 if (r
!= NULL
|| win
->ws
->r
!= NULL
) {
10244 load_float_geom(win
);
10256 int errcode
= xcb_connection_has_error(conn
);
10257 #ifdef XCB_CONN_ERROR
10260 case XCB_CONN_ERROR
:
10261 s
= "Socket error, pipe error or other stream error.";
10263 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
10264 s
= "Extension not supported.";
10266 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
10267 s
= "Insufficient memory.";
10269 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
10270 s
= "Request length was exceeded.";
10272 case XCB_CONN_CLOSED_PARSE_ERR
:
10273 s
= "Error parsing display string.";
10276 s
= "Unknown error.";
10279 errx(errcode
, "X CONNECTION ERROR: %s", s
);
10282 errx(errcode
, "X CONNECTION ERROR");
10289 int num_screens
, i
;
10290 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
10291 XCB_EVENT_MASK_ENTER_WINDOW
;
10293 xcb_void_cookie_t wac
;
10294 xcb_generic_error_t
*error
;
10296 /* this causes an error if some other window manager is running */
10297 num_screens
= get_screen_count();
10298 for (i
= 0; i
< num_screens
; i
++) {
10299 if ((sc
= get_screen(i
)) == NULL
)
10300 errx(1, "ERROR: can't get screen %d.", i
);
10301 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: %#x\n",
10303 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
10304 XCB_CW_EVENT_MASK
, &val
);
10305 if ((error
= xcb_request_check(conn
, wac
))) {
10306 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
10307 error
->error_code
);
10312 /* click to focus on empty region */
10313 xcb_grab_button(conn
, 1, sc
->root
, BUTTONMASK
,
10314 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
10315 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_1
, XCB_BUTTON_MASK_ANY
);
10322 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
10324 struct swm_region
*r
= NULL
, *n
;
10325 struct workspace
*ws
= NULL
;
10329 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
10330 s
->idx
, w
, h
, x
, y
);
10332 /* remove any conflicting regions */
10333 n
= TAILQ_FIRST(&s
->rl
);
10336 n
= TAILQ_NEXT(r
, entry
);
10337 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
10338 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
10339 if (r
->ws
->r
!= NULL
)
10340 r
->ws
->old_r
= r
->ws
->r
;
10343 xcb_destroy_window(conn
, r
->id
);
10344 TAILQ_REMOVE(&s
->rl
, r
, entry
);
10345 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
10349 /* search old regions for one to reuse */
10351 /* size + location match */
10352 TAILQ_FOREACH(r
, &s
->orl
, entry
)
10353 if (X(r
) == x
&& Y(r
) == y
&&
10354 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
10358 TAILQ_FOREACH(r
, &s
->orl
, entry
)
10359 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
10363 TAILQ_REMOVE(&s
->orl
, r
, entry
);
10364 /* try to use old region's workspace */
10365 if (r
->ws
->r
== NULL
)
10368 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
10369 err(1, "new_region: calloc: failed to allocate memory "
10372 /* if we don't have a workspace already, find one */
10374 for (i
= 0; i
< workspace_limit
; i
++)
10375 if (s
->ws
[i
].r
== NULL
) {
10382 errx(1, "new_region: no free workspaces");
10384 if (ws
->state
== SWM_WS_STATE_HIDDEN
)
10385 ws
->state
= SWM_WS_STATE_MAPPING
;
10393 r
->ws_prior
= NULL
;
10396 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
10398 /* Invisible region window to detect pointer events on empty regions. */
10399 r
->id
= xcb_generate_id(conn
);
10400 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
10401 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
10403 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
10404 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
10405 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
10407 /* Make sure region input is at the bottom. */
10408 wa
[0] = XCB_STACK_MODE_BELOW
;
10409 xcb_configure_window(conn
, r
->id
, XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
10411 xcb_map_window(conn
, r
->id
);
10415 scan_randr(int idx
)
10417 #ifdef SWM_XRR_HAS_CRTC
10420 #endif /* SWM_XRR_HAS_CRTC */
10421 struct swm_region
*r
;
10422 struct ws_win
*win
;
10424 xcb_randr_get_screen_resources_current_cookie_t src
;
10425 xcb_randr_get_screen_resources_current_reply_t
*srr
;
10426 xcb_randr_get_crtc_info_cookie_t cic
;
10427 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
10428 xcb_randr_crtc_t
*crtc
;
10429 xcb_screen_t
*screen
;
10431 DNPRINTF(SWM_D_MISC
, "scan_randr: screen: %d\n", idx
);
10433 if ((screen
= get_screen(idx
)) == NULL
)
10434 errx(1, "ERROR: can't get screen %d.", idx
);
10436 num_screens
= get_screen_count();
10437 if (idx
>= num_screens
)
10438 errx(1, "scan_randr: invalid screen");
10440 /* remove any old regions */
10441 while ((r
= TAILQ_FIRST(&screens
[idx
].rl
)) != NULL
) {
10442 r
->ws
->old_r
= r
->ws
->r
= NULL
;
10444 xcb_destroy_window(conn
, r
->id
);
10445 TAILQ_REMOVE(&screens
[idx
].rl
, r
, entry
);
10446 TAILQ_INSERT_TAIL(&screens
[idx
].orl
, r
, entry
);
10450 /* map virtual screens onto physical screens */
10451 #ifdef SWM_XRR_HAS_CRTC
10452 if (randr_support
) {
10453 src
= xcb_randr_get_screen_resources_current(conn
,
10454 screens
[idx
].root
);
10455 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
10458 new_region(&screens
[idx
], 0, 0,
10459 screen
->width_in_pixels
,
10460 screen
->height_in_pixels
);
10463 ncrtc
= srr
->num_crtcs
;
10465 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
10466 for (c
= 0; c
< ncrtc
; c
++) {
10467 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
10469 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
10472 if (cir
->num_outputs
== 0) {
10477 if (cir
->mode
== 0)
10478 new_region(&screens
[idx
], 0, 0,
10479 screen
->width_in_pixels
,
10480 screen
->height_in_pixels
);
10482 new_region(&screens
[idx
],
10483 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
10488 #endif /* SWM_XRR_HAS_CRTC */
10490 /* If detection failed, create a single region that spans the screen. */
10491 if (TAILQ_EMPTY(&screens
[idx
].rl
))
10492 new_region(&screens
[idx
], 0, 0, screen
->width_in_pixels
,
10493 screen
->height_in_pixels
);
10496 /* Cleanup unused previously visible workspaces. */
10497 TAILQ_FOREACH(r
, &screens
[idx
].orl
, entry
) {
10498 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
10500 r
->ws
->state
= SWM_WS_STATE_HIDDEN
;
10502 /* The screen shouldn't focus on an unused region. */
10503 if (screens
[idx
].r_focus
== r
)
10504 screens
[idx
].r_focus
= NULL
;
10507 DNPRINTF(SWM_D_MISC
, "scan_randr: done.\n");
10511 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
10513 struct swm_region
*r
;
10514 int i
, num_screens
;
10516 DNPRINTF(SWM_D_EVENT
, "screenchange: root: %#x\n", e
->root
);
10518 num_screens
= get_screen_count();
10519 /* silly event doesn't include the screen index */
10520 for (i
= 0; i
< num_screens
; i
++)
10521 if (screens
[i
].root
== e
->root
)
10523 if (i
>= num_screens
)
10524 errx(1, "screenchange: screen not found");
10526 /* brute force for now, just re-enumerate the regions */
10530 print_win_geom(e
->root
);
10532 /* add bars to all regions */
10533 for (i
= 0; i
< num_screens
; i
++) {
10534 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10540 /* Make sure a region has focus on each screen. */
10541 for (i
= 0; i
< num_screens
; i
++) {
10542 if (screens
[i
].r_focus
== NULL
) {
10543 r
= TAILQ_FIRST(&screens
[i
].rl
);
10552 /* Update workspace state on all regions. */
10553 for (i
= 0; i
< num_screens
; i
++)
10554 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10555 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
10561 struct swm_region
*r
= NULL
;
10562 xcb_window_t
*wins
= NULL
, trans
, *cwins
= NULL
;
10563 int i
, j
, k
, n
, no
, num_screens
;
10565 bool manage
, mapped
;
10567 xcb_query_tree_cookie_t qtc
;
10568 xcb_query_tree_reply_t
*qtr
;
10569 xcb_get_window_attributes_cookie_t gac
;
10570 xcb_get_window_attributes_reply_t
*gar
;
10571 xcb_get_property_cookie_t pc
;
10572 xcb_get_property_reply_t
*pr
;
10574 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
10575 num_screens
= get_screen_count();
10576 for (i
= 0; i
< num_screens
; i
++) {
10577 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
10578 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
10581 wins
= xcb_query_tree_children(qtr
);
10582 no
= xcb_query_tree_children_length(qtr
);
10584 /* Try to sort windows according to _NET_CLIENT_LIST. */
10585 pr
= xcb_get_property_reply(conn
, xcb_get_property(conn
, 0,
10586 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
10587 XCB_ATOM_WINDOW
, 0, UINT32_MAX
), NULL
);
10589 cwins
= xcb_get_property_value(pr
);
10590 n
= xcb_get_property_value_length(pr
) /
10591 sizeof(xcb_atom_t
);
10593 for (j
= 0; j
< n
; ++j
) {
10594 for (k
= j
; k
< no
; ++k
) {
10595 if (wins
[k
] == cwins
[j
]) {
10596 /* Swap wins j and k. */
10598 wins
[j
] = cwins
[j
];
10606 /* attach windows to a region */
10607 /* normal windows */
10608 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top level windows.\n");
10609 for (j
= 0; j
< no
; j
++) {
10610 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
10611 if (r
->id
== wins
[j
]) {
10612 DNPRINTF(SWM_D_INIT
, "grab_windows: "
10613 "skip %#x; region input window.\n",
10616 } else if (r
->bar
->id
== wins
[j
]) {
10617 DNPRINTF(SWM_D_INIT
, "grab_windows: "
10618 "skip %#x; region bar.\n",
10627 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
10628 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
10630 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10631 "doesn't exist.\n", wins
[j
]);
10635 if (gar
->override_redirect
) {
10636 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10637 "override_redirect set.\n", wins
[j
]);
10642 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
10643 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
10645 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10646 "is transient for %#x.\n", wins
[j
], trans
);
10651 state
= get_win_state(wins
[j
]);
10652 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
10653 mapped
= gar
->map_state
== XCB_MAP_STATE_VIEWABLE
;
10654 if (mapped
|| manage
)
10655 manage_window(wins
[j
], SWM_STACK_TOP
, mapped
);
10658 /* transient windows */
10659 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
10660 for (j
= 0; j
< no
; j
++) {
10661 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
10662 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
10664 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10665 "doesn't exist.\n", wins
[j
]);
10669 if (gar
->override_redirect
) {
10670 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10671 "override_redirect set.\n", wins
[j
]);
10676 state
= get_win_state(wins
[j
]);
10677 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
10678 mapped
= gar
->map_state
== XCB_MAP_STATE_VIEWABLE
;
10679 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
10680 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
10681 &trans
, NULL
) && manage
)
10682 manage_window(wins
[j
], SWM_STACK_TOP
, mapped
);
10687 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
10691 setup_screens(void)
10693 int i
, j
, k
, num_screens
;
10694 struct workspace
*ws
;
10695 uint32_t gcv
[1], wa
[1];
10696 const xcb_query_extension_reply_t
*qep
;
10697 xcb_screen_t
*screen
;
10698 xcb_randr_query_version_cookie_t c
;
10699 xcb_randr_query_version_reply_t
*r
;
10701 num_screens
= get_screen_count();
10702 if ((screens
= calloc(num_screens
, sizeof(struct swm_screen
))) == NULL
)
10703 err(1, "setup_screens: calloc: failed to allocate memory for "
10706 /* Initial RandR setup. */
10707 randr_support
= false;
10708 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
10709 if (qep
->present
) {
10710 c
= xcb_randr_query_version(conn
, 1, 1);
10711 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
10713 if (r
->major_version
>= 1) {
10714 randr_support
= true;
10715 randr_eventbase
= qep
->first_event
;
10721 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
10723 /* map physical screens */
10724 for (i
= 0; i
< num_screens
; i
++) {
10725 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
10726 screens
[i
].idx
= i
;
10727 screens
[i
].r_focus
= NULL
;
10729 TAILQ_INIT(&screens
[i
].rl
);
10730 TAILQ_INIT(&screens
[i
].orl
);
10731 if ((screen
= get_screen(i
)) == NULL
)
10732 errx(1, "ERROR: can't get screen %d.", i
);
10733 screens
[i
].root
= screen
->root
;
10735 /* set default colors */
10736 setscreencolor("red", i
, SWM_S_COLOR_FOCUS
);
10737 setscreencolor("rgb:88/88/88", i
, SWM_S_COLOR_UNFOCUS
);
10738 setscreencolor("rgb:00/80/80", i
, SWM_S_COLOR_BAR_BORDER
);
10739 setscreencolor("rgb:00/40/40", i
,
10740 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
10741 setscreencolor("black", i
, SWM_S_COLOR_BAR
);
10742 setscreencolor("rgb:a0/a0/a0", i
, SWM_S_COLOR_BAR_FONT
);
10743 setscreencolor("red", i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
10744 setscreencolor("rgb:88/88/88", i
,
10745 SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
10747 /* create graphics context on screen */
10748 screens
[i
].bar_gc
= xcb_generate_id(conn
);
10750 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
10751 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
10753 /* set default cursor */
10754 xcb_change_window_attributes(conn
, screens
[i
].root
,
10755 XCB_CW_CURSOR
, wa
);
10757 /* init all workspaces */
10758 /* XXX these should be dynamically allocated too */
10759 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
10760 ws
= &screens
[i
].ws
[j
];
10763 ws
->bar_enabled
= true;
10765 ws
->focus_prev
= NULL
;
10766 ws
->focus_pending
= NULL
;
10769 ws
->state
= SWM_WS_STATE_HIDDEN
;
10770 TAILQ_INIT(&ws
->stack
);
10771 TAILQ_INIT(&ws
->winlist
);
10772 TAILQ_INIT(&ws
->unmanagedlist
);
10774 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
10775 if (layouts
[k
].l_config
!= NULL
)
10776 layouts
[k
].l_config(ws
,
10777 SWM_ARG_ID_STACKINIT
);
10778 ws
->cur_layout
= &layouts
[0];
10779 ws
->cur_layout
->l_string(ws
);
10785 xcb_randr_select_input(conn
, screens
[i
].root
,
10786 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
10791 setup_globals(void)
10793 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
10794 err(1, "setup_globals: strdup: failed to allocate memory.");
10796 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
10797 err(1, "setup_globals: strdup: failed to allocate memory.");
10799 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
10800 errx(1, "unable to allocate key symbols");
10802 a_state
= get_atom_from_string("WM_STATE");
10803 a_prot
= get_atom_from_string("WM_PROTOCOLS");
10804 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
10805 a_net_supported
= get_atom_from_string("_NET_SUPPORTED");
10806 a_net_wm_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
10807 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
10808 a_utf8_string
= get_atom_from_string("UTF8_STRING");
10809 a_swm_ws
= get_atom_from_string("_SWM_WS");
10813 shutdown_cleanup(void)
10815 struct swm_region
*r
;
10817 struct workspace
*ws
;
10818 int i
, num_screens
;
10820 /* disable alarm because the following code may not be interrupted */
10822 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
10823 err(1, "can't disable alarm");
10836 num_screens
= get_screen_count();
10837 for (i
= 0; i
< num_screens
; ++i
) {
10840 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
10841 screens
[i
].root
, XCB_CURRENT_TIME
);
10843 if (screens
[i
].bar_gc
!= XCB_NONE
)
10844 xcb_free_gc(conn
, screens
[i
].bar_gc
);
10845 if (!bar_font_legacy
) {
10846 XftColorFree(display
, DefaultVisual(display
, i
),
10847 DefaultColormap(display
, i
), &bar_font_color
);
10848 XftColorFree(display
, DefaultVisual(display
, i
),
10849 DefaultColormap(display
, i
), &search_font_color
);
10852 for (j
= 0; j
< SWM_S_COLOR_MAX
; ++j
) {
10853 free(screens
[i
].c
[j
].name
);
10856 /* Free window memory. */
10857 for (j
= 0; j
< SWM_WS_MAX
; ++j
) {
10858 ws
= &screens
[i
].ws
[j
];
10861 while ((w
= TAILQ_FIRST(&ws
->winlist
)) != NULL
) {
10862 TAILQ_REMOVE(&ws
->winlist
, w
, entry
);
10866 while ((w
= TAILQ_FIRST(&ws
->unmanagedlist
)) != NULL
) {
10867 TAILQ_REMOVE(&ws
->unmanagedlist
, w
, entry
);
10872 /* Free region memory. */
10873 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
10874 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
10879 while ((r
= TAILQ_FIRST(&screens
[i
].orl
)) != NULL
) {
10880 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
10889 free(clock_format
);
10890 free(startup_exception
);
10893 XFreeFontSet(display
, bar_fs
);
10895 XftFontClose(display
, bar_font
);
10897 xcb_key_symbols_free(syms
);
10899 xcb_disconnect(conn
);
10903 event_error(xcb_generic_error_t
*e
)
10907 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
10908 "resource_id: %u, minor_code: %u\n",
10909 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
10910 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
10911 e
->sequence
, e
->resource_id
, e
->minor_code
);
10915 event_handle(xcb_generic_event_t
*evt
)
10917 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
10919 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
10920 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
10921 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
10924 #define EVENT(type, callback) case type: callback((void *)evt); return
10925 EVENT(0, event_error
);
10926 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
10927 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
10928 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
10929 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
10930 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
10931 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
10932 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
10933 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
10934 /*EVENT(XCB_CREATE_NOTIFY, );*/
10935 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
10936 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
10937 EVENT(XCB_EXPOSE
, expose
);
10939 EVENT(XCB_FOCUS_IN
, focusin
);
10940 EVENT(XCB_FOCUS_OUT
, focusout
);
10942 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
10943 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
10944 EVENT(XCB_KEY_PRESS
, keypress
);
10945 /*EVENT(XCB_KEY_RELEASE, keypress);*/
10946 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
10948 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
10950 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
10951 EVENT(XCB_MAP_REQUEST
, maprequest
);
10952 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
10953 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
10954 /*EVENT(XCB_NO_EXPOSURE, );*/
10955 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
10956 /*EVENT(XCB_REPARENT_NOTIFY, );*/
10957 /*EVENT(XCB_RESIZE_REQUEST, );*/
10958 /*EVENT(XCB_SELECTION_CLEAR, );*/
10959 /*EVENT(XCB_SELECTION_NOTIFY, );*/
10960 /*EVENT(XCB_SELECTION_REQUEST, );*/
10961 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
10962 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
10965 if (type
- randr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
10966 screenchange((void *)evt
);
10970 main(int argc
, char *argv
[])
10972 struct pollfd pfd
[2];
10973 struct sigaction sact
;
10975 struct passwd
*pwd
;
10976 struct swm_region
*r
;
10977 xcb_generic_event_t
*evt
;
10978 int xfd
, i
, num_screens
, num_readable
;
10979 char conf
[PATH_MAX
], *cfile
= NULL
;
10980 bool stdin_ready
= false, startup
= true;
10982 /* suppress unused warning since var is needed */
10986 time_started
= time(NULL
);
10990 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
10991 if (setlocale(LC_CTYPE
, "") == NULL
|| setlocale(LC_TIME
, "") == NULL
)
10992 warnx("no locale support");
10994 /* handle some signals */
10995 bzero(&sact
, sizeof(sact
));
10996 sigemptyset(&sact
.sa_mask
);
10998 sact
.sa_handler
= sighdlr
;
10999 sigaction(SIGINT
, &sact
, NULL
);
11000 sigaction(SIGQUIT
, &sact
, NULL
);
11001 sigaction(SIGTERM
, &sact
, NULL
);
11002 sigaction(SIGHUP
, &sact
, NULL
);
11004 sact
.sa_handler
= sighdlr
;
11005 sact
.sa_flags
= SA_NOCLDSTOP
;
11006 sigaction(SIGCHLD
, &sact
, NULL
);
11008 if ((display
= XOpenDisplay(0)) == NULL
)
11009 errx(1, "can not open display");
11011 conn
= XGetXCBConnection(display
);
11012 if (xcb_connection_has_error(conn
))
11013 errx(1, "can not get XCB connection");
11015 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
11017 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
11018 xfd
= xcb_get_file_descriptor(conn
);
11020 /* look for local and global conf file */
11021 pwd
= getpwuid(getuid());
11023 errx(1, "invalid user: %d", getuid());
11025 xcb_grab_server(conn
);
11026 xcb_aux_sync(conn
);
11028 /* flush all events */
11029 while ((evt
= xcb_poll_for_event(conn
))) {
11030 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
11036 errx(1, "another window manager is currently running");
11038 /* Load Xcursors and/or cursorfont glyph cursors. */
11041 xcb_aux_sync(conn
);
11051 for (i
= 0; ; i
++) {
11056 snprintf(conf
, sizeof conf
, "%s/.%s",
11057 pwd
->pw_dir
, SWM_CONF_FILE
);
11061 snprintf(conf
, sizeof conf
, "/etc/%s",
11066 snprintf(conf
, sizeof conf
, "%s/.%s",
11067 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
11070 /* global compat */
11071 snprintf(conf
, sizeof conf
, "/etc/%s",
11072 SWM_CONF_FILE_OLD
);
11078 if (strlen(conf
) && stat(conf
, &sb
) != -1)
11079 if (S_ISREG(sb
.st_mode
)) {
11086 /* load conf (if any) */
11088 conf_load(cfile
, SWM_CONF_DEFAULT
);
11092 if (getenv("SWM_STARTED") == NULL
)
11093 setenv("SWM_STARTED", "YES", 1);
11095 /* setup all bars */
11096 num_screens
= get_screen_count();
11097 for (i
= 0; i
< num_screens
; i
++)
11098 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11101 /* Manage existing windows. */
11108 xcb_ungrab_server(conn
);
11111 /* Update state of each newly mapped workspace. */
11112 for (i
= 0; i
< num_screens
; i
++)
11113 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11114 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
11116 memset(&pfd
, 0, sizeof(pfd
));
11118 pfd
[0].events
= POLLIN
;
11119 pfd
[1].fd
= STDIN_FILENO
;
11120 pfd
[1].events
= POLLIN
;
11123 while ((evt
= xcb_poll_for_event(conn
))) {
11130 /* If just (re)started, set default focus if needed. */
11134 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
11135 r
= TAILQ_FIRST(&screens
[0].rl
);
11144 num_readable
= poll(pfd
, bar_extra
? 2 : 1, 1000);
11145 if (num_readable
== -1) {
11146 DNPRINTF(SWM_D_MISC
, "poll failed: %s",
11148 } else if (num_readable
> 0 && bar_extra
&&
11149 pfd
[1].revents
& POLLIN
) {
11150 stdin_ready
= true;
11154 restart(NULL
, NULL
);
11163 stdin_ready
= false;
11164 bar_extra_update();
11171 shutdown_cleanup();