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;
399 bool track_pid_ws
= true;
405 XFontSet bar_fs
= NULL
;
406 XFontSetExtents
*bar_fs_extents
;
407 XftFont
*bar_font
= NULL
;
408 bool bar_font_legacy
= true;
409 char *bar_fonts
= NULL
;
410 XftColor bar_font_color
;
411 XftColor search_font_color
;
412 char *startup_exception
= NULL
;
413 unsigned int nr_exceptions
= 0;
415 /* layout manager data */
416 struct swm_geometry
{
429 struct swm_geometry g
;
430 struct swm_region
*r
; /* Associated region. */
433 /* virtual "screens" */
435 TAILQ_ENTRY(swm_region
) entry
;
437 struct swm_geometry g
;
438 struct workspace
*ws
; /* current workspace on this region */
439 struct workspace
*ws_prior
; /* prior workspace on this region */
440 struct swm_screen
*s
; /* screen idx */
443 TAILQ_HEAD(swm_region_list
, swm_region
);
446 SWM_WIN_STATE_REPARENTING
,
447 SWM_WIN_STATE_REPARENTED
,
448 SWM_WIN_STATE_UNPARENTING
,
449 SWM_WIN_STATE_UNPARENTED
,
453 TAILQ_ENTRY(ws_win
) entry
;
454 TAILQ_ENTRY(ws_win
) stack_entry
;
457 xcb_window_t transient
;
458 struct ws_win
*focus_child
; /* focus on child transient */
459 struct swm_geometry g
; /* current geometry */
460 struct swm_geometry g_prev
; /* prev configured geometry */
461 struct swm_geometry g_float
; /* region coordinates */
462 bool g_floatvalid
; /* g_float geometry validity */
467 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
474 struct workspace
*ws
; /* always valid */
475 struct swm_screen
*s
; /* always valid, never changes */
477 xcb_icccm_get_wm_class_reply_t ch
;
478 xcb_icccm_wm_hints_t hints
;
483 TAILQ_HEAD(ws_win_list
, ws_win
);
484 TAILQ_HEAD(ws_win_stack
, ws_win
);
488 TAILQ_ENTRY(pid_e
) entry
;
492 TAILQ_HEAD(pid_list
, pid_e
);
493 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
495 /* layout handlers */
496 void stack(struct swm_region
*);
497 void vertical_config(struct workspace
*, int);
498 void vertical_stack(struct workspace
*, struct swm_geometry
*);
499 void horizontal_config(struct workspace
*, int);
500 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
501 void max_stack(struct workspace
*, struct swm_geometry
*);
502 void max_config(struct workspace
*, int);
503 void plain_stacker(struct workspace
*);
504 void fancy_stacker(struct workspace
*);
507 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
508 void (*l_config
)(struct workspace
*, int);
510 #define SWM_L_FOCUSPREV (1<<0)
511 #define SWM_L_MAPONFOCUS (1<<1)
512 void (*l_string
)(struct workspace
*);
514 /* stack, configure */
515 { vertical_stack
, vertical_config
, 0, plain_stacker
},
516 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
517 { max_stack
, max_config
,
518 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
519 { NULL
, NULL
, 0, NULL
},
522 /* position of max_stack mode in the layouts array, index into layouts! */
523 #define SWM_V_STACK (0)
524 #define SWM_H_STACK (1)
525 #define SWM_MAX_STACK (2)
527 #define SWM_H_SLICE (32)
528 #define SWM_V_SLICE (32)
530 /* define work spaces */
532 int idx
; /* workspace index */
533 char *name
; /* workspace name */
534 bool always_raise
; /* raise windows on focus */
535 bool bar_enabled
; /* bar visibility */
536 struct layout
*cur_layout
; /* current layout handlers */
537 struct ws_win
*focus
; /* may be NULL */
538 struct ws_win
*focus_prev
; /* may be NULL */
539 struct ws_win
*focus_pending
; /* may be NULL */
540 struct ws_win
*focus_raise
; /* may be NULL */
541 struct swm_region
*r
; /* may be NULL */
542 struct swm_region
*old_r
; /* may be NULL */
543 struct ws_win_list winlist
; /* list of windows in ws */
544 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
545 struct ws_win_stack stack
; /* stacking order */
546 int state
; /* mapping state */
547 char stacker
[10]; /* display stacker and layout */
551 int horizontal_msize
;
553 int horizontal_stacks
;
554 bool horizontal_flip
;
564 SWM_WS_STATE_MAPPING
,
570 SWM_S_COLOR_BAR_BORDER
,
571 SWM_S_COLOR_BAR_BORDER_UNFOCUS
,
572 SWM_S_COLOR_BAR_FONT
,
574 SWM_S_COLOR_FOCUS_MAXIMIZED
,
576 SWM_S_COLOR_UNFOCUS_MAXIMIZED
,
580 /* physical screen mapping */
581 #define SWM_WS_MAX (22) /* hard limit */
582 int workspace_limit
= 10; /* soft limit */
585 int idx
; /* screen index */
586 struct swm_region_list rl
; /* list of regions on this screen */
587 struct swm_region_list orl
; /* list of old regions */
589 struct workspace ws
[SWM_WS_MAX
];
590 struct swm_region
*r_focus
;
597 } c
[SWM_S_COLOR_MAX
];
599 xcb_gcontext_t bar_gc
;
602 struct swm_screen
*screens
;
604 struct layout_config
{
612 } initial_layouts
[SWM_WS_MAX
];
614 /* args to functions */
617 #define SWM_ARG_ID_FOCUSNEXT (0)
618 #define SWM_ARG_ID_FOCUSPREV (1)
619 #define SWM_ARG_ID_FOCUSMAIN (2)
620 #define SWM_ARG_ID_FOCUSURGENT (3)
621 #define SWM_ARG_ID_SWAPNEXT (10)
622 #define SWM_ARG_ID_SWAPPREV (11)
623 #define SWM_ARG_ID_SWAPMAIN (12)
624 #define SWM_ARG_ID_MOVELAST (13)
625 #define SWM_ARG_ID_MASTERSHRINK (20)
626 #define SWM_ARG_ID_MASTERGROW (21)
627 #define SWM_ARG_ID_MASTERADD (22)
628 #define SWM_ARG_ID_MASTERDEL (23)
629 #define SWM_ARG_ID_FLIPLAYOUT (24)
630 #define SWM_ARG_ID_STACKINIT (31)
631 #define SWM_ARG_ID_STACKBALANCE (32)
632 #define SWM_ARG_ID_CYCLEWS_UP (40)
633 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
634 #define SWM_ARG_ID_CYCLERG_UP (42)
635 #define SWM_ARG_ID_CYCLERG_DOWN (43)
636 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
637 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
638 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
639 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
640 #define SWM_ARG_ID_STACKINC (50)
641 #define SWM_ARG_ID_STACKDEC (51)
642 #define SWM_ARG_ID_DONTCENTER (70)
643 #define SWM_ARG_ID_CENTER (71)
644 #define SWM_ARG_ID_KILLWINDOW (80)
645 #define SWM_ARG_ID_DELETEWINDOW (81)
646 #define SWM_ARG_ID_WIDTHGROW (90)
647 #define SWM_ARG_ID_WIDTHSHRINK (91)
648 #define SWM_ARG_ID_HEIGHTGROW (92)
649 #define SWM_ARG_ID_HEIGHTSHRINK (93)
650 #define SWM_ARG_ID_MOVEUP (100)
651 #define SWM_ARG_ID_MOVEDOWN (101)
652 #define SWM_ARG_ID_MOVELEFT (102)
653 #define SWM_ARG_ID_MOVERIGHT (103)
654 #define SWM_ARG_ID_BAR_TOGGLE (110)
655 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
656 #define SWM_ARG_ID_CYCLERG_MOVE_UP (112)
657 #define SWM_ARG_ID_CYCLERG_MOVE_DOWN (113)
663 TAILQ_ENTRY(quirk
) entry
;
664 char *class; /* WM_CLASS:class */
665 char *instance
; /* WM_CLASS:instance */
666 char *name
; /* WM_NAME */
668 regex_t regex_instance
;
671 int ws
; /* Initial workspace. */
672 #define SWM_Q_FLOAT (1<<0) /* Float this window. */
673 #define SWM_Q_TRANSSZ (1<<1) /* Transient window size too small. */
674 #define SWM_Q_ANYWHERE (1<<2) /* Don't position this window */
675 #define SWM_Q_XTERM_FONTADJ (1<<3) /* Adjust xterm fonts when resizing. */
676 #define SWM_Q_FULLSCREEN (1<<4) /* Remove border when fullscreen. */
677 #define SWM_Q_FOCUSPREV (1<<5) /* Focus on caller. */
678 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
679 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
680 #define SWM_Q_OBEYAPPFOCUSREQ (1<<8) /* Focus when applications ask. */
681 #define SWM_Q_IGNOREPID (1<<9) /* Ignore PID when determining ws. */
682 #define SWM_Q_IGNORESPAWNWS (1<<10) /* Ignore _SWM_WS when managing win. */
683 #define SWM_Q_NOFOCUSCYCLE (1<<11) /* Remove from normal focus cycle. */
684 #define SWM_Q_MINIMALBORDER (1<<12) /* No border when floating/unfocused. */
686 TAILQ_HEAD(quirk_list
, quirk
);
687 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
690 * Supported EWMH hints should be added to
691 * both the enum and the ewmh array
697 _NET_CURRENT_DESKTOP
,
698 _NET_DESKTOP_GEOMETRY
,
700 _NET_DESKTOP_VIEWPORT
,
701 _NET_MOVERESIZE_WINDOW
,
702 _NET_NUMBER_OF_DESKTOPS
,
703 _NET_REQUEST_FRAME_EXTENTS
,
705 _NET_WM_ACTION_ABOVE
,
706 _NET_WM_ACTION_CLOSE
,
707 _NET_WM_ACTION_FULLSCREEN
,
709 _NET_WM_ACTION_RESIZE
,
710 _NET_WM_ALLOWED_ACTIONS
,
712 _NET_WM_FULL_PLACEMENT
,
716 _NET_WM_STATE_FULLSCREEN
,
717 _NET_WM_STATE_HIDDEN
,
718 _NET_WM_STATE_MAXIMIZED_VERT
,
719 _NET_WM_STATE_MAXIMIZED_HORZ
,
720 _NET_WM_STATE_SKIP_PAGER
,
721 _NET_WM_STATE_SKIP_TASKBAR
,
723 _NET_WM_WINDOW_TYPE_DESKTOP
,
724 _NET_WM_WINDOW_TYPE_DIALOG
,
725 _NET_WM_WINDOW_TYPE_DOCK
,
726 _NET_WM_WINDOW_TYPE_NORMAL
,
727 _NET_WM_WINDOW_TYPE_SPLASH
,
728 _NET_WM_WINDOW_TYPE_TOOLBAR
,
729 _NET_WM_WINDOW_TYPE_UTILITY
,
730 _SWM_WM_STATE_MANUAL
,
737 } ewmh
[SWM_EWMH_HINT_MAX
] = {
738 /* must be in same order as in the enum */
739 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
740 {"_NET_CLIENT_LIST", XCB_ATOM_NONE
},
741 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
742 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE
},
743 {"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE
},
744 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE
},
745 {"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE
},
746 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
747 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE
},
748 {"_NET_REQUEST_FRAME_EXTENTS", XCB_ATOM_NONE
},
749 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE
},
750 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
751 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
752 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
753 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
754 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
755 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
756 {"_NET_WM_DESKTOP", XCB_ATOM_NONE
},
757 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE
},
758 {"_NET_WM_NAME", XCB_ATOM_NONE
},
759 {"_NET_WM_STATE", XCB_ATOM_NONE
},
760 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
761 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
762 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
763 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
764 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
765 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
766 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
767 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
768 {"_NET_WM_WINDOW_TYPE_DESKTOP", XCB_ATOM_NONE
},
769 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
770 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
771 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
772 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
773 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
774 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
775 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
778 /* EWMH source type */
780 EWMH_SOURCE_TYPE_NONE
= 0,
781 EWMH_SOURCE_TYPE_NORMAL
= 1,
782 EWMH_SOURCE_TYPE_OTHER
= 2,
789 XC_BOTTOM_LEFT_CORNER
,
790 XC_BOTTOM_RIGHT_CORNER
,
798 char *name
; /* Name used by Xcursor .*/
799 uint8_t cf_char
; /* cursorfont index. */
801 } cursors
[XC_MAX
] = {
802 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
803 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
804 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
805 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
806 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
807 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
808 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
811 #define SWM_SPAWN_OPTIONAL 0x1
815 TAILQ_ENTRY(spawn_prog
) entry
;
821 TAILQ_HEAD(spawn_list
, spawn_prog
);
822 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
828 /* User callable function IDs. */
841 FN_FULLSCREEN_TOGGLE
,
955 /* SWM_DEBUG actions MUST be here: */
968 BINDING_F_REPLAY
= 0x1,
972 RB_ENTRY(binding
) entry
;
973 uint16_t mod
; /* Modifier Mask. */
974 enum binding_type type
; /* Key or Button. */
975 uint32_t value
; /* KeySym or Button Index. */
976 enum actionid action
; /* Action Identifier. */
980 RB_HEAD(binding_tree
, binding
);
982 /* function prototypes */
983 void adjust_font(struct ws_win
*);
984 char *argsep(char **);
985 void bar_cleanup(struct swm_region
*);
986 void bar_extra_setup(void);
987 void bar_extra_stop(void);
988 int bar_extra_update(void);
989 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
990 void bar_fmt_expand(char *, size_t);
991 void bar_draw(struct swm_bar
*);
992 void bar_print(struct swm_region
*, const char *);
993 void bar_print_legacy(struct swm_region
*, const char *);
994 void bar_replace(char *, char *, struct swm_region
*, size_t);
995 void bar_replace_pad(char *, int *, size_t);
996 char *bar_replace_seq(char *, char *, struct swm_region
*, size_t *, size_t);
997 void bar_setup(struct swm_region
*);
998 void bar_toggle(struct binding
*, struct swm_region
*, union arg
*);
999 void bar_urgent(char *, size_t);
1000 void bar_window_class(char *, size_t, struct swm_region
*);
1001 void bar_window_class_instance(char *, size_t, struct swm_region
*);
1002 void bar_window_instance(char *, size_t, struct swm_region
*);
1003 void bar_window_name(char *, size_t, struct swm_region
*);
1004 void bar_window_state(char *, size_t, struct swm_region
*);
1005 void bar_workspace_name(char *, size_t, struct swm_region
*);
1006 void bar_workspace_state(char *, size_t, struct swm_region
*);
1007 int binding_cmp(struct binding
*, struct binding
*);
1008 void binding_insert(uint16_t, enum binding_type
, uint32_t, enum actionid
,
1009 uint32_t, const char *);
1010 struct binding
*binding_lookup(uint16_t, enum binding_type
, uint32_t);
1011 void binding_remove(struct binding
*);
1012 void buttonpress(xcb_button_press_event_t
*);
1013 void buttonrelease(xcb_button_release_event_t
*);
1014 void center_pointer(struct swm_region
*);
1015 void check_conn(void);
1016 void clear_bindings(void);
1017 void clear_keybindings(void);
1018 int clear_maximized(struct workspace
*);
1019 void clear_quirks(void);
1020 void clear_spawns(void);
1021 void clientmessage(xcb_client_message_event_t
*);
1022 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
1023 int conf_load(const char *, int);
1024 void configurenotify(xcb_configure_notify_event_t
*);
1025 void configurerequest(xcb_configure_request_event_t
*);
1026 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
1027 void constrain_window(struct ws_win
*, struct swm_geometry
*, int *);
1028 int count_win(struct workspace
*, bool);
1029 void cursors_cleanup(void);
1030 void cursors_load(void);
1031 void custom_region(const char *);
1032 void cycle_layout(struct binding
*, struct swm_region
*, union arg
*);
1033 void cyclerg(struct binding
*, struct swm_region
*, union arg
*);
1034 void cyclews(struct binding
*, struct swm_region
*, union arg
*);
1036 void debug_refresh(struct ws_win
*);
1038 void debug_toggle(struct binding
*, struct swm_region
*, union arg
*);
1039 void destroynotify(xcb_destroy_notify_event_t
*);
1040 void dumpwins(struct binding
*, struct swm_region
*, union arg
*);
1041 int enable_wm(void);
1042 void enternotify(xcb_enter_notify_event_t
*);
1043 void event_drain(uint8_t);
1044 void event_error(xcb_generic_error_t
*);
1045 void event_handle(xcb_generic_event_t
*);
1046 void ewmh_apply_flags(struct ws_win
*, uint32_t);
1047 void ewmh_autoquirk(struct ws_win
*);
1048 void ewmh_get_desktop_names(void);
1049 void ewmh_get_wm_state(struct ws_win
*);
1050 int ewmh_handle_special_types(xcb_window_t
, struct swm_region
*);
1051 void ewmh_update_actions(struct ws_win
*);
1052 void ewmh_update_client_list(void);
1053 void ewmh_update_current_desktop(void);
1054 void ewmh_update_desktop_names(void);
1055 void ewmh_update_desktops(void);
1056 void ewmh_change_wm_state(struct ws_win
*, xcb_atom_t
, long);
1057 void ewmh_update_wm_state(struct ws_win
*);
1058 char *expand_tilde(const char *);
1059 void expose(xcb_expose_event_t
*);
1060 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
1061 struct swm_bar
*find_bar(xcb_window_t
);
1062 struct ws_win
*find_frame_window(xcb_window_t
);
1063 struct pid_e
*find_pid(pid_t
);
1064 struct swm_region
*find_region(xcb_window_t
);
1065 struct ws_win
*find_unmanaged_window(xcb_window_t
);
1066 struct ws_win
*find_window(xcb_window_t
);
1067 void floating_toggle(struct binding
*, struct swm_region
*, union arg
*);
1068 void focus(struct binding
*, struct swm_region
*, union arg
*);
1069 void focus_flush(void);
1070 void focus_pointer(struct binding
*, struct swm_region
*, union arg
*);
1071 void focus_region(struct swm_region
*);
1072 void focus_win(struct ws_win
*);
1073 void focusin(xcb_focus_in_event_t
*);
1075 void focusout(xcb_focus_out_event_t
*);
1077 void focusrg(struct binding
*, struct swm_region
*, union arg
*);
1078 void fontset_init(void);
1079 void free_window(struct ws_win
*);
1080 void fullscreen_toggle(struct binding
*, struct swm_region
*, union arg
*);
1081 xcb_atom_t
get_atom_from_string(const char *);
1083 char *get_atom_name(xcb_atom_t
);
1085 struct ws_win
*get_focus_magic(struct ws_win
*);
1086 struct ws_win
*get_focus_prev(struct ws_win
*);
1087 xcb_generic_event_t
*get_next_event(bool);
1089 char *get_notify_detail_label(uint8_t);
1090 char *get_notify_mode_label(uint8_t);
1092 struct ws_win
*get_pointer_win(xcb_window_t
);
1093 struct ws_win
*get_region_focus(struct swm_region
*);
1094 int get_region_index(struct swm_region
*);
1095 xcb_screen_t
*get_screen(int);
1096 int get_screen_count(void);
1098 char *get_source_type_label(uint32_t);
1099 char *get_stack_mode_name(uint8_t);
1100 char *get_state_mask_label(uint16_t);
1102 int32_t get_swm_ws(xcb_window_t
);
1103 bool get_urgent(struct ws_win
*);
1105 char *get_win_input_model(struct ws_win
*);
1107 char *get_win_name(xcb_window_t
);
1108 uint8_t get_win_state(xcb_window_t
);
1109 void get_wm_protocols(struct ws_win
*);
1110 int get_ws_idx(struct ws_win
*);
1111 void grab_windows(void);
1112 void grabbuttons(void);
1113 void grabkeys(void);
1114 void iconify(struct binding
*, struct swm_region
*, union arg
*);
1115 void initlayout(struct workspace
*);
1116 bool isxlfd(char *);
1117 bool keybindreleased(struct binding
*, xcb_key_release_event_t
*);
1118 void keypress(xcb_key_press_event_t
*);
1119 void keyrelease(xcb_key_release_event_t
*);
1120 bool keyrepeating(xcb_key_release_event_t
*);
1121 void kill_bar_extra_atexit(void);
1122 void kill_refs(struct ws_win
*);
1124 void leavenotify(xcb_leave_notify_event_t
*);
1126 void load_float_geom(struct ws_win
*);
1127 void lower_window(struct ws_win
*);
1128 struct ws_win
*manage_window(xcb_window_t
, int, bool);
1129 void map_window(struct ws_win
*);
1130 void mapnotify(xcb_map_notify_event_t
*);
1131 void mappingnotify(xcb_mapping_notify_event_t
*);
1132 void maprequest(xcb_map_request_event_t
*);
1133 void maximize_toggle(struct binding
*, struct swm_region
*, union arg
*);
1134 void motionnotify(xcb_motion_notify_event_t
*);
1135 void move(struct binding
*, struct swm_region
*, union arg
*);
1136 void move_win(struct ws_win
*, struct binding
*, int);
1137 uint32_t name_to_pixel(int, const char *);
1138 void name_workspace(struct binding
*, struct swm_region
*, union arg
*);
1139 void new_region(struct swm_screen
*, int, int, int, int);
1140 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1141 int parsebinding(const char *, uint16_t *, enum binding_type
*, uint32_t *,
1143 int parsequirks(const char *, uint32_t *, int *);
1144 void pressbutton(struct binding
*, struct swm_region
*, union arg
*);
1145 void priorws(struct binding
*, struct swm_region
*, union arg
*);
1147 void print_win_geom(xcb_window_t
);
1149 void propertynotify(xcb_property_notify_event_t
*);
1150 void put_back_event(xcb_generic_event_t
*);
1151 void quirk_free(struct quirk
*);
1152 void quirk_insert(const char *, const char *, const char *, uint32_t, int);
1153 void quirk_remove(struct quirk
*);
1154 void quirk_replace(struct quirk
*, const char *, const char *, const char *,
1156 void quit(struct binding
*, struct swm_region
*, union arg
*);
1157 void raise_focus(struct binding
*, struct swm_region
*, union arg
*);
1158 void raise_toggle(struct binding
*, struct swm_region
*, union arg
*);
1159 void raise_window(struct ws_win
*);
1160 void region_containment(struct ws_win
*, struct swm_region
*, int);
1161 struct swm_region
*region_under(struct swm_screen
*, int, int);
1162 void regionize(struct ws_win
*, int, int);
1163 void reparent_window(struct ws_win
*);
1164 void reparentnotify(xcb_reparent_notify_event_t
*);
1165 void resize(struct binding
*, struct swm_region
*, union arg
*);
1166 void resize_win(struct ws_win
*, struct binding
*, int);
1167 void restart(struct binding
*, struct swm_region
*, union arg
*);
1168 struct swm_region
*root_to_region(xcb_window_t
, int);
1169 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1170 void scan_randr(int);
1171 void search_do_resp(void);
1172 void search_resp_name_workspace(const char *, size_t);
1173 void search_resp_search_window(const char *);
1174 void search_resp_search_workspace(const char *);
1175 void search_resp_uniconify(const char *, size_t);
1176 void search_win(struct binding
*, struct swm_region
*, union arg
*);
1177 void search_win_cleanup(void);
1178 void search_workspace(struct binding
*, struct swm_region
*, union arg
*);
1179 void send_to_rg(struct binding
*, struct swm_region
*, union arg
*);
1180 void send_to_rg_relative(struct binding
*, struct swm_region
*, union arg
*);
1181 void send_to_ws(struct binding
*, struct swm_region
*, union arg
*);
1182 void set_region(struct swm_region
*);
1183 int setautorun(const char *, const char *, int);
1184 void setbinding(uint16_t, enum binding_type
, uint32_t, enum actionid
,
1185 uint32_t, const char *);
1186 int setconfbinding(const char *, const char *, int);
1187 int setconfcolor(const char *, const char *, int);
1188 int setconfmodkey(const char *, const char *, int);
1189 int setconfquirk(const char *, const char *, int);
1190 int setconfregion(const char *, const char *, int);
1191 int setconfspawn(const char *, const char *, int);
1192 int setconfvalue(const char *, const char *, int);
1193 int setkeymapping(const char *, const char *, int);
1194 int setlayout(const char *, const char *, int);
1195 void setquirk(const char *, const char *, const char *, uint32_t, int);
1196 void setscreencolor(const char *, int, int);
1197 void setspawn(const char *, const char *, int);
1198 void setup_btnbindings(void);
1199 void setup_ewmh(void);
1200 void setup_globals(void);
1201 void setup_keybindings(void);
1202 void setup_quirks(void);
1203 void setup_screens(void);
1204 void setup_spawn(void);
1205 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1206 void set_win_state(struct ws_win
*, uint8_t);
1207 void shutdown_cleanup(void);
1209 void socket_setnonblock(int);
1210 void sort_windows(struct ws_win_list
*);
1211 void spawn(int, union arg
*, bool);
1212 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1213 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1214 void spawn_insert(const char *, const char *, int);
1215 struct spawn_prog
*spawn_find(const char *);
1216 void spawn_remove(struct spawn_prog
*);
1217 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1218 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1219 void stack_config(struct binding
*, struct swm_region
*, union arg
*);
1220 void stack_master(struct workspace
*, struct swm_geometry
*, int, bool);
1221 void store_float_geom(struct ws_win
*);
1222 char *strdupsafe(const char *);
1223 void swapwin(struct binding
*, struct swm_region
*, union arg
*);
1224 void switchws(struct binding
*, struct swm_region
*, union arg
*);
1225 void teardown_ewmh(void);
1226 void unescape_selector(char *);
1227 void unfocus_win(struct ws_win
*);
1228 void uniconify(struct binding
*, struct swm_region
*, union arg
*);
1229 void unmanage_window(struct ws_win
*);
1230 void unmap_all(void);
1231 void unmap_window(struct ws_win
*);
1232 void unmapnotify(xcb_unmap_notify_event_t
*);
1233 void unparent_window(struct ws_win
*);
1234 void update_floater(struct ws_win
*);
1235 void update_modkey(uint16_t);
1236 void update_win_stacking(struct ws_win
*);
1237 void update_window(struct ws_win
*);
1238 void draw_frame(struct ws_win
*);
1239 void update_wm_state(struct ws_win
*win
);
1240 void updatenumlockmask(void);
1241 void validate_spawns(void);
1242 int validate_win(struct ws_win
*);
1243 int validate_ws(struct workspace
*);
1244 void version(struct binding
*, struct swm_region
*, union arg
*);
1245 void win_to_ws(struct ws_win
*, int, bool);
1246 pid_t
window_get_pid(xcb_window_t
);
1247 void wkill(struct binding
*, struct swm_region
*, union arg
*);
1248 void update_ws_stack(struct workspace
*);
1249 void xft_init(struct swm_region
*);
1250 void _add_startup_exception(const char *, va_list);
1251 void add_startup_exception(const char *, ...);
1253 RB_PROTOTYPE(binding_tree
, binding
, entry
, binding_cmp
);
1254 RB_GENERATE(binding_tree
, binding
, entry
, binding_cmp
);
1255 struct binding_tree bindings
;
1260 xcb_font_t cf
= XCB_NONE
;
1263 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1264 /* try to load Xcursor first. */
1265 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1268 /* fallback to cursorfont. */
1269 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1270 if (cf
== XCB_NONE
) {
1271 cf
= xcb_generate_id(conn
);
1272 xcb_open_font(conn
, cf
, strlen("cursor"),
1276 cursors
[i
].cid
= xcb_generate_id(conn
);
1277 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1278 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1279 0xffff, 0xffff, 0xffff);
1285 xcb_close_font(conn
, cf
);
1289 cursors_cleanup(void)
1292 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1293 xcb_free_cursor(conn
, cursors
[i
].cid
);
1297 expand_tilde(const char *s
)
1299 struct passwd
*ppwd
;
1307 errx(1, "expand_tilde: NULL string.");
1310 result
= strdup(sc
);
1316 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1317 errx(1, "expand_tilde: sysconf");
1319 if ((user
= calloc(1, max
+ 1)) == NULL
)
1320 errx(1, "expand_tilde: calloc");
1322 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1327 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1331 result
= strdup(sc
);
1333 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1337 errx(1, "expand_tilde: failed to allocate memory.");
1343 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1345 unsigned int tmpr
, tmpg
, tmpb
;
1347 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1350 *rr
= RGB_8_TO_16(tmpr
);
1351 *gg
= RGB_8_TO_16(tmpg
);
1352 *bb
= RGB_8_TO_16(tmpb
);
1358 get_screen(int screen
)
1360 const xcb_setup_t
*r
;
1361 xcb_screen_iterator_t iter
;
1363 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1364 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1368 iter
= xcb_setup_roots_iterator(r
);
1369 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1377 get_screen_count(void)
1379 const xcb_setup_t
*r
;
1381 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1382 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1386 return xcb_setup_roots_length(r
);
1390 get_region_index(struct swm_region
*r
)
1392 struct swm_region
*rr
;
1398 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1413 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1414 event_drain(XCB_ENTER_NOTIFY
);
1420 get_atom_from_string(const char *str
)
1422 xcb_intern_atom_cookie_t c
;
1423 xcb_intern_atom_reply_t
*r
;
1426 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1427 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1435 return (XCB_ATOM_NONE
);
1439 get_wm_protocols(struct ws_win
*win
) {
1441 xcb_icccm_get_wm_protocols_reply_t wpr
;
1443 if (xcb_icccm_get_wm_protocols_reply(conn
,
1444 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
1446 for (i
= 0; i
< (int)wpr
.atoms_len
; i
++) {
1447 if (wpr
.atoms
[i
] == a_takefocus
)
1448 win
->take_focus
= true;
1449 if (wpr
.atoms
[i
] == a_delete
)
1450 win
->can_delete
= true;
1452 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
1459 xcb_window_t root
, win
;
1460 int i
, j
, num_screens
;
1462 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1463 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1465 num_screens
= get_screen_count();
1466 for (i
= 0; i
< num_screens
; i
++) {
1467 root
= screens
[i
].root
;
1469 /* Set up _NET_SUPPORTING_WM_CHECK. */
1470 win
= xcb_generate_id(conn
);
1471 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
1472 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1473 XCB_COPY_FROM_PARENT
, 0, NULL
);
1475 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
1476 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1477 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1478 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1480 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1481 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1482 8, strlen("spectrwm"), "spectrwm");
1484 /* Report supported atoms */
1485 xcb_delete_property(conn
, root
, a_net_supported
);
1486 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1487 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, root
,
1488 a_net_supported
, XCB_ATOM_ATOM
, 32, 1,
1493 ewmh_update_desktops();
1494 ewmh_get_desktop_names();
1502 xcb_get_property_cookie_t pc
;
1503 xcb_get_property_reply_t
*pr
;
1505 num_screens
= get_screen_count();
1507 for (i
= 0; i
< num_screens
; i
++) {
1508 /* Get the support check window and destroy it */
1509 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, a_net_wm_check
,
1510 XCB_ATOM_WINDOW
, 0, 1);
1511 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1514 if (pr
->format
== a_net_wm_check
) {
1515 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1517 xcb_destroy_window(conn
, id
);
1518 xcb_delete_property(conn
, screens
[i
].root
,
1520 xcb_delete_property(conn
, screens
[i
].root
,
1528 ewmh_handle_special_types(xcb_window_t id
, struct swm_region
*region
)
1530 xcb_get_property_reply_t
*r
;
1531 xcb_get_property_cookie_t c
;
1534 uint16_t configure_mask
= 0;
1537 c
= xcb_get_property(conn
, 0, id
,
1538 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1539 r
= xcb_get_property_reply(conn
, c
, NULL
);
1543 type
= xcb_get_property_value(r
);
1544 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1546 for (i
= 0; i
< n
; i
++) {
1547 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DESKTOP
].atom
) {
1548 configure_mask
= XCB_CONFIG_WINDOW_STACK_MODE
|
1549 XCB_CONFIG_WINDOW_SIBLING
;
1551 wa
[1] = XCB_STACK_MODE_ABOVE
;
1555 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
) {
1556 configure_mask
= XCB_CONFIG_WINDOW_STACK_MODE
;
1557 wa
[0] = XCB_STACK_MODE_ABOVE
;
1563 if (configure_mask
!= 0) {
1564 xcb_map_window(conn
, id
);
1565 xcb_configure_window (conn
, id
, configure_mask
, wa
);
1573 ewmh_autoquirk(struct ws_win
*win
)
1575 xcb_get_property_reply_t
*r
;
1576 xcb_get_property_cookie_t c
;
1580 c
= xcb_get_property(conn
, 0, win
->id
,
1581 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1582 r
= xcb_get_property_reply(conn
, c
, NULL
);
1586 type
= xcb_get_property_value(r
);
1587 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1589 for (i
= 0; i
< n
; i
++) {
1590 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1592 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1593 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1594 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1597 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1598 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1599 win
->quirks
= SWM_Q_FLOAT
;
1606 c
= xcb_get_property(conn
, 0, win
->id
,
1607 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1608 r
= xcb_get_property_reply(conn
, c
, NULL
);
1612 type
= xcb_get_property_value(r
);
1613 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1615 for (i
= 0; i
< n
; i
++) {
1616 if (type
[i
] == ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
||
1617 type
[i
] == ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
) {
1618 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1626 ewmh_update_actions(struct ws_win
*win
)
1628 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1634 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1637 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1638 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1639 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1642 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1643 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1646 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1647 #define _NET_WM_STATE_ADD 1 /* add/set property */
1648 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1651 ewmh_change_wm_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1658 name
= get_atom_name(state
);
1659 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: win %#x, state: %s, "
1660 "action: %ld\n", WINID(win
), name
, action
);
1666 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1667 flag
= EWMH_F_FULLSCREEN
;
1668 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1669 flag
= EWMH_F_ABOVE
;
1670 else if (state
== ewmh
[_NET_WM_STATE_HIDDEN
].atom
)
1671 flag
= EWMH_F_HIDDEN
;
1672 else if (state
== ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
||
1673 state
== ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
)
1674 flag
= EWMH_F_MAXIMIZED
;
1675 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1676 flag
= SWM_F_MANUAL
;
1677 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1678 flag
= EWMH_F_SKIP_PAGER
;
1679 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1680 flag
= EWMH_F_SKIP_TASKBAR
;
1682 /* Disallow unfloating transients. */
1683 if (TRANS(win
) && flag
== EWMH_F_ABOVE
)
1686 new_flags
= win
->ewmh_flags
;
1689 case _NET_WM_STATE_REMOVE
:
1692 case _NET_WM_STATE_ADD
:
1695 case _NET_WM_STATE_TOGGLE
:
1700 ewmh_apply_flags(win
, new_flags
);
1703 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: done.\n");
1707 ewmh_apply_flags(struct ws_win
*win
, uint32_t pending
)
1709 struct workspace
*ws
;
1712 changed
= win
->ewmh_flags
^ pending
;
1716 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: pending: %d\n", pending
);
1718 win
->ewmh_flags
= pending
;
1721 if (changed
& EWMH_F_HIDDEN
) {
1723 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1724 ws
->focus_pending
= get_focus_prev(win
);
1729 /* Reload floating geometry in case region changed. */
1731 load_float_geom(win
);
1733 /* The window is no longer iconic, prepare focus. */
1734 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1735 ws
->focus_pending
= get_focus_magic(win
);
1740 if (changed
& EWMH_F_ABOVE
) {
1741 if (ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1743 load_float_geom(win
);
1744 else if (!MAXIMIZED(win
))
1745 store_float_geom(win
);
1747 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1748 changed
&= ~EWMH_F_MAXIMIZED
;
1752 win
->ewmh_flags
^= EWMH_F_ABOVE
& pending
;
1756 if (changed
& EWMH_F_MAXIMIZED
) {
1757 /* VERT and/or HORZ changed. */
1759 if (!MAXIMIZED(win
))
1760 load_float_geom(win
);
1762 store_float_geom(win
);
1765 if (MAXIMIZED(win
)) {
1766 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
1767 ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1771 ws
->focus_pending
= win
;
1779 if (changed
& EWMH_F_FULLSCREEN
) {
1780 if (FULLSCREEN(win
)) {
1781 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
1785 ws
->focus_pending
= win
;
1788 load_float_geom(win
);
1791 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1795 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: done.\n");
1799 ewmh_update_wm_state(struct ws_win
*win
) {
1800 xcb_atom_t vals
[SWM_EWMH_ACTION_COUNT_MAX
];
1804 vals
[n
++] = ewmh
[_NET_WM_STATE_HIDDEN
].atom
;
1805 if (FULLSCREEN(win
))
1806 vals
[n
++] = ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
;
1807 if (MAXIMIZED_VERT(win
))
1808 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
;
1809 if (MAXIMIZED_HORZ(win
))
1810 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
;
1811 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1812 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
;
1813 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1814 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
;
1815 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1816 vals
[n
++] = ewmh
[_NET_WM_STATE_ABOVE
].atom
;
1817 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1818 vals
[n
++] = ewmh
[_SWM_WM_STATE_MANUAL
].atom
;
1821 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1822 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, n
, vals
);
1824 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1828 ewmh_get_wm_state(struct ws_win
*win
)
1831 xcb_get_property_cookie_t c
;
1832 xcb_get_property_reply_t
*r
;
1838 win
->ewmh_flags
= 0;
1840 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1841 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1842 r
= xcb_get_property_reply(conn
, c
, NULL
);
1846 states
= xcb_get_property_value(r
);
1847 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1849 for (i
= 0; i
< n
; i
++)
1850 ewmh_change_wm_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1858 dumpwins(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
1862 xcb_get_window_attributes_cookie_t c
;
1863 xcb_get_window_attributes_reply_t
*wa
;
1865 /* suppress unused warning since var is needed */
1869 if (r
->ws
== NULL
) {
1870 DPRINTF("dumpwins: invalid workspace\n");
1874 DPRINTF("=== managed window list ws %02d ===\n", r
->ws
->idx
);
1875 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
) {
1876 state
= get_win_state(w
->id
);
1877 c
= xcb_get_window_attributes(conn
, w
->id
);
1878 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1880 DPRINTF("win %#x (%#x), map_state: %d, state: %u, "
1881 "transient: %#x\n", w
->frame
, w
->id
, wa
->map_state
,
1882 state
, w
->transient
);
1885 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1889 DPRINTF("=== stacking order (top down) === \n");
1890 TAILQ_FOREACH(w
, &r
->ws
->stack
, stack_entry
) {
1891 DPRINTF("win %#x (%#x), fs: %s, maximized: %s, above: %s, "
1892 "iconic: %s\n", w
->frame
, w
->id
, YESNO(FULLSCREEN(w
)),
1893 YESNO(MAXIMIZED(w
)), YESNO(ABOVE(w
)), YESNO(ICONIC(w
)));
1896 DPRINTF("===== unmanaged window list =====\n");
1897 TAILQ_FOREACH(w
, &r
->ws
->unmanagedlist
, entry
) {
1898 state
= get_win_state(w
->id
);
1899 c
= xcb_get_window_attributes(conn
, w
->id
);
1900 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1902 DPRINTF("win %#x, map_state: %d, state: %u, "
1903 "transient: %#x\n", w
->id
, wa
->map_state
,
1904 state
, w
->transient
);
1907 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1911 DPRINTF("=================================\n");
1915 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
1918 int num_screens
, i
, j
;
1920 /* Suppress warnings. */
1925 DNPRINTF(SWM_D_MISC
, "debug_toggle\n");
1927 debug_enabled
= !debug_enabled
;
1929 num_screens
= get_screen_count();
1930 for (i
= 0; i
< num_screens
; i
++)
1931 for (j
= 0; j
< workspace_limit
; j
++)
1932 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1939 debug_refresh(struct ws_win
*win
)
1946 XRectangle l_ibox
, l_lbox
;
1947 xcb_rectangle_t rect
;
1949 uint32_t wc
[4], mask
, width
, height
, gcv
[1];
1952 xcb_screen_t
*screen
;
1954 if (debug_enabled
) {
1955 /* Create debug window if it doesn't exist. */
1956 if (win
->debug
== XCB_WINDOW_NONE
) {
1957 if ((screen
= get_screen(win
->s
->idx
)) == NULL
)
1958 errx(1, "ERROR: can't get screen %d.",
1961 win
->debug
= xcb_generate_id(conn
);
1962 wc
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1963 wc
[1] = win
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
1964 wc
[2] = screen
->default_colormap
;
1966 xcb_create_window(conn
, screen
->root_depth
, win
->debug
,
1967 win
->frame
, 0, 0, 10, 10, 1,
1968 XCB_WINDOW_CLASS_INPUT_OUTPUT
, screen
->root_visual
,
1969 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
|
1970 XCB_CW_COLORMAP
, wc
);
1972 xcb_map_window(conn
, win
->debug
);
1975 /* Determine workspace window list index. */
1977 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
1983 /* Determine stacking index (top down). */
1985 TAILQ_FOREACH(w
, &win
->ws
->stack
, stack_entry
) {
1991 if (asprintf(&s
, "%#x f:%#x wl:%d s:%d im:%s", win
->id
,
1992 win
->frame
, widx
, sidx
, get_win_input_model(win
)) == -1)
1997 /* Update window to an appropriate dimension. */
1998 if (bar_font_legacy
) {
1999 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
2000 width
= l_lbox
.width
+ 4;
2001 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
2003 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
2005 width
= info
.width
+ 4;
2006 height
= bar_font
->height
+ 4;
2009 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
2010 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
;
2012 wc
[0] = wc
[1] = border_width
;
2019 xcb_configure_window(conn
, win
->debug
, mask
, wc
);
2021 /* Draw a filled rectangle to 'clear' window. */
2025 rect
.height
= height
;
2027 gcv
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2028 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2029 xcb_poly_fill_rectangle(conn
, win
->debug
, win
->s
->bar_gc
, 1,
2033 if (bar_font_legacy
) {
2034 l_gcv
.graphics_exposures
= 0;
2035 l_draw
= XCreateGC(display
, win
->debug
, 0, &l_gcv
);
2037 XSetForeground(display
, l_draw
,
2038 win
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2040 DRAWSTRING(display
, win
->debug
, bar_fs
, l_draw
, 2,
2041 (bar_fs_extents
->max_logical_extent
.height
-
2042 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
2044 XFreeGC(display
, l_draw
);
2046 draw
= XftDrawCreate(display
, win
->debug
,
2047 DefaultVisual(display
, win
->s
->idx
),
2048 DefaultColormap(display
, win
->s
->idx
));
2050 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
2051 (bar_height
+ bar_font
->height
) / 2 -
2052 bar_font
->descent
, (FcChar8
*)s
, len
);
2054 XftDrawDestroy(draw
);
2058 } else if (win
->debug
!= XCB_WINDOW_NONE
) {
2059 xcb_destroy_window(conn
, win
->debug
);
2060 win
->debug
= XCB_WINDOW_NONE
;
2065 dumpwins(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
2073 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
2079 #endif /* SWM_DEBUG */
2084 int saved_errno
, status
;
2087 saved_errno
= errno
;
2091 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
2096 if (errno
!= ECHILD
)
2097 warn("sighdlr: waitpid");
2098 #endif /* SWM_DEBUG */
2101 if (pid
== searchpid
)
2105 if (WIFEXITED(status
)) {
2106 if (WEXITSTATUS(status
) != 0)
2107 warnx("sighdlr: child exit status: %d",
2108 WEXITSTATUS(status
));
2110 warnx("sighdlr: child is terminated "
2112 #endif /* SWM_DEBUG */
2126 errno
= saved_errno
;
2132 struct pid_e
*p
= NULL
;
2134 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
2139 TAILQ_FOREACH(p
, &pidlist
, entry
) {
2148 name_to_pixel(int sidx
, const char *colorname
)
2150 uint32_t result
= 0;
2151 char cname
[32] = "#";
2152 xcb_screen_t
*screen
;
2153 xcb_colormap_t cmap
;
2154 xcb_alloc_color_reply_t
*cr
;
2155 xcb_alloc_named_color_reply_t
*nr
;
2156 uint16_t rr
, gg
, bb
;
2158 screen
= get_screen(sidx
);
2159 cmap
= screen
->default_colormap
;
2161 /* color is in format rgb://rr/gg/bb */
2162 if (strncmp(colorname
, "rgb:", 4) == 0) {
2163 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
2164 warnx("could not parse rgb %s", colorname
);
2166 cr
= xcb_alloc_color_reply(conn
,
2167 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
2173 warnx("color '%s' not found", colorname
);
2176 nr
= xcb_alloc_named_color_reply(conn
,
2177 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
2180 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
2181 nr
= xcb_alloc_named_color_reply(conn
,
2182 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
2189 warnx("color '%s' not found", colorname
);
2196 setscreencolor(const char *val
, int i
, int c
)
2198 if (i
< 0 || i
>= get_screen_count())
2201 screens
[i
].c
[c
].pixel
= name_to_pixel(i
, val
);
2202 free(screens
[i
].c
[c
].name
);
2203 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
2208 fancy_stacker(struct workspace
*ws
)
2210 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2211 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2212 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2213 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
2214 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
2215 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2216 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2217 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
2218 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
2222 plain_stacker(struct workspace
*ws
)
2224 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2225 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2226 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
2227 sizeof ws
->stacker
);
2228 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2229 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
2230 sizeof ws
->stacker
);
2234 custom_region(const char *val
)
2236 unsigned int x
, y
, w
, h
;
2237 int sidx
, num_screens
;
2238 xcb_screen_t
*screen
;
2240 DNPRINTF(SWM_D_CONF
, "custom_region: %s\n", val
);
2242 num_screens
= get_screen_count();
2243 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
2244 errx(1, "invalid custom region, "
2245 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
2246 if (sidx
< 1 || sidx
> num_screens
)
2247 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
2251 if ((screen
= get_screen(sidx
)) == NULL
)
2252 errx(1, "ERROR: can't get screen %d.", sidx
);
2255 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
2257 if (x
> screen
->width_in_pixels
||
2258 y
> screen
->height_in_pixels
||
2259 w
+ x
> screen
->width_in_pixels
||
2260 h
+ y
> screen
->height_in_pixels
) {
2261 warnx("ignoring region %ux%u+%u+%u - not within screen "
2262 "boundaries (%ux%u)", w
, h
, x
, y
,
2263 screen
->width_in_pixels
, screen
->height_in_pixels
);
2267 new_region(&screens
[sidx
], x
, y
, w
, h
);
2271 socket_setnonblock(int fd
)
2275 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
2276 err(1, "fcntl F_GETFL");
2277 flags
|= O_NONBLOCK
;
2278 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
2279 err(1, "fcntl F_SETFL");
2283 bar_print_legacy(struct swm_region
*r
, const char *s
)
2285 xcb_rectangle_t rect
;
2290 XRectangle ibox
, lbox
;
2294 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
2296 switch (bar_justify
) {
2297 case SWM_BAR_JUSTIFY_LEFT
:
2300 case SWM_BAR_JUSTIFY_CENTER
:
2301 x
= (WIDTH(r
) - lbox
.width
) / 2;
2303 case SWM_BAR_JUSTIFY_RIGHT
:
2304 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
2308 if (x
< SWM_BAR_OFFSET
)
2313 rect
.width
= WIDTH(r
->bar
);
2314 rect
.height
= HEIGHT(r
->bar
);
2316 /* clear back buffer */
2317 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2318 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2319 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2321 /* draw back buffer */
2322 gcvd
.graphics_exposures
= 0;
2323 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
2324 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2325 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
2326 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
2328 XFreeGC(display
, draw
);
2331 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2332 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2336 bar_print(struct swm_region
*r
, const char *s
)
2339 xcb_rectangle_t rect
;
2347 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
2349 switch (bar_justify
) {
2350 case SWM_BAR_JUSTIFY_LEFT
:
2353 case SWM_BAR_JUSTIFY_CENTER
:
2354 x
= (WIDTH(r
) - info
.width
) / 2;
2356 case SWM_BAR_JUSTIFY_RIGHT
:
2357 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
2361 if (x
< SWM_BAR_OFFSET
)
2366 rect
.width
= WIDTH(r
->bar
);
2367 rect
.height
= HEIGHT(r
->bar
);
2369 /* clear back buffer */
2370 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2371 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2372 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2374 /* draw back buffer */
2375 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
2376 DefaultVisual(display
, r
->s
->idx
),
2377 DefaultColormap(display
, r
->s
->idx
));
2379 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
2380 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
2383 XftDrawDestroy(draw
);
2386 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2387 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2391 bar_extra_stop(void)
2395 bzero(bar_pipe
, sizeof bar_pipe
);
2398 kill(bar_pid
, SIGTERM
);
2401 strlcpy(bar_ext
, "", sizeof bar_ext
);
2406 bar_window_class(char *s
, size_t sz
, struct swm_region
*r
)
2408 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2410 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
2411 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
2415 bar_window_instance(char *s
, size_t sz
, struct swm_region
*r
)
2417 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2419 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
2420 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
2424 bar_window_class_instance(char *s
, size_t sz
, struct swm_region
*r
)
2426 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2429 bar_window_class(s
, sz
, r
);
2430 strlcat(s
, ":", sz
);
2431 bar_window_instance(s
, sz
, r
);
2435 bar_window_state(char *s
, size_t sz
, struct swm_region
*r
)
2437 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2439 if (MAXIMIZED(r
->ws
->focus
))
2440 strlcat(s
, "(m)", sz
);
2441 else if (ABOVE(r
->ws
->focus
))
2442 strlcat(s
, "(f)", sz
);
2446 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
2450 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2453 title
= get_win_name(r
->ws
->focus
->id
);
2454 strlcat(s
, title
, sz
);
2459 get_urgent(struct ws_win
*win
)
2461 xcb_icccm_wm_hints_t hints
;
2462 xcb_get_property_cookie_t c
;
2463 bool urgent
= false;
2466 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2467 if (xcb_icccm_get_wm_hints_reply(conn
, c
, &hints
, NULL
))
2468 urgent
= xcb_icccm_wm_hints_get_urgency(&hints
);
2475 bar_urgent(char *s
, size_t sz
)
2478 int i
, j
, num_screens
;
2479 bool urgent
[SWM_WS_MAX
];
2482 for (i
= 0; i
< workspace_limit
; i
++)
2485 num_screens
= get_screen_count();
2486 for (i
= 0; i
< num_screens
; i
++)
2487 for (j
= 0; j
< workspace_limit
; j
++)
2488 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2489 if (get_urgent(win
))
2492 for (i
= 0; i
< workspace_limit
; i
++) {
2494 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2496 } else if (!urgent_collapse
) {
2497 strlcat(s
, "- ", sz
);
2500 if (urgent_collapse
&& s
[0])
2501 s
[strlen(s
) - 1] = 0;
2505 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2507 if (r
== NULL
|| r
->ws
== NULL
)
2509 if (r
->ws
->name
!= NULL
)
2510 strlcat(s
, r
->ws
->name
, sz
);
2514 bar_workspace_state(char *s
, size_t sz
, struct swm_region
*r
)
2517 int i
, j
, num_screens
;
2518 bool used_workspaces
[SWM_WS_MAX
];
2523 if (r
== NULL
|| r
->ws
== NULL
)
2526 for (i
= 0; i
< SWM_WS_MAX
; ++i
)
2527 used_workspaces
[i
] = false;
2529 num_screens
= get_screen_count();
2530 for (i
= 0; i
< num_screens
; i
++)
2531 for (j
= 0; j
< workspace_limit
; j
++)
2532 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2533 ++used_workspaces
[win
->ws
->idx
];
2535 for (i
= 0; i
< SWM_WS_MAX
; ++i
) {
2537 if (i
== r
->ws
->idx
) {
2539 } else if (used_workspaces
[i
]) {
2545 snprintf(tmp
, sizeof tmp
, fmt
, i
+ 1);
2546 strlcat(s
, tmp
, sz
);
2551 /* build the default bar format according to the defined enabled options */
2553 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2557 /* if format provided, just copy the buffers */
2558 if (bar_format
!= NULL
) {
2559 strlcpy(fmtnew
, fmtexp
, sz
);
2563 /* reset the output buffer */
2566 strlcat(fmtnew
, "+N:+I ", sz
);
2568 strlcat(fmtnew
, "+S", sz
);
2569 strlcat(fmtnew
, " ", sz
);
2571 /* only show the workspace name if there's actually one */
2572 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2573 strlcat(fmtnew
, "<+D>", sz
);
2575 /* If enabled, only show the iconic count if there are iconic wins. */
2576 if (iconic_enabled
&& r
!= NULL
&& r
->ws
!= NULL
)
2577 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2579 strlcat(fmtnew
, "{+M}", sz
);
2583 strlcat(fmtnew
, "+3<", sz
);
2585 if (clock_enabled
) {
2586 strlcat(fmtnew
, fmtexp
, sz
);
2587 strlcat(fmtnew
, "+4<", sz
);
2590 /* bar_urgent already adds the space before the last asterisk */
2592 strlcat(fmtnew
, (urgent_collapse
? "*+U*+4<" : "* +U*+4<"), sz
);
2594 if (window_class_enabled
) {
2595 strlcat(fmtnew
, "+C", sz
);
2596 if (!window_instance_enabled
)
2597 strlcat(fmtnew
, "+4<", sz
);
2600 /* checks needed by the colon and floating strlcat(3) calls below */
2601 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2602 if (window_instance_enabled
) {
2603 if (window_class_enabled
)
2604 strlcat(fmtnew
, ":", sz
);
2605 strlcat(fmtnew
, "+T+4<", sz
);
2607 if (window_name_enabled
) {
2608 if (ABOVE(r
->ws
->focus
) || MAXIMIZED(r
->ws
->focus
))
2609 strlcat(fmtnew
, "+F ", sz
);
2610 strlcat(fmtnew
, "+64W ", sz
);
2614 /* finally add the action script output and the version */
2615 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2619 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2621 /* special case; no limit given, pad one space, instead */
2622 if (*limit
== (int)sz
- 1)
2624 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2627 /* replaces the bar format character sequences (like in tmux(1)) */
2629 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2634 char tmp
[SWM_BAR_MAX
];
2635 int limit
, size
, count
;
2638 /* reset strlcat(3) buffer */
2641 /* get number, if any */
2644 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2645 limit
= sizeof tmp
- 1;
2646 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2647 limit
= sizeof tmp
- 1;
2649 /* there is nothing to replace (ie EOL) */
2656 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2659 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2662 bar_window_class(tmp
, sizeof tmp
, r
);
2665 bar_workspace_name(tmp
, sizeof tmp
, r
);
2668 bar_window_state(tmp
, sizeof tmp
, r
);
2671 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2674 bar_workspace_state(tmp
, sizeof tmp
, r
);
2678 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2682 snprintf(tmp
, sizeof tmp
, "%d", count
);
2685 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2688 bar_window_class_instance(tmp
, sizeof tmp
, r
);
2691 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2694 bar_window_instance(tmp
, sizeof tmp
, r
);
2697 bar_urgent(tmp
, sizeof tmp
);
2700 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2703 bar_window_name(tmp
, sizeof tmp
, r
);
2706 /* unknown character sequence; copy as-is */
2707 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2713 if ((int)len
< limit
)
2715 while (limit
-- > 0) {
2716 if (*offrep
>= sz
- 1)
2718 fmtrep
[(*offrep
)++] = *ptr
++;
2726 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2731 while (*fmt
!= '\0') {
2733 /* skip ordinary characters */
2736 fmtrep
[off
++] = *fmt
++;
2740 /* character sequence found; replace it */
2741 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2750 bar_fmt_expand(char *fmtexp
, size_t sz
)
2757 /* start by grabbing the current time and date */
2759 localtime_r(&tmt
, &tm
);
2761 /* figure out what to expand */
2762 if (bar_format
!= NULL
)
2764 else if (bar_format
== NULL
&& clock_enabled
)
2766 /* if nothing to expand bail out */
2772 /* copy as-is, just in case the format shouldn't be expanded below */
2773 strlcpy(fmtexp
, fmt
, sz
);
2774 /* finally pass the string through strftime(3) */
2775 #ifndef SWM_DENY_CLOCK_FORMAT
2776 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2777 warnx("format too long");
2782 /* Redraws a region bar; need to follow with xcb_flush() or focus_flush(). */
2784 bar_draw(struct swm_bar
*bar
)
2786 struct swm_region
*r
;
2787 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2788 char fmtrep
[SWM_BAR_MAX
];
2790 /* expand the format by first passing it through strftime(3) */
2791 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2798 if (bar_enabled
&& r
->ws
->bar_enabled
)
2799 xcb_map_window(conn
, bar
->id
);
2801 xcb_unmap_window(conn
, bar
->id
);
2805 if (startup_exception
)
2806 snprintf(fmtrep
, sizeof fmtrep
, "total "
2807 "exceptions: %d, first exception: %s",
2811 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2812 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2815 if (bar_font_legacy
)
2816 bar_print_legacy(r
, fmtrep
);
2818 bar_print(r
, fmtrep
);
2822 * Reads external script output; call when stdin is readable.
2823 * Returns 1 if bar_ext was updated; otherwise 0.
2826 bar_extra_update(void)
2829 char b
[SWM_BAR_MAX
];
2830 bool changed
= false;
2835 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2838 if (b
[len
- 1] == '\n') {
2839 /* Remove newline. */
2842 /* "Clear" bar_ext. */
2845 /* Flush buffered output. */
2846 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2847 bar_ext_buf
[0] = '\0';
2849 /* Append new output to bar. */
2850 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2854 /* Buffer output. */
2855 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2860 if (errno
!= EAGAIN
) {
2861 warn("bar_action failed");
2870 bar_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
2872 struct swm_region
*tmpr
;
2875 /* suppress unused warnings since vars are needed */
2880 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2883 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2884 /* Only change if master switch is enabled. */
2886 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2888 bar_enabled
= r
->ws
->bar_enabled
= true;
2890 case SWM_ARG_ID_BAR_TOGGLE
:
2891 bar_enabled
= !bar_enabled
;
2895 /* update bars as necessary */
2896 num_screens
= get_screen_count();
2897 for (i
= 0; i
< num_screens
; i
++)
2898 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2900 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2901 xcb_map_window(conn
, tmpr
->bar
->id
);
2903 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2906 /* Restack all regions and redraw bar. */
2907 num_screens
= get_screen_count();
2908 for (i
= 0; i
< num_screens
; i
++)
2909 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
2911 bar_draw(tmpr
->bar
);
2918 bar_extra_setup(void)
2920 /* do this here because the conf file is in memory */
2921 if (!bar_extra
&& bar_argv
[0]) {
2922 /* launch external status app */
2924 if (pipe(bar_pipe
) == -1)
2925 err(1, "pipe error");
2926 socket_setnonblock(bar_pipe
[0]);
2927 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2929 /* Set stdin to read from the pipe. */
2930 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2933 /* Set stdout to write to the pipe. */
2934 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2937 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2938 err(1, "could not disable SIGPIPE");
2939 switch (bar_pid
= fork()) {
2941 err(1, "cannot fork");
2945 execvp(bar_argv
[0], bar_argv
);
2946 err(1, "%s external app failed", bar_argv
[0]);
2948 default: /* parent */
2953 atexit(kill_bar_extra_atexit
);
2958 kill_bar_extra_atexit(void)
2961 kill(bar_pid
, SIGTERM
);
2969 while ((s
= index(s
, '-'))) {
2974 return (count
== 14);
2980 char *default_string
;
2981 char **missing_charsets
;
2982 int num_missing_charsets
= 0;
2986 XFreeFontSet(display
, bar_fs
);
2990 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n",
2993 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2994 &num_missing_charsets
, &default_string
);
2996 if (num_missing_charsets
> 0) {
2997 warnx("Unable to load charset(s):");
2999 for (i
= 0; i
< num_missing_charsets
; ++i
)
3000 warnx("%s", missing_charsets
[i
]);
3002 XFreeStringList(missing_charsets
);
3004 if (strcmp(default_string
, ""))
3005 warnx("Glyphs from those sets will be replaced "
3006 "by '%s'.", default_string
);
3008 warnx("Glyphs from those sets won't be drawn.");
3012 errx(1, "Error creating font set structure.");
3014 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
3016 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
3017 2 * bar_border_width
;
3024 xft_init(struct swm_region
*r
)
3026 char *font
, *str
, *search
;
3029 if (bar_font
== NULL
) {
3030 if ((search
= str
= strdup(bar_fonts
)) == NULL
)
3031 errx(1, "insufficient memory.");
3033 while ((font
= strsep(&search
, ",")) != NULL
) {
3037 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
3040 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
3043 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
3047 if (bar_font
== NULL
) {
3048 warnx("unable to load font %s", font
);
3051 DNPRINTF(SWM_D_INIT
, "successfully opened "
3059 if (bar_font
== NULL
)
3060 errx(1, "unable to open a font");
3062 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
3064 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
3065 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
3066 warn("Xft error: unable to allocate color.");
3068 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR
].pixel
, color
);
3070 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
3071 DefaultColormap(display
, r
->s
->idx
), &color
, &search_font_color
))
3072 warn("Xft error: unable to allocate color.");
3074 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
3081 bar_setup(struct swm_region
*r
)
3083 xcb_screen_t
*screen
;
3086 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
3089 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
3090 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
3095 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
3096 err(1, "bar_setup: calloc: failed to allocate memory.");
3098 if (bar_font_legacy
)
3105 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
3106 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
3107 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
3109 /* Assume region is unfocused when we create the bar. */
3110 r
->bar
->id
= xcb_generate_id(conn
);
3111 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
3112 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
3113 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
3114 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
3116 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
3117 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
3118 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
3119 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
3120 | XCB_CW_EVENT_MASK
, wa
);
3122 /* Stack bar window above region window to start. */
3124 wa
[1] = XCB_STACK_MODE_ABOVE
;
3126 xcb_configure_window(conn
, r
->bar
->id
, XCB_CONFIG_WINDOW_SIBLING
|
3127 XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
3129 r
->bar
->buffer
= xcb_generate_id(conn
);
3130 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
3131 WIDTH(r
->bar
), HEIGHT(r
->bar
));
3134 xcb_randr_select_input(conn
, r
->bar
->id
,
3135 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
3138 xcb_map_window(conn
, r
->bar
->id
);
3140 DNPRINTF(SWM_D_BAR
, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
3141 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
3148 bar_cleanup(struct swm_region
*r
)
3152 xcb_destroy_window(conn
, r
->bar
->id
);
3153 xcb_free_pixmap(conn
, r
->bar
->buffer
);
3159 set_win_state(struct ws_win
*win
, uint8_t state
)
3161 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
3163 DNPRINTF(SWM_D_EVENT
, "set_win_state: win %#x, state: %u\n",
3169 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
3170 a_state
, 32, 2, data
);
3174 get_win_state(xcb_window_t w
)
3176 xcb_get_property_reply_t
*r
;
3177 xcb_get_property_cookie_t c
;
3178 uint32_t result
= 0;
3180 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
3181 r
= xcb_get_property_reply(conn
, c
, NULL
);
3183 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
3184 result
= *((uint32_t *)xcb_get_property_value(r
));
3188 DNPRINTF(SWM_D_MISC
, "get_win_state property: win %#x state %u\n", w
,
3194 version(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3196 struct swm_region
*tmpr
;
3199 /* suppress unused warnings since vars are needed */
3204 bar_version
= !bar_version
;
3206 snprintf(bar_vertext
, sizeof bar_vertext
,
3207 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
3209 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
3211 num_screens
= get_screen_count();
3212 for (i
= 0; i
< num_screens
; i
++) {
3213 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
3214 bar_draw(tmpr
->bar
);
3221 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
3223 xcb_client_message_event_t ev
;
3231 name
= get_atom_name(a
);
3232 DNPRINTF(SWM_D_EVENT
, "client_msg: win %#x, atom: %s(%u), "
3234 win
->id
, name
, a
, t
);
3238 bzero(&ev
, sizeof ev
);
3239 ev
.response_type
= XCB_CLIENT_MESSAGE
;
3240 ev
.window
= win
->id
;
3243 ev
.data
.data32
[0] = a
;
3244 ev
.data
.data32
[1] = t
;
3246 xcb_send_event(conn
, 0, win
->id
,
3247 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
3250 /* synthetic response to a ConfigureRequest when not making a change */
3252 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
3254 xcb_configure_notify_event_t ce
;
3259 /* send notification of unchanged state. */
3260 bzero(&ce
, sizeof(ce
));
3261 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
3264 ce
.width
= WIDTH(win
);
3265 ce
.height
= HEIGHT(win
);
3266 ce
.border_width
= 0;
3267 ce
.override_redirect
= 0;
3272 ce
.window
= win
->id
;
3273 ce
.above_sibling
= XCB_WINDOW_NONE
;
3276 ce
.event
= ev
->window
;
3277 ce
.window
= ev
->window
;
3279 /* make response appear more WM_SIZE_HINTS-compliant */
3280 if (win
->sh
.flags
) {
3281 DNPRINTF(SWM_D_MISC
, "config_win: hints: win %#x,"
3282 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
3283 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
3284 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
3285 SH_INC_W(win
), SH_INC_H(win
));
3290 /* the hint may be set... to 0! */
3291 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
3292 ce
.width
= SH_MIN_W(win
);
3293 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
3294 ce
.height
= SH_MIN_H(win
);
3299 /* may also be advertized as 0 */
3300 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
3301 ce
.width
= SH_MAX_W(win
);
3302 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
3303 ce
.height
= SH_MAX_H(win
);
3306 /* resize increment. */
3308 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
3309 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
3311 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
3312 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
3316 /* adjust x and y for requested border_width. */
3317 ce
.x
+= ev
->border_width
;
3318 ce
.y
+= ev
->border_width
;
3320 ce
.above_sibling
= ev
->sibling
;
3323 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
3324 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
3325 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
3327 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
3332 count_win(struct workspace
*ws
, bool count_transient
)
3337 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3338 if (!count_transient
&& FLOATING(win
))
3344 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
3350 quit(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3352 /* suppress unused warnings since vars are needed */
3357 DNPRINTF(SWM_D_MISC
, "quit\n");
3362 lower_window(struct ws_win
*win
)
3364 struct ws_win
*target
= NULL
;
3365 struct workspace
*ws
;
3367 DNPRINTF(SWM_D_EVENT
, "lower_window: win %#x\n", WINID(win
));
3374 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3375 if (target
== win
|| ICONIC(target
))
3377 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3380 if (win
->transient
== target
->transient
)
3382 if (win
->transient
== target
->id
)
3385 if (FULLSCREEN(target
))
3387 if (FULLSCREEN(win
))
3389 if (MAXIMIZED(target
))
3393 if (ABOVE(target
) || TRANS(target
))
3395 if (ABOVE(win
) || TRANS(win
))
3399 /* Change stack position. */
3400 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3402 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3404 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3406 update_win_stacking(win
);
3409 if (swm_debug
& SWM_D_STACK
) {
3410 DPRINTF("=== stacking order (top down) === \n");
3411 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3412 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3413 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3414 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3415 YESNO(ICONIC(target
)));
3419 DNPRINTF(SWM_D_EVENT
, "lower_window: done\n");
3423 raise_window(struct ws_win
*win
)
3425 struct ws_win
*target
= NULL
;
3426 struct workspace
*ws
;
3428 DNPRINTF(SWM_D_EVENT
, "raise_window: win %#x\n", WINID(win
));
3435 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3436 if (target
== win
|| ICONIC(target
))
3438 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3440 if (TRANS(win
) && (win
->transient
== target
->transient
||
3441 win
->transient
== target
->id
))
3443 if (FULLSCREEN(win
))
3445 if (FULLSCREEN(target
))
3449 if (MAXIMIZED(target
))
3451 if (ABOVE(win
) || TRANS(win
) ||
3452 (win
->ws
->focus
== win
&& ws
->always_raise
))
3454 if (!ABOVE(target
) && !TRANS(target
))
3458 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3460 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3462 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3464 update_win_stacking(win
);
3467 if (swm_debug
& SWM_D_STACK
) {
3468 DPRINTF("=== stacking order (top down) === \n");
3469 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3470 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3471 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3472 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3473 YESNO(ICONIC(target
)));
3477 DNPRINTF(SWM_D_EVENT
, "raise_window: done\n");
3481 update_win_stacking(struct ws_win
*win
)
3483 struct ws_win
*sibling
;
3487 struct swm_region
*r
;
3490 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
3493 if (win
->frame
== XCB_WINDOW_NONE
) {
3494 DNPRINTF(SWM_D_EVENT
, "update_window_stacking: win %#x not "
3495 "reparented.\n", win
->id
);
3499 sibling
= TAILQ_NEXT(win
, stack_entry
);
3500 if (sibling
!= NULL
&& (FLOATING(win
) == FLOATING(sibling
) ||
3501 (win
->ws
->always_raise
&& win
->ws
->focus
== win
))) {
3502 val
[0] = sibling
->frame
;
3503 val
[1] = XCB_STACK_MODE_ABOVE
;
3504 } else if (FLOATING(win
) || (win
->ws
->always_raise
&&
3505 win
->ws
->focus
== win
)) {
3506 val
[0] = r
->bar
->id
;
3507 val
[1] = XCB_STACK_MODE_ABOVE
;
3509 val
[0] = r
->bar
->id
;
3510 val
[1] = XCB_STACK_MODE_BELOW
;
3513 DNPRINTF(SWM_D_EVENT
, "update_win_stacking: win %#x (%#x), "
3514 "sibling %#x mode %#x\n", win
->frame
, win
->id
, val
[0], val
[1]);
3516 xcb_configure_window(conn
, win
->frame
, XCB_CONFIG_WINDOW_SIBLING
|
3517 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
3520 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
)
3526 map_window(struct ws_win
*win
)
3531 DNPRINTF(SWM_D_EVENT
, "map_window: win %#x, mapped: %s\n",
3532 win
->id
, YESNO(win
->mapped
));
3537 xcb_map_window(conn
, win
->frame
);
3538 xcb_map_window(conn
, win
->id
);
3539 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
3544 unmap_window(struct ws_win
*win
)
3549 DNPRINTF(SWM_D_EVENT
, "unmap_window: win %#x, mapped: %s\n", win
->id
,
3550 YESNO(win
->mapped
));
3555 xcb_unmap_window(conn
, win
->id
);
3556 xcb_unmap_window(conn
, win
->frame
);
3557 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
3558 win
->mapped
= false;
3565 int i
, j
, num_screens
;
3567 num_screens
= get_screen_count();
3568 for (i
= 0; i
< num_screens
; i
++)
3569 for (j
= 0; j
< workspace_limit
; j
++)
3570 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3575 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
3577 xcb_key_press_event_t event
;
3578 xcb_keycode_t
*keycode
;
3583 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
3585 DNPRINTF(SWM_D_MISC
, "fake_keypress: win %#x, keycode %u\n",
3588 bzero(&event
, sizeof(event
));
3589 event
.event
= win
->id
;
3590 event
.root
= win
->s
->root
;
3591 event
.child
= XCB_WINDOW_NONE
;
3592 event
.time
= XCB_CURRENT_TIME
;
3593 event
.event_x
= X(win
);
3594 event
.event_y
= Y(win
);
3597 event
.same_screen
= 1;
3598 event
.detail
= *keycode
;
3599 event
.state
= modifiers
;
3601 event
.response_type
= XCB_KEY_PRESS
;
3602 xcb_send_event(conn
, 1, win
->id
,
3603 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
3605 event
.response_type
= XCB_KEY_RELEASE
;
3606 xcb_send_event(conn
, 1, win
->id
,
3607 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
3613 restart(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3615 /* suppress unused warning since var is needed */
3620 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
3624 execvp(start_argv
[0], start_argv
);
3625 warn("execvp failed");
3626 quit(NULL
, NULL
, NULL
);
3630 get_pointer_win(xcb_window_t root
)
3632 struct ws_win
*win
= NULL
;
3633 xcb_query_pointer_reply_t
*r
;
3635 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: %#x.\n", root
);
3637 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
3639 win
= find_window(r
->child
);
3641 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: %#x.\n",
3644 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
3653 center_pointer(struct swm_region
*r
)
3657 if (!warp_pointer
|| r
== NULL
)
3662 DNPRINTF(SWM_D_EVENT
, "center_pointer: win %#x.\n", WINID(win
));
3664 if (win
&& win
->mapped
)
3665 xcb_warp_pointer(conn
, XCB_NONE
, win
->frame
, 0, 0, 0, 0,
3666 WIDTH(win
) / 2, HEIGHT(win
) / 2);
3668 xcb_warp_pointer(conn
, XCB_NONE
, r
->id
, 0, 0, 0, 0,
3669 WIDTH(r
) / 2, HEIGHT(r
) / 2);
3673 root_to_region(xcb_window_t root
, int check
)
3676 struct swm_region
*r
= NULL
;
3678 xcb_query_pointer_reply_t
*qpr
;
3679 xcb_get_input_focus_reply_t
*gifr
;
3681 DNPRINTF(SWM_D_MISC
, "root_to_region: win %#x\n", root
);
3683 num_screens
= get_screen_count();
3684 for (i
= 0; i
< num_screens
; i
++)
3685 if (screens
[i
].root
== root
)
3688 if (check
& SWM_CK_REGION
)
3689 r
= screens
[i
].r_focus
;
3691 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
3692 /* Try to find an actively focused window */
3693 gifr
= xcb_get_input_focus_reply(conn
,
3694 xcb_get_input_focus(conn
), NULL
);
3696 cfw
= find_window(gifr
->focus
);
3697 if (cfw
&& cfw
->ws
->r
)
3704 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
3705 /* No region with an active focus; try to use pointer. */
3706 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
3707 screens
[i
].root
), NULL
);
3709 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
3710 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
3711 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3712 if (X(r
) <= qpr
->root_x
&&
3713 qpr
->root_x
< MAX_X(r
) &&
3714 Y(r
) <= qpr
->root_y
&&
3715 qpr
->root_y
< MAX_Y(r
))
3722 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
3723 r
= TAILQ_FIRST(&screens
[i
].rl
);
3725 DNPRINTF(SWM_D_MISC
, "root_to_region: idx: %d\n", get_region_index(r
));
3731 find_region(xcb_window_t id
)
3733 struct swm_region
*r
;
3736 num_screens
= get_screen_count();
3737 for (i
= 0; i
< num_screens
; i
++)
3738 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3746 find_bar(xcb_window_t id
)
3748 struct swm_region
*r
;
3751 num_screens
= get_screen_count();
3752 for (i
= 0; i
< num_screens
; i
++)
3753 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3754 if (r
->bar
&& r
->bar
->id
== id
)
3761 find_frame_window(xcb_window_t id
) {
3762 struct swm_region
*r
;
3766 num_screens
= get_screen_count();
3767 for (i
= 0; i
< num_screens
; i
++)
3768 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3769 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
3777 find_window(xcb_window_t id
)
3779 struct ws_win
*win
= NULL
;
3780 int i
, j
, num_screens
;
3781 xcb_query_tree_reply_t
*qtr
;
3783 DNPRINTF(SWM_D_MISC
, "find_window: id: %#x\n", id
);
3785 num_screens
= get_screen_count();
3786 for (i
= 0; i
< num_screens
; i
++)
3787 for (j
= 0; j
< workspace_limit
; j
++)
3788 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3789 if (id
== win
->id
|| id
== win
->frame
)
3793 /* If window isn't top-level, try to find managed ancestor. */
3794 qtr
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3796 if (qtr
->parent
!= XCB_WINDOW_NONE
&& qtr
->parent
!= qtr
->root
)
3797 win
= find_window(qtr
->parent
);
3801 DNPRINTF(SWM_D_MISC
, "find_window: found child %#x "
3802 "of %#x.\n", win
->id
, qtr
->parent
);
3812 find_unmanaged_window(xcb_window_t id
)
3815 int i
, j
, num_screens
;
3817 num_screens
= get_screen_count();
3818 for (i
= 0; i
< num_screens
; i
++)
3819 for (j
= 0; j
< workspace_limit
; j
++)
3820 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
3828 spawn(int ws_idx
, union arg
*args
, bool close_fd
)
3833 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3835 close(xcb_get_file_descriptor(conn
));
3838 if ((ret
= getenv("LD_PRELOAD"))) {
3839 if (asprintf(&ret
, "%s:%s", SWM_LIB
, ret
) == -1) {
3840 warn("spawn: asprintf LD_PRELOAD");
3843 setenv("LD_PRELOAD", ret
, 1);
3846 setenv("LD_PRELOAD", SWM_LIB
, 1);
3849 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3850 warn("spawn: asprintf SWM_WS");
3853 setenv("_SWM_WS", ret
, 1);
3857 if (asprintf(&ret
, "%d", getpid()) == -1) {
3858 warn("spawn: asprintf _SWM_PID");
3861 setenv("_SWM_PID", ret
, 1);
3866 if (setsid() == -1) {
3867 warn("spawn: setsid");
3873 * close stdin and stdout to prevent interaction between apps
3874 * and the baraction script
3875 * leave stderr open to record errors
3877 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3878 warn("spawn: open");
3881 dup2(fd
, STDIN_FILENO
);
3882 dup2(fd
, STDOUT_FILENO
);
3887 execvp(args
->argv
[0], args
->argv
);
3889 warn("spawn: execvp");
3894 kill_refs(struct ws_win
*win
)
3896 struct workspace
*ws
;
3898 int i
, j
, num_screens
;
3903 num_screens
= get_screen_count();
3904 for (i
= 0; i
< num_screens
; i
++) {
3905 for (j
= 0; j
< workspace_limit
; j
++) {
3906 ws
= &screens
[i
].ws
[j
];
3908 if (win
== ws
->focus
)
3910 if (win
== ws
->focus_prev
)
3911 ws
->focus_prev
= NULL
;
3912 if (win
== ws
->focus_pending
)
3913 ws
->focus_pending
= NULL
;
3914 if (win
== ws
->focus_raise
)
3915 ws
->focus_raise
= NULL
;
3918 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3919 if (win
== w
->focus_child
)
3920 w
->focus_child
= NULL
;
3926 validate_win(struct ws_win
*testwin
)
3929 struct workspace
*ws
;
3930 struct swm_region
*r
;
3931 int i
, x
, num_screens
;
3933 if (testwin
== NULL
)
3936 num_screens
= get_screen_count();
3937 for (i
= 0; i
< num_screens
; i
++)
3938 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3939 for (x
= 0; x
< workspace_limit
; x
++) {
3941 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3949 validate_ws(struct workspace
*testws
)
3951 struct swm_region
*r
;
3952 struct workspace
*ws
;
3953 int i
, x
, num_screens
;
3955 /* validate all ws */
3956 num_screens
= get_screen_count();
3957 for (i
= 0; i
< num_screens
; i
++)
3958 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3959 for (x
= 0; x
< workspace_limit
; x
++) {
3968 unfocus_win(struct ws_win
*win
)
3970 xcb_window_t none
= XCB_WINDOW_NONE
;
3972 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: win %#x\n", WINID(win
));
3977 if (win
->ws
== NULL
) {
3978 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3982 if (validate_ws(win
->ws
)) {
3983 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3987 if (win
->ws
->r
== NULL
) {
3988 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3992 if (validate_win(win
)) {
3993 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3998 if (win
->ws
->focus
== win
) {
3999 win
->ws
->focus
= NULL
;
4000 win
->ws
->focus_prev
= win
;
4001 if (win
->ws
->focus_raise
== win
&& !FLOATING(win
)) {
4002 update_win_stacking(win
);
4006 if (validate_win(win
->ws
->focus
)) {
4007 kill_refs(win
->ws
->focus
);
4008 win
->ws
->focus
= NULL
;
4011 if (validate_win(win
->ws
->focus_prev
)) {
4012 kill_refs(win
->ws
->focus_prev
);
4013 win
->ws
->focus_prev
= NULL
;
4018 /* Raise window to "top unfocused position." */
4019 if (win
->ws
->always_raise
)
4022 /* Update border width */
4023 if (win
->bordered
&& (win
->quirks
& SWM_Q_MINIMALBORDER
) &&
4026 X(win
) += border_width
;
4027 Y(win
) += border_width
;
4031 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4032 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
4034 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
4038 focus_win(struct ws_win
*win
)
4040 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
, *tmpw
;
4041 struct workspace
*ws
;
4042 xcb_get_input_focus_reply_t
*gifr
= NULL
;
4043 xcb_get_window_attributes_reply_t
*war
= NULL
;
4045 DNPRINTF(SWM_D_FOCUS
, "focus_win: win %#x\n", WINID(win
));
4047 if (win
== NULL
|| win
->ws
== NULL
|| !win
->mapped
)
4052 if (validate_ws(ws
))
4055 if (validate_win(win
)) {
4060 gifr
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
4062 DNPRINTF(SWM_D_FOCUS
, "focus_win: cur focus: %#x\n",
4065 cfw
= find_window(gifr
->focus
);
4068 if (cfw
->ws
!= ws
&& cfw
->ws
->r
!= NULL
&&
4069 cfw
->frame
!= XCB_WINDOW_NONE
) {
4076 war
= xcb_get_window_attributes_reply(conn
,
4077 xcb_get_window_attributes(conn
, gifr
->focus
), NULL
);
4078 if (war
&& war
->override_redirect
&& ws
->focus
== win
) {
4079 DNPRINTF(SWM_D_FOCUS
, "focus_win: skip refocus "
4080 "from override_redirect.\n");
4086 if (ws
->focus
!= win
) {
4087 if (ws
->focus
&& ws
->focus
!= cfw
)
4088 unfocus_win(ws
->focus
);
4092 /* Clear focus child redirect. */
4093 win
->focus_child
= NULL
;
4095 /* If transient, adjust parent's focus child for focus_magic. */
4097 parent
= find_window(win
->transient
);
4098 if (parent
&& parent
->focus_child
!= win
)
4099 parent
->focus_child
= win
;
4102 /* Update window border even if workspace is hidden. */
4106 DNPRINTF(SWM_D_FOCUS
, "focus_win: already focused.\n");
4111 /* Set input focus if no input hint, or indicated by hint. */
4112 if (ACCEPTS_FOCUS(win
)) {
4113 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
4114 " revert-to: parent, time: %#x\n", win
->id
,
4116 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
4117 win
->id
, last_event_time
);
4118 } else if (!win
->take_focus
) {
4119 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
4120 " revert-to: parent, time: 0\n", ws
->r
->id
);
4121 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
4122 ws
->r
->id
, XCB_CURRENT_TIME
);
4125 /* Tell app it can adjust focus to a specific window. */
4126 if (win
->take_focus
) {
4127 /* java is special; always tell parent */
4128 if (TRANS(win
) && win
->java
)
4129 client_msg(parent
, a_takefocus
,
4132 client_msg(win
, a_takefocus
, last_event_time
);
4135 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
4137 /* If a parent exists, map it first. */
4139 raise_window(parent
);
4142 /* Map siblings next. */
4143 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
,
4145 if (w
!= win
&& !ICONIC(w
) &&
4146 w
->transient
== parent
->id
) {
4152 /* Map focused window. */
4156 /* Stack any children of focus window. */
4157 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
4158 if (w
->transient
== win
->id
&& !ICONIC(w
)) {
4162 } else if (tile_gap
< 0 && !ABOVE(win
)) {
4164 * Windows overlap in the layout.
4165 * Raise focused win above all tiled wins.
4173 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4174 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4177 bar_draw(ws
->r
->bar
);
4183 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
4186 /* If a child window should have focus instead, return it. */
4188 get_focus_magic(struct ws_win
*win
)
4190 struct ws_win
*parent
= NULL
;
4191 struct ws_win
*child
= NULL
;
4193 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: win %#x\n", WINID(win
));
4198 parent
= find_window(win
->transient
);
4200 /* If parent prefers focus elsewhere, then try to do so. */
4201 if (parent
&& (child
= parent
->focus_child
)) {
4202 if (validate_win(child
) == 0 && child
->mapped
)
4205 parent
->focus_child
= NULL
;
4209 /* If this window prefers focus elsewhere, then try to do so. */
4210 if ((child
= win
->focus_child
)) {
4211 if (validate_win(child
) == 0 && child
->mapped
)
4214 win
->focus_child
= NULL
;
4221 event_drain(uint8_t rt
)
4223 xcb_generic_event_t
*evt
;
4225 /* ensure all pending requests have been processed before filtering. */
4227 while ((evt
= get_next_event(false))) {
4228 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
4236 set_region(struct swm_region
*r
)
4238 struct swm_region
*rf
;
4245 /* Unfocus old region bar. */
4250 xcb_change_window_attributes(conn
, rf
->bar
->id
,
4251 XCB_CW_BORDER_PIXEL
,
4252 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
4255 if (rf
!= NULL
&& rf
!= r
&& (X(rf
) != X(r
) || Y(rf
) != Y(r
) ||
4256 WIDTH(rf
) != WIDTH(r
) || HEIGHT(rf
) != HEIGHT(r
))) {
4257 /* Set _NET_DESKTOP_GEOMETRY. */
4259 vals
[1] = HEIGHT(r
);
4260 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, r
->s
->root
,
4261 ewmh
[_NET_DESKTOP_GEOMETRY
].atom
, XCB_ATOM_CARDINAL
, 32, 2,
4265 /* Set region bar border to focus_color. */
4266 xcb_change_window_attributes(conn
, r
->bar
->id
,
4267 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
4271 /* Update the focus window frame on the now unfocused region. */
4272 if (rf
&& rf
->ws
->focus
)
4273 draw_frame(rf
->ws
->focus
);
4275 ewmh_update_current_desktop();
4279 focus_region(struct swm_region
*r
)
4282 struct swm_region
*old_r
;
4287 old_r
= r
->s
->r_focus
;
4290 nfw
= get_region_focus(r
);
4294 /* New region is empty; need to manually unfocus win. */
4296 unfocus_win(old_r
->ws
->focus
);
4297 /* Clear bar since empty. */
4298 bar_draw(old_r
->bar
);
4301 DNPRINTF(SWM_D_FOCUS
, "focus_region: set_input_focus: %#x, "
4302 "revert-to: parent, time: 0\n", r
->id
);
4303 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4310 switchws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4312 struct swm_region
*this_r
, *other_r
;
4314 struct workspace
*new_ws
, *old_ws
;
4315 xcb_window_t none
= XCB_WINDOW_NONE
;
4316 int wsid
= args
->id
;
4317 bool unmap_old
= false;
4322 if (wsid
>= workspace_limit
)
4326 old_ws
= this_r
->ws
;
4327 new_ws
= &this_r
->s
->ws
[wsid
];
4329 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
4330 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
4332 if (new_ws
== NULL
|| old_ws
== NULL
)
4334 if (new_ws
== old_ws
)
4337 other_r
= new_ws
->r
;
4338 if (other_r
&& workspace_clamp
&&
4339 bp
->action
!= FN_RG_MOVE_NEXT
&& bp
->action
!= FN_RG_MOVE_PREV
) {
4340 DNPRINTF(SWM_D_WS
, "switchws: ws clamped.\n");
4343 DNPRINTF(SWM_D_WS
, "switchws: warping focus to region "
4344 "with ws %d.\n", wsid
);
4345 focus_region(other_r
);
4346 center_pointer(other_r
);
4352 if ((win
= old_ws
->focus
) != NULL
) {
4355 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4356 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4361 /* the other ws is visible in another region, exchange them */
4362 other_r
->ws_prior
= new_ws
;
4363 other_r
->ws
= old_ws
;
4364 old_ws
->r
= other_r
;
4366 /* the other workspace is hidden, hide this one */
4371 this_r
->ws_prior
= old_ws
;
4372 this_r
->ws
= new_ws
;
4375 /* Set focus_pending before stacking, if needed. */
4376 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
4377 validate_win(new_ws
->focus_pending
))) {
4378 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
4379 new_ws
->focus
= new_ws
->focus_prev
;
4380 new_ws
->focus_prev
= NULL
;
4383 new_ws
->state
= SWM_WS_STATE_MAPPING
;
4388 /* unmap old windows */
4390 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
4392 old_ws
->state
= SWM_WS_STATE_HIDDEN
;
4395 /* if workspaces were swapped, then don't wait to set focus */
4396 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
4397 if (new_ws
->focus_pending
) {
4398 focus_win(new_ws
->focus_pending
);
4399 new_ws
->focus_pending
= NULL
;
4403 /* Clear bar and set focus on region input win if new ws is empty. */
4404 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
4405 DNPRINTF(SWM_D_FOCUS
, "switchws: set_input_focus: %#x, "
4406 "revert-to: parent, time: 0\n", r
->id
);
4407 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4412 ewmh_update_current_desktop();
4416 new_ws
->state
= SWM_WS_STATE_MAPPED
;
4418 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
4422 cyclews(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4425 struct swm_screen
*s
= r
->s
;
4426 bool cycle_all
= false, mv
= false;
4428 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4429 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4435 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
4438 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
4441 case SWM_ARG_ID_CYCLEWS_UP
:
4442 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
4444 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
4447 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
4450 case SWM_ARG_ID_CYCLEWS_DOWN
:
4451 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
4458 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
4460 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
4464 send_to_ws(bp
, r
, &a
);
4466 switchws(bp
, r
, &a
);
4467 } while (a
.id
!= r
->ws
->idx
);
4469 DNPRINTF(SWM_D_FOCUS
, "cyclews: done\n");
4473 priorws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4479 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4480 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4482 if (r
->ws_prior
== NULL
)
4485 a
.id
= r
->ws_prior
->idx
;
4486 switchws(bp
, r
, &a
);
4487 DNPRINTF(SWM_D_FOCUS
, "priorws: done\n");
4491 focusrg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4493 int ridx
= args
->id
, i
, num_screens
;
4494 struct swm_region
*rr
= NULL
;
4498 num_screens
= get_screen_count();
4499 /* do nothing if we don't have more than one screen */
4500 if (!(num_screens
> 1 || outputs
> 1))
4503 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
4505 rr
= TAILQ_FIRST(&r
->s
->rl
);
4506 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
4507 rr
= TAILQ_NEXT(rr
, entry
);
4515 DNPRINTF(SWM_D_FOCUS
, "focusrg: done\n");
4519 cyclerg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4522 struct swm_region
*rr
= NULL
;
4525 num_screens
= get_screen_count();
4526 /* do nothing if we don't have more than one screen */
4527 if (!(num_screens
> 1 || outputs
> 1))
4531 DNPRINTF(SWM_D_FOCUS
, "cyclerg: id: %d, region: %d\n", args
->id
, i
);
4534 case SWM_ARG_ID_CYCLERG_UP
:
4535 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4536 rr
= TAILQ_NEXT(r
, entry
);
4538 rr
= TAILQ_FIRST(&screens
[i
].rl
);
4540 case SWM_ARG_ID_CYCLERG_DOWN
:
4541 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4542 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
4544 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
4553 case SWM_ARG_ID_CYCLERG_UP
:
4554 case SWM_ARG_ID_CYCLERG_DOWN
:
4559 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4560 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4562 switchws(bp
, r
, &a
);
4568 DNPRINTF(SWM_D_FOCUS
, "cyclerg: done\n");
4571 /* Sorts transients after parent. */
4573 sort_windows(struct ws_win_list
*wl
)
4575 struct ws_win
*win
, *parent
, *nxt
;
4580 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
4581 nxt
= TAILQ_NEXT(win
, entry
);
4583 parent
= find_window(win
->transient
);
4584 if (parent
== NULL
) {
4585 warnx("not possible bug");
4588 TAILQ_REMOVE(wl
, win
, entry
);
4589 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
4595 swapwin(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4597 struct ws_win
*target
, *source
;
4598 struct ws_win
*cur_focus
;
4599 struct ws_win_list
*wl
;
4603 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4604 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4606 cur_focus
= r
->ws
->focus
;
4607 if (cur_focus
== NULL
|| FULLSCREEN(cur_focus
))
4610 /* Adjust stacking in floating layer. */
4611 if (ABOVE(cur_focus
)) {
4613 case SWM_ARG_ID_SWAPPREV
:
4614 target
= TAILQ_PREV(cur_focus
, ws_win_stack
,
4616 if (target
!= NULL
&& FLOATING(target
)) {
4617 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4619 TAILQ_INSERT_BEFORE(target
, cur_focus
,
4621 update_win_stacking(cur_focus
);
4625 case SWM_ARG_ID_SWAPNEXT
:
4626 target
= TAILQ_NEXT(cur_focus
, stack_entry
);
4627 if (target
!= NULL
&& FLOATING(target
)) {
4628 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4630 TAILQ_INSERT_AFTER(&cur_focus
->ws
->stack
,
4631 target
, cur_focus
, stack_entry
);
4632 update_win_stacking(cur_focus
);
4640 if (r
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4643 clear_maximized(r
->ws
);
4646 wl
= &source
->ws
->winlist
;
4649 case SWM_ARG_ID_SWAPPREV
:
4651 source
= find_window(source
->transient
);
4652 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
4653 if (target
&& target
->transient
)
4654 target
= find_window(target
->transient
);
4655 TAILQ_REMOVE(wl
, source
, entry
);
4657 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4659 TAILQ_INSERT_BEFORE(target
, source
, entry
);
4661 case SWM_ARG_ID_SWAPNEXT
:
4662 target
= TAILQ_NEXT(source
, entry
);
4663 /* move the parent and let the sort handle the move */
4665 source
= find_window(source
->transient
);
4666 TAILQ_REMOVE(wl
, source
, entry
);
4668 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4670 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
4672 case SWM_ARG_ID_SWAPMAIN
:
4673 target
= TAILQ_FIRST(wl
);
4674 if (target
== source
) {
4675 if (source
->ws
->focus_prev
!= NULL
&&
4676 source
->ws
->focus_prev
!= target
)
4677 source
= source
->ws
->focus_prev
;
4681 if (target
== NULL
|| source
== NULL
)
4683 source
->ws
->focus_prev
= target
;
4684 TAILQ_REMOVE(wl
, target
, entry
);
4685 TAILQ_INSERT_BEFORE(source
, target
, entry
);
4686 TAILQ_REMOVE(wl
, source
, entry
);
4687 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4689 case SWM_ARG_ID_MOVELAST
:
4690 TAILQ_REMOVE(wl
, source
, entry
);
4691 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4694 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
4699 ewmh_update_client_list();
4705 DNPRINTF(SWM_D_MOVE
, "swapwin: done\n");
4709 get_focus_prev(struct ws_win
*win
)
4711 struct ws_win
*winfocus
= NULL
;
4712 struct ws_win
*cur_focus
= NULL
;
4713 struct ws_win_list
*wl
= NULL
;
4714 struct workspace
*ws
= NULL
;
4716 if (!(win
&& win
->ws
))
4721 cur_focus
= ws
->focus
;
4723 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: win %#x, cur_focus: %#x, "
4724 "focus_prev: %#x\n", WINID(win
), WINID(cur_focus
),
4725 WINID(ws
->focus_prev
));
4727 /* pickle, just focus on whatever */
4728 if (cur_focus
== NULL
) {
4729 /* use prev_focus if valid */
4730 if (ws
->focus_prev
&& find_window(ws
->focus_prev
->id
))
4731 winfocus
= ws
->focus_prev
;
4735 /* if transient focus on parent */
4736 if (TRANS(cur_focus
)) {
4737 winfocus
= find_window(cur_focus
->transient
);
4741 /* if in max_stack try harder */
4742 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
4743 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
4744 if (cur_focus
!= ws
->focus_prev
)
4745 winfocus
= ws
->focus_prev
;
4747 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
4752 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
4754 if (winfocus
== NULL
|| winfocus
== win
) {
4755 switch (focus_close
) {
4756 case SWM_STACK_BOTTOM
:
4757 TAILQ_FOREACH(winfocus
, wl
, entry
)
4758 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4762 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4763 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4766 case SWM_STACK_ABOVE
:
4767 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
4768 while (winfocus
&& ICONIC(winfocus
))
4769 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4771 if (winfocus
== NULL
) {
4772 if (focus_close_wrap
) {
4773 TAILQ_FOREACH(winfocus
, wl
, entry
)
4774 if (!ICONIC(winfocus
) &&
4775 winfocus
!= cur_focus
)
4778 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4780 if (!ICONIC(winfocus
) &&
4781 winfocus
!= cur_focus
)
4786 case SWM_STACK_BELOW
:
4787 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
4788 while (winfocus
&& ICONIC(winfocus
))
4789 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
4792 if (winfocus
== NULL
) {
4793 if (focus_close_wrap
) {
4794 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4796 if (!ICONIC(winfocus
) &&
4797 winfocus
!= cur_focus
)
4800 TAILQ_FOREACH(winfocus
, wl
, entry
)
4801 if (!ICONIC(winfocus
) &&
4802 winfocus
!= cur_focus
)
4810 if (winfocus
== NULL
||
4811 (winfocus
&& (ICONIC(winfocus
) || winfocus
== cur_focus
))) {
4812 if (focus_default
== SWM_STACK_TOP
) {
4813 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4814 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4817 TAILQ_FOREACH(winfocus
, wl
, entry
)
4818 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4825 return get_focus_magic(winfocus
);
4829 get_region_focus(struct swm_region
*r
)
4831 struct ws_win
*winfocus
= NULL
;
4836 if (r
->ws
->focus
&& !ICONIC(r
->ws
->focus
))
4837 winfocus
= r
->ws
->focus
;
4838 else if (r
->ws
->focus_prev
&& !ICONIC(r
->ws
->focus_prev
))
4839 winfocus
= r
->ws
->focus_prev
;
4841 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
4842 if (!ICONIC(winfocus
))
4845 return get_focus_magic(winfocus
);
4849 focus(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4851 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
4852 struct ws_win_list
*wl
= NULL
;
4853 struct workspace
*ws
= NULL
;
4860 cur_focus
= r
->ws
->focus
;
4864 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d, cur_focus: %#x\n", args
->id
,
4867 /* Make sure an uniconified window has focus, if one exists. */
4868 if (cur_focus
== NULL
) {
4869 cur_focus
= TAILQ_FIRST(wl
);
4870 while (cur_focus
!= NULL
&& ICONIC(cur_focus
))
4871 cur_focus
= TAILQ_NEXT(cur_focus
, entry
);
4873 DNPRINTF(SWM_D_FOCUS
, "focus: new cur_focus: %#x\n",
4878 case SWM_ARG_ID_FOCUSPREV
:
4879 if (cur_focus
== NULL
)
4882 winfocus
= cur_focus
;
4884 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
4885 if (winfocus
== NULL
)
4886 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
4887 if (winfocus
== cur_focus
)
4889 } while (winfocus
&& (ICONIC(winfocus
) ||
4890 winfocus
->id
== cur_focus
->transient
||
4891 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4892 winfocus
->transient
== cur_focus
->transient
) ||
4893 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4895 case SWM_ARG_ID_FOCUSNEXT
:
4896 if (cur_focus
== NULL
)
4899 winfocus
= cur_focus
;
4901 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4902 if (winfocus
== NULL
)
4903 winfocus
= TAILQ_FIRST(wl
);
4904 if (winfocus
== cur_focus
)
4906 } while (winfocus
&& (ICONIC(winfocus
) ||
4907 winfocus
->id
== cur_focus
->transient
||
4908 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4909 winfocus
->transient
== cur_focus
->transient
) ||
4910 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4912 case SWM_ARG_ID_FOCUSMAIN
:
4913 if (cur_focus
== NULL
)
4916 winfocus
= TAILQ_FIRST(wl
);
4917 if (winfocus
== cur_focus
)
4918 winfocus
= cur_focus
->ws
->focus_prev
;
4920 case SWM_ARG_ID_FOCUSURGENT
:
4921 /* Search forward for the next urgent window. */
4925 for (i
= 0; i
<= workspace_limit
; ++i
) {
4927 head
= TAILQ_FIRST(&r
->s
->ws
[(ws
->idx
+ i
) %
4928 workspace_limit
].winlist
);
4931 if (head
== cur_focus
) {
4933 winfocus
= cur_focus
;
4936 } else if (get_urgent(head
)) {
4941 head
= TAILQ_NEXT(head
, entry
);
4948 /* Switch ws if new focus is on a different ws. */
4949 if (winfocus
&& winfocus
->ws
!= ws
) {
4950 a
.id
= winfocus
->ws
->idx
;
4951 switchws(bp
, r
, &a
);
4958 if (clear_maximized(ws
) > 0)
4961 focus_win(get_focus_magic(winfocus
));
4966 DNPRINTF(SWM_D_FOCUS
, "focus: done\n");
4970 focus_pointer(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4974 /* Not needed for buttons since this is already done in buttonpress. */
4975 if (bp
->type
== KEYBIND
) {
4976 focus_win(get_pointer_win(r
->s
->root
));
4982 cycle_layout(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4984 struct workspace
*ws
= r
->ws
;
4986 /* suppress unused warning since var is needed */
4990 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
4993 if (ws
->cur_layout
->l_stack
== NULL
)
4994 ws
->cur_layout
= &layouts
[0];
4996 clear_maximized(ws
);
5001 focus_win(get_region_focus(r
));
5008 stack_config(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5010 struct workspace
*ws
= r
->ws
;
5014 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
5017 if (args
->id
== SWM_ARG_ID_STACKINIT
) {
5020 ws
->cur_layout
->l_config(ws
, args
->id
);
5023 clear_maximized(ws
);
5033 stack(struct swm_region
*r
) {
5034 struct swm_geometry g
;
5035 struct swm_region
*r_prev
;
5041 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
5043 /* Adjust stack area for region bar and padding. */
5045 g
.x
+= region_padding
;
5046 g
.y
+= region_padding
;
5047 g
.w
-= 2 * border_width
+ 2 * region_padding
;
5048 g
.h
-= 2 * border_width
+ 2 * region_padding
;
5049 if (bar_enabled
&& r
->ws
->bar_enabled
) {
5055 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d (screen: "
5056 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
5057 r
->ws
->idx
, r
->s
->idx
, get_region_index(r
), g
.x
, g
.y
, g
.w
, g
.h
);
5059 r_prev
= TAILQ_PREV(r
, swm_region_list
, entry
);
5061 /* Stack bar/input relative to prev. region. */
5062 val
[1] = XCB_STACK_MODE_ABOVE
;
5064 val
[0] = r_prev
->id
;
5065 DNPRINTF(SWM_D_STACK
, "stack: region input %#x "
5066 "relative to %#x.\n", r
->id
, val
[0]);
5067 xcb_configure_window(conn
, r
->id
,
5068 XCB_CONFIG_WINDOW_SIBLING
|
5069 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
5071 val
[0] = r_prev
->bar
->id
;
5072 DNPRINTF(SWM_D_STACK
, "stack: region bar %#x "
5073 "relative to %#x.\n", r
->bar
->id
, val
[0]);
5074 xcb_configure_window(conn
, r
->bar
->id
,
5075 XCB_CONFIG_WINDOW_SIBLING
|
5076 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
5079 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
5080 r
->ws
->cur_layout
->l_string(r
->ws
);
5081 /* save r so we can track region changes */
5087 DNPRINTF(SWM_D_STACK
, "stack: end\n");
5091 store_float_geom(struct ws_win
*win
)
5093 if (win
== NULL
|| win
->ws
->r
== NULL
)
5096 /* retain window geom and region geom */
5097 win
->g_float
= win
->g
;
5098 win
->g_float
.x
-= X(win
->ws
->r
);
5099 win
->g_float
.y
-= Y(win
->ws
->r
);
5100 win
->g_floatvalid
= true;
5101 DNPRINTF(SWM_D_MISC
, "store_float_geom: win %#x, g: (%d,%d)"
5102 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
5103 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
5104 win
->g_float
.w
, win
->g_float
.h
);
5108 load_float_geom(struct ws_win
*win
)
5110 if (win
== NULL
|| win
->ws
->r
== NULL
)
5113 if (win
->g_floatvalid
) {
5114 win
->g
= win
->g_float
;
5115 X(win
) += X(win
->ws
->r
);
5116 Y(win
) += Y(win
->ws
->r
);
5117 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g: (%d,%d)"
5118 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
5121 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g_float "
5122 "is not set.\n", win
->id
);
5127 update_floater(struct ws_win
*win
)
5129 struct workspace
*ws
;
5130 struct swm_region
*r
;
5132 DNPRINTF(SWM_D_MISC
, "update_floater: win %#x\n", WINID(win
));
5139 if ((r
= ws
->r
) == NULL
)
5142 win
->bordered
= true;
5144 if (FULLSCREEN(win
)) {
5145 /* _NET_WM_FULLSCREEN: fullscreen without border. */
5146 if (!win
->g_floatvalid
)
5147 store_float_geom(win
);
5150 win
->bordered
= false;
5151 } else if (MAXIMIZED(win
)) {
5152 /* Maximize: like a single stacked window. */
5153 if (!win
->g_floatvalid
)
5154 store_float_geom(win
);
5158 if (bar_enabled
&& ws
->bar_enabled
&& !maximize_hide_bar
) {
5160 Y(win
) += bar_height
;
5161 HEIGHT(win
) -= bar_height
;
5162 } else if (disable_border
) {
5163 win
->bordered
= false;
5166 if (win
->bordered
) {
5167 /* Window geometry excludes frame. */
5168 X(win
) += border_width
;
5169 Y(win
) += border_width
;
5170 HEIGHT(win
) -= 2 * border_width
;
5171 WIDTH(win
) -= 2 * border_width
;
5174 /* Normal floating window. */
5175 /* Update geometry if new region. */
5177 load_float_geom(win
);
5179 if (((win
->quirks
& SWM_Q_FULLSCREEN
) &&
5180 WIDTH(win
) >= WIDTH(r
) && HEIGHT(win
) >= HEIGHT(r
)) ||
5181 ((!WS_FOCUSED(win
->ws
) || win
->ws
->focus
!= win
) &&
5182 (win
->quirks
& SWM_Q_MINIMALBORDER
))) {
5184 win
->bordered
= false;
5185 } else if (!MANUAL(win
)) {
5186 if (TRANS(win
) && (win
->quirks
& SWM_Q_TRANSSZ
)) {
5187 /* Adjust size on TRANSSZ quirk. */
5188 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
5189 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
5192 if (!(win
->quirks
& SWM_Q_ANYWHERE
)) {
5194 * Floaters and transients are auto-centred
5195 * unless manually moved, resized or ANYWHERE
5198 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2;
5199 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2;
5200 store_float_geom(win
);
5205 /* Ensure at least 1 pixel of the window is in the region. */
5206 region_containment(win
, r
, SWM_CW_ALLSIDES
);
5212 * Send keystrokes to terminal to decrease/increase the font size as the
5213 * window size changes.
5216 adjust_font(struct ws_win
*win
)
5218 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
5219 ABOVE(win
) || TRANS(win
))
5222 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
5223 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
5224 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
5225 win
->font_size_boundary
[win
->font_steps
] =
5226 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
5229 win
->last_inc
= win
->sh
.width_inc
;
5230 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
5231 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
5232 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
5235 win
->last_inc
= win
->sh
.width_inc
;
5236 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
5240 #define SWAPXY(g) do { \
5242 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
5243 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
5246 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, bool flip
)
5248 struct swm_geometry cell
, r_g
= *g
;
5250 int i
= 0, j
= 0, s
= 0, stacks
= 0;
5251 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
5252 int hrh
= 0, extra
= 0, h_slice
= 0, last_h
= 0;
5253 int split
= 0, colno
= 0;
5254 int winno
, mwin
= 0, msize
= 0;
5255 int remain
, missing
, v_slice
, mscale
;
5256 bool bordered
= true, reconfigure
= false;
5259 * cell: geometry for window, including frame.
5260 * mwin: # of windows in master area.
5261 * mscale: size increment of master area.
5262 * stacks: # of stack columns
5265 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
5266 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
5268 memset(&cell
, 0, sizeof(cell
));
5270 /* Prepare tiling variables, if needed. */
5271 if ((winno
= count_win(ws
, false)) > 0) {
5272 /* Find first tiled window. */
5273 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
5274 if (!FLOATING(win
) && !ICONIC(win
))
5277 /* Take into account size hints of first tiled window. */
5279 w_inc
= win
->sh
.width_inc
;
5280 w_base
= win
->sh
.base_width
;
5281 mwin
= ws
->l_state
.horizontal_mwin
;
5282 mscale
= ws
->l_state
.horizontal_msize
;
5283 stacks
= ws
->l_state
.horizontal_stacks
;
5286 w_inc
= win
->sh
.height_inc
;
5287 w_base
= win
->sh
.base_height
;
5288 mwin
= ws
->l_state
.vertical_mwin
;
5289 mscale
= ws
->l_state
.vertical_msize
;
5290 stacks
= ws
->l_state
.vertical_stacks
;
5294 cell
.x
+= border_width
;
5295 cell
.y
+= border_width
;
5297 if (stacks
> winno
- mwin
)
5298 stacks
= winno
- mwin
;
5302 h_slice
= r_g
.h
/ SWM_H_SLICE
;
5303 if (mwin
&& winno
> mwin
) {
5304 v_slice
= r_g
.w
/ SWM_V_SLICE
;
5308 cell
.w
= v_slice
* mscale
;
5310 if (w_inc
> 1 && w_inc
< v_slice
) {
5311 /* Adjust for requested size increment. */
5312 remain
= (cell
.w
- w_base
) % w_inc
;
5318 cell
.x
+= r_g
.w
- msize
;
5321 colno
= split
= winno
/ stacks
;
5322 cell
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
5323 2 * border_width
) / stacks
);
5326 hrh
= r_g
.h
/ colno
;
5327 extra
= r_g
.h
- (colno
* hrh
);
5328 cell
.h
= hrh
- 2 * border_width
;
5329 i
= j
= 0, s
= stacks
;
5332 /* Update window geometry. */
5333 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
5337 if (FLOATING(win
)) {
5338 update_floater(win
);
5343 if (split
&& i
== split
) {
5344 colno
= (winno
- mwin
) / stacks
;
5345 if (s
<= (winno
- mwin
) % stacks
)
5348 hrh
= r_g
.h
/ colno
;
5349 extra
= r_g
.h
- (colno
* hrh
);
5352 cell
.x
+= cell
.w
+ 2 * border_width
+
5355 cell
.w
= (r_g
.w
- msize
-
5356 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
5358 cell
.w
+= (r_g
.w
- msize
-
5359 (stacks
* (2 * border_width
+ tile_gap
))) %
5363 cell
.x
-= cell
.w
+ 2 * border_width
+
5369 cell
.h
= hrh
- 2 * border_width
- tile_gap
;
5372 h_inc
= win
->sh
.width_inc
;
5373 h_base
= win
->sh
.base_width
;
5375 h_inc
= win
->sh
.height_inc
;
5376 h_base
= win
->sh
.base_height
;
5379 if (j
== colno
- 1) {
5380 cell
.h
= hrh
+ extra
;
5381 } else if (h_inc
> 1 && h_inc
< h_slice
) {
5382 /* adjust for window's requested size increment */
5383 remain
= (cell
.h
- h_base
) % h_inc
;
5384 missing
= h_inc
- remain
;
5386 if (missing
<= extra
|| j
== 0) {
5396 cell
.y
= r_g
.y
+ border_width
;
5398 cell
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
5400 /* Window coordinates exclude frame. */
5402 if (winno
> 1 || !disable_border
||
5403 (bar_enabled
&& ws
->bar_enabled
)) {
5410 if (X(win
) != cell
.y
|| Y(win
) != cell
.x
||
5411 WIDTH(win
) != cell
.h
|| HEIGHT(win
) != cell
.w
) {
5415 WIDTH(win
) = cell
.h
;
5416 HEIGHT(win
) = cell
.w
;
5419 if (X(win
) != cell
.x
|| Y(win
) != cell
.y
||
5420 WIDTH(win
) != cell
.w
|| HEIGHT(win
) != cell
.h
) {
5424 WIDTH(win
) = cell
.w
;
5425 HEIGHT(win
) = cell
.h
;
5430 X(win
) -= border_width
;
5431 Y(win
) -= border_width
;
5432 WIDTH(win
) += 2 * border_width
;
5433 HEIGHT(win
) += 2 * border_width
;
5436 if (bordered
!= win
->bordered
) {
5438 win
->bordered
= bordered
;
5451 /* Stack all windows from bottom up. */
5452 TAILQ_FOREACH_REVERSE(win
, &ws
->stack
, ws_win_stack
, stack_entry
)
5454 update_win_stacking(win
);
5456 /* Map all windows from top down. */
5457 TAILQ_FOREACH(win
, &ws
->stack
, stack_entry
)
5461 DNPRINTF(SWM_D_STACK
, "stack_master: done\n");
5465 vertical_config(struct workspace
*ws
, int id
)
5467 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
5471 case SWM_ARG_ID_STACKINIT
:
5472 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
5473 ws
->l_state
.vertical_mwin
= 1;
5474 ws
->l_state
.vertical_stacks
= 1;
5476 case SWM_ARG_ID_MASTERSHRINK
:
5477 if (ws
->l_state
.vertical_msize
> 1)
5478 ws
->l_state
.vertical_msize
--;
5480 case SWM_ARG_ID_MASTERGROW
:
5481 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
5482 ws
->l_state
.vertical_msize
++;
5484 case SWM_ARG_ID_MASTERADD
:
5485 ws
->l_state
.vertical_mwin
++;
5487 case SWM_ARG_ID_MASTERDEL
:
5488 if (ws
->l_state
.vertical_mwin
> 0)
5489 ws
->l_state
.vertical_mwin
--;
5491 case SWM_ARG_ID_STACKBALANCE
:
5492 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ (ws
->l_state
.vertical_stacks
+ 1);
5494 case SWM_ARG_ID_STACKINC
:
5495 ws
->l_state
.vertical_stacks
++;
5497 case SWM_ARG_ID_STACKDEC
:
5498 if (ws
->l_state
.vertical_stacks
> 1)
5499 ws
->l_state
.vertical_stacks
--;
5501 case SWM_ARG_ID_FLIPLAYOUT
:
5502 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
5510 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5512 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
5514 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
5518 horizontal_config(struct workspace
*ws
, int id
)
5520 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
5523 case SWM_ARG_ID_STACKINIT
:
5524 ws
->l_state
.horizontal_mwin
= 1;
5525 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
5526 ws
->l_state
.horizontal_stacks
= 1;
5528 case SWM_ARG_ID_MASTERSHRINK
:
5529 if (ws
->l_state
.horizontal_msize
> 1)
5530 ws
->l_state
.horizontal_msize
--;
5532 case SWM_ARG_ID_MASTERGROW
:
5533 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
5534 ws
->l_state
.horizontal_msize
++;
5536 case SWM_ARG_ID_MASTERADD
:
5537 ws
->l_state
.horizontal_mwin
++;
5539 case SWM_ARG_ID_MASTERDEL
:
5540 if (ws
->l_state
.horizontal_mwin
> 0)
5541 ws
->l_state
.horizontal_mwin
--;
5543 case SWM_ARG_ID_STACKBALANCE
:
5544 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ (ws
->l_state
.horizontal_stacks
+ 1);
5546 case SWM_ARG_ID_STACKINC
:
5547 ws
->l_state
.horizontal_stacks
++;
5549 case SWM_ARG_ID_STACKDEC
:
5550 if (ws
->l_state
.horizontal_stacks
> 1)
5551 ws
->l_state
.horizontal_stacks
--;
5553 case SWM_ARG_ID_FLIPLAYOUT
:
5554 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
5562 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5564 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
5566 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
5569 /* fullscreen view */
5571 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5573 struct swm_geometry gg
= *g
;
5574 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
, *tmpw
;
5577 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
5582 winno
= count_win(ws
, false);
5583 if (winno
== 0 && count_win(ws
, true) == 0)
5586 /* Figure out which top level window should be visible. */
5587 if (ws
->focus_pending
)
5588 win
= ws
->focus_pending
;
5591 else if (ws
->focus_prev
)
5592 win
= ws
->focus_prev
;
5594 win
= TAILQ_FIRST(&ws
->winlist
);
5596 DNPRINTF(SWM_D_STACK
, "max_stack: focus_pending: %#x, focus: %#x, "
5597 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws
->focus_pending
),
5598 WINID(ws
->focus
), WINID(ws
->focus_prev
),
5599 WINID(TAILQ_FIRST(&ws
->winlist
)), win
->id
);
5601 /* Update window geometry. */
5602 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5611 /* Set maximized flag for all maxed windows. */
5612 if (!MAXIMIZED(w
)) {
5613 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_MAXIMIZED
);
5614 ewmh_update_wm_state(w
);
5617 /* Only reconfigure if necessary. */
5618 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
5619 HEIGHT(w
) != gg
.h
) {
5622 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
)) {
5623 w
->bordered
= false;
5624 WIDTH(w
) += 2 * border_width
;
5625 HEIGHT(w
) += 2 * border_width
;
5628 X(w
) += border_width
;
5629 Y(w
) += border_width
;
5636 /* If transient, stack parent and its children. */
5637 if (TRANS(win
) && (parent
= find_window(win
->transient
))) {
5638 raise_window(parent
);
5640 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5641 if (w
->transient
== parent
->id
)
5645 /* Make sure focus window is on top. */
5648 /* Stack any children of focus window. */
5649 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5650 if (w
->transient
== win
->id
)
5653 /* Map all windows. */
5654 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
5660 max_config(struct workspace
*ws
, int id
)
5662 /* suppress unused warning since vars are needed */
5666 DNPRINTF(SWM_D_STACK
, "max_config: workspace: %d (noop)\n", ws
->idx
);
5670 send_to_rg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5672 int ridx
= args
->id
, i
, num_screens
;
5673 struct swm_region
*rr
= NULL
;
5676 num_screens
= get_screen_count();
5677 /* do nothing if we don't have more than one screen */
5678 if (!(num_screens
> 1 || outputs
> 1))
5681 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
5683 rr
= TAILQ_FIRST(&r
->s
->rl
);
5684 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
5685 rr
= TAILQ_NEXT(rr
, entry
);
5692 send_to_ws(bp
, r
, &a
);
5696 region_under(struct swm_screen
*s
, int x
, int y
)
5698 struct swm_region
*r
= NULL
;
5700 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
5701 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
5702 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
5703 WIDTH(r
), HEIGHT(r
), x
, y
);
5704 if (X(r
) <= x
&& x
< MAX_X(r
))
5705 if (Y(r
) <= y
&& y
< MAX_Y(r
))
5712 /* Transfer focused window to target workspace and focus. */
5714 send_to_ws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5716 int wsid
= args
->id
;
5717 struct ws_win
*win
= NULL
;
5721 if (r
&& r
->ws
&& r
->ws
->focus
)
5726 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win %#x, ws %d\n", win
->id
, wsid
);
5728 if (wsid
< 0 || wsid
>= workspace_limit
)
5731 if (win
->ws
->idx
== wsid
)
5734 win_to_ws(win
, wsid
, true);
5736 /* Set new focus on target ws. */
5737 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5738 win
->ws
->focus_prev
= win
->ws
->focus
;
5739 win
->ws
->focus
= win
;
5740 win
->ws
->focus_pending
= NULL
;
5742 if (win
->ws
->focus_prev
)
5743 draw_frame(win
->ws
->focus_prev
);
5746 DNPRINTF(SWM_D_STACK
, "send_to_ws: focus_pending: %#x, focus: %#x, "
5747 "focus_prev: %#x, first: %#x, win: %#x\n",
5748 WINID(r
->ws
->focus_pending
), WINID(r
->ws
->focus
),
5749 WINID(r
->ws
->focus_prev
), WINID(TAILQ_FIRST(&r
->ws
->winlist
)),
5752 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5753 ewmh_update_wm_state(win
);
5755 /* Restack focused region. */
5758 /* If destination ws has a region, restack. */
5761 load_float_geom(win
);
5766 /* Set new focus on current ws. */
5767 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5768 if (r
->ws
->focus
!= NULL
) {
5769 focus_win(r
->ws
->focus
);
5771 DNPRINTF(SWM_D_FOCUS
, "send_to_ws: set_input_focus: "
5772 "%#x, revert-to: parent, time: 0\n", r
->id
);
5773 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
5774 r
->id
, XCB_CURRENT_TIME
);
5783 /* Transfer focused window to region-relative workspace and focus. */
5785 send_to_rg_relative(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5788 struct swm_region
*r_other
;
5790 if (args
->id
== 1) {
5791 r_other
= TAILQ_NEXT(r
, entry
);
5792 if (r_other
== NULL
)
5793 r_other
= TAILQ_FIRST(&r
->s
->rl
);
5795 r_other
= TAILQ_PREV(r
, swm_region_list
, entry
);
5796 if (r_other
== NULL
)
5797 r_other
= TAILQ_LAST(&r
->s
->rl
, swm_region_list
);
5800 /* Map relative to absolute */
5802 args_abs
.id
= r_other
->ws
->idx
;
5804 send_to_ws(bp
, r
, &args_abs
);
5808 win_to_ws(struct ws_win
*win
, int wsid
, bool unfocus
)
5810 struct ws_win
*parent
;
5811 struct workspace
*ws
, *nws
, *pws
;
5813 if (wsid
< 0 || wsid
>= workspace_limit
)
5816 if (win
->ws
->idx
== wsid
)
5820 nws
= &win
->s
->ws
[wsid
];
5822 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win %#x, ws %d -> %d\n", win
->id
,
5825 /* Cleanup focus on source ws. */
5826 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
5827 (ws
->focus
== win
|| ws
->focus_pending
== win
))
5828 ws
->focus_pending
= get_focus_prev(win
);
5830 /* Move the parent if this is a transient window. */
5832 parent
= find_window(win
->transient
);
5835 /* Set new focus in parent's ws if needed. */
5836 if (pws
->focus
== parent
) {
5837 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5838 pws
->focus_pending
=
5839 get_focus_prev(parent
);
5841 unfocus_win(parent
);
5843 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5844 pws
->focus
= pws
->focus_pending
;
5845 pws
->focus_pending
= NULL
;
5849 /* Don't unmap parent if new ws is visible */
5851 unmap_window(parent
);
5854 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
5855 TAILQ_REMOVE(&ws
->stack
, parent
, stack_entry
);
5856 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
5857 TAILQ_INSERT_TAIL(&nws
->stack
, parent
, stack_entry
);
5860 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5861 "_NET_WM_DESKTOP: %d\n", wsid
);
5862 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5863 parent
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
5864 XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5871 if (ws
->focus_prev
== win
)
5872 ws
->focus_prev
= NULL
;
5874 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& ws
->focus_pending
!= NULL
) {
5875 ws
->focus
= ws
->focus_pending
;
5876 ws
->focus_pending
= NULL
;
5879 /* Don't unmap if new ws is visible */
5884 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
5885 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
5886 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
5887 TAILQ_INSERT_TAIL(&nws
->stack
, win
, stack_entry
);
5890 /* Update the window's workspace property: _NET_WM_DESKTOP */
5891 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5892 "_NET_WM_DESKTOP: %d\n", wsid
);
5893 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
5894 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5896 ewmh_update_client_list();
5898 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
5902 pressbutton(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5904 /* suppress unused warning since var is needed */
5908 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
5909 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5910 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
5911 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5915 raise_focus(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5920 /* Suppress warning. */
5924 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
5928 r
->ws
->focus_raise
= win
;
5931 /* Temporarily override stacking order also in the stack */
5932 if (!FLOATING(win
)) {
5933 val
= XCB_STACK_MODE_ABOVE
;
5934 xcb_configure_window(conn
, win
->frame
,
5935 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
5940 raise_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5942 /* Suppress warning. */
5946 if (r
== NULL
|| r
->ws
== NULL
)
5949 if (r
->ws
->focus
&& MAXIMIZED(r
->ws
->focus
))
5952 r
->ws
->always_raise
= !r
->ws
->always_raise
;
5954 /* Update focused win stacking order based on new always_raise value. */
5955 raise_window(r
->ws
->focus
);
5961 iconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5965 /* Suppress warning. */
5969 if ((w
= r
->ws
->focus
) == NULL
)
5972 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_HIDDEN
);
5973 ewmh_update_wm_state(w
);
5981 get_win_name(xcb_window_t win
)
5984 xcb_get_property_cookie_t c
;
5985 xcb_get_property_reply_t
*r
;
5987 /* First try _NET_WM_NAME for UTF-8. */
5988 c
= xcb_get_property(conn
, 0, win
, ewmh
[_NET_WM_NAME
].atom
,
5989 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5990 r
= xcb_get_property_reply(conn
, c
, NULL
);
5991 if (r
&& r
->type
== XCB_NONE
) {
5993 /* Use WM_NAME instead; no UTF-8. */
5994 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
5995 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5996 r
= xcb_get_property_reply(conn
, c
, NULL
);
5999 if (r
&& r
->type
!= XCB_NONE
&& r
->length
> 0)
6000 name
= strndup(xcb_get_property_value(r
),
6001 xcb_get_property_value_length(r
));
6006 err(1, "get_win_name: failed to allocate memory.");
6014 uniconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6023 DNPRINTF(SWM_D_MISC
, "uniconify\n");
6025 if (r
== NULL
|| r
->ws
== NULL
)
6028 /* make sure we have anything to uniconify */
6029 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
6030 if (win
->ws
== NULL
)
6031 continue; /* should never happen */
6037 DNPRINTF(SWM_D_MISC
, "uniconify: count: %d\n", count
);
6043 search_resp_action
= SWM_SEARCH_UNICONIFY
;
6045 spawn_select(r
, args
, "search", &searchpid
);
6047 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
6050 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
6051 if (win
->ws
== NULL
)
6052 continue; /* should never happen */
6056 name
= get_win_name(win
->id
);
6057 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
6065 name_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6071 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
6077 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
6079 spawn_select(r
, args
, "name_workspace", &searchpid
);
6081 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
6084 fprintf(lfile
, "%s", "");
6089 search_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6092 struct workspace
*ws
;
6097 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
6103 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
6105 spawn_select(r
, args
, "search", &searchpid
);
6107 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
6110 for (i
= 0; i
< workspace_limit
; i
++) {
6114 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
6115 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
6122 search_win_cleanup(void)
6124 struct search_window
*sw
= NULL
;
6126 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
6127 xcb_destroy_window(conn
, sw
->indicator
);
6128 TAILQ_REMOVE(&search_wl
, sw
, entry
);
6134 search_win(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6136 struct ws_win
*win
= NULL
;
6137 struct search_window
*sw
= NULL
;
6140 xcb_screen_t
*screen
;
6141 int i
, width
, height
;
6149 XRectangle l_ibox
, l_lbox
;
6153 DNPRINTF(SWM_D_MISC
, "search_win\n");
6156 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
6158 spawn_select(r
, args
, "search", &searchpid
);
6160 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
6163 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
6164 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
6166 TAILQ_INIT(&search_wl
);
6169 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
6173 sw
= calloc(1, sizeof(struct search_window
));
6175 warn("search_win: calloc");
6177 search_win_cleanup();
6183 snprintf(s
, sizeof s
, "%d", i
);
6186 w
= xcb_generate_id(conn
);
6187 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6188 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6189 wa
[2] = screen
->default_colormap
;
6191 if (bar_font_legacy
) {
6192 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
6193 width
= l_lbox
.width
+ 4;
6194 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
6196 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
6198 width
= info
.width
+ 4;
6199 height
= bar_font
->height
+ 4;
6202 xcb_create_window(conn
, screen
->root_depth
, w
, win
->frame
, 0, 0,
6203 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
6204 screen
->root_visual
, XCB_CW_BACK_PIXEL
|
6205 XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
, wa
);
6207 xcb_map_window(conn
, w
);
6210 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
6212 if (bar_font_legacy
) {
6213 l_gcv
.graphics_exposures
= 0;
6214 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
6216 XSetForeground(display
, l_draw
,
6217 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
6219 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
6220 (bar_fs_extents
->max_logical_extent
.height
-
6221 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
6223 XFreeGC(display
, l_draw
);
6226 draw
= XftDrawCreate(display
, w
,
6227 DefaultVisual(display
, r
->s
->idx
),
6228 DefaultColormap(display
, r
->s
->idx
));
6230 XftDrawStringUtf8(draw
, &search_font_color
, bar_font
, 2,
6231 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
6232 bar_font
->descent
, (FcChar8
*)s
, len
);
6234 XftDrawDestroy(draw
);
6237 DNPRINTF(SWM_D_MISC
, "search_win: mapped win %#x\n", w
);
6239 fprintf(lfile
, "%d\n", i
);
6249 search_resp_uniconify(const char *resp
, size_t len
)
6255 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
6257 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
6260 name
= get_win_name(win
->id
);
6261 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
6266 if (strncmp(s
, resp
, len
) == 0) {
6267 /* XXX this should be a callback to generalize */
6268 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_HIDDEN
);
6269 ewmh_update_wm_state(win
);
6279 search_resp_name_workspace(const char *resp
, size_t len
)
6281 struct workspace
*ws
;
6283 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
6285 if (search_r
->ws
== NULL
)
6290 free(search_r
->ws
->name
);
6291 search_r
->ws
->name
= NULL
;
6295 ws
->name
= strdup(resp
);
6296 if (ws
->name
== NULL
) {
6297 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
6298 "strdup: %s", strerror(errno
));
6302 ewmh_update_desktop_names();
6303 ewmh_get_desktop_names();
6308 ewmh_update_desktop_names(void)
6310 char *name_list
= NULL
, *p
;
6311 int num_screens
, i
, j
, len
= 0, tot
= 0;
6313 num_screens
= get_screen_count();
6314 for (i
= 0; i
< num_screens
; ++i
) {
6315 for (j
= 0; j
< workspace_limit
; ++j
) {
6316 if (screens
[i
].ws
[j
].name
!= NULL
)
6317 len
+= strlen(screens
[i
].ws
[j
].name
);
6321 if ((name_list
= calloc(len
, sizeof(char))) == NULL
)
6322 err(1, "update_desktop_names: calloc: failed to "
6323 "allocate memory.");
6326 for (j
= 0; j
< workspace_limit
; ++j
) {
6327 if (screens
[i
].ws
[j
].name
!= NULL
) {
6328 len
= strlen(screens
[i
].ws
[j
].name
);
6329 memcpy(p
, screens
[i
].ws
[j
].name
, len
);
6338 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6339 screens
[i
].root
, ewmh
[_NET_DESKTOP_NAMES
].atom
,
6340 a_utf8_string
, 8, tot
, name_list
);
6350 ewmh_get_desktop_names(void)
6353 xcb_get_property_cookie_t c
;
6354 xcb_get_property_reply_t
*r
;
6355 int num_screens
, i
, j
, n
, k
;
6357 num_screens
= get_screen_count();
6358 for (i
= 0; i
< num_screens
; ++i
) {
6359 for (j
= 0; j
< workspace_limit
; ++j
) {
6360 free(screens
[i
].ws
[j
].name
);
6361 screens
[i
].ws
[j
].name
= NULL
;
6364 c
= xcb_get_property(conn
, 0, screens
[i
].root
,
6365 ewmh
[_NET_DESKTOP_NAMES
].atom
,
6366 a_utf8_string
, 0, UINT32_MAX
);
6367 r
= xcb_get_property_reply(conn
, c
, NULL
);
6371 names
= xcb_get_property_value(r
);
6372 n
= xcb_get_property_value_length(r
);
6374 for (j
= 0, k
= 0; j
< n
; ++j
) {
6375 if (*(names
+ j
) != '\0') {
6376 screens
[i
].ws
[k
].name
= strdup(names
+ j
);
6377 j
+= strlen(names
+ j
);
6386 ewmh_update_client_list(void)
6389 int num_screens
, i
, j
, k
= 0, count
= 0;
6392 num_screens
= get_screen_count();
6393 for (i
= 0; i
< num_screens
; ++i
) {
6394 for (j
= 0; j
< workspace_limit
; ++j
)
6395 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6398 DNPRINTF(SWM_D_PROP
, "ewmh_update_client_list: win count: %d\n",
6404 wins
= calloc(count
, sizeof(xcb_window_t
));
6406 err(1, "ewmh_update_client_list: calloc: failed to "
6407 "allocate memory.");
6409 for (j
= 0, k
= 0; j
< workspace_limit
; ++j
)
6410 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6411 wins
[k
++] = win
->id
;
6413 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6414 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
6415 XCB_ATOM_WINDOW
, 32, count
, wins
);
6422 ewmh_update_current_desktop(void)
6426 num_screens
= get_screen_count();
6427 for (i
= 0; i
< num_screens
; ++i
) {
6428 if (screens
[i
].r_focus
)
6429 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6430 screens
[i
].root
, ewmh
[_NET_CURRENT_DESKTOP
].atom
,
6431 XCB_ATOM_CARDINAL
, 32, 1,
6432 &screens
[i
].r_focus
->ws
->idx
);
6437 ewmh_update_desktops(void)
6439 int num_screens
, i
, j
;
6442 vals
= calloc(workspace_limit
* 2, sizeof(uint32_t));
6444 err(1, "ewmh_update_desktops: calloc: failed to allocate "
6447 num_screens
= get_screen_count();
6448 for (i
= 0; i
< num_screens
; i
++) {
6449 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6450 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
6451 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
6453 for (j
= 0; j
< workspace_limit
; ++j
) {
6454 if (screens
[i
].ws
[j
].r
!= NULL
) {
6455 vals
[j
* 2] = X(screens
[i
].ws
[j
].r
);
6456 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].r
);
6457 } else if (screens
[i
].ws
[j
].old_r
!= NULL
) {
6458 vals
[j
* 2] = X(screens
[i
].ws
[j
].old_r
);
6459 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].old_r
);
6461 vals
[j
* 2] = vals
[j
* 2 + 1] = 0;
6465 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6466 screens
[i
].root
, ewmh
[_NET_DESKTOP_VIEWPORT
].atom
,
6467 XCB_ATOM_CARDINAL
, 32, workspace_limit
* 2, vals
);
6474 search_resp_search_workspace(const char *resp
)
6481 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
6485 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
6492 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
6494 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
6501 switchws(NULL
, search_r
, &a
);
6505 search_resp_search_window(const char *resp
)
6510 struct search_window
*sw
;
6512 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
6516 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
6521 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
6523 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
6530 TAILQ_FOREACH(sw
, &search_wl
, entry
)
6531 if (idx
== sw
->idx
) {
6537 #define MAX_RESP_LEN 1024
6540 search_do_resp(void)
6546 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
6551 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
6552 warn("search: calloc");
6556 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
6558 warn("search: read error");
6561 resp
[rbytes
] = '\0';
6564 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
6565 * newline, so work around that by sanitizing the resp now.
6567 resp
[strcspn(resp
, "\n")] = '\0';
6570 switch (search_resp_action
) {
6571 case SWM_SEARCH_UNICONIFY
:
6572 search_resp_uniconify(resp
, len
);
6574 case SWM_SEARCH_NAME_WORKSPACE
:
6575 search_resp_name_workspace(resp
, len
);
6577 case SWM_SEARCH_SEARCH_WORKSPACE
:
6578 search_resp_search_workspace(resp
);
6580 case SWM_SEARCH_SEARCH_WINDOW
:
6581 search_resp_search_window(resp
);
6586 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
6587 search_win_cleanup();
6589 search_resp_action
= SWM_SEARCH_NONE
;
6590 close(select_resp_pipe
[0]);
6597 wkill(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6601 DNPRINTF(SWM_D_MISC
, "wkill: win %#x, id: %d\n", WINID(r
->ws
->focus
),
6604 if (r
->ws
->focus
== NULL
)
6607 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
6608 xcb_kill_client(conn
, r
->ws
->focus
->id
);
6610 if (r
->ws
->focus
->can_delete
)
6611 client_msg(r
->ws
->focus
, a_delete
, 0);
6617 clear_maximized(struct workspace
*ws
)
6622 /* Clear any maximized win(s) on ws, from bottom up. */
6623 TAILQ_FOREACH_REVERSE(w
, &ws
->stack
, ws_win_stack
, stack_entry
)
6625 ewmh_apply_flags(w
, w
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
6626 ewmh_update_wm_state(w
);
6634 maximize_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6636 struct ws_win
*w
= r
->ws
->focus
;
6638 /* suppress unused warning since var is needed */
6645 DNPRINTF(SWM_D_MISC
, "maximize_toggle: win %#x\n", w
->id
);
6650 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6653 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_MAXIMIZED
);
6654 ewmh_update_wm_state(w
);
6658 if (w
== w
->ws
->focus
)
6663 DNPRINTF(SWM_D_MISC
, "maximize_toggle: done\n");
6667 floating_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6669 struct ws_win
*w
= r
->ws
->focus
;
6671 /* suppress unused warning since var is needed */
6678 DNPRINTF(SWM_D_MISC
, "floating_toggle: win %#x\n", w
->id
);
6680 if (FULLSCREEN(w
) || TRANS(w
))
6683 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6686 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_ABOVE
);
6687 ewmh_update_wm_state(w
);
6691 if (w
== w
->ws
->focus
)
6696 DNPRINTF(SWM_D_MISC
, "floating_toggle: done\n");
6700 fullscreen_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6702 struct ws_win
*w
= r
->ws
->focus
;
6704 /* suppress unused warning since var is needed */
6711 DNPRINTF(SWM_D_MISC
, "fullscreen_toggle: win %#x\n", w
->id
);
6713 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_FULLSCREEN
);
6714 ewmh_update_wm_state(w
);
6718 if (w
== w
->ws
->focus
)
6723 DNPRINTF(SWM_D_MISC
, "fullscreen_toggle: done\n");
6726 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
6728 struct swm_geometry g
= r
->g
;
6729 int rt
, lt
, tp
, bm
, bw
;
6731 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
6734 * Perpendicular distance of each side of the window to the respective
6735 * side of the region boundary. Positive values indicate the side of
6736 * the window has passed beyond the region boundary.
6738 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) - MAX_X(r
) : bw
;
6739 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) : bw
;
6740 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) - MAX_Y(r
) : bw
;
6741 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) : bw
;
6743 DNPRINTF(SWM_D_MISC
, "region_containment: win %#x, rt: %d, lt: %d, "
6744 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
6745 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
6746 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
6749 * Disable containment if any of the flagged sides went beyond the
6750 * containment boundary, or if containment is disabled.
6752 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
6753 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
6754 /* Make sure window has at least 1 pixel in the region */
6755 g
.x
+= 1 - WIDTH(win
);
6756 g
.y
+= 1 - HEIGHT(win
);
6757 g
.w
+= 2 * WIDTH(win
) - 2;
6758 g
.h
+= 2 * HEIGHT(win
) - 2;
6761 constrain_window(win
, &g
, &opts
);
6764 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6766 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
6768 DNPRINTF(SWM_D_MISC
, "constrain_window: win %#x, (x,y) w x h: "
6769 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6770 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
6771 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
6772 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
6773 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
6774 YESNO(*opts
& SWM_CW_RESIZABLE
));
6776 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) > b
->x
+ b
->w
) {
6777 if (*opts
& SWM_CW_RESIZABLE
)
6778 WIDTH(win
) = b
->x
+ b
->w
- X(win
);
6780 X(win
) = b
->x
+ b
->w
- WIDTH(win
);
6783 if ((*opts
& SWM_CW_LEFT
) && X(win
) < b
->x
) {
6784 if (*opts
& SWM_CW_RESIZABLE
)
6785 WIDTH(win
) -= b
->x
- X(win
);
6790 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) > b
->y
+ b
->h
) {
6791 if (*opts
& SWM_CW_RESIZABLE
)
6792 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
);
6794 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
);
6797 if ((*opts
& SWM_CW_TOP
) && Y(win
) < b
->y
) {
6798 if (*opts
& SWM_CW_RESIZABLE
)
6799 HEIGHT(win
) -= b
->y
- Y(win
);
6804 if (*opts
& SWM_CW_RESIZABLE
) {
6807 if (HEIGHT(win
) < 1)
6813 draw_frame(struct ws_win
*win
)
6815 xcb_rectangle_t rect
[4];
6819 if (win
->frame
== XCB_WINDOW_NONE
) {
6820 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x not "
6821 "reparented.\n", win
->id
);
6825 if (!win
->bordered
) {
6826 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x frame "
6827 "disabled\n", win
->id
);
6830 if (WS_FOCUSED(win
->ws
) && win
->ws
->focus
== win
)
6831 pixel
= MAXIMIZED(win
) ?
6832 &win
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].pixel
:
6833 &win
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6835 pixel
= MAXIMIZED(win
) ?
6836 &win
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].pixel
:
6837 &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6839 /* Top (with corners) */
6842 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6843 rect
[n
].height
= border_width
;
6844 /* Left (without corners) */
6846 rect
[n
].y
= border_width
;
6847 rect
[n
].width
= border_width
;
6848 rect
[n
].height
= HEIGHT(win
);
6849 /* Right (without corners) */
6850 rect
[++n
].x
= border_width
+ WIDTH(win
);
6851 rect
[n
].y
= border_width
;
6852 rect
[n
].width
= border_width
;
6853 rect
[n
].height
= HEIGHT(win
);
6854 /* Bottom (with corners)*/
6856 rect
[n
].y
= border_width
+ HEIGHT(win
);
6857 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6858 rect
[n
].height
= border_width
;
6860 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, pixel
);
6861 xcb_poly_fill_rectangle(conn
, win
->frame
, win
->s
->bar_gc
, 4, rect
);
6865 update_window(struct ws_win
*win
)
6870 if (win
->frame
== XCB_WINDOW_NONE
) {
6871 DNPRINTF(SWM_D_EVENT
, "update_window: skip win %#x; "
6872 "not reparented.\n", win
->id
);
6876 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
6877 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
6878 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6880 /* Reconfigure frame. */
6881 if (win
->bordered
) {
6882 wc
[0] = X(win
) - border_width
;
6883 wc
[1] = Y(win
) - border_width
;
6884 wc
[2] = WIDTH(win
) + 2 * border_width
;
6885 wc
[3] = HEIGHT(win
) + 2 * border_width
;
6890 wc
[3] = HEIGHT(win
);
6895 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x frame %#x, (x,y) w x h: "
6896 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, win
->frame
, wc
[0],
6897 wc
[1], wc
[2], wc
[3], YESNO(win
->bordered
));
6899 xcb_configure_window(conn
, win
->frame
, mask
, wc
);
6901 /* Reconfigure client window. */
6902 wc
[0] = wc
[1] = win
->bordered
? border_width
: 0;
6904 wc
[3] = HEIGHT(win
);
6906 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x, (x,y) w x h: "
6907 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
6908 wc
[3], YESNO(win
->bordered
));
6909 xcb_configure_window(conn
, win
->id
, mask
, wc
);
6911 /* ICCCM 4.2.3 Synthetic ConfigureNotify when moved and not resized. */
6912 if ((X(win
) != win
->g_prev
.x
|| Y(win
) != win
->g_prev
.y
) &&
6913 (win
->java
|| (WIDTH(win
) == win
->g_prev
.w
&&
6914 HEIGHT(win
) == win
->g_prev
.h
))) {
6915 /* Java has special needs when moved together with a resize. */
6916 config_win(win
, NULL
);
6919 win
->g_prev
= win
->g
;
6923 SIMPLEQ_ENTRY(event
) entry
;
6924 xcb_generic_event_t
*ev
;
6926 SIMPLEQ_HEAD(event_queue
, event
) events
= SIMPLEQ_HEAD_INITIALIZER(events
);
6928 xcb_generic_event_t
*
6929 get_next_event(bool dowait
)
6932 xcb_generic_event_t
*evt
;
6934 /* Try queue first. */
6935 if ((ep
= SIMPLEQ_FIRST(&events
))) {
6937 SIMPLEQ_REMOVE_HEAD(&events
, entry
);
6940 evt
= xcb_wait_for_event(conn
);
6942 evt
= xcb_poll_for_event(conn
);
6948 put_back_event(xcb_generic_event_t
*evt
)
6951 if ((ep
= malloc(sizeof (struct event
))) == NULL
)
6952 err(1, "put_back_event: failed to allocate memory.");
6954 SIMPLEQ_INSERT_HEAD(&events
, ep
, entry
);
6957 /* Peeks at next event to detect auto-repeat. */
6959 keyrepeating(xcb_key_release_event_t
*kre
)
6961 xcb_generic_event_t
*evt
;
6963 /* Ensure repeating keypress is finished processing. */
6966 if ((evt
= get_next_event(false))) {
6967 put_back_event(evt
);
6969 if (XCB_EVENT_RESPONSE_TYPE(evt
) == XCB_KEY_PRESS
&&
6970 kre
->sequence
== evt
->sequence
&&
6971 kre
->detail
== ((xcb_key_press_event_t
*)evt
)->detail
)
6979 keybindreleased(struct binding
*bp
, xcb_key_release_event_t
*kre
)
6981 if (bp
->type
== KEYBIND
&& !keyrepeating(kre
) &&
6982 bp
->value
== xcb_key_press_lookup_keysym(syms
, kre
, 0))
6988 #define SWM_RESIZE_STEPS (50)
6991 resize_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
6993 xcb_timestamp_t timestamp
= 0;
6994 struct swm_region
*r
= NULL
;
6995 struct swm_geometry g
;
6996 int top
= 0, left
= 0;
6998 xcb_cursor_t cursor
;
6999 xcb_query_pointer_reply_t
*xpr
= NULL
;
7000 xcb_generic_event_t
*evt
;
7001 xcb_motion_notify_event_t
*mne
;
7002 bool resizing
, step
= false;
7008 if (FULLSCREEN(win
))
7011 /* In max_stack mode, should only resize transients. */
7012 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
7015 DNPRINTF(SWM_D_EVENT
, "resize: win %#x, floating: %s, "
7016 "transient: %#x\n", win
->id
, YESNO(ABOVE(win
)),
7020 store_float_geom(win
);
7021 else if (!(TRANS(win
) || ABOVE(win
)))
7024 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
7026 ewmh_update_wm_state(win
);
7032 /* It's possible for win to have been freed during focus_flush(). */
7033 if (validate_win(win
)) {
7034 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
7039 case SWM_ARG_ID_WIDTHSHRINK
:
7040 WIDTH(win
) -= SWM_RESIZE_STEPS
;
7043 case SWM_ARG_ID_WIDTHGROW
:
7044 WIDTH(win
) += SWM_RESIZE_STEPS
;
7047 case SWM_ARG_ID_HEIGHTSHRINK
:
7048 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
7051 case SWM_ARG_ID_HEIGHTGROW
:
7052 HEIGHT(win
) += SWM_RESIZE_STEPS
;
7059 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
7060 SWM_CW_HARDBOUNDARY
);
7062 store_float_geom(win
);
7066 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
7067 SWM_CW_SOFTBOUNDARY
);
7070 /* get cursor offset from window root */
7071 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
7078 if (xpr
->win_x
< WIDTH(win
) / 2)
7081 if (xpr
->win_y
< HEIGHT(win
) / 2)
7084 if (opt
== SWM_ARG_ID_CENTER
)
7085 cursor
= cursors
[XC_SIZING
].cid
;
7087 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
7088 XC_TOP_RIGHT_CORNER
].cid
;
7090 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
7091 XC_BOTTOM_RIGHT_CORNER
].cid
;
7093 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
7094 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
7097 /* Release keyboard freeze if called via keybind. */
7098 if (bp
->type
== KEYBIND
)
7099 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7104 while (resizing
&& (evt
= get_next_event(true))) {
7105 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
7106 case XCB_BUTTON_RELEASE
:
7107 if (bp
->type
== BTNBIND
&& bp
->value
==
7108 ((xcb_button_release_event_t
*)evt
)->detail
)
7111 case XCB_KEY_RELEASE
:
7112 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
7115 case XCB_MOTION_NOTIFY
:
7116 mne
= (xcb_motion_notify_event_t
*)evt
;
7117 DNPRINTF(SWM_D_EVENT
, "resize: MOTION_NOTIFY: "
7118 "root: %#x\n", mne
->root
);
7120 /* cursor offset/delta from start of the operation */
7121 dx
= mne
->root_x
- xpr
->root_x
;
7122 dy
= mne
->root_y
- xpr
->root_y
;
7128 if (opt
== SWM_ARG_ID_CENTER
) {
7129 if (g
.h
/ 2 + dy
< 1)
7133 HEIGHT(win
) = g
.h
+ 2 * dy
;
7141 HEIGHT(win
) = g
.h
+ dy
;
7148 if (opt
== SWM_ARG_ID_CENTER
) {
7149 if (g
.w
/ 2 + dx
< 1)
7153 WIDTH(win
) = g
.w
+ 2 * dx
;
7161 WIDTH(win
) = g
.w
+ dx
;
7164 /* not free, don't sync more than 120 times / second */
7165 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
7166 timestamp
= mne
->time
;
7167 regionize(win
, mne
->root_x
, mne
->root_y
);
7168 region_containment(win
, r
, SWM_CW_ALLSIDES
|
7169 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
7170 SWM_CW_SOFTBOUNDARY
);
7175 case XCB_BUTTON_PRESS
:
7177 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_PRESS ignored\n");
7178 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7179 ((xcb_button_press_event_t
*)evt
)->time
);
7184 DNPRINTF(SWM_D_EVENT
, "resize: KEY_PRESS ignored\n");
7185 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7186 ((xcb_key_press_event_t
*)evt
)->time
);
7192 /* It's possible for win to have been freed above. */
7193 if (validate_win(win
)) {
7194 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
7202 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
7203 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
7207 store_float_geom(win
);
7209 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7211 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
7215 resize(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
7217 struct ws_win
*win
= NULL
;
7218 xcb_query_pointer_reply_t
*qpr
= NULL
;
7223 if (args
->id
!= SWM_ARG_ID_DONTCENTER
&&
7224 args
->id
!= SWM_ARG_ID_CENTER
) {
7225 /* {height,width}_{grow,shrink} use the focus window. */
7229 /* resize uses window under pointer. */
7230 qpr
= xcb_query_pointer_reply(conn
,
7231 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7233 win
= find_window(qpr
->child
);
7239 resize_win(win
, bp
, args
->id
);
7241 if (args
->id
&& bp
->type
== KEYBIND
)
7247 /* Try to set window region based on supplied coordinates or window center. */
7249 regionize(struct ws_win
*win
, int x
, int y
)
7251 struct swm_region
*r
= NULL
;
7253 r
= region_under(win
->s
, x
, y
);
7255 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
7256 Y(win
) + HEIGHT(win
) / 2);
7258 if (r
&& r
!= win
->ws
->r
) {
7259 clear_maximized(r
->ws
);
7261 win_to_ws(win
, r
->ws
->idx
, false);
7263 /* Stack old and new region. */
7267 /* Set focus on new ws. */
7268 unfocus_win(r
->ws
->focus
);
7276 #define SWM_MOVE_STEPS (50)
7279 move_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
7281 struct swm_region
*r
;
7282 xcb_timestamp_t timestamp
= 0;
7283 xcb_query_pointer_reply_t
*qpr
= NULL
;
7284 xcb_generic_event_t
*evt
;
7285 xcb_motion_notify_event_t
*mne
;
7286 bool moving
, restack
= false, step
= false;
7291 if ((r
= win
->ws
->r
) == NULL
)
7294 if (FULLSCREEN(win
))
7297 DNPRINTF(SWM_D_EVENT
, "move: win %#x, floating: %s, transient: "
7298 "%#x\n", win
->id
, YESNO(ABOVE(win
)), win
->transient
);
7300 /* in max_stack mode should only move transients */
7301 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
7304 if (!(ABOVE(win
) || TRANS(win
)) || MAXIMIZED(win
)) {
7305 store_float_geom(win
);
7309 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
7311 ewmh_update_wm_state(win
);
7318 /* It's possible for win to have been freed during focus_flush(). */
7319 if (validate_win(win
)) {
7320 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7325 case SWM_ARG_ID_MOVELEFT
:
7326 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
7329 case SWM_ARG_ID_MOVERIGHT
:
7330 X(win
) += (SWM_MOVE_STEPS
- border_width
);
7333 case SWM_ARG_ID_MOVEUP
:
7334 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
7337 case SWM_ARG_ID_MOVEDOWN
:
7338 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
7345 regionize(win
, -1, -1);
7346 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
7348 store_float_geom(win
);
7352 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
7353 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
7354 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
7356 /* get cursor offset from window root */
7357 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
7360 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7364 /* Release keyboard freeze if called via keybind. */
7365 if (bp
->type
== KEYBIND
)
7366 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7369 regionize(win
, qpr
->root_x
, qpr
->root_y
);
7370 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7371 SWM_CW_SOFTBOUNDARY
);
7375 while (moving
&& (evt
= get_next_event(true))) {
7376 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
7377 case XCB_BUTTON_RELEASE
:
7378 if (bp
->type
== BTNBIND
&& bp
->value
==
7379 ((xcb_button_release_event_t
*)evt
)->detail
)
7382 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7383 ((xcb_button_release_event_t
*)evt
)->time
);
7386 case XCB_KEY_RELEASE
:
7387 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
7390 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7391 ((xcb_key_release_event_t
*)evt
)->time
);
7394 case XCB_MOTION_NOTIFY
:
7395 mne
= (xcb_motion_notify_event_t
*)evt
;
7396 DNPRINTF(SWM_D_EVENT
, "move: MOTION_NOTIFY: "
7397 "root: %#x\n", mne
->root
);
7398 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
7399 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
7401 /* not free, don't sync more than 120 times / second */
7402 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
7403 timestamp
= mne
->time
;
7404 regionize(win
, mne
->root_x
, mne
->root_y
);
7405 region_containment(win
, win
->ws
->r
,
7406 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
7411 case XCB_BUTTON_PRESS
:
7412 /* Thaw and ignore. */
7413 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7414 ((xcb_button_press_event_t
*)evt
)->time
);
7419 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7420 ((xcb_key_press_event_t
*)evt
)->time
);
7426 /* It's possible for win to have been freed. */
7427 if (validate_win(win
)) {
7428 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7436 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7437 SWM_CW_SOFTBOUNDARY
);
7441 store_float_geom(win
);
7443 /* New region set to fullscreen layout. */
7444 if (win
->ws
->r
&& win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
]) {
7451 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7452 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
7456 move(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
7458 struct ws_win
*win
= NULL
;
7459 xcb_query_pointer_reply_t
*qpr
= NULL
;
7465 /* move_* uses focus window. */
7469 /* Disallow move_ on tiled. */
7470 if (win
&& !(TRANS(win
) || ABOVE(win
)))
7473 /* move uses window under pointer. */
7474 qpr
= xcb_query_pointer_reply(conn
,
7475 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7477 win
= find_window(qpr
->child
);
7483 move_win(win
, bp
, args
->id
);
7485 if (args
->id
&& bp
->type
== KEYBIND
)
7491 /* action definitions */
7493 char name
[SWM_FUNCNAME_LEN
];
7494 void (*func
)(struct binding
*, struct swm_region
*,
7498 } actions
[FN_INVALID
+ 2] = {
7499 /* name function argument */
7500 { "bar_toggle", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
7501 { "bar_toggle_ws", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
7502 { "button2", pressbutton
, 0, {.id
= 2} },
7503 { "cycle_layout", cycle_layout
, 0, {0} },
7504 { "flip_layout", stack_config
, 0, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
7505 { "float_toggle", floating_toggle
,0, {0} },
7506 { "focus", focus_pointer
, 0, {0} },
7507 { "focus_main", focus
, 0, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
7508 { "focus_next", focus
, 0, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
7509 { "focus_prev", focus
, 0, {.id
= SWM_ARG_ID_FOCUSPREV
} },
7510 { "focus_urgent", focus
, 0, {.id
= SWM_ARG_ID_FOCUSURGENT
} },
7511 { "fullscreen_toggle", fullscreen_toggle
, 0, {0} },
7512 { "maximize_toggle", maximize_toggle
,0, {0} },
7513 { "height_grow", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
7514 { "height_shrink", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
7515 { "iconify", iconify
, 0, {0} },
7516 { "master_shrink", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
7517 { "master_grow", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERGROW
} },
7518 { "master_add", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERADD
} },
7519 { "master_del", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERDEL
} },
7520 { "move", move
, FN_F_NOREPLAY
, {0} },
7521 { "move_down", move
, 0, {.id
= SWM_ARG_ID_MOVEDOWN
} },
7522 { "move_left", move
, 0, {.id
= SWM_ARG_ID_MOVELEFT
} },
7523 { "move_right", move
, 0, {.id
= SWM_ARG_ID_MOVERIGHT
} },
7524 { "move_up", move
, 0, {.id
= SWM_ARG_ID_MOVEUP
} },
7525 { "mvrg_1", send_to_rg
, 0, {.id
= 0} },
7526 { "mvrg_2", send_to_rg
, 0, {.id
= 1} },
7527 { "mvrg_3", send_to_rg
, 0, {.id
= 2} },
7528 { "mvrg_4", send_to_rg
, 0, {.id
= 3} },
7529 { "mvrg_5", send_to_rg
, 0, {.id
= 4} },
7530 { "mvrg_6", send_to_rg
, 0, {.id
= 5} },
7531 { "mvrg_7", send_to_rg
, 0, {.id
= 6} },
7532 { "mvrg_8", send_to_rg
, 0, {.id
= 7} },
7533 { "mvrg_9", send_to_rg
, 0, {.id
= 8} },
7534 { "mvrg_next", send_to_rg_relative
, 0, {.id
= 1} },
7535 { "mvrg_prev", send_to_rg_relative
, 0, {.id
= -1} },
7536 { "mvws_1", send_to_ws
, 0, {.id
= 0} },
7537 { "mvws_2", send_to_ws
, 0, {.id
= 1} },
7538 { "mvws_3", send_to_ws
, 0, {.id
= 2} },
7539 { "mvws_4", send_to_ws
, 0, {.id
= 3} },
7540 { "mvws_5", send_to_ws
, 0, {.id
= 4} },
7541 { "mvws_6", send_to_ws
, 0, {.id
= 5} },
7542 { "mvws_7", send_to_ws
, 0, {.id
= 6} },
7543 { "mvws_8", send_to_ws
, 0, {.id
= 7} },
7544 { "mvws_9", send_to_ws
, 0, {.id
= 8} },
7545 { "mvws_10", send_to_ws
, 0, {.id
= 9} },
7546 { "mvws_11", send_to_ws
, 0, {.id
= 10} },
7547 { "mvws_12", send_to_ws
, 0, {.id
= 11} },
7548 { "mvws_13", send_to_ws
, 0, {.id
= 12} },
7549 { "mvws_14", send_to_ws
, 0, {.id
= 13} },
7550 { "mvws_15", send_to_ws
, 0, {.id
= 14} },
7551 { "mvws_16", send_to_ws
, 0, {.id
= 15} },
7552 { "mvws_17", send_to_ws
, 0, {.id
= 16} },
7553 { "mvws_18", send_to_ws
, 0, {.id
= 17} },
7554 { "mvws_19", send_to_ws
, 0, {.id
= 18} },
7555 { "mvws_20", send_to_ws
, 0, {.id
= 19} },
7556 { "mvws_21", send_to_ws
, 0, {.id
= 20} },
7557 { "mvws_22", send_to_ws
, 0, {.id
= 21} },
7558 { "name_workspace", name_workspace
, 0, {0} },
7559 { "quit", quit
, 0, {0} },
7560 { "raise", raise_focus
, 0, {0} },
7561 { "raise_toggle", raise_toggle
, 0, {0} },
7562 { "resize", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_DONTCENTER
} },
7563 { "resize_centered", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_CENTER
} },
7564 { "restart", restart
, 0, {0} },
7565 { "rg_1", focusrg
, 0, {.id
= 0} },
7566 { "rg_2", focusrg
, 0, {.id
= 1} },
7567 { "rg_3", focusrg
, 0, {.id
= 2} },
7568 { "rg_4", focusrg
, 0, {.id
= 3} },
7569 { "rg_5", focusrg
, 0, {.id
= 4} },
7570 { "rg_6", focusrg
, 0, {.id
= 5} },
7571 { "rg_7", focusrg
, 0, {.id
= 6} },
7572 { "rg_8", focusrg
, 0, {.id
= 7} },
7573 { "rg_9", focusrg
, 0, {.id
= 8} },
7574 { "rg_move_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_UP
} },
7575 { "rg_move_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_DOWN
} },
7576 { "rg_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7577 { "rg_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7578 { "screen_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7579 { "screen_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7580 { "search_win", search_win
, 0, {0} },
7581 { "search_workspace", search_workspace
, 0, {0} },
7582 { "spawn_custom", NULL
, 0, {0} },
7583 { "stack_balance", stack_config
, 0, {.id
= SWM_ARG_ID_STACKBALANCE
} },
7584 { "stack_inc", stack_config
, 0, {.id
= SWM_ARG_ID_STACKINC
} },
7585 { "stack_dec", stack_config
, 0, {.id
= SWM_ARG_ID_STACKDEC
} },
7586 { "stack_reset", stack_config
, 0, {.id
= SWM_ARG_ID_STACKINIT
} },
7587 { "swap_main", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPMAIN
} },
7588 { "swap_next", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPNEXT
} },
7589 { "swap_prev", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPPREV
} },
7590 { "uniconify", uniconify
, 0, {0} },
7591 { "version", version
, 0, {0} },
7592 { "width_grow", resize
, 0, {.id
= SWM_ARG_ID_WIDTHGROW
} },
7593 { "width_shrink", resize
, 0, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
7594 { "wind_del", wkill
, 0, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
7595 { "wind_kill", wkill
, 0, {.id
= SWM_ARG_ID_KILLWINDOW
} },
7596 { "ws_1", switchws
, 0, {.id
= 0} },
7597 { "ws_2", switchws
, 0, {.id
= 1} },
7598 { "ws_3", switchws
, 0, {.id
= 2} },
7599 { "ws_4", switchws
, 0, {.id
= 3} },
7600 { "ws_5", switchws
, 0, {.id
= 4} },
7601 { "ws_6", switchws
, 0, {.id
= 5} },
7602 { "ws_7", switchws
, 0, {.id
= 6} },
7603 { "ws_8", switchws
, 0, {.id
= 7} },
7604 { "ws_9", switchws
, 0, {.id
= 8} },
7605 { "ws_10", switchws
, 0, {.id
= 9} },
7606 { "ws_11", switchws
, 0, {.id
= 10} },
7607 { "ws_12", switchws
, 0, {.id
= 11} },
7608 { "ws_13", switchws
, 0, {.id
= 12} },
7609 { "ws_14", switchws
, 0, {.id
= 13} },
7610 { "ws_15", switchws
, 0, {.id
= 14} },
7611 { "ws_16", switchws
, 0, {.id
= 15} },
7612 { "ws_17", switchws
, 0, {.id
= 16} },
7613 { "ws_18", switchws
, 0, {.id
= 17} },
7614 { "ws_19", switchws
, 0, {.id
= 18} },
7615 { "ws_20", switchws
, 0, {.id
= 19} },
7616 { "ws_21", switchws
, 0, {.id
= 20} },
7617 { "ws_22", switchws
, 0, {.id
= 21} },
7618 { "ws_next", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
7619 { "ws_next_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
7620 { "ws_next_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
7621 { "ws_prev", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
7622 { "ws_prev_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
7623 { "ws_prev_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
7624 { "ws_prior", priorws
, 0, {0} },
7625 /* SWM_DEBUG actions MUST be here: */
7626 { "debug_toggle", debug_toggle
, 0, {0} },
7627 { "dumpwins", dumpwins
, 0, {0} },
7629 { "invalid action", NULL
, 0, {0} },
7633 update_modkey(uint16_t mod
)
7637 /* Replace all instances of the old mod key. */
7638 RB_FOREACH(bp
, binding_tree
, &bindings
)
7639 if (bp
->mod
& mod_key
)
7640 bp
->mod
= (bp
->mod
& ~mod_key
) | mod
;
7645 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7648 struct spawn_prog
*prog
= NULL
;
7650 char *ap
, **real_args
;
7652 /* suppress unused warning since var is needed */
7655 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
7658 TAILQ_FOREACH(prog
, &spawns
, entry
) {
7659 if (strcasecmp(spawn_name
, prog
->name
) == 0)
7663 warnx("spawn_custom: program %s not found", spawn_name
);
7667 /* make room for expanded args */
7668 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
7669 err(1, "spawn_custom: calloc real_args");
7671 /* expand spawn_args into real_args */
7672 for (i
= c
= 0; i
< prog
->argc
; i
++) {
7674 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
7675 if (strcasecmp(ap
, "$bar_border") == 0) {
7677 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
7679 err(1, "spawn_custom border color");
7680 } else if (strcasecmp(ap
, "$bar_color") == 0) {
7682 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
7684 err(1, "spawn_custom bar color");
7685 } else if (strcasecmp(ap
, "$bar_font") == 0) {
7686 if ((real_args
[c
] = strdup(bar_fonts
))
7688 err(1, "spawn_custom bar fonts");
7689 } else if (strcasecmp(ap
, "$bar_font_color") == 0) {
7691 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
7693 err(1, "spawn_custom color font");
7694 } else if (strcasecmp(ap
, "$color_focus") == 0) {
7696 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
7698 err(1, "spawn_custom color focus");
7699 } else if (strcasecmp(ap
, "$color_focus_maximized") == 0) {
7701 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].name
))
7703 err(1, "spawn_custom color focus maximized");
7704 } else if (strcasecmp(ap
, "$color_unfocus") == 0) {
7706 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
7708 err(1, "spawn_custom color unfocus");
7709 } else if (strcasecmp(ap
, "$color_unfocus_maximized") == 0) {
7711 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].name
))
7713 err(1, "spawn_custom color unfocus maximized");
7714 } else if (strcasecmp(ap
, "$region_index") == 0) {
7715 if (asprintf(&real_args
[c
], "%d",
7716 get_region_index(r
) + 1) < 1)
7717 err(1, "spawn_custom region index");
7718 } else if (strcasecmp(ap
, "$workspace_index") == 0) {
7719 if (asprintf(&real_args
[c
], "%d", r
->ws
->idx
+ 1) < 1)
7720 err(1, "spawn_custom workspace index");
7721 } else if (strcasecmp(ap
, "$dmenu_bottom") == 0) {
7724 if ((real_args
[c
] = strdup("-b")) == NULL
)
7725 err(1, "spawn_custom workspace index");
7727 /* no match --> copy as is */
7728 if ((real_args
[c
] = strdup(ap
)) == NULL
)
7729 err(1, "spawn_custom strdup(ap)");
7731 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
7737 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
7738 for (i
= 0; i
< c
; ++i
)
7739 DPRINTF("\"%s\" ", real_args
[i
]);
7742 *ret_args
= real_args
;
7747 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
7753 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7757 spawn(r
->ws
->idx
, &a
, true);
7759 for (i
= 0; i
< spawn_argc
; i
++)
7765 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7772 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7776 if (pipe(select_list_pipe
) == -1)
7777 err(1, "pipe error");
7778 if (pipe(select_resp_pipe
) == -1)
7779 err(1, "pipe error");
7781 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
7782 err(1, "could not disable SIGPIPE");
7783 switch (*pid
= fork()) {
7785 err(1, "cannot fork");
7788 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
7790 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
7792 close(select_list_pipe
[1]);
7793 close(select_resp_pipe
[0]);
7794 spawn(r
->ws
->idx
, &a
, false);
7796 default: /* parent */
7797 close(select_list_pipe
[0]);
7798 close(select_resp_pipe
[1]);
7802 for (i
= 0; i
< spawn_argc
; i
++)
7807 /* Argument tokenizer. */
7810 char *arg
, *cp
, *next
;
7811 bool single_quoted
= false, double_quoted
= false;
7816 /* Eat and move characters until end of argument is found. */
7817 for (arg
= next
= cp
= *sp
; *cp
!= '\0'; ++cp
) {
7818 if (!double_quoted
&& *cp
== '\'') {
7819 /* Eat single-quote. */
7820 single_quoted
= !single_quoted
;
7821 } else if (!single_quoted
&& *cp
== '"') {
7822 /* Eat double-quote. */
7823 double_quoted
= !double_quoted
;
7824 } else if (!single_quoted
&& *cp
== '\\' && *(cp
+ 1) == '"') {
7825 /* Eat backslash; copy escaped character to arg. */
7827 } else if (!single_quoted
&& !double_quoted
&& *cp
== '\\' &&
7828 (*(cp
+ 1) == '\'' || *(cp
+ 1) == ' ')) {
7829 /* Eat backslash; move escaped character. */
7831 } else if (!single_quoted
&& !double_quoted
&&
7832 (*cp
== ' ' || *cp
== '\t')) {
7833 /* Terminate argument. */
7835 /* Point sp to beginning of next argument. */
7839 /* Move regular character. */
7844 /* Terminate argument if end of string. */
7854 spawn_insert(const char *name
, const char *args
, int flags
)
7856 struct spawn_prog
*sp
;
7857 char *arg
, *cp
, *ptr
;
7859 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s[%s]\n", name
, args
);
7861 if (args
== NULL
|| *args
== '\0')
7864 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
7865 err(1, "spawn_insert: calloc");
7866 if ((sp
->name
= strdup(name
)) == NULL
)
7867 err(1, "spawn_insert: strdup");
7869 /* Convert the arguments to an argument list. */
7870 if ((ptr
= cp
= strdup(args
)) == NULL
)
7871 err(1, "spawn_insert: strdup");
7872 while ((arg
= argsep(&ptr
)) != NULL
) {
7873 /* Null argument; skip it. */
7878 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
7879 sizeof *sp
->argv
)) == NULL
)
7880 err(1, "spawn_insert: realloc");
7881 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
7882 err(1, "spawn_insert: strdup");
7888 DNPRINTF(SWM_D_SPAWN
, "arg %d: [%s]\n", sp
->argc
, sp
->argv
[sp
->argc
-1]);
7889 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
7890 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
7894 spawn_remove(struct spawn_prog
*sp
)
7898 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
7900 TAILQ_REMOVE(&spawns
, sp
, entry
);
7901 for (i
= 0; i
< sp
->argc
; i
++)
7907 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
7913 struct spawn_prog
*sp
;
7915 while ((sp
= TAILQ_FIRST(&spawns
)) != NULL
) {
7921 spawn_find(const char *name
)
7923 struct spawn_prog
*sp
;
7925 TAILQ_FOREACH(sp
, &spawns
, entry
)
7926 if (strcasecmp(sp
->name
, name
) == 0)
7933 setspawn(const char *name
, const char *args
, int flags
)
7935 struct spawn_prog
*sp
;
7937 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
7942 /* Remove any old spawn under the same name. */
7943 if ((sp
= spawn_find(name
)) != NULL
)
7947 spawn_insert(name
, args
, flags
);
7949 warnx("error: setspawn: cannot find program: %s", name
);
7951 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
7955 setconfspawn(const char *selector
, const char *value
, int flags
)
7959 if (selector
== NULL
|| strlen(selector
) == 0)
7962 args
= expand_tilde(value
);
7964 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
7966 setspawn(selector
, args
, flags
);
7969 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
7974 validate_spawns(void)
7977 struct spawn_prog
*sp
;
7978 char which
[PATH_MAX
];
7981 RB_FOREACH(bp
, binding_tree
, &bindings
) {
7982 if (bp
->action
!= FN_SPAWN_CUSTOM
)
7986 sp
= spawn_find(bp
->spawn_name
);
7987 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
7990 /* verify we have the goods */
7991 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
7992 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
7994 for (i
= strlen("which "); i
< strlen(which
); i
++)
7995 if (which
[i
] == ' ') {
7999 if (system(which
) != 0)
8000 add_startup_exception("could not find %s",
8001 &which
[strlen("which ")]);
8008 setconfspawn("lock", "xlock", 0);
8010 setconfspawn("term", "xterm", 0);
8011 setconfspawn("spawn_term", "xterm", 0);
8013 setconfspawn("menu", "dmenu_run"
8017 " -nf $bar_font_color"
8019 " -sf $bar_color", 0);
8021 setconfspawn("search", "dmenu"
8026 " -nf $bar_font_color"
8028 " -sf $bar_color", 0);
8030 setconfspawn("name_workspace", "dmenu"
8035 " -nf $bar_font_color"
8037 " -sf $bar_color", 0);
8039 /* These are not verified for existence, even with a binding set. */
8040 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
8041 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
8042 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
8046 #define SWM_MODNAME_SIZE 32
8047 #define SWM_KEY_WS "\n+ \t"
8049 parsebinding(const char *bindstr
, uint16_t *mod
, enum binding_type
*type
,
8050 uint32_t *val
, uint32_t *flags
)
8052 char *str
, *cp
, *name
;
8053 KeySym ks
, lks
, uks
;
8055 DNPRINTF(SWM_D_KEY
, "parsebinding: enter [%s]\n", bindstr
);
8056 if (mod
== NULL
|| val
== NULL
) {
8057 DNPRINTF(SWM_D_KEY
, "parsebinding: no mod or key vars\n");
8060 if (bindstr
== NULL
|| strlen(bindstr
) == 0) {
8061 DNPRINTF(SWM_D_KEY
, "parsebinding: no bindstr\n");
8065 if ((cp
= str
= strdup(bindstr
)) == NULL
)
8066 err(1, "parsebinding: strdup");
8068 *val
= XCB_NO_SYMBOL
;
8072 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
8073 DNPRINTF(SWM_D_KEY
, "parsebinding: entry [%s]\n", name
);
8075 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
8076 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
8078 else if (strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
) == 0)
8079 *mod
|= XCB_MOD_MASK_1
;
8080 else if (strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
) == 0)
8081 *mod
|= XCB_MOD_MASK_2
;
8082 else if (strncmp(name
, "Mod3", SWM_MODNAME_SIZE
) == 0)
8083 *mod
|= XCB_MOD_MASK_3
;
8084 else if (strncmp(name
, "Mod4", SWM_MODNAME_SIZE
) == 0)
8085 *mod
|= XCB_MOD_MASK_4
;
8086 else if (strncmp(name
, "Mod5", SWM_MODNAME_SIZE
) == 0)
8087 *mod
|= XCB_MOD_MASK_5
;
8088 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
8089 *mod
|= XCB_MOD_MASK_SHIFT
;
8090 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
8091 *mod
|= XCB_MOD_MASK_CONTROL
;
8092 else if (strncasecmp(name
, "ANYMOD", SWM_MODNAME_SIZE
) == 0)
8093 *mod
|= XCB_MOD_MASK_ANY
;
8094 else if (strncasecmp(name
, "REPLAY", SWM_MODNAME_SIZE
) == 0)
8095 *flags
|= BINDING_F_REPLAY
;
8096 else if (sscanf(name
, "Button%u", val
) == 1) {
8097 DNPRINTF(SWM_D_KEY
, "parsebinding: button %u\n", *val
);
8099 if (*val
> 255 || *val
== 0) {
8101 "parsebinding: invalid button %u\n", *val
);
8105 /* TODO: do this without Xlib. */
8106 ks
= XStringToKeysym(name
);
8107 if (ks
== NoSymbol
) {
8109 "parsebinding: invalid key %s\n", name
);
8114 XConvertCase(ks
, &lks
, &uks
);
8115 *val
= (uint32_t)lks
;
8119 /* If ANYMOD was specified, ignore the rest. */
8120 if (*mod
& XCB_MOD_MASK_ANY
)
8121 *mod
= XCB_MOD_MASK_ANY
;
8124 DNPRINTF(SWM_D_KEY
, "parsebinding: leave ok\n");
8129 strdupsafe(const char *str
)
8134 return (strdup(str
));
8138 binding_cmp(struct binding
*bp1
, struct binding
*bp2
)
8140 if (bp1
->type
< bp2
->type
)
8142 if (bp1
->type
> bp2
->type
)
8145 if (bp1
->value
< bp2
->value
)
8147 if (bp1
->value
> bp2
->value
)
8150 if (bp1
->mod
< bp2
->mod
)
8152 if (bp1
->mod
> bp2
->mod
)
8159 binding_insert(uint16_t mod
, enum binding_type type
, uint32_t val
,
8160 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
8164 DNPRINTF(SWM_D_KEY
, "binding_insert: mod: %u, type: %d, val: %u, "
8165 "action: %s(%d), spawn_name: %s\n", mod
, type
, val
,
8166 actions
[aid
].name
, aid
, spawn_name
);
8168 if ((bp
= malloc(sizeof *bp
)) == NULL
)
8169 err(1, "binding_insert: malloc");
8176 bp
->spawn_name
= strdupsafe(spawn_name
);
8177 RB_INSERT(binding_tree
, &bindings
, bp
);
8179 DNPRINTF(SWM_D_KEY
, "binding_insert: leave\n");
8183 binding_lookup(uint16_t mod
, enum binding_type type
, uint32_t val
)
8191 return (RB_FIND(binding_tree
, &bindings
, &bp
));
8195 binding_remove(struct binding
*bp
)
8197 DNPRINTF(SWM_D_KEY
, "binding_remove: mod: %u, type: %d, val: %u, "
8198 "action: %s(%d), spawn_name: %s\n", bp
->mod
, bp
->type
, bp
->value
,
8199 actions
[bp
->action
].name
, bp
->action
, bp
->spawn_name
);
8201 RB_REMOVE(binding_tree
, &bindings
, bp
);
8202 free(bp
->spawn_name
);
8205 DNPRINTF(SWM_D_KEY
, "binding_remove: leave\n");
8209 setbinding(uint16_t mod
, enum binding_type type
, uint32_t val
,
8210 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
8214 DNPRINTF(SWM_D_KEY
, "setbinding: enter %s [%s]\n",
8215 actions
[aid
].name
, spawn_name
);
8217 /* Unbind any existing. Loop is to handle MOD_MASK_ANY. */
8218 while ((bp
= binding_lookup(mod
, type
, val
)))
8221 if (aid
!= FN_INVALID
)
8222 binding_insert(mod
, type
, val
, aid
, flags
, spawn_name
);
8224 DNPRINTF(SWM_D_KEY
, "setbinding: leave\n");
8228 setconfbinding(const char *selector
, const char *value
, int flags
)
8230 struct spawn_prog
*sp
;
8231 uint32_t keybtn
, opts
;
8234 enum binding_type type
;
8236 /* suppress unused warning since var is needed */
8239 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter selector: [%s], "
8240 "value: [%s]\n", selector
, value
);
8241 if (selector
== NULL
|| strlen(selector
) == 0) {
8242 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
8243 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8244 setbinding(mod
, type
, keybtn
, FN_INVALID
, opts
, NULL
);
8249 /* search by key function name */
8250 for (aid
= 0; aid
< FN_INVALID
; aid
++) {
8251 if (strncasecmp(selector
, actions
[aid
].name
,
8252 SWM_FUNCNAME_LEN
) == 0) {
8253 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8254 "action\n", selector
);
8255 if (parsebinding(value
, &mod
, &type
, &keybtn
,
8257 setbinding(mod
, type
, keybtn
, aid
, opts
, NULL
);
8263 /* search by custom spawn name */
8264 if ((sp
= spawn_find(selector
)) != NULL
) {
8265 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8266 "spawn\n", selector
);
8267 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8268 setbinding(mod
, type
, keybtn
, FN_SPAWN_CUSTOM
, opts
,
8274 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
8278 #define MODSHIFT MODKEY | XCB_MOD_MASK_SHIFT
8280 setup_keybindings(void)
8282 #define BINDKEY(m, k, a) setbinding(m, KEYBIND, k, a, 0, NULL)
8283 #define BINDKEYSPAWN(m, k, s) setbinding(m, KEYBIND, k, FN_SPAWN_CUSTOM, 0, s)
8284 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8285 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8286 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8287 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8288 BINDKEY(MODKEY
, XK_v
, FN_BUTTON2
);
8289 BINDKEY(MODKEY
, XK_space
, FN_CYCLE_LAYOUT
);
8290 BINDKEY(MODSHIFT
, XK_backslash
, FN_FLIP_LAYOUT
);
8291 BINDKEY(MODKEY
, XK_t
, FN_FLOAT_TOGGLE
);
8292 BINDKEY(MODKEY
, XK_m
, FN_FOCUS_MAIN
);
8293 BINDKEY(MODKEY
, XK_j
, FN_FOCUS_NEXT
);
8294 BINDKEY(MODKEY
, XK_Tab
, FN_FOCUS_NEXT
);
8295 BINDKEY(MODKEY
, XK_k
, FN_FOCUS_PREV
);
8296 BINDKEY(MODSHIFT
, XK_Tab
, FN_FOCUS_PREV
);
8297 BINDKEY(MODKEY
, XK_u
, FN_FOCUS_URGENT
);
8298 BINDKEY(MODSHIFT
, XK_e
, FN_FULLSCREEN_TOGGLE
);
8299 BINDKEY(MODKEY
, XK_e
, FN_MAXIMIZE_TOGGLE
);
8300 BINDKEY(MODSHIFT
, XK_equal
, FN_HEIGHT_GROW
);
8301 BINDKEY(MODSHIFT
, XK_minus
, FN_HEIGHT_SHRINK
);
8302 BINDKEY(MODKEY
, XK_w
, FN_ICONIFY
);
8303 BINDKEY(MODKEY
, XK_h
, FN_MASTER_SHRINK
);
8304 BINDKEY(MODKEY
, XK_l
, FN_MASTER_GROW
);
8305 BINDKEY(MODKEY
, XK_comma
, FN_MASTER_ADD
);
8306 BINDKEY(MODKEY
, XK_period
, FN_MASTER_DEL
);
8307 BINDKEY(MODSHIFT
, XK_bracketright
, FN_MOVE_DOWN
);
8308 BINDKEY(MODKEY
, XK_bracketleft
, FN_MOVE_LEFT
);
8309 BINDKEY(MODKEY
, XK_bracketright
, FN_MOVE_RIGHT
);
8310 BINDKEY(MODSHIFT
, XK_bracketleft
, FN_MOVE_UP
);
8311 BINDKEY(MODSHIFT
, XK_KP_End
, FN_MVRG_1
);
8312 BINDKEY(MODSHIFT
, XK_KP_Down
, FN_MVRG_2
);
8313 BINDKEY(MODSHIFT
, XK_KP_Next
, FN_MVRG_3
);
8314 BINDKEY(MODSHIFT
, XK_KP_Left
, FN_MVRG_4
);
8315 BINDKEY(MODSHIFT
, XK_KP_Begin
, FN_MVRG_5
);
8316 BINDKEY(MODSHIFT
, XK_KP_Right
, FN_MVRG_6
);
8317 BINDKEY(MODSHIFT
, XK_KP_Home
, FN_MVRG_7
);
8318 BINDKEY(MODSHIFT
, XK_KP_Up
, FN_MVRG_8
);
8319 BINDKEY(MODSHIFT
, XK_KP_Prior
, FN_MVRG_9
);
8320 BINDKEY(MODSHIFT
, XK_1
, FN_MVWS_1
);
8321 BINDKEY(MODSHIFT
, XK_2
, FN_MVWS_2
);
8322 BINDKEY(MODSHIFT
, XK_3
, FN_MVWS_3
);
8323 BINDKEY(MODSHIFT
, XK_4
, FN_MVWS_4
);
8324 BINDKEY(MODSHIFT
, XK_5
, FN_MVWS_5
);
8325 BINDKEY(MODSHIFT
, XK_6
, FN_MVWS_6
);
8326 BINDKEY(MODSHIFT
, XK_7
, FN_MVWS_7
);
8327 BINDKEY(MODSHIFT
, XK_8
, FN_MVWS_8
);
8328 BINDKEY(MODSHIFT
, XK_9
, FN_MVWS_9
);
8329 BINDKEY(MODSHIFT
, XK_0
, FN_MVWS_10
);
8330 BINDKEY(MODSHIFT
, XK_F1
, FN_MVWS_11
);
8331 BINDKEY(MODSHIFT
, XK_F2
, FN_MVWS_12
);
8332 BINDKEY(MODSHIFT
, XK_F3
, FN_MVWS_13
);
8333 BINDKEY(MODSHIFT
, XK_F4
, FN_MVWS_14
);
8334 BINDKEY(MODSHIFT
, XK_F5
, FN_MVWS_15
);
8335 BINDKEY(MODSHIFT
, XK_F6
, FN_MVWS_16
);
8336 BINDKEY(MODSHIFT
, XK_F7
, FN_MVWS_17
);
8337 BINDKEY(MODSHIFT
, XK_F8
, FN_MVWS_18
);
8338 BINDKEY(MODSHIFT
, XK_F9
, FN_MVWS_19
);
8339 BINDKEY(MODSHIFT
, XK_F10
, FN_MVWS_20
);
8340 BINDKEY(MODSHIFT
, XK_F11
, FN_MVWS_21
);
8341 BINDKEY(MODSHIFT
, XK_F12
, FN_MVWS_22
);
8342 BINDKEY(MODSHIFT
, XK_slash
, FN_NAME_WORKSPACE
);
8343 BINDKEY(MODSHIFT
, XK_q
, FN_QUIT
);
8344 BINDKEY(MODKEY
, XK_r
, FN_RAISE
);
8345 BINDKEY(MODSHIFT
, XK_r
, FN_RAISE_TOGGLE
);
8346 BINDKEY(MODKEY
, XK_q
, FN_RESTART
);
8347 BINDKEY(MODKEY
, XK_KP_End
, FN_RG_1
);
8348 BINDKEY(MODKEY
, XK_KP_Down
, FN_RG_2
);
8349 BINDKEY(MODKEY
, XK_KP_Next
, FN_RG_3
);
8350 BINDKEY(MODKEY
, XK_KP_Left
, FN_RG_4
);
8351 BINDKEY(MODKEY
, XK_KP_Begin
, FN_RG_5
);
8352 BINDKEY(MODKEY
, XK_KP_Right
, FN_RG_6
);
8353 BINDKEY(MODKEY
, XK_KP_Home
, FN_RG_7
);
8354 BINDKEY(MODKEY
, XK_KP_Up
, FN_RG_8
);
8355 BINDKEY(MODKEY
, XK_KP_Prior
, FN_RG_9
);
8356 BINDKEY(MODSHIFT
, XK_Right
, FN_RG_NEXT
);
8357 BINDKEY(MODSHIFT
, XK_Left
, FN_RG_PREV
);
8358 BINDKEY(MODKEY
, XK_f
, FN_SEARCH_WIN
);
8359 BINDKEY(MODKEY
, XK_slash
, FN_SEARCH_WORKSPACE
);
8360 BINDKEYSPAWN(MODSHIFT
, XK_i
, "initscr");
8361 BINDKEYSPAWN(MODSHIFT
, XK_Delete
, "lock");
8362 BINDKEYSPAWN(MODKEY
, XK_p
, "menu");
8363 BINDKEYSPAWN(MODKEY
, XK_s
, "screenshot_all");
8364 BINDKEYSPAWN(MODSHIFT
, XK_s
, "screenshot_wind");
8365 BINDKEYSPAWN(MODSHIFT
, XK_Return
, "term");
8366 BINDKEY(MODSHIFT
, XK_comma
, FN_STACK_INC
);
8367 BINDKEY(MODSHIFT
, XK_period
, FN_STACK_DEC
);
8368 BINDKEY(MODSHIFT
, XK_space
, FN_STACK_INIT
);
8369 BINDKEY(MODKEY
, XK_Return
, FN_SWAP_MAIN
);
8370 BINDKEY(MODSHIFT
, XK_j
, FN_SWAP_NEXT
);
8371 BINDKEY(MODSHIFT
, XK_k
, FN_SWAP_PREV
);
8372 BINDKEY(MODSHIFT
, XK_w
, FN_UNICONIFY
);
8373 BINDKEY(MODSHIFT
, XK_v
, FN_VERSION
);
8374 BINDKEY(MODKEY
, XK_equal
, FN_WIDTH_GROW
);
8375 BINDKEY(MODKEY
, XK_minus
, FN_WIDTH_SHRINK
);
8376 BINDKEY(MODKEY
, XK_x
, FN_WIND_DEL
);
8377 BINDKEY(MODSHIFT
, XK_x
, FN_WIND_KILL
);
8378 BINDKEY(MODKEY
, XK_1
, FN_WS_1
);
8379 BINDKEY(MODKEY
, XK_2
, FN_WS_2
);
8380 BINDKEY(MODKEY
, XK_3
, FN_WS_3
);
8381 BINDKEY(MODKEY
, XK_4
, FN_WS_4
);
8382 BINDKEY(MODKEY
, XK_5
, FN_WS_5
);
8383 BINDKEY(MODKEY
, XK_6
, FN_WS_6
);
8384 BINDKEY(MODKEY
, XK_7
, FN_WS_7
);
8385 BINDKEY(MODKEY
, XK_8
, FN_WS_8
);
8386 BINDKEY(MODKEY
, XK_9
, FN_WS_9
);
8387 BINDKEY(MODKEY
, XK_0
, FN_WS_10
);
8388 BINDKEY(MODKEY
, XK_F1
, FN_WS_11
);
8389 BINDKEY(MODKEY
, XK_F2
, FN_WS_12
);
8390 BINDKEY(MODKEY
, XK_F3
, FN_WS_13
);
8391 BINDKEY(MODKEY
, XK_F4
, FN_WS_14
);
8392 BINDKEY(MODKEY
, XK_F5
, FN_WS_15
);
8393 BINDKEY(MODKEY
, XK_F6
, FN_WS_16
);
8394 BINDKEY(MODKEY
, XK_F7
, FN_WS_17
);
8395 BINDKEY(MODKEY
, XK_F8
, FN_WS_18
);
8396 BINDKEY(MODKEY
, XK_F9
, FN_WS_19
);
8397 BINDKEY(MODKEY
, XK_F10
, FN_WS_20
);
8398 BINDKEY(MODKEY
, XK_F11
, FN_WS_21
);
8399 BINDKEY(MODKEY
, XK_F12
, FN_WS_22
);
8400 BINDKEY(MODKEY
, XK_Right
, FN_WS_NEXT
);
8401 BINDKEY(MODKEY
, XK_Left
, FN_WS_PREV
);
8402 BINDKEY(MODKEY
, XK_Up
, FN_WS_NEXT_ALL
);
8403 BINDKEY(MODKEY
, XK_Down
, FN_WS_PREV_ALL
);
8404 BINDKEY(MODSHIFT
, XK_Up
, FN_WS_NEXT_MOVE
);
8405 BINDKEY(MODSHIFT
, XK_Down
, FN_WS_PREV_MOVE
);
8406 BINDKEY(MODKEY
, XK_a
, FN_WS_PRIOR
);
8408 BINDKEY(MODKEY
, XK_d
, FN_DEBUG_TOGGLE
);
8409 BINDKEY(MODSHIFT
, XK_d
, FN_DUMPWINS
);
8416 setup_btnbindings(void)
8418 setbinding(ANYMOD
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_FOCUS
,
8419 BINDING_F_REPLAY
, NULL
);
8420 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE
, 0, NULL
);
8421 setbinding(MODSHIFT
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE_CENTERED
, 0,
8423 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_MOVE
, 0, NULL
);
8428 clear_bindings(void)
8432 while ((bp
= RB_ROOT(&bindings
)))
8437 clear_keybindings(void)
8439 struct binding
*bp
, *bptmp
;
8441 RB_FOREACH_SAFE(bp
, binding_tree
, &bindings
, bptmp
) {
8442 if (bp
->type
!= KEYBIND
)
8449 setkeymapping(const char *selector
, const char *value
, int flags
)
8451 char *keymapping_file
;
8453 /* suppress unused warnings since vars are needed */
8457 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
8459 keymapping_file
= expand_tilde(value
);
8461 clear_keybindings();
8462 /* load new key bindings; if it fails, revert to default bindings */
8463 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
8464 clear_keybindings();
8465 setup_keybindings();
8468 free(keymapping_file
);
8470 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
8475 updatenumlockmask(void)
8478 xcb_get_modifier_mapping_reply_t
*modmap_r
;
8479 xcb_keycode_t
*modmap
, kc
, *keycode
;
8483 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
8484 xcb_get_modifier_mapping(conn
),
8487 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
8488 for (i
= 0; i
< 8; i
++) {
8489 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
8490 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
8492 keycode
= xcb_key_symbols_get_keycode(syms
,
8496 numlockmask
= (1 << i
);
8503 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
8510 int num_screens
, k
, j
;
8511 uint16_t modifiers
[4];
8512 xcb_keycode_t
*code
;
8514 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
8515 updatenumlockmask();
8518 modifiers
[1] = numlockmask
;
8519 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8520 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8522 num_screens
= get_screen_count();
8523 for (k
= 0; k
< num_screens
; k
++) {
8524 if (TAILQ_EMPTY(&screens
[k
].rl
))
8526 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
8528 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8529 if (bp
->type
!= KEYBIND
)
8532 /* If there is a catch-all, only bind that. */
8533 if ((binding_lookup(ANYMOD
, KEYBIND
, bp
->value
)) &&
8537 /* Skip unused ws binds. */
8538 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8539 bp
->action
<= FN_WS_22
)
8542 /* Skip unused mvws binds. */
8543 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8544 bp
->action
<= FN_MVWS_22
)
8547 if ((code
= xcb_key_symbols_get_keycode(syms
,
8548 bp
->value
)) == NULL
)
8551 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8552 /* All modifiers are grabbed in one pass. */
8553 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8554 "key: %u, modifiers: %d\n", bp
->value
,
8556 xcb_grab_key(conn
, 1, screens
[k
].root
,
8557 bp
->mod
, *code
, XCB_GRAB_MODE_ASYNC
,
8558 XCB_GRAB_MODE_SYNC
);
8560 /* Need to grab each modifier permutation. */
8561 for (j
= 0; j
< LENGTH(modifiers
); j
++) {
8562 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8563 "key: %u, modifiers: %d\n",
8564 bp
->value
, bp
->mod
| modifiers
[j
]);
8565 xcb_grab_key(conn
, 1,
8567 bp
->mod
| modifiers
[j
],
8568 *code
, XCB_GRAB_MODE_ASYNC
,
8569 XCB_GRAB_MODE_SYNC
);
8581 int num_screens
, i
, k
;
8582 uint16_t modifiers
[4];
8584 DNPRINTF(SWM_D_MOUSE
, "grabbuttons\n");
8585 updatenumlockmask();
8588 modifiers
[1] = numlockmask
;
8589 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8590 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8592 num_screens
= get_screen_count();
8593 for (k
= 0; k
< num_screens
; k
++) {
8594 if (TAILQ_EMPTY(&screens
[k
].rl
))
8596 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, screens
[k
].root
,
8598 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8599 if (bp
->type
!= BTNBIND
)
8602 /* If there is a catch-all, only bind that. */
8603 if ((binding_lookup(ANYMOD
, BTNBIND
, bp
->value
)) &&
8607 /* Skip unused ws binds. */
8608 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8609 bp
->action
<= FN_WS_22
)
8612 /* Skip unused mvws binds. */
8613 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8614 bp
->action
<= FN_MVWS_22
)
8617 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8618 /* All modifiers are grabbed in one pass. */
8619 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: grab, "
8620 "button: %u, modifiers: %d\n", bp
->value
,
8622 xcb_grab_button(conn
, 0, screens
[k
].root
,
8623 BUTTONMASK
, XCB_GRAB_MODE_SYNC
,
8624 XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
8625 XCB_CURSOR_NONE
, bp
->value
, bp
->mod
);
8627 /* Need to grab each modifier permutation. */
8628 for (i
= 0; i
< LENGTH(modifiers
); ++i
) {
8629 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: "
8630 "grab, button: %u, modifiers: %u\n",
8631 bp
->value
, bp
->mod
| modifiers
[i
]);
8632 xcb_grab_button(conn
, 0,
8633 screens
[k
].root
, BUTTONMASK
,
8635 XCB_GRAB_MODE_ASYNC
,
8637 XCB_CURSOR_NONE
, bp
->value
,
8638 bp
->mod
| modifiers
[i
]);
8645 const char *quirkname
[] = {
8646 "NONE", /* config string for "no value" */
8654 "FOCUSONMAP_SINGLE",
8662 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
8663 #define SWM_Q_DELIM "\n|+ \t"
8665 parsequirks(const char *qstr
, uint32_t *quirk
, int *ws
)
8667 char *str
, *cp
, *name
;
8670 if (quirk
== NULL
|| qstr
== NULL
)
8673 if ((cp
= str
= strdup(qstr
)) == NULL
)
8674 err(1, "parsequirks: strdup");
8677 while ((name
= strsep(&cp
, SWM_Q_DELIM
)) != NULL
) {
8679 cp
+= (long)strspn(cp
, SWM_Q_DELIM
);
8681 if (sscanf(name
, "WS[%d]", ws
) == 1) {
8687 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
8688 if (strncasecmp(name
, quirkname
[i
],
8689 SWM_QUIRK_LEN
) == 0) {
8690 DNPRINTF(SWM_D_QUIRK
,
8691 "parsequirks: %s\n", name
);
8697 *quirk
|= 1 << (i
-1);
8701 if (i
>= LENGTH(quirkname
)) {
8702 DNPRINTF(SWM_D_QUIRK
,
8703 "parsequirks: invalid quirk [%s]\n", name
);
8714 quirk_insert(const char *class, const char *instance
, const char *name
,
8715 uint32_t quirk
, int ws
)
8719 bool failed
= false;
8721 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: class: %s, instance: %s, name: %s,"
8722 " value: %u, ws: %d\n", class, instance
, name
, quirk
, ws
);
8724 if ((qp
= malloc(sizeof *qp
)) == NULL
)
8725 err(1, "quirk_insert: malloc");
8727 if ((qp
->class = strdup(class)) == NULL
)
8728 err(1, "quirk_insert: strdup");
8729 if ((qp
->instance
= strdup(instance
)) == NULL
)
8730 err(1, "quirk_insert: strdup");
8731 if ((qp
->name
= strdup(name
)) == NULL
)
8732 err(1, "quirk_insert: strdup");
8734 if (asprintf(&str
, "^%s$", class) == -1)
8735 err(1, "quirk_insert: asprintf");
8736 if (regcomp(&qp
->regex_class
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8737 add_startup_exception("regex failed to compile quirk 'class' "
8738 "field: %s", class);
8741 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8744 if (asprintf(&str
, "^%s$", instance
) == -1)
8745 err(1, "quirk_insert: asprintf");
8746 if (regcomp(&qp
->regex_instance
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8747 add_startup_exception("regex failed to compile quirk 'instance'"
8748 " field: %s", instance
);
8751 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8754 if (asprintf(&str
, "^%s$", name
) == -1)
8755 err(1, "quirk_insert: asprintf");
8756 if (regcomp(&qp
->regex_name
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8757 add_startup_exception("regex failed to compile quirk 'name' "
8761 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8765 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: regex error; skipping.\n");
8770 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
8772 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
8776 quirk_remove(struct quirk
*qp
)
8778 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%u]\n", qp
->class,
8779 qp
->name
, qp
->quirk
);
8781 TAILQ_REMOVE(&quirks
, qp
, entry
);
8784 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
8788 quirk_free(struct quirk
*qp
)
8790 regfree(&qp
->regex_class
);
8791 regfree(&qp
->regex_instance
);
8792 regfree(&qp
->regex_name
);
8804 while ((qp
= TAILQ_FIRST(&quirks
)) != NULL
) {
8810 quirk_replace(struct quirk
*qp
, const char *class, const char *instance
,
8811 const char *name
, uint32_t quirk
, int ws
)
8813 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp
->class,
8814 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
8817 quirk_insert(class, instance
, name
, quirk
, ws
);
8819 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
8823 setquirk(const char *class, const char *instance
, const char *name
,
8824 uint32_t quirk
, int ws
)
8828 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
8829 instance
, name
, quirk
, ws
);
8831 /* Remove/replace existing quirk. */
8832 TAILQ_FOREACH(qp
, &quirks
, entry
) {
8833 if (strcmp(qp
->class, class) == 0 &&
8834 strcmp(qp
->instance
, instance
) == 0 &&
8835 strcmp(qp
->name
, name
) == 0) {
8836 if (quirk
== 0 && ws
== -1)
8839 quirk_replace(qp
, class, instance
, name
, quirk
,
8841 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8846 /* Only insert if quirk is not NONE or forced ws is set. */
8847 if (quirk
|| ws
!= -1)
8848 quirk_insert(class, instance
, name
, quirk
, ws
);
8850 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8853 /* Eat '\' in str used to escape square brackets and colon. */
8855 unescape_selector(char *str
)
8859 for (cp
= str
; *str
!= '\0'; ++str
, ++cp
) {
8860 if (*str
== '\\' && (*(str
+ 1) == ':' || *(str
+ 1) == ']' ||
8870 setconfquirk(const char *selector
, const char *value
, int flags
)
8872 char *str
, *cp
, *class;
8873 char *instance
= NULL
, *name
= NULL
;
8874 int retval
, count
= 0, ws
= -1;
8877 /* suppress unused warning since var is needed */
8880 if (selector
== NULL
|| strlen(selector
) == 0)
8883 if ((str
= strdup(selector
)) == NULL
)
8884 err(1, "setconfquirk: strdup");
8886 /* Find non-escaped colon. */
8893 for (++cp
; *cp
!= '\0'; ++cp
) {
8894 if (*cp
== ':' && *(cp
- 1) != '\\') {
8900 unescape_selector(class);
8902 instance
= class + strlen(class) + 1;
8903 unescape_selector(instance
);
8909 name
= instance
+ strlen(instance
) + 1;
8910 unescape_selector(name
);
8915 DNPRINTF(SWM_D_CONF
, "setconfquirk: class: %s, instance: %s, "
8916 "name: %s\n", class, instance
, name
);
8918 if ((retval
= parsequirks(value
, &qrks
, &ws
)) == 0)
8919 setquirk(class, instance
, name
, qrks
, ws
);
8928 setquirk("MPlayer", "xv", ".*",
8929 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
, -1);
8930 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
8932 setquirk("Firefox-bin", "firefox-bin", ".*",
8934 setquirk("Firefox", "Dialog", ".*",
8936 setquirk("Gimp", "gimp", ".*",
8937 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8938 setquirk("XTerm", "xterm", ".*",
8939 SWM_Q_XTERM_FONTADJ
, -1);
8940 setquirk("xine", "Xine Window", ".*",
8941 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8942 setquirk("Xitk", "Xitk Combo", ".*",
8943 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8944 setquirk("xine", "xine Panel", ".*",
8945 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8946 setquirk("Xitk", "Xine Window", ".*",
8947 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8948 setquirk("xine", "xine Video Fullscreen Window", ".*",
8949 SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
, -1);
8950 setquirk("pcb", "pcb", ".*",
8952 setquirk("SDL_App", "SDL_App", ".*",
8953 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
, -1);
8956 /* conf file stuff */
8957 #define SWM_CONF_FILE "spectrwm.conf"
8958 #define SWM_CONF_FILE_OLD "scrotwm.conf"
8962 SWM_S_BAR_AT_BOTTOM
,
8963 SWM_S_BAR_BORDER_WIDTH
,
8966 SWM_S_BAR_ENABLED_WS
,
8971 SWM_S_BOUNDARY_WIDTH
,
8972 SWM_S_CLOCK_ENABLED
,
8975 SWM_S_CYCLE_VISIBLE
,
8977 SWM_S_DISABLE_BORDER
,
8979 SWM_S_FOCUS_CLOSE_WRAP
,
8980 SWM_S_FOCUS_DEFAULT
,
8982 SWM_S_ICONIC_ENABLED
,
8983 SWM_S_JAVA_WORKAROUND
,
8984 SWM_S_MAXIMIZE_HIDE_BAR
,
8985 SWM_S_REGION_PADDING
,
8990 SWM_S_STACK_ENABLED
,
8994 SWM_S_URGENT_COLLAPSE
,
8995 SWM_S_URGENT_ENABLED
,
8996 SWM_S_VERBOSE_LAYOUT
,
8999 SWM_S_WINDOW_CLASS_ENABLED
,
9000 SWM_S_WINDOW_INSTANCE_ENABLED
,
9001 SWM_S_WINDOW_NAME_ENABLED
,
9002 SWM_S_WORKSPACE_CLAMP
,
9003 SWM_S_WORKSPACE_LIMIT
,
9004 SWM_S_WORKSPACE_NAME
,
9008 setconfvalue(const char *selector
, const char *value
, int flags
)
9010 struct workspace
*ws
;
9011 int i
, ws_id
, num_screens
;
9012 char *b
, *str
, *sp
, s
[1024];
9015 case SWM_S_BAR_ACTION
:
9017 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
9018 err(1, "setconfvalue: bar_action");
9020 case SWM_S_BAR_AT_BOTTOM
:
9021 bar_at_bottom
= (atoi(value
) != 0);
9023 case SWM_S_BAR_BORDER_WIDTH
:
9024 bar_border_width
= atoi(value
);
9025 if (bar_border_width
< 0)
9026 bar_border_width
= 0;
9028 case SWM_S_BAR_DELAY
:
9029 /* No longer needed; leave to not break old conf files. */
9031 case SWM_S_BAR_ENABLED
:
9032 bar_enabled
= (atoi(value
) != 0);
9034 case SWM_S_BAR_ENABLED_WS
:
9035 ws_id
= atoi(selector
) - 1;
9036 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9037 errx(1, "setconfvalue: bar_enabled_ws: invalid "
9038 "workspace %d.", ws_id
+ 1);
9040 num_screens
= get_screen_count();
9041 for (i
= 0; i
< num_screens
; i
++) {
9042 ws
= (struct workspace
*)&screens
[i
].ws
;
9043 ws
[ws_id
].bar_enabled
= (atoi(value
) != 0);
9046 case SWM_S_BAR_FONT
:
9048 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
9049 err(1, "setconfvalue: asprintf: failed to allocate "
9050 "memory for bar_fonts.");
9053 /* If already in xft mode, then we are done. */
9054 if (!bar_font_legacy
)
9057 if ((sp
= str
= strdup(value
)) == NULL
)
9058 err(1, "setconfvalue: strdup");
9060 /* If there are any non-XLFD entries, switch to Xft mode. */
9061 while ((b
= strsep(&sp
, ",")) != NULL
) {
9065 bar_font_legacy
= false;
9072 case SWM_S_BAR_FORMAT
:
9074 if ((bar_format
= strdup(value
)) == NULL
)
9075 err(1, "setconfvalue: bar_format");
9077 case SWM_S_BAR_JUSTIFY
:
9078 if (strcmp(value
, "left") == 0)
9079 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
9080 else if (strcmp(value
, "center") == 0)
9081 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
9082 else if (strcmp(value
, "right") == 0)
9083 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
9085 errx(1, "invalid bar_justify");
9087 case SWM_S_BORDER_WIDTH
:
9088 border_width
= atoi(value
);
9089 if (border_width
< 0)
9092 case SWM_S_BOUNDARY_WIDTH
:
9093 boundary_width
= atoi(value
);
9094 if (boundary_width
< 0)
9097 case SWM_S_CLOCK_ENABLED
:
9098 clock_enabled
= (atoi(value
) != 0);
9100 case SWM_S_CLOCK_FORMAT
:
9101 #ifndef SWM_DENY_CLOCK_FORMAT
9103 if ((clock_format
= strdup(value
)) == NULL
)
9104 err(1, "setconfvalue: clock_format");
9107 case SWM_S_CYCLE_EMPTY
:
9108 cycle_empty
= (atoi(value
) != 0);
9110 case SWM_S_CYCLE_VISIBLE
:
9111 cycle_visible
= (atoi(value
) != 0);
9113 case SWM_S_DIALOG_RATIO
:
9114 dialog_ratio
= atof(value
);
9115 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
9118 case SWM_S_DISABLE_BORDER
:
9119 disable_border
= (atoi(value
) != 0);
9121 case SWM_S_FOCUS_CLOSE
:
9122 if (strcmp(value
, "first") == 0)
9123 focus_close
= SWM_STACK_BOTTOM
;
9124 else if (strcmp(value
, "last") == 0)
9125 focus_close
= SWM_STACK_TOP
;
9126 else if (strcmp(value
, "next") == 0)
9127 focus_close
= SWM_STACK_ABOVE
;
9128 else if (strcmp(value
, "previous") == 0)
9129 focus_close
= SWM_STACK_BELOW
;
9131 errx(1, "focus_close");
9133 case SWM_S_FOCUS_CLOSE_WRAP
:
9134 focus_close_wrap
= (atoi(value
) != 0);
9136 case SWM_S_FOCUS_DEFAULT
:
9137 if (strcmp(value
, "last") == 0)
9138 focus_default
= SWM_STACK_TOP
;
9139 else if (strcmp(value
, "first") == 0)
9140 focus_default
= SWM_STACK_BOTTOM
;
9142 errx(1, "focus_default");
9144 case SWM_S_FOCUS_MODE
:
9145 if (strcmp(value
, "default") == 0)
9146 focus_mode
= SWM_FOCUS_DEFAULT
;
9147 else if (strcmp(value
, "follow") == 0 ||
9148 strcmp(value
, "follow_cursor") == 0)
9149 focus_mode
= SWM_FOCUS_FOLLOW
;
9150 else if (strcmp(value
, "manual") == 0)
9151 focus_mode
= SWM_FOCUS_MANUAL
;
9153 errx(1, "focus_mode");
9155 case SWM_S_ICONIC_ENABLED
:
9156 iconic_enabled
= (atoi(value
) != 0);
9158 case SWM_S_JAVA_WORKAROUND
:
9159 java_workaround
= (atoi(value
) != 0);
9161 case SWM_S_MAXIMIZE_HIDE_BAR
:
9162 maximize_hide_bar
= atoi(value
);
9164 case SWM_S_REGION_PADDING
:
9165 region_padding
= atoi(value
);
9166 if (region_padding
< 0)
9169 case SWM_S_SPAWN_ORDER
:
9170 if (strcmp(value
, "first") == 0)
9171 spawn_position
= SWM_STACK_BOTTOM
;
9172 else if (strcmp(value
, "last") == 0)
9173 spawn_position
= SWM_STACK_TOP
;
9174 else if (strcmp(value
, "next") == 0)
9175 spawn_position
= SWM_STACK_ABOVE
;
9176 else if (strcmp(value
, "previous") == 0)
9177 spawn_position
= SWM_STACK_BELOW
;
9179 errx(1, "spawn_position");
9181 case SWM_S_SPAWN_TERM
:
9182 setconfspawn("term", value
, 0);
9183 setconfspawn("spawn_term", value
, 0);
9186 /* No longer needed; leave to not break old conf files. */
9188 case SWM_S_SS_ENABLED
:
9189 /* No longer needed; leave to not break old conf files. */
9191 case SWM_S_STACK_ENABLED
:
9192 stack_enabled
= (atoi(value
) != 0);
9194 case SWM_S_TERM_WIDTH
:
9195 term_width
= atoi(value
);
9199 case SWM_S_TILE_GAP
:
9200 tile_gap
= atoi(value
);
9202 case SWM_S_TRACK_PID_WS
:
9203 track_pid_ws
= atoi(value
);
9205 case SWM_S_URGENT_COLLAPSE
:
9206 urgent_collapse
= (atoi(value
) != 0);
9208 case SWM_S_URGENT_ENABLED
:
9209 urgent_enabled
= (atoi(value
) != 0);
9211 case SWM_S_VERBOSE_LAYOUT
:
9212 verbose_layout
= (atoi(value
) != 0);
9213 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
9215 layouts
[i
].l_string
= fancy_stacker
;
9217 layouts
[i
].l_string
= plain_stacker
;
9220 case SWM_S_WARP_FOCUS
:
9221 warp_focus
= (atoi(value
) != 0);
9223 case SWM_S_WARP_POINTER
:
9224 warp_pointer
= (atoi(value
) != 0);
9226 case SWM_S_WINDOW_CLASS_ENABLED
:
9227 window_class_enabled
= (atoi(value
) != 0);
9229 case SWM_S_WINDOW_INSTANCE_ENABLED
:
9230 window_instance_enabled
= (atoi(value
) != 0);
9232 case SWM_S_WINDOW_NAME_ENABLED
:
9233 window_name_enabled
= (atoi(value
) != 0);
9235 case SWM_S_WORKSPACE_CLAMP
:
9236 workspace_clamp
= (atoi(value
) != 0);
9238 case SWM_S_WORKSPACE_LIMIT
:
9239 workspace_limit
= atoi(value
);
9240 if (workspace_limit
> SWM_WS_MAX
)
9241 workspace_limit
= SWM_WS_MAX
;
9242 else if (workspace_limit
< 1)
9243 workspace_limit
= 1;
9245 ewmh_update_desktops();
9247 case SWM_S_WORKSPACE_NAME
:
9248 if (getenv("SWM_STARTED") != NULL
)
9252 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9253 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
9255 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9256 errx(1, "setconfvalue: workspace_name: invalid "
9257 "workspace %d.", ws_id
+ 1);
9259 num_screens
= get_screen_count();
9260 for (i
= 0; i
< num_screens
; ++i
) {
9261 ws
= (struct workspace
*)&screens
[i
].ws
;
9263 if (strlen(s
) > 0) {
9264 free(ws
[ws_id
].name
);
9265 if ((ws
[ws_id
].name
= strdup(s
)) == NULL
)
9266 err(1, "setconfvalue: workspace_name.");
9268 ewmh_update_desktop_names();
9269 ewmh_get_desktop_names();
9280 setconfmodkey(const char *selector
, const char *value
, int flags
)
9282 /* suppress unused warnings since vars are needed */
9286 if (strncasecmp(value
, "Mod1", strlen("Mod1")) == 0)
9287 update_modkey(XCB_MOD_MASK_1
);
9288 else if (strncasecmp(value
, "Mod2", strlen("Mod2")) == 0)
9289 update_modkey(XCB_MOD_MASK_2
);
9290 else if (strncasecmp(value
, "Mod3", strlen("Mod3")) == 0)
9291 update_modkey(XCB_MOD_MASK_3
);
9292 else if (strncasecmp(value
, "Mod4", strlen("Mod4")) == 0)
9293 update_modkey(XCB_MOD_MASK_4
);
9294 else if (strncasecmp(value
, "Mod5", strlen("Mod5")) == 0)
9295 update_modkey(XCB_MOD_MASK_5
);
9302 setconfcolor(const char *selector
, const char *value
, int flags
)
9304 int first
, last
, i
= 0, num_screens
;
9306 num_screens
= get_screen_count();
9308 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
9309 if (selector
== NULL
|| strlen(selector
) == 0 ||
9310 (last
= atoi(selector
) - 1) < 0) {
9312 last
= num_screens
- 1;
9317 if (last
>= num_screens
) {
9318 add_startup_exception("invalid screen index: %d out of bounds "
9319 "(maximum %d)", last
+ 1, num_screens
);
9323 for (i
= first
; i
<= last
; ++i
) {
9324 setscreencolor(value
, i
, flags
);
9327 * When setting focus/unfocus colors, we need to also
9328 * set maximize colors to match if they haven't been customized.
9330 if (flags
== SWM_S_COLOR_FOCUS
&&
9331 !screens
[i
].c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].manual
)
9332 setscreencolor(value
, i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
9333 else if (flags
== SWM_S_COLOR_UNFOCUS
&&
9334 !screens
[i
].c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].manual
)
9335 setscreencolor(value
, i
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
9337 screens
[i
].c
[flags
].manual
= 1;
9344 setconfregion(const char *selector
, const char *value
, int flags
)
9346 /* suppress unused warnings since vars are needed */
9350 custom_region(value
);
9355 setautorun(const char *selector
, const char *value
, int flags
)
9359 char *ap
, *sp
, *str
;
9365 /* suppress unused warnings since vars are needed */
9369 if (getenv("SWM_STARTED"))
9373 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9374 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
9376 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9377 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
9379 sp
= str
= expand_tilde((char *)&s
);
9382 * This is a little intricate
9384 * If the pid already exists we simply reuse it because it means it was
9385 * used before AND not claimed by manage_window. We get away with
9386 * altering it in the parent after INSERT because this can not be a race
9389 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
9392 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
9394 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
9395 err(1, "setautorun: realloc");
9396 a
.argv
[argc
- 1] = ap
;
9399 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
9400 err(1, "setautorun: realloc");
9401 a
.argv
[argc
] = NULL
;
9403 if ((pid
= fork()) == 0) {
9404 spawn(ws_id
, &a
, true);
9414 p
= calloc(1, sizeof *p
);
9417 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
9427 setlayout(const char *selector
, const char *value
, int flags
)
9429 struct workspace
*ws
;
9430 int ws_id
, i
, mg
, ma
, si
, ar
;
9431 int st
= SWM_V_STACK
, num_screens
;
9435 /* suppress unused warnings since vars are needed */
9440 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
9441 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
9442 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9443 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9446 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9447 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
9449 if (strcasecmp(s
, "vertical") == 0)
9451 else if (strcasecmp(s
, "vertical_flip") == 0) {
9454 } else if (strcasecmp(s
, "horizontal") == 0)
9456 else if (strcasecmp(s
, "horizontal_flip") == 0) {
9459 } else if (strcasecmp(s
, "fullscreen") == 0)
9462 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9463 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9468 initial_layouts
[ws_id
].configured
= true;
9469 initial_layouts
[ws_id
].layout
= st
;
9470 initial_layouts
[ws_id
].master_grow
= mg
;
9471 initial_layouts
[ws_id
].master_add
= ma
;
9472 initial_layouts
[ws_id
].stack_inc
= si
;
9473 initial_layouts
[ws_id
].always_raise
= ar
;
9474 initial_layouts
[ws_id
].apply_flip
= f
;
9476 num_screens
= get_screen_count();
9477 for (i
= 0; i
< num_screens
; ++i
) {
9478 ws
= &screens
[i
].ws
[ws_id
];
9486 initlayout(struct workspace
*ws
)
9492 if (!initial_layouts
[ws_id
].configured
)
9495 ws
->cur_layout
= &layouts
[initial_layouts
[ws_id
].layout
];
9497 ws
->cur_layout
->l_config(ws
, SWM_ARG_ID_STACKINIT
);
9499 ws
->always_raise
= (initial_layouts
[ws_id
].always_raise
!= 0);
9502 for (i
= 0; i
< abs(initial_layouts
[ws_id
].master_grow
); i
++) {
9503 ws
->cur_layout
->l_config(ws
,
9504 initial_layouts
[ws_id
].master_grow
>= 0 ? SWM_ARG_ID_MASTERGROW
:
9505 SWM_ARG_ID_MASTERSHRINK
);
9508 for (i
= 0; i
< abs(initial_layouts
[ws_id
].master_add
); i
++) {
9509 ws
->cur_layout
->l_config(ws
,
9510 initial_layouts
[ws_id
].master_add
>= 0 ? SWM_ARG_ID_MASTERADD
:
9511 SWM_ARG_ID_MASTERDEL
);
9514 for (i
= 0; i
< abs(initial_layouts
[ws_id
].stack_inc
); i
++) {
9515 ws
->cur_layout
->l_config(ws
,
9516 initial_layouts
[ws_id
].stack_inc
>= 0 ? SWM_ARG_ID_STACKINC
:
9517 SWM_ARG_ID_STACKDEC
);
9520 if (initial_layouts
[ws_id
].apply_flip
) {
9521 ws
->cur_layout
->l_config(ws
,
9522 SWM_ARG_ID_FLIPLAYOUT
);
9526 /* config options */
9527 struct config_option
{
9529 int (*func
)(const char*, const char*, int);
9532 struct config_option configopt
[] = {
9533 { "autorun", setautorun
, 0 },
9534 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
9535 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
9536 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
9537 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
9538 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
9539 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
9540 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
9541 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
9542 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
9543 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
9544 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
9545 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
9546 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
9547 { "bind", setconfbinding
, 0 },
9548 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
9549 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
9550 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
9551 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
9552 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
9553 { "color_focus_maximized", setconfcolor
, SWM_S_COLOR_FOCUS_MAXIMIZED
},
9554 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
9555 { "color_unfocus_maximized", setconfcolor
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
},
9556 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
9557 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
9558 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
9559 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
9560 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
9561 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
9562 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
9563 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
9564 { "iconic_enabled", setconfvalue
, SWM_S_ICONIC_ENABLED
},
9565 { "java_workaround", setconfvalue
, SWM_S_JAVA_WORKAROUND
},
9566 { "keyboard_mapping", setkeymapping
, 0 },
9567 { "layout", setlayout
, 0 },
9568 { "maximize_hide_bar", setconfvalue
, SWM_S_MAXIMIZE_HIDE_BAR
},
9569 { "modkey", setconfmodkey
, 0 },
9570 { "program", setconfspawn
, 0 },
9571 { "quirk", setconfquirk
, 0 },
9572 { "region", setconfregion
, 0 },
9573 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
9574 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
9575 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
9576 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
9577 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
9578 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
9579 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
9580 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
9581 { "title_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
}, /* For backwards compat. */
9582 { "title_name_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
}, /* For backwards compat. */
9583 { "track_pid_ws", setconfvalue
, SWM_S_TRACK_PID_WS
},
9584 { "urgent_collapse", setconfvalue
, SWM_S_URGENT_COLLAPSE
},
9585 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
9586 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
9587 { "warp_focus", setconfvalue
, SWM_S_WARP_FOCUS
},
9588 { "warp_pointer", setconfvalue
, SWM_S_WARP_POINTER
},
9589 { "window_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
},
9590 { "window_instance_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
},
9591 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
9592 { "workspace_clamp", setconfvalue
, SWM_S_WORKSPACE_CLAMP
},
9593 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
9594 { "name", setconfvalue
, SWM_S_WORKSPACE_NAME
},
9598 _add_startup_exception(const char *fmt
, va_list ap
)
9600 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
9601 warn("%s: asprintf", __func__
);
9605 add_startup_exception(const char *fmt
, ...)
9611 if (startup_exception
)
9614 /* force bar to be enabled due to exception */
9618 _add_startup_exception(fmt
, ap
);
9623 conf_load(const char *filename
, int keymapping
)
9626 char *line
= NULL
, *cp
, *ce
, *optsub
, *optval
= NULL
;
9627 size_t linelen
, lineno
= 0;
9628 int wordlen
, i
, optidx
, count
;
9629 struct config_option
*opt
= NULL
;
9631 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
9633 if (filename
== NULL
) {
9634 warnx("conf_load: no filename");
9638 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
9640 if ((config
= fopen(filename
, "r")) == NULL
) {
9641 warn("conf_load: fopen: %s", filename
);
9645 while (!feof(config
)) {
9649 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
9650 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
9652 err(1, "%s", filename
);
9657 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9658 if (cp
[0] == '\0') {
9662 /* get config option */
9663 wordlen
= strcspn(cp
, "=[ \t\n");
9665 add_startup_exception("%s: line %zd: no option found",
9670 for (i
= 0; i
< LENGTH(configopt
); i
++) {
9671 opt
= &configopt
[i
];
9672 if (strncasecmp(cp
, opt
->optname
, wordlen
) == 0 &&
9673 (int)strlen(opt
->optname
) == wordlen
) {
9679 add_startup_exception("%s: line %zd: unknown option "
9680 "%.*s", filename
, lineno
, wordlen
, cp
);
9683 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
9684 add_startup_exception("%s: line %zd: invalid option "
9685 "%.*s", filename
, lineno
, wordlen
, cp
);
9689 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9691 /* get [selector] if any */
9696 /* Get length of selector. */
9697 for (ce
= ++cp
; *ce
!= '\0'; ++ce
) {
9698 /* Find matching (not escaped) bracket. */
9699 if (*ce
== ']' && *(ce
- 1) != '\\') {
9706 add_startup_exception("%s: line %zd: syntax "
9707 "error: unterminated selector", filename
,
9712 /* ce points at ']'; terminate optsub. */
9717 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
9720 if (strlen(optval
) == 0) {
9721 add_startup_exception("%s: line %zd: must supply value "
9722 "to %s", filename
, lineno
,
9723 configopt
[optidx
].optname
);
9726 /* call function to deal with it all */
9727 if (configopt
[optidx
].func(optsub
, optval
,
9728 configopt
[optidx
].funcflags
) != 0) {
9729 add_startup_exception("%s: line %zd: invalid data for "
9730 "%s", filename
, lineno
, configopt
[optidx
].optname
);
9739 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
9745 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
9747 struct ws_win
*parent
, *w
;
9748 struct swm_region
*r
;
9749 struct workspace
*ws
;
9750 xcb_icccm_wm_hints_t wmh
;
9752 parent
= find_window(win
->transient
);
9754 parent
->focus_child
= win
;
9756 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
9757 " for %#x trans %#x\n", win
->id
, win
->transient
);
9759 r
= root_to_region(win
->s
->root
, SWM_CK_ALL
);
9761 /* parent doen't exist in our window list */
9762 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
9763 if (xcb_icccm_get_wm_hints_reply(conn
,
9764 xcb_icccm_get_wm_hints(conn
, w
->id
),
9766 warnx("can't get hints for %#x", w
->id
);
9770 if (win
->hints
.window_group
!= wmh
.window_group
)
9773 w
->focus_child
= win
;
9774 win
->transient
= w
->id
;
9776 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
9777 "transient to %#x\n", win
->transient
);
9784 window_get_pid(xcb_window_t win
)
9789 xcb_get_property_cookie_t pc
;
9790 xcb_get_property_reply_t
*pr
;
9792 apid
= get_atom_from_string("_NET_WM_PID");
9793 if (apid
== XCB_ATOM_NONE
)
9796 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
9797 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9800 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
9805 if (pr
->type
== apid
&& pr
->format
== 32)
9806 ret
= *((pid_t
*)xcb_get_property_value(pr
));
9812 apid
= get_atom_from_string("_SWM_PID");
9813 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
9815 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9818 if (pr
->type
!= apid
) {
9823 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
9830 get_swm_ws(xcb_window_t id
)
9836 xcb_get_property_reply_t
*gpr
;
9838 gpr
= xcb_get_property_reply(conn
,
9839 xcb_get_property(conn
, 0, id
, a_swm_ws
,
9840 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
9845 proplen
= xcb_get_property_value_length(gpr
);
9847 prop
= malloc(proplen
+ 1);
9850 xcb_get_property_value(gpr
),
9852 prop
[proplen
] = '\0';
9859 DNPRINTF(SWM_D_PROP
, "get_swm_ws: _SWM_WS: %s\n", prop
);
9860 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
9862 DNPRINTF(SWM_D_PROP
, "get_swm_ws: win #%s: %s",
9872 get_ws_idx(struct ws_win
*win
)
9874 xcb_get_property_reply_t
*gpr
;
9880 gpr
= xcb_get_property_reply(conn
,
9881 xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
9882 XCB_ATOM_CARDINAL
, 0, 1),
9885 if (gpr
->type
== XCB_ATOM_CARDINAL
&& gpr
->format
== 32)
9886 ws_idx
= *((int *)xcb_get_property_value(gpr
));
9890 if (ws_idx
== -1 && !(win
->quirks
& SWM_Q_IGNORESPAWNWS
))
9891 ws_idx
= get_swm_ws(win
->id
);
9893 if (ws_idx
> workspace_limit
- 1 || ws_idx
< -1)
9896 DNPRINTF(SWM_D_PROP
, "get_ws_idx: win %#x, ws_idx: %d\n", win
->id
,
9903 reparent_window(struct ws_win
*win
)
9905 xcb_void_cookie_t c
;
9906 xcb_generic_error_t
*error
;
9909 win
->frame
= xcb_generate_id(conn
);
9911 DNPRINTF(SWM_D_MISC
, "reparent_window: win %#x, frame: %#x\n",
9912 win
->id
, win
->frame
);
9915 XCB_EVENT_MASK_ENTER_WINDOW
|
9916 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
9917 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
9918 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
|
9919 XCB_EVENT_MASK_EXPOSURE
;
9921 wa
[0] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
9924 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
->frame
, win
->s
->root
,
9925 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), 0,
9926 XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
9927 XCB_CW_EVENT_MASK
, wa
);
9929 win
->state
= SWM_WIN_STATE_REPARENTING
;
9930 c
= xcb_reparent_window_checked(conn
, win
->id
, win
->frame
, 0, 0);
9931 if ((error
= xcb_request_check(conn
, c
))) {
9932 DNPRINTF(SWM_D_MISC
, "reparent_window: error:\n");
9937 xcb_destroy_window(conn
, win
->frame
);
9938 win
->frame
= XCB_WINDOW_NONE
;
9940 xcb_change_save_set(conn
, XCB_SET_MODE_INSERT
, win
->id
);
9942 DNPRINTF(SWM_D_MISC
, "reparent_window: done.\n");
9946 unparent_window(struct ws_win
*win
)
9948 xcb_change_save_set(conn
, XCB_SET_MODE_DELETE
, win
->id
);
9949 xcb_reparent_window(conn
, win
->id
, win
->s
->root
, X(win
), Y(win
));
9950 xcb_destroy_window(conn
, win
->frame
);
9951 win
->frame
= XCB_WINDOW_NONE
;
9952 win
->state
= SWM_WIN_STATE_UNPARENTING
;
9956 manage_window(xcb_window_t id
, int spawn_pos
, bool mapping
)
9958 struct ws_win
*win
= NULL
, *ww
;
9959 struct swm_region
*r
;
9962 xcb_get_geometry_reply_t
*gr
;
9963 xcb_get_window_attributes_reply_t
*war
= NULL
;
9964 xcb_window_t trans
= XCB_WINDOW_NONE
;
9965 uint32_t i
, wa
[1], new_flags
;
9966 int ws_idx
, force_ws
= -1;
9967 char *class, *instance
, *name
;
9970 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region bar; "
9975 if (find_region(id
)) {
9976 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region window; "
9981 if ((win
= find_window(id
)) != NULL
) {
9982 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x already "
9983 "managed; skipping.)\n", id
);
9984 return (win
); /* Already managed. */
9987 /* See if window is on the unmanaged list. */
9988 if ((win
= find_unmanaged_window(id
)) != NULL
) {
9989 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x found on "
9990 "unmanaged list.\n", id
);
9991 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
9993 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is new.\n", id
);
9996 war
= xcb_get_window_attributes_reply(conn
,
9997 xcb_get_window_attributes(conn
, id
), NULL
);
9999 DNPRINTF(SWM_D_EVENT
, "manage_window: window lost.\n");
10003 if (war
->override_redirect
) {
10004 DNPRINTF(SWM_D_EVENT
, "manage_window: override_redirect; "
10009 if (!mapping
&& war
->map_state
== XCB_MAP_STATE_UNMAPPED
&&
10010 get_win_state(id
) == XCB_ICCCM_WM_STATE_WITHDRAWN
) {
10011 DNPRINTF(SWM_D_EVENT
, "manage_window: window withdrawn; "
10016 /* Try to get initial window geometry. */
10017 gr
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, id
), NULL
);
10019 DNPRINTF(SWM_D_MISC
, "manage_window: get geometry failed.\n");
10023 /* Figure out which region the window belongs to. */
10024 r
= root_to_region(gr
->root
, SWM_CK_ALL
);
10026 /* Handle special windows with special _NET_WM_WINDOW_TYPE */
10027 if (ewmh_handle_special_types(id
, r
)) {
10028 DNPRINTF(SWM_D_EVENT
, "manage_window: "
10029 "unmanaged ewmh window type\n");
10034 /* Create and initialize ws_win object. */
10035 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
10036 err(1, "manage_window: calloc: failed to allocate "
10037 "memory for new window");
10042 /* Ignore window border if there is one. */
10043 WIDTH(win
) = gr
->width
;
10044 HEIGHT(win
) = gr
->height
;
10045 X(win
) = gr
->x
+ gr
->border_width
;
10046 Y(win
) = gr
->y
+ gr
->border_width
;
10047 win
->bordered
= false;
10048 win
->mapped
= (war
->map_state
!= XCB_MAP_STATE_UNMAPPED
);
10049 win
->s
= r
->s
; /* this never changes */
10053 /* Select which X events to monitor and set border pixel color. */
10054 wa
[0] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
10055 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
10057 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_EVENT_MASK
, wa
);
10059 /* Get WM_SIZE_HINTS. */
10060 xcb_icccm_get_wm_normal_hints_reply(conn
,
10061 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
10064 /* Get WM_HINTS. */
10065 xcb_icccm_get_wm_hints_reply(conn
,
10066 xcb_icccm_get_wm_hints(conn
, win
->id
),
10067 &win
->hints
, NULL
);
10069 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
10070 xcb_icccm_get_wm_transient_for_reply(conn
,
10071 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
10074 win
->transient
= trans
;
10075 set_child_transient(win
, &win
->transient
);
10078 /* Get WM_PROTOCOLS. */
10079 get_wm_protocols(win
);
10082 /* Must be after getting WM_HINTS and WM_PROTOCOLS. */
10083 DNPRINTF(SWM_D_FOCUS
, "manage_window: input_model: %s\n",
10084 get_win_input_model(win
));
10087 /* Set initial quirks based on EWMH. */
10088 ewmh_autoquirk(win
);
10090 /* Determine initial quirks. */
10091 xcb_icccm_get_wm_class_reply(conn
,
10092 xcb_icccm_get_wm_class(conn
, win
->id
),
10095 class = win
->ch
.class_name
? win
->ch
.class_name
: "";
10096 instance
= win
->ch
.instance_name
? win
->ch
.instance_name
: "";
10097 name
= get_win_name(win
->id
);
10099 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, instance: %s, "
10100 "name: %s\n", class, instance
, name
);
10102 /* java is retarded so treat it special */
10103 if (strstr(instance
, "sun-awt")) {
10104 DNPRINTF(SWM_D_CLASS
, "manage_window: java window detected.\n");
10108 TAILQ_FOREACH(qp
, &quirks
, entry
) {
10109 if (regexec(&qp
->regex_class
, class, 0, NULL
, 0) == 0 &&
10110 regexec(&qp
->regex_instance
, instance
, 0, NULL
, 0) == 0 &&
10111 regexec(&qp
->regex_name
, name
, 0, NULL
, 0) == 0) {
10112 DNPRINTF(SWM_D_CLASS
, "manage_window: matched "
10113 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp
->class,
10114 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
10115 win
->quirks
= qp
->quirk
;
10116 if (qp
->ws
>= 0 && qp
->ws
< workspace_limit
)
10123 /* Reset font sizes (the bruteforce way; no default keybinding). */
10124 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
10125 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
10126 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
10127 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
10128 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
10131 /* Figure out which workspace the window belongs to. */
10132 if (!(win
->quirks
& SWM_Q_IGNOREPID
) &&
10133 (p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
10134 win
->ws
= &r
->s
->ws
[p
->ws
];
10135 TAILQ_REMOVE(&pidlist
, p
, entry
);
10138 } else if ((ws_idx
= get_ws_idx(win
)) != -1 &&
10140 /* _SWM_WS is set; use that. */
10141 win
->ws
= &r
->s
->ws
[ws_idx
];
10142 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
10143 /* Launch transients in the same ws as parent. */
10149 if (force_ws
!= -1)
10150 win
->ws
= &r
->s
->ws
[force_ws
];
10152 /* Set the _NET_WM_DESKTOP atom. */
10153 DNPRINTF(SWM_D_PROP
, "manage_window: set _NET_WM_DESKTOP: %d\n",
10155 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
10156 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &win
->ws
->idx
);
10158 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
10159 xcb_delete_property(conn
, win
->id
, a_swm_ws
);
10161 /* WS must already be set for this to work. */
10162 store_float_geom(win
);
10164 /* Make sure window is positioned inside its region, if its active. */
10166 region_containment(win
, r
, SWM_CW_ALLSIDES
|
10167 SWM_CW_HARDBOUNDARY
);
10168 update_window(win
);
10171 /* Figure out where to insert the window in the workspace list. */
10172 if (trans
&& (ww
= find_window(trans
)))
10173 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
10174 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_ABOVE
)
10175 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
10177 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_BELOW
)
10178 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
10179 else switch (spawn_pos
) {
10181 case SWM_STACK_TOP
:
10182 case SWM_STACK_ABOVE
:
10183 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
10185 case SWM_STACK_BOTTOM
:
10186 case SWM_STACK_BELOW
:
10187 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
10190 ewmh_update_client_list();
10192 TAILQ_INSERT_HEAD(&win
->ws
->stack
, win
, stack_entry
);
10195 /* Get/apply initial _NET_WM_STATE */
10196 ewmh_get_wm_state(win
);
10198 /* Apply quirks. */
10199 new_flags
= win
->ewmh_flags
;
10201 if (win
->quirks
& SWM_Q_FLOAT
)
10202 new_flags
|= EWMH_F_ABOVE
;
10204 if (win
->quirks
& SWM_Q_ANYWHERE
)
10205 new_flags
|= SWM_F_MANUAL
;
10207 ewmh_apply_flags(win
, new_flags
);
10208 ewmh_update_wm_state(win
);
10210 /* Set initial _NET_WM_ALLOWED_ACTIONS */
10211 ewmh_update_actions(win
);
10213 reparent_window(win
);
10215 DNPRINTF(SWM_D_MISC
, "manage_window: done. win %#x, (x,y) w x h: "
10216 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win
->id
,
10217 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
10218 YESNO(ICONIC(win
)), win
->transient
);
10225 free_window(struct ws_win
*win
)
10227 DNPRINTF(SWM_D_MISC
, "free_window: win %#x\n", WINID(win
));
10232 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
10235 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
10238 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
10242 unmanage_window(struct ws_win
*win
)
10244 DNPRINTF(SWM_D_MISC
, "unmanage_window: win %#x\n", WINID(win
));
10250 unparent_window(win
);
10252 TAILQ_REMOVE(&win
->ws
->stack
, win
, stack_entry
);
10253 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
10254 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
10256 ewmh_update_client_list();
10260 expose(xcb_expose_event_t
*e
)
10265 struct workspace
*ws
;
10268 DNPRINTF(SWM_D_EVENT
, "expose: win %#x, count: %d\n", e
->window
,
10274 if ((b
= find_bar(e
->window
))) {
10277 } else if ((w
= find_window(e
->window
)) && w
->frame
== e
->window
) {
10281 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
10287 DNPRINTF(SWM_D_EVENT
, "expose: done\n");
10291 focusin(xcb_focus_in_event_t
*e
)
10293 struct ws_win
*win
;
10295 DNPRINTF(SWM_D_EVENT
, "focusin: win %#x, mode: %s(%u), "
10296 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10297 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10298 if ((win
= find_window(e
->event
)) && win
!= win
->ws
->focus
&&
10299 win
!= win
->ws
->focus_pending
&&
10300 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10301 win
->ws
->focus_prev
= win
->ws
->focus
;
10302 win
->ws
->focus
= win
;
10303 win
->ws
->focus_pending
= NULL
;
10305 if (win
->ws
->focus_prev
)
10306 draw_frame(win
->ws
->focus_prev
);
10314 focusout(xcb_focus_out_event_t
*e
)
10316 DNPRINTF(SWM_D_EVENT
, "focusout: win %#x, mode: %s(%u), "
10317 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10318 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10323 keypress(xcb_key_press_event_t
*e
)
10326 struct binding
*bp
;
10327 xcb_keysym_t keysym
;
10328 bool replay
= true;
10330 last_event_time
= e
->time
;
10332 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
10334 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
10335 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10336 "state: %u, cleaned: %u, same_screen: %s\n", keysym
, e
->event
,
10337 e
->event_x
, e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
,
10338 e
->root_y
, e
->child
, e
->state
, CLEANMASK(e
->state
),
10339 YESNO(e
->same_screen
));
10341 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10343 /* Look for catch-all. */
10344 if ((bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
)) == NULL
)
10348 replay
= bp
->flags
& BINDING_F_REPLAY
;
10350 if ((ap
= &actions
[bp
->action
]) == NULL
)
10353 if (bp
->action
== FN_SPAWN_CUSTOM
)
10354 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10357 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10359 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10363 DNPRINTF(SWM_D_EVENT
, "keypress: replaying.\n");
10364 /* Pass keypress to event window and unfreeze keyboard queue. */
10365 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10367 /* Release freeze. */
10368 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10372 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
10376 keyrelease(xcb_key_release_event_t
*e
)
10379 struct binding
*bp
;
10380 xcb_keysym_t keysym
;
10382 last_event_time
= e
->time
;
10384 keysym
= xcb_key_release_lookup_keysym(syms
, e
, 0);
10386 DNPRINTF(SWM_D_EVENT
, "keyrelease: keysym: %u, win (x,y): %#x (%d,%d), "
10387 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10388 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
10389 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
10390 e
->child
, e
->state
, YESNO(e
->same_screen
));
10392 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10394 /* Look for catch-all. */
10395 bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
);
10397 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10398 bp
->flags
& BINDING_F_REPLAY
) {
10399 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10400 DNPRINTF(SWM_D_EVENT
, "keyrelease: replaying.\n");
10402 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10403 DNPRINTF(SWM_D_EVENT
, "keyrelease: unfreezing.\n");
10408 DNPRINTF(SWM_D_EVENT
, "keyrelease: done.\n");
10412 buttonpress(xcb_button_press_event_t
*e
)
10414 struct ws_win
*win
= NULL
, *newf
;
10415 struct swm_region
*r
, *old_r
;
10417 struct binding
*bp
;
10418 bool replay
= true;
10420 last_event_time
= e
->time
;
10422 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): %#x (%d,%d), "
10423 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10424 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10425 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10426 e
->state
, YESNO(e
->same_screen
));
10428 if (e
->event
== e
->root
) {
10430 win
= find_window(e
->child
);
10432 /* Focus on empty region */
10433 /* If no windows on region if its empty. */
10434 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10435 if (r
&& TAILQ_EMPTY(&r
->ws
->winlist
)) {
10436 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
10437 if (old_r
&& old_r
!= r
)
10438 unfocus_win(old_r
->ws
->focus
);
10440 DNPRINTF(SWM_D_FOCUS
, "buttonpress: "
10441 "set_input_focus: %#x, revert-to: parent, "
10442 "time: %#x\n", e
->root
, e
->time
);
10443 xcb_set_input_focus(conn
,
10444 XCB_INPUT_FOCUS_POINTER_ROOT
,
10447 /* Clear bar since empty. */
10450 /* No need to replay event. */
10455 win
= find_window(e
->event
);
10459 newf
= get_focus_magic(win
);
10460 if (win
->ws
->focus
== newf
&& newf
!= win
) {
10461 if (win
->focus_child
== win
)
10462 win
->focus_child
= NULL
;
10468 /* Handle any bound action. */
10469 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10471 /* Look for catch-all. */
10472 if ((bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
)) == NULL
)
10477 replay
= bp
->flags
& BINDING_F_REPLAY
;
10479 if ((ap
= &actions
[bp
->action
]) == NULL
)
10482 if (bp
->action
== FN_SPAWN_CUSTOM
)
10483 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10486 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10488 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10492 DNPRINTF(SWM_D_EVENT
, "buttonpress: replaying.\n");
10493 /* Replay event to event window */
10494 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10496 /* Unfreeze grab events. */
10497 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10504 buttonrelease(xcb_button_release_event_t
*e
)
10507 struct binding
*bp
;
10509 last_event_time
= e
->time
;
10511 DNPRINTF(SWM_D_EVENT
, "buttonrelease: win (x,y): %#x (%d,%d), "
10512 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10513 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10514 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10515 e
->state
, YESNO(e
->same_screen
));
10517 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10519 /* Look for catch-all. */
10520 bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
);
10522 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10523 bp
->flags
& BINDING_F_REPLAY
) {
10524 DNPRINTF(SWM_D_EVENT
, "buttonrelease: replaying.\n");
10525 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10527 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10535 get_win_input_model(struct ws_win
*win
)
10539 * Input Model Input Field WM_TAKE_FOCUS
10540 * No Input False Absent
10541 * Passive True Absent
10542 * Locally Active True Present
10543 * Globally Active False Present
10546 if (ACCEPTS_FOCUS(win
))
10547 inputmodel
= (win
->take_focus
) ? "Locally Active" : "Passive";
10549 inputmodel
= (win
->take_focus
) ? "Globally Active" : "No Input";
10555 print_win_geom(xcb_window_t w
)
10557 xcb_get_geometry_reply_t
*wa
;
10559 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
10561 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: %#x\n",
10566 DNPRINTF(SWM_D_MISC
, "print_win_geom: win %#x, root: %#x, "
10567 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
10568 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
10577 get_stack_mode_name(uint8_t mode
)
10582 case XCB_STACK_MODE_ABOVE
:
10585 case XCB_STACK_MODE_BELOW
:
10588 case XCB_STACK_MODE_TOP_IF
:
10591 case XCB_STACK_MODE_BOTTOM_IF
:
10594 case XCB_STACK_MODE_OPPOSITE
:
10606 configurerequest(xcb_configure_request_event_t
*e
)
10608 struct ws_win
*win
;
10609 struct swm_region
*r
= NULL
;
10611 uint32_t wc
[7] = {0};
10615 if ((win
= find_window(e
->window
)) == NULL
)
10616 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10620 if (swm_debug
& SWM_D_EVENT
) {
10621 print_win_geom(e
->window
);
10623 DNPRINTF(SWM_D_EVENT
, "configurerequest: win %#x, "
10624 "parent: %#x, new: %s, value_mask: %u { ", e
->window
,
10625 e
->parent
, YESNO(new), e
->value_mask
);
10626 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
10627 DPRINTF("X: %d ", e
->x
);
10628 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
10629 DPRINTF("Y: %d ", e
->y
);
10630 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10631 DPRINTF("W: %u ", e
->width
);
10632 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10633 DPRINTF("H: %u ", e
->height
);
10634 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
10635 DPRINTF("Border: %u ", e
->border_width
);
10636 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
10637 DPRINTF("Sibling: %#x ", e
->sibling
);
10638 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
10639 DPRINTF("StackMode: %s(%u) ",
10640 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
10646 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10647 mask
|= XCB_CONFIG_WINDOW_X
;
10650 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10651 mask
|= XCB_CONFIG_WINDOW_Y
;
10654 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
10655 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
10656 wc
[i
++] = e
->width
;
10658 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
10659 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
10660 wc
[i
++] = e
->height
;
10662 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
10663 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
10664 wc
[i
++] = e
->border_width
;
10666 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
10667 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
10668 wc
[i
++] = e
->sibling
;
10670 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
10671 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
10672 wc
[i
++] = e
->stack_mode
;
10676 xcb_configure_window(conn
, e
->window
, mask
, wc
);
10679 } else if ((!MANUAL(win
) || win
->quirks
& SWM_Q_ANYWHERE
) &&
10680 !FULLSCREEN(win
) && !MAXIMIZED(win
)) {
10683 else if (win
->ws
->old_r
)
10684 r
= win
->ws
->old_r
;
10686 /* windows are centered unless ANYWHERE quirk is set. */
10687 if (win
->quirks
& SWM_Q_ANYWHERE
) {
10688 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10689 win
->g_float
.x
= e
->x
;
10691 win
->g_float
.x
-= X(r
);
10694 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10695 win
->g_float
.y
= e
->y
;
10697 win
->g_float
.y
-= Y(r
);
10701 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10702 win
->g_float
.w
= e
->width
;
10704 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10705 win
->g_float
.h
= e
->height
;
10707 win
->g_floatvalid
= true;
10709 if (!MAXIMIZED(win
) && !FULLSCREEN(win
) &&
10710 (TRANS(win
) || (ABOVE(win
) &&
10711 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]))) {
10712 WIDTH(win
) = win
->g_float
.w
;
10713 HEIGHT(win
) = win
->g_float
.h
;
10716 update_floater(win
);
10719 config_win(win
, e
);
10723 config_win(win
, e
);
10727 config_win(win
, e
);
10731 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
10735 configurenotify(xcb_configure_notify_event_t
*e
)
10737 struct ws_win
*win
;
10739 DNPRINTF(SWM_D_EVENT
, "configurenotify: win %#x, event win: %#x, "
10740 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
10741 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
10742 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
10743 YESNO(e
->override_redirect
));
10745 win
= find_window(e
->window
);
10748 if (font_adjusted
&& win
->ws
->r
) {
10756 destroynotify(xcb_destroy_notify_event_t
*e
)
10758 struct ws_win
*win
;
10759 struct workspace
*ws
;
10761 DNPRINTF(SWM_D_EVENT
, "destroynotify: win %#x\n", e
->window
);
10763 if ((win
= find_window(e
->window
)) == NULL
) {
10764 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10766 /* Window is on unmanaged list. */
10767 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10774 if (win
->frame
== e
->window
) {
10775 DNPRINTF(SWM_D_EVENT
, "destroynotify: frame for win %#x\n",
10777 win
->frame
= XCB_WINDOW_NONE
;
10781 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10782 /* If we were focused, make sure we focus on something else. */
10783 if (win
== ws
->focus
) {
10784 ws
->focus_pending
= get_focus_prev(win
);
10785 if (ws
->focus_pending
== win
)
10786 ws
->focus_pending
= NULL
;
10790 unmanage_window(win
);
10791 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10795 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
10796 if (ws
->focus_pending
) {
10797 focus_win(ws
->focus_pending
);
10798 ws
->focus_pending
= NULL
;
10799 } else if (ws
->focus
== NULL
) {
10800 DNPRINTF(SWM_D_FOCUS
, "destroynotify: set_input_focus: "
10801 "%#x, revert-to: parent, time: 0\n", ws
->r
->id
);
10802 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
10803 ws
->r
->id
, XCB_CURRENT_TIME
);
10810 DNPRINTF(SWM_D_EVENT
, "destroynotify: done.\n");
10815 get_notify_detail_label(uint8_t detail
)
10820 case XCB_NOTIFY_DETAIL_ANCESTOR
:
10821 label
= "Ancestor";
10823 case XCB_NOTIFY_DETAIL_VIRTUAL
:
10826 case XCB_NOTIFY_DETAIL_INFERIOR
:
10827 label
= "Inferior";
10829 case XCB_NOTIFY_DETAIL_NONLINEAR
:
10830 label
= "Nonlinear";
10832 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
10833 label
= "NonlinearVirtual";
10835 case XCB_NOTIFY_DETAIL_POINTER
:
10838 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
10839 label
= "PointerRoot";
10841 case XCB_NOTIFY_DETAIL_NONE
:
10852 get_notify_mode_label(uint8_t mode
)
10857 case XCB_NOTIFY_MODE_NORMAL
:
10860 case XCB_NOTIFY_MODE_GRAB
:
10863 case XCB_NOTIFY_MODE_UNGRAB
:
10866 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
10867 label
= "WhileGrabbed";
10877 get_state_mask_label(uint16_t state
)
10882 case XCB_KEY_BUT_MASK_SHIFT
:
10883 label
= "ShiftMask";
10885 case XCB_KEY_BUT_MASK_LOCK
:
10886 label
= "LockMask";
10888 case XCB_KEY_BUT_MASK_CONTROL
:
10889 label
= "ControlMask";
10891 case XCB_KEY_BUT_MASK_MOD_1
:
10892 label
= "Mod1Mask";
10894 case XCB_KEY_BUT_MASK_MOD_2
:
10895 label
= "Mod2Mask";
10897 case XCB_KEY_BUT_MASK_MOD_3
:
10898 label
= "Mod3Mask";
10900 case XCB_KEY_BUT_MASK_MOD_4
:
10901 label
= "Mod4Mask";
10903 case XCB_KEY_BUT_MASK_MOD_5
:
10904 label
= "Mod5Mask";
10906 case XCB_KEY_BUT_MASK_BUTTON_1
:
10907 label
= "Button1Mask";
10909 case XCB_KEY_BUT_MASK_BUTTON_2
:
10910 label
= "Button2Mask";
10912 case XCB_KEY_BUT_MASK_BUTTON_3
:
10913 label
= "Button3Mask";
10915 case XCB_KEY_BUT_MASK_BUTTON_4
:
10916 label
= "Button4Mask";
10918 case XCB_KEY_BUT_MASK_BUTTON_5
:
10919 label
= "Button5Mask";
10933 enternotify(xcb_enter_notify_event_t
*e
)
10935 struct ws_win
*win
;
10936 struct swm_region
*r
;
10938 last_event_time
= e
->time
;
10940 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): %#x "
10941 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10942 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10943 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10944 get_notify_mode_label(e
->mode
), e
->mode
,
10945 get_notify_detail_label(e
->detail
), e
->detail
,
10946 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10947 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10950 if (e
->event
== e
->root
&& e
->child
== XCB_WINDOW_NONE
&&
10951 e
->mode
== XCB_NOTIFY_MODE_GRAB
&&
10952 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10953 DNPRINTF(SWM_D_EVENT
, "enternotify: grab inferior; ignoring.\n");
10957 if (focus_mode
== SWM_FOCUS_MANUAL
&&
10958 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10959 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
10963 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10964 e
->mode
== XCB_NOTIFY_MODE_UNGRAB
&&
10965 e
->detail
!= XCB_NOTIFY_DETAIL_ANCESTOR
) {
10966 DNPRINTF(SWM_D_EVENT
, "enternotify: ungrab; ignoring.\n");
10970 if ((win
= find_window(e
->event
)) == NULL
) {
10971 if (e
->event
== e
->root
) {
10972 /* If no windows on pointer region, then focus root. */
10973 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10975 DNPRINTF(SWM_D_EVENT
, "enternotify: "
10976 "NULL region; ignoring.\n");
10982 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
10987 if (e
->mode
== XCB_NOTIFY_MODE_NORMAL
&&
10988 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10989 DNPRINTF(SWM_D_EVENT
, "enternotify: entering from "
10990 "inferior; ignoring\n");
10994 focus_win(get_focus_magic(win
));
10997 DNPRINTF(SWM_D_EVENT
, "enternotify: done\n");
11004 leavenotify(xcb_leave_notify_event_t
*e
)
11006 last_event_time
= e
->time
;
11008 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): %#x "
11009 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
11010 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
11011 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
11012 get_notify_mode_label(e
->mode
), e
->mode
,
11013 get_notify_detail_label(e
->detail
), e
->detail
,
11014 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
11015 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
11021 mapnotify(xcb_map_notify_event_t
*e
)
11023 struct ws_win
*win
, *parent
= NULL
;
11024 struct workspace
*ws
;
11026 DNPRINTF(SWM_D_EVENT
, "mapnotify: win %#x\n", e
->window
);
11028 if ((win
= manage_window(e
->window
, spawn_position
, false)) == NULL
)
11032 if (win
->state
== SWM_WIN_STATE_REPARENTING
)
11035 if (ws
->r
== NULL
) {
11040 /* Need to know if win was mapped due to ws switch. */
11041 if (ws
->state
== SWM_WS_STATE_MAPPED
) {
11042 if (ws
->focus_pending
&& TRANS(ws
->focus_pending
))
11043 parent
= find_window(win
->transient
);
11045 /* If window's parent is maximized, don't clear it. */
11046 if ((parent
== NULL
) || !MAXIMIZED(parent
))
11047 if (clear_maximized(ws
) > 0)
11051 win
->mapped
= true;
11052 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
11054 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
11055 if (ws
->focus_pending
== win
) {
11057 ws
->focus_pending
= NULL
;
11058 center_pointer(ws
->r
);
11064 DNPRINTF(SWM_D_EVENT
, "mapnotify: done\n");
11070 mappingnotify(xcb_mapping_notify_event_t
*e
)
11072 if (e
->request
!= XCB_MAPPING_POINTER
) {
11073 xcb_refresh_keyboard_mapping(syms
, e
);
11081 maprequest(xcb_map_request_event_t
*e
)
11083 struct ws_win
*win
, *w
= NULL
;
11085 DNPRINTF(SWM_D_EVENT
, "maprequest: win %#x\n",
11088 win
= manage_window(e
->window
, spawn_position
, true);
11092 /* The new window should get focus; prepare. */
11093 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
11094 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
) && ACCEPTS_FOCUS(win
)) {
11095 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
11096 /* See if other wins of same type are already mapped. */
11097 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
11098 if (w
== win
|| !w
->mapped
)
11101 if (w
->ch
.class_name
&&
11102 win
->ch
.class_name
&&
11103 strcmp(w
->ch
.class_name
,
11104 win
->ch
.class_name
) == 0 &&
11105 w
->ch
.instance_name
&&
11106 win
->ch
.instance_name
&&
11107 strcmp(w
->ch
.instance_name
,
11108 win
->ch
.instance_name
) == 0)
11114 win
->ws
->focus_pending
= get_focus_magic(win
);
11117 /* All windows need to be mapped if they are in the current workspace.*/
11120 /* Ignore EnterNotify to handle the mapnotify without interference. */
11121 if (focus_mode
== SWM_FOCUS_DEFAULT
)
11122 event_drain(XCB_ENTER_NOTIFY
);
11124 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
11128 motionnotify(xcb_motion_notify_event_t
*e
)
11130 struct swm_region
*r
= NULL
;
11131 int i
, num_screens
;
11133 last_event_time
= e
->time
;
11135 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): %#x "
11136 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
11137 "child: %#x, same_screen_focus: %s, state: %d\n",
11138 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
11139 get_notify_detail_label(e
->detail
), e
->detail
,
11140 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
11141 YESNO(e
->same_screen
), e
->state
);
11143 if (focus_mode
== SWM_FOCUS_MANUAL
)
11146 num_screens
= get_screen_count();
11147 for (i
= 0; i
< num_screens
; i
++)
11148 if (screens
[i
].root
== e
->root
)
11151 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11152 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
11153 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
11161 get_atom_name(xcb_atom_t atom
)
11164 #ifdef SWM_DEBUG_ATOM_NAMES
11166 * This should be disabled during most debugging since
11167 * xcb_get_* causes an xcb_flush.
11170 xcb_get_atom_name_reply_t
*r
;
11172 r
= xcb_get_atom_name_reply(conn
,
11173 xcb_get_atom_name(conn
, atom
),
11176 len
= xcb_get_atom_name_name_length(r
);
11178 name
= malloc(len
+ 1);
11180 memcpy(name
, xcb_get_atom_name_name(r
), len
);
11194 propertynotify(xcb_property_notify_event_t
*e
)
11196 struct ws_win
*win
;
11197 struct workspace
*ws
;
11201 name
= get_atom_name(e
->atom
);
11202 DNPRINTF(SWM_D_EVENT
, "propertynotify: win %#x, atom: %s(%u), "
11203 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
11207 last_event_time
= e
->time
;
11209 win
= find_window(e
->window
);
11215 if (e
->atom
== a_state
) {
11216 /* State just changed, make sure it gets focused if mapped. */
11217 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
11218 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
11220 win
->state
== SWM_WIN_STATE_REPARENTED
&&
11221 ws
->focus_pending
== win
) {
11222 focus_win(ws
->focus_pending
);
11223 ws
->focus_pending
= NULL
;
11227 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
11228 e
->atom
== XCB_ATOM_WM_NAME
) {
11230 bar_draw(ws
->r
->bar
);
11231 } else if (e
->atom
== a_prot
) {
11232 get_wm_protocols(win
);
11233 } else if (e
->atom
== XCB_ATOM_WM_NORMAL_HINTS
) {
11234 xcb_icccm_get_wm_normal_hints_reply(conn
,
11235 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
11243 reparentnotify(xcb_reparent_notify_event_t
*e
)
11245 struct ws_win
*win
;
11247 DNPRINTF(SWM_D_EVENT
, "reparentnotify: event: %#x, win %#x, "
11248 "parent: %#x, (x,y): (%u,%u), override_redirect: %u\n",
11249 e
->event
, e
->window
, e
->parent
, e
->x
, e
->y
, e
->override_redirect
);
11251 win
= find_window(e
->window
);
11253 if (win
->state
== SWM_WIN_STATE_REPARENTING
) {
11254 win
->state
= SWM_WIN_STATE_REPARENTED
;
11256 if (win
->ws
->r
&& !ICONIC(win
))
11261 update_window(win
);
11262 update_win_stacking(win
);
11263 } else if (win
->state
== SWM_WIN_STATE_UNPARENTING
) {
11264 win
->state
= SWM_WIN_STATE_UNPARENTED
;
11270 unmapnotify(xcb_unmap_notify_event_t
*e
)
11272 struct ws_win
*win
;
11273 struct workspace
*ws
;
11275 DNPRINTF(SWM_D_EVENT
, "unmapnotify: win %#x\n", e
->window
);
11277 /* If we aren't managing the window, then ignore. */
11278 win
= find_window(e
->window
);
11279 if (win
== NULL
|| win
->id
!= e
->window
) {
11280 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore unmanaged.\n");
11284 /* Do nothing if already withdrawn. */
11285 if (!win
->mapped
&& !ICONIC(win
)) {
11286 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore withdrawn.\n");
11291 win
->mapped
= false;
11293 /* Ignore if reparenting-related. */
11294 if (win
->state
!= SWM_WIN_STATE_REPARENTED
) {
11295 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore not reparented.\n");
11299 /* If win was focused, make sure to focus on something else. */
11300 if (win
== ws
->focus
) {
11301 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
11302 ws
->focus_pending
= get_focus_prev(win
);
11303 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
11304 "focus_pending: %#x\n",
11305 WINID(ws
->focus_pending
));
11313 DNPRINTF(SWM_D_EVENT
, "unmapnotify: iconify.\n");
11314 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
11317 DNPRINTF(SWM_D_EVENT
, "unmapnotify: withdraw.\n");
11318 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
11319 unmanage_window(win
);
11324 /* Update focus if ws is active. */
11325 if (WS_FOCUSED(ws
)) {
11326 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
11327 focus_win(get_pointer_win(ws
->r
->s
->root
));
11328 } else if (ws
->focus_pending
) {
11329 focus_win(ws
->focus_pending
);
11330 ws
->focus_pending
= NULL
;
11331 } else if (ws
->focus
== NULL
) {
11332 DNPRINTF(SWM_D_FOCUS
, "unmapnotify: set_input_focus: "
11333 "%#x, revert-to: parent, time: 0\n",
11335 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11336 ws
->r
->id
, XCB_CURRENT_TIME
);
11340 center_pointer(ws
->r
);
11342 DNPRINTF(SWM_D_EVENT
, "unmapnotify: done.\n");
11347 get_source_type_label(uint32_t type
)
11352 case EWMH_SOURCE_TYPE_NONE
:
11355 case EWMH_SOURCE_TYPE_NORMAL
:
11358 case EWMH_SOURCE_TYPE_OTHER
:
11370 clientmessage(xcb_client_message_event_t
*e
)
11372 struct ws_win
*win
;
11373 struct swm_region
*r
= NULL
;
11376 int num_screens
, i
;
11377 xcb_map_request_event_t mre
;
11381 name
= get_atom_name(e
->type
);
11382 DNPRINTF(SWM_D_EVENT
, "clientmessage: win %#x, atom: %s(%u)\n",
11383 e
->window
, name
, e
->type
);
11387 if (e
->type
== ewmh
[_NET_CURRENT_DESKTOP
].atom
) {
11388 num_screens
= get_screen_count();
11389 for (i
= 0; i
< num_screens
; i
++)
11390 if (screens
[i
].root
== e
->window
) {
11391 r
= screens
[i
].r_focus
;
11395 if (r
&& e
->data
.data32
[0] < (uint32_t)workspace_limit
) {
11396 a
.id
= e
->data
.data32
[0];
11397 switchws(NULL
, r
, &a
);
11402 } else if (e
->type
== ewmh
[_NET_REQUEST_FRAME_EXTENTS
].atom
) {
11403 DNPRINTF(SWM_D_EVENT
,
11404 "clientmessage: set _NET_FRAME_EXTENTS on window.\n");
11405 vals
[0] = vals
[1] = vals
[2] = vals
[3] = border_width
;
11406 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, e
->window
,
11407 a_net_frame_extents
, XCB_ATOM_CARDINAL
, 32, 4, vals
);
11412 win
= find_window(e
->window
);
11414 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11415 /* Manage the window with maprequest. */
11416 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
11417 "unmanaged window.\n");
11418 mre
.window
= e
->window
;
11424 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11425 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW, "
11426 "source_type: %s(%d)\n",
11427 get_source_type_label(e
->data
.data32
[0]),
11428 e
->data
.data32
[0]);
11431 * Allow focus changes that are a result of direct user
11432 * action and from applications that use the old EWMH spec.
11434 if (e
->data
.data32
[0] != EWMH_SOURCE_TYPE_NORMAL
||
11435 win
->quirks
& SWM_Q_OBEYAPPFOCUSREQ
) {
11436 if (WS_FOCUSED(win
->ws
))
11439 win
->ws
->focus_pending
= win
;
11441 } else if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
11442 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
11443 if (win
->can_delete
)
11444 client_msg(win
, a_delete
, 0);
11446 xcb_kill_client(conn
, win
->id
);
11447 } else if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
11448 DNPRINTF(SWM_D_EVENT
,
11449 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
11451 if (e
->data
.data32
[0] & (1<<8)) /* x */
11452 X(win
) = e
->data
.data32
[1];
11453 if (e
->data
.data32
[0] & (1<<9)) /* y */
11454 Y(win
) = e
->data
.data32
[2];
11455 if (e
->data
.data32
[0] & (1<<10)) /* width */
11456 WIDTH(win
) = e
->data
.data32
[3];
11457 if (e
->data
.data32
[0] & (1<<11)) /* height */
11458 HEIGHT(win
) = e
->data
.data32
[4];
11460 update_window(win
);
11462 /* Notify no change was made. */
11463 config_win(win
, NULL
);
11464 /* TODO: Change stack sizes */
11466 } else if (e
->type
== ewmh
[_NET_RESTACK_WINDOW
].atom
) {
11467 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_RESTACK_WINDOW\n");
11468 vals
[0] = e
->data
.data32
[1]; /* Sibling window. */
11469 vals
[1] = e
->data
.data32
[2]; /* Stack mode detail. */
11471 if (win
->frame
!= XCB_WINDOW_NONE
)
11472 xcb_configure_window(conn
, win
->frame
,
11473 XCB_CONFIG_WINDOW_SIBLING
|
11474 XCB_CONFIG_WINDOW_STACK_MODE
, vals
);
11475 } else if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
11476 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
11477 ewmh_change_wm_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
11478 if (e
->data
.data32
[2])
11479 ewmh_change_wm_state(win
, e
->data
.data32
[2],
11480 e
->data
.data32
[0]);
11482 ewmh_update_wm_state(win
);
11484 } else if (e
->type
== ewmh
[_NET_WM_DESKTOP
].atom
) {
11485 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_DESKTOP\n");
11488 win_to_ws(win
, e
->data
.data32
[0], true);
11490 /* Stack source and destination ws, if mapped. */
11491 if (r
!= win
->ws
->r
) {
11497 load_float_geom(win
);
11510 int errcode
= xcb_connection_has_error(conn
);
11511 #ifdef XCB_CONN_ERROR
11514 case XCB_CONN_ERROR
:
11515 s
= "Socket error, pipe error or other stream error.";
11517 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
11518 s
= "Extension not supported.";
11520 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
11521 s
= "Insufficient memory.";
11523 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
11524 s
= "Request length was exceeded.";
11526 case XCB_CONN_CLOSED_PARSE_ERR
:
11527 s
= "Error parsing display string.";
11530 s
= "Unknown error.";
11533 errx(errcode
, "X CONNECTION ERROR: %s", s
);
11536 errx(errcode
, "X CONNECTION ERROR");
11543 int num_screens
, i
;
11544 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
11545 XCB_EVENT_MASK_ENTER_WINDOW
|
11546 XCB_EVENT_MASK_OWNER_GRAB_BUTTON
|
11547 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
11548 XCB_EVENT_MASK_ENTER_WINDOW
|
11549 XCB_EVENT_MASK_LEAVE_WINDOW
|
11550 XCB_EVENT_MASK_BUTTON_PRESS
|
11551 XCB_EVENT_MASK_BUTTON_RELEASE
|
11552 XCB_EVENT_MASK_KEY_PRESS
|
11553 XCB_EVENT_MASK_KEY_RELEASE
|
11554 XCB_EVENT_MASK_PROPERTY_CHANGE
;
11556 xcb_void_cookie_t wac
;
11557 xcb_generic_error_t
*error
;
11559 /* this causes an error if some other window manager is running */
11560 num_screens
= get_screen_count();
11561 for (i
= 0; i
< num_screens
; i
++) {
11562 if ((sc
= get_screen(i
)) == NULL
)
11563 errx(1, "ERROR: can't get screen %d.", i
);
11564 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: %#x\n",
11566 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
11567 XCB_CW_EVENT_MASK
, &val
);
11568 if ((error
= xcb_request_check(conn
, wac
))) {
11569 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
11570 error
->error_code
);
11580 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
11582 struct swm_region
*r
= NULL
, *n
;
11583 struct workspace
*ws
= NULL
;
11587 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
11588 s
->idx
, w
, h
, x
, y
);
11590 /* remove any conflicting regions */
11591 n
= TAILQ_FIRST(&s
->rl
);
11594 n
= TAILQ_NEXT(r
, entry
);
11595 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
11596 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
11597 if (r
->ws
->r
!= NULL
)
11598 r
->ws
->old_r
= r
->ws
->r
;
11601 xcb_destroy_window(conn
, r
->id
);
11602 r
->id
= XCB_WINDOW_NONE
;
11603 TAILQ_REMOVE(&s
->rl
, r
, entry
);
11604 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
11608 /* search old regions for one to reuse */
11610 /* size + location match */
11611 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11612 if (X(r
) == x
&& Y(r
) == y
&&
11613 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11617 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11618 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11622 TAILQ_REMOVE(&s
->orl
, r
, entry
);
11623 /* try to use old region's workspace */
11624 if (r
->ws
->r
== NULL
)
11627 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
11628 err(1, "new_region: calloc: failed to allocate memory "
11631 /* if we don't have a workspace already, find one */
11633 for (i
= 0; i
< workspace_limit
; i
++)
11634 if (s
->ws
[i
].r
== NULL
) {
11641 errx(1, "new_region: no free workspaces");
11643 if (ws
->state
== SWM_WS_STATE_HIDDEN
)
11644 ws
->state
= SWM_WS_STATE_MAPPING
;
11653 r
->ws_prior
= NULL
;
11656 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
11658 /* Invisible region window to detect pointer events on empty regions. */
11659 r
->id
= xcb_generate_id(conn
);
11660 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
11661 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
11663 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
11664 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
11665 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
11667 /* Make sure region input is at the bottom. */
11668 wa
[0] = XCB_STACK_MODE_BELOW
;
11669 xcb_configure_window(conn
, r
->id
, XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
11671 xcb_map_window(conn
, r
->id
);
11675 scan_randr(int idx
)
11677 #ifdef SWM_XRR_HAS_CRTC
11680 #endif /* SWM_XRR_HAS_CRTC */
11681 struct swm_region
*r
;
11683 xcb_randr_get_screen_resources_current_cookie_t src
;
11684 xcb_randr_get_screen_resources_current_reply_t
*srr
;
11685 xcb_randr_get_crtc_info_cookie_t cic
;
11686 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
11687 xcb_randr_crtc_t
*crtc
;
11688 xcb_screen_t
*screen
;
11690 DNPRINTF(SWM_D_MISC
, "scan_randr: screen: %d\n", idx
);
11692 if ((screen
= get_screen(idx
)) == NULL
)
11693 errx(1, "ERROR: can't get screen %d.", idx
);
11695 num_screens
= get_screen_count();
11696 if (idx
>= num_screens
)
11697 errx(1, "scan_randr: invalid screen");
11699 /* remove any old regions */
11700 while ((r
= TAILQ_FIRST(&screens
[idx
].rl
)) != NULL
) {
11701 r
->ws
->old_r
= r
->ws
->r
= NULL
;
11703 xcb_destroy_window(conn
, r
->id
);
11704 r
->id
= XCB_WINDOW_NONE
;
11705 TAILQ_REMOVE(&screens
[idx
].rl
, r
, entry
);
11706 TAILQ_INSERT_TAIL(&screens
[idx
].orl
, r
, entry
);
11710 /* map virtual screens onto physical screens */
11711 #ifdef SWM_XRR_HAS_CRTC
11712 if (randr_support
) {
11713 src
= xcb_randr_get_screen_resources_current(conn
,
11714 screens
[idx
].root
);
11715 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
11718 new_region(&screens
[idx
], 0, 0,
11719 screen
->width_in_pixels
,
11720 screen
->height_in_pixels
);
11723 ncrtc
= srr
->num_crtcs
;
11725 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
11726 for (c
= 0; c
< ncrtc
; c
++) {
11727 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
11729 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
11732 if (cir
->num_outputs
== 0) {
11737 if (cir
->mode
== 0)
11738 new_region(&screens
[idx
], 0, 0,
11739 screen
->width_in_pixels
,
11740 screen
->height_in_pixels
);
11742 new_region(&screens
[idx
],
11743 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
11748 #endif /* SWM_XRR_HAS_CRTC */
11750 /* If detection failed, create a single region that spans the screen. */
11751 if (TAILQ_EMPTY(&screens
[idx
].rl
))
11752 new_region(&screens
[idx
], 0, 0, screen
->width_in_pixels
,
11753 screen
->height_in_pixels
);
11756 /* The screen shouldn't focus on unused regions. */
11757 TAILQ_FOREACH(r
, &screens
[idx
].orl
, entry
) {
11758 if (screens
[idx
].r_focus
== r
)
11759 screens
[idx
].r_focus
= NULL
;
11762 DNPRINTF(SWM_D_MISC
, "scan_randr: done.\n");
11766 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
11768 struct swm_region
*r
;
11769 struct workspace
*ws
;
11770 struct ws_win
*win
;
11771 int i
, j
, num_screens
;
11773 DNPRINTF(SWM_D_EVENT
, "screenchange: root: %#x\n", e
->root
);
11775 num_screens
= get_screen_count();
11776 /* silly event doesn't include the screen index */
11777 for (i
= 0; i
< num_screens
; i
++)
11778 if (screens
[i
].root
== e
->root
)
11780 if (i
>= num_screens
)
11781 errx(1, "screenchange: screen not found");
11783 /* brute force for now, just re-enumerate the regions */
11787 print_win_geom(e
->root
);
11789 /* add bars to all regions */
11790 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11793 /* Stack all regions. */
11794 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11797 /* Make sure a region has focus. */
11798 if (screens
[i
].r_focus
== NULL
) {
11799 r
= TAILQ_FIRST(&screens
[i
].rl
);
11804 /* Cleanup unused previously visible workspaces. */
11805 for (j
= 0; j
< workspace_limit
; j
++) {
11806 ws
= &screens
[i
].ws
[j
];
11807 if (ws
->r
== NULL
&& ws
->state
!= SWM_WS_STATE_HIDDEN
) {
11808 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
11810 ws
->state
= SWM_WS_STATE_HIDDEN
;
11816 /* Update workspace state and bar on all regions. */
11817 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11818 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
11826 struct swm_region
*r
= NULL
;
11827 xcb_query_tree_cookie_t qtc
;
11828 xcb_query_tree_reply_t
*qtr
;
11829 xcb_get_property_cookie_t pc
;
11830 xcb_get_property_reply_t
*pr
;
11831 xcb_window_t
*wins
= NULL
, trans
, *cwins
= NULL
;
11832 int i
, j
, k
, n
, no
, num_screens
;
11834 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
11835 num_screens
= get_screen_count();
11836 for (i
= 0; i
< num_screens
; i
++) {
11837 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
11838 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
11841 wins
= xcb_query_tree_children(qtr
);
11842 no
= xcb_query_tree_children_length(qtr
);
11844 /* Try to sort windows according to _NET_CLIENT_LIST. */
11845 pr
= xcb_get_property_reply(conn
, xcb_get_property(conn
, 0,
11846 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
11847 XCB_ATOM_WINDOW
, 0, UINT32_MAX
), NULL
);
11849 cwins
= xcb_get_property_value(pr
);
11850 n
= xcb_get_property_value_length(pr
) /
11851 sizeof(xcb_atom_t
);
11853 for (j
= 0; j
< n
; ++j
) {
11854 for (k
= j
; k
< no
; ++k
) {
11855 if (wins
[k
] == cwins
[j
]) {
11856 /* Swap wins j and k. */
11858 wins
[j
] = cwins
[j
];
11866 /* Manage top-level windows first, then transients. */
11867 /* TODO: allow transients to be managed before leader. */
11868 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top-level windows.\n");
11869 for (j
= 0; j
< no
; j
++) {
11870 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11871 if (r
->id
== wins
[j
]) {
11872 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11873 "skip %#x; region input window.\n",
11876 } else if (r
->bar
->id
== wins
[j
]) {
11877 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11878 "skip %#x; region bar.\n",
11887 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11888 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11890 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
11891 "is transient for %#x.\n", wins
[j
], trans
);
11895 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11898 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
11899 for (j
= 0; j
< no
; j
++) {
11900 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11901 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11903 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11907 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
11911 setup_screens(void)
11913 int i
, j
, k
, num_screens
;
11914 struct workspace
*ws
;
11915 uint32_t gcv
[1], wa
[1];
11916 const xcb_query_extension_reply_t
*qep
;
11917 xcb_screen_t
*screen
;
11918 xcb_randr_query_version_cookie_t c
;
11919 xcb_randr_query_version_reply_t
*r
;
11921 num_screens
= get_screen_count();
11922 if ((screens
= calloc(num_screens
, sizeof(struct swm_screen
))) == NULL
)
11923 err(1, "setup_screens: calloc: failed to allocate memory for "
11926 /* Initial RandR setup. */
11927 randr_support
= false;
11928 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
11929 if (qep
->present
) {
11930 c
= xcb_randr_query_version(conn
, 1, 1);
11931 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
11933 if (r
->major_version
>= 1) {
11934 randr_support
= true;
11935 randr_eventbase
= qep
->first_event
;
11941 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
11943 /* map physical screens */
11944 for (i
= 0; i
< num_screens
; i
++) {
11945 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
11946 screens
[i
].idx
= i
;
11947 screens
[i
].r_focus
= NULL
;
11949 TAILQ_INIT(&screens
[i
].rl
);
11950 TAILQ_INIT(&screens
[i
].orl
);
11951 if ((screen
= get_screen(i
)) == NULL
)
11952 errx(1, "ERROR: can't get screen %d.", i
);
11953 screens
[i
].root
= screen
->root
;
11955 /* set default colors */
11956 setscreencolor("red", i
, SWM_S_COLOR_FOCUS
);
11957 setscreencolor("rgb:88/88/88", i
, SWM_S_COLOR_UNFOCUS
);
11958 setscreencolor("rgb:00/80/80", i
, SWM_S_COLOR_BAR_BORDER
);
11959 setscreencolor("rgb:00/40/40", i
,
11960 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
11961 setscreencolor("black", i
, SWM_S_COLOR_BAR
);
11962 setscreencolor("rgb:a0/a0/a0", i
, SWM_S_COLOR_BAR_FONT
);
11963 setscreencolor("red", i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
11964 setscreencolor("rgb:88/88/88", i
,
11965 SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
11967 /* create graphics context on screen */
11968 screens
[i
].bar_gc
= xcb_generate_id(conn
);
11970 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
11971 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
11973 /* set default cursor */
11974 xcb_change_window_attributes(conn
, screens
[i
].root
,
11975 XCB_CW_CURSOR
, wa
);
11977 /* init all workspaces */
11978 /* XXX these should be dynamically allocated too */
11979 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
11980 ws
= &screens
[i
].ws
[j
];
11983 ws
->bar_enabled
= true;
11985 ws
->focus_prev
= NULL
;
11986 ws
->focus_pending
= NULL
;
11987 ws
->focus_raise
= NULL
;
11990 ws
->state
= SWM_WS_STATE_HIDDEN
;
11991 TAILQ_INIT(&ws
->stack
);
11992 TAILQ_INIT(&ws
->winlist
);
11993 TAILQ_INIT(&ws
->unmanagedlist
);
11995 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
11996 layouts
[k
].l_config(ws
, SWM_ARG_ID_STACKINIT
);
11997 ws
->cur_layout
= &layouts
[0];
11998 ws
->cur_layout
->l_string(ws
);
12004 xcb_randr_select_input(conn
, screens
[i
].root
,
12005 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
12010 setup_globals(void)
12012 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
12013 err(1, "setup_globals: strdup: failed to allocate memory.");
12015 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
12016 err(1, "setup_globals: strdup: failed to allocate memory.");
12018 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
12019 errx(1, "unable to allocate key symbols");
12021 a_state
= get_atom_from_string("WM_STATE");
12022 a_prot
= get_atom_from_string("WM_PROTOCOLS");
12023 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
12024 a_net_frame_extents
= get_atom_from_string("_NET_FRAME_EXTENTS");
12025 a_net_supported
= get_atom_from_string("_NET_SUPPORTED");
12026 a_net_wm_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
12027 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
12028 a_utf8_string
= get_atom_from_string("UTF8_STRING");
12029 a_swm_ws
= get_atom_from_string("_SWM_WS");
12033 shutdown_cleanup(void)
12035 struct swm_region
*r
;
12037 struct workspace
*ws
;
12038 int i
, num_screens
;
12040 /* disable alarm because the following code may not be interrupted */
12042 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
12043 err(1, "can't disable alarm");
12056 num_screens
= get_screen_count();
12057 for (i
= 0; i
< num_screens
; ++i
) {
12060 DNPRINTF(SWM_D_FOCUS
, "shutdown_cleanup: set_input_focus: "
12061 "%#x, revert-to: root, time: 0\n", screens
[i
].root
);
12062 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
12063 screens
[i
].root
, XCB_CURRENT_TIME
);
12065 if (screens
[i
].bar_gc
!= XCB_NONE
)
12066 xcb_free_gc(conn
, screens
[i
].bar_gc
);
12067 if (!bar_font_legacy
) {
12068 XftColorFree(display
, DefaultVisual(display
, i
),
12069 DefaultColormap(display
, i
), &bar_font_color
);
12070 XftColorFree(display
, DefaultVisual(display
, i
),
12071 DefaultColormap(display
, i
), &search_font_color
);
12074 for (j
= 0; j
< SWM_S_COLOR_MAX
; ++j
) {
12075 free(screens
[i
].c
[j
].name
);
12078 /* Free window memory. */
12079 for (j
= 0; j
< SWM_WS_MAX
; ++j
) {
12080 ws
= &screens
[i
].ws
[j
];
12083 while ((w
= TAILQ_FIRST(&ws
->winlist
)) != NULL
) {
12084 TAILQ_REMOVE(&ws
->winlist
, w
, entry
);
12088 while ((w
= TAILQ_FIRST(&ws
->unmanagedlist
)) != NULL
) {
12089 TAILQ_REMOVE(&ws
->unmanagedlist
, w
, entry
);
12094 /* Free region memory. */
12095 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
12096 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
12101 while ((r
= TAILQ_FIRST(&screens
[i
].orl
)) != NULL
) {
12102 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
12111 free(clock_format
);
12112 free(startup_exception
);
12115 XFreeFontSet(display
, bar_fs
);
12117 XftFontClose(display
, bar_font
);
12119 xcb_key_symbols_free(syms
);
12121 xcb_aux_sync(conn
);
12122 xcb_disconnect(conn
);
12126 event_error(xcb_generic_error_t
*e
)
12130 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
12131 "resource_id: %u, minor_code: %u\n",
12132 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
12133 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
12134 e
->sequence
, e
->resource_id
, e
->minor_code
);
12138 event_handle(xcb_generic_event_t
*evt
)
12140 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
12142 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
12143 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
12144 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
12147 #define EVENT(type, callback) case type: callback((void *)evt); return
12148 EVENT(0, event_error
);
12149 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
12150 EVENT(XCB_BUTTON_RELEASE
, buttonrelease
);
12151 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
12152 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
12153 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
12154 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
12155 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
12156 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
12157 /*EVENT(XCB_CREATE_NOTIFY, );*/
12158 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
12159 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
12160 EVENT(XCB_EXPOSE
, expose
);
12161 EVENT(XCB_FOCUS_IN
, focusin
);
12163 EVENT(XCB_FOCUS_OUT
, focusout
);
12165 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
12166 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
12167 EVENT(XCB_KEY_PRESS
, keypress
);
12168 EVENT(XCB_KEY_RELEASE
, keyrelease
);
12169 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
12171 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
12173 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
12174 EVENT(XCB_MAP_REQUEST
, maprequest
);
12175 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
12176 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
12177 /*EVENT(XCB_NO_EXPOSURE, );*/
12178 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
12179 EVENT(XCB_REPARENT_NOTIFY
, reparentnotify
);
12180 /*EVENT(XCB_RESIZE_REQUEST, );*/
12181 /*EVENT(XCB_SELECTION_CLEAR, );*/
12182 /*EVENT(XCB_SELECTION_NOTIFY, );*/
12183 /*EVENT(XCB_SELECTION_REQUEST, );*/
12184 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
12185 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
12188 if (type
- randr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
12189 screenchange((void *)evt
);
12193 main(int argc
, char *argv
[])
12195 struct pollfd pfd
[2];
12196 struct sigaction sact
;
12198 struct passwd
*pwd
;
12199 struct swm_region
*r
;
12200 xcb_generic_event_t
*evt
;
12201 int xfd
, i
, num_screens
, num_readable
;
12202 char conf
[PATH_MAX
], *cfile
= NULL
;
12203 bool stdin_ready
= false, startup
= true;
12205 /* suppress unused warning since var is needed */
12209 time_started
= time(NULL
);
12213 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
12214 if (setlocale(LC_CTYPE
, "") == NULL
|| setlocale(LC_TIME
, "") == NULL
)
12215 warnx("no locale support");
12217 /* handle some signals */
12218 bzero(&sact
, sizeof(sact
));
12219 sigemptyset(&sact
.sa_mask
);
12221 sact
.sa_handler
= sighdlr
;
12222 sigaction(SIGINT
, &sact
, NULL
);
12223 sigaction(SIGQUIT
, &sact
, NULL
);
12224 sigaction(SIGTERM
, &sact
, NULL
);
12225 sigaction(SIGHUP
, &sact
, NULL
);
12227 sact
.sa_handler
= sighdlr
;
12228 sact
.sa_flags
= SA_NOCLDSTOP
;
12229 sigaction(SIGCHLD
, &sact
, NULL
);
12231 if ((display
= XOpenDisplay(0)) == NULL
)
12232 errx(1, "can not open display");
12234 conn
= XGetXCBConnection(display
);
12235 if (xcb_connection_has_error(conn
))
12236 errx(1, "can not get XCB connection");
12238 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
12240 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
12241 xfd
= xcb_get_file_descriptor(conn
);
12243 /* look for local and global conf file */
12244 pwd
= getpwuid(getuid());
12246 errx(1, "invalid user: %d", getuid());
12248 xcb_grab_server(conn
);
12249 xcb_aux_sync(conn
);
12251 /* flush all events */
12252 while ((evt
= get_next_event(false))) {
12253 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
12259 errx(1, "another window manager is currently running");
12261 /* Load Xcursors and/or cursorfont glyph cursors. */
12264 xcb_aux_sync(conn
);
12269 setup_keybindings();
12270 setup_btnbindings();
12275 for (i
= 0; ; i
++) {
12280 snprintf(conf
, sizeof conf
, "%s/.%s",
12281 pwd
->pw_dir
, SWM_CONF_FILE
);
12285 snprintf(conf
, sizeof conf
, "/etc/%s",
12290 snprintf(conf
, sizeof conf
, "%s/.%s",
12291 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
12294 /* global compat */
12295 snprintf(conf
, sizeof conf
, "/etc/%s",
12296 SWM_CONF_FILE_OLD
);
12302 if (strlen(conf
) && stat(conf
, &sb
) != -1)
12303 if (S_ISREG(sb
.st_mode
)) {
12310 /* load conf (if any) */
12312 conf_load(cfile
, SWM_CONF_DEFAULT
);
12316 if (getenv("SWM_STARTED") == NULL
)
12317 setenv("SWM_STARTED", "YES", 1);
12319 /* setup all bars */
12320 num_screens
= get_screen_count();
12321 for (i
= 0; i
< num_screens
; i
++)
12322 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12325 /* Manage existing windows. */
12331 /* Stack all regions to trigger mapping. */
12332 for (i
= 0; i
< num_screens
; i
++)
12333 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12336 xcb_ungrab_server(conn
);
12339 /* Update state and bar of each newly mapped workspace. */
12340 for (i
= 0; i
< num_screens
; i
++)
12341 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
12342 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
12346 memset(&pfd
, 0, sizeof(pfd
));
12348 pfd
[0].events
= POLLIN
;
12349 pfd
[1].fd
= STDIN_FILENO
;
12350 pfd
[1].events
= POLLIN
;
12353 while ((evt
= get_next_event(false))) {
12360 /* If just (re)started, set default focus if needed. */
12364 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
12365 r
= TAILQ_FIRST(&screens
[0].rl
);
12377 num_readable
= poll(pfd
, bar_extra
? 2 : 1, 1000);
12378 if (num_readable
== -1) {
12379 DNPRINTF(SWM_D_MISC
, "poll failed: %s",
12381 } else if (num_readable
> 0 && bar_extra
&&
12382 pfd
[1].revents
& POLLIN
) {
12383 stdin_ready
= true;
12387 restart(NULL
, NULL
, NULL
);
12393 stdin_ready
= false;
12394 bar_extra_update();
12397 /* Need to ensure the bar(s) are always updated. */
12398 for (i
= 0; i
< num_screens
; i
++)
12399 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12405 shutdown_cleanup();