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>
73 #include <xcb/xcb_atom.h>
74 #include <xcb/xcb_aux.h>
75 #include <xcb/xcb_event.h>
76 #include <xcb/xcb_icccm.h>
77 #include <xcb/xcb_keysyms.h>
78 #include <xcb/xtest.h>
79 #include <xcb/randr.h>
87 #ifdef SPECTRWM_BUILDSTR
88 static const char *buildstr
= SPECTRWM_BUILDSTR
;
90 static const char *buildstr
= SPECTRWM_VERSION
;
93 #if !defined(__CYGWIN__) /* cygwin chokes on randr stuff */
95 # error RandR versions less than 1.0 are not supported
100 # define SWM_XRR_HAS_CRTC
103 #endif /* __CYGWIN__ */
105 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
106 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
107 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
108 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
109 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
110 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
111 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
112 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
113 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
114 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
115 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
116 #define xcb_icccm_get_wm_class xcb_get_wm_class
117 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
118 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
119 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
120 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
121 #define xcb_icccm_wm_hints_get_urgency xcb_wm_hints_get_urgency
122 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
123 #define xcb_icccm_get_wm_name xcb_get_wm_name
124 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
125 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
126 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
127 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
128 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
129 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
130 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
131 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
132 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
133 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
136 /*#define SWM_DEBUG*/
138 #define DPRINTF(x...) do { \
140 fprintf(stderr, x); \
142 #define DNPRINTF(n,x...) do { \
143 if (swm_debug & n) { \
144 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
145 fprintf(stderr, x); \
148 #define SWM_D_MISC 0x0001
149 #define SWM_D_EVENT 0x0002
150 #define SWM_D_WS 0x0004
151 #define SWM_D_FOCUS 0x0008
152 #define SWM_D_MOVE 0x0010
153 #define SWM_D_STACK 0x0020
154 #define SWM_D_MOUSE 0x0040
155 #define SWM_D_PROP 0x0080
156 #define SWM_D_CLASS 0x0100
157 #define SWM_D_KEY 0x0200
158 #define SWM_D_QUIRK 0x0400
159 #define SWM_D_SPAWN 0x0800
160 #define SWM_D_EVENTQ 0x1000
161 #define SWM_D_CONF 0x2000
162 #define SWM_D_BAR 0x4000
163 #define SWM_D_INIT 0x8000
165 uint32_t swm_debug
= 0
184 #define DPRINTF(x...)
185 #define DNPRINTF(n,x...)
188 #define SWM_EWMH_ACTION_COUNT_MAX (8)
189 #define EWMH_F_FULLSCREEN (0x001)
190 #define EWMH_F_ABOVE (0x002)
191 #define EWMH_F_HIDDEN (0x004)
192 #define EWMH_F_MAXIMIZED_VERT (0x008)
193 #define EWMH_F_MAXIMIZED_HORZ (0x010)
194 #define EWMH_F_SKIP_PAGER (0x020)
195 #define EWMH_F_SKIP_TASKBAR (0x040)
196 #define SWM_F_MANUAL (0x080)
198 #define EWMH_F_MAXIMIZED (EWMH_F_MAXIMIZED_VERT | EWMH_F_MAXIMIZED_HORZ)
200 /* convert 8-bit to 16-bit */
201 #define RGB_8_TO_16(col) (((col) << 8) + (col))
203 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
204 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
205 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
206 xrc.blue = RGB_8_TO_16((px) & 0xff); \
209 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
210 #define MODKEY XCB_MOD_MASK_1
211 #define ANYMOD XCB_MOD_MASK_ANY
212 #define CLEANMASK(mask) ((mask) & (XCB_KEY_BUT_MASK_SHIFT | \
213 XCB_KEY_BUT_MASK_CONTROL | XCB_KEY_BUT_MASK_MOD_1 | \
214 XCB_KEY_BUT_MASK_MOD_2 | XCB_KEY_BUT_MASK_MOD_3 | \
215 XCB_KEY_BUT_MASK_MOD_4 | XCB_KEY_BUT_MASK_MOD_5) & ~(numlockmask))
216 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
217 XCB_EVENT_MASK_BUTTON_RELEASE)
218 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
219 #define SWM_PROPLEN (16)
220 #define SWM_FUNCNAME_LEN (32)
221 #define SWM_QUIRK_LEN (64)
222 #define X(r) ((r)->g.x)
223 #define Y(r) ((r)->g.y)
224 #define WIDTH(r) ((r)->g.w)
225 #define HEIGHT(r) ((r)->g.h)
226 #define MAX_X(r) ((r)->g.x + (r)->g.w)
227 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
228 #define SH_MIN(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
229 #define SH_MIN_W(w) ((w)->sh.min_width)
230 #define SH_MIN_H(w) ((w)->sh.min_height)
231 #define SH_MAX(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
232 #define SH_MAX_W(w) ((w)->sh.max_width)
233 #define SH_MAX_H(w) ((w)->sh.max_height)
234 #define SH_INC(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)
235 #define SH_INC_W(w) ((w)->sh.width_inc)
236 #define SH_INC_H(w) ((w)->sh.height_inc)
237 #define SWM_MAX_FONT_STEPS (3)
238 #define WINID(w) ((w) ? (w)->id : XCB_WINDOW_NONE)
239 #define ACCEPTS_FOCUS(w) (!((w)->hints.flags & XCB_ICCCM_WM_HINT_INPUT) \
240 || ((w)->hints.input))
241 #define WS_FOCUSED(ws) ((ws)->r && (ws)->r->s->r_focus == (ws)->r)
242 #define YESNO(x) ((x) ? "yes" : "no")
243 #define ICONIC(w) ((w)->ewmh_flags & EWMH_F_HIDDEN)
244 #define ABOVE(w) ((w)->ewmh_flags & EWMH_F_ABOVE)
245 #define FULLSCREEN(w) ((w)->ewmh_flags & EWMH_F_FULLSCREEN)
246 #define MAXIMIZED_VERT(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_VERT)
247 #define MAXIMIZED_HORZ(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_HORZ)
248 #define MAXIMIZED(w) (MAXIMIZED_VERT(w) || MAXIMIZED_HORZ(w))
249 #define MANUAL(w) ((w)->ewmh_flags & SWM_F_MANUAL)
250 #define TRANS(w) ((w)->transient != XCB_WINDOW_NONE)
251 #define FLOATING(w) (ABOVE(w) || TRANS(w))
252 #define FLOATINGFULLMAX(w) (ABOVE(w) || TRANS(w) || FULLSCREEN(w) || \
255 /* Constrain Window flags */
256 #define SWM_CW_RESIZABLE (0x01)
257 #define SWM_CW_SOFTBOUNDARY (0x02)
258 #define SWM_CW_HARDBOUNDARY (0x04)
259 #define SWM_CW_RIGHT (0x10)
260 #define SWM_CW_LEFT (0x20)
261 #define SWM_CW_BOTTOM (0x40)
262 #define SWM_CW_TOP (0x80)
263 #define SWM_CW_ALLSIDES (0xf0)
265 #define SWM_FOCUS_DEFAULT (0)
266 #define SWM_FOCUS_FOLLOW (1)
267 #define SWM_FOCUS_MANUAL (2)
269 #define SWM_CK_ALL (0xf)
270 #define SWM_CK_FOCUS (0x1)
271 #define SWM_CK_POINTER (0x2)
272 #define SWM_CK_FALLBACK (0x4)
273 #define SWM_CK_REGION (0x8)
275 #define SWM_CONF_DEFAULT (0)
276 #define SWM_CONF_KEYMAPPING (1)
279 #define SWM_LIB "/usr/local/lib/libswmhack.so"
286 xcb_atom_t a_net_frame_extents
;
287 xcb_atom_t a_net_wm_check
;
288 xcb_atom_t a_net_supported
;
289 xcb_atom_t a_takefocus
;
290 xcb_atom_t a_utf8_string
;
292 volatile sig_atomic_t running
= 1;
293 volatile sig_atomic_t restart_wm
= 0;
294 xcb_timestamp_t last_event_time
= 0;
298 unsigned int numlockmask
= 0;
301 xcb_connection_t
*conn
;
302 xcb_key_symbols_t
*syms
;
304 int boundary_width
= 50;
305 bool cycle_empty
= false;
306 bool cycle_visible
= false;
308 int font_adjusted
= 0;
309 uint16_t mod_key
= MODKEY
;
310 bool warp_focus
= false;
311 bool warp_pointer
= false;
312 bool workspace_clamp
= false;
315 struct swm_region
*search_r
;
316 int select_list_pipe
[2];
317 int select_resp_pipe
[2];
319 volatile sig_atomic_t search_resp
;
320 int search_resp_action
;
322 struct search_window
{
323 TAILQ_ENTRY(search_window
) entry
;
327 xcb_window_t indicator
;
329 TAILQ_HEAD(search_winlist
, search_window
);
330 struct search_winlist search_wl
;
335 SWM_SEARCH_UNICONIFY
,
336 SWM_SEARCH_NAME_WORKSPACE
,
337 SWM_SEARCH_SEARCH_WORKSPACE
,
338 SWM_SEARCH_SEARCH_WINDOW
341 #define SWM_STACK_TOP (0)
342 #define SWM_STACK_BOTTOM (1)
343 #define SWM_STACK_ABOVE (2)
344 #define SWM_STACK_BELOW (3)
347 double dialog_ratio
= 0.6;
349 #define SWM_BAR_MAX (256)
350 #define SWM_BAR_JUSTIFY_LEFT (0)
351 #define SWM_BAR_JUSTIFY_CENTER (1)
352 #define SWM_BAR_JUSTIFY_RIGHT (2)
353 #define SWM_BAR_OFFSET (4)
354 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
355 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
356 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
357 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
358 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
360 #ifdef X_HAVE_UTF8_STRING
361 #define DRAWSTRING(x...) Xutf8DrawString(x)
363 #define DRAWSTRING(x...) XmbDrawString(x)
366 char *bar_argv
[] = { NULL
, NULL
};
368 char bar_ext
[SWM_BAR_MAX
];
369 char bar_ext_buf
[SWM_BAR_MAX
];
370 char bar_vertext
[SWM_BAR_MAX
];
371 bool bar_version
= false;
372 bool bar_enabled
= true;
373 int bar_border_width
= 1;
374 bool bar_at_bottom
= false;
375 bool bar_extra
= false;
377 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
378 char *bar_format
= NULL
;
379 bool stack_enabled
= true;
380 bool clock_enabled
= true;
381 bool iconic_enabled
= false;
382 bool maximize_hide_bar
= false;
383 bool urgent_enabled
= false;
384 bool urgent_collapse
= false;
385 char *clock_format
= NULL
;
386 bool window_class_enabled
= false;
387 bool window_instance_enabled
= false;
388 bool window_name_enabled
= false;
389 int focus_mode
= SWM_FOCUS_DEFAULT
;
390 int focus_close
= SWM_STACK_BELOW
;
391 bool focus_close_wrap
= true;
392 int focus_default
= SWM_STACK_TOP
;
393 int spawn_position
= SWM_STACK_TOP
;
394 bool disable_border
= false;
395 int border_width
= 1;
396 int region_padding
= 0;
398 bool java_workaround
= true;
399 bool verbose_layout
= false;
400 bool track_pid_ws
= true;
406 XFontSet bar_fs
= NULL
;
407 XFontSetExtents
*bar_fs_extents
;
408 XftFont
*bar_font
= NULL
;
409 bool bar_font_legacy
= true;
410 char *bar_fonts
= NULL
;
411 XftColor bar_font_color
;
412 XftColor search_font_color
;
413 char *startup_exception
= NULL
;
414 unsigned int nr_exceptions
= 0;
416 /* layout manager data */
417 struct swm_geometry
{
430 struct swm_geometry g
;
431 struct swm_region
*r
; /* Associated region. */
434 /* virtual "screens" */
436 TAILQ_ENTRY(swm_region
) entry
;
438 struct swm_geometry g
;
439 struct workspace
*ws
; /* current workspace on this region */
440 struct workspace
*ws_prior
; /* prior workspace on this region */
441 struct swm_screen
*s
; /* screen idx */
444 TAILQ_HEAD(swm_region_list
, swm_region
);
447 SWM_WIN_STATE_REPARENTING
,
448 SWM_WIN_STATE_REPARENTED
,
449 SWM_WIN_STATE_UNPARENTING
,
450 SWM_WIN_STATE_UNPARENTED
,
454 TAILQ_ENTRY(ws_win
) entry
;
455 TAILQ_ENTRY(ws_win
) stack_entry
;
458 xcb_window_t transient
;
459 struct ws_win
*focus_child
; /* focus on child transient */
460 struct swm_geometry g
; /* current geometry */
461 struct swm_geometry g_prev
; /* prev configured geometry */
462 struct swm_geometry g_float
; /* region coordinates */
463 bool g_floatvalid
; /* g_float geometry validity */
468 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
475 struct workspace
*ws
; /* always valid */
476 struct swm_screen
*s
; /* always valid, never changes */
478 xcb_icccm_get_wm_class_reply_t ch
;
479 xcb_icccm_wm_hints_t hints
;
484 TAILQ_HEAD(ws_win_list
, ws_win
);
485 TAILQ_HEAD(ws_win_stack
, ws_win
);
489 TAILQ_ENTRY(pid_e
) entry
;
493 TAILQ_HEAD(pid_list
, pid_e
);
494 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
496 /* layout handlers */
497 void stack(struct swm_region
*);
498 void vertical_config(struct workspace
*, int);
499 void vertical_stack(struct workspace
*, struct swm_geometry
*);
500 void horizontal_config(struct workspace
*, int);
501 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
502 void max_stack(struct workspace
*, struct swm_geometry
*);
503 void max_config(struct workspace
*, int);
504 void plain_stacker(struct workspace
*);
505 void fancy_stacker(struct workspace
*);
508 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
509 void (*l_config
)(struct workspace
*, int);
511 #define SWM_L_FOCUSPREV (1<<0)
512 #define SWM_L_MAPONFOCUS (1<<1)
513 void (*l_string
)(struct workspace
*);
515 /* stack, configure */
516 { vertical_stack
, vertical_config
, 0, plain_stacker
},
517 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
518 { max_stack
, max_config
,
519 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
520 { NULL
, NULL
, 0, NULL
},
523 /* position of max_stack mode in the layouts array, index into layouts! */
524 #define SWM_V_STACK (0)
525 #define SWM_H_STACK (1)
526 #define SWM_MAX_STACK (2)
528 #define SWM_H_SLICE (32)
529 #define SWM_V_SLICE (32)
531 /* define work spaces */
533 int idx
; /* workspace index */
534 char *name
; /* workspace name */
535 bool always_raise
; /* raise windows on focus */
536 bool bar_enabled
; /* bar visibility */
537 struct layout
*cur_layout
; /* current layout handlers */
538 struct ws_win
*focus
; /* may be NULL */
539 struct ws_win
*focus_prev
; /* may be NULL */
540 struct ws_win
*focus_pending
; /* may be NULL */
541 struct ws_win
*focus_raise
; /* may be NULL */
542 struct swm_region
*r
; /* may be NULL */
543 struct swm_region
*old_r
; /* may be NULL */
544 struct ws_win_list winlist
; /* list of windows in ws */
545 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
546 struct ws_win_stack stack
; /* stacking order */
547 int state
; /* mapping state */
548 char stacker
[10]; /* display stacker and layout */
552 int horizontal_msize
;
554 int horizontal_stacks
;
555 bool horizontal_flip
;
565 SWM_WS_STATE_MAPPING
,
571 SWM_S_COLOR_BAR_BORDER
,
572 SWM_S_COLOR_BAR_BORDER_UNFOCUS
,
573 SWM_S_COLOR_BAR_FONT
,
575 SWM_S_COLOR_FOCUS_MAXIMIZED
,
577 SWM_S_COLOR_UNFOCUS_MAXIMIZED
,
581 /* physical screen mapping */
582 #define SWM_WS_MAX (22) /* hard limit */
583 int workspace_limit
= 10; /* soft limit */
586 int idx
; /* screen index */
587 struct swm_region_list rl
; /* list of regions on this screen */
588 struct swm_region_list orl
; /* list of old regions */
590 struct workspace ws
[SWM_WS_MAX
];
591 struct swm_region
*r_focus
;
598 } c
[SWM_S_COLOR_MAX
];
600 xcb_gcontext_t bar_gc
;
603 struct swm_screen
*screens
;
605 struct layout_config
{
613 } initial_layouts
[SWM_WS_MAX
];
615 /* args to functions */
618 #define SWM_ARG_ID_FOCUSNEXT (0)
619 #define SWM_ARG_ID_FOCUSPREV (1)
620 #define SWM_ARG_ID_FOCUSMAIN (2)
621 #define SWM_ARG_ID_FOCUSURGENT (3)
622 #define SWM_ARG_ID_SWAPNEXT (10)
623 #define SWM_ARG_ID_SWAPPREV (11)
624 #define SWM_ARG_ID_SWAPMAIN (12)
625 #define SWM_ARG_ID_MOVELAST (13)
626 #define SWM_ARG_ID_MASTERSHRINK (20)
627 #define SWM_ARG_ID_MASTERGROW (21)
628 #define SWM_ARG_ID_MASTERADD (22)
629 #define SWM_ARG_ID_MASTERDEL (23)
630 #define SWM_ARG_ID_FLIPLAYOUT (24)
631 #define SWM_ARG_ID_STACKINIT (31)
632 #define SWM_ARG_ID_STACKBALANCE (32)
633 #define SWM_ARG_ID_CYCLEWS_UP (40)
634 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
635 #define SWM_ARG_ID_CYCLERG_UP (42)
636 #define SWM_ARG_ID_CYCLERG_DOWN (43)
637 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
638 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
639 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
640 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
641 #define SWM_ARG_ID_STACKINC (50)
642 #define SWM_ARG_ID_STACKDEC (51)
643 #define SWM_ARG_ID_DONTCENTER (70)
644 #define SWM_ARG_ID_CENTER (71)
645 #define SWM_ARG_ID_KILLWINDOW (80)
646 #define SWM_ARG_ID_DELETEWINDOW (81)
647 #define SWM_ARG_ID_WIDTHGROW (90)
648 #define SWM_ARG_ID_WIDTHSHRINK (91)
649 #define SWM_ARG_ID_HEIGHTGROW (92)
650 #define SWM_ARG_ID_HEIGHTSHRINK (93)
651 #define SWM_ARG_ID_MOVEUP (100)
652 #define SWM_ARG_ID_MOVEDOWN (101)
653 #define SWM_ARG_ID_MOVELEFT (102)
654 #define SWM_ARG_ID_MOVERIGHT (103)
655 #define SWM_ARG_ID_BAR_TOGGLE (110)
656 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
657 #define SWM_ARG_ID_CYCLERG_MOVE_UP (112)
658 #define SWM_ARG_ID_CYCLERG_MOVE_DOWN (113)
664 TAILQ_ENTRY(quirk
) entry
;
665 char *class; /* WM_CLASS:class */
666 char *instance
; /* WM_CLASS:instance */
667 char *name
; /* WM_NAME */
669 regex_t regex_instance
;
672 int ws
; /* Initial workspace. */
673 #define SWM_Q_FLOAT (1<<0) /* Float this window. */
674 #define SWM_Q_TRANSSZ (1<<1) /* Transient window size too small. */
675 #define SWM_Q_ANYWHERE (1<<2) /* Don't position this window */
676 #define SWM_Q_XTERM_FONTADJ (1<<3) /* Adjust xterm fonts when resizing. */
677 #define SWM_Q_FULLSCREEN (1<<4) /* Remove border when fullscreen. */
678 #define SWM_Q_FOCUSPREV (1<<5) /* Focus on caller. */
679 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
680 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
681 #define SWM_Q_OBEYAPPFOCUSREQ (1<<8) /* Focus when applications ask. */
682 #define SWM_Q_IGNOREPID (1<<9) /* Ignore PID when determining ws. */
683 #define SWM_Q_IGNORESPAWNWS (1<<10) /* Ignore _SWM_WS when managing win. */
684 #define SWM_Q_NOFOCUSCYCLE (1<<11) /* Remove from normal focus cycle. */
685 #define SWM_Q_MINIMALBORDER (1<<12) /* No border when floating/unfocused. */
687 TAILQ_HEAD(quirk_list
, quirk
);
688 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
691 * Supported EWMH hints should be added to
692 * both the enum and the ewmh array
698 _NET_CURRENT_DESKTOP
,
699 _NET_DESKTOP_GEOMETRY
,
701 _NET_DESKTOP_VIEWPORT
,
702 _NET_MOVERESIZE_WINDOW
,
703 _NET_NUMBER_OF_DESKTOPS
,
704 _NET_REQUEST_FRAME_EXTENTS
,
706 _NET_WM_ACTION_ABOVE
,
707 _NET_WM_ACTION_CLOSE
,
708 _NET_WM_ACTION_FULLSCREEN
,
710 _NET_WM_ACTION_RESIZE
,
711 _NET_WM_ALLOWED_ACTIONS
,
713 _NET_WM_FULL_PLACEMENT
,
717 _NET_WM_STATE_FULLSCREEN
,
718 _NET_WM_STATE_HIDDEN
,
719 _NET_WM_STATE_MAXIMIZED_VERT
,
720 _NET_WM_STATE_MAXIMIZED_HORZ
,
721 _NET_WM_STATE_SKIP_PAGER
,
722 _NET_WM_STATE_SKIP_TASKBAR
,
724 _NET_WM_WINDOW_TYPE_DESKTOP
,
725 _NET_WM_WINDOW_TYPE_DIALOG
,
726 _NET_WM_WINDOW_TYPE_DOCK
,
727 _NET_WM_WINDOW_TYPE_NORMAL
,
728 _NET_WM_WINDOW_TYPE_SPLASH
,
729 _NET_WM_WINDOW_TYPE_TOOLBAR
,
730 _NET_WM_WINDOW_TYPE_UTILITY
,
731 _SWM_WM_STATE_MANUAL
,
738 } ewmh
[SWM_EWMH_HINT_MAX
] = {
739 /* must be in same order as in the enum */
740 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
741 {"_NET_CLIENT_LIST", XCB_ATOM_NONE
},
742 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
743 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE
},
744 {"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE
},
745 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE
},
746 {"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE
},
747 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
748 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE
},
749 {"_NET_REQUEST_FRAME_EXTENTS", XCB_ATOM_NONE
},
750 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE
},
751 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
752 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
753 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
754 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
755 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
756 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
757 {"_NET_WM_DESKTOP", XCB_ATOM_NONE
},
758 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE
},
759 {"_NET_WM_NAME", XCB_ATOM_NONE
},
760 {"_NET_WM_STATE", XCB_ATOM_NONE
},
761 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
762 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
763 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
764 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
765 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
766 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
767 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
768 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
769 {"_NET_WM_WINDOW_TYPE_DESKTOP", XCB_ATOM_NONE
},
770 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
771 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
772 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
773 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
774 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
775 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
776 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
779 /* EWMH source type */
781 EWMH_SOURCE_TYPE_NONE
= 0,
782 EWMH_SOURCE_TYPE_NORMAL
= 1,
783 EWMH_SOURCE_TYPE_OTHER
= 2,
790 XC_BOTTOM_LEFT_CORNER
,
791 XC_BOTTOM_RIGHT_CORNER
,
799 char *name
; /* Name used by Xcursor .*/
800 uint8_t cf_char
; /* cursorfont index. */
802 } cursors
[XC_MAX
] = {
803 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
804 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
805 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
806 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
807 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
808 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
809 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
812 #define SWM_SPAWN_OPTIONAL 0x1
816 TAILQ_ENTRY(spawn_prog
) entry
;
822 TAILQ_HEAD(spawn_list
, spawn_prog
);
823 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
829 /* User callable function IDs. */
842 FN_FULLSCREEN_TOGGLE
,
956 /* SWM_DEBUG actions MUST be here: */
969 BINDING_F_REPLAY
= 0x1,
973 RB_ENTRY(binding
) entry
;
974 uint16_t mod
; /* Modifier Mask. */
975 enum binding_type type
; /* Key or Button. */
976 uint32_t value
; /* KeySym or Button Index. */
977 enum actionid action
; /* Action Identifier. */
981 RB_HEAD(binding_tree
, binding
);
983 /* function prototypes */
984 void adjust_font(struct ws_win
*);
985 char *argsep(char **);
986 void bar_cleanup(struct swm_region
*);
987 void bar_extra_setup(void);
988 void bar_extra_stop(void);
989 int bar_extra_update(void);
990 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
991 void bar_fmt_expand(char *, size_t);
992 void bar_draw(struct swm_bar
*);
993 void bar_print(struct swm_region
*, const char *);
994 void bar_print_legacy(struct swm_region
*, const char *);
995 void bar_replace(char *, char *, struct swm_region
*, size_t);
996 void bar_replace_pad(char *, int *, size_t);
997 char *bar_replace_seq(char *, char *, struct swm_region
*, size_t *, size_t);
998 void bar_setup(struct swm_region
*);
999 void bar_toggle(struct binding
*, struct swm_region
*, union arg
*);
1000 void bar_urgent(char *, size_t);
1001 void bar_window_class(char *, size_t, struct swm_region
*);
1002 void bar_window_class_instance(char *, size_t, struct swm_region
*);
1003 void bar_window_index_count(char *, size_t, struct swm_region
*);
1004 void bar_window_instance(char *, size_t, struct swm_region
*);
1005 void bar_window_name(char *, size_t, struct swm_region
*);
1006 void bar_window_state(char *, size_t, struct swm_region
*);
1007 void bar_workspace_name(char *, size_t, struct swm_region
*);
1008 void bar_workspace_state(char *, size_t, struct swm_region
*);
1009 int binding_cmp(struct binding
*, struct binding
*);
1010 void binding_insert(uint16_t, enum binding_type
, uint32_t, enum actionid
,
1011 uint32_t, const char *);
1012 struct binding
*binding_lookup(uint16_t, enum binding_type
, uint32_t);
1013 void binding_remove(struct binding
*);
1014 void buttonpress(xcb_button_press_event_t
*);
1015 void buttonrelease(xcb_button_release_event_t
*);
1016 void center_pointer(struct swm_region
*);
1017 void check_conn(void);
1018 void clear_bindings(void);
1019 void clear_keybindings(void);
1020 int clear_maximized(struct workspace
*);
1021 void clear_quirks(void);
1022 void clear_spawns(void);
1023 void clientmessage(xcb_client_message_event_t
*);
1024 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
1025 int conf_load(const char *, int);
1026 void configurenotify(xcb_configure_notify_event_t
*);
1027 void configurerequest(xcb_configure_request_event_t
*);
1028 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
1029 void constrain_window(struct ws_win
*, struct swm_geometry
*, int *);
1030 int count_win(struct workspace
*, bool);
1031 void cursors_cleanup(void);
1032 void cursors_load(void);
1033 void custom_region(const char *);
1034 void cycle_layout(struct binding
*, struct swm_region
*, union arg
*);
1035 void cyclerg(struct binding
*, struct swm_region
*, union arg
*);
1036 void cyclews(struct binding
*, struct swm_region
*, union arg
*);
1038 void debug_refresh(struct ws_win
*);
1040 void debug_toggle(struct binding
*, struct swm_region
*, union arg
*);
1041 void destroynotify(xcb_destroy_notify_event_t
*);
1042 void dumpwins(struct binding
*, struct swm_region
*, union arg
*);
1043 int enable_wm(void);
1044 void enternotify(xcb_enter_notify_event_t
*);
1045 void event_drain(uint8_t);
1046 void event_error(xcb_generic_error_t
*);
1047 void event_handle(xcb_generic_event_t
*);
1048 void ewmh_apply_flags(struct ws_win
*, uint32_t);
1049 void ewmh_autoquirk(struct ws_win
*);
1050 void ewmh_get_desktop_names(void);
1051 void ewmh_get_wm_state(struct ws_win
*);
1052 int ewmh_handle_special_types(xcb_window_t
, struct swm_region
*);
1053 void ewmh_update_actions(struct ws_win
*);
1054 void ewmh_update_client_list(void);
1055 void ewmh_update_current_desktop(void);
1056 void ewmh_update_desktop_names(void);
1057 void ewmh_update_desktops(void);
1058 void ewmh_change_wm_state(struct ws_win
*, xcb_atom_t
, long);
1059 void ewmh_update_wm_state(struct ws_win
*);
1060 char *expand_tilde(const char *);
1061 void expose(xcb_expose_event_t
*);
1062 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
1063 struct swm_bar
*find_bar(xcb_window_t
);
1064 struct ws_win
*find_frame_window(xcb_window_t
);
1065 struct pid_e
*find_pid(pid_t
);
1066 struct swm_region
*find_region(xcb_window_t
);
1067 struct ws_win
*find_unmanaged_window(xcb_window_t
);
1068 struct ws_win
*find_window(xcb_window_t
);
1069 void floating_toggle(struct binding
*, struct swm_region
*, union arg
*);
1070 void focus(struct binding
*, struct swm_region
*, union arg
*);
1071 void focus_flush(void);
1072 void focus_pointer(struct binding
*, struct swm_region
*, union arg
*);
1073 void focus_region(struct swm_region
*);
1074 void focus_win(struct ws_win
*);
1075 void focusin(xcb_focus_in_event_t
*);
1077 void focusout(xcb_focus_out_event_t
*);
1079 void focusrg(struct binding
*, struct swm_region
*, union arg
*);
1080 void fontset_init(void);
1081 void free_window(struct ws_win
*);
1082 void fullscreen_toggle(struct binding
*, struct swm_region
*, union arg
*);
1083 xcb_atom_t
get_atom_from_string(const char *);
1085 char *get_atom_name(xcb_atom_t
);
1087 struct ws_win
*get_focus_magic(struct ws_win
*);
1088 struct ws_win
*get_focus_prev(struct ws_win
*);
1089 xcb_generic_event_t
*get_next_event(bool);
1091 char *get_notify_detail_label(uint8_t);
1092 char *get_notify_mode_label(uint8_t);
1094 struct ws_win
*get_pointer_win(xcb_window_t
);
1095 struct ws_win
*get_region_focus(struct swm_region
*);
1096 int get_region_index(struct swm_region
*);
1097 xcb_screen_t
*get_screen(int);
1098 int get_screen_count(void);
1100 char *get_source_type_label(uint32_t);
1101 char *get_stack_mode_name(uint8_t);
1102 char *get_state_mask_label(uint16_t);
1104 int32_t get_swm_ws(xcb_window_t
);
1105 bool get_urgent(struct ws_win
*);
1107 char *get_win_input_model(struct ws_win
*);
1109 char *get_win_name(xcb_window_t
);
1110 uint8_t get_win_state(xcb_window_t
);
1111 void get_wm_protocols(struct ws_win
*);
1112 int get_ws_idx(struct ws_win
*);
1113 void grab_windows(void);
1114 void grabbuttons(void);
1115 void grabkeys(void);
1116 void iconify(struct binding
*, struct swm_region
*, union arg
*);
1117 void initlayout(struct workspace
*);
1118 bool isxlfd(char *);
1119 bool keybindreleased(struct binding
*, xcb_key_release_event_t
*);
1120 void keypress(xcb_key_press_event_t
*);
1121 void keyrelease(xcb_key_release_event_t
*);
1122 bool keyrepeating(xcb_key_release_event_t
*);
1123 void kill_bar_extra_atexit(void);
1124 void kill_refs(struct ws_win
*);
1126 void leavenotify(xcb_leave_notify_event_t
*);
1128 void load_float_geom(struct ws_win
*);
1129 void lower_window(struct ws_win
*);
1130 struct ws_win
*manage_window(xcb_window_t
, int, bool);
1131 void map_window(struct ws_win
*);
1132 void mapnotify(xcb_map_notify_event_t
*);
1133 void mappingnotify(xcb_mapping_notify_event_t
*);
1134 void maprequest(xcb_map_request_event_t
*);
1135 void maximize_toggle(struct binding
*, struct swm_region
*, union arg
*);
1136 void motionnotify(xcb_motion_notify_event_t
*);
1137 void move(struct binding
*, struct swm_region
*, union arg
*);
1138 void move_win(struct ws_win
*, struct binding
*, int);
1139 uint32_t name_to_pixel(int, const char *);
1140 void name_workspace(struct binding
*, struct swm_region
*, union arg
*);
1141 void new_region(struct swm_screen
*, int, int, int, int);
1142 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1143 int parsebinding(const char *, uint16_t *, enum binding_type
*, uint32_t *,
1145 int parsequirks(const char *, uint32_t *, int *);
1146 void pressbutton(struct binding
*, struct swm_region
*, union arg
*);
1147 void priorws(struct binding
*, struct swm_region
*, union arg
*);
1149 void print_win_geom(xcb_window_t
);
1151 void propertynotify(xcb_property_notify_event_t
*);
1152 void put_back_event(xcb_generic_event_t
*);
1153 void quirk_free(struct quirk
*);
1154 void quirk_insert(const char *, const char *, const char *, uint32_t, int);
1155 void quirk_remove(struct quirk
*);
1156 void quirk_replace(struct quirk
*, const char *, const char *, const char *,
1158 void quit(struct binding
*, struct swm_region
*, union arg
*);
1159 void raise_focus(struct binding
*, struct swm_region
*, union arg
*);
1160 void raise_toggle(struct binding
*, struct swm_region
*, union arg
*);
1161 void raise_window(struct ws_win
*);
1162 void region_containment(struct ws_win
*, struct swm_region
*, int);
1163 struct swm_region
*region_under(struct swm_screen
*, int, int);
1164 void regionize(struct ws_win
*, int, int);
1165 void reparent_window(struct ws_win
*);
1166 void reparentnotify(xcb_reparent_notify_event_t
*);
1167 void resize(struct binding
*, struct swm_region
*, union arg
*);
1168 void resize_win(struct ws_win
*, struct binding
*, int);
1169 void restart(struct binding
*, struct swm_region
*, union arg
*);
1170 struct swm_region
*root_to_region(xcb_window_t
, int);
1171 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1172 void scan_randr(int);
1173 void search_do_resp(void);
1174 void search_resp_name_workspace(const char *, size_t);
1175 void search_resp_search_window(const char *);
1176 void search_resp_search_workspace(const char *);
1177 void search_resp_uniconify(const char *, size_t);
1178 void search_win(struct binding
*, struct swm_region
*, union arg
*);
1179 void search_win_cleanup(void);
1180 void search_workspace(struct binding
*, struct swm_region
*, union arg
*);
1181 void send_to_rg(struct binding
*, struct swm_region
*, union arg
*);
1182 void send_to_rg_relative(struct binding
*, struct swm_region
*, union arg
*);
1183 void send_to_ws(struct binding
*, struct swm_region
*, union arg
*);
1184 void set_region(struct swm_region
*);
1185 int setautorun(const char *, const char *, int);
1186 void setbinding(uint16_t, enum binding_type
, uint32_t, enum actionid
,
1187 uint32_t, const char *);
1188 int setconfbinding(const char *, const char *, int);
1189 int setconfcolor(const char *, const char *, int);
1190 int setconfmodkey(const char *, const char *, int);
1191 int setconfquirk(const char *, const char *, int);
1192 int setconfregion(const char *, const char *, int);
1193 int setconfspawn(const char *, const char *, int);
1194 int setconfvalue(const char *, const char *, int);
1195 int setkeymapping(const char *, const char *, int);
1196 int setlayout(const char *, const char *, int);
1197 void setquirk(const char *, const char *, const char *, uint32_t, int);
1198 void setscreencolor(const char *, int, int);
1199 void setspawn(const char *, const char *, int);
1200 void setup_btnbindings(void);
1201 void setup_ewmh(void);
1202 void setup_globals(void);
1203 void setup_keybindings(void);
1204 void setup_quirks(void);
1205 void setup_screens(void);
1206 void setup_spawn(void);
1207 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1208 void set_win_state(struct ws_win
*, uint8_t);
1209 void shutdown_cleanup(void);
1211 void socket_setnonblock(int);
1212 void sort_windows(struct ws_win_list
*);
1213 void spawn(int, union arg
*, bool);
1214 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1215 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1216 void spawn_insert(const char *, const char *, int);
1217 struct spawn_prog
*spawn_find(const char *);
1218 void spawn_remove(struct spawn_prog
*);
1219 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1220 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1221 void stack_config(struct binding
*, struct swm_region
*, union arg
*);
1222 void stack_master(struct workspace
*, struct swm_geometry
*, int, bool);
1223 void store_float_geom(struct ws_win
*);
1224 char *strdupsafe(const char *);
1225 void swapwin(struct binding
*, struct swm_region
*, union arg
*);
1226 void switchws(struct binding
*, struct swm_region
*, union arg
*);
1227 void teardown_ewmh(void);
1228 void unescape_selector(char *);
1229 void unfocus_win(struct ws_win
*);
1230 void uniconify(struct binding
*, struct swm_region
*, union arg
*);
1231 void unmanage_window(struct ws_win
*);
1232 void unmap_all(void);
1233 void unmap_window(struct ws_win
*);
1234 void unmapnotify(xcb_unmap_notify_event_t
*);
1235 void unparent_window(struct ws_win
*);
1236 void update_floater(struct ws_win
*);
1237 void update_modkey(uint16_t);
1238 void update_win_stacking(struct ws_win
*);
1239 void update_window(struct ws_win
*);
1240 void draw_frame(struct ws_win
*);
1241 void update_wm_state(struct ws_win
*win
);
1242 void updatenumlockmask(void);
1243 void validate_spawns(void);
1244 int validate_win(struct ws_win
*);
1245 int validate_ws(struct workspace
*);
1246 void version(struct binding
*, struct swm_region
*, union arg
*);
1247 void win_to_ws(struct ws_win
*, int, bool);
1248 pid_t
window_get_pid(xcb_window_t
);
1249 void wkill(struct binding
*, struct swm_region
*, union arg
*);
1250 void update_ws_stack(struct workspace
*);
1251 void xft_init(struct swm_region
*);
1252 void _add_startup_exception(const char *, va_list);
1253 void add_startup_exception(const char *, ...);
1255 RB_PROTOTYPE(binding_tree
, binding
, entry
, binding_cmp
);
1256 RB_GENERATE(binding_tree
, binding
, entry
, binding_cmp
);
1257 struct binding_tree bindings
;
1262 xcb_font_t cf
= XCB_NONE
;
1265 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1266 /* try to load Xcursor first. */
1267 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1270 /* fallback to cursorfont. */
1271 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1272 if (cf
== XCB_NONE
) {
1273 cf
= xcb_generate_id(conn
);
1274 xcb_open_font(conn
, cf
, strlen("cursor"),
1278 cursors
[i
].cid
= xcb_generate_id(conn
);
1279 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1280 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1281 0xffff, 0xffff, 0xffff);
1287 xcb_close_font(conn
, cf
);
1291 cursors_cleanup(void)
1294 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1295 xcb_free_cursor(conn
, cursors
[i
].cid
);
1299 expand_tilde(const char *s
)
1301 struct passwd
*ppwd
;
1309 errx(1, "expand_tilde: NULL string.");
1312 result
= strdup(sc
);
1318 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1319 errx(1, "expand_tilde: sysconf");
1321 if ((user
= calloc(1, max
+ 1)) == NULL
)
1322 errx(1, "expand_tilde: calloc");
1324 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1329 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1333 result
= strdup(sc
);
1335 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1339 errx(1, "expand_tilde: failed to allocate memory.");
1345 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1347 unsigned int tmpr
, tmpg
, tmpb
;
1349 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1352 *rr
= RGB_8_TO_16(tmpr
);
1353 *gg
= RGB_8_TO_16(tmpg
);
1354 *bb
= RGB_8_TO_16(tmpb
);
1360 get_screen(int screen
)
1362 const xcb_setup_t
*r
;
1363 xcb_screen_iterator_t iter
;
1365 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1366 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1370 iter
= xcb_setup_roots_iterator(r
);
1371 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1379 get_screen_count(void)
1381 const xcb_setup_t
*r
;
1383 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1384 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1388 return xcb_setup_roots_length(r
);
1392 get_region_index(struct swm_region
*r
)
1394 struct swm_region
*rr
;
1400 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1415 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1416 event_drain(XCB_ENTER_NOTIFY
);
1422 get_atom_from_string(const char *str
)
1424 xcb_intern_atom_cookie_t c
;
1425 xcb_intern_atom_reply_t
*r
;
1428 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1429 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1437 return (XCB_ATOM_NONE
);
1441 get_wm_protocols(struct ws_win
*win
) {
1443 xcb_icccm_get_wm_protocols_reply_t wpr
;
1445 if (xcb_icccm_get_wm_protocols_reply(conn
,
1446 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
1448 for (i
= 0; i
< (int)wpr
.atoms_len
; i
++) {
1449 if (wpr
.atoms
[i
] == a_takefocus
)
1450 win
->take_focus
= true;
1451 if (wpr
.atoms
[i
] == a_delete
)
1452 win
->can_delete
= true;
1454 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
1461 xcb_window_t root
, win
;
1462 int i
, j
, num_screens
;
1464 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1465 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1467 num_screens
= get_screen_count();
1468 for (i
= 0; i
< num_screens
; i
++) {
1469 root
= screens
[i
].root
;
1471 /* Set up _NET_SUPPORTING_WM_CHECK. */
1472 win
= xcb_generate_id(conn
);
1473 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
1474 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1475 XCB_COPY_FROM_PARENT
, 0, NULL
);
1477 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
1478 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1479 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1480 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1482 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1483 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1484 8, strlen("spectrwm"), "spectrwm");
1486 /* Report supported atoms */
1487 xcb_delete_property(conn
, root
, a_net_supported
);
1488 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1489 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, root
,
1490 a_net_supported
, XCB_ATOM_ATOM
, 32, 1,
1495 ewmh_update_desktops();
1496 ewmh_get_desktop_names();
1504 xcb_get_property_cookie_t pc
;
1505 xcb_get_property_reply_t
*pr
;
1507 num_screens
= get_screen_count();
1509 for (i
= 0; i
< num_screens
; i
++) {
1510 /* Get the support check window and destroy it */
1511 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, a_net_wm_check
,
1512 XCB_ATOM_WINDOW
, 0, 1);
1513 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1516 if (pr
->format
== a_net_wm_check
) {
1517 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1519 xcb_destroy_window(conn
, id
);
1520 xcb_delete_property(conn
, screens
[i
].root
,
1522 xcb_delete_property(conn
, screens
[i
].root
,
1530 ewmh_handle_special_types(xcb_window_t id
, struct swm_region
*region
)
1532 xcb_get_property_reply_t
*r
;
1533 xcb_get_property_cookie_t c
;
1536 uint16_t configure_mask
= 0;
1539 c
= xcb_get_property(conn
, 0, id
,
1540 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1541 r
= xcb_get_property_reply(conn
, c
, NULL
);
1545 type
= xcb_get_property_value(r
);
1546 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1548 for (i
= 0; i
< n
; i
++) {
1549 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DESKTOP
].atom
) {
1550 configure_mask
= XCB_CONFIG_WINDOW_STACK_MODE
|
1551 XCB_CONFIG_WINDOW_SIBLING
;
1553 wa
[1] = XCB_STACK_MODE_ABOVE
;
1557 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
) {
1558 configure_mask
= XCB_CONFIG_WINDOW_STACK_MODE
;
1559 wa
[0] = XCB_STACK_MODE_ABOVE
;
1565 if (configure_mask
!= 0) {
1566 xcb_map_window(conn
, id
);
1567 xcb_configure_window (conn
, id
, configure_mask
, wa
);
1575 ewmh_autoquirk(struct ws_win
*win
)
1577 xcb_get_property_reply_t
*r
;
1578 xcb_get_property_cookie_t c
;
1582 c
= xcb_get_property(conn
, 0, win
->id
,
1583 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1584 r
= xcb_get_property_reply(conn
, c
, NULL
);
1588 type
= xcb_get_property_value(r
);
1589 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1591 for (i
= 0; i
< n
; i
++) {
1592 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1594 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1595 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1596 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1599 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1600 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1601 win
->quirks
= SWM_Q_FLOAT
;
1608 c
= xcb_get_property(conn
, 0, win
->id
,
1609 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1610 r
= xcb_get_property_reply(conn
, c
, NULL
);
1614 type
= xcb_get_property_value(r
);
1615 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1617 for (i
= 0; i
< n
; i
++) {
1618 if (type
[i
] == ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
||
1619 type
[i
] == ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
) {
1620 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1628 ewmh_update_actions(struct ws_win
*win
)
1630 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1636 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1639 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1640 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1641 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1644 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1645 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1648 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1649 #define _NET_WM_STATE_ADD 1 /* add/set property */
1650 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1653 ewmh_change_wm_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1660 name
= get_atom_name(state
);
1661 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: win %#x, state: %s, "
1662 "action: %ld\n", WINID(win
), name
, action
);
1668 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1669 flag
= EWMH_F_FULLSCREEN
;
1670 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1671 flag
= EWMH_F_ABOVE
;
1672 else if (state
== ewmh
[_NET_WM_STATE_HIDDEN
].atom
)
1673 flag
= EWMH_F_HIDDEN
;
1674 else if (state
== ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
||
1675 state
== ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
)
1676 flag
= EWMH_F_MAXIMIZED
;
1677 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1678 flag
= SWM_F_MANUAL
;
1679 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1680 flag
= EWMH_F_SKIP_PAGER
;
1681 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1682 flag
= EWMH_F_SKIP_TASKBAR
;
1684 /* Disallow unfloating transients. */
1685 if (TRANS(win
) && flag
== EWMH_F_ABOVE
)
1688 new_flags
= win
->ewmh_flags
;
1691 case _NET_WM_STATE_REMOVE
:
1694 case _NET_WM_STATE_ADD
:
1697 case _NET_WM_STATE_TOGGLE
:
1702 ewmh_apply_flags(win
, new_flags
);
1705 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: done.\n");
1709 ewmh_apply_flags(struct ws_win
*win
, uint32_t pending
)
1711 struct workspace
*ws
;
1714 changed
= win
->ewmh_flags
^ pending
;
1718 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: pending: %d\n", pending
);
1720 win
->ewmh_flags
= pending
;
1723 if (changed
& EWMH_F_HIDDEN
) {
1725 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1726 ws
->focus_pending
= get_focus_prev(win
);
1731 /* Reload floating geometry in case region changed. */
1733 load_float_geom(win
);
1735 /* The window is no longer iconic, prepare focus. */
1736 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1737 ws
->focus_pending
= get_focus_magic(win
);
1742 if (changed
& EWMH_F_ABOVE
) {
1743 if (ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1745 load_float_geom(win
);
1746 else if (!MAXIMIZED(win
))
1747 store_float_geom(win
);
1749 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1750 changed
&= ~EWMH_F_MAXIMIZED
;
1754 win
->ewmh_flags
^= EWMH_F_ABOVE
& pending
;
1758 if (changed
& EWMH_F_MAXIMIZED
) {
1759 /* VERT and/or HORZ changed. */
1761 if (!MAXIMIZED(win
))
1762 load_float_geom(win
);
1764 store_float_geom(win
);
1767 if (MAXIMIZED(win
)) {
1768 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
1769 ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1773 ws
->focus_pending
= win
;
1781 if (changed
& EWMH_F_FULLSCREEN
) {
1782 if (FULLSCREEN(win
)) {
1783 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
1787 ws
->focus_pending
= win
;
1790 load_float_geom(win
);
1793 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1797 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: done.\n");
1801 ewmh_update_wm_state(struct ws_win
*win
) {
1802 xcb_atom_t vals
[SWM_EWMH_ACTION_COUNT_MAX
];
1806 vals
[n
++] = ewmh
[_NET_WM_STATE_HIDDEN
].atom
;
1807 if (FULLSCREEN(win
))
1808 vals
[n
++] = ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
;
1809 if (MAXIMIZED_VERT(win
))
1810 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
;
1811 if (MAXIMIZED_HORZ(win
))
1812 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
;
1813 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1814 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
;
1815 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1816 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
;
1817 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1818 vals
[n
++] = ewmh
[_NET_WM_STATE_ABOVE
].atom
;
1819 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1820 vals
[n
++] = ewmh
[_SWM_WM_STATE_MANUAL
].atom
;
1823 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1824 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, n
, vals
);
1826 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1830 ewmh_get_wm_state(struct ws_win
*win
)
1833 xcb_get_property_cookie_t c
;
1834 xcb_get_property_reply_t
*r
;
1840 win
->ewmh_flags
= 0;
1842 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1843 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1844 r
= xcb_get_property_reply(conn
, c
, NULL
);
1848 states
= xcb_get_property_value(r
);
1849 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1851 for (i
= 0; i
< n
; i
++)
1852 ewmh_change_wm_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1860 dumpwins(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
1864 xcb_get_window_attributes_cookie_t c
;
1865 xcb_get_window_attributes_reply_t
*wa
;
1867 /* suppress unused warning since var is needed */
1871 if (r
->ws
== NULL
) {
1872 DPRINTF("dumpwins: invalid workspace\n");
1876 DPRINTF("=== managed window list ws %02d ===\n", r
->ws
->idx
);
1877 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
) {
1878 state
= get_win_state(w
->id
);
1879 c
= xcb_get_window_attributes(conn
, w
->id
);
1880 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1882 DPRINTF("win %#x (%#x), map_state: %d, state: %u, "
1883 "transient: %#x\n", w
->frame
, w
->id
, wa
->map_state
,
1884 state
, w
->transient
);
1887 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1891 DPRINTF("=== stacking order (top down) === \n");
1892 TAILQ_FOREACH(w
, &r
->ws
->stack
, stack_entry
) {
1893 DPRINTF("win %#x (%#x), fs: %s, maximized: %s, above: %s, "
1894 "iconic: %s\n", w
->frame
, w
->id
, YESNO(FULLSCREEN(w
)),
1895 YESNO(MAXIMIZED(w
)), YESNO(ABOVE(w
)), YESNO(ICONIC(w
)));
1898 DPRINTF("===== unmanaged window list =====\n");
1899 TAILQ_FOREACH(w
, &r
->ws
->unmanagedlist
, entry
) {
1900 state
= get_win_state(w
->id
);
1901 c
= xcb_get_window_attributes(conn
, w
->id
);
1902 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1904 DPRINTF("win %#x, map_state: %d, state: %u, "
1905 "transient: %#x\n", w
->id
, wa
->map_state
,
1906 state
, w
->transient
);
1909 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1913 DPRINTF("=================================\n");
1917 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
1920 int num_screens
, i
, j
;
1922 /* Suppress warnings. */
1927 DNPRINTF(SWM_D_MISC
, "debug_toggle\n");
1929 debug_enabled
= !debug_enabled
;
1931 num_screens
= get_screen_count();
1932 for (i
= 0; i
< num_screens
; i
++)
1933 for (j
= 0; j
< workspace_limit
; j
++)
1934 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1941 debug_refresh(struct ws_win
*win
)
1948 XRectangle l_ibox
, l_lbox
;
1949 xcb_rectangle_t rect
;
1951 uint32_t wc
[4], mask
, width
, height
, gcv
[1];
1954 xcb_screen_t
*screen
;
1956 if (debug_enabled
) {
1957 /* Create debug window if it doesn't exist. */
1958 if (win
->debug
== XCB_WINDOW_NONE
) {
1959 if ((screen
= get_screen(win
->s
->idx
)) == NULL
)
1960 errx(1, "ERROR: can't get screen %d.",
1963 win
->debug
= xcb_generate_id(conn
);
1964 wc
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1965 wc
[1] = win
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
1966 wc
[2] = screen
->default_colormap
;
1968 xcb_create_window(conn
, screen
->root_depth
, win
->debug
,
1969 win
->frame
, 0, 0, 10, 10, 1,
1970 XCB_WINDOW_CLASS_INPUT_OUTPUT
, screen
->root_visual
,
1971 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
|
1972 XCB_CW_COLORMAP
, wc
);
1974 xcb_map_window(conn
, win
->debug
);
1977 /* Determine workspace window list index. */
1979 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
1985 /* Determine stacking index (top down). */
1987 TAILQ_FOREACH(w
, &win
->ws
->stack
, stack_entry
) {
1993 if (asprintf(&s
, "%#x f:%#x wl:%d s:%d im:%s", win
->id
,
1994 win
->frame
, widx
, sidx
, get_win_input_model(win
)) == -1)
1999 /* Update window to an appropriate dimension. */
2000 if (bar_font_legacy
) {
2001 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
2002 width
= l_lbox
.width
+ 4;
2003 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
2005 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
2007 width
= info
.width
+ 4;
2008 height
= bar_font
->height
+ 4;
2011 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
2012 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
;
2014 wc
[0] = wc
[1] = border_width
;
2021 xcb_configure_window(conn
, win
->debug
, mask
, wc
);
2023 /* Draw a filled rectangle to 'clear' window. */
2027 rect
.height
= height
;
2029 gcv
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2030 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2031 xcb_poly_fill_rectangle(conn
, win
->debug
, win
->s
->bar_gc
, 1,
2035 if (bar_font_legacy
) {
2036 l_gcv
.graphics_exposures
= 0;
2037 l_draw
= XCreateGC(display
, win
->debug
, 0, &l_gcv
);
2039 XSetForeground(display
, l_draw
,
2040 win
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2042 DRAWSTRING(display
, win
->debug
, bar_fs
, l_draw
, 2,
2043 (bar_fs_extents
->max_logical_extent
.height
-
2044 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
2046 XFreeGC(display
, l_draw
);
2048 draw
= XftDrawCreate(display
, win
->debug
,
2049 DefaultVisual(display
, win
->s
->idx
),
2050 DefaultColormap(display
, win
->s
->idx
));
2052 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
2053 (bar_height
+ bar_font
->height
) / 2 -
2054 bar_font
->descent
, (FcChar8
*)s
, len
);
2056 XftDrawDestroy(draw
);
2060 } else if (win
->debug
!= XCB_WINDOW_NONE
) {
2061 xcb_destroy_window(conn
, win
->debug
);
2062 win
->debug
= XCB_WINDOW_NONE
;
2067 dumpwins(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
2075 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
2081 #endif /* SWM_DEBUG */
2086 int saved_errno
, status
;
2089 saved_errno
= errno
;
2093 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
2098 if (errno
!= ECHILD
)
2099 warn("sighdlr: waitpid");
2100 #endif /* SWM_DEBUG */
2103 if (pid
== searchpid
)
2107 if (WIFEXITED(status
)) {
2108 if (WEXITSTATUS(status
) != 0)
2109 warnx("sighdlr: child exit status: %d",
2110 WEXITSTATUS(status
));
2112 warnx("sighdlr: child is terminated "
2114 #endif /* SWM_DEBUG */
2128 errno
= saved_errno
;
2134 struct pid_e
*p
= NULL
;
2136 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
2141 TAILQ_FOREACH(p
, &pidlist
, entry
) {
2150 name_to_pixel(int sidx
, const char *colorname
)
2152 uint32_t result
= 0;
2153 char cname
[32] = "#";
2154 xcb_screen_t
*screen
;
2155 xcb_colormap_t cmap
;
2156 xcb_alloc_color_reply_t
*cr
;
2157 xcb_alloc_named_color_reply_t
*nr
;
2158 uint16_t rr
, gg
, bb
;
2160 screen
= get_screen(sidx
);
2161 cmap
= screen
->default_colormap
;
2163 /* color is in format rgb://rr/gg/bb */
2164 if (strncmp(colorname
, "rgb:", 4) == 0) {
2165 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
2166 warnx("could not parse rgb %s", colorname
);
2168 cr
= xcb_alloc_color_reply(conn
,
2169 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
2175 warnx("color '%s' not found", colorname
);
2178 nr
= xcb_alloc_named_color_reply(conn
,
2179 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
2182 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
2183 nr
= xcb_alloc_named_color_reply(conn
,
2184 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
2191 warnx("color '%s' not found", colorname
);
2198 setscreencolor(const char *val
, int i
, int c
)
2200 if (i
< 0 || i
>= get_screen_count())
2203 screens
[i
].c
[c
].pixel
= name_to_pixel(i
, val
);
2204 free(screens
[i
].c
[c
].name
);
2205 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
2210 fancy_stacker(struct workspace
*ws
)
2212 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2213 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2214 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2215 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
2216 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
2217 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2218 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2219 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
2220 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
2224 plain_stacker(struct workspace
*ws
)
2226 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2227 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2228 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
2229 sizeof ws
->stacker
);
2230 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2231 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
2232 sizeof ws
->stacker
);
2236 custom_region(const char *val
)
2238 unsigned int x
, y
, w
, h
;
2239 int sidx
, num_screens
;
2240 xcb_screen_t
*screen
;
2242 DNPRINTF(SWM_D_CONF
, "custom_region: %s\n", val
);
2244 num_screens
= get_screen_count();
2245 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
2246 errx(1, "invalid custom region, "
2247 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
2248 if (sidx
< 1 || sidx
> num_screens
)
2249 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
2253 if ((screen
= get_screen(sidx
)) == NULL
)
2254 errx(1, "ERROR: can't get screen %d.", sidx
);
2257 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
2259 if (x
> screen
->width_in_pixels
||
2260 y
> screen
->height_in_pixels
||
2261 w
+ x
> screen
->width_in_pixels
||
2262 h
+ y
> screen
->height_in_pixels
) {
2263 warnx("ignoring region %ux%u+%u+%u - not within screen "
2264 "boundaries (%ux%u)", w
, h
, x
, y
,
2265 screen
->width_in_pixels
, screen
->height_in_pixels
);
2269 new_region(&screens
[sidx
], x
, y
, w
, h
);
2273 socket_setnonblock(int fd
)
2277 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
2278 err(1, "fcntl F_GETFL");
2279 flags
|= O_NONBLOCK
;
2280 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
2281 err(1, "fcntl F_SETFL");
2285 bar_print_legacy(struct swm_region
*r
, const char *s
)
2287 xcb_rectangle_t rect
;
2292 XRectangle ibox
, lbox
;
2296 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
2298 switch (bar_justify
) {
2299 case SWM_BAR_JUSTIFY_LEFT
:
2302 case SWM_BAR_JUSTIFY_CENTER
:
2303 x
= (WIDTH(r
) - lbox
.width
) / 2;
2305 case SWM_BAR_JUSTIFY_RIGHT
:
2306 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
2310 if (x
< SWM_BAR_OFFSET
)
2315 rect
.width
= WIDTH(r
->bar
);
2316 rect
.height
= HEIGHT(r
->bar
);
2318 /* clear back buffer */
2319 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2320 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2321 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2323 /* draw back buffer */
2324 gcvd
.graphics_exposures
= 0;
2325 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
2326 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2327 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
2328 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
2330 XFreeGC(display
, draw
);
2333 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2334 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2338 bar_print(struct swm_region
*r
, const char *s
)
2341 xcb_rectangle_t rect
;
2349 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
2351 switch (bar_justify
) {
2352 case SWM_BAR_JUSTIFY_LEFT
:
2355 case SWM_BAR_JUSTIFY_CENTER
:
2356 x
= (WIDTH(r
) - info
.width
) / 2;
2358 case SWM_BAR_JUSTIFY_RIGHT
:
2359 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
2363 if (x
< SWM_BAR_OFFSET
)
2368 rect
.width
= WIDTH(r
->bar
);
2369 rect
.height
= HEIGHT(r
->bar
);
2371 /* clear back buffer */
2372 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2373 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2374 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2376 /* draw back buffer */
2377 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
2378 DefaultVisual(display
, r
->s
->idx
),
2379 DefaultColormap(display
, r
->s
->idx
));
2381 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
2382 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
2385 XftDrawDestroy(draw
);
2388 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2389 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2393 bar_extra_stop(void)
2397 bzero(bar_pipe
, sizeof bar_pipe
);
2400 kill(bar_pid
, SIGTERM
);
2403 strlcpy(bar_ext
, "", sizeof bar_ext
);
2408 bar_window_class(char *s
, size_t sz
, struct swm_region
*r
)
2410 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2412 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
2413 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
2417 bar_window_instance(char *s
, size_t sz
, struct swm_region
*r
)
2419 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2421 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
2422 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
2426 bar_window_class_instance(char *s
, size_t sz
, struct swm_region
*r
)
2428 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2431 bar_window_class(s
, sz
, r
);
2432 strlcat(s
, ":", sz
);
2433 bar_window_instance(s
, sz
, r
);
2437 bar_window_index_count(char *s
, size_t sz
, struct swm_region
*r
)
2442 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
) {
2443 strlcat(s
, "0/0", sz
);
2450 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
) {
2452 if (w
->id
== r
->ws
->focus
->id
) {
2457 snprintf(s
, sz
, "%d/%d", index
, count
);
2461 bar_window_state(char *s
, size_t sz
, struct swm_region
*r
)
2463 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2465 if (MAXIMIZED(r
->ws
->focus
))
2466 strlcat(s
, "(m)", sz
);
2467 else if (ABOVE(r
->ws
->focus
))
2468 strlcat(s
, "(f)", sz
);
2472 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
2476 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2479 title
= get_win_name(r
->ws
->focus
->id
);
2480 strlcat(s
, title
, sz
);
2485 get_urgent(struct ws_win
*win
)
2487 xcb_icccm_wm_hints_t hints
;
2488 xcb_get_property_cookie_t c
;
2489 bool urgent
= false;
2492 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2493 if (xcb_icccm_get_wm_hints_reply(conn
, c
, &hints
, NULL
))
2494 urgent
= xcb_icccm_wm_hints_get_urgency(&hints
);
2501 bar_urgent(char *s
, size_t sz
)
2504 int i
, j
, num_screens
;
2505 bool urgent
[SWM_WS_MAX
];
2508 for (i
= 0; i
< workspace_limit
; i
++)
2511 num_screens
= get_screen_count();
2512 for (i
= 0; i
< num_screens
; i
++)
2513 for (j
= 0; j
< workspace_limit
; j
++)
2514 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2515 if (get_urgent(win
))
2518 for (i
= 0; i
< workspace_limit
; i
++) {
2520 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2522 } else if (!urgent_collapse
) {
2523 strlcat(s
, "- ", sz
);
2526 if (urgent_collapse
&& s
[0])
2527 s
[strlen(s
) - 1] = 0;
2531 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2533 if (r
== NULL
|| r
->ws
== NULL
)
2535 if (r
->ws
->name
!= NULL
)
2536 strlcat(s
, r
->ws
->name
, sz
);
2540 bar_workspace_state(char *s
, size_t sz
, struct swm_region
*r
)
2543 int i
, j
, num_screens
;
2544 bool used_workspaces
[SWM_WS_MAX
];
2549 if (r
== NULL
|| r
->ws
== NULL
)
2552 for (i
= 0; i
< SWM_WS_MAX
; ++i
)
2553 used_workspaces
[i
] = false;
2555 num_screens
= get_screen_count();
2556 for (i
= 0; i
< num_screens
; i
++)
2557 for (j
= 0; j
< workspace_limit
; j
++)
2558 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2559 ++used_workspaces
[win
->ws
->idx
];
2561 for (i
= 0; i
< SWM_WS_MAX
; ++i
) {
2563 if (i
== r
->ws
->idx
) {
2565 } else if (used_workspaces
[i
]) {
2571 snprintf(tmp
, sizeof tmp
, fmt
, i
+ 1);
2572 strlcat(s
, tmp
, sz
);
2577 /* build the default bar format according to the defined enabled options */
2579 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2583 /* if format provided, just copy the buffers */
2584 if (bar_format
!= NULL
) {
2585 strlcpy(fmtnew
, fmtexp
, sz
);
2589 /* reset the output buffer */
2592 strlcat(fmtnew
, "+N:+I ", sz
);
2594 strlcat(fmtnew
, "+S", sz
);
2595 strlcat(fmtnew
, " ", sz
);
2597 /* only show the workspace name if there's actually one */
2598 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2599 strlcat(fmtnew
, "<+D>", sz
);
2601 /* If enabled, only show the iconic count if there are iconic wins. */
2602 if (iconic_enabled
&& r
!= NULL
&& r
->ws
!= NULL
)
2603 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2605 strlcat(fmtnew
, "{+M}", sz
);
2609 strlcat(fmtnew
, "+3<", sz
);
2611 if (clock_enabled
) {
2612 strlcat(fmtnew
, fmtexp
, sz
);
2613 strlcat(fmtnew
, "+4<", sz
);
2616 /* bar_urgent already adds the space before the last asterisk */
2618 strlcat(fmtnew
, (urgent_collapse
? "*+U*+4<" : "* +U*+4<"), sz
);
2620 if (window_class_enabled
) {
2621 strlcat(fmtnew
, "+C", sz
);
2622 if (!window_instance_enabled
)
2623 strlcat(fmtnew
, "+4<", sz
);
2626 /* checks needed by the colon and floating strlcat(3) calls below */
2627 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2628 if (window_instance_enabled
) {
2629 if (window_class_enabled
)
2630 strlcat(fmtnew
, ":", sz
);
2631 strlcat(fmtnew
, "+T+4<", sz
);
2633 if (window_name_enabled
) {
2634 if (ABOVE(r
->ws
->focus
) || MAXIMIZED(r
->ws
->focus
))
2635 strlcat(fmtnew
, "+F ", sz
);
2636 strlcat(fmtnew
, "+64W ", sz
);
2640 /* finally add the action script output and the version */
2641 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2645 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2647 /* special case; no limit given, pad one space, instead */
2648 if (*limit
== (int)sz
- 1)
2650 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2653 /* replaces the bar format character sequences (like in tmux(1)) */
2655 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2660 char tmp
[SWM_BAR_MAX
];
2661 int limit
, size
, count
;
2664 /* reset strlcat(3) buffer */
2667 /* get number, if any */
2670 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2671 limit
= sizeof tmp
- 1;
2672 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2673 limit
= sizeof tmp
- 1;
2675 /* there is nothing to replace (ie EOL) */
2682 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2685 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2688 bar_window_class(tmp
, sizeof tmp
, r
);
2691 bar_workspace_name(tmp
, sizeof tmp
, r
);
2694 bar_window_state(tmp
, sizeof tmp
, r
);
2697 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2700 bar_workspace_state(tmp
, sizeof tmp
, r
);
2704 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2708 snprintf(tmp
, sizeof tmp
, "%d", count
);
2711 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2714 bar_window_index_count(tmp
, sizeof tmp
, r
);
2717 bar_window_class_instance(tmp
, sizeof tmp
, r
);
2720 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2723 bar_window_instance(tmp
, sizeof tmp
, r
);
2726 bar_urgent(tmp
, sizeof tmp
);
2729 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2732 bar_window_name(tmp
, sizeof tmp
, r
);
2735 /* unknown character sequence; copy as-is */
2736 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2742 if ((int)len
< limit
)
2744 while (limit
-- > 0) {
2745 if (*offrep
>= sz
- 1)
2747 fmtrep
[(*offrep
)++] = *ptr
++;
2755 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2760 while (*fmt
!= '\0') {
2762 /* skip ordinary characters */
2765 fmtrep
[off
++] = *fmt
++;
2769 /* character sequence found; replace it */
2770 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2779 bar_fmt_expand(char *fmtexp
, size_t sz
)
2786 /* start by grabbing the current time and date */
2788 localtime_r(&tmt
, &tm
);
2790 /* figure out what to expand */
2791 if (bar_format
!= NULL
)
2793 else if (bar_format
== NULL
&& clock_enabled
)
2795 /* if nothing to expand bail out */
2801 /* copy as-is, just in case the format shouldn't be expanded below */
2802 strlcpy(fmtexp
, fmt
, sz
);
2803 /* finally pass the string through strftime(3) */
2804 #ifndef SWM_DENY_CLOCK_FORMAT
2805 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2806 warnx("format too long");
2811 /* Redraws a region bar; need to follow with xcb_flush() or focus_flush(). */
2813 bar_draw(struct swm_bar
*bar
)
2815 struct swm_region
*r
;
2816 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2817 char fmtrep
[SWM_BAR_MAX
];
2819 /* expand the format by first passing it through strftime(3) */
2820 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2827 if (bar_enabled
&& r
->ws
->bar_enabled
)
2828 xcb_map_window(conn
, bar
->id
);
2830 xcb_unmap_window(conn
, bar
->id
);
2834 if (startup_exception
)
2835 snprintf(fmtrep
, sizeof fmtrep
, "total "
2836 "exceptions: %d, first exception: %s",
2840 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2841 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2844 if (bar_font_legacy
)
2845 bar_print_legacy(r
, fmtrep
);
2847 bar_print(r
, fmtrep
);
2851 * Reads external script output; call when stdin is readable.
2852 * Returns 1 if bar_ext was updated; otherwise 0.
2855 bar_extra_update(void)
2858 char b
[SWM_BAR_MAX
];
2859 bool changed
= false;
2864 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2867 if (b
[len
- 1] == '\n') {
2868 /* Remove newline. */
2871 /* "Clear" bar_ext. */
2874 /* Flush buffered output. */
2875 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2876 bar_ext_buf
[0] = '\0';
2878 /* Append new output to bar. */
2879 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2883 /* Buffer output. */
2884 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2889 if (errno
!= EAGAIN
) {
2890 warn("bar_action failed");
2899 bar_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
2901 struct swm_region
*tmpr
;
2904 /* suppress unused warnings since vars are needed */
2909 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2912 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2913 /* Only change if master switch is enabled. */
2915 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2917 bar_enabled
= r
->ws
->bar_enabled
= true;
2919 case SWM_ARG_ID_BAR_TOGGLE
:
2920 bar_enabled
= !bar_enabled
;
2924 /* update bars as necessary */
2925 num_screens
= get_screen_count();
2926 for (i
= 0; i
< num_screens
; i
++)
2927 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2929 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2930 xcb_map_window(conn
, tmpr
->bar
->id
);
2932 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2935 /* Restack all regions and redraw bar. */
2936 num_screens
= get_screen_count();
2937 for (i
= 0; i
< num_screens
; i
++)
2938 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
2940 bar_draw(tmpr
->bar
);
2947 bar_extra_setup(void)
2949 /* do this here because the conf file is in memory */
2950 if (!bar_extra
&& bar_argv
[0]) {
2951 /* launch external status app */
2953 if (pipe(bar_pipe
) == -1)
2954 err(1, "pipe error");
2955 socket_setnonblock(bar_pipe
[0]);
2956 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2958 /* Set stdin to read from the pipe. */
2959 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2962 /* Set stdout to write to the pipe. */
2963 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2966 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2967 err(1, "could not disable SIGPIPE");
2968 switch (bar_pid
= fork()) {
2970 err(1, "cannot fork");
2974 execvp(bar_argv
[0], bar_argv
);
2975 err(1, "%s external app failed", bar_argv
[0]);
2977 default: /* parent */
2982 atexit(kill_bar_extra_atexit
);
2987 kill_bar_extra_atexit(void)
2990 kill(bar_pid
, SIGTERM
);
2998 while ((s
= index(s
, '-'))) {
3003 return (count
== 14);
3009 char *default_string
;
3010 char **missing_charsets
;
3011 int num_missing_charsets
= 0;
3015 XFreeFontSet(display
, bar_fs
);
3019 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n",
3022 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
3023 &num_missing_charsets
, &default_string
);
3025 if (num_missing_charsets
> 0) {
3026 warnx("Unable to load charset(s):");
3028 for (i
= 0; i
< num_missing_charsets
; ++i
)
3029 warnx("%s", missing_charsets
[i
]);
3031 XFreeStringList(missing_charsets
);
3033 if (strcmp(default_string
, ""))
3034 warnx("Glyphs from those sets will be replaced "
3035 "by '%s'.", default_string
);
3037 warnx("Glyphs from those sets won't be drawn.");
3041 errx(1, "Error creating font set structure.");
3043 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
3045 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
3046 2 * bar_border_width
;
3053 xft_init(struct swm_region
*r
)
3055 char *font
, *str
, *search
;
3058 if (bar_font
== NULL
) {
3059 if ((search
= str
= strdup(bar_fonts
)) == NULL
)
3060 errx(1, "insufficient memory.");
3062 while ((font
= strsep(&search
, ",")) != NULL
) {
3066 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
3069 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
3072 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
3076 if (bar_font
== NULL
) {
3077 warnx("unable to load font %s", font
);
3080 DNPRINTF(SWM_D_INIT
, "successfully opened "
3088 if (bar_font
== NULL
)
3089 errx(1, "unable to open a font");
3091 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
3093 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
3094 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
3095 warn("Xft error: unable to allocate color.");
3097 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR
].pixel
, color
);
3099 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
3100 DefaultColormap(display
, r
->s
->idx
), &color
, &search_font_color
))
3101 warn("Xft error: unable to allocate color.");
3103 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
3110 bar_setup(struct swm_region
*r
)
3112 xcb_screen_t
*screen
;
3115 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
3118 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
3119 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
3124 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
3125 err(1, "bar_setup: calloc: failed to allocate memory.");
3127 if (bar_font_legacy
)
3134 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
3135 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
3136 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
3138 /* Assume region is unfocused when we create the bar. */
3139 r
->bar
->id
= xcb_generate_id(conn
);
3140 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
3141 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
3142 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
3143 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
3145 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
3146 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
3147 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
3148 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
3149 | XCB_CW_EVENT_MASK
, wa
);
3151 /* Stack bar window above region window to start. */
3153 wa
[1] = XCB_STACK_MODE_ABOVE
;
3155 xcb_configure_window(conn
, r
->bar
->id
, XCB_CONFIG_WINDOW_SIBLING
|
3156 XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
3158 r
->bar
->buffer
= xcb_generate_id(conn
);
3159 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
3160 WIDTH(r
->bar
), HEIGHT(r
->bar
));
3163 xcb_randr_select_input(conn
, r
->bar
->id
,
3164 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
3167 xcb_map_window(conn
, r
->bar
->id
);
3169 DNPRINTF(SWM_D_BAR
, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
3170 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
3177 bar_cleanup(struct swm_region
*r
)
3181 xcb_destroy_window(conn
, r
->bar
->id
);
3182 xcb_free_pixmap(conn
, r
->bar
->buffer
);
3188 set_win_state(struct ws_win
*win
, uint8_t state
)
3190 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
3192 DNPRINTF(SWM_D_EVENT
, "set_win_state: win %#x, state: %u\n",
3198 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
3199 a_state
, 32, 2, data
);
3203 get_win_state(xcb_window_t w
)
3205 xcb_get_property_reply_t
*r
;
3206 xcb_get_property_cookie_t c
;
3207 uint32_t result
= 0;
3209 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
3210 r
= xcb_get_property_reply(conn
, c
, NULL
);
3212 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
3213 result
= *((uint32_t *)xcb_get_property_value(r
));
3217 DNPRINTF(SWM_D_MISC
, "get_win_state property: win %#x state %u\n", w
,
3223 version(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3225 struct swm_region
*tmpr
;
3228 /* suppress unused warnings since vars are needed */
3233 bar_version
= !bar_version
;
3235 snprintf(bar_vertext
, sizeof bar_vertext
,
3236 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
3238 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
3240 num_screens
= get_screen_count();
3241 for (i
= 0; i
< num_screens
; i
++) {
3242 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
3243 bar_draw(tmpr
->bar
);
3250 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
3252 xcb_client_message_event_t ev
;
3260 name
= get_atom_name(a
);
3261 DNPRINTF(SWM_D_EVENT
, "client_msg: win %#x, atom: %s(%u), "
3263 win
->id
, name
, a
, t
);
3267 bzero(&ev
, sizeof ev
);
3268 ev
.response_type
= XCB_CLIENT_MESSAGE
;
3269 ev
.window
= win
->id
;
3272 ev
.data
.data32
[0] = a
;
3273 ev
.data
.data32
[1] = t
;
3275 xcb_send_event(conn
, 0, win
->id
,
3276 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
3279 /* synthetic response to a ConfigureRequest when not making a change */
3281 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
3283 xcb_configure_notify_event_t ce
;
3288 /* send notification of unchanged state. */
3289 bzero(&ce
, sizeof(ce
));
3290 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
3293 ce
.width
= WIDTH(win
);
3294 ce
.height
= HEIGHT(win
);
3295 ce
.border_width
= 0;
3296 ce
.override_redirect
= 0;
3301 ce
.window
= win
->id
;
3302 ce
.above_sibling
= XCB_WINDOW_NONE
;
3305 ce
.event
= ev
->window
;
3306 ce
.window
= ev
->window
;
3308 /* make response appear more WM_SIZE_HINTS-compliant */
3309 if (win
->sh
.flags
) {
3310 DNPRINTF(SWM_D_MISC
, "config_win: hints: win %#x,"
3311 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
3312 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
3313 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
3314 SH_INC_W(win
), SH_INC_H(win
));
3319 /* the hint may be set... to 0! */
3320 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
3321 ce
.width
= SH_MIN_W(win
);
3322 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
3323 ce
.height
= SH_MIN_H(win
);
3328 /* may also be advertized as 0 */
3329 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
3330 ce
.width
= SH_MAX_W(win
);
3331 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
3332 ce
.height
= SH_MAX_H(win
);
3335 /* resize increment. */
3337 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
3338 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
3340 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
3341 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
3345 /* adjust x and y for requested border_width. */
3346 ce
.x
+= ev
->border_width
;
3347 ce
.y
+= ev
->border_width
;
3349 ce
.above_sibling
= ev
->sibling
;
3352 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
3353 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
3354 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
3356 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
3361 count_win(struct workspace
*ws
, bool count_floating
)
3366 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3367 if (!count_floating
&& FLOATING(win
))
3373 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
3379 quit(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3381 /* suppress unused warnings since vars are needed */
3386 DNPRINTF(SWM_D_MISC
, "quit\n");
3391 lower_window(struct ws_win
*win
)
3393 struct ws_win
*target
= NULL
;
3394 struct workspace
*ws
;
3396 DNPRINTF(SWM_D_EVENT
, "lower_window: win %#x\n", WINID(win
));
3403 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3404 if (target
== win
|| ICONIC(target
))
3406 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3409 if (win
->transient
== target
->transient
)
3411 if (win
->transient
== target
->id
)
3414 if (FULLSCREEN(target
))
3416 if (FULLSCREEN(win
))
3418 if (MAXIMIZED(target
))
3422 if (ABOVE(target
) || TRANS(target
))
3424 if (ABOVE(win
) || TRANS(win
))
3428 /* Change stack position. */
3429 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3431 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3433 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3435 update_win_stacking(win
);
3438 if (swm_debug
& SWM_D_STACK
) {
3439 DPRINTF("=== stacking order (top down) === \n");
3440 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3441 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3442 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3443 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3444 YESNO(ICONIC(target
)));
3448 DNPRINTF(SWM_D_EVENT
, "lower_window: done\n");
3452 raise_window(struct ws_win
*win
)
3454 struct ws_win
*target
= NULL
;
3455 struct workspace
*ws
;
3457 DNPRINTF(SWM_D_EVENT
, "raise_window: win %#x\n", WINID(win
));
3464 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3465 if (target
== win
|| ICONIC(target
))
3467 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3469 if (TRANS(win
) && (win
->transient
== target
->transient
||
3470 win
->transient
== target
->id
))
3472 if (FULLSCREEN(win
))
3474 if (FULLSCREEN(target
))
3478 if (MAXIMIZED(target
))
3480 if (ABOVE(win
) || TRANS(win
) ||
3481 (win
->ws
->focus
== win
&& ws
->always_raise
))
3483 if (!ABOVE(target
) && !TRANS(target
))
3487 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3489 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3491 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3493 update_win_stacking(win
);
3496 if (swm_debug
& SWM_D_STACK
) {
3497 DPRINTF("=== stacking order (top down) === \n");
3498 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3499 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3500 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3501 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3502 YESNO(ICONIC(target
)));
3506 DNPRINTF(SWM_D_EVENT
, "raise_window: done\n");
3510 update_win_stacking(struct ws_win
*win
)
3515 struct swm_region
*r
;
3516 uint16_t configure_mask
;
3519 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
3522 if (win
->frame
== XCB_WINDOW_NONE
) {
3523 DNPRINTF(SWM_D_EVENT
, "update_window_stacking: win %#x not "
3524 "reparented.\n", win
->id
);
3528 if (FLOATINGFULLMAX(win
) || (win
->ws
->always_raise
&&
3529 win
->ws
->focus
== win
)) {
3530 configure_mask
= XCB_CONFIG_WINDOW_STACK_MODE
;
3531 val
[0] = XCB_STACK_MODE_ABOVE
;
3534 DNPRINTF(SWM_D_EVENT
, "update_win_stacking: to very top "
3535 "win %#x (%#x), ", win
->frame
, win
->id
);
3537 configure_mask
= XCB_CONFIG_WINDOW_STACK_MODE
|
3538 XCB_CONFIG_WINDOW_SIBLING
;
3539 val
[0] = r
->bar
->id
;
3540 val
[1] = XCB_STACK_MODE_BELOW
;
3542 DNPRINTF(SWM_D_EVENT
, "update_win_stacking: to tile top "
3543 "win %#x (%#x), ", win
->frame
, win
->id
);
3546 xcb_configure_window(conn
, win
->frame
, configure_mask
, val
);
3549 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
)
3555 map_window(struct ws_win
*win
)
3560 DNPRINTF(SWM_D_EVENT
, "map_window: win %#x, mapped: %s\n",
3561 win
->id
, YESNO(win
->mapped
));
3566 xcb_map_window(conn
, win
->frame
);
3567 xcb_map_window(conn
, win
->id
);
3568 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
3573 unmap_window(struct ws_win
*win
)
3578 DNPRINTF(SWM_D_EVENT
, "unmap_window: win %#x, mapped: %s\n", win
->id
,
3579 YESNO(win
->mapped
));
3584 xcb_unmap_window(conn
, win
->id
);
3585 xcb_unmap_window(conn
, win
->frame
);
3586 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
3587 win
->mapped
= false;
3594 int i
, j
, num_screens
;
3596 num_screens
= get_screen_count();
3597 for (i
= 0; i
< num_screens
; i
++)
3598 for (j
= 0; j
< workspace_limit
; j
++)
3599 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3604 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
3606 xcb_key_press_event_t event
;
3607 xcb_keycode_t
*keycode
;
3612 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
3614 DNPRINTF(SWM_D_MISC
, "fake_keypress: win %#x, keycode %u\n",
3617 bzero(&event
, sizeof(event
));
3618 event
.event
= win
->id
;
3619 event
.root
= win
->s
->root
;
3620 event
.child
= XCB_WINDOW_NONE
;
3621 event
.time
= XCB_CURRENT_TIME
;
3622 event
.event_x
= X(win
);
3623 event
.event_y
= Y(win
);
3626 event
.same_screen
= 1;
3627 event
.detail
= *keycode
;
3628 event
.state
= modifiers
;
3630 event
.response_type
= XCB_KEY_PRESS
;
3631 xcb_send_event(conn
, 1, win
->id
,
3632 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
3634 event
.response_type
= XCB_KEY_RELEASE
;
3635 xcb_send_event(conn
, 1, win
->id
,
3636 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
3642 restart(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3644 /* suppress unused warning since var is needed */
3649 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
3653 execvp(start_argv
[0], start_argv
);
3654 warn("execvp failed");
3655 quit(NULL
, NULL
, NULL
);
3659 get_pointer_win(xcb_window_t root
)
3661 struct ws_win
*win
= NULL
;
3662 xcb_query_pointer_reply_t
*r
;
3664 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: %#x.\n", root
);
3666 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
3668 win
= find_window(r
->child
);
3670 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: %#x.\n",
3673 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
3682 center_pointer(struct swm_region
*r
)
3686 if (!warp_pointer
|| r
== NULL
)
3691 DNPRINTF(SWM_D_EVENT
, "center_pointer: win %#x.\n", WINID(win
));
3693 if (win
&& win
->mapped
)
3694 xcb_warp_pointer(conn
, XCB_NONE
, win
->frame
, 0, 0, 0, 0,
3695 WIDTH(win
) / 2, HEIGHT(win
) / 2);
3697 xcb_warp_pointer(conn
, XCB_NONE
, r
->id
, 0, 0, 0, 0,
3698 WIDTH(r
) / 2, HEIGHT(r
) / 2);
3702 root_to_region(xcb_window_t root
, int check
)
3705 struct swm_region
*r
= NULL
;
3707 xcb_query_pointer_reply_t
*qpr
;
3708 xcb_get_input_focus_reply_t
*gifr
;
3710 DNPRINTF(SWM_D_MISC
, "root_to_region: win %#x\n", root
);
3712 num_screens
= get_screen_count();
3713 for (i
= 0; i
< num_screens
; i
++)
3714 if (screens
[i
].root
== root
)
3717 if (check
& SWM_CK_REGION
)
3718 r
= screens
[i
].r_focus
;
3720 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
3721 /* Try to find an actively focused window */
3722 gifr
= xcb_get_input_focus_reply(conn
,
3723 xcb_get_input_focus(conn
), NULL
);
3725 cfw
= find_window(gifr
->focus
);
3726 if (cfw
&& cfw
->ws
->r
)
3733 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
3734 /* No region with an active focus; try to use pointer. */
3735 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
3736 screens
[i
].root
), NULL
);
3738 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
3739 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
3740 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3741 if (X(r
) <= qpr
->root_x
&&
3742 qpr
->root_x
< MAX_X(r
) &&
3743 Y(r
) <= qpr
->root_y
&&
3744 qpr
->root_y
< MAX_Y(r
))
3751 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
3752 r
= TAILQ_FIRST(&screens
[i
].rl
);
3754 DNPRINTF(SWM_D_MISC
, "root_to_region: idx: %d\n", get_region_index(r
));
3760 find_region(xcb_window_t id
)
3762 struct swm_region
*r
;
3765 num_screens
= get_screen_count();
3766 for (i
= 0; i
< num_screens
; i
++)
3767 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3775 find_bar(xcb_window_t id
)
3777 struct swm_region
*r
;
3780 num_screens
= get_screen_count();
3781 for (i
= 0; i
< num_screens
; i
++)
3782 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3783 if (r
->bar
&& r
->bar
->id
== id
)
3790 find_frame_window(xcb_window_t id
) {
3791 struct swm_region
*r
;
3795 num_screens
= get_screen_count();
3796 for (i
= 0; i
< num_screens
; i
++)
3797 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3798 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
3806 find_window(xcb_window_t id
)
3808 struct ws_win
*win
= NULL
;
3809 int i
, j
, num_screens
;
3810 xcb_query_tree_reply_t
*qtr
;
3812 DNPRINTF(SWM_D_MISC
, "find_window: id: %#x\n", id
);
3814 num_screens
= get_screen_count();
3815 for (i
= 0; i
< num_screens
; i
++)
3816 for (j
= 0; j
< workspace_limit
; j
++)
3817 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3818 if (id
== win
->id
|| id
== win
->frame
)
3822 /* If window isn't top-level, try to find managed ancestor. */
3823 qtr
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3825 if (qtr
->parent
!= XCB_WINDOW_NONE
&& qtr
->parent
!= qtr
->root
)
3826 win
= find_window(qtr
->parent
);
3830 DNPRINTF(SWM_D_MISC
, "find_window: found child %#x "
3831 "of %#x.\n", win
->id
, qtr
->parent
);
3841 find_unmanaged_window(xcb_window_t id
)
3844 int i
, j
, num_screens
;
3846 num_screens
= get_screen_count();
3847 for (i
= 0; i
< num_screens
; i
++)
3848 for (j
= 0; j
< workspace_limit
; j
++)
3849 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
3857 spawn(int ws_idx
, union arg
*args
, bool close_fd
)
3862 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3864 close(xcb_get_file_descriptor(conn
));
3867 if ((ret
= getenv("LD_PRELOAD"))) {
3868 if (asprintf(&ret
, "%s:%s", SWM_LIB
, ret
) == -1) {
3869 warn("spawn: asprintf LD_PRELOAD");
3872 setenv("LD_PRELOAD", ret
, 1);
3875 setenv("LD_PRELOAD", SWM_LIB
, 1);
3878 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3879 warn("spawn: asprintf SWM_WS");
3882 setenv("_SWM_WS", ret
, 1);
3886 if (asprintf(&ret
, "%d", getpid()) == -1) {
3887 warn("spawn: asprintf _SWM_PID");
3890 setenv("_SWM_PID", ret
, 1);
3895 if (setsid() == -1) {
3896 warn("spawn: setsid");
3902 * close stdin and stdout to prevent interaction between apps
3903 * and the baraction script
3904 * leave stderr open to record errors
3906 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3907 warn("spawn: open");
3910 dup2(fd
, STDIN_FILENO
);
3911 dup2(fd
, STDOUT_FILENO
);
3916 execvp(args
->argv
[0], args
->argv
);
3918 warn("spawn: execvp");
3923 kill_refs(struct ws_win
*win
)
3925 struct workspace
*ws
;
3927 int i
, j
, num_screens
;
3932 num_screens
= get_screen_count();
3933 for (i
= 0; i
< num_screens
; i
++) {
3934 for (j
= 0; j
< workspace_limit
; j
++) {
3935 ws
= &screens
[i
].ws
[j
];
3937 if (win
== ws
->focus
)
3939 if (win
== ws
->focus_prev
)
3940 ws
->focus_prev
= NULL
;
3941 if (win
== ws
->focus_pending
)
3942 ws
->focus_pending
= NULL
;
3943 if (win
== ws
->focus_raise
)
3944 ws
->focus_raise
= NULL
;
3947 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3948 if (win
== w
->focus_child
)
3949 w
->focus_child
= NULL
;
3955 validate_win(struct ws_win
*testwin
)
3958 struct workspace
*ws
;
3959 struct swm_region
*r
;
3960 int i
, x
, num_screens
;
3962 if (testwin
== NULL
)
3965 num_screens
= get_screen_count();
3966 for (i
= 0; i
< num_screens
; i
++)
3967 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3968 for (x
= 0; x
< workspace_limit
; x
++) {
3970 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3978 validate_ws(struct workspace
*testws
)
3980 struct swm_region
*r
;
3981 struct workspace
*ws
;
3982 int i
, x
, num_screens
;
3984 /* validate all ws */
3985 num_screens
= get_screen_count();
3986 for (i
= 0; i
< num_screens
; i
++)
3987 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3988 for (x
= 0; x
< workspace_limit
; x
++) {
3997 unfocus_win(struct ws_win
*win
)
3999 xcb_window_t none
= XCB_WINDOW_NONE
;
4001 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: win %#x\n", WINID(win
));
4006 if (win
->ws
== NULL
) {
4007 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
4011 if (validate_ws(win
->ws
)) {
4012 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
4016 if (win
->ws
->r
== NULL
) {
4017 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
4021 if (validate_win(win
)) {
4022 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
4027 if (win
->ws
->focus
== win
) {
4028 win
->ws
->focus
= NULL
;
4029 win
->ws
->focus_prev
= win
;
4030 if (win
->ws
->focus_raise
== win
&& !FLOATING(win
)) {
4031 update_win_stacking(win
);
4035 if (validate_win(win
->ws
->focus
)) {
4036 kill_refs(win
->ws
->focus
);
4037 win
->ws
->focus
= NULL
;
4040 if (validate_win(win
->ws
->focus_prev
)) {
4041 kill_refs(win
->ws
->focus_prev
);
4042 win
->ws
->focus_prev
= NULL
;
4047 /* Raise window to "top unfocused position." */
4048 if (win
->ws
->always_raise
)
4051 /* Update border width */
4052 if (win
->bordered
&& (win
->quirks
& SWM_Q_MINIMALBORDER
) &&
4055 X(win
) += border_width
;
4056 Y(win
) += border_width
;
4060 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4061 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
4063 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
4067 focus_win(struct ws_win
*win
)
4069 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
, *tmpw
;
4070 struct workspace
*ws
;
4071 xcb_get_input_focus_reply_t
*gifr
= NULL
;
4072 xcb_get_window_attributes_reply_t
*war
= NULL
;
4074 DNPRINTF(SWM_D_FOCUS
, "focus_win: win %#x\n", WINID(win
));
4076 if (win
== NULL
|| win
->ws
== NULL
|| !win
->mapped
)
4081 if (validate_ws(ws
))
4084 if (validate_win(win
)) {
4089 gifr
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
4091 DNPRINTF(SWM_D_FOCUS
, "focus_win: cur focus: %#x\n",
4094 cfw
= find_window(gifr
->focus
);
4097 if (cfw
->ws
!= ws
&& cfw
->ws
->r
!= NULL
&&
4098 cfw
->frame
!= XCB_WINDOW_NONE
) {
4105 war
= xcb_get_window_attributes_reply(conn
,
4106 xcb_get_window_attributes(conn
, gifr
->focus
), NULL
);
4107 if (war
&& war
->override_redirect
&& ws
->focus
== win
) {
4108 DNPRINTF(SWM_D_FOCUS
, "focus_win: skip refocus "
4109 "from override_redirect.\n");
4115 if (ws
->focus
!= win
) {
4116 if (ws
->focus
&& ws
->focus
!= cfw
)
4117 unfocus_win(ws
->focus
);
4121 /* Clear focus child redirect. */
4122 win
->focus_child
= NULL
;
4124 /* If transient, adjust parent's focus child for focus_magic. */
4126 parent
= find_window(win
->transient
);
4127 if (parent
&& parent
->focus_child
!= win
)
4128 parent
->focus_child
= win
;
4131 /* Update window border even if workspace is hidden. */
4135 DNPRINTF(SWM_D_FOCUS
, "focus_win: already focused.\n");
4140 /* Set input focus if no input hint, or indicated by hint. */
4141 if (ACCEPTS_FOCUS(win
)) {
4142 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
4143 " revert-to: parent, time: %#x\n", win
->id
,
4145 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
4146 win
->id
, last_event_time
);
4147 } else if (!win
->take_focus
) {
4148 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
4149 " revert-to: parent, time: 0\n", ws
->r
->id
);
4150 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
4151 ws
->r
->id
, XCB_CURRENT_TIME
);
4154 /* Tell app it can adjust focus to a specific window. */
4155 if (win
->take_focus
) {
4156 /* java is special; always tell parent */
4157 if (TRANS(win
) && win
->java
)
4158 client_msg(parent
, a_takefocus
,
4161 client_msg(win
, a_takefocus
, last_event_time
);
4164 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
4166 /* If a parent exists, map it first. */
4168 raise_window(parent
);
4171 /* Map siblings next. */
4172 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
,
4174 if (w
!= win
&& !ICONIC(w
) &&
4175 w
->transient
== parent
->id
) {
4181 /* Map focused window. */
4185 /* Stack any children of focus window. */
4186 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
4187 if (w
->transient
== win
->id
&& !ICONIC(w
)) {
4191 } else if (tile_gap
< 0 && !ABOVE(win
)) {
4193 * Windows overlap in the layout.
4194 * Raise focused win above all tiled wins.
4202 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4203 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4206 bar_draw(ws
->r
->bar
);
4212 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
4215 /* If a child window should have focus instead, return it. */
4217 get_focus_magic(struct ws_win
*win
)
4219 struct ws_win
*parent
= NULL
;
4220 struct ws_win
*child
= NULL
;
4222 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: win %#x\n", WINID(win
));
4227 parent
= find_window(win
->transient
);
4229 /* If parent prefers focus elsewhere, then try to do so. */
4230 if (parent
&& (child
= parent
->focus_child
)) {
4231 if (validate_win(child
) == 0 && child
->mapped
)
4234 parent
->focus_child
= NULL
;
4238 /* If this window prefers focus elsewhere, then try to do so. */
4239 if ((child
= win
->focus_child
)) {
4240 if (validate_win(child
) == 0 && child
->mapped
)
4243 win
->focus_child
= NULL
;
4250 event_drain(uint8_t rt
)
4252 xcb_generic_event_t
*evt
;
4254 /* ensure all pending requests have been processed before filtering. */
4256 while ((evt
= get_next_event(false))) {
4257 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
4265 set_region(struct swm_region
*r
)
4267 struct swm_region
*rf
;
4274 /* Unfocus old region bar. */
4279 xcb_change_window_attributes(conn
, rf
->bar
->id
,
4280 XCB_CW_BORDER_PIXEL
,
4281 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
4284 if (rf
!= NULL
&& rf
!= r
&& (X(rf
) != X(r
) || Y(rf
) != Y(r
) ||
4285 WIDTH(rf
) != WIDTH(r
) || HEIGHT(rf
) != HEIGHT(r
))) {
4286 /* Set _NET_DESKTOP_GEOMETRY. */
4288 vals
[1] = HEIGHT(r
);
4289 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, r
->s
->root
,
4290 ewmh
[_NET_DESKTOP_GEOMETRY
].atom
, XCB_ATOM_CARDINAL
, 32, 2,
4294 /* Set region bar border to focus_color. */
4295 xcb_change_window_attributes(conn
, r
->bar
->id
,
4296 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
4300 /* Update the focus window frame on the now unfocused region. */
4301 if (rf
&& rf
->ws
->focus
)
4302 draw_frame(rf
->ws
->focus
);
4304 ewmh_update_current_desktop();
4308 focus_region(struct swm_region
*r
)
4311 struct swm_region
*old_r
;
4316 old_r
= r
->s
->r_focus
;
4319 nfw
= get_region_focus(r
);
4323 /* New region is empty; need to manually unfocus win. */
4325 unfocus_win(old_r
->ws
->focus
);
4326 /* Clear bar since empty. */
4327 bar_draw(old_r
->bar
);
4330 DNPRINTF(SWM_D_FOCUS
, "focus_region: set_input_focus: %#x, "
4331 "revert-to: parent, time: 0\n", r
->id
);
4332 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4339 switchws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4341 struct swm_region
*this_r
, *other_r
;
4343 struct workspace
*new_ws
, *old_ws
;
4344 xcb_window_t none
= XCB_WINDOW_NONE
;
4345 int wsid
= args
->id
;
4346 bool unmap_old
= false;
4351 if (wsid
>= workspace_limit
)
4355 old_ws
= this_r
->ws
;
4356 new_ws
= &this_r
->s
->ws
[wsid
];
4358 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
4359 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
4361 if (new_ws
== NULL
|| old_ws
== NULL
)
4363 if (new_ws
== old_ws
)
4366 other_r
= new_ws
->r
;
4367 if (other_r
&& workspace_clamp
&&
4368 bp
->action
!= FN_RG_MOVE_NEXT
&& bp
->action
!= FN_RG_MOVE_PREV
) {
4369 DNPRINTF(SWM_D_WS
, "switchws: ws clamped.\n");
4372 DNPRINTF(SWM_D_WS
, "switchws: warping focus to region "
4373 "with ws %d.\n", wsid
);
4374 focus_region(other_r
);
4375 center_pointer(other_r
);
4381 if ((win
= old_ws
->focus
) != NULL
) {
4384 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4385 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4390 /* the other ws is visible in another region, exchange them */
4391 other_r
->ws_prior
= new_ws
;
4392 other_r
->ws
= old_ws
;
4393 old_ws
->r
= other_r
;
4395 /* the other workspace is hidden, hide this one */
4400 this_r
->ws_prior
= old_ws
;
4401 this_r
->ws
= new_ws
;
4404 /* Set focus_pending before stacking, if needed. */
4405 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
4406 validate_win(new_ws
->focus_pending
))) {
4407 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
4408 new_ws
->focus
= new_ws
->focus_prev
;
4409 new_ws
->focus_prev
= NULL
;
4412 new_ws
->state
= SWM_WS_STATE_MAPPING
;
4417 /* unmap old windows */
4419 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
4421 old_ws
->state
= SWM_WS_STATE_HIDDEN
;
4424 /* if workspaces were swapped, then don't wait to set focus */
4425 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
4426 if (new_ws
->focus_pending
) {
4427 focus_win(new_ws
->focus_pending
);
4428 new_ws
->focus_pending
= NULL
;
4432 /* Clear bar and set focus on region input win if new ws is empty. */
4433 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
4434 DNPRINTF(SWM_D_FOCUS
, "switchws: set_input_focus: %#x, "
4435 "revert-to: parent, time: 0\n", r
->id
);
4436 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4441 ewmh_update_current_desktop();
4445 new_ws
->state
= SWM_WS_STATE_MAPPED
;
4447 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
4451 cyclews(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4454 struct swm_screen
*s
= r
->s
;
4455 bool cycle_all
= false, mv
= false;
4457 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4458 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4464 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
4467 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
4470 case SWM_ARG_ID_CYCLEWS_UP
:
4471 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
4473 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
4476 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
4479 case SWM_ARG_ID_CYCLEWS_DOWN
:
4480 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
4487 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
4489 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
4493 send_to_ws(bp
, r
, &a
);
4495 switchws(bp
, r
, &a
);
4496 } while (a
.id
!= r
->ws
->idx
);
4498 DNPRINTF(SWM_D_FOCUS
, "cyclews: done\n");
4502 priorws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4508 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4509 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4511 if (r
->ws_prior
== NULL
)
4514 a
.id
= r
->ws_prior
->idx
;
4515 switchws(bp
, r
, &a
);
4516 DNPRINTF(SWM_D_FOCUS
, "priorws: done\n");
4520 focusrg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4522 int ridx
= args
->id
, i
, num_screens
;
4523 struct swm_region
*rr
= NULL
;
4527 num_screens
= get_screen_count();
4528 /* do nothing if we don't have more than one screen */
4529 if (!(num_screens
> 1 || outputs
> 1))
4532 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
4534 rr
= TAILQ_FIRST(&r
->s
->rl
);
4535 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
4536 rr
= TAILQ_NEXT(rr
, entry
);
4544 DNPRINTF(SWM_D_FOCUS
, "focusrg: done\n");
4548 cyclerg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4551 struct swm_region
*rr
= NULL
;
4554 num_screens
= get_screen_count();
4555 /* do nothing if we don't have more than one screen */
4556 if (!(num_screens
> 1 || outputs
> 1))
4560 DNPRINTF(SWM_D_FOCUS
, "cyclerg: id: %d, region: %d\n", args
->id
, i
);
4563 case SWM_ARG_ID_CYCLERG_UP
:
4564 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4565 rr
= TAILQ_NEXT(r
, entry
);
4567 rr
= TAILQ_FIRST(&screens
[i
].rl
);
4569 case SWM_ARG_ID_CYCLERG_DOWN
:
4570 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4571 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
4573 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
4582 case SWM_ARG_ID_CYCLERG_UP
:
4583 case SWM_ARG_ID_CYCLERG_DOWN
:
4588 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4589 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4591 switchws(bp
, r
, &a
);
4597 DNPRINTF(SWM_D_FOCUS
, "cyclerg: done\n");
4600 /* Sorts transients after parent. */
4602 sort_windows(struct ws_win_list
*wl
)
4604 struct ws_win
*win
, *parent
, *nxt
;
4609 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
4610 nxt
= TAILQ_NEXT(win
, entry
);
4612 parent
= find_window(win
->transient
);
4613 if (parent
== NULL
) {
4614 warnx("not possible bug");
4617 TAILQ_REMOVE(wl
, win
, entry
);
4618 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
4624 swapwin(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4626 struct ws_win
*target
, *source
;
4627 struct ws_win
*cur_focus
;
4628 struct ws_win_list
*wl
;
4632 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4633 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4635 cur_focus
= r
->ws
->focus
;
4636 if (cur_focus
== NULL
|| FULLSCREEN(cur_focus
))
4639 /* Adjust stacking in floating layer. */
4640 if (ABOVE(cur_focus
)) {
4642 case SWM_ARG_ID_SWAPPREV
:
4643 target
= TAILQ_PREV(cur_focus
, ws_win_stack
,
4645 if (target
!= NULL
&& FLOATING(target
)) {
4646 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4648 TAILQ_INSERT_BEFORE(target
, cur_focus
,
4650 update_win_stacking(cur_focus
);
4654 case SWM_ARG_ID_SWAPNEXT
:
4655 target
= TAILQ_NEXT(cur_focus
, stack_entry
);
4656 if (target
!= NULL
&& FLOATING(target
)) {
4657 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4659 TAILQ_INSERT_AFTER(&cur_focus
->ws
->stack
,
4660 target
, cur_focus
, stack_entry
);
4661 update_win_stacking(cur_focus
);
4669 if (r
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4672 clear_maximized(r
->ws
);
4675 wl
= &source
->ws
->winlist
;
4678 case SWM_ARG_ID_SWAPPREV
:
4680 source
= find_window(source
->transient
);
4681 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
4682 if (target
&& target
->transient
)
4683 target
= find_window(target
->transient
);
4684 TAILQ_REMOVE(wl
, source
, entry
);
4686 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4688 TAILQ_INSERT_BEFORE(target
, source
, entry
);
4690 case SWM_ARG_ID_SWAPNEXT
:
4691 target
= TAILQ_NEXT(source
, entry
);
4692 /* move the parent and let the sort handle the move */
4694 source
= find_window(source
->transient
);
4695 TAILQ_REMOVE(wl
, source
, entry
);
4697 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4699 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
4701 case SWM_ARG_ID_SWAPMAIN
:
4702 target
= TAILQ_FIRST(wl
);
4703 if (target
== source
) {
4704 if (source
->ws
->focus_prev
!= NULL
&&
4705 source
->ws
->focus_prev
!= target
)
4706 source
= source
->ws
->focus_prev
;
4710 if (target
== NULL
|| source
== NULL
)
4712 source
->ws
->focus_prev
= target
;
4713 TAILQ_REMOVE(wl
, target
, entry
);
4714 TAILQ_INSERT_BEFORE(source
, target
, entry
);
4715 TAILQ_REMOVE(wl
, source
, entry
);
4716 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4718 case SWM_ARG_ID_MOVELAST
:
4719 TAILQ_REMOVE(wl
, source
, entry
);
4720 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4723 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
4728 ewmh_update_client_list();
4734 DNPRINTF(SWM_D_MOVE
, "swapwin: done\n");
4738 get_focus_prev(struct ws_win
*win
)
4740 struct ws_win
*winfocus
= NULL
;
4741 struct ws_win
*cur_focus
= NULL
;
4742 struct ws_win_list
*wl
= NULL
;
4743 struct workspace
*ws
= NULL
;
4745 if (!(win
&& win
->ws
))
4750 cur_focus
= ws
->focus
;
4752 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: win %#x, cur_focus: %#x, "
4753 "focus_prev: %#x\n", WINID(win
), WINID(cur_focus
),
4754 WINID(ws
->focus_prev
));
4756 /* pickle, just focus on whatever */
4757 if (cur_focus
== NULL
) {
4758 /* use prev_focus if valid */
4759 if (ws
->focus_prev
&& find_window(ws
->focus_prev
->id
))
4760 winfocus
= ws
->focus_prev
;
4764 /* if transient focus on parent */
4765 if (TRANS(cur_focus
)) {
4766 winfocus
= find_window(cur_focus
->transient
);
4770 /* if in max_stack try harder */
4771 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
4772 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
4773 if (cur_focus
!= ws
->focus_prev
)
4774 winfocus
= ws
->focus_prev
;
4776 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
4781 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
4783 if (winfocus
== NULL
|| winfocus
== win
) {
4784 switch (focus_close
) {
4785 case SWM_STACK_BOTTOM
:
4786 TAILQ_FOREACH(winfocus
, wl
, entry
)
4787 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4791 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4792 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4795 case SWM_STACK_ABOVE
:
4796 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
4797 while (winfocus
&& ICONIC(winfocus
))
4798 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4800 if (winfocus
== NULL
) {
4801 if (focus_close_wrap
) {
4802 TAILQ_FOREACH(winfocus
, wl
, entry
)
4803 if (!ICONIC(winfocus
) &&
4804 winfocus
!= cur_focus
)
4807 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4809 if (!ICONIC(winfocus
) &&
4810 winfocus
!= cur_focus
)
4815 case SWM_STACK_BELOW
:
4816 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
4817 while (winfocus
&& ICONIC(winfocus
))
4818 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
4821 if (winfocus
== NULL
) {
4822 if (focus_close_wrap
) {
4823 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4825 if (!ICONIC(winfocus
) &&
4826 winfocus
!= cur_focus
)
4829 TAILQ_FOREACH(winfocus
, wl
, entry
)
4830 if (!ICONIC(winfocus
) &&
4831 winfocus
!= cur_focus
)
4839 if (winfocus
== NULL
||
4840 (winfocus
&& (ICONIC(winfocus
) || winfocus
== cur_focus
))) {
4841 if (focus_default
== SWM_STACK_TOP
) {
4842 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4843 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4846 TAILQ_FOREACH(winfocus
, wl
, entry
)
4847 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4854 return get_focus_magic(winfocus
);
4858 get_region_focus(struct swm_region
*r
)
4860 struct ws_win
*winfocus
= NULL
;
4865 if (r
->ws
->focus
&& !ICONIC(r
->ws
->focus
))
4866 winfocus
= r
->ws
->focus
;
4867 else if (r
->ws
->focus_prev
&& !ICONIC(r
->ws
->focus_prev
))
4868 winfocus
= r
->ws
->focus_prev
;
4870 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
4871 if (!ICONIC(winfocus
))
4874 return get_focus_magic(winfocus
);
4878 focus(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4880 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
4881 struct ws_win_list
*wl
= NULL
;
4882 struct workspace
*ws
= NULL
;
4889 cur_focus
= r
->ws
->focus
;
4893 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d, cur_focus: %#x\n", args
->id
,
4896 /* Make sure an uniconified window has focus, if one exists. */
4897 if (cur_focus
== NULL
) {
4898 cur_focus
= TAILQ_FIRST(wl
);
4899 while (cur_focus
!= NULL
&& ICONIC(cur_focus
))
4900 cur_focus
= TAILQ_NEXT(cur_focus
, entry
);
4902 DNPRINTF(SWM_D_FOCUS
, "focus: new cur_focus: %#x\n",
4907 case SWM_ARG_ID_FOCUSPREV
:
4908 if (cur_focus
== NULL
)
4911 winfocus
= cur_focus
;
4913 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
4914 if (winfocus
== NULL
)
4915 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
4916 if (winfocus
== cur_focus
)
4918 } while (winfocus
&& (ICONIC(winfocus
) ||
4919 winfocus
->id
== cur_focus
->transient
||
4920 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4921 winfocus
->transient
== cur_focus
->transient
) ||
4922 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4924 case SWM_ARG_ID_FOCUSNEXT
:
4925 if (cur_focus
== NULL
)
4928 winfocus
= cur_focus
;
4930 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4931 if (winfocus
== NULL
)
4932 winfocus
= TAILQ_FIRST(wl
);
4933 if (winfocus
== cur_focus
)
4935 } while (winfocus
&& (ICONIC(winfocus
) ||
4936 winfocus
->id
== cur_focus
->transient
||
4937 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4938 winfocus
->transient
== cur_focus
->transient
) ||
4939 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4941 case SWM_ARG_ID_FOCUSMAIN
:
4942 if (cur_focus
== NULL
)
4945 winfocus
= TAILQ_FIRST(wl
);
4946 if (winfocus
== cur_focus
)
4947 winfocus
= cur_focus
->ws
->focus_prev
;
4949 case SWM_ARG_ID_FOCUSURGENT
:
4950 /* Search forward for the next urgent window. */
4954 for (i
= 0; i
<= workspace_limit
; ++i
) {
4956 head
= TAILQ_FIRST(&r
->s
->ws
[(ws
->idx
+ i
) %
4957 workspace_limit
].winlist
);
4960 if (head
== cur_focus
) {
4962 winfocus
= cur_focus
;
4965 } else if (get_urgent(head
)) {
4970 head
= TAILQ_NEXT(head
, entry
);
4977 /* Switch ws if new focus is on a different ws. */
4978 if (winfocus
&& winfocus
->ws
!= ws
) {
4979 a
.id
= winfocus
->ws
->idx
;
4980 switchws(bp
, r
, &a
);
4987 if (clear_maximized(ws
) > 0)
4990 focus_win(get_focus_magic(winfocus
));
4995 DNPRINTF(SWM_D_FOCUS
, "focus: done\n");
4999 focus_pointer(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5003 /* Not needed for buttons since this is already done in buttonpress. */
5004 if (bp
->type
== KEYBIND
) {
5005 focus_win(get_pointer_win(r
->s
->root
));
5011 cycle_layout(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5013 struct workspace
*ws
= r
->ws
;
5015 /* suppress unused warning since var is needed */
5019 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
5022 if (ws
->cur_layout
->l_stack
== NULL
)
5023 ws
->cur_layout
= &layouts
[0];
5025 clear_maximized(ws
);
5030 focus_win(get_region_focus(r
));
5037 stack_config(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5039 struct workspace
*ws
= r
->ws
;
5043 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
5046 if (args
->id
== SWM_ARG_ID_STACKINIT
) {
5049 ws
->cur_layout
->l_config(ws
, args
->id
);
5052 clear_maximized(ws
);
5062 stack(struct swm_region
*r
) {
5063 struct swm_geometry g
;
5064 struct swm_region
*r_prev
;
5070 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
5072 /* Adjust stack area for region bar and padding. */
5074 g
.x
+= region_padding
;
5075 g
.y
+= region_padding
;
5076 g
.w
-= 2 * border_width
+ 2 * region_padding
;
5077 g
.h
-= 2 * border_width
+ 2 * region_padding
;
5078 if (bar_enabled
&& r
->ws
->bar_enabled
) {
5084 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d (screen: "
5085 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
5086 r
->ws
->idx
, r
->s
->idx
, get_region_index(r
), g
.x
, g
.y
, g
.w
, g
.h
);
5088 r_prev
= TAILQ_PREV(r
, swm_region_list
, entry
);
5090 /* Stack bar/input relative to prev. region. */
5091 val
[1] = XCB_STACK_MODE_ABOVE
;
5093 val
[0] = r_prev
->id
;
5094 DNPRINTF(SWM_D_STACK
, "stack: region input %#x "
5095 "relative to %#x.\n", r
->id
, val
[0]);
5096 xcb_configure_window(conn
, r
->id
,
5097 XCB_CONFIG_WINDOW_SIBLING
|
5098 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
5100 val
[0] = r_prev
->bar
->id
;
5101 DNPRINTF(SWM_D_STACK
, "stack: region bar %#x "
5102 "relative to %#x.\n", r
->bar
->id
, val
[0]);
5103 xcb_configure_window(conn
, r
->bar
->id
,
5104 XCB_CONFIG_WINDOW_SIBLING
|
5105 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
5108 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
5109 r
->ws
->cur_layout
->l_string(r
->ws
);
5110 /* save r so we can track region changes */
5116 DNPRINTF(SWM_D_STACK
, "stack: end\n");
5120 store_float_geom(struct ws_win
*win
)
5122 if (win
== NULL
|| win
->ws
->r
== NULL
)
5125 /* retain window geom and region geom */
5126 win
->g_float
= win
->g
;
5127 win
->g_float
.x
-= X(win
->ws
->r
);
5128 win
->g_float
.y
-= Y(win
->ws
->r
);
5129 win
->g_floatvalid
= true;
5130 DNPRINTF(SWM_D_MISC
, "store_float_geom: win %#x, g: (%d,%d)"
5131 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
5132 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
5133 win
->g_float
.w
, win
->g_float
.h
);
5137 load_float_geom(struct ws_win
*win
)
5139 if (win
== NULL
|| win
->ws
->r
== NULL
)
5142 if (win
->g_floatvalid
) {
5143 win
->g
= win
->g_float
;
5144 X(win
) += X(win
->ws
->r
);
5145 Y(win
) += Y(win
->ws
->r
);
5146 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g: (%d,%d)"
5147 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
5150 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g_float "
5151 "is not set.\n", win
->id
);
5156 update_floater(struct ws_win
*win
)
5158 struct workspace
*ws
;
5159 struct swm_region
*r
;
5161 DNPRINTF(SWM_D_MISC
, "update_floater: win %#x\n", WINID(win
));
5168 if ((r
= ws
->r
) == NULL
)
5171 win
->bordered
= true;
5173 if (FULLSCREEN(win
)) {
5174 /* _NET_WM_FULLSCREEN: fullscreen without border. */
5175 if (!win
->g_floatvalid
)
5176 store_float_geom(win
);
5179 win
->bordered
= false;
5180 } else if (MAXIMIZED(win
)) {
5181 /* Maximize: like a single stacked window. */
5182 if (!win
->g_floatvalid
)
5183 store_float_geom(win
);
5187 if (bar_enabled
&& ws
->bar_enabled
&& !maximize_hide_bar
) {
5189 Y(win
) += bar_height
;
5190 HEIGHT(win
) -= bar_height
;
5191 } else if (disable_border
) {
5192 win
->bordered
= false;
5195 if (win
->bordered
) {
5196 /* Window geometry excludes frame. */
5197 X(win
) += border_width
;
5198 Y(win
) += border_width
;
5199 HEIGHT(win
) -= 2 * border_width
;
5200 WIDTH(win
) -= 2 * border_width
;
5203 /* Normal floating window. */
5204 /* Update geometry if new region. */
5206 load_float_geom(win
);
5208 if (((win
->quirks
& SWM_Q_FULLSCREEN
) &&
5209 WIDTH(win
) >= WIDTH(r
) && HEIGHT(win
) >= HEIGHT(r
)) ||
5210 ((!WS_FOCUSED(win
->ws
) || win
->ws
->focus
!= win
) &&
5211 (win
->quirks
& SWM_Q_MINIMALBORDER
))) {
5213 win
->bordered
= false;
5214 } else if (!MANUAL(win
)) {
5215 if (TRANS(win
) && (win
->quirks
& SWM_Q_TRANSSZ
)) {
5216 /* Adjust size on TRANSSZ quirk. */
5217 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
5218 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
5221 if (!(win
->quirks
& SWM_Q_ANYWHERE
)) {
5223 * Floaters and transients are auto-centred
5224 * unless manually moved, resized or ANYWHERE
5227 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2;
5228 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2;
5229 store_float_geom(win
);
5234 /* Ensure at least 1 pixel of the window is in the region. */
5235 region_containment(win
, r
, SWM_CW_ALLSIDES
);
5241 * Send keystrokes to terminal to decrease/increase the font size as the
5242 * window size changes.
5245 adjust_font(struct ws_win
*win
)
5247 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
5248 ABOVE(win
) || TRANS(win
))
5251 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
5252 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
5253 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
5254 win
->font_size_boundary
[win
->font_steps
] =
5255 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
5258 win
->last_inc
= win
->sh
.width_inc
;
5259 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
5260 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
5261 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
5264 win
->last_inc
= win
->sh
.width_inc
;
5265 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
5269 #define SWAPXY(g) do { \
5271 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
5272 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
5275 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, bool flip
)
5277 struct swm_geometry cell
, r_g
= *g
;
5279 int i
= 0, j
= 0, s
= 0, stacks
= 0;
5280 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
5281 int hrh
= 0, extra
= 0, h_slice
= 0, last_h
= 0;
5282 int split
= 0, colno
= 0;
5283 int winno
, mwin
= 0, msize
= 0;
5284 int remain
, missing
, v_slice
, mscale
;
5285 bool bordered
= true, reconfigure
= false;
5288 * cell: geometry for window, including frame.
5289 * mwin: # of windows in master area.
5290 * mscale: size increment of master area.
5291 * stacks: # of stack columns
5294 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
5295 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
5297 memset(&cell
, 0, sizeof(cell
));
5299 /* Prepare tiling variables, if needed. */
5300 if ((winno
= count_win(ws
, false)) > 0) {
5301 /* Find first tiled window. */
5302 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
5303 if (!FLOATING(win
) && !ICONIC(win
))
5306 /* Take into account size hints of first tiled window. */
5308 w_inc
= win
->sh
.width_inc
;
5309 w_base
= win
->sh
.base_width
;
5310 mwin
= ws
->l_state
.horizontal_mwin
;
5311 mscale
= ws
->l_state
.horizontal_msize
;
5312 stacks
= ws
->l_state
.horizontal_stacks
;
5315 w_inc
= win
->sh
.height_inc
;
5316 w_base
= win
->sh
.base_height
;
5317 mwin
= ws
->l_state
.vertical_mwin
;
5318 mscale
= ws
->l_state
.vertical_msize
;
5319 stacks
= ws
->l_state
.vertical_stacks
;
5323 cell
.x
+= border_width
;
5324 cell
.y
+= border_width
;
5326 if (stacks
> winno
- mwin
)
5327 stacks
= winno
- mwin
;
5331 h_slice
= r_g
.h
/ SWM_H_SLICE
;
5332 if (mwin
&& winno
> mwin
) {
5333 v_slice
= r_g
.w
/ SWM_V_SLICE
;
5337 cell
.w
= v_slice
* mscale
;
5339 if (w_inc
> 1 && w_inc
< v_slice
) {
5340 /* Adjust for requested size increment. */
5341 remain
= (cell
.w
- w_base
) % w_inc
;
5347 cell
.x
+= r_g
.w
- msize
;
5350 colno
= split
= winno
/ stacks
;
5351 cell
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
5352 2 * border_width
) / stacks
);
5355 hrh
= r_g
.h
/ colno
;
5356 extra
= r_g
.h
- (colno
* hrh
);
5357 cell
.h
= hrh
- 2 * border_width
;
5358 i
= j
= 0, s
= stacks
;
5361 /* Update window geometry. */
5362 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
5366 if (FLOATINGFULLMAX(win
)) {
5367 update_floater(win
);
5372 if (split
&& i
== split
) {
5373 colno
= (winno
- mwin
) / stacks
;
5374 if (s
<= (winno
- mwin
) % stacks
)
5377 hrh
= r_g
.h
/ colno
;
5378 extra
= r_g
.h
- (colno
* hrh
);
5381 cell
.x
+= cell
.w
+ 2 * border_width
+
5384 cell
.w
= (r_g
.w
- msize
-
5385 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
5387 cell
.w
+= (r_g
.w
- msize
-
5388 (stacks
* (2 * border_width
+ tile_gap
))) %
5392 cell
.x
-= cell
.w
+ 2 * border_width
+
5398 cell
.h
= hrh
- 2 * border_width
- tile_gap
;
5401 h_inc
= win
->sh
.width_inc
;
5402 h_base
= win
->sh
.base_width
;
5404 h_inc
= win
->sh
.height_inc
;
5405 h_base
= win
->sh
.base_height
;
5408 if (j
== colno
- 1) {
5409 cell
.h
= hrh
+ extra
;
5410 } else if (h_inc
> 1 && h_inc
< h_slice
) {
5411 /* adjust for window's requested size increment */
5412 remain
= (cell
.h
- h_base
) % h_inc
;
5413 missing
= h_inc
- remain
;
5415 if (missing
<= extra
|| j
== 0) {
5425 cell
.y
= r_g
.y
+ border_width
;
5427 cell
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
5429 /* Window coordinates exclude frame. */
5431 if (winno
> 1 || !disable_border
||
5432 (bar_enabled
&& ws
->bar_enabled
)) {
5439 if (X(win
) != cell
.y
|| Y(win
) != cell
.x
||
5440 WIDTH(win
) != cell
.h
|| HEIGHT(win
) != cell
.w
) {
5444 WIDTH(win
) = cell
.h
;
5445 HEIGHT(win
) = cell
.w
;
5448 if (X(win
) != cell
.x
|| Y(win
) != cell
.y
||
5449 WIDTH(win
) != cell
.w
|| HEIGHT(win
) != cell
.h
) {
5453 WIDTH(win
) = cell
.w
;
5454 HEIGHT(win
) = cell
.h
;
5459 X(win
) -= border_width
;
5460 Y(win
) -= border_width
;
5461 WIDTH(win
) += 2 * border_width
;
5462 HEIGHT(win
) += 2 * border_width
;
5465 if (bordered
!= win
->bordered
) {
5467 win
->bordered
= bordered
;
5480 /* Stack all windows from bottom up. */
5481 TAILQ_FOREACH_REVERSE(win
, &ws
->stack
, ws_win_stack
, stack_entry
)
5483 update_win_stacking(win
);
5485 /* Map all windows from top down. */
5486 TAILQ_FOREACH(win
, &ws
->stack
, stack_entry
)
5490 DNPRINTF(SWM_D_STACK
, "stack_master: done\n");
5494 vertical_config(struct workspace
*ws
, int id
)
5496 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
5500 case SWM_ARG_ID_STACKINIT
:
5501 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
5502 ws
->l_state
.vertical_mwin
= 1;
5503 ws
->l_state
.vertical_stacks
= 1;
5505 case SWM_ARG_ID_MASTERSHRINK
:
5506 if (ws
->l_state
.vertical_msize
> 1)
5507 ws
->l_state
.vertical_msize
--;
5509 case SWM_ARG_ID_MASTERGROW
:
5510 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
5511 ws
->l_state
.vertical_msize
++;
5513 case SWM_ARG_ID_MASTERADD
:
5514 ws
->l_state
.vertical_mwin
++;
5516 case SWM_ARG_ID_MASTERDEL
:
5517 if (ws
->l_state
.vertical_mwin
> 0)
5518 ws
->l_state
.vertical_mwin
--;
5520 case SWM_ARG_ID_STACKBALANCE
:
5521 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ (ws
->l_state
.vertical_stacks
+ 1);
5523 case SWM_ARG_ID_STACKINC
:
5524 ws
->l_state
.vertical_stacks
++;
5526 case SWM_ARG_ID_STACKDEC
:
5527 if (ws
->l_state
.vertical_stacks
> 1)
5528 ws
->l_state
.vertical_stacks
--;
5530 case SWM_ARG_ID_FLIPLAYOUT
:
5531 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
5539 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5541 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
5543 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
5547 horizontal_config(struct workspace
*ws
, int id
)
5549 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
5552 case SWM_ARG_ID_STACKINIT
:
5553 ws
->l_state
.horizontal_mwin
= 1;
5554 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
5555 ws
->l_state
.horizontal_stacks
= 1;
5557 case SWM_ARG_ID_MASTERSHRINK
:
5558 if (ws
->l_state
.horizontal_msize
> 1)
5559 ws
->l_state
.horizontal_msize
--;
5561 case SWM_ARG_ID_MASTERGROW
:
5562 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
5563 ws
->l_state
.horizontal_msize
++;
5565 case SWM_ARG_ID_MASTERADD
:
5566 ws
->l_state
.horizontal_mwin
++;
5568 case SWM_ARG_ID_MASTERDEL
:
5569 if (ws
->l_state
.horizontal_mwin
> 0)
5570 ws
->l_state
.horizontal_mwin
--;
5572 case SWM_ARG_ID_STACKBALANCE
:
5573 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ (ws
->l_state
.horizontal_stacks
+ 1);
5575 case SWM_ARG_ID_STACKINC
:
5576 ws
->l_state
.horizontal_stacks
++;
5578 case SWM_ARG_ID_STACKDEC
:
5579 if (ws
->l_state
.horizontal_stacks
> 1)
5580 ws
->l_state
.horizontal_stacks
--;
5582 case SWM_ARG_ID_FLIPLAYOUT
:
5583 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
5591 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5593 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
5595 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
5598 /* fullscreen view */
5600 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5602 struct swm_geometry gg
= *g
;
5603 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
, *tmpw
;
5605 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
5610 if (count_win(ws
, true) == 0)
5613 /* Figure out which top level window should be visible. */
5614 if (ws
->focus_pending
)
5615 win
= ws
->focus_pending
;
5618 else if (ws
->focus_prev
)
5619 win
= ws
->focus_prev
;
5621 win
= TAILQ_FIRST(&ws
->winlist
);
5623 DNPRINTF(SWM_D_STACK
, "max_stack: focus_pending: %#x, focus: %#x, "
5624 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws
->focus_pending
),
5625 WINID(ws
->focus
), WINID(ws
->focus_prev
),
5626 WINID(TAILQ_FIRST(&ws
->winlist
)), win
->id
);
5628 /* Update window geometry. */
5629 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5638 /* Set maximized flag for all maxed windows. */
5639 if (!MAXIMIZED(w
)) {
5640 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_MAXIMIZED
);
5641 ewmh_update_wm_state(w
);
5644 /* Only reconfigure if necessary. */
5645 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
5646 HEIGHT(w
) != gg
.h
) {
5649 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
)) {
5650 w
->bordered
= false;
5651 WIDTH(w
) += 2 * border_width
;
5652 HEIGHT(w
) += 2 * border_width
;
5655 X(w
) += border_width
;
5656 Y(w
) += border_width
;
5663 /* If transient, stack parent and its children. */
5664 if (TRANS(win
) && (parent
= find_window(win
->transient
))) {
5665 raise_window(parent
);
5667 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5668 if (w
->transient
== parent
->id
)
5672 /* Make sure focus window is on top. */
5675 /* Stack any children of focus window. */
5676 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5677 if (w
->transient
== win
->id
)
5680 /* Map all windows. */
5681 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
5687 max_config(struct workspace
*ws
, int id
)
5689 /* suppress unused warning since vars are needed */
5693 DNPRINTF(SWM_D_STACK
, "max_config: workspace: %d (noop)\n", ws
->idx
);
5697 send_to_rg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5699 int ridx
= args
->id
, i
, num_screens
;
5700 struct swm_region
*rr
= NULL
;
5703 num_screens
= get_screen_count();
5704 /* do nothing if we don't have more than one screen */
5705 if (!(num_screens
> 1 || outputs
> 1))
5708 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
5710 rr
= TAILQ_FIRST(&r
->s
->rl
);
5711 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
5712 rr
= TAILQ_NEXT(rr
, entry
);
5719 send_to_ws(bp
, r
, &a
);
5723 region_under(struct swm_screen
*s
, int x
, int y
)
5725 struct swm_region
*r
= NULL
;
5727 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
5728 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
5729 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
5730 WIDTH(r
), HEIGHT(r
), x
, y
);
5731 if (X(r
) <= x
&& x
< MAX_X(r
))
5732 if (Y(r
) <= y
&& y
< MAX_Y(r
))
5739 /* Transfer focused window to target workspace and focus. */
5741 send_to_ws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5743 int wsid
= args
->id
;
5744 struct ws_win
*win
= NULL
;
5748 if (r
&& r
->ws
&& r
->ws
->focus
)
5753 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win %#x, ws %d\n", win
->id
, wsid
);
5755 if (wsid
< 0 || wsid
>= workspace_limit
)
5758 if (win
->ws
->idx
== wsid
)
5761 win_to_ws(win
, wsid
, true);
5763 /* Set new focus on target ws. */
5764 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5765 win
->ws
->focus_prev
= win
->ws
->focus
;
5766 win
->ws
->focus
= win
;
5767 win
->ws
->focus_pending
= NULL
;
5769 if (win
->ws
->focus_prev
)
5770 draw_frame(win
->ws
->focus_prev
);
5773 DNPRINTF(SWM_D_STACK
, "send_to_ws: focus_pending: %#x, focus: %#x, "
5774 "focus_prev: %#x, first: %#x, win: %#x\n",
5775 WINID(r
->ws
->focus_pending
), WINID(r
->ws
->focus
),
5776 WINID(r
->ws
->focus_prev
), WINID(TAILQ_FIRST(&r
->ws
->winlist
)),
5779 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5780 ewmh_update_wm_state(win
);
5782 /* Restack focused region. */
5785 /* If destination ws has a region, restack. */
5788 load_float_geom(win
);
5793 /* Set new focus on current ws. */
5794 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5795 if (r
->ws
->focus
!= NULL
) {
5796 focus_win(r
->ws
->focus
);
5798 DNPRINTF(SWM_D_FOCUS
, "send_to_ws: set_input_focus: "
5799 "%#x, revert-to: parent, time: 0\n", r
->id
);
5800 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
5801 r
->id
, XCB_CURRENT_TIME
);
5810 /* Transfer focused window to region-relative workspace and focus. */
5812 send_to_rg_relative(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5815 struct swm_region
*r_other
;
5817 if (args
->id
== 1) {
5818 r_other
= TAILQ_NEXT(r
, entry
);
5819 if (r_other
== NULL
)
5820 r_other
= TAILQ_FIRST(&r
->s
->rl
);
5822 r_other
= TAILQ_PREV(r
, swm_region_list
, entry
);
5823 if (r_other
== NULL
)
5824 r_other
= TAILQ_LAST(&r
->s
->rl
, swm_region_list
);
5827 /* Map relative to absolute */
5829 args_abs
.id
= r_other
->ws
->idx
;
5831 send_to_ws(bp
, r
, &args_abs
);
5835 win_to_ws(struct ws_win
*win
, int wsid
, bool unfocus
)
5837 struct ws_win
*parent
;
5838 struct workspace
*ws
, *nws
, *pws
;
5840 if (wsid
< 0 || wsid
>= workspace_limit
)
5843 if (win
->ws
->idx
== wsid
)
5847 nws
= &win
->s
->ws
[wsid
];
5849 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win %#x, ws %d -> %d\n", win
->id
,
5852 /* Cleanup focus on source ws. */
5853 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
5854 (ws
->focus
== win
|| ws
->focus_pending
== win
))
5855 ws
->focus_pending
= get_focus_prev(win
);
5857 /* Move the parent if this is a transient window. */
5859 parent
= find_window(win
->transient
);
5862 /* Set new focus in parent's ws if needed. */
5863 if (pws
->focus
== parent
) {
5864 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5865 pws
->focus_pending
=
5866 get_focus_prev(parent
);
5868 unfocus_win(parent
);
5870 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5871 pws
->focus
= pws
->focus_pending
;
5872 pws
->focus_pending
= NULL
;
5876 /* Don't unmap parent if new ws is visible */
5878 unmap_window(parent
);
5881 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
5882 TAILQ_REMOVE(&ws
->stack
, parent
, stack_entry
);
5883 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
5884 TAILQ_INSERT_TAIL(&nws
->stack
, parent
, stack_entry
);
5887 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5888 "_NET_WM_DESKTOP: %d\n", wsid
);
5889 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5890 parent
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
5891 XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5898 if (ws
->focus_prev
== win
)
5899 ws
->focus_prev
= NULL
;
5901 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& ws
->focus_pending
!= NULL
) {
5902 ws
->focus
= ws
->focus_pending
;
5903 ws
->focus_pending
= NULL
;
5906 /* Don't unmap if new ws is visible */
5911 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
5912 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
5913 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
5914 TAILQ_INSERT_TAIL(&nws
->stack
, win
, stack_entry
);
5917 /* Update the window's workspace property: _NET_WM_DESKTOP */
5918 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5919 "_NET_WM_DESKTOP: %d\n", wsid
);
5920 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
5921 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5923 ewmh_update_client_list();
5925 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
5929 pressbutton(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5931 /* suppress unused warning since var is needed */
5935 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
5936 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5937 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
5938 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5942 raise_focus(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5947 /* Suppress warning. */
5951 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
5955 r
->ws
->focus_raise
= win
;
5958 /* Temporarily override stacking order also in the stack */
5959 if (!FLOATING(win
)) {
5960 val
= XCB_STACK_MODE_ABOVE
;
5961 xcb_configure_window(conn
, win
->frame
,
5962 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
5967 raise_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5969 /* Suppress warning. */
5973 if (r
== NULL
|| r
->ws
== NULL
)
5976 if (r
->ws
->focus
&& MAXIMIZED(r
->ws
->focus
))
5979 r
->ws
->always_raise
= !r
->ws
->always_raise
;
5981 /* Update focused win stacking order based on new always_raise value. */
5982 raise_window(r
->ws
->focus
);
5988 iconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5992 /* Suppress warning. */
5996 if ((w
= r
->ws
->focus
) == NULL
)
5999 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_HIDDEN
);
6000 ewmh_update_wm_state(w
);
6008 get_win_name(xcb_window_t win
)
6011 xcb_get_property_cookie_t c
;
6012 xcb_get_property_reply_t
*r
;
6014 /* First try _NET_WM_NAME for UTF-8. */
6015 c
= xcb_get_property(conn
, 0, win
, ewmh
[_NET_WM_NAME
].atom
,
6016 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
6017 r
= xcb_get_property_reply(conn
, c
, NULL
);
6018 if (r
&& r
->type
== XCB_NONE
) {
6020 /* Use WM_NAME instead; no UTF-8. */
6021 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
6022 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
6023 r
= xcb_get_property_reply(conn
, c
, NULL
);
6026 if (r
&& r
->type
!= XCB_NONE
&& r
->length
> 0)
6027 name
= strndup(xcb_get_property_value(r
),
6028 xcb_get_property_value_length(r
));
6033 err(1, "get_win_name: failed to allocate memory.");
6041 uniconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6050 DNPRINTF(SWM_D_MISC
, "uniconify\n");
6052 if (r
== NULL
|| r
->ws
== NULL
)
6055 /* make sure we have anything to uniconify */
6056 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
6057 if (win
->ws
== NULL
)
6058 continue; /* should never happen */
6064 DNPRINTF(SWM_D_MISC
, "uniconify: count: %d\n", count
);
6070 search_resp_action
= SWM_SEARCH_UNICONIFY
;
6072 spawn_select(r
, args
, "search", &searchpid
);
6074 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
6077 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
6078 if (win
->ws
== NULL
)
6079 continue; /* should never happen */
6083 name
= get_win_name(win
->id
);
6084 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
6092 name_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6098 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
6104 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
6106 spawn_select(r
, args
, "name_workspace", &searchpid
);
6108 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
6111 fprintf(lfile
, "%s", "");
6116 search_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6119 struct workspace
*ws
;
6124 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
6130 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
6132 spawn_select(r
, args
, "search", &searchpid
);
6134 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
6137 for (i
= 0; i
< workspace_limit
; i
++) {
6141 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
6142 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
6149 search_win_cleanup(void)
6151 struct search_window
*sw
= NULL
;
6153 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
6154 xcb_destroy_window(conn
, sw
->indicator
);
6155 TAILQ_REMOVE(&search_wl
, sw
, entry
);
6161 search_win(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6163 struct ws_win
*win
= NULL
;
6164 struct search_window
*sw
= NULL
;
6167 xcb_screen_t
*screen
;
6168 int i
, width
, height
;
6176 XRectangle l_ibox
, l_lbox
;
6180 DNPRINTF(SWM_D_MISC
, "search_win\n");
6183 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
6185 spawn_select(r
, args
, "search", &searchpid
);
6187 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
6190 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
6191 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
6193 TAILQ_INIT(&search_wl
);
6196 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
6200 sw
= calloc(1, sizeof(struct search_window
));
6202 warn("search_win: calloc");
6204 search_win_cleanup();
6210 snprintf(s
, sizeof s
, "%d", i
);
6213 w
= xcb_generate_id(conn
);
6214 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6215 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6216 wa
[2] = screen
->default_colormap
;
6218 if (bar_font_legacy
) {
6219 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
6220 width
= l_lbox
.width
+ 4;
6221 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
6223 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
6225 width
= info
.width
+ 4;
6226 height
= bar_font
->height
+ 4;
6229 xcb_create_window(conn
, screen
->root_depth
, w
, win
->frame
, 0, 0,
6230 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
6231 screen
->root_visual
, XCB_CW_BACK_PIXEL
|
6232 XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
, wa
);
6234 xcb_map_window(conn
, w
);
6237 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
6239 if (bar_font_legacy
) {
6240 l_gcv
.graphics_exposures
= 0;
6241 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
6243 XSetForeground(display
, l_draw
,
6244 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
6246 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
6247 (bar_fs_extents
->max_logical_extent
.height
-
6248 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
6250 XFreeGC(display
, l_draw
);
6253 draw
= XftDrawCreate(display
, w
,
6254 DefaultVisual(display
, r
->s
->idx
),
6255 DefaultColormap(display
, r
->s
->idx
));
6257 XftDrawStringUtf8(draw
, &search_font_color
, bar_font
, 2,
6258 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
6259 bar_font
->descent
, (FcChar8
*)s
, len
);
6261 XftDrawDestroy(draw
);
6264 DNPRINTF(SWM_D_MISC
, "search_win: mapped win %#x\n", w
);
6266 fprintf(lfile
, "%d\n", i
);
6276 search_resp_uniconify(const char *resp
, size_t len
)
6282 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
6284 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
6287 name
= get_win_name(win
->id
);
6288 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
6293 if (strncmp(s
, resp
, len
) == 0) {
6294 /* XXX this should be a callback to generalize */
6295 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_HIDDEN
);
6296 ewmh_update_wm_state(win
);
6306 search_resp_name_workspace(const char *resp
, size_t len
)
6308 struct workspace
*ws
;
6310 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
6312 if (search_r
->ws
== NULL
)
6317 free(search_r
->ws
->name
);
6318 search_r
->ws
->name
= NULL
;
6322 ws
->name
= strdup(resp
);
6323 if (ws
->name
== NULL
) {
6324 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
6325 "strdup: %s", strerror(errno
));
6329 ewmh_update_desktop_names();
6330 ewmh_get_desktop_names();
6335 ewmh_update_desktop_names(void)
6337 char *name_list
= NULL
, *p
;
6338 int num_screens
, i
, j
, len
= 0, tot
= 0;
6340 num_screens
= get_screen_count();
6341 for (i
= 0; i
< num_screens
; ++i
) {
6342 for (j
= 0; j
< workspace_limit
; ++j
) {
6343 if (screens
[i
].ws
[j
].name
!= NULL
)
6344 len
+= strlen(screens
[i
].ws
[j
].name
);
6348 if ((name_list
= calloc(len
, sizeof(char))) == NULL
)
6349 err(1, "update_desktop_names: calloc: failed to "
6350 "allocate memory.");
6353 for (j
= 0; j
< workspace_limit
; ++j
) {
6354 if (screens
[i
].ws
[j
].name
!= NULL
) {
6355 len
= strlen(screens
[i
].ws
[j
].name
);
6356 memcpy(p
, screens
[i
].ws
[j
].name
, len
);
6365 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6366 screens
[i
].root
, ewmh
[_NET_DESKTOP_NAMES
].atom
,
6367 a_utf8_string
, 8, tot
, name_list
);
6377 ewmh_get_desktop_names(void)
6380 xcb_get_property_cookie_t c
;
6381 xcb_get_property_reply_t
*r
;
6382 int num_screens
, i
, j
, n
, k
;
6384 num_screens
= get_screen_count();
6385 for (i
= 0; i
< num_screens
; ++i
) {
6386 for (j
= 0; j
< workspace_limit
; ++j
) {
6387 free(screens
[i
].ws
[j
].name
);
6388 screens
[i
].ws
[j
].name
= NULL
;
6391 c
= xcb_get_property(conn
, 0, screens
[i
].root
,
6392 ewmh
[_NET_DESKTOP_NAMES
].atom
,
6393 a_utf8_string
, 0, UINT32_MAX
);
6394 r
= xcb_get_property_reply(conn
, c
, NULL
);
6398 names
= xcb_get_property_value(r
);
6399 n
= xcb_get_property_value_length(r
);
6401 for (j
= 0, k
= 0; j
< n
; ++j
) {
6402 if (*(names
+ j
) != '\0') {
6403 screens
[i
].ws
[k
].name
= strdup(names
+ j
);
6404 j
+= strlen(names
+ j
);
6413 ewmh_update_client_list(void)
6416 int num_screens
, i
, j
, k
= 0, count
= 0;
6419 num_screens
= get_screen_count();
6420 for (i
= 0; i
< num_screens
; ++i
) {
6421 for (j
= 0; j
< workspace_limit
; ++j
)
6422 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6425 DNPRINTF(SWM_D_PROP
, "ewmh_update_client_list: win count: %d\n",
6431 wins
= calloc(count
, sizeof(xcb_window_t
));
6433 err(1, "ewmh_update_client_list: calloc: failed to "
6434 "allocate memory.");
6436 for (j
= 0, k
= 0; j
< workspace_limit
; ++j
)
6437 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6438 wins
[k
++] = win
->id
;
6440 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6441 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
6442 XCB_ATOM_WINDOW
, 32, count
, wins
);
6449 ewmh_update_current_desktop(void)
6453 num_screens
= get_screen_count();
6454 for (i
= 0; i
< num_screens
; ++i
) {
6455 if (screens
[i
].r_focus
)
6456 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6457 screens
[i
].root
, ewmh
[_NET_CURRENT_DESKTOP
].atom
,
6458 XCB_ATOM_CARDINAL
, 32, 1,
6459 &screens
[i
].r_focus
->ws
->idx
);
6464 ewmh_update_desktops(void)
6466 int num_screens
, i
, j
;
6469 vals
= calloc(workspace_limit
* 2, sizeof(uint32_t));
6471 err(1, "ewmh_update_desktops: calloc: failed to allocate "
6474 num_screens
= get_screen_count();
6475 for (i
= 0; i
< num_screens
; i
++) {
6476 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6477 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
6478 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
6480 for (j
= 0; j
< workspace_limit
; ++j
) {
6481 if (screens
[i
].ws
[j
].r
!= NULL
) {
6482 vals
[j
* 2] = X(screens
[i
].ws
[j
].r
);
6483 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].r
);
6484 } else if (screens
[i
].ws
[j
].old_r
!= NULL
) {
6485 vals
[j
* 2] = X(screens
[i
].ws
[j
].old_r
);
6486 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].old_r
);
6488 vals
[j
* 2] = vals
[j
* 2 + 1] = 0;
6492 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6493 screens
[i
].root
, ewmh
[_NET_DESKTOP_VIEWPORT
].atom
,
6494 XCB_ATOM_CARDINAL
, 32, workspace_limit
* 2, vals
);
6501 search_resp_search_workspace(const char *resp
)
6508 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
6512 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
6519 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
6521 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
6528 switchws(NULL
, search_r
, &a
);
6532 search_resp_search_window(const char *resp
)
6537 struct search_window
*sw
;
6539 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
6543 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
6548 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
6550 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
6557 TAILQ_FOREACH(sw
, &search_wl
, entry
)
6558 if (idx
== sw
->idx
) {
6564 #define MAX_RESP_LEN 1024
6567 search_do_resp(void)
6573 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
6578 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
6579 warn("search: calloc");
6583 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
6585 warn("search: read error");
6588 resp
[rbytes
] = '\0';
6591 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
6592 * newline, so work around that by sanitizing the resp now.
6594 resp
[strcspn(resp
, "\n")] = '\0';
6597 switch (search_resp_action
) {
6598 case SWM_SEARCH_UNICONIFY
:
6599 search_resp_uniconify(resp
, len
);
6601 case SWM_SEARCH_NAME_WORKSPACE
:
6602 search_resp_name_workspace(resp
, len
);
6604 case SWM_SEARCH_SEARCH_WORKSPACE
:
6605 search_resp_search_workspace(resp
);
6607 case SWM_SEARCH_SEARCH_WINDOW
:
6608 search_resp_search_window(resp
);
6613 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
6614 search_win_cleanup();
6616 search_resp_action
= SWM_SEARCH_NONE
;
6617 close(select_resp_pipe
[0]);
6624 wkill(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6628 DNPRINTF(SWM_D_MISC
, "wkill: win %#x, id: %d\n", WINID(r
->ws
->focus
),
6631 if (r
->ws
->focus
== NULL
)
6634 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
6635 xcb_kill_client(conn
, r
->ws
->focus
->id
);
6637 if (r
->ws
->focus
->can_delete
)
6638 client_msg(r
->ws
->focus
, a_delete
, 0);
6644 clear_maximized(struct workspace
*ws
)
6649 /* Clear any maximized win(s) on ws, from bottom up. */
6650 TAILQ_FOREACH_REVERSE(w
, &ws
->stack
, ws_win_stack
, stack_entry
)
6652 ewmh_apply_flags(w
, w
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
6653 ewmh_update_wm_state(w
);
6661 maximize_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6663 struct ws_win
*w
= r
->ws
->focus
;
6665 /* suppress unused warning since var is needed */
6672 DNPRINTF(SWM_D_MISC
, "maximize_toggle: win %#x\n", w
->id
);
6677 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6680 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_MAXIMIZED
);
6681 ewmh_update_wm_state(w
);
6685 if (w
== w
->ws
->focus
)
6690 DNPRINTF(SWM_D_MISC
, "maximize_toggle: done\n");
6694 floating_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6696 struct ws_win
*w
= r
->ws
->focus
;
6698 /* suppress unused warning since var is needed */
6705 DNPRINTF(SWM_D_MISC
, "floating_toggle: win %#x\n", w
->id
);
6707 if (FULLSCREEN(w
) || TRANS(w
))
6710 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6713 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_ABOVE
);
6714 ewmh_update_wm_state(w
);
6718 if (w
== w
->ws
->focus
)
6723 DNPRINTF(SWM_D_MISC
, "floating_toggle: done\n");
6727 fullscreen_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6729 struct ws_win
*w
= r
->ws
->focus
;
6731 /* suppress unused warning since var is needed */
6738 DNPRINTF(SWM_D_MISC
, "fullscreen_toggle: win %#x\n", w
->id
);
6740 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_FULLSCREEN
);
6741 ewmh_update_wm_state(w
);
6745 if (w
== w
->ws
->focus
)
6750 DNPRINTF(SWM_D_MISC
, "fullscreen_toggle: done\n");
6753 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
6755 struct swm_geometry g
= r
->g
;
6756 int rt
, lt
, tp
, bm
, bw
;
6758 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
6761 * Perpendicular distance of each side of the window to the respective
6762 * side of the region boundary. Positive values indicate the side of
6763 * the window has passed beyond the region boundary.
6765 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) - MAX_X(r
) : bw
;
6766 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) : bw
;
6767 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) - MAX_Y(r
) : bw
;
6768 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) : bw
;
6770 DNPRINTF(SWM_D_MISC
, "region_containment: win %#x, rt: %d, lt: %d, "
6771 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
6772 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
6773 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
6776 * Disable containment if any of the flagged sides went beyond the
6777 * containment boundary, or if containment is disabled.
6779 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
6780 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
6781 /* Make sure window has at least 1 pixel in the region */
6782 g
.x
+= 1 - WIDTH(win
);
6783 g
.y
+= 1 - HEIGHT(win
);
6784 g
.w
+= 2 * WIDTH(win
) - 2;
6785 g
.h
+= 2 * HEIGHT(win
) - 2;
6788 constrain_window(win
, &g
, &opts
);
6791 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6793 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
6795 DNPRINTF(SWM_D_MISC
, "constrain_window: win %#x, (x,y) w x h: "
6796 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6797 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
6798 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
6799 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
6800 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
6801 YESNO(*opts
& SWM_CW_RESIZABLE
));
6803 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) > b
->x
+ b
->w
) {
6804 if (*opts
& SWM_CW_RESIZABLE
)
6805 WIDTH(win
) = b
->x
+ b
->w
- X(win
);
6807 X(win
) = b
->x
+ b
->w
- WIDTH(win
);
6810 if ((*opts
& SWM_CW_LEFT
) && X(win
) < b
->x
) {
6811 if (*opts
& SWM_CW_RESIZABLE
)
6812 WIDTH(win
) -= b
->x
- X(win
);
6817 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) > b
->y
+ b
->h
) {
6818 if (*opts
& SWM_CW_RESIZABLE
)
6819 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
);
6821 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
);
6824 if ((*opts
& SWM_CW_TOP
) && Y(win
) < b
->y
) {
6825 if (*opts
& SWM_CW_RESIZABLE
)
6826 HEIGHT(win
) -= b
->y
- Y(win
);
6831 if (*opts
& SWM_CW_RESIZABLE
) {
6834 if (HEIGHT(win
) < 1)
6840 draw_frame(struct ws_win
*win
)
6842 xcb_rectangle_t rect
[4];
6846 if (win
->frame
== XCB_WINDOW_NONE
) {
6847 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x not "
6848 "reparented.\n", win
->id
);
6852 if (!win
->bordered
) {
6853 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x frame "
6854 "disabled\n", win
->id
);
6857 if (WS_FOCUSED(win
->ws
) && win
->ws
->focus
== win
)
6858 pixel
= MAXIMIZED(win
) ?
6859 &win
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].pixel
:
6860 &win
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6862 pixel
= MAXIMIZED(win
) ?
6863 &win
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].pixel
:
6864 &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6866 /* Top (with corners) */
6869 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6870 rect
[n
].height
= border_width
;
6871 /* Left (without corners) */
6873 rect
[n
].y
= border_width
;
6874 rect
[n
].width
= border_width
;
6875 rect
[n
].height
= HEIGHT(win
);
6876 /* Right (without corners) */
6877 rect
[++n
].x
= border_width
+ WIDTH(win
);
6878 rect
[n
].y
= border_width
;
6879 rect
[n
].width
= border_width
;
6880 rect
[n
].height
= HEIGHT(win
);
6881 /* Bottom (with corners)*/
6883 rect
[n
].y
= border_width
+ HEIGHT(win
);
6884 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6885 rect
[n
].height
= border_width
;
6887 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, pixel
);
6888 xcb_poly_fill_rectangle(conn
, win
->frame
, win
->s
->bar_gc
, 4, rect
);
6892 update_window(struct ws_win
*win
)
6897 if (win
->frame
== XCB_WINDOW_NONE
) {
6898 DNPRINTF(SWM_D_EVENT
, "update_window: skip win %#x; "
6899 "not reparented.\n", win
->id
);
6903 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
6904 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
6905 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6907 /* Reconfigure frame. */
6908 if (win
->bordered
) {
6909 wc
[0] = X(win
) - border_width
;
6910 wc
[1] = Y(win
) - border_width
;
6911 wc
[2] = WIDTH(win
) + 2 * border_width
;
6912 wc
[3] = HEIGHT(win
) + 2 * border_width
;
6917 wc
[3] = HEIGHT(win
);
6922 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x frame %#x, (x,y) w x h: "
6923 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, win
->frame
, wc
[0],
6924 wc
[1], wc
[2], wc
[3], YESNO(win
->bordered
));
6926 xcb_configure_window(conn
, win
->frame
, mask
, wc
);
6928 /* Reconfigure client window. */
6929 wc
[0] = wc
[1] = win
->bordered
? border_width
: 0;
6931 wc
[3] = HEIGHT(win
);
6933 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x, (x,y) w x h: "
6934 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
6935 wc
[3], YESNO(win
->bordered
));
6936 xcb_configure_window(conn
, win
->id
, mask
, wc
);
6938 /* ICCCM 4.2.3 Synthetic ConfigureNotify when moved and not resized. */
6939 if ((X(win
) != win
->g_prev
.x
|| Y(win
) != win
->g_prev
.y
) &&
6940 (win
->java
|| (WIDTH(win
) == win
->g_prev
.w
&&
6941 HEIGHT(win
) == win
->g_prev
.h
))) {
6942 /* Java has special needs when moved together with a resize. */
6943 config_win(win
, NULL
);
6946 win
->g_prev
= win
->g
;
6950 SIMPLEQ_ENTRY(event
) entry
;
6951 xcb_generic_event_t
*ev
;
6953 SIMPLEQ_HEAD(event_queue
, event
) events
= SIMPLEQ_HEAD_INITIALIZER(events
);
6955 xcb_generic_event_t
*
6956 get_next_event(bool dowait
)
6959 xcb_generic_event_t
*evt
;
6961 /* Try queue first. */
6962 if ((ep
= SIMPLEQ_FIRST(&events
))) {
6964 SIMPLEQ_REMOVE_HEAD(&events
, entry
);
6967 evt
= xcb_wait_for_event(conn
);
6969 evt
= xcb_poll_for_event(conn
);
6975 put_back_event(xcb_generic_event_t
*evt
)
6978 if ((ep
= malloc(sizeof (struct event
))) == NULL
)
6979 err(1, "put_back_event: failed to allocate memory.");
6981 SIMPLEQ_INSERT_HEAD(&events
, ep
, entry
);
6984 /* Peeks at next event to detect auto-repeat. */
6986 keyrepeating(xcb_key_release_event_t
*kre
)
6988 xcb_generic_event_t
*evt
;
6990 /* Ensure repeating keypress is finished processing. */
6993 if ((evt
= get_next_event(false))) {
6994 put_back_event(evt
);
6996 if (XCB_EVENT_RESPONSE_TYPE(evt
) == XCB_KEY_PRESS
&&
6997 kre
->sequence
== evt
->sequence
&&
6998 kre
->detail
== ((xcb_key_press_event_t
*)evt
)->detail
)
7006 keybindreleased(struct binding
*bp
, xcb_key_release_event_t
*kre
)
7008 if (bp
->type
== KEYBIND
&& !keyrepeating(kre
) &&
7009 bp
->value
== xcb_key_press_lookup_keysym(syms
, kre
, 0))
7015 #define SWM_RESIZE_STEPS (50)
7018 resize_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
7020 xcb_timestamp_t timestamp
= 0;
7021 struct swm_region
*r
= NULL
;
7022 struct swm_geometry g
;
7023 int top
= 0, left
= 0;
7025 xcb_cursor_t cursor
;
7026 xcb_query_pointer_reply_t
*xpr
= NULL
;
7027 xcb_generic_event_t
*evt
;
7028 xcb_motion_notify_event_t
*mne
;
7029 bool resizing
, step
= false;
7035 if (FULLSCREEN(win
))
7038 /* In max_stack mode, should only resize transients. */
7039 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
7042 DNPRINTF(SWM_D_EVENT
, "resize: win %#x, floating: %s, "
7043 "transient: %#x\n", win
->id
, YESNO(ABOVE(win
)),
7047 store_float_geom(win
);
7048 else if (!(TRANS(win
) || ABOVE(win
)))
7051 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
7053 ewmh_update_wm_state(win
);
7059 /* It's possible for win to have been freed during focus_flush(). */
7060 if (validate_win(win
)) {
7061 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
7066 case SWM_ARG_ID_WIDTHSHRINK
:
7067 WIDTH(win
) -= SWM_RESIZE_STEPS
;
7070 case SWM_ARG_ID_WIDTHGROW
:
7071 WIDTH(win
) += SWM_RESIZE_STEPS
;
7074 case SWM_ARG_ID_HEIGHTSHRINK
:
7075 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
7078 case SWM_ARG_ID_HEIGHTGROW
:
7079 HEIGHT(win
) += SWM_RESIZE_STEPS
;
7086 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
7087 SWM_CW_HARDBOUNDARY
);
7089 store_float_geom(win
);
7093 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
7094 SWM_CW_SOFTBOUNDARY
);
7097 /* get cursor offset from window root */
7098 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
7105 if (xpr
->win_x
< WIDTH(win
) / 2)
7108 if (xpr
->win_y
< HEIGHT(win
) / 2)
7111 if (opt
== SWM_ARG_ID_CENTER
)
7112 cursor
= cursors
[XC_SIZING
].cid
;
7114 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
7115 XC_TOP_RIGHT_CORNER
].cid
;
7117 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
7118 XC_BOTTOM_RIGHT_CORNER
].cid
;
7120 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
7121 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
7124 /* Release keyboard freeze if called via keybind. */
7125 if (bp
->type
== KEYBIND
)
7126 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7131 while (resizing
&& (evt
= get_next_event(true))) {
7132 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
7133 case XCB_BUTTON_RELEASE
:
7134 if (bp
->type
== BTNBIND
&& bp
->value
==
7135 ((xcb_button_release_event_t
*)evt
)->detail
)
7138 case XCB_KEY_RELEASE
:
7139 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
7142 case XCB_MOTION_NOTIFY
:
7143 mne
= (xcb_motion_notify_event_t
*)evt
;
7144 DNPRINTF(SWM_D_EVENT
, "resize: MOTION_NOTIFY: "
7145 "root: %#x\n", mne
->root
);
7147 /* cursor offset/delta from start of the operation */
7148 dx
= mne
->root_x
- xpr
->root_x
;
7149 dy
= mne
->root_y
- xpr
->root_y
;
7155 if (opt
== SWM_ARG_ID_CENTER
) {
7156 if (g
.h
/ 2 + dy
< 1)
7160 HEIGHT(win
) = g
.h
+ 2 * dy
;
7168 HEIGHT(win
) = g
.h
+ dy
;
7175 if (opt
== SWM_ARG_ID_CENTER
) {
7176 if (g
.w
/ 2 + dx
< 1)
7180 WIDTH(win
) = g
.w
+ 2 * dx
;
7188 WIDTH(win
) = g
.w
+ dx
;
7191 /* not free, don't sync more than 120 times / second */
7192 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
7193 timestamp
= mne
->time
;
7194 regionize(win
, mne
->root_x
, mne
->root_y
);
7195 region_containment(win
, r
, SWM_CW_ALLSIDES
|
7196 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
7197 SWM_CW_SOFTBOUNDARY
);
7202 case XCB_BUTTON_PRESS
:
7204 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_PRESS ignored\n");
7205 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7206 ((xcb_button_press_event_t
*)evt
)->time
);
7211 DNPRINTF(SWM_D_EVENT
, "resize: KEY_PRESS ignored\n");
7212 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7213 ((xcb_key_press_event_t
*)evt
)->time
);
7219 /* It's possible for win to have been freed above. */
7220 if (validate_win(win
)) {
7221 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
7229 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
7230 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
7234 store_float_geom(win
);
7236 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7238 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
7242 resize(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
7244 struct ws_win
*win
= NULL
;
7245 xcb_query_pointer_reply_t
*qpr
= NULL
;
7250 if (args
->id
!= SWM_ARG_ID_DONTCENTER
&&
7251 args
->id
!= SWM_ARG_ID_CENTER
) {
7252 /* {height,width}_{grow,shrink} use the focus window. */
7256 /* resize uses window under pointer. */
7257 qpr
= xcb_query_pointer_reply(conn
,
7258 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7260 win
= find_window(qpr
->child
);
7266 resize_win(win
, bp
, args
->id
);
7268 if (args
->id
&& bp
->type
== KEYBIND
)
7274 /* Try to set window region based on supplied coordinates or window center. */
7276 regionize(struct ws_win
*win
, int x
, int y
)
7278 struct swm_region
*r
= NULL
;
7280 r
= region_under(win
->s
, x
, y
);
7282 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
7283 Y(win
) + HEIGHT(win
) / 2);
7285 if (r
&& r
!= win
->ws
->r
) {
7286 clear_maximized(r
->ws
);
7288 win_to_ws(win
, r
->ws
->idx
, false);
7290 /* Stack old and new region. */
7294 /* Set focus on new ws. */
7295 unfocus_win(r
->ws
->focus
);
7303 #define SWM_MOVE_STEPS (50)
7306 move_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
7308 struct swm_region
*r
;
7309 xcb_timestamp_t timestamp
= 0;
7310 xcb_query_pointer_reply_t
*qpr
= NULL
;
7311 xcb_generic_event_t
*evt
;
7312 xcb_motion_notify_event_t
*mne
;
7313 bool moving
, restack
= false, step
= false;
7318 if ((r
= win
->ws
->r
) == NULL
)
7321 if (FULLSCREEN(win
))
7324 DNPRINTF(SWM_D_EVENT
, "move: win %#x, floating: %s, transient: "
7325 "%#x\n", win
->id
, YESNO(ABOVE(win
)), win
->transient
);
7327 /* in max_stack mode should only move transients */
7328 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
7331 if (!(ABOVE(win
) || TRANS(win
)) || MAXIMIZED(win
)) {
7332 store_float_geom(win
);
7336 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
7338 ewmh_update_wm_state(win
);
7345 /* It's possible for win to have been freed during focus_flush(). */
7346 if (validate_win(win
)) {
7347 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7352 case SWM_ARG_ID_MOVELEFT
:
7353 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
7356 case SWM_ARG_ID_MOVERIGHT
:
7357 X(win
) += (SWM_MOVE_STEPS
- border_width
);
7360 case SWM_ARG_ID_MOVEUP
:
7361 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
7364 case SWM_ARG_ID_MOVEDOWN
:
7365 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
7372 regionize(win
, -1, -1);
7373 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
7375 store_float_geom(win
);
7379 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
7380 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
7381 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
7383 /* get cursor offset from window root */
7384 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
7387 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7391 /* Release keyboard freeze if called via keybind. */
7392 if (bp
->type
== KEYBIND
)
7393 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7396 regionize(win
, qpr
->root_x
, qpr
->root_y
);
7397 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7398 SWM_CW_SOFTBOUNDARY
);
7402 while (moving
&& (evt
= get_next_event(true))) {
7403 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
7404 case XCB_BUTTON_RELEASE
:
7405 if (bp
->type
== BTNBIND
&& bp
->value
==
7406 ((xcb_button_release_event_t
*)evt
)->detail
)
7409 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7410 ((xcb_button_release_event_t
*)evt
)->time
);
7413 case XCB_KEY_RELEASE
:
7414 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
7417 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7418 ((xcb_key_release_event_t
*)evt
)->time
);
7421 case XCB_MOTION_NOTIFY
:
7422 mne
= (xcb_motion_notify_event_t
*)evt
;
7423 DNPRINTF(SWM_D_EVENT
, "move: MOTION_NOTIFY: "
7424 "root: %#x\n", mne
->root
);
7425 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
7426 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
7428 /* not free, don't sync more than 120 times / second */
7429 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
7430 timestamp
= mne
->time
;
7431 regionize(win
, mne
->root_x
, mne
->root_y
);
7432 region_containment(win
, win
->ws
->r
,
7433 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
7438 case XCB_BUTTON_PRESS
:
7439 /* Thaw and ignore. */
7440 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7441 ((xcb_button_press_event_t
*)evt
)->time
);
7446 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7447 ((xcb_key_press_event_t
*)evt
)->time
);
7453 /* It's possible for win to have been freed. */
7454 if (validate_win(win
)) {
7455 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7463 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7464 SWM_CW_SOFTBOUNDARY
);
7468 store_float_geom(win
);
7470 /* New region set to fullscreen layout. */
7471 if (win
->ws
->r
&& win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
]) {
7478 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7479 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
7483 move(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
7485 struct ws_win
*win
= NULL
;
7486 xcb_query_pointer_reply_t
*qpr
= NULL
;
7492 /* move_* uses focus window. */
7496 /* Disallow move_ on tiled. */
7497 if (win
&& !(TRANS(win
) || ABOVE(win
)))
7500 /* move uses window under pointer. */
7501 qpr
= xcb_query_pointer_reply(conn
,
7502 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7504 win
= find_window(qpr
->child
);
7510 move_win(win
, bp
, args
->id
);
7512 if (args
->id
&& bp
->type
== KEYBIND
)
7518 /* action definitions */
7520 char name
[SWM_FUNCNAME_LEN
];
7521 void (*func
)(struct binding
*, struct swm_region
*,
7525 } actions
[FN_INVALID
+ 2] = {
7526 /* name function argument */
7527 { "bar_toggle", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
7528 { "bar_toggle_ws", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
7529 { "button2", pressbutton
, 0, {.id
= 2} },
7530 { "cycle_layout", cycle_layout
, 0, {0} },
7531 { "flip_layout", stack_config
, 0, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
7532 { "float_toggle", floating_toggle
,0, {0} },
7533 { "focus", focus_pointer
, 0, {0} },
7534 { "focus_main", focus
, 0, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
7535 { "focus_next", focus
, 0, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
7536 { "focus_prev", focus
, 0, {.id
= SWM_ARG_ID_FOCUSPREV
} },
7537 { "focus_urgent", focus
, 0, {.id
= SWM_ARG_ID_FOCUSURGENT
} },
7538 { "fullscreen_toggle", fullscreen_toggle
, 0, {0} },
7539 { "maximize_toggle", maximize_toggle
,0, {0} },
7540 { "height_grow", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
7541 { "height_shrink", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
7542 { "iconify", iconify
, 0, {0} },
7543 { "master_shrink", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
7544 { "master_grow", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERGROW
} },
7545 { "master_add", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERADD
} },
7546 { "master_del", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERDEL
} },
7547 { "move", move
, FN_F_NOREPLAY
, {0} },
7548 { "move_down", move
, 0, {.id
= SWM_ARG_ID_MOVEDOWN
} },
7549 { "move_left", move
, 0, {.id
= SWM_ARG_ID_MOVELEFT
} },
7550 { "move_right", move
, 0, {.id
= SWM_ARG_ID_MOVERIGHT
} },
7551 { "move_up", move
, 0, {.id
= SWM_ARG_ID_MOVEUP
} },
7552 { "mvrg_1", send_to_rg
, 0, {.id
= 0} },
7553 { "mvrg_2", send_to_rg
, 0, {.id
= 1} },
7554 { "mvrg_3", send_to_rg
, 0, {.id
= 2} },
7555 { "mvrg_4", send_to_rg
, 0, {.id
= 3} },
7556 { "mvrg_5", send_to_rg
, 0, {.id
= 4} },
7557 { "mvrg_6", send_to_rg
, 0, {.id
= 5} },
7558 { "mvrg_7", send_to_rg
, 0, {.id
= 6} },
7559 { "mvrg_8", send_to_rg
, 0, {.id
= 7} },
7560 { "mvrg_9", send_to_rg
, 0, {.id
= 8} },
7561 { "mvrg_next", send_to_rg_relative
, 0, {.id
= 1} },
7562 { "mvrg_prev", send_to_rg_relative
, 0, {.id
= -1} },
7563 { "mvws_1", send_to_ws
, 0, {.id
= 0} },
7564 { "mvws_2", send_to_ws
, 0, {.id
= 1} },
7565 { "mvws_3", send_to_ws
, 0, {.id
= 2} },
7566 { "mvws_4", send_to_ws
, 0, {.id
= 3} },
7567 { "mvws_5", send_to_ws
, 0, {.id
= 4} },
7568 { "mvws_6", send_to_ws
, 0, {.id
= 5} },
7569 { "mvws_7", send_to_ws
, 0, {.id
= 6} },
7570 { "mvws_8", send_to_ws
, 0, {.id
= 7} },
7571 { "mvws_9", send_to_ws
, 0, {.id
= 8} },
7572 { "mvws_10", send_to_ws
, 0, {.id
= 9} },
7573 { "mvws_11", send_to_ws
, 0, {.id
= 10} },
7574 { "mvws_12", send_to_ws
, 0, {.id
= 11} },
7575 { "mvws_13", send_to_ws
, 0, {.id
= 12} },
7576 { "mvws_14", send_to_ws
, 0, {.id
= 13} },
7577 { "mvws_15", send_to_ws
, 0, {.id
= 14} },
7578 { "mvws_16", send_to_ws
, 0, {.id
= 15} },
7579 { "mvws_17", send_to_ws
, 0, {.id
= 16} },
7580 { "mvws_18", send_to_ws
, 0, {.id
= 17} },
7581 { "mvws_19", send_to_ws
, 0, {.id
= 18} },
7582 { "mvws_20", send_to_ws
, 0, {.id
= 19} },
7583 { "mvws_21", send_to_ws
, 0, {.id
= 20} },
7584 { "mvws_22", send_to_ws
, 0, {.id
= 21} },
7585 { "name_workspace", name_workspace
, 0, {0} },
7586 { "quit", quit
, 0, {0} },
7587 { "raise", raise_focus
, 0, {0} },
7588 { "raise_toggle", raise_toggle
, 0, {0} },
7589 { "resize", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_DONTCENTER
} },
7590 { "resize_centered", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_CENTER
} },
7591 { "restart", restart
, 0, {0} },
7592 { "rg_1", focusrg
, 0, {.id
= 0} },
7593 { "rg_2", focusrg
, 0, {.id
= 1} },
7594 { "rg_3", focusrg
, 0, {.id
= 2} },
7595 { "rg_4", focusrg
, 0, {.id
= 3} },
7596 { "rg_5", focusrg
, 0, {.id
= 4} },
7597 { "rg_6", focusrg
, 0, {.id
= 5} },
7598 { "rg_7", focusrg
, 0, {.id
= 6} },
7599 { "rg_8", focusrg
, 0, {.id
= 7} },
7600 { "rg_9", focusrg
, 0, {.id
= 8} },
7601 { "rg_move_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_UP
} },
7602 { "rg_move_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_DOWN
} },
7603 { "rg_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7604 { "rg_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7605 { "screen_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7606 { "screen_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7607 { "search_win", search_win
, 0, {0} },
7608 { "search_workspace", search_workspace
, 0, {0} },
7609 { "spawn_custom", NULL
, 0, {0} },
7610 { "stack_balance", stack_config
, 0, {.id
= SWM_ARG_ID_STACKBALANCE
} },
7611 { "stack_inc", stack_config
, 0, {.id
= SWM_ARG_ID_STACKINC
} },
7612 { "stack_dec", stack_config
, 0, {.id
= SWM_ARG_ID_STACKDEC
} },
7613 { "stack_reset", stack_config
, 0, {.id
= SWM_ARG_ID_STACKINIT
} },
7614 { "swap_main", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPMAIN
} },
7615 { "swap_next", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPNEXT
} },
7616 { "swap_prev", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPPREV
} },
7617 { "uniconify", uniconify
, 0, {0} },
7618 { "version", version
, 0, {0} },
7619 { "width_grow", resize
, 0, {.id
= SWM_ARG_ID_WIDTHGROW
} },
7620 { "width_shrink", resize
, 0, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
7621 { "wind_del", wkill
, 0, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
7622 { "wind_kill", wkill
, 0, {.id
= SWM_ARG_ID_KILLWINDOW
} },
7623 { "ws_1", switchws
, 0, {.id
= 0} },
7624 { "ws_2", switchws
, 0, {.id
= 1} },
7625 { "ws_3", switchws
, 0, {.id
= 2} },
7626 { "ws_4", switchws
, 0, {.id
= 3} },
7627 { "ws_5", switchws
, 0, {.id
= 4} },
7628 { "ws_6", switchws
, 0, {.id
= 5} },
7629 { "ws_7", switchws
, 0, {.id
= 6} },
7630 { "ws_8", switchws
, 0, {.id
= 7} },
7631 { "ws_9", switchws
, 0, {.id
= 8} },
7632 { "ws_10", switchws
, 0, {.id
= 9} },
7633 { "ws_11", switchws
, 0, {.id
= 10} },
7634 { "ws_12", switchws
, 0, {.id
= 11} },
7635 { "ws_13", switchws
, 0, {.id
= 12} },
7636 { "ws_14", switchws
, 0, {.id
= 13} },
7637 { "ws_15", switchws
, 0, {.id
= 14} },
7638 { "ws_16", switchws
, 0, {.id
= 15} },
7639 { "ws_17", switchws
, 0, {.id
= 16} },
7640 { "ws_18", switchws
, 0, {.id
= 17} },
7641 { "ws_19", switchws
, 0, {.id
= 18} },
7642 { "ws_20", switchws
, 0, {.id
= 19} },
7643 { "ws_21", switchws
, 0, {.id
= 20} },
7644 { "ws_22", switchws
, 0, {.id
= 21} },
7645 { "ws_next", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
7646 { "ws_next_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
7647 { "ws_next_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
7648 { "ws_prev", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
7649 { "ws_prev_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
7650 { "ws_prev_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
7651 { "ws_prior", priorws
, 0, {0} },
7652 /* SWM_DEBUG actions MUST be here: */
7653 { "debug_toggle", debug_toggle
, 0, {0} },
7654 { "dumpwins", dumpwins
, 0, {0} },
7656 { "invalid action", NULL
, 0, {0} },
7660 update_modkey(uint16_t mod
)
7664 /* Replace all instances of the old mod key. */
7665 RB_FOREACH(bp
, binding_tree
, &bindings
)
7666 if (bp
->mod
& mod_key
)
7667 bp
->mod
= (bp
->mod
& ~mod_key
) | mod
;
7672 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7675 struct spawn_prog
*prog
= NULL
;
7677 char *ap
, **real_args
;
7679 /* suppress unused warning since var is needed */
7682 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
7685 TAILQ_FOREACH(prog
, &spawns
, entry
) {
7686 if (strcasecmp(spawn_name
, prog
->name
) == 0)
7690 warnx("spawn_custom: program %s not found", spawn_name
);
7694 /* make room for expanded args */
7695 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
7696 err(1, "spawn_custom: calloc real_args");
7698 /* expand spawn_args into real_args */
7699 for (i
= c
= 0; i
< prog
->argc
; i
++) {
7701 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
7702 if (strcasecmp(ap
, "$bar_border") == 0) {
7704 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
7706 err(1, "spawn_custom border color");
7707 } else if (strcasecmp(ap
, "$bar_color") == 0) {
7709 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
7711 err(1, "spawn_custom bar color");
7712 } else if (strcasecmp(ap
, "$bar_font") == 0) {
7713 if ((real_args
[c
] = strdup(bar_fonts
))
7715 err(1, "spawn_custom bar fonts");
7716 } else if (strcasecmp(ap
, "$bar_font_color") == 0) {
7718 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
7720 err(1, "spawn_custom color font");
7721 } else if (strcasecmp(ap
, "$color_focus") == 0) {
7723 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
7725 err(1, "spawn_custom color focus");
7726 } else if (strcasecmp(ap
, "$color_focus_maximized") == 0) {
7728 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].name
))
7730 err(1, "spawn_custom color focus maximized");
7731 } else if (strcasecmp(ap
, "$color_unfocus") == 0) {
7733 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
7735 err(1, "spawn_custom color unfocus");
7736 } else if (strcasecmp(ap
, "$color_unfocus_maximized") == 0) {
7738 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].name
))
7740 err(1, "spawn_custom color unfocus maximized");
7741 } else if (strcasecmp(ap
, "$region_index") == 0) {
7742 if (asprintf(&real_args
[c
], "%d",
7743 get_region_index(r
) + 1) < 1)
7744 err(1, "spawn_custom region index");
7745 } else if (strcasecmp(ap
, "$workspace_index") == 0) {
7746 if (asprintf(&real_args
[c
], "%d", r
->ws
->idx
+ 1) < 1)
7747 err(1, "spawn_custom workspace index");
7748 } else if (strcasecmp(ap
, "$dmenu_bottom") == 0) {
7751 if ((real_args
[c
] = strdup("-b")) == NULL
)
7752 err(1, "spawn_custom workspace index");
7754 /* no match --> copy as is */
7755 if ((real_args
[c
] = strdup(ap
)) == NULL
)
7756 err(1, "spawn_custom strdup(ap)");
7758 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
7764 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
7765 for (i
= 0; i
< c
; ++i
)
7766 DPRINTF("\"%s\" ", real_args
[i
]);
7769 *ret_args
= real_args
;
7774 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
7780 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7784 spawn(r
->ws
->idx
, &a
, true);
7786 for (i
= 0; i
< spawn_argc
; i
++)
7792 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7799 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7803 if (pipe(select_list_pipe
) == -1)
7804 err(1, "pipe error");
7805 if (pipe(select_resp_pipe
) == -1)
7806 err(1, "pipe error");
7808 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
7809 err(1, "could not disable SIGPIPE");
7810 switch (*pid
= fork()) {
7812 err(1, "cannot fork");
7815 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
7817 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
7819 close(select_list_pipe
[1]);
7820 close(select_resp_pipe
[0]);
7821 spawn(r
->ws
->idx
, &a
, false);
7823 default: /* parent */
7824 close(select_list_pipe
[0]);
7825 close(select_resp_pipe
[1]);
7829 for (i
= 0; i
< spawn_argc
; i
++)
7834 /* Argument tokenizer. */
7837 char *arg
, *cp
, *next
;
7838 bool single_quoted
= false, double_quoted
= false;
7843 /* Eat and move characters until end of argument is found. */
7844 for (arg
= next
= cp
= *sp
; *cp
!= '\0'; ++cp
) {
7845 if (!double_quoted
&& *cp
== '\'') {
7846 /* Eat single-quote. */
7847 single_quoted
= !single_quoted
;
7848 } else if (!single_quoted
&& *cp
== '"') {
7849 /* Eat double-quote. */
7850 double_quoted
= !double_quoted
;
7851 } else if (!single_quoted
&& *cp
== '\\' && *(cp
+ 1) == '"') {
7852 /* Eat backslash; copy escaped character to arg. */
7854 } else if (!single_quoted
&& !double_quoted
&& *cp
== '\\' &&
7855 (*(cp
+ 1) == '\'' || *(cp
+ 1) == ' ')) {
7856 /* Eat backslash; move escaped character. */
7858 } else if (!single_quoted
&& !double_quoted
&&
7859 (*cp
== ' ' || *cp
== '\t')) {
7860 /* Terminate argument. */
7862 /* Point sp to beginning of next argument. */
7866 /* Move regular character. */
7871 /* Terminate argument if end of string. */
7881 spawn_insert(const char *name
, const char *args
, int flags
)
7883 struct spawn_prog
*sp
;
7884 char *arg
, *cp
, *ptr
;
7886 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s[%s]\n", name
, args
);
7888 if (args
== NULL
|| *args
== '\0')
7891 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
7892 err(1, "spawn_insert: calloc");
7893 if ((sp
->name
= strdup(name
)) == NULL
)
7894 err(1, "spawn_insert: strdup");
7896 /* Convert the arguments to an argument list. */
7897 if ((ptr
= cp
= strdup(args
)) == NULL
)
7898 err(1, "spawn_insert: strdup");
7899 while ((arg
= argsep(&ptr
)) != NULL
) {
7900 /* Null argument; skip it. */
7905 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
7906 sizeof *sp
->argv
)) == NULL
)
7907 err(1, "spawn_insert: realloc");
7908 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
7909 err(1, "spawn_insert: strdup");
7915 DNPRINTF(SWM_D_SPAWN
, "arg %d: [%s]\n", sp
->argc
, sp
->argv
[sp
->argc
-1]);
7916 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
7917 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
7921 spawn_remove(struct spawn_prog
*sp
)
7925 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
7927 TAILQ_REMOVE(&spawns
, sp
, entry
);
7928 for (i
= 0; i
< sp
->argc
; i
++)
7934 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
7940 struct spawn_prog
*sp
;
7942 while ((sp
= TAILQ_FIRST(&spawns
)) != NULL
) {
7948 spawn_find(const char *name
)
7950 struct spawn_prog
*sp
;
7952 TAILQ_FOREACH(sp
, &spawns
, entry
)
7953 if (strcasecmp(sp
->name
, name
) == 0)
7960 setspawn(const char *name
, const char *args
, int flags
)
7962 struct spawn_prog
*sp
;
7964 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
7969 /* Remove any old spawn under the same name. */
7970 if ((sp
= spawn_find(name
)) != NULL
)
7974 spawn_insert(name
, args
, flags
);
7976 warnx("error: setspawn: cannot find program: %s", name
);
7978 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
7982 setconfspawn(const char *selector
, const char *value
, int flags
)
7986 if (selector
== NULL
|| strlen(selector
) == 0)
7989 args
= expand_tilde(value
);
7991 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
7993 setspawn(selector
, args
, flags
);
7996 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
8001 validate_spawns(void)
8004 struct spawn_prog
*sp
;
8005 char which
[PATH_MAX
];
8008 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8009 if (bp
->action
!= FN_SPAWN_CUSTOM
)
8013 sp
= spawn_find(bp
->spawn_name
);
8014 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
8017 /* verify we have the goods */
8018 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
8019 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
8021 for (i
= strlen("which "); i
< strlen(which
); i
++)
8022 if (which
[i
] == ' ') {
8026 if (system(which
) != 0)
8027 add_startup_exception("could not find %s",
8028 &which
[strlen("which ")]);
8035 setconfspawn("lock", "xlock", 0);
8037 setconfspawn("term", "xterm", 0);
8038 setconfspawn("spawn_term", "xterm", 0);
8040 setconfspawn("menu", "dmenu_run"
8044 " -nf $bar_font_color"
8046 " -sf $bar_color", 0);
8048 setconfspawn("search", "dmenu"
8053 " -nf $bar_font_color"
8055 " -sf $bar_color", 0);
8057 setconfspawn("name_workspace", "dmenu"
8062 " -nf $bar_font_color"
8064 " -sf $bar_color", 0);
8066 /* These are not verified for existence, even with a binding set. */
8067 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
8068 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
8069 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
8073 #define SWM_MODNAME_SIZE 32
8074 #define SWM_KEY_WS "\n+ \t"
8076 parsebinding(const char *bindstr
, uint16_t *mod
, enum binding_type
*type
,
8077 uint32_t *val
, uint32_t *flags
)
8079 char *str
, *cp
, *name
;
8080 KeySym ks
, lks
, uks
;
8082 DNPRINTF(SWM_D_KEY
, "parsebinding: enter [%s]\n", bindstr
);
8083 if (mod
== NULL
|| val
== NULL
) {
8084 DNPRINTF(SWM_D_KEY
, "parsebinding: no mod or key vars\n");
8087 if (bindstr
== NULL
|| strlen(bindstr
) == 0) {
8088 DNPRINTF(SWM_D_KEY
, "parsebinding: no bindstr\n");
8092 if ((cp
= str
= strdup(bindstr
)) == NULL
)
8093 err(1, "parsebinding: strdup");
8095 *val
= XCB_NO_SYMBOL
;
8099 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
8100 DNPRINTF(SWM_D_KEY
, "parsebinding: entry [%s]\n", name
);
8102 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
8103 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
8105 else if (strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
) == 0)
8106 *mod
|= XCB_MOD_MASK_1
;
8107 else if (strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
) == 0)
8108 *mod
|= XCB_MOD_MASK_2
;
8109 else if (strncmp(name
, "Mod3", SWM_MODNAME_SIZE
) == 0)
8110 *mod
|= XCB_MOD_MASK_3
;
8111 else if (strncmp(name
, "Mod4", SWM_MODNAME_SIZE
) == 0)
8112 *mod
|= XCB_MOD_MASK_4
;
8113 else if (strncmp(name
, "Mod5", SWM_MODNAME_SIZE
) == 0)
8114 *mod
|= XCB_MOD_MASK_5
;
8115 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
8116 *mod
|= XCB_MOD_MASK_SHIFT
;
8117 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
8118 *mod
|= XCB_MOD_MASK_CONTROL
;
8119 else if (strncasecmp(name
, "ANYMOD", SWM_MODNAME_SIZE
) == 0)
8120 *mod
|= XCB_MOD_MASK_ANY
;
8121 else if (strncasecmp(name
, "REPLAY", SWM_MODNAME_SIZE
) == 0)
8122 *flags
|= BINDING_F_REPLAY
;
8123 else if (sscanf(name
, "Button%u", val
) == 1) {
8124 DNPRINTF(SWM_D_KEY
, "parsebinding: button %u\n", *val
);
8126 if (*val
> 255 || *val
== 0) {
8128 "parsebinding: invalid button %u\n", *val
);
8132 /* TODO: do this without Xlib. */
8133 ks
= XStringToKeysym(name
);
8134 if (ks
== NoSymbol
) {
8136 "parsebinding: invalid key %s\n", name
);
8141 XConvertCase(ks
, &lks
, &uks
);
8142 *val
= (uint32_t)lks
;
8146 /* If ANYMOD was specified, ignore the rest. */
8147 if (*mod
& XCB_MOD_MASK_ANY
)
8148 *mod
= XCB_MOD_MASK_ANY
;
8151 DNPRINTF(SWM_D_KEY
, "parsebinding: leave ok\n");
8156 strdupsafe(const char *str
)
8161 return (strdup(str
));
8165 binding_cmp(struct binding
*bp1
, struct binding
*bp2
)
8167 if (bp1
->type
< bp2
->type
)
8169 if (bp1
->type
> bp2
->type
)
8172 if (bp1
->value
< bp2
->value
)
8174 if (bp1
->value
> bp2
->value
)
8177 if (bp1
->mod
< bp2
->mod
)
8179 if (bp1
->mod
> bp2
->mod
)
8186 binding_insert(uint16_t mod
, enum binding_type type
, uint32_t val
,
8187 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
8191 DNPRINTF(SWM_D_KEY
, "binding_insert: mod: %u, type: %d, val: %u, "
8192 "action: %s(%d), spawn_name: %s\n", mod
, type
, val
,
8193 actions
[aid
].name
, aid
, spawn_name
);
8195 if ((bp
= malloc(sizeof *bp
)) == NULL
)
8196 err(1, "binding_insert: malloc");
8203 bp
->spawn_name
= strdupsafe(spawn_name
);
8204 RB_INSERT(binding_tree
, &bindings
, bp
);
8206 DNPRINTF(SWM_D_KEY
, "binding_insert: leave\n");
8210 binding_lookup(uint16_t mod
, enum binding_type type
, uint32_t val
)
8218 return (RB_FIND(binding_tree
, &bindings
, &bp
));
8222 binding_remove(struct binding
*bp
)
8224 DNPRINTF(SWM_D_KEY
, "binding_remove: mod: %u, type: %d, val: %u, "
8225 "action: %s(%d), spawn_name: %s\n", bp
->mod
, bp
->type
, bp
->value
,
8226 actions
[bp
->action
].name
, bp
->action
, bp
->spawn_name
);
8228 RB_REMOVE(binding_tree
, &bindings
, bp
);
8229 free(bp
->spawn_name
);
8232 DNPRINTF(SWM_D_KEY
, "binding_remove: leave\n");
8236 setbinding(uint16_t mod
, enum binding_type type
, uint32_t val
,
8237 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
8241 DNPRINTF(SWM_D_KEY
, "setbinding: enter %s [%s]\n",
8242 actions
[aid
].name
, spawn_name
);
8244 /* Unbind any existing. Loop is to handle MOD_MASK_ANY. */
8245 while ((bp
= binding_lookup(mod
, type
, val
)))
8248 if (aid
!= FN_INVALID
)
8249 binding_insert(mod
, type
, val
, aid
, flags
, spawn_name
);
8251 DNPRINTF(SWM_D_KEY
, "setbinding: leave\n");
8255 setconfbinding(const char *selector
, const char *value
, int flags
)
8257 struct spawn_prog
*sp
;
8258 uint32_t keybtn
, opts
;
8261 enum binding_type type
;
8263 /* suppress unused warning since var is needed */
8266 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter selector: [%s], "
8267 "value: [%s]\n", selector
, value
);
8268 if (selector
== NULL
|| strlen(selector
) == 0) {
8269 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
8270 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8271 setbinding(mod
, type
, keybtn
, FN_INVALID
, opts
, NULL
);
8276 /* search by key function name */
8277 for (aid
= 0; aid
< FN_INVALID
; aid
++) {
8278 if (strncasecmp(selector
, actions
[aid
].name
,
8279 SWM_FUNCNAME_LEN
) == 0) {
8280 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8281 "action\n", selector
);
8282 if (parsebinding(value
, &mod
, &type
, &keybtn
,
8284 setbinding(mod
, type
, keybtn
, aid
, opts
, NULL
);
8290 /* search by custom spawn name */
8291 if ((sp
= spawn_find(selector
)) != NULL
) {
8292 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8293 "spawn\n", selector
);
8294 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8295 setbinding(mod
, type
, keybtn
, FN_SPAWN_CUSTOM
, opts
,
8301 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
8305 #define MODSHIFT MODKEY | XCB_MOD_MASK_SHIFT
8307 setup_keybindings(void)
8309 #define BINDKEY(m, k, a) setbinding(m, KEYBIND, k, a, 0, NULL)
8310 #define BINDKEYSPAWN(m, k, s) setbinding(m, KEYBIND, k, FN_SPAWN_CUSTOM, 0, s)
8311 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8312 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8313 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8314 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8315 BINDKEY(MODKEY
, XK_v
, FN_BUTTON2
);
8316 BINDKEY(MODKEY
, XK_space
, FN_CYCLE_LAYOUT
);
8317 BINDKEY(MODSHIFT
, XK_backslash
, FN_FLIP_LAYOUT
);
8318 BINDKEY(MODKEY
, XK_t
, FN_FLOAT_TOGGLE
);
8319 BINDKEY(MODKEY
, XK_m
, FN_FOCUS_MAIN
);
8320 BINDKEY(MODKEY
, XK_j
, FN_FOCUS_NEXT
);
8321 BINDKEY(MODKEY
, XK_Tab
, FN_FOCUS_NEXT
);
8322 BINDKEY(MODKEY
, XK_k
, FN_FOCUS_PREV
);
8323 BINDKEY(MODSHIFT
, XK_Tab
, FN_FOCUS_PREV
);
8324 BINDKEY(MODKEY
, XK_u
, FN_FOCUS_URGENT
);
8325 BINDKEY(MODSHIFT
, XK_e
, FN_FULLSCREEN_TOGGLE
);
8326 BINDKEY(MODKEY
, XK_e
, FN_MAXIMIZE_TOGGLE
);
8327 BINDKEY(MODSHIFT
, XK_equal
, FN_HEIGHT_GROW
);
8328 BINDKEY(MODSHIFT
, XK_minus
, FN_HEIGHT_SHRINK
);
8329 BINDKEY(MODKEY
, XK_w
, FN_ICONIFY
);
8330 BINDKEY(MODKEY
, XK_h
, FN_MASTER_SHRINK
);
8331 BINDKEY(MODKEY
, XK_l
, FN_MASTER_GROW
);
8332 BINDKEY(MODKEY
, XK_comma
, FN_MASTER_ADD
);
8333 BINDKEY(MODKEY
, XK_period
, FN_MASTER_DEL
);
8334 BINDKEY(MODSHIFT
, XK_bracketright
, FN_MOVE_DOWN
);
8335 BINDKEY(MODKEY
, XK_bracketleft
, FN_MOVE_LEFT
);
8336 BINDKEY(MODKEY
, XK_bracketright
, FN_MOVE_RIGHT
);
8337 BINDKEY(MODSHIFT
, XK_bracketleft
, FN_MOVE_UP
);
8338 BINDKEY(MODSHIFT
, XK_KP_End
, FN_MVRG_1
);
8339 BINDKEY(MODSHIFT
, XK_KP_Down
, FN_MVRG_2
);
8340 BINDKEY(MODSHIFT
, XK_KP_Next
, FN_MVRG_3
);
8341 BINDKEY(MODSHIFT
, XK_KP_Left
, FN_MVRG_4
);
8342 BINDKEY(MODSHIFT
, XK_KP_Begin
, FN_MVRG_5
);
8343 BINDKEY(MODSHIFT
, XK_KP_Right
, FN_MVRG_6
);
8344 BINDKEY(MODSHIFT
, XK_KP_Home
, FN_MVRG_7
);
8345 BINDKEY(MODSHIFT
, XK_KP_Up
, FN_MVRG_8
);
8346 BINDKEY(MODSHIFT
, XK_KP_Prior
, FN_MVRG_9
);
8347 BINDKEY(MODSHIFT
, XK_1
, FN_MVWS_1
);
8348 BINDKEY(MODSHIFT
, XK_2
, FN_MVWS_2
);
8349 BINDKEY(MODSHIFT
, XK_3
, FN_MVWS_3
);
8350 BINDKEY(MODSHIFT
, XK_4
, FN_MVWS_4
);
8351 BINDKEY(MODSHIFT
, XK_5
, FN_MVWS_5
);
8352 BINDKEY(MODSHIFT
, XK_6
, FN_MVWS_6
);
8353 BINDKEY(MODSHIFT
, XK_7
, FN_MVWS_7
);
8354 BINDKEY(MODSHIFT
, XK_8
, FN_MVWS_8
);
8355 BINDKEY(MODSHIFT
, XK_9
, FN_MVWS_9
);
8356 BINDKEY(MODSHIFT
, XK_0
, FN_MVWS_10
);
8357 BINDKEY(MODSHIFT
, XK_F1
, FN_MVWS_11
);
8358 BINDKEY(MODSHIFT
, XK_F2
, FN_MVWS_12
);
8359 BINDKEY(MODSHIFT
, XK_F3
, FN_MVWS_13
);
8360 BINDKEY(MODSHIFT
, XK_F4
, FN_MVWS_14
);
8361 BINDKEY(MODSHIFT
, XK_F5
, FN_MVWS_15
);
8362 BINDKEY(MODSHIFT
, XK_F6
, FN_MVWS_16
);
8363 BINDKEY(MODSHIFT
, XK_F7
, FN_MVWS_17
);
8364 BINDKEY(MODSHIFT
, XK_F8
, FN_MVWS_18
);
8365 BINDKEY(MODSHIFT
, XK_F9
, FN_MVWS_19
);
8366 BINDKEY(MODSHIFT
, XK_F10
, FN_MVWS_20
);
8367 BINDKEY(MODSHIFT
, XK_F11
, FN_MVWS_21
);
8368 BINDKEY(MODSHIFT
, XK_F12
, FN_MVWS_22
);
8369 BINDKEY(MODSHIFT
, XK_slash
, FN_NAME_WORKSPACE
);
8370 BINDKEY(MODSHIFT
, XK_q
, FN_QUIT
);
8371 BINDKEY(MODKEY
, XK_r
, FN_RAISE
);
8372 BINDKEY(MODSHIFT
, XK_r
, FN_RAISE_TOGGLE
);
8373 BINDKEY(MODKEY
, XK_q
, FN_RESTART
);
8374 BINDKEY(MODKEY
, XK_KP_End
, FN_RG_1
);
8375 BINDKEY(MODKEY
, XK_KP_Down
, FN_RG_2
);
8376 BINDKEY(MODKEY
, XK_KP_Next
, FN_RG_3
);
8377 BINDKEY(MODKEY
, XK_KP_Left
, FN_RG_4
);
8378 BINDKEY(MODKEY
, XK_KP_Begin
, FN_RG_5
);
8379 BINDKEY(MODKEY
, XK_KP_Right
, FN_RG_6
);
8380 BINDKEY(MODKEY
, XK_KP_Home
, FN_RG_7
);
8381 BINDKEY(MODKEY
, XK_KP_Up
, FN_RG_8
);
8382 BINDKEY(MODKEY
, XK_KP_Prior
, FN_RG_9
);
8383 BINDKEY(MODSHIFT
, XK_Right
, FN_RG_NEXT
);
8384 BINDKEY(MODSHIFT
, XK_Left
, FN_RG_PREV
);
8385 BINDKEY(MODKEY
, XK_f
, FN_SEARCH_WIN
);
8386 BINDKEY(MODKEY
, XK_slash
, FN_SEARCH_WORKSPACE
);
8387 BINDKEYSPAWN(MODSHIFT
, XK_i
, "initscr");
8388 BINDKEYSPAWN(MODSHIFT
, XK_Delete
, "lock");
8389 BINDKEYSPAWN(MODKEY
, XK_p
, "menu");
8390 BINDKEYSPAWN(MODKEY
, XK_s
, "screenshot_all");
8391 BINDKEYSPAWN(MODSHIFT
, XK_s
, "screenshot_wind");
8392 BINDKEYSPAWN(MODSHIFT
, XK_Return
, "term");
8393 BINDKEY(MODSHIFT
, XK_comma
, FN_STACK_INC
);
8394 BINDKEY(MODSHIFT
, XK_period
, FN_STACK_DEC
);
8395 BINDKEY(MODSHIFT
, XK_space
, FN_STACK_INIT
);
8396 BINDKEY(MODKEY
, XK_Return
, FN_SWAP_MAIN
);
8397 BINDKEY(MODSHIFT
, XK_j
, FN_SWAP_NEXT
);
8398 BINDKEY(MODSHIFT
, XK_k
, FN_SWAP_PREV
);
8399 BINDKEY(MODSHIFT
, XK_w
, FN_UNICONIFY
);
8400 BINDKEY(MODSHIFT
, XK_v
, FN_VERSION
);
8401 BINDKEY(MODKEY
, XK_equal
, FN_WIDTH_GROW
);
8402 BINDKEY(MODKEY
, XK_minus
, FN_WIDTH_SHRINK
);
8403 BINDKEY(MODKEY
, XK_x
, FN_WIND_DEL
);
8404 BINDKEY(MODSHIFT
, XK_x
, FN_WIND_KILL
);
8405 BINDKEY(MODKEY
, XK_1
, FN_WS_1
);
8406 BINDKEY(MODKEY
, XK_2
, FN_WS_2
);
8407 BINDKEY(MODKEY
, XK_3
, FN_WS_3
);
8408 BINDKEY(MODKEY
, XK_4
, FN_WS_4
);
8409 BINDKEY(MODKEY
, XK_5
, FN_WS_5
);
8410 BINDKEY(MODKEY
, XK_6
, FN_WS_6
);
8411 BINDKEY(MODKEY
, XK_7
, FN_WS_7
);
8412 BINDKEY(MODKEY
, XK_8
, FN_WS_8
);
8413 BINDKEY(MODKEY
, XK_9
, FN_WS_9
);
8414 BINDKEY(MODKEY
, XK_0
, FN_WS_10
);
8415 BINDKEY(MODKEY
, XK_F1
, FN_WS_11
);
8416 BINDKEY(MODKEY
, XK_F2
, FN_WS_12
);
8417 BINDKEY(MODKEY
, XK_F3
, FN_WS_13
);
8418 BINDKEY(MODKEY
, XK_F4
, FN_WS_14
);
8419 BINDKEY(MODKEY
, XK_F5
, FN_WS_15
);
8420 BINDKEY(MODKEY
, XK_F6
, FN_WS_16
);
8421 BINDKEY(MODKEY
, XK_F7
, FN_WS_17
);
8422 BINDKEY(MODKEY
, XK_F8
, FN_WS_18
);
8423 BINDKEY(MODKEY
, XK_F9
, FN_WS_19
);
8424 BINDKEY(MODKEY
, XK_F10
, FN_WS_20
);
8425 BINDKEY(MODKEY
, XK_F11
, FN_WS_21
);
8426 BINDKEY(MODKEY
, XK_F12
, FN_WS_22
);
8427 BINDKEY(MODKEY
, XK_Right
, FN_WS_NEXT
);
8428 BINDKEY(MODKEY
, XK_Left
, FN_WS_PREV
);
8429 BINDKEY(MODKEY
, XK_Up
, FN_WS_NEXT_ALL
);
8430 BINDKEY(MODKEY
, XK_Down
, FN_WS_PREV_ALL
);
8431 BINDKEY(MODSHIFT
, XK_Up
, FN_WS_NEXT_MOVE
);
8432 BINDKEY(MODSHIFT
, XK_Down
, FN_WS_PREV_MOVE
);
8433 BINDKEY(MODKEY
, XK_a
, FN_WS_PRIOR
);
8435 BINDKEY(MODKEY
, XK_d
, FN_DEBUG_TOGGLE
);
8436 BINDKEY(MODSHIFT
, XK_d
, FN_DUMPWINS
);
8443 setup_btnbindings(void)
8445 setbinding(ANYMOD
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_FOCUS
,
8446 BINDING_F_REPLAY
, NULL
);
8447 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE
, 0, NULL
);
8448 setbinding(MODSHIFT
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE_CENTERED
, 0,
8450 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_MOVE
, 0, NULL
);
8455 clear_bindings(void)
8459 while ((bp
= RB_ROOT(&bindings
)))
8464 clear_keybindings(void)
8466 struct binding
*bp
, *bptmp
;
8468 RB_FOREACH_SAFE(bp
, binding_tree
, &bindings
, bptmp
) {
8469 if (bp
->type
!= KEYBIND
)
8476 setkeymapping(const char *selector
, const char *value
, int flags
)
8478 char *keymapping_file
;
8480 /* suppress unused warnings since vars are needed */
8484 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
8486 keymapping_file
= expand_tilde(value
);
8488 clear_keybindings();
8489 /* load new key bindings; if it fails, revert to default bindings */
8490 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
8491 clear_keybindings();
8492 setup_keybindings();
8495 free(keymapping_file
);
8497 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
8502 updatenumlockmask(void)
8505 xcb_get_modifier_mapping_reply_t
*modmap_r
;
8506 xcb_keycode_t
*modmap
, kc
, *keycode
;
8510 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
8511 xcb_get_modifier_mapping(conn
),
8514 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
8515 for (i
= 0; i
< 8; i
++) {
8516 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
8517 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
8519 keycode
= xcb_key_symbols_get_keycode(syms
,
8523 numlockmask
= (1 << i
);
8530 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
8537 int num_screens
, k
, j
;
8538 uint16_t modifiers
[4];
8539 xcb_keycode_t
*code
;
8541 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
8542 updatenumlockmask();
8545 modifiers
[1] = numlockmask
;
8546 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8547 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8549 num_screens
= get_screen_count();
8550 for (k
= 0; k
< num_screens
; k
++) {
8551 if (TAILQ_EMPTY(&screens
[k
].rl
))
8553 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
8555 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8556 if (bp
->type
!= KEYBIND
)
8559 /* If there is a catch-all, only bind that. */
8560 if ((binding_lookup(ANYMOD
, KEYBIND
, bp
->value
)) &&
8564 /* Skip unused ws binds. */
8565 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8566 bp
->action
<= FN_WS_22
)
8569 /* Skip unused mvws binds. */
8570 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8571 bp
->action
<= FN_MVWS_22
)
8574 if ((code
= xcb_key_symbols_get_keycode(syms
,
8575 bp
->value
)) == NULL
)
8578 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8579 /* All modifiers are grabbed in one pass. */
8580 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8581 "key: %u, modifiers: %d\n", bp
->value
,
8583 xcb_grab_key(conn
, 1, screens
[k
].root
,
8584 bp
->mod
, *code
, XCB_GRAB_MODE_ASYNC
,
8585 XCB_GRAB_MODE_SYNC
);
8587 /* Need to grab each modifier permutation. */
8588 for (j
= 0; j
< LENGTH(modifiers
); j
++) {
8589 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8590 "key: %u, modifiers: %d\n",
8591 bp
->value
, bp
->mod
| modifiers
[j
]);
8592 xcb_grab_key(conn
, 1,
8594 bp
->mod
| modifiers
[j
],
8595 *code
, XCB_GRAB_MODE_ASYNC
,
8596 XCB_GRAB_MODE_SYNC
);
8608 int num_screens
, i
, k
;
8609 uint16_t modifiers
[4];
8611 DNPRINTF(SWM_D_MOUSE
, "grabbuttons\n");
8612 updatenumlockmask();
8615 modifiers
[1] = numlockmask
;
8616 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8617 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8619 num_screens
= get_screen_count();
8620 for (k
= 0; k
< num_screens
; k
++) {
8621 if (TAILQ_EMPTY(&screens
[k
].rl
))
8623 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, screens
[k
].root
,
8625 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8626 if (bp
->type
!= BTNBIND
)
8629 /* If there is a catch-all, only bind that. */
8630 if ((binding_lookup(ANYMOD
, BTNBIND
, bp
->value
)) &&
8634 /* Skip unused ws binds. */
8635 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8636 bp
->action
<= FN_WS_22
)
8639 /* Skip unused mvws binds. */
8640 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8641 bp
->action
<= FN_MVWS_22
)
8644 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8645 /* All modifiers are grabbed in one pass. */
8646 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: grab, "
8647 "button: %u, modifiers: %d\n", bp
->value
,
8649 xcb_grab_button(conn
, 0, screens
[k
].root
,
8650 BUTTONMASK
, XCB_GRAB_MODE_SYNC
,
8651 XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
8652 XCB_CURSOR_NONE
, bp
->value
, bp
->mod
);
8654 /* Need to grab each modifier permutation. */
8655 for (i
= 0; i
< LENGTH(modifiers
); ++i
) {
8656 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: "
8657 "grab, button: %u, modifiers: %u\n",
8658 bp
->value
, bp
->mod
| modifiers
[i
]);
8659 xcb_grab_button(conn
, 0,
8660 screens
[k
].root
, BUTTONMASK
,
8662 XCB_GRAB_MODE_ASYNC
,
8664 XCB_CURSOR_NONE
, bp
->value
,
8665 bp
->mod
| modifiers
[i
]);
8672 const char *quirkname
[] = {
8673 "NONE", /* config string for "no value" */
8681 "FOCUSONMAP_SINGLE",
8689 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
8690 #define SWM_Q_DELIM "\n|+ \t"
8692 parsequirks(const char *qstr
, uint32_t *quirk
, int *ws
)
8694 char *str
, *cp
, *name
;
8697 if (quirk
== NULL
|| qstr
== NULL
)
8700 if ((cp
= str
= strdup(qstr
)) == NULL
)
8701 err(1, "parsequirks: strdup");
8704 while ((name
= strsep(&cp
, SWM_Q_DELIM
)) != NULL
) {
8706 cp
+= (long)strspn(cp
, SWM_Q_DELIM
);
8708 if (sscanf(name
, "WS[%d]", ws
) == 1) {
8714 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
8715 if (strncasecmp(name
, quirkname
[i
],
8716 SWM_QUIRK_LEN
) == 0) {
8717 DNPRINTF(SWM_D_QUIRK
,
8718 "parsequirks: %s\n", name
);
8724 *quirk
|= 1 << (i
-1);
8728 if (i
>= LENGTH(quirkname
)) {
8729 DNPRINTF(SWM_D_QUIRK
,
8730 "parsequirks: invalid quirk [%s]\n", name
);
8741 quirk_insert(const char *class, const char *instance
, const char *name
,
8742 uint32_t quirk
, int ws
)
8746 bool failed
= false;
8748 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: class: %s, instance: %s, name: %s,"
8749 " value: %u, ws: %d\n", class, instance
, name
, quirk
, ws
);
8751 if ((qp
= malloc(sizeof *qp
)) == NULL
)
8752 err(1, "quirk_insert: malloc");
8754 if ((qp
->class = strdup(class)) == NULL
)
8755 err(1, "quirk_insert: strdup");
8756 if ((qp
->instance
= strdup(instance
)) == NULL
)
8757 err(1, "quirk_insert: strdup");
8758 if ((qp
->name
= strdup(name
)) == NULL
)
8759 err(1, "quirk_insert: strdup");
8761 if (asprintf(&str
, "^%s$", class) == -1)
8762 err(1, "quirk_insert: asprintf");
8763 if (regcomp(&qp
->regex_class
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8764 add_startup_exception("regex failed to compile quirk 'class' "
8765 "field: %s", class);
8768 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8771 if (asprintf(&str
, "^%s$", instance
) == -1)
8772 err(1, "quirk_insert: asprintf");
8773 if (regcomp(&qp
->regex_instance
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8774 add_startup_exception("regex failed to compile quirk 'instance'"
8775 " field: %s", instance
);
8778 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8781 if (asprintf(&str
, "^%s$", name
) == -1)
8782 err(1, "quirk_insert: asprintf");
8783 if (regcomp(&qp
->regex_name
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8784 add_startup_exception("regex failed to compile quirk 'name' "
8788 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8792 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: regex error; skipping.\n");
8797 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
8799 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
8803 quirk_remove(struct quirk
*qp
)
8805 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%u]\n", qp
->class,
8806 qp
->name
, qp
->quirk
);
8808 TAILQ_REMOVE(&quirks
, qp
, entry
);
8811 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
8815 quirk_free(struct quirk
*qp
)
8817 regfree(&qp
->regex_class
);
8818 regfree(&qp
->regex_instance
);
8819 regfree(&qp
->regex_name
);
8831 while ((qp
= TAILQ_FIRST(&quirks
)) != NULL
) {
8837 quirk_replace(struct quirk
*qp
, const char *class, const char *instance
,
8838 const char *name
, uint32_t quirk
, int ws
)
8840 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp
->class,
8841 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
8844 quirk_insert(class, instance
, name
, quirk
, ws
);
8846 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
8850 setquirk(const char *class, const char *instance
, const char *name
,
8851 uint32_t quirk
, int ws
)
8855 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
8856 instance
, name
, quirk
, ws
);
8858 /* Remove/replace existing quirk. */
8859 TAILQ_FOREACH(qp
, &quirks
, entry
) {
8860 if (strcmp(qp
->class, class) == 0 &&
8861 strcmp(qp
->instance
, instance
) == 0 &&
8862 strcmp(qp
->name
, name
) == 0) {
8863 if (quirk
== 0 && ws
== -1)
8866 quirk_replace(qp
, class, instance
, name
, quirk
,
8868 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8873 /* Only insert if quirk is not NONE or forced ws is set. */
8874 if (quirk
|| ws
!= -1)
8875 quirk_insert(class, instance
, name
, quirk
, ws
);
8877 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8880 /* Eat '\' in str used to escape square brackets and colon. */
8882 unescape_selector(char *str
)
8886 for (cp
= str
; *str
!= '\0'; ++str
, ++cp
) {
8887 if (*str
== '\\' && (*(str
+ 1) == ':' || *(str
+ 1) == ']' ||
8897 setconfquirk(const char *selector
, const char *value
, int flags
)
8899 char *str
, *cp
, *class;
8900 char *instance
= NULL
, *name
= NULL
;
8901 int retval
, count
= 0, ws
= -1;
8904 /* suppress unused warning since var is needed */
8907 if (selector
== NULL
|| strlen(selector
) == 0)
8910 if ((str
= strdup(selector
)) == NULL
)
8911 err(1, "setconfquirk: strdup");
8913 /* Find non-escaped colon. */
8920 for (++cp
; *cp
!= '\0'; ++cp
) {
8921 if (*cp
== ':' && *(cp
- 1) != '\\') {
8927 unescape_selector(class);
8929 instance
= class + strlen(class) + 1;
8930 unescape_selector(instance
);
8936 name
= instance
+ strlen(instance
) + 1;
8937 unescape_selector(name
);
8942 DNPRINTF(SWM_D_CONF
, "setconfquirk: class: %s, instance: %s, "
8943 "name: %s\n", class, instance
, name
);
8945 if ((retval
= parsequirks(value
, &qrks
, &ws
)) == 0)
8946 setquirk(class, instance
, name
, qrks
, ws
);
8955 setquirk("MPlayer", "xv", ".*",
8956 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
, -1);
8957 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
8959 setquirk("Firefox-bin", "firefox-bin", ".*",
8961 setquirk("Firefox", "Dialog", ".*",
8963 setquirk("Gimp", "gimp", ".*",
8964 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8965 setquirk("XTerm", "xterm", ".*",
8966 SWM_Q_XTERM_FONTADJ
, -1);
8967 setquirk("xine", "Xine Window", ".*",
8968 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8969 setquirk("Xitk", "Xitk Combo", ".*",
8970 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8971 setquirk("xine", "xine Panel", ".*",
8972 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8973 setquirk("Xitk", "Xine Window", ".*",
8974 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8975 setquirk("xine", "xine Video Fullscreen Window", ".*",
8976 SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
, -1);
8977 setquirk("pcb", "pcb", ".*",
8979 setquirk("SDL_App", "SDL_App", ".*",
8980 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
, -1);
8983 /* conf file stuff */
8984 #define SWM_CONF_FILE "spectrwm.conf"
8985 #define SWM_CONF_FILE_OLD "scrotwm.conf"
8989 SWM_S_BAR_AT_BOTTOM
,
8990 SWM_S_BAR_BORDER_WIDTH
,
8993 SWM_S_BAR_ENABLED_WS
,
8998 SWM_S_BOUNDARY_WIDTH
,
8999 SWM_S_CLOCK_ENABLED
,
9002 SWM_S_CYCLE_VISIBLE
,
9004 SWM_S_DISABLE_BORDER
,
9006 SWM_S_FOCUS_CLOSE_WRAP
,
9007 SWM_S_FOCUS_DEFAULT
,
9009 SWM_S_ICONIC_ENABLED
,
9010 SWM_S_JAVA_WORKAROUND
,
9011 SWM_S_MAXIMIZE_HIDE_BAR
,
9012 SWM_S_REGION_PADDING
,
9017 SWM_S_STACK_ENABLED
,
9021 SWM_S_URGENT_COLLAPSE
,
9022 SWM_S_URGENT_ENABLED
,
9023 SWM_S_VERBOSE_LAYOUT
,
9026 SWM_S_WINDOW_CLASS_ENABLED
,
9027 SWM_S_WINDOW_INSTANCE_ENABLED
,
9028 SWM_S_WINDOW_NAME_ENABLED
,
9029 SWM_S_WORKSPACE_CLAMP
,
9030 SWM_S_WORKSPACE_LIMIT
,
9031 SWM_S_WORKSPACE_NAME
,
9035 setconfvalue(const char *selector
, const char *value
, int flags
)
9037 struct workspace
*ws
;
9038 int i
, ws_id
, num_screens
;
9039 char *b
, *str
, *sp
, s
[1024];
9042 case SWM_S_BAR_ACTION
:
9044 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
9045 err(1, "setconfvalue: bar_action");
9047 case SWM_S_BAR_AT_BOTTOM
:
9048 bar_at_bottom
= (atoi(value
) != 0);
9050 case SWM_S_BAR_BORDER_WIDTH
:
9051 bar_border_width
= atoi(value
);
9052 if (bar_border_width
< 0)
9053 bar_border_width
= 0;
9055 case SWM_S_BAR_DELAY
:
9056 /* No longer needed; leave to not break old conf files. */
9058 case SWM_S_BAR_ENABLED
:
9059 bar_enabled
= (atoi(value
) != 0);
9061 case SWM_S_BAR_ENABLED_WS
:
9062 ws_id
= atoi(selector
) - 1;
9063 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9064 errx(1, "setconfvalue: bar_enabled_ws: invalid "
9065 "workspace %d.", ws_id
+ 1);
9067 num_screens
= get_screen_count();
9068 for (i
= 0; i
< num_screens
; i
++) {
9069 ws
= (struct workspace
*)&screens
[i
].ws
;
9070 ws
[ws_id
].bar_enabled
= (atoi(value
) != 0);
9073 case SWM_S_BAR_FONT
:
9075 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
9076 err(1, "setconfvalue: asprintf: failed to allocate "
9077 "memory for bar_fonts.");
9080 /* If already in xft mode, then we are done. */
9081 if (!bar_font_legacy
)
9084 if ((sp
= str
= strdup(value
)) == NULL
)
9085 err(1, "setconfvalue: strdup");
9087 /* If there are any non-XLFD entries, switch to Xft mode. */
9088 while ((b
= strsep(&sp
, ",")) != NULL
) {
9092 bar_font_legacy
= false;
9099 case SWM_S_BAR_FORMAT
:
9101 if ((bar_format
= strdup(value
)) == NULL
)
9102 err(1, "setconfvalue: bar_format");
9104 case SWM_S_BAR_JUSTIFY
:
9105 if (strcmp(value
, "left") == 0)
9106 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
9107 else if (strcmp(value
, "center") == 0)
9108 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
9109 else if (strcmp(value
, "right") == 0)
9110 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
9112 errx(1, "invalid bar_justify");
9114 case SWM_S_BORDER_WIDTH
:
9115 border_width
= atoi(value
);
9116 if (border_width
< 0)
9119 case SWM_S_BOUNDARY_WIDTH
:
9120 boundary_width
= atoi(value
);
9121 if (boundary_width
< 0)
9124 case SWM_S_CLOCK_ENABLED
:
9125 clock_enabled
= (atoi(value
) != 0);
9127 case SWM_S_CLOCK_FORMAT
:
9128 #ifndef SWM_DENY_CLOCK_FORMAT
9130 if ((clock_format
= strdup(value
)) == NULL
)
9131 err(1, "setconfvalue: clock_format");
9134 case SWM_S_CYCLE_EMPTY
:
9135 cycle_empty
= (atoi(value
) != 0);
9137 case SWM_S_CYCLE_VISIBLE
:
9138 cycle_visible
= (atoi(value
) != 0);
9140 case SWM_S_DIALOG_RATIO
:
9141 dialog_ratio
= atof(value
);
9142 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
9145 case SWM_S_DISABLE_BORDER
:
9146 disable_border
= (atoi(value
) != 0);
9148 case SWM_S_FOCUS_CLOSE
:
9149 if (strcmp(value
, "first") == 0)
9150 focus_close
= SWM_STACK_BOTTOM
;
9151 else if (strcmp(value
, "last") == 0)
9152 focus_close
= SWM_STACK_TOP
;
9153 else if (strcmp(value
, "next") == 0)
9154 focus_close
= SWM_STACK_ABOVE
;
9155 else if (strcmp(value
, "previous") == 0)
9156 focus_close
= SWM_STACK_BELOW
;
9158 errx(1, "focus_close");
9160 case SWM_S_FOCUS_CLOSE_WRAP
:
9161 focus_close_wrap
= (atoi(value
) != 0);
9163 case SWM_S_FOCUS_DEFAULT
:
9164 if (strcmp(value
, "last") == 0)
9165 focus_default
= SWM_STACK_TOP
;
9166 else if (strcmp(value
, "first") == 0)
9167 focus_default
= SWM_STACK_BOTTOM
;
9169 errx(1, "focus_default");
9171 case SWM_S_FOCUS_MODE
:
9172 if (strcmp(value
, "default") == 0)
9173 focus_mode
= SWM_FOCUS_DEFAULT
;
9174 else if (strcmp(value
, "follow") == 0 ||
9175 strcmp(value
, "follow_cursor") == 0)
9176 focus_mode
= SWM_FOCUS_FOLLOW
;
9177 else if (strcmp(value
, "manual") == 0)
9178 focus_mode
= SWM_FOCUS_MANUAL
;
9180 errx(1, "focus_mode");
9182 case SWM_S_ICONIC_ENABLED
:
9183 iconic_enabled
= (atoi(value
) != 0);
9185 case SWM_S_JAVA_WORKAROUND
:
9186 java_workaround
= (atoi(value
) != 0);
9188 case SWM_S_MAXIMIZE_HIDE_BAR
:
9189 maximize_hide_bar
= atoi(value
);
9191 case SWM_S_REGION_PADDING
:
9192 region_padding
= atoi(value
);
9193 if (region_padding
< 0)
9196 case SWM_S_SPAWN_ORDER
:
9197 if (strcmp(value
, "first") == 0)
9198 spawn_position
= SWM_STACK_BOTTOM
;
9199 else if (strcmp(value
, "last") == 0)
9200 spawn_position
= SWM_STACK_TOP
;
9201 else if (strcmp(value
, "next") == 0)
9202 spawn_position
= SWM_STACK_ABOVE
;
9203 else if (strcmp(value
, "previous") == 0)
9204 spawn_position
= SWM_STACK_BELOW
;
9206 errx(1, "spawn_position");
9208 case SWM_S_SPAWN_TERM
:
9209 setconfspawn("term", value
, 0);
9210 setconfspawn("spawn_term", value
, 0);
9213 /* No longer needed; leave to not break old conf files. */
9215 case SWM_S_SS_ENABLED
:
9216 /* No longer needed; leave to not break old conf files. */
9218 case SWM_S_STACK_ENABLED
:
9219 stack_enabled
= (atoi(value
) != 0);
9221 case SWM_S_TERM_WIDTH
:
9222 term_width
= atoi(value
);
9226 case SWM_S_TILE_GAP
:
9227 tile_gap
= atoi(value
);
9229 case SWM_S_TRACK_PID_WS
:
9230 track_pid_ws
= atoi(value
);
9232 case SWM_S_URGENT_COLLAPSE
:
9233 urgent_collapse
= (atoi(value
) != 0);
9235 case SWM_S_URGENT_ENABLED
:
9236 urgent_enabled
= (atoi(value
) != 0);
9238 case SWM_S_VERBOSE_LAYOUT
:
9239 verbose_layout
= (atoi(value
) != 0);
9240 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
9242 layouts
[i
].l_string
= fancy_stacker
;
9244 layouts
[i
].l_string
= plain_stacker
;
9247 case SWM_S_WARP_FOCUS
:
9248 warp_focus
= (atoi(value
) != 0);
9250 case SWM_S_WARP_POINTER
:
9251 warp_pointer
= (atoi(value
) != 0);
9253 case SWM_S_WINDOW_CLASS_ENABLED
:
9254 window_class_enabled
= (atoi(value
) != 0);
9256 case SWM_S_WINDOW_INSTANCE_ENABLED
:
9257 window_instance_enabled
= (atoi(value
) != 0);
9259 case SWM_S_WINDOW_NAME_ENABLED
:
9260 window_name_enabled
= (atoi(value
) != 0);
9262 case SWM_S_WORKSPACE_CLAMP
:
9263 workspace_clamp
= (atoi(value
) != 0);
9265 case SWM_S_WORKSPACE_LIMIT
:
9266 workspace_limit
= atoi(value
);
9267 if (workspace_limit
> SWM_WS_MAX
)
9268 workspace_limit
= SWM_WS_MAX
;
9269 else if (workspace_limit
< 1)
9270 workspace_limit
= 1;
9272 ewmh_update_desktops();
9274 case SWM_S_WORKSPACE_NAME
:
9275 if (getenv("SWM_STARTED") != NULL
)
9279 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9280 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
9282 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9283 errx(1, "setconfvalue: workspace_name: invalid "
9284 "workspace %d.", ws_id
+ 1);
9286 num_screens
= get_screen_count();
9287 for (i
= 0; i
< num_screens
; ++i
) {
9288 ws
= (struct workspace
*)&screens
[i
].ws
;
9290 if (strlen(s
) > 0) {
9291 free(ws
[ws_id
].name
);
9292 if ((ws
[ws_id
].name
= strdup(s
)) == NULL
)
9293 err(1, "setconfvalue: workspace_name.");
9295 ewmh_update_desktop_names();
9296 ewmh_get_desktop_names();
9307 setconfmodkey(const char *selector
, const char *value
, int flags
)
9309 /* suppress unused warnings since vars are needed */
9313 if (strncasecmp(value
, "Mod1", strlen("Mod1")) == 0)
9314 update_modkey(XCB_MOD_MASK_1
);
9315 else if (strncasecmp(value
, "Mod2", strlen("Mod2")) == 0)
9316 update_modkey(XCB_MOD_MASK_2
);
9317 else if (strncasecmp(value
, "Mod3", strlen("Mod3")) == 0)
9318 update_modkey(XCB_MOD_MASK_3
);
9319 else if (strncasecmp(value
, "Mod4", strlen("Mod4")) == 0)
9320 update_modkey(XCB_MOD_MASK_4
);
9321 else if (strncasecmp(value
, "Mod5", strlen("Mod5")) == 0)
9322 update_modkey(XCB_MOD_MASK_5
);
9329 setconfcolor(const char *selector
, const char *value
, int flags
)
9331 int first
, last
, i
= 0, num_screens
;
9333 num_screens
= get_screen_count();
9335 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
9336 if (selector
== NULL
|| strlen(selector
) == 0 ||
9337 (last
= atoi(selector
) - 1) < 0) {
9339 last
= num_screens
- 1;
9344 if (last
>= num_screens
) {
9345 add_startup_exception("invalid screen index: %d out of bounds "
9346 "(maximum %d)", last
+ 1, num_screens
);
9350 for (i
= first
; i
<= last
; ++i
) {
9351 setscreencolor(value
, i
, flags
);
9354 * When setting focus/unfocus colors, we need to also
9355 * set maximize colors to match if they haven't been customized.
9357 if (flags
== SWM_S_COLOR_FOCUS
&&
9358 !screens
[i
].c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].manual
)
9359 setscreencolor(value
, i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
9360 else if (flags
== SWM_S_COLOR_UNFOCUS
&&
9361 !screens
[i
].c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].manual
)
9362 setscreencolor(value
, i
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
9364 screens
[i
].c
[flags
].manual
= 1;
9371 setconfregion(const char *selector
, const char *value
, int flags
)
9373 /* suppress unused warnings since vars are needed */
9377 custom_region(value
);
9382 setautorun(const char *selector
, const char *value
, int flags
)
9386 char *ap
, *sp
, *str
;
9392 /* suppress unused warnings since vars are needed */
9396 if (getenv("SWM_STARTED"))
9400 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9401 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
9403 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9404 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
9406 sp
= str
= expand_tilde((char *)&s
);
9409 * This is a little intricate
9411 * If the pid already exists we simply reuse it because it means it was
9412 * used before AND not claimed by manage_window. We get away with
9413 * altering it in the parent after INSERT because this can not be a race
9416 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
9419 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
9421 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
9422 err(1, "setautorun: realloc");
9423 a
.argv
[argc
- 1] = ap
;
9426 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
9427 err(1, "setautorun: realloc");
9428 a
.argv
[argc
] = NULL
;
9430 if ((pid
= fork()) == 0) {
9431 spawn(ws_id
, &a
, true);
9441 p
= calloc(1, sizeof *p
);
9444 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
9454 setlayout(const char *selector
, const char *value
, int flags
)
9456 struct workspace
*ws
;
9457 int ws_id
, i
, mg
, ma
, si
, ar
;
9458 int st
= SWM_V_STACK
, num_screens
;
9462 /* suppress unused warnings since vars are needed */
9467 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
9468 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
9469 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9470 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9473 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9474 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
9476 if (strcasecmp(s
, "vertical") == 0)
9478 else if (strcasecmp(s
, "vertical_flip") == 0) {
9481 } else if (strcasecmp(s
, "horizontal") == 0)
9483 else if (strcasecmp(s
, "horizontal_flip") == 0) {
9486 } else if (strcasecmp(s
, "fullscreen") == 0)
9489 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9490 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9495 initial_layouts
[ws_id
].configured
= true;
9496 initial_layouts
[ws_id
].layout
= st
;
9497 initial_layouts
[ws_id
].master_grow
= mg
;
9498 initial_layouts
[ws_id
].master_add
= ma
;
9499 initial_layouts
[ws_id
].stack_inc
= si
;
9500 initial_layouts
[ws_id
].always_raise
= ar
;
9501 initial_layouts
[ws_id
].apply_flip
= f
;
9503 num_screens
= get_screen_count();
9504 for (i
= 0; i
< num_screens
; ++i
) {
9505 ws
= &screens
[i
].ws
[ws_id
];
9513 initlayout(struct workspace
*ws
)
9519 if (!initial_layouts
[ws_id
].configured
)
9522 ws
->cur_layout
= &layouts
[initial_layouts
[ws_id
].layout
];
9524 ws
->cur_layout
->l_config(ws
, SWM_ARG_ID_STACKINIT
);
9526 ws
->always_raise
= (initial_layouts
[ws_id
].always_raise
!= 0);
9529 for (i
= 0; i
< abs(initial_layouts
[ws_id
].master_grow
); i
++) {
9530 ws
->cur_layout
->l_config(ws
,
9531 initial_layouts
[ws_id
].master_grow
>= 0 ? SWM_ARG_ID_MASTERGROW
:
9532 SWM_ARG_ID_MASTERSHRINK
);
9535 for (i
= 0; i
< abs(initial_layouts
[ws_id
].master_add
); i
++) {
9536 ws
->cur_layout
->l_config(ws
,
9537 initial_layouts
[ws_id
].master_add
>= 0 ? SWM_ARG_ID_MASTERADD
:
9538 SWM_ARG_ID_MASTERDEL
);
9541 for (i
= 0; i
< abs(initial_layouts
[ws_id
].stack_inc
); i
++) {
9542 ws
->cur_layout
->l_config(ws
,
9543 initial_layouts
[ws_id
].stack_inc
>= 0 ? SWM_ARG_ID_STACKINC
:
9544 SWM_ARG_ID_STACKDEC
);
9547 if (initial_layouts
[ws_id
].apply_flip
) {
9548 ws
->cur_layout
->l_config(ws
,
9549 SWM_ARG_ID_FLIPLAYOUT
);
9553 /* config options */
9554 struct config_option
{
9556 int (*func
)(const char*, const char*, int);
9559 struct config_option configopt
[] = {
9560 { "autorun", setautorun
, 0 },
9561 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
9562 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
9563 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
9564 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
9565 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
9566 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
9567 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
9568 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
9569 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
9570 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
9571 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
9572 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
9573 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
9574 { "bind", setconfbinding
, 0 },
9575 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
9576 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
9577 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
9578 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
9579 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
9580 { "color_focus_maximized", setconfcolor
, SWM_S_COLOR_FOCUS_MAXIMIZED
},
9581 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
9582 { "color_unfocus_maximized", setconfcolor
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
},
9583 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
9584 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
9585 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
9586 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
9587 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
9588 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
9589 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
9590 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
9591 { "iconic_enabled", setconfvalue
, SWM_S_ICONIC_ENABLED
},
9592 { "java_workaround", setconfvalue
, SWM_S_JAVA_WORKAROUND
},
9593 { "keyboard_mapping", setkeymapping
, 0 },
9594 { "layout", setlayout
, 0 },
9595 { "maximize_hide_bar", setconfvalue
, SWM_S_MAXIMIZE_HIDE_BAR
},
9596 { "modkey", setconfmodkey
, 0 },
9597 { "program", setconfspawn
, 0 },
9598 { "quirk", setconfquirk
, 0 },
9599 { "region", setconfregion
, 0 },
9600 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
9601 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
9602 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
9603 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
9604 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
9605 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
9606 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
9607 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
9608 { "title_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
}, /* For backwards compat. */
9609 { "title_name_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
}, /* For backwards compat. */
9610 { "track_pid_ws", setconfvalue
, SWM_S_TRACK_PID_WS
},
9611 { "urgent_collapse", setconfvalue
, SWM_S_URGENT_COLLAPSE
},
9612 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
9613 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
9614 { "warp_focus", setconfvalue
, SWM_S_WARP_FOCUS
},
9615 { "warp_pointer", setconfvalue
, SWM_S_WARP_POINTER
},
9616 { "window_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
},
9617 { "window_instance_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
},
9618 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
9619 { "workspace_clamp", setconfvalue
, SWM_S_WORKSPACE_CLAMP
},
9620 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
9621 { "name", setconfvalue
, SWM_S_WORKSPACE_NAME
},
9625 _add_startup_exception(const char *fmt
, va_list ap
)
9627 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
9628 warn("%s: asprintf", __func__
);
9632 add_startup_exception(const char *fmt
, ...)
9638 if (startup_exception
)
9641 /* force bar to be enabled due to exception */
9645 _add_startup_exception(fmt
, ap
);
9650 conf_load(const char *filename
, int keymapping
)
9653 char *line
= NULL
, *cp
, *ce
, *optsub
, *optval
= NULL
;
9654 size_t linelen
, lineno
= 0;
9655 int wordlen
, i
, optidx
, count
;
9656 struct config_option
*opt
= NULL
;
9658 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
9660 if (filename
== NULL
) {
9661 warnx("conf_load: no filename");
9665 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
9667 if ((config
= fopen(filename
, "r")) == NULL
) {
9668 warn("conf_load: fopen: %s", filename
);
9672 while (!feof(config
)) {
9676 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
9677 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
9679 err(1, "%s", filename
);
9684 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9685 if (cp
[0] == '\0') {
9689 /* get config option */
9690 wordlen
= strcspn(cp
, "=[ \t\n");
9692 add_startup_exception("%s: line %zd: no option found",
9697 for (i
= 0; i
< LENGTH(configopt
); i
++) {
9698 opt
= &configopt
[i
];
9699 if (strncasecmp(cp
, opt
->optname
, wordlen
) == 0 &&
9700 (int)strlen(opt
->optname
) == wordlen
) {
9706 add_startup_exception("%s: line %zd: unknown option "
9707 "%.*s", filename
, lineno
, wordlen
, cp
);
9710 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
9711 add_startup_exception("%s: line %zd: invalid option "
9712 "%.*s", filename
, lineno
, wordlen
, cp
);
9716 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9718 /* get [selector] if any */
9723 /* Get length of selector. */
9724 for (ce
= ++cp
; *ce
!= '\0'; ++ce
) {
9725 /* Find matching (not escaped) bracket. */
9726 if (*ce
== ']' && *(ce
- 1) != '\\') {
9733 add_startup_exception("%s: line %zd: syntax "
9734 "error: unterminated selector", filename
,
9739 /* ce points at ']'; terminate optsub. */
9744 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
9747 if (strlen(optval
) == 0) {
9748 add_startup_exception("%s: line %zd: must supply value "
9749 "to %s", filename
, lineno
,
9750 configopt
[optidx
].optname
);
9753 /* trim trailing spaces */
9754 ce
= optval
+ strlen(optval
) - 1;
9755 while (ce
> optval
&& isspace(*ce
)) --ce
;
9757 /* call function to deal with it all */
9758 if (configopt
[optidx
].func(optsub
, optval
,
9759 configopt
[optidx
].funcflags
) != 0) {
9760 add_startup_exception("%s: line %zd: invalid data for "
9761 "%s", filename
, lineno
, configopt
[optidx
].optname
);
9770 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
9776 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
9778 struct ws_win
*parent
, *w
;
9779 struct swm_region
*r
;
9780 struct workspace
*ws
;
9781 xcb_icccm_wm_hints_t wmh
;
9783 parent
= find_window(win
->transient
);
9785 parent
->focus_child
= win
;
9787 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
9788 " for %#x trans %#x\n", win
->id
, win
->transient
);
9790 r
= root_to_region(win
->s
->root
, SWM_CK_ALL
);
9792 /* parent doen't exist in our window list */
9793 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
9794 if (xcb_icccm_get_wm_hints_reply(conn
,
9795 xcb_icccm_get_wm_hints(conn
, w
->id
),
9797 warnx("can't get hints for %#x", w
->id
);
9801 if (win
->hints
.window_group
!= wmh
.window_group
)
9804 w
->focus_child
= win
;
9805 win
->transient
= w
->id
;
9807 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
9808 "transient to %#x\n", win
->transient
);
9815 window_get_pid(xcb_window_t win
)
9820 xcb_get_property_cookie_t pc
;
9821 xcb_get_property_reply_t
*pr
;
9823 apid
= get_atom_from_string("_NET_WM_PID");
9824 if (apid
== XCB_ATOM_NONE
)
9827 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
9828 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9831 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
9836 if (pr
->type
== apid
&& pr
->format
== 32)
9837 ret
= *((pid_t
*)xcb_get_property_value(pr
));
9843 apid
= get_atom_from_string("_SWM_PID");
9844 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
9846 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9849 if (pr
->type
!= apid
) {
9854 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
9861 get_swm_ws(xcb_window_t id
)
9867 xcb_get_property_reply_t
*gpr
;
9869 gpr
= xcb_get_property_reply(conn
,
9870 xcb_get_property(conn
, 0, id
, a_swm_ws
,
9871 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
9876 proplen
= xcb_get_property_value_length(gpr
);
9878 prop
= malloc(proplen
+ 1);
9881 xcb_get_property_value(gpr
),
9883 prop
[proplen
] = '\0';
9890 DNPRINTF(SWM_D_PROP
, "get_swm_ws: _SWM_WS: %s\n", prop
);
9891 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
9893 DNPRINTF(SWM_D_PROP
, "get_swm_ws: win #%s: %s",
9903 get_ws_idx(struct ws_win
*win
)
9905 xcb_get_property_reply_t
*gpr
;
9911 gpr
= xcb_get_property_reply(conn
,
9912 xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
9913 XCB_ATOM_CARDINAL
, 0, 1),
9916 if (gpr
->type
== XCB_ATOM_CARDINAL
&& gpr
->format
== 32)
9917 ws_idx
= *((int *)xcb_get_property_value(gpr
));
9921 if (ws_idx
== -1 && !(win
->quirks
& SWM_Q_IGNORESPAWNWS
))
9922 ws_idx
= get_swm_ws(win
->id
);
9924 if (ws_idx
> workspace_limit
- 1 || ws_idx
< -1)
9927 DNPRINTF(SWM_D_PROP
, "get_ws_idx: win %#x, ws_idx: %d\n", win
->id
,
9934 reparent_window(struct ws_win
*win
)
9936 xcb_void_cookie_t c
;
9937 xcb_generic_error_t
*error
;
9940 win
->frame
= xcb_generate_id(conn
);
9942 DNPRINTF(SWM_D_MISC
, "reparent_window: win %#x, frame: %#x\n",
9943 win
->id
, win
->frame
);
9946 XCB_EVENT_MASK_ENTER_WINDOW
|
9947 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
9948 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
9949 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
|
9950 XCB_EVENT_MASK_EXPOSURE
;
9952 wa
[0] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
9955 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
->frame
, win
->s
->root
,
9956 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), 0,
9957 XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
9958 XCB_CW_EVENT_MASK
, wa
);
9960 win
->state
= SWM_WIN_STATE_REPARENTING
;
9961 c
= xcb_reparent_window_checked(conn
, win
->id
, win
->frame
, 0, 0);
9962 if ((error
= xcb_request_check(conn
, c
))) {
9963 DNPRINTF(SWM_D_MISC
, "reparent_window: error:\n");
9968 xcb_destroy_window(conn
, win
->frame
);
9969 win
->frame
= XCB_WINDOW_NONE
;
9971 xcb_change_save_set(conn
, XCB_SET_MODE_INSERT
, win
->id
);
9973 DNPRINTF(SWM_D_MISC
, "reparent_window: done.\n");
9977 unparent_window(struct ws_win
*win
)
9979 xcb_change_save_set(conn
, XCB_SET_MODE_DELETE
, win
->id
);
9980 xcb_reparent_window(conn
, win
->id
, win
->s
->root
, X(win
), Y(win
));
9981 xcb_destroy_window(conn
, win
->frame
);
9982 win
->frame
= XCB_WINDOW_NONE
;
9983 win
->state
= SWM_WIN_STATE_UNPARENTING
;
9987 manage_window(xcb_window_t id
, int spawn_pos
, bool mapping
)
9989 struct ws_win
*win
= NULL
, *ww
;
9990 struct swm_region
*r
;
9993 xcb_get_geometry_reply_t
*gr
;
9994 xcb_get_window_attributes_reply_t
*war
= NULL
;
9995 xcb_window_t trans
= XCB_WINDOW_NONE
;
9996 uint32_t i
, wa
[1], new_flags
;
9997 int ws_idx
, force_ws
= -1;
9998 char *class, *instance
, *name
;
10000 if (find_bar(id
)) {
10001 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region bar; "
10002 "skipping.\n", id
);
10006 if (find_region(id
)) {
10007 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region window; "
10008 "skipping.\n", id
);
10012 if ((win
= find_window(id
)) != NULL
) {
10013 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x already "
10014 "managed; skipping.)\n", id
);
10015 return (win
); /* Already managed. */
10018 /* See if window is on the unmanaged list. */
10019 if ((win
= find_unmanaged_window(id
)) != NULL
) {
10020 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x found on "
10021 "unmanaged list.\n", id
);
10022 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10024 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is new.\n", id
);
10027 war
= xcb_get_window_attributes_reply(conn
,
10028 xcb_get_window_attributes(conn
, id
), NULL
);
10030 DNPRINTF(SWM_D_EVENT
, "manage_window: window lost.\n");
10034 if (war
->override_redirect
) {
10035 DNPRINTF(SWM_D_EVENT
, "manage_window: override_redirect; "
10040 if (!mapping
&& war
->map_state
== XCB_MAP_STATE_UNMAPPED
&&
10041 get_win_state(id
) == XCB_ICCCM_WM_STATE_WITHDRAWN
) {
10042 DNPRINTF(SWM_D_EVENT
, "manage_window: window withdrawn; "
10047 /* Try to get initial window geometry. */
10048 gr
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, id
), NULL
);
10050 DNPRINTF(SWM_D_MISC
, "manage_window: get geometry failed.\n");
10054 /* Figure out which region the window belongs to. */
10055 r
= root_to_region(gr
->root
, SWM_CK_ALL
);
10057 /* Handle special windows with special _NET_WM_WINDOW_TYPE */
10058 if (ewmh_handle_special_types(id
, r
)) {
10059 DNPRINTF(SWM_D_EVENT
, "manage_window: "
10060 "unmanaged ewmh window type\n");
10065 /* Create and initialize ws_win object. */
10066 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
10067 err(1, "manage_window: calloc: failed to allocate "
10068 "memory for new window");
10073 /* Ignore window border if there is one. */
10074 WIDTH(win
) = gr
->width
;
10075 HEIGHT(win
) = gr
->height
;
10076 X(win
) = gr
->x
+ gr
->border_width
;
10077 Y(win
) = gr
->y
+ gr
->border_width
;
10078 win
->bordered
= false;
10079 win
->mapped
= (war
->map_state
!= XCB_MAP_STATE_UNMAPPED
);
10080 win
->s
= r
->s
; /* this never changes */
10084 /* Select which X events to monitor and set border pixel color. */
10085 wa
[0] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
10086 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
10088 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_EVENT_MASK
, wa
);
10090 /* Get WM_SIZE_HINTS. */
10091 xcb_icccm_get_wm_normal_hints_reply(conn
,
10092 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
10095 /* Get WM_HINTS. */
10096 xcb_icccm_get_wm_hints_reply(conn
,
10097 xcb_icccm_get_wm_hints(conn
, win
->id
),
10098 &win
->hints
, NULL
);
10100 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
10101 xcb_icccm_get_wm_transient_for_reply(conn
,
10102 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
10105 win
->transient
= trans
;
10106 set_child_transient(win
, &win
->transient
);
10109 /* Get WM_PROTOCOLS. */
10110 get_wm_protocols(win
);
10113 /* Must be after getting WM_HINTS and WM_PROTOCOLS. */
10114 DNPRINTF(SWM_D_FOCUS
, "manage_window: input_model: %s\n",
10115 get_win_input_model(win
));
10118 /* Set initial quirks based on EWMH. */
10119 ewmh_autoquirk(win
);
10121 /* Determine initial quirks. */
10122 xcb_icccm_get_wm_class_reply(conn
,
10123 xcb_icccm_get_wm_class(conn
, win
->id
),
10126 class = win
->ch
.class_name
? win
->ch
.class_name
: "";
10127 instance
= win
->ch
.instance_name
? win
->ch
.instance_name
: "";
10128 name
= get_win_name(win
->id
);
10130 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, instance: %s, "
10131 "name: %s\n", class, instance
, name
);
10133 /* java is retarded so treat it special */
10134 if (strstr(instance
, "sun-awt")) {
10135 DNPRINTF(SWM_D_CLASS
, "manage_window: java window detected.\n");
10139 TAILQ_FOREACH(qp
, &quirks
, entry
) {
10140 if (regexec(&qp
->regex_class
, class, 0, NULL
, 0) == 0 &&
10141 regexec(&qp
->regex_instance
, instance
, 0, NULL
, 0) == 0 &&
10142 regexec(&qp
->regex_name
, name
, 0, NULL
, 0) == 0) {
10143 DNPRINTF(SWM_D_CLASS
, "manage_window: matched "
10144 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp
->class,
10145 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
10146 win
->quirks
= qp
->quirk
;
10147 if (qp
->ws
>= 0 && qp
->ws
< workspace_limit
)
10154 /* Reset font sizes (the bruteforce way; no default keybinding). */
10155 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
10156 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
10157 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
10158 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
10159 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
10162 /* Figure out which workspace the window belongs to. */
10163 if (!(win
->quirks
& SWM_Q_IGNOREPID
) &&
10164 (p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
10165 win
->ws
= &r
->s
->ws
[p
->ws
];
10166 TAILQ_REMOVE(&pidlist
, p
, entry
);
10169 } else if ((ws_idx
= get_ws_idx(win
)) != -1 &&
10171 /* _SWM_WS is set; use that. */
10172 win
->ws
= &r
->s
->ws
[ws_idx
];
10173 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
10174 /* Launch transients in the same ws as parent. */
10180 if (force_ws
!= -1)
10181 win
->ws
= &r
->s
->ws
[force_ws
];
10183 /* Set the _NET_WM_DESKTOP atom. */
10184 DNPRINTF(SWM_D_PROP
, "manage_window: set _NET_WM_DESKTOP: %d\n",
10186 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
10187 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &win
->ws
->idx
);
10189 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
10190 xcb_delete_property(conn
, win
->id
, a_swm_ws
);
10192 /* WS must already be set for this to work. */
10193 store_float_geom(win
);
10195 /* Make sure window is positioned inside its region, if its active. */
10197 region_containment(win
, r
, SWM_CW_ALLSIDES
|
10198 SWM_CW_HARDBOUNDARY
);
10199 update_window(win
);
10202 /* Figure out where to insert the window in the workspace list. */
10203 if (trans
&& (ww
= find_window(trans
)))
10204 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
10205 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_ABOVE
)
10206 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
10208 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_BELOW
)
10209 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
10210 else switch (spawn_pos
) {
10212 case SWM_STACK_TOP
:
10213 case SWM_STACK_ABOVE
:
10214 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
10216 case SWM_STACK_BOTTOM
:
10217 case SWM_STACK_BELOW
:
10218 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
10221 ewmh_update_client_list();
10223 TAILQ_INSERT_HEAD(&win
->ws
->stack
, win
, stack_entry
);
10226 /* Get/apply initial _NET_WM_STATE */
10227 ewmh_get_wm_state(win
);
10229 /* Apply quirks. */
10230 new_flags
= win
->ewmh_flags
;
10232 if (win
->quirks
& SWM_Q_FLOAT
)
10233 new_flags
|= EWMH_F_ABOVE
;
10235 if (win
->quirks
& SWM_Q_ANYWHERE
)
10236 new_flags
|= SWM_F_MANUAL
;
10238 ewmh_apply_flags(win
, new_flags
);
10239 ewmh_update_wm_state(win
);
10241 /* Set initial _NET_WM_ALLOWED_ACTIONS */
10242 ewmh_update_actions(win
);
10244 reparent_window(win
);
10246 DNPRINTF(SWM_D_MISC
, "manage_window: done. win %#x, (x,y) w x h: "
10247 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win
->id
,
10248 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
10249 YESNO(ICONIC(win
)), win
->transient
);
10256 free_window(struct ws_win
*win
)
10258 DNPRINTF(SWM_D_MISC
, "free_window: win %#x\n", WINID(win
));
10263 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
10266 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
10269 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
10273 unmanage_window(struct ws_win
*win
)
10275 DNPRINTF(SWM_D_MISC
, "unmanage_window: win %#x\n", WINID(win
));
10281 unparent_window(win
);
10283 TAILQ_REMOVE(&win
->ws
->stack
, win
, stack_entry
);
10284 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
10285 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
10287 ewmh_update_client_list();
10291 expose(xcb_expose_event_t
*e
)
10296 struct workspace
*ws
;
10299 DNPRINTF(SWM_D_EVENT
, "expose: win %#x, count: %d\n", e
->window
,
10305 if ((b
= find_bar(e
->window
))) {
10308 } else if ((w
= find_window(e
->window
)) && w
->frame
== e
->window
) {
10312 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
10318 DNPRINTF(SWM_D_EVENT
, "expose: done\n");
10322 focusin(xcb_focus_in_event_t
*e
)
10324 struct ws_win
*win
;
10326 DNPRINTF(SWM_D_EVENT
, "focusin: win %#x, mode: %s(%u), "
10327 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10328 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10329 if ((win
= find_window(e
->event
)) && win
!= win
->ws
->focus
&&
10330 win
!= win
->ws
->focus_pending
&&
10331 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10332 win
->ws
->focus_prev
= win
->ws
->focus
;
10333 win
->ws
->focus
= win
;
10334 win
->ws
->focus_pending
= NULL
;
10336 if (win
->ws
->focus_prev
)
10337 draw_frame(win
->ws
->focus_prev
);
10345 focusout(xcb_focus_out_event_t
*e
)
10347 DNPRINTF(SWM_D_EVENT
, "focusout: win %#x, mode: %s(%u), "
10348 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10349 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10354 keypress(xcb_key_press_event_t
*e
)
10357 struct binding
*bp
;
10358 xcb_keysym_t keysym
;
10359 bool replay
= true;
10361 last_event_time
= e
->time
;
10363 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
10365 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
10366 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10367 "state: %u, cleaned: %u, same_screen: %s\n", keysym
, e
->event
,
10368 e
->event_x
, e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
,
10369 e
->root_y
, e
->child
, e
->state
, CLEANMASK(e
->state
),
10370 YESNO(e
->same_screen
));
10372 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10374 /* Look for catch-all. */
10375 if ((bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
)) == NULL
)
10379 replay
= bp
->flags
& BINDING_F_REPLAY
;
10381 if ((ap
= &actions
[bp
->action
]) == NULL
)
10384 if (bp
->action
== FN_SPAWN_CUSTOM
)
10385 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10388 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10390 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10394 DNPRINTF(SWM_D_EVENT
, "keypress: replaying.\n");
10395 /* Pass keypress to event window and unfreeze keyboard queue. */
10396 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10398 /* Release freeze. */
10399 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10403 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
10407 keyrelease(xcb_key_release_event_t
*e
)
10410 struct binding
*bp
;
10411 xcb_keysym_t keysym
;
10413 last_event_time
= e
->time
;
10415 keysym
= xcb_key_release_lookup_keysym(syms
, e
, 0);
10417 DNPRINTF(SWM_D_EVENT
, "keyrelease: keysym: %u, win (x,y): %#x (%d,%d), "
10418 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10419 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
10420 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
10421 e
->child
, e
->state
, YESNO(e
->same_screen
));
10423 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10425 /* Look for catch-all. */
10426 bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
);
10428 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10429 bp
->flags
& BINDING_F_REPLAY
) {
10430 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10431 DNPRINTF(SWM_D_EVENT
, "keyrelease: replaying.\n");
10433 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10434 DNPRINTF(SWM_D_EVENT
, "keyrelease: unfreezing.\n");
10439 DNPRINTF(SWM_D_EVENT
, "keyrelease: done.\n");
10443 buttonpress(xcb_button_press_event_t
*e
)
10445 struct ws_win
*win
= NULL
, *newf
;
10446 struct swm_region
*r
, *old_r
;
10448 struct binding
*bp
;
10449 bool replay
= true;
10451 last_event_time
= e
->time
;
10453 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): %#x (%d,%d), "
10454 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10455 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10456 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10457 e
->state
, YESNO(e
->same_screen
));
10459 if (e
->event
== e
->root
) {
10461 win
= find_window(e
->child
);
10463 /* Focus on empty region */
10464 /* If no windows on region if its empty. */
10465 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10466 if (r
&& TAILQ_EMPTY(&r
->ws
->winlist
)) {
10467 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
10468 if (old_r
&& old_r
!= r
)
10469 unfocus_win(old_r
->ws
->focus
);
10471 DNPRINTF(SWM_D_FOCUS
, "buttonpress: "
10472 "set_input_focus: %#x, revert-to: parent, "
10473 "time: %#x\n", e
->root
, e
->time
);
10474 xcb_set_input_focus(conn
,
10475 XCB_INPUT_FOCUS_POINTER_ROOT
,
10478 /* Clear bar since empty. */
10481 /* No need to replay event. */
10486 win
= find_window(e
->event
);
10490 newf
= get_focus_magic(win
);
10491 if (win
->ws
->focus
== newf
&& newf
!= win
) {
10492 if (win
->focus_child
== win
)
10493 win
->focus_child
= NULL
;
10499 /* Handle any bound action. */
10500 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10502 /* Look for catch-all. */
10503 if ((bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
)) == NULL
)
10508 replay
= bp
->flags
& BINDING_F_REPLAY
;
10510 if ((ap
= &actions
[bp
->action
]) == NULL
)
10513 if (bp
->action
== FN_SPAWN_CUSTOM
)
10514 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10517 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10519 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10523 DNPRINTF(SWM_D_EVENT
, "buttonpress: replaying.\n");
10524 /* Replay event to event window */
10525 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10527 /* Unfreeze grab events. */
10528 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10535 buttonrelease(xcb_button_release_event_t
*e
)
10538 struct binding
*bp
;
10540 last_event_time
= e
->time
;
10542 DNPRINTF(SWM_D_EVENT
, "buttonrelease: win (x,y): %#x (%d,%d), "
10543 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10544 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10545 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10546 e
->state
, YESNO(e
->same_screen
));
10548 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10550 /* Look for catch-all. */
10551 bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
);
10553 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10554 bp
->flags
& BINDING_F_REPLAY
) {
10555 DNPRINTF(SWM_D_EVENT
, "buttonrelease: replaying.\n");
10556 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10558 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10566 get_win_input_model(struct ws_win
*win
)
10570 * Input Model Input Field WM_TAKE_FOCUS
10571 * No Input False Absent
10572 * Passive True Absent
10573 * Locally Active True Present
10574 * Globally Active False Present
10577 if (ACCEPTS_FOCUS(win
))
10578 inputmodel
= (win
->take_focus
) ? "Locally Active" : "Passive";
10580 inputmodel
= (win
->take_focus
) ? "Globally Active" : "No Input";
10586 print_win_geom(xcb_window_t w
)
10588 xcb_get_geometry_reply_t
*wa
;
10590 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
10592 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: %#x\n",
10597 DNPRINTF(SWM_D_MISC
, "print_win_geom: win %#x, root: %#x, "
10598 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
10599 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
10608 get_stack_mode_name(uint8_t mode
)
10613 case XCB_STACK_MODE_ABOVE
:
10616 case XCB_STACK_MODE_BELOW
:
10619 case XCB_STACK_MODE_TOP_IF
:
10622 case XCB_STACK_MODE_BOTTOM_IF
:
10625 case XCB_STACK_MODE_OPPOSITE
:
10637 configurerequest(xcb_configure_request_event_t
*e
)
10639 struct ws_win
*win
;
10640 struct swm_region
*r
= NULL
;
10642 uint32_t wc
[7] = {0};
10646 if ((win
= find_window(e
->window
)) == NULL
)
10647 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10651 if (swm_debug
& SWM_D_EVENT
) {
10652 print_win_geom(e
->window
);
10654 DNPRINTF(SWM_D_EVENT
, "configurerequest: win %#x, "
10655 "parent: %#x, new: %s, value_mask: %u { ", e
->window
,
10656 e
->parent
, YESNO(new), e
->value_mask
);
10657 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
10658 DPRINTF("X: %d ", e
->x
);
10659 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
10660 DPRINTF("Y: %d ", e
->y
);
10661 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10662 DPRINTF("W: %u ", e
->width
);
10663 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10664 DPRINTF("H: %u ", e
->height
);
10665 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
10666 DPRINTF("Border: %u ", e
->border_width
);
10667 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
10668 DPRINTF("Sibling: %#x ", e
->sibling
);
10669 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
10670 DPRINTF("StackMode: %s(%u) ",
10671 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
10677 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10678 mask
|= XCB_CONFIG_WINDOW_X
;
10681 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10682 mask
|= XCB_CONFIG_WINDOW_Y
;
10685 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
10686 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
10687 wc
[i
++] = e
->width
;
10689 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
10690 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
10691 wc
[i
++] = e
->height
;
10693 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
10694 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
10695 wc
[i
++] = e
->border_width
;
10697 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
10698 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
10699 wc
[i
++] = e
->sibling
;
10701 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
10702 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
10703 wc
[i
++] = e
->stack_mode
;
10707 xcb_configure_window(conn
, e
->window
, mask
, wc
);
10710 } else if ((!MANUAL(win
) || win
->quirks
& SWM_Q_ANYWHERE
) &&
10711 !FULLSCREEN(win
) && !MAXIMIZED(win
)) {
10714 else if (win
->ws
->old_r
)
10715 r
= win
->ws
->old_r
;
10717 /* windows are centered unless ANYWHERE quirk is set. */
10718 if (win
->quirks
& SWM_Q_ANYWHERE
) {
10719 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10720 win
->g_float
.x
= e
->x
;
10722 win
->g_float
.x
-= X(r
);
10725 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10726 win
->g_float
.y
= e
->y
;
10728 win
->g_float
.y
-= Y(r
);
10732 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10733 win
->g_float
.w
= e
->width
;
10735 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10736 win
->g_float
.h
= e
->height
;
10738 win
->g_floatvalid
= true;
10740 if (!MAXIMIZED(win
) && !FULLSCREEN(win
) &&
10741 (TRANS(win
) || (ABOVE(win
) &&
10742 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]))) {
10743 WIDTH(win
) = win
->g_float
.w
;
10744 HEIGHT(win
) = win
->g_float
.h
;
10747 update_floater(win
);
10750 config_win(win
, e
);
10754 config_win(win
, e
);
10758 config_win(win
, e
);
10762 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
10766 configurenotify(xcb_configure_notify_event_t
*e
)
10768 struct ws_win
*win
;
10770 DNPRINTF(SWM_D_EVENT
, "configurenotify: win %#x, event win: %#x, "
10771 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
10772 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
10773 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
10774 YESNO(e
->override_redirect
));
10776 win
= find_window(e
->window
);
10779 if (font_adjusted
&& win
->ws
->r
) {
10787 destroynotify(xcb_destroy_notify_event_t
*e
)
10789 struct ws_win
*win
;
10790 struct workspace
*ws
;
10792 DNPRINTF(SWM_D_EVENT
, "destroynotify: win %#x\n", e
->window
);
10794 if ((win
= find_window(e
->window
)) == NULL
) {
10795 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10797 /* Window is on unmanaged list. */
10798 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10805 if (win
->frame
== e
->window
) {
10806 DNPRINTF(SWM_D_EVENT
, "destroynotify: frame for win %#x\n",
10808 win
->frame
= XCB_WINDOW_NONE
;
10812 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10813 /* If we were focused, make sure we focus on something else. */
10814 if (win
== ws
->focus
) {
10815 ws
->focus_pending
= get_focus_prev(win
);
10816 if (ws
->focus_pending
== win
)
10817 ws
->focus_pending
= NULL
;
10821 unmanage_window(win
);
10822 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10826 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
10827 if (ws
->focus_pending
) {
10828 focus_win(ws
->focus_pending
);
10829 ws
->focus_pending
= NULL
;
10830 } else if (ws
->focus
== NULL
) {
10831 DNPRINTF(SWM_D_FOCUS
, "destroynotify: set_input_focus: "
10832 "%#x, revert-to: parent, time: 0\n", ws
->r
->id
);
10833 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
10834 ws
->r
->id
, XCB_CURRENT_TIME
);
10841 DNPRINTF(SWM_D_EVENT
, "destroynotify: done.\n");
10846 get_notify_detail_label(uint8_t detail
)
10851 case XCB_NOTIFY_DETAIL_ANCESTOR
:
10852 label
= "Ancestor";
10854 case XCB_NOTIFY_DETAIL_VIRTUAL
:
10857 case XCB_NOTIFY_DETAIL_INFERIOR
:
10858 label
= "Inferior";
10860 case XCB_NOTIFY_DETAIL_NONLINEAR
:
10861 label
= "Nonlinear";
10863 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
10864 label
= "NonlinearVirtual";
10866 case XCB_NOTIFY_DETAIL_POINTER
:
10869 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
10870 label
= "PointerRoot";
10872 case XCB_NOTIFY_DETAIL_NONE
:
10883 get_notify_mode_label(uint8_t mode
)
10888 case XCB_NOTIFY_MODE_NORMAL
:
10891 case XCB_NOTIFY_MODE_GRAB
:
10894 case XCB_NOTIFY_MODE_UNGRAB
:
10897 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
10898 label
= "WhileGrabbed";
10908 get_state_mask_label(uint16_t state
)
10913 case XCB_KEY_BUT_MASK_SHIFT
:
10914 label
= "ShiftMask";
10916 case XCB_KEY_BUT_MASK_LOCK
:
10917 label
= "LockMask";
10919 case XCB_KEY_BUT_MASK_CONTROL
:
10920 label
= "ControlMask";
10922 case XCB_KEY_BUT_MASK_MOD_1
:
10923 label
= "Mod1Mask";
10925 case XCB_KEY_BUT_MASK_MOD_2
:
10926 label
= "Mod2Mask";
10928 case XCB_KEY_BUT_MASK_MOD_3
:
10929 label
= "Mod3Mask";
10931 case XCB_KEY_BUT_MASK_MOD_4
:
10932 label
= "Mod4Mask";
10934 case XCB_KEY_BUT_MASK_MOD_5
:
10935 label
= "Mod5Mask";
10937 case XCB_KEY_BUT_MASK_BUTTON_1
:
10938 label
= "Button1Mask";
10940 case XCB_KEY_BUT_MASK_BUTTON_2
:
10941 label
= "Button2Mask";
10943 case XCB_KEY_BUT_MASK_BUTTON_3
:
10944 label
= "Button3Mask";
10946 case XCB_KEY_BUT_MASK_BUTTON_4
:
10947 label
= "Button4Mask";
10949 case XCB_KEY_BUT_MASK_BUTTON_5
:
10950 label
= "Button5Mask";
10964 enternotify(xcb_enter_notify_event_t
*e
)
10966 struct ws_win
*win
;
10967 struct swm_region
*r
;
10969 last_event_time
= e
->time
;
10971 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): %#x "
10972 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10973 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10974 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10975 get_notify_mode_label(e
->mode
), e
->mode
,
10976 get_notify_detail_label(e
->detail
), e
->detail
,
10977 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10978 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10981 if (e
->event
== e
->root
&& e
->child
== XCB_WINDOW_NONE
&&
10982 e
->mode
== XCB_NOTIFY_MODE_GRAB
&&
10983 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10984 DNPRINTF(SWM_D_EVENT
, "enternotify: grab inferior; ignoring.\n");
10988 if (focus_mode
== SWM_FOCUS_MANUAL
&&
10989 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10990 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
10994 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10995 e
->mode
== XCB_NOTIFY_MODE_UNGRAB
&&
10996 e
->detail
!= XCB_NOTIFY_DETAIL_ANCESTOR
) {
10997 DNPRINTF(SWM_D_EVENT
, "enternotify: ungrab; ignoring.\n");
11001 if ((win
= find_window(e
->event
)) == NULL
) {
11002 if (e
->event
== e
->root
) {
11003 /* If no windows on pointer region, then focus root. */
11004 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
11006 DNPRINTF(SWM_D_EVENT
, "enternotify: "
11007 "NULL region; ignoring.\n");
11013 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
11018 if (e
->mode
== XCB_NOTIFY_MODE_NORMAL
&&
11019 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
11020 DNPRINTF(SWM_D_EVENT
, "enternotify: entering from "
11021 "inferior; ignoring\n");
11025 focus_win(get_focus_magic(win
));
11028 DNPRINTF(SWM_D_EVENT
, "enternotify: done\n");
11035 leavenotify(xcb_leave_notify_event_t
*e
)
11037 last_event_time
= e
->time
;
11039 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): %#x "
11040 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
11041 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
11042 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
11043 get_notify_mode_label(e
->mode
), e
->mode
,
11044 get_notify_detail_label(e
->detail
), e
->detail
,
11045 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
11046 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
11052 mapnotify(xcb_map_notify_event_t
*e
)
11054 struct ws_win
*win
, *parent
= NULL
;
11055 struct workspace
*ws
;
11057 DNPRINTF(SWM_D_EVENT
, "mapnotify: win %#x\n", e
->window
);
11059 if ((win
= manage_window(e
->window
, spawn_position
, false)) == NULL
)
11063 if (win
->state
== SWM_WIN_STATE_REPARENTING
)
11066 if (ws
->r
== NULL
) {
11071 /* Need to know if win was mapped due to ws switch. */
11072 if (ws
->state
== SWM_WS_STATE_MAPPED
) {
11073 if (ws
->focus_pending
&& TRANS(ws
->focus_pending
))
11074 parent
= find_window(win
->transient
);
11076 /* If window's parent is maximized, don't clear it. */
11077 if ((parent
== NULL
) || !MAXIMIZED(parent
))
11078 if (clear_maximized(ws
) > 0)
11082 win
->mapped
= true;
11083 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
11085 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
11086 if (ws
->focus_pending
== win
) {
11088 ws
->focus_pending
= NULL
;
11089 center_pointer(ws
->r
);
11095 DNPRINTF(SWM_D_EVENT
, "mapnotify: done\n");
11101 mappingnotify(xcb_mapping_notify_event_t
*e
)
11103 if (e
->request
!= XCB_MAPPING_POINTER
) {
11104 xcb_refresh_keyboard_mapping(syms
, e
);
11112 maprequest(xcb_map_request_event_t
*e
)
11114 struct ws_win
*win
, *w
= NULL
;
11116 DNPRINTF(SWM_D_EVENT
, "maprequest: win %#x\n",
11119 win
= manage_window(e
->window
, spawn_position
, true);
11123 /* The new window should get focus; prepare. */
11124 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
11125 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
) && ACCEPTS_FOCUS(win
)) {
11126 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
11127 /* See if other wins of same type are already mapped. */
11128 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
11129 if (w
== win
|| !w
->mapped
)
11132 if (w
->ch
.class_name
&&
11133 win
->ch
.class_name
&&
11134 strcmp(w
->ch
.class_name
,
11135 win
->ch
.class_name
) == 0 &&
11136 w
->ch
.instance_name
&&
11137 win
->ch
.instance_name
&&
11138 strcmp(w
->ch
.instance_name
,
11139 win
->ch
.instance_name
) == 0)
11145 win
->ws
->focus_pending
= get_focus_magic(win
);
11148 /* All windows need to be mapped if they are in the current workspace.*/
11151 /* Ignore EnterNotify to handle the mapnotify without interference. */
11152 if (focus_mode
== SWM_FOCUS_DEFAULT
)
11153 event_drain(XCB_ENTER_NOTIFY
);
11155 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
11159 motionnotify(xcb_motion_notify_event_t
*e
)
11161 struct swm_region
*r
= NULL
;
11162 int i
, num_screens
;
11164 last_event_time
= e
->time
;
11166 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): %#x "
11167 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
11168 "child: %#x, same_screen_focus: %s, state: %d\n",
11169 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
11170 get_notify_detail_label(e
->detail
), e
->detail
,
11171 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
11172 YESNO(e
->same_screen
), e
->state
);
11174 if (focus_mode
== SWM_FOCUS_MANUAL
)
11177 num_screens
= get_screen_count();
11178 for (i
= 0; i
< num_screens
; i
++)
11179 if (screens
[i
].root
== e
->root
)
11182 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11183 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
11184 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
11192 get_atom_name(xcb_atom_t atom
)
11195 #ifdef SWM_DEBUG_ATOM_NAMES
11197 * This should be disabled during most debugging since
11198 * xcb_get_* causes an xcb_flush.
11201 xcb_get_atom_name_reply_t
*r
;
11203 r
= xcb_get_atom_name_reply(conn
,
11204 xcb_get_atom_name(conn
, atom
),
11207 len
= xcb_get_atom_name_name_length(r
);
11209 name
= malloc(len
+ 1);
11211 memcpy(name
, xcb_get_atom_name_name(r
), len
);
11225 propertynotify(xcb_property_notify_event_t
*e
)
11227 struct ws_win
*win
;
11228 struct workspace
*ws
;
11232 name
= get_atom_name(e
->atom
);
11233 DNPRINTF(SWM_D_EVENT
, "propertynotify: win %#x, atom: %s(%u), "
11234 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
11238 last_event_time
= e
->time
;
11240 win
= find_window(e
->window
);
11246 if (e
->atom
== a_state
) {
11247 /* State just changed, make sure it gets focused if mapped. */
11248 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
11249 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
11251 win
->state
== SWM_WIN_STATE_REPARENTED
&&
11252 ws
->focus_pending
== win
) {
11253 focus_win(ws
->focus_pending
);
11254 ws
->focus_pending
= NULL
;
11258 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
11259 e
->atom
== XCB_ATOM_WM_NAME
) {
11261 bar_draw(ws
->r
->bar
);
11262 } else if (e
->atom
== a_prot
) {
11263 get_wm_protocols(win
);
11264 } else if (e
->atom
== XCB_ATOM_WM_NORMAL_HINTS
) {
11265 xcb_icccm_get_wm_normal_hints_reply(conn
,
11266 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
11274 reparentnotify(xcb_reparent_notify_event_t
*e
)
11276 struct ws_win
*win
;
11278 DNPRINTF(SWM_D_EVENT
, "reparentnotify: event: %#x, win %#x, "
11279 "parent: %#x, (x,y): (%u,%u), override_redirect: %u\n",
11280 e
->event
, e
->window
, e
->parent
, e
->x
, e
->y
, e
->override_redirect
);
11282 win
= find_window(e
->window
);
11284 if (win
->state
== SWM_WIN_STATE_REPARENTING
) {
11285 win
->state
= SWM_WIN_STATE_REPARENTED
;
11287 if (win
->ws
->r
&& !ICONIC(win
))
11292 update_window(win
);
11293 update_win_stacking(win
);
11294 } else if (win
->state
== SWM_WIN_STATE_UNPARENTING
) {
11295 win
->state
= SWM_WIN_STATE_UNPARENTED
;
11301 unmapnotify(xcb_unmap_notify_event_t
*e
)
11303 struct ws_win
*win
;
11304 struct workspace
*ws
;
11306 DNPRINTF(SWM_D_EVENT
, "unmapnotify: win %#x\n", e
->window
);
11308 /* If we aren't managing the window, then ignore. */
11309 win
= find_window(e
->window
);
11310 if (win
== NULL
|| win
->id
!= e
->window
) {
11311 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore unmanaged.\n");
11315 /* Do nothing if already withdrawn. */
11316 if (!win
->mapped
&& !ICONIC(win
)) {
11317 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore withdrawn.\n");
11322 win
->mapped
= false;
11324 /* Ignore if reparenting-related. */
11325 if (win
->state
!= SWM_WIN_STATE_REPARENTED
) {
11326 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore not reparented.\n");
11330 /* If win was focused, make sure to focus on something else. */
11331 if (win
== ws
->focus
) {
11332 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
11333 ws
->focus_pending
= get_focus_prev(win
);
11334 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
11335 "focus_pending: %#x\n",
11336 WINID(ws
->focus_pending
));
11344 DNPRINTF(SWM_D_EVENT
, "unmapnotify: iconify.\n");
11345 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
11348 DNPRINTF(SWM_D_EVENT
, "unmapnotify: withdraw.\n");
11349 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
11350 unmanage_window(win
);
11355 /* Update focus if ws is active. */
11356 if (WS_FOCUSED(ws
)) {
11357 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
11358 focus_win(get_pointer_win(ws
->r
->s
->root
));
11359 } else if (ws
->focus_pending
) {
11360 focus_win(ws
->focus_pending
);
11361 ws
->focus_pending
= NULL
;
11362 } else if (ws
->focus
== NULL
) {
11363 DNPRINTF(SWM_D_FOCUS
, "unmapnotify: set_input_focus: "
11364 "%#x, revert-to: parent, time: 0\n",
11366 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11367 ws
->r
->id
, XCB_CURRENT_TIME
);
11371 center_pointer(ws
->r
);
11373 DNPRINTF(SWM_D_EVENT
, "unmapnotify: done.\n");
11378 get_source_type_label(uint32_t type
)
11383 case EWMH_SOURCE_TYPE_NONE
:
11386 case EWMH_SOURCE_TYPE_NORMAL
:
11389 case EWMH_SOURCE_TYPE_OTHER
:
11401 clientmessage(xcb_client_message_event_t
*e
)
11403 struct ws_win
*win
;
11404 struct swm_region
*r
= NULL
;
11407 int num_screens
, i
;
11408 xcb_map_request_event_t mre
;
11412 name
= get_atom_name(e
->type
);
11413 DNPRINTF(SWM_D_EVENT
, "clientmessage: win %#x, atom: %s(%u)\n",
11414 e
->window
, name
, e
->type
);
11418 if (e
->type
== ewmh
[_NET_CURRENT_DESKTOP
].atom
) {
11419 num_screens
= get_screen_count();
11420 for (i
= 0; i
< num_screens
; i
++)
11421 if (screens
[i
].root
== e
->window
) {
11422 r
= screens
[i
].r_focus
;
11426 if (r
&& e
->data
.data32
[0] < (uint32_t)workspace_limit
) {
11427 a
.id
= e
->data
.data32
[0];
11428 switchws(NULL
, r
, &a
);
11433 } else if (e
->type
== ewmh
[_NET_REQUEST_FRAME_EXTENTS
].atom
) {
11434 DNPRINTF(SWM_D_EVENT
,
11435 "clientmessage: set _NET_FRAME_EXTENTS on window.\n");
11436 vals
[0] = vals
[1] = vals
[2] = vals
[3] = border_width
;
11437 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, e
->window
,
11438 a_net_frame_extents
, XCB_ATOM_CARDINAL
, 32, 4, vals
);
11443 win
= find_window(e
->window
);
11445 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11446 /* Manage the window with maprequest. */
11447 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
11448 "unmanaged window.\n");
11449 mre
.window
= e
->window
;
11455 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11456 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW, "
11457 "source_type: %s(%d)\n",
11458 get_source_type_label(e
->data
.data32
[0]),
11459 e
->data
.data32
[0]);
11462 * Allow focus changes that are a result of direct user
11463 * action and from applications that use the old EWMH spec.
11465 if (e
->data
.data32
[0] != EWMH_SOURCE_TYPE_NORMAL
||
11466 win
->quirks
& SWM_Q_OBEYAPPFOCUSREQ
) {
11467 if (WS_FOCUSED(win
->ws
))
11470 win
->ws
->focus_pending
= win
;
11472 } else if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
11473 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
11474 if (win
->can_delete
)
11475 client_msg(win
, a_delete
, 0);
11477 xcb_kill_client(conn
, win
->id
);
11478 } else if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
11479 DNPRINTF(SWM_D_EVENT
,
11480 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
11482 if (e
->data
.data32
[0] & (1<<8)) /* x */
11483 X(win
) = e
->data
.data32
[1];
11484 if (e
->data
.data32
[0] & (1<<9)) /* y */
11485 Y(win
) = e
->data
.data32
[2];
11486 if (e
->data
.data32
[0] & (1<<10)) /* width */
11487 WIDTH(win
) = e
->data
.data32
[3];
11488 if (e
->data
.data32
[0] & (1<<11)) /* height */
11489 HEIGHT(win
) = e
->data
.data32
[4];
11491 update_window(win
);
11493 /* Notify no change was made. */
11494 config_win(win
, NULL
);
11495 /* TODO: Change stack sizes */
11497 } else if (e
->type
== ewmh
[_NET_RESTACK_WINDOW
].atom
) {
11498 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_RESTACK_WINDOW\n");
11499 vals
[0] = e
->data
.data32
[1]; /* Sibling window. */
11500 vals
[1] = e
->data
.data32
[2]; /* Stack mode detail. */
11502 if (win
->frame
!= XCB_WINDOW_NONE
)
11503 xcb_configure_window(conn
, win
->frame
,
11504 XCB_CONFIG_WINDOW_SIBLING
|
11505 XCB_CONFIG_WINDOW_STACK_MODE
, vals
);
11506 } else if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
11507 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
11508 ewmh_change_wm_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
11509 if (e
->data
.data32
[2])
11510 ewmh_change_wm_state(win
, e
->data
.data32
[2],
11511 e
->data
.data32
[0]);
11513 ewmh_update_wm_state(win
);
11515 } else if (e
->type
== ewmh
[_NET_WM_DESKTOP
].atom
) {
11516 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_DESKTOP\n");
11519 win_to_ws(win
, e
->data
.data32
[0], true);
11521 /* Stack source and destination ws, if mapped. */
11522 if (r
!= win
->ws
->r
) {
11528 load_float_geom(win
);
11541 int errcode
= xcb_connection_has_error(conn
);
11542 #ifdef XCB_CONN_ERROR
11545 case XCB_CONN_ERROR
:
11546 s
= "Socket error, pipe error or other stream error.";
11548 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
11549 s
= "Extension not supported.";
11551 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
11552 s
= "Insufficient memory.";
11554 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
11555 s
= "Request length was exceeded.";
11557 case XCB_CONN_CLOSED_PARSE_ERR
:
11558 s
= "Error parsing display string.";
11561 s
= "Unknown error.";
11564 errx(errcode
, "X CONNECTION ERROR: %s", s
);
11567 errx(errcode
, "X CONNECTION ERROR");
11574 int num_screens
, i
;
11575 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
11576 XCB_EVENT_MASK_ENTER_WINDOW
|
11577 XCB_EVENT_MASK_OWNER_GRAB_BUTTON
|
11578 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
11579 XCB_EVENT_MASK_ENTER_WINDOW
|
11580 XCB_EVENT_MASK_LEAVE_WINDOW
|
11581 XCB_EVENT_MASK_BUTTON_PRESS
|
11582 XCB_EVENT_MASK_BUTTON_RELEASE
|
11583 XCB_EVENT_MASK_KEY_PRESS
|
11584 XCB_EVENT_MASK_KEY_RELEASE
|
11585 XCB_EVENT_MASK_PROPERTY_CHANGE
;
11587 xcb_void_cookie_t wac
;
11588 xcb_generic_error_t
*error
;
11590 /* this causes an error if some other window manager is running */
11591 num_screens
= get_screen_count();
11592 for (i
= 0; i
< num_screens
; i
++) {
11593 if ((sc
= get_screen(i
)) == NULL
)
11594 errx(1, "ERROR: can't get screen %d.", i
);
11595 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: %#x\n",
11597 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
11598 XCB_CW_EVENT_MASK
, &val
);
11599 if ((error
= xcb_request_check(conn
, wac
))) {
11600 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
11601 error
->error_code
);
11611 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
11613 struct swm_region
*r
= NULL
, *n
;
11614 struct workspace
*ws
= NULL
;
11618 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
11619 s
->idx
, w
, h
, x
, y
);
11621 /* remove any conflicting regions */
11622 n
= TAILQ_FIRST(&s
->rl
);
11625 n
= TAILQ_NEXT(r
, entry
);
11626 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
11627 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
11628 if (r
->ws
->r
!= NULL
)
11629 r
->ws
->old_r
= r
->ws
->r
;
11632 xcb_destroy_window(conn
, r
->id
);
11633 r
->id
= XCB_WINDOW_NONE
;
11634 TAILQ_REMOVE(&s
->rl
, r
, entry
);
11635 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
11639 /* search old regions for one to reuse */
11641 /* size + location match */
11642 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11643 if (X(r
) == x
&& Y(r
) == y
&&
11644 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11648 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11649 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11653 TAILQ_REMOVE(&s
->orl
, r
, entry
);
11654 /* try to use old region's workspace */
11655 if (r
->ws
->r
== NULL
)
11658 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
11659 err(1, "new_region: calloc: failed to allocate memory "
11662 /* if we don't have a workspace already, find one */
11664 for (i
= 0; i
< workspace_limit
; i
++)
11665 if (s
->ws
[i
].r
== NULL
) {
11672 errx(1, "new_region: no free workspaces");
11674 if (ws
->state
== SWM_WS_STATE_HIDDEN
)
11675 ws
->state
= SWM_WS_STATE_MAPPING
;
11684 r
->ws_prior
= NULL
;
11687 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
11689 /* Invisible region window to detect pointer events on empty regions. */
11690 r
->id
= xcb_generate_id(conn
);
11691 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
11692 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
11694 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
11695 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
11696 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
11698 /* Make sure region input is at the bottom. */
11699 wa
[0] = XCB_STACK_MODE_BELOW
;
11700 xcb_configure_window(conn
, r
->id
, XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
11702 xcb_map_window(conn
, r
->id
);
11706 scan_randr(int idx
)
11708 #ifdef SWM_XRR_HAS_CRTC
11711 #endif /* SWM_XRR_HAS_CRTC */
11712 struct swm_region
*r
;
11714 xcb_randr_get_screen_resources_current_cookie_t src
;
11715 xcb_randr_get_screen_resources_current_reply_t
*srr
;
11716 xcb_randr_get_crtc_info_cookie_t cic
;
11717 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
11718 xcb_randr_crtc_t
*crtc
;
11719 xcb_screen_t
*screen
;
11721 DNPRINTF(SWM_D_MISC
, "scan_randr: screen: %d\n", idx
);
11723 if ((screen
= get_screen(idx
)) == NULL
)
11724 errx(1, "ERROR: can't get screen %d.", idx
);
11726 num_screens
= get_screen_count();
11727 if (idx
>= num_screens
)
11728 errx(1, "scan_randr: invalid screen");
11730 /* remove any old regions */
11731 while ((r
= TAILQ_FIRST(&screens
[idx
].rl
)) != NULL
) {
11732 r
->ws
->old_r
= r
->ws
->r
= NULL
;
11734 xcb_destroy_window(conn
, r
->id
);
11735 r
->id
= XCB_WINDOW_NONE
;
11736 TAILQ_REMOVE(&screens
[idx
].rl
, r
, entry
);
11737 TAILQ_INSERT_TAIL(&screens
[idx
].orl
, r
, entry
);
11741 /* map virtual screens onto physical screens */
11742 #ifdef SWM_XRR_HAS_CRTC
11743 if (randr_support
) {
11744 src
= xcb_randr_get_screen_resources_current(conn
,
11745 screens
[idx
].root
);
11746 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
11749 new_region(&screens
[idx
], 0, 0,
11750 screen
->width_in_pixels
,
11751 screen
->height_in_pixels
);
11754 ncrtc
= srr
->num_crtcs
;
11756 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
11757 for (c
= 0; c
< ncrtc
; c
++) {
11758 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
11760 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
11763 if (cir
->num_outputs
== 0) {
11768 if (cir
->mode
== 0)
11769 new_region(&screens
[idx
], 0, 0,
11770 screen
->width_in_pixels
,
11771 screen
->height_in_pixels
);
11773 new_region(&screens
[idx
],
11774 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
11779 #endif /* SWM_XRR_HAS_CRTC */
11781 /* If detection failed, create a single region that spans the screen. */
11782 if (TAILQ_EMPTY(&screens
[idx
].rl
))
11783 new_region(&screens
[idx
], 0, 0, screen
->width_in_pixels
,
11784 screen
->height_in_pixels
);
11787 /* The screen shouldn't focus on unused regions. */
11788 TAILQ_FOREACH(r
, &screens
[idx
].orl
, entry
) {
11789 if (screens
[idx
].r_focus
== r
)
11790 screens
[idx
].r_focus
= NULL
;
11793 DNPRINTF(SWM_D_MISC
, "scan_randr: done.\n");
11797 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
11799 struct swm_region
*r
;
11800 struct workspace
*ws
;
11801 struct ws_win
*win
;
11802 int i
, j
, num_screens
;
11804 DNPRINTF(SWM_D_EVENT
, "screenchange: root: %#x\n", e
->root
);
11806 num_screens
= get_screen_count();
11807 /* silly event doesn't include the screen index */
11808 for (i
= 0; i
< num_screens
; i
++)
11809 if (screens
[i
].root
== e
->root
)
11811 if (i
>= num_screens
)
11812 errx(1, "screenchange: screen not found");
11814 /* brute force for now, just re-enumerate the regions */
11818 print_win_geom(e
->root
);
11820 /* add bars to all regions */
11821 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11824 /* Stack all regions. */
11825 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11828 /* Make sure a region has focus. */
11829 if (screens
[i
].r_focus
== NULL
) {
11830 r
= TAILQ_FIRST(&screens
[i
].rl
);
11835 /* Cleanup unused previously visible workspaces. */
11836 for (j
= 0; j
< workspace_limit
; j
++) {
11837 ws
= &screens
[i
].ws
[j
];
11838 if (ws
->r
== NULL
&& ws
->state
!= SWM_WS_STATE_HIDDEN
) {
11839 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
11841 ws
->state
= SWM_WS_STATE_HIDDEN
;
11847 /* Update workspace state and bar on all regions. */
11848 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11849 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
11857 struct swm_region
*r
= NULL
;
11858 xcb_query_tree_cookie_t qtc
;
11859 xcb_query_tree_reply_t
*qtr
;
11860 xcb_get_property_cookie_t pc
;
11861 xcb_get_property_reply_t
*pr
;
11862 xcb_window_t
*wins
= NULL
, trans
, *cwins
= NULL
;
11863 int i
, j
, k
, n
, no
, num_screens
;
11865 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
11866 num_screens
= get_screen_count();
11867 for (i
= 0; i
< num_screens
; i
++) {
11868 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
11869 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
11872 wins
= xcb_query_tree_children(qtr
);
11873 no
= xcb_query_tree_children_length(qtr
);
11875 /* Try to sort windows according to _NET_CLIENT_LIST. */
11876 pr
= xcb_get_property_reply(conn
, xcb_get_property(conn
, 0,
11877 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
11878 XCB_ATOM_WINDOW
, 0, UINT32_MAX
), NULL
);
11880 cwins
= xcb_get_property_value(pr
);
11881 n
= xcb_get_property_value_length(pr
) /
11882 sizeof(xcb_atom_t
);
11884 for (j
= 0; j
< n
; ++j
) {
11885 for (k
= j
; k
< no
; ++k
) {
11886 if (wins
[k
] == cwins
[j
]) {
11887 /* Swap wins j and k. */
11889 wins
[j
] = cwins
[j
];
11897 /* Manage top-level windows first, then transients. */
11898 /* TODO: allow transients to be managed before leader. */
11899 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top-level windows.\n");
11900 for (j
= 0; j
< no
; j
++) {
11901 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11902 if (r
->id
== wins
[j
]) {
11903 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11904 "skip %#x; region input window.\n",
11907 } else if (r
->bar
->id
== wins
[j
]) {
11908 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11909 "skip %#x; region bar.\n",
11918 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11919 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11921 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
11922 "is transient for %#x.\n", wins
[j
], trans
);
11926 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11929 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
11930 for (j
= 0; j
< no
; j
++) {
11931 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11932 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11934 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11938 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
11942 setup_screens(void)
11944 int i
, j
, k
, num_screens
;
11945 struct workspace
*ws
;
11946 uint32_t gcv
[1], wa
[1];
11947 const xcb_query_extension_reply_t
*qep
;
11948 xcb_screen_t
*screen
;
11949 xcb_randr_query_version_cookie_t c
;
11950 xcb_randr_query_version_reply_t
*r
;
11952 num_screens
= get_screen_count();
11953 if ((screens
= calloc(num_screens
, sizeof(struct swm_screen
))) == NULL
)
11954 err(1, "setup_screens: calloc: failed to allocate memory for "
11957 /* Initial RandR setup. */
11958 randr_support
= false;
11959 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
11960 if (qep
->present
) {
11961 c
= xcb_randr_query_version(conn
, 1, 1);
11962 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
11964 if (r
->major_version
>= 1) {
11965 randr_support
= true;
11966 randr_eventbase
= qep
->first_event
;
11972 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
11974 /* map physical screens */
11975 for (i
= 0; i
< num_screens
; i
++) {
11976 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
11977 screens
[i
].idx
= i
;
11978 screens
[i
].r_focus
= NULL
;
11980 TAILQ_INIT(&screens
[i
].rl
);
11981 TAILQ_INIT(&screens
[i
].orl
);
11982 if ((screen
= get_screen(i
)) == NULL
)
11983 errx(1, "ERROR: can't get screen %d.", i
);
11984 screens
[i
].root
= screen
->root
;
11986 /* set default colors */
11987 setscreencolor("red", i
, SWM_S_COLOR_FOCUS
);
11988 setscreencolor("rgb:88/88/88", i
, SWM_S_COLOR_UNFOCUS
);
11989 setscreencolor("rgb:00/80/80", i
, SWM_S_COLOR_BAR_BORDER
);
11990 setscreencolor("rgb:00/40/40", i
,
11991 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
11992 setscreencolor("black", i
, SWM_S_COLOR_BAR
);
11993 setscreencolor("rgb:a0/a0/a0", i
, SWM_S_COLOR_BAR_FONT
);
11994 setscreencolor("red", i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
11995 setscreencolor("rgb:88/88/88", i
,
11996 SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
11998 /* create graphics context on screen */
11999 screens
[i
].bar_gc
= xcb_generate_id(conn
);
12001 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
12002 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
12004 /* set default cursor */
12005 xcb_change_window_attributes(conn
, screens
[i
].root
,
12006 XCB_CW_CURSOR
, wa
);
12008 /* init all workspaces */
12009 /* XXX these should be dynamically allocated too */
12010 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
12011 ws
= &screens
[i
].ws
[j
];
12014 ws
->bar_enabled
= true;
12016 ws
->focus_prev
= NULL
;
12017 ws
->focus_pending
= NULL
;
12018 ws
->focus_raise
= NULL
;
12021 ws
->state
= SWM_WS_STATE_HIDDEN
;
12022 TAILQ_INIT(&ws
->stack
);
12023 TAILQ_INIT(&ws
->winlist
);
12024 TAILQ_INIT(&ws
->unmanagedlist
);
12026 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
12027 layouts
[k
].l_config(ws
, SWM_ARG_ID_STACKINIT
);
12028 ws
->cur_layout
= &layouts
[0];
12029 ws
->cur_layout
->l_string(ws
);
12035 xcb_randr_select_input(conn
, screens
[i
].root
,
12036 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
12041 setup_globals(void)
12043 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
12044 err(1, "setup_globals: strdup: failed to allocate memory.");
12046 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
12047 err(1, "setup_globals: strdup: failed to allocate memory.");
12049 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
12050 errx(1, "unable to allocate key symbols");
12052 a_state
= get_atom_from_string("WM_STATE");
12053 a_prot
= get_atom_from_string("WM_PROTOCOLS");
12054 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
12055 a_net_frame_extents
= get_atom_from_string("_NET_FRAME_EXTENTS");
12056 a_net_supported
= get_atom_from_string("_NET_SUPPORTED");
12057 a_net_wm_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
12058 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
12059 a_utf8_string
= get_atom_from_string("UTF8_STRING");
12060 a_swm_ws
= get_atom_from_string("_SWM_WS");
12064 shutdown_cleanup(void)
12066 struct swm_region
*r
;
12068 struct workspace
*ws
;
12069 int i
, num_screens
;
12071 /* disable alarm because the following code may not be interrupted */
12073 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
12074 err(1, "can't disable alarm");
12087 num_screens
= get_screen_count();
12088 for (i
= 0; i
< num_screens
; ++i
) {
12091 DNPRINTF(SWM_D_FOCUS
, "shutdown_cleanup: set_input_focus: "
12092 "%#x, revert-to: root, time: 0\n", screens
[i
].root
);
12093 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
12094 screens
[i
].root
, XCB_CURRENT_TIME
);
12096 if (screens
[i
].bar_gc
!= XCB_NONE
)
12097 xcb_free_gc(conn
, screens
[i
].bar_gc
);
12098 if (!bar_font_legacy
) {
12099 XftColorFree(display
, DefaultVisual(display
, i
),
12100 DefaultColormap(display
, i
), &bar_font_color
);
12101 XftColorFree(display
, DefaultVisual(display
, i
),
12102 DefaultColormap(display
, i
), &search_font_color
);
12105 for (j
= 0; j
< SWM_S_COLOR_MAX
; ++j
) {
12106 free(screens
[i
].c
[j
].name
);
12109 /* Free window memory. */
12110 for (j
= 0; j
< SWM_WS_MAX
; ++j
) {
12111 ws
= &screens
[i
].ws
[j
];
12114 while ((w
= TAILQ_FIRST(&ws
->winlist
)) != NULL
) {
12115 TAILQ_REMOVE(&ws
->winlist
, w
, entry
);
12119 while ((w
= TAILQ_FIRST(&ws
->unmanagedlist
)) != NULL
) {
12120 TAILQ_REMOVE(&ws
->unmanagedlist
, w
, entry
);
12125 /* Free region memory. */
12126 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
12127 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
12132 while ((r
= TAILQ_FIRST(&screens
[i
].orl
)) != NULL
) {
12133 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
12142 free(clock_format
);
12143 free(startup_exception
);
12146 XFreeFontSet(display
, bar_fs
);
12148 XftFontClose(display
, bar_font
);
12150 xcb_key_symbols_free(syms
);
12152 xcb_aux_sync(conn
);
12153 xcb_disconnect(conn
);
12157 event_error(xcb_generic_error_t
*e
)
12161 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
12162 "resource_id: %u, minor_code: %u\n",
12163 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
12164 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
12165 e
->sequence
, e
->resource_id
, e
->minor_code
);
12169 event_handle(xcb_generic_event_t
*evt
)
12171 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
12173 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
12174 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
12175 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
12178 #define EVENT(type, callback) case type: callback((void *)evt); return
12179 EVENT(0, event_error
);
12180 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
12181 EVENT(XCB_BUTTON_RELEASE
, buttonrelease
);
12182 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
12183 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
12184 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
12185 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
12186 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
12187 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
12188 /*EVENT(XCB_CREATE_NOTIFY, );*/
12189 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
12190 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
12191 EVENT(XCB_EXPOSE
, expose
);
12192 EVENT(XCB_FOCUS_IN
, focusin
);
12194 EVENT(XCB_FOCUS_OUT
, focusout
);
12196 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
12197 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
12198 EVENT(XCB_KEY_PRESS
, keypress
);
12199 EVENT(XCB_KEY_RELEASE
, keyrelease
);
12200 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
12202 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
12204 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
12205 EVENT(XCB_MAP_REQUEST
, maprequest
);
12206 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
12207 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
12208 /*EVENT(XCB_NO_EXPOSURE, );*/
12209 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
12210 EVENT(XCB_REPARENT_NOTIFY
, reparentnotify
);
12211 /*EVENT(XCB_RESIZE_REQUEST, );*/
12212 /*EVENT(XCB_SELECTION_CLEAR, );*/
12213 /*EVENT(XCB_SELECTION_NOTIFY, );*/
12214 /*EVENT(XCB_SELECTION_REQUEST, );*/
12215 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
12216 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
12219 if (type
- randr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
12220 screenchange((void *)evt
);
12224 main(int argc
, char *argv
[])
12226 struct pollfd pfd
[2];
12227 struct sigaction sact
;
12229 struct passwd
*pwd
;
12230 struct swm_region
*r
;
12231 xcb_generic_event_t
*evt
;
12232 int xfd
, i
, num_screens
, num_readable
;
12233 char conf
[PATH_MAX
], *cfile
= NULL
;
12234 bool stdin_ready
= false, startup
= true;
12236 /* suppress unused warning since var is needed */
12240 time_started
= time(NULL
);
12244 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
12245 if (setlocale(LC_CTYPE
, "") == NULL
|| setlocale(LC_TIME
, "") == NULL
)
12246 warnx("no locale support");
12248 /* handle some signals */
12249 bzero(&sact
, sizeof(sact
));
12250 sigemptyset(&sact
.sa_mask
);
12252 sact
.sa_handler
= sighdlr
;
12253 sigaction(SIGINT
, &sact
, NULL
);
12254 sigaction(SIGQUIT
, &sact
, NULL
);
12255 sigaction(SIGTERM
, &sact
, NULL
);
12256 sigaction(SIGHUP
, &sact
, NULL
);
12258 sact
.sa_handler
= sighdlr
;
12259 sact
.sa_flags
= SA_NOCLDSTOP
;
12260 sigaction(SIGCHLD
, &sact
, NULL
);
12262 if ((display
= XOpenDisplay(0)) == NULL
)
12263 errx(1, "can not open display");
12265 conn
= XGetXCBConnection(display
);
12266 if (xcb_connection_has_error(conn
))
12267 errx(1, "can not get XCB connection");
12269 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
12271 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
12272 xfd
= xcb_get_file_descriptor(conn
);
12274 /* look for local and global conf file */
12275 pwd
= getpwuid(getuid());
12277 errx(1, "invalid user: %d", getuid());
12279 xcb_grab_server(conn
);
12280 xcb_aux_sync(conn
);
12282 /* flush all events */
12283 while ((evt
= get_next_event(false))) {
12284 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
12290 errx(1, "another window manager is currently running");
12292 /* Load Xcursors and/or cursorfont glyph cursors. */
12295 xcb_aux_sync(conn
);
12300 setup_keybindings();
12301 setup_btnbindings();
12306 for (i
= 0; ; i
++) {
12311 snprintf(conf
, sizeof conf
, "%s/.%s",
12312 pwd
->pw_dir
, SWM_CONF_FILE
);
12316 snprintf(conf
, sizeof conf
, "/etc/%s",
12321 snprintf(conf
, sizeof conf
, "%s/.%s",
12322 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
12325 /* global compat */
12326 snprintf(conf
, sizeof conf
, "/etc/%s",
12327 SWM_CONF_FILE_OLD
);
12333 if (strlen(conf
) && stat(conf
, &sb
) != -1)
12334 if (S_ISREG(sb
.st_mode
)) {
12341 /* load conf (if any) */
12343 conf_load(cfile
, SWM_CONF_DEFAULT
);
12347 if (getenv("SWM_STARTED") == NULL
)
12348 setenv("SWM_STARTED", "YES", 1);
12350 /* setup all bars */
12351 num_screens
= get_screen_count();
12352 for (i
= 0; i
< num_screens
; i
++)
12353 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12356 /* Manage existing windows. */
12362 /* Stack all regions to trigger mapping. */
12363 for (i
= 0; i
< num_screens
; i
++)
12364 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12367 xcb_ungrab_server(conn
);
12370 /* Update state and bar of each newly mapped workspace. */
12371 for (i
= 0; i
< num_screens
; i
++)
12372 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
12373 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
12377 memset(&pfd
, 0, sizeof(pfd
));
12379 pfd
[0].events
= POLLIN
;
12380 pfd
[1].fd
= STDIN_FILENO
;
12381 pfd
[1].events
= POLLIN
;
12384 while ((evt
= get_next_event(false))) {
12391 /* If just (re)started, set default focus if needed. */
12395 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
12396 r
= TAILQ_FIRST(&screens
[0].rl
);
12408 num_readable
= poll(pfd
, bar_extra
? 2 : 1, 1000);
12409 if (num_readable
== -1) {
12410 DNPRINTF(SWM_D_MISC
, "poll failed: %s",
12412 } else if (num_readable
> 0 && bar_extra
&&
12413 pfd
[1].revents
& POLLIN
) {
12414 stdin_ready
= true;
12418 restart(NULL
, NULL
, NULL
);
12424 stdin_ready
= false;
12425 bar_extra_update();
12428 /* Need to ensure the bar(s) are always updated. */
12429 for (i
= 0; i
< num_screens
; i
++)
12430 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12436 shutdown_cleanup();