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 java_workaround
= 1;
419 int verbose_layout
= 0;
423 XFontSetExtents
*bar_fs_extents
;
425 int bar_font_legacy
= 1;
427 XftColor bar_font_color
;
429 char *startup_exception
;
430 unsigned int nr_exceptions
= 0;
432 /* layout manager data */
433 struct swm_geometry
{
446 struct swm_geometry g
;
449 /* virtual "screens" */
451 TAILQ_ENTRY(swm_region
) entry
;
453 struct swm_geometry g
;
454 struct workspace
*ws
; /* current workspace on this region */
455 struct workspace
*ws_prior
; /* prior workspace on this region */
456 struct swm_screen
*s
; /* screen idx */
459 TAILQ_HEAD(swm_region_list
, swm_region
);
462 TAILQ_ENTRY(ws_win
) entry
;
463 TAILQ_ENTRY(ws_win
) stack_entry
;
465 xcb_window_t transient
;
466 struct ws_win
*focus_child
; /* focus on child transient */
467 struct swm_geometry g
; /* current geometry */
468 struct swm_geometry g_float
; /* region coordinates */
469 int g_floatvalid
; /* g_float geometry validity */
473 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
479 unsigned long quirks
;
480 struct workspace
*ws
; /* always valid */
481 struct swm_screen
*s
; /* always valid, never changes */
483 xcb_icccm_get_wm_class_reply_t ch
;
484 xcb_icccm_wm_hints_t hints
;
486 TAILQ_HEAD(ws_win_list
, ws_win
);
487 TAILQ_HEAD(ws_win_stack
, ws_win
);
491 TAILQ_ENTRY(pid_e
) entry
;
495 TAILQ_HEAD(pid_list
, pid_e
);
496 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
498 /* layout handlers */
500 void vertical_config(struct workspace
*, int);
501 void vertical_stack(struct workspace
*, struct swm_geometry
*);
502 void horizontal_config(struct workspace
*, int);
503 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
504 void max_stack(struct workspace
*, struct swm_geometry
*);
505 void plain_stacker(struct workspace
*);
506 void fancy_stacker(struct workspace
*);
509 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
510 void (*l_config
)(struct workspace
*, int);
512 #define SWM_L_FOCUSPREV (1<<0)
513 #define SWM_L_MAPONFOCUS (1<<1)
514 void (*l_string
)(struct workspace
*);
516 /* stack, configure */
517 { vertical_stack
, vertical_config
, 0, plain_stacker
},
518 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
520 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
521 { NULL
, NULL
, 0, NULL
},
524 /* position of max_stack mode in the layouts array, index into layouts! */
525 #define SWM_V_STACK (0)
526 #define SWM_H_STACK (1)
527 #define SWM_MAX_STACK (2)
529 #define SWM_H_SLICE (32)
530 #define SWM_V_SLICE (32)
532 /* define work spaces */
534 int idx
; /* workspace index */
535 char *name
; /* workspace name */
536 int always_raise
; /* raise windows on focus */
537 int bar_enabled
; /* bar visibility */
538 struct layout
*cur_layout
; /* current layout handlers */
539 struct ws_win
*focus
; /* may be NULL */
540 struct ws_win
*focus_prev
; /* may be NULL */
541 struct ws_win
*focus_pending
; /* may be NULL */
542 struct swm_region
*r
; /* may be NULL */
543 struct swm_region
*old_r
; /* may be NULL */
544 struct ws_win_list winlist
; /* list of windows in ws */
545 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
546 struct ws_win_stack stack
; /* stacking order */
547 int32_t state
; /* mapping state */
548 char stacker
[10]; /* display stacker and layout */
552 int horizontal_msize
;
554 int horizontal_stacks
;
565 SWM_WS_STATE_MAPPING
,
571 SWM_S_COLOR_BAR_BORDER
,
572 SWM_S_COLOR_BAR_BORDER_UNFOCUS
,
573 SWM_S_COLOR_BAR_FONT
,
579 /* physical screen mapping */
580 #define SWM_WS_MAX (22) /* hard limit */
581 int workspace_limit
= 10; /* soft limit */
584 int idx
; /* screen index */
585 struct swm_region_list rl
; /* list of regions on this screen */
586 struct swm_region_list orl
; /* list of old regions */
588 struct workspace ws
[SWM_WS_MAX
];
589 struct swm_region
*r_focus
;
595 } c
[SWM_S_COLOR_MAX
];
597 xcb_gcontext_t bar_gc
;
600 struct swm_screen
*screens
;
602 /* args to functions */
605 #define SWM_ARG_ID_FOCUSNEXT (0)
606 #define SWM_ARG_ID_FOCUSPREV (1)
607 #define SWM_ARG_ID_FOCUSMAIN (2)
608 #define SWM_ARG_ID_FOCUSURGENT (3)
609 #define SWM_ARG_ID_SWAPNEXT (10)
610 #define SWM_ARG_ID_SWAPPREV (11)
611 #define SWM_ARG_ID_SWAPMAIN (12)
612 #define SWM_ARG_ID_MOVELAST (13)
613 #define SWM_ARG_ID_MASTERSHRINK (20)
614 #define SWM_ARG_ID_MASTERGROW (21)
615 #define SWM_ARG_ID_MASTERADD (22)
616 #define SWM_ARG_ID_MASTERDEL (23)
617 #define SWM_ARG_ID_FLIPLAYOUT (24)
618 #define SWM_ARG_ID_STACKRESET (30)
619 #define SWM_ARG_ID_STACKINIT (31)
620 #define SWM_ARG_ID_CYCLEWS_UP (40)
621 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
622 #define SWM_ARG_ID_CYCLERG_UP (42)
623 #define SWM_ARG_ID_CYCLERG_DOWN (43)
624 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
625 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
626 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
627 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
628 #define SWM_ARG_ID_STACKINC (50)
629 #define SWM_ARG_ID_STACKDEC (51)
630 #define SWM_ARG_ID_SS_ALL (60)
631 #define SWM_ARG_ID_SS_WINDOW (61)
632 #define SWM_ARG_ID_DONTCENTER (70)
633 #define SWM_ARG_ID_CENTER (71)
634 #define SWM_ARG_ID_KILLWINDOW (80)
635 #define SWM_ARG_ID_DELETEWINDOW (81)
636 #define SWM_ARG_ID_WIDTHGROW (90)
637 #define SWM_ARG_ID_WIDTHSHRINK (91)
638 #define SWM_ARG_ID_HEIGHTGROW (92)
639 #define SWM_ARG_ID_HEIGHTSHRINK (93)
640 #define SWM_ARG_ID_MOVEUP (100)
641 #define SWM_ARG_ID_MOVEDOWN (101)
642 #define SWM_ARG_ID_MOVELEFT (102)
643 #define SWM_ARG_ID_MOVERIGHT (103)
644 #define SWM_ARG_ID_BAR_TOGGLE (110)
645 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
651 TAILQ_ENTRY(quirk
) entry
;
652 char *class; /* WM_CLASS:class */
653 char *instance
; /* WM_CLASS:instance */
654 char *name
; /* WM_NAME */
656 regex_t regex_instance
;
659 #define SWM_Q_FLOAT (1<<0) /* float this window */
660 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
661 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
662 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
663 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
664 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
665 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
666 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
668 TAILQ_HEAD(quirk_list
, quirk
);
669 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
672 * Supported EWMH hints should be added to
673 * both the enum and the ewmh array
679 _NET_CURRENT_DESKTOP
,
681 _NET_MOVERESIZE_WINDOW
,
682 _NET_NUMBER_OF_DESKTOPS
,
684 _NET_WM_ACTION_ABOVE
,
685 _NET_WM_ACTION_CLOSE
,
686 _NET_WM_ACTION_FULLSCREEN
,
688 _NET_WM_ACTION_RESIZE
,
689 _NET_WM_ALLOWED_ACTIONS
,
691 _NET_WM_FULL_PLACEMENT
,
695 _NET_WM_STATE_FULLSCREEN
,
696 _NET_WM_STATE_HIDDEN
,
697 _NET_WM_STATE_MAXIMIZED_VERT
,
698 _NET_WM_STATE_MAXIMIZED_HORZ
,
699 _NET_WM_STATE_SKIP_PAGER
,
700 _NET_WM_STATE_SKIP_TASKBAR
,
702 _NET_WM_WINDOW_TYPE_DIALOG
,
703 _NET_WM_WINDOW_TYPE_DOCK
,
704 _NET_WM_WINDOW_TYPE_NORMAL
,
705 _NET_WM_WINDOW_TYPE_SPLASH
,
706 _NET_WM_WINDOW_TYPE_TOOLBAR
,
707 _NET_WM_WINDOW_TYPE_UTILITY
,
708 _SWM_WM_STATE_MANUAL
,
715 } ewmh
[SWM_EWMH_HINT_MAX
] = {
716 /* must be in same order as in the enum */
717 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
718 {"_NET_CLIENT_LIST", XCB_ATOM_NONE
},
719 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
720 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE
},
721 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE
},
722 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
723 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE
},
724 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE
},
725 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
726 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
727 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
728 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
729 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
730 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
731 {"_NET_WM_DESKTOP", XCB_ATOM_NONE
},
732 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE
},
733 {"_NET_WM_NAME", XCB_ATOM_NONE
},
734 {"_NET_WM_STATE", XCB_ATOM_NONE
},
735 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
736 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
737 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
738 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
739 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
740 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
741 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
742 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
743 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
744 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
745 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
746 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
747 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
748 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
749 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
752 /* EWMH source type */
754 EWMH_SOURCE_TYPE_NONE
= 0,
755 EWMH_SOURCE_TYPE_NORMAL
= 1,
756 EWMH_SOURCE_TYPE_OTHER
= 2,
763 XC_BOTTOM_LEFT_CORNER
,
764 XC_BOTTOM_RIGHT_CORNER
,
772 char *name
; /* Name used by Xcursor .*/
773 uint8_t cf_char
; /* cursorfont index. */
775 } cursors
[XC_MAX
] = {
776 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
777 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
778 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
779 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
780 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
781 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
782 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
785 #define SWM_SPAWN_OPTIONAL 0x1
789 TAILQ_ENTRY(spawn_prog
) entry
;
795 TAILQ_HEAD(spawn_list
, spawn_prog
);
796 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
798 /* user/key callable function IDs */
914 KF_DUMPWINS
, /* MUST BE LAST */
922 enum keyfuncid funcid
;
925 RB_HEAD(key_tree
, key
);
927 /* function prototypes */
928 void adjust_font(struct ws_win
*);
929 char *argsep(char **);
930 void bar_cleanup(struct swm_region
*);
931 void bar_extra_setup(void);
932 void bar_extra_stop(void);
933 int bar_extra_update(void);
934 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
935 void bar_fmt_expand(char *, size_t);
937 void bar_print(struct swm_region
*, const char *);
938 void bar_print_legacy(struct swm_region
*, const char *);
939 void bar_replace(char *, char *, struct swm_region
*, size_t);
940 void bar_replace_pad(char *, int *, size_t);
941 char *bar_replace_seq(char *, char *, struct swm_region
*, size_t *, size_t);
942 void bar_setup(struct swm_region
*);
943 void bar_toggle(struct swm_region
*, union arg
*);
944 void bar_urgent(char *, size_t);
945 void bar_window_class(char *, size_t, struct swm_region
*);
946 void bar_window_class_instance(char *, size_t, struct swm_region
*);
947 void bar_window_float(char *, size_t, struct swm_region
*);
948 void bar_window_instance(char *, size_t, struct swm_region
*);
949 void bar_window_name(char *, size_t, struct swm_region
*);
950 void bar_window_state(char *, size_t, struct swm_region
*);
951 void bar_workspace_name(char *, size_t, struct swm_region
*);
952 void buttonpress(xcb_button_press_event_t
*);
953 void check_conn(void);
954 void clear_keys(void);
955 int clear_maximized(struct workspace
*);
956 void clientmessage(xcb_client_message_event_t
*);
957 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
958 int conf_load(const char *, int);
959 void configurenotify(xcb_configure_notify_event_t
*);
960 void configurerequest(xcb_configure_request_event_t
*);
961 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
962 void constrain_window(struct ws_win
*, struct swm_geometry
*, int *);
963 int count_win(struct workspace
*, int);
964 void cursors_cleanup(void);
965 void cursors_load(void);
966 void custom_region(const char *);
967 void cyclerg(struct swm_region
*, union arg
*);
968 void cyclews(struct swm_region
*, union arg
*);
969 void cycle_layout(struct swm_region
*, union arg
*);
970 void destroynotify(xcb_destroy_notify_event_t
*);
971 void dumpwins(struct swm_region
*, union arg
*);
973 void enternotify(xcb_enter_notify_event_t
*);
974 void event_drain(uint8_t);
975 void event_error(xcb_generic_error_t
*);
976 void event_handle(xcb_generic_event_t
*);
977 void ewmh_apply_flags(struct ws_win
*, uint32_t);
978 void ewmh_autoquirk(struct ws_win
*);
979 void ewmh_get_desktop_names(void);
980 void ewmh_get_wm_state(struct ws_win
*);
981 void ewmh_update_actions(struct ws_win
*);
982 void ewmh_update_client_list(void);
983 void ewmh_update_current_desktop(void);
984 void ewmh_update_desktop_names(void);
985 void ewmh_change_wm_state(struct ws_win
*, xcb_atom_t
, long);
986 void ewmh_update_wm_state(struct ws_win
*);
987 char *expand_tilde(const char *);
988 void expose(xcb_expose_event_t
*);
989 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
990 struct pid_e
*find_pid(pid_t
);
991 struct ws_win
*find_unmanaged_window(xcb_window_t
);
992 struct ws_win
*find_window(xcb_window_t
);
993 void floating_toggle(struct swm_region
*, union arg
*);
994 void focus(struct swm_region
*, union arg
*);
996 void focusin(xcb_focus_in_event_t
*);
997 void focusout(xcb_focus_out_event_t
*);
999 void focus_flush(void);
1000 void focus_region(struct swm_region
*);
1001 void focusrg(struct swm_region
*, union arg
*);
1002 void focus_win(struct ws_win
*);
1003 void fontset_init(void);
1004 void free_window(struct ws_win
*);
1005 xcb_atom_t
get_atom_from_string(const char *);
1007 char *get_atom_name(xcb_atom_t
);
1009 struct ws_win
*get_focus_magic(struct ws_win
*);
1010 struct ws_win
*get_focus_prev(struct ws_win
*);
1012 char *get_notify_detail_label(uint8_t);
1013 char *get_notify_mode_label(uint8_t);
1015 struct ws_win
*get_pointer_win(xcb_window_t
);
1016 struct ws_win
*get_region_focus(struct swm_region
*);
1017 int get_region_index(struct swm_region
*);
1018 xcb_screen_t
*get_screen(int);
1019 int get_screen_count(void);
1021 char *get_stack_mode_name(uint8_t);
1023 int32_t get_swm_ws(xcb_window_t
);
1024 char *get_win_name(xcb_window_t
);
1025 void get_wm_protocols(struct ws_win
*);
1026 int get_ws_idx(xcb_window_t
);
1027 uint32_t getstate(xcb_window_t
);
1028 void grabbuttons(struct ws_win
*);
1029 void grabkeys(void);
1030 void grab_windows(void);
1031 void iconify(struct swm_region
*, union arg
*);
1033 void keypress(xcb_key_press_event_t
*);
1034 int key_cmp(struct key
*, struct key
*);
1035 void key_insert(unsigned int, KeySym
, enum keyfuncid
, const char *);
1036 struct key
*key_lookup(unsigned int, KeySym
);
1037 void key_remove(struct key
*);
1038 void key_replace(struct key
*, unsigned int, KeySym
, enum keyfuncid
,
1040 void kill_bar_extra_atexit(void);
1041 void kill_refs(struct ws_win
*);
1043 void leavenotify(xcb_leave_notify_event_t
*);
1045 void load_float_geom(struct ws_win
*);
1046 struct ws_win
*manage_window(xcb_window_t
, uint16_t);
1047 void map_window(struct ws_win
*);
1048 void mapnotify(xcb_map_notify_event_t
*);
1049 void mappingnotify(xcb_mapping_notify_event_t
*);
1050 void maprequest(xcb_map_request_event_t
*);
1051 void maximize_toggle(struct swm_region
*, union arg
*);
1052 void motionnotify(xcb_motion_notify_event_t
*);
1053 void move(struct ws_win
*, union arg
*);
1054 void move_step(struct swm_region
*, union arg
*);
1055 uint32_t name_to_pixel(int, const char *);
1056 void name_workspace(struct swm_region
*, union arg
*);
1057 void new_region(struct swm_screen
*, int, int, int, int);
1058 int parsekeys(const char *, unsigned int, unsigned int *, KeySym
*);
1059 int parsequirks(const char *, unsigned long *);
1060 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1061 void pressbutton(struct swm_region
*, union arg
*);
1062 void priorws(struct swm_region
*, union arg
*);
1064 void print_win_geom(xcb_window_t
);
1066 void propertynotify(xcb_property_notify_event_t
*);
1067 void quirk_free(struct quirk
*);
1068 void quirk_insert(const char *, const char *, const char *,unsigned long);
1069 void quirk_remove(struct quirk
*);
1070 void quirk_replace(struct quirk
*, const char *, const char *, const char *,
1072 void quit(struct swm_region
*, union arg
*);
1073 void raise_toggle(struct swm_region
*, union arg
*);
1074 void raise_window(struct ws_win
*);
1075 void region_containment(struct ws_win
*, struct swm_region
*, int);
1076 struct swm_region
*region_under(struct swm_screen
*, int, int);
1077 void regionize(struct ws_win
*, int, int);
1078 void resize(struct ws_win
*, union arg
*);
1079 void resize_step(struct swm_region
*, union arg
*);
1080 void restart(struct swm_region
*, union arg
*);
1081 struct swm_region
*root_to_region(xcb_window_t
, int);
1082 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1083 void scan_xrandr(int);
1084 void search_do_resp(void);
1085 void search_resp_name_workspace(const char *, size_t);
1086 void search_resp_search_window(const char *);
1087 void search_resp_search_workspace(const char *);
1088 void search_resp_uniconify(const char *, size_t);
1089 void search_win(struct swm_region
*, union arg
*);
1090 void search_win_cleanup(void);
1091 void search_workspace(struct swm_region
*, union arg
*);
1092 void send_to_rg(struct swm_region
*, union arg
*);
1093 void send_to_ws(struct swm_region
*, union arg
*);
1094 void set_region(struct swm_region
*);
1095 int setautorun(const char *, const char *, int);
1096 int setconfbinding(const char *, const char *, int);
1097 int setconfcolor(const char *, const char *, int);
1098 int setconfmodkey(const char *, const char *, int);
1099 int setconfquirk(const char *, const char *, int);
1100 int setconfregion(const char *, const char *, int);
1101 int setconfspawn(const char *, const char *, int);
1102 int setconfvalue(const char *, const char *, int);
1103 void setkeybinding(unsigned int, KeySym
, enum keyfuncid
, const char *);
1104 int setkeymapping(const char *, const char *, int);
1105 int setlayout(const char *, const char *, int);
1106 void setquirk(const char *, const char *, const char *,unsigned long);
1107 void setscreencolor(const char *, int, int);
1108 void setspawn(const char *, const char *, int);
1109 void setup_ewmh(void);
1110 void setup_globals(void);
1111 void setup_keys(void);
1112 void setup_quirks(void);
1113 void setup_screens(void);
1114 void setup_spawn(void);
1115 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1116 void set_win_state(struct ws_win
*, uint16_t);
1117 void shutdown_cleanup(void);
1119 void socket_setnonblock(int);
1120 void sort_windows(struct ws_win_list
*);
1121 void spawn(int, union arg
*, int);
1122 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1123 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1124 void spawn_insert(const char *, const char *, int);
1125 struct spawn_prog
*spawn_find(const char *);
1126 void spawn_remove(struct spawn_prog
*);
1127 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1128 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1129 void stack_config(struct swm_region
*, union arg
*);
1130 void stack_master(struct workspace
*, struct swm_geometry
*, int, int);
1131 void store_float_geom(struct ws_win
*);
1132 char *strdupsafe(const char *);
1133 void swapwin(struct swm_region
*, union arg
*);
1134 void switchws(struct swm_region
*, union arg
*);
1135 void teardown_ewmh(void);
1136 void unescape_selector(char *);
1137 void unfocus_win(struct ws_win
*);
1138 void uniconify(struct swm_region
*, union arg
*);
1139 void unmanage_window(struct ws_win
*);
1140 void unmapnotify(xcb_unmap_notify_event_t
*);
1141 void unmap_all(void);
1142 void unmap_window(struct ws_win
*);
1143 void updatenumlockmask(void);
1144 void update_floater(struct ws_win
*);
1145 void update_modkey(unsigned int);
1146 void update_win_stacking(struct ws_win
*);
1147 void update_window(struct ws_win
*);
1148 void update_wm_state(struct ws_win
*win
);
1149 void validate_spawns(void);
1150 int validate_win(struct ws_win
*);
1151 int validate_ws(struct workspace
*);
1152 void version(struct swm_region
*, union arg
*);
1153 void win_to_ws(struct ws_win
*, int, int);
1154 pid_t
window_get_pid(xcb_window_t
);
1155 void wkill(struct swm_region
*, union arg
*);
1156 void update_ws_stack(struct workspace
*);
1157 void xft_init(struct swm_region
*);
1158 void _add_startup_exception(const char *, va_list);
1159 void add_startup_exception(const char *, ...);
1161 RB_PROTOTYPE(key_tree
, key
, entry
, key_cmp
);
1162 RB_GENERATE(key_tree
, key
, entry
, key_cmp
);
1163 struct key_tree keys
;
1168 xcb_font_t cf
= XCB_NONE
;
1171 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1172 /* try to load Xcursor first. */
1173 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1176 /* fallback to cursorfont. */
1177 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1178 if (cf
== XCB_NONE
) {
1179 cf
= xcb_generate_id(conn
);
1180 xcb_open_font(conn
, cf
, strlen("cursor"),
1184 cursors
[i
].cid
= xcb_generate_id(conn
);
1185 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1186 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1187 0xffff, 0xffff, 0xffff);
1193 xcb_close_font(conn
, cf
);
1197 cursors_cleanup(void)
1200 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1201 xcb_free_cursor(conn
, cursors
[i
].cid
);
1205 expand_tilde(const char *s
)
1207 struct passwd
*ppwd
;
1215 errx(1, "expand_tilde: NULL string.");
1218 result
= strdup(sc
);
1224 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1225 errx(1, "expand_tilde: sysconf");
1227 if ((user
= calloc(1, max
+ 1)) == NULL
)
1228 errx(1, "expand_tilde: calloc");
1230 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1235 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1239 result
= strdup(sc
);
1241 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1245 errx(1, "expand_tilde: failed to allocate memory.");
1251 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1253 unsigned int tmpr
, tmpg
, tmpb
;
1255 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1258 *rr
= RGB_8_TO_16(tmpr
);
1259 *gg
= RGB_8_TO_16(tmpg
);
1260 *bb
= RGB_8_TO_16(tmpb
);
1266 get_screen(int screen
)
1268 const xcb_setup_t
*r
;
1269 xcb_screen_iterator_t iter
;
1271 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1272 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1276 iter
= xcb_setup_roots_iterator(r
);
1277 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1285 get_screen_count(void)
1287 const xcb_setup_t
*r
;
1289 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1290 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1294 return xcb_setup_roots_length(r
);
1298 get_region_index(struct swm_region
*r
)
1300 struct swm_region
*rr
;
1306 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1321 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1322 event_drain(XCB_ENTER_NOTIFY
);
1328 get_atom_from_string(const char *str
)
1330 xcb_intern_atom_cookie_t c
;
1331 xcb_intern_atom_reply_t
*r
;
1334 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1335 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1343 return (XCB_ATOM_NONE
);
1347 get_wm_protocols(struct ws_win
*win
) {
1349 xcb_icccm_get_wm_protocols_reply_t wpr
;
1351 if (xcb_icccm_get_wm_protocols_reply(conn
,
1352 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
1354 for (i
= 0; i
< (int)wpr
.atoms_len
; i
++) {
1355 if (wpr
.atoms
[i
] == a_takefocus
)
1356 win
->take_focus
= 1;
1357 if (wpr
.atoms
[i
] == a_delete
)
1358 win
->can_delete
= 1;
1360 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
1367 xcb_window_t root
, win
;
1368 int i
, j
, num_screens
;
1370 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1371 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1373 num_screens
= get_screen_count();
1374 for (i
= 0; i
< num_screens
; i
++) {
1375 root
= screens
[i
].root
;
1377 /* Set up _NET_SUPPORTING_WM_CHECK. */
1378 win
= xcb_generate_id(conn
);
1379 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
1380 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1381 XCB_COPY_FROM_PARENT
, 0, NULL
);
1383 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
1384 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1385 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1386 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1389 * Impersonate LG3D non-reparenting WM, written by Sun, to
1390 * workaround a Java GUI rendering issue.
1392 if (java_workaround
)
1393 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1394 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1395 8, strlen("LG3D"), "LG3D");
1397 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1398 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1399 8, strlen("spectrwm"), "spectrwm");
1401 /* Report supported atoms */
1402 xcb_delete_property(conn
, root
, a_net_supported
);
1403 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1404 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, root
,
1405 a_net_supported
, XCB_ATOM_ATOM
, 32, 1,
1408 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
1409 ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
, XCB_ATOM_CARDINAL
, 32,
1410 1, &workspace_limit
);
1419 xcb_get_property_cookie_t pc
;
1420 xcb_get_property_reply_t
*pr
;
1422 num_screens
= get_screen_count();
1424 for (i
= 0; i
< num_screens
; i
++) {
1425 /* Get the support check window and destroy it */
1426 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, a_net_wm_check
,
1427 XCB_ATOM_WINDOW
, 0, 1);
1428 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1431 if (pr
->format
== a_net_wm_check
) {
1432 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1434 xcb_destroy_window(conn
, id
);
1435 xcb_delete_property(conn
, screens
[i
].root
,
1437 xcb_delete_property(conn
, screens
[i
].root
,
1445 ewmh_autoquirk(struct ws_win
*win
)
1449 xcb_get_property_cookie_t c
;
1450 xcb_get_property_reply_t
*r
;
1452 c
= xcb_get_property(conn
, 0, win
->id
,
1453 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1454 r
= xcb_get_property_reply(conn
, c
, NULL
);
1458 type
= xcb_get_property_value(r
);
1459 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1461 for (i
= 0; i
< n
; i
++) {
1462 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1464 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1465 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1466 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1467 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1470 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1471 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1472 win
->quirks
= SWM_Q_FLOAT
;
1480 ewmh_update_actions(struct ws_win
*win
)
1482 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1488 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1491 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1492 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1493 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1496 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1497 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1500 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1501 #define _NET_WM_STATE_ADD 1 /* add/set property */
1502 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1505 ewmh_change_wm_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1512 name
= get_atom_name(state
);
1513 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: win %#x, state: %s, "
1514 "action: %ld\n", WINID(win
), name
, action
);
1520 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1521 flag
= EWMH_F_FULLSCREEN
;
1522 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1523 flag
= EWMH_F_ABOVE
;
1524 else if (state
== ewmh
[_NET_WM_STATE_HIDDEN
].atom
)
1525 flag
= EWMH_F_HIDDEN
;
1526 else if (state
== ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
||
1527 state
== ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
)
1528 flag
= EWMH_F_MAXIMIZED
;
1529 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1530 flag
= SWM_F_MANUAL
;
1531 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1532 flag
= EWMH_F_SKIP_PAGER
;
1533 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1534 flag
= EWMH_F_SKIP_TASKBAR
;
1536 /* Disallow unfloating transients. */
1537 if (TRANS(win
) && flag
== EWMH_F_ABOVE
)
1540 new_flags
= win
->ewmh_flags
;
1543 case _NET_WM_STATE_REMOVE
:
1546 case _NET_WM_STATE_ADD
:
1549 case _NET_WM_STATE_TOGGLE
:
1554 ewmh_apply_flags(win
, new_flags
);
1557 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: done.\n");
1561 ewmh_apply_flags(struct ws_win
*win
, uint32_t pending
)
1563 struct workspace
*ws
;
1566 changed
= win
->ewmh_flags
^ pending
;
1570 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: pending: %d\n", pending
);
1572 win
->ewmh_flags
= pending
;
1575 if (changed
& EWMH_F_HIDDEN
) {
1577 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1578 ws
->focus_pending
= get_focus_prev(win
);
1583 /* Reload floating geometry in case region changed. */
1585 load_float_geom(win
);
1587 /* The window is no longer iconic, prepare focus. */
1588 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1589 ws
->focus_pending
= get_focus_magic(win
);
1594 if (changed
& EWMH_F_ABOVE
) {
1595 if (ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1597 load_float_geom(win
);
1598 else if (!MAXIMIZED(win
))
1599 store_float_geom(win
);
1601 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1602 changed
&= ~EWMH_F_MAXIMIZED
;
1606 win
->ewmh_flags
^= EWMH_F_ABOVE
& pending
;
1610 if (changed
& EWMH_F_MAXIMIZED
) {
1611 /* VERT and/or HORZ changed. */
1613 if (!MAXIMIZED(win
))
1614 load_float_geom(win
);
1616 store_float_geom(win
);
1619 if (MAXIMIZED(win
)) {
1620 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
1621 ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1625 ws
->focus_pending
= win
;
1631 if (changed
& EWMH_F_FULLSCREEN
) {
1632 if (FULLSCREEN(win
)) {
1633 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
1637 ws
->focus_pending
= win
;
1640 load_float_geom(win
);
1643 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1647 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: done.\n");
1651 ewmh_update_wm_state(struct ws_win
*win
) {
1652 xcb_atom_t vals
[SWM_EWMH_ACTION_COUNT_MAX
];
1656 vals
[n
++] = ewmh
[_NET_WM_STATE_HIDDEN
].atom
;
1657 if (FULLSCREEN(win
))
1658 vals
[n
++] = ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
;
1659 if (MAXIMIZED_VERT(win
))
1660 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
;
1661 if (MAXIMIZED_HORZ(win
))
1662 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
;
1663 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1664 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
;
1665 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1666 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
;
1667 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1668 vals
[n
++] = ewmh
[_NET_WM_STATE_ABOVE
].atom
;
1669 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1670 vals
[n
++] = ewmh
[_SWM_WM_STATE_MANUAL
].atom
;
1673 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1674 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, n
, vals
);
1676 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1680 ewmh_get_wm_state(struct ws_win
*win
)
1683 xcb_get_property_cookie_t c
;
1684 xcb_get_property_reply_t
*r
;
1690 win
->ewmh_flags
= 0;
1692 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1693 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1694 r
= xcb_get_property_reply(conn
, c
, NULL
);
1698 states
= xcb_get_property_value(r
);
1699 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1701 for (i
= 0; i
< n
; i
++)
1702 ewmh_change_wm_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1710 dumpwins(struct swm_region
*r
, union arg
*args
)
1714 xcb_get_window_attributes_cookie_t c
;
1715 xcb_get_window_attributes_reply_t
*wa
;
1717 /* suppress unused warning since var is needed */
1720 if (r
->ws
== NULL
) {
1721 DPRINTF("dumpwins: invalid workspace\n");
1725 DPRINTF("=== managed window list ws %02d ===\n", r
->ws
->idx
);
1726 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
1727 state
= getstate(win
->id
);
1728 c
= xcb_get_window_attributes(conn
, win
->id
);
1729 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1731 DPRINTF("win %#x, map_state: %d, state: %u, "
1732 "transient: %#x\n", win
->id
, wa
->map_state
,
1733 state
, win
->transient
);
1736 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1740 DPRINTF("===== unmanaged window list =====\n");
1741 TAILQ_FOREACH(win
, &r
->ws
->unmanagedlist
, entry
) {
1742 state
= getstate(win
->id
);
1743 c
= xcb_get_window_attributes(conn
, win
->id
);
1744 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1746 DPRINTF("win %#x, map_state: %d, state: %u, "
1747 "transient: %#x\n", win
->id
, wa
->map_state
,
1748 state
, win
->transient
);
1751 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1755 DPRINTF("=================================\n");
1759 dumpwins(struct swm_region
*r
, union arg
*s
)
1764 #endif /* SWM_DEBUG */
1769 int saved_errno
, status
;
1772 saved_errno
= errno
;
1776 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
1781 if (errno
!= ECHILD
)
1782 warn("sighdlr: waitpid");
1783 #endif /* SWM_DEBUG */
1786 if (pid
== searchpid
)
1790 if (WIFEXITED(status
)) {
1791 if (WEXITSTATUS(status
) != 0)
1792 warnx("sighdlr: child exit status: %d",
1793 WEXITSTATUS(status
));
1795 warnx("sighdlr: child is terminated "
1797 #endif /* SWM_DEBUG */
1811 errno
= saved_errno
;
1817 struct pid_e
*p
= NULL
;
1819 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
1824 TAILQ_FOREACH(p
, &pidlist
, entry
) {
1833 name_to_pixel(int sidx
, const char *colorname
)
1835 uint32_t result
= 0;
1836 char cname
[32] = "#";
1837 xcb_screen_t
*screen
;
1838 xcb_colormap_t cmap
;
1839 xcb_alloc_color_reply_t
*cr
;
1840 xcb_alloc_named_color_reply_t
*nr
;
1841 uint16_t rr
, gg
, bb
;
1843 screen
= get_screen(sidx
);
1844 cmap
= screen
->default_colormap
;
1846 /* color is in format rgb://rr/gg/bb */
1847 if (strncmp(colorname
, "rgb:", 4) == 0) {
1848 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
1849 warnx("could not parse rgb %s", colorname
);
1851 cr
= xcb_alloc_color_reply(conn
,
1852 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
1858 warnx("color '%s' not found", colorname
);
1861 nr
= xcb_alloc_named_color_reply(conn
,
1862 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
1865 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
1866 nr
= xcb_alloc_named_color_reply(conn
,
1867 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
1874 warnx("color '%s' not found", colorname
);
1881 setscreencolor(const char *val
, int i
, int c
)
1885 num_screens
= get_screen_count();
1886 if (i
> 0 && i
<= num_screens
) {
1887 screens
[i
- 1].c
[c
].pixel
= name_to_pixel(i
- 1, val
);
1888 free(screens
[i
- 1].c
[c
].name
);
1889 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
1891 } else if (i
== -1) {
1892 for (i
= 0; i
< num_screens
; i
++) {
1893 screens
[i
].c
[c
].pixel
= name_to_pixel(0, val
);
1894 free(screens
[i
].c
[c
].name
);
1895 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
1899 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1904 fancy_stacker(struct workspace
*ws
)
1906 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1907 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1908 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1909 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
1910 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
1911 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1912 snprintf(ws
->stacker
, sizeof ws
->stacker
,
1913 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
1914 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
1918 plain_stacker(struct workspace
*ws
)
1920 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
1921 if (ws
->cur_layout
->l_stack
== vertical_stack
)
1922 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
1923 sizeof ws
->stacker
);
1924 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
1925 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
1926 sizeof ws
->stacker
);
1930 custom_region(const char *val
)
1932 unsigned int x
, y
, w
, h
;
1933 int sidx
, num_screens
;
1934 xcb_screen_t
*screen
;
1936 num_screens
= get_screen_count();
1937 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
1938 errx(1, "invalid custom region, "
1939 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
1940 if (sidx
< 1 || sidx
> num_screens
)
1941 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
1945 if ((screen
= get_screen(sidx
)) == NULL
)
1946 errx(1, "ERROR: can't get screen %d.", sidx
);
1949 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
1951 if (x
> screen
->width_in_pixels
||
1952 y
> screen
->height_in_pixels
||
1953 w
+ x
> screen
->width_in_pixels
||
1954 h
+ y
> screen
->height_in_pixels
) {
1955 warnx("ignoring region %ux%u+%u+%u - not within screen "
1956 "boundaries (%ux%u)", w
, h
, x
, y
,
1957 screen
->width_in_pixels
, screen
->height_in_pixels
);
1961 new_region(&screens
[sidx
], x
, y
, w
, h
);
1965 socket_setnonblock(int fd
)
1969 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
1970 err(1, "fcntl F_GETFL");
1971 flags
|= O_NONBLOCK
;
1972 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
1973 err(1, "fcntl F_SETFL");
1977 bar_print_legacy(struct swm_region
*r
, const char *s
)
1979 xcb_rectangle_t rect
;
1984 XRectangle ibox
, lbox
;
1988 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
1990 switch (bar_justify
) {
1991 case SWM_BAR_JUSTIFY_LEFT
:
1994 case SWM_BAR_JUSTIFY_CENTER
:
1995 x
= (WIDTH(r
) - lbox
.width
) / 2;
1997 case SWM_BAR_JUSTIFY_RIGHT
:
1998 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
2002 if (x
< SWM_BAR_OFFSET
)
2007 rect
.width
= WIDTH(r
->bar
);
2008 rect
.height
= HEIGHT(r
->bar
);
2010 /* clear back buffer */
2011 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2012 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2013 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2015 /* draw back buffer */
2016 gcvd
.graphics_exposures
= 0;
2017 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
2018 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2019 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
2020 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
2022 XFreeGC(display
, draw
);
2025 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2026 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2030 bar_print(struct swm_region
*r
, const char *s
)
2033 xcb_rectangle_t rect
;
2041 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
2043 switch (bar_justify
) {
2044 case SWM_BAR_JUSTIFY_LEFT
:
2047 case SWM_BAR_JUSTIFY_CENTER
:
2048 x
= (WIDTH(r
) - info
.width
) / 2;
2050 case SWM_BAR_JUSTIFY_RIGHT
:
2051 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
2055 if (x
< SWM_BAR_OFFSET
)
2060 rect
.width
= WIDTH(r
->bar
);
2061 rect
.height
= HEIGHT(r
->bar
);
2063 /* clear back buffer */
2064 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2065 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2066 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2068 /* draw back buffer */
2069 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
2070 DefaultVisual(display
, r
->s
->idx
),
2071 DefaultColormap(display
, r
->s
->idx
));
2073 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
2074 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
2077 XftDrawDestroy(draw
);
2080 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2081 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2085 bar_extra_stop(void)
2089 bzero(bar_pipe
, sizeof bar_pipe
);
2092 kill(bar_pid
, SIGTERM
);
2095 strlcpy(bar_ext
, "", sizeof bar_ext
);
2100 bar_window_class(char *s
, size_t sz
, struct swm_region
*r
)
2102 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2104 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
2105 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
2109 bar_window_instance(char *s
, size_t sz
, struct swm_region
*r
)
2111 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2113 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
2114 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
2118 bar_window_class_instance(char *s
, size_t sz
, struct swm_region
*r
)
2120 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2123 bar_window_class(s
, sz
, r
);
2124 strlcat(s
, ":", sz
);
2125 bar_window_instance(s
, sz
, r
);
2129 bar_window_state(char *s
, size_t sz
, struct swm_region
*r
)
2131 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2133 if (MAXIMIZED(r
->ws
->focus
))
2134 strlcat(s
, "(m)", sz
);
2135 else if (ABOVE(r
->ws
->focus
))
2136 strlcat(s
, "(f)", sz
);
2140 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
2144 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2147 title
= get_win_name(r
->ws
->focus
->id
);
2148 strlcat(s
, title
, sz
);
2153 bar_urgent(char *s
, size_t sz
)
2156 int i
, j
, num_screens
;
2157 int urgent
[SWM_WS_MAX
];
2159 xcb_get_property_cookie_t c
;
2160 xcb_icccm_wm_hints_t hints
;
2162 for (i
= 0; i
< workspace_limit
; i
++)
2165 num_screens
= get_screen_count();
2166 for (i
= 0; i
< num_screens
; i
++)
2167 for (j
= 0; j
< workspace_limit
; j
++)
2168 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
2169 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2170 if (xcb_icccm_get_wm_hints_reply(conn
, c
,
2173 if (hints
.flags
& XCB_ICCCM_WM_HINT_X_URGENCY
)
2177 for (i
= 0; i
< workspace_limit
; i
++) {
2179 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2181 snprintf(b
, sizeof b
, "- ");
2187 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2189 if (r
== NULL
|| r
->ws
== NULL
)
2191 if (r
->ws
->name
!= NULL
)
2192 strlcat(s
, r
->ws
->name
, sz
);
2195 /* build the default bar format according to the defined enabled options */
2197 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2201 /* if format provided, just copy the buffers */
2202 if (bar_format
!= NULL
) {
2203 strlcpy(fmtnew
, fmtexp
, sz
);
2207 /* reset the output buffer */
2210 strlcat(fmtnew
, "+N:+I ", sz
);
2212 strlcat(fmtnew
, "+S", sz
);
2213 strlcat(fmtnew
, " ", sz
);
2215 /* only show the workspace name if there's actually one */
2216 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2217 strlcat(fmtnew
, "<+D>", sz
);
2219 /* If enabled, only show the iconic count if there are iconic wins. */
2220 if (iconic_enabled
&& r
!= NULL
&& r
->ws
!= NULL
)
2221 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2223 strlcat(fmtnew
, "{+M}", sz
);
2227 strlcat(fmtnew
, "+3<", sz
);
2229 if (clock_enabled
) {
2230 strlcat(fmtnew
, fmtexp
, sz
);
2231 strlcat(fmtnew
, "+4<", sz
);
2234 /* bar_urgent already adds the space before the last asterisk */
2236 strlcat(fmtnew
, "* +U*+4<", sz
);
2238 if (window_class_enabled
) {
2239 strlcat(fmtnew
, "+C", sz
);
2240 if (!window_instance_enabled
)
2241 strlcat(fmtnew
, "+4<", sz
);
2244 /* checks needed by the colon and floating strlcat(3) calls below */
2245 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2246 if (window_instance_enabled
) {
2247 if (window_class_enabled
)
2248 strlcat(fmtnew
, ":", sz
);
2249 strlcat(fmtnew
, "+T+4<", sz
);
2251 if (window_name_enabled
) {
2252 if (ABOVE(r
->ws
->focus
) || MAXIMIZED(r
->ws
->focus
))
2253 strlcat(fmtnew
, "+F ", sz
);
2254 strlcat(fmtnew
, "+64W ", sz
);
2258 /* finally add the action script output and the version */
2259 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2263 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2265 /* special case; no limit given, pad one space, instead */
2266 if (*limit
== (int)sz
- 1)
2268 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2271 /* replaces the bar format character sequences (like in tmux(1)) */
2273 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2278 char tmp
[SWM_BAR_MAX
];
2279 int limit
, size
, count
;
2282 /* reset strlcat(3) buffer */
2285 /* get number, if any */
2288 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2289 limit
= sizeof tmp
- 1;
2290 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2291 limit
= sizeof tmp
- 1;
2293 /* there is nothing to replace (ie EOL) */
2300 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2303 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2306 bar_window_class(tmp
, sizeof tmp
, r
);
2309 bar_workspace_name(tmp
, sizeof tmp
, r
);
2312 bar_window_state(tmp
, sizeof tmp
, r
);
2315 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2319 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2323 snprintf(tmp
, sizeof tmp
, "%d", count
);
2326 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2329 bar_window_class_instance(tmp
, sizeof tmp
, r
);
2332 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2335 bar_window_instance(tmp
, sizeof tmp
, r
);
2338 bar_urgent(tmp
, sizeof tmp
);
2341 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2344 bar_window_name(tmp
, sizeof tmp
, r
);
2347 /* unknown character sequence; copy as-is */
2348 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2354 if ((int)len
< limit
)
2356 while (limit
-- > 0) {
2357 if (*offrep
>= sz
- 1)
2359 fmtrep
[(*offrep
)++] = *ptr
++;
2367 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2372 while (*fmt
!= '\0') {
2374 /* skip ordinary characters */
2377 fmtrep
[off
++] = *fmt
++;
2381 /* character sequence found; replace it */
2382 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2391 bar_fmt_expand(char *fmtexp
, size_t sz
)
2398 /* start by grabbing the current time and date */
2400 localtime_r(&tmt
, &tm
);
2402 /* figure out what to expand */
2403 if (bar_format
!= NULL
)
2405 else if (bar_format
== NULL
&& clock_enabled
)
2407 /* if nothing to expand bail out */
2413 /* copy as-is, just in case the format shouldn't be expanded below */
2414 strlcpy(fmtexp
, fmt
, sz
);
2415 /* finally pass the string through strftime(3) */
2416 #ifndef SWM_DENY_CLOCK_FORMAT
2417 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2418 warnx("format too long");
2423 /* Redraws the bar; need to follow with xcb_flush() or focus_flush(). */
2427 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2428 char fmtrep
[SWM_BAR_MAX
];
2430 struct swm_region
*r
;
2432 /* expand the format by first passing it through strftime(3) */
2433 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2435 num_screens
= get_screen_count();
2436 for (i
= 0; i
< num_screens
; i
++) {
2437 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
2441 if (bar_enabled
&& r
->ws
->bar_enabled
)
2442 xcb_map_window(conn
, r
->bar
->id
);
2444 xcb_unmap_window(conn
, r
->bar
->id
);
2448 if (startup_exception
)
2449 snprintf(fmtrep
, sizeof fmtrep
, "total "
2450 "exceptions: %d, first exception: %s",
2454 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2455 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2457 if (bar_font_legacy
)
2458 bar_print_legacy(r
, fmtrep
);
2460 bar_print(r
, fmtrep
);
2466 * Reads external script output; call when stdin is readable.
2467 * Returns 1 if bar_ext was updated; otherwise 0.
2470 bar_extra_update(void)
2473 char b
[SWM_BAR_MAX
];
2479 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2482 if (b
[len
- 1] == '\n') {
2483 /* Remove newline. */
2486 /* "Clear" bar_ext. */
2489 /* Flush buffered output. */
2490 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2491 bar_ext_buf
[0] = '\0';
2493 /* Append new output to bar. */
2494 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2498 /* Buffer output. */
2499 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2504 if (errno
!= EAGAIN
) {
2505 warn("bar_action failed");
2514 bar_toggle(struct swm_region
*r
, union arg
*args
)
2516 struct swm_region
*tmpr
;
2519 /* suppress unused warnings since vars are needed */
2523 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2526 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2527 /* Only change if master switch is enabled. */
2529 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2531 bar_enabled
= r
->ws
->bar_enabled
= 1;
2533 case SWM_ARG_ID_BAR_TOGGLE
:
2534 bar_enabled
= !bar_enabled
;
2538 /* update bars as necessary */
2539 num_screens
= get_screen_count();
2540 for (i
= 0; i
< num_screens
; i
++)
2541 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2543 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2544 xcb_map_window(conn
, tmpr
->bar
->id
);
2546 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2551 /* must be after stack */
2558 bar_extra_setup(void)
2560 /* do this here because the conf file is in memory */
2561 if (!bar_extra
&& bar_argv
[0]) {
2562 /* launch external status app */
2564 if (pipe(bar_pipe
) == -1)
2565 err(1, "pipe error");
2566 socket_setnonblock(bar_pipe
[0]);
2567 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2569 /* Set stdin to read from the pipe. */
2570 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2573 /* Set stdout to write to the pipe. */
2574 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2577 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2578 err(1, "could not disable SIGPIPE");
2579 switch (bar_pid
= fork()) {
2581 err(1, "cannot fork");
2585 execvp(bar_argv
[0], bar_argv
);
2586 err(1, "%s external app failed", bar_argv
[0]);
2588 default: /* parent */
2593 atexit(kill_bar_extra_atexit
);
2598 kill_bar_extra_atexit(void)
2601 kill(bar_pid
, SIGTERM
);
2609 while ((s
= index(s
, '-'))) {
2614 return (count
== 14);
2620 char *default_string
;
2621 char **missing_charsets
;
2622 int num_missing_charsets
= 0;
2626 XFreeFontSet(display
, bar_fs
);
2630 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n", bar_fonts
);
2632 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2633 &num_missing_charsets
, &default_string
);
2635 if (num_missing_charsets
> 0) {
2636 warnx("Unable to load charset(s):");
2638 for (i
= 0; i
< num_missing_charsets
; ++i
)
2639 warnx("%s", missing_charsets
[i
]);
2641 XFreeStringList(missing_charsets
);
2643 if (strcmp(default_string
, ""))
2644 warnx("Glyphs from those sets will be replaced "
2645 "by '%s'.", default_string
);
2647 warnx("Glyphs from those sets won't be drawn.");
2651 errx(1, "Error creating font set structure.");
2653 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2655 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2656 2 * bar_border_width
;
2663 xft_init(struct swm_region
*r
)
2665 char *font
, *d
, *search
;
2668 if (bar_font
== NULL
) {
2669 if ((d
= strdup(bar_fonts
)) == NULL
)
2670 errx(1, "insufficient memory.");
2672 while ((font
= strsep(&search
, ",")) != NULL
) {
2676 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2679 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2682 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2686 if (bar_font
== NULL
) {
2687 warnx("unable to load font %s", font
);
2690 DNPRINTF(SWM_D_INIT
, "successfully opened "
2698 if (bar_font
== NULL
)
2699 errx(1, "unable to open a font");
2701 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2703 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2704 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2705 warn("Xft error: unable to allocate color.");
2707 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2714 bar_setup(struct swm_region
*r
)
2716 xcb_screen_t
*screen
;
2719 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2722 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2723 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2728 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2729 err(1, "bar_setup: calloc: failed to allocate memory.");
2731 if (bar_font_legacy
)
2737 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2738 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2739 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2741 /* Assume region is unfocused when we create the bar. */
2742 r
->bar
->id
= xcb_generate_id(conn
);
2743 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2744 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
2745 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
2746 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
2748 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2749 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2750 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
2751 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
2752 | XCB_CW_EVENT_MASK
, wa
);
2754 r
->bar
->buffer
= xcb_generate_id(conn
);
2755 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
2756 WIDTH(r
->bar
), HEIGHT(r
->bar
));
2759 xcb_randr_select_input(conn
, r
->bar
->id
,
2760 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
2763 xcb_map_window(conn
, r
->bar
->id
);
2765 DNPRINTF(SWM_D_BAR
, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
2766 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
2773 bar_cleanup(struct swm_region
*r
)
2777 xcb_destroy_window(conn
, r
->bar
->id
);
2778 xcb_free_pixmap(conn
, r
->bar
->buffer
);
2784 set_win_state(struct ws_win
*win
, uint16_t state
)
2786 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
2788 DNPRINTF(SWM_D_EVENT
, "set_win_state: win %#x, state: %u\n",
2794 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
2795 a_state
, 32, 2, data
);
2799 getstate(xcb_window_t w
)
2801 uint32_t result
= 0;
2802 xcb_get_property_cookie_t c
;
2803 xcb_get_property_reply_t
*r
;
2805 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
2806 r
= xcb_get_property_reply(conn
, c
, NULL
);
2808 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
2809 result
= *((uint32_t *)xcb_get_property_value(r
));
2813 DNPRINTF(SWM_D_MISC
, "getstate property: win %#x state %u\n", w
,
2819 version(struct swm_region
*r
, union arg
*args
)
2821 /* suppress unused warnings since vars are needed */
2825 bar_version
= !bar_version
;
2827 snprintf(bar_vertext
, sizeof bar_vertext
,
2828 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
2830 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
2837 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
2839 xcb_client_message_event_t ev
;
2847 name
= get_atom_name(a
);
2848 DNPRINTF(SWM_D_EVENT
, "client_msg: win %#x, atom: %s(%u), "
2850 win
->id
, name
, a
, t
);
2854 bzero(&ev
, sizeof ev
);
2855 ev
.response_type
= XCB_CLIENT_MESSAGE
;
2856 ev
.window
= win
->id
;
2859 ev
.data
.data32
[0] = a
;
2860 ev
.data
.data32
[1] = t
;
2862 xcb_send_event(conn
, 0, win
->id
,
2863 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
2866 /* synthetic response to a ConfigureRequest when not making a change */
2868 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
2870 xcb_configure_notify_event_t ce
;
2875 /* send notification of unchanged state. */
2876 bzero(&ce
, sizeof(ce
));
2877 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
2880 ce
.width
= WIDTH(win
);
2881 ce
.height
= HEIGHT(win
);
2882 ce
.override_redirect
= 0;
2887 ce
.window
= win
->id
;
2888 ce
.border_width
= BORDER(win
);
2889 ce
.above_sibling
= XCB_WINDOW_NONE
;
2892 ce
.event
= ev
->window
;
2893 ce
.window
= ev
->window
;
2895 /* make response appear more WM_SIZE_HINTS-compliant */
2896 if (win
->sh
.flags
) {
2897 DNPRINTF(SWM_D_MISC
, "config_win: hints: win %#x,"
2898 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
2899 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
2900 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
2901 SH_INC_W(win
), SH_INC_H(win
));
2906 /* the hint may be set... to 0! */
2907 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
2908 ce
.width
= SH_MIN_W(win
);
2909 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
2910 ce
.height
= SH_MIN_H(win
);
2915 /* may also be advertized as 0 */
2916 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
2917 ce
.width
= SH_MAX_W(win
);
2918 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
2919 ce
.height
= SH_MAX_H(win
);
2922 /* resize increment. */
2924 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
2925 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
2927 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
2928 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
2932 /* adjust x and y for requested border_width. */
2933 ce
.x
+= BORDER(win
) - ev
->border_width
;
2934 ce
.y
+= BORDER(win
) - ev
->border_width
;
2935 ce
.border_width
= ev
->border_width
;
2936 ce
.above_sibling
= ev
->sibling
;
2939 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
2940 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
2941 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
2943 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
2948 count_win(struct workspace
*ws
, int count_transient
)
2953 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
2954 if (!count_transient
&& FLOATING(win
))
2960 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
2966 quit(struct swm_region
*r
, union arg
*args
)
2968 /* suppress unused warnings since vars are needed */
2972 DNPRINTF(SWM_D_MISC
, "quit\n");
2977 raise_window(struct ws_win
*win
)
2979 struct ws_win
*target
= NULL
;
2980 struct swm_region
*r
;
2981 struct workspace
*ws
;
2983 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
2987 DNPRINTF(SWM_D_EVENT
, "raise_window: win %#x\n", win
->id
);
2989 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
2990 if (target
== win
|| ICONIC(target
))
2992 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
2994 if (FULLSCREEN(win
))
2996 if (FULLSCREEN(target
))
3000 if (MAXIMIZED(target
))
3002 if (ABOVE(win
) || TRANS(win
))
3004 if (!ABOVE(target
) && !TRANS(target
))
3008 if (target
!= NULL
) {
3009 /* Change stack position. */
3010 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3011 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3012 update_win_stacking(win
);
3015 DNPRINTF(SWM_D_EVENT
, "raise_window: done\n");
3019 update_win_stacking(struct ws_win
*win
)
3021 struct ws_win
*sibling
;
3022 struct swm_region
*r
;
3025 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
3028 sibling
= TAILQ_NEXT(win
, stack_entry
);
3029 if (sibling
!= NULL
&& FLOATING(win
) == FLOATING(sibling
))
3030 val
[0] = sibling
->id
;
3032 val
[0] = FLOATING(win
) ? r
->bar
->id
: r
->id
;
3034 DNPRINTF(SWM_D_EVENT
, "update_win_stacking: %#x, sibling %#x\n",
3037 val
[1] = XCB_STACK_MODE_ABOVE
;
3039 xcb_configure_window(conn
, win
->id
, XCB_CONFIG_WINDOW_SIBLING
|
3040 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
3044 map_window(struct ws_win
*win
)
3049 DNPRINTF(SWM_D_EVENT
, "map_window: win %#x, mapped: %s\n",
3050 win
->id
, YESNO(win
->mapped
));
3055 xcb_map_window(conn
, win
->id
);
3056 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
3061 unmap_window(struct ws_win
*win
)
3066 DNPRINTF(SWM_D_EVENT
, "unmap_window: win %#x, mapped: %s\n", win
->id
,
3067 YESNO(win
->mapped
));
3072 xcb_unmap_window(conn
, win
->id
);
3073 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
3081 int i
, j
, num_screens
;
3083 num_screens
= get_screen_count();
3084 for (i
= 0; i
< num_screens
; i
++)
3085 for (j
= 0; j
< workspace_limit
; j
++)
3086 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3091 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
3093 xcb_key_press_event_t event
;
3094 xcb_keycode_t
*keycode
;
3099 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
3101 DNPRINTF(SWM_D_MISC
, "fake_keypress: win %#x, keycode %u\n",
3104 bzero(&event
, sizeof(event
));
3105 event
.event
= win
->id
;
3106 event
.root
= win
->s
->root
;
3107 event
.child
= XCB_WINDOW_NONE
;
3108 event
.time
= XCB_CURRENT_TIME
;
3109 event
.event_x
= X(win
);
3110 event
.event_y
= Y(win
);
3113 event
.same_screen
= 1;
3114 event
.detail
= *keycode
;
3115 event
.state
= modifiers
;
3117 event
.response_type
= XCB_KEY_PRESS
;
3118 xcb_send_event(conn
, 1, win
->id
,
3119 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
3121 event
.response_type
= XCB_KEY_RELEASE
;
3122 xcb_send_event(conn
, 1, win
->id
,
3123 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
3129 restart(struct swm_region
*r
, union arg
*args
)
3131 /* suppress unused warning since var is needed */
3135 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
3139 execvp(start_argv
[0], start_argv
);
3140 warn("execvp failed");
3145 get_pointer_win(xcb_window_t root
)
3147 struct ws_win
*win
= NULL
;
3148 xcb_query_pointer_reply_t
*r
;
3150 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: %#x.\n", root
);
3152 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
3154 win
= find_window(r
->child
);
3156 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: %#x.\n",
3159 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
3168 root_to_region(xcb_window_t root
, int check
)
3171 struct swm_region
*r
= NULL
;
3173 xcb_query_pointer_reply_t
*qpr
;
3174 xcb_get_input_focus_reply_t
*gifr
;
3176 DNPRINTF(SWM_D_MISC
, "root_to_region: win %#x\n", root
);
3178 num_screens
= get_screen_count();
3179 for (i
= 0; i
< num_screens
; i
++)
3180 if (screens
[i
].root
== root
)
3183 if (check
& SWM_CK_REGION
)
3184 r
= screens
[i
].r_focus
;
3186 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
3187 /* Try to find an actively focused window */
3188 gifr
= xcb_get_input_focus_reply(conn
,
3189 xcb_get_input_focus(conn
), NULL
);
3191 cfw
= find_window(gifr
->focus
);
3192 if (cfw
&& cfw
->ws
->r
)
3199 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
3200 /* No region with an active focus; try to use pointer. */
3201 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
3202 screens
[i
].root
), NULL
);
3204 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
3205 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
3206 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3207 if (X(r
) <= qpr
->root_x
&&
3208 qpr
->root_x
< MAX_X(r
) &&
3209 Y(r
) <= qpr
->root_y
&&
3210 qpr
->root_y
< MAX_Y(r
))
3217 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
3218 r
= TAILQ_FIRST(&screens
[i
].rl
);
3220 DNPRINTF(SWM_D_MISC
, "root_to_region: idx: %d\n", get_region_index(r
));
3226 find_unmanaged_window(xcb_window_t id
)
3229 int i
, j
, num_screens
;
3231 num_screens
= get_screen_count();
3232 for (i
= 0; i
< num_screens
; i
++)
3233 for (j
= 0; j
< workspace_limit
; j
++)
3234 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
3242 find_window(xcb_window_t id
)
3245 int i
, j
, num_screens
;
3246 xcb_query_tree_reply_t
*r
;
3248 num_screens
= get_screen_count();
3249 for (i
= 0; i
< num_screens
; i
++)
3250 for (j
= 0; j
< workspace_limit
; j
++)
3251 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3255 r
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3259 /* if we were looking for the parent return that window instead */
3260 if (r
->parent
== 0 || r
->root
== r
->parent
) {
3265 /* look for parent */
3266 for (i
= 0; i
< num_screens
; i
++)
3267 for (j
= 0; j
< workspace_limit
; j
++)
3268 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3269 if (r
->parent
== win
->id
) {
3279 spawn(int ws_idx
, union arg
*args
, int close_fd
)
3284 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3286 close(xcb_get_file_descriptor(conn
));
3288 setenv("LD_PRELOAD", SWM_LIB
, 1);
3290 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3291 warn("spawn: asprintf SWM_WS");
3294 setenv("_SWM_WS", ret
, 1);
3298 if (asprintf(&ret
, "%d", getpid()) == -1) {
3299 warn("spawn: asprintf _SWM_PID");
3302 setenv("_SWM_PID", ret
, 1);
3306 if (setsid() == -1) {
3307 warn("spawn: setsid");
3313 * close stdin and stdout to prevent interaction between apps
3314 * and the baraction script
3315 * leave stderr open to record errors
3317 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3318 warn("spawn: open");
3321 dup2(fd
, STDIN_FILENO
);
3322 dup2(fd
, STDOUT_FILENO
);
3327 execvp(args
->argv
[0], args
->argv
);
3329 warn("spawn: execvp");
3334 kill_refs(struct ws_win
*win
)
3336 int i
, x
, num_screens
;
3337 struct swm_region
*r
;
3338 struct workspace
*ws
;
3343 num_screens
= get_screen_count();
3344 for (i
= 0; i
< num_screens
; i
++)
3345 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3346 for (x
= 0; x
< workspace_limit
; x
++) {
3348 if (win
== ws
->focus
)
3350 if (win
== ws
->focus_prev
)
3351 ws
->focus_prev
= NULL
;
3356 validate_win(struct ws_win
*testwin
)
3359 struct workspace
*ws
;
3360 struct swm_region
*r
;
3361 int i
, x
, num_screens
;
3363 if (testwin
== NULL
)
3366 num_screens
= get_screen_count();
3367 for (i
= 0; i
< num_screens
; i
++)
3368 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3369 for (x
= 0; x
< workspace_limit
; x
++) {
3371 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3379 validate_ws(struct workspace
*testws
)
3381 struct swm_region
*r
;
3382 struct workspace
*ws
;
3383 int i
, x
, num_screens
;
3385 /* validate all ws */
3386 num_screens
= get_screen_count();
3387 for (i
= 0; i
< num_screens
; i
++)
3388 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3389 for (x
= 0; x
< workspace_limit
; x
++) {
3398 unfocus_win(struct ws_win
*win
)
3400 xcb_window_t none
= XCB_WINDOW_NONE
;
3402 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: win %#x\n", WINID(win
));
3407 if (win
->ws
== NULL
) {
3408 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3412 if (validate_ws(win
->ws
)) {
3413 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3417 if (win
->ws
->r
== NULL
) {
3418 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3422 if (validate_win(win
)) {
3423 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3428 if (win
->ws
->focus
== win
) {
3429 win
->ws
->focus
= NULL
;
3430 win
->ws
->focus_prev
= win
;
3433 if (validate_win(win
->ws
->focus
)) {
3434 kill_refs(win
->ws
->focus
);
3435 win
->ws
->focus
= NULL
;
3438 if (validate_win(win
->ws
->focus_prev
)) {
3439 kill_refs(win
->ws
->focus_prev
);
3440 win
->ws
->focus_prev
= NULL
;
3443 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
3444 &win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
3446 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3447 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3449 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3453 focus_win(struct ws_win
*win
)
3455 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
;
3456 struct workspace
*ws
;
3457 xcb_get_input_focus_reply_t
*gifr
;
3459 DNPRINTF(SWM_D_FOCUS
, "focus_win: win %#x\n", WINID(win
));
3464 if (win
->ws
== NULL
)
3472 if (validate_ws(ws
))
3475 if (validate_win(win
)) {
3480 gifr
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3482 cfw
= find_window(gifr
->focus
);
3483 if (cfw
!= NULL
&& cfw
!= win
) {
3484 if (cfw
->ws
!= ws
&& cfw
->ws
->r
!= NULL
) {
3485 /* Change border to unfocused color. */
3486 xcb_change_window_attributes(conn
, cfw
->id
,
3487 XCB_CW_BORDER_PIXEL
,
3488 &cfw
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
3496 if (ws
->focus
!= win
) {
3497 if (ws
->focus
&& ws
->focus
!= cfw
)
3498 unfocus_win(ws
->focus
);
3502 /* If this window directs focus to a child window, then clear. */
3503 if (win
->focus_child
)
3504 win
->focus_child
= NULL
;
3506 /* If transient, adjust parent's focus child for focus_magic. */
3508 parent
= find_window(win
->transient
);
3509 if (parent
&& parent
->focus_child
!= win
)
3510 parent
->focus_child
= win
;
3513 if (cfw
!= win
&& ws
->r
!= NULL
) {
3514 /* Set input focus if no input hint, or indicated by hint. */
3515 if (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
3516 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
3518 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3519 win
->id
, last_event_time
);
3521 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
3522 ws
->r
->id
, XCB_CURRENT_TIME
);
3524 /* Tell app it can adjust focus to a specific window. */
3525 if (win
->take_focus
) {
3526 /* java is special; always tell parent */
3527 if (TRANS(win
) && win
->java
)
3528 client_msg(parent
, a_takefocus
,
3531 client_msg(win
, a_takefocus
, last_event_time
);
3534 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
3535 &ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
);
3537 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
3539 /* If a parent exists, map it first. */
3541 raise_window(parent
);
3544 /* Map siblings next. */
3545 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3546 if (w
!= win
&& !ICONIC(w
) &&
3547 win
->transient
== parent
->id
)
3551 /* Map focused window. */
3555 /* Finally, map children of focus window. */
3556 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3557 if (w
->transient
== win
->id
&& !ICONIC(w
))
3559 } else if (tile_gap
< 0 && !ABOVE(win
)) {
3561 * Windows overlap in the layout.
3562 * Raise focused win above all tiled wins.
3570 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3571 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3578 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
3581 /* If a child window should have focus instead, return it. */
3583 get_focus_magic(struct ws_win
*win
)
3585 struct ws_win
*parent
= NULL
;
3586 struct ws_win
*child
= NULL
;
3588 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: win %#x\n", WINID(win
));
3593 parent
= find_window(win
->transient
);
3595 /* If parent prefers focus elsewhere, then try to do so. */
3596 if (parent
&& (child
= parent
->focus_child
)) {
3597 if (validate_win(child
) == 0 && child
->mapped
)
3600 parent
->focus_child
= NULL
;
3604 /* If this window prefers focus elsewhere, then try to do so. */
3605 if ((child
= win
->focus_child
)) {
3606 if (validate_win(child
) == 0 && child
->mapped
)
3609 win
->focus_child
= NULL
;
3616 event_drain(uint8_t rt
)
3618 xcb_generic_event_t
*evt
;
3620 /* ensure all pending requests have been processed before filtering. */
3622 while ((evt
= xcb_poll_for_event(conn
))) {
3623 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
3631 set_region(struct swm_region
*r
)
3633 struct swm_region
*rf
;
3639 /* Unfocus old region bar. */
3644 xcb_change_window_attributes(conn
, rf
->bar
->id
,
3645 XCB_CW_BORDER_PIXEL
,
3646 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
3649 /* Set region bar border to focus_color. */
3650 xcb_change_window_attributes(conn
, r
->bar
->id
,
3651 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
3655 ewmh_update_current_desktop();
3659 focus_region(struct swm_region
*r
)
3662 struct swm_region
*old_r
;
3667 old_r
= r
->s
->r_focus
;
3670 nfw
= get_region_focus(r
);
3674 /* New region is empty; need to manually unfocus win. */
3676 unfocus_win(old_r
->ws
->focus
);
3678 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
3681 /* Clear bar since empty. */
3687 switchws(struct swm_region
*r
, union arg
*args
)
3689 struct swm_region
*this_r
, *other_r
;
3691 struct workspace
*new_ws
, *old_ws
;
3692 xcb_window_t none
= XCB_WINDOW_NONE
;
3693 int wsid
= args
->id
, unmap_old
= 0;
3698 if (wsid
>= workspace_limit
)
3702 old_ws
= this_r
->ws
;
3703 new_ws
= &this_r
->s
->ws
[wsid
];
3705 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
3706 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
3708 if (new_ws
== NULL
|| old_ws
== NULL
)
3710 if (new_ws
== old_ws
)
3713 if ((win
= old_ws
->focus
) != NULL
) {
3714 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
,
3715 &win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
);
3717 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3718 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
3722 other_r
= new_ws
->r
;
3723 if (other_r
== NULL
) {
3724 /* the other workspace is hidden, hide this one */
3728 /* the other ws is visible in another region, exchange them */
3729 other_r
->ws_prior
= new_ws
;
3730 other_r
->ws
= old_ws
;
3731 old_ws
->r
= other_r
;
3733 this_r
->ws_prior
= old_ws
;
3734 this_r
->ws
= new_ws
;
3737 /* Set focus_pending before stacking, if needed. */
3738 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
3739 validate_win(new_ws
->focus_pending
))) {
3740 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
3741 new_ws
->focus
= new_ws
->focus_prev
;
3742 new_ws
->focus_prev
= NULL
;
3745 new_ws
->state
= SWM_WS_STATE_MAPPING
;
3748 /* unmap old windows */
3750 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
3752 old_ws
->state
= SWM_WS_STATE_HIDDEN
;
3755 /* if workspaces were swapped, then don't wait to set focus */
3756 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
3757 if (new_ws
->focus_pending
) {
3758 focus_win(new_ws
->focus_pending
);
3759 new_ws
->focus_pending
= NULL
;
3763 /* Clear bar and set focus on region input win if new ws is empty. */
3764 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
3765 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
, r
->id
,
3770 ewmh_update_current_desktop();
3773 new_ws
->state
= SWM_WS_STATE_MAPPED
;
3775 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
3779 cyclews(struct swm_region
*r
, union arg
*args
)
3782 struct swm_screen
*s
= r
->s
;
3786 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3787 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3793 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
3796 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
3799 case SWM_ARG_ID_CYCLEWS_UP
:
3800 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
3802 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
3805 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
3808 case SWM_ARG_ID_CYCLEWS_DOWN
:
3809 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
3816 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
3818 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
3825 } while (a
.id
!= r
->ws
->idx
);
3827 DNPRINTF(SWM_D_FOCUS
, "cyclews: done\n");
3831 priorws(struct swm_region
*r
, union arg
*args
)
3837 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3838 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3840 if (r
->ws_prior
== NULL
)
3843 a
.id
= r
->ws_prior
->idx
;
3845 DNPRINTF(SWM_D_FOCUS
, "priorws: done\n");
3849 focusrg(struct swm_region
*r
, union arg
*args
)
3851 int ridx
= args
->id
, i
, num_screens
;
3852 struct swm_region
*rr
= NULL
;
3854 num_screens
= get_screen_count();
3855 /* do nothing if we don't have more than one screen */
3856 if (!(num_screens
> 1 || outputs
> 1))
3859 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
3861 rr
= TAILQ_FIRST(&r
->s
->rl
);
3862 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
3863 rr
= TAILQ_NEXT(rr
, entry
);
3870 DNPRINTF(SWM_D_FOCUS
, "focusrg: done\n");
3874 cyclerg(struct swm_region
*r
, union arg
*args
)
3876 struct swm_region
*rr
= NULL
;
3879 num_screens
= get_screen_count();
3880 /* do nothing if we don't have more than one screen */
3881 if (!(num_screens
> 1 || outputs
> 1))
3885 DNPRINTF(SWM_D_FOCUS
, "cyclerg: id: %d, region: %d\n", args
->id
, i
);
3888 case SWM_ARG_ID_CYCLERG_UP
:
3889 rr
= TAILQ_NEXT(r
, entry
);
3891 rr
= TAILQ_FIRST(&screens
[i
].rl
);
3893 case SWM_ARG_ID_CYCLERG_DOWN
:
3894 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
3896 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
3906 DNPRINTF(SWM_D_FOCUS
, "cyclerg: done\n");
3909 /* Sorts transients after parent. */
3911 sort_windows(struct ws_win_list
*wl
)
3913 struct ws_win
*win
, *parent
, *nxt
;
3918 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
3919 nxt
= TAILQ_NEXT(win
, entry
);
3921 parent
= find_window(win
->transient
);
3922 if (parent
== NULL
) {
3923 warnx("not possible bug");
3926 TAILQ_REMOVE(wl
, win
, entry
);
3927 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
3933 swapwin(struct swm_region
*r
, union arg
*args
)
3935 struct ws_win
*target
, *source
;
3936 struct ws_win
*cur_focus
;
3937 struct ws_win_list
*wl
;
3939 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
3940 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
3942 cur_focus
= r
->ws
->focus
;
3943 if (cur_focus
== NULL
|| ABOVE(cur_focus
) || FULLSCREEN(cur_focus
))
3946 if (r
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3949 clear_maximized(r
->ws
);
3952 wl
= &source
->ws
->winlist
;
3955 case SWM_ARG_ID_SWAPPREV
:
3957 source
= find_window(source
->transient
);
3958 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
3959 if (target
&& target
->transient
)
3960 target
= find_window(target
->transient
);
3961 TAILQ_REMOVE(wl
, source
, entry
);
3963 TAILQ_INSERT_TAIL(wl
, source
, entry
);
3965 TAILQ_INSERT_BEFORE(target
, source
, entry
);
3967 case SWM_ARG_ID_SWAPNEXT
:
3968 target
= TAILQ_NEXT(source
, entry
);
3969 /* move the parent and let the sort handle the move */
3971 source
= find_window(source
->transient
);
3972 TAILQ_REMOVE(wl
, source
, entry
);
3974 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3976 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
3978 case SWM_ARG_ID_SWAPMAIN
:
3979 target
= TAILQ_FIRST(wl
);
3980 if (target
== source
) {
3981 if (source
->ws
->focus_prev
!= NULL
&&
3982 source
->ws
->focus_prev
!= target
)
3983 source
= source
->ws
->focus_prev
;
3987 if (target
== NULL
|| source
== NULL
)
3989 source
->ws
->focus_prev
= target
;
3990 TAILQ_REMOVE(wl
, target
, entry
);
3991 TAILQ_INSERT_BEFORE(source
, target
, entry
);
3992 TAILQ_REMOVE(wl
, source
, entry
);
3993 TAILQ_INSERT_HEAD(wl
, source
, entry
);
3995 case SWM_ARG_ID_MOVELAST
:
3996 TAILQ_REMOVE(wl
, source
, entry
);
3997 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4000 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
4006 ewmh_update_client_list();
4014 get_focus_prev(struct ws_win
*win
)
4016 struct ws_win
*winfocus
= NULL
;
4017 struct ws_win
*cur_focus
= NULL
;
4018 struct ws_win_list
*wl
= NULL
;
4019 struct workspace
*ws
= NULL
;
4021 if (!(win
&& win
->ws
))
4026 cur_focus
= ws
->focus
;
4028 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: win %#x, cur_focus: %#x, "
4029 "focus_prev: %#x\n", WINID(win
), WINID(cur_focus
),
4030 WINID(ws
->focus_prev
));
4032 /* pickle, just focus on whatever */
4033 if (cur_focus
== NULL
) {
4034 /* use prev_focus if valid */
4035 if (ws
->focus_prev
&& find_window(ws
->focus_prev
->id
))
4036 winfocus
= ws
->focus_prev
;
4040 /* if transient focus on parent */
4041 if (TRANS(cur_focus
)) {
4042 winfocus
= find_window(cur_focus
->transient
);
4046 /* if in max_stack try harder */
4047 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
4048 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
4049 if (cur_focus
!= ws
->focus_prev
)
4050 winfocus
= ws
->focus_prev
;
4052 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
4057 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
4059 if (winfocus
== NULL
|| winfocus
== win
) {
4060 switch (focus_close
) {
4061 case SWM_STACK_BOTTOM
:
4062 TAILQ_FOREACH(winfocus
, wl
, entry
)
4063 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4067 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4068 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4071 case SWM_STACK_ABOVE
:
4072 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
4073 while (winfocus
&& ICONIC(winfocus
))
4074 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4076 if (winfocus
== NULL
) {
4077 if (focus_close_wrap
) {
4078 TAILQ_FOREACH(winfocus
, wl
, entry
)
4079 if (!ICONIC(winfocus
) &&
4080 winfocus
!= cur_focus
)
4083 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4085 if (!ICONIC(winfocus
) &&
4086 winfocus
!= cur_focus
)
4091 case SWM_STACK_BELOW
:
4092 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
4093 while (winfocus
&& ICONIC(winfocus
))
4094 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
4097 if (winfocus
== NULL
) {
4098 if (focus_close_wrap
) {
4099 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4101 if (!ICONIC(winfocus
) &&
4102 winfocus
!= cur_focus
)
4105 TAILQ_FOREACH(winfocus
, wl
, entry
)
4106 if (!ICONIC(winfocus
) &&
4107 winfocus
!= cur_focus
)
4115 if (winfocus
== NULL
||
4116 (winfocus
&& (ICONIC(winfocus
) || winfocus
== cur_focus
))) {
4117 if (focus_default
== SWM_STACK_TOP
) {
4118 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4119 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4122 TAILQ_FOREACH(winfocus
, wl
, entry
)
4123 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4130 return get_focus_magic(winfocus
);
4134 get_region_focus(struct swm_region
*r
)
4136 struct ws_win
*winfocus
= NULL
;
4141 if (r
->ws
->focus
&& !ICONIC(r
->ws
->focus
))
4142 winfocus
= r
->ws
->focus
;
4143 else if (r
->ws
->focus_prev
&& !ICONIC(r
->ws
->focus_prev
))
4144 winfocus
= r
->ws
->focus_prev
;
4146 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
4147 if (!ICONIC(winfocus
))
4150 return get_focus_magic(winfocus
);
4154 focus(struct swm_region
*r
, union arg
*args
)
4156 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
4157 struct ws_win_list
*wl
= NULL
;
4158 struct workspace
*ws
= NULL
;
4161 xcb_icccm_wm_hints_t hints
;
4166 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d\n", args
->id
);
4168 cur_focus
= r
->ws
->focus
;
4172 /* Make sure an uniconified window has focus, if one exists. */
4173 if (cur_focus
== NULL
) {
4174 cur_focus
= TAILQ_FIRST(wl
);
4175 while (cur_focus
!= NULL
&& ICONIC(cur_focus
))
4176 cur_focus
= TAILQ_NEXT(cur_focus
, entry
);
4180 case SWM_ARG_ID_FOCUSPREV
:
4181 if (cur_focus
== NULL
)
4184 winfocus
= cur_focus
;
4186 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
4187 if (winfocus
== NULL
)
4188 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
4189 if (winfocus
== cur_focus
)
4191 } while (winfocus
!= NULL
&&
4192 (ICONIC(winfocus
) || winfocus
->id
== cur_focus
->transient
));
4194 case SWM_ARG_ID_FOCUSNEXT
:
4195 if (cur_focus
== NULL
)
4198 winfocus
= cur_focus
;
4200 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4201 if (winfocus
== NULL
)
4202 winfocus
= TAILQ_FIRST(wl
);
4203 if (winfocus
== cur_focus
)
4205 } while (winfocus
!= NULL
&&
4206 (ICONIC(winfocus
) || winfocus
->id
== cur_focus
->transient
));
4208 case SWM_ARG_ID_FOCUSMAIN
:
4209 if (cur_focus
== NULL
)
4212 winfocus
= TAILQ_FIRST(wl
);
4213 if (winfocus
== cur_focus
)
4214 winfocus
= cur_focus
->ws
->focus_prev
;
4216 case SWM_ARG_ID_FOCUSURGENT
:
4217 /* Search forward for the next urgent window. */
4221 for (i
= 0; i
<= workspace_limit
; ++i
) {
4223 head
= TAILQ_FIRST(&r
->s
->ws
[(ws
->idx
+ i
) %
4224 workspace_limit
].winlist
);
4226 while (head
!= NULL
&&
4227 (head
= TAILQ_NEXT(head
, entry
)) != NULL
) {
4228 if (head
== cur_focus
) {
4229 winfocus
= cur_focus
;
4232 if (xcb_icccm_get_wm_hints_reply(conn
,
4233 xcb_icccm_get_wm_hints(conn
, head
->id
),
4234 &hints
, NULL
) != 0 &&
4235 xcb_icccm_wm_hints_get_urgency(&hints
)) {
4241 if (winfocus
!= NULL
)
4245 /* Switch ws if new focus is on a different ws. */
4246 if (winfocus
!= NULL
&& winfocus
->ws
!= ws
) {
4247 a
.id
= winfocus
->ws
->idx
;
4255 if (clear_maximized(ws
) > 0)
4258 focus_win(get_focus_magic(winfocus
));
4262 DNPRINTF(SWM_D_FOCUS
, "focus: done\n");
4266 cycle_layout(struct swm_region
*r
, union arg
*args
)
4268 struct workspace
*ws
= r
->ws
;
4270 /* suppress unused warning since var is needed */
4273 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
4276 if (ws
->cur_layout
->l_stack
== NULL
)
4277 ws
->cur_layout
= &layouts
[0];
4279 clear_maximized(ws
);
4284 focus_win(get_region_focus(r
));
4290 stack_config(struct swm_region
*r
, union arg
*args
)
4292 struct workspace
*ws
= r
->ws
;
4294 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
4297 if (clear_maximized(ws
) > 0)
4300 if (ws
->cur_layout
->l_config
!= NULL
)
4301 ws
->cur_layout
->l_config(ws
, args
->id
);
4303 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
4312 struct swm_geometry g
;
4313 struct swm_region
*r
, *r_prev
= NULL
;
4320 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
4322 num_screens
= get_screen_count();
4323 for (i
= 0; i
< num_screens
; i
++) {
4327 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
4328 /* Adjust stack area for region bar and padding. */
4330 g
.x
+= region_padding
;
4331 g
.y
+= region_padding
;
4332 g
.w
-= 2 * border_width
+ 2 * region_padding
;
4333 g
.h
-= 2 * border_width
+ 2 * region_padding
;
4334 if (bar_enabled
&& r
->ws
->bar_enabled
) {
4340 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d (screen: "
4341 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4342 r
->ws
->idx
, i
, j
++, g
.x
, g
.y
, g
.w
, g
.h
);
4345 /* Stack bar/input relative to prev. region. */
4346 val
[1] = XCB_STACK_MODE_ABOVE
;
4348 val
[0] = r_prev
->id
;
4349 DNPRINTF(SWM_D_STACK
, "stack: region input %#x "
4350 "relative to %#x.\n", r
->id
, val
[0]);
4351 xcb_configure_window(conn
, r
->id
,
4352 XCB_CONFIG_WINDOW_SIBLING
|
4353 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4355 val
[0] = r_prev
->bar
->id
;
4356 DNPRINTF(SWM_D_STACK
, "stack: region bar %#x "
4357 "relative to %#x.\n", r
->bar
->id
, val
[0]);
4358 xcb_configure_window(conn
, r
->bar
->id
,
4359 XCB_CONFIG_WINDOW_SIBLING
|
4360 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4363 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
4364 r
->ws
->cur_layout
->l_string(r
->ws
);
4365 /* save r so we can track region changes */
4373 DNPRINTF(SWM_D_STACK
, "stack: end\n");
4377 store_float_geom(struct ws_win
*win
)
4379 if (win
== NULL
|| win
->ws
->r
== NULL
)
4382 /* retain window geom and region geom */
4383 win
->g_float
= win
->g
;
4384 win
->g_float
.x
-= X(win
->ws
->r
);
4385 win
->g_float
.y
-= Y(win
->ws
->r
);
4386 win
->g_floatvalid
= 1;
4387 DNPRINTF(SWM_D_MISC
, "store_float_geom: win %#x, g: (%d,%d)"
4388 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
4389 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
4390 win
->g_float
.w
, win
->g_float
.h
);
4394 load_float_geom(struct ws_win
*win
)
4396 if (win
== NULL
|| win
->ws
->r
== NULL
)
4399 if (win
->g_floatvalid
) {
4400 win
->g
= win
->g_float
;
4401 X(win
) += X(win
->ws
->r
);
4402 Y(win
) += Y(win
->ws
->r
);
4403 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g: (%d,%d)"
4404 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
4407 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g_float "
4408 "is not set.\n", win
->id
);
4413 update_floater(struct ws_win
*win
)
4415 struct workspace
*ws
;
4416 struct swm_region
*r
;
4423 if ((r
= ws
->r
) == NULL
)
4426 DNPRINTF(SWM_D_MISC
, "update_floater: win %#x\n", win
->id
);
4428 if (FULLSCREEN(win
)) {
4429 /* _NET_WM_FULLSCREEN: fullscreen without border. */
4430 if (!win
->g_floatvalid
)
4431 store_float_geom(win
);
4435 } else if (MAXIMIZED(win
)) {
4436 /* Maximize: like a single stacked window. */
4437 if (!win
->g_floatvalid
)
4438 store_float_geom(win
);
4442 if (bar_enabled
&& ws
->bar_enabled
) {
4445 Y(win
) += bar_height
;
4446 HEIGHT(win
) -= bar_height
;
4447 } else if (disable_border
) {
4453 if (win
->bordered
) {
4454 HEIGHT(win
) -= 2 * border_width
;
4455 WIDTH(win
) -= 2 * border_width
;
4458 /* Normal floating window. */
4459 /* Update geometry if new region. */
4461 load_float_geom(win
);
4463 if ((win
->quirks
& SWM_Q_FULLSCREEN
) &&
4464 WIDTH(win
) >= WIDTH(r
) && HEIGHT(win
) >= HEIGHT(r
)) {
4465 /* Remove border for FULLSCREEN quirk. */
4467 } else if (!MANUAL(win
)) {
4468 if (TRANS(win
) && (win
->quirks
& SWM_Q_TRANSSZ
)) {
4469 /* Adjust size on TRANSSZ quirk. */
4470 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
4471 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
4474 if (!(win
->quirks
& SWM_Q_ANYWHERE
)) {
4476 * Floaters and transients are auto-centred
4477 * unless manually moved, resized or ANYWHERE
4480 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2 -
4482 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2 -
4484 store_float_geom(win
);
4489 /* Ensure at least 1 pixel of the window is in the region. */
4490 region_containment(win
, r
, SWM_CW_ALLSIDES
);
4496 * Send keystrokes to terminal to decrease/increase the font size as the
4497 * window size changes.
4500 adjust_font(struct ws_win
*win
)
4502 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
4503 ABOVE(win
) || TRANS(win
))
4506 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
4507 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
4508 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
4509 win
->font_size_boundary
[win
->font_steps
] =
4510 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
4513 win
->last_inc
= win
->sh
.width_inc
;
4514 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
4515 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
4516 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
4519 win
->last_inc
= win
->sh
.width_inc
;
4520 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
4524 #define SWAPXY(g) do { \
4526 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
4527 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
4530 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
4532 struct swm_geometry win_g
, r_g
= *g
;
4534 int i
, j
, s
, stacks
;
4535 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
4536 int hrh
, extra
= 0, h_slice
, last_h
= 0;
4537 int split
, colno
, winno
, mwin
, msize
, mscale
;
4538 int remain
, missing
, v_slice
, reconfigure
= 0;
4541 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
4542 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
4544 /* Prepare tiling variables, if needed. */
4545 if ((winno
= count_win(ws
, 0)) > 0) {
4546 /* Find first tiled window. */
4547 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
4548 if (!FLOATING(win
) && !ICONIC(win
))
4551 /* Take into account size hints of first tiled window. */
4553 w_inc
= win
->sh
.width_inc
;
4554 w_base
= win
->sh
.base_width
;
4555 mwin
= ws
->l_state
.horizontal_mwin
;
4556 mscale
= ws
->l_state
.horizontal_msize
;
4557 stacks
= ws
->l_state
.horizontal_stacks
;
4560 w_inc
= win
->sh
.height_inc
;
4561 w_base
= win
->sh
.base_height
;
4562 mwin
= ws
->l_state
.vertical_mwin
;
4563 mscale
= ws
->l_state
.vertical_msize
;
4564 stacks
= ws
->l_state
.vertical_stacks
;
4568 if (stacks
> winno
- mwin
)
4569 stacks
= winno
- mwin
;
4573 h_slice
= r_g
.h
/ SWM_H_SLICE
;
4574 if (mwin
&& winno
> mwin
) {
4575 v_slice
= r_g
.w
/ SWM_V_SLICE
;
4579 win_g
.w
= v_slice
* mscale
;
4581 if (w_inc
> 1 && w_inc
< v_slice
) {
4582 /* Adjust for requested size increment. */
4583 remain
= (win_g
.w
- w_base
) % w_inc
;
4589 win_g
.x
+= r_g
.w
- msize
;
4592 colno
= split
= winno
/ stacks
;
4593 win_g
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
4594 2 * border_width
) / stacks
);
4596 hrh
= r_g
.h
/ colno
;
4597 extra
= r_g
.h
- (colno
* hrh
);
4598 win_g
.h
= hrh
- 2 * border_width
;
4599 i
= j
= 0, s
= stacks
;
4602 /* Update window geometry. */
4603 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
4607 if (FLOATING(win
)) {
4608 update_floater(win
);
4613 if (split
&& i
== split
) {
4614 colno
= (winno
- mwin
) / stacks
;
4615 if (s
<= (winno
- mwin
) % stacks
)
4618 hrh
= r_g
.h
/ colno
;
4619 extra
= r_g
.h
- (colno
* hrh
);
4622 win_g
.x
+= win_g
.w
+ 2 * border_width
+
4625 win_g
.w
= (r_g
.w
- msize
-
4626 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
4628 win_g
.w
+= (r_g
.w
- msize
-
4629 (stacks
* (2 * border_width
+ tile_gap
))) %
4633 win_g
.x
-= win_g
.w
+ 2 * border_width
+
4639 win_g
.h
= hrh
- 2 * border_width
- tile_gap
;
4642 h_inc
= win
->sh
.width_inc
;
4643 h_base
= win
->sh
.base_width
;
4645 h_inc
= win
->sh
.height_inc
;
4646 h_base
= win
->sh
.base_height
;
4649 if (j
== colno
- 1) {
4650 win_g
.h
= hrh
+ extra
;
4651 } else if (h_inc
> 1 && h_inc
< h_slice
) {
4652 /* adjust for window's requested size increment */
4653 remain
= (win_g
.h
- h_base
) % h_inc
;
4654 missing
= h_inc
- remain
;
4656 if (missing
<= extra
|| j
== 0) {
4668 win_g
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
4670 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
) &&
4673 win_g
.w
+= 2 * border_width
;
4674 win_g
.h
+= 2 * border_width
;
4680 if (X(win
) != win_g
.y
|| Y(win
) != win_g
.x
||
4681 WIDTH(win
) != win_g
.h
|| HEIGHT(win
) != win_g
.w
) {
4685 WIDTH(win
) = win_g
.h
;
4686 HEIGHT(win
) = win_g
.w
;
4689 if (X(win
) != win_g
.x
|| Y(win
) != win_g
.y
||
4690 WIDTH(win
) != win_g
.w
|| HEIGHT(win
) != win_g
.h
) {
4694 WIDTH(win
) = win_g
.w
;
4695 HEIGHT(win
) = win_g
.h
;
4699 if (bordered
!= win
->bordered
) {
4701 win
->bordered
= bordered
;
4714 /* Stack all windows from bottom up. */
4715 TAILQ_FOREACH_REVERSE(win
, &ws
->stack
, ws_win_stack
, stack_entry
)
4717 update_win_stacking(win
);
4719 /* Map all windows from top down. */
4720 TAILQ_FOREACH(win
, &ws
->stack
, stack_entry
)
4724 DNPRINTF(SWM_D_STACK
, "stack_master: done\n");
4728 vertical_config(struct workspace
*ws
, int id
)
4730 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
4734 case SWM_ARG_ID_STACKRESET
:
4735 case SWM_ARG_ID_STACKINIT
:
4736 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
4737 ws
->l_state
.vertical_mwin
= 1;
4738 ws
->l_state
.vertical_stacks
= 1;
4740 case SWM_ARG_ID_MASTERSHRINK
:
4741 if (ws
->l_state
.vertical_msize
> 1)
4742 ws
->l_state
.vertical_msize
--;
4744 case SWM_ARG_ID_MASTERGROW
:
4745 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
4746 ws
->l_state
.vertical_msize
++;
4748 case SWM_ARG_ID_MASTERADD
:
4749 ws
->l_state
.vertical_mwin
++;
4751 case SWM_ARG_ID_MASTERDEL
:
4752 if (ws
->l_state
.vertical_mwin
> 0)
4753 ws
->l_state
.vertical_mwin
--;
4755 case SWM_ARG_ID_STACKINC
:
4756 ws
->l_state
.vertical_stacks
++;
4758 case SWM_ARG_ID_STACKDEC
:
4759 if (ws
->l_state
.vertical_stacks
> 1)
4760 ws
->l_state
.vertical_stacks
--;
4762 case SWM_ARG_ID_FLIPLAYOUT
:
4763 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
4771 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4773 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
4775 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
4779 horizontal_config(struct workspace
*ws
, int id
)
4781 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
4784 case SWM_ARG_ID_STACKRESET
:
4785 case SWM_ARG_ID_STACKINIT
:
4786 ws
->l_state
.horizontal_mwin
= 1;
4787 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
4788 ws
->l_state
.horizontal_stacks
= 1;
4790 case SWM_ARG_ID_MASTERSHRINK
:
4791 if (ws
->l_state
.horizontal_msize
> 1)
4792 ws
->l_state
.horizontal_msize
--;
4794 case SWM_ARG_ID_MASTERGROW
:
4795 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
4796 ws
->l_state
.horizontal_msize
++;
4798 case SWM_ARG_ID_MASTERADD
:
4799 ws
->l_state
.horizontal_mwin
++;
4801 case SWM_ARG_ID_MASTERDEL
:
4802 if (ws
->l_state
.horizontal_mwin
> 0)
4803 ws
->l_state
.horizontal_mwin
--;
4805 case SWM_ARG_ID_STACKINC
:
4806 ws
->l_state
.horizontal_stacks
++;
4808 case SWM_ARG_ID_STACKDEC
:
4809 if (ws
->l_state
.horizontal_stacks
> 1)
4810 ws
->l_state
.horizontal_stacks
--;
4812 case SWM_ARG_ID_FLIPLAYOUT
:
4813 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
4821 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4823 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
4825 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
4828 /* fullscreen view */
4830 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
4832 struct swm_geometry gg
= *g
;
4833 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
;
4836 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
4841 winno
= count_win(ws
, 0);
4842 if (winno
== 0 && count_win(ws
, 1) == 0)
4845 /* Figure out which top level window should be visible. */
4846 if (ws
->focus_pending
)
4847 win
= ws
->focus_pending
;
4850 else if (ws
->focus_prev
)
4851 win
= ws
->focus_prev
;
4853 win
= TAILQ_FIRST(&ws
->winlist
);
4855 DNPRINTF(SWM_D_STACK
, "max_stack: win: %#x\n", win
->id
);
4857 /* Update window geometry. */
4858 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
4867 /* Set maximized flag for all maxed windows. */
4868 if (!MAXIMIZED(w
)) {
4869 /* Preserve floating geometry. */
4871 store_float_geom(w
);
4873 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_MAXIMIZED
);
4874 ewmh_update_wm_state(w
);
4877 /* Only reconfigure if necessary. */
4878 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
4879 HEIGHT(w
) != gg
.h
) {
4881 if (bar_enabled
&& ws
->bar_enabled
){
4885 WIDTH(w
) += 2 * border_width
;
4886 HEIGHT(w
) += 2 * border_width
;
4894 parent
= find_window(win
->transient
);
4895 raise_window(parent
);
4897 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
4898 if (w
->transient
== parent
->id
)
4904 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
4905 if (w
->transient
== win
->id
)
4908 /* Map all windows. */
4909 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
4915 send_to_rg(struct swm_region
*r
, union arg
*args
)
4917 int ridx
= args
->id
, i
, num_screens
;
4918 struct swm_region
*rr
= NULL
;
4921 num_screens
= get_screen_count();
4922 /* do nothing if we don't have more than one screen */
4923 if (!(num_screens
> 1 || outputs
> 1))
4926 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
4928 rr
= TAILQ_FIRST(&r
->s
->rl
);
4929 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
4930 rr
= TAILQ_NEXT(rr
, entry
);
4941 region_under(struct swm_screen
*s
, int x
, int y
)
4943 struct swm_region
*r
= NULL
;
4945 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
4946 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
4947 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
4948 WIDTH(r
), HEIGHT(r
), x
, y
);
4949 if (X(r
) <= x
&& x
< MAX_X(r
))
4950 if (Y(r
) <= y
&& y
< MAX_Y(r
))
4958 send_to_ws(struct swm_region
*r
, union arg
*args
)
4960 int wsid
= args
->id
;
4961 struct ws_win
*win
= NULL
;
4963 if (r
&& r
->ws
&& r
->ws
->focus
)
4968 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win %#x, ws %d\n", win
->id
, wsid
);
4970 if (wsid
>= workspace_limit
)
4973 if (win
->ws
->idx
== wsid
)
4976 win_to_ws(win
, wsid
, 1);
4978 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
4979 ewmh_update_wm_state(win
);
4981 /* Restack and set new focus. */
4984 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
4985 if (r
->ws
->focus_pending
) {
4986 focus_win(r
->ws
->focus_pending
);
4987 r
->ws
->focus_pending
= NULL
;
4989 xcb_set_input_focus(conn
,
4990 XCB_INPUT_FOCUS_PARENT
, r
->id
,
4999 win_to_ws(struct ws_win
*win
, int wsid
, int unfocus
)
5001 struct ws_win
*parent
;
5002 struct workspace
*ws
, *nws
, *pws
;
5004 if (wsid
>= workspace_limit
)
5007 if (win
->ws
->idx
== wsid
)
5011 nws
= &win
->s
->ws
[wsid
];
5013 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win %#x, ws %d -> %d\n", win
->id
,
5016 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5017 ws
->focus_pending
= get_focus_prev(win
);
5019 /* Move the parent if this is a transient window. */
5021 parent
= find_window(win
->transient
);
5024 /* Set new focus in parent's ws if needed. */
5025 if (pws
->focus
== parent
) {
5026 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5027 pws
->focus_pending
=
5028 get_focus_prev(parent
);
5030 unfocus_win(parent
);
5032 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5033 pws
->focus
= pws
->focus_pending
;
5034 pws
->focus_pending
= NULL
;
5038 /* Don't unmap parent if new ws is visible */
5040 unmap_window(parent
);
5043 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
5044 TAILQ_REMOVE(&ws
->stack
, parent
, stack_entry
);
5045 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
5046 TAILQ_INSERT_TAIL(&nws
->stack
, parent
, stack_entry
);
5049 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5050 "_NET_WM_DESKTOP: %d\n", wsid
);
5051 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5052 parent
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
5053 XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5060 /* Don't unmap if new ws is visible */
5065 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
5066 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
5067 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
5068 TAILQ_INSERT_TAIL(&nws
->stack
, win
, stack_entry
);
5071 /* Set focus on new ws. */
5072 unfocus_win(nws
->focus
);
5075 /* Update the window's workspace property: _NET_WM_DESKTOP */
5076 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5077 "_NET_WM_DESKTOP: %d\n", wsid
);
5078 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
5079 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5081 ewmh_update_client_list();
5083 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
5087 pressbutton(struct swm_region
*r
, union arg
*args
)
5089 /* suppress unused warning since var is needed */
5092 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
5093 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5094 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
5095 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5099 raise_toggle(struct swm_region
*r
, union arg
*args
)
5101 /* suppress unused warning since var is needed */
5104 if (r
== NULL
|| r
->ws
== NULL
)
5107 if (r
->ws
->focus
&& MAXIMIZED(r
->ws
->focus
))
5110 r
->ws
->always_raise
= !r
->ws
->always_raise
;
5112 /* bring floaters back to top */
5113 if (!r
->ws
->always_raise
)
5120 iconify(struct swm_region
*r
, union arg
*args
)
5123 /* suppress unused warning since var is needed */
5126 if ((w
= r
->ws
->focus
) == NULL
)
5129 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_HIDDEN
);
5130 ewmh_update_wm_state(w
);
5138 get_win_name(xcb_window_t win
)
5141 xcb_get_property_cookie_t c
;
5142 xcb_get_property_reply_t
*r
;
5144 /* First try _NET_WM_NAME for UTF-8. */
5145 c
= xcb_get_property(conn
, 0, win
, ewmh
[_NET_WM_NAME
].atom
,
5146 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5147 r
= xcb_get_property_reply(conn
, c
, NULL
);
5148 if (r
&& r
->type
== XCB_NONE
) {
5150 /* Use WM_NAME instead; no UTF-8. */
5151 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
5152 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5153 r
= xcb_get_property_reply(conn
, c
, NULL
);
5156 if (r
&& r
->type
!= XCB_NONE
&& r
->length
> 0)
5157 name
= strndup(xcb_get_property_value(r
),
5158 xcb_get_property_value_length(r
));
5163 err(1, "get_win_name: failed to allocate memory.");
5171 uniconify(struct swm_region
*r
, union arg
*args
)
5178 DNPRINTF(SWM_D_MISC
, "uniconify\n");
5180 if (r
== NULL
|| r
->ws
== NULL
)
5183 /* make sure we have anything to uniconify */
5184 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5185 if (win
->ws
== NULL
)
5186 continue; /* should never happen */
5192 DNPRINTF(SWM_D_MISC
, "uniconify: count: %d\n", count
);
5198 search_resp_action
= SWM_SEARCH_UNICONIFY
;
5200 spawn_select(r
, args
, "search", &searchpid
);
5202 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5205 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5206 if (win
->ws
== NULL
)
5207 continue; /* should never happen */
5211 name
= get_win_name(win
->id
);
5212 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
5220 name_workspace(struct swm_region
*r
, union arg
*args
)
5224 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
5230 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
5232 spawn_select(r
, args
, "name_workspace", &searchpid
);
5234 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5237 fprintf(lfile
, "%s", "");
5242 search_workspace(struct swm_region
*r
, union arg
*args
)
5245 struct workspace
*ws
;
5248 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
5254 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
5256 spawn_select(r
, args
, "search", &searchpid
);
5258 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5261 for (i
= 0; i
< workspace_limit
; i
++) {
5265 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
5266 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
5273 search_win_cleanup(void)
5275 struct search_window
*sw
= NULL
;
5277 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
5278 xcb_destroy_window(conn
, sw
->indicator
);
5279 TAILQ_REMOVE(&search_wl
, sw
, entry
);
5285 search_win(struct swm_region
*r
, union arg
*args
)
5287 struct ws_win
*win
= NULL
;
5288 struct search_window
*sw
= NULL
;
5291 int i
, width
, height
;
5299 XRectangle l_ibox
, l_lbox
;
5301 DNPRINTF(SWM_D_MISC
, "search_win\n");
5304 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
5306 spawn_select(r
, args
, "search", &searchpid
);
5308 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5311 TAILQ_INIT(&search_wl
);
5314 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5318 sw
= calloc(1, sizeof(struct search_window
));
5320 warn("search_win: calloc");
5322 search_win_cleanup();
5328 snprintf(s
, sizeof s
, "%d", i
);
5331 w
= xcb_generate_id(conn
);
5332 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
5333 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
5335 if (bar_font_legacy
) {
5336 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
5337 width
= l_lbox
.width
+ 4;
5338 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
5340 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
5342 width
= info
.width
+ 4;
5343 height
= bar_font
->height
+ 4;
5346 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, w
, win
->id
, 0, 0,
5347 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
5348 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
|
5349 XCB_CW_BORDER_PIXEL
, wa
);
5351 xcb_map_window(conn
, w
);
5354 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
5356 if (bar_font_legacy
) {
5357 l_gcv
.graphics_exposures
= 0;
5358 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
5360 XSetForeground(display
, l_draw
,
5361 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
5363 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
5364 (bar_fs_extents
->max_logical_extent
.height
-
5365 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
5367 XFreeGC(display
, l_draw
);
5370 draw
= XftDrawCreate(display
, w
,
5371 DefaultVisual(display
, r
->s
->idx
),
5372 DefaultColormap(display
, r
->s
->idx
));
5374 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
5375 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
5376 bar_font
->descent
, (FcChar8
*)s
, len
);
5378 XftDrawDestroy(draw
);
5381 DNPRINTF(SWM_D_MISC
, "search_win: mapped win %#x\n", w
);
5383 fprintf(lfile
, "%d\n", i
);
5393 search_resp_uniconify(const char *resp
, size_t len
)
5399 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
5401 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
5404 name
= get_win_name(win
->id
);
5405 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
5410 if (strncmp(s
, resp
, len
) == 0) {
5411 /* XXX this should be a callback to generalize */
5412 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_HIDDEN
);
5413 ewmh_update_wm_state(win
);
5423 search_resp_name_workspace(const char *resp
, size_t len
)
5425 struct workspace
*ws
;
5427 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
5429 if (search_r
->ws
== NULL
)
5434 free(search_r
->ws
->name
);
5435 search_r
->ws
->name
= NULL
;
5439 ws
->name
= strdup(resp
);
5440 if (ws
->name
== NULL
) {
5441 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
5442 "strdup: %s", strerror(errno
));
5446 ewmh_update_desktop_names();
5447 ewmh_get_desktop_names();
5452 ewmh_update_desktop_names(void)
5454 char *name_list
= NULL
, *p
;
5455 int num_screens
, i
, j
, len
= 0, tot
= 0;
5457 num_screens
= get_screen_count();
5458 for (i
= 0; i
< num_screens
; ++i
) {
5459 for (j
= 0; j
< workspace_limit
; ++j
) {
5460 if (screens
[i
].ws
[j
].name
!= NULL
)
5461 len
+= strlen(screens
[i
].ws
[j
].name
);
5465 if((name_list
= calloc(sizeof(char *), len
)) == NULL
)
5466 err(1, "update_desktop_names: calloc: failed to "
5467 "allocate memory.");
5470 for (j
= 0; j
< workspace_limit
; ++j
) {
5471 if (screens
[i
].ws
[j
].name
!= NULL
) {
5472 len
= strlen(screens
[i
].ws
[j
].name
);
5473 memcpy(p
, screens
[i
].ws
[j
].name
, len
);
5482 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5483 screens
[i
].root
, ewmh
[_NET_DESKTOP_NAMES
].atom
,
5484 a_utf8_string
, 8, tot
, name_list
);
5494 ewmh_get_desktop_names(void)
5497 xcb_get_property_cookie_t c
;
5498 xcb_get_property_reply_t
*r
;
5499 int num_screens
, i
, j
, n
, k
;
5501 num_screens
= get_screen_count();
5502 for (i
= 0; i
< num_screens
; ++i
) {
5503 for (j
= 0; j
< workspace_limit
; ++j
) {
5504 free(screens
[i
].ws
[j
].name
);
5505 screens
[i
].ws
[j
].name
= NULL
;
5508 c
= xcb_get_property(conn
, 0, screens
[i
].root
,
5509 ewmh
[_NET_DESKTOP_NAMES
].atom
,
5510 a_utf8_string
, 0, UINT32_MAX
);
5511 r
= xcb_get_property_reply(conn
, c
, NULL
);
5515 names
= xcb_get_property_value(r
);
5516 n
= xcb_get_property_value_length(r
);
5518 for (j
= 0, k
= 0; j
< n
; ++j
) {
5519 if (*(names
+ j
) != '\0') {
5520 screens
[i
].ws
[k
].name
= strdup(names
+ j
);
5521 j
+= strlen(names
+ j
);
5530 ewmh_update_client_list(void)
5533 int num_screens
, i
, j
, k
= 0, count
= 0;
5536 num_screens
= get_screen_count();
5537 for (i
= 0; i
< num_screens
; ++i
) {
5538 for (j
= 0; j
< workspace_limit
; ++j
)
5539 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
5542 DNPRINTF(SWM_D_PROP
, "ewmh_update_client_list: win count: %d\n",
5548 wins
= calloc(sizeof(xcb_window_t
), count
);
5550 err(1, "ewmh_update_client_list: calloc: failed to "
5551 "allocate memory.");
5553 for (j
= 0, k
= 0; j
< workspace_limit
; ++j
)
5554 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
5555 wins
[k
++] = win
->id
;
5557 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5558 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
5559 XCB_ATOM_WINDOW
, 32, count
, wins
);
5566 ewmh_update_current_desktop(void)
5570 num_screens
= get_screen_count();
5571 for (i
= 0; i
< num_screens
; ++i
)
5572 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5573 screens
[i
].root
, ewmh
[_NET_CURRENT_DESKTOP
].atom
,
5574 XCB_ATOM_CARDINAL
, 32, 1, &screens
[i
].r_focus
->ws
->idx
);
5578 search_resp_search_workspace(const char *resp
)
5585 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
5589 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
5596 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
5598 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
5605 switchws(search_r
, &a
);
5609 search_resp_search_window(const char *resp
)
5614 struct search_window
*sw
;
5616 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
5620 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
5625 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
5627 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
5634 TAILQ_FOREACH(sw
, &search_wl
, entry
)
5635 if (idx
== sw
->idx
) {
5641 #define MAX_RESP_LEN 1024
5644 search_do_resp(void)
5650 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
5655 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
5656 warn("search: calloc");
5660 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
5662 warn("search: read error");
5665 resp
[rbytes
] = '\0';
5668 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
5669 * newline, so work around that by sanitizing the resp now.
5671 resp
[strcspn(resp
, "\n")] = '\0';
5674 switch (search_resp_action
) {
5675 case SWM_SEARCH_UNICONIFY
:
5676 search_resp_uniconify(resp
, len
);
5678 case SWM_SEARCH_NAME_WORKSPACE
:
5679 search_resp_name_workspace(resp
, len
);
5681 case SWM_SEARCH_SEARCH_WORKSPACE
:
5682 search_resp_search_workspace(resp
);
5684 case SWM_SEARCH_SEARCH_WINDOW
:
5685 search_resp_search_window(resp
);
5690 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
5691 search_win_cleanup();
5693 search_resp_action
= SWM_SEARCH_NONE
;
5694 close(select_resp_pipe
[0]);
5701 wkill(struct swm_region
*r
, union arg
*args
)
5703 DNPRINTF(SWM_D_MISC
, "wkill: win %#x, id: %d\n", WINID(r
->ws
->focus
),
5706 if (r
->ws
->focus
== NULL
)
5709 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
5710 xcb_kill_client(conn
, r
->ws
->focus
->id
);
5712 if (r
->ws
->focus
->can_delete
)
5713 client_msg(r
->ws
->focus
, a_delete
, 0);
5719 clear_maximized(struct workspace
*ws
)
5724 /* Clear any maximized win(s) on ws, from bottom up. */
5725 TAILQ_FOREACH_REVERSE(w
, &ws
->stack
, ws_win_stack
, stack_entry
)
5727 ewmh_apply_flags(w
, w
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5728 ewmh_update_wm_state(w
);
5736 maximize_toggle(struct swm_region
*r
, union arg
*args
)
5738 struct ws_win
*w
= r
->ws
->focus
;
5740 /* suppress unused warning since var is needed */
5746 DNPRINTF(SWM_D_MISC
, "maximize_toggle: win %#x\n", w
->id
);
5751 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
5754 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_MAXIMIZED
);
5755 ewmh_update_wm_state(w
);
5759 if (w
== w
->ws
->focus
)
5763 DNPRINTF(SWM_D_MISC
, "maximize_toggle: done\n");
5767 floating_toggle(struct swm_region
*r
, union arg
*args
)
5769 struct ws_win
*win
= r
->ws
->focus
;
5771 /* suppress unused warning since var is needed */
5777 if (FULLSCREEN(win
) || TRANS(win
))
5780 ewmh_apply_flags(win
, win
->ewmh_flags
^ EWMH_F_ABOVE
);
5781 ewmh_update_wm_state(win
);
5785 if (win
== win
->ws
->focus
)
5792 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
5794 struct swm_geometry g
= r
->g
;
5795 int rt
, lt
, tp
, bm
, bw
;
5797 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
5800 * Perpendicular distance of each side of the window to the respective
5801 * side of the region boundary. Positive values indicate the side of
5802 * the window has passed beyond the region boundary.
5804 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) + BORDER(win
) - MAX_X(r
) : bw
;
5805 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) + BORDER(win
) : bw
;
5806 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) + BORDER(win
) - MAX_Y(r
) : bw
;
5807 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) + BORDER(win
) : bw
;
5809 DNPRINTF(SWM_D_MISC
, "region_containment: win %#x, rt: %d, lt: %d, "
5810 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
5811 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
5812 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
5815 * Disable containment if any of the flagged sides went beyond the
5816 * containment boundary, or if containment is disabled.
5818 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
5819 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
5820 /* Make sure window has at least 1 pixel in the region */
5821 g
.x
+= 1 - WIDTH(win
);
5822 g
.y
+= 1 - HEIGHT(win
);
5823 g
.w
+= 2 * WIDTH(win
) - 2;
5824 g
.h
+= 2 * HEIGHT(win
) - 2;
5827 constrain_window(win
, &g
, &opts
);
5830 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
5832 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
5834 DNPRINTF(SWM_D_MISC
, "constrain_window: win %#x, (x,y) w x h: "
5835 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
5836 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
5837 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
5838 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
5839 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
5840 YESNO(*opts
& SWM_CW_RESIZABLE
));
5842 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) + BORDER(win
) > b
->x
+ b
->w
) {
5843 if (*opts
& SWM_CW_RESIZABLE
)
5844 WIDTH(win
) = b
->x
+ b
->w
- X(win
) - BORDER(win
);
5846 X(win
) = b
->x
+ b
->w
- WIDTH(win
) - BORDER(win
);
5849 if ((*opts
& SWM_CW_LEFT
) && X(win
) + BORDER(win
) < b
->x
) {
5850 if (*opts
& SWM_CW_RESIZABLE
)
5851 WIDTH(win
) -= b
->x
- X(win
) - BORDER(win
);
5853 X(win
) = b
->x
- BORDER(win
);
5856 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) + BORDER(win
) > b
->y
+ b
->h
) {
5857 if (*opts
& SWM_CW_RESIZABLE
)
5858 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
) - BORDER(win
);
5860 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
) - BORDER(win
);
5863 if ((*opts
& SWM_CW_TOP
) && Y(win
) + BORDER(win
) < b
->y
) {
5864 if (*opts
& SWM_CW_RESIZABLE
)
5865 HEIGHT(win
) -= b
->y
- Y(win
) - BORDER(win
);
5867 Y(win
) = b
->y
- BORDER(win
);
5870 if (*opts
& SWM_CW_RESIZABLE
) {
5873 if (HEIGHT(win
) < 1)
5879 update_window(struct ws_win
*win
)
5884 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
5885 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
5886 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
5890 wc
[3] = HEIGHT(win
);
5891 wc
[4] = BORDER(win
);
5893 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x, (x,y) w x h: "
5894 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
5895 wc
[3], YESNO(win
->bordered
));
5897 xcb_configure_window(conn
, win
->id
, mask
, wc
);
5900 #define SWM_RESIZE_STEPS (50)
5903 resize(struct ws_win
*win
, union arg
*args
)
5905 xcb_timestamp_t timestamp
= 0;
5906 struct swm_region
*r
= NULL
;
5907 struct swm_geometry g
;
5909 int top
= 0, left
= 0, resizing
;
5911 xcb_cursor_t cursor
;
5912 xcb_query_pointer_reply_t
*xpr
= NULL
;
5913 xcb_generic_event_t
*evt
;
5914 xcb_motion_notify_event_t
*mne
;
5920 if (FULLSCREEN(win
))
5923 /* In max_stack mode, should only resize transients. */
5924 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
5927 DNPRINTF(SWM_D_EVENT
, "resize: win %#x, floating: %s, "
5928 "transient: %#x\n", win
->id
, YESNO(ABOVE(win
)),
5932 store_float_geom(win
);
5933 else if (!(TRANS(win
) || ABOVE(win
)))
5936 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
5938 ewmh_update_wm_state(win
);
5944 /* It's possible for win to have been freed during focus_flush(). */
5945 if (validate_win(win
)) {
5946 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
5951 case SWM_ARG_ID_WIDTHSHRINK
:
5952 WIDTH(win
) -= SWM_RESIZE_STEPS
;
5955 case SWM_ARG_ID_WIDTHGROW
:
5956 WIDTH(win
) += SWM_RESIZE_STEPS
;
5959 case SWM_ARG_ID_HEIGHTSHRINK
:
5960 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
5963 case SWM_ARG_ID_HEIGHTGROW
:
5964 HEIGHT(win
) += SWM_RESIZE_STEPS
;
5971 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
5972 SWM_CW_HARDBOUNDARY
);
5974 store_float_geom(win
);
5978 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
5979 SWM_CW_SOFTBOUNDARY
);
5982 /* get cursor offset from window root */
5983 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
5990 if (xpr
->win_x
< WIDTH(win
) / 2)
5993 if (xpr
->win_y
< HEIGHT(win
) / 2)
5996 if (args
->id
== SWM_ARG_ID_CENTER
)
5997 cursor
= cursors
[XC_SIZING
].cid
;
5999 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
6000 XC_TOP_RIGHT_CORNER
].cid
;
6002 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
6003 XC_BOTTOM_RIGHT_CORNER
].cid
;
6005 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6006 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
6011 while (resizing
&& (evt
= xcb_wait_for_event(conn
))) {
6012 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6013 case XCB_BUTTON_RELEASE
:
6014 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_RELEASE\n");
6017 case XCB_MOTION_NOTIFY
:
6018 mne
= (xcb_motion_notify_event_t
*)evt
;
6019 /* cursor offset/delta from start of the operation */
6020 dx
= mne
->root_x
- xpr
->root_x
;
6021 dy
= mne
->root_y
- xpr
->root_y
;
6027 if (args
->id
== SWM_ARG_ID_CENTER
) {
6028 if (g
.h
/ 2 + dy
< 1)
6032 HEIGHT(win
) = g
.h
+ 2 * dy
;
6040 HEIGHT(win
) = g
.h
+ dy
;
6047 if (args
->id
== SWM_ARG_ID_CENTER
) {
6048 if (g
.w
/ 2 + dx
< 1)
6052 WIDTH(win
) = g
.w
+ 2 * dx
;
6060 WIDTH(win
) = g
.w
+ dx
;
6063 /* not free, don't sync more than 120 times / second */
6064 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6065 timestamp
= mne
->time
;
6066 regionize(win
, mne
->root_x
, mne
->root_y
);
6067 region_containment(win
, r
, SWM_CW_ALLSIDES
|
6068 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
6069 SWM_CW_SOFTBOUNDARY
);
6077 /* It's possible for win to have been freed above. */
6078 if (validate_win(win
)) {
6079 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6087 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6088 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
6092 store_float_geom(win
);
6094 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6096 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
6100 resize_step(struct swm_region
*r
, union arg
*args
)
6102 struct ws_win
*win
= NULL
;
6104 if (r
&& r
->ws
&& r
->ws
->focus
)
6113 /* Try to set window region based on supplied coordinates or window center. */
6115 regionize(struct ws_win
*win
, int x
, int y
)
6117 struct swm_region
*r
= NULL
;
6119 r
= region_under(win
->s
, x
, y
);
6121 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
6122 Y(win
) + HEIGHT(win
) / 2);
6124 if (r
&& r
!= win
->ws
->r
) {
6125 win_to_ws(win
, r
->ws
->idx
, 0);
6130 #define SWM_MOVE_STEPS (50)
6133 move(struct ws_win
*win
, union arg
*args
)
6135 struct swm_region
*r
;
6136 xcb_timestamp_t timestamp
= 0;
6137 int move_stp
= 0, moving
, restack
= 0;
6138 xcb_query_pointer_reply_t
*qpr
= NULL
;
6139 xcb_generic_event_t
*evt
;
6140 xcb_motion_notify_event_t
*mne
;
6145 if ((r
= win
->ws
->r
) == NULL
)
6148 if (FULLSCREEN(win
))
6151 DNPRINTF(SWM_D_EVENT
, "move: win %#x, floating: %s, transient: "
6152 "%#x\n", win
->id
, YESNO(ABOVE(win
)), win
->transient
);
6154 /* in max_stack mode should only move transients */
6155 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6158 if (!(ABOVE(win
) || TRANS(win
)) || MAXIMIZED(win
)) {
6159 store_float_geom(win
);
6163 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6165 ewmh_update_wm_state(win
);
6172 /* It's possible for win to have been freed during focus_flush(). */
6173 if (validate_win(win
)) {
6174 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
6180 case SWM_ARG_ID_MOVELEFT
:
6181 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
6184 case SWM_ARG_ID_MOVERIGHT
:
6185 X(win
) += (SWM_MOVE_STEPS
- border_width
);
6188 case SWM_ARG_ID_MOVEUP
:
6189 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
6192 case SWM_ARG_ID_MOVEDOWN
:
6193 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
6200 regionize(win
, -1, -1);
6201 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
6203 store_float_geom(win
);
6207 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6208 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
6209 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
6211 /* get cursor offset from window root */
6212 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6215 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6219 regionize(win
, qpr
->root_x
, qpr
->root_y
);
6220 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
6221 SWM_CW_SOFTBOUNDARY
);
6225 while (moving
&& (evt
= xcb_wait_for_event(conn
))) {
6226 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6227 case XCB_BUTTON_RELEASE
:
6228 DNPRINTF(SWM_D_EVENT
, "move: BUTTON_RELEASE\n");
6231 case XCB_MOTION_NOTIFY
:
6232 mne
= (xcb_motion_notify_event_t
*)evt
;
6233 DNPRINTF(SWM_D_EVENT
, "motion: root: %#x\n", mne
->root
);
6234 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
6235 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
6237 /* not free, don't sync more than 120 times / second */
6238 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6239 timestamp
= mne
->time
;
6240 regionize(win
, mne
->root_x
, mne
->root_y
);
6241 region_containment(win
, win
->ws
->r
,
6242 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
6250 /* It's possible for win to have been freed above. */
6251 if (validate_win(win
)) {
6252 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
6260 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
6261 SWM_CW_SOFTBOUNDARY
);
6265 store_float_geom(win
);
6268 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6269 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
6273 move_step(struct swm_region
*r
, union arg
*args
)
6275 struct ws_win
*win
= NULL
;
6277 if (r
&& r
->ws
&& r
->ws
->focus
)
6282 if (!TRANS(win
) && !ABOVE(win
))
6289 /* key definitions */
6291 char name
[SWM_FUNCNAME_LEN
];
6292 void (*func
)(struct swm_region
*r
, union arg
*);
6294 } keyfuncs
[KF_INVALID
+ 1] = {
6295 /* name function argument */
6296 { "bar_toggle", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
6297 { "bar_toggle_ws", bar_toggle
, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
6298 { "button2", pressbutton
, {2} },
6299 { "cycle_layout", cycle_layout
, {0} },
6300 { "flip_layout", stack_config
, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
6301 { "float_toggle", floating_toggle
,{0} },
6302 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
6303 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
6304 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
6305 { "focus_urgent", focus
, {.id
= SWM_ARG_ID_FOCUSURGENT
} },
6306 { "maximize_toggle", maximize_toggle
,{0} },
6307 { "height_grow", resize_step
, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
6308 { "height_shrink", resize_step
, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
6309 { "iconify", iconify
, {0} },
6310 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
6311 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
6312 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
6313 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
6314 { "move_down", move_step
, {.id
= SWM_ARG_ID_MOVEDOWN
} },
6315 { "move_left", move_step
, {.id
= SWM_ARG_ID_MOVELEFT
} },
6316 { "move_right", move_step
, {.id
= SWM_ARG_ID_MOVERIGHT
} },
6317 { "move_up", move_step
, {.id
= SWM_ARG_ID_MOVEUP
} },
6318 { "mvrg_1", send_to_rg
, {.id
= 0} },
6319 { "mvrg_2", send_to_rg
, {.id
= 1} },
6320 { "mvrg_3", send_to_rg
, {.id
= 2} },
6321 { "mvrg_4", send_to_rg
, {.id
= 3} },
6322 { "mvrg_5", send_to_rg
, {.id
= 4} },
6323 { "mvrg_6", send_to_rg
, {.id
= 5} },
6324 { "mvrg_7", send_to_rg
, {.id
= 6} },
6325 { "mvrg_8", send_to_rg
, {.id
= 7} },
6326 { "mvrg_9", send_to_rg
, {.id
= 8} },
6327 { "mvws_1", send_to_ws
, {.id
= 0} },
6328 { "mvws_2", send_to_ws
, {.id
= 1} },
6329 { "mvws_3", send_to_ws
, {.id
= 2} },
6330 { "mvws_4", send_to_ws
, {.id
= 3} },
6331 { "mvws_5", send_to_ws
, {.id
= 4} },
6332 { "mvws_6", send_to_ws
, {.id
= 5} },
6333 { "mvws_7", send_to_ws
, {.id
= 6} },
6334 { "mvws_8", send_to_ws
, {.id
= 7} },
6335 { "mvws_9", send_to_ws
, {.id
= 8} },
6336 { "mvws_10", send_to_ws
, {.id
= 9} },
6337 { "mvws_11", send_to_ws
, {.id
= 10} },
6338 { "mvws_12", send_to_ws
, {.id
= 11} },
6339 { "mvws_13", send_to_ws
, {.id
= 12} },
6340 { "mvws_14", send_to_ws
, {.id
= 13} },
6341 { "mvws_15", send_to_ws
, {.id
= 14} },
6342 { "mvws_16", send_to_ws
, {.id
= 15} },
6343 { "mvws_17", send_to_ws
, {.id
= 16} },
6344 { "mvws_18", send_to_ws
, {.id
= 17} },
6345 { "mvws_19", send_to_ws
, {.id
= 18} },
6346 { "mvws_20", send_to_ws
, {.id
= 19} },
6347 { "mvws_21", send_to_ws
, {.id
= 20} },
6348 { "mvws_22", send_to_ws
, {.id
= 21} },
6349 { "name_workspace", name_workspace
, {0} },
6350 { "quit", quit
, {0} },
6351 { "raise_toggle", raise_toggle
, {0} },
6352 { "restart", restart
, {0} },
6353 { "rg_1", focusrg
, {.id
= 0} },
6354 { "rg_2", focusrg
, {.id
= 1} },
6355 { "rg_3", focusrg
, {.id
= 2} },
6356 { "rg_4", focusrg
, {.id
= 3} },
6357 { "rg_5", focusrg
, {.id
= 4} },
6358 { "rg_6", focusrg
, {.id
= 5} },
6359 { "rg_7", focusrg
, {.id
= 6} },
6360 { "rg_8", focusrg
, {.id
= 7} },
6361 { "rg_9", focusrg
, {.id
= 8} },
6362 { "rg_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
6363 { "rg_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
6364 { "screen_next", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
6365 { "screen_prev", cyclerg
, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
6366 { "search_win", search_win
, {0} },
6367 { "search_workspace", search_workspace
, {0} },
6368 { "spawn_custom", NULL
, {0} },
6369 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
6370 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
6371 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
6372 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
6373 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
6374 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
6375 { "uniconify", uniconify
, {0} },
6376 { "version", version
, {0} },
6377 { "width_grow", resize_step
, {.id
= SWM_ARG_ID_WIDTHGROW
} },
6378 { "width_shrink", resize_step
, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
6379 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
6380 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
6381 { "ws_1", switchws
, {.id
= 0} },
6382 { "ws_2", switchws
, {.id
= 1} },
6383 { "ws_3", switchws
, {.id
= 2} },
6384 { "ws_4", switchws
, {.id
= 3} },
6385 { "ws_5", switchws
, {.id
= 4} },
6386 { "ws_6", switchws
, {.id
= 5} },
6387 { "ws_7", switchws
, {.id
= 6} },
6388 { "ws_8", switchws
, {.id
= 7} },
6389 { "ws_9", switchws
, {.id
= 8} },
6390 { "ws_10", switchws
, {.id
= 9} },
6391 { "ws_11", switchws
, {.id
= 10} },
6392 { "ws_12", switchws
, {.id
= 11} },
6393 { "ws_13", switchws
, {.id
= 12} },
6394 { "ws_14", switchws
, {.id
= 13} },
6395 { "ws_15", switchws
, {.id
= 14} },
6396 { "ws_16", switchws
, {.id
= 15} },
6397 { "ws_17", switchws
, {.id
= 16} },
6398 { "ws_18", switchws
, {.id
= 17} },
6399 { "ws_19", switchws
, {.id
= 18} },
6400 { "ws_20", switchws
, {.id
= 19} },
6401 { "ws_21", switchws
, {.id
= 20} },
6402 { "ws_22", switchws
, {.id
= 21} },
6403 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
6404 { "ws_next_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
6405 { "ws_next_move", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
6406 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
6407 { "ws_prev_all", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
6408 { "ws_prev_move", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
6409 { "ws_prior", priorws
, {0} },
6410 { "dumpwins", dumpwins
, {0} }, /* MUST BE LAST */
6411 { "invalid key func", NULL
, {0} },
6415 key_cmp(struct key
*kp1
, struct key
*kp2
)
6417 if (kp1
->keysym
< kp2
->keysym
)
6419 if (kp1
->keysym
> kp2
->keysym
)
6422 if (kp1
->mod
< kp2
->mod
)
6424 if (kp1
->mod
> kp2
->mod
)
6431 enum { client_click
, root_click
};
6433 unsigned int action
;
6435 unsigned int button
;
6436 void (*func
)(struct ws_win
*, union arg
*);
6439 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
6440 /* action key mouse button func args */
6441 { client_click
, MODKEY
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
6442 { client_click
, MODKEY_SHIFT
, XCB_BUTTON_INDEX_3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
6443 { client_click
, MODKEY
, XCB_BUTTON_INDEX_1
, move
, {0} },
6448 update_modkey(unsigned int mod
)
6454 RB_FOREACH(kp
, key_tree
, &keys
)
6455 if (kp
->mod
& XCB_MOD_MASK_SHIFT
)
6456 kp
->mod
= mod
| XCB_MOD_MASK_SHIFT
;
6460 for (i
= 0; i
< LENGTH(buttons
); i
++)
6461 if (buttons
[i
].mask
& XCB_MOD_MASK_SHIFT
)
6462 buttons
[i
].mask
= mod
| XCB_MOD_MASK_SHIFT
;
6464 buttons
[i
].mask
= mod
;
6468 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
6471 struct spawn_prog
*prog
= NULL
;
6473 char *ap
, **real_args
;
6475 /* suppress unused warning since var is needed */
6478 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
6481 TAILQ_FOREACH(prog
, &spawns
, entry
) {
6482 if (strcasecmp(spawn_name
, prog
->name
) == 0)
6486 warnx("spawn_custom: program %s not found", spawn_name
);
6490 /* make room for expanded args */
6491 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
6492 err(1, "spawn_custom: calloc real_args");
6494 /* expand spawn_args into real_args */
6495 for (i
= c
= 0; i
< prog
->argc
; i
++) {
6497 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
6498 if (strcasecmp(ap
, "$bar_border") == 0) {
6500 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
6502 err(1, "spawn_custom border color");
6503 } else if (strcasecmp(ap
, "$bar_color") == 0) {
6505 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
6507 err(1, "spawn_custom bar color");
6508 } else if (strcasecmp(ap
, "$bar_font") == 0) {
6509 if ((real_args
[c
] = strdup(bar_fonts
))
6511 err(1, "spawn_custom bar fonts");
6512 } else if (strcasecmp(ap
, "$bar_font_color") == 0) {
6514 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
6516 err(1, "spawn_custom color font");
6517 } else if (strcasecmp(ap
, "$color_focus") == 0) {
6519 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
6521 err(1, "spawn_custom color focus");
6522 } else if (strcasecmp(ap
, "$color_unfocus") == 0) {
6524 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
6526 err(1, "spawn_custom color unfocus");
6527 } else if (strcasecmp(ap
, "$region_index") == 0) {
6528 if (asprintf(&real_args
[c
], "%d",
6529 get_region_index(r
) + 1) < 1)
6530 err(1, "spawn_custom region index");
6531 } else if (strcasecmp(ap
, "$workspace_index") == 0) {
6532 if (asprintf(&real_args
[c
], "%d", r
->ws
->idx
+ 1) < 1)
6533 err(1, "spawn_custom workspace index");
6534 } else if (strcasecmp(ap
, "$dmenu_bottom") == 0) {
6537 if ((real_args
[c
] = strdup("-b")) == NULL
)
6538 err(1, "spawn_custom workspace index");
6540 /* no match --> copy as is */
6541 if ((real_args
[c
] = strdup(ap
)) == NULL
)
6542 err(1, "spawn_custom strdup(ap)");
6544 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
6550 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
6551 for (i
= 0; i
< c
; ++i
)
6552 DPRINTF("\"%s\" ", real_args
[i
]);
6555 *ret_args
= real_args
;
6560 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
6566 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
6570 spawn(r
->ws
->idx
, &a
, 1);
6572 for (i
= 0; i
< spawn_argc
; i
++)
6578 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
6585 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
6589 if (pipe(select_list_pipe
) == -1)
6590 err(1, "pipe error");
6591 if (pipe(select_resp_pipe
) == -1)
6592 err(1, "pipe error");
6594 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
6595 err(1, "could not disable SIGPIPE");
6596 switch (*pid
= fork()) {
6598 err(1, "cannot fork");
6601 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
6603 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
6605 close(select_list_pipe
[1]);
6606 close(select_resp_pipe
[0]);
6607 spawn(r
->ws
->idx
, &a
, 0);
6609 default: /* parent */
6610 close(select_list_pipe
[0]);
6611 close(select_resp_pipe
[1]);
6615 for (i
= 0; i
< spawn_argc
; i
++)
6620 /* Argument tokenizer. */
6623 int single_quoted
= 0, double_quoted
= 0;
6624 char *arg
, *cp
, *next
;
6629 /* Eat and move characters until end of argument is found. */
6630 for (arg
= next
= cp
= *sp
; *cp
!= '\0'; ++cp
) {
6631 if (!double_quoted
&& *cp
== '\'') {
6632 /* Eat single-quote. */
6633 single_quoted
= !single_quoted
;
6634 } else if (!single_quoted
&& *cp
== '"') {
6635 /* Eat double-quote. */
6636 double_quoted
= !double_quoted
;
6637 } else if (!single_quoted
&& *cp
== '\\' && *(cp
+ 1) == '"') {
6638 /* Eat backslash; copy escaped character to arg. */
6640 } else if (!single_quoted
&& !double_quoted
&& *cp
== '\\' &&
6641 (*(cp
+ 1) == '\'' || *(cp
+ 1) == ' ')) {
6642 /* Eat backslash; move escaped character. */
6644 } else if (!single_quoted
&& !double_quoted
&&
6645 (*cp
== ' ' || *cp
== '\t')) {
6646 /* Terminate argument. */
6648 /* Point sp to beginning of next argument. */
6652 /* Move regular character. */
6657 /* Terminate argument if end of string. */
6667 spawn_insert(const char *name
, const char *args
, int flags
)
6669 struct spawn_prog
*sp
;
6670 char *arg
, *dup
, *ptr
;
6672 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s[%s]\n", name
, args
);
6674 if (args
== NULL
|| *args
== '\0')
6677 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
6678 err(1, "spawn_insert: calloc");
6679 if ((sp
->name
= strdup(name
)) == NULL
)
6680 err(1, "spawn_insert: strdup");
6682 /* Convert the arguments to an argument list. */
6683 if ((ptr
= dup
= strdup(args
)) == NULL
)
6684 err(1, "spawn_insert: strdup");
6685 while ((arg
= argsep(&ptr
)) != NULL
) {
6686 /* Null argument; skip it. */
6691 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
6692 sizeof *sp
->argv
)) == NULL
)
6693 err(1, "spawn_insert: realloc");
6694 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
6695 err(1, "spawn_insert: strdup");
6701 DNPRINTF(SWM_D_SPAWN
, "arg %d: [%s]\n", sp
->argc
, sp
->argv
[sp
->argc
-1]);
6702 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
6703 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
6707 spawn_remove(struct spawn_prog
*sp
)
6711 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
6713 TAILQ_REMOVE(&spawns
, sp
, entry
);
6714 for (i
= 0; i
< sp
->argc
; i
++)
6720 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
6724 spawn_find(const char *name
)
6726 struct spawn_prog
*sp
;
6728 TAILQ_FOREACH(sp
, &spawns
, entry
)
6729 if (strcasecmp(sp
->name
, name
) == 0)
6736 setspawn(const char *name
, const char *args
, int flags
)
6738 struct spawn_prog
*sp
;
6740 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
6745 /* Remove any old spawn under the same name. */
6746 if ((sp
= spawn_find(name
)) != NULL
)
6750 spawn_insert(name
, args
, flags
);
6752 warnx("error: setspawn: cannot find program: %s", name
);
6754 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
6758 setconfspawn(const char *selector
, const char *value
, int flags
)
6762 if (selector
== NULL
|| strlen(selector
) == 0)
6765 args
= expand_tilde(value
);
6767 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
6769 setspawn(selector
, args
, flags
);
6772 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
6777 validate_spawns(void)
6779 struct spawn_prog
*sp
;
6780 char which
[PATH_MAX
];
6785 RB_FOREACH(kp
, key_tree
, &keys
) {
6786 if (kp
->funcid
!= KF_SPAWN_CUSTOM
)
6790 sp
= spawn_find(kp
->spawn_name
);
6791 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
6794 /* verify we have the goods */
6795 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
6796 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
6798 for (i
= strlen("which "); i
< strlen(which
); i
++)
6799 if (which
[i
] == ' ') {
6803 if (system(which
) != 0)
6804 add_startup_exception("could not find %s",
6805 &which
[strlen("which ")]);
6812 setconfspawn("lock", "xlock", 0);
6814 setconfspawn("term", "xterm", 0);
6815 setconfspawn("spawn_term", "xterm", 0);
6817 setconfspawn("menu", "dmenu_run"
6821 " -nf $bar_font_color"
6823 " -sf $bar_color", 0);
6825 setconfspawn("search", "dmenu"
6830 " -nf $bar_font_color"
6832 " -sf $bar_color", 0);
6834 setconfspawn("name_workspace", "dmenu"
6839 " -nf $bar_font_color"
6841 " -sf $bar_color", 0);
6843 /* These are not verified for existence, even with a binding set. */
6844 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
6845 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
6846 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
6850 #define SWM_MODNAME_SIZE 32
6851 #define SWM_KEY_WS "\n+ \t"
6853 parsekeys(const char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
6855 char *str
, *cp
, *name
;
6858 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
6859 if (mod
== NULL
|| ks
== NULL
) {
6860 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
6863 if (keystr
== NULL
|| strlen(keystr
) == 0) {
6864 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
6868 if ((cp
= str
= strdup(keystr
)) == NULL
)
6869 err(1, "parsekeys: strdup");
6871 *ks
= XCB_NO_SYMBOL
;
6873 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
6874 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
6876 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
6877 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
6879 else if (strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
) == 0)
6880 *mod
|= XCB_MOD_MASK_1
;
6881 else if (strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
) == 0)
6882 *mod
+= XCB_MOD_MASK_2
;
6883 else if (strncmp(name
, "Mod3", SWM_MODNAME_SIZE
) == 0)
6884 *mod
|= XCB_MOD_MASK_3
;
6885 else if (strncmp(name
, "Mod4", SWM_MODNAME_SIZE
) == 0)
6886 *mod
|= XCB_MOD_MASK_4
;
6887 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
6888 *mod
|= XCB_MOD_MASK_SHIFT
;
6889 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
6890 *mod
|= XCB_MOD_MASK_CONTROL
;
6892 *ks
= XStringToKeysym(name
);
6893 XConvertCase(*ks
, ks
, &uks
);
6894 if (ks
== XCB_NO_SYMBOL
) {
6896 "parsekeys: invalid key %s\n",
6905 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
6910 strdupsafe(const char *str
)
6915 return (strdup(str
));
6919 key_insert(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
6920 const char *spawn_name
)
6924 DNPRINTF(SWM_D_KEY
, "key_insert: enter %s [%s]\n",
6925 keyfuncs
[kfid
].name
, spawn_name
);
6927 if ((kp
= malloc(sizeof *kp
)) == NULL
)
6928 err(1, "key_insert: malloc");
6933 kp
->spawn_name
= strdupsafe(spawn_name
);
6934 RB_INSERT(key_tree
, &keys
, kp
);
6936 DNPRINTF(SWM_D_KEY
, "key_insert: leave\n");
6940 key_lookup(unsigned int mod
, KeySym ks
)
6947 return (RB_FIND(key_tree
, &keys
, &kp
));
6951 key_remove(struct key
*kp
)
6953 DNPRINTF(SWM_D_KEY
, "key_remove: %s\n", keyfuncs
[kp
->funcid
].name
);
6955 RB_REMOVE(key_tree
, &keys
, kp
);
6956 free(kp
->spawn_name
);
6959 DNPRINTF(SWM_D_KEY
, "key_remove: leave\n");
6963 key_replace(struct key
*kp
, unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
6964 const char *spawn_name
)
6966 DNPRINTF(SWM_D_KEY
, "key_replace: %s [%s]\n", keyfuncs
[kp
->funcid
].name
,
6970 key_insert(mod
, ks
, kfid
, spawn_name
);
6972 DNPRINTF(SWM_D_KEY
, "key_replace: leave\n");
6976 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
,
6977 const char *spawn_name
)
6981 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
6982 keyfuncs
[kfid
].name
, spawn_name
);
6984 if ((kp
= key_lookup(mod
, ks
)) != NULL
) {
6985 if (kfid
== KF_INVALID
)
6988 key_replace(kp
, mod
, ks
, kfid
, spawn_name
);
6989 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
6992 if (kfid
== KF_INVALID
) {
6993 warnx("bind: Key combination already unbound.");
6994 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
6998 key_insert(mod
, ks
, kfid
, spawn_name
);
6999 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
7003 setconfbinding(const char *selector
, const char *value
, int flags
)
7005 enum keyfuncid kfid
;
7008 struct spawn_prog
*sp
;
7010 /* suppress unused warning since var is needed */
7013 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter selector: [%s], "
7014 "value: [%s]\n", selector
, value
);
7015 if (selector
== NULL
|| strlen(selector
) == 0) {
7016 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
7017 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7019 setkeybinding(mod
, ks
, kfid
, NULL
);
7024 /* search by key function name */
7025 for (kfid
= 0; kfid
< KF_INVALID
; (kfid
)++) {
7026 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
7027 SWM_FUNCNAME_LEN
) == 0) {
7028 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
7029 "keyfunc\n", selector
);
7030 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7031 setkeybinding(mod
, ks
, kfid
, NULL
);
7037 /* search by custom spawn name */
7038 if ((sp
= spawn_find(selector
)) != NULL
) {
7039 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
7040 "spawn\n", selector
);
7041 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
7042 setkeybinding(mod
, ks
, KF_SPAWN_CUSTOM
,
7048 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
7055 #define MODKEY_SHIFT MODKEY | XCB_MOD_MASK_SHIFT
7056 setkeybinding(MODKEY
, XK_b
, KF_BAR_TOGGLE
, NULL
);
7057 setkeybinding(MODKEY_SHIFT
, XK_b
, KF_BAR_TOGGLE_WS
,NULL
);
7058 setkeybinding(MODKEY
, XK_v
, KF_BUTTON2
, NULL
);
7059 setkeybinding(MODKEY
, XK_space
, KF_CYCLE_LAYOUT
,NULL
);
7060 setkeybinding(MODKEY_SHIFT
, XK_backslash
, KF_FLIP_LAYOUT
, NULL
);
7061 setkeybinding(MODKEY
, XK_t
, KF_FLOAT_TOGGLE
,NULL
);
7062 setkeybinding(MODKEY
, XK_m
, KF_FOCUS_MAIN
, NULL
);
7063 setkeybinding(MODKEY
, XK_j
, KF_FOCUS_NEXT
, NULL
);
7064 setkeybinding(MODKEY
, XK_Tab
, KF_FOCUS_NEXT
, NULL
);
7065 setkeybinding(MODKEY
, XK_k
, KF_FOCUS_PREV
, NULL
);
7066 setkeybinding(MODKEY_SHIFT
, XK_Tab
, KF_FOCUS_PREV
, NULL
);
7067 setkeybinding(MODKEY
, XK_u
, KF_FOCUS_URGENT
,NULL
);
7068 setkeybinding(MODKEY
, XK_e
, KF_MAXIMIZE_TOGGLE
,NULL
);
7069 setkeybinding(MODKEY_SHIFT
, XK_equal
, KF_HEIGHT_GROW
,NULL
);
7070 setkeybinding(MODKEY_SHIFT
, XK_minus
, KF_HEIGHT_SHRINK
,NULL
);
7071 setkeybinding(MODKEY
, XK_w
, KF_ICONIFY
, NULL
);
7072 setkeybinding(MODKEY
, XK_h
, KF_MASTER_SHRINK
, NULL
);
7073 setkeybinding(MODKEY
, XK_l
, KF_MASTER_GROW
, NULL
);
7074 setkeybinding(MODKEY
, XK_comma
, KF_MASTER_ADD
, NULL
);
7075 setkeybinding(MODKEY
, XK_period
, KF_MASTER_DEL
, NULL
);
7076 setkeybinding(MODKEY_SHIFT
, XK_bracketright
,KF_MOVE_DOWN
,NULL
);
7077 setkeybinding(MODKEY
, XK_bracketleft
, KF_MOVE_LEFT
,NULL
);
7078 setkeybinding(MODKEY
, XK_bracketright
,KF_MOVE_RIGHT
,NULL
);
7079 setkeybinding(MODKEY_SHIFT
, XK_bracketleft
, KF_MOVE_UP
, NULL
);
7080 setkeybinding(MODKEY_SHIFT
, XK_KP_End
, KF_MVRG_1
, NULL
);
7081 setkeybinding(MODKEY_SHIFT
, XK_KP_Down
, KF_MVRG_2
, NULL
);
7082 setkeybinding(MODKEY_SHIFT
, XK_KP_Next
, KF_MVRG_3
, NULL
);
7083 setkeybinding(MODKEY_SHIFT
, XK_KP_Left
, KF_MVRG_4
, NULL
);
7084 setkeybinding(MODKEY_SHIFT
, XK_KP_Begin
, KF_MVRG_5
, NULL
);
7085 setkeybinding(MODKEY_SHIFT
, XK_KP_Right
, KF_MVRG_6
, NULL
);
7086 setkeybinding(MODKEY_SHIFT
, XK_KP_Home
, KF_MVRG_7
, NULL
);
7087 setkeybinding(MODKEY_SHIFT
, XK_KP_Up
, KF_MVRG_8
, NULL
);
7088 setkeybinding(MODKEY_SHIFT
, XK_KP_Prior
, KF_MVRG_9
, NULL
);
7089 setkeybinding(MODKEY_SHIFT
, XK_1
, KF_MVWS_1
, NULL
);
7090 setkeybinding(MODKEY_SHIFT
, XK_2
, KF_MVWS_2
, NULL
);
7091 setkeybinding(MODKEY_SHIFT
, XK_3
, KF_MVWS_3
, NULL
);
7092 setkeybinding(MODKEY_SHIFT
, XK_4
, KF_MVWS_4
, NULL
);
7093 setkeybinding(MODKEY_SHIFT
, XK_5
, KF_MVWS_5
, NULL
);
7094 setkeybinding(MODKEY_SHIFT
, XK_6
, KF_MVWS_6
, NULL
);
7095 setkeybinding(MODKEY_SHIFT
, XK_7
, KF_MVWS_7
, NULL
);
7096 setkeybinding(MODKEY_SHIFT
, XK_8
, KF_MVWS_8
, NULL
);
7097 setkeybinding(MODKEY_SHIFT
, XK_9
, KF_MVWS_9
, NULL
);
7098 setkeybinding(MODKEY_SHIFT
, XK_0
, KF_MVWS_10
, NULL
);
7099 setkeybinding(MODKEY_SHIFT
, XK_F1
, KF_MVWS_11
, NULL
);
7100 setkeybinding(MODKEY_SHIFT
, XK_F2
, KF_MVWS_12
, NULL
);
7101 setkeybinding(MODKEY_SHIFT
, XK_F3
, KF_MVWS_13
, NULL
);
7102 setkeybinding(MODKEY_SHIFT
, XK_F4
, KF_MVWS_14
, NULL
);
7103 setkeybinding(MODKEY_SHIFT
, XK_F5
, KF_MVWS_15
, NULL
);
7104 setkeybinding(MODKEY_SHIFT
, XK_F6
, KF_MVWS_16
, NULL
);
7105 setkeybinding(MODKEY_SHIFT
, XK_F7
, KF_MVWS_17
, NULL
);
7106 setkeybinding(MODKEY_SHIFT
, XK_F8
, KF_MVWS_18
, NULL
);
7107 setkeybinding(MODKEY_SHIFT
, XK_F9
, KF_MVWS_19
, NULL
);
7108 setkeybinding(MODKEY_SHIFT
, XK_F10
, KF_MVWS_20
, NULL
);
7109 setkeybinding(MODKEY_SHIFT
, XK_F11
, KF_MVWS_21
, NULL
);
7110 setkeybinding(MODKEY_SHIFT
, XK_F12
, KF_MVWS_22
, NULL
);
7111 setkeybinding(MODKEY_SHIFT
, XK_slash
, KF_NAME_WORKSPACE
,NULL
);
7112 setkeybinding(MODKEY_SHIFT
, XK_q
, KF_QUIT
, NULL
);
7113 setkeybinding(MODKEY_SHIFT
, XK_r
, KF_RAISE_TOGGLE
,NULL
);
7114 setkeybinding(MODKEY
, XK_q
, KF_RESTART
, NULL
);
7115 setkeybinding(MODKEY
, XK_KP_End
, KF_RG_1
, NULL
);
7116 setkeybinding(MODKEY
, XK_KP_Down
, KF_RG_2
, NULL
);
7117 setkeybinding(MODKEY
, XK_KP_Next
, KF_RG_3
, NULL
);
7118 setkeybinding(MODKEY
, XK_KP_Left
, KF_RG_4
, NULL
);
7119 setkeybinding(MODKEY
, XK_KP_Begin
, KF_RG_5
, NULL
);
7120 setkeybinding(MODKEY
, XK_KP_Right
, KF_RG_6
, NULL
);
7121 setkeybinding(MODKEY
, XK_KP_Home
, KF_RG_7
, NULL
);
7122 setkeybinding(MODKEY
, XK_KP_Up
, KF_RG_8
, NULL
);
7123 setkeybinding(MODKEY
, XK_KP_Prior
, KF_RG_9
, NULL
);
7124 setkeybinding(MODKEY_SHIFT
, XK_Right
, KF_RG_NEXT
, NULL
);
7125 setkeybinding(MODKEY_SHIFT
, XK_Left
, KF_RG_PREV
, NULL
);
7126 setkeybinding(MODKEY
, XK_f
, KF_SEARCH_WIN
, NULL
);
7127 setkeybinding(MODKEY
, XK_slash
, KF_SEARCH_WORKSPACE
,NULL
);
7128 setkeybinding(MODKEY_SHIFT
, XK_i
, KF_SPAWN_CUSTOM
,"initscr");
7129 setkeybinding(MODKEY_SHIFT
, XK_Delete
, KF_SPAWN_CUSTOM
,"lock");
7130 setkeybinding(MODKEY
, XK_p
, KF_SPAWN_CUSTOM
,"menu");
7131 setkeybinding(MODKEY
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_all");
7132 setkeybinding(MODKEY_SHIFT
, XK_s
, KF_SPAWN_CUSTOM
,"screenshot_wind");
7133 setkeybinding(MODKEY_SHIFT
, XK_Return
, KF_SPAWN_CUSTOM
,"term");
7134 setkeybinding(MODKEY_SHIFT
, XK_comma
, KF_STACK_INC
, NULL
);
7135 setkeybinding(MODKEY_SHIFT
, XK_period
, KF_STACK_DEC
, NULL
);
7136 setkeybinding(MODKEY_SHIFT
, XK_space
, KF_STACK_RESET
, NULL
);
7137 setkeybinding(MODKEY
, XK_Return
, KF_SWAP_MAIN
, NULL
);
7138 setkeybinding(MODKEY_SHIFT
, XK_j
, KF_SWAP_NEXT
, NULL
);
7139 setkeybinding(MODKEY_SHIFT
, XK_k
, KF_SWAP_PREV
, NULL
);
7140 setkeybinding(MODKEY_SHIFT
, XK_w
, KF_UNICONIFY
, NULL
);
7141 setkeybinding(MODKEY_SHIFT
, XK_v
, KF_VERSION
, NULL
);
7142 setkeybinding(MODKEY
, XK_equal
, KF_WIDTH_GROW
, NULL
);
7143 setkeybinding(MODKEY
, XK_minus
, KF_WIDTH_SHRINK
,NULL
);
7144 setkeybinding(MODKEY
, XK_x
, KF_WIND_DEL
, NULL
);
7145 setkeybinding(MODKEY_SHIFT
, XK_x
, KF_WIND_KILL
, NULL
);
7146 setkeybinding(MODKEY
, XK_1
, KF_WS_1
, NULL
);
7147 setkeybinding(MODKEY
, XK_2
, KF_WS_2
, NULL
);
7148 setkeybinding(MODKEY
, XK_3
, KF_WS_3
, NULL
);
7149 setkeybinding(MODKEY
, XK_4
, KF_WS_4
, NULL
);
7150 setkeybinding(MODKEY
, XK_5
, KF_WS_5
, NULL
);
7151 setkeybinding(MODKEY
, XK_6
, KF_WS_6
, NULL
);
7152 setkeybinding(MODKEY
, XK_7
, KF_WS_7
, NULL
);
7153 setkeybinding(MODKEY
, XK_8
, KF_WS_8
, NULL
);
7154 setkeybinding(MODKEY
, XK_9
, KF_WS_9
, NULL
);
7155 setkeybinding(MODKEY
, XK_0
, KF_WS_10
, NULL
);
7156 setkeybinding(MODKEY
, XK_F1
, KF_WS_11
, NULL
);
7157 setkeybinding(MODKEY
, XK_F2
, KF_WS_12
, NULL
);
7158 setkeybinding(MODKEY
, XK_F3
, KF_WS_13
, NULL
);
7159 setkeybinding(MODKEY
, XK_F4
, KF_WS_14
, NULL
);
7160 setkeybinding(MODKEY
, XK_F5
, KF_WS_15
, NULL
);
7161 setkeybinding(MODKEY
, XK_F6
, KF_WS_16
, NULL
);
7162 setkeybinding(MODKEY
, XK_F7
, KF_WS_17
, NULL
);
7163 setkeybinding(MODKEY
, XK_F8
, KF_WS_18
, NULL
);
7164 setkeybinding(MODKEY
, XK_F9
, KF_WS_19
, NULL
);
7165 setkeybinding(MODKEY
, XK_F10
, KF_WS_20
, NULL
);
7166 setkeybinding(MODKEY
, XK_F11
, KF_WS_21
, NULL
);
7167 setkeybinding(MODKEY
, XK_F12
, KF_WS_22
, NULL
);
7168 setkeybinding(MODKEY
, XK_Right
, KF_WS_NEXT
, NULL
);
7169 setkeybinding(MODKEY
, XK_Left
, KF_WS_PREV
, NULL
);
7170 setkeybinding(MODKEY
, XK_Up
, KF_WS_NEXT_ALL
, NULL
);
7171 setkeybinding(MODKEY
, XK_Down
, KF_WS_PREV_ALL
, NULL
);
7172 setkeybinding(MODKEY_SHIFT
, XK_Up
, KF_WS_NEXT_MOVE
,NULL
);
7173 setkeybinding(MODKEY_SHIFT
, XK_Down
, KF_WS_PREV_MOVE
,NULL
);
7174 setkeybinding(MODKEY
, XK_a
, KF_WS_PRIOR
, NULL
);
7176 setkeybinding(MODKEY_SHIFT
, XK_d
, KF_DUMPWINS
, NULL
);
7186 while (RB_EMPTY(&keys
) == 0) {
7187 kp
= RB_ROOT(&keys
);
7193 setkeymapping(const char *selector
, const char *value
, int flags
)
7195 char *keymapping_file
;
7197 /* suppress unused warnings since vars are needed */
7201 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
7203 keymapping_file
= expand_tilde(value
);
7206 /* load new key bindings; if it fails, revert to default bindings */
7207 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
7212 free(keymapping_file
);
7214 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
7219 updatenumlockmask(void)
7222 xcb_get_modifier_mapping_reply_t
*modmap_r
;
7223 xcb_keycode_t
*modmap
, kc
, *keycode
;
7227 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
7228 xcb_get_modifier_mapping(conn
),
7231 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
7232 for (i
= 0; i
< 8; i
++) {
7233 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
7234 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
7236 keycode
= xcb_key_symbols_get_keycode(syms
,
7240 numlockmask
= (1 << i
);
7247 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
7254 int num_screens
, k
, j
;
7255 unsigned int modifiers
[4];
7256 xcb_keycode_t
*code
;
7258 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
7259 updatenumlockmask();
7262 modifiers
[1] = numlockmask
;
7263 modifiers
[2] = XCB_MOD_MASK_LOCK
;
7264 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
7266 num_screens
= get_screen_count();
7267 for (k
= 0; k
< num_screens
; k
++) {
7268 if (TAILQ_EMPTY(&screens
[k
].rl
))
7270 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
7272 RB_FOREACH(kp
, key_tree
, &keys
) {
7273 /* Skip unused ws binds. */
7274 if ((int)kp
->funcid
> KF_WS_1
+ workspace_limit
- 1 &&
7275 kp
->funcid
<= KF_WS_22
)
7278 /* Skip unused mvws binds. */
7279 if ((int)kp
->funcid
> KF_MVWS_1
+ workspace_limit
- 1 &&
7280 kp
->funcid
<= KF_MVWS_22
)
7283 if ((code
= xcb_key_symbols_get_keycode(syms
,
7285 for (j
= 0; j
< LENGTH(modifiers
); j
++)
7286 xcb_grab_key(conn
, 1,
7288 kp
->mod
| modifiers
[j
],
7289 *code
, XCB_GRAB_MODE_SYNC
,
7290 XCB_GRAB_MODE_SYNC
);
7298 grabbuttons(struct ws_win
*win
)
7300 unsigned int modifiers
[4];
7303 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: win %#x\n", win
->id
);
7304 updatenumlockmask();
7307 modifiers
[1] = numlockmask
;
7308 modifiers
[2] = XCB_MOD_MASK_LOCK
;
7309 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
7311 for (i
= 0; i
< LENGTH(buttons
); i
++)
7312 if (buttons
[i
].action
== client_click
)
7313 for (j
= 0; j
< LENGTH(modifiers
); ++j
)
7314 xcb_grab_button(conn
, 0, win
->id
, BUTTONMASK
,
7315 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
,
7316 XCB_WINDOW_NONE
, XCB_CURSOR_NONE
,
7317 buttons
[i
].button
, buttons
[i
].mask
|
7321 const char *quirkname
[] = {
7322 "NONE", /* config string for "no value" */
7330 "FOCUSONMAP_SINGLE",
7333 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
7334 #define SWM_Q_WS "\n|+ \t"
7336 parsequirks(const char *qstr
, unsigned long *quirk
)
7338 char *str
, *cp
, *name
;
7341 if (quirk
== NULL
|| qstr
== NULL
)
7344 if ((str
= strdup(qstr
)) == NULL
)
7345 err(1, "parsequirks: strdup");
7349 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
7351 cp
+= (long)strspn(cp
, SWM_Q_WS
);
7352 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
7353 if (strncasecmp(name
, quirkname
[i
],
7354 SWM_QUIRK_LEN
) == 0) {
7355 DNPRINTF(SWM_D_QUIRK
,
7356 "parsequirks: %s\n", name
);
7362 *quirk
|= 1 << (i
-1);
7366 if (i
>= LENGTH(quirkname
)) {
7367 DNPRINTF(SWM_D_QUIRK
,
7368 "parsequirks: invalid quirk [%s]\n", name
);
7379 quirk_insert(const char *class, const char *instance
, const char *name
,
7380 unsigned long quirk
)
7386 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: class: %s, instance: %s, name: %s,"
7387 " value: %lu\n", class, instance
, name
, quirk
);
7389 if ((qp
= malloc(sizeof *qp
)) == NULL
)
7390 err(1, "quirk_insert: malloc");
7392 if ((qp
->class = strdup(class)) == NULL
)
7393 err(1, "quirk_insert: strdup");
7394 if ((qp
->instance
= strdup(instance
)) == NULL
)
7395 err(1, "quirk_insert: strdup");
7396 if ((qp
->name
= strdup(name
)) == NULL
)
7397 err(1, "quirk_insert: strdup");
7399 if (asprintf(&str
, "^%s$", class) == -1)
7400 err(1, "quirk_insert: asprintf");
7401 if (regcomp(&qp
->regex_class
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7402 add_startup_exception("regex failed to compile quirk 'class' "
7403 "field: %s", class);
7406 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7409 if (asprintf(&str
, "^%s$", instance
) == -1)
7410 err(1, "quirk_insert: asprintf");
7411 if (regcomp(&qp
->regex_instance
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7412 add_startup_exception("regex failed to compile quirk 'instance'"
7413 " field: %s", instance
);
7416 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7419 if (asprintf(&str
, "^%s$", name
) == -1)
7420 err(1, "quirk_insert: asprintf");
7421 if (regcomp(&qp
->regex_name
, str
, REG_EXTENDED
| REG_NOSUB
)) {
7422 add_startup_exception("regex failed to compile quirk 'name' "
7426 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
7430 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: regex error; skipping.\n");
7434 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
7436 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
7440 quirk_remove(struct quirk
*qp
)
7442 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%lu]\n", qp
->class,
7443 qp
->name
, qp
->quirk
);
7445 TAILQ_REMOVE(&quirks
, qp
, entry
);
7448 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
7452 quirk_free(struct quirk
*qp
)
7454 regfree(&qp
->regex_class
);
7455 regfree(&qp
->regex_instance
);
7456 regfree(&qp
->regex_name
);
7464 quirk_replace(struct quirk
*qp
, const char *class, const char *instance
,
7465 const char *name
, unsigned long quirk
)
7467 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s:%s [%lu]\n", qp
->class,
7468 qp
->instance
, qp
->name
, qp
->quirk
);
7471 quirk_insert(class, instance
, name
, quirk
);
7473 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
7477 setquirk(const char *class, const char *instance
, const char *name
,
7478 unsigned long quirk
)
7482 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s:%s [%lu]\n", class,
7483 instance
, name
, quirk
);
7485 /* Remove/replace existing quirk. */
7486 TAILQ_FOREACH(qp
, &quirks
, entry
) {
7487 if (strcmp(qp
->class, class) == 0 &&
7488 strcmp(qp
->instance
, instance
) == 0 &&
7489 strcmp(qp
->name
, name
) == 0) {
7493 quirk_replace(qp
, class, instance
, name
, quirk
);
7494 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
7499 /* Only insert if quirk is not NONE. */
7501 quirk_insert(class, instance
, name
, quirk
);
7503 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
7506 /* Eat '\' in str used to escape square brackets and colon. */
7508 unescape_selector(char *str
)
7512 for (cp
= str
; *str
!= '\0'; ++str
, ++cp
) {
7513 if (*str
== '\\' && (*(str
+ 1) == ':' || *(str
+ 1) == ']' ||
7523 setconfquirk(const char *selector
, const char *value
, int flags
)
7525 char *str
, *cp
, *class;
7526 char *instance
= NULL
, *name
= NULL
;
7527 int retval
, count
= 0;
7530 /* suppress unused warning since var is needed */
7533 if (selector
== NULL
|| strlen(selector
) == 0)
7536 if ((str
= strdup(selector
)) == NULL
)
7537 err(1, "setconfquirk: strdup");
7539 /* Find non-escaped colon. */
7546 for (++cp
; *cp
!= '\0'; ++cp
) {
7547 if (*cp
== ':' && *(cp
- 1) != '\\') {
7553 unescape_selector(class);
7555 instance
= class + strlen(class) + 1;
7556 unescape_selector(instance
);
7562 name
= instance
+ strlen(instance
) + 1;
7563 unescape_selector(name
);
7568 DNPRINTF(SWM_D_CONF
, "setconfquirk: class: %s, instance: %s, "
7569 "name: %s\n", class, instance
, name
);
7571 if ((retval
= parsequirks(value
, &qrks
)) == 0)
7572 setquirk(class, instance
, name
, qrks
);
7581 setquirk("MPlayer", "xv", ".*", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
);
7582 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*", SWM_Q_FLOAT
);
7583 setquirk("Firefox-bin", "firefox-bin", ".*", SWM_Q_TRANSSZ
);
7584 setquirk("Firefox", "Dialog", ".*", SWM_Q_FLOAT
);
7585 setquirk("Gimp", "gimp", ".*", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
7586 setquirk("XTerm", "xterm", ".*", SWM_Q_XTERM_FONTADJ
);
7587 setquirk("xine", "Xine Window", ".*", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
7588 setquirk("Xitk", "Xitk Combo", ".*", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
7589 setquirk("xine", "xine Panel", ".*", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
7590 setquirk("Xitk", "Xine Window", ".*", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
7591 setquirk("xine", "xine Video Fullscreen Window", ".*", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
7592 setquirk("pcb", "pcb", ".*", SWM_Q_FLOAT
);
7593 setquirk("SDL_App", "SDL_App", ".*", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
7596 /* conf file stuff */
7597 #define SWM_CONF_FILE "spectrwm.conf"
7598 #define SWM_CONF_FILE_OLD "scrotwm.conf"
7602 SWM_S_BAR_AT_BOTTOM
,
7603 SWM_S_BAR_BORDER_WIDTH
,
7606 SWM_S_BAR_ENABLED_WS
,
7611 SWM_S_BOUNDARY_WIDTH
,
7612 SWM_S_CLOCK_ENABLED
,
7615 SWM_S_CYCLE_VISIBLE
,
7617 SWM_S_DISABLE_BORDER
,
7619 SWM_S_FOCUS_CLOSE_WRAP
,
7620 SWM_S_FOCUS_DEFAULT
,
7622 SWM_S_ICONIC_ENABLED
,
7623 SWM_S_REGION_PADDING
,
7628 SWM_S_STACK_ENABLED
,
7631 SWM_S_URGENT_ENABLED
,
7632 SWM_S_VERBOSE_LAYOUT
,
7633 SWM_S_WINDOW_CLASS_ENABLED
,
7634 SWM_S_WINDOW_INSTANCE_ENABLED
,
7635 SWM_S_WINDOW_NAME_ENABLED
,
7636 SWM_S_WORKSPACE_LIMIT
7640 setconfvalue(const char *selector
, const char *value
, int flags
)
7642 struct workspace
*ws
;
7643 int i
, ws_id
, num_screens
;
7647 case SWM_S_BAR_ACTION
:
7649 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
7650 err(1, "setconfvalue: bar_action");
7652 case SWM_S_BAR_AT_BOTTOM
:
7653 bar_at_bottom
= atoi(value
);
7655 case SWM_S_BAR_BORDER_WIDTH
:
7656 bar_border_width
= atoi(value
);
7657 if (bar_border_width
< 0)
7658 bar_border_width
= 0;
7660 case SWM_S_BAR_DELAY
:
7661 /* No longer needed; leave to not break old conf files. */
7663 case SWM_S_BAR_ENABLED
:
7664 bar_enabled
= atoi(value
);
7666 case SWM_S_BAR_ENABLED_WS
:
7667 ws_id
= atoi(selector
) - 1;
7668 if (ws_id
< 0 || ws_id
>= workspace_limit
)
7669 errx(1, "setconfvalue: bar_enabled_ws: invalid "
7670 "workspace %d.", ws_id
+ 1);
7672 num_screens
= get_screen_count();
7673 for (i
= 0; i
< num_screens
; i
++) {
7674 ws
= (struct workspace
*)&screens
[i
].ws
;
7675 ws
[ws_id
].bar_enabled
= atoi(value
);
7678 case SWM_S_BAR_FONT
:
7680 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
7681 err(1, "setconfvalue: asprintf: failed to allocate "
7682 "memory for bar_fonts.");
7685 /* If already in xft mode, then we are done. */
7686 if (!bar_font_legacy
)
7689 if ((str
= strdup(value
)) == NULL
)
7690 err(1, "setconfvalue: strdup");
7692 /* If there are any non-XLFD entries, switch to Xft mode. */
7693 while ((b
= strsep(&str
, ",")) != NULL
) {
7697 bar_font_legacy
= 0;
7704 case SWM_S_BAR_FORMAT
:
7706 if ((bar_format
= strdup(value
)) == NULL
)
7707 err(1, "setconfvalue: bar_format");
7709 case SWM_S_BAR_JUSTIFY
:
7710 if (strcmp(value
, "left") == 0)
7711 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
7712 else if (strcmp(value
, "center") == 0)
7713 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
7714 else if (strcmp(value
, "right") == 0)
7715 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
7717 errx(1, "invalid bar_justify");
7719 case SWM_S_BORDER_WIDTH
:
7720 border_width
= atoi(value
);
7721 if (border_width
< 0)
7724 case SWM_S_BOUNDARY_WIDTH
:
7725 boundary_width
= atoi(value
);
7726 if (boundary_width
< 0)
7729 case SWM_S_CLOCK_ENABLED
:
7730 clock_enabled
= atoi(value
);
7732 case SWM_S_CLOCK_FORMAT
:
7733 #ifndef SWM_DENY_CLOCK_FORMAT
7735 if ((clock_format
= strdup(value
)) == NULL
)
7736 err(1, "setconfvalue: clock_format");
7739 case SWM_S_CYCLE_EMPTY
:
7740 cycle_empty
= atoi(value
);
7742 case SWM_S_CYCLE_VISIBLE
:
7743 cycle_visible
= atoi(value
);
7745 case SWM_S_DIALOG_RATIO
:
7746 dialog_ratio
= atof(value
);
7747 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
7750 case SWM_S_DISABLE_BORDER
:
7751 disable_border
= atoi(value
);
7753 case SWM_S_FOCUS_CLOSE
:
7754 if (strcmp(value
, "first") == 0)
7755 focus_close
= SWM_STACK_BOTTOM
;
7756 else if (strcmp(value
, "last") == 0)
7757 focus_close
= SWM_STACK_TOP
;
7758 else if (strcmp(value
, "next") == 0)
7759 focus_close
= SWM_STACK_ABOVE
;
7760 else if (strcmp(value
, "previous") == 0)
7761 focus_close
= SWM_STACK_BELOW
;
7763 errx(1, "focus_close");
7765 case SWM_S_FOCUS_CLOSE_WRAP
:
7766 focus_close_wrap
= atoi(value
);
7768 case SWM_S_FOCUS_DEFAULT
:
7769 if (strcmp(value
, "last") == 0)
7770 focus_default
= SWM_STACK_TOP
;
7771 else if (strcmp(value
, "first") == 0)
7772 focus_default
= SWM_STACK_BOTTOM
;
7774 errx(1, "focus_default");
7776 case SWM_S_FOCUS_MODE
:
7777 if (strcmp(value
, "default") == 0)
7778 focus_mode
= SWM_FOCUS_DEFAULT
;
7779 else if (strcmp(value
, "follow") == 0 ||
7780 strcmp(value
, "follow_cursor") == 0)
7781 focus_mode
= SWM_FOCUS_FOLLOW
;
7782 else if (strcmp(value
, "manual") == 0)
7783 focus_mode
= SWM_FOCUS_MANUAL
;
7785 errx(1, "focus_mode");
7787 case SWM_S_ICONIC_ENABLED
:
7788 iconic_enabled
= atoi(value
);
7790 case SWM_S_REGION_PADDING
:
7791 region_padding
= atoi(value
);
7792 if (region_padding
< 0)
7795 case SWM_S_SPAWN_ORDER
:
7796 if (strcmp(value
, "first") == 0)
7797 spawn_position
= SWM_STACK_BOTTOM
;
7798 else if (strcmp(value
, "last") == 0)
7799 spawn_position
= SWM_STACK_TOP
;
7800 else if (strcmp(value
, "next") == 0)
7801 spawn_position
= SWM_STACK_ABOVE
;
7802 else if (strcmp(value
, "previous") == 0)
7803 spawn_position
= SWM_STACK_BELOW
;
7805 errx(1, "spawn_position");
7807 case SWM_S_SPAWN_TERM
:
7808 setconfspawn("term", value
, 0);
7809 setconfspawn("spawn_term", value
, 0);
7812 /* No longer needed; leave to not break old conf files. */
7814 case SWM_S_SS_ENABLED
:
7815 /* No longer needed; leave to not break old conf files. */
7817 case SWM_S_STACK_ENABLED
:
7818 stack_enabled
= atoi(value
);
7820 case SWM_S_TERM_WIDTH
:
7821 term_width
= atoi(value
);
7825 case SWM_S_TILE_GAP
:
7826 tile_gap
= atoi(value
);
7828 case SWM_S_URGENT_ENABLED
:
7829 urgent_enabled
= atoi(value
);
7831 case SWM_S_VERBOSE_LAYOUT
:
7832 verbose_layout
= atoi(value
);
7833 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
7835 layouts
[i
].l_string
= fancy_stacker
;
7837 layouts
[i
].l_string
= plain_stacker
;
7840 case SWM_S_WINDOW_CLASS_ENABLED
:
7841 window_class_enabled
= atoi(value
);
7843 case SWM_S_WINDOW_INSTANCE_ENABLED
:
7844 window_instance_enabled
= atoi(value
);
7846 case SWM_S_WINDOW_NAME_ENABLED
:
7847 window_name_enabled
= atoi(value
);
7849 case SWM_S_WORKSPACE_LIMIT
:
7850 workspace_limit
= atoi(value
);
7851 if (workspace_limit
> SWM_WS_MAX
)
7852 workspace_limit
= SWM_WS_MAX
;
7853 else if (workspace_limit
< 1)
7854 workspace_limit
= 1;
7856 num_screens
= get_screen_count();
7857 for (i
= 0; i
< num_screens
; i
++) {
7858 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
7859 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
7860 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
7870 setconfmodkey(const char *selector
, const char *value
, int flags
)
7872 /* suppress unused warnings since vars are needed */
7876 if (strncasecmp(value
, "Mod1", strlen("Mod1")) == 0)
7877 update_modkey(XCB_MOD_MASK_1
);
7878 else if (strncasecmp(value
, "Mod2", strlen("Mod2")) == 0)
7879 update_modkey(XCB_MOD_MASK_2
);
7880 else if (strncasecmp(value
, "Mod3", strlen("Mod3")) == 0)
7881 update_modkey(XCB_MOD_MASK_3
);
7882 else if (strncasecmp(value
, "Mod4", strlen("Mod4")) == 0)
7883 update_modkey(XCB_MOD_MASK_4
);
7890 setconfcolor(const char *selector
, const char *value
, int flags
)
7892 setscreencolor(value
,
7893 (selector
== NULL
|| strlen(selector
) == 0) ? -1 : atoi(selector
),
7899 setconfregion(const char *selector
, const char *value
, int flags
)
7901 /* suppress unused warnings since vars are needed */
7905 custom_region(value
);
7910 setautorun(const char *selector
, const char *value
, int flags
)
7920 /* suppress unused warnings since vars are needed */
7924 if (getenv("SWM_STARTED"))
7928 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
7929 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
7931 if (ws_id
< 0 || ws_id
>= workspace_limit
)
7932 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
7934 sp
= expand_tilde((char *)&s
);
7937 * This is a little intricate
7939 * If the pid already exists we simply reuse it because it means it was
7940 * used before AND not claimed by manage_window. We get away with
7941 * altering it in the parent after INSERT because this can not be a race
7944 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
7947 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
7949 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
7950 err(1, "setautorun: realloc");
7951 a
.argv
[argc
- 1] = ap
;
7955 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
7956 err(1, "setautorun: realloc");
7957 a
.argv
[argc
] = NULL
;
7959 if ((pid
= fork()) == 0) {
7960 spawn(ws_id
, &a
, 1);
7969 p
= calloc(1, sizeof *p
);
7972 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
7982 setlayout(const char *selector
, const char *value
, int flags
)
7984 int ws_id
, i
, x
, mg
, ma
, si
, ar
, f
= 0;
7985 int st
= SWM_V_STACK
, num_screens
;
7987 struct workspace
*ws
;
7989 /* suppress unused warnings since vars are needed */
7993 if (getenv("SWM_STARTED"))
7997 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
7998 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
7999 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8000 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8003 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8004 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
8006 if (strcasecmp(s
, "vertical") == 0)
8008 else if (strcasecmp(s
, "vertical_flip") == 0) {
8011 } else if (strcasecmp(s
, "horizontal") == 0)
8013 else if (strcasecmp(s
, "horizontal_flip") == 0) {
8016 } else if (strcasecmp(s
, "fullscreen") == 0)
8019 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
8020 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
8023 num_screens
= get_screen_count();
8024 for (i
= 0; i
< num_screens
; i
++) {
8025 ws
= (struct workspace
*)&screens
[i
].ws
;
8026 ws
[ws_id
].cur_layout
= &layouts
[st
];
8028 ws
[ws_id
].always_raise
= ar
;
8029 if (st
== SWM_MAX_STACK
)
8033 for (x
= 0; x
< abs(mg
); x
++) {
8034 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8035 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
8036 SWM_ARG_ID_MASTERSHRINK
);
8040 for (x
= 0; x
< abs(ma
); x
++) {
8041 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8042 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
8043 SWM_ARG_ID_MASTERDEL
);
8047 for (x
= 0; x
< abs(si
); x
++) {
8048 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8049 si
>= 0 ? SWM_ARG_ID_STACKINC
:
8050 SWM_ARG_ID_STACKDEC
);
8055 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
8056 SWM_ARG_ID_FLIPLAYOUT
);
8066 /* config options */
8067 struct config_option
{
8069 int (*func
)(const char*, const char*, int);
8072 struct config_option configopt
[] = {
8073 { "autorun", setautorun
, 0 },
8074 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
8075 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
8076 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
8077 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
8078 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
8079 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
8080 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
8081 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
8082 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
8083 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
8084 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
8085 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
8086 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
8087 { "bind", setconfbinding
, 0 },
8088 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
8089 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
8090 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
8091 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
8092 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
8093 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
8094 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
8095 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
8096 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
8097 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
8098 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
8099 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
8100 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
8101 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
8102 { "iconic_enabled", setconfvalue
, SWM_S_ICONIC_ENABLED
},
8103 { "keyboard_mapping", setkeymapping
, 0 },
8104 { "layout", setlayout
, 0 },
8105 { "modkey", setconfmodkey
, 0 },
8106 { "program", setconfspawn
, 0 },
8107 { "quirk", setconfquirk
, 0 },
8108 { "region", setconfregion
, 0 },
8109 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
8110 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
8111 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
8112 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
8113 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
8114 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
8115 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
8116 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
8117 { "title_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
}, /* For backwards compat. */
8118 { "title_name_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
}, /* For backwards compat. */
8119 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
8120 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
8121 { "window_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
},
8122 { "window_instance_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
},
8123 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
8124 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
8128 _add_startup_exception(const char *fmt
, va_list ap
)
8130 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
8131 warn("%s: asprintf", __func__
);
8135 add_startup_exception(const char *fmt
, ...)
8141 if (startup_exception
)
8144 /* force bar to be enabled due to exception */
8148 _add_startup_exception(fmt
, ap
);
8153 conf_load(const char *filename
, int keymapping
)
8156 char *line
= NULL
, *cp
, *ce
, *optsub
, *optval
= NULL
;
8157 size_t linelen
, lineno
= 0;
8158 int wordlen
, i
, optidx
, count
;
8159 struct config_option
*opt
= NULL
;
8161 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
8163 if (filename
== NULL
) {
8164 warnx("conf_load: no filename");
8168 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
8170 if ((config
= fopen(filename
, "r")) == NULL
) {
8171 warn("conf_load: fopen: %s", filename
);
8175 while (!feof(config
)) {
8179 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
8180 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
8182 err(1, "%s", filename
);
8187 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
8188 if (cp
[0] == '\0') {
8192 /* get config option */
8193 wordlen
= strcspn(cp
, "=[ \t\n");
8195 add_startup_exception("%s: line %zd: no option found",
8200 for (i
= 0; i
< LENGTH(configopt
); i
++) {
8201 opt
= &configopt
[i
];
8202 if (strncasecmp(cp
, opt
->optname
, wordlen
) == 0 &&
8203 (int)strlen(opt
->optname
) == wordlen
) {
8209 add_startup_exception("%s: line %zd: unknown option "
8210 "%.*s", filename
, lineno
, wordlen
, cp
);
8213 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
8214 add_startup_exception("%s: line %zd: invalid option "
8215 "%.*s", filename
, lineno
, wordlen
, cp
);
8219 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
8221 /* get [selector] if any */
8226 /* Get length of selector. */
8227 for (ce
= ++cp
; *ce
!= '\0'; ++ce
) {
8228 /* Find matching (not escaped) bracket. */
8229 if (*ce
== ']' && *(ce
- 1) != '\\') {
8236 add_startup_exception("%s: line %zd: syntax "
8237 "error: unterminated selector", filename
,
8242 /* ce points at ']'; terminate optsub. */
8247 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
8250 if (strlen(optval
) == 0) {
8251 add_startup_exception("%s: line %zd: must supply value "
8252 "to %s", filename
, lineno
,
8253 configopt
[optidx
].optname
);
8256 /* call function to deal with it all */
8257 if (configopt
[optidx
].func(optsub
, optval
,
8258 configopt
[optidx
].funcflags
) != 0) {
8259 add_startup_exception("%s: line %zd: invalid data for "
8260 "%s", filename
, lineno
, configopt
[optidx
].optname
);
8268 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
8274 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
8276 struct ws_win
*parent
, *w
;
8277 struct swm_region
*r
;
8278 struct workspace
*ws
;
8279 xcb_icccm_wm_hints_t wmh
;
8281 parent
= find_window(win
->transient
);
8283 parent
->focus_child
= win
;
8285 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
8286 " for %#x trans %#x\n", win
->id
, win
->transient
);
8288 r
= root_to_region(win
->s
->root
, SWM_CK_ALL
);
8290 /* parent doen't exist in our window list */
8291 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
8292 if (xcb_icccm_get_wm_hints_reply(conn
,
8293 xcb_icccm_get_wm_hints(conn
, w
->id
),
8295 warnx("can't get hints for %#x", w
->id
);
8299 if (win
->hints
.window_group
!= wmh
.window_group
)
8302 w
->focus_child
= win
;
8303 win
->transient
= w
->id
;
8305 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
8306 "transient to %#x\n", win
->transient
);
8313 window_get_pid(xcb_window_t win
)
8318 xcb_get_property_cookie_t pc
;
8319 xcb_get_property_reply_t
*pr
;
8321 apid
= get_atom_from_string("_NET_WM_PID");
8322 if (apid
== XCB_ATOM_NONE
)
8325 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
8326 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
8329 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
8334 if (pr
->type
== apid
&& pr
->format
== 32)
8335 ret
= *((pid_t
*)xcb_get_property_value(pr
));
8341 apid
= get_atom_from_string("_SWM_PID");
8342 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
8344 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
8347 if (pr
->type
!= apid
) {
8352 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
8359 get_swm_ws(xcb_window_t id
)
8365 xcb_get_property_reply_t
*gpr
;
8367 gpr
= xcb_get_property_reply(conn
,
8368 xcb_get_property(conn
, 0, id
, a_swm_ws
,
8369 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
8374 proplen
= xcb_get_property_value_length(gpr
);
8376 prop
= malloc(proplen
+ 1);
8379 xcb_get_property_value(gpr
),
8381 prop
[proplen
] = '\0';
8388 DNPRINTF(SWM_D_PROP
, "get_swm_ws: _SWM_WS: %s\n", prop
);
8389 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
8391 DNPRINTF(SWM_D_PROP
, "get_swm_ws: win #%s: %s",
8401 get_ws_idx(xcb_window_t id
)
8403 xcb_get_property_reply_t
*gpr
;
8406 gpr
= xcb_get_property_reply(conn
,
8407 xcb_get_property(conn
, 0, id
, ewmh
[_NET_WM_DESKTOP
].atom
,
8408 XCB_ATOM_CARDINAL
, 0, 1),
8411 if (gpr
->type
== XCB_ATOM_CARDINAL
&& gpr
->format
== 32)
8412 ws_idx
= *((int *)xcb_get_property_value(gpr
));
8417 if ((ws_idx
= get_swm_ws(id
)) != -1)
8418 xcb_delete_property(conn
, id
, a_swm_ws
);
8420 if (ws_idx
> workspace_limit
- 1 || ws_idx
< -1)
8423 DNPRINTF(SWM_D_PROP
, "get_ws_idx: win %#x, ws_idx: %d\n", id
, ws_idx
);
8429 manage_window(xcb_window_t id
, uint16_t mapped
)
8431 xcb_window_t trans
= XCB_WINDOW_NONE
;
8432 struct ws_win
*win
, *ww
;
8434 char *class, *instance
, *name
;
8435 struct swm_region
*r
;
8438 uint32_t i
, wa
[2], new_flags
;
8439 xcb_get_geometry_reply_t
*gr
;
8441 if ((win
= find_window(id
)) != NULL
) {
8442 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x already "
8443 "managed; skipping.)\n", id
);
8444 return (win
); /* Already managed. */
8447 /* See if window is on the unmanaged list. */
8448 if ((win
= find_unmanaged_window(id
)) != NULL
) {
8449 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x found on "
8450 "unmanaged list.\n", id
);
8451 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
8454 set_child_transient(win
, &trans
);
8458 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is new.\n", id
);
8461 /* Try to get initial window geometry. */
8462 gr
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, id
), NULL
);
8464 DNPRINTF(SWM_D_MISC
, "manage_window: get geometry failed.\n");
8468 /* Create and initialize ws_win object. */
8469 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
8470 err(1, "manage_window: calloc: failed to allocate memory for "
8475 /* Figureout which region the window belongs to. */
8476 r
= root_to_region(gr
->root
, SWM_CK_ALL
);
8478 /* Ignore window border if there is one. */
8479 WIDTH(win
) = gr
->width
;
8480 HEIGHT(win
) = gr
->height
;
8481 X(win
) = gr
->x
+ gr
->border_width
- border_width
;
8482 Y(win
) = gr
->y
+ gr
->border_width
- border_width
;
8484 win
->mapped
= mapped
;
8485 win
->s
= r
->s
; /* this never changes */
8489 /* Select which X events to monitor and set border pixel color. */
8490 wa
[0] = win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
8491 wa
[1] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
8492 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
8494 wa
[1] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
8497 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_BORDER_PIXEL
|
8498 XCB_CW_EVENT_MASK
, wa
);
8500 /* Get WM_SIZE_HINTS. */
8501 xcb_icccm_get_wm_normal_hints_reply(conn
,
8502 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
8506 xcb_icccm_get_wm_hints_reply(conn
,
8507 xcb_icccm_get_wm_hints(conn
, win
->id
),
8510 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
8511 xcb_icccm_get_wm_transient_for_reply(conn
,
8512 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
8515 win
->transient
= trans
;
8516 set_child_transient(win
, &win
->transient
);
8519 /* Get WM_PROTOCOLS. */
8520 get_wm_protocols(win
);
8522 /* Figure out which workspace the window belongs to. */
8523 if ((p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
8524 win
->ws
= &r
->s
->ws
[p
->ws
];
8525 TAILQ_REMOVE(&pidlist
, p
, entry
);
8528 } else if ((ws_idx
= get_ws_idx(win
->id
)) != -1 &&
8530 /* _SWM_WS is set; use that. */
8531 win
->ws
= &r
->s
->ws
[ws_idx
];
8532 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
8533 /* Launch transients in the same ws as parent. */
8539 /* Set the _NET_WM_DESKTOP atom. */
8540 DNPRINTF(SWM_D_PROP
, "manage_window: set _NET_WM_DESKTOP: %d\n",
8542 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
8543 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &win
->ws
->idx
);
8545 /* WS must already be set for this to work. */
8546 store_float_geom(win
);
8548 /* Set initial quirks based on EWMH. */
8549 ewmh_autoquirk(win
);
8551 /* Determine initial quirks. */
8552 xcb_icccm_get_wm_class_reply(conn
,
8553 xcb_icccm_get_wm_class(conn
, win
->id
),
8556 class = win
->ch
.class_name
? win
->ch
.class_name
: "";
8557 instance
= win
->ch
.instance_name
? win
->ch
.instance_name
: "";
8558 name
= get_win_name(win
->id
);
8560 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, instance: %s, "
8561 "name: %s\n", class, instance
, name
);
8563 /* java is retarded so treat it special */
8564 if (strstr(instance
, "sun-awt")) {
8565 DNPRINTF(SWM_D_CLASS
, "manage_window: java window detected.\n");
8569 TAILQ_FOREACH(qp
, &quirks
, entry
) {
8570 if (regexec(&qp
->regex_class
, class, 0, NULL
, 0) == 0 &&
8571 regexec(&qp
->regex_instance
, instance
, 0, NULL
, 0) == 0 &&
8572 regexec(&qp
->regex_name
, name
, 0, NULL
, 0) == 0) {
8573 DNPRINTF(SWM_D_CLASS
, "manage_window: matched "
8574 "quirk: %s:%s:%s mask: %#lx\n", qp
->class,
8575 qp
->instance
, qp
->name
, qp
->quirk
);
8576 win
->quirks
= qp
->quirk
;
8582 /* Reset font sizes (the bruteforce way; no default keybinding). */
8583 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
8584 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
8585 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
8586 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
8587 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
8590 /* Make sure window is positioned inside its region, if its active. */
8592 region_containment(win
, r
, SWM_CW_ALLSIDES
|
8593 SWM_CW_HARDBOUNDARY
);
8598 /* Figure out where to stack the window in the workspace. */
8599 if (trans
&& (ww
= find_window(trans
)))
8600 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
8601 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_ABOVE
)
8602 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
8604 else if (win
->ws
->focus
&& spawn_position
== SWM_STACK_BELOW
)
8605 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
8606 else switch (spawn_position
) {
8609 case SWM_STACK_ABOVE
:
8610 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
8612 case SWM_STACK_BOTTOM
:
8613 case SWM_STACK_BELOW
:
8614 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
8617 ewmh_update_client_list();
8619 TAILQ_INSERT_TAIL(&win
->ws
->stack
, win
, stack_entry
);
8621 /* Get/apply initial _NET_WM_STATE */
8622 ewmh_get_wm_state(win
);
8625 new_flags
= win
->ewmh_flags
;
8627 if (win
->quirks
& SWM_Q_FLOAT
)
8628 new_flags
|= EWMH_F_ABOVE
;
8630 if (win
->quirks
& SWM_Q_ANYWHERE
)
8631 new_flags
|= SWM_F_MANUAL
;
8633 ewmh_apply_flags(win
, new_flags
);
8634 ewmh_update_wm_state(win
);
8636 /* Set initial _NET_WM_ALLOWED_ACTIONS */
8637 ewmh_update_actions(win
);
8641 DNPRINTF(SWM_D_MISC
, "manage_window: done. win %#x, (x,y) w x h: "
8642 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win
->id
,
8643 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
8644 YESNO(ICONIC(win
)), win
->transient
);
8650 free_window(struct ws_win
*win
)
8652 DNPRINTF(SWM_D_MISC
, "free_window: win %#x\n", win
->id
);
8657 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
8659 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
8664 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
8667 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
8671 unmanage_window(struct ws_win
*win
)
8673 struct ws_win
*parent
;
8678 DNPRINTF(SWM_D_MISC
, "unmanage_window: win %#x\n", win
->id
);
8681 parent
= find_window(win
->transient
);
8683 parent
->focus_child
= NULL
;
8686 TAILQ_REMOVE(&win
->ws
->stack
, win
, stack_entry
);
8687 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
8688 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
8690 ewmh_update_client_list();
8694 expose(xcb_expose_event_t
*e
)
8697 struct swm_region
*r
;
8699 DNPRINTF(SWM_D_EVENT
, "expose: win %#x\n", e
->window
);
8701 num_screens
= get_screen_count();
8702 for (i
= 0; i
< num_screens
; i
++)
8703 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
8704 if (e
->window
== WINID(r
->bar
))
8712 focusin(xcb_focus_in_event_t
*e
)
8714 DNPRINTF(SWM_D_EVENT
, "focusin: win %#x, mode: %s(%u), "
8715 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
8716 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
8720 focusout(xcb_focus_out_event_t
*e
)
8722 DNPRINTF(SWM_D_EVENT
, "focusout: win %#x, mode: %s(%u), "
8723 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
8724 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
8729 keypress(xcb_key_press_event_t
*e
)
8731 xcb_keysym_t keysym
;
8734 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
8736 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
8737 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
8738 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
8739 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
8740 e
->child
, e
->state
, YESNO(e
->same_screen
));
8742 if ((kp
= key_lookup(CLEANMASK(e
->state
), keysym
)) == NULL
)
8745 last_event_time
= e
->time
;
8747 if (kp
->funcid
== KF_SPAWN_CUSTOM
)
8748 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
),
8749 &(keyfuncs
[kp
->funcid
].args
), kp
->spawn_name
);
8750 else if (keyfuncs
[kp
->funcid
].func
)
8751 keyfuncs
[kp
->funcid
].func(root_to_region(e
->root
, SWM_CK_ALL
),
8752 &(keyfuncs
[kp
->funcid
].args
));
8755 /* Unfreeze grab events. */
8756 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
, e
->time
);
8759 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
8763 buttonpress(xcb_button_press_event_t
*e
)
8765 struct ws_win
*win
= NULL
;
8766 struct swm_region
*r
, *old_r
;
8770 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): %#x (%d,%d), "
8771 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
8772 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
8773 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
8774 e
->state
, YESNO(e
->same_screen
));
8776 if (e
->event
== e
->root
) {
8777 if (e
->child
!= 0) {
8778 win
= find_window(e
->child
);
8779 /* Pass ButtonPress to window if it isn't managed. */
8783 /* Focus on empty region */
8784 /* If no windows on region if its empty. */
8785 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
8787 DNPRINTF(SWM_D_EVENT
, "buttonpress: "
8788 "NULL region; ignoring.\n");
8792 if (TAILQ_EMPTY(&r
->ws
->winlist
)) {
8793 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
8794 if (old_r
&& old_r
!= r
)
8795 unfocus_win(old_r
->ws
->focus
);
8797 xcb_set_input_focus(conn
,
8798 XCB_INPUT_FOCUS_PARENT
, e
->root
, e
->time
);
8800 /* Clear bar since empty. */
8808 win
= find_window(e
->event
);
8814 last_event_time
= e
->time
;
8816 focus_win(get_focus_magic(win
));
8818 for (i
= 0; i
< LENGTH(buttons
); i
++)
8819 if (client_click
== buttons
[i
].action
&& buttons
[i
].func
&&
8820 buttons
[i
].button
== e
->detail
&&
8821 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(e
->state
)) {
8822 buttons
[i
].func(win
, &buttons
[i
].args
);
8828 DNPRINTF(SWM_D_EVENT
, "buttonpress: passing to window.\n");
8829 /* Replay event to event window */
8830 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
8832 DNPRINTF(SWM_D_EVENT
, "buttonpress: handled.\n");
8833 /* Unfreeze grab events. */
8834 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
8842 print_win_geom(xcb_window_t w
)
8844 xcb_get_geometry_reply_t
*wa
;
8846 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
8848 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: %#x\n",
8853 DNPRINTF(SWM_D_MISC
, "print_win_geom: win %#x, root: %#x, "
8854 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
8855 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
8864 get_stack_mode_name(uint8_t mode
)
8869 case XCB_STACK_MODE_ABOVE
:
8872 case XCB_STACK_MODE_BELOW
:
8875 case XCB_STACK_MODE_TOP_IF
:
8878 case XCB_STACK_MODE_BOTTOM_IF
:
8881 case XCB_STACK_MODE_OPPOSITE
:
8893 configurerequest(xcb_configure_request_event_t
*e
)
8896 struct swm_region
*r
= NULL
;
8899 uint32_t wc
[7] = {0};
8901 if ((win
= find_window(e
->window
)) == NULL
)
8902 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
8906 if (swm_debug
& SWM_D_EVENT
) {
8907 print_win_geom(e
->window
);
8909 DNPRINTF(SWM_D_EVENT
, "configurerequest: win %#x, "
8910 "parent: %#x, new: %s, value_mask: %u { ", e
->window
,
8911 e
->parent
, YESNO(new), e
->value_mask
);
8912 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
8913 DPRINTF("X: %d ", e
->x
);
8914 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
8915 DPRINTF("Y: %d ", e
->y
);
8916 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
8917 DPRINTF("W: %u ", e
->width
);
8918 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
8919 DPRINTF("H: %u ", e
->height
);
8920 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
8921 DPRINTF("Border: %u ", e
->border_width
);
8922 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
8923 DPRINTF("Sibling: %#x ", e
->sibling
);
8924 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
8925 DPRINTF("StackMode: %s(%u) ",
8926 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
8932 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
8933 mask
|= XCB_CONFIG_WINDOW_X
;
8936 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
8937 mask
|= XCB_CONFIG_WINDOW_Y
;
8940 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
8941 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
8944 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
8945 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
8946 wc
[i
++] = e
->height
;
8948 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
8949 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
8950 wc
[i
++] = e
->border_width
;
8952 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
8953 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
8954 wc
[i
++] = e
->sibling
;
8956 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
8957 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
8958 wc
[i
++] = e
->stack_mode
;
8962 xcb_configure_window(conn
, e
->window
, mask
, wc
);
8965 } else if ((!MANUAL(win
) || win
->quirks
& SWM_Q_ANYWHERE
) &&
8966 !FULLSCREEN(win
) && !MAXIMIZED(win
)) {
8969 else if (win
->ws
->old_r
)
8972 /* windows are centered unless ANYWHERE quirk is set. */
8973 if (win
->quirks
& SWM_Q_ANYWHERE
) {
8974 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
8975 win
->g_float
.x
= e
->x
;
8977 win
->g_float
.x
-= X(r
);
8980 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
8981 win
->g_float
.y
= e
->y
;
8983 win
->g_float
.y
-= Y(r
);
8987 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
8988 win
->g_float
.w
= e
->width
;
8990 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
8991 win
->g_float
.h
= e
->height
;
8993 win
->g_floatvalid
= 1;
8995 if (ABOVE(win
) && r
&& !MAXIMIZED(win
) && (TRANS(win
) ||
8996 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
])) {
8997 WIDTH(win
) = win
->g_float
.w
;
8998 HEIGHT(win
) = win
->g_float
.h
;
9001 update_floater(win
);
9013 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
9017 configurenotify(xcb_configure_notify_event_t
*e
)
9021 DNPRINTF(SWM_D_EVENT
, "configurenotify: win %#x, event win: %#x, "
9022 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
9023 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
9024 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
9025 YESNO(e
->override_redirect
));
9027 win
= find_window(e
->window
);
9029 xcb_icccm_get_wm_normal_hints_reply(conn
,
9030 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
9033 if (font_adjusted
) {
9041 destroynotify(xcb_destroy_notify_event_t
*e
)
9045 DNPRINTF(SWM_D_EVENT
, "destroynotify: win %#x\n", e
->window
);
9047 if ((win
= find_window(e
->window
)) == NULL
) {
9048 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
9054 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
9055 /* If we were focused, make sure we focus on something else. */
9056 if (win
== win
->ws
->focus
)
9057 win
->ws
->focus_pending
= get_focus_prev(win
);
9060 unmanage_window(win
);
9063 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
9064 if (win
->ws
->focus_pending
) {
9065 focus_win(win
->ws
->focus_pending
);
9066 win
->ws
->focus_pending
= NULL
;
9067 } else if (win
== win
->ws
->focus
) {
9068 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
9069 win
->ws
->r
->id
, XCB_CURRENT_TIME
);
9080 get_notify_detail_label(uint8_t detail
)
9085 case XCB_NOTIFY_DETAIL_ANCESTOR
:
9088 case XCB_NOTIFY_DETAIL_VIRTUAL
:
9091 case XCB_NOTIFY_DETAIL_INFERIOR
:
9094 case XCB_NOTIFY_DETAIL_NONLINEAR
:
9095 label
= "Nonlinear";
9097 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
9098 label
= "NonlinearVirtual";
9100 case XCB_NOTIFY_DETAIL_POINTER
:
9103 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
9104 label
= "PointerRoot";
9106 case XCB_NOTIFY_DETAIL_NONE
:
9117 get_notify_mode_label(uint8_t mode
)
9122 case XCB_NOTIFY_MODE_NORMAL
:
9125 case XCB_NOTIFY_MODE_GRAB
:
9128 case XCB_NOTIFY_MODE_UNGRAB
:
9131 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
9132 label
= "WhileGrabbed";
9143 enternotify(xcb_enter_notify_event_t
*e
)
9146 struct swm_region
*r
;
9148 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): %#x "
9149 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9150 "child: %#x, same_screen_focus: %s, state: %d\n",
9151 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9152 get_notify_mode_label(e
->mode
), e
->mode
,
9153 get_notify_detail_label(e
->detail
), e
->detail
,
9154 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9155 YESNO(e
->same_screen_focus
), e
->state
);
9157 if (focus_mode
== SWM_FOCUS_MANUAL
&&
9158 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
9159 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
9163 last_event_time
= e
->time
;
9165 if ((win
= find_window(e
->event
)) == NULL
) {
9166 if (e
->event
== e
->root
) {
9167 /* If no windows on pointer region, then focus root. */
9168 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
9170 DNPRINTF(SWM_D_EVENT
, "enternotify: "
9171 "NULL region; ignoring.\n");
9177 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
9182 focus_win(get_focus_magic(win
));
9190 leavenotify(xcb_leave_notify_event_t
*e
)
9192 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): %#x "
9193 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9194 "child: %#x, same_screen_focus: %s, state: %d\n",
9195 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9196 get_notify_mode_label(e
->mode
), e
->mode
,
9197 get_notify_detail_label(e
->detail
), e
->detail
,
9198 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9199 YESNO(e
->same_screen_focus
), e
->state
);
9204 mapnotify(xcb_map_notify_event_t
*e
)
9206 struct ws_win
*win
, *parent
= NULL
;
9207 struct workspace
*ws
;
9209 DNPRINTF(SWM_D_EVENT
, "mapnotify: win %#x\n", e
->window
);
9211 if ((win
= manage_window(e
->window
, 1)) == NULL
)
9215 /* Need to know if win was mapped due to ws switch. */
9216 if (ws
->state
== SWM_WS_STATE_MAPPED
) {
9217 if (ws
->focus_pending
&& TRANS(ws
->focus_pending
))
9218 parent
= find_window(win
->transient
);
9220 /* If window's parent is maximized, don't clear it. */
9221 if ((parent
== NULL
) || !MAXIMIZED(parent
))
9222 if (clear_maximized(ws
) > 0)
9227 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
9229 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
9230 if (ws
->focus_pending
== win
) {
9232 ws
->focus_pending
= NULL
;
9241 mappingnotify(xcb_mapping_notify_event_t
*e
)
9244 int i
, j
, num_screens
;
9246 xcb_refresh_keyboard_mapping(syms
, e
);
9248 if (e
->request
== XCB_MAPPING_KEYBOARD
) {
9251 /* Regrab buttons on all managed windows. */
9252 num_screens
= get_screen_count();
9253 for (i
= 0; i
< num_screens
; i
++)
9254 for (j
= 0; j
< workspace_limit
; j
++)
9255 TAILQ_FOREACH(w
, &screens
[i
].ws
[j
].winlist
,
9262 maprequest(xcb_map_request_event_t
*e
)
9264 struct ws_win
*win
, *w
= NULL
;
9265 xcb_get_window_attributes_reply_t
*war
;
9267 DNPRINTF(SWM_D_EVENT
, "maprequest: win %#x\n",
9270 war
= xcb_get_window_attributes_reply(conn
,
9271 xcb_get_window_attributes(conn
, e
->window
),
9274 DNPRINTF(SWM_D_EVENT
, "maprequest: window lost.\n");
9278 if (war
->override_redirect
) {
9279 DNPRINTF(SWM_D_EVENT
, "maprequest: override_redirect; "
9284 win
= manage_window(e
->window
,
9285 (war
->map_state
== XCB_MAP_STATE_VIEWABLE
));
9289 /* The new window should get focus; prepare. */
9290 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
9291 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
) &&
9292 (!(win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
) ||
9293 (win
->hints
.flags
& XCB_ICCCM_WM_HINT_INPUT
&&
9294 win
->hints
.input
))) {
9295 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
9296 /* See if other wins of same type are already mapped. */
9297 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
9298 if (w
== win
|| !w
->mapped
)
9301 if (w
->ch
.class_name
&&
9302 win
->ch
.class_name
&&
9303 strcmp(w
->ch
.class_name
,
9304 win
->ch
.class_name
) == 0 &&
9305 w
->ch
.instance_name
&&
9306 win
->ch
.instance_name
&&
9307 strcmp(w
->ch
.instance_name
,
9308 win
->ch
.instance_name
) == 0)
9314 win
->ws
->focus_pending
= get_focus_magic(win
);
9317 /* All windows need to be mapped if they are in the current workspace.*/
9321 /* Ignore EnterNotify to handle the mapnotify without interference. */
9322 if (focus_mode
== SWM_FOCUS_DEFAULT
)
9323 event_drain(XCB_ENTER_NOTIFY
);
9326 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
9330 motionnotify(xcb_motion_notify_event_t
*e
)
9332 struct swm_region
*r
= NULL
;
9335 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): %#x "
9336 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
9337 "child: %#x, same_screen_focus: %s, state: %d\n",
9338 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
9339 get_notify_detail_label(e
->detail
), e
->detail
,
9340 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
9341 YESNO(e
->same_screen
), e
->state
);
9343 last_event_time
= e
->time
;
9345 if (focus_mode
== SWM_FOCUS_MANUAL
)
9348 num_screens
= get_screen_count();
9349 for (i
= 0; i
< num_screens
; i
++)
9350 if (screens
[i
].root
== e
->root
)
9353 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9354 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
9355 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
9363 get_atom_name(xcb_atom_t atom
)
9366 #ifdef SWM_DEBUG_ATOM_NAMES
9368 * This should be disabled during most debugging since
9369 * xcb_get_* causes an xcb_flush.
9372 xcb_get_atom_name_reply_t
*r
;
9374 r
= xcb_get_atom_name_reply(conn
,
9375 xcb_get_atom_name(conn
, atom
),
9378 len
= xcb_get_atom_name_name_length(r
);
9380 name
= malloc(len
+ 1);
9382 memcpy(name
, xcb_get_atom_name_name(r
), len
);
9396 propertynotify(xcb_property_notify_event_t
*e
)
9399 struct workspace
*ws
;
9403 name
= get_atom_name(e
->atom
);
9404 DNPRINTF(SWM_D_EVENT
, "propertynotify: win %#x, atom: %s(%u), "
9405 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
9409 win
= find_window(e
->window
);
9415 last_event_time
= e
->time
;
9417 if (e
->atom
== a_state
) {
9418 /* State just changed, make sure it gets focused if mapped. */
9419 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
9420 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
9422 ws
->focus_pending
== win
) {
9423 focus_win(ws
->focus_pending
);
9424 ws
->focus_pending
= NULL
;
9428 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
9429 e
->atom
== XCB_ATOM_WM_NAME
) {
9431 } else if (e
->atom
== a_prot
) {
9432 get_wm_protocols(win
);
9439 unmapnotify(xcb_unmap_notify_event_t
*e
)
9442 struct workspace
*ws
;
9444 DNPRINTF(SWM_D_EVENT
, "unmapnotify: win %#x\n", e
->window
);
9446 /* If we aren't managing the window, then ignore. */
9447 win
= find_window(e
->window
);
9448 if (win
== NULL
|| win
->id
!= e
->window
)
9451 /* Do nothing if already withdrawn. */
9452 if (!win
->mapped
&& !ICONIC(win
))
9458 /* If win was focused, make sure to focus on something else. */
9459 if (win
== ws
->focus
) {
9460 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
9461 ws
->focus_pending
= get_focus_prev(win
);
9462 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
9463 "focus_pending: %#x\n",
9464 WINID(ws
->focus_pending
));
9472 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
9475 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
9476 unmanage_window(win
);
9482 /* Update focus if ws is active. */
9483 if (WS_FOCUSED(ws
)) {
9484 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
9485 focus_win(get_pointer_win(ws
->r
->s
->root
));
9486 } else if (ws
->focus_pending
) {
9487 focus_win(ws
->focus_pending
);
9488 ws
->focus_pending
= NULL
;
9489 } else if (ws
->focus
== NULL
) {
9490 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_PARENT
,
9491 ws
->r
->id
, XCB_CURRENT_TIME
);
9499 clientmessage(xcb_client_message_event_t
*e
)
9502 struct swm_region
*r
= NULL
;
9506 xcb_map_request_event_t mre
;
9510 name
= get_atom_name(e
->type
);
9511 DNPRINTF(SWM_D_EVENT
, "clientmessage: win %#x, atom: %s(%u)\n",
9512 e
->window
, name
, e
->type
);
9516 if (e
->type
== ewmh
[_NET_CURRENT_DESKTOP
].atom
) {
9517 num_screens
= get_screen_count();
9518 for (i
= 0; i
< num_screens
; i
++)
9519 if (screens
[i
].root
== e
->window
) {
9520 r
= screens
[i
].r_focus
;
9524 if (r
&& e
->data
.data32
[0] <
9525 (uint32_t)workspace_limit
) {
9526 a
.id
= e
->data
.data32
[0];
9534 win
= find_window(e
->window
);
9536 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
9537 /* Manage the window with maprequest. */
9538 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
9539 "unmanaged window.\n");
9540 mre
.window
= e
->window
;
9546 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
9547 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW\n");
9548 if (WS_FOCUSED(win
->ws
))
9551 win
->ws
->focus_pending
= win
;
9552 } else if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
9553 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
9554 if (win
->can_delete
)
9555 client_msg(win
, a_delete
, 0);
9557 xcb_kill_client(conn
, win
->id
);
9558 } else if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
9559 DNPRINTF(SWM_D_EVENT
,
9560 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
9562 if (e
->data
.data32
[0] & (1<<8)) /* x */
9563 X(win
) = e
->data
.data32
[1];
9564 if (e
->data
.data32
[0] & (1<<9)) /* y */
9565 Y(win
) = e
->data
.data32
[2];
9566 if (e
->data
.data32
[0] & (1<<10)) /* width */
9567 WIDTH(win
) = e
->data
.data32
[3];
9568 if (e
->data
.data32
[0] & (1<<11)) /* height */
9569 HEIGHT(win
) = e
->data
.data32
[4];
9573 /* TODO: Change stack sizes */
9574 /* notify no change was made. */
9575 config_win(win
, NULL
);
9577 } else if (e
->type
== ewmh
[_NET_RESTACK_WINDOW
].atom
) {
9578 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_RESTACK_WINDOW\n");
9579 val
[0] = e
->data
.data32
[1]; /* Sibling window. */
9580 val
[1] = e
->data
.data32
[2]; /* Stack mode detail. */
9582 xcb_configure_window(conn
, win
->id
, XCB_CONFIG_WINDOW_SIBLING
|
9583 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
9584 } else if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
9585 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
9586 ewmh_change_wm_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
9587 if (e
->data
.data32
[2])
9588 ewmh_change_wm_state(win
, e
->data
.data32
[2],
9591 ewmh_update_wm_state(win
);
9601 int errcode
= xcb_connection_has_error(conn
);
9602 #ifdef XCB_CONN_ERROR
9605 case XCB_CONN_ERROR
:
9606 s
= "Socket error, pipe error or other stream error.";
9608 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
9609 s
= "Extension not supported.";
9611 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
9612 s
= "Insufficient memory.";
9614 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
9615 s
= "Request length was exceeded.";
9617 case XCB_CONN_CLOSED_PARSE_ERR
:
9618 s
= "Error parsing display string.";
9621 s
= "Unknown error.";
9624 errx(errcode
, "X CONNECTION ERROR: %s", s
);
9627 errx(errcode
, "X CONNECTION ERROR");
9635 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
9636 XCB_EVENT_MASK_ENTER_WINDOW
;
9638 xcb_void_cookie_t wac
;
9639 xcb_generic_error_t
*error
;
9641 /* this causes an error if some other window manager is running */
9642 num_screens
= get_screen_count();
9643 for (i
= 0; i
< num_screens
; i
++) {
9644 if ((sc
= get_screen(i
)) == NULL
)
9645 errx(1, "ERROR: can't get screen %d.", i
);
9646 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: %#x\n",
9648 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
9649 XCB_CW_EVENT_MASK
, &val
);
9650 if ((error
= xcb_request_check(conn
, wac
))) {
9651 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
9657 /* click to focus on empty region */
9658 xcb_grab_button(conn
, 1, sc
->root
, BUTTONMASK
,
9659 XCB_GRAB_MODE_SYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
9660 XCB_CURSOR_NONE
, XCB_BUTTON_INDEX_1
, XCB_BUTTON_MASK_ANY
);
9667 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
9669 struct swm_region
*r
= NULL
, *n
;
9670 struct workspace
*ws
= NULL
;
9674 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
9675 s
->idx
, w
, h
, x
, y
);
9677 /* remove any conflicting regions */
9678 n
= TAILQ_FIRST(&s
->rl
);
9681 n
= TAILQ_NEXT(r
, entry
);
9682 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
9683 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
9684 if (r
->ws
->r
!= NULL
)
9685 r
->ws
->old_r
= r
->ws
->r
;
9688 xcb_destroy_window(conn
, r
->id
);
9689 TAILQ_REMOVE(&s
->rl
, r
, entry
);
9690 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
9694 /* search old regions for one to reuse */
9696 /* size + location match */
9697 TAILQ_FOREACH(r
, &s
->orl
, entry
)
9698 if (X(r
) == x
&& Y(r
) == y
&&
9699 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
9703 TAILQ_FOREACH(r
, &s
->orl
, entry
)
9704 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
9708 TAILQ_REMOVE(&s
->orl
, r
, entry
);
9709 /* try to use old region's workspace */
9710 if (r
->ws
->r
== NULL
)
9713 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
9714 err(1, "new_region: calloc: failed to allocate memory "
9717 /* if we don't have a workspace already, find one */
9719 for (i
= 0; i
< workspace_limit
; i
++)
9720 if (s
->ws
[i
].r
== NULL
) {
9727 errx(1, "new_region: no free workspaces");
9729 if (ws
->state
== SWM_WS_STATE_HIDDEN
)
9730 ws
->state
= SWM_WS_STATE_MAPPING
;
9741 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
9743 /* Invisible region window to detect pointer events on empty regions. */
9744 r
->id
= xcb_generate_id(conn
);
9745 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
9746 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
9748 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
9749 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
9750 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
9752 /* Make sure region input is at the bottom. */
9753 wa
[0] = XCB_STACK_MODE_BELOW
;
9754 xcb_configure_window(conn
, r
->id
, XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
9756 xcb_map_window(conn
, r
->id
);
9762 #ifdef SWM_XRR_HAS_CRTC
9765 #endif /* SWM_XRR_HAS_CRTC */
9766 struct swm_region
*r
;
9769 xcb_randr_get_screen_resources_current_cookie_t src
;
9770 xcb_randr_get_screen_resources_current_reply_t
*srr
;
9771 xcb_randr_get_crtc_info_cookie_t cic
;
9772 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
9773 xcb_randr_crtc_t
*crtc
;
9774 xcb_screen_t
*screen
;
9776 DNPRINTF(SWM_D_MISC
, "scan_xrandr: screen: %d\n", i
);
9778 if ((screen
= get_screen(i
)) == NULL
)
9779 errx(1, "ERROR: can't get screen %d.", i
);
9781 num_screens
= get_screen_count();
9782 if (i
>= num_screens
)
9783 errx(1, "scan_xrandr: invalid screen");
9785 /* remove any old regions */
9786 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
9787 r
->ws
->old_r
= r
->ws
->r
= NULL
;
9789 xcb_destroy_window(conn
, r
->id
);
9790 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
9791 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
9795 /* map virtual screens onto physical screens */
9796 #ifdef SWM_XRR_HAS_CRTC
9797 if (xrandr_support
) {
9798 src
= xcb_randr_get_screen_resources_current(conn
,
9800 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
9803 new_region(&screens
[i
], 0, 0,
9804 screen
->width_in_pixels
,
9805 screen
->height_in_pixels
);
9808 ncrtc
= srr
->num_crtcs
;
9810 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
9811 for (c
= 0; c
< ncrtc
; c
++) {
9812 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
9814 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
9817 if (cir
->num_outputs
== 0) {
9823 new_region(&screens
[i
], 0, 0,
9824 screen
->width_in_pixels
,
9825 screen
->height_in_pixels
);
9827 new_region(&screens
[i
],
9828 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
9833 #endif /* SWM_XRR_HAS_CRTC */
9835 /* If detection failed, create a single region that spans the screen. */
9836 if (TAILQ_EMPTY(&screens
[i
].rl
))
9837 new_region(&screens
[i
], 0, 0, screen
->width_in_pixels
,
9838 screen
->height_in_pixels
);
9841 /* Cleanup unused previously visible workspaces. */
9842 TAILQ_FOREACH(r
, &screens
[i
].orl
, entry
) {
9843 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
9845 r
->ws
->state
= SWM_WS_STATE_HIDDEN
;
9847 /* The screen shouldn't focus on an unused region. */
9848 if (screens
[i
].r_focus
== r
)
9849 screens
[i
].r_focus
= NULL
;
9852 DNPRINTF(SWM_D_MISC
, "scan_xrandr: done.\n");
9856 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
9858 struct swm_region
*r
;
9861 DNPRINTF(SWM_D_EVENT
, "screenchange: root: %#x\n", e
->root
);
9863 num_screens
= get_screen_count();
9864 /* silly event doesn't include the screen index */
9865 for (i
= 0; i
< num_screens
; i
++)
9866 if (screens
[i
].root
== e
->root
)
9868 if (i
>= num_screens
)
9869 errx(1, "screenchange: screen not found");
9871 /* brute force for now, just re-enumerate the regions */
9875 print_win_geom(e
->root
);
9877 /* add bars to all regions */
9878 for (i
= 0; i
< num_screens
; i
++) {
9879 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9885 /* Make sure a region has focus on each screen. */
9886 for (i
= 0; i
< num_screens
; i
++) {
9887 if (screens
[i
].r_focus
== NULL
) {
9888 r
= TAILQ_FIRST(&screens
[i
].rl
);
9897 /* Update workspace state on all regions. */
9898 for (i
= 0; i
< num_screens
; i
++)
9899 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
9900 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
9906 struct swm_region
*r
= NULL
;
9907 xcb_window_t
*wins
= NULL
, trans
, *cwins
= NULL
;
9908 int i
, j
, k
, n
, no
, num_screens
, manage
, mapped
;
9911 xcb_query_tree_cookie_t qtc
;
9912 xcb_query_tree_reply_t
*qtr
;
9913 xcb_get_window_attributes_cookie_t gac
;
9914 xcb_get_window_attributes_reply_t
*gar
;
9915 xcb_get_property_cookie_t pc
;
9916 xcb_get_property_reply_t
*pr
;
9918 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
9919 num_screens
= get_screen_count();
9920 for (i
= 0; i
< num_screens
; i
++) {
9921 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
9922 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
9925 wins
= xcb_query_tree_children(qtr
);
9926 no
= xcb_query_tree_children_length(qtr
);
9928 /* Try to sort windows according to _NET_CLIENT_LIST. */
9929 pr
= xcb_get_property_reply(conn
, xcb_get_property(conn
, 0,
9930 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
9931 XCB_ATOM_WINDOW
, 0, UINT32_MAX
), NULL
);
9933 cwins
= xcb_get_property_value(pr
);
9934 n
= xcb_get_property_value_length(pr
) /
9937 for (j
= 0; j
< n
; ++j
) {
9938 for (k
= j
; k
< no
; ++k
) {
9939 if (wins
[k
] == cwins
[j
]) {
9940 /* Swap wins j and k. */
9950 /* attach windows to a region */
9951 /* normal windows */
9952 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top level windows.\n");
9953 for (j
= 0; j
< no
; j
++) {
9954 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
9955 if (r
->id
== wins
[j
]) {
9956 DNPRINTF(SWM_D_INIT
, "grab_windows: "
9957 "skip %#x; region input window.\n",
9966 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
9967 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
9969 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
9970 "doesn't exist.\n", wins
[j
]);
9974 if (gar
->override_redirect
) {
9975 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
9976 "override_redirect set.\n", wins
[j
]);
9981 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
9982 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
9984 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
9985 "is transient for %#x.\n", wins
[j
], trans
);
9990 state
= getstate(wins
[j
]);
9991 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
9992 mapped
= gar
->map_state
== XCB_MAP_STATE_VIEWABLE
;
9993 if (mapped
|| manage
)
9994 manage_window(wins
[j
], mapped
);
9997 /* transient windows */
9998 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
9999 for (j
= 0; j
< no
; j
++) {
10000 gac
= xcb_get_window_attributes(conn
, wins
[j
]);
10001 gar
= xcb_get_window_attributes_reply(conn
, gac
, NULL
);
10003 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10004 "doesn't exist.\n", wins
[j
]);
10008 if (gar
->override_redirect
) {
10009 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
10010 "override_redirect set.\n", wins
[j
]);
10015 state
= getstate(wins
[j
]);
10016 manage
= state
!= XCB_ICCCM_WM_STATE_WITHDRAWN
;
10017 mapped
= gar
->map_state
== XCB_MAP_STATE_VIEWABLE
;
10018 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
10019 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
10020 &trans
, NULL
) && manage
)
10021 manage_window(wins
[j
], mapped
);
10026 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
10030 setup_screens(void)
10032 int i
, j
, k
, num_screens
;
10033 struct workspace
*ws
;
10034 uint32_t gcv
[1], wa
[1];
10035 const xcb_query_extension_reply_t
*qep
;
10036 xcb_screen_t
*screen
;
10037 xcb_randr_query_version_cookie_t c
;
10038 xcb_randr_query_version_reply_t
*r
;
10040 num_screens
= get_screen_count();
10041 if ((screens
= calloc(num_screens
,
10042 sizeof(struct swm_screen
))) == NULL
)
10043 err(1, "setup_screens: calloc: failed to allocate memory for "
10046 /* initial Xrandr setup */
10047 xrandr_support
= 0;
10048 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
10049 if (qep
->present
) {
10050 c
= xcb_randr_query_version(conn
, 1, 1);
10051 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
10053 if (r
->major_version
>= 1) {
10054 xrandr_support
= 1;
10055 xrandr_eventbase
= qep
->first_event
;
10061 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
10063 /* map physical screens */
10064 for (i
= 0; i
< num_screens
; i
++) {
10065 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
10066 screens
[i
].idx
= i
;
10067 screens
[i
].r_focus
= NULL
;
10069 TAILQ_INIT(&screens
[i
].rl
);
10070 TAILQ_INIT(&screens
[i
].orl
);
10071 if ((screen
= get_screen(i
)) == NULL
)
10072 errx(1, "ERROR: can't get screen %d.", i
);
10073 screens
[i
].root
= screen
->root
;
10075 /* set default colors */
10076 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
10077 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
10078 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
10079 setscreencolor("rgb:00/40/40", i
+ 1,
10080 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
10081 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
10082 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
10084 /* create graphics context on screen */
10085 screens
[i
].bar_gc
= xcb_generate_id(conn
);
10087 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
10088 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
10090 /* set default cursor */
10091 xcb_change_window_attributes(conn
, screens
[i
].root
,
10092 XCB_CW_CURSOR
, wa
);
10094 /* init all workspaces */
10095 /* XXX these should be dynamically allocated too */
10096 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
10097 ws
= &screens
[i
].ws
[j
];
10100 ws
->bar_enabled
= 1;
10102 ws
->focus_prev
= NULL
;
10103 ws
->focus_pending
= NULL
;
10106 ws
->state
= SWM_WS_STATE_HIDDEN
;
10107 TAILQ_INIT(&ws
->stack
);
10108 TAILQ_INIT(&ws
->winlist
);
10109 TAILQ_INIT(&ws
->unmanagedlist
);
10111 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
10112 if (layouts
[k
].l_config
!= NULL
)
10113 layouts
[k
].l_config(ws
,
10114 SWM_ARG_ID_STACKINIT
);
10115 ws
->cur_layout
= &layouts
[0];
10116 ws
->cur_layout
->l_string(ws
);
10121 if (xrandr_support
)
10122 xcb_randr_select_input(conn
, screens
[i
].root
,
10123 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
10128 setup_globals(void)
10130 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
10131 err(1, "setup_globals: strdup: failed to allocate memory.");
10133 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
10134 err(1, "setup_globals: strdup: failed to allocate memory.");
10136 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
10137 errx(1, "unable to allocate key symbols");
10139 a_state
= get_atom_from_string("WM_STATE");
10140 a_prot
= get_atom_from_string("WM_PROTOCOLS");
10141 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
10142 a_net_supported
= get_atom_from_string("_NET_SUPPORTED");
10143 a_net_wm_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
10144 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
10145 a_utf8_string
= get_atom_from_string("UTF8_STRING");
10146 a_swm_ws
= get_atom_from_string("_SWM_WS");
10150 shutdown_cleanup(void)
10152 int i
, num_screens
;
10154 /* disable alarm because the following code may not be interrupted */
10156 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
10157 err(1, "can't disable alarm");
10166 num_screens
= get_screen_count();
10167 for (i
= 0; i
< num_screens
; ++i
) {
10168 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
10169 screens
[i
].root
, XCB_CURRENT_TIME
);
10171 if (screens
[i
].bar_gc
!= XCB_NONE
)
10172 xcb_free_gc(conn
, screens
[i
].bar_gc
);
10173 if (!bar_font_legacy
)
10174 XftColorFree(display
, DefaultVisual(display
, i
),
10175 DefaultColormap(display
, i
), &bar_font_color
);
10178 if (bar_font_legacy
)
10179 XFreeFontSet(display
, bar_fs
);
10181 XftFontClose(display
, bar_font
);
10184 xcb_key_symbols_free(syms
);
10186 xcb_disconnect(conn
);
10190 event_error(xcb_generic_error_t
*e
)
10194 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
10195 "resource_id: %u, minor_code: %u\n",
10196 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
10197 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
10198 e
->sequence
, e
->resource_id
, e
->minor_code
);
10202 event_handle(xcb_generic_event_t
*evt
)
10204 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
10206 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
10207 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
10208 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
10211 #define EVENT(type, callback) case type: callback((void *)evt); return
10212 EVENT(0, event_error
);
10213 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
10214 /*EVENT(XCB_BUTTON_RELEASE, buttonpress);*/
10215 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
10216 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
10217 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
10218 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
10219 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
10220 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
10221 /*EVENT(XCB_CREATE_NOTIFY, );*/
10222 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
10223 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
10224 EVENT(XCB_EXPOSE
, expose
);
10226 EVENT(XCB_FOCUS_IN
, focusin
);
10227 EVENT(XCB_FOCUS_OUT
, focusout
);
10229 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
10230 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
10231 EVENT(XCB_KEY_PRESS
, keypress
);
10232 /*EVENT(XCB_KEY_RELEASE, keypress);*/
10233 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
10235 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
10237 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
10238 EVENT(XCB_MAP_REQUEST
, maprequest
);
10239 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
10240 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
10241 /*EVENT(XCB_NO_EXPOSURE, );*/
10242 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
10243 /*EVENT(XCB_REPARENT_NOTIFY, );*/
10244 /*EVENT(XCB_RESIZE_REQUEST, );*/
10245 /*EVENT(XCB_SELECTION_CLEAR, );*/
10246 /*EVENT(XCB_SELECTION_NOTIFY, );*/
10247 /*EVENT(XCB_SELECTION_REQUEST, );*/
10248 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
10249 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
10252 if (type
- xrandr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
10253 screenchange((void *)evt
);
10257 main(int argc
, char *argv
[])
10259 struct swm_region
*r
;
10260 char conf
[PATH_MAX
], *cfile
= NULL
;
10262 int xfd
, i
, num_screens
, startup
= 1;
10263 struct sigaction sact
;
10264 xcb_generic_event_t
*evt
;
10268 int stdin_ready
= 0;
10271 /* suppress unused warning since var is needed */
10274 time_started
= time(NULL
);
10277 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
10278 if (setlocale(LC_CTYPE
, "") == NULL
|| setlocale(LC_TIME
, "") == NULL
)
10279 warnx("no locale support");
10281 /* handle some signals */
10282 bzero(&sact
, sizeof(sact
));
10283 sigemptyset(&sact
.sa_mask
);
10285 sact
.sa_handler
= sighdlr
;
10286 sigaction(SIGINT
, &sact
, NULL
);
10287 sigaction(SIGQUIT
, &sact
, NULL
);
10288 sigaction(SIGTERM
, &sact
, NULL
);
10289 sigaction(SIGHUP
, &sact
, NULL
);
10291 sact
.sa_handler
= sighdlr
;
10292 sact
.sa_flags
= SA_NOCLDSTOP
;
10293 sigaction(SIGCHLD
, &sact
, NULL
);
10295 if ((display
= XOpenDisplay(0)) == NULL
)
10296 errx(1, "can not open display");
10298 conn
= XGetXCBConnection(display
);
10299 if (xcb_connection_has_error(conn
))
10300 errx(1, "can not get XCB connection");
10302 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
10304 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
10305 xfd
= xcb_get_file_descriptor(conn
);
10307 /* look for local and global conf file */
10308 pwd
= getpwuid(getuid());
10310 errx(1, "invalid user: %d", getuid());
10312 xcb_grab_server(conn
);
10313 xcb_aux_sync(conn
);
10315 /* flush all events */
10316 while ((evt
= xcb_poll_for_event(conn
))) {
10317 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
10323 errx(1, "another window manager is currently running");
10325 /* Load Xcursors and/or cursorfont glyph cursors. */
10328 xcb_aux_sync(conn
);
10338 for (i
= 0; ; i
++) {
10343 snprintf(conf
, sizeof conf
, "%s/.%s",
10344 pwd
->pw_dir
, SWM_CONF_FILE
);
10348 snprintf(conf
, sizeof conf
, "/etc/%s",
10353 snprintf(conf
, sizeof conf
, "%s/.%s",
10354 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
10357 /* global compat */
10358 snprintf(conf
, sizeof conf
, "/etc/%s",
10359 SWM_CONF_FILE_OLD
);
10365 if (strlen(conf
) && stat(conf
, &sb
) != -1)
10366 if (S_ISREG(sb
.st_mode
)) {
10373 /* load conf (if any) */
10375 conf_load(cfile
, SWM_CONF_DEFAULT
);
10379 /* grab existing windows (before we build the bars) */
10382 if (getenv("SWM_STARTED") == NULL
)
10383 setenv("SWM_STARTED", "YES", 1);
10385 /* setup all bars */
10386 num_screens
= get_screen_count();
10387 for (i
= 0; i
< num_screens
; i
++)
10388 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10395 xcb_ungrab_server(conn
);
10398 /* Update state of each newly mapped workspace. */
10399 for (i
= 0; i
< num_screens
; i
++)
10400 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10401 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
10403 rd_max
= xfd
> STDIN_FILENO
? xfd
: STDIN_FILENO
;
10406 while ((evt
= xcb_poll_for_event(conn
))) {
10413 /* If just (re)started, set default focus if needed. */
10417 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10418 r
= TAILQ_FIRST(&screens
[0].rl
);
10430 FD_SET(STDIN_FILENO
, &rd
);
10435 num_readable
= select(rd_max
+ 1, &rd
, NULL
, NULL
, &tv
);
10436 if (num_readable
== -1 && errno
!= EINTR
) {
10437 DNPRINTF(SWM_D_MISC
, "select failed");
10438 } else if (num_readable
> 0 && FD_ISSET(STDIN_FILENO
, &rd
)) {
10443 restart(NULL
, NULL
);
10453 bar_extra_update();
10460 shutdown_cleanup();