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) || FULLSCREEN(w) || \
254 /* Constrain Window flags */
255 #define SWM_CW_RESIZABLE (0x01)
256 #define SWM_CW_SOFTBOUNDARY (0x02)
257 #define SWM_CW_HARDBOUNDARY (0x04)
258 #define SWM_CW_RIGHT (0x10)
259 #define SWM_CW_LEFT (0x20)
260 #define SWM_CW_BOTTOM (0x40)
261 #define SWM_CW_TOP (0x80)
262 #define SWM_CW_ALLSIDES (0xf0)
264 #define SWM_FOCUS_DEFAULT (0)
265 #define SWM_FOCUS_FOLLOW (1)
266 #define SWM_FOCUS_MANUAL (2)
268 #define SWM_CK_ALL (0xf)
269 #define SWM_CK_FOCUS (0x1)
270 #define SWM_CK_POINTER (0x2)
271 #define SWM_CK_FALLBACK (0x4)
272 #define SWM_CK_REGION (0x8)
274 #define SWM_CONF_DEFAULT (0)
275 #define SWM_CONF_KEYMAPPING (1)
278 #define SWM_LIB "/usr/local/lib/libswmhack.so"
285 xcb_atom_t a_net_frame_extents
;
286 xcb_atom_t a_net_wm_check
;
287 xcb_atom_t a_net_supported
;
288 xcb_atom_t a_takefocus
;
289 xcb_atom_t a_utf8_string
;
291 volatile sig_atomic_t running
= 1;
292 volatile sig_atomic_t restart_wm
= 0;
293 xcb_timestamp_t last_event_time
= 0;
297 unsigned int numlockmask
= 0;
300 xcb_connection_t
*conn
;
301 xcb_key_symbols_t
*syms
;
303 int boundary_width
= 50;
304 bool cycle_empty
= false;
305 bool cycle_visible
= false;
307 int font_adjusted
= 0;
308 uint16_t mod_key
= MODKEY
;
309 bool warp_focus
= false;
310 bool warp_pointer
= false;
311 bool workspace_clamp
= false;
314 struct swm_region
*search_r
;
315 int select_list_pipe
[2];
316 int select_resp_pipe
[2];
318 volatile sig_atomic_t search_resp
;
319 int search_resp_action
;
321 struct search_window
{
322 TAILQ_ENTRY(search_window
) entry
;
326 xcb_window_t indicator
;
328 TAILQ_HEAD(search_winlist
, search_window
);
329 struct search_winlist search_wl
;
334 SWM_SEARCH_UNICONIFY
,
335 SWM_SEARCH_NAME_WORKSPACE
,
336 SWM_SEARCH_SEARCH_WORKSPACE
,
337 SWM_SEARCH_SEARCH_WINDOW
340 #define SWM_STACK_TOP (0)
341 #define SWM_STACK_BOTTOM (1)
342 #define SWM_STACK_ABOVE (2)
343 #define SWM_STACK_BELOW (3)
346 double dialog_ratio
= 0.6;
348 #define SWM_BAR_MAX (256)
349 #define SWM_BAR_JUSTIFY_LEFT (0)
350 #define SWM_BAR_JUSTIFY_CENTER (1)
351 #define SWM_BAR_JUSTIFY_RIGHT (2)
352 #define SWM_BAR_OFFSET (4)
353 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
354 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
355 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
356 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
357 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
359 #ifdef X_HAVE_UTF8_STRING
360 #define DRAWSTRING(x...) Xutf8DrawString(x)
362 #define DRAWSTRING(x...) XmbDrawString(x)
365 char *bar_argv
[] = { NULL
, NULL
};
367 char bar_ext
[SWM_BAR_MAX
];
368 char bar_ext_buf
[SWM_BAR_MAX
];
369 char bar_vertext
[SWM_BAR_MAX
];
370 bool bar_version
= false;
371 bool bar_enabled
= true;
372 int bar_border_width
= 1;
373 bool bar_at_bottom
= false;
374 bool bar_extra
= false;
376 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
377 char *bar_format
= NULL
;
378 bool stack_enabled
= true;
379 bool clock_enabled
= true;
380 bool iconic_enabled
= false;
381 bool maximize_hide_bar
= false;
382 bool urgent_enabled
= false;
383 bool urgent_collapse
= false;
384 char *clock_format
= NULL
;
385 bool window_class_enabled
= false;
386 bool window_instance_enabled
= false;
387 bool window_name_enabled
= false;
388 int focus_mode
= SWM_FOCUS_DEFAULT
;
389 int focus_close
= SWM_STACK_BELOW
;
390 bool focus_close_wrap
= true;
391 int focus_default
= SWM_STACK_TOP
;
392 int spawn_position
= SWM_STACK_TOP
;
393 bool disable_border
= false;
394 int border_width
= 1;
395 int region_padding
= 0;
397 bool java_workaround
= true;
398 bool verbose_layout
= false;
404 XFontSet bar_fs
= NULL
;
405 XFontSetExtents
*bar_fs_extents
;
406 XftFont
*bar_font
= NULL
;
407 bool bar_font_legacy
= true;
408 char *bar_fonts
= NULL
;
409 XftColor bar_font_color
;
410 XftColor search_font_color
;
411 char *startup_exception
= NULL
;
412 unsigned int nr_exceptions
= 0;
414 /* layout manager data */
415 struct swm_geometry
{
428 struct swm_geometry g
;
429 struct swm_region
*r
; /* Associated region. */
432 /* virtual "screens" */
434 TAILQ_ENTRY(swm_region
) entry
;
436 struct swm_geometry g
;
437 struct workspace
*ws
; /* current workspace on this region */
438 struct workspace
*ws_prior
; /* prior workspace on this region */
439 struct swm_screen
*s
; /* screen idx */
442 TAILQ_HEAD(swm_region_list
, swm_region
);
445 SWM_WIN_STATE_REPARENTING
,
446 SWM_WIN_STATE_REPARENTED
,
447 SWM_WIN_STATE_UNPARENTING
,
448 SWM_WIN_STATE_UNPARENTED
,
452 TAILQ_ENTRY(ws_win
) entry
;
453 TAILQ_ENTRY(ws_win
) stack_entry
;
456 xcb_window_t transient
;
457 struct ws_win
*focus_child
; /* focus on child transient */
458 struct swm_geometry g
; /* current geometry */
459 struct swm_geometry g_prev
; /* prev configured geometry */
460 struct swm_geometry g_float
; /* region coordinates */
461 bool g_floatvalid
; /* g_float geometry validity */
466 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
473 struct workspace
*ws
; /* always valid */
474 struct swm_screen
*s
; /* always valid, never changes */
476 xcb_icccm_get_wm_class_reply_t ch
;
477 xcb_icccm_wm_hints_t hints
;
482 TAILQ_HEAD(ws_win_list
, ws_win
);
483 TAILQ_HEAD(ws_win_stack
, ws_win
);
487 TAILQ_ENTRY(pid_e
) entry
;
491 TAILQ_HEAD(pid_list
, pid_e
);
492 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
494 /* layout handlers */
495 void stack(struct swm_region
*);
496 void vertical_config(struct workspace
*, int);
497 void vertical_stack(struct workspace
*, struct swm_geometry
*);
498 void horizontal_config(struct workspace
*, int);
499 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
500 void max_stack(struct workspace
*, struct swm_geometry
*);
501 void plain_stacker(struct workspace
*);
502 void fancy_stacker(struct workspace
*);
505 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
506 void (*l_config
)(struct workspace
*, int);
508 #define SWM_L_FOCUSPREV (1<<0)
509 #define SWM_L_MAPONFOCUS (1<<1)
510 void (*l_string
)(struct workspace
*);
512 /* stack, configure */
513 { vertical_stack
, vertical_config
, 0, plain_stacker
},
514 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
516 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
517 { NULL
, NULL
, 0, NULL
},
520 /* position of max_stack mode in the layouts array, index into layouts! */
521 #define SWM_V_STACK (0)
522 #define SWM_H_STACK (1)
523 #define SWM_MAX_STACK (2)
525 #define SWM_H_SLICE (32)
526 #define SWM_V_SLICE (32)
528 /* define work spaces */
530 int idx
; /* workspace index */
531 char *name
; /* workspace name */
532 bool always_raise
; /* raise windows on focus */
533 bool bar_enabled
; /* bar visibility */
534 struct layout
*cur_layout
; /* current layout handlers */
535 struct ws_win
*focus
; /* may be NULL */
536 struct ws_win
*focus_prev
; /* may be NULL */
537 struct ws_win
*focus_pending
; /* may be NULL */
538 struct ws_win
*focus_raise
; /* may be NULL */
539 struct swm_region
*r
; /* may be NULL */
540 struct swm_region
*old_r
; /* may be NULL */
541 struct ws_win_list winlist
; /* list of windows in ws */
542 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
543 struct ws_win_stack stack
; /* stacking order */
544 int state
; /* mapping state */
545 char stacker
[10]; /* display stacker and layout */
549 int horizontal_msize
;
551 int horizontal_stacks
;
552 bool horizontal_flip
;
562 SWM_WS_STATE_MAPPING
,
568 SWM_S_COLOR_BAR_BORDER
,
569 SWM_S_COLOR_BAR_BORDER_UNFOCUS
,
570 SWM_S_COLOR_BAR_FONT
,
572 SWM_S_COLOR_FOCUS_MAXIMIZED
,
574 SWM_S_COLOR_UNFOCUS_MAXIMIZED
,
578 /* physical screen mapping */
579 #define SWM_WS_MAX (22) /* hard limit */
580 int workspace_limit
= 10; /* soft limit */
583 int idx
; /* screen index */
584 struct swm_region_list rl
; /* list of regions on this screen */
585 struct swm_region_list orl
; /* list of old regions */
587 struct workspace ws
[SWM_WS_MAX
];
588 struct swm_region
*r_focus
;
595 } c
[SWM_S_COLOR_MAX
];
597 xcb_gcontext_t bar_gc
;
600 struct swm_screen
*screens
;
602 /* args to functions */
605 #define SWM_ARG_ID_FOCUSNEXT (0)
606 #define SWM_ARG_ID_FOCUSPREV (1)
607 #define SWM_ARG_ID_FOCUSMAIN (2)
608 #define SWM_ARG_ID_FOCUSURGENT (3)
609 #define SWM_ARG_ID_SWAPNEXT (10)
610 #define SWM_ARG_ID_SWAPPREV (11)
611 #define SWM_ARG_ID_SWAPMAIN (12)
612 #define SWM_ARG_ID_MOVELAST (13)
613 #define SWM_ARG_ID_MASTERSHRINK (20)
614 #define SWM_ARG_ID_MASTERGROW (21)
615 #define SWM_ARG_ID_MASTERADD (22)
616 #define SWM_ARG_ID_MASTERDEL (23)
617 #define SWM_ARG_ID_FLIPLAYOUT (24)
618 #define SWM_ARG_ID_STACKRESET (30)
619 #define SWM_ARG_ID_STACKINIT (31)
620 #define SWM_ARG_ID_STACKBALANCE (32)
621 #define SWM_ARG_ID_CYCLEWS_UP (40)
622 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
623 #define SWM_ARG_ID_CYCLERG_UP (42)
624 #define SWM_ARG_ID_CYCLERG_DOWN (43)
625 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
626 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
627 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
628 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
629 #define SWM_ARG_ID_STACKINC (50)
630 #define SWM_ARG_ID_STACKDEC (51)
631 #define SWM_ARG_ID_DONTCENTER (70)
632 #define SWM_ARG_ID_CENTER (71)
633 #define SWM_ARG_ID_KILLWINDOW (80)
634 #define SWM_ARG_ID_DELETEWINDOW (81)
635 #define SWM_ARG_ID_WIDTHGROW (90)
636 #define SWM_ARG_ID_WIDTHSHRINK (91)
637 #define SWM_ARG_ID_HEIGHTGROW (92)
638 #define SWM_ARG_ID_HEIGHTSHRINK (93)
639 #define SWM_ARG_ID_MOVEUP (100)
640 #define SWM_ARG_ID_MOVEDOWN (101)
641 #define SWM_ARG_ID_MOVELEFT (102)
642 #define SWM_ARG_ID_MOVERIGHT (103)
643 #define SWM_ARG_ID_BAR_TOGGLE (110)
644 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
645 #define SWM_ARG_ID_CYCLERG_MOVE_UP (112)
646 #define SWM_ARG_ID_CYCLERG_MOVE_DOWN (113)
652 TAILQ_ENTRY(quirk
) entry
;
653 char *class; /* WM_CLASS:class */
654 char *instance
; /* WM_CLASS:instance */
655 char *name
; /* WM_NAME */
657 regex_t regex_instance
;
660 int ws
; /* Initial workspace. */
661 #define SWM_Q_FLOAT (1<<0) /* Float this window. */
662 #define SWM_Q_TRANSSZ (1<<1) /* Transient window size too small. */
663 #define SWM_Q_ANYWHERE (1<<2) /* Don't position this window */
664 #define SWM_Q_XTERM_FONTADJ (1<<3) /* Adjust xterm fonts when resizing. */
665 #define SWM_Q_FULLSCREEN (1<<4) /* Remove border when fullscreen. */
666 #define SWM_Q_FOCUSPREV (1<<5) /* Focus on caller. */
667 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
668 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
669 #define SWM_Q_OBEYAPPFOCUSREQ (1<<8) /* Focus when applications ask. */
670 #define SWM_Q_IGNOREPID (1<<9) /* Ignore PID when determining ws. */
671 #define SWM_Q_IGNORESPAWNWS (1<<10) /* Ignore _SWM_WS when managing win. */
672 #define SWM_Q_NOFOCUSCYCLE (1<<11) /* Remove from normal focus cycle. */
673 #define SWM_Q_MINIMALBORDER (1<<12) /* No border when floating/unfocused. */
675 TAILQ_HEAD(quirk_list
, quirk
);
676 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
679 * Supported EWMH hints should be added to
680 * both the enum and the ewmh array
686 _NET_CURRENT_DESKTOP
,
687 _NET_DESKTOP_GEOMETRY
,
689 _NET_DESKTOP_VIEWPORT
,
690 _NET_MOVERESIZE_WINDOW
,
691 _NET_NUMBER_OF_DESKTOPS
,
692 _NET_REQUEST_FRAME_EXTENTS
,
694 _NET_WM_ACTION_ABOVE
,
695 _NET_WM_ACTION_CLOSE
,
696 _NET_WM_ACTION_FULLSCREEN
,
698 _NET_WM_ACTION_RESIZE
,
699 _NET_WM_ALLOWED_ACTIONS
,
701 _NET_WM_FULL_PLACEMENT
,
705 _NET_WM_STATE_FULLSCREEN
,
706 _NET_WM_STATE_HIDDEN
,
707 _NET_WM_STATE_MAXIMIZED_VERT
,
708 _NET_WM_STATE_MAXIMIZED_HORZ
,
709 _NET_WM_STATE_SKIP_PAGER
,
710 _NET_WM_STATE_SKIP_TASKBAR
,
712 _NET_WM_WINDOW_TYPE_DESKTOP
,
713 _NET_WM_WINDOW_TYPE_DIALOG
,
714 _NET_WM_WINDOW_TYPE_DOCK
,
715 _NET_WM_WINDOW_TYPE_NORMAL
,
716 _NET_WM_WINDOW_TYPE_SPLASH
,
717 _NET_WM_WINDOW_TYPE_TOOLBAR
,
718 _NET_WM_WINDOW_TYPE_UTILITY
,
719 _SWM_WM_STATE_MANUAL
,
726 } ewmh
[SWM_EWMH_HINT_MAX
] = {
727 /* must be in same order as in the enum */
728 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
729 {"_NET_CLIENT_LIST", XCB_ATOM_NONE
},
730 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
731 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE
},
732 {"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE
},
733 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE
},
734 {"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE
},
735 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
736 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE
},
737 {"_NET_REQUEST_FRAME_EXTENTS", XCB_ATOM_NONE
},
738 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE
},
739 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
740 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
741 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
742 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
743 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
744 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
745 {"_NET_WM_DESKTOP", XCB_ATOM_NONE
},
746 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE
},
747 {"_NET_WM_NAME", XCB_ATOM_NONE
},
748 {"_NET_WM_STATE", XCB_ATOM_NONE
},
749 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
750 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
751 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
752 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
753 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
754 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
755 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
756 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
757 {"_NET_WM_WINDOW_TYPE_DESKTOP", XCB_ATOM_NONE
},
758 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
759 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
760 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
761 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
762 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
763 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
764 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
767 /* EWMH source type */
769 EWMH_SOURCE_TYPE_NONE
= 0,
770 EWMH_SOURCE_TYPE_NORMAL
= 1,
771 EWMH_SOURCE_TYPE_OTHER
= 2,
778 XC_BOTTOM_LEFT_CORNER
,
779 XC_BOTTOM_RIGHT_CORNER
,
787 char *name
; /* Name used by Xcursor .*/
788 uint8_t cf_char
; /* cursorfont index. */
790 } cursors
[XC_MAX
] = {
791 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
792 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
793 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
794 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
795 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
796 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
797 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
800 #define SWM_SPAWN_OPTIONAL 0x1
804 TAILQ_ENTRY(spawn_prog
) entry
;
810 TAILQ_HEAD(spawn_list
, spawn_prog
);
811 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
817 /* User callable function IDs. */
830 FN_FULLSCREEN_TOGGLE
,
944 /* SWM_DEBUG actions MUST be here: */
957 BINDING_F_REPLAY
= 0x1,
961 RB_ENTRY(binding
) entry
;
962 uint16_t mod
; /* Modifier Mask. */
963 enum binding_type type
; /* Key or Button. */
964 uint32_t value
; /* KeySym or Button Index. */
965 enum actionid action
; /* Action Identifier. */
969 RB_HEAD(binding_tree
, binding
);
971 /* function prototypes */
972 void adjust_font(struct ws_win
*);
973 char *argsep(char **);
974 void bar_cleanup(struct swm_region
*);
975 void bar_extra_setup(void);
976 void bar_extra_stop(void);
977 int bar_extra_update(void);
978 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
979 void bar_fmt_expand(char *, size_t);
980 void bar_draw(struct swm_bar
*);
981 void bar_print(struct swm_region
*, const char *);
982 void bar_print_legacy(struct swm_region
*, const char *);
983 void bar_replace(char *, char *, struct swm_region
*, size_t);
984 void bar_replace_pad(char *, int *, size_t);
985 char *bar_replace_seq(char *, char *, struct swm_region
*, size_t *, size_t);
986 void bar_setup(struct swm_region
*);
987 void bar_toggle(struct binding
*, struct swm_region
*, union arg
*);
988 void bar_urgent(char *, size_t);
989 void bar_window_class(char *, size_t, struct swm_region
*);
990 void bar_window_class_instance(char *, size_t, struct swm_region
*);
991 void bar_window_instance(char *, size_t, struct swm_region
*);
992 void bar_window_name(char *, size_t, struct swm_region
*);
993 void bar_window_state(char *, size_t, struct swm_region
*);
994 void bar_workspace_name(char *, size_t, struct swm_region
*);
995 int binding_cmp(struct binding
*, struct binding
*);
996 void binding_insert(uint16_t, enum binding_type
, uint32_t, enum actionid
,
997 uint32_t, const char *);
998 struct binding
*binding_lookup(uint16_t, enum binding_type
, uint32_t);
999 void binding_remove(struct binding
*);
1000 void buttonpress(xcb_button_press_event_t
*);
1001 void buttonrelease(xcb_button_release_event_t
*);
1002 void center_pointer(struct swm_region
*);
1003 void check_conn(void);
1004 void clear_bindings(void);
1005 void clear_keybindings(void);
1006 int clear_maximized(struct workspace
*);
1007 void clear_quirks(void);
1008 void clear_spawns(void);
1009 void clientmessage(xcb_client_message_event_t
*);
1010 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
1011 int conf_load(const char *, int);
1012 void configurenotify(xcb_configure_notify_event_t
*);
1013 void configurerequest(xcb_configure_request_event_t
*);
1014 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
1015 void constrain_window(struct ws_win
*, struct swm_geometry
*, int *);
1016 int count_win(struct workspace
*, bool);
1017 void cursors_cleanup(void);
1018 void cursors_load(void);
1019 void custom_region(const char *);
1020 void cycle_layout(struct binding
*, struct swm_region
*, union arg
*);
1021 void cyclerg(struct binding
*, struct swm_region
*, union arg
*);
1022 void cyclews(struct binding
*, struct swm_region
*, union arg
*);
1024 void debug_refresh(struct ws_win
*);
1026 void debug_toggle(struct binding
*, struct swm_region
*, union arg
*);
1027 void destroynotify(xcb_destroy_notify_event_t
*);
1028 void dumpwins(struct binding
*, struct swm_region
*, union arg
*);
1029 int enable_wm(void);
1030 void enternotify(xcb_enter_notify_event_t
*);
1031 void event_drain(uint8_t);
1032 void event_error(xcb_generic_error_t
*);
1033 void event_handle(xcb_generic_event_t
*);
1034 void ewmh_apply_flags(struct ws_win
*, uint32_t);
1035 void ewmh_autoquirk(struct ws_win
*);
1036 void ewmh_get_desktop_names(void);
1037 void ewmh_get_wm_state(struct ws_win
*);
1038 int ewmh_handle_special_types(xcb_window_t
, struct swm_region
*);
1039 void ewmh_update_actions(struct ws_win
*);
1040 void ewmh_update_client_list(void);
1041 void ewmh_update_current_desktop(void);
1042 void ewmh_update_desktop_names(void);
1043 void ewmh_update_desktops(void);
1044 void ewmh_change_wm_state(struct ws_win
*, xcb_atom_t
, long);
1045 void ewmh_update_wm_state(struct ws_win
*);
1046 char *expand_tilde(const char *);
1047 void expose(xcb_expose_event_t
*);
1048 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
1049 struct swm_bar
*find_bar(xcb_window_t
);
1050 struct ws_win
*find_frame_window(xcb_window_t
);
1051 struct pid_e
*find_pid(pid_t
);
1052 struct swm_region
*find_region(xcb_window_t
);
1053 struct ws_win
*find_unmanaged_window(xcb_window_t
);
1054 struct ws_win
*find_window(xcb_window_t
);
1055 void floating_toggle(struct binding
*, struct swm_region
*, union arg
*);
1056 void focus(struct binding
*, struct swm_region
*, union arg
*);
1057 void focus_flush(void);
1058 void focus_pointer(struct binding
*, struct swm_region
*, union arg
*);
1059 void focus_region(struct swm_region
*);
1060 void focus_win(struct ws_win
*);
1061 void focusin(xcb_focus_in_event_t
*);
1063 void focusout(xcb_focus_out_event_t
*);
1065 void focusrg(struct binding
*, struct swm_region
*, union arg
*);
1066 void fontset_init(void);
1067 void free_window(struct ws_win
*);
1068 void fullscreen_toggle(struct binding
*, struct swm_region
*, union arg
*);
1069 xcb_atom_t
get_atom_from_string(const char *);
1071 char *get_atom_name(xcb_atom_t
);
1073 struct ws_win
*get_focus_magic(struct ws_win
*);
1074 struct ws_win
*get_focus_prev(struct ws_win
*);
1075 xcb_generic_event_t
*get_next_event(bool);
1077 char *get_notify_detail_label(uint8_t);
1078 char *get_notify_mode_label(uint8_t);
1080 struct ws_win
*get_pointer_win(xcb_window_t
);
1081 struct ws_win
*get_region_focus(struct swm_region
*);
1082 int get_region_index(struct swm_region
*);
1083 xcb_screen_t
*get_screen(int);
1084 int get_screen_count(void);
1086 char *get_source_type_label(uint32_t);
1087 char *get_stack_mode_name(uint8_t);
1088 char *get_state_mask_label(uint16_t);
1090 int32_t get_swm_ws(xcb_window_t
);
1091 bool get_urgent(struct ws_win
*);
1093 char *get_win_input_model(struct ws_win
*);
1095 char *get_win_name(xcb_window_t
);
1096 uint8_t get_win_state(xcb_window_t
);
1097 void get_wm_protocols(struct ws_win
*);
1098 int get_ws_idx(struct ws_win
*);
1099 void grab_windows(void);
1100 void grabbuttons(void);
1101 void grabkeys(void);
1102 void iconify(struct binding
*, struct swm_region
*, union arg
*);
1103 bool isxlfd(char *);
1104 bool keybindreleased(struct binding
*, xcb_key_release_event_t
*);
1105 void keypress(xcb_key_press_event_t
*);
1106 void keyrelease(xcb_key_release_event_t
*);
1107 bool keyrepeating(xcb_key_release_event_t
*);
1108 void kill_bar_extra_atexit(void);
1109 void kill_refs(struct ws_win
*);
1111 void leavenotify(xcb_leave_notify_event_t
*);
1113 void load_float_geom(struct ws_win
*);
1114 void lower_window(struct ws_win
*);
1115 struct ws_win
*manage_window(xcb_window_t
, int, bool);
1116 void map_window(struct ws_win
*);
1117 void mapnotify(xcb_map_notify_event_t
*);
1118 void mappingnotify(xcb_mapping_notify_event_t
*);
1119 void maprequest(xcb_map_request_event_t
*);
1120 void maximize_toggle(struct binding
*, struct swm_region
*, union arg
*);
1121 void motionnotify(xcb_motion_notify_event_t
*);
1122 void move(struct binding
*, struct swm_region
*, union arg
*);
1123 void move_win(struct ws_win
*, struct binding
*, int);
1124 uint32_t name_to_pixel(int, const char *);
1125 void name_workspace(struct binding
*, struct swm_region
*, union arg
*);
1126 void new_region(struct swm_screen
*, int, int, int, int);
1127 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1128 int parsebinding(const char *, uint16_t *, enum binding_type
*, uint32_t *,
1130 int parsequirks(const char *, uint32_t *, int *);
1131 void pressbutton(struct binding
*, struct swm_region
*, union arg
*);
1132 void priorws(struct binding
*, struct swm_region
*, union arg
*);
1134 void print_win_geom(xcb_window_t
);
1136 void propertynotify(xcb_property_notify_event_t
*);
1137 void put_back_event(xcb_generic_event_t
*);
1138 void quirk_free(struct quirk
*);
1139 void quirk_insert(const char *, const char *, const char *, uint32_t, int);
1140 void quirk_remove(struct quirk
*);
1141 void quirk_replace(struct quirk
*, const char *, const char *, const char *,
1143 void quit(struct binding
*, struct swm_region
*, union arg
*);
1144 void raise_focus(struct binding
*, struct swm_region
*, union arg
*);
1145 void raise_toggle(struct binding
*, struct swm_region
*, union arg
*);
1146 void raise_window(struct ws_win
*);
1147 void region_containment(struct ws_win
*, struct swm_region
*, int);
1148 struct swm_region
*region_under(struct swm_screen
*, int, int);
1149 void regionize(struct ws_win
*, int, int);
1150 void reparent_window(struct ws_win
*);
1151 void reparentnotify(xcb_reparent_notify_event_t
*);
1152 void resize(struct binding
*, struct swm_region
*, union arg
*);
1153 void resize_win(struct ws_win
*, struct binding
*, int);
1154 void restart(struct binding
*, struct swm_region
*, union arg
*);
1155 struct swm_region
*root_to_region(xcb_window_t
, int);
1156 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1157 void scan_randr(int);
1158 void search_do_resp(void);
1159 void search_resp_name_workspace(const char *, size_t);
1160 void search_resp_search_window(const char *);
1161 void search_resp_search_workspace(const char *);
1162 void search_resp_uniconify(const char *, size_t);
1163 void search_win(struct binding
*, struct swm_region
*, union arg
*);
1164 void search_win_cleanup(void);
1165 void search_workspace(struct binding
*, struct swm_region
*, union arg
*);
1166 void send_to_rg(struct binding
*, struct swm_region
*, union arg
*);
1167 void send_to_rg_relative(struct binding
*, struct swm_region
*, union arg
*);
1168 void send_to_ws(struct binding
*, struct swm_region
*, union arg
*);
1169 void set_region(struct swm_region
*);
1170 int setautorun(const char *, const char *, int);
1171 void setbinding(uint16_t, enum binding_type
, uint32_t, enum actionid
,
1172 uint32_t, const char *);
1173 int setconfbinding(const char *, const char *, int);
1174 int setconfcolor(const char *, const char *, int);
1175 int setconfmodkey(const char *, const char *, int);
1176 int setconfquirk(const char *, const char *, int);
1177 int setconfregion(const char *, const char *, int);
1178 int setconfspawn(const char *, const char *, int);
1179 int setconfvalue(const char *, const char *, int);
1180 int setkeymapping(const char *, const char *, int);
1181 int setlayout(const char *, const char *, int);
1182 void setquirk(const char *, const char *, const char *, uint32_t, int);
1183 void setscreencolor(const char *, int, int);
1184 void setspawn(const char *, const char *, int);
1185 void setup_btnbindings(void);
1186 void setup_ewmh(void);
1187 void setup_globals(void);
1188 void setup_keybindings(void);
1189 void setup_quirks(void);
1190 void setup_screens(void);
1191 void setup_spawn(void);
1192 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1193 void set_win_state(struct ws_win
*, uint8_t);
1194 void shutdown_cleanup(void);
1196 void socket_setnonblock(int);
1197 void sort_windows(struct ws_win_list
*);
1198 void spawn(int, union arg
*, bool);
1199 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1200 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1201 void spawn_insert(const char *, const char *, int);
1202 struct spawn_prog
*spawn_find(const char *);
1203 void spawn_remove(struct spawn_prog
*);
1204 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1205 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1206 void stack_config(struct binding
*, struct swm_region
*, union arg
*);
1207 void stack_master(struct workspace
*, struct swm_geometry
*, int, bool);
1208 void store_float_geom(struct ws_win
*);
1209 char *strdupsafe(const char *);
1210 void swapwin(struct binding
*, struct swm_region
*, union arg
*);
1211 void switchws(struct binding
*, struct swm_region
*, union arg
*);
1212 void teardown_ewmh(void);
1213 void unescape_selector(char *);
1214 void unfocus_win(struct ws_win
*);
1215 void uniconify(struct binding
*, struct swm_region
*, union arg
*);
1216 void unmanage_window(struct ws_win
*);
1217 void unmap_all(void);
1218 void unmap_window(struct ws_win
*);
1219 void unmapnotify(xcb_unmap_notify_event_t
*);
1220 void unparent_window(struct ws_win
*);
1221 void update_floater(struct ws_win
*);
1222 void update_modkey(uint16_t);
1223 void update_win_stacking(struct ws_win
*);
1224 void update_window(struct ws_win
*);
1225 void draw_frame(struct ws_win
*);
1226 void update_wm_state(struct ws_win
*win
);
1227 void updatenumlockmask(void);
1228 void validate_spawns(void);
1229 int validate_win(struct ws_win
*);
1230 int validate_ws(struct workspace
*);
1231 void version(struct binding
*, struct swm_region
*, union arg
*);
1232 void win_to_ws(struct ws_win
*, int, bool);
1233 pid_t
window_get_pid(xcb_window_t
);
1234 void wkill(struct binding
*, struct swm_region
*, union arg
*);
1235 void update_ws_stack(struct workspace
*);
1236 void xft_init(struct swm_region
*);
1237 void _add_startup_exception(const char *, va_list);
1238 void add_startup_exception(const char *, ...);
1240 RB_PROTOTYPE(binding_tree
, binding
, entry
, binding_cmp
);
1241 RB_GENERATE(binding_tree
, binding
, entry
, binding_cmp
);
1242 struct binding_tree bindings
;
1247 xcb_font_t cf
= XCB_NONE
;
1250 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1251 /* try to load Xcursor first. */
1252 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1255 /* fallback to cursorfont. */
1256 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1257 if (cf
== XCB_NONE
) {
1258 cf
= xcb_generate_id(conn
);
1259 xcb_open_font(conn
, cf
, strlen("cursor"),
1263 cursors
[i
].cid
= xcb_generate_id(conn
);
1264 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1265 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1266 0xffff, 0xffff, 0xffff);
1272 xcb_close_font(conn
, cf
);
1276 cursors_cleanup(void)
1279 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1280 xcb_free_cursor(conn
, cursors
[i
].cid
);
1284 expand_tilde(const char *s
)
1286 struct passwd
*ppwd
;
1294 errx(1, "expand_tilde: NULL string.");
1297 result
= strdup(sc
);
1303 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1304 errx(1, "expand_tilde: sysconf");
1306 if ((user
= calloc(1, max
+ 1)) == NULL
)
1307 errx(1, "expand_tilde: calloc");
1309 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1314 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1318 result
= strdup(sc
);
1320 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1324 errx(1, "expand_tilde: failed to allocate memory.");
1330 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1332 unsigned int tmpr
, tmpg
, tmpb
;
1334 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1337 *rr
= RGB_8_TO_16(tmpr
);
1338 *gg
= RGB_8_TO_16(tmpg
);
1339 *bb
= RGB_8_TO_16(tmpb
);
1345 get_screen(int screen
)
1347 const xcb_setup_t
*r
;
1348 xcb_screen_iterator_t iter
;
1350 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1351 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1355 iter
= xcb_setup_roots_iterator(r
);
1356 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1364 get_screen_count(void)
1366 const xcb_setup_t
*r
;
1368 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1369 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1373 return xcb_setup_roots_length(r
);
1377 get_region_index(struct swm_region
*r
)
1379 struct swm_region
*rr
;
1385 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1400 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1401 event_drain(XCB_ENTER_NOTIFY
);
1407 get_atom_from_string(const char *str
)
1409 xcb_intern_atom_cookie_t c
;
1410 xcb_intern_atom_reply_t
*r
;
1413 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1414 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1422 return (XCB_ATOM_NONE
);
1426 get_wm_protocols(struct ws_win
*win
) {
1428 xcb_icccm_get_wm_protocols_reply_t wpr
;
1430 if (xcb_icccm_get_wm_protocols_reply(conn
,
1431 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
1433 for (i
= 0; i
< (int)wpr
.atoms_len
; i
++) {
1434 if (wpr
.atoms
[i
] == a_takefocus
)
1435 win
->take_focus
= true;
1436 if (wpr
.atoms
[i
] == a_delete
)
1437 win
->can_delete
= true;
1439 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
1446 xcb_window_t root
, win
;
1447 int i
, j
, num_screens
;
1449 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1450 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1452 num_screens
= get_screen_count();
1453 for (i
= 0; i
< num_screens
; i
++) {
1454 root
= screens
[i
].root
;
1456 /* Set up _NET_SUPPORTING_WM_CHECK. */
1457 win
= xcb_generate_id(conn
);
1458 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
1459 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1460 XCB_COPY_FROM_PARENT
, 0, NULL
);
1462 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
1463 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1464 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1465 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1467 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1468 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1469 8, strlen("spectrwm"), "spectrwm");
1471 /* Report supported atoms */
1472 xcb_delete_property(conn
, root
, a_net_supported
);
1473 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1474 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, root
,
1475 a_net_supported
, XCB_ATOM_ATOM
, 32, 1,
1480 ewmh_update_desktops();
1481 ewmh_get_desktop_names();
1489 xcb_get_property_cookie_t pc
;
1490 xcb_get_property_reply_t
*pr
;
1492 num_screens
= get_screen_count();
1494 for (i
= 0; i
< num_screens
; i
++) {
1495 /* Get the support check window and destroy it */
1496 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, a_net_wm_check
,
1497 XCB_ATOM_WINDOW
, 0, 1);
1498 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1501 if (pr
->format
== a_net_wm_check
) {
1502 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1504 xcb_destroy_window(conn
, id
);
1505 xcb_delete_property(conn
, screens
[i
].root
,
1507 xcb_delete_property(conn
, screens
[i
].root
,
1515 ewmh_handle_special_types(xcb_window_t id
, struct swm_region
*region
)
1517 xcb_get_property_reply_t
*r
;
1518 xcb_get_property_cookie_t c
;
1521 uint16_t configure_mask
= 0;
1524 c
= xcb_get_property(conn
, 0, id
,
1525 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1526 r
= xcb_get_property_reply(conn
, c
, NULL
);
1530 type
= xcb_get_property_value(r
);
1531 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1533 for (i
= 0; i
< n
; i
++) {
1534 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DESKTOP
].atom
) {
1535 configure_mask
= XCB_CONFIG_WINDOW_STACK_MODE
|
1536 XCB_CONFIG_WINDOW_SIBLING
;
1538 wa
[1] = XCB_STACK_MODE_ABOVE
;
1542 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
) {
1543 configure_mask
= XCB_CONFIG_WINDOW_STACK_MODE
;
1544 wa
[0] = XCB_STACK_MODE_ABOVE
;
1550 if (configure_mask
!= 0) {
1551 xcb_map_window(conn
, id
);
1552 xcb_configure_window (conn
, id
, configure_mask
, wa
);
1560 ewmh_autoquirk(struct ws_win
*win
)
1562 xcb_get_property_reply_t
*r
;
1563 xcb_get_property_cookie_t c
;
1567 c
= xcb_get_property(conn
, 0, win
->id
,
1568 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1569 r
= xcb_get_property_reply(conn
, c
, NULL
);
1573 type
= xcb_get_property_value(r
);
1574 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1576 for (i
= 0; i
< n
; i
++) {
1577 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1579 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1580 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1581 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1584 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1585 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1586 win
->quirks
= SWM_Q_FLOAT
;
1594 ewmh_update_actions(struct ws_win
*win
)
1596 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1602 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1605 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1606 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1607 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1610 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1611 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1614 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1615 #define _NET_WM_STATE_ADD 1 /* add/set property */
1616 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1619 ewmh_change_wm_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1626 name
= get_atom_name(state
);
1627 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: win %#x, state: %s, "
1628 "action: %ld\n", WINID(win
), name
, action
);
1634 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1635 flag
= EWMH_F_FULLSCREEN
;
1636 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1637 flag
= EWMH_F_ABOVE
;
1638 else if (state
== ewmh
[_NET_WM_STATE_HIDDEN
].atom
)
1639 flag
= EWMH_F_HIDDEN
;
1640 else if (state
== ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
||
1641 state
== ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
)
1642 flag
= EWMH_F_MAXIMIZED
;
1643 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1644 flag
= SWM_F_MANUAL
;
1645 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1646 flag
= EWMH_F_SKIP_PAGER
;
1647 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1648 flag
= EWMH_F_SKIP_TASKBAR
;
1650 /* Disallow unfloating transients. */
1651 if (TRANS(win
) && flag
== EWMH_F_ABOVE
)
1654 new_flags
= win
->ewmh_flags
;
1657 case _NET_WM_STATE_REMOVE
:
1660 case _NET_WM_STATE_ADD
:
1663 case _NET_WM_STATE_TOGGLE
:
1668 ewmh_apply_flags(win
, new_flags
);
1671 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: done.\n");
1675 ewmh_apply_flags(struct ws_win
*win
, uint32_t pending
)
1677 struct workspace
*ws
;
1680 changed
= win
->ewmh_flags
^ pending
;
1684 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: pending: %d\n", pending
);
1686 win
->ewmh_flags
= pending
;
1689 if (changed
& EWMH_F_HIDDEN
) {
1691 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1692 ws
->focus_pending
= get_focus_prev(win
);
1697 /* Reload floating geometry in case region changed. */
1699 load_float_geom(win
);
1701 /* The window is no longer iconic, prepare focus. */
1702 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1703 ws
->focus_pending
= get_focus_magic(win
);
1708 if (changed
& EWMH_F_ABOVE
) {
1709 if (ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1711 load_float_geom(win
);
1712 else if (!MAXIMIZED(win
))
1713 store_float_geom(win
);
1715 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1716 changed
&= ~EWMH_F_MAXIMIZED
;
1720 win
->ewmh_flags
^= EWMH_F_ABOVE
& pending
;
1724 if (changed
& EWMH_F_MAXIMIZED
) {
1725 /* VERT and/or HORZ changed. */
1727 if (!MAXIMIZED(win
))
1728 load_float_geom(win
);
1730 store_float_geom(win
);
1733 if (MAXIMIZED(win
)) {
1734 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
1735 ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1739 ws
->focus_pending
= win
;
1747 if (changed
& EWMH_F_FULLSCREEN
) {
1748 if (FULLSCREEN(win
)) {
1749 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
1753 ws
->focus_pending
= win
;
1756 load_float_geom(win
);
1759 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1763 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: done.\n");
1767 ewmh_update_wm_state(struct ws_win
*win
) {
1768 xcb_atom_t vals
[SWM_EWMH_ACTION_COUNT_MAX
];
1772 vals
[n
++] = ewmh
[_NET_WM_STATE_HIDDEN
].atom
;
1773 if (FULLSCREEN(win
))
1774 vals
[n
++] = ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
;
1775 if (MAXIMIZED_VERT(win
))
1776 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
;
1777 if (MAXIMIZED_HORZ(win
))
1778 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
;
1779 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1780 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
;
1781 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1782 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
;
1783 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1784 vals
[n
++] = ewmh
[_NET_WM_STATE_ABOVE
].atom
;
1785 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1786 vals
[n
++] = ewmh
[_SWM_WM_STATE_MANUAL
].atom
;
1789 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1790 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, n
, vals
);
1792 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1796 ewmh_get_wm_state(struct ws_win
*win
)
1799 xcb_get_property_cookie_t c
;
1800 xcb_get_property_reply_t
*r
;
1806 win
->ewmh_flags
= 0;
1808 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1809 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1810 r
= xcb_get_property_reply(conn
, c
, NULL
);
1814 states
= xcb_get_property_value(r
);
1815 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1817 for (i
= 0; i
< n
; i
++)
1818 ewmh_change_wm_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1826 dumpwins(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
1830 xcb_get_window_attributes_cookie_t c
;
1831 xcb_get_window_attributes_reply_t
*wa
;
1833 /* suppress unused warning since var is needed */
1837 if (r
->ws
== NULL
) {
1838 DPRINTF("dumpwins: invalid workspace\n");
1842 DPRINTF("=== managed window list ws %02d ===\n", r
->ws
->idx
);
1843 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
) {
1844 state
= get_win_state(w
->id
);
1845 c
= xcb_get_window_attributes(conn
, w
->id
);
1846 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1848 DPRINTF("win %#x (%#x), map_state: %d, state: %u, "
1849 "transient: %#x\n", w
->frame
, w
->id
, wa
->map_state
,
1850 state
, w
->transient
);
1853 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1857 DPRINTF("=== stacking order (top down) === \n");
1858 TAILQ_FOREACH(w
, &r
->ws
->stack
, stack_entry
) {
1859 DPRINTF("win %#x (%#x), fs: %s, maximized: %s, above: %s, "
1860 "iconic: %s\n", w
->frame
, w
->id
, YESNO(FULLSCREEN(w
)),
1861 YESNO(MAXIMIZED(w
)), YESNO(ABOVE(w
)), YESNO(ICONIC(w
)));
1864 DPRINTF("===== unmanaged window list =====\n");
1865 TAILQ_FOREACH(w
, &r
->ws
->unmanagedlist
, entry
) {
1866 state
= get_win_state(w
->id
);
1867 c
= xcb_get_window_attributes(conn
, w
->id
);
1868 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1870 DPRINTF("win %#x, map_state: %d, state: %u, "
1871 "transient: %#x\n", w
->id
, wa
->map_state
,
1872 state
, w
->transient
);
1875 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1879 DPRINTF("=================================\n");
1883 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
1886 int num_screens
, i
, j
;
1888 /* Suppress warnings. */
1893 DNPRINTF(SWM_D_MISC
, "debug_toggle\n");
1895 debug_enabled
= !debug_enabled
;
1897 num_screens
= get_screen_count();
1898 for (i
= 0; i
< num_screens
; i
++)
1899 for (j
= 0; j
< workspace_limit
; j
++)
1900 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1907 debug_refresh(struct ws_win
*win
)
1914 XRectangle l_ibox
, l_lbox
;
1915 xcb_rectangle_t rect
;
1917 uint32_t wc
[4], mask
, width
, height
, gcv
[1];
1920 xcb_screen_t
*screen
;
1922 if (debug_enabled
) {
1923 /* Create debug window if it doesn't exist. */
1924 if (win
->debug
== XCB_WINDOW_NONE
) {
1925 if ((screen
= get_screen(win
->s
->idx
)) == NULL
)
1926 errx(1, "ERROR: can't get screen %d.",
1929 win
->debug
= xcb_generate_id(conn
);
1930 wc
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1931 wc
[1] = win
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
1932 wc
[2] = screen
->default_colormap
;
1934 xcb_create_window(conn
, screen
->root_depth
, win
->debug
,
1935 win
->frame
, 0, 0, 10, 10, 1,
1936 XCB_WINDOW_CLASS_INPUT_OUTPUT
, screen
->root_visual
,
1937 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
|
1938 XCB_CW_COLORMAP
, wc
);
1940 xcb_map_window(conn
, win
->debug
);
1943 /* Determine workspace window list index. */
1945 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
1951 /* Determine stacking index (top down). */
1953 TAILQ_FOREACH(w
, &win
->ws
->stack
, stack_entry
) {
1959 if (asprintf(&s
, "%#x f:%#x wl:%d s:%d im:%s", win
->id
,
1960 win
->frame
, widx
, sidx
, get_win_input_model(win
)) == -1)
1965 /* Update window to an appropriate dimension. */
1966 if (bar_font_legacy
) {
1967 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
1968 width
= l_lbox
.width
+ 4;
1969 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
1971 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
1973 width
= info
.width
+ 4;
1974 height
= bar_font
->height
+ 4;
1977 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
1978 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
;
1980 wc
[0] = wc
[1] = border_width
;
1987 xcb_configure_window(conn
, win
->debug
, mask
, wc
);
1989 /* Draw a filled rectangle to 'clear' window. */
1993 rect
.height
= height
;
1995 gcv
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1996 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1997 xcb_poly_fill_rectangle(conn
, win
->debug
, win
->s
->bar_gc
, 1,
2001 if (bar_font_legacy
) {
2002 l_gcv
.graphics_exposures
= 0;
2003 l_draw
= XCreateGC(display
, win
->debug
, 0, &l_gcv
);
2005 XSetForeground(display
, l_draw
,
2006 win
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2008 DRAWSTRING(display
, win
->debug
, bar_fs
, l_draw
, 2,
2009 (bar_fs_extents
->max_logical_extent
.height
-
2010 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
2012 XFreeGC(display
, l_draw
);
2014 draw
= XftDrawCreate(display
, win
->debug
,
2015 DefaultVisual(display
, win
->s
->idx
),
2016 DefaultColormap(display
, win
->s
->idx
));
2018 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
2019 (bar_height
+ bar_font
->height
) / 2 -
2020 bar_font
->descent
, (FcChar8
*)s
, len
);
2022 XftDrawDestroy(draw
);
2026 } else if (win
->debug
!= XCB_WINDOW_NONE
) {
2027 xcb_destroy_window(conn
, win
->debug
);
2028 win
->debug
= XCB_WINDOW_NONE
;
2033 dumpwins(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
2041 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
2047 #endif /* SWM_DEBUG */
2052 int saved_errno
, status
;
2055 saved_errno
= errno
;
2059 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
2064 if (errno
!= ECHILD
)
2065 warn("sighdlr: waitpid");
2066 #endif /* SWM_DEBUG */
2069 if (pid
== searchpid
)
2073 if (WIFEXITED(status
)) {
2074 if (WEXITSTATUS(status
) != 0)
2075 warnx("sighdlr: child exit status: %d",
2076 WEXITSTATUS(status
));
2078 warnx("sighdlr: child is terminated "
2080 #endif /* SWM_DEBUG */
2094 errno
= saved_errno
;
2100 struct pid_e
*p
= NULL
;
2102 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
2107 TAILQ_FOREACH(p
, &pidlist
, entry
) {
2116 name_to_pixel(int sidx
, const char *colorname
)
2118 uint32_t result
= 0;
2119 char cname
[32] = "#";
2120 xcb_screen_t
*screen
;
2121 xcb_colormap_t cmap
;
2122 xcb_alloc_color_reply_t
*cr
;
2123 xcb_alloc_named_color_reply_t
*nr
;
2124 uint16_t rr
, gg
, bb
;
2126 screen
= get_screen(sidx
);
2127 cmap
= screen
->default_colormap
;
2129 /* color is in format rgb://rr/gg/bb */
2130 if (strncmp(colorname
, "rgb:", 4) == 0) {
2131 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
2132 warnx("could not parse rgb %s", colorname
);
2134 cr
= xcb_alloc_color_reply(conn
,
2135 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
2141 warnx("color '%s' not found", colorname
);
2144 nr
= xcb_alloc_named_color_reply(conn
,
2145 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
2148 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
2149 nr
= xcb_alloc_named_color_reply(conn
,
2150 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
2157 warnx("color '%s' not found", colorname
);
2164 setscreencolor(const char *val
, int i
, int c
)
2166 if (i
< 0 || i
>= get_screen_count())
2169 screens
[i
].c
[c
].pixel
= name_to_pixel(i
, val
);
2170 free(screens
[i
].c
[c
].name
);
2171 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
2176 fancy_stacker(struct workspace
*ws
)
2178 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2179 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2180 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2181 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
2182 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
2183 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2184 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2185 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
2186 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
2190 plain_stacker(struct workspace
*ws
)
2192 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2193 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2194 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
2195 sizeof ws
->stacker
);
2196 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2197 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
2198 sizeof ws
->stacker
);
2202 custom_region(const char *val
)
2204 unsigned int x
, y
, w
, h
;
2205 int sidx
, num_screens
;
2206 xcb_screen_t
*screen
;
2208 DNPRINTF(SWM_D_CONF
, "custom_region: %s\n", val
);
2210 num_screens
= get_screen_count();
2211 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
2212 errx(1, "invalid custom region, "
2213 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
2214 if (sidx
< 1 || sidx
> num_screens
)
2215 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
2219 if ((screen
= get_screen(sidx
)) == NULL
)
2220 errx(1, "ERROR: can't get screen %d.", sidx
);
2223 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
2225 if (x
> screen
->width_in_pixels
||
2226 y
> screen
->height_in_pixels
||
2227 w
+ x
> screen
->width_in_pixels
||
2228 h
+ y
> screen
->height_in_pixels
) {
2229 warnx("ignoring region %ux%u+%u+%u - not within screen "
2230 "boundaries (%ux%u)", w
, h
, x
, y
,
2231 screen
->width_in_pixels
, screen
->height_in_pixels
);
2235 new_region(&screens
[sidx
], x
, y
, w
, h
);
2239 socket_setnonblock(int fd
)
2243 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
2244 err(1, "fcntl F_GETFL");
2245 flags
|= O_NONBLOCK
;
2246 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
2247 err(1, "fcntl F_SETFL");
2251 bar_print_legacy(struct swm_region
*r
, const char *s
)
2253 xcb_rectangle_t rect
;
2258 XRectangle ibox
, lbox
;
2262 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
2264 switch (bar_justify
) {
2265 case SWM_BAR_JUSTIFY_LEFT
:
2268 case SWM_BAR_JUSTIFY_CENTER
:
2269 x
= (WIDTH(r
) - lbox
.width
) / 2;
2271 case SWM_BAR_JUSTIFY_RIGHT
:
2272 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
2276 if (x
< SWM_BAR_OFFSET
)
2281 rect
.width
= WIDTH(r
->bar
);
2282 rect
.height
= HEIGHT(r
->bar
);
2284 /* clear back buffer */
2285 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2286 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2287 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2289 /* draw back buffer */
2290 gcvd
.graphics_exposures
= 0;
2291 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
2292 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2293 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
2294 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
2296 XFreeGC(display
, draw
);
2299 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2300 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2304 bar_print(struct swm_region
*r
, const char *s
)
2307 xcb_rectangle_t rect
;
2315 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
2317 switch (bar_justify
) {
2318 case SWM_BAR_JUSTIFY_LEFT
:
2321 case SWM_BAR_JUSTIFY_CENTER
:
2322 x
= (WIDTH(r
) - info
.width
) / 2;
2324 case SWM_BAR_JUSTIFY_RIGHT
:
2325 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
2329 if (x
< SWM_BAR_OFFSET
)
2334 rect
.width
= WIDTH(r
->bar
);
2335 rect
.height
= HEIGHT(r
->bar
);
2337 /* clear back buffer */
2338 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2339 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2340 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2342 /* draw back buffer */
2343 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
2344 DefaultVisual(display
, r
->s
->idx
),
2345 DefaultColormap(display
, r
->s
->idx
));
2347 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
2348 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
2351 XftDrawDestroy(draw
);
2354 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2355 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2359 bar_extra_stop(void)
2363 bzero(bar_pipe
, sizeof bar_pipe
);
2366 kill(bar_pid
, SIGTERM
);
2369 strlcpy(bar_ext
, "", sizeof bar_ext
);
2374 bar_window_class(char *s
, size_t sz
, struct swm_region
*r
)
2376 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2378 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
2379 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
2383 bar_window_instance(char *s
, size_t sz
, struct swm_region
*r
)
2385 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2387 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
2388 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
2392 bar_window_class_instance(char *s
, size_t sz
, struct swm_region
*r
)
2394 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2397 bar_window_class(s
, sz
, r
);
2398 strlcat(s
, ":", sz
);
2399 bar_window_instance(s
, sz
, r
);
2403 bar_window_state(char *s
, size_t sz
, struct swm_region
*r
)
2405 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2407 if (MAXIMIZED(r
->ws
->focus
))
2408 strlcat(s
, "(m)", sz
);
2409 else if (ABOVE(r
->ws
->focus
))
2410 strlcat(s
, "(f)", sz
);
2414 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
2418 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2421 title
= get_win_name(r
->ws
->focus
->id
);
2422 strlcat(s
, title
, sz
);
2427 get_urgent(struct ws_win
*win
)
2429 xcb_icccm_wm_hints_t hints
;
2430 xcb_get_property_cookie_t c
;
2431 bool urgent
= false;
2434 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2435 if (xcb_icccm_get_wm_hints_reply(conn
, c
, &hints
, NULL
))
2436 urgent
= xcb_icccm_wm_hints_get_urgency(&hints
);
2443 bar_urgent(char *s
, size_t sz
)
2446 int i
, j
, num_screens
;
2447 bool urgent
[SWM_WS_MAX
];
2450 for (i
= 0; i
< workspace_limit
; i
++)
2453 num_screens
= get_screen_count();
2454 for (i
= 0; i
< num_screens
; i
++)
2455 for (j
= 0; j
< workspace_limit
; j
++)
2456 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2457 if (get_urgent(win
))
2460 for (i
= 0; i
< workspace_limit
; i
++) {
2462 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2464 } else if (!urgent_collapse
) {
2465 strlcat(s
, "- ", sz
);
2468 if (urgent_collapse
&& s
[0])
2469 s
[strlen(s
) - 1] = 0;
2473 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2475 if (r
== NULL
|| r
->ws
== NULL
)
2477 if (r
->ws
->name
!= NULL
)
2478 strlcat(s
, r
->ws
->name
, sz
);
2481 /* build the default bar format according to the defined enabled options */
2483 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2487 /* if format provided, just copy the buffers */
2488 if (bar_format
!= NULL
) {
2489 strlcpy(fmtnew
, fmtexp
, sz
);
2493 /* reset the output buffer */
2496 strlcat(fmtnew
, "+N:+I ", sz
);
2498 strlcat(fmtnew
, "+S", sz
);
2499 strlcat(fmtnew
, " ", sz
);
2501 /* only show the workspace name if there's actually one */
2502 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2503 strlcat(fmtnew
, "<+D>", sz
);
2505 /* If enabled, only show the iconic count if there are iconic wins. */
2506 if (iconic_enabled
&& r
!= NULL
&& r
->ws
!= NULL
)
2507 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2509 strlcat(fmtnew
, "{+M}", sz
);
2513 strlcat(fmtnew
, "+3<", sz
);
2515 if (clock_enabled
) {
2516 strlcat(fmtnew
, fmtexp
, sz
);
2517 strlcat(fmtnew
, "+4<", sz
);
2520 /* bar_urgent already adds the space before the last asterisk */
2522 strlcat(fmtnew
, (urgent_collapse
? "*+U*+4<" : "* +U*+4<"), sz
);
2524 if (window_class_enabled
) {
2525 strlcat(fmtnew
, "+C", sz
);
2526 if (!window_instance_enabled
)
2527 strlcat(fmtnew
, "+4<", sz
);
2530 /* checks needed by the colon and floating strlcat(3) calls below */
2531 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2532 if (window_instance_enabled
) {
2533 if (window_class_enabled
)
2534 strlcat(fmtnew
, ":", sz
);
2535 strlcat(fmtnew
, "+T+4<", sz
);
2537 if (window_name_enabled
) {
2538 if (ABOVE(r
->ws
->focus
) || MAXIMIZED(r
->ws
->focus
))
2539 strlcat(fmtnew
, "+F ", sz
);
2540 strlcat(fmtnew
, "+64W ", sz
);
2544 /* finally add the action script output and the version */
2545 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2549 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2551 /* special case; no limit given, pad one space, instead */
2552 if (*limit
== (int)sz
- 1)
2554 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2557 /* replaces the bar format character sequences (like in tmux(1)) */
2559 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2564 char tmp
[SWM_BAR_MAX
];
2565 int limit
, size
, count
;
2568 /* reset strlcat(3) buffer */
2571 /* get number, if any */
2574 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2575 limit
= sizeof tmp
- 1;
2576 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2577 limit
= sizeof tmp
- 1;
2579 /* there is nothing to replace (ie EOL) */
2586 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2589 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2592 bar_window_class(tmp
, sizeof tmp
, r
);
2595 bar_workspace_name(tmp
, sizeof tmp
, r
);
2598 bar_window_state(tmp
, sizeof tmp
, r
);
2601 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2605 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2609 snprintf(tmp
, sizeof tmp
, "%d", count
);
2612 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2615 bar_window_class_instance(tmp
, sizeof tmp
, r
);
2618 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2621 bar_window_instance(tmp
, sizeof tmp
, r
);
2624 bar_urgent(tmp
, sizeof tmp
);
2627 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2630 bar_window_name(tmp
, sizeof tmp
, r
);
2633 /* unknown character sequence; copy as-is */
2634 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2640 if ((int)len
< limit
)
2642 while (limit
-- > 0) {
2643 if (*offrep
>= sz
- 1)
2645 fmtrep
[(*offrep
)++] = *ptr
++;
2653 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2658 while (*fmt
!= '\0') {
2660 /* skip ordinary characters */
2663 fmtrep
[off
++] = *fmt
++;
2667 /* character sequence found; replace it */
2668 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2677 bar_fmt_expand(char *fmtexp
, size_t sz
)
2684 /* start by grabbing the current time and date */
2686 localtime_r(&tmt
, &tm
);
2688 /* figure out what to expand */
2689 if (bar_format
!= NULL
)
2691 else if (bar_format
== NULL
&& clock_enabled
)
2693 /* if nothing to expand bail out */
2699 /* copy as-is, just in case the format shouldn't be expanded below */
2700 strlcpy(fmtexp
, fmt
, sz
);
2701 /* finally pass the string through strftime(3) */
2702 #ifndef SWM_DENY_CLOCK_FORMAT
2703 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2704 warnx("format too long");
2709 /* Redraws a region bar; need to follow with xcb_flush() or focus_flush(). */
2711 bar_draw(struct swm_bar
*bar
)
2713 struct swm_region
*r
;
2714 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2715 char fmtrep
[SWM_BAR_MAX
];
2717 /* expand the format by first passing it through strftime(3) */
2718 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2725 if (bar_enabled
&& r
->ws
->bar_enabled
)
2726 xcb_map_window(conn
, bar
->id
);
2728 xcb_unmap_window(conn
, bar
->id
);
2732 if (startup_exception
)
2733 snprintf(fmtrep
, sizeof fmtrep
, "total "
2734 "exceptions: %d, first exception: %s",
2738 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2739 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2742 if (bar_font_legacy
)
2743 bar_print_legacy(r
, fmtrep
);
2745 bar_print(r
, fmtrep
);
2749 * Reads external script output; call when stdin is readable.
2750 * Returns 1 if bar_ext was updated; otherwise 0.
2753 bar_extra_update(void)
2756 char b
[SWM_BAR_MAX
];
2757 bool changed
= false;
2762 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2765 if (b
[len
- 1] == '\n') {
2766 /* Remove newline. */
2769 /* "Clear" bar_ext. */
2772 /* Flush buffered output. */
2773 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2774 bar_ext_buf
[0] = '\0';
2776 /* Append new output to bar. */
2777 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2781 /* Buffer output. */
2782 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2787 if (errno
!= EAGAIN
) {
2788 warn("bar_action failed");
2797 bar_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
2799 struct swm_region
*tmpr
;
2802 /* suppress unused warnings since vars are needed */
2807 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2810 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2811 /* Only change if master switch is enabled. */
2813 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2815 bar_enabled
= r
->ws
->bar_enabled
= true;
2817 case SWM_ARG_ID_BAR_TOGGLE
:
2818 bar_enabled
= !bar_enabled
;
2822 /* update bars as necessary */
2823 num_screens
= get_screen_count();
2824 for (i
= 0; i
< num_screens
; i
++)
2825 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2827 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2828 xcb_map_window(conn
, tmpr
->bar
->id
);
2830 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2833 /* Restack all regions and redraw bar. */
2834 num_screens
= get_screen_count();
2835 for (i
= 0; i
< num_screens
; i
++)
2836 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
2838 bar_draw(tmpr
->bar
);
2845 bar_extra_setup(void)
2847 /* do this here because the conf file is in memory */
2848 if (!bar_extra
&& bar_argv
[0]) {
2849 /* launch external status app */
2851 if (pipe(bar_pipe
) == -1)
2852 err(1, "pipe error");
2853 socket_setnonblock(bar_pipe
[0]);
2854 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2856 /* Set stdin to read from the pipe. */
2857 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2860 /* Set stdout to write to the pipe. */
2861 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2864 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2865 err(1, "could not disable SIGPIPE");
2866 switch (bar_pid
= fork()) {
2868 err(1, "cannot fork");
2872 execvp(bar_argv
[0], bar_argv
);
2873 err(1, "%s external app failed", bar_argv
[0]);
2875 default: /* parent */
2880 atexit(kill_bar_extra_atexit
);
2885 kill_bar_extra_atexit(void)
2888 kill(bar_pid
, SIGTERM
);
2896 while ((s
= index(s
, '-'))) {
2901 return (count
== 14);
2907 char *default_string
;
2908 char **missing_charsets
;
2909 int num_missing_charsets
= 0;
2913 XFreeFontSet(display
, bar_fs
);
2917 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n",
2920 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2921 &num_missing_charsets
, &default_string
);
2923 if (num_missing_charsets
> 0) {
2924 warnx("Unable to load charset(s):");
2926 for (i
= 0; i
< num_missing_charsets
; ++i
)
2927 warnx("%s", missing_charsets
[i
]);
2929 XFreeStringList(missing_charsets
);
2931 if (strcmp(default_string
, ""))
2932 warnx("Glyphs from those sets will be replaced "
2933 "by '%s'.", default_string
);
2935 warnx("Glyphs from those sets won't be drawn.");
2939 errx(1, "Error creating font set structure.");
2941 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2943 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2944 2 * bar_border_width
;
2951 xft_init(struct swm_region
*r
)
2953 char *font
, *str
, *search
;
2956 if (bar_font
== NULL
) {
2957 if ((search
= str
= strdup(bar_fonts
)) == NULL
)
2958 errx(1, "insufficient memory.");
2960 while ((font
= strsep(&search
, ",")) != NULL
) {
2964 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2967 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2970 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2974 if (bar_font
== NULL
) {
2975 warnx("unable to load font %s", font
);
2978 DNPRINTF(SWM_D_INIT
, "successfully opened "
2986 if (bar_font
== NULL
)
2987 errx(1, "unable to open a font");
2989 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2991 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2992 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2993 warn("Xft error: unable to allocate color.");
2995 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR
].pixel
, color
);
2997 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2998 DefaultColormap(display
, r
->s
->idx
), &color
, &search_font_color
))
2999 warn("Xft error: unable to allocate color.");
3001 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
3008 bar_setup(struct swm_region
*r
)
3010 xcb_screen_t
*screen
;
3013 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
3016 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
3017 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
3022 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
3023 err(1, "bar_setup: calloc: failed to allocate memory.");
3025 if (bar_font_legacy
)
3032 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
3033 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
3034 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
3036 /* Assume region is unfocused when we create the bar. */
3037 r
->bar
->id
= xcb_generate_id(conn
);
3038 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
3039 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
3040 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
3041 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
3043 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
3044 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
3045 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
3046 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
3047 | XCB_CW_EVENT_MASK
, wa
);
3049 /* Stack bar window above region window to start. */
3051 wa
[1] = XCB_STACK_MODE_ABOVE
;
3053 xcb_configure_window(conn
, r
->bar
->id
, XCB_CONFIG_WINDOW_SIBLING
|
3054 XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
3056 r
->bar
->buffer
= xcb_generate_id(conn
);
3057 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
3058 WIDTH(r
->bar
), HEIGHT(r
->bar
));
3061 xcb_randr_select_input(conn
, r
->bar
->id
,
3062 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
3065 xcb_map_window(conn
, r
->bar
->id
);
3067 DNPRINTF(SWM_D_BAR
, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
3068 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
3075 bar_cleanup(struct swm_region
*r
)
3079 xcb_destroy_window(conn
, r
->bar
->id
);
3080 xcb_free_pixmap(conn
, r
->bar
->buffer
);
3086 set_win_state(struct ws_win
*win
, uint8_t state
)
3088 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
3090 DNPRINTF(SWM_D_EVENT
, "set_win_state: win %#x, state: %u\n",
3096 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
3097 a_state
, 32, 2, data
);
3101 get_win_state(xcb_window_t w
)
3103 xcb_get_property_reply_t
*r
;
3104 xcb_get_property_cookie_t c
;
3105 uint32_t result
= 0;
3107 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
3108 r
= xcb_get_property_reply(conn
, c
, NULL
);
3110 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
3111 result
= *((uint32_t *)xcb_get_property_value(r
));
3115 DNPRINTF(SWM_D_MISC
, "get_win_state property: win %#x state %u\n", w
,
3121 version(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3123 struct swm_region
*tmpr
;
3126 /* suppress unused warnings since vars are needed */
3131 bar_version
= !bar_version
;
3133 snprintf(bar_vertext
, sizeof bar_vertext
,
3134 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
3136 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
3138 num_screens
= get_screen_count();
3139 for (i
= 0; i
< num_screens
; i
++) {
3140 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
3141 bar_draw(tmpr
->bar
);
3148 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
3150 xcb_client_message_event_t ev
;
3158 name
= get_atom_name(a
);
3159 DNPRINTF(SWM_D_EVENT
, "client_msg: win %#x, atom: %s(%u), "
3161 win
->id
, name
, a
, t
);
3165 bzero(&ev
, sizeof ev
);
3166 ev
.response_type
= XCB_CLIENT_MESSAGE
;
3167 ev
.window
= win
->id
;
3170 ev
.data
.data32
[0] = a
;
3171 ev
.data
.data32
[1] = t
;
3173 xcb_send_event(conn
, 0, win
->id
,
3174 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
3177 /* synthetic response to a ConfigureRequest when not making a change */
3179 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
3181 xcb_configure_notify_event_t ce
;
3186 /* send notification of unchanged state. */
3187 bzero(&ce
, sizeof(ce
));
3188 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
3191 ce
.width
= WIDTH(win
);
3192 ce
.height
= HEIGHT(win
);
3193 ce
.border_width
= 0;
3194 ce
.override_redirect
= 0;
3199 ce
.window
= win
->id
;
3200 ce
.above_sibling
= XCB_WINDOW_NONE
;
3203 ce
.event
= ev
->window
;
3204 ce
.window
= ev
->window
;
3206 /* make response appear more WM_SIZE_HINTS-compliant */
3207 if (win
->sh
.flags
) {
3208 DNPRINTF(SWM_D_MISC
, "config_win: hints: win %#x,"
3209 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
3210 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
3211 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
3212 SH_INC_W(win
), SH_INC_H(win
));
3217 /* the hint may be set... to 0! */
3218 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
3219 ce
.width
= SH_MIN_W(win
);
3220 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
3221 ce
.height
= SH_MIN_H(win
);
3226 /* may also be advertized as 0 */
3227 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
3228 ce
.width
= SH_MAX_W(win
);
3229 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
3230 ce
.height
= SH_MAX_H(win
);
3233 /* resize increment. */
3235 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
3236 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
3238 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
3239 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
3243 /* adjust x and y for requested border_width. */
3244 ce
.x
+= ev
->border_width
;
3245 ce
.y
+= ev
->border_width
;
3247 ce
.above_sibling
= ev
->sibling
;
3250 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
3251 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
3252 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
3254 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
3259 count_win(struct workspace
*ws
, bool count_transient
)
3264 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3265 if (!count_transient
&& FLOATING(win
))
3271 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
3277 quit(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3279 /* suppress unused warnings since vars are needed */
3284 DNPRINTF(SWM_D_MISC
, "quit\n");
3289 lower_window(struct ws_win
*win
)
3291 struct ws_win
*target
= NULL
;
3292 struct workspace
*ws
;
3294 DNPRINTF(SWM_D_EVENT
, "lower_window: win %#x\n", WINID(win
));
3301 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3302 if (target
== win
|| ICONIC(target
))
3304 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3307 if (win
->transient
== target
->transient
)
3309 if (win
->transient
== target
->id
)
3312 if (FULLSCREEN(target
))
3314 if (FULLSCREEN(win
))
3316 if (MAXIMIZED(target
))
3320 if (ABOVE(target
) || TRANS(target
))
3322 if (ABOVE(win
) || TRANS(win
))
3326 /* Change stack position. */
3327 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3329 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3331 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3333 update_win_stacking(win
);
3336 if (swm_debug
& SWM_D_STACK
) {
3337 DPRINTF("=== stacking order (top down) === \n");
3338 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3339 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3340 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3341 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3342 YESNO(ICONIC(target
)));
3346 DNPRINTF(SWM_D_EVENT
, "lower_window: done\n");
3350 raise_window(struct ws_win
*win
)
3352 struct ws_win
*target
= NULL
;
3353 struct workspace
*ws
;
3355 DNPRINTF(SWM_D_EVENT
, "raise_window: win %#x\n", WINID(win
));
3362 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3363 if (target
== win
|| ICONIC(target
))
3365 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3367 if (TRANS(win
) && (win
->transient
== target
->transient
||
3368 win
->transient
== target
->id
))
3370 if (FULLSCREEN(win
))
3372 if (FULLSCREEN(target
))
3376 if (MAXIMIZED(target
))
3378 if (ABOVE(win
) || TRANS(win
) ||
3379 (win
->ws
->focus
== win
&& ws
->always_raise
))
3381 if (!ABOVE(target
) && !TRANS(target
))
3385 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3387 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3389 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3391 update_win_stacking(win
);
3394 if (swm_debug
& SWM_D_STACK
) {
3395 DPRINTF("=== stacking order (top down) === \n");
3396 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3397 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3398 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3399 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3400 YESNO(ICONIC(target
)));
3404 DNPRINTF(SWM_D_EVENT
, "raise_window: done\n");
3408 update_win_stacking(struct ws_win
*win
)
3410 struct ws_win
*sibling
;
3414 struct swm_region
*r
;
3417 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
3420 if (win
->frame
== XCB_WINDOW_NONE
) {
3421 DNPRINTF(SWM_D_EVENT
, "update_window_stacking: win %#x not "
3422 "reparented.\n", win
->id
);
3426 sibling
= TAILQ_NEXT(win
, stack_entry
);
3427 if (sibling
!= NULL
&& (FLOATING(win
) == FLOATING(sibling
) ||
3428 (win
->ws
->always_raise
&& win
->ws
->focus
== win
))) {
3429 val
[0] = sibling
->frame
;
3430 val
[1] = XCB_STACK_MODE_ABOVE
;
3431 } else if (FLOATING(win
) || (win
->ws
->always_raise
&&
3432 win
->ws
->focus
== win
)) {
3433 val
[0] = r
->bar
->id
;
3434 val
[1] = XCB_STACK_MODE_ABOVE
;
3436 val
[0] = r
->bar
->id
;
3437 val
[1] = XCB_STACK_MODE_BELOW
;
3440 DNPRINTF(SWM_D_EVENT
, "update_win_stacking: win %#x (%#x), "
3441 "sibling %#x mode %#x\n", win
->frame
, win
->id
, val
[0], val
[1]);
3443 xcb_configure_window(conn
, win
->frame
, XCB_CONFIG_WINDOW_SIBLING
|
3444 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
3447 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
)
3453 map_window(struct ws_win
*win
)
3458 DNPRINTF(SWM_D_EVENT
, "map_window: win %#x, mapped: %s\n",
3459 win
->id
, YESNO(win
->mapped
));
3464 xcb_map_window(conn
, win
->frame
);
3465 xcb_map_window(conn
, win
->id
);
3466 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
3471 unmap_window(struct ws_win
*win
)
3476 DNPRINTF(SWM_D_EVENT
, "unmap_window: win %#x, mapped: %s\n", win
->id
,
3477 YESNO(win
->mapped
));
3482 xcb_unmap_window(conn
, win
->id
);
3483 xcb_unmap_window(conn
, win
->frame
);
3484 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
3485 win
->mapped
= false;
3492 int i
, j
, num_screens
;
3494 num_screens
= get_screen_count();
3495 for (i
= 0; i
< num_screens
; i
++)
3496 for (j
= 0; j
< workspace_limit
; j
++)
3497 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3502 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
3504 xcb_key_press_event_t event
;
3505 xcb_keycode_t
*keycode
;
3510 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
3512 DNPRINTF(SWM_D_MISC
, "fake_keypress: win %#x, keycode %u\n",
3515 bzero(&event
, sizeof(event
));
3516 event
.event
= win
->id
;
3517 event
.root
= win
->s
->root
;
3518 event
.child
= XCB_WINDOW_NONE
;
3519 event
.time
= XCB_CURRENT_TIME
;
3520 event
.event_x
= X(win
);
3521 event
.event_y
= Y(win
);
3524 event
.same_screen
= 1;
3525 event
.detail
= *keycode
;
3526 event
.state
= modifiers
;
3528 event
.response_type
= XCB_KEY_PRESS
;
3529 xcb_send_event(conn
, 1, win
->id
,
3530 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
3532 event
.response_type
= XCB_KEY_RELEASE
;
3533 xcb_send_event(conn
, 1, win
->id
,
3534 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
3540 restart(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3542 /* suppress unused warning since var is needed */
3547 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
3551 execvp(start_argv
[0], start_argv
);
3552 warn("execvp failed");
3553 quit(NULL
, NULL
, NULL
);
3557 get_pointer_win(xcb_window_t root
)
3559 struct ws_win
*win
= NULL
;
3560 xcb_query_pointer_reply_t
*r
;
3562 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: %#x.\n", root
);
3564 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
3566 win
= find_window(r
->child
);
3568 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: %#x.\n",
3571 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
3580 center_pointer(struct swm_region
*r
)
3584 if (!warp_pointer
|| r
== NULL
)
3589 DNPRINTF(SWM_D_EVENT
, "center_pointer: win %#x.\n", WINID(win
));
3591 if (win
&& win
->mapped
)
3592 xcb_warp_pointer(conn
, XCB_NONE
, win
->frame
, 0, 0, 0, 0,
3593 WIDTH(win
) / 2, HEIGHT(win
) / 2);
3595 xcb_warp_pointer(conn
, XCB_NONE
, r
->id
, 0, 0, 0, 0,
3596 WIDTH(r
) / 2, HEIGHT(r
) / 2);
3600 root_to_region(xcb_window_t root
, int check
)
3603 struct swm_region
*r
= NULL
;
3605 xcb_query_pointer_reply_t
*qpr
;
3606 xcb_get_input_focus_reply_t
*gifr
;
3608 DNPRINTF(SWM_D_MISC
, "root_to_region: win %#x\n", root
);
3610 num_screens
= get_screen_count();
3611 for (i
= 0; i
< num_screens
; i
++)
3612 if (screens
[i
].root
== root
)
3615 if (check
& SWM_CK_REGION
)
3616 r
= screens
[i
].r_focus
;
3618 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
3619 /* Try to find an actively focused window */
3620 gifr
= xcb_get_input_focus_reply(conn
,
3621 xcb_get_input_focus(conn
), NULL
);
3623 cfw
= find_window(gifr
->focus
);
3624 if (cfw
&& cfw
->ws
->r
)
3631 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
3632 /* No region with an active focus; try to use pointer. */
3633 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
3634 screens
[i
].root
), NULL
);
3636 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
3637 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
3638 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3639 if (X(r
) <= qpr
->root_x
&&
3640 qpr
->root_x
< MAX_X(r
) &&
3641 Y(r
) <= qpr
->root_y
&&
3642 qpr
->root_y
< MAX_Y(r
))
3649 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
3650 r
= TAILQ_FIRST(&screens
[i
].rl
);
3652 DNPRINTF(SWM_D_MISC
, "root_to_region: idx: %d\n", get_region_index(r
));
3658 find_region(xcb_window_t id
)
3660 struct swm_region
*r
;
3663 num_screens
= get_screen_count();
3664 for (i
= 0; i
< num_screens
; i
++)
3665 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3673 find_bar(xcb_window_t id
)
3675 struct swm_region
*r
;
3678 num_screens
= get_screen_count();
3679 for (i
= 0; i
< num_screens
; i
++)
3680 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3681 if (r
->bar
&& r
->bar
->id
== id
)
3688 find_frame_window(xcb_window_t id
) {
3689 struct swm_region
*r
;
3693 num_screens
= get_screen_count();
3694 for (i
= 0; i
< num_screens
; i
++)
3695 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3696 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
3704 find_window(xcb_window_t id
)
3706 struct ws_win
*win
= NULL
;
3707 int i
, j
, num_screens
;
3708 xcb_query_tree_reply_t
*qtr
;
3710 DNPRINTF(SWM_D_MISC
, "find_window: id: %#x\n", id
);
3712 num_screens
= get_screen_count();
3713 for (i
= 0; i
< num_screens
; i
++)
3714 for (j
= 0; j
< workspace_limit
; j
++)
3715 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3716 if (id
== win
->id
|| id
== win
->frame
)
3720 /* If window isn't top-level, try to find managed ancestor. */
3721 qtr
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3723 if (qtr
->parent
!= XCB_WINDOW_NONE
&& qtr
->parent
!= qtr
->root
)
3724 win
= find_window(qtr
->parent
);
3728 DNPRINTF(SWM_D_MISC
, "find_window: found child %#x "
3729 "of %#x.\n", win
->id
, qtr
->parent
);
3739 find_unmanaged_window(xcb_window_t id
)
3742 int i
, j
, num_screens
;
3744 num_screens
= get_screen_count();
3745 for (i
= 0; i
< num_screens
; i
++)
3746 for (j
= 0; j
< workspace_limit
; j
++)
3747 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
3755 spawn(int ws_idx
, union arg
*args
, bool close_fd
)
3760 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3762 close(xcb_get_file_descriptor(conn
));
3764 if ((ret
= getenv("LD_PRELOAD"))) {
3765 if (asprintf(&ret
, "%s:%s", SWM_LIB
, ret
) == -1) {
3766 warn("spawn: asprintf LD_PRELOAD");
3769 setenv("LD_PRELOAD", ret
, 1);
3772 setenv("LD_PRELOAD", SWM_LIB
, 1);
3775 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3776 warn("spawn: asprintf SWM_WS");
3779 setenv("_SWM_WS", ret
, 1);
3783 if (asprintf(&ret
, "%d", getpid()) == -1) {
3784 warn("spawn: asprintf _SWM_PID");
3787 setenv("_SWM_PID", ret
, 1);
3791 if (setsid() == -1) {
3792 warn("spawn: setsid");
3798 * close stdin and stdout to prevent interaction between apps
3799 * and the baraction script
3800 * leave stderr open to record errors
3802 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3803 warn("spawn: open");
3806 dup2(fd
, STDIN_FILENO
);
3807 dup2(fd
, STDOUT_FILENO
);
3812 execvp(args
->argv
[0], args
->argv
);
3814 warn("spawn: execvp");
3819 kill_refs(struct ws_win
*win
)
3821 struct workspace
*ws
;
3823 int i
, j
, num_screens
;
3828 num_screens
= get_screen_count();
3829 for (i
= 0; i
< num_screens
; i
++) {
3830 for (j
= 0; j
< workspace_limit
; j
++) {
3831 ws
= &screens
[i
].ws
[j
];
3833 if (win
== ws
->focus
)
3835 if (win
== ws
->focus_prev
)
3836 ws
->focus_prev
= NULL
;
3837 if (win
== ws
->focus_pending
)
3838 ws
->focus_pending
= NULL
;
3839 if (win
== ws
->focus_raise
)
3840 ws
->focus_raise
= NULL
;
3843 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3844 if (win
== w
->focus_child
)
3845 w
->focus_child
= NULL
;
3851 validate_win(struct ws_win
*testwin
)
3854 struct workspace
*ws
;
3855 struct swm_region
*r
;
3856 int i
, x
, num_screens
;
3858 if (testwin
== NULL
)
3861 num_screens
= get_screen_count();
3862 for (i
= 0; i
< num_screens
; i
++)
3863 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3864 for (x
= 0; x
< workspace_limit
; x
++) {
3866 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3874 validate_ws(struct workspace
*testws
)
3876 struct swm_region
*r
;
3877 struct workspace
*ws
;
3878 int i
, x
, num_screens
;
3880 /* validate all ws */
3881 num_screens
= get_screen_count();
3882 for (i
= 0; i
< num_screens
; i
++)
3883 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3884 for (x
= 0; x
< workspace_limit
; x
++) {
3893 unfocus_win(struct ws_win
*win
)
3895 xcb_window_t none
= XCB_WINDOW_NONE
;
3897 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: win %#x\n", WINID(win
));
3902 if (win
->ws
== NULL
) {
3903 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3907 if (validate_ws(win
->ws
)) {
3908 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3912 if (win
->ws
->r
== NULL
) {
3913 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3917 if (validate_win(win
)) {
3918 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3923 if (win
->ws
->focus
== win
) {
3924 win
->ws
->focus
= NULL
;
3925 win
->ws
->focus_prev
= win
;
3926 if (win
->ws
->focus_raise
== win
&& !FLOATING(win
)) {
3927 update_win_stacking(win
);
3931 if (validate_win(win
->ws
->focus
)) {
3932 kill_refs(win
->ws
->focus
);
3933 win
->ws
->focus
= NULL
;
3936 if (validate_win(win
->ws
->focus_prev
)) {
3937 kill_refs(win
->ws
->focus_prev
);
3938 win
->ws
->focus_prev
= NULL
;
3943 /* Raise window to "top unfocused position." */
3944 if (win
->ws
->always_raise
)
3947 /* Update border width */
3948 if (win
->bordered
&& (win
->quirks
& SWM_Q_MINIMALBORDER
) &&
3951 X(win
) += border_width
;
3952 Y(win
) += border_width
;
3956 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3957 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3959 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3963 focus_win(struct ws_win
*win
)
3965 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
, *tmpw
;
3966 struct workspace
*ws
;
3967 xcb_get_input_focus_reply_t
*gifr
= NULL
;
3968 xcb_get_window_attributes_reply_t
*war
= NULL
;
3970 DNPRINTF(SWM_D_FOCUS
, "focus_win: win %#x\n", WINID(win
));
3972 if (win
== NULL
|| win
->ws
== NULL
|| !win
->mapped
)
3977 if (validate_ws(ws
))
3980 if (validate_win(win
)) {
3985 gifr
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3987 DNPRINTF(SWM_D_FOCUS
, "focus_win: cur focus: %#x\n",
3990 cfw
= find_window(gifr
->focus
);
3993 if (cfw
->ws
!= ws
&& cfw
->ws
->r
!= NULL
&&
3994 cfw
->frame
!= XCB_WINDOW_NONE
) {
4001 war
= xcb_get_window_attributes_reply(conn
,
4002 xcb_get_window_attributes(conn
, gifr
->focus
), NULL
);
4003 if (war
&& war
->override_redirect
&& ws
->focus
== win
) {
4004 DNPRINTF(SWM_D_FOCUS
, "focus_win: skip refocus "
4005 "from override_redirect.\n");
4011 if (ws
->focus
!= win
) {
4012 if (ws
->focus
&& ws
->focus
!= cfw
)
4013 unfocus_win(ws
->focus
);
4017 /* Clear focus child redirect. */
4018 win
->focus_child
= NULL
;
4020 /* If transient, adjust parent's focus child for focus_magic. */
4022 parent
= find_window(win
->transient
);
4023 if (parent
&& parent
->focus_child
!= win
)
4024 parent
->focus_child
= win
;
4027 /* Update window border even if workspace is hidden. */
4031 DNPRINTF(SWM_D_FOCUS
, "focus_win: already focused.\n");
4036 /* Set input focus if no input hint, or indicated by hint. */
4037 if (ACCEPTS_FOCUS(win
)) {
4038 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
4039 " revert-to: parent, time: %#x\n", win
->id
,
4041 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
4042 win
->id
, last_event_time
);
4043 } else if (!win
->take_focus
) {
4044 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
4045 " revert-to: parent, time: 0\n", ws
->r
->id
);
4046 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
4047 ws
->r
->id
, XCB_CURRENT_TIME
);
4050 /* Tell app it can adjust focus to a specific window. */
4051 if (win
->take_focus
) {
4052 /* java is special; always tell parent */
4053 if (TRANS(win
) && win
->java
)
4054 client_msg(parent
, a_takefocus
,
4057 client_msg(win
, a_takefocus
, last_event_time
);
4060 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
4062 /* If a parent exists, map it first. */
4064 raise_window(parent
);
4067 /* Map siblings next. */
4068 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
,
4070 if (w
!= win
&& !ICONIC(w
) &&
4071 w
->transient
== parent
->id
) {
4077 /* Map focused window. */
4081 /* Stack any children of focus window. */
4082 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
4083 if (w
->transient
== win
->id
&& !ICONIC(w
)) {
4087 } else if (tile_gap
< 0 && !ABOVE(win
)) {
4089 * Windows overlap in the layout.
4090 * Raise focused win above all tiled wins.
4098 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4099 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4102 bar_draw(ws
->r
->bar
);
4108 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
4111 /* If a child window should have focus instead, return it. */
4113 get_focus_magic(struct ws_win
*win
)
4115 struct ws_win
*parent
= NULL
;
4116 struct ws_win
*child
= NULL
;
4118 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: win %#x\n", WINID(win
));
4123 parent
= find_window(win
->transient
);
4125 /* If parent prefers focus elsewhere, then try to do so. */
4126 if (parent
&& (child
= parent
->focus_child
)) {
4127 if (validate_win(child
) == 0 && child
->mapped
)
4130 parent
->focus_child
= NULL
;
4134 /* If this window prefers focus elsewhere, then try to do so. */
4135 if ((child
= win
->focus_child
)) {
4136 if (validate_win(child
) == 0 && child
->mapped
)
4139 win
->focus_child
= NULL
;
4146 event_drain(uint8_t rt
)
4148 xcb_generic_event_t
*evt
;
4150 /* ensure all pending requests have been processed before filtering. */
4152 while ((evt
= get_next_event(false))) {
4153 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
4161 set_region(struct swm_region
*r
)
4163 struct swm_region
*rf
;
4170 /* Unfocus old region bar. */
4175 xcb_change_window_attributes(conn
, rf
->bar
->id
,
4176 XCB_CW_BORDER_PIXEL
,
4177 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
4180 if (rf
!= NULL
&& rf
!= r
&& (X(rf
) != X(r
) || Y(rf
) != Y(r
) ||
4181 WIDTH(rf
) != WIDTH(r
) || HEIGHT(rf
) != HEIGHT(r
))) {
4182 /* Set _NET_DESKTOP_GEOMETRY. */
4184 vals
[1] = HEIGHT(r
);
4185 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, r
->s
->root
,
4186 ewmh
[_NET_DESKTOP_GEOMETRY
].atom
, XCB_ATOM_CARDINAL
, 32, 2,
4190 /* Set region bar border to focus_color. */
4191 xcb_change_window_attributes(conn
, r
->bar
->id
,
4192 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
4196 /* Update the focus window frame on the now unfocused region. */
4197 if (rf
&& rf
->ws
->focus
)
4198 draw_frame(rf
->ws
->focus
);
4200 ewmh_update_current_desktop();
4204 focus_region(struct swm_region
*r
)
4207 struct swm_region
*old_r
;
4212 old_r
= r
->s
->r_focus
;
4215 nfw
= get_region_focus(r
);
4219 /* New region is empty; need to manually unfocus win. */
4221 unfocus_win(old_r
->ws
->focus
);
4222 /* Clear bar since empty. */
4223 bar_draw(old_r
->bar
);
4226 DNPRINTF(SWM_D_FOCUS
, "focus_region: set_input_focus: %#x, "
4227 "revert-to: parent, time: 0\n", r
->id
);
4228 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4235 switchws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4237 struct swm_region
*this_r
, *other_r
;
4239 struct workspace
*new_ws
, *old_ws
;
4240 xcb_window_t none
= XCB_WINDOW_NONE
;
4241 int wsid
= args
->id
;
4242 bool unmap_old
= false;
4247 if (wsid
>= workspace_limit
)
4251 old_ws
= this_r
->ws
;
4252 new_ws
= &this_r
->s
->ws
[wsid
];
4254 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
4255 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
4257 if (new_ws
== NULL
|| old_ws
== NULL
)
4259 if (new_ws
== old_ws
)
4262 other_r
= new_ws
->r
;
4263 if (other_r
&& workspace_clamp
&&
4264 bp
->action
!= FN_RG_MOVE_NEXT
&& bp
->action
!= FN_RG_MOVE_PREV
) {
4265 DNPRINTF(SWM_D_WS
, "switchws: ws clamped.\n");
4268 DNPRINTF(SWM_D_WS
, "switchws: warping focus to region "
4269 "with ws %d.\n", wsid
);
4270 focus_region(other_r
);
4271 center_pointer(other_r
);
4277 if ((win
= old_ws
->focus
) != NULL
) {
4280 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4281 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4286 /* the other ws is visible in another region, exchange them */
4287 other_r
->ws_prior
= new_ws
;
4288 other_r
->ws
= old_ws
;
4289 old_ws
->r
= other_r
;
4291 /* the other workspace is hidden, hide this one */
4296 this_r
->ws_prior
= old_ws
;
4297 this_r
->ws
= new_ws
;
4300 /* Set focus_pending before stacking, if needed. */
4301 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
4302 validate_win(new_ws
->focus_pending
))) {
4303 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
4304 new_ws
->focus
= new_ws
->focus_prev
;
4305 new_ws
->focus_prev
= NULL
;
4308 new_ws
->state
= SWM_WS_STATE_MAPPING
;
4313 /* unmap old windows */
4315 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
4317 old_ws
->state
= SWM_WS_STATE_HIDDEN
;
4320 /* if workspaces were swapped, then don't wait to set focus */
4321 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
4322 if (new_ws
->focus_pending
) {
4323 focus_win(new_ws
->focus_pending
);
4324 new_ws
->focus_pending
= NULL
;
4328 /* Clear bar and set focus on region input win if new ws is empty. */
4329 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
4330 DNPRINTF(SWM_D_FOCUS
, "switchws: set_input_focus: %#x, "
4331 "revert-to: parent, time: 0\n", r
->id
);
4332 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4337 ewmh_update_current_desktop();
4341 new_ws
->state
= SWM_WS_STATE_MAPPED
;
4343 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
4347 cyclews(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4350 struct swm_screen
*s
= r
->s
;
4351 bool cycle_all
= false, mv
= false;
4353 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4354 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4360 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
4363 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
4366 case SWM_ARG_ID_CYCLEWS_UP
:
4367 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
4369 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
4372 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
4375 case SWM_ARG_ID_CYCLEWS_DOWN
:
4376 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
4383 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
4385 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
4389 send_to_ws(bp
, r
, &a
);
4391 switchws(bp
, r
, &a
);
4392 } while (a
.id
!= r
->ws
->idx
);
4394 DNPRINTF(SWM_D_FOCUS
, "cyclews: done\n");
4398 priorws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4404 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4405 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4407 if (r
->ws_prior
== NULL
)
4410 a
.id
= r
->ws_prior
->idx
;
4411 switchws(bp
, r
, &a
);
4412 DNPRINTF(SWM_D_FOCUS
, "priorws: done\n");
4416 focusrg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4418 int ridx
= args
->id
, i
, num_screens
;
4419 struct swm_region
*rr
= NULL
;
4423 num_screens
= get_screen_count();
4424 /* do nothing if we don't have more than one screen */
4425 if (!(num_screens
> 1 || outputs
> 1))
4428 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
4430 rr
= TAILQ_FIRST(&r
->s
->rl
);
4431 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
4432 rr
= TAILQ_NEXT(rr
, entry
);
4440 DNPRINTF(SWM_D_FOCUS
, "focusrg: done\n");
4444 cyclerg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4447 struct swm_region
*rr
= NULL
;
4450 num_screens
= get_screen_count();
4451 /* do nothing if we don't have more than one screen */
4452 if (!(num_screens
> 1 || outputs
> 1))
4456 DNPRINTF(SWM_D_FOCUS
, "cyclerg: id: %d, region: %d\n", args
->id
, i
);
4459 case SWM_ARG_ID_CYCLERG_UP
:
4460 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4461 rr
= TAILQ_NEXT(r
, entry
);
4463 rr
= TAILQ_FIRST(&screens
[i
].rl
);
4465 case SWM_ARG_ID_CYCLERG_DOWN
:
4466 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4467 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
4469 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
4478 case SWM_ARG_ID_CYCLERG_UP
:
4479 case SWM_ARG_ID_CYCLERG_DOWN
:
4484 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4485 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4487 switchws(bp
, r
, &a
);
4493 DNPRINTF(SWM_D_FOCUS
, "cyclerg: done\n");
4496 /* Sorts transients after parent. */
4498 sort_windows(struct ws_win_list
*wl
)
4500 struct ws_win
*win
, *parent
, *nxt
;
4505 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
4506 nxt
= TAILQ_NEXT(win
, entry
);
4508 parent
= find_window(win
->transient
);
4509 if (parent
== NULL
) {
4510 warnx("not possible bug");
4513 TAILQ_REMOVE(wl
, win
, entry
);
4514 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
4520 swapwin(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4522 struct ws_win
*target
, *source
;
4523 struct ws_win
*cur_focus
;
4524 struct ws_win_list
*wl
;
4528 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4529 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4531 cur_focus
= r
->ws
->focus
;
4532 if (cur_focus
== NULL
|| FULLSCREEN(cur_focus
))
4535 /* Adjust stacking in floating layer. */
4536 if (ABOVE(cur_focus
)) {
4538 case SWM_ARG_ID_SWAPPREV
:
4539 target
= TAILQ_PREV(cur_focus
, ws_win_stack
,
4541 if (target
!= NULL
&& FLOATING(target
)) {
4542 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4544 TAILQ_INSERT_BEFORE(target
, cur_focus
,
4546 update_win_stacking(cur_focus
);
4550 case SWM_ARG_ID_SWAPNEXT
:
4551 target
= TAILQ_NEXT(cur_focus
, stack_entry
);
4552 if (target
!= NULL
&& FLOATING(target
)) {
4553 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4555 TAILQ_INSERT_AFTER(&cur_focus
->ws
->stack
,
4556 target
, cur_focus
, stack_entry
);
4557 update_win_stacking(cur_focus
);
4565 if (r
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4568 clear_maximized(r
->ws
);
4571 wl
= &source
->ws
->winlist
;
4574 case SWM_ARG_ID_SWAPPREV
:
4576 source
= find_window(source
->transient
);
4577 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
4578 if (target
&& target
->transient
)
4579 target
= find_window(target
->transient
);
4580 TAILQ_REMOVE(wl
, source
, entry
);
4582 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4584 TAILQ_INSERT_BEFORE(target
, source
, entry
);
4586 case SWM_ARG_ID_SWAPNEXT
:
4587 target
= TAILQ_NEXT(source
, entry
);
4588 /* move the parent and let the sort handle the move */
4590 source
= find_window(source
->transient
);
4591 TAILQ_REMOVE(wl
, source
, entry
);
4593 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4595 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
4597 case SWM_ARG_ID_SWAPMAIN
:
4598 target
= TAILQ_FIRST(wl
);
4599 if (target
== source
) {
4600 if (source
->ws
->focus_prev
!= NULL
&&
4601 source
->ws
->focus_prev
!= target
)
4602 source
= source
->ws
->focus_prev
;
4606 if (target
== NULL
|| source
== NULL
)
4608 source
->ws
->focus_prev
= target
;
4609 TAILQ_REMOVE(wl
, target
, entry
);
4610 TAILQ_INSERT_BEFORE(source
, target
, entry
);
4611 TAILQ_REMOVE(wl
, source
, entry
);
4612 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4614 case SWM_ARG_ID_MOVELAST
:
4615 TAILQ_REMOVE(wl
, source
, entry
);
4616 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4619 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
4624 ewmh_update_client_list();
4630 DNPRINTF(SWM_D_MOVE
, "swapwin: done\n");
4634 get_focus_prev(struct ws_win
*win
)
4636 struct ws_win
*winfocus
= NULL
;
4637 struct ws_win
*cur_focus
= NULL
;
4638 struct ws_win_list
*wl
= NULL
;
4639 struct workspace
*ws
= NULL
;
4641 if (!(win
&& win
->ws
))
4646 cur_focus
= ws
->focus
;
4648 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: win %#x, cur_focus: %#x, "
4649 "focus_prev: %#x\n", WINID(win
), WINID(cur_focus
),
4650 WINID(ws
->focus_prev
));
4652 /* pickle, just focus on whatever */
4653 if (cur_focus
== NULL
) {
4654 /* use prev_focus if valid */
4655 if (ws
->focus_prev
&& find_window(ws
->focus_prev
->id
))
4656 winfocus
= ws
->focus_prev
;
4660 /* if transient focus on parent */
4661 if (TRANS(cur_focus
)) {
4662 winfocus
= find_window(cur_focus
->transient
);
4666 /* if in max_stack try harder */
4667 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
4668 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
4669 if (cur_focus
!= ws
->focus_prev
)
4670 winfocus
= ws
->focus_prev
;
4672 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
4677 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
4679 if (winfocus
== NULL
|| winfocus
== win
) {
4680 switch (focus_close
) {
4681 case SWM_STACK_BOTTOM
:
4682 TAILQ_FOREACH(winfocus
, wl
, entry
)
4683 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4687 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4688 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4691 case SWM_STACK_ABOVE
:
4692 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
4693 while (winfocus
&& ICONIC(winfocus
))
4694 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4696 if (winfocus
== NULL
) {
4697 if (focus_close_wrap
) {
4698 TAILQ_FOREACH(winfocus
, wl
, entry
)
4699 if (!ICONIC(winfocus
) &&
4700 winfocus
!= cur_focus
)
4703 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4705 if (!ICONIC(winfocus
) &&
4706 winfocus
!= cur_focus
)
4711 case SWM_STACK_BELOW
:
4712 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
4713 while (winfocus
&& ICONIC(winfocus
))
4714 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
4717 if (winfocus
== NULL
) {
4718 if (focus_close_wrap
) {
4719 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4721 if (!ICONIC(winfocus
) &&
4722 winfocus
!= cur_focus
)
4725 TAILQ_FOREACH(winfocus
, wl
, entry
)
4726 if (!ICONIC(winfocus
) &&
4727 winfocus
!= cur_focus
)
4735 if (winfocus
== NULL
||
4736 (winfocus
&& (ICONIC(winfocus
) || winfocus
== cur_focus
))) {
4737 if (focus_default
== SWM_STACK_TOP
) {
4738 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4739 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4742 TAILQ_FOREACH(winfocus
, wl
, entry
)
4743 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4750 return get_focus_magic(winfocus
);
4754 get_region_focus(struct swm_region
*r
)
4756 struct ws_win
*winfocus
= NULL
;
4761 if (r
->ws
->focus
&& !ICONIC(r
->ws
->focus
))
4762 winfocus
= r
->ws
->focus
;
4763 else if (r
->ws
->focus_prev
&& !ICONIC(r
->ws
->focus_prev
))
4764 winfocus
= r
->ws
->focus_prev
;
4766 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
4767 if (!ICONIC(winfocus
))
4770 return get_focus_magic(winfocus
);
4774 focus(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4776 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
4777 struct ws_win_list
*wl
= NULL
;
4778 struct workspace
*ws
= NULL
;
4785 cur_focus
= r
->ws
->focus
;
4789 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d, cur_focus: %#x\n", args
->id
,
4792 /* Make sure an uniconified window has focus, if one exists. */
4793 if (cur_focus
== NULL
) {
4794 cur_focus
= TAILQ_FIRST(wl
);
4795 while (cur_focus
!= NULL
&& ICONIC(cur_focus
))
4796 cur_focus
= TAILQ_NEXT(cur_focus
, entry
);
4798 DNPRINTF(SWM_D_FOCUS
, "focus: new cur_focus: %#x\n",
4803 case SWM_ARG_ID_FOCUSPREV
:
4804 if (cur_focus
== NULL
)
4807 winfocus
= cur_focus
;
4809 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
4810 if (winfocus
== NULL
)
4811 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
4812 if (winfocus
== cur_focus
)
4814 } while (winfocus
&& (ICONIC(winfocus
) ||
4815 winfocus
->id
== cur_focus
->transient
||
4816 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4817 winfocus
->transient
== cur_focus
->transient
) ||
4818 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4820 case SWM_ARG_ID_FOCUSNEXT
:
4821 if (cur_focus
== NULL
)
4824 winfocus
= cur_focus
;
4826 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4827 if (winfocus
== NULL
)
4828 winfocus
= TAILQ_FIRST(wl
);
4829 if (winfocus
== cur_focus
)
4831 } while (winfocus
&& (ICONIC(winfocus
) ||
4832 winfocus
->id
== cur_focus
->transient
||
4833 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4834 winfocus
->transient
== cur_focus
->transient
) ||
4835 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4837 case SWM_ARG_ID_FOCUSMAIN
:
4838 if (cur_focus
== NULL
)
4841 winfocus
= TAILQ_FIRST(wl
);
4842 if (winfocus
== cur_focus
)
4843 winfocus
= cur_focus
->ws
->focus_prev
;
4845 case SWM_ARG_ID_FOCUSURGENT
:
4846 /* Search forward for the next urgent window. */
4850 for (i
= 0; i
<= workspace_limit
; ++i
) {
4852 head
= TAILQ_FIRST(&r
->s
->ws
[(ws
->idx
+ i
) %
4853 workspace_limit
].winlist
);
4856 if (head
== cur_focus
) {
4858 winfocus
= cur_focus
;
4861 } else if (get_urgent(head
)) {
4866 head
= TAILQ_NEXT(head
, entry
);
4873 /* Switch ws if new focus is on a different ws. */
4874 if (winfocus
&& winfocus
->ws
!= ws
) {
4875 a
.id
= winfocus
->ws
->idx
;
4876 switchws(bp
, r
, &a
);
4883 if (clear_maximized(ws
) > 0)
4886 focus_win(get_focus_magic(winfocus
));
4891 DNPRINTF(SWM_D_FOCUS
, "focus: done\n");
4895 focus_pointer(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4899 /* Not needed for buttons since this is already done in buttonpress. */
4900 if (bp
->type
== KEYBIND
) {
4901 focus_win(get_pointer_win(r
->s
->root
));
4907 cycle_layout(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4909 struct workspace
*ws
= r
->ws
;
4911 /* suppress unused warning since var is needed */
4915 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
4918 if (ws
->cur_layout
->l_stack
== NULL
)
4919 ws
->cur_layout
= &layouts
[0];
4921 clear_maximized(ws
);
4926 focus_win(get_region_focus(r
));
4933 stack_config(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4935 struct workspace
*ws
= r
->ws
;
4939 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
4942 if (clear_maximized(ws
) > 0)
4945 if (ws
->cur_layout
->l_config
!= NULL
)
4946 ws
->cur_layout
->l_config(ws
, args
->id
);
4948 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
4957 stack(struct swm_region
*r
) {
4958 struct swm_geometry g
;
4959 struct swm_region
*r_prev
;
4965 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
4967 /* Adjust stack area for region bar and padding. */
4969 g
.x
+= region_padding
;
4970 g
.y
+= region_padding
;
4971 g
.w
-= 2 * border_width
+ 2 * region_padding
;
4972 g
.h
-= 2 * border_width
+ 2 * region_padding
;
4973 if (bar_enabled
&& r
->ws
->bar_enabled
) {
4979 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d (screen: "
4980 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4981 r
->ws
->idx
, r
->s
->idx
, get_region_index(r
), g
.x
, g
.y
, g
.w
, g
.h
);
4983 r_prev
= TAILQ_PREV(r
, swm_region_list
, entry
);
4985 /* Stack bar/input relative to prev. region. */
4986 val
[1] = XCB_STACK_MODE_ABOVE
;
4988 val
[0] = r_prev
->id
;
4989 DNPRINTF(SWM_D_STACK
, "stack: region input %#x "
4990 "relative to %#x.\n", r
->id
, val
[0]);
4991 xcb_configure_window(conn
, r
->id
,
4992 XCB_CONFIG_WINDOW_SIBLING
|
4993 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4995 val
[0] = r_prev
->bar
->id
;
4996 DNPRINTF(SWM_D_STACK
, "stack: region bar %#x "
4997 "relative to %#x.\n", r
->bar
->id
, val
[0]);
4998 xcb_configure_window(conn
, r
->bar
->id
,
4999 XCB_CONFIG_WINDOW_SIBLING
|
5000 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
5003 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
5004 r
->ws
->cur_layout
->l_string(r
->ws
);
5005 /* save r so we can track region changes */
5011 DNPRINTF(SWM_D_STACK
, "stack: end\n");
5015 store_float_geom(struct ws_win
*win
)
5017 if (win
== NULL
|| win
->ws
->r
== NULL
)
5020 /* retain window geom and region geom */
5021 win
->g_float
= win
->g
;
5022 win
->g_float
.x
-= X(win
->ws
->r
);
5023 win
->g_float
.y
-= Y(win
->ws
->r
);
5024 win
->g_floatvalid
= true;
5025 DNPRINTF(SWM_D_MISC
, "store_float_geom: win %#x, g: (%d,%d)"
5026 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
5027 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
5028 win
->g_float
.w
, win
->g_float
.h
);
5032 load_float_geom(struct ws_win
*win
)
5034 if (win
== NULL
|| win
->ws
->r
== NULL
)
5037 if (win
->g_floatvalid
) {
5038 win
->g
= win
->g_float
;
5039 X(win
) += X(win
->ws
->r
);
5040 Y(win
) += Y(win
->ws
->r
);
5041 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g: (%d,%d)"
5042 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
5045 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g_float "
5046 "is not set.\n", win
->id
);
5051 update_floater(struct ws_win
*win
)
5053 struct workspace
*ws
;
5054 struct swm_region
*r
;
5056 DNPRINTF(SWM_D_MISC
, "update_floater: win %#x\n", WINID(win
));
5063 if ((r
= ws
->r
) == NULL
)
5066 win
->bordered
= true;
5068 if (FULLSCREEN(win
)) {
5069 /* _NET_WM_FULLSCREEN: fullscreen without border. */
5070 if (!win
->g_floatvalid
)
5071 store_float_geom(win
);
5074 win
->bordered
= false;
5075 } else if (MAXIMIZED(win
)) {
5076 /* Maximize: like a single stacked window. */
5077 if (!win
->g_floatvalid
)
5078 store_float_geom(win
);
5082 if (bar_enabled
&& ws
->bar_enabled
&& !maximize_hide_bar
) {
5084 Y(win
) += bar_height
;
5085 HEIGHT(win
) -= bar_height
;
5086 } else if (disable_border
) {
5087 win
->bordered
= false;
5090 if (win
->bordered
) {
5091 /* Window geometry excludes frame. */
5092 X(win
) += border_width
;
5093 Y(win
) += border_width
;
5094 HEIGHT(win
) -= 2 * border_width
;
5095 WIDTH(win
) -= 2 * border_width
;
5098 /* Normal floating window. */
5099 /* Update geometry if new region. */
5101 load_float_geom(win
);
5103 if (((win
->quirks
& SWM_Q_FULLSCREEN
) &&
5104 WIDTH(win
) >= WIDTH(r
) && HEIGHT(win
) >= HEIGHT(r
)) ||
5105 ((!WS_FOCUSED(win
->ws
) || win
->ws
->focus
!= win
) &&
5106 (win
->quirks
& SWM_Q_MINIMALBORDER
))) {
5108 win
->bordered
= false;
5109 } else if (!MANUAL(win
)) {
5110 if (TRANS(win
) && (win
->quirks
& SWM_Q_TRANSSZ
)) {
5111 /* Adjust size on TRANSSZ quirk. */
5112 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
5113 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
5116 if (!(win
->quirks
& SWM_Q_ANYWHERE
)) {
5118 * Floaters and transients are auto-centred
5119 * unless manually moved, resized or ANYWHERE
5122 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2;
5123 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2;
5124 store_float_geom(win
);
5129 /* Ensure at least 1 pixel of the window is in the region. */
5130 region_containment(win
, r
, SWM_CW_ALLSIDES
);
5136 * Send keystrokes to terminal to decrease/increase the font size as the
5137 * window size changes.
5140 adjust_font(struct ws_win
*win
)
5142 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
5143 ABOVE(win
) || TRANS(win
))
5146 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
5147 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
5148 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
5149 win
->font_size_boundary
[win
->font_steps
] =
5150 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
5153 win
->last_inc
= win
->sh
.width_inc
;
5154 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
5155 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
5156 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
5159 win
->last_inc
= win
->sh
.width_inc
;
5160 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
5164 #define SWAPXY(g) do { \
5166 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
5167 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
5170 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, bool flip
)
5172 struct swm_geometry cell
, r_g
= *g
;
5174 int i
= 0, j
= 0, s
= 0, stacks
= 0;
5175 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
5176 int hrh
= 0, extra
= 0, h_slice
= 0, last_h
= 0;
5177 int split
= 0, colno
= 0;
5178 int winno
, mwin
= 0, msize
= 0;
5179 int remain
, missing
, v_slice
, mscale
;
5180 bool bordered
= true, reconfigure
= false;
5183 * cell: geometry for window, including frame.
5184 * mwin: # of windows in master area.
5185 * mscale: size increment of master area.
5186 * stacks: # of stack columns
5189 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
5190 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
5192 memset(&cell
, 0, sizeof(cell
));
5194 /* Prepare tiling variables, if needed. */
5195 if ((winno
= count_win(ws
, false)) > 0) {
5196 /* Find first tiled window. */
5197 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
5198 if (!FLOATING(win
) && !ICONIC(win
))
5201 /* Take into account size hints of first tiled window. */
5203 w_inc
= win
->sh
.width_inc
;
5204 w_base
= win
->sh
.base_width
;
5205 mwin
= ws
->l_state
.horizontal_mwin
;
5206 mscale
= ws
->l_state
.horizontal_msize
;
5207 stacks
= ws
->l_state
.horizontal_stacks
;
5210 w_inc
= win
->sh
.height_inc
;
5211 w_base
= win
->sh
.base_height
;
5212 mwin
= ws
->l_state
.vertical_mwin
;
5213 mscale
= ws
->l_state
.vertical_msize
;
5214 stacks
= ws
->l_state
.vertical_stacks
;
5218 cell
.x
+= border_width
;
5219 cell
.y
+= border_width
;
5221 if (stacks
> winno
- mwin
)
5222 stacks
= winno
- mwin
;
5226 h_slice
= r_g
.h
/ SWM_H_SLICE
;
5227 if (mwin
&& winno
> mwin
) {
5228 v_slice
= r_g
.w
/ SWM_V_SLICE
;
5232 cell
.w
= v_slice
* mscale
;
5234 if (w_inc
> 1 && w_inc
< v_slice
) {
5235 /* Adjust for requested size increment. */
5236 remain
= (cell
.w
- w_base
) % w_inc
;
5242 cell
.x
+= r_g
.w
- msize
;
5245 colno
= split
= winno
/ stacks
;
5246 cell
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
5247 2 * border_width
) / stacks
);
5250 hrh
= r_g
.h
/ colno
;
5251 extra
= r_g
.h
- (colno
* hrh
);
5252 cell
.h
= hrh
- 2 * border_width
;
5253 i
= j
= 0, s
= stacks
;
5256 /* Update window geometry. */
5257 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
5261 if (FLOATING(win
)) {
5262 update_floater(win
);
5267 if (split
&& i
== split
) {
5268 colno
= (winno
- mwin
) / stacks
;
5269 if (s
<= (winno
- mwin
) % stacks
)
5272 hrh
= r_g
.h
/ colno
;
5273 extra
= r_g
.h
- (colno
* hrh
);
5276 cell
.x
+= cell
.w
+ 2 * border_width
+
5279 cell
.w
= (r_g
.w
- msize
-
5280 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
5282 cell
.w
+= (r_g
.w
- msize
-
5283 (stacks
* (2 * border_width
+ tile_gap
))) %
5287 cell
.x
-= cell
.w
+ 2 * border_width
+
5293 cell
.h
= hrh
- 2 * border_width
- tile_gap
;
5296 h_inc
= win
->sh
.width_inc
;
5297 h_base
= win
->sh
.base_width
;
5299 h_inc
= win
->sh
.height_inc
;
5300 h_base
= win
->sh
.base_height
;
5303 if (j
== colno
- 1) {
5304 cell
.h
= hrh
+ extra
;
5305 } else if (h_inc
> 1 && h_inc
< h_slice
) {
5306 /* adjust for window's requested size increment */
5307 remain
= (cell
.h
- h_base
) % h_inc
;
5308 missing
= h_inc
- remain
;
5310 if (missing
<= extra
|| j
== 0) {
5320 cell
.y
= r_g
.y
+ border_width
;
5322 cell
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
5324 /* Window coordinates exclude frame. */
5326 if (winno
> 1 || !disable_border
||
5327 (bar_enabled
&& ws
->bar_enabled
)) {
5334 if (X(win
) != cell
.y
|| Y(win
) != cell
.x
||
5335 WIDTH(win
) != cell
.h
|| HEIGHT(win
) != cell
.w
) {
5339 WIDTH(win
) = cell
.h
;
5340 HEIGHT(win
) = cell
.w
;
5343 if (X(win
) != cell
.x
|| Y(win
) != cell
.y
||
5344 WIDTH(win
) != cell
.w
|| HEIGHT(win
) != cell
.h
) {
5348 WIDTH(win
) = cell
.w
;
5349 HEIGHT(win
) = cell
.h
;
5354 X(win
) -= border_width
;
5355 Y(win
) -= border_width
;
5356 WIDTH(win
) += 2 * border_width
;
5357 HEIGHT(win
) += 2 * border_width
;
5360 if (bordered
!= win
->bordered
) {
5362 win
->bordered
= bordered
;
5375 /* Stack all windows from bottom up. */
5376 TAILQ_FOREACH_REVERSE(win
, &ws
->stack
, ws_win_stack
, stack_entry
)
5378 update_win_stacking(win
);
5380 /* Map all windows from top down. */
5381 TAILQ_FOREACH(win
, &ws
->stack
, stack_entry
)
5385 DNPRINTF(SWM_D_STACK
, "stack_master: done\n");
5389 vertical_config(struct workspace
*ws
, int id
)
5391 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
5395 case SWM_ARG_ID_STACKRESET
:
5396 case SWM_ARG_ID_STACKINIT
:
5397 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
5398 ws
->l_state
.vertical_mwin
= 1;
5399 ws
->l_state
.vertical_stacks
= 1;
5401 case SWM_ARG_ID_MASTERSHRINK
:
5402 if (ws
->l_state
.vertical_msize
> 1)
5403 ws
->l_state
.vertical_msize
--;
5405 case SWM_ARG_ID_MASTERGROW
:
5406 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
5407 ws
->l_state
.vertical_msize
++;
5409 case SWM_ARG_ID_MASTERADD
:
5410 ws
->l_state
.vertical_mwin
++;
5412 case SWM_ARG_ID_MASTERDEL
:
5413 if (ws
->l_state
.vertical_mwin
> 0)
5414 ws
->l_state
.vertical_mwin
--;
5416 case SWM_ARG_ID_STACKBALANCE
:
5417 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ (ws
->l_state
.vertical_stacks
+ 1);
5419 case SWM_ARG_ID_STACKINC
:
5420 ws
->l_state
.vertical_stacks
++;
5422 case SWM_ARG_ID_STACKDEC
:
5423 if (ws
->l_state
.vertical_stacks
> 1)
5424 ws
->l_state
.vertical_stacks
--;
5426 case SWM_ARG_ID_FLIPLAYOUT
:
5427 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
5435 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5437 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
5439 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
5443 horizontal_config(struct workspace
*ws
, int id
)
5445 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
5448 case SWM_ARG_ID_STACKRESET
:
5449 case SWM_ARG_ID_STACKINIT
:
5450 ws
->l_state
.horizontal_mwin
= 1;
5451 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
5452 ws
->l_state
.horizontal_stacks
= 1;
5454 case SWM_ARG_ID_MASTERSHRINK
:
5455 if (ws
->l_state
.horizontal_msize
> 1)
5456 ws
->l_state
.horizontal_msize
--;
5458 case SWM_ARG_ID_MASTERGROW
:
5459 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
5460 ws
->l_state
.horizontal_msize
++;
5462 case SWM_ARG_ID_MASTERADD
:
5463 ws
->l_state
.horizontal_mwin
++;
5465 case SWM_ARG_ID_MASTERDEL
:
5466 if (ws
->l_state
.horizontal_mwin
> 0)
5467 ws
->l_state
.horizontal_mwin
--;
5469 case SWM_ARG_ID_STACKBALANCE
:
5470 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ (ws
->l_state
.horizontal_stacks
+ 1);
5472 case SWM_ARG_ID_STACKINC
:
5473 ws
->l_state
.horizontal_stacks
++;
5475 case SWM_ARG_ID_STACKDEC
:
5476 if (ws
->l_state
.horizontal_stacks
> 1)
5477 ws
->l_state
.horizontal_stacks
--;
5479 case SWM_ARG_ID_FLIPLAYOUT
:
5480 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
5488 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5490 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
5492 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
5495 /* fullscreen view */
5497 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5499 struct swm_geometry gg
= *g
;
5500 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
, *tmpw
;
5503 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
5508 winno
= count_win(ws
, false);
5509 if (winno
== 0 && count_win(ws
, true) == 0)
5512 /* Figure out which top level window should be visible. */
5513 if (ws
->focus_pending
)
5514 win
= ws
->focus_pending
;
5517 else if (ws
->focus_prev
)
5518 win
= ws
->focus_prev
;
5520 win
= TAILQ_FIRST(&ws
->winlist
);
5522 DNPRINTF(SWM_D_STACK
, "max_stack: focus_pending: %#x, focus: %#x, "
5523 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws
->focus_pending
),
5524 WINID(ws
->focus
), WINID(ws
->focus_prev
),
5525 WINID(TAILQ_FIRST(&ws
->winlist
)), win
->id
);
5527 /* Update window geometry. */
5528 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5537 /* Set maximized flag for all maxed windows. */
5538 if (!MAXIMIZED(w
)) {
5539 /* Preserve floating geometry. */
5541 store_float_geom(w
);
5543 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_MAXIMIZED
);
5544 ewmh_update_wm_state(w
);
5547 /* Only reconfigure if necessary. */
5548 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
5549 HEIGHT(w
) != gg
.h
) {
5552 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
)) {
5553 w
->bordered
= false;
5554 WIDTH(w
) += 2 * border_width
;
5555 HEIGHT(w
) += 2 * border_width
;
5558 X(w
) += border_width
;
5559 Y(w
) += border_width
;
5566 /* If transient, stack parent and its children. */
5567 if (TRANS(win
) && (parent
= find_window(win
->transient
))) {
5568 raise_window(parent
);
5570 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5571 if (w
->transient
== parent
->id
)
5575 /* Make sure focus window is on top. */
5578 /* Stack any children of focus window. */
5579 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5580 if (w
->transient
== win
->id
)
5583 /* Map all windows. */
5584 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
5590 send_to_rg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5592 int ridx
= args
->id
, i
, num_screens
;
5593 struct swm_region
*rr
= NULL
;
5596 num_screens
= get_screen_count();
5597 /* do nothing if we don't have more than one screen */
5598 if (!(num_screens
> 1 || outputs
> 1))
5601 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
5603 rr
= TAILQ_FIRST(&r
->s
->rl
);
5604 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
5605 rr
= TAILQ_NEXT(rr
, entry
);
5612 send_to_ws(bp
, r
, &a
);
5616 region_under(struct swm_screen
*s
, int x
, int y
)
5618 struct swm_region
*r
= NULL
;
5620 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
5621 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
5622 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
5623 WIDTH(r
), HEIGHT(r
), x
, y
);
5624 if (X(r
) <= x
&& x
< MAX_X(r
))
5625 if (Y(r
) <= y
&& y
< MAX_Y(r
))
5632 /* Transfer focused window to target workspace and focus. */
5634 send_to_ws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5636 int wsid
= args
->id
;
5637 struct ws_win
*win
= NULL
;
5641 if (r
&& r
->ws
&& r
->ws
->focus
)
5646 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win %#x, ws %d\n", win
->id
, wsid
);
5648 if (wsid
< 0 || wsid
>= workspace_limit
)
5651 if (win
->ws
->idx
== wsid
)
5654 win_to_ws(win
, wsid
, true);
5656 /* Set new focus on target ws. */
5657 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5658 win
->ws
->focus_prev
= win
->ws
->focus
;
5659 win
->ws
->focus
= win
;
5660 win
->ws
->focus_pending
= NULL
;
5662 if (win
->ws
->focus_prev
)
5663 draw_frame(win
->ws
->focus_prev
);
5666 DNPRINTF(SWM_D_STACK
, "send_to_ws: focus_pending: %#x, focus: %#x, "
5667 "focus_prev: %#x, first: %#x, win: %#x\n",
5668 WINID(r
->ws
->focus_pending
), WINID(r
->ws
->focus
),
5669 WINID(r
->ws
->focus_prev
), WINID(TAILQ_FIRST(&r
->ws
->winlist
)),
5672 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5673 ewmh_update_wm_state(win
);
5675 /* Restack focused region. */
5678 /* If destination ws has a region, restack. */
5681 load_float_geom(win
);
5686 /* Set new focus on current ws. */
5687 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5688 if (r
->ws
->focus
!= NULL
) {
5689 focus_win(r
->ws
->focus
);
5691 DNPRINTF(SWM_D_FOCUS
, "send_to_ws: set_input_focus: "
5692 "%#x, revert-to: parent, time: 0\n", r
->id
);
5693 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
5694 r
->id
, XCB_CURRENT_TIME
);
5703 /* Transfer focused window to region-relative workspace and focus. */
5705 send_to_rg_relative(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5708 struct swm_region
*r_other
;
5710 if (args
->id
== 1) {
5711 r_other
= TAILQ_NEXT(r
, entry
);
5712 if (r_other
== NULL
)
5713 r_other
= TAILQ_FIRST(&r
->s
->rl
);
5715 r_other
= TAILQ_PREV(r
, swm_region_list
, entry
);
5716 if (r_other
== NULL
)
5717 r_other
= TAILQ_LAST(&r
->s
->rl
, swm_region_list
);
5720 /* Map relative to absolute */
5722 args_abs
.id
= r_other
->ws
->idx
;
5724 send_to_ws(bp
, r
, &args_abs
);
5728 win_to_ws(struct ws_win
*win
, int wsid
, bool unfocus
)
5730 struct ws_win
*parent
;
5731 struct workspace
*ws
, *nws
, *pws
;
5733 if (wsid
< 0 || wsid
>= workspace_limit
)
5736 if (win
->ws
->idx
== wsid
)
5740 nws
= &win
->s
->ws
[wsid
];
5742 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win %#x, ws %d -> %d\n", win
->id
,
5745 /* Cleanup focus on source ws. */
5746 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
5747 (ws
->focus
== win
|| ws
->focus_pending
== win
))
5748 ws
->focus_pending
= get_focus_prev(win
);
5750 /* Move the parent if this is a transient window. */
5752 parent
= find_window(win
->transient
);
5755 /* Set new focus in parent's ws if needed. */
5756 if (pws
->focus
== parent
) {
5757 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5758 pws
->focus_pending
=
5759 get_focus_prev(parent
);
5761 unfocus_win(parent
);
5763 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5764 pws
->focus
= pws
->focus_pending
;
5765 pws
->focus_pending
= NULL
;
5769 /* Don't unmap parent if new ws is visible */
5771 unmap_window(parent
);
5774 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
5775 TAILQ_REMOVE(&ws
->stack
, parent
, stack_entry
);
5776 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
5777 TAILQ_INSERT_TAIL(&nws
->stack
, parent
, stack_entry
);
5780 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5781 "_NET_WM_DESKTOP: %d\n", wsid
);
5782 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5783 parent
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
5784 XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5791 if (ws
->focus_prev
== win
)
5792 ws
->focus_prev
= NULL
;
5794 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& ws
->focus_pending
!= NULL
) {
5795 ws
->focus
= ws
->focus_pending
;
5796 ws
->focus_pending
= NULL
;
5799 /* Don't unmap if new ws is visible */
5804 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
5805 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
5806 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
5807 TAILQ_INSERT_TAIL(&nws
->stack
, win
, stack_entry
);
5810 /* Update the window's workspace property: _NET_WM_DESKTOP */
5811 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5812 "_NET_WM_DESKTOP: %d\n", wsid
);
5813 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
5814 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5816 ewmh_update_client_list();
5818 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
5822 pressbutton(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5824 /* suppress unused warning since var is needed */
5828 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
5829 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5830 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
5831 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5835 raise_focus(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5840 /* Suppress warning. */
5844 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
5848 r
->ws
->focus_raise
= win
;
5851 /* Temporarily override stacking order also in the stack */
5852 if (!FLOATING(win
)) {
5853 val
= XCB_STACK_MODE_ABOVE
;
5854 xcb_configure_window(conn
, win
->frame
,
5855 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
5860 raise_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5862 /* Suppress warning. */
5866 if (r
== NULL
|| r
->ws
== NULL
)
5869 if (r
->ws
->focus
&& MAXIMIZED(r
->ws
->focus
))
5872 r
->ws
->always_raise
= !r
->ws
->always_raise
;
5874 /* Update focused win stacking order based on new always_raise value. */
5875 raise_window(r
->ws
->focus
);
5881 iconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5885 /* Suppress warning. */
5889 if ((w
= r
->ws
->focus
) == NULL
)
5892 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_HIDDEN
);
5893 ewmh_update_wm_state(w
);
5901 get_win_name(xcb_window_t win
)
5904 xcb_get_property_cookie_t c
;
5905 xcb_get_property_reply_t
*r
;
5907 /* First try _NET_WM_NAME for UTF-8. */
5908 c
= xcb_get_property(conn
, 0, win
, ewmh
[_NET_WM_NAME
].atom
,
5909 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5910 r
= xcb_get_property_reply(conn
, c
, NULL
);
5911 if (r
&& r
->type
== XCB_NONE
) {
5913 /* Use WM_NAME instead; no UTF-8. */
5914 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
5915 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5916 r
= xcb_get_property_reply(conn
, c
, NULL
);
5919 if (r
&& r
->type
!= XCB_NONE
&& r
->length
> 0)
5920 name
= strndup(xcb_get_property_value(r
),
5921 xcb_get_property_value_length(r
));
5926 err(1, "get_win_name: failed to allocate memory.");
5934 uniconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5943 DNPRINTF(SWM_D_MISC
, "uniconify\n");
5945 if (r
== NULL
|| r
->ws
== NULL
)
5948 /* make sure we have anything to uniconify */
5949 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5950 if (win
->ws
== NULL
)
5951 continue; /* should never happen */
5957 DNPRINTF(SWM_D_MISC
, "uniconify: count: %d\n", count
);
5963 search_resp_action
= SWM_SEARCH_UNICONIFY
;
5965 spawn_select(r
, args
, "search", &searchpid
);
5967 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5970 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5971 if (win
->ws
== NULL
)
5972 continue; /* should never happen */
5976 name
= get_win_name(win
->id
);
5977 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
5985 name_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5991 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
5997 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
5999 spawn_select(r
, args
, "name_workspace", &searchpid
);
6001 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
6004 fprintf(lfile
, "%s", "");
6009 search_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6012 struct workspace
*ws
;
6017 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
6023 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
6025 spawn_select(r
, args
, "search", &searchpid
);
6027 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
6030 for (i
= 0; i
< workspace_limit
; i
++) {
6034 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
6035 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
6042 search_win_cleanup(void)
6044 struct search_window
*sw
= NULL
;
6046 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
6047 xcb_destroy_window(conn
, sw
->indicator
);
6048 TAILQ_REMOVE(&search_wl
, sw
, entry
);
6054 search_win(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6056 struct ws_win
*win
= NULL
;
6057 struct search_window
*sw
= NULL
;
6060 xcb_screen_t
*screen
;
6061 int i
, width
, height
;
6069 XRectangle l_ibox
, l_lbox
;
6073 DNPRINTF(SWM_D_MISC
, "search_win\n");
6076 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
6078 spawn_select(r
, args
, "search", &searchpid
);
6080 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
6083 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
6084 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
6086 TAILQ_INIT(&search_wl
);
6089 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
6093 sw
= calloc(1, sizeof(struct search_window
));
6095 warn("search_win: calloc");
6097 search_win_cleanup();
6103 snprintf(s
, sizeof s
, "%d", i
);
6106 w
= xcb_generate_id(conn
);
6107 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6108 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6109 wa
[2] = screen
->default_colormap
;
6111 if (bar_font_legacy
) {
6112 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
6113 width
= l_lbox
.width
+ 4;
6114 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
6116 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
6118 width
= info
.width
+ 4;
6119 height
= bar_font
->height
+ 4;
6122 xcb_create_window(conn
, screen
->root_depth
, w
, win
->frame
, 0, 0,
6123 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
6124 screen
->root_visual
, XCB_CW_BACK_PIXEL
|
6125 XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
, wa
);
6127 xcb_map_window(conn
, w
);
6130 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
6132 if (bar_font_legacy
) {
6133 l_gcv
.graphics_exposures
= 0;
6134 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
6136 XSetForeground(display
, l_draw
,
6137 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
6139 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
6140 (bar_fs_extents
->max_logical_extent
.height
-
6141 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
6143 XFreeGC(display
, l_draw
);
6146 draw
= XftDrawCreate(display
, w
,
6147 DefaultVisual(display
, r
->s
->idx
),
6148 DefaultColormap(display
, r
->s
->idx
));
6150 XftDrawStringUtf8(draw
, &search_font_color
, bar_font
, 2,
6151 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
6152 bar_font
->descent
, (FcChar8
*)s
, len
);
6154 XftDrawDestroy(draw
);
6157 DNPRINTF(SWM_D_MISC
, "search_win: mapped win %#x\n", w
);
6159 fprintf(lfile
, "%d\n", i
);
6169 search_resp_uniconify(const char *resp
, size_t len
)
6175 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
6177 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
6180 name
= get_win_name(win
->id
);
6181 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
6186 if (strncmp(s
, resp
, len
) == 0) {
6187 /* XXX this should be a callback to generalize */
6188 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_HIDDEN
);
6189 ewmh_update_wm_state(win
);
6199 search_resp_name_workspace(const char *resp
, size_t len
)
6201 struct workspace
*ws
;
6203 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
6205 if (search_r
->ws
== NULL
)
6210 free(search_r
->ws
->name
);
6211 search_r
->ws
->name
= NULL
;
6215 ws
->name
= strdup(resp
);
6216 if (ws
->name
== NULL
) {
6217 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
6218 "strdup: %s", strerror(errno
));
6222 ewmh_update_desktop_names();
6223 ewmh_get_desktop_names();
6228 ewmh_update_desktop_names(void)
6230 char *name_list
= NULL
, *p
;
6231 int num_screens
, i
, j
, len
= 0, tot
= 0;
6233 num_screens
= get_screen_count();
6234 for (i
= 0; i
< num_screens
; ++i
) {
6235 for (j
= 0; j
< workspace_limit
; ++j
) {
6236 if (screens
[i
].ws
[j
].name
!= NULL
)
6237 len
+= strlen(screens
[i
].ws
[j
].name
);
6241 if ((name_list
= calloc(len
, sizeof(char))) == NULL
)
6242 err(1, "update_desktop_names: calloc: failed to "
6243 "allocate memory.");
6246 for (j
= 0; j
< workspace_limit
; ++j
) {
6247 if (screens
[i
].ws
[j
].name
!= NULL
) {
6248 len
= strlen(screens
[i
].ws
[j
].name
);
6249 memcpy(p
, screens
[i
].ws
[j
].name
, len
);
6258 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6259 screens
[i
].root
, ewmh
[_NET_DESKTOP_NAMES
].atom
,
6260 a_utf8_string
, 8, tot
, name_list
);
6270 ewmh_get_desktop_names(void)
6273 xcb_get_property_cookie_t c
;
6274 xcb_get_property_reply_t
*r
;
6275 int num_screens
, i
, j
, n
, k
;
6277 num_screens
= get_screen_count();
6278 for (i
= 0; i
< num_screens
; ++i
) {
6279 for (j
= 0; j
< workspace_limit
; ++j
) {
6280 free(screens
[i
].ws
[j
].name
);
6281 screens
[i
].ws
[j
].name
= NULL
;
6284 c
= xcb_get_property(conn
, 0, screens
[i
].root
,
6285 ewmh
[_NET_DESKTOP_NAMES
].atom
,
6286 a_utf8_string
, 0, UINT32_MAX
);
6287 r
= xcb_get_property_reply(conn
, c
, NULL
);
6291 names
= xcb_get_property_value(r
);
6292 n
= xcb_get_property_value_length(r
);
6294 for (j
= 0, k
= 0; j
< n
; ++j
) {
6295 if (*(names
+ j
) != '\0') {
6296 screens
[i
].ws
[k
].name
= strdup(names
+ j
);
6297 j
+= strlen(names
+ j
);
6306 ewmh_update_client_list(void)
6309 int num_screens
, i
, j
, k
= 0, count
= 0;
6312 num_screens
= get_screen_count();
6313 for (i
= 0; i
< num_screens
; ++i
) {
6314 for (j
= 0; j
< workspace_limit
; ++j
)
6315 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6318 DNPRINTF(SWM_D_PROP
, "ewmh_update_client_list: win count: %d\n",
6324 wins
= calloc(count
, sizeof(xcb_window_t
));
6326 err(1, "ewmh_update_client_list: calloc: failed to "
6327 "allocate memory.");
6329 for (j
= 0, k
= 0; j
< workspace_limit
; ++j
)
6330 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6331 wins
[k
++] = win
->id
;
6333 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6334 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
6335 XCB_ATOM_WINDOW
, 32, count
, wins
);
6342 ewmh_update_current_desktop(void)
6346 num_screens
= get_screen_count();
6347 for (i
= 0; i
< num_screens
; ++i
) {
6348 if (screens
[i
].r_focus
)
6349 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6350 screens
[i
].root
, ewmh
[_NET_CURRENT_DESKTOP
].atom
,
6351 XCB_ATOM_CARDINAL
, 32, 1,
6352 &screens
[i
].r_focus
->ws
->idx
);
6357 ewmh_update_desktops(void)
6359 int num_screens
, i
, j
;
6362 vals
= calloc(workspace_limit
* 2, sizeof(uint32_t));
6364 err(1, "ewmh_update_desktops: calloc: failed to allocate "
6367 num_screens
= get_screen_count();
6368 for (i
= 0; i
< num_screens
; i
++) {
6369 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6370 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
6371 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
6373 for (j
= 0; j
< workspace_limit
; ++j
) {
6374 if (screens
[i
].ws
[j
].r
!= NULL
) {
6375 vals
[j
* 2] = X(screens
[i
].ws
[j
].r
);
6376 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].r
);
6377 } else if (screens
[i
].ws
[j
].old_r
!= NULL
) {
6378 vals
[j
* 2] = X(screens
[i
].ws
[j
].old_r
);
6379 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].old_r
);
6381 vals
[j
* 2] = vals
[j
* 2 + 1] = 0;
6385 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6386 screens
[i
].root
, ewmh
[_NET_DESKTOP_VIEWPORT
].atom
,
6387 XCB_ATOM_CARDINAL
, 32, workspace_limit
* 2, vals
);
6394 search_resp_search_workspace(const char *resp
)
6401 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
6405 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
6412 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
6414 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
6421 switchws(NULL
, search_r
, &a
);
6425 search_resp_search_window(const char *resp
)
6430 struct search_window
*sw
;
6432 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
6436 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
6441 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
6443 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
6450 TAILQ_FOREACH(sw
, &search_wl
, entry
)
6451 if (idx
== sw
->idx
) {
6457 #define MAX_RESP_LEN 1024
6460 search_do_resp(void)
6466 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
6471 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
6472 warn("search: calloc");
6476 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
6478 warn("search: read error");
6481 resp
[rbytes
] = '\0';
6484 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
6485 * newline, so work around that by sanitizing the resp now.
6487 resp
[strcspn(resp
, "\n")] = '\0';
6490 switch (search_resp_action
) {
6491 case SWM_SEARCH_UNICONIFY
:
6492 search_resp_uniconify(resp
, len
);
6494 case SWM_SEARCH_NAME_WORKSPACE
:
6495 search_resp_name_workspace(resp
, len
);
6497 case SWM_SEARCH_SEARCH_WORKSPACE
:
6498 search_resp_search_workspace(resp
);
6500 case SWM_SEARCH_SEARCH_WINDOW
:
6501 search_resp_search_window(resp
);
6506 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
6507 search_win_cleanup();
6509 search_resp_action
= SWM_SEARCH_NONE
;
6510 close(select_resp_pipe
[0]);
6517 wkill(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6521 DNPRINTF(SWM_D_MISC
, "wkill: win %#x, id: %d\n", WINID(r
->ws
->focus
),
6524 if (r
->ws
->focus
== NULL
)
6527 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
6528 xcb_kill_client(conn
, r
->ws
->focus
->id
);
6530 if (r
->ws
->focus
->can_delete
)
6531 client_msg(r
->ws
->focus
, a_delete
, 0);
6537 clear_maximized(struct workspace
*ws
)
6542 /* Clear any maximized win(s) on ws, from bottom up. */
6543 TAILQ_FOREACH_REVERSE(w
, &ws
->stack
, ws_win_stack
, stack_entry
)
6545 ewmh_apply_flags(w
, w
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
6546 ewmh_update_wm_state(w
);
6554 maximize_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6556 struct ws_win
*w
= r
->ws
->focus
;
6558 /* suppress unused warning since var is needed */
6565 DNPRINTF(SWM_D_MISC
, "maximize_toggle: win %#x\n", w
->id
);
6570 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6573 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_MAXIMIZED
);
6574 ewmh_update_wm_state(w
);
6578 if (w
== w
->ws
->focus
)
6583 DNPRINTF(SWM_D_MISC
, "maximize_toggle: done\n");
6587 floating_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6589 struct ws_win
*w
= r
->ws
->focus
;
6591 /* suppress unused warning since var is needed */
6598 DNPRINTF(SWM_D_MISC
, "floating_toggle: win %#x\n", w
->id
);
6600 if (FULLSCREEN(w
) || TRANS(w
))
6603 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6606 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_ABOVE
);
6607 ewmh_update_wm_state(w
);
6611 if (w
== w
->ws
->focus
)
6616 DNPRINTF(SWM_D_MISC
, "floating_toggle: done\n");
6620 fullscreen_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6622 struct ws_win
*w
= r
->ws
->focus
;
6624 /* suppress unused warning since var is needed */
6631 DNPRINTF(SWM_D_MISC
, "fullscreen_toggle: win %#x\n", w
->id
);
6633 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_FULLSCREEN
);
6634 ewmh_update_wm_state(w
);
6638 if (w
== w
->ws
->focus
)
6643 DNPRINTF(SWM_D_MISC
, "fullscreen_toggle: done\n");
6646 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
6648 struct swm_geometry g
= r
->g
;
6649 int rt
, lt
, tp
, bm
, bw
;
6651 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
6654 * Perpendicular distance of each side of the window to the respective
6655 * side of the region boundary. Positive values indicate the side of
6656 * the window has passed beyond the region boundary.
6658 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) - MAX_X(r
) : bw
;
6659 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) : bw
;
6660 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) - MAX_Y(r
) : bw
;
6661 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) : bw
;
6663 DNPRINTF(SWM_D_MISC
, "region_containment: win %#x, rt: %d, lt: %d, "
6664 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
6665 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
6666 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
6669 * Disable containment if any of the flagged sides went beyond the
6670 * containment boundary, or if containment is disabled.
6672 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
6673 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
6674 /* Make sure window has at least 1 pixel in the region */
6675 g
.x
+= 1 - WIDTH(win
);
6676 g
.y
+= 1 - HEIGHT(win
);
6677 g
.w
+= 2 * WIDTH(win
) - 2;
6678 g
.h
+= 2 * HEIGHT(win
) - 2;
6681 constrain_window(win
, &g
, &opts
);
6684 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6686 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
6688 DNPRINTF(SWM_D_MISC
, "constrain_window: win %#x, (x,y) w x h: "
6689 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6690 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
6691 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
6692 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
6693 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
6694 YESNO(*opts
& SWM_CW_RESIZABLE
));
6696 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) > b
->x
+ b
->w
) {
6697 if (*opts
& SWM_CW_RESIZABLE
)
6698 WIDTH(win
) = b
->x
+ b
->w
- X(win
);
6700 X(win
) = b
->x
+ b
->w
- WIDTH(win
);
6703 if ((*opts
& SWM_CW_LEFT
) && X(win
) < b
->x
) {
6704 if (*opts
& SWM_CW_RESIZABLE
)
6705 WIDTH(win
) -= b
->x
- X(win
);
6710 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) > b
->y
+ b
->h
) {
6711 if (*opts
& SWM_CW_RESIZABLE
)
6712 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
);
6714 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
);
6717 if ((*opts
& SWM_CW_TOP
) && Y(win
) < b
->y
) {
6718 if (*opts
& SWM_CW_RESIZABLE
)
6719 HEIGHT(win
) -= b
->y
- Y(win
);
6724 if (*opts
& SWM_CW_RESIZABLE
) {
6727 if (HEIGHT(win
) < 1)
6733 draw_frame(struct ws_win
*win
)
6735 xcb_rectangle_t rect
[4];
6739 if (win
->frame
== XCB_WINDOW_NONE
) {
6740 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x not "
6741 "reparented.\n", win
->id
);
6745 if (!win
->bordered
) {
6746 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x frame "
6747 "disabled\n", win
->id
);
6750 if (WS_FOCUSED(win
->ws
) && win
->ws
->focus
== win
)
6751 pixel
= MAXIMIZED(win
) ?
6752 &win
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].pixel
:
6753 &win
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6755 pixel
= MAXIMIZED(win
) ?
6756 &win
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].pixel
:
6757 &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6759 /* Top (with corners) */
6762 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6763 rect
[n
].height
= border_width
;
6764 /* Left (without corners) */
6766 rect
[n
].y
= border_width
;
6767 rect
[n
].width
= border_width
;
6768 rect
[n
].height
= HEIGHT(win
);
6769 /* Right (without corners) */
6770 rect
[++n
].x
= border_width
+ WIDTH(win
);
6771 rect
[n
].y
= border_width
;
6772 rect
[n
].width
= border_width
;
6773 rect
[n
].height
= HEIGHT(win
);
6774 /* Bottom (with corners)*/
6776 rect
[n
].y
= border_width
+ HEIGHT(win
);
6777 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6778 rect
[n
].height
= border_width
;
6780 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, pixel
);
6781 xcb_poly_fill_rectangle(conn
, win
->frame
, win
->s
->bar_gc
, 4, rect
);
6785 update_window(struct ws_win
*win
)
6790 if (win
->frame
== XCB_WINDOW_NONE
) {
6791 DNPRINTF(SWM_D_EVENT
, "update_window: skip win %#x; "
6792 "not reparented.\n", win
->id
);
6796 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
6797 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
6798 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6800 /* Reconfigure frame. */
6801 if (win
->bordered
) {
6802 wc
[0] = X(win
) - border_width
;
6803 wc
[1] = Y(win
) - border_width
;
6804 wc
[2] = WIDTH(win
) + 2 * border_width
;
6805 wc
[3] = HEIGHT(win
) + 2 * border_width
;
6810 wc
[3] = HEIGHT(win
);
6815 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x frame %#x, (x,y) w x h: "
6816 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, win
->frame
, wc
[0],
6817 wc
[1], wc
[2], wc
[3], YESNO(win
->bordered
));
6819 xcb_configure_window(conn
, win
->frame
, mask
, wc
);
6821 /* Reconfigure client window. */
6822 wc
[0] = wc
[1] = win
->bordered
? border_width
: 0;
6824 wc
[3] = HEIGHT(win
);
6826 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x, (x,y) w x h: "
6827 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
6828 wc
[3], YESNO(win
->bordered
));
6829 xcb_configure_window(conn
, win
->id
, mask
, wc
);
6831 /* ICCCM 4.2.3 Synthetic ConfigureNotify when moved and not resized. */
6832 if ((X(win
) != win
->g_prev
.x
|| Y(win
) != win
->g_prev
.y
) &&
6833 (win
->java
|| (WIDTH(win
) == win
->g_prev
.w
&&
6834 HEIGHT(win
) == win
->g_prev
.h
))) {
6835 /* Java has special needs when moved together with a resize. */
6836 config_win(win
, NULL
);
6839 win
->g_prev
= win
->g
;
6843 SIMPLEQ_ENTRY(event
) entry
;
6844 xcb_generic_event_t
*ev
;
6846 SIMPLEQ_HEAD(event_queue
, event
) events
= SIMPLEQ_HEAD_INITIALIZER(events
);
6848 xcb_generic_event_t
*
6849 get_next_event(bool dowait
)
6852 xcb_generic_event_t
*evt
;
6854 /* Try queue first. */
6855 if ((ep
= SIMPLEQ_FIRST(&events
))) {
6857 SIMPLEQ_REMOVE_HEAD(&events
, entry
);
6860 evt
= xcb_wait_for_event(conn
);
6862 evt
= xcb_poll_for_event(conn
);
6868 put_back_event(xcb_generic_event_t
*evt
)
6871 if ((ep
= malloc(sizeof (struct event
))) == NULL
)
6872 err(1, "put_back_event: failed to allocate memory.");
6874 SIMPLEQ_INSERT_HEAD(&events
, ep
, entry
);
6877 /* Peeks at next event to detect auto-repeat. */
6879 keyrepeating(xcb_key_release_event_t
*kre
)
6881 xcb_generic_event_t
*evt
;
6883 /* Ensure repeating keypress is finished processing. */
6886 if ((evt
= get_next_event(false))) {
6887 put_back_event(evt
);
6889 if (XCB_EVENT_RESPONSE_TYPE(evt
) == XCB_KEY_PRESS
&&
6890 kre
->sequence
== evt
->sequence
&&
6891 kre
->detail
== ((xcb_key_press_event_t
*)evt
)->detail
)
6899 keybindreleased(struct binding
*bp
, xcb_key_release_event_t
*kre
)
6901 if (bp
->type
== KEYBIND
&& !keyrepeating(kre
) &&
6902 bp
->value
== xcb_key_press_lookup_keysym(syms
, kre
, 0))
6908 #define SWM_RESIZE_STEPS (50)
6911 resize_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
6913 xcb_timestamp_t timestamp
= 0;
6914 struct swm_region
*r
= NULL
;
6915 struct swm_geometry g
;
6916 int top
= 0, left
= 0;
6918 xcb_cursor_t cursor
;
6919 xcb_query_pointer_reply_t
*xpr
= NULL
;
6920 xcb_generic_event_t
*evt
;
6921 xcb_motion_notify_event_t
*mne
;
6922 bool resizing
, step
= false;
6928 if (FULLSCREEN(win
))
6931 /* In max_stack mode, should only resize transients. */
6932 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6935 DNPRINTF(SWM_D_EVENT
, "resize: win %#x, floating: %s, "
6936 "transient: %#x\n", win
->id
, YESNO(ABOVE(win
)),
6940 store_float_geom(win
);
6941 else if (!(TRANS(win
) || ABOVE(win
)))
6944 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6946 ewmh_update_wm_state(win
);
6952 /* It's possible for win to have been freed during focus_flush(). */
6953 if (validate_win(win
)) {
6954 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6959 case SWM_ARG_ID_WIDTHSHRINK
:
6960 WIDTH(win
) -= SWM_RESIZE_STEPS
;
6963 case SWM_ARG_ID_WIDTHGROW
:
6964 WIDTH(win
) += SWM_RESIZE_STEPS
;
6967 case SWM_ARG_ID_HEIGHTSHRINK
:
6968 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
6971 case SWM_ARG_ID_HEIGHTGROW
:
6972 HEIGHT(win
) += SWM_RESIZE_STEPS
;
6979 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6980 SWM_CW_HARDBOUNDARY
);
6982 store_float_geom(win
);
6986 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6987 SWM_CW_SOFTBOUNDARY
);
6990 /* get cursor offset from window root */
6991 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6998 if (xpr
->win_x
< WIDTH(win
) / 2)
7001 if (xpr
->win_y
< HEIGHT(win
) / 2)
7004 if (opt
== SWM_ARG_ID_CENTER
)
7005 cursor
= cursors
[XC_SIZING
].cid
;
7007 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
7008 XC_TOP_RIGHT_CORNER
].cid
;
7010 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
7011 XC_BOTTOM_RIGHT_CORNER
].cid
;
7013 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
7014 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
7017 /* Release keyboard freeze if called via keybind. */
7018 if (bp
->type
== KEYBIND
)
7019 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7024 while (resizing
&& (evt
= get_next_event(true))) {
7025 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
7026 case XCB_BUTTON_RELEASE
:
7027 if (bp
->type
== BTNBIND
&& bp
->value
==
7028 ((xcb_button_release_event_t
*)evt
)->detail
)
7031 case XCB_KEY_RELEASE
:
7032 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
7035 case XCB_MOTION_NOTIFY
:
7036 mne
= (xcb_motion_notify_event_t
*)evt
;
7037 DNPRINTF(SWM_D_EVENT
, "resize: MOTION_NOTIFY: "
7038 "root: %#x\n", mne
->root
);
7040 /* cursor offset/delta from start of the operation */
7041 dx
= mne
->root_x
- xpr
->root_x
;
7042 dy
= mne
->root_y
- xpr
->root_y
;
7048 if (opt
== SWM_ARG_ID_CENTER
) {
7049 if (g
.h
/ 2 + dy
< 1)
7053 HEIGHT(win
) = g
.h
+ 2 * dy
;
7061 HEIGHT(win
) = g
.h
+ dy
;
7068 if (opt
== SWM_ARG_ID_CENTER
) {
7069 if (g
.w
/ 2 + dx
< 1)
7073 WIDTH(win
) = g
.w
+ 2 * dx
;
7081 WIDTH(win
) = g
.w
+ dx
;
7084 /* not free, don't sync more than 120 times / second */
7085 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
7086 timestamp
= mne
->time
;
7087 regionize(win
, mne
->root_x
, mne
->root_y
);
7088 region_containment(win
, r
, SWM_CW_ALLSIDES
|
7089 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
7090 SWM_CW_SOFTBOUNDARY
);
7095 case XCB_BUTTON_PRESS
:
7097 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_PRESS ignored\n");
7098 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7099 ((xcb_button_press_event_t
*)evt
)->time
);
7104 DNPRINTF(SWM_D_EVENT
, "resize: KEY_PRESS ignored\n");
7105 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7106 ((xcb_key_press_event_t
*)evt
)->time
);
7112 /* It's possible for win to have been freed above. */
7113 if (validate_win(win
)) {
7114 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
7122 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
7123 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
7127 store_float_geom(win
);
7129 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7131 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
7135 resize(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
7137 struct ws_win
*win
= NULL
;
7138 xcb_query_pointer_reply_t
*qpr
= NULL
;
7143 if (args
->id
!= SWM_ARG_ID_DONTCENTER
&&
7144 args
->id
!= SWM_ARG_ID_CENTER
) {
7145 /* {height,width}_{grow,shrink} use the focus window. */
7149 /* resize uses window under pointer. */
7150 qpr
= xcb_query_pointer_reply(conn
,
7151 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7153 win
= find_window(qpr
->child
);
7159 resize_win(win
, bp
, args
->id
);
7161 if (args
->id
&& bp
->type
== KEYBIND
)
7167 /* Try to set window region based on supplied coordinates or window center. */
7169 regionize(struct ws_win
*win
, int x
, int y
)
7171 struct swm_region
*r
= NULL
;
7173 r
= region_under(win
->s
, x
, y
);
7175 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
7176 Y(win
) + HEIGHT(win
) / 2);
7178 if (r
&& r
!= win
->ws
->r
) {
7179 clear_maximized(r
->ws
);
7181 win_to_ws(win
, r
->ws
->idx
, false);
7183 /* Stack old and new region. */
7187 /* Set focus on new ws. */
7188 unfocus_win(r
->ws
->focus
);
7196 #define SWM_MOVE_STEPS (50)
7199 move_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
7201 struct swm_region
*r
;
7202 xcb_timestamp_t timestamp
= 0;
7203 xcb_query_pointer_reply_t
*qpr
= NULL
;
7204 xcb_generic_event_t
*evt
;
7205 xcb_motion_notify_event_t
*mne
;
7206 bool moving
, restack
= false, step
= false;
7211 if ((r
= win
->ws
->r
) == NULL
)
7214 if (FULLSCREEN(win
))
7217 DNPRINTF(SWM_D_EVENT
, "move: win %#x, floating: %s, transient: "
7218 "%#x\n", win
->id
, YESNO(ABOVE(win
)), win
->transient
);
7220 /* in max_stack mode should only move transients */
7221 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
7224 if (!(ABOVE(win
) || TRANS(win
)) || MAXIMIZED(win
)) {
7225 store_float_geom(win
);
7229 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
7231 ewmh_update_wm_state(win
);
7238 /* It's possible for win to have been freed during focus_flush(). */
7239 if (validate_win(win
)) {
7240 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7245 case SWM_ARG_ID_MOVELEFT
:
7246 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
7249 case SWM_ARG_ID_MOVERIGHT
:
7250 X(win
) += (SWM_MOVE_STEPS
- border_width
);
7253 case SWM_ARG_ID_MOVEUP
:
7254 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
7257 case SWM_ARG_ID_MOVEDOWN
:
7258 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
7265 regionize(win
, -1, -1);
7266 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
7268 store_float_geom(win
);
7272 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
7273 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
7274 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
7276 /* get cursor offset from window root */
7277 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
7280 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7284 /* Release keyboard freeze if called via keybind. */
7285 if (bp
->type
== KEYBIND
)
7286 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7289 regionize(win
, qpr
->root_x
, qpr
->root_y
);
7290 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7291 SWM_CW_SOFTBOUNDARY
);
7295 while (moving
&& (evt
= get_next_event(true))) {
7296 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
7297 case XCB_BUTTON_RELEASE
:
7298 if (bp
->type
== BTNBIND
&& bp
->value
==
7299 ((xcb_button_release_event_t
*)evt
)->detail
)
7302 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7303 ((xcb_button_release_event_t
*)evt
)->time
);
7306 case XCB_KEY_RELEASE
:
7307 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
7310 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7311 ((xcb_key_release_event_t
*)evt
)->time
);
7314 case XCB_MOTION_NOTIFY
:
7315 mne
= (xcb_motion_notify_event_t
*)evt
;
7316 DNPRINTF(SWM_D_EVENT
, "move: MOTION_NOTIFY: "
7317 "root: %#x\n", mne
->root
);
7318 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
7319 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
7321 /* not free, don't sync more than 120 times / second */
7322 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
7323 timestamp
= mne
->time
;
7324 regionize(win
, mne
->root_x
, mne
->root_y
);
7325 region_containment(win
, win
->ws
->r
,
7326 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
7331 case XCB_BUTTON_PRESS
:
7332 /* Thaw and ignore. */
7333 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7334 ((xcb_button_press_event_t
*)evt
)->time
);
7339 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7340 ((xcb_key_press_event_t
*)evt
)->time
);
7346 /* It's possible for win to have been freed. */
7347 if (validate_win(win
)) {
7348 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7356 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7357 SWM_CW_SOFTBOUNDARY
);
7361 store_float_geom(win
);
7363 /* New region set to fullscreen layout. */
7364 if (win
->ws
->r
&& win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
]) {
7371 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7372 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
7376 move(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
7378 struct ws_win
*win
= NULL
;
7379 xcb_query_pointer_reply_t
*qpr
= NULL
;
7385 /* move_* uses focus window. */
7389 /* Disallow move_ on tiled. */
7390 if (win
&& !(TRANS(win
) || ABOVE(win
)))
7393 /* move uses window under pointer. */
7394 qpr
= xcb_query_pointer_reply(conn
,
7395 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7397 win
= find_window(qpr
->child
);
7403 move_win(win
, bp
, args
->id
);
7405 if (args
->id
&& bp
->type
== KEYBIND
)
7411 /* action definitions */
7413 char name
[SWM_FUNCNAME_LEN
];
7414 void (*func
)(struct binding
*, struct swm_region
*,
7418 } actions
[FN_INVALID
+ 2] = {
7419 /* name function argument */
7420 { "bar_toggle", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
7421 { "bar_toggle_ws", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
7422 { "button2", pressbutton
, 0, {.id
= 2} },
7423 { "cycle_layout", cycle_layout
, 0, {0} },
7424 { "flip_layout", stack_config
, 0, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
7425 { "float_toggle", floating_toggle
,0, {0} },
7426 { "focus", focus_pointer
, 0, {0} },
7427 { "focus_main", focus
, 0, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
7428 { "focus_next", focus
, 0, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
7429 { "focus_prev", focus
, 0, {.id
= SWM_ARG_ID_FOCUSPREV
} },
7430 { "focus_urgent", focus
, 0, {.id
= SWM_ARG_ID_FOCUSURGENT
} },
7431 { "fullscreen_toggle", fullscreen_toggle
, 0, {0} },
7432 { "maximize_toggle", maximize_toggle
,0, {0} },
7433 { "height_grow", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
7434 { "height_shrink", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
7435 { "iconify", iconify
, 0, {0} },
7436 { "master_shrink", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
7437 { "master_grow", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERGROW
} },
7438 { "master_add", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERADD
} },
7439 { "master_del", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERDEL
} },
7440 { "move", move
, FN_F_NOREPLAY
, {0} },
7441 { "move_down", move
, 0, {.id
= SWM_ARG_ID_MOVEDOWN
} },
7442 { "move_left", move
, 0, {.id
= SWM_ARG_ID_MOVELEFT
} },
7443 { "move_right", move
, 0, {.id
= SWM_ARG_ID_MOVERIGHT
} },
7444 { "move_up", move
, 0, {.id
= SWM_ARG_ID_MOVEUP
} },
7445 { "mvrg_1", send_to_rg
, 0, {.id
= 0} },
7446 { "mvrg_2", send_to_rg
, 0, {.id
= 1} },
7447 { "mvrg_3", send_to_rg
, 0, {.id
= 2} },
7448 { "mvrg_4", send_to_rg
, 0, {.id
= 3} },
7449 { "mvrg_5", send_to_rg
, 0, {.id
= 4} },
7450 { "mvrg_6", send_to_rg
, 0, {.id
= 5} },
7451 { "mvrg_7", send_to_rg
, 0, {.id
= 6} },
7452 { "mvrg_8", send_to_rg
, 0, {.id
= 7} },
7453 { "mvrg_9", send_to_rg
, 0, {.id
= 8} },
7454 { "mvrg_next", send_to_rg_relative
, 0, {.id
= 1} },
7455 { "mvrg_prev", send_to_rg_relative
, 0, {.id
= -1} },
7456 { "mvws_1", send_to_ws
, 0, {.id
= 0} },
7457 { "mvws_2", send_to_ws
, 0, {.id
= 1} },
7458 { "mvws_3", send_to_ws
, 0, {.id
= 2} },
7459 { "mvws_4", send_to_ws
, 0, {.id
= 3} },
7460 { "mvws_5", send_to_ws
, 0, {.id
= 4} },
7461 { "mvws_6", send_to_ws
, 0, {.id
= 5} },
7462 { "mvws_7", send_to_ws
, 0, {.id
= 6} },
7463 { "mvws_8", send_to_ws
, 0, {.id
= 7} },
7464 { "mvws_9", send_to_ws
, 0, {.id
= 8} },
7465 { "mvws_10", send_to_ws
, 0, {.id
= 9} },
7466 { "mvws_11", send_to_ws
, 0, {.id
= 10} },
7467 { "mvws_12", send_to_ws
, 0, {.id
= 11} },
7468 { "mvws_13", send_to_ws
, 0, {.id
= 12} },
7469 { "mvws_14", send_to_ws
, 0, {.id
= 13} },
7470 { "mvws_15", send_to_ws
, 0, {.id
= 14} },
7471 { "mvws_16", send_to_ws
, 0, {.id
= 15} },
7472 { "mvws_17", send_to_ws
, 0, {.id
= 16} },
7473 { "mvws_18", send_to_ws
, 0, {.id
= 17} },
7474 { "mvws_19", send_to_ws
, 0, {.id
= 18} },
7475 { "mvws_20", send_to_ws
, 0, {.id
= 19} },
7476 { "mvws_21", send_to_ws
, 0, {.id
= 20} },
7477 { "mvws_22", send_to_ws
, 0, {.id
= 21} },
7478 { "name_workspace", name_workspace
, 0, {0} },
7479 { "quit", quit
, 0, {0} },
7480 { "raise", raise_focus
, 0, {0} },
7481 { "raise_toggle", raise_toggle
, 0, {0} },
7482 { "resize", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_DONTCENTER
} },
7483 { "resize_centered", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_CENTER
} },
7484 { "restart", restart
, 0, {0} },
7485 { "rg_1", focusrg
, 0, {.id
= 0} },
7486 { "rg_2", focusrg
, 0, {.id
= 1} },
7487 { "rg_3", focusrg
, 0, {.id
= 2} },
7488 { "rg_4", focusrg
, 0, {.id
= 3} },
7489 { "rg_5", focusrg
, 0, {.id
= 4} },
7490 { "rg_6", focusrg
, 0, {.id
= 5} },
7491 { "rg_7", focusrg
, 0, {.id
= 6} },
7492 { "rg_8", focusrg
, 0, {.id
= 7} },
7493 { "rg_9", focusrg
, 0, {.id
= 8} },
7494 { "rg_move_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_UP
} },
7495 { "rg_move_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_DOWN
} },
7496 { "rg_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7497 { "rg_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7498 { "screen_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7499 { "screen_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7500 { "search_win", search_win
, 0, {0} },
7501 { "search_workspace", search_workspace
, 0, {0} },
7502 { "spawn_custom", NULL
, 0, {0} },
7503 { "stack_balance", stack_config
, 0, {.id
= SWM_ARG_ID_STACKBALANCE
} },
7504 { "stack_inc", stack_config
, 0, {.id
= SWM_ARG_ID_STACKINC
} },
7505 { "stack_dec", stack_config
, 0, {.id
= SWM_ARG_ID_STACKDEC
} },
7506 { "stack_reset", stack_config
, 0, {.id
= SWM_ARG_ID_STACKRESET
} },
7507 { "swap_main", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPMAIN
} },
7508 { "swap_next", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPNEXT
} },
7509 { "swap_prev", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPPREV
} },
7510 { "uniconify", uniconify
, 0, {0} },
7511 { "version", version
, 0, {0} },
7512 { "width_grow", resize
, 0, {.id
= SWM_ARG_ID_WIDTHGROW
} },
7513 { "width_shrink", resize
, 0, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
7514 { "wind_del", wkill
, 0, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
7515 { "wind_kill", wkill
, 0, {.id
= SWM_ARG_ID_KILLWINDOW
} },
7516 { "ws_1", switchws
, 0, {.id
= 0} },
7517 { "ws_2", switchws
, 0, {.id
= 1} },
7518 { "ws_3", switchws
, 0, {.id
= 2} },
7519 { "ws_4", switchws
, 0, {.id
= 3} },
7520 { "ws_5", switchws
, 0, {.id
= 4} },
7521 { "ws_6", switchws
, 0, {.id
= 5} },
7522 { "ws_7", switchws
, 0, {.id
= 6} },
7523 { "ws_8", switchws
, 0, {.id
= 7} },
7524 { "ws_9", switchws
, 0, {.id
= 8} },
7525 { "ws_10", switchws
, 0, {.id
= 9} },
7526 { "ws_11", switchws
, 0, {.id
= 10} },
7527 { "ws_12", switchws
, 0, {.id
= 11} },
7528 { "ws_13", switchws
, 0, {.id
= 12} },
7529 { "ws_14", switchws
, 0, {.id
= 13} },
7530 { "ws_15", switchws
, 0, {.id
= 14} },
7531 { "ws_16", switchws
, 0, {.id
= 15} },
7532 { "ws_17", switchws
, 0, {.id
= 16} },
7533 { "ws_18", switchws
, 0, {.id
= 17} },
7534 { "ws_19", switchws
, 0, {.id
= 18} },
7535 { "ws_20", switchws
, 0, {.id
= 19} },
7536 { "ws_21", switchws
, 0, {.id
= 20} },
7537 { "ws_22", switchws
, 0, {.id
= 21} },
7538 { "ws_next", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
7539 { "ws_next_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
7540 { "ws_next_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
7541 { "ws_prev", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
7542 { "ws_prev_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
7543 { "ws_prev_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
7544 { "ws_prior", priorws
, 0, {0} },
7545 /* SWM_DEBUG actions MUST be here: */
7546 { "debug_toggle", debug_toggle
, 0, {0} },
7547 { "dumpwins", dumpwins
, 0, {0} },
7549 { "invalid action", NULL
, 0, {0} },
7553 update_modkey(uint16_t mod
)
7557 /* Replace all instances of the old mod key. */
7558 RB_FOREACH(bp
, binding_tree
, &bindings
)
7559 if (bp
->mod
& mod_key
)
7560 bp
->mod
= (bp
->mod
& ~mod_key
) | mod
;
7565 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7568 struct spawn_prog
*prog
= NULL
;
7570 char *ap
, **real_args
;
7572 /* suppress unused warning since var is needed */
7575 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
7578 TAILQ_FOREACH(prog
, &spawns
, entry
) {
7579 if (strcasecmp(spawn_name
, prog
->name
) == 0)
7583 warnx("spawn_custom: program %s not found", spawn_name
);
7587 /* make room for expanded args */
7588 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
7589 err(1, "spawn_custom: calloc real_args");
7591 /* expand spawn_args into real_args */
7592 for (i
= c
= 0; i
< prog
->argc
; i
++) {
7594 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
7595 if (strcasecmp(ap
, "$bar_border") == 0) {
7597 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
7599 err(1, "spawn_custom border color");
7600 } else if (strcasecmp(ap
, "$bar_color") == 0) {
7602 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
7604 err(1, "spawn_custom bar color");
7605 } else if (strcasecmp(ap
, "$bar_font") == 0) {
7606 if ((real_args
[c
] = strdup(bar_fonts
))
7608 err(1, "spawn_custom bar fonts");
7609 } else if (strcasecmp(ap
, "$bar_font_color") == 0) {
7611 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
7613 err(1, "spawn_custom color font");
7614 } else if (strcasecmp(ap
, "$color_focus") == 0) {
7616 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
7618 err(1, "spawn_custom color focus");
7619 } else if (strcasecmp(ap
, "$color_focus_maximized") == 0) {
7621 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].name
))
7623 err(1, "spawn_custom color focus maximized");
7624 } else if (strcasecmp(ap
, "$color_unfocus") == 0) {
7626 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
7628 err(1, "spawn_custom color unfocus");
7629 } else if (strcasecmp(ap
, "$color_unfocus_maximized") == 0) {
7631 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].name
))
7633 err(1, "spawn_custom color unfocus maximized");
7634 } else if (strcasecmp(ap
, "$region_index") == 0) {
7635 if (asprintf(&real_args
[c
], "%d",
7636 get_region_index(r
) + 1) < 1)
7637 err(1, "spawn_custom region index");
7638 } else if (strcasecmp(ap
, "$workspace_index") == 0) {
7639 if (asprintf(&real_args
[c
], "%d", r
->ws
->idx
+ 1) < 1)
7640 err(1, "spawn_custom workspace index");
7641 } else if (strcasecmp(ap
, "$dmenu_bottom") == 0) {
7644 if ((real_args
[c
] = strdup("-b")) == NULL
)
7645 err(1, "spawn_custom workspace index");
7647 /* no match --> copy as is */
7648 if ((real_args
[c
] = strdup(ap
)) == NULL
)
7649 err(1, "spawn_custom strdup(ap)");
7651 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
7657 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
7658 for (i
= 0; i
< c
; ++i
)
7659 DPRINTF("\"%s\" ", real_args
[i
]);
7662 *ret_args
= real_args
;
7667 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
7673 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7677 spawn(r
->ws
->idx
, &a
, true);
7679 for (i
= 0; i
< spawn_argc
; i
++)
7685 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7692 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7696 if (pipe(select_list_pipe
) == -1)
7697 err(1, "pipe error");
7698 if (pipe(select_resp_pipe
) == -1)
7699 err(1, "pipe error");
7701 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
7702 err(1, "could not disable SIGPIPE");
7703 switch (*pid
= fork()) {
7705 err(1, "cannot fork");
7708 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
7710 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
7712 close(select_list_pipe
[1]);
7713 close(select_resp_pipe
[0]);
7714 spawn(r
->ws
->idx
, &a
, false);
7716 default: /* parent */
7717 close(select_list_pipe
[0]);
7718 close(select_resp_pipe
[1]);
7722 for (i
= 0; i
< spawn_argc
; i
++)
7727 /* Argument tokenizer. */
7730 char *arg
, *cp
, *next
;
7731 bool single_quoted
= false, double_quoted
= false;
7736 /* Eat and move characters until end of argument is found. */
7737 for (arg
= next
= cp
= *sp
; *cp
!= '\0'; ++cp
) {
7738 if (!double_quoted
&& *cp
== '\'') {
7739 /* Eat single-quote. */
7740 single_quoted
= !single_quoted
;
7741 } else if (!single_quoted
&& *cp
== '"') {
7742 /* Eat double-quote. */
7743 double_quoted
= !double_quoted
;
7744 } else if (!single_quoted
&& *cp
== '\\' && *(cp
+ 1) == '"') {
7745 /* Eat backslash; copy escaped character to arg. */
7747 } else if (!single_quoted
&& !double_quoted
&& *cp
== '\\' &&
7748 (*(cp
+ 1) == '\'' || *(cp
+ 1) == ' ')) {
7749 /* Eat backslash; move escaped character. */
7751 } else if (!single_quoted
&& !double_quoted
&&
7752 (*cp
== ' ' || *cp
== '\t')) {
7753 /* Terminate argument. */
7755 /* Point sp to beginning of next argument. */
7759 /* Move regular character. */
7764 /* Terminate argument if end of string. */
7774 spawn_insert(const char *name
, const char *args
, int flags
)
7776 struct spawn_prog
*sp
;
7777 char *arg
, *cp
, *ptr
;
7779 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s[%s]\n", name
, args
);
7781 if (args
== NULL
|| *args
== '\0')
7784 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
7785 err(1, "spawn_insert: calloc");
7786 if ((sp
->name
= strdup(name
)) == NULL
)
7787 err(1, "spawn_insert: strdup");
7789 /* Convert the arguments to an argument list. */
7790 if ((ptr
= cp
= strdup(args
)) == NULL
)
7791 err(1, "spawn_insert: strdup");
7792 while ((arg
= argsep(&ptr
)) != NULL
) {
7793 /* Null argument; skip it. */
7798 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
7799 sizeof *sp
->argv
)) == NULL
)
7800 err(1, "spawn_insert: realloc");
7801 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
7802 err(1, "spawn_insert: strdup");
7808 DNPRINTF(SWM_D_SPAWN
, "arg %d: [%s]\n", sp
->argc
, sp
->argv
[sp
->argc
-1]);
7809 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
7810 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
7814 spawn_remove(struct spawn_prog
*sp
)
7818 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
7820 TAILQ_REMOVE(&spawns
, sp
, entry
);
7821 for (i
= 0; i
< sp
->argc
; i
++)
7827 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
7833 struct spawn_prog
*sp
;
7835 while ((sp
= TAILQ_FIRST(&spawns
)) != NULL
) {
7841 spawn_find(const char *name
)
7843 struct spawn_prog
*sp
;
7845 TAILQ_FOREACH(sp
, &spawns
, entry
)
7846 if (strcasecmp(sp
->name
, name
) == 0)
7853 setspawn(const char *name
, const char *args
, int flags
)
7855 struct spawn_prog
*sp
;
7857 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
7862 /* Remove any old spawn under the same name. */
7863 if ((sp
= spawn_find(name
)) != NULL
)
7867 spawn_insert(name
, args
, flags
);
7869 warnx("error: setspawn: cannot find program: %s", name
);
7871 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
7875 setconfspawn(const char *selector
, const char *value
, int flags
)
7879 if (selector
== NULL
|| strlen(selector
) == 0)
7882 args
= expand_tilde(value
);
7884 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
7886 setspawn(selector
, args
, flags
);
7889 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
7894 validate_spawns(void)
7897 struct spawn_prog
*sp
;
7898 char which
[PATH_MAX
];
7901 RB_FOREACH(bp
, binding_tree
, &bindings
) {
7902 if (bp
->action
!= FN_SPAWN_CUSTOM
)
7906 sp
= spawn_find(bp
->spawn_name
);
7907 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
7910 /* verify we have the goods */
7911 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
7912 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
7914 for (i
= strlen("which "); i
< strlen(which
); i
++)
7915 if (which
[i
] == ' ') {
7919 if (system(which
) != 0)
7920 add_startup_exception("could not find %s",
7921 &which
[strlen("which ")]);
7928 setconfspawn("lock", "xlock", 0);
7930 setconfspawn("term", "xterm", 0);
7931 setconfspawn("spawn_term", "xterm", 0);
7933 setconfspawn("menu", "dmenu_run"
7937 " -nf $bar_font_color"
7939 " -sf $bar_color", 0);
7941 setconfspawn("search", "dmenu"
7946 " -nf $bar_font_color"
7948 " -sf $bar_color", 0);
7950 setconfspawn("name_workspace", "dmenu"
7955 " -nf $bar_font_color"
7957 " -sf $bar_color", 0);
7959 /* These are not verified for existence, even with a binding set. */
7960 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
7961 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
7962 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
7966 #define SWM_MODNAME_SIZE 32
7967 #define SWM_KEY_WS "\n+ \t"
7969 parsebinding(const char *bindstr
, uint16_t *mod
, enum binding_type
*type
,
7970 uint32_t *val
, uint32_t *flags
)
7972 char *str
, *cp
, *name
;
7973 KeySym ks
, lks
, uks
;
7975 DNPRINTF(SWM_D_KEY
, "parsebinding: enter [%s]\n", bindstr
);
7976 if (mod
== NULL
|| val
== NULL
) {
7977 DNPRINTF(SWM_D_KEY
, "parsebinding: no mod or key vars\n");
7980 if (bindstr
== NULL
|| strlen(bindstr
) == 0) {
7981 DNPRINTF(SWM_D_KEY
, "parsebinding: no bindstr\n");
7985 if ((cp
= str
= strdup(bindstr
)) == NULL
)
7986 err(1, "parsebinding: strdup");
7988 *val
= XCB_NO_SYMBOL
;
7992 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
7993 DNPRINTF(SWM_D_KEY
, "parsebinding: entry [%s]\n", name
);
7995 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
7996 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
7998 else if (strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
) == 0)
7999 *mod
|= XCB_MOD_MASK_1
;
8000 else if (strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
) == 0)
8001 *mod
|= XCB_MOD_MASK_2
;
8002 else if (strncmp(name
, "Mod3", SWM_MODNAME_SIZE
) == 0)
8003 *mod
|= XCB_MOD_MASK_3
;
8004 else if (strncmp(name
, "Mod4", SWM_MODNAME_SIZE
) == 0)
8005 *mod
|= XCB_MOD_MASK_4
;
8006 else if (strncmp(name
, "Mod5", SWM_MODNAME_SIZE
) == 0)
8007 *mod
|= XCB_MOD_MASK_5
;
8008 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
8009 *mod
|= XCB_MOD_MASK_SHIFT
;
8010 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
8011 *mod
|= XCB_MOD_MASK_CONTROL
;
8012 else if (strncasecmp(name
, "ANYMOD", SWM_MODNAME_SIZE
) == 0)
8013 *mod
|= XCB_MOD_MASK_ANY
;
8014 else if (strncasecmp(name
, "REPLAY", SWM_MODNAME_SIZE
) == 0)
8015 *flags
|= BINDING_F_REPLAY
;
8016 else if (sscanf(name
, "Button%u", val
) == 1) {
8017 DNPRINTF(SWM_D_KEY
, "parsebinding: button %u\n", *val
);
8019 if (*val
> 255 || *val
== 0) {
8021 "parsebinding: invalid button %u\n", *val
);
8025 /* TODO: do this without Xlib. */
8026 ks
= XStringToKeysym(name
);
8027 if (ks
== NoSymbol
) {
8029 "parsebinding: invalid key %s\n", name
);
8034 XConvertCase(ks
, &lks
, &uks
);
8035 *val
= (uint32_t)lks
;
8039 /* If ANYMOD was specified, ignore the rest. */
8040 if (*mod
& XCB_MOD_MASK_ANY
)
8041 *mod
= XCB_MOD_MASK_ANY
;
8044 DNPRINTF(SWM_D_KEY
, "parsebinding: leave ok\n");
8049 strdupsafe(const char *str
)
8054 return (strdup(str
));
8058 binding_cmp(struct binding
*bp1
, struct binding
*bp2
)
8060 if (bp1
->type
< bp2
->type
)
8062 if (bp1
->type
> bp2
->type
)
8065 if (bp1
->value
< bp2
->value
)
8067 if (bp1
->value
> bp2
->value
)
8070 if (bp1
->mod
< bp2
->mod
)
8072 if (bp1
->mod
> bp2
->mod
)
8079 binding_insert(uint16_t mod
, enum binding_type type
, uint32_t val
,
8080 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
8084 DNPRINTF(SWM_D_KEY
, "binding_insert: mod: %u, type: %d, val: %u, "
8085 "action: %s(%d), spawn_name: %s\n", mod
, type
, val
,
8086 actions
[aid
].name
, aid
, spawn_name
);
8088 if ((bp
= malloc(sizeof *bp
)) == NULL
)
8089 err(1, "binding_insert: malloc");
8096 bp
->spawn_name
= strdupsafe(spawn_name
);
8097 RB_INSERT(binding_tree
, &bindings
, bp
);
8099 DNPRINTF(SWM_D_KEY
, "binding_insert: leave\n");
8103 binding_lookup(uint16_t mod
, enum binding_type type
, uint32_t val
)
8111 return (RB_FIND(binding_tree
, &bindings
, &bp
));
8115 binding_remove(struct binding
*bp
)
8117 DNPRINTF(SWM_D_KEY
, "binding_remove: mod: %u, type: %d, val: %u, "
8118 "action: %s(%d), spawn_name: %s\n", bp
->mod
, bp
->type
, bp
->value
,
8119 actions
[bp
->action
].name
, bp
->action
, bp
->spawn_name
);
8121 RB_REMOVE(binding_tree
, &bindings
, bp
);
8122 free(bp
->spawn_name
);
8125 DNPRINTF(SWM_D_KEY
, "binding_remove: leave\n");
8129 setbinding(uint16_t mod
, enum binding_type type
, uint32_t val
,
8130 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
8134 DNPRINTF(SWM_D_KEY
, "setbinding: enter %s [%s]\n",
8135 actions
[aid
].name
, spawn_name
);
8137 /* Unbind any existing. Loop is to handle MOD_MASK_ANY. */
8138 while ((bp
= binding_lookup(mod
, type
, val
)))
8141 if (aid
!= FN_INVALID
)
8142 binding_insert(mod
, type
, val
, aid
, flags
, spawn_name
);
8144 DNPRINTF(SWM_D_KEY
, "setbinding: leave\n");
8148 setconfbinding(const char *selector
, const char *value
, int flags
)
8150 struct spawn_prog
*sp
;
8151 uint32_t keybtn
, opts
;
8154 enum binding_type type
;
8156 /* suppress unused warning since var is needed */
8159 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter selector: [%s], "
8160 "value: [%s]\n", selector
, value
);
8161 if (selector
== NULL
|| strlen(selector
) == 0) {
8162 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
8163 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8164 setbinding(mod
, type
, keybtn
, FN_INVALID
, opts
, NULL
);
8169 /* search by key function name */
8170 for (aid
= 0; aid
< FN_INVALID
; aid
++) {
8171 if (strncasecmp(selector
, actions
[aid
].name
,
8172 SWM_FUNCNAME_LEN
) == 0) {
8173 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8174 "action\n", selector
);
8175 if (parsebinding(value
, &mod
, &type
, &keybtn
,
8177 setbinding(mod
, type
, keybtn
, aid
, opts
, NULL
);
8183 /* search by custom spawn name */
8184 if ((sp
= spawn_find(selector
)) != NULL
) {
8185 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8186 "spawn\n", selector
);
8187 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8188 setbinding(mod
, type
, keybtn
, FN_SPAWN_CUSTOM
, opts
,
8194 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
8198 #define MODSHIFT MODKEY | XCB_MOD_MASK_SHIFT
8200 setup_keybindings(void)
8202 #define BINDKEY(m, k, a) setbinding(m, KEYBIND, k, a, 0, NULL)
8203 #define BINDKEYSPAWN(m, k, s) setbinding(m, KEYBIND, k, FN_SPAWN_CUSTOM, 0, s)
8204 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8205 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8206 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8207 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8208 BINDKEY(MODKEY
, XK_v
, FN_BUTTON2
);
8209 BINDKEY(MODKEY
, XK_space
, FN_CYCLE_LAYOUT
);
8210 BINDKEY(MODSHIFT
, XK_backslash
, FN_FLIP_LAYOUT
);
8211 BINDKEY(MODKEY
, XK_t
, FN_FLOAT_TOGGLE
);
8212 BINDKEY(MODKEY
, XK_m
, FN_FOCUS_MAIN
);
8213 BINDKEY(MODKEY
, XK_j
, FN_FOCUS_NEXT
);
8214 BINDKEY(MODKEY
, XK_Tab
, FN_FOCUS_NEXT
);
8215 BINDKEY(MODKEY
, XK_k
, FN_FOCUS_PREV
);
8216 BINDKEY(MODSHIFT
, XK_Tab
, FN_FOCUS_PREV
);
8217 BINDKEY(MODKEY
, XK_u
, FN_FOCUS_URGENT
);
8218 BINDKEY(MODSHIFT
, XK_e
, FN_FULLSCREEN_TOGGLE
);
8219 BINDKEY(MODKEY
, XK_e
, FN_MAXIMIZE_TOGGLE
);
8220 BINDKEY(MODSHIFT
, XK_equal
, FN_HEIGHT_GROW
);
8221 BINDKEY(MODSHIFT
, XK_minus
, FN_HEIGHT_SHRINK
);
8222 BINDKEY(MODKEY
, XK_w
, FN_ICONIFY
);
8223 BINDKEY(MODKEY
, XK_h
, FN_MASTER_SHRINK
);
8224 BINDKEY(MODKEY
, XK_l
, FN_MASTER_GROW
);
8225 BINDKEY(MODKEY
, XK_comma
, FN_MASTER_ADD
);
8226 BINDKEY(MODKEY
, XK_period
, FN_MASTER_DEL
);
8227 BINDKEY(MODSHIFT
, XK_bracketright
, FN_MOVE_DOWN
);
8228 BINDKEY(MODKEY
, XK_bracketleft
, FN_MOVE_LEFT
);
8229 BINDKEY(MODKEY
, XK_bracketright
, FN_MOVE_RIGHT
);
8230 BINDKEY(MODSHIFT
, XK_bracketleft
, FN_MOVE_UP
);
8231 BINDKEY(MODSHIFT
, XK_KP_End
, FN_MVRG_1
);
8232 BINDKEY(MODSHIFT
, XK_KP_Down
, FN_MVRG_2
);
8233 BINDKEY(MODSHIFT
, XK_KP_Next
, FN_MVRG_3
);
8234 BINDKEY(MODSHIFT
, XK_KP_Left
, FN_MVRG_4
);
8235 BINDKEY(MODSHIFT
, XK_KP_Begin
, FN_MVRG_5
);
8236 BINDKEY(MODSHIFT
, XK_KP_Right
, FN_MVRG_6
);
8237 BINDKEY(MODSHIFT
, XK_KP_Home
, FN_MVRG_7
);
8238 BINDKEY(MODSHIFT
, XK_KP_Up
, FN_MVRG_8
);
8239 BINDKEY(MODSHIFT
, XK_KP_Prior
, FN_MVRG_9
);
8240 BINDKEY(MODSHIFT
, XK_1
, FN_MVWS_1
);
8241 BINDKEY(MODSHIFT
, XK_2
, FN_MVWS_2
);
8242 BINDKEY(MODSHIFT
, XK_3
, FN_MVWS_3
);
8243 BINDKEY(MODSHIFT
, XK_4
, FN_MVWS_4
);
8244 BINDKEY(MODSHIFT
, XK_5
, FN_MVWS_5
);
8245 BINDKEY(MODSHIFT
, XK_6
, FN_MVWS_6
);
8246 BINDKEY(MODSHIFT
, XK_7
, FN_MVWS_7
);
8247 BINDKEY(MODSHIFT
, XK_8
, FN_MVWS_8
);
8248 BINDKEY(MODSHIFT
, XK_9
, FN_MVWS_9
);
8249 BINDKEY(MODSHIFT
, XK_0
, FN_MVWS_10
);
8250 BINDKEY(MODSHIFT
, XK_F1
, FN_MVWS_11
);
8251 BINDKEY(MODSHIFT
, XK_F2
, FN_MVWS_12
);
8252 BINDKEY(MODSHIFT
, XK_F3
, FN_MVWS_13
);
8253 BINDKEY(MODSHIFT
, XK_F4
, FN_MVWS_14
);
8254 BINDKEY(MODSHIFT
, XK_F5
, FN_MVWS_15
);
8255 BINDKEY(MODSHIFT
, XK_F6
, FN_MVWS_16
);
8256 BINDKEY(MODSHIFT
, XK_F7
, FN_MVWS_17
);
8257 BINDKEY(MODSHIFT
, XK_F8
, FN_MVWS_18
);
8258 BINDKEY(MODSHIFT
, XK_F9
, FN_MVWS_19
);
8259 BINDKEY(MODSHIFT
, XK_F10
, FN_MVWS_20
);
8260 BINDKEY(MODSHIFT
, XK_F11
, FN_MVWS_21
);
8261 BINDKEY(MODSHIFT
, XK_F12
, FN_MVWS_22
);
8262 BINDKEY(MODSHIFT
, XK_slash
, FN_NAME_WORKSPACE
);
8263 BINDKEY(MODSHIFT
, XK_q
, FN_QUIT
);
8264 BINDKEY(MODKEY
, XK_r
, FN_RAISE
);
8265 BINDKEY(MODSHIFT
, XK_r
, FN_RAISE_TOGGLE
);
8266 BINDKEY(MODKEY
, XK_q
, FN_RESTART
);
8267 BINDKEY(MODKEY
, XK_KP_End
, FN_RG_1
);
8268 BINDKEY(MODKEY
, XK_KP_Down
, FN_RG_2
);
8269 BINDKEY(MODKEY
, XK_KP_Next
, FN_RG_3
);
8270 BINDKEY(MODKEY
, XK_KP_Left
, FN_RG_4
);
8271 BINDKEY(MODKEY
, XK_KP_Begin
, FN_RG_5
);
8272 BINDKEY(MODKEY
, XK_KP_Right
, FN_RG_6
);
8273 BINDKEY(MODKEY
, XK_KP_Home
, FN_RG_7
);
8274 BINDKEY(MODKEY
, XK_KP_Up
, FN_RG_8
);
8275 BINDKEY(MODKEY
, XK_KP_Prior
, FN_RG_9
);
8276 BINDKEY(MODSHIFT
, XK_Right
, FN_RG_NEXT
);
8277 BINDKEY(MODSHIFT
, XK_Left
, FN_RG_PREV
);
8278 BINDKEY(MODKEY
, XK_f
, FN_SEARCH_WIN
);
8279 BINDKEY(MODKEY
, XK_slash
, FN_SEARCH_WORKSPACE
);
8280 BINDKEYSPAWN(MODSHIFT
, XK_i
, "initscr");
8281 BINDKEYSPAWN(MODSHIFT
, XK_Delete
, "lock");
8282 BINDKEYSPAWN(MODKEY
, XK_p
, "menu");
8283 BINDKEYSPAWN(MODKEY
, XK_s
, "screenshot_all");
8284 BINDKEYSPAWN(MODSHIFT
, XK_s
, "screenshot_wind");
8285 BINDKEYSPAWN(MODSHIFT
, XK_Return
, "term");
8286 BINDKEY(MODSHIFT
, XK_comma
, FN_STACK_INC
);
8287 BINDKEY(MODSHIFT
, XK_period
, FN_STACK_DEC
);
8288 BINDKEY(MODSHIFT
, XK_space
, FN_STACK_RESET
);
8289 BINDKEY(MODKEY
, XK_Return
, FN_SWAP_MAIN
);
8290 BINDKEY(MODSHIFT
, XK_j
, FN_SWAP_NEXT
);
8291 BINDKEY(MODSHIFT
, XK_k
, FN_SWAP_PREV
);
8292 BINDKEY(MODSHIFT
, XK_w
, FN_UNICONIFY
);
8293 BINDKEY(MODSHIFT
, XK_v
, FN_VERSION
);
8294 BINDKEY(MODKEY
, XK_equal
, FN_WIDTH_GROW
);
8295 BINDKEY(MODKEY
, XK_minus
, FN_WIDTH_SHRINK
);
8296 BINDKEY(MODKEY
, XK_x
, FN_WIND_DEL
);
8297 BINDKEY(MODSHIFT
, XK_x
, FN_WIND_KILL
);
8298 BINDKEY(MODKEY
, XK_1
, FN_WS_1
);
8299 BINDKEY(MODKEY
, XK_2
, FN_WS_2
);
8300 BINDKEY(MODKEY
, XK_3
, FN_WS_3
);
8301 BINDKEY(MODKEY
, XK_4
, FN_WS_4
);
8302 BINDKEY(MODKEY
, XK_5
, FN_WS_5
);
8303 BINDKEY(MODKEY
, XK_6
, FN_WS_6
);
8304 BINDKEY(MODKEY
, XK_7
, FN_WS_7
);
8305 BINDKEY(MODKEY
, XK_8
, FN_WS_8
);
8306 BINDKEY(MODKEY
, XK_9
, FN_WS_9
);
8307 BINDKEY(MODKEY
, XK_0
, FN_WS_10
);
8308 BINDKEY(MODKEY
, XK_F1
, FN_WS_11
);
8309 BINDKEY(MODKEY
, XK_F2
, FN_WS_12
);
8310 BINDKEY(MODKEY
, XK_F3
, FN_WS_13
);
8311 BINDKEY(MODKEY
, XK_F4
, FN_WS_14
);
8312 BINDKEY(MODKEY
, XK_F5
, FN_WS_15
);
8313 BINDKEY(MODKEY
, XK_F6
, FN_WS_16
);
8314 BINDKEY(MODKEY
, XK_F7
, FN_WS_17
);
8315 BINDKEY(MODKEY
, XK_F8
, FN_WS_18
);
8316 BINDKEY(MODKEY
, XK_F9
, FN_WS_19
);
8317 BINDKEY(MODKEY
, XK_F10
, FN_WS_20
);
8318 BINDKEY(MODKEY
, XK_F11
, FN_WS_21
);
8319 BINDKEY(MODKEY
, XK_F12
, FN_WS_22
);
8320 BINDKEY(MODKEY
, XK_Right
, FN_WS_NEXT
);
8321 BINDKEY(MODKEY
, XK_Left
, FN_WS_PREV
);
8322 BINDKEY(MODKEY
, XK_Up
, FN_WS_NEXT_ALL
);
8323 BINDKEY(MODKEY
, XK_Down
, FN_WS_PREV_ALL
);
8324 BINDKEY(MODSHIFT
, XK_Up
, FN_WS_NEXT_MOVE
);
8325 BINDKEY(MODSHIFT
, XK_Down
, FN_WS_PREV_MOVE
);
8326 BINDKEY(MODKEY
, XK_a
, FN_WS_PRIOR
);
8328 BINDKEY(MODKEY
, XK_d
, FN_DEBUG_TOGGLE
);
8329 BINDKEY(MODSHIFT
, XK_d
, FN_DUMPWINS
);
8336 setup_btnbindings(void)
8338 setbinding(ANYMOD
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_FOCUS
,
8339 BINDING_F_REPLAY
, NULL
);
8340 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE
, 0, NULL
);
8341 setbinding(MODSHIFT
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE_CENTERED
, 0,
8343 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_MOVE
, 0, NULL
);
8348 clear_bindings(void)
8352 while ((bp
= RB_ROOT(&bindings
)))
8357 clear_keybindings(void)
8359 struct binding
*bp
, *bptmp
;
8361 RB_FOREACH_SAFE(bp
, binding_tree
, &bindings
, bptmp
) {
8362 if (bp
->type
!= KEYBIND
)
8369 setkeymapping(const char *selector
, const char *value
, int flags
)
8371 char *keymapping_file
;
8373 /* suppress unused warnings since vars are needed */
8377 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
8379 keymapping_file
= expand_tilde(value
);
8381 clear_keybindings();
8382 /* load new key bindings; if it fails, revert to default bindings */
8383 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
8384 clear_keybindings();
8385 setup_keybindings();
8388 free(keymapping_file
);
8390 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
8395 updatenumlockmask(void)
8398 xcb_get_modifier_mapping_reply_t
*modmap_r
;
8399 xcb_keycode_t
*modmap
, kc
, *keycode
;
8403 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
8404 xcb_get_modifier_mapping(conn
),
8407 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
8408 for (i
= 0; i
< 8; i
++) {
8409 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
8410 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
8412 keycode
= xcb_key_symbols_get_keycode(syms
,
8416 numlockmask
= (1 << i
);
8423 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
8430 int num_screens
, k
, j
;
8431 uint16_t modifiers
[4];
8432 xcb_keycode_t
*code
;
8434 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
8435 updatenumlockmask();
8438 modifiers
[1] = numlockmask
;
8439 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8440 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8442 num_screens
= get_screen_count();
8443 for (k
= 0; k
< num_screens
; k
++) {
8444 if (TAILQ_EMPTY(&screens
[k
].rl
))
8446 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
8448 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8449 if (bp
->type
!= KEYBIND
)
8452 /* If there is a catch-all, only bind that. */
8453 if ((binding_lookup(ANYMOD
, KEYBIND
, bp
->value
)) &&
8457 /* Skip unused ws binds. */
8458 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8459 bp
->action
<= FN_WS_22
)
8462 /* Skip unused mvws binds. */
8463 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8464 bp
->action
<= FN_MVWS_22
)
8467 if ((code
= xcb_key_symbols_get_keycode(syms
,
8468 bp
->value
)) == NULL
)
8471 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8472 /* All modifiers are grabbed in one pass. */
8473 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8474 "key: %u, modifiers: %d\n", bp
->value
,
8476 xcb_grab_key(conn
, 1, screens
[k
].root
,
8477 bp
->mod
, *code
, XCB_GRAB_MODE_ASYNC
,
8478 XCB_GRAB_MODE_SYNC
);
8480 /* Need to grab each modifier permutation. */
8481 for (j
= 0; j
< LENGTH(modifiers
); j
++) {
8482 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8483 "key: %u, modifiers: %d\n",
8484 bp
->value
, bp
->mod
| modifiers
[j
]);
8485 xcb_grab_key(conn
, 1,
8487 bp
->mod
| modifiers
[j
],
8488 *code
, XCB_GRAB_MODE_ASYNC
,
8489 XCB_GRAB_MODE_SYNC
);
8501 int num_screens
, i
, k
;
8502 uint16_t modifiers
[4];
8504 DNPRINTF(SWM_D_MOUSE
, "grabbuttons\n");
8505 updatenumlockmask();
8508 modifiers
[1] = numlockmask
;
8509 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8510 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8512 num_screens
= get_screen_count();
8513 for (k
= 0; k
< num_screens
; k
++) {
8514 if (TAILQ_EMPTY(&screens
[k
].rl
))
8516 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, screens
[k
].root
,
8518 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8519 if (bp
->type
!= BTNBIND
)
8522 /* If there is a catch-all, only bind that. */
8523 if ((binding_lookup(ANYMOD
, BTNBIND
, bp
->value
)) &&
8527 /* Skip unused ws binds. */
8528 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8529 bp
->action
<= FN_WS_22
)
8532 /* Skip unused mvws binds. */
8533 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8534 bp
->action
<= FN_MVWS_22
)
8537 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8538 /* All modifiers are grabbed in one pass. */
8539 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: grab, "
8540 "button: %u, modifiers: %d\n", bp
->value
,
8542 xcb_grab_button(conn
, 0, screens
[k
].root
,
8543 BUTTONMASK
, XCB_GRAB_MODE_SYNC
,
8544 XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
8545 XCB_CURSOR_NONE
, bp
->value
, bp
->mod
);
8547 /* Need to grab each modifier permutation. */
8548 for (i
= 0; i
< LENGTH(modifiers
); ++i
) {
8549 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: "
8550 "grab, button: %u, modifiers: %u\n",
8551 bp
->value
, bp
->mod
| modifiers
[i
]);
8552 xcb_grab_button(conn
, 0,
8553 screens
[k
].root
, BUTTONMASK
,
8555 XCB_GRAB_MODE_ASYNC
,
8557 XCB_CURSOR_NONE
, bp
->value
,
8558 bp
->mod
| modifiers
[i
]);
8565 const char *quirkname
[] = {
8566 "NONE", /* config string for "no value" */
8574 "FOCUSONMAP_SINGLE",
8582 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
8583 #define SWM_Q_DELIM "\n|+ \t"
8585 parsequirks(const char *qstr
, uint32_t *quirk
, int *ws
)
8587 char *str
, *cp
, *name
;
8590 if (quirk
== NULL
|| qstr
== NULL
)
8593 if ((cp
= str
= strdup(qstr
)) == NULL
)
8594 err(1, "parsequirks: strdup");
8597 while ((name
= strsep(&cp
, SWM_Q_DELIM
)) != NULL
) {
8599 cp
+= (long)strspn(cp
, SWM_Q_DELIM
);
8601 if (sscanf(name
, "WS[%d]", ws
) == 1) {
8607 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
8608 if (strncasecmp(name
, quirkname
[i
],
8609 SWM_QUIRK_LEN
) == 0) {
8610 DNPRINTF(SWM_D_QUIRK
,
8611 "parsequirks: %s\n", name
);
8617 *quirk
|= 1 << (i
-1);
8621 if (i
>= LENGTH(quirkname
)) {
8622 DNPRINTF(SWM_D_QUIRK
,
8623 "parsequirks: invalid quirk [%s]\n", name
);
8634 quirk_insert(const char *class, const char *instance
, const char *name
,
8635 uint32_t quirk
, int ws
)
8639 bool failed
= false;
8641 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: class: %s, instance: %s, name: %s,"
8642 " value: %u, ws: %d\n", class, instance
, name
, quirk
, ws
);
8644 if ((qp
= malloc(sizeof *qp
)) == NULL
)
8645 err(1, "quirk_insert: malloc");
8647 if ((qp
->class = strdup(class)) == NULL
)
8648 err(1, "quirk_insert: strdup");
8649 if ((qp
->instance
= strdup(instance
)) == NULL
)
8650 err(1, "quirk_insert: strdup");
8651 if ((qp
->name
= strdup(name
)) == NULL
)
8652 err(1, "quirk_insert: strdup");
8654 if (asprintf(&str
, "^%s$", class) == -1)
8655 err(1, "quirk_insert: asprintf");
8656 if (regcomp(&qp
->regex_class
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8657 add_startup_exception("regex failed to compile quirk 'class' "
8658 "field: %s", class);
8661 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8664 if (asprintf(&str
, "^%s$", instance
) == -1)
8665 err(1, "quirk_insert: asprintf");
8666 if (regcomp(&qp
->regex_instance
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8667 add_startup_exception("regex failed to compile quirk 'instance'"
8668 " field: %s", instance
);
8671 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8674 if (asprintf(&str
, "^%s$", name
) == -1)
8675 err(1, "quirk_insert: asprintf");
8676 if (regcomp(&qp
->regex_name
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8677 add_startup_exception("regex failed to compile quirk 'name' "
8681 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8685 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: regex error; skipping.\n");
8690 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
8692 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
8696 quirk_remove(struct quirk
*qp
)
8698 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%u]\n", qp
->class,
8699 qp
->name
, qp
->quirk
);
8701 TAILQ_REMOVE(&quirks
, qp
, entry
);
8704 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
8708 quirk_free(struct quirk
*qp
)
8710 regfree(&qp
->regex_class
);
8711 regfree(&qp
->regex_instance
);
8712 regfree(&qp
->regex_name
);
8724 while ((qp
= TAILQ_FIRST(&quirks
)) != NULL
) {
8730 quirk_replace(struct quirk
*qp
, const char *class, const char *instance
,
8731 const char *name
, uint32_t quirk
, int ws
)
8733 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp
->class,
8734 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
8737 quirk_insert(class, instance
, name
, quirk
, ws
);
8739 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
8743 setquirk(const char *class, const char *instance
, const char *name
,
8744 uint32_t quirk
, int ws
)
8748 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
8749 instance
, name
, quirk
, ws
);
8751 /* Remove/replace existing quirk. */
8752 TAILQ_FOREACH(qp
, &quirks
, entry
) {
8753 if (strcmp(qp
->class, class) == 0 &&
8754 strcmp(qp
->instance
, instance
) == 0 &&
8755 strcmp(qp
->name
, name
) == 0) {
8756 if (quirk
== 0 && ws
== -1)
8759 quirk_replace(qp
, class, instance
, name
, quirk
,
8761 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8766 /* Only insert if quirk is not NONE or forced ws is set. */
8767 if (quirk
|| ws
!= -1)
8768 quirk_insert(class, instance
, name
, quirk
, ws
);
8770 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8773 /* Eat '\' in str used to escape square brackets and colon. */
8775 unescape_selector(char *str
)
8779 for (cp
= str
; *str
!= '\0'; ++str
, ++cp
) {
8780 if (*str
== '\\' && (*(str
+ 1) == ':' || *(str
+ 1) == ']' ||
8790 setconfquirk(const char *selector
, const char *value
, int flags
)
8792 char *str
, *cp
, *class;
8793 char *instance
= NULL
, *name
= NULL
;
8794 int retval
, count
= 0, ws
= -1;
8797 /* suppress unused warning since var is needed */
8800 if (selector
== NULL
|| strlen(selector
) == 0)
8803 if ((str
= strdup(selector
)) == NULL
)
8804 err(1, "setconfquirk: strdup");
8806 /* Find non-escaped colon. */
8813 for (++cp
; *cp
!= '\0'; ++cp
) {
8814 if (*cp
== ':' && *(cp
- 1) != '\\') {
8820 unescape_selector(class);
8822 instance
= class + strlen(class) + 1;
8823 unescape_selector(instance
);
8829 name
= instance
+ strlen(instance
) + 1;
8830 unescape_selector(name
);
8835 DNPRINTF(SWM_D_CONF
, "setconfquirk: class: %s, instance: %s, "
8836 "name: %s\n", class, instance
, name
);
8838 if ((retval
= parsequirks(value
, &qrks
, &ws
)) == 0)
8839 setquirk(class, instance
, name
, qrks
, ws
);
8848 setquirk("MPlayer", "xv", ".*",
8849 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
, -1);
8850 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
8852 setquirk("Firefox-bin", "firefox-bin", ".*",
8854 setquirk("Firefox", "Dialog", ".*",
8856 setquirk("Gimp", "gimp", ".*",
8857 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8858 setquirk("XTerm", "xterm", ".*",
8859 SWM_Q_XTERM_FONTADJ
, -1);
8860 setquirk("xine", "Xine Window", ".*",
8861 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8862 setquirk("Xitk", "Xitk Combo", ".*",
8863 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8864 setquirk("xine", "xine Panel", ".*",
8865 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8866 setquirk("Xitk", "Xine Window", ".*",
8867 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8868 setquirk("xine", "xine Video Fullscreen Window", ".*",
8869 SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
, -1);
8870 setquirk("pcb", "pcb", ".*",
8872 setquirk("SDL_App", "SDL_App", ".*",
8873 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
, -1);
8876 /* conf file stuff */
8877 #define SWM_CONF_FILE "spectrwm.conf"
8878 #define SWM_CONF_FILE_OLD "scrotwm.conf"
8882 SWM_S_BAR_AT_BOTTOM
,
8883 SWM_S_BAR_BORDER_WIDTH
,
8886 SWM_S_BAR_ENABLED_WS
,
8891 SWM_S_BOUNDARY_WIDTH
,
8892 SWM_S_CLOCK_ENABLED
,
8895 SWM_S_CYCLE_VISIBLE
,
8897 SWM_S_DISABLE_BORDER
,
8899 SWM_S_FOCUS_CLOSE_WRAP
,
8900 SWM_S_FOCUS_DEFAULT
,
8902 SWM_S_ICONIC_ENABLED
,
8903 SWM_S_JAVA_WORKAROUND
,
8904 SWM_S_MAXIMIZE_HIDE_BAR
,
8905 SWM_S_REGION_PADDING
,
8910 SWM_S_STACK_ENABLED
,
8913 SWM_S_URGENT_COLLAPSE
,
8914 SWM_S_URGENT_ENABLED
,
8915 SWM_S_VERBOSE_LAYOUT
,
8918 SWM_S_WINDOW_CLASS_ENABLED
,
8919 SWM_S_WINDOW_INSTANCE_ENABLED
,
8920 SWM_S_WINDOW_NAME_ENABLED
,
8921 SWM_S_WORKSPACE_CLAMP
,
8922 SWM_S_WORKSPACE_LIMIT
,
8923 SWM_S_WORKSPACE_NAME
,
8927 setconfvalue(const char *selector
, const char *value
, int flags
)
8929 struct workspace
*ws
;
8930 int i
, ws_id
, num_screens
;
8931 char *b
, *str
, *sp
, s
[1024];
8934 case SWM_S_BAR_ACTION
:
8936 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
8937 err(1, "setconfvalue: bar_action");
8939 case SWM_S_BAR_AT_BOTTOM
:
8940 bar_at_bottom
= (atoi(value
) != 0);
8942 case SWM_S_BAR_BORDER_WIDTH
:
8943 bar_border_width
= atoi(value
);
8944 if (bar_border_width
< 0)
8945 bar_border_width
= 0;
8947 case SWM_S_BAR_DELAY
:
8948 /* No longer needed; leave to not break old conf files. */
8950 case SWM_S_BAR_ENABLED
:
8951 bar_enabled
= (atoi(value
) != 0);
8953 case SWM_S_BAR_ENABLED_WS
:
8954 ws_id
= atoi(selector
) - 1;
8955 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8956 errx(1, "setconfvalue: bar_enabled_ws: invalid "
8957 "workspace %d.", ws_id
+ 1);
8959 num_screens
= get_screen_count();
8960 for (i
= 0; i
< num_screens
; i
++) {
8961 ws
= (struct workspace
*)&screens
[i
].ws
;
8962 ws
[ws_id
].bar_enabled
= (atoi(value
) != 0);
8965 case SWM_S_BAR_FONT
:
8967 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
8968 err(1, "setconfvalue: asprintf: failed to allocate "
8969 "memory for bar_fonts.");
8972 /* If already in xft mode, then we are done. */
8973 if (!bar_font_legacy
)
8976 if ((sp
= str
= strdup(value
)) == NULL
)
8977 err(1, "setconfvalue: strdup");
8979 /* If there are any non-XLFD entries, switch to Xft mode. */
8980 while ((b
= strsep(&sp
, ",")) != NULL
) {
8984 bar_font_legacy
= false;
8991 case SWM_S_BAR_FORMAT
:
8993 if ((bar_format
= strdup(value
)) == NULL
)
8994 err(1, "setconfvalue: bar_format");
8996 case SWM_S_BAR_JUSTIFY
:
8997 if (strcmp(value
, "left") == 0)
8998 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
8999 else if (strcmp(value
, "center") == 0)
9000 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
9001 else if (strcmp(value
, "right") == 0)
9002 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
9004 errx(1, "invalid bar_justify");
9006 case SWM_S_BORDER_WIDTH
:
9007 border_width
= atoi(value
);
9008 if (border_width
< 0)
9011 case SWM_S_BOUNDARY_WIDTH
:
9012 boundary_width
= atoi(value
);
9013 if (boundary_width
< 0)
9016 case SWM_S_CLOCK_ENABLED
:
9017 clock_enabled
= (atoi(value
) != 0);
9019 case SWM_S_CLOCK_FORMAT
:
9020 #ifndef SWM_DENY_CLOCK_FORMAT
9022 if ((clock_format
= strdup(value
)) == NULL
)
9023 err(1, "setconfvalue: clock_format");
9026 case SWM_S_CYCLE_EMPTY
:
9027 cycle_empty
= (atoi(value
) != 0);
9029 case SWM_S_CYCLE_VISIBLE
:
9030 cycle_visible
= (atoi(value
) != 0);
9032 case SWM_S_DIALOG_RATIO
:
9033 dialog_ratio
= atof(value
);
9034 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
9037 case SWM_S_DISABLE_BORDER
:
9038 disable_border
= (atoi(value
) != 0);
9040 case SWM_S_FOCUS_CLOSE
:
9041 if (strcmp(value
, "first") == 0)
9042 focus_close
= SWM_STACK_BOTTOM
;
9043 else if (strcmp(value
, "last") == 0)
9044 focus_close
= SWM_STACK_TOP
;
9045 else if (strcmp(value
, "next") == 0)
9046 focus_close
= SWM_STACK_ABOVE
;
9047 else if (strcmp(value
, "previous") == 0)
9048 focus_close
= SWM_STACK_BELOW
;
9050 errx(1, "focus_close");
9052 case SWM_S_FOCUS_CLOSE_WRAP
:
9053 focus_close_wrap
= (atoi(value
) != 0);
9055 case SWM_S_FOCUS_DEFAULT
:
9056 if (strcmp(value
, "last") == 0)
9057 focus_default
= SWM_STACK_TOP
;
9058 else if (strcmp(value
, "first") == 0)
9059 focus_default
= SWM_STACK_BOTTOM
;
9061 errx(1, "focus_default");
9063 case SWM_S_FOCUS_MODE
:
9064 if (strcmp(value
, "default") == 0)
9065 focus_mode
= SWM_FOCUS_DEFAULT
;
9066 else if (strcmp(value
, "follow") == 0 ||
9067 strcmp(value
, "follow_cursor") == 0)
9068 focus_mode
= SWM_FOCUS_FOLLOW
;
9069 else if (strcmp(value
, "manual") == 0)
9070 focus_mode
= SWM_FOCUS_MANUAL
;
9072 errx(1, "focus_mode");
9074 case SWM_S_ICONIC_ENABLED
:
9075 iconic_enabled
= (atoi(value
) != 0);
9077 case SWM_S_JAVA_WORKAROUND
:
9078 java_workaround
= (atoi(value
) != 0);
9080 case SWM_S_MAXIMIZE_HIDE_BAR
:
9081 maximize_hide_bar
= atoi(value
);
9083 case SWM_S_REGION_PADDING
:
9084 region_padding
= atoi(value
);
9085 if (region_padding
< 0)
9088 case SWM_S_SPAWN_ORDER
:
9089 if (strcmp(value
, "first") == 0)
9090 spawn_position
= SWM_STACK_BOTTOM
;
9091 else if (strcmp(value
, "last") == 0)
9092 spawn_position
= SWM_STACK_TOP
;
9093 else if (strcmp(value
, "next") == 0)
9094 spawn_position
= SWM_STACK_ABOVE
;
9095 else if (strcmp(value
, "previous") == 0)
9096 spawn_position
= SWM_STACK_BELOW
;
9098 errx(1, "spawn_position");
9100 case SWM_S_SPAWN_TERM
:
9101 setconfspawn("term", value
, 0);
9102 setconfspawn("spawn_term", value
, 0);
9105 /* No longer needed; leave to not break old conf files. */
9107 case SWM_S_SS_ENABLED
:
9108 /* No longer needed; leave to not break old conf files. */
9110 case SWM_S_STACK_ENABLED
:
9111 stack_enabled
= (atoi(value
) != 0);
9113 case SWM_S_TERM_WIDTH
:
9114 term_width
= atoi(value
);
9118 case SWM_S_TILE_GAP
:
9119 tile_gap
= atoi(value
);
9121 case SWM_S_URGENT_COLLAPSE
:
9122 urgent_collapse
= (atoi(value
) != 0);
9124 case SWM_S_URGENT_ENABLED
:
9125 urgent_enabled
= (atoi(value
) != 0);
9127 case SWM_S_VERBOSE_LAYOUT
:
9128 verbose_layout
= (atoi(value
) != 0);
9129 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
9131 layouts
[i
].l_string
= fancy_stacker
;
9133 layouts
[i
].l_string
= plain_stacker
;
9136 case SWM_S_WARP_FOCUS
:
9137 warp_focus
= (atoi(value
) != 0);
9139 case SWM_S_WARP_POINTER
:
9140 warp_pointer
= (atoi(value
) != 0);
9142 case SWM_S_WINDOW_CLASS_ENABLED
:
9143 window_class_enabled
= (atoi(value
) != 0);
9145 case SWM_S_WINDOW_INSTANCE_ENABLED
:
9146 window_instance_enabled
= (atoi(value
) != 0);
9148 case SWM_S_WINDOW_NAME_ENABLED
:
9149 window_name_enabled
= (atoi(value
) != 0);
9151 case SWM_S_WORKSPACE_CLAMP
:
9152 workspace_clamp
= (atoi(value
) != 0);
9154 case SWM_S_WORKSPACE_LIMIT
:
9155 workspace_limit
= atoi(value
);
9156 if (workspace_limit
> SWM_WS_MAX
)
9157 workspace_limit
= SWM_WS_MAX
;
9158 else if (workspace_limit
< 1)
9159 workspace_limit
= 1;
9161 ewmh_update_desktops();
9163 case SWM_S_WORKSPACE_NAME
:
9164 if (getenv("SWM_STARTED") != NULL
)
9168 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9169 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
9171 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9172 errx(1, "setconfvalue: workspace_name: invalid "
9173 "workspace %d.", ws_id
+ 1);
9175 num_screens
= get_screen_count();
9176 for (i
= 0; i
< num_screens
; ++i
) {
9177 ws
= (struct workspace
*)&screens
[i
].ws
;
9179 if (strlen(s
) > 0) {
9180 free(ws
[ws_id
].name
);
9181 if ((ws
[ws_id
].name
= strdup(s
)) == NULL
)
9182 err(1, "setconfvalue: workspace_name.");
9184 ewmh_update_desktop_names();
9185 ewmh_get_desktop_names();
9196 setconfmodkey(const char *selector
, const char *value
, int flags
)
9198 /* suppress unused warnings since vars are needed */
9202 if (strncasecmp(value
, "Mod1", strlen("Mod1")) == 0)
9203 update_modkey(XCB_MOD_MASK_1
);
9204 else if (strncasecmp(value
, "Mod2", strlen("Mod2")) == 0)
9205 update_modkey(XCB_MOD_MASK_2
);
9206 else if (strncasecmp(value
, "Mod3", strlen("Mod3")) == 0)
9207 update_modkey(XCB_MOD_MASK_3
);
9208 else if (strncasecmp(value
, "Mod4", strlen("Mod4")) == 0)
9209 update_modkey(XCB_MOD_MASK_4
);
9210 else if (strncasecmp(value
, "Mod5", strlen("Mod5")) == 0)
9211 update_modkey(XCB_MOD_MASK_5
);
9218 setconfcolor(const char *selector
, const char *value
, int flags
)
9220 int first
, last
, i
= 0, num_screens
;
9222 num_screens
= get_screen_count();
9224 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
9225 if (selector
== NULL
|| strlen(selector
) == 0 ||
9226 (last
= atoi(selector
) - 1) < 0) {
9228 last
= num_screens
- 1;
9233 if (last
>= num_screens
) {
9234 add_startup_exception("invalid screen index: %d out of bounds "
9235 "(maximum %d)", last
+ 1, num_screens
);
9239 for (i
= first
; i
<= last
; ++i
) {
9240 setscreencolor(value
, i
, flags
);
9243 * When setting focus/unfocus colors, we need to also
9244 * set maximize colors to match if they haven't been customized.
9246 if (flags
== SWM_S_COLOR_FOCUS
&&
9247 !screens
[i
].c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].manual
)
9248 setscreencolor(value
, i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
9249 else if (flags
== SWM_S_COLOR_UNFOCUS
&&
9250 !screens
[i
].c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].manual
)
9251 setscreencolor(value
, i
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
9253 screens
[i
].c
[flags
].manual
= 1;
9260 setconfregion(const char *selector
, const char *value
, int flags
)
9262 /* suppress unused warnings since vars are needed */
9266 custom_region(value
);
9271 setautorun(const char *selector
, const char *value
, int flags
)
9275 char *ap
, *sp
, *str
;
9281 /* suppress unused warnings since vars are needed */
9285 if (getenv("SWM_STARTED"))
9289 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9290 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
9292 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9293 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
9295 sp
= str
= expand_tilde((char *)&s
);
9298 * This is a little intricate
9300 * If the pid already exists we simply reuse it because it means it was
9301 * used before AND not claimed by manage_window. We get away with
9302 * altering it in the parent after INSERT because this can not be a race
9305 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
9308 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
9310 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
9311 err(1, "setautorun: realloc");
9312 a
.argv
[argc
- 1] = ap
;
9315 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
9316 err(1, "setautorun: realloc");
9317 a
.argv
[argc
] = NULL
;
9319 if ((pid
= fork()) == 0) {
9320 spawn(ws_id
, &a
, true);
9330 p
= calloc(1, sizeof *p
);
9333 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
9343 setlayout(const char *selector
, const char *value
, int flags
)
9345 struct workspace
*ws
;
9346 int ws_id
, i
, x
, mg
, ma
, si
, ar
;
9347 int st
= SWM_V_STACK
, num_screens
;
9351 /* suppress unused warnings since vars are needed */
9355 if (getenv("SWM_STARTED"))
9359 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
9360 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
9361 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9362 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9365 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9366 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
9368 if (strcasecmp(s
, "vertical") == 0)
9370 else if (strcasecmp(s
, "vertical_flip") == 0) {
9373 } else if (strcasecmp(s
, "horizontal") == 0)
9375 else if (strcasecmp(s
, "horizontal_flip") == 0) {
9378 } else if (strcasecmp(s
, "fullscreen") == 0)
9381 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9382 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9385 num_screens
= get_screen_count();
9386 for (i
= 0; i
< num_screens
; i
++) {
9387 ws
= (struct workspace
*)&screens
[i
].ws
;
9388 ws
[ws_id
].cur_layout
= &layouts
[st
];
9390 ws
[ws_id
].always_raise
= (ar
!= 0);
9391 if (st
== SWM_MAX_STACK
)
9395 for (x
= 0; x
< abs(mg
); x
++) {
9396 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9397 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
9398 SWM_ARG_ID_MASTERSHRINK
);
9401 for (x
= 0; x
< abs(ma
); x
++) {
9402 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9403 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
9404 SWM_ARG_ID_MASTERDEL
);
9407 for (x
= 0; x
< abs(si
); x
++) {
9408 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9409 si
>= 0 ? SWM_ARG_ID_STACKINC
:
9410 SWM_ARG_ID_STACKDEC
);
9414 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9415 SWM_ARG_ID_FLIPLAYOUT
);
9422 /* config options */
9423 struct config_option
{
9425 int (*func
)(const char*, const char*, int);
9428 struct config_option configopt
[] = {
9429 { "autorun", setautorun
, 0 },
9430 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
9431 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
9432 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
9433 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
9434 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
9435 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
9436 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
9437 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
9438 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
9439 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
9440 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
9441 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
9442 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
9443 { "bind", setconfbinding
, 0 },
9444 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
9445 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
9446 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
9447 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
9448 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
9449 { "color_focus_maximized", setconfcolor
, SWM_S_COLOR_FOCUS_MAXIMIZED
},
9450 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
9451 { "color_unfocus_maximized", setconfcolor
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
},
9452 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
9453 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
9454 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
9455 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
9456 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
9457 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
9458 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
9459 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
9460 { "iconic_enabled", setconfvalue
, SWM_S_ICONIC_ENABLED
},
9461 { "java_workaround", setconfvalue
, SWM_S_JAVA_WORKAROUND
},
9462 { "keyboard_mapping", setkeymapping
, 0 },
9463 { "layout", setlayout
, 0 },
9464 { "maximize_hide_bar", setconfvalue
, SWM_S_MAXIMIZE_HIDE_BAR
},
9465 { "modkey", setconfmodkey
, 0 },
9466 { "program", setconfspawn
, 0 },
9467 { "quirk", setconfquirk
, 0 },
9468 { "region", setconfregion
, 0 },
9469 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
9470 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
9471 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
9472 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
9473 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
9474 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
9475 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
9476 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
9477 { "title_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
}, /* For backwards compat. */
9478 { "title_name_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
}, /* For backwards compat. */
9479 { "urgent_collapse", setconfvalue
, SWM_S_URGENT_COLLAPSE
},
9480 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
9481 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
9482 { "warp_focus", setconfvalue
, SWM_S_WARP_FOCUS
},
9483 { "warp_pointer", setconfvalue
, SWM_S_WARP_POINTER
},
9484 { "window_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
},
9485 { "window_instance_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
},
9486 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
9487 { "workspace_clamp", setconfvalue
, SWM_S_WORKSPACE_CLAMP
},
9488 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
9489 { "name", setconfvalue
, SWM_S_WORKSPACE_NAME
},
9493 _add_startup_exception(const char *fmt
, va_list ap
)
9495 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
9496 warn("%s: asprintf", __func__
);
9500 add_startup_exception(const char *fmt
, ...)
9506 if (startup_exception
)
9509 /* force bar to be enabled due to exception */
9513 _add_startup_exception(fmt
, ap
);
9518 conf_load(const char *filename
, int keymapping
)
9521 char *line
= NULL
, *cp
, *ce
, *optsub
, *optval
= NULL
;
9522 size_t linelen
, lineno
= 0;
9523 int wordlen
, i
, optidx
, count
;
9524 struct config_option
*opt
= NULL
;
9526 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
9528 if (filename
== NULL
) {
9529 warnx("conf_load: no filename");
9533 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
9535 if ((config
= fopen(filename
, "r")) == NULL
) {
9536 warn("conf_load: fopen: %s", filename
);
9540 while (!feof(config
)) {
9544 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
9545 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
9547 err(1, "%s", filename
);
9552 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9553 if (cp
[0] == '\0') {
9557 /* get config option */
9558 wordlen
= strcspn(cp
, "=[ \t\n");
9560 add_startup_exception("%s: line %zd: no option found",
9565 for (i
= 0; i
< LENGTH(configopt
); i
++) {
9566 opt
= &configopt
[i
];
9567 if (strncasecmp(cp
, opt
->optname
, wordlen
) == 0 &&
9568 (int)strlen(opt
->optname
) == wordlen
) {
9574 add_startup_exception("%s: line %zd: unknown option "
9575 "%.*s", filename
, lineno
, wordlen
, cp
);
9578 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
9579 add_startup_exception("%s: line %zd: invalid option "
9580 "%.*s", filename
, lineno
, wordlen
, cp
);
9584 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9586 /* get [selector] if any */
9591 /* Get length of selector. */
9592 for (ce
= ++cp
; *ce
!= '\0'; ++ce
) {
9593 /* Find matching (not escaped) bracket. */
9594 if (*ce
== ']' && *(ce
- 1) != '\\') {
9601 add_startup_exception("%s: line %zd: syntax "
9602 "error: unterminated selector", filename
,
9607 /* ce points at ']'; terminate optsub. */
9612 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
9615 if (strlen(optval
) == 0) {
9616 add_startup_exception("%s: line %zd: must supply value "
9617 "to %s", filename
, lineno
,
9618 configopt
[optidx
].optname
);
9621 /* call function to deal with it all */
9622 if (configopt
[optidx
].func(optsub
, optval
,
9623 configopt
[optidx
].funcflags
) != 0) {
9624 add_startup_exception("%s: line %zd: invalid data for "
9625 "%s", filename
, lineno
, configopt
[optidx
].optname
);
9634 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
9640 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
9642 struct ws_win
*parent
, *w
;
9643 struct swm_region
*r
;
9644 struct workspace
*ws
;
9645 xcb_icccm_wm_hints_t wmh
;
9647 parent
= find_window(win
->transient
);
9649 parent
->focus_child
= win
;
9651 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
9652 " for %#x trans %#x\n", win
->id
, win
->transient
);
9654 r
= root_to_region(win
->s
->root
, SWM_CK_ALL
);
9656 /* parent doen't exist in our window list */
9657 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
9658 if (xcb_icccm_get_wm_hints_reply(conn
,
9659 xcb_icccm_get_wm_hints(conn
, w
->id
),
9661 warnx("can't get hints for %#x", w
->id
);
9665 if (win
->hints
.window_group
!= wmh
.window_group
)
9668 w
->focus_child
= win
;
9669 win
->transient
= w
->id
;
9671 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
9672 "transient to %#x\n", win
->transient
);
9679 window_get_pid(xcb_window_t win
)
9684 xcb_get_property_cookie_t pc
;
9685 xcb_get_property_reply_t
*pr
;
9687 apid
= get_atom_from_string("_NET_WM_PID");
9688 if (apid
== XCB_ATOM_NONE
)
9691 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
9692 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9695 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
9700 if (pr
->type
== apid
&& pr
->format
== 32)
9701 ret
= *((pid_t
*)xcb_get_property_value(pr
));
9707 apid
= get_atom_from_string("_SWM_PID");
9708 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
9710 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9713 if (pr
->type
!= apid
) {
9718 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
9725 get_swm_ws(xcb_window_t id
)
9731 xcb_get_property_reply_t
*gpr
;
9733 gpr
= xcb_get_property_reply(conn
,
9734 xcb_get_property(conn
, 0, id
, a_swm_ws
,
9735 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
9740 proplen
= xcb_get_property_value_length(gpr
);
9742 prop
= malloc(proplen
+ 1);
9745 xcb_get_property_value(gpr
),
9747 prop
[proplen
] = '\0';
9754 DNPRINTF(SWM_D_PROP
, "get_swm_ws: _SWM_WS: %s\n", prop
);
9755 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
9757 DNPRINTF(SWM_D_PROP
, "get_swm_ws: win #%s: %s",
9767 get_ws_idx(struct ws_win
*win
)
9769 xcb_get_property_reply_t
*gpr
;
9775 gpr
= xcb_get_property_reply(conn
,
9776 xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
9777 XCB_ATOM_CARDINAL
, 0, 1),
9780 if (gpr
->type
== XCB_ATOM_CARDINAL
&& gpr
->format
== 32)
9781 ws_idx
= *((int *)xcb_get_property_value(gpr
));
9785 if (ws_idx
== -1 && !(win
->quirks
& SWM_Q_IGNORESPAWNWS
))
9786 ws_idx
= get_swm_ws(win
->id
);
9788 if (ws_idx
> workspace_limit
- 1 || ws_idx
< -1)
9791 DNPRINTF(SWM_D_PROP
, "get_ws_idx: win %#x, ws_idx: %d\n", win
->id
,
9798 reparent_window(struct ws_win
*win
)
9800 xcb_void_cookie_t c
;
9801 xcb_generic_error_t
*error
;
9804 win
->frame
= xcb_generate_id(conn
);
9806 DNPRINTF(SWM_D_MISC
, "reparent_window: win %#x, frame: %#x\n",
9807 win
->id
, win
->frame
);
9810 XCB_EVENT_MASK_ENTER_WINDOW
|
9811 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
9812 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
9813 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
|
9814 XCB_EVENT_MASK_EXPOSURE
;
9816 wa
[0] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
9819 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
->frame
, win
->s
->root
,
9820 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), 0,
9821 XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
9822 XCB_CW_EVENT_MASK
, wa
);
9824 win
->state
= SWM_WIN_STATE_REPARENTING
;
9825 c
= xcb_reparent_window_checked(conn
, win
->id
, win
->frame
, 0, 0);
9826 if ((error
= xcb_request_check(conn
, c
))) {
9827 DNPRINTF(SWM_D_MISC
, "reparent_window: error:\n");
9832 xcb_destroy_window(conn
, win
->frame
);
9833 win
->frame
= XCB_WINDOW_NONE
;
9835 xcb_change_save_set(conn
, XCB_SET_MODE_INSERT
, win
->id
);
9837 DNPRINTF(SWM_D_MISC
, "reparent_window: done.\n");
9841 unparent_window(struct ws_win
*win
)
9843 xcb_change_save_set(conn
, XCB_SET_MODE_DELETE
, win
->id
);
9844 xcb_reparent_window(conn
, win
->id
, win
->s
->root
, X(win
), Y(win
));
9845 xcb_destroy_window(conn
, win
->frame
);
9846 win
->frame
= XCB_WINDOW_NONE
;
9847 win
->state
= SWM_WIN_STATE_UNPARENTING
;
9851 manage_window(xcb_window_t id
, int spawn_pos
, bool mapping
)
9853 struct ws_win
*win
= NULL
, *ww
;
9854 struct swm_region
*r
;
9857 xcb_get_geometry_reply_t
*gr
;
9858 xcb_get_window_attributes_reply_t
*war
= NULL
;
9859 xcb_window_t trans
= XCB_WINDOW_NONE
;
9860 uint32_t i
, wa
[1], new_flags
;
9861 int ws_idx
, force_ws
= -1;
9862 char *class, *instance
, *name
;
9865 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region bar; "
9870 if (find_region(id
)) {
9871 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region window; "
9876 if ((win
= find_window(id
)) != NULL
) {
9877 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x already "
9878 "managed; skipping.)\n", id
);
9879 return (win
); /* Already managed. */
9882 /* See if window is on the unmanaged list. */
9883 if ((win
= find_unmanaged_window(id
)) != NULL
) {
9884 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x found on "
9885 "unmanaged list.\n", id
);
9886 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
9888 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is new.\n", id
);
9891 war
= xcb_get_window_attributes_reply(conn
,
9892 xcb_get_window_attributes(conn
, id
), NULL
);
9894 DNPRINTF(SWM_D_EVENT
, "manage_window: window lost.\n");
9898 if (war
->override_redirect
) {
9899 DNPRINTF(SWM_D_EVENT
, "manage_window: override_redirect; "
9904 if (!mapping
&& war
->map_state
== XCB_MAP_STATE_UNMAPPED
&&
9905 get_win_state(id
) == XCB_ICCCM_WM_STATE_WITHDRAWN
) {
9906 DNPRINTF(SWM_D_EVENT
, "manage_window: window withdrawn; "
9911 /* Try to get initial window geometry. */
9912 gr
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, id
), NULL
);
9914 DNPRINTF(SWM_D_MISC
, "manage_window: get geometry failed.\n");
9918 /* Figure out which region the window belongs to. */
9919 r
= root_to_region(gr
->root
, SWM_CK_ALL
);
9921 /* Handle special windows with special _NET_WM_WINDOW_TYPE */
9922 if (ewmh_handle_special_types(id
, r
)) {
9923 DNPRINTF(SWM_D_EVENT
, "manage_window: "
9924 "unmanaged ewmh window type\n");
9929 /* Create and initialize ws_win object. */
9930 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
9931 err(1, "manage_window: calloc: failed to allocate "
9932 "memory for new window");
9937 /* Ignore window border if there is one. */
9938 WIDTH(win
) = gr
->width
;
9939 HEIGHT(win
) = gr
->height
;
9940 X(win
) = gr
->x
+ gr
->border_width
;
9941 Y(win
) = gr
->y
+ gr
->border_width
;
9942 win
->bordered
= false;
9943 win
->mapped
= (war
->map_state
!= XCB_MAP_STATE_UNMAPPED
);
9944 win
->s
= r
->s
; /* this never changes */
9948 /* Select which X events to monitor and set border pixel color. */
9949 wa
[0] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
9950 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
9952 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_EVENT_MASK
, wa
);
9954 /* Get WM_SIZE_HINTS. */
9955 xcb_icccm_get_wm_normal_hints_reply(conn
,
9956 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
9960 xcb_icccm_get_wm_hints_reply(conn
,
9961 xcb_icccm_get_wm_hints(conn
, win
->id
),
9964 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
9965 xcb_icccm_get_wm_transient_for_reply(conn
,
9966 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
9969 win
->transient
= trans
;
9970 set_child_transient(win
, &win
->transient
);
9973 /* Get WM_PROTOCOLS. */
9974 get_wm_protocols(win
);
9977 /* Must be after getting WM_HINTS and WM_PROTOCOLS. */
9978 DNPRINTF(SWM_D_FOCUS
, "manage_window: input_model: %s\n",
9979 get_win_input_model(win
));
9982 /* Set initial quirks based on EWMH. */
9983 ewmh_autoquirk(win
);
9985 /* Determine initial quirks. */
9986 xcb_icccm_get_wm_class_reply(conn
,
9987 xcb_icccm_get_wm_class(conn
, win
->id
),
9990 class = win
->ch
.class_name
? win
->ch
.class_name
: "";
9991 instance
= win
->ch
.instance_name
? win
->ch
.instance_name
: "";
9992 name
= get_win_name(win
->id
);
9994 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, instance: %s, "
9995 "name: %s\n", class, instance
, name
);
9997 /* java is retarded so treat it special */
9998 if (strstr(instance
, "sun-awt")) {
9999 DNPRINTF(SWM_D_CLASS
, "manage_window: java window detected.\n");
10003 TAILQ_FOREACH(qp
, &quirks
, entry
) {
10004 if (regexec(&qp
->regex_class
, class, 0, NULL
, 0) == 0 &&
10005 regexec(&qp
->regex_instance
, instance
, 0, NULL
, 0) == 0 &&
10006 regexec(&qp
->regex_name
, name
, 0, NULL
, 0) == 0) {
10007 DNPRINTF(SWM_D_CLASS
, "manage_window: matched "
10008 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp
->class,
10009 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
10010 win
->quirks
= qp
->quirk
;
10011 if (qp
->ws
>= 0 && qp
->ws
< workspace_limit
)
10018 /* Reset font sizes (the bruteforce way; no default keybinding). */
10019 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
10020 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
10021 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
10022 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
10023 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
10026 /* Figure out which workspace the window belongs to. */
10027 if (!(win
->quirks
& SWM_Q_IGNOREPID
) &&
10028 (p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
10029 win
->ws
= &r
->s
->ws
[p
->ws
];
10030 TAILQ_REMOVE(&pidlist
, p
, entry
);
10033 } else if ((ws_idx
= get_ws_idx(win
)) != -1 &&
10035 /* _SWM_WS is set; use that. */
10036 win
->ws
= &r
->s
->ws
[ws_idx
];
10037 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
10038 /* Launch transients in the same ws as parent. */
10044 if (force_ws
!= -1)
10045 win
->ws
= &r
->s
->ws
[force_ws
];
10047 /* Set the _NET_WM_DESKTOP atom. */
10048 DNPRINTF(SWM_D_PROP
, "manage_window: set _NET_WM_DESKTOP: %d\n",
10050 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
10051 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &win
->ws
->idx
);
10053 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
10054 xcb_delete_property(conn
, win
->id
, a_swm_ws
);
10056 /* WS must already be set for this to work. */
10057 store_float_geom(win
);
10059 /* Make sure window is positioned inside its region, if its active. */
10061 region_containment(win
, r
, SWM_CW_ALLSIDES
|
10062 SWM_CW_HARDBOUNDARY
);
10063 update_window(win
);
10066 /* Figure out where to insert the window in the workspace list. */
10067 if (trans
&& (ww
= find_window(trans
)))
10068 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
10069 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_ABOVE
)
10070 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
10072 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_BELOW
)
10073 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
10074 else switch (spawn_pos
) {
10076 case SWM_STACK_TOP
:
10077 case SWM_STACK_ABOVE
:
10078 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
10080 case SWM_STACK_BOTTOM
:
10081 case SWM_STACK_BELOW
:
10082 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
10085 ewmh_update_client_list();
10087 TAILQ_INSERT_HEAD(&win
->ws
->stack
, win
, stack_entry
);
10090 /* Get/apply initial _NET_WM_STATE */
10091 ewmh_get_wm_state(win
);
10093 /* Apply quirks. */
10094 new_flags
= win
->ewmh_flags
;
10096 if (win
->quirks
& SWM_Q_FLOAT
)
10097 new_flags
|= EWMH_F_ABOVE
;
10099 if (win
->quirks
& SWM_Q_ANYWHERE
)
10100 new_flags
|= SWM_F_MANUAL
;
10102 ewmh_apply_flags(win
, new_flags
);
10103 ewmh_update_wm_state(win
);
10105 /* Set initial _NET_WM_ALLOWED_ACTIONS */
10106 ewmh_update_actions(win
);
10108 reparent_window(win
);
10110 DNPRINTF(SWM_D_MISC
, "manage_window: done. win %#x, (x,y) w x h: "
10111 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win
->id
,
10112 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
10113 YESNO(ICONIC(win
)), win
->transient
);
10120 free_window(struct ws_win
*win
)
10122 DNPRINTF(SWM_D_MISC
, "free_window: win %#x\n", WINID(win
));
10127 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
10130 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
10133 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
10137 unmanage_window(struct ws_win
*win
)
10139 DNPRINTF(SWM_D_MISC
, "unmanage_window: win %#x\n", WINID(win
));
10145 unparent_window(win
);
10147 TAILQ_REMOVE(&win
->ws
->stack
, win
, stack_entry
);
10148 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
10149 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
10151 ewmh_update_client_list();
10155 expose(xcb_expose_event_t
*e
)
10160 struct workspace
*ws
;
10163 DNPRINTF(SWM_D_EVENT
, "expose: win %#x, count: %d\n", e
->window
,
10169 if ((b
= find_bar(e
->window
))) {
10172 } else if ((w
= find_window(e
->window
)) && w
->frame
== e
->window
) {
10176 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
10182 DNPRINTF(SWM_D_EVENT
, "expose: done\n");
10186 focusin(xcb_focus_in_event_t
*e
)
10188 struct ws_win
*win
;
10190 DNPRINTF(SWM_D_EVENT
, "focusin: win %#x, mode: %s(%u), "
10191 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10192 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10193 if ((win
= find_window(e
->event
)) && win
!= win
->ws
->focus
&&
10194 win
!= win
->ws
->focus_pending
&&
10195 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10196 win
->ws
->focus_prev
= win
->ws
->focus
;
10197 win
->ws
->focus
= win
;
10198 win
->ws
->focus_pending
= NULL
;
10200 if (win
->ws
->focus_prev
)
10201 draw_frame(win
->ws
->focus_prev
);
10209 focusout(xcb_focus_out_event_t
*e
)
10211 DNPRINTF(SWM_D_EVENT
, "focusout: win %#x, mode: %s(%u), "
10212 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10213 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10218 keypress(xcb_key_press_event_t
*e
)
10221 struct binding
*bp
;
10222 xcb_keysym_t keysym
;
10223 bool replay
= true;
10225 last_event_time
= e
->time
;
10227 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
10229 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
10230 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10231 "state: %u, cleaned: %u, same_screen: %s\n", keysym
, e
->event
,
10232 e
->event_x
, e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
,
10233 e
->root_y
, e
->child
, e
->state
, CLEANMASK(e
->state
),
10234 YESNO(e
->same_screen
));
10236 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10238 /* Look for catch-all. */
10239 if ((bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
)) == NULL
)
10243 replay
= bp
->flags
& BINDING_F_REPLAY
;
10245 if ((ap
= &actions
[bp
->action
]) == NULL
)
10248 if (bp
->action
== FN_SPAWN_CUSTOM
)
10249 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10252 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10254 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10258 DNPRINTF(SWM_D_EVENT
, "keypress: replaying.\n");
10259 /* Pass keypress to event window and unfreeze keyboard queue. */
10260 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10262 /* Release freeze. */
10263 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10267 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
10271 keyrelease(xcb_key_release_event_t
*e
)
10274 struct binding
*bp
;
10275 xcb_keysym_t keysym
;
10277 last_event_time
= e
->time
;
10279 keysym
= xcb_key_release_lookup_keysym(syms
, e
, 0);
10281 DNPRINTF(SWM_D_EVENT
, "keyrelease: keysym: %u, win (x,y): %#x (%d,%d), "
10282 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10283 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
10284 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
10285 e
->child
, e
->state
, YESNO(e
->same_screen
));
10287 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10289 /* Look for catch-all. */
10290 bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
);
10292 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10293 bp
->flags
& BINDING_F_REPLAY
) {
10294 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10295 DNPRINTF(SWM_D_EVENT
, "keyrelease: replaying.\n");
10297 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10298 DNPRINTF(SWM_D_EVENT
, "keyrelease: unfreezing.\n");
10303 DNPRINTF(SWM_D_EVENT
, "keyrelease: done.\n");
10307 buttonpress(xcb_button_press_event_t
*e
)
10309 struct ws_win
*win
= NULL
, *newf
;
10310 struct swm_region
*r
, *old_r
;
10312 struct binding
*bp
;
10313 bool replay
= true;
10315 last_event_time
= e
->time
;
10317 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): %#x (%d,%d), "
10318 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10319 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10320 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10321 e
->state
, YESNO(e
->same_screen
));
10323 if (e
->event
== e
->root
) {
10325 win
= find_window(e
->child
);
10327 /* Focus on empty region */
10328 /* If no windows on region if its empty. */
10329 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10330 if (r
&& TAILQ_EMPTY(&r
->ws
->winlist
)) {
10331 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
10332 if (old_r
&& old_r
!= r
)
10333 unfocus_win(old_r
->ws
->focus
);
10335 DNPRINTF(SWM_D_FOCUS
, "buttonpress: "
10336 "set_input_focus: %#x, revert-to: parent, "
10337 "time: %#x\n", e
->root
, e
->time
);
10338 xcb_set_input_focus(conn
,
10339 XCB_INPUT_FOCUS_POINTER_ROOT
,
10342 /* Clear bar since empty. */
10345 /* No need to replay event. */
10350 win
= find_window(e
->event
);
10354 newf
= get_focus_magic(win
);
10355 if (win
->ws
->focus
== newf
&& newf
!= win
) {
10356 if (win
->focus_child
== win
)
10357 win
->focus_child
= NULL
;
10363 /* Handle any bound action. */
10364 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10366 /* Look for catch-all. */
10367 if ((bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
)) == NULL
)
10372 replay
= bp
->flags
& BINDING_F_REPLAY
;
10374 if ((ap
= &actions
[bp
->action
]) == NULL
)
10377 if (bp
->action
== FN_SPAWN_CUSTOM
)
10378 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10381 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10383 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10387 DNPRINTF(SWM_D_EVENT
, "buttonpress: replaying.\n");
10388 /* Replay event to event window */
10389 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10391 /* Unfreeze grab events. */
10392 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10399 buttonrelease(xcb_button_release_event_t
*e
)
10402 struct binding
*bp
;
10404 last_event_time
= e
->time
;
10406 DNPRINTF(SWM_D_EVENT
, "buttonrelease: win (x,y): %#x (%d,%d), "
10407 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10408 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10409 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10410 e
->state
, YESNO(e
->same_screen
));
10412 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10414 /* Look for catch-all. */
10415 bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
);
10417 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10418 bp
->flags
& BINDING_F_REPLAY
) {
10419 DNPRINTF(SWM_D_EVENT
, "buttonrelease: replaying.\n");
10420 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10422 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10430 get_win_input_model(struct ws_win
*win
)
10434 * Input Model Input Field WM_TAKE_FOCUS
10435 * No Input False Absent
10436 * Passive True Absent
10437 * Locally Active True Present
10438 * Globally Active False Present
10441 if (ACCEPTS_FOCUS(win
))
10442 inputmodel
= (win
->take_focus
) ? "Locally Active" : "Passive";
10444 inputmodel
= (win
->take_focus
) ? "Globally Active" : "No Input";
10450 print_win_geom(xcb_window_t w
)
10452 xcb_get_geometry_reply_t
*wa
;
10454 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
10456 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: %#x\n",
10461 DNPRINTF(SWM_D_MISC
, "print_win_geom: win %#x, root: %#x, "
10462 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
10463 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
10472 get_stack_mode_name(uint8_t mode
)
10477 case XCB_STACK_MODE_ABOVE
:
10480 case XCB_STACK_MODE_BELOW
:
10483 case XCB_STACK_MODE_TOP_IF
:
10486 case XCB_STACK_MODE_BOTTOM_IF
:
10489 case XCB_STACK_MODE_OPPOSITE
:
10501 configurerequest(xcb_configure_request_event_t
*e
)
10503 struct ws_win
*win
;
10504 struct swm_region
*r
= NULL
;
10506 uint32_t wc
[7] = {0};
10510 if ((win
= find_window(e
->window
)) == NULL
)
10511 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10515 if (swm_debug
& SWM_D_EVENT
) {
10516 print_win_geom(e
->window
);
10518 DNPRINTF(SWM_D_EVENT
, "configurerequest: win %#x, "
10519 "parent: %#x, new: %s, value_mask: %u { ", e
->window
,
10520 e
->parent
, YESNO(new), e
->value_mask
);
10521 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
10522 DPRINTF("X: %d ", e
->x
);
10523 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
10524 DPRINTF("Y: %d ", e
->y
);
10525 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10526 DPRINTF("W: %u ", e
->width
);
10527 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10528 DPRINTF("H: %u ", e
->height
);
10529 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
10530 DPRINTF("Border: %u ", e
->border_width
);
10531 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
10532 DPRINTF("Sibling: %#x ", e
->sibling
);
10533 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
10534 DPRINTF("StackMode: %s(%u) ",
10535 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
10541 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10542 mask
|= XCB_CONFIG_WINDOW_X
;
10545 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10546 mask
|= XCB_CONFIG_WINDOW_Y
;
10549 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
10550 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
10551 wc
[i
++] = e
->width
;
10553 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
10554 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
10555 wc
[i
++] = e
->height
;
10557 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
10558 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
10559 wc
[i
++] = e
->border_width
;
10561 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
10562 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
10563 wc
[i
++] = e
->sibling
;
10565 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
10566 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
10567 wc
[i
++] = e
->stack_mode
;
10571 xcb_configure_window(conn
, e
->window
, mask
, wc
);
10574 } else if ((!MANUAL(win
) || win
->quirks
& SWM_Q_ANYWHERE
) &&
10575 !FULLSCREEN(win
) && !MAXIMIZED(win
)) {
10578 else if (win
->ws
->old_r
)
10579 r
= win
->ws
->old_r
;
10581 /* windows are centered unless ANYWHERE quirk is set. */
10582 if (win
->quirks
& SWM_Q_ANYWHERE
) {
10583 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10584 win
->g_float
.x
= e
->x
;
10586 win
->g_float
.x
-= X(r
);
10589 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10590 win
->g_float
.y
= e
->y
;
10592 win
->g_float
.y
-= Y(r
);
10596 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10597 win
->g_float
.w
= e
->width
;
10599 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10600 win
->g_float
.h
= e
->height
;
10602 win
->g_floatvalid
= true;
10604 if (!MAXIMIZED(win
) && !FULLSCREEN(win
) &&
10605 (TRANS(win
) || (ABOVE(win
) &&
10606 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]))) {
10607 WIDTH(win
) = win
->g_float
.w
;
10608 HEIGHT(win
) = win
->g_float
.h
;
10611 update_floater(win
);
10614 config_win(win
, e
);
10618 config_win(win
, e
);
10622 config_win(win
, e
);
10626 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
10630 configurenotify(xcb_configure_notify_event_t
*e
)
10632 struct ws_win
*win
;
10634 DNPRINTF(SWM_D_EVENT
, "configurenotify: win %#x, event win: %#x, "
10635 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
10636 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
10637 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
10638 YESNO(e
->override_redirect
));
10640 win
= find_window(e
->window
);
10643 if (font_adjusted
&& win
->ws
->r
) {
10651 destroynotify(xcb_destroy_notify_event_t
*e
)
10653 struct ws_win
*win
;
10654 struct workspace
*ws
;
10656 DNPRINTF(SWM_D_EVENT
, "destroynotify: win %#x\n", e
->window
);
10658 if ((win
= find_window(e
->window
)) == NULL
) {
10659 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10661 /* Window is on unmanaged list. */
10662 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10669 if (win
->frame
== e
->window
) {
10670 DNPRINTF(SWM_D_EVENT
, "destroynotify: frame for win %#x\n",
10672 win
->frame
= XCB_WINDOW_NONE
;
10676 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10677 /* If we were focused, make sure we focus on something else. */
10678 if (win
== ws
->focus
) {
10679 ws
->focus_pending
= get_focus_prev(win
);
10680 if (ws
->focus_pending
== win
)
10681 ws
->focus_pending
= NULL
;
10685 unmanage_window(win
);
10686 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10690 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
10691 if (ws
->focus_pending
) {
10692 focus_win(ws
->focus_pending
);
10693 ws
->focus_pending
= NULL
;
10694 } else if (ws
->focus
== NULL
) {
10695 DNPRINTF(SWM_D_FOCUS
, "destroynotify: set_input_focus: "
10696 "%#x, revert-to: parent, time: 0\n", ws
->r
->id
);
10697 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
10698 ws
->r
->id
, XCB_CURRENT_TIME
);
10705 DNPRINTF(SWM_D_EVENT
, "destroynotify: done.\n");
10710 get_notify_detail_label(uint8_t detail
)
10715 case XCB_NOTIFY_DETAIL_ANCESTOR
:
10716 label
= "Ancestor";
10718 case XCB_NOTIFY_DETAIL_VIRTUAL
:
10721 case XCB_NOTIFY_DETAIL_INFERIOR
:
10722 label
= "Inferior";
10724 case XCB_NOTIFY_DETAIL_NONLINEAR
:
10725 label
= "Nonlinear";
10727 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
10728 label
= "NonlinearVirtual";
10730 case XCB_NOTIFY_DETAIL_POINTER
:
10733 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
10734 label
= "PointerRoot";
10736 case XCB_NOTIFY_DETAIL_NONE
:
10747 get_notify_mode_label(uint8_t mode
)
10752 case XCB_NOTIFY_MODE_NORMAL
:
10755 case XCB_NOTIFY_MODE_GRAB
:
10758 case XCB_NOTIFY_MODE_UNGRAB
:
10761 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
10762 label
= "WhileGrabbed";
10772 get_state_mask_label(uint16_t state
)
10777 case XCB_KEY_BUT_MASK_SHIFT
:
10778 label
= "ShiftMask";
10780 case XCB_KEY_BUT_MASK_LOCK
:
10781 label
= "LockMask";
10783 case XCB_KEY_BUT_MASK_CONTROL
:
10784 label
= "ControlMask";
10786 case XCB_KEY_BUT_MASK_MOD_1
:
10787 label
= "Mod1Mask";
10789 case XCB_KEY_BUT_MASK_MOD_2
:
10790 label
= "Mod2Mask";
10792 case XCB_KEY_BUT_MASK_MOD_3
:
10793 label
= "Mod3Mask";
10795 case XCB_KEY_BUT_MASK_MOD_4
:
10796 label
= "Mod4Mask";
10798 case XCB_KEY_BUT_MASK_MOD_5
:
10799 label
= "Mod5Mask";
10801 case XCB_KEY_BUT_MASK_BUTTON_1
:
10802 label
= "Button1Mask";
10804 case XCB_KEY_BUT_MASK_BUTTON_2
:
10805 label
= "Button2Mask";
10807 case XCB_KEY_BUT_MASK_BUTTON_3
:
10808 label
= "Button3Mask";
10810 case XCB_KEY_BUT_MASK_BUTTON_4
:
10811 label
= "Button4Mask";
10813 case XCB_KEY_BUT_MASK_BUTTON_5
:
10814 label
= "Button5Mask";
10828 enternotify(xcb_enter_notify_event_t
*e
)
10830 struct ws_win
*win
;
10831 struct swm_region
*r
;
10833 last_event_time
= e
->time
;
10835 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): %#x "
10836 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10837 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10838 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10839 get_notify_mode_label(e
->mode
), e
->mode
,
10840 get_notify_detail_label(e
->detail
), e
->detail
,
10841 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10842 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10845 if (e
->event
== e
->root
&& e
->child
== XCB_WINDOW_NONE
&&
10846 e
->mode
== XCB_NOTIFY_MODE_GRAB
&&
10847 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10848 DNPRINTF(SWM_D_EVENT
, "enternotify: grab inferior; ignoring.\n");
10852 if (focus_mode
== SWM_FOCUS_MANUAL
&&
10853 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10854 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
10858 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10859 e
->mode
== XCB_NOTIFY_MODE_UNGRAB
&&
10860 e
->detail
!= XCB_NOTIFY_DETAIL_ANCESTOR
) {
10861 DNPRINTF(SWM_D_EVENT
, "enternotify: ungrab; ignoring.\n");
10865 if ((win
= find_window(e
->event
)) == NULL
) {
10866 if (e
->event
== e
->root
) {
10867 /* If no windows on pointer region, then focus root. */
10868 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10870 DNPRINTF(SWM_D_EVENT
, "enternotify: "
10871 "NULL region; ignoring.\n");
10877 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
10882 if (e
->mode
== XCB_NOTIFY_MODE_NORMAL
&&
10883 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10884 DNPRINTF(SWM_D_EVENT
, "enternotify: entering from "
10885 "inferior; ignoring\n");
10889 focus_win(get_focus_magic(win
));
10892 DNPRINTF(SWM_D_EVENT
, "enternotify: done\n");
10899 leavenotify(xcb_leave_notify_event_t
*e
)
10901 last_event_time
= e
->time
;
10903 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): %#x "
10904 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10905 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10906 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10907 get_notify_mode_label(e
->mode
), e
->mode
,
10908 get_notify_detail_label(e
->detail
), e
->detail
,
10909 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10910 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10916 mapnotify(xcb_map_notify_event_t
*e
)
10918 struct ws_win
*win
, *parent
= NULL
;
10919 struct workspace
*ws
;
10921 DNPRINTF(SWM_D_EVENT
, "mapnotify: win %#x\n", e
->window
);
10923 if ((win
= manage_window(e
->window
, spawn_position
, false)) == NULL
)
10927 if (win
->state
== SWM_WIN_STATE_REPARENTING
)
10930 if (ws
->r
== NULL
) {
10935 /* Need to know if win was mapped due to ws switch. */
10936 if (ws
->state
== SWM_WS_STATE_MAPPED
) {
10937 if (ws
->focus_pending
&& TRANS(ws
->focus_pending
))
10938 parent
= find_window(win
->transient
);
10940 /* If window's parent is maximized, don't clear it. */
10941 if ((parent
== NULL
) || !MAXIMIZED(parent
))
10942 if (clear_maximized(ws
) > 0)
10946 win
->mapped
= true;
10947 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
10949 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
10950 if (ws
->focus_pending
== win
) {
10952 ws
->focus_pending
= NULL
;
10953 center_pointer(ws
->r
);
10959 DNPRINTF(SWM_D_EVENT
, "mapnotify: done\n");
10965 mappingnotify(xcb_mapping_notify_event_t
*e
)
10967 if (e
->request
!= XCB_MAPPING_POINTER
) {
10968 xcb_refresh_keyboard_mapping(syms
, e
);
10976 maprequest(xcb_map_request_event_t
*e
)
10978 struct ws_win
*win
, *w
= NULL
;
10980 DNPRINTF(SWM_D_EVENT
, "maprequest: win %#x\n",
10983 win
= manage_window(e
->window
, spawn_position
, true);
10987 /* The new window should get focus; prepare. */
10988 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10989 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
) && ACCEPTS_FOCUS(win
)) {
10990 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
10991 /* See if other wins of same type are already mapped. */
10992 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
10993 if (w
== win
|| !w
->mapped
)
10996 if (w
->ch
.class_name
&&
10997 win
->ch
.class_name
&&
10998 strcmp(w
->ch
.class_name
,
10999 win
->ch
.class_name
) == 0 &&
11000 w
->ch
.instance_name
&&
11001 win
->ch
.instance_name
&&
11002 strcmp(w
->ch
.instance_name
,
11003 win
->ch
.instance_name
) == 0)
11009 win
->ws
->focus_pending
= get_focus_magic(win
);
11012 /* All windows need to be mapped if they are in the current workspace.*/
11015 /* Ignore EnterNotify to handle the mapnotify without interference. */
11016 if (focus_mode
== SWM_FOCUS_DEFAULT
)
11017 event_drain(XCB_ENTER_NOTIFY
);
11019 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
11023 motionnotify(xcb_motion_notify_event_t
*e
)
11025 struct swm_region
*r
= NULL
;
11026 int i
, num_screens
;
11028 last_event_time
= e
->time
;
11030 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): %#x "
11031 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
11032 "child: %#x, same_screen_focus: %s, state: %d\n",
11033 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
11034 get_notify_detail_label(e
->detail
), e
->detail
,
11035 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
11036 YESNO(e
->same_screen
), e
->state
);
11038 if (focus_mode
== SWM_FOCUS_MANUAL
)
11041 num_screens
= get_screen_count();
11042 for (i
= 0; i
< num_screens
; i
++)
11043 if (screens
[i
].root
== e
->root
)
11046 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11047 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
11048 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
11056 get_atom_name(xcb_atom_t atom
)
11059 #ifdef SWM_DEBUG_ATOM_NAMES
11061 * This should be disabled during most debugging since
11062 * xcb_get_* causes an xcb_flush.
11065 xcb_get_atom_name_reply_t
*r
;
11067 r
= xcb_get_atom_name_reply(conn
,
11068 xcb_get_atom_name(conn
, atom
),
11071 len
= xcb_get_atom_name_name_length(r
);
11073 name
= malloc(len
+ 1);
11075 memcpy(name
, xcb_get_atom_name_name(r
), len
);
11089 propertynotify(xcb_property_notify_event_t
*e
)
11091 struct ws_win
*win
;
11092 struct workspace
*ws
;
11096 name
= get_atom_name(e
->atom
);
11097 DNPRINTF(SWM_D_EVENT
, "propertynotify: win %#x, atom: %s(%u), "
11098 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
11102 last_event_time
= e
->time
;
11104 win
= find_window(e
->window
);
11110 if (e
->atom
== a_state
) {
11111 /* State just changed, make sure it gets focused if mapped. */
11112 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
11113 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
11115 win
->state
== SWM_WIN_STATE_REPARENTED
&&
11116 ws
->focus_pending
== win
) {
11117 focus_win(ws
->focus_pending
);
11118 ws
->focus_pending
= NULL
;
11122 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
11123 e
->atom
== XCB_ATOM_WM_NAME
) {
11125 bar_draw(ws
->r
->bar
);
11126 } else if (e
->atom
== a_prot
) {
11127 get_wm_protocols(win
);
11128 } else if (e
->atom
== XCB_ATOM_WM_NORMAL_HINTS
) {
11129 xcb_icccm_get_wm_normal_hints_reply(conn
,
11130 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
11138 reparentnotify(xcb_reparent_notify_event_t
*e
)
11140 struct ws_win
*win
;
11142 DNPRINTF(SWM_D_EVENT
, "reparentnotify: event: %#x, win %#x, "
11143 "parent: %#x, (x,y): (%u,%u), override_redirect: %u\n",
11144 e
->event
, e
->window
, e
->parent
, e
->x
, e
->y
, e
->override_redirect
);
11146 win
= find_window(e
->window
);
11148 if (win
->state
== SWM_WIN_STATE_REPARENTING
) {
11149 win
->state
= SWM_WIN_STATE_REPARENTED
;
11151 if (win
->ws
->r
&& !ICONIC(win
))
11156 update_window(win
);
11157 update_win_stacking(win
);
11158 } else if (win
->state
== SWM_WIN_STATE_UNPARENTING
) {
11159 win
->state
= SWM_WIN_STATE_UNPARENTED
;
11165 unmapnotify(xcb_unmap_notify_event_t
*e
)
11167 struct ws_win
*win
;
11168 struct workspace
*ws
;
11170 DNPRINTF(SWM_D_EVENT
, "unmapnotify: win %#x\n", e
->window
);
11172 /* If we aren't managing the window, then ignore. */
11173 win
= find_window(e
->window
);
11174 if (win
== NULL
|| win
->id
!= e
->window
) {
11175 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore unmanaged.\n");
11179 /* Do nothing if already withdrawn. */
11180 if (!win
->mapped
&& !ICONIC(win
)) {
11181 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore withdrawn.\n");
11186 win
->mapped
= false;
11188 /* Ignore if reparenting-related. */
11189 if (win
->state
!= SWM_WIN_STATE_REPARENTED
) {
11190 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore not reparented.\n");
11194 /* If win was focused, make sure to focus on something else. */
11195 if (win
== ws
->focus
) {
11196 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
11197 ws
->focus_pending
= get_focus_prev(win
);
11198 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
11199 "focus_pending: %#x\n",
11200 WINID(ws
->focus_pending
));
11208 DNPRINTF(SWM_D_EVENT
, "unmapnotify: iconify.\n");
11209 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
11212 DNPRINTF(SWM_D_EVENT
, "unmapnotify: withdraw.\n");
11213 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
11214 unmanage_window(win
);
11219 /* Update focus if ws is active. */
11220 if (WS_FOCUSED(ws
)) {
11221 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
11222 focus_win(get_pointer_win(ws
->r
->s
->root
));
11223 } else if (ws
->focus_pending
) {
11224 focus_win(ws
->focus_pending
);
11225 ws
->focus_pending
= NULL
;
11226 } else if (ws
->focus
== NULL
) {
11227 DNPRINTF(SWM_D_FOCUS
, "unmapnotify: set_input_focus: "
11228 "%#x, revert-to: parent, time: 0\n",
11230 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11231 ws
->r
->id
, XCB_CURRENT_TIME
);
11235 center_pointer(ws
->r
);
11237 DNPRINTF(SWM_D_EVENT
, "unmapnotify: done.\n");
11242 get_source_type_label(uint32_t type
)
11247 case EWMH_SOURCE_TYPE_NONE
:
11250 case EWMH_SOURCE_TYPE_NORMAL
:
11253 case EWMH_SOURCE_TYPE_OTHER
:
11265 clientmessage(xcb_client_message_event_t
*e
)
11267 struct ws_win
*win
;
11268 struct swm_region
*r
= NULL
;
11271 int num_screens
, i
;
11272 xcb_map_request_event_t mre
;
11276 name
= get_atom_name(e
->type
);
11277 DNPRINTF(SWM_D_EVENT
, "clientmessage: win %#x, atom: %s(%u)\n",
11278 e
->window
, name
, e
->type
);
11282 if (e
->type
== ewmh
[_NET_CURRENT_DESKTOP
].atom
) {
11283 num_screens
= get_screen_count();
11284 for (i
= 0; i
< num_screens
; i
++)
11285 if (screens
[i
].root
== e
->window
) {
11286 r
= screens
[i
].r_focus
;
11290 if (r
&& e
->data
.data32
[0] < (uint32_t)workspace_limit
) {
11291 a
.id
= e
->data
.data32
[0];
11292 switchws(NULL
, r
, &a
);
11297 } else if (e
->type
== ewmh
[_NET_REQUEST_FRAME_EXTENTS
].atom
) {
11298 DNPRINTF(SWM_D_EVENT
,
11299 "clientmessage: set _NET_FRAME_EXTENTS on window.\n");
11300 vals
[0] = vals
[1] = vals
[2] = vals
[3] = border_width
;
11301 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, e
->window
,
11302 a_net_frame_extents
, XCB_ATOM_CARDINAL
, 32, 4, vals
);
11307 win
= find_window(e
->window
);
11309 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11310 /* Manage the window with maprequest. */
11311 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
11312 "unmanaged window.\n");
11313 mre
.window
= e
->window
;
11319 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11320 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW, "
11321 "source_type: %s(%d)\n",
11322 get_source_type_label(e
->data
.data32
[0]),
11323 e
->data
.data32
[0]);
11326 * Allow focus changes that are a result of direct user
11327 * action and from applications that use the old EWMH spec.
11329 if (e
->data
.data32
[0] != EWMH_SOURCE_TYPE_NORMAL
||
11330 win
->quirks
& SWM_Q_OBEYAPPFOCUSREQ
) {
11331 if (WS_FOCUSED(win
->ws
))
11334 win
->ws
->focus_pending
= win
;
11336 } else if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
11337 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
11338 if (win
->can_delete
)
11339 client_msg(win
, a_delete
, 0);
11341 xcb_kill_client(conn
, win
->id
);
11342 } else if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
11343 DNPRINTF(SWM_D_EVENT
,
11344 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
11346 if (e
->data
.data32
[0] & (1<<8)) /* x */
11347 X(win
) = e
->data
.data32
[1];
11348 if (e
->data
.data32
[0] & (1<<9)) /* y */
11349 Y(win
) = e
->data
.data32
[2];
11350 if (e
->data
.data32
[0] & (1<<10)) /* width */
11351 WIDTH(win
) = e
->data
.data32
[3];
11352 if (e
->data
.data32
[0] & (1<<11)) /* height */
11353 HEIGHT(win
) = e
->data
.data32
[4];
11355 update_window(win
);
11357 /* Notify no change was made. */
11358 config_win(win
, NULL
);
11359 /* TODO: Change stack sizes */
11361 } else if (e
->type
== ewmh
[_NET_RESTACK_WINDOW
].atom
) {
11362 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_RESTACK_WINDOW\n");
11363 vals
[0] = e
->data
.data32
[1]; /* Sibling window. */
11364 vals
[1] = e
->data
.data32
[2]; /* Stack mode detail. */
11366 if (win
->frame
!= XCB_WINDOW_NONE
)
11367 xcb_configure_window(conn
, win
->frame
,
11368 XCB_CONFIG_WINDOW_SIBLING
|
11369 XCB_CONFIG_WINDOW_STACK_MODE
, vals
);
11370 } else if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
11371 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
11372 ewmh_change_wm_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
11373 if (e
->data
.data32
[2])
11374 ewmh_change_wm_state(win
, e
->data
.data32
[2],
11375 e
->data
.data32
[0]);
11377 ewmh_update_wm_state(win
);
11379 } else if (e
->type
== ewmh
[_NET_WM_DESKTOP
].atom
) {
11380 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_DESKTOP\n");
11383 win_to_ws(win
, e
->data
.data32
[0], true);
11385 /* Stack source and destination ws, if mapped. */
11386 if (r
!= win
->ws
->r
) {
11392 load_float_geom(win
);
11405 int errcode
= xcb_connection_has_error(conn
);
11406 #ifdef XCB_CONN_ERROR
11409 case XCB_CONN_ERROR
:
11410 s
= "Socket error, pipe error or other stream error.";
11412 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
11413 s
= "Extension not supported.";
11415 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
11416 s
= "Insufficient memory.";
11418 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
11419 s
= "Request length was exceeded.";
11421 case XCB_CONN_CLOSED_PARSE_ERR
:
11422 s
= "Error parsing display string.";
11425 s
= "Unknown error.";
11428 errx(errcode
, "X CONNECTION ERROR: %s", s
);
11431 errx(errcode
, "X CONNECTION ERROR");
11438 int num_screens
, i
;
11439 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
11440 XCB_EVENT_MASK_ENTER_WINDOW
|
11441 XCB_EVENT_MASK_OWNER_GRAB_BUTTON
|
11442 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
11443 XCB_EVENT_MASK_ENTER_WINDOW
|
11444 XCB_EVENT_MASK_LEAVE_WINDOW
|
11445 XCB_EVENT_MASK_BUTTON_PRESS
|
11446 XCB_EVENT_MASK_BUTTON_RELEASE
|
11447 XCB_EVENT_MASK_KEY_PRESS
|
11448 XCB_EVENT_MASK_KEY_RELEASE
|
11449 XCB_EVENT_MASK_PROPERTY_CHANGE
;
11451 xcb_void_cookie_t wac
;
11452 xcb_generic_error_t
*error
;
11454 /* this causes an error if some other window manager is running */
11455 num_screens
= get_screen_count();
11456 for (i
= 0; i
< num_screens
; i
++) {
11457 if ((sc
= get_screen(i
)) == NULL
)
11458 errx(1, "ERROR: can't get screen %d.", i
);
11459 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: %#x\n",
11461 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
11462 XCB_CW_EVENT_MASK
, &val
);
11463 if ((error
= xcb_request_check(conn
, wac
))) {
11464 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
11465 error
->error_code
);
11475 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
11477 struct swm_region
*r
= NULL
, *n
;
11478 struct workspace
*ws
= NULL
;
11482 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
11483 s
->idx
, w
, h
, x
, y
);
11485 /* remove any conflicting regions */
11486 n
= TAILQ_FIRST(&s
->rl
);
11489 n
= TAILQ_NEXT(r
, entry
);
11490 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
11491 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
11492 if (r
->ws
->r
!= NULL
)
11493 r
->ws
->old_r
= r
->ws
->r
;
11496 xcb_destroy_window(conn
, r
->id
);
11497 r
->id
= XCB_WINDOW_NONE
;
11498 TAILQ_REMOVE(&s
->rl
, r
, entry
);
11499 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
11503 /* search old regions for one to reuse */
11505 /* size + location match */
11506 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11507 if (X(r
) == x
&& Y(r
) == y
&&
11508 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11512 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11513 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11517 TAILQ_REMOVE(&s
->orl
, r
, entry
);
11518 /* try to use old region's workspace */
11519 if (r
->ws
->r
== NULL
)
11522 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
11523 err(1, "new_region: calloc: failed to allocate memory "
11526 /* if we don't have a workspace already, find one */
11528 for (i
= 0; i
< workspace_limit
; i
++)
11529 if (s
->ws
[i
].r
== NULL
) {
11536 errx(1, "new_region: no free workspaces");
11538 if (ws
->state
== SWM_WS_STATE_HIDDEN
)
11539 ws
->state
= SWM_WS_STATE_MAPPING
;
11548 r
->ws_prior
= NULL
;
11551 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
11553 /* Invisible region window to detect pointer events on empty regions. */
11554 r
->id
= xcb_generate_id(conn
);
11555 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
11556 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
11558 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
11559 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
11560 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
11562 /* Make sure region input is at the bottom. */
11563 wa
[0] = XCB_STACK_MODE_BELOW
;
11564 xcb_configure_window(conn
, r
->id
, XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
11566 xcb_map_window(conn
, r
->id
);
11570 scan_randr(int idx
)
11572 #ifdef SWM_XRR_HAS_CRTC
11575 #endif /* SWM_XRR_HAS_CRTC */
11576 struct swm_region
*r
;
11578 xcb_randr_get_screen_resources_current_cookie_t src
;
11579 xcb_randr_get_screen_resources_current_reply_t
*srr
;
11580 xcb_randr_get_crtc_info_cookie_t cic
;
11581 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
11582 xcb_randr_crtc_t
*crtc
;
11583 xcb_screen_t
*screen
;
11585 DNPRINTF(SWM_D_MISC
, "scan_randr: screen: %d\n", idx
);
11587 if ((screen
= get_screen(idx
)) == NULL
)
11588 errx(1, "ERROR: can't get screen %d.", idx
);
11590 num_screens
= get_screen_count();
11591 if (idx
>= num_screens
)
11592 errx(1, "scan_randr: invalid screen");
11594 /* remove any old regions */
11595 while ((r
= TAILQ_FIRST(&screens
[idx
].rl
)) != NULL
) {
11596 r
->ws
->old_r
= r
->ws
->r
= NULL
;
11598 xcb_destroy_window(conn
, r
->id
);
11599 r
->id
= XCB_WINDOW_NONE
;
11600 TAILQ_REMOVE(&screens
[idx
].rl
, r
, entry
);
11601 TAILQ_INSERT_TAIL(&screens
[idx
].orl
, r
, entry
);
11605 /* map virtual screens onto physical screens */
11606 #ifdef SWM_XRR_HAS_CRTC
11607 if (randr_support
) {
11608 src
= xcb_randr_get_screen_resources_current(conn
,
11609 screens
[idx
].root
);
11610 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
11613 new_region(&screens
[idx
], 0, 0,
11614 screen
->width_in_pixels
,
11615 screen
->height_in_pixels
);
11618 ncrtc
= srr
->num_crtcs
;
11620 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
11621 for (c
= 0; c
< ncrtc
; c
++) {
11622 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
11624 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
11627 if (cir
->num_outputs
== 0) {
11632 if (cir
->mode
== 0)
11633 new_region(&screens
[idx
], 0, 0,
11634 screen
->width_in_pixels
,
11635 screen
->height_in_pixels
);
11637 new_region(&screens
[idx
],
11638 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
11643 #endif /* SWM_XRR_HAS_CRTC */
11645 /* If detection failed, create a single region that spans the screen. */
11646 if (TAILQ_EMPTY(&screens
[idx
].rl
))
11647 new_region(&screens
[idx
], 0, 0, screen
->width_in_pixels
,
11648 screen
->height_in_pixels
);
11651 /* The screen shouldn't focus on unused regions. */
11652 TAILQ_FOREACH(r
, &screens
[idx
].orl
, entry
) {
11653 if (screens
[idx
].r_focus
== r
)
11654 screens
[idx
].r_focus
= NULL
;
11657 DNPRINTF(SWM_D_MISC
, "scan_randr: done.\n");
11661 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
11663 struct swm_region
*r
;
11664 struct workspace
*ws
;
11665 struct ws_win
*win
;
11666 int i
, j
, num_screens
;
11668 DNPRINTF(SWM_D_EVENT
, "screenchange: root: %#x\n", e
->root
);
11670 num_screens
= get_screen_count();
11671 /* silly event doesn't include the screen index */
11672 for (i
= 0; i
< num_screens
; i
++)
11673 if (screens
[i
].root
== e
->root
)
11675 if (i
>= num_screens
)
11676 errx(1, "screenchange: screen not found");
11678 /* brute force for now, just re-enumerate the regions */
11682 print_win_geom(e
->root
);
11684 /* add bars to all regions */
11685 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11688 /* Stack all regions. */
11689 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11692 /* Make sure a region has focus. */
11693 if (screens
[i
].r_focus
== NULL
) {
11694 r
= TAILQ_FIRST(&screens
[i
].rl
);
11699 /* Cleanup unused previously visible workspaces. */
11700 for (j
= 0; j
< workspace_limit
; j
++) {
11701 ws
= &screens
[i
].ws
[j
];
11702 if (ws
->r
== NULL
&& ws
->state
!= SWM_WS_STATE_HIDDEN
) {
11703 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
11705 ws
->state
= SWM_WS_STATE_HIDDEN
;
11711 /* Update workspace state and bar on all regions. */
11712 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11713 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
11721 struct swm_region
*r
= NULL
;
11722 xcb_query_tree_cookie_t qtc
;
11723 xcb_query_tree_reply_t
*qtr
;
11724 xcb_get_property_cookie_t pc
;
11725 xcb_get_property_reply_t
*pr
;
11726 xcb_window_t
*wins
= NULL
, trans
, *cwins
= NULL
;
11727 int i
, j
, k
, n
, no
, num_screens
;
11729 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
11730 num_screens
= get_screen_count();
11731 for (i
= 0; i
< num_screens
; i
++) {
11732 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
11733 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
11736 wins
= xcb_query_tree_children(qtr
);
11737 no
= xcb_query_tree_children_length(qtr
);
11739 /* Try to sort windows according to _NET_CLIENT_LIST. */
11740 pr
= xcb_get_property_reply(conn
, xcb_get_property(conn
, 0,
11741 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
11742 XCB_ATOM_WINDOW
, 0, UINT32_MAX
), NULL
);
11744 cwins
= xcb_get_property_value(pr
);
11745 n
= xcb_get_property_value_length(pr
) /
11746 sizeof(xcb_atom_t
);
11748 for (j
= 0; j
< n
; ++j
) {
11749 for (k
= j
; k
< no
; ++k
) {
11750 if (wins
[k
] == cwins
[j
]) {
11751 /* Swap wins j and k. */
11753 wins
[j
] = cwins
[j
];
11761 /* Manage top-level windows first, then transients. */
11762 /* TODO: allow transients to be managed before leader. */
11763 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top-level windows.\n");
11764 for (j
= 0; j
< no
; j
++) {
11765 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11766 if (r
->id
== wins
[j
]) {
11767 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11768 "skip %#x; region input window.\n",
11771 } else if (r
->bar
->id
== wins
[j
]) {
11772 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11773 "skip %#x; region bar.\n",
11782 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11783 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11785 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
11786 "is transient for %#x.\n", wins
[j
], trans
);
11790 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11793 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
11794 for (j
= 0; j
< no
; j
++) {
11795 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11796 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11798 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11802 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
11806 setup_screens(void)
11808 int i
, j
, k
, num_screens
;
11809 struct workspace
*ws
;
11810 uint32_t gcv
[1], wa
[1];
11811 const xcb_query_extension_reply_t
*qep
;
11812 xcb_screen_t
*screen
;
11813 xcb_randr_query_version_cookie_t c
;
11814 xcb_randr_query_version_reply_t
*r
;
11816 num_screens
= get_screen_count();
11817 if ((screens
= calloc(num_screens
, sizeof(struct swm_screen
))) == NULL
)
11818 err(1, "setup_screens: calloc: failed to allocate memory for "
11821 /* Initial RandR setup. */
11822 randr_support
= false;
11823 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
11824 if (qep
->present
) {
11825 c
= xcb_randr_query_version(conn
, 1, 1);
11826 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
11828 if (r
->major_version
>= 1) {
11829 randr_support
= true;
11830 randr_eventbase
= qep
->first_event
;
11836 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
11838 /* map physical screens */
11839 for (i
= 0; i
< num_screens
; i
++) {
11840 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
11841 screens
[i
].idx
= i
;
11842 screens
[i
].r_focus
= NULL
;
11844 TAILQ_INIT(&screens
[i
].rl
);
11845 TAILQ_INIT(&screens
[i
].orl
);
11846 if ((screen
= get_screen(i
)) == NULL
)
11847 errx(1, "ERROR: can't get screen %d.", i
);
11848 screens
[i
].root
= screen
->root
;
11850 /* set default colors */
11851 setscreencolor("red", i
, SWM_S_COLOR_FOCUS
);
11852 setscreencolor("rgb:88/88/88", i
, SWM_S_COLOR_UNFOCUS
);
11853 setscreencolor("rgb:00/80/80", i
, SWM_S_COLOR_BAR_BORDER
);
11854 setscreencolor("rgb:00/40/40", i
,
11855 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
11856 setscreencolor("black", i
, SWM_S_COLOR_BAR
);
11857 setscreencolor("rgb:a0/a0/a0", i
, SWM_S_COLOR_BAR_FONT
);
11858 setscreencolor("red", i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
11859 setscreencolor("rgb:88/88/88", i
,
11860 SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
11862 /* create graphics context on screen */
11863 screens
[i
].bar_gc
= xcb_generate_id(conn
);
11865 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
11866 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
11868 /* set default cursor */
11869 xcb_change_window_attributes(conn
, screens
[i
].root
,
11870 XCB_CW_CURSOR
, wa
);
11872 /* init all workspaces */
11873 /* XXX these should be dynamically allocated too */
11874 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
11875 ws
= &screens
[i
].ws
[j
];
11878 ws
->bar_enabled
= true;
11880 ws
->focus_prev
= NULL
;
11881 ws
->focus_pending
= NULL
;
11882 ws
->focus_raise
= NULL
;
11885 ws
->state
= SWM_WS_STATE_HIDDEN
;
11886 TAILQ_INIT(&ws
->stack
);
11887 TAILQ_INIT(&ws
->winlist
);
11888 TAILQ_INIT(&ws
->unmanagedlist
);
11890 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
11891 if (layouts
[k
].l_config
!= NULL
)
11892 layouts
[k
].l_config(ws
,
11893 SWM_ARG_ID_STACKINIT
);
11894 ws
->cur_layout
= &layouts
[0];
11895 ws
->cur_layout
->l_string(ws
);
11901 xcb_randr_select_input(conn
, screens
[i
].root
,
11902 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
11907 setup_globals(void)
11909 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
11910 err(1, "setup_globals: strdup: failed to allocate memory.");
11912 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
11913 err(1, "setup_globals: strdup: failed to allocate memory.");
11915 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
11916 errx(1, "unable to allocate key symbols");
11918 a_state
= get_atom_from_string("WM_STATE");
11919 a_prot
= get_atom_from_string("WM_PROTOCOLS");
11920 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
11921 a_net_frame_extents
= get_atom_from_string("_NET_FRAME_EXTENTS");
11922 a_net_supported
= get_atom_from_string("_NET_SUPPORTED");
11923 a_net_wm_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
11924 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
11925 a_utf8_string
= get_atom_from_string("UTF8_STRING");
11926 a_swm_ws
= get_atom_from_string("_SWM_WS");
11930 shutdown_cleanup(void)
11932 struct swm_region
*r
;
11934 struct workspace
*ws
;
11935 int i
, num_screens
;
11937 /* disable alarm because the following code may not be interrupted */
11939 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
11940 err(1, "can't disable alarm");
11953 num_screens
= get_screen_count();
11954 for (i
= 0; i
< num_screens
; ++i
) {
11957 DNPRINTF(SWM_D_FOCUS
, "shutdown_cleanup: set_input_focus: "
11958 "%#x, revert-to: root, time: 0\n", screens
[i
].root
);
11959 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11960 screens
[i
].root
, XCB_CURRENT_TIME
);
11962 if (screens
[i
].bar_gc
!= XCB_NONE
)
11963 xcb_free_gc(conn
, screens
[i
].bar_gc
);
11964 if (!bar_font_legacy
) {
11965 XftColorFree(display
, DefaultVisual(display
, i
),
11966 DefaultColormap(display
, i
), &bar_font_color
);
11967 XftColorFree(display
, DefaultVisual(display
, i
),
11968 DefaultColormap(display
, i
), &search_font_color
);
11971 for (j
= 0; j
< SWM_S_COLOR_MAX
; ++j
) {
11972 free(screens
[i
].c
[j
].name
);
11975 /* Free window memory. */
11976 for (j
= 0; j
< SWM_WS_MAX
; ++j
) {
11977 ws
= &screens
[i
].ws
[j
];
11980 while ((w
= TAILQ_FIRST(&ws
->winlist
)) != NULL
) {
11981 TAILQ_REMOVE(&ws
->winlist
, w
, entry
);
11985 while ((w
= TAILQ_FIRST(&ws
->unmanagedlist
)) != NULL
) {
11986 TAILQ_REMOVE(&ws
->unmanagedlist
, w
, entry
);
11991 /* Free region memory. */
11992 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
11993 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
11998 while ((r
= TAILQ_FIRST(&screens
[i
].orl
)) != NULL
) {
11999 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
12008 free(clock_format
);
12009 free(startup_exception
);
12012 XFreeFontSet(display
, bar_fs
);
12014 XftFontClose(display
, bar_font
);
12016 xcb_key_symbols_free(syms
);
12018 xcb_aux_sync(conn
);
12019 xcb_disconnect(conn
);
12023 event_error(xcb_generic_error_t
*e
)
12027 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
12028 "resource_id: %u, minor_code: %u\n",
12029 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
12030 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
12031 e
->sequence
, e
->resource_id
, e
->minor_code
);
12035 event_handle(xcb_generic_event_t
*evt
)
12037 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
12039 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
12040 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
12041 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
12044 #define EVENT(type, callback) case type: callback((void *)evt); return
12045 EVENT(0, event_error
);
12046 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
12047 EVENT(XCB_BUTTON_RELEASE
, buttonrelease
);
12048 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
12049 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
12050 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
12051 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
12052 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
12053 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
12054 /*EVENT(XCB_CREATE_NOTIFY, );*/
12055 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
12056 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
12057 EVENT(XCB_EXPOSE
, expose
);
12058 EVENT(XCB_FOCUS_IN
, focusin
);
12060 EVENT(XCB_FOCUS_OUT
, focusout
);
12062 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
12063 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
12064 EVENT(XCB_KEY_PRESS
, keypress
);
12065 EVENT(XCB_KEY_RELEASE
, keyrelease
);
12066 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
12068 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
12070 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
12071 EVENT(XCB_MAP_REQUEST
, maprequest
);
12072 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
12073 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
12074 /*EVENT(XCB_NO_EXPOSURE, );*/
12075 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
12076 EVENT(XCB_REPARENT_NOTIFY
, reparentnotify
);
12077 /*EVENT(XCB_RESIZE_REQUEST, );*/
12078 /*EVENT(XCB_SELECTION_CLEAR, );*/
12079 /*EVENT(XCB_SELECTION_NOTIFY, );*/
12080 /*EVENT(XCB_SELECTION_REQUEST, );*/
12081 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
12082 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
12085 if (type
- randr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
12086 screenchange((void *)evt
);
12090 main(int argc
, char *argv
[])
12092 struct pollfd pfd
[2];
12093 struct sigaction sact
;
12095 struct passwd
*pwd
;
12096 struct swm_region
*r
;
12097 xcb_generic_event_t
*evt
;
12098 int xfd
, i
, num_screens
, num_readable
;
12099 char conf
[PATH_MAX
], *cfile
= NULL
;
12100 bool stdin_ready
= false, startup
= true;
12102 /* suppress unused warning since var is needed */
12106 time_started
= time(NULL
);
12110 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
12111 if (setlocale(LC_CTYPE
, "") == NULL
|| setlocale(LC_TIME
, "") == NULL
)
12112 warnx("no locale support");
12114 /* handle some signals */
12115 bzero(&sact
, sizeof(sact
));
12116 sigemptyset(&sact
.sa_mask
);
12118 sact
.sa_handler
= sighdlr
;
12119 sigaction(SIGINT
, &sact
, NULL
);
12120 sigaction(SIGQUIT
, &sact
, NULL
);
12121 sigaction(SIGTERM
, &sact
, NULL
);
12122 sigaction(SIGHUP
, &sact
, NULL
);
12124 sact
.sa_handler
= sighdlr
;
12125 sact
.sa_flags
= SA_NOCLDSTOP
;
12126 sigaction(SIGCHLD
, &sact
, NULL
);
12128 if ((display
= XOpenDisplay(0)) == NULL
)
12129 errx(1, "can not open display");
12131 conn
= XGetXCBConnection(display
);
12132 if (xcb_connection_has_error(conn
))
12133 errx(1, "can not get XCB connection");
12135 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
12137 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
12138 xfd
= xcb_get_file_descriptor(conn
);
12140 /* look for local and global conf file */
12141 pwd
= getpwuid(getuid());
12143 errx(1, "invalid user: %d", getuid());
12145 xcb_grab_server(conn
);
12146 xcb_aux_sync(conn
);
12148 /* flush all events */
12149 while ((evt
= get_next_event(false))) {
12150 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
12156 errx(1, "another window manager is currently running");
12158 /* Load Xcursors and/or cursorfont glyph cursors. */
12161 xcb_aux_sync(conn
);
12166 setup_keybindings();
12167 setup_btnbindings();
12172 for (i
= 0; ; i
++) {
12177 snprintf(conf
, sizeof conf
, "%s/.%s",
12178 pwd
->pw_dir
, SWM_CONF_FILE
);
12182 snprintf(conf
, sizeof conf
, "/etc/%s",
12187 snprintf(conf
, sizeof conf
, "%s/.%s",
12188 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
12191 /* global compat */
12192 snprintf(conf
, sizeof conf
, "/etc/%s",
12193 SWM_CONF_FILE_OLD
);
12199 if (strlen(conf
) && stat(conf
, &sb
) != -1)
12200 if (S_ISREG(sb
.st_mode
)) {
12207 /* load conf (if any) */
12209 conf_load(cfile
, SWM_CONF_DEFAULT
);
12213 if (getenv("SWM_STARTED") == NULL
)
12214 setenv("SWM_STARTED", "YES", 1);
12216 /* setup all bars */
12217 num_screens
= get_screen_count();
12218 for (i
= 0; i
< num_screens
; i
++)
12219 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12222 /* Manage existing windows. */
12228 /* Stack all regions to trigger mapping. */
12229 for (i
= 0; i
< num_screens
; i
++)
12230 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12233 xcb_ungrab_server(conn
);
12236 /* Update state and bar of each newly mapped workspace. */
12237 for (i
= 0; i
< num_screens
; i
++)
12238 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
12239 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
12243 memset(&pfd
, 0, sizeof(pfd
));
12245 pfd
[0].events
= POLLIN
;
12246 pfd
[1].fd
= STDIN_FILENO
;
12247 pfd
[1].events
= POLLIN
;
12250 while ((evt
= get_next_event(false))) {
12257 /* If just (re)started, set default focus if needed. */
12261 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
12262 r
= TAILQ_FIRST(&screens
[0].rl
);
12274 num_readable
= poll(pfd
, bar_extra
? 2 : 1, 1000);
12275 if (num_readable
== -1) {
12276 DNPRINTF(SWM_D_MISC
, "poll failed: %s",
12278 } else if (num_readable
> 0 && bar_extra
&&
12279 pfd
[1].revents
& POLLIN
) {
12280 stdin_ready
= true;
12284 restart(NULL
, NULL
, NULL
);
12290 stdin_ready
= false;
12291 bar_extra_update();
12294 /* Need to ensure the bar(s) are always updated. */
12295 for (i
= 0; i
< num_screens
; i
++)
12296 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12302 shutdown_cleanup();