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;
4174 if (wsid
>= workspace_limit
)
4178 old_ws
= this_r
->ws
;
4179 new_ws
= &this_r
->s
->ws
[wsid
];
4181 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
4182 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
4184 if (new_ws
== NULL
|| old_ws
== NULL
)
4186 if (new_ws
== old_ws
)
4189 other_r
= new_ws
->r
;
4190 if (other_r
&& workspace_clamp
) {
4191 DNPRINTF(SWM_D_WS
, "switchws: ws clamped.\n");
4194 DNPRINTF(SWM_D_WS
, "switchws: warping focus to region "
4195 "with ws %d.\n", wsid
);
4196 focus_region(other_r
);
4197 center_pointer(other_r
);
4203 if ((win
= old_ws
->focus
) != NULL
) {
4206 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4207 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4212 /* the other ws is visible in another region, exchange them */
4213 other_r
->ws_prior
= new_ws
;
4214 other_r
->ws
= old_ws
;
4215 old_ws
->r
= other_r
;
4217 /* the other workspace is hidden, hide this one */
4222 this_r
->ws_prior
= old_ws
;
4223 this_r
->ws
= new_ws
;
4226 /* Set focus_pending before stacking, if needed. */
4227 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
4228 validate_win(new_ws
->focus_pending
))) {
4229 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
4230 new_ws
->focus
= new_ws
->focus_prev
;
4231 new_ws
->focus_prev
= NULL
;
4234 new_ws
->state
= SWM_WS_STATE_MAPPING
;
4239 /* unmap old windows */
4241 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
4243 old_ws
->state
= SWM_WS_STATE_HIDDEN
;
4246 /* if workspaces were swapped, then don't wait to set focus */
4247 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
4248 if (new_ws
->focus_pending
) {
4249 focus_win(new_ws
->focus_pending
);
4250 new_ws
->focus_pending
= NULL
;
4254 /* Clear bar and set focus on region input win if new ws is empty. */
4255 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
4256 DNPRINTF(SWM_D_FOCUS
, "switchws: set_input_focus: %#x, "
4257 "revert-to: parent, time: 0\n", r
->id
);
4258 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4263 ewmh_update_current_desktop();
4267 new_ws
->state
= SWM_WS_STATE_MAPPED
;
4269 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
4273 cyclews(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4276 struct swm_screen
*s
= r
->s
;
4277 bool cycle_all
= false, mv
= false;
4279 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4280 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4286 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
4289 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
4292 case SWM_ARG_ID_CYCLEWS_UP
:
4293 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
4295 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
4298 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
4301 case SWM_ARG_ID_CYCLEWS_DOWN
:
4302 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
4309 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
4311 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
4315 send_to_ws(bp
, r
, &a
);
4317 switchws(bp
, r
, &a
);
4318 } while (a
.id
!= r
->ws
->idx
);
4320 DNPRINTF(SWM_D_FOCUS
, "cyclews: done\n");
4324 priorws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4330 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4331 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4333 if (r
->ws_prior
== NULL
)
4336 a
.id
= r
->ws_prior
->idx
;
4337 switchws(bp
, r
, &a
);
4338 DNPRINTF(SWM_D_FOCUS
, "priorws: done\n");
4342 focusrg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4344 int ridx
= args
->id
, i
, num_screens
;
4345 struct swm_region
*rr
= NULL
;
4349 num_screens
= get_screen_count();
4350 /* do nothing if we don't have more than one screen */
4351 if (!(num_screens
> 1 || outputs
> 1))
4354 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
4356 rr
= TAILQ_FIRST(&r
->s
->rl
);
4357 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
4358 rr
= TAILQ_NEXT(rr
, entry
);
4366 DNPRINTF(SWM_D_FOCUS
, "focusrg: done\n");
4370 cyclerg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4373 struct swm_region
*rr
= NULL
;
4376 num_screens
= get_screen_count();
4377 /* do nothing if we don't have more than one screen */
4378 if (!(num_screens
> 1 || outputs
> 1))
4382 DNPRINTF(SWM_D_FOCUS
, "cyclerg: id: %d, region: %d\n", args
->id
, i
);
4385 case SWM_ARG_ID_CYCLERG_UP
:
4386 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4387 rr
= TAILQ_NEXT(r
, entry
);
4389 rr
= TAILQ_FIRST(&screens
[i
].rl
);
4391 case SWM_ARG_ID_CYCLERG_DOWN
:
4392 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4393 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
4395 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
4404 case SWM_ARG_ID_CYCLERG_UP
:
4405 case SWM_ARG_ID_CYCLERG_DOWN
:
4410 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4411 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4413 switchws(bp
, r
, &a
);
4419 DNPRINTF(SWM_D_FOCUS
, "cyclerg: done\n");
4422 /* Sorts transients after parent. */
4424 sort_windows(struct ws_win_list
*wl
)
4426 struct ws_win
*win
, *parent
, *nxt
;
4431 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
4432 nxt
= TAILQ_NEXT(win
, entry
);
4434 parent
= find_window(win
->transient
);
4435 if (parent
== NULL
) {
4436 warnx("not possible bug");
4439 TAILQ_REMOVE(wl
, win
, entry
);
4440 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
4446 swapwin(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4448 struct ws_win
*target
, *source
;
4449 struct ws_win
*cur_focus
;
4450 struct ws_win_list
*wl
;
4454 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4455 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4457 cur_focus
= r
->ws
->focus
;
4458 if (cur_focus
== NULL
|| FULLSCREEN(cur_focus
))
4461 /* Adjust stacking in floating layer. */
4462 if (ABOVE(cur_focus
)) {
4464 case SWM_ARG_ID_SWAPPREV
:
4465 target
= TAILQ_PREV(cur_focus
, ws_win_stack
,
4467 if (target
!= NULL
&& FLOATING(target
)) {
4468 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4470 TAILQ_INSERT_BEFORE(target
, cur_focus
,
4472 update_win_stacking(cur_focus
);
4476 case SWM_ARG_ID_SWAPNEXT
:
4477 target
= TAILQ_NEXT(cur_focus
, stack_entry
);
4478 if (target
!= NULL
&& FLOATING(target
)) {
4479 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4481 TAILQ_INSERT_AFTER(&cur_focus
->ws
->stack
,
4482 target
, cur_focus
, stack_entry
);
4483 update_win_stacking(cur_focus
);
4491 if (r
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4494 clear_maximized(r
->ws
);
4497 wl
= &source
->ws
->winlist
;
4500 case SWM_ARG_ID_SWAPPREV
:
4502 source
= find_window(source
->transient
);
4503 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
4504 if (target
&& target
->transient
)
4505 target
= find_window(target
->transient
);
4506 TAILQ_REMOVE(wl
, source
, entry
);
4508 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4510 TAILQ_INSERT_BEFORE(target
, source
, entry
);
4512 case SWM_ARG_ID_SWAPNEXT
:
4513 target
= TAILQ_NEXT(source
, entry
);
4514 /* move the parent and let the sort handle the move */
4516 source
= find_window(source
->transient
);
4517 TAILQ_REMOVE(wl
, source
, entry
);
4519 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4521 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
4523 case SWM_ARG_ID_SWAPMAIN
:
4524 target
= TAILQ_FIRST(wl
);
4525 if (target
== source
) {
4526 if (source
->ws
->focus_prev
!= NULL
&&
4527 source
->ws
->focus_prev
!= target
)
4528 source
= source
->ws
->focus_prev
;
4532 if (target
== NULL
|| source
== NULL
)
4534 source
->ws
->focus_prev
= target
;
4535 TAILQ_REMOVE(wl
, target
, entry
);
4536 TAILQ_INSERT_BEFORE(source
, target
, entry
);
4537 TAILQ_REMOVE(wl
, source
, entry
);
4538 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4540 case SWM_ARG_ID_MOVELAST
:
4541 TAILQ_REMOVE(wl
, source
, entry
);
4542 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4545 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
4550 ewmh_update_client_list();
4556 DNPRINTF(SWM_D_MOVE
, "swapwin: done\n");
4560 get_focus_prev(struct ws_win
*win
)
4562 struct ws_win
*winfocus
= NULL
;
4563 struct ws_win
*cur_focus
= NULL
;
4564 struct ws_win_list
*wl
= NULL
;
4565 struct workspace
*ws
= NULL
;
4567 if (!(win
&& win
->ws
))
4572 cur_focus
= ws
->focus
;
4574 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: win %#x, cur_focus: %#x, "
4575 "focus_prev: %#x\n", WINID(win
), WINID(cur_focus
),
4576 WINID(ws
->focus_prev
));
4578 /* pickle, just focus on whatever */
4579 if (cur_focus
== NULL
) {
4580 /* use prev_focus if valid */
4581 if (ws
->focus_prev
&& find_window(ws
->focus_prev
->id
))
4582 winfocus
= ws
->focus_prev
;
4586 /* if transient focus on parent */
4587 if (TRANS(cur_focus
)) {
4588 winfocus
= find_window(cur_focus
->transient
);
4592 /* if in max_stack try harder */
4593 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
4594 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
4595 if (cur_focus
!= ws
->focus_prev
)
4596 winfocus
= ws
->focus_prev
;
4598 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
4603 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
4605 if (winfocus
== NULL
|| winfocus
== win
) {
4606 switch (focus_close
) {
4607 case SWM_STACK_BOTTOM
:
4608 TAILQ_FOREACH(winfocus
, wl
, entry
)
4609 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4613 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4614 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4617 case SWM_STACK_ABOVE
:
4618 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
4619 while (winfocus
&& ICONIC(winfocus
))
4620 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4622 if (winfocus
== NULL
) {
4623 if (focus_close_wrap
) {
4624 TAILQ_FOREACH(winfocus
, wl
, entry
)
4625 if (!ICONIC(winfocus
) &&
4626 winfocus
!= cur_focus
)
4629 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4631 if (!ICONIC(winfocus
) &&
4632 winfocus
!= cur_focus
)
4637 case SWM_STACK_BELOW
:
4638 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
4639 while (winfocus
&& ICONIC(winfocus
))
4640 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
4643 if (winfocus
== NULL
) {
4644 if (focus_close_wrap
) {
4645 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4647 if (!ICONIC(winfocus
) &&
4648 winfocus
!= cur_focus
)
4651 TAILQ_FOREACH(winfocus
, wl
, entry
)
4652 if (!ICONIC(winfocus
) &&
4653 winfocus
!= cur_focus
)
4661 if (winfocus
== NULL
||
4662 (winfocus
&& (ICONIC(winfocus
) || winfocus
== cur_focus
))) {
4663 if (focus_default
== SWM_STACK_TOP
) {
4664 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4665 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4668 TAILQ_FOREACH(winfocus
, wl
, entry
)
4669 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4676 return get_focus_magic(winfocus
);
4680 get_region_focus(struct swm_region
*r
)
4682 struct ws_win
*winfocus
= NULL
;
4687 if (r
->ws
->focus
&& !ICONIC(r
->ws
->focus
))
4688 winfocus
= r
->ws
->focus
;
4689 else if (r
->ws
->focus_prev
&& !ICONIC(r
->ws
->focus_prev
))
4690 winfocus
= r
->ws
->focus_prev
;
4692 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
4693 if (!ICONIC(winfocus
))
4696 return get_focus_magic(winfocus
);
4700 focus(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4702 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
4703 struct ws_win_list
*wl
= NULL
;
4704 struct workspace
*ws
= NULL
;
4711 cur_focus
= r
->ws
->focus
;
4715 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d, cur_focus: %#x\n", args
->id
,
4718 /* Make sure an uniconified window has focus, if one exists. */
4719 if (cur_focus
== NULL
) {
4720 cur_focus
= TAILQ_FIRST(wl
);
4721 while (cur_focus
!= NULL
&& ICONIC(cur_focus
))
4722 cur_focus
= TAILQ_NEXT(cur_focus
, entry
);
4724 DNPRINTF(SWM_D_FOCUS
, "focus: new cur_focus: %#x\n",
4729 case SWM_ARG_ID_FOCUSPREV
:
4730 if (cur_focus
== NULL
)
4733 winfocus
= cur_focus
;
4735 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
4736 if (winfocus
== NULL
)
4737 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
4738 if (winfocus
== cur_focus
)
4740 } while (winfocus
&& (ICONIC(winfocus
) ||
4741 winfocus
->id
== cur_focus
->transient
||
4742 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4743 winfocus
->transient
== cur_focus
->transient
) ||
4744 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4746 case SWM_ARG_ID_FOCUSNEXT
:
4747 if (cur_focus
== NULL
)
4750 winfocus
= cur_focus
;
4752 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4753 if (winfocus
== NULL
)
4754 winfocus
= TAILQ_FIRST(wl
);
4755 if (winfocus
== cur_focus
)
4757 } while (winfocus
&& (ICONIC(winfocus
) ||
4758 winfocus
->id
== cur_focus
->transient
||
4759 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4760 winfocus
->transient
== cur_focus
->transient
) ||
4761 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4763 case SWM_ARG_ID_FOCUSMAIN
:
4764 if (cur_focus
== NULL
)
4767 winfocus
= TAILQ_FIRST(wl
);
4768 if (winfocus
== cur_focus
)
4769 winfocus
= cur_focus
->ws
->focus_prev
;
4771 case SWM_ARG_ID_FOCUSURGENT
:
4772 /* Search forward for the next urgent window. */
4776 for (i
= 0; i
<= workspace_limit
; ++i
) {
4778 head
= TAILQ_FIRST(&r
->s
->ws
[(ws
->idx
+ i
) %
4779 workspace_limit
].winlist
);
4782 if (head
== cur_focus
) {
4784 winfocus
= cur_focus
;
4787 } else if (get_urgent(head
)) {
4792 head
= TAILQ_NEXT(head
, entry
);
4799 /* Switch ws if new focus is on a different ws. */
4800 if (winfocus
&& winfocus
->ws
!= ws
) {
4801 a
.id
= winfocus
->ws
->idx
;
4802 switchws(bp
, r
, &a
);
4809 if (clear_maximized(ws
) > 0)
4812 focus_win(get_focus_magic(winfocus
));
4817 DNPRINTF(SWM_D_FOCUS
, "focus: done\n");
4821 focus_pointer(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4825 /* Not needed for buttons since this is already done in buttonpress. */
4826 if (bp
->type
== KEYBIND
) {
4827 focus_win(get_pointer_win(r
->s
->root
));
4833 cycle_layout(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4835 struct workspace
*ws
= r
->ws
;
4837 /* suppress unused warning since var is needed */
4841 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
4844 if (ws
->cur_layout
->l_stack
== NULL
)
4845 ws
->cur_layout
= &layouts
[0];
4847 clear_maximized(ws
);
4852 focus_win(get_region_focus(r
));
4859 stack_config(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4861 struct workspace
*ws
= r
->ws
;
4865 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
4868 if (clear_maximized(ws
) > 0)
4871 if (ws
->cur_layout
->l_config
!= NULL
)
4872 ws
->cur_layout
->l_config(ws
, args
->id
);
4874 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
4883 stack(struct swm_region
*r
) {
4884 struct swm_geometry g
;
4885 struct swm_region
*r_prev
;
4891 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
4893 /* Adjust stack area for region bar and padding. */
4895 g
.x
+= region_padding
;
4896 g
.y
+= region_padding
;
4897 g
.w
-= 2 * border_width
+ 2 * region_padding
;
4898 g
.h
-= 2 * border_width
+ 2 * region_padding
;
4899 if (bar_enabled
&& r
->ws
->bar_enabled
) {
4905 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d (screen: "
4906 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4907 r
->ws
->idx
, r
->s
->idx
, get_region_index(r
), g
.x
, g
.y
, g
.w
, g
.h
);
4909 r_prev
= TAILQ_PREV(r
, swm_region_list
, entry
);
4911 /* Stack bar/input relative to prev. region. */
4912 val
[1] = XCB_STACK_MODE_ABOVE
;
4914 val
[0] = r_prev
->id
;
4915 DNPRINTF(SWM_D_STACK
, "stack: region input %#x "
4916 "relative to %#x.\n", r
->id
, val
[0]);
4917 xcb_configure_window(conn
, r
->id
,
4918 XCB_CONFIG_WINDOW_SIBLING
|
4919 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4921 val
[0] = r_prev
->bar
->id
;
4922 DNPRINTF(SWM_D_STACK
, "stack: region bar %#x "
4923 "relative to %#x.\n", r
->bar
->id
, val
[0]);
4924 xcb_configure_window(conn
, r
->bar
->id
,
4925 XCB_CONFIG_WINDOW_SIBLING
|
4926 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4929 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
4930 r
->ws
->cur_layout
->l_string(r
->ws
);
4931 /* save r so we can track region changes */
4937 DNPRINTF(SWM_D_STACK
, "stack: end\n");
4941 store_float_geom(struct ws_win
*win
)
4943 if (win
== NULL
|| win
->ws
->r
== NULL
)
4946 /* retain window geom and region geom */
4947 win
->g_float
= win
->g
;
4948 win
->g_float
.x
-= X(win
->ws
->r
);
4949 win
->g_float
.y
-= Y(win
->ws
->r
);
4950 win
->g_floatvalid
= true;
4951 DNPRINTF(SWM_D_MISC
, "store_float_geom: win %#x, g: (%d,%d)"
4952 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
4953 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
4954 win
->g_float
.w
, win
->g_float
.h
);
4958 load_float_geom(struct ws_win
*win
)
4960 if (win
== NULL
|| win
->ws
->r
== NULL
)
4963 if (win
->g_floatvalid
) {
4964 win
->g
= win
->g_float
;
4965 X(win
) += X(win
->ws
->r
);
4966 Y(win
) += Y(win
->ws
->r
);
4967 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g: (%d,%d)"
4968 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
4971 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g_float "
4972 "is not set.\n", win
->id
);
4977 update_floater(struct ws_win
*win
)
4979 struct workspace
*ws
;
4980 struct swm_region
*r
;
4982 DNPRINTF(SWM_D_MISC
, "update_floater: win %#x\n", WINID(win
));
4989 if ((r
= ws
->r
) == NULL
)
4992 win
->bordered
= true;
4994 if (FULLSCREEN(win
)) {
4995 /* _NET_WM_FULLSCREEN: fullscreen without border. */
4996 if (!win
->g_floatvalid
)
4997 store_float_geom(win
);
5000 win
->bordered
= false;
5001 } else if (MAXIMIZED(win
)) {
5002 /* Maximize: like a single stacked window. */
5003 if (!win
->g_floatvalid
)
5004 store_float_geom(win
);
5008 if (bar_enabled
&& ws
->bar_enabled
&& !maximize_hide_bar
) {
5010 Y(win
) += bar_height
;
5011 HEIGHT(win
) -= bar_height
;
5012 } else if (disable_border
) {
5013 win
->bordered
= false;
5016 if (win
->bordered
) {
5017 /* Window geometry excludes frame. */
5018 X(win
) += border_width
;
5019 Y(win
) += border_width
;
5020 HEIGHT(win
) -= 2 * border_width
;
5021 WIDTH(win
) -= 2 * border_width
;
5024 /* Normal floating window. */
5025 /* Update geometry if new region. */
5027 load_float_geom(win
);
5029 if (((win
->quirks
& SWM_Q_FULLSCREEN
) &&
5030 WIDTH(win
) >= WIDTH(r
) && HEIGHT(win
) >= HEIGHT(r
)) ||
5031 ((!WS_FOCUSED(win
->ws
) || win
->ws
->focus
!= win
) &&
5032 (win
->quirks
& SWM_Q_MINIMALBORDER
))) {
5034 win
->bordered
= false;
5035 } else if (!MANUAL(win
)) {
5036 if (TRANS(win
) && (win
->quirks
& SWM_Q_TRANSSZ
)) {
5037 /* Adjust size on TRANSSZ quirk. */
5038 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
5039 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
5042 if (!(win
->quirks
& SWM_Q_ANYWHERE
)) {
5044 * Floaters and transients are auto-centred
5045 * unless manually moved, resized or ANYWHERE
5048 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2;
5049 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2;
5050 store_float_geom(win
);
5055 /* Ensure at least 1 pixel of the window is in the region. */
5056 region_containment(win
, r
, SWM_CW_ALLSIDES
);
5062 * Send keystrokes to terminal to decrease/increase the font size as the
5063 * window size changes.
5066 adjust_font(struct ws_win
*win
)
5068 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
5069 ABOVE(win
) || TRANS(win
))
5072 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
5073 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
5074 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
5075 win
->font_size_boundary
[win
->font_steps
] =
5076 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
5079 win
->last_inc
= win
->sh
.width_inc
;
5080 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
5081 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
5082 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
5085 win
->last_inc
= win
->sh
.width_inc
;
5086 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
5090 #define SWAPXY(g) do { \
5092 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
5093 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
5096 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, bool flip
)
5098 struct swm_geometry cell
, r_g
= *g
;
5100 int i
= 0, j
= 0, s
= 0, stacks
= 0;
5101 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
5102 int hrh
= 0, extra
= 0, h_slice
= 0, last_h
= 0;
5103 int split
= 0, colno
= 0;
5104 int winno
, mwin
= 0, msize
= 0;
5105 int remain
, missing
, v_slice
, mscale
;
5106 bool bordered
= true, reconfigure
= false;
5109 * cell: geometry for window, including frame.
5110 * mwin: # of windows in master area.
5111 * mscale: size increment of master area.
5112 * stacks: # of stack columns
5115 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
5116 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
5118 memset(&cell
, 0, sizeof(cell
));
5120 /* Prepare tiling variables, if needed. */
5121 if ((winno
= count_win(ws
, false)) > 0) {
5122 /* Find first tiled window. */
5123 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
5124 if (!FLOATING(win
) && !ICONIC(win
))
5127 /* Take into account size hints of first tiled window. */
5129 w_inc
= win
->sh
.width_inc
;
5130 w_base
= win
->sh
.base_width
;
5131 mwin
= ws
->l_state
.horizontal_mwin
;
5132 mscale
= ws
->l_state
.horizontal_msize
;
5133 stacks
= ws
->l_state
.horizontal_stacks
;
5136 w_inc
= win
->sh
.height_inc
;
5137 w_base
= win
->sh
.base_height
;
5138 mwin
= ws
->l_state
.vertical_mwin
;
5139 mscale
= ws
->l_state
.vertical_msize
;
5140 stacks
= ws
->l_state
.vertical_stacks
;
5144 cell
.x
+= border_width
;
5145 cell
.y
+= border_width
;
5147 if (stacks
> winno
- mwin
)
5148 stacks
= winno
- mwin
;
5152 h_slice
= r_g
.h
/ SWM_H_SLICE
;
5153 if (mwin
&& winno
> mwin
) {
5154 v_slice
= r_g
.w
/ SWM_V_SLICE
;
5158 cell
.w
= v_slice
* mscale
;
5160 if (w_inc
> 1 && w_inc
< v_slice
) {
5161 /* Adjust for requested size increment. */
5162 remain
= (cell
.w
- w_base
) % w_inc
;
5168 cell
.x
+= r_g
.w
- msize
;
5171 colno
= split
= winno
/ stacks
;
5172 cell
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
5173 2 * border_width
) / stacks
);
5176 hrh
= r_g
.h
/ colno
;
5177 extra
= r_g
.h
- (colno
* hrh
);
5178 cell
.h
= hrh
- 2 * border_width
;
5179 i
= j
= 0, s
= stacks
;
5182 /* Update window geometry. */
5183 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
5187 if (FLOATING(win
)) {
5188 update_floater(win
);
5193 if (split
&& i
== split
) {
5194 colno
= (winno
- mwin
) / stacks
;
5195 if (s
<= (winno
- mwin
) % stacks
)
5198 hrh
= r_g
.h
/ colno
;
5199 extra
= r_g
.h
- (colno
* hrh
);
5202 cell
.x
+= cell
.w
+ 2 * border_width
+
5205 cell
.w
= (r_g
.w
- msize
-
5206 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
5208 cell
.w
+= (r_g
.w
- msize
-
5209 (stacks
* (2 * border_width
+ tile_gap
))) %
5213 cell
.x
-= cell
.w
+ 2 * border_width
+
5219 cell
.h
= hrh
- 2 * border_width
- tile_gap
;
5222 h_inc
= win
->sh
.width_inc
;
5223 h_base
= win
->sh
.base_width
;
5225 h_inc
= win
->sh
.height_inc
;
5226 h_base
= win
->sh
.base_height
;
5229 if (j
== colno
- 1) {
5230 cell
.h
= hrh
+ extra
;
5231 } else if (h_inc
> 1 && h_inc
< h_slice
) {
5232 /* adjust for window's requested size increment */
5233 remain
= (cell
.h
- h_base
) % h_inc
;
5234 missing
= h_inc
- remain
;
5236 if (missing
<= extra
|| j
== 0) {
5246 cell
.y
= r_g
.y
+ border_width
;
5248 cell
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
5250 /* Window coordinates exclude frame. */
5252 if (winno
> 1 || !disable_border
||
5253 (bar_enabled
&& ws
->bar_enabled
)) {
5260 if (X(win
) != cell
.y
|| Y(win
) != cell
.x
||
5261 WIDTH(win
) != cell
.h
|| HEIGHT(win
) != cell
.w
) {
5265 WIDTH(win
) = cell
.h
;
5266 HEIGHT(win
) = cell
.w
;
5269 if (X(win
) != cell
.x
|| Y(win
) != cell
.y
||
5270 WIDTH(win
) != cell
.w
|| HEIGHT(win
) != cell
.h
) {
5274 WIDTH(win
) = cell
.w
;
5275 HEIGHT(win
) = cell
.h
;
5280 X(win
) -= border_width
;
5281 Y(win
) -= border_width
;
5282 WIDTH(win
) += 2 * border_width
;
5283 HEIGHT(win
) += 2 * border_width
;
5286 if (bordered
!= win
->bordered
) {
5288 win
->bordered
= bordered
;
5301 /* Stack all windows from bottom up. */
5302 TAILQ_FOREACH_REVERSE(win
, &ws
->stack
, ws_win_stack
, stack_entry
)
5304 update_win_stacking(win
);
5306 /* Map all windows from top down. */
5307 TAILQ_FOREACH(win
, &ws
->stack
, stack_entry
)
5311 DNPRINTF(SWM_D_STACK
, "stack_master: done\n");
5315 vertical_config(struct workspace
*ws
, int id
)
5317 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
5321 case SWM_ARG_ID_STACKRESET
:
5322 case SWM_ARG_ID_STACKINIT
:
5323 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
5324 ws
->l_state
.vertical_mwin
= 1;
5325 ws
->l_state
.vertical_stacks
= 1;
5327 case SWM_ARG_ID_MASTERSHRINK
:
5328 if (ws
->l_state
.vertical_msize
> 1)
5329 ws
->l_state
.vertical_msize
--;
5331 case SWM_ARG_ID_MASTERGROW
:
5332 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
5333 ws
->l_state
.vertical_msize
++;
5335 case SWM_ARG_ID_MASTERADD
:
5336 ws
->l_state
.vertical_mwin
++;
5338 case SWM_ARG_ID_MASTERDEL
:
5339 if (ws
->l_state
.vertical_mwin
> 0)
5340 ws
->l_state
.vertical_mwin
--;
5342 case SWM_ARG_ID_STACKBALANCE
:
5343 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ (ws
->l_state
.vertical_stacks
+ 1);
5345 case SWM_ARG_ID_STACKINC
:
5346 ws
->l_state
.vertical_stacks
++;
5348 case SWM_ARG_ID_STACKDEC
:
5349 if (ws
->l_state
.vertical_stacks
> 1)
5350 ws
->l_state
.vertical_stacks
--;
5352 case SWM_ARG_ID_FLIPLAYOUT
:
5353 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
5361 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5363 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
5365 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
5369 horizontal_config(struct workspace
*ws
, int id
)
5371 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
5374 case SWM_ARG_ID_STACKRESET
:
5375 case SWM_ARG_ID_STACKINIT
:
5376 ws
->l_state
.horizontal_mwin
= 1;
5377 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
5378 ws
->l_state
.horizontal_stacks
= 1;
5380 case SWM_ARG_ID_MASTERSHRINK
:
5381 if (ws
->l_state
.horizontal_msize
> 1)
5382 ws
->l_state
.horizontal_msize
--;
5384 case SWM_ARG_ID_MASTERGROW
:
5385 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
5386 ws
->l_state
.horizontal_msize
++;
5388 case SWM_ARG_ID_MASTERADD
:
5389 ws
->l_state
.horizontal_mwin
++;
5391 case SWM_ARG_ID_MASTERDEL
:
5392 if (ws
->l_state
.horizontal_mwin
> 0)
5393 ws
->l_state
.horizontal_mwin
--;
5395 case SWM_ARG_ID_STACKBALANCE
:
5396 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ (ws
->l_state
.horizontal_stacks
+ 1);
5398 case SWM_ARG_ID_STACKINC
:
5399 ws
->l_state
.horizontal_stacks
++;
5401 case SWM_ARG_ID_STACKDEC
:
5402 if (ws
->l_state
.horizontal_stacks
> 1)
5403 ws
->l_state
.horizontal_stacks
--;
5405 case SWM_ARG_ID_FLIPLAYOUT
:
5406 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
5414 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5416 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
5418 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
5421 /* fullscreen view */
5423 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5425 struct swm_geometry gg
= *g
;
5426 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
, *tmpw
;
5429 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
5434 winno
= count_win(ws
, false);
5435 if (winno
== 0 && count_win(ws
, true) == 0)
5438 /* Figure out which top level window should be visible. */
5439 if (ws
->focus_pending
)
5440 win
= ws
->focus_pending
;
5443 else if (ws
->focus_prev
)
5444 win
= ws
->focus_prev
;
5446 win
= TAILQ_FIRST(&ws
->winlist
);
5448 DNPRINTF(SWM_D_STACK
, "max_stack: focus_pending: %#x, focus: %#x, "
5449 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws
->focus_pending
),
5450 WINID(ws
->focus
), WINID(ws
->focus_prev
),
5451 WINID(TAILQ_FIRST(&ws
->winlist
)), win
->id
);
5453 /* Update window geometry. */
5454 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5463 /* Set maximized flag for all maxed windows. */
5464 if (!MAXIMIZED(w
)) {
5465 /* Preserve floating geometry. */
5467 store_float_geom(w
);
5469 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_MAXIMIZED
);
5470 ewmh_update_wm_state(w
);
5473 /* Only reconfigure if necessary. */
5474 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
5475 HEIGHT(w
) != gg
.h
) {
5478 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
)) {
5479 w
->bordered
= false;
5480 WIDTH(w
) += 2 * border_width
;
5481 HEIGHT(w
) += 2 * border_width
;
5484 X(w
) += border_width
;
5485 Y(w
) += border_width
;
5492 /* If transient, stack parent and its children. */
5493 if (TRANS(win
) && (parent
= find_window(win
->transient
))) {
5494 raise_window(parent
);
5496 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5497 if (w
->transient
== parent
->id
)
5501 /* Make sure focus window is on top. */
5504 /* Stack any children of focus window. */
5505 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5506 if (w
->transient
== win
->id
)
5509 /* Map all windows. */
5510 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
5516 send_to_rg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5518 int ridx
= args
->id
, i
, num_screens
;
5519 struct swm_region
*rr
= NULL
;
5522 num_screens
= get_screen_count();
5523 /* do nothing if we don't have more than one screen */
5524 if (!(num_screens
> 1 || outputs
> 1))
5527 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
5529 rr
= TAILQ_FIRST(&r
->s
->rl
);
5530 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
5531 rr
= TAILQ_NEXT(rr
, entry
);
5538 send_to_ws(bp
, r
, &a
);
5542 region_under(struct swm_screen
*s
, int x
, int y
)
5544 struct swm_region
*r
= NULL
;
5546 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
5547 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
5548 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
5549 WIDTH(r
), HEIGHT(r
), x
, y
);
5550 if (X(r
) <= x
&& x
< MAX_X(r
))
5551 if (Y(r
) <= y
&& y
< MAX_Y(r
))
5558 /* Transfer focused window to target workspace and focus. */
5560 send_to_ws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5562 int wsid
= args
->id
;
5563 struct ws_win
*win
= NULL
;
5567 if (r
&& r
->ws
&& r
->ws
->focus
)
5572 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win %#x, ws %d\n", win
->id
, wsid
);
5574 if (wsid
< 0 || wsid
>= workspace_limit
)
5577 if (win
->ws
->idx
== wsid
)
5580 win_to_ws(win
, wsid
, true);
5582 /* Set new focus on target ws. */
5583 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5584 win
->ws
->focus_prev
= win
->ws
->focus
;
5585 win
->ws
->focus
= win
;
5586 win
->ws
->focus_pending
= NULL
;
5588 if (win
->ws
->focus_prev
)
5589 draw_frame(win
->ws
->focus_prev
);
5592 DNPRINTF(SWM_D_STACK
, "send_to_ws: focus_pending: %#x, focus: %#x, "
5593 "focus_prev: %#x, first: %#x, win: %#x\n",
5594 WINID(r
->ws
->focus_pending
), WINID(r
->ws
->focus
),
5595 WINID(r
->ws
->focus_prev
), WINID(TAILQ_FIRST(&r
->ws
->winlist
)),
5598 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5599 ewmh_update_wm_state(win
);
5601 /* Restack focused region. */
5604 /* If destination ws has a region, restack. */
5607 load_float_geom(win
);
5612 /* Set new focus on current ws. */
5613 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5614 if (r
->ws
->focus
!= NULL
) {
5615 focus_win(r
->ws
->focus
);
5617 DNPRINTF(SWM_D_FOCUS
, "send_to_ws: set_input_focus: "
5618 "%#x, revert-to: parent, time: 0\n", r
->id
);
5619 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
5620 r
->id
, XCB_CURRENT_TIME
);
5630 win_to_ws(struct ws_win
*win
, int wsid
, bool unfocus
)
5632 struct ws_win
*parent
;
5633 struct workspace
*ws
, *nws
, *pws
;
5635 if (wsid
< 0 || wsid
>= workspace_limit
)
5638 if (win
->ws
->idx
== wsid
)
5642 nws
= &win
->s
->ws
[wsid
];
5644 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win %#x, ws %d -> %d\n", win
->id
,
5647 /* Cleanup focus on source ws. */
5648 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
5649 (ws
->focus
== win
|| ws
->focus_pending
== win
))
5650 ws
->focus_pending
= get_focus_prev(win
);
5652 /* Move the parent if this is a transient window. */
5654 parent
= find_window(win
->transient
);
5657 /* Set new focus in parent's ws if needed. */
5658 if (pws
->focus
== parent
) {
5659 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5660 pws
->focus_pending
=
5661 get_focus_prev(parent
);
5663 unfocus_win(parent
);
5665 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5666 pws
->focus
= pws
->focus_pending
;
5667 pws
->focus_pending
= NULL
;
5671 /* Don't unmap parent if new ws is visible */
5673 unmap_window(parent
);
5676 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
5677 TAILQ_REMOVE(&ws
->stack
, parent
, stack_entry
);
5678 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
5679 TAILQ_INSERT_TAIL(&nws
->stack
, parent
, stack_entry
);
5682 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5683 "_NET_WM_DESKTOP: %d\n", wsid
);
5684 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5685 parent
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
5686 XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5693 if (ws
->focus_prev
== win
)
5694 ws
->focus_prev
= NULL
;
5696 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& ws
->focus_pending
!= NULL
) {
5697 ws
->focus
= ws
->focus_pending
;
5698 ws
->focus_pending
= NULL
;
5701 /* Don't unmap if new ws is visible */
5706 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
5707 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
5708 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
5709 TAILQ_INSERT_TAIL(&nws
->stack
, win
, stack_entry
);
5712 /* Update the window's workspace property: _NET_WM_DESKTOP */
5713 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5714 "_NET_WM_DESKTOP: %d\n", wsid
);
5715 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
5716 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5718 ewmh_update_client_list();
5720 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
5724 pressbutton(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5726 /* suppress unused warning since var is needed */
5730 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
5731 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5732 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
5733 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5737 raise_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5739 /* Suppress warning. */
5743 if (r
== NULL
|| r
->ws
== NULL
)
5746 if (r
->ws
->focus
&& MAXIMIZED(r
->ws
->focus
))
5749 r
->ws
->always_raise
= !r
->ws
->always_raise
;
5751 /* Update focused win stacking order based on new always_raise value. */
5752 raise_window(r
->ws
->focus
);
5758 iconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5762 /* Suppress warning. */
5766 if ((w
= r
->ws
->focus
) == NULL
)
5769 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_HIDDEN
);
5770 ewmh_update_wm_state(w
);
5778 get_win_name(xcb_window_t win
)
5781 xcb_get_property_cookie_t c
;
5782 xcb_get_property_reply_t
*r
;
5784 /* First try _NET_WM_NAME for UTF-8. */
5785 c
= xcb_get_property(conn
, 0, win
, ewmh
[_NET_WM_NAME
].atom
,
5786 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5787 r
= xcb_get_property_reply(conn
, c
, NULL
);
5788 if (r
&& r
->type
== XCB_NONE
) {
5790 /* Use WM_NAME instead; no UTF-8. */
5791 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
5792 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5793 r
= xcb_get_property_reply(conn
, c
, NULL
);
5796 if (r
&& r
->type
!= XCB_NONE
&& r
->length
> 0)
5797 name
= strndup(xcb_get_property_value(r
),
5798 xcb_get_property_value_length(r
));
5803 err(1, "get_win_name: failed to allocate memory.");
5811 uniconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5820 DNPRINTF(SWM_D_MISC
, "uniconify\n");
5822 if (r
== NULL
|| r
->ws
== NULL
)
5825 /* make sure we have anything to uniconify */
5826 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5827 if (win
->ws
== NULL
)
5828 continue; /* should never happen */
5834 DNPRINTF(SWM_D_MISC
, "uniconify: count: %d\n", count
);
5840 search_resp_action
= SWM_SEARCH_UNICONIFY
;
5842 spawn_select(r
, args
, "search", &searchpid
);
5844 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5847 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5848 if (win
->ws
== NULL
)
5849 continue; /* should never happen */
5853 name
= get_win_name(win
->id
);
5854 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
5862 name_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5868 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
5874 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
5876 spawn_select(r
, args
, "name_workspace", &searchpid
);
5878 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5881 fprintf(lfile
, "%s", "");
5886 search_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5889 struct workspace
*ws
;
5894 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
5900 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
5902 spawn_select(r
, args
, "search", &searchpid
);
5904 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5907 for (i
= 0; i
< workspace_limit
; i
++) {
5911 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
5912 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
5919 search_win_cleanup(void)
5921 struct search_window
*sw
= NULL
;
5923 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
5924 xcb_destroy_window(conn
, sw
->indicator
);
5925 TAILQ_REMOVE(&search_wl
, sw
, entry
);
5931 search_win(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5933 struct ws_win
*win
= NULL
;
5934 struct search_window
*sw
= NULL
;
5937 xcb_screen_t
*screen
;
5938 int i
, width
, height
;
5946 XRectangle l_ibox
, l_lbox
;
5950 DNPRINTF(SWM_D_MISC
, "search_win\n");
5953 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
5955 spawn_select(r
, args
, "search", &searchpid
);
5957 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5960 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
5961 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
5963 TAILQ_INIT(&search_wl
);
5966 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5970 sw
= calloc(1, sizeof(struct search_window
));
5972 warn("search_win: calloc");
5974 search_win_cleanup();
5980 snprintf(s
, sizeof s
, "%d", i
);
5983 w
= xcb_generate_id(conn
);
5984 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
5985 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
5986 wa
[2] = screen
->default_colormap
;
5988 if (bar_font_legacy
) {
5989 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
5990 width
= l_lbox
.width
+ 4;
5991 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
5993 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
5995 width
= info
.width
+ 4;
5996 height
= bar_font
->height
+ 4;
5999 xcb_create_window(conn
, screen
->root_depth
, w
, win
->frame
, 0, 0,
6000 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
6001 screen
->root_visual
, XCB_CW_BACK_PIXEL
|
6002 XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
, wa
);
6004 xcb_map_window(conn
, w
);
6007 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
6009 if (bar_font_legacy
) {
6010 l_gcv
.graphics_exposures
= 0;
6011 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
6013 XSetForeground(display
, l_draw
,
6014 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
6016 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
6017 (bar_fs_extents
->max_logical_extent
.height
-
6018 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
6020 XFreeGC(display
, l_draw
);
6023 draw
= XftDrawCreate(display
, w
,
6024 DefaultVisual(display
, r
->s
->idx
),
6025 DefaultColormap(display
, r
->s
->idx
));
6027 XftDrawStringUtf8(draw
, &search_font_color
, bar_font
, 2,
6028 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
6029 bar_font
->descent
, (FcChar8
*)s
, len
);
6031 XftDrawDestroy(draw
);
6034 DNPRINTF(SWM_D_MISC
, "search_win: mapped win %#x\n", w
);
6036 fprintf(lfile
, "%d\n", i
);
6046 search_resp_uniconify(const char *resp
, size_t len
)
6052 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
6054 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
6057 name
= get_win_name(win
->id
);
6058 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
6063 if (strncmp(s
, resp
, len
) == 0) {
6064 /* XXX this should be a callback to generalize */
6065 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_HIDDEN
);
6066 ewmh_update_wm_state(win
);
6076 search_resp_name_workspace(const char *resp
, size_t len
)
6078 struct workspace
*ws
;
6080 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
6082 if (search_r
->ws
== NULL
)
6087 free(search_r
->ws
->name
);
6088 search_r
->ws
->name
= NULL
;
6092 ws
->name
= strdup(resp
);
6093 if (ws
->name
== NULL
) {
6094 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
6095 "strdup: %s", strerror(errno
));
6099 ewmh_update_desktop_names();
6100 ewmh_get_desktop_names();
6105 ewmh_update_desktop_names(void)
6107 char *name_list
= NULL
, *p
;
6108 int num_screens
, i
, j
, len
= 0, tot
= 0;
6110 num_screens
= get_screen_count();
6111 for (i
= 0; i
< num_screens
; ++i
) {
6112 for (j
= 0; j
< workspace_limit
; ++j
) {
6113 if (screens
[i
].ws
[j
].name
!= NULL
)
6114 len
+= strlen(screens
[i
].ws
[j
].name
);
6118 if((name_list
= calloc(len
, sizeof(char))) == NULL
)
6119 err(1, "update_desktop_names: calloc: failed to "
6120 "allocate memory.");
6123 for (j
= 0; j
< workspace_limit
; ++j
) {
6124 if (screens
[i
].ws
[j
].name
!= NULL
) {
6125 len
= strlen(screens
[i
].ws
[j
].name
);
6126 memcpy(p
, screens
[i
].ws
[j
].name
, len
);
6135 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6136 screens
[i
].root
, ewmh
[_NET_DESKTOP_NAMES
].atom
,
6137 a_utf8_string
, 8, tot
, name_list
);
6147 ewmh_get_desktop_names(void)
6150 xcb_get_property_cookie_t c
;
6151 xcb_get_property_reply_t
*r
;
6152 int num_screens
, i
, j
, n
, k
;
6154 num_screens
= get_screen_count();
6155 for (i
= 0; i
< num_screens
; ++i
) {
6156 for (j
= 0; j
< workspace_limit
; ++j
) {
6157 free(screens
[i
].ws
[j
].name
);
6158 screens
[i
].ws
[j
].name
= NULL
;
6161 c
= xcb_get_property(conn
, 0, screens
[i
].root
,
6162 ewmh
[_NET_DESKTOP_NAMES
].atom
,
6163 a_utf8_string
, 0, UINT32_MAX
);
6164 r
= xcb_get_property_reply(conn
, c
, NULL
);
6168 names
= xcb_get_property_value(r
);
6169 n
= xcb_get_property_value_length(r
);
6171 for (j
= 0, k
= 0; j
< n
; ++j
) {
6172 if (*(names
+ j
) != '\0') {
6173 screens
[i
].ws
[k
].name
= strdup(names
+ j
);
6174 j
+= strlen(names
+ j
);
6183 ewmh_update_client_list(void)
6186 int num_screens
, i
, j
, k
= 0, count
= 0;
6189 num_screens
= get_screen_count();
6190 for (i
= 0; i
< num_screens
; ++i
) {
6191 for (j
= 0; j
< workspace_limit
; ++j
)
6192 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6195 DNPRINTF(SWM_D_PROP
, "ewmh_update_client_list: win count: %d\n",
6201 wins
= calloc(count
, sizeof(xcb_window_t
));
6203 err(1, "ewmh_update_client_list: calloc: failed to "
6204 "allocate memory.");
6206 for (j
= 0, k
= 0; j
< workspace_limit
; ++j
)
6207 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6208 wins
[k
++] = win
->id
;
6210 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6211 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
6212 XCB_ATOM_WINDOW
, 32, count
, wins
);
6219 ewmh_update_current_desktop(void)
6223 num_screens
= get_screen_count();
6224 for (i
= 0; i
< num_screens
; ++i
) {
6225 if (screens
[i
].r_focus
)
6226 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6227 screens
[i
].root
, ewmh
[_NET_CURRENT_DESKTOP
].atom
,
6228 XCB_ATOM_CARDINAL
, 32, 1,
6229 &screens
[i
].r_focus
->ws
->idx
);
6234 ewmh_update_desktops(void)
6236 int num_screens
, i
, j
;
6239 vals
= calloc(workspace_limit
* 2, sizeof(uint32_t));
6241 err(1, "ewmh_update_desktops: calloc: failed to allocate "
6244 num_screens
= get_screen_count();
6245 for (i
= 0; i
< num_screens
; i
++) {
6246 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6247 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
6248 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
6250 for (j
= 0; j
< workspace_limit
; ++j
) {
6251 if (screens
[i
].ws
[j
].r
!= NULL
) {
6252 vals
[j
* 2] = X(screens
[i
].ws
[j
].r
);
6253 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].r
);
6254 } else if (screens
[i
].ws
[j
].old_r
!= NULL
) {
6255 vals
[j
* 2] = X(screens
[i
].ws
[j
].old_r
);
6256 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].old_r
);
6258 vals
[j
* 2] = vals
[j
* 2 + 1] = 0;
6262 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6263 screens
[i
].root
, ewmh
[_NET_DESKTOP_VIEWPORT
].atom
,
6264 XCB_ATOM_CARDINAL
, 32, workspace_limit
* 2, vals
);
6271 search_resp_search_workspace(const char *resp
)
6278 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
6282 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
6289 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
6291 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
6298 switchws(NULL
, search_r
, &a
);
6302 search_resp_search_window(const char *resp
)
6307 struct search_window
*sw
;
6309 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
6313 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
6318 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
6320 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
6327 TAILQ_FOREACH(sw
, &search_wl
, entry
)
6328 if (idx
== sw
->idx
) {
6334 #define MAX_RESP_LEN 1024
6337 search_do_resp(void)
6343 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
6348 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
6349 warn("search: calloc");
6353 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
6355 warn("search: read error");
6358 resp
[rbytes
] = '\0';
6361 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
6362 * newline, so work around that by sanitizing the resp now.
6364 resp
[strcspn(resp
, "\n")] = '\0';
6367 switch (search_resp_action
) {
6368 case SWM_SEARCH_UNICONIFY
:
6369 search_resp_uniconify(resp
, len
);
6371 case SWM_SEARCH_NAME_WORKSPACE
:
6372 search_resp_name_workspace(resp
, len
);
6374 case SWM_SEARCH_SEARCH_WORKSPACE
:
6375 search_resp_search_workspace(resp
);
6377 case SWM_SEARCH_SEARCH_WINDOW
:
6378 search_resp_search_window(resp
);
6383 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
6384 search_win_cleanup();
6386 search_resp_action
= SWM_SEARCH_NONE
;
6387 close(select_resp_pipe
[0]);
6394 wkill(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6398 DNPRINTF(SWM_D_MISC
, "wkill: win %#x, id: %d\n", WINID(r
->ws
->focus
),
6401 if (r
->ws
->focus
== NULL
)
6404 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
6405 xcb_kill_client(conn
, r
->ws
->focus
->id
);
6407 if (r
->ws
->focus
->can_delete
)
6408 client_msg(r
->ws
->focus
, a_delete
, 0);
6414 clear_maximized(struct workspace
*ws
)
6419 /* Clear any maximized win(s) on ws, from bottom up. */
6420 TAILQ_FOREACH_REVERSE(w
, &ws
->stack
, ws_win_stack
, stack_entry
)
6422 ewmh_apply_flags(w
, w
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
6423 ewmh_update_wm_state(w
);
6431 maximize_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6433 struct ws_win
*w
= r
->ws
->focus
;
6435 /* suppress unused warning since var is needed */
6442 DNPRINTF(SWM_D_MISC
, "maximize_toggle: win %#x\n", w
->id
);
6447 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6450 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_MAXIMIZED
);
6451 ewmh_update_wm_state(w
);
6455 if (w
== w
->ws
->focus
)
6460 DNPRINTF(SWM_D_MISC
, "maximize_toggle: done\n");
6464 floating_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6466 struct ws_win
*w
= r
->ws
->focus
;
6468 /* suppress unused warning since var is needed */
6475 DNPRINTF(SWM_D_MISC
, "floating_toggle: win %#x\n", w
->id
);
6477 if (FULLSCREEN(w
) || TRANS(w
))
6480 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6483 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_ABOVE
);
6484 ewmh_update_wm_state(w
);
6488 if (w
== w
->ws
->focus
)
6493 DNPRINTF(SWM_D_MISC
, "floating_toggle: done\n");
6497 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
6499 struct swm_geometry g
= r
->g
;
6500 int rt
, lt
, tp
, bm
, bw
;
6502 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
6505 * Perpendicular distance of each side of the window to the respective
6506 * side of the region boundary. Positive values indicate the side of
6507 * the window has passed beyond the region boundary.
6509 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) - MAX_X(r
) : bw
;
6510 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) : bw
;
6511 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) - MAX_Y(r
) : bw
;
6512 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) : bw
;
6514 DNPRINTF(SWM_D_MISC
, "region_containment: win %#x, rt: %d, lt: %d, "
6515 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
6516 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
6517 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
6520 * Disable containment if any of the flagged sides went beyond the
6521 * containment boundary, or if containment is disabled.
6523 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
6524 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
6525 /* Make sure window has at least 1 pixel in the region */
6526 g
.x
+= 1 - WIDTH(win
);
6527 g
.y
+= 1 - HEIGHT(win
);
6528 g
.w
+= 2 * WIDTH(win
) - 2;
6529 g
.h
+= 2 * HEIGHT(win
) - 2;
6532 constrain_window(win
, &g
, &opts
);
6535 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6537 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
6539 DNPRINTF(SWM_D_MISC
, "constrain_window: win %#x, (x,y) w x h: "
6540 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6541 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
6542 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
6543 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
6544 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
6545 YESNO(*opts
& SWM_CW_RESIZABLE
));
6547 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) > b
->x
+ b
->w
) {
6548 if (*opts
& SWM_CW_RESIZABLE
)
6549 WIDTH(win
) = b
->x
+ b
->w
- X(win
);
6551 X(win
) = b
->x
+ b
->w
- WIDTH(win
);
6554 if ((*opts
& SWM_CW_LEFT
) && X(win
) < b
->x
) {
6555 if (*opts
& SWM_CW_RESIZABLE
)
6556 WIDTH(win
) -= b
->x
- X(win
);
6561 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) > b
->y
+ b
->h
) {
6562 if (*opts
& SWM_CW_RESIZABLE
)
6563 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
);
6565 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
);
6568 if ((*opts
& SWM_CW_TOP
) && Y(win
) < b
->y
) {
6569 if (*opts
& SWM_CW_RESIZABLE
)
6570 HEIGHT(win
) -= b
->y
- Y(win
);
6575 if (*opts
& SWM_CW_RESIZABLE
) {
6578 if (HEIGHT(win
) < 1)
6584 draw_frame(struct ws_win
*win
)
6586 xcb_rectangle_t rect
[4];
6590 if (win
->frame
== XCB_WINDOW_NONE
) {
6591 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x not "
6592 "reparented.\n", win
->id
);
6596 if (!win
->bordered
) {
6597 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x frame "
6598 "disabled\n", win
->id
);
6601 if (WS_FOCUSED(win
->ws
) && win
->ws
->focus
== win
)
6602 pixel
= MAXIMIZED(win
) ?
6603 &win
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].pixel
:
6604 &win
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6606 pixel
= MAXIMIZED(win
) ?
6607 &win
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].pixel
:
6608 &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6610 /* Top (with corners) */
6613 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6614 rect
[n
].height
= border_width
;
6615 /* Left (without corners) */
6617 rect
[n
].y
= border_width
;
6618 rect
[n
].width
= border_width
;
6619 rect
[n
].height
= HEIGHT(win
);
6620 /* Right (without corners) */
6621 rect
[++n
].x
= border_width
+ WIDTH(win
);
6622 rect
[n
].y
= border_width
;
6623 rect
[n
].width
= border_width
;
6624 rect
[n
].height
= HEIGHT(win
);
6625 /* Bottom (with corners)*/
6627 rect
[n
].y
= border_width
+ HEIGHT(win
);
6628 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6629 rect
[n
].height
= border_width
;
6631 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, pixel
);
6632 xcb_poly_fill_rectangle(conn
, win
->frame
, win
->s
->bar_gc
, 4, rect
);
6636 update_window(struct ws_win
*win
)
6641 if (win
->frame
== XCB_WINDOW_NONE
) {
6642 DNPRINTF(SWM_D_EVENT
, "update_window: skip win %#x; "
6643 "not reparented.\n", win
->id
);
6647 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
6648 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
6649 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6651 /* Reconfigure frame. */
6652 if (win
->bordered
) {
6653 wc
[0] = X(win
) - border_width
;
6654 wc
[1] = Y(win
) - border_width
;
6655 wc
[2] = WIDTH(win
) + 2 * border_width
;
6656 wc
[3] = HEIGHT(win
) + 2 * border_width
;
6661 wc
[3] = HEIGHT(win
);
6666 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x frame %#x, (x,y) w x h: "
6667 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, win
->frame
, wc
[0],
6668 wc
[1], wc
[2], wc
[3], YESNO(win
->bordered
));
6670 xcb_configure_window(conn
, win
->frame
, mask
, wc
);
6672 /* Reconfigure client window. */
6673 wc
[0] = wc
[1] = win
->bordered
? border_width
: 0;
6675 wc
[3] = HEIGHT(win
);
6677 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x, (x,y) w x h: "
6678 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
6679 wc
[3], YESNO(win
->bordered
));
6680 xcb_configure_window(conn
, win
->id
, mask
, wc
);
6682 /* ICCCM 4.2.3 Synthetic ConfigureNotify when moved and not resized. */
6683 if ((X(win
) != win
->g_prev
.x
|| Y(win
) != win
->g_prev
.y
) &&
6684 (win
->java
|| (WIDTH(win
) == win
->g_prev
.w
&&
6685 HEIGHT(win
) == win
->g_prev
.h
))) {
6686 /* Java has special needs when moved together with a resize. */
6687 config_win(win
, NULL
);
6690 win
->g_prev
= win
->g
;
6694 SIMPLEQ_ENTRY(event
) entry
;
6695 xcb_generic_event_t
*ev
;
6697 SIMPLEQ_HEAD(event_queue
, event
) events
= SIMPLEQ_HEAD_INITIALIZER(events
);
6699 xcb_generic_event_t
*
6700 get_next_event(bool dowait
)
6703 xcb_generic_event_t
*evt
;
6705 /* Try queue first. */
6706 if ((ep
= SIMPLEQ_FIRST(&events
))) {
6708 SIMPLEQ_REMOVE_HEAD(&events
, entry
);
6711 evt
= xcb_wait_for_event(conn
);
6713 evt
= xcb_poll_for_event(conn
);
6719 put_back_event(xcb_generic_event_t
*evt
)
6722 if ((ep
= malloc(sizeof (struct event
))) == NULL
)
6723 err(1, "put_back_event: failed to allocate memory.");
6725 SIMPLEQ_INSERT_HEAD(&events
, ep
, entry
);
6728 /* Peeks at next event to detect auto-repeat. */
6730 keyrepeating(xcb_key_release_event_t
*kre
)
6732 xcb_generic_event_t
*evt
;
6734 /* Ensure repeating keypress is finished processing. */
6737 if ((evt
= get_next_event(false))) {
6738 put_back_event(evt
);
6740 if (XCB_EVENT_RESPONSE_TYPE(evt
) == XCB_KEY_PRESS
&&
6741 kre
->sequence
== evt
->sequence
&&
6742 kre
->detail
== ((xcb_key_press_event_t
*)evt
)->detail
)
6750 keybindreleased(struct binding
*bp
, xcb_key_release_event_t
*kre
)
6752 if (bp
->type
== KEYBIND
&& !keyrepeating(kre
) &&
6753 bp
->value
== xcb_key_press_lookup_keysym(syms
, kre
, 0))
6759 #define SWM_RESIZE_STEPS (50)
6762 resize_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
6764 xcb_timestamp_t timestamp
= 0;
6765 struct swm_region
*r
= NULL
;
6766 struct swm_geometry g
;
6767 int top
= 0, left
= 0;
6769 xcb_cursor_t cursor
;
6770 xcb_query_pointer_reply_t
*xpr
= NULL
;
6771 xcb_generic_event_t
*evt
;
6772 xcb_motion_notify_event_t
*mne
;
6773 bool resizing
, step
= false;
6779 if (FULLSCREEN(win
))
6782 /* In max_stack mode, should only resize transients. */
6783 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6786 DNPRINTF(SWM_D_EVENT
, "resize: win %#x, floating: %s, "
6787 "transient: %#x\n", win
->id
, YESNO(ABOVE(win
)),
6791 store_float_geom(win
);
6792 else if (!(TRANS(win
) || ABOVE(win
)))
6795 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6797 ewmh_update_wm_state(win
);
6803 /* It's possible for win to have been freed during focus_flush(). */
6804 if (validate_win(win
)) {
6805 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6810 case SWM_ARG_ID_WIDTHSHRINK
:
6811 WIDTH(win
) -= SWM_RESIZE_STEPS
;
6814 case SWM_ARG_ID_WIDTHGROW
:
6815 WIDTH(win
) += SWM_RESIZE_STEPS
;
6818 case SWM_ARG_ID_HEIGHTSHRINK
:
6819 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
6822 case SWM_ARG_ID_HEIGHTGROW
:
6823 HEIGHT(win
) += SWM_RESIZE_STEPS
;
6830 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6831 SWM_CW_HARDBOUNDARY
);
6833 store_float_geom(win
);
6837 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6838 SWM_CW_SOFTBOUNDARY
);
6841 /* get cursor offset from window root */
6842 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6849 if (xpr
->win_x
< WIDTH(win
) / 2)
6852 if (xpr
->win_y
< HEIGHT(win
) / 2)
6855 if (opt
== SWM_ARG_ID_CENTER
)
6856 cursor
= cursors
[XC_SIZING
].cid
;
6858 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
6859 XC_TOP_RIGHT_CORNER
].cid
;
6861 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
6862 XC_BOTTOM_RIGHT_CORNER
].cid
;
6864 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6865 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
6868 /* Release keyboard freeze if called via keybind. */
6869 if (bp
->type
== KEYBIND
)
6870 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6875 while (resizing
&& (evt
= get_next_event(true))) {
6876 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6877 case XCB_BUTTON_RELEASE
:
6878 if (bp
->type
== BTNBIND
&& bp
->value
==
6879 ((xcb_button_release_event_t
*)evt
)->detail
)
6882 case XCB_KEY_RELEASE
:
6883 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
6886 case XCB_MOTION_NOTIFY
:
6887 mne
= (xcb_motion_notify_event_t
*)evt
;
6888 DNPRINTF(SWM_D_EVENT
, "resize: MOTION_NOTIFY: "
6889 "root: %#x\n", mne
->root
);
6891 /* cursor offset/delta from start of the operation */
6892 dx
= mne
->root_x
- xpr
->root_x
;
6893 dy
= mne
->root_y
- xpr
->root_y
;
6899 if (opt
== SWM_ARG_ID_CENTER
) {
6900 if (g
.h
/ 2 + dy
< 1)
6904 HEIGHT(win
) = g
.h
+ 2 * dy
;
6912 HEIGHT(win
) = g
.h
+ dy
;
6919 if (opt
== SWM_ARG_ID_CENTER
) {
6920 if (g
.w
/ 2 + dx
< 1)
6924 WIDTH(win
) = g
.w
+ 2 * dx
;
6932 WIDTH(win
) = g
.w
+ dx
;
6935 /* not free, don't sync more than 120 times / second */
6936 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6937 timestamp
= mne
->time
;
6938 regionize(win
, mne
->root_x
, mne
->root_y
);
6939 region_containment(win
, r
, SWM_CW_ALLSIDES
|
6940 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
6941 SWM_CW_SOFTBOUNDARY
);
6946 case XCB_BUTTON_PRESS
:
6948 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_PRESS ignored\n");
6949 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
6950 ((xcb_button_press_event_t
*)evt
)->time
);
6955 DNPRINTF(SWM_D_EVENT
, "resize: KEY_PRESS ignored\n");
6956 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6957 ((xcb_key_press_event_t
*)evt
)->time
);
6963 /* It's possible for win to have been freed above. */
6964 if (validate_win(win
)) {
6965 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6973 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6974 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
6978 store_float_geom(win
);
6980 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6982 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
6986 resize(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6988 struct ws_win
*win
= NULL
;
6989 xcb_query_pointer_reply_t
*qpr
= NULL
;
6994 if (args
->id
!= SWM_ARG_ID_DONTCENTER
&&
6995 args
->id
!= SWM_ARG_ID_CENTER
) {
6996 /* {height,width}_{grow,shrink} use the focus window. */
7000 /* resize uses window under pointer. */
7001 qpr
= xcb_query_pointer_reply(conn
,
7002 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7004 win
= find_window(qpr
->child
);
7010 resize_win(win
, bp
, args
->id
);
7012 if (args
->id
&& bp
->type
== KEYBIND
)
7018 /* Try to set window region based on supplied coordinates or window center. */
7020 regionize(struct ws_win
*win
, int x
, int y
)
7022 struct swm_region
*r
= NULL
;
7024 r
= region_under(win
->s
, x
, y
);
7026 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
7027 Y(win
) + HEIGHT(win
) / 2);
7029 if (r
&& r
!= win
->ws
->r
) {
7030 clear_maximized(r
->ws
);
7032 win_to_ws(win
, r
->ws
->idx
, false);
7034 /* Stack old and new region. */
7038 /* Set focus on new ws. */
7039 unfocus_win(r
->ws
->focus
);
7047 #define SWM_MOVE_STEPS (50)
7050 move_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
7052 struct swm_region
*r
;
7053 xcb_timestamp_t timestamp
= 0;
7054 xcb_query_pointer_reply_t
*qpr
= NULL
;
7055 xcb_generic_event_t
*evt
;
7056 xcb_motion_notify_event_t
*mne
;
7057 bool moving
, restack
= false, step
= false;
7062 if ((r
= win
->ws
->r
) == NULL
)
7065 if (FULLSCREEN(win
))
7068 DNPRINTF(SWM_D_EVENT
, "move: win %#x, floating: %s, transient: "
7069 "%#x\n", win
->id
, YESNO(ABOVE(win
)), win
->transient
);
7071 /* in max_stack mode should only move transients */
7072 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
7075 if (!(ABOVE(win
) || TRANS(win
)) || MAXIMIZED(win
)) {
7076 store_float_geom(win
);
7080 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
7082 ewmh_update_wm_state(win
);
7089 /* It's possible for win to have been freed during focus_flush(). */
7090 if (validate_win(win
)) {
7091 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7096 case SWM_ARG_ID_MOVELEFT
:
7097 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
7100 case SWM_ARG_ID_MOVERIGHT
:
7101 X(win
) += (SWM_MOVE_STEPS
- border_width
);
7104 case SWM_ARG_ID_MOVEUP
:
7105 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
7108 case SWM_ARG_ID_MOVEDOWN
:
7109 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
7116 regionize(win
, -1, -1);
7117 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
7119 store_float_geom(win
);
7123 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
7124 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
7125 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
7127 /* get cursor offset from window root */
7128 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
7131 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7135 /* Release keyboard freeze if called via keybind. */
7136 if (bp
->type
== KEYBIND
)
7137 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7140 regionize(win
, qpr
->root_x
, qpr
->root_y
);
7141 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7142 SWM_CW_SOFTBOUNDARY
);
7146 while (moving
&& (evt
= get_next_event(true))) {
7147 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
7148 case XCB_BUTTON_RELEASE
:
7149 if (bp
->type
== BTNBIND
&& bp
->value
==
7150 ((xcb_button_release_event_t
*)evt
)->detail
)
7153 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7154 ((xcb_button_release_event_t
*)evt
)->time
);
7157 case XCB_KEY_RELEASE
:
7158 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
7161 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7162 ((xcb_key_release_event_t
*)evt
)->time
);
7165 case XCB_MOTION_NOTIFY
:
7166 mne
= (xcb_motion_notify_event_t
*)evt
;
7167 DNPRINTF(SWM_D_EVENT
, "move: MOTION_NOTIFY: "
7168 "root: %#x\n", mne
->root
);
7169 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
7170 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
7172 /* not free, don't sync more than 120 times / second */
7173 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
7174 timestamp
= mne
->time
;
7175 regionize(win
, mne
->root_x
, mne
->root_y
);
7176 region_containment(win
, win
->ws
->r
,
7177 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
7182 case XCB_BUTTON_PRESS
:
7183 /* Thaw and ignore. */
7184 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7185 ((xcb_button_press_event_t
*)evt
)->time
);
7190 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7191 ((xcb_key_press_event_t
*)evt
)->time
);
7197 /* It's possible for win to have been freed. */
7198 if (validate_win(win
)) {
7199 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7207 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7208 SWM_CW_SOFTBOUNDARY
);
7212 store_float_geom(win
);
7214 /* New region set to fullscreen layout. */
7215 if (win
->ws
->r
&& win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
]) {
7222 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7223 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
7227 move(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
7229 struct ws_win
*win
= NULL
;
7230 xcb_query_pointer_reply_t
*qpr
= NULL
;
7236 /* move_* uses focus window. */
7240 /* Disallow move_ on tiled. */
7241 if (win
&& !(TRANS(win
) || ABOVE(win
)))
7244 /* move uses window under pointer. */
7245 qpr
= xcb_query_pointer_reply(conn
,
7246 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7248 win
= find_window(qpr
->child
);
7254 move_win(win
, bp
, args
->id
);
7256 if (args
->id
&& bp
->type
== KEYBIND
)
7262 /* action definitions */
7264 char name
[SWM_FUNCNAME_LEN
];
7265 void (*func
)(struct binding
*, struct swm_region
*,
7269 } actions
[FN_INVALID
+ 2] = {
7270 /* name function argument */
7271 { "bar_toggle", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
7272 { "bar_toggle_ws", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
7273 { "button2", pressbutton
, 0, {.id
= 2} },
7274 { "cycle_layout", cycle_layout
, 0, {0} },
7275 { "flip_layout", stack_config
, 0, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
7276 { "float_toggle", floating_toggle
,0, {0} },
7277 { "focus", focus_pointer
, 0, {0} },
7278 { "focus_main", focus
, 0, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
7279 { "focus_next", focus
, 0, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
7280 { "focus_prev", focus
, 0, {.id
= SWM_ARG_ID_FOCUSPREV
} },
7281 { "focus_urgent", focus
, 0, {.id
= SWM_ARG_ID_FOCUSURGENT
} },
7282 { "maximize_toggle", maximize_toggle
,0, {0} },
7283 { "height_grow", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
7284 { "height_shrink", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
7285 { "iconify", iconify
, 0, {0} },
7286 { "master_shrink", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
7287 { "master_grow", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERGROW
} },
7288 { "master_add", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERADD
} },
7289 { "master_del", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERDEL
} },
7290 { "move", move
, FN_F_NOREPLAY
, {0} },
7291 { "move_down", move
, 0, {.id
= SWM_ARG_ID_MOVEDOWN
} },
7292 { "move_left", move
, 0, {.id
= SWM_ARG_ID_MOVELEFT
} },
7293 { "move_right", move
, 0, {.id
= SWM_ARG_ID_MOVERIGHT
} },
7294 { "move_up", move
, 0, {.id
= SWM_ARG_ID_MOVEUP
} },
7295 { "mvrg_1", send_to_rg
, 0, {.id
= 0} },
7296 { "mvrg_2", send_to_rg
, 0, {.id
= 1} },
7297 { "mvrg_3", send_to_rg
, 0, {.id
= 2} },
7298 { "mvrg_4", send_to_rg
, 0, {.id
= 3} },
7299 { "mvrg_5", send_to_rg
, 0, {.id
= 4} },
7300 { "mvrg_6", send_to_rg
, 0, {.id
= 5} },
7301 { "mvrg_7", send_to_rg
, 0, {.id
= 6} },
7302 { "mvrg_8", send_to_rg
, 0, {.id
= 7} },
7303 { "mvrg_9", send_to_rg
, 0, {.id
= 8} },
7304 { "mvws_1", send_to_ws
, 0, {.id
= 0} },
7305 { "mvws_2", send_to_ws
, 0, {.id
= 1} },
7306 { "mvws_3", send_to_ws
, 0, {.id
= 2} },
7307 { "mvws_4", send_to_ws
, 0, {.id
= 3} },
7308 { "mvws_5", send_to_ws
, 0, {.id
= 4} },
7309 { "mvws_6", send_to_ws
, 0, {.id
= 5} },
7310 { "mvws_7", send_to_ws
, 0, {.id
= 6} },
7311 { "mvws_8", send_to_ws
, 0, {.id
= 7} },
7312 { "mvws_9", send_to_ws
, 0, {.id
= 8} },
7313 { "mvws_10", send_to_ws
, 0, {.id
= 9} },
7314 { "mvws_11", send_to_ws
, 0, {.id
= 10} },
7315 { "mvws_12", send_to_ws
, 0, {.id
= 11} },
7316 { "mvws_13", send_to_ws
, 0, {.id
= 12} },
7317 { "mvws_14", send_to_ws
, 0, {.id
= 13} },
7318 { "mvws_15", send_to_ws
, 0, {.id
= 14} },
7319 { "mvws_16", send_to_ws
, 0, {.id
= 15} },
7320 { "mvws_17", send_to_ws
, 0, {.id
= 16} },
7321 { "mvws_18", send_to_ws
, 0, {.id
= 17} },
7322 { "mvws_19", send_to_ws
, 0, {.id
= 18} },
7323 { "mvws_20", send_to_ws
, 0, {.id
= 19} },
7324 { "mvws_21", send_to_ws
, 0, {.id
= 20} },
7325 { "mvws_22", send_to_ws
, 0, {.id
= 21} },
7326 { "name_workspace", name_workspace
, 0, {0} },
7327 { "quit", quit
, 0, {0} },
7328 { "raise_toggle", raise_toggle
, 0, {0} },
7329 { "resize", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_DONTCENTER
} },
7330 { "resize_centered", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_CENTER
} },
7331 { "restart", restart
, 0, {0} },
7332 { "rg_1", focusrg
, 0, {.id
= 0} },
7333 { "rg_2", focusrg
, 0, {.id
= 1} },
7334 { "rg_3", focusrg
, 0, {.id
= 2} },
7335 { "rg_4", focusrg
, 0, {.id
= 3} },
7336 { "rg_5", focusrg
, 0, {.id
= 4} },
7337 { "rg_6", focusrg
, 0, {.id
= 5} },
7338 { "rg_7", focusrg
, 0, {.id
= 6} },
7339 { "rg_8", focusrg
, 0, {.id
= 7} },
7340 { "rg_9", focusrg
, 0, {.id
= 8} },
7341 { "rg_move_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_UP
} },
7342 { "rg_move_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_DOWN
} },
7343 { "rg_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7344 { "rg_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7345 { "screen_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7346 { "screen_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7347 { "search_win", search_win
, 0, {0} },
7348 { "search_workspace", search_workspace
, 0, {0} },
7349 { "spawn_custom", NULL
, 0, {0} },
7350 { "stack_balance", stack_config
, 0, {.id
= SWM_ARG_ID_STACKBALANCE
} },
7351 { "stack_inc", stack_config
, 0, {.id
= SWM_ARG_ID_STACKINC
} },
7352 { "stack_dec", stack_config
, 0, {.id
= SWM_ARG_ID_STACKDEC
} },
7353 { "stack_reset", stack_config
, 0, {.id
= SWM_ARG_ID_STACKRESET
} },
7354 { "swap_main", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPMAIN
} },
7355 { "swap_next", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPNEXT
} },
7356 { "swap_prev", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPPREV
} },
7357 { "uniconify", uniconify
, 0, {0} },
7358 { "version", version
, 0, {0} },
7359 { "width_grow", resize
, 0, {.id
= SWM_ARG_ID_WIDTHGROW
} },
7360 { "width_shrink", resize
, 0, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
7361 { "wind_del", wkill
, 0, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
7362 { "wind_kill", wkill
, 0, {.id
= SWM_ARG_ID_KILLWINDOW
} },
7363 { "ws_1", switchws
, 0, {.id
= 0} },
7364 { "ws_2", switchws
, 0, {.id
= 1} },
7365 { "ws_3", switchws
, 0, {.id
= 2} },
7366 { "ws_4", switchws
, 0, {.id
= 3} },
7367 { "ws_5", switchws
, 0, {.id
= 4} },
7368 { "ws_6", switchws
, 0, {.id
= 5} },
7369 { "ws_7", switchws
, 0, {.id
= 6} },
7370 { "ws_8", switchws
, 0, {.id
= 7} },
7371 { "ws_9", switchws
, 0, {.id
= 8} },
7372 { "ws_10", switchws
, 0, {.id
= 9} },
7373 { "ws_11", switchws
, 0, {.id
= 10} },
7374 { "ws_12", switchws
, 0, {.id
= 11} },
7375 { "ws_13", switchws
, 0, {.id
= 12} },
7376 { "ws_14", switchws
, 0, {.id
= 13} },
7377 { "ws_15", switchws
, 0, {.id
= 14} },
7378 { "ws_16", switchws
, 0, {.id
= 15} },
7379 { "ws_17", switchws
, 0, {.id
= 16} },
7380 { "ws_18", switchws
, 0, {.id
= 17} },
7381 { "ws_19", switchws
, 0, {.id
= 18} },
7382 { "ws_20", switchws
, 0, {.id
= 19} },
7383 { "ws_21", switchws
, 0, {.id
= 20} },
7384 { "ws_22", switchws
, 0, {.id
= 21} },
7385 { "ws_next", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
7386 { "ws_next_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
7387 { "ws_next_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
7388 { "ws_prev", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
7389 { "ws_prev_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
7390 { "ws_prev_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
7391 { "ws_prior", priorws
, 0, {0} },
7392 /* SWM_DEBUG actions MUST be here: */
7393 { "debug_toggle", debug_toggle
, 0, {0} },
7394 { "dumpwins", dumpwins
, 0, {0} },
7396 { "invalid action", NULL
, 0, {0} },
7400 update_modkey(uint16_t mod
)
7404 /* Replace all instances of the old mod key. */
7405 RB_FOREACH(bp
, binding_tree
, &bindings
)
7406 if (bp
->mod
& mod_key
)
7407 bp
->mod
= (bp
->mod
& ~mod_key
) | mod
;
7412 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7415 struct spawn_prog
*prog
= NULL
;
7417 char *ap
, **real_args
;
7419 /* suppress unused warning since var is needed */
7422 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
7425 TAILQ_FOREACH(prog
, &spawns
, entry
) {
7426 if (strcasecmp(spawn_name
, prog
->name
) == 0)
7430 warnx("spawn_custom: program %s not found", spawn_name
);
7434 /* make room for expanded args */
7435 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
7436 err(1, "spawn_custom: calloc real_args");
7438 /* expand spawn_args into real_args */
7439 for (i
= c
= 0; i
< prog
->argc
; i
++) {
7441 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
7442 if (strcasecmp(ap
, "$bar_border") == 0) {
7444 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
7446 err(1, "spawn_custom border color");
7447 } else if (strcasecmp(ap
, "$bar_color") == 0) {
7449 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
7451 err(1, "spawn_custom bar color");
7452 } else if (strcasecmp(ap
, "$bar_font") == 0) {
7453 if ((real_args
[c
] = strdup(bar_fonts
))
7455 err(1, "spawn_custom bar fonts");
7456 } else if (strcasecmp(ap
, "$bar_font_color") == 0) {
7458 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
7460 err(1, "spawn_custom color font");
7461 } else if (strcasecmp(ap
, "$color_focus") == 0) {
7463 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
7465 err(1, "spawn_custom color focus");
7466 } else if (strcasecmp(ap
, "$color_focus_maximized") == 0) {
7468 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].name
))
7470 err(1, "spawn_custom color focus maximized");
7471 } else if (strcasecmp(ap
, "$color_unfocus") == 0) {
7473 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
7475 err(1, "spawn_custom color unfocus");
7476 } else if (strcasecmp(ap
, "$color_unfocus_maximized") == 0) {
7478 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].name
))
7480 err(1, "spawn_custom color unfocus maximized");
7481 } else if (strcasecmp(ap
, "$region_index") == 0) {
7482 if (asprintf(&real_args
[c
], "%d",
7483 get_region_index(r
) + 1) < 1)
7484 err(1, "spawn_custom region index");
7485 } else if (strcasecmp(ap
, "$workspace_index") == 0) {
7486 if (asprintf(&real_args
[c
], "%d", r
->ws
->idx
+ 1) < 1)
7487 err(1, "spawn_custom workspace index");
7488 } else if (strcasecmp(ap
, "$dmenu_bottom") == 0) {
7491 if ((real_args
[c
] = strdup("-b")) == NULL
)
7492 err(1, "spawn_custom workspace index");
7494 /* no match --> copy as is */
7495 if ((real_args
[c
] = strdup(ap
)) == NULL
)
7496 err(1, "spawn_custom strdup(ap)");
7498 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
7504 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
7505 for (i
= 0; i
< c
; ++i
)
7506 DPRINTF("\"%s\" ", real_args
[i
]);
7509 *ret_args
= real_args
;
7514 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
7520 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7524 spawn(r
->ws
->idx
, &a
, true);
7526 for (i
= 0; i
< spawn_argc
; i
++)
7532 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7539 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7543 if (pipe(select_list_pipe
) == -1)
7544 err(1, "pipe error");
7545 if (pipe(select_resp_pipe
) == -1)
7546 err(1, "pipe error");
7548 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
7549 err(1, "could not disable SIGPIPE");
7550 switch (*pid
= fork()) {
7552 err(1, "cannot fork");
7555 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
7557 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
7559 close(select_list_pipe
[1]);
7560 close(select_resp_pipe
[0]);
7561 spawn(r
->ws
->idx
, &a
, false);
7563 default: /* parent */
7564 close(select_list_pipe
[0]);
7565 close(select_resp_pipe
[1]);
7569 for (i
= 0; i
< spawn_argc
; i
++)
7574 /* Argument tokenizer. */
7577 char *arg
, *cp
, *next
;
7578 bool single_quoted
= false, double_quoted
= false;
7583 /* Eat and move characters until end of argument is found. */
7584 for (arg
= next
= cp
= *sp
; *cp
!= '\0'; ++cp
) {
7585 if (!double_quoted
&& *cp
== '\'') {
7586 /* Eat single-quote. */
7587 single_quoted
= !single_quoted
;
7588 } else if (!single_quoted
&& *cp
== '"') {
7589 /* Eat double-quote. */
7590 double_quoted
= !double_quoted
;
7591 } else if (!single_quoted
&& *cp
== '\\' && *(cp
+ 1) == '"') {
7592 /* Eat backslash; copy escaped character to arg. */
7594 } else if (!single_quoted
&& !double_quoted
&& *cp
== '\\' &&
7595 (*(cp
+ 1) == '\'' || *(cp
+ 1) == ' ')) {
7596 /* Eat backslash; move escaped character. */
7598 } else if (!single_quoted
&& !double_quoted
&&
7599 (*cp
== ' ' || *cp
== '\t')) {
7600 /* Terminate argument. */
7602 /* Point sp to beginning of next argument. */
7606 /* Move regular character. */
7611 /* Terminate argument if end of string. */
7621 spawn_insert(const char *name
, const char *args
, int flags
)
7623 struct spawn_prog
*sp
;
7624 char *arg
, *cp
, *ptr
;
7626 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s[%s]\n", name
, args
);
7628 if (args
== NULL
|| *args
== '\0')
7631 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
7632 err(1, "spawn_insert: calloc");
7633 if ((sp
->name
= strdup(name
)) == NULL
)
7634 err(1, "spawn_insert: strdup");
7636 /* Convert the arguments to an argument list. */
7637 if ((ptr
= cp
= strdup(args
)) == NULL
)
7638 err(1, "spawn_insert: strdup");
7639 while ((arg
= argsep(&ptr
)) != NULL
) {
7640 /* Null argument; skip it. */
7645 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
7646 sizeof *sp
->argv
)) == NULL
)
7647 err(1, "spawn_insert: realloc");
7648 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
7649 err(1, "spawn_insert: strdup");
7655 DNPRINTF(SWM_D_SPAWN
, "arg %d: [%s]\n", sp
->argc
, sp
->argv
[sp
->argc
-1]);
7656 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
7657 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
7661 spawn_remove(struct spawn_prog
*sp
)
7665 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
7667 TAILQ_REMOVE(&spawns
, sp
, entry
);
7668 for (i
= 0; i
< sp
->argc
; i
++)
7674 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
7680 struct spawn_prog
*sp
;
7682 while ((sp
= TAILQ_FIRST(&spawns
)) != NULL
) {
7688 spawn_find(const char *name
)
7690 struct spawn_prog
*sp
;
7692 TAILQ_FOREACH(sp
, &spawns
, entry
)
7693 if (strcasecmp(sp
->name
, name
) == 0)
7700 setspawn(const char *name
, const char *args
, int flags
)
7702 struct spawn_prog
*sp
;
7704 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
7709 /* Remove any old spawn under the same name. */
7710 if ((sp
= spawn_find(name
)) != NULL
)
7714 spawn_insert(name
, args
, flags
);
7716 warnx("error: setspawn: cannot find program: %s", name
);
7718 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
7722 setconfspawn(const char *selector
, const char *value
, int flags
)
7726 if (selector
== NULL
|| strlen(selector
) == 0)
7729 args
= expand_tilde(value
);
7731 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
7733 setspawn(selector
, args
, flags
);
7736 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
7741 validate_spawns(void)
7744 struct spawn_prog
*sp
;
7745 char which
[PATH_MAX
];
7748 RB_FOREACH(bp
, binding_tree
, &bindings
) {
7749 if (bp
->action
!= FN_SPAWN_CUSTOM
)
7753 sp
= spawn_find(bp
->spawn_name
);
7754 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
7757 /* verify we have the goods */
7758 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
7759 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
7761 for (i
= strlen("which "); i
< strlen(which
); i
++)
7762 if (which
[i
] == ' ') {
7766 if (system(which
) != 0)
7767 add_startup_exception("could not find %s",
7768 &which
[strlen("which ")]);
7775 setconfspawn("lock", "xlock", 0);
7777 setconfspawn("term", "xterm", 0);
7778 setconfspawn("spawn_term", "xterm", 0);
7780 setconfspawn("menu", "dmenu_run"
7784 " -nf $bar_font_color"
7786 " -sf $bar_color", 0);
7788 setconfspawn("search", "dmenu"
7793 " -nf $bar_font_color"
7795 " -sf $bar_color", 0);
7797 setconfspawn("name_workspace", "dmenu"
7802 " -nf $bar_font_color"
7804 " -sf $bar_color", 0);
7806 /* These are not verified for existence, even with a binding set. */
7807 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
7808 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
7809 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
7813 #define SWM_MODNAME_SIZE 32
7814 #define SWM_KEY_WS "\n+ \t"
7816 parsebinding(const char *bindstr
, uint16_t *mod
, enum binding_type
*type
,
7817 uint32_t *val
, uint32_t *flags
)
7819 char *str
, *cp
, *name
;
7820 KeySym ks
, lks
, uks
;
7822 DNPRINTF(SWM_D_KEY
, "parsebinding: enter [%s]\n", bindstr
);
7823 if (mod
== NULL
|| val
== NULL
) {
7824 DNPRINTF(SWM_D_KEY
, "parsebinding: no mod or key vars\n");
7827 if (bindstr
== NULL
|| strlen(bindstr
) == 0) {
7828 DNPRINTF(SWM_D_KEY
, "parsebinding: no bindstr\n");
7832 if ((cp
= str
= strdup(bindstr
)) == NULL
)
7833 err(1, "parsebinding: strdup");
7835 *val
= XCB_NO_SYMBOL
;
7839 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
7840 DNPRINTF(SWM_D_KEY
, "parsebinding: entry [%s]\n", name
);
7842 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
7843 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
7845 else if (strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
) == 0)
7846 *mod
|= XCB_MOD_MASK_1
;
7847 else if (strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
) == 0)
7848 *mod
|= XCB_MOD_MASK_2
;
7849 else if (strncmp(name
, "Mod3", SWM_MODNAME_SIZE
) == 0)
7850 *mod
|= XCB_MOD_MASK_3
;
7851 else if (strncmp(name
, "Mod4", SWM_MODNAME_SIZE
) == 0)
7852 *mod
|= XCB_MOD_MASK_4
;
7853 else if (strncmp(name
, "Mod5", SWM_MODNAME_SIZE
) == 0)
7854 *mod
|= XCB_MOD_MASK_5
;
7855 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
7856 *mod
|= XCB_MOD_MASK_SHIFT
;
7857 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
7858 *mod
|= XCB_MOD_MASK_CONTROL
;
7859 else if (strncasecmp(name
, "ANYMOD", SWM_MODNAME_SIZE
) == 0)
7860 *mod
|= XCB_MOD_MASK_ANY
;
7861 else if (strncasecmp(name
, "REPLAY", SWM_MODNAME_SIZE
) == 0)
7862 *flags
|= BINDING_F_REPLAY
;
7863 else if (sscanf(name
, "Button%u", val
) == 1) {
7864 DNPRINTF(SWM_D_KEY
, "parsebinding: button %u\n", *val
);
7866 if (*val
> 255 || *val
== 0) {
7868 "parsebinding: invalid button %u\n", *val
);
7872 /* TODO: do this without Xlib. */
7873 ks
= XStringToKeysym(name
);
7874 if (ks
== NoSymbol
) {
7876 "parsebinding: invalid key %s\n", name
);
7881 XConvertCase(ks
, &lks
, &uks
);
7882 *val
= (uint32_t)lks
;
7886 /* If ANYMOD was specified, ignore the rest. */
7887 if (*mod
& XCB_MOD_MASK_ANY
)
7888 *mod
= XCB_MOD_MASK_ANY
;
7891 DNPRINTF(SWM_D_KEY
, "parsebinding: leave ok\n");
7896 strdupsafe(const char *str
)
7901 return (strdup(str
));
7905 binding_cmp(struct binding
*bp1
, struct binding
*bp2
)
7907 if (bp1
->type
< bp2
->type
)
7909 if (bp1
->type
> bp2
->type
)
7912 if (bp1
->value
< bp2
->value
)
7914 if (bp1
->value
> bp2
->value
)
7917 if (bp1
->mod
< bp2
->mod
)
7919 if (bp1
->mod
> bp2
->mod
)
7926 binding_insert(uint16_t mod
, enum binding_type type
, uint32_t val
,
7927 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
7931 DNPRINTF(SWM_D_KEY
, "binding_insert: mod: %u, type: %d, val: %u, "
7932 "action: %s(%d), spawn_name: %s\n", mod
, type
, val
,
7933 actions
[aid
].name
, aid
, spawn_name
);
7935 if ((bp
= malloc(sizeof *bp
)) == NULL
)
7936 err(1, "binding_insert: malloc");
7943 bp
->spawn_name
= strdupsafe(spawn_name
);
7944 RB_INSERT(binding_tree
, &bindings
, bp
);
7946 DNPRINTF(SWM_D_KEY
, "binding_insert: leave\n");
7950 binding_lookup(uint16_t mod
, enum binding_type type
, uint32_t val
)
7958 return (RB_FIND(binding_tree
, &bindings
, &bp
));
7962 binding_remove(struct binding
*bp
)
7964 DNPRINTF(SWM_D_KEY
, "binding_remove: mod: %u, type: %d, val: %u, "
7965 "action: %s(%d), spawn_name: %s\n", bp
->mod
, bp
->type
, bp
->value
,
7966 actions
[bp
->action
].name
, bp
->action
, bp
->spawn_name
);
7968 RB_REMOVE(binding_tree
, &bindings
, bp
);
7969 free(bp
->spawn_name
);
7972 DNPRINTF(SWM_D_KEY
, "binding_remove: leave\n");
7976 setbinding(uint16_t mod
, enum binding_type type
, uint32_t val
,
7977 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
7981 DNPRINTF(SWM_D_KEY
, "setbinding: enter %s [%s]\n",
7982 actions
[aid
].name
, spawn_name
);
7984 /* Unbind any existing. Loop is to handle MOD_MASK_ANY. */
7985 while ((bp
= binding_lookup(mod
, type
, val
)))
7988 if (aid
!= FN_INVALID
)
7989 binding_insert(mod
, type
, val
, aid
, flags
, spawn_name
);
7991 DNPRINTF(SWM_D_KEY
, "setbinding: leave\n");
7995 setconfbinding(const char *selector
, const char *value
, int flags
)
7997 struct spawn_prog
*sp
;
7998 uint32_t keybtn
, opts
;
8001 enum binding_type type
;
8003 /* suppress unused warning since var is needed */
8006 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter selector: [%s], "
8007 "value: [%s]\n", selector
, value
);
8008 if (selector
== NULL
|| strlen(selector
) == 0) {
8009 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
8010 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8011 setbinding(mod
, type
, keybtn
, FN_INVALID
, opts
, NULL
);
8016 /* search by key function name */
8017 for (aid
= 0; aid
< FN_INVALID
; aid
++) {
8018 if (strncasecmp(selector
, actions
[aid
].name
,
8019 SWM_FUNCNAME_LEN
) == 0) {
8020 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8021 "action\n", selector
);
8022 if (parsebinding(value
, &mod
, &type
, &keybtn
,
8024 setbinding(mod
, type
, keybtn
, aid
, opts
, NULL
);
8030 /* search by custom spawn name */
8031 if ((sp
= spawn_find(selector
)) != NULL
) {
8032 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8033 "spawn\n", selector
);
8034 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8035 setbinding(mod
, type
, keybtn
, FN_SPAWN_CUSTOM
, opts
,
8041 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
8045 #define MODSHIFT MODKEY | XCB_MOD_MASK_SHIFT
8047 setup_keybindings(void)
8049 #define BINDKEY(m, k, a) setbinding(m, KEYBIND, k, a, 0, NULL)
8050 #define BINDKEYSPAWN(m, k, s) setbinding(m, KEYBIND, k, FN_SPAWN_CUSTOM, 0, s)
8051 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8052 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8053 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8054 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8055 BINDKEY(MODKEY
, XK_v
, FN_BUTTON2
);
8056 BINDKEY(MODKEY
, XK_space
, FN_CYCLE_LAYOUT
);
8057 BINDKEY(MODSHIFT
, XK_backslash
, FN_FLIP_LAYOUT
);
8058 BINDKEY(MODKEY
, XK_t
, FN_FLOAT_TOGGLE
);
8059 BINDKEY(MODKEY
, XK_m
, FN_FOCUS_MAIN
);
8060 BINDKEY(MODKEY
, XK_j
, FN_FOCUS_NEXT
);
8061 BINDKEY(MODKEY
, XK_Tab
, FN_FOCUS_NEXT
);
8062 BINDKEY(MODKEY
, XK_k
, FN_FOCUS_PREV
);
8063 BINDKEY(MODSHIFT
, XK_Tab
, FN_FOCUS_PREV
);
8064 BINDKEY(MODKEY
, XK_u
, FN_FOCUS_URGENT
);
8065 BINDKEY(MODKEY
, XK_e
, FN_MAXIMIZE_TOGGLE
);
8066 BINDKEY(MODSHIFT
, XK_equal
, FN_HEIGHT_GROW
);
8067 BINDKEY(MODSHIFT
, XK_minus
, FN_HEIGHT_SHRINK
);
8068 BINDKEY(MODKEY
, XK_w
, FN_ICONIFY
);
8069 BINDKEY(MODKEY
, XK_h
, FN_MASTER_SHRINK
);
8070 BINDKEY(MODKEY
, XK_l
, FN_MASTER_GROW
);
8071 BINDKEY(MODKEY
, XK_comma
, FN_MASTER_ADD
);
8072 BINDKEY(MODKEY
, XK_period
, FN_MASTER_DEL
);
8073 BINDKEY(MODSHIFT
, XK_bracketright
, FN_MOVE_DOWN
);
8074 BINDKEY(MODKEY
, XK_bracketleft
, FN_MOVE_LEFT
);
8075 BINDKEY(MODKEY
, XK_bracketright
, FN_MOVE_RIGHT
);
8076 BINDKEY(MODSHIFT
, XK_bracketleft
, FN_MOVE_UP
);
8077 BINDKEY(MODSHIFT
, XK_KP_End
, FN_MVRG_1
);
8078 BINDKEY(MODSHIFT
, XK_KP_Down
, FN_MVRG_2
);
8079 BINDKEY(MODSHIFT
, XK_KP_Next
, FN_MVRG_3
);
8080 BINDKEY(MODSHIFT
, XK_KP_Left
, FN_MVRG_4
);
8081 BINDKEY(MODSHIFT
, XK_KP_Begin
, FN_MVRG_5
);
8082 BINDKEY(MODSHIFT
, XK_KP_Right
, FN_MVRG_6
);
8083 BINDKEY(MODSHIFT
, XK_KP_Home
, FN_MVRG_7
);
8084 BINDKEY(MODSHIFT
, XK_KP_Up
, FN_MVRG_8
);
8085 BINDKEY(MODSHIFT
, XK_KP_Prior
, FN_MVRG_9
);
8086 BINDKEY(MODSHIFT
, XK_1
, FN_MVWS_1
);
8087 BINDKEY(MODSHIFT
, XK_2
, FN_MVWS_2
);
8088 BINDKEY(MODSHIFT
, XK_3
, FN_MVWS_3
);
8089 BINDKEY(MODSHIFT
, XK_4
, FN_MVWS_4
);
8090 BINDKEY(MODSHIFT
, XK_5
, FN_MVWS_5
);
8091 BINDKEY(MODSHIFT
, XK_6
, FN_MVWS_6
);
8092 BINDKEY(MODSHIFT
, XK_7
, FN_MVWS_7
);
8093 BINDKEY(MODSHIFT
, XK_8
, FN_MVWS_8
);
8094 BINDKEY(MODSHIFT
, XK_9
, FN_MVWS_9
);
8095 BINDKEY(MODSHIFT
, XK_0
, FN_MVWS_10
);
8096 BINDKEY(MODSHIFT
, XK_F1
, FN_MVWS_11
);
8097 BINDKEY(MODSHIFT
, XK_F2
, FN_MVWS_12
);
8098 BINDKEY(MODSHIFT
, XK_F3
, FN_MVWS_13
);
8099 BINDKEY(MODSHIFT
, XK_F4
, FN_MVWS_14
);
8100 BINDKEY(MODSHIFT
, XK_F5
, FN_MVWS_15
);
8101 BINDKEY(MODSHIFT
, XK_F6
, FN_MVWS_16
);
8102 BINDKEY(MODSHIFT
, XK_F7
, FN_MVWS_17
);
8103 BINDKEY(MODSHIFT
, XK_F8
, FN_MVWS_18
);
8104 BINDKEY(MODSHIFT
, XK_F9
, FN_MVWS_19
);
8105 BINDKEY(MODSHIFT
, XK_F10
, FN_MVWS_20
);
8106 BINDKEY(MODSHIFT
, XK_F11
, FN_MVWS_21
);
8107 BINDKEY(MODSHIFT
, XK_F12
, FN_MVWS_22
);
8108 BINDKEY(MODSHIFT
, XK_slash
, FN_NAME_WORKSPACE
);
8109 BINDKEY(MODSHIFT
, XK_q
, FN_QUIT
);
8110 BINDKEY(MODSHIFT
, XK_r
, FN_RAISE_TOGGLE
);
8111 BINDKEY(MODKEY
, XK_q
, FN_RESTART
);
8112 BINDKEY(MODKEY
, XK_KP_End
, FN_RG_1
);
8113 BINDKEY(MODKEY
, XK_KP_Down
, FN_RG_2
);
8114 BINDKEY(MODKEY
, XK_KP_Next
, FN_RG_3
);
8115 BINDKEY(MODKEY
, XK_KP_Left
, FN_RG_4
);
8116 BINDKEY(MODKEY
, XK_KP_Begin
, FN_RG_5
);
8117 BINDKEY(MODKEY
, XK_KP_Right
, FN_RG_6
);
8118 BINDKEY(MODKEY
, XK_KP_Home
, FN_RG_7
);
8119 BINDKEY(MODKEY
, XK_KP_Up
, FN_RG_8
);
8120 BINDKEY(MODKEY
, XK_KP_Prior
, FN_RG_9
);
8121 BINDKEY(MODSHIFT
, XK_Right
, FN_RG_NEXT
);
8122 BINDKEY(MODSHIFT
, XK_Left
, FN_RG_PREV
);
8123 BINDKEY(MODKEY
, XK_f
, FN_SEARCH_WIN
);
8124 BINDKEY(MODKEY
, XK_slash
, FN_SEARCH_WORKSPACE
);
8125 BINDKEYSPAWN(MODSHIFT
, XK_i
, "initscr");
8126 BINDKEYSPAWN(MODSHIFT
, XK_Delete
, "lock");
8127 BINDKEYSPAWN(MODKEY
, XK_p
, "menu");
8128 BINDKEYSPAWN(MODKEY
, XK_s
, "screenshot_all");
8129 BINDKEYSPAWN(MODSHIFT
, XK_s
, "screenshot_wind");
8130 BINDKEYSPAWN(MODSHIFT
, XK_Return
, "term");
8131 BINDKEY(MODSHIFT
, XK_comma
, FN_STACK_INC
);
8132 BINDKEY(MODSHIFT
, XK_period
, FN_STACK_DEC
);
8133 BINDKEY(MODSHIFT
, XK_space
, FN_STACK_RESET
);
8134 BINDKEY(MODKEY
, XK_Return
, FN_SWAP_MAIN
);
8135 BINDKEY(MODSHIFT
, XK_j
, FN_SWAP_NEXT
);
8136 BINDKEY(MODSHIFT
, XK_k
, FN_SWAP_PREV
);
8137 BINDKEY(MODSHIFT
, XK_w
, FN_UNICONIFY
);
8138 BINDKEY(MODSHIFT
, XK_v
, FN_VERSION
);
8139 BINDKEY(MODKEY
, XK_equal
, FN_WIDTH_GROW
);
8140 BINDKEY(MODKEY
, XK_minus
, FN_WIDTH_SHRINK
);
8141 BINDKEY(MODKEY
, XK_x
, FN_WIND_DEL
);
8142 BINDKEY(MODSHIFT
, XK_x
, FN_WIND_KILL
);
8143 BINDKEY(MODKEY
, XK_1
, FN_WS_1
);
8144 BINDKEY(MODKEY
, XK_2
, FN_WS_2
);
8145 BINDKEY(MODKEY
, XK_3
, FN_WS_3
);
8146 BINDKEY(MODKEY
, XK_4
, FN_WS_4
);
8147 BINDKEY(MODKEY
, XK_5
, FN_WS_5
);
8148 BINDKEY(MODKEY
, XK_6
, FN_WS_6
);
8149 BINDKEY(MODKEY
, XK_7
, FN_WS_7
);
8150 BINDKEY(MODKEY
, XK_8
, FN_WS_8
);
8151 BINDKEY(MODKEY
, XK_9
, FN_WS_9
);
8152 BINDKEY(MODKEY
, XK_0
, FN_WS_10
);
8153 BINDKEY(MODKEY
, XK_F1
, FN_WS_11
);
8154 BINDKEY(MODKEY
, XK_F2
, FN_WS_12
);
8155 BINDKEY(MODKEY
, XK_F3
, FN_WS_13
);
8156 BINDKEY(MODKEY
, XK_F4
, FN_WS_14
);
8157 BINDKEY(MODKEY
, XK_F5
, FN_WS_15
);
8158 BINDKEY(MODKEY
, XK_F6
, FN_WS_16
);
8159 BINDKEY(MODKEY
, XK_F7
, FN_WS_17
);
8160 BINDKEY(MODKEY
, XK_F8
, FN_WS_18
);
8161 BINDKEY(MODKEY
, XK_F9
, FN_WS_19
);
8162 BINDKEY(MODKEY
, XK_F10
, FN_WS_20
);
8163 BINDKEY(MODKEY
, XK_F11
, FN_WS_21
);
8164 BINDKEY(MODKEY
, XK_F12
, FN_WS_22
);
8165 BINDKEY(MODKEY
, XK_Right
, FN_WS_NEXT
);
8166 BINDKEY(MODKEY
, XK_Left
, FN_WS_PREV
);
8167 BINDKEY(MODKEY
, XK_Up
, FN_WS_NEXT_ALL
);
8168 BINDKEY(MODKEY
, XK_Down
, FN_WS_PREV_ALL
);
8169 BINDKEY(MODSHIFT
, XK_Up
, FN_WS_NEXT_MOVE
);
8170 BINDKEY(MODSHIFT
, XK_Down
, FN_WS_PREV_MOVE
);
8171 BINDKEY(MODKEY
, XK_a
, FN_WS_PRIOR
);
8173 BINDKEY(MODKEY
, XK_d
, FN_DEBUG_TOGGLE
);
8174 BINDKEY(MODSHIFT
, XK_d
, FN_DUMPWINS
);
8181 setup_btnbindings(void)
8183 setbinding(ANYMOD
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_FOCUS
,
8184 BINDING_F_REPLAY
, NULL
);
8185 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE
, 0, NULL
);
8186 setbinding(MODSHIFT
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE_CENTERED
, 0,
8188 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_MOVE
, 0, NULL
);
8193 clear_bindings(void)
8197 while ((bp
= RB_ROOT(&bindings
)))
8202 clear_keybindings(void)
8204 struct binding
*bp
, *bptmp
;
8206 RB_FOREACH_SAFE(bp
, binding_tree
, &bindings
, bptmp
) {
8207 if (bp
->type
!= KEYBIND
)
8214 setkeymapping(const char *selector
, const char *value
, int flags
)
8216 char *keymapping_file
;
8218 /* suppress unused warnings since vars are needed */
8222 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
8224 keymapping_file
= expand_tilde(value
);
8226 clear_keybindings();
8227 /* load new key bindings; if it fails, revert to default bindings */
8228 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
8229 clear_keybindings();
8230 setup_keybindings();
8233 free(keymapping_file
);
8235 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
8240 updatenumlockmask(void)
8243 xcb_get_modifier_mapping_reply_t
*modmap_r
;
8244 xcb_keycode_t
*modmap
, kc
, *keycode
;
8248 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
8249 xcb_get_modifier_mapping(conn
),
8252 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
8253 for (i
= 0; i
< 8; i
++) {
8254 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
8255 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
8257 keycode
= xcb_key_symbols_get_keycode(syms
,
8261 numlockmask
= (1 << i
);
8268 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
8275 int num_screens
, k
, j
;
8276 uint16_t modifiers
[4];
8277 xcb_keycode_t
*code
;
8279 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
8280 updatenumlockmask();
8283 modifiers
[1] = numlockmask
;
8284 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8285 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8287 num_screens
= get_screen_count();
8288 for (k
= 0; k
< num_screens
; k
++) {
8289 if (TAILQ_EMPTY(&screens
[k
].rl
))
8291 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
8293 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8294 if (bp
->type
!= KEYBIND
)
8297 /* If there is a catch-all, only bind that. */
8298 if ((binding_lookup(ANYMOD
, KEYBIND
, bp
->value
)) &&
8302 /* Skip unused ws binds. */
8303 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8304 bp
->action
<= FN_WS_22
)
8307 /* Skip unused mvws binds. */
8308 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8309 bp
->action
<= FN_MVWS_22
)
8312 if ((code
= xcb_key_symbols_get_keycode(syms
,
8313 bp
->value
)) == NULL
)
8316 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8317 /* All modifiers are grabbed in one pass. */
8318 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8319 "key: %u, modifiers: %d\n", bp
->value
,
8321 xcb_grab_key(conn
, 1, screens
[k
].root
,
8322 bp
->mod
, *code
, XCB_GRAB_MODE_ASYNC
,
8323 XCB_GRAB_MODE_SYNC
);
8325 /* Need to grab each modifier permutation. */
8326 for (j
= 0; j
< LENGTH(modifiers
); j
++) {
8327 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8328 "key: %u, modifiers: %d\n",
8329 bp
->value
, bp
->mod
| modifiers
[j
]);
8330 xcb_grab_key(conn
, 1,
8332 bp
->mod
| modifiers
[j
],
8333 *code
, XCB_GRAB_MODE_ASYNC
,
8334 XCB_GRAB_MODE_SYNC
);
8346 int num_screens
, i
, k
;
8347 uint16_t modifiers
[4];
8349 DNPRINTF(SWM_D_MOUSE
, "grabbuttons\n");
8350 updatenumlockmask();
8353 modifiers
[1] = numlockmask
;
8354 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8355 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8357 num_screens
= get_screen_count();
8358 for (k
= 0; k
< num_screens
; k
++) {
8359 if (TAILQ_EMPTY(&screens
[k
].rl
))
8361 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, screens
[k
].root
,
8363 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8364 if (bp
->type
!= BTNBIND
)
8367 /* If there is a catch-all, only bind that. */
8368 if ((binding_lookup(ANYMOD
, BTNBIND
, bp
->value
)) &&
8372 /* Skip unused ws binds. */
8373 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8374 bp
->action
<= FN_WS_22
)
8377 /* Skip unused mvws binds. */
8378 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8379 bp
->action
<= FN_MVWS_22
)
8382 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8383 /* All modifiers are grabbed in one pass. */
8384 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: grab, "
8385 "button: %u, modifiers: %d\n", bp
->value
,
8387 xcb_grab_button(conn
, 0, screens
[k
].root
,
8388 BUTTONMASK
, XCB_GRAB_MODE_SYNC
,
8389 XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
8390 XCB_CURSOR_NONE
, bp
->value
, bp
->mod
);
8392 /* Need to grab each modifier permutation. */
8393 for (i
= 0; i
< LENGTH(modifiers
); ++i
) {
8394 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: "
8395 "grab, button: %u, modifiers: %u\n",
8396 bp
->value
, bp
->mod
| modifiers
[i
]);
8397 xcb_grab_button(conn
, 0,
8398 screens
[k
].root
, BUTTONMASK
,
8400 XCB_GRAB_MODE_ASYNC
,
8402 XCB_CURSOR_NONE
, bp
->value
,
8403 bp
->mod
| modifiers
[i
]);
8410 const char *quirkname
[] = {
8411 "NONE", /* config string for "no value" */
8419 "FOCUSONMAP_SINGLE",
8427 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
8428 #define SWM_Q_DELIM "\n|+ \t"
8430 parsequirks(const char *qstr
, uint32_t *quirk
, int *ws
)
8432 char *str
, *cp
, *name
;
8435 if (quirk
== NULL
|| qstr
== NULL
)
8438 if ((cp
= str
= strdup(qstr
)) == NULL
)
8439 err(1, "parsequirks: strdup");
8442 while ((name
= strsep(&cp
, SWM_Q_DELIM
)) != NULL
) {
8444 cp
+= (long)strspn(cp
, SWM_Q_DELIM
);
8446 if (sscanf(name
, "WS[%d]", ws
) == 1) {
8452 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
8453 if (strncasecmp(name
, quirkname
[i
],
8454 SWM_QUIRK_LEN
) == 0) {
8455 DNPRINTF(SWM_D_QUIRK
,
8456 "parsequirks: %s\n", name
);
8462 *quirk
|= 1 << (i
-1);
8466 if (i
>= LENGTH(quirkname
)) {
8467 DNPRINTF(SWM_D_QUIRK
,
8468 "parsequirks: invalid quirk [%s]\n", name
);
8479 quirk_insert(const char *class, const char *instance
, const char *name
,
8480 uint32_t quirk
, int ws
)
8484 bool failed
= false;
8486 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: class: %s, instance: %s, name: %s,"
8487 " value: %u, ws: %d\n", class, instance
, name
, quirk
, ws
);
8489 if ((qp
= malloc(sizeof *qp
)) == NULL
)
8490 err(1, "quirk_insert: malloc");
8492 if ((qp
->class = strdup(class)) == NULL
)
8493 err(1, "quirk_insert: strdup");
8494 if ((qp
->instance
= strdup(instance
)) == NULL
)
8495 err(1, "quirk_insert: strdup");
8496 if ((qp
->name
= strdup(name
)) == NULL
)
8497 err(1, "quirk_insert: strdup");
8499 if (asprintf(&str
, "^%s$", class) == -1)
8500 err(1, "quirk_insert: asprintf");
8501 if (regcomp(&qp
->regex_class
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8502 add_startup_exception("regex failed to compile quirk 'class' "
8503 "field: %s", class);
8506 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8509 if (asprintf(&str
, "^%s$", instance
) == -1)
8510 err(1, "quirk_insert: asprintf");
8511 if (regcomp(&qp
->regex_instance
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8512 add_startup_exception("regex failed to compile quirk 'instance'"
8513 " field: %s", instance
);
8516 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8519 if (asprintf(&str
, "^%s$", name
) == -1)
8520 err(1, "quirk_insert: asprintf");
8521 if (regcomp(&qp
->regex_name
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8522 add_startup_exception("regex failed to compile quirk 'name' "
8526 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8530 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: regex error; skipping.\n");
8535 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
8537 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
8541 quirk_remove(struct quirk
*qp
)
8543 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%u]\n", qp
->class,
8544 qp
->name
, qp
->quirk
);
8546 TAILQ_REMOVE(&quirks
, qp
, entry
);
8549 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
8553 quirk_free(struct quirk
*qp
)
8555 regfree(&qp
->regex_class
);
8556 regfree(&qp
->regex_instance
);
8557 regfree(&qp
->regex_name
);
8569 while ((qp
= TAILQ_FIRST(&quirks
)) != NULL
) {
8575 quirk_replace(struct quirk
*qp
, const char *class, const char *instance
,
8576 const char *name
, uint32_t quirk
, int ws
)
8578 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp
->class,
8579 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
8582 quirk_insert(class, instance
, name
, quirk
, ws
);
8584 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
8588 setquirk(const char *class, const char *instance
, const char *name
,
8589 uint32_t quirk
, int ws
)
8593 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
8594 instance
, name
, quirk
, ws
);
8596 /* Remove/replace existing quirk. */
8597 TAILQ_FOREACH(qp
, &quirks
, entry
) {
8598 if (strcmp(qp
->class, class) == 0 &&
8599 strcmp(qp
->instance
, instance
) == 0 &&
8600 strcmp(qp
->name
, name
) == 0) {
8601 if (quirk
== 0 && ws
== -1)
8604 quirk_replace(qp
, class, instance
, name
, quirk
,
8606 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8611 /* Only insert if quirk is not NONE or forced ws is set. */
8612 if (quirk
|| ws
!= -1)
8613 quirk_insert(class, instance
, name
, quirk
, ws
);
8615 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8618 /* Eat '\' in str used to escape square brackets and colon. */
8620 unescape_selector(char *str
)
8624 for (cp
= str
; *str
!= '\0'; ++str
, ++cp
) {
8625 if (*str
== '\\' && (*(str
+ 1) == ':' || *(str
+ 1) == ']' ||
8635 setconfquirk(const char *selector
, const char *value
, int flags
)
8637 char *str
, *cp
, *class;
8638 char *instance
= NULL
, *name
= NULL
;
8639 int retval
, count
= 0, ws
= -1;
8642 /* suppress unused warning since var is needed */
8645 if (selector
== NULL
|| strlen(selector
) == 0)
8648 if ((str
= strdup(selector
)) == NULL
)
8649 err(1, "setconfquirk: strdup");
8651 /* Find non-escaped colon. */
8658 for (++cp
; *cp
!= '\0'; ++cp
) {
8659 if (*cp
== ':' && *(cp
- 1) != '\\') {
8665 unescape_selector(class);
8667 instance
= class + strlen(class) + 1;
8668 unescape_selector(instance
);
8674 name
= instance
+ strlen(instance
) + 1;
8675 unescape_selector(name
);
8680 DNPRINTF(SWM_D_CONF
, "setconfquirk: class: %s, instance: %s, "
8681 "name: %s\n", class, instance
, name
);
8683 if ((retval
= parsequirks(value
, &qrks
, &ws
)) == 0)
8684 setquirk(class, instance
, name
, qrks
, ws
);
8693 setquirk("MPlayer", "xv", ".*",
8694 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
, -1);
8695 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
8697 setquirk("Firefox-bin", "firefox-bin", ".*",
8699 setquirk("Firefox", "Dialog", ".*",
8701 setquirk("Gimp", "gimp", ".*",
8702 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8703 setquirk("XTerm", "xterm", ".*",
8704 SWM_Q_XTERM_FONTADJ
, -1);
8705 setquirk("xine", "Xine Window", ".*",
8706 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8707 setquirk("Xitk", "Xitk Combo", ".*",
8708 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8709 setquirk("xine", "xine Panel", ".*",
8710 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8711 setquirk("Xitk", "Xine Window", ".*",
8712 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8713 setquirk("xine", "xine Video Fullscreen Window", ".*",
8714 SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
, -1);
8715 setquirk("pcb", "pcb", ".*",
8717 setquirk("SDL_App", "SDL_App", ".*",
8718 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
, -1);
8721 /* conf file stuff */
8722 #define SWM_CONF_FILE "spectrwm.conf"
8723 #define SWM_CONF_FILE_OLD "scrotwm.conf"
8727 SWM_S_BAR_AT_BOTTOM
,
8728 SWM_S_BAR_BORDER_WIDTH
,
8731 SWM_S_BAR_ENABLED_WS
,
8736 SWM_S_BOUNDARY_WIDTH
,
8737 SWM_S_CLOCK_ENABLED
,
8740 SWM_S_CYCLE_VISIBLE
,
8742 SWM_S_DISABLE_BORDER
,
8744 SWM_S_FOCUS_CLOSE_WRAP
,
8745 SWM_S_FOCUS_DEFAULT
,
8747 SWM_S_ICONIC_ENABLED
,
8748 SWM_S_JAVA_WORKAROUND
,
8749 SWM_S_MAXIMIZE_HIDE_BAR
,
8750 SWM_S_REGION_PADDING
,
8755 SWM_S_STACK_ENABLED
,
8758 SWM_S_URGENT_COLLAPSE
,
8759 SWM_S_URGENT_ENABLED
,
8760 SWM_S_VERBOSE_LAYOUT
,
8763 SWM_S_WINDOW_CLASS_ENABLED
,
8764 SWM_S_WINDOW_INSTANCE_ENABLED
,
8765 SWM_S_WINDOW_NAME_ENABLED
,
8766 SWM_S_WORKSPACE_CLAMP
,
8767 SWM_S_WORKSPACE_LIMIT
,
8768 SWM_S_WORKSPACE_NAME
,
8772 setconfvalue(const char *selector
, const char *value
, int flags
)
8774 struct workspace
*ws
;
8775 int i
, ws_id
, num_screens
;
8776 char *b
, *str
, *sp
, s
[1024];
8779 case SWM_S_BAR_ACTION
:
8781 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
8782 err(1, "setconfvalue: bar_action");
8784 case SWM_S_BAR_AT_BOTTOM
:
8785 bar_at_bottom
= (atoi(value
) != 0);
8787 case SWM_S_BAR_BORDER_WIDTH
:
8788 bar_border_width
= atoi(value
);
8789 if (bar_border_width
< 0)
8790 bar_border_width
= 0;
8792 case SWM_S_BAR_DELAY
:
8793 /* No longer needed; leave to not break old conf files. */
8795 case SWM_S_BAR_ENABLED
:
8796 bar_enabled
= (atoi(value
) != 0);
8798 case SWM_S_BAR_ENABLED_WS
:
8799 ws_id
= atoi(selector
) - 1;
8800 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8801 errx(1, "setconfvalue: bar_enabled_ws: invalid "
8802 "workspace %d.", ws_id
+ 1);
8804 num_screens
= get_screen_count();
8805 for (i
= 0; i
< num_screens
; i
++) {
8806 ws
= (struct workspace
*)&screens
[i
].ws
;
8807 ws
[ws_id
].bar_enabled
= (atoi(value
) != 0);
8810 case SWM_S_BAR_FONT
:
8812 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
8813 err(1, "setconfvalue: asprintf: failed to allocate "
8814 "memory for bar_fonts.");
8817 /* If already in xft mode, then we are done. */
8818 if (!bar_font_legacy
)
8821 if ((sp
= str
= strdup(value
)) == NULL
)
8822 err(1, "setconfvalue: strdup");
8824 /* If there are any non-XLFD entries, switch to Xft mode. */
8825 while ((b
= strsep(&sp
, ",")) != NULL
) {
8829 bar_font_legacy
= false;
8836 case SWM_S_BAR_FORMAT
:
8838 if ((bar_format
= strdup(value
)) == NULL
)
8839 err(1, "setconfvalue: bar_format");
8841 case SWM_S_BAR_JUSTIFY
:
8842 if (strcmp(value
, "left") == 0)
8843 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
8844 else if (strcmp(value
, "center") == 0)
8845 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
8846 else if (strcmp(value
, "right") == 0)
8847 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
8849 errx(1, "invalid bar_justify");
8851 case SWM_S_BORDER_WIDTH
:
8852 border_width
= atoi(value
);
8853 if (border_width
< 0)
8856 case SWM_S_BOUNDARY_WIDTH
:
8857 boundary_width
= atoi(value
);
8858 if (boundary_width
< 0)
8861 case SWM_S_CLOCK_ENABLED
:
8862 clock_enabled
= (atoi(value
) != 0);
8864 case SWM_S_CLOCK_FORMAT
:
8865 #ifndef SWM_DENY_CLOCK_FORMAT
8867 if ((clock_format
= strdup(value
)) == NULL
)
8868 err(1, "setconfvalue: clock_format");
8871 case SWM_S_CYCLE_EMPTY
:
8872 cycle_empty
= (atoi(value
) != 0);
8874 case SWM_S_CYCLE_VISIBLE
:
8875 cycle_visible
= (atoi(value
) != 0);
8877 case SWM_S_DIALOG_RATIO
:
8878 dialog_ratio
= atof(value
);
8879 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
8882 case SWM_S_DISABLE_BORDER
:
8883 disable_border
= (atoi(value
) != 0);
8885 case SWM_S_FOCUS_CLOSE
:
8886 if (strcmp(value
, "first") == 0)
8887 focus_close
= SWM_STACK_BOTTOM
;
8888 else if (strcmp(value
, "last") == 0)
8889 focus_close
= SWM_STACK_TOP
;
8890 else if (strcmp(value
, "next") == 0)
8891 focus_close
= SWM_STACK_ABOVE
;
8892 else if (strcmp(value
, "previous") == 0)
8893 focus_close
= SWM_STACK_BELOW
;
8895 errx(1, "focus_close");
8897 case SWM_S_FOCUS_CLOSE_WRAP
:
8898 focus_close_wrap
= (atoi(value
) != 0);
8900 case SWM_S_FOCUS_DEFAULT
:
8901 if (strcmp(value
, "last") == 0)
8902 focus_default
= SWM_STACK_TOP
;
8903 else if (strcmp(value
, "first") == 0)
8904 focus_default
= SWM_STACK_BOTTOM
;
8906 errx(1, "focus_default");
8908 case SWM_S_FOCUS_MODE
:
8909 if (strcmp(value
, "default") == 0)
8910 focus_mode
= SWM_FOCUS_DEFAULT
;
8911 else if (strcmp(value
, "follow") == 0 ||
8912 strcmp(value
, "follow_cursor") == 0)
8913 focus_mode
= SWM_FOCUS_FOLLOW
;
8914 else if (strcmp(value
, "manual") == 0)
8915 focus_mode
= SWM_FOCUS_MANUAL
;
8917 errx(1, "focus_mode");
8919 case SWM_S_ICONIC_ENABLED
:
8920 iconic_enabled
= (atoi(value
) != 0);
8922 case SWM_S_JAVA_WORKAROUND
:
8923 java_workaround
= (atoi(value
) != 0);
8925 case SWM_S_MAXIMIZE_HIDE_BAR
:
8926 maximize_hide_bar
= atoi(value
);
8928 case SWM_S_REGION_PADDING
:
8929 region_padding
= atoi(value
);
8930 if (region_padding
< 0)
8933 case SWM_S_SPAWN_ORDER
:
8934 if (strcmp(value
, "first") == 0)
8935 spawn_position
= SWM_STACK_BOTTOM
;
8936 else if (strcmp(value
, "last") == 0)
8937 spawn_position
= SWM_STACK_TOP
;
8938 else if (strcmp(value
, "next") == 0)
8939 spawn_position
= SWM_STACK_ABOVE
;
8940 else if (strcmp(value
, "previous") == 0)
8941 spawn_position
= SWM_STACK_BELOW
;
8943 errx(1, "spawn_position");
8945 case SWM_S_SPAWN_TERM
:
8946 setconfspawn("term", value
, 0);
8947 setconfspawn("spawn_term", value
, 0);
8950 /* No longer needed; leave to not break old conf files. */
8952 case SWM_S_SS_ENABLED
:
8953 /* No longer needed; leave to not break old conf files. */
8955 case SWM_S_STACK_ENABLED
:
8956 stack_enabled
= (atoi(value
) != 0);
8958 case SWM_S_TERM_WIDTH
:
8959 term_width
= atoi(value
);
8963 case SWM_S_TILE_GAP
:
8964 tile_gap
= atoi(value
);
8966 case SWM_S_URGENT_COLLAPSE
:
8967 urgent_collapse
= (atoi(value
) != 0);
8969 case SWM_S_URGENT_ENABLED
:
8970 urgent_enabled
= (atoi(value
) != 0);
8972 case SWM_S_VERBOSE_LAYOUT
:
8973 verbose_layout
= (atoi(value
) != 0);
8974 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
8976 layouts
[i
].l_string
= fancy_stacker
;
8978 layouts
[i
].l_string
= plain_stacker
;
8981 case SWM_S_WARP_FOCUS
:
8982 warp_focus
= (atoi(value
) != 0);
8984 case SWM_S_WARP_POINTER
:
8985 warp_pointer
= (atoi(value
) != 0);
8987 case SWM_S_WINDOW_CLASS_ENABLED
:
8988 window_class_enabled
= (atoi(value
) != 0);
8990 case SWM_S_WINDOW_INSTANCE_ENABLED
:
8991 window_instance_enabled
= (atoi(value
) != 0);
8993 case SWM_S_WINDOW_NAME_ENABLED
:
8994 window_name_enabled
= (atoi(value
) != 0);
8996 case SWM_S_WORKSPACE_CLAMP
:
8997 workspace_clamp
= (atoi(value
) != 0);
8999 case SWM_S_WORKSPACE_LIMIT
:
9000 workspace_limit
= atoi(value
);
9001 if (workspace_limit
> SWM_WS_MAX
)
9002 workspace_limit
= SWM_WS_MAX
;
9003 else if (workspace_limit
< 1)
9004 workspace_limit
= 1;
9006 ewmh_update_desktops();
9008 case SWM_S_WORKSPACE_NAME
:
9009 if (getenv("SWM_STARTED") != NULL
)
9013 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9014 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
9016 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9017 errx(1, "setconfvalue: workspace_name: invalid "
9018 "workspace %d.", ws_id
+ 1);
9020 num_screens
= get_screen_count();
9021 for (i
= 0; i
< num_screens
; ++i
) {
9022 ws
= (struct workspace
*)&screens
[i
].ws
;
9024 if (strlen(s
) > 0) {
9025 free(ws
[ws_id
].name
);
9026 if ((ws
[ws_id
].name
= strdup(s
)) == NULL
)
9027 err(1, "setconfvalue: workspace_name.");
9029 ewmh_update_desktop_names();
9030 ewmh_get_desktop_names();
9041 setconfmodkey(const char *selector
, const char *value
, int flags
)
9043 /* suppress unused warnings since vars are needed */
9047 if (strncasecmp(value
, "Mod1", strlen("Mod1")) == 0)
9048 update_modkey(XCB_MOD_MASK_1
);
9049 else if (strncasecmp(value
, "Mod2", strlen("Mod2")) == 0)
9050 update_modkey(XCB_MOD_MASK_2
);
9051 else if (strncasecmp(value
, "Mod3", strlen("Mod3")) == 0)
9052 update_modkey(XCB_MOD_MASK_3
);
9053 else if (strncasecmp(value
, "Mod4", strlen("Mod4")) == 0)
9054 update_modkey(XCB_MOD_MASK_4
);
9055 else if (strncasecmp(value
, "Mod5", strlen("Mod5")) == 0)
9056 update_modkey(XCB_MOD_MASK_5
);
9063 setconfcolor(const char *selector
, const char *value
, int flags
)
9065 int first
, last
, i
= 0, num_screens
;
9067 num_screens
= get_screen_count();
9069 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
9070 if (selector
== NULL
|| strlen(selector
) == 0 ||
9071 (last
= atoi(selector
) - 1) < 0) {
9073 last
= num_screens
- 1;
9078 if (last
>= num_screens
) {
9079 add_startup_exception("invalid screen index: %d out of bounds "
9080 "(maximum %d)", last
+ 1, num_screens
);
9084 for (i
= first
; i
<= last
; ++i
) {
9085 setscreencolor(value
, i
, flags
);
9088 * When setting focus/unfocus colors, we need to also
9089 * set maximize colors to match if they haven't been customized.
9091 if (flags
== SWM_S_COLOR_FOCUS
&&
9092 !screens
[i
].c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].manual
)
9093 setscreencolor(value
, i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
9094 else if (flags
== SWM_S_COLOR_UNFOCUS
&&
9095 !screens
[i
].c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].manual
)
9096 setscreencolor(value
, i
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
9098 screens
[i
].c
[flags
].manual
= 1;
9105 setconfregion(const char *selector
, const char *value
, int flags
)
9107 /* suppress unused warnings since vars are needed */
9111 custom_region(value
);
9116 setautorun(const char *selector
, const char *value
, int flags
)
9120 char *ap
, *sp
, *str
;
9126 /* suppress unused warnings since vars are needed */
9130 if (getenv("SWM_STARTED"))
9134 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9135 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
9137 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9138 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
9140 sp
= str
= expand_tilde((char *)&s
);
9143 * This is a little intricate
9145 * If the pid already exists we simply reuse it because it means it was
9146 * used before AND not claimed by manage_window. We get away with
9147 * altering it in the parent after INSERT because this can not be a race
9150 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
9153 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
9155 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
9156 err(1, "setautorun: realloc");
9157 a
.argv
[argc
- 1] = ap
;
9160 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
9161 err(1, "setautorun: realloc");
9162 a
.argv
[argc
] = NULL
;
9164 if ((pid
= fork()) == 0) {
9165 spawn(ws_id
, &a
, true);
9175 p
= calloc(1, sizeof *p
);
9178 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
9188 setlayout(const char *selector
, const char *value
, int flags
)
9190 struct workspace
*ws
;
9191 int ws_id
, i
, x
, mg
, ma
, si
, ar
;
9192 int st
= SWM_V_STACK
, num_screens
;
9196 /* suppress unused warnings since vars are needed */
9200 if (getenv("SWM_STARTED"))
9204 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
9205 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
9206 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9207 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9210 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9211 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
9213 if (strcasecmp(s
, "vertical") == 0)
9215 else if (strcasecmp(s
, "vertical_flip") == 0) {
9218 } else if (strcasecmp(s
, "horizontal") == 0)
9220 else if (strcasecmp(s
, "horizontal_flip") == 0) {
9223 } else if (strcasecmp(s
, "fullscreen") == 0)
9226 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9227 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9230 num_screens
= get_screen_count();
9231 for (i
= 0; i
< num_screens
; i
++) {
9232 ws
= (struct workspace
*)&screens
[i
].ws
;
9233 ws
[ws_id
].cur_layout
= &layouts
[st
];
9235 ws
[ws_id
].always_raise
= (ar
!= 0);
9236 if (st
== SWM_MAX_STACK
)
9240 for (x
= 0; x
< abs(mg
); x
++) {
9241 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9242 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
9243 SWM_ARG_ID_MASTERSHRINK
);
9246 for (x
= 0; x
< abs(ma
); x
++) {
9247 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9248 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
9249 SWM_ARG_ID_MASTERDEL
);
9252 for (x
= 0; x
< abs(si
); x
++) {
9253 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9254 si
>= 0 ? SWM_ARG_ID_STACKINC
:
9255 SWM_ARG_ID_STACKDEC
);
9259 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9260 SWM_ARG_ID_FLIPLAYOUT
);
9267 /* config options */
9268 struct config_option
{
9270 int (*func
)(const char*, const char*, int);
9273 struct config_option configopt
[] = {
9274 { "autorun", setautorun
, 0 },
9275 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
9276 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
9277 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
9278 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
9279 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
9280 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
9281 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
9282 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
9283 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
9284 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
9285 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
9286 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
9287 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
9288 { "bind", setconfbinding
, 0 },
9289 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
9290 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
9291 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
9292 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
9293 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
9294 { "color_focus_maximized", setconfcolor
, SWM_S_COLOR_FOCUS_MAXIMIZED
},
9295 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
9296 { "color_unfocus_maximized", setconfcolor
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
},
9297 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
9298 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
9299 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
9300 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
9301 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
9302 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
9303 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
9304 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
9305 { "iconic_enabled", setconfvalue
, SWM_S_ICONIC_ENABLED
},
9306 { "java_workaround", setconfvalue
, SWM_S_JAVA_WORKAROUND
},
9307 { "keyboard_mapping", setkeymapping
, 0 },
9308 { "layout", setlayout
, 0 },
9309 { "maximize_hide_bar", setconfvalue
, SWM_S_MAXIMIZE_HIDE_BAR
},
9310 { "modkey", setconfmodkey
, 0 },
9311 { "program", setconfspawn
, 0 },
9312 { "quirk", setconfquirk
, 0 },
9313 { "region", setconfregion
, 0 },
9314 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
9315 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
9316 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
9317 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
9318 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
9319 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
9320 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
9321 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
9322 { "title_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
}, /* For backwards compat. */
9323 { "title_name_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
}, /* For backwards compat. */
9324 { "urgent_collapse", setconfvalue
, SWM_S_URGENT_COLLAPSE
},
9325 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
9326 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
9327 { "warp_focus", setconfvalue
, SWM_S_WARP_FOCUS
},
9328 { "warp_pointer", setconfvalue
, SWM_S_WARP_POINTER
},
9329 { "window_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
},
9330 { "window_instance_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
},
9331 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
9332 { "workspace_clamp", setconfvalue
, SWM_S_WORKSPACE_CLAMP
},
9333 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
9334 { "name", setconfvalue
, SWM_S_WORKSPACE_NAME
},
9338 _add_startup_exception(const char *fmt
, va_list ap
)
9340 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
9341 warn("%s: asprintf", __func__
);
9345 add_startup_exception(const char *fmt
, ...)
9351 if (startup_exception
)
9354 /* force bar to be enabled due to exception */
9358 _add_startup_exception(fmt
, ap
);
9363 conf_load(const char *filename
, int keymapping
)
9366 char *line
= NULL
, *cp
, *ce
, *optsub
, *optval
= NULL
;
9367 size_t linelen
, lineno
= 0;
9368 int wordlen
, i
, optidx
, count
;
9369 struct config_option
*opt
= NULL
;
9371 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
9373 if (filename
== NULL
) {
9374 warnx("conf_load: no filename");
9378 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
9380 if ((config
= fopen(filename
, "r")) == NULL
) {
9381 warn("conf_load: fopen: %s", filename
);
9385 while (!feof(config
)) {
9389 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
9390 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
9392 err(1, "%s", filename
);
9397 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9398 if (cp
[0] == '\0') {
9402 /* get config option */
9403 wordlen
= strcspn(cp
, "=[ \t\n");
9405 add_startup_exception("%s: line %zd: no option found",
9410 for (i
= 0; i
< LENGTH(configopt
); i
++) {
9411 opt
= &configopt
[i
];
9412 if (strncasecmp(cp
, opt
->optname
, wordlen
) == 0 &&
9413 (int)strlen(opt
->optname
) == wordlen
) {
9419 add_startup_exception("%s: line %zd: unknown option "
9420 "%.*s", filename
, lineno
, wordlen
, cp
);
9423 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
9424 add_startup_exception("%s: line %zd: invalid option "
9425 "%.*s", filename
, lineno
, wordlen
, cp
);
9429 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9431 /* get [selector] if any */
9436 /* Get length of selector. */
9437 for (ce
= ++cp
; *ce
!= '\0'; ++ce
) {
9438 /* Find matching (not escaped) bracket. */
9439 if (*ce
== ']' && *(ce
- 1) != '\\') {
9446 add_startup_exception("%s: line %zd: syntax "
9447 "error: unterminated selector", filename
,
9452 /* ce points at ']'; terminate optsub. */
9457 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
9460 if (strlen(optval
) == 0) {
9461 add_startup_exception("%s: line %zd: must supply value "
9462 "to %s", filename
, lineno
,
9463 configopt
[optidx
].optname
);
9466 /* call function to deal with it all */
9467 if (configopt
[optidx
].func(optsub
, optval
,
9468 configopt
[optidx
].funcflags
) != 0) {
9469 add_startup_exception("%s: line %zd: invalid data for "
9470 "%s", filename
, lineno
, configopt
[optidx
].optname
);
9479 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
9485 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
9487 struct ws_win
*parent
, *w
;
9488 struct swm_region
*r
;
9489 struct workspace
*ws
;
9490 xcb_icccm_wm_hints_t wmh
;
9492 parent
= find_window(win
->transient
);
9494 parent
->focus_child
= win
;
9496 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
9497 " for %#x trans %#x\n", win
->id
, win
->transient
);
9499 r
= root_to_region(win
->s
->root
, SWM_CK_ALL
);
9501 /* parent doen't exist in our window list */
9502 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
9503 if (xcb_icccm_get_wm_hints_reply(conn
,
9504 xcb_icccm_get_wm_hints(conn
, w
->id
),
9506 warnx("can't get hints for %#x", w
->id
);
9510 if (win
->hints
.window_group
!= wmh
.window_group
)
9513 w
->focus_child
= win
;
9514 win
->transient
= w
->id
;
9516 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
9517 "transient to %#x\n", win
->transient
);
9524 window_get_pid(xcb_window_t win
)
9529 xcb_get_property_cookie_t pc
;
9530 xcb_get_property_reply_t
*pr
;
9532 apid
= get_atom_from_string("_NET_WM_PID");
9533 if (apid
== XCB_ATOM_NONE
)
9536 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
9537 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9540 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
9545 if (pr
->type
== apid
&& pr
->format
== 32)
9546 ret
= *((pid_t
*)xcb_get_property_value(pr
));
9552 apid
= get_atom_from_string("_SWM_PID");
9553 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
9555 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9558 if (pr
->type
!= apid
) {
9563 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
9570 get_swm_ws(xcb_window_t id
)
9576 xcb_get_property_reply_t
*gpr
;
9578 gpr
= xcb_get_property_reply(conn
,
9579 xcb_get_property(conn
, 0, id
, a_swm_ws
,
9580 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
9585 proplen
= xcb_get_property_value_length(gpr
);
9587 prop
= malloc(proplen
+ 1);
9590 xcb_get_property_value(gpr
),
9592 prop
[proplen
] = '\0';
9599 DNPRINTF(SWM_D_PROP
, "get_swm_ws: _SWM_WS: %s\n", prop
);
9600 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
9602 DNPRINTF(SWM_D_PROP
, "get_swm_ws: win #%s: %s",
9612 get_ws_idx(struct ws_win
*win
)
9614 xcb_get_property_reply_t
*gpr
;
9620 gpr
= xcb_get_property_reply(conn
,
9621 xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
9622 XCB_ATOM_CARDINAL
, 0, 1),
9625 if (gpr
->type
== XCB_ATOM_CARDINAL
&& gpr
->format
== 32)
9626 ws_idx
= *((int *)xcb_get_property_value(gpr
));
9630 if (ws_idx
== -1 && !(win
->quirks
& SWM_Q_IGNORESPAWNWS
))
9631 ws_idx
= get_swm_ws(win
->id
);
9633 if (ws_idx
> workspace_limit
- 1 || ws_idx
< -1)
9636 DNPRINTF(SWM_D_PROP
, "get_ws_idx: win %#x, ws_idx: %d\n", win
->id
,
9643 reparent_window(struct ws_win
*win
)
9645 xcb_void_cookie_t c
;
9646 xcb_generic_error_t
*error
;
9649 win
->frame
= xcb_generate_id(conn
);
9651 DNPRINTF(SWM_D_MISC
, "reparent_window: win %#x, frame: %#x\n",
9652 win
->id
, win
->frame
);
9655 XCB_EVENT_MASK_ENTER_WINDOW
|
9656 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
9657 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
9658 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
|
9659 XCB_EVENT_MASK_EXPOSURE
;
9661 wa
[0] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
9664 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
->frame
, win
->s
->root
,
9665 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), 0,
9666 XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
9667 XCB_CW_EVENT_MASK
, wa
);
9669 win
->state
= SWM_WIN_STATE_REPARENTING
;
9670 c
= xcb_reparent_window_checked(conn
, win
->id
, win
->frame
, 0, 0);
9671 if ((error
= xcb_request_check(conn
, c
))) {
9672 DNPRINTF(SWM_D_MISC
, "reparent_window: error:\n");
9677 xcb_destroy_window(conn
, win
->frame
);
9678 win
->frame
= XCB_WINDOW_NONE
;
9680 xcb_change_save_set(conn
, XCB_SET_MODE_INSERT
, win
->id
);
9682 DNPRINTF(SWM_D_MISC
, "reparent_window: done.\n");
9686 unparent_window(struct ws_win
*win
)
9688 xcb_change_save_set(conn
, XCB_SET_MODE_DELETE
, win
->id
);
9689 xcb_reparent_window(conn
, win
->id
, win
->s
->root
, X(win
), Y(win
));
9690 xcb_destroy_window(conn
, win
->frame
);
9691 win
->frame
= XCB_WINDOW_NONE
;
9692 win
->state
= SWM_WIN_STATE_UNPARENTING
;
9696 manage_window(xcb_window_t id
, int spawn_pos
, bool mapping
)
9698 struct ws_win
*win
= NULL
, *ww
;
9699 struct swm_region
*r
;
9702 xcb_get_geometry_reply_t
*gr
;
9703 xcb_get_window_attributes_reply_t
*war
= NULL
;
9704 xcb_window_t trans
= XCB_WINDOW_NONE
;
9705 uint32_t i
, wa
[1], new_flags
;
9706 int ws_idx
, force_ws
= -1;
9707 char *class, *instance
, *name
;
9710 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region bar; "
9715 if (find_region(id
)) {
9716 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region window; "
9721 if ((win
= find_window(id
)) != NULL
) {
9722 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x already "
9723 "managed; skipping.)\n", id
);
9724 return (win
); /* Already managed. */
9727 /* See if window is on the unmanaged list. */
9728 if ((win
= find_unmanaged_window(id
)) != NULL
) {
9729 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x found on "
9730 "unmanaged list.\n", id
);
9731 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
9734 set_child_transient(win
, &trans
);
9738 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is new.\n", id
);
9741 war
= xcb_get_window_attributes_reply(conn
,
9742 xcb_get_window_attributes(conn
, id
), NULL
);
9744 DNPRINTF(SWM_D_EVENT
, "manage_window: window lost.\n");
9748 if (war
->override_redirect
) {
9749 DNPRINTF(SWM_D_EVENT
, "manage_window: override_redirect; "
9754 if (!mapping
&& war
->map_state
== XCB_MAP_STATE_UNMAPPED
&&
9755 get_win_state(id
) == XCB_ICCCM_WM_STATE_WITHDRAWN
) {
9756 DNPRINTF(SWM_D_EVENT
, "manage_window: window withdrawn; "
9761 /* Try to get initial window geometry. */
9762 gr
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, id
), NULL
);
9764 DNPRINTF(SWM_D_MISC
, "manage_window: get geometry failed.\n");
9768 /* Create and initialize ws_win object. */
9769 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
9770 err(1, "manage_window: calloc: failed to allocate memory for "
9775 /* Figureout which region the window belongs to. */
9776 r
= root_to_region(gr
->root
, SWM_CK_ALL
);
9778 /* Ignore window border if there is one. */
9779 WIDTH(win
) = gr
->width
;
9780 HEIGHT(win
) = gr
->height
;
9781 X(win
) = gr
->x
+ gr
->border_width
;
9782 Y(win
) = gr
->y
+ gr
->border_width
;
9783 win
->bordered
= false;
9784 win
->mapped
= (war
->map_state
!= XCB_MAP_STATE_UNMAPPED
);
9785 win
->s
= r
->s
; /* this never changes */
9789 /* Select which X events to monitor and set border pixel color. */
9790 wa
[0] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
9791 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
9793 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_EVENT_MASK
, wa
);
9795 /* Get WM_SIZE_HINTS. */
9796 xcb_icccm_get_wm_normal_hints_reply(conn
,
9797 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
9801 xcb_icccm_get_wm_hints_reply(conn
,
9802 xcb_icccm_get_wm_hints(conn
, win
->id
),
9805 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
9806 xcb_icccm_get_wm_transient_for_reply(conn
,
9807 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
9810 win
->transient
= trans
;
9811 set_child_transient(win
, &win
->transient
);
9814 /* Get WM_PROTOCOLS. */
9815 get_wm_protocols(win
);
9818 /* Must be after getting WM_HINTS and WM_PROTOCOLS. */
9819 DNPRINTF(SWM_D_FOCUS
, "manage_window: input_model: %s\n",
9820 get_win_input_model(win
));
9823 /* Set initial quirks based on EWMH. */
9824 ewmh_autoquirk(win
);
9826 /* Determine initial quirks. */
9827 xcb_icccm_get_wm_class_reply(conn
,
9828 xcb_icccm_get_wm_class(conn
, win
->id
),
9831 class = win
->ch
.class_name
? win
->ch
.class_name
: "";
9832 instance
= win
->ch
.instance_name
? win
->ch
.instance_name
: "";
9833 name
= get_win_name(win
->id
);
9835 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, instance: %s, "
9836 "name: %s\n", class, instance
, name
);
9838 /* java is retarded so treat it special */
9839 if (strstr(instance
, "sun-awt")) {
9840 DNPRINTF(SWM_D_CLASS
, "manage_window: java window detected.\n");
9844 TAILQ_FOREACH(qp
, &quirks
, entry
) {
9845 if (regexec(&qp
->regex_class
, class, 0, NULL
, 0) == 0 &&
9846 regexec(&qp
->regex_instance
, instance
, 0, NULL
, 0) == 0 &&
9847 regexec(&qp
->regex_name
, name
, 0, NULL
, 0) == 0) {
9848 DNPRINTF(SWM_D_CLASS
, "manage_window: matched "
9849 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp
->class,
9850 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
9851 win
->quirks
= qp
->quirk
;
9852 if (qp
->ws
>= 0 && qp
->ws
< workspace_limit
)
9859 /* Reset font sizes (the bruteforce way; no default keybinding). */
9860 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
9861 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9862 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
9863 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9864 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
9867 /* Figure out which workspace the window belongs to. */
9868 if (!(win
->quirks
& SWM_Q_IGNOREPID
) &&
9869 (p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
9870 win
->ws
= &r
->s
->ws
[p
->ws
];
9871 TAILQ_REMOVE(&pidlist
, p
, entry
);
9874 } else if ((ws_idx
= get_ws_idx(win
)) != -1 &&
9876 /* _SWM_WS is set; use that. */
9877 win
->ws
= &r
->s
->ws
[ws_idx
];
9878 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
9879 /* Launch transients in the same ws as parent. */
9886 win
->ws
= &r
->s
->ws
[force_ws
];
9888 /* Set the _NET_WM_DESKTOP atom. */
9889 DNPRINTF(SWM_D_PROP
, "manage_window: set _NET_WM_DESKTOP: %d\n",
9891 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
9892 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &win
->ws
->idx
);
9894 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
9895 xcb_delete_property(conn
, win
->id
, a_swm_ws
);
9897 /* WS must already be set for this to work. */
9898 store_float_geom(win
);
9900 /* Make sure window is positioned inside its region, if its active. */
9902 region_containment(win
, r
, SWM_CW_ALLSIDES
|
9903 SWM_CW_HARDBOUNDARY
);
9908 /* Figure out where to insert the window in the workspace list. */
9909 if (trans
&& (ww
= find_window(trans
)))
9910 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
9911 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_ABOVE
)
9912 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
9914 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_BELOW
)
9915 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
9916 else switch (spawn_pos
) {
9919 case SWM_STACK_ABOVE
:
9920 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
9922 case SWM_STACK_BOTTOM
:
9923 case SWM_STACK_BELOW
:
9924 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
9927 ewmh_update_client_list();
9929 TAILQ_INSERT_HEAD(&win
->ws
->stack
, win
, stack_entry
);
9932 /* Get/apply initial _NET_WM_STATE */
9933 ewmh_get_wm_state(win
);
9936 new_flags
= win
->ewmh_flags
;
9938 if (win
->quirks
& SWM_Q_FLOAT
)
9939 new_flags
|= EWMH_F_ABOVE
;
9941 if (win
->quirks
& SWM_Q_ANYWHERE
)
9942 new_flags
|= SWM_F_MANUAL
;
9944 ewmh_apply_flags(win
, new_flags
);
9945 ewmh_update_wm_state(win
);
9947 /* Set initial _NET_WM_ALLOWED_ACTIONS */
9948 ewmh_update_actions(win
);
9950 reparent_window(win
);
9952 DNPRINTF(SWM_D_MISC
, "manage_window: done. win %#x, (x,y) w x h: "
9953 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win
->id
,
9954 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
9955 YESNO(ICONIC(win
)), win
->transient
);
9962 free_window(struct ws_win
*win
)
9964 DNPRINTF(SWM_D_MISC
, "free_window: win %#x\n", WINID(win
));
9969 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
9972 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
9975 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
9979 unmanage_window(struct ws_win
*win
)
9981 DNPRINTF(SWM_D_MISC
, "unmanage_window: win %#x\n", WINID(win
));
9987 unparent_window(win
);
9989 TAILQ_REMOVE(&win
->ws
->stack
, win
, stack_entry
);
9990 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
9991 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
9993 ewmh_update_client_list();
9997 expose(xcb_expose_event_t
*e
)
10002 struct workspace
*ws
;
10005 DNPRINTF(SWM_D_EVENT
, "expose: win %#x, count: %d\n", e
->window
,
10011 if ((b
= find_bar(e
->window
))) {
10014 } else if ((w
= find_window(e
->window
)) && w
->frame
== e
->window
) {
10018 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
10024 DNPRINTF(SWM_D_EVENT
, "expose: done\n");
10028 focusin(xcb_focus_in_event_t
*e
)
10030 struct ws_win
*win
;
10032 DNPRINTF(SWM_D_EVENT
, "focusin: win %#x, mode: %s(%u), "
10033 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10034 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10035 if ((win
= find_window(e
->event
)) && win
!= win
->ws
->focus
&&
10036 win
!= win
->ws
->focus_pending
&&
10037 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10038 win
->ws
->focus_prev
= win
->ws
->focus
;
10039 win
->ws
->focus
= win
;
10040 win
->ws
->focus_pending
= NULL
;
10042 if (win
->ws
->focus_prev
)
10043 draw_frame(win
->ws
->focus_prev
);
10051 focusout(xcb_focus_out_event_t
*e
)
10053 DNPRINTF(SWM_D_EVENT
, "focusout: win %#x, mode: %s(%u), "
10054 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10055 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10060 keypress(xcb_key_press_event_t
*e
)
10063 struct binding
*bp
;
10064 xcb_keysym_t keysym
;
10065 bool replay
= true;
10067 last_event_time
= e
->time
;
10069 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
10071 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
10072 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10073 "state: %u, cleaned: %u, same_screen: %s\n", keysym
, e
->event
,
10074 e
->event_x
, e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
,
10075 e
->root_y
, e
->child
, e
->state
, CLEANMASK(e
->state
),
10076 YESNO(e
->same_screen
));
10078 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10080 /* Look for catch-all. */
10081 if ((bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
)) == NULL
)
10085 replay
= bp
->flags
& BINDING_F_REPLAY
;
10087 if ((ap
= &actions
[bp
->action
]) == NULL
)
10090 if (bp
->action
== FN_SPAWN_CUSTOM
)
10091 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10094 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10096 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10100 DNPRINTF(SWM_D_EVENT
, "keypress: replaying.\n");
10101 /* Pass keypress to event window and unfreeze keyboard queue. */
10102 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10104 /* Release freeze. */
10105 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10109 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
10113 keyrelease(xcb_key_release_event_t
*e
)
10116 struct binding
*bp
;
10117 xcb_keysym_t keysym
;
10119 last_event_time
= e
->time
;
10121 keysym
= xcb_key_release_lookup_keysym(syms
, e
, 0);
10123 DNPRINTF(SWM_D_EVENT
, "keyrelease: keysym: %u, win (x,y): %#x (%d,%d), "
10124 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10125 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
10126 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
10127 e
->child
, e
->state
, YESNO(e
->same_screen
));
10129 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10131 /* Look for catch-all. */
10132 bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
);
10134 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10135 bp
->flags
& BINDING_F_REPLAY
) {
10136 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10137 DNPRINTF(SWM_D_EVENT
, "keyrelease: replaying.\n");
10139 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10140 DNPRINTF(SWM_D_EVENT
, "keyrelease: unfreezing.\n");
10145 DNPRINTF(SWM_D_EVENT
, "keyrelease: done.\n");
10149 buttonpress(xcb_button_press_event_t
*e
)
10151 struct ws_win
*win
= NULL
, *newf
;
10152 struct swm_region
*r
, *old_r
;
10154 struct binding
*bp
;
10155 bool replay
= true;
10157 last_event_time
= e
->time
;
10159 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): %#x (%d,%d), "
10160 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10161 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10162 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10163 e
->state
, YESNO(e
->same_screen
));
10165 if (e
->event
== e
->root
) {
10167 win
= find_window(e
->child
);
10169 /* Focus on empty region */
10170 /* If no windows on region if its empty. */
10171 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10172 if (r
&& TAILQ_EMPTY(&r
->ws
->winlist
)) {
10173 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
10174 if (old_r
&& old_r
!= r
)
10175 unfocus_win(old_r
->ws
->focus
);
10177 DNPRINTF(SWM_D_FOCUS
, "buttonpress: "
10178 "set_input_focus: %#x, revert-to: parent, "
10179 "time: %#x\n", e
->root
, e
->time
);
10180 xcb_set_input_focus(conn
,
10181 XCB_INPUT_FOCUS_POINTER_ROOT
,
10184 /* Clear bar since empty. */
10187 /* No need to replay event. */
10192 win
= find_window(e
->event
);
10196 newf
= get_focus_magic(win
);
10197 if (win
->ws
->focus
== newf
&& newf
!= win
) {
10198 if (win
->focus_child
== win
)
10199 win
->focus_child
= NULL
;
10205 /* Handle any bound action. */
10206 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10208 /* Look for catch-all. */
10209 if ((bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
)) == NULL
)
10214 replay
= bp
->flags
& BINDING_F_REPLAY
;
10216 if ((ap
= &actions
[bp
->action
]) == NULL
)
10219 if (bp
->action
== FN_SPAWN_CUSTOM
)
10220 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10223 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10225 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10229 DNPRINTF(SWM_D_EVENT
, "buttonpress: replaying.\n");
10230 /* Replay event to event window */
10231 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10233 /* Unfreeze grab events. */
10234 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10241 buttonrelease(xcb_button_release_event_t
*e
)
10244 struct binding
*bp
;
10246 last_event_time
= e
->time
;
10248 DNPRINTF(SWM_D_EVENT
, "buttonrelease: win (x,y): %#x (%d,%d), "
10249 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10250 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10251 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10252 e
->state
, YESNO(e
->same_screen
));
10254 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10256 /* Look for catch-all. */
10257 bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
);
10259 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10260 bp
->flags
& BINDING_F_REPLAY
) {
10261 DNPRINTF(SWM_D_EVENT
, "buttonrelease: replaying.\n");
10262 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10264 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10272 get_win_input_model(struct ws_win
*win
)
10276 * Input Model Input Field WM_TAKE_FOCUS
10277 * No Input False Absent
10278 * Passive True Absent
10279 * Locally Active True Present
10280 * Globally Active False Present
10283 if (ACCEPTS_FOCUS(win
))
10284 inputmodel
= (win
->take_focus
) ? "Locally Active" : "Passive";
10286 inputmodel
= (win
->take_focus
) ? "Globally Active" : "No Input";
10292 print_win_geom(xcb_window_t w
)
10294 xcb_get_geometry_reply_t
*wa
;
10296 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
10298 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: %#x\n",
10303 DNPRINTF(SWM_D_MISC
, "print_win_geom: win %#x, root: %#x, "
10304 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
10305 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
10314 get_stack_mode_name(uint8_t mode
)
10319 case XCB_STACK_MODE_ABOVE
:
10322 case XCB_STACK_MODE_BELOW
:
10325 case XCB_STACK_MODE_TOP_IF
:
10328 case XCB_STACK_MODE_BOTTOM_IF
:
10331 case XCB_STACK_MODE_OPPOSITE
:
10343 configurerequest(xcb_configure_request_event_t
*e
)
10345 struct ws_win
*win
;
10346 struct swm_region
*r
= NULL
;
10348 uint32_t wc
[7] = {0};
10352 if ((win
= find_window(e
->window
)) == NULL
)
10353 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10357 if (swm_debug
& SWM_D_EVENT
) {
10358 print_win_geom(e
->window
);
10360 DNPRINTF(SWM_D_EVENT
, "configurerequest: win %#x, "
10361 "parent: %#x, new: %s, value_mask: %u { ", e
->window
,
10362 e
->parent
, YESNO(new), e
->value_mask
);
10363 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
10364 DPRINTF("X: %d ", e
->x
);
10365 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
10366 DPRINTF("Y: %d ", e
->y
);
10367 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10368 DPRINTF("W: %u ", e
->width
);
10369 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10370 DPRINTF("H: %u ", e
->height
);
10371 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
10372 DPRINTF("Border: %u ", e
->border_width
);
10373 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
10374 DPRINTF("Sibling: %#x ", e
->sibling
);
10375 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
10376 DPRINTF("StackMode: %s(%u) ",
10377 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
10383 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10384 mask
|= XCB_CONFIG_WINDOW_X
;
10387 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10388 mask
|= XCB_CONFIG_WINDOW_Y
;
10391 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
10392 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
10393 wc
[i
++] = e
->width
;
10395 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
10396 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
10397 wc
[i
++] = e
->height
;
10399 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
10400 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
10401 wc
[i
++] = e
->border_width
;
10403 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
10404 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
10405 wc
[i
++] = e
->sibling
;
10407 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
10408 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
10409 wc
[i
++] = e
->stack_mode
;
10413 xcb_configure_window(conn
, e
->window
, mask
, wc
);
10416 } else if ((!MANUAL(win
) || win
->quirks
& SWM_Q_ANYWHERE
) &&
10417 !FULLSCREEN(win
) && !MAXIMIZED(win
)) {
10420 else if (win
->ws
->old_r
)
10421 r
= win
->ws
->old_r
;
10423 /* windows are centered unless ANYWHERE quirk is set. */
10424 if (win
->quirks
& SWM_Q_ANYWHERE
) {
10425 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10426 win
->g_float
.x
= e
->x
;
10428 win
->g_float
.x
-= X(r
);
10431 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10432 win
->g_float
.y
= e
->y
;
10434 win
->g_float
.y
-= Y(r
);
10438 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10439 win
->g_float
.w
= e
->width
;
10441 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10442 win
->g_float
.h
= e
->height
;
10444 win
->g_floatvalid
= true;
10446 if (!MAXIMIZED(win
) && !FULLSCREEN(win
) &&
10447 (TRANS(win
) || (ABOVE(win
) &&
10448 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]))) {
10449 WIDTH(win
) = win
->g_float
.w
;
10450 HEIGHT(win
) = win
->g_float
.h
;
10453 update_floater(win
);
10456 config_win(win
, e
);
10460 config_win(win
, e
);
10464 config_win(win
, e
);
10468 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
10472 configurenotify(xcb_configure_notify_event_t
*e
)
10474 struct ws_win
*win
;
10476 DNPRINTF(SWM_D_EVENT
, "configurenotify: win %#x, event win: %#x, "
10477 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
10478 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
10479 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
10480 YESNO(e
->override_redirect
));
10482 win
= find_window(e
->window
);
10485 if (font_adjusted
&& win
->ws
->r
) {
10493 destroynotify(xcb_destroy_notify_event_t
*e
)
10495 struct ws_win
*win
;
10496 struct workspace
*ws
;
10498 DNPRINTF(SWM_D_EVENT
, "destroynotify: win %#x\n", e
->window
);
10500 if ((win
= find_window(e
->window
)) == NULL
) {
10501 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10503 /* Window is on unmanaged list. */
10504 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10511 if (win
->frame
== e
->window
) {
10512 DNPRINTF(SWM_D_EVENT
, "destroynotify: frame for win %#x\n",
10514 win
->frame
= XCB_WINDOW_NONE
;
10518 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10519 /* If we were focused, make sure we focus on something else. */
10520 if (win
== ws
->focus
) {
10521 ws
->focus_pending
= get_focus_prev(win
);
10522 if (ws
->focus_pending
== win
)
10523 ws
->focus_pending
= NULL
;
10527 unmanage_window(win
);
10528 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10532 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
10533 if (ws
->focus_pending
) {
10534 focus_win(ws
->focus_pending
);
10535 ws
->focus_pending
= NULL
;
10536 } else if (ws
->focus
== NULL
) {
10537 DNPRINTF(SWM_D_FOCUS
, "destroynotify: set_input_focus: "
10538 "%#x, revert-to: parent, time: 0\n", ws
->r
->id
);
10539 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
10540 ws
->r
->id
, XCB_CURRENT_TIME
);
10547 DNPRINTF(SWM_D_EVENT
, "destroynotify: done.\n");
10552 get_notify_detail_label(uint8_t detail
)
10557 case XCB_NOTIFY_DETAIL_ANCESTOR
:
10558 label
= "Ancestor";
10560 case XCB_NOTIFY_DETAIL_VIRTUAL
:
10563 case XCB_NOTIFY_DETAIL_INFERIOR
:
10564 label
= "Inferior";
10566 case XCB_NOTIFY_DETAIL_NONLINEAR
:
10567 label
= "Nonlinear";
10569 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
10570 label
= "NonlinearVirtual";
10572 case XCB_NOTIFY_DETAIL_POINTER
:
10575 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
10576 label
= "PointerRoot";
10578 case XCB_NOTIFY_DETAIL_NONE
:
10589 get_notify_mode_label(uint8_t mode
)
10594 case XCB_NOTIFY_MODE_NORMAL
:
10597 case XCB_NOTIFY_MODE_GRAB
:
10600 case XCB_NOTIFY_MODE_UNGRAB
:
10603 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
10604 label
= "WhileGrabbed";
10614 get_state_mask_label(uint16_t state
)
10619 case XCB_KEY_BUT_MASK_SHIFT
:
10620 label
= "ShiftMask";
10622 case XCB_KEY_BUT_MASK_LOCK
:
10623 label
= "LockMask";
10625 case XCB_KEY_BUT_MASK_CONTROL
:
10626 label
= "ControlMask";
10628 case XCB_KEY_BUT_MASK_MOD_1
:
10629 label
= "Mod1Mask";
10631 case XCB_KEY_BUT_MASK_MOD_2
:
10632 label
= "Mod2Mask";
10634 case XCB_KEY_BUT_MASK_MOD_3
:
10635 label
= "Mod3Mask";
10637 case XCB_KEY_BUT_MASK_MOD_4
:
10638 label
= "Mod4Mask";
10640 case XCB_KEY_BUT_MASK_MOD_5
:
10641 label
= "Mod5Mask";
10643 case XCB_KEY_BUT_MASK_BUTTON_1
:
10644 label
= "Button1Mask";
10646 case XCB_KEY_BUT_MASK_BUTTON_2
:
10647 label
= "Button2Mask";
10649 case XCB_KEY_BUT_MASK_BUTTON_3
:
10650 label
= "Button3Mask";
10652 case XCB_KEY_BUT_MASK_BUTTON_4
:
10653 label
= "Button4Mask";
10655 case XCB_KEY_BUT_MASK_BUTTON_5
:
10656 label
= "Button5Mask";
10670 enternotify(xcb_enter_notify_event_t
*e
)
10672 struct ws_win
*win
;
10673 struct swm_region
*r
;
10675 last_event_time
= e
->time
;
10677 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): %#x "
10678 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10679 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10680 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10681 get_notify_mode_label(e
->mode
), e
->mode
,
10682 get_notify_detail_label(e
->detail
), e
->detail
,
10683 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10684 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10687 if (e
->event
== e
->root
&& e
->child
== XCB_WINDOW_NONE
&&
10688 e
->mode
== XCB_NOTIFY_MODE_GRAB
&&
10689 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10690 DNPRINTF(SWM_D_EVENT
, "enternotify: grab inferior; ignoring.\n");
10694 if (focus_mode
== SWM_FOCUS_MANUAL
&&
10695 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10696 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
10700 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10701 e
->mode
== XCB_NOTIFY_MODE_UNGRAB
&&
10702 e
->detail
!= XCB_NOTIFY_DETAIL_ANCESTOR
) {
10703 DNPRINTF(SWM_D_EVENT
, "enternotify: ungrab; ignoring.\n");
10707 if ((win
= find_window(e
->event
)) == NULL
) {
10708 if (e
->event
== e
->root
) {
10709 /* If no windows on pointer region, then focus root. */
10710 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10712 DNPRINTF(SWM_D_EVENT
, "enternotify: "
10713 "NULL region; ignoring.\n");
10719 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
10724 if (e
->mode
== XCB_NOTIFY_MODE_NORMAL
&&
10725 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10726 DNPRINTF(SWM_D_EVENT
, "enternotify: entering from "
10727 "inferior; ignoring\n");
10731 focus_win(get_focus_magic(win
));
10734 DNPRINTF(SWM_D_EVENT
, "enternotify: done\n");
10741 leavenotify(xcb_leave_notify_event_t
*e
)
10743 last_event_time
= e
->time
;
10745 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): %#x "
10746 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10747 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10748 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10749 get_notify_mode_label(e
->mode
), e
->mode
,
10750 get_notify_detail_label(e
->detail
), e
->detail
,
10751 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10752 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10758 mapnotify(xcb_map_notify_event_t
*e
)
10760 struct ws_win
*win
, *parent
= NULL
;
10761 struct workspace
*ws
;
10763 DNPRINTF(SWM_D_EVENT
, "mapnotify: win %#x\n", e
->window
);
10765 if ((win
= manage_window(e
->window
, spawn_position
, false)) == NULL
)
10769 if (win
->state
== SWM_WIN_STATE_REPARENTING
)
10772 if (ws
->r
== NULL
) {
10777 /* Need to know if win was mapped due to ws switch. */
10778 if (ws
->state
== SWM_WS_STATE_MAPPED
) {
10779 if (ws
->focus_pending
&& TRANS(ws
->focus_pending
))
10780 parent
= find_window(win
->transient
);
10782 /* If window's parent is maximized, don't clear it. */
10783 if ((parent
== NULL
) || !MAXIMIZED(parent
))
10784 if (clear_maximized(ws
) > 0)
10788 win
->mapped
= true;
10789 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
10791 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
10792 if (ws
->focus_pending
== win
) {
10794 ws
->focus_pending
= NULL
;
10795 center_pointer(ws
->r
);
10801 DNPRINTF(SWM_D_EVENT
, "mapnotify: done\n");
10807 mappingnotify(xcb_mapping_notify_event_t
*e
)
10809 if (e
->request
!= XCB_MAPPING_POINTER
) {
10810 xcb_refresh_keyboard_mapping(syms
, e
);
10818 maprequest(xcb_map_request_event_t
*e
)
10820 struct ws_win
*win
, *w
= NULL
;
10822 DNPRINTF(SWM_D_EVENT
, "maprequest: win %#x\n",
10825 win
= manage_window(e
->window
, spawn_position
, true);
10829 /* The new window should get focus; prepare. */
10830 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10831 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
) && ACCEPTS_FOCUS(win
)) {
10832 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
10833 /* See if other wins of same type are already mapped. */
10834 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
10835 if (w
== win
|| !w
->mapped
)
10838 if (w
->ch
.class_name
&&
10839 win
->ch
.class_name
&&
10840 strcmp(w
->ch
.class_name
,
10841 win
->ch
.class_name
) == 0 &&
10842 w
->ch
.instance_name
&&
10843 win
->ch
.instance_name
&&
10844 strcmp(w
->ch
.instance_name
,
10845 win
->ch
.instance_name
) == 0)
10851 win
->ws
->focus_pending
= get_focus_magic(win
);
10854 /* All windows need to be mapped if they are in the current workspace.*/
10857 /* Ignore EnterNotify to handle the mapnotify without interference. */
10858 if (focus_mode
== SWM_FOCUS_DEFAULT
)
10859 event_drain(XCB_ENTER_NOTIFY
);
10861 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
10865 motionnotify(xcb_motion_notify_event_t
*e
)
10867 struct swm_region
*r
= NULL
;
10868 int i
, num_screens
;
10870 last_event_time
= e
->time
;
10872 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): %#x "
10873 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10874 "child: %#x, same_screen_focus: %s, state: %d\n",
10875 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10876 get_notify_detail_label(e
->detail
), e
->detail
,
10877 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10878 YESNO(e
->same_screen
), e
->state
);
10880 if (focus_mode
== SWM_FOCUS_MANUAL
)
10883 num_screens
= get_screen_count();
10884 for (i
= 0; i
< num_screens
; i
++)
10885 if (screens
[i
].root
== e
->root
)
10888 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10889 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
10890 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
10898 get_atom_name(xcb_atom_t atom
)
10901 #ifdef SWM_DEBUG_ATOM_NAMES
10903 * This should be disabled during most debugging since
10904 * xcb_get_* causes an xcb_flush.
10907 xcb_get_atom_name_reply_t
*r
;
10909 r
= xcb_get_atom_name_reply(conn
,
10910 xcb_get_atom_name(conn
, atom
),
10913 len
= xcb_get_atom_name_name_length(r
);
10915 name
= malloc(len
+ 1);
10917 memcpy(name
, xcb_get_atom_name_name(r
), len
);
10931 propertynotify(xcb_property_notify_event_t
*e
)
10933 struct ws_win
*win
;
10934 struct workspace
*ws
;
10938 name
= get_atom_name(e
->atom
);
10939 DNPRINTF(SWM_D_EVENT
, "propertynotify: win %#x, atom: %s(%u), "
10940 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
10944 last_event_time
= e
->time
;
10946 win
= find_window(e
->window
);
10952 if (e
->atom
== a_state
) {
10953 /* State just changed, make sure it gets focused if mapped. */
10954 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
10955 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
10957 win
->state
== SWM_WIN_STATE_REPARENTED
&&
10958 ws
->focus_pending
== win
) {
10959 focus_win(ws
->focus_pending
);
10960 ws
->focus_pending
= NULL
;
10964 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
10965 e
->atom
== XCB_ATOM_WM_NAME
) {
10967 bar_draw(ws
->r
->bar
);
10968 } else if (e
->atom
== a_prot
) {
10969 get_wm_protocols(win
);
10970 } else if (e
->atom
== XCB_ATOM_WM_NORMAL_HINTS
) {
10971 xcb_icccm_get_wm_normal_hints_reply(conn
,
10972 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
10980 reparentnotify(xcb_reparent_notify_event_t
*e
)
10982 struct ws_win
*win
;
10984 DNPRINTF(SWM_D_EVENT
, "reparentnotify: event: %#x, win %#x, "
10985 "parent: %#x, (x,y): (%u,%u), override_redirect: %u\n",
10986 e
->event
, e
->window
, e
->parent
, e
->x
, e
->y
, e
->override_redirect
);
10988 win
= find_window(e
->window
);
10990 if (win
->state
== SWM_WIN_STATE_REPARENTING
) {
10991 win
->state
= SWM_WIN_STATE_REPARENTED
;
10998 update_window(win
);
10999 update_win_stacking(win
);
11000 } else if (win
->state
== SWM_WIN_STATE_UNPARENTING
) {
11001 win
->state
= SWM_WIN_STATE_UNPARENTED
;
11007 unmapnotify(xcb_unmap_notify_event_t
*e
)
11009 struct ws_win
*win
;
11010 struct workspace
*ws
;
11012 DNPRINTF(SWM_D_EVENT
, "unmapnotify: win %#x\n", e
->window
);
11014 /* If we aren't managing the window, then ignore. */
11015 win
= find_window(e
->window
);
11016 if (win
== NULL
|| win
->id
!= e
->window
) {
11017 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore unmanaged.\n");
11021 /* Do nothing if already withdrawn. */
11022 if (!win
->mapped
&& !ICONIC(win
)) {
11023 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore withdrawn.\n");
11028 win
->mapped
= false;
11030 /* Ignore if reparenting-related. */
11031 if (win
->state
!= SWM_WIN_STATE_REPARENTED
) {
11032 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore not reparented.\n");
11036 /* If win was focused, make sure to focus on something else. */
11037 if (win
== ws
->focus
) {
11038 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
11039 ws
->focus_pending
= get_focus_prev(win
);
11040 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
11041 "focus_pending: %#x\n",
11042 WINID(ws
->focus_pending
));
11050 DNPRINTF(SWM_D_EVENT
, "unmapnotify: iconify.\n");
11051 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
11054 DNPRINTF(SWM_D_EVENT
, "unmapnotify: withdraw.\n");
11055 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
11056 unmanage_window(win
);
11061 /* Update focus if ws is active. */
11062 if (WS_FOCUSED(ws
)) {
11063 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
11064 focus_win(get_pointer_win(ws
->r
->s
->root
));
11065 } else if (ws
->focus_pending
) {
11066 focus_win(ws
->focus_pending
);
11067 ws
->focus_pending
= NULL
;
11068 } else if (ws
->focus
== NULL
) {
11069 DNPRINTF(SWM_D_FOCUS
, "unmapnotify: set_input_focus: "
11070 "%#x, revert-to: parent, time: 0\n",
11072 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11073 ws
->r
->id
, XCB_CURRENT_TIME
);
11077 center_pointer(ws
->r
);
11079 DNPRINTF(SWM_D_EVENT
, "unmapnotify: done.\n");
11084 get_source_type_label(uint32_t type
)
11089 case EWMH_SOURCE_TYPE_NONE
:
11092 case EWMH_SOURCE_TYPE_NORMAL
:
11095 case EWMH_SOURCE_TYPE_OTHER
:
11107 clientmessage(xcb_client_message_event_t
*e
)
11109 struct ws_win
*win
;
11110 struct swm_region
*r
= NULL
;
11113 int num_screens
, i
;
11114 xcb_map_request_event_t mre
;
11118 name
= get_atom_name(e
->type
);
11119 DNPRINTF(SWM_D_EVENT
, "clientmessage: win %#x, atom: %s(%u)\n",
11120 e
->window
, name
, e
->type
);
11124 if (e
->type
== ewmh
[_NET_CURRENT_DESKTOP
].atom
) {
11125 num_screens
= get_screen_count();
11126 for (i
= 0; i
< num_screens
; i
++)
11127 if (screens
[i
].root
== e
->window
) {
11128 r
= screens
[i
].r_focus
;
11132 if (r
&& e
->data
.data32
[0] < (uint32_t)workspace_limit
) {
11133 a
.id
= e
->data
.data32
[0];
11134 switchws(NULL
, r
, &a
);
11139 } else if (e
->type
== ewmh
[_NET_REQUEST_FRAME_EXTENTS
].atom
) {
11140 DNPRINTF(SWM_D_EVENT
,
11141 "clientmessage: set _NET_FRAME_EXTENTS on window.\n");
11142 vals
[0] = vals
[1] = vals
[2] = vals
[3] = border_width
;
11143 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, e
->window
,
11144 a_net_frame_extents
, XCB_ATOM_CARDINAL
, 32, 4, vals
);
11149 win
= find_window(e
->window
);
11151 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11152 /* Manage the window with maprequest. */
11153 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
11154 "unmanaged window.\n");
11155 mre
.window
= e
->window
;
11161 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11162 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW, "
11163 "source_type: %s(%d)\n",
11164 get_source_type_label(e
->data
.data32
[0]),
11165 e
->data
.data32
[0]);
11168 * Allow focus changes that are a result of direct user
11169 * action and from applications that use the old EWMH spec.
11171 if (e
->data
.data32
[0] != EWMH_SOURCE_TYPE_NORMAL
||
11172 win
->quirks
& SWM_Q_OBEYAPPFOCUSREQ
) {
11173 if (WS_FOCUSED(win
->ws
))
11176 win
->ws
->focus_pending
= win
;
11178 } else if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
11179 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
11180 if (win
->can_delete
)
11181 client_msg(win
, a_delete
, 0);
11183 xcb_kill_client(conn
, win
->id
);
11184 } else if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
11185 DNPRINTF(SWM_D_EVENT
,
11186 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
11188 if (e
->data
.data32
[0] & (1<<8)) /* x */
11189 X(win
) = e
->data
.data32
[1];
11190 if (e
->data
.data32
[0] & (1<<9)) /* y */
11191 Y(win
) = e
->data
.data32
[2];
11192 if (e
->data
.data32
[0] & (1<<10)) /* width */
11193 WIDTH(win
) = e
->data
.data32
[3];
11194 if (e
->data
.data32
[0] & (1<<11)) /* height */
11195 HEIGHT(win
) = e
->data
.data32
[4];
11197 update_window(win
);
11199 /* Notify no change was made. */
11200 config_win(win
, NULL
);
11201 /* TODO: Change stack sizes */
11203 } else if (e
->type
== ewmh
[_NET_RESTACK_WINDOW
].atom
) {
11204 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_RESTACK_WINDOW\n");
11205 vals
[0] = e
->data
.data32
[1]; /* Sibling window. */
11206 vals
[1] = e
->data
.data32
[2]; /* Stack mode detail. */
11208 if (win
->frame
!= XCB_WINDOW_NONE
)
11209 xcb_configure_window(conn
, win
->frame
,
11210 XCB_CONFIG_WINDOW_SIBLING
|
11211 XCB_CONFIG_WINDOW_STACK_MODE
, vals
);
11212 } else if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
11213 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
11214 ewmh_change_wm_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
11215 if (e
->data
.data32
[2])
11216 ewmh_change_wm_state(win
, e
->data
.data32
[2],
11217 e
->data
.data32
[0]);
11219 ewmh_update_wm_state(win
);
11221 } else if (e
->type
== ewmh
[_NET_WM_DESKTOP
].atom
) {
11222 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_DESKTOP\n");
11225 win_to_ws(win
, e
->data
.data32
[0], true);
11227 /* Stack source and destination ws, if mapped. */
11228 if (r
!= win
->ws
->r
) {
11234 load_float_geom(win
);
11247 int errcode
= xcb_connection_has_error(conn
);
11248 #ifdef XCB_CONN_ERROR
11251 case XCB_CONN_ERROR
:
11252 s
= "Socket error, pipe error or other stream error.";
11254 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
11255 s
= "Extension not supported.";
11257 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
11258 s
= "Insufficient memory.";
11260 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
11261 s
= "Request length was exceeded.";
11263 case XCB_CONN_CLOSED_PARSE_ERR
:
11264 s
= "Error parsing display string.";
11267 s
= "Unknown error.";
11270 errx(errcode
, "X CONNECTION ERROR: %s", s
);
11273 errx(errcode
, "X CONNECTION ERROR");
11280 int num_screens
, i
;
11281 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
11282 XCB_EVENT_MASK_ENTER_WINDOW
|
11283 XCB_EVENT_MASK_OWNER_GRAB_BUTTON
|
11284 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
11285 XCB_EVENT_MASK_ENTER_WINDOW
|
11286 XCB_EVENT_MASK_LEAVE_WINDOW
|
11287 XCB_EVENT_MASK_BUTTON_PRESS
|
11288 XCB_EVENT_MASK_BUTTON_RELEASE
|
11289 XCB_EVENT_MASK_KEY_PRESS
|
11290 XCB_EVENT_MASK_KEY_RELEASE
|
11291 XCB_EVENT_MASK_PROPERTY_CHANGE
;
11293 xcb_void_cookie_t wac
;
11294 xcb_generic_error_t
*error
;
11296 /* this causes an error if some other window manager is running */
11297 num_screens
= get_screen_count();
11298 for (i
= 0; i
< num_screens
; i
++) {
11299 if ((sc
= get_screen(i
)) == NULL
)
11300 errx(1, "ERROR: can't get screen %d.", i
);
11301 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: %#x\n",
11303 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
11304 XCB_CW_EVENT_MASK
, &val
);
11305 if ((error
= xcb_request_check(conn
, wac
))) {
11306 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
11307 error
->error_code
);
11317 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
11319 struct swm_region
*r
= NULL
, *n
;
11320 struct workspace
*ws
= NULL
;
11324 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
11325 s
->idx
, w
, h
, x
, y
);
11327 /* remove any conflicting regions */
11328 n
= TAILQ_FIRST(&s
->rl
);
11331 n
= TAILQ_NEXT(r
, entry
);
11332 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
11333 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
11334 if (r
->ws
->r
!= NULL
)
11335 r
->ws
->old_r
= r
->ws
->r
;
11338 xcb_destroy_window(conn
, r
->id
);
11339 r
->id
= XCB_WINDOW_NONE
;
11340 TAILQ_REMOVE(&s
->rl
, r
, entry
);
11341 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
11345 /* search old regions for one to reuse */
11347 /* size + location match */
11348 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11349 if (X(r
) == x
&& Y(r
) == y
&&
11350 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11354 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11355 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11359 TAILQ_REMOVE(&s
->orl
, r
, entry
);
11360 /* try to use old region's workspace */
11361 if (r
->ws
->r
== NULL
)
11364 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
11365 err(1, "new_region: calloc: failed to allocate memory "
11368 /* if we don't have a workspace already, find one */
11370 for (i
= 0; i
< workspace_limit
; i
++)
11371 if (s
->ws
[i
].r
== NULL
) {
11378 errx(1, "new_region: no free workspaces");
11380 if (ws
->state
== SWM_WS_STATE_HIDDEN
)
11381 ws
->state
= SWM_WS_STATE_MAPPING
;
11390 r
->ws_prior
= NULL
;
11393 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
11395 /* Invisible region window to detect pointer events on empty regions. */
11396 r
->id
= xcb_generate_id(conn
);
11397 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
11398 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
11400 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
11401 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
11402 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
11404 /* Make sure region input is at the bottom. */
11405 wa
[0] = XCB_STACK_MODE_BELOW
;
11406 xcb_configure_window(conn
, r
->id
, XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
11408 xcb_map_window(conn
, r
->id
);
11412 scan_randr(int idx
)
11414 #ifdef SWM_XRR_HAS_CRTC
11417 #endif /* SWM_XRR_HAS_CRTC */
11418 struct swm_region
*r
;
11419 struct ws_win
*win
;
11421 xcb_randr_get_screen_resources_current_cookie_t src
;
11422 xcb_randr_get_screen_resources_current_reply_t
*srr
;
11423 xcb_randr_get_crtc_info_cookie_t cic
;
11424 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
11425 xcb_randr_crtc_t
*crtc
;
11426 xcb_screen_t
*screen
;
11428 DNPRINTF(SWM_D_MISC
, "scan_randr: screen: %d\n", idx
);
11430 if ((screen
= get_screen(idx
)) == NULL
)
11431 errx(1, "ERROR: can't get screen %d.", idx
);
11433 num_screens
= get_screen_count();
11434 if (idx
>= num_screens
)
11435 errx(1, "scan_randr: invalid screen");
11437 /* remove any old regions */
11438 while ((r
= TAILQ_FIRST(&screens
[idx
].rl
)) != NULL
) {
11439 r
->ws
->old_r
= r
->ws
->r
= NULL
;
11441 xcb_destroy_window(conn
, r
->id
);
11442 r
->id
= XCB_WINDOW_NONE
;
11443 TAILQ_REMOVE(&screens
[idx
].rl
, r
, entry
);
11444 TAILQ_INSERT_TAIL(&screens
[idx
].orl
, r
, entry
);
11448 /* map virtual screens onto physical screens */
11449 #ifdef SWM_XRR_HAS_CRTC
11450 if (randr_support
) {
11451 src
= xcb_randr_get_screen_resources_current(conn
,
11452 screens
[idx
].root
);
11453 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
11456 new_region(&screens
[idx
], 0, 0,
11457 screen
->width_in_pixels
,
11458 screen
->height_in_pixels
);
11461 ncrtc
= srr
->num_crtcs
;
11463 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
11464 for (c
= 0; c
< ncrtc
; c
++) {
11465 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
11467 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
11470 if (cir
->num_outputs
== 0) {
11475 if (cir
->mode
== 0)
11476 new_region(&screens
[idx
], 0, 0,
11477 screen
->width_in_pixels
,
11478 screen
->height_in_pixels
);
11480 new_region(&screens
[idx
],
11481 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
11486 #endif /* SWM_XRR_HAS_CRTC */
11488 /* If detection failed, create a single region that spans the screen. */
11489 if (TAILQ_EMPTY(&screens
[idx
].rl
))
11490 new_region(&screens
[idx
], 0, 0, screen
->width_in_pixels
,
11491 screen
->height_in_pixels
);
11494 /* Cleanup unused previously visible workspaces. */
11495 TAILQ_FOREACH(r
, &screens
[idx
].orl
, entry
) {
11496 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
11498 r
->ws
->state
= SWM_WS_STATE_HIDDEN
;
11500 /* The screen shouldn't focus on an unused region. */
11501 if (screens
[idx
].r_focus
== r
)
11502 screens
[idx
].r_focus
= NULL
;
11505 DNPRINTF(SWM_D_MISC
, "scan_randr: done.\n");
11509 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
11511 struct swm_region
*r
;
11512 int i
, num_screens
;
11514 DNPRINTF(SWM_D_EVENT
, "screenchange: root: %#x\n", e
->root
);
11516 num_screens
= get_screen_count();
11517 /* silly event doesn't include the screen index */
11518 for (i
= 0; i
< num_screens
; i
++)
11519 if (screens
[i
].root
== e
->root
)
11521 if (i
>= num_screens
)
11522 errx(1, "screenchange: screen not found");
11524 /* brute force for now, just re-enumerate the regions */
11528 print_win_geom(e
->root
);
11530 /* add bars to all regions */
11531 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11534 /* Stack all regions. */
11535 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11538 /* Make sure a region has focus. */
11539 if (screens
[i
].r_focus
== NULL
) {
11540 r
= TAILQ_FIRST(&screens
[i
].rl
);
11547 /* Update workspace state and bar on all regions. */
11548 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11549 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
11557 struct swm_region
*r
= NULL
;
11558 xcb_query_tree_cookie_t qtc
;
11559 xcb_query_tree_reply_t
*qtr
;
11560 xcb_get_property_cookie_t pc
;
11561 xcb_get_property_reply_t
*pr
;
11562 xcb_window_t
*wins
= NULL
, trans
, *cwins
= NULL
;
11563 int i
, j
, k
, n
, no
, num_screens
;
11565 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
11566 num_screens
= get_screen_count();
11567 for (i
= 0; i
< num_screens
; i
++) {
11568 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
11569 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
11572 wins
= xcb_query_tree_children(qtr
);
11573 no
= xcb_query_tree_children_length(qtr
);
11575 /* Try to sort windows according to _NET_CLIENT_LIST. */
11576 pr
= xcb_get_property_reply(conn
, xcb_get_property(conn
, 0,
11577 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
11578 XCB_ATOM_WINDOW
, 0, UINT32_MAX
), NULL
);
11580 cwins
= xcb_get_property_value(pr
);
11581 n
= xcb_get_property_value_length(pr
) /
11582 sizeof(xcb_atom_t
);
11584 for (j
= 0; j
< n
; ++j
) {
11585 for (k
= j
; k
< no
; ++k
) {
11586 if (wins
[k
] == cwins
[j
]) {
11587 /* Swap wins j and k. */
11589 wins
[j
] = cwins
[j
];
11597 /* Manage top-level windows first, then transients. */
11598 /* TODO: allow transients to be managed before leader. */
11599 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top-level windows.\n");
11600 for (j
= 0; j
< no
; j
++) {
11601 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11602 if (r
->id
== wins
[j
]) {
11603 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11604 "skip %#x; region input window.\n",
11607 } else if (r
->bar
->id
== wins
[j
]) {
11608 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11609 "skip %#x; region bar.\n",
11618 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11619 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11621 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
11622 "is transient for %#x.\n", wins
[j
], trans
);
11626 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11629 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
11630 for (j
= 0; j
< no
; j
++) {
11631 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11632 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11634 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11638 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
11642 setup_screens(void)
11644 int i
, j
, k
, num_screens
;
11645 struct workspace
*ws
;
11646 uint32_t gcv
[1], wa
[1];
11647 const xcb_query_extension_reply_t
*qep
;
11648 xcb_screen_t
*screen
;
11649 xcb_randr_query_version_cookie_t c
;
11650 xcb_randr_query_version_reply_t
*r
;
11652 num_screens
= get_screen_count();
11653 if ((screens
= calloc(num_screens
, sizeof(struct swm_screen
))) == NULL
)
11654 err(1, "setup_screens: calloc: failed to allocate memory for "
11657 /* Initial RandR setup. */
11658 randr_support
= false;
11659 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
11660 if (qep
->present
) {
11661 c
= xcb_randr_query_version(conn
, 1, 1);
11662 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
11664 if (r
->major_version
>= 1) {
11665 randr_support
= true;
11666 randr_eventbase
= qep
->first_event
;
11672 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
11674 /* map physical screens */
11675 for (i
= 0; i
< num_screens
; i
++) {
11676 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
11677 screens
[i
].idx
= i
;
11678 screens
[i
].r_focus
= NULL
;
11680 TAILQ_INIT(&screens
[i
].rl
);
11681 TAILQ_INIT(&screens
[i
].orl
);
11682 if ((screen
= get_screen(i
)) == NULL
)
11683 errx(1, "ERROR: can't get screen %d.", i
);
11684 screens
[i
].root
= screen
->root
;
11686 /* set default colors */
11687 setscreencolor("red", i
, SWM_S_COLOR_FOCUS
);
11688 setscreencolor("rgb:88/88/88", i
, SWM_S_COLOR_UNFOCUS
);
11689 setscreencolor("rgb:00/80/80", i
, SWM_S_COLOR_BAR_BORDER
);
11690 setscreencolor("rgb:00/40/40", i
,
11691 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
11692 setscreencolor("black", i
, SWM_S_COLOR_BAR
);
11693 setscreencolor("rgb:a0/a0/a0", i
, SWM_S_COLOR_BAR_FONT
);
11694 setscreencolor("red", i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
11695 setscreencolor("rgb:88/88/88", i
,
11696 SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
11698 /* create graphics context on screen */
11699 screens
[i
].bar_gc
= xcb_generate_id(conn
);
11701 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
11702 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
11704 /* set default cursor */
11705 xcb_change_window_attributes(conn
, screens
[i
].root
,
11706 XCB_CW_CURSOR
, wa
);
11708 /* init all workspaces */
11709 /* XXX these should be dynamically allocated too */
11710 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
11711 ws
= &screens
[i
].ws
[j
];
11714 ws
->bar_enabled
= true;
11716 ws
->focus_prev
= NULL
;
11717 ws
->focus_pending
= NULL
;
11720 ws
->state
= SWM_WS_STATE_HIDDEN
;
11721 TAILQ_INIT(&ws
->stack
);
11722 TAILQ_INIT(&ws
->winlist
);
11723 TAILQ_INIT(&ws
->unmanagedlist
);
11725 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
11726 if (layouts
[k
].l_config
!= NULL
)
11727 layouts
[k
].l_config(ws
,
11728 SWM_ARG_ID_STACKINIT
);
11729 ws
->cur_layout
= &layouts
[0];
11730 ws
->cur_layout
->l_string(ws
);
11736 xcb_randr_select_input(conn
, screens
[i
].root
,
11737 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
11742 setup_globals(void)
11744 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
11745 err(1, "setup_globals: strdup: failed to allocate memory.");
11747 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
11748 err(1, "setup_globals: strdup: failed to allocate memory.");
11750 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
11751 errx(1, "unable to allocate key symbols");
11753 a_state
= get_atom_from_string("WM_STATE");
11754 a_prot
= get_atom_from_string("WM_PROTOCOLS");
11755 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
11756 a_net_frame_extents
= get_atom_from_string("_NET_FRAME_EXTENTS");
11757 a_net_supported
= get_atom_from_string("_NET_SUPPORTED");
11758 a_net_wm_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
11759 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
11760 a_utf8_string
= get_atom_from_string("UTF8_STRING");
11761 a_swm_ws
= get_atom_from_string("_SWM_WS");
11765 shutdown_cleanup(void)
11767 struct swm_region
*r
;
11769 struct workspace
*ws
;
11770 int i
, num_screens
;
11772 /* disable alarm because the following code may not be interrupted */
11774 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
11775 err(1, "can't disable alarm");
11788 num_screens
= get_screen_count();
11789 for (i
= 0; i
< num_screens
; ++i
) {
11792 DNPRINTF(SWM_D_FOCUS
, "shutdown_cleanup: set_input_focus: "
11793 "%#x, revert-to: root, time: 0\n", screens
[i
].root
);
11794 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11795 screens
[i
].root
, XCB_CURRENT_TIME
);
11797 if (screens
[i
].bar_gc
!= XCB_NONE
)
11798 xcb_free_gc(conn
, screens
[i
].bar_gc
);
11799 if (!bar_font_legacy
) {
11800 XftColorFree(display
, DefaultVisual(display
, i
),
11801 DefaultColormap(display
, i
), &bar_font_color
);
11802 XftColorFree(display
, DefaultVisual(display
, i
),
11803 DefaultColormap(display
, i
), &search_font_color
);
11806 for (j
= 0; j
< SWM_S_COLOR_MAX
; ++j
) {
11807 free(screens
[i
].c
[j
].name
);
11810 /* Free window memory. */
11811 for (j
= 0; j
< SWM_WS_MAX
; ++j
) {
11812 ws
= &screens
[i
].ws
[j
];
11815 while ((w
= TAILQ_FIRST(&ws
->winlist
)) != NULL
) {
11816 TAILQ_REMOVE(&ws
->winlist
, w
, entry
);
11820 while ((w
= TAILQ_FIRST(&ws
->unmanagedlist
)) != NULL
) {
11821 TAILQ_REMOVE(&ws
->unmanagedlist
, w
, entry
);
11826 /* Free region memory. */
11827 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
11828 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
11833 while ((r
= TAILQ_FIRST(&screens
[i
].orl
)) != NULL
) {
11834 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
11843 free(clock_format
);
11844 free(startup_exception
);
11847 XFreeFontSet(display
, bar_fs
);
11849 XftFontClose(display
, bar_font
);
11851 xcb_key_symbols_free(syms
);
11853 xcb_aux_sync(conn
);
11854 xcb_disconnect(conn
);
11858 event_error(xcb_generic_error_t
*e
)
11862 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
11863 "resource_id: %u, minor_code: %u\n",
11864 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
11865 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
11866 e
->sequence
, e
->resource_id
, e
->minor_code
);
11870 event_handle(xcb_generic_event_t
*evt
)
11872 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
11874 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
11875 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
11876 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
11879 #define EVENT(type, callback) case type: callback((void *)evt); return
11880 EVENT(0, event_error
);
11881 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
11882 EVENT(XCB_BUTTON_RELEASE
, buttonrelease
);
11883 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
11884 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
11885 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
11886 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
11887 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
11888 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
11889 /*EVENT(XCB_CREATE_NOTIFY, );*/
11890 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
11891 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
11892 EVENT(XCB_EXPOSE
, expose
);
11893 EVENT(XCB_FOCUS_IN
, focusin
);
11895 EVENT(XCB_FOCUS_OUT
, focusout
);
11897 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
11898 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
11899 EVENT(XCB_KEY_PRESS
, keypress
);
11900 EVENT(XCB_KEY_RELEASE
, keyrelease
);
11901 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
11903 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
11905 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
11906 EVENT(XCB_MAP_REQUEST
, maprequest
);
11907 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
11908 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
11909 /*EVENT(XCB_NO_EXPOSURE, );*/
11910 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
11911 EVENT(XCB_REPARENT_NOTIFY
, reparentnotify
);
11912 /*EVENT(XCB_RESIZE_REQUEST, );*/
11913 /*EVENT(XCB_SELECTION_CLEAR, );*/
11914 /*EVENT(XCB_SELECTION_NOTIFY, );*/
11915 /*EVENT(XCB_SELECTION_REQUEST, );*/
11916 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
11917 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
11920 if (type
- randr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
11921 screenchange((void *)evt
);
11925 main(int argc
, char *argv
[])
11927 struct pollfd pfd
[2];
11928 struct sigaction sact
;
11930 struct passwd
*pwd
;
11931 struct swm_region
*r
;
11932 xcb_generic_event_t
*evt
;
11933 int xfd
, i
, num_screens
, num_readable
;
11934 char conf
[PATH_MAX
], *cfile
= NULL
;
11935 bool stdin_ready
= false, startup
= true;
11937 /* suppress unused warning since var is needed */
11941 time_started
= time(NULL
);
11945 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
11946 if (setlocale(LC_CTYPE
, "") == NULL
|| setlocale(LC_TIME
, "") == NULL
)
11947 warnx("no locale support");
11949 /* handle some signals */
11950 bzero(&sact
, sizeof(sact
));
11951 sigemptyset(&sact
.sa_mask
);
11953 sact
.sa_handler
= sighdlr
;
11954 sigaction(SIGINT
, &sact
, NULL
);
11955 sigaction(SIGQUIT
, &sact
, NULL
);
11956 sigaction(SIGTERM
, &sact
, NULL
);
11957 sigaction(SIGHUP
, &sact
, NULL
);
11959 sact
.sa_handler
= sighdlr
;
11960 sact
.sa_flags
= SA_NOCLDSTOP
;
11961 sigaction(SIGCHLD
, &sact
, NULL
);
11963 if ((display
= XOpenDisplay(0)) == NULL
)
11964 errx(1, "can not open display");
11966 conn
= XGetXCBConnection(display
);
11967 if (xcb_connection_has_error(conn
))
11968 errx(1, "can not get XCB connection");
11970 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
11972 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
11973 xfd
= xcb_get_file_descriptor(conn
);
11975 /* look for local and global conf file */
11976 pwd
= getpwuid(getuid());
11978 errx(1, "invalid user: %d", getuid());
11980 xcb_grab_server(conn
);
11981 xcb_aux_sync(conn
);
11983 /* flush all events */
11984 while ((evt
= get_next_event(false))) {
11985 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
11991 errx(1, "another window manager is currently running");
11993 /* Load Xcursors and/or cursorfont glyph cursors. */
11996 xcb_aux_sync(conn
);
12001 setup_keybindings();
12002 setup_btnbindings();
12007 for (i
= 0; ; i
++) {
12012 snprintf(conf
, sizeof conf
, "%s/.%s",
12013 pwd
->pw_dir
, SWM_CONF_FILE
);
12017 snprintf(conf
, sizeof conf
, "/etc/%s",
12022 snprintf(conf
, sizeof conf
, "%s/.%s",
12023 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
12026 /* global compat */
12027 snprintf(conf
, sizeof conf
, "/etc/%s",
12028 SWM_CONF_FILE_OLD
);
12034 if (strlen(conf
) && stat(conf
, &sb
) != -1)
12035 if (S_ISREG(sb
.st_mode
)) {
12042 /* load conf (if any) */
12044 conf_load(cfile
, SWM_CONF_DEFAULT
);
12048 if (getenv("SWM_STARTED") == NULL
)
12049 setenv("SWM_STARTED", "YES", 1);
12051 /* setup all bars */
12052 num_screens
= get_screen_count();
12053 for (i
= 0; i
< num_screens
; i
++)
12054 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12057 /* Manage existing windows. */
12063 /* Stack all regions to trigger mapping. */
12064 for (i
= 0; i
< num_screens
; i
++)
12065 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12068 xcb_ungrab_server(conn
);
12071 /* Update state and bar of each newly mapped workspace. */
12072 for (i
= 0; i
< num_screens
; i
++)
12073 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
12074 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
12078 memset(&pfd
, 0, sizeof(pfd
));
12080 pfd
[0].events
= POLLIN
;
12081 pfd
[1].fd
= STDIN_FILENO
;
12082 pfd
[1].events
= POLLIN
;
12085 while ((evt
= get_next_event(false))) {
12092 /* If just (re)started, set default focus if needed. */
12096 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
12097 r
= TAILQ_FIRST(&screens
[0].rl
);
12109 num_readable
= poll(pfd
, bar_extra
? 2 : 1, 1000);
12110 if (num_readable
== -1) {
12111 DNPRINTF(SWM_D_MISC
, "poll failed: %s",
12113 } else if (num_readable
> 0 && bar_extra
&&
12114 pfd
[1].revents
& POLLIN
) {
12115 stdin_ready
= true;
12119 restart(NULL
, NULL
, NULL
);
12125 stdin_ready
= false;
12126 bar_extra_update();
12129 /* Need to ensure the bar(s) are always updated. */
12130 for (i
= 0; i
< num_screens
; i
++)
12131 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12137 shutdown_cleanup();