2 * Copyright (c) 2009-2015 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2016 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 * Copyright (c) 2012-2015 David Hill <dhill@mindcry.org>
12 * Copyright (c) 2014-2015 Yuri D'Elia <yuri.delia@eurac.edu>
14 * Permission to use, copy, modify, and distribute this software for any
15 * purpose with or without fee is hereby granted, provided that the above
16 * copyright notice and this permission notice appear in all copies.
18 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
19 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
21 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
23 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
24 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 #include <sys/types.h>
35 #include <sys/queue.h>
37 #include <sys/param.h>
38 #include <sys/select.h>
39 #if defined(__linux__)
41 #elif defined(__OpenBSD__)
43 #elif defined(__FreeBSD__)
67 #include <X11/cursorfont.h>
68 #include <X11/extensions/Xrandr.h>
69 #include <X11/Xcursor/Xcursor.h>
70 #include <X11/Xft/Xft.h>
71 #include <X11/Xlib-xcb.h>
72 #include <xcb/xcb_atom.h>
73 #include <xcb/xcb_aux.h>
74 #include <xcb/xcb_event.h>
75 #include <xcb/xcb_icccm.h>
76 #include <xcb/xcb_keysyms.h>
77 #include <xcb/xtest.h>
78 #include <xcb/randr.h>
86 #ifdef SPECTRWM_BUILDSTR
87 static const char *buildstr
= SPECTRWM_BUILDSTR
;
89 static const char *buildstr
= SPECTRWM_VERSION
;
92 #if !defined(__CYGWIN__) /* cygwin chokes on randr stuff */
94 # error RandR versions less than 1.0 are not supported
99 # define SWM_XRR_HAS_CRTC
102 #endif /* __CYGWIN__ */
104 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
105 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
106 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
107 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
108 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
109 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
110 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
111 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
112 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
113 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
114 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
115 #define xcb_icccm_get_wm_class xcb_get_wm_class
116 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
117 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
118 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
119 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
120 #define xcb_icccm_wm_hints_get_urgency xcb_wm_hints_get_urgency
121 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
122 #define xcb_icccm_get_wm_name xcb_get_wm_name
123 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
124 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
125 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
126 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
127 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
128 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
129 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
130 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
131 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
132 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
135 /*#define SWM_DEBUG*/
137 #define DPRINTF(x...) do { \
139 fprintf(stderr, x); \
141 #define DNPRINTF(n,x...) do { \
142 if (swm_debug & n) { \
143 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
144 fprintf(stderr, x); \
147 #define SWM_D_MISC 0x0001
148 #define SWM_D_EVENT 0x0002
149 #define SWM_D_WS 0x0004
150 #define SWM_D_FOCUS 0x0008
151 #define SWM_D_MOVE 0x0010
152 #define SWM_D_STACK 0x0020
153 #define SWM_D_MOUSE 0x0040
154 #define SWM_D_PROP 0x0080
155 #define SWM_D_CLASS 0x0100
156 #define SWM_D_KEY 0x0200
157 #define SWM_D_QUIRK 0x0400
158 #define SWM_D_SPAWN 0x0800
159 #define SWM_D_EVENTQ 0x1000
160 #define SWM_D_CONF 0x2000
161 #define SWM_D_BAR 0x4000
162 #define SWM_D_INIT 0x8000
164 uint32_t swm_debug
= 0
183 #define DPRINTF(x...)
184 #define DNPRINTF(n,x...)
187 #define SWM_EWMH_ACTION_COUNT_MAX (8)
188 #define EWMH_F_FULLSCREEN (0x001)
189 #define EWMH_F_ABOVE (0x002)
190 #define EWMH_F_HIDDEN (0x004)
191 #define EWMH_F_MAXIMIZED_VERT (0x008)
192 #define EWMH_F_MAXIMIZED_HORZ (0x010)
193 #define EWMH_F_SKIP_PAGER (0x020)
194 #define EWMH_F_SKIP_TASKBAR (0x040)
195 #define SWM_F_MANUAL (0x080)
197 #define EWMH_F_MAXIMIZED (EWMH_F_MAXIMIZED_VERT | EWMH_F_MAXIMIZED_HORZ)
199 /* convert 8-bit to 16-bit */
200 #define RGB_8_TO_16(col) (((col) << 8) + (col))
202 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
203 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
204 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
205 xrc.blue = RGB_8_TO_16((px) & 0xff); \
208 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
209 #define MODKEY XCB_MOD_MASK_1
210 #define ANYMOD XCB_MOD_MASK_ANY
211 #define CLEANMASK(mask) ((mask) & (XCB_KEY_BUT_MASK_SHIFT | \
212 XCB_KEY_BUT_MASK_CONTROL | XCB_KEY_BUT_MASK_MOD_1 | \
213 XCB_KEY_BUT_MASK_MOD_2 | XCB_KEY_BUT_MASK_MOD_3 | \
214 XCB_KEY_BUT_MASK_MOD_4 | XCB_KEY_BUT_MASK_MOD_5) & ~(numlockmask))
215 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
216 XCB_EVENT_MASK_BUTTON_RELEASE)
217 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
218 #define SWM_PROPLEN (16)
219 #define SWM_FUNCNAME_LEN (32)
220 #define SWM_QUIRK_LEN (64)
221 #define X(r) ((r)->g.x)
222 #define Y(r) ((r)->g.y)
223 #define WIDTH(r) ((r)->g.w)
224 #define HEIGHT(r) ((r)->g.h)
225 #define MAX_X(r) ((r)->g.x + (r)->g.w)
226 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
227 #define SH_MIN(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
228 #define SH_MIN_W(w) ((w)->sh.min_width)
229 #define SH_MIN_H(w) ((w)->sh.min_height)
230 #define SH_MAX(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
231 #define SH_MAX_W(w) ((w)->sh.max_width)
232 #define SH_MAX_H(w) ((w)->sh.max_height)
233 #define SH_INC(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)
234 #define SH_INC_W(w) ((w)->sh.width_inc)
235 #define SH_INC_H(w) ((w)->sh.height_inc)
236 #define SWM_MAX_FONT_STEPS (3)
237 #define WINID(w) ((w) ? (w)->id : XCB_WINDOW_NONE)
238 #define ACCEPTS_FOCUS(w) (!((w)->hints.flags & XCB_ICCCM_WM_HINT_INPUT) \
239 || ((w)->hints.input))
240 #define WS_FOCUSED(ws) ((ws)->r && (ws)->r->s->r_focus == (ws)->r)
241 #define YESNO(x) ((x) ? "yes" : "no")
242 #define ICONIC(w) ((w)->ewmh_flags & EWMH_F_HIDDEN)
243 #define ABOVE(w) ((w)->ewmh_flags & EWMH_F_ABOVE)
244 #define FULLSCREEN(w) ((w)->ewmh_flags & EWMH_F_FULLSCREEN)
245 #define MAXIMIZED_VERT(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_VERT)
246 #define MAXIMIZED_HORZ(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_HORZ)
247 #define MAXIMIZED(w) (MAXIMIZED_VERT(w) || MAXIMIZED_HORZ(w))
248 #define MANUAL(w) ((w)->ewmh_flags & SWM_F_MANUAL)
249 #define TRANS(w) ((w)->transient != XCB_WINDOW_NONE)
250 #define FLOATING(w) (ABOVE(w) || TRANS(w) || FULLSCREEN(w) || \
253 /* Constrain Window flags */
254 #define SWM_CW_RESIZABLE (0x01)
255 #define SWM_CW_SOFTBOUNDARY (0x02)
256 #define SWM_CW_HARDBOUNDARY (0x04)
257 #define SWM_CW_RIGHT (0x10)
258 #define SWM_CW_LEFT (0x20)
259 #define SWM_CW_BOTTOM (0x40)
260 #define SWM_CW_TOP (0x80)
261 #define SWM_CW_ALLSIDES (0xf0)
263 #define SWM_FOCUS_DEFAULT (0)
264 #define SWM_FOCUS_FOLLOW (1)
265 #define SWM_FOCUS_MANUAL (2)
267 #define SWM_CK_ALL (0xf)
268 #define SWM_CK_FOCUS (0x1)
269 #define SWM_CK_POINTER (0x2)
270 #define SWM_CK_FALLBACK (0x4)
271 #define SWM_CK_REGION (0x8)
273 #define SWM_CONF_DEFAULT (0)
274 #define SWM_CONF_KEYMAPPING (1)
277 #define SWM_LIB "/usr/local/lib/libswmhack.so"
284 xcb_atom_t a_net_frame_extents
;
285 xcb_atom_t a_net_wm_check
;
286 xcb_atom_t a_net_supported
;
287 xcb_atom_t a_takefocus
;
288 xcb_atom_t a_utf8_string
;
290 volatile sig_atomic_t running
= 1;
291 volatile sig_atomic_t restart_wm
= 0;
292 xcb_timestamp_t last_event_time
= 0;
296 unsigned int numlockmask
= 0;
299 xcb_connection_t
*conn
;
300 xcb_key_symbols_t
*syms
;
302 int boundary_width
= 50;
303 bool cycle_empty
= false;
304 bool cycle_visible
= false;
306 int font_adjusted
= 0;
307 uint16_t mod_key
= MODKEY
;
308 bool warp_focus
= false;
309 bool warp_pointer
= false;
310 bool workspace_clamp
= false;
313 struct swm_region
*search_r
;
314 int select_list_pipe
[2];
315 int select_resp_pipe
[2];
317 volatile sig_atomic_t search_resp
;
318 int search_resp_action
;
320 struct search_window
{
321 TAILQ_ENTRY(search_window
) entry
;
325 xcb_window_t indicator
;
327 TAILQ_HEAD(search_winlist
, search_window
);
328 struct search_winlist search_wl
;
333 SWM_SEARCH_UNICONIFY
,
334 SWM_SEARCH_NAME_WORKSPACE
,
335 SWM_SEARCH_SEARCH_WORKSPACE
,
336 SWM_SEARCH_SEARCH_WINDOW
339 #define SWM_STACK_TOP (0)
340 #define SWM_STACK_BOTTOM (1)
341 #define SWM_STACK_ABOVE (2)
342 #define SWM_STACK_BELOW (3)
345 double dialog_ratio
= 0.6;
347 #define SWM_BAR_MAX (256)
348 #define SWM_BAR_JUSTIFY_LEFT (0)
349 #define SWM_BAR_JUSTIFY_CENTER (1)
350 #define SWM_BAR_JUSTIFY_RIGHT (2)
351 #define SWM_BAR_OFFSET (4)
352 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
353 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
354 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
355 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
356 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
358 #ifdef X_HAVE_UTF8_STRING
359 #define DRAWSTRING(x...) Xutf8DrawString(x)
361 #define DRAWSTRING(x...) XmbDrawString(x)
364 char *bar_argv
[] = { NULL
, NULL
};
366 char bar_ext
[SWM_BAR_MAX
];
367 char bar_ext_buf
[SWM_BAR_MAX
];
368 char bar_vertext
[SWM_BAR_MAX
];
369 bool bar_version
= false;
370 bool bar_enabled
= true;
371 int bar_border_width
= 1;
372 bool bar_at_bottom
= false;
373 bool bar_extra
= false;
375 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
376 char *bar_format
= NULL
;
377 bool stack_enabled
= true;
378 bool clock_enabled
= true;
379 bool iconic_enabled
= false;
380 bool maximize_hide_bar
= false;
381 bool urgent_enabled
= false;
382 bool urgent_collapse
= false;
383 char *clock_format
= NULL
;
384 bool window_class_enabled
= false;
385 bool window_instance_enabled
= false;
386 bool window_name_enabled
= false;
387 int focus_mode
= SWM_FOCUS_DEFAULT
;
388 int focus_close
= SWM_STACK_BELOW
;
389 bool focus_close_wrap
= true;
390 int focus_default
= SWM_STACK_TOP
;
391 int spawn_position
= SWM_STACK_TOP
;
392 bool disable_border
= false;
393 int border_width
= 1;
394 int region_padding
= 0;
396 bool java_workaround
= true;
397 bool verbose_layout
= false;
403 XFontSet bar_fs
= NULL
;
404 XFontSetExtents
*bar_fs_extents
;
405 XftFont
*bar_font
= NULL
;
406 bool bar_font_legacy
= true;
407 char *bar_fonts
= NULL
;
408 XftColor bar_font_color
;
409 XftColor search_font_color
;
410 char *startup_exception
= NULL
;
411 unsigned int nr_exceptions
= 0;
413 /* layout manager data */
414 struct swm_geometry
{
427 struct swm_geometry g
;
428 struct swm_region
*r
; /* Associated region. */
431 /* virtual "screens" */
433 TAILQ_ENTRY(swm_region
) entry
;
435 struct swm_geometry g
;
436 struct workspace
*ws
; /* current workspace on this region */
437 struct workspace
*ws_prior
; /* prior workspace on this region */
438 struct swm_screen
*s
; /* screen idx */
441 TAILQ_HEAD(swm_region_list
, swm_region
);
444 SWM_WIN_STATE_REPARENTING
,
445 SWM_WIN_STATE_REPARENTED
,
446 SWM_WIN_STATE_UNPARENTING
,
447 SWM_WIN_STATE_UNPARENTED
,
451 TAILQ_ENTRY(ws_win
) entry
;
452 TAILQ_ENTRY(ws_win
) stack_entry
;
455 xcb_window_t transient
;
456 struct ws_win
*focus_child
; /* focus on child transient */
457 struct swm_geometry g
; /* current geometry */
458 struct swm_geometry g_prev
; /* prev configured geometry */
459 struct swm_geometry g_float
; /* region coordinates */
460 bool g_floatvalid
; /* g_float geometry validity */
465 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
472 struct workspace
*ws
; /* always valid */
473 struct swm_screen
*s
; /* always valid, never changes */
475 xcb_icccm_get_wm_class_reply_t ch
;
476 xcb_icccm_wm_hints_t hints
;
481 TAILQ_HEAD(ws_win_list
, ws_win
);
482 TAILQ_HEAD(ws_win_stack
, ws_win
);
486 TAILQ_ENTRY(pid_e
) entry
;
490 TAILQ_HEAD(pid_list
, pid_e
);
491 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
493 /* layout handlers */
494 void stack(struct swm_region
*);
495 void vertical_config(struct workspace
*, int);
496 void vertical_stack(struct workspace
*, struct swm_geometry
*);
497 void horizontal_config(struct workspace
*, int);
498 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
499 void max_stack(struct workspace
*, struct swm_geometry
*);
500 void plain_stacker(struct workspace
*);
501 void fancy_stacker(struct workspace
*);
504 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
505 void (*l_config
)(struct workspace
*, int);
507 #define SWM_L_FOCUSPREV (1<<0)
508 #define SWM_L_MAPONFOCUS (1<<1)
509 void (*l_string
)(struct workspace
*);
511 /* stack, configure */
512 { vertical_stack
, vertical_config
, 0, plain_stacker
},
513 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
515 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
516 { NULL
, NULL
, 0, NULL
},
519 /* position of max_stack mode in the layouts array, index into layouts! */
520 #define SWM_V_STACK (0)
521 #define SWM_H_STACK (1)
522 #define SWM_MAX_STACK (2)
524 #define SWM_H_SLICE (32)
525 #define SWM_V_SLICE (32)
527 /* define work spaces */
529 int idx
; /* workspace index */
530 char *name
; /* workspace name */
531 bool always_raise
; /* raise windows on focus */
532 bool bar_enabled
; /* bar visibility */
533 struct layout
*cur_layout
; /* current layout handlers */
534 struct ws_win
*focus
; /* may be NULL */
535 struct ws_win
*focus_prev
; /* may be NULL */
536 struct ws_win
*focus_pending
; /* may be NULL */
537 struct ws_win
*raised
; /* may be NULL */
538 struct swm_region
*r
; /* may be NULL */
539 struct swm_region
*old_r
; /* may be NULL */
540 struct ws_win_list winlist
; /* list of windows in ws */
541 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
542 struct ws_win_stack stack
; /* stacking order */
543 int state
; /* mapping state */
544 char stacker
[10]; /* display stacker and layout */
548 int horizontal_msize
;
550 int horizontal_stacks
;
551 bool horizontal_flip
;
561 SWM_WS_STATE_MAPPING
,
567 SWM_S_COLOR_BAR_BORDER
,
568 SWM_S_COLOR_BAR_BORDER_UNFOCUS
,
569 SWM_S_COLOR_BAR_FONT
,
571 SWM_S_COLOR_FOCUS_MAXIMIZED
,
573 SWM_S_COLOR_UNFOCUS_MAXIMIZED
,
577 /* physical screen mapping */
578 #define SWM_WS_MAX (22) /* hard limit */
579 int workspace_limit
= 10; /* soft limit */
582 int idx
; /* screen index */
583 struct swm_region_list rl
; /* list of regions on this screen */
584 struct swm_region_list orl
; /* list of old regions */
586 struct workspace ws
[SWM_WS_MAX
];
587 struct swm_region
*r_focus
;
594 } c
[SWM_S_COLOR_MAX
];
596 xcb_gcontext_t bar_gc
;
599 struct swm_screen
*screens
;
601 /* args to functions */
604 #define SWM_ARG_ID_FOCUSNEXT (0)
605 #define SWM_ARG_ID_FOCUSPREV (1)
606 #define SWM_ARG_ID_FOCUSMAIN (2)
607 #define SWM_ARG_ID_FOCUSURGENT (3)
608 #define SWM_ARG_ID_SWAPNEXT (10)
609 #define SWM_ARG_ID_SWAPPREV (11)
610 #define SWM_ARG_ID_SWAPMAIN (12)
611 #define SWM_ARG_ID_MOVELAST (13)
612 #define SWM_ARG_ID_MASTERSHRINK (20)
613 #define SWM_ARG_ID_MASTERGROW (21)
614 #define SWM_ARG_ID_MASTERADD (22)
615 #define SWM_ARG_ID_MASTERDEL (23)
616 #define SWM_ARG_ID_FLIPLAYOUT (24)
617 #define SWM_ARG_ID_STACKRESET (30)
618 #define SWM_ARG_ID_STACKINIT (31)
619 #define SWM_ARG_ID_STACKBALANCE (32)
620 #define SWM_ARG_ID_CYCLEWS_UP (40)
621 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
622 #define SWM_ARG_ID_CYCLERG_UP (42)
623 #define SWM_ARG_ID_CYCLERG_DOWN (43)
624 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
625 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
626 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
627 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
628 #define SWM_ARG_ID_STACKINC (50)
629 #define SWM_ARG_ID_STACKDEC (51)
630 #define SWM_ARG_ID_DONTCENTER (70)
631 #define SWM_ARG_ID_CENTER (71)
632 #define SWM_ARG_ID_KILLWINDOW (80)
633 #define SWM_ARG_ID_DELETEWINDOW (81)
634 #define SWM_ARG_ID_WIDTHGROW (90)
635 #define SWM_ARG_ID_WIDTHSHRINK (91)
636 #define SWM_ARG_ID_HEIGHTGROW (92)
637 #define SWM_ARG_ID_HEIGHTSHRINK (93)
638 #define SWM_ARG_ID_MOVEUP (100)
639 #define SWM_ARG_ID_MOVEDOWN (101)
640 #define SWM_ARG_ID_MOVELEFT (102)
641 #define SWM_ARG_ID_MOVERIGHT (103)
642 #define SWM_ARG_ID_BAR_TOGGLE (110)
643 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
644 #define SWM_ARG_ID_CYCLERG_MOVE_UP (112)
645 #define SWM_ARG_ID_CYCLERG_MOVE_DOWN (113)
651 TAILQ_ENTRY(quirk
) entry
;
652 char *class; /* WM_CLASS:class */
653 char *instance
; /* WM_CLASS:instance */
654 char *name
; /* WM_NAME */
656 regex_t regex_instance
;
659 int ws
; /* Initial workspace. */
660 #define SWM_Q_FLOAT (1<<0) /* Float this window. */
661 #define SWM_Q_TRANSSZ (1<<1) /* Transient window size too small. */
662 #define SWM_Q_ANYWHERE (1<<2) /* Don't position this window */
663 #define SWM_Q_XTERM_FONTADJ (1<<3) /* Adjust xterm fonts when resizing. */
664 #define SWM_Q_FULLSCREEN (1<<4) /* Remove border when fullscreen. */
665 #define SWM_Q_FOCUSPREV (1<<5) /* Focus on caller. */
666 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
667 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
668 #define SWM_Q_OBEYAPPFOCUSREQ (1<<8) /* Focus when applications ask. */
669 #define SWM_Q_IGNOREPID (1<<9) /* Ignore PID when determining ws. */
670 #define SWM_Q_IGNORESPAWNWS (1<<10) /* Ignore _SWM_WS when managing win. */
671 #define SWM_Q_NOFOCUSCYCLE (1<<11) /* Remove from normal focus cycle. */
672 #define SWM_Q_MINIMALBORDER (1<<12) /* No border when floating/unfocused. */
674 TAILQ_HEAD(quirk_list
, quirk
);
675 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
678 * Supported EWMH hints should be added to
679 * both the enum and the ewmh array
685 _NET_CURRENT_DESKTOP
,
686 _NET_DESKTOP_GEOMETRY
,
688 _NET_DESKTOP_VIEWPORT
,
689 _NET_MOVERESIZE_WINDOW
,
690 _NET_NUMBER_OF_DESKTOPS
,
691 _NET_REQUEST_FRAME_EXTENTS
,
693 _NET_WM_ACTION_ABOVE
,
694 _NET_WM_ACTION_CLOSE
,
695 _NET_WM_ACTION_FULLSCREEN
,
697 _NET_WM_ACTION_RESIZE
,
698 _NET_WM_ALLOWED_ACTIONS
,
700 _NET_WM_FULL_PLACEMENT
,
704 _NET_WM_STATE_FULLSCREEN
,
705 _NET_WM_STATE_HIDDEN
,
706 _NET_WM_STATE_MAXIMIZED_VERT
,
707 _NET_WM_STATE_MAXIMIZED_HORZ
,
708 _NET_WM_STATE_SKIP_PAGER
,
709 _NET_WM_STATE_SKIP_TASKBAR
,
711 _NET_WM_WINDOW_TYPE_DIALOG
,
712 _NET_WM_WINDOW_TYPE_DOCK
,
713 _NET_WM_WINDOW_TYPE_NORMAL
,
714 _NET_WM_WINDOW_TYPE_SPLASH
,
715 _NET_WM_WINDOW_TYPE_TOOLBAR
,
716 _NET_WM_WINDOW_TYPE_UTILITY
,
717 _SWM_WM_STATE_MANUAL
,
724 } ewmh
[SWM_EWMH_HINT_MAX
] = {
725 /* must be in same order as in the enum */
726 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
727 {"_NET_CLIENT_LIST", XCB_ATOM_NONE
},
728 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
729 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE
},
730 {"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE
},
731 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE
},
732 {"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE
},
733 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
734 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE
},
735 {"_NET_REQUEST_FRAME_EXTENTS", XCB_ATOM_NONE
},
736 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE
},
737 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
738 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
739 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
740 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
741 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
742 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
743 {"_NET_WM_DESKTOP", XCB_ATOM_NONE
},
744 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE
},
745 {"_NET_WM_NAME", XCB_ATOM_NONE
},
746 {"_NET_WM_STATE", XCB_ATOM_NONE
},
747 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
748 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
749 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
750 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
751 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
752 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
753 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
754 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
755 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
756 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
757 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
758 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
759 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
760 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
761 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
764 /* EWMH source type */
766 EWMH_SOURCE_TYPE_NONE
= 0,
767 EWMH_SOURCE_TYPE_NORMAL
= 1,
768 EWMH_SOURCE_TYPE_OTHER
= 2,
775 XC_BOTTOM_LEFT_CORNER
,
776 XC_BOTTOM_RIGHT_CORNER
,
784 char *name
; /* Name used by Xcursor .*/
785 uint8_t cf_char
; /* cursorfont index. */
787 } cursors
[XC_MAX
] = {
788 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
789 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
790 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
791 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
792 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
793 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
794 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
797 #define SWM_SPAWN_OPTIONAL 0x1
801 TAILQ_ENTRY(spawn_prog
) entry
;
807 TAILQ_HEAD(spawn_list
, spawn_prog
);
808 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
814 /* User callable function IDs. */
940 /* SWM_DEBUG actions MUST be here: */
953 BINDING_F_REPLAY
= 0x1,
957 RB_ENTRY(binding
) entry
;
958 uint16_t mod
; /* Modifier Mask. */
959 enum binding_type type
; /* Key or Button. */
960 uint32_t value
; /* KeySym or Button Index. */
961 enum actionid action
; /* Action Identifier. */
965 RB_HEAD(binding_tree
, binding
);
967 /* function prototypes */
968 void adjust_font(struct ws_win
*);
969 char *argsep(char **);
970 void bar_cleanup(struct swm_region
*);
971 void bar_extra_setup(void);
972 void bar_extra_stop(void);
973 int bar_extra_update(void);
974 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
975 void bar_fmt_expand(char *, size_t);
976 void bar_draw(struct swm_bar
*);
977 void bar_print(struct swm_region
*, const char *);
978 void bar_print_legacy(struct swm_region
*, const char *);
979 void bar_replace(char *, char *, struct swm_region
*, size_t);
980 void bar_replace_pad(char *, int *, size_t);
981 char *bar_replace_seq(char *, char *, struct swm_region
*, size_t *, size_t);
982 void bar_setup(struct swm_region
*);
983 void bar_toggle(struct binding
*, struct swm_region
*, union arg
*);
984 void bar_urgent(char *, size_t);
985 void bar_window_class(char *, size_t, struct swm_region
*);
986 void bar_window_class_instance(char *, size_t, struct swm_region
*);
987 void bar_window_float(char *, size_t, struct swm_region
*);
988 void bar_window_instance(char *, size_t, struct swm_region
*);
989 void bar_window_name(char *, size_t, struct swm_region
*);
990 void bar_window_state(char *, size_t, struct swm_region
*);
991 void bar_workspace_name(char *, size_t, struct swm_region
*);
992 int binding_cmp(struct binding
*, struct binding
*);
993 void binding_insert(uint16_t, enum binding_type
, uint32_t, enum actionid
,
994 uint32_t, const char *);
995 struct binding
*binding_lookup(uint16_t, enum binding_type
, uint32_t);
996 void binding_remove(struct binding
*);
997 void buttonpress(xcb_button_press_event_t
*);
998 void buttonrelease(xcb_button_release_event_t
*);
999 void center_pointer(struct swm_region
*);
1000 void check_conn(void);
1001 void clear_bindings(void);
1002 void clear_keybindings(void);
1003 int clear_maximized(struct workspace
*);
1004 void clear_quirks(void);
1005 void clear_spawns(void);
1006 void clientmessage(xcb_client_message_event_t
*);
1007 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
1008 int conf_load(const char *, int);
1009 void configurenotify(xcb_configure_notify_event_t
*);
1010 void configurerequest(xcb_configure_request_event_t
*);
1011 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
1012 void constrain_window(struct ws_win
*, struct swm_geometry
*, int *);
1013 int count_win(struct workspace
*, bool);
1014 void cursors_cleanup(void);
1015 void cursors_load(void);
1016 void custom_region(const char *);
1017 void cycle_layout(struct binding
*, struct swm_region
*, union arg
*);
1018 void cyclerg(struct binding
*, struct swm_region
*, union arg
*);
1019 void cyclews(struct binding
*, struct swm_region
*, union arg
*);
1021 void debug_refresh(struct ws_win
*);
1023 void debug_toggle(struct binding
*, struct swm_region
*, union arg
*);
1024 void destroynotify(xcb_destroy_notify_event_t
*);
1025 void dumpwins(struct binding
*, struct swm_region
*, union arg
*);
1026 int enable_wm(void);
1027 void enternotify(xcb_enter_notify_event_t
*);
1028 void event_drain(uint8_t);
1029 void event_error(xcb_generic_error_t
*);
1030 void event_handle(xcb_generic_event_t
*);
1031 void ewmh_apply_flags(struct ws_win
*, uint32_t);
1032 void ewmh_autoquirk(struct ws_win
*);
1033 void ewmh_get_desktop_names(void);
1034 void ewmh_get_wm_state(struct ws_win
*);
1035 void ewmh_update_actions(struct ws_win
*);
1036 void ewmh_update_client_list(void);
1037 void ewmh_update_current_desktop(void);
1038 void ewmh_update_desktop_names(void);
1039 void ewmh_update_desktops(void);
1040 void ewmh_change_wm_state(struct ws_win
*, xcb_atom_t
, long);
1041 void ewmh_update_wm_state(struct ws_win
*);
1042 char *expand_tilde(const char *);
1043 void expose(xcb_expose_event_t
*);
1044 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
1045 struct swm_bar
*find_bar(xcb_window_t
);
1046 struct ws_win
*find_frame_window(xcb_window_t
);
1047 struct pid_e
*find_pid(pid_t
);
1048 struct swm_region
*find_region(xcb_window_t
);
1049 struct ws_win
*find_unmanaged_window(xcb_window_t
);
1050 struct ws_win
*find_window(xcb_window_t
);
1051 void floating_toggle(struct binding
*, struct swm_region
*, union arg
*);
1052 void focus(struct binding
*, struct swm_region
*, union arg
*);
1053 void focus_flush(void);
1054 void focus_pointer(struct binding
*, struct swm_region
*, union arg
*);
1055 void focus_region(struct swm_region
*);
1056 void focus_win(struct ws_win
*);
1057 void focusin(xcb_focus_in_event_t
*);
1059 void focusout(xcb_focus_out_event_t
*);
1061 void focusrg(struct binding
*, struct swm_region
*, union arg
*);
1062 void fontset_init(void);
1063 void free_window(struct ws_win
*);
1064 xcb_atom_t
get_atom_from_string(const char *);
1066 char *get_atom_name(xcb_atom_t
);
1068 struct ws_win
*get_focus_magic(struct ws_win
*);
1069 struct ws_win
*get_focus_prev(struct ws_win
*);
1070 xcb_generic_event_t
*get_next_event(bool);
1072 char *get_notify_detail_label(uint8_t);
1073 char *get_notify_mode_label(uint8_t);
1075 struct ws_win
*get_pointer_win(xcb_window_t
);
1076 struct ws_win
*get_region_focus(struct swm_region
*);
1077 int get_region_index(struct swm_region
*);
1078 xcb_screen_t
*get_screen(int);
1079 int get_screen_count(void);
1081 char *get_source_type_label(uint32_t);
1082 char *get_stack_mode_name(uint8_t);
1083 char *get_state_mask_label(uint16_t);
1085 int32_t get_swm_ws(xcb_window_t
);
1086 bool get_urgent(struct ws_win
*);
1088 char *get_win_input_model(struct ws_win
*);
1090 char *get_win_name(xcb_window_t
);
1091 uint8_t get_win_state(xcb_window_t
);
1092 void get_wm_protocols(struct ws_win
*);
1093 int get_ws_idx(struct ws_win
*);
1094 void grab_windows(void);
1095 void grabbuttons(void);
1096 void grabkeys(void);
1097 void iconify(struct binding
*, struct swm_region
*, union arg
*);
1098 bool isxlfd(char *);
1099 bool keybindreleased(struct binding
*, xcb_key_release_event_t
*);
1100 void keypress(xcb_key_press_event_t
*);
1101 void keyrelease(xcb_key_release_event_t
*);
1102 bool keyrepeating(xcb_key_release_event_t
*);
1103 void kill_bar_extra_atexit(void);
1104 void kill_refs(struct ws_win
*);
1106 void leavenotify(xcb_leave_notify_event_t
*);
1108 void load_float_geom(struct ws_win
*);
1109 void lower_window(struct ws_win
*);
1110 struct ws_win
*manage_window(xcb_window_t
, int, bool);
1111 void map_window(struct ws_win
*);
1112 void mapnotify(xcb_map_notify_event_t
*);
1113 void mappingnotify(xcb_mapping_notify_event_t
*);
1114 void maprequest(xcb_map_request_event_t
*);
1115 void maximize_toggle(struct binding
*, struct swm_region
*, union arg
*);
1116 void motionnotify(xcb_motion_notify_event_t
*);
1117 void move(struct binding
*, struct swm_region
*, union arg
*);
1118 void move_win(struct ws_win
*, struct binding
*, int);
1119 uint32_t name_to_pixel(int, const char *);
1120 void name_workspace(struct binding
*, struct swm_region
*, union arg
*);
1121 void new_region(struct swm_screen
*, int, int, int, int);
1122 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1123 int parsebinding(const char *, uint16_t *, enum binding_type
*, uint32_t *,
1125 int parsequirks(const char *, uint32_t *, int *);
1126 void pressbutton(struct binding
*, struct swm_region
*, union arg
*);
1127 void priorws(struct binding
*, struct swm_region
*, union arg
*);
1129 void print_win_geom(xcb_window_t
);
1131 void propertynotify(xcb_property_notify_event_t
*);
1132 void put_back_event(xcb_generic_event_t
*);
1133 void quirk_free(struct quirk
*);
1134 void quirk_insert(const char *, const char *, const char *, uint32_t, int);
1135 void quirk_remove(struct quirk
*);
1136 void quirk_replace(struct quirk
*, const char *, const char *, const char *,
1138 void quit(struct binding
*, struct swm_region
*, union arg
*);
1139 void raise_focused(struct binding
*, struct swm_region
*, union arg
*);
1140 void raise_toggle(struct binding
*, struct swm_region
*, union arg
*);
1141 void raise_window(struct ws_win
*);
1142 void region_containment(struct ws_win
*, struct swm_region
*, int);
1143 struct swm_region
*region_under(struct swm_screen
*, int, int);
1144 void regionize(struct ws_win
*, int, int);
1145 void reparent_window(struct ws_win
*);
1146 void reparentnotify(xcb_reparent_notify_event_t
*);
1147 void resize(struct binding
*, struct swm_region
*, union arg
*);
1148 void resize_win(struct ws_win
*, struct binding
*, int);
1149 void restart(struct binding
*, struct swm_region
*, union arg
*);
1150 struct swm_region
*root_to_region(xcb_window_t
, int);
1151 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1152 void scan_randr(int);
1153 void search_do_resp(void);
1154 void search_resp_name_workspace(const char *, size_t);
1155 void search_resp_search_window(const char *);
1156 void search_resp_search_workspace(const char *);
1157 void search_resp_uniconify(const char *, size_t);
1158 void search_win(struct binding
*, struct swm_region
*, union arg
*);
1159 void search_win_cleanup(void);
1160 void search_workspace(struct binding
*, struct swm_region
*, union arg
*);
1161 void send_to_rg(struct binding
*, struct swm_region
*, union arg
*);
1162 void send_to_ws(struct binding
*, struct swm_region
*, union arg
*);
1163 void send_to_ws_relative(struct binding
*, struct swm_region
*, union arg
*);
1164 void set_region(struct swm_region
*);
1165 int setautorun(const char *, const char *, int);
1166 void setbinding(uint16_t, enum binding_type
, uint32_t, enum actionid
,
1167 uint32_t, const char *);
1168 int setconfbinding(const char *, const char *, int);
1169 int setconfcolor(const char *, const char *, int);
1170 int setconfmodkey(const char *, const char *, int);
1171 int setconfquirk(const char *, const char *, int);
1172 int setconfregion(const char *, const char *, int);
1173 int setconfspawn(const char *, const char *, int);
1174 int setconfvalue(const char *, const char *, int);
1175 int setkeymapping(const char *, const char *, int);
1176 int setlayout(const char *, const char *, int);
1177 void setquirk(const char *, const char *, const char *, uint32_t, int);
1178 void setscreencolor(const char *, int, int);
1179 void setspawn(const char *, const char *, int);
1180 void setup_btnbindings(void);
1181 void setup_ewmh(void);
1182 void setup_globals(void);
1183 void setup_keybindings(void);
1184 void setup_quirks(void);
1185 void setup_screens(void);
1186 void setup_spawn(void);
1187 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1188 void set_win_state(struct ws_win
*, uint8_t);
1189 void shutdown_cleanup(void);
1191 void socket_setnonblock(int);
1192 void sort_windows(struct ws_win_list
*);
1193 void spawn(int, union arg
*, bool);
1194 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1195 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1196 void spawn_insert(const char *, const char *, int);
1197 struct spawn_prog
*spawn_find(const char *);
1198 void spawn_remove(struct spawn_prog
*);
1199 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1200 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1201 void stack_config(struct binding
*, struct swm_region
*, union arg
*);
1202 void stack_master(struct workspace
*, struct swm_geometry
*, int, bool);
1203 void store_float_geom(struct ws_win
*);
1204 char *strdupsafe(const char *);
1205 void swapwin(struct binding
*, struct swm_region
*, union arg
*);
1206 void switchws(struct binding
*, struct swm_region
*, union arg
*);
1207 void teardown_ewmh(void);
1208 void unescape_selector(char *);
1209 void unfocus_win(struct ws_win
*);
1210 void uniconify(struct binding
*, struct swm_region
*, union arg
*);
1211 void unmanage_window(struct ws_win
*);
1212 void unmap_all(void);
1213 void unmap_window(struct ws_win
*);
1214 void unmapnotify(xcb_unmap_notify_event_t
*);
1215 void unparent_window(struct ws_win
*);
1216 void update_floater(struct ws_win
*);
1217 void update_modkey(uint16_t);
1218 void update_win_stacking(struct ws_win
*);
1219 void update_window(struct ws_win
*);
1220 void draw_frame(struct ws_win
*);
1221 void update_wm_state(struct ws_win
*win
);
1222 void updatenumlockmask(void);
1223 void validate_spawns(void);
1224 int validate_win(struct ws_win
*);
1225 int validate_ws(struct workspace
*);
1226 void version(struct binding
*, struct swm_region
*, union arg
*);
1227 void win_to_ws(struct ws_win
*, int, bool);
1228 pid_t
window_get_pid(xcb_window_t
);
1229 void wkill(struct binding
*, struct swm_region
*, union arg
*);
1230 void update_ws_stack(struct workspace
*);
1231 void xft_init(struct swm_region
*);
1232 void _add_startup_exception(const char *, va_list);
1233 void add_startup_exception(const char *, ...);
1235 RB_PROTOTYPE(binding_tree
, binding
, entry
, binding_cmp
);
1236 RB_GENERATE(binding_tree
, binding
, entry
, binding_cmp
);
1237 struct binding_tree bindings
;
1242 xcb_font_t cf
= XCB_NONE
;
1245 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1246 /* try to load Xcursor first. */
1247 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1250 /* fallback to cursorfont. */
1251 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1252 if (cf
== XCB_NONE
) {
1253 cf
= xcb_generate_id(conn
);
1254 xcb_open_font(conn
, cf
, strlen("cursor"),
1258 cursors
[i
].cid
= xcb_generate_id(conn
);
1259 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1260 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1261 0xffff, 0xffff, 0xffff);
1267 xcb_close_font(conn
, cf
);
1271 cursors_cleanup(void)
1274 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1275 xcb_free_cursor(conn
, cursors
[i
].cid
);
1279 expand_tilde(const char *s
)
1281 struct passwd
*ppwd
;
1289 errx(1, "expand_tilde: NULL string.");
1292 result
= strdup(sc
);
1298 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1299 errx(1, "expand_tilde: sysconf");
1301 if ((user
= calloc(1, max
+ 1)) == NULL
)
1302 errx(1, "expand_tilde: calloc");
1304 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1309 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1313 result
= strdup(sc
);
1315 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1319 errx(1, "expand_tilde: failed to allocate memory.");
1325 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1327 unsigned int tmpr
, tmpg
, tmpb
;
1329 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1332 *rr
= RGB_8_TO_16(tmpr
);
1333 *gg
= RGB_8_TO_16(tmpg
);
1334 *bb
= RGB_8_TO_16(tmpb
);
1340 get_screen(int screen
)
1342 const xcb_setup_t
*r
;
1343 xcb_screen_iterator_t iter
;
1345 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1346 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1350 iter
= xcb_setup_roots_iterator(r
);
1351 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1359 get_screen_count(void)
1361 const xcb_setup_t
*r
;
1363 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1364 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1368 return xcb_setup_roots_length(r
);
1372 get_region_index(struct swm_region
*r
)
1374 struct swm_region
*rr
;
1380 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1395 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1396 event_drain(XCB_ENTER_NOTIFY
);
1402 get_atom_from_string(const char *str
)
1404 xcb_intern_atom_cookie_t c
;
1405 xcb_intern_atom_reply_t
*r
;
1408 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1409 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1417 return (XCB_ATOM_NONE
);
1421 get_wm_protocols(struct ws_win
*win
) {
1423 xcb_icccm_get_wm_protocols_reply_t wpr
;
1425 if (xcb_icccm_get_wm_protocols_reply(conn
,
1426 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
1428 for (i
= 0; i
< (int)wpr
.atoms_len
; i
++) {
1429 if (wpr
.atoms
[i
] == a_takefocus
)
1430 win
->take_focus
= true;
1431 if (wpr
.atoms
[i
] == a_delete
)
1432 win
->can_delete
= true;
1434 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
1441 xcb_window_t root
, win
;
1442 int i
, j
, num_screens
;
1444 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1445 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1447 num_screens
= get_screen_count();
1448 for (i
= 0; i
< num_screens
; i
++) {
1449 root
= screens
[i
].root
;
1451 /* Set up _NET_SUPPORTING_WM_CHECK. */
1452 win
= xcb_generate_id(conn
);
1453 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
1454 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1455 XCB_COPY_FROM_PARENT
, 0, NULL
);
1457 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
1458 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1459 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1460 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1462 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1463 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1464 8, strlen("spectrwm"), "spectrwm");
1466 /* Report supported atoms */
1467 xcb_delete_property(conn
, root
, a_net_supported
);
1468 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1469 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, root
,
1470 a_net_supported
, XCB_ATOM_ATOM
, 32, 1,
1475 ewmh_update_desktops();
1476 ewmh_get_desktop_names();
1484 xcb_get_property_cookie_t pc
;
1485 xcb_get_property_reply_t
*pr
;
1487 num_screens
= get_screen_count();
1489 for (i
= 0; i
< num_screens
; i
++) {
1490 /* Get the support check window and destroy it */
1491 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, a_net_wm_check
,
1492 XCB_ATOM_WINDOW
, 0, 1);
1493 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1496 if (pr
->format
== a_net_wm_check
) {
1497 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1499 xcb_destroy_window(conn
, id
);
1500 xcb_delete_property(conn
, screens
[i
].root
,
1502 xcb_delete_property(conn
, screens
[i
].root
,
1510 ewmh_autoquirk(struct ws_win
*win
)
1512 xcb_get_property_reply_t
*r
;
1513 xcb_get_property_cookie_t c
;
1517 c
= xcb_get_property(conn
, 0, win
->id
,
1518 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1519 r
= xcb_get_property_reply(conn
, c
, NULL
);
1523 type
= xcb_get_property_value(r
);
1524 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1526 for (i
= 0; i
< n
; i
++) {
1527 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1529 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1530 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1531 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1532 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1535 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1536 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1537 win
->quirks
= SWM_Q_FLOAT
;
1545 ewmh_update_actions(struct ws_win
*win
)
1547 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1553 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1556 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1557 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1558 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1561 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1562 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1565 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1566 #define _NET_WM_STATE_ADD 1 /* add/set property */
1567 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1570 ewmh_change_wm_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1577 name
= get_atom_name(state
);
1578 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: win %#x, state: %s, "
1579 "action: %ld\n", WINID(win
), name
, action
);
1585 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1586 flag
= EWMH_F_FULLSCREEN
;
1587 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1588 flag
= EWMH_F_ABOVE
;
1589 else if (state
== ewmh
[_NET_WM_STATE_HIDDEN
].atom
)
1590 flag
= EWMH_F_HIDDEN
;
1591 else if (state
== ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
||
1592 state
== ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
)
1593 flag
= EWMH_F_MAXIMIZED
;
1594 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1595 flag
= SWM_F_MANUAL
;
1596 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1597 flag
= EWMH_F_SKIP_PAGER
;
1598 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1599 flag
= EWMH_F_SKIP_TASKBAR
;
1601 /* Disallow unfloating transients. */
1602 if (TRANS(win
) && flag
== EWMH_F_ABOVE
)
1605 new_flags
= win
->ewmh_flags
;
1608 case _NET_WM_STATE_REMOVE
:
1611 case _NET_WM_STATE_ADD
:
1614 case _NET_WM_STATE_TOGGLE
:
1619 ewmh_apply_flags(win
, new_flags
);
1622 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: done.\n");
1626 ewmh_apply_flags(struct ws_win
*win
, uint32_t pending
)
1628 struct workspace
*ws
;
1631 changed
= win
->ewmh_flags
^ pending
;
1635 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: pending: %d\n", pending
);
1637 win
->ewmh_flags
= pending
;
1640 if (changed
& EWMH_F_HIDDEN
) {
1642 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1643 ws
->focus_pending
= get_focus_prev(win
);
1648 /* Reload floating geometry in case region changed. */
1650 load_float_geom(win
);
1652 /* The window is no longer iconic, prepare focus. */
1653 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1654 ws
->focus_pending
= get_focus_magic(win
);
1659 if (changed
& EWMH_F_ABOVE
) {
1660 if (ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1662 load_float_geom(win
);
1663 else if (!MAXIMIZED(win
))
1664 store_float_geom(win
);
1666 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1667 changed
&= ~EWMH_F_MAXIMIZED
;
1671 win
->ewmh_flags
^= EWMH_F_ABOVE
& pending
;
1675 if (changed
& EWMH_F_MAXIMIZED
) {
1676 /* VERT and/or HORZ changed. */
1678 if (!MAXIMIZED(win
))
1679 load_float_geom(win
);
1681 store_float_geom(win
);
1684 if (MAXIMIZED(win
)) {
1685 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
1686 ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1690 ws
->focus_pending
= win
;
1698 if (changed
& EWMH_F_FULLSCREEN
) {
1699 if (FULLSCREEN(win
)) {
1700 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
1704 ws
->focus_pending
= win
;
1707 load_float_geom(win
);
1710 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1714 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: done.\n");
1718 ewmh_update_wm_state(struct ws_win
*win
) {
1719 xcb_atom_t vals
[SWM_EWMH_ACTION_COUNT_MAX
];
1723 vals
[n
++] = ewmh
[_NET_WM_STATE_HIDDEN
].atom
;
1724 if (FULLSCREEN(win
))
1725 vals
[n
++] = ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
;
1726 if (MAXIMIZED_VERT(win
))
1727 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
;
1728 if (MAXIMIZED_HORZ(win
))
1729 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
;
1730 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1731 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
;
1732 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1733 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
;
1734 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1735 vals
[n
++] = ewmh
[_NET_WM_STATE_ABOVE
].atom
;
1736 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1737 vals
[n
++] = ewmh
[_SWM_WM_STATE_MANUAL
].atom
;
1740 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1741 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, n
, vals
);
1743 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1747 ewmh_get_wm_state(struct ws_win
*win
)
1750 xcb_get_property_cookie_t c
;
1751 xcb_get_property_reply_t
*r
;
1757 win
->ewmh_flags
= 0;
1759 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1760 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1761 r
= xcb_get_property_reply(conn
, c
, NULL
);
1765 states
= xcb_get_property_value(r
);
1766 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1768 for (i
= 0; i
< n
; i
++)
1769 ewmh_change_wm_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1777 dumpwins(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
1781 xcb_get_window_attributes_cookie_t c
;
1782 xcb_get_window_attributes_reply_t
*wa
;
1784 /* suppress unused warning since var is needed */
1788 if (r
->ws
== NULL
) {
1789 DPRINTF("dumpwins: invalid workspace\n");
1793 DPRINTF("=== managed window list ws %02d ===\n", r
->ws
->idx
);
1794 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
) {
1795 state
= get_win_state(w
->id
);
1796 c
= xcb_get_window_attributes(conn
, w
->id
);
1797 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1799 DPRINTF("win %#x (%#x), map_state: %d, state: %u, "
1800 "transient: %#x\n", w
->frame
, w
->id
, wa
->map_state
,
1801 state
, w
->transient
);
1804 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1808 DPRINTF("=== stacking order (top down) === \n");
1809 TAILQ_FOREACH(w
, &r
->ws
->stack
, stack_entry
) {
1810 DPRINTF("win %#x (%#x), fs: %s, maximized: %s, above: %s, "
1811 "iconic: %s\n", w
->frame
, w
->id
, YESNO(FULLSCREEN(w
)),
1812 YESNO(MAXIMIZED(w
)), YESNO(ABOVE(w
)), YESNO(ICONIC(w
)));
1815 DPRINTF("===== unmanaged window list =====\n");
1816 TAILQ_FOREACH(w
, &r
->ws
->unmanagedlist
, entry
) {
1817 state
= get_win_state(w
->id
);
1818 c
= xcb_get_window_attributes(conn
, w
->id
);
1819 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1821 DPRINTF("win %#x, map_state: %d, state: %u, "
1822 "transient: %#x\n", w
->id
, wa
->map_state
,
1823 state
, w
->transient
);
1826 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1830 DPRINTF("=================================\n");
1834 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
1837 int num_screens
, i
, j
;
1839 /* Suppress warnings. */
1844 DNPRINTF(SWM_D_MISC
, "debug_toggle\n");
1846 debug_enabled
= !debug_enabled
;
1848 num_screens
= get_screen_count();
1849 for (i
= 0; i
< num_screens
; i
++)
1850 for (j
= 0; j
< workspace_limit
; j
++)
1851 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1858 debug_refresh(struct ws_win
*win
)
1865 XRectangle l_ibox
, l_lbox
;
1866 xcb_rectangle_t rect
;
1868 uint32_t wc
[4], mask
, width
, height
, gcv
[1];
1871 xcb_screen_t
*screen
;
1873 if (debug_enabled
) {
1874 /* Create debug window if it doesn't exist. */
1875 if (win
->debug
== XCB_WINDOW_NONE
) {
1876 if ((screen
= get_screen(win
->s
->idx
)) == NULL
)
1877 errx(1, "ERROR: can't get screen %d.",
1880 win
->debug
= xcb_generate_id(conn
);
1881 wc
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1882 wc
[1] = win
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
1883 wc
[2] = screen
->default_colormap
;
1885 xcb_create_window(conn
, screen
->root_depth
, win
->debug
,
1886 win
->frame
, 0, 0, 10, 10, 1,
1887 XCB_WINDOW_CLASS_INPUT_OUTPUT
, screen
->root_visual
,
1888 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
|
1889 XCB_CW_COLORMAP
, wc
);
1891 xcb_map_window(conn
, win
->debug
);
1894 /* Determine workspace window list index. */
1896 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
1902 /* Determine stacking index (top down). */
1904 TAILQ_FOREACH(w
, &win
->ws
->stack
, stack_entry
) {
1910 if (asprintf(&s
, "%#x f:%#x wl:%d s:%d im:%s", win
->id
,
1911 win
->frame
, widx
, sidx
, get_win_input_model(win
)) == -1)
1916 /* Update window to an appropriate dimension. */
1917 if (bar_font_legacy
) {
1918 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
1919 width
= l_lbox
.width
+ 4;
1920 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
1922 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
1924 width
= info
.width
+ 4;
1925 height
= bar_font
->height
+ 4;
1928 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
1929 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
;
1931 wc
[0] = wc
[1] = border_width
;
1938 xcb_configure_window(conn
, win
->debug
, mask
, wc
);
1940 /* Draw a filled rectangle to 'clear' window. */
1944 rect
.height
= height
;
1946 gcv
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1947 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1948 xcb_poly_fill_rectangle(conn
, win
->debug
, win
->s
->bar_gc
, 1,
1952 if (bar_font_legacy
) {
1953 l_gcv
.graphics_exposures
= 0;
1954 l_draw
= XCreateGC(display
, win
->debug
, 0, &l_gcv
);
1956 XSetForeground(display
, l_draw
,
1957 win
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
1959 DRAWSTRING(display
, win
->debug
, bar_fs
, l_draw
, 2,
1960 (bar_fs_extents
->max_logical_extent
.height
-
1961 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
1963 XFreeGC(display
, l_draw
);
1965 draw
= XftDrawCreate(display
, win
->debug
,
1966 DefaultVisual(display
, win
->s
->idx
),
1967 DefaultColormap(display
, win
->s
->idx
));
1969 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
1970 (bar_height
+ bar_font
->height
) / 2 -
1971 bar_font
->descent
, (FcChar8
*)s
, len
);
1973 XftDrawDestroy(draw
);
1977 } else if (win
->debug
!= XCB_WINDOW_NONE
) {
1978 xcb_destroy_window(conn
, win
->debug
);
1979 win
->debug
= XCB_WINDOW_NONE
;
1984 dumpwins(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
1992 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
1998 #endif /* SWM_DEBUG */
2003 int saved_errno
, status
;
2006 saved_errno
= errno
;
2010 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
2015 if (errno
!= ECHILD
)
2016 warn("sighdlr: waitpid");
2017 #endif /* SWM_DEBUG */
2020 if (pid
== searchpid
)
2024 if (WIFEXITED(status
)) {
2025 if (WEXITSTATUS(status
) != 0)
2026 warnx("sighdlr: child exit status: %d",
2027 WEXITSTATUS(status
));
2029 warnx("sighdlr: child is terminated "
2031 #endif /* SWM_DEBUG */
2045 errno
= saved_errno
;
2051 struct pid_e
*p
= NULL
;
2053 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
2058 TAILQ_FOREACH(p
, &pidlist
, entry
) {
2067 name_to_pixel(int sidx
, const char *colorname
)
2069 uint32_t result
= 0;
2070 char cname
[32] = "#";
2071 xcb_screen_t
*screen
;
2072 xcb_colormap_t cmap
;
2073 xcb_alloc_color_reply_t
*cr
;
2074 xcb_alloc_named_color_reply_t
*nr
;
2075 uint16_t rr
, gg
, bb
;
2077 screen
= get_screen(sidx
);
2078 cmap
= screen
->default_colormap
;
2080 /* color is in format rgb://rr/gg/bb */
2081 if (strncmp(colorname
, "rgb:", 4) == 0) {
2082 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
2083 warnx("could not parse rgb %s", colorname
);
2085 cr
= xcb_alloc_color_reply(conn
,
2086 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
2092 warnx("color '%s' not found", colorname
);
2095 nr
= xcb_alloc_named_color_reply(conn
,
2096 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
2099 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
2100 nr
= xcb_alloc_named_color_reply(conn
,
2101 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
2108 warnx("color '%s' not found", colorname
);
2115 setscreencolor(const char *val
, int i
, int c
)
2117 if (i
< 0 || i
>= get_screen_count())
2120 screens
[i
].c
[c
].pixel
= name_to_pixel(i
, val
);
2121 free(screens
[i
].c
[c
].name
);
2122 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
2127 fancy_stacker(struct workspace
*ws
)
2129 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2130 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2131 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2132 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
2133 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
2134 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2135 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2136 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
2137 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
2141 plain_stacker(struct workspace
*ws
)
2143 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2144 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2145 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
2146 sizeof ws
->stacker
);
2147 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2148 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
2149 sizeof ws
->stacker
);
2153 custom_region(const char *val
)
2155 unsigned int x
, y
, w
, h
;
2156 int sidx
, num_screens
;
2157 xcb_screen_t
*screen
;
2159 DNPRINTF(SWM_D_CONF
, "custom_region: %s\n", val
);
2161 num_screens
= get_screen_count();
2162 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
2163 errx(1, "invalid custom region, "
2164 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
2165 if (sidx
< 1 || sidx
> num_screens
)
2166 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
2170 if ((screen
= get_screen(sidx
)) == NULL
)
2171 errx(1, "ERROR: can't get screen %d.", sidx
);
2174 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
2176 if (x
> screen
->width_in_pixels
||
2177 y
> screen
->height_in_pixels
||
2178 w
+ x
> screen
->width_in_pixels
||
2179 h
+ y
> screen
->height_in_pixels
) {
2180 warnx("ignoring region %ux%u+%u+%u - not within screen "
2181 "boundaries (%ux%u)", w
, h
, x
, y
,
2182 screen
->width_in_pixels
, screen
->height_in_pixels
);
2186 new_region(&screens
[sidx
], x
, y
, w
, h
);
2190 socket_setnonblock(int fd
)
2194 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
2195 err(1, "fcntl F_GETFL");
2196 flags
|= O_NONBLOCK
;
2197 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
2198 err(1, "fcntl F_SETFL");
2202 bar_print_legacy(struct swm_region
*r
, const char *s
)
2204 xcb_rectangle_t rect
;
2209 XRectangle ibox
, lbox
;
2213 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
2215 switch (bar_justify
) {
2216 case SWM_BAR_JUSTIFY_LEFT
:
2219 case SWM_BAR_JUSTIFY_CENTER
:
2220 x
= (WIDTH(r
) - lbox
.width
) / 2;
2222 case SWM_BAR_JUSTIFY_RIGHT
:
2223 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
2227 if (x
< SWM_BAR_OFFSET
)
2232 rect
.width
= WIDTH(r
->bar
);
2233 rect
.height
= HEIGHT(r
->bar
);
2235 /* clear back buffer */
2236 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2237 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2238 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2240 /* draw back buffer */
2241 gcvd
.graphics_exposures
= 0;
2242 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
2243 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2244 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
2245 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
2247 XFreeGC(display
, draw
);
2250 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2251 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2255 bar_print(struct swm_region
*r
, const char *s
)
2258 xcb_rectangle_t rect
;
2266 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
2268 switch (bar_justify
) {
2269 case SWM_BAR_JUSTIFY_LEFT
:
2272 case SWM_BAR_JUSTIFY_CENTER
:
2273 x
= (WIDTH(r
) - info
.width
) / 2;
2275 case SWM_BAR_JUSTIFY_RIGHT
:
2276 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
2280 if (x
< SWM_BAR_OFFSET
)
2285 rect
.width
= WIDTH(r
->bar
);
2286 rect
.height
= HEIGHT(r
->bar
);
2288 /* clear back buffer */
2289 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2290 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2291 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2293 /* draw back buffer */
2294 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
2295 DefaultVisual(display
, r
->s
->idx
),
2296 DefaultColormap(display
, r
->s
->idx
));
2298 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
2299 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
2302 XftDrawDestroy(draw
);
2305 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2306 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2310 bar_extra_stop(void)
2314 bzero(bar_pipe
, sizeof bar_pipe
);
2317 kill(bar_pid
, SIGTERM
);
2320 strlcpy(bar_ext
, "", sizeof bar_ext
);
2325 bar_window_class(char *s
, size_t sz
, struct swm_region
*r
)
2327 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2329 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
2330 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
2334 bar_window_instance(char *s
, size_t sz
, struct swm_region
*r
)
2336 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2338 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
2339 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
2343 bar_window_class_instance(char *s
, size_t sz
, struct swm_region
*r
)
2345 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2348 bar_window_class(s
, sz
, r
);
2349 strlcat(s
, ":", sz
);
2350 bar_window_instance(s
, sz
, r
);
2354 bar_window_state(char *s
, size_t sz
, struct swm_region
*r
)
2356 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2358 if (MAXIMIZED(r
->ws
->focus
))
2359 strlcat(s
, "(m)", sz
);
2360 else if (ABOVE(r
->ws
->focus
))
2361 strlcat(s
, "(f)", sz
);
2365 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
2369 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2372 title
= get_win_name(r
->ws
->focus
->id
);
2373 strlcat(s
, title
, sz
);
2378 get_urgent(struct ws_win
*win
)
2380 xcb_icccm_wm_hints_t hints
;
2381 xcb_get_property_cookie_t c
;
2382 bool urgent
= false;
2385 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2386 if (xcb_icccm_get_wm_hints_reply(conn
, c
, &hints
, NULL
))
2387 urgent
= xcb_icccm_wm_hints_get_urgency(&hints
);
2394 bar_urgent(char *s
, size_t sz
)
2397 int i
, j
, num_screens
;
2398 bool urgent
[SWM_WS_MAX
];
2401 for (i
= 0; i
< workspace_limit
; i
++)
2404 num_screens
= get_screen_count();
2405 for (i
= 0; i
< num_screens
; i
++)
2406 for (j
= 0; j
< workspace_limit
; j
++)
2407 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2408 if (get_urgent(win
))
2411 for (i
= 0; i
< workspace_limit
; i
++) {
2413 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2415 } else if (!urgent_collapse
) {
2416 strlcat(s
, "- ", sz
);
2419 if(urgent_collapse
&& s
[0])
2420 s
[strlen(s
) - 1] = 0;
2424 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2426 if (r
== NULL
|| r
->ws
== NULL
)
2428 if (r
->ws
->name
!= NULL
)
2429 strlcat(s
, r
->ws
->name
, sz
);
2432 /* build the default bar format according to the defined enabled options */
2434 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2438 /* if format provided, just copy the buffers */
2439 if (bar_format
!= NULL
) {
2440 strlcpy(fmtnew
, fmtexp
, sz
);
2444 /* reset the output buffer */
2447 strlcat(fmtnew
, "+N:+I ", sz
);
2449 strlcat(fmtnew
, "+S", sz
);
2450 strlcat(fmtnew
, " ", sz
);
2452 /* only show the workspace name if there's actually one */
2453 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2454 strlcat(fmtnew
, "<+D>", sz
);
2456 /* If enabled, only show the iconic count if there are iconic wins. */
2457 if (iconic_enabled
&& r
!= NULL
&& r
->ws
!= NULL
)
2458 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2460 strlcat(fmtnew
, "{+M}", sz
);
2464 strlcat(fmtnew
, "+3<", sz
);
2466 if (clock_enabled
) {
2467 strlcat(fmtnew
, fmtexp
, sz
);
2468 strlcat(fmtnew
, "+4<", sz
);
2471 /* bar_urgent already adds the space before the last asterisk */
2473 strlcat(fmtnew
, (urgent_collapse
? "*+U*+4<" : "* +U*+4<"), sz
);
2475 if (window_class_enabled
) {
2476 strlcat(fmtnew
, "+C", sz
);
2477 if (!window_instance_enabled
)
2478 strlcat(fmtnew
, "+4<", sz
);
2481 /* checks needed by the colon and floating strlcat(3) calls below */
2482 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2483 if (window_instance_enabled
) {
2484 if (window_class_enabled
)
2485 strlcat(fmtnew
, ":", sz
);
2486 strlcat(fmtnew
, "+T+4<", sz
);
2488 if (window_name_enabled
) {
2489 if (ABOVE(r
->ws
->focus
) || MAXIMIZED(r
->ws
->focus
))
2490 strlcat(fmtnew
, "+F ", sz
);
2491 strlcat(fmtnew
, "+64W ", sz
);
2495 /* finally add the action script output and the version */
2496 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2500 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2502 /* special case; no limit given, pad one space, instead */
2503 if (*limit
== (int)sz
- 1)
2505 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2508 /* replaces the bar format character sequences (like in tmux(1)) */
2510 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2515 char tmp
[SWM_BAR_MAX
];
2516 int limit
, size
, count
;
2519 /* reset strlcat(3) buffer */
2522 /* get number, if any */
2525 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2526 limit
= sizeof tmp
- 1;
2527 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2528 limit
= sizeof tmp
- 1;
2530 /* there is nothing to replace (ie EOL) */
2537 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2540 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2543 bar_window_class(tmp
, sizeof tmp
, r
);
2546 bar_workspace_name(tmp
, sizeof tmp
, r
);
2549 bar_window_state(tmp
, sizeof tmp
, r
);
2552 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2556 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2560 snprintf(tmp
, sizeof tmp
, "%d", count
);
2563 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2566 bar_window_class_instance(tmp
, sizeof tmp
, r
);
2569 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2572 bar_window_instance(tmp
, sizeof tmp
, r
);
2575 bar_urgent(tmp
, sizeof tmp
);
2578 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2581 bar_window_name(tmp
, sizeof tmp
, r
);
2584 /* unknown character sequence; copy as-is */
2585 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2591 if ((int)len
< limit
)
2593 while (limit
-- > 0) {
2594 if (*offrep
>= sz
- 1)
2596 fmtrep
[(*offrep
)++] = *ptr
++;
2604 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2609 while (*fmt
!= '\0') {
2611 /* skip ordinary characters */
2614 fmtrep
[off
++] = *fmt
++;
2618 /* character sequence found; replace it */
2619 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2628 bar_fmt_expand(char *fmtexp
, size_t sz
)
2635 /* start by grabbing the current time and date */
2637 localtime_r(&tmt
, &tm
);
2639 /* figure out what to expand */
2640 if (bar_format
!= NULL
)
2642 else if (bar_format
== NULL
&& clock_enabled
)
2644 /* if nothing to expand bail out */
2650 /* copy as-is, just in case the format shouldn't be expanded below */
2651 strlcpy(fmtexp
, fmt
, sz
);
2652 /* finally pass the string through strftime(3) */
2653 #ifndef SWM_DENY_CLOCK_FORMAT
2654 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2655 warnx("format too long");
2660 /* Redraws a region bar; need to follow with xcb_flush() or focus_flush(). */
2662 bar_draw(struct swm_bar
*bar
)
2664 struct swm_region
*r
;
2665 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2666 char fmtrep
[SWM_BAR_MAX
];
2668 /* expand the format by first passing it through strftime(3) */
2669 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2676 if (bar_enabled
&& r
->ws
->bar_enabled
)
2677 xcb_map_window(conn
, bar
->id
);
2679 xcb_unmap_window(conn
, bar
->id
);
2683 if (startup_exception
)
2684 snprintf(fmtrep
, sizeof fmtrep
, "total "
2685 "exceptions: %d, first exception: %s",
2689 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2690 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2693 if (bar_font_legacy
)
2694 bar_print_legacy(r
, fmtrep
);
2696 bar_print(r
, fmtrep
);
2700 * Reads external script output; call when stdin is readable.
2701 * Returns 1 if bar_ext was updated; otherwise 0.
2704 bar_extra_update(void)
2707 char b
[SWM_BAR_MAX
];
2708 bool changed
= false;
2713 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2716 if (b
[len
- 1] == '\n') {
2717 /* Remove newline. */
2720 /* "Clear" bar_ext. */
2723 /* Flush buffered output. */
2724 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2725 bar_ext_buf
[0] = '\0';
2727 /* Append new output to bar. */
2728 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2732 /* Buffer output. */
2733 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2738 if (errno
!= EAGAIN
) {
2739 warn("bar_action failed");
2748 bar_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
2750 struct swm_region
*tmpr
;
2753 /* suppress unused warnings since vars are needed */
2758 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2761 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2762 /* Only change if master switch is enabled. */
2764 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2766 bar_enabled
= r
->ws
->bar_enabled
= true;
2768 case SWM_ARG_ID_BAR_TOGGLE
:
2769 bar_enabled
= !bar_enabled
;
2773 /* update bars as necessary */
2774 num_screens
= get_screen_count();
2775 for (i
= 0; i
< num_screens
; i
++)
2776 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2778 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2779 xcb_map_window(conn
, tmpr
->bar
->id
);
2781 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2784 /* Restack all regions and redraw bar. */
2785 num_screens
= get_screen_count();
2786 for (i
= 0; i
< num_screens
; i
++)
2787 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
2789 bar_draw(tmpr
->bar
);
2796 bar_extra_setup(void)
2798 /* do this here because the conf file is in memory */
2799 if (!bar_extra
&& bar_argv
[0]) {
2800 /* launch external status app */
2802 if (pipe(bar_pipe
) == -1)
2803 err(1, "pipe error");
2804 socket_setnonblock(bar_pipe
[0]);
2805 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2807 /* Set stdin to read from the pipe. */
2808 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2811 /* Set stdout to write to the pipe. */
2812 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2815 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2816 err(1, "could not disable SIGPIPE");
2817 switch (bar_pid
= fork()) {
2819 err(1, "cannot fork");
2823 execvp(bar_argv
[0], bar_argv
);
2824 err(1, "%s external app failed", bar_argv
[0]);
2826 default: /* parent */
2831 atexit(kill_bar_extra_atexit
);
2836 kill_bar_extra_atexit(void)
2839 kill(bar_pid
, SIGTERM
);
2847 while ((s
= index(s
, '-'))) {
2852 return (count
== 14);
2858 char *default_string
;
2859 char **missing_charsets
;
2860 int num_missing_charsets
= 0;
2864 XFreeFontSet(display
, bar_fs
);
2868 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n",
2871 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2872 &num_missing_charsets
, &default_string
);
2874 if (num_missing_charsets
> 0) {
2875 warnx("Unable to load charset(s):");
2877 for (i
= 0; i
< num_missing_charsets
; ++i
)
2878 warnx("%s", missing_charsets
[i
]);
2880 XFreeStringList(missing_charsets
);
2882 if (strcmp(default_string
, ""))
2883 warnx("Glyphs from those sets will be replaced "
2884 "by '%s'.", default_string
);
2886 warnx("Glyphs from those sets won't be drawn.");
2890 errx(1, "Error creating font set structure.");
2892 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2894 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2895 2 * bar_border_width
;
2902 xft_init(struct swm_region
*r
)
2904 char *font
, *str
, *search
;
2907 if (bar_font
== NULL
) {
2908 if ((search
= str
= strdup(bar_fonts
)) == NULL
)
2909 errx(1, "insufficient memory.");
2911 while ((font
= strsep(&search
, ",")) != NULL
) {
2915 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2918 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2921 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2925 if (bar_font
== NULL
) {
2926 warnx("unable to load font %s", font
);
2929 DNPRINTF(SWM_D_INIT
, "successfully opened "
2937 if (bar_font
== NULL
)
2938 errx(1, "unable to open a font");
2940 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2942 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2943 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2944 warn("Xft error: unable to allocate color.");
2946 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR
].pixel
, color
);
2948 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2949 DefaultColormap(display
, r
->s
->idx
), &color
, &search_font_color
))
2950 warn("Xft error: unable to allocate color.");
2952 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2959 bar_setup(struct swm_region
*r
)
2961 xcb_screen_t
*screen
;
2964 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2967 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2968 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2973 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2974 err(1, "bar_setup: calloc: failed to allocate memory.");
2976 if (bar_font_legacy
)
2983 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2984 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2985 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2987 /* Assume region is unfocused when we create the bar. */
2988 r
->bar
->id
= xcb_generate_id(conn
);
2989 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2990 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
2991 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
2992 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
2994 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2995 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2996 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
2997 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
2998 | XCB_CW_EVENT_MASK
, wa
);
3000 /* Stack bar window above region window to start. */
3002 wa
[1] = XCB_STACK_MODE_ABOVE
;
3004 xcb_configure_window(conn
, r
->bar
->id
, XCB_CONFIG_WINDOW_SIBLING
|
3005 XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
3007 r
->bar
->buffer
= xcb_generate_id(conn
);
3008 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
3009 WIDTH(r
->bar
), HEIGHT(r
->bar
));
3012 xcb_randr_select_input(conn
, r
->bar
->id
,
3013 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
3016 xcb_map_window(conn
, r
->bar
->id
);
3018 DNPRINTF(SWM_D_BAR
, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
3019 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
3026 bar_cleanup(struct swm_region
*r
)
3030 xcb_destroy_window(conn
, r
->bar
->id
);
3031 xcb_free_pixmap(conn
, r
->bar
->buffer
);
3037 set_win_state(struct ws_win
*win
, uint8_t state
)
3039 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
3041 DNPRINTF(SWM_D_EVENT
, "set_win_state: win %#x, state: %u\n",
3047 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
3048 a_state
, 32, 2, data
);
3052 get_win_state(xcb_window_t w
)
3054 xcb_get_property_reply_t
*r
;
3055 xcb_get_property_cookie_t c
;
3056 uint32_t result
= 0;
3058 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
3059 r
= xcb_get_property_reply(conn
, c
, NULL
);
3061 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
3062 result
= *((uint32_t *)xcb_get_property_value(r
));
3066 DNPRINTF(SWM_D_MISC
, "get_win_state property: win %#x state %u\n", w
,
3072 version(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3074 struct swm_region
*tmpr
;
3077 /* suppress unused warnings since vars are needed */
3082 bar_version
= !bar_version
;
3084 snprintf(bar_vertext
, sizeof bar_vertext
,
3085 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
3087 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
3089 num_screens
= get_screen_count();
3090 for (i
= 0; i
< num_screens
; i
++) {
3091 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
3092 bar_draw(tmpr
->bar
);
3099 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
3101 xcb_client_message_event_t ev
;
3109 name
= get_atom_name(a
);
3110 DNPRINTF(SWM_D_EVENT
, "client_msg: win %#x, atom: %s(%u), "
3112 win
->id
, name
, a
, t
);
3116 bzero(&ev
, sizeof ev
);
3117 ev
.response_type
= XCB_CLIENT_MESSAGE
;
3118 ev
.window
= win
->id
;
3121 ev
.data
.data32
[0] = a
;
3122 ev
.data
.data32
[1] = t
;
3124 xcb_send_event(conn
, 0, win
->id
,
3125 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
3128 /* synthetic response to a ConfigureRequest when not making a change */
3130 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
3132 xcb_configure_notify_event_t ce
;
3137 /* send notification of unchanged state. */
3138 bzero(&ce
, sizeof(ce
));
3139 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
3142 ce
.width
= WIDTH(win
);
3143 ce
.height
= HEIGHT(win
);
3144 ce
.border_width
= 0;
3145 ce
.override_redirect
= 0;
3150 ce
.window
= win
->id
;
3151 ce
.above_sibling
= XCB_WINDOW_NONE
;
3154 ce
.event
= ev
->window
;
3155 ce
.window
= ev
->window
;
3157 /* make response appear more WM_SIZE_HINTS-compliant */
3158 if (win
->sh
.flags
) {
3159 DNPRINTF(SWM_D_MISC
, "config_win: hints: win %#x,"
3160 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
3161 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
3162 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
3163 SH_INC_W(win
), SH_INC_H(win
));
3168 /* the hint may be set... to 0! */
3169 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
3170 ce
.width
= SH_MIN_W(win
);
3171 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
3172 ce
.height
= SH_MIN_H(win
);
3177 /* may also be advertized as 0 */
3178 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
3179 ce
.width
= SH_MAX_W(win
);
3180 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
3181 ce
.height
= SH_MAX_H(win
);
3184 /* resize increment. */
3186 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
3187 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
3189 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
3190 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
3194 /* adjust x and y for requested border_width. */
3195 ce
.x
+= ev
->border_width
;
3196 ce
.y
+= ev
->border_width
;
3198 ce
.above_sibling
= ev
->sibling
;
3201 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
3202 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
3203 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
3205 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
3210 count_win(struct workspace
*ws
, bool count_transient
)
3215 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3216 if (!count_transient
&& FLOATING(win
))
3222 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
3228 quit(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3230 /* suppress unused warnings since vars are needed */
3235 DNPRINTF(SWM_D_MISC
, "quit\n");
3240 lower_window(struct ws_win
*win
)
3242 struct ws_win
*target
= NULL
;
3243 struct workspace
*ws
;
3245 DNPRINTF(SWM_D_EVENT
, "lower_window: win %#x\n", WINID(win
));
3252 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3253 if (target
== win
|| ICONIC(target
))
3255 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3258 if (win
->transient
== target
->transient
)
3260 if (win
->transient
== target
->id
)
3263 if (FULLSCREEN(target
))
3265 if (FULLSCREEN(win
))
3267 if (MAXIMIZED(target
))
3271 if (ABOVE(target
) || TRANS(target
))
3273 if (ABOVE(win
) || TRANS(win
))
3277 /* Change stack position. */
3278 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3280 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3282 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3284 update_win_stacking(win
);
3287 if (swm_debug
& SWM_D_STACK
) {
3288 DPRINTF("=== stacking order (top down) === \n");
3289 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3290 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3291 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3292 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3293 YESNO(ICONIC(target
)));
3297 DNPRINTF(SWM_D_EVENT
, "lower_window: done\n");
3301 raise_window(struct ws_win
*win
)
3303 struct ws_win
*target
= NULL
;
3304 struct workspace
*ws
;
3306 DNPRINTF(SWM_D_EVENT
, "raise_window: win %#x\n", WINID(win
));
3313 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3314 if (target
== win
|| ICONIC(target
))
3316 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3318 if (TRANS(win
) && (win
->transient
== target
->transient
||
3319 win
->transient
== target
->id
))
3321 if (FULLSCREEN(win
))
3323 if (FULLSCREEN(target
))
3327 if (MAXIMIZED(target
))
3329 if (ABOVE(win
) || TRANS(win
) ||
3330 (win
->ws
->focus
== win
&& ws
->always_raise
))
3332 if (!ABOVE(target
) && !TRANS(target
))
3336 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3338 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3340 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3342 update_win_stacking(win
);
3345 if (swm_debug
& SWM_D_STACK
) {
3346 DPRINTF("=== stacking order (top down) === \n");
3347 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3348 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3349 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3350 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3351 YESNO(ICONIC(target
)));
3355 DNPRINTF(SWM_D_EVENT
, "raise_window: done\n");
3359 update_win_stacking(struct ws_win
*win
)
3361 struct ws_win
*sibling
;
3365 struct swm_region
*r
;
3368 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
3371 if (win
->frame
== XCB_WINDOW_NONE
) {
3372 DNPRINTF(SWM_D_EVENT
, "update_window_stacking: win %#x not "
3373 "reparented.\n", win
->id
);
3377 sibling
= TAILQ_NEXT(win
, stack_entry
);
3378 if (sibling
!= NULL
&& (FLOATING(win
) == FLOATING(sibling
) ||
3379 (win
->ws
->always_raise
&& win
->ws
->focus
== win
)))
3380 val
[0] = sibling
->frame
;
3381 else if (FLOATING(win
) || (win
->ws
->always_raise
&&
3382 win
->ws
->focus
== win
))
3383 val
[0] = r
->bar
->id
;
3387 DNPRINTF(SWM_D_EVENT
, "update_win_stacking: win %#x (%#x), "
3388 "sibling %#x\n", win
->frame
, win
->id
, val
[0]);
3390 val
[1] = XCB_STACK_MODE_ABOVE
;
3392 xcb_configure_window(conn
, win
->frame
, XCB_CONFIG_WINDOW_SIBLING
|
3393 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
3396 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
)
3402 map_window(struct ws_win
*win
)
3407 DNPRINTF(SWM_D_EVENT
, "map_window: win %#x, mapped: %s\n",
3408 win
->id
, YESNO(win
->mapped
));
3413 xcb_map_window(conn
, win
->frame
);
3414 xcb_map_window(conn
, win
->id
);
3415 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
3420 unmap_window(struct ws_win
*win
)
3425 DNPRINTF(SWM_D_EVENT
, "unmap_window: win %#x, mapped: %s\n", win
->id
,
3426 YESNO(win
->mapped
));
3431 xcb_unmap_window(conn
, win
->id
);
3432 xcb_unmap_window(conn
, win
->frame
);
3433 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
3434 win
->mapped
= false;
3441 int i
, j
, num_screens
;
3443 num_screens
= get_screen_count();
3444 for (i
= 0; i
< num_screens
; i
++)
3445 for (j
= 0; j
< workspace_limit
; j
++)
3446 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3451 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
3453 xcb_key_press_event_t event
;
3454 xcb_keycode_t
*keycode
;
3459 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
3461 DNPRINTF(SWM_D_MISC
, "fake_keypress: win %#x, keycode %u\n",
3464 bzero(&event
, sizeof(event
));
3465 event
.event
= win
->id
;
3466 event
.root
= win
->s
->root
;
3467 event
.child
= XCB_WINDOW_NONE
;
3468 event
.time
= XCB_CURRENT_TIME
;
3469 event
.event_x
= X(win
);
3470 event
.event_y
= Y(win
);
3473 event
.same_screen
= 1;
3474 event
.detail
= *keycode
;
3475 event
.state
= modifiers
;
3477 event
.response_type
= XCB_KEY_PRESS
;
3478 xcb_send_event(conn
, 1, win
->id
,
3479 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
3481 event
.response_type
= XCB_KEY_RELEASE
;
3482 xcb_send_event(conn
, 1, win
->id
,
3483 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
3489 restart(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3491 /* suppress unused warning since var is needed */
3496 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
3500 execvp(start_argv
[0], start_argv
);
3501 warn("execvp failed");
3502 quit(NULL
, NULL
, NULL
);
3506 get_pointer_win(xcb_window_t root
)
3508 struct ws_win
*win
= NULL
;
3509 xcb_query_pointer_reply_t
*r
;
3511 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: %#x.\n", root
);
3513 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
3515 win
= find_window(r
->child
);
3517 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: %#x.\n",
3520 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
3529 center_pointer(struct swm_region
*r
)
3533 if (!warp_pointer
|| r
== NULL
)
3538 DNPRINTF(SWM_D_EVENT
, "center_pointer: win %#x.\n", WINID(win
));
3540 if (win
&& win
->mapped
)
3541 xcb_warp_pointer(conn
, XCB_NONE
, win
->frame
, 0, 0, 0, 0,
3542 WIDTH(win
) / 2, HEIGHT(win
) / 2);
3544 xcb_warp_pointer(conn
, XCB_NONE
, r
->id
, 0, 0, 0, 0,
3545 WIDTH(r
) / 2, HEIGHT(r
) / 2);
3549 root_to_region(xcb_window_t root
, int check
)
3552 struct swm_region
*r
= NULL
;
3554 xcb_query_pointer_reply_t
*qpr
;
3555 xcb_get_input_focus_reply_t
*gifr
;
3557 DNPRINTF(SWM_D_MISC
, "root_to_region: win %#x\n", root
);
3559 num_screens
= get_screen_count();
3560 for (i
= 0; i
< num_screens
; i
++)
3561 if (screens
[i
].root
== root
)
3564 if (check
& SWM_CK_REGION
)
3565 r
= screens
[i
].r_focus
;
3567 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
3568 /* Try to find an actively focused window */
3569 gifr
= xcb_get_input_focus_reply(conn
,
3570 xcb_get_input_focus(conn
), NULL
);
3572 cfw
= find_window(gifr
->focus
);
3573 if (cfw
&& cfw
->ws
->r
)
3580 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
3581 /* No region with an active focus; try to use pointer. */
3582 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
3583 screens
[i
].root
), NULL
);
3585 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
3586 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
3587 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3588 if (X(r
) <= qpr
->root_x
&&
3589 qpr
->root_x
< MAX_X(r
) &&
3590 Y(r
) <= qpr
->root_y
&&
3591 qpr
->root_y
< MAX_Y(r
))
3598 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
3599 r
= TAILQ_FIRST(&screens
[i
].rl
);
3601 DNPRINTF(SWM_D_MISC
, "root_to_region: idx: %d\n", get_region_index(r
));
3607 find_region(xcb_window_t id
)
3609 struct swm_region
*r
;
3612 num_screens
= get_screen_count();
3613 for (i
= 0; i
< num_screens
; i
++)
3614 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3622 find_bar(xcb_window_t id
)
3624 struct swm_region
*r
;
3627 num_screens
= get_screen_count();
3628 for (i
= 0; i
< num_screens
; i
++)
3629 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3630 if (r
->bar
&& r
->bar
->id
== id
)
3637 find_frame_window(xcb_window_t id
) {
3638 struct swm_region
*r
;
3642 num_screens
= get_screen_count();
3643 for (i
= 0; i
< num_screens
; i
++)
3644 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3645 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
3653 find_window(xcb_window_t id
)
3655 struct ws_win
*win
= NULL
;
3656 int i
, j
, num_screens
;
3657 xcb_query_tree_reply_t
*qtr
;
3659 DNPRINTF(SWM_D_MISC
, "find_window: id: %#x\n", id
);
3661 num_screens
= get_screen_count();
3662 for (i
= 0; i
< num_screens
; i
++)
3663 for (j
= 0; j
< workspace_limit
; j
++)
3664 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3665 if (id
== win
->id
|| id
== win
->frame
)
3669 /* If window isn't top-level, try to find managed ancestor. */
3670 qtr
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3672 if (qtr
->parent
!= XCB_WINDOW_NONE
&& qtr
->parent
!= qtr
->root
)
3673 win
= find_window(qtr
->parent
);
3677 DNPRINTF(SWM_D_MISC
, "find_window: found child %#x "
3678 "of %#x.\n", win
->id
, qtr
->parent
);
3688 find_unmanaged_window(xcb_window_t id
)
3691 int i
, j
, num_screens
;
3693 num_screens
= get_screen_count();
3694 for (i
= 0; i
< num_screens
; i
++)
3695 for (j
= 0; j
< workspace_limit
; j
++)
3696 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
3704 spawn(int ws_idx
, union arg
*args
, bool close_fd
)
3709 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3711 close(xcb_get_file_descriptor(conn
));
3713 setenv("LD_PRELOAD", SWM_LIB
, 1);
3715 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3716 warn("spawn: asprintf SWM_WS");
3719 setenv("_SWM_WS", ret
, 1);
3723 if (asprintf(&ret
, "%d", getpid()) == -1) {
3724 warn("spawn: asprintf _SWM_PID");
3727 setenv("_SWM_PID", ret
, 1);
3731 if (setsid() == -1) {
3732 warn("spawn: setsid");
3738 * close stdin and stdout to prevent interaction between apps
3739 * and the baraction script
3740 * leave stderr open to record errors
3742 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3743 warn("spawn: open");
3746 dup2(fd
, STDIN_FILENO
);
3747 dup2(fd
, STDOUT_FILENO
);
3752 execvp(args
->argv
[0], args
->argv
);
3754 warn("spawn: execvp");
3759 kill_refs(struct ws_win
*win
)
3761 struct workspace
*ws
;
3763 int i
, j
, num_screens
;
3768 num_screens
= get_screen_count();
3769 for (i
= 0; i
< num_screens
; i
++) {
3770 for (j
= 0; j
< workspace_limit
; j
++) {
3771 ws
= &screens
[i
].ws
[j
];
3773 if (win
== ws
->focus
)
3775 if (win
== ws
->focus_prev
)
3776 ws
->focus_prev
= NULL
;
3777 if (win
== ws
->focus_pending
)
3778 ws
->focus_pending
= NULL
;
3779 if (win
== ws
->raised
)
3783 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3784 if (win
== w
->focus_child
)
3785 w
->focus_child
= NULL
;
3791 validate_win(struct ws_win
*testwin
)
3794 struct workspace
*ws
;
3795 struct swm_region
*r
;
3796 int i
, x
, num_screens
;
3798 if (testwin
== NULL
)
3801 num_screens
= get_screen_count();
3802 for (i
= 0; i
< num_screens
; i
++)
3803 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3804 for (x
= 0; x
< workspace_limit
; x
++) {
3806 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3814 validate_ws(struct workspace
*testws
)
3816 struct swm_region
*r
;
3817 struct workspace
*ws
;
3818 int i
, x
, num_screens
;
3820 /* validate all ws */
3821 num_screens
= get_screen_count();
3822 for (i
= 0; i
< num_screens
; i
++)
3823 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3824 for (x
= 0; x
< workspace_limit
; x
++) {
3833 unfocus_win(struct ws_win
*win
)
3835 xcb_window_t none
= XCB_WINDOW_NONE
;
3837 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: win %#x\n", WINID(win
));
3842 if (win
->ws
== NULL
) {
3843 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3847 if (validate_ws(win
->ws
)) {
3848 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3852 if (win
->ws
->r
== NULL
) {
3853 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3857 if (validate_win(win
)) {
3858 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3863 if (win
->ws
->focus
== win
) {
3864 win
->ws
->focus
= NULL
;
3865 win
->ws
->focus_prev
= win
;
3866 if(win
->ws
->raised
== win
&& !FLOATING(win
)) {
3867 update_win_stacking(win
);
3871 if (validate_win(win
->ws
->focus
)) {
3872 kill_refs(win
->ws
->focus
);
3873 win
->ws
->focus
= NULL
;
3876 if (validate_win(win
->ws
->focus_prev
)) {
3877 kill_refs(win
->ws
->focus_prev
);
3878 win
->ws
->focus_prev
= NULL
;
3883 /* Raise window to "top unfocused position." */
3884 if (win
->ws
->always_raise
)
3887 /* Update border width */
3888 if (win
->bordered
&& (win
->quirks
& SWM_Q_MINIMALBORDER
) &&
3891 X(win
) += border_width
;
3892 Y(win
) += border_width
;
3896 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3897 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3899 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3903 focus_win(struct ws_win
*win
)
3905 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
, *tmpw
;
3906 struct workspace
*ws
;
3907 xcb_get_input_focus_reply_t
*gifr
= NULL
;
3908 xcb_get_window_attributes_reply_t
*war
= NULL
;
3910 DNPRINTF(SWM_D_FOCUS
, "focus_win: win %#x\n", WINID(win
));
3912 if (win
== NULL
|| win
->ws
== NULL
|| !win
->mapped
)
3917 if (validate_ws(ws
))
3920 if (validate_win(win
)) {
3925 gifr
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3927 DNPRINTF(SWM_D_FOCUS
, "focus_win: cur focus: %#x\n",
3930 cfw
= find_window(gifr
->focus
);
3933 if (cfw
->ws
!= ws
&& cfw
->ws
->r
!= NULL
&&
3934 cfw
->frame
!= XCB_WINDOW_NONE
) {
3941 war
= xcb_get_window_attributes_reply(conn
,
3942 xcb_get_window_attributes(conn
, gifr
->focus
), NULL
);
3943 if (war
&& war
->override_redirect
&& ws
->focus
== win
) {
3944 DNPRINTF(SWM_D_FOCUS
, "focus_win: skip refocus "
3945 "from override_redirect.\n");
3951 if (ws
->focus
!= win
) {
3952 if (ws
->focus
&& ws
->focus
!= cfw
)
3953 unfocus_win(ws
->focus
);
3957 /* Clear focus child redirect. */
3958 win
->focus_child
= NULL
;
3960 /* If transient, adjust parent's focus child for focus_magic. */
3962 parent
= find_window(win
->transient
);
3963 if (parent
&& parent
->focus_child
!= win
)
3964 parent
->focus_child
= win
;
3967 /* Update window border even if workspace is hidden. */
3971 DNPRINTF(SWM_D_FOCUS
, "focus_win: already focused.\n");
3976 /* Set input focus if no input hint, or indicated by hint. */
3977 if (ACCEPTS_FOCUS(win
)) {
3978 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
3979 " revert-to: parent, time: %#x\n", win
->id
,
3981 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
3982 win
->id
, last_event_time
);
3983 } else if (!win
->take_focus
) {
3984 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
3985 " revert-to: parent, time: 0\n", ws
->r
->id
);
3986 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
3987 ws
->r
->id
, XCB_CURRENT_TIME
);
3990 /* Tell app it can adjust focus to a specific window. */
3991 if (win
->take_focus
) {
3992 /* java is special; always tell parent */
3993 if (TRANS(win
) && win
->java
)
3994 client_msg(parent
, a_takefocus
,
3997 client_msg(win
, a_takefocus
, last_event_time
);
4000 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
4002 /* If a parent exists, map it first. */
4004 raise_window(parent
);
4007 /* Map siblings next. */
4008 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
,
4010 if (w
!= win
&& !ICONIC(w
) &&
4011 w
->transient
== parent
->id
) {
4017 /* Map focused window. */
4021 /* Stack any children of focus window. */
4022 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
4023 if (w
->transient
== win
->id
&& !ICONIC(w
)) {
4027 } else if (tile_gap
< 0 && !ABOVE(win
)) {
4029 * Windows overlap in the layout.
4030 * Raise focused win above all tiled wins.
4038 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4039 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4042 bar_draw(ws
->r
->bar
);
4048 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
4051 /* If a child window should have focus instead, return it. */
4053 get_focus_magic(struct ws_win
*win
)
4055 struct ws_win
*parent
= NULL
;
4056 struct ws_win
*child
= NULL
;
4058 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: win %#x\n", WINID(win
));
4063 parent
= find_window(win
->transient
);
4065 /* If parent prefers focus elsewhere, then try to do so. */
4066 if (parent
&& (child
= parent
->focus_child
)) {
4067 if (validate_win(child
) == 0 && child
->mapped
)
4070 parent
->focus_child
= NULL
;
4074 /* If this window prefers focus elsewhere, then try to do so. */
4075 if ((child
= win
->focus_child
)) {
4076 if (validate_win(child
) == 0 && child
->mapped
)
4079 win
->focus_child
= NULL
;
4086 event_drain(uint8_t rt
)
4088 xcb_generic_event_t
*evt
;
4090 /* ensure all pending requests have been processed before filtering. */
4092 while ((evt
= get_next_event(false))) {
4093 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
4101 set_region(struct swm_region
*r
)
4103 struct swm_region
*rf
;
4110 /* Unfocus old region bar. */
4115 xcb_change_window_attributes(conn
, rf
->bar
->id
,
4116 XCB_CW_BORDER_PIXEL
,
4117 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
4120 if (rf
!= NULL
&& rf
!= r
&& (X(rf
) != X(r
) || Y(rf
) != Y(r
) ||
4121 WIDTH(rf
) != WIDTH(r
) || HEIGHT(rf
) != HEIGHT(r
))) {
4122 /* Set _NET_DESKTOP_GEOMETRY. */
4124 vals
[1] = HEIGHT(r
);
4125 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, r
->s
->root
,
4126 ewmh
[_NET_DESKTOP_GEOMETRY
].atom
, XCB_ATOM_CARDINAL
, 32, 2,
4130 /* Set region bar border to focus_color. */
4131 xcb_change_window_attributes(conn
, r
->bar
->id
,
4132 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
4136 ewmh_update_current_desktop();
4140 focus_region(struct swm_region
*r
)
4143 struct swm_region
*old_r
;
4148 old_r
= r
->s
->r_focus
;
4151 nfw
= get_region_focus(r
);
4155 /* New region is empty; need to manually unfocus win. */
4157 unfocus_win(old_r
->ws
->focus
);
4158 /* Clear bar since empty. */
4159 bar_draw(old_r
->bar
);
4162 DNPRINTF(SWM_D_FOCUS
, "focus_region: set_input_focus: %#x, "
4163 "revert-to: parent, time: 0\n", r
->id
);
4164 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4171 switchws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4173 struct swm_region
*this_r
, *other_r
;
4175 struct workspace
*new_ws
, *old_ws
;
4176 xcb_window_t none
= XCB_WINDOW_NONE
;
4177 int wsid
= args
->id
;
4178 bool unmap_old
= false;
4183 if (wsid
>= workspace_limit
)
4187 old_ws
= this_r
->ws
;
4188 new_ws
= &this_r
->s
->ws
[wsid
];
4190 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
4191 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
4193 if (new_ws
== NULL
|| old_ws
== NULL
)
4195 if (new_ws
== old_ws
)
4198 other_r
= new_ws
->r
;
4199 if (other_r
&& workspace_clamp
&&
4200 bp
->action
!= FN_RG_MOVE_NEXT
&& bp
->action
!= FN_RG_MOVE_PREV
) {
4201 DNPRINTF(SWM_D_WS
, "switchws: ws clamped.\n");
4204 DNPRINTF(SWM_D_WS
, "switchws: warping focus to region "
4205 "with ws %d.\n", wsid
);
4206 focus_region(other_r
);
4207 center_pointer(other_r
);
4213 if ((win
= old_ws
->focus
) != NULL
) {
4216 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4217 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4222 /* the other ws is visible in another region, exchange them */
4223 other_r
->ws_prior
= new_ws
;
4224 other_r
->ws
= old_ws
;
4225 old_ws
->r
= other_r
;
4227 /* the other workspace is hidden, hide this one */
4232 this_r
->ws_prior
= old_ws
;
4233 this_r
->ws
= new_ws
;
4236 /* Set focus_pending before stacking, if needed. */
4237 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
4238 validate_win(new_ws
->focus_pending
))) {
4239 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
4240 new_ws
->focus
= new_ws
->focus_prev
;
4241 new_ws
->focus_prev
= NULL
;
4244 new_ws
->state
= SWM_WS_STATE_MAPPING
;
4249 /* unmap old windows */
4251 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
4253 old_ws
->state
= SWM_WS_STATE_HIDDEN
;
4256 /* if workspaces were swapped, then don't wait to set focus */
4257 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
4258 if (new_ws
->focus_pending
) {
4259 focus_win(new_ws
->focus_pending
);
4260 new_ws
->focus_pending
= NULL
;
4264 /* Clear bar and set focus on region input win if new ws is empty. */
4265 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
4266 DNPRINTF(SWM_D_FOCUS
, "switchws: set_input_focus: %#x, "
4267 "revert-to: parent, time: 0\n", r
->id
);
4268 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4273 ewmh_update_current_desktop();
4277 new_ws
->state
= SWM_WS_STATE_MAPPED
;
4279 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
4283 cyclews(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4286 struct swm_screen
*s
= r
->s
;
4287 bool cycle_all
= false, mv
= false;
4289 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4290 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4296 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
4299 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
4302 case SWM_ARG_ID_CYCLEWS_UP
:
4303 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
4305 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
4308 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
4311 case SWM_ARG_ID_CYCLEWS_DOWN
:
4312 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
4319 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
4321 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
4325 send_to_ws(bp
, r
, &a
);
4327 switchws(bp
, r
, &a
);
4328 } while (a
.id
!= r
->ws
->idx
);
4330 DNPRINTF(SWM_D_FOCUS
, "cyclews: done\n");
4334 priorws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4340 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4341 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4343 if (r
->ws_prior
== NULL
)
4346 a
.id
= r
->ws_prior
->idx
;
4347 switchws(bp
, r
, &a
);
4348 DNPRINTF(SWM_D_FOCUS
, "priorws: done\n");
4352 focusrg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4354 int ridx
= args
->id
, i
, num_screens
;
4355 struct swm_region
*rr
= NULL
;
4359 num_screens
= get_screen_count();
4360 /* do nothing if we don't have more than one screen */
4361 if (!(num_screens
> 1 || outputs
> 1))
4364 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
4366 rr
= TAILQ_FIRST(&r
->s
->rl
);
4367 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
4368 rr
= TAILQ_NEXT(rr
, entry
);
4376 DNPRINTF(SWM_D_FOCUS
, "focusrg: done\n");
4380 cyclerg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4383 struct swm_region
*rr
= NULL
;
4386 num_screens
= get_screen_count();
4387 /* do nothing if we don't have more than one screen */
4388 if (!(num_screens
> 1 || outputs
> 1))
4392 DNPRINTF(SWM_D_FOCUS
, "cyclerg: id: %d, region: %d\n", args
->id
, i
);
4395 case SWM_ARG_ID_CYCLERG_UP
:
4396 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4397 rr
= TAILQ_NEXT(r
, entry
);
4399 rr
= TAILQ_FIRST(&screens
[i
].rl
);
4401 case SWM_ARG_ID_CYCLERG_DOWN
:
4402 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4403 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
4405 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
4414 case SWM_ARG_ID_CYCLERG_UP
:
4415 case SWM_ARG_ID_CYCLERG_DOWN
:
4420 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4421 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4423 switchws(bp
, r
, &a
);
4429 DNPRINTF(SWM_D_FOCUS
, "cyclerg: done\n");
4432 /* Sorts transients after parent. */
4434 sort_windows(struct ws_win_list
*wl
)
4436 struct ws_win
*win
, *parent
, *nxt
;
4441 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
4442 nxt
= TAILQ_NEXT(win
, entry
);
4444 parent
= find_window(win
->transient
);
4445 if (parent
== NULL
) {
4446 warnx("not possible bug");
4449 TAILQ_REMOVE(wl
, win
, entry
);
4450 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
4456 swapwin(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4458 struct ws_win
*target
, *source
;
4459 struct ws_win
*cur_focus
;
4460 struct ws_win_list
*wl
;
4464 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4465 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4467 cur_focus
= r
->ws
->focus
;
4468 if (cur_focus
== NULL
|| FULLSCREEN(cur_focus
))
4471 /* Adjust stacking in floating layer. */
4472 if (ABOVE(cur_focus
)) {
4474 case SWM_ARG_ID_SWAPPREV
:
4475 target
= TAILQ_PREV(cur_focus
, ws_win_stack
,
4477 if (target
!= NULL
&& FLOATING(target
)) {
4478 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4480 TAILQ_INSERT_BEFORE(target
, cur_focus
,
4482 update_win_stacking(cur_focus
);
4486 case SWM_ARG_ID_SWAPNEXT
:
4487 target
= TAILQ_NEXT(cur_focus
, stack_entry
);
4488 if (target
!= NULL
&& FLOATING(target
)) {
4489 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4491 TAILQ_INSERT_AFTER(&cur_focus
->ws
->stack
,
4492 target
, cur_focus
, stack_entry
);
4493 update_win_stacking(cur_focus
);
4501 if (r
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4504 clear_maximized(r
->ws
);
4507 wl
= &source
->ws
->winlist
;
4510 case SWM_ARG_ID_SWAPPREV
:
4512 source
= find_window(source
->transient
);
4513 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
4514 if (target
&& target
->transient
)
4515 target
= find_window(target
->transient
);
4516 TAILQ_REMOVE(wl
, source
, entry
);
4518 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4520 TAILQ_INSERT_BEFORE(target
, source
, entry
);
4522 case SWM_ARG_ID_SWAPNEXT
:
4523 target
= TAILQ_NEXT(source
, entry
);
4524 /* move the parent and let the sort handle the move */
4526 source
= find_window(source
->transient
);
4527 TAILQ_REMOVE(wl
, source
, entry
);
4529 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4531 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
4533 case SWM_ARG_ID_SWAPMAIN
:
4534 target
= TAILQ_FIRST(wl
);
4535 if (target
== source
) {
4536 if (source
->ws
->focus_prev
!= NULL
&&
4537 source
->ws
->focus_prev
!= target
)
4538 source
= source
->ws
->focus_prev
;
4542 if (target
== NULL
|| source
== NULL
)
4544 source
->ws
->focus_prev
= target
;
4545 TAILQ_REMOVE(wl
, target
, entry
);
4546 TAILQ_INSERT_BEFORE(source
, target
, entry
);
4547 TAILQ_REMOVE(wl
, source
, entry
);
4548 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4550 case SWM_ARG_ID_MOVELAST
:
4551 TAILQ_REMOVE(wl
, source
, entry
);
4552 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4555 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
4560 ewmh_update_client_list();
4566 DNPRINTF(SWM_D_MOVE
, "swapwin: done\n");
4570 get_focus_prev(struct ws_win
*win
)
4572 struct ws_win
*winfocus
= NULL
;
4573 struct ws_win
*cur_focus
= NULL
;
4574 struct ws_win_list
*wl
= NULL
;
4575 struct workspace
*ws
= NULL
;
4577 if (!(win
&& win
->ws
))
4582 cur_focus
= ws
->focus
;
4584 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: win %#x, cur_focus: %#x, "
4585 "focus_prev: %#x\n", WINID(win
), WINID(cur_focus
),
4586 WINID(ws
->focus_prev
));
4588 /* pickle, just focus on whatever */
4589 if (cur_focus
== NULL
) {
4590 /* use prev_focus if valid */
4591 if (ws
->focus_prev
&& find_window(ws
->focus_prev
->id
))
4592 winfocus
= ws
->focus_prev
;
4596 /* if transient focus on parent */
4597 if (TRANS(cur_focus
)) {
4598 winfocus
= find_window(cur_focus
->transient
);
4602 /* if in max_stack try harder */
4603 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
4604 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
4605 if (cur_focus
!= ws
->focus_prev
)
4606 winfocus
= ws
->focus_prev
;
4608 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
4613 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
4615 if (winfocus
== NULL
|| winfocus
== win
) {
4616 switch (focus_close
) {
4617 case SWM_STACK_BOTTOM
:
4618 TAILQ_FOREACH(winfocus
, wl
, entry
)
4619 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4623 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4624 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4627 case SWM_STACK_ABOVE
:
4628 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
4629 while (winfocus
&& ICONIC(winfocus
))
4630 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4632 if (winfocus
== NULL
) {
4633 if (focus_close_wrap
) {
4634 TAILQ_FOREACH(winfocus
, wl
, entry
)
4635 if (!ICONIC(winfocus
) &&
4636 winfocus
!= cur_focus
)
4639 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4641 if (!ICONIC(winfocus
) &&
4642 winfocus
!= cur_focus
)
4647 case SWM_STACK_BELOW
:
4648 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
4649 while (winfocus
&& ICONIC(winfocus
))
4650 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
4653 if (winfocus
== NULL
) {
4654 if (focus_close_wrap
) {
4655 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4657 if (!ICONIC(winfocus
) &&
4658 winfocus
!= cur_focus
)
4661 TAILQ_FOREACH(winfocus
, wl
, entry
)
4662 if (!ICONIC(winfocus
) &&
4663 winfocus
!= cur_focus
)
4671 if (winfocus
== NULL
||
4672 (winfocus
&& (ICONIC(winfocus
) || winfocus
== cur_focus
))) {
4673 if (focus_default
== SWM_STACK_TOP
) {
4674 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4675 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4678 TAILQ_FOREACH(winfocus
, wl
, entry
)
4679 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4686 return get_focus_magic(winfocus
);
4690 get_region_focus(struct swm_region
*r
)
4692 struct ws_win
*winfocus
= NULL
;
4697 if (r
->ws
->focus
&& !ICONIC(r
->ws
->focus
))
4698 winfocus
= r
->ws
->focus
;
4699 else if (r
->ws
->focus_prev
&& !ICONIC(r
->ws
->focus_prev
))
4700 winfocus
= r
->ws
->focus_prev
;
4702 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
4703 if (!ICONIC(winfocus
))
4706 return get_focus_magic(winfocus
);
4710 focus(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4712 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
4713 struct ws_win_list
*wl
= NULL
;
4714 struct workspace
*ws
= NULL
;
4721 cur_focus
= r
->ws
->focus
;
4725 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d, cur_focus: %#x\n", args
->id
,
4728 /* Make sure an uniconified window has focus, if one exists. */
4729 if (cur_focus
== NULL
) {
4730 cur_focus
= TAILQ_FIRST(wl
);
4731 while (cur_focus
!= NULL
&& ICONIC(cur_focus
))
4732 cur_focus
= TAILQ_NEXT(cur_focus
, entry
);
4734 DNPRINTF(SWM_D_FOCUS
, "focus: new cur_focus: %#x\n",
4739 case SWM_ARG_ID_FOCUSPREV
:
4740 if (cur_focus
== NULL
)
4743 winfocus
= cur_focus
;
4745 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
4746 if (winfocus
== NULL
)
4747 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
4748 if (winfocus
== cur_focus
)
4750 } while (winfocus
&& (ICONIC(winfocus
) ||
4751 winfocus
->id
== cur_focus
->transient
||
4752 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4753 winfocus
->transient
== cur_focus
->transient
) ||
4754 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4756 case SWM_ARG_ID_FOCUSNEXT
:
4757 if (cur_focus
== NULL
)
4760 winfocus
= cur_focus
;
4762 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4763 if (winfocus
== NULL
)
4764 winfocus
= TAILQ_FIRST(wl
);
4765 if (winfocus
== cur_focus
)
4767 } while (winfocus
&& (ICONIC(winfocus
) ||
4768 winfocus
->id
== cur_focus
->transient
||
4769 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4770 winfocus
->transient
== cur_focus
->transient
) ||
4771 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4773 case SWM_ARG_ID_FOCUSMAIN
:
4774 if (cur_focus
== NULL
)
4777 winfocus
= TAILQ_FIRST(wl
);
4778 if (winfocus
== cur_focus
)
4779 winfocus
= cur_focus
->ws
->focus_prev
;
4781 case SWM_ARG_ID_FOCUSURGENT
:
4782 /* Search forward for the next urgent window. */
4786 for (i
= 0; i
<= workspace_limit
; ++i
) {
4788 head
= TAILQ_FIRST(&r
->s
->ws
[(ws
->idx
+ i
) %
4789 workspace_limit
].winlist
);
4792 if (head
== cur_focus
) {
4794 winfocus
= cur_focus
;
4797 } else if (get_urgent(head
)) {
4802 head
= TAILQ_NEXT(head
, entry
);
4809 /* Switch ws if new focus is on a different ws. */
4810 if (winfocus
&& winfocus
->ws
!= ws
) {
4811 a
.id
= winfocus
->ws
->idx
;
4812 switchws(bp
, r
, &a
);
4819 if (clear_maximized(ws
) > 0)
4822 focus_win(get_focus_magic(winfocus
));
4827 DNPRINTF(SWM_D_FOCUS
, "focus: done\n");
4831 focus_pointer(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4835 /* Not needed for buttons since this is already done in buttonpress. */
4836 if (bp
->type
== KEYBIND
) {
4837 focus_win(get_pointer_win(r
->s
->root
));
4843 cycle_layout(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4845 struct workspace
*ws
= r
->ws
;
4847 /* suppress unused warning since var is needed */
4851 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
4854 if (ws
->cur_layout
->l_stack
== NULL
)
4855 ws
->cur_layout
= &layouts
[0];
4857 clear_maximized(ws
);
4862 focus_win(get_region_focus(r
));
4869 stack_config(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4871 struct workspace
*ws
= r
->ws
;
4875 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
4878 if (clear_maximized(ws
) > 0)
4881 if (ws
->cur_layout
->l_config
!= NULL
)
4882 ws
->cur_layout
->l_config(ws
, args
->id
);
4884 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
4893 stack(struct swm_region
*r
) {
4894 struct swm_geometry g
;
4895 struct swm_region
*r_prev
;
4901 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
4903 /* Adjust stack area for region bar and padding. */
4905 g
.x
+= region_padding
;
4906 g
.y
+= region_padding
;
4907 g
.w
-= 2 * border_width
+ 2 * region_padding
;
4908 g
.h
-= 2 * border_width
+ 2 * region_padding
;
4909 if (bar_enabled
&& r
->ws
->bar_enabled
) {
4915 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d (screen: "
4916 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4917 r
->ws
->idx
, r
->s
->idx
, get_region_index(r
), g
.x
, g
.y
, g
.w
, g
.h
);
4919 r_prev
= TAILQ_PREV(r
, swm_region_list
, entry
);
4921 /* Stack bar/input relative to prev. region. */
4922 val
[1] = XCB_STACK_MODE_ABOVE
;
4924 val
[0] = r_prev
->id
;
4925 DNPRINTF(SWM_D_STACK
, "stack: region input %#x "
4926 "relative to %#x.\n", r
->id
, val
[0]);
4927 xcb_configure_window(conn
, r
->id
,
4928 XCB_CONFIG_WINDOW_SIBLING
|
4929 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4931 val
[0] = r_prev
->bar
->id
;
4932 DNPRINTF(SWM_D_STACK
, "stack: region bar %#x "
4933 "relative to %#x.\n", r
->bar
->id
, val
[0]);
4934 xcb_configure_window(conn
, r
->bar
->id
,
4935 XCB_CONFIG_WINDOW_SIBLING
|
4936 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4939 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
4940 r
->ws
->cur_layout
->l_string(r
->ws
);
4941 /* save r so we can track region changes */
4947 DNPRINTF(SWM_D_STACK
, "stack: end\n");
4951 store_float_geom(struct ws_win
*win
)
4953 if (win
== NULL
|| win
->ws
->r
== NULL
)
4956 /* retain window geom and region geom */
4957 win
->g_float
= win
->g
;
4958 win
->g_float
.x
-= X(win
->ws
->r
);
4959 win
->g_float
.y
-= Y(win
->ws
->r
);
4960 win
->g_floatvalid
= true;
4961 DNPRINTF(SWM_D_MISC
, "store_float_geom: win %#x, g: (%d,%d)"
4962 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
4963 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
4964 win
->g_float
.w
, win
->g_float
.h
);
4968 load_float_geom(struct ws_win
*win
)
4970 if (win
== NULL
|| win
->ws
->r
== NULL
)
4973 if (win
->g_floatvalid
) {
4974 win
->g
= win
->g_float
;
4975 X(win
) += X(win
->ws
->r
);
4976 Y(win
) += Y(win
->ws
->r
);
4977 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g: (%d,%d)"
4978 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
4981 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g_float "
4982 "is not set.\n", win
->id
);
4987 update_floater(struct ws_win
*win
)
4989 struct workspace
*ws
;
4990 struct swm_region
*r
;
4992 DNPRINTF(SWM_D_MISC
, "update_floater: win %#x\n", WINID(win
));
4999 if ((r
= ws
->r
) == NULL
)
5002 win
->bordered
= true;
5004 if (FULLSCREEN(win
)) {
5005 /* _NET_WM_FULLSCREEN: fullscreen without border. */
5006 if (!win
->g_floatvalid
)
5007 store_float_geom(win
);
5010 win
->bordered
= false;
5011 } else if (MAXIMIZED(win
)) {
5012 /* Maximize: like a single stacked window. */
5013 if (!win
->g_floatvalid
)
5014 store_float_geom(win
);
5018 if (bar_enabled
&& ws
->bar_enabled
&& !maximize_hide_bar
) {
5020 Y(win
) += bar_height
;
5021 HEIGHT(win
) -= bar_height
;
5022 } else if (disable_border
) {
5023 win
->bordered
= false;
5026 if (win
->bordered
) {
5027 /* Window geometry excludes frame. */
5028 X(win
) += border_width
;
5029 Y(win
) += border_width
;
5030 HEIGHT(win
) -= 2 * border_width
;
5031 WIDTH(win
) -= 2 * border_width
;
5034 /* Normal floating window. */
5035 /* Update geometry if new region. */
5037 load_float_geom(win
);
5039 if (((win
->quirks
& SWM_Q_FULLSCREEN
) &&
5040 WIDTH(win
) >= WIDTH(r
) && HEIGHT(win
) >= HEIGHT(r
)) ||
5041 ((!WS_FOCUSED(win
->ws
) || win
->ws
->focus
!= win
) &&
5042 (win
->quirks
& SWM_Q_MINIMALBORDER
))) {
5044 win
->bordered
= false;
5045 } else if (!MANUAL(win
)) {
5046 if (TRANS(win
) && (win
->quirks
& SWM_Q_TRANSSZ
)) {
5047 /* Adjust size on TRANSSZ quirk. */
5048 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
5049 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
5052 if (!(win
->quirks
& SWM_Q_ANYWHERE
)) {
5054 * Floaters and transients are auto-centred
5055 * unless manually moved, resized or ANYWHERE
5058 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2;
5059 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2;
5060 store_float_geom(win
);
5065 /* Ensure at least 1 pixel of the window is in the region. */
5066 region_containment(win
, r
, SWM_CW_ALLSIDES
);
5072 * Send keystrokes to terminal to decrease/increase the font size as the
5073 * window size changes.
5076 adjust_font(struct ws_win
*win
)
5078 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
5079 ABOVE(win
) || TRANS(win
))
5082 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
5083 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
5084 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
5085 win
->font_size_boundary
[win
->font_steps
] =
5086 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
5089 win
->last_inc
= win
->sh
.width_inc
;
5090 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
5091 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
5092 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
5095 win
->last_inc
= win
->sh
.width_inc
;
5096 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
5100 #define SWAPXY(g) do { \
5102 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
5103 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
5106 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, bool flip
)
5108 struct swm_geometry cell
, r_g
= *g
;
5110 int i
= 0, j
= 0, s
= 0, stacks
= 0;
5111 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
5112 int hrh
= 0, extra
= 0, h_slice
= 0, last_h
= 0;
5113 int split
= 0, colno
= 0;
5114 int winno
, mwin
= 0, msize
= 0;
5115 int remain
, missing
, v_slice
, mscale
;
5116 bool bordered
= true, reconfigure
= false;
5119 * cell: geometry for window, including frame.
5120 * mwin: # of windows in master area.
5121 * mscale: size increment of master area.
5122 * stacks: # of stack columns
5125 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
5126 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
5128 memset(&cell
, 0, sizeof(cell
));
5130 /* Prepare tiling variables, if needed. */
5131 if ((winno
= count_win(ws
, false)) > 0) {
5132 /* Find first tiled window. */
5133 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
5134 if (!FLOATING(win
) && !ICONIC(win
))
5137 /* Take into account size hints of first tiled window. */
5139 w_inc
= win
->sh
.width_inc
;
5140 w_base
= win
->sh
.base_width
;
5141 mwin
= ws
->l_state
.horizontal_mwin
;
5142 mscale
= ws
->l_state
.horizontal_msize
;
5143 stacks
= ws
->l_state
.horizontal_stacks
;
5146 w_inc
= win
->sh
.height_inc
;
5147 w_base
= win
->sh
.base_height
;
5148 mwin
= ws
->l_state
.vertical_mwin
;
5149 mscale
= ws
->l_state
.vertical_msize
;
5150 stacks
= ws
->l_state
.vertical_stacks
;
5154 cell
.x
+= border_width
;
5155 cell
.y
+= border_width
;
5157 if (stacks
> winno
- mwin
)
5158 stacks
= winno
- mwin
;
5162 h_slice
= r_g
.h
/ SWM_H_SLICE
;
5163 if (mwin
&& winno
> mwin
) {
5164 v_slice
= r_g
.w
/ SWM_V_SLICE
;
5168 cell
.w
= v_slice
* mscale
;
5170 if (w_inc
> 1 && w_inc
< v_slice
) {
5171 /* Adjust for requested size increment. */
5172 remain
= (cell
.w
- w_base
) % w_inc
;
5178 cell
.x
+= r_g
.w
- msize
;
5181 colno
= split
= winno
/ stacks
;
5182 cell
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
5183 2 * border_width
) / stacks
);
5186 hrh
= r_g
.h
/ colno
;
5187 extra
= r_g
.h
- (colno
* hrh
);
5188 cell
.h
= hrh
- 2 * border_width
;
5189 i
= j
= 0, s
= stacks
;
5192 /* Update window geometry. */
5193 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
5197 if (FLOATING(win
)) {
5198 update_floater(win
);
5203 if (split
&& i
== split
) {
5204 colno
= (winno
- mwin
) / stacks
;
5205 if (s
<= (winno
- mwin
) % stacks
)
5208 hrh
= r_g
.h
/ colno
;
5209 extra
= r_g
.h
- (colno
* hrh
);
5212 cell
.x
+= cell
.w
+ 2 * border_width
+
5215 cell
.w
= (r_g
.w
- msize
-
5216 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
5218 cell
.w
+= (r_g
.w
- msize
-
5219 (stacks
* (2 * border_width
+ tile_gap
))) %
5223 cell
.x
-= cell
.w
+ 2 * border_width
+
5229 cell
.h
= hrh
- 2 * border_width
- tile_gap
;
5232 h_inc
= win
->sh
.width_inc
;
5233 h_base
= win
->sh
.base_width
;
5235 h_inc
= win
->sh
.height_inc
;
5236 h_base
= win
->sh
.base_height
;
5239 if (j
== colno
- 1) {
5240 cell
.h
= hrh
+ extra
;
5241 } else if (h_inc
> 1 && h_inc
< h_slice
) {
5242 /* adjust for window's requested size increment */
5243 remain
= (cell
.h
- h_base
) % h_inc
;
5244 missing
= h_inc
- remain
;
5246 if (missing
<= extra
|| j
== 0) {
5256 cell
.y
= r_g
.y
+ border_width
;
5258 cell
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
5260 /* Window coordinates exclude frame. */
5262 if (winno
> 1 || !disable_border
||
5263 (bar_enabled
&& ws
->bar_enabled
)) {
5270 if (X(win
) != cell
.y
|| Y(win
) != cell
.x
||
5271 WIDTH(win
) != cell
.h
|| HEIGHT(win
) != cell
.w
) {
5275 WIDTH(win
) = cell
.h
;
5276 HEIGHT(win
) = cell
.w
;
5279 if (X(win
) != cell
.x
|| Y(win
) != cell
.y
||
5280 WIDTH(win
) != cell
.w
|| HEIGHT(win
) != cell
.h
) {
5284 WIDTH(win
) = cell
.w
;
5285 HEIGHT(win
) = cell
.h
;
5290 X(win
) -= border_width
;
5291 Y(win
) -= border_width
;
5292 WIDTH(win
) += 2 * border_width
;
5293 HEIGHT(win
) += 2 * border_width
;
5296 if (bordered
!= win
->bordered
) {
5298 win
->bordered
= bordered
;
5311 /* Stack all windows from bottom up. */
5312 TAILQ_FOREACH_REVERSE(win
, &ws
->stack
, ws_win_stack
, stack_entry
)
5314 update_win_stacking(win
);
5316 /* Map all windows from top down. */
5317 TAILQ_FOREACH(win
, &ws
->stack
, stack_entry
)
5321 DNPRINTF(SWM_D_STACK
, "stack_master: done\n");
5325 vertical_config(struct workspace
*ws
, int id
)
5327 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
5331 case SWM_ARG_ID_STACKRESET
:
5332 case SWM_ARG_ID_STACKINIT
:
5333 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
5334 ws
->l_state
.vertical_mwin
= 1;
5335 ws
->l_state
.vertical_stacks
= 1;
5337 case SWM_ARG_ID_MASTERSHRINK
:
5338 if (ws
->l_state
.vertical_msize
> 1)
5339 ws
->l_state
.vertical_msize
--;
5341 case SWM_ARG_ID_MASTERGROW
:
5342 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
5343 ws
->l_state
.vertical_msize
++;
5345 case SWM_ARG_ID_MASTERADD
:
5346 ws
->l_state
.vertical_mwin
++;
5348 case SWM_ARG_ID_MASTERDEL
:
5349 if (ws
->l_state
.vertical_mwin
> 0)
5350 ws
->l_state
.vertical_mwin
--;
5352 case SWM_ARG_ID_STACKBALANCE
:
5353 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ (ws
->l_state
.vertical_stacks
+ 1);
5355 case SWM_ARG_ID_STACKINC
:
5356 ws
->l_state
.vertical_stacks
++;
5358 case SWM_ARG_ID_STACKDEC
:
5359 if (ws
->l_state
.vertical_stacks
> 1)
5360 ws
->l_state
.vertical_stacks
--;
5362 case SWM_ARG_ID_FLIPLAYOUT
:
5363 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
5371 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5373 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
5375 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
5379 horizontal_config(struct workspace
*ws
, int id
)
5381 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
5384 case SWM_ARG_ID_STACKRESET
:
5385 case SWM_ARG_ID_STACKINIT
:
5386 ws
->l_state
.horizontal_mwin
= 1;
5387 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
5388 ws
->l_state
.horizontal_stacks
= 1;
5390 case SWM_ARG_ID_MASTERSHRINK
:
5391 if (ws
->l_state
.horizontal_msize
> 1)
5392 ws
->l_state
.horizontal_msize
--;
5394 case SWM_ARG_ID_MASTERGROW
:
5395 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
5396 ws
->l_state
.horizontal_msize
++;
5398 case SWM_ARG_ID_MASTERADD
:
5399 ws
->l_state
.horizontal_mwin
++;
5401 case SWM_ARG_ID_MASTERDEL
:
5402 if (ws
->l_state
.horizontal_mwin
> 0)
5403 ws
->l_state
.horizontal_mwin
--;
5405 case SWM_ARG_ID_STACKBALANCE
:
5406 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ (ws
->l_state
.horizontal_stacks
+ 1);
5408 case SWM_ARG_ID_STACKINC
:
5409 ws
->l_state
.horizontal_stacks
++;
5411 case SWM_ARG_ID_STACKDEC
:
5412 if (ws
->l_state
.horizontal_stacks
> 1)
5413 ws
->l_state
.horizontal_stacks
--;
5415 case SWM_ARG_ID_FLIPLAYOUT
:
5416 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
5424 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5426 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
5428 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
5431 /* fullscreen view */
5433 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5435 struct swm_geometry gg
= *g
;
5436 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
, *tmpw
;
5439 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
5444 winno
= count_win(ws
, false);
5445 if (winno
== 0 && count_win(ws
, true) == 0)
5448 /* Figure out which top level window should be visible. */
5449 if (ws
->focus_pending
)
5450 win
= ws
->focus_pending
;
5453 else if (ws
->focus_prev
)
5454 win
= ws
->focus_prev
;
5456 win
= TAILQ_FIRST(&ws
->winlist
);
5458 DNPRINTF(SWM_D_STACK
, "max_stack: focus_pending: %#x, focus: %#x, "
5459 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws
->focus_pending
),
5460 WINID(ws
->focus
), WINID(ws
->focus_prev
),
5461 WINID(TAILQ_FIRST(&ws
->winlist
)), win
->id
);
5463 /* Update window geometry. */
5464 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5473 /* Set maximized flag for all maxed windows. */
5474 if (!MAXIMIZED(w
)) {
5475 /* Preserve floating geometry. */
5477 store_float_geom(w
);
5479 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_MAXIMIZED
);
5480 ewmh_update_wm_state(w
);
5483 /* Only reconfigure if necessary. */
5484 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
5485 HEIGHT(w
) != gg
.h
) {
5488 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
)) {
5489 w
->bordered
= false;
5490 WIDTH(w
) += 2 * border_width
;
5491 HEIGHT(w
) += 2 * border_width
;
5494 X(w
) += border_width
;
5495 Y(w
) += border_width
;
5502 /* If transient, stack parent and its children. */
5503 if (TRANS(win
) && (parent
= find_window(win
->transient
))) {
5504 raise_window(parent
);
5506 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5507 if (w
->transient
== parent
->id
)
5511 /* Make sure focus window is on top. */
5514 /* Stack any children of focus window. */
5515 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5516 if (w
->transient
== win
->id
)
5519 /* Map all windows. */
5520 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
5526 send_to_rg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5528 int ridx
= args
->id
, i
, num_screens
;
5529 struct swm_region
*rr
= NULL
;
5532 num_screens
= get_screen_count();
5533 /* do nothing if we don't have more than one screen */
5534 if (!(num_screens
> 1 || outputs
> 1))
5537 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
5539 rr
= TAILQ_FIRST(&r
->s
->rl
);
5540 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
5541 rr
= TAILQ_NEXT(rr
, entry
);
5548 send_to_ws(bp
, r
, &a
);
5552 region_under(struct swm_screen
*s
, int x
, int y
)
5554 struct swm_region
*r
= NULL
;
5556 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
5557 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
5558 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
5559 WIDTH(r
), HEIGHT(r
), x
, y
);
5560 if (X(r
) <= x
&& x
< MAX_X(r
))
5561 if (Y(r
) <= y
&& y
< MAX_Y(r
))
5568 /* Transfer focused window to target workspace and focus. */
5570 send_to_ws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5572 int wsid
= args
->id
;
5573 struct ws_win
*win
= NULL
;
5577 if (r
&& r
->ws
&& r
->ws
->focus
)
5582 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win %#x, ws %d\n", win
->id
, wsid
);
5584 if (wsid
< 0 || wsid
>= workspace_limit
)
5587 if (win
->ws
->idx
== wsid
)
5590 win_to_ws(win
, wsid
, true);
5592 /* Set new focus on target ws. */
5593 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5594 win
->ws
->focus_prev
= win
->ws
->focus
;
5595 win
->ws
->focus
= win
;
5596 win
->ws
->focus_pending
= NULL
;
5598 if (win
->ws
->focus_prev
)
5599 draw_frame(win
->ws
->focus_prev
);
5602 DNPRINTF(SWM_D_STACK
, "send_to_ws: focus_pending: %#x, focus: %#x, "
5603 "focus_prev: %#x, first: %#x, win: %#x\n",
5604 WINID(r
->ws
->focus_pending
), WINID(r
->ws
->focus
),
5605 WINID(r
->ws
->focus_prev
), WINID(TAILQ_FIRST(&r
->ws
->winlist
)),
5608 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5609 ewmh_update_wm_state(win
);
5611 /* Restack focused region. */
5614 /* If destination ws has a region, restack. */
5617 load_float_geom(win
);
5622 /* Set new focus on current ws. */
5623 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5624 if (r
->ws
->focus
!= NULL
) {
5625 focus_win(r
->ws
->focus
);
5627 DNPRINTF(SWM_D_FOCUS
, "send_to_ws: set_input_focus: "
5628 "%#x, revert-to: parent, time: 0\n", r
->id
);
5629 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
5630 r
->id
, XCB_CURRENT_TIME
);
5639 /* Transfer focused window to region-relative workspace and focus. */
5641 send_to_ws_relative(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5644 struct swm_region
*r_other
;
5646 if (args
->id
== 1) {
5647 r_other
= TAILQ_NEXT(r
, entry
);
5648 if (r_other
== NULL
)
5649 r_other
= TAILQ_FIRST(&r
->s
->rl
);
5651 r_other
= TAILQ_PREV(r
, swm_region_list
, entry
);
5652 if (r_other
== NULL
)
5653 r_other
= TAILQ_LAST(&r
->s
->rl
, swm_region_list
);
5656 /* Map relative to absolute */
5658 args_abs
.id
= r_other
->ws
->idx
;
5660 send_to_ws(bp
, r
, &args_abs
);
5664 win_to_ws(struct ws_win
*win
, int wsid
, bool unfocus
)
5666 struct ws_win
*parent
;
5667 struct workspace
*ws
, *nws
, *pws
;
5669 if (wsid
< 0 || wsid
>= workspace_limit
)
5672 if (win
->ws
->idx
== wsid
)
5676 nws
= &win
->s
->ws
[wsid
];
5678 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win %#x, ws %d -> %d\n", win
->id
,
5681 /* Cleanup focus on source ws. */
5682 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
5683 (ws
->focus
== win
|| ws
->focus_pending
== win
))
5684 ws
->focus_pending
= get_focus_prev(win
);
5686 /* Move the parent if this is a transient window. */
5688 parent
= find_window(win
->transient
);
5691 /* Set new focus in parent's ws if needed. */
5692 if (pws
->focus
== parent
) {
5693 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5694 pws
->focus_pending
=
5695 get_focus_prev(parent
);
5697 unfocus_win(parent
);
5699 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5700 pws
->focus
= pws
->focus_pending
;
5701 pws
->focus_pending
= NULL
;
5705 /* Don't unmap parent if new ws is visible */
5707 unmap_window(parent
);
5710 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
5711 TAILQ_REMOVE(&ws
->stack
, parent
, stack_entry
);
5712 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
5713 TAILQ_INSERT_TAIL(&nws
->stack
, parent
, stack_entry
);
5716 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5717 "_NET_WM_DESKTOP: %d\n", wsid
);
5718 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5719 parent
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
5720 XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5727 if (ws
->focus_prev
== win
)
5728 ws
->focus_prev
= NULL
;
5730 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& ws
->focus_pending
!= NULL
) {
5731 ws
->focus
= ws
->focus_pending
;
5732 ws
->focus_pending
= NULL
;
5735 /* Don't unmap if new ws is visible */
5740 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
5741 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
5742 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
5743 TAILQ_INSERT_TAIL(&nws
->stack
, win
, stack_entry
);
5746 /* Update the window's workspace property: _NET_WM_DESKTOP */
5747 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5748 "_NET_WM_DESKTOP: %d\n", wsid
);
5749 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
5750 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5752 ewmh_update_client_list();
5754 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
5758 pressbutton(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5760 /* suppress unused warning since var is needed */
5764 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
5765 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5766 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
5767 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5771 raise_focused(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5776 /* Suppress warning. */
5780 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
5784 r
->ws
->raised
= win
;
5787 /* Temporarily override stacking order also in the stack */
5788 if (!FLOATING(win
)) {
5789 val
= XCB_STACK_MODE_ABOVE
;
5790 xcb_configure_window(conn
, win
->frame
,
5791 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
5796 raise_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5798 /* Suppress warning. */
5802 if (r
== NULL
|| r
->ws
== NULL
)
5805 if (r
->ws
->focus
&& MAXIMIZED(r
->ws
->focus
))
5808 r
->ws
->always_raise
= !r
->ws
->always_raise
;
5810 /* Update focused win stacking order based on new always_raise value. */
5811 raise_window(r
->ws
->focus
);
5817 iconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5821 /* Suppress warning. */
5825 if ((w
= r
->ws
->focus
) == NULL
)
5828 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_HIDDEN
);
5829 ewmh_update_wm_state(w
);
5837 get_win_name(xcb_window_t win
)
5840 xcb_get_property_cookie_t c
;
5841 xcb_get_property_reply_t
*r
;
5843 /* First try _NET_WM_NAME for UTF-8. */
5844 c
= xcb_get_property(conn
, 0, win
, ewmh
[_NET_WM_NAME
].atom
,
5845 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5846 r
= xcb_get_property_reply(conn
, c
, NULL
);
5847 if (r
&& r
->type
== XCB_NONE
) {
5849 /* Use WM_NAME instead; no UTF-8. */
5850 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
5851 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5852 r
= xcb_get_property_reply(conn
, c
, NULL
);
5855 if (r
&& r
->type
!= XCB_NONE
&& r
->length
> 0)
5856 name
= strndup(xcb_get_property_value(r
),
5857 xcb_get_property_value_length(r
));
5862 err(1, "get_win_name: failed to allocate memory.");
5870 uniconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5879 DNPRINTF(SWM_D_MISC
, "uniconify\n");
5881 if (r
== NULL
|| r
->ws
== NULL
)
5884 /* make sure we have anything to uniconify */
5885 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5886 if (win
->ws
== NULL
)
5887 continue; /* should never happen */
5893 DNPRINTF(SWM_D_MISC
, "uniconify: count: %d\n", count
);
5899 search_resp_action
= SWM_SEARCH_UNICONIFY
;
5901 spawn_select(r
, args
, "search", &searchpid
);
5903 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5906 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5907 if (win
->ws
== NULL
)
5908 continue; /* should never happen */
5912 name
= get_win_name(win
->id
);
5913 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
5921 name_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5927 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
5933 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
5935 spawn_select(r
, args
, "name_workspace", &searchpid
);
5937 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5940 fprintf(lfile
, "%s", "");
5945 search_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5948 struct workspace
*ws
;
5953 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
5959 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
5961 spawn_select(r
, args
, "search", &searchpid
);
5963 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5966 for (i
= 0; i
< workspace_limit
; i
++) {
5970 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
5971 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
5978 search_win_cleanup(void)
5980 struct search_window
*sw
= NULL
;
5982 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
5983 xcb_destroy_window(conn
, sw
->indicator
);
5984 TAILQ_REMOVE(&search_wl
, sw
, entry
);
5990 search_win(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5992 struct ws_win
*win
= NULL
;
5993 struct search_window
*sw
= NULL
;
5996 xcb_screen_t
*screen
;
5997 int i
, width
, height
;
6005 XRectangle l_ibox
, l_lbox
;
6009 DNPRINTF(SWM_D_MISC
, "search_win\n");
6012 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
6014 spawn_select(r
, args
, "search", &searchpid
);
6016 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
6019 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
6020 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
6022 TAILQ_INIT(&search_wl
);
6025 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
6029 sw
= calloc(1, sizeof(struct search_window
));
6031 warn("search_win: calloc");
6033 search_win_cleanup();
6039 snprintf(s
, sizeof s
, "%d", i
);
6042 w
= xcb_generate_id(conn
);
6043 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6044 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6045 wa
[2] = screen
->default_colormap
;
6047 if (bar_font_legacy
) {
6048 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
6049 width
= l_lbox
.width
+ 4;
6050 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
6052 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
6054 width
= info
.width
+ 4;
6055 height
= bar_font
->height
+ 4;
6058 xcb_create_window(conn
, screen
->root_depth
, w
, win
->frame
, 0, 0,
6059 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
6060 screen
->root_visual
, XCB_CW_BACK_PIXEL
|
6061 XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
, wa
);
6063 xcb_map_window(conn
, w
);
6066 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
6068 if (bar_font_legacy
) {
6069 l_gcv
.graphics_exposures
= 0;
6070 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
6072 XSetForeground(display
, l_draw
,
6073 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
6075 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
6076 (bar_fs_extents
->max_logical_extent
.height
-
6077 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
6079 XFreeGC(display
, l_draw
);
6082 draw
= XftDrawCreate(display
, w
,
6083 DefaultVisual(display
, r
->s
->idx
),
6084 DefaultColormap(display
, r
->s
->idx
));
6086 XftDrawStringUtf8(draw
, &search_font_color
, bar_font
, 2,
6087 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
6088 bar_font
->descent
, (FcChar8
*)s
, len
);
6090 XftDrawDestroy(draw
);
6093 DNPRINTF(SWM_D_MISC
, "search_win: mapped win %#x\n", w
);
6095 fprintf(lfile
, "%d\n", i
);
6105 search_resp_uniconify(const char *resp
, size_t len
)
6111 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
6113 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
6116 name
= get_win_name(win
->id
);
6117 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
6122 if (strncmp(s
, resp
, len
) == 0) {
6123 /* XXX this should be a callback to generalize */
6124 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_HIDDEN
);
6125 ewmh_update_wm_state(win
);
6135 search_resp_name_workspace(const char *resp
, size_t len
)
6137 struct workspace
*ws
;
6139 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
6141 if (search_r
->ws
== NULL
)
6146 free(search_r
->ws
->name
);
6147 search_r
->ws
->name
= NULL
;
6151 ws
->name
= strdup(resp
);
6152 if (ws
->name
== NULL
) {
6153 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
6154 "strdup: %s", strerror(errno
));
6158 ewmh_update_desktop_names();
6159 ewmh_get_desktop_names();
6164 ewmh_update_desktop_names(void)
6166 char *name_list
= NULL
, *p
;
6167 int num_screens
, i
, j
, len
= 0, tot
= 0;
6169 num_screens
= get_screen_count();
6170 for (i
= 0; i
< num_screens
; ++i
) {
6171 for (j
= 0; j
< workspace_limit
; ++j
) {
6172 if (screens
[i
].ws
[j
].name
!= NULL
)
6173 len
+= strlen(screens
[i
].ws
[j
].name
);
6177 if((name_list
= calloc(len
, sizeof(char))) == NULL
)
6178 err(1, "update_desktop_names: calloc: failed to "
6179 "allocate memory.");
6182 for (j
= 0; j
< workspace_limit
; ++j
) {
6183 if (screens
[i
].ws
[j
].name
!= NULL
) {
6184 len
= strlen(screens
[i
].ws
[j
].name
);
6185 memcpy(p
, screens
[i
].ws
[j
].name
, len
);
6194 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6195 screens
[i
].root
, ewmh
[_NET_DESKTOP_NAMES
].atom
,
6196 a_utf8_string
, 8, tot
, name_list
);
6206 ewmh_get_desktop_names(void)
6209 xcb_get_property_cookie_t c
;
6210 xcb_get_property_reply_t
*r
;
6211 int num_screens
, i
, j
, n
, k
;
6213 num_screens
= get_screen_count();
6214 for (i
= 0; i
< num_screens
; ++i
) {
6215 for (j
= 0; j
< workspace_limit
; ++j
) {
6216 free(screens
[i
].ws
[j
].name
);
6217 screens
[i
].ws
[j
].name
= NULL
;
6220 c
= xcb_get_property(conn
, 0, screens
[i
].root
,
6221 ewmh
[_NET_DESKTOP_NAMES
].atom
,
6222 a_utf8_string
, 0, UINT32_MAX
);
6223 r
= xcb_get_property_reply(conn
, c
, NULL
);
6227 names
= xcb_get_property_value(r
);
6228 n
= xcb_get_property_value_length(r
);
6230 for (j
= 0, k
= 0; j
< n
; ++j
) {
6231 if (*(names
+ j
) != '\0') {
6232 screens
[i
].ws
[k
].name
= strdup(names
+ j
);
6233 j
+= strlen(names
+ j
);
6242 ewmh_update_client_list(void)
6245 int num_screens
, i
, j
, k
= 0, count
= 0;
6248 num_screens
= get_screen_count();
6249 for (i
= 0; i
< num_screens
; ++i
) {
6250 for (j
= 0; j
< workspace_limit
; ++j
)
6251 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6254 DNPRINTF(SWM_D_PROP
, "ewmh_update_client_list: win count: %d\n",
6260 wins
= calloc(count
, sizeof(xcb_window_t
));
6262 err(1, "ewmh_update_client_list: calloc: failed to "
6263 "allocate memory.");
6265 for (j
= 0, k
= 0; j
< workspace_limit
; ++j
)
6266 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6267 wins
[k
++] = win
->id
;
6269 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6270 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
6271 XCB_ATOM_WINDOW
, 32, count
, wins
);
6278 ewmh_update_current_desktop(void)
6282 num_screens
= get_screen_count();
6283 for (i
= 0; i
< num_screens
; ++i
) {
6284 if (screens
[i
].r_focus
)
6285 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6286 screens
[i
].root
, ewmh
[_NET_CURRENT_DESKTOP
].atom
,
6287 XCB_ATOM_CARDINAL
, 32, 1,
6288 &screens
[i
].r_focus
->ws
->idx
);
6293 ewmh_update_desktops(void)
6295 int num_screens
, i
, j
;
6298 vals
= calloc(workspace_limit
* 2, sizeof(uint32_t));
6300 err(1, "ewmh_update_desktops: calloc: failed to allocate "
6303 num_screens
= get_screen_count();
6304 for (i
= 0; i
< num_screens
; i
++) {
6305 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6306 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
6307 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
6309 for (j
= 0; j
< workspace_limit
; ++j
) {
6310 if (screens
[i
].ws
[j
].r
!= NULL
) {
6311 vals
[j
* 2] = X(screens
[i
].ws
[j
].r
);
6312 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].r
);
6313 } else if (screens
[i
].ws
[j
].old_r
!= NULL
) {
6314 vals
[j
* 2] = X(screens
[i
].ws
[j
].old_r
);
6315 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].old_r
);
6317 vals
[j
* 2] = vals
[j
* 2 + 1] = 0;
6321 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6322 screens
[i
].root
, ewmh
[_NET_DESKTOP_VIEWPORT
].atom
,
6323 XCB_ATOM_CARDINAL
, 32, workspace_limit
* 2, vals
);
6330 search_resp_search_workspace(const char *resp
)
6337 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
6341 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
6348 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
6350 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
6357 switchws(NULL
, search_r
, &a
);
6361 search_resp_search_window(const char *resp
)
6366 struct search_window
*sw
;
6368 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
6372 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
6377 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
6379 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
6386 TAILQ_FOREACH(sw
, &search_wl
, entry
)
6387 if (idx
== sw
->idx
) {
6393 #define MAX_RESP_LEN 1024
6396 search_do_resp(void)
6402 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
6407 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
6408 warn("search: calloc");
6412 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
6414 warn("search: read error");
6417 resp
[rbytes
] = '\0';
6420 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
6421 * newline, so work around that by sanitizing the resp now.
6423 resp
[strcspn(resp
, "\n")] = '\0';
6426 switch (search_resp_action
) {
6427 case SWM_SEARCH_UNICONIFY
:
6428 search_resp_uniconify(resp
, len
);
6430 case SWM_SEARCH_NAME_WORKSPACE
:
6431 search_resp_name_workspace(resp
, len
);
6433 case SWM_SEARCH_SEARCH_WORKSPACE
:
6434 search_resp_search_workspace(resp
);
6436 case SWM_SEARCH_SEARCH_WINDOW
:
6437 search_resp_search_window(resp
);
6442 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
6443 search_win_cleanup();
6445 search_resp_action
= SWM_SEARCH_NONE
;
6446 close(select_resp_pipe
[0]);
6453 wkill(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6457 DNPRINTF(SWM_D_MISC
, "wkill: win %#x, id: %d\n", WINID(r
->ws
->focus
),
6460 if (r
->ws
->focus
== NULL
)
6463 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
6464 xcb_kill_client(conn
, r
->ws
->focus
->id
);
6466 if (r
->ws
->focus
->can_delete
)
6467 client_msg(r
->ws
->focus
, a_delete
, 0);
6473 clear_maximized(struct workspace
*ws
)
6478 /* Clear any maximized win(s) on ws, from bottom up. */
6479 TAILQ_FOREACH_REVERSE(w
, &ws
->stack
, ws_win_stack
, stack_entry
)
6481 ewmh_apply_flags(w
, w
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
6482 ewmh_update_wm_state(w
);
6490 maximize_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6492 struct ws_win
*w
= r
->ws
->focus
;
6494 /* suppress unused warning since var is needed */
6501 DNPRINTF(SWM_D_MISC
, "maximize_toggle: win %#x\n", w
->id
);
6506 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6509 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_MAXIMIZED
);
6510 ewmh_update_wm_state(w
);
6514 if (w
== w
->ws
->focus
)
6519 DNPRINTF(SWM_D_MISC
, "maximize_toggle: done\n");
6523 floating_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6525 struct ws_win
*w
= r
->ws
->focus
;
6527 /* suppress unused warning since var is needed */
6534 DNPRINTF(SWM_D_MISC
, "floating_toggle: win %#x\n", w
->id
);
6536 if (FULLSCREEN(w
) || TRANS(w
))
6539 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6542 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_ABOVE
);
6543 ewmh_update_wm_state(w
);
6547 if (w
== w
->ws
->focus
)
6552 DNPRINTF(SWM_D_MISC
, "floating_toggle: done\n");
6556 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
6558 struct swm_geometry g
= r
->g
;
6559 int rt
, lt
, tp
, bm
, bw
;
6561 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
6564 * Perpendicular distance of each side of the window to the respective
6565 * side of the region boundary. Positive values indicate the side of
6566 * the window has passed beyond the region boundary.
6568 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) - MAX_X(r
) : bw
;
6569 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) : bw
;
6570 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) - MAX_Y(r
) : bw
;
6571 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) : bw
;
6573 DNPRINTF(SWM_D_MISC
, "region_containment: win %#x, rt: %d, lt: %d, "
6574 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
6575 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
6576 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
6579 * Disable containment if any of the flagged sides went beyond the
6580 * containment boundary, or if containment is disabled.
6582 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
6583 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
6584 /* Make sure window has at least 1 pixel in the region */
6585 g
.x
+= 1 - WIDTH(win
);
6586 g
.y
+= 1 - HEIGHT(win
);
6587 g
.w
+= 2 * WIDTH(win
) - 2;
6588 g
.h
+= 2 * HEIGHT(win
) - 2;
6591 constrain_window(win
, &g
, &opts
);
6594 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6596 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
6598 DNPRINTF(SWM_D_MISC
, "constrain_window: win %#x, (x,y) w x h: "
6599 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6600 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
6601 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
6602 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
6603 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
6604 YESNO(*opts
& SWM_CW_RESIZABLE
));
6606 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) > b
->x
+ b
->w
) {
6607 if (*opts
& SWM_CW_RESIZABLE
)
6608 WIDTH(win
) = b
->x
+ b
->w
- X(win
);
6610 X(win
) = b
->x
+ b
->w
- WIDTH(win
);
6613 if ((*opts
& SWM_CW_LEFT
) && X(win
) < b
->x
) {
6614 if (*opts
& SWM_CW_RESIZABLE
)
6615 WIDTH(win
) -= b
->x
- X(win
);
6620 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) > b
->y
+ b
->h
) {
6621 if (*opts
& SWM_CW_RESIZABLE
)
6622 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
);
6624 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
);
6627 if ((*opts
& SWM_CW_TOP
) && Y(win
) < b
->y
) {
6628 if (*opts
& SWM_CW_RESIZABLE
)
6629 HEIGHT(win
) -= b
->y
- Y(win
);
6634 if (*opts
& SWM_CW_RESIZABLE
) {
6637 if (HEIGHT(win
) < 1)
6643 draw_frame(struct ws_win
*win
)
6645 xcb_rectangle_t rect
[4];
6649 if (win
->frame
== XCB_WINDOW_NONE
) {
6650 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x not "
6651 "reparented.\n", win
->id
);
6655 if (!win
->bordered
) {
6656 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x frame "
6657 "disabled\n", win
->id
);
6660 if (WS_FOCUSED(win
->ws
) && win
->ws
->focus
== win
)
6661 pixel
= MAXIMIZED(win
) ?
6662 &win
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].pixel
:
6663 &win
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6665 pixel
= MAXIMIZED(win
) ?
6666 &win
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].pixel
:
6667 &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6669 /* Top (with corners) */
6672 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6673 rect
[n
].height
= border_width
;
6674 /* Left (without corners) */
6676 rect
[n
].y
= border_width
;
6677 rect
[n
].width
= border_width
;
6678 rect
[n
].height
= HEIGHT(win
);
6679 /* Right (without corners) */
6680 rect
[++n
].x
= border_width
+ WIDTH(win
);
6681 rect
[n
].y
= border_width
;
6682 rect
[n
].width
= border_width
;
6683 rect
[n
].height
= HEIGHT(win
);
6684 /* Bottom (with corners)*/
6686 rect
[n
].y
= border_width
+ HEIGHT(win
);
6687 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6688 rect
[n
].height
= border_width
;
6690 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, pixel
);
6691 xcb_poly_fill_rectangle(conn
, win
->frame
, win
->s
->bar_gc
, 4, rect
);
6695 update_window(struct ws_win
*win
)
6700 if (win
->frame
== XCB_WINDOW_NONE
) {
6701 DNPRINTF(SWM_D_EVENT
, "update_window: skip win %#x; "
6702 "not reparented.\n", win
->id
);
6706 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
6707 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
6708 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6710 /* Reconfigure frame. */
6711 if (win
->bordered
) {
6712 wc
[0] = X(win
) - border_width
;
6713 wc
[1] = Y(win
) - border_width
;
6714 wc
[2] = WIDTH(win
) + 2 * border_width
;
6715 wc
[3] = HEIGHT(win
) + 2 * border_width
;
6720 wc
[3] = HEIGHT(win
);
6725 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x frame %#x, (x,y) w x h: "
6726 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, win
->frame
, wc
[0],
6727 wc
[1], wc
[2], wc
[3], YESNO(win
->bordered
));
6729 xcb_configure_window(conn
, win
->frame
, mask
, wc
);
6731 /* Reconfigure client window. */
6732 wc
[0] = wc
[1] = win
->bordered
? border_width
: 0;
6734 wc
[3] = HEIGHT(win
);
6736 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x, (x,y) w x h: "
6737 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
6738 wc
[3], YESNO(win
->bordered
));
6739 xcb_configure_window(conn
, win
->id
, mask
, wc
);
6741 /* ICCCM 4.2.3 Synthetic ConfigureNotify when moved and not resized. */
6742 if ((X(win
) != win
->g_prev
.x
|| Y(win
) != win
->g_prev
.y
) &&
6743 (win
->java
|| (WIDTH(win
) == win
->g_prev
.w
&&
6744 HEIGHT(win
) == win
->g_prev
.h
))) {
6745 /* Java has special needs when moved together with a resize. */
6746 config_win(win
, NULL
);
6749 win
->g_prev
= win
->g
;
6753 SIMPLEQ_ENTRY(event
) entry
;
6754 xcb_generic_event_t
*ev
;
6756 SIMPLEQ_HEAD(event_queue
, event
) events
= SIMPLEQ_HEAD_INITIALIZER(events
);
6758 xcb_generic_event_t
*
6759 get_next_event(bool dowait
)
6762 xcb_generic_event_t
*evt
;
6764 /* Try queue first. */
6765 if ((ep
= SIMPLEQ_FIRST(&events
))) {
6767 SIMPLEQ_REMOVE_HEAD(&events
, entry
);
6770 evt
= xcb_wait_for_event(conn
);
6772 evt
= xcb_poll_for_event(conn
);
6778 put_back_event(xcb_generic_event_t
*evt
)
6781 if ((ep
= malloc(sizeof (struct event
))) == NULL
)
6782 err(1, "put_back_event: failed to allocate memory.");
6784 SIMPLEQ_INSERT_HEAD(&events
, ep
, entry
);
6787 /* Peeks at next event to detect auto-repeat. */
6789 keyrepeating(xcb_key_release_event_t
*kre
)
6791 xcb_generic_event_t
*evt
;
6793 /* Ensure repeating keypress is finished processing. */
6796 if ((evt
= get_next_event(false))) {
6797 put_back_event(evt
);
6799 if (XCB_EVENT_RESPONSE_TYPE(evt
) == XCB_KEY_PRESS
&&
6800 kre
->sequence
== evt
->sequence
&&
6801 kre
->detail
== ((xcb_key_press_event_t
*)evt
)->detail
)
6809 keybindreleased(struct binding
*bp
, xcb_key_release_event_t
*kre
)
6811 if (bp
->type
== KEYBIND
&& !keyrepeating(kre
) &&
6812 bp
->value
== xcb_key_press_lookup_keysym(syms
, kre
, 0))
6818 #define SWM_RESIZE_STEPS (50)
6821 resize_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
6823 xcb_timestamp_t timestamp
= 0;
6824 struct swm_region
*r
= NULL
;
6825 struct swm_geometry g
;
6826 int top
= 0, left
= 0;
6828 xcb_cursor_t cursor
;
6829 xcb_query_pointer_reply_t
*xpr
= NULL
;
6830 xcb_generic_event_t
*evt
;
6831 xcb_motion_notify_event_t
*mne
;
6832 bool resizing
, step
= false;
6838 if (FULLSCREEN(win
))
6841 /* In max_stack mode, should only resize transients. */
6842 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6845 DNPRINTF(SWM_D_EVENT
, "resize: win %#x, floating: %s, "
6846 "transient: %#x\n", win
->id
, YESNO(ABOVE(win
)),
6850 store_float_geom(win
);
6851 else if (!(TRANS(win
) || ABOVE(win
)))
6854 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6856 ewmh_update_wm_state(win
);
6862 /* It's possible for win to have been freed during focus_flush(). */
6863 if (validate_win(win
)) {
6864 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6869 case SWM_ARG_ID_WIDTHSHRINK
:
6870 WIDTH(win
) -= SWM_RESIZE_STEPS
;
6873 case SWM_ARG_ID_WIDTHGROW
:
6874 WIDTH(win
) += SWM_RESIZE_STEPS
;
6877 case SWM_ARG_ID_HEIGHTSHRINK
:
6878 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
6881 case SWM_ARG_ID_HEIGHTGROW
:
6882 HEIGHT(win
) += SWM_RESIZE_STEPS
;
6889 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6890 SWM_CW_HARDBOUNDARY
);
6892 store_float_geom(win
);
6896 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6897 SWM_CW_SOFTBOUNDARY
);
6900 /* get cursor offset from window root */
6901 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6908 if (xpr
->win_x
< WIDTH(win
) / 2)
6911 if (xpr
->win_y
< HEIGHT(win
) / 2)
6914 if (opt
== SWM_ARG_ID_CENTER
)
6915 cursor
= cursors
[XC_SIZING
].cid
;
6917 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
6918 XC_TOP_RIGHT_CORNER
].cid
;
6920 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
6921 XC_BOTTOM_RIGHT_CORNER
].cid
;
6923 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6924 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
6927 /* Release keyboard freeze if called via keybind. */
6928 if (bp
->type
== KEYBIND
)
6929 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6934 while (resizing
&& (evt
= get_next_event(true))) {
6935 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6936 case XCB_BUTTON_RELEASE
:
6937 if (bp
->type
== BTNBIND
&& bp
->value
==
6938 ((xcb_button_release_event_t
*)evt
)->detail
)
6941 case XCB_KEY_RELEASE
:
6942 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
6945 case XCB_MOTION_NOTIFY
:
6946 mne
= (xcb_motion_notify_event_t
*)evt
;
6947 DNPRINTF(SWM_D_EVENT
, "resize: MOTION_NOTIFY: "
6948 "root: %#x\n", mne
->root
);
6950 /* cursor offset/delta from start of the operation */
6951 dx
= mne
->root_x
- xpr
->root_x
;
6952 dy
= mne
->root_y
- xpr
->root_y
;
6958 if (opt
== SWM_ARG_ID_CENTER
) {
6959 if (g
.h
/ 2 + dy
< 1)
6963 HEIGHT(win
) = g
.h
+ 2 * dy
;
6971 HEIGHT(win
) = g
.h
+ dy
;
6978 if (opt
== SWM_ARG_ID_CENTER
) {
6979 if (g
.w
/ 2 + dx
< 1)
6983 WIDTH(win
) = g
.w
+ 2 * dx
;
6991 WIDTH(win
) = g
.w
+ dx
;
6994 /* not free, don't sync more than 120 times / second */
6995 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6996 timestamp
= mne
->time
;
6997 regionize(win
, mne
->root_x
, mne
->root_y
);
6998 region_containment(win
, r
, SWM_CW_ALLSIDES
|
6999 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
7000 SWM_CW_SOFTBOUNDARY
);
7005 case XCB_BUTTON_PRESS
:
7007 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_PRESS ignored\n");
7008 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7009 ((xcb_button_press_event_t
*)evt
)->time
);
7014 DNPRINTF(SWM_D_EVENT
, "resize: KEY_PRESS ignored\n");
7015 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7016 ((xcb_key_press_event_t
*)evt
)->time
);
7022 /* It's possible for win to have been freed above. */
7023 if (validate_win(win
)) {
7024 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
7032 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
7033 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
7037 store_float_geom(win
);
7039 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7041 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
7045 resize(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
7047 struct ws_win
*win
= NULL
;
7048 xcb_query_pointer_reply_t
*qpr
= NULL
;
7053 if (args
->id
!= SWM_ARG_ID_DONTCENTER
&&
7054 args
->id
!= SWM_ARG_ID_CENTER
) {
7055 /* {height,width}_{grow,shrink} use the focus window. */
7059 /* resize uses window under pointer. */
7060 qpr
= xcb_query_pointer_reply(conn
,
7061 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7063 win
= find_window(qpr
->child
);
7069 resize_win(win
, bp
, args
->id
);
7071 if (args
->id
&& bp
->type
== KEYBIND
)
7077 /* Try to set window region based on supplied coordinates or window center. */
7079 regionize(struct ws_win
*win
, int x
, int y
)
7081 struct swm_region
*r
= NULL
;
7083 r
= region_under(win
->s
, x
, y
);
7085 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
7086 Y(win
) + HEIGHT(win
) / 2);
7088 if (r
&& r
!= win
->ws
->r
) {
7089 clear_maximized(r
->ws
);
7091 win_to_ws(win
, r
->ws
->idx
, false);
7093 /* Stack old and new region. */
7097 /* Set focus on new ws. */
7098 unfocus_win(r
->ws
->focus
);
7106 #define SWM_MOVE_STEPS (50)
7109 move_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
7111 struct swm_region
*r
;
7112 xcb_timestamp_t timestamp
= 0;
7113 xcb_query_pointer_reply_t
*qpr
= NULL
;
7114 xcb_generic_event_t
*evt
;
7115 xcb_motion_notify_event_t
*mne
;
7116 bool moving
, restack
= false, step
= false;
7121 if ((r
= win
->ws
->r
) == NULL
)
7124 if (FULLSCREEN(win
))
7127 DNPRINTF(SWM_D_EVENT
, "move: win %#x, floating: %s, transient: "
7128 "%#x\n", win
->id
, YESNO(ABOVE(win
)), win
->transient
);
7130 /* in max_stack mode should only move transients */
7131 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
7134 if (!(ABOVE(win
) || TRANS(win
)) || MAXIMIZED(win
)) {
7135 store_float_geom(win
);
7139 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
7141 ewmh_update_wm_state(win
);
7148 /* It's possible for win to have been freed during focus_flush(). */
7149 if (validate_win(win
)) {
7150 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7155 case SWM_ARG_ID_MOVELEFT
:
7156 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
7159 case SWM_ARG_ID_MOVERIGHT
:
7160 X(win
) += (SWM_MOVE_STEPS
- border_width
);
7163 case SWM_ARG_ID_MOVEUP
:
7164 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
7167 case SWM_ARG_ID_MOVEDOWN
:
7168 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
7175 regionize(win
, -1, -1);
7176 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
7178 store_float_geom(win
);
7182 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
7183 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
7184 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
7186 /* get cursor offset from window root */
7187 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
7190 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7194 /* Release keyboard freeze if called via keybind. */
7195 if (bp
->type
== KEYBIND
)
7196 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7199 regionize(win
, qpr
->root_x
, qpr
->root_y
);
7200 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7201 SWM_CW_SOFTBOUNDARY
);
7205 while (moving
&& (evt
= get_next_event(true))) {
7206 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
7207 case XCB_BUTTON_RELEASE
:
7208 if (bp
->type
== BTNBIND
&& bp
->value
==
7209 ((xcb_button_release_event_t
*)evt
)->detail
)
7212 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7213 ((xcb_button_release_event_t
*)evt
)->time
);
7216 case XCB_KEY_RELEASE
:
7217 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
7220 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7221 ((xcb_key_release_event_t
*)evt
)->time
);
7224 case XCB_MOTION_NOTIFY
:
7225 mne
= (xcb_motion_notify_event_t
*)evt
;
7226 DNPRINTF(SWM_D_EVENT
, "move: MOTION_NOTIFY: "
7227 "root: %#x\n", mne
->root
);
7228 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
7229 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
7231 /* not free, don't sync more than 120 times / second */
7232 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
7233 timestamp
= mne
->time
;
7234 regionize(win
, mne
->root_x
, mne
->root_y
);
7235 region_containment(win
, win
->ws
->r
,
7236 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
7241 case XCB_BUTTON_PRESS
:
7242 /* Thaw and ignore. */
7243 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7244 ((xcb_button_press_event_t
*)evt
)->time
);
7249 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7250 ((xcb_key_press_event_t
*)evt
)->time
);
7256 /* It's possible for win to have been freed. */
7257 if (validate_win(win
)) {
7258 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7266 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7267 SWM_CW_SOFTBOUNDARY
);
7271 store_float_geom(win
);
7273 /* New region set to fullscreen layout. */
7274 if (win
->ws
->r
&& win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
]) {
7281 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7282 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
7286 move(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
7288 struct ws_win
*win
= NULL
;
7289 xcb_query_pointer_reply_t
*qpr
= NULL
;
7295 /* move_* uses focus window. */
7299 /* Disallow move_ on tiled. */
7300 if (win
&& !(TRANS(win
) || ABOVE(win
)))
7303 /* move uses window under pointer. */
7304 qpr
= xcb_query_pointer_reply(conn
,
7305 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7307 win
= find_window(qpr
->child
);
7313 move_win(win
, bp
, args
->id
);
7315 if (args
->id
&& bp
->type
== KEYBIND
)
7321 /* action definitions */
7323 char name
[SWM_FUNCNAME_LEN
];
7324 void (*func
)(struct binding
*, struct swm_region
*,
7328 } actions
[FN_INVALID
+ 2] = {
7329 /* name function argument */
7330 { "bar_toggle", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
7331 { "bar_toggle_ws", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
7332 { "button2", pressbutton
, 0, {.id
= 2} },
7333 { "cycle_layout", cycle_layout
, 0, {0} },
7334 { "flip_layout", stack_config
, 0, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
7335 { "float_toggle", floating_toggle
,0, {0} },
7336 { "focus", focus_pointer
, 0, {0} },
7337 { "focus_main", focus
, 0, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
7338 { "focus_next", focus
, 0, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
7339 { "focus_prev", focus
, 0, {.id
= SWM_ARG_ID_FOCUSPREV
} },
7340 { "focus_urgent", focus
, 0, {.id
= SWM_ARG_ID_FOCUSURGENT
} },
7341 { "maximize_toggle", maximize_toggle
,0, {0} },
7342 { "height_grow", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
7343 { "height_shrink", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
7344 { "iconify", iconify
, 0, {0} },
7345 { "master_shrink", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
7346 { "master_grow", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERGROW
} },
7347 { "master_add", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERADD
} },
7348 { "master_del", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERDEL
} },
7349 { "move", move
, FN_F_NOREPLAY
, {0} },
7350 { "move_down", move
, 0, {.id
= SWM_ARG_ID_MOVEDOWN
} },
7351 { "move_left", move
, 0, {.id
= SWM_ARG_ID_MOVELEFT
} },
7352 { "move_right", move
, 0, {.id
= SWM_ARG_ID_MOVERIGHT
} },
7353 { "move_up", move
, 0, {.id
= SWM_ARG_ID_MOVEUP
} },
7354 { "mvrg_1", send_to_rg
, 0, {.id
= 0} },
7355 { "mvrg_2", send_to_rg
, 0, {.id
= 1} },
7356 { "mvrg_3", send_to_rg
, 0, {.id
= 2} },
7357 { "mvrg_4", send_to_rg
, 0, {.id
= 3} },
7358 { "mvrg_5", send_to_rg
, 0, {.id
= 4} },
7359 { "mvrg_6", send_to_rg
, 0, {.id
= 5} },
7360 { "mvrg_7", send_to_rg
, 0, {.id
= 6} },
7361 { "mvrg_8", send_to_rg
, 0, {.id
= 7} },
7362 { "mvrg_9", send_to_rg
, 0, {.id
= 8} },
7363 { "mvws_1", send_to_ws
, 0, {.id
= 0} },
7364 { "mvws_2", send_to_ws
, 0, {.id
= 1} },
7365 { "mvws_3", send_to_ws
, 0, {.id
= 2} },
7366 { "mvws_4", send_to_ws
, 0, {.id
= 3} },
7367 { "mvws_5", send_to_ws
, 0, {.id
= 4} },
7368 { "mvws_6", send_to_ws
, 0, {.id
= 5} },
7369 { "mvws_7", send_to_ws
, 0, {.id
= 6} },
7370 { "mvws_8", send_to_ws
, 0, {.id
= 7} },
7371 { "mvws_9", send_to_ws
, 0, {.id
= 8} },
7372 { "mvws_10", send_to_ws
, 0, {.id
= 9} },
7373 { "mvws_11", send_to_ws
, 0, {.id
= 10} },
7374 { "mvws_12", send_to_ws
, 0, {.id
= 11} },
7375 { "mvws_13", send_to_ws
, 0, {.id
= 12} },
7376 { "mvws_14", send_to_ws
, 0, {.id
= 13} },
7377 { "mvws_15", send_to_ws
, 0, {.id
= 14} },
7378 { "mvws_16", send_to_ws
, 0, {.id
= 15} },
7379 { "mvws_17", send_to_ws
, 0, {.id
= 16} },
7380 { "mvws_18", send_to_ws
, 0, {.id
= 17} },
7381 { "mvws_19", send_to_ws
, 0, {.id
= 18} },
7382 { "mvws_20", send_to_ws
, 0, {.id
= 19} },
7383 { "mvws_21", send_to_ws
, 0, {.id
= 20} },
7384 { "mvws_22", send_to_ws
, 0, {.id
= 21} },
7385 { "mvws_next", send_to_ws_relative
, 0, {.id
= 1} },
7386 { "mvws_prev", send_to_ws_relative
, 0, {.id
= -1} },
7387 { "name_workspace", name_workspace
, 0, {0} },
7388 { "quit", quit
, 0, {0} },
7389 { "raise_focused", raise_focused
, 0, {0} },
7390 { "raise_toggle", raise_toggle
, 0, {0} },
7391 { "resize", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_DONTCENTER
} },
7392 { "resize_centered", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_CENTER
} },
7393 { "restart", restart
, 0, {0} },
7394 { "rg_1", focusrg
, 0, {.id
= 0} },
7395 { "rg_2", focusrg
, 0, {.id
= 1} },
7396 { "rg_3", focusrg
, 0, {.id
= 2} },
7397 { "rg_4", focusrg
, 0, {.id
= 3} },
7398 { "rg_5", focusrg
, 0, {.id
= 4} },
7399 { "rg_6", focusrg
, 0, {.id
= 5} },
7400 { "rg_7", focusrg
, 0, {.id
= 6} },
7401 { "rg_8", focusrg
, 0, {.id
= 7} },
7402 { "rg_9", focusrg
, 0, {.id
= 8} },
7403 { "rg_move_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_UP
} },
7404 { "rg_move_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_DOWN
} },
7405 { "rg_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7406 { "rg_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7407 { "screen_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7408 { "screen_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7409 { "search_win", search_win
, 0, {0} },
7410 { "search_workspace", search_workspace
, 0, {0} },
7411 { "spawn_custom", NULL
, 0, {0} },
7412 { "stack_balance", stack_config
, 0, {.id
= SWM_ARG_ID_STACKBALANCE
} },
7413 { "stack_inc", stack_config
, 0, {.id
= SWM_ARG_ID_STACKINC
} },
7414 { "stack_dec", stack_config
, 0, {.id
= SWM_ARG_ID_STACKDEC
} },
7415 { "stack_reset", stack_config
, 0, {.id
= SWM_ARG_ID_STACKRESET
} },
7416 { "swap_main", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPMAIN
} },
7417 { "swap_next", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPNEXT
} },
7418 { "swap_prev", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPPREV
} },
7419 { "uniconify", uniconify
, 0, {0} },
7420 { "version", version
, 0, {0} },
7421 { "width_grow", resize
, 0, {.id
= SWM_ARG_ID_WIDTHGROW
} },
7422 { "width_shrink", resize
, 0, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
7423 { "wind_del", wkill
, 0, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
7424 { "wind_kill", wkill
, 0, {.id
= SWM_ARG_ID_KILLWINDOW
} },
7425 { "ws_1", switchws
, 0, {.id
= 0} },
7426 { "ws_2", switchws
, 0, {.id
= 1} },
7427 { "ws_3", switchws
, 0, {.id
= 2} },
7428 { "ws_4", switchws
, 0, {.id
= 3} },
7429 { "ws_5", switchws
, 0, {.id
= 4} },
7430 { "ws_6", switchws
, 0, {.id
= 5} },
7431 { "ws_7", switchws
, 0, {.id
= 6} },
7432 { "ws_8", switchws
, 0, {.id
= 7} },
7433 { "ws_9", switchws
, 0, {.id
= 8} },
7434 { "ws_10", switchws
, 0, {.id
= 9} },
7435 { "ws_11", switchws
, 0, {.id
= 10} },
7436 { "ws_12", switchws
, 0, {.id
= 11} },
7437 { "ws_13", switchws
, 0, {.id
= 12} },
7438 { "ws_14", switchws
, 0, {.id
= 13} },
7439 { "ws_15", switchws
, 0, {.id
= 14} },
7440 { "ws_16", switchws
, 0, {.id
= 15} },
7441 { "ws_17", switchws
, 0, {.id
= 16} },
7442 { "ws_18", switchws
, 0, {.id
= 17} },
7443 { "ws_19", switchws
, 0, {.id
= 18} },
7444 { "ws_20", switchws
, 0, {.id
= 19} },
7445 { "ws_21", switchws
, 0, {.id
= 20} },
7446 { "ws_22", switchws
, 0, {.id
= 21} },
7447 { "ws_next", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
7448 { "ws_next_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
7449 { "ws_next_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
7450 { "ws_prev", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
7451 { "ws_prev_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
7452 { "ws_prev_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
7453 { "ws_prior", priorws
, 0, {0} },
7454 /* SWM_DEBUG actions MUST be here: */
7455 { "debug_toggle", debug_toggle
, 0, {0} },
7456 { "dumpwins", dumpwins
, 0, {0} },
7458 { "invalid action", NULL
, 0, {0} },
7462 update_modkey(uint16_t mod
)
7466 /* Replace all instances of the old mod key. */
7467 RB_FOREACH(bp
, binding_tree
, &bindings
)
7468 if (bp
->mod
& mod_key
)
7469 bp
->mod
= (bp
->mod
& ~mod_key
) | mod
;
7474 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7477 struct spawn_prog
*prog
= NULL
;
7479 char *ap
, **real_args
;
7481 /* suppress unused warning since var is needed */
7484 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
7487 TAILQ_FOREACH(prog
, &spawns
, entry
) {
7488 if (strcasecmp(spawn_name
, prog
->name
) == 0)
7492 warnx("spawn_custom: program %s not found", spawn_name
);
7496 /* make room for expanded args */
7497 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
7498 err(1, "spawn_custom: calloc real_args");
7500 /* expand spawn_args into real_args */
7501 for (i
= c
= 0; i
< prog
->argc
; i
++) {
7503 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
7504 if (strcasecmp(ap
, "$bar_border") == 0) {
7506 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
7508 err(1, "spawn_custom border color");
7509 } else if (strcasecmp(ap
, "$bar_color") == 0) {
7511 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
7513 err(1, "spawn_custom bar color");
7514 } else if (strcasecmp(ap
, "$bar_font") == 0) {
7515 if ((real_args
[c
] = strdup(bar_fonts
))
7517 err(1, "spawn_custom bar fonts");
7518 } else if (strcasecmp(ap
, "$bar_font_color") == 0) {
7520 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
7522 err(1, "spawn_custom color font");
7523 } else if (strcasecmp(ap
, "$color_focus") == 0) {
7525 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
7527 err(1, "spawn_custom color focus");
7528 } else if (strcasecmp(ap
, "$color_focus_maximized") == 0) {
7530 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].name
))
7532 err(1, "spawn_custom color focus maximized");
7533 } else if (strcasecmp(ap
, "$color_unfocus") == 0) {
7535 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
7537 err(1, "spawn_custom color unfocus");
7538 } else if (strcasecmp(ap
, "$color_unfocus_maximized") == 0) {
7540 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].name
))
7542 err(1, "spawn_custom color unfocus maximized");
7543 } else if (strcasecmp(ap
, "$region_index") == 0) {
7544 if (asprintf(&real_args
[c
], "%d",
7545 get_region_index(r
) + 1) < 1)
7546 err(1, "spawn_custom region index");
7547 } else if (strcasecmp(ap
, "$workspace_index") == 0) {
7548 if (asprintf(&real_args
[c
], "%d", r
->ws
->idx
+ 1) < 1)
7549 err(1, "spawn_custom workspace index");
7550 } else if (strcasecmp(ap
, "$dmenu_bottom") == 0) {
7553 if ((real_args
[c
] = strdup("-b")) == NULL
)
7554 err(1, "spawn_custom workspace index");
7556 /* no match --> copy as is */
7557 if ((real_args
[c
] = strdup(ap
)) == NULL
)
7558 err(1, "spawn_custom strdup(ap)");
7560 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
7566 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
7567 for (i
= 0; i
< c
; ++i
)
7568 DPRINTF("\"%s\" ", real_args
[i
]);
7571 *ret_args
= real_args
;
7576 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
7582 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7586 spawn(r
->ws
->idx
, &a
, true);
7588 for (i
= 0; i
< spawn_argc
; i
++)
7594 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7601 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7605 if (pipe(select_list_pipe
) == -1)
7606 err(1, "pipe error");
7607 if (pipe(select_resp_pipe
) == -1)
7608 err(1, "pipe error");
7610 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
7611 err(1, "could not disable SIGPIPE");
7612 switch (*pid
= fork()) {
7614 err(1, "cannot fork");
7617 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
7619 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
7621 close(select_list_pipe
[1]);
7622 close(select_resp_pipe
[0]);
7623 spawn(r
->ws
->idx
, &a
, false);
7625 default: /* parent */
7626 close(select_list_pipe
[0]);
7627 close(select_resp_pipe
[1]);
7631 for (i
= 0; i
< spawn_argc
; i
++)
7636 /* Argument tokenizer. */
7639 char *arg
, *cp
, *next
;
7640 bool single_quoted
= false, double_quoted
= false;
7645 /* Eat and move characters until end of argument is found. */
7646 for (arg
= next
= cp
= *sp
; *cp
!= '\0'; ++cp
) {
7647 if (!double_quoted
&& *cp
== '\'') {
7648 /* Eat single-quote. */
7649 single_quoted
= !single_quoted
;
7650 } else if (!single_quoted
&& *cp
== '"') {
7651 /* Eat double-quote. */
7652 double_quoted
= !double_quoted
;
7653 } else if (!single_quoted
&& *cp
== '\\' && *(cp
+ 1) == '"') {
7654 /* Eat backslash; copy escaped character to arg. */
7656 } else if (!single_quoted
&& !double_quoted
&& *cp
== '\\' &&
7657 (*(cp
+ 1) == '\'' || *(cp
+ 1) == ' ')) {
7658 /* Eat backslash; move escaped character. */
7660 } else if (!single_quoted
&& !double_quoted
&&
7661 (*cp
== ' ' || *cp
== '\t')) {
7662 /* Terminate argument. */
7664 /* Point sp to beginning of next argument. */
7668 /* Move regular character. */
7673 /* Terminate argument if end of string. */
7683 spawn_insert(const char *name
, const char *args
, int flags
)
7685 struct spawn_prog
*sp
;
7686 char *arg
, *cp
, *ptr
;
7688 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s[%s]\n", name
, args
);
7690 if (args
== NULL
|| *args
== '\0')
7693 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
7694 err(1, "spawn_insert: calloc");
7695 if ((sp
->name
= strdup(name
)) == NULL
)
7696 err(1, "spawn_insert: strdup");
7698 /* Convert the arguments to an argument list. */
7699 if ((ptr
= cp
= strdup(args
)) == NULL
)
7700 err(1, "spawn_insert: strdup");
7701 while ((arg
= argsep(&ptr
)) != NULL
) {
7702 /* Null argument; skip it. */
7707 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
7708 sizeof *sp
->argv
)) == NULL
)
7709 err(1, "spawn_insert: realloc");
7710 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
7711 err(1, "spawn_insert: strdup");
7717 DNPRINTF(SWM_D_SPAWN
, "arg %d: [%s]\n", sp
->argc
, sp
->argv
[sp
->argc
-1]);
7718 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
7719 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
7723 spawn_remove(struct spawn_prog
*sp
)
7727 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
7729 TAILQ_REMOVE(&spawns
, sp
, entry
);
7730 for (i
= 0; i
< sp
->argc
; i
++)
7736 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
7742 struct spawn_prog
*sp
;
7744 while ((sp
= TAILQ_FIRST(&spawns
)) != NULL
) {
7750 spawn_find(const char *name
)
7752 struct spawn_prog
*sp
;
7754 TAILQ_FOREACH(sp
, &spawns
, entry
)
7755 if (strcasecmp(sp
->name
, name
) == 0)
7762 setspawn(const char *name
, const char *args
, int flags
)
7764 struct spawn_prog
*sp
;
7766 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
7771 /* Remove any old spawn under the same name. */
7772 if ((sp
= spawn_find(name
)) != NULL
)
7776 spawn_insert(name
, args
, flags
);
7778 warnx("error: setspawn: cannot find program: %s", name
);
7780 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
7784 setconfspawn(const char *selector
, const char *value
, int flags
)
7788 if (selector
== NULL
|| strlen(selector
) == 0)
7791 args
= expand_tilde(value
);
7793 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
7795 setspawn(selector
, args
, flags
);
7798 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
7803 validate_spawns(void)
7806 struct spawn_prog
*sp
;
7807 char which
[PATH_MAX
];
7810 RB_FOREACH(bp
, binding_tree
, &bindings
) {
7811 if (bp
->action
!= FN_SPAWN_CUSTOM
)
7815 sp
= spawn_find(bp
->spawn_name
);
7816 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
7819 /* verify we have the goods */
7820 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
7821 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
7823 for (i
= strlen("which "); i
< strlen(which
); i
++)
7824 if (which
[i
] == ' ') {
7828 if (system(which
) != 0)
7829 add_startup_exception("could not find %s",
7830 &which
[strlen("which ")]);
7837 setconfspawn("lock", "xlock", 0);
7839 setconfspawn("term", "xterm", 0);
7840 setconfspawn("spawn_term", "xterm", 0);
7842 setconfspawn("menu", "dmenu_run"
7846 " -nf $bar_font_color"
7848 " -sf $bar_color", 0);
7850 setconfspawn("search", "dmenu"
7855 " -nf $bar_font_color"
7857 " -sf $bar_color", 0);
7859 setconfspawn("name_workspace", "dmenu"
7864 " -nf $bar_font_color"
7866 " -sf $bar_color", 0);
7868 /* These are not verified for existence, even with a binding set. */
7869 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
7870 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
7871 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
7875 #define SWM_MODNAME_SIZE 32
7876 #define SWM_KEY_WS "\n+ \t"
7878 parsebinding(const char *bindstr
, uint16_t *mod
, enum binding_type
*type
,
7879 uint32_t *val
, uint32_t *flags
)
7881 char *str
, *cp
, *name
;
7882 KeySym ks
, lks
, uks
;
7884 DNPRINTF(SWM_D_KEY
, "parsebinding: enter [%s]\n", bindstr
);
7885 if (mod
== NULL
|| val
== NULL
) {
7886 DNPRINTF(SWM_D_KEY
, "parsebinding: no mod or key vars\n");
7889 if (bindstr
== NULL
|| strlen(bindstr
) == 0) {
7890 DNPRINTF(SWM_D_KEY
, "parsebinding: no bindstr\n");
7894 if ((cp
= str
= strdup(bindstr
)) == NULL
)
7895 err(1, "parsebinding: strdup");
7897 *val
= XCB_NO_SYMBOL
;
7901 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
7902 DNPRINTF(SWM_D_KEY
, "parsebinding: entry [%s]\n", name
);
7904 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
7905 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
7907 else if (strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
) == 0)
7908 *mod
|= XCB_MOD_MASK_1
;
7909 else if (strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
) == 0)
7910 *mod
|= XCB_MOD_MASK_2
;
7911 else if (strncmp(name
, "Mod3", SWM_MODNAME_SIZE
) == 0)
7912 *mod
|= XCB_MOD_MASK_3
;
7913 else if (strncmp(name
, "Mod4", SWM_MODNAME_SIZE
) == 0)
7914 *mod
|= XCB_MOD_MASK_4
;
7915 else if (strncmp(name
, "Mod5", SWM_MODNAME_SIZE
) == 0)
7916 *mod
|= XCB_MOD_MASK_5
;
7917 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
7918 *mod
|= XCB_MOD_MASK_SHIFT
;
7919 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
7920 *mod
|= XCB_MOD_MASK_CONTROL
;
7921 else if (strncasecmp(name
, "ANYMOD", SWM_MODNAME_SIZE
) == 0)
7922 *mod
|= XCB_MOD_MASK_ANY
;
7923 else if (strncasecmp(name
, "REPLAY", SWM_MODNAME_SIZE
) == 0)
7924 *flags
|= BINDING_F_REPLAY
;
7925 else if (sscanf(name
, "Button%u", val
) == 1) {
7926 DNPRINTF(SWM_D_KEY
, "parsebinding: button %u\n", *val
);
7928 if (*val
> 255 || *val
== 0) {
7930 "parsebinding: invalid button %u\n", *val
);
7934 /* TODO: do this without Xlib. */
7935 ks
= XStringToKeysym(name
);
7936 if (ks
== NoSymbol
) {
7938 "parsebinding: invalid key %s\n", name
);
7943 XConvertCase(ks
, &lks
, &uks
);
7944 *val
= (uint32_t)lks
;
7948 /* If ANYMOD was specified, ignore the rest. */
7949 if (*mod
& XCB_MOD_MASK_ANY
)
7950 *mod
= XCB_MOD_MASK_ANY
;
7953 DNPRINTF(SWM_D_KEY
, "parsebinding: leave ok\n");
7958 strdupsafe(const char *str
)
7963 return (strdup(str
));
7967 binding_cmp(struct binding
*bp1
, struct binding
*bp2
)
7969 if (bp1
->type
< bp2
->type
)
7971 if (bp1
->type
> bp2
->type
)
7974 if (bp1
->value
< bp2
->value
)
7976 if (bp1
->value
> bp2
->value
)
7979 if (bp1
->mod
< bp2
->mod
)
7981 if (bp1
->mod
> bp2
->mod
)
7988 binding_insert(uint16_t mod
, enum binding_type type
, uint32_t val
,
7989 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
7993 DNPRINTF(SWM_D_KEY
, "binding_insert: mod: %u, type: %d, val: %u, "
7994 "action: %s(%d), spawn_name: %s\n", mod
, type
, val
,
7995 actions
[aid
].name
, aid
, spawn_name
);
7997 if ((bp
= malloc(sizeof *bp
)) == NULL
)
7998 err(1, "binding_insert: malloc");
8005 bp
->spawn_name
= strdupsafe(spawn_name
);
8006 RB_INSERT(binding_tree
, &bindings
, bp
);
8008 DNPRINTF(SWM_D_KEY
, "binding_insert: leave\n");
8012 binding_lookup(uint16_t mod
, enum binding_type type
, uint32_t val
)
8020 return (RB_FIND(binding_tree
, &bindings
, &bp
));
8024 binding_remove(struct binding
*bp
)
8026 DNPRINTF(SWM_D_KEY
, "binding_remove: mod: %u, type: %d, val: %u, "
8027 "action: %s(%d), spawn_name: %s\n", bp
->mod
, bp
->type
, bp
->value
,
8028 actions
[bp
->action
].name
, bp
->action
, bp
->spawn_name
);
8030 RB_REMOVE(binding_tree
, &bindings
, bp
);
8031 free(bp
->spawn_name
);
8034 DNPRINTF(SWM_D_KEY
, "binding_remove: leave\n");
8038 setbinding(uint16_t mod
, enum binding_type type
, uint32_t val
,
8039 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
8043 DNPRINTF(SWM_D_KEY
, "setbinding: enter %s [%s]\n",
8044 actions
[aid
].name
, spawn_name
);
8046 /* Unbind any existing. Loop is to handle MOD_MASK_ANY. */
8047 while ((bp
= binding_lookup(mod
, type
, val
)))
8050 if (aid
!= FN_INVALID
)
8051 binding_insert(mod
, type
, val
, aid
, flags
, spawn_name
);
8053 DNPRINTF(SWM_D_KEY
, "setbinding: leave\n");
8057 setconfbinding(const char *selector
, const char *value
, int flags
)
8059 struct spawn_prog
*sp
;
8060 uint32_t keybtn
, opts
;
8063 enum binding_type type
;
8065 /* suppress unused warning since var is needed */
8068 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter selector: [%s], "
8069 "value: [%s]\n", selector
, value
);
8070 if (selector
== NULL
|| strlen(selector
) == 0) {
8071 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
8072 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8073 setbinding(mod
, type
, keybtn
, FN_INVALID
, opts
, NULL
);
8078 /* search by key function name */
8079 for (aid
= 0; aid
< FN_INVALID
; aid
++) {
8080 if (strncasecmp(selector
, actions
[aid
].name
,
8081 SWM_FUNCNAME_LEN
) == 0) {
8082 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8083 "action\n", selector
);
8084 if (parsebinding(value
, &mod
, &type
, &keybtn
,
8086 setbinding(mod
, type
, keybtn
, aid
, opts
, NULL
);
8092 /* search by custom spawn name */
8093 if ((sp
= spawn_find(selector
)) != NULL
) {
8094 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8095 "spawn\n", selector
);
8096 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8097 setbinding(mod
, type
, keybtn
, FN_SPAWN_CUSTOM
, opts
,
8103 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
8107 #define MODSHIFT MODKEY | XCB_MOD_MASK_SHIFT
8109 setup_keybindings(void)
8111 #define BINDKEY(m, k, a) setbinding(m, KEYBIND, k, a, 0, NULL)
8112 #define BINDKEYSPAWN(m, k, s) setbinding(m, KEYBIND, k, FN_SPAWN_CUSTOM, 0, s)
8113 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8114 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8115 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8116 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8117 BINDKEY(MODKEY
, XK_v
, FN_BUTTON2
);
8118 BINDKEY(MODKEY
, XK_space
, FN_CYCLE_LAYOUT
);
8119 BINDKEY(MODSHIFT
, XK_backslash
, FN_FLIP_LAYOUT
);
8120 BINDKEY(MODKEY
, XK_t
, FN_FLOAT_TOGGLE
);
8121 BINDKEY(MODKEY
, XK_m
, FN_FOCUS_MAIN
);
8122 BINDKEY(MODKEY
, XK_j
, FN_FOCUS_NEXT
);
8123 BINDKEY(MODKEY
, XK_Tab
, FN_FOCUS_NEXT
);
8124 BINDKEY(MODKEY
, XK_k
, FN_FOCUS_PREV
);
8125 BINDKEY(MODSHIFT
, XK_Tab
, FN_FOCUS_PREV
);
8126 BINDKEY(MODKEY
, XK_u
, FN_FOCUS_URGENT
);
8127 BINDKEY(MODKEY
, XK_e
, FN_MAXIMIZE_TOGGLE
);
8128 BINDKEY(MODSHIFT
, XK_equal
, FN_HEIGHT_GROW
);
8129 BINDKEY(MODSHIFT
, XK_minus
, FN_HEIGHT_SHRINK
);
8130 BINDKEY(MODKEY
, XK_w
, FN_ICONIFY
);
8131 BINDKEY(MODKEY
, XK_h
, FN_MASTER_SHRINK
);
8132 BINDKEY(MODKEY
, XK_l
, FN_MASTER_GROW
);
8133 BINDKEY(MODKEY
, XK_comma
, FN_MASTER_ADD
);
8134 BINDKEY(MODKEY
, XK_period
, FN_MASTER_DEL
);
8135 BINDKEY(MODSHIFT
, XK_bracketright
, FN_MOVE_DOWN
);
8136 BINDKEY(MODKEY
, XK_bracketleft
, FN_MOVE_LEFT
);
8137 BINDKEY(MODKEY
, XK_bracketright
, FN_MOVE_RIGHT
);
8138 BINDKEY(MODSHIFT
, XK_bracketleft
, FN_MOVE_UP
);
8139 BINDKEY(MODSHIFT
, XK_KP_End
, FN_MVRG_1
);
8140 BINDKEY(MODSHIFT
, XK_KP_Down
, FN_MVRG_2
);
8141 BINDKEY(MODSHIFT
, XK_KP_Next
, FN_MVRG_3
);
8142 BINDKEY(MODSHIFT
, XK_KP_Left
, FN_MVRG_4
);
8143 BINDKEY(MODSHIFT
, XK_KP_Begin
, FN_MVRG_5
);
8144 BINDKEY(MODSHIFT
, XK_KP_Right
, FN_MVRG_6
);
8145 BINDKEY(MODSHIFT
, XK_KP_Home
, FN_MVRG_7
);
8146 BINDKEY(MODSHIFT
, XK_KP_Up
, FN_MVRG_8
);
8147 BINDKEY(MODSHIFT
, XK_KP_Prior
, FN_MVRG_9
);
8148 BINDKEY(MODSHIFT
, XK_1
, FN_MVWS_1
);
8149 BINDKEY(MODSHIFT
, XK_2
, FN_MVWS_2
);
8150 BINDKEY(MODSHIFT
, XK_3
, FN_MVWS_3
);
8151 BINDKEY(MODSHIFT
, XK_4
, FN_MVWS_4
);
8152 BINDKEY(MODSHIFT
, XK_5
, FN_MVWS_5
);
8153 BINDKEY(MODSHIFT
, XK_6
, FN_MVWS_6
);
8154 BINDKEY(MODSHIFT
, XK_7
, FN_MVWS_7
);
8155 BINDKEY(MODSHIFT
, XK_8
, FN_MVWS_8
);
8156 BINDKEY(MODSHIFT
, XK_9
, FN_MVWS_9
);
8157 BINDKEY(MODSHIFT
, XK_0
, FN_MVWS_10
);
8158 BINDKEY(MODSHIFT
, XK_F1
, FN_MVWS_11
);
8159 BINDKEY(MODSHIFT
, XK_F2
, FN_MVWS_12
);
8160 BINDKEY(MODSHIFT
, XK_F3
, FN_MVWS_13
);
8161 BINDKEY(MODSHIFT
, XK_F4
, FN_MVWS_14
);
8162 BINDKEY(MODSHIFT
, XK_F5
, FN_MVWS_15
);
8163 BINDKEY(MODSHIFT
, XK_F6
, FN_MVWS_16
);
8164 BINDKEY(MODSHIFT
, XK_F7
, FN_MVWS_17
);
8165 BINDKEY(MODSHIFT
, XK_F8
, FN_MVWS_18
);
8166 BINDKEY(MODSHIFT
, XK_F9
, FN_MVWS_19
);
8167 BINDKEY(MODSHIFT
, XK_F10
, FN_MVWS_20
);
8168 BINDKEY(MODSHIFT
, XK_F11
, FN_MVWS_21
);
8169 BINDKEY(MODSHIFT
, XK_F12
, FN_MVWS_22
);
8170 BINDKEY(MODSHIFT
, XK_slash
, FN_NAME_WORKSPACE
);
8171 BINDKEY(MODSHIFT
, XK_q
, FN_QUIT
);
8172 BINDKEY(MODSHIFT
, XK_r
, FN_RAISE_TOGGLE
);
8173 BINDKEY(MODKEY
, XK_q
, FN_RESTART
);
8174 BINDKEY(MODKEY
, XK_KP_End
, FN_RG_1
);
8175 BINDKEY(MODKEY
, XK_KP_Down
, FN_RG_2
);
8176 BINDKEY(MODKEY
, XK_KP_Next
, FN_RG_3
);
8177 BINDKEY(MODKEY
, XK_KP_Left
, FN_RG_4
);
8178 BINDKEY(MODKEY
, XK_KP_Begin
, FN_RG_5
);
8179 BINDKEY(MODKEY
, XK_KP_Right
, FN_RG_6
);
8180 BINDKEY(MODKEY
, XK_KP_Home
, FN_RG_7
);
8181 BINDKEY(MODKEY
, XK_KP_Up
, FN_RG_8
);
8182 BINDKEY(MODKEY
, XK_KP_Prior
, FN_RG_9
);
8183 BINDKEY(MODSHIFT
, XK_Right
, FN_RG_NEXT
);
8184 BINDKEY(MODSHIFT
, XK_Left
, FN_RG_PREV
);
8185 BINDKEY(MODKEY
, XK_f
, FN_SEARCH_WIN
);
8186 BINDKEY(MODKEY
, XK_slash
, FN_SEARCH_WORKSPACE
);
8187 BINDKEYSPAWN(MODSHIFT
, XK_i
, "initscr");
8188 BINDKEYSPAWN(MODSHIFT
, XK_Delete
, "lock");
8189 BINDKEYSPAWN(MODKEY
, XK_p
, "menu");
8190 BINDKEYSPAWN(MODKEY
, XK_s
, "screenshot_all");
8191 BINDKEYSPAWN(MODSHIFT
, XK_s
, "screenshot_wind");
8192 BINDKEYSPAWN(MODSHIFT
, XK_Return
, "term");
8193 BINDKEY(MODSHIFT
, XK_comma
, FN_STACK_INC
);
8194 BINDKEY(MODSHIFT
, XK_period
, FN_STACK_DEC
);
8195 BINDKEY(MODSHIFT
, XK_space
, FN_STACK_RESET
);
8196 BINDKEY(MODKEY
, XK_Return
, FN_SWAP_MAIN
);
8197 BINDKEY(MODSHIFT
, XK_j
, FN_SWAP_NEXT
);
8198 BINDKEY(MODSHIFT
, XK_k
, FN_SWAP_PREV
);
8199 BINDKEY(MODSHIFT
, XK_w
, FN_UNICONIFY
);
8200 BINDKEY(MODSHIFT
, XK_v
, FN_VERSION
);
8201 BINDKEY(MODKEY
, XK_equal
, FN_WIDTH_GROW
);
8202 BINDKEY(MODKEY
, XK_minus
, FN_WIDTH_SHRINK
);
8203 BINDKEY(MODKEY
, XK_x
, FN_WIND_DEL
);
8204 BINDKEY(MODSHIFT
, XK_x
, FN_WIND_KILL
);
8205 BINDKEY(MODKEY
, XK_1
, FN_WS_1
);
8206 BINDKEY(MODKEY
, XK_2
, FN_WS_2
);
8207 BINDKEY(MODKEY
, XK_3
, FN_WS_3
);
8208 BINDKEY(MODKEY
, XK_4
, FN_WS_4
);
8209 BINDKEY(MODKEY
, XK_5
, FN_WS_5
);
8210 BINDKEY(MODKEY
, XK_6
, FN_WS_6
);
8211 BINDKEY(MODKEY
, XK_7
, FN_WS_7
);
8212 BINDKEY(MODKEY
, XK_8
, FN_WS_8
);
8213 BINDKEY(MODKEY
, XK_9
, FN_WS_9
);
8214 BINDKEY(MODKEY
, XK_0
, FN_WS_10
);
8215 BINDKEY(MODKEY
, XK_F1
, FN_WS_11
);
8216 BINDKEY(MODKEY
, XK_F2
, FN_WS_12
);
8217 BINDKEY(MODKEY
, XK_F3
, FN_WS_13
);
8218 BINDKEY(MODKEY
, XK_F4
, FN_WS_14
);
8219 BINDKEY(MODKEY
, XK_F5
, FN_WS_15
);
8220 BINDKEY(MODKEY
, XK_F6
, FN_WS_16
);
8221 BINDKEY(MODKEY
, XK_F7
, FN_WS_17
);
8222 BINDKEY(MODKEY
, XK_F8
, FN_WS_18
);
8223 BINDKEY(MODKEY
, XK_F9
, FN_WS_19
);
8224 BINDKEY(MODKEY
, XK_F10
, FN_WS_20
);
8225 BINDKEY(MODKEY
, XK_F11
, FN_WS_21
);
8226 BINDKEY(MODKEY
, XK_F12
, FN_WS_22
);
8227 BINDKEY(MODKEY
, XK_Right
, FN_WS_NEXT
);
8228 BINDKEY(MODKEY
, XK_Left
, FN_WS_PREV
);
8229 BINDKEY(MODKEY
, XK_Up
, FN_WS_NEXT_ALL
);
8230 BINDKEY(MODKEY
, XK_Down
, FN_WS_PREV_ALL
);
8231 BINDKEY(MODSHIFT
, XK_Up
, FN_WS_NEXT_MOVE
);
8232 BINDKEY(MODSHIFT
, XK_Down
, FN_WS_PREV_MOVE
);
8233 BINDKEY(MODKEY
, XK_a
, FN_WS_PRIOR
);
8235 BINDKEY(MODKEY
, XK_d
, FN_DEBUG_TOGGLE
);
8236 BINDKEY(MODSHIFT
, XK_d
, FN_DUMPWINS
);
8243 setup_btnbindings(void)
8245 setbinding(ANYMOD
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_FOCUS
,
8246 BINDING_F_REPLAY
, NULL
);
8247 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE
, 0, NULL
);
8248 setbinding(MODSHIFT
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE_CENTERED
, 0,
8250 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_MOVE
, 0, NULL
);
8255 clear_bindings(void)
8259 while ((bp
= RB_ROOT(&bindings
)))
8264 clear_keybindings(void)
8266 struct binding
*bp
, *bptmp
;
8268 RB_FOREACH_SAFE(bp
, binding_tree
, &bindings
, bptmp
) {
8269 if (bp
->type
!= KEYBIND
)
8276 setkeymapping(const char *selector
, const char *value
, int flags
)
8278 char *keymapping_file
;
8280 /* suppress unused warnings since vars are needed */
8284 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
8286 keymapping_file
= expand_tilde(value
);
8288 clear_keybindings();
8289 /* load new key bindings; if it fails, revert to default bindings */
8290 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
8291 clear_keybindings();
8292 setup_keybindings();
8295 free(keymapping_file
);
8297 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
8302 updatenumlockmask(void)
8305 xcb_get_modifier_mapping_reply_t
*modmap_r
;
8306 xcb_keycode_t
*modmap
, kc
, *keycode
;
8310 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
8311 xcb_get_modifier_mapping(conn
),
8314 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
8315 for (i
= 0; i
< 8; i
++) {
8316 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
8317 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
8319 keycode
= xcb_key_symbols_get_keycode(syms
,
8323 numlockmask
= (1 << i
);
8330 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
8337 int num_screens
, k
, j
;
8338 uint16_t modifiers
[4];
8339 xcb_keycode_t
*code
;
8341 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
8342 updatenumlockmask();
8345 modifiers
[1] = numlockmask
;
8346 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8347 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8349 num_screens
= get_screen_count();
8350 for (k
= 0; k
< num_screens
; k
++) {
8351 if (TAILQ_EMPTY(&screens
[k
].rl
))
8353 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
8355 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8356 if (bp
->type
!= KEYBIND
)
8359 /* If there is a catch-all, only bind that. */
8360 if ((binding_lookup(ANYMOD
, KEYBIND
, bp
->value
)) &&
8364 /* Skip unused ws binds. */
8365 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8366 bp
->action
<= FN_WS_22
)
8369 /* Skip unused mvws binds. */
8370 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8371 bp
->action
<= FN_MVWS_22
)
8374 if ((code
= xcb_key_symbols_get_keycode(syms
,
8375 bp
->value
)) == NULL
)
8378 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8379 /* All modifiers are grabbed in one pass. */
8380 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8381 "key: %u, modifiers: %d\n", bp
->value
,
8383 xcb_grab_key(conn
, 1, screens
[k
].root
,
8384 bp
->mod
, *code
, XCB_GRAB_MODE_ASYNC
,
8385 XCB_GRAB_MODE_SYNC
);
8387 /* Need to grab each modifier permutation. */
8388 for (j
= 0; j
< LENGTH(modifiers
); j
++) {
8389 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8390 "key: %u, modifiers: %d\n",
8391 bp
->value
, bp
->mod
| modifiers
[j
]);
8392 xcb_grab_key(conn
, 1,
8394 bp
->mod
| modifiers
[j
],
8395 *code
, XCB_GRAB_MODE_ASYNC
,
8396 XCB_GRAB_MODE_SYNC
);
8408 int num_screens
, i
, k
;
8409 uint16_t modifiers
[4];
8411 DNPRINTF(SWM_D_MOUSE
, "grabbuttons\n");
8412 updatenumlockmask();
8415 modifiers
[1] = numlockmask
;
8416 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8417 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8419 num_screens
= get_screen_count();
8420 for (k
= 0; k
< num_screens
; k
++) {
8421 if (TAILQ_EMPTY(&screens
[k
].rl
))
8423 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, screens
[k
].root
,
8425 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8426 if (bp
->type
!= BTNBIND
)
8429 /* If there is a catch-all, only bind that. */
8430 if ((binding_lookup(ANYMOD
, BTNBIND
, bp
->value
)) &&
8434 /* Skip unused ws binds. */
8435 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8436 bp
->action
<= FN_WS_22
)
8439 /* Skip unused mvws binds. */
8440 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8441 bp
->action
<= FN_MVWS_22
)
8444 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8445 /* All modifiers are grabbed in one pass. */
8446 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: grab, "
8447 "button: %u, modifiers: %d\n", bp
->value
,
8449 xcb_grab_button(conn
, 0, screens
[k
].root
,
8450 BUTTONMASK
, XCB_GRAB_MODE_SYNC
,
8451 XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
8452 XCB_CURSOR_NONE
, bp
->value
, bp
->mod
);
8454 /* Need to grab each modifier permutation. */
8455 for (i
= 0; i
< LENGTH(modifiers
); ++i
) {
8456 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: "
8457 "grab, button: %u, modifiers: %u\n",
8458 bp
->value
, bp
->mod
| modifiers
[i
]);
8459 xcb_grab_button(conn
, 0,
8460 screens
[k
].root
, BUTTONMASK
,
8462 XCB_GRAB_MODE_ASYNC
,
8464 XCB_CURSOR_NONE
, bp
->value
,
8465 bp
->mod
| modifiers
[i
]);
8472 const char *quirkname
[] = {
8473 "NONE", /* config string for "no value" */
8481 "FOCUSONMAP_SINGLE",
8489 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
8490 #define SWM_Q_DELIM "\n|+ \t"
8492 parsequirks(const char *qstr
, uint32_t *quirk
, int *ws
)
8494 char *str
, *cp
, *name
;
8497 if (quirk
== NULL
|| qstr
== NULL
)
8500 if ((cp
= str
= strdup(qstr
)) == NULL
)
8501 err(1, "parsequirks: strdup");
8504 while ((name
= strsep(&cp
, SWM_Q_DELIM
)) != NULL
) {
8506 cp
+= (long)strspn(cp
, SWM_Q_DELIM
);
8508 if (sscanf(name
, "WS[%d]", ws
) == 1) {
8514 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
8515 if (strncasecmp(name
, quirkname
[i
],
8516 SWM_QUIRK_LEN
) == 0) {
8517 DNPRINTF(SWM_D_QUIRK
,
8518 "parsequirks: %s\n", name
);
8524 *quirk
|= 1 << (i
-1);
8528 if (i
>= LENGTH(quirkname
)) {
8529 DNPRINTF(SWM_D_QUIRK
,
8530 "parsequirks: invalid quirk [%s]\n", name
);
8541 quirk_insert(const char *class, const char *instance
, const char *name
,
8542 uint32_t quirk
, int ws
)
8546 bool failed
= false;
8548 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: class: %s, instance: %s, name: %s,"
8549 " value: %u, ws: %d\n", class, instance
, name
, quirk
, ws
);
8551 if ((qp
= malloc(sizeof *qp
)) == NULL
)
8552 err(1, "quirk_insert: malloc");
8554 if ((qp
->class = strdup(class)) == NULL
)
8555 err(1, "quirk_insert: strdup");
8556 if ((qp
->instance
= strdup(instance
)) == NULL
)
8557 err(1, "quirk_insert: strdup");
8558 if ((qp
->name
= strdup(name
)) == NULL
)
8559 err(1, "quirk_insert: strdup");
8561 if (asprintf(&str
, "^%s$", class) == -1)
8562 err(1, "quirk_insert: asprintf");
8563 if (regcomp(&qp
->regex_class
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8564 add_startup_exception("regex failed to compile quirk 'class' "
8565 "field: %s", class);
8568 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8571 if (asprintf(&str
, "^%s$", instance
) == -1)
8572 err(1, "quirk_insert: asprintf");
8573 if (regcomp(&qp
->regex_instance
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8574 add_startup_exception("regex failed to compile quirk 'instance'"
8575 " field: %s", instance
);
8578 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8581 if (asprintf(&str
, "^%s$", name
) == -1)
8582 err(1, "quirk_insert: asprintf");
8583 if (regcomp(&qp
->regex_name
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8584 add_startup_exception("regex failed to compile quirk 'name' "
8588 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8592 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: regex error; skipping.\n");
8597 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
8599 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
8603 quirk_remove(struct quirk
*qp
)
8605 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%u]\n", qp
->class,
8606 qp
->name
, qp
->quirk
);
8608 TAILQ_REMOVE(&quirks
, qp
, entry
);
8611 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
8615 quirk_free(struct quirk
*qp
)
8617 regfree(&qp
->regex_class
);
8618 regfree(&qp
->regex_instance
);
8619 regfree(&qp
->regex_name
);
8631 while ((qp
= TAILQ_FIRST(&quirks
)) != NULL
) {
8637 quirk_replace(struct quirk
*qp
, const char *class, const char *instance
,
8638 const char *name
, uint32_t quirk
, int ws
)
8640 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp
->class,
8641 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
8644 quirk_insert(class, instance
, name
, quirk
, ws
);
8646 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
8650 setquirk(const char *class, const char *instance
, const char *name
,
8651 uint32_t quirk
, int ws
)
8655 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
8656 instance
, name
, quirk
, ws
);
8658 /* Remove/replace existing quirk. */
8659 TAILQ_FOREACH(qp
, &quirks
, entry
) {
8660 if (strcmp(qp
->class, class) == 0 &&
8661 strcmp(qp
->instance
, instance
) == 0 &&
8662 strcmp(qp
->name
, name
) == 0) {
8663 if (quirk
== 0 && ws
== -1)
8666 quirk_replace(qp
, class, instance
, name
, quirk
,
8668 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8673 /* Only insert if quirk is not NONE or forced ws is set. */
8674 if (quirk
|| ws
!= -1)
8675 quirk_insert(class, instance
, name
, quirk
, ws
);
8677 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8680 /* Eat '\' in str used to escape square brackets and colon. */
8682 unescape_selector(char *str
)
8686 for (cp
= str
; *str
!= '\0'; ++str
, ++cp
) {
8687 if (*str
== '\\' && (*(str
+ 1) == ':' || *(str
+ 1) == ']' ||
8697 setconfquirk(const char *selector
, const char *value
, int flags
)
8699 char *str
, *cp
, *class;
8700 char *instance
= NULL
, *name
= NULL
;
8701 int retval
, count
= 0, ws
= -1;
8704 /* suppress unused warning since var is needed */
8707 if (selector
== NULL
|| strlen(selector
) == 0)
8710 if ((str
= strdup(selector
)) == NULL
)
8711 err(1, "setconfquirk: strdup");
8713 /* Find non-escaped colon. */
8720 for (++cp
; *cp
!= '\0'; ++cp
) {
8721 if (*cp
== ':' && *(cp
- 1) != '\\') {
8727 unescape_selector(class);
8729 instance
= class + strlen(class) + 1;
8730 unescape_selector(instance
);
8736 name
= instance
+ strlen(instance
) + 1;
8737 unescape_selector(name
);
8742 DNPRINTF(SWM_D_CONF
, "setconfquirk: class: %s, instance: %s, "
8743 "name: %s\n", class, instance
, name
);
8745 if ((retval
= parsequirks(value
, &qrks
, &ws
)) == 0)
8746 setquirk(class, instance
, name
, qrks
, ws
);
8755 setquirk("MPlayer", "xv", ".*",
8756 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
, -1);
8757 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
8759 setquirk("Firefox-bin", "firefox-bin", ".*",
8761 setquirk("Firefox", "Dialog", ".*",
8763 setquirk("Gimp", "gimp", ".*",
8764 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8765 setquirk("XTerm", "xterm", ".*",
8766 SWM_Q_XTERM_FONTADJ
, -1);
8767 setquirk("xine", "Xine Window", ".*",
8768 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8769 setquirk("Xitk", "Xitk Combo", ".*",
8770 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8771 setquirk("xine", "xine Panel", ".*",
8772 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8773 setquirk("Xitk", "Xine Window", ".*",
8774 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8775 setquirk("xine", "xine Video Fullscreen Window", ".*",
8776 SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
, -1);
8777 setquirk("pcb", "pcb", ".*",
8779 setquirk("SDL_App", "SDL_App", ".*",
8780 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
, -1);
8783 /* conf file stuff */
8784 #define SWM_CONF_FILE "spectrwm.conf"
8785 #define SWM_CONF_FILE_OLD "scrotwm.conf"
8789 SWM_S_BAR_AT_BOTTOM
,
8790 SWM_S_BAR_BORDER_WIDTH
,
8793 SWM_S_BAR_ENABLED_WS
,
8798 SWM_S_BOUNDARY_WIDTH
,
8799 SWM_S_CLOCK_ENABLED
,
8802 SWM_S_CYCLE_VISIBLE
,
8804 SWM_S_DISABLE_BORDER
,
8806 SWM_S_FOCUS_CLOSE_WRAP
,
8807 SWM_S_FOCUS_DEFAULT
,
8809 SWM_S_ICONIC_ENABLED
,
8810 SWM_S_JAVA_WORKAROUND
,
8811 SWM_S_MAXIMIZE_HIDE_BAR
,
8812 SWM_S_REGION_PADDING
,
8817 SWM_S_STACK_ENABLED
,
8820 SWM_S_URGENT_COLLAPSE
,
8821 SWM_S_URGENT_ENABLED
,
8822 SWM_S_VERBOSE_LAYOUT
,
8825 SWM_S_WINDOW_CLASS_ENABLED
,
8826 SWM_S_WINDOW_INSTANCE_ENABLED
,
8827 SWM_S_WINDOW_NAME_ENABLED
,
8828 SWM_S_WORKSPACE_CLAMP
,
8829 SWM_S_WORKSPACE_LIMIT
,
8830 SWM_S_WORKSPACE_NAME
,
8834 setconfvalue(const char *selector
, const char *value
, int flags
)
8836 struct workspace
*ws
;
8837 int i
, ws_id
, num_screens
;
8838 char *b
, *str
, *sp
, s
[1024];
8841 case SWM_S_BAR_ACTION
:
8843 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
8844 err(1, "setconfvalue: bar_action");
8846 case SWM_S_BAR_AT_BOTTOM
:
8847 bar_at_bottom
= (atoi(value
) != 0);
8849 case SWM_S_BAR_BORDER_WIDTH
:
8850 bar_border_width
= atoi(value
);
8851 if (bar_border_width
< 0)
8852 bar_border_width
= 0;
8854 case SWM_S_BAR_DELAY
:
8855 /* No longer needed; leave to not break old conf files. */
8857 case SWM_S_BAR_ENABLED
:
8858 bar_enabled
= (atoi(value
) != 0);
8860 case SWM_S_BAR_ENABLED_WS
:
8861 ws_id
= atoi(selector
) - 1;
8862 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8863 errx(1, "setconfvalue: bar_enabled_ws: invalid "
8864 "workspace %d.", ws_id
+ 1);
8866 num_screens
= get_screen_count();
8867 for (i
= 0; i
< num_screens
; i
++) {
8868 ws
= (struct workspace
*)&screens
[i
].ws
;
8869 ws
[ws_id
].bar_enabled
= (atoi(value
) != 0);
8872 case SWM_S_BAR_FONT
:
8874 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
8875 err(1, "setconfvalue: asprintf: failed to allocate "
8876 "memory for bar_fonts.");
8879 /* If already in xft mode, then we are done. */
8880 if (!bar_font_legacy
)
8883 if ((sp
= str
= strdup(value
)) == NULL
)
8884 err(1, "setconfvalue: strdup");
8886 /* If there are any non-XLFD entries, switch to Xft mode. */
8887 while ((b
= strsep(&sp
, ",")) != NULL
) {
8891 bar_font_legacy
= false;
8898 case SWM_S_BAR_FORMAT
:
8900 if ((bar_format
= strdup(value
)) == NULL
)
8901 err(1, "setconfvalue: bar_format");
8903 case SWM_S_BAR_JUSTIFY
:
8904 if (strcmp(value
, "left") == 0)
8905 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
8906 else if (strcmp(value
, "center") == 0)
8907 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
8908 else if (strcmp(value
, "right") == 0)
8909 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
8911 errx(1, "invalid bar_justify");
8913 case SWM_S_BORDER_WIDTH
:
8914 border_width
= atoi(value
);
8915 if (border_width
< 0)
8918 case SWM_S_BOUNDARY_WIDTH
:
8919 boundary_width
= atoi(value
);
8920 if (boundary_width
< 0)
8923 case SWM_S_CLOCK_ENABLED
:
8924 clock_enabled
= (atoi(value
) != 0);
8926 case SWM_S_CLOCK_FORMAT
:
8927 #ifndef SWM_DENY_CLOCK_FORMAT
8929 if ((clock_format
= strdup(value
)) == NULL
)
8930 err(1, "setconfvalue: clock_format");
8933 case SWM_S_CYCLE_EMPTY
:
8934 cycle_empty
= (atoi(value
) != 0);
8936 case SWM_S_CYCLE_VISIBLE
:
8937 cycle_visible
= (atoi(value
) != 0);
8939 case SWM_S_DIALOG_RATIO
:
8940 dialog_ratio
= atof(value
);
8941 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
8944 case SWM_S_DISABLE_BORDER
:
8945 disable_border
= (atoi(value
) != 0);
8947 case SWM_S_FOCUS_CLOSE
:
8948 if (strcmp(value
, "first") == 0)
8949 focus_close
= SWM_STACK_BOTTOM
;
8950 else if (strcmp(value
, "last") == 0)
8951 focus_close
= SWM_STACK_TOP
;
8952 else if (strcmp(value
, "next") == 0)
8953 focus_close
= SWM_STACK_ABOVE
;
8954 else if (strcmp(value
, "previous") == 0)
8955 focus_close
= SWM_STACK_BELOW
;
8957 errx(1, "focus_close");
8959 case SWM_S_FOCUS_CLOSE_WRAP
:
8960 focus_close_wrap
= (atoi(value
) != 0);
8962 case SWM_S_FOCUS_DEFAULT
:
8963 if (strcmp(value
, "last") == 0)
8964 focus_default
= SWM_STACK_TOP
;
8965 else if (strcmp(value
, "first") == 0)
8966 focus_default
= SWM_STACK_BOTTOM
;
8968 errx(1, "focus_default");
8970 case SWM_S_FOCUS_MODE
:
8971 if (strcmp(value
, "default") == 0)
8972 focus_mode
= SWM_FOCUS_DEFAULT
;
8973 else if (strcmp(value
, "follow") == 0 ||
8974 strcmp(value
, "follow_cursor") == 0)
8975 focus_mode
= SWM_FOCUS_FOLLOW
;
8976 else if (strcmp(value
, "manual") == 0)
8977 focus_mode
= SWM_FOCUS_MANUAL
;
8979 errx(1, "focus_mode");
8981 case SWM_S_ICONIC_ENABLED
:
8982 iconic_enabled
= (atoi(value
) != 0);
8984 case SWM_S_JAVA_WORKAROUND
:
8985 java_workaround
= (atoi(value
) != 0);
8987 case SWM_S_MAXIMIZE_HIDE_BAR
:
8988 maximize_hide_bar
= atoi(value
);
8990 case SWM_S_REGION_PADDING
:
8991 region_padding
= atoi(value
);
8992 if (region_padding
< 0)
8995 case SWM_S_SPAWN_ORDER
:
8996 if (strcmp(value
, "first") == 0)
8997 spawn_position
= SWM_STACK_BOTTOM
;
8998 else if (strcmp(value
, "last") == 0)
8999 spawn_position
= SWM_STACK_TOP
;
9000 else if (strcmp(value
, "next") == 0)
9001 spawn_position
= SWM_STACK_ABOVE
;
9002 else if (strcmp(value
, "previous") == 0)
9003 spawn_position
= SWM_STACK_BELOW
;
9005 errx(1, "spawn_position");
9007 case SWM_S_SPAWN_TERM
:
9008 setconfspawn("term", value
, 0);
9009 setconfspawn("spawn_term", value
, 0);
9012 /* No longer needed; leave to not break old conf files. */
9014 case SWM_S_SS_ENABLED
:
9015 /* No longer needed; leave to not break old conf files. */
9017 case SWM_S_STACK_ENABLED
:
9018 stack_enabled
= (atoi(value
) != 0);
9020 case SWM_S_TERM_WIDTH
:
9021 term_width
= atoi(value
);
9025 case SWM_S_TILE_GAP
:
9026 tile_gap
= atoi(value
);
9028 case SWM_S_URGENT_COLLAPSE
:
9029 urgent_collapse
= (atoi(value
) != 0);
9031 case SWM_S_URGENT_ENABLED
:
9032 urgent_enabled
= (atoi(value
) != 0);
9034 case SWM_S_VERBOSE_LAYOUT
:
9035 verbose_layout
= (atoi(value
) != 0);
9036 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
9038 layouts
[i
].l_string
= fancy_stacker
;
9040 layouts
[i
].l_string
= plain_stacker
;
9043 case SWM_S_WARP_FOCUS
:
9044 warp_focus
= (atoi(value
) != 0);
9046 case SWM_S_WARP_POINTER
:
9047 warp_pointer
= (atoi(value
) != 0);
9049 case SWM_S_WINDOW_CLASS_ENABLED
:
9050 window_class_enabled
= (atoi(value
) != 0);
9052 case SWM_S_WINDOW_INSTANCE_ENABLED
:
9053 window_instance_enabled
= (atoi(value
) != 0);
9055 case SWM_S_WINDOW_NAME_ENABLED
:
9056 window_name_enabled
= (atoi(value
) != 0);
9058 case SWM_S_WORKSPACE_CLAMP
:
9059 workspace_clamp
= (atoi(value
) != 0);
9061 case SWM_S_WORKSPACE_LIMIT
:
9062 workspace_limit
= atoi(value
);
9063 if (workspace_limit
> SWM_WS_MAX
)
9064 workspace_limit
= SWM_WS_MAX
;
9065 else if (workspace_limit
< 1)
9066 workspace_limit
= 1;
9068 ewmh_update_desktops();
9070 case SWM_S_WORKSPACE_NAME
:
9071 if (getenv("SWM_STARTED") != NULL
)
9075 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9076 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
9078 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9079 errx(1, "setconfvalue: workspace_name: invalid "
9080 "workspace %d.", ws_id
+ 1);
9082 num_screens
= get_screen_count();
9083 for (i
= 0; i
< num_screens
; ++i
) {
9084 ws
= (struct workspace
*)&screens
[i
].ws
;
9086 if (strlen(s
) > 0) {
9087 free(ws
[ws_id
].name
);
9088 if ((ws
[ws_id
].name
= strdup(s
)) == NULL
)
9089 err(1, "setconfvalue: workspace_name.");
9091 ewmh_update_desktop_names();
9092 ewmh_get_desktop_names();
9103 setconfmodkey(const char *selector
, const char *value
, int flags
)
9105 /* suppress unused warnings since vars are needed */
9109 if (strncasecmp(value
, "Mod1", strlen("Mod1")) == 0)
9110 update_modkey(XCB_MOD_MASK_1
);
9111 else if (strncasecmp(value
, "Mod2", strlen("Mod2")) == 0)
9112 update_modkey(XCB_MOD_MASK_2
);
9113 else if (strncasecmp(value
, "Mod3", strlen("Mod3")) == 0)
9114 update_modkey(XCB_MOD_MASK_3
);
9115 else if (strncasecmp(value
, "Mod4", strlen("Mod4")) == 0)
9116 update_modkey(XCB_MOD_MASK_4
);
9117 else if (strncasecmp(value
, "Mod5", strlen("Mod5")) == 0)
9118 update_modkey(XCB_MOD_MASK_5
);
9125 setconfcolor(const char *selector
, const char *value
, int flags
)
9127 int first
, last
, i
= 0, num_screens
;
9129 num_screens
= get_screen_count();
9131 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
9132 if (selector
== NULL
|| strlen(selector
) == 0 ||
9133 (last
= atoi(selector
) - 1) < 0) {
9135 last
= num_screens
- 1;
9140 if (last
>= num_screens
) {
9141 add_startup_exception("invalid screen index: %d out of bounds "
9142 "(maximum %d)", last
+ 1, num_screens
);
9146 for (i
= first
; i
<= last
; ++i
) {
9147 setscreencolor(value
, i
, flags
);
9150 * When setting focus/unfocus colors, we need to also
9151 * set maximize colors to match if they haven't been customized.
9153 if (flags
== SWM_S_COLOR_FOCUS
&&
9154 !screens
[i
].c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].manual
)
9155 setscreencolor(value
, i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
9156 else if (flags
== SWM_S_COLOR_UNFOCUS
&&
9157 !screens
[i
].c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].manual
)
9158 setscreencolor(value
, i
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
9160 screens
[i
].c
[flags
].manual
= 1;
9167 setconfregion(const char *selector
, const char *value
, int flags
)
9169 /* suppress unused warnings since vars are needed */
9173 custom_region(value
);
9178 setautorun(const char *selector
, const char *value
, int flags
)
9182 char *ap
, *sp
, *str
;
9188 /* suppress unused warnings since vars are needed */
9192 if (getenv("SWM_STARTED"))
9196 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9197 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
9199 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9200 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
9202 sp
= str
= expand_tilde((char *)&s
);
9205 * This is a little intricate
9207 * If the pid already exists we simply reuse it because it means it was
9208 * used before AND not claimed by manage_window. We get away with
9209 * altering it in the parent after INSERT because this can not be a race
9212 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
9215 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
9217 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
9218 err(1, "setautorun: realloc");
9219 a
.argv
[argc
- 1] = ap
;
9222 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
9223 err(1, "setautorun: realloc");
9224 a
.argv
[argc
] = NULL
;
9226 if ((pid
= fork()) == 0) {
9227 spawn(ws_id
, &a
, true);
9237 p
= calloc(1, sizeof *p
);
9240 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
9250 setlayout(const char *selector
, const char *value
, int flags
)
9252 struct workspace
*ws
;
9253 int ws_id
, i
, x
, mg
, ma
, si
, ar
;
9254 int st
= SWM_V_STACK
, num_screens
;
9258 /* suppress unused warnings since vars are needed */
9262 if (getenv("SWM_STARTED"))
9266 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
9267 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
9268 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9269 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9272 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9273 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
9275 if (strcasecmp(s
, "vertical") == 0)
9277 else if (strcasecmp(s
, "vertical_flip") == 0) {
9280 } else if (strcasecmp(s
, "horizontal") == 0)
9282 else if (strcasecmp(s
, "horizontal_flip") == 0) {
9285 } else if (strcasecmp(s
, "fullscreen") == 0)
9288 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9289 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9292 num_screens
= get_screen_count();
9293 for (i
= 0; i
< num_screens
; i
++) {
9294 ws
= (struct workspace
*)&screens
[i
].ws
;
9295 ws
[ws_id
].cur_layout
= &layouts
[st
];
9297 ws
[ws_id
].always_raise
= (ar
!= 0);
9298 if (st
== SWM_MAX_STACK
)
9302 for (x
= 0; x
< abs(mg
); x
++) {
9303 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9304 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
9305 SWM_ARG_ID_MASTERSHRINK
);
9308 for (x
= 0; x
< abs(ma
); x
++) {
9309 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9310 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
9311 SWM_ARG_ID_MASTERDEL
);
9314 for (x
= 0; x
< abs(si
); x
++) {
9315 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9316 si
>= 0 ? SWM_ARG_ID_STACKINC
:
9317 SWM_ARG_ID_STACKDEC
);
9321 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9322 SWM_ARG_ID_FLIPLAYOUT
);
9329 /* config options */
9330 struct config_option
{
9332 int (*func
)(const char*, const char*, int);
9335 struct config_option configopt
[] = {
9336 { "autorun", setautorun
, 0 },
9337 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
9338 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
9339 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
9340 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
9341 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
9342 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
9343 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
9344 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
9345 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
9346 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
9347 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
9348 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
9349 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
9350 { "bind", setconfbinding
, 0 },
9351 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
9352 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
9353 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
9354 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
9355 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
9356 { "color_focus_maximized", setconfcolor
, SWM_S_COLOR_FOCUS_MAXIMIZED
},
9357 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
9358 { "color_unfocus_maximized", setconfcolor
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
},
9359 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
9360 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
9361 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
9362 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
9363 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
9364 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
9365 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
9366 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
9367 { "iconic_enabled", setconfvalue
, SWM_S_ICONIC_ENABLED
},
9368 { "java_workaround", setconfvalue
, SWM_S_JAVA_WORKAROUND
},
9369 { "keyboard_mapping", setkeymapping
, 0 },
9370 { "layout", setlayout
, 0 },
9371 { "maximize_hide_bar", setconfvalue
, SWM_S_MAXIMIZE_HIDE_BAR
},
9372 { "modkey", setconfmodkey
, 0 },
9373 { "program", setconfspawn
, 0 },
9374 { "quirk", setconfquirk
, 0 },
9375 { "region", setconfregion
, 0 },
9376 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
9377 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
9378 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
9379 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
9380 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
9381 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
9382 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
9383 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
9384 { "title_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
}, /* For backwards compat. */
9385 { "title_name_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
}, /* For backwards compat. */
9386 { "urgent_collapse", setconfvalue
, SWM_S_URGENT_COLLAPSE
},
9387 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
9388 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
9389 { "warp_focus", setconfvalue
, SWM_S_WARP_FOCUS
},
9390 { "warp_pointer", setconfvalue
, SWM_S_WARP_POINTER
},
9391 { "window_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
},
9392 { "window_instance_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
},
9393 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
9394 { "workspace_clamp", setconfvalue
, SWM_S_WORKSPACE_CLAMP
},
9395 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
9396 { "name", setconfvalue
, SWM_S_WORKSPACE_NAME
},
9400 _add_startup_exception(const char *fmt
, va_list ap
)
9402 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
9403 warn("%s: asprintf", __func__
);
9407 add_startup_exception(const char *fmt
, ...)
9413 if (startup_exception
)
9416 /* force bar to be enabled due to exception */
9420 _add_startup_exception(fmt
, ap
);
9425 conf_load(const char *filename
, int keymapping
)
9428 char *line
= NULL
, *cp
, *ce
, *optsub
, *optval
= NULL
;
9429 size_t linelen
, lineno
= 0;
9430 int wordlen
, i
, optidx
, count
;
9431 struct config_option
*opt
= NULL
;
9433 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
9435 if (filename
== NULL
) {
9436 warnx("conf_load: no filename");
9440 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
9442 if ((config
= fopen(filename
, "r")) == NULL
) {
9443 warn("conf_load: fopen: %s", filename
);
9447 while (!feof(config
)) {
9451 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
9452 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
9454 err(1, "%s", filename
);
9459 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9460 if (cp
[0] == '\0') {
9464 /* get config option */
9465 wordlen
= strcspn(cp
, "=[ \t\n");
9467 add_startup_exception("%s: line %zd: no option found",
9472 for (i
= 0; i
< LENGTH(configopt
); i
++) {
9473 opt
= &configopt
[i
];
9474 if (strncasecmp(cp
, opt
->optname
, wordlen
) == 0 &&
9475 (int)strlen(opt
->optname
) == wordlen
) {
9481 add_startup_exception("%s: line %zd: unknown option "
9482 "%.*s", filename
, lineno
, wordlen
, cp
);
9485 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
9486 add_startup_exception("%s: line %zd: invalid option "
9487 "%.*s", filename
, lineno
, wordlen
, cp
);
9491 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9493 /* get [selector] if any */
9498 /* Get length of selector. */
9499 for (ce
= ++cp
; *ce
!= '\0'; ++ce
) {
9500 /* Find matching (not escaped) bracket. */
9501 if (*ce
== ']' && *(ce
- 1) != '\\') {
9508 add_startup_exception("%s: line %zd: syntax "
9509 "error: unterminated selector", filename
,
9514 /* ce points at ']'; terminate optsub. */
9519 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
9522 if (strlen(optval
) == 0) {
9523 add_startup_exception("%s: line %zd: must supply value "
9524 "to %s", filename
, lineno
,
9525 configopt
[optidx
].optname
);
9528 /* call function to deal with it all */
9529 if (configopt
[optidx
].func(optsub
, optval
,
9530 configopt
[optidx
].funcflags
) != 0) {
9531 add_startup_exception("%s: line %zd: invalid data for "
9532 "%s", filename
, lineno
, configopt
[optidx
].optname
);
9541 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
9547 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
9549 struct ws_win
*parent
, *w
;
9550 struct swm_region
*r
;
9551 struct workspace
*ws
;
9552 xcb_icccm_wm_hints_t wmh
;
9554 parent
= find_window(win
->transient
);
9556 parent
->focus_child
= win
;
9558 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
9559 " for %#x trans %#x\n", win
->id
, win
->transient
);
9561 r
= root_to_region(win
->s
->root
, SWM_CK_ALL
);
9563 /* parent doen't exist in our window list */
9564 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
9565 if (xcb_icccm_get_wm_hints_reply(conn
,
9566 xcb_icccm_get_wm_hints(conn
, w
->id
),
9568 warnx("can't get hints for %#x", w
->id
);
9572 if (win
->hints
.window_group
!= wmh
.window_group
)
9575 w
->focus_child
= win
;
9576 win
->transient
= w
->id
;
9578 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
9579 "transient to %#x\n", win
->transient
);
9586 window_get_pid(xcb_window_t win
)
9591 xcb_get_property_cookie_t pc
;
9592 xcb_get_property_reply_t
*pr
;
9594 apid
= get_atom_from_string("_NET_WM_PID");
9595 if (apid
== XCB_ATOM_NONE
)
9598 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
9599 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9602 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
9607 if (pr
->type
== apid
&& pr
->format
== 32)
9608 ret
= *((pid_t
*)xcb_get_property_value(pr
));
9614 apid
= get_atom_from_string("_SWM_PID");
9615 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
9617 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9620 if (pr
->type
!= apid
) {
9625 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
9632 get_swm_ws(xcb_window_t id
)
9638 xcb_get_property_reply_t
*gpr
;
9640 gpr
= xcb_get_property_reply(conn
,
9641 xcb_get_property(conn
, 0, id
, a_swm_ws
,
9642 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
9647 proplen
= xcb_get_property_value_length(gpr
);
9649 prop
= malloc(proplen
+ 1);
9652 xcb_get_property_value(gpr
),
9654 prop
[proplen
] = '\0';
9661 DNPRINTF(SWM_D_PROP
, "get_swm_ws: _SWM_WS: %s\n", prop
);
9662 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
9664 DNPRINTF(SWM_D_PROP
, "get_swm_ws: win #%s: %s",
9674 get_ws_idx(struct ws_win
*win
)
9676 xcb_get_property_reply_t
*gpr
;
9682 gpr
= xcb_get_property_reply(conn
,
9683 xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
9684 XCB_ATOM_CARDINAL
, 0, 1),
9687 if (gpr
->type
== XCB_ATOM_CARDINAL
&& gpr
->format
== 32)
9688 ws_idx
= *((int *)xcb_get_property_value(gpr
));
9692 if (ws_idx
== -1 && !(win
->quirks
& SWM_Q_IGNORESPAWNWS
))
9693 ws_idx
= get_swm_ws(win
->id
);
9695 if (ws_idx
> workspace_limit
- 1 || ws_idx
< -1)
9698 DNPRINTF(SWM_D_PROP
, "get_ws_idx: win %#x, ws_idx: %d\n", win
->id
,
9705 reparent_window(struct ws_win
*win
)
9707 xcb_void_cookie_t c
;
9708 xcb_generic_error_t
*error
;
9711 win
->frame
= xcb_generate_id(conn
);
9713 DNPRINTF(SWM_D_MISC
, "reparent_window: win %#x, frame: %#x\n",
9714 win
->id
, win
->frame
);
9717 XCB_EVENT_MASK_ENTER_WINDOW
|
9718 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
9719 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
9720 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
|
9721 XCB_EVENT_MASK_EXPOSURE
;
9723 wa
[0] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
9726 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
->frame
, win
->s
->root
,
9727 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), 0,
9728 XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
9729 XCB_CW_EVENT_MASK
, wa
);
9731 win
->state
= SWM_WIN_STATE_REPARENTING
;
9732 c
= xcb_reparent_window_checked(conn
, win
->id
, win
->frame
, 0, 0);
9733 if ((error
= xcb_request_check(conn
, c
))) {
9734 DNPRINTF(SWM_D_MISC
, "reparent_window: error:\n");
9739 xcb_destroy_window(conn
, win
->frame
);
9740 win
->frame
= XCB_WINDOW_NONE
;
9742 xcb_change_save_set(conn
, XCB_SET_MODE_INSERT
, win
->id
);
9744 DNPRINTF(SWM_D_MISC
, "reparent_window: done.\n");
9748 unparent_window(struct ws_win
*win
)
9750 xcb_change_save_set(conn
, XCB_SET_MODE_DELETE
, win
->id
);
9751 xcb_reparent_window(conn
, win
->id
, win
->s
->root
, X(win
), Y(win
));
9752 xcb_destroy_window(conn
, win
->frame
);
9753 win
->frame
= XCB_WINDOW_NONE
;
9754 win
->state
= SWM_WIN_STATE_UNPARENTING
;
9758 manage_window(xcb_window_t id
, int spawn_pos
, bool mapping
)
9760 struct ws_win
*win
= NULL
, *ww
;
9761 struct swm_region
*r
;
9764 xcb_get_geometry_reply_t
*gr
;
9765 xcb_get_window_attributes_reply_t
*war
= NULL
;
9766 xcb_window_t trans
= XCB_WINDOW_NONE
;
9767 uint32_t i
, wa
[1], new_flags
;
9768 int ws_idx
, force_ws
= -1;
9769 char *class, *instance
, *name
;
9772 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region bar; "
9777 if (find_region(id
)) {
9778 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region window; "
9783 if ((win
= find_window(id
)) != NULL
) {
9784 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x already "
9785 "managed; skipping.)\n", id
);
9786 return (win
); /* Already managed. */
9789 /* See if window is on the unmanaged list. */
9790 if ((win
= find_unmanaged_window(id
)) != NULL
) {
9791 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x found on "
9792 "unmanaged list.\n", id
);
9793 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
9796 set_child_transient(win
, &trans
);
9800 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is new.\n", id
);
9803 war
= xcb_get_window_attributes_reply(conn
,
9804 xcb_get_window_attributes(conn
, id
), NULL
);
9806 DNPRINTF(SWM_D_EVENT
, "manage_window: window lost.\n");
9810 if (war
->override_redirect
) {
9811 DNPRINTF(SWM_D_EVENT
, "manage_window: override_redirect; "
9816 if (!mapping
&& war
->map_state
== XCB_MAP_STATE_UNMAPPED
&&
9817 get_win_state(id
) == XCB_ICCCM_WM_STATE_WITHDRAWN
) {
9818 DNPRINTF(SWM_D_EVENT
, "manage_window: window withdrawn; "
9823 /* Try to get initial window geometry. */
9824 gr
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, id
), NULL
);
9826 DNPRINTF(SWM_D_MISC
, "manage_window: get geometry failed.\n");
9830 /* Create and initialize ws_win object. */
9831 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
9832 err(1, "manage_window: calloc: failed to allocate memory for "
9837 /* Figureout which region the window belongs to. */
9838 r
= root_to_region(gr
->root
, SWM_CK_ALL
);
9840 /* Ignore window border if there is one. */
9841 WIDTH(win
) = gr
->width
;
9842 HEIGHT(win
) = gr
->height
;
9843 X(win
) = gr
->x
+ gr
->border_width
;
9844 Y(win
) = gr
->y
+ gr
->border_width
;
9845 win
->bordered
= false;
9846 win
->mapped
= (war
->map_state
!= XCB_MAP_STATE_UNMAPPED
);
9847 win
->s
= r
->s
; /* this never changes */
9851 /* Select which X events to monitor and set border pixel color. */
9852 wa
[0] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
9853 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
9855 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_EVENT_MASK
, wa
);
9857 /* Get WM_SIZE_HINTS. */
9858 xcb_icccm_get_wm_normal_hints_reply(conn
,
9859 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
9863 xcb_icccm_get_wm_hints_reply(conn
,
9864 xcb_icccm_get_wm_hints(conn
, win
->id
),
9867 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
9868 xcb_icccm_get_wm_transient_for_reply(conn
,
9869 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
9872 win
->transient
= trans
;
9873 set_child_transient(win
, &win
->transient
);
9876 /* Get WM_PROTOCOLS. */
9877 get_wm_protocols(win
);
9880 /* Must be after getting WM_HINTS and WM_PROTOCOLS. */
9881 DNPRINTF(SWM_D_FOCUS
, "manage_window: input_model: %s\n",
9882 get_win_input_model(win
));
9885 /* Set initial quirks based on EWMH. */
9886 ewmh_autoquirk(win
);
9888 /* Determine initial quirks. */
9889 xcb_icccm_get_wm_class_reply(conn
,
9890 xcb_icccm_get_wm_class(conn
, win
->id
),
9893 class = win
->ch
.class_name
? win
->ch
.class_name
: "";
9894 instance
= win
->ch
.instance_name
? win
->ch
.instance_name
: "";
9895 name
= get_win_name(win
->id
);
9897 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, instance: %s, "
9898 "name: %s\n", class, instance
, name
);
9900 /* java is retarded so treat it special */
9901 if (strstr(instance
, "sun-awt")) {
9902 DNPRINTF(SWM_D_CLASS
, "manage_window: java window detected.\n");
9906 TAILQ_FOREACH(qp
, &quirks
, entry
) {
9907 if (regexec(&qp
->regex_class
, class, 0, NULL
, 0) == 0 &&
9908 regexec(&qp
->regex_instance
, instance
, 0, NULL
, 0) == 0 &&
9909 regexec(&qp
->regex_name
, name
, 0, NULL
, 0) == 0) {
9910 DNPRINTF(SWM_D_CLASS
, "manage_window: matched "
9911 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp
->class,
9912 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
9913 win
->quirks
= qp
->quirk
;
9914 if (qp
->ws
>= 0 && qp
->ws
< workspace_limit
)
9921 /* Reset font sizes (the bruteforce way; no default keybinding). */
9922 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
9923 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9924 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
9925 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9926 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
9929 /* Figure out which workspace the window belongs to. */
9930 if (!(win
->quirks
& SWM_Q_IGNOREPID
) &&
9931 (p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
9932 win
->ws
= &r
->s
->ws
[p
->ws
];
9933 TAILQ_REMOVE(&pidlist
, p
, entry
);
9936 } else if ((ws_idx
= get_ws_idx(win
)) != -1 &&
9938 /* _SWM_WS is set; use that. */
9939 win
->ws
= &r
->s
->ws
[ws_idx
];
9940 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
9941 /* Launch transients in the same ws as parent. */
9948 win
->ws
= &r
->s
->ws
[force_ws
];
9950 /* Set the _NET_WM_DESKTOP atom. */
9951 DNPRINTF(SWM_D_PROP
, "manage_window: set _NET_WM_DESKTOP: %d\n",
9953 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
9954 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &win
->ws
->idx
);
9956 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
9957 xcb_delete_property(conn
, win
->id
, a_swm_ws
);
9959 /* WS must already be set for this to work. */
9960 store_float_geom(win
);
9962 /* Make sure window is positioned inside its region, if its active. */
9964 region_containment(win
, r
, SWM_CW_ALLSIDES
|
9965 SWM_CW_HARDBOUNDARY
);
9970 /* Figure out where to insert the window in the workspace list. */
9971 if (trans
&& (ww
= find_window(trans
)))
9972 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
9973 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_ABOVE
)
9974 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
9976 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_BELOW
)
9977 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
9978 else switch (spawn_pos
) {
9981 case SWM_STACK_ABOVE
:
9982 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
9984 case SWM_STACK_BOTTOM
:
9985 case SWM_STACK_BELOW
:
9986 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
9989 ewmh_update_client_list();
9991 TAILQ_INSERT_HEAD(&win
->ws
->stack
, win
, stack_entry
);
9994 /* Get/apply initial _NET_WM_STATE */
9995 ewmh_get_wm_state(win
);
9998 new_flags
= win
->ewmh_flags
;
10000 if (win
->quirks
& SWM_Q_FLOAT
)
10001 new_flags
|= EWMH_F_ABOVE
;
10003 if (win
->quirks
& SWM_Q_ANYWHERE
)
10004 new_flags
|= SWM_F_MANUAL
;
10006 ewmh_apply_flags(win
, new_flags
);
10007 ewmh_update_wm_state(win
);
10009 /* Set initial _NET_WM_ALLOWED_ACTIONS */
10010 ewmh_update_actions(win
);
10012 reparent_window(win
);
10014 DNPRINTF(SWM_D_MISC
, "manage_window: done. win %#x, (x,y) w x h: "
10015 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win
->id
,
10016 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
10017 YESNO(ICONIC(win
)), win
->transient
);
10024 free_window(struct ws_win
*win
)
10026 DNPRINTF(SWM_D_MISC
, "free_window: win %#x\n", WINID(win
));
10031 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
10034 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
10037 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
10041 unmanage_window(struct ws_win
*win
)
10043 DNPRINTF(SWM_D_MISC
, "unmanage_window: win %#x\n", WINID(win
));
10049 unparent_window(win
);
10051 TAILQ_REMOVE(&win
->ws
->stack
, win
, stack_entry
);
10052 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
10053 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
10055 ewmh_update_client_list();
10059 expose(xcb_expose_event_t
*e
)
10064 struct workspace
*ws
;
10067 DNPRINTF(SWM_D_EVENT
, "expose: win %#x, count: %d\n", e
->window
,
10073 if ((b
= find_bar(e
->window
))) {
10076 } else if ((w
= find_window(e
->window
)) && w
->frame
== e
->window
) {
10080 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
10086 DNPRINTF(SWM_D_EVENT
, "expose: done\n");
10090 focusin(xcb_focus_in_event_t
*e
)
10092 struct ws_win
*win
;
10094 DNPRINTF(SWM_D_EVENT
, "focusin: win %#x, mode: %s(%u), "
10095 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10096 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10097 if ((win
= find_window(e
->event
)) && win
!= win
->ws
->focus
&&
10098 win
!= win
->ws
->focus_pending
&&
10099 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10100 win
->ws
->focus_prev
= win
->ws
->focus
;
10101 win
->ws
->focus
= win
;
10102 win
->ws
->focus_pending
= NULL
;
10104 if (win
->ws
->focus_prev
)
10105 draw_frame(win
->ws
->focus_prev
);
10113 focusout(xcb_focus_out_event_t
*e
)
10115 DNPRINTF(SWM_D_EVENT
, "focusout: win %#x, mode: %s(%u), "
10116 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10117 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10122 keypress(xcb_key_press_event_t
*e
)
10125 struct binding
*bp
;
10126 xcb_keysym_t keysym
;
10127 bool replay
= true;
10129 last_event_time
= e
->time
;
10131 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
10133 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
10134 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10135 "state: %u, cleaned: %u, same_screen: %s\n", keysym
, e
->event
,
10136 e
->event_x
, e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
,
10137 e
->root_y
, e
->child
, e
->state
, CLEANMASK(e
->state
),
10138 YESNO(e
->same_screen
));
10140 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10142 /* Look for catch-all. */
10143 if ((bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
)) == NULL
)
10147 replay
= bp
->flags
& BINDING_F_REPLAY
;
10149 if ((ap
= &actions
[bp
->action
]) == NULL
)
10152 if (bp
->action
== FN_SPAWN_CUSTOM
)
10153 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10156 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10158 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10162 DNPRINTF(SWM_D_EVENT
, "keypress: replaying.\n");
10163 /* Pass keypress to event window and unfreeze keyboard queue. */
10164 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10166 /* Release freeze. */
10167 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10171 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
10175 keyrelease(xcb_key_release_event_t
*e
)
10178 struct binding
*bp
;
10179 xcb_keysym_t keysym
;
10181 last_event_time
= e
->time
;
10183 keysym
= xcb_key_release_lookup_keysym(syms
, e
, 0);
10185 DNPRINTF(SWM_D_EVENT
, "keyrelease: keysym: %u, win (x,y): %#x (%d,%d), "
10186 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10187 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
10188 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
10189 e
->child
, e
->state
, YESNO(e
->same_screen
));
10191 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10193 /* Look for catch-all. */
10194 bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
);
10196 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10197 bp
->flags
& BINDING_F_REPLAY
) {
10198 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10199 DNPRINTF(SWM_D_EVENT
, "keyrelease: replaying.\n");
10201 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10202 DNPRINTF(SWM_D_EVENT
, "keyrelease: unfreezing.\n");
10207 DNPRINTF(SWM_D_EVENT
, "keyrelease: done.\n");
10211 buttonpress(xcb_button_press_event_t
*e
)
10213 struct ws_win
*win
= NULL
, *newf
;
10214 struct swm_region
*r
, *old_r
;
10216 struct binding
*bp
;
10217 bool replay
= true;
10219 last_event_time
= e
->time
;
10221 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): %#x (%d,%d), "
10222 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10223 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10224 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10225 e
->state
, YESNO(e
->same_screen
));
10227 if (e
->event
== e
->root
) {
10229 win
= find_window(e
->child
);
10231 /* Focus on empty region */
10232 /* If no windows on region if its empty. */
10233 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10234 if (r
&& TAILQ_EMPTY(&r
->ws
->winlist
)) {
10235 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
10236 if (old_r
&& old_r
!= r
)
10237 unfocus_win(old_r
->ws
->focus
);
10239 DNPRINTF(SWM_D_FOCUS
, "buttonpress: "
10240 "set_input_focus: %#x, revert-to: parent, "
10241 "time: %#x\n", e
->root
, e
->time
);
10242 xcb_set_input_focus(conn
,
10243 XCB_INPUT_FOCUS_POINTER_ROOT
,
10246 /* Clear bar since empty. */
10249 /* No need to replay event. */
10254 win
= find_window(e
->event
);
10258 newf
= get_focus_magic(win
);
10259 if (win
->ws
->focus
== newf
&& newf
!= win
) {
10260 if (win
->focus_child
== win
)
10261 win
->focus_child
= NULL
;
10267 /* Handle any bound action. */
10268 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10270 /* Look for catch-all. */
10271 if ((bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
)) == NULL
)
10276 replay
= bp
->flags
& BINDING_F_REPLAY
;
10278 if ((ap
= &actions
[bp
->action
]) == NULL
)
10281 if (bp
->action
== FN_SPAWN_CUSTOM
)
10282 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10285 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10287 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10291 DNPRINTF(SWM_D_EVENT
, "buttonpress: replaying.\n");
10292 /* Replay event to event window */
10293 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10295 /* Unfreeze grab events. */
10296 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10303 buttonrelease(xcb_button_release_event_t
*e
)
10306 struct binding
*bp
;
10308 last_event_time
= e
->time
;
10310 DNPRINTF(SWM_D_EVENT
, "buttonrelease: win (x,y): %#x (%d,%d), "
10311 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10312 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10313 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10314 e
->state
, YESNO(e
->same_screen
));
10316 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10318 /* Look for catch-all. */
10319 bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
);
10321 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10322 bp
->flags
& BINDING_F_REPLAY
) {
10323 DNPRINTF(SWM_D_EVENT
, "buttonrelease: replaying.\n");
10324 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10326 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10334 get_win_input_model(struct ws_win
*win
)
10338 * Input Model Input Field WM_TAKE_FOCUS
10339 * No Input False Absent
10340 * Passive True Absent
10341 * Locally Active True Present
10342 * Globally Active False Present
10345 if (ACCEPTS_FOCUS(win
))
10346 inputmodel
= (win
->take_focus
) ? "Locally Active" : "Passive";
10348 inputmodel
= (win
->take_focus
) ? "Globally Active" : "No Input";
10354 print_win_geom(xcb_window_t w
)
10356 xcb_get_geometry_reply_t
*wa
;
10358 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
10360 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: %#x\n",
10365 DNPRINTF(SWM_D_MISC
, "print_win_geom: win %#x, root: %#x, "
10366 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
10367 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
10376 get_stack_mode_name(uint8_t mode
)
10381 case XCB_STACK_MODE_ABOVE
:
10384 case XCB_STACK_MODE_BELOW
:
10387 case XCB_STACK_MODE_TOP_IF
:
10390 case XCB_STACK_MODE_BOTTOM_IF
:
10393 case XCB_STACK_MODE_OPPOSITE
:
10405 configurerequest(xcb_configure_request_event_t
*e
)
10407 struct ws_win
*win
;
10408 struct swm_region
*r
= NULL
;
10410 uint32_t wc
[7] = {0};
10414 if ((win
= find_window(e
->window
)) == NULL
)
10415 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10419 if (swm_debug
& SWM_D_EVENT
) {
10420 print_win_geom(e
->window
);
10422 DNPRINTF(SWM_D_EVENT
, "configurerequest: win %#x, "
10423 "parent: %#x, new: %s, value_mask: %u { ", e
->window
,
10424 e
->parent
, YESNO(new), e
->value_mask
);
10425 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
10426 DPRINTF("X: %d ", e
->x
);
10427 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
10428 DPRINTF("Y: %d ", e
->y
);
10429 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10430 DPRINTF("W: %u ", e
->width
);
10431 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10432 DPRINTF("H: %u ", e
->height
);
10433 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
10434 DPRINTF("Border: %u ", e
->border_width
);
10435 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
10436 DPRINTF("Sibling: %#x ", e
->sibling
);
10437 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
10438 DPRINTF("StackMode: %s(%u) ",
10439 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
10445 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10446 mask
|= XCB_CONFIG_WINDOW_X
;
10449 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10450 mask
|= XCB_CONFIG_WINDOW_Y
;
10453 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
10454 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
10455 wc
[i
++] = e
->width
;
10457 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
10458 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
10459 wc
[i
++] = e
->height
;
10461 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
10462 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
10463 wc
[i
++] = e
->border_width
;
10465 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
10466 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
10467 wc
[i
++] = e
->sibling
;
10469 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
10470 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
10471 wc
[i
++] = e
->stack_mode
;
10475 xcb_configure_window(conn
, e
->window
, mask
, wc
);
10478 } else if ((!MANUAL(win
) || win
->quirks
& SWM_Q_ANYWHERE
) &&
10479 !FULLSCREEN(win
) && !MAXIMIZED(win
)) {
10482 else if (win
->ws
->old_r
)
10483 r
= win
->ws
->old_r
;
10485 /* windows are centered unless ANYWHERE quirk is set. */
10486 if (win
->quirks
& SWM_Q_ANYWHERE
) {
10487 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10488 win
->g_float
.x
= e
->x
;
10490 win
->g_float
.x
-= X(r
);
10493 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10494 win
->g_float
.y
= e
->y
;
10496 win
->g_float
.y
-= Y(r
);
10500 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10501 win
->g_float
.w
= e
->width
;
10503 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10504 win
->g_float
.h
= e
->height
;
10506 win
->g_floatvalid
= true;
10508 if (!MAXIMIZED(win
) && !FULLSCREEN(win
) &&
10509 (TRANS(win
) || (ABOVE(win
) &&
10510 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]))) {
10511 WIDTH(win
) = win
->g_float
.w
;
10512 HEIGHT(win
) = win
->g_float
.h
;
10515 update_floater(win
);
10518 config_win(win
, e
);
10522 config_win(win
, e
);
10526 config_win(win
, e
);
10530 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
10534 configurenotify(xcb_configure_notify_event_t
*e
)
10536 struct ws_win
*win
;
10538 DNPRINTF(SWM_D_EVENT
, "configurenotify: win %#x, event win: %#x, "
10539 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
10540 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
10541 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
10542 YESNO(e
->override_redirect
));
10544 win
= find_window(e
->window
);
10547 if (font_adjusted
&& win
->ws
->r
) {
10555 destroynotify(xcb_destroy_notify_event_t
*e
)
10557 struct ws_win
*win
;
10558 struct workspace
*ws
;
10560 DNPRINTF(SWM_D_EVENT
, "destroynotify: win %#x\n", e
->window
);
10562 if ((win
= find_window(e
->window
)) == NULL
) {
10563 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10565 /* Window is on unmanaged list. */
10566 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10573 if (win
->frame
== e
->window
) {
10574 DNPRINTF(SWM_D_EVENT
, "destroynotify: frame for win %#x\n",
10576 win
->frame
= XCB_WINDOW_NONE
;
10580 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10581 /* If we were focused, make sure we focus on something else. */
10582 if (win
== ws
->focus
) {
10583 ws
->focus_pending
= get_focus_prev(win
);
10584 if (ws
->focus_pending
== win
)
10585 ws
->focus_pending
= NULL
;
10589 unmanage_window(win
);
10590 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10594 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
10595 if (ws
->focus_pending
) {
10596 focus_win(ws
->focus_pending
);
10597 ws
->focus_pending
= NULL
;
10598 } else if (ws
->focus
== NULL
) {
10599 DNPRINTF(SWM_D_FOCUS
, "destroynotify: set_input_focus: "
10600 "%#x, revert-to: parent, time: 0\n", ws
->r
->id
);
10601 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
10602 ws
->r
->id
, XCB_CURRENT_TIME
);
10609 DNPRINTF(SWM_D_EVENT
, "destroynotify: done.\n");
10614 get_notify_detail_label(uint8_t detail
)
10619 case XCB_NOTIFY_DETAIL_ANCESTOR
:
10620 label
= "Ancestor";
10622 case XCB_NOTIFY_DETAIL_VIRTUAL
:
10625 case XCB_NOTIFY_DETAIL_INFERIOR
:
10626 label
= "Inferior";
10628 case XCB_NOTIFY_DETAIL_NONLINEAR
:
10629 label
= "Nonlinear";
10631 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
10632 label
= "NonlinearVirtual";
10634 case XCB_NOTIFY_DETAIL_POINTER
:
10637 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
10638 label
= "PointerRoot";
10640 case XCB_NOTIFY_DETAIL_NONE
:
10651 get_notify_mode_label(uint8_t mode
)
10656 case XCB_NOTIFY_MODE_NORMAL
:
10659 case XCB_NOTIFY_MODE_GRAB
:
10662 case XCB_NOTIFY_MODE_UNGRAB
:
10665 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
10666 label
= "WhileGrabbed";
10676 get_state_mask_label(uint16_t state
)
10681 case XCB_KEY_BUT_MASK_SHIFT
:
10682 label
= "ShiftMask";
10684 case XCB_KEY_BUT_MASK_LOCK
:
10685 label
= "LockMask";
10687 case XCB_KEY_BUT_MASK_CONTROL
:
10688 label
= "ControlMask";
10690 case XCB_KEY_BUT_MASK_MOD_1
:
10691 label
= "Mod1Mask";
10693 case XCB_KEY_BUT_MASK_MOD_2
:
10694 label
= "Mod2Mask";
10696 case XCB_KEY_BUT_MASK_MOD_3
:
10697 label
= "Mod3Mask";
10699 case XCB_KEY_BUT_MASK_MOD_4
:
10700 label
= "Mod4Mask";
10702 case XCB_KEY_BUT_MASK_MOD_5
:
10703 label
= "Mod5Mask";
10705 case XCB_KEY_BUT_MASK_BUTTON_1
:
10706 label
= "Button1Mask";
10708 case XCB_KEY_BUT_MASK_BUTTON_2
:
10709 label
= "Button2Mask";
10711 case XCB_KEY_BUT_MASK_BUTTON_3
:
10712 label
= "Button3Mask";
10714 case XCB_KEY_BUT_MASK_BUTTON_4
:
10715 label
= "Button4Mask";
10717 case XCB_KEY_BUT_MASK_BUTTON_5
:
10718 label
= "Button5Mask";
10732 enternotify(xcb_enter_notify_event_t
*e
)
10734 struct ws_win
*win
;
10735 struct swm_region
*r
;
10737 last_event_time
= e
->time
;
10739 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): %#x "
10740 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10741 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10742 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10743 get_notify_mode_label(e
->mode
), e
->mode
,
10744 get_notify_detail_label(e
->detail
), e
->detail
,
10745 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10746 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10749 if (e
->event
== e
->root
&& e
->child
== XCB_WINDOW_NONE
&&
10750 e
->mode
== XCB_NOTIFY_MODE_GRAB
&&
10751 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10752 DNPRINTF(SWM_D_EVENT
, "enternotify: grab inferior; ignoring.\n");
10756 if (focus_mode
== SWM_FOCUS_MANUAL
&&
10757 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10758 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
10762 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10763 e
->mode
== XCB_NOTIFY_MODE_UNGRAB
&&
10764 e
->detail
!= XCB_NOTIFY_DETAIL_ANCESTOR
) {
10765 DNPRINTF(SWM_D_EVENT
, "enternotify: ungrab; ignoring.\n");
10769 if ((win
= find_window(e
->event
)) == NULL
) {
10770 if (e
->event
== e
->root
) {
10771 /* If no windows on pointer region, then focus root. */
10772 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10774 DNPRINTF(SWM_D_EVENT
, "enternotify: "
10775 "NULL region; ignoring.\n");
10781 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
10786 if (e
->mode
== XCB_NOTIFY_MODE_NORMAL
&&
10787 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10788 DNPRINTF(SWM_D_EVENT
, "enternotify: entering from "
10789 "inferior; ignoring\n");
10793 focus_win(get_focus_magic(win
));
10796 DNPRINTF(SWM_D_EVENT
, "enternotify: done\n");
10803 leavenotify(xcb_leave_notify_event_t
*e
)
10805 last_event_time
= e
->time
;
10807 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): %#x "
10808 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10809 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10810 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10811 get_notify_mode_label(e
->mode
), e
->mode
,
10812 get_notify_detail_label(e
->detail
), e
->detail
,
10813 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10814 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10820 mapnotify(xcb_map_notify_event_t
*e
)
10822 struct ws_win
*win
, *parent
= NULL
;
10823 struct workspace
*ws
;
10825 DNPRINTF(SWM_D_EVENT
, "mapnotify: win %#x\n", e
->window
);
10827 if ((win
= manage_window(e
->window
, spawn_position
, false)) == NULL
)
10831 if (win
->state
== SWM_WIN_STATE_REPARENTING
)
10834 if (ws
->r
== NULL
) {
10839 /* Need to know if win was mapped due to ws switch. */
10840 if (ws
->state
== SWM_WS_STATE_MAPPED
) {
10841 if (ws
->focus_pending
&& TRANS(ws
->focus_pending
))
10842 parent
= find_window(win
->transient
);
10844 /* If window's parent is maximized, don't clear it. */
10845 if ((parent
== NULL
) || !MAXIMIZED(parent
))
10846 if (clear_maximized(ws
) > 0)
10850 win
->mapped
= true;
10851 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
10853 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
10854 if (ws
->focus_pending
== win
) {
10856 ws
->focus_pending
= NULL
;
10857 center_pointer(ws
->r
);
10863 DNPRINTF(SWM_D_EVENT
, "mapnotify: done\n");
10869 mappingnotify(xcb_mapping_notify_event_t
*e
)
10871 if (e
->request
!= XCB_MAPPING_POINTER
) {
10872 xcb_refresh_keyboard_mapping(syms
, e
);
10880 maprequest(xcb_map_request_event_t
*e
)
10882 struct ws_win
*win
, *w
= NULL
;
10884 DNPRINTF(SWM_D_EVENT
, "maprequest: win %#x\n",
10887 win
= manage_window(e
->window
, spawn_position
, true);
10891 /* The new window should get focus; prepare. */
10892 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10893 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
) && ACCEPTS_FOCUS(win
)) {
10894 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
10895 /* See if other wins of same type are already mapped. */
10896 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
10897 if (w
== win
|| !w
->mapped
)
10900 if (w
->ch
.class_name
&&
10901 win
->ch
.class_name
&&
10902 strcmp(w
->ch
.class_name
,
10903 win
->ch
.class_name
) == 0 &&
10904 w
->ch
.instance_name
&&
10905 win
->ch
.instance_name
&&
10906 strcmp(w
->ch
.instance_name
,
10907 win
->ch
.instance_name
) == 0)
10913 win
->ws
->focus_pending
= get_focus_magic(win
);
10916 /* All windows need to be mapped if they are in the current workspace.*/
10919 /* Ignore EnterNotify to handle the mapnotify without interference. */
10920 if (focus_mode
== SWM_FOCUS_DEFAULT
)
10921 event_drain(XCB_ENTER_NOTIFY
);
10923 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
10927 motionnotify(xcb_motion_notify_event_t
*e
)
10929 struct swm_region
*r
= NULL
;
10930 int i
, num_screens
;
10932 last_event_time
= e
->time
;
10934 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): %#x "
10935 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10936 "child: %#x, same_screen_focus: %s, state: %d\n",
10937 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10938 get_notify_detail_label(e
->detail
), e
->detail
,
10939 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10940 YESNO(e
->same_screen
), e
->state
);
10942 if (focus_mode
== SWM_FOCUS_MANUAL
)
10945 num_screens
= get_screen_count();
10946 for (i
= 0; i
< num_screens
; i
++)
10947 if (screens
[i
].root
== e
->root
)
10950 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10951 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
10952 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
10960 get_atom_name(xcb_atom_t atom
)
10963 #ifdef SWM_DEBUG_ATOM_NAMES
10965 * This should be disabled during most debugging since
10966 * xcb_get_* causes an xcb_flush.
10969 xcb_get_atom_name_reply_t
*r
;
10971 r
= xcb_get_atom_name_reply(conn
,
10972 xcb_get_atom_name(conn
, atom
),
10975 len
= xcb_get_atom_name_name_length(r
);
10977 name
= malloc(len
+ 1);
10979 memcpy(name
, xcb_get_atom_name_name(r
), len
);
10993 propertynotify(xcb_property_notify_event_t
*e
)
10995 struct ws_win
*win
;
10996 struct workspace
*ws
;
11000 name
= get_atom_name(e
->atom
);
11001 DNPRINTF(SWM_D_EVENT
, "propertynotify: win %#x, atom: %s(%u), "
11002 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
11006 last_event_time
= e
->time
;
11008 win
= find_window(e
->window
);
11014 if (e
->atom
== a_state
) {
11015 /* State just changed, make sure it gets focused if mapped. */
11016 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
11017 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
11019 win
->state
== SWM_WIN_STATE_REPARENTED
&&
11020 ws
->focus_pending
== win
) {
11021 focus_win(ws
->focus_pending
);
11022 ws
->focus_pending
= NULL
;
11026 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
11027 e
->atom
== XCB_ATOM_WM_NAME
) {
11029 bar_draw(ws
->r
->bar
);
11030 } else if (e
->atom
== a_prot
) {
11031 get_wm_protocols(win
);
11032 } else if (e
->atom
== XCB_ATOM_WM_NORMAL_HINTS
) {
11033 xcb_icccm_get_wm_normal_hints_reply(conn
,
11034 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
11042 reparentnotify(xcb_reparent_notify_event_t
*e
)
11044 struct ws_win
*win
;
11046 DNPRINTF(SWM_D_EVENT
, "reparentnotify: event: %#x, win %#x, "
11047 "parent: %#x, (x,y): (%u,%u), override_redirect: %u\n",
11048 e
->event
, e
->window
, e
->parent
, e
->x
, e
->y
, e
->override_redirect
);
11050 win
= find_window(e
->window
);
11052 if (win
->state
== SWM_WIN_STATE_REPARENTING
) {
11053 win
->state
= SWM_WIN_STATE_REPARENTED
;
11060 update_window(win
);
11061 update_win_stacking(win
);
11062 } else if (win
->state
== SWM_WIN_STATE_UNPARENTING
) {
11063 win
->state
= SWM_WIN_STATE_UNPARENTED
;
11069 unmapnotify(xcb_unmap_notify_event_t
*e
)
11071 struct ws_win
*win
;
11072 struct workspace
*ws
;
11074 DNPRINTF(SWM_D_EVENT
, "unmapnotify: win %#x\n", e
->window
);
11076 /* If we aren't managing the window, then ignore. */
11077 win
= find_window(e
->window
);
11078 if (win
== NULL
|| win
->id
!= e
->window
) {
11079 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore unmanaged.\n");
11083 /* Do nothing if already withdrawn. */
11084 if (!win
->mapped
&& !ICONIC(win
)) {
11085 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore withdrawn.\n");
11090 win
->mapped
= false;
11092 /* Ignore if reparenting-related. */
11093 if (win
->state
!= SWM_WIN_STATE_REPARENTED
) {
11094 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore not reparented.\n");
11098 /* If win was focused, make sure to focus on something else. */
11099 if (win
== ws
->focus
) {
11100 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
11101 ws
->focus_pending
= get_focus_prev(win
);
11102 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
11103 "focus_pending: %#x\n",
11104 WINID(ws
->focus_pending
));
11112 DNPRINTF(SWM_D_EVENT
, "unmapnotify: iconify.\n");
11113 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
11116 DNPRINTF(SWM_D_EVENT
, "unmapnotify: withdraw.\n");
11117 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
11118 unmanage_window(win
);
11123 /* Update focus if ws is active. */
11124 if (WS_FOCUSED(ws
)) {
11125 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
11126 focus_win(get_pointer_win(ws
->r
->s
->root
));
11127 } else if (ws
->focus_pending
) {
11128 focus_win(ws
->focus_pending
);
11129 ws
->focus_pending
= NULL
;
11130 } else if (ws
->focus
== NULL
) {
11131 DNPRINTF(SWM_D_FOCUS
, "unmapnotify: set_input_focus: "
11132 "%#x, revert-to: parent, time: 0\n",
11134 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11135 ws
->r
->id
, XCB_CURRENT_TIME
);
11139 center_pointer(ws
->r
);
11141 DNPRINTF(SWM_D_EVENT
, "unmapnotify: done.\n");
11146 get_source_type_label(uint32_t type
)
11151 case EWMH_SOURCE_TYPE_NONE
:
11154 case EWMH_SOURCE_TYPE_NORMAL
:
11157 case EWMH_SOURCE_TYPE_OTHER
:
11169 clientmessage(xcb_client_message_event_t
*e
)
11171 struct ws_win
*win
;
11172 struct swm_region
*r
= NULL
;
11175 int num_screens
, i
;
11176 xcb_map_request_event_t mre
;
11180 name
= get_atom_name(e
->type
);
11181 DNPRINTF(SWM_D_EVENT
, "clientmessage: win %#x, atom: %s(%u)\n",
11182 e
->window
, name
, e
->type
);
11186 if (e
->type
== ewmh
[_NET_CURRENT_DESKTOP
].atom
) {
11187 num_screens
= get_screen_count();
11188 for (i
= 0; i
< num_screens
; i
++)
11189 if (screens
[i
].root
== e
->window
) {
11190 r
= screens
[i
].r_focus
;
11194 if (r
&& e
->data
.data32
[0] < (uint32_t)workspace_limit
) {
11195 a
.id
= e
->data
.data32
[0];
11196 switchws(NULL
, r
, &a
);
11201 } else if (e
->type
== ewmh
[_NET_REQUEST_FRAME_EXTENTS
].atom
) {
11202 DNPRINTF(SWM_D_EVENT
,
11203 "clientmessage: set _NET_FRAME_EXTENTS on window.\n");
11204 vals
[0] = vals
[1] = vals
[2] = vals
[3] = border_width
;
11205 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, e
->window
,
11206 a_net_frame_extents
, XCB_ATOM_CARDINAL
, 32, 4, vals
);
11211 win
= find_window(e
->window
);
11213 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11214 /* Manage the window with maprequest. */
11215 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
11216 "unmanaged window.\n");
11217 mre
.window
= e
->window
;
11223 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11224 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW, "
11225 "source_type: %s(%d)\n",
11226 get_source_type_label(e
->data
.data32
[0]),
11227 e
->data
.data32
[0]);
11230 * Allow focus changes that are a result of direct user
11231 * action and from applications that use the old EWMH spec.
11233 if (e
->data
.data32
[0] != EWMH_SOURCE_TYPE_NORMAL
||
11234 win
->quirks
& SWM_Q_OBEYAPPFOCUSREQ
) {
11235 if (WS_FOCUSED(win
->ws
))
11238 win
->ws
->focus_pending
= win
;
11240 } else if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
11241 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
11242 if (win
->can_delete
)
11243 client_msg(win
, a_delete
, 0);
11245 xcb_kill_client(conn
, win
->id
);
11246 } else if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
11247 DNPRINTF(SWM_D_EVENT
,
11248 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
11250 if (e
->data
.data32
[0] & (1<<8)) /* x */
11251 X(win
) = e
->data
.data32
[1];
11252 if (e
->data
.data32
[0] & (1<<9)) /* y */
11253 Y(win
) = e
->data
.data32
[2];
11254 if (e
->data
.data32
[0] & (1<<10)) /* width */
11255 WIDTH(win
) = e
->data
.data32
[3];
11256 if (e
->data
.data32
[0] & (1<<11)) /* height */
11257 HEIGHT(win
) = e
->data
.data32
[4];
11259 update_window(win
);
11261 /* Notify no change was made. */
11262 config_win(win
, NULL
);
11263 /* TODO: Change stack sizes */
11265 } else if (e
->type
== ewmh
[_NET_RESTACK_WINDOW
].atom
) {
11266 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_RESTACK_WINDOW\n");
11267 vals
[0] = e
->data
.data32
[1]; /* Sibling window. */
11268 vals
[1] = e
->data
.data32
[2]; /* Stack mode detail. */
11270 if (win
->frame
!= XCB_WINDOW_NONE
)
11271 xcb_configure_window(conn
, win
->frame
,
11272 XCB_CONFIG_WINDOW_SIBLING
|
11273 XCB_CONFIG_WINDOW_STACK_MODE
, vals
);
11274 } else if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
11275 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
11276 ewmh_change_wm_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
11277 if (e
->data
.data32
[2])
11278 ewmh_change_wm_state(win
, e
->data
.data32
[2],
11279 e
->data
.data32
[0]);
11281 ewmh_update_wm_state(win
);
11283 } else if (e
->type
== ewmh
[_NET_WM_DESKTOP
].atom
) {
11284 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_DESKTOP\n");
11287 win_to_ws(win
, e
->data
.data32
[0], true);
11289 /* Stack source and destination ws, if mapped. */
11290 if (r
!= win
->ws
->r
) {
11296 load_float_geom(win
);
11309 int errcode
= xcb_connection_has_error(conn
);
11310 #ifdef XCB_CONN_ERROR
11313 case XCB_CONN_ERROR
:
11314 s
= "Socket error, pipe error or other stream error.";
11316 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
11317 s
= "Extension not supported.";
11319 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
11320 s
= "Insufficient memory.";
11322 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
11323 s
= "Request length was exceeded.";
11325 case XCB_CONN_CLOSED_PARSE_ERR
:
11326 s
= "Error parsing display string.";
11329 s
= "Unknown error.";
11332 errx(errcode
, "X CONNECTION ERROR: %s", s
);
11335 errx(errcode
, "X CONNECTION ERROR");
11342 int num_screens
, i
;
11343 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
11344 XCB_EVENT_MASK_ENTER_WINDOW
|
11345 XCB_EVENT_MASK_OWNER_GRAB_BUTTON
|
11346 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
11347 XCB_EVENT_MASK_ENTER_WINDOW
|
11348 XCB_EVENT_MASK_LEAVE_WINDOW
|
11349 XCB_EVENT_MASK_BUTTON_PRESS
|
11350 XCB_EVENT_MASK_BUTTON_RELEASE
|
11351 XCB_EVENT_MASK_KEY_PRESS
|
11352 XCB_EVENT_MASK_KEY_RELEASE
|
11353 XCB_EVENT_MASK_PROPERTY_CHANGE
;
11355 xcb_void_cookie_t wac
;
11356 xcb_generic_error_t
*error
;
11358 /* this causes an error if some other window manager is running */
11359 num_screens
= get_screen_count();
11360 for (i
= 0; i
< num_screens
; i
++) {
11361 if ((sc
= get_screen(i
)) == NULL
)
11362 errx(1, "ERROR: can't get screen %d.", i
);
11363 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: %#x\n",
11365 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
11366 XCB_CW_EVENT_MASK
, &val
);
11367 if ((error
= xcb_request_check(conn
, wac
))) {
11368 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
11369 error
->error_code
);
11379 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
11381 struct swm_region
*r
= NULL
, *n
;
11382 struct workspace
*ws
= NULL
;
11386 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
11387 s
->idx
, w
, h
, x
, y
);
11389 /* remove any conflicting regions */
11390 n
= TAILQ_FIRST(&s
->rl
);
11393 n
= TAILQ_NEXT(r
, entry
);
11394 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
11395 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
11396 if (r
->ws
->r
!= NULL
)
11397 r
->ws
->old_r
= r
->ws
->r
;
11400 xcb_destroy_window(conn
, r
->id
);
11401 r
->id
= XCB_WINDOW_NONE
;
11402 TAILQ_REMOVE(&s
->rl
, r
, entry
);
11403 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
11407 /* search old regions for one to reuse */
11409 /* size + location match */
11410 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11411 if (X(r
) == x
&& Y(r
) == y
&&
11412 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11416 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11417 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11421 TAILQ_REMOVE(&s
->orl
, r
, entry
);
11422 /* try to use old region's workspace */
11423 if (r
->ws
->r
== NULL
)
11426 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
11427 err(1, "new_region: calloc: failed to allocate memory "
11430 /* if we don't have a workspace already, find one */
11432 for (i
= 0; i
< workspace_limit
; i
++)
11433 if (s
->ws
[i
].r
== NULL
) {
11440 errx(1, "new_region: no free workspaces");
11442 if (ws
->state
== SWM_WS_STATE_HIDDEN
)
11443 ws
->state
= SWM_WS_STATE_MAPPING
;
11452 r
->ws_prior
= NULL
;
11455 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
11457 /* Invisible region window to detect pointer events on empty regions. */
11458 r
->id
= xcb_generate_id(conn
);
11459 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
11460 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
11462 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
11463 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
11464 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
11466 /* Make sure region input is at the bottom. */
11467 wa
[0] = XCB_STACK_MODE_BELOW
;
11468 xcb_configure_window(conn
, r
->id
, XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
11470 xcb_map_window(conn
, r
->id
);
11474 scan_randr(int idx
)
11476 #ifdef SWM_XRR_HAS_CRTC
11479 #endif /* SWM_XRR_HAS_CRTC */
11480 struct swm_region
*r
;
11481 struct ws_win
*win
;
11483 xcb_randr_get_screen_resources_current_cookie_t src
;
11484 xcb_randr_get_screen_resources_current_reply_t
*srr
;
11485 xcb_randr_get_crtc_info_cookie_t cic
;
11486 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
11487 xcb_randr_crtc_t
*crtc
;
11488 xcb_screen_t
*screen
;
11490 DNPRINTF(SWM_D_MISC
, "scan_randr: screen: %d\n", idx
);
11492 if ((screen
= get_screen(idx
)) == NULL
)
11493 errx(1, "ERROR: can't get screen %d.", idx
);
11495 num_screens
= get_screen_count();
11496 if (idx
>= num_screens
)
11497 errx(1, "scan_randr: invalid screen");
11499 /* remove any old regions */
11500 while ((r
= TAILQ_FIRST(&screens
[idx
].rl
)) != NULL
) {
11501 r
->ws
->old_r
= r
->ws
->r
= NULL
;
11503 xcb_destroy_window(conn
, r
->id
);
11504 r
->id
= XCB_WINDOW_NONE
;
11505 TAILQ_REMOVE(&screens
[idx
].rl
, r
, entry
);
11506 TAILQ_INSERT_TAIL(&screens
[idx
].orl
, r
, entry
);
11510 /* map virtual screens onto physical screens */
11511 #ifdef SWM_XRR_HAS_CRTC
11512 if (randr_support
) {
11513 src
= xcb_randr_get_screen_resources_current(conn
,
11514 screens
[idx
].root
);
11515 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
11518 new_region(&screens
[idx
], 0, 0,
11519 screen
->width_in_pixels
,
11520 screen
->height_in_pixels
);
11523 ncrtc
= srr
->num_crtcs
;
11525 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
11526 for (c
= 0; c
< ncrtc
; c
++) {
11527 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
11529 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
11532 if (cir
->num_outputs
== 0) {
11537 if (cir
->mode
== 0)
11538 new_region(&screens
[idx
], 0, 0,
11539 screen
->width_in_pixels
,
11540 screen
->height_in_pixels
);
11542 new_region(&screens
[idx
],
11543 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
11548 #endif /* SWM_XRR_HAS_CRTC */
11550 /* If detection failed, create a single region that spans the screen. */
11551 if (TAILQ_EMPTY(&screens
[idx
].rl
))
11552 new_region(&screens
[idx
], 0, 0, screen
->width_in_pixels
,
11553 screen
->height_in_pixels
);
11556 /* Cleanup unused previously visible workspaces. */
11557 TAILQ_FOREACH(r
, &screens
[idx
].orl
, entry
) {
11558 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
11560 r
->ws
->state
= SWM_WS_STATE_HIDDEN
;
11562 /* The screen shouldn't focus on an unused region. */
11563 if (screens
[idx
].r_focus
== r
)
11564 screens
[idx
].r_focus
= NULL
;
11567 DNPRINTF(SWM_D_MISC
, "scan_randr: done.\n");
11571 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
11573 struct swm_region
*r
;
11574 int i
, num_screens
;
11576 DNPRINTF(SWM_D_EVENT
, "screenchange: root: %#x\n", e
->root
);
11578 num_screens
= get_screen_count();
11579 /* silly event doesn't include the screen index */
11580 for (i
= 0; i
< num_screens
; i
++)
11581 if (screens
[i
].root
== e
->root
)
11583 if (i
>= num_screens
)
11584 errx(1, "screenchange: screen not found");
11586 /* brute force for now, just re-enumerate the regions */
11590 print_win_geom(e
->root
);
11592 /* add bars to all regions */
11593 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11596 /* Stack all regions. */
11597 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11600 /* Make sure a region has focus. */
11601 if (screens
[i
].r_focus
== NULL
) {
11602 r
= TAILQ_FIRST(&screens
[i
].rl
);
11609 /* Update workspace state and bar on all regions. */
11610 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11611 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
11619 struct swm_region
*r
= NULL
;
11620 xcb_query_tree_cookie_t qtc
;
11621 xcb_query_tree_reply_t
*qtr
;
11622 xcb_get_property_cookie_t pc
;
11623 xcb_get_property_reply_t
*pr
;
11624 xcb_window_t
*wins
= NULL
, trans
, *cwins
= NULL
;
11625 int i
, j
, k
, n
, no
, num_screens
;
11627 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
11628 num_screens
= get_screen_count();
11629 for (i
= 0; i
< num_screens
; i
++) {
11630 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
11631 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
11634 wins
= xcb_query_tree_children(qtr
);
11635 no
= xcb_query_tree_children_length(qtr
);
11637 /* Try to sort windows according to _NET_CLIENT_LIST. */
11638 pr
= xcb_get_property_reply(conn
, xcb_get_property(conn
, 0,
11639 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
11640 XCB_ATOM_WINDOW
, 0, UINT32_MAX
), NULL
);
11642 cwins
= xcb_get_property_value(pr
);
11643 n
= xcb_get_property_value_length(pr
) /
11644 sizeof(xcb_atom_t
);
11646 for (j
= 0; j
< n
; ++j
) {
11647 for (k
= j
; k
< no
; ++k
) {
11648 if (wins
[k
] == cwins
[j
]) {
11649 /* Swap wins j and k. */
11651 wins
[j
] = cwins
[j
];
11659 /* Manage top-level windows first, then transients. */
11660 /* TODO: allow transients to be managed before leader. */
11661 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top-level windows.\n");
11662 for (j
= 0; j
< no
; j
++) {
11663 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11664 if (r
->id
== wins
[j
]) {
11665 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11666 "skip %#x; region input window.\n",
11669 } else if (r
->bar
->id
== wins
[j
]) {
11670 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11671 "skip %#x; region bar.\n",
11680 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11681 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11683 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
11684 "is transient for %#x.\n", wins
[j
], trans
);
11688 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11691 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
11692 for (j
= 0; j
< no
; j
++) {
11693 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11694 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11696 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11700 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
11704 setup_screens(void)
11706 int i
, j
, k
, num_screens
;
11707 struct workspace
*ws
;
11708 uint32_t gcv
[1], wa
[1];
11709 const xcb_query_extension_reply_t
*qep
;
11710 xcb_screen_t
*screen
;
11711 xcb_randr_query_version_cookie_t c
;
11712 xcb_randr_query_version_reply_t
*r
;
11714 num_screens
= get_screen_count();
11715 if ((screens
= calloc(num_screens
, sizeof(struct swm_screen
))) == NULL
)
11716 err(1, "setup_screens: calloc: failed to allocate memory for "
11719 /* Initial RandR setup. */
11720 randr_support
= false;
11721 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
11722 if (qep
->present
) {
11723 c
= xcb_randr_query_version(conn
, 1, 1);
11724 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
11726 if (r
->major_version
>= 1) {
11727 randr_support
= true;
11728 randr_eventbase
= qep
->first_event
;
11734 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
11736 /* map physical screens */
11737 for (i
= 0; i
< num_screens
; i
++) {
11738 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
11739 screens
[i
].idx
= i
;
11740 screens
[i
].r_focus
= NULL
;
11742 TAILQ_INIT(&screens
[i
].rl
);
11743 TAILQ_INIT(&screens
[i
].orl
);
11744 if ((screen
= get_screen(i
)) == NULL
)
11745 errx(1, "ERROR: can't get screen %d.", i
);
11746 screens
[i
].root
= screen
->root
;
11748 /* set default colors */
11749 setscreencolor("red", i
, SWM_S_COLOR_FOCUS
);
11750 setscreencolor("rgb:88/88/88", i
, SWM_S_COLOR_UNFOCUS
);
11751 setscreencolor("rgb:00/80/80", i
, SWM_S_COLOR_BAR_BORDER
);
11752 setscreencolor("rgb:00/40/40", i
,
11753 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
11754 setscreencolor("black", i
, SWM_S_COLOR_BAR
);
11755 setscreencolor("rgb:a0/a0/a0", i
, SWM_S_COLOR_BAR_FONT
);
11756 setscreencolor("red", i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
11757 setscreencolor("rgb:88/88/88", i
,
11758 SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
11760 /* create graphics context on screen */
11761 screens
[i
].bar_gc
= xcb_generate_id(conn
);
11763 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
11764 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
11766 /* set default cursor */
11767 xcb_change_window_attributes(conn
, screens
[i
].root
,
11768 XCB_CW_CURSOR
, wa
);
11770 /* init all workspaces */
11771 /* XXX these should be dynamically allocated too */
11772 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
11773 ws
= &screens
[i
].ws
[j
];
11776 ws
->bar_enabled
= true;
11778 ws
->focus_prev
= NULL
;
11779 ws
->focus_pending
= NULL
;
11783 ws
->state
= SWM_WS_STATE_HIDDEN
;
11784 TAILQ_INIT(&ws
->stack
);
11785 TAILQ_INIT(&ws
->winlist
);
11786 TAILQ_INIT(&ws
->unmanagedlist
);
11788 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
11789 if (layouts
[k
].l_config
!= NULL
)
11790 layouts
[k
].l_config(ws
,
11791 SWM_ARG_ID_STACKINIT
);
11792 ws
->cur_layout
= &layouts
[0];
11793 ws
->cur_layout
->l_string(ws
);
11799 xcb_randr_select_input(conn
, screens
[i
].root
,
11800 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
11805 setup_globals(void)
11807 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
11808 err(1, "setup_globals: strdup: failed to allocate memory.");
11810 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
11811 err(1, "setup_globals: strdup: failed to allocate memory.");
11813 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
11814 errx(1, "unable to allocate key symbols");
11816 a_state
= get_atom_from_string("WM_STATE");
11817 a_prot
= get_atom_from_string("WM_PROTOCOLS");
11818 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
11819 a_net_frame_extents
= get_atom_from_string("_NET_FRAME_EXTENTS");
11820 a_net_supported
= get_atom_from_string("_NET_SUPPORTED");
11821 a_net_wm_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
11822 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
11823 a_utf8_string
= get_atom_from_string("UTF8_STRING");
11824 a_swm_ws
= get_atom_from_string("_SWM_WS");
11828 shutdown_cleanup(void)
11830 struct swm_region
*r
;
11832 struct workspace
*ws
;
11833 int i
, num_screens
;
11835 /* disable alarm because the following code may not be interrupted */
11837 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
11838 err(1, "can't disable alarm");
11851 num_screens
= get_screen_count();
11852 for (i
= 0; i
< num_screens
; ++i
) {
11855 DNPRINTF(SWM_D_FOCUS
, "shutdown_cleanup: set_input_focus: "
11856 "%#x, revert-to: root, time: 0\n", screens
[i
].root
);
11857 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11858 screens
[i
].root
, XCB_CURRENT_TIME
);
11860 if (screens
[i
].bar_gc
!= XCB_NONE
)
11861 xcb_free_gc(conn
, screens
[i
].bar_gc
);
11862 if (!bar_font_legacy
) {
11863 XftColorFree(display
, DefaultVisual(display
, i
),
11864 DefaultColormap(display
, i
), &bar_font_color
);
11865 XftColorFree(display
, DefaultVisual(display
, i
),
11866 DefaultColormap(display
, i
), &search_font_color
);
11869 for (j
= 0; j
< SWM_S_COLOR_MAX
; ++j
) {
11870 free(screens
[i
].c
[j
].name
);
11873 /* Free window memory. */
11874 for (j
= 0; j
< SWM_WS_MAX
; ++j
) {
11875 ws
= &screens
[i
].ws
[j
];
11878 while ((w
= TAILQ_FIRST(&ws
->winlist
)) != NULL
) {
11879 TAILQ_REMOVE(&ws
->winlist
, w
, entry
);
11883 while ((w
= TAILQ_FIRST(&ws
->unmanagedlist
)) != NULL
) {
11884 TAILQ_REMOVE(&ws
->unmanagedlist
, w
, entry
);
11889 /* Free region memory. */
11890 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
11891 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
11896 while ((r
= TAILQ_FIRST(&screens
[i
].orl
)) != NULL
) {
11897 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
11906 free(clock_format
);
11907 free(startup_exception
);
11910 XFreeFontSet(display
, bar_fs
);
11912 XftFontClose(display
, bar_font
);
11914 xcb_key_symbols_free(syms
);
11916 xcb_aux_sync(conn
);
11917 xcb_disconnect(conn
);
11921 event_error(xcb_generic_error_t
*e
)
11925 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
11926 "resource_id: %u, minor_code: %u\n",
11927 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
11928 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
11929 e
->sequence
, e
->resource_id
, e
->minor_code
);
11933 event_handle(xcb_generic_event_t
*evt
)
11935 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
11937 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
11938 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
11939 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
11942 #define EVENT(type, callback) case type: callback((void *)evt); return
11943 EVENT(0, event_error
);
11944 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
11945 EVENT(XCB_BUTTON_RELEASE
, buttonrelease
);
11946 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
11947 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
11948 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
11949 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
11950 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
11951 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
11952 /*EVENT(XCB_CREATE_NOTIFY, );*/
11953 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
11954 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
11955 EVENT(XCB_EXPOSE
, expose
);
11956 EVENT(XCB_FOCUS_IN
, focusin
);
11958 EVENT(XCB_FOCUS_OUT
, focusout
);
11960 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
11961 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
11962 EVENT(XCB_KEY_PRESS
, keypress
);
11963 EVENT(XCB_KEY_RELEASE
, keyrelease
);
11964 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
11966 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
11968 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
11969 EVENT(XCB_MAP_REQUEST
, maprequest
);
11970 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
11971 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
11972 /*EVENT(XCB_NO_EXPOSURE, );*/
11973 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
11974 EVENT(XCB_REPARENT_NOTIFY
, reparentnotify
);
11975 /*EVENT(XCB_RESIZE_REQUEST, );*/
11976 /*EVENT(XCB_SELECTION_CLEAR, );*/
11977 /*EVENT(XCB_SELECTION_NOTIFY, );*/
11978 /*EVENT(XCB_SELECTION_REQUEST, );*/
11979 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
11980 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
11983 if (type
- randr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
11984 screenchange((void *)evt
);
11988 main(int argc
, char *argv
[])
11990 struct pollfd pfd
[2];
11991 struct sigaction sact
;
11993 struct passwd
*pwd
;
11994 struct swm_region
*r
;
11995 xcb_generic_event_t
*evt
;
11996 int xfd
, i
, num_screens
, num_readable
;
11997 char conf
[PATH_MAX
], *cfile
= NULL
;
11998 bool stdin_ready
= false, startup
= true;
12000 /* suppress unused warning since var is needed */
12004 time_started
= time(NULL
);
12008 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
12009 if (setlocale(LC_CTYPE
, "") == NULL
|| setlocale(LC_TIME
, "") == NULL
)
12010 warnx("no locale support");
12012 /* handle some signals */
12013 bzero(&sact
, sizeof(sact
));
12014 sigemptyset(&sact
.sa_mask
);
12016 sact
.sa_handler
= sighdlr
;
12017 sigaction(SIGINT
, &sact
, NULL
);
12018 sigaction(SIGQUIT
, &sact
, NULL
);
12019 sigaction(SIGTERM
, &sact
, NULL
);
12020 sigaction(SIGHUP
, &sact
, NULL
);
12022 sact
.sa_handler
= sighdlr
;
12023 sact
.sa_flags
= SA_NOCLDSTOP
;
12024 sigaction(SIGCHLD
, &sact
, NULL
);
12026 if ((display
= XOpenDisplay(0)) == NULL
)
12027 errx(1, "can not open display");
12029 conn
= XGetXCBConnection(display
);
12030 if (xcb_connection_has_error(conn
))
12031 errx(1, "can not get XCB connection");
12033 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
12035 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
12036 xfd
= xcb_get_file_descriptor(conn
);
12038 /* look for local and global conf file */
12039 pwd
= getpwuid(getuid());
12041 errx(1, "invalid user: %d", getuid());
12043 xcb_grab_server(conn
);
12044 xcb_aux_sync(conn
);
12046 /* flush all events */
12047 while ((evt
= get_next_event(false))) {
12048 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
12054 errx(1, "another window manager is currently running");
12056 /* Load Xcursors and/or cursorfont glyph cursors. */
12059 xcb_aux_sync(conn
);
12064 setup_keybindings();
12065 setup_btnbindings();
12070 for (i
= 0; ; i
++) {
12075 snprintf(conf
, sizeof conf
, "%s/.%s",
12076 pwd
->pw_dir
, SWM_CONF_FILE
);
12080 snprintf(conf
, sizeof conf
, "/etc/%s",
12085 snprintf(conf
, sizeof conf
, "%s/.%s",
12086 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
12089 /* global compat */
12090 snprintf(conf
, sizeof conf
, "/etc/%s",
12091 SWM_CONF_FILE_OLD
);
12097 if (strlen(conf
) && stat(conf
, &sb
) != -1)
12098 if (S_ISREG(sb
.st_mode
)) {
12105 /* load conf (if any) */
12107 conf_load(cfile
, SWM_CONF_DEFAULT
);
12111 if (getenv("SWM_STARTED") == NULL
)
12112 setenv("SWM_STARTED", "YES", 1);
12114 /* setup all bars */
12115 num_screens
= get_screen_count();
12116 for (i
= 0; i
< num_screens
; i
++)
12117 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12120 /* Manage existing windows. */
12126 /* Stack all regions to trigger mapping. */
12127 for (i
= 0; i
< num_screens
; i
++)
12128 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12131 xcb_ungrab_server(conn
);
12134 /* Update state and bar of each newly mapped workspace. */
12135 for (i
= 0; i
< num_screens
; i
++)
12136 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
12137 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
12141 memset(&pfd
, 0, sizeof(pfd
));
12143 pfd
[0].events
= POLLIN
;
12144 pfd
[1].fd
= STDIN_FILENO
;
12145 pfd
[1].events
= POLLIN
;
12148 while ((evt
= get_next_event(false))) {
12155 /* If just (re)started, set default focus if needed. */
12159 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
12160 r
= TAILQ_FIRST(&screens
[0].rl
);
12172 num_readable
= poll(pfd
, bar_extra
? 2 : 1, 1000);
12173 if (num_readable
== -1) {
12174 DNPRINTF(SWM_D_MISC
, "poll failed: %s",
12176 } else if (num_readable
> 0 && bar_extra
&&
12177 pfd
[1].revents
& POLLIN
) {
12178 stdin_ready
= true;
12182 restart(NULL
, NULL
, NULL
);
12188 stdin_ready
= false;
12189 bar_extra_update();
12192 /* Need to ensure the bar(s) are always updated. */
12193 for (i
= 0; i
< num_screens
; i
++)
12194 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12200 shutdown_cleanup();