2 * Copyright (c) 2009-2015 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2016 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 * Copyright (c) 2012-2015 David Hill <dhill@mindcry.org>
12 * Copyright (c) 2014-2015 Yuri D'Elia <yuri.delia@eurac.edu>
14 * Permission to use, copy, modify, and distribute this software for any
15 * purpose with or without fee is hereby granted, provided that the above
16 * copyright notice and this permission notice appear in all copies.
18 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
19 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
21 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
23 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
24 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 #include <sys/types.h>
35 #include <sys/queue.h>
37 #include <sys/param.h>
38 #include <sys/select.h>
39 #if defined(__linux__)
41 #elif defined(__OpenBSD__)
43 #elif defined(__FreeBSD__)
67 #include <X11/cursorfont.h>
68 #include <X11/extensions/Xrandr.h>
69 #include <X11/Xcursor/Xcursor.h>
70 #include <X11/Xft/Xft.h>
71 #include <X11/Xlib-xcb.h>
72 #include <xcb/xcb_atom.h>
73 #include <xcb/xcb_aux.h>
74 #include <xcb/xcb_event.h>
75 #include <xcb/xcb_icccm.h>
76 #include <xcb/xcb_keysyms.h>
77 #include <xcb/xtest.h>
78 #include <xcb/randr.h>
86 #ifdef SPECTRWM_BUILDSTR
87 static const char *buildstr
= SPECTRWM_BUILDSTR
;
89 static const char *buildstr
= SPECTRWM_VERSION
;
92 #if !defined(__CYGWIN__) /* cygwin chokes on randr stuff */
94 # error RandR versions less than 1.0 are not supported
99 # define SWM_XRR_HAS_CRTC
102 #endif /* __CYGWIN__ */
104 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
105 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
106 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
107 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
108 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
109 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
110 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
111 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
112 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
113 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
114 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
115 #define xcb_icccm_get_wm_class xcb_get_wm_class
116 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
117 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
118 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
119 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
120 #define xcb_icccm_wm_hints_get_urgency xcb_wm_hints_get_urgency
121 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
122 #define xcb_icccm_get_wm_name xcb_get_wm_name
123 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
124 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
125 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
126 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
127 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
128 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
129 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
130 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
131 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
132 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
135 /*#define SWM_DEBUG*/
137 #define DPRINTF(x...) do { \
139 fprintf(stderr, x); \
141 #define DNPRINTF(n,x...) do { \
142 if (swm_debug & n) { \
143 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
144 fprintf(stderr, x); \
147 #define SWM_D_MISC 0x0001
148 #define SWM_D_EVENT 0x0002
149 #define SWM_D_WS 0x0004
150 #define SWM_D_FOCUS 0x0008
151 #define SWM_D_MOVE 0x0010
152 #define SWM_D_STACK 0x0020
153 #define SWM_D_MOUSE 0x0040
154 #define SWM_D_PROP 0x0080
155 #define SWM_D_CLASS 0x0100
156 #define SWM_D_KEY 0x0200
157 #define SWM_D_QUIRK 0x0400
158 #define SWM_D_SPAWN 0x0800
159 #define SWM_D_EVENTQ 0x1000
160 #define SWM_D_CONF 0x2000
161 #define SWM_D_BAR 0x4000
162 #define SWM_D_INIT 0x8000
164 uint32_t swm_debug
= 0
183 #define DPRINTF(x...)
184 #define DNPRINTF(n,x...)
187 #define SWM_EWMH_ACTION_COUNT_MAX (8)
188 #define EWMH_F_FULLSCREEN (0x001)
189 #define EWMH_F_ABOVE (0x002)
190 #define EWMH_F_HIDDEN (0x004)
191 #define EWMH_F_MAXIMIZED_VERT (0x008)
192 #define EWMH_F_MAXIMIZED_HORZ (0x010)
193 #define EWMH_F_SKIP_PAGER (0x020)
194 #define EWMH_F_SKIP_TASKBAR (0x040)
195 #define SWM_F_MANUAL (0x080)
197 #define EWMH_F_MAXIMIZED (EWMH_F_MAXIMIZED_VERT | EWMH_F_MAXIMIZED_HORZ)
199 /* convert 8-bit to 16-bit */
200 #define RGB_8_TO_16(col) (((col) << 8) + (col))
202 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
203 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
204 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
205 xrc.blue = RGB_8_TO_16((px) & 0xff); \
208 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
209 #define MODKEY XCB_MOD_MASK_1
210 #define ANYMOD XCB_MOD_MASK_ANY
211 #define CLEANMASK(mask) ((mask) & (XCB_KEY_BUT_MASK_SHIFT | \
212 XCB_KEY_BUT_MASK_CONTROL | XCB_KEY_BUT_MASK_MOD_1 | \
213 XCB_KEY_BUT_MASK_MOD_2 | XCB_KEY_BUT_MASK_MOD_3 | \
214 XCB_KEY_BUT_MASK_MOD_4 | XCB_KEY_BUT_MASK_MOD_5) & ~(numlockmask))
215 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
216 XCB_EVENT_MASK_BUTTON_RELEASE)
217 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
218 #define SWM_PROPLEN (16)
219 #define SWM_FUNCNAME_LEN (32)
220 #define SWM_QUIRK_LEN (64)
221 #define X(r) ((r)->g.x)
222 #define Y(r) ((r)->g.y)
223 #define WIDTH(r) ((r)->g.w)
224 #define HEIGHT(r) ((r)->g.h)
225 #define MAX_X(r) ((r)->g.x + (r)->g.w)
226 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
227 #define SH_MIN(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
228 #define SH_MIN_W(w) ((w)->sh.min_width)
229 #define SH_MIN_H(w) ((w)->sh.min_height)
230 #define SH_MAX(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
231 #define SH_MAX_W(w) ((w)->sh.max_width)
232 #define SH_MAX_H(w) ((w)->sh.max_height)
233 #define SH_INC(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)
234 #define SH_INC_W(w) ((w)->sh.width_inc)
235 #define SH_INC_H(w) ((w)->sh.height_inc)
236 #define SWM_MAX_FONT_STEPS (3)
237 #define WINID(w) ((w) ? (w)->id : XCB_WINDOW_NONE)
238 #define ACCEPTS_FOCUS(w) (!((w)->hints.flags & XCB_ICCCM_WM_HINT_INPUT) \
239 || ((w)->hints.input))
240 #define WS_FOCUSED(ws) ((ws)->r && (ws)->r->s->r_focus == (ws)->r)
241 #define YESNO(x) ((x) ? "yes" : "no")
242 #define ICONIC(w) ((w)->ewmh_flags & EWMH_F_HIDDEN)
243 #define ABOVE(w) ((w)->ewmh_flags & EWMH_F_ABOVE)
244 #define FULLSCREEN(w) ((w)->ewmh_flags & EWMH_F_FULLSCREEN)
245 #define MAXIMIZED_VERT(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_VERT)
246 #define MAXIMIZED_HORZ(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_HORZ)
247 #define MAXIMIZED(w) (MAXIMIZED_VERT(w) || MAXIMIZED_HORZ(w))
248 #define MANUAL(w) ((w)->ewmh_flags & SWM_F_MANUAL)
249 #define TRANS(w) ((w)->transient != XCB_WINDOW_NONE)
250 #define FLOATING(w) (ABOVE(w) || TRANS(w) || FULLSCREEN(w) || \
253 /* Constrain Window flags */
254 #define SWM_CW_RESIZABLE (0x01)
255 #define SWM_CW_SOFTBOUNDARY (0x02)
256 #define SWM_CW_HARDBOUNDARY (0x04)
257 #define SWM_CW_RIGHT (0x10)
258 #define SWM_CW_LEFT (0x20)
259 #define SWM_CW_BOTTOM (0x40)
260 #define SWM_CW_TOP (0x80)
261 #define SWM_CW_ALLSIDES (0xf0)
263 #define SWM_FOCUS_DEFAULT (0)
264 #define SWM_FOCUS_FOLLOW (1)
265 #define SWM_FOCUS_MANUAL (2)
267 #define SWM_CK_ALL (0xf)
268 #define SWM_CK_FOCUS (0x1)
269 #define SWM_CK_POINTER (0x2)
270 #define SWM_CK_FALLBACK (0x4)
271 #define SWM_CK_REGION (0x8)
273 #define SWM_CONF_DEFAULT (0)
274 #define SWM_CONF_KEYMAPPING (1)
277 #define SWM_LIB "/usr/local/lib/libswmhack.so"
284 xcb_atom_t a_net_frame_extents
;
285 xcb_atom_t a_net_wm_check
;
286 xcb_atom_t a_net_supported
;
287 xcb_atom_t a_takefocus
;
288 xcb_atom_t a_utf8_string
;
290 volatile sig_atomic_t running
= 1;
291 volatile sig_atomic_t restart_wm
= 0;
292 xcb_timestamp_t last_event_time
= 0;
296 unsigned int numlockmask
= 0;
299 xcb_connection_t
*conn
;
300 xcb_key_symbols_t
*syms
;
302 int boundary_width
= 50;
303 bool cycle_empty
= false;
304 bool cycle_visible
= false;
306 int font_adjusted
= 0;
307 uint16_t mod_key
= MODKEY
;
308 bool warp_focus
= false;
309 bool warp_pointer
= false;
310 bool workspace_clamp
= false;
313 struct swm_region
*search_r
;
314 int select_list_pipe
[2];
315 int select_resp_pipe
[2];
317 volatile sig_atomic_t search_resp
;
318 int search_resp_action
;
320 struct search_window
{
321 TAILQ_ENTRY(search_window
) entry
;
325 xcb_window_t indicator
;
327 TAILQ_HEAD(search_winlist
, search_window
);
328 struct search_winlist search_wl
;
333 SWM_SEARCH_UNICONIFY
,
334 SWM_SEARCH_NAME_WORKSPACE
,
335 SWM_SEARCH_SEARCH_WORKSPACE
,
336 SWM_SEARCH_SEARCH_WINDOW
339 #define SWM_STACK_TOP (0)
340 #define SWM_STACK_BOTTOM (1)
341 #define SWM_STACK_ABOVE (2)
342 #define SWM_STACK_BELOW (3)
345 double dialog_ratio
= 0.6;
347 #define SWM_BAR_MAX (256)
348 #define SWM_BAR_JUSTIFY_LEFT (0)
349 #define SWM_BAR_JUSTIFY_CENTER (1)
350 #define SWM_BAR_JUSTIFY_RIGHT (2)
351 #define SWM_BAR_OFFSET (4)
352 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
353 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
354 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
355 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
356 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
358 #ifdef X_HAVE_UTF8_STRING
359 #define DRAWSTRING(x...) Xutf8DrawString(x)
361 #define DRAWSTRING(x...) XmbDrawString(x)
364 char *bar_argv
[] = { NULL
, NULL
};
366 char bar_ext
[SWM_BAR_MAX
];
367 char bar_ext_buf
[SWM_BAR_MAX
];
368 char bar_vertext
[SWM_BAR_MAX
];
369 bool bar_version
= false;
370 bool bar_enabled
= true;
371 int bar_border_width
= 1;
372 bool bar_at_bottom
= false;
373 bool bar_extra
= false;
375 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
376 char *bar_format
= NULL
;
377 bool stack_enabled
= true;
378 bool clock_enabled
= true;
379 bool iconic_enabled
= false;
380 bool maximize_hide_bar
= false;
381 bool urgent_enabled
= false;
382 bool urgent_collapse
= false;
383 char *clock_format
= NULL
;
384 bool window_class_enabled
= false;
385 bool window_instance_enabled
= false;
386 bool window_name_enabled
= false;
387 int focus_mode
= SWM_FOCUS_DEFAULT
;
388 int focus_close
= SWM_STACK_BELOW
;
389 bool focus_close_wrap
= true;
390 int focus_default
= SWM_STACK_TOP
;
391 int spawn_position
= SWM_STACK_TOP
;
392 bool disable_border
= false;
393 int border_width
= 1;
394 int region_padding
= 0;
396 bool java_workaround
= true;
397 bool verbose_layout
= false;
403 XFontSet bar_fs
= NULL
;
404 XFontSetExtents
*bar_fs_extents
;
405 XftFont
*bar_font
= NULL
;
406 bool bar_font_legacy
= true;
407 char *bar_fonts
= NULL
;
408 XftColor bar_font_color
;
409 XftColor search_font_color
;
410 char *startup_exception
= NULL
;
411 unsigned int nr_exceptions
= 0;
413 /* layout manager data */
414 struct swm_geometry
{
427 struct swm_geometry g
;
428 struct swm_region
*r
; /* Associated region. */
431 /* virtual "screens" */
433 TAILQ_ENTRY(swm_region
) entry
;
435 struct swm_geometry g
;
436 struct workspace
*ws
; /* current workspace on this region */
437 struct workspace
*ws_prior
; /* prior workspace on this region */
438 struct swm_screen
*s
; /* screen idx */
441 TAILQ_HEAD(swm_region_list
, swm_region
);
444 SWM_WIN_STATE_REPARENTING
,
445 SWM_WIN_STATE_REPARENTED
,
446 SWM_WIN_STATE_UNPARENTING
,
447 SWM_WIN_STATE_UNPARENTED
,
451 TAILQ_ENTRY(ws_win
) entry
;
452 TAILQ_ENTRY(ws_win
) stack_entry
;
455 xcb_window_t transient
;
456 struct ws_win
*focus_child
; /* focus on child transient */
457 struct swm_geometry g
; /* current geometry */
458 struct swm_geometry g_prev
; /* prev configured geometry */
459 struct swm_geometry g_float
; /* region coordinates */
460 bool g_floatvalid
; /* g_float geometry validity */
465 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
472 struct workspace
*ws
; /* always valid */
473 struct swm_screen
*s
; /* always valid, never changes */
475 xcb_icccm_get_wm_class_reply_t ch
;
476 xcb_icccm_wm_hints_t hints
;
481 TAILQ_HEAD(ws_win_list
, ws_win
);
482 TAILQ_HEAD(ws_win_stack
, ws_win
);
486 TAILQ_ENTRY(pid_e
) entry
;
490 TAILQ_HEAD(pid_list
, pid_e
);
491 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
493 /* layout handlers */
494 void stack(struct swm_region
*);
495 void vertical_config(struct workspace
*, int);
496 void vertical_stack(struct workspace
*, struct swm_geometry
*);
497 void horizontal_config(struct workspace
*, int);
498 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
499 void max_stack(struct workspace
*, struct swm_geometry
*);
500 void plain_stacker(struct workspace
*);
501 void fancy_stacker(struct workspace
*);
504 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
505 void (*l_config
)(struct workspace
*, int);
507 #define SWM_L_FOCUSPREV (1<<0)
508 #define SWM_L_MAPONFOCUS (1<<1)
509 void (*l_string
)(struct workspace
*);
511 /* stack, configure */
512 { vertical_stack
, vertical_config
, 0, plain_stacker
},
513 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
515 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
516 { NULL
, NULL
, 0, NULL
},
519 /* position of max_stack mode in the layouts array, index into layouts! */
520 #define SWM_V_STACK (0)
521 #define SWM_H_STACK (1)
522 #define SWM_MAX_STACK (2)
524 #define SWM_H_SLICE (32)
525 #define SWM_V_SLICE (32)
527 /* define work spaces */
529 int idx
; /* workspace index */
530 char *name
; /* workspace name */
531 bool always_raise
; /* raise windows on focus */
532 bool bar_enabled
; /* bar visibility */
533 struct layout
*cur_layout
; /* current layout handlers */
534 struct ws_win
*focus
; /* may be NULL */
535 struct ws_win
*focus_prev
; /* may be NULL */
536 struct ws_win
*focus_pending
; /* may be NULL */
537 struct swm_region
*r
; /* may be NULL */
538 struct swm_region
*old_r
; /* may be NULL */
539 struct ws_win_list winlist
; /* list of windows in ws */
540 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
541 struct ws_win_stack stack
; /* stacking order */
542 int state
; /* mapping state */
543 char stacker
[10]; /* display stacker and layout */
547 int horizontal_msize
;
549 int horizontal_stacks
;
550 bool horizontal_flip
;
560 SWM_WS_STATE_MAPPING
,
566 SWM_S_COLOR_BAR_BORDER
,
567 SWM_S_COLOR_BAR_BORDER_UNFOCUS
,
568 SWM_S_COLOR_BAR_FONT
,
570 SWM_S_COLOR_FOCUS_MAXIMIZED
,
572 SWM_S_COLOR_UNFOCUS_MAXIMIZED
,
576 /* physical screen mapping */
577 #define SWM_WS_MAX (22) /* hard limit */
578 int workspace_limit
= 10; /* soft limit */
581 int idx
; /* screen index */
582 struct swm_region_list rl
; /* list of regions on this screen */
583 struct swm_region_list orl
; /* list of old regions */
585 struct workspace ws
[SWM_WS_MAX
];
586 struct swm_region
*r_focus
;
593 } c
[SWM_S_COLOR_MAX
];
595 xcb_gcontext_t bar_gc
;
598 struct swm_screen
*screens
;
600 /* args to functions */
603 #define SWM_ARG_ID_FOCUSNEXT (0)
604 #define SWM_ARG_ID_FOCUSPREV (1)
605 #define SWM_ARG_ID_FOCUSMAIN (2)
606 #define SWM_ARG_ID_FOCUSURGENT (3)
607 #define SWM_ARG_ID_SWAPNEXT (10)
608 #define SWM_ARG_ID_SWAPPREV (11)
609 #define SWM_ARG_ID_SWAPMAIN (12)
610 #define SWM_ARG_ID_MOVELAST (13)
611 #define SWM_ARG_ID_MASTERSHRINK (20)
612 #define SWM_ARG_ID_MASTERGROW (21)
613 #define SWM_ARG_ID_MASTERADD (22)
614 #define SWM_ARG_ID_MASTERDEL (23)
615 #define SWM_ARG_ID_FLIPLAYOUT (24)
616 #define SWM_ARG_ID_STACKRESET (30)
617 #define SWM_ARG_ID_STACKINIT (31)
618 #define SWM_ARG_ID_STACKBALANCE (32)
619 #define SWM_ARG_ID_CYCLEWS_UP (40)
620 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
621 #define SWM_ARG_ID_CYCLERG_UP (42)
622 #define SWM_ARG_ID_CYCLERG_DOWN (43)
623 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
624 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
625 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
626 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
627 #define SWM_ARG_ID_STACKINC (50)
628 #define SWM_ARG_ID_STACKDEC (51)
629 #define SWM_ARG_ID_DONTCENTER (70)
630 #define SWM_ARG_ID_CENTER (71)
631 #define SWM_ARG_ID_KILLWINDOW (80)
632 #define SWM_ARG_ID_DELETEWINDOW (81)
633 #define SWM_ARG_ID_WIDTHGROW (90)
634 #define SWM_ARG_ID_WIDTHSHRINK (91)
635 #define SWM_ARG_ID_HEIGHTGROW (92)
636 #define SWM_ARG_ID_HEIGHTSHRINK (93)
637 #define SWM_ARG_ID_MOVEUP (100)
638 #define SWM_ARG_ID_MOVEDOWN (101)
639 #define SWM_ARG_ID_MOVELEFT (102)
640 #define SWM_ARG_ID_MOVERIGHT (103)
641 #define SWM_ARG_ID_BAR_TOGGLE (110)
642 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
643 #define SWM_ARG_ID_CYCLERG_MOVE_UP (112)
644 #define SWM_ARG_ID_CYCLERG_MOVE_DOWN (113)
650 TAILQ_ENTRY(quirk
) entry
;
651 char *class; /* WM_CLASS:class */
652 char *instance
; /* WM_CLASS:instance */
653 char *name
; /* WM_NAME */
655 regex_t regex_instance
;
658 int ws
; /* Initial workspace. */
659 #define SWM_Q_FLOAT (1<<0) /* Float this window. */
660 #define SWM_Q_TRANSSZ (1<<1) /* Transient 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 when fullscreen. */
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. */
667 #define SWM_Q_OBEYAPPFOCUSREQ (1<<8) /* Focus when applications ask. */
668 #define SWM_Q_IGNOREPID (1<<9) /* Ignore PID when determining ws. */
669 #define SWM_Q_IGNORESPAWNWS (1<<10) /* Ignore _SWM_WS when managing win. */
670 #define SWM_Q_NOFOCUSCYCLE (1<<11) /* Remove from normal focus cycle. */
671 #define SWM_Q_MINIMALBORDER (1<<12) /* No border when floating/unfocused. */
673 TAILQ_HEAD(quirk_list
, quirk
);
674 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
677 * Supported EWMH hints should be added to
678 * both the enum and the ewmh array
684 _NET_CURRENT_DESKTOP
,
685 _NET_DESKTOP_GEOMETRY
,
687 _NET_DESKTOP_VIEWPORT
,
688 _NET_MOVERESIZE_WINDOW
,
689 _NET_NUMBER_OF_DESKTOPS
,
690 _NET_REQUEST_FRAME_EXTENTS
,
692 _NET_WM_ACTION_ABOVE
,
693 _NET_WM_ACTION_CLOSE
,
694 _NET_WM_ACTION_FULLSCREEN
,
696 _NET_WM_ACTION_RESIZE
,
697 _NET_WM_ALLOWED_ACTIONS
,
699 _NET_WM_FULL_PLACEMENT
,
703 _NET_WM_STATE_FULLSCREEN
,
704 _NET_WM_STATE_HIDDEN
,
705 _NET_WM_STATE_MAXIMIZED_VERT
,
706 _NET_WM_STATE_MAXIMIZED_HORZ
,
707 _NET_WM_STATE_SKIP_PAGER
,
708 _NET_WM_STATE_SKIP_TASKBAR
,
710 _NET_WM_WINDOW_TYPE_DIALOG
,
711 _NET_WM_WINDOW_TYPE_DOCK
,
712 _NET_WM_WINDOW_TYPE_NORMAL
,
713 _NET_WM_WINDOW_TYPE_SPLASH
,
714 _NET_WM_WINDOW_TYPE_TOOLBAR
,
715 _NET_WM_WINDOW_TYPE_UTILITY
,
716 _SWM_WM_STATE_MANUAL
,
723 } ewmh
[SWM_EWMH_HINT_MAX
] = {
724 /* must be in same order as in the enum */
725 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
726 {"_NET_CLIENT_LIST", XCB_ATOM_NONE
},
727 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
728 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE
},
729 {"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE
},
730 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE
},
731 {"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE
},
732 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
733 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE
},
734 {"_NET_REQUEST_FRAME_EXTENTS", XCB_ATOM_NONE
},
735 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE
},
736 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
737 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
738 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
739 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
740 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
741 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
742 {"_NET_WM_DESKTOP", XCB_ATOM_NONE
},
743 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE
},
744 {"_NET_WM_NAME", XCB_ATOM_NONE
},
745 {"_NET_WM_STATE", XCB_ATOM_NONE
},
746 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
747 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
748 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
749 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
750 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
751 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
752 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
753 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
754 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
755 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
756 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
757 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
758 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
759 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
760 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
763 /* EWMH source type */
765 EWMH_SOURCE_TYPE_NONE
= 0,
766 EWMH_SOURCE_TYPE_NORMAL
= 1,
767 EWMH_SOURCE_TYPE_OTHER
= 2,
774 XC_BOTTOM_LEFT_CORNER
,
775 XC_BOTTOM_RIGHT_CORNER
,
783 char *name
; /* Name used by Xcursor .*/
784 uint8_t cf_char
; /* cursorfont index. */
786 } cursors
[XC_MAX
] = {
787 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
788 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
789 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
790 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
791 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
792 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
793 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
796 #define SWM_SPAWN_OPTIONAL 0x1
800 TAILQ_ENTRY(spawn_prog
) entry
;
806 TAILQ_HEAD(spawn_list
, spawn_prog
);
807 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
813 /* User callable function IDs. */
936 /* SWM_DEBUG actions MUST be here: */
949 BINDING_F_REPLAY
= 0x1,
953 RB_ENTRY(binding
) entry
;
954 uint16_t mod
; /* Modifier Mask. */
955 enum binding_type type
; /* Key or Button. */
956 uint32_t value
; /* KeySym or Button Index. */
957 enum actionid action
; /* Action Identifier. */
961 RB_HEAD(binding_tree
, binding
);
963 /* function prototypes */
964 void adjust_font(struct ws_win
*);
965 char *argsep(char **);
966 void bar_cleanup(struct swm_region
*);
967 void bar_extra_setup(void);
968 void bar_extra_stop(void);
969 int bar_extra_update(void);
970 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
971 void bar_fmt_expand(char *, size_t);
972 void bar_draw(struct swm_bar
*);
973 void bar_print(struct swm_region
*, const char *);
974 void bar_print_legacy(struct swm_region
*, const char *);
975 void bar_replace(char *, char *, struct swm_region
*, size_t);
976 void bar_replace_pad(char *, int *, size_t);
977 char *bar_replace_seq(char *, char *, struct swm_region
*, size_t *, size_t);
978 void bar_setup(struct swm_region
*);
979 void bar_toggle(struct binding
*, struct swm_region
*, union arg
*);
980 void bar_urgent(char *, size_t);
981 void bar_window_class(char *, size_t, struct swm_region
*);
982 void bar_window_class_instance(char *, size_t, struct swm_region
*);
983 void bar_window_float(char *, size_t, struct swm_region
*);
984 void bar_window_instance(char *, size_t, struct swm_region
*);
985 void bar_window_name(char *, size_t, struct swm_region
*);
986 void bar_window_state(char *, size_t, struct swm_region
*);
987 void bar_workspace_name(char *, size_t, struct swm_region
*);
988 int binding_cmp(struct binding
*, struct binding
*);
989 void binding_insert(uint16_t, enum binding_type
, uint32_t, enum actionid
,
990 uint32_t, const char *);
991 struct binding
*binding_lookup(uint16_t, enum binding_type
, uint32_t);
992 void binding_remove(struct binding
*);
993 void buttonpress(xcb_button_press_event_t
*);
994 void buttonrelease(xcb_button_release_event_t
*);
995 void center_pointer(struct swm_region
*);
996 void check_conn(void);
997 void clear_bindings(void);
998 void clear_keybindings(void);
999 int clear_maximized(struct workspace
*);
1000 void clear_quirks(void);
1001 void clear_spawns(void);
1002 void clientmessage(xcb_client_message_event_t
*);
1003 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
1004 int conf_load(const char *, int);
1005 void configurenotify(xcb_configure_notify_event_t
*);
1006 void configurerequest(xcb_configure_request_event_t
*);
1007 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
1008 void constrain_window(struct ws_win
*, struct swm_geometry
*, int *);
1009 int count_win(struct workspace
*, bool);
1010 void cursors_cleanup(void);
1011 void cursors_load(void);
1012 void custom_region(const char *);
1013 void cycle_layout(struct binding
*, struct swm_region
*, union arg
*);
1014 void cyclerg(struct binding
*, struct swm_region
*, union arg
*);
1015 void cyclews(struct binding
*, struct swm_region
*, union arg
*);
1017 void debug_refresh(struct ws_win
*);
1019 void debug_toggle(struct binding
*, struct swm_region
*, union arg
*);
1020 void destroynotify(xcb_destroy_notify_event_t
*);
1021 void dumpwins(struct binding
*, struct swm_region
*, union arg
*);
1022 int enable_wm(void);
1023 void enternotify(xcb_enter_notify_event_t
*);
1024 void event_drain(uint8_t);
1025 void event_error(xcb_generic_error_t
*);
1026 void event_handle(xcb_generic_event_t
*);
1027 void ewmh_apply_flags(struct ws_win
*, uint32_t);
1028 void ewmh_autoquirk(struct ws_win
*);
1029 void ewmh_get_desktop_names(void);
1030 void ewmh_get_wm_state(struct ws_win
*);
1031 void ewmh_update_actions(struct ws_win
*);
1032 void ewmh_update_client_list(void);
1033 void ewmh_update_current_desktop(void);
1034 void ewmh_update_desktop_names(void);
1035 void ewmh_update_desktops(void);
1036 void ewmh_change_wm_state(struct ws_win
*, xcb_atom_t
, long);
1037 void ewmh_update_wm_state(struct ws_win
*);
1038 char *expand_tilde(const char *);
1039 void expose(xcb_expose_event_t
*);
1040 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
1041 struct swm_bar
*find_bar(xcb_window_t
);
1042 struct ws_win
*find_frame_window(xcb_window_t
);
1043 struct pid_e
*find_pid(pid_t
);
1044 struct swm_region
*find_region(xcb_window_t
);
1045 struct ws_win
*find_unmanaged_window(xcb_window_t
);
1046 struct ws_win
*find_window(xcb_window_t
);
1047 void floating_toggle(struct binding
*, struct swm_region
*, union arg
*);
1048 void focus(struct binding
*, struct swm_region
*, union arg
*);
1049 void focus_flush(void);
1050 void focus_pointer(struct binding
*, struct swm_region
*, union arg
*);
1051 void focus_region(struct swm_region
*);
1052 void focus_win(struct ws_win
*);
1053 void focusin(xcb_focus_in_event_t
*);
1055 void focusout(xcb_focus_out_event_t
*);
1057 void focusrg(struct binding
*, struct swm_region
*, union arg
*);
1058 void fontset_init(void);
1059 void free_window(struct ws_win
*);
1060 xcb_atom_t
get_atom_from_string(const char *);
1062 char *get_atom_name(xcb_atom_t
);
1064 struct ws_win
*get_focus_magic(struct ws_win
*);
1065 struct ws_win
*get_focus_prev(struct ws_win
*);
1066 xcb_generic_event_t
*get_next_event(bool);
1068 char *get_notify_detail_label(uint8_t);
1069 char *get_notify_mode_label(uint8_t);
1071 struct ws_win
*get_pointer_win(xcb_window_t
);
1072 struct ws_win
*get_region_focus(struct swm_region
*);
1073 int get_region_index(struct swm_region
*);
1074 xcb_screen_t
*get_screen(int);
1075 int get_screen_count(void);
1077 char *get_source_type_label(uint32_t);
1078 char *get_stack_mode_name(uint8_t);
1079 char *get_state_mask_label(uint16_t);
1081 int32_t get_swm_ws(xcb_window_t
);
1082 bool get_urgent(struct ws_win
*);
1084 char *get_win_input_model(struct ws_win
*);
1086 char *get_win_name(xcb_window_t
);
1087 uint8_t get_win_state(xcb_window_t
);
1088 void get_wm_protocols(struct ws_win
*);
1089 int get_ws_idx(struct ws_win
*);
1090 void grab_windows(void);
1091 void grabbuttons(void);
1092 void grabkeys(void);
1093 void iconify(struct binding
*, struct swm_region
*, union arg
*);
1094 bool isxlfd(char *);
1095 bool keybindreleased(struct binding
*, xcb_key_release_event_t
*);
1096 void keypress(xcb_key_press_event_t
*);
1097 void keyrelease(xcb_key_release_event_t
*);
1098 bool keyrepeating(xcb_key_release_event_t
*);
1099 void kill_bar_extra_atexit(void);
1100 void kill_refs(struct ws_win
*);
1102 void leavenotify(xcb_leave_notify_event_t
*);
1104 void load_float_geom(struct ws_win
*);
1105 void lower_window(struct ws_win
*);
1106 struct ws_win
*manage_window(xcb_window_t
, int, bool);
1107 void map_window(struct ws_win
*);
1108 void mapnotify(xcb_map_notify_event_t
*);
1109 void mappingnotify(xcb_mapping_notify_event_t
*);
1110 void maprequest(xcb_map_request_event_t
*);
1111 void maximize_toggle(struct binding
*, struct swm_region
*, union arg
*);
1112 void motionnotify(xcb_motion_notify_event_t
*);
1113 void move(struct binding
*, struct swm_region
*, union arg
*);
1114 void move_win(struct ws_win
*, struct binding
*, int);
1115 uint32_t name_to_pixel(int, const char *);
1116 void name_workspace(struct binding
*, struct swm_region
*, union arg
*);
1117 void new_region(struct swm_screen
*, int, int, int, int);
1118 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1119 int parsebinding(const char *, uint16_t *, enum binding_type
*, uint32_t *,
1121 int parsequirks(const char *, uint32_t *, int *);
1122 void pressbutton(struct binding
*, struct swm_region
*, union arg
*);
1123 void priorws(struct binding
*, struct swm_region
*, union arg
*);
1125 void print_win_geom(xcb_window_t
);
1127 void propertynotify(xcb_property_notify_event_t
*);
1128 void put_back_event(xcb_generic_event_t
*);
1129 void quirk_free(struct quirk
*);
1130 void quirk_insert(const char *, const char *, const char *, uint32_t, int);
1131 void quirk_remove(struct quirk
*);
1132 void quirk_replace(struct quirk
*, const char *, const char *, const char *,
1134 void quit(struct binding
*, struct swm_region
*, union arg
*);
1135 void raise_toggle(struct binding
*, struct swm_region
*, union arg
*);
1136 void raise_window(struct ws_win
*);
1137 void region_containment(struct ws_win
*, struct swm_region
*, int);
1138 struct swm_region
*region_under(struct swm_screen
*, int, int);
1139 void regionize(struct ws_win
*, int, int);
1140 void reparent_window(struct ws_win
*);
1141 void reparentnotify(xcb_reparent_notify_event_t
*);
1142 void resize(struct binding
*, struct swm_region
*, union arg
*);
1143 void resize_win(struct ws_win
*, struct binding
*, int);
1144 void restart(struct binding
*, struct swm_region
*, union arg
*);
1145 struct swm_region
*root_to_region(xcb_window_t
, int);
1146 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1147 void scan_randr(int);
1148 void search_do_resp(void);
1149 void search_resp_name_workspace(const char *, size_t);
1150 void search_resp_search_window(const char *);
1151 void search_resp_search_workspace(const char *);
1152 void search_resp_uniconify(const char *, size_t);
1153 void search_win(struct binding
*, struct swm_region
*, union arg
*);
1154 void search_win_cleanup(void);
1155 void search_workspace(struct binding
*, struct swm_region
*, union arg
*);
1156 void send_to_rg(struct binding
*, struct swm_region
*, union arg
*);
1157 void send_to_ws(struct binding
*, struct swm_region
*, union arg
*);
1158 void set_region(struct swm_region
*);
1159 int setautorun(const char *, const char *, int);
1160 void setbinding(uint16_t, enum binding_type
, uint32_t, enum actionid
,
1161 uint32_t, const char *);
1162 int setconfbinding(const char *, const char *, int);
1163 int setconfcolor(const char *, const char *, int);
1164 int setconfmodkey(const char *, const char *, int);
1165 int setconfquirk(const char *, const char *, int);
1166 int setconfregion(const char *, const char *, int);
1167 int setconfspawn(const char *, const char *, int);
1168 int setconfvalue(const char *, const char *, int);
1169 int setkeymapping(const char *, const char *, int);
1170 int setlayout(const char *, const char *, int);
1171 void setquirk(const char *, const char *, const char *, uint32_t, int);
1172 void setscreencolor(const char *, int, int);
1173 void setspawn(const char *, const char *, int);
1174 void setup_btnbindings(void);
1175 void setup_ewmh(void);
1176 void setup_globals(void);
1177 void setup_keybindings(void);
1178 void setup_quirks(void);
1179 void setup_screens(void);
1180 void setup_spawn(void);
1181 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1182 void set_win_state(struct ws_win
*, uint8_t);
1183 void shutdown_cleanup(void);
1185 void socket_setnonblock(int);
1186 void sort_windows(struct ws_win_list
*);
1187 void spawn(int, union arg
*, bool);
1188 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1189 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1190 void spawn_insert(const char *, const char *, int);
1191 struct spawn_prog
*spawn_find(const char *);
1192 void spawn_remove(struct spawn_prog
*);
1193 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1194 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1195 void stack_config(struct binding
*, struct swm_region
*, union arg
*);
1196 void stack_master(struct workspace
*, struct swm_geometry
*, int, bool);
1197 void store_float_geom(struct ws_win
*);
1198 char *strdupsafe(const char *);
1199 void swapwin(struct binding
*, struct swm_region
*, union arg
*);
1200 void switchws(struct binding
*, struct swm_region
*, union arg
*);
1201 void teardown_ewmh(void);
1202 void unescape_selector(char *);
1203 void unfocus_win(struct ws_win
*);
1204 void uniconify(struct binding
*, struct swm_region
*, union arg
*);
1205 void unmanage_window(struct ws_win
*);
1206 void unmap_all(void);
1207 void unmap_window(struct ws_win
*);
1208 void unmapnotify(xcb_unmap_notify_event_t
*);
1209 void unparent_window(struct ws_win
*);
1210 void update_floater(struct ws_win
*);
1211 void update_modkey(uint16_t);
1212 void update_win_stacking(struct ws_win
*);
1213 void update_window(struct ws_win
*);
1214 void draw_frame(struct ws_win
*);
1215 void update_wm_state(struct ws_win
*win
);
1216 void updatenumlockmask(void);
1217 void validate_spawns(void);
1218 int validate_win(struct ws_win
*);
1219 int validate_ws(struct workspace
*);
1220 void version(struct binding
*, struct swm_region
*, union arg
*);
1221 void win_to_ws(struct ws_win
*, int, bool);
1222 pid_t
window_get_pid(xcb_window_t
);
1223 void wkill(struct binding
*, struct swm_region
*, union arg
*);
1224 void update_ws_stack(struct workspace
*);
1225 void xft_init(struct swm_region
*);
1226 void _add_startup_exception(const char *, va_list);
1227 void add_startup_exception(const char *, ...);
1229 RB_PROTOTYPE(binding_tree
, binding
, entry
, binding_cmp
);
1230 RB_GENERATE(binding_tree
, binding
, entry
, binding_cmp
);
1231 struct binding_tree bindings
;
1236 xcb_font_t cf
= XCB_NONE
;
1239 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1240 /* try to load Xcursor first. */
1241 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1244 /* fallback to cursorfont. */
1245 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1246 if (cf
== XCB_NONE
) {
1247 cf
= xcb_generate_id(conn
);
1248 xcb_open_font(conn
, cf
, strlen("cursor"),
1252 cursors
[i
].cid
= xcb_generate_id(conn
);
1253 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1254 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1255 0xffff, 0xffff, 0xffff);
1261 xcb_close_font(conn
, cf
);
1265 cursors_cleanup(void)
1268 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1269 xcb_free_cursor(conn
, cursors
[i
].cid
);
1273 expand_tilde(const char *s
)
1275 struct passwd
*ppwd
;
1283 errx(1, "expand_tilde: NULL string.");
1286 result
= strdup(sc
);
1292 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1293 errx(1, "expand_tilde: sysconf");
1295 if ((user
= calloc(1, max
+ 1)) == NULL
)
1296 errx(1, "expand_tilde: calloc");
1298 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1303 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1307 result
= strdup(sc
);
1309 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1313 errx(1, "expand_tilde: failed to allocate memory.");
1319 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1321 unsigned int tmpr
, tmpg
, tmpb
;
1323 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1326 *rr
= RGB_8_TO_16(tmpr
);
1327 *gg
= RGB_8_TO_16(tmpg
);
1328 *bb
= RGB_8_TO_16(tmpb
);
1334 get_screen(int screen
)
1336 const xcb_setup_t
*r
;
1337 xcb_screen_iterator_t iter
;
1339 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1340 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1344 iter
= xcb_setup_roots_iterator(r
);
1345 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1353 get_screen_count(void)
1355 const xcb_setup_t
*r
;
1357 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1358 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1362 return xcb_setup_roots_length(r
);
1366 get_region_index(struct swm_region
*r
)
1368 struct swm_region
*rr
;
1374 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1389 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1390 event_drain(XCB_ENTER_NOTIFY
);
1396 get_atom_from_string(const char *str
)
1398 xcb_intern_atom_cookie_t c
;
1399 xcb_intern_atom_reply_t
*r
;
1402 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1403 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1411 return (XCB_ATOM_NONE
);
1415 get_wm_protocols(struct ws_win
*win
) {
1417 xcb_icccm_get_wm_protocols_reply_t wpr
;
1419 if (xcb_icccm_get_wm_protocols_reply(conn
,
1420 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
1422 for (i
= 0; i
< (int)wpr
.atoms_len
; i
++) {
1423 if (wpr
.atoms
[i
] == a_takefocus
)
1424 win
->take_focus
= true;
1425 if (wpr
.atoms
[i
] == a_delete
)
1426 win
->can_delete
= true;
1428 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
1435 xcb_window_t root
, win
;
1436 int i
, j
, num_screens
;
1438 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1439 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1441 num_screens
= get_screen_count();
1442 for (i
= 0; i
< num_screens
; i
++) {
1443 root
= screens
[i
].root
;
1445 /* Set up _NET_SUPPORTING_WM_CHECK. */
1446 win
= xcb_generate_id(conn
);
1447 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
1448 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1449 XCB_COPY_FROM_PARENT
, 0, NULL
);
1451 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
1452 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1453 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1454 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1456 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1457 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1458 8, strlen("spectrwm"), "spectrwm");
1460 /* Report supported atoms */
1461 xcb_delete_property(conn
, root
, a_net_supported
);
1462 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1463 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, root
,
1464 a_net_supported
, XCB_ATOM_ATOM
, 32, 1,
1469 ewmh_update_desktops();
1470 ewmh_get_desktop_names();
1478 xcb_get_property_cookie_t pc
;
1479 xcb_get_property_reply_t
*pr
;
1481 num_screens
= get_screen_count();
1483 for (i
= 0; i
< num_screens
; i
++) {
1484 /* Get the support check window and destroy it */
1485 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, a_net_wm_check
,
1486 XCB_ATOM_WINDOW
, 0, 1);
1487 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1490 if (pr
->format
== a_net_wm_check
) {
1491 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1493 xcb_destroy_window(conn
, id
);
1494 xcb_delete_property(conn
, screens
[i
].root
,
1496 xcb_delete_property(conn
, screens
[i
].root
,
1504 ewmh_autoquirk(struct ws_win
*win
)
1506 xcb_get_property_reply_t
*r
;
1507 xcb_get_property_cookie_t c
;
1511 c
= xcb_get_property(conn
, 0, win
->id
,
1512 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1513 r
= xcb_get_property_reply(conn
, c
, NULL
);
1517 type
= xcb_get_property_value(r
);
1518 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1520 for (i
= 0; i
< n
; i
++) {
1521 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1523 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1524 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1525 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1526 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1529 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1530 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1531 win
->quirks
= SWM_Q_FLOAT
;
1539 ewmh_update_actions(struct ws_win
*win
)
1541 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1547 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1550 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1551 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1552 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1555 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1556 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1559 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1560 #define _NET_WM_STATE_ADD 1 /* add/set property */
1561 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1564 ewmh_change_wm_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1571 name
= get_atom_name(state
);
1572 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: win %#x, state: %s, "
1573 "action: %ld\n", WINID(win
), name
, action
);
1579 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1580 flag
= EWMH_F_FULLSCREEN
;
1581 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1582 flag
= EWMH_F_ABOVE
;
1583 else if (state
== ewmh
[_NET_WM_STATE_HIDDEN
].atom
)
1584 flag
= EWMH_F_HIDDEN
;
1585 else if (state
== ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
||
1586 state
== ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
)
1587 flag
= EWMH_F_MAXIMIZED
;
1588 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1589 flag
= SWM_F_MANUAL
;
1590 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1591 flag
= EWMH_F_SKIP_PAGER
;
1592 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1593 flag
= EWMH_F_SKIP_TASKBAR
;
1595 /* Disallow unfloating transients. */
1596 if (TRANS(win
) && flag
== EWMH_F_ABOVE
)
1599 new_flags
= win
->ewmh_flags
;
1602 case _NET_WM_STATE_REMOVE
:
1605 case _NET_WM_STATE_ADD
:
1608 case _NET_WM_STATE_TOGGLE
:
1613 ewmh_apply_flags(win
, new_flags
);
1616 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: done.\n");
1620 ewmh_apply_flags(struct ws_win
*win
, uint32_t pending
)
1622 struct workspace
*ws
;
1625 changed
= win
->ewmh_flags
^ pending
;
1629 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: pending: %d\n", pending
);
1631 win
->ewmh_flags
= pending
;
1634 if (changed
& EWMH_F_HIDDEN
) {
1636 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1637 ws
->focus_pending
= get_focus_prev(win
);
1642 /* Reload floating geometry in case region changed. */
1644 load_float_geom(win
);
1646 /* The window is no longer iconic, prepare focus. */
1647 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1648 ws
->focus_pending
= get_focus_magic(win
);
1653 if (changed
& EWMH_F_ABOVE
) {
1654 if (ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1656 load_float_geom(win
);
1657 else if (!MAXIMIZED(win
))
1658 store_float_geom(win
);
1660 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1661 changed
&= ~EWMH_F_MAXIMIZED
;
1665 win
->ewmh_flags
^= EWMH_F_ABOVE
& pending
;
1669 if (changed
& EWMH_F_MAXIMIZED
) {
1670 /* VERT and/or HORZ changed. */
1672 if (!MAXIMIZED(win
))
1673 load_float_geom(win
);
1675 store_float_geom(win
);
1678 if (MAXIMIZED(win
)) {
1679 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
1680 ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1684 ws
->focus_pending
= win
;
1692 if (changed
& EWMH_F_FULLSCREEN
) {
1693 if (FULLSCREEN(win
)) {
1694 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
1698 ws
->focus_pending
= win
;
1701 load_float_geom(win
);
1704 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1708 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: done.\n");
1712 ewmh_update_wm_state(struct ws_win
*win
) {
1713 xcb_atom_t vals
[SWM_EWMH_ACTION_COUNT_MAX
];
1717 vals
[n
++] = ewmh
[_NET_WM_STATE_HIDDEN
].atom
;
1718 if (FULLSCREEN(win
))
1719 vals
[n
++] = ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
;
1720 if (MAXIMIZED_VERT(win
))
1721 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
;
1722 if (MAXIMIZED_HORZ(win
))
1723 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
;
1724 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1725 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
;
1726 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1727 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
;
1728 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1729 vals
[n
++] = ewmh
[_NET_WM_STATE_ABOVE
].atom
;
1730 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1731 vals
[n
++] = ewmh
[_SWM_WM_STATE_MANUAL
].atom
;
1734 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1735 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, n
, vals
);
1737 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1741 ewmh_get_wm_state(struct ws_win
*win
)
1744 xcb_get_property_cookie_t c
;
1745 xcb_get_property_reply_t
*r
;
1751 win
->ewmh_flags
= 0;
1753 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1754 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1755 r
= xcb_get_property_reply(conn
, c
, NULL
);
1759 states
= xcb_get_property_value(r
);
1760 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1762 for (i
= 0; i
< n
; i
++)
1763 ewmh_change_wm_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1771 dumpwins(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
1775 xcb_get_window_attributes_cookie_t c
;
1776 xcb_get_window_attributes_reply_t
*wa
;
1778 /* suppress unused warning since var is needed */
1782 if (r
->ws
== NULL
) {
1783 DPRINTF("dumpwins: invalid workspace\n");
1787 DPRINTF("=== managed window list ws %02d ===\n", r
->ws
->idx
);
1788 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
) {
1789 state
= get_win_state(w
->id
);
1790 c
= xcb_get_window_attributes(conn
, w
->id
);
1791 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1793 DPRINTF("win %#x (%#x), map_state: %d, state: %u, "
1794 "transient: %#x\n", w
->frame
, w
->id
, wa
->map_state
,
1795 state
, w
->transient
);
1798 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1802 DPRINTF("=== stacking order (top down) === \n");
1803 TAILQ_FOREACH(w
, &r
->ws
->stack
, stack_entry
) {
1804 DPRINTF("win %#x (%#x), fs: %s, maximized: %s, above: %s, "
1805 "iconic: %s\n", w
->frame
, w
->id
, YESNO(FULLSCREEN(w
)),
1806 YESNO(MAXIMIZED(w
)), YESNO(ABOVE(w
)), YESNO(ICONIC(w
)));
1809 DPRINTF("===== unmanaged window list =====\n");
1810 TAILQ_FOREACH(w
, &r
->ws
->unmanagedlist
, entry
) {
1811 state
= get_win_state(w
->id
);
1812 c
= xcb_get_window_attributes(conn
, w
->id
);
1813 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1815 DPRINTF("win %#x, map_state: %d, state: %u, "
1816 "transient: %#x\n", w
->id
, wa
->map_state
,
1817 state
, w
->transient
);
1820 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1824 DPRINTF("=================================\n");
1828 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
1831 int num_screens
, i
, j
;
1833 /* Suppress warnings. */
1838 DNPRINTF(SWM_D_MISC
, "debug_toggle\n");
1840 debug_enabled
= !debug_enabled
;
1842 num_screens
= get_screen_count();
1843 for (i
= 0; i
< num_screens
; i
++)
1844 for (j
= 0; j
< workspace_limit
; j
++)
1845 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1852 debug_refresh(struct ws_win
*win
)
1859 XRectangle l_ibox
, l_lbox
;
1860 xcb_rectangle_t rect
;
1862 uint32_t wc
[4], mask
, width
, height
, gcv
[1];
1865 xcb_screen_t
*screen
;
1867 if (debug_enabled
) {
1868 /* Create debug window if it doesn't exist. */
1869 if (win
->debug
== XCB_WINDOW_NONE
) {
1870 if ((screen
= get_screen(win
->s
->idx
)) == NULL
)
1871 errx(1, "ERROR: can't get screen %d.",
1874 win
->debug
= xcb_generate_id(conn
);
1875 wc
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1876 wc
[1] = win
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
1877 wc
[2] = screen
->default_colormap
;
1879 xcb_create_window(conn
, screen
->root_depth
, win
->debug
,
1880 win
->frame
, 0, 0, 10, 10, 1,
1881 XCB_WINDOW_CLASS_INPUT_OUTPUT
, screen
->root_visual
,
1882 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
|
1883 XCB_CW_COLORMAP
, wc
);
1885 xcb_map_window(conn
, win
->debug
);
1888 /* Determine workspace window list index. */
1890 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
1896 /* Determine stacking index (top down). */
1898 TAILQ_FOREACH(w
, &win
->ws
->stack
, stack_entry
) {
1904 if (asprintf(&s
, "%#x f:%#x wl:%d s:%d im:%s", win
->id
,
1905 win
->frame
, widx
, sidx
, get_win_input_model(win
)) == -1)
1910 /* Update window to an appropriate dimension. */
1911 if (bar_font_legacy
) {
1912 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
1913 width
= l_lbox
.width
+ 4;
1914 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
1916 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
1918 width
= info
.width
+ 4;
1919 height
= bar_font
->height
+ 4;
1922 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
1923 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
;
1925 wc
[0] = wc
[1] = border_width
;
1932 xcb_configure_window(conn
, win
->debug
, mask
, wc
);
1934 /* Draw a filled rectangle to 'clear' window. */
1938 rect
.height
= height
;
1940 gcv
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1941 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1942 xcb_poly_fill_rectangle(conn
, win
->debug
, win
->s
->bar_gc
, 1,
1946 if (bar_font_legacy
) {
1947 l_gcv
.graphics_exposures
= 0;
1948 l_draw
= XCreateGC(display
, win
->debug
, 0, &l_gcv
);
1950 XSetForeground(display
, l_draw
,
1951 win
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
1953 DRAWSTRING(display
, win
->debug
, bar_fs
, l_draw
, 2,
1954 (bar_fs_extents
->max_logical_extent
.height
-
1955 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
1957 XFreeGC(display
, l_draw
);
1959 draw
= XftDrawCreate(display
, win
->debug
,
1960 DefaultVisual(display
, win
->s
->idx
),
1961 DefaultColormap(display
, win
->s
->idx
));
1963 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
1964 (bar_height
+ bar_font
->height
) / 2 -
1965 bar_font
->descent
, (FcChar8
*)s
, len
);
1967 XftDrawDestroy(draw
);
1971 } else if (win
->debug
!= XCB_WINDOW_NONE
) {
1972 xcb_destroy_window(conn
, win
->debug
);
1973 win
->debug
= XCB_WINDOW_NONE
;
1978 dumpwins(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
1986 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
1992 #endif /* SWM_DEBUG */
1997 int saved_errno
, status
;
2000 saved_errno
= errno
;
2004 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
2009 if (errno
!= ECHILD
)
2010 warn("sighdlr: waitpid");
2011 #endif /* SWM_DEBUG */
2014 if (pid
== searchpid
)
2018 if (WIFEXITED(status
)) {
2019 if (WEXITSTATUS(status
) != 0)
2020 warnx("sighdlr: child exit status: %d",
2021 WEXITSTATUS(status
));
2023 warnx("sighdlr: child is terminated "
2025 #endif /* SWM_DEBUG */
2039 errno
= saved_errno
;
2045 struct pid_e
*p
= NULL
;
2047 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
2052 TAILQ_FOREACH(p
, &pidlist
, entry
) {
2061 name_to_pixel(int sidx
, const char *colorname
)
2063 uint32_t result
= 0;
2064 char cname
[32] = "#";
2065 xcb_screen_t
*screen
;
2066 xcb_colormap_t cmap
;
2067 xcb_alloc_color_reply_t
*cr
;
2068 xcb_alloc_named_color_reply_t
*nr
;
2069 uint16_t rr
, gg
, bb
;
2071 screen
= get_screen(sidx
);
2072 cmap
= screen
->default_colormap
;
2074 /* color is in format rgb://rr/gg/bb */
2075 if (strncmp(colorname
, "rgb:", 4) == 0) {
2076 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
2077 warnx("could not parse rgb %s", colorname
);
2079 cr
= xcb_alloc_color_reply(conn
,
2080 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
2086 warnx("color '%s' not found", colorname
);
2089 nr
= xcb_alloc_named_color_reply(conn
,
2090 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
2093 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
2094 nr
= xcb_alloc_named_color_reply(conn
,
2095 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
2102 warnx("color '%s' not found", colorname
);
2109 setscreencolor(const char *val
, int i
, int c
)
2111 if (i
< 0 || i
>= get_screen_count())
2114 screens
[i
].c
[c
].pixel
= name_to_pixel(i
, val
);
2115 free(screens
[i
].c
[c
].name
);
2116 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
2121 fancy_stacker(struct workspace
*ws
)
2123 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2124 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2125 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2126 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
2127 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
2128 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2129 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2130 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
2131 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
2135 plain_stacker(struct workspace
*ws
)
2137 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2138 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2139 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
2140 sizeof ws
->stacker
);
2141 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2142 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
2143 sizeof ws
->stacker
);
2147 custom_region(const char *val
)
2149 unsigned int x
, y
, w
, h
;
2150 int sidx
, num_screens
;
2151 xcb_screen_t
*screen
;
2153 DNPRINTF(SWM_D_CONF
, "custom_region: %s\n", val
);
2155 num_screens
= get_screen_count();
2156 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
2157 errx(1, "invalid custom region, "
2158 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
2159 if (sidx
< 1 || sidx
> num_screens
)
2160 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
2164 if ((screen
= get_screen(sidx
)) == NULL
)
2165 errx(1, "ERROR: can't get screen %d.", sidx
);
2168 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
2170 if (x
> screen
->width_in_pixels
||
2171 y
> screen
->height_in_pixels
||
2172 w
+ x
> screen
->width_in_pixels
||
2173 h
+ y
> screen
->height_in_pixels
) {
2174 warnx("ignoring region %ux%u+%u+%u - not within screen "
2175 "boundaries (%ux%u)", w
, h
, x
, y
,
2176 screen
->width_in_pixels
, screen
->height_in_pixels
);
2180 new_region(&screens
[sidx
], x
, y
, w
, h
);
2184 socket_setnonblock(int fd
)
2188 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
2189 err(1, "fcntl F_GETFL");
2190 flags
|= O_NONBLOCK
;
2191 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
2192 err(1, "fcntl F_SETFL");
2196 bar_print_legacy(struct swm_region
*r
, const char *s
)
2198 xcb_rectangle_t rect
;
2203 XRectangle ibox
, lbox
;
2207 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
2209 switch (bar_justify
) {
2210 case SWM_BAR_JUSTIFY_LEFT
:
2213 case SWM_BAR_JUSTIFY_CENTER
:
2214 x
= (WIDTH(r
) - lbox
.width
) / 2;
2216 case SWM_BAR_JUSTIFY_RIGHT
:
2217 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
2221 if (x
< SWM_BAR_OFFSET
)
2226 rect
.width
= WIDTH(r
->bar
);
2227 rect
.height
= HEIGHT(r
->bar
);
2229 /* clear back buffer */
2230 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2231 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2232 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2234 /* draw back buffer */
2235 gcvd
.graphics_exposures
= 0;
2236 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
2237 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2238 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
2239 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
2241 XFreeGC(display
, draw
);
2244 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2245 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2249 bar_print(struct swm_region
*r
, const char *s
)
2252 xcb_rectangle_t rect
;
2260 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
2262 switch (bar_justify
) {
2263 case SWM_BAR_JUSTIFY_LEFT
:
2266 case SWM_BAR_JUSTIFY_CENTER
:
2267 x
= (WIDTH(r
) - info
.width
) / 2;
2269 case SWM_BAR_JUSTIFY_RIGHT
:
2270 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
2274 if (x
< SWM_BAR_OFFSET
)
2279 rect
.width
= WIDTH(r
->bar
);
2280 rect
.height
= HEIGHT(r
->bar
);
2282 /* clear back buffer */
2283 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2284 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2285 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2287 /* draw back buffer */
2288 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
2289 DefaultVisual(display
, r
->s
->idx
),
2290 DefaultColormap(display
, r
->s
->idx
));
2292 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
2293 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
2296 XftDrawDestroy(draw
);
2299 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2300 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2304 bar_extra_stop(void)
2308 bzero(bar_pipe
, sizeof bar_pipe
);
2311 kill(bar_pid
, SIGTERM
);
2314 strlcpy(bar_ext
, "", sizeof bar_ext
);
2319 bar_window_class(char *s
, size_t sz
, struct swm_region
*r
)
2321 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2323 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
2324 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
2328 bar_window_instance(char *s
, size_t sz
, struct swm_region
*r
)
2330 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2332 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
2333 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
2337 bar_window_class_instance(char *s
, size_t sz
, struct swm_region
*r
)
2339 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2342 bar_window_class(s
, sz
, r
);
2343 strlcat(s
, ":", sz
);
2344 bar_window_instance(s
, sz
, r
);
2348 bar_window_state(char *s
, size_t sz
, struct swm_region
*r
)
2350 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2352 if (MAXIMIZED(r
->ws
->focus
))
2353 strlcat(s
, "(m)", sz
);
2354 else if (ABOVE(r
->ws
->focus
))
2355 strlcat(s
, "(f)", sz
);
2359 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
2363 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2366 title
= get_win_name(r
->ws
->focus
->id
);
2367 strlcat(s
, title
, sz
);
2372 get_urgent(struct ws_win
*win
)
2374 xcb_icccm_wm_hints_t hints
;
2375 xcb_get_property_cookie_t c
;
2376 bool urgent
= false;
2379 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2380 if (xcb_icccm_get_wm_hints_reply(conn
, c
, &hints
, NULL
))
2381 urgent
= xcb_icccm_wm_hints_get_urgency(&hints
);
2388 bar_urgent(char *s
, size_t sz
)
2391 int i
, j
, num_screens
;
2392 bool urgent
[SWM_WS_MAX
];
2395 for (i
= 0; i
< workspace_limit
; i
++)
2398 num_screens
= get_screen_count();
2399 for (i
= 0; i
< num_screens
; i
++)
2400 for (j
= 0; j
< workspace_limit
; j
++)
2401 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2402 if (get_urgent(win
))
2405 for (i
= 0; i
< workspace_limit
; i
++) {
2407 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2409 } else if (!urgent_collapse
) {
2410 strlcat(s
, "- ", sz
);
2413 if(urgent_collapse
&& s
[0])
2414 s
[strlen(s
) - 1] = 0;
2418 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2420 if (r
== NULL
|| r
->ws
== NULL
)
2422 if (r
->ws
->name
!= NULL
)
2423 strlcat(s
, r
->ws
->name
, sz
);
2426 /* build the default bar format according to the defined enabled options */
2428 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2432 /* if format provided, just copy the buffers */
2433 if (bar_format
!= NULL
) {
2434 strlcpy(fmtnew
, fmtexp
, sz
);
2438 /* reset the output buffer */
2441 strlcat(fmtnew
, "+N:+I ", sz
);
2443 strlcat(fmtnew
, "+S", sz
);
2444 strlcat(fmtnew
, " ", sz
);
2446 /* only show the workspace name if there's actually one */
2447 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2448 strlcat(fmtnew
, "<+D>", sz
);
2450 /* If enabled, only show the iconic count if there are iconic wins. */
2451 if (iconic_enabled
&& r
!= NULL
&& r
->ws
!= NULL
)
2452 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2454 strlcat(fmtnew
, "{+M}", sz
);
2458 strlcat(fmtnew
, "+3<", sz
);
2460 if (clock_enabled
) {
2461 strlcat(fmtnew
, fmtexp
, sz
);
2462 strlcat(fmtnew
, "+4<", sz
);
2465 /* bar_urgent already adds the space before the last asterisk */
2467 strlcat(fmtnew
, (urgent_collapse
? "*+U*+4<" : "* +U*+4<"), sz
);
2469 if (window_class_enabled
) {
2470 strlcat(fmtnew
, "+C", sz
);
2471 if (!window_instance_enabled
)
2472 strlcat(fmtnew
, "+4<", sz
);
2475 /* checks needed by the colon and floating strlcat(3) calls below */
2476 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2477 if (window_instance_enabled
) {
2478 if (window_class_enabled
)
2479 strlcat(fmtnew
, ":", sz
);
2480 strlcat(fmtnew
, "+T+4<", sz
);
2482 if (window_name_enabled
) {
2483 if (ABOVE(r
->ws
->focus
) || MAXIMIZED(r
->ws
->focus
))
2484 strlcat(fmtnew
, "+F ", sz
);
2485 strlcat(fmtnew
, "+64W ", sz
);
2489 /* finally add the action script output and the version */
2490 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2494 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2496 /* special case; no limit given, pad one space, instead */
2497 if (*limit
== (int)sz
- 1)
2499 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2502 /* replaces the bar format character sequences (like in tmux(1)) */
2504 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2509 char tmp
[SWM_BAR_MAX
];
2510 int limit
, size
, count
;
2513 /* reset strlcat(3) buffer */
2516 /* get number, if any */
2519 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2520 limit
= sizeof tmp
- 1;
2521 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2522 limit
= sizeof tmp
- 1;
2524 /* there is nothing to replace (ie EOL) */
2531 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2534 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2537 bar_window_class(tmp
, sizeof tmp
, r
);
2540 bar_workspace_name(tmp
, sizeof tmp
, r
);
2543 bar_window_state(tmp
, sizeof tmp
, r
);
2546 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2550 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2554 snprintf(tmp
, sizeof tmp
, "%d", count
);
2557 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2560 bar_window_class_instance(tmp
, sizeof tmp
, r
);
2563 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2566 bar_window_instance(tmp
, sizeof tmp
, r
);
2569 bar_urgent(tmp
, sizeof tmp
);
2572 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2575 bar_window_name(tmp
, sizeof tmp
, r
);
2578 /* unknown character sequence; copy as-is */
2579 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2585 if ((int)len
< limit
)
2587 while (limit
-- > 0) {
2588 if (*offrep
>= sz
- 1)
2590 fmtrep
[(*offrep
)++] = *ptr
++;
2598 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2603 while (*fmt
!= '\0') {
2605 /* skip ordinary characters */
2608 fmtrep
[off
++] = *fmt
++;
2612 /* character sequence found; replace it */
2613 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2622 bar_fmt_expand(char *fmtexp
, size_t sz
)
2629 /* start by grabbing the current time and date */
2631 localtime_r(&tmt
, &tm
);
2633 /* figure out what to expand */
2634 if (bar_format
!= NULL
)
2636 else if (bar_format
== NULL
&& clock_enabled
)
2638 /* if nothing to expand bail out */
2644 /* copy as-is, just in case the format shouldn't be expanded below */
2645 strlcpy(fmtexp
, fmt
, sz
);
2646 /* finally pass the string through strftime(3) */
2647 #ifndef SWM_DENY_CLOCK_FORMAT
2648 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2649 warnx("format too long");
2654 /* Redraws a region bar; need to follow with xcb_flush() or focus_flush(). */
2656 bar_draw(struct swm_bar
*bar
)
2658 struct swm_region
*r
;
2659 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2660 char fmtrep
[SWM_BAR_MAX
];
2662 /* expand the format by first passing it through strftime(3) */
2663 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2670 if (bar_enabled
&& r
->ws
->bar_enabled
)
2671 xcb_map_window(conn
, bar
->id
);
2673 xcb_unmap_window(conn
, bar
->id
);
2677 if (startup_exception
)
2678 snprintf(fmtrep
, sizeof fmtrep
, "total "
2679 "exceptions: %d, first exception: %s",
2683 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2684 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2687 if (bar_font_legacy
)
2688 bar_print_legacy(r
, fmtrep
);
2690 bar_print(r
, fmtrep
);
2694 * Reads external script output; call when stdin is readable.
2695 * Returns 1 if bar_ext was updated; otherwise 0.
2698 bar_extra_update(void)
2701 char b
[SWM_BAR_MAX
];
2702 bool changed
= false;
2707 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2710 if (b
[len
- 1] == '\n') {
2711 /* Remove newline. */
2714 /* "Clear" bar_ext. */
2717 /* Flush buffered output. */
2718 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2719 bar_ext_buf
[0] = '\0';
2721 /* Append new output to bar. */
2722 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2726 /* Buffer output. */
2727 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2732 if (errno
!= EAGAIN
) {
2733 warn("bar_action failed");
2742 bar_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
2744 struct swm_region
*tmpr
;
2747 /* suppress unused warnings since vars are needed */
2752 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2755 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2756 /* Only change if master switch is enabled. */
2758 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2760 bar_enabled
= r
->ws
->bar_enabled
= true;
2762 case SWM_ARG_ID_BAR_TOGGLE
:
2763 bar_enabled
= !bar_enabled
;
2767 /* update bars as necessary */
2768 num_screens
= get_screen_count();
2769 for (i
= 0; i
< num_screens
; i
++)
2770 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2772 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2773 xcb_map_window(conn
, tmpr
->bar
->id
);
2775 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2778 /* Restack all regions and redraw bar. */
2779 num_screens
= get_screen_count();
2780 for (i
= 0; i
< num_screens
; i
++)
2781 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
2783 bar_draw(tmpr
->bar
);
2790 bar_extra_setup(void)
2792 /* do this here because the conf file is in memory */
2793 if (!bar_extra
&& bar_argv
[0]) {
2794 /* launch external status app */
2796 if (pipe(bar_pipe
) == -1)
2797 err(1, "pipe error");
2798 socket_setnonblock(bar_pipe
[0]);
2799 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2801 /* Set stdin to read from the pipe. */
2802 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2805 /* Set stdout to write to the pipe. */
2806 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2809 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2810 err(1, "could not disable SIGPIPE");
2811 switch (bar_pid
= fork()) {
2813 err(1, "cannot fork");
2817 execvp(bar_argv
[0], bar_argv
);
2818 err(1, "%s external app failed", bar_argv
[0]);
2820 default: /* parent */
2825 atexit(kill_bar_extra_atexit
);
2830 kill_bar_extra_atexit(void)
2833 kill(bar_pid
, SIGTERM
);
2841 while ((s
= index(s
, '-'))) {
2846 return (count
== 14);
2852 char *default_string
;
2853 char **missing_charsets
;
2854 int num_missing_charsets
= 0;
2858 XFreeFontSet(display
, bar_fs
);
2862 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n",
2865 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2866 &num_missing_charsets
, &default_string
);
2868 if (num_missing_charsets
> 0) {
2869 warnx("Unable to load charset(s):");
2871 for (i
= 0; i
< num_missing_charsets
; ++i
)
2872 warnx("%s", missing_charsets
[i
]);
2874 XFreeStringList(missing_charsets
);
2876 if (strcmp(default_string
, ""))
2877 warnx("Glyphs from those sets will be replaced "
2878 "by '%s'.", default_string
);
2880 warnx("Glyphs from those sets won't be drawn.");
2884 errx(1, "Error creating font set structure.");
2886 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2888 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2889 2 * bar_border_width
;
2896 xft_init(struct swm_region
*r
)
2898 char *font
, *str
, *search
;
2901 if (bar_font
== NULL
) {
2902 if ((search
= str
= strdup(bar_fonts
)) == NULL
)
2903 errx(1, "insufficient memory.");
2905 while ((font
= strsep(&search
, ",")) != NULL
) {
2909 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2912 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2915 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2919 if (bar_font
== NULL
) {
2920 warnx("unable to load font %s", font
);
2923 DNPRINTF(SWM_D_INIT
, "successfully opened "
2931 if (bar_font
== NULL
)
2932 errx(1, "unable to open a font");
2934 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2936 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2937 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2938 warn("Xft error: unable to allocate color.");
2940 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR
].pixel
, color
);
2942 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2943 DefaultColormap(display
, r
->s
->idx
), &color
, &search_font_color
))
2944 warn("Xft error: unable to allocate color.");
2946 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2953 bar_setup(struct swm_region
*r
)
2955 xcb_screen_t
*screen
;
2958 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2961 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2962 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2967 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2968 err(1, "bar_setup: calloc: failed to allocate memory.");
2970 if (bar_font_legacy
)
2977 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2978 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2979 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2981 /* Assume region is unfocused when we create the bar. */
2982 r
->bar
->id
= xcb_generate_id(conn
);
2983 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2984 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
2985 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
2986 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
2988 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2989 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2990 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
2991 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
2992 | XCB_CW_EVENT_MASK
, wa
);
2994 /* Stack bar window above region window to start. */
2996 wa
[1] = XCB_STACK_MODE_ABOVE
;
2998 xcb_configure_window(conn
, r
->bar
->id
, XCB_CONFIG_WINDOW_SIBLING
|
2999 XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
3001 r
->bar
->buffer
= xcb_generate_id(conn
);
3002 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
3003 WIDTH(r
->bar
), HEIGHT(r
->bar
));
3006 xcb_randr_select_input(conn
, r
->bar
->id
,
3007 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
3010 xcb_map_window(conn
, r
->bar
->id
);
3012 DNPRINTF(SWM_D_BAR
, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
3013 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
3020 bar_cleanup(struct swm_region
*r
)
3024 xcb_destroy_window(conn
, r
->bar
->id
);
3025 xcb_free_pixmap(conn
, r
->bar
->buffer
);
3031 set_win_state(struct ws_win
*win
, uint8_t state
)
3033 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
3035 DNPRINTF(SWM_D_EVENT
, "set_win_state: win %#x, state: %u\n",
3041 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
3042 a_state
, 32, 2, data
);
3046 get_win_state(xcb_window_t w
)
3048 xcb_get_property_reply_t
*r
;
3049 xcb_get_property_cookie_t c
;
3050 uint32_t result
= 0;
3052 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
3053 r
= xcb_get_property_reply(conn
, c
, NULL
);
3055 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
3056 result
= *((uint32_t *)xcb_get_property_value(r
));
3060 DNPRINTF(SWM_D_MISC
, "get_win_state property: win %#x state %u\n", w
,
3066 version(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3068 struct swm_region
*tmpr
;
3071 /* suppress unused warnings since vars are needed */
3076 bar_version
= !bar_version
;
3078 snprintf(bar_vertext
, sizeof bar_vertext
,
3079 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
3081 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
3083 num_screens
= get_screen_count();
3084 for (i
= 0; i
< num_screens
; i
++) {
3085 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
3086 bar_draw(tmpr
->bar
);
3093 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
3095 xcb_client_message_event_t ev
;
3103 name
= get_atom_name(a
);
3104 DNPRINTF(SWM_D_EVENT
, "client_msg: win %#x, atom: %s(%u), "
3106 win
->id
, name
, a
, t
);
3110 bzero(&ev
, sizeof ev
);
3111 ev
.response_type
= XCB_CLIENT_MESSAGE
;
3112 ev
.window
= win
->id
;
3115 ev
.data
.data32
[0] = a
;
3116 ev
.data
.data32
[1] = t
;
3118 xcb_send_event(conn
, 0, win
->id
,
3119 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
3122 /* synthetic response to a ConfigureRequest when not making a change */
3124 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
3126 xcb_configure_notify_event_t ce
;
3131 /* send notification of unchanged state. */
3132 bzero(&ce
, sizeof(ce
));
3133 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
3136 ce
.width
= WIDTH(win
);
3137 ce
.height
= HEIGHT(win
);
3138 ce
.border_width
= 0;
3139 ce
.override_redirect
= 0;
3144 ce
.window
= win
->id
;
3145 ce
.above_sibling
= XCB_WINDOW_NONE
;
3148 ce
.event
= ev
->window
;
3149 ce
.window
= ev
->window
;
3151 /* make response appear more WM_SIZE_HINTS-compliant */
3152 if (win
->sh
.flags
) {
3153 DNPRINTF(SWM_D_MISC
, "config_win: hints: win %#x,"
3154 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
3155 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
3156 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
3157 SH_INC_W(win
), SH_INC_H(win
));
3162 /* the hint may be set... to 0! */
3163 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
3164 ce
.width
= SH_MIN_W(win
);
3165 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
3166 ce
.height
= SH_MIN_H(win
);
3171 /* may also be advertized as 0 */
3172 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
3173 ce
.width
= SH_MAX_W(win
);
3174 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
3175 ce
.height
= SH_MAX_H(win
);
3178 /* resize increment. */
3180 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
3181 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
3183 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
3184 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
3188 /* adjust x and y for requested border_width. */
3189 ce
.x
+= ev
->border_width
;
3190 ce
.y
+= ev
->border_width
;
3192 ce
.above_sibling
= ev
->sibling
;
3195 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
3196 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
3197 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
3199 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
3204 count_win(struct workspace
*ws
, bool count_transient
)
3209 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3210 if (!count_transient
&& FLOATING(win
))
3216 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
3222 quit(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3224 /* suppress unused warnings since vars are needed */
3229 DNPRINTF(SWM_D_MISC
, "quit\n");
3234 lower_window(struct ws_win
*win
)
3236 struct ws_win
*target
= NULL
;
3237 struct workspace
*ws
;
3239 DNPRINTF(SWM_D_EVENT
, "lower_window: win %#x\n", WINID(win
));
3246 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3247 if (target
== win
|| ICONIC(target
))
3249 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3252 if (win
->transient
== target
->transient
)
3254 if (win
->transient
== target
->id
)
3257 if (FULLSCREEN(target
))
3259 if (FULLSCREEN(win
))
3261 if (MAXIMIZED(target
))
3265 if (ABOVE(target
) || TRANS(target
))
3267 if (ABOVE(win
) || TRANS(win
))
3271 /* Change stack position. */
3272 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3274 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3276 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3278 update_win_stacking(win
);
3281 if (swm_debug
& SWM_D_STACK
) {
3282 DPRINTF("=== stacking order (top down) === \n");
3283 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3284 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3285 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3286 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3287 YESNO(ICONIC(target
)));
3291 DNPRINTF(SWM_D_EVENT
, "lower_window: done\n");
3295 raise_window(struct ws_win
*win
)
3297 struct ws_win
*target
= NULL
;
3298 struct workspace
*ws
;
3300 DNPRINTF(SWM_D_EVENT
, "raise_window: win %#x\n", WINID(win
));
3307 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3308 if (target
== win
|| ICONIC(target
))
3310 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3312 if (TRANS(win
) && (win
->transient
== target
->transient
||
3313 win
->transient
== target
->id
))
3315 if (FULLSCREEN(win
))
3317 if (FULLSCREEN(target
))
3321 if (MAXIMIZED(target
))
3323 if (ABOVE(win
) || TRANS(win
) ||
3324 (win
->ws
->focus
== win
&& ws
->always_raise
))
3326 if (!ABOVE(target
) && !TRANS(target
))
3330 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3332 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3334 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3336 update_win_stacking(win
);
3339 if (swm_debug
& SWM_D_STACK
) {
3340 DPRINTF("=== stacking order (top down) === \n");
3341 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3342 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3343 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3344 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3345 YESNO(ICONIC(target
)));
3349 DNPRINTF(SWM_D_EVENT
, "raise_window: done\n");
3353 update_win_stacking(struct ws_win
*win
)
3355 struct ws_win
*sibling
;
3359 struct swm_region
*r
;
3362 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
3365 if (win
->frame
== XCB_WINDOW_NONE
) {
3366 DNPRINTF(SWM_D_EVENT
, "update_window_stacking: win %#x not "
3367 "reparented.\n", win
->id
);
3371 sibling
= TAILQ_NEXT(win
, stack_entry
);
3372 if (sibling
!= NULL
&& (FLOATING(win
) == FLOATING(sibling
) ||
3373 (win
->ws
->always_raise
&& win
->ws
->focus
== win
)))
3374 val
[0] = sibling
->frame
;
3375 else if (FLOATING(win
) || (win
->ws
->always_raise
&&
3376 win
->ws
->focus
== win
))
3377 val
[0] = r
->bar
->id
;
3381 DNPRINTF(SWM_D_EVENT
, "update_win_stacking: win %#x (%#x), "
3382 "sibling %#x\n", win
->frame
, win
->id
, val
[0]);
3384 val
[1] = XCB_STACK_MODE_ABOVE
;
3386 xcb_configure_window(conn
, win
->frame
, XCB_CONFIG_WINDOW_SIBLING
|
3387 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
3390 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
)
3396 map_window(struct ws_win
*win
)
3401 DNPRINTF(SWM_D_EVENT
, "map_window: win %#x, mapped: %s\n",
3402 win
->id
, YESNO(win
->mapped
));
3407 xcb_map_window(conn
, win
->frame
);
3408 xcb_map_window(conn
, win
->id
);
3409 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
3414 unmap_window(struct ws_win
*win
)
3419 DNPRINTF(SWM_D_EVENT
, "unmap_window: win %#x, mapped: %s\n", win
->id
,
3420 YESNO(win
->mapped
));
3425 xcb_unmap_window(conn
, win
->id
);
3426 xcb_unmap_window(conn
, win
->frame
);
3427 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
3428 win
->mapped
= false;
3435 int i
, j
, num_screens
;
3437 num_screens
= get_screen_count();
3438 for (i
= 0; i
< num_screens
; i
++)
3439 for (j
= 0; j
< workspace_limit
; j
++)
3440 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3445 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
3447 xcb_key_press_event_t event
;
3448 xcb_keycode_t
*keycode
;
3453 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
3455 DNPRINTF(SWM_D_MISC
, "fake_keypress: win %#x, keycode %u\n",
3458 bzero(&event
, sizeof(event
));
3459 event
.event
= win
->id
;
3460 event
.root
= win
->s
->root
;
3461 event
.child
= XCB_WINDOW_NONE
;
3462 event
.time
= XCB_CURRENT_TIME
;
3463 event
.event_x
= X(win
);
3464 event
.event_y
= Y(win
);
3467 event
.same_screen
= 1;
3468 event
.detail
= *keycode
;
3469 event
.state
= modifiers
;
3471 event
.response_type
= XCB_KEY_PRESS
;
3472 xcb_send_event(conn
, 1, win
->id
,
3473 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
3475 event
.response_type
= XCB_KEY_RELEASE
;
3476 xcb_send_event(conn
, 1, win
->id
,
3477 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
3483 restart(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3485 /* suppress unused warning since var is needed */
3490 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
3494 execvp(start_argv
[0], start_argv
);
3495 warn("execvp failed");
3496 quit(NULL
, NULL
, NULL
);
3500 get_pointer_win(xcb_window_t root
)
3502 struct ws_win
*win
= NULL
;
3503 xcb_query_pointer_reply_t
*r
;
3505 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: %#x.\n", root
);
3507 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
3509 win
= find_window(r
->child
);
3511 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: %#x.\n",
3514 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
3523 center_pointer(struct swm_region
*r
)
3527 if (!warp_pointer
|| r
== NULL
)
3532 DNPRINTF(SWM_D_EVENT
, "center_pointer: win %#x.\n", WINID(win
));
3534 if (win
&& win
->mapped
)
3535 xcb_warp_pointer(conn
, XCB_NONE
, win
->frame
, 0, 0, 0, 0,
3536 WIDTH(win
) / 2, HEIGHT(win
) / 2);
3538 xcb_warp_pointer(conn
, XCB_NONE
, r
->id
, 0, 0, 0, 0,
3539 WIDTH(r
) / 2, HEIGHT(r
) / 2);
3543 root_to_region(xcb_window_t root
, int check
)
3546 struct swm_region
*r
= NULL
;
3548 xcb_query_pointer_reply_t
*qpr
;
3549 xcb_get_input_focus_reply_t
*gifr
;
3551 DNPRINTF(SWM_D_MISC
, "root_to_region: win %#x\n", root
);
3553 num_screens
= get_screen_count();
3554 for (i
= 0; i
< num_screens
; i
++)
3555 if (screens
[i
].root
== root
)
3558 if (check
& SWM_CK_REGION
)
3559 r
= screens
[i
].r_focus
;
3561 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
3562 /* Try to find an actively focused window */
3563 gifr
= xcb_get_input_focus_reply(conn
,
3564 xcb_get_input_focus(conn
), NULL
);
3566 cfw
= find_window(gifr
->focus
);
3567 if (cfw
&& cfw
->ws
->r
)
3574 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
3575 /* No region with an active focus; try to use pointer. */
3576 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
3577 screens
[i
].root
), NULL
);
3579 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
3580 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
3581 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3582 if (X(r
) <= qpr
->root_x
&&
3583 qpr
->root_x
< MAX_X(r
) &&
3584 Y(r
) <= qpr
->root_y
&&
3585 qpr
->root_y
< MAX_Y(r
))
3592 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
3593 r
= TAILQ_FIRST(&screens
[i
].rl
);
3595 DNPRINTF(SWM_D_MISC
, "root_to_region: idx: %d\n", get_region_index(r
));
3601 find_region(xcb_window_t id
)
3603 struct swm_region
*r
;
3606 num_screens
= get_screen_count();
3607 for (i
= 0; i
< num_screens
; i
++)
3608 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3616 find_bar(xcb_window_t id
)
3618 struct swm_region
*r
;
3621 num_screens
= get_screen_count();
3622 for (i
= 0; i
< num_screens
; i
++)
3623 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3624 if (r
->bar
&& r
->bar
->id
== id
)
3631 find_frame_window(xcb_window_t id
) {
3632 struct swm_region
*r
;
3636 num_screens
= get_screen_count();
3637 for (i
= 0; i
< num_screens
; i
++)
3638 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3639 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
3647 find_window(xcb_window_t id
)
3649 struct ws_win
*win
= NULL
;
3650 int i
, j
, num_screens
;
3651 xcb_query_tree_reply_t
*qtr
;
3653 DNPRINTF(SWM_D_MISC
, "find_window: id: %#x\n", id
);
3655 num_screens
= get_screen_count();
3656 for (i
= 0; i
< num_screens
; i
++)
3657 for (j
= 0; j
< workspace_limit
; j
++)
3658 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3659 if (id
== win
->id
|| id
== win
->frame
)
3663 /* If window isn't top-level, try to find managed ancestor. */
3664 qtr
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3666 if (qtr
->parent
!= XCB_WINDOW_NONE
&& qtr
->parent
!= qtr
->root
)
3667 win
= find_window(qtr
->parent
);
3671 DNPRINTF(SWM_D_MISC
, "find_window: found child %#x "
3672 "of %#x.\n", win
->id
, qtr
->parent
);
3682 find_unmanaged_window(xcb_window_t id
)
3685 int i
, j
, num_screens
;
3687 num_screens
= get_screen_count();
3688 for (i
= 0; i
< num_screens
; i
++)
3689 for (j
= 0; j
< workspace_limit
; j
++)
3690 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
3698 spawn(int ws_idx
, union arg
*args
, bool close_fd
)
3703 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3705 close(xcb_get_file_descriptor(conn
));
3707 setenv("LD_PRELOAD", SWM_LIB
, 1);
3709 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3710 warn("spawn: asprintf SWM_WS");
3713 setenv("_SWM_WS", ret
, 1);
3717 if (asprintf(&ret
, "%d", getpid()) == -1) {
3718 warn("spawn: asprintf _SWM_PID");
3721 setenv("_SWM_PID", ret
, 1);
3725 if (setsid() == -1) {
3726 warn("spawn: setsid");
3732 * close stdin and stdout to prevent interaction between apps
3733 * and the baraction script
3734 * leave stderr open to record errors
3736 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3737 warn("spawn: open");
3740 dup2(fd
, STDIN_FILENO
);
3741 dup2(fd
, STDOUT_FILENO
);
3746 execvp(args
->argv
[0], args
->argv
);
3748 warn("spawn: execvp");
3753 kill_refs(struct ws_win
*win
)
3755 struct workspace
*ws
;
3757 int i
, j
, num_screens
;
3762 num_screens
= get_screen_count();
3763 for (i
= 0; i
< num_screens
; i
++) {
3764 for (j
= 0; j
< workspace_limit
; j
++) {
3765 ws
= &screens
[i
].ws
[j
];
3767 if (win
== ws
->focus
)
3769 if (win
== ws
->focus_prev
)
3770 ws
->focus_prev
= NULL
;
3771 if (win
== ws
->focus_pending
)
3772 ws
->focus_pending
= NULL
;
3775 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3776 if (win
== w
->focus_child
)
3777 w
->focus_child
= NULL
;
3783 validate_win(struct ws_win
*testwin
)
3786 struct workspace
*ws
;
3787 struct swm_region
*r
;
3788 int i
, x
, num_screens
;
3790 if (testwin
== NULL
)
3793 num_screens
= get_screen_count();
3794 for (i
= 0; i
< num_screens
; i
++)
3795 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3796 for (x
= 0; x
< workspace_limit
; x
++) {
3798 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3806 validate_ws(struct workspace
*testws
)
3808 struct swm_region
*r
;
3809 struct workspace
*ws
;
3810 int i
, x
, num_screens
;
3812 /* validate all ws */
3813 num_screens
= get_screen_count();
3814 for (i
= 0; i
< num_screens
; i
++)
3815 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3816 for (x
= 0; x
< workspace_limit
; x
++) {
3825 unfocus_win(struct ws_win
*win
)
3827 xcb_window_t none
= XCB_WINDOW_NONE
;
3829 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: win %#x\n", WINID(win
));
3834 if (win
->ws
== NULL
) {
3835 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3839 if (validate_ws(win
->ws
)) {
3840 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3844 if (win
->ws
->r
== NULL
) {
3845 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3849 if (validate_win(win
)) {
3850 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3855 if (win
->ws
->focus
== win
) {
3856 win
->ws
->focus
= NULL
;
3857 win
->ws
->focus_prev
= win
;
3860 if (validate_win(win
->ws
->focus
)) {
3861 kill_refs(win
->ws
->focus
);
3862 win
->ws
->focus
= NULL
;
3865 if (validate_win(win
->ws
->focus_prev
)) {
3866 kill_refs(win
->ws
->focus_prev
);
3867 win
->ws
->focus_prev
= NULL
;
3872 /* Raise window to "top unfocused position." */
3873 if (win
->ws
->always_raise
)
3876 /* Update border width */
3877 if (win
->bordered
&& (win
->quirks
& SWM_Q_MINIMALBORDER
) &&
3880 X(win
) += border_width
;
3881 Y(win
) += border_width
;
3885 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3886 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3888 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3892 focus_win(struct ws_win
*win
)
3894 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
, *tmpw
;
3895 struct workspace
*ws
;
3896 xcb_get_input_focus_reply_t
*gifr
= NULL
;
3897 xcb_get_window_attributes_reply_t
*war
= NULL
;
3899 DNPRINTF(SWM_D_FOCUS
, "focus_win: win %#x\n", WINID(win
));
3901 if (win
== NULL
|| win
->ws
== NULL
|| !win
->mapped
)
3906 if (validate_ws(ws
))
3909 if (validate_win(win
)) {
3914 gifr
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3916 DNPRINTF(SWM_D_FOCUS
, "focus_win: cur focus: %#x\n",
3919 cfw
= find_window(gifr
->focus
);
3922 if (cfw
->ws
!= ws
&& cfw
->ws
->r
!= NULL
&&
3923 cfw
->frame
!= XCB_WINDOW_NONE
) {
3930 war
= xcb_get_window_attributes_reply(conn
,
3931 xcb_get_window_attributes(conn
, gifr
->focus
), NULL
);
3932 if (war
&& war
->override_redirect
&& ws
->focus
== win
) {
3933 DNPRINTF(SWM_D_FOCUS
, "focus_win: skip refocus "
3934 "from override_redirect.\n");
3940 if (ws
->focus
!= win
) {
3941 if (ws
->focus
&& ws
->focus
!= cfw
)
3942 unfocus_win(ws
->focus
);
3946 /* Clear focus child redirect. */
3947 win
->focus_child
= NULL
;
3949 /* If transient, adjust parent's focus child for focus_magic. */
3951 parent
= find_window(win
->transient
);
3952 if (parent
&& parent
->focus_child
!= win
)
3953 parent
->focus_child
= win
;
3956 /* Update window border even if workspace is hidden. */
3960 DNPRINTF(SWM_D_FOCUS
, "focus_win: already focused.\n");
3965 /* Set input focus if no input hint, or indicated by hint. */
3966 if (ACCEPTS_FOCUS(win
)) {
3967 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
3968 " revert-to: parent, time: %#x\n", win
->id
,
3970 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
3971 win
->id
, last_event_time
);
3972 } else if (!win
->take_focus
) {
3973 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
3974 " revert-to: parent, time: 0\n", ws
->r
->id
);
3975 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
3976 ws
->r
->id
, XCB_CURRENT_TIME
);
3979 /* Tell app it can adjust focus to a specific window. */
3980 if (win
->take_focus
) {
3981 /* java is special; always tell parent */
3982 if (TRANS(win
) && win
->java
)
3983 client_msg(parent
, a_takefocus
,
3986 client_msg(win
, a_takefocus
, last_event_time
);
3989 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
3991 /* If a parent exists, map it first. */
3993 raise_window(parent
);
3996 /* Map siblings next. */
3997 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
,
3999 if (w
!= win
&& !ICONIC(w
) &&
4000 w
->transient
== parent
->id
) {
4006 /* Map focused window. */
4010 /* Stack any children of focus window. */
4011 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
4012 if (w
->transient
== win
->id
&& !ICONIC(w
)) {
4016 } else if (tile_gap
< 0 && !ABOVE(win
)) {
4018 * Windows overlap in the layout.
4019 * Raise focused win above all tiled wins.
4027 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4028 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4031 bar_draw(ws
->r
->bar
);
4037 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
4040 /* If a child window should have focus instead, return it. */
4042 get_focus_magic(struct ws_win
*win
)
4044 struct ws_win
*parent
= NULL
;
4045 struct ws_win
*child
= NULL
;
4047 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: win %#x\n", WINID(win
));
4052 parent
= find_window(win
->transient
);
4054 /* If parent prefers focus elsewhere, then try to do so. */
4055 if (parent
&& (child
= parent
->focus_child
)) {
4056 if (validate_win(child
) == 0 && child
->mapped
)
4059 parent
->focus_child
= NULL
;
4063 /* If this window prefers focus elsewhere, then try to do so. */
4064 if ((child
= win
->focus_child
)) {
4065 if (validate_win(child
) == 0 && child
->mapped
)
4068 win
->focus_child
= NULL
;
4075 event_drain(uint8_t rt
)
4077 xcb_generic_event_t
*evt
;
4079 /* ensure all pending requests have been processed before filtering. */
4081 while ((evt
= get_next_event(false))) {
4082 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
4090 set_region(struct swm_region
*r
)
4092 struct swm_region
*rf
;
4099 /* Unfocus old region bar. */
4104 xcb_change_window_attributes(conn
, rf
->bar
->id
,
4105 XCB_CW_BORDER_PIXEL
,
4106 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
4109 if (rf
!= NULL
&& rf
!= r
&& (X(rf
) != X(r
) || Y(rf
) != Y(r
) ||
4110 WIDTH(rf
) != WIDTH(r
) || HEIGHT(rf
) != HEIGHT(r
))) {
4111 /* Set _NET_DESKTOP_GEOMETRY. */
4113 vals
[1] = HEIGHT(r
);
4114 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, r
->s
->root
,
4115 ewmh
[_NET_DESKTOP_GEOMETRY
].atom
, XCB_ATOM_CARDINAL
, 32, 2,
4119 /* Set region bar border to focus_color. */
4120 xcb_change_window_attributes(conn
, r
->bar
->id
,
4121 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
4125 ewmh_update_current_desktop();
4129 focus_region(struct swm_region
*r
)
4132 struct swm_region
*old_r
;
4137 old_r
= r
->s
->r_focus
;
4140 nfw
= get_region_focus(r
);
4144 /* New region is empty; need to manually unfocus win. */
4146 unfocus_win(old_r
->ws
->focus
);
4147 /* Clear bar since empty. */
4148 bar_draw(old_r
->bar
);
4151 DNPRINTF(SWM_D_FOCUS
, "focus_region: set_input_focus: %#x, "
4152 "revert-to: parent, time: 0\n", r
->id
);
4153 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4160 switchws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4162 struct swm_region
*this_r
, *other_r
;
4164 struct workspace
*new_ws
, *old_ws
;
4165 xcb_window_t none
= XCB_WINDOW_NONE
;
4166 int wsid
= args
->id
;
4167 bool unmap_old
= false;
4172 if (wsid
>= workspace_limit
)
4176 old_ws
= this_r
->ws
;
4177 new_ws
= &this_r
->s
->ws
[wsid
];
4179 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
4180 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
4182 if (new_ws
== NULL
|| old_ws
== NULL
)
4184 if (new_ws
== old_ws
)
4187 other_r
= new_ws
->r
;
4188 if (other_r
&& workspace_clamp
&&
4189 bp
->action
!= FN_RG_MOVE_NEXT
&& bp
->action
!= FN_RG_MOVE_PREV
) {
4190 DNPRINTF(SWM_D_WS
, "switchws: ws clamped.\n");
4193 DNPRINTF(SWM_D_WS
, "switchws: warping focus to region "
4194 "with ws %d.\n", wsid
);
4195 focus_region(other_r
);
4196 center_pointer(other_r
);
4202 if ((win
= old_ws
->focus
) != NULL
) {
4205 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4206 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4211 /* the other ws is visible in another region, exchange them */
4212 other_r
->ws_prior
= new_ws
;
4213 other_r
->ws
= old_ws
;
4214 old_ws
->r
= other_r
;
4216 /* the other workspace is hidden, hide this one */
4221 this_r
->ws_prior
= old_ws
;
4222 this_r
->ws
= new_ws
;
4225 /* Set focus_pending before stacking, if needed. */
4226 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
4227 validate_win(new_ws
->focus_pending
))) {
4228 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
4229 new_ws
->focus
= new_ws
->focus_prev
;
4230 new_ws
->focus_prev
= NULL
;
4233 new_ws
->state
= SWM_WS_STATE_MAPPING
;
4238 /* unmap old windows */
4240 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
4242 old_ws
->state
= SWM_WS_STATE_HIDDEN
;
4245 /* if workspaces were swapped, then don't wait to set focus */
4246 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
4247 if (new_ws
->focus_pending
) {
4248 focus_win(new_ws
->focus_pending
);
4249 new_ws
->focus_pending
= NULL
;
4253 /* Clear bar and set focus on region input win if new ws is empty. */
4254 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
4255 DNPRINTF(SWM_D_FOCUS
, "switchws: set_input_focus: %#x, "
4256 "revert-to: parent, time: 0\n", r
->id
);
4257 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4262 ewmh_update_current_desktop();
4266 new_ws
->state
= SWM_WS_STATE_MAPPED
;
4268 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
4272 cyclews(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4275 struct swm_screen
*s
= r
->s
;
4276 bool cycle_all
= false, mv
= false;
4278 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4279 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4285 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
4288 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
4291 case SWM_ARG_ID_CYCLEWS_UP
:
4292 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
4294 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
4297 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
4300 case SWM_ARG_ID_CYCLEWS_DOWN
:
4301 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
4308 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
4310 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
4314 send_to_ws(bp
, r
, &a
);
4316 switchws(bp
, r
, &a
);
4317 } while (a
.id
!= r
->ws
->idx
);
4319 DNPRINTF(SWM_D_FOCUS
, "cyclews: done\n");
4323 priorws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4329 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4330 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4332 if (r
->ws_prior
== NULL
)
4335 a
.id
= r
->ws_prior
->idx
;
4336 switchws(bp
, r
, &a
);
4337 DNPRINTF(SWM_D_FOCUS
, "priorws: done\n");
4341 focusrg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4343 int ridx
= args
->id
, i
, num_screens
;
4344 struct swm_region
*rr
= NULL
;
4348 num_screens
= get_screen_count();
4349 /* do nothing if we don't have more than one screen */
4350 if (!(num_screens
> 1 || outputs
> 1))
4353 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
4355 rr
= TAILQ_FIRST(&r
->s
->rl
);
4356 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
4357 rr
= TAILQ_NEXT(rr
, entry
);
4365 DNPRINTF(SWM_D_FOCUS
, "focusrg: done\n");
4369 cyclerg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4372 struct swm_region
*rr
= NULL
;
4375 num_screens
= get_screen_count();
4376 /* do nothing if we don't have more than one screen */
4377 if (!(num_screens
> 1 || outputs
> 1))
4381 DNPRINTF(SWM_D_FOCUS
, "cyclerg: id: %d, region: %d\n", args
->id
, i
);
4384 case SWM_ARG_ID_CYCLERG_UP
:
4385 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4386 rr
= TAILQ_NEXT(r
, entry
);
4388 rr
= TAILQ_FIRST(&screens
[i
].rl
);
4390 case SWM_ARG_ID_CYCLERG_DOWN
:
4391 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4392 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
4394 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
4403 case SWM_ARG_ID_CYCLERG_UP
:
4404 case SWM_ARG_ID_CYCLERG_DOWN
:
4409 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4410 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4412 switchws(bp
, r
, &a
);
4418 DNPRINTF(SWM_D_FOCUS
, "cyclerg: done\n");
4421 /* Sorts transients after parent. */
4423 sort_windows(struct ws_win_list
*wl
)
4425 struct ws_win
*win
, *parent
, *nxt
;
4430 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
4431 nxt
= TAILQ_NEXT(win
, entry
);
4433 parent
= find_window(win
->transient
);
4434 if (parent
== NULL
) {
4435 warnx("not possible bug");
4438 TAILQ_REMOVE(wl
, win
, entry
);
4439 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
4445 swapwin(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4447 struct ws_win
*target
, *source
;
4448 struct ws_win
*cur_focus
;
4449 struct ws_win_list
*wl
;
4453 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4454 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4456 cur_focus
= r
->ws
->focus
;
4457 if (cur_focus
== NULL
|| FULLSCREEN(cur_focus
))
4460 /* Adjust stacking in floating layer. */
4461 if (ABOVE(cur_focus
)) {
4463 case SWM_ARG_ID_SWAPPREV
:
4464 target
= TAILQ_PREV(cur_focus
, ws_win_stack
,
4466 if (target
!= NULL
&& FLOATING(target
)) {
4467 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4469 TAILQ_INSERT_BEFORE(target
, cur_focus
,
4471 update_win_stacking(cur_focus
);
4475 case SWM_ARG_ID_SWAPNEXT
:
4476 target
= TAILQ_NEXT(cur_focus
, stack_entry
);
4477 if (target
!= NULL
&& FLOATING(target
)) {
4478 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4480 TAILQ_INSERT_AFTER(&cur_focus
->ws
->stack
,
4481 target
, cur_focus
, stack_entry
);
4482 update_win_stacking(cur_focus
);
4490 if (r
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4493 clear_maximized(r
->ws
);
4496 wl
= &source
->ws
->winlist
;
4499 case SWM_ARG_ID_SWAPPREV
:
4501 source
= find_window(source
->transient
);
4502 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
4503 if (target
&& target
->transient
)
4504 target
= find_window(target
->transient
);
4505 TAILQ_REMOVE(wl
, source
, entry
);
4507 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4509 TAILQ_INSERT_BEFORE(target
, source
, entry
);
4511 case SWM_ARG_ID_SWAPNEXT
:
4512 target
= TAILQ_NEXT(source
, entry
);
4513 /* move the parent and let the sort handle the move */
4515 source
= find_window(source
->transient
);
4516 TAILQ_REMOVE(wl
, source
, entry
);
4518 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4520 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
4522 case SWM_ARG_ID_SWAPMAIN
:
4523 target
= TAILQ_FIRST(wl
);
4524 if (target
== source
) {
4525 if (source
->ws
->focus_prev
!= NULL
&&
4526 source
->ws
->focus_prev
!= target
)
4527 source
= source
->ws
->focus_prev
;
4531 if (target
== NULL
|| source
== NULL
)
4533 source
->ws
->focus_prev
= target
;
4534 TAILQ_REMOVE(wl
, target
, entry
);
4535 TAILQ_INSERT_BEFORE(source
, target
, entry
);
4536 TAILQ_REMOVE(wl
, source
, entry
);
4537 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4539 case SWM_ARG_ID_MOVELAST
:
4540 TAILQ_REMOVE(wl
, source
, entry
);
4541 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4544 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
4549 ewmh_update_client_list();
4555 DNPRINTF(SWM_D_MOVE
, "swapwin: done\n");
4559 get_focus_prev(struct ws_win
*win
)
4561 struct ws_win
*winfocus
= NULL
;
4562 struct ws_win
*cur_focus
= NULL
;
4563 struct ws_win_list
*wl
= NULL
;
4564 struct workspace
*ws
= NULL
;
4566 if (!(win
&& win
->ws
))
4571 cur_focus
= ws
->focus
;
4573 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: win %#x, cur_focus: %#x, "
4574 "focus_prev: %#x\n", WINID(win
), WINID(cur_focus
),
4575 WINID(ws
->focus_prev
));
4577 /* pickle, just focus on whatever */
4578 if (cur_focus
== NULL
) {
4579 /* use prev_focus if valid */
4580 if (ws
->focus_prev
&& find_window(ws
->focus_prev
->id
))
4581 winfocus
= ws
->focus_prev
;
4585 /* if transient focus on parent */
4586 if (TRANS(cur_focus
)) {
4587 winfocus
= find_window(cur_focus
->transient
);
4591 /* if in max_stack try harder */
4592 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
4593 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
4594 if (cur_focus
!= ws
->focus_prev
)
4595 winfocus
= ws
->focus_prev
;
4597 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
4602 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
4604 if (winfocus
== NULL
|| winfocus
== win
) {
4605 switch (focus_close
) {
4606 case SWM_STACK_BOTTOM
:
4607 TAILQ_FOREACH(winfocus
, wl
, entry
)
4608 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4612 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4613 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4616 case SWM_STACK_ABOVE
:
4617 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
4618 while (winfocus
&& ICONIC(winfocus
))
4619 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4621 if (winfocus
== NULL
) {
4622 if (focus_close_wrap
) {
4623 TAILQ_FOREACH(winfocus
, wl
, entry
)
4624 if (!ICONIC(winfocus
) &&
4625 winfocus
!= cur_focus
)
4628 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4630 if (!ICONIC(winfocus
) &&
4631 winfocus
!= cur_focus
)
4636 case SWM_STACK_BELOW
:
4637 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
4638 while (winfocus
&& ICONIC(winfocus
))
4639 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
4642 if (winfocus
== NULL
) {
4643 if (focus_close_wrap
) {
4644 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4646 if (!ICONIC(winfocus
) &&
4647 winfocus
!= cur_focus
)
4650 TAILQ_FOREACH(winfocus
, wl
, entry
)
4651 if (!ICONIC(winfocus
) &&
4652 winfocus
!= cur_focus
)
4660 if (winfocus
== NULL
||
4661 (winfocus
&& (ICONIC(winfocus
) || winfocus
== cur_focus
))) {
4662 if (focus_default
== SWM_STACK_TOP
) {
4663 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4664 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4667 TAILQ_FOREACH(winfocus
, wl
, entry
)
4668 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4675 return get_focus_magic(winfocus
);
4679 get_region_focus(struct swm_region
*r
)
4681 struct ws_win
*winfocus
= NULL
;
4686 if (r
->ws
->focus
&& !ICONIC(r
->ws
->focus
))
4687 winfocus
= r
->ws
->focus
;
4688 else if (r
->ws
->focus_prev
&& !ICONIC(r
->ws
->focus_prev
))
4689 winfocus
= r
->ws
->focus_prev
;
4691 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
4692 if (!ICONIC(winfocus
))
4695 return get_focus_magic(winfocus
);
4699 focus(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4701 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
4702 struct ws_win_list
*wl
= NULL
;
4703 struct workspace
*ws
= NULL
;
4710 cur_focus
= r
->ws
->focus
;
4714 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d, cur_focus: %#x\n", args
->id
,
4717 /* Make sure an uniconified window has focus, if one exists. */
4718 if (cur_focus
== NULL
) {
4719 cur_focus
= TAILQ_FIRST(wl
);
4720 while (cur_focus
!= NULL
&& ICONIC(cur_focus
))
4721 cur_focus
= TAILQ_NEXT(cur_focus
, entry
);
4723 DNPRINTF(SWM_D_FOCUS
, "focus: new cur_focus: %#x\n",
4728 case SWM_ARG_ID_FOCUSPREV
:
4729 if (cur_focus
== NULL
)
4732 winfocus
= cur_focus
;
4734 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
4735 if (winfocus
== NULL
)
4736 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
4737 if (winfocus
== cur_focus
)
4739 } while (winfocus
&& (ICONIC(winfocus
) ||
4740 winfocus
->id
== cur_focus
->transient
||
4741 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4742 winfocus
->transient
== cur_focus
->transient
) ||
4743 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4745 case SWM_ARG_ID_FOCUSNEXT
:
4746 if (cur_focus
== NULL
)
4749 winfocus
= cur_focus
;
4751 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4752 if (winfocus
== NULL
)
4753 winfocus
= TAILQ_FIRST(wl
);
4754 if (winfocus
== cur_focus
)
4756 } while (winfocus
&& (ICONIC(winfocus
) ||
4757 winfocus
->id
== cur_focus
->transient
||
4758 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4759 winfocus
->transient
== cur_focus
->transient
) ||
4760 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4762 case SWM_ARG_ID_FOCUSMAIN
:
4763 if (cur_focus
== NULL
)
4766 winfocus
= TAILQ_FIRST(wl
);
4767 if (winfocus
== cur_focus
)
4768 winfocus
= cur_focus
->ws
->focus_prev
;
4770 case SWM_ARG_ID_FOCUSURGENT
:
4771 /* Search forward for the next urgent window. */
4775 for (i
= 0; i
<= workspace_limit
; ++i
) {
4777 head
= TAILQ_FIRST(&r
->s
->ws
[(ws
->idx
+ i
) %
4778 workspace_limit
].winlist
);
4781 if (head
== cur_focus
) {
4783 winfocus
= cur_focus
;
4786 } else if (get_urgent(head
)) {
4791 head
= TAILQ_NEXT(head
, entry
);
4798 /* Switch ws if new focus is on a different ws. */
4799 if (winfocus
&& winfocus
->ws
!= ws
) {
4800 a
.id
= winfocus
->ws
->idx
;
4801 switchws(bp
, r
, &a
);
4808 if (clear_maximized(ws
) > 0)
4811 focus_win(get_focus_magic(winfocus
));
4816 DNPRINTF(SWM_D_FOCUS
, "focus: done\n");
4820 focus_pointer(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4824 /* Not needed for buttons since this is already done in buttonpress. */
4825 if (bp
->type
== KEYBIND
) {
4826 focus_win(get_pointer_win(r
->s
->root
));
4832 cycle_layout(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4834 struct workspace
*ws
= r
->ws
;
4836 /* suppress unused warning since var is needed */
4840 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
4843 if (ws
->cur_layout
->l_stack
== NULL
)
4844 ws
->cur_layout
= &layouts
[0];
4846 clear_maximized(ws
);
4851 focus_win(get_region_focus(r
));
4858 stack_config(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4860 struct workspace
*ws
= r
->ws
;
4864 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
4867 if (clear_maximized(ws
) > 0)
4870 if (ws
->cur_layout
->l_config
!= NULL
)
4871 ws
->cur_layout
->l_config(ws
, args
->id
);
4873 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
4882 stack(struct swm_region
*r
) {
4883 struct swm_geometry g
;
4884 struct swm_region
*r_prev
;
4890 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
4892 /* Adjust stack area for region bar and padding. */
4894 g
.x
+= region_padding
;
4895 g
.y
+= region_padding
;
4896 g
.w
-= 2 * border_width
+ 2 * region_padding
;
4897 g
.h
-= 2 * border_width
+ 2 * region_padding
;
4898 if (bar_enabled
&& r
->ws
->bar_enabled
) {
4904 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d (screen: "
4905 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4906 r
->ws
->idx
, r
->s
->idx
, get_region_index(r
), g
.x
, g
.y
, g
.w
, g
.h
);
4908 r_prev
= TAILQ_PREV(r
, swm_region_list
, entry
);
4910 /* Stack bar/input relative to prev. region. */
4911 val
[1] = XCB_STACK_MODE_ABOVE
;
4913 val
[0] = r_prev
->id
;
4914 DNPRINTF(SWM_D_STACK
, "stack: region input %#x "
4915 "relative to %#x.\n", r
->id
, val
[0]);
4916 xcb_configure_window(conn
, r
->id
,
4917 XCB_CONFIG_WINDOW_SIBLING
|
4918 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4920 val
[0] = r_prev
->bar
->id
;
4921 DNPRINTF(SWM_D_STACK
, "stack: region bar %#x "
4922 "relative to %#x.\n", r
->bar
->id
, val
[0]);
4923 xcb_configure_window(conn
, r
->bar
->id
,
4924 XCB_CONFIG_WINDOW_SIBLING
|
4925 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4928 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
4929 r
->ws
->cur_layout
->l_string(r
->ws
);
4930 /* save r so we can track region changes */
4936 DNPRINTF(SWM_D_STACK
, "stack: end\n");
4940 store_float_geom(struct ws_win
*win
)
4942 if (win
== NULL
|| win
->ws
->r
== NULL
)
4945 /* retain window geom and region geom */
4946 win
->g_float
= win
->g
;
4947 win
->g_float
.x
-= X(win
->ws
->r
);
4948 win
->g_float
.y
-= Y(win
->ws
->r
);
4949 win
->g_floatvalid
= true;
4950 DNPRINTF(SWM_D_MISC
, "store_float_geom: win %#x, g: (%d,%d)"
4951 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
4952 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
4953 win
->g_float
.w
, win
->g_float
.h
);
4957 load_float_geom(struct ws_win
*win
)
4959 if (win
== NULL
|| win
->ws
->r
== NULL
)
4962 if (win
->g_floatvalid
) {
4963 win
->g
= win
->g_float
;
4964 X(win
) += X(win
->ws
->r
);
4965 Y(win
) += Y(win
->ws
->r
);
4966 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g: (%d,%d)"
4967 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
4970 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g_float "
4971 "is not set.\n", win
->id
);
4976 update_floater(struct ws_win
*win
)
4978 struct workspace
*ws
;
4979 struct swm_region
*r
;
4981 DNPRINTF(SWM_D_MISC
, "update_floater: win %#x\n", WINID(win
));
4988 if ((r
= ws
->r
) == NULL
)
4991 win
->bordered
= true;
4993 if (FULLSCREEN(win
)) {
4994 /* _NET_WM_FULLSCREEN: fullscreen without border. */
4995 if (!win
->g_floatvalid
)
4996 store_float_geom(win
);
4999 win
->bordered
= false;
5000 } else if (MAXIMIZED(win
)) {
5001 /* Maximize: like a single stacked window. */
5002 if (!win
->g_floatvalid
)
5003 store_float_geom(win
);
5007 if (bar_enabled
&& ws
->bar_enabled
&& !maximize_hide_bar
) {
5009 Y(win
) += bar_height
;
5010 HEIGHT(win
) -= bar_height
;
5011 } else if (disable_border
) {
5012 win
->bordered
= false;
5015 if (win
->bordered
) {
5016 /* Window geometry excludes frame. */
5017 X(win
) += border_width
;
5018 Y(win
) += border_width
;
5019 HEIGHT(win
) -= 2 * border_width
;
5020 WIDTH(win
) -= 2 * border_width
;
5023 /* Normal floating window. */
5024 /* Update geometry if new region. */
5026 load_float_geom(win
);
5028 if (((win
->quirks
& SWM_Q_FULLSCREEN
) &&
5029 WIDTH(win
) >= WIDTH(r
) && HEIGHT(win
) >= HEIGHT(r
)) ||
5030 ((!WS_FOCUSED(win
->ws
) || win
->ws
->focus
!= win
) &&
5031 (win
->quirks
& SWM_Q_MINIMALBORDER
))) {
5033 win
->bordered
= false;
5034 } else if (!MANUAL(win
)) {
5035 if (TRANS(win
) && (win
->quirks
& SWM_Q_TRANSSZ
)) {
5036 /* Adjust size on TRANSSZ quirk. */
5037 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
5038 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
5041 if (!(win
->quirks
& SWM_Q_ANYWHERE
)) {
5043 * Floaters and transients are auto-centred
5044 * unless manually moved, resized or ANYWHERE
5047 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2;
5048 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2;
5049 store_float_geom(win
);
5054 /* Ensure at least 1 pixel of the window is in the region. */
5055 region_containment(win
, r
, SWM_CW_ALLSIDES
);
5061 * Send keystrokes to terminal to decrease/increase the font size as the
5062 * window size changes.
5065 adjust_font(struct ws_win
*win
)
5067 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
5068 ABOVE(win
) || TRANS(win
))
5071 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
5072 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
5073 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
5074 win
->font_size_boundary
[win
->font_steps
] =
5075 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
5078 win
->last_inc
= win
->sh
.width_inc
;
5079 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
5080 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
5081 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
5084 win
->last_inc
= win
->sh
.width_inc
;
5085 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
5089 #define SWAPXY(g) do { \
5091 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
5092 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
5095 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, bool flip
)
5097 struct swm_geometry cell
, r_g
= *g
;
5099 int i
= 0, j
= 0, s
= 0, stacks
= 0;
5100 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
5101 int hrh
= 0, extra
= 0, h_slice
= 0, last_h
= 0;
5102 int split
= 0, colno
= 0;
5103 int winno
, mwin
= 0, msize
= 0;
5104 int remain
, missing
, v_slice
, mscale
;
5105 bool bordered
= true, reconfigure
= false;
5108 * cell: geometry for window, including frame.
5109 * mwin: # of windows in master area.
5110 * mscale: size increment of master area.
5111 * stacks: # of stack columns
5114 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
5115 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
5117 memset(&cell
, 0, sizeof(cell
));
5119 /* Prepare tiling variables, if needed. */
5120 if ((winno
= count_win(ws
, false)) > 0) {
5121 /* Find first tiled window. */
5122 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
5123 if (!FLOATING(win
) && !ICONIC(win
))
5126 /* Take into account size hints of first tiled window. */
5128 w_inc
= win
->sh
.width_inc
;
5129 w_base
= win
->sh
.base_width
;
5130 mwin
= ws
->l_state
.horizontal_mwin
;
5131 mscale
= ws
->l_state
.horizontal_msize
;
5132 stacks
= ws
->l_state
.horizontal_stacks
;
5135 w_inc
= win
->sh
.height_inc
;
5136 w_base
= win
->sh
.base_height
;
5137 mwin
= ws
->l_state
.vertical_mwin
;
5138 mscale
= ws
->l_state
.vertical_msize
;
5139 stacks
= ws
->l_state
.vertical_stacks
;
5143 cell
.x
+= border_width
;
5144 cell
.y
+= border_width
;
5146 if (stacks
> winno
- mwin
)
5147 stacks
= winno
- mwin
;
5151 h_slice
= r_g
.h
/ SWM_H_SLICE
;
5152 if (mwin
&& winno
> mwin
) {
5153 v_slice
= r_g
.w
/ SWM_V_SLICE
;
5157 cell
.w
= v_slice
* mscale
;
5159 if (w_inc
> 1 && w_inc
< v_slice
) {
5160 /* Adjust for requested size increment. */
5161 remain
= (cell
.w
- w_base
) % w_inc
;
5167 cell
.x
+= r_g
.w
- msize
;
5170 colno
= split
= winno
/ stacks
;
5171 cell
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
5172 2 * border_width
) / stacks
);
5175 hrh
= r_g
.h
/ colno
;
5176 extra
= r_g
.h
- (colno
* hrh
);
5177 cell
.h
= hrh
- 2 * border_width
;
5178 i
= j
= 0, s
= stacks
;
5181 /* Update window geometry. */
5182 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
5186 if (FLOATING(win
)) {
5187 update_floater(win
);
5192 if (split
&& i
== split
) {
5193 colno
= (winno
- mwin
) / stacks
;
5194 if (s
<= (winno
- mwin
) % stacks
)
5197 hrh
= r_g
.h
/ colno
;
5198 extra
= r_g
.h
- (colno
* hrh
);
5201 cell
.x
+= cell
.w
+ 2 * border_width
+
5204 cell
.w
= (r_g
.w
- msize
-
5205 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
5207 cell
.w
+= (r_g
.w
- msize
-
5208 (stacks
* (2 * border_width
+ tile_gap
))) %
5212 cell
.x
-= cell
.w
+ 2 * border_width
+
5218 cell
.h
= hrh
- 2 * border_width
- tile_gap
;
5221 h_inc
= win
->sh
.width_inc
;
5222 h_base
= win
->sh
.base_width
;
5224 h_inc
= win
->sh
.height_inc
;
5225 h_base
= win
->sh
.base_height
;
5228 if (j
== colno
- 1) {
5229 cell
.h
= hrh
+ extra
;
5230 } else if (h_inc
> 1 && h_inc
< h_slice
) {
5231 /* adjust for window's requested size increment */
5232 remain
= (cell
.h
- h_base
) % h_inc
;
5233 missing
= h_inc
- remain
;
5235 if (missing
<= extra
|| j
== 0) {
5245 cell
.y
= r_g
.y
+ border_width
;
5247 cell
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
5249 /* Window coordinates exclude frame. */
5251 if (winno
> 1 || !disable_border
||
5252 (bar_enabled
&& ws
->bar_enabled
)) {
5259 if (X(win
) != cell
.y
|| Y(win
) != cell
.x
||
5260 WIDTH(win
) != cell
.h
|| HEIGHT(win
) != cell
.w
) {
5264 WIDTH(win
) = cell
.h
;
5265 HEIGHT(win
) = cell
.w
;
5268 if (X(win
) != cell
.x
|| Y(win
) != cell
.y
||
5269 WIDTH(win
) != cell
.w
|| HEIGHT(win
) != cell
.h
) {
5273 WIDTH(win
) = cell
.w
;
5274 HEIGHT(win
) = cell
.h
;
5279 X(win
) -= border_width
;
5280 Y(win
) -= border_width
;
5281 WIDTH(win
) += 2 * border_width
;
5282 HEIGHT(win
) += 2 * border_width
;
5285 if (bordered
!= win
->bordered
) {
5287 win
->bordered
= bordered
;
5300 /* Stack all windows from bottom up. */
5301 TAILQ_FOREACH_REVERSE(win
, &ws
->stack
, ws_win_stack
, stack_entry
)
5303 update_win_stacking(win
);
5305 /* Map all windows from top down. */
5306 TAILQ_FOREACH(win
, &ws
->stack
, stack_entry
)
5310 DNPRINTF(SWM_D_STACK
, "stack_master: done\n");
5314 vertical_config(struct workspace
*ws
, int id
)
5316 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
5320 case SWM_ARG_ID_STACKRESET
:
5321 case SWM_ARG_ID_STACKINIT
:
5322 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
5323 ws
->l_state
.vertical_mwin
= 1;
5324 ws
->l_state
.vertical_stacks
= 1;
5326 case SWM_ARG_ID_MASTERSHRINK
:
5327 if (ws
->l_state
.vertical_msize
> 1)
5328 ws
->l_state
.vertical_msize
--;
5330 case SWM_ARG_ID_MASTERGROW
:
5331 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
5332 ws
->l_state
.vertical_msize
++;
5334 case SWM_ARG_ID_MASTERADD
:
5335 ws
->l_state
.vertical_mwin
++;
5337 case SWM_ARG_ID_MASTERDEL
:
5338 if (ws
->l_state
.vertical_mwin
> 0)
5339 ws
->l_state
.vertical_mwin
--;
5341 case SWM_ARG_ID_STACKBALANCE
:
5342 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ (ws
->l_state
.vertical_stacks
+ 1);
5344 case SWM_ARG_ID_STACKINC
:
5345 ws
->l_state
.vertical_stacks
++;
5347 case SWM_ARG_ID_STACKDEC
:
5348 if (ws
->l_state
.vertical_stacks
> 1)
5349 ws
->l_state
.vertical_stacks
--;
5351 case SWM_ARG_ID_FLIPLAYOUT
:
5352 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
5360 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5362 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
5364 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
5368 horizontal_config(struct workspace
*ws
, int id
)
5370 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
5373 case SWM_ARG_ID_STACKRESET
:
5374 case SWM_ARG_ID_STACKINIT
:
5375 ws
->l_state
.horizontal_mwin
= 1;
5376 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
5377 ws
->l_state
.horizontal_stacks
= 1;
5379 case SWM_ARG_ID_MASTERSHRINK
:
5380 if (ws
->l_state
.horizontal_msize
> 1)
5381 ws
->l_state
.horizontal_msize
--;
5383 case SWM_ARG_ID_MASTERGROW
:
5384 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
5385 ws
->l_state
.horizontal_msize
++;
5387 case SWM_ARG_ID_MASTERADD
:
5388 ws
->l_state
.horizontal_mwin
++;
5390 case SWM_ARG_ID_MASTERDEL
:
5391 if (ws
->l_state
.horizontal_mwin
> 0)
5392 ws
->l_state
.horizontal_mwin
--;
5394 case SWM_ARG_ID_STACKBALANCE
:
5395 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ (ws
->l_state
.horizontal_stacks
+ 1);
5397 case SWM_ARG_ID_STACKINC
:
5398 ws
->l_state
.horizontal_stacks
++;
5400 case SWM_ARG_ID_STACKDEC
:
5401 if (ws
->l_state
.horizontal_stacks
> 1)
5402 ws
->l_state
.horizontal_stacks
--;
5404 case SWM_ARG_ID_FLIPLAYOUT
:
5405 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
5413 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5415 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
5417 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
5420 /* fullscreen view */
5422 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5424 struct swm_geometry gg
= *g
;
5425 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
, *tmpw
;
5428 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
5433 winno
= count_win(ws
, false);
5434 if (winno
== 0 && count_win(ws
, true) == 0)
5437 /* Figure out which top level window should be visible. */
5438 if (ws
->focus_pending
)
5439 win
= ws
->focus_pending
;
5442 else if (ws
->focus_prev
)
5443 win
= ws
->focus_prev
;
5445 win
= TAILQ_FIRST(&ws
->winlist
);
5447 DNPRINTF(SWM_D_STACK
, "max_stack: focus_pending: %#x, focus: %#x, "
5448 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws
->focus_pending
),
5449 WINID(ws
->focus
), WINID(ws
->focus_prev
),
5450 WINID(TAILQ_FIRST(&ws
->winlist
)), win
->id
);
5452 /* Update window geometry. */
5453 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5462 /* Set maximized flag for all maxed windows. */
5463 if (!MAXIMIZED(w
)) {
5464 /* Preserve floating geometry. */
5466 store_float_geom(w
);
5468 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_MAXIMIZED
);
5469 ewmh_update_wm_state(w
);
5472 /* Only reconfigure if necessary. */
5473 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
5474 HEIGHT(w
) != gg
.h
) {
5477 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
)) {
5478 w
->bordered
= false;
5479 WIDTH(w
) += 2 * border_width
;
5480 HEIGHT(w
) += 2 * border_width
;
5483 X(w
) += border_width
;
5484 Y(w
) += border_width
;
5491 /* If transient, stack parent and its children. */
5492 if (TRANS(win
) && (parent
= find_window(win
->transient
))) {
5493 raise_window(parent
);
5495 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5496 if (w
->transient
== parent
->id
)
5500 /* Make sure focus window is on top. */
5503 /* Stack any children of focus window. */
5504 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5505 if (w
->transient
== win
->id
)
5508 /* Map all windows. */
5509 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
5515 send_to_rg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5517 int ridx
= args
->id
, i
, num_screens
;
5518 struct swm_region
*rr
= NULL
;
5521 num_screens
= get_screen_count();
5522 /* do nothing if we don't have more than one screen */
5523 if (!(num_screens
> 1 || outputs
> 1))
5526 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
5528 rr
= TAILQ_FIRST(&r
->s
->rl
);
5529 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
5530 rr
= TAILQ_NEXT(rr
, entry
);
5537 send_to_ws(bp
, r
, &a
);
5541 region_under(struct swm_screen
*s
, int x
, int y
)
5543 struct swm_region
*r
= NULL
;
5545 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
5546 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
5547 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
5548 WIDTH(r
), HEIGHT(r
), x
, y
);
5549 if (X(r
) <= x
&& x
< MAX_X(r
))
5550 if (Y(r
) <= y
&& y
< MAX_Y(r
))
5557 /* Transfer focused window to target workspace and focus. */
5559 send_to_ws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5561 int wsid
= args
->id
;
5562 struct ws_win
*win
= NULL
;
5566 if (r
&& r
->ws
&& r
->ws
->focus
)
5571 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win %#x, ws %d\n", win
->id
, wsid
);
5573 if (wsid
< 0 || wsid
>= workspace_limit
)
5576 if (win
->ws
->idx
== wsid
)
5579 win_to_ws(win
, wsid
, true);
5581 /* Set new focus on target ws. */
5582 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5583 win
->ws
->focus_prev
= win
->ws
->focus
;
5584 win
->ws
->focus
= win
;
5585 win
->ws
->focus_pending
= NULL
;
5587 if (win
->ws
->focus_prev
)
5588 draw_frame(win
->ws
->focus_prev
);
5591 DNPRINTF(SWM_D_STACK
, "send_to_ws: focus_pending: %#x, focus: %#x, "
5592 "focus_prev: %#x, first: %#x, win: %#x\n",
5593 WINID(r
->ws
->focus_pending
), WINID(r
->ws
->focus
),
5594 WINID(r
->ws
->focus_prev
), WINID(TAILQ_FIRST(&r
->ws
->winlist
)),
5597 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5598 ewmh_update_wm_state(win
);
5600 /* Restack focused region. */
5603 /* If destination ws has a region, restack. */
5606 load_float_geom(win
);
5611 /* Set new focus on current ws. */
5612 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5613 if (r
->ws
->focus
!= NULL
) {
5614 focus_win(r
->ws
->focus
);
5616 DNPRINTF(SWM_D_FOCUS
, "send_to_ws: set_input_focus: "
5617 "%#x, revert-to: parent, time: 0\n", r
->id
);
5618 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
5619 r
->id
, XCB_CURRENT_TIME
);
5629 win_to_ws(struct ws_win
*win
, int wsid
, bool unfocus
)
5631 struct ws_win
*parent
;
5632 struct workspace
*ws
, *nws
, *pws
;
5634 if (wsid
< 0 || wsid
>= workspace_limit
)
5637 if (win
->ws
->idx
== wsid
)
5641 nws
= &win
->s
->ws
[wsid
];
5643 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win %#x, ws %d -> %d\n", win
->id
,
5646 /* Cleanup focus on source ws. */
5647 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
5648 (ws
->focus
== win
|| ws
->focus_pending
== win
))
5649 ws
->focus_pending
= get_focus_prev(win
);
5651 /* Move the parent if this is a transient window. */
5653 parent
= find_window(win
->transient
);
5656 /* Set new focus in parent's ws if needed. */
5657 if (pws
->focus
== parent
) {
5658 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5659 pws
->focus_pending
=
5660 get_focus_prev(parent
);
5662 unfocus_win(parent
);
5664 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5665 pws
->focus
= pws
->focus_pending
;
5666 pws
->focus_pending
= NULL
;
5670 /* Don't unmap parent if new ws is visible */
5672 unmap_window(parent
);
5675 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
5676 TAILQ_REMOVE(&ws
->stack
, parent
, stack_entry
);
5677 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
5678 TAILQ_INSERT_TAIL(&nws
->stack
, parent
, stack_entry
);
5681 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5682 "_NET_WM_DESKTOP: %d\n", wsid
);
5683 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5684 parent
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
5685 XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5692 if (ws
->focus_prev
== win
)
5693 ws
->focus_prev
= NULL
;
5695 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& ws
->focus_pending
!= NULL
) {
5696 ws
->focus
= ws
->focus_pending
;
5697 ws
->focus_pending
= NULL
;
5700 /* Don't unmap if new ws is visible */
5705 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
5706 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
5707 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
5708 TAILQ_INSERT_TAIL(&nws
->stack
, win
, stack_entry
);
5711 /* Update the window's workspace property: _NET_WM_DESKTOP */
5712 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5713 "_NET_WM_DESKTOP: %d\n", wsid
);
5714 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
5715 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5717 ewmh_update_client_list();
5719 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
5723 pressbutton(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5725 /* suppress unused warning since var is needed */
5729 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
5730 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5731 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
5732 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5736 raise_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5738 /* Suppress warning. */
5742 if (r
== NULL
|| r
->ws
== NULL
)
5745 if (r
->ws
->focus
&& MAXIMIZED(r
->ws
->focus
))
5748 r
->ws
->always_raise
= !r
->ws
->always_raise
;
5750 /* Update focused win stacking order based on new always_raise value. */
5751 raise_window(r
->ws
->focus
);
5757 iconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5761 /* Suppress warning. */
5765 if ((w
= r
->ws
->focus
) == NULL
)
5768 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_HIDDEN
);
5769 ewmh_update_wm_state(w
);
5777 get_win_name(xcb_window_t win
)
5780 xcb_get_property_cookie_t c
;
5781 xcb_get_property_reply_t
*r
;
5783 /* First try _NET_WM_NAME for UTF-8. */
5784 c
= xcb_get_property(conn
, 0, win
, ewmh
[_NET_WM_NAME
].atom
,
5785 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5786 r
= xcb_get_property_reply(conn
, c
, NULL
);
5787 if (r
&& r
->type
== XCB_NONE
) {
5789 /* Use WM_NAME instead; no UTF-8. */
5790 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
5791 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5792 r
= xcb_get_property_reply(conn
, c
, NULL
);
5795 if (r
&& r
->type
!= XCB_NONE
&& r
->length
> 0)
5796 name
= strndup(xcb_get_property_value(r
),
5797 xcb_get_property_value_length(r
));
5802 err(1, "get_win_name: failed to allocate memory.");
5810 uniconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5819 DNPRINTF(SWM_D_MISC
, "uniconify\n");
5821 if (r
== NULL
|| r
->ws
== NULL
)
5824 /* make sure we have anything to uniconify */
5825 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5826 if (win
->ws
== NULL
)
5827 continue; /* should never happen */
5833 DNPRINTF(SWM_D_MISC
, "uniconify: count: %d\n", count
);
5839 search_resp_action
= SWM_SEARCH_UNICONIFY
;
5841 spawn_select(r
, args
, "search", &searchpid
);
5843 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5846 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5847 if (win
->ws
== NULL
)
5848 continue; /* should never happen */
5852 name
= get_win_name(win
->id
);
5853 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
5861 name_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5867 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
5873 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
5875 spawn_select(r
, args
, "name_workspace", &searchpid
);
5877 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5880 fprintf(lfile
, "%s", "");
5885 search_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5888 struct workspace
*ws
;
5893 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
5899 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
5901 spawn_select(r
, args
, "search", &searchpid
);
5903 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5906 for (i
= 0; i
< workspace_limit
; i
++) {
5910 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
5911 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
5918 search_win_cleanup(void)
5920 struct search_window
*sw
= NULL
;
5922 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
5923 xcb_destroy_window(conn
, sw
->indicator
);
5924 TAILQ_REMOVE(&search_wl
, sw
, entry
);
5930 search_win(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5932 struct ws_win
*win
= NULL
;
5933 struct search_window
*sw
= NULL
;
5936 xcb_screen_t
*screen
;
5937 int i
, width
, height
;
5945 XRectangle l_ibox
, l_lbox
;
5949 DNPRINTF(SWM_D_MISC
, "search_win\n");
5952 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
5954 spawn_select(r
, args
, "search", &searchpid
);
5956 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5959 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
5960 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
5962 TAILQ_INIT(&search_wl
);
5965 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5969 sw
= calloc(1, sizeof(struct search_window
));
5971 warn("search_win: calloc");
5973 search_win_cleanup();
5979 snprintf(s
, sizeof s
, "%d", i
);
5982 w
= xcb_generate_id(conn
);
5983 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
5984 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
5985 wa
[2] = screen
->default_colormap
;
5987 if (bar_font_legacy
) {
5988 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
5989 width
= l_lbox
.width
+ 4;
5990 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
5992 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
5994 width
= info
.width
+ 4;
5995 height
= bar_font
->height
+ 4;
5998 xcb_create_window(conn
, screen
->root_depth
, w
, win
->frame
, 0, 0,
5999 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
6000 screen
->root_visual
, XCB_CW_BACK_PIXEL
|
6001 XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
, wa
);
6003 xcb_map_window(conn
, w
);
6006 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
6008 if (bar_font_legacy
) {
6009 l_gcv
.graphics_exposures
= 0;
6010 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
6012 XSetForeground(display
, l_draw
,
6013 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
6015 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
6016 (bar_fs_extents
->max_logical_extent
.height
-
6017 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
6019 XFreeGC(display
, l_draw
);
6022 draw
= XftDrawCreate(display
, w
,
6023 DefaultVisual(display
, r
->s
->idx
),
6024 DefaultColormap(display
, r
->s
->idx
));
6026 XftDrawStringUtf8(draw
, &search_font_color
, bar_font
, 2,
6027 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
6028 bar_font
->descent
, (FcChar8
*)s
, len
);
6030 XftDrawDestroy(draw
);
6033 DNPRINTF(SWM_D_MISC
, "search_win: mapped win %#x\n", w
);
6035 fprintf(lfile
, "%d\n", i
);
6045 search_resp_uniconify(const char *resp
, size_t len
)
6051 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
6053 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
6056 name
= get_win_name(win
->id
);
6057 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
6062 if (strncmp(s
, resp
, len
) == 0) {
6063 /* XXX this should be a callback to generalize */
6064 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_HIDDEN
);
6065 ewmh_update_wm_state(win
);
6075 search_resp_name_workspace(const char *resp
, size_t len
)
6077 struct workspace
*ws
;
6079 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
6081 if (search_r
->ws
== NULL
)
6086 free(search_r
->ws
->name
);
6087 search_r
->ws
->name
= NULL
;
6091 ws
->name
= strdup(resp
);
6092 if (ws
->name
== NULL
) {
6093 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
6094 "strdup: %s", strerror(errno
));
6098 ewmh_update_desktop_names();
6099 ewmh_get_desktop_names();
6104 ewmh_update_desktop_names(void)
6106 char *name_list
= NULL
, *p
;
6107 int num_screens
, i
, j
, len
= 0, tot
= 0;
6109 num_screens
= get_screen_count();
6110 for (i
= 0; i
< num_screens
; ++i
) {
6111 for (j
= 0; j
< workspace_limit
; ++j
) {
6112 if (screens
[i
].ws
[j
].name
!= NULL
)
6113 len
+= strlen(screens
[i
].ws
[j
].name
);
6117 if((name_list
= calloc(len
, sizeof(char))) == NULL
)
6118 err(1, "update_desktop_names: calloc: failed to "
6119 "allocate memory.");
6122 for (j
= 0; j
< workspace_limit
; ++j
) {
6123 if (screens
[i
].ws
[j
].name
!= NULL
) {
6124 len
= strlen(screens
[i
].ws
[j
].name
);
6125 memcpy(p
, screens
[i
].ws
[j
].name
, len
);
6134 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6135 screens
[i
].root
, ewmh
[_NET_DESKTOP_NAMES
].atom
,
6136 a_utf8_string
, 8, tot
, name_list
);
6146 ewmh_get_desktop_names(void)
6149 xcb_get_property_cookie_t c
;
6150 xcb_get_property_reply_t
*r
;
6151 int num_screens
, i
, j
, n
, k
;
6153 num_screens
= get_screen_count();
6154 for (i
= 0; i
< num_screens
; ++i
) {
6155 for (j
= 0; j
< workspace_limit
; ++j
) {
6156 free(screens
[i
].ws
[j
].name
);
6157 screens
[i
].ws
[j
].name
= NULL
;
6160 c
= xcb_get_property(conn
, 0, screens
[i
].root
,
6161 ewmh
[_NET_DESKTOP_NAMES
].atom
,
6162 a_utf8_string
, 0, UINT32_MAX
);
6163 r
= xcb_get_property_reply(conn
, c
, NULL
);
6167 names
= xcb_get_property_value(r
);
6168 n
= xcb_get_property_value_length(r
);
6170 for (j
= 0, k
= 0; j
< n
; ++j
) {
6171 if (*(names
+ j
) != '\0') {
6172 screens
[i
].ws
[k
].name
= strdup(names
+ j
);
6173 j
+= strlen(names
+ j
);
6182 ewmh_update_client_list(void)
6185 int num_screens
, i
, j
, k
= 0, count
= 0;
6188 num_screens
= get_screen_count();
6189 for (i
= 0; i
< num_screens
; ++i
) {
6190 for (j
= 0; j
< workspace_limit
; ++j
)
6191 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6194 DNPRINTF(SWM_D_PROP
, "ewmh_update_client_list: win count: %d\n",
6200 wins
= calloc(count
, sizeof(xcb_window_t
));
6202 err(1, "ewmh_update_client_list: calloc: failed to "
6203 "allocate memory.");
6205 for (j
= 0, k
= 0; j
< workspace_limit
; ++j
)
6206 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6207 wins
[k
++] = win
->id
;
6209 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6210 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
6211 XCB_ATOM_WINDOW
, 32, count
, wins
);
6218 ewmh_update_current_desktop(void)
6222 num_screens
= get_screen_count();
6223 for (i
= 0; i
< num_screens
; ++i
) {
6224 if (screens
[i
].r_focus
)
6225 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6226 screens
[i
].root
, ewmh
[_NET_CURRENT_DESKTOP
].atom
,
6227 XCB_ATOM_CARDINAL
, 32, 1,
6228 &screens
[i
].r_focus
->ws
->idx
);
6233 ewmh_update_desktops(void)
6235 int num_screens
, i
, j
;
6238 vals
= calloc(workspace_limit
* 2, sizeof(uint32_t));
6240 err(1, "ewmh_update_desktops: calloc: failed to allocate "
6243 num_screens
= get_screen_count();
6244 for (i
= 0; i
< num_screens
; i
++) {
6245 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6246 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
6247 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
6249 for (j
= 0; j
< workspace_limit
; ++j
) {
6250 if (screens
[i
].ws
[j
].r
!= NULL
) {
6251 vals
[j
* 2] = X(screens
[i
].ws
[j
].r
);
6252 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].r
);
6253 } else if (screens
[i
].ws
[j
].old_r
!= NULL
) {
6254 vals
[j
* 2] = X(screens
[i
].ws
[j
].old_r
);
6255 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].old_r
);
6257 vals
[j
* 2] = vals
[j
* 2 + 1] = 0;
6261 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6262 screens
[i
].root
, ewmh
[_NET_DESKTOP_VIEWPORT
].atom
,
6263 XCB_ATOM_CARDINAL
, 32, workspace_limit
* 2, vals
);
6270 search_resp_search_workspace(const char *resp
)
6277 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
6281 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
6288 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
6290 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
6297 switchws(NULL
, search_r
, &a
);
6301 search_resp_search_window(const char *resp
)
6306 struct search_window
*sw
;
6308 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
6312 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
6317 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
6319 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
6326 TAILQ_FOREACH(sw
, &search_wl
, entry
)
6327 if (idx
== sw
->idx
) {
6333 #define MAX_RESP_LEN 1024
6336 search_do_resp(void)
6342 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
6347 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
6348 warn("search: calloc");
6352 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
6354 warn("search: read error");
6357 resp
[rbytes
] = '\0';
6360 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
6361 * newline, so work around that by sanitizing the resp now.
6363 resp
[strcspn(resp
, "\n")] = '\0';
6366 switch (search_resp_action
) {
6367 case SWM_SEARCH_UNICONIFY
:
6368 search_resp_uniconify(resp
, len
);
6370 case SWM_SEARCH_NAME_WORKSPACE
:
6371 search_resp_name_workspace(resp
, len
);
6373 case SWM_SEARCH_SEARCH_WORKSPACE
:
6374 search_resp_search_workspace(resp
);
6376 case SWM_SEARCH_SEARCH_WINDOW
:
6377 search_resp_search_window(resp
);
6382 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
6383 search_win_cleanup();
6385 search_resp_action
= SWM_SEARCH_NONE
;
6386 close(select_resp_pipe
[0]);
6393 wkill(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6397 DNPRINTF(SWM_D_MISC
, "wkill: win %#x, id: %d\n", WINID(r
->ws
->focus
),
6400 if (r
->ws
->focus
== NULL
)
6403 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
6404 xcb_kill_client(conn
, r
->ws
->focus
->id
);
6406 if (r
->ws
->focus
->can_delete
)
6407 client_msg(r
->ws
->focus
, a_delete
, 0);
6413 clear_maximized(struct workspace
*ws
)
6418 /* Clear any maximized win(s) on ws, from bottom up. */
6419 TAILQ_FOREACH_REVERSE(w
, &ws
->stack
, ws_win_stack
, stack_entry
)
6421 ewmh_apply_flags(w
, w
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
6422 ewmh_update_wm_state(w
);
6430 maximize_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6432 struct ws_win
*w
= r
->ws
->focus
;
6434 /* suppress unused warning since var is needed */
6441 DNPRINTF(SWM_D_MISC
, "maximize_toggle: win %#x\n", w
->id
);
6446 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6449 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_MAXIMIZED
);
6450 ewmh_update_wm_state(w
);
6454 if (w
== w
->ws
->focus
)
6459 DNPRINTF(SWM_D_MISC
, "maximize_toggle: done\n");
6463 floating_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6465 struct ws_win
*w
= r
->ws
->focus
;
6467 /* suppress unused warning since var is needed */
6474 DNPRINTF(SWM_D_MISC
, "floating_toggle: win %#x\n", w
->id
);
6476 if (FULLSCREEN(w
) || TRANS(w
))
6479 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6482 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_ABOVE
);
6483 ewmh_update_wm_state(w
);
6487 if (w
== w
->ws
->focus
)
6492 DNPRINTF(SWM_D_MISC
, "floating_toggle: done\n");
6496 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
6498 struct swm_geometry g
= r
->g
;
6499 int rt
, lt
, tp
, bm
, bw
;
6501 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
6504 * Perpendicular distance of each side of the window to the respective
6505 * side of the region boundary. Positive values indicate the side of
6506 * the window has passed beyond the region boundary.
6508 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) - MAX_X(r
) : bw
;
6509 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) : bw
;
6510 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) - MAX_Y(r
) : bw
;
6511 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) : bw
;
6513 DNPRINTF(SWM_D_MISC
, "region_containment: win %#x, rt: %d, lt: %d, "
6514 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
6515 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
6516 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
6519 * Disable containment if any of the flagged sides went beyond the
6520 * containment boundary, or if containment is disabled.
6522 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
6523 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
6524 /* Make sure window has at least 1 pixel in the region */
6525 g
.x
+= 1 - WIDTH(win
);
6526 g
.y
+= 1 - HEIGHT(win
);
6527 g
.w
+= 2 * WIDTH(win
) - 2;
6528 g
.h
+= 2 * HEIGHT(win
) - 2;
6531 constrain_window(win
, &g
, &opts
);
6534 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6536 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
6538 DNPRINTF(SWM_D_MISC
, "constrain_window: win %#x, (x,y) w x h: "
6539 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6540 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
6541 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
6542 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
6543 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
6544 YESNO(*opts
& SWM_CW_RESIZABLE
));
6546 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) > b
->x
+ b
->w
) {
6547 if (*opts
& SWM_CW_RESIZABLE
)
6548 WIDTH(win
) = b
->x
+ b
->w
- X(win
);
6550 X(win
) = b
->x
+ b
->w
- WIDTH(win
);
6553 if ((*opts
& SWM_CW_LEFT
) && X(win
) < b
->x
) {
6554 if (*opts
& SWM_CW_RESIZABLE
)
6555 WIDTH(win
) -= b
->x
- X(win
);
6560 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) > b
->y
+ b
->h
) {
6561 if (*opts
& SWM_CW_RESIZABLE
)
6562 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
);
6564 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
);
6567 if ((*opts
& SWM_CW_TOP
) && Y(win
) < b
->y
) {
6568 if (*opts
& SWM_CW_RESIZABLE
)
6569 HEIGHT(win
) -= b
->y
- Y(win
);
6574 if (*opts
& SWM_CW_RESIZABLE
) {
6577 if (HEIGHT(win
) < 1)
6583 draw_frame(struct ws_win
*win
)
6585 xcb_rectangle_t rect
[4];
6589 if (win
->frame
== XCB_WINDOW_NONE
) {
6590 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x not "
6591 "reparented.\n", win
->id
);
6595 if (!win
->bordered
) {
6596 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x frame "
6597 "disabled\n", win
->id
);
6600 if (WS_FOCUSED(win
->ws
) && win
->ws
->focus
== win
)
6601 pixel
= MAXIMIZED(win
) ?
6602 &win
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].pixel
:
6603 &win
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6605 pixel
= MAXIMIZED(win
) ?
6606 &win
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].pixel
:
6607 &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6609 /* Top (with corners) */
6612 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6613 rect
[n
].height
= border_width
;
6614 /* Left (without corners) */
6616 rect
[n
].y
= border_width
;
6617 rect
[n
].width
= border_width
;
6618 rect
[n
].height
= HEIGHT(win
);
6619 /* Right (without corners) */
6620 rect
[++n
].x
= border_width
+ WIDTH(win
);
6621 rect
[n
].y
= border_width
;
6622 rect
[n
].width
= border_width
;
6623 rect
[n
].height
= HEIGHT(win
);
6624 /* Bottom (with corners)*/
6626 rect
[n
].y
= border_width
+ HEIGHT(win
);
6627 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6628 rect
[n
].height
= border_width
;
6630 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, pixel
);
6631 xcb_poly_fill_rectangle(conn
, win
->frame
, win
->s
->bar_gc
, 4, rect
);
6635 update_window(struct ws_win
*win
)
6640 if (win
->frame
== XCB_WINDOW_NONE
) {
6641 DNPRINTF(SWM_D_EVENT
, "update_window: skip win %#x; "
6642 "not reparented.\n", win
->id
);
6646 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
6647 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
6648 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6650 /* Reconfigure frame. */
6651 if (win
->bordered
) {
6652 wc
[0] = X(win
) - border_width
;
6653 wc
[1] = Y(win
) - border_width
;
6654 wc
[2] = WIDTH(win
) + 2 * border_width
;
6655 wc
[3] = HEIGHT(win
) + 2 * border_width
;
6660 wc
[3] = HEIGHT(win
);
6665 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x frame %#x, (x,y) w x h: "
6666 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, win
->frame
, wc
[0],
6667 wc
[1], wc
[2], wc
[3], YESNO(win
->bordered
));
6669 xcb_configure_window(conn
, win
->frame
, mask
, wc
);
6671 /* Reconfigure client window. */
6672 wc
[0] = wc
[1] = win
->bordered
? border_width
: 0;
6674 wc
[3] = HEIGHT(win
);
6676 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x, (x,y) w x h: "
6677 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
6678 wc
[3], YESNO(win
->bordered
));
6679 xcb_configure_window(conn
, win
->id
, mask
, wc
);
6681 /* ICCCM 4.2.3 Synthetic ConfigureNotify when moved and not resized. */
6682 if ((X(win
) != win
->g_prev
.x
|| Y(win
) != win
->g_prev
.y
) &&
6683 (win
->java
|| (WIDTH(win
) == win
->g_prev
.w
&&
6684 HEIGHT(win
) == win
->g_prev
.h
))) {
6685 /* Java has special needs when moved together with a resize. */
6686 config_win(win
, NULL
);
6689 win
->g_prev
= win
->g
;
6693 SIMPLEQ_ENTRY(event
) entry
;
6694 xcb_generic_event_t
*ev
;
6696 SIMPLEQ_HEAD(event_queue
, event
) events
= SIMPLEQ_HEAD_INITIALIZER(events
);
6698 xcb_generic_event_t
*
6699 get_next_event(bool dowait
)
6702 xcb_generic_event_t
*evt
;
6704 /* Try queue first. */
6705 if ((ep
= SIMPLEQ_FIRST(&events
))) {
6707 SIMPLEQ_REMOVE_HEAD(&events
, entry
);
6710 evt
= xcb_wait_for_event(conn
);
6712 evt
= xcb_poll_for_event(conn
);
6718 put_back_event(xcb_generic_event_t
*evt
)
6721 if ((ep
= malloc(sizeof (struct event
))) == NULL
)
6722 err(1, "put_back_event: failed to allocate memory.");
6724 SIMPLEQ_INSERT_HEAD(&events
, ep
, entry
);
6727 /* Peeks at next event to detect auto-repeat. */
6729 keyrepeating(xcb_key_release_event_t
*kre
)
6731 xcb_generic_event_t
*evt
;
6733 /* Ensure repeating keypress is finished processing. */
6736 if ((evt
= get_next_event(false))) {
6737 put_back_event(evt
);
6739 if (XCB_EVENT_RESPONSE_TYPE(evt
) == XCB_KEY_PRESS
&&
6740 kre
->sequence
== evt
->sequence
&&
6741 kre
->detail
== ((xcb_key_press_event_t
*)evt
)->detail
)
6749 keybindreleased(struct binding
*bp
, xcb_key_release_event_t
*kre
)
6751 if (bp
->type
== KEYBIND
&& !keyrepeating(kre
) &&
6752 bp
->value
== xcb_key_press_lookup_keysym(syms
, kre
, 0))
6758 #define SWM_RESIZE_STEPS (50)
6761 resize_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
6763 xcb_timestamp_t timestamp
= 0;
6764 struct swm_region
*r
= NULL
;
6765 struct swm_geometry g
;
6766 int top
= 0, left
= 0;
6768 xcb_cursor_t cursor
;
6769 xcb_query_pointer_reply_t
*xpr
= NULL
;
6770 xcb_generic_event_t
*evt
;
6771 xcb_motion_notify_event_t
*mne
;
6772 bool resizing
, step
= false;
6778 if (FULLSCREEN(win
))
6781 /* In max_stack mode, should only resize transients. */
6782 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6785 DNPRINTF(SWM_D_EVENT
, "resize: win %#x, floating: %s, "
6786 "transient: %#x\n", win
->id
, YESNO(ABOVE(win
)),
6790 store_float_geom(win
);
6791 else if (!(TRANS(win
) || ABOVE(win
)))
6794 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6796 ewmh_update_wm_state(win
);
6802 /* It's possible for win to have been freed during focus_flush(). */
6803 if (validate_win(win
)) {
6804 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6809 case SWM_ARG_ID_WIDTHSHRINK
:
6810 WIDTH(win
) -= SWM_RESIZE_STEPS
;
6813 case SWM_ARG_ID_WIDTHGROW
:
6814 WIDTH(win
) += SWM_RESIZE_STEPS
;
6817 case SWM_ARG_ID_HEIGHTSHRINK
:
6818 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
6821 case SWM_ARG_ID_HEIGHTGROW
:
6822 HEIGHT(win
) += SWM_RESIZE_STEPS
;
6829 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6830 SWM_CW_HARDBOUNDARY
);
6832 store_float_geom(win
);
6836 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6837 SWM_CW_SOFTBOUNDARY
);
6840 /* get cursor offset from window root */
6841 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6848 if (xpr
->win_x
< WIDTH(win
) / 2)
6851 if (xpr
->win_y
< HEIGHT(win
) / 2)
6854 if (opt
== SWM_ARG_ID_CENTER
)
6855 cursor
= cursors
[XC_SIZING
].cid
;
6857 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
6858 XC_TOP_RIGHT_CORNER
].cid
;
6860 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
6861 XC_BOTTOM_RIGHT_CORNER
].cid
;
6863 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6864 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
6867 /* Release keyboard freeze if called via keybind. */
6868 if (bp
->type
== KEYBIND
)
6869 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6874 while (resizing
&& (evt
= get_next_event(true))) {
6875 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6876 case XCB_BUTTON_RELEASE
:
6877 if (bp
->type
== BTNBIND
&& bp
->value
==
6878 ((xcb_button_release_event_t
*)evt
)->detail
)
6881 case XCB_KEY_RELEASE
:
6882 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
6885 case XCB_MOTION_NOTIFY
:
6886 mne
= (xcb_motion_notify_event_t
*)evt
;
6887 DNPRINTF(SWM_D_EVENT
, "resize: MOTION_NOTIFY: "
6888 "root: %#x\n", mne
->root
);
6890 /* cursor offset/delta from start of the operation */
6891 dx
= mne
->root_x
- xpr
->root_x
;
6892 dy
= mne
->root_y
- xpr
->root_y
;
6898 if (opt
== SWM_ARG_ID_CENTER
) {
6899 if (g
.h
/ 2 + dy
< 1)
6903 HEIGHT(win
) = g
.h
+ 2 * dy
;
6911 HEIGHT(win
) = g
.h
+ dy
;
6918 if (opt
== SWM_ARG_ID_CENTER
) {
6919 if (g
.w
/ 2 + dx
< 1)
6923 WIDTH(win
) = g
.w
+ 2 * dx
;
6931 WIDTH(win
) = g
.w
+ dx
;
6934 /* not free, don't sync more than 120 times / second */
6935 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6936 timestamp
= mne
->time
;
6937 regionize(win
, mne
->root_x
, mne
->root_y
);
6938 region_containment(win
, r
, SWM_CW_ALLSIDES
|
6939 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
6940 SWM_CW_SOFTBOUNDARY
);
6945 case XCB_BUTTON_PRESS
:
6947 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_PRESS ignored\n");
6948 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
6949 ((xcb_button_press_event_t
*)evt
)->time
);
6954 DNPRINTF(SWM_D_EVENT
, "resize: KEY_PRESS ignored\n");
6955 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6956 ((xcb_key_press_event_t
*)evt
)->time
);
6962 /* It's possible for win to have been freed above. */
6963 if (validate_win(win
)) {
6964 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6972 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6973 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
6977 store_float_geom(win
);
6979 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6981 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
6985 resize(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6987 struct ws_win
*win
= NULL
;
6988 xcb_query_pointer_reply_t
*qpr
= NULL
;
6993 if (args
->id
!= SWM_ARG_ID_DONTCENTER
&&
6994 args
->id
!= SWM_ARG_ID_CENTER
) {
6995 /* {height,width}_{grow,shrink} use the focus window. */
6999 /* resize uses window under pointer. */
7000 qpr
= xcb_query_pointer_reply(conn
,
7001 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7003 win
= find_window(qpr
->child
);
7009 resize_win(win
, bp
, args
->id
);
7011 if (args
->id
&& bp
->type
== KEYBIND
)
7017 /* Try to set window region based on supplied coordinates or window center. */
7019 regionize(struct ws_win
*win
, int x
, int y
)
7021 struct swm_region
*r
= NULL
;
7023 r
= region_under(win
->s
, x
, y
);
7025 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
7026 Y(win
) + HEIGHT(win
) / 2);
7028 if (r
&& r
!= win
->ws
->r
) {
7029 clear_maximized(r
->ws
);
7031 win_to_ws(win
, r
->ws
->idx
, false);
7033 /* Stack old and new region. */
7037 /* Set focus on new ws. */
7038 unfocus_win(r
->ws
->focus
);
7046 #define SWM_MOVE_STEPS (50)
7049 move_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
7051 struct swm_region
*r
;
7052 xcb_timestamp_t timestamp
= 0;
7053 xcb_query_pointer_reply_t
*qpr
= NULL
;
7054 xcb_generic_event_t
*evt
;
7055 xcb_motion_notify_event_t
*mne
;
7056 bool moving
, restack
= false, step
= false;
7061 if ((r
= win
->ws
->r
) == NULL
)
7064 if (FULLSCREEN(win
))
7067 DNPRINTF(SWM_D_EVENT
, "move: win %#x, floating: %s, transient: "
7068 "%#x\n", win
->id
, YESNO(ABOVE(win
)), win
->transient
);
7070 /* in max_stack mode should only move transients */
7071 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
7074 if (!(ABOVE(win
) || TRANS(win
)) || MAXIMIZED(win
)) {
7075 store_float_geom(win
);
7079 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
7081 ewmh_update_wm_state(win
);
7088 /* It's possible for win to have been freed during focus_flush(). */
7089 if (validate_win(win
)) {
7090 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7095 case SWM_ARG_ID_MOVELEFT
:
7096 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
7099 case SWM_ARG_ID_MOVERIGHT
:
7100 X(win
) += (SWM_MOVE_STEPS
- border_width
);
7103 case SWM_ARG_ID_MOVEUP
:
7104 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
7107 case SWM_ARG_ID_MOVEDOWN
:
7108 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
7115 regionize(win
, -1, -1);
7116 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
7118 store_float_geom(win
);
7122 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
7123 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
7124 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
7126 /* get cursor offset from window root */
7127 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
7130 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7134 /* Release keyboard freeze if called via keybind. */
7135 if (bp
->type
== KEYBIND
)
7136 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7139 regionize(win
, qpr
->root_x
, qpr
->root_y
);
7140 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7141 SWM_CW_SOFTBOUNDARY
);
7145 while (moving
&& (evt
= get_next_event(true))) {
7146 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
7147 case XCB_BUTTON_RELEASE
:
7148 if (bp
->type
== BTNBIND
&& bp
->value
==
7149 ((xcb_button_release_event_t
*)evt
)->detail
)
7152 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7153 ((xcb_button_release_event_t
*)evt
)->time
);
7156 case XCB_KEY_RELEASE
:
7157 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
7160 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7161 ((xcb_key_release_event_t
*)evt
)->time
);
7164 case XCB_MOTION_NOTIFY
:
7165 mne
= (xcb_motion_notify_event_t
*)evt
;
7166 DNPRINTF(SWM_D_EVENT
, "move: MOTION_NOTIFY: "
7167 "root: %#x\n", mne
->root
);
7168 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
7169 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
7171 /* not free, don't sync more than 120 times / second */
7172 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
7173 timestamp
= mne
->time
;
7174 regionize(win
, mne
->root_x
, mne
->root_y
);
7175 region_containment(win
, win
->ws
->r
,
7176 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
7181 case XCB_BUTTON_PRESS
:
7182 /* Thaw and ignore. */
7183 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7184 ((xcb_button_press_event_t
*)evt
)->time
);
7189 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7190 ((xcb_key_press_event_t
*)evt
)->time
);
7196 /* It's possible for win to have been freed. */
7197 if (validate_win(win
)) {
7198 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7206 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7207 SWM_CW_SOFTBOUNDARY
);
7211 store_float_geom(win
);
7213 /* New region set to fullscreen layout. */
7214 if (win
->ws
->r
&& win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
]) {
7221 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7222 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
7226 move(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
7228 struct ws_win
*win
= NULL
;
7229 xcb_query_pointer_reply_t
*qpr
= NULL
;
7235 /* move_* uses focus window. */
7239 /* Disallow move_ on tiled. */
7240 if (win
&& !(TRANS(win
) || ABOVE(win
)))
7243 /* move uses window under pointer. */
7244 qpr
= xcb_query_pointer_reply(conn
,
7245 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7247 win
= find_window(qpr
->child
);
7253 move_win(win
, bp
, args
->id
);
7255 if (args
->id
&& bp
->type
== KEYBIND
)
7261 /* action definitions */
7263 char name
[SWM_FUNCNAME_LEN
];
7264 void (*func
)(struct binding
*, struct swm_region
*,
7268 } actions
[FN_INVALID
+ 2] = {
7269 /* name function argument */
7270 { "bar_toggle", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
7271 { "bar_toggle_ws", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
7272 { "button2", pressbutton
, 0, {.id
= 2} },
7273 { "cycle_layout", cycle_layout
, 0, {0} },
7274 { "flip_layout", stack_config
, 0, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
7275 { "float_toggle", floating_toggle
,0, {0} },
7276 { "focus", focus_pointer
, 0, {0} },
7277 { "focus_main", focus
, 0, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
7278 { "focus_next", focus
, 0, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
7279 { "focus_prev", focus
, 0, {.id
= SWM_ARG_ID_FOCUSPREV
} },
7280 { "focus_urgent", focus
, 0, {.id
= SWM_ARG_ID_FOCUSURGENT
} },
7281 { "maximize_toggle", maximize_toggle
,0, {0} },
7282 { "height_grow", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
7283 { "height_shrink", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
7284 { "iconify", iconify
, 0, {0} },
7285 { "master_shrink", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
7286 { "master_grow", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERGROW
} },
7287 { "master_add", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERADD
} },
7288 { "master_del", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERDEL
} },
7289 { "move", move
, FN_F_NOREPLAY
, {0} },
7290 { "move_down", move
, 0, {.id
= SWM_ARG_ID_MOVEDOWN
} },
7291 { "move_left", move
, 0, {.id
= SWM_ARG_ID_MOVELEFT
} },
7292 { "move_right", move
, 0, {.id
= SWM_ARG_ID_MOVERIGHT
} },
7293 { "move_up", move
, 0, {.id
= SWM_ARG_ID_MOVEUP
} },
7294 { "mvrg_1", send_to_rg
, 0, {.id
= 0} },
7295 { "mvrg_2", send_to_rg
, 0, {.id
= 1} },
7296 { "mvrg_3", send_to_rg
, 0, {.id
= 2} },
7297 { "mvrg_4", send_to_rg
, 0, {.id
= 3} },
7298 { "mvrg_5", send_to_rg
, 0, {.id
= 4} },
7299 { "mvrg_6", send_to_rg
, 0, {.id
= 5} },
7300 { "mvrg_7", send_to_rg
, 0, {.id
= 6} },
7301 { "mvrg_8", send_to_rg
, 0, {.id
= 7} },
7302 { "mvrg_9", send_to_rg
, 0, {.id
= 8} },
7303 { "mvws_1", send_to_ws
, 0, {.id
= 0} },
7304 { "mvws_2", send_to_ws
, 0, {.id
= 1} },
7305 { "mvws_3", send_to_ws
, 0, {.id
= 2} },
7306 { "mvws_4", send_to_ws
, 0, {.id
= 3} },
7307 { "mvws_5", send_to_ws
, 0, {.id
= 4} },
7308 { "mvws_6", send_to_ws
, 0, {.id
= 5} },
7309 { "mvws_7", send_to_ws
, 0, {.id
= 6} },
7310 { "mvws_8", send_to_ws
, 0, {.id
= 7} },
7311 { "mvws_9", send_to_ws
, 0, {.id
= 8} },
7312 { "mvws_10", send_to_ws
, 0, {.id
= 9} },
7313 { "mvws_11", send_to_ws
, 0, {.id
= 10} },
7314 { "mvws_12", send_to_ws
, 0, {.id
= 11} },
7315 { "mvws_13", send_to_ws
, 0, {.id
= 12} },
7316 { "mvws_14", send_to_ws
, 0, {.id
= 13} },
7317 { "mvws_15", send_to_ws
, 0, {.id
= 14} },
7318 { "mvws_16", send_to_ws
, 0, {.id
= 15} },
7319 { "mvws_17", send_to_ws
, 0, {.id
= 16} },
7320 { "mvws_18", send_to_ws
, 0, {.id
= 17} },
7321 { "mvws_19", send_to_ws
, 0, {.id
= 18} },
7322 { "mvws_20", send_to_ws
, 0, {.id
= 19} },
7323 { "mvws_21", send_to_ws
, 0, {.id
= 20} },
7324 { "mvws_22", send_to_ws
, 0, {.id
= 21} },
7325 { "name_workspace", name_workspace
, 0, {0} },
7326 { "quit", quit
, 0, {0} },
7327 { "raise_toggle", raise_toggle
, 0, {0} },
7328 { "resize", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_DONTCENTER
} },
7329 { "resize_centered", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_CENTER
} },
7330 { "restart", restart
, 0, {0} },
7331 { "rg_1", focusrg
, 0, {.id
= 0} },
7332 { "rg_2", focusrg
, 0, {.id
= 1} },
7333 { "rg_3", focusrg
, 0, {.id
= 2} },
7334 { "rg_4", focusrg
, 0, {.id
= 3} },
7335 { "rg_5", focusrg
, 0, {.id
= 4} },
7336 { "rg_6", focusrg
, 0, {.id
= 5} },
7337 { "rg_7", focusrg
, 0, {.id
= 6} },
7338 { "rg_8", focusrg
, 0, {.id
= 7} },
7339 { "rg_9", focusrg
, 0, {.id
= 8} },
7340 { "rg_move_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_UP
} },
7341 { "rg_move_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_DOWN
} },
7342 { "rg_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7343 { "rg_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7344 { "screen_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7345 { "screen_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7346 { "search_win", search_win
, 0, {0} },
7347 { "search_workspace", search_workspace
, 0, {0} },
7348 { "spawn_custom", NULL
, 0, {0} },
7349 { "stack_balance", stack_config
, 0, {.id
= SWM_ARG_ID_STACKBALANCE
} },
7350 { "stack_inc", stack_config
, 0, {.id
= SWM_ARG_ID_STACKINC
} },
7351 { "stack_dec", stack_config
, 0, {.id
= SWM_ARG_ID_STACKDEC
} },
7352 { "stack_reset", stack_config
, 0, {.id
= SWM_ARG_ID_STACKRESET
} },
7353 { "swap_main", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPMAIN
} },
7354 { "swap_next", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPNEXT
} },
7355 { "swap_prev", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPPREV
} },
7356 { "uniconify", uniconify
, 0, {0} },
7357 { "version", version
, 0, {0} },
7358 { "width_grow", resize
, 0, {.id
= SWM_ARG_ID_WIDTHGROW
} },
7359 { "width_shrink", resize
, 0, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
7360 { "wind_del", wkill
, 0, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
7361 { "wind_kill", wkill
, 0, {.id
= SWM_ARG_ID_KILLWINDOW
} },
7362 { "ws_1", switchws
, 0, {.id
= 0} },
7363 { "ws_2", switchws
, 0, {.id
= 1} },
7364 { "ws_3", switchws
, 0, {.id
= 2} },
7365 { "ws_4", switchws
, 0, {.id
= 3} },
7366 { "ws_5", switchws
, 0, {.id
= 4} },
7367 { "ws_6", switchws
, 0, {.id
= 5} },
7368 { "ws_7", switchws
, 0, {.id
= 6} },
7369 { "ws_8", switchws
, 0, {.id
= 7} },
7370 { "ws_9", switchws
, 0, {.id
= 8} },
7371 { "ws_10", switchws
, 0, {.id
= 9} },
7372 { "ws_11", switchws
, 0, {.id
= 10} },
7373 { "ws_12", switchws
, 0, {.id
= 11} },
7374 { "ws_13", switchws
, 0, {.id
= 12} },
7375 { "ws_14", switchws
, 0, {.id
= 13} },
7376 { "ws_15", switchws
, 0, {.id
= 14} },
7377 { "ws_16", switchws
, 0, {.id
= 15} },
7378 { "ws_17", switchws
, 0, {.id
= 16} },
7379 { "ws_18", switchws
, 0, {.id
= 17} },
7380 { "ws_19", switchws
, 0, {.id
= 18} },
7381 { "ws_20", switchws
, 0, {.id
= 19} },
7382 { "ws_21", switchws
, 0, {.id
= 20} },
7383 { "ws_22", switchws
, 0, {.id
= 21} },
7384 { "ws_next", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
7385 { "ws_next_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
7386 { "ws_next_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
7387 { "ws_prev", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
7388 { "ws_prev_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
7389 { "ws_prev_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
7390 { "ws_prior", priorws
, 0, {0} },
7391 /* SWM_DEBUG actions MUST be here: */
7392 { "debug_toggle", debug_toggle
, 0, {0} },
7393 { "dumpwins", dumpwins
, 0, {0} },
7395 { "invalid action", NULL
, 0, {0} },
7399 update_modkey(uint16_t mod
)
7403 /* Replace all instances of the old mod key. */
7404 RB_FOREACH(bp
, binding_tree
, &bindings
)
7405 if (bp
->mod
& mod_key
)
7406 bp
->mod
= (bp
->mod
& ~mod_key
) | mod
;
7411 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7414 struct spawn_prog
*prog
= NULL
;
7416 char *ap
, **real_args
;
7418 /* suppress unused warning since var is needed */
7421 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
7424 TAILQ_FOREACH(prog
, &spawns
, entry
) {
7425 if (strcasecmp(spawn_name
, prog
->name
) == 0)
7429 warnx("spawn_custom: program %s not found", spawn_name
);
7433 /* make room for expanded args */
7434 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
7435 err(1, "spawn_custom: calloc real_args");
7437 /* expand spawn_args into real_args */
7438 for (i
= c
= 0; i
< prog
->argc
; i
++) {
7440 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
7441 if (strcasecmp(ap
, "$bar_border") == 0) {
7443 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
7445 err(1, "spawn_custom border color");
7446 } else if (strcasecmp(ap
, "$bar_color") == 0) {
7448 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
7450 err(1, "spawn_custom bar color");
7451 } else if (strcasecmp(ap
, "$bar_font") == 0) {
7452 if ((real_args
[c
] = strdup(bar_fonts
))
7454 err(1, "spawn_custom bar fonts");
7455 } else if (strcasecmp(ap
, "$bar_font_color") == 0) {
7457 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
7459 err(1, "spawn_custom color font");
7460 } else if (strcasecmp(ap
, "$color_focus") == 0) {
7462 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
7464 err(1, "spawn_custom color focus");
7465 } else if (strcasecmp(ap
, "$color_focus_maximized") == 0) {
7467 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].name
))
7469 err(1, "spawn_custom color focus maximized");
7470 } else if (strcasecmp(ap
, "$color_unfocus") == 0) {
7472 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
7474 err(1, "spawn_custom color unfocus");
7475 } else if (strcasecmp(ap
, "$color_unfocus_maximized") == 0) {
7477 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].name
))
7479 err(1, "spawn_custom color unfocus maximized");
7480 } else if (strcasecmp(ap
, "$region_index") == 0) {
7481 if (asprintf(&real_args
[c
], "%d",
7482 get_region_index(r
) + 1) < 1)
7483 err(1, "spawn_custom region index");
7484 } else if (strcasecmp(ap
, "$workspace_index") == 0) {
7485 if (asprintf(&real_args
[c
], "%d", r
->ws
->idx
+ 1) < 1)
7486 err(1, "spawn_custom workspace index");
7487 } else if (strcasecmp(ap
, "$dmenu_bottom") == 0) {
7490 if ((real_args
[c
] = strdup("-b")) == NULL
)
7491 err(1, "spawn_custom workspace index");
7493 /* no match --> copy as is */
7494 if ((real_args
[c
] = strdup(ap
)) == NULL
)
7495 err(1, "spawn_custom strdup(ap)");
7497 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
7503 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
7504 for (i
= 0; i
< c
; ++i
)
7505 DPRINTF("\"%s\" ", real_args
[i
]);
7508 *ret_args
= real_args
;
7513 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
7519 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7523 spawn(r
->ws
->idx
, &a
, true);
7525 for (i
= 0; i
< spawn_argc
; i
++)
7531 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7538 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7542 if (pipe(select_list_pipe
) == -1)
7543 err(1, "pipe error");
7544 if (pipe(select_resp_pipe
) == -1)
7545 err(1, "pipe error");
7547 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
7548 err(1, "could not disable SIGPIPE");
7549 switch (*pid
= fork()) {
7551 err(1, "cannot fork");
7554 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
7556 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
7558 close(select_list_pipe
[1]);
7559 close(select_resp_pipe
[0]);
7560 spawn(r
->ws
->idx
, &a
, false);
7562 default: /* parent */
7563 close(select_list_pipe
[0]);
7564 close(select_resp_pipe
[1]);
7568 for (i
= 0; i
< spawn_argc
; i
++)
7573 /* Argument tokenizer. */
7576 char *arg
, *cp
, *next
;
7577 bool single_quoted
= false, double_quoted
= false;
7582 /* Eat and move characters until end of argument is found. */
7583 for (arg
= next
= cp
= *sp
; *cp
!= '\0'; ++cp
) {
7584 if (!double_quoted
&& *cp
== '\'') {
7585 /* Eat single-quote. */
7586 single_quoted
= !single_quoted
;
7587 } else if (!single_quoted
&& *cp
== '"') {
7588 /* Eat double-quote. */
7589 double_quoted
= !double_quoted
;
7590 } else if (!single_quoted
&& *cp
== '\\' && *(cp
+ 1) == '"') {
7591 /* Eat backslash; copy escaped character to arg. */
7593 } else if (!single_quoted
&& !double_quoted
&& *cp
== '\\' &&
7594 (*(cp
+ 1) == '\'' || *(cp
+ 1) == ' ')) {
7595 /* Eat backslash; move escaped character. */
7597 } else if (!single_quoted
&& !double_quoted
&&
7598 (*cp
== ' ' || *cp
== '\t')) {
7599 /* Terminate argument. */
7601 /* Point sp to beginning of next argument. */
7605 /* Move regular character. */
7610 /* Terminate argument if end of string. */
7620 spawn_insert(const char *name
, const char *args
, int flags
)
7622 struct spawn_prog
*sp
;
7623 char *arg
, *cp
, *ptr
;
7625 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s[%s]\n", name
, args
);
7627 if (args
== NULL
|| *args
== '\0')
7630 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
7631 err(1, "spawn_insert: calloc");
7632 if ((sp
->name
= strdup(name
)) == NULL
)
7633 err(1, "spawn_insert: strdup");
7635 /* Convert the arguments to an argument list. */
7636 if ((ptr
= cp
= strdup(args
)) == NULL
)
7637 err(1, "spawn_insert: strdup");
7638 while ((arg
= argsep(&ptr
)) != NULL
) {
7639 /* Null argument; skip it. */
7644 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
7645 sizeof *sp
->argv
)) == NULL
)
7646 err(1, "spawn_insert: realloc");
7647 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
7648 err(1, "spawn_insert: strdup");
7654 DNPRINTF(SWM_D_SPAWN
, "arg %d: [%s]\n", sp
->argc
, sp
->argv
[sp
->argc
-1]);
7655 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
7656 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
7660 spawn_remove(struct spawn_prog
*sp
)
7664 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
7666 TAILQ_REMOVE(&spawns
, sp
, entry
);
7667 for (i
= 0; i
< sp
->argc
; i
++)
7673 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
7679 struct spawn_prog
*sp
;
7681 while ((sp
= TAILQ_FIRST(&spawns
)) != NULL
) {
7687 spawn_find(const char *name
)
7689 struct spawn_prog
*sp
;
7691 TAILQ_FOREACH(sp
, &spawns
, entry
)
7692 if (strcasecmp(sp
->name
, name
) == 0)
7699 setspawn(const char *name
, const char *args
, int flags
)
7701 struct spawn_prog
*sp
;
7703 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
7708 /* Remove any old spawn under the same name. */
7709 if ((sp
= spawn_find(name
)) != NULL
)
7713 spawn_insert(name
, args
, flags
);
7715 warnx("error: setspawn: cannot find program: %s", name
);
7717 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
7721 setconfspawn(const char *selector
, const char *value
, int flags
)
7725 if (selector
== NULL
|| strlen(selector
) == 0)
7728 args
= expand_tilde(value
);
7730 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
7732 setspawn(selector
, args
, flags
);
7735 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
7740 validate_spawns(void)
7743 struct spawn_prog
*sp
;
7744 char which
[PATH_MAX
];
7747 RB_FOREACH(bp
, binding_tree
, &bindings
) {
7748 if (bp
->action
!= FN_SPAWN_CUSTOM
)
7752 sp
= spawn_find(bp
->spawn_name
);
7753 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
7756 /* verify we have the goods */
7757 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
7758 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
7760 for (i
= strlen("which "); i
< strlen(which
); i
++)
7761 if (which
[i
] == ' ') {
7765 if (system(which
) != 0)
7766 add_startup_exception("could not find %s",
7767 &which
[strlen("which ")]);
7774 setconfspawn("lock", "xlock", 0);
7776 setconfspawn("term", "xterm", 0);
7777 setconfspawn("spawn_term", "xterm", 0);
7779 setconfspawn("menu", "dmenu_run"
7783 " -nf $bar_font_color"
7785 " -sf $bar_color", 0);
7787 setconfspawn("search", "dmenu"
7792 " -nf $bar_font_color"
7794 " -sf $bar_color", 0);
7796 setconfspawn("name_workspace", "dmenu"
7801 " -nf $bar_font_color"
7803 " -sf $bar_color", 0);
7805 /* These are not verified for existence, even with a binding set. */
7806 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
7807 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
7808 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
7812 #define SWM_MODNAME_SIZE 32
7813 #define SWM_KEY_WS "\n+ \t"
7815 parsebinding(const char *bindstr
, uint16_t *mod
, enum binding_type
*type
,
7816 uint32_t *val
, uint32_t *flags
)
7818 char *str
, *cp
, *name
;
7819 KeySym ks
, lks
, uks
;
7821 DNPRINTF(SWM_D_KEY
, "parsebinding: enter [%s]\n", bindstr
);
7822 if (mod
== NULL
|| val
== NULL
) {
7823 DNPRINTF(SWM_D_KEY
, "parsebinding: no mod or key vars\n");
7826 if (bindstr
== NULL
|| strlen(bindstr
) == 0) {
7827 DNPRINTF(SWM_D_KEY
, "parsebinding: no bindstr\n");
7831 if ((cp
= str
= strdup(bindstr
)) == NULL
)
7832 err(1, "parsebinding: strdup");
7834 *val
= XCB_NO_SYMBOL
;
7838 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
7839 DNPRINTF(SWM_D_KEY
, "parsebinding: entry [%s]\n", name
);
7841 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
7842 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
7844 else if (strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
) == 0)
7845 *mod
|= XCB_MOD_MASK_1
;
7846 else if (strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
) == 0)
7847 *mod
|= XCB_MOD_MASK_2
;
7848 else if (strncmp(name
, "Mod3", SWM_MODNAME_SIZE
) == 0)
7849 *mod
|= XCB_MOD_MASK_3
;
7850 else if (strncmp(name
, "Mod4", SWM_MODNAME_SIZE
) == 0)
7851 *mod
|= XCB_MOD_MASK_4
;
7852 else if (strncmp(name
, "Mod5", SWM_MODNAME_SIZE
) == 0)
7853 *mod
|= XCB_MOD_MASK_5
;
7854 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
7855 *mod
|= XCB_MOD_MASK_SHIFT
;
7856 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
7857 *mod
|= XCB_MOD_MASK_CONTROL
;
7858 else if (strncasecmp(name
, "ANYMOD", SWM_MODNAME_SIZE
) == 0)
7859 *mod
|= XCB_MOD_MASK_ANY
;
7860 else if (strncasecmp(name
, "REPLAY", SWM_MODNAME_SIZE
) == 0)
7861 *flags
|= BINDING_F_REPLAY
;
7862 else if (sscanf(name
, "Button%u", val
) == 1) {
7863 DNPRINTF(SWM_D_KEY
, "parsebinding: button %u\n", *val
);
7865 if (*val
> 255 || *val
== 0) {
7867 "parsebinding: invalid button %u\n", *val
);
7871 /* TODO: do this without Xlib. */
7872 ks
= XStringToKeysym(name
);
7873 if (ks
== NoSymbol
) {
7875 "parsebinding: invalid key %s\n", name
);
7880 XConvertCase(ks
, &lks
, &uks
);
7881 *val
= (uint32_t)lks
;
7885 /* If ANYMOD was specified, ignore the rest. */
7886 if (*mod
& XCB_MOD_MASK_ANY
)
7887 *mod
= XCB_MOD_MASK_ANY
;
7890 DNPRINTF(SWM_D_KEY
, "parsebinding: leave ok\n");
7895 strdupsafe(const char *str
)
7900 return (strdup(str
));
7904 binding_cmp(struct binding
*bp1
, struct binding
*bp2
)
7906 if (bp1
->type
< bp2
->type
)
7908 if (bp1
->type
> bp2
->type
)
7911 if (bp1
->value
< bp2
->value
)
7913 if (bp1
->value
> bp2
->value
)
7916 if (bp1
->mod
< bp2
->mod
)
7918 if (bp1
->mod
> bp2
->mod
)
7925 binding_insert(uint16_t mod
, enum binding_type type
, uint32_t val
,
7926 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
7930 DNPRINTF(SWM_D_KEY
, "binding_insert: mod: %u, type: %d, val: %u, "
7931 "action: %s(%d), spawn_name: %s\n", mod
, type
, val
,
7932 actions
[aid
].name
, aid
, spawn_name
);
7934 if ((bp
= malloc(sizeof *bp
)) == NULL
)
7935 err(1, "binding_insert: malloc");
7942 bp
->spawn_name
= strdupsafe(spawn_name
);
7943 RB_INSERT(binding_tree
, &bindings
, bp
);
7945 DNPRINTF(SWM_D_KEY
, "binding_insert: leave\n");
7949 binding_lookup(uint16_t mod
, enum binding_type type
, uint32_t val
)
7957 return (RB_FIND(binding_tree
, &bindings
, &bp
));
7961 binding_remove(struct binding
*bp
)
7963 DNPRINTF(SWM_D_KEY
, "binding_remove: mod: %u, type: %d, val: %u, "
7964 "action: %s(%d), spawn_name: %s\n", bp
->mod
, bp
->type
, bp
->value
,
7965 actions
[bp
->action
].name
, bp
->action
, bp
->spawn_name
);
7967 RB_REMOVE(binding_tree
, &bindings
, bp
);
7968 free(bp
->spawn_name
);
7971 DNPRINTF(SWM_D_KEY
, "binding_remove: leave\n");
7975 setbinding(uint16_t mod
, enum binding_type type
, uint32_t val
,
7976 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
7980 DNPRINTF(SWM_D_KEY
, "setbinding: enter %s [%s]\n",
7981 actions
[aid
].name
, spawn_name
);
7983 /* Unbind any existing. Loop is to handle MOD_MASK_ANY. */
7984 while ((bp
= binding_lookup(mod
, type
, val
)))
7987 if (aid
!= FN_INVALID
)
7988 binding_insert(mod
, type
, val
, aid
, flags
, spawn_name
);
7990 DNPRINTF(SWM_D_KEY
, "setbinding: leave\n");
7994 setconfbinding(const char *selector
, const char *value
, int flags
)
7996 struct spawn_prog
*sp
;
7997 uint32_t keybtn
, opts
;
8000 enum binding_type type
;
8002 /* suppress unused warning since var is needed */
8005 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter selector: [%s], "
8006 "value: [%s]\n", selector
, value
);
8007 if (selector
== NULL
|| strlen(selector
) == 0) {
8008 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
8009 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8010 setbinding(mod
, type
, keybtn
, FN_INVALID
, opts
, NULL
);
8015 /* search by key function name */
8016 for (aid
= 0; aid
< FN_INVALID
; aid
++) {
8017 if (strncasecmp(selector
, actions
[aid
].name
,
8018 SWM_FUNCNAME_LEN
) == 0) {
8019 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8020 "action\n", selector
);
8021 if (parsebinding(value
, &mod
, &type
, &keybtn
,
8023 setbinding(mod
, type
, keybtn
, aid
, opts
, NULL
);
8029 /* search by custom spawn name */
8030 if ((sp
= spawn_find(selector
)) != NULL
) {
8031 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8032 "spawn\n", selector
);
8033 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8034 setbinding(mod
, type
, keybtn
, FN_SPAWN_CUSTOM
, opts
,
8040 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
8044 #define MODSHIFT MODKEY | XCB_MOD_MASK_SHIFT
8046 setup_keybindings(void)
8048 #define BINDKEY(m, k, a) setbinding(m, KEYBIND, k, a, 0, NULL)
8049 #define BINDKEYSPAWN(m, k, s) setbinding(m, KEYBIND, k, FN_SPAWN_CUSTOM, 0, s)
8050 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8051 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8052 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8053 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8054 BINDKEY(MODKEY
, XK_v
, FN_BUTTON2
);
8055 BINDKEY(MODKEY
, XK_space
, FN_CYCLE_LAYOUT
);
8056 BINDKEY(MODSHIFT
, XK_backslash
, FN_FLIP_LAYOUT
);
8057 BINDKEY(MODKEY
, XK_t
, FN_FLOAT_TOGGLE
);
8058 BINDKEY(MODKEY
, XK_m
, FN_FOCUS_MAIN
);
8059 BINDKEY(MODKEY
, XK_j
, FN_FOCUS_NEXT
);
8060 BINDKEY(MODKEY
, XK_Tab
, FN_FOCUS_NEXT
);
8061 BINDKEY(MODKEY
, XK_k
, FN_FOCUS_PREV
);
8062 BINDKEY(MODSHIFT
, XK_Tab
, FN_FOCUS_PREV
);
8063 BINDKEY(MODKEY
, XK_u
, FN_FOCUS_URGENT
);
8064 BINDKEY(MODKEY
, XK_e
, FN_MAXIMIZE_TOGGLE
);
8065 BINDKEY(MODSHIFT
, XK_equal
, FN_HEIGHT_GROW
);
8066 BINDKEY(MODSHIFT
, XK_minus
, FN_HEIGHT_SHRINK
);
8067 BINDKEY(MODKEY
, XK_w
, FN_ICONIFY
);
8068 BINDKEY(MODKEY
, XK_h
, FN_MASTER_SHRINK
);
8069 BINDKEY(MODKEY
, XK_l
, FN_MASTER_GROW
);
8070 BINDKEY(MODKEY
, XK_comma
, FN_MASTER_ADD
);
8071 BINDKEY(MODKEY
, XK_period
, FN_MASTER_DEL
);
8072 BINDKEY(MODSHIFT
, XK_bracketright
, FN_MOVE_DOWN
);
8073 BINDKEY(MODKEY
, XK_bracketleft
, FN_MOVE_LEFT
);
8074 BINDKEY(MODKEY
, XK_bracketright
, FN_MOVE_RIGHT
);
8075 BINDKEY(MODSHIFT
, XK_bracketleft
, FN_MOVE_UP
);
8076 BINDKEY(MODSHIFT
, XK_KP_End
, FN_MVRG_1
);
8077 BINDKEY(MODSHIFT
, XK_KP_Down
, FN_MVRG_2
);
8078 BINDKEY(MODSHIFT
, XK_KP_Next
, FN_MVRG_3
);
8079 BINDKEY(MODSHIFT
, XK_KP_Left
, FN_MVRG_4
);
8080 BINDKEY(MODSHIFT
, XK_KP_Begin
, FN_MVRG_5
);
8081 BINDKEY(MODSHIFT
, XK_KP_Right
, FN_MVRG_6
);
8082 BINDKEY(MODSHIFT
, XK_KP_Home
, FN_MVRG_7
);
8083 BINDKEY(MODSHIFT
, XK_KP_Up
, FN_MVRG_8
);
8084 BINDKEY(MODSHIFT
, XK_KP_Prior
, FN_MVRG_9
);
8085 BINDKEY(MODSHIFT
, XK_1
, FN_MVWS_1
);
8086 BINDKEY(MODSHIFT
, XK_2
, FN_MVWS_2
);
8087 BINDKEY(MODSHIFT
, XK_3
, FN_MVWS_3
);
8088 BINDKEY(MODSHIFT
, XK_4
, FN_MVWS_4
);
8089 BINDKEY(MODSHIFT
, XK_5
, FN_MVWS_5
);
8090 BINDKEY(MODSHIFT
, XK_6
, FN_MVWS_6
);
8091 BINDKEY(MODSHIFT
, XK_7
, FN_MVWS_7
);
8092 BINDKEY(MODSHIFT
, XK_8
, FN_MVWS_8
);
8093 BINDKEY(MODSHIFT
, XK_9
, FN_MVWS_9
);
8094 BINDKEY(MODSHIFT
, XK_0
, FN_MVWS_10
);
8095 BINDKEY(MODSHIFT
, XK_F1
, FN_MVWS_11
);
8096 BINDKEY(MODSHIFT
, XK_F2
, FN_MVWS_12
);
8097 BINDKEY(MODSHIFT
, XK_F3
, FN_MVWS_13
);
8098 BINDKEY(MODSHIFT
, XK_F4
, FN_MVWS_14
);
8099 BINDKEY(MODSHIFT
, XK_F5
, FN_MVWS_15
);
8100 BINDKEY(MODSHIFT
, XK_F6
, FN_MVWS_16
);
8101 BINDKEY(MODSHIFT
, XK_F7
, FN_MVWS_17
);
8102 BINDKEY(MODSHIFT
, XK_F8
, FN_MVWS_18
);
8103 BINDKEY(MODSHIFT
, XK_F9
, FN_MVWS_19
);
8104 BINDKEY(MODSHIFT
, XK_F10
, FN_MVWS_20
);
8105 BINDKEY(MODSHIFT
, XK_F11
, FN_MVWS_21
);
8106 BINDKEY(MODSHIFT
, XK_F12
, FN_MVWS_22
);
8107 BINDKEY(MODSHIFT
, XK_slash
, FN_NAME_WORKSPACE
);
8108 BINDKEY(MODSHIFT
, XK_q
, FN_QUIT
);
8109 BINDKEY(MODSHIFT
, XK_r
, FN_RAISE_TOGGLE
);
8110 BINDKEY(MODKEY
, XK_q
, FN_RESTART
);
8111 BINDKEY(MODKEY
, XK_KP_End
, FN_RG_1
);
8112 BINDKEY(MODKEY
, XK_KP_Down
, FN_RG_2
);
8113 BINDKEY(MODKEY
, XK_KP_Next
, FN_RG_3
);
8114 BINDKEY(MODKEY
, XK_KP_Left
, FN_RG_4
);
8115 BINDKEY(MODKEY
, XK_KP_Begin
, FN_RG_5
);
8116 BINDKEY(MODKEY
, XK_KP_Right
, FN_RG_6
);
8117 BINDKEY(MODKEY
, XK_KP_Home
, FN_RG_7
);
8118 BINDKEY(MODKEY
, XK_KP_Up
, FN_RG_8
);
8119 BINDKEY(MODKEY
, XK_KP_Prior
, FN_RG_9
);
8120 BINDKEY(MODSHIFT
, XK_Right
, FN_RG_NEXT
);
8121 BINDKEY(MODSHIFT
, XK_Left
, FN_RG_PREV
);
8122 BINDKEY(MODKEY
, XK_f
, FN_SEARCH_WIN
);
8123 BINDKEY(MODKEY
, XK_slash
, FN_SEARCH_WORKSPACE
);
8124 BINDKEYSPAWN(MODSHIFT
, XK_i
, "initscr");
8125 BINDKEYSPAWN(MODSHIFT
, XK_Delete
, "lock");
8126 BINDKEYSPAWN(MODKEY
, XK_p
, "menu");
8127 BINDKEYSPAWN(MODKEY
, XK_s
, "screenshot_all");
8128 BINDKEYSPAWN(MODSHIFT
, XK_s
, "screenshot_wind");
8129 BINDKEYSPAWN(MODSHIFT
, XK_Return
, "term");
8130 BINDKEY(MODSHIFT
, XK_comma
, FN_STACK_INC
);
8131 BINDKEY(MODSHIFT
, XK_period
, FN_STACK_DEC
);
8132 BINDKEY(MODSHIFT
, XK_space
, FN_STACK_RESET
);
8133 BINDKEY(MODKEY
, XK_Return
, FN_SWAP_MAIN
);
8134 BINDKEY(MODSHIFT
, XK_j
, FN_SWAP_NEXT
);
8135 BINDKEY(MODSHIFT
, XK_k
, FN_SWAP_PREV
);
8136 BINDKEY(MODSHIFT
, XK_w
, FN_UNICONIFY
);
8137 BINDKEY(MODSHIFT
, XK_v
, FN_VERSION
);
8138 BINDKEY(MODKEY
, XK_equal
, FN_WIDTH_GROW
);
8139 BINDKEY(MODKEY
, XK_minus
, FN_WIDTH_SHRINK
);
8140 BINDKEY(MODKEY
, XK_x
, FN_WIND_DEL
);
8141 BINDKEY(MODSHIFT
, XK_x
, FN_WIND_KILL
);
8142 BINDKEY(MODKEY
, XK_1
, FN_WS_1
);
8143 BINDKEY(MODKEY
, XK_2
, FN_WS_2
);
8144 BINDKEY(MODKEY
, XK_3
, FN_WS_3
);
8145 BINDKEY(MODKEY
, XK_4
, FN_WS_4
);
8146 BINDKEY(MODKEY
, XK_5
, FN_WS_5
);
8147 BINDKEY(MODKEY
, XK_6
, FN_WS_6
);
8148 BINDKEY(MODKEY
, XK_7
, FN_WS_7
);
8149 BINDKEY(MODKEY
, XK_8
, FN_WS_8
);
8150 BINDKEY(MODKEY
, XK_9
, FN_WS_9
);
8151 BINDKEY(MODKEY
, XK_0
, FN_WS_10
);
8152 BINDKEY(MODKEY
, XK_F1
, FN_WS_11
);
8153 BINDKEY(MODKEY
, XK_F2
, FN_WS_12
);
8154 BINDKEY(MODKEY
, XK_F3
, FN_WS_13
);
8155 BINDKEY(MODKEY
, XK_F4
, FN_WS_14
);
8156 BINDKEY(MODKEY
, XK_F5
, FN_WS_15
);
8157 BINDKEY(MODKEY
, XK_F6
, FN_WS_16
);
8158 BINDKEY(MODKEY
, XK_F7
, FN_WS_17
);
8159 BINDKEY(MODKEY
, XK_F8
, FN_WS_18
);
8160 BINDKEY(MODKEY
, XK_F9
, FN_WS_19
);
8161 BINDKEY(MODKEY
, XK_F10
, FN_WS_20
);
8162 BINDKEY(MODKEY
, XK_F11
, FN_WS_21
);
8163 BINDKEY(MODKEY
, XK_F12
, FN_WS_22
);
8164 BINDKEY(MODKEY
, XK_Right
, FN_WS_NEXT
);
8165 BINDKEY(MODKEY
, XK_Left
, FN_WS_PREV
);
8166 BINDKEY(MODKEY
, XK_Up
, FN_WS_NEXT_ALL
);
8167 BINDKEY(MODKEY
, XK_Down
, FN_WS_PREV_ALL
);
8168 BINDKEY(MODSHIFT
, XK_Up
, FN_WS_NEXT_MOVE
);
8169 BINDKEY(MODSHIFT
, XK_Down
, FN_WS_PREV_MOVE
);
8170 BINDKEY(MODKEY
, XK_a
, FN_WS_PRIOR
);
8172 BINDKEY(MODKEY
, XK_d
, FN_DEBUG_TOGGLE
);
8173 BINDKEY(MODSHIFT
, XK_d
, FN_DUMPWINS
);
8180 setup_btnbindings(void)
8182 setbinding(ANYMOD
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_FOCUS
,
8183 BINDING_F_REPLAY
, NULL
);
8184 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE
, 0, NULL
);
8185 setbinding(MODSHIFT
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE_CENTERED
, 0,
8187 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_MOVE
, 0, NULL
);
8192 clear_bindings(void)
8196 while ((bp
= RB_ROOT(&bindings
)))
8201 clear_keybindings(void)
8203 struct binding
*bp
, *bptmp
;
8205 RB_FOREACH_SAFE(bp
, binding_tree
, &bindings
, bptmp
) {
8206 if (bp
->type
!= KEYBIND
)
8213 setkeymapping(const char *selector
, const char *value
, int flags
)
8215 char *keymapping_file
;
8217 /* suppress unused warnings since vars are needed */
8221 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
8223 keymapping_file
= expand_tilde(value
);
8225 clear_keybindings();
8226 /* load new key bindings; if it fails, revert to default bindings */
8227 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
8228 clear_keybindings();
8229 setup_keybindings();
8232 free(keymapping_file
);
8234 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
8239 updatenumlockmask(void)
8242 xcb_get_modifier_mapping_reply_t
*modmap_r
;
8243 xcb_keycode_t
*modmap
, kc
, *keycode
;
8247 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
8248 xcb_get_modifier_mapping(conn
),
8251 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
8252 for (i
= 0; i
< 8; i
++) {
8253 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
8254 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
8256 keycode
= xcb_key_symbols_get_keycode(syms
,
8260 numlockmask
= (1 << i
);
8267 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
8274 int num_screens
, k
, j
;
8275 uint16_t modifiers
[4];
8276 xcb_keycode_t
*code
;
8278 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
8279 updatenumlockmask();
8282 modifiers
[1] = numlockmask
;
8283 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8284 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8286 num_screens
= get_screen_count();
8287 for (k
= 0; k
< num_screens
; k
++) {
8288 if (TAILQ_EMPTY(&screens
[k
].rl
))
8290 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
8292 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8293 if (bp
->type
!= KEYBIND
)
8296 /* If there is a catch-all, only bind that. */
8297 if ((binding_lookup(ANYMOD
, KEYBIND
, bp
->value
)) &&
8301 /* Skip unused ws binds. */
8302 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8303 bp
->action
<= FN_WS_22
)
8306 /* Skip unused mvws binds. */
8307 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8308 bp
->action
<= FN_MVWS_22
)
8311 if ((code
= xcb_key_symbols_get_keycode(syms
,
8312 bp
->value
)) == NULL
)
8315 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8316 /* All modifiers are grabbed in one pass. */
8317 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8318 "key: %u, modifiers: %d\n", bp
->value
,
8320 xcb_grab_key(conn
, 1, screens
[k
].root
,
8321 bp
->mod
, *code
, XCB_GRAB_MODE_ASYNC
,
8322 XCB_GRAB_MODE_SYNC
);
8324 /* Need to grab each modifier permutation. */
8325 for (j
= 0; j
< LENGTH(modifiers
); j
++) {
8326 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8327 "key: %u, modifiers: %d\n",
8328 bp
->value
, bp
->mod
| modifiers
[j
]);
8329 xcb_grab_key(conn
, 1,
8331 bp
->mod
| modifiers
[j
],
8332 *code
, XCB_GRAB_MODE_ASYNC
,
8333 XCB_GRAB_MODE_SYNC
);
8345 int num_screens
, i
, k
;
8346 uint16_t modifiers
[4];
8348 DNPRINTF(SWM_D_MOUSE
, "grabbuttons\n");
8349 updatenumlockmask();
8352 modifiers
[1] = numlockmask
;
8353 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8354 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8356 num_screens
= get_screen_count();
8357 for (k
= 0; k
< num_screens
; k
++) {
8358 if (TAILQ_EMPTY(&screens
[k
].rl
))
8360 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, screens
[k
].root
,
8362 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8363 if (bp
->type
!= BTNBIND
)
8366 /* If there is a catch-all, only bind that. */
8367 if ((binding_lookup(ANYMOD
, BTNBIND
, bp
->value
)) &&
8371 /* Skip unused ws binds. */
8372 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8373 bp
->action
<= FN_WS_22
)
8376 /* Skip unused mvws binds. */
8377 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8378 bp
->action
<= FN_MVWS_22
)
8381 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8382 /* All modifiers are grabbed in one pass. */
8383 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: grab, "
8384 "button: %u, modifiers: %d\n", bp
->value
,
8386 xcb_grab_button(conn
, 0, screens
[k
].root
,
8387 BUTTONMASK
, XCB_GRAB_MODE_SYNC
,
8388 XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
8389 XCB_CURSOR_NONE
, bp
->value
, bp
->mod
);
8391 /* Need to grab each modifier permutation. */
8392 for (i
= 0; i
< LENGTH(modifiers
); ++i
) {
8393 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: "
8394 "grab, button: %u, modifiers: %u\n",
8395 bp
->value
, bp
->mod
| modifiers
[i
]);
8396 xcb_grab_button(conn
, 0,
8397 screens
[k
].root
, BUTTONMASK
,
8399 XCB_GRAB_MODE_ASYNC
,
8401 XCB_CURSOR_NONE
, bp
->value
,
8402 bp
->mod
| modifiers
[i
]);
8409 const char *quirkname
[] = {
8410 "NONE", /* config string for "no value" */
8418 "FOCUSONMAP_SINGLE",
8426 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
8427 #define SWM_Q_DELIM "\n|+ \t"
8429 parsequirks(const char *qstr
, uint32_t *quirk
, int *ws
)
8431 char *str
, *cp
, *name
;
8434 if (quirk
== NULL
|| qstr
== NULL
)
8437 if ((cp
= str
= strdup(qstr
)) == NULL
)
8438 err(1, "parsequirks: strdup");
8441 while ((name
= strsep(&cp
, SWM_Q_DELIM
)) != NULL
) {
8443 cp
+= (long)strspn(cp
, SWM_Q_DELIM
);
8445 if (sscanf(name
, "WS[%d]", ws
) == 1) {
8451 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
8452 if (strncasecmp(name
, quirkname
[i
],
8453 SWM_QUIRK_LEN
) == 0) {
8454 DNPRINTF(SWM_D_QUIRK
,
8455 "parsequirks: %s\n", name
);
8461 *quirk
|= 1 << (i
-1);
8465 if (i
>= LENGTH(quirkname
)) {
8466 DNPRINTF(SWM_D_QUIRK
,
8467 "parsequirks: invalid quirk [%s]\n", name
);
8478 quirk_insert(const char *class, const char *instance
, const char *name
,
8479 uint32_t quirk
, int ws
)
8483 bool failed
= false;
8485 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: class: %s, instance: %s, name: %s,"
8486 " value: %u, ws: %d\n", class, instance
, name
, quirk
, ws
);
8488 if ((qp
= malloc(sizeof *qp
)) == NULL
)
8489 err(1, "quirk_insert: malloc");
8491 if ((qp
->class = strdup(class)) == NULL
)
8492 err(1, "quirk_insert: strdup");
8493 if ((qp
->instance
= strdup(instance
)) == NULL
)
8494 err(1, "quirk_insert: strdup");
8495 if ((qp
->name
= strdup(name
)) == NULL
)
8496 err(1, "quirk_insert: strdup");
8498 if (asprintf(&str
, "^%s$", class) == -1)
8499 err(1, "quirk_insert: asprintf");
8500 if (regcomp(&qp
->regex_class
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8501 add_startup_exception("regex failed to compile quirk 'class' "
8502 "field: %s", class);
8505 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8508 if (asprintf(&str
, "^%s$", instance
) == -1)
8509 err(1, "quirk_insert: asprintf");
8510 if (regcomp(&qp
->regex_instance
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8511 add_startup_exception("regex failed to compile quirk 'instance'"
8512 " field: %s", instance
);
8515 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8518 if (asprintf(&str
, "^%s$", name
) == -1)
8519 err(1, "quirk_insert: asprintf");
8520 if (regcomp(&qp
->regex_name
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8521 add_startup_exception("regex failed to compile quirk 'name' "
8525 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8529 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: regex error; skipping.\n");
8534 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
8536 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
8540 quirk_remove(struct quirk
*qp
)
8542 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%u]\n", qp
->class,
8543 qp
->name
, qp
->quirk
);
8545 TAILQ_REMOVE(&quirks
, qp
, entry
);
8548 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
8552 quirk_free(struct quirk
*qp
)
8554 regfree(&qp
->regex_class
);
8555 regfree(&qp
->regex_instance
);
8556 regfree(&qp
->regex_name
);
8568 while ((qp
= TAILQ_FIRST(&quirks
)) != NULL
) {
8574 quirk_replace(struct quirk
*qp
, const char *class, const char *instance
,
8575 const char *name
, uint32_t quirk
, int ws
)
8577 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp
->class,
8578 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
8581 quirk_insert(class, instance
, name
, quirk
, ws
);
8583 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
8587 setquirk(const char *class, const char *instance
, const char *name
,
8588 uint32_t quirk
, int ws
)
8592 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
8593 instance
, name
, quirk
, ws
);
8595 /* Remove/replace existing quirk. */
8596 TAILQ_FOREACH(qp
, &quirks
, entry
) {
8597 if (strcmp(qp
->class, class) == 0 &&
8598 strcmp(qp
->instance
, instance
) == 0 &&
8599 strcmp(qp
->name
, name
) == 0) {
8600 if (quirk
== 0 && ws
== -1)
8603 quirk_replace(qp
, class, instance
, name
, quirk
,
8605 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8610 /* Only insert if quirk is not NONE or forced ws is set. */
8611 if (quirk
|| ws
!= -1)
8612 quirk_insert(class, instance
, name
, quirk
, ws
);
8614 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8617 /* Eat '\' in str used to escape square brackets and colon. */
8619 unescape_selector(char *str
)
8623 for (cp
= str
; *str
!= '\0'; ++str
, ++cp
) {
8624 if (*str
== '\\' && (*(str
+ 1) == ':' || *(str
+ 1) == ']' ||
8634 setconfquirk(const char *selector
, const char *value
, int flags
)
8636 char *str
, *cp
, *class;
8637 char *instance
= NULL
, *name
= NULL
;
8638 int retval
, count
= 0, ws
= -1;
8641 /* suppress unused warning since var is needed */
8644 if (selector
== NULL
|| strlen(selector
) == 0)
8647 if ((str
= strdup(selector
)) == NULL
)
8648 err(1, "setconfquirk: strdup");
8650 /* Find non-escaped colon. */
8657 for (++cp
; *cp
!= '\0'; ++cp
) {
8658 if (*cp
== ':' && *(cp
- 1) != '\\') {
8664 unescape_selector(class);
8666 instance
= class + strlen(class) + 1;
8667 unescape_selector(instance
);
8673 name
= instance
+ strlen(instance
) + 1;
8674 unescape_selector(name
);
8679 DNPRINTF(SWM_D_CONF
, "setconfquirk: class: %s, instance: %s, "
8680 "name: %s\n", class, instance
, name
);
8682 if ((retval
= parsequirks(value
, &qrks
, &ws
)) == 0)
8683 setquirk(class, instance
, name
, qrks
, ws
);
8692 setquirk("MPlayer", "xv", ".*",
8693 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
, -1);
8694 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
8696 setquirk("Firefox-bin", "firefox-bin", ".*",
8698 setquirk("Firefox", "Dialog", ".*",
8700 setquirk("Gimp", "gimp", ".*",
8701 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8702 setquirk("XTerm", "xterm", ".*",
8703 SWM_Q_XTERM_FONTADJ
, -1);
8704 setquirk("xine", "Xine Window", ".*",
8705 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8706 setquirk("Xitk", "Xitk Combo", ".*",
8707 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8708 setquirk("xine", "xine Panel", ".*",
8709 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8710 setquirk("Xitk", "Xine Window", ".*",
8711 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8712 setquirk("xine", "xine Video Fullscreen Window", ".*",
8713 SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
, -1);
8714 setquirk("pcb", "pcb", ".*",
8716 setquirk("SDL_App", "SDL_App", ".*",
8717 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
, -1);
8720 /* conf file stuff */
8721 #define SWM_CONF_FILE "spectrwm.conf"
8722 #define SWM_CONF_FILE_OLD "scrotwm.conf"
8726 SWM_S_BAR_AT_BOTTOM
,
8727 SWM_S_BAR_BORDER_WIDTH
,
8730 SWM_S_BAR_ENABLED_WS
,
8735 SWM_S_BOUNDARY_WIDTH
,
8736 SWM_S_CLOCK_ENABLED
,
8739 SWM_S_CYCLE_VISIBLE
,
8741 SWM_S_DISABLE_BORDER
,
8743 SWM_S_FOCUS_CLOSE_WRAP
,
8744 SWM_S_FOCUS_DEFAULT
,
8746 SWM_S_ICONIC_ENABLED
,
8747 SWM_S_JAVA_WORKAROUND
,
8748 SWM_S_MAXIMIZE_HIDE_BAR
,
8749 SWM_S_REGION_PADDING
,
8754 SWM_S_STACK_ENABLED
,
8757 SWM_S_URGENT_COLLAPSE
,
8758 SWM_S_URGENT_ENABLED
,
8759 SWM_S_VERBOSE_LAYOUT
,
8762 SWM_S_WINDOW_CLASS_ENABLED
,
8763 SWM_S_WINDOW_INSTANCE_ENABLED
,
8764 SWM_S_WINDOW_NAME_ENABLED
,
8765 SWM_S_WORKSPACE_CLAMP
,
8766 SWM_S_WORKSPACE_LIMIT
,
8767 SWM_S_WORKSPACE_NAME
,
8771 setconfvalue(const char *selector
, const char *value
, int flags
)
8773 struct workspace
*ws
;
8774 int i
, ws_id
, num_screens
;
8775 char *b
, *str
, *sp
, s
[1024];
8778 case SWM_S_BAR_ACTION
:
8780 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
8781 err(1, "setconfvalue: bar_action");
8783 case SWM_S_BAR_AT_BOTTOM
:
8784 bar_at_bottom
= (atoi(value
) != 0);
8786 case SWM_S_BAR_BORDER_WIDTH
:
8787 bar_border_width
= atoi(value
);
8788 if (bar_border_width
< 0)
8789 bar_border_width
= 0;
8791 case SWM_S_BAR_DELAY
:
8792 /* No longer needed; leave to not break old conf files. */
8794 case SWM_S_BAR_ENABLED
:
8795 bar_enabled
= (atoi(value
) != 0);
8797 case SWM_S_BAR_ENABLED_WS
:
8798 ws_id
= atoi(selector
) - 1;
8799 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8800 errx(1, "setconfvalue: bar_enabled_ws: invalid "
8801 "workspace %d.", ws_id
+ 1);
8803 num_screens
= get_screen_count();
8804 for (i
= 0; i
< num_screens
; i
++) {
8805 ws
= (struct workspace
*)&screens
[i
].ws
;
8806 ws
[ws_id
].bar_enabled
= (atoi(value
) != 0);
8809 case SWM_S_BAR_FONT
:
8811 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
8812 err(1, "setconfvalue: asprintf: failed to allocate "
8813 "memory for bar_fonts.");
8816 /* If already in xft mode, then we are done. */
8817 if (!bar_font_legacy
)
8820 if ((sp
= str
= strdup(value
)) == NULL
)
8821 err(1, "setconfvalue: strdup");
8823 /* If there are any non-XLFD entries, switch to Xft mode. */
8824 while ((b
= strsep(&sp
, ",")) != NULL
) {
8828 bar_font_legacy
= false;
8835 case SWM_S_BAR_FORMAT
:
8837 if ((bar_format
= strdup(value
)) == NULL
)
8838 err(1, "setconfvalue: bar_format");
8840 case SWM_S_BAR_JUSTIFY
:
8841 if (strcmp(value
, "left") == 0)
8842 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
8843 else if (strcmp(value
, "center") == 0)
8844 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
8845 else if (strcmp(value
, "right") == 0)
8846 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
8848 errx(1, "invalid bar_justify");
8850 case SWM_S_BORDER_WIDTH
:
8851 border_width
= atoi(value
);
8852 if (border_width
< 0)
8855 case SWM_S_BOUNDARY_WIDTH
:
8856 boundary_width
= atoi(value
);
8857 if (boundary_width
< 0)
8860 case SWM_S_CLOCK_ENABLED
:
8861 clock_enabled
= (atoi(value
) != 0);
8863 case SWM_S_CLOCK_FORMAT
:
8864 #ifndef SWM_DENY_CLOCK_FORMAT
8866 if ((clock_format
= strdup(value
)) == NULL
)
8867 err(1, "setconfvalue: clock_format");
8870 case SWM_S_CYCLE_EMPTY
:
8871 cycle_empty
= (atoi(value
) != 0);
8873 case SWM_S_CYCLE_VISIBLE
:
8874 cycle_visible
= (atoi(value
) != 0);
8876 case SWM_S_DIALOG_RATIO
:
8877 dialog_ratio
= atof(value
);
8878 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
8881 case SWM_S_DISABLE_BORDER
:
8882 disable_border
= (atoi(value
) != 0);
8884 case SWM_S_FOCUS_CLOSE
:
8885 if (strcmp(value
, "first") == 0)
8886 focus_close
= SWM_STACK_BOTTOM
;
8887 else if (strcmp(value
, "last") == 0)
8888 focus_close
= SWM_STACK_TOP
;
8889 else if (strcmp(value
, "next") == 0)
8890 focus_close
= SWM_STACK_ABOVE
;
8891 else if (strcmp(value
, "previous") == 0)
8892 focus_close
= SWM_STACK_BELOW
;
8894 errx(1, "focus_close");
8896 case SWM_S_FOCUS_CLOSE_WRAP
:
8897 focus_close_wrap
= (atoi(value
) != 0);
8899 case SWM_S_FOCUS_DEFAULT
:
8900 if (strcmp(value
, "last") == 0)
8901 focus_default
= SWM_STACK_TOP
;
8902 else if (strcmp(value
, "first") == 0)
8903 focus_default
= SWM_STACK_BOTTOM
;
8905 errx(1, "focus_default");
8907 case SWM_S_FOCUS_MODE
:
8908 if (strcmp(value
, "default") == 0)
8909 focus_mode
= SWM_FOCUS_DEFAULT
;
8910 else if (strcmp(value
, "follow") == 0 ||
8911 strcmp(value
, "follow_cursor") == 0)
8912 focus_mode
= SWM_FOCUS_FOLLOW
;
8913 else if (strcmp(value
, "manual") == 0)
8914 focus_mode
= SWM_FOCUS_MANUAL
;
8916 errx(1, "focus_mode");
8918 case SWM_S_ICONIC_ENABLED
:
8919 iconic_enabled
= (atoi(value
) != 0);
8921 case SWM_S_JAVA_WORKAROUND
:
8922 java_workaround
= (atoi(value
) != 0);
8924 case SWM_S_MAXIMIZE_HIDE_BAR
:
8925 maximize_hide_bar
= atoi(value
);
8927 case SWM_S_REGION_PADDING
:
8928 region_padding
= atoi(value
);
8929 if (region_padding
< 0)
8932 case SWM_S_SPAWN_ORDER
:
8933 if (strcmp(value
, "first") == 0)
8934 spawn_position
= SWM_STACK_BOTTOM
;
8935 else if (strcmp(value
, "last") == 0)
8936 spawn_position
= SWM_STACK_TOP
;
8937 else if (strcmp(value
, "next") == 0)
8938 spawn_position
= SWM_STACK_ABOVE
;
8939 else if (strcmp(value
, "previous") == 0)
8940 spawn_position
= SWM_STACK_BELOW
;
8942 errx(1, "spawn_position");
8944 case SWM_S_SPAWN_TERM
:
8945 setconfspawn("term", value
, 0);
8946 setconfspawn("spawn_term", value
, 0);
8949 /* No longer needed; leave to not break old conf files. */
8951 case SWM_S_SS_ENABLED
:
8952 /* No longer needed; leave to not break old conf files. */
8954 case SWM_S_STACK_ENABLED
:
8955 stack_enabled
= (atoi(value
) != 0);
8957 case SWM_S_TERM_WIDTH
:
8958 term_width
= atoi(value
);
8962 case SWM_S_TILE_GAP
:
8963 tile_gap
= atoi(value
);
8965 case SWM_S_URGENT_COLLAPSE
:
8966 urgent_collapse
= (atoi(value
) != 0);
8968 case SWM_S_URGENT_ENABLED
:
8969 urgent_enabled
= (atoi(value
) != 0);
8971 case SWM_S_VERBOSE_LAYOUT
:
8972 verbose_layout
= (atoi(value
) != 0);
8973 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
8975 layouts
[i
].l_string
= fancy_stacker
;
8977 layouts
[i
].l_string
= plain_stacker
;
8980 case SWM_S_WARP_FOCUS
:
8981 warp_focus
= (atoi(value
) != 0);
8983 case SWM_S_WARP_POINTER
:
8984 warp_pointer
= (atoi(value
) != 0);
8986 case SWM_S_WINDOW_CLASS_ENABLED
:
8987 window_class_enabled
= (atoi(value
) != 0);
8989 case SWM_S_WINDOW_INSTANCE_ENABLED
:
8990 window_instance_enabled
= (atoi(value
) != 0);
8992 case SWM_S_WINDOW_NAME_ENABLED
:
8993 window_name_enabled
= (atoi(value
) != 0);
8995 case SWM_S_WORKSPACE_CLAMP
:
8996 workspace_clamp
= (atoi(value
) != 0);
8998 case SWM_S_WORKSPACE_LIMIT
:
8999 workspace_limit
= atoi(value
);
9000 if (workspace_limit
> SWM_WS_MAX
)
9001 workspace_limit
= SWM_WS_MAX
;
9002 else if (workspace_limit
< 1)
9003 workspace_limit
= 1;
9005 ewmh_update_desktops();
9007 case SWM_S_WORKSPACE_NAME
:
9008 if (getenv("SWM_STARTED") != NULL
)
9012 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9013 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
9015 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9016 errx(1, "setconfvalue: workspace_name: invalid "
9017 "workspace %d.", ws_id
+ 1);
9019 num_screens
= get_screen_count();
9020 for (i
= 0; i
< num_screens
; ++i
) {
9021 ws
= (struct workspace
*)&screens
[i
].ws
;
9023 if (strlen(s
) > 0) {
9024 free(ws
[ws_id
].name
);
9025 if ((ws
[ws_id
].name
= strdup(s
)) == NULL
)
9026 err(1, "setconfvalue: workspace_name.");
9028 ewmh_update_desktop_names();
9029 ewmh_get_desktop_names();
9040 setconfmodkey(const char *selector
, const char *value
, int flags
)
9042 /* suppress unused warnings since vars are needed */
9046 if (strncasecmp(value
, "Mod1", strlen("Mod1")) == 0)
9047 update_modkey(XCB_MOD_MASK_1
);
9048 else if (strncasecmp(value
, "Mod2", strlen("Mod2")) == 0)
9049 update_modkey(XCB_MOD_MASK_2
);
9050 else if (strncasecmp(value
, "Mod3", strlen("Mod3")) == 0)
9051 update_modkey(XCB_MOD_MASK_3
);
9052 else if (strncasecmp(value
, "Mod4", strlen("Mod4")) == 0)
9053 update_modkey(XCB_MOD_MASK_4
);
9054 else if (strncasecmp(value
, "Mod5", strlen("Mod5")) == 0)
9055 update_modkey(XCB_MOD_MASK_5
);
9062 setconfcolor(const char *selector
, const char *value
, int flags
)
9064 int first
, last
, i
= 0, num_screens
;
9066 num_screens
= get_screen_count();
9068 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
9069 if (selector
== NULL
|| strlen(selector
) == 0 ||
9070 (last
= atoi(selector
) - 1) < 0) {
9072 last
= num_screens
- 1;
9077 if (last
>= num_screens
) {
9078 add_startup_exception("invalid screen index: %d out of bounds "
9079 "(maximum %d)", last
+ 1, num_screens
);
9083 for (i
= first
; i
<= last
; ++i
) {
9084 setscreencolor(value
, i
, flags
);
9087 * When setting focus/unfocus colors, we need to also
9088 * set maximize colors to match if they haven't been customized.
9090 if (flags
== SWM_S_COLOR_FOCUS
&&
9091 !screens
[i
].c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].manual
)
9092 setscreencolor(value
, i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
9093 else if (flags
== SWM_S_COLOR_UNFOCUS
&&
9094 !screens
[i
].c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].manual
)
9095 setscreencolor(value
, i
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
9097 screens
[i
].c
[flags
].manual
= 1;
9104 setconfregion(const char *selector
, const char *value
, int flags
)
9106 /* suppress unused warnings since vars are needed */
9110 custom_region(value
);
9115 setautorun(const char *selector
, const char *value
, int flags
)
9119 char *ap
, *sp
, *str
;
9125 /* suppress unused warnings since vars are needed */
9129 if (getenv("SWM_STARTED"))
9133 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9134 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
9136 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9137 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
9139 sp
= str
= expand_tilde((char *)&s
);
9142 * This is a little intricate
9144 * If the pid already exists we simply reuse it because it means it was
9145 * used before AND not claimed by manage_window. We get away with
9146 * altering it in the parent after INSERT because this can not be a race
9149 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
9152 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
9154 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
9155 err(1, "setautorun: realloc");
9156 a
.argv
[argc
- 1] = ap
;
9159 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
9160 err(1, "setautorun: realloc");
9161 a
.argv
[argc
] = NULL
;
9163 if ((pid
= fork()) == 0) {
9164 spawn(ws_id
, &a
, true);
9174 p
= calloc(1, sizeof *p
);
9177 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
9187 setlayout(const char *selector
, const char *value
, int flags
)
9189 struct workspace
*ws
;
9190 int ws_id
, i
, x
, mg
, ma
, si
, ar
;
9191 int st
= SWM_V_STACK
, num_screens
;
9195 /* suppress unused warnings since vars are needed */
9199 if (getenv("SWM_STARTED"))
9203 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
9204 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
9205 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9206 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9209 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9210 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
9212 if (strcasecmp(s
, "vertical") == 0)
9214 else if (strcasecmp(s
, "vertical_flip") == 0) {
9217 } else if (strcasecmp(s
, "horizontal") == 0)
9219 else if (strcasecmp(s
, "horizontal_flip") == 0) {
9222 } else if (strcasecmp(s
, "fullscreen") == 0)
9225 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9226 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9229 num_screens
= get_screen_count();
9230 for (i
= 0; i
< num_screens
; i
++) {
9231 ws
= (struct workspace
*)&screens
[i
].ws
;
9232 ws
[ws_id
].cur_layout
= &layouts
[st
];
9234 ws
[ws_id
].always_raise
= (ar
!= 0);
9235 if (st
== SWM_MAX_STACK
)
9239 for (x
= 0; x
< abs(mg
); x
++) {
9240 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9241 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
9242 SWM_ARG_ID_MASTERSHRINK
);
9245 for (x
= 0; x
< abs(ma
); x
++) {
9246 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9247 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
9248 SWM_ARG_ID_MASTERDEL
);
9251 for (x
= 0; x
< abs(si
); x
++) {
9252 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9253 si
>= 0 ? SWM_ARG_ID_STACKINC
:
9254 SWM_ARG_ID_STACKDEC
);
9258 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9259 SWM_ARG_ID_FLIPLAYOUT
);
9266 /* config options */
9267 struct config_option
{
9269 int (*func
)(const char*, const char*, int);
9272 struct config_option configopt
[] = {
9273 { "autorun", setautorun
, 0 },
9274 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
9275 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
9276 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
9277 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
9278 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
9279 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
9280 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
9281 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
9282 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
9283 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
9284 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
9285 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
9286 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
9287 { "bind", setconfbinding
, 0 },
9288 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
9289 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
9290 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
9291 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
9292 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
9293 { "color_focus_maximized", setconfcolor
, SWM_S_COLOR_FOCUS_MAXIMIZED
},
9294 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
9295 { "color_unfocus_maximized", setconfcolor
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
},
9296 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
9297 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
9298 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
9299 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
9300 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
9301 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
9302 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
9303 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
9304 { "iconic_enabled", setconfvalue
, SWM_S_ICONIC_ENABLED
},
9305 { "java_workaround", setconfvalue
, SWM_S_JAVA_WORKAROUND
},
9306 { "keyboard_mapping", setkeymapping
, 0 },
9307 { "layout", setlayout
, 0 },
9308 { "maximize_hide_bar", setconfvalue
, SWM_S_MAXIMIZE_HIDE_BAR
},
9309 { "modkey", setconfmodkey
, 0 },
9310 { "program", setconfspawn
, 0 },
9311 { "quirk", setconfquirk
, 0 },
9312 { "region", setconfregion
, 0 },
9313 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
9314 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
9315 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
9316 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
9317 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
9318 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
9319 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
9320 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
9321 { "title_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
}, /* For backwards compat. */
9322 { "title_name_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
}, /* For backwards compat. */
9323 { "urgent_collapse", setconfvalue
, SWM_S_URGENT_COLLAPSE
},
9324 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
9325 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
9326 { "warp_focus", setconfvalue
, SWM_S_WARP_FOCUS
},
9327 { "warp_pointer", setconfvalue
, SWM_S_WARP_POINTER
},
9328 { "window_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
},
9329 { "window_instance_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
},
9330 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
9331 { "workspace_clamp", setconfvalue
, SWM_S_WORKSPACE_CLAMP
},
9332 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
9333 { "name", setconfvalue
, SWM_S_WORKSPACE_NAME
},
9337 _add_startup_exception(const char *fmt
, va_list ap
)
9339 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
9340 warn("%s: asprintf", __func__
);
9344 add_startup_exception(const char *fmt
, ...)
9350 if (startup_exception
)
9353 /* force bar to be enabled due to exception */
9357 _add_startup_exception(fmt
, ap
);
9362 conf_load(const char *filename
, int keymapping
)
9365 char *line
= NULL
, *cp
, *ce
, *optsub
, *optval
= NULL
;
9366 size_t linelen
, lineno
= 0;
9367 int wordlen
, i
, optidx
, count
;
9368 struct config_option
*opt
= NULL
;
9370 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
9372 if (filename
== NULL
) {
9373 warnx("conf_load: no filename");
9377 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
9379 if ((config
= fopen(filename
, "r")) == NULL
) {
9380 warn("conf_load: fopen: %s", filename
);
9384 while (!feof(config
)) {
9388 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
9389 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
9391 err(1, "%s", filename
);
9396 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9397 if (cp
[0] == '\0') {
9401 /* get config option */
9402 wordlen
= strcspn(cp
, "=[ \t\n");
9404 add_startup_exception("%s: line %zd: no option found",
9409 for (i
= 0; i
< LENGTH(configopt
); i
++) {
9410 opt
= &configopt
[i
];
9411 if (strncasecmp(cp
, opt
->optname
, wordlen
) == 0 &&
9412 (int)strlen(opt
->optname
) == wordlen
) {
9418 add_startup_exception("%s: line %zd: unknown option "
9419 "%.*s", filename
, lineno
, wordlen
, cp
);
9422 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
9423 add_startup_exception("%s: line %zd: invalid option "
9424 "%.*s", filename
, lineno
, wordlen
, cp
);
9428 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9430 /* get [selector] if any */
9435 /* Get length of selector. */
9436 for (ce
= ++cp
; *ce
!= '\0'; ++ce
) {
9437 /* Find matching (not escaped) bracket. */
9438 if (*ce
== ']' && *(ce
- 1) != '\\') {
9445 add_startup_exception("%s: line %zd: syntax "
9446 "error: unterminated selector", filename
,
9451 /* ce points at ']'; terminate optsub. */
9456 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
9459 if (strlen(optval
) == 0) {
9460 add_startup_exception("%s: line %zd: must supply value "
9461 "to %s", filename
, lineno
,
9462 configopt
[optidx
].optname
);
9465 /* call function to deal with it all */
9466 if (configopt
[optidx
].func(optsub
, optval
,
9467 configopt
[optidx
].funcflags
) != 0) {
9468 add_startup_exception("%s: line %zd: invalid data for "
9469 "%s", filename
, lineno
, configopt
[optidx
].optname
);
9478 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
9484 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
9486 struct ws_win
*parent
, *w
;
9487 struct swm_region
*r
;
9488 struct workspace
*ws
;
9489 xcb_icccm_wm_hints_t wmh
;
9491 parent
= find_window(win
->transient
);
9493 parent
->focus_child
= win
;
9495 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
9496 " for %#x trans %#x\n", win
->id
, win
->transient
);
9498 r
= root_to_region(win
->s
->root
, SWM_CK_ALL
);
9500 /* parent doen't exist in our window list */
9501 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
9502 if (xcb_icccm_get_wm_hints_reply(conn
,
9503 xcb_icccm_get_wm_hints(conn
, w
->id
),
9505 warnx("can't get hints for %#x", w
->id
);
9509 if (win
->hints
.window_group
!= wmh
.window_group
)
9512 w
->focus_child
= win
;
9513 win
->transient
= w
->id
;
9515 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
9516 "transient to %#x\n", win
->transient
);
9523 window_get_pid(xcb_window_t win
)
9528 xcb_get_property_cookie_t pc
;
9529 xcb_get_property_reply_t
*pr
;
9531 apid
= get_atom_from_string("_NET_WM_PID");
9532 if (apid
== XCB_ATOM_NONE
)
9535 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
9536 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9539 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
9544 if (pr
->type
== apid
&& pr
->format
== 32)
9545 ret
= *((pid_t
*)xcb_get_property_value(pr
));
9551 apid
= get_atom_from_string("_SWM_PID");
9552 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
9554 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9557 if (pr
->type
!= apid
) {
9562 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
9569 get_swm_ws(xcb_window_t id
)
9575 xcb_get_property_reply_t
*gpr
;
9577 gpr
= xcb_get_property_reply(conn
,
9578 xcb_get_property(conn
, 0, id
, a_swm_ws
,
9579 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
9584 proplen
= xcb_get_property_value_length(gpr
);
9586 prop
= malloc(proplen
+ 1);
9589 xcb_get_property_value(gpr
),
9591 prop
[proplen
] = '\0';
9598 DNPRINTF(SWM_D_PROP
, "get_swm_ws: _SWM_WS: %s\n", prop
);
9599 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
9601 DNPRINTF(SWM_D_PROP
, "get_swm_ws: win #%s: %s",
9611 get_ws_idx(struct ws_win
*win
)
9613 xcb_get_property_reply_t
*gpr
;
9619 gpr
= xcb_get_property_reply(conn
,
9620 xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
9621 XCB_ATOM_CARDINAL
, 0, 1),
9624 if (gpr
->type
== XCB_ATOM_CARDINAL
&& gpr
->format
== 32)
9625 ws_idx
= *((int *)xcb_get_property_value(gpr
));
9629 if (ws_idx
== -1 && !(win
->quirks
& SWM_Q_IGNORESPAWNWS
))
9630 ws_idx
= get_swm_ws(win
->id
);
9632 if (ws_idx
> workspace_limit
- 1 || ws_idx
< -1)
9635 DNPRINTF(SWM_D_PROP
, "get_ws_idx: win %#x, ws_idx: %d\n", win
->id
,
9642 reparent_window(struct ws_win
*win
)
9644 xcb_void_cookie_t c
;
9645 xcb_generic_error_t
*error
;
9648 win
->frame
= xcb_generate_id(conn
);
9650 DNPRINTF(SWM_D_MISC
, "reparent_window: win %#x, frame: %#x\n",
9651 win
->id
, win
->frame
);
9654 XCB_EVENT_MASK_ENTER_WINDOW
|
9655 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
9656 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
9657 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
|
9658 XCB_EVENT_MASK_EXPOSURE
;
9660 wa
[0] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
9663 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
->frame
, win
->s
->root
,
9664 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), 0,
9665 XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
9666 XCB_CW_EVENT_MASK
, wa
);
9668 win
->state
= SWM_WIN_STATE_REPARENTING
;
9669 c
= xcb_reparent_window_checked(conn
, win
->id
, win
->frame
, 0, 0);
9670 if ((error
= xcb_request_check(conn
, c
))) {
9671 DNPRINTF(SWM_D_MISC
, "reparent_window: error:\n");
9676 xcb_destroy_window(conn
, win
->frame
);
9677 win
->frame
= XCB_WINDOW_NONE
;
9679 xcb_change_save_set(conn
, XCB_SET_MODE_INSERT
, win
->id
);
9681 DNPRINTF(SWM_D_MISC
, "reparent_window: done.\n");
9685 unparent_window(struct ws_win
*win
)
9687 xcb_change_save_set(conn
, XCB_SET_MODE_DELETE
, win
->id
);
9688 xcb_reparent_window(conn
, win
->id
, win
->s
->root
, X(win
), Y(win
));
9689 xcb_destroy_window(conn
, win
->frame
);
9690 win
->frame
= XCB_WINDOW_NONE
;
9691 win
->state
= SWM_WIN_STATE_UNPARENTING
;
9695 manage_window(xcb_window_t id
, int spawn_pos
, bool mapping
)
9697 struct ws_win
*win
= NULL
, *ww
;
9698 struct swm_region
*r
;
9701 xcb_get_geometry_reply_t
*gr
;
9702 xcb_get_window_attributes_reply_t
*war
= NULL
;
9703 xcb_window_t trans
= XCB_WINDOW_NONE
;
9704 uint32_t i
, wa
[1], new_flags
;
9705 int ws_idx
, force_ws
= -1;
9706 char *class, *instance
, *name
;
9709 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region bar; "
9714 if (find_region(id
)) {
9715 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region window; "
9720 if ((win
= find_window(id
)) != NULL
) {
9721 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x already "
9722 "managed; skipping.)\n", id
);
9723 return (win
); /* Already managed. */
9726 /* See if window is on the unmanaged list. */
9727 if ((win
= find_unmanaged_window(id
)) != NULL
) {
9728 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x found on "
9729 "unmanaged list.\n", id
);
9730 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
9733 set_child_transient(win
, &trans
);
9737 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is new.\n", id
);
9740 war
= xcb_get_window_attributes_reply(conn
,
9741 xcb_get_window_attributes(conn
, id
), NULL
);
9743 DNPRINTF(SWM_D_EVENT
, "manage_window: window lost.\n");
9747 if (war
->override_redirect
) {
9748 DNPRINTF(SWM_D_EVENT
, "manage_window: override_redirect; "
9753 if (!mapping
&& war
->map_state
== XCB_MAP_STATE_UNMAPPED
&&
9754 get_win_state(id
) == XCB_ICCCM_WM_STATE_WITHDRAWN
) {
9755 DNPRINTF(SWM_D_EVENT
, "manage_window: window withdrawn; "
9760 /* Try to get initial window geometry. */
9761 gr
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, id
), NULL
);
9763 DNPRINTF(SWM_D_MISC
, "manage_window: get geometry failed.\n");
9767 /* Create and initialize ws_win object. */
9768 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
9769 err(1, "manage_window: calloc: failed to allocate memory for "
9774 /* Figureout which region the window belongs to. */
9775 r
= root_to_region(gr
->root
, SWM_CK_ALL
);
9777 /* Ignore window border if there is one. */
9778 WIDTH(win
) = gr
->width
;
9779 HEIGHT(win
) = gr
->height
;
9780 X(win
) = gr
->x
+ gr
->border_width
;
9781 Y(win
) = gr
->y
+ gr
->border_width
;
9782 win
->bordered
= false;
9783 win
->mapped
= (war
->map_state
!= XCB_MAP_STATE_UNMAPPED
);
9784 win
->s
= r
->s
; /* this never changes */
9788 /* Select which X events to monitor and set border pixel color. */
9789 wa
[0] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
9790 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
9792 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_EVENT_MASK
, wa
);
9794 /* Get WM_SIZE_HINTS. */
9795 xcb_icccm_get_wm_normal_hints_reply(conn
,
9796 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
9800 xcb_icccm_get_wm_hints_reply(conn
,
9801 xcb_icccm_get_wm_hints(conn
, win
->id
),
9804 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
9805 xcb_icccm_get_wm_transient_for_reply(conn
,
9806 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
9809 win
->transient
= trans
;
9810 set_child_transient(win
, &win
->transient
);
9813 /* Get WM_PROTOCOLS. */
9814 get_wm_protocols(win
);
9817 /* Must be after getting WM_HINTS and WM_PROTOCOLS. */
9818 DNPRINTF(SWM_D_FOCUS
, "manage_window: input_model: %s\n",
9819 get_win_input_model(win
));
9822 /* Set initial quirks based on EWMH. */
9823 ewmh_autoquirk(win
);
9825 /* Determine initial quirks. */
9826 xcb_icccm_get_wm_class_reply(conn
,
9827 xcb_icccm_get_wm_class(conn
, win
->id
),
9830 class = win
->ch
.class_name
? win
->ch
.class_name
: "";
9831 instance
= win
->ch
.instance_name
? win
->ch
.instance_name
: "";
9832 name
= get_win_name(win
->id
);
9834 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, instance: %s, "
9835 "name: %s\n", class, instance
, name
);
9837 /* java is retarded so treat it special */
9838 if (strstr(instance
, "sun-awt")) {
9839 DNPRINTF(SWM_D_CLASS
, "manage_window: java window detected.\n");
9843 TAILQ_FOREACH(qp
, &quirks
, entry
) {
9844 if (regexec(&qp
->regex_class
, class, 0, NULL
, 0) == 0 &&
9845 regexec(&qp
->regex_instance
, instance
, 0, NULL
, 0) == 0 &&
9846 regexec(&qp
->regex_name
, name
, 0, NULL
, 0) == 0) {
9847 DNPRINTF(SWM_D_CLASS
, "manage_window: matched "
9848 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp
->class,
9849 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
9850 win
->quirks
= qp
->quirk
;
9851 if (qp
->ws
>= 0 && qp
->ws
< workspace_limit
)
9858 /* Reset font sizes (the bruteforce way; no default keybinding). */
9859 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
9860 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9861 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
9862 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9863 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
9866 /* Figure out which workspace the window belongs to. */
9867 if (!(win
->quirks
& SWM_Q_IGNOREPID
) &&
9868 (p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
9869 win
->ws
= &r
->s
->ws
[p
->ws
];
9870 TAILQ_REMOVE(&pidlist
, p
, entry
);
9873 } else if ((ws_idx
= get_ws_idx(win
)) != -1 &&
9875 /* _SWM_WS is set; use that. */
9876 win
->ws
= &r
->s
->ws
[ws_idx
];
9877 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
9878 /* Launch transients in the same ws as parent. */
9885 win
->ws
= &r
->s
->ws
[force_ws
];
9887 /* Set the _NET_WM_DESKTOP atom. */
9888 DNPRINTF(SWM_D_PROP
, "manage_window: set _NET_WM_DESKTOP: %d\n",
9890 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
9891 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &win
->ws
->idx
);
9893 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
9894 xcb_delete_property(conn
, win
->id
, a_swm_ws
);
9896 /* WS must already be set for this to work. */
9897 store_float_geom(win
);
9899 /* Make sure window is positioned inside its region, if its active. */
9901 region_containment(win
, r
, SWM_CW_ALLSIDES
|
9902 SWM_CW_HARDBOUNDARY
);
9907 /* Figure out where to insert the window in the workspace list. */
9908 if (trans
&& (ww
= find_window(trans
)))
9909 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
9910 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_ABOVE
)
9911 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
9913 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_BELOW
)
9914 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
9915 else switch (spawn_pos
) {
9918 case SWM_STACK_ABOVE
:
9919 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
9921 case SWM_STACK_BOTTOM
:
9922 case SWM_STACK_BELOW
:
9923 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
9926 ewmh_update_client_list();
9928 TAILQ_INSERT_HEAD(&win
->ws
->stack
, win
, stack_entry
);
9931 /* Get/apply initial _NET_WM_STATE */
9932 ewmh_get_wm_state(win
);
9935 new_flags
= win
->ewmh_flags
;
9937 if (win
->quirks
& SWM_Q_FLOAT
)
9938 new_flags
|= EWMH_F_ABOVE
;
9940 if (win
->quirks
& SWM_Q_ANYWHERE
)
9941 new_flags
|= SWM_F_MANUAL
;
9943 ewmh_apply_flags(win
, new_flags
);
9944 ewmh_update_wm_state(win
);
9946 /* Set initial _NET_WM_ALLOWED_ACTIONS */
9947 ewmh_update_actions(win
);
9949 reparent_window(win
);
9951 DNPRINTF(SWM_D_MISC
, "manage_window: done. win %#x, (x,y) w x h: "
9952 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win
->id
,
9953 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
9954 YESNO(ICONIC(win
)), win
->transient
);
9961 free_window(struct ws_win
*win
)
9963 DNPRINTF(SWM_D_MISC
, "free_window: win %#x\n", WINID(win
));
9968 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
9971 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
9974 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
9978 unmanage_window(struct ws_win
*win
)
9980 DNPRINTF(SWM_D_MISC
, "unmanage_window: win %#x\n", WINID(win
));
9986 unparent_window(win
);
9988 TAILQ_REMOVE(&win
->ws
->stack
, win
, stack_entry
);
9989 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
9990 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
9992 ewmh_update_client_list();
9996 expose(xcb_expose_event_t
*e
)
10001 struct workspace
*ws
;
10004 DNPRINTF(SWM_D_EVENT
, "expose: win %#x, count: %d\n", e
->window
,
10010 if ((b
= find_bar(e
->window
))) {
10013 } else if ((w
= find_window(e
->window
)) && w
->frame
== e
->window
) {
10017 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
10023 DNPRINTF(SWM_D_EVENT
, "expose: done\n");
10027 focusin(xcb_focus_in_event_t
*e
)
10029 struct ws_win
*win
;
10031 DNPRINTF(SWM_D_EVENT
, "focusin: win %#x, mode: %s(%u), "
10032 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10033 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10034 if ((win
= find_window(e
->event
)) && win
!= win
->ws
->focus
&&
10035 win
!= win
->ws
->focus_pending
&&
10036 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10037 win
->ws
->focus_prev
= win
->ws
->focus
;
10038 win
->ws
->focus
= win
;
10039 win
->ws
->focus_pending
= NULL
;
10041 if (win
->ws
->focus_prev
)
10042 draw_frame(win
->ws
->focus_prev
);
10050 focusout(xcb_focus_out_event_t
*e
)
10052 DNPRINTF(SWM_D_EVENT
, "focusout: win %#x, mode: %s(%u), "
10053 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10054 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10059 keypress(xcb_key_press_event_t
*e
)
10062 struct binding
*bp
;
10063 xcb_keysym_t keysym
;
10064 bool replay
= true;
10066 last_event_time
= e
->time
;
10068 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
10070 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
10071 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10072 "state: %u, cleaned: %u, same_screen: %s\n", keysym
, e
->event
,
10073 e
->event_x
, e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
,
10074 e
->root_y
, e
->child
, e
->state
, CLEANMASK(e
->state
),
10075 YESNO(e
->same_screen
));
10077 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10079 /* Look for catch-all. */
10080 if ((bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
)) == NULL
)
10084 replay
= bp
->flags
& BINDING_F_REPLAY
;
10086 if ((ap
= &actions
[bp
->action
]) == NULL
)
10089 if (bp
->action
== FN_SPAWN_CUSTOM
)
10090 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10093 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10095 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10099 DNPRINTF(SWM_D_EVENT
, "keypress: replaying.\n");
10100 /* Pass keypress to event window and unfreeze keyboard queue. */
10101 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10103 /* Release freeze. */
10104 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10108 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
10112 keyrelease(xcb_key_release_event_t
*e
)
10115 struct binding
*bp
;
10116 xcb_keysym_t keysym
;
10118 last_event_time
= e
->time
;
10120 keysym
= xcb_key_release_lookup_keysym(syms
, e
, 0);
10122 DNPRINTF(SWM_D_EVENT
, "keyrelease: keysym: %u, win (x,y): %#x (%d,%d), "
10123 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10124 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
10125 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
10126 e
->child
, e
->state
, YESNO(e
->same_screen
));
10128 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10130 /* Look for catch-all. */
10131 bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
);
10133 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10134 bp
->flags
& BINDING_F_REPLAY
) {
10135 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10136 DNPRINTF(SWM_D_EVENT
, "keyrelease: replaying.\n");
10138 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10139 DNPRINTF(SWM_D_EVENT
, "keyrelease: unfreezing.\n");
10144 DNPRINTF(SWM_D_EVENT
, "keyrelease: done.\n");
10148 buttonpress(xcb_button_press_event_t
*e
)
10150 struct ws_win
*win
= NULL
, *newf
;
10151 struct swm_region
*r
, *old_r
;
10153 struct binding
*bp
;
10154 bool replay
= true;
10156 last_event_time
= e
->time
;
10158 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): %#x (%d,%d), "
10159 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10160 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10161 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10162 e
->state
, YESNO(e
->same_screen
));
10164 if (e
->event
== e
->root
) {
10166 win
= find_window(e
->child
);
10168 /* Focus on empty region */
10169 /* If no windows on region if its empty. */
10170 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10171 if (r
&& TAILQ_EMPTY(&r
->ws
->winlist
)) {
10172 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
10173 if (old_r
&& old_r
!= r
)
10174 unfocus_win(old_r
->ws
->focus
);
10176 DNPRINTF(SWM_D_FOCUS
, "buttonpress: "
10177 "set_input_focus: %#x, revert-to: parent, "
10178 "time: %#x\n", e
->root
, e
->time
);
10179 xcb_set_input_focus(conn
,
10180 XCB_INPUT_FOCUS_POINTER_ROOT
,
10183 /* Clear bar since empty. */
10186 /* No need to replay event. */
10191 win
= find_window(e
->event
);
10195 newf
= get_focus_magic(win
);
10196 if (win
->ws
->focus
== newf
&& newf
!= win
) {
10197 if (win
->focus_child
== win
)
10198 win
->focus_child
= NULL
;
10204 /* Handle any bound action. */
10205 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10207 /* Look for catch-all. */
10208 if ((bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
)) == NULL
)
10213 replay
= bp
->flags
& BINDING_F_REPLAY
;
10215 if ((ap
= &actions
[bp
->action
]) == NULL
)
10218 if (bp
->action
== FN_SPAWN_CUSTOM
)
10219 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10222 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10224 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10228 DNPRINTF(SWM_D_EVENT
, "buttonpress: replaying.\n");
10229 /* Replay event to event window */
10230 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10232 /* Unfreeze grab events. */
10233 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10240 buttonrelease(xcb_button_release_event_t
*e
)
10243 struct binding
*bp
;
10245 last_event_time
= e
->time
;
10247 DNPRINTF(SWM_D_EVENT
, "buttonrelease: win (x,y): %#x (%d,%d), "
10248 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10249 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10250 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10251 e
->state
, YESNO(e
->same_screen
));
10253 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10255 /* Look for catch-all. */
10256 bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
);
10258 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10259 bp
->flags
& BINDING_F_REPLAY
) {
10260 DNPRINTF(SWM_D_EVENT
, "buttonrelease: replaying.\n");
10261 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10263 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10271 get_win_input_model(struct ws_win
*win
)
10275 * Input Model Input Field WM_TAKE_FOCUS
10276 * No Input False Absent
10277 * Passive True Absent
10278 * Locally Active True Present
10279 * Globally Active False Present
10282 if (ACCEPTS_FOCUS(win
))
10283 inputmodel
= (win
->take_focus
) ? "Locally Active" : "Passive";
10285 inputmodel
= (win
->take_focus
) ? "Globally Active" : "No Input";
10291 print_win_geom(xcb_window_t w
)
10293 xcb_get_geometry_reply_t
*wa
;
10295 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
10297 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: %#x\n",
10302 DNPRINTF(SWM_D_MISC
, "print_win_geom: win %#x, root: %#x, "
10303 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
10304 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
10313 get_stack_mode_name(uint8_t mode
)
10318 case XCB_STACK_MODE_ABOVE
:
10321 case XCB_STACK_MODE_BELOW
:
10324 case XCB_STACK_MODE_TOP_IF
:
10327 case XCB_STACK_MODE_BOTTOM_IF
:
10330 case XCB_STACK_MODE_OPPOSITE
:
10342 configurerequest(xcb_configure_request_event_t
*e
)
10344 struct ws_win
*win
;
10345 struct swm_region
*r
= NULL
;
10347 uint32_t wc
[7] = {0};
10351 if ((win
= find_window(e
->window
)) == NULL
)
10352 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10356 if (swm_debug
& SWM_D_EVENT
) {
10357 print_win_geom(e
->window
);
10359 DNPRINTF(SWM_D_EVENT
, "configurerequest: win %#x, "
10360 "parent: %#x, new: %s, value_mask: %u { ", e
->window
,
10361 e
->parent
, YESNO(new), e
->value_mask
);
10362 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
10363 DPRINTF("X: %d ", e
->x
);
10364 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
10365 DPRINTF("Y: %d ", e
->y
);
10366 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10367 DPRINTF("W: %u ", e
->width
);
10368 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10369 DPRINTF("H: %u ", e
->height
);
10370 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
10371 DPRINTF("Border: %u ", e
->border_width
);
10372 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
10373 DPRINTF("Sibling: %#x ", e
->sibling
);
10374 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
10375 DPRINTF("StackMode: %s(%u) ",
10376 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
10382 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10383 mask
|= XCB_CONFIG_WINDOW_X
;
10386 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10387 mask
|= XCB_CONFIG_WINDOW_Y
;
10390 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
10391 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
10392 wc
[i
++] = e
->width
;
10394 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
10395 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
10396 wc
[i
++] = e
->height
;
10398 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
10399 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
10400 wc
[i
++] = e
->border_width
;
10402 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
10403 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
10404 wc
[i
++] = e
->sibling
;
10406 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
10407 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
10408 wc
[i
++] = e
->stack_mode
;
10412 xcb_configure_window(conn
, e
->window
, mask
, wc
);
10415 } else if ((!MANUAL(win
) || win
->quirks
& SWM_Q_ANYWHERE
) &&
10416 !FULLSCREEN(win
) && !MAXIMIZED(win
)) {
10419 else if (win
->ws
->old_r
)
10420 r
= win
->ws
->old_r
;
10422 /* windows are centered unless ANYWHERE quirk is set. */
10423 if (win
->quirks
& SWM_Q_ANYWHERE
) {
10424 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10425 win
->g_float
.x
= e
->x
;
10427 win
->g_float
.x
-= X(r
);
10430 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10431 win
->g_float
.y
= e
->y
;
10433 win
->g_float
.y
-= Y(r
);
10437 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10438 win
->g_float
.w
= e
->width
;
10440 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10441 win
->g_float
.h
= e
->height
;
10443 win
->g_floatvalid
= true;
10445 if (!MAXIMIZED(win
) && !FULLSCREEN(win
) &&
10446 (TRANS(win
) || (ABOVE(win
) &&
10447 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]))) {
10448 WIDTH(win
) = win
->g_float
.w
;
10449 HEIGHT(win
) = win
->g_float
.h
;
10452 update_floater(win
);
10455 config_win(win
, e
);
10459 config_win(win
, e
);
10463 config_win(win
, e
);
10467 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
10471 configurenotify(xcb_configure_notify_event_t
*e
)
10473 struct ws_win
*win
;
10475 DNPRINTF(SWM_D_EVENT
, "configurenotify: win %#x, event win: %#x, "
10476 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
10477 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
10478 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
10479 YESNO(e
->override_redirect
));
10481 win
= find_window(e
->window
);
10484 if (font_adjusted
&& win
->ws
->r
) {
10492 destroynotify(xcb_destroy_notify_event_t
*e
)
10494 struct ws_win
*win
;
10495 struct workspace
*ws
;
10497 DNPRINTF(SWM_D_EVENT
, "destroynotify: win %#x\n", e
->window
);
10499 if ((win
= find_window(e
->window
)) == NULL
) {
10500 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10502 /* Window is on unmanaged list. */
10503 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10510 if (win
->frame
== e
->window
) {
10511 DNPRINTF(SWM_D_EVENT
, "destroynotify: frame for win %#x\n",
10513 win
->frame
= XCB_WINDOW_NONE
;
10517 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10518 /* If we were focused, make sure we focus on something else. */
10519 if (win
== ws
->focus
) {
10520 ws
->focus_pending
= get_focus_prev(win
);
10521 if (ws
->focus_pending
== win
)
10522 ws
->focus_pending
= NULL
;
10526 unmanage_window(win
);
10527 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10531 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
10532 if (ws
->focus_pending
) {
10533 focus_win(ws
->focus_pending
);
10534 ws
->focus_pending
= NULL
;
10535 } else if (ws
->focus
== NULL
) {
10536 DNPRINTF(SWM_D_FOCUS
, "destroynotify: set_input_focus: "
10537 "%#x, revert-to: parent, time: 0\n", ws
->r
->id
);
10538 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
10539 ws
->r
->id
, XCB_CURRENT_TIME
);
10546 DNPRINTF(SWM_D_EVENT
, "destroynotify: done.\n");
10551 get_notify_detail_label(uint8_t detail
)
10556 case XCB_NOTIFY_DETAIL_ANCESTOR
:
10557 label
= "Ancestor";
10559 case XCB_NOTIFY_DETAIL_VIRTUAL
:
10562 case XCB_NOTIFY_DETAIL_INFERIOR
:
10563 label
= "Inferior";
10565 case XCB_NOTIFY_DETAIL_NONLINEAR
:
10566 label
= "Nonlinear";
10568 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
10569 label
= "NonlinearVirtual";
10571 case XCB_NOTIFY_DETAIL_POINTER
:
10574 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
10575 label
= "PointerRoot";
10577 case XCB_NOTIFY_DETAIL_NONE
:
10588 get_notify_mode_label(uint8_t mode
)
10593 case XCB_NOTIFY_MODE_NORMAL
:
10596 case XCB_NOTIFY_MODE_GRAB
:
10599 case XCB_NOTIFY_MODE_UNGRAB
:
10602 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
10603 label
= "WhileGrabbed";
10613 get_state_mask_label(uint16_t state
)
10618 case XCB_KEY_BUT_MASK_SHIFT
:
10619 label
= "ShiftMask";
10621 case XCB_KEY_BUT_MASK_LOCK
:
10622 label
= "LockMask";
10624 case XCB_KEY_BUT_MASK_CONTROL
:
10625 label
= "ControlMask";
10627 case XCB_KEY_BUT_MASK_MOD_1
:
10628 label
= "Mod1Mask";
10630 case XCB_KEY_BUT_MASK_MOD_2
:
10631 label
= "Mod2Mask";
10633 case XCB_KEY_BUT_MASK_MOD_3
:
10634 label
= "Mod3Mask";
10636 case XCB_KEY_BUT_MASK_MOD_4
:
10637 label
= "Mod4Mask";
10639 case XCB_KEY_BUT_MASK_MOD_5
:
10640 label
= "Mod5Mask";
10642 case XCB_KEY_BUT_MASK_BUTTON_1
:
10643 label
= "Button1Mask";
10645 case XCB_KEY_BUT_MASK_BUTTON_2
:
10646 label
= "Button2Mask";
10648 case XCB_KEY_BUT_MASK_BUTTON_3
:
10649 label
= "Button3Mask";
10651 case XCB_KEY_BUT_MASK_BUTTON_4
:
10652 label
= "Button4Mask";
10654 case XCB_KEY_BUT_MASK_BUTTON_5
:
10655 label
= "Button5Mask";
10669 enternotify(xcb_enter_notify_event_t
*e
)
10671 struct ws_win
*win
;
10672 struct swm_region
*r
;
10674 last_event_time
= e
->time
;
10676 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): %#x "
10677 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10678 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10679 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10680 get_notify_mode_label(e
->mode
), e
->mode
,
10681 get_notify_detail_label(e
->detail
), e
->detail
,
10682 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10683 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10686 if (e
->event
== e
->root
&& e
->child
== XCB_WINDOW_NONE
&&
10687 e
->mode
== XCB_NOTIFY_MODE_GRAB
&&
10688 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10689 DNPRINTF(SWM_D_EVENT
, "enternotify: grab inferior; ignoring.\n");
10693 if (focus_mode
== SWM_FOCUS_MANUAL
&&
10694 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10695 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
10699 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10700 e
->mode
== XCB_NOTIFY_MODE_UNGRAB
&&
10701 e
->detail
!= XCB_NOTIFY_DETAIL_ANCESTOR
) {
10702 DNPRINTF(SWM_D_EVENT
, "enternotify: ungrab; ignoring.\n");
10706 if ((win
= find_window(e
->event
)) == NULL
) {
10707 if (e
->event
== e
->root
) {
10708 /* If no windows on pointer region, then focus root. */
10709 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10711 DNPRINTF(SWM_D_EVENT
, "enternotify: "
10712 "NULL region; ignoring.\n");
10718 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
10723 if (e
->mode
== XCB_NOTIFY_MODE_NORMAL
&&
10724 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10725 DNPRINTF(SWM_D_EVENT
, "enternotify: entering from "
10726 "inferior; ignoring\n");
10730 focus_win(get_focus_magic(win
));
10733 DNPRINTF(SWM_D_EVENT
, "enternotify: done\n");
10740 leavenotify(xcb_leave_notify_event_t
*e
)
10742 last_event_time
= e
->time
;
10744 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): %#x "
10745 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10746 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10747 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10748 get_notify_mode_label(e
->mode
), e
->mode
,
10749 get_notify_detail_label(e
->detail
), e
->detail
,
10750 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10751 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10757 mapnotify(xcb_map_notify_event_t
*e
)
10759 struct ws_win
*win
, *parent
= NULL
;
10760 struct workspace
*ws
;
10762 DNPRINTF(SWM_D_EVENT
, "mapnotify: win %#x\n", e
->window
);
10764 if ((win
= manage_window(e
->window
, spawn_position
, false)) == NULL
)
10768 if (win
->state
== SWM_WIN_STATE_REPARENTING
)
10771 if (ws
->r
== NULL
) {
10776 /* Need to know if win was mapped due to ws switch. */
10777 if (ws
->state
== SWM_WS_STATE_MAPPED
) {
10778 if (ws
->focus_pending
&& TRANS(ws
->focus_pending
))
10779 parent
= find_window(win
->transient
);
10781 /* If window's parent is maximized, don't clear it. */
10782 if ((parent
== NULL
) || !MAXIMIZED(parent
))
10783 if (clear_maximized(ws
) > 0)
10787 win
->mapped
= true;
10788 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
10790 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
10791 if (ws
->focus_pending
== win
) {
10793 ws
->focus_pending
= NULL
;
10794 center_pointer(ws
->r
);
10800 DNPRINTF(SWM_D_EVENT
, "mapnotify: done\n");
10806 mappingnotify(xcb_mapping_notify_event_t
*e
)
10808 if (e
->request
!= XCB_MAPPING_POINTER
) {
10809 xcb_refresh_keyboard_mapping(syms
, e
);
10817 maprequest(xcb_map_request_event_t
*e
)
10819 struct ws_win
*win
, *w
= NULL
;
10821 DNPRINTF(SWM_D_EVENT
, "maprequest: win %#x\n",
10824 win
= manage_window(e
->window
, spawn_position
, true);
10828 /* The new window should get focus; prepare. */
10829 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10830 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
) && ACCEPTS_FOCUS(win
)) {
10831 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
10832 /* See if other wins of same type are already mapped. */
10833 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
10834 if (w
== win
|| !w
->mapped
)
10837 if (w
->ch
.class_name
&&
10838 win
->ch
.class_name
&&
10839 strcmp(w
->ch
.class_name
,
10840 win
->ch
.class_name
) == 0 &&
10841 w
->ch
.instance_name
&&
10842 win
->ch
.instance_name
&&
10843 strcmp(w
->ch
.instance_name
,
10844 win
->ch
.instance_name
) == 0)
10850 win
->ws
->focus_pending
= get_focus_magic(win
);
10853 /* All windows need to be mapped if they are in the current workspace.*/
10856 /* Ignore EnterNotify to handle the mapnotify without interference. */
10857 if (focus_mode
== SWM_FOCUS_DEFAULT
)
10858 event_drain(XCB_ENTER_NOTIFY
);
10860 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
10864 motionnotify(xcb_motion_notify_event_t
*e
)
10866 struct swm_region
*r
= NULL
;
10867 int i
, num_screens
;
10869 last_event_time
= e
->time
;
10871 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): %#x "
10872 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10873 "child: %#x, same_screen_focus: %s, state: %d\n",
10874 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10875 get_notify_detail_label(e
->detail
), e
->detail
,
10876 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10877 YESNO(e
->same_screen
), e
->state
);
10879 if (focus_mode
== SWM_FOCUS_MANUAL
)
10882 num_screens
= get_screen_count();
10883 for (i
= 0; i
< num_screens
; i
++)
10884 if (screens
[i
].root
== e
->root
)
10887 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10888 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
10889 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
10897 get_atom_name(xcb_atom_t atom
)
10900 #ifdef SWM_DEBUG_ATOM_NAMES
10902 * This should be disabled during most debugging since
10903 * xcb_get_* causes an xcb_flush.
10906 xcb_get_atom_name_reply_t
*r
;
10908 r
= xcb_get_atom_name_reply(conn
,
10909 xcb_get_atom_name(conn
, atom
),
10912 len
= xcb_get_atom_name_name_length(r
);
10914 name
= malloc(len
+ 1);
10916 memcpy(name
, xcb_get_atom_name_name(r
), len
);
10930 propertynotify(xcb_property_notify_event_t
*e
)
10932 struct ws_win
*win
;
10933 struct workspace
*ws
;
10937 name
= get_atom_name(e
->atom
);
10938 DNPRINTF(SWM_D_EVENT
, "propertynotify: win %#x, atom: %s(%u), "
10939 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
10943 last_event_time
= e
->time
;
10945 win
= find_window(e
->window
);
10951 if (e
->atom
== a_state
) {
10952 /* State just changed, make sure it gets focused if mapped. */
10953 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
10954 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
10956 win
->state
== SWM_WIN_STATE_REPARENTED
&&
10957 ws
->focus_pending
== win
) {
10958 focus_win(ws
->focus_pending
);
10959 ws
->focus_pending
= NULL
;
10963 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
10964 e
->atom
== XCB_ATOM_WM_NAME
) {
10966 bar_draw(ws
->r
->bar
);
10967 } else if (e
->atom
== a_prot
) {
10968 get_wm_protocols(win
);
10969 } else if (e
->atom
== XCB_ATOM_WM_NORMAL_HINTS
) {
10970 xcb_icccm_get_wm_normal_hints_reply(conn
,
10971 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
10979 reparentnotify(xcb_reparent_notify_event_t
*e
)
10981 struct ws_win
*win
;
10983 DNPRINTF(SWM_D_EVENT
, "reparentnotify: event: %#x, win %#x, "
10984 "parent: %#x, (x,y): (%u,%u), override_redirect: %u\n",
10985 e
->event
, e
->window
, e
->parent
, e
->x
, e
->y
, e
->override_redirect
);
10987 win
= find_window(e
->window
);
10989 if (win
->state
== SWM_WIN_STATE_REPARENTING
) {
10990 win
->state
= SWM_WIN_STATE_REPARENTED
;
10997 update_window(win
);
10998 update_win_stacking(win
);
10999 } else if (win
->state
== SWM_WIN_STATE_UNPARENTING
) {
11000 win
->state
= SWM_WIN_STATE_UNPARENTED
;
11006 unmapnotify(xcb_unmap_notify_event_t
*e
)
11008 struct ws_win
*win
;
11009 struct workspace
*ws
;
11011 DNPRINTF(SWM_D_EVENT
, "unmapnotify: win %#x\n", e
->window
);
11013 /* If we aren't managing the window, then ignore. */
11014 win
= find_window(e
->window
);
11015 if (win
== NULL
|| win
->id
!= e
->window
) {
11016 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore unmanaged.\n");
11020 /* Do nothing if already withdrawn. */
11021 if (!win
->mapped
&& !ICONIC(win
)) {
11022 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore withdrawn.\n");
11027 win
->mapped
= false;
11029 /* Ignore if reparenting-related. */
11030 if (win
->state
!= SWM_WIN_STATE_REPARENTED
) {
11031 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore not reparented.\n");
11035 /* If win was focused, make sure to focus on something else. */
11036 if (win
== ws
->focus
) {
11037 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
11038 ws
->focus_pending
= get_focus_prev(win
);
11039 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
11040 "focus_pending: %#x\n",
11041 WINID(ws
->focus_pending
));
11049 DNPRINTF(SWM_D_EVENT
, "unmapnotify: iconify.\n");
11050 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
11053 DNPRINTF(SWM_D_EVENT
, "unmapnotify: withdraw.\n");
11054 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
11055 unmanage_window(win
);
11060 /* Update focus if ws is active. */
11061 if (WS_FOCUSED(ws
)) {
11062 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
11063 focus_win(get_pointer_win(ws
->r
->s
->root
));
11064 } else if (ws
->focus_pending
) {
11065 focus_win(ws
->focus_pending
);
11066 ws
->focus_pending
= NULL
;
11067 } else if (ws
->focus
== NULL
) {
11068 DNPRINTF(SWM_D_FOCUS
, "unmapnotify: set_input_focus: "
11069 "%#x, revert-to: parent, time: 0\n",
11071 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11072 ws
->r
->id
, XCB_CURRENT_TIME
);
11076 center_pointer(ws
->r
);
11078 DNPRINTF(SWM_D_EVENT
, "unmapnotify: done.\n");
11083 get_source_type_label(uint32_t type
)
11088 case EWMH_SOURCE_TYPE_NONE
:
11091 case EWMH_SOURCE_TYPE_NORMAL
:
11094 case EWMH_SOURCE_TYPE_OTHER
:
11106 clientmessage(xcb_client_message_event_t
*e
)
11108 struct ws_win
*win
;
11109 struct swm_region
*r
= NULL
;
11112 int num_screens
, i
;
11113 xcb_map_request_event_t mre
;
11117 name
= get_atom_name(e
->type
);
11118 DNPRINTF(SWM_D_EVENT
, "clientmessage: win %#x, atom: %s(%u)\n",
11119 e
->window
, name
, e
->type
);
11123 if (e
->type
== ewmh
[_NET_CURRENT_DESKTOP
].atom
) {
11124 num_screens
= get_screen_count();
11125 for (i
= 0; i
< num_screens
; i
++)
11126 if (screens
[i
].root
== e
->window
) {
11127 r
= screens
[i
].r_focus
;
11131 if (r
&& e
->data
.data32
[0] < (uint32_t)workspace_limit
) {
11132 a
.id
= e
->data
.data32
[0];
11133 switchws(NULL
, r
, &a
);
11138 } else if (e
->type
== ewmh
[_NET_REQUEST_FRAME_EXTENTS
].atom
) {
11139 DNPRINTF(SWM_D_EVENT
,
11140 "clientmessage: set _NET_FRAME_EXTENTS on window.\n");
11141 vals
[0] = vals
[1] = vals
[2] = vals
[3] = border_width
;
11142 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, e
->window
,
11143 a_net_frame_extents
, XCB_ATOM_CARDINAL
, 32, 4, vals
);
11148 win
= find_window(e
->window
);
11150 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11151 /* Manage the window with maprequest. */
11152 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
11153 "unmanaged window.\n");
11154 mre
.window
= e
->window
;
11160 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11161 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW, "
11162 "source_type: %s(%d)\n",
11163 get_source_type_label(e
->data
.data32
[0]),
11164 e
->data
.data32
[0]);
11167 * Allow focus changes that are a result of direct user
11168 * action and from applications that use the old EWMH spec.
11170 if (e
->data
.data32
[0] != EWMH_SOURCE_TYPE_NORMAL
||
11171 win
->quirks
& SWM_Q_OBEYAPPFOCUSREQ
) {
11172 if (WS_FOCUSED(win
->ws
))
11175 win
->ws
->focus_pending
= win
;
11177 } else if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
11178 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
11179 if (win
->can_delete
)
11180 client_msg(win
, a_delete
, 0);
11182 xcb_kill_client(conn
, win
->id
);
11183 } else if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
11184 DNPRINTF(SWM_D_EVENT
,
11185 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
11187 if (e
->data
.data32
[0] & (1<<8)) /* x */
11188 X(win
) = e
->data
.data32
[1];
11189 if (e
->data
.data32
[0] & (1<<9)) /* y */
11190 Y(win
) = e
->data
.data32
[2];
11191 if (e
->data
.data32
[0] & (1<<10)) /* width */
11192 WIDTH(win
) = e
->data
.data32
[3];
11193 if (e
->data
.data32
[0] & (1<<11)) /* height */
11194 HEIGHT(win
) = e
->data
.data32
[4];
11196 update_window(win
);
11198 /* Notify no change was made. */
11199 config_win(win
, NULL
);
11200 /* TODO: Change stack sizes */
11202 } else if (e
->type
== ewmh
[_NET_RESTACK_WINDOW
].atom
) {
11203 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_RESTACK_WINDOW\n");
11204 vals
[0] = e
->data
.data32
[1]; /* Sibling window. */
11205 vals
[1] = e
->data
.data32
[2]; /* Stack mode detail. */
11207 if (win
->frame
!= XCB_WINDOW_NONE
)
11208 xcb_configure_window(conn
, win
->frame
,
11209 XCB_CONFIG_WINDOW_SIBLING
|
11210 XCB_CONFIG_WINDOW_STACK_MODE
, vals
);
11211 } else if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
11212 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
11213 ewmh_change_wm_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
11214 if (e
->data
.data32
[2])
11215 ewmh_change_wm_state(win
, e
->data
.data32
[2],
11216 e
->data
.data32
[0]);
11218 ewmh_update_wm_state(win
);
11220 } else if (e
->type
== ewmh
[_NET_WM_DESKTOP
].atom
) {
11221 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_DESKTOP\n");
11224 win_to_ws(win
, e
->data
.data32
[0], true);
11226 /* Stack source and destination ws, if mapped. */
11227 if (r
!= win
->ws
->r
) {
11233 load_float_geom(win
);
11246 int errcode
= xcb_connection_has_error(conn
);
11247 #ifdef XCB_CONN_ERROR
11250 case XCB_CONN_ERROR
:
11251 s
= "Socket error, pipe error or other stream error.";
11253 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
11254 s
= "Extension not supported.";
11256 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
11257 s
= "Insufficient memory.";
11259 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
11260 s
= "Request length was exceeded.";
11262 case XCB_CONN_CLOSED_PARSE_ERR
:
11263 s
= "Error parsing display string.";
11266 s
= "Unknown error.";
11269 errx(errcode
, "X CONNECTION ERROR: %s", s
);
11272 errx(errcode
, "X CONNECTION ERROR");
11279 int num_screens
, i
;
11280 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
11281 XCB_EVENT_MASK_ENTER_WINDOW
|
11282 XCB_EVENT_MASK_OWNER_GRAB_BUTTON
|
11283 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
11284 XCB_EVENT_MASK_ENTER_WINDOW
|
11285 XCB_EVENT_MASK_LEAVE_WINDOW
|
11286 XCB_EVENT_MASK_BUTTON_PRESS
|
11287 XCB_EVENT_MASK_BUTTON_RELEASE
|
11288 XCB_EVENT_MASK_KEY_PRESS
|
11289 XCB_EVENT_MASK_KEY_RELEASE
|
11290 XCB_EVENT_MASK_PROPERTY_CHANGE
;
11292 xcb_void_cookie_t wac
;
11293 xcb_generic_error_t
*error
;
11295 /* this causes an error if some other window manager is running */
11296 num_screens
= get_screen_count();
11297 for (i
= 0; i
< num_screens
; i
++) {
11298 if ((sc
= get_screen(i
)) == NULL
)
11299 errx(1, "ERROR: can't get screen %d.", i
);
11300 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: %#x\n",
11302 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
11303 XCB_CW_EVENT_MASK
, &val
);
11304 if ((error
= xcb_request_check(conn
, wac
))) {
11305 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
11306 error
->error_code
);
11316 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
11318 struct swm_region
*r
= NULL
, *n
;
11319 struct workspace
*ws
= NULL
;
11323 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
11324 s
->idx
, w
, h
, x
, y
);
11326 /* remove any conflicting regions */
11327 n
= TAILQ_FIRST(&s
->rl
);
11330 n
= TAILQ_NEXT(r
, entry
);
11331 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
11332 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
11333 if (r
->ws
->r
!= NULL
)
11334 r
->ws
->old_r
= r
->ws
->r
;
11337 xcb_destroy_window(conn
, r
->id
);
11338 r
->id
= XCB_WINDOW_NONE
;
11339 TAILQ_REMOVE(&s
->rl
, r
, entry
);
11340 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
11344 /* search old regions for one to reuse */
11346 /* size + location match */
11347 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11348 if (X(r
) == x
&& Y(r
) == y
&&
11349 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11353 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11354 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11358 TAILQ_REMOVE(&s
->orl
, r
, entry
);
11359 /* try to use old region's workspace */
11360 if (r
->ws
->r
== NULL
)
11363 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
11364 err(1, "new_region: calloc: failed to allocate memory "
11367 /* if we don't have a workspace already, find one */
11369 for (i
= 0; i
< workspace_limit
; i
++)
11370 if (s
->ws
[i
].r
== NULL
) {
11377 errx(1, "new_region: no free workspaces");
11379 if (ws
->state
== SWM_WS_STATE_HIDDEN
)
11380 ws
->state
= SWM_WS_STATE_MAPPING
;
11389 r
->ws_prior
= NULL
;
11392 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
11394 /* Invisible region window to detect pointer events on empty regions. */
11395 r
->id
= xcb_generate_id(conn
);
11396 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
11397 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
11399 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
11400 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
11401 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
11403 /* Make sure region input is at the bottom. */
11404 wa
[0] = XCB_STACK_MODE_BELOW
;
11405 xcb_configure_window(conn
, r
->id
, XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
11407 xcb_map_window(conn
, r
->id
);
11411 scan_randr(int idx
)
11413 #ifdef SWM_XRR_HAS_CRTC
11416 #endif /* SWM_XRR_HAS_CRTC */
11417 struct swm_region
*r
;
11418 struct ws_win
*win
;
11420 xcb_randr_get_screen_resources_current_cookie_t src
;
11421 xcb_randr_get_screen_resources_current_reply_t
*srr
;
11422 xcb_randr_get_crtc_info_cookie_t cic
;
11423 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
11424 xcb_randr_crtc_t
*crtc
;
11425 xcb_screen_t
*screen
;
11427 DNPRINTF(SWM_D_MISC
, "scan_randr: screen: %d\n", idx
);
11429 if ((screen
= get_screen(idx
)) == NULL
)
11430 errx(1, "ERROR: can't get screen %d.", idx
);
11432 num_screens
= get_screen_count();
11433 if (idx
>= num_screens
)
11434 errx(1, "scan_randr: invalid screen");
11436 /* remove any old regions */
11437 while ((r
= TAILQ_FIRST(&screens
[idx
].rl
)) != NULL
) {
11438 r
->ws
->old_r
= r
->ws
->r
= NULL
;
11440 xcb_destroy_window(conn
, r
->id
);
11441 r
->id
= XCB_WINDOW_NONE
;
11442 TAILQ_REMOVE(&screens
[idx
].rl
, r
, entry
);
11443 TAILQ_INSERT_TAIL(&screens
[idx
].orl
, r
, entry
);
11447 /* map virtual screens onto physical screens */
11448 #ifdef SWM_XRR_HAS_CRTC
11449 if (randr_support
) {
11450 src
= xcb_randr_get_screen_resources_current(conn
,
11451 screens
[idx
].root
);
11452 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
11455 new_region(&screens
[idx
], 0, 0,
11456 screen
->width_in_pixels
,
11457 screen
->height_in_pixels
);
11460 ncrtc
= srr
->num_crtcs
;
11462 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
11463 for (c
= 0; c
< ncrtc
; c
++) {
11464 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
11466 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
11469 if (cir
->num_outputs
== 0) {
11474 if (cir
->mode
== 0)
11475 new_region(&screens
[idx
], 0, 0,
11476 screen
->width_in_pixels
,
11477 screen
->height_in_pixels
);
11479 new_region(&screens
[idx
],
11480 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
11485 #endif /* SWM_XRR_HAS_CRTC */
11487 /* If detection failed, create a single region that spans the screen. */
11488 if (TAILQ_EMPTY(&screens
[idx
].rl
))
11489 new_region(&screens
[idx
], 0, 0, screen
->width_in_pixels
,
11490 screen
->height_in_pixels
);
11493 /* Cleanup unused previously visible workspaces. */
11494 TAILQ_FOREACH(r
, &screens
[idx
].orl
, entry
) {
11495 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
11497 r
->ws
->state
= SWM_WS_STATE_HIDDEN
;
11499 /* The screen shouldn't focus on an unused region. */
11500 if (screens
[idx
].r_focus
== r
)
11501 screens
[idx
].r_focus
= NULL
;
11504 DNPRINTF(SWM_D_MISC
, "scan_randr: done.\n");
11508 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
11510 struct swm_region
*r
;
11511 int i
, num_screens
;
11513 DNPRINTF(SWM_D_EVENT
, "screenchange: root: %#x\n", e
->root
);
11515 num_screens
= get_screen_count();
11516 /* silly event doesn't include the screen index */
11517 for (i
= 0; i
< num_screens
; i
++)
11518 if (screens
[i
].root
== e
->root
)
11520 if (i
>= num_screens
)
11521 errx(1, "screenchange: screen not found");
11523 /* brute force for now, just re-enumerate the regions */
11527 print_win_geom(e
->root
);
11529 /* add bars to all regions */
11530 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11533 /* Stack all regions. */
11534 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11537 /* Make sure a region has focus. */
11538 if (screens
[i
].r_focus
== NULL
) {
11539 r
= TAILQ_FIRST(&screens
[i
].rl
);
11546 /* Update workspace state and bar on all regions. */
11547 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11548 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
11556 struct swm_region
*r
= NULL
;
11557 xcb_query_tree_cookie_t qtc
;
11558 xcb_query_tree_reply_t
*qtr
;
11559 xcb_get_property_cookie_t pc
;
11560 xcb_get_property_reply_t
*pr
;
11561 xcb_window_t
*wins
= NULL
, trans
, *cwins
= NULL
;
11562 int i
, j
, k
, n
, no
, num_screens
;
11564 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
11565 num_screens
= get_screen_count();
11566 for (i
= 0; i
< num_screens
; i
++) {
11567 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
11568 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
11571 wins
= xcb_query_tree_children(qtr
);
11572 no
= xcb_query_tree_children_length(qtr
);
11574 /* Try to sort windows according to _NET_CLIENT_LIST. */
11575 pr
= xcb_get_property_reply(conn
, xcb_get_property(conn
, 0,
11576 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
11577 XCB_ATOM_WINDOW
, 0, UINT32_MAX
), NULL
);
11579 cwins
= xcb_get_property_value(pr
);
11580 n
= xcb_get_property_value_length(pr
) /
11581 sizeof(xcb_atom_t
);
11583 for (j
= 0; j
< n
; ++j
) {
11584 for (k
= j
; k
< no
; ++k
) {
11585 if (wins
[k
] == cwins
[j
]) {
11586 /* Swap wins j and k. */
11588 wins
[j
] = cwins
[j
];
11596 /* Manage top-level windows first, then transients. */
11597 /* TODO: allow transients to be managed before leader. */
11598 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top-level windows.\n");
11599 for (j
= 0; j
< no
; j
++) {
11600 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11601 if (r
->id
== wins
[j
]) {
11602 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11603 "skip %#x; region input window.\n",
11606 } else if (r
->bar
->id
== wins
[j
]) {
11607 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11608 "skip %#x; region bar.\n",
11617 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11618 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11620 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
11621 "is transient for %#x.\n", wins
[j
], trans
);
11625 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11628 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
11629 for (j
= 0; j
< no
; j
++) {
11630 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11631 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11633 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11637 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
11641 setup_screens(void)
11643 int i
, j
, k
, num_screens
;
11644 struct workspace
*ws
;
11645 uint32_t gcv
[1], wa
[1];
11646 const xcb_query_extension_reply_t
*qep
;
11647 xcb_screen_t
*screen
;
11648 xcb_randr_query_version_cookie_t c
;
11649 xcb_randr_query_version_reply_t
*r
;
11651 num_screens
= get_screen_count();
11652 if ((screens
= calloc(num_screens
, sizeof(struct swm_screen
))) == NULL
)
11653 err(1, "setup_screens: calloc: failed to allocate memory for "
11656 /* Initial RandR setup. */
11657 randr_support
= false;
11658 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
11659 if (qep
->present
) {
11660 c
= xcb_randr_query_version(conn
, 1, 1);
11661 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
11663 if (r
->major_version
>= 1) {
11664 randr_support
= true;
11665 randr_eventbase
= qep
->first_event
;
11671 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
11673 /* map physical screens */
11674 for (i
= 0; i
< num_screens
; i
++) {
11675 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
11676 screens
[i
].idx
= i
;
11677 screens
[i
].r_focus
= NULL
;
11679 TAILQ_INIT(&screens
[i
].rl
);
11680 TAILQ_INIT(&screens
[i
].orl
);
11681 if ((screen
= get_screen(i
)) == NULL
)
11682 errx(1, "ERROR: can't get screen %d.", i
);
11683 screens
[i
].root
= screen
->root
;
11685 /* set default colors */
11686 setscreencolor("red", i
, SWM_S_COLOR_FOCUS
);
11687 setscreencolor("rgb:88/88/88", i
, SWM_S_COLOR_UNFOCUS
);
11688 setscreencolor("rgb:00/80/80", i
, SWM_S_COLOR_BAR_BORDER
);
11689 setscreencolor("rgb:00/40/40", i
,
11690 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
11691 setscreencolor("black", i
, SWM_S_COLOR_BAR
);
11692 setscreencolor("rgb:a0/a0/a0", i
, SWM_S_COLOR_BAR_FONT
);
11693 setscreencolor("red", i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
11694 setscreencolor("rgb:88/88/88", i
,
11695 SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
11697 /* create graphics context on screen */
11698 screens
[i
].bar_gc
= xcb_generate_id(conn
);
11700 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
11701 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
11703 /* set default cursor */
11704 xcb_change_window_attributes(conn
, screens
[i
].root
,
11705 XCB_CW_CURSOR
, wa
);
11707 /* init all workspaces */
11708 /* XXX these should be dynamically allocated too */
11709 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
11710 ws
= &screens
[i
].ws
[j
];
11713 ws
->bar_enabled
= true;
11715 ws
->focus_prev
= NULL
;
11716 ws
->focus_pending
= NULL
;
11719 ws
->state
= SWM_WS_STATE_HIDDEN
;
11720 TAILQ_INIT(&ws
->stack
);
11721 TAILQ_INIT(&ws
->winlist
);
11722 TAILQ_INIT(&ws
->unmanagedlist
);
11724 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
11725 if (layouts
[k
].l_config
!= NULL
)
11726 layouts
[k
].l_config(ws
,
11727 SWM_ARG_ID_STACKINIT
);
11728 ws
->cur_layout
= &layouts
[0];
11729 ws
->cur_layout
->l_string(ws
);
11735 xcb_randr_select_input(conn
, screens
[i
].root
,
11736 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
11741 setup_globals(void)
11743 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
11744 err(1, "setup_globals: strdup: failed to allocate memory.");
11746 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
11747 err(1, "setup_globals: strdup: failed to allocate memory.");
11749 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
11750 errx(1, "unable to allocate key symbols");
11752 a_state
= get_atom_from_string("WM_STATE");
11753 a_prot
= get_atom_from_string("WM_PROTOCOLS");
11754 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
11755 a_net_frame_extents
= get_atom_from_string("_NET_FRAME_EXTENTS");
11756 a_net_supported
= get_atom_from_string("_NET_SUPPORTED");
11757 a_net_wm_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
11758 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
11759 a_utf8_string
= get_atom_from_string("UTF8_STRING");
11760 a_swm_ws
= get_atom_from_string("_SWM_WS");
11764 shutdown_cleanup(void)
11766 struct swm_region
*r
;
11768 struct workspace
*ws
;
11769 int i
, num_screens
;
11771 /* disable alarm because the following code may not be interrupted */
11773 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
11774 err(1, "can't disable alarm");
11787 num_screens
= get_screen_count();
11788 for (i
= 0; i
< num_screens
; ++i
) {
11791 DNPRINTF(SWM_D_FOCUS
, "shutdown_cleanup: set_input_focus: "
11792 "%#x, revert-to: root, time: 0\n", screens
[i
].root
);
11793 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11794 screens
[i
].root
, XCB_CURRENT_TIME
);
11796 if (screens
[i
].bar_gc
!= XCB_NONE
)
11797 xcb_free_gc(conn
, screens
[i
].bar_gc
);
11798 if (!bar_font_legacy
) {
11799 XftColorFree(display
, DefaultVisual(display
, i
),
11800 DefaultColormap(display
, i
), &bar_font_color
);
11801 XftColorFree(display
, DefaultVisual(display
, i
),
11802 DefaultColormap(display
, i
), &search_font_color
);
11805 for (j
= 0; j
< SWM_S_COLOR_MAX
; ++j
) {
11806 free(screens
[i
].c
[j
].name
);
11809 /* Free window memory. */
11810 for (j
= 0; j
< SWM_WS_MAX
; ++j
) {
11811 ws
= &screens
[i
].ws
[j
];
11814 while ((w
= TAILQ_FIRST(&ws
->winlist
)) != NULL
) {
11815 TAILQ_REMOVE(&ws
->winlist
, w
, entry
);
11819 while ((w
= TAILQ_FIRST(&ws
->unmanagedlist
)) != NULL
) {
11820 TAILQ_REMOVE(&ws
->unmanagedlist
, w
, entry
);
11825 /* Free region memory. */
11826 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
11827 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
11832 while ((r
= TAILQ_FIRST(&screens
[i
].orl
)) != NULL
) {
11833 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
11842 free(clock_format
);
11843 free(startup_exception
);
11846 XFreeFontSet(display
, bar_fs
);
11848 XftFontClose(display
, bar_font
);
11850 xcb_key_symbols_free(syms
);
11852 xcb_aux_sync(conn
);
11853 xcb_disconnect(conn
);
11857 event_error(xcb_generic_error_t
*e
)
11861 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
11862 "resource_id: %u, minor_code: %u\n",
11863 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
11864 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
11865 e
->sequence
, e
->resource_id
, e
->minor_code
);
11869 event_handle(xcb_generic_event_t
*evt
)
11871 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
11873 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
11874 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
11875 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
11878 #define EVENT(type, callback) case type: callback((void *)evt); return
11879 EVENT(0, event_error
);
11880 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
11881 EVENT(XCB_BUTTON_RELEASE
, buttonrelease
);
11882 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
11883 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
11884 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
11885 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
11886 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
11887 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
11888 /*EVENT(XCB_CREATE_NOTIFY, );*/
11889 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
11890 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
11891 EVENT(XCB_EXPOSE
, expose
);
11892 EVENT(XCB_FOCUS_IN
, focusin
);
11894 EVENT(XCB_FOCUS_OUT
, focusout
);
11896 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
11897 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
11898 EVENT(XCB_KEY_PRESS
, keypress
);
11899 EVENT(XCB_KEY_RELEASE
, keyrelease
);
11900 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
11902 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
11904 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
11905 EVENT(XCB_MAP_REQUEST
, maprequest
);
11906 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
11907 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
11908 /*EVENT(XCB_NO_EXPOSURE, );*/
11909 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
11910 EVENT(XCB_REPARENT_NOTIFY
, reparentnotify
);
11911 /*EVENT(XCB_RESIZE_REQUEST, );*/
11912 /*EVENT(XCB_SELECTION_CLEAR, );*/
11913 /*EVENT(XCB_SELECTION_NOTIFY, );*/
11914 /*EVENT(XCB_SELECTION_REQUEST, );*/
11915 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
11916 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
11919 if (type
- randr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
11920 screenchange((void *)evt
);
11924 main(int argc
, char *argv
[])
11926 struct pollfd pfd
[2];
11927 struct sigaction sact
;
11929 struct passwd
*pwd
;
11930 struct swm_region
*r
;
11931 xcb_generic_event_t
*evt
;
11932 int xfd
, i
, num_screens
, num_readable
;
11933 char conf
[PATH_MAX
], *cfile
= NULL
;
11934 bool stdin_ready
= false, startup
= true;
11936 /* suppress unused warning since var is needed */
11940 time_started
= time(NULL
);
11944 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
11945 if (setlocale(LC_CTYPE
, "") == NULL
|| setlocale(LC_TIME
, "") == NULL
)
11946 warnx("no locale support");
11948 /* handle some signals */
11949 bzero(&sact
, sizeof(sact
));
11950 sigemptyset(&sact
.sa_mask
);
11952 sact
.sa_handler
= sighdlr
;
11953 sigaction(SIGINT
, &sact
, NULL
);
11954 sigaction(SIGQUIT
, &sact
, NULL
);
11955 sigaction(SIGTERM
, &sact
, NULL
);
11956 sigaction(SIGHUP
, &sact
, NULL
);
11958 sact
.sa_handler
= sighdlr
;
11959 sact
.sa_flags
= SA_NOCLDSTOP
;
11960 sigaction(SIGCHLD
, &sact
, NULL
);
11962 if ((display
= XOpenDisplay(0)) == NULL
)
11963 errx(1, "can not open display");
11965 conn
= XGetXCBConnection(display
);
11966 if (xcb_connection_has_error(conn
))
11967 errx(1, "can not get XCB connection");
11969 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
11971 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
11972 xfd
= xcb_get_file_descriptor(conn
);
11974 /* look for local and global conf file */
11975 pwd
= getpwuid(getuid());
11977 errx(1, "invalid user: %d", getuid());
11979 xcb_grab_server(conn
);
11980 xcb_aux_sync(conn
);
11982 /* flush all events */
11983 while ((evt
= get_next_event(false))) {
11984 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
11990 errx(1, "another window manager is currently running");
11992 /* Load Xcursors and/or cursorfont glyph cursors. */
11995 xcb_aux_sync(conn
);
12000 setup_keybindings();
12001 setup_btnbindings();
12006 for (i
= 0; ; i
++) {
12011 snprintf(conf
, sizeof conf
, "%s/.%s",
12012 pwd
->pw_dir
, SWM_CONF_FILE
);
12016 snprintf(conf
, sizeof conf
, "/etc/%s",
12021 snprintf(conf
, sizeof conf
, "%s/.%s",
12022 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
12025 /* global compat */
12026 snprintf(conf
, sizeof conf
, "/etc/%s",
12027 SWM_CONF_FILE_OLD
);
12033 if (strlen(conf
) && stat(conf
, &sb
) != -1)
12034 if (S_ISREG(sb
.st_mode
)) {
12041 /* load conf (if any) */
12043 conf_load(cfile
, SWM_CONF_DEFAULT
);
12047 if (getenv("SWM_STARTED") == NULL
)
12048 setenv("SWM_STARTED", "YES", 1);
12050 /* setup all bars */
12051 num_screens
= get_screen_count();
12052 for (i
= 0; i
< num_screens
; i
++)
12053 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12056 /* Manage existing windows. */
12062 /* Stack all regions to trigger mapping. */
12063 for (i
= 0; i
< num_screens
; i
++)
12064 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12067 xcb_ungrab_server(conn
);
12070 /* Update state and bar of each newly mapped workspace. */
12071 for (i
= 0; i
< num_screens
; i
++)
12072 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
12073 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
12077 memset(&pfd
, 0, sizeof(pfd
));
12079 pfd
[0].events
= POLLIN
;
12080 pfd
[1].fd
= STDIN_FILENO
;
12081 pfd
[1].events
= POLLIN
;
12084 while ((evt
= get_next_event(false))) {
12091 /* If just (re)started, set default focus if needed. */
12095 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
12096 r
= TAILQ_FIRST(&screens
[0].rl
);
12108 num_readable
= poll(pfd
, bar_extra
? 2 : 1, 1000);
12109 if (num_readable
== -1) {
12110 DNPRINTF(SWM_D_MISC
, "poll failed: %s",
12112 } else if (num_readable
> 0 && bar_extra
&&
12113 pfd
[1].revents
& POLLIN
) {
12114 stdin_ready
= true;
12118 restart(NULL
, NULL
, NULL
);
12124 stdin_ready
= false;
12125 bar_extra_update();
12128 /* Need to ensure the bar(s) are always updated. */
12129 for (i
= 0; i
< num_screens
; i
++)
12130 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12136 shutdown_cleanup();