2 * Copyright (c) 2009-2012 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-2013 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-2013 David Hill <dhill@mindcry.org>
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Much code and ideas taken from dwm under the following license:
27 * MIT/X Consortium License
29 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
30 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
31 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
32 * 2007 Premysl Hruby <dfenze at gmail dot com>
33 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
34 * 2007 Christof Musik <christof at sendfax dot de>
35 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
36 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
37 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
46 * The above copyright notice and this permission notice shall be included in
47 * all copies or substantial portions of the Software.
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
52 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
54 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
55 * DEALINGS IN THE SOFTWARE.
59 #include <sys/types.h>
63 #include <sys/queue.h>
64 #include <sys/param.h>
65 #include <sys/select.h>
66 #if defined(__linux__)
68 #elif defined(__OpenBSD__)
70 #elif defined(__FreeBSD__)
92 #include <X11/cursorfont.h>
93 #include <X11/extensions/Xrandr.h>
94 #include <X11/Xcursor/Xcursor.h>
95 #include <X11/Xft/Xft.h>
96 #include <X11/Xlib-xcb.h>
97 #include <xcb/xcb_atom.h>
98 #include <xcb/xcb_aux.h>
99 #include <xcb/xcb_event.h>
100 #include <xcb/xcb_icccm.h>
101 #include <xcb/xcb_keysyms.h>
102 #include <xcb/xtest.h>
103 #include <xcb/randr.h>
111 #ifdef SPECTRWM_BUILDSTR
112 static const char *buildstr
= SPECTRWM_BUILDSTR
;
114 static const char *buildstr
= SPECTRWM_VERSION
;
117 #if !defined(__CYGWIN__) /* cygwin chokes on xrandr stuff */
119 # error XRandR versions less than 1.0 are not supported
122 # if RANDR_MAJOR >= 1
123 # if RANDR_MINOR >= 2
124 # define SWM_XRR_HAS_CRTC
127 #endif /* __CYGWIN__ */
129 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
130 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
131 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
132 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
133 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
134 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
135 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
136 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
137 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
138 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
139 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
140 #define xcb_icccm_get_wm_class xcb_get_wm_class
141 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
142 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
143 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
144 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
145 #define xcb_icccm_wm_hints_get_urgency xcb_wm_hints_get_urgency
146 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
147 #define xcb_icccm_get_wm_name xcb_get_wm_name
148 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
149 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
150 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
151 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
152 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
153 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
154 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
155 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
156 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
157 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
160 /*#define SWM_DEBUG*/
162 #define DPRINTF(x...) do { \
164 fprintf(stderr, x); \
166 #define DNPRINTF(n,x...) do { \
167 if (swm_debug & n) { \
168 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
169 fprintf(stderr, x); \
172 #define SWM_D_MISC 0x0001
173 #define SWM_D_EVENT 0x0002
174 #define SWM_D_WS 0x0004
175 #define SWM_D_FOCUS 0x0008
176 #define SWM_D_MOVE 0x0010
177 #define SWM_D_STACK 0x0020
178 #define SWM_D_MOUSE 0x0040
179 #define SWM_D_PROP 0x0080
180 #define SWM_D_CLASS 0x0100
181 #define SWM_D_KEY 0x0200
182 #define SWM_D_QUIRK 0x0400
183 #define SWM_D_SPAWN 0x0800
184 #define SWM_D_EVENTQ 0x1000
185 #define SWM_D_CONF 0x2000
186 #define SWM_D_BAR 0x4000
187 #define SWM_D_INIT 0x8000
189 u_int32_t swm_debug
= 0
208 #define DPRINTF(x...)
209 #define DNPRINTF(n,x...)
212 #define SWM_EWMH_ACTION_COUNT_MAX (8)
213 #define EWMH_F_FULLSCREEN (0x001)
214 #define EWMH_F_ABOVE (0x002)
215 #define EWMH_F_HIDDEN (0x004)
216 #define EWMH_F_MAXIMIZED_VERT (0x008)
217 #define EWMH_F_MAXIMIZED_HORZ (0x010)
218 #define EWMH_F_SKIP_PAGER (0x020)
219 #define EWMH_F_SKIP_TASKBAR (0x040)
220 #define SWM_F_MANUAL (0x080)
222 #define EWMH_F_MAXIMIZED (EWMH_F_MAXIMIZED_VERT | EWMH_F_MAXIMIZED_HORZ)
224 /* convert 8-bit to 16-bit */
225 #define RGB_8_TO_16(col) (((col) << 8) + (col))
227 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
228 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
229 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
230 xrc.blue = RGB_8_TO_16((px) & 0xff); \
233 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
234 #define MODKEY XCB_MOD_MASK_1
235 #define CLEANMASK(mask) ((mask) & ~(numlockmask | XCB_MOD_MASK_LOCK))
236 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
237 XCB_EVENT_MASK_BUTTON_RELEASE)
238 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
239 #define SWM_PROPLEN (16)
240 #define SWM_FUNCNAME_LEN (32)
241 #define SWM_KEYS_LEN (255)
242 #define SWM_QUIRK_LEN (64)
243 #define X(r) ((r)->g.x)
244 #define Y(r) ((r)->g.y)
245 #define WIDTH(r) ((r)->g.w)
246 #define HEIGHT(r) ((r)->g.h)
247 #define BORDER(w) ((w)->bordered ? border_width : 0)
248 #define MAX_X(r) ((r)->g.x + (r)->g.w)
249 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
250 #define SH_MIN(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
251 #define SH_MIN_W(w) ((w)->sh.min_width)
252 #define SH_MIN_H(w) ((w)->sh.min_height)
253 #define SH_MAX(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
254 #define SH_MAX_W(w) ((w)->sh.max_width)
255 #define SH_MAX_H(w) ((w)->sh.max_height)
256 #define SH_INC(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)
257 #define SH_INC_W(w) ((w)->sh.width_inc)
258 #define SH_INC_H(w) ((w)->sh.height_inc)
259 #define SWM_MAX_FONT_STEPS (3)
260 #define WINID(w) ((w) ? (w)->id : XCB_WINDOW_NONE)
261 #define WS_FOCUSED(ws) ((ws)->r && (ws)->r->s->r_focus == (ws)->r)
262 #define YESNO(x) ((x) ? "yes" : "no")
263 #define ICONIC(w) ((w)->ewmh_flags & EWMH_F_HIDDEN)
264 #define ABOVE(w) ((w)->ewmh_flags & EWMH_F_ABOVE)
265 #define FULLSCREEN(w) ((w)->ewmh_flags & EWMH_F_FULLSCREEN)
266 #define MAXIMIZED_VERT(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_VERT)
267 #define MAXIMIZED_HORZ(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_HORZ)
268 #define MAXIMIZED(w) (MAXIMIZED_VERT(w) || MAXIMIZED_HORZ(w))
269 #define MANUAL(w) ((w)->ewmh_flags & SWM_F_MANUAL)
270 #define TRANS(w) ((w)->transient != XCB_WINDOW_NONE)
271 #define FLOATING(w) (ABOVE(w) || TRANS(w) || FULLSCREEN(w) || \
274 /* Constrain Window flags */
275 #define SWM_CW_RESIZABLE (0x01)
276 #define SWM_CW_SOFTBOUNDARY (0x02)
277 #define SWM_CW_HARDBOUNDARY (0x04)
278 #define SWM_CW_RIGHT (0x10)
279 #define SWM_CW_LEFT (0x20)
280 #define SWM_CW_BOTTOM (0x40)
281 #define SWM_CW_TOP (0x80)
282 #define SWM_CW_ALLSIDES (0xf0)
284 #define SWM_FOCUS_DEFAULT (0)
285 #define SWM_FOCUS_FOLLOW (1)
286 #define SWM_FOCUS_MANUAL (2)
288 #define SWM_CK_NONE (0)
289 #define SWM_CK_ALL (0xf)
290 #define SWM_CK_FOCUS (0x1)
291 #define SWM_CK_POINTER (0x2)
292 #define SWM_CK_FALLBACK (0x4)
293 #define SWM_CK_REGION (0x8)
295 #define SWM_G_ALL (0xf)
296 #define SWM_G_SIZE (0x1)
297 #define SWM_G_POS (0x2)
299 #define SWM_CONF_DEFAULT (0)
300 #define SWM_CONF_KEYMAPPING (1)
303 #define SWM_LIB "/usr/local/lib/libswmhack.so"
310 xcb_atom_t a_net_wm_check
;
311 xcb_atom_t a_net_supported
;
312 xcb_atom_t a_takefocus
;
313 xcb_atom_t a_utf8_string
;
315 volatile sig_atomic_t running
= 1;
316 volatile sig_atomic_t restart_wm
= 0;
317 xcb_timestamp_t last_event_time
= 0;
321 int xrandr_eventbase
;
322 unsigned int numlockmask
= 0;
325 xcb_connection_t
*conn
;
326 xcb_key_symbols_t
*syms
;
328 int boundary_width
= 50;
330 int cycle_visible
= 0;
332 int font_adjusted
= 0;
333 unsigned int mod_key
= MODKEY
;
336 struct swm_region
*search_r
;
337 int select_list_pipe
[2];
338 int select_resp_pipe
[2];
340 volatile sig_atomic_t search_resp
;
341 int search_resp_action
;
343 struct search_window
{
344 TAILQ_ENTRY(search_window
) entry
;
348 xcb_window_t indicator
;
350 TAILQ_HEAD(search_winlist
, search_window
);
351 struct search_winlist search_wl
;
356 SWM_SEARCH_UNICONIFY
,
357 SWM_SEARCH_NAME_WORKSPACE
,
358 SWM_SEARCH_SEARCH_WORKSPACE
,
359 SWM_SEARCH_SEARCH_WINDOW
362 #define SWM_STACK_TOP (0)
363 #define SWM_STACK_BOTTOM (1)
364 #define SWM_STACK_ABOVE (2)
365 #define SWM_STACK_BELOW (3)
368 double dialog_ratio
= 0.6;
370 #define SWM_BAR_MAX (256)
371 #define SWM_BAR_JUSTIFY_LEFT (0)
372 #define SWM_BAR_JUSTIFY_CENTER (1)
373 #define SWM_BAR_JUSTIFY_RIGHT (2)
374 #define SWM_BAR_OFFSET (4)
375 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
376 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
377 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
378 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
379 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
381 #ifdef X_HAVE_UTF8_STRING
382 #define DRAWSTRING(x...) Xutf8DrawString(x)
384 #define DRAWSTRING(x...) XmbDrawString(x)
387 char *bar_argv
[] = { NULL
, NULL
};
389 char bar_ext
[SWM_BAR_MAX
];
390 char bar_ext_buf
[SWM_BAR_MAX
];
391 char bar_vertext
[SWM_BAR_MAX
];
394 int bar_border_width
= 1;
395 int bar_at_bottom
= 0;
399 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
400 char *bar_format
= NULL
;
401 int stack_enabled
= 1;
402 int clock_enabled
= 1;
403 int iconic_enabled
= 0;
404 int urgent_enabled
= 0;
405 char *clock_format
= NULL
;
406 int window_class_enabled
= 0;
407 int window_instance_enabled
= 0;
408 int window_name_enabled
= 0;
409 int focus_mode
= SWM_FOCUS_DEFAULT
;
410 int focus_close
= SWM_STACK_BELOW
;
411 int focus_close_wrap
= 1;
412 int focus_default
= SWM_STACK_TOP
;
413 int spawn_position
= SWM_STACK_TOP
;
414 int disable_border
= 0;
415 int border_width
= 1;
416 int region_padding
= 0;
418 int verbose_layout
= 0;
422 XFontSetExtents
*bar_fs_extents
;
424 int bar_font_legacy
= 1;
426 XftColor bar_font_color
;
428 char *startup_exception
;
429 unsigned int nr_exceptions
= 0;
431 /* layout manager data */
432 struct swm_geometry
{
445 struct swm_geometry g
;
448 /* virtual "screens" */
450 TAILQ_ENTRY(swm_region
) entry
;
452 struct swm_geometry g
;
453 struct workspace
*ws
; /* current workspace on this region */
454 struct workspace
*ws_prior
; /* prior workspace on this region */
455 struct swm_screen
*s
; /* screen idx */
458 TAILQ_HEAD(swm_region_list
, swm_region
);
461 TAILQ_ENTRY(ws_win
) entry
;
462 TAILQ_ENTRY(ws_win
) stack_entry
;
464 xcb_window_t transient
;
465 struct ws_win
*focus_child
; /* focus on child transient */
466 struct swm_geometry g
; /* current geometry */
467 struct swm_geometry g_float
; /* region coordinates */
468 int g_floatvalid
; /* g_float geometry validity */
472 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
478 unsigned long quirks
;
479 struct workspace
*ws
; /* always valid */
480 struct swm_screen
*s
; /* always valid, never changes */
482 xcb_icccm_get_wm_class_reply_t ch
;
483 xcb_icccm_wm_hints_t hints
;
485 TAILQ_HEAD(ws_win_list
, ws_win
);
486 TAILQ_HEAD(ws_win_stack
, ws_win
);
490 TAILQ_ENTRY(pid_e
) entry
;
494 TAILQ_HEAD(pid_list
, pid_e
);
495 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
497 /* layout handlers */
499 void vertical_config(struct workspace
*, int);
500 void vertical_stack(struct workspace
*, struct swm_geometry
*);
501 void horizontal_config(struct workspace
*, int);
502 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
503 void max_stack(struct workspace
*, struct swm_geometry
*);
504 void plain_stacker(struct workspace
*);
505 void fancy_stacker(struct workspace
*);
508 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
509 void (*l_config
)(struct workspace
*, int);
511 #define SWM_L_FOCUSPREV (1<<0)
512 #define SWM_L_MAPONFOCUS (1<<1)
513 void (*l_string
)(struct workspace
*);
515 /* stack, configure */
516 { vertical_stack
, vertical_config
, 0, plain_stacker
},
517 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
519 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
520 { NULL
, NULL
, 0, NULL
},
523 /* position of max_stack mode in the layouts array, index into layouts! */
524 #define SWM_V_STACK (0)
525 #define SWM_H_STACK (1)
526 #define SWM_MAX_STACK (2)
528 #define SWM_H_SLICE (32)
529 #define SWM_V_SLICE (32)
531 /* define work spaces */
533 int idx
; /* workspace index */
534 char *name
; /* workspace name */
535 int always_raise
; /* raise windows on focus */
536 int bar_enabled
; /* bar visibility */
537 struct layout
*cur_layout
; /* current layout handlers */
538 struct ws_win
*focus
; /* may be NULL */
539 struct ws_win
*focus_prev
; /* may be NULL */
540 struct ws_win
*focus_pending
; /* may be NULL */
541 struct swm_region
*r
; /* may be NULL */
542 struct swm_region
*old_r
; /* may be NULL */
543 struct ws_win_list winlist
; /* list of windows in ws */
544 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
545 struct ws_win_stack stack
; /* stacking order */
546 int32_t state
; /* mapping state */
547 char stacker
[10]; /* display stacker and layout */
551 int horizontal_msize
;
553 int horizontal_stacks
;
564 SWM_WS_STATE_MAPPING
,
570 SWM_S_COLOR_BAR_BORDER
,
571 SWM_S_COLOR_BAR_BORDER_UNFOCUS
,
572 SWM_S_COLOR_BAR_FONT
,
578 /* physical screen mapping */
579 #define SWM_WS_MAX (22) /* hard limit */
580 int workspace_limit
= 10; /* soft limit */
583 int idx
; /* screen index */
584 struct swm_region_list rl
; /* list of regions on this screen */
585 struct swm_region_list orl
; /* list of old regions */
587 struct workspace ws
[SWM_WS_MAX
];
588 struct swm_region
*r_focus
;
594 } c
[SWM_S_COLOR_MAX
];
596 xcb_gcontext_t bar_gc
;
599 struct swm_screen
*screens
;
601 /* args to functions */
604 #define SWM_ARG_ID_FOCUSNEXT (0)
605 #define SWM_ARG_ID_FOCUSPREV (1)
606 #define SWM_ARG_ID_FOCUSMAIN (2)
607 #define SWM_ARG_ID_FOCUSURGENT (3)
608 #define SWM_ARG_ID_SWAPNEXT (10)
609 #define SWM_ARG_ID_SWAPPREV (11)
610 #define SWM_ARG_ID_SWAPMAIN (12)
611 #define SWM_ARG_ID_MOVELAST (13)
612 #define SWM_ARG_ID_MASTERSHRINK (20)
613 #define SWM_ARG_ID_MASTERGROW (21)
614 #define SWM_ARG_ID_MASTERADD (22)
615 #define SWM_ARG_ID_MASTERDEL (23)
616 #define SWM_ARG_ID_FLIPLAYOUT (24)
617 #define SWM_ARG_ID_STACKRESET (30)
618 #define SWM_ARG_ID_STACKINIT (31)
619 #define SWM_ARG_ID_CYCLEWS_UP (40)
620 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
621 #define SWM_ARG_ID_CYCLERG_UP (42)
622 #define SWM_ARG_ID_CYCLERG_DOWN (43)
623 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
624 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
625 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
626 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
627 #define SWM_ARG_ID_STACKINC (50)
628 #define SWM_ARG_ID_STACKDEC (51)
629 #define SWM_ARG_ID_SS_ALL (60)
630 #define SWM_ARG_ID_SS_WINDOW (61)
631 #define SWM_ARG_ID_DONTCENTER (70)
632 #define SWM_ARG_ID_CENTER (71)
633 #define SWM_ARG_ID_KILLWINDOW (80)
634 #define SWM_ARG_ID_DELETEWINDOW (81)
635 #define SWM_ARG_ID_WIDTHGROW (90)
636 #define SWM_ARG_ID_WIDTHSHRINK (91)
637 #define SWM_ARG_ID_HEIGHTGROW (92)
638 #define SWM_ARG_ID_HEIGHTSHRINK (93)
639 #define SWM_ARG_ID_MOVEUP (100)
640 #define SWM_ARG_ID_MOVEDOWN (101)
641 #define SWM_ARG_ID_MOVELEFT (102)
642 #define SWM_ARG_ID_MOVERIGHT (103)
643 #define SWM_ARG_ID_BAR_TOGGLE (110)
644 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
650 TAILQ_ENTRY(quirk
) entry
;
651 char *class; /* WM_CLASS:class */
652 char *instance
; /* WM_CLASS:instance */
653 char *name
; /* WM_NAME */
655 regex_t regex_instance
;
658 #define SWM_Q_FLOAT (1<<0) /* float this window */
659 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
660 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
661 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
662 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
663 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
664 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
665 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
667 TAILQ_HEAD(quirk_list
, quirk
);
668 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
671 * Supported EWMH hints should be added to
672 * both the enum and the ewmh array
678 _NET_CURRENT_DESKTOP
,
680 _NET_MOVERESIZE_WINDOW
,
681 _NET_WM_ACTION_ABOVE
,
682 _NET_WM_ACTION_CLOSE
,
683 _NET_WM_ACTION_FULLSCREEN
,
685 _NET_WM_ACTION_RESIZE
,
686 _NET_WM_ALLOWED_ACTIONS
,
688 _NET_WM_FULL_PLACEMENT
,
690 _NET_NUMBER_OF_DESKTOPS
,
693 _NET_WM_STATE_FULLSCREEN
,
694 _NET_WM_STATE_HIDDEN
,
695 _NET_WM_STATE_MAXIMIZED_VERT
,
696 _NET_WM_STATE_MAXIMIZED_HORZ
,
697 _NET_WM_STATE_SKIP_PAGER
,
698 _NET_WM_STATE_SKIP_TASKBAR
,
700 _NET_WM_WINDOW_TYPE_DIALOG
,
701 _NET_WM_WINDOW_TYPE_DOCK
,
702 _NET_WM_WINDOW_TYPE_NORMAL
,
703 _NET_WM_WINDOW_TYPE_SPLASH
,
704 _NET_WM_WINDOW_TYPE_TOOLBAR
,
705 _NET_WM_WINDOW_TYPE_UTILITY
,
706 _SWM_WM_STATE_MANUAL
,
713 } ewmh
[SWM_EWMH_HINT_MAX
] = {
714 /* must be in same order as in the enum */
715 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
716 {"_NET_CLIENT_LIST", XCB_ATOM_NONE
},
717 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
718 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE
},
719 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE
},
720 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
721 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
722 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
723 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
724 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
725 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
726 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
727 {"_NET_WM_DESKTOP", XCB_ATOM_NONE
},
728 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE
},
729 {"_NET_WM_NAME", XCB_ATOM_NONE
},
730 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE
},
731 {"_NET_WM_STATE", XCB_ATOM_NONE
},
732 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
733 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
734 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
735 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
736 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
737 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
738 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
739 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
740 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
741 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
742 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
743 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
744 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
745 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
746 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
749 /* EWMH source type */
751 EWMH_SOURCE_TYPE_NONE
= 0,
752 EWMH_SOURCE_TYPE_NORMAL
= 1,
753 EWMH_SOURCE_TYPE_OTHER
= 2,
760 XC_BOTTOM_LEFT_CORNER
,
761 XC_BOTTOM_RIGHT_CORNER
,
769 char *name
; /* Name used by Xcursor .*/
770 uint8_t cf_char
; /* cursorfont index. */
772 } cursors
[XC_MAX
] = {
773 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
774 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
775 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
776 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
777 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
778 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
779 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
782 #define SWM_SPAWN_OPTIONAL 0x1
786 TAILQ_ENTRY(spawn_prog
) entry
;
792 TAILQ_HEAD(spawn_list
, spawn_prog
);
793 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
795 /* user/key callable function IDs */
911 KF_DUMPWINS
, /* MUST BE LAST */
919 enum keyfuncid funcid
;
922 RB_HEAD(key_tree
, key
);
924 /* function prototypes */
925 void adjust_font(struct ws_win
*);
926 char *argsep(char **);
927 void bar_cleanup(struct swm_region
*);
928 void bar_extra_setup(void);
929 void bar_extra_stop(void);
930 int bar_extra_update(void);
931 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
932 void bar_fmt_expand(char *, size_t);
934 void bar_print(struct swm_region
*, const char *);
935 void bar_print_legacy(struct swm_region
*, const char *);
936 void bar_replace(char *, char *, struct swm_region
*, size_t);
937 void bar_replace_pad(char *, int *, size_t);
938 char *bar_replace_seq(char *, char *, struct swm_region
*, size_t *, size_t);
939 void bar_setup(struct swm_region
*);
940 void bar_toggle(struct swm_region
*, union arg
*);
941 void bar_urgent(char *, size_t);
942 void bar_window_class(char *, size_t, struct swm_region
*);
943 void bar_window_class_instance(char *, size_t, struct swm_region
*);
944 void bar_window_float(char *, size_t, struct swm_region
*);
945 void bar_window_instance(char *, size_t, struct swm_region
*);
946 void bar_window_name(char *, size_t, struct swm_region
*);
947 void bar_window_state(char *, size_t, struct swm_region
*);
948 void bar_workspace_name(char *, size_t, struct swm_region
*);
949 void buttonpress(xcb_button_press_event_t
*);
950 void check_conn(void);
951 void clear_keys(void);
952 int clear_maximized(struct workspace
*);
953 void clientmessage(xcb_client_message_event_t
*);
954 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
955 int conf_load(const char *, int);
956 void configurenotify(xcb_configure_notify_event_t
*);
957 void configurerequest(xcb_configure_request_event_t
*);
958 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
959 void constrain_window(struct ws_win
*, struct swm_geometry
*, int *);
960 int count_win(struct workspace
*, int);
961 void cursors_cleanup(void);
962 void cursors_load(void);
963 void custom_region(const char *);
964 void cyclerg(struct swm_region
*, union arg
*);
965 void cyclews(struct swm_region
*, union arg
*);
966 void cycle_layout(struct swm_region
*, union arg
*);
967 void destroynotify(xcb_destroy_notify_event_t
*);
968 void dumpwins(struct swm_region
*, union arg
*);
970 void enternotify(xcb_enter_notify_event_t
*);
971 void event_drain(uint8_t);
972 void event_error(xcb_generic_error_t
*);
973 void event_handle(xcb_generic_event_t
*);
974 void ewmh_apply_flags(struct ws_win
*, uint32_t);
975 void ewmh_autoquirk(struct ws_win
*);
976 void ewmh_get_desktop_names(void);
977 void ewmh_get_wm_state(struct ws_win
*);
978 void ewmh_update_actions(struct ws_win
*);
979 void ewmh_update_client_list(void);
980 void ewmh_update_current_desktop(void);
981 void ewmh_update_desktop_names(void);
982 void ewmh_change_wm_state(struct ws_win
*, xcb_atom_t
, long);
983 void ewmh_update_wm_state(struct ws_win
*);
984 char *expand_tilde(const char *);
985 void expose(xcb_expose_event_t
*);
986 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
987 struct pid_e
*find_pid(pid_t
);
988 struct ws_win
*find_unmanaged_window(xcb_window_t
);
989 struct ws_win
*find_window(xcb_window_t
);
990 void floating_toggle(struct swm_region
*, union arg
*);
991 void focus(struct swm_region
*, union arg
*);
993 void focusin(xcb_focus_in_event_t
*);
994 void focusout(xcb_focus_out_event_t
*);
996 void focus_flush(void);
997 void focus_region(struct swm_region
*);
998 void focusrg(struct swm_region
*, union arg
*);
999 void focus_win(struct ws_win
*);
1000 void fontset_init(void);
1001 void free_window(struct ws_win
*);
1002 xcb_atom_t
get_atom_from_string(const char *);
1004 char *get_atom_name(xcb_atom_t
);
1006 struct ws_win
*get_focus_magic(struct ws_win
*);
1007 struct ws_win
*get_focus_prev(struct ws_win
*);
1009 char *get_notify_detail_label(uint8_t);
1010 char *get_notify_mode_label(uint8_t);
1012 struct ws_win
*get_pointer_win(xcb_window_t
);
1013 struct ws_win
*get_region_focus(struct swm_region
*);
1014 int get_region_index(struct swm_region
*);
1015 xcb_screen_t
*get_screen(int);
1016 int get_screen_count(void);
1018 char *get_stack_mode_name(uint8_t);
1020 int32_t get_swm_ws(xcb_window_t
);
1021 char *get_win_name(xcb_window_t
);
1022 void get_wm_protocols(struct ws_win
*);
1023 int get_ws_idx(xcb_window_t
);
1024 uint32_t getstate(xcb_window_t
);
1025 void grabbuttons(struct ws_win
*);
1026 void grabkeys(void);
1027 void grab_windows(void);
1028 void iconify(struct swm_region
*, union arg
*);
1030 void keypress(xcb_key_press_event_t
*);
1031 int key_cmp(struct key
*, struct key
*);
1032 void key_insert(unsigned int, KeySym
, enum keyfuncid
, const char *);
1033 struct key
*key_lookup(unsigned int, KeySym
);
1034 void key_remove(struct key
*);
1035 void key_replace(struct key
*, unsigned int, KeySym
, enum keyfuncid
,
1037 void kill_bar_extra_atexit(void);
1038 void kill_refs(struct ws_win
*);
1040 void leavenotify(xcb_leave_notify_event_t
*);
1042 void load_float_geom(struct ws_win
*);
1043 struct ws_win
*manage_window(xcb_window_t
, uint16_t);
1044 void map_window(struct ws_win
*);
1045 void mapnotify(xcb_map_notify_event_t
*);
1046 void mappingnotify(xcb_mapping_notify_event_t
*);
1047 void maprequest(xcb_map_request_event_t
*);
1048 void maximize_toggle(struct swm_region
*, union arg
*);
1049 void motionnotify(xcb_motion_notify_event_t
*);
1050 void move(struct ws_win
*, union arg
*);
1051 void move_step(struct swm_region
*, union arg
*);
1052 uint32_t name_to_pixel(int, const char *);
1053 void name_workspace(struct swm_region
*, union arg
*);
1054 void new_region(struct swm_screen
*, int, int, int, int);
1055 int parsekeys(const char *, unsigned int, unsigned int *, KeySym
*);
1056 int parsequirks(const char *, unsigned long *);
1057 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1058 void pressbutton(struct swm_region
*, union arg
*);
1059 void priorws(struct swm_region
*, union arg
*);
1061 void print_win_geom(xcb_window_t
);
1063 void propertynotify(xcb_property_notify_event_t
*);
1064 void quirk_free(struct quirk
*);
1065 void quirk_insert(const char *, const char *, const char *,unsigned long);
1066 void quirk_remove(struct quirk
*);
1067 void quirk_replace(struct quirk
*, const char *, const char *, const char *,
1069 void quit(struct swm_region
*, union arg
*);
1070 void raise_toggle(struct swm_region
*, union arg
*);
1071 void raise_window(struct ws_win
*);
1072 void region_containment(struct ws_win
*, struct swm_region
*, int);
1073 struct swm_region
*region_under(struct swm_screen
*, int, int);
1074 void regionize(struct ws_win
*, int, int);
1075 void resize(struct ws_win
*, union arg
*);
1076 void resize_step(struct swm_region
*, union arg
*);
1077 void restart(struct swm_region
*, union arg
*);
1078 struct swm_region
*root_to_region(xcb_window_t
, int);
1079 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1080 void scan_xrandr(int);
1081 void search_do_resp(void);
1082 void search_resp_name_workspace(const char *, size_t);
1083 void search_resp_search_window(const char *);
1084 void search_resp_search_workspace(const char *);
1085 void search_resp_uniconify(const char *, size_t);
1086 void search_win(struct swm_region
*, union arg
*);
1087 void search_win_cleanup(void);
1088 void search_workspace(struct swm_region
*, union arg
*);
1089 void send_to_rg(struct swm_region
*, union arg
*);
1090 void send_to_ws(struct swm_region
*, union arg
*);
1091 void set_region(struct swm_region
*);
1092 int setautorun(const char *, const char *, int);
1093 int setconfbinding(const char *, const char *, int);
1094 int setconfcolor(const char *, const char *, int);
1095 int setconfmodkey(const char *, const char *, int);
1096 int setconfquirk(const char *, const char *, int);
1097 int setconfregion(const char *, const char *, int);
1098 int setconfspawn(const char *, const char *, int);
1099 int setconfvalue(const char *, const char *, int);
1100 void setkeybinding(unsigned int, KeySym
, enum keyfuncid
, const char *);
1101 int setkeymapping(const char *, const char *, int);
1102 int setlayout(const char *, const char *, int);
1103 void setquirk(const char *, const char *, const char *,unsigned long);
1104 void setscreencolor(const char *, int, int);
1105 void setspawn(const char *, const char *, int);
1106 void setup_ewmh(void);
1107 void setup_globals(void);
1108 void setup_keys(void);
1109 void setup_quirks(void);
1110 void setup_screens(void);
1111 void setup_spawn(void);
1112 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1113 void set_win_state(struct ws_win
*, uint16_t);
1114 void shutdown_cleanup(void);
1116 void socket_setnonblock(int);
1117 void sort_windows(struct ws_win_list
*);
1118 void spawn(int, union arg
*, int);
1119 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1120 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1121 void spawn_insert(const char *, const char *, int);
1122 struct spawn_prog
*spawn_find(const char *);
1123 void spawn_remove(struct spawn_prog
*);
1124 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1125 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1126 void stack_config(struct swm_region
*, union arg
*);
1127 void stack_master(struct workspace
*, struct swm_geometry
*, int, int);
1128 void store_float_geom(struct ws_win
*);
1129 char *strdupsafe(const char *);
1130 void swapwin(struct swm_region
*, union arg
*);
1131 void switchws(struct swm_region
*, union arg
*);
1132 void teardown_ewmh(void);
1133 void unescape_selector(char *);
1134 void unfocus_win(struct ws_win
*);
1135 void uniconify(struct swm_region
*, union arg
*);
1136 void unmanage_window(struct ws_win
*);
1137 void unmapnotify(xcb_unmap_notify_event_t
*);
1138 void unmap_all(void);
1139 void unmap_window(struct ws_win
*);
1140 void updatenumlockmask(void);
1141 void update_floater(struct ws_win
*);
1142 void update_modkey(unsigned int);
1143 void update_win_stacking(struct ws_win
*);
1144 void update_window(struct ws_win
*);
1145 void update_wm_state(struct ws_win
*win
);
1146 void validate_spawns(void);
1147 int validate_win(struct ws_win
*);
1148 int validate_ws(struct workspace
*);
1149 void version(struct swm_region
*, union arg
*);
1150 void win_to_ws(struct ws_win
*, int, int);
1151 pid_t
window_get_pid(xcb_window_t
);
1152 void wkill(struct swm_region
*, union arg
*);
1153 void workaround(void);
1154 void update_ws_stack(struct workspace
*);
1155 void xft_init(struct swm_region
*);
1156 void _add_startup_exception(const char *, va_list);
1157 void add_startup_exception(const char *, ...);
1159 RB_PROTOTYPE(key_tree
, key
, entry
, key_cmp
);
1160 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
1161 struct key_tree keys
;
1166 xcb_font_t cf
= XCB_NONE
;
1169 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1170 /* try to load Xcursor first. */
1171 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1174 /* fallback to cursorfont. */
1175 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1176 if (cf
== XCB_NONE
) {
1177 cf
= xcb_generate_id(conn
);
1178 xcb_open_font(conn
, cf
, strlen("cursor"),
1182 cursors
[i
].cid
= xcb_generate_id(conn
);
1183 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1184 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1185 0xffff, 0xffff, 0xffff);
1191 xcb_close_font(conn
, cf
);
1195 cursors_cleanup(void)
1198 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1199 xcb_free_cursor(conn
, cursors
[i
].cid
);
1203 expand_tilde(const char *s
)
1205 struct passwd
*ppwd
;
1213 errx(1, "expand_tilde: NULL string.");
1216 result
= strdup(sc
);
1222 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1223 errx(1, "expand_tilde: sysconf");
1225 if ((user
= calloc(1, max
+ 1)) == NULL
)
1226 errx(1, "expand_tilde: calloc");
1228 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1233 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1237 result
= strdup(sc
);
1239 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1243 errx(1, "expand_tilde: failed to allocate memory.");
1249 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1251 unsigned int tmpr
, tmpg
, tmpb
;
1253 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1256 *rr
= RGB_8_TO_16(tmpr
);
1257 *gg
= RGB_8_TO_16(tmpg
);
1258 *bb
= RGB_8_TO_16(tmpb
);
1264 get_screen(int screen
)
1266 const xcb_setup_t
*r
;
1267 xcb_screen_iterator_t iter
;
1269 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1270 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1274 iter
= xcb_setup_roots_iterator(r
);
1275 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1283 get_screen_count(void)
1285 const xcb_setup_t
*r
;
1287 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1288 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1292 return xcb_setup_roots_length(r
);
1296 get_region_index(struct swm_region
*r
)
1298 struct swm_region
*rr
;
1304 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1319 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1320 event_drain(XCB_ENTER_NOTIFY
);
1326 get_atom_from_string(const char *str
)
1328 xcb_intern_atom_cookie_t c
;
1329 xcb_intern_atom_reply_t
*r
;
1332 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1333 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1341 return (XCB_ATOM_NONE
);
1345 get_wm_protocols(struct ws_win
*win
) {
1347 xcb_icccm_get_wm_protocols_reply_t wpr
;
1349 if (xcb_icccm_get_wm_protocols_reply(conn
,
1350 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
1352 for (i
= 0; i
< (int)wpr
.atoms_len
; i
++) {
1353 if (wpr
.atoms
[i
] == a_takefocus
)
1354 win
->take_focus
= 1;
1355 if (wpr
.atoms
[i
] == a_delete
)
1356 win
->can_delete
= 1;
1358 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
1365 int i
, j
, num_screens
;
1368 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1369 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1371 num_screens
= get_screen_count();
1372 for (i
= 0; i
< num_screens
; i
++) {
1373 /* Support check window will be created by workaround(). */
1375 /* Report supported atoms */
1376 xcb_delete_property(conn
, screens
[i
].root
, a_net_supported
);
1377 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1378 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
,
1379 screens
[i
].root
, a_net_supported
, XCB_ATOM_ATOM
,
1380 32, 1, &ewmh
[j
].atom
);
1382 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
1383 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
1384 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
1393 xcb_get_property_cookie_t pc
;
1394 xcb_get_property_reply_t
*pr
;
1396 num_screens
= get_screen_count();
1398 for (i
= 0; i
< num_screens
; i
++) {
1399 /* Get the support check window and destroy it */
1400 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, a_net_wm_check
,
1401 XCB_ATOM_WINDOW
, 0, 1);
1402 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1405 if (pr
->format
== a_net_wm_check
) {
1406 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1408 xcb_destroy_window(conn
, id
);
1409 xcb_delete_property(conn
, screens
[i
].root
,
1411 xcb_delete_property(conn
, screens
[i
].root
,
1419 ewmh_autoquirk(struct ws_win
*win
)
1423 xcb_get_property_cookie_t c
;
1424 xcb_get_property_reply_t
*r
;
1426 c
= xcb_get_property(conn
, 0, win
->id
,
1427 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1428 r
= xcb_get_property_reply(conn
, c
, NULL
);
1432 type
= xcb_get_property_value(r
);
1433 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1435 for (i
= 0; i
< n
; i
++) {
1436 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1438 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1439 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1440 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1441 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1444 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1445 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1446 win
->quirks
= SWM_Q_FLOAT
;
1454 ewmh_update_actions(struct ws_win
*win
)
1456 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1462 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1465 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1466 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1467 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1470 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1471 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1474 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1475 #define _NET_WM_STATE_ADD 1 /* add/set property */
1476 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1479 ewmh_change_wm_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1486 name
= get_atom_name(state
);
1487 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: win %#x, state: %s, "
1488 "action: %ld\n", WINID(win
), name
, action
);
1494 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1495 flag
= EWMH_F_FULLSCREEN
;
1496 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1497 flag
= EWMH_F_ABOVE
;
1498 else if (state
== ewmh
[_NET_WM_STATE_HIDDEN
].atom
)
1499 flag
= EWMH_F_HIDDEN
;
1500 else if (state
== ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
||
1501 state
== ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
)
1502 flag
= EWMH_F_MAXIMIZED
;
1503 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1504 flag
= SWM_F_MANUAL
;
1505 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1506 flag
= EWMH_F_SKIP_PAGER
;
1507 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1508 flag
= EWMH_F_SKIP_TASKBAR
;
1510 /* Disallow unfloating transients. */
1511 if (TRANS(win
) && flag
== EWMH_F_ABOVE
)
1514 new_flags
= win
->ewmh_flags
;
1517 case _NET_WM_STATE_REMOVE
:
1520 case _NET_WM_STATE_ADD
:
1523 case _NET_WM_STATE_TOGGLE
:
1528 ewmh_apply_flags(win
, new_flags
);
1531 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: done.\n");
1535 ewmh_apply_flags(struct ws_win
*win
, uint32_t pending
)
1537 struct workspace
*ws
;
1540 changed
= win
->ewmh_flags
^ pending
;
1544 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: pending: %d\n", pending
);
1546 win
->ewmh_flags
= pending
;
1549 if (changed
& EWMH_F_HIDDEN
) {
1551 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1552 ws
->focus_pending
= get_focus_prev(win
);
1557 /* Reload floating geometry in case region changed. */
1559 load_float_geom(win
);
1561 /* The window is no longer iconic, prepare focus. */
1562 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1563 ws
->focus_pending
= get_focus_magic(win
);
1568 if (changed
& EWMH_F_ABOVE
) {
1569 if (ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1571 load_float_geom(win
);
1572 else if (!MAXIMIZED(win
))
1573 store_float_geom(win
);
1575 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1576 changed
&= ~EWMH_F_MAXIMIZED
;
1580 win
->ewmh_flags
^= EWMH_F_ABOVE
& pending
;
1584 if (changed
& EWMH_F_MAXIMIZED
) {
1585 /* VERT and/or HORZ changed. */
1587 if (!MAXIMIZED(win
))
1588 load_float_geom(win
);
1590 store_float_geom(win
);
1593 if (MAXIMIZED(win
)) {
1594 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
1595 ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1599 ws
->focus_pending
= win
;
1605 if (changed
& EWMH_F_FULLSCREEN
) {
1606 if (FULLSCREEN(win
)) {
1607 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
1611 ws
->focus_pending
= win
;
1614 load_float_geom(win
);
1617 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1621 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: done.\n");
1625 ewmh_update_wm_state(struct ws_win
*win
) {
1626 xcb_atom_t vals
[SWM_EWMH_ACTION_COUNT_MAX
];
1630 vals
[n
++] = ewmh
[_NET_WM_STATE_HIDDEN
].atom
;
1631 if (FULLSCREEN(win
))
1632 vals
[n
++] = ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
;
1633 if (MAXIMIZED_VERT(win
))
1634 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
;
1635 if (MAXIMIZED_HORZ(win
))
1636 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
;
1637 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1638 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
;
1639 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1640 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
;
1641 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1642 vals
[n
++] = ewmh
[_NET_WM_STATE_ABOVE
].atom
;
1643 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1644 vals
[n
++] = ewmh
[_SWM_WM_STATE_MANUAL
].atom
;
1647 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1648 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, n
, vals
);
1650 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1654 ewmh_get_wm_state(struct ws_win
*win
)
1657 xcb_get_property_cookie_t c
;
1658 xcb_get_property_reply_t
*r
;
1664 win
->ewmh_flags
= 0;
1666 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1667 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1668 r
= xcb_get_property_reply(conn
, c
, NULL
);
1672 states
= xcb_get_property_value(r
);
1673 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1675 for (i
= 0; i
< n
; i
++)
1676 ewmh_change_wm_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1684 dumpwins(struct swm_region
*r
, union arg
*args
)
1688 xcb_get_window_attributes_cookie_t c
;
1689 xcb_get_window_attributes_reply_t
*wa
;
1691 /* suppress unused warning since var is needed */
1694 if (r
->ws
== NULL
) {
1695 DPRINTF("dumpwins: invalid workspace\n");
1699 DPRINTF("=== managed window list ws %02d ===\n", r
->ws
->idx
);
1700 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1701 state
= getstate(win
->id
);
1702 c
= xcb_get_window_attributes(conn
, win
->id
);
1703 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1705 DPRINTF("win %#x, map_state: %d, state: %u, "
1706 "transient: %#x\n", win
->id
, wa
->map_state
,
1707 state
, win
->transient
);
1710 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1714 DPRINTF("===== unmanaged window list =====\n");
1715 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1716 state
= getstate(win
->id
);
1717 c
= xcb_get_window_attributes(conn
, win
->id
);
1718 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1720 DPRINTF("win %#x, map_state: %d, state: %u, "
1721 "transient: %#x\n", win
->id
, wa
->map_state
,
1722 state
, win
->transient
);
1725 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1729 DPRINTF("=================================\n");
1733 dumpwins(struct swm_region
*r
, union arg
*s
)
1738 #endif /* SWM_DEBUG */
1743 int saved_errno
, status
;
1746 saved_errno
= errno
;
1750 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1755 if (errno
!= ECHILD
)
1756 warn("sighdlr: waitpid");
1757 #endif /* SWM_DEBUG */
1760 if (pid
== searchpid
)
1764 if (WIFEXITED(status
)) {
1765 if (WEXITSTATUS(status
) != 0)
1766 warnx("sighdlr: child exit status: %d",
1767 WEXITSTATUS(status
));
1769 warnx("sighdlr: child is terminated "
1771 #endif /* SWM_DEBUG */
1785 errno
= saved_errno
;
1791 struct pid_e
*p
= NULL
;
1793 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1798 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1807 name_to_pixel(int sidx
, const char *colorname
)
1809 uint32_t result
= 0;
1810 char cname
[32] = "#";
1811 xcb_screen_t
*screen
;
1812 xcb_colormap_t cmap
;
1813 xcb_alloc_color_reply_t
*cr
;
1814 xcb_alloc_named_color_reply_t
*nr
;
1815 uint16_t rr
, gg
, bb
;
1817 screen
= get_screen(sidx
);
1818 cmap
= screen
->default_colormap
;
1820 /* color is in format rgb://rr/gg/bb */
1821 if (strncmp(colorname
, "rgb:", 4) == 0) {
1822 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1823 warnx("could not parse rgb %s", colorname
);
1825 cr
= xcb_alloc_color_reply(conn
,
1826 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1832 warnx("color '%s' not found", colorname
);
1835 nr
= xcb_alloc_named_color_reply(conn
,
1836 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1839 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1840 nr
= xcb_alloc_named_color_reply(conn
,
1841 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1848 warnx("color '%s' not found", colorname
);
1855 setscreencolor(const char *val
, int i
, int c
)
1859 num_screens
= get_screen_count();
1860 if (i
> 0 && i
<= num_screens
) {
1861 screens
[i
- 1].c
[c
].pixel
= name_to_pixel(i
- 1, val
);
1862 free(screens
[i
- 1].c
[c
].name
);
1863 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1865 } else if (i
== -1) {
1866 for (i
= 0; i
< num_screens
; i
++) {
1867 screens
[i
].c
[c
].pixel
= name_to_pixel(0, val
);
1868 free(screens
[i
].c
[c
].name
);
1869 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1873 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1878 fancy_stacker(struct workspace
*ws
)
1880 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1881 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1882 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1883 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1884 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1885 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1886 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1887 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1888 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1892 plain_stacker(struct workspace
*ws
)
1894 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1895 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1896 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1897 sizeof ws
->stacker
);
1898 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1899 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1900 sizeof ws
->stacker
);
1904 custom_region(const char *val
)
1906 unsigned int x
, y
, w
, h
;
1907 int sidx
, num_screens
;
1908 xcb_screen_t
*screen
;
1910 num_screens
= get_screen_count();
1911 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1912 errx(1, "invalid custom region, "
1913 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1914 if (sidx
< 1 || sidx
> num_screens
)
1915 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1919 if ((screen
= get_screen(sidx
)) == NULL
)
1920 errx(1, "ERROR: can't get screen %d.", sidx
);
1923 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1925 if (x
> screen
->width_in_pixels
||
1926 y
> screen
->height_in_pixels
||
1927 w
+ x
> screen
->width_in_pixels
||
1928 h
+ y
> screen
->height_in_pixels
) {
1929 warnx("ignoring region %ux%u+%u+%u - not within screen "
1930 "boundaries (%ux%u)", w
, h
, x
, y
,
1931 screen
->width_in_pixels
, screen
->height_in_pixels
);
1935 new_region(&screens
[sidx
], x
, y
, w
, h
);
1939 socket_setnonblock(int fd
)
1943 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1944 err(1, "fcntl F_GETFL");
1945 flags
|= O_NONBLOCK
;
1946 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1947 err(1, "fcntl F_SETFL");
1951 bar_print_legacy(struct swm_region
*r
, const char *s
)
1953 xcb_rectangle_t rect
;
1958 XRectangle ibox
, lbox
;
1962 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1964 switch (bar_justify
) {
1965 case SWM_BAR_JUSTIFY_LEFT
:
1968 case SWM_BAR_JUSTIFY_CENTER
:
1969 x
= (WIDTH(r
) - lbox
.width
) / 2;
1971 case SWM_BAR_JUSTIFY_RIGHT
:
1972 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
1976 if (x
< SWM_BAR_OFFSET
)
1981 rect
.width
= WIDTH(r
->bar
);
1982 rect
.height
= HEIGHT(r
->bar
);
1984 /* clear back buffer */
1985 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1986 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1987 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
1989 /* draw back buffer */
1990 gcvd
.graphics_exposures
= 0;
1991 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
1992 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
1993 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
1994 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
1996 XFreeGC(display
, draw
);
1999 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2000 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2004 bar_print(struct swm_region
*r
, const char *s
)
2007 xcb_rectangle_t rect
;
2015 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
2017 switch (bar_justify
) {
2018 case SWM_BAR_JUSTIFY_LEFT
:
2021 case SWM_BAR_JUSTIFY_CENTER
:
2022 x
= (WIDTH(r
) - info
.width
) / 2;
2024 case SWM_BAR_JUSTIFY_RIGHT
:
2025 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
2029 if (x
< SWM_BAR_OFFSET
)
2034 rect
.width
= WIDTH(r
->bar
);
2035 rect
.height
= HEIGHT(r
->bar
);
2037 /* clear back buffer */
2038 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2039 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2040 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2042 /* draw back buffer */
2043 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
2044 DefaultVisual(display
, r
->s
->idx
),
2045 DefaultColormap(display
, r
->s
->idx
));
2047 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
2048 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
2051 XftDrawDestroy(draw
);
2054 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2055 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2059 bar_extra_stop(void)
2063 bzero(bar_pipe
, sizeof bar_pipe
);
2066 kill(bar_pid
, SIGTERM
);
2069 strlcpy(bar_ext
, "", sizeof bar_ext
);
2074 bar_window_class(char *s
, size_t sz
, struct swm_region
*r
)
2076 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2078 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
2079 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
2083 bar_window_instance(char *s
, size_t sz
, struct swm_region
*r
)
2085 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2087 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
2088 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
2092 bar_window_class_instance(char *s
, size_t sz
, struct swm_region
*r
)
2094 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2097 bar_window_class(s
, sz
, r
);
2098 strlcat(s
, ":", sz
);
2099 bar_window_instance(s
, sz
, r
);
2103 bar_window_state(char *s
, size_t sz
, struct swm_region
*r
)
2105 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2107 if (MAXIMIZED(r
->ws
->focus
))
2108 strlcat(s
, "(m)", sz
);
2109 else if (ABOVE(r
->ws
->focus
))
2110 strlcat(s
, "(f)", sz
);
2114 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
2118 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2121 title
= get_win_name(r
->ws
->focus
->id
);
2122 strlcat(s
, title
, sz
);
2127 bar_urgent(char *s
, size_t sz
)
2130 int i
, j
, num_screens
;
2131 int urgent
[SWM_WS_MAX
];
2133 xcb_get_property_cookie_t c
;
2134 xcb_icccm_wm_hints_t hints
;
2136 for (i
= 0; i
< workspace_limit
; i
++)
2139 num_screens
= get_screen_count();
2140 for (i
= 0; i
< num_screens
; i
++)
2141 for (j
= 0; j
< workspace_limit
; j
++)
2142 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
2143 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2144 if (xcb_icccm_get_wm_hints_reply(conn
, c
,
2147 if (hints
.flags
& XCB_ICCCM_WM_HINT_X_URGENCY
)
2151 for (i
= 0; i
< workspace_limit
; i
++) {
2153 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2155 snprintf(b
, sizeof b
, "- ");
2161 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2163 if (r
== NULL
|| r
->ws
== NULL
)
2165 if (r
->ws
->name
!= NULL
)
2166 strlcat(s
, r
->ws
->name
, sz
);
2169 /* build the default bar format according to the defined enabled options */
2171 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2175 /* if format provided, just copy the buffers */
2176 if (bar_format
!= NULL
) {
2177 strlcpy(fmtnew
, fmtexp
, sz
);
2181 /* reset the output buffer */
2184 strlcat(fmtnew
, "+N:+I ", sz
);
2186 strlcat(fmtnew
, "+S", sz
);
2187 strlcat(fmtnew
, " ", sz
);
2189 /* only show the workspace name if there's actually one */
2190 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2191 strlcat(fmtnew
, "<+D>", sz
);
2193 /* If enabled, only show the iconic count if there are iconic wins. */
2194 if (iconic_enabled
&& r
!= NULL
&& r
->ws
!= NULL
)
2195 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2197 strlcat(fmtnew
, "{+M}", sz
);
2201 strlcat(fmtnew
, "+3<", sz
);
2203 if (clock_enabled
) {
2204 strlcat(fmtnew
, fmtexp
, sz
);
2205 strlcat(fmtnew
, "+4<", sz
);
2208 /* bar_urgent already adds the space before the last asterisk */
2210 strlcat(fmtnew
, "* +U*+4<", sz
);
2212 if (window_class_enabled
) {
2213 strlcat(fmtnew
, "+C", sz
);
2214 if (!window_instance_enabled
)
2215 strlcat(fmtnew
, "+4<", sz
);
2218 /* checks needed by the colon and floating strlcat(3) calls below */
2219 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2220 if (window_instance_enabled
) {
2221 if (window_class_enabled
)
2222 strlcat(fmtnew
, ":", sz
);
2223 strlcat(fmtnew
, "+T+4<", sz
);
2225 if (window_name_enabled
) {
2226 if (ABOVE(r
->ws
->focus
) || MAXIMIZED(r
->ws
->focus
))
2227 strlcat(fmtnew
, "+F ", sz
);
2228 strlcat(fmtnew
, "+64W ", sz
);
2232 /* finally add the action script output and the version */
2233 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2237 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2239 /* special case; no limit given, pad one space, instead */
2240 if (*limit
== (int)sz
- 1)
2242 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2245 /* replaces the bar format character sequences (like in tmux(1)) */
2247 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2252 char tmp
[SWM_BAR_MAX
];
2253 int limit
, size
, count
;
2256 /* reset strlcat(3) buffer */
2259 /* get number, if any */
2262 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2263 limit
= sizeof tmp
- 1;
2264 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2265 limit
= sizeof tmp
- 1;
2267 /* there is nothing to replace (ie EOL) */
2274 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2277 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2280 bar_window_class(tmp
, sizeof tmp
, r
);
2283 bar_workspace_name(tmp
, sizeof tmp
, r
);
2286 bar_window_state(tmp
, sizeof tmp
, r
);
2289 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2293 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2297 snprintf(tmp
, sizeof tmp
, "%d", count
);
2300 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2303 bar_window_class_instance(tmp
, sizeof tmp
, r
);
2306 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2309 bar_window_instance(tmp
, sizeof tmp
, r
);
2312 bar_urgent(tmp
, sizeof tmp
);
2315 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2318 bar_window_name(tmp
, sizeof tmp
, r
);
2321 /* unknown character sequence; copy as-is */
2322 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2328 if ((int)len
< limit
)
2330 while (limit
-- > 0) {
2331 if (*offrep
>= sz
- 1)
2333 fmtrep
[(*offrep
)++] = *ptr
++;
2341 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2346 while (*fmt
!= '\0') {
2348 /* skip ordinary characters */
2351 fmtrep
[off
++] = *fmt
++;
2355 /* character sequence found; replace it */
2356 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2365 bar_fmt_expand(char *fmtexp
, size_t sz
)
2372 /* start by grabbing the current time and date */
2374 localtime_r(&tmt
, &tm
);
2376 /* figure out what to expand */
2377 if (bar_format
!= NULL
)
2379 else if (bar_format
== NULL
&& clock_enabled
)
2381 /* if nothing to expand bail out */
2387 /* copy as-is, just in case the format shouldn't be expanded below */
2388 strlcpy(fmtexp
, fmt
, sz
);
2389 /* finally pass the string through strftime(3) */
2390 #ifndef SWM_DENY_CLOCK_FORMAT
2391 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2392 warnx("format too long");
2397 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2401 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2402 char fmtrep
[SWM_BAR_MAX
];
2404 struct swm_region
*r
;
2406 /* expand the format by first passing it through strftime(3) */
2407 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2409 num_screens
= get_screen_count();
2410 for (i
= 0; i
< num_screens
; i
++) {
2411 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2415 if (bar_enabled
&& r
->ws
->bar_enabled
)
2416 xcb_map_window(conn
, r
->bar
->id
);
2418 xcb_unmap_window(conn
, r
->bar
->id
);
2422 if (startup_exception
)
2423 snprintf(fmtrep
, sizeof fmtrep
, "total "
2424 "exceptions: %d, first exception: %s",
2428 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2429 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2431 if (bar_font_legacy
)
2432 bar_print_legacy(r
, fmtrep
);
2434 bar_print(r
, fmtrep
);
2440 * Reads external script output; call when stdin is readable.
2441 * Returns 1 if bar_ext was updated; otherwise 0.
2444 bar_extra_update(void)
2447 char b
[SWM_BAR_MAX
];
2453 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2456 if (b
[len
- 1] == '\n') {
2457 /* Remove newline. */
2460 /* "Clear" bar_ext. */
2463 /* Flush buffered output. */
2464 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2465 bar_ext_buf
[0] = '\0';
2467 /* Append new output to bar. */
2468 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2472 /* Buffer output. */
2473 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2478 if (errno
!= EAGAIN
) {
2479 warn("bar_action failed");
2488 bar_toggle(struct swm_region
*r
, union arg
*args
)
2490 struct swm_region
*tmpr
;
2493 /* suppress unused warnings since vars are needed */
2497 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2500 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2501 /* Only change if master switch is enabled. */
2503 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2505 bar_enabled
= r
->ws
->bar_enabled
= 1;
2507 case SWM_ARG_ID_BAR_TOGGLE
:
2508 bar_enabled
= !bar_enabled
;
2512 /* update bars as necessary */
2513 num_screens
= get_screen_count();
2514 for (i
= 0; i
< num_screens
; i
++)
2515 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2517 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2518 xcb_map_window(conn
, tmpr
->bar
->id
);
2520 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2525 /* must be after stack */
2532 bar_extra_setup(void)
2534 /* do this here because the conf file is in memory */
2535 if (!bar_extra
&& bar_argv
[0]) {
2536 /* launch external status app */
2538 if (pipe(bar_pipe
) == -1)
2539 err(1, "pipe error");
2540 socket_setnonblock(bar_pipe
[0]);
2541 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2543 /* Set stdin to read from the pipe. */
2544 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2547 /* Set stdout to write to the pipe. */
2548 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2551 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2552 err(1, "could not disable SIGPIPE");
2553 switch (bar_pid
= fork()) {
2555 err(1, "cannot fork");
2559 execvp(bar_argv
[0], bar_argv
);
2560 err(1, "%s external app failed", bar_argv
[0]);
2562 default: /* parent */
2567 atexit(kill_bar_extra_atexit
);
2572 kill_bar_extra_atexit(void)
2575 kill(bar_pid
, SIGTERM
);
2583 while ((s
= index(s
, '-'))) {
2588 return (count
== 14);
2594 char *default_string
;
2595 char **missing_charsets
;
2596 int num_missing_charsets
= 0;
2600 XFreeFontSet(display
, bar_fs
);
2604 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n", bar_fonts
);
2606 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2607 &num_missing_charsets
, &default_string
);
2609 if (num_missing_charsets
> 0) {
2610 warnx("Unable to load charset(s):");
2612 for (i
= 0; i
< num_missing_charsets
; ++i
)
2613 warnx("%s", missing_charsets
[i
]);
2615 XFreeStringList(missing_charsets
);
2617 if (strcmp(default_string
, ""))
2618 warnx("Glyphs from those sets will be replaced "
2619 "by '%s'.", default_string
);
2621 warnx("Glyphs from those sets won't be drawn.");
2625 errx(1, "Error creating font set structure.");
2627 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2629 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2630 2 * bar_border_width
;
2637 xft_init(struct swm_region
*r
)
2639 char *font
, *d
, *search
;
2642 if (bar_font
== NULL
) {
2643 if ((d
= strdup(bar_fonts
)) == NULL
)
2644 errx(1, "insufficient memory.");
2646 while ((font
= strsep(&search
, ",")) != NULL
) {
2650 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2653 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2656 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2660 if (bar_font
== NULL
) {
2661 warnx("unable to load font %s", font
);
2664 DNPRINTF(SWM_D_INIT
, "successfully opened "
2672 if (bar_font
== NULL
)
2673 errx(1, "unable to open a font");
2675 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2677 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2678 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2679 warn("Xft error: unable to allocate color.");
2681 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2688 bar_setup(struct swm_region
*r
)
2690 xcb_screen_t
*screen
;
2693 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2696 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2697 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2702 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2703 err(1, "bar_setup: calloc: failed to allocate memory.");
2705 if (bar_font_legacy
)
2711 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2712 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2713 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2715 /* Assume region is unfocused when we create the bar. */
2716 r
->bar
->id
= xcb_generate_id(conn
);
2717 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2718 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
2719 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
2720 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
2722 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2723 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2724 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
2725 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
2726 | XCB_CW_EVENT_MASK
, wa
);
2728 r
->bar
->buffer
= xcb_generate_id(conn
);
2729 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
2730 WIDTH(r
->bar
), HEIGHT(r
->bar
));
2733 xcb_randr_select_input(conn
, r
->bar
->id
,
2734 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
2737 xcb_map_window(conn
, r
->bar
->id
);
2739 DNPRINTF(SWM_D_BAR
, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
2740 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
2747 bar_cleanup(struct swm_region
*r
)
2751 xcb_destroy_window(conn
, r
->bar
->id
);
2752 xcb_free_pixmap(conn
, r
->bar
->buffer
);
2758 set_win_state(struct ws_win
*win
, uint16_t state
)
2760 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
2762 DNPRINTF(SWM_D_EVENT
, "set_win_state: win %#x, state: %u\n",
2768 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
2769 a_state
, 32, 2, data
);
2773 getstate(xcb_window_t w
)
2775 uint32_t result
= 0;
2776 xcb_get_property_cookie_t c
;
2777 xcb_get_property_reply_t
*r
;
2779 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
2780 r
= xcb_get_property_reply(conn
, c
, NULL
);
2782 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
2783 result
= *((uint32_t *)xcb_get_property_value(r
));
2787 DNPRINTF(SWM_D_MISC
, "getstate property: win %#x state %u\n", w
,
2793 version(struct swm_region
*r
, union arg
*args
)
2795 /* suppress unused warnings since vars are needed */
2799 bar_version
= !bar_version
;
2801 snprintf(bar_vertext
, sizeof bar_vertext
,
2802 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2804 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2811 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
2813 xcb_client_message_event_t ev
;
2821 name
= get_atom_name(a
);
2822 DNPRINTF(SWM_D_EVENT
, "client_msg: win %#x, atom: %s(%u), "
2824 win
->id
, name
, a
, t
);
2828 bzero(&ev
, sizeof ev
);
2829 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2830 ev
.window
= win
->id
;
2833 ev
.data
.data32
[0] = a
;
2834 ev
.data
.data32
[1] = t
;
2836 xcb_send_event(conn
, 0, win
->id
,
2837 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2840 /* synthetic response to a ConfigureRequest when not making a change */
2842 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2844 xcb_configure_notify_event_t ce
;
2849 /* send notification of unchanged state. */
2850 bzero(&ce
, sizeof(ce
));
2851 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2854 ce
.width
= WIDTH(win
);
2855 ce
.height
= HEIGHT(win
);
2856 ce
.override_redirect
= 0;
2861 ce
.window
= win
->id
;
2862 ce
.border_width
= BORDER(win
);
2863 ce
.above_sibling
= XCB_WINDOW_NONE
;
2866 ce
.event
= ev
->window
;
2867 ce
.window
= ev
->window
;
2869 /* make response appear more WM_SIZE_HINTS-compliant */
2870 if (win
->sh
.flags
) {
2871 DNPRINTF(SWM_D_MISC
, "config_win: hints: win %#x,"
2872 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2873 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2874 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2875 SH_INC_W(win
), SH_INC_H(win
));
2880 /* the hint may be set... to 0! */
2881 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2882 ce
.width
= SH_MIN_W(win
);
2883 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2884 ce
.height
= SH_MIN_H(win
);
2889 /* may also be advertized as 0 */
2890 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2891 ce
.width
= SH_MAX_W(win
);
2892 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2893 ce
.height
= SH_MAX_H(win
);
2896 /* resize increment. */
2898 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2899 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2901 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2902 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2906 /* adjust x and y for requested border_width. */
2907 ce
.x
+= BORDER(win
) - ev
->border_width
;
2908 ce
.y
+= BORDER(win
) - ev
->border_width
;
2909 ce
.border_width
= ev
->border_width
;
2910 ce
.above_sibling
= ev
->sibling
;
2913 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
2914 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2915 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2917 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2922 count_win(struct workspace
*ws
, int count_transient
)
2927 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2928 if (!count_transient
&& FLOATING(win
))
2934 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2940 quit(struct swm_region
*r
, union arg
*args
)
2942 /* suppress unused warnings since vars are needed */
2946 DNPRINTF(SWM_D_MISC
, "quit\n");
2951 raise_window(struct ws_win
*win
)
2953 struct ws_win
*target
= NULL
;
2954 struct swm_region
*r
;
2955 struct workspace
*ws
;
2957 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
2961 DNPRINTF(SWM_D_EVENT
, "raise_window: win %#x\n", win
->id
);
2963 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
2964 if (target
== win
|| ICONIC(target
))
2966 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
2968 if (FULLSCREEN(win
))
2970 if (FULLSCREEN(target
))
2974 if (MAXIMIZED(target
))
2976 if (ABOVE(win
) || TRANS(win
))
2978 if (!ABOVE(target
) && !TRANS(target
))
2982 if (target
!= NULL
) {
2983 /* Change stack position. */
2984 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
2985 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
2986 update_win_stacking(win
);
2989 DNPRINTF(SWM_D_EVENT
, "raise_window: done\n");
2993 update_win_stacking(struct ws_win
*win
)
2995 struct ws_win
*sibling
;
2996 struct swm_region
*r
;
2999 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
3002 sibling
= TAILQ_NEXT(win
, stack_entry
);
3003 if (sibling
!= NULL
&& FLOATING(win
) == FLOATING(sibling
))
3004 val
[0] = sibling
->id
;
3006 val
[0] = FLOATING(win
) ? r
->bar
->id
: r
->id
;
3008 DNPRINTF(SWM_D_EVENT
, "update_win_stacking: %#x, sibling %#x\n",
3011 val
[1] = XCB_STACK_MODE_ABOVE
;
3013 xcb_configure_window(conn
, win
->id
, XCB_CONFIG_WINDOW_SIBLING
|
3014 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
3018 map_window(struct ws_win
*win
)
3023 DNPRINTF(SWM_D_EVENT
, "map_window: win %#x, mapped: %s\n",
3024 win
->id
, YESNO(win
->mapped
));
3029 xcb_map_window(conn
, win
->id
);
3030 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
3035 unmap_window(struct ws_win
*win
)
3040 DNPRINTF(SWM_D_EVENT
, "unmap_window: win %#x, mapped: %s\n", win
->id
,
3041 YESNO(win
->mapped
));
3046 xcb_unmap_window(conn
, win
->id
);
3047 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
3055 int i
, j
, num_screens
;
3057 num_screens
= get_screen_count();
3058 for (i
= 0; i
< num_screens
; i
++)
3059 for (j
= 0; j
< workspace_limit
; j
++)
3060 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3065 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
3067 xcb_key_press_event_t event
;
3068 xcb_keycode_t
*keycode
;
3073 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
3075 DNPRINTF(SWM_D_MISC
, "fake_keypress: win %#x, keycode %u\n",
3078 bzero(&event
, sizeof(event
));
3079 event
.event
= win
->id
;
3080 event
.root
= win
->s
->root
;
3081 event
.child
= XCB_WINDOW_NONE
;
3082 event
.time
= XCB_CURRENT_TIME
;
3083 event
.event_x
= X(win
);
3084 event
.event_y
= Y(win
);
3087 event
.same_screen
= 1;
3088 event
.detail
= *keycode
;
3089 event
.state
= modifiers
;
3091 event
.response_type
= XCB_KEY_PRESS
;
3092 xcb_send_event(conn
, 1, win
->id
,
3093 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
3095 event
.response_type
= XCB_KEY_RELEASE
;
3096 xcb_send_event(conn
, 1, win
->id
,
3097 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
3103 restart(struct swm_region
*r
, union arg
*args
)
3105 /* suppress unused warning since var is needed */
3109 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
3113 execvp(start_argv
[0], start_argv
);
3114 warn("execvp failed");
3119 get_pointer_win(xcb_window_t root
)
3121 struct ws_win
*win
= NULL
;
3122 xcb_query_pointer_reply_t
*r
;
3124 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: %#x.\n", root
);
3126 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
3128 win
= find_window(r
->child
);
3130 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: %#x.\n",
3133 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
3142 root_to_region(xcb_window_t root
, int check
)
3145 struct swm_region
*r
= NULL
;
3147 xcb_query_pointer_reply_t
*qpr
;
3148 xcb_get_input_focus_reply_t
*gifr
;
3150 DNPRINTF(SWM_D_MISC
, "root_to_region: win %#x\n", root
);
3152 num_screens
= get_screen_count();
3153 for (i
= 0; i
< num_screens
; i
++)
3154 if (screens
[i
].root
== root
)
3157 if (check
& SWM_CK_REGION
)
3158 r
= screens
[i
].r_focus
;
3160 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
3161 /* Try to find an actively focused window */
3162 gifr
= xcb_get_input_focus_reply(conn
,
3163 xcb_get_input_focus(conn
), NULL
);
3165 cfw
= find_window(gifr
->focus
);
3166 if (cfw
&& cfw
->ws
->r
)
3173 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
3174 /* No region with an active focus; try to use pointer. */
3175 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
3176 screens
[i
].root
), NULL
);
3178 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
3179 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
3180 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3181 if (X(r
) <= qpr
->root_x
&&
3182 qpr
->root_x
< MAX_X(r
) &&
3183 Y(r
) <= qpr
->root_y
&&
3184 qpr
->root_y
< MAX_Y(r
))
3191 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
3192 r
= TAILQ_FIRST(&screens
[i
].rl
);
3194 DNPRINTF(SWM_D_MISC
, "root_to_region: idx: %d\n", get_region_index(r
));
3200 find_unmanaged_window(xcb_window_t id
)
3203 int i
, j
, num_screens
;
3205 num_screens
= get_screen_count();
3206 for (i
= 0; i
< num_screens
; i
++)
3207 for (j
= 0; j
< workspace_limit
; j
++)
3208 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
3216 find_window(xcb_window_t id
)
3219 int i
, j
, num_screens
;
3220 xcb_query_tree_reply_t
*r
;
3222 num_screens
= get_screen_count();
3223 for (i
= 0; i
< num_screens
; i
++)
3224 for (j
= 0; j
< workspace_limit
; j
++)
3225 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3229 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3233 /* if we were looking for the parent return that window instead */
3234 if (r
->parent
== 0 || r
->root
== r
->parent
) {
3239 /* look for parent */
3240 for (i
= 0; i
< num_screens
; i
++)
3241 for (j
= 0; j
< workspace_limit
; j
++)
3242 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3243 if (r
->parent
== win
->id
) {
3253 spawn(int ws_idx
, union arg
*args
, int close_fd
)
3258 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3260 close(xcb_get_file_descriptor(conn
));
3262 setenv("LD_PRELOAD", SWM_LIB
, 1);
3264 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3265 warn("spawn: asprintf SWM_WS");
3268 setenv("_SWM_WS", ret
, 1);
3272 if (asprintf(&ret
, "%d", getpid()) == -1) {
3273 warn("spawn: asprintf _SWM_PID");
3276 setenv("_SWM_PID", ret
, 1);
3280 if (setsid() == -1) {
3281 warn("spawn: setsid");
3287 * close stdin and stdout to prevent interaction between apps
3288 * and the baraction script
3289 * leave stderr open to record errors
3291 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3292 warn("spawn: open");
3295 dup2(fd
, STDIN_FILENO
);
3296 dup2(fd
, STDOUT_FILENO
);
3301 execvp(args
->argv
[0], args
->argv
);
3303 warn("spawn: execvp");
3308 kill_refs(struct ws_win
*win
)
3310 int i
, x
, num_screens
;
3311 struct swm_region
*r
;
3312 struct workspace
*ws
;
3317 num_screens
= get_screen_count();
3318 for (i
= 0; i
< num_screens
; i
++)
3319 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3320 for (x
= 0; x
< workspace_limit
; x
++) {
3322 if (win
== ws
->focus
)
3324 if (win
== ws
->focus_prev
)
3325 ws
->focus_prev
= NULL
;
3330 validate_win(struct ws_win
*testwin
)
3333 struct workspace
*ws
;
3334 struct swm_region
*r
;
3335 int i
, x
, num_screens
;
3337 if (testwin
== NULL
)
3340 num_screens
= get_screen_count();
3341 for (i
= 0; i
< num_screens
; i
++)
3342 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3343 for (x
= 0; x
< workspace_limit
; x
++) {
3345 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3353 validate_ws(struct workspace
*testws
)
3355 struct swm_region
*r
;
3356 struct workspace
*ws
;
3357 int i
, x
, num_screens
;
3359 /* validate all ws */
3360 num_screens
= get_screen_count();
3361 for (i
= 0; i
< num_screens
; i
++)
3362 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3363 for (x
= 0; x
< workspace_limit
; x
++) {
3372 unfocus_win(struct ws_win
*win
)
3374 xcb_window_t none
= XCB_WINDOW_NONE
;
3376 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: win %#x\n", WINID(win
));
3381 if (win
->ws
== NULL
) {
3382 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3386 if (validate_ws(win
->ws
)) {
3387 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3391 if (win
->ws
->r
== NULL
) {
3392 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3396 if (validate_win(win
)) {
3397 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3402 if (win
->ws
->focus
== win
) {
3403 win
->ws
->focus
= NULL
;
3404 win
->ws
->focus_prev
= win
;
3407 if (validate_win(win
->ws
->focus
)) {
3408 kill_refs(win
->ws
->focus
);
3409 win
->ws
->focus
= NULL
;
3412 if (validate_win(win
->ws
->focus_prev
)) {
3413 kill_refs(win
->ws
->focus_prev
);
3414 win
->ws
->focus_prev
= NULL
;
3417 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
3418 &win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
3420 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3421 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3423 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3427 focus_win(struct ws_win
*win
)
3429 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
;
3430 struct workspace
*ws
;
3431 xcb_get_input_focus_reply_t
*gifr
;
3433 DNPRINTF(SWM_D_FOCUS
, "focus_win: win %#x\n", WINID(win
));
3438 if (win
->ws
== NULL
)
3446 if (validate_ws(ws
))
3449 if (validate_win(win
)) {
3454 gifr
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3456 cfw
= find_window(gifr
->focus
);
3457 if (cfw
!= NULL
&& cfw
!= win
) {
3458 if (cfw
->ws
!= ws
&& cfw
->ws
->r
!= NULL
) {
3459 /* Change border to unfocused color. */
3460 xcb_change_window_attributes(conn
, cfw
->id
,
3461 XCB_CW_BORDER_PIXEL
,
3462 &cfw
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
3470 if (ws
->focus
!= win
) {
3471 if (ws
->focus
&& ws
->focus
!= cfw
)
3472 unfocus_win(ws
->focus
);
3476 /* If this window directs focus to a child window, then clear. */
3477 if (win
->focus_child
)
3478 win
->focus_child
= NULL
;
3480 /* If transient, adjust parent's focus child for focus_magic. */
3482 parent
= find_window(win
->transient
);
3483 if (parent
&& parent
->focus_child
!= win
)
3484 parent
->focus_child
= win
;
3487 if (cfw
!= win
&& ws
->r
!= NULL
) {
3488 /* Set input focus if no input hint, or indicated by hint. */
3489 if (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
3490 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
3492 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3493 win
->id
, last_event_time
);
3495 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3496 ws
->r
->id
, XCB_CURRENT_TIME
);
3498 /* Tell app it can adjust focus to a specific window. */
3499 if (win
->take_focus
) {
3500 /* java is special; always tell parent */
3501 if (TRANS(win
) && win
->java
)
3502 client_msg(parent
, a_takefocus
,
3505 client_msg(win
, a_takefocus
, last_event_time
);
3508 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
3509 &ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
);
3511 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
3513 /* If a parent exists, map it first. */
3515 raise_window(parent
);
3518 /* Map siblings next. */
3519 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3520 if (w
!= win
&& !ICONIC(w
) &&
3521 win
->transient
== parent
->id
)
3525 /* Map focused window. */
3529 /* Finally, map children of focus window. */
3530 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3531 if (w
->transient
== win
->id
&& !ICONIC(w
))
3533 } else if (tile_gap
< 0 && !ABOVE(win
)) {
3535 * Windows overlap in the layout.
3536 * Raise focused win above all tiled wins.
3544 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3545 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3552 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
3555 /* If a child window should have focus instead, return it. */
3557 get_focus_magic(struct ws_win
*win
)
3559 struct ws_win
*parent
= NULL
;
3560 struct ws_win
*child
= NULL
;
3562 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: win %#x\n", WINID(win
));
3567 parent
= find_window(win
->transient
);
3569 /* If parent prefers focus elsewhere, then try to do so. */
3570 if (parent
&& (child
= parent
->focus_child
)) {
3571 if (validate_win(child
) == 0 && child
->mapped
)
3574 parent
->focus_child
= NULL
;
3578 /* If this window prefers focus elsewhere, then try to do so. */
3579 if ((child
= win
->focus_child
)) {
3580 if (validate_win(child
) == 0 && child
->mapped
)
3583 win
->focus_child
= NULL
;
3590 event_drain(uint8_t rt
)
3592 xcb_generic_event_t
*evt
;
3594 /* ensure all pending requests have been processed before filtering. */
3596 while ((evt
= xcb_poll_for_event(conn
))) {
3597 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
3605 set_region(struct swm_region
*r
)
3607 struct swm_region
*rf
;
3613 /* Unfocus old region bar. */
3618 xcb_change_window_attributes(conn
, rf
->bar
->id
,
3619 XCB_CW_BORDER_PIXEL
,
3620 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
3623 /* Set region bar border to focus_color. */
3624 xcb_change_window_attributes(conn
, r
->bar
->id
,
3625 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
3629 ewmh_update_current_desktop();
3633 focus_region(struct swm_region
*r
)
3636 struct swm_region
*old_r
;
3641 old_r
= r
->s
->r_focus
;
3644 nfw
= get_region_focus(r
);
3648 /* New region is empty; need to manually unfocus win. */
3650 unfocus_win(old_r
->ws
->focus
);
3652 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
3655 /* Clear bar since empty. */
3661 switchws(struct swm_region
*r
, union arg
*args
)
3663 struct swm_region
*this_r
, *other_r
;
3665 struct workspace
*new_ws
, *old_ws
;
3666 xcb_window_t none
= XCB_WINDOW_NONE
;
3667 int wsid
= args
->id
, unmap_old
= 0;
3672 if (wsid
>= workspace_limit
)
3676 old_ws
= this_r
->ws
;
3677 new_ws
= &this_r
->s
->ws
[wsid
];
3679 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3680 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
3682 if (new_ws
== NULL
|| old_ws
== NULL
)
3684 if (new_ws
== old_ws
)
3687 if ((win
= old_ws
->focus
) != NULL
) {
3688 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
3689 &win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
3691 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3692 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3696 other_r
= new_ws
->r
;
3697 if (other_r
== NULL
) {
3698 /* the other workspace is hidden, hide this one */
3702 /* the other ws is visible in another region, exchange them */
3703 other_r
->ws_prior
= new_ws
;
3704 other_r
->ws
= old_ws
;
3705 old_ws
->r
= other_r
;
3707 this_r
->ws_prior
= old_ws
;
3708 this_r
->ws
= new_ws
;
3711 /* Set focus_pending before stacking, if needed. */
3712 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
3713 validate_win(new_ws
->focus_pending
))) {
3714 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
3715 new_ws
->focus
= new_ws
->focus_prev
;
3716 new_ws
->focus_prev
= NULL
;
3719 new_ws
->state
= SWM_WS_STATE_MAPPING
;
3722 /* unmap old windows */
3724 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
3726 old_ws
->state
= SWM_WS_STATE_HIDDEN
;
3729 /* if workspaces were swapped, then don't wait to set focus */
3730 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
3731 if (new_ws
->focus_pending
) {
3732 focus_win(new_ws
->focus_pending
);
3733 new_ws
->focus_pending
= NULL
;
3737 /* Clear bar and set focus on region input win if new ws is empty. */
3738 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
3739 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
3744 ewmh_update_current_desktop();
3747 new_ws
->state
= SWM_WS_STATE_MAPPED
;
3749 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
3753 cyclews(struct swm_region
*r
, union arg
*args
)
3756 struct swm_screen
*s
= r
->s
;
3760 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3761 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3767 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
3770 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
3773 case SWM_ARG_ID_CYCLEWS_UP
:
3774 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
3776 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
3779 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
3782 case SWM_ARG_ID_CYCLEWS_DOWN
:
3783 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
3790 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
3792 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
3799 } while (a
.id
!= r
->ws
->idx
);
3801 DNPRINTF(SWM_D_FOCUS
, "cyclews: done\n");
3805 priorws(struct swm_region
*r
, union arg
*args
)
3811 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3812 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3814 if (r
->ws_prior
== NULL
)
3817 a
.id
= r
->ws_prior
->idx
;
3819 DNPRINTF(SWM_D_FOCUS
, "priorws: done\n");
3823 focusrg(struct swm_region
*r
, union arg
*args
)
3825 int ridx
= args
->id
, i
, num_screens
;
3826 struct swm_region
*rr
= NULL
;
3828 num_screens
= get_screen_count();
3829 /* do nothing if we don't have more than one screen */
3830 if (!(num_screens
> 1 || outputs
> 1))
3833 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
3835 rr
= TAILQ_FIRST(&r
->s
->rl
);
3836 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
3837 rr
= TAILQ_NEXT(rr
, entry
);
3844 DNPRINTF(SWM_D_FOCUS
, "focusrg: done\n");
3848 cyclerg(struct swm_region
*r
, union arg
*args
)
3850 struct swm_region
*rr
= NULL
;
3853 num_screens
= get_screen_count();
3854 /* do nothing if we don't have more than one screen */
3855 if (!(num_screens
> 1 || outputs
> 1))
3859 DNPRINTF(SWM_D_FOCUS
, "cyclerg: id: %d, region: %d\n", args
->id
, i
);
3862 case SWM_ARG_ID_CYCLERG_UP
:
3863 rr
= TAILQ_NEXT(r
, entry
);
3865 rr
= TAILQ_FIRST(&screens
[i
].rl
);
3867 case SWM_ARG_ID_CYCLERG_DOWN
:
3868 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
3870 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
3880 DNPRINTF(SWM_D_FOCUS
, "cyclerg: done\n");
3883 /* Sorts transients after parent. */
3885 sort_windows(struct ws_win_list
*wl
)
3887 struct ws_win
*win
, *parent
, *nxt
;
3892 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
3893 nxt
= TAILQ_NEXT(win
, entry
);
3895 parent
= find_window(win
->transient
);
3896 if (parent
== NULL
) {
3897 warnx("not possible bug");
3900 TAILQ_REMOVE(wl
, win
, entry
);
3901 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
3907 swapwin(struct swm_region
*r
, union arg
*args
)
3909 struct ws_win
*target
, *source
;
3910 struct ws_win
*cur_focus
;
3911 struct ws_win_list
*wl
;
3913 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3914 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3916 cur_focus
= r
->ws
->focus
;
3917 if (cur_focus
== NULL
|| ABOVE(cur_focus
) || FULLSCREEN(cur_focus
))
3920 if (r
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3923 clear_maximized(r
->ws
);
3926 wl
= &source
->ws
->winlist
;
3929 case SWM_ARG_ID_SWAPPREV
:
3931 source
= find_window(source
->transient
);
3932 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
3933 if (target
&& target
->transient
)
3934 target
= find_window(target
->transient
);
3935 TAILQ_REMOVE(wl
, source
, entry
);
3937 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3939 TAILQ_INSERT_BEFORE(target
, source
, entry
);
3941 case SWM_ARG_ID_SWAPNEXT
:
3942 target
= TAILQ_NEXT(source
, entry
);
3943 /* move the parent and let the sort handle the move */
3945 source
= find_window(source
->transient
);
3946 TAILQ_REMOVE(wl
, source
, entry
);
3948 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3950 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
3952 case SWM_ARG_ID_SWAPMAIN
:
3953 target
= TAILQ_FIRST(wl
);
3954 if (target
== source
) {
3955 if (source
->ws
->focus_prev
!= NULL
&&
3956 source
->ws
->focus_prev
!= target
)
3957 source
= source
->ws
->focus_prev
;
3961 if (target
== NULL
|| source
== NULL
)
3963 source
->ws
->focus_prev
= target
;
3964 TAILQ_REMOVE(wl
, target
, entry
);
3965 TAILQ_INSERT_BEFORE(source
, target
, entry
);
3966 TAILQ_REMOVE(wl
, source
, entry
);
3967 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3969 case SWM_ARG_ID_MOVELAST
:
3970 TAILQ_REMOVE(wl
, source
, entry
);
3971 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3974 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
3980 ewmh_update_client_list();
3988 get_focus_prev(struct ws_win
*win
)
3990 struct ws_win
*winfocus
= NULL
;
3991 struct ws_win
*cur_focus
= NULL
;
3992 struct ws_win_list
*wl
= NULL
;
3993 struct workspace
*ws
= NULL
;
3995 if (!(win
&& win
->ws
))
4000 cur_focus
= ws
->focus
;
4002 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: win %#x, cur_focus: %#x, "
4003 "focus_prev: %#x\n", WINID(win
), WINID(cur_focus
),
4004 WINID(ws
->focus_prev
));
4006 /* pickle, just focus on whatever */
4007 if (cur_focus
== NULL
) {
4008 /* use prev_focus if valid */
4009 if (ws
->focus_prev
&& find_window(ws
->focus_prev
->id
))
4010 winfocus
= ws
->focus_prev
;
4014 /* if transient focus on parent */
4015 if (TRANS(cur_focus
)) {
4016 winfocus
= find_window(cur_focus
->transient
);
4020 /* if in max_stack try harder */
4021 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
4022 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
4023 if (cur_focus
!= ws
->focus_prev
)
4024 winfocus
= ws
->focus_prev
;
4026 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
4031 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
4033 if (winfocus
== NULL
|| winfocus
== win
) {
4034 switch (focus_close
) {
4035 case SWM_STACK_BOTTOM
:
4036 TAILQ_FOREACH(winfocus
, wl
, entry
)
4037 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4041 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4042 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4045 case SWM_STACK_ABOVE
:
4046 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
4047 while (winfocus
&& ICONIC(winfocus
))
4048 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4050 if (winfocus
== NULL
) {
4051 if (focus_close_wrap
) {
4052 TAILQ_FOREACH(winfocus
, wl
, entry
)
4053 if (!ICONIC(winfocus
) &&
4054 winfocus
!= cur_focus
)
4057 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4059 if (!ICONIC(winfocus
) &&
4060 winfocus
!= cur_focus
)
4065 case SWM_STACK_BELOW
:
4066 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
4067 while (winfocus
&& ICONIC(winfocus
))
4068 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
4071 if (winfocus
== NULL
) {
4072 if (focus_close_wrap
) {
4073 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4075 if (!ICONIC(winfocus
) &&
4076 winfocus
!= cur_focus
)
4079 TAILQ_FOREACH(winfocus
, wl
, entry
)
4080 if (!ICONIC(winfocus
) &&
4081 winfocus
!= cur_focus
)
4089 if (winfocus
== NULL
||
4090 (winfocus
&& (ICONIC(winfocus
) || winfocus
== cur_focus
))) {
4091 if (focus_default
== SWM_STACK_TOP
) {
4092 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4093 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4096 TAILQ_FOREACH(winfocus
, wl
, entry
)
4097 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4104 return get_focus_magic(winfocus
);
4108 get_region_focus(struct swm_region
*r
)
4110 struct ws_win
*winfocus
= NULL
;
4115 if (r
->ws
->focus
&& !ICONIC(r
->ws
->focus
))
4116 winfocus
= r
->ws
->focus
;
4117 else if (r
->ws
->focus_prev
&& !ICONIC(r
->ws
->focus_prev
))
4118 winfocus
= r
->ws
->focus_prev
;
4120 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
4121 if (!ICONIC(winfocus
))
4124 return get_focus_magic(winfocus
);
4128 focus(struct swm_region
*r
, union arg
*args
)
4130 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
4131 struct ws_win_list
*wl
= NULL
;
4132 struct workspace
*ws
= NULL
;
4135 xcb_icccm_wm_hints_t hints
;
4140 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
4142 cur_focus
= r
->ws
->focus
;
4146 /* Make sure an uniconified window has focus, if one exists. */
4147 if (cur_focus
== NULL
) {
4148 cur_focus
= TAILQ_FIRST(wl
);
4149 while (cur_focus
!= NULL
&& ICONIC(cur_focus
))
4150 cur_focus
= TAILQ_NEXT(cur_focus
, entry
);
4154 case SWM_ARG_ID_FOCUSPREV
:
4155 if (cur_focus
== NULL
)
4158 winfocus
= cur_focus
;
4160 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
4161 if (winfocus
== NULL
)
4162 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
4163 if (winfocus
== cur_focus
)
4165 } while (winfocus
!= NULL
&&
4166 (ICONIC(winfocus
) || winfocus
->id
== cur_focus
->transient
));
4168 case SWM_ARG_ID_FOCUSNEXT
:
4169 if (cur_focus
== NULL
)
4172 winfocus
= cur_focus
;
4174 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4175 if (winfocus
== NULL
)
4176 winfocus
= TAILQ_FIRST(wl
);
4177 if (winfocus
== cur_focus
)
4179 } while (winfocus
!= NULL
&&
4180 (ICONIC(winfocus
) || winfocus
->id
== cur_focus
->transient
));
4182 case SWM_ARG_ID_FOCUSMAIN
:
4183 if (cur_focus
== NULL
)
4186 winfocus
= TAILQ_FIRST(wl
);
4187 if (winfocus
== cur_focus
)
4188 winfocus
= cur_focus
->ws
->focus_prev
;
4190 case SWM_ARG_ID_FOCUSURGENT
:
4191 /* Search forward for the next urgent window. */
4195 for (i
= 0; i
<= workspace_limit
; ++i
) {
4197 head
= TAILQ_FIRST(&r
->s
->ws
[(ws
->idx
+ i
) %
4198 workspace_limit
].winlist
);
4200 while (head
!= NULL
&&
4201 (head
= TAILQ_NEXT(head
, entry
)) != NULL
) {
4202 if (head
== cur_focus
) {
4203 winfocus
= cur_focus
;
4206 if (xcb_icccm_get_wm_hints_reply(conn
,
4207 xcb_icccm_get_wm_hints(conn
, head
->id
),
4208 &hints
, NULL
) != 0 &&
4209 xcb_icccm_wm_hints_get_urgency(&hints
)) {
4215 if (winfocus
!= NULL
)
4219 /* Switch ws if new focus is on a different ws. */
4220 if (winfocus
!= NULL
&& winfocus
->ws
!= ws
) {
4221 a
.id
= winfocus
->ws
->idx
;
4229 if (clear_maximized(ws
) > 0)
4232 focus_win(get_focus_magic(winfocus
));
4236 DNPRINTF(SWM_D_FOCUS
, "focus: done\n");
4240 cycle_layout(struct swm_region
*r
, union arg
*args
)
4242 struct workspace
*ws
= r
->ws
;
4244 /* suppress unused warning since var is needed */
4247 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
4250 if (ws
->cur_layout
->l_stack
== NULL
)
4251 ws
->cur_layout
= &layouts
[0];
4253 clear_maximized(ws
);
4258 focus_win(get_region_focus(r
));
4264 stack_config(struct swm_region
*r
, union arg
*args
)
4266 struct workspace
*ws
= r
->ws
;
4268 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
4271 if (clear_maximized(ws
) > 0)
4274 if (ws
->cur_layout
->l_config
!= NULL
)
4275 ws
->cur_layout
->l_config(ws
, args
->id
);
4277 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
4286 struct swm_geometry g
;
4287 struct swm_region
*r
, *r_prev
= NULL
;
4294 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
4296 num_screens
= get_screen_count();
4297 for (i
= 0; i
< num_screens
; i
++) {
4301 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
4302 /* Adjust stack area for region bar and padding. */
4304 g
.x
+= region_padding
;
4305 g
.y
+= region_padding
;
4306 g
.w
-= 2 * border_width
+ 2 * region_padding
;
4307 g
.h
-= 2 * border_width
+ 2 * region_padding
;
4308 if (bar_enabled
&& r
->ws
->bar_enabled
) {
4314 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d (screen: "
4315 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4316 r
->ws
->idx
, i
, j
++, g
.x
, g
.y
, g
.w
, g
.h
);
4319 /* Stack bar/input relative to prev. region. */
4320 val
[1] = XCB_STACK_MODE_ABOVE
;
4322 val
[0] = r_prev
->id
;
4323 DNPRINTF(SWM_D_STACK
, "stack: region input %#x "
4324 "relative to %#x.\n", r
->id
, val
[0]);
4325 xcb_configure_window(conn
, r
->id
,
4326 XCB_CONFIG_WINDOW_SIBLING
|
4327 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4329 val
[0] = r_prev
->bar
->id
;
4330 DNPRINTF(SWM_D_STACK
, "stack: region bar %#x "
4331 "relative to %#x.\n", r
->bar
->id
, val
[0]);
4332 xcb_configure_window(conn
, r
->bar
->id
,
4333 XCB_CONFIG_WINDOW_SIBLING
|
4334 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4337 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
4338 r
->ws
->cur_layout
->l_string(r
->ws
);
4339 /* save r so we can track region changes */
4347 DNPRINTF(SWM_D_STACK
, "stack: end\n");
4351 store_float_geom(struct ws_win
*win
)
4353 if (win
== NULL
|| win
->ws
->r
== NULL
)
4356 /* retain window geom and region geom */
4357 win
->g_float
= win
->g
;
4358 win
->g_float
.x
-= X(win
->ws
->r
);
4359 win
->g_float
.y
-= Y(win
->ws
->r
);
4360 win
->g_floatvalid
= 1;
4361 DNPRINTF(SWM_D_MISC
, "store_float_geom: win %#x, g: (%d,%d)"
4362 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
4363 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
4364 win
->g_float
.w
, win
->g_float
.h
);
4368 load_float_geom(struct ws_win
*win
)
4370 if (win
== NULL
|| win
->ws
->r
== NULL
)
4373 if (win
->g_floatvalid
) {
4374 win
->g
= win
->g_float
;
4375 X(win
) += X(win
->ws
->r
);
4376 Y(win
) += Y(win
->ws
->r
);
4377 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g: (%d,%d)"
4378 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
4381 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g_float "
4382 "is not set.\n", win
->id
);
4387 update_floater(struct ws_win
*win
)
4389 struct workspace
*ws
;
4390 struct swm_region
*r
;
4397 if ((r
= ws
->r
) == NULL
)
4400 DNPRINTF(SWM_D_MISC
, "update_floater: win %#x\n", win
->id
);
4402 if (FULLSCREEN(win
)) {
4403 /* _NET_WM_FULLSCREEN: fullscreen without border. */
4404 if (!win
->g_floatvalid
)
4405 store_float_geom(win
);
4409 } else if (MAXIMIZED(win
)) {
4410 /* Maximize: like a single stacked window. */
4411 if (!win
->g_floatvalid
)
4412 store_float_geom(win
);
4416 if (bar_enabled
&& ws
->bar_enabled
) {
4419 Y(win
) += bar_height
;
4420 HEIGHT(win
) -= bar_height
;
4421 } else if (disable_border
) {
4427 if (win
->bordered
) {
4428 HEIGHT(win
) -= 2 * border_width
;
4429 WIDTH(win
) -= 2 * border_width
;
4432 /* Normal floating window. */
4433 /* Update geometry if new region. */
4435 load_float_geom(win
);
4437 if ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
4438 WIDTH(win
) >= WIDTH(r
) && HEIGHT(win
) >= HEIGHT(r
)) {
4439 /* Remove border for FULLSCREEN quirk. */
4441 } else if (!MANUAL(win
)) {
4442 if (TRANS(win
) && (win
->quirks
& SWM_Q_TRANSSZ
)) {
4443 /* Adjust size on TRANSSZ quirk. */
4444 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
4445 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
4448 if (!(win
->quirks
& SWM_Q_ANYWHERE
)) {
4450 * Floaters and transients are auto-centred
4451 * unless manually moved, resized or ANYWHERE
4454 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 -
4456 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 -
4458 store_float_geom(win
);
4463 /* Ensure at least 1 pixel of the window is in the region. */
4464 region_containment(win
, r
, SWM_CW_ALLSIDES
);
4470 * Send keystrokes to terminal to decrease/increase the font size as the
4471 * window size changes.
4474 adjust_font(struct ws_win
*win
)
4476 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
4477 ABOVE(win
) || TRANS(win
))
4480 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
4481 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
4482 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
4483 win
->font_size_boundary
[win
->font_steps
] =
4484 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
4487 win
->last_inc
= win
->sh
.width_inc
;
4488 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
4489 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
4490 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
4493 win
->last_inc
= win
->sh
.width_inc
;
4494 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
4498 #define SWAPXY(g) do { \
4500 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4501 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4504 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
4506 struct swm_geometry win_g
, r_g
= *g
;
4508 int i
, j
, s
, stacks
;
4509 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
4510 int hrh
, extra
= 0, h_slice
, last_h
= 0;
4511 int split
, colno
, winno
, mwin
, msize
, mscale
;
4512 int remain
, missing
, v_slice
, reconfigure
= 0;
4515 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
4516 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
4518 /* Prepare tiling variables, if needed. */
4519 if ((winno
= count_win(ws
, 0)) > 0) {
4520 /* Find first tiled window. */
4521 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
4522 if (!FLOATING(win
) && !ICONIC(win
))
4525 /* Take into account size hints of first tiled window. */
4527 w_inc
= win
->sh
.width_inc
;
4528 w_base
= win
->sh
.base_width
;
4529 mwin
= ws
->l_state
.horizontal_mwin
;
4530 mscale
= ws
->l_state
.horizontal_msize
;
4531 stacks
= ws
->l_state
.horizontal_stacks
;
4534 w_inc
= win
->sh
.height_inc
;
4535 w_base
= win
->sh
.base_height
;
4536 mwin
= ws
->l_state
.vertical_mwin
;
4537 mscale
= ws
->l_state
.vertical_msize
;
4538 stacks
= ws
->l_state
.vertical_stacks
;
4542 if (stacks
> winno
- mwin
)
4543 stacks
= winno
- mwin
;
4547 h_slice
= r_g
.h
/ SWM_H_SLICE
;
4548 if (mwin
&& winno
> mwin
) {
4549 v_slice
= r_g
.w
/ SWM_V_SLICE
;
4553 win_g
.w
= v_slice
* mscale
;
4555 if (w_inc
> 1 && w_inc
< v_slice
) {
4556 /* Adjust for requested size increment. */
4557 remain
= (win_g
.w
- w_base
) % w_inc
;
4563 win_g
.x
+= r_g
.w
- msize
;
4566 colno
= split
= winno
/ stacks
;
4567 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
4568 2 * border_width
) / stacks
);
4570 hrh
= r_g
.h
/ colno
;
4571 extra
= r_g
.h
- (colno
* hrh
);
4572 win_g
.h
= hrh
- 2 * border_width
;
4573 i
= j
= 0, s
= stacks
;
4576 /* Update window geometry. */
4577 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
4581 if (FLOATING(win
)) {
4582 update_floater(win
);
4587 if (split
&& i
== split
) {
4588 colno
= (winno
- mwin
) / stacks
;
4589 if (s
<= (winno
- mwin
) % stacks
)
4592 hrh
= r_g
.h
/ colno
;
4593 extra
= r_g
.h
- (colno
* hrh
);
4596 win_g
.x
+= win_g
.w
+ 2 * border_width
+
4599 win_g
.w
= (r_g
.w
- msize
-
4600 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
4602 win_g
.w
+= (r_g
.w
- msize
-
4603 (stacks
* (2 * border_width
+ tile_gap
))) %
4607 win_g
.x
-= win_g
.w
+ 2 * border_width
+
4613 win_g
.h
= hrh
- 2 * border_width
- tile_gap
;
4616 h_inc
= win
->sh
.width_inc
;
4617 h_base
= win
->sh
.base_width
;
4619 h_inc
= win
->sh
.height_inc
;
4620 h_base
= win
->sh
.base_height
;
4623 if (j
== colno
- 1) {
4624 win_g
.h
= hrh
+ extra
;
4625 } else if (h_inc
> 1 && h_inc
< h_slice
) {
4626 /* adjust for window's requested size increment */
4627 remain
= (win_g
.h
- h_base
) % h_inc
;
4628 missing
= h_inc
- remain
;
4630 if (missing
<= extra
|| j
== 0) {
4642 win_g
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
4644 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
) &&
4647 win_g
.w
+= 2 * border_width
;
4648 win_g
.h
+= 2 * border_width
;
4654 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
4655 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
4659 WIDTH(win
) = win_g
.h
;
4660 HEIGHT(win
) = win_g
.w
;
4663 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
4664 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
4668 WIDTH(win
) = win_g
.w
;
4669 HEIGHT(win
) = win_g
.h
;
4673 if (bordered
!= win
->bordered
) {
4675 win
->bordered
= bordered
;
4688 /* Stack all windows from bottom up. */
4689 TAILQ_FOREACH_REVERSE(win
, &ws
->stack
, ws_win_stack
, stack_entry
)
4691 update_win_stacking(win
);
4693 /* Map all windows from top down. */
4694 TAILQ_FOREACH(win
, &ws
->stack
, stack_entry
)
4698 DNPRINTF(SWM_D_STACK
, "stack_master: done\n");
4702 vertical_config(struct workspace
*ws
, int id
)
4704 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
4708 case SWM_ARG_ID_STACKRESET
:
4709 case SWM_ARG_ID_STACKINIT
:
4710 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
4711 ws
->l_state
.vertical_mwin
= 1;
4712 ws
->l_state
.vertical_stacks
= 1;
4714 case SWM_ARG_ID_MASTERSHRINK
:
4715 if (ws
->l_state
.vertical_msize
> 1)
4716 ws
->l_state
.vertical_msize
--;
4718 case SWM_ARG_ID_MASTERGROW
:
4719 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
4720 ws
->l_state
.vertical_msize
++;
4722 case SWM_ARG_ID_MASTERADD
:
4723 ws
->l_state
.vertical_mwin
++;
4725 case SWM_ARG_ID_MASTERDEL
:
4726 if (ws
->l_state
.vertical_mwin
> 0)
4727 ws
->l_state
.vertical_mwin
--;
4729 case SWM_ARG_ID_STACKINC
:
4730 ws
->l_state
.vertical_stacks
++;
4732 case SWM_ARG_ID_STACKDEC
:
4733 if (ws
->l_state
.vertical_stacks
> 1)
4734 ws
->l_state
.vertical_stacks
--;
4736 case SWM_ARG_ID_FLIPLAYOUT
:
4737 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
4745 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4747 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
4749 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
4753 horizontal_config(struct workspace
*ws
, int id
)
4755 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
4758 case SWM_ARG_ID_STACKRESET
:
4759 case SWM_ARG_ID_STACKINIT
:
4760 ws
->l_state
.horizontal_mwin
= 1;
4761 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
4762 ws
->l_state
.horizontal_stacks
= 1;
4764 case SWM_ARG_ID_MASTERSHRINK
:
4765 if (ws
->l_state
.horizontal_msize
> 1)
4766 ws
->l_state
.horizontal_msize
--;
4768 case SWM_ARG_ID_MASTERGROW
:
4769 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
4770 ws
->l_state
.horizontal_msize
++;
4772 case SWM_ARG_ID_MASTERADD
:
4773 ws
->l_state
.horizontal_mwin
++;
4775 case SWM_ARG_ID_MASTERDEL
:
4776 if (ws
->l_state
.horizontal_mwin
> 0)
4777 ws
->l_state
.horizontal_mwin
--;
4779 case SWM_ARG_ID_STACKINC
:
4780 ws
->l_state
.horizontal_stacks
++;
4782 case SWM_ARG_ID_STACKDEC
:
4783 if (ws
->l_state
.horizontal_stacks
> 1)
4784 ws
->l_state
.horizontal_stacks
--;
4786 case SWM_ARG_ID_FLIPLAYOUT
:
4787 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
4795 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4797 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
4799 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
4802 /* fullscreen view */
4804 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4806 struct swm_geometry gg
= *g
;
4807 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
;
4810 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
4815 winno
= count_win(ws
, 0);
4816 if (winno
== 0 && count_win(ws
, 1) == 0)
4819 /* Figure out which top level window should be visible. */
4820 if (ws
->focus_pending
)
4821 win
= ws
->focus_pending
;
4824 else if (ws
->focus_prev
)
4825 win
= ws
->focus_prev
;
4827 win
= TAILQ_FIRST(&ws
->winlist
);
4829 DNPRINTF(SWM_D_STACK
, "max_stack: win: %#x\n", win
->id
);
4831 /* Update window geometry. */
4832 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
4841 /* Set maximized flag for all maxed windows. */
4842 if (!MAXIMIZED(w
)) {
4843 /* Preserve floating geometry. */
4845 store_float_geom(w
);
4847 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_MAXIMIZED
);
4848 ewmh_update_wm_state(w
);
4851 /* Only reconfigure if necessary. */
4852 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
4853 HEIGHT(w
) != gg
.h
) {
4855 if (bar_enabled
&& ws
->bar_enabled
){
4859 WIDTH(w
) += 2 * border_width
;
4860 HEIGHT(w
) += 2 * border_width
;
4868 parent
= find_window(win
->transient
);
4869 raise_window(parent
);
4871 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
4872 if (w
->transient
== parent
->id
)
4878 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
4879 if (w
->transient
== win
->id
)
4882 /* Map all windows. */
4883 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
4889 send_to_rg(struct swm_region
*r
, union arg
*args
)
4891 int ridx
= args
->id
, i
, num_screens
;
4892 struct swm_region
*rr
= NULL
;
4895 num_screens
= get_screen_count();
4896 /* do nothing if we don't have more than one screen */
4897 if (!(num_screens
> 1 || outputs
> 1))
4900 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
4902 rr
= TAILQ_FIRST(&r
->s
->rl
);
4903 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
4904 rr
= TAILQ_NEXT(rr
, entry
);
4915 region_under(struct swm_screen
*s
, int x
, int y
)
4917 struct swm_region
*r
= NULL
;
4919 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
4920 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
4921 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
4922 WIDTH(r
), HEIGHT(r
), x
, y
);
4923 if (X(r
) <= x
&& x
< MAX_X(r
))
4924 if (Y(r
) <= y
&& y
< MAX_Y(r
))
4932 send_to_ws(struct swm_region
*r
, union arg
*args
)
4934 int wsid
= args
->id
;
4935 struct ws_win
*win
= NULL
;
4937 if (r
&& r
->ws
&& r
->ws
->focus
)
4942 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win %#x, ws %d\n", win
->id
, wsid
);
4944 if (wsid
>= workspace_limit
)
4947 if (win
->ws
->idx
== wsid
)
4950 win_to_ws(win
, wsid
, 1);
4952 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
4953 ewmh_update_wm_state(win
);
4955 /* Restack and set new focus. */
4958 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
4959 if (r
->ws
->focus_pending
) {
4960 focus_win(r
->ws
->focus_pending
);
4961 r
->ws
->focus_pending
= NULL
;
4963 xcb_set_input_focus(conn
,
4964 XCB_INPUT_FOCUS_PARENT
, r
->id
,
4973 win_to_ws(struct ws_win
*win
, int wsid
, int unfocus
)
4975 struct ws_win
*parent
;
4976 struct workspace
*ws
, *nws
, *pws
;
4978 if (wsid
>= workspace_limit
)
4981 if (win
->ws
->idx
== wsid
)
4985 nws
= &win
->s
->ws
[wsid
];
4987 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win %#x, ws %d -> %d\n", win
->id
,
4990 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
4991 ws
->focus_pending
= get_focus_prev(win
);
4993 /* Move the parent if this is a transient window. */
4995 parent
= find_window(win
->transient
);
4998 /* Set new focus in parent's ws if needed. */
4999 if (pws
->focus
== parent
) {
5000 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5001 pws
->focus_pending
=
5002 get_focus_prev(parent
);
5004 unfocus_win(parent
);
5006 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5007 pws
->focus
= pws
->focus_pending
;
5008 pws
->focus_pending
= NULL
;
5012 /* Don't unmap parent if new ws is visible */
5014 unmap_window(parent
);
5017 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
5018 TAILQ_REMOVE(&ws
->stack
, parent
, stack_entry
);
5019 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
5020 TAILQ_INSERT_TAIL(&nws
->stack
, parent
, stack_entry
);
5023 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5024 "_NET_WM_DESKTOP: %d\n", wsid
);
5025 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5026 parent
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
5027 XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5034 /* Don't unmap if new ws is visible */
5039 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
5040 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
5041 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
5042 TAILQ_INSERT_TAIL(&nws
->stack
, win
, stack_entry
);
5045 /* Set focus on new ws. */
5046 unfocus_win(nws
->focus
);
5049 /* Update the window's workspace property: _NET_WM_DESKTOP */
5050 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5051 "_NET_WM_DESKTOP: %d\n", wsid
);
5052 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
5053 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5055 ewmh_update_client_list();
5057 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
5061 pressbutton(struct swm_region
*r
, union arg
*args
)
5063 /* suppress unused warning since var is needed */
5066 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
5067 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5068 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
5069 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5073 raise_toggle(struct swm_region
*r
, union arg
*args
)
5075 /* suppress unused warning since var is needed */
5078 if (r
== NULL
|| r
->ws
== NULL
)
5081 if (r
->ws
->focus
&& MAXIMIZED(r
->ws
->focus
))
5084 r
->ws
->always_raise
= !r
->ws
->always_raise
;
5086 /* bring floaters back to top */
5087 if (!r
->ws
->always_raise
)
5094 iconify(struct swm_region
*r
, union arg
*args
)
5097 /* suppress unused warning since var is needed */
5100 if ((w
= r
->ws
->focus
) == NULL
)
5103 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_HIDDEN
);
5104 ewmh_update_wm_state(w
);
5112 get_win_name(xcb_window_t win
)
5115 xcb_get_property_cookie_t c
;
5116 xcb_get_property_reply_t
*r
;
5118 /* First try _NET_WM_NAME for UTF-8. */
5119 c
= xcb_get_property(conn
, 0, win
, ewmh
[_NET_WM_NAME
].atom
,
5120 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5121 r
= xcb_get_property_reply(conn
, c
, NULL
);
5122 if (r
&& r
->type
== XCB_NONE
) {
5124 /* Use WM_NAME instead; no UTF-8. */
5125 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
5126 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5127 r
= xcb_get_property_reply(conn
, c
, NULL
);
5130 if (r
&& r
->type
!= XCB_NONE
&& r
->length
> 0)
5131 name
= strndup(xcb_get_property_value(r
),
5132 xcb_get_property_value_length(r
));
5137 err(1, "get_win_name: failed to allocate memory.");
5145 uniconify(struct swm_region
*r
, union arg
*args
)
5152 DNPRINTF(SWM_D_MISC
, "uniconify\n");
5154 if (r
== NULL
|| r
->ws
== NULL
)
5157 /* make sure we have anything to uniconify */
5158 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5159 if (win
->ws
== NULL
)
5160 continue; /* should never happen */
5166 DNPRINTF(SWM_D_MISC
, "uniconify: count: %d\n", count
);
5172 search_resp_action
= SWM_SEARCH_UNICONIFY
;
5174 spawn_select(r
, args
, "search", &searchpid
);
5176 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5179 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5180 if (win
->ws
== NULL
)
5181 continue; /* should never happen */
5185 name
= get_win_name(win
->id
);
5186 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
5194 name_workspace(struct swm_region
*r
, union arg
*args
)
5198 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
5204 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
5206 spawn_select(r
, args
, "name_workspace", &searchpid
);
5208 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5211 fprintf(lfile
, "%s", "");
5216 search_workspace(struct swm_region
*r
, union arg
*args
)
5219 struct workspace
*ws
;
5222 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
5228 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
5230 spawn_select(r
, args
, "search", &searchpid
);
5232 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5235 for (i
= 0; i
< workspace_limit
; i
++) {
5239 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
5240 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
5247 search_win_cleanup(void)
5249 struct search_window
*sw
= NULL
;
5251 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
5252 xcb_destroy_window(conn
, sw
->indicator
);
5253 TAILQ_REMOVE(&search_wl
, sw
, entry
);
5259 search_win(struct swm_region
*r
, union arg
*args
)
5261 struct ws_win
*win
= NULL
;
5262 struct search_window
*sw
= NULL
;
5265 int i
, width
, height
;
5273 XRectangle l_ibox
, l_lbox
;
5275 DNPRINTF(SWM_D_MISC
, "search_win\n");
5278 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
5280 spawn_select(r
, args
, "search", &searchpid
);
5282 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5285 TAILQ_INIT(&search_wl
);
5288 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5292 sw
= calloc(1, sizeof(struct search_window
));
5294 warn("search_win: calloc");
5296 search_win_cleanup();
5302 snprintf(s
, sizeof s
, "%d", i
);
5305 w
= xcb_generate_id(conn
);
5306 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
5307 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
5309 if (bar_font_legacy
) {
5310 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
5311 width
= l_lbox
.width
+ 4;
5312 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
5314 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
5316 width
= info
.width
+ 4;
5317 height
= bar_font
->height
+ 4;
5320 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, w
, win
->id
, 0, 0,
5321 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
5322 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
|
5323 XCB_CW_BORDER_PIXEL
, wa
);
5325 xcb_map_window(conn
, w
);
5328 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
5330 if (bar_font_legacy
) {
5331 l_gcv
.graphics_exposures
= 0;
5332 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
5334 XSetForeground(display
, l_draw
,
5335 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
5337 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
5338 (bar_fs_extents
->max_logical_extent
.height
-
5339 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
5341 XFreeGC(display
, l_draw
);
5344 draw
= XftDrawCreate(display
, w
,
5345 DefaultVisual(display
, r
->s
->idx
),
5346 DefaultColormap(display
, r
->s
->idx
));
5348 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
5349 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
5350 bar_font
->descent
, (FcChar8
*)s
, len
);
5352 XftDrawDestroy(draw
);
5355 DNPRINTF(SWM_D_MISC
, "search_win: mapped win %#x\n", w
);
5357 fprintf(lfile
, "%d\n", i
);
5367 search_resp_uniconify(const char *resp
, size_t len
)
5373 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
5375 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
5378 name
= get_win_name(win
->id
);
5379 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
5384 if (strncmp(s
, resp
, len
) == 0) {
5385 /* XXX this should be a callback to generalize */
5386 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_HIDDEN
);
5387 ewmh_update_wm_state(win
);
5397 search_resp_name_workspace(const char *resp
, size_t len
)
5399 struct workspace
*ws
;
5401 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
5403 if (search_r
->ws
== NULL
)
5408 free(search_r
->ws
->name
);
5409 search_r
->ws
->name
= NULL
;
5413 ws
->name
= strdup(resp
);
5414 if (ws
->name
== NULL
) {
5415 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
5416 "strdup: %s", strerror(errno
));
5420 ewmh_update_desktop_names();
5421 ewmh_get_desktop_names();
5426 ewmh_update_desktop_names(void)
5428 char *name_list
= NULL
, *p
;
5429 int num_screens
, i
, j
, len
= 0, tot
= 0;
5431 num_screens
= get_screen_count();
5432 for (i
= 0; i
< num_screens
; ++i
) {
5433 for (j
= 0; j
< workspace_limit
; ++j
) {
5434 if (screens
[i
].ws
[j
].name
!= NULL
)
5435 len
+= strlen(screens
[i
].ws
[j
].name
);
5439 if((name_list
= calloc(sizeof(char *), len
)) == NULL
)
5440 err(1, "update_desktop_names: calloc: failed to "
5441 "allocate memory.");
5444 for (j
= 0; j
< workspace_limit
; ++j
) {
5445 if (screens
[i
].ws
[j
].name
!= NULL
) {
5446 len
= strlen(screens
[i
].ws
[j
].name
);
5447 memcpy(p
, screens
[i
].ws
[j
].name
, len
);
5456 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5457 screens
[i
].root
, ewmh
[_NET_DESKTOP_NAMES
].atom
,
5458 a_utf8_string
, 8, tot
, name_list
);
5468 ewmh_get_desktop_names(void)
5471 xcb_get_property_cookie_t c
;
5472 xcb_get_property_reply_t
*r
;
5473 int num_screens
, i
, j
, n
, k
;
5475 num_screens
= get_screen_count();
5476 for (i
= 0; i
< num_screens
; ++i
) {
5477 for (j
= 0; j
< workspace_limit
; ++j
) {
5478 free(screens
[i
].ws
[j
].name
);
5479 screens
[i
].ws
[j
].name
= NULL
;
5482 c
= xcb_get_property(conn
, 0, screens
[i
].root
,
5483 ewmh
[_NET_DESKTOP_NAMES
].atom
,
5484 a_utf8_string
, 0, UINT32_MAX
);
5485 r
= xcb_get_property_reply(conn
, c
, NULL
);
5489 names
= xcb_get_property_value(r
);
5490 n
= xcb_get_property_value_length(r
);
5492 for (j
= 0, k
= 0; j
< n
; ++j
) {
5493 if (*(names
+ j
) != '\0') {
5494 screens
[i
].ws
[k
].name
= strdup(names
+ j
);
5495 j
+= strlen(names
+ j
);
5504 ewmh_update_client_list(void)
5507 int num_screens
, i
, j
, k
= 0, count
= 0;
5510 num_screens
= get_screen_count();
5511 for (i
= 0; i
< num_screens
; ++i
) {
5512 for (j
= 0; j
< workspace_limit
; ++j
)
5513 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
5516 DNPRINTF(SWM_D_PROP
, "ewmh_update_client_list: win count: %d\n",
5522 wins
= calloc(sizeof(xcb_window_t
), count
);
5524 err(1, "ewmh_update_client_list: calloc: failed to "
5525 "allocate memory.");
5527 for (j
= 0, k
= 0; j
< workspace_limit
; ++j
)
5528 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
5529 wins
[k
++] = win
->id
;
5531 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5532 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
5533 XCB_ATOM_WINDOW
, 32, count
, wins
);
5540 ewmh_update_current_desktop(void)
5544 num_screens
= get_screen_count();
5545 for (i
= 0; i
< num_screens
; ++i
)
5546 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5547 screens
[i
].root
, ewmh
[_NET_CURRENT_DESKTOP
].atom
,
5548 XCB_ATOM_CARDINAL
, 32, 1, &screens
[i
].r_focus
->ws
->idx
);
5552 search_resp_search_workspace(const char *resp
)
5559 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
5563 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
5570 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
5572 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
5579 switchws(search_r
, &a
);
5583 search_resp_search_window(const char *resp
)
5588 struct search_window
*sw
;
5590 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
5594 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
5599 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
5601 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
5608 TAILQ_FOREACH(sw
, &search_wl
, entry
)
5609 if (idx
== sw
->idx
) {
5615 #define MAX_RESP_LEN 1024
5618 search_do_resp(void)
5624 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
5629 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
5630 warn("search: calloc");
5634 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
5636 warn("search: read error");
5639 resp
[rbytes
] = '\0';
5642 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5643 * newline, so work around that by sanitizing the resp now.
5645 resp
[strcspn(resp
, "\n")] = '\0';
5648 switch (search_resp_action
) {
5649 case SWM_SEARCH_UNICONIFY
:
5650 search_resp_uniconify(resp
, len
);
5652 case SWM_SEARCH_NAME_WORKSPACE
:
5653 search_resp_name_workspace(resp
, len
);
5655 case SWM_SEARCH_SEARCH_WORKSPACE
:
5656 search_resp_search_workspace(resp
);
5658 case SWM_SEARCH_SEARCH_WINDOW
:
5659 search_resp_search_window(resp
);
5664 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
5665 search_win_cleanup();
5667 search_resp_action
= SWM_SEARCH_NONE
;
5668 close(select_resp_pipe
[0]);
5675 wkill(struct swm_region
*r
, union arg
*args
)
5677 DNPRINTF(SWM_D_MISC
, "wkill: win %#x, id: %d\n", WINID(r
->ws
->focus
),
5680 if (r
->ws
->focus
== NULL
)
5683 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
5684 xcb_kill_client(conn
, r
->ws
->focus
->id
);
5686 if (r
->ws
->focus
->can_delete
)
5687 client_msg(r
->ws
->focus
, a_delete
, 0);
5693 clear_maximized(struct workspace
*ws
)
5698 /* Clear any maximized win(s) on ws, from bottom up. */
5699 TAILQ_FOREACH_REVERSE(w
, &ws
->stack
, ws_win_stack
, stack_entry
)
5701 ewmh_apply_flags(w
, w
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5702 ewmh_update_wm_state(w
);
5710 maximize_toggle(struct swm_region
*r
, union arg
*args
)
5712 struct ws_win
*w
= r
->ws
->focus
;
5714 /* suppress unused warning since var is needed */
5720 DNPRINTF(SWM_D_MISC
, "maximize_toggle: win %#x\n", w
->id
);
5725 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
5728 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_MAXIMIZED
);
5729 ewmh_update_wm_state(w
);
5733 if (w
== w
->ws
->focus
)
5737 DNPRINTF(SWM_D_MISC
, "maximize_toggle: done\n");
5741 floating_toggle(struct swm_region
*r
, union arg
*args
)
5743 struct ws_win
*win
= r
->ws
->focus
;
5745 /* suppress unused warning since var is needed */
5751 if (FULLSCREEN(win
) || TRANS(win
))
5754 ewmh_apply_flags(win
, win
->ewmh_flags
^ EWMH_F_ABOVE
);
5755 ewmh_update_wm_state(win
);
5759 if (win
== win
->ws
->focus
)
5766 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
5768 struct swm_geometry g
= r
->g
;
5769 int rt
, lt
, tp
, bm
, bw
;
5771 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
5774 * Perpendicular distance of each side of the window to the respective
5775 * side of the region boundary. Positive values indicate the side of
5776 * the window has passed beyond the region boundary.
5778 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) + BORDER(win
) - MAX_X(r
) : bw
;
5779 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) + BORDER(win
) : bw
;
5780 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) + BORDER(win
) - MAX_Y(r
) : bw
;
5781 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) + BORDER(win
) : bw
;
5783 DNPRINTF(SWM_D_MISC
, "region_containment: win %#x, rt: %d, lt: %d, "
5784 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
5785 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
5786 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
5789 * Disable containment if any of the flagged sides went beyond the
5790 * containment boundary, or if containment is disabled.
5792 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
5793 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
5794 /* Make sure window has at least 1 pixel in the region */
5795 g
.x
+= 1 - WIDTH(win
);
5796 g
.y
+= 1 - HEIGHT(win
);
5797 g
.w
+= 2 * WIDTH(win
) - 2;
5798 g
.h
+= 2 * HEIGHT(win
) - 2;
5801 constrain_window(win
, &g
, &opts
);
5804 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
5806 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
5808 DNPRINTF(SWM_D_MISC
, "constrain_window: win %#x, (x,y) w x h: "
5809 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
5810 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
5811 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
5812 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
5813 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
5814 YESNO(*opts
& SWM_CW_RESIZABLE
));
5816 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) + BORDER(win
) > b
->x
+ b
->w
) {
5817 if (*opts
& SWM_CW_RESIZABLE
)
5818 WIDTH(win
) = b
->x
+ b
->w
- X(win
) - BORDER(win
);
5820 X(win
) = b
->x
+ b
->w
- WIDTH(win
) - BORDER(win
);
5823 if ((*opts
& SWM_CW_LEFT
) && X(win
) + BORDER(win
) < b
->x
) {
5824 if (*opts
& SWM_CW_RESIZABLE
)
5825 WIDTH(win
) -= b
->x
- X(win
) - BORDER(win
);
5827 X(win
) = b
->x
- BORDER(win
);
5830 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) + BORDER(win
) > b
->y
+ b
->h
) {
5831 if (*opts
& SWM_CW_RESIZABLE
)
5832 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
) - BORDER(win
);
5834 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
) - BORDER(win
);
5837 if ((*opts
& SWM_CW_TOP
) && Y(win
) + BORDER(win
) < b
->y
) {
5838 if (*opts
& SWM_CW_RESIZABLE
)
5839 HEIGHT(win
) -= b
->y
- Y(win
) - BORDER(win
);
5841 Y(win
) = b
->y
- BORDER(win
);
5844 if (*opts
& SWM_CW_RESIZABLE
) {
5847 if (HEIGHT(win
) < 1)
5853 update_window(struct ws_win
*win
)
5858 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
5859 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
5860 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
5864 wc
[3] = HEIGHT(win
);
5865 wc
[4] = BORDER(win
);
5867 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x, (x,y) w x h: "
5868 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
5869 wc
[3], YESNO(win
->bordered
));
5871 xcb_configure_window(conn
, win
->id
, mask
, wc
);
5874 #define SWM_RESIZE_STEPS (50)
5877 resize(struct ws_win
*win
, union arg
*args
)
5879 xcb_timestamp_t timestamp
= 0;
5880 struct swm_region
*r
= NULL
;
5881 struct swm_geometry g
;
5883 int top
= 0, left
= 0, resizing
;
5885 xcb_cursor_t cursor
;
5886 xcb_query_pointer_reply_t
*xpr
= NULL
;
5887 xcb_generic_event_t
*evt
;
5888 xcb_motion_notify_event_t
*mne
;
5894 if (FULLSCREEN(win
))
5897 /* In max_stack mode, should only resize transients. */
5898 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
5901 DNPRINTF(SWM_D_EVENT
, "resize: win %#x, floating: %s, "
5902 "transient: %#x\n", win
->id
, YESNO(ABOVE(win
)),
5906 store_float_geom(win
);
5907 else if (!(TRANS(win
) || ABOVE(win
)))
5910 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
5912 ewmh_update_wm_state(win
);
5918 /* It's possible for win to have been freed during focus_flush(). */
5919 if (validate_win(win
)) {
5920 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
5925 case SWM_ARG_ID_WIDTHSHRINK
:
5926 WIDTH(win
) -= SWM_RESIZE_STEPS
;
5929 case SWM_ARG_ID_WIDTHGROW
:
5930 WIDTH(win
) += SWM_RESIZE_STEPS
;
5933 case SWM_ARG_ID_HEIGHTSHRINK
:
5934 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
5937 case SWM_ARG_ID_HEIGHTGROW
:
5938 HEIGHT(win
) += SWM_RESIZE_STEPS
;
5945 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
5946 SWM_CW_HARDBOUNDARY
);
5948 store_float_geom(win
);
5952 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
5953 SWM_CW_SOFTBOUNDARY
);
5956 /* get cursor offset from window root */
5957 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
5964 if (xpr
->win_x
< WIDTH(win
) / 2)
5967 if (xpr
->win_y
< HEIGHT(win
) / 2)
5970 if (args
->id
== SWM_ARG_ID_CENTER
)
5971 cursor
= cursors
[XC_SIZING
].cid
;
5973 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
5974 XC_TOP_RIGHT_CORNER
].cid
;
5976 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
5977 XC_BOTTOM_RIGHT_CORNER
].cid
;
5979 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
5980 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
5985 while (resizing
&& (evt
= xcb_wait_for_event(conn
))) {
5986 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
5987 case XCB_BUTTON_RELEASE
:
5988 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
5991 case XCB_MOTION_NOTIFY
:
5992 mne
= (xcb_motion_notify_event_t
*)evt
;
5993 /* cursor offset/delta from start of the operation */
5994 dx
= mne
->root_x
- xpr
->root_x
;
5995 dy
= mne
->root_y
- xpr
->root_y
;
6001 if (args
->id
== SWM_ARG_ID_CENTER
) {
6002 if (g
.h
/ 2 + dy
< 1)
6006 HEIGHT(win
) = g
.h
+ 2 * dy
;
6014 HEIGHT(win
) = g
.h
+ dy
;
6021 if (args
->id
== SWM_ARG_ID_CENTER
) {
6022 if (g
.w
/ 2 + dx
< 1)
6026 WIDTH(win
) = g
.w
+ 2 * dx
;
6034 WIDTH(win
) = g
.w
+ dx
;
6037 /* not free, don't sync more than 120 times / second */
6038 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6039 timestamp
= mne
->time
;
6040 regionize(win
, mne
->root_x
, mne
->root_y
);
6041 region_containment(win
, r
, SWM_CW_ALLSIDES
|
6042 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
6043 SWM_CW_SOFTBOUNDARY
);
6051 /* It's possible for win to have been freed above. */
6052 if (validate_win(win
)) {
6053 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6061 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6062 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
6066 store_float_geom(win
);
6068 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6070 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
6074 resize_step(struct swm_region
*r
, union arg
*args
)
6076 struct ws_win
*win
= NULL
;
6078 if (r
&& r
->ws
&& r
->ws
->focus
)
6087 /* Try to set window region based on supplied coordinates or window center. */
6089 regionize(struct ws_win
*win
, int x
, int y
)
6091 struct swm_region
*r
= NULL
;
6093 r
= region_under(win
->s
, x
, y
);
6095 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
6096 Y(win
) + HEIGHT(win
) / 2);
6098 if (r
&& r
!= win
->ws
->r
) {
6099 win_to_ws(win
, r
->ws
->idx
, 0);
6104 #define SWM_MOVE_STEPS (50)
6107 move(struct ws_win
*win
, union arg
*args
)
6109 struct swm_region
*r
;
6110 xcb_timestamp_t timestamp
= 0;
6111 int move_stp
= 0, moving
, restack
= 0;
6112 xcb_query_pointer_reply_t
*qpr
= NULL
;
6113 xcb_generic_event_t
*evt
;
6114 xcb_motion_notify_event_t
*mne
;
6119 if ((r
= win
->ws
->r
) == NULL
)
6122 if (FULLSCREEN(win
))
6125 DNPRINTF(SWM_D_EVENT
, "move: win %#x, floating: %s, transient: "
6126 "%#x\n", win
->id
, YESNO(ABOVE(win
)), win
->transient
);
6128 /* in max_stack mode should only move transients */
6129 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6132 if (!(ABOVE(win
) || TRANS(win
)) || MAXIMIZED(win
)) {
6133 store_float_geom(win
);
6137 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6139 ewmh_update_wm_state(win
);
6146 /* It's possible for win to have been freed during focus_flush(). */
6147 if (validate_win(win
)) {
6148 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
6154 case SWM_ARG_ID_MOVELEFT
:
6155 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
6158 case SWM_ARG_ID_MOVERIGHT
:
6159 X(win
) += (SWM_MOVE_STEPS
- border_width
);
6162 case SWM_ARG_ID_MOVEUP
:
6163 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
6166 case SWM_ARG_ID_MOVEDOWN
:
6167 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
6174 regionize(win
, -1, -1);
6175 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
6177 store_float_geom(win
);
6181 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6182 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
6183 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
6185 /* get cursor offset from window root */
6186 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6189 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6193 regionize(win
, qpr
->root_x
, qpr
->root_y
);
6194 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
6195 SWM_CW_SOFTBOUNDARY
);
6199 while (moving
&& (evt
= xcb_wait_for_event(conn
))) {
6200 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6201 case XCB_BUTTON_RELEASE
:
6202 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
6205 case XCB_MOTION_NOTIFY
:
6206 mne
= (xcb_motion_notify_event_t
*)evt
;
6207 DNPRINTF(SWM_D_EVENT
, "motion: root: %#x\n", mne
->root
);
6208 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
6209 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
6211 /* not free, don't sync more than 120 times / second */
6212 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6213 timestamp
= mne
->time
;
6214 regionize(win
, mne
->root_x
, mne
->root_y
);
6215 region_containment(win
, win
->ws
->r
,
6216 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
6224 /* It's possible for win to have been freed above. */
6225 if (validate_win(win
)) {
6226 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
6234 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
6235 SWM_CW_SOFTBOUNDARY
);
6239 store_float_geom(win
);
6242 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6243 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
6247 move_step(struct swm_region
*r
, union arg
*args
)
6249 struct ws_win
*win
= NULL
;
6251 if (r
&& r
->ws
&& r
->ws
->focus
)
6256 if (!TRANS(win
) && !ABOVE(win
))
6263 /* key definitions */
6265 char name
[SWM_FUNCNAME_LEN
];
6266 void (*func
)(struct swm_region
*r
, union arg
*);
6268 } keyfuncs
[KF_INVALID
+ 1] = {
6269 /* name function argument */
6270 { "bar_toggle", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
6271 { "bar_toggle_ws", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
6272 { "button2", pressbutton
, {2} },
6273 { "cycle_layout", cycle_layout
, {0} },
6274 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
6275 { "float_toggle", floating_toggle
,{0} },
6276 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
6277 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
6278 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
6279 { "focus_urgent", focus
, {.id
= SWM_ARG_ID_FOCUSURGENT
} },
6280 { "maximize_toggle", maximize_toggle
,{0} },
6281 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
6282 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
6283 { "iconify", iconify
, {0} },
6284 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
6285 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
6286 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
6287 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
6288 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
6289 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
6290 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
6291 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
6292 { "mvrg_1", send_to_rg
, {.id
= 0} },
6293 { "mvrg_2", send_to_rg
, {.id
= 1} },
6294 { "mvrg_3", send_to_rg
, {.id
= 2} },
6295 { "mvrg_4", send_to_rg
, {.id
= 3} },
6296 { "mvrg_5", send_to_rg
, {.id
= 4} },
6297 { "mvrg_6", send_to_rg
, {.id
= 5} },
6298 { "mvrg_7", send_to_rg
, {.id
= 6} },
6299 { "mvrg_8", send_to_rg
, {.id
= 7} },
6300 { "mvrg_9", send_to_rg
, {.id
= 8} },
6301 { "mvws_1", send_to_ws
, {.id
= 0} },
6302 { "mvws_2", send_to_ws
, {.id
= 1} },
6303 { "mvws_3", send_to_ws
, {.id
= 2} },
6304 { "mvws_4", send_to_ws
, {.id
= 3} },
6305 { "mvws_5", send_to_ws
, {.id
= 4} },
6306 { "mvws_6", send_to_ws
, {.id
= 5} },
6307 { "mvws_7", send_to_ws
, {.id
= 6} },
6308 { "mvws_8", send_to_ws
, {.id
= 7} },
6309 { "mvws_9", send_to_ws
, {.id
= 8} },
6310 { "mvws_10", send_to_ws
, {.id
= 9} },
6311 { "mvws_11", send_to_ws
, {.id
= 10} },
6312 { "mvws_12", send_to_ws
, {.id
= 11} },
6313 { "mvws_13", send_to_ws
, {.id
= 12} },
6314 { "mvws_14", send_to_ws
, {.id
= 13} },
6315 { "mvws_15", send_to_ws
, {.id
= 14} },
6316 { "mvws_16", send_to_ws
, {.id
= 15} },
6317 { "mvws_17", send_to_ws
, {.id
= 16} },
6318 { "mvws_18", send_to_ws
, {.id
= 17} },
6319 { "mvws_19", send_to_ws
, {.id
= 18} },
6320 { "mvws_20", send_to_ws
, {.id
= 19} },
6321 { "mvws_21", send_to_ws
, {.id
= 20} },
6322 { "mvws_22", send_to_ws
, {.id
= 21} },
6323 { "name_workspace", name_workspace
, {0} },
6324 { "quit", quit
, {0} },
6325 { "raise_toggle", raise_toggle
, {0} },
6326 { "restart", restart
, {0} },
6327 { "rg_1", focusrg
, {.id
= 0} },
6328 { "rg_2", focusrg
, {.id
= 1} },
6329 { "rg_3", focusrg
, {.id
= 2} },
6330 { "rg_4", focusrg
, {.id
= 3} },
6331 { "rg_5", focusrg
, {.id
= 4} },
6332 { "rg_6", focusrg
, {.id
= 5} },
6333 { "rg_7", focusrg
, {.id
= 6} },
6334 { "rg_8", focusrg
, {.id
= 7} },
6335 { "rg_9", focusrg
, {.id
= 8} },
6336 { "rg_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
6337 { "rg_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
6338 { "screen_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
6339 { "screen_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
6340 { "search_win", search_win
, {0} },
6341 { "search_workspace", search_workspace
, {0} },
6342 { "spawn_custom", NULL
, {0} },
6343 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
6344 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
6345 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
6346 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
6347 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
6348 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
6349 { "uniconify", uniconify
, {0} },
6350 { "version", version
, {0} },
6351 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
6352 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
6353 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
6354 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
6355 { "ws_1", switchws
, {.id
= 0} },
6356 { "ws_2", switchws
, {.id
= 1} },
6357 { "ws_3", switchws
, {.id
= 2} },
6358 { "ws_4", switchws
, {.id
= 3} },
6359 { "ws_5", switchws
, {.id
= 4} },
6360 { "ws_6", switchws
, {.id
= 5} },
6361 { "ws_7", switchws
, {.id
= 6} },
6362 { "ws_8", switchws
, {.id
= 7} },
6363 { "ws_9", switchws
, {.id
= 8} },
6364 { "ws_10", switchws
, {.id
= 9} },
6365 { "ws_11", switchws
, {.id
= 10} },
6366 { "ws_12", switchws
, {.id
= 11} },
6367 { "ws_13", switchws
, {.id
= 12} },
6368 { "ws_14", switchws
, {.id
= 13} },
6369 { "ws_15", switchws
, {.id
= 14} },
6370 { "ws_16", switchws
, {.id
= 15} },
6371 { "ws_17", switchws
, {.id
= 16} },
6372 { "ws_18", switchws
, {.id
= 17} },
6373 { "ws_19", switchws
, {.id
= 18} },
6374 { "ws_20", switchws
, {.id
= 19} },
6375 { "ws_21", switchws
, {.id
= 20} },
6376 { "ws_22", switchws
, {.id
= 21} },
6377 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
6378 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
6379 { "ws_next_move", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
6380 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
6381 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
6382 { "ws_prev_move", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
6383 { "ws_prior", priorws
, {0} },
6384 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
6385 { "invalid key func", NULL
, {0} },
6389 key_cmp(struct key
*kp1
, struct key
*kp2
)
6391 if (kp1
->keysym
< kp2
->keysym
)
6393 if (kp1
->keysym
> kp2
->keysym
)
6396 if (kp1
->mod
< kp2
->mod
)
6398 if (kp1
->mod
> kp2
->mod
)
6405 enum { client_click
, root_click
};
6407 unsigned int action
;
6409 unsigned int button
;
6410 void (*func
)(struct ws_win
*, union arg
*);
6413 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6414 /* action key mouse button func args */
6415 { client_click
, MODKEY
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
6416 { client_click
, MODKEY_SHIFT
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
6417 { client_click
, MODKEY
, XCB_BUTTON_INDEX_1
, move
, {0} },
6422 update_modkey(unsigned int mod
)
6428 RB_FOREACH(kp
, key_tree
, &keys
)
6429 if (kp
->mod
& XCB_MOD_MASK_SHIFT
)
6430 kp
->mod
= mod
| XCB_MOD_MASK_SHIFT
;
6434 for (i
= 0; i
< LENGTH(buttons
); i
++)
6435 if (buttons
[i
].mask
& XCB_MOD_MASK_SHIFT
)
6436 buttons
[i
].mask
= mod
| XCB_MOD_MASK_SHIFT
;
6438 buttons
[i
].mask
= mod
;
6442 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
6445 struct spawn_prog
*prog
= NULL
;
6447 char *ap
, **real_args
;
6449 /* suppress unused warning since var is needed */
6452 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
6455 TAILQ_FOREACH(prog
, &spawns
, entry
) {
6456 if (strcasecmp(spawn_name
, prog
->name
) == 0)
6460 warnx("spawn_custom: program %s not found", spawn_name
);
6464 /* make room for expanded args */
6465 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
6466 err(1, "spawn_custom: calloc real_args");
6468 /* expand spawn_args into real_args */
6469 for (i
= c
= 0; i
< prog
->argc
; i
++) {
6471 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
6472 if (strcasecmp(ap
, "$bar_border") == 0) {
6474 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
6476 err(1, "spawn_custom border color");
6477 } else if (strcasecmp(ap
, "$bar_color") == 0) {
6479 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
6481 err(1, "spawn_custom bar color");
6482 } else if (strcasecmp(ap
, "$bar_font") == 0) {
6483 if ((real_args
[c
] = strdup(bar_fonts
))
6485 err(1, "spawn_custom bar fonts");
6486 } else if (strcasecmp(ap
, "$bar_font_color") == 0) {
6488 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
6490 err(1, "spawn_custom color font");
6491 } else if (strcasecmp(ap
, "$color_focus") == 0) {
6493 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
6495 err(1, "spawn_custom color focus");
6496 } else if (strcasecmp(ap
, "$color_unfocus") == 0) {
6498 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
6500 err(1, "spawn_custom color unfocus");
6501 } else if (strcasecmp(ap
, "$region_index") == 0) {
6502 if (asprintf(&real_args
[c
], "%d",
6503 get_region_index(r
) + 1) < 1)
6504 err(1, "spawn_custom region index");
6505 } else if (strcasecmp(ap
, "$workspace_index") == 0) {
6506 if (asprintf(&real_args
[c
], "%d", r
->ws
->idx
+ 1) < 1)
6507 err(1, "spawn_custom workspace index");
6508 } else if (strcasecmp(ap
, "$dmenu_bottom") == 0) {
6511 if ((real_args
[c
] = strdup("-b")) == NULL
)
6512 err(1, "spawn_custom workspace index");
6514 /* no match --> copy as is */
6515 if ((real_args
[c
] = strdup(ap
)) == NULL
)
6516 err(1, "spawn_custom strdup(ap)");
6518 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
6524 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
6525 for (i
= 0; i
< c
; ++i
)
6526 DPRINTF("\"%s\" ", real_args
[i
]);
6529 *ret_args
= real_args
;
6534 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
6540 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
6544 spawn(r
->ws
->idx
, &a
, 1);
6546 for (i
= 0; i
< spawn_argc
; i
++)
6552 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
6559 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
6563 if (pipe(select_list_pipe
) == -1)
6564 err(1, "pipe error");
6565 if (pipe(select_resp_pipe
) == -1)
6566 err(1, "pipe error");
6568 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
6569 err(1, "could not disable SIGPIPE");
6570 switch (*pid
= fork()) {
6572 err(1, "cannot fork");
6575 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
6577 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
6579 close(select_list_pipe
[1]);
6580 close(select_resp_pipe
[0]);
6581 spawn(r
->ws
->idx
, &a
, 0);
6583 default: /* parent */
6584 close(select_list_pipe
[0]);
6585 close(select_resp_pipe
[1]);
6589 for (i
= 0; i
< spawn_argc
; i
++)
6594 /* Argument tokenizer. */
6597 int single_quoted
= 0, double_quoted
= 0;
6598 char *arg
, *cp
, *next
;
6603 /* Eat and move characters until end of argument is found. */
6604 for (arg
= next
= cp
= *sp
; *cp
!= '\0'; ++cp
) {
6605 if (!double_quoted
&& *cp
== '\'') {
6606 /* Eat single-quote. */
6607 single_quoted
= !single_quoted
;
6608 } else if (!single_quoted
&& *cp
== '"') {
6609 /* Eat double-quote. */
6610 double_quoted
= !double_quoted
;
6611 } else if (!single_quoted
&& *cp
== '\\' && *(cp
+ 1) == '"') {
6612 /* Eat backslash; copy escaped character to arg. */
6614 } else if (!single_quoted
&& !double_quoted
&& *cp
== '\\' &&
6615 (*(cp
+ 1) == '\'' || *(cp
+ 1) == ' ')) {
6616 /* Eat backslash; move escaped character. */
6618 } else if (!single_quoted
&& !double_quoted
&&
6619 (*cp
== ' ' || *cp
== '\t')) {
6620 /* Terminate argument. */
6622 /* Point sp to beginning of next argument. */
6626 /* Move regular character. */
6631 /* Terminate argument if end of string. */
6641 spawn_insert(const char *name
, const char *args
, int flags
)
6643 struct spawn_prog
*sp
;
6644 char *arg
, *dup
, *ptr
;
6646 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s[%s]\n", name
, args
);
6648 if (args
== NULL
|| *args
== '\0')
6651 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
6652 err(1, "spawn_insert: calloc");
6653 if ((sp
->name
= strdup(name
)) == NULL
)
6654 err(1, "spawn_insert: strdup");
6656 /* Convert the arguments to an argument list. */
6657 if ((ptr
= dup
= strdup(args
)) == NULL
)
6658 err(1, "spawn_insert: strdup");
6659 while ((arg
= argsep(&ptr
)) != NULL
) {
6660 /* Null argument; skip it. */
6665 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
6666 sizeof *sp
->argv
)) == NULL
)
6667 err(1, "spawn_insert: realloc");
6668 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
6669 err(1, "spawn_insert: strdup");
6675 DNPRINTF(SWM_D_SPAWN
, "arg %d: [%s]\n", sp
->argc
, sp
->argv
[sp
->argc
-1]);
6676 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
6677 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
6681 spawn_remove(struct spawn_prog
*sp
)
6685 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
6687 TAILQ_REMOVE(&spawns
, sp
, entry
);
6688 for (i
= 0; i
< sp
->argc
; i
++)
6694 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
6698 spawn_find(const char *name
)
6700 struct spawn_prog
*sp
;
6702 TAILQ_FOREACH(sp
, &spawns
, entry
)
6703 if (strcasecmp(sp
->name
, name
) == 0)
6710 setspawn(const char *name
, const char *args
, int flags
)
6712 struct spawn_prog
*sp
;
6714 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
6719 /* Remove any old spawn under the same name. */
6720 if ((sp
= spawn_find(name
)) != NULL
)
6724 spawn_insert(name
, args
, flags
);
6726 warnx("error: setspawn: cannot find program: %s", name
);
6728 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
6732 setconfspawn(const char *selector
, const char *value
, int flags
)
6736 if (selector
== NULL
|| strlen(selector
) == 0)
6739 args
= expand_tilde(value
);
6741 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
6743 setspawn(selector
, args
, flags
);
6746 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
6751 validate_spawns(void)
6753 struct spawn_prog
*sp
;
6754 char which
[PATH_MAX
];
6759 RB_FOREACH(kp
, key_tree
, &keys
) {
6760 if (kp
->funcid
!= KF_SPAWN_CUSTOM
)
6764 sp
= spawn_find(kp
->spawn_name
);
6765 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
6768 /* verify we have the goods */
6769 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
6770 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
6772 for (i
= strlen("which "); i
< strlen(which
); i
++)
6773 if (which
[i
] == ' ') {
6777 if (system(which
) != 0)
6778 add_startup_exception("could not find %s",
6779 &which
[strlen("which ")]);
6786 setconfspawn("lock", "xlock", 0);
6788 setconfspawn("term", "xterm", 0);
6789 setconfspawn("spawn_term", "xterm", 0);
6791 setconfspawn("menu", "dmenu_run"
6795 " -nf $bar_font_color"
6797 " -sf $bar_color", 0);
6799 setconfspawn("search", "dmenu"
6804 " -nf $bar_font_color"
6806 " -sf $bar_color", 0);
6808 setconfspawn("name_workspace", "dmenu"
6813 " -nf $bar_font_color"
6815 " -sf $bar_color", 0);
6817 /* These are not verified for existence, even with a binding set. */
6818 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
6819 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
6820 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
6824 #define SWM_MODNAME_SIZE 32
6825 #define SWM_KEY_WS "\n+ \t"
6827 parsekeys(const char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
6829 char *str
, *cp
, *name
;
6832 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
6833 if (mod
== NULL
|| ks
== NULL
) {
6834 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
6837 if (keystr
== NULL
|| strlen(keystr
) == 0) {
6838 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
6842 if ((cp
= str
= strdup(keystr
)) == NULL
)
6843 err(1, "parsekeys: strdup");
6845 *ks
= XCB_NO_SYMBOL
;
6847 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
6848 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
6850 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
6851 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
6853 else if (strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
) == 0)
6854 *mod
|= XCB_MOD_MASK_1
;
6855 else if (strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
) == 0)
6856 *mod
+= XCB_MOD_MASK_2
;
6857 else if (strncmp(name
, "Mod3", SWM_MODNAME_SIZE
) == 0)
6858 *mod
|= XCB_MOD_MASK_3
;
6859 else if (strncmp(name
, "Mod4", SWM_MODNAME_SIZE
) == 0)
6860 *mod
|= XCB_MOD_MASK_4
;
6861 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
6862 *mod
|= XCB_MOD_MASK_SHIFT
;
6863 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
6864 *mod
|= XCB_MOD_MASK_CONTROL
;
6866 *ks
= XStringToKeysym(name
);
6867 XConvertCase(*ks
, ks
, &uks
);
6868 if (ks
== XCB_NO_SYMBOL
) {
6870 "parsekeys: invalid key %s\n",
6879 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
6884 strdupsafe(const char *str
)
6889 return (strdup(str
));
6893 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
6894 const char *spawn_name
)
6898 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
6899 keyfuncs
[kfid
].name
, spawn_name
);
6901 if ((kp
= malloc(sizeof *kp
)) == NULL
)
6902 err(1, "key_insert: malloc");
6907 kp
->spawn_name
= strdupsafe(spawn_name
);
6908 RB_INSERT(key_tree
, &keys
, kp
);
6910 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
6914 key_lookup(unsigned int mod
, KeySym ks
)
6921 return (RB_FIND(key_tree
, &keys
, &kp
));
6925 key_remove(struct key
*kp
)
6927 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
6929 RB_REMOVE(key_tree
, &keys
, kp
);
6930 free(kp
->spawn_name
);
6933 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
6937 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
6938 const char *spawn_name
)
6940 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
6944 key_insert(mod
, ks
, kfid
, spawn_name
);
6946 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
6950 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
6951 const char *spawn_name
)
6955 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
6956 keyfuncs
[kfid
].name
, spawn_name
);
6958 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
6959 if (kfid
== KF_INVALID
)
6962 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
6963 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
6966 if (kfid
== KF_INVALID
) {
6967 warnx("bind: Key combination already unbound.");
6968 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
6972 key_insert(mod
, ks
, kfid
, spawn_name
);
6973 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
6977 setconfbinding(const char *selector
, const char *value
, int flags
)
6979 enum keyfuncid kfid
;
6982 struct spawn_prog
*sp
;
6984 /* suppress unused warning since var is needed */
6987 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter selector: [%s], "
6988 "value: [%s]\n", selector
, value
);
6989 if (selector
== NULL
|| strlen(selector
) == 0) {
6990 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
6991 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
6993 setkeybinding(mod
, ks
, kfid
, NULL
);
6998 /* search by key function name */
6999 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
7000 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
7001 SWM_FUNCNAME_LEN
) == 0) {
7002 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
7003 "keyfunc\n", selector
);
7004 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7005 setkeybinding(mod
, ks
, kfid
, NULL
);
7011 /* search by custom spawn name */
7012 if ((sp
= spawn_find(selector
)) != NULL
) {
7013 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
7014 "spawn\n", selector
);
7015 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7016 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
7022 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
7029 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
7030 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
7031 setkeybinding(MODKEY_SHIFT
, XK_b
, KF_BAR_TOGGLE_WS
,NULL
);
7032 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
7033 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
7034 setkeybinding(MODKEY_SHIFT
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
7035 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
7036 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
7037 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
7038 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
7039 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
7040 setkeybinding(MODKEY_SHIFT
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
7041 setkeybinding(MODKEY
, XK_u
, KF_FOCUS_URGENT
,NULL
);
7042 setkeybinding(MODKEY
, XK_e
, KF_MAXIMIZE_TOGGLE
,NULL
);
7043 setkeybinding(MODKEY_SHIFT
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
7044 setkeybinding(MODKEY_SHIFT
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
7045 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
7046 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
7047 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
7048 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
7049 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
7050 setkeybinding(MODKEY_SHIFT
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
7051 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
7052 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
7053 setkeybinding(MODKEY_SHIFT
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
7054 setkeybinding(MODKEY_SHIFT
, XK_KP_End
, KF_MVRG_1
, NULL
);
7055 setkeybinding(MODKEY_SHIFT
, XK_KP_Down
, KF_MVRG_2
, NULL
);
7056 setkeybinding(MODKEY_SHIFT
, XK_KP_Next
, KF_MVRG_3
, NULL
);
7057 setkeybinding(MODKEY_SHIFT
, XK_KP_Left
, KF_MVRG_4
, NULL
);
7058 setkeybinding(MODKEY_SHIFT
, XK_KP_Begin
, KF_MVRG_5
, NULL
);
7059 setkeybinding(MODKEY_SHIFT
, XK_KP_Right
, KF_MVRG_6
, NULL
);
7060 setkeybinding(MODKEY_SHIFT
, XK_KP_Home
, KF_MVRG_7
, NULL
);
7061 setkeybinding(MODKEY_SHIFT
, XK_KP_Up
, KF_MVRG_8
, NULL
);
7062 setkeybinding(MODKEY_SHIFT
, XK_KP_Prior
, KF_MVRG_9
, NULL
);
7063 setkeybinding(MODKEY_SHIFT
, XK_1
, KF_MVWS_1
, NULL
);
7064 setkeybinding(MODKEY_SHIFT
, XK_2
, KF_MVWS_2
, NULL
);
7065 setkeybinding(MODKEY_SHIFT
, XK_3
, KF_MVWS_3
, NULL
);
7066 setkeybinding(MODKEY_SHIFT
, XK_4
, KF_MVWS_4
, NULL
);
7067 setkeybinding(MODKEY_SHIFT
, XK_5
, KF_MVWS_5
, NULL
);
7068 setkeybinding(MODKEY_SHIFT
, XK_6
, KF_MVWS_6
, NULL
);
7069 setkeybinding(MODKEY_SHIFT
, XK_7
, KF_MVWS_7
, NULL
);
7070 setkeybinding(MODKEY_SHIFT
, XK_8
, KF_MVWS_8
, NULL
);
7071 setkeybinding(MODKEY_SHIFT
, XK_9
, KF_MVWS_9
, NULL
);
7072 setkeybinding(MODKEY_SHIFT
, XK_0
, KF_MVWS_10
, NULL
);
7073 setkeybinding(MODKEY_SHIFT
, XK_F1
, KF_MVWS_11
, NULL
);
7074 setkeybinding(MODKEY_SHIFT
, XK_F2
, KF_MVWS_12
, NULL
);
7075 setkeybinding(MODKEY_SHIFT
, XK_F3
, KF_MVWS_13
, NULL
);
7076 setkeybinding(MODKEY_SHIFT
, XK_F4
, KF_MVWS_14
, NULL
);
7077 setkeybinding(MODKEY_SHIFT
, XK_F5
, KF_MVWS_15
, NULL
);
7078 setkeybinding(MODKEY_SHIFT
, XK_F6
, KF_MVWS_16
, NULL
);
7079 setkeybinding(MODKEY_SHIFT
, XK_F7
, KF_MVWS_17
, NULL
);
7080 setkeybinding(MODKEY_SHIFT
, XK_F8
, KF_MVWS_18
, NULL
);
7081 setkeybinding(MODKEY_SHIFT
, XK_F9
, KF_MVWS_19
, NULL
);
7082 setkeybinding(MODKEY_SHIFT
, XK_F10
, KF_MVWS_20
, NULL
);
7083 setkeybinding(MODKEY_SHIFT
, XK_F11
, KF_MVWS_21
, NULL
);
7084 setkeybinding(MODKEY_SHIFT
, XK_F12
, KF_MVWS_22
, NULL
);
7085 setkeybinding(MODKEY_SHIFT
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
7086 setkeybinding(MODKEY_SHIFT
, XK_q
, KF_QUIT
, NULL
);
7087 setkeybinding(MODKEY_SHIFT
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
7088 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
7089 setkeybinding(MODKEY
, XK_KP_End
, KF_RG_1
, NULL
);
7090 setkeybinding(MODKEY
, XK_KP_Down
, KF_RG_2
, NULL
);
7091 setkeybinding(MODKEY
, XK_KP_Next
, KF_RG_3
, NULL
);
7092 setkeybinding(MODKEY
, XK_KP_Left
, KF_RG_4
, NULL
);
7093 setkeybinding(MODKEY
, XK_KP_Begin
, KF_RG_5
, NULL
);
7094 setkeybinding(MODKEY
, XK_KP_Right
, KF_RG_6
, NULL
);
7095 setkeybinding(MODKEY
, XK_KP_Home
, KF_RG_7
, NULL
);
7096 setkeybinding(MODKEY
, XK_KP_Up
, KF_RG_8
, NULL
);
7097 setkeybinding(MODKEY
, XK_KP_Prior
, KF_RG_9
, NULL
);
7098 setkeybinding(MODKEY_SHIFT
, XK_Right
, KF_RG_NEXT
, NULL
);
7099 setkeybinding(MODKEY_SHIFT
, XK_Left
, KF_RG_PREV
, NULL
);
7100 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
7101 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
7102 setkeybinding(MODKEY_SHIFT
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
7103 setkeybinding(MODKEY_SHIFT
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
7104 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
7105 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
7106 setkeybinding(MODKEY_SHIFT
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
7107 setkeybinding(MODKEY_SHIFT
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
7108 setkeybinding(MODKEY_SHIFT
, XK_comma
, KF_STACK_INC
, NULL
);
7109 setkeybinding(MODKEY_SHIFT
, XK_period
, KF_STACK_DEC
, NULL
);
7110 setkeybinding(MODKEY_SHIFT
, XK_space
, KF_STACK_RESET
, NULL
);
7111 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
7112 setkeybinding(MODKEY_SHIFT
, XK_j
, KF_SWAP_NEXT
, NULL
);
7113 setkeybinding(MODKEY_SHIFT
, XK_k
, KF_SWAP_PREV
, NULL
);
7114 setkeybinding(MODKEY_SHIFT
, XK_w
, KF_UNICONIFY
, NULL
);
7115 setkeybinding(MODKEY_SHIFT
, XK_v
, KF_VERSION
, NULL
);
7116 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
7117 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
7118 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
7119 setkeybinding(MODKEY_SHIFT
, XK_x
, KF_WIND_KILL
, NULL
);
7120 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
7121 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
7122 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
7123 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
7124 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
7125 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
7126 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
7127 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
7128 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
7129 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
7130 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
7131 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
7132 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
7133 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
7134 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
7135 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
7136 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
7137 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
7138 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
7139 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
7140 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
7141 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
7142 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
7143 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
7144 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
7145 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
7146 setkeybinding(MODKEY_SHIFT
, XK_Up
, KF_WS_NEXT_MOVE
,NULL
);
7147 setkeybinding(MODKEY_SHIFT
, XK_Down
, KF_WS_PREV_MOVE
,NULL
);
7148 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
7150 setkeybinding(MODKEY_SHIFT
, XK_d
, KF_DUMPWINS
, NULL
);
7160 while (RB_EMPTY(&keys
) == 0) {
7161 kp
= RB_ROOT(&keys
);
7167 setkeymapping(const char *selector
, const char *value
, int flags
)
7169 char *keymapping_file
;
7171 /* suppress unused warnings since vars are needed */
7175 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
7177 keymapping_file
= expand_tilde(value
);
7180 /* load new key bindings; if it fails, revert to default bindings */
7181 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
7186 free(keymapping_file
);
7188 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
7193 updatenumlockmask(void)
7196 xcb_get_modifier_mapping_reply_t
*modmap_r
;
7197 xcb_keycode_t
*modmap
, kc
, *keycode
;
7201 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
7202 xcb_get_modifier_mapping(conn
),
7205 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
7206 for (i
= 0; i
< 8; i
++) {
7207 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
7208 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
7210 keycode
= xcb_key_symbols_get_keycode(syms
,
7214 numlockmask
= (1 << i
);
7221 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
7228 int num_screens
, k
, j
;
7229 unsigned int modifiers
[4];
7230 xcb_keycode_t
*code
;
7232 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
7233 updatenumlockmask();
7236 modifiers
[1] = numlockmask
;
7237 modifiers
[2] = XCB_MOD_MASK_LOCK
;
7238 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
7240 num_screens
= get_screen_count();
7241 for (k
= 0; k
< num_screens
; k
++) {
7242 if (TAILQ_EMPTY(&screens
[k
].rl
))
7244 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
7246 RB_FOREACH(kp
, key_tree
, &keys
) {
7247 /* Skip unused ws binds. */
7248 if ((int)kp
->funcid
> KF_WS_1
+ workspace_limit
- 1 &&
7249 kp
->funcid
<= KF_WS_22
)
7252 /* Skip unused mvws binds. */
7253 if ((int)kp
->funcid
> KF_MVWS_1
+ workspace_limit
- 1 &&
7254 kp
->funcid
<= KF_MVWS_22
)
7257 if ((code
= xcb_key_symbols_get_keycode(syms
,
7259 for (j
= 0; j
< LENGTH(modifiers
); j
++)
7260 xcb_grab_key(conn
, 1,
7262 kp
->mod
| modifiers
[j
],
7263 *code
, XCB_GRAB_MODE_SYNC
,
7264 XCB_GRAB_MODE_SYNC
);
7272 grabbuttons(struct ws_win
*win
)
7274 unsigned int modifiers
[4];
7277 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: win %#x\n", win
->id
);
7278 updatenumlockmask();
7281 modifiers
[1] = numlockmask
;
7282 modifiers
[2] = XCB_MOD_MASK_LOCK
;
7283 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
7285 for (i
= 0; i
< LENGTH(buttons
); i
++)
7286 if (buttons
[i
].action
== client_click
)
7287 for (j
= 0; j
< LENGTH(modifiers
); ++j
)
7288 xcb_grab_button(conn
, 0, win
->id
, BUTTONMASK
,
7289 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
,
7290 XCB_WINDOW_NONE
, XCB_CURSOR_NONE
,
7291 buttons
[i
].button
, buttons
[i
].mask
|
7295 const char *quirkname
[] = {
7296 "NONE", /* config string for "no value" */
7304 "FOCUSONMAP_SINGLE",
7307 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
7308 #define SWM_Q_WS "\n|+ \t"
7310 parsequirks(const char *qstr
, unsigned long *quirk
)
7312 char *str
, *cp
, *name
;
7315 if (quirk
== NULL
|| qstr
== NULL
)
7318 if ((str
= strdup(qstr
)) == NULL
)
7319 err(1, "parsequirks: strdup");
7323 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
7325 cp
+= (long)strspn(cp
, SWM_Q_WS
);
7326 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
7327 if (strncasecmp(name
, quirkname
[i
],
7328 SWM_QUIRK_LEN
) == 0) {
7329 DNPRINTF(SWM_D_QUIRK
,
7330 "parsequirks: %s\n", name
);
7336 *quirk
|= 1 << (i
-1);
7340 if (i
>= LENGTH(quirkname
)) {
7341 DNPRINTF(SWM_D_QUIRK
,
7342 "parsequirks: invalid quirk [%s]\n", name
);
7353 quirk_insert(const char *class, const char *instance
, const char *name
,
7354 unsigned long quirk
)
7360 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: class: %s, instance: %s, name: %s,"
7361 " value: %lu\n", class, instance
, name
, quirk
);
7363 if ((qp
= malloc(sizeof *qp
)) == NULL
)
7364 err(1, "quirk_insert: malloc");
7366 if ((qp
->class = strdup(class)) == NULL
)
7367 err(1, "quirk_insert: strdup");
7368 if ((qp
->instance
= strdup(instance
)) == NULL
)
7369 err(1, "quirk_insert: strdup");
7370 if ((qp
->name
= strdup(name
)) == NULL
)
7371 err(1, "quirk_insert: strdup");
7373 if (asprintf(&str
, "^%s$", class) == -1)
7374 err(1, "quirk_insert: asprintf");
7375 if (regcomp(&qp
->regex_class
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7376 add_startup_exception("regex failed to compile quirk 'class' "
7377 "field: %s", class);
7380 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7383 if (asprintf(&str
, "^%s$", instance
) == -1)
7384 err(1, "quirk_insert: asprintf");
7385 if (regcomp(&qp
->regex_instance
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7386 add_startup_exception("regex failed to compile quirk 'instance'"
7387 " field: %s", instance
);
7390 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7393 if (asprintf(&str
, "^%s$", name
) == -1)
7394 err(1, "quirk_insert: asprintf");
7395 if (regcomp(&qp
->regex_name
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7396 add_startup_exception("regex failed to compile quirk 'name' "
7400 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7404 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: regex error; skipping.\n");
7408 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
7410 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
7414 quirk_remove(struct quirk
*qp
)
7416 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
7417 qp
->name
, qp
->quirk
);
7419 TAILQ_REMOVE(&quirks
, qp
, entry
);
7422 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
7426 quirk_free(struct quirk
*qp
)
7428 regfree(&qp
->regex_class
);
7429 regfree(&qp
->regex_instance
);
7430 regfree(&qp
->regex_name
);
7438 quirk_replace(struct quirk
*qp
, const char *class, const char *instance
,
7439 const char *name
, unsigned long quirk
)
7441 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s:%s [%lu]\n", qp
->class,
7442 qp
->instance
, qp
->name
, qp
->quirk
);
7445 quirk_insert(class, instance
, name
, quirk
);
7447 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
7451 setquirk(const char *class, const char *instance
, const char *name
,
7452 unsigned long quirk
)
7456 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s:%s [%lu]\n", class,
7457 instance
, name
, quirk
);
7459 /* Remove/replace existing quirk. */
7460 TAILQ_FOREACH(qp
, &quirks
, entry
) {
7461 if (strcmp(qp
->class, class) == 0 &&
7462 strcmp(qp
->instance
, instance
) == 0 &&
7463 strcmp(qp
->name
, name
) == 0) {
7467 quirk_replace(qp
, class, instance
, name
, quirk
);
7468 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
7473 /* Only insert if quirk is not NONE. */
7475 quirk_insert(class, instance
, name
, quirk
);
7477 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
7480 /* Eat '\' in str used to escape square brackets and colon. */
7482 unescape_selector(char *str
)
7486 for (cp
= str
; *str
!= '\0'; ++str
, ++cp
) {
7487 if (*str
== '\\' && (*(str
+ 1) == ':' || *(str
+ 1) == ']' ||
7497 setconfquirk(const char *selector
, const char *value
, int flags
)
7499 char *str
, *cp
, *class;
7500 char *instance
= NULL
, *name
= NULL
;
7501 int retval
, count
= 0;
7504 /* suppress unused warning since var is needed */
7507 if (selector
== NULL
|| strlen(selector
) == 0)
7510 if ((str
= strdup(selector
)) == NULL
)
7511 err(1, "setconfquirk: strdup");
7513 /* Find non-escaped colon. */
7520 for (++cp
; *cp
!= '\0'; ++cp
) {
7521 if (*cp
== ':' && *(cp
- 1) != '\\') {
7527 unescape_selector(class);
7529 instance
= class + strlen(class) + 1;
7530 unescape_selector(instance
);
7536 name
= instance
+ strlen(instance
) + 1;
7537 unescape_selector(name
);
7542 DNPRINTF(SWM_D_CONF
, "setconfquirk: class: %s, instance: %s, "
7543 "name: %s\n", class, instance
, name
);
7545 if ((retval
= parsequirks(value
, &qrks
)) == 0)
7546 setquirk(class, instance
, name
, qrks
);
7555 setquirk("MPlayer", "xv", ".*", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
7556 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*", SWM_Q_FLOAT
);
7557 setquirk("Firefox-bin", "firefox-bin", ".*", SWM_Q_TRANSSZ
);
7558 setquirk("Firefox", "Dialog", ".*", SWM_Q_FLOAT
);
7559 setquirk("Gimp", "gimp", ".*", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
7560 setquirk("XTerm", "xterm", ".*", SWM_Q_XTERM_FONTADJ
);
7561 setquirk("xine", "Xine Window", ".*", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
7562 setquirk("Xitk", "Xitk Combo", ".*", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
7563 setquirk("xine", "xine Panel", ".*", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
7564 setquirk("Xitk", "Xine Window", ".*", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
7565 setquirk("xine", "xine Video Fullscreen Window", ".*", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
7566 setquirk("pcb", "pcb", ".*", SWM_Q_FLOAT
);
7567 setquirk("SDL_App", "SDL_App", ".*", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
7570 /* conf file stuff */
7571 #define SWM_CONF_FILE "spectrwm.conf"
7572 #define SWM_CONF_FILE_OLD "scrotwm.conf"
7576 SWM_S_BAR_AT_BOTTOM
,
7577 SWM_S_BAR_BORDER_WIDTH
,
7580 SWM_S_BAR_ENABLED_WS
,
7585 SWM_S_BOUNDARY_WIDTH
,
7586 SWM_S_CLOCK_ENABLED
,
7589 SWM_S_CYCLE_VISIBLE
,
7591 SWM_S_DISABLE_BORDER
,
7593 SWM_S_FOCUS_CLOSE_WRAP
,
7594 SWM_S_FOCUS_DEFAULT
,
7596 SWM_S_ICONIC_ENABLED
,
7597 SWM_S_REGION_PADDING
,
7602 SWM_S_STACK_ENABLED
,
7605 SWM_S_URGENT_ENABLED
,
7606 SWM_S_VERBOSE_LAYOUT
,
7607 SWM_S_WINDOW_CLASS_ENABLED
,
7608 SWM_S_WINDOW_INSTANCE_ENABLED
,
7609 SWM_S_WINDOW_NAME_ENABLED
,
7610 SWM_S_WORKSPACE_LIMIT
7614 setconfvalue(const char *selector
, const char *value
, int flags
)
7616 struct workspace
*ws
;
7617 int i
, ws_id
, num_screens
;
7621 case SWM_S_BAR_ACTION
:
7623 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
7624 err(1, "setconfvalue: bar_action");
7626 case SWM_S_BAR_AT_BOTTOM
:
7627 bar_at_bottom
= atoi(value
);
7629 case SWM_S_BAR_BORDER_WIDTH
:
7630 bar_border_width
= atoi(value
);
7631 if (bar_border_width
< 0)
7632 bar_border_width
= 0;
7634 case SWM_S_BAR_DELAY
:
7635 /* No longer needed; leave to not break old conf files. */
7637 case SWM_S_BAR_ENABLED
:
7638 bar_enabled
= atoi(value
);
7640 case SWM_S_BAR_ENABLED_WS
:
7641 ws_id
= atoi(selector
) - 1;
7642 if (ws_id
< 0 || ws_id
>= workspace_limit
)
7643 errx(1, "setconfvalue: bar_enabled_ws: invalid "
7644 "workspace %d.", ws_id
+ 1);
7646 num_screens
= get_screen_count();
7647 for (i
= 0; i
< num_screens
; i
++) {
7648 ws
= (struct workspace
*)&screens
[i
].ws
;
7649 ws
[ws_id
].bar_enabled
= atoi(value
);
7652 case SWM_S_BAR_FONT
:
7654 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
7655 err(1, "setconfvalue: asprintf: failed to allocate "
7656 "memory for bar_fonts.");
7659 /* If already in xft mode, then we are done. */
7660 if (!bar_font_legacy
)
7663 if ((str
= strdup(value
)) == NULL
)
7664 err(1, "setconfvalue: strdup");
7666 /* If there are any non-XLFD entries, switch to Xft mode. */
7667 while ((b
= strsep(&str
, ",")) != NULL
) {
7671 bar_font_legacy
= 0;
7678 case SWM_S_BAR_FORMAT
:
7680 if ((bar_format
= strdup(value
)) == NULL
)
7681 err(1, "setconfvalue: bar_format");
7683 case SWM_S_BAR_JUSTIFY
:
7684 if (strcmp(value
, "left") == 0)
7685 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
7686 else if (strcmp(value
, "center") == 0)
7687 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
7688 else if (strcmp(value
, "right") == 0)
7689 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
7691 errx(1, "invalid bar_justify");
7693 case SWM_S_BORDER_WIDTH
:
7694 border_width
= atoi(value
);
7695 if (border_width
< 0)
7698 case SWM_S_BOUNDARY_WIDTH
:
7699 boundary_width
= atoi(value
);
7700 if (boundary_width
< 0)
7703 case SWM_S_CLOCK_ENABLED
:
7704 clock_enabled
= atoi(value
);
7706 case SWM_S_CLOCK_FORMAT
:
7707 #ifndef SWM_DENY_CLOCK_FORMAT
7709 if ((clock_format
= strdup(value
)) == NULL
)
7710 err(1, "setconfvalue: clock_format");
7713 case SWM_S_CYCLE_EMPTY
:
7714 cycle_empty
= atoi(value
);
7716 case SWM_S_CYCLE_VISIBLE
:
7717 cycle_visible
= atoi(value
);
7719 case SWM_S_DIALOG_RATIO
:
7720 dialog_ratio
= atof(value
);
7721 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
7724 case SWM_S_DISABLE_BORDER
:
7725 disable_border
= atoi(value
);
7727 case SWM_S_FOCUS_CLOSE
:
7728 if (strcmp(value
, "first") == 0)
7729 focus_close
= SWM_STACK_BOTTOM
;
7730 else if (strcmp(value
, "last") == 0)
7731 focus_close
= SWM_STACK_TOP
;
7732 else if (strcmp(value
, "next") == 0)
7733 focus_close
= SWM_STACK_ABOVE
;
7734 else if (strcmp(value
, "previous") == 0)
7735 focus_close
= SWM_STACK_BELOW
;
7737 errx(1, "focus_close");
7739 case SWM_S_FOCUS_CLOSE_WRAP
:
7740 focus_close_wrap
= atoi(value
);
7742 case SWM_S_FOCUS_DEFAULT
:
7743 if (strcmp(value
, "last") == 0)
7744 focus_default
= SWM_STACK_TOP
;
7745 else if (strcmp(value
, "first") == 0)
7746 focus_default
= SWM_STACK_BOTTOM
;
7748 errx(1, "focus_default");
7750 case SWM_S_FOCUS_MODE
:
7751 if (strcmp(value
, "default") == 0)
7752 focus_mode
= SWM_FOCUS_DEFAULT
;
7753 else if (strcmp(value
, "follow") == 0 ||
7754 strcmp(value
, "follow_cursor") == 0)
7755 focus_mode
= SWM_FOCUS_FOLLOW
;
7756 else if (strcmp(value
, "manual") == 0)
7757 focus_mode
= SWM_FOCUS_MANUAL
;
7759 errx(1, "focus_mode");
7761 case SWM_S_ICONIC_ENABLED
:
7762 iconic_enabled
= atoi(value
);
7764 case SWM_S_REGION_PADDING
:
7765 region_padding
= atoi(value
);
7766 if (region_padding
< 0)
7769 case SWM_S_SPAWN_ORDER
:
7770 if (strcmp(value
, "first") == 0)
7771 spawn_position
= SWM_STACK_BOTTOM
;
7772 else if (strcmp(value
, "last") == 0)
7773 spawn_position
= SWM_STACK_TOP
;
7774 else if (strcmp(value
, "next") == 0)
7775 spawn_position
= SWM_STACK_ABOVE
;
7776 else if (strcmp(value
, "previous") == 0)
7777 spawn_position
= SWM_STACK_BELOW
;
7779 errx(1, "spawn_position");
7781 case SWM_S_SPAWN_TERM
:
7782 setconfspawn("term", value
, 0);
7783 setconfspawn("spawn_term", value
, 0);
7786 /* No longer needed; leave to not break old conf files. */
7788 case SWM_S_SS_ENABLED
:
7789 /* No longer needed; leave to not break old conf files. */
7791 case SWM_S_STACK_ENABLED
:
7792 stack_enabled
= atoi(value
);
7794 case SWM_S_TERM_WIDTH
:
7795 term_width
= atoi(value
);
7799 case SWM_S_TILE_GAP
:
7800 tile_gap
= atoi(value
);
7802 case SWM_S_URGENT_ENABLED
:
7803 urgent_enabled
= atoi(value
);
7805 case SWM_S_VERBOSE_LAYOUT
:
7806 verbose_layout
= atoi(value
);
7807 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
7809 layouts
[i
].l_string
= fancy_stacker
;
7811 layouts
[i
].l_string
= plain_stacker
;
7814 case SWM_S_WINDOW_CLASS_ENABLED
:
7815 window_class_enabled
= atoi(value
);
7817 case SWM_S_WINDOW_INSTANCE_ENABLED
:
7818 window_instance_enabled
= atoi(value
);
7820 case SWM_S_WINDOW_NAME_ENABLED
:
7821 window_name_enabled
= atoi(value
);
7823 case SWM_S_WORKSPACE_LIMIT
:
7824 workspace_limit
= atoi(value
);
7825 if (workspace_limit
> SWM_WS_MAX
)
7826 workspace_limit
= SWM_WS_MAX
;
7827 else if (workspace_limit
< 1)
7828 workspace_limit
= 1;
7830 num_screens
= get_screen_count();
7831 for (i
= 0; i
< num_screens
; i
++) {
7832 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
7833 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
7834 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
7844 setconfmodkey(const char *selector
, const char *value
, int flags
)
7846 /* suppress unused warnings since vars are needed */
7850 if (strncasecmp(value
, "Mod1", strlen("Mod1")) == 0)
7851 update_modkey(XCB_MOD_MASK_1
);
7852 else if (strncasecmp(value
, "Mod2", strlen("Mod2")) == 0)
7853 update_modkey(XCB_MOD_MASK_2
);
7854 else if (strncasecmp(value
, "Mod3", strlen("Mod3")) == 0)
7855 update_modkey(XCB_MOD_MASK_3
);
7856 else if (strncasecmp(value
, "Mod4", strlen("Mod4")) == 0)
7857 update_modkey(XCB_MOD_MASK_4
);
7864 setconfcolor(const char *selector
, const char *value
, int flags
)
7866 setscreencolor(value
,
7867 (selector
== NULL
|| strlen(selector
) == 0) ? -1 : atoi(selector
),
7873 setconfregion(const char *selector
, const char *value
, int flags
)
7875 /* suppress unused warnings since vars are needed */
7879 custom_region(value
);
7884 setautorun(const char *selector
, const char *value
, int flags
)
7894 /* suppress unused warnings since vars are needed */
7898 if (getenv("SWM_STARTED"))
7902 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
7903 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
7905 if (ws_id
< 0 || ws_id
>= workspace_limit
)
7906 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
7908 sp
= expand_tilde((char *)&s
);
7911 * This is a little intricate
7913 * If the pid already exists we simply reuse it because it means it was
7914 * used before AND not claimed by manage_window. We get away with
7915 * altering it in the parent after INSERT because this can not be a race
7918 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
7921 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
7923 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
7924 err(1, "setautorun: realloc");
7925 a
.argv
[argc
- 1] = ap
;
7929 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
7930 err(1, "setautorun: realloc");
7931 a
.argv
[argc
] = NULL
;
7933 if ((pid
= fork()) == 0) {
7934 spawn(ws_id
, &a
, 1);
7943 p
= calloc(1, sizeof *p
);
7946 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
7956 setlayout(const char *selector
, const char *value
, int flags
)
7958 int ws_id
, i
, x
, mg
, ma
, si
, ar
, f
= 0;
7959 int st
= SWM_V_STACK
, num_screens
;
7961 struct workspace
*ws
;
7963 /* suppress unused warnings since vars are needed */
7967 if (getenv("SWM_STARTED"))
7971 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
7972 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
7973 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7974 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7977 if (ws_id
< 0 || ws_id
>= workspace_limit
)
7978 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
7980 if (strcasecmp(s
, "vertical") == 0)
7982 else if (strcasecmp(s
, "vertical_flip") == 0) {
7985 } else if (strcasecmp(s
, "horizontal") == 0)
7987 else if (strcasecmp(s
, "horizontal_flip") == 0) {
7990 } else if (strcasecmp(s
, "fullscreen") == 0)
7993 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
7994 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
7997 num_screens
= get_screen_count();
7998 for (i
= 0; i
< num_screens
; i
++) {
7999 ws
= (struct workspace
*)&screens
[i
].ws
;
8000 ws
[ws_id
].cur_layout
= &layouts
[st
];
8002 ws
[ws_id
].always_raise
= ar
;
8003 if (st
== SWM_MAX_STACK
)
8007 for (x
= 0; x
< abs(mg
); x
++) {
8008 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8009 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
8010 SWM_ARG_ID_MASTERSHRINK
);
8014 for (x
= 0; x
< abs(ma
); x
++) {
8015 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8016 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
8017 SWM_ARG_ID_MASTERDEL
);
8021 for (x
= 0; x
< abs(si
); x
++) {
8022 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8023 si
>= 0 ? SWM_ARG_ID_STACKINC
:
8024 SWM_ARG_ID_STACKDEC
);
8029 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8030 SWM_ARG_ID_FLIPLAYOUT
);
8040 /* config options */
8041 struct config_option
{
8043 int (*func
)(const char*, const char*, int);
8046 struct config_option configopt
[] = {
8047 { "autorun", setautorun
, 0 },
8048 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
8049 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
8050 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
8051 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
8052 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
8053 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
8054 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
8055 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
8056 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
8057 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
8058 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
8059 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
8060 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
8061 { "bind", setconfbinding
, 0 },
8062 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
8063 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
8064 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
8065 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
8066 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
8067 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
8068 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
8069 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
8070 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
8071 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
8072 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
8073 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
8074 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
8075 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
8076 { "iconic_enabled", setconfvalue
, SWM_S_ICONIC_ENABLED
},
8077 { "keyboard_mapping", setkeymapping
, 0 },
8078 { "layout", setlayout
, 0 },
8079 { "modkey", setconfmodkey
, 0 },
8080 { "program", setconfspawn
, 0 },
8081 { "quirk", setconfquirk
, 0 },
8082 { "region", setconfregion
, 0 },
8083 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
8084 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
8085 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
8086 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
8087 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
8088 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
8089 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
8090 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
8091 { "title_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
}, /* For backwards compat. */
8092 { "title_name_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
}, /* For backwards compat. */
8093 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
8094 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
8095 { "window_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
},
8096 { "window_instance_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
},
8097 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
8098 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
8102 _add_startup_exception(const char *fmt
, va_list ap
)
8104 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
8105 warn("%s: asprintf", __func__
);
8109 add_startup_exception(const char *fmt
, ...)
8115 if (startup_exception
)
8118 /* force bar to be enabled due to exception */
8122 _add_startup_exception(fmt
, ap
);
8127 conf_load(const char *filename
, int keymapping
)
8130 char *line
= NULL
, *cp
, *ce
, *optsub
, *optval
= NULL
;
8131 size_t linelen
, lineno
= 0;
8132 int wordlen
, i
, optidx
, count
;
8133 struct config_option
*opt
= NULL
;
8135 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
8137 if (filename
== NULL
) {
8138 warnx("conf_load: no filename");
8142 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
8144 if ((config
= fopen(filename
, "r")) == NULL
) {
8145 warn("conf_load: fopen: %s", filename
);
8149 while (!feof(config
)) {
8153 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
8154 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
8156 err(1, "%s", filename
);
8161 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
8162 if (cp
[0] == '\0') {
8166 /* get config option */
8167 wordlen
= strcspn(cp
, "=[ \t\n");
8169 add_startup_exception("%s: line %zd: no option found",
8174 for (i
= 0; i
< LENGTH(configopt
); i
++) {
8175 opt
= &configopt
[i
];
8176 if (strncasecmp(cp
, opt
->optname
, wordlen
) == 0 &&
8177 (int)strlen(opt
->optname
) == wordlen
) {
8183 add_startup_exception("%s: line %zd: unknown option "
8184 "%.*s", filename
, lineno
, wordlen
, cp
);
8187 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
8188 add_startup_exception("%s: line %zd: invalid option "
8189 "%.*s", filename
, lineno
, wordlen
, cp
);
8193 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
8195 /* get [selector] if any */
8200 /* Get length of selector. */
8201 for (ce
= ++cp
; *ce
!= '\0'; ++ce
) {
8202 /* Find matching (not escaped) bracket. */
8203 if (*ce
== ']' && *(ce
- 1) != '\\') {
8210 add_startup_exception("%s: line %zd: syntax "
8211 "error: unterminated selector", filename
,
8216 /* ce points at ']'; terminate optsub. */
8221 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
8224 if (strlen(optval
) == 0) {
8225 add_startup_exception("%s: line %zd: must supply value "
8226 "to %s", filename
, lineno
,
8227 configopt
[optidx
].optname
);
8230 /* call function to deal with it all */
8231 if (configopt
[optidx
].func(optsub
, optval
,
8232 configopt
[optidx
].funcflags
) != 0) {
8233 add_startup_exception("%s: line %zd: invalid data for "
8234 "%s", filename
, lineno
, configopt
[optidx
].optname
);
8242 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
8248 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
8250 struct ws_win
*parent
, *w
;
8251 struct swm_region
*r
;
8252 struct workspace
*ws
;
8253 xcb_icccm_wm_hints_t wmh
;
8255 parent
= find_window(win
->transient
);
8257 parent
->focus_child
= win
;
8259 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
8260 " for %#x trans %#x\n", win
->id
, win
->transient
);
8262 r
= root_to_region(win
->s
->root
, SWM_CK_ALL
);
8264 /* parent doen't exist in our window list */
8265 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
8266 if (xcb_icccm_get_wm_hints_reply(conn
,
8267 xcb_icccm_get_wm_hints(conn
, w
->id
),
8269 warnx("can't get hints for %#x", w
->id
);
8273 if (win
->hints
.window_group
!= wmh
.window_group
)
8276 w
->focus_child
= win
;
8277 win
->transient
= w
->id
;
8279 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
8280 "transient to %#x\n", win
->transient
);
8287 window_get_pid(xcb_window_t win
)
8292 xcb_get_property_cookie_t pc
;
8293 xcb_get_property_reply_t
*pr
;
8295 apid
= get_atom_from_string("_NET_WM_PID");
8296 if (apid
== XCB_ATOM_NONE
)
8299 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
8300 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
8303 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
8308 if (pr
->type
== apid
&& pr
->format
== 32)
8309 ret
= *((pid_t
*)xcb_get_property_value(pr
));
8315 apid
= get_atom_from_string("_SWM_PID");
8316 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
8318 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
8321 if (pr
->type
!= apid
) {
8326 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
8333 get_swm_ws(xcb_window_t id
)
8339 xcb_get_property_reply_t
*gpr
;
8341 gpr
= xcb_get_property_reply(conn
,
8342 xcb_get_property(conn
, 0, id
, a_swm_ws
,
8343 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
8348 proplen
= xcb_get_property_value_length(gpr
);
8350 prop
= malloc(proplen
+ 1);
8353 xcb_get_property_value(gpr
),
8355 prop
[proplen
] = '\0';
8362 DNPRINTF(SWM_D_PROP
, "get_swm_ws: _SWM_WS: %s\n", prop
);
8363 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
8365 DNPRINTF(SWM_D_PROP
, "get_swm_ws: win #%s: %s",
8375 get_ws_idx(xcb_window_t id
)
8377 xcb_get_property_reply_t
*gpr
;
8380 gpr
= xcb_get_property_reply(conn
,
8381 xcb_get_property(conn
, 0, id
, ewmh
[_NET_WM_DESKTOP
].atom
,
8382 XCB_ATOM_CARDINAL
, 0, 1),
8385 if (gpr
->type
== XCB_ATOM_CARDINAL
&& gpr
->format
== 32)
8386 ws_idx
= *((int *)xcb_get_property_value(gpr
));
8391 if ((ws_idx
= get_swm_ws(id
)) != -1)
8392 xcb_delete_property(conn
, id
, a_swm_ws
);
8394 if (ws_idx
> workspace_limit
- 1 || ws_idx
< -1)
8397 DNPRINTF(SWM_D_PROP
, "get_ws_idx: win %#x, ws_idx: %d\n", id
, ws_idx
);
8403 manage_window(xcb_window_t id
, uint16_t mapped
)
8405 xcb_window_t trans
= XCB_WINDOW_NONE
;
8406 struct ws_win
*win
, *ww
;
8408 char *class, *instance
, *name
;
8409 struct swm_region
*r
;
8412 uint32_t i
, wa
[2], new_flags
;
8413 xcb_get_geometry_reply_t
*gr
;
8415 if ((win
= find_window(id
)) != NULL
) {
8416 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x already "
8417 "managed; skipping.)\n", id
);
8418 return (win
); /* Already managed. */
8421 /* See if window is on the unmanaged list. */
8422 if ((win
= find_unmanaged_window(id
)) != NULL
) {
8423 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x found on "
8424 "unmanaged list.\n", id
);
8425 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
8428 set_child_transient(win
, &trans
);
8432 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is new.\n", id
);
8435 /* Try to get initial window geometry. */
8436 gr
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, id
), NULL
);
8438 DNPRINTF(SWM_D_MISC
, "manage_window: get geometry failed.\n");
8442 /* Create and initialize ws_win object. */
8443 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
8444 err(1, "manage_window: calloc: failed to allocate memory for "
8449 /* Figureout which region the window belongs to. */
8450 r
= root_to_region(gr
->root
, SWM_CK_ALL
);
8452 /* Ignore window border if there is one. */
8453 WIDTH(win
) = gr
->width
;
8454 HEIGHT(win
) = gr
->height
;
8455 X(win
) = gr
->x
+ gr
->border_width
- border_width
;
8456 Y(win
) = gr
->y
+ gr
->border_width
- border_width
;
8458 win
->mapped
= mapped
;
8459 win
->s
= r
->s
; /* this never changes */
8463 /* Select which X events to monitor and set border pixel color. */
8464 wa
[0] = win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
8465 wa
[1] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
8466 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
8468 wa
[1] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
8471 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
|
8472 XCB_CW_EVENT_MASK
, wa
);
8474 /* Get WM_SIZE_HINTS. */
8475 xcb_icccm_get_wm_normal_hints_reply(conn
,
8476 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
8480 xcb_icccm_get_wm_hints_reply(conn
,
8481 xcb_icccm_get_wm_hints(conn
, win
->id
),
8484 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
8485 xcb_icccm_get_wm_transient_for_reply(conn
,
8486 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
8489 win
->transient
= trans
;
8490 set_child_transient(win
, &win
->transient
);
8493 /* Get WM_PROTOCOLS. */
8494 get_wm_protocols(win
);
8496 /* Figure out which workspace the window belongs to. */
8497 if ((p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
8498 win
->ws
= &r
->s
->ws
[p
->ws
];
8499 TAILQ_REMOVE(&pidlist
, p
, entry
);
8502 } else if ((ws_idx
= get_ws_idx(win
->id
)) != -1 &&
8504 /* _SWM_WS is set; use that. */
8505 win
->ws
= &r
->s
->ws
[ws_idx
];
8506 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
8507 /* Launch transients in the same ws as parent. */
8513 /* Set the _NET_WM_DESKTOP atom. */
8514 DNPRINTF(SWM_D_PROP
, "manage_window: set _NET_WM_DESKTOP: %d\n",
8516 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
8517 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &win
->ws
->idx
);
8519 /* WS must already be set for this to work. */
8520 store_float_geom(win
);
8522 /* Set initial quirks based on EWMH. */
8523 ewmh_autoquirk(win
);
8525 /* Determine initial quirks. */
8526 xcb_icccm_get_wm_class_reply(conn
,
8527 xcb_icccm_get_wm_class(conn
, win
->id
),
8530 class = win
->ch
.class_name
? win
->ch
.class_name
: "";
8531 instance
= win
->ch
.instance_name
? win
->ch
.instance_name
: "";
8532 name
= get_win_name(win
->id
);
8534 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, instance: %s, "
8535 "name: %s\n", class, instance
, name
);
8537 /* java is retarded so treat it special */
8538 if (strstr(instance
, "sun-awt")) {
8539 DNPRINTF(SWM_D_CLASS
, "manage_window: java window detected.\n");
8543 TAILQ_FOREACH(qp
, &quirks
, entry
) {
8544 if (regexec(&qp
->regex_class
, class, 0, NULL
, 0) == 0 &&
8545 regexec(&qp
->regex_instance
, instance
, 0, NULL
, 0) == 0 &&
8546 regexec(&qp
->regex_name
, name
, 0, NULL
, 0) == 0) {
8547 DNPRINTF(SWM_D_CLASS
, "manage_window: matched "
8548 "quirk: %s:%s:%s mask: %#lx\n", qp
->class,
8549 qp
->instance
, qp
->name
, qp
->quirk
);
8550 win
->quirks
= qp
->quirk
;
8556 /* Reset font sizes (the bruteforce way; no default keybinding). */
8557 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
8558 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
8559 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
8560 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
8561 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
8564 /* Make sure window is positioned inside its region, if its active. */
8566 region_containment(win
, r
, SWM_CW_ALLSIDES
|
8567 SWM_CW_HARDBOUNDARY
);
8572 /* Figure out where to stack the window in the workspace. */
8573 if (trans
&& (ww
= find_window(trans
)))
8574 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
8575 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
8576 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
8578 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
8579 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
8580 else switch (spawn_position
) {
8583 case SWM_STACK_ABOVE
:
8584 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
8586 case SWM_STACK_BOTTOM
:
8587 case SWM_STACK_BELOW
:
8588 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
8591 ewmh_update_client_list();
8593 TAILQ_INSERT_TAIL(&win
->ws
->stack
, win
, stack_entry
);
8595 /* Get/apply initial _NET_WM_STATE */
8596 ewmh_get_wm_state(win
);
8599 new_flags
= win
->ewmh_flags
;
8601 if (win
->quirks
& SWM_Q_FLOAT
)
8602 new_flags
|= EWMH_F_ABOVE
;
8604 if (win
->quirks
& SWM_Q_ANYWHERE
)
8605 new_flags
|= SWM_F_MANUAL
;
8607 ewmh_apply_flags(win
, new_flags
);
8608 ewmh_update_wm_state(win
);
8610 /* Set initial _NET_WM_ALLOWED_ACTIONS */
8611 ewmh_update_actions(win
);
8615 DNPRINTF(SWM_D_MISC
, "manage_window: done. win %#x, (x,y) w x h: "
8616 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win
->id
,
8617 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
8618 YESNO(ICONIC(win
)), win
->transient
);
8624 free_window(struct ws_win
*win
)
8626 DNPRINTF(SWM_D_MISC
, "free_window: win %#x\n", win
->id
);
8631 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
8633 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
8638 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
8641 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
8645 unmanage_window(struct ws_win
*win
)
8647 struct ws_win
*parent
;
8652 DNPRINTF(SWM_D_MISC
, "unmanage_window: win %#x\n", win
->id
);
8655 parent
= find_window(win
->transient
);
8657 parent
->focus_child
= NULL
;
8660 TAILQ_REMOVE(&win
->ws
->stack
, win
, stack_entry
);
8661 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
8662 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
8664 ewmh_update_client_list();
8668 expose(xcb_expose_event_t
*e
)
8671 struct swm_region
*r
;
8673 DNPRINTF(SWM_D_EVENT
, "expose: win %#x\n", e
->window
);
8675 num_screens
= get_screen_count();
8676 for (i
= 0; i
< num_screens
; i
++)
8677 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
8678 if (e
->window
== WINID(r
->bar
))
8686 focusin(xcb_focus_in_event_t
*e
)
8688 DNPRINTF(SWM_D_EVENT
, "focusin: win %#x, mode: %s(%u), "
8689 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
8690 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
8694 focusout(xcb_focus_out_event_t
*e
)
8696 DNPRINTF(SWM_D_EVENT
, "focusout: win %#x, mode: %s(%u), "
8697 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
8698 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
8703 keypress(xcb_key_press_event_t
*e
)
8705 xcb_keysym_t keysym
;
8708 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
8710 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
8711 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
8712 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
8713 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
8714 e
->child
, e
->state
, YESNO(e
->same_screen
));
8716 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
8719 last_event_time
= e
->time
;
8721 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
8722 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
),
8723 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
8724 else if (keyfuncs
[kp
->funcid
].func
)
8725 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
, SWM_CK_ALL
),
8726 &(keyfuncs
[kp
->funcid
].args
));
8729 /* Unfreeze grab events. */
8730 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
, e
->time
);
8733 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
8737 buttonpress(xcb_button_press_event_t
*e
)
8739 struct ws_win
*win
= NULL
;
8740 struct swm_region
*r
, *old_r
;
8744 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): %#x (%d,%d), "
8745 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
8746 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
8747 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
8748 e
->state
, YESNO(e
->same_screen
));
8750 if (e
->event
== e
->root
) {
8751 if (e
->child
!= 0) {
8752 win
= find_window(e
->child
);
8753 /* Pass ButtonPress to window if it isn't managed. */
8757 /* Focus on empty region */
8758 /* If no windows on region if its empty. */
8759 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
8761 DNPRINTF(SWM_D_EVENT
, "buttonpress: "
8762 "NULL region; ignoring.\n");
8766 if (TAILQ_EMPTY(&r
->ws
->winlist
)) {
8767 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
8768 if (old_r
&& old_r
!= r
)
8769 unfocus_win(old_r
->ws
->focus
);
8771 xcb_set_input_focus(conn
,
8772 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
8774 /* Clear bar since empty. */
8782 win
= find_window(e
->event
);
8788 last_event_time
= e
->time
;
8790 focus_win(get_focus_magic(win
));
8792 for (i
= 0; i
< LENGTH(buttons
); i
++)
8793 if (client_click
== buttons
[i
].action
&& buttons
[i
].func
&&
8794 buttons
[i
].button
== e
->detail
&&
8795 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
)) {
8796 buttons
[i
].func(win
, &buttons
[i
].args
);
8802 DNPRINTF(SWM_D_EVENT
, "buttonpress: passing to window.\n");
8803 /* Replay event to event window */
8804 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
8806 DNPRINTF(SWM_D_EVENT
, "buttonpress: handled.\n");
8807 /* Unfreeze grab events. */
8808 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
8816 print_win_geom(xcb_window_t w
)
8818 xcb_get_geometry_reply_t
*wa
;
8820 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
8822 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: %#x\n",
8827 DNPRINTF(SWM_D_MISC
, "print_win_geom: win %#x, root: %#x, "
8828 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
8829 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
8838 get_stack_mode_name(uint8_t mode
)
8843 case XCB_STACK_MODE_ABOVE
:
8846 case XCB_STACK_MODE_BELOW
:
8849 case XCB_STACK_MODE_TOP_IF
:
8852 case XCB_STACK_MODE_BOTTOM_IF
:
8855 case XCB_STACK_MODE_OPPOSITE
:
8867 configurerequest(xcb_configure_request_event_t
*e
)
8870 struct swm_region
*r
= NULL
;
8873 uint32_t wc
[7] = {0};
8875 if ((win
= find_window(e
->window
)) == NULL
)
8876 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
8880 if (swm_debug
& SWM_D_EVENT
) {
8881 print_win_geom(e
->window
);
8883 DNPRINTF(SWM_D_EVENT
, "configurerequest: win %#x, "
8884 "parent: %#x, new: %s, value_mask: %u { ", e
->window
,
8885 e
->parent
, YESNO(new), e
->value_mask
);
8886 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
8887 DPRINTF("X: %d ", e
->x
);
8888 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
8889 DPRINTF("Y: %d ", e
->y
);
8890 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
8891 DPRINTF("W: %u ", e
->width
);
8892 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
8893 DPRINTF("H: %u ", e
->height
);
8894 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
8895 DPRINTF("Border: %u ", e
->border_width
);
8896 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
8897 DPRINTF("Sibling: %#x ", e
->sibling
);
8898 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
8899 DPRINTF("StackMode: %s(%u) ",
8900 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
8906 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
8907 mask
|= XCB_CONFIG_WINDOW_X
;
8910 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
8911 mask
|= XCB_CONFIG_WINDOW_Y
;
8914 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
8915 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
8918 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
8919 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
8920 wc
[i
++] = e
->height
;
8922 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
8923 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
8924 wc
[i
++] = e
->border_width
;
8926 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
8927 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
8928 wc
[i
++] = e
->sibling
;
8930 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
8931 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
8932 wc
[i
++] = e
->stack_mode
;
8936 xcb_configure_window(conn
, e
->window
, mask
, wc
);
8939 } else if ((!MANUAL(win
) || win
->quirks
& SWM_Q_ANYWHERE
) &&
8940 !FULLSCREEN(win
) && !MAXIMIZED(win
)) {
8943 else if (win
->ws
->old_r
)
8946 /* windows are centered unless ANYWHERE quirk is set. */
8947 if (win
->quirks
& SWM_Q_ANYWHERE
) {
8948 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
8949 win
->g_float
.x
= e
->x
;
8951 win
->g_float
.x
-= X(r
);
8954 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
8955 win
->g_float
.y
= e
->y
;
8957 win
->g_float
.y
-= Y(r
);
8961 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
8962 win
->g_float
.w
= e
->width
;
8964 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
8965 win
->g_float
.h
= e
->height
;
8967 win
->g_floatvalid
= 1;
8969 if (ABOVE(win
) && r
&& !MAXIMIZED(win
) && (TRANS(win
) ||
8970 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
])) {
8971 WIDTH(win
) = win
->g_float
.w
;
8972 HEIGHT(win
) = win
->g_float
.h
;
8975 update_floater(win
);
8987 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
8991 configurenotify(xcb_configure_notify_event_t
*e
)
8995 DNPRINTF(SWM_D_EVENT
, "configurenotify: win %#x, event win: %#x, "
8996 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
8997 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
8998 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
8999 YESNO(e
->override_redirect
));
9001 win
= find_window(e
->window
);
9003 xcb_icccm_get_wm_normal_hints_reply(conn
,
9004 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
9007 if (font_adjusted
) {
9015 destroynotify(xcb_destroy_notify_event_t
*e
)
9019 DNPRINTF(SWM_D_EVENT
, "destroynotify: win %#x\n", e
->window
);
9021 if ((win
= find_window(e
->window
)) == NULL
) {
9022 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
9028 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
9029 /* If we were focused, make sure we focus on something else. */
9030 if (win
== win
->ws
->focus
)
9031 win
->ws
->focus_pending
= get_focus_prev(win
);
9034 unmanage_window(win
);
9037 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
9038 if (win
->ws
->focus_pending
) {
9039 focus_win(win
->ws
->focus_pending
);
9040 win
->ws
->focus_pending
= NULL
;
9041 } else if (win
== win
->ws
->focus
) {
9042 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
9043 win
->ws
->r
->id
, XCB_CURRENT_TIME
);
9054 get_notify_detail_label(uint8_t detail
)
9059 case XCB_NOTIFY_DETAIL_ANCESTOR
:
9062 case XCB_NOTIFY_DETAIL_VIRTUAL
:
9065 case XCB_NOTIFY_DETAIL_INFERIOR
:
9068 case XCB_NOTIFY_DETAIL_NONLINEAR
:
9069 label
= "Nonlinear";
9071 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
9072 label
= "NonlinearVirtual";
9074 case XCB_NOTIFY_DETAIL_POINTER
:
9077 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
9078 label
= "PointerRoot";
9080 case XCB_NOTIFY_DETAIL_NONE
:
9091 get_notify_mode_label(uint8_t mode
)
9096 case XCB_NOTIFY_MODE_NORMAL
:
9099 case XCB_NOTIFY_MODE_GRAB
:
9102 case XCB_NOTIFY_MODE_UNGRAB
:
9105 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
9106 label
= "WhileGrabbed";
9117 enternotify(xcb_enter_notify_event_t
*e
)
9120 struct swm_region
*r
;
9122 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): %#x "
9123 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9124 "child: %#x, same_screen_focus: %s, state: %d\n",
9125 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9126 get_notify_mode_label(e
->mode
), e
->mode
,
9127 get_notify_detail_label(e
->detail
), e
->detail
,
9128 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9129 YESNO(e
->same_screen_focus
), e
->state
);
9131 if (focus_mode
== SWM_FOCUS_MANUAL
&&
9132 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
9133 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
9137 last_event_time
= e
->time
;
9139 if ((win
= find_window(e
->event
)) == NULL
) {
9140 if (e
->event
== e
->root
) {
9141 /* If no windows on pointer region, then focus root. */
9142 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
9144 DNPRINTF(SWM_D_EVENT
, "enternotify: "
9145 "NULL region; ignoring.\n");
9151 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
9156 focus_win(get_focus_magic(win
));
9164 leavenotify(xcb_leave_notify_event_t
*e
)
9166 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): %#x "
9167 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9168 "child: %#x, same_screen_focus: %s, state: %d\n",
9169 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9170 get_notify_mode_label(e
->mode
), e
->mode
,
9171 get_notify_detail_label(e
->detail
), e
->detail
,
9172 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9173 YESNO(e
->same_screen_focus
), e
->state
);
9178 mapnotify(xcb_map_notify_event_t
*e
)
9180 struct ws_win
*win
, *parent
= NULL
;
9181 struct workspace
*ws
;
9183 DNPRINTF(SWM_D_EVENT
, "mapnotify: win %#x\n", e
->window
);
9185 if ((win
= manage_window(e
->window
, 1)) == NULL
)
9189 /* Need to know if win was mapped due to ws switch. */
9190 if (ws
->state
== SWM_WS_STATE_MAPPED
) {
9191 if (ws
->focus_pending
&& TRANS(ws
->focus_pending
))
9192 parent
= find_window(win
->transient
);
9194 /* If window's parent is maximized, don't clear it. */
9195 if ((parent
== NULL
) || !MAXIMIZED(parent
))
9196 if (clear_maximized(ws
) > 0)
9201 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
9203 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
9204 if (ws
->focus_pending
== win
) {
9206 ws
->focus_pending
= NULL
;
9215 mappingnotify(xcb_mapping_notify_event_t
*e
)
9218 int i
, j
, num_screens
;
9220 xcb_refresh_keyboard_mapping(syms
, e
);
9222 if (e
->request
== XCB_MAPPING_KEYBOARD
) {
9225 /* Regrab buttons on all managed windows. */
9226 num_screens
= get_screen_count();
9227 for (i
= 0; i
< num_screens
; i
++)
9228 for (j
= 0; j
< workspace_limit
; j
++)
9229 TAILQ_FOREACH(w
, &screens
[i
].ws
[j
].winlist
,
9236 maprequest(xcb_map_request_event_t
*e
)
9238 struct ws_win
*win
, *w
= NULL
;
9239 xcb_get_window_attributes_reply_t
*war
;
9241 DNPRINTF(SWM_D_EVENT
, "maprequest: win %#x\n",
9244 war
= xcb_get_window_attributes_reply(conn
,
9245 xcb_get_window_attributes(conn
, e
->window
),
9248 DNPRINTF(SWM_D_EVENT
, "maprequest: window lost.\n");
9252 if (war
->override_redirect
) {
9253 DNPRINTF(SWM_D_EVENT
, "maprequest: override_redirect; "
9258 win
= manage_window(e
->window
,
9259 (war
->map_state
== XCB_MAP_STATE_VIEWABLE
));
9263 /* The new window should get focus; prepare. */
9264 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
9265 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
) &&
9266 (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
9267 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
9268 win
->hints
.input
))) {
9269 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
9270 /* See if other wins of same type are already mapped. */
9271 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
9272 if (w
== win
|| !w
->mapped
)
9275 if (w
->ch
.class_name
&&
9276 win
->ch
.class_name
&&
9277 strcmp(w
->ch
.class_name
,
9278 win
->ch
.class_name
) == 0 &&
9279 w
->ch
.instance_name
&&
9280 win
->ch
.instance_name
&&
9281 strcmp(w
->ch
.instance_name
,
9282 win
->ch
.instance_name
) == 0)
9288 win
->ws
->focus_pending
= get_focus_magic(win
);
9291 /* All windows need to be mapped if they are in the current workspace.*/
9295 /* Ignore EnterNotify to handle the mapnotify without interference. */
9296 if (focus_mode
== SWM_FOCUS_DEFAULT
)
9297 event_drain(XCB_ENTER_NOTIFY
);
9300 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
9304 motionnotify(xcb_motion_notify_event_t
*e
)
9306 struct swm_region
*r
= NULL
;
9309 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): %#x "
9310 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9311 "child: %#x, same_screen_focus: %s, state: %d\n",
9312 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9313 get_notify_detail_label(e
->detail
), e
->detail
,
9314 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9315 YESNO(e
->same_screen
), e
->state
);
9317 last_event_time
= e
->time
;
9319 if (focus_mode
== SWM_FOCUS_MANUAL
)
9322 num_screens
= get_screen_count();
9323 for (i
= 0; i
< num_screens
; i
++)
9324 if (screens
[i
].root
== e
->root
)
9327 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9328 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
9329 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
9337 get_atom_name(xcb_atom_t atom
)
9340 #ifdef SWM_DEBUG_ATOM_NAMES
9342 * This should be disabled during most debugging since
9343 * xcb_get_* causes an xcb_flush.
9346 xcb_get_atom_name_reply_t
*r
;
9348 r
= xcb_get_atom_name_reply(conn
,
9349 xcb_get_atom_name(conn
, atom
),
9352 len
= xcb_get_atom_name_name_length(r
);
9354 name
= malloc(len
+ 1);
9356 memcpy(name
, xcb_get_atom_name_name(r
), len
);
9370 propertynotify(xcb_property_notify_event_t
*e
)
9373 struct workspace
*ws
;
9377 name
= get_atom_name(e
->atom
);
9378 DNPRINTF(SWM_D_EVENT
, "propertynotify: win %#x, atom: %s(%u), "
9379 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
9383 win
= find_window(e
->window
);
9389 last_event_time
= e
->time
;
9391 if (e
->atom
== a_state
) {
9392 /* State just changed, make sure it gets focused if mapped. */
9393 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
9394 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
9396 ws
->focus_pending
== win
) {
9397 focus_win(ws
->focus_pending
);
9398 ws
->focus_pending
= NULL
;
9402 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
9403 e
->atom
== XCB_ATOM_WM_NAME
) {
9405 } else if (e
->atom
== a_prot
) {
9406 get_wm_protocols(win
);
9413 unmapnotify(xcb_unmap_notify_event_t
*e
)
9416 struct workspace
*ws
;
9418 DNPRINTF(SWM_D_EVENT
, "unmapnotify: win %#x\n", e
->window
);
9420 /* If we aren't managing the window, then ignore. */
9421 win
= find_window(e
->window
);
9422 if (win
== NULL
|| win
->id
!= e
->window
)
9425 /* Do nothing if already withdrawn. */
9426 if (!win
->mapped
&& !ICONIC(win
))
9432 /* If win was focused, make sure to focus on something else. */
9433 if (win
== ws
->focus
) {
9434 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
9435 ws
->focus_pending
= get_focus_prev(win
);
9436 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
9437 "focus_pending: %#x\n",
9438 WINID(ws
->focus_pending
));
9446 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
9449 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
9450 unmanage_window(win
);
9456 /* Update focus if ws is active. */
9457 if (WS_FOCUSED(ws
)) {
9458 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
9459 focus_win(get_pointer_win(ws
->r
->s
->root
));
9460 } else if (ws
->focus_pending
) {
9461 focus_win(ws
->focus_pending
);
9462 ws
->focus_pending
= NULL
;
9463 } else if (ws
->focus
== NULL
) {
9464 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
9465 ws
->r
->id
, XCB_CURRENT_TIME
);
9473 clientmessage(xcb_client_message_event_t
*e
)
9476 struct swm_region
*r
= NULL
;
9479 xcb_map_request_event_t mre
;
9483 name
= get_atom_name(e
->type
);
9484 DNPRINTF(SWM_D_EVENT
, "clientmessage: win %#x, atom: %s(%u)\n",
9485 e
->window
, name
, e
->type
);
9489 if (e
->type
== ewmh
[_NET_CURRENT_DESKTOP
].atom
) {
9490 num_screens
= get_screen_count();
9491 for (i
= 0; i
< num_screens
; i
++)
9492 if (screens
[i
].root
== e
->window
) {
9493 r
= screens
[i
].r_focus
;
9497 if (r
&& e
->data
.data32
[0] <
9498 (uint32_t)workspace_limit
) {
9499 a
.id
= e
->data
.data32
[0];
9507 win
= find_window(e
->window
);
9509 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
9510 /* Manage the window with maprequest. */
9511 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
9512 "unmanaged window.\n");
9513 mre
.window
= e
->window
;
9519 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
9520 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
9521 if (WS_FOCUSED(win
->ws
))
9524 win
->ws
->focus_pending
= win
;
9525 } else if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
9526 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
9527 if (win
->can_delete
)
9528 client_msg(win
, a_delete
, 0);
9530 xcb_kill_client(conn
, win
->id
);
9531 } else if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
9532 DNPRINTF(SWM_D_EVENT
,
9533 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
9535 if (e
->data
.data32
[0] & (1<<8)) /* x */
9536 X(win
) = e
->data
.data32
[1];
9537 if (e
->data
.data32
[0] & (1<<9)) /* y */
9538 Y(win
) = e
->data
.data32
[2];
9539 if (e
->data
.data32
[0] & (1<<10)) /* width */
9540 WIDTH(win
) = e
->data
.data32
[3];
9541 if (e
->data
.data32
[0] & (1<<11)) /* height */
9542 HEIGHT(win
) = e
->data
.data32
[4];
9547 /* TODO: Change stack sizes */
9548 /* notify no change was made. */
9549 config_win(win
, NULL
);
9551 } else if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
9552 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
9553 ewmh_change_wm_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
9554 if (e
->data
.data32
[2])
9555 ewmh_change_wm_state(win
, e
->data
.data32
[2],
9558 ewmh_update_wm_state(win
);
9568 int errcode
= xcb_connection_has_error(conn
);
9569 #ifdef XCB_CONN_ERROR
9572 case XCB_CONN_ERROR
:
9573 s
= "Socket error, pipe error or other stream error.";
9575 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
9576 s
= "Extension not supported.";
9578 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
9579 s
= "Insufficient memory.";
9581 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
9582 s
= "Request length was exceeded.";
9584 case XCB_CONN_CLOSED_PARSE_ERR
:
9585 s
= "Error parsing display string.";
9588 s
= "Unknown error.";
9591 errx(errcode
, "X CONNECTION ERROR: %s", s
);
9594 errx(errcode
, "X CONNECTION ERROR");
9602 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
9603 XCB_EVENT_MASK_ENTER_WINDOW
;
9605 xcb_void_cookie_t wac
;
9606 xcb_generic_error_t
*error
;
9608 /* this causes an error if some other window manager is running */
9609 num_screens
= get_screen_count();
9610 for (i
= 0; i
< num_screens
; i
++) {
9611 if ((sc
= get_screen(i
)) == NULL
)
9612 errx(1, "ERROR: can't get screen %d.", i
);
9613 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: %#x\n",
9615 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
9616 XCB_CW_EVENT_MASK
, &val
);
9617 if ((error
= xcb_request_check(conn
, wac
))) {
9618 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
9624 /* click to focus on empty region */
9625 xcb_grab_button(conn
, 1, sc
->root
, BUTTONMASK
,
9626 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
9627 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_1
, XCB_BUTTON_MASK_ANY
);
9634 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
9636 struct swm_region
*r
= NULL
, *n
;
9637 struct workspace
*ws
= NULL
;
9641 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
9642 s
->idx
, w
, h
, x
, y
);
9644 /* remove any conflicting regions */
9645 n
= TAILQ_FIRST(&s
->rl
);
9648 n
= TAILQ_NEXT(r
, entry
);
9649 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
9650 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
9651 if (r
->ws
->r
!= NULL
)
9652 r
->ws
->old_r
= r
->ws
->r
;
9655 xcb_destroy_window(conn
, r
->id
);
9656 TAILQ_REMOVE(&s
->rl
, r
, entry
);
9657 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
9661 /* search old regions for one to reuse */
9663 /* size + location match */
9664 TAILQ_FOREACH(r
, &s
->orl
, entry
)
9665 if (X(r
) == x
&& Y(r
) == y
&&
9666 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
9670 TAILQ_FOREACH(r
, &s
->orl
, entry
)
9671 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
9675 TAILQ_REMOVE(&s
->orl
, r
, entry
);
9676 /* try to use old region's workspace */
9677 if (r
->ws
->r
== NULL
)
9680 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
9681 err(1, "new_region: calloc: failed to allocate memory "
9684 /* if we don't have a workspace already, find one */
9686 for (i
= 0; i
< workspace_limit
; i
++)
9687 if (s
->ws
[i
].r
== NULL
) {
9694 errx(1, "new_region: no free workspaces");
9696 if (ws
->state
== SWM_WS_STATE_HIDDEN
)
9697 ws
->state
= SWM_WS_STATE_MAPPING
;
9708 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
9710 /* Invisible region window to detect pointer events on empty regions. */
9711 r
->id
= xcb_generate_id(conn
);
9712 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
9713 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
9715 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
9716 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
9717 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
9719 /* Make sure region input is at the bottom. */
9720 wa
[0] = XCB_STACK_MODE_BELOW
;
9721 xcb_configure_window(conn
, r
->id
, XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
9723 xcb_map_window(conn
, r
->id
);
9729 #ifdef SWM_XRR_HAS_CRTC
9732 #endif /* SWM_XRR_HAS_CRTC */
9733 struct swm_region
*r
;
9736 xcb_randr_get_screen_resources_current_cookie_t src
;
9737 xcb_randr_get_screen_resources_current_reply_t
*srr
;
9738 xcb_randr_get_crtc_info_cookie_t cic
;
9739 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
9740 xcb_randr_crtc_t
*crtc
;
9741 xcb_screen_t
*screen
;
9743 DNPRINTF(SWM_D_MISC
, "scan_xrandr: screen: %d\n", i
);
9745 if ((screen
= get_screen(i
)) == NULL
)
9746 errx(1, "ERROR: can't get screen %d.", i
);
9748 num_screens
= get_screen_count();
9749 if (i
>= num_screens
)
9750 errx(1, "scan_xrandr: invalid screen");
9752 /* remove any old regions */
9753 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
9754 r
->ws
->old_r
= r
->ws
->r
= NULL
;
9756 xcb_destroy_window(conn
, r
->id
);
9757 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
9758 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
9762 /* map virtual screens onto physical screens */
9763 #ifdef SWM_XRR_HAS_CRTC
9764 if (xrandr_support
) {
9765 src
= xcb_randr_get_screen_resources_current(conn
,
9767 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
9770 new_region(&screens
[i
], 0, 0,
9771 screen
->width_in_pixels
,
9772 screen
->height_in_pixels
);
9775 ncrtc
= srr
->num_crtcs
;
9777 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
9778 for (c
= 0; c
< ncrtc
; c
++) {
9779 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
9781 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
9784 if (cir
->num_outputs
== 0) {
9790 new_region(&screens
[i
], 0, 0,
9791 screen
->width_in_pixels
,
9792 screen
->height_in_pixels
);
9794 new_region(&screens
[i
],
9795 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
9800 #endif /* SWM_XRR_HAS_CRTC */
9802 /* If detection failed, create a single region that spans the screen. */
9803 if (TAILQ_EMPTY(&screens
[i
].rl
))
9804 new_region(&screens
[i
], 0, 0, screen
->width_in_pixels
,
9805 screen
->height_in_pixels
);
9808 /* Cleanup unused previously visible workspaces. */
9809 TAILQ_FOREACH(r
, &screens
[i
].orl
, entry
) {
9810 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
9812 r
->ws
->state
= SWM_WS_STATE_HIDDEN
;
9814 /* The screen shouldn't focus on an unused region. */
9815 if (screens
[i
].r_focus
== r
)
9816 screens
[i
].r_focus
= NULL
;
9819 DNPRINTF(SWM_D_MISC
, "scan_xrandr: done.\n");
9823 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
9825 struct swm_region
*r
;
9828 DNPRINTF(SWM_D_EVENT
, "screenchange: root: %#x\n", e
->root
);
9830 num_screens
= get_screen_count();
9831 /* silly event doesn't include the screen index */
9832 for (i
= 0; i
< num_screens
; i
++)
9833 if (screens
[i
].root
== e
->root
)
9835 if (i
>= num_screens
)
9836 errx(1, "screenchange: screen not found");
9838 /* brute force for now, just re-enumerate the regions */
9842 print_win_geom(e
->root
);
9844 /* add bars to all regions */
9845 for (i
= 0; i
< num_screens
; i
++) {
9846 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9852 /* Make sure a region has focus on each screen. */
9853 for (i
= 0; i
< num_screens
; i
++) {
9854 if (screens
[i
].r_focus
== NULL
) {
9855 r
= TAILQ_FIRST(&screens
[i
].rl
);
9864 /* Update workspace state on all regions. */
9865 for (i
= 0; i
< num_screens
; i
++)
9866 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9867 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
9873 struct swm_region
*r
= NULL
;
9874 xcb_window_t
*wins
= NULL
, trans
, *cwins
= NULL
;
9875 int i
, j
, k
, n
, no
, num_screens
, manage
, mapped
;
9878 xcb_query_tree_cookie_t qtc
;
9879 xcb_query_tree_reply_t
*qtr
;
9880 xcb_get_window_attributes_cookie_t gac
;
9881 xcb_get_window_attributes_reply_t
*gar
;
9882 xcb_get_property_cookie_t pc
;
9883 xcb_get_property_reply_t
*pr
;
9885 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
9886 num_screens
= get_screen_count();
9887 for (i
= 0; i
< num_screens
; i
++) {
9888 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
9889 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
9892 wins
= xcb_query_tree_children(qtr
);
9893 no
= xcb_query_tree_children_length(qtr
);
9895 /* Try to sort windows according to _NET_CLIENT_LIST. */
9896 pr
= xcb_get_property_reply(conn
, xcb_get_property(conn
, 0,
9897 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
9898 XCB_ATOM_WINDOW
, 0, UINT32_MAX
), NULL
);
9900 cwins
= xcb_get_property_value(pr
);
9901 n
= xcb_get_property_value_length(pr
) /
9904 for (j
= 0; j
< n
; ++j
) {
9905 for (k
= j
; k
< no
; ++k
) {
9906 if (wins
[k
] == cwins
[j
]) {
9907 /* Swap wins j and k. */
9917 /* attach windows to a region */
9918 /* normal windows */
9919 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top level windows.\n");
9920 for (j
= 0; j
< no
; j
++) {
9921 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
9922 if (r
->id
== wins
[j
]) {
9923 DNPRINTF(SWM_D_INIT
, "grab_windows: "
9924 "skip %#x; region input window.\n",
9933 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
9934 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
9936 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
9937 "doesn't exist.\n", wins
[j
]);
9941 if (gar
->override_redirect
) {
9942 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
9943 "override_redirect set.\n", wins
[j
]);
9948 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
9949 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
9951 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
9952 "is transient for %#x.\n", wins
[j
], trans
);
9957 state
= getstate(wins
[j
]);
9958 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
9959 mapped
= gar
->map_state
== XCB_MAP_STATE_VIEWABLE
;
9960 if (mapped
|| manage
)
9961 manage_window(wins
[j
], mapped
);
9964 /* transient windows */
9965 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
9966 for (j
= 0; j
< no
; j
++) {
9967 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
9968 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
9970 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
9971 "doesn't exist.\n", wins
[j
]);
9975 if (gar
->override_redirect
) {
9976 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
9977 "override_redirect set.\n", wins
[j
]);
9982 state
= getstate(wins
[j
]);
9983 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
9984 mapped
= gar
->map_state
== XCB_MAP_STATE_VIEWABLE
;
9985 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
9986 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
9987 &trans
, NULL
) && manage
)
9988 manage_window(wins
[j
], mapped
);
9993 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
9999 int i
, j
, k
, num_screens
;
10000 struct workspace
*ws
;
10001 uint32_t gcv
[1], wa
[1];
10002 const xcb_query_extension_reply_t
*qep
;
10003 xcb_screen_t
*screen
;
10004 xcb_randr_query_version_cookie_t c
;
10005 xcb_randr_query_version_reply_t
*r
;
10007 num_screens
= get_screen_count();
10008 if ((screens
= calloc(num_screens
,
10009 sizeof(struct swm_screen
))) == NULL
)
10010 err(1, "setup_screens: calloc: failed to allocate memory for "
10013 /* initial Xrandr setup */
10014 xrandr_support
= 0;
10015 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
10016 if (qep
->present
) {
10017 c
= xcb_randr_query_version(conn
, 1, 1);
10018 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
10020 if (r
->major_version
>= 1) {
10021 xrandr_support
= 1;
10022 xrandr_eventbase
= qep
->first_event
;
10028 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
10030 /* map physical screens */
10031 for (i
= 0; i
< num_screens
; i
++) {
10032 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
10033 screens
[i
].idx
= i
;
10034 screens
[i
].r_focus
= NULL
;
10036 TAILQ_INIT(&screens
[i
].rl
);
10037 TAILQ_INIT(&screens
[i
].orl
);
10038 if ((screen
= get_screen(i
)) == NULL
)
10039 errx(1, "ERROR: can't get screen %d.", i
);
10040 screens
[i
].root
= screen
->root
;
10042 /* set default colors */
10043 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
10044 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
10045 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
10046 setscreencolor("rgb:00/40/40", i
+ 1,
10047 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
10048 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
10049 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
10051 /* create graphics context on screen */
10052 screens
[i
].bar_gc
= xcb_generate_id(conn
);
10054 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
10055 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
10057 /* set default cursor */
10058 xcb_change_window_attributes(conn
, screens
[i
].root
,
10059 XCB_CW_CURSOR
, wa
);
10061 /* init all workspaces */
10062 /* XXX these should be dynamically allocated too */
10063 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
10064 ws
= &screens
[i
].ws
[j
];
10067 ws
->bar_enabled
= 1;
10069 ws
->focus_prev
= NULL
;
10070 ws
->focus_pending
= NULL
;
10073 ws
->state
= SWM_WS_STATE_HIDDEN
;
10074 TAILQ_INIT(&ws
->stack
);
10075 TAILQ_INIT(&ws
->winlist
);
10076 TAILQ_INIT(&ws
->unmanagedlist
);
10078 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
10079 if (layouts
[k
].l_config
!= NULL
)
10080 layouts
[k
].l_config(ws
,
10081 SWM_ARG_ID_STACKINIT
);
10082 ws
->cur_layout
= &layouts
[0];
10083 ws
->cur_layout
->l_string(ws
);
10088 if (xrandr_support
)
10089 xcb_randr_select_input(conn
, screens
[i
].root
,
10090 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
10095 setup_globals(void)
10097 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
10098 err(1, "setup_globals: strdup: failed to allocate memory.");
10100 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
10101 err(1, "setup_globals: strdup: failed to allocate memory.");
10103 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
10104 errx(1, "unable to allocate key symbols");
10106 a_state
= get_atom_from_string("WM_STATE");
10107 a_prot
= get_atom_from_string("WM_PROTOCOLS");
10108 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
10109 a_net_supported
= get_atom_from_string("_NET_SUPPORTED");
10110 a_net_wm_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
10111 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
10112 a_utf8_string
= get_atom_from_string("UTF8_STRING");
10113 a_swm_ws
= get_atom_from_string("_SWM_WS");
10119 int i
, num_screens
;
10120 xcb_window_t root
, win
;
10122 /* work around sun jdk bugs, code from wmname */
10124 num_screens
= get_screen_count();
10125 for (i
= 0; i
< num_screens
; i
++) {
10126 root
= screens
[i
].root
;
10128 win
= xcb_generate_id(conn
);
10129 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
10130 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
10131 XCB_COPY_FROM_PARENT
, 0, NULL
);
10133 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
10134 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
10135 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
10136 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
10137 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
10138 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
, 8, strlen("LG3D"),
10144 shutdown_cleanup(void)
10146 int i
, num_screens
;
10148 /* disable alarm because the following code may not be interrupted */
10150 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
10151 err(1, "can't disable alarm");
10160 num_screens
= get_screen_count();
10161 for (i
= 0; i
< num_screens
; ++i
) {
10162 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
10163 screens
[i
].root
, XCB_CURRENT_TIME
);
10165 if (screens
[i
].bar_gc
!= XCB_NONE
)
10166 xcb_free_gc(conn
, screens
[i
].bar_gc
);
10167 if (!bar_font_legacy
)
10168 XftColorFree(display
, DefaultVisual(display
, i
),
10169 DefaultColormap(display
, i
), &bar_font_color
);
10172 if (bar_font_legacy
)
10173 XFreeFontSet(display
, bar_fs
);
10175 XftFontClose(display
, bar_font
);
10178 xcb_key_symbols_free(syms
);
10180 xcb_disconnect(conn
);
10184 event_error(xcb_generic_error_t
*e
)
10188 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
10189 "resource_id: %u, minor_code: %u\n",
10190 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
10191 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
10192 e
->sequence
, e
->resource_id
, e
->minor_code
);
10196 event_handle(xcb_generic_event_t
*evt
)
10198 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
10200 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
10201 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
10202 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
10205 #define EVENT(type, callback) case type: callback((void *)evt); return
10206 EVENT(0, event_error
);
10207 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
10208 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
10209 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
10210 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
10211 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
10212 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
10213 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
10214 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
10215 /*EVENT(XCB_CREATE_NOTIFY, );*/
10216 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
10217 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
10218 EVENT(XCB_EXPOSE
, expose
);
10220 EVENT(XCB_FOCUS_IN
, focusin
);
10221 EVENT(XCB_FOCUS_OUT
, focusout
);
10223 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
10224 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
10225 EVENT(XCB_KEY_PRESS
, keypress
);
10226 /*EVENT(XCB_KEY_RELEASE, keypress);*/
10227 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
10229 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
10231 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
10232 EVENT(XCB_MAP_REQUEST
, maprequest
);
10233 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
10234 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
10235 /*EVENT(XCB_NO_EXPOSURE, );*/
10236 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
10237 /*EVENT(XCB_REPARENT_NOTIFY, );*/
10238 /*EVENT(XCB_RESIZE_REQUEST, );*/
10239 /*EVENT(XCB_SELECTION_CLEAR, );*/
10240 /*EVENT(XCB_SELECTION_NOTIFY, );*/
10241 /*EVENT(XCB_SELECTION_REQUEST, );*/
10242 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
10243 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
10246 if (type
- xrandr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
10247 screenchange((void *)evt
);
10251 main(int argc
, char *argv
[])
10253 struct swm_region
*r
;
10254 char conf
[PATH_MAX
], *cfile
= NULL
;
10256 int xfd
, i
, num_screens
, startup
= 1;
10257 struct sigaction sact
;
10258 xcb_generic_event_t
*evt
;
10262 int stdin_ready
= 0;
10265 /* suppress unused warning since var is needed */
10268 time_started
= time(NULL
);
10271 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
10272 if (setlocale(LC_CTYPE
, "") == NULL
|| setlocale(LC_TIME
, "") == NULL
)
10273 warnx("no locale support");
10275 /* handle some signals */
10276 bzero(&sact
, sizeof(sact
));
10277 sigemptyset(&sact
.sa_mask
);
10279 sact
.sa_handler
= sighdlr
;
10280 sigaction(SIGINT
, &sact
, NULL
);
10281 sigaction(SIGQUIT
, &sact
, NULL
);
10282 sigaction(SIGTERM
, &sact
, NULL
);
10283 sigaction(SIGHUP
, &sact
, NULL
);
10285 sact
.sa_handler
= sighdlr
;
10286 sact
.sa_flags
= SA_NOCLDSTOP
;
10287 sigaction(SIGCHLD
, &sact
, NULL
);
10289 if ((display
= XOpenDisplay(0)) == NULL
)
10290 errx(1, "can not open display");
10292 conn
= XGetXCBConnection(display
);
10293 if (xcb_connection_has_error(conn
))
10294 errx(1, "can not get XCB connection");
10296 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
10298 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
10299 xfd
= xcb_get_file_descriptor(conn
);
10301 /* look for local and global conf file */
10302 pwd
= getpwuid(getuid());
10304 errx(1, "invalid user: %d", getuid());
10306 xcb_grab_server(conn
);
10307 xcb_aux_sync(conn
);
10309 /* flush all events */
10310 while ((evt
= xcb_poll_for_event(conn
))) {
10311 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
10317 errx(1, "another window manager is currently running");
10319 /* Load Xcursors and/or cursorfont glyph cursors. */
10322 xcb_aux_sync(conn
);
10332 for (i
= 0; ; i
++) {
10337 snprintf(conf
, sizeof conf
, "%s/.%s",
10338 pwd
->pw_dir
, SWM_CONF_FILE
);
10342 snprintf(conf
, sizeof conf
, "/etc/%s",
10347 snprintf(conf
, sizeof conf
, "%s/.%s",
10348 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
10351 /* global compat */
10352 snprintf(conf
, sizeof conf
, "/etc/%s",
10353 SWM_CONF_FILE_OLD
);
10359 if (strlen(conf
) && stat(conf
, &sb
) != -1)
10360 if (S_ISREG(sb
.st_mode
)) {
10367 /* load conf (if any) */
10369 conf_load(cfile
, SWM_CONF_DEFAULT
);
10373 /* set some values to work around bad programs */
10375 /* grab existing windows (before we build the bars) */
10378 if (getenv("SWM_STARTED") == NULL
)
10379 setenv("SWM_STARTED", "YES", 1);
10381 /* setup all bars */
10382 num_screens
= get_screen_count();
10383 for (i
= 0; i
< num_screens
; i
++)
10384 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10391 xcb_ungrab_server(conn
);
10394 /* Update state of each newly mapped workspace. */
10395 for (i
= 0; i
< num_screens
; i
++)
10396 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10397 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
10399 rd_max
= xfd
> STDIN_FILENO
? xfd
: STDIN_FILENO
;
10402 while ((evt
= xcb_poll_for_event(conn
))) {
10409 /* If just (re)started, set default focus if needed. */
10413 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10414 r
= TAILQ_FIRST(&screens
[0].rl
);
10426 FD_SET(STDIN_FILENO
, &rd
);
10431 num_readable
= select(rd_max
+ 1, &rd
, NULL
, NULL
, &tv
);
10432 if (num_readable
== -1 && errno
!= EINTR
) {
10433 DNPRINTF(SWM_D_MISC
, "select failed");
10434 } else if (num_readable
> 0 && FD_ISSET(STDIN_FILENO
, &rd
)) {
10439 restart(NULL
, NULL
);
10449 bar_extra_update();
10456 shutdown_cleanup();