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
) {
1744 load_float_geom(win
);
1745 else if (!MAXIMIZED(win
))
1746 store_float_geom(win
);
1748 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1749 changed
&= ~EWMH_F_MAXIMIZED
;
1753 if (changed
& EWMH_F_MAXIMIZED
) {
1754 /* VERT and/or HORZ changed. */
1756 if (!MAXIMIZED(win
))
1757 load_float_geom(win
);
1759 store_float_geom(win
);
1762 if (MAXIMIZED(win
)) {
1763 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
1764 ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1768 ws
->focus_pending
= win
;
1776 if (changed
& EWMH_F_FULLSCREEN
) {
1777 if (FULLSCREEN(win
)) {
1778 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
1782 ws
->focus_pending
= win
;
1785 load_float_geom(win
);
1788 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1792 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: done.\n");
1796 ewmh_update_wm_state(struct ws_win
*win
) {
1797 xcb_atom_t vals
[SWM_EWMH_ACTION_COUNT_MAX
];
1801 vals
[n
++] = ewmh
[_NET_WM_STATE_HIDDEN
].atom
;
1802 if (FULLSCREEN(win
))
1803 vals
[n
++] = ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
;
1804 if (MAXIMIZED_VERT(win
))
1805 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
;
1806 if (MAXIMIZED_HORZ(win
))
1807 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
;
1808 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1809 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
;
1810 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1811 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
;
1812 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1813 vals
[n
++] = ewmh
[_NET_WM_STATE_ABOVE
].atom
;
1814 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1815 vals
[n
++] = ewmh
[_SWM_WM_STATE_MANUAL
].atom
;
1818 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1819 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, n
, vals
);
1821 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1825 ewmh_get_wm_state(struct ws_win
*win
)
1828 xcb_get_property_cookie_t c
;
1829 xcb_get_property_reply_t
*r
;
1835 win
->ewmh_flags
= 0;
1837 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1838 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1839 r
= xcb_get_property_reply(conn
, c
, NULL
);
1843 states
= xcb_get_property_value(r
);
1844 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1846 for (i
= 0; i
< n
; i
++)
1847 ewmh_change_wm_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1855 dumpwins(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
1859 xcb_get_window_attributes_cookie_t c
;
1860 xcb_get_window_attributes_reply_t
*wa
;
1862 /* suppress unused warning since var is needed */
1866 if (r
->ws
== NULL
) {
1867 DPRINTF("dumpwins: invalid workspace\n");
1871 DPRINTF("=== managed window list ws %02d ===\n", r
->ws
->idx
);
1872 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
) {
1873 state
= get_win_state(w
->id
);
1874 c
= xcb_get_window_attributes(conn
, w
->id
);
1875 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1877 DPRINTF("win %#x (%#x), map_state: %d, state: %u, "
1878 "transient: %#x\n", w
->frame
, w
->id
, wa
->map_state
,
1879 state
, w
->transient
);
1882 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1886 DPRINTF("=== stacking order (top down) === \n");
1887 TAILQ_FOREACH(w
, &r
->ws
->stack
, stack_entry
) {
1888 DPRINTF("win %#x (%#x), fs: %s, maximized: %s, above: %s, "
1889 "iconic: %s\n", w
->frame
, w
->id
, YESNO(FULLSCREEN(w
)),
1890 YESNO(MAXIMIZED(w
)), YESNO(ABOVE(w
)), YESNO(ICONIC(w
)));
1893 DPRINTF("===== unmanaged window list =====\n");
1894 TAILQ_FOREACH(w
, &r
->ws
->unmanagedlist
, entry
) {
1895 state
= get_win_state(w
->id
);
1896 c
= xcb_get_window_attributes(conn
, w
->id
);
1897 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1899 DPRINTF("win %#x, map_state: %d, state: %u, "
1900 "transient: %#x\n", w
->id
, wa
->map_state
,
1901 state
, w
->transient
);
1904 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1908 DPRINTF("=================================\n");
1912 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
1915 int num_screens
, i
, j
;
1917 /* Suppress warnings. */
1922 DNPRINTF(SWM_D_MISC
, "debug_toggle\n");
1924 debug_enabled
= !debug_enabled
;
1926 num_screens
= get_screen_count();
1927 for (i
= 0; i
< num_screens
; i
++)
1928 for (j
= 0; j
< workspace_limit
; j
++)
1929 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1936 debug_refresh(struct ws_win
*win
)
1943 XRectangle l_ibox
, l_lbox
;
1944 xcb_rectangle_t rect
;
1946 uint32_t wc
[4], mask
, width
, height
, gcv
[1];
1949 xcb_screen_t
*screen
;
1951 if (debug_enabled
) {
1952 /* Create debug window if it doesn't exist. */
1953 if (win
->debug
== XCB_WINDOW_NONE
) {
1954 if ((screen
= get_screen(win
->s
->idx
)) == NULL
)
1955 errx(1, "ERROR: can't get screen %d.",
1958 win
->debug
= xcb_generate_id(conn
);
1959 wc
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1960 wc
[1] = win
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
1961 wc
[2] = screen
->default_colormap
;
1963 xcb_create_window(conn
, screen
->root_depth
, win
->debug
,
1964 win
->frame
, 0, 0, 10, 10, 1,
1965 XCB_WINDOW_CLASS_INPUT_OUTPUT
, screen
->root_visual
,
1966 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
|
1967 XCB_CW_COLORMAP
, wc
);
1969 xcb_map_window(conn
, win
->debug
);
1972 /* Determine workspace window list index. */
1974 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
1980 /* Determine stacking index (top down). */
1982 TAILQ_FOREACH(w
, &win
->ws
->stack
, stack_entry
) {
1988 if (asprintf(&s
, "%#x f:%#x wl:%d s:%d im:%s", win
->id
,
1989 win
->frame
, widx
, sidx
, get_win_input_model(win
)) == -1)
1994 /* Update window to an appropriate dimension. */
1995 if (bar_font_legacy
) {
1996 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
1997 width
= l_lbox
.width
+ 4;
1998 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
2000 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
2002 width
= info
.width
+ 4;
2003 height
= bar_font
->height
+ 4;
2006 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
2007 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
;
2009 wc
[0] = wc
[1] = border_width
;
2016 xcb_configure_window(conn
, win
->debug
, mask
, wc
);
2018 /* Draw a filled rectangle to 'clear' window. */
2022 rect
.height
= height
;
2024 gcv
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2025 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2026 xcb_poly_fill_rectangle(conn
, win
->debug
, win
->s
->bar_gc
, 1,
2030 if (bar_font_legacy
) {
2031 l_gcv
.graphics_exposures
= 0;
2032 l_draw
= XCreateGC(display
, win
->debug
, 0, &l_gcv
);
2034 XSetForeground(display
, l_draw
,
2035 win
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2037 DRAWSTRING(display
, win
->debug
, bar_fs
, l_draw
, 2,
2038 (bar_fs_extents
->max_logical_extent
.height
-
2039 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
2041 XFreeGC(display
, l_draw
);
2043 draw
= XftDrawCreate(display
, win
->debug
,
2044 DefaultVisual(display
, win
->s
->idx
),
2045 DefaultColormap(display
, win
->s
->idx
));
2047 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
2048 (bar_height
+ bar_font
->height
) / 2 -
2049 bar_font
->descent
, (FcChar8
*)s
, len
);
2051 XftDrawDestroy(draw
);
2055 } else if (win
->debug
!= XCB_WINDOW_NONE
) {
2056 xcb_destroy_window(conn
, win
->debug
);
2057 win
->debug
= XCB_WINDOW_NONE
;
2062 dumpwins(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
2070 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
2076 #endif /* SWM_DEBUG */
2081 int saved_errno
, status
;
2084 saved_errno
= errno
;
2088 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
2093 if (errno
!= ECHILD
)
2094 warn("sighdlr: waitpid");
2095 #endif /* SWM_DEBUG */
2098 if (pid
== searchpid
)
2102 if (WIFEXITED(status
)) {
2103 if (WEXITSTATUS(status
) != 0)
2104 warnx("sighdlr: child exit status: %d",
2105 WEXITSTATUS(status
));
2107 warnx("sighdlr: child is terminated "
2109 #endif /* SWM_DEBUG */
2123 errno
= saved_errno
;
2129 struct pid_e
*p
= NULL
;
2131 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
2136 TAILQ_FOREACH(p
, &pidlist
, entry
) {
2145 name_to_pixel(int sidx
, const char *colorname
)
2147 uint32_t result
= 0;
2148 char cname
[32] = "#";
2149 xcb_screen_t
*screen
;
2150 xcb_colormap_t cmap
;
2151 xcb_alloc_color_reply_t
*cr
;
2152 xcb_alloc_named_color_reply_t
*nr
;
2153 uint16_t rr
, gg
, bb
;
2155 screen
= get_screen(sidx
);
2156 cmap
= screen
->default_colormap
;
2158 /* color is in format rgb://rr/gg/bb */
2159 if (strncmp(colorname
, "rgb:", 4) == 0) {
2160 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
2161 warnx("could not parse rgb %s", colorname
);
2163 cr
= xcb_alloc_color_reply(conn
,
2164 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
2170 warnx("color '%s' not found", colorname
);
2173 nr
= xcb_alloc_named_color_reply(conn
,
2174 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
2177 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
2178 nr
= xcb_alloc_named_color_reply(conn
,
2179 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
2186 warnx("color '%s' not found", colorname
);
2193 setscreencolor(const char *val
, int i
, int c
)
2195 if (i
< 0 || i
>= get_screen_count())
2198 screens
[i
].c
[c
].pixel
= name_to_pixel(i
, val
);
2199 free(screens
[i
].c
[c
].name
);
2200 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
2205 fancy_stacker(struct workspace
*ws
)
2207 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2208 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2209 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2210 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
2211 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
2212 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2213 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2214 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
2215 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
2219 plain_stacker(struct workspace
*ws
)
2221 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2222 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2223 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
2224 sizeof ws
->stacker
);
2225 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2226 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
2227 sizeof ws
->stacker
);
2231 custom_region(const char *val
)
2233 unsigned int x
, y
, w
, h
;
2234 int sidx
, num_screens
;
2235 xcb_screen_t
*screen
;
2237 DNPRINTF(SWM_D_CONF
, "custom_region: %s\n", val
);
2239 num_screens
= get_screen_count();
2240 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
2241 errx(1, "invalid custom region, "
2242 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
2243 if (sidx
< 1 || sidx
> num_screens
)
2244 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
2248 if ((screen
= get_screen(sidx
)) == NULL
)
2249 errx(1, "ERROR: can't get screen %d.", sidx
);
2252 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
2254 if (x
> screen
->width_in_pixels
||
2255 y
> screen
->height_in_pixels
||
2256 w
+ x
> screen
->width_in_pixels
||
2257 h
+ y
> screen
->height_in_pixels
) {
2258 warnx("ignoring region %ux%u+%u+%u - not within screen "
2259 "boundaries (%ux%u)", w
, h
, x
, y
,
2260 screen
->width_in_pixels
, screen
->height_in_pixels
);
2264 new_region(&screens
[sidx
], x
, y
, w
, h
);
2268 socket_setnonblock(int fd
)
2272 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
2273 err(1, "fcntl F_GETFL");
2274 flags
|= O_NONBLOCK
;
2275 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
2276 err(1, "fcntl F_SETFL");
2280 bar_print_legacy(struct swm_region
*r
, const char *s
)
2282 xcb_rectangle_t rect
;
2287 XRectangle ibox
, lbox
;
2291 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
2293 switch (bar_justify
) {
2294 case SWM_BAR_JUSTIFY_LEFT
:
2297 case SWM_BAR_JUSTIFY_CENTER
:
2298 x
= (WIDTH(r
) - lbox
.width
) / 2;
2300 case SWM_BAR_JUSTIFY_RIGHT
:
2301 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
2305 if (x
< SWM_BAR_OFFSET
)
2310 rect
.width
= WIDTH(r
->bar
);
2311 rect
.height
= HEIGHT(r
->bar
);
2313 /* clear back buffer */
2314 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2315 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2316 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2318 /* draw back buffer */
2319 gcvd
.graphics_exposures
= 0;
2320 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
2321 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2322 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
2323 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
2325 XFreeGC(display
, draw
);
2328 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2329 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2333 bar_print(struct swm_region
*r
, const char *s
)
2336 xcb_rectangle_t rect
;
2344 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
2346 switch (bar_justify
) {
2347 case SWM_BAR_JUSTIFY_LEFT
:
2350 case SWM_BAR_JUSTIFY_CENTER
:
2351 x
= (WIDTH(r
) - info
.width
) / 2;
2353 case SWM_BAR_JUSTIFY_RIGHT
:
2354 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
2358 if (x
< SWM_BAR_OFFSET
)
2363 rect
.width
= WIDTH(r
->bar
);
2364 rect
.height
= HEIGHT(r
->bar
);
2366 /* clear back buffer */
2367 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2368 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2369 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2371 /* draw back buffer */
2372 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
2373 DefaultVisual(display
, r
->s
->idx
),
2374 DefaultColormap(display
, r
->s
->idx
));
2376 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
2377 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
2380 XftDrawDestroy(draw
);
2383 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2384 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2388 bar_extra_stop(void)
2392 bzero(bar_pipe
, sizeof bar_pipe
);
2395 kill(bar_pid
, SIGTERM
);
2398 strlcpy(bar_ext
, "", sizeof bar_ext
);
2403 bar_window_class(char *s
, size_t sz
, struct swm_region
*r
)
2405 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2407 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
2408 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
2412 bar_window_instance(char *s
, size_t sz
, struct swm_region
*r
)
2414 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2416 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
2417 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
2421 bar_window_class_instance(char *s
, size_t sz
, struct swm_region
*r
)
2423 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2426 bar_window_class(s
, sz
, r
);
2427 strlcat(s
, ":", sz
);
2428 bar_window_instance(s
, sz
, r
);
2432 bar_window_index_count(char *s
, size_t sz
, struct swm_region
*r
)
2437 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
) {
2438 strlcat(s
, "0/0", sz
);
2445 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
) {
2447 if (w
->id
== r
->ws
->focus
->id
) {
2452 snprintf(s
, sz
, "%d/%d", index
, count
);
2456 bar_window_state(char *s
, size_t sz
, struct swm_region
*r
)
2458 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2460 if (MAXIMIZED(r
->ws
->focus
))
2461 strlcat(s
, "(m)", sz
);
2462 else if (ABOVE(r
->ws
->focus
))
2463 strlcat(s
, "(f)", sz
);
2467 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
2471 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2474 title
= get_win_name(r
->ws
->focus
->id
);
2475 strlcat(s
, title
, sz
);
2480 get_urgent(struct ws_win
*win
)
2482 xcb_icccm_wm_hints_t hints
;
2483 xcb_get_property_cookie_t c
;
2484 bool urgent
= false;
2487 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2488 if (xcb_icccm_get_wm_hints_reply(conn
, c
, &hints
, NULL
))
2489 urgent
= xcb_icccm_wm_hints_get_urgency(&hints
);
2496 bar_urgent(char *s
, size_t sz
)
2499 int i
, j
, num_screens
;
2500 bool urgent
[SWM_WS_MAX
];
2503 for (i
= 0; i
< workspace_limit
; i
++)
2506 num_screens
= get_screen_count();
2507 for (i
= 0; i
< num_screens
; i
++)
2508 for (j
= 0; j
< workspace_limit
; j
++)
2509 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2510 if (get_urgent(win
))
2513 for (i
= 0; i
< workspace_limit
; i
++) {
2515 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2517 } else if (!urgent_collapse
) {
2518 strlcat(s
, "- ", sz
);
2521 if (urgent_collapse
&& s
[0])
2522 s
[strlen(s
) - 1] = 0;
2526 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2528 if (r
== NULL
|| r
->ws
== NULL
)
2530 if (r
->ws
->name
!= NULL
)
2531 strlcat(s
, r
->ws
->name
, sz
);
2535 bar_workspace_state(char *s
, size_t sz
, struct swm_region
*r
)
2538 int i
, j
, num_screens
;
2539 bool used_workspaces
[SWM_WS_MAX
];
2544 if (r
== NULL
|| r
->ws
== NULL
)
2547 for (i
= 0; i
< SWM_WS_MAX
; ++i
)
2548 used_workspaces
[i
] = false;
2550 num_screens
= get_screen_count();
2551 for (i
= 0; i
< num_screens
; i
++)
2552 for (j
= 0; j
< workspace_limit
; j
++)
2553 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2554 ++used_workspaces
[win
->ws
->idx
];
2556 for (i
= 0; i
< SWM_WS_MAX
; ++i
) {
2558 if (i
== r
->ws
->idx
) {
2560 } else if (used_workspaces
[i
]) {
2566 snprintf(tmp
, sizeof tmp
, fmt
, i
+ 1);
2567 strlcat(s
, tmp
, sz
);
2572 /* build the default bar format according to the defined enabled options */
2574 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2578 /* if format provided, just copy the buffers */
2579 if (bar_format
!= NULL
) {
2580 strlcpy(fmtnew
, fmtexp
, sz
);
2584 /* reset the output buffer */
2587 strlcat(fmtnew
, "+N:+I ", sz
);
2589 strlcat(fmtnew
, "+S", sz
);
2590 strlcat(fmtnew
, " ", sz
);
2592 /* only show the workspace name if there's actually one */
2593 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2594 strlcat(fmtnew
, "<+D>", sz
);
2596 /* If enabled, only show the iconic count if there are iconic wins. */
2597 if (iconic_enabled
&& r
!= NULL
&& r
->ws
!= NULL
)
2598 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2600 strlcat(fmtnew
, "{+M}", sz
);
2604 strlcat(fmtnew
, "+3<", sz
);
2606 if (clock_enabled
) {
2607 strlcat(fmtnew
, fmtexp
, sz
);
2608 strlcat(fmtnew
, "+4<", sz
);
2611 /* bar_urgent already adds the space before the last asterisk */
2613 strlcat(fmtnew
, (urgent_collapse
? "*+U*+4<" : "* +U*+4<"), sz
);
2615 if (window_class_enabled
) {
2616 strlcat(fmtnew
, "+C", sz
);
2617 if (!window_instance_enabled
)
2618 strlcat(fmtnew
, "+4<", sz
);
2621 /* checks needed by the colon and floating strlcat(3) calls below */
2622 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2623 if (window_instance_enabled
) {
2624 if (window_class_enabled
)
2625 strlcat(fmtnew
, ":", sz
);
2626 strlcat(fmtnew
, "+T+4<", sz
);
2628 if (window_name_enabled
) {
2629 if (ABOVE(r
->ws
->focus
) || MAXIMIZED(r
->ws
->focus
))
2630 strlcat(fmtnew
, "+F ", sz
);
2631 strlcat(fmtnew
, "+64W ", sz
);
2635 /* finally add the action script output and the version */
2636 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2640 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2642 /* special case; no limit given, pad one space, instead */
2643 if (*limit
== (int)sz
- 1)
2645 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2648 /* replaces the bar format character sequences (like in tmux(1)) */
2650 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2655 char tmp
[SWM_BAR_MAX
];
2656 int limit
, size
, count
;
2659 /* reset strlcat(3) buffer */
2662 /* get number, if any */
2665 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2666 limit
= sizeof tmp
- 1;
2667 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2668 limit
= sizeof tmp
- 1;
2670 /* there is nothing to replace (ie EOL) */
2677 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2680 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2683 bar_window_class(tmp
, sizeof tmp
, r
);
2686 bar_workspace_name(tmp
, sizeof tmp
, r
);
2689 bar_window_state(tmp
, sizeof tmp
, r
);
2692 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2695 bar_workspace_state(tmp
, sizeof tmp
, r
);
2699 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2703 snprintf(tmp
, sizeof tmp
, "%d", count
);
2706 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2709 bar_window_index_count(tmp
, sizeof tmp
, r
);
2712 bar_window_class_instance(tmp
, sizeof tmp
, r
);
2715 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2718 bar_window_instance(tmp
, sizeof tmp
, r
);
2721 bar_urgent(tmp
, sizeof tmp
);
2724 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2727 bar_window_name(tmp
, sizeof tmp
, r
);
2730 /* unknown character sequence; copy as-is */
2731 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2737 if ((int)len
< limit
)
2739 while (limit
-- > 0) {
2740 if (*offrep
>= sz
- 1)
2742 fmtrep
[(*offrep
)++] = *ptr
++;
2750 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2755 while (*fmt
!= '\0') {
2757 /* skip ordinary characters */
2760 fmtrep
[off
++] = *fmt
++;
2764 /* character sequence found; replace it */
2765 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2774 bar_fmt_expand(char *fmtexp
, size_t sz
)
2781 /* start by grabbing the current time and date */
2783 localtime_r(&tmt
, &tm
);
2785 /* figure out what to expand */
2786 if (bar_format
!= NULL
)
2788 else if (bar_format
== NULL
&& clock_enabled
)
2790 /* if nothing to expand bail out */
2796 /* copy as-is, just in case the format shouldn't be expanded below */
2797 strlcpy(fmtexp
, fmt
, sz
);
2798 /* finally pass the string through strftime(3) */
2799 #ifndef SWM_DENY_CLOCK_FORMAT
2800 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2801 warnx("format too long");
2806 /* Redraws a region bar; need to follow with xcb_flush() or focus_flush(). */
2808 bar_draw(struct swm_bar
*bar
)
2810 struct swm_region
*r
;
2811 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2812 char fmtrep
[SWM_BAR_MAX
];
2814 /* expand the format by first passing it through strftime(3) */
2815 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2822 if (bar_enabled
&& r
->ws
->bar_enabled
)
2823 xcb_map_window(conn
, bar
->id
);
2825 xcb_unmap_window(conn
, bar
->id
);
2829 if (startup_exception
)
2830 snprintf(fmtrep
, sizeof fmtrep
, "total "
2831 "exceptions: %d, first exception: %s",
2835 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2836 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2839 if (bar_font_legacy
)
2840 bar_print_legacy(r
, fmtrep
);
2842 bar_print(r
, fmtrep
);
2846 * Reads external script output; call when stdin is readable.
2847 * Returns 1 if bar_ext was updated; otherwise 0.
2850 bar_extra_update(void)
2853 char b
[SWM_BAR_MAX
];
2854 bool changed
= false;
2859 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2862 if (b
[len
- 1] == '\n') {
2863 /* Remove newline. */
2866 /* "Clear" bar_ext. */
2869 /* Flush buffered output. */
2870 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2871 bar_ext_buf
[0] = '\0';
2873 /* Append new output to bar. */
2874 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2878 /* Buffer output. */
2879 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2884 if (errno
!= EAGAIN
) {
2885 warn("bar_action failed");
2894 bar_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
2896 struct swm_region
*tmpr
;
2899 /* suppress unused warnings since vars are needed */
2904 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2907 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2908 /* Only change if master switch is enabled. */
2910 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2912 bar_enabled
= r
->ws
->bar_enabled
= true;
2914 case SWM_ARG_ID_BAR_TOGGLE
:
2915 bar_enabled
= !bar_enabled
;
2919 /* update bars as necessary */
2920 num_screens
= get_screen_count();
2921 for (i
= 0; i
< num_screens
; i
++)
2922 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2924 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2925 xcb_map_window(conn
, tmpr
->bar
->id
);
2927 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2930 /* Restack all regions and redraw bar. */
2931 num_screens
= get_screen_count();
2932 for (i
= 0; i
< num_screens
; i
++)
2933 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
2935 bar_draw(tmpr
->bar
);
2942 bar_extra_setup(void)
2944 /* do this here because the conf file is in memory */
2945 if (!bar_extra
&& bar_argv
[0]) {
2946 /* launch external status app */
2948 if (pipe(bar_pipe
) == -1)
2949 err(1, "pipe error");
2950 socket_setnonblock(bar_pipe
[0]);
2951 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2953 /* Set stdin to read from the pipe. */
2954 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2957 /* Set stdout to write to the pipe. */
2958 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2961 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2962 err(1, "could not disable SIGPIPE");
2963 switch (bar_pid
= fork()) {
2965 err(1, "cannot fork");
2969 execvp(bar_argv
[0], bar_argv
);
2970 err(1, "%s external app failed", bar_argv
[0]);
2972 default: /* parent */
2977 atexit(kill_bar_extra_atexit
);
2982 kill_bar_extra_atexit(void)
2985 kill(bar_pid
, SIGTERM
);
2993 while ((s
= index(s
, '-'))) {
2998 return (count
== 14);
3004 char *default_string
;
3005 char **missing_charsets
;
3006 int num_missing_charsets
= 0;
3010 XFreeFontSet(display
, bar_fs
);
3014 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n",
3017 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
3018 &num_missing_charsets
, &default_string
);
3020 if (num_missing_charsets
> 0) {
3021 warnx("Unable to load charset(s):");
3023 for (i
= 0; i
< num_missing_charsets
; ++i
)
3024 warnx("%s", missing_charsets
[i
]);
3026 XFreeStringList(missing_charsets
);
3028 if (strcmp(default_string
, ""))
3029 warnx("Glyphs from those sets will be replaced "
3030 "by '%s'.", default_string
);
3032 warnx("Glyphs from those sets won't be drawn.");
3036 errx(1, "Error creating font set structure.");
3038 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
3040 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
3041 2 * bar_border_width
;
3048 xft_init(struct swm_region
*r
)
3050 char *font
, *str
, *search
;
3053 if (bar_font
== NULL
) {
3054 if ((search
= str
= strdup(bar_fonts
)) == NULL
)
3055 errx(1, "insufficient memory.");
3057 while ((font
= strsep(&search
, ",")) != NULL
) {
3061 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
3064 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
3067 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
3071 if (bar_font
== NULL
) {
3072 warnx("unable to load font %s", font
);
3075 DNPRINTF(SWM_D_INIT
, "successfully opened "
3083 if (bar_font
== NULL
)
3084 errx(1, "unable to open a font");
3086 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
3088 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
3089 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
3090 warn("Xft error: unable to allocate color.");
3092 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR
].pixel
, color
);
3094 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
3095 DefaultColormap(display
, r
->s
->idx
), &color
, &search_font_color
))
3096 warn("Xft error: unable to allocate color.");
3098 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
3105 bar_setup(struct swm_region
*r
)
3107 xcb_screen_t
*screen
;
3110 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
3113 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
3114 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
3119 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
3120 err(1, "bar_setup: calloc: failed to allocate memory.");
3122 if (bar_font_legacy
)
3129 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
3130 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
3131 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
3133 /* Assume region is unfocused when we create the bar. */
3134 r
->bar
->id
= xcb_generate_id(conn
);
3135 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
3136 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
3137 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
3138 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
3140 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
3141 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
3142 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
3143 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
3144 | XCB_CW_EVENT_MASK
, wa
);
3146 /* Stack bar window above region window to start. */
3148 wa
[1] = XCB_STACK_MODE_ABOVE
;
3150 xcb_configure_window(conn
, r
->bar
->id
, XCB_CONFIG_WINDOW_SIBLING
|
3151 XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
3153 r
->bar
->buffer
= xcb_generate_id(conn
);
3154 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
3155 WIDTH(r
->bar
), HEIGHT(r
->bar
));
3158 xcb_randr_select_input(conn
, r
->bar
->id
,
3159 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
3162 xcb_map_window(conn
, r
->bar
->id
);
3164 DNPRINTF(SWM_D_BAR
, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
3165 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
3172 bar_cleanup(struct swm_region
*r
)
3176 xcb_destroy_window(conn
, r
->bar
->id
);
3177 xcb_free_pixmap(conn
, r
->bar
->buffer
);
3183 set_win_state(struct ws_win
*win
, uint8_t state
)
3185 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
3187 DNPRINTF(SWM_D_EVENT
, "set_win_state: win %#x, state: %u\n",
3193 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
3194 a_state
, 32, 2, data
);
3198 get_win_state(xcb_window_t w
)
3200 xcb_get_property_reply_t
*r
;
3201 xcb_get_property_cookie_t c
;
3202 uint32_t result
= 0;
3204 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
3205 r
= xcb_get_property_reply(conn
, c
, NULL
);
3207 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
3208 result
= *((uint32_t *)xcb_get_property_value(r
));
3212 DNPRINTF(SWM_D_MISC
, "get_win_state property: win %#x state %u\n", w
,
3218 version(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3220 struct swm_region
*tmpr
;
3223 /* suppress unused warnings since vars are needed */
3228 bar_version
= !bar_version
;
3230 snprintf(bar_vertext
, sizeof bar_vertext
,
3231 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
3233 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
3235 num_screens
= get_screen_count();
3236 for (i
= 0; i
< num_screens
; i
++) {
3237 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
3238 bar_draw(tmpr
->bar
);
3245 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
3247 xcb_client_message_event_t ev
;
3255 name
= get_atom_name(a
);
3256 DNPRINTF(SWM_D_EVENT
, "client_msg: win %#x, atom: %s(%u), "
3258 win
->id
, name
, a
, t
);
3262 bzero(&ev
, sizeof ev
);
3263 ev
.response_type
= XCB_CLIENT_MESSAGE
;
3264 ev
.window
= win
->id
;
3267 ev
.data
.data32
[0] = a
;
3268 ev
.data
.data32
[1] = t
;
3270 xcb_send_event(conn
, 0, win
->id
,
3271 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
3274 /* synthetic response to a ConfigureRequest when not making a change */
3276 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
3278 xcb_configure_notify_event_t ce
;
3283 /* send notification of unchanged state. */
3284 bzero(&ce
, sizeof(ce
));
3285 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
3288 ce
.width
= WIDTH(win
);
3289 ce
.height
= HEIGHT(win
);
3290 ce
.border_width
= 0;
3291 ce
.override_redirect
= 0;
3296 ce
.window
= win
->id
;
3297 ce
.above_sibling
= XCB_WINDOW_NONE
;
3300 ce
.event
= ev
->window
;
3301 ce
.window
= ev
->window
;
3303 /* make response appear more WM_SIZE_HINTS-compliant */
3304 if (win
->sh
.flags
) {
3305 DNPRINTF(SWM_D_MISC
, "config_win: hints: win %#x,"
3306 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
3307 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
3308 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
3309 SH_INC_W(win
), SH_INC_H(win
));
3314 /* the hint may be set... to 0! */
3315 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
3316 ce
.width
= SH_MIN_W(win
);
3317 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
3318 ce
.height
= SH_MIN_H(win
);
3323 /* may also be advertized as 0 */
3324 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
3325 ce
.width
= SH_MAX_W(win
);
3326 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
3327 ce
.height
= SH_MAX_H(win
);
3330 /* resize increment. */
3332 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
3333 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
3335 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
3336 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
3340 /* adjust x and y for requested border_width. */
3341 ce
.x
+= ev
->border_width
;
3342 ce
.y
+= ev
->border_width
;
3344 ce
.above_sibling
= ev
->sibling
;
3347 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
3348 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
3349 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
3351 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
3356 count_win(struct workspace
*ws
, bool count_floating
)
3361 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3362 if (!count_floating
&& FLOATING(win
))
3368 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
3374 quit(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3376 /* suppress unused warnings since vars are needed */
3381 DNPRINTF(SWM_D_MISC
, "quit\n");
3386 lower_window(struct ws_win
*win
)
3388 struct ws_win
*target
= NULL
;
3389 struct workspace
*ws
;
3391 DNPRINTF(SWM_D_EVENT
, "lower_window: win %#x\n", WINID(win
));
3398 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3399 if (target
== win
|| ICONIC(target
))
3401 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3404 if (win
->transient
== target
->transient
)
3406 if (win
->transient
== target
->id
)
3409 if (FULLSCREEN(target
))
3411 if (FULLSCREEN(win
))
3413 if (MAXIMIZED(target
))
3417 if (ABOVE(target
) || TRANS(target
))
3419 if (ABOVE(win
) || TRANS(win
))
3423 /* Change stack position. */
3424 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3426 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3428 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3430 update_win_stacking(win
);
3433 if (swm_debug
& SWM_D_STACK
) {
3434 DPRINTF("=== stacking order (top down) === \n");
3435 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3436 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3437 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3438 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3439 YESNO(ICONIC(target
)));
3443 DNPRINTF(SWM_D_EVENT
, "lower_window: done\n");
3447 raise_window(struct ws_win
*win
)
3449 struct ws_win
*target
= NULL
;
3450 struct workspace
*ws
;
3452 DNPRINTF(SWM_D_EVENT
, "raise_window: win %#x\n", WINID(win
));
3459 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3460 if (target
== win
|| ICONIC(target
))
3462 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3464 if (TRANS(win
) && (win
->transient
== target
->transient
||
3465 win
->transient
== target
->id
))
3467 if (FULLSCREEN(win
))
3469 if (FULLSCREEN(target
))
3473 if (MAXIMIZED(target
))
3475 if (ABOVE(win
) || TRANS(win
) ||
3476 (win
->ws
->focus
== win
&& ws
->always_raise
))
3478 if (!ABOVE(target
) && !TRANS(target
))
3482 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3484 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3486 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3488 update_win_stacking(win
);
3491 if (swm_debug
& SWM_D_STACK
) {
3492 DPRINTF("=== stacking order (top down) === \n");
3493 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3494 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3495 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3496 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3497 YESNO(ICONIC(target
)));
3501 DNPRINTF(SWM_D_EVENT
, "raise_window: done\n");
3505 update_win_stacking(struct ws_win
*win
)
3510 struct swm_region
*r
;
3511 uint16_t configure_mask
;
3514 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
3517 if (win
->frame
== XCB_WINDOW_NONE
) {
3518 DNPRINTF(SWM_D_EVENT
, "update_window_stacking: win %#x not "
3519 "reparented.\n", win
->id
);
3523 if (FLOATINGFULLMAX(win
) || (win
->ws
->always_raise
&&
3524 win
->ws
->focus
== win
)) {
3525 configure_mask
= XCB_CONFIG_WINDOW_STACK_MODE
;
3526 val
[0] = XCB_STACK_MODE_ABOVE
;
3529 DNPRINTF(SWM_D_EVENT
, "update_win_stacking: to very top "
3530 "win %#x (%#x), ", win
->frame
, win
->id
);
3532 configure_mask
= XCB_CONFIG_WINDOW_STACK_MODE
|
3533 XCB_CONFIG_WINDOW_SIBLING
;
3534 val
[0] = r
->bar
->id
;
3535 val
[1] = XCB_STACK_MODE_BELOW
;
3537 DNPRINTF(SWM_D_EVENT
, "update_win_stacking: to tile top "
3538 "win %#x (%#x), ", win
->frame
, win
->id
);
3541 xcb_configure_window(conn
, win
->frame
, configure_mask
, val
);
3544 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
)
3550 map_window(struct ws_win
*win
)
3555 DNPRINTF(SWM_D_EVENT
, "map_window: win %#x, mapped: %s\n",
3556 win
->id
, YESNO(win
->mapped
));
3561 xcb_map_window(conn
, win
->frame
);
3562 xcb_map_window(conn
, win
->id
);
3563 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
3568 unmap_window(struct ws_win
*win
)
3573 DNPRINTF(SWM_D_EVENT
, "unmap_window: win %#x, mapped: %s\n", win
->id
,
3574 YESNO(win
->mapped
));
3579 xcb_unmap_window(conn
, win
->id
);
3580 xcb_unmap_window(conn
, win
->frame
);
3581 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
3582 win
->mapped
= false;
3589 int i
, j
, num_screens
;
3591 num_screens
= get_screen_count();
3592 for (i
= 0; i
< num_screens
; i
++)
3593 for (j
= 0; j
< workspace_limit
; j
++)
3594 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3599 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
3601 xcb_key_press_event_t event
;
3602 xcb_keycode_t
*keycode
;
3607 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
3609 DNPRINTF(SWM_D_MISC
, "fake_keypress: win %#x, keycode %u\n",
3612 bzero(&event
, sizeof(event
));
3613 event
.event
= win
->id
;
3614 event
.root
= win
->s
->root
;
3615 event
.child
= XCB_WINDOW_NONE
;
3616 event
.time
= XCB_CURRENT_TIME
;
3617 event
.event_x
= X(win
);
3618 event
.event_y
= Y(win
);
3621 event
.same_screen
= 1;
3622 event
.detail
= *keycode
;
3623 event
.state
= modifiers
;
3625 event
.response_type
= XCB_KEY_PRESS
;
3626 xcb_send_event(conn
, 1, win
->id
,
3627 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
3629 event
.response_type
= XCB_KEY_RELEASE
;
3630 xcb_send_event(conn
, 1, win
->id
,
3631 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
3637 restart(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3639 /* suppress unused warning since var is needed */
3644 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
3648 execvp(start_argv
[0], start_argv
);
3649 warn("execvp failed");
3650 quit(NULL
, NULL
, NULL
);
3654 get_pointer_win(xcb_window_t root
)
3656 struct ws_win
*win
= NULL
;
3657 xcb_query_pointer_reply_t
*r
;
3659 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: %#x.\n", root
);
3661 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
3663 win
= find_window(r
->child
);
3665 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: %#x.\n",
3668 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
3677 center_pointer(struct swm_region
*r
)
3681 if (!warp_pointer
|| r
== NULL
)
3686 DNPRINTF(SWM_D_EVENT
, "center_pointer: win %#x.\n", WINID(win
));
3688 if (win
&& win
->mapped
)
3689 xcb_warp_pointer(conn
, XCB_NONE
, win
->frame
, 0, 0, 0, 0,
3690 WIDTH(win
) / 2, HEIGHT(win
) / 2);
3692 xcb_warp_pointer(conn
, XCB_NONE
, r
->id
, 0, 0, 0, 0,
3693 WIDTH(r
) / 2, HEIGHT(r
) / 2);
3697 root_to_region(xcb_window_t root
, int check
)
3700 struct swm_region
*r
= NULL
;
3702 xcb_query_pointer_reply_t
*qpr
;
3703 xcb_get_input_focus_reply_t
*gifr
;
3705 DNPRINTF(SWM_D_MISC
, "root_to_region: win %#x\n", root
);
3707 num_screens
= get_screen_count();
3708 for (i
= 0; i
< num_screens
; i
++)
3709 if (screens
[i
].root
== root
)
3712 if (check
& SWM_CK_REGION
)
3713 r
= screens
[i
].r_focus
;
3715 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
3716 /* Try to find an actively focused window */
3717 gifr
= xcb_get_input_focus_reply(conn
,
3718 xcb_get_input_focus(conn
), NULL
);
3720 cfw
= find_window(gifr
->focus
);
3721 if (cfw
&& cfw
->ws
->r
)
3728 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
3729 /* No region with an active focus; try to use pointer. */
3730 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
3731 screens
[i
].root
), NULL
);
3733 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
3734 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
3735 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3736 if (X(r
) <= qpr
->root_x
&&
3737 qpr
->root_x
< MAX_X(r
) &&
3738 Y(r
) <= qpr
->root_y
&&
3739 qpr
->root_y
< MAX_Y(r
))
3746 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
3747 r
= TAILQ_FIRST(&screens
[i
].rl
);
3749 DNPRINTF(SWM_D_MISC
, "root_to_region: idx: %d\n", get_region_index(r
));
3755 find_region(xcb_window_t id
)
3757 struct swm_region
*r
;
3760 num_screens
= get_screen_count();
3761 for (i
= 0; i
< num_screens
; i
++)
3762 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3770 find_bar(xcb_window_t id
)
3772 struct swm_region
*r
;
3775 num_screens
= get_screen_count();
3776 for (i
= 0; i
< num_screens
; i
++)
3777 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3778 if (r
->bar
&& r
->bar
->id
== id
)
3785 find_frame_window(xcb_window_t id
) {
3786 struct swm_region
*r
;
3790 num_screens
= get_screen_count();
3791 for (i
= 0; i
< num_screens
; i
++)
3792 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3793 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
3801 find_window(xcb_window_t id
)
3803 struct ws_win
*win
= NULL
;
3804 int i
, j
, num_screens
;
3805 xcb_query_tree_reply_t
*qtr
;
3807 DNPRINTF(SWM_D_MISC
, "find_window: id: %#x\n", id
);
3809 num_screens
= get_screen_count();
3810 for (i
= 0; i
< num_screens
; i
++)
3811 for (j
= 0; j
< workspace_limit
; j
++)
3812 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3813 if (id
== win
->id
|| id
== win
->frame
)
3817 /* If window isn't top-level, try to find managed ancestor. */
3818 qtr
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3820 if (qtr
->parent
!= XCB_WINDOW_NONE
&& qtr
->parent
!= qtr
->root
)
3821 win
= find_window(qtr
->parent
);
3825 DNPRINTF(SWM_D_MISC
, "find_window: found child %#x "
3826 "of %#x.\n", win
->id
, qtr
->parent
);
3836 find_unmanaged_window(xcb_window_t id
)
3839 int i
, j
, num_screens
;
3841 num_screens
= get_screen_count();
3842 for (i
= 0; i
< num_screens
; i
++)
3843 for (j
= 0; j
< workspace_limit
; j
++)
3844 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
3852 spawn(int ws_idx
, union arg
*args
, bool close_fd
)
3857 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3859 close(xcb_get_file_descriptor(conn
));
3862 if ((ret
= getenv("LD_PRELOAD"))) {
3863 if (asprintf(&ret
, "%s:%s", SWM_LIB
, ret
) == -1) {
3864 warn("spawn: asprintf LD_PRELOAD");
3867 setenv("LD_PRELOAD", ret
, 1);
3870 setenv("LD_PRELOAD", SWM_LIB
, 1);
3873 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3874 warn("spawn: asprintf SWM_WS");
3877 setenv("_SWM_WS", ret
, 1);
3881 if (asprintf(&ret
, "%d", getpid()) == -1) {
3882 warn("spawn: asprintf _SWM_PID");
3885 setenv("_SWM_PID", ret
, 1);
3890 if (setsid() == -1) {
3891 warn("spawn: setsid");
3897 * close stdin and stdout to prevent interaction between apps
3898 * and the baraction script
3899 * leave stderr open to record errors
3901 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3902 warn("spawn: open");
3905 dup2(fd
, STDIN_FILENO
);
3906 dup2(fd
, STDOUT_FILENO
);
3911 execvp(args
->argv
[0], args
->argv
);
3913 warn("spawn: execvp");
3918 kill_refs(struct ws_win
*win
)
3920 struct workspace
*ws
;
3922 int i
, j
, num_screens
;
3927 num_screens
= get_screen_count();
3928 for (i
= 0; i
< num_screens
; i
++) {
3929 for (j
= 0; j
< workspace_limit
; j
++) {
3930 ws
= &screens
[i
].ws
[j
];
3932 if (win
== ws
->focus
)
3934 if (win
== ws
->focus_prev
)
3935 ws
->focus_prev
= NULL
;
3936 if (win
== ws
->focus_pending
)
3937 ws
->focus_pending
= NULL
;
3938 if (win
== ws
->focus_raise
)
3939 ws
->focus_raise
= NULL
;
3942 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3943 if (win
== w
->focus_child
)
3944 w
->focus_child
= NULL
;
3950 validate_win(struct ws_win
*testwin
)
3953 struct workspace
*ws
;
3954 struct swm_region
*r
;
3955 int i
, x
, num_screens
;
3957 if (testwin
== NULL
)
3960 num_screens
= get_screen_count();
3961 for (i
= 0; i
< num_screens
; i
++)
3962 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3963 for (x
= 0; x
< workspace_limit
; x
++) {
3965 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3973 validate_ws(struct workspace
*testws
)
3975 struct swm_region
*r
;
3976 struct workspace
*ws
;
3977 int i
, x
, num_screens
;
3979 /* validate all ws */
3980 num_screens
= get_screen_count();
3981 for (i
= 0; i
< num_screens
; i
++)
3982 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3983 for (x
= 0; x
< workspace_limit
; x
++) {
3992 unfocus_win(struct ws_win
*win
)
3994 xcb_window_t none
= XCB_WINDOW_NONE
;
3996 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: win %#x\n", WINID(win
));
4001 if (win
->ws
== NULL
) {
4002 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
4006 if (validate_ws(win
->ws
)) {
4007 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
4011 if (win
->ws
->r
== NULL
) {
4012 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
4016 if (validate_win(win
)) {
4017 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
4022 if (win
->ws
->focus
== win
) {
4023 win
->ws
->focus
= NULL
;
4024 win
->ws
->focus_prev
= win
;
4025 if (win
->ws
->focus_raise
== win
&& !FLOATING(win
)) {
4026 update_win_stacking(win
);
4030 if (validate_win(win
->ws
->focus
)) {
4031 kill_refs(win
->ws
->focus
);
4032 win
->ws
->focus
= NULL
;
4035 if (validate_win(win
->ws
->focus_prev
)) {
4036 kill_refs(win
->ws
->focus_prev
);
4037 win
->ws
->focus_prev
= NULL
;
4042 /* Raise window to "top unfocused position." */
4043 if (win
->ws
->always_raise
)
4046 /* Update border width */
4047 if (win
->bordered
&& (win
->quirks
& SWM_Q_MINIMALBORDER
) &&
4050 X(win
) += border_width
;
4051 Y(win
) += border_width
;
4055 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4056 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
4058 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
4062 focus_win(struct ws_win
*win
)
4064 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
, *tmpw
;
4065 struct workspace
*ws
;
4066 xcb_get_input_focus_reply_t
*gifr
= NULL
;
4067 xcb_get_window_attributes_reply_t
*war
= NULL
;
4069 DNPRINTF(SWM_D_FOCUS
, "focus_win: win %#x\n", WINID(win
));
4071 if (win
== NULL
|| win
->ws
== NULL
|| !win
->mapped
)
4076 if (validate_ws(ws
))
4079 if (validate_win(win
)) {
4084 gifr
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
4086 DNPRINTF(SWM_D_FOCUS
, "focus_win: cur focus: %#x\n",
4089 cfw
= find_window(gifr
->focus
);
4092 if (cfw
->ws
!= ws
&& cfw
->ws
->r
!= NULL
&&
4093 cfw
->frame
!= XCB_WINDOW_NONE
) {
4100 war
= xcb_get_window_attributes_reply(conn
,
4101 xcb_get_window_attributes(conn
, gifr
->focus
), NULL
);
4102 if (war
&& war
->override_redirect
&& ws
->focus
== win
) {
4103 DNPRINTF(SWM_D_FOCUS
, "focus_win: skip refocus "
4104 "from override_redirect.\n");
4110 if (ws
->focus
!= win
) {
4111 if (ws
->focus
&& ws
->focus
!= cfw
)
4112 unfocus_win(ws
->focus
);
4116 /* Clear focus child redirect. */
4117 win
->focus_child
= NULL
;
4119 /* If transient, adjust parent's focus child for focus_magic. */
4121 parent
= find_window(win
->transient
);
4122 if (parent
&& parent
->focus_child
!= win
)
4123 parent
->focus_child
= win
;
4126 /* Update window border even if workspace is hidden. */
4130 DNPRINTF(SWM_D_FOCUS
, "focus_win: already focused.\n");
4135 /* Set input focus if no input hint, or indicated by hint. */
4136 if (ACCEPTS_FOCUS(win
)) {
4137 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
4138 " revert-to: parent, time: %#x\n", win
->id
,
4140 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
4141 win
->id
, last_event_time
);
4142 } else if (!win
->take_focus
) {
4143 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
4144 " revert-to: parent, time: 0\n", ws
->r
->id
);
4145 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
4146 ws
->r
->id
, XCB_CURRENT_TIME
);
4149 /* Tell app it can adjust focus to a specific window. */
4150 if (win
->take_focus
) {
4151 /* java is special; always tell parent */
4152 if (TRANS(win
) && win
->java
)
4153 client_msg(parent
, a_takefocus
,
4156 client_msg(win
, a_takefocus
, last_event_time
);
4159 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
4161 /* If a parent exists, map it first. */
4163 raise_window(parent
);
4166 /* Map siblings next. */
4167 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
,
4169 if (w
!= win
&& !ICONIC(w
) &&
4170 w
->transient
== parent
->id
) {
4176 /* Map focused window. */
4180 /* Stack any children of focus window. */
4181 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
4182 if (w
->transient
== win
->id
&& !ICONIC(w
)) {
4186 } else if (tile_gap
< 0 && !ABOVE(win
)) {
4188 * Windows overlap in the layout.
4189 * Raise focused win above all tiled wins.
4197 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4198 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4201 bar_draw(ws
->r
->bar
);
4207 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
4210 /* If a child window should have focus instead, return it. */
4212 get_focus_magic(struct ws_win
*win
)
4214 struct ws_win
*parent
= NULL
;
4215 struct ws_win
*child
= NULL
;
4217 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: win %#x\n", WINID(win
));
4222 parent
= find_window(win
->transient
);
4224 /* If parent prefers focus elsewhere, then try to do so. */
4225 if (parent
&& (child
= parent
->focus_child
)) {
4226 if (validate_win(child
) == 0 && child
->mapped
)
4229 parent
->focus_child
= NULL
;
4233 /* If this window prefers focus elsewhere, then try to do so. */
4234 if ((child
= win
->focus_child
)) {
4235 if (validate_win(child
) == 0 && child
->mapped
)
4238 win
->focus_child
= NULL
;
4245 event_drain(uint8_t rt
)
4247 xcb_generic_event_t
*evt
;
4249 /* ensure all pending requests have been processed before filtering. */
4251 while ((evt
= get_next_event(false))) {
4252 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
4260 set_region(struct swm_region
*r
)
4262 struct swm_region
*rf
;
4269 /* Unfocus old region bar. */
4274 xcb_change_window_attributes(conn
, rf
->bar
->id
,
4275 XCB_CW_BORDER_PIXEL
,
4276 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
4279 if (rf
!= NULL
&& rf
!= r
&& (X(rf
) != X(r
) || Y(rf
) != Y(r
) ||
4280 WIDTH(rf
) != WIDTH(r
) || HEIGHT(rf
) != HEIGHT(r
))) {
4281 /* Set _NET_DESKTOP_GEOMETRY. */
4283 vals
[1] = HEIGHT(r
);
4284 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, r
->s
->root
,
4285 ewmh
[_NET_DESKTOP_GEOMETRY
].atom
, XCB_ATOM_CARDINAL
, 32, 2,
4289 /* Set region bar border to focus_color. */
4290 xcb_change_window_attributes(conn
, r
->bar
->id
,
4291 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
4295 /* Update the focus window frame on the now unfocused region. */
4296 if (rf
&& rf
->ws
->focus
)
4297 draw_frame(rf
->ws
->focus
);
4299 ewmh_update_current_desktop();
4303 focus_region(struct swm_region
*r
)
4306 struct swm_region
*old_r
;
4311 old_r
= r
->s
->r_focus
;
4314 nfw
= get_region_focus(r
);
4318 /* New region is empty; need to manually unfocus win. */
4320 unfocus_win(old_r
->ws
->focus
);
4321 /* Clear bar since empty. */
4322 bar_draw(old_r
->bar
);
4325 DNPRINTF(SWM_D_FOCUS
, "focus_region: set_input_focus: %#x, "
4326 "revert-to: parent, time: 0\n", r
->id
);
4327 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4334 switchws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4336 struct swm_region
*this_r
, *other_r
;
4338 struct workspace
*new_ws
, *old_ws
;
4339 xcb_window_t none
= XCB_WINDOW_NONE
;
4340 int wsid
= args
->id
;
4341 bool unmap_old
= false;
4346 if (wsid
>= workspace_limit
)
4350 old_ws
= this_r
->ws
;
4351 new_ws
= &this_r
->s
->ws
[wsid
];
4353 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
4354 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
4356 if (new_ws
== NULL
|| old_ws
== NULL
)
4358 if (new_ws
== old_ws
)
4361 other_r
= new_ws
->r
;
4362 if (other_r
&& workspace_clamp
&&
4363 bp
->action
!= FN_RG_MOVE_NEXT
&& bp
->action
!= FN_RG_MOVE_PREV
) {
4364 DNPRINTF(SWM_D_WS
, "switchws: ws clamped.\n");
4367 DNPRINTF(SWM_D_WS
, "switchws: warping focus to region "
4368 "with ws %d.\n", wsid
);
4369 focus_region(other_r
);
4370 center_pointer(other_r
);
4376 if ((win
= old_ws
->focus
) != NULL
) {
4379 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4380 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4385 /* the other ws is visible in another region, exchange them */
4386 other_r
->ws_prior
= new_ws
;
4387 other_r
->ws
= old_ws
;
4388 old_ws
->r
= other_r
;
4390 /* the other workspace is hidden, hide this one */
4395 this_r
->ws_prior
= old_ws
;
4396 this_r
->ws
= new_ws
;
4399 /* Set focus_pending before stacking, if needed. */
4400 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
4401 validate_win(new_ws
->focus_pending
))) {
4402 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
4403 new_ws
->focus
= new_ws
->focus_prev
;
4404 new_ws
->focus_prev
= NULL
;
4407 new_ws
->state
= SWM_WS_STATE_MAPPING
;
4412 /* unmap old windows */
4414 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
4416 old_ws
->state
= SWM_WS_STATE_HIDDEN
;
4419 /* if workspaces were swapped, then don't wait to set focus */
4420 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
4421 if (new_ws
->focus_pending
) {
4422 focus_win(new_ws
->focus_pending
);
4423 new_ws
->focus_pending
= NULL
;
4427 /* Clear bar and set focus on region input win if new ws is empty. */
4428 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
4429 DNPRINTF(SWM_D_FOCUS
, "switchws: set_input_focus: %#x, "
4430 "revert-to: parent, time: 0\n", r
->id
);
4431 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4436 ewmh_update_current_desktop();
4440 new_ws
->state
= SWM_WS_STATE_MAPPED
;
4442 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
4446 cyclews(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4449 struct swm_screen
*s
= r
->s
;
4450 bool cycle_all
= false, mv
= false;
4452 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4453 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4459 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
4462 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
4465 case SWM_ARG_ID_CYCLEWS_UP
:
4466 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
4468 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
4471 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
4474 case SWM_ARG_ID_CYCLEWS_DOWN
:
4475 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
4482 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
4484 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
4488 send_to_ws(bp
, r
, &a
);
4490 switchws(bp
, r
, &a
);
4491 } while (a
.id
!= r
->ws
->idx
);
4493 DNPRINTF(SWM_D_FOCUS
, "cyclews: done\n");
4497 priorws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4503 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4504 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4506 if (r
->ws_prior
== NULL
)
4509 a
.id
= r
->ws_prior
->idx
;
4510 switchws(bp
, r
, &a
);
4511 DNPRINTF(SWM_D_FOCUS
, "priorws: done\n");
4515 focusrg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4517 int ridx
= args
->id
, i
, num_screens
;
4518 struct swm_region
*rr
= NULL
;
4522 num_screens
= get_screen_count();
4523 /* do nothing if we don't have more than one screen */
4524 if (!(num_screens
> 1 || outputs
> 1))
4527 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
4529 rr
= TAILQ_FIRST(&r
->s
->rl
);
4530 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
4531 rr
= TAILQ_NEXT(rr
, entry
);
4539 DNPRINTF(SWM_D_FOCUS
, "focusrg: done\n");
4543 cyclerg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4546 struct swm_region
*rr
= NULL
;
4549 num_screens
= get_screen_count();
4550 /* do nothing if we don't have more than one screen */
4551 if (!(num_screens
> 1 || outputs
> 1))
4555 DNPRINTF(SWM_D_FOCUS
, "cyclerg: id: %d, region: %d\n", args
->id
, i
);
4558 case SWM_ARG_ID_CYCLERG_UP
:
4559 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4560 rr
= TAILQ_NEXT(r
, entry
);
4562 rr
= TAILQ_FIRST(&screens
[i
].rl
);
4564 case SWM_ARG_ID_CYCLERG_DOWN
:
4565 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4566 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
4568 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
4577 case SWM_ARG_ID_CYCLERG_UP
:
4578 case SWM_ARG_ID_CYCLERG_DOWN
:
4583 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4584 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4586 switchws(bp
, r
, &a
);
4592 DNPRINTF(SWM_D_FOCUS
, "cyclerg: done\n");
4595 /* Sorts transients after parent. */
4597 sort_windows(struct ws_win_list
*wl
)
4599 struct ws_win
*win
, *parent
, *nxt
;
4604 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
4605 nxt
= TAILQ_NEXT(win
, entry
);
4607 parent
= find_window(win
->transient
);
4608 if (parent
== NULL
) {
4609 warnx("not possible bug");
4612 TAILQ_REMOVE(wl
, win
, entry
);
4613 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
4619 swapwin(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4621 struct ws_win
*target
, *source
;
4622 struct ws_win
*cur_focus
;
4623 struct ws_win_list
*wl
;
4627 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4628 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4630 cur_focus
= r
->ws
->focus
;
4631 if (cur_focus
== NULL
|| FULLSCREEN(cur_focus
))
4634 /* Adjust stacking in floating layer. */
4635 if (ABOVE(cur_focus
)) {
4637 case SWM_ARG_ID_SWAPPREV
:
4638 target
= TAILQ_PREV(cur_focus
, ws_win_stack
,
4640 if (target
!= NULL
&& FLOATING(target
)) {
4641 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4643 TAILQ_INSERT_BEFORE(target
, cur_focus
,
4645 update_win_stacking(cur_focus
);
4649 case SWM_ARG_ID_SWAPNEXT
:
4650 target
= TAILQ_NEXT(cur_focus
, stack_entry
);
4651 if (target
!= NULL
&& FLOATING(target
)) {
4652 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4654 TAILQ_INSERT_AFTER(&cur_focus
->ws
->stack
,
4655 target
, cur_focus
, stack_entry
);
4656 update_win_stacking(cur_focus
);
4664 clear_maximized(r
->ws
);
4667 wl
= &source
->ws
->winlist
;
4670 case SWM_ARG_ID_SWAPPREV
:
4672 source
= find_window(source
->transient
);
4673 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
4674 if (target
&& target
->transient
)
4675 target
= find_window(target
->transient
);
4676 TAILQ_REMOVE(wl
, source
, entry
);
4678 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4680 TAILQ_INSERT_BEFORE(target
, source
, entry
);
4682 case SWM_ARG_ID_SWAPNEXT
:
4683 target
= TAILQ_NEXT(source
, entry
);
4684 /* move the parent and let the sort handle the move */
4686 source
= find_window(source
->transient
);
4687 TAILQ_REMOVE(wl
, source
, entry
);
4689 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4691 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
4693 case SWM_ARG_ID_SWAPMAIN
:
4694 target
= TAILQ_FIRST(wl
);
4695 if (target
== source
) {
4696 if (source
->ws
->focus_prev
!= NULL
&&
4697 source
->ws
->focus_prev
!= target
)
4698 source
= source
->ws
->focus_prev
;
4702 if (target
== NULL
|| source
== NULL
)
4704 source
->ws
->focus_prev
= target
;
4705 TAILQ_REMOVE(wl
, target
, entry
);
4706 TAILQ_INSERT_BEFORE(source
, target
, entry
);
4707 TAILQ_REMOVE(wl
, source
, entry
);
4708 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4710 case SWM_ARG_ID_MOVELAST
:
4711 TAILQ_REMOVE(wl
, source
, entry
);
4712 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4715 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
4720 ewmh_update_client_list();
4726 DNPRINTF(SWM_D_MOVE
, "swapwin: done\n");
4730 get_focus_prev(struct ws_win
*win
)
4732 struct ws_win
*winfocus
= NULL
;
4733 struct ws_win
*cur_focus
= NULL
;
4734 struct ws_win_list
*wl
= NULL
;
4735 struct workspace
*ws
= NULL
;
4737 if (!(win
&& win
->ws
))
4742 cur_focus
= ws
->focus
;
4744 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: win %#x, cur_focus: %#x, "
4745 "focus_prev: %#x\n", WINID(win
), WINID(cur_focus
),
4746 WINID(ws
->focus_prev
));
4748 /* pickle, just focus on whatever */
4749 if (cur_focus
== NULL
) {
4750 /* use prev_focus if valid */
4751 if (ws
->focus_prev
&& find_window(ws
->focus_prev
->id
))
4752 winfocus
= ws
->focus_prev
;
4756 /* if transient focus on parent */
4757 if (TRANS(cur_focus
)) {
4758 winfocus
= find_window(cur_focus
->transient
);
4762 /* if in max_stack try harder */
4763 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
4764 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
4765 if (cur_focus
!= ws
->focus_prev
)
4766 winfocus
= ws
->focus_prev
;
4768 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
4773 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
4775 if (winfocus
== NULL
|| winfocus
== win
) {
4776 switch (focus_close
) {
4777 case SWM_STACK_BOTTOM
:
4778 TAILQ_FOREACH(winfocus
, wl
, entry
)
4779 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4783 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4784 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4787 case SWM_STACK_ABOVE
:
4788 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
4789 while (winfocus
&& ICONIC(winfocus
))
4790 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4792 if (winfocus
== NULL
) {
4793 if (focus_close_wrap
) {
4794 TAILQ_FOREACH(winfocus
, wl
, entry
)
4795 if (!ICONIC(winfocus
) &&
4796 winfocus
!= cur_focus
)
4799 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4801 if (!ICONIC(winfocus
) &&
4802 winfocus
!= cur_focus
)
4807 case SWM_STACK_BELOW
:
4808 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
4809 while (winfocus
&& ICONIC(winfocus
))
4810 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
4813 if (winfocus
== NULL
) {
4814 if (focus_close_wrap
) {
4815 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4817 if (!ICONIC(winfocus
) &&
4818 winfocus
!= cur_focus
)
4821 TAILQ_FOREACH(winfocus
, wl
, entry
)
4822 if (!ICONIC(winfocus
) &&
4823 winfocus
!= cur_focus
)
4831 if (winfocus
== NULL
||
4832 (winfocus
&& (ICONIC(winfocus
) || winfocus
== cur_focus
))) {
4833 if (focus_default
== SWM_STACK_TOP
) {
4834 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4835 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4838 TAILQ_FOREACH(winfocus
, wl
, entry
)
4839 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4846 return get_focus_magic(winfocus
);
4850 get_region_focus(struct swm_region
*r
)
4852 struct ws_win
*winfocus
= NULL
;
4857 if (r
->ws
->focus
&& !ICONIC(r
->ws
->focus
))
4858 winfocus
= r
->ws
->focus
;
4859 else if (r
->ws
->focus_prev
&& !ICONIC(r
->ws
->focus_prev
))
4860 winfocus
= r
->ws
->focus_prev
;
4862 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
4863 if (!ICONIC(winfocus
))
4866 return get_focus_magic(winfocus
);
4870 focus(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4872 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
4873 struct ws_win_list
*wl
= NULL
;
4874 struct workspace
*ws
= NULL
;
4881 cur_focus
= r
->ws
->focus
;
4885 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d, cur_focus: %#x\n", args
->id
,
4888 /* Make sure an uniconified window has focus, if one exists. */
4889 if (cur_focus
== NULL
) {
4890 cur_focus
= TAILQ_FIRST(wl
);
4891 while (cur_focus
!= NULL
&& ICONIC(cur_focus
))
4892 cur_focus
= TAILQ_NEXT(cur_focus
, entry
);
4894 DNPRINTF(SWM_D_FOCUS
, "focus: new cur_focus: %#x\n",
4899 case SWM_ARG_ID_FOCUSPREV
:
4900 if (cur_focus
== NULL
)
4903 winfocus
= cur_focus
;
4905 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
4906 if (winfocus
== NULL
)
4907 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
4908 if (winfocus
== cur_focus
)
4910 } while (winfocus
&& (ICONIC(winfocus
) ||
4911 winfocus
->id
== cur_focus
->transient
||
4912 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4913 winfocus
->transient
== cur_focus
->transient
) ||
4914 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4916 case SWM_ARG_ID_FOCUSNEXT
:
4917 if (cur_focus
== NULL
)
4920 winfocus
= cur_focus
;
4922 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4923 if (winfocus
== NULL
)
4924 winfocus
= TAILQ_FIRST(wl
);
4925 if (winfocus
== cur_focus
)
4927 } while (winfocus
&& (ICONIC(winfocus
) ||
4928 winfocus
->id
== cur_focus
->transient
||
4929 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4930 winfocus
->transient
== cur_focus
->transient
) ||
4931 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4933 case SWM_ARG_ID_FOCUSMAIN
:
4934 if (cur_focus
== NULL
)
4937 winfocus
= TAILQ_FIRST(wl
);
4938 if (winfocus
== cur_focus
)
4939 winfocus
= cur_focus
->ws
->focus_prev
;
4941 case SWM_ARG_ID_FOCUSURGENT
:
4942 /* Search forward for the next urgent window. */
4946 for (i
= 0; i
<= workspace_limit
; ++i
) {
4948 head
= TAILQ_FIRST(&r
->s
->ws
[(ws
->idx
+ i
) %
4949 workspace_limit
].winlist
);
4952 if (head
== cur_focus
) {
4954 winfocus
= cur_focus
;
4957 } else if (get_urgent(head
)) {
4962 head
= TAILQ_NEXT(head
, entry
);
4969 /* Switch ws if new focus is on a different ws. */
4970 if (winfocus
&& winfocus
->ws
!= ws
) {
4971 a
.id
= winfocus
->ws
->idx
;
4972 switchws(bp
, r
, &a
);
4979 if (clear_maximized(ws
) > 0)
4982 focus_win(get_focus_magic(winfocus
));
4987 DNPRINTF(SWM_D_FOCUS
, "focus: done\n");
4991 focus_pointer(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4995 /* Not needed for buttons since this is already done in buttonpress. */
4996 if (bp
->type
== KEYBIND
) {
4997 focus_win(get_pointer_win(r
->s
->root
));
5003 cycle_layout(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5005 struct workspace
*ws
= r
->ws
;
5007 /* suppress unused warning since var is needed */
5011 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
5014 if (ws
->cur_layout
->l_stack
== NULL
)
5015 ws
->cur_layout
= &layouts
[0];
5017 clear_maximized(ws
);
5022 focus_win(get_region_focus(r
));
5029 stack_config(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5031 struct workspace
*ws
= r
->ws
;
5035 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
5038 if (args
->id
== SWM_ARG_ID_STACKINIT
) {
5041 ws
->cur_layout
->l_config(ws
, args
->id
);
5044 clear_maximized(ws
);
5054 stack(struct swm_region
*r
) {
5055 struct swm_geometry g
;
5056 struct swm_region
*r_prev
;
5062 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
5064 /* Adjust stack area for region bar and padding. */
5066 g
.x
+= region_padding
;
5067 g
.y
+= region_padding
;
5068 g
.w
-= 2 * border_width
+ 2 * region_padding
;
5069 g
.h
-= 2 * border_width
+ 2 * region_padding
;
5070 if (bar_enabled
&& r
->ws
->bar_enabled
) {
5076 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d (screen: "
5077 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
5078 r
->ws
->idx
, r
->s
->idx
, get_region_index(r
), g
.x
, g
.y
, g
.w
, g
.h
);
5080 r_prev
= TAILQ_PREV(r
, swm_region_list
, entry
);
5082 /* Stack bar/input relative to prev. region. */
5083 val
[1] = XCB_STACK_MODE_ABOVE
;
5085 val
[0] = r_prev
->id
;
5086 DNPRINTF(SWM_D_STACK
, "stack: region input %#x "
5087 "relative to %#x.\n", r
->id
, val
[0]);
5088 xcb_configure_window(conn
, r
->id
,
5089 XCB_CONFIG_WINDOW_SIBLING
|
5090 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
5092 val
[0] = r_prev
->bar
->id
;
5093 DNPRINTF(SWM_D_STACK
, "stack: region bar %#x "
5094 "relative to %#x.\n", r
->bar
->id
, val
[0]);
5095 xcb_configure_window(conn
, r
->bar
->id
,
5096 XCB_CONFIG_WINDOW_SIBLING
|
5097 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
5100 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
5101 r
->ws
->cur_layout
->l_string(r
->ws
);
5102 /* save r so we can track region changes */
5108 DNPRINTF(SWM_D_STACK
, "stack: end\n");
5112 store_float_geom(struct ws_win
*win
)
5114 if (win
== NULL
|| win
->ws
->r
== NULL
)
5117 /* retain window geom and region geom */
5118 win
->g_float
= win
->g
;
5119 win
->g_float
.x
-= X(win
->ws
->r
);
5120 win
->g_float
.y
-= Y(win
->ws
->r
);
5121 win
->g_floatvalid
= true;
5122 DNPRINTF(SWM_D_MISC
, "store_float_geom: win %#x, g: (%d,%d)"
5123 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
5124 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
5125 win
->g_float
.w
, win
->g_float
.h
);
5129 load_float_geom(struct ws_win
*win
)
5131 if (win
== NULL
|| win
->ws
->r
== NULL
)
5134 if (win
->g_floatvalid
) {
5135 win
->g
= win
->g_float
;
5136 X(win
) += X(win
->ws
->r
);
5137 Y(win
) += Y(win
->ws
->r
);
5138 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g: (%d,%d)"
5139 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
5142 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g_float "
5143 "is not set.\n", win
->id
);
5148 update_floater(struct ws_win
*win
)
5150 struct workspace
*ws
;
5151 struct swm_region
*r
;
5153 DNPRINTF(SWM_D_MISC
, "update_floater: win %#x\n", WINID(win
));
5160 if ((r
= ws
->r
) == NULL
)
5163 win
->bordered
= true;
5165 if (FULLSCREEN(win
)) {
5166 /* _NET_WM_FULLSCREEN: fullscreen without border. */
5167 if (!win
->g_floatvalid
)
5168 store_float_geom(win
);
5171 win
->bordered
= false;
5172 } else if (MAXIMIZED(win
)) {
5173 /* Maximize: like a single stacked window. */
5174 if (!win
->g_floatvalid
)
5175 store_float_geom(win
);
5179 if (bar_enabled
&& ws
->bar_enabled
&& !maximize_hide_bar
) {
5181 Y(win
) += bar_height
;
5182 HEIGHT(win
) -= bar_height
;
5183 } else if (disable_border
) {
5184 win
->bordered
= false;
5187 if (win
->bordered
) {
5188 /* Window geometry excludes frame. */
5189 X(win
) += border_width
;
5190 Y(win
) += border_width
;
5191 HEIGHT(win
) -= 2 * border_width
;
5192 WIDTH(win
) -= 2 * border_width
;
5195 /* Normal floating window. */
5196 /* Update geometry if new region. */
5198 load_float_geom(win
);
5200 if (((win
->quirks
& SWM_Q_FULLSCREEN
) &&
5201 WIDTH(win
) >= WIDTH(r
) && HEIGHT(win
) >= HEIGHT(r
)) ||
5202 ((!WS_FOCUSED(win
->ws
) || win
->ws
->focus
!= win
) &&
5203 (win
->quirks
& SWM_Q_MINIMALBORDER
))) {
5205 win
->bordered
= false;
5206 } else if (!MANUAL(win
)) {
5207 if (TRANS(win
) && (win
->quirks
& SWM_Q_TRANSSZ
)) {
5208 /* Adjust size on TRANSSZ quirk. */
5209 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
5210 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
5213 if (!(win
->quirks
& SWM_Q_ANYWHERE
)) {
5215 * Floaters and transients are auto-centred
5216 * unless manually moved, resized or ANYWHERE
5219 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2;
5220 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2;
5221 store_float_geom(win
);
5226 /* Ensure at least 1 pixel of the window is in the region. */
5227 region_containment(win
, r
, SWM_CW_ALLSIDES
);
5233 * Send keystrokes to terminal to decrease/increase the font size as the
5234 * window size changes.
5237 adjust_font(struct ws_win
*win
)
5239 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
5240 ABOVE(win
) || TRANS(win
))
5243 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
5244 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
5245 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
5246 win
->font_size_boundary
[win
->font_steps
] =
5247 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
5250 win
->last_inc
= win
->sh
.width_inc
;
5251 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
5252 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
5253 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
5256 win
->last_inc
= win
->sh
.width_inc
;
5257 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
5261 #define SWAPXY(g) do { \
5263 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
5264 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
5267 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, bool flip
)
5269 struct swm_geometry cell
, r_g
= *g
;
5271 int i
= 0, j
= 0, s
= 0, stacks
= 0;
5272 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
5273 int hrh
= 0, extra
= 0, h_slice
= 0, last_h
= 0;
5274 int split
= 0, colno
= 0;
5275 int winno
, mwin
= 0, msize
= 0;
5276 int remain
, missing
, v_slice
, mscale
;
5277 bool bordered
= true, reconfigure
= false;
5280 * cell: geometry for window, including frame.
5281 * mwin: # of windows in master area.
5282 * mscale: size increment of master area.
5283 * stacks: # of stack columns
5286 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
5287 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
5289 memset(&cell
, 0, sizeof(cell
));
5291 /* Prepare tiling variables, if needed. */
5292 if ((winno
= count_win(ws
, false)) > 0) {
5293 /* Find first tiled window. */
5294 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
5295 if (!FLOATING(win
) && !ICONIC(win
))
5298 /* Take into account size hints of first tiled window. */
5300 w_inc
= win
->sh
.width_inc
;
5301 w_base
= win
->sh
.base_width
;
5302 mwin
= ws
->l_state
.horizontal_mwin
;
5303 mscale
= ws
->l_state
.horizontal_msize
;
5304 stacks
= ws
->l_state
.horizontal_stacks
;
5307 w_inc
= win
->sh
.height_inc
;
5308 w_base
= win
->sh
.base_height
;
5309 mwin
= ws
->l_state
.vertical_mwin
;
5310 mscale
= ws
->l_state
.vertical_msize
;
5311 stacks
= ws
->l_state
.vertical_stacks
;
5315 cell
.x
+= border_width
;
5316 cell
.y
+= border_width
;
5318 if (stacks
> winno
- mwin
)
5319 stacks
= winno
- mwin
;
5323 h_slice
= r_g
.h
/ SWM_H_SLICE
;
5324 if (mwin
&& winno
> mwin
) {
5325 v_slice
= r_g
.w
/ SWM_V_SLICE
;
5329 cell
.w
= v_slice
* mscale
;
5331 if (w_inc
> 1 && w_inc
< v_slice
) {
5332 /* Adjust for requested size increment. */
5333 remain
= (cell
.w
- w_base
) % w_inc
;
5339 cell
.x
+= r_g
.w
- msize
;
5342 colno
= split
= winno
/ stacks
;
5343 cell
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
5344 2 * border_width
) / stacks
);
5347 hrh
= r_g
.h
/ colno
;
5348 extra
= r_g
.h
- (colno
* hrh
);
5349 cell
.h
= hrh
- 2 * border_width
;
5350 i
= j
= 0, s
= stacks
;
5353 /* Update window geometry. */
5354 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
5358 if (FLOATINGFULLMAX(win
)) {
5359 update_floater(win
);
5364 if (split
&& i
== split
) {
5365 colno
= (winno
- mwin
) / stacks
;
5366 if (s
<= (winno
- mwin
) % stacks
)
5369 hrh
= r_g
.h
/ colno
;
5370 extra
= r_g
.h
- (colno
* hrh
);
5373 cell
.x
+= cell
.w
+ 2 * border_width
+
5376 cell
.w
= (r_g
.w
- msize
-
5377 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
5379 cell
.w
+= (r_g
.w
- msize
-
5380 (stacks
* (2 * border_width
+ tile_gap
))) %
5384 cell
.x
-= cell
.w
+ 2 * border_width
+
5390 cell
.h
= hrh
- 2 * border_width
- tile_gap
;
5393 h_inc
= win
->sh
.width_inc
;
5394 h_base
= win
->sh
.base_width
;
5396 h_inc
= win
->sh
.height_inc
;
5397 h_base
= win
->sh
.base_height
;
5400 if (j
== colno
- 1) {
5401 cell
.h
= hrh
+ extra
;
5402 } else if (h_inc
> 1 && h_inc
< h_slice
) {
5403 /* adjust for window's requested size increment */
5404 remain
= (cell
.h
- h_base
) % h_inc
;
5405 missing
= h_inc
- remain
;
5407 if (missing
<= extra
|| j
== 0) {
5417 cell
.y
= r_g
.y
+ border_width
;
5419 cell
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
5421 /* Window coordinates exclude frame. */
5423 if (winno
> 1 || !disable_border
||
5424 (bar_enabled
&& ws
->bar_enabled
)) {
5431 if (X(win
) != cell
.y
|| Y(win
) != cell
.x
||
5432 WIDTH(win
) != cell
.h
|| HEIGHT(win
) != cell
.w
) {
5436 WIDTH(win
) = cell
.h
;
5437 HEIGHT(win
) = cell
.w
;
5440 if (X(win
) != cell
.x
|| Y(win
) != cell
.y
||
5441 WIDTH(win
) != cell
.w
|| HEIGHT(win
) != cell
.h
) {
5445 WIDTH(win
) = cell
.w
;
5446 HEIGHT(win
) = cell
.h
;
5451 X(win
) -= border_width
;
5452 Y(win
) -= border_width
;
5453 WIDTH(win
) += 2 * border_width
;
5454 HEIGHT(win
) += 2 * border_width
;
5457 if (bordered
!= win
->bordered
) {
5459 win
->bordered
= bordered
;
5472 /* Stack all windows from bottom up. */
5473 TAILQ_FOREACH_REVERSE(win
, &ws
->stack
, ws_win_stack
, stack_entry
)
5475 update_win_stacking(win
);
5477 /* Map all windows from top down. */
5478 TAILQ_FOREACH(win
, &ws
->stack
, stack_entry
)
5482 DNPRINTF(SWM_D_STACK
, "stack_master: done\n");
5486 vertical_config(struct workspace
*ws
, int id
)
5488 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
5492 case SWM_ARG_ID_STACKINIT
:
5493 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
5494 ws
->l_state
.vertical_mwin
= 1;
5495 ws
->l_state
.vertical_stacks
= 1;
5497 case SWM_ARG_ID_MASTERSHRINK
:
5498 if (ws
->l_state
.vertical_msize
> 1)
5499 ws
->l_state
.vertical_msize
--;
5501 case SWM_ARG_ID_MASTERGROW
:
5502 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
5503 ws
->l_state
.vertical_msize
++;
5505 case SWM_ARG_ID_MASTERADD
:
5506 ws
->l_state
.vertical_mwin
++;
5508 case SWM_ARG_ID_MASTERDEL
:
5509 if (ws
->l_state
.vertical_mwin
> 0)
5510 ws
->l_state
.vertical_mwin
--;
5512 case SWM_ARG_ID_STACKBALANCE
:
5513 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ (ws
->l_state
.vertical_stacks
+ 1);
5515 case SWM_ARG_ID_STACKINC
:
5516 ws
->l_state
.vertical_stacks
++;
5518 case SWM_ARG_ID_STACKDEC
:
5519 if (ws
->l_state
.vertical_stacks
> 1)
5520 ws
->l_state
.vertical_stacks
--;
5522 case SWM_ARG_ID_FLIPLAYOUT
:
5523 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
5531 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5533 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
5535 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
5539 horizontal_config(struct workspace
*ws
, int id
)
5541 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
5544 case SWM_ARG_ID_STACKINIT
:
5545 ws
->l_state
.horizontal_mwin
= 1;
5546 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
5547 ws
->l_state
.horizontal_stacks
= 1;
5549 case SWM_ARG_ID_MASTERSHRINK
:
5550 if (ws
->l_state
.horizontal_msize
> 1)
5551 ws
->l_state
.horizontal_msize
--;
5553 case SWM_ARG_ID_MASTERGROW
:
5554 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
5555 ws
->l_state
.horizontal_msize
++;
5557 case SWM_ARG_ID_MASTERADD
:
5558 ws
->l_state
.horizontal_mwin
++;
5560 case SWM_ARG_ID_MASTERDEL
:
5561 if (ws
->l_state
.horizontal_mwin
> 0)
5562 ws
->l_state
.horizontal_mwin
--;
5564 case SWM_ARG_ID_STACKBALANCE
:
5565 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ (ws
->l_state
.horizontal_stacks
+ 1);
5567 case SWM_ARG_ID_STACKINC
:
5568 ws
->l_state
.horizontal_stacks
++;
5570 case SWM_ARG_ID_STACKDEC
:
5571 if (ws
->l_state
.horizontal_stacks
> 1)
5572 ws
->l_state
.horizontal_stacks
--;
5574 case SWM_ARG_ID_FLIPLAYOUT
:
5575 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
5583 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5585 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
5587 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
5590 /* fullscreen view */
5592 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5594 struct swm_geometry gg
= *g
;
5595 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
, *tmpw
;
5597 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
5602 if (count_win(ws
, true) == 0)
5605 /* Figure out which top level window should be visible. */
5606 if (ws
->focus_pending
)
5607 win
= ws
->focus_pending
;
5610 else if (ws
->focus_prev
)
5611 win
= ws
->focus_prev
;
5613 win
= TAILQ_FIRST(&ws
->winlist
);
5615 DNPRINTF(SWM_D_STACK
, "max_stack: focus_pending: %#x, focus: %#x, "
5616 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws
->focus_pending
),
5617 WINID(ws
->focus
), WINID(ws
->focus_prev
),
5618 WINID(TAILQ_FIRST(&ws
->winlist
)), win
->id
);
5620 /* Update window geometry. */
5621 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5625 if (TRANS(w
) || ABOVE(w
)) {
5630 /* Set maximized flag for all maxed windows. */
5631 if (!MAXIMIZED(w
)) {
5632 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_MAXIMIZED
);
5633 ewmh_update_wm_state(w
);
5636 /* Only reconfigure if necessary. */
5637 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
5638 HEIGHT(w
) != gg
.h
) {
5641 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
)) {
5642 w
->bordered
= false;
5643 WIDTH(w
) += 2 * border_width
;
5644 HEIGHT(w
) += 2 * border_width
;
5647 X(w
) += border_width
;
5648 Y(w
) += border_width
;
5655 /* If transient, stack parent and its children. */
5656 if (TRANS(win
) && (parent
= find_window(win
->transient
))) {
5657 raise_window(parent
);
5659 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5660 if (w
->transient
== parent
->id
)
5664 /* Make sure focus window is on top. */
5667 /* Stack any children of focus window. */
5668 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5669 if (w
->transient
== win
->id
)
5672 /* Map all windows. */
5673 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
5679 max_config(struct workspace
*ws
, int id
)
5681 /* suppress unused warning since vars are needed */
5685 DNPRINTF(SWM_D_STACK
, "max_config: workspace: %d (noop)\n", ws
->idx
);
5689 send_to_rg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5691 int ridx
= args
->id
, i
, num_screens
;
5692 struct swm_region
*rr
= NULL
;
5695 num_screens
= get_screen_count();
5696 /* do nothing if we don't have more than one screen */
5697 if (!(num_screens
> 1 || outputs
> 1))
5700 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
5702 rr
= TAILQ_FIRST(&r
->s
->rl
);
5703 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
5704 rr
= TAILQ_NEXT(rr
, entry
);
5711 send_to_ws(bp
, r
, &a
);
5715 region_under(struct swm_screen
*s
, int x
, int y
)
5717 struct swm_region
*r
= NULL
;
5719 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
5720 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
5721 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
5722 WIDTH(r
), HEIGHT(r
), x
, y
);
5723 if (X(r
) <= x
&& x
< MAX_X(r
))
5724 if (Y(r
) <= y
&& y
< MAX_Y(r
))
5731 /* Transfer focused window to target workspace and focus. */
5733 send_to_ws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5735 int wsid
= args
->id
;
5736 struct ws_win
*win
= NULL
;
5740 if (r
&& r
->ws
&& r
->ws
->focus
)
5745 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win %#x, ws %d\n", win
->id
, wsid
);
5747 if (wsid
< 0 || wsid
>= workspace_limit
)
5750 if (win
->ws
->idx
== wsid
)
5753 win_to_ws(win
, wsid
, true);
5755 /* Set new focus on target ws. */
5756 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5757 win
->ws
->focus_prev
= win
->ws
->focus
;
5758 win
->ws
->focus
= win
;
5759 win
->ws
->focus_pending
= NULL
;
5761 if (win
->ws
->focus_prev
)
5762 draw_frame(win
->ws
->focus_prev
);
5765 DNPRINTF(SWM_D_STACK
, "send_to_ws: focus_pending: %#x, focus: %#x, "
5766 "focus_prev: %#x, first: %#x, win: %#x\n",
5767 WINID(r
->ws
->focus_pending
), WINID(r
->ws
->focus
),
5768 WINID(r
->ws
->focus_prev
), WINID(TAILQ_FIRST(&r
->ws
->winlist
)),
5771 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5772 ewmh_update_wm_state(win
);
5774 /* Restack focused region. */
5777 /* If destination ws has a region, restack. */
5780 load_float_geom(win
);
5785 /* Set new focus on current ws. */
5786 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5787 if (r
->ws
->focus
!= NULL
) {
5788 focus_win(r
->ws
->focus
);
5790 DNPRINTF(SWM_D_FOCUS
, "send_to_ws: set_input_focus: "
5791 "%#x, revert-to: parent, time: 0\n", r
->id
);
5792 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
5793 r
->id
, XCB_CURRENT_TIME
);
5802 /* Transfer focused window to region-relative workspace and focus. */
5804 send_to_rg_relative(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5807 struct swm_region
*r_other
;
5809 if (args
->id
== 1) {
5810 r_other
= TAILQ_NEXT(r
, entry
);
5811 if (r_other
== NULL
)
5812 r_other
= TAILQ_FIRST(&r
->s
->rl
);
5814 r_other
= TAILQ_PREV(r
, swm_region_list
, entry
);
5815 if (r_other
== NULL
)
5816 r_other
= TAILQ_LAST(&r
->s
->rl
, swm_region_list
);
5819 /* Map relative to absolute */
5821 args_abs
.id
= r_other
->ws
->idx
;
5823 send_to_ws(bp
, r
, &args_abs
);
5827 win_to_ws(struct ws_win
*win
, int wsid
, bool unfocus
)
5829 struct ws_win
*parent
;
5830 struct workspace
*ws
, *nws
, *pws
;
5832 if (wsid
< 0 || wsid
>= workspace_limit
)
5835 if (win
->ws
->idx
== wsid
)
5839 nws
= &win
->s
->ws
[wsid
];
5841 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win %#x, ws %d -> %d\n", win
->id
,
5844 /* Cleanup focus on source ws. */
5845 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
5846 (ws
->focus
== win
|| ws
->focus_pending
== win
))
5847 ws
->focus_pending
= get_focus_prev(win
);
5849 /* Move the parent if this is a transient window. */
5851 parent
= find_window(win
->transient
);
5854 /* Set new focus in parent's ws if needed. */
5855 if (pws
->focus
== parent
) {
5856 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5857 pws
->focus_pending
=
5858 get_focus_prev(parent
);
5860 unfocus_win(parent
);
5862 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5863 pws
->focus
= pws
->focus_pending
;
5864 pws
->focus_pending
= NULL
;
5868 /* Don't unmap parent if new ws is visible */
5870 unmap_window(parent
);
5873 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
5874 TAILQ_REMOVE(&ws
->stack
, parent
, stack_entry
);
5875 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
5876 TAILQ_INSERT_TAIL(&nws
->stack
, parent
, stack_entry
);
5879 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5880 "_NET_WM_DESKTOP: %d\n", wsid
);
5881 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5882 parent
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
5883 XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5890 if (ws
->focus_prev
== win
)
5891 ws
->focus_prev
= NULL
;
5893 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& ws
->focus_pending
!= NULL
) {
5894 ws
->focus
= ws
->focus_pending
;
5895 ws
->focus_pending
= NULL
;
5898 /* Don't unmap if new ws is visible */
5903 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
5904 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
5905 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
5906 TAILQ_INSERT_TAIL(&nws
->stack
, win
, stack_entry
);
5909 /* Update the window's workspace property: _NET_WM_DESKTOP */
5910 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5911 "_NET_WM_DESKTOP: %d\n", wsid
);
5912 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
5913 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5915 ewmh_update_client_list();
5917 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
5921 pressbutton(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5923 /* suppress unused warning since var is needed */
5927 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
5928 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5929 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
5930 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5934 raise_focus(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5939 /* Suppress warning. */
5943 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
5947 r
->ws
->focus_raise
= win
;
5950 /* Temporarily override stacking order also in the stack */
5951 if (!FLOATING(win
)) {
5952 val
= XCB_STACK_MODE_ABOVE
;
5953 xcb_configure_window(conn
, win
->frame
,
5954 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
5959 raise_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5961 /* Suppress warning. */
5965 if (r
== NULL
|| r
->ws
== NULL
)
5968 if (r
->ws
->focus
&& MAXIMIZED(r
->ws
->focus
))
5971 r
->ws
->always_raise
= !r
->ws
->always_raise
;
5973 /* Update focused win stacking order based on new always_raise value. */
5974 raise_window(r
->ws
->focus
);
5980 iconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5984 /* Suppress warning. */
5988 if ((w
= r
->ws
->focus
) == NULL
)
5991 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_HIDDEN
);
5992 ewmh_update_wm_state(w
);
6000 get_win_name(xcb_window_t win
)
6003 xcb_get_property_cookie_t c
;
6004 xcb_get_property_reply_t
*r
;
6006 /* First try _NET_WM_NAME for UTF-8. */
6007 c
= xcb_get_property(conn
, 0, win
, ewmh
[_NET_WM_NAME
].atom
,
6008 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
6009 r
= xcb_get_property_reply(conn
, c
, NULL
);
6010 if (r
&& r
->type
== XCB_NONE
) {
6012 /* Use WM_NAME instead; no UTF-8. */
6013 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
6014 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
6015 r
= xcb_get_property_reply(conn
, c
, NULL
);
6018 if (r
&& r
->type
!= XCB_NONE
&& r
->length
> 0)
6019 name
= strndup(xcb_get_property_value(r
),
6020 xcb_get_property_value_length(r
));
6025 err(1, "get_win_name: failed to allocate memory.");
6033 uniconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6042 DNPRINTF(SWM_D_MISC
, "uniconify\n");
6044 if (r
== NULL
|| r
->ws
== NULL
)
6047 /* make sure we have anything to uniconify */
6048 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
6049 if (win
->ws
== NULL
)
6050 continue; /* should never happen */
6056 DNPRINTF(SWM_D_MISC
, "uniconify: count: %d\n", count
);
6062 search_resp_action
= SWM_SEARCH_UNICONIFY
;
6064 spawn_select(r
, args
, "search", &searchpid
);
6066 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
6069 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
6070 if (win
->ws
== NULL
)
6071 continue; /* should never happen */
6075 name
= get_win_name(win
->id
);
6076 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
6084 name_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6090 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
6096 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
6098 spawn_select(r
, args
, "name_workspace", &searchpid
);
6100 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
6103 fprintf(lfile
, "%s", "");
6108 search_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6111 struct workspace
*ws
;
6116 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
6122 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
6124 spawn_select(r
, args
, "search", &searchpid
);
6126 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
6129 for (i
= 0; i
< workspace_limit
; i
++) {
6133 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
6134 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
6141 search_win_cleanup(void)
6143 struct search_window
*sw
= NULL
;
6145 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
6146 xcb_destroy_window(conn
, sw
->indicator
);
6147 TAILQ_REMOVE(&search_wl
, sw
, entry
);
6153 search_win(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6155 struct ws_win
*win
= NULL
;
6156 struct search_window
*sw
= NULL
;
6159 xcb_screen_t
*screen
;
6160 int i
, width
, height
;
6168 XRectangle l_ibox
, l_lbox
;
6172 DNPRINTF(SWM_D_MISC
, "search_win\n");
6175 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
6177 spawn_select(r
, args
, "search", &searchpid
);
6179 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
6182 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
6183 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
6185 TAILQ_INIT(&search_wl
);
6188 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
6192 sw
= calloc(1, sizeof(struct search_window
));
6194 warn("search_win: calloc");
6196 search_win_cleanup();
6202 snprintf(s
, sizeof s
, "%d", i
);
6205 w
= xcb_generate_id(conn
);
6206 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6207 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6208 wa
[2] = screen
->default_colormap
;
6210 if (bar_font_legacy
) {
6211 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
6212 width
= l_lbox
.width
+ 4;
6213 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
6215 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
6217 width
= info
.width
+ 4;
6218 height
= bar_font
->height
+ 4;
6221 xcb_create_window(conn
, screen
->root_depth
, w
, win
->frame
, 0, 0,
6222 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
6223 screen
->root_visual
, XCB_CW_BACK_PIXEL
|
6224 XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
, wa
);
6226 xcb_map_window(conn
, w
);
6229 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
6231 if (bar_font_legacy
) {
6232 l_gcv
.graphics_exposures
= 0;
6233 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
6235 XSetForeground(display
, l_draw
,
6236 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
6238 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
6239 (bar_fs_extents
->max_logical_extent
.height
-
6240 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
6242 XFreeGC(display
, l_draw
);
6245 draw
= XftDrawCreate(display
, w
,
6246 DefaultVisual(display
, r
->s
->idx
),
6247 DefaultColormap(display
, r
->s
->idx
));
6249 XftDrawStringUtf8(draw
, &search_font_color
, bar_font
, 2,
6250 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
6251 bar_font
->descent
, (FcChar8
*)s
, len
);
6253 XftDrawDestroy(draw
);
6256 DNPRINTF(SWM_D_MISC
, "search_win: mapped win %#x\n", w
);
6258 fprintf(lfile
, "%d\n", i
);
6268 search_resp_uniconify(const char *resp
, size_t len
)
6274 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
6276 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
6279 name
= get_win_name(win
->id
);
6280 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
6285 if (strncmp(s
, resp
, len
) == 0) {
6286 /* XXX this should be a callback to generalize */
6287 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_HIDDEN
);
6288 ewmh_update_wm_state(win
);
6298 search_resp_name_workspace(const char *resp
, size_t len
)
6300 struct workspace
*ws
;
6302 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
6304 if (search_r
->ws
== NULL
)
6309 free(search_r
->ws
->name
);
6310 search_r
->ws
->name
= NULL
;
6314 ws
->name
= strdup(resp
);
6315 if (ws
->name
== NULL
) {
6316 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
6317 "strdup: %s", strerror(errno
));
6321 ewmh_update_desktop_names();
6322 ewmh_get_desktop_names();
6327 ewmh_update_desktop_names(void)
6329 char *name_list
= NULL
, *p
;
6330 int num_screens
, i
, j
, len
= 0, tot
= 0;
6332 num_screens
= get_screen_count();
6333 for (i
= 0; i
< num_screens
; ++i
) {
6334 for (j
= 0; j
< workspace_limit
; ++j
) {
6335 if (screens
[i
].ws
[j
].name
!= NULL
)
6336 len
+= strlen(screens
[i
].ws
[j
].name
);
6340 if ((name_list
= calloc(len
, sizeof(char))) == NULL
)
6341 err(1, "update_desktop_names: calloc: failed to "
6342 "allocate memory.");
6345 for (j
= 0; j
< workspace_limit
; ++j
) {
6346 if (screens
[i
].ws
[j
].name
!= NULL
) {
6347 len
= strlen(screens
[i
].ws
[j
].name
);
6348 memcpy(p
, screens
[i
].ws
[j
].name
, len
);
6357 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6358 screens
[i
].root
, ewmh
[_NET_DESKTOP_NAMES
].atom
,
6359 a_utf8_string
, 8, tot
, name_list
);
6369 ewmh_get_desktop_names(void)
6372 xcb_get_property_cookie_t c
;
6373 xcb_get_property_reply_t
*r
;
6374 int num_screens
, i
, j
, n
, k
;
6376 num_screens
= get_screen_count();
6377 for (i
= 0; i
< num_screens
; ++i
) {
6378 for (j
= 0; j
< workspace_limit
; ++j
) {
6379 free(screens
[i
].ws
[j
].name
);
6380 screens
[i
].ws
[j
].name
= NULL
;
6383 c
= xcb_get_property(conn
, 0, screens
[i
].root
,
6384 ewmh
[_NET_DESKTOP_NAMES
].atom
,
6385 a_utf8_string
, 0, UINT32_MAX
);
6386 r
= xcb_get_property_reply(conn
, c
, NULL
);
6390 names
= xcb_get_property_value(r
);
6391 n
= xcb_get_property_value_length(r
);
6393 for (j
= 0, k
= 0; j
< n
; ++j
) {
6394 if (*(names
+ j
) != '\0') {
6395 screens
[i
].ws
[k
].name
= strdup(names
+ j
);
6396 j
+= strlen(names
+ j
);
6405 ewmh_update_client_list(void)
6408 int num_screens
, i
, j
, k
= 0, count
= 0;
6411 num_screens
= get_screen_count();
6412 for (i
= 0; i
< num_screens
; ++i
) {
6413 for (j
= 0; j
< workspace_limit
; ++j
)
6414 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6417 DNPRINTF(SWM_D_PROP
, "ewmh_update_client_list: win count: %d\n",
6423 wins
= calloc(count
, sizeof(xcb_window_t
));
6425 err(1, "ewmh_update_client_list: calloc: failed to "
6426 "allocate memory.");
6428 for (j
= 0, k
= 0; j
< workspace_limit
; ++j
)
6429 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6430 wins
[k
++] = win
->id
;
6432 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6433 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
6434 XCB_ATOM_WINDOW
, 32, count
, wins
);
6441 ewmh_update_current_desktop(void)
6445 num_screens
= get_screen_count();
6446 for (i
= 0; i
< num_screens
; ++i
) {
6447 if (screens
[i
].r_focus
)
6448 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6449 screens
[i
].root
, ewmh
[_NET_CURRENT_DESKTOP
].atom
,
6450 XCB_ATOM_CARDINAL
, 32, 1,
6451 &screens
[i
].r_focus
->ws
->idx
);
6456 ewmh_update_desktops(void)
6458 int num_screens
, i
, j
;
6461 vals
= calloc(workspace_limit
* 2, sizeof(uint32_t));
6463 err(1, "ewmh_update_desktops: calloc: failed to allocate "
6466 num_screens
= get_screen_count();
6467 for (i
= 0; i
< num_screens
; i
++) {
6468 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6469 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
6470 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
6472 for (j
= 0; j
< workspace_limit
; ++j
) {
6473 if (screens
[i
].ws
[j
].r
!= NULL
) {
6474 vals
[j
* 2] = X(screens
[i
].ws
[j
].r
);
6475 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].r
);
6476 } else if (screens
[i
].ws
[j
].old_r
!= NULL
) {
6477 vals
[j
* 2] = X(screens
[i
].ws
[j
].old_r
);
6478 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].old_r
);
6480 vals
[j
* 2] = vals
[j
* 2 + 1] = 0;
6484 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6485 screens
[i
].root
, ewmh
[_NET_DESKTOP_VIEWPORT
].atom
,
6486 XCB_ATOM_CARDINAL
, 32, workspace_limit
* 2, vals
);
6493 search_resp_search_workspace(const char *resp
)
6500 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
6504 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
6511 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
6513 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
6520 switchws(NULL
, search_r
, &a
);
6524 search_resp_search_window(const char *resp
)
6529 struct search_window
*sw
;
6531 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
6535 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
6540 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
6542 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
6549 TAILQ_FOREACH(sw
, &search_wl
, entry
)
6550 if (idx
== sw
->idx
) {
6556 #define MAX_RESP_LEN 1024
6559 search_do_resp(void)
6565 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
6570 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
6571 warn("search: calloc");
6575 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
6577 warn("search: read error");
6580 resp
[rbytes
] = '\0';
6583 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
6584 * newline, so work around that by sanitizing the resp now.
6586 resp
[strcspn(resp
, "\n")] = '\0';
6589 switch (search_resp_action
) {
6590 case SWM_SEARCH_UNICONIFY
:
6591 search_resp_uniconify(resp
, len
);
6593 case SWM_SEARCH_NAME_WORKSPACE
:
6594 search_resp_name_workspace(resp
, len
);
6596 case SWM_SEARCH_SEARCH_WORKSPACE
:
6597 search_resp_search_workspace(resp
);
6599 case SWM_SEARCH_SEARCH_WINDOW
:
6600 search_resp_search_window(resp
);
6605 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
6606 search_win_cleanup();
6608 search_resp_action
= SWM_SEARCH_NONE
;
6609 close(select_resp_pipe
[0]);
6616 wkill(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6620 DNPRINTF(SWM_D_MISC
, "wkill: win %#x, id: %d\n", WINID(r
->ws
->focus
),
6623 if (r
->ws
->focus
== NULL
)
6626 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
6627 xcb_kill_client(conn
, r
->ws
->focus
->id
);
6629 if (r
->ws
->focus
->can_delete
)
6630 client_msg(r
->ws
->focus
, a_delete
, 0);
6636 clear_maximized(struct workspace
*ws
)
6641 /* Clear any maximized win(s) on ws, from bottom up. */
6642 TAILQ_FOREACH_REVERSE(w
, &ws
->stack
, ws_win_stack
, stack_entry
)
6644 ewmh_apply_flags(w
, w
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
6645 ewmh_update_wm_state(w
);
6653 maximize_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6655 struct ws_win
*w
= r
->ws
->focus
;
6657 /* suppress unused warning since var is needed */
6664 DNPRINTF(SWM_D_MISC
, "maximize_toggle: win %#x\n", w
->id
);
6669 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6672 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_MAXIMIZED
);
6673 ewmh_update_wm_state(w
);
6677 if (w
== w
->ws
->focus
)
6682 DNPRINTF(SWM_D_MISC
, "maximize_toggle: done\n");
6686 floating_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6688 struct ws_win
*w
= r
->ws
->focus
;
6690 /* suppress unused warning since var is needed */
6697 DNPRINTF(SWM_D_MISC
, "floating_toggle: win %#x\n", w
->id
);
6699 if (FULLSCREEN(w
) || TRANS(w
))
6702 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6705 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_ABOVE
);
6706 ewmh_update_wm_state(w
);
6710 if (w
== w
->ws
->focus
)
6715 DNPRINTF(SWM_D_MISC
, "floating_toggle: done\n");
6719 fullscreen_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6721 struct ws_win
*w
= r
->ws
->focus
;
6723 /* suppress unused warning since var is needed */
6730 DNPRINTF(SWM_D_MISC
, "fullscreen_toggle: win %#x\n", w
->id
);
6732 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_FULLSCREEN
);
6733 ewmh_update_wm_state(w
);
6737 if (w
== w
->ws
->focus
)
6742 DNPRINTF(SWM_D_MISC
, "fullscreen_toggle: done\n");
6745 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
6747 struct swm_geometry g
= r
->g
;
6748 int rt
, lt
, tp
, bm
, bw
;
6750 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
6753 * Perpendicular distance of each side of the window to the respective
6754 * side of the region boundary. Positive values indicate the side of
6755 * the window has passed beyond the region boundary.
6757 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) - MAX_X(r
) : bw
;
6758 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) : bw
;
6759 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) - MAX_Y(r
) : bw
;
6760 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) : bw
;
6762 DNPRINTF(SWM_D_MISC
, "region_containment: win %#x, rt: %d, lt: %d, "
6763 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
6764 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
6765 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
6768 * Disable containment if any of the flagged sides went beyond the
6769 * containment boundary, or if containment is disabled.
6771 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
6772 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
6773 /* Make sure window has at least 1 pixel in the region */
6774 g
.x
+= 1 - WIDTH(win
);
6775 g
.y
+= 1 - HEIGHT(win
);
6776 g
.w
+= 2 * WIDTH(win
) - 2;
6777 g
.h
+= 2 * HEIGHT(win
) - 2;
6780 constrain_window(win
, &g
, &opts
);
6783 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6785 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
6787 DNPRINTF(SWM_D_MISC
, "constrain_window: win %#x, (x,y) w x h: "
6788 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6789 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
6790 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
6791 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
6792 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
6793 YESNO(*opts
& SWM_CW_RESIZABLE
));
6795 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) > b
->x
+ b
->w
) {
6796 if (*opts
& SWM_CW_RESIZABLE
)
6797 WIDTH(win
) = b
->x
+ b
->w
- X(win
);
6799 X(win
) = b
->x
+ b
->w
- WIDTH(win
);
6802 if ((*opts
& SWM_CW_LEFT
) && X(win
) < b
->x
) {
6803 if (*opts
& SWM_CW_RESIZABLE
)
6804 WIDTH(win
) -= b
->x
- X(win
);
6809 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) > b
->y
+ b
->h
) {
6810 if (*opts
& SWM_CW_RESIZABLE
)
6811 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
);
6813 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
);
6816 if ((*opts
& SWM_CW_TOP
) && Y(win
) < b
->y
) {
6817 if (*opts
& SWM_CW_RESIZABLE
)
6818 HEIGHT(win
) -= b
->y
- Y(win
);
6823 if (*opts
& SWM_CW_RESIZABLE
) {
6826 if (HEIGHT(win
) < 1)
6832 draw_frame(struct ws_win
*win
)
6834 xcb_rectangle_t rect
[4];
6838 if (win
->frame
== XCB_WINDOW_NONE
) {
6839 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x not "
6840 "reparented.\n", win
->id
);
6844 if (!win
->bordered
) {
6845 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x frame "
6846 "disabled\n", win
->id
);
6849 if (WS_FOCUSED(win
->ws
) && win
->ws
->focus
== win
)
6850 pixel
= MAXIMIZED(win
) ?
6851 &win
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].pixel
:
6852 &win
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6854 pixel
= MAXIMIZED(win
) ?
6855 &win
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].pixel
:
6856 &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6858 /* Top (with corners) */
6861 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6862 rect
[n
].height
= border_width
;
6863 /* Left (without corners) */
6865 rect
[n
].y
= border_width
;
6866 rect
[n
].width
= border_width
;
6867 rect
[n
].height
= HEIGHT(win
);
6868 /* Right (without corners) */
6869 rect
[++n
].x
= border_width
+ WIDTH(win
);
6870 rect
[n
].y
= border_width
;
6871 rect
[n
].width
= border_width
;
6872 rect
[n
].height
= HEIGHT(win
);
6873 /* Bottom (with corners)*/
6875 rect
[n
].y
= border_width
+ HEIGHT(win
);
6876 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6877 rect
[n
].height
= border_width
;
6879 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, pixel
);
6880 xcb_poly_fill_rectangle(conn
, win
->frame
, win
->s
->bar_gc
, 4, rect
);
6884 update_window(struct ws_win
*win
)
6889 if (win
->frame
== XCB_WINDOW_NONE
) {
6890 DNPRINTF(SWM_D_EVENT
, "update_window: skip win %#x; "
6891 "not reparented.\n", win
->id
);
6895 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
6896 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
6897 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6899 /* Reconfigure frame. */
6900 if (win
->bordered
) {
6901 wc
[0] = X(win
) - border_width
;
6902 wc
[1] = Y(win
) - border_width
;
6903 wc
[2] = WIDTH(win
) + 2 * border_width
;
6904 wc
[3] = HEIGHT(win
) + 2 * border_width
;
6909 wc
[3] = HEIGHT(win
);
6914 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x frame %#x, (x,y) w x h: "
6915 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, win
->frame
, wc
[0],
6916 wc
[1], wc
[2], wc
[3], YESNO(win
->bordered
));
6918 xcb_configure_window(conn
, win
->frame
, mask
, wc
);
6920 /* Reconfigure client window. */
6921 wc
[0] = wc
[1] = win
->bordered
? border_width
: 0;
6923 wc
[3] = HEIGHT(win
);
6925 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x, (x,y) w x h: "
6926 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
6927 wc
[3], YESNO(win
->bordered
));
6928 xcb_configure_window(conn
, win
->id
, mask
, wc
);
6930 /* ICCCM 4.2.3 Synthetic ConfigureNotify when moved and not resized. */
6931 if ((X(win
) != win
->g_prev
.x
|| Y(win
) != win
->g_prev
.y
) &&
6932 (win
->java
|| (WIDTH(win
) == win
->g_prev
.w
&&
6933 HEIGHT(win
) == win
->g_prev
.h
))) {
6934 /* Java has special needs when moved together with a resize. */
6935 config_win(win
, NULL
);
6938 win
->g_prev
= win
->g
;
6942 SIMPLEQ_ENTRY(event
) entry
;
6943 xcb_generic_event_t
*ev
;
6945 SIMPLEQ_HEAD(event_queue
, event
) events
= SIMPLEQ_HEAD_INITIALIZER(events
);
6947 xcb_generic_event_t
*
6948 get_next_event(bool dowait
)
6951 xcb_generic_event_t
*evt
;
6953 /* Try queue first. */
6954 if ((ep
= SIMPLEQ_FIRST(&events
))) {
6956 SIMPLEQ_REMOVE_HEAD(&events
, entry
);
6959 evt
= xcb_wait_for_event(conn
);
6961 evt
= xcb_poll_for_event(conn
);
6967 put_back_event(xcb_generic_event_t
*evt
)
6970 if ((ep
= malloc(sizeof (struct event
))) == NULL
)
6971 err(1, "put_back_event: failed to allocate memory.");
6973 SIMPLEQ_INSERT_HEAD(&events
, ep
, entry
);
6976 /* Peeks at next event to detect auto-repeat. */
6978 keyrepeating(xcb_key_release_event_t
*kre
)
6980 xcb_generic_event_t
*evt
;
6982 /* Ensure repeating keypress is finished processing. */
6985 if ((evt
= get_next_event(false))) {
6986 put_back_event(evt
);
6988 if (XCB_EVENT_RESPONSE_TYPE(evt
) == XCB_KEY_PRESS
&&
6989 kre
->sequence
== evt
->sequence
&&
6990 kre
->detail
== ((xcb_key_press_event_t
*)evt
)->detail
)
6998 keybindreleased(struct binding
*bp
, xcb_key_release_event_t
*kre
)
7000 if (bp
->type
== KEYBIND
&& !keyrepeating(kre
) &&
7001 bp
->value
== xcb_key_press_lookup_keysym(syms
, kre
, 0))
7007 #define SWM_RESIZE_STEPS (50)
7010 resize_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
7012 xcb_timestamp_t timestamp
= 0;
7013 struct swm_region
*r
= NULL
;
7014 struct swm_geometry g
;
7015 int top
= 0, left
= 0;
7017 xcb_cursor_t cursor
;
7018 xcb_query_pointer_reply_t
*xpr
= NULL
;
7019 xcb_generic_event_t
*evt
;
7020 xcb_motion_notify_event_t
*mne
;
7021 bool resizing
, step
= false;
7027 if (FULLSCREEN(win
))
7030 /* In max_stack mode, should only resize transients/floating. */
7031 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
) && !ABOVE(win
))
7034 DNPRINTF(SWM_D_EVENT
, "resize: win %#x, floating: %s, "
7035 "transient: %#x\n", win
->id
, YESNO(ABOVE(win
)),
7039 store_float_geom(win
);
7040 else if (!(TRANS(win
) || ABOVE(win
)))
7043 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
7045 ewmh_update_wm_state(win
);
7051 /* It's possible for win to have been freed during focus_flush(). */
7052 if (validate_win(win
)) {
7053 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
7058 case SWM_ARG_ID_WIDTHSHRINK
:
7059 WIDTH(win
) -= SWM_RESIZE_STEPS
;
7062 case SWM_ARG_ID_WIDTHGROW
:
7063 WIDTH(win
) += SWM_RESIZE_STEPS
;
7066 case SWM_ARG_ID_HEIGHTSHRINK
:
7067 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
7070 case SWM_ARG_ID_HEIGHTGROW
:
7071 HEIGHT(win
) += SWM_RESIZE_STEPS
;
7078 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
7079 SWM_CW_HARDBOUNDARY
);
7081 store_float_geom(win
);
7085 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
7086 SWM_CW_SOFTBOUNDARY
);
7089 /* get cursor offset from window root */
7090 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
7097 if (xpr
->win_x
< WIDTH(win
) / 2)
7100 if (xpr
->win_y
< HEIGHT(win
) / 2)
7103 if (opt
== SWM_ARG_ID_CENTER
)
7104 cursor
= cursors
[XC_SIZING
].cid
;
7106 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
7107 XC_TOP_RIGHT_CORNER
].cid
;
7109 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
7110 XC_BOTTOM_RIGHT_CORNER
].cid
;
7112 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
7113 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
7116 /* Release keyboard freeze if called via keybind. */
7117 if (bp
->type
== KEYBIND
)
7118 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7123 while (resizing
&& (evt
= get_next_event(true))) {
7124 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
7125 case XCB_BUTTON_RELEASE
:
7126 if (bp
->type
== BTNBIND
&& bp
->value
==
7127 ((xcb_button_release_event_t
*)evt
)->detail
)
7130 case XCB_KEY_RELEASE
:
7131 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
7134 case XCB_MOTION_NOTIFY
:
7135 mne
= (xcb_motion_notify_event_t
*)evt
;
7136 DNPRINTF(SWM_D_EVENT
, "resize: MOTION_NOTIFY: "
7137 "root: %#x\n", mne
->root
);
7139 /* cursor offset/delta from start of the operation */
7140 dx
= mne
->root_x
- xpr
->root_x
;
7141 dy
= mne
->root_y
- xpr
->root_y
;
7147 if (opt
== SWM_ARG_ID_CENTER
) {
7148 if (g
.h
/ 2 + dy
< 1)
7152 HEIGHT(win
) = g
.h
+ 2 * dy
;
7160 HEIGHT(win
) = g
.h
+ dy
;
7167 if (opt
== SWM_ARG_ID_CENTER
) {
7168 if (g
.w
/ 2 + dx
< 1)
7172 WIDTH(win
) = g
.w
+ 2 * dx
;
7180 WIDTH(win
) = g
.w
+ dx
;
7183 /* not free, don't sync more than 120 times / second */
7184 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
7185 timestamp
= mne
->time
;
7186 regionize(win
, mne
->root_x
, mne
->root_y
);
7187 region_containment(win
, r
, SWM_CW_ALLSIDES
|
7188 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
7189 SWM_CW_SOFTBOUNDARY
);
7194 case XCB_BUTTON_PRESS
:
7196 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_PRESS ignored\n");
7197 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7198 ((xcb_button_press_event_t
*)evt
)->time
);
7203 DNPRINTF(SWM_D_EVENT
, "resize: KEY_PRESS ignored\n");
7204 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7205 ((xcb_key_press_event_t
*)evt
)->time
);
7211 /* It's possible for win to have been freed above. */
7212 if (validate_win(win
)) {
7213 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
7221 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
7222 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
7226 store_float_geom(win
);
7228 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7230 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
7234 resize(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
7236 struct ws_win
*win
= NULL
;
7237 xcb_query_pointer_reply_t
*qpr
= NULL
;
7242 if (args
->id
!= SWM_ARG_ID_DONTCENTER
&&
7243 args
->id
!= SWM_ARG_ID_CENTER
) {
7244 /* {height,width}_{grow,shrink} use the focus window. */
7248 /* resize uses window under pointer. */
7249 qpr
= xcb_query_pointer_reply(conn
,
7250 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7252 win
= find_window(qpr
->child
);
7258 resize_win(win
, bp
, args
->id
);
7260 if (args
->id
&& bp
->type
== KEYBIND
)
7266 /* Try to set window region based on supplied coordinates or window center. */
7268 regionize(struct ws_win
*win
, int x
, int y
)
7270 struct swm_region
*r
= NULL
;
7272 r
= region_under(win
->s
, x
, y
);
7274 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
7275 Y(win
) + HEIGHT(win
) / 2);
7277 if (r
&& r
!= win
->ws
->r
) {
7278 clear_maximized(r
->ws
);
7280 win_to_ws(win
, r
->ws
->idx
, false);
7282 /* Stack old and new region. */
7286 /* Set focus on new ws. */
7287 unfocus_win(r
->ws
->focus
);
7295 #define SWM_MOVE_STEPS (50)
7298 move_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
7300 struct swm_region
*r
;
7301 xcb_timestamp_t timestamp
= 0;
7302 xcb_query_pointer_reply_t
*qpr
= NULL
;
7303 xcb_generic_event_t
*evt
;
7304 xcb_motion_notify_event_t
*mne
;
7305 bool moving
, restack
= false, step
= false;
7310 if ((r
= win
->ws
->r
) == NULL
)
7313 if (FULLSCREEN(win
))
7316 DNPRINTF(SWM_D_EVENT
, "move: win %#x, floating: %s, transient: "
7317 "%#x\n", win
->id
, YESNO(ABOVE(win
)), win
->transient
);
7319 if (!(ABOVE(win
) || TRANS(win
)) || MAXIMIZED(win
)) {
7320 store_float_geom(win
);
7324 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
7326 ewmh_update_wm_state(win
);
7333 /* It's possible for win to have been freed during focus_flush(). */
7334 if (validate_win(win
)) {
7335 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7340 case SWM_ARG_ID_MOVELEFT
:
7341 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
7344 case SWM_ARG_ID_MOVERIGHT
:
7345 X(win
) += (SWM_MOVE_STEPS
- border_width
);
7348 case SWM_ARG_ID_MOVEUP
:
7349 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
7352 case SWM_ARG_ID_MOVEDOWN
:
7353 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
7360 regionize(win
, -1, -1);
7361 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
7363 store_float_geom(win
);
7367 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
7368 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
7369 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
7371 /* get cursor offset from window root */
7372 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
7375 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7379 /* Release keyboard freeze if called via keybind. */
7380 if (bp
->type
== KEYBIND
)
7381 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7384 regionize(win
, qpr
->root_x
, qpr
->root_y
);
7385 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7386 SWM_CW_SOFTBOUNDARY
);
7390 while (moving
&& (evt
= get_next_event(true))) {
7391 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
7392 case XCB_BUTTON_RELEASE
:
7393 if (bp
->type
== BTNBIND
&& bp
->value
==
7394 ((xcb_button_release_event_t
*)evt
)->detail
)
7397 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7398 ((xcb_button_release_event_t
*)evt
)->time
);
7401 case XCB_KEY_RELEASE
:
7402 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
7405 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7406 ((xcb_key_release_event_t
*)evt
)->time
);
7409 case XCB_MOTION_NOTIFY
:
7410 mne
= (xcb_motion_notify_event_t
*)evt
;
7411 DNPRINTF(SWM_D_EVENT
, "move: MOTION_NOTIFY: "
7412 "root: %#x\n", mne
->root
);
7413 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
7414 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
7416 /* not free, don't sync more than 120 times / second */
7417 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
7418 timestamp
= mne
->time
;
7419 regionize(win
, mne
->root_x
, mne
->root_y
);
7420 region_containment(win
, win
->ws
->r
,
7421 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
7426 case XCB_BUTTON_PRESS
:
7427 /* Thaw and ignore. */
7428 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7429 ((xcb_button_press_event_t
*)evt
)->time
);
7434 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7435 ((xcb_key_press_event_t
*)evt
)->time
);
7441 /* It's possible for win to have been freed. */
7442 if (validate_win(win
)) {
7443 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7451 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7452 SWM_CW_SOFTBOUNDARY
);
7456 store_float_geom(win
);
7458 /* New region set to fullscreen layout. */
7459 if (win
->ws
->r
&& win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
]) {
7466 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7467 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
7471 move(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
7473 struct ws_win
*win
= NULL
;
7474 xcb_query_pointer_reply_t
*qpr
= NULL
;
7480 /* move_* uses focus window. */
7484 /* Disallow move_ on tiled. */
7485 if (win
&& !(TRANS(win
) || ABOVE(win
)))
7488 /* move uses window under pointer. */
7489 qpr
= xcb_query_pointer_reply(conn
,
7490 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7492 win
= find_window(qpr
->child
);
7498 move_win(win
, bp
, args
->id
);
7500 if (args
->id
&& bp
->type
== KEYBIND
)
7506 /* action definitions */
7508 char name
[SWM_FUNCNAME_LEN
];
7509 void (*func
)(struct binding
*, struct swm_region
*,
7513 } actions
[FN_INVALID
+ 2] = {
7514 /* name function argument */
7515 { "bar_toggle", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
7516 { "bar_toggle_ws", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
7517 { "button2", pressbutton
, 0, {.id
= 2} },
7518 { "cycle_layout", cycle_layout
, 0, {0} },
7519 { "flip_layout", stack_config
, 0, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
7520 { "float_toggle", floating_toggle
,0, {0} },
7521 { "focus", focus_pointer
, 0, {0} },
7522 { "focus_main", focus
, 0, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
7523 { "focus_next", focus
, 0, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
7524 { "focus_prev", focus
, 0, {.id
= SWM_ARG_ID_FOCUSPREV
} },
7525 { "focus_urgent", focus
, 0, {.id
= SWM_ARG_ID_FOCUSURGENT
} },
7526 { "fullscreen_toggle", fullscreen_toggle
, 0, {0} },
7527 { "maximize_toggle", maximize_toggle
,0, {0} },
7528 { "height_grow", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
7529 { "height_shrink", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
7530 { "iconify", iconify
, 0, {0} },
7531 { "master_shrink", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
7532 { "master_grow", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERGROW
} },
7533 { "master_add", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERADD
} },
7534 { "master_del", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERDEL
} },
7535 { "move", move
, FN_F_NOREPLAY
, {0} },
7536 { "move_down", move
, 0, {.id
= SWM_ARG_ID_MOVEDOWN
} },
7537 { "move_left", move
, 0, {.id
= SWM_ARG_ID_MOVELEFT
} },
7538 { "move_right", move
, 0, {.id
= SWM_ARG_ID_MOVERIGHT
} },
7539 { "move_up", move
, 0, {.id
= SWM_ARG_ID_MOVEUP
} },
7540 { "mvrg_1", send_to_rg
, 0, {.id
= 0} },
7541 { "mvrg_2", send_to_rg
, 0, {.id
= 1} },
7542 { "mvrg_3", send_to_rg
, 0, {.id
= 2} },
7543 { "mvrg_4", send_to_rg
, 0, {.id
= 3} },
7544 { "mvrg_5", send_to_rg
, 0, {.id
= 4} },
7545 { "mvrg_6", send_to_rg
, 0, {.id
= 5} },
7546 { "mvrg_7", send_to_rg
, 0, {.id
= 6} },
7547 { "mvrg_8", send_to_rg
, 0, {.id
= 7} },
7548 { "mvrg_9", send_to_rg
, 0, {.id
= 8} },
7549 { "mvrg_next", send_to_rg_relative
, 0, {.id
= 1} },
7550 { "mvrg_prev", send_to_rg_relative
, 0, {.id
= -1} },
7551 { "mvws_1", send_to_ws
, 0, {.id
= 0} },
7552 { "mvws_2", send_to_ws
, 0, {.id
= 1} },
7553 { "mvws_3", send_to_ws
, 0, {.id
= 2} },
7554 { "mvws_4", send_to_ws
, 0, {.id
= 3} },
7555 { "mvws_5", send_to_ws
, 0, {.id
= 4} },
7556 { "mvws_6", send_to_ws
, 0, {.id
= 5} },
7557 { "mvws_7", send_to_ws
, 0, {.id
= 6} },
7558 { "mvws_8", send_to_ws
, 0, {.id
= 7} },
7559 { "mvws_9", send_to_ws
, 0, {.id
= 8} },
7560 { "mvws_10", send_to_ws
, 0, {.id
= 9} },
7561 { "mvws_11", send_to_ws
, 0, {.id
= 10} },
7562 { "mvws_12", send_to_ws
, 0, {.id
= 11} },
7563 { "mvws_13", send_to_ws
, 0, {.id
= 12} },
7564 { "mvws_14", send_to_ws
, 0, {.id
= 13} },
7565 { "mvws_15", send_to_ws
, 0, {.id
= 14} },
7566 { "mvws_16", send_to_ws
, 0, {.id
= 15} },
7567 { "mvws_17", send_to_ws
, 0, {.id
= 16} },
7568 { "mvws_18", send_to_ws
, 0, {.id
= 17} },
7569 { "mvws_19", send_to_ws
, 0, {.id
= 18} },
7570 { "mvws_20", send_to_ws
, 0, {.id
= 19} },
7571 { "mvws_21", send_to_ws
, 0, {.id
= 20} },
7572 { "mvws_22", send_to_ws
, 0, {.id
= 21} },
7573 { "name_workspace", name_workspace
, 0, {0} },
7574 { "quit", quit
, 0, {0} },
7575 { "raise", raise_focus
, 0, {0} },
7576 { "raise_toggle", raise_toggle
, 0, {0} },
7577 { "resize", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_DONTCENTER
} },
7578 { "resize_centered", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_CENTER
} },
7579 { "restart", restart
, 0, {0} },
7580 { "rg_1", focusrg
, 0, {.id
= 0} },
7581 { "rg_2", focusrg
, 0, {.id
= 1} },
7582 { "rg_3", focusrg
, 0, {.id
= 2} },
7583 { "rg_4", focusrg
, 0, {.id
= 3} },
7584 { "rg_5", focusrg
, 0, {.id
= 4} },
7585 { "rg_6", focusrg
, 0, {.id
= 5} },
7586 { "rg_7", focusrg
, 0, {.id
= 6} },
7587 { "rg_8", focusrg
, 0, {.id
= 7} },
7588 { "rg_9", focusrg
, 0, {.id
= 8} },
7589 { "rg_move_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_UP
} },
7590 { "rg_move_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_DOWN
} },
7591 { "rg_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7592 { "rg_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7593 { "screen_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7594 { "screen_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7595 { "search_win", search_win
, 0, {0} },
7596 { "search_workspace", search_workspace
, 0, {0} },
7597 { "spawn_custom", NULL
, 0, {0} },
7598 { "stack_balance", stack_config
, 0, {.id
= SWM_ARG_ID_STACKBALANCE
} },
7599 { "stack_inc", stack_config
, 0, {.id
= SWM_ARG_ID_STACKINC
} },
7600 { "stack_dec", stack_config
, 0, {.id
= SWM_ARG_ID_STACKDEC
} },
7601 { "stack_reset", stack_config
, 0, {.id
= SWM_ARG_ID_STACKINIT
} },
7602 { "swap_main", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPMAIN
} },
7603 { "swap_next", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPNEXT
} },
7604 { "swap_prev", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPPREV
} },
7605 { "uniconify", uniconify
, 0, {0} },
7606 { "version", version
, 0, {0} },
7607 { "width_grow", resize
, 0, {.id
= SWM_ARG_ID_WIDTHGROW
} },
7608 { "width_shrink", resize
, 0, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
7609 { "wind_del", wkill
, 0, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
7610 { "wind_kill", wkill
, 0, {.id
= SWM_ARG_ID_KILLWINDOW
} },
7611 { "ws_1", switchws
, 0, {.id
= 0} },
7612 { "ws_2", switchws
, 0, {.id
= 1} },
7613 { "ws_3", switchws
, 0, {.id
= 2} },
7614 { "ws_4", switchws
, 0, {.id
= 3} },
7615 { "ws_5", switchws
, 0, {.id
= 4} },
7616 { "ws_6", switchws
, 0, {.id
= 5} },
7617 { "ws_7", switchws
, 0, {.id
= 6} },
7618 { "ws_8", switchws
, 0, {.id
= 7} },
7619 { "ws_9", switchws
, 0, {.id
= 8} },
7620 { "ws_10", switchws
, 0, {.id
= 9} },
7621 { "ws_11", switchws
, 0, {.id
= 10} },
7622 { "ws_12", switchws
, 0, {.id
= 11} },
7623 { "ws_13", switchws
, 0, {.id
= 12} },
7624 { "ws_14", switchws
, 0, {.id
= 13} },
7625 { "ws_15", switchws
, 0, {.id
= 14} },
7626 { "ws_16", switchws
, 0, {.id
= 15} },
7627 { "ws_17", switchws
, 0, {.id
= 16} },
7628 { "ws_18", switchws
, 0, {.id
= 17} },
7629 { "ws_19", switchws
, 0, {.id
= 18} },
7630 { "ws_20", switchws
, 0, {.id
= 19} },
7631 { "ws_21", switchws
, 0, {.id
= 20} },
7632 { "ws_22", switchws
, 0, {.id
= 21} },
7633 { "ws_next", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
7634 { "ws_next_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
7635 { "ws_next_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
7636 { "ws_prev", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
7637 { "ws_prev_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
7638 { "ws_prev_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
7639 { "ws_prior", priorws
, 0, {0} },
7640 /* SWM_DEBUG actions MUST be here: */
7641 { "debug_toggle", debug_toggle
, 0, {0} },
7642 { "dumpwins", dumpwins
, 0, {0} },
7644 { "invalid action", NULL
, 0, {0} },
7648 update_modkey(uint16_t mod
)
7652 /* Replace all instances of the old mod key. */
7653 RB_FOREACH(bp
, binding_tree
, &bindings
)
7654 if (bp
->mod
& mod_key
)
7655 bp
->mod
= (bp
->mod
& ~mod_key
) | mod
;
7660 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7663 struct spawn_prog
*prog
= NULL
;
7665 char *ap
, **real_args
;
7667 /* suppress unused warning since var is needed */
7670 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
7673 TAILQ_FOREACH(prog
, &spawns
, entry
) {
7674 if (strcasecmp(spawn_name
, prog
->name
) == 0)
7678 warnx("spawn_custom: program %s not found", spawn_name
);
7682 /* make room for expanded args */
7683 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
7684 err(1, "spawn_custom: calloc real_args");
7686 /* expand spawn_args into real_args */
7687 for (i
= c
= 0; i
< prog
->argc
; i
++) {
7689 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
7690 if (strcasecmp(ap
, "$bar_border") == 0) {
7692 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
7694 err(1, "spawn_custom border color");
7695 } else if (strcasecmp(ap
, "$bar_color") == 0) {
7697 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
7699 err(1, "spawn_custom bar color");
7700 } else if (strcasecmp(ap
, "$bar_font") == 0) {
7701 if ((real_args
[c
] = strdup(bar_fonts
))
7703 err(1, "spawn_custom bar fonts");
7704 } else if (strcasecmp(ap
, "$bar_font_color") == 0) {
7706 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
7708 err(1, "spawn_custom color font");
7709 } else if (strcasecmp(ap
, "$color_focus") == 0) {
7711 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
7713 err(1, "spawn_custom color focus");
7714 } else if (strcasecmp(ap
, "$color_focus_maximized") == 0) {
7716 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].name
))
7718 err(1, "spawn_custom color focus maximized");
7719 } else if (strcasecmp(ap
, "$color_unfocus") == 0) {
7721 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
7723 err(1, "spawn_custom color unfocus");
7724 } else if (strcasecmp(ap
, "$color_unfocus_maximized") == 0) {
7726 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].name
))
7728 err(1, "spawn_custom color unfocus maximized");
7729 } else if (strcasecmp(ap
, "$region_index") == 0) {
7730 if (asprintf(&real_args
[c
], "%d",
7731 get_region_index(r
) + 1) < 1)
7732 err(1, "spawn_custom region index");
7733 } else if (strcasecmp(ap
, "$workspace_index") == 0) {
7734 if (asprintf(&real_args
[c
], "%d", r
->ws
->idx
+ 1) < 1)
7735 err(1, "spawn_custom workspace index");
7736 } else if (strcasecmp(ap
, "$dmenu_bottom") == 0) {
7739 if ((real_args
[c
] = strdup("-b")) == NULL
)
7740 err(1, "spawn_custom workspace index");
7742 /* no match --> copy as is */
7743 if ((real_args
[c
] = strdup(ap
)) == NULL
)
7744 err(1, "spawn_custom strdup(ap)");
7746 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
7752 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
7753 for (i
= 0; i
< c
; ++i
)
7754 DPRINTF("\"%s\" ", real_args
[i
]);
7757 *ret_args
= real_args
;
7762 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
7768 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7772 spawn(r
->ws
->idx
, &a
, true);
7774 for (i
= 0; i
< spawn_argc
; i
++)
7780 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7787 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7791 if (pipe(select_list_pipe
) == -1)
7792 err(1, "pipe error");
7793 if (pipe(select_resp_pipe
) == -1)
7794 err(1, "pipe error");
7796 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
7797 err(1, "could not disable SIGPIPE");
7798 switch (*pid
= fork()) {
7800 err(1, "cannot fork");
7803 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
7805 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
7807 close(select_list_pipe
[1]);
7808 close(select_resp_pipe
[0]);
7809 spawn(r
->ws
->idx
, &a
, false);
7811 default: /* parent */
7812 close(select_list_pipe
[0]);
7813 close(select_resp_pipe
[1]);
7817 for (i
= 0; i
< spawn_argc
; i
++)
7822 /* Argument tokenizer. */
7825 char *arg
, *cp
, *next
;
7826 bool single_quoted
= false, double_quoted
= false;
7831 /* Eat and move characters until end of argument is found. */
7832 for (arg
= next
= cp
= *sp
; *cp
!= '\0'; ++cp
) {
7833 if (!double_quoted
&& *cp
== '\'') {
7834 /* Eat single-quote. */
7835 single_quoted
= !single_quoted
;
7836 } else if (!single_quoted
&& *cp
== '"') {
7837 /* Eat double-quote. */
7838 double_quoted
= !double_quoted
;
7839 } else if (!single_quoted
&& *cp
== '\\' && *(cp
+ 1) == '"') {
7840 /* Eat backslash; copy escaped character to arg. */
7842 } else if (!single_quoted
&& !double_quoted
&& *cp
== '\\' &&
7843 (*(cp
+ 1) == '\'' || *(cp
+ 1) == ' ')) {
7844 /* Eat backslash; move escaped character. */
7846 } else if (!single_quoted
&& !double_quoted
&&
7847 (*cp
== ' ' || *cp
== '\t')) {
7848 /* Terminate argument. */
7850 /* Point sp to beginning of next argument. */
7854 /* Move regular character. */
7859 /* Terminate argument if end of string. */
7869 spawn_insert(const char *name
, const char *args
, int flags
)
7871 struct spawn_prog
*sp
;
7872 char *arg
, *cp
, *ptr
;
7874 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s[%s]\n", name
, args
);
7876 if (args
== NULL
|| *args
== '\0')
7879 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
7880 err(1, "spawn_insert: calloc");
7881 if ((sp
->name
= strdup(name
)) == NULL
)
7882 err(1, "spawn_insert: strdup");
7884 /* Convert the arguments to an argument list. */
7885 if ((ptr
= cp
= strdup(args
)) == NULL
)
7886 err(1, "spawn_insert: strdup");
7887 while ((arg
= argsep(&ptr
)) != NULL
) {
7888 /* Null argument; skip it. */
7893 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
7894 sizeof *sp
->argv
)) == NULL
)
7895 err(1, "spawn_insert: realloc");
7896 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
7897 err(1, "spawn_insert: strdup");
7903 DNPRINTF(SWM_D_SPAWN
, "arg %d: [%s]\n", sp
->argc
, sp
->argv
[sp
->argc
-1]);
7904 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
7905 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
7909 spawn_remove(struct spawn_prog
*sp
)
7913 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
7915 TAILQ_REMOVE(&spawns
, sp
, entry
);
7916 for (i
= 0; i
< sp
->argc
; i
++)
7922 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
7928 struct spawn_prog
*sp
;
7930 while ((sp
= TAILQ_FIRST(&spawns
)) != NULL
) {
7936 spawn_find(const char *name
)
7938 struct spawn_prog
*sp
;
7940 TAILQ_FOREACH(sp
, &spawns
, entry
)
7941 if (strcasecmp(sp
->name
, name
) == 0)
7948 setspawn(const char *name
, const char *args
, int flags
)
7950 struct spawn_prog
*sp
;
7952 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
7957 /* Remove any old spawn under the same name. */
7958 if ((sp
= spawn_find(name
)) != NULL
)
7962 spawn_insert(name
, args
, flags
);
7964 warnx("error: setspawn: cannot find program: %s", name
);
7966 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
7970 setconfspawn(const char *selector
, const char *value
, int flags
)
7974 if (selector
== NULL
|| strlen(selector
) == 0)
7977 args
= expand_tilde(value
);
7979 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
7981 setspawn(selector
, args
, flags
);
7984 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
7989 validate_spawns(void)
7992 struct spawn_prog
*sp
;
7993 char which
[PATH_MAX
];
7996 RB_FOREACH(bp
, binding_tree
, &bindings
) {
7997 if (bp
->action
!= FN_SPAWN_CUSTOM
)
8001 sp
= spawn_find(bp
->spawn_name
);
8002 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
8005 /* verify we have the goods */
8006 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
8007 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
8009 for (i
= strlen("which "); i
< strlen(which
); i
++)
8010 if (which
[i
] == ' ') {
8014 if (system(which
) != 0)
8015 add_startup_exception("could not find %s",
8016 &which
[strlen("which ")]);
8023 setconfspawn("lock", "xlock", 0);
8025 setconfspawn("term", "xterm", 0);
8026 setconfspawn("spawn_term", "xterm", 0);
8028 setconfspawn("menu", "dmenu_run"
8032 " -nf $bar_font_color"
8034 " -sf $bar_color", 0);
8036 setconfspawn("search", "dmenu"
8041 " -nf $bar_font_color"
8043 " -sf $bar_color", 0);
8045 setconfspawn("name_workspace", "dmenu"
8050 " -nf $bar_font_color"
8052 " -sf $bar_color", 0);
8054 /* These are not verified for existence, even with a binding set. */
8055 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
8056 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
8057 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
8061 #define SWM_MODNAME_SIZE 32
8062 #define SWM_KEY_WS "\n+ \t"
8064 parsebinding(const char *bindstr
, uint16_t *mod
, enum binding_type
*type
,
8065 uint32_t *val
, uint32_t *flags
)
8067 char *str
, *cp
, *name
;
8068 KeySym ks
, lks
, uks
;
8070 DNPRINTF(SWM_D_KEY
, "parsebinding: enter [%s]\n", bindstr
);
8071 if (mod
== NULL
|| val
== NULL
) {
8072 DNPRINTF(SWM_D_KEY
, "parsebinding: no mod or key vars\n");
8075 if (bindstr
== NULL
|| strlen(bindstr
) == 0) {
8076 DNPRINTF(SWM_D_KEY
, "parsebinding: no bindstr\n");
8080 if ((cp
= str
= strdup(bindstr
)) == NULL
)
8081 err(1, "parsebinding: strdup");
8083 *val
= XCB_NO_SYMBOL
;
8087 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
8088 DNPRINTF(SWM_D_KEY
, "parsebinding: entry [%s]\n", name
);
8090 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
8091 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
8093 else if (strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
) == 0)
8094 *mod
|= XCB_MOD_MASK_1
;
8095 else if (strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
) == 0)
8096 *mod
|= XCB_MOD_MASK_2
;
8097 else if (strncmp(name
, "Mod3", SWM_MODNAME_SIZE
) == 0)
8098 *mod
|= XCB_MOD_MASK_3
;
8099 else if (strncmp(name
, "Mod4", SWM_MODNAME_SIZE
) == 0)
8100 *mod
|= XCB_MOD_MASK_4
;
8101 else if (strncmp(name
, "Mod5", SWM_MODNAME_SIZE
) == 0)
8102 *mod
|= XCB_MOD_MASK_5
;
8103 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
8104 *mod
|= XCB_MOD_MASK_SHIFT
;
8105 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
8106 *mod
|= XCB_MOD_MASK_CONTROL
;
8107 else if (strncasecmp(name
, "ANYMOD", SWM_MODNAME_SIZE
) == 0)
8108 *mod
|= XCB_MOD_MASK_ANY
;
8109 else if (strncasecmp(name
, "REPLAY", SWM_MODNAME_SIZE
) == 0)
8110 *flags
|= BINDING_F_REPLAY
;
8111 else if (sscanf(name
, "Button%u", val
) == 1) {
8112 DNPRINTF(SWM_D_KEY
, "parsebinding: button %u\n", *val
);
8114 if (*val
> 255 || *val
== 0) {
8116 "parsebinding: invalid button %u\n", *val
);
8120 /* TODO: do this without Xlib. */
8121 ks
= XStringToKeysym(name
);
8122 if (ks
== NoSymbol
) {
8124 "parsebinding: invalid key %s\n", name
);
8129 XConvertCase(ks
, &lks
, &uks
);
8130 *val
= (uint32_t)lks
;
8134 /* If ANYMOD was specified, ignore the rest. */
8135 if (*mod
& XCB_MOD_MASK_ANY
)
8136 *mod
= XCB_MOD_MASK_ANY
;
8139 DNPRINTF(SWM_D_KEY
, "parsebinding: leave ok\n");
8144 strdupsafe(const char *str
)
8149 return (strdup(str
));
8153 binding_cmp(struct binding
*bp1
, struct binding
*bp2
)
8155 if (bp1
->type
< bp2
->type
)
8157 if (bp1
->type
> bp2
->type
)
8160 if (bp1
->value
< bp2
->value
)
8162 if (bp1
->value
> bp2
->value
)
8165 if (bp1
->mod
< bp2
->mod
)
8167 if (bp1
->mod
> bp2
->mod
)
8174 binding_insert(uint16_t mod
, enum binding_type type
, uint32_t val
,
8175 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
8179 DNPRINTF(SWM_D_KEY
, "binding_insert: mod: %u, type: %d, val: %u, "
8180 "action: %s(%d), spawn_name: %s\n", mod
, type
, val
,
8181 actions
[aid
].name
, aid
, spawn_name
);
8183 if ((bp
= malloc(sizeof *bp
)) == NULL
)
8184 err(1, "binding_insert: malloc");
8191 bp
->spawn_name
= strdupsafe(spawn_name
);
8192 RB_INSERT(binding_tree
, &bindings
, bp
);
8194 DNPRINTF(SWM_D_KEY
, "binding_insert: leave\n");
8198 binding_lookup(uint16_t mod
, enum binding_type type
, uint32_t val
)
8206 return (RB_FIND(binding_tree
, &bindings
, &bp
));
8210 binding_remove(struct binding
*bp
)
8212 DNPRINTF(SWM_D_KEY
, "binding_remove: mod: %u, type: %d, val: %u, "
8213 "action: %s(%d), spawn_name: %s\n", bp
->mod
, bp
->type
, bp
->value
,
8214 actions
[bp
->action
].name
, bp
->action
, bp
->spawn_name
);
8216 RB_REMOVE(binding_tree
, &bindings
, bp
);
8217 free(bp
->spawn_name
);
8220 DNPRINTF(SWM_D_KEY
, "binding_remove: leave\n");
8224 setbinding(uint16_t mod
, enum binding_type type
, uint32_t val
,
8225 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
8229 DNPRINTF(SWM_D_KEY
, "setbinding: enter %s [%s]\n",
8230 actions
[aid
].name
, spawn_name
);
8232 /* Unbind any existing. Loop is to handle MOD_MASK_ANY. */
8233 while ((bp
= binding_lookup(mod
, type
, val
)))
8236 if (aid
!= FN_INVALID
)
8237 binding_insert(mod
, type
, val
, aid
, flags
, spawn_name
);
8239 DNPRINTF(SWM_D_KEY
, "setbinding: leave\n");
8243 setconfbinding(const char *selector
, const char *value
, int flags
)
8245 struct spawn_prog
*sp
;
8246 uint32_t keybtn
, opts
;
8249 enum binding_type type
;
8251 /* suppress unused warning since var is needed */
8254 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter selector: [%s], "
8255 "value: [%s]\n", selector
, value
);
8256 if (selector
== NULL
|| strlen(selector
) == 0) {
8257 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
8258 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8259 setbinding(mod
, type
, keybtn
, FN_INVALID
, opts
, NULL
);
8264 /* search by key function name */
8265 for (aid
= 0; aid
< FN_INVALID
; aid
++) {
8266 if (strncasecmp(selector
, actions
[aid
].name
,
8267 SWM_FUNCNAME_LEN
) == 0) {
8268 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8269 "action\n", selector
);
8270 if (parsebinding(value
, &mod
, &type
, &keybtn
,
8272 setbinding(mod
, type
, keybtn
, aid
, opts
, NULL
);
8278 /* search by custom spawn name */
8279 if ((sp
= spawn_find(selector
)) != NULL
) {
8280 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8281 "spawn\n", selector
);
8282 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8283 setbinding(mod
, type
, keybtn
, FN_SPAWN_CUSTOM
, opts
,
8289 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
8293 #define MODSHIFT MODKEY | XCB_MOD_MASK_SHIFT
8295 setup_keybindings(void)
8297 #define BINDKEY(m, k, a) setbinding(m, KEYBIND, k, a, 0, NULL)
8298 #define BINDKEYSPAWN(m, k, s) setbinding(m, KEYBIND, k, FN_SPAWN_CUSTOM, 0, s)
8299 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8300 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8301 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8302 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8303 BINDKEY(MODKEY
, XK_v
, FN_BUTTON2
);
8304 BINDKEY(MODKEY
, XK_space
, FN_CYCLE_LAYOUT
);
8305 BINDKEY(MODSHIFT
, XK_backslash
, FN_FLIP_LAYOUT
);
8306 BINDKEY(MODKEY
, XK_t
, FN_FLOAT_TOGGLE
);
8307 BINDKEY(MODKEY
, XK_m
, FN_FOCUS_MAIN
);
8308 BINDKEY(MODKEY
, XK_j
, FN_FOCUS_NEXT
);
8309 BINDKEY(MODKEY
, XK_Tab
, FN_FOCUS_NEXT
);
8310 BINDKEY(MODKEY
, XK_k
, FN_FOCUS_PREV
);
8311 BINDKEY(MODSHIFT
, XK_Tab
, FN_FOCUS_PREV
);
8312 BINDKEY(MODKEY
, XK_u
, FN_FOCUS_URGENT
);
8313 BINDKEY(MODSHIFT
, XK_e
, FN_FULLSCREEN_TOGGLE
);
8314 BINDKEY(MODKEY
, XK_e
, FN_MAXIMIZE_TOGGLE
);
8315 BINDKEY(MODSHIFT
, XK_equal
, FN_HEIGHT_GROW
);
8316 BINDKEY(MODSHIFT
, XK_minus
, FN_HEIGHT_SHRINK
);
8317 BINDKEY(MODKEY
, XK_w
, FN_ICONIFY
);
8318 BINDKEY(MODKEY
, XK_h
, FN_MASTER_SHRINK
);
8319 BINDKEY(MODKEY
, XK_l
, FN_MASTER_GROW
);
8320 BINDKEY(MODKEY
, XK_comma
, FN_MASTER_ADD
);
8321 BINDKEY(MODKEY
, XK_period
, FN_MASTER_DEL
);
8322 BINDKEY(MODSHIFT
, XK_bracketright
, FN_MOVE_DOWN
);
8323 BINDKEY(MODKEY
, XK_bracketleft
, FN_MOVE_LEFT
);
8324 BINDKEY(MODKEY
, XK_bracketright
, FN_MOVE_RIGHT
);
8325 BINDKEY(MODSHIFT
, XK_bracketleft
, FN_MOVE_UP
);
8326 BINDKEY(MODSHIFT
, XK_KP_End
, FN_MVRG_1
);
8327 BINDKEY(MODSHIFT
, XK_KP_Down
, FN_MVRG_2
);
8328 BINDKEY(MODSHIFT
, XK_KP_Next
, FN_MVRG_3
);
8329 BINDKEY(MODSHIFT
, XK_KP_Left
, FN_MVRG_4
);
8330 BINDKEY(MODSHIFT
, XK_KP_Begin
, FN_MVRG_5
);
8331 BINDKEY(MODSHIFT
, XK_KP_Right
, FN_MVRG_6
);
8332 BINDKEY(MODSHIFT
, XK_KP_Home
, FN_MVRG_7
);
8333 BINDKEY(MODSHIFT
, XK_KP_Up
, FN_MVRG_8
);
8334 BINDKEY(MODSHIFT
, XK_KP_Prior
, FN_MVRG_9
);
8335 BINDKEY(MODSHIFT
, XK_1
, FN_MVWS_1
);
8336 BINDKEY(MODSHIFT
, XK_2
, FN_MVWS_2
);
8337 BINDKEY(MODSHIFT
, XK_3
, FN_MVWS_3
);
8338 BINDKEY(MODSHIFT
, XK_4
, FN_MVWS_4
);
8339 BINDKEY(MODSHIFT
, XK_5
, FN_MVWS_5
);
8340 BINDKEY(MODSHIFT
, XK_6
, FN_MVWS_6
);
8341 BINDKEY(MODSHIFT
, XK_7
, FN_MVWS_7
);
8342 BINDKEY(MODSHIFT
, XK_8
, FN_MVWS_8
);
8343 BINDKEY(MODSHIFT
, XK_9
, FN_MVWS_9
);
8344 BINDKEY(MODSHIFT
, XK_0
, FN_MVWS_10
);
8345 BINDKEY(MODSHIFT
, XK_F1
, FN_MVWS_11
);
8346 BINDKEY(MODSHIFT
, XK_F2
, FN_MVWS_12
);
8347 BINDKEY(MODSHIFT
, XK_F3
, FN_MVWS_13
);
8348 BINDKEY(MODSHIFT
, XK_F4
, FN_MVWS_14
);
8349 BINDKEY(MODSHIFT
, XK_F5
, FN_MVWS_15
);
8350 BINDKEY(MODSHIFT
, XK_F6
, FN_MVWS_16
);
8351 BINDKEY(MODSHIFT
, XK_F7
, FN_MVWS_17
);
8352 BINDKEY(MODSHIFT
, XK_F8
, FN_MVWS_18
);
8353 BINDKEY(MODSHIFT
, XK_F9
, FN_MVWS_19
);
8354 BINDKEY(MODSHIFT
, XK_F10
, FN_MVWS_20
);
8355 BINDKEY(MODSHIFT
, XK_F11
, FN_MVWS_21
);
8356 BINDKEY(MODSHIFT
, XK_F12
, FN_MVWS_22
);
8357 BINDKEY(MODSHIFT
, XK_slash
, FN_NAME_WORKSPACE
);
8358 BINDKEY(MODSHIFT
, XK_q
, FN_QUIT
);
8359 BINDKEY(MODKEY
, XK_r
, FN_RAISE
);
8360 BINDKEY(MODSHIFT
, XK_r
, FN_RAISE_TOGGLE
);
8361 BINDKEY(MODKEY
, XK_q
, FN_RESTART
);
8362 BINDKEY(MODKEY
, XK_KP_End
, FN_RG_1
);
8363 BINDKEY(MODKEY
, XK_KP_Down
, FN_RG_2
);
8364 BINDKEY(MODKEY
, XK_KP_Next
, FN_RG_3
);
8365 BINDKEY(MODKEY
, XK_KP_Left
, FN_RG_4
);
8366 BINDKEY(MODKEY
, XK_KP_Begin
, FN_RG_5
);
8367 BINDKEY(MODKEY
, XK_KP_Right
, FN_RG_6
);
8368 BINDKEY(MODKEY
, XK_KP_Home
, FN_RG_7
);
8369 BINDKEY(MODKEY
, XK_KP_Up
, FN_RG_8
);
8370 BINDKEY(MODKEY
, XK_KP_Prior
, FN_RG_9
);
8371 BINDKEY(MODSHIFT
, XK_Right
, FN_RG_NEXT
);
8372 BINDKEY(MODSHIFT
, XK_Left
, FN_RG_PREV
);
8373 BINDKEY(MODKEY
, XK_f
, FN_SEARCH_WIN
);
8374 BINDKEY(MODKEY
, XK_slash
, FN_SEARCH_WORKSPACE
);
8375 BINDKEYSPAWN(MODSHIFT
, XK_i
, "initscr");
8376 BINDKEYSPAWN(MODSHIFT
, XK_Delete
, "lock");
8377 BINDKEYSPAWN(MODKEY
, XK_p
, "menu");
8378 BINDKEYSPAWN(MODKEY
, XK_s
, "screenshot_all");
8379 BINDKEYSPAWN(MODSHIFT
, XK_s
, "screenshot_wind");
8380 BINDKEYSPAWN(MODSHIFT
, XK_Return
, "term");
8381 BINDKEY(MODSHIFT
, XK_comma
, FN_STACK_INC
);
8382 BINDKEY(MODSHIFT
, XK_period
, FN_STACK_DEC
);
8383 BINDKEY(MODSHIFT
, XK_space
, FN_STACK_INIT
);
8384 BINDKEY(MODKEY
, XK_Return
, FN_SWAP_MAIN
);
8385 BINDKEY(MODSHIFT
, XK_j
, FN_SWAP_NEXT
);
8386 BINDKEY(MODSHIFT
, XK_k
, FN_SWAP_PREV
);
8387 BINDKEY(MODSHIFT
, XK_w
, FN_UNICONIFY
);
8388 BINDKEY(MODSHIFT
, XK_v
, FN_VERSION
);
8389 BINDKEY(MODKEY
, XK_equal
, FN_WIDTH_GROW
);
8390 BINDKEY(MODKEY
, XK_minus
, FN_WIDTH_SHRINK
);
8391 BINDKEY(MODKEY
, XK_x
, FN_WIND_DEL
);
8392 BINDKEY(MODSHIFT
, XK_x
, FN_WIND_KILL
);
8393 BINDKEY(MODKEY
, XK_1
, FN_WS_1
);
8394 BINDKEY(MODKEY
, XK_2
, FN_WS_2
);
8395 BINDKEY(MODKEY
, XK_3
, FN_WS_3
);
8396 BINDKEY(MODKEY
, XK_4
, FN_WS_4
);
8397 BINDKEY(MODKEY
, XK_5
, FN_WS_5
);
8398 BINDKEY(MODKEY
, XK_6
, FN_WS_6
);
8399 BINDKEY(MODKEY
, XK_7
, FN_WS_7
);
8400 BINDKEY(MODKEY
, XK_8
, FN_WS_8
);
8401 BINDKEY(MODKEY
, XK_9
, FN_WS_9
);
8402 BINDKEY(MODKEY
, XK_0
, FN_WS_10
);
8403 BINDKEY(MODKEY
, XK_F1
, FN_WS_11
);
8404 BINDKEY(MODKEY
, XK_F2
, FN_WS_12
);
8405 BINDKEY(MODKEY
, XK_F3
, FN_WS_13
);
8406 BINDKEY(MODKEY
, XK_F4
, FN_WS_14
);
8407 BINDKEY(MODKEY
, XK_F5
, FN_WS_15
);
8408 BINDKEY(MODKEY
, XK_F6
, FN_WS_16
);
8409 BINDKEY(MODKEY
, XK_F7
, FN_WS_17
);
8410 BINDKEY(MODKEY
, XK_F8
, FN_WS_18
);
8411 BINDKEY(MODKEY
, XK_F9
, FN_WS_19
);
8412 BINDKEY(MODKEY
, XK_F10
, FN_WS_20
);
8413 BINDKEY(MODKEY
, XK_F11
, FN_WS_21
);
8414 BINDKEY(MODKEY
, XK_F12
, FN_WS_22
);
8415 BINDKEY(MODKEY
, XK_Right
, FN_WS_NEXT
);
8416 BINDKEY(MODKEY
, XK_Left
, FN_WS_PREV
);
8417 BINDKEY(MODKEY
, XK_Up
, FN_WS_NEXT_ALL
);
8418 BINDKEY(MODKEY
, XK_Down
, FN_WS_PREV_ALL
);
8419 BINDKEY(MODSHIFT
, XK_Up
, FN_WS_NEXT_MOVE
);
8420 BINDKEY(MODSHIFT
, XK_Down
, FN_WS_PREV_MOVE
);
8421 BINDKEY(MODKEY
, XK_a
, FN_WS_PRIOR
);
8423 BINDKEY(MODKEY
, XK_d
, FN_DEBUG_TOGGLE
);
8424 BINDKEY(MODSHIFT
, XK_d
, FN_DUMPWINS
);
8431 setup_btnbindings(void)
8433 setbinding(ANYMOD
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_FOCUS
,
8434 BINDING_F_REPLAY
, NULL
);
8435 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE
, 0, NULL
);
8436 setbinding(MODSHIFT
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE_CENTERED
, 0,
8438 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_MOVE
, 0, NULL
);
8443 clear_bindings(void)
8447 while ((bp
= RB_ROOT(&bindings
)))
8452 clear_keybindings(void)
8454 struct binding
*bp
, *bptmp
;
8456 RB_FOREACH_SAFE(bp
, binding_tree
, &bindings
, bptmp
) {
8457 if (bp
->type
!= KEYBIND
)
8464 setkeymapping(const char *selector
, const char *value
, int flags
)
8466 char *keymapping_file
;
8468 /* suppress unused warnings since vars are needed */
8472 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
8474 keymapping_file
= expand_tilde(value
);
8476 clear_keybindings();
8477 /* load new key bindings; if it fails, revert to default bindings */
8478 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
8479 clear_keybindings();
8480 setup_keybindings();
8483 free(keymapping_file
);
8485 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
8490 updatenumlockmask(void)
8493 xcb_get_modifier_mapping_reply_t
*modmap_r
;
8494 xcb_keycode_t
*modmap
, kc
, *keycode
;
8498 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
8499 xcb_get_modifier_mapping(conn
),
8502 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
8503 for (i
= 0; i
< 8; i
++) {
8504 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
8505 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
8507 keycode
= xcb_key_symbols_get_keycode(syms
,
8511 numlockmask
= (1 << i
);
8518 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
8525 int num_screens
, k
, j
;
8526 uint16_t modifiers
[4];
8527 xcb_keycode_t
*code
;
8529 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
8530 updatenumlockmask();
8533 modifiers
[1] = numlockmask
;
8534 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8535 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8537 num_screens
= get_screen_count();
8538 for (k
= 0; k
< num_screens
; k
++) {
8539 if (TAILQ_EMPTY(&screens
[k
].rl
))
8541 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
8543 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8544 if (bp
->type
!= KEYBIND
)
8547 /* If there is a catch-all, only bind that. */
8548 if ((binding_lookup(ANYMOD
, KEYBIND
, bp
->value
)) &&
8552 /* Skip unused ws binds. */
8553 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8554 bp
->action
<= FN_WS_22
)
8557 /* Skip unused mvws binds. */
8558 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8559 bp
->action
<= FN_MVWS_22
)
8562 if ((code
= xcb_key_symbols_get_keycode(syms
,
8563 bp
->value
)) == NULL
)
8566 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8567 /* All modifiers are grabbed in one pass. */
8568 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8569 "key: %u, modifiers: %d\n", bp
->value
,
8571 xcb_grab_key(conn
, 1, screens
[k
].root
,
8572 bp
->mod
, *code
, XCB_GRAB_MODE_ASYNC
,
8573 XCB_GRAB_MODE_SYNC
);
8575 /* Need to grab each modifier permutation. */
8576 for (j
= 0; j
< LENGTH(modifiers
); j
++) {
8577 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8578 "key: %u, modifiers: %d\n",
8579 bp
->value
, bp
->mod
| modifiers
[j
]);
8580 xcb_grab_key(conn
, 1,
8582 bp
->mod
| modifiers
[j
],
8583 *code
, XCB_GRAB_MODE_ASYNC
,
8584 XCB_GRAB_MODE_SYNC
);
8596 int num_screens
, i
, k
;
8597 uint16_t modifiers
[4];
8599 DNPRINTF(SWM_D_MOUSE
, "grabbuttons\n");
8600 updatenumlockmask();
8603 modifiers
[1] = numlockmask
;
8604 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8605 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8607 num_screens
= get_screen_count();
8608 for (k
= 0; k
< num_screens
; k
++) {
8609 if (TAILQ_EMPTY(&screens
[k
].rl
))
8611 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, screens
[k
].root
,
8613 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8614 if (bp
->type
!= BTNBIND
)
8617 /* If there is a catch-all, only bind that. */
8618 if ((binding_lookup(ANYMOD
, BTNBIND
, bp
->value
)) &&
8622 /* Skip unused ws binds. */
8623 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8624 bp
->action
<= FN_WS_22
)
8627 /* Skip unused mvws binds. */
8628 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8629 bp
->action
<= FN_MVWS_22
)
8632 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8633 /* All modifiers are grabbed in one pass. */
8634 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: grab, "
8635 "button: %u, modifiers: %d\n", bp
->value
,
8637 xcb_grab_button(conn
, 0, screens
[k
].root
,
8638 BUTTONMASK
, XCB_GRAB_MODE_SYNC
,
8639 XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
8640 XCB_CURSOR_NONE
, bp
->value
, bp
->mod
);
8642 /* Need to grab each modifier permutation. */
8643 for (i
= 0; i
< LENGTH(modifiers
); ++i
) {
8644 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: "
8645 "grab, button: %u, modifiers: %u\n",
8646 bp
->value
, bp
->mod
| modifiers
[i
]);
8647 xcb_grab_button(conn
, 0,
8648 screens
[k
].root
, BUTTONMASK
,
8650 XCB_GRAB_MODE_ASYNC
,
8652 XCB_CURSOR_NONE
, bp
->value
,
8653 bp
->mod
| modifiers
[i
]);
8660 const char *quirkname
[] = {
8661 "NONE", /* config string for "no value" */
8669 "FOCUSONMAP_SINGLE",
8677 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
8678 #define SWM_Q_DELIM "\n|+ \t"
8680 parsequirks(const char *qstr
, uint32_t *quirk
, int *ws
)
8682 char *str
, *cp
, *name
;
8685 if (quirk
== NULL
|| qstr
== NULL
)
8688 if ((cp
= str
= strdup(qstr
)) == NULL
)
8689 err(1, "parsequirks: strdup");
8692 while ((name
= strsep(&cp
, SWM_Q_DELIM
)) != NULL
) {
8694 cp
+= (long)strspn(cp
, SWM_Q_DELIM
);
8696 if (sscanf(name
, "WS[%d]", ws
) == 1) {
8702 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
8703 if (strncasecmp(name
, quirkname
[i
],
8704 SWM_QUIRK_LEN
) == 0) {
8705 DNPRINTF(SWM_D_QUIRK
,
8706 "parsequirks: %s\n", name
);
8712 *quirk
|= 1 << (i
-1);
8716 if (i
>= LENGTH(quirkname
)) {
8717 DNPRINTF(SWM_D_QUIRK
,
8718 "parsequirks: invalid quirk [%s]\n", name
);
8729 quirk_insert(const char *class, const char *instance
, const char *name
,
8730 uint32_t quirk
, int ws
)
8734 bool failed
= false;
8736 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: class: %s, instance: %s, name: %s,"
8737 " value: %u, ws: %d\n", class, instance
, name
, quirk
, ws
);
8739 if ((qp
= malloc(sizeof *qp
)) == NULL
)
8740 err(1, "quirk_insert: malloc");
8742 if ((qp
->class = strdup(class)) == NULL
)
8743 err(1, "quirk_insert: strdup");
8744 if ((qp
->instance
= strdup(instance
)) == NULL
)
8745 err(1, "quirk_insert: strdup");
8746 if ((qp
->name
= strdup(name
)) == NULL
)
8747 err(1, "quirk_insert: strdup");
8749 if (asprintf(&str
, "^%s$", class) == -1)
8750 err(1, "quirk_insert: asprintf");
8751 if (regcomp(&qp
->regex_class
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8752 add_startup_exception("regex failed to compile quirk 'class' "
8753 "field: %s", class);
8756 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8759 if (asprintf(&str
, "^%s$", instance
) == -1)
8760 err(1, "quirk_insert: asprintf");
8761 if (regcomp(&qp
->regex_instance
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8762 add_startup_exception("regex failed to compile quirk 'instance'"
8763 " field: %s", instance
);
8766 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8769 if (asprintf(&str
, "^%s$", name
) == -1)
8770 err(1, "quirk_insert: asprintf");
8771 if (regcomp(&qp
->regex_name
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8772 add_startup_exception("regex failed to compile quirk 'name' "
8776 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8780 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: regex error; skipping.\n");
8785 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
8787 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
8791 quirk_remove(struct quirk
*qp
)
8793 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%u]\n", qp
->class,
8794 qp
->name
, qp
->quirk
);
8796 TAILQ_REMOVE(&quirks
, qp
, entry
);
8799 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
8803 quirk_free(struct quirk
*qp
)
8805 regfree(&qp
->regex_class
);
8806 regfree(&qp
->regex_instance
);
8807 regfree(&qp
->regex_name
);
8819 while ((qp
= TAILQ_FIRST(&quirks
)) != NULL
) {
8825 quirk_replace(struct quirk
*qp
, const char *class, const char *instance
,
8826 const char *name
, uint32_t quirk
, int ws
)
8828 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp
->class,
8829 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
8832 quirk_insert(class, instance
, name
, quirk
, ws
);
8834 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
8838 setquirk(const char *class, const char *instance
, const char *name
,
8839 uint32_t quirk
, int ws
)
8843 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
8844 instance
, name
, quirk
, ws
);
8846 /* Remove/replace existing quirk. */
8847 TAILQ_FOREACH(qp
, &quirks
, entry
) {
8848 if (strcmp(qp
->class, class) == 0 &&
8849 strcmp(qp
->instance
, instance
) == 0 &&
8850 strcmp(qp
->name
, name
) == 0) {
8851 if (quirk
== 0 && ws
== -1)
8854 quirk_replace(qp
, class, instance
, name
, quirk
,
8856 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8861 /* Only insert if quirk is not NONE or forced ws is set. */
8862 if (quirk
|| ws
!= -1)
8863 quirk_insert(class, instance
, name
, quirk
, ws
);
8865 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8868 /* Eat '\' in str used to escape square brackets and colon. */
8870 unescape_selector(char *str
)
8874 for (cp
= str
; *str
!= '\0'; ++str
, ++cp
) {
8875 if (*str
== '\\' && (*(str
+ 1) == ':' || *(str
+ 1) == ']' ||
8885 setconfquirk(const char *selector
, const char *value
, int flags
)
8887 char *str
, *cp
, *class;
8888 char *instance
= NULL
, *name
= NULL
;
8889 int retval
, count
= 0, ws
= -1;
8892 /* suppress unused warning since var is needed */
8895 if (selector
== NULL
|| strlen(selector
) == 0)
8898 if ((str
= strdup(selector
)) == NULL
)
8899 err(1, "setconfquirk: strdup");
8901 /* Find non-escaped colon. */
8908 for (++cp
; *cp
!= '\0'; ++cp
) {
8909 if (*cp
== ':' && *(cp
- 1) != '\\') {
8915 unescape_selector(class);
8917 instance
= class + strlen(class) + 1;
8918 unescape_selector(instance
);
8924 name
= instance
+ strlen(instance
) + 1;
8925 unescape_selector(name
);
8930 DNPRINTF(SWM_D_CONF
, "setconfquirk: class: %s, instance: %s, "
8931 "name: %s\n", class, instance
, name
);
8933 if ((retval
= parsequirks(value
, &qrks
, &ws
)) == 0)
8934 setquirk(class, instance
, name
, qrks
, ws
);
8943 setquirk("MPlayer", "xv", ".*",
8944 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
, -1);
8945 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
8947 setquirk("Firefox-bin", "firefox-bin", ".*",
8949 setquirk("Firefox", "Dialog", ".*",
8951 setquirk("Gimp", "gimp", ".*",
8952 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8953 setquirk("XTerm", "xterm", ".*",
8954 SWM_Q_XTERM_FONTADJ
, -1);
8955 setquirk("xine", "Xine Window", ".*",
8956 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8957 setquirk("Xitk", "Xitk Combo", ".*",
8958 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8959 setquirk("xine", "xine Panel", ".*",
8960 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8961 setquirk("Xitk", "Xine Window", ".*",
8962 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8963 setquirk("xine", "xine Video Fullscreen Window", ".*",
8964 SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
, -1);
8965 setquirk("pcb", "pcb", ".*",
8967 setquirk("SDL_App", "SDL_App", ".*",
8968 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
, -1);
8971 /* conf file stuff */
8972 #define SWM_CONF_FILE "spectrwm.conf"
8973 #define SWM_CONF_FILE_OLD "scrotwm.conf"
8977 SWM_S_BAR_AT_BOTTOM
,
8978 SWM_S_BAR_BORDER_WIDTH
,
8981 SWM_S_BAR_ENABLED_WS
,
8986 SWM_S_BOUNDARY_WIDTH
,
8987 SWM_S_CLOCK_ENABLED
,
8990 SWM_S_CYCLE_VISIBLE
,
8992 SWM_S_DISABLE_BORDER
,
8994 SWM_S_FOCUS_CLOSE_WRAP
,
8995 SWM_S_FOCUS_DEFAULT
,
8997 SWM_S_ICONIC_ENABLED
,
8998 SWM_S_JAVA_WORKAROUND
,
8999 SWM_S_MAXIMIZE_HIDE_BAR
,
9000 SWM_S_REGION_PADDING
,
9005 SWM_S_STACK_ENABLED
,
9009 SWM_S_URGENT_COLLAPSE
,
9010 SWM_S_URGENT_ENABLED
,
9011 SWM_S_VERBOSE_LAYOUT
,
9014 SWM_S_WINDOW_CLASS_ENABLED
,
9015 SWM_S_WINDOW_INSTANCE_ENABLED
,
9016 SWM_S_WINDOW_NAME_ENABLED
,
9017 SWM_S_WORKSPACE_CLAMP
,
9018 SWM_S_WORKSPACE_LIMIT
,
9019 SWM_S_WORKSPACE_NAME
,
9023 setconfvalue(const char *selector
, const char *value
, int flags
)
9025 struct workspace
*ws
;
9026 int i
, ws_id
, num_screens
;
9027 char *b
, *str
, *sp
, s
[1024];
9030 case SWM_S_BAR_ACTION
:
9032 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
9033 err(1, "setconfvalue: bar_action");
9035 case SWM_S_BAR_AT_BOTTOM
:
9036 bar_at_bottom
= (atoi(value
) != 0);
9038 case SWM_S_BAR_BORDER_WIDTH
:
9039 bar_border_width
= atoi(value
);
9040 if (bar_border_width
< 0)
9041 bar_border_width
= 0;
9043 case SWM_S_BAR_DELAY
:
9044 /* No longer needed; leave to not break old conf files. */
9046 case SWM_S_BAR_ENABLED
:
9047 bar_enabled
= (atoi(value
) != 0);
9049 case SWM_S_BAR_ENABLED_WS
:
9050 ws_id
= atoi(selector
) - 1;
9051 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9052 errx(1, "setconfvalue: bar_enabled_ws: invalid "
9053 "workspace %d.", ws_id
+ 1);
9055 num_screens
= get_screen_count();
9056 for (i
= 0; i
< num_screens
; i
++) {
9057 ws
= (struct workspace
*)&screens
[i
].ws
;
9058 ws
[ws_id
].bar_enabled
= (atoi(value
) != 0);
9061 case SWM_S_BAR_FONT
:
9063 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
9064 err(1, "setconfvalue: asprintf: failed to allocate "
9065 "memory for bar_fonts.");
9068 /* If already in xft mode, then we are done. */
9069 if (!bar_font_legacy
)
9072 if ((sp
= str
= strdup(value
)) == NULL
)
9073 err(1, "setconfvalue: strdup");
9075 /* If there are any non-XLFD entries, switch to Xft mode. */
9076 while ((b
= strsep(&sp
, ",")) != NULL
) {
9080 bar_font_legacy
= false;
9087 case SWM_S_BAR_FORMAT
:
9089 if ((bar_format
= strdup(value
)) == NULL
)
9090 err(1, "setconfvalue: bar_format");
9092 case SWM_S_BAR_JUSTIFY
:
9093 if (strcmp(value
, "left") == 0)
9094 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
9095 else if (strcmp(value
, "center") == 0)
9096 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
9097 else if (strcmp(value
, "right") == 0)
9098 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
9100 errx(1, "invalid bar_justify");
9102 case SWM_S_BORDER_WIDTH
:
9103 border_width
= atoi(value
);
9104 if (border_width
< 0)
9107 case SWM_S_BOUNDARY_WIDTH
:
9108 boundary_width
= atoi(value
);
9109 if (boundary_width
< 0)
9112 case SWM_S_CLOCK_ENABLED
:
9113 clock_enabled
= (atoi(value
) != 0);
9115 case SWM_S_CLOCK_FORMAT
:
9116 #ifndef SWM_DENY_CLOCK_FORMAT
9118 if ((clock_format
= strdup(value
)) == NULL
)
9119 err(1, "setconfvalue: clock_format");
9122 case SWM_S_CYCLE_EMPTY
:
9123 cycle_empty
= (atoi(value
) != 0);
9125 case SWM_S_CYCLE_VISIBLE
:
9126 cycle_visible
= (atoi(value
) != 0);
9128 case SWM_S_DIALOG_RATIO
:
9129 dialog_ratio
= atof(value
);
9130 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
9133 case SWM_S_DISABLE_BORDER
:
9134 disable_border
= (atoi(value
) != 0);
9136 case SWM_S_FOCUS_CLOSE
:
9137 if (strcmp(value
, "first") == 0)
9138 focus_close
= SWM_STACK_BOTTOM
;
9139 else if (strcmp(value
, "last") == 0)
9140 focus_close
= SWM_STACK_TOP
;
9141 else if (strcmp(value
, "next") == 0)
9142 focus_close
= SWM_STACK_ABOVE
;
9143 else if (strcmp(value
, "previous") == 0)
9144 focus_close
= SWM_STACK_BELOW
;
9146 errx(1, "focus_close");
9148 case SWM_S_FOCUS_CLOSE_WRAP
:
9149 focus_close_wrap
= (atoi(value
) != 0);
9151 case SWM_S_FOCUS_DEFAULT
:
9152 if (strcmp(value
, "last") == 0)
9153 focus_default
= SWM_STACK_TOP
;
9154 else if (strcmp(value
, "first") == 0)
9155 focus_default
= SWM_STACK_BOTTOM
;
9157 errx(1, "focus_default");
9159 case SWM_S_FOCUS_MODE
:
9160 if (strcmp(value
, "default") == 0)
9161 focus_mode
= SWM_FOCUS_DEFAULT
;
9162 else if (strcmp(value
, "follow") == 0 ||
9163 strcmp(value
, "follow_cursor") == 0)
9164 focus_mode
= SWM_FOCUS_FOLLOW
;
9165 else if (strcmp(value
, "manual") == 0)
9166 focus_mode
= SWM_FOCUS_MANUAL
;
9168 errx(1, "focus_mode");
9170 case SWM_S_ICONIC_ENABLED
:
9171 iconic_enabled
= (atoi(value
) != 0);
9173 case SWM_S_JAVA_WORKAROUND
:
9174 java_workaround
= (atoi(value
) != 0);
9176 case SWM_S_MAXIMIZE_HIDE_BAR
:
9177 maximize_hide_bar
= atoi(value
);
9179 case SWM_S_REGION_PADDING
:
9180 region_padding
= atoi(value
);
9181 if (region_padding
< 0)
9184 case SWM_S_SPAWN_ORDER
:
9185 if (strcmp(value
, "first") == 0)
9186 spawn_position
= SWM_STACK_BOTTOM
;
9187 else if (strcmp(value
, "last") == 0)
9188 spawn_position
= SWM_STACK_TOP
;
9189 else if (strcmp(value
, "next") == 0)
9190 spawn_position
= SWM_STACK_ABOVE
;
9191 else if (strcmp(value
, "previous") == 0)
9192 spawn_position
= SWM_STACK_BELOW
;
9194 errx(1, "spawn_position");
9196 case SWM_S_SPAWN_TERM
:
9197 setconfspawn("term", value
, 0);
9198 setconfspawn("spawn_term", value
, 0);
9201 /* No longer needed; leave to not break old conf files. */
9203 case SWM_S_SS_ENABLED
:
9204 /* No longer needed; leave to not break old conf files. */
9206 case SWM_S_STACK_ENABLED
:
9207 stack_enabled
= (atoi(value
) != 0);
9209 case SWM_S_TERM_WIDTH
:
9210 term_width
= atoi(value
);
9214 case SWM_S_TILE_GAP
:
9215 tile_gap
= atoi(value
);
9217 case SWM_S_TRACK_PID_WS
:
9218 track_pid_ws
= atoi(value
);
9220 case SWM_S_URGENT_COLLAPSE
:
9221 urgent_collapse
= (atoi(value
) != 0);
9223 case SWM_S_URGENT_ENABLED
:
9224 urgent_enabled
= (atoi(value
) != 0);
9226 case SWM_S_VERBOSE_LAYOUT
:
9227 verbose_layout
= (atoi(value
) != 0);
9228 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
9230 layouts
[i
].l_string
= fancy_stacker
;
9232 layouts
[i
].l_string
= plain_stacker
;
9235 case SWM_S_WARP_FOCUS
:
9236 warp_focus
= (atoi(value
) != 0);
9238 case SWM_S_WARP_POINTER
:
9239 warp_pointer
= (atoi(value
) != 0);
9241 case SWM_S_WINDOW_CLASS_ENABLED
:
9242 window_class_enabled
= (atoi(value
) != 0);
9244 case SWM_S_WINDOW_INSTANCE_ENABLED
:
9245 window_instance_enabled
= (atoi(value
) != 0);
9247 case SWM_S_WINDOW_NAME_ENABLED
:
9248 window_name_enabled
= (atoi(value
) != 0);
9250 case SWM_S_WORKSPACE_CLAMP
:
9251 workspace_clamp
= (atoi(value
) != 0);
9253 case SWM_S_WORKSPACE_LIMIT
:
9254 workspace_limit
= atoi(value
);
9255 if (workspace_limit
> SWM_WS_MAX
)
9256 workspace_limit
= SWM_WS_MAX
;
9257 else if (workspace_limit
< 1)
9258 workspace_limit
= 1;
9260 ewmh_update_desktops();
9262 case SWM_S_WORKSPACE_NAME
:
9263 if (getenv("SWM_STARTED") != NULL
)
9267 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9268 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
9270 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9271 errx(1, "setconfvalue: workspace_name: invalid "
9272 "workspace %d.", ws_id
+ 1);
9274 num_screens
= get_screen_count();
9275 for (i
= 0; i
< num_screens
; ++i
) {
9276 ws
= (struct workspace
*)&screens
[i
].ws
;
9278 if (strlen(s
) > 0) {
9279 free(ws
[ws_id
].name
);
9280 if ((ws
[ws_id
].name
= strdup(s
)) == NULL
)
9281 err(1, "setconfvalue: workspace_name.");
9283 ewmh_update_desktop_names();
9284 ewmh_get_desktop_names();
9295 setconfmodkey(const char *selector
, const char *value
, int flags
)
9297 /* suppress unused warnings since vars are needed */
9301 if (strncasecmp(value
, "Mod1", strlen("Mod1")) == 0)
9302 update_modkey(XCB_MOD_MASK_1
);
9303 else if (strncasecmp(value
, "Mod2", strlen("Mod2")) == 0)
9304 update_modkey(XCB_MOD_MASK_2
);
9305 else if (strncasecmp(value
, "Mod3", strlen("Mod3")) == 0)
9306 update_modkey(XCB_MOD_MASK_3
);
9307 else if (strncasecmp(value
, "Mod4", strlen("Mod4")) == 0)
9308 update_modkey(XCB_MOD_MASK_4
);
9309 else if (strncasecmp(value
, "Mod5", strlen("Mod5")) == 0)
9310 update_modkey(XCB_MOD_MASK_5
);
9317 setconfcolor(const char *selector
, const char *value
, int flags
)
9319 int first
, last
, i
= 0, num_screens
;
9321 num_screens
= get_screen_count();
9323 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
9324 if (selector
== NULL
|| strlen(selector
) == 0 ||
9325 (last
= atoi(selector
) - 1) < 0) {
9327 last
= num_screens
- 1;
9332 if (last
>= num_screens
) {
9333 add_startup_exception("invalid screen index: %d out of bounds "
9334 "(maximum %d)", last
+ 1, num_screens
);
9338 for (i
= first
; i
<= last
; ++i
) {
9339 setscreencolor(value
, i
, flags
);
9342 * When setting focus/unfocus colors, we need to also
9343 * set maximize colors to match if they haven't been customized.
9345 if (flags
== SWM_S_COLOR_FOCUS
&&
9346 !screens
[i
].c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].manual
)
9347 setscreencolor(value
, i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
9348 else if (flags
== SWM_S_COLOR_UNFOCUS
&&
9349 !screens
[i
].c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].manual
)
9350 setscreencolor(value
, i
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
9352 screens
[i
].c
[flags
].manual
= 1;
9359 setconfregion(const char *selector
, const char *value
, int flags
)
9361 /* suppress unused warnings since vars are needed */
9365 custom_region(value
);
9370 setautorun(const char *selector
, const char *value
, int flags
)
9374 char *ap
, *sp
, *str
;
9380 /* suppress unused warnings since vars are needed */
9384 if (getenv("SWM_STARTED"))
9388 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9389 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
9391 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9392 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
9394 sp
= str
= expand_tilde((char *)&s
);
9397 * This is a little intricate
9399 * If the pid already exists we simply reuse it because it means it was
9400 * used before AND not claimed by manage_window. We get away with
9401 * altering it in the parent after INSERT because this can not be a race
9404 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
9407 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
9409 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
9410 err(1, "setautorun: realloc");
9411 a
.argv
[argc
- 1] = ap
;
9414 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
9415 err(1, "setautorun: realloc");
9416 a
.argv
[argc
] = NULL
;
9418 if ((pid
= fork()) == 0) {
9419 spawn(ws_id
, &a
, true);
9429 p
= calloc(1, sizeof *p
);
9432 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
9442 setlayout(const char *selector
, const char *value
, int flags
)
9444 struct workspace
*ws
;
9445 int ws_id
, i
, mg
, ma
, si
, ar
;
9446 int st
= SWM_V_STACK
, num_screens
;
9450 /* suppress unused warnings since vars are needed */
9455 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
9456 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
9457 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9458 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9461 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9462 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
9464 if (strcasecmp(s
, "vertical") == 0)
9466 else if (strcasecmp(s
, "vertical_flip") == 0) {
9469 } else if (strcasecmp(s
, "horizontal") == 0)
9471 else if (strcasecmp(s
, "horizontal_flip") == 0) {
9474 } else if (strcasecmp(s
, "fullscreen") == 0)
9477 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9478 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9483 initial_layouts
[ws_id
].configured
= true;
9484 initial_layouts
[ws_id
].layout
= st
;
9485 initial_layouts
[ws_id
].master_grow
= mg
;
9486 initial_layouts
[ws_id
].master_add
= ma
;
9487 initial_layouts
[ws_id
].stack_inc
= si
;
9488 initial_layouts
[ws_id
].always_raise
= ar
;
9489 initial_layouts
[ws_id
].apply_flip
= f
;
9491 num_screens
= get_screen_count();
9492 for (i
= 0; i
< num_screens
; ++i
) {
9493 ws
= &screens
[i
].ws
[ws_id
];
9501 initlayout(struct workspace
*ws
)
9507 if (!initial_layouts
[ws_id
].configured
)
9510 ws
->cur_layout
= &layouts
[initial_layouts
[ws_id
].layout
];
9512 ws
->cur_layout
->l_config(ws
, SWM_ARG_ID_STACKINIT
);
9514 ws
->always_raise
= (initial_layouts
[ws_id
].always_raise
!= 0);
9517 for (i
= 0; i
< abs(initial_layouts
[ws_id
].master_grow
); i
++) {
9518 ws
->cur_layout
->l_config(ws
,
9519 initial_layouts
[ws_id
].master_grow
>= 0 ? SWM_ARG_ID_MASTERGROW
:
9520 SWM_ARG_ID_MASTERSHRINK
);
9523 for (i
= 0; i
< abs(initial_layouts
[ws_id
].master_add
); i
++) {
9524 ws
->cur_layout
->l_config(ws
,
9525 initial_layouts
[ws_id
].master_add
>= 0 ? SWM_ARG_ID_MASTERADD
:
9526 SWM_ARG_ID_MASTERDEL
);
9529 for (i
= 0; i
< abs(initial_layouts
[ws_id
].stack_inc
); i
++) {
9530 ws
->cur_layout
->l_config(ws
,
9531 initial_layouts
[ws_id
].stack_inc
>= 0 ? SWM_ARG_ID_STACKINC
:
9532 SWM_ARG_ID_STACKDEC
);
9535 if (initial_layouts
[ws_id
].apply_flip
) {
9536 ws
->cur_layout
->l_config(ws
,
9537 SWM_ARG_ID_FLIPLAYOUT
);
9541 /* config options */
9542 struct config_option
{
9544 int (*func
)(const char*, const char*, int);
9547 struct config_option configopt
[] = {
9548 { "autorun", setautorun
, 0 },
9549 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
9550 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
9551 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
9552 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
9553 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
9554 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
9555 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
9556 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
9557 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
9558 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
9559 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
9560 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
9561 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
9562 { "bind", setconfbinding
, 0 },
9563 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
9564 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
9565 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
9566 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
9567 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
9568 { "color_focus_maximized", setconfcolor
, SWM_S_COLOR_FOCUS_MAXIMIZED
},
9569 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
9570 { "color_unfocus_maximized", setconfcolor
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
},
9571 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
9572 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
9573 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
9574 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
9575 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
9576 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
9577 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
9578 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
9579 { "iconic_enabled", setconfvalue
, SWM_S_ICONIC_ENABLED
},
9580 { "java_workaround", setconfvalue
, SWM_S_JAVA_WORKAROUND
},
9581 { "keyboard_mapping", setkeymapping
, 0 },
9582 { "layout", setlayout
, 0 },
9583 { "maximize_hide_bar", setconfvalue
, SWM_S_MAXIMIZE_HIDE_BAR
},
9584 { "modkey", setconfmodkey
, 0 },
9585 { "program", setconfspawn
, 0 },
9586 { "quirk", setconfquirk
, 0 },
9587 { "region", setconfregion
, 0 },
9588 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
9589 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
9590 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
9591 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
9592 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
9593 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
9594 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
9595 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
9596 { "title_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
}, /* For backwards compat. */
9597 { "title_name_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
}, /* For backwards compat. */
9598 { "track_pid_ws", setconfvalue
, SWM_S_TRACK_PID_WS
},
9599 { "urgent_collapse", setconfvalue
, SWM_S_URGENT_COLLAPSE
},
9600 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
9601 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
9602 { "warp_focus", setconfvalue
, SWM_S_WARP_FOCUS
},
9603 { "warp_pointer", setconfvalue
, SWM_S_WARP_POINTER
},
9604 { "window_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
},
9605 { "window_instance_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
},
9606 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
9607 { "workspace_clamp", setconfvalue
, SWM_S_WORKSPACE_CLAMP
},
9608 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
9609 { "name", setconfvalue
, SWM_S_WORKSPACE_NAME
},
9613 _add_startup_exception(const char *fmt
, va_list ap
)
9615 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
9616 warn("%s: asprintf", __func__
);
9620 add_startup_exception(const char *fmt
, ...)
9626 if (startup_exception
)
9629 /* force bar to be enabled due to exception */
9633 _add_startup_exception(fmt
, ap
);
9638 conf_load(const char *filename
, int keymapping
)
9641 char *line
= NULL
, *cp
, *ce
, *optsub
, *optval
= NULL
;
9642 size_t linelen
, lineno
= 0;
9643 int wordlen
, i
, optidx
, count
;
9644 struct config_option
*opt
= NULL
;
9646 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
9648 if (filename
== NULL
) {
9649 warnx("conf_load: no filename");
9653 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
9655 if ((config
= fopen(filename
, "r")) == NULL
) {
9656 warn("conf_load: fopen: %s", filename
);
9660 while (!feof(config
)) {
9664 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
9665 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
9667 err(1, "%s", filename
);
9672 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9673 if (cp
[0] == '\0') {
9677 /* get config option */
9678 wordlen
= strcspn(cp
, "=[ \t\n");
9680 add_startup_exception("%s: line %zd: no option found",
9685 for (i
= 0; i
< LENGTH(configopt
); i
++) {
9686 opt
= &configopt
[i
];
9687 if (strncasecmp(cp
, opt
->optname
, wordlen
) == 0 &&
9688 (int)strlen(opt
->optname
) == wordlen
) {
9694 add_startup_exception("%s: line %zd: unknown option "
9695 "%.*s", filename
, lineno
, wordlen
, cp
);
9698 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
9699 add_startup_exception("%s: line %zd: invalid option "
9700 "%.*s", filename
, lineno
, wordlen
, cp
);
9704 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9706 /* get [selector] if any */
9711 /* Get length of selector. */
9712 for (ce
= ++cp
; *ce
!= '\0'; ++ce
) {
9713 /* Find matching (not escaped) bracket. */
9714 if (*ce
== ']' && *(ce
- 1) != '\\') {
9721 add_startup_exception("%s: line %zd: syntax "
9722 "error: unterminated selector", filename
,
9727 /* ce points at ']'; terminate optsub. */
9732 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
9735 if (strlen(optval
) == 0) {
9736 add_startup_exception("%s: line %zd: must supply value "
9737 "to %s", filename
, lineno
,
9738 configopt
[optidx
].optname
);
9741 /* trim trailing spaces */
9742 ce
= optval
+ strlen(optval
) - 1;
9743 while (ce
> optval
&& isspace(*ce
)) --ce
;
9745 /* call function to deal with it all */
9746 if (configopt
[optidx
].func(optsub
, optval
,
9747 configopt
[optidx
].funcflags
) != 0) {
9748 add_startup_exception("%s: line %zd: invalid data for "
9749 "%s", filename
, lineno
, configopt
[optidx
].optname
);
9758 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
9764 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
9766 struct ws_win
*parent
, *w
;
9767 struct swm_region
*r
;
9768 struct workspace
*ws
;
9769 xcb_icccm_wm_hints_t wmh
;
9771 parent
= find_window(win
->transient
);
9773 parent
->focus_child
= win
;
9775 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
9776 " for %#x trans %#x\n", win
->id
, win
->transient
);
9778 r
= root_to_region(win
->s
->root
, SWM_CK_ALL
);
9780 /* parent doen't exist in our window list */
9781 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
9782 if (xcb_icccm_get_wm_hints_reply(conn
,
9783 xcb_icccm_get_wm_hints(conn
, w
->id
),
9785 warnx("can't get hints for %#x", w
->id
);
9789 if (win
->hints
.window_group
!= wmh
.window_group
)
9792 w
->focus_child
= win
;
9793 win
->transient
= w
->id
;
9795 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
9796 "transient to %#x\n", win
->transient
);
9803 window_get_pid(xcb_window_t win
)
9808 xcb_get_property_cookie_t pc
;
9809 xcb_get_property_reply_t
*pr
;
9811 apid
= get_atom_from_string("_NET_WM_PID");
9812 if (apid
== XCB_ATOM_NONE
)
9815 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
9816 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9819 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
9824 if (pr
->type
== apid
&& pr
->format
== 32)
9825 ret
= *((pid_t
*)xcb_get_property_value(pr
));
9831 apid
= get_atom_from_string("_SWM_PID");
9832 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
9834 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9837 if (pr
->type
!= apid
) {
9842 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
9849 get_swm_ws(xcb_window_t id
)
9855 xcb_get_property_reply_t
*gpr
;
9857 gpr
= xcb_get_property_reply(conn
,
9858 xcb_get_property(conn
, 0, id
, a_swm_ws
,
9859 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
9864 proplen
= xcb_get_property_value_length(gpr
);
9866 prop
= malloc(proplen
+ 1);
9869 xcb_get_property_value(gpr
),
9871 prop
[proplen
] = '\0';
9878 DNPRINTF(SWM_D_PROP
, "get_swm_ws: _SWM_WS: %s\n", prop
);
9879 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
9881 DNPRINTF(SWM_D_PROP
, "get_swm_ws: win #%s: %s",
9891 get_ws_idx(struct ws_win
*win
)
9893 xcb_get_property_reply_t
*gpr
;
9899 gpr
= xcb_get_property_reply(conn
,
9900 xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
9901 XCB_ATOM_CARDINAL
, 0, 1),
9904 if (gpr
->type
== XCB_ATOM_CARDINAL
&& gpr
->format
== 32)
9905 ws_idx
= *((int *)xcb_get_property_value(gpr
));
9909 if (ws_idx
== -1 && !(win
->quirks
& SWM_Q_IGNORESPAWNWS
))
9910 ws_idx
= get_swm_ws(win
->id
);
9912 if (ws_idx
> workspace_limit
- 1 || ws_idx
< -1)
9915 DNPRINTF(SWM_D_PROP
, "get_ws_idx: win %#x, ws_idx: %d\n", win
->id
,
9922 reparent_window(struct ws_win
*win
)
9924 xcb_void_cookie_t c
;
9925 xcb_generic_error_t
*error
;
9928 win
->frame
= xcb_generate_id(conn
);
9930 DNPRINTF(SWM_D_MISC
, "reparent_window: win %#x, frame: %#x\n",
9931 win
->id
, win
->frame
);
9934 XCB_EVENT_MASK_ENTER_WINDOW
|
9935 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
9936 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
9937 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
|
9938 XCB_EVENT_MASK_EXPOSURE
;
9940 wa
[0] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
9943 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
->frame
, win
->s
->root
,
9944 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), 0,
9945 XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
9946 XCB_CW_EVENT_MASK
, wa
);
9948 win
->state
= SWM_WIN_STATE_REPARENTING
;
9949 c
= xcb_reparent_window_checked(conn
, win
->id
, win
->frame
, 0, 0);
9950 if ((error
= xcb_request_check(conn
, c
))) {
9951 DNPRINTF(SWM_D_MISC
, "reparent_window: error:\n");
9956 xcb_destroy_window(conn
, win
->frame
);
9957 win
->frame
= XCB_WINDOW_NONE
;
9959 xcb_change_save_set(conn
, XCB_SET_MODE_INSERT
, win
->id
);
9961 DNPRINTF(SWM_D_MISC
, "reparent_window: done.\n");
9965 unparent_window(struct ws_win
*win
)
9967 xcb_change_save_set(conn
, XCB_SET_MODE_DELETE
, win
->id
);
9968 xcb_reparent_window(conn
, win
->id
, win
->s
->root
, X(win
), Y(win
));
9969 xcb_destroy_window(conn
, win
->frame
);
9970 win
->frame
= XCB_WINDOW_NONE
;
9971 win
->state
= SWM_WIN_STATE_UNPARENTING
;
9975 manage_window(xcb_window_t id
, int spawn_pos
, bool mapping
)
9977 struct ws_win
*win
= NULL
, *ww
;
9978 struct swm_region
*r
;
9981 xcb_get_geometry_reply_t
*gr
;
9982 xcb_get_window_attributes_reply_t
*war
= NULL
;
9983 xcb_window_t trans
= XCB_WINDOW_NONE
;
9984 uint32_t i
, wa
[1], new_flags
;
9985 int ws_idx
, force_ws
= -1;
9986 char *class, *instance
, *name
;
9989 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region bar; "
9994 if (find_region(id
)) {
9995 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region window; "
10000 if ((win
= find_window(id
)) != NULL
) {
10001 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x already "
10002 "managed; skipping.)\n", id
);
10003 return (win
); /* Already managed. */
10006 /* See if window is on the unmanaged list. */
10007 if ((win
= find_unmanaged_window(id
)) != NULL
) {
10008 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x found on "
10009 "unmanaged list.\n", id
);
10010 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10012 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is new.\n", id
);
10015 war
= xcb_get_window_attributes_reply(conn
,
10016 xcb_get_window_attributes(conn
, id
), NULL
);
10018 DNPRINTF(SWM_D_EVENT
, "manage_window: window lost.\n");
10022 if (war
->override_redirect
) {
10023 DNPRINTF(SWM_D_EVENT
, "manage_window: override_redirect; "
10028 if (!mapping
&& war
->map_state
== XCB_MAP_STATE_UNMAPPED
&&
10029 get_win_state(id
) == XCB_ICCCM_WM_STATE_WITHDRAWN
) {
10030 DNPRINTF(SWM_D_EVENT
, "manage_window: window withdrawn; "
10035 /* Try to get initial window geometry. */
10036 gr
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, id
), NULL
);
10038 DNPRINTF(SWM_D_MISC
, "manage_window: get geometry failed.\n");
10042 /* Figure out which region the window belongs to. */
10043 r
= root_to_region(gr
->root
, SWM_CK_ALL
);
10045 /* Handle special windows with special _NET_WM_WINDOW_TYPE */
10046 if (ewmh_handle_special_types(id
, r
)) {
10047 DNPRINTF(SWM_D_EVENT
, "manage_window: "
10048 "unmanaged ewmh window type\n");
10053 /* Create and initialize ws_win object. */
10054 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
10055 err(1, "manage_window: calloc: failed to allocate "
10056 "memory for new window");
10061 /* Ignore window border if there is one. */
10062 WIDTH(win
) = gr
->width
;
10063 HEIGHT(win
) = gr
->height
;
10064 X(win
) = gr
->x
+ gr
->border_width
;
10065 Y(win
) = gr
->y
+ gr
->border_width
;
10066 win
->bordered
= false;
10067 win
->mapped
= (war
->map_state
!= XCB_MAP_STATE_UNMAPPED
);
10068 win
->s
= r
->s
; /* this never changes */
10072 /* Select which X events to monitor and set border pixel color. */
10073 wa
[0] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
10074 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
10076 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_EVENT_MASK
, wa
);
10078 /* Get WM_SIZE_HINTS. */
10079 xcb_icccm_get_wm_normal_hints_reply(conn
,
10080 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
10083 /* Get WM_HINTS. */
10084 xcb_icccm_get_wm_hints_reply(conn
,
10085 xcb_icccm_get_wm_hints(conn
, win
->id
),
10086 &win
->hints
, NULL
);
10088 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
10089 xcb_icccm_get_wm_transient_for_reply(conn
,
10090 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
10093 win
->transient
= trans
;
10094 set_child_transient(win
, &win
->transient
);
10097 /* Get WM_PROTOCOLS. */
10098 get_wm_protocols(win
);
10101 /* Must be after getting WM_HINTS and WM_PROTOCOLS. */
10102 DNPRINTF(SWM_D_FOCUS
, "manage_window: input_model: %s\n",
10103 get_win_input_model(win
));
10106 /* Set initial quirks based on EWMH. */
10107 ewmh_autoquirk(win
);
10109 /* Determine initial quirks. */
10110 xcb_icccm_get_wm_class_reply(conn
,
10111 xcb_icccm_get_wm_class(conn
, win
->id
),
10114 class = win
->ch
.class_name
? win
->ch
.class_name
: "";
10115 instance
= win
->ch
.instance_name
? win
->ch
.instance_name
: "";
10116 name
= get_win_name(win
->id
);
10118 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, instance: %s, "
10119 "name: %s\n", class, instance
, name
);
10121 /* java is retarded so treat it special */
10122 if (strstr(instance
, "sun-awt")) {
10123 DNPRINTF(SWM_D_CLASS
, "manage_window: java window detected.\n");
10127 TAILQ_FOREACH(qp
, &quirks
, entry
) {
10128 if (regexec(&qp
->regex_class
, class, 0, NULL
, 0) == 0 &&
10129 regexec(&qp
->regex_instance
, instance
, 0, NULL
, 0) == 0 &&
10130 regexec(&qp
->regex_name
, name
, 0, NULL
, 0) == 0) {
10131 DNPRINTF(SWM_D_CLASS
, "manage_window: matched "
10132 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp
->class,
10133 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
10134 win
->quirks
= qp
->quirk
;
10135 if (qp
->ws
>= 0 && qp
->ws
< workspace_limit
)
10142 /* Reset font sizes (the bruteforce way; no default keybinding). */
10143 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
10144 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
10145 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
10146 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
10147 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
10150 /* Figure out which workspace the window belongs to. */
10151 if (!(win
->quirks
& SWM_Q_IGNOREPID
) &&
10152 (p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
10153 win
->ws
= &r
->s
->ws
[p
->ws
];
10154 TAILQ_REMOVE(&pidlist
, p
, entry
);
10157 } else if ((ws_idx
= get_ws_idx(win
)) != -1 &&
10159 /* _SWM_WS is set; use that. */
10160 win
->ws
= &r
->s
->ws
[ws_idx
];
10161 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
10162 /* Launch transients in the same ws as parent. */
10168 if (force_ws
!= -1)
10169 win
->ws
= &r
->s
->ws
[force_ws
];
10171 /* Set the _NET_WM_DESKTOP atom. */
10172 DNPRINTF(SWM_D_PROP
, "manage_window: set _NET_WM_DESKTOP: %d\n",
10174 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
10175 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &win
->ws
->idx
);
10177 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
10178 xcb_delete_property(conn
, win
->id
, a_swm_ws
);
10180 /* WS must already be set for this to work. */
10181 store_float_geom(win
);
10183 /* Make sure window is positioned inside its region, if its active. */
10185 region_containment(win
, r
, SWM_CW_ALLSIDES
|
10186 SWM_CW_HARDBOUNDARY
);
10187 update_window(win
);
10190 /* Figure out where to insert the window in the workspace list. */
10191 if (trans
&& (ww
= find_window(trans
)))
10192 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
10193 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_ABOVE
)
10194 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
10196 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_BELOW
)
10197 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
10198 else switch (spawn_pos
) {
10200 case SWM_STACK_TOP
:
10201 case SWM_STACK_ABOVE
:
10202 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
10204 case SWM_STACK_BOTTOM
:
10205 case SWM_STACK_BELOW
:
10206 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
10209 ewmh_update_client_list();
10211 TAILQ_INSERT_HEAD(&win
->ws
->stack
, win
, stack_entry
);
10214 /* Get/apply initial _NET_WM_STATE */
10215 ewmh_get_wm_state(win
);
10217 /* Apply quirks. */
10218 new_flags
= win
->ewmh_flags
;
10220 if (win
->quirks
& SWM_Q_FLOAT
)
10221 new_flags
|= EWMH_F_ABOVE
;
10223 if (win
->quirks
& SWM_Q_ANYWHERE
)
10224 new_flags
|= SWM_F_MANUAL
;
10226 ewmh_apply_flags(win
, new_flags
);
10227 ewmh_update_wm_state(win
);
10229 /* Set initial _NET_WM_ALLOWED_ACTIONS */
10230 ewmh_update_actions(win
);
10232 reparent_window(win
);
10234 DNPRINTF(SWM_D_MISC
, "manage_window: done. win %#x, (x,y) w x h: "
10235 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win
->id
,
10236 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
10237 YESNO(ICONIC(win
)), win
->transient
);
10244 free_window(struct ws_win
*win
)
10246 DNPRINTF(SWM_D_MISC
, "free_window: win %#x\n", WINID(win
));
10251 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
10254 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
10257 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
10261 unmanage_window(struct ws_win
*win
)
10263 DNPRINTF(SWM_D_MISC
, "unmanage_window: win %#x\n", WINID(win
));
10269 unparent_window(win
);
10271 TAILQ_REMOVE(&win
->ws
->stack
, win
, stack_entry
);
10272 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
10273 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
10275 ewmh_update_client_list();
10279 expose(xcb_expose_event_t
*e
)
10284 struct workspace
*ws
;
10287 DNPRINTF(SWM_D_EVENT
, "expose: win %#x, count: %d\n", e
->window
,
10293 if ((b
= find_bar(e
->window
))) {
10296 } else if ((w
= find_window(e
->window
)) && w
->frame
== e
->window
) {
10300 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
10306 DNPRINTF(SWM_D_EVENT
, "expose: done\n");
10310 focusin(xcb_focus_in_event_t
*e
)
10312 struct ws_win
*win
;
10314 DNPRINTF(SWM_D_EVENT
, "focusin: win %#x, mode: %s(%u), "
10315 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10316 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10317 if ((win
= find_window(e
->event
)) && win
!= win
->ws
->focus
&&
10318 win
!= win
->ws
->focus_pending
&&
10319 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10320 win
->ws
->focus_prev
= win
->ws
->focus
;
10321 win
->ws
->focus
= win
;
10322 win
->ws
->focus_pending
= NULL
;
10324 if (win
->ws
->focus_prev
)
10325 draw_frame(win
->ws
->focus_prev
);
10333 focusout(xcb_focus_out_event_t
*e
)
10335 DNPRINTF(SWM_D_EVENT
, "focusout: win %#x, mode: %s(%u), "
10336 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10337 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10342 keypress(xcb_key_press_event_t
*e
)
10345 struct binding
*bp
;
10346 xcb_keysym_t keysym
;
10347 bool replay
= true;
10349 last_event_time
= e
->time
;
10351 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
10353 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
10354 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10355 "state: %u, cleaned: %u, same_screen: %s\n", keysym
, e
->event
,
10356 e
->event_x
, e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
,
10357 e
->root_y
, e
->child
, e
->state
, CLEANMASK(e
->state
),
10358 YESNO(e
->same_screen
));
10360 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10362 /* Look for catch-all. */
10363 if ((bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
)) == NULL
)
10367 replay
= bp
->flags
& BINDING_F_REPLAY
;
10369 if ((ap
= &actions
[bp
->action
]) == NULL
)
10372 if (bp
->action
== FN_SPAWN_CUSTOM
)
10373 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10376 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10378 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10382 DNPRINTF(SWM_D_EVENT
, "keypress: replaying.\n");
10383 /* Pass keypress to event window and unfreeze keyboard queue. */
10384 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10386 /* Release freeze. */
10387 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10391 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
10395 keyrelease(xcb_key_release_event_t
*e
)
10398 struct binding
*bp
;
10399 xcb_keysym_t keysym
;
10401 last_event_time
= e
->time
;
10403 keysym
= xcb_key_release_lookup_keysym(syms
, e
, 0);
10405 DNPRINTF(SWM_D_EVENT
, "keyrelease: keysym: %u, win (x,y): %#x (%d,%d), "
10406 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10407 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
10408 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
10409 e
->child
, e
->state
, YESNO(e
->same_screen
));
10411 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10413 /* Look for catch-all. */
10414 bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
);
10416 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10417 bp
->flags
& BINDING_F_REPLAY
) {
10418 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10419 DNPRINTF(SWM_D_EVENT
, "keyrelease: replaying.\n");
10421 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10422 DNPRINTF(SWM_D_EVENT
, "keyrelease: unfreezing.\n");
10427 DNPRINTF(SWM_D_EVENT
, "keyrelease: done.\n");
10431 buttonpress(xcb_button_press_event_t
*e
)
10433 struct ws_win
*win
= NULL
, *newf
;
10434 struct swm_region
*r
, *old_r
;
10436 struct binding
*bp
;
10437 bool replay
= true;
10439 last_event_time
= e
->time
;
10441 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): %#x (%d,%d), "
10442 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10443 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10444 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10445 e
->state
, YESNO(e
->same_screen
));
10447 if (e
->event
== e
->root
) {
10449 win
= find_window(e
->child
);
10451 /* Focus on empty region */
10452 /* If no windows on region if its empty. */
10453 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10454 if (r
&& TAILQ_EMPTY(&r
->ws
->winlist
)) {
10455 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
10456 if (old_r
&& old_r
!= r
)
10457 unfocus_win(old_r
->ws
->focus
);
10459 DNPRINTF(SWM_D_FOCUS
, "buttonpress: "
10460 "set_input_focus: %#x, revert-to: parent, "
10461 "time: %#x\n", e
->root
, e
->time
);
10462 xcb_set_input_focus(conn
,
10463 XCB_INPUT_FOCUS_POINTER_ROOT
,
10466 /* Clear bar since empty. */
10469 /* No need to replay event. */
10474 win
= find_window(e
->event
);
10478 newf
= get_focus_magic(win
);
10479 if (win
->ws
->focus
== newf
&& newf
!= win
) {
10480 if (win
->focus_child
== win
)
10481 win
->focus_child
= NULL
;
10487 /* Handle any bound action. */
10488 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10490 /* Look for catch-all. */
10491 if ((bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
)) == NULL
)
10496 replay
= bp
->flags
& BINDING_F_REPLAY
;
10498 if ((ap
= &actions
[bp
->action
]) == NULL
)
10501 if (bp
->action
== FN_SPAWN_CUSTOM
)
10502 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10505 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10507 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10511 DNPRINTF(SWM_D_EVENT
, "buttonpress: replaying.\n");
10512 /* Replay event to event window */
10513 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10515 /* Unfreeze grab events. */
10516 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10523 buttonrelease(xcb_button_release_event_t
*e
)
10526 struct binding
*bp
;
10528 last_event_time
= e
->time
;
10530 DNPRINTF(SWM_D_EVENT
, "buttonrelease: win (x,y): %#x (%d,%d), "
10531 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10532 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10533 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10534 e
->state
, YESNO(e
->same_screen
));
10536 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10538 /* Look for catch-all. */
10539 bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
);
10541 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10542 bp
->flags
& BINDING_F_REPLAY
) {
10543 DNPRINTF(SWM_D_EVENT
, "buttonrelease: replaying.\n");
10544 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10546 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10554 get_win_input_model(struct ws_win
*win
)
10558 * Input Model Input Field WM_TAKE_FOCUS
10559 * No Input False Absent
10560 * Passive True Absent
10561 * Locally Active True Present
10562 * Globally Active False Present
10565 if (ACCEPTS_FOCUS(win
))
10566 inputmodel
= (win
->take_focus
) ? "Locally Active" : "Passive";
10568 inputmodel
= (win
->take_focus
) ? "Globally Active" : "No Input";
10574 print_win_geom(xcb_window_t w
)
10576 xcb_get_geometry_reply_t
*wa
;
10578 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
10580 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: %#x\n",
10585 DNPRINTF(SWM_D_MISC
, "print_win_geom: win %#x, root: %#x, "
10586 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
10587 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
10596 get_stack_mode_name(uint8_t mode
)
10601 case XCB_STACK_MODE_ABOVE
:
10604 case XCB_STACK_MODE_BELOW
:
10607 case XCB_STACK_MODE_TOP_IF
:
10610 case XCB_STACK_MODE_BOTTOM_IF
:
10613 case XCB_STACK_MODE_OPPOSITE
:
10625 configurerequest(xcb_configure_request_event_t
*e
)
10627 struct ws_win
*win
;
10628 struct swm_region
*r
= NULL
;
10630 uint32_t wc
[7] = {0};
10634 if ((win
= find_window(e
->window
)) == NULL
)
10635 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10639 if (swm_debug
& SWM_D_EVENT
) {
10640 print_win_geom(e
->window
);
10642 DNPRINTF(SWM_D_EVENT
, "configurerequest: win %#x, "
10643 "parent: %#x, new: %s, value_mask: %u { ", e
->window
,
10644 e
->parent
, YESNO(new), e
->value_mask
);
10645 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
10646 DPRINTF("X: %d ", e
->x
);
10647 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
10648 DPRINTF("Y: %d ", e
->y
);
10649 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10650 DPRINTF("W: %u ", e
->width
);
10651 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10652 DPRINTF("H: %u ", e
->height
);
10653 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
10654 DPRINTF("Border: %u ", e
->border_width
);
10655 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
10656 DPRINTF("Sibling: %#x ", e
->sibling
);
10657 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
10658 DPRINTF("StackMode: %s(%u) ",
10659 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
10665 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10666 mask
|= XCB_CONFIG_WINDOW_X
;
10669 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10670 mask
|= XCB_CONFIG_WINDOW_Y
;
10673 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
10674 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
10675 wc
[i
++] = e
->width
;
10677 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
10678 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
10679 wc
[i
++] = e
->height
;
10681 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
10682 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
10683 wc
[i
++] = e
->border_width
;
10685 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
10686 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
10687 wc
[i
++] = e
->sibling
;
10689 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
10690 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
10691 wc
[i
++] = e
->stack_mode
;
10695 xcb_configure_window(conn
, e
->window
, mask
, wc
);
10698 } else if ((!MANUAL(win
) || win
->quirks
& SWM_Q_ANYWHERE
) &&
10699 !FULLSCREEN(win
) && !MAXIMIZED(win
)) {
10702 else if (win
->ws
->old_r
)
10703 r
= win
->ws
->old_r
;
10705 /* windows are centered unless ANYWHERE quirk is set. */
10706 if (win
->quirks
& SWM_Q_ANYWHERE
) {
10707 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10708 win
->g_float
.x
= e
->x
;
10710 win
->g_float
.x
-= X(r
);
10713 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10714 win
->g_float
.y
= e
->y
;
10716 win
->g_float
.y
-= Y(r
);
10720 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10721 win
->g_float
.w
= e
->width
;
10723 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10724 win
->g_float
.h
= e
->height
;
10726 win
->g_floatvalid
= true;
10728 if (!MAXIMIZED(win
) && !FULLSCREEN(win
) &&
10729 (TRANS(win
) || (ABOVE(win
) &&
10730 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]))) {
10731 WIDTH(win
) = win
->g_float
.w
;
10732 HEIGHT(win
) = win
->g_float
.h
;
10735 update_floater(win
);
10738 config_win(win
, e
);
10742 config_win(win
, e
);
10746 config_win(win
, e
);
10750 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
10754 configurenotify(xcb_configure_notify_event_t
*e
)
10756 struct ws_win
*win
;
10758 DNPRINTF(SWM_D_EVENT
, "configurenotify: win %#x, event win: %#x, "
10759 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
10760 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
10761 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
10762 YESNO(e
->override_redirect
));
10764 win
= find_window(e
->window
);
10767 if (font_adjusted
&& win
->ws
->r
) {
10775 destroynotify(xcb_destroy_notify_event_t
*e
)
10777 struct ws_win
*win
;
10778 struct workspace
*ws
;
10780 DNPRINTF(SWM_D_EVENT
, "destroynotify: win %#x\n", e
->window
);
10782 if ((win
= find_window(e
->window
)) == NULL
) {
10783 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10785 /* Window is on unmanaged list. */
10786 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10793 if (win
->frame
== e
->window
) {
10794 DNPRINTF(SWM_D_EVENT
, "destroynotify: frame for win %#x\n",
10796 win
->frame
= XCB_WINDOW_NONE
;
10800 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10801 /* If we were focused, make sure we focus on something else. */
10802 if (win
== ws
->focus
) {
10803 ws
->focus_pending
= get_focus_prev(win
);
10804 if (ws
->focus_pending
== win
)
10805 ws
->focus_pending
= NULL
;
10809 unmanage_window(win
);
10810 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10814 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
10815 if (ws
->focus_pending
) {
10816 focus_win(ws
->focus_pending
);
10817 ws
->focus_pending
= NULL
;
10818 } else if (ws
->focus
== NULL
) {
10819 DNPRINTF(SWM_D_FOCUS
, "destroynotify: set_input_focus: "
10820 "%#x, revert-to: parent, time: 0\n", ws
->r
->id
);
10821 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
10822 ws
->r
->id
, XCB_CURRENT_TIME
);
10829 DNPRINTF(SWM_D_EVENT
, "destroynotify: done.\n");
10834 get_notify_detail_label(uint8_t detail
)
10839 case XCB_NOTIFY_DETAIL_ANCESTOR
:
10840 label
= "Ancestor";
10842 case XCB_NOTIFY_DETAIL_VIRTUAL
:
10845 case XCB_NOTIFY_DETAIL_INFERIOR
:
10846 label
= "Inferior";
10848 case XCB_NOTIFY_DETAIL_NONLINEAR
:
10849 label
= "Nonlinear";
10851 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
10852 label
= "NonlinearVirtual";
10854 case XCB_NOTIFY_DETAIL_POINTER
:
10857 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
10858 label
= "PointerRoot";
10860 case XCB_NOTIFY_DETAIL_NONE
:
10871 get_notify_mode_label(uint8_t mode
)
10876 case XCB_NOTIFY_MODE_NORMAL
:
10879 case XCB_NOTIFY_MODE_GRAB
:
10882 case XCB_NOTIFY_MODE_UNGRAB
:
10885 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
10886 label
= "WhileGrabbed";
10896 get_state_mask_label(uint16_t state
)
10901 case XCB_KEY_BUT_MASK_SHIFT
:
10902 label
= "ShiftMask";
10904 case XCB_KEY_BUT_MASK_LOCK
:
10905 label
= "LockMask";
10907 case XCB_KEY_BUT_MASK_CONTROL
:
10908 label
= "ControlMask";
10910 case XCB_KEY_BUT_MASK_MOD_1
:
10911 label
= "Mod1Mask";
10913 case XCB_KEY_BUT_MASK_MOD_2
:
10914 label
= "Mod2Mask";
10916 case XCB_KEY_BUT_MASK_MOD_3
:
10917 label
= "Mod3Mask";
10919 case XCB_KEY_BUT_MASK_MOD_4
:
10920 label
= "Mod4Mask";
10922 case XCB_KEY_BUT_MASK_MOD_5
:
10923 label
= "Mod5Mask";
10925 case XCB_KEY_BUT_MASK_BUTTON_1
:
10926 label
= "Button1Mask";
10928 case XCB_KEY_BUT_MASK_BUTTON_2
:
10929 label
= "Button2Mask";
10931 case XCB_KEY_BUT_MASK_BUTTON_3
:
10932 label
= "Button3Mask";
10934 case XCB_KEY_BUT_MASK_BUTTON_4
:
10935 label
= "Button4Mask";
10937 case XCB_KEY_BUT_MASK_BUTTON_5
:
10938 label
= "Button5Mask";
10952 enternotify(xcb_enter_notify_event_t
*e
)
10954 struct ws_win
*win
;
10955 struct swm_region
*r
;
10957 last_event_time
= e
->time
;
10959 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): %#x "
10960 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10961 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10962 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10963 get_notify_mode_label(e
->mode
), e
->mode
,
10964 get_notify_detail_label(e
->detail
), e
->detail
,
10965 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10966 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10969 if (e
->event
== e
->root
&& e
->child
== XCB_WINDOW_NONE
&&
10970 e
->mode
== XCB_NOTIFY_MODE_GRAB
&&
10971 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10972 DNPRINTF(SWM_D_EVENT
, "enternotify: grab inferior; ignoring.\n");
10976 if (focus_mode
== SWM_FOCUS_MANUAL
&&
10977 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10978 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
10982 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10983 e
->mode
== XCB_NOTIFY_MODE_UNGRAB
&&
10984 e
->detail
!= XCB_NOTIFY_DETAIL_ANCESTOR
) {
10985 DNPRINTF(SWM_D_EVENT
, "enternotify: ungrab; ignoring.\n");
10989 if ((win
= find_window(e
->event
)) == NULL
) {
10990 if (e
->event
== e
->root
) {
10991 /* If no windows on pointer region, then focus root. */
10992 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10994 DNPRINTF(SWM_D_EVENT
, "enternotify: "
10995 "NULL region; ignoring.\n");
11001 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
11006 if (e
->mode
== XCB_NOTIFY_MODE_NORMAL
&&
11007 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
11008 DNPRINTF(SWM_D_EVENT
, "enternotify: entering from "
11009 "inferior; ignoring\n");
11013 focus_win(get_focus_magic(win
));
11016 DNPRINTF(SWM_D_EVENT
, "enternotify: done\n");
11023 leavenotify(xcb_leave_notify_event_t
*e
)
11025 last_event_time
= e
->time
;
11027 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): %#x "
11028 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
11029 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
11030 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
11031 get_notify_mode_label(e
->mode
), e
->mode
,
11032 get_notify_detail_label(e
->detail
), e
->detail
,
11033 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
11034 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
11040 mapnotify(xcb_map_notify_event_t
*e
)
11042 struct ws_win
*win
, *parent
= NULL
;
11043 struct workspace
*ws
;
11045 DNPRINTF(SWM_D_EVENT
, "mapnotify: win %#x\n", e
->window
);
11047 if ((win
= manage_window(e
->window
, spawn_position
, false)) == NULL
)
11051 if (win
->state
== SWM_WIN_STATE_REPARENTING
)
11054 if (ws
->r
== NULL
) {
11059 /* Need to know if win was mapped due to ws switch. */
11060 if (ws
->state
== SWM_WS_STATE_MAPPED
) {
11061 if (ws
->focus_pending
&& TRANS(ws
->focus_pending
))
11062 parent
= find_window(win
->transient
);
11064 /* If window's parent is maximized, don't clear it. */
11065 if ((parent
== NULL
) || !MAXIMIZED(parent
))
11066 if (clear_maximized(ws
) > 0)
11070 win
->mapped
= true;
11071 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
11073 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
11074 if (ws
->focus_pending
== win
) {
11076 ws
->focus_pending
= NULL
;
11077 center_pointer(ws
->r
);
11083 DNPRINTF(SWM_D_EVENT
, "mapnotify: done\n");
11089 mappingnotify(xcb_mapping_notify_event_t
*e
)
11091 if (e
->request
!= XCB_MAPPING_POINTER
) {
11092 xcb_refresh_keyboard_mapping(syms
, e
);
11100 maprequest(xcb_map_request_event_t
*e
)
11102 struct ws_win
*win
, *w
= NULL
;
11104 DNPRINTF(SWM_D_EVENT
, "maprequest: win %#x\n",
11107 win
= manage_window(e
->window
, spawn_position
, true);
11111 /* The new window should get focus; prepare. */
11112 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
11113 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
) && ACCEPTS_FOCUS(win
)) {
11114 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
11115 /* See if other wins of same type are already mapped. */
11116 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
11117 if (w
== win
|| !w
->mapped
)
11120 if (w
->ch
.class_name
&&
11121 win
->ch
.class_name
&&
11122 strcmp(w
->ch
.class_name
,
11123 win
->ch
.class_name
) == 0 &&
11124 w
->ch
.instance_name
&&
11125 win
->ch
.instance_name
&&
11126 strcmp(w
->ch
.instance_name
,
11127 win
->ch
.instance_name
) == 0)
11133 win
->ws
->focus_pending
= get_focus_magic(win
);
11136 /* All windows need to be mapped if they are in the current workspace.*/
11139 /* Ignore EnterNotify to handle the mapnotify without interference. */
11140 if (focus_mode
== SWM_FOCUS_DEFAULT
)
11141 event_drain(XCB_ENTER_NOTIFY
);
11143 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
11147 motionnotify(xcb_motion_notify_event_t
*e
)
11149 struct swm_region
*r
= NULL
;
11150 int i
, num_screens
;
11152 last_event_time
= e
->time
;
11154 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): %#x "
11155 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
11156 "child: %#x, same_screen_focus: %s, state: %d\n",
11157 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
11158 get_notify_detail_label(e
->detail
), e
->detail
,
11159 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
11160 YESNO(e
->same_screen
), e
->state
);
11162 if (focus_mode
== SWM_FOCUS_MANUAL
)
11165 num_screens
= get_screen_count();
11166 for (i
= 0; i
< num_screens
; i
++)
11167 if (screens
[i
].root
== e
->root
)
11170 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11171 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
11172 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
11180 get_atom_name(xcb_atom_t atom
)
11183 #ifdef SWM_DEBUG_ATOM_NAMES
11185 * This should be disabled during most debugging since
11186 * xcb_get_* causes an xcb_flush.
11189 xcb_get_atom_name_reply_t
*r
;
11191 r
= xcb_get_atom_name_reply(conn
,
11192 xcb_get_atom_name(conn
, atom
),
11195 len
= xcb_get_atom_name_name_length(r
);
11197 name
= malloc(len
+ 1);
11199 memcpy(name
, xcb_get_atom_name_name(r
), len
);
11213 propertynotify(xcb_property_notify_event_t
*e
)
11215 struct ws_win
*win
;
11216 struct workspace
*ws
;
11220 name
= get_atom_name(e
->atom
);
11221 DNPRINTF(SWM_D_EVENT
, "propertynotify: win %#x, atom: %s(%u), "
11222 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
11226 last_event_time
= e
->time
;
11228 win
= find_window(e
->window
);
11234 if (e
->atom
== a_state
) {
11235 /* State just changed, make sure it gets focused if mapped. */
11236 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
11237 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
11239 win
->state
== SWM_WIN_STATE_REPARENTED
&&
11240 ws
->focus_pending
== win
) {
11241 focus_win(ws
->focus_pending
);
11242 ws
->focus_pending
= NULL
;
11246 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
11247 e
->atom
== XCB_ATOM_WM_NAME
) {
11249 bar_draw(ws
->r
->bar
);
11250 } else if (e
->atom
== a_prot
) {
11251 get_wm_protocols(win
);
11252 } else if (e
->atom
== XCB_ATOM_WM_NORMAL_HINTS
) {
11253 xcb_icccm_get_wm_normal_hints_reply(conn
,
11254 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
11262 reparentnotify(xcb_reparent_notify_event_t
*e
)
11264 struct ws_win
*win
;
11266 DNPRINTF(SWM_D_EVENT
, "reparentnotify: event: %#x, win %#x, "
11267 "parent: %#x, (x,y): (%u,%u), override_redirect: %u\n",
11268 e
->event
, e
->window
, e
->parent
, e
->x
, e
->y
, e
->override_redirect
);
11270 win
= find_window(e
->window
);
11272 if (win
->state
== SWM_WIN_STATE_REPARENTING
) {
11273 win
->state
= SWM_WIN_STATE_REPARENTED
;
11275 if (win
->ws
->r
&& !ICONIC(win
))
11280 update_window(win
);
11281 update_win_stacking(win
);
11282 } else if (win
->state
== SWM_WIN_STATE_UNPARENTING
) {
11283 win
->state
= SWM_WIN_STATE_UNPARENTED
;
11289 unmapnotify(xcb_unmap_notify_event_t
*e
)
11291 struct ws_win
*win
;
11292 struct workspace
*ws
;
11294 DNPRINTF(SWM_D_EVENT
, "unmapnotify: win %#x\n", e
->window
);
11296 /* If we aren't managing the window, then ignore. */
11297 win
= find_window(e
->window
);
11298 if (win
== NULL
|| win
->id
!= e
->window
) {
11299 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore unmanaged.\n");
11303 /* Do nothing if already withdrawn. */
11304 if (!win
->mapped
&& !ICONIC(win
)) {
11305 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore withdrawn.\n");
11310 win
->mapped
= false;
11312 /* Ignore if reparenting-related. */
11313 if (win
->state
!= SWM_WIN_STATE_REPARENTED
) {
11314 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore not reparented.\n");
11318 /* If win was focused, make sure to focus on something else. */
11319 if (win
== ws
->focus
) {
11320 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
11321 ws
->focus_pending
= get_focus_prev(win
);
11322 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
11323 "focus_pending: %#x\n",
11324 WINID(ws
->focus_pending
));
11332 DNPRINTF(SWM_D_EVENT
, "unmapnotify: iconify.\n");
11333 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
11336 DNPRINTF(SWM_D_EVENT
, "unmapnotify: withdraw.\n");
11337 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
11338 unmanage_window(win
);
11343 /* Update focus if ws is active. */
11344 if (WS_FOCUSED(ws
)) {
11345 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
11346 focus_win(get_pointer_win(ws
->r
->s
->root
));
11347 } else if (ws
->focus_pending
) {
11348 focus_win(ws
->focus_pending
);
11349 ws
->focus_pending
= NULL
;
11350 } else if (ws
->focus
== NULL
) {
11351 DNPRINTF(SWM_D_FOCUS
, "unmapnotify: set_input_focus: "
11352 "%#x, revert-to: parent, time: 0\n",
11354 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11355 ws
->r
->id
, XCB_CURRENT_TIME
);
11359 center_pointer(ws
->r
);
11361 DNPRINTF(SWM_D_EVENT
, "unmapnotify: done.\n");
11366 get_source_type_label(uint32_t type
)
11371 case EWMH_SOURCE_TYPE_NONE
:
11374 case EWMH_SOURCE_TYPE_NORMAL
:
11377 case EWMH_SOURCE_TYPE_OTHER
:
11389 clientmessage(xcb_client_message_event_t
*e
)
11391 struct ws_win
*win
;
11392 struct swm_region
*r
= NULL
;
11395 int num_screens
, i
;
11396 xcb_map_request_event_t mre
;
11400 name
= get_atom_name(e
->type
);
11401 DNPRINTF(SWM_D_EVENT
, "clientmessage: win %#x, atom: %s(%u)\n",
11402 e
->window
, name
, e
->type
);
11406 if (e
->type
== ewmh
[_NET_CURRENT_DESKTOP
].atom
) {
11407 num_screens
= get_screen_count();
11408 for (i
= 0; i
< num_screens
; i
++)
11409 if (screens
[i
].root
== e
->window
) {
11410 r
= screens
[i
].r_focus
;
11414 if (r
&& e
->data
.data32
[0] < (uint32_t)workspace_limit
) {
11415 a
.id
= e
->data
.data32
[0];
11416 switchws(NULL
, r
, &a
);
11421 } else if (e
->type
== ewmh
[_NET_REQUEST_FRAME_EXTENTS
].atom
) {
11422 DNPRINTF(SWM_D_EVENT
,
11423 "clientmessage: set _NET_FRAME_EXTENTS on window.\n");
11424 vals
[0] = vals
[1] = vals
[2] = vals
[3] = border_width
;
11425 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, e
->window
,
11426 a_net_frame_extents
, XCB_ATOM_CARDINAL
, 32, 4, vals
);
11431 win
= find_window(e
->window
);
11433 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11434 /* Manage the window with maprequest. */
11435 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
11436 "unmanaged window.\n");
11437 mre
.window
= e
->window
;
11443 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11444 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW, "
11445 "source_type: %s(%d)\n",
11446 get_source_type_label(e
->data
.data32
[0]),
11447 e
->data
.data32
[0]);
11450 * Allow focus changes that are a result of direct user
11451 * action and from applications that use the old EWMH spec.
11453 if (e
->data
.data32
[0] != EWMH_SOURCE_TYPE_NORMAL
||
11454 win
->quirks
& SWM_Q_OBEYAPPFOCUSREQ
) {
11455 if (WS_FOCUSED(win
->ws
))
11458 win
->ws
->focus_pending
= win
;
11460 } else if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
11461 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
11462 if (win
->can_delete
)
11463 client_msg(win
, a_delete
, 0);
11465 xcb_kill_client(conn
, win
->id
);
11466 } else if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
11467 DNPRINTF(SWM_D_EVENT
,
11468 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
11470 if (e
->data
.data32
[0] & (1<<8)) /* x */
11471 X(win
) = e
->data
.data32
[1];
11472 if (e
->data
.data32
[0] & (1<<9)) /* y */
11473 Y(win
) = e
->data
.data32
[2];
11474 if (e
->data
.data32
[0] & (1<<10)) /* width */
11475 WIDTH(win
) = e
->data
.data32
[3];
11476 if (e
->data
.data32
[0] & (1<<11)) /* height */
11477 HEIGHT(win
) = e
->data
.data32
[4];
11479 update_window(win
);
11481 /* Notify no change was made. */
11482 config_win(win
, NULL
);
11483 /* TODO: Change stack sizes */
11485 } else if (e
->type
== ewmh
[_NET_RESTACK_WINDOW
].atom
) {
11486 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_RESTACK_WINDOW\n");
11487 vals
[0] = e
->data
.data32
[1]; /* Sibling window. */
11488 vals
[1] = e
->data
.data32
[2]; /* Stack mode detail. */
11490 if (win
->frame
!= XCB_WINDOW_NONE
)
11491 xcb_configure_window(conn
, win
->frame
,
11492 XCB_CONFIG_WINDOW_SIBLING
|
11493 XCB_CONFIG_WINDOW_STACK_MODE
, vals
);
11494 } else if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
11495 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
11496 ewmh_change_wm_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
11497 if (e
->data
.data32
[2])
11498 ewmh_change_wm_state(win
, e
->data
.data32
[2],
11499 e
->data
.data32
[0]);
11501 ewmh_update_wm_state(win
);
11503 } else if (e
->type
== ewmh
[_NET_WM_DESKTOP
].atom
) {
11504 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_DESKTOP\n");
11507 win_to_ws(win
, e
->data
.data32
[0], true);
11509 /* Stack source and destination ws, if mapped. */
11510 if (r
!= win
->ws
->r
) {
11516 load_float_geom(win
);
11529 int errcode
= xcb_connection_has_error(conn
);
11530 #ifdef XCB_CONN_ERROR
11533 case XCB_CONN_ERROR
:
11534 s
= "Socket error, pipe error or other stream error.";
11536 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
11537 s
= "Extension not supported.";
11539 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
11540 s
= "Insufficient memory.";
11542 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
11543 s
= "Request length was exceeded.";
11545 case XCB_CONN_CLOSED_PARSE_ERR
:
11546 s
= "Error parsing display string.";
11549 s
= "Unknown error.";
11552 errx(errcode
, "X CONNECTION ERROR: %s", s
);
11555 errx(errcode
, "X CONNECTION ERROR");
11562 int num_screens
, i
;
11563 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
11564 XCB_EVENT_MASK_ENTER_WINDOW
|
11565 XCB_EVENT_MASK_OWNER_GRAB_BUTTON
|
11566 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
11567 XCB_EVENT_MASK_ENTER_WINDOW
|
11568 XCB_EVENT_MASK_LEAVE_WINDOW
|
11569 XCB_EVENT_MASK_BUTTON_PRESS
|
11570 XCB_EVENT_MASK_BUTTON_RELEASE
|
11571 XCB_EVENT_MASK_KEY_PRESS
|
11572 XCB_EVENT_MASK_KEY_RELEASE
|
11573 XCB_EVENT_MASK_PROPERTY_CHANGE
;
11575 xcb_void_cookie_t wac
;
11576 xcb_generic_error_t
*error
;
11578 /* this causes an error if some other window manager is running */
11579 num_screens
= get_screen_count();
11580 for (i
= 0; i
< num_screens
; i
++) {
11581 if ((sc
= get_screen(i
)) == NULL
)
11582 errx(1, "ERROR: can't get screen %d.", i
);
11583 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: %#x\n",
11585 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
11586 XCB_CW_EVENT_MASK
, &val
);
11587 if ((error
= xcb_request_check(conn
, wac
))) {
11588 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
11589 error
->error_code
);
11599 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
11601 struct swm_region
*r
= NULL
, *n
;
11602 struct workspace
*ws
= NULL
;
11606 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
11607 s
->idx
, w
, h
, x
, y
);
11609 /* remove any conflicting regions */
11610 n
= TAILQ_FIRST(&s
->rl
);
11613 n
= TAILQ_NEXT(r
, entry
);
11614 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
11615 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
11616 if (r
->ws
->r
!= NULL
)
11617 r
->ws
->old_r
= r
->ws
->r
;
11620 xcb_destroy_window(conn
, r
->id
);
11621 r
->id
= XCB_WINDOW_NONE
;
11622 TAILQ_REMOVE(&s
->rl
, r
, entry
);
11623 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
11627 /* search old regions for one to reuse */
11629 /* size + location match */
11630 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11631 if (X(r
) == x
&& Y(r
) == y
&&
11632 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11636 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11637 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11641 TAILQ_REMOVE(&s
->orl
, r
, entry
);
11642 /* try to use old region's workspace */
11643 if (r
->ws
->r
== NULL
)
11646 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
11647 err(1, "new_region: calloc: failed to allocate memory "
11650 /* if we don't have a workspace already, find one */
11652 for (i
= 0; i
< workspace_limit
; i
++)
11653 if (s
->ws
[i
].r
== NULL
) {
11660 errx(1, "new_region: no free workspaces");
11662 if (ws
->state
== SWM_WS_STATE_HIDDEN
)
11663 ws
->state
= SWM_WS_STATE_MAPPING
;
11672 r
->ws_prior
= NULL
;
11675 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
11677 /* Invisible region window to detect pointer events on empty regions. */
11678 r
->id
= xcb_generate_id(conn
);
11679 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
11680 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
11682 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
11683 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
11684 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
11686 /* Make sure region input is at the bottom. */
11687 wa
[0] = XCB_STACK_MODE_BELOW
;
11688 xcb_configure_window(conn
, r
->id
, XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
11690 xcb_map_window(conn
, r
->id
);
11694 scan_randr(int idx
)
11696 #ifdef SWM_XRR_HAS_CRTC
11699 #endif /* SWM_XRR_HAS_CRTC */
11700 struct swm_region
*r
;
11702 xcb_randr_get_screen_resources_current_cookie_t src
;
11703 xcb_randr_get_screen_resources_current_reply_t
*srr
;
11704 xcb_randr_get_crtc_info_cookie_t cic
;
11705 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
11706 xcb_randr_crtc_t
*crtc
;
11707 xcb_screen_t
*screen
;
11709 DNPRINTF(SWM_D_MISC
, "scan_randr: screen: %d\n", idx
);
11711 if ((screen
= get_screen(idx
)) == NULL
)
11712 errx(1, "ERROR: can't get screen %d.", idx
);
11714 num_screens
= get_screen_count();
11715 if (idx
>= num_screens
)
11716 errx(1, "scan_randr: invalid screen");
11718 /* remove any old regions */
11719 while ((r
= TAILQ_FIRST(&screens
[idx
].rl
)) != NULL
) {
11720 r
->ws
->old_r
= r
->ws
->r
= NULL
;
11722 xcb_destroy_window(conn
, r
->id
);
11723 r
->id
= XCB_WINDOW_NONE
;
11724 TAILQ_REMOVE(&screens
[idx
].rl
, r
, entry
);
11725 TAILQ_INSERT_TAIL(&screens
[idx
].orl
, r
, entry
);
11729 /* map virtual screens onto physical screens */
11730 #ifdef SWM_XRR_HAS_CRTC
11731 if (randr_support
) {
11732 src
= xcb_randr_get_screen_resources_current(conn
,
11733 screens
[idx
].root
);
11734 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
11737 new_region(&screens
[idx
], 0, 0,
11738 screen
->width_in_pixels
,
11739 screen
->height_in_pixels
);
11742 ncrtc
= srr
->num_crtcs
;
11744 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
11745 for (c
= 0; c
< ncrtc
; c
++) {
11746 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
11748 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
11751 if (cir
->num_outputs
== 0) {
11756 if (cir
->mode
== 0)
11757 new_region(&screens
[idx
], 0, 0,
11758 screen
->width_in_pixels
,
11759 screen
->height_in_pixels
);
11761 new_region(&screens
[idx
],
11762 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
11767 #endif /* SWM_XRR_HAS_CRTC */
11769 /* If detection failed, create a single region that spans the screen. */
11770 if (TAILQ_EMPTY(&screens
[idx
].rl
))
11771 new_region(&screens
[idx
], 0, 0, screen
->width_in_pixels
,
11772 screen
->height_in_pixels
);
11775 /* The screen shouldn't focus on unused regions. */
11776 TAILQ_FOREACH(r
, &screens
[idx
].orl
, entry
) {
11777 if (screens
[idx
].r_focus
== r
)
11778 screens
[idx
].r_focus
= NULL
;
11781 DNPRINTF(SWM_D_MISC
, "scan_randr: done.\n");
11785 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
11787 struct swm_region
*r
;
11788 struct workspace
*ws
;
11789 struct ws_win
*win
;
11790 int i
, j
, num_screens
;
11792 DNPRINTF(SWM_D_EVENT
, "screenchange: root: %#x\n", e
->root
);
11794 num_screens
= get_screen_count();
11795 /* silly event doesn't include the screen index */
11796 for (i
= 0; i
< num_screens
; i
++)
11797 if (screens
[i
].root
== e
->root
)
11799 if (i
>= num_screens
)
11800 errx(1, "screenchange: screen not found");
11802 /* brute force for now, just re-enumerate the regions */
11806 print_win_geom(e
->root
);
11808 /* add bars to all regions */
11809 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11812 /* Stack all regions. */
11813 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11816 /* Make sure a region has focus. */
11817 if (screens
[i
].r_focus
== NULL
) {
11818 r
= TAILQ_FIRST(&screens
[i
].rl
);
11823 /* Cleanup unused previously visible workspaces. */
11824 for (j
= 0; j
< workspace_limit
; j
++) {
11825 ws
= &screens
[i
].ws
[j
];
11826 if (ws
->r
== NULL
&& ws
->state
!= SWM_WS_STATE_HIDDEN
) {
11827 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
11829 ws
->state
= SWM_WS_STATE_HIDDEN
;
11835 /* Update workspace state and bar on all regions. */
11836 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11837 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
11845 struct swm_region
*r
= NULL
;
11846 xcb_query_tree_cookie_t qtc
;
11847 xcb_query_tree_reply_t
*qtr
;
11848 xcb_get_property_cookie_t pc
;
11849 xcb_get_property_reply_t
*pr
;
11850 xcb_window_t
*wins
= NULL
, trans
, *cwins
= NULL
;
11851 int i
, j
, k
, n
, no
, num_screens
;
11853 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
11854 num_screens
= get_screen_count();
11855 for (i
= 0; i
< num_screens
; i
++) {
11856 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
11857 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
11860 wins
= xcb_query_tree_children(qtr
);
11861 no
= xcb_query_tree_children_length(qtr
);
11863 /* Try to sort windows according to _NET_CLIENT_LIST. */
11864 pr
= xcb_get_property_reply(conn
, xcb_get_property(conn
, 0,
11865 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
11866 XCB_ATOM_WINDOW
, 0, UINT32_MAX
), NULL
);
11868 cwins
= xcb_get_property_value(pr
);
11869 n
= xcb_get_property_value_length(pr
) /
11870 sizeof(xcb_atom_t
);
11872 for (j
= 0; j
< n
; ++j
) {
11873 for (k
= j
; k
< no
; ++k
) {
11874 if (wins
[k
] == cwins
[j
]) {
11875 /* Swap wins j and k. */
11877 wins
[j
] = cwins
[j
];
11885 /* Manage top-level windows first, then transients. */
11886 /* TODO: allow transients to be managed before leader. */
11887 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top-level windows.\n");
11888 for (j
= 0; j
< no
; j
++) {
11889 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11890 if (r
->id
== wins
[j
]) {
11891 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11892 "skip %#x; region input window.\n",
11895 } else if (r
->bar
->id
== wins
[j
]) {
11896 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11897 "skip %#x; region bar.\n",
11906 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11907 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11909 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
11910 "is transient for %#x.\n", wins
[j
], trans
);
11914 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11917 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
11918 for (j
= 0; j
< no
; j
++) {
11919 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11920 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11922 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11926 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
11930 setup_screens(void)
11932 int i
, j
, k
, num_screens
;
11933 struct workspace
*ws
;
11934 uint32_t gcv
[1], wa
[1];
11935 const xcb_query_extension_reply_t
*qep
;
11936 xcb_screen_t
*screen
;
11937 xcb_randr_query_version_cookie_t c
;
11938 xcb_randr_query_version_reply_t
*r
;
11940 num_screens
= get_screen_count();
11941 if ((screens
= calloc(num_screens
, sizeof(struct swm_screen
))) == NULL
)
11942 err(1, "setup_screens: calloc: failed to allocate memory for "
11945 /* Initial RandR setup. */
11946 randr_support
= false;
11947 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
11948 if (qep
->present
) {
11949 c
= xcb_randr_query_version(conn
, 1, 1);
11950 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
11952 if (r
->major_version
>= 1) {
11953 randr_support
= true;
11954 randr_eventbase
= qep
->first_event
;
11960 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
11962 /* map physical screens */
11963 for (i
= 0; i
< num_screens
; i
++) {
11964 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
11965 screens
[i
].idx
= i
;
11966 screens
[i
].r_focus
= NULL
;
11968 TAILQ_INIT(&screens
[i
].rl
);
11969 TAILQ_INIT(&screens
[i
].orl
);
11970 if ((screen
= get_screen(i
)) == NULL
)
11971 errx(1, "ERROR: can't get screen %d.", i
);
11972 screens
[i
].root
= screen
->root
;
11974 /* set default colors */
11975 setscreencolor("red", i
, SWM_S_COLOR_FOCUS
);
11976 setscreencolor("rgb:88/88/88", i
, SWM_S_COLOR_UNFOCUS
);
11977 setscreencolor("rgb:00/80/80", i
, SWM_S_COLOR_BAR_BORDER
);
11978 setscreencolor("rgb:00/40/40", i
,
11979 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
11980 setscreencolor("black", i
, SWM_S_COLOR_BAR
);
11981 setscreencolor("rgb:a0/a0/a0", i
, SWM_S_COLOR_BAR_FONT
);
11982 setscreencolor("red", i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
11983 setscreencolor("rgb:88/88/88", i
,
11984 SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
11986 /* create graphics context on screen */
11987 screens
[i
].bar_gc
= xcb_generate_id(conn
);
11989 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
11990 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
11992 /* set default cursor */
11993 xcb_change_window_attributes(conn
, screens
[i
].root
,
11994 XCB_CW_CURSOR
, wa
);
11996 /* init all workspaces */
11997 /* XXX these should be dynamically allocated too */
11998 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
11999 ws
= &screens
[i
].ws
[j
];
12002 ws
->bar_enabled
= true;
12004 ws
->focus_prev
= NULL
;
12005 ws
->focus_pending
= NULL
;
12006 ws
->focus_raise
= NULL
;
12009 ws
->state
= SWM_WS_STATE_HIDDEN
;
12010 TAILQ_INIT(&ws
->stack
);
12011 TAILQ_INIT(&ws
->winlist
);
12012 TAILQ_INIT(&ws
->unmanagedlist
);
12014 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
12015 layouts
[k
].l_config(ws
, SWM_ARG_ID_STACKINIT
);
12016 ws
->cur_layout
= &layouts
[0];
12017 ws
->cur_layout
->l_string(ws
);
12023 xcb_randr_select_input(conn
, screens
[i
].root
,
12024 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
12029 setup_globals(void)
12031 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
12032 err(1, "setup_globals: strdup: failed to allocate memory.");
12034 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
12035 err(1, "setup_globals: strdup: failed to allocate memory.");
12037 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
12038 errx(1, "unable to allocate key symbols");
12040 a_state
= get_atom_from_string("WM_STATE");
12041 a_prot
= get_atom_from_string("WM_PROTOCOLS");
12042 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
12043 a_net_frame_extents
= get_atom_from_string("_NET_FRAME_EXTENTS");
12044 a_net_supported
= get_atom_from_string("_NET_SUPPORTED");
12045 a_net_wm_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
12046 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
12047 a_utf8_string
= get_atom_from_string("UTF8_STRING");
12048 a_swm_ws
= get_atom_from_string("_SWM_WS");
12052 shutdown_cleanup(void)
12054 struct swm_region
*r
;
12056 struct workspace
*ws
;
12057 int i
, num_screens
;
12059 /* disable alarm because the following code may not be interrupted */
12061 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
12062 err(1, "can't disable alarm");
12075 num_screens
= get_screen_count();
12076 for (i
= 0; i
< num_screens
; ++i
) {
12079 DNPRINTF(SWM_D_FOCUS
, "shutdown_cleanup: set_input_focus: "
12080 "%#x, revert-to: root, time: 0\n", screens
[i
].root
);
12081 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
12082 screens
[i
].root
, XCB_CURRENT_TIME
);
12084 if (screens
[i
].bar_gc
!= XCB_NONE
)
12085 xcb_free_gc(conn
, screens
[i
].bar_gc
);
12086 if (!bar_font_legacy
) {
12087 XftColorFree(display
, DefaultVisual(display
, i
),
12088 DefaultColormap(display
, i
), &bar_font_color
);
12089 XftColorFree(display
, DefaultVisual(display
, i
),
12090 DefaultColormap(display
, i
), &search_font_color
);
12093 for (j
= 0; j
< SWM_S_COLOR_MAX
; ++j
) {
12094 free(screens
[i
].c
[j
].name
);
12097 /* Free window memory. */
12098 for (j
= 0; j
< SWM_WS_MAX
; ++j
) {
12099 ws
= &screens
[i
].ws
[j
];
12102 while ((w
= TAILQ_FIRST(&ws
->winlist
)) != NULL
) {
12103 TAILQ_REMOVE(&ws
->winlist
, w
, entry
);
12107 while ((w
= TAILQ_FIRST(&ws
->unmanagedlist
)) != NULL
) {
12108 TAILQ_REMOVE(&ws
->unmanagedlist
, w
, entry
);
12113 /* Free region memory. */
12114 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
12115 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
12120 while ((r
= TAILQ_FIRST(&screens
[i
].orl
)) != NULL
) {
12121 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
12130 free(clock_format
);
12131 free(startup_exception
);
12134 XFreeFontSet(display
, bar_fs
);
12136 XftFontClose(display
, bar_font
);
12138 xcb_key_symbols_free(syms
);
12140 xcb_aux_sync(conn
);
12141 xcb_disconnect(conn
);
12145 event_error(xcb_generic_error_t
*e
)
12149 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
12150 "resource_id: %u, minor_code: %u\n",
12151 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
12152 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
12153 e
->sequence
, e
->resource_id
, e
->minor_code
);
12157 event_handle(xcb_generic_event_t
*evt
)
12159 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
12161 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
12162 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
12163 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
12166 #define EVENT(type, callback) case type: callback((void *)evt); return
12167 EVENT(0, event_error
);
12168 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
12169 EVENT(XCB_BUTTON_RELEASE
, buttonrelease
);
12170 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
12171 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
12172 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
12173 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
12174 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
12175 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
12176 /*EVENT(XCB_CREATE_NOTIFY, );*/
12177 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
12178 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
12179 EVENT(XCB_EXPOSE
, expose
);
12180 EVENT(XCB_FOCUS_IN
, focusin
);
12182 EVENT(XCB_FOCUS_OUT
, focusout
);
12184 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
12185 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
12186 EVENT(XCB_KEY_PRESS
, keypress
);
12187 EVENT(XCB_KEY_RELEASE
, keyrelease
);
12188 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
12190 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
12192 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
12193 EVENT(XCB_MAP_REQUEST
, maprequest
);
12194 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
12195 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
12196 /*EVENT(XCB_NO_EXPOSURE, );*/
12197 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
12198 EVENT(XCB_REPARENT_NOTIFY
, reparentnotify
);
12199 /*EVENT(XCB_RESIZE_REQUEST, );*/
12200 /*EVENT(XCB_SELECTION_CLEAR, );*/
12201 /*EVENT(XCB_SELECTION_NOTIFY, );*/
12202 /*EVENT(XCB_SELECTION_REQUEST, );*/
12203 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
12204 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
12207 if (type
- randr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
12208 screenchange((void *)evt
);
12212 main(int argc
, char *argv
[])
12214 struct pollfd pfd
[2];
12215 struct sigaction sact
;
12217 struct passwd
*pwd
;
12218 struct swm_region
*r
;
12219 xcb_generic_event_t
*evt
;
12220 int xfd
, i
, num_screens
, num_readable
;
12221 char conf
[PATH_MAX
], *cfile
= NULL
;
12222 bool stdin_ready
= false, startup
= true;
12224 /* suppress unused warning since var is needed */
12228 time_started
= time(NULL
);
12232 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
12233 if (setlocale(LC_CTYPE
, "") == NULL
|| setlocale(LC_TIME
, "") == NULL
)
12234 warnx("no locale support");
12236 /* handle some signals */
12237 bzero(&sact
, sizeof(sact
));
12238 sigemptyset(&sact
.sa_mask
);
12240 sact
.sa_handler
= sighdlr
;
12241 sigaction(SIGINT
, &sact
, NULL
);
12242 sigaction(SIGQUIT
, &sact
, NULL
);
12243 sigaction(SIGTERM
, &sact
, NULL
);
12244 sigaction(SIGHUP
, &sact
, NULL
);
12246 sact
.sa_handler
= sighdlr
;
12247 sact
.sa_flags
= SA_NOCLDSTOP
;
12248 sigaction(SIGCHLD
, &sact
, NULL
);
12250 if ((display
= XOpenDisplay(0)) == NULL
)
12251 errx(1, "can not open display");
12253 conn
= XGetXCBConnection(display
);
12254 if (xcb_connection_has_error(conn
))
12255 errx(1, "can not get XCB connection");
12257 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
12259 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
12260 xfd
= xcb_get_file_descriptor(conn
);
12262 /* look for local and global conf file */
12263 pwd
= getpwuid(getuid());
12265 errx(1, "invalid user: %d", getuid());
12267 xcb_grab_server(conn
);
12268 xcb_aux_sync(conn
);
12270 /* flush all events */
12271 while ((evt
= get_next_event(false))) {
12272 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
12278 errx(1, "another window manager is currently running");
12280 /* Load Xcursors and/or cursorfont glyph cursors. */
12283 xcb_aux_sync(conn
);
12288 setup_keybindings();
12289 setup_btnbindings();
12294 for (i
= 0; ; i
++) {
12299 snprintf(conf
, sizeof conf
, "%s/.%s",
12300 pwd
->pw_dir
, SWM_CONF_FILE
);
12304 snprintf(conf
, sizeof conf
, "/etc/%s",
12309 snprintf(conf
, sizeof conf
, "%s/.%s",
12310 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
12313 /* global compat */
12314 snprintf(conf
, sizeof conf
, "/etc/%s",
12315 SWM_CONF_FILE_OLD
);
12321 if (strlen(conf
) && stat(conf
, &sb
) != -1)
12322 if (S_ISREG(sb
.st_mode
)) {
12329 /* load conf (if any) */
12331 conf_load(cfile
, SWM_CONF_DEFAULT
);
12335 if (getenv("SWM_STARTED") == NULL
)
12336 setenv("SWM_STARTED", "YES", 1);
12338 /* setup all bars */
12339 num_screens
= get_screen_count();
12340 for (i
= 0; i
< num_screens
; i
++)
12341 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12344 /* Manage existing windows. */
12350 /* Stack all regions to trigger mapping. */
12351 for (i
= 0; i
< num_screens
; i
++)
12352 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12355 xcb_ungrab_server(conn
);
12358 /* Update state and bar of each newly mapped workspace. */
12359 for (i
= 0; i
< num_screens
; i
++)
12360 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
12361 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
12365 memset(&pfd
, 0, sizeof(pfd
));
12367 pfd
[0].events
= POLLIN
;
12368 pfd
[1].fd
= STDIN_FILENO
;
12369 pfd
[1].events
= POLLIN
;
12372 while ((evt
= get_next_event(false))) {
12379 /* If just (re)started, set default focus if needed. */
12383 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
12384 r
= TAILQ_FIRST(&screens
[0].rl
);
12396 num_readable
= poll(pfd
, bar_extra
? 2 : 1, 1000);
12397 if (num_readable
== -1) {
12398 DNPRINTF(SWM_D_MISC
, "poll failed: %s",
12400 } else if (num_readable
> 0 && bar_extra
&&
12401 pfd
[1].revents
& POLLIN
) {
12402 stdin_ready
= true;
12406 restart(NULL
, NULL
, NULL
);
12412 stdin_ready
= false;
12413 bar_extra_update();
12416 /* Need to ensure the bar(s) are always updated. */
12417 for (i
= 0; i
< num_screens
; i
++)
12418 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12424 shutdown_cleanup();