2 * Copyright (c) 2009-2015 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2016 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 * Copyright (c) 2012-2015 David Hill <dhill@mindcry.org>
12 * Copyright (c) 2014-2015 Yuri D'Elia <yuri.delia@eurac.edu>
14 * Permission to use, copy, modify, and distribute this software for any
15 * purpose with or without fee is hereby granted, provided that the above
16 * copyright notice and this permission notice appear in all copies.
18 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
19 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
21 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
23 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
24 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 #include <sys/types.h>
35 #include <sys/queue.h>
37 #include <sys/param.h>
38 #include <sys/select.h>
39 #if defined(__linux__)
41 #elif defined(__OpenBSD__)
43 #elif defined(__FreeBSD__)
67 #include <X11/cursorfont.h>
68 #include <X11/extensions/Xrandr.h>
69 #include <X11/Xcursor/Xcursor.h>
70 #include <X11/Xft/Xft.h>
71 #include <X11/Xlib-xcb.h>
73 #include <xcb/xcb_atom.h>
74 #include <xcb/xcb_aux.h>
75 #include <xcb/xcb_event.h>
76 #include <xcb/xcb_icccm.h>
77 #include <xcb/xcb_keysyms.h>
78 #include <xcb/xtest.h>
79 #include <xcb/randr.h>
87 #ifdef SPECTRWM_BUILDSTR
88 static const char *buildstr
= SPECTRWM_BUILDSTR
;
90 static const char *buildstr
= SPECTRWM_VERSION
;
93 #if !defined(__CYGWIN__) /* cygwin chokes on randr stuff */
95 # error RandR versions less than 1.0 are not supported
100 # define SWM_XRR_HAS_CRTC
103 #endif /* __CYGWIN__ */
105 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
106 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
107 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
108 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
109 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
110 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
111 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
112 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
113 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
114 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
115 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
116 #define xcb_icccm_get_wm_class xcb_get_wm_class
117 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
118 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
119 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
120 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
121 #define xcb_icccm_wm_hints_get_urgency xcb_wm_hints_get_urgency
122 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
123 #define xcb_icccm_get_wm_name xcb_get_wm_name
124 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
125 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
126 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
127 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
128 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
129 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
130 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
131 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
132 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
133 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
136 /*#define SWM_DEBUG*/
138 #define DPRINTF(x...) do { \
140 fprintf(stderr, x); \
142 #define DNPRINTF(n,x...) do { \
143 if (swm_debug & n) { \
144 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
145 fprintf(stderr, x); \
148 #define SWM_D_MISC 0x0001
149 #define SWM_D_EVENT 0x0002
150 #define SWM_D_WS 0x0004
151 #define SWM_D_FOCUS 0x0008
152 #define SWM_D_MOVE 0x0010
153 #define SWM_D_STACK 0x0020
154 #define SWM_D_MOUSE 0x0040
155 #define SWM_D_PROP 0x0080
156 #define SWM_D_CLASS 0x0100
157 #define SWM_D_KEY 0x0200
158 #define SWM_D_QUIRK 0x0400
159 #define SWM_D_SPAWN 0x0800
160 #define SWM_D_EVENTQ 0x1000
161 #define SWM_D_CONF 0x2000
162 #define SWM_D_BAR 0x4000
163 #define SWM_D_INIT 0x8000
165 uint32_t swm_debug
= 0
184 #define DPRINTF(x...)
185 #define DNPRINTF(n,x...)
188 #define SWM_EWMH_ACTION_COUNT_MAX (8)
189 #define EWMH_F_FULLSCREEN (0x001)
190 #define EWMH_F_ABOVE (0x002)
191 #define EWMH_F_HIDDEN (0x004)
192 #define EWMH_F_MAXIMIZED_VERT (0x008)
193 #define EWMH_F_MAXIMIZED_HORZ (0x010)
194 #define EWMH_F_SKIP_PAGER (0x020)
195 #define EWMH_F_SKIP_TASKBAR (0x040)
196 #define SWM_F_MANUAL (0x080)
198 #define EWMH_F_MAXIMIZED (EWMH_F_MAXIMIZED_VERT | EWMH_F_MAXIMIZED_HORZ)
200 /* convert 8-bit to 16-bit */
201 #define RGB_8_TO_16(col) (((col) << 8) + (col))
203 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
204 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
205 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
206 xrc.blue = RGB_8_TO_16((px) & 0xff); \
209 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
210 #define MODKEY XCB_MOD_MASK_1
211 #define ANYMOD XCB_MOD_MASK_ANY
212 #define CLEANMASK(mask) ((mask) & (XCB_KEY_BUT_MASK_SHIFT | \
213 XCB_KEY_BUT_MASK_CONTROL | XCB_KEY_BUT_MASK_MOD_1 | \
214 XCB_KEY_BUT_MASK_MOD_2 | XCB_KEY_BUT_MASK_MOD_3 | \
215 XCB_KEY_BUT_MASK_MOD_4 | XCB_KEY_BUT_MASK_MOD_5) & ~(numlockmask))
216 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
217 XCB_EVENT_MASK_BUTTON_RELEASE)
218 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
219 #define SWM_PROPLEN (16)
220 #define SWM_FUNCNAME_LEN (32)
221 #define SWM_QUIRK_LEN (64)
222 #define X(r) ((r)->g.x)
223 #define Y(r) ((r)->g.y)
224 #define WIDTH(r) ((r)->g.w)
225 #define HEIGHT(r) ((r)->g.h)
226 #define MAX_X(r) ((r)->g.x + (r)->g.w)
227 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
228 #define SH_MIN(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
229 #define SH_MIN_W(w) ((w)->sh.min_width)
230 #define SH_MIN_H(w) ((w)->sh.min_height)
231 #define SH_MAX(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
232 #define SH_MAX_W(w) ((w)->sh.max_width)
233 #define SH_MAX_H(w) ((w)->sh.max_height)
234 #define SH_INC(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)
235 #define SH_INC_W(w) ((w)->sh.width_inc)
236 #define SH_INC_H(w) ((w)->sh.height_inc)
237 #define SWM_MAX_FONT_STEPS (3)
238 #define WINID(w) ((w) ? (w)->id : XCB_WINDOW_NONE)
239 #define ACCEPTS_FOCUS(w) (!((w)->hints.flags & XCB_ICCCM_WM_HINT_INPUT) \
240 || ((w)->hints.input))
241 #define WS_FOCUSED(ws) ((ws)->r && (ws)->r->s->r_focus == (ws)->r)
242 #define YESNO(x) ((x) ? "yes" : "no")
243 #define ICONIC(w) ((w)->ewmh_flags & EWMH_F_HIDDEN)
244 #define ABOVE(w) ((w)->ewmh_flags & EWMH_F_ABOVE)
245 #define FULLSCREEN(w) ((w)->ewmh_flags & EWMH_F_FULLSCREEN)
246 #define MAXIMIZED_VERT(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_VERT)
247 #define MAXIMIZED_HORZ(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_HORZ)
248 #define MAXIMIZED(w) (MAXIMIZED_VERT(w) || MAXIMIZED_HORZ(w))
249 #define MANUAL(w) ((w)->ewmh_flags & SWM_F_MANUAL)
250 #define TRANS(w) ((w)->transient != XCB_WINDOW_NONE)
251 #define FLOATING(w) (ABOVE(w) || TRANS(w) || FULLSCREEN(w) || \
254 /* Constrain Window flags */
255 #define SWM_CW_RESIZABLE (0x01)
256 #define SWM_CW_SOFTBOUNDARY (0x02)
257 #define SWM_CW_HARDBOUNDARY (0x04)
258 #define SWM_CW_RIGHT (0x10)
259 #define SWM_CW_LEFT (0x20)
260 #define SWM_CW_BOTTOM (0x40)
261 #define SWM_CW_TOP (0x80)
262 #define SWM_CW_ALLSIDES (0xf0)
264 #define SWM_FOCUS_DEFAULT (0)
265 #define SWM_FOCUS_FOLLOW (1)
266 #define SWM_FOCUS_MANUAL (2)
268 #define SWM_CK_ALL (0xf)
269 #define SWM_CK_FOCUS (0x1)
270 #define SWM_CK_POINTER (0x2)
271 #define SWM_CK_FALLBACK (0x4)
272 #define SWM_CK_REGION (0x8)
274 #define SWM_CONF_DEFAULT (0)
275 #define SWM_CONF_KEYMAPPING (1)
278 #define SWM_LIB "/usr/local/lib/libswmhack.so"
285 xcb_atom_t a_net_frame_extents
;
286 xcb_atom_t a_net_wm_check
;
287 xcb_atom_t a_net_supported
;
288 xcb_atom_t a_takefocus
;
289 xcb_atom_t a_utf8_string
;
291 volatile sig_atomic_t running
= 1;
292 volatile sig_atomic_t restart_wm
= 0;
293 xcb_timestamp_t last_event_time
= 0;
297 unsigned int numlockmask
= 0;
300 xcb_connection_t
*conn
;
301 xcb_key_symbols_t
*syms
;
303 int boundary_width
= 50;
304 bool cycle_empty
= false;
305 bool cycle_visible
= false;
307 int font_adjusted
= 0;
308 uint16_t mod_key
= MODKEY
;
309 bool warp_focus
= false;
310 bool warp_pointer
= false;
311 bool workspace_clamp
= false;
314 struct swm_region
*search_r
;
315 int select_list_pipe
[2];
316 int select_resp_pipe
[2];
318 volatile sig_atomic_t search_resp
;
319 int search_resp_action
;
321 struct search_window
{
322 TAILQ_ENTRY(search_window
) entry
;
326 xcb_window_t indicator
;
328 TAILQ_HEAD(search_winlist
, search_window
);
329 struct search_winlist search_wl
;
334 SWM_SEARCH_UNICONIFY
,
335 SWM_SEARCH_NAME_WORKSPACE
,
336 SWM_SEARCH_SEARCH_WORKSPACE
,
337 SWM_SEARCH_SEARCH_WINDOW
340 #define SWM_STACK_TOP (0)
341 #define SWM_STACK_BOTTOM (1)
342 #define SWM_STACK_ABOVE (2)
343 #define SWM_STACK_BELOW (3)
346 double dialog_ratio
= 0.6;
348 #define SWM_BAR_MAX (256)
349 #define SWM_BAR_JUSTIFY_LEFT (0)
350 #define SWM_BAR_JUSTIFY_CENTER (1)
351 #define SWM_BAR_JUSTIFY_RIGHT (2)
352 #define SWM_BAR_OFFSET (4)
353 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
354 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
355 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
356 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
357 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
359 #ifdef X_HAVE_UTF8_STRING
360 #define DRAWSTRING(x...) Xutf8DrawString(x)
362 #define DRAWSTRING(x...) XmbDrawString(x)
365 char *bar_argv
[] = { NULL
, NULL
};
367 char bar_ext
[SWM_BAR_MAX
];
368 char bar_ext_buf
[SWM_BAR_MAX
];
369 char bar_vertext
[SWM_BAR_MAX
];
370 bool bar_version
= false;
371 bool bar_enabled
= true;
372 int bar_border_width
= 1;
373 bool bar_at_bottom
= false;
374 bool bar_extra
= false;
376 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
377 char *bar_format
= NULL
;
378 bool stack_enabled
= true;
379 bool clock_enabled
= true;
380 bool iconic_enabled
= false;
381 bool maximize_hide_bar
= false;
382 bool urgent_enabled
= false;
383 bool urgent_collapse
= false;
384 char *clock_format
= NULL
;
385 bool window_class_enabled
= false;
386 bool window_instance_enabled
= false;
387 bool window_name_enabled
= false;
388 int focus_mode
= SWM_FOCUS_DEFAULT
;
389 int focus_close
= SWM_STACK_BELOW
;
390 bool focus_close_wrap
= true;
391 int focus_default
= SWM_STACK_TOP
;
392 int spawn_position
= SWM_STACK_TOP
;
393 bool disable_border
= false;
394 int border_width
= 1;
395 int region_padding
= 0;
397 bool java_workaround
= true;
398 bool verbose_layout
= false;
404 XFontSet bar_fs
= NULL
;
405 XFontSetExtents
*bar_fs_extents
;
406 XftFont
*bar_font
= NULL
;
407 bool bar_font_legacy
= true;
408 char *bar_fonts
= NULL
;
409 XftColor bar_font_color
;
410 XftColor search_font_color
;
411 char *startup_exception
= NULL
;
412 unsigned int nr_exceptions
= 0;
414 /* layout manager data */
415 struct swm_geometry
{
428 struct swm_geometry g
;
429 struct swm_region
*r
; /* Associated region. */
432 /* virtual "screens" */
434 TAILQ_ENTRY(swm_region
) entry
;
436 struct swm_geometry g
;
437 struct workspace
*ws
; /* current workspace on this region */
438 struct workspace
*ws_prior
; /* prior workspace on this region */
439 struct swm_screen
*s
; /* screen idx */
442 TAILQ_HEAD(swm_region_list
, swm_region
);
445 SWM_WIN_STATE_REPARENTING
,
446 SWM_WIN_STATE_REPARENTED
,
447 SWM_WIN_STATE_UNPARENTING
,
448 SWM_WIN_STATE_UNPARENTED
,
452 TAILQ_ENTRY(ws_win
) entry
;
453 TAILQ_ENTRY(ws_win
) stack_entry
;
456 xcb_window_t transient
;
457 struct ws_win
*focus_child
; /* focus on child transient */
458 struct swm_geometry g
; /* current geometry */
459 struct swm_geometry g_prev
; /* prev configured geometry */
460 struct swm_geometry g_float
; /* region coordinates */
461 bool g_floatvalid
; /* g_float geometry validity */
466 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
473 struct workspace
*ws
; /* always valid */
474 struct swm_screen
*s
; /* always valid, never changes */
476 xcb_icccm_get_wm_class_reply_t ch
;
477 xcb_icccm_wm_hints_t hints
;
482 TAILQ_HEAD(ws_win_list
, ws_win
);
483 TAILQ_HEAD(ws_win_stack
, ws_win
);
487 TAILQ_ENTRY(pid_e
) entry
;
491 TAILQ_HEAD(pid_list
, pid_e
);
492 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
494 /* layout handlers */
495 void stack(struct swm_region
*);
496 void vertical_config(struct workspace
*, int);
497 void vertical_stack(struct workspace
*, struct swm_geometry
*);
498 void horizontal_config(struct workspace
*, int);
499 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
500 void max_stack(struct workspace
*, struct swm_geometry
*);
501 void plain_stacker(struct workspace
*);
502 void fancy_stacker(struct workspace
*);
505 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
506 void (*l_config
)(struct workspace
*, int);
508 #define SWM_L_FOCUSPREV (1<<0)
509 #define SWM_L_MAPONFOCUS (1<<1)
510 void (*l_string
)(struct workspace
*);
512 /* stack, configure */
513 { vertical_stack
, vertical_config
, 0, plain_stacker
},
514 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
516 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
517 { NULL
, NULL
, 0, NULL
},
520 /* position of max_stack mode in the layouts array, index into layouts! */
521 #define SWM_V_STACK (0)
522 #define SWM_H_STACK (1)
523 #define SWM_MAX_STACK (2)
525 #define SWM_H_SLICE (32)
526 #define SWM_V_SLICE (32)
528 /* define work spaces */
530 int idx
; /* workspace index */
531 char *name
; /* workspace name */
532 bool always_raise
; /* raise windows on focus */
533 bool bar_enabled
; /* bar visibility */
534 struct layout
*cur_layout
; /* current layout handlers */
535 struct ws_win
*focus
; /* may be NULL */
536 struct ws_win
*focus_prev
; /* may be NULL */
537 struct ws_win
*focus_pending
; /* may be NULL */
538 struct ws_win
*focus_raise
; /* may be NULL */
539 struct swm_region
*r
; /* may be NULL */
540 struct swm_region
*old_r
; /* may be NULL */
541 struct ws_win_list winlist
; /* list of windows in ws */
542 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
543 struct ws_win_stack stack
; /* stacking order */
544 int state
; /* mapping state */
545 char stacker
[10]; /* display stacker and layout */
549 int horizontal_msize
;
551 int horizontal_stacks
;
552 bool horizontal_flip
;
562 SWM_WS_STATE_MAPPING
,
568 SWM_S_COLOR_BAR_BORDER
,
569 SWM_S_COLOR_BAR_BORDER_UNFOCUS
,
570 SWM_S_COLOR_BAR_FONT
,
572 SWM_S_COLOR_FOCUS_MAXIMIZED
,
574 SWM_S_COLOR_UNFOCUS_MAXIMIZED
,
578 /* physical screen mapping */
579 #define SWM_WS_MAX (22) /* hard limit */
580 int workspace_limit
= 10; /* soft limit */
583 int idx
; /* screen index */
584 struct swm_region_list rl
; /* list of regions on this screen */
585 struct swm_region_list orl
; /* list of old regions */
587 struct workspace ws
[SWM_WS_MAX
];
588 struct swm_region
*r_focus
;
595 } c
[SWM_S_COLOR_MAX
];
597 xcb_gcontext_t bar_gc
;
600 struct swm_screen
*screens
;
602 /* args to functions */
605 #define SWM_ARG_ID_FOCUSNEXT (0)
606 #define SWM_ARG_ID_FOCUSPREV (1)
607 #define SWM_ARG_ID_FOCUSMAIN (2)
608 #define SWM_ARG_ID_FOCUSURGENT (3)
609 #define SWM_ARG_ID_SWAPNEXT (10)
610 #define SWM_ARG_ID_SWAPPREV (11)
611 #define SWM_ARG_ID_SWAPMAIN (12)
612 #define SWM_ARG_ID_MOVELAST (13)
613 #define SWM_ARG_ID_MASTERSHRINK (20)
614 #define SWM_ARG_ID_MASTERGROW (21)
615 #define SWM_ARG_ID_MASTERADD (22)
616 #define SWM_ARG_ID_MASTERDEL (23)
617 #define SWM_ARG_ID_FLIPLAYOUT (24)
618 #define SWM_ARG_ID_STACKRESET (30)
619 #define SWM_ARG_ID_STACKINIT (31)
620 #define SWM_ARG_ID_STACKBALANCE (32)
621 #define SWM_ARG_ID_CYCLEWS_UP (40)
622 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
623 #define SWM_ARG_ID_CYCLERG_UP (42)
624 #define SWM_ARG_ID_CYCLERG_DOWN (43)
625 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
626 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
627 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
628 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
629 #define SWM_ARG_ID_STACKINC (50)
630 #define SWM_ARG_ID_STACKDEC (51)
631 #define SWM_ARG_ID_DONTCENTER (70)
632 #define SWM_ARG_ID_CENTER (71)
633 #define SWM_ARG_ID_KILLWINDOW (80)
634 #define SWM_ARG_ID_DELETEWINDOW (81)
635 #define SWM_ARG_ID_WIDTHGROW (90)
636 #define SWM_ARG_ID_WIDTHSHRINK (91)
637 #define SWM_ARG_ID_HEIGHTGROW (92)
638 #define SWM_ARG_ID_HEIGHTSHRINK (93)
639 #define SWM_ARG_ID_MOVEUP (100)
640 #define SWM_ARG_ID_MOVEDOWN (101)
641 #define SWM_ARG_ID_MOVELEFT (102)
642 #define SWM_ARG_ID_MOVERIGHT (103)
643 #define SWM_ARG_ID_BAR_TOGGLE (110)
644 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
645 #define SWM_ARG_ID_CYCLERG_MOVE_UP (112)
646 #define SWM_ARG_ID_CYCLERG_MOVE_DOWN (113)
652 TAILQ_ENTRY(quirk
) entry
;
653 char *class; /* WM_CLASS:class */
654 char *instance
; /* WM_CLASS:instance */
655 char *name
; /* WM_NAME */
657 regex_t regex_instance
;
660 int ws
; /* Initial workspace. */
661 #define SWM_Q_FLOAT (1<<0) /* Float this window. */
662 #define SWM_Q_TRANSSZ (1<<1) /* Transient window size too small. */
663 #define SWM_Q_ANYWHERE (1<<2) /* Don't position this window */
664 #define SWM_Q_XTERM_FONTADJ (1<<3) /* Adjust xterm fonts when resizing. */
665 #define SWM_Q_FULLSCREEN (1<<4) /* Remove border when fullscreen. */
666 #define SWM_Q_FOCUSPREV (1<<5) /* Focus on caller. */
667 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
668 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
669 #define SWM_Q_OBEYAPPFOCUSREQ (1<<8) /* Focus when applications ask. */
670 #define SWM_Q_IGNOREPID (1<<9) /* Ignore PID when determining ws. */
671 #define SWM_Q_IGNORESPAWNWS (1<<10) /* Ignore _SWM_WS when managing win. */
672 #define SWM_Q_NOFOCUSCYCLE (1<<11) /* Remove from normal focus cycle. */
673 #define SWM_Q_MINIMALBORDER (1<<12) /* No border when floating/unfocused. */
675 TAILQ_HEAD(quirk_list
, quirk
);
676 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
679 * Supported EWMH hints should be added to
680 * both the enum and the ewmh array
686 _NET_CURRENT_DESKTOP
,
687 _NET_DESKTOP_GEOMETRY
,
689 _NET_DESKTOP_VIEWPORT
,
690 _NET_MOVERESIZE_WINDOW
,
691 _NET_NUMBER_OF_DESKTOPS
,
692 _NET_REQUEST_FRAME_EXTENTS
,
694 _NET_WM_ACTION_ABOVE
,
695 _NET_WM_ACTION_CLOSE
,
696 _NET_WM_ACTION_FULLSCREEN
,
698 _NET_WM_ACTION_RESIZE
,
699 _NET_WM_ALLOWED_ACTIONS
,
701 _NET_WM_FULL_PLACEMENT
,
705 _NET_WM_STATE_FULLSCREEN
,
706 _NET_WM_STATE_HIDDEN
,
707 _NET_WM_STATE_MAXIMIZED_VERT
,
708 _NET_WM_STATE_MAXIMIZED_HORZ
,
709 _NET_WM_STATE_SKIP_PAGER
,
710 _NET_WM_STATE_SKIP_TASKBAR
,
712 _NET_WM_WINDOW_TYPE_DIALOG
,
713 _NET_WM_WINDOW_TYPE_DOCK
,
714 _NET_WM_WINDOW_TYPE_NORMAL
,
715 _NET_WM_WINDOW_TYPE_SPLASH
,
716 _NET_WM_WINDOW_TYPE_TOOLBAR
,
717 _NET_WM_WINDOW_TYPE_UTILITY
,
718 _SWM_WM_STATE_MANUAL
,
725 } ewmh
[SWM_EWMH_HINT_MAX
] = {
726 /* must be in same order as in the enum */
727 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
728 {"_NET_CLIENT_LIST", XCB_ATOM_NONE
},
729 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
730 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE
},
731 {"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE
},
732 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE
},
733 {"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE
},
734 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
735 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE
},
736 {"_NET_REQUEST_FRAME_EXTENTS", XCB_ATOM_NONE
},
737 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE
},
738 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
739 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
740 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
741 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
742 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
743 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
744 {"_NET_WM_DESKTOP", XCB_ATOM_NONE
},
745 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE
},
746 {"_NET_WM_NAME", XCB_ATOM_NONE
},
747 {"_NET_WM_STATE", XCB_ATOM_NONE
},
748 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
749 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
750 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
751 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
752 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
753 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
754 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
755 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
756 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
757 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
758 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
759 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
760 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
761 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
762 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
765 /* EWMH source type */
767 EWMH_SOURCE_TYPE_NONE
= 0,
768 EWMH_SOURCE_TYPE_NORMAL
= 1,
769 EWMH_SOURCE_TYPE_OTHER
= 2,
776 XC_BOTTOM_LEFT_CORNER
,
777 XC_BOTTOM_RIGHT_CORNER
,
785 char *name
; /* Name used by Xcursor .*/
786 uint8_t cf_char
; /* cursorfont index. */
788 } cursors
[XC_MAX
] = {
789 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
790 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
791 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
792 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
793 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
794 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
795 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
798 #define SWM_SPAWN_OPTIONAL 0x1
802 TAILQ_ENTRY(spawn_prog
) entry
;
808 TAILQ_HEAD(spawn_list
, spawn_prog
);
809 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
815 /* User callable function IDs. */
828 FN_FULLSCREEN_TOGGLE
,
942 /* SWM_DEBUG actions MUST be here: */
955 BINDING_F_REPLAY
= 0x1,
959 RB_ENTRY(binding
) entry
;
960 uint16_t mod
; /* Modifier Mask. */
961 enum binding_type type
; /* Key or Button. */
962 uint32_t value
; /* KeySym or Button Index. */
963 enum actionid action
; /* Action Identifier. */
967 RB_HEAD(binding_tree
, binding
);
969 /* function prototypes */
970 void adjust_font(struct ws_win
*);
971 char *argsep(char **);
972 void bar_cleanup(struct swm_region
*);
973 void bar_extra_setup(void);
974 void bar_extra_stop(void);
975 int bar_extra_update(void);
976 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
977 void bar_fmt_expand(char *, size_t);
978 void bar_draw(struct swm_bar
*);
979 void bar_print(struct swm_region
*, const char *);
980 void bar_print_legacy(struct swm_region
*, const char *);
981 void bar_replace(char *, char *, struct swm_region
*, size_t);
982 void bar_replace_pad(char *, int *, size_t);
983 char *bar_replace_seq(char *, char *, struct swm_region
*, size_t *, size_t);
984 void bar_setup(struct swm_region
*);
985 void bar_toggle(struct binding
*, struct swm_region
*, union arg
*);
986 void bar_urgent(char *, size_t);
987 void bar_window_class(char *, size_t, struct swm_region
*);
988 void bar_window_class_instance(char *, size_t, struct swm_region
*);
989 void bar_window_float(char *, size_t, struct swm_region
*);
990 void bar_window_instance(char *, size_t, struct swm_region
*);
991 void bar_window_name(char *, size_t, struct swm_region
*);
992 void bar_window_state(char *, size_t, struct swm_region
*);
993 void bar_workspace_name(char *, size_t, struct swm_region
*);
994 int binding_cmp(struct binding
*, struct binding
*);
995 void binding_insert(uint16_t, enum binding_type
, uint32_t, enum actionid
,
996 uint32_t, const char *);
997 struct binding
*binding_lookup(uint16_t, enum binding_type
, uint32_t);
998 void binding_remove(struct binding
*);
999 void buttonpress(xcb_button_press_event_t
*);
1000 void buttonrelease(xcb_button_release_event_t
*);
1001 void center_pointer(struct swm_region
*);
1002 void check_conn(void);
1003 void clear_bindings(void);
1004 void clear_keybindings(void);
1005 int clear_maximized(struct workspace
*);
1006 void clear_quirks(void);
1007 void clear_spawns(void);
1008 void clientmessage(xcb_client_message_event_t
*);
1009 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
1010 int conf_load(const char *, int);
1011 void configurenotify(xcb_configure_notify_event_t
*);
1012 void configurerequest(xcb_configure_request_event_t
*);
1013 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
1014 void constrain_window(struct ws_win
*, struct swm_geometry
*, int *);
1015 int count_win(struct workspace
*, bool);
1016 void cursors_cleanup(void);
1017 void cursors_load(void);
1018 void custom_region(const char *);
1019 void cycle_layout(struct binding
*, struct swm_region
*, union arg
*);
1020 void cyclerg(struct binding
*, struct swm_region
*, union arg
*);
1021 void cyclews(struct binding
*, struct swm_region
*, union arg
*);
1023 void debug_refresh(struct ws_win
*);
1025 void debug_toggle(struct binding
*, struct swm_region
*, union arg
*);
1026 void destroynotify(xcb_destroy_notify_event_t
*);
1027 void dumpwins(struct binding
*, struct swm_region
*, union arg
*);
1028 int enable_wm(void);
1029 void enternotify(xcb_enter_notify_event_t
*);
1030 void event_drain(uint8_t);
1031 void event_error(xcb_generic_error_t
*);
1032 void event_handle(xcb_generic_event_t
*);
1033 void ewmh_apply_flags(struct ws_win
*, uint32_t);
1034 void ewmh_autoquirk(struct ws_win
*);
1035 void ewmh_get_desktop_names(void);
1036 void ewmh_get_wm_state(struct ws_win
*);
1037 void ewmh_update_actions(struct ws_win
*);
1038 void ewmh_update_client_list(void);
1039 void ewmh_update_current_desktop(void);
1040 void ewmh_update_desktop_names(void);
1041 void ewmh_update_desktops(void);
1042 void ewmh_change_wm_state(struct ws_win
*, xcb_atom_t
, long);
1043 void ewmh_update_wm_state(struct ws_win
*);
1044 char *expand_tilde(const char *);
1045 void expose(xcb_expose_event_t
*);
1046 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
1047 struct swm_bar
*find_bar(xcb_window_t
);
1048 struct ws_win
*find_frame_window(xcb_window_t
);
1049 struct pid_e
*find_pid(pid_t
);
1050 struct swm_region
*find_region(xcb_window_t
);
1051 struct ws_win
*find_unmanaged_window(xcb_window_t
);
1052 struct ws_win
*find_window(xcb_window_t
);
1053 void floating_toggle(struct binding
*, struct swm_region
*, union arg
*);
1054 void focus(struct binding
*, struct swm_region
*, union arg
*);
1055 void focus_flush(void);
1056 void focus_pointer(struct binding
*, struct swm_region
*, union arg
*);
1057 void focus_region(struct swm_region
*);
1058 void focus_win(struct ws_win
*);
1059 void focusin(xcb_focus_in_event_t
*);
1061 void focusout(xcb_focus_out_event_t
*);
1063 void focusrg(struct binding
*, struct swm_region
*, union arg
*);
1064 void fontset_init(void);
1065 void free_window(struct ws_win
*);
1066 void fullscreen_toggle(struct binding
*, struct swm_region
*, union arg
*);
1067 xcb_atom_t
get_atom_from_string(const char *);
1069 char *get_atom_name(xcb_atom_t
);
1071 struct ws_win
*get_focus_magic(struct ws_win
*);
1072 struct ws_win
*get_focus_prev(struct ws_win
*);
1073 xcb_generic_event_t
*get_next_event(bool);
1075 char *get_notify_detail_label(uint8_t);
1076 char *get_notify_mode_label(uint8_t);
1078 struct ws_win
*get_pointer_win(xcb_window_t
);
1079 struct ws_win
*get_region_focus(struct swm_region
*);
1080 int get_region_index(struct swm_region
*);
1081 xcb_screen_t
*get_screen(int);
1082 int get_screen_count(void);
1084 char *get_source_type_label(uint32_t);
1085 char *get_stack_mode_name(uint8_t);
1086 char *get_state_mask_label(uint16_t);
1088 int32_t get_swm_ws(xcb_window_t
);
1089 bool get_urgent(struct ws_win
*);
1091 char *get_win_input_model(struct ws_win
*);
1093 char *get_win_name(xcb_window_t
);
1094 uint8_t get_win_state(xcb_window_t
);
1095 void get_wm_protocols(struct ws_win
*);
1096 int get_ws_idx(struct ws_win
*);
1097 void grab_windows(void);
1098 void grabbuttons(void);
1099 void grabkeys(void);
1100 void iconify(struct binding
*, struct swm_region
*, union arg
*);
1101 bool isxlfd(char *);
1102 bool keybindreleased(struct binding
*, xcb_key_release_event_t
*);
1103 void keypress(xcb_key_press_event_t
*);
1104 void keyrelease(xcb_key_release_event_t
*);
1105 bool keyrepeating(xcb_key_release_event_t
*);
1106 void kill_bar_extra_atexit(void);
1107 void kill_refs(struct ws_win
*);
1109 void leavenotify(xcb_leave_notify_event_t
*);
1111 void load_float_geom(struct ws_win
*);
1112 void lower_window(struct ws_win
*);
1113 struct ws_win
*manage_window(xcb_window_t
, int, bool);
1114 void map_window(struct ws_win
*);
1115 void mapnotify(xcb_map_notify_event_t
*);
1116 void mappingnotify(xcb_mapping_notify_event_t
*);
1117 void maprequest(xcb_map_request_event_t
*);
1118 void maximize_toggle(struct binding
*, struct swm_region
*, union arg
*);
1119 void motionnotify(xcb_motion_notify_event_t
*);
1120 void move(struct binding
*, struct swm_region
*, union arg
*);
1121 void move_win(struct ws_win
*, struct binding
*, int);
1122 uint32_t name_to_pixel(int, const char *);
1123 void name_workspace(struct binding
*, struct swm_region
*, union arg
*);
1124 void new_region(struct swm_screen
*, int, int, int, int);
1125 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1126 int parsebinding(const char *, uint16_t *, enum binding_type
*, uint32_t *,
1128 int parsequirks(const char *, uint32_t *, int *);
1129 void pressbutton(struct binding
*, struct swm_region
*, union arg
*);
1130 void priorws(struct binding
*, struct swm_region
*, union arg
*);
1132 void print_win_geom(xcb_window_t
);
1134 void propertynotify(xcb_property_notify_event_t
*);
1135 void put_back_event(xcb_generic_event_t
*);
1136 void quirk_free(struct quirk
*);
1137 void quirk_insert(const char *, const char *, const char *, uint32_t, int);
1138 void quirk_remove(struct quirk
*);
1139 void quirk_replace(struct quirk
*, const char *, const char *, const char *,
1141 void quit(struct binding
*, struct swm_region
*, union arg
*);
1142 void raise_focus(struct binding
*, struct swm_region
*, union arg
*);
1143 void raise_toggle(struct binding
*, struct swm_region
*, union arg
*);
1144 void raise_window(struct ws_win
*);
1145 void region_containment(struct ws_win
*, struct swm_region
*, int);
1146 struct swm_region
*region_under(struct swm_screen
*, int, int);
1147 void regionize(struct ws_win
*, int, int);
1148 void reparent_window(struct ws_win
*);
1149 void reparentnotify(xcb_reparent_notify_event_t
*);
1150 void resize(struct binding
*, struct swm_region
*, union arg
*);
1151 void resize_win(struct ws_win
*, struct binding
*, int);
1152 void restart(struct binding
*, struct swm_region
*, union arg
*);
1153 struct swm_region
*root_to_region(xcb_window_t
, int);
1154 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1155 void scan_randr(int);
1156 void search_do_resp(void);
1157 void search_resp_name_workspace(const char *, size_t);
1158 void search_resp_search_window(const char *);
1159 void search_resp_search_workspace(const char *);
1160 void search_resp_uniconify(const char *, size_t);
1161 void search_win(struct binding
*, struct swm_region
*, union arg
*);
1162 void search_win_cleanup(void);
1163 void search_workspace(struct binding
*, struct swm_region
*, union arg
*);
1164 void send_to_rg(struct binding
*, struct swm_region
*, union arg
*);
1165 void send_to_rg_relative(struct binding
*, struct swm_region
*, union arg
*);
1166 void send_to_ws(struct binding
*, struct swm_region
*, union arg
*);
1167 void set_region(struct swm_region
*);
1168 int setautorun(const char *, const char *, int);
1169 void setbinding(uint16_t, enum binding_type
, uint32_t, enum actionid
,
1170 uint32_t, const char *);
1171 int setconfbinding(const char *, const char *, int);
1172 int setconfcolor(const char *, const char *, int);
1173 int setconfmodkey(const char *, const char *, int);
1174 int setconfquirk(const char *, const char *, int);
1175 int setconfregion(const char *, const char *, int);
1176 int setconfspawn(const char *, const char *, int);
1177 int setconfvalue(const char *, const char *, int);
1178 int setkeymapping(const char *, const char *, int);
1179 int setlayout(const char *, const char *, int);
1180 void setquirk(const char *, const char *, const char *, uint32_t, int);
1181 void setscreencolor(const char *, int, int);
1182 void setspawn(const char *, const char *, int);
1183 void setup_btnbindings(void);
1184 void setup_ewmh(void);
1185 void setup_globals(void);
1186 void setup_keybindings(void);
1187 void setup_quirks(void);
1188 void setup_screens(void);
1189 void setup_spawn(void);
1190 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1191 void set_win_state(struct ws_win
*, uint8_t);
1192 void shutdown_cleanup(void);
1194 void socket_setnonblock(int);
1195 void sort_windows(struct ws_win_list
*);
1196 void spawn(int, union arg
*, bool);
1197 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1198 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1199 void spawn_insert(const char *, const char *, int);
1200 struct spawn_prog
*spawn_find(const char *);
1201 void spawn_remove(struct spawn_prog
*);
1202 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1203 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1204 void stack_config(struct binding
*, struct swm_region
*, union arg
*);
1205 void stack_master(struct workspace
*, struct swm_geometry
*, int, bool);
1206 void store_float_geom(struct ws_win
*);
1207 char *strdupsafe(const char *);
1208 void swapwin(struct binding
*, struct swm_region
*, union arg
*);
1209 void switchws(struct binding
*, struct swm_region
*, union arg
*);
1210 void teardown_ewmh(void);
1211 void unescape_selector(char *);
1212 void unfocus_win(struct ws_win
*);
1213 void uniconify(struct binding
*, struct swm_region
*, union arg
*);
1214 void unmanage_window(struct ws_win
*);
1215 void unmap_all(void);
1216 void unmap_window(struct ws_win
*);
1217 void unmapnotify(xcb_unmap_notify_event_t
*);
1218 void unparent_window(struct ws_win
*);
1219 void update_floater(struct ws_win
*);
1220 void update_modkey(uint16_t);
1221 void update_win_stacking(struct ws_win
*);
1222 void update_window(struct ws_win
*);
1223 void draw_frame(struct ws_win
*);
1224 void update_wm_state(struct ws_win
*win
);
1225 void updatenumlockmask(void);
1226 void validate_spawns(void);
1227 int validate_win(struct ws_win
*);
1228 int validate_ws(struct workspace
*);
1229 void version(struct binding
*, struct swm_region
*, union arg
*);
1230 void win_to_ws(struct ws_win
*, int, bool);
1231 pid_t
window_get_pid(xcb_window_t
);
1232 void wkill(struct binding
*, struct swm_region
*, union arg
*);
1233 void update_ws_stack(struct workspace
*);
1234 void xft_init(struct swm_region
*);
1235 void _add_startup_exception(const char *, va_list);
1236 void add_startup_exception(const char *, ...);
1238 RB_PROTOTYPE(binding_tree
, binding
, entry
, binding_cmp
);
1239 RB_GENERATE(binding_tree
, binding
, entry
, binding_cmp
);
1240 struct binding_tree bindings
;
1245 xcb_font_t cf
= XCB_NONE
;
1248 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1249 /* try to load Xcursor first. */
1250 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1253 /* fallback to cursorfont. */
1254 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1255 if (cf
== XCB_NONE
) {
1256 cf
= xcb_generate_id(conn
);
1257 xcb_open_font(conn
, cf
, strlen("cursor"),
1261 cursors
[i
].cid
= xcb_generate_id(conn
);
1262 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1263 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1264 0xffff, 0xffff, 0xffff);
1270 xcb_close_font(conn
, cf
);
1274 cursors_cleanup(void)
1277 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1278 xcb_free_cursor(conn
, cursors
[i
].cid
);
1282 expand_tilde(const char *s
)
1284 struct passwd
*ppwd
;
1292 errx(1, "expand_tilde: NULL string.");
1295 result
= strdup(sc
);
1301 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1302 errx(1, "expand_tilde: sysconf");
1304 if ((user
= calloc(1, max
+ 1)) == NULL
)
1305 errx(1, "expand_tilde: calloc");
1307 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1312 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1316 result
= strdup(sc
);
1318 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1322 errx(1, "expand_tilde: failed to allocate memory.");
1328 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1330 unsigned int tmpr
, tmpg
, tmpb
;
1332 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1335 *rr
= RGB_8_TO_16(tmpr
);
1336 *gg
= RGB_8_TO_16(tmpg
);
1337 *bb
= RGB_8_TO_16(tmpb
);
1343 get_screen(int screen
)
1345 const xcb_setup_t
*r
;
1346 xcb_screen_iterator_t iter
;
1348 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1349 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1353 iter
= xcb_setup_roots_iterator(r
);
1354 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1362 get_screen_count(void)
1364 const xcb_setup_t
*r
;
1366 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1367 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1371 return xcb_setup_roots_length(r
);
1375 get_region_index(struct swm_region
*r
)
1377 struct swm_region
*rr
;
1383 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1398 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1399 event_drain(XCB_ENTER_NOTIFY
);
1405 get_atom_from_string(const char *str
)
1407 xcb_intern_atom_cookie_t c
;
1408 xcb_intern_atom_reply_t
*r
;
1411 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1412 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1420 return (XCB_ATOM_NONE
);
1424 get_wm_protocols(struct ws_win
*win
) {
1426 xcb_icccm_get_wm_protocols_reply_t wpr
;
1428 if (xcb_icccm_get_wm_protocols_reply(conn
,
1429 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
1431 for (i
= 0; i
< (int)wpr
.atoms_len
; i
++) {
1432 if (wpr
.atoms
[i
] == a_takefocus
)
1433 win
->take_focus
= true;
1434 if (wpr
.atoms
[i
] == a_delete
)
1435 win
->can_delete
= true;
1437 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
1444 xcb_window_t root
, win
;
1445 int i
, j
, num_screens
;
1447 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1448 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1450 num_screens
= get_screen_count();
1451 for (i
= 0; i
< num_screens
; i
++) {
1452 root
= screens
[i
].root
;
1454 /* Set up _NET_SUPPORTING_WM_CHECK. */
1455 win
= xcb_generate_id(conn
);
1456 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
1457 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1458 XCB_COPY_FROM_PARENT
, 0, NULL
);
1460 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
1461 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1462 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1463 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1465 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1466 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1467 8, strlen("spectrwm"), "spectrwm");
1469 /* Report supported atoms */
1470 xcb_delete_property(conn
, root
, a_net_supported
);
1471 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1472 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, root
,
1473 a_net_supported
, XCB_ATOM_ATOM
, 32, 1,
1478 ewmh_update_desktops();
1479 ewmh_get_desktop_names();
1487 xcb_get_property_cookie_t pc
;
1488 xcb_get_property_reply_t
*pr
;
1490 num_screens
= get_screen_count();
1492 for (i
= 0; i
< num_screens
; i
++) {
1493 /* Get the support check window and destroy it */
1494 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, a_net_wm_check
,
1495 XCB_ATOM_WINDOW
, 0, 1);
1496 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1499 if (pr
->format
== a_net_wm_check
) {
1500 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1502 xcb_destroy_window(conn
, id
);
1503 xcb_delete_property(conn
, screens
[i
].root
,
1505 xcb_delete_property(conn
, screens
[i
].root
,
1513 ewmh_autoquirk(struct ws_win
*win
)
1515 xcb_get_property_reply_t
*r
;
1516 xcb_get_property_cookie_t c
;
1520 c
= xcb_get_property(conn
, 0, win
->id
,
1521 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1522 r
= xcb_get_property_reply(conn
, c
, NULL
);
1526 type
= xcb_get_property_value(r
);
1527 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1529 for (i
= 0; i
< n
; i
++) {
1530 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1532 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1533 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1534 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1535 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1538 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1539 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1540 win
->quirks
= SWM_Q_FLOAT
;
1547 c
= xcb_get_property(conn
, 0, win
->id
,
1548 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1549 r
= xcb_get_property_reply(conn
, c
, NULL
);
1553 type
= xcb_get_property_value(r
);
1554 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1556 for (i
= 0; i
< n
; i
++) {
1557 if (type
[i
] == ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
||
1558 type
[i
] == ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
) {
1559 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1567 ewmh_update_actions(struct ws_win
*win
)
1569 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1575 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1578 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1579 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1580 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1583 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1584 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1587 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1588 #define _NET_WM_STATE_ADD 1 /* add/set property */
1589 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1592 ewmh_change_wm_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1599 name
= get_atom_name(state
);
1600 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: win %#x, state: %s, "
1601 "action: %ld\n", WINID(win
), name
, action
);
1607 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1608 flag
= EWMH_F_FULLSCREEN
;
1609 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1610 flag
= EWMH_F_ABOVE
;
1611 else if (state
== ewmh
[_NET_WM_STATE_HIDDEN
].atom
)
1612 flag
= EWMH_F_HIDDEN
;
1613 else if (state
== ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
||
1614 state
== ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
)
1615 flag
= EWMH_F_MAXIMIZED
;
1616 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1617 flag
= SWM_F_MANUAL
;
1618 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1619 flag
= EWMH_F_SKIP_PAGER
;
1620 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1621 flag
= EWMH_F_SKIP_TASKBAR
;
1623 /* Disallow unfloating transients. */
1624 if (TRANS(win
) && flag
== EWMH_F_ABOVE
)
1627 new_flags
= win
->ewmh_flags
;
1630 case _NET_WM_STATE_REMOVE
:
1633 case _NET_WM_STATE_ADD
:
1636 case _NET_WM_STATE_TOGGLE
:
1641 ewmh_apply_flags(win
, new_flags
);
1644 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: done.\n");
1648 ewmh_apply_flags(struct ws_win
*win
, uint32_t pending
)
1650 struct workspace
*ws
;
1653 changed
= win
->ewmh_flags
^ pending
;
1657 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: pending: %d\n", pending
);
1659 win
->ewmh_flags
= pending
;
1662 if (changed
& EWMH_F_HIDDEN
) {
1664 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1665 ws
->focus_pending
= get_focus_prev(win
);
1670 /* Reload floating geometry in case region changed. */
1672 load_float_geom(win
);
1674 /* The window is no longer iconic, prepare focus. */
1675 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1676 ws
->focus_pending
= get_focus_magic(win
);
1681 if (changed
& EWMH_F_ABOVE
) {
1682 if (ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1684 load_float_geom(win
);
1685 else if (!MAXIMIZED(win
))
1686 store_float_geom(win
);
1688 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1689 changed
&= ~EWMH_F_MAXIMIZED
;
1693 win
->ewmh_flags
^= EWMH_F_ABOVE
& pending
;
1697 if (changed
& EWMH_F_MAXIMIZED
) {
1698 /* VERT and/or HORZ changed. */
1700 if (!MAXIMIZED(win
))
1701 load_float_geom(win
);
1703 store_float_geom(win
);
1706 if (MAXIMIZED(win
)) {
1707 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
1708 ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1712 ws
->focus_pending
= win
;
1720 if (changed
& EWMH_F_FULLSCREEN
) {
1721 if (FULLSCREEN(win
)) {
1722 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
1726 ws
->focus_pending
= win
;
1729 load_float_geom(win
);
1732 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1736 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: done.\n");
1740 ewmh_update_wm_state(struct ws_win
*win
) {
1741 xcb_atom_t vals
[SWM_EWMH_ACTION_COUNT_MAX
];
1745 vals
[n
++] = ewmh
[_NET_WM_STATE_HIDDEN
].atom
;
1746 if (FULLSCREEN(win
))
1747 vals
[n
++] = ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
;
1748 if (MAXIMIZED_VERT(win
))
1749 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
;
1750 if (MAXIMIZED_HORZ(win
))
1751 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
;
1752 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1753 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
;
1754 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1755 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
;
1756 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1757 vals
[n
++] = ewmh
[_NET_WM_STATE_ABOVE
].atom
;
1758 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1759 vals
[n
++] = ewmh
[_SWM_WM_STATE_MANUAL
].atom
;
1762 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1763 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, n
, vals
);
1765 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1769 ewmh_get_wm_state(struct ws_win
*win
)
1772 xcb_get_property_cookie_t c
;
1773 xcb_get_property_reply_t
*r
;
1779 win
->ewmh_flags
= 0;
1781 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1782 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1783 r
= xcb_get_property_reply(conn
, c
, NULL
);
1787 states
= xcb_get_property_value(r
);
1788 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1790 for (i
= 0; i
< n
; i
++)
1791 ewmh_change_wm_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1799 dumpwins(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
1803 xcb_get_window_attributes_cookie_t c
;
1804 xcb_get_window_attributes_reply_t
*wa
;
1806 /* suppress unused warning since var is needed */
1810 if (r
->ws
== NULL
) {
1811 DPRINTF("dumpwins: invalid workspace\n");
1815 DPRINTF("=== managed window list ws %02d ===\n", r
->ws
->idx
);
1816 TAILQ_FOREACH(w
, &r
->ws
->winlist
, 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 (%#x), map_state: %d, state: %u, "
1822 "transient: %#x\n", w
->frame
, w
->id
, wa
->map_state
,
1823 state
, w
->transient
);
1826 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1830 DPRINTF("=== stacking order (top down) === \n");
1831 TAILQ_FOREACH(w
, &r
->ws
->stack
, stack_entry
) {
1832 DPRINTF("win %#x (%#x), fs: %s, maximized: %s, above: %s, "
1833 "iconic: %s\n", w
->frame
, w
->id
, YESNO(FULLSCREEN(w
)),
1834 YESNO(MAXIMIZED(w
)), YESNO(ABOVE(w
)), YESNO(ICONIC(w
)));
1837 DPRINTF("===== unmanaged window list =====\n");
1838 TAILQ_FOREACH(w
, &r
->ws
->unmanagedlist
, entry
) {
1839 state
= get_win_state(w
->id
);
1840 c
= xcb_get_window_attributes(conn
, w
->id
);
1841 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1843 DPRINTF("win %#x, map_state: %d, state: %u, "
1844 "transient: %#x\n", w
->id
, wa
->map_state
,
1845 state
, w
->transient
);
1848 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1852 DPRINTF("=================================\n");
1856 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
1859 int num_screens
, i
, j
;
1861 /* Suppress warnings. */
1866 DNPRINTF(SWM_D_MISC
, "debug_toggle\n");
1868 debug_enabled
= !debug_enabled
;
1870 num_screens
= get_screen_count();
1871 for (i
= 0; i
< num_screens
; i
++)
1872 for (j
= 0; j
< workspace_limit
; j
++)
1873 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1880 debug_refresh(struct ws_win
*win
)
1887 XRectangle l_ibox
, l_lbox
;
1888 xcb_rectangle_t rect
;
1890 uint32_t wc
[4], mask
, width
, height
, gcv
[1];
1893 xcb_screen_t
*screen
;
1895 if (debug_enabled
) {
1896 /* Create debug window if it doesn't exist. */
1897 if (win
->debug
== XCB_WINDOW_NONE
) {
1898 if ((screen
= get_screen(win
->s
->idx
)) == NULL
)
1899 errx(1, "ERROR: can't get screen %d.",
1902 win
->debug
= xcb_generate_id(conn
);
1903 wc
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1904 wc
[1] = win
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
1905 wc
[2] = screen
->default_colormap
;
1907 xcb_create_window(conn
, screen
->root_depth
, win
->debug
,
1908 win
->frame
, 0, 0, 10, 10, 1,
1909 XCB_WINDOW_CLASS_INPUT_OUTPUT
, screen
->root_visual
,
1910 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
|
1911 XCB_CW_COLORMAP
, wc
);
1913 xcb_map_window(conn
, win
->debug
);
1916 /* Determine workspace window list index. */
1918 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
1924 /* Determine stacking index (top down). */
1926 TAILQ_FOREACH(w
, &win
->ws
->stack
, stack_entry
) {
1932 if (asprintf(&s
, "%#x f:%#x wl:%d s:%d im:%s", win
->id
,
1933 win
->frame
, widx
, sidx
, get_win_input_model(win
)) == -1)
1938 /* Update window to an appropriate dimension. */
1939 if (bar_font_legacy
) {
1940 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
1941 width
= l_lbox
.width
+ 4;
1942 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
1944 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
1946 width
= info
.width
+ 4;
1947 height
= bar_font
->height
+ 4;
1950 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
1951 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
;
1953 wc
[0] = wc
[1] = border_width
;
1960 xcb_configure_window(conn
, win
->debug
, mask
, wc
);
1962 /* Draw a filled rectangle to 'clear' window. */
1966 rect
.height
= height
;
1968 gcv
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1969 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1970 xcb_poly_fill_rectangle(conn
, win
->debug
, win
->s
->bar_gc
, 1,
1974 if (bar_font_legacy
) {
1975 l_gcv
.graphics_exposures
= 0;
1976 l_draw
= XCreateGC(display
, win
->debug
, 0, &l_gcv
);
1978 XSetForeground(display
, l_draw
,
1979 win
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
1981 DRAWSTRING(display
, win
->debug
, bar_fs
, l_draw
, 2,
1982 (bar_fs_extents
->max_logical_extent
.height
-
1983 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
1985 XFreeGC(display
, l_draw
);
1987 draw
= XftDrawCreate(display
, win
->debug
,
1988 DefaultVisual(display
, win
->s
->idx
),
1989 DefaultColormap(display
, win
->s
->idx
));
1991 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
1992 (bar_height
+ bar_font
->height
) / 2 -
1993 bar_font
->descent
, (FcChar8
*)s
, len
);
1995 XftDrawDestroy(draw
);
1999 } else if (win
->debug
!= XCB_WINDOW_NONE
) {
2000 xcb_destroy_window(conn
, win
->debug
);
2001 win
->debug
= XCB_WINDOW_NONE
;
2006 dumpwins(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
2014 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
2020 #endif /* SWM_DEBUG */
2025 int saved_errno
, status
;
2028 saved_errno
= errno
;
2032 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
2037 if (errno
!= ECHILD
)
2038 warn("sighdlr: waitpid");
2039 #endif /* SWM_DEBUG */
2042 if (pid
== searchpid
)
2046 if (WIFEXITED(status
)) {
2047 if (WEXITSTATUS(status
) != 0)
2048 warnx("sighdlr: child exit status: %d",
2049 WEXITSTATUS(status
));
2051 warnx("sighdlr: child is terminated "
2053 #endif /* SWM_DEBUG */
2067 errno
= saved_errno
;
2073 struct pid_e
*p
= NULL
;
2075 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
2080 TAILQ_FOREACH(p
, &pidlist
, entry
) {
2089 name_to_pixel(int sidx
, const char *colorname
)
2091 uint32_t result
= 0;
2092 char cname
[32] = "#";
2093 xcb_screen_t
*screen
;
2094 xcb_colormap_t cmap
;
2095 xcb_alloc_color_reply_t
*cr
;
2096 xcb_alloc_named_color_reply_t
*nr
;
2097 uint16_t rr
, gg
, bb
;
2099 screen
= get_screen(sidx
);
2100 cmap
= screen
->default_colormap
;
2102 /* color is in format rgb://rr/gg/bb */
2103 if (strncmp(colorname
, "rgb:", 4) == 0) {
2104 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
2105 warnx("could not parse rgb %s", colorname
);
2107 cr
= xcb_alloc_color_reply(conn
,
2108 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
2114 warnx("color '%s' not found", colorname
);
2117 nr
= xcb_alloc_named_color_reply(conn
,
2118 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
2121 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
2122 nr
= xcb_alloc_named_color_reply(conn
,
2123 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
2130 warnx("color '%s' not found", colorname
);
2137 setscreencolor(const char *val
, int i
, int c
)
2139 if (i
< 0 || i
>= get_screen_count())
2142 screens
[i
].c
[c
].pixel
= name_to_pixel(i
, val
);
2143 free(screens
[i
].c
[c
].name
);
2144 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
2149 fancy_stacker(struct workspace
*ws
)
2151 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2152 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2153 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2154 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
2155 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
2156 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2157 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2158 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
2159 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
2163 plain_stacker(struct workspace
*ws
)
2165 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2166 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2167 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
2168 sizeof ws
->stacker
);
2169 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2170 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
2171 sizeof ws
->stacker
);
2175 custom_region(const char *val
)
2177 unsigned int x
, y
, w
, h
;
2178 int sidx
, num_screens
;
2179 xcb_screen_t
*screen
;
2181 DNPRINTF(SWM_D_CONF
, "custom_region: %s\n", val
);
2183 num_screens
= get_screen_count();
2184 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
2185 errx(1, "invalid custom region, "
2186 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
2187 if (sidx
< 1 || sidx
> num_screens
)
2188 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
2192 if ((screen
= get_screen(sidx
)) == NULL
)
2193 errx(1, "ERROR: can't get screen %d.", sidx
);
2196 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
2198 if (x
> screen
->width_in_pixels
||
2199 y
> screen
->height_in_pixels
||
2200 w
+ x
> screen
->width_in_pixels
||
2201 h
+ y
> screen
->height_in_pixels
) {
2202 warnx("ignoring region %ux%u+%u+%u - not within screen "
2203 "boundaries (%ux%u)", w
, h
, x
, y
,
2204 screen
->width_in_pixels
, screen
->height_in_pixels
);
2208 new_region(&screens
[sidx
], x
, y
, w
, h
);
2212 socket_setnonblock(int fd
)
2216 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
2217 err(1, "fcntl F_GETFL");
2218 flags
|= O_NONBLOCK
;
2219 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
2220 err(1, "fcntl F_SETFL");
2224 bar_print_legacy(struct swm_region
*r
, const char *s
)
2226 xcb_rectangle_t rect
;
2231 XRectangle ibox
, lbox
;
2235 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
2237 switch (bar_justify
) {
2238 case SWM_BAR_JUSTIFY_LEFT
:
2241 case SWM_BAR_JUSTIFY_CENTER
:
2242 x
= (WIDTH(r
) - lbox
.width
) / 2;
2244 case SWM_BAR_JUSTIFY_RIGHT
:
2245 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
2249 if (x
< SWM_BAR_OFFSET
)
2254 rect
.width
= WIDTH(r
->bar
);
2255 rect
.height
= HEIGHT(r
->bar
);
2257 /* clear back buffer */
2258 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2259 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2260 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2262 /* draw back buffer */
2263 gcvd
.graphics_exposures
= 0;
2264 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
2265 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2266 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
2267 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
2269 XFreeGC(display
, draw
);
2272 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2273 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2277 bar_print(struct swm_region
*r
, const char *s
)
2280 xcb_rectangle_t rect
;
2288 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
2290 switch (bar_justify
) {
2291 case SWM_BAR_JUSTIFY_LEFT
:
2294 case SWM_BAR_JUSTIFY_CENTER
:
2295 x
= (WIDTH(r
) - info
.width
) / 2;
2297 case SWM_BAR_JUSTIFY_RIGHT
:
2298 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
2302 if (x
< SWM_BAR_OFFSET
)
2307 rect
.width
= WIDTH(r
->bar
);
2308 rect
.height
= HEIGHT(r
->bar
);
2310 /* clear back buffer */
2311 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2312 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2313 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2315 /* draw back buffer */
2316 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
2317 DefaultVisual(display
, r
->s
->idx
),
2318 DefaultColormap(display
, r
->s
->idx
));
2320 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
2321 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
2324 XftDrawDestroy(draw
);
2327 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2328 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2332 bar_extra_stop(void)
2336 bzero(bar_pipe
, sizeof bar_pipe
);
2339 kill(bar_pid
, SIGTERM
);
2342 strlcpy(bar_ext
, "", sizeof bar_ext
);
2347 bar_window_class(char *s
, size_t sz
, struct swm_region
*r
)
2349 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2351 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
2352 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
2356 bar_window_instance(char *s
, size_t sz
, struct swm_region
*r
)
2358 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2360 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
2361 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
2365 bar_window_class_instance(char *s
, size_t sz
, struct swm_region
*r
)
2367 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2370 bar_window_class(s
, sz
, r
);
2371 strlcat(s
, ":", sz
);
2372 bar_window_instance(s
, sz
, r
);
2376 bar_window_state(char *s
, size_t sz
, struct swm_region
*r
)
2378 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2380 if (MAXIMIZED(r
->ws
->focus
))
2381 strlcat(s
, "(m)", sz
);
2382 else if (ABOVE(r
->ws
->focus
))
2383 strlcat(s
, "(f)", sz
);
2387 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
2391 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2394 title
= get_win_name(r
->ws
->focus
->id
);
2395 strlcat(s
, title
, sz
);
2400 get_urgent(struct ws_win
*win
)
2402 xcb_icccm_wm_hints_t hints
;
2403 xcb_get_property_cookie_t c
;
2404 bool urgent
= false;
2407 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2408 if (xcb_icccm_get_wm_hints_reply(conn
, c
, &hints
, NULL
))
2409 urgent
= xcb_icccm_wm_hints_get_urgency(&hints
);
2416 bar_urgent(char *s
, size_t sz
)
2419 int i
, j
, num_screens
;
2420 bool urgent
[SWM_WS_MAX
];
2423 for (i
= 0; i
< workspace_limit
; i
++)
2426 num_screens
= get_screen_count();
2427 for (i
= 0; i
< num_screens
; i
++)
2428 for (j
= 0; j
< workspace_limit
; j
++)
2429 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2430 if (get_urgent(win
))
2433 for (i
= 0; i
< workspace_limit
; i
++) {
2435 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2437 } else if (!urgent_collapse
) {
2438 strlcat(s
, "- ", sz
);
2441 if (urgent_collapse
&& s
[0])
2442 s
[strlen(s
) - 1] = 0;
2446 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2448 if (r
== NULL
|| r
->ws
== NULL
)
2450 if (r
->ws
->name
!= NULL
)
2451 strlcat(s
, r
->ws
->name
, sz
);
2454 /* build the default bar format according to the defined enabled options */
2456 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2460 /* if format provided, just copy the buffers */
2461 if (bar_format
!= NULL
) {
2462 strlcpy(fmtnew
, fmtexp
, sz
);
2466 /* reset the output buffer */
2469 strlcat(fmtnew
, "+N:+I ", sz
);
2471 strlcat(fmtnew
, "+S", sz
);
2472 strlcat(fmtnew
, " ", sz
);
2474 /* only show the workspace name if there's actually one */
2475 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2476 strlcat(fmtnew
, "<+D>", sz
);
2478 /* If enabled, only show the iconic count if there are iconic wins. */
2479 if (iconic_enabled
&& r
!= NULL
&& r
->ws
!= NULL
)
2480 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2482 strlcat(fmtnew
, "{+M}", sz
);
2486 strlcat(fmtnew
, "+3<", sz
);
2488 if (clock_enabled
) {
2489 strlcat(fmtnew
, fmtexp
, sz
);
2490 strlcat(fmtnew
, "+4<", sz
);
2493 /* bar_urgent already adds the space before the last asterisk */
2495 strlcat(fmtnew
, (urgent_collapse
? "*+U*+4<" : "* +U*+4<"), sz
);
2497 if (window_class_enabled
) {
2498 strlcat(fmtnew
, "+C", sz
);
2499 if (!window_instance_enabled
)
2500 strlcat(fmtnew
, "+4<", sz
);
2503 /* checks needed by the colon and floating strlcat(3) calls below */
2504 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2505 if (window_instance_enabled
) {
2506 if (window_class_enabled
)
2507 strlcat(fmtnew
, ":", sz
);
2508 strlcat(fmtnew
, "+T+4<", sz
);
2510 if (window_name_enabled
) {
2511 if (ABOVE(r
->ws
->focus
) || MAXIMIZED(r
->ws
->focus
))
2512 strlcat(fmtnew
, "+F ", sz
);
2513 strlcat(fmtnew
, "+64W ", sz
);
2517 /* finally add the action script output and the version */
2518 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2522 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2524 /* special case; no limit given, pad one space, instead */
2525 if (*limit
== (int)sz
- 1)
2527 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2530 /* replaces the bar format character sequences (like in tmux(1)) */
2532 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2537 char tmp
[SWM_BAR_MAX
];
2538 int limit
, size
, count
;
2541 /* reset strlcat(3) buffer */
2544 /* get number, if any */
2547 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2548 limit
= sizeof tmp
- 1;
2549 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2550 limit
= sizeof tmp
- 1;
2552 /* there is nothing to replace (ie EOL) */
2559 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2562 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2565 bar_window_class(tmp
, sizeof tmp
, r
);
2568 bar_workspace_name(tmp
, sizeof tmp
, r
);
2571 bar_window_state(tmp
, sizeof tmp
, r
);
2574 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2578 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2582 snprintf(tmp
, sizeof tmp
, "%d", count
);
2585 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2588 bar_window_class_instance(tmp
, sizeof tmp
, r
);
2591 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2594 bar_window_instance(tmp
, sizeof tmp
, r
);
2597 bar_urgent(tmp
, sizeof tmp
);
2600 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2603 bar_window_name(tmp
, sizeof tmp
, r
);
2606 /* unknown character sequence; copy as-is */
2607 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2613 if ((int)len
< limit
)
2615 while (limit
-- > 0) {
2616 if (*offrep
>= sz
- 1)
2618 fmtrep
[(*offrep
)++] = *ptr
++;
2626 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2631 while (*fmt
!= '\0') {
2633 /* skip ordinary characters */
2636 fmtrep
[off
++] = *fmt
++;
2640 /* character sequence found; replace it */
2641 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2650 bar_fmt_expand(char *fmtexp
, size_t sz
)
2657 /* start by grabbing the current time and date */
2659 localtime_r(&tmt
, &tm
);
2661 /* figure out what to expand */
2662 if (bar_format
!= NULL
)
2664 else if (bar_format
== NULL
&& clock_enabled
)
2666 /* if nothing to expand bail out */
2672 /* copy as-is, just in case the format shouldn't be expanded below */
2673 strlcpy(fmtexp
, fmt
, sz
);
2674 /* finally pass the string through strftime(3) */
2675 #ifndef SWM_DENY_CLOCK_FORMAT
2676 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2677 warnx("format too long");
2682 /* Redraws a region bar; need to follow with xcb_flush() or focus_flush(). */
2684 bar_draw(struct swm_bar
*bar
)
2686 struct swm_region
*r
;
2687 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2688 char fmtrep
[SWM_BAR_MAX
];
2690 /* expand the format by first passing it through strftime(3) */
2691 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2698 if (bar_enabled
&& r
->ws
->bar_enabled
)
2699 xcb_map_window(conn
, bar
->id
);
2701 xcb_unmap_window(conn
, bar
->id
);
2705 if (startup_exception
)
2706 snprintf(fmtrep
, sizeof fmtrep
, "total "
2707 "exceptions: %d, first exception: %s",
2711 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2712 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2715 if (bar_font_legacy
)
2716 bar_print_legacy(r
, fmtrep
);
2718 bar_print(r
, fmtrep
);
2722 * Reads external script output; call when stdin is readable.
2723 * Returns 1 if bar_ext was updated; otherwise 0.
2726 bar_extra_update(void)
2729 char b
[SWM_BAR_MAX
];
2730 bool changed
= false;
2735 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2738 if (b
[len
- 1] == '\n') {
2739 /* Remove newline. */
2742 /* "Clear" bar_ext. */
2745 /* Flush buffered output. */
2746 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2747 bar_ext_buf
[0] = '\0';
2749 /* Append new output to bar. */
2750 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2754 /* Buffer output. */
2755 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2760 if (errno
!= EAGAIN
) {
2761 warn("bar_action failed");
2770 bar_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
2772 struct swm_region
*tmpr
;
2775 /* suppress unused warnings since vars are needed */
2780 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2783 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2784 /* Only change if master switch is enabled. */
2786 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2788 bar_enabled
= r
->ws
->bar_enabled
= true;
2790 case SWM_ARG_ID_BAR_TOGGLE
:
2791 bar_enabled
= !bar_enabled
;
2795 /* update bars as necessary */
2796 num_screens
= get_screen_count();
2797 for (i
= 0; i
< num_screens
; i
++)
2798 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2800 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2801 xcb_map_window(conn
, tmpr
->bar
->id
);
2803 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2806 /* Restack all regions and redraw bar. */
2807 num_screens
= get_screen_count();
2808 for (i
= 0; i
< num_screens
; i
++)
2809 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
2811 bar_draw(tmpr
->bar
);
2818 bar_extra_setup(void)
2820 /* do this here because the conf file is in memory */
2821 if (!bar_extra
&& bar_argv
[0]) {
2822 /* launch external status app */
2824 if (pipe(bar_pipe
) == -1)
2825 err(1, "pipe error");
2826 socket_setnonblock(bar_pipe
[0]);
2827 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2829 /* Set stdin to read from the pipe. */
2830 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2833 /* Set stdout to write to the pipe. */
2834 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2837 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2838 err(1, "could not disable SIGPIPE");
2839 switch (bar_pid
= fork()) {
2841 err(1, "cannot fork");
2845 execvp(bar_argv
[0], bar_argv
);
2846 err(1, "%s external app failed", bar_argv
[0]);
2848 default: /* parent */
2853 atexit(kill_bar_extra_atexit
);
2858 kill_bar_extra_atexit(void)
2861 kill(bar_pid
, SIGTERM
);
2869 while ((s
= index(s
, '-'))) {
2874 return (count
== 14);
2880 char *default_string
;
2881 char **missing_charsets
;
2882 int num_missing_charsets
= 0;
2886 XFreeFontSet(display
, bar_fs
);
2890 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n",
2893 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2894 &num_missing_charsets
, &default_string
);
2896 if (num_missing_charsets
> 0) {
2897 warnx("Unable to load charset(s):");
2899 for (i
= 0; i
< num_missing_charsets
; ++i
)
2900 warnx("%s", missing_charsets
[i
]);
2902 XFreeStringList(missing_charsets
);
2904 if (strcmp(default_string
, ""))
2905 warnx("Glyphs from those sets will be replaced "
2906 "by '%s'.", default_string
);
2908 warnx("Glyphs from those sets won't be drawn.");
2912 errx(1, "Error creating font set structure.");
2914 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2916 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2917 2 * bar_border_width
;
2924 xft_init(struct swm_region
*r
)
2926 char *font
, *str
, *search
;
2929 if (bar_font
== NULL
) {
2930 if ((search
= str
= strdup(bar_fonts
)) == NULL
)
2931 errx(1, "insufficient memory.");
2933 while ((font
= strsep(&search
, ",")) != NULL
) {
2937 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2940 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2943 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2947 if (bar_font
== NULL
) {
2948 warnx("unable to load font %s", font
);
2951 DNPRINTF(SWM_D_INIT
, "successfully opened "
2959 if (bar_font
== NULL
)
2960 errx(1, "unable to open a font");
2962 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2964 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2965 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2966 warn("Xft error: unable to allocate color.");
2968 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR
].pixel
, color
);
2970 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2971 DefaultColormap(display
, r
->s
->idx
), &color
, &search_font_color
))
2972 warn("Xft error: unable to allocate color.");
2974 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2981 bar_setup(struct swm_region
*r
)
2983 xcb_screen_t
*screen
;
2986 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2989 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2990 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2995 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2996 err(1, "bar_setup: calloc: failed to allocate memory.");
2998 if (bar_font_legacy
)
3005 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
3006 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
3007 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
3009 /* Assume region is unfocused when we create the bar. */
3010 r
->bar
->id
= xcb_generate_id(conn
);
3011 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
3012 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
3013 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
3014 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
3016 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
3017 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
3018 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
3019 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
3020 | XCB_CW_EVENT_MASK
, wa
);
3022 /* Stack bar window above region window to start. */
3024 wa
[1] = XCB_STACK_MODE_ABOVE
;
3026 xcb_configure_window(conn
, r
->bar
->id
, XCB_CONFIG_WINDOW_SIBLING
|
3027 XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
3029 r
->bar
->buffer
= xcb_generate_id(conn
);
3030 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
3031 WIDTH(r
->bar
), HEIGHT(r
->bar
));
3034 xcb_randr_select_input(conn
, r
->bar
->id
,
3035 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
3038 xcb_map_window(conn
, r
->bar
->id
);
3040 DNPRINTF(SWM_D_BAR
, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
3041 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
3048 bar_cleanup(struct swm_region
*r
)
3052 xcb_destroy_window(conn
, r
->bar
->id
);
3053 xcb_free_pixmap(conn
, r
->bar
->buffer
);
3059 set_win_state(struct ws_win
*win
, uint8_t state
)
3061 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
3063 DNPRINTF(SWM_D_EVENT
, "set_win_state: win %#x, state: %u\n",
3069 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
3070 a_state
, 32, 2, data
);
3074 get_win_state(xcb_window_t w
)
3076 xcb_get_property_reply_t
*r
;
3077 xcb_get_property_cookie_t c
;
3078 uint32_t result
= 0;
3080 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
3081 r
= xcb_get_property_reply(conn
, c
, NULL
);
3083 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
3084 result
= *((uint32_t *)xcb_get_property_value(r
));
3088 DNPRINTF(SWM_D_MISC
, "get_win_state property: win %#x state %u\n", w
,
3094 version(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3096 struct swm_region
*tmpr
;
3099 /* suppress unused warnings since vars are needed */
3104 bar_version
= !bar_version
;
3106 snprintf(bar_vertext
, sizeof bar_vertext
,
3107 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
3109 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
3111 num_screens
= get_screen_count();
3112 for (i
= 0; i
< num_screens
; i
++) {
3113 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
3114 bar_draw(tmpr
->bar
);
3121 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
3123 xcb_client_message_event_t ev
;
3131 name
= get_atom_name(a
);
3132 DNPRINTF(SWM_D_EVENT
, "client_msg: win %#x, atom: %s(%u), "
3134 win
->id
, name
, a
, t
);
3138 bzero(&ev
, sizeof ev
);
3139 ev
.response_type
= XCB_CLIENT_MESSAGE
;
3140 ev
.window
= win
->id
;
3143 ev
.data
.data32
[0] = a
;
3144 ev
.data
.data32
[1] = t
;
3146 xcb_send_event(conn
, 0, win
->id
,
3147 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
3150 /* synthetic response to a ConfigureRequest when not making a change */
3152 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
3154 xcb_configure_notify_event_t ce
;
3159 /* send notification of unchanged state. */
3160 bzero(&ce
, sizeof(ce
));
3161 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
3164 ce
.width
= WIDTH(win
);
3165 ce
.height
= HEIGHT(win
);
3166 ce
.border_width
= 0;
3167 ce
.override_redirect
= 0;
3172 ce
.window
= win
->id
;
3173 ce
.above_sibling
= XCB_WINDOW_NONE
;
3176 ce
.event
= ev
->window
;
3177 ce
.window
= ev
->window
;
3179 /* make response appear more WM_SIZE_HINTS-compliant */
3180 if (win
->sh
.flags
) {
3181 DNPRINTF(SWM_D_MISC
, "config_win: hints: win %#x,"
3182 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
3183 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
3184 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
3185 SH_INC_W(win
), SH_INC_H(win
));
3190 /* the hint may be set... to 0! */
3191 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
3192 ce
.width
= SH_MIN_W(win
);
3193 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
3194 ce
.height
= SH_MIN_H(win
);
3199 /* may also be advertized as 0 */
3200 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
3201 ce
.width
= SH_MAX_W(win
);
3202 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
3203 ce
.height
= SH_MAX_H(win
);
3206 /* resize increment. */
3208 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
3209 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
3211 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
3212 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
3216 /* adjust x and y for requested border_width. */
3217 ce
.x
+= ev
->border_width
;
3218 ce
.y
+= ev
->border_width
;
3220 ce
.above_sibling
= ev
->sibling
;
3223 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
3224 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
3225 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
3227 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
3232 count_win(struct workspace
*ws
, bool count_transient
)
3237 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3238 if (!count_transient
&& FLOATING(win
))
3244 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
3250 quit(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3252 /* suppress unused warnings since vars are needed */
3257 DNPRINTF(SWM_D_MISC
, "quit\n");
3262 lower_window(struct ws_win
*win
)
3264 struct ws_win
*target
= NULL
;
3265 struct workspace
*ws
;
3267 DNPRINTF(SWM_D_EVENT
, "lower_window: win %#x\n", WINID(win
));
3274 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3275 if (target
== win
|| ICONIC(target
))
3277 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3280 if (win
->transient
== target
->transient
)
3282 if (win
->transient
== target
->id
)
3285 if (FULLSCREEN(target
))
3287 if (FULLSCREEN(win
))
3289 if (MAXIMIZED(target
))
3293 if (ABOVE(target
) || TRANS(target
))
3295 if (ABOVE(win
) || TRANS(win
))
3299 /* Change stack position. */
3300 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3302 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3304 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3306 update_win_stacking(win
);
3309 if (swm_debug
& SWM_D_STACK
) {
3310 DPRINTF("=== stacking order (top down) === \n");
3311 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3312 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3313 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3314 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3315 YESNO(ICONIC(target
)));
3319 DNPRINTF(SWM_D_EVENT
, "lower_window: done\n");
3323 raise_window(struct ws_win
*win
)
3325 struct ws_win
*target
= NULL
;
3326 struct workspace
*ws
;
3328 DNPRINTF(SWM_D_EVENT
, "raise_window: win %#x\n", WINID(win
));
3335 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3336 if (target
== win
|| ICONIC(target
))
3338 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3340 if (TRANS(win
) && (win
->transient
== target
->transient
||
3341 win
->transient
== target
->id
))
3343 if (FULLSCREEN(win
))
3345 if (FULLSCREEN(target
))
3349 if (MAXIMIZED(target
))
3351 if (ABOVE(win
) || TRANS(win
) ||
3352 (win
->ws
->focus
== win
&& ws
->always_raise
))
3354 if (!ABOVE(target
) && !TRANS(target
))
3358 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3360 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3362 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3364 update_win_stacking(win
);
3367 if (swm_debug
& SWM_D_STACK
) {
3368 DPRINTF("=== stacking order (top down) === \n");
3369 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3370 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3371 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3372 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3373 YESNO(ICONIC(target
)));
3377 DNPRINTF(SWM_D_EVENT
, "raise_window: done\n");
3381 update_win_stacking(struct ws_win
*win
)
3383 struct ws_win
*sibling
;
3387 struct swm_region
*r
;
3390 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
3393 if (win
->frame
== XCB_WINDOW_NONE
) {
3394 DNPRINTF(SWM_D_EVENT
, "update_window_stacking: win %#x not "
3395 "reparented.\n", win
->id
);
3399 sibling
= TAILQ_NEXT(win
, stack_entry
);
3400 if (sibling
!= NULL
&& (FLOATING(win
) == FLOATING(sibling
) ||
3401 (win
->ws
->always_raise
&& win
->ws
->focus
== win
)))
3402 val
[0] = sibling
->frame
;
3403 else if (FLOATING(win
) || (win
->ws
->always_raise
&&
3404 win
->ws
->focus
== win
))
3405 val
[0] = r
->bar
->id
;
3409 DNPRINTF(SWM_D_EVENT
, "update_win_stacking: win %#x (%#x), "
3410 "sibling %#x\n", win
->frame
, win
->id
, val
[0]);
3412 val
[1] = XCB_STACK_MODE_ABOVE
;
3414 xcb_configure_window(conn
, win
->frame
, XCB_CONFIG_WINDOW_SIBLING
|
3415 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
3418 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
)
3424 map_window(struct ws_win
*win
)
3429 DNPRINTF(SWM_D_EVENT
, "map_window: win %#x, mapped: %s\n",
3430 win
->id
, YESNO(win
->mapped
));
3435 xcb_map_window(conn
, win
->frame
);
3436 xcb_map_window(conn
, win
->id
);
3437 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
3442 unmap_window(struct ws_win
*win
)
3447 DNPRINTF(SWM_D_EVENT
, "unmap_window: win %#x, mapped: %s\n", win
->id
,
3448 YESNO(win
->mapped
));
3453 xcb_unmap_window(conn
, win
->id
);
3454 xcb_unmap_window(conn
, win
->frame
);
3455 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
3456 win
->mapped
= false;
3463 int i
, j
, num_screens
;
3465 num_screens
= get_screen_count();
3466 for (i
= 0; i
< num_screens
; i
++)
3467 for (j
= 0; j
< workspace_limit
; j
++)
3468 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3473 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
3475 xcb_key_press_event_t event
;
3476 xcb_keycode_t
*keycode
;
3481 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
3483 DNPRINTF(SWM_D_MISC
, "fake_keypress: win %#x, keycode %u\n",
3486 bzero(&event
, sizeof(event
));
3487 event
.event
= win
->id
;
3488 event
.root
= win
->s
->root
;
3489 event
.child
= XCB_WINDOW_NONE
;
3490 event
.time
= XCB_CURRENT_TIME
;
3491 event
.event_x
= X(win
);
3492 event
.event_y
= Y(win
);
3495 event
.same_screen
= 1;
3496 event
.detail
= *keycode
;
3497 event
.state
= modifiers
;
3499 event
.response_type
= XCB_KEY_PRESS
;
3500 xcb_send_event(conn
, 1, win
->id
,
3501 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
3503 event
.response_type
= XCB_KEY_RELEASE
;
3504 xcb_send_event(conn
, 1, win
->id
,
3505 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
3511 restart(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3513 /* suppress unused warning since var is needed */
3518 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
3522 execvp(start_argv
[0], start_argv
);
3523 warn("execvp failed");
3524 quit(NULL
, NULL
, NULL
);
3528 get_pointer_win(xcb_window_t root
)
3530 struct ws_win
*win
= NULL
;
3531 xcb_query_pointer_reply_t
*r
;
3533 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: %#x.\n", root
);
3535 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
3537 win
= find_window(r
->child
);
3539 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: %#x.\n",
3542 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
3551 center_pointer(struct swm_region
*r
)
3555 if (!warp_pointer
|| r
== NULL
)
3560 DNPRINTF(SWM_D_EVENT
, "center_pointer: win %#x.\n", WINID(win
));
3562 if (win
&& win
->mapped
)
3563 xcb_warp_pointer(conn
, XCB_NONE
, win
->frame
, 0, 0, 0, 0,
3564 WIDTH(win
) / 2, HEIGHT(win
) / 2);
3566 xcb_warp_pointer(conn
, XCB_NONE
, r
->id
, 0, 0, 0, 0,
3567 WIDTH(r
) / 2, HEIGHT(r
) / 2);
3571 root_to_region(xcb_window_t root
, int check
)
3574 struct swm_region
*r
= NULL
;
3576 xcb_query_pointer_reply_t
*qpr
;
3577 xcb_get_input_focus_reply_t
*gifr
;
3579 DNPRINTF(SWM_D_MISC
, "root_to_region: win %#x\n", root
);
3581 num_screens
= get_screen_count();
3582 for (i
= 0; i
< num_screens
; i
++)
3583 if (screens
[i
].root
== root
)
3586 if (check
& SWM_CK_REGION
)
3587 r
= screens
[i
].r_focus
;
3589 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
3590 /* Try to find an actively focused window */
3591 gifr
= xcb_get_input_focus_reply(conn
,
3592 xcb_get_input_focus(conn
), NULL
);
3594 cfw
= find_window(gifr
->focus
);
3595 if (cfw
&& cfw
->ws
->r
)
3602 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
3603 /* No region with an active focus; try to use pointer. */
3604 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
3605 screens
[i
].root
), NULL
);
3607 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
3608 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
3609 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3610 if (X(r
) <= qpr
->root_x
&&
3611 qpr
->root_x
< MAX_X(r
) &&
3612 Y(r
) <= qpr
->root_y
&&
3613 qpr
->root_y
< MAX_Y(r
))
3620 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
3621 r
= TAILQ_FIRST(&screens
[i
].rl
);
3623 DNPRINTF(SWM_D_MISC
, "root_to_region: idx: %d\n", get_region_index(r
));
3629 find_region(xcb_window_t id
)
3631 struct swm_region
*r
;
3634 num_screens
= get_screen_count();
3635 for (i
= 0; i
< num_screens
; i
++)
3636 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3644 find_bar(xcb_window_t id
)
3646 struct swm_region
*r
;
3649 num_screens
= get_screen_count();
3650 for (i
= 0; i
< num_screens
; i
++)
3651 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3652 if (r
->bar
&& r
->bar
->id
== id
)
3659 find_frame_window(xcb_window_t id
) {
3660 struct swm_region
*r
;
3664 num_screens
= get_screen_count();
3665 for (i
= 0; i
< num_screens
; i
++)
3666 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3667 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
3675 find_window(xcb_window_t id
)
3677 struct ws_win
*win
= NULL
;
3678 int i
, j
, num_screens
;
3679 xcb_query_tree_reply_t
*qtr
;
3681 DNPRINTF(SWM_D_MISC
, "find_window: id: %#x\n", id
);
3683 num_screens
= get_screen_count();
3684 for (i
= 0; i
< num_screens
; i
++)
3685 for (j
= 0; j
< workspace_limit
; j
++)
3686 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3687 if (id
== win
->id
|| id
== win
->frame
)
3691 /* If window isn't top-level, try to find managed ancestor. */
3692 qtr
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3694 if (qtr
->parent
!= XCB_WINDOW_NONE
&& qtr
->parent
!= qtr
->root
)
3695 win
= find_window(qtr
->parent
);
3699 DNPRINTF(SWM_D_MISC
, "find_window: found child %#x "
3700 "of %#x.\n", win
->id
, qtr
->parent
);
3710 find_unmanaged_window(xcb_window_t id
)
3713 int i
, j
, num_screens
;
3715 num_screens
= get_screen_count();
3716 for (i
= 0; i
< num_screens
; i
++)
3717 for (j
= 0; j
< workspace_limit
; j
++)
3718 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
3726 spawn(int ws_idx
, union arg
*args
, bool close_fd
)
3731 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3733 close(xcb_get_file_descriptor(conn
));
3735 if ((ret
= getenv("LD_PRELOAD"))) {
3736 if (asprintf(&ret
, "%s:%s", SWM_LIB
, ret
) == -1) {
3737 warn("spawn: asprintf LD_PRELOAD");
3740 setenv("LD_PRELOAD", ret
, 1);
3743 setenv("LD_PRELOAD", SWM_LIB
, 1);
3746 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3747 warn("spawn: asprintf SWM_WS");
3750 setenv("_SWM_WS", ret
, 1);
3754 if (asprintf(&ret
, "%d", getpid()) == -1) {
3755 warn("spawn: asprintf _SWM_PID");
3758 setenv("_SWM_PID", ret
, 1);
3762 if (setsid() == -1) {
3763 warn("spawn: setsid");
3769 * close stdin and stdout to prevent interaction between apps
3770 * and the baraction script
3771 * leave stderr open to record errors
3773 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3774 warn("spawn: open");
3777 dup2(fd
, STDIN_FILENO
);
3778 dup2(fd
, STDOUT_FILENO
);
3783 execvp(args
->argv
[0], args
->argv
);
3785 warn("spawn: execvp");
3790 kill_refs(struct ws_win
*win
)
3792 struct workspace
*ws
;
3794 int i
, j
, num_screens
;
3799 num_screens
= get_screen_count();
3800 for (i
= 0; i
< num_screens
; i
++) {
3801 for (j
= 0; j
< workspace_limit
; j
++) {
3802 ws
= &screens
[i
].ws
[j
];
3804 if (win
== ws
->focus
)
3806 if (win
== ws
->focus_prev
)
3807 ws
->focus_prev
= NULL
;
3808 if (win
== ws
->focus_pending
)
3809 ws
->focus_pending
= NULL
;
3810 if (win
== ws
->focus_raise
)
3811 ws
->focus_raise
= NULL
;
3814 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3815 if (win
== w
->focus_child
)
3816 w
->focus_child
= NULL
;
3822 validate_win(struct ws_win
*testwin
)
3825 struct workspace
*ws
;
3826 struct swm_region
*r
;
3827 int i
, x
, num_screens
;
3829 if (testwin
== NULL
)
3832 num_screens
= get_screen_count();
3833 for (i
= 0; i
< num_screens
; i
++)
3834 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3835 for (x
= 0; x
< workspace_limit
; x
++) {
3837 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3845 validate_ws(struct workspace
*testws
)
3847 struct swm_region
*r
;
3848 struct workspace
*ws
;
3849 int i
, x
, num_screens
;
3851 /* validate all ws */
3852 num_screens
= get_screen_count();
3853 for (i
= 0; i
< num_screens
; i
++)
3854 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3855 for (x
= 0; x
< workspace_limit
; x
++) {
3864 unfocus_win(struct ws_win
*win
)
3866 xcb_window_t none
= XCB_WINDOW_NONE
;
3868 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: win %#x\n", WINID(win
));
3873 if (win
->ws
== NULL
) {
3874 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3878 if (validate_ws(win
->ws
)) {
3879 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3883 if (win
->ws
->r
== NULL
) {
3884 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3888 if (validate_win(win
)) {
3889 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3894 if (win
->ws
->focus
== win
) {
3895 win
->ws
->focus
= NULL
;
3896 win
->ws
->focus_prev
= win
;
3897 if (win
->ws
->focus_raise
== win
&& !FLOATING(win
)) {
3898 update_win_stacking(win
);
3902 if (validate_win(win
->ws
->focus
)) {
3903 kill_refs(win
->ws
->focus
);
3904 win
->ws
->focus
= NULL
;
3907 if (validate_win(win
->ws
->focus_prev
)) {
3908 kill_refs(win
->ws
->focus_prev
);
3909 win
->ws
->focus_prev
= NULL
;
3914 /* Raise window to "top unfocused position." */
3915 if (win
->ws
->always_raise
)
3918 /* Update border width */
3919 if (win
->bordered
&& (win
->quirks
& SWM_Q_MINIMALBORDER
) &&
3922 X(win
) += border_width
;
3923 Y(win
) += border_width
;
3927 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3928 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3930 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3934 focus_win(struct ws_win
*win
)
3936 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
, *tmpw
;
3937 struct workspace
*ws
;
3938 xcb_get_input_focus_reply_t
*gifr
= NULL
;
3939 xcb_get_window_attributes_reply_t
*war
= NULL
;
3941 DNPRINTF(SWM_D_FOCUS
, "focus_win: win %#x\n", WINID(win
));
3943 if (win
== NULL
|| win
->ws
== NULL
|| !win
->mapped
)
3948 if (validate_ws(ws
))
3951 if (validate_win(win
)) {
3956 gifr
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3958 DNPRINTF(SWM_D_FOCUS
, "focus_win: cur focus: %#x\n",
3961 cfw
= find_window(gifr
->focus
);
3964 if (cfw
->ws
!= ws
&& cfw
->ws
->r
!= NULL
&&
3965 cfw
->frame
!= XCB_WINDOW_NONE
) {
3972 war
= xcb_get_window_attributes_reply(conn
,
3973 xcb_get_window_attributes(conn
, gifr
->focus
), NULL
);
3974 if (war
&& war
->override_redirect
&& ws
->focus
== win
) {
3975 DNPRINTF(SWM_D_FOCUS
, "focus_win: skip refocus "
3976 "from override_redirect.\n");
3982 if (ws
->focus
!= win
) {
3983 if (ws
->focus
&& ws
->focus
!= cfw
)
3984 unfocus_win(ws
->focus
);
3988 /* Clear focus child redirect. */
3989 win
->focus_child
= NULL
;
3991 /* If transient, adjust parent's focus child for focus_magic. */
3993 parent
= find_window(win
->transient
);
3994 if (parent
&& parent
->focus_child
!= win
)
3995 parent
->focus_child
= win
;
3998 /* Update window border even if workspace is hidden. */
4002 DNPRINTF(SWM_D_FOCUS
, "focus_win: already focused.\n");
4007 /* Set input focus if no input hint, or indicated by hint. */
4008 if (ACCEPTS_FOCUS(win
)) {
4009 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
4010 " revert-to: parent, time: %#x\n", win
->id
,
4012 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
4013 win
->id
, last_event_time
);
4014 } else if (!win
->take_focus
) {
4015 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
4016 " revert-to: parent, time: 0\n", ws
->r
->id
);
4017 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
4018 ws
->r
->id
, XCB_CURRENT_TIME
);
4021 /* Tell app it can adjust focus to a specific window. */
4022 if (win
->take_focus
) {
4023 /* java is special; always tell parent */
4024 if (TRANS(win
) && win
->java
)
4025 client_msg(parent
, a_takefocus
,
4028 client_msg(win
, a_takefocus
, last_event_time
);
4031 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
4033 /* If a parent exists, map it first. */
4035 raise_window(parent
);
4038 /* Map siblings next. */
4039 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
,
4041 if (w
!= win
&& !ICONIC(w
) &&
4042 w
->transient
== parent
->id
) {
4048 /* Map focused window. */
4052 /* Stack any children of focus window. */
4053 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
4054 if (w
->transient
== win
->id
&& !ICONIC(w
)) {
4058 } else if (tile_gap
< 0 && !ABOVE(win
)) {
4060 * Windows overlap in the layout.
4061 * Raise focused win above all tiled wins.
4069 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4070 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4073 bar_draw(ws
->r
->bar
);
4079 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
4082 /* If a child window should have focus instead, return it. */
4084 get_focus_magic(struct ws_win
*win
)
4086 struct ws_win
*parent
= NULL
;
4087 struct ws_win
*child
= NULL
;
4089 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: win %#x\n", WINID(win
));
4094 parent
= find_window(win
->transient
);
4096 /* If parent prefers focus elsewhere, then try to do so. */
4097 if (parent
&& (child
= parent
->focus_child
)) {
4098 if (validate_win(child
) == 0 && child
->mapped
)
4101 parent
->focus_child
= NULL
;
4105 /* If this window prefers focus elsewhere, then try to do so. */
4106 if ((child
= win
->focus_child
)) {
4107 if (validate_win(child
) == 0 && child
->mapped
)
4110 win
->focus_child
= NULL
;
4117 event_drain(uint8_t rt
)
4119 xcb_generic_event_t
*evt
;
4121 /* ensure all pending requests have been processed before filtering. */
4123 while ((evt
= get_next_event(false))) {
4124 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
4132 set_region(struct swm_region
*r
)
4134 struct swm_region
*rf
;
4141 /* Unfocus old region bar. */
4146 xcb_change_window_attributes(conn
, rf
->bar
->id
,
4147 XCB_CW_BORDER_PIXEL
,
4148 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
4151 if (rf
!= NULL
&& rf
!= r
&& (X(rf
) != X(r
) || Y(rf
) != Y(r
) ||
4152 WIDTH(rf
) != WIDTH(r
) || HEIGHT(rf
) != HEIGHT(r
))) {
4153 /* Set _NET_DESKTOP_GEOMETRY. */
4155 vals
[1] = HEIGHT(r
);
4156 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, r
->s
->root
,
4157 ewmh
[_NET_DESKTOP_GEOMETRY
].atom
, XCB_ATOM_CARDINAL
, 32, 2,
4161 /* Set region bar border to focus_color. */
4162 xcb_change_window_attributes(conn
, r
->bar
->id
,
4163 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
4167 /* Update the focus window frame on the now unfocused region. */
4168 if (rf
&& rf
->ws
->focus
)
4169 draw_frame(rf
->ws
->focus
);
4171 ewmh_update_current_desktop();
4175 focus_region(struct swm_region
*r
)
4178 struct swm_region
*old_r
;
4183 old_r
= r
->s
->r_focus
;
4186 nfw
= get_region_focus(r
);
4190 /* New region is empty; need to manually unfocus win. */
4192 unfocus_win(old_r
->ws
->focus
);
4193 /* Clear bar since empty. */
4194 bar_draw(old_r
->bar
);
4197 DNPRINTF(SWM_D_FOCUS
, "focus_region: set_input_focus: %#x, "
4198 "revert-to: parent, time: 0\n", r
->id
);
4199 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4206 switchws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4208 struct swm_region
*this_r
, *other_r
;
4210 struct workspace
*new_ws
, *old_ws
;
4211 xcb_window_t none
= XCB_WINDOW_NONE
;
4212 int wsid
= args
->id
;
4213 bool unmap_old
= false;
4218 if (wsid
>= workspace_limit
)
4222 old_ws
= this_r
->ws
;
4223 new_ws
= &this_r
->s
->ws
[wsid
];
4225 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
4226 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
4228 if (new_ws
== NULL
|| old_ws
== NULL
)
4230 if (new_ws
== old_ws
)
4233 other_r
= new_ws
->r
;
4234 if (other_r
&& workspace_clamp
&&
4235 bp
->action
!= FN_RG_MOVE_NEXT
&& bp
->action
!= FN_RG_MOVE_PREV
) {
4236 DNPRINTF(SWM_D_WS
, "switchws: ws clamped.\n");
4239 DNPRINTF(SWM_D_WS
, "switchws: warping focus to region "
4240 "with ws %d.\n", wsid
);
4241 focus_region(other_r
);
4242 center_pointer(other_r
);
4248 if ((win
= old_ws
->focus
) != NULL
) {
4251 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4252 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4257 /* the other ws is visible in another region, exchange them */
4258 other_r
->ws_prior
= new_ws
;
4259 other_r
->ws
= old_ws
;
4260 old_ws
->r
= other_r
;
4262 /* the other workspace is hidden, hide this one */
4267 this_r
->ws_prior
= old_ws
;
4268 this_r
->ws
= new_ws
;
4271 /* Set focus_pending before stacking, if needed. */
4272 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
4273 validate_win(new_ws
->focus_pending
))) {
4274 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
4275 new_ws
->focus
= new_ws
->focus_prev
;
4276 new_ws
->focus_prev
= NULL
;
4279 new_ws
->state
= SWM_WS_STATE_MAPPING
;
4284 /* unmap old windows */
4286 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
4288 old_ws
->state
= SWM_WS_STATE_HIDDEN
;
4291 /* if workspaces were swapped, then don't wait to set focus */
4292 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
4293 if (new_ws
->focus_pending
) {
4294 focus_win(new_ws
->focus_pending
);
4295 new_ws
->focus_pending
= NULL
;
4299 /* Clear bar and set focus on region input win if new ws is empty. */
4300 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
4301 DNPRINTF(SWM_D_FOCUS
, "switchws: set_input_focus: %#x, "
4302 "revert-to: parent, time: 0\n", r
->id
);
4303 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4308 ewmh_update_current_desktop();
4312 new_ws
->state
= SWM_WS_STATE_MAPPED
;
4314 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
4318 cyclews(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4321 struct swm_screen
*s
= r
->s
;
4322 bool cycle_all
= false, mv
= false;
4324 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4325 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4331 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
4334 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
4337 case SWM_ARG_ID_CYCLEWS_UP
:
4338 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
4340 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
4343 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
4346 case SWM_ARG_ID_CYCLEWS_DOWN
:
4347 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
4354 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
4356 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
4360 send_to_ws(bp
, r
, &a
);
4362 switchws(bp
, r
, &a
);
4363 } while (a
.id
!= r
->ws
->idx
);
4365 DNPRINTF(SWM_D_FOCUS
, "cyclews: done\n");
4369 priorws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4375 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4376 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4378 if (r
->ws_prior
== NULL
)
4381 a
.id
= r
->ws_prior
->idx
;
4382 switchws(bp
, r
, &a
);
4383 DNPRINTF(SWM_D_FOCUS
, "priorws: done\n");
4387 focusrg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4389 int ridx
= args
->id
, i
, num_screens
;
4390 struct swm_region
*rr
= NULL
;
4394 num_screens
= get_screen_count();
4395 /* do nothing if we don't have more than one screen */
4396 if (!(num_screens
> 1 || outputs
> 1))
4399 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
4401 rr
= TAILQ_FIRST(&r
->s
->rl
);
4402 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
4403 rr
= TAILQ_NEXT(rr
, entry
);
4411 DNPRINTF(SWM_D_FOCUS
, "focusrg: done\n");
4415 cyclerg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4418 struct swm_region
*rr
= NULL
;
4421 num_screens
= get_screen_count();
4422 /* do nothing if we don't have more than one screen */
4423 if (!(num_screens
> 1 || outputs
> 1))
4427 DNPRINTF(SWM_D_FOCUS
, "cyclerg: id: %d, region: %d\n", args
->id
, i
);
4430 case SWM_ARG_ID_CYCLERG_UP
:
4431 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4432 rr
= TAILQ_NEXT(r
, entry
);
4434 rr
= TAILQ_FIRST(&screens
[i
].rl
);
4436 case SWM_ARG_ID_CYCLERG_DOWN
:
4437 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4438 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
4440 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
4449 case SWM_ARG_ID_CYCLERG_UP
:
4450 case SWM_ARG_ID_CYCLERG_DOWN
:
4455 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4456 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4458 switchws(bp
, r
, &a
);
4464 DNPRINTF(SWM_D_FOCUS
, "cyclerg: done\n");
4467 /* Sorts transients after parent. */
4469 sort_windows(struct ws_win_list
*wl
)
4471 struct ws_win
*win
, *parent
, *nxt
;
4476 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
4477 nxt
= TAILQ_NEXT(win
, entry
);
4479 parent
= find_window(win
->transient
);
4480 if (parent
== NULL
) {
4481 warnx("not possible bug");
4484 TAILQ_REMOVE(wl
, win
, entry
);
4485 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
4491 swapwin(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4493 struct ws_win
*target
, *source
;
4494 struct ws_win
*cur_focus
;
4495 struct ws_win_list
*wl
;
4499 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4500 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4502 cur_focus
= r
->ws
->focus
;
4503 if (cur_focus
== NULL
|| FULLSCREEN(cur_focus
))
4506 /* Adjust stacking in floating layer. */
4507 if (ABOVE(cur_focus
)) {
4509 case SWM_ARG_ID_SWAPPREV
:
4510 target
= TAILQ_PREV(cur_focus
, ws_win_stack
,
4512 if (target
!= NULL
&& FLOATING(target
)) {
4513 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4515 TAILQ_INSERT_BEFORE(target
, cur_focus
,
4517 update_win_stacking(cur_focus
);
4521 case SWM_ARG_ID_SWAPNEXT
:
4522 target
= TAILQ_NEXT(cur_focus
, stack_entry
);
4523 if (target
!= NULL
&& FLOATING(target
)) {
4524 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4526 TAILQ_INSERT_AFTER(&cur_focus
->ws
->stack
,
4527 target
, cur_focus
, stack_entry
);
4528 update_win_stacking(cur_focus
);
4536 if (r
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4539 clear_maximized(r
->ws
);
4542 wl
= &source
->ws
->winlist
;
4545 case SWM_ARG_ID_SWAPPREV
:
4547 source
= find_window(source
->transient
);
4548 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
4549 if (target
&& target
->transient
)
4550 target
= find_window(target
->transient
);
4551 TAILQ_REMOVE(wl
, source
, entry
);
4553 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4555 TAILQ_INSERT_BEFORE(target
, source
, entry
);
4557 case SWM_ARG_ID_SWAPNEXT
:
4558 target
= TAILQ_NEXT(source
, entry
);
4559 /* move the parent and let the sort handle the move */
4561 source
= find_window(source
->transient
);
4562 TAILQ_REMOVE(wl
, source
, entry
);
4564 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4566 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
4568 case SWM_ARG_ID_SWAPMAIN
:
4569 target
= TAILQ_FIRST(wl
);
4570 if (target
== source
) {
4571 if (source
->ws
->focus_prev
!= NULL
&&
4572 source
->ws
->focus_prev
!= target
)
4573 source
= source
->ws
->focus_prev
;
4577 if (target
== NULL
|| source
== NULL
)
4579 source
->ws
->focus_prev
= target
;
4580 TAILQ_REMOVE(wl
, target
, entry
);
4581 TAILQ_INSERT_BEFORE(source
, target
, entry
);
4582 TAILQ_REMOVE(wl
, source
, entry
);
4583 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4585 case SWM_ARG_ID_MOVELAST
:
4586 TAILQ_REMOVE(wl
, source
, entry
);
4587 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4590 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
4595 ewmh_update_client_list();
4601 DNPRINTF(SWM_D_MOVE
, "swapwin: done\n");
4605 get_focus_prev(struct ws_win
*win
)
4607 struct ws_win
*winfocus
= NULL
;
4608 struct ws_win
*cur_focus
= NULL
;
4609 struct ws_win_list
*wl
= NULL
;
4610 struct workspace
*ws
= NULL
;
4612 if (!(win
&& win
->ws
))
4617 cur_focus
= ws
->focus
;
4619 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: win %#x, cur_focus: %#x, "
4620 "focus_prev: %#x\n", WINID(win
), WINID(cur_focus
),
4621 WINID(ws
->focus_prev
));
4623 /* pickle, just focus on whatever */
4624 if (cur_focus
== NULL
) {
4625 /* use prev_focus if valid */
4626 if (ws
->focus_prev
&& find_window(ws
->focus_prev
->id
))
4627 winfocus
= ws
->focus_prev
;
4631 /* if transient focus on parent */
4632 if (TRANS(cur_focus
)) {
4633 winfocus
= find_window(cur_focus
->transient
);
4637 /* if in max_stack try harder */
4638 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
4639 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
4640 if (cur_focus
!= ws
->focus_prev
)
4641 winfocus
= ws
->focus_prev
;
4643 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
4648 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
4650 if (winfocus
== NULL
|| winfocus
== win
) {
4651 switch (focus_close
) {
4652 case SWM_STACK_BOTTOM
:
4653 TAILQ_FOREACH(winfocus
, wl
, entry
)
4654 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4658 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4659 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4662 case SWM_STACK_ABOVE
:
4663 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
4664 while (winfocus
&& ICONIC(winfocus
))
4665 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4667 if (winfocus
== NULL
) {
4668 if (focus_close_wrap
) {
4669 TAILQ_FOREACH(winfocus
, wl
, entry
)
4670 if (!ICONIC(winfocus
) &&
4671 winfocus
!= cur_focus
)
4674 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4676 if (!ICONIC(winfocus
) &&
4677 winfocus
!= cur_focus
)
4682 case SWM_STACK_BELOW
:
4683 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
4684 while (winfocus
&& ICONIC(winfocus
))
4685 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
4688 if (winfocus
== NULL
) {
4689 if (focus_close_wrap
) {
4690 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4692 if (!ICONIC(winfocus
) &&
4693 winfocus
!= cur_focus
)
4696 TAILQ_FOREACH(winfocus
, wl
, entry
)
4697 if (!ICONIC(winfocus
) &&
4698 winfocus
!= cur_focus
)
4706 if (winfocus
== NULL
||
4707 (winfocus
&& (ICONIC(winfocus
) || winfocus
== cur_focus
))) {
4708 if (focus_default
== SWM_STACK_TOP
) {
4709 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4710 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4713 TAILQ_FOREACH(winfocus
, wl
, entry
)
4714 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4721 return get_focus_magic(winfocus
);
4725 get_region_focus(struct swm_region
*r
)
4727 struct ws_win
*winfocus
= NULL
;
4732 if (r
->ws
->focus
&& !ICONIC(r
->ws
->focus
))
4733 winfocus
= r
->ws
->focus
;
4734 else if (r
->ws
->focus_prev
&& !ICONIC(r
->ws
->focus_prev
))
4735 winfocus
= r
->ws
->focus_prev
;
4737 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
4738 if (!ICONIC(winfocus
))
4741 return get_focus_magic(winfocus
);
4745 focus(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4747 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
4748 struct ws_win_list
*wl
= NULL
;
4749 struct workspace
*ws
= NULL
;
4756 cur_focus
= r
->ws
->focus
;
4760 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d, cur_focus: %#x\n", args
->id
,
4763 /* Make sure an uniconified window has focus, if one exists. */
4764 if (cur_focus
== NULL
) {
4765 cur_focus
= TAILQ_FIRST(wl
);
4766 while (cur_focus
!= NULL
&& ICONIC(cur_focus
))
4767 cur_focus
= TAILQ_NEXT(cur_focus
, entry
);
4769 DNPRINTF(SWM_D_FOCUS
, "focus: new cur_focus: %#x\n",
4774 case SWM_ARG_ID_FOCUSPREV
:
4775 if (cur_focus
== NULL
)
4778 winfocus
= cur_focus
;
4780 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
4781 if (winfocus
== NULL
)
4782 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
4783 if (winfocus
== cur_focus
)
4785 } while (winfocus
&& (ICONIC(winfocus
) ||
4786 winfocus
->id
== cur_focus
->transient
||
4787 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4788 winfocus
->transient
== cur_focus
->transient
) ||
4789 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4791 case SWM_ARG_ID_FOCUSNEXT
:
4792 if (cur_focus
== NULL
)
4795 winfocus
= cur_focus
;
4797 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4798 if (winfocus
== NULL
)
4799 winfocus
= TAILQ_FIRST(wl
);
4800 if (winfocus
== cur_focus
)
4802 } while (winfocus
&& (ICONIC(winfocus
) ||
4803 winfocus
->id
== cur_focus
->transient
||
4804 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4805 winfocus
->transient
== cur_focus
->transient
) ||
4806 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4808 case SWM_ARG_ID_FOCUSMAIN
:
4809 if (cur_focus
== NULL
)
4812 winfocus
= TAILQ_FIRST(wl
);
4813 if (winfocus
== cur_focus
)
4814 winfocus
= cur_focus
->ws
->focus_prev
;
4816 case SWM_ARG_ID_FOCUSURGENT
:
4817 /* Search forward for the next urgent window. */
4821 for (i
= 0; i
<= workspace_limit
; ++i
) {
4823 head
= TAILQ_FIRST(&r
->s
->ws
[(ws
->idx
+ i
) %
4824 workspace_limit
].winlist
);
4827 if (head
== cur_focus
) {
4829 winfocus
= cur_focus
;
4832 } else if (get_urgent(head
)) {
4837 head
= TAILQ_NEXT(head
, entry
);
4844 /* Switch ws if new focus is on a different ws. */
4845 if (winfocus
&& winfocus
->ws
!= ws
) {
4846 a
.id
= winfocus
->ws
->idx
;
4847 switchws(bp
, r
, &a
);
4854 if (clear_maximized(ws
) > 0)
4857 focus_win(get_focus_magic(winfocus
));
4862 DNPRINTF(SWM_D_FOCUS
, "focus: done\n");
4866 focus_pointer(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4870 /* Not needed for buttons since this is already done in buttonpress. */
4871 if (bp
->type
== KEYBIND
) {
4872 focus_win(get_pointer_win(r
->s
->root
));
4878 cycle_layout(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4880 struct workspace
*ws
= r
->ws
;
4882 /* suppress unused warning since var is needed */
4886 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
4889 if (ws
->cur_layout
->l_stack
== NULL
)
4890 ws
->cur_layout
= &layouts
[0];
4892 clear_maximized(ws
);
4897 focus_win(get_region_focus(r
));
4904 stack_config(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4906 struct workspace
*ws
= r
->ws
;
4910 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
4913 if (clear_maximized(ws
) > 0)
4916 if (ws
->cur_layout
->l_config
!= NULL
)
4917 ws
->cur_layout
->l_config(ws
, args
->id
);
4919 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
4928 stack(struct swm_region
*r
) {
4929 struct swm_geometry g
;
4930 struct swm_region
*r_prev
;
4936 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
4938 /* Adjust stack area for region bar and padding. */
4940 g
.x
+= region_padding
;
4941 g
.y
+= region_padding
;
4942 g
.w
-= 2 * border_width
+ 2 * region_padding
;
4943 g
.h
-= 2 * border_width
+ 2 * region_padding
;
4944 if (bar_enabled
&& r
->ws
->bar_enabled
) {
4950 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d (screen: "
4951 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4952 r
->ws
->idx
, r
->s
->idx
, get_region_index(r
), g
.x
, g
.y
, g
.w
, g
.h
);
4954 r_prev
= TAILQ_PREV(r
, swm_region_list
, entry
);
4956 /* Stack bar/input relative to prev. region. */
4957 val
[1] = XCB_STACK_MODE_ABOVE
;
4959 val
[0] = r_prev
->id
;
4960 DNPRINTF(SWM_D_STACK
, "stack: region input %#x "
4961 "relative to %#x.\n", r
->id
, val
[0]);
4962 xcb_configure_window(conn
, r
->id
,
4963 XCB_CONFIG_WINDOW_SIBLING
|
4964 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4966 val
[0] = r_prev
->bar
->id
;
4967 DNPRINTF(SWM_D_STACK
, "stack: region bar %#x "
4968 "relative to %#x.\n", r
->bar
->id
, val
[0]);
4969 xcb_configure_window(conn
, r
->bar
->id
,
4970 XCB_CONFIG_WINDOW_SIBLING
|
4971 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4974 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
4975 r
->ws
->cur_layout
->l_string(r
->ws
);
4976 /* save r so we can track region changes */
4982 DNPRINTF(SWM_D_STACK
, "stack: end\n");
4986 store_float_geom(struct ws_win
*win
)
4988 if (win
== NULL
|| win
->ws
->r
== NULL
)
4991 /* retain window geom and region geom */
4992 win
->g_float
= win
->g
;
4993 win
->g_float
.x
-= X(win
->ws
->r
);
4994 win
->g_float
.y
-= Y(win
->ws
->r
);
4995 win
->g_floatvalid
= true;
4996 DNPRINTF(SWM_D_MISC
, "store_float_geom: win %#x, g: (%d,%d)"
4997 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
4998 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
4999 win
->g_float
.w
, win
->g_float
.h
);
5003 load_float_geom(struct ws_win
*win
)
5005 if (win
== NULL
|| win
->ws
->r
== NULL
)
5008 if (win
->g_floatvalid
) {
5009 win
->g
= win
->g_float
;
5010 X(win
) += X(win
->ws
->r
);
5011 Y(win
) += Y(win
->ws
->r
);
5012 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g: (%d,%d)"
5013 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
5016 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g_float "
5017 "is not set.\n", win
->id
);
5022 update_floater(struct ws_win
*win
)
5024 struct workspace
*ws
;
5025 struct swm_region
*r
;
5027 DNPRINTF(SWM_D_MISC
, "update_floater: win %#x\n", WINID(win
));
5034 if ((r
= ws
->r
) == NULL
)
5037 win
->bordered
= true;
5039 if (FULLSCREEN(win
)) {
5040 /* _NET_WM_FULLSCREEN: fullscreen without border. */
5041 if (!win
->g_floatvalid
)
5042 store_float_geom(win
);
5045 win
->bordered
= false;
5046 } else if (MAXIMIZED(win
)) {
5047 /* Maximize: like a single stacked window. */
5048 if (!win
->g_floatvalid
)
5049 store_float_geom(win
);
5053 if (bar_enabled
&& ws
->bar_enabled
&& !maximize_hide_bar
) {
5055 Y(win
) += bar_height
;
5056 HEIGHT(win
) -= bar_height
;
5057 } else if (disable_border
) {
5058 win
->bordered
= false;
5061 if (win
->bordered
) {
5062 /* Window geometry excludes frame. */
5063 X(win
) += border_width
;
5064 Y(win
) += border_width
;
5065 HEIGHT(win
) -= 2 * border_width
;
5066 WIDTH(win
) -= 2 * border_width
;
5069 /* Normal floating window. */
5070 /* Update geometry if new region. */
5072 load_float_geom(win
);
5074 if (((win
->quirks
& SWM_Q_FULLSCREEN
) &&
5075 WIDTH(win
) >= WIDTH(r
) && HEIGHT(win
) >= HEIGHT(r
)) ||
5076 ((!WS_FOCUSED(win
->ws
) || win
->ws
->focus
!= win
) &&
5077 (win
->quirks
& SWM_Q_MINIMALBORDER
))) {
5079 win
->bordered
= false;
5080 } else if (!MANUAL(win
)) {
5081 if (TRANS(win
) && (win
->quirks
& SWM_Q_TRANSSZ
)) {
5082 /* Adjust size on TRANSSZ quirk. */
5083 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
5084 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
5087 if (!(win
->quirks
& SWM_Q_ANYWHERE
)) {
5089 * Floaters and transients are auto-centred
5090 * unless manually moved, resized or ANYWHERE
5093 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2;
5094 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2;
5095 store_float_geom(win
);
5100 /* Ensure at least 1 pixel of the window is in the region. */
5101 region_containment(win
, r
, SWM_CW_ALLSIDES
);
5107 * Send keystrokes to terminal to decrease/increase the font size as the
5108 * window size changes.
5111 adjust_font(struct ws_win
*win
)
5113 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
5114 ABOVE(win
) || TRANS(win
))
5117 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
5118 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
5119 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
5120 win
->font_size_boundary
[win
->font_steps
] =
5121 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
5124 win
->last_inc
= win
->sh
.width_inc
;
5125 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
5126 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
5127 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
5130 win
->last_inc
= win
->sh
.width_inc
;
5131 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
5135 #define SWAPXY(g) do { \
5137 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
5138 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
5141 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, bool flip
)
5143 struct swm_geometry cell
, r_g
= *g
;
5145 int i
= 0, j
= 0, s
= 0, stacks
= 0;
5146 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
5147 int hrh
= 0, extra
= 0, h_slice
= 0, last_h
= 0;
5148 int split
= 0, colno
= 0;
5149 int winno
, mwin
= 0, msize
= 0;
5150 int remain
, missing
, v_slice
, mscale
;
5151 bool bordered
= true, reconfigure
= false;
5154 * cell: geometry for window, including frame.
5155 * mwin: # of windows in master area.
5156 * mscale: size increment of master area.
5157 * stacks: # of stack columns
5160 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
5161 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
5163 memset(&cell
, 0, sizeof(cell
));
5165 /* Prepare tiling variables, if needed. */
5166 if ((winno
= count_win(ws
, false)) > 0) {
5167 /* Find first tiled window. */
5168 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
5169 if (!FLOATING(win
) && !ICONIC(win
))
5172 /* Take into account size hints of first tiled window. */
5174 w_inc
= win
->sh
.width_inc
;
5175 w_base
= win
->sh
.base_width
;
5176 mwin
= ws
->l_state
.horizontal_mwin
;
5177 mscale
= ws
->l_state
.horizontal_msize
;
5178 stacks
= ws
->l_state
.horizontal_stacks
;
5181 w_inc
= win
->sh
.height_inc
;
5182 w_base
= win
->sh
.base_height
;
5183 mwin
= ws
->l_state
.vertical_mwin
;
5184 mscale
= ws
->l_state
.vertical_msize
;
5185 stacks
= ws
->l_state
.vertical_stacks
;
5189 cell
.x
+= border_width
;
5190 cell
.y
+= border_width
;
5192 if (stacks
> winno
- mwin
)
5193 stacks
= winno
- mwin
;
5197 h_slice
= r_g
.h
/ SWM_H_SLICE
;
5198 if (mwin
&& winno
> mwin
) {
5199 v_slice
= r_g
.w
/ SWM_V_SLICE
;
5203 cell
.w
= v_slice
* mscale
;
5205 if (w_inc
> 1 && w_inc
< v_slice
) {
5206 /* Adjust for requested size increment. */
5207 remain
= (cell
.w
- w_base
) % w_inc
;
5213 cell
.x
+= r_g
.w
- msize
;
5216 colno
= split
= winno
/ stacks
;
5217 cell
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
5218 2 * border_width
) / stacks
);
5221 hrh
= r_g
.h
/ colno
;
5222 extra
= r_g
.h
- (colno
* hrh
);
5223 cell
.h
= hrh
- 2 * border_width
;
5224 i
= j
= 0, s
= stacks
;
5227 /* Update window geometry. */
5228 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
5232 if (FLOATING(win
)) {
5233 update_floater(win
);
5238 if (split
&& i
== split
) {
5239 colno
= (winno
- mwin
) / stacks
;
5240 if (s
<= (winno
- mwin
) % stacks
)
5243 hrh
= r_g
.h
/ colno
;
5244 extra
= r_g
.h
- (colno
* hrh
);
5247 cell
.x
+= cell
.w
+ 2 * border_width
+
5250 cell
.w
= (r_g
.w
- msize
-
5251 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
5253 cell
.w
+= (r_g
.w
- msize
-
5254 (stacks
* (2 * border_width
+ tile_gap
))) %
5258 cell
.x
-= cell
.w
+ 2 * border_width
+
5264 cell
.h
= hrh
- 2 * border_width
- tile_gap
;
5267 h_inc
= win
->sh
.width_inc
;
5268 h_base
= win
->sh
.base_width
;
5270 h_inc
= win
->sh
.height_inc
;
5271 h_base
= win
->sh
.base_height
;
5274 if (j
== colno
- 1) {
5275 cell
.h
= hrh
+ extra
;
5276 } else if (h_inc
> 1 && h_inc
< h_slice
) {
5277 /* adjust for window's requested size increment */
5278 remain
= (cell
.h
- h_base
) % h_inc
;
5279 missing
= h_inc
- remain
;
5281 if (missing
<= extra
|| j
== 0) {
5291 cell
.y
= r_g
.y
+ border_width
;
5293 cell
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
5295 /* Window coordinates exclude frame. */
5297 if (winno
> 1 || !disable_border
||
5298 (bar_enabled
&& ws
->bar_enabled
)) {
5305 if (X(win
) != cell
.y
|| Y(win
) != cell
.x
||
5306 WIDTH(win
) != cell
.h
|| HEIGHT(win
) != cell
.w
) {
5310 WIDTH(win
) = cell
.h
;
5311 HEIGHT(win
) = cell
.w
;
5314 if (X(win
) != cell
.x
|| Y(win
) != cell
.y
||
5315 WIDTH(win
) != cell
.w
|| HEIGHT(win
) != cell
.h
) {
5319 WIDTH(win
) = cell
.w
;
5320 HEIGHT(win
) = cell
.h
;
5325 X(win
) -= border_width
;
5326 Y(win
) -= border_width
;
5327 WIDTH(win
) += 2 * border_width
;
5328 HEIGHT(win
) += 2 * border_width
;
5331 if (bordered
!= win
->bordered
) {
5333 win
->bordered
= bordered
;
5346 /* Stack all windows from bottom up. */
5347 TAILQ_FOREACH_REVERSE(win
, &ws
->stack
, ws_win_stack
, stack_entry
)
5349 update_win_stacking(win
);
5351 /* Map all windows from top down. */
5352 TAILQ_FOREACH(win
, &ws
->stack
, stack_entry
)
5356 DNPRINTF(SWM_D_STACK
, "stack_master: done\n");
5360 vertical_config(struct workspace
*ws
, int id
)
5362 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
5366 case SWM_ARG_ID_STACKRESET
:
5367 case SWM_ARG_ID_STACKINIT
:
5368 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
5369 ws
->l_state
.vertical_mwin
= 1;
5370 ws
->l_state
.vertical_stacks
= 1;
5372 case SWM_ARG_ID_MASTERSHRINK
:
5373 if (ws
->l_state
.vertical_msize
> 1)
5374 ws
->l_state
.vertical_msize
--;
5376 case SWM_ARG_ID_MASTERGROW
:
5377 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
5378 ws
->l_state
.vertical_msize
++;
5380 case SWM_ARG_ID_MASTERADD
:
5381 ws
->l_state
.vertical_mwin
++;
5383 case SWM_ARG_ID_MASTERDEL
:
5384 if (ws
->l_state
.vertical_mwin
> 0)
5385 ws
->l_state
.vertical_mwin
--;
5387 case SWM_ARG_ID_STACKBALANCE
:
5388 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ (ws
->l_state
.vertical_stacks
+ 1);
5390 case SWM_ARG_ID_STACKINC
:
5391 ws
->l_state
.vertical_stacks
++;
5393 case SWM_ARG_ID_STACKDEC
:
5394 if (ws
->l_state
.vertical_stacks
> 1)
5395 ws
->l_state
.vertical_stacks
--;
5397 case SWM_ARG_ID_FLIPLAYOUT
:
5398 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
5406 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5408 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
5410 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
5414 horizontal_config(struct workspace
*ws
, int id
)
5416 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
5419 case SWM_ARG_ID_STACKRESET
:
5420 case SWM_ARG_ID_STACKINIT
:
5421 ws
->l_state
.horizontal_mwin
= 1;
5422 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
5423 ws
->l_state
.horizontal_stacks
= 1;
5425 case SWM_ARG_ID_MASTERSHRINK
:
5426 if (ws
->l_state
.horizontal_msize
> 1)
5427 ws
->l_state
.horizontal_msize
--;
5429 case SWM_ARG_ID_MASTERGROW
:
5430 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
5431 ws
->l_state
.horizontal_msize
++;
5433 case SWM_ARG_ID_MASTERADD
:
5434 ws
->l_state
.horizontal_mwin
++;
5436 case SWM_ARG_ID_MASTERDEL
:
5437 if (ws
->l_state
.horizontal_mwin
> 0)
5438 ws
->l_state
.horizontal_mwin
--;
5440 case SWM_ARG_ID_STACKBALANCE
:
5441 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ (ws
->l_state
.horizontal_stacks
+ 1);
5443 case SWM_ARG_ID_STACKINC
:
5444 ws
->l_state
.horizontal_stacks
++;
5446 case SWM_ARG_ID_STACKDEC
:
5447 if (ws
->l_state
.horizontal_stacks
> 1)
5448 ws
->l_state
.horizontal_stacks
--;
5450 case SWM_ARG_ID_FLIPLAYOUT
:
5451 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
5459 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5461 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
5463 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
5466 /* fullscreen view */
5468 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5470 struct swm_geometry gg
= *g
;
5471 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
, *tmpw
;
5474 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
5479 winno
= count_win(ws
, false);
5480 if (winno
== 0 && count_win(ws
, true) == 0)
5483 /* Figure out which top level window should be visible. */
5484 if (ws
->focus_pending
)
5485 win
= ws
->focus_pending
;
5488 else if (ws
->focus_prev
)
5489 win
= ws
->focus_prev
;
5491 win
= TAILQ_FIRST(&ws
->winlist
);
5493 DNPRINTF(SWM_D_STACK
, "max_stack: focus_pending: %#x, focus: %#x, "
5494 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws
->focus_pending
),
5495 WINID(ws
->focus
), WINID(ws
->focus_prev
),
5496 WINID(TAILQ_FIRST(&ws
->winlist
)), win
->id
);
5498 /* Update window geometry. */
5499 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5508 /* Set maximized flag for all maxed windows. */
5509 if (!MAXIMIZED(w
)) {
5510 /* Preserve floating geometry. */
5512 store_float_geom(w
);
5514 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_MAXIMIZED
);
5515 ewmh_update_wm_state(w
);
5518 /* Only reconfigure if necessary. */
5519 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
5520 HEIGHT(w
) != gg
.h
) {
5523 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
)) {
5524 w
->bordered
= false;
5525 WIDTH(w
) += 2 * border_width
;
5526 HEIGHT(w
) += 2 * border_width
;
5529 X(w
) += border_width
;
5530 Y(w
) += border_width
;
5537 /* If transient, stack parent and its children. */
5538 if (TRANS(win
) && (parent
= find_window(win
->transient
))) {
5539 raise_window(parent
);
5541 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5542 if (w
->transient
== parent
->id
)
5546 /* Make sure focus window is on top. */
5549 /* Stack any children of focus window. */
5550 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5551 if (w
->transient
== win
->id
)
5554 /* Map all windows. */
5555 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
5561 send_to_rg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5563 int ridx
= args
->id
, i
, num_screens
;
5564 struct swm_region
*rr
= NULL
;
5567 num_screens
= get_screen_count();
5568 /* do nothing if we don't have more than one screen */
5569 if (!(num_screens
> 1 || outputs
> 1))
5572 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
5574 rr
= TAILQ_FIRST(&r
->s
->rl
);
5575 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
5576 rr
= TAILQ_NEXT(rr
, entry
);
5583 send_to_ws(bp
, r
, &a
);
5587 region_under(struct swm_screen
*s
, int x
, int y
)
5589 struct swm_region
*r
= NULL
;
5591 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
5592 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
5593 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
5594 WIDTH(r
), HEIGHT(r
), x
, y
);
5595 if (X(r
) <= x
&& x
< MAX_X(r
))
5596 if (Y(r
) <= y
&& y
< MAX_Y(r
))
5603 /* Transfer focused window to target workspace and focus. */
5605 send_to_ws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5607 int wsid
= args
->id
;
5608 struct ws_win
*win
= NULL
;
5612 if (r
&& r
->ws
&& r
->ws
->focus
)
5617 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win %#x, ws %d\n", win
->id
, wsid
);
5619 if (wsid
< 0 || wsid
>= workspace_limit
)
5622 if (win
->ws
->idx
== wsid
)
5625 win_to_ws(win
, wsid
, true);
5627 /* Set new focus on target ws. */
5628 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5629 win
->ws
->focus_prev
= win
->ws
->focus
;
5630 win
->ws
->focus
= win
;
5631 win
->ws
->focus_pending
= NULL
;
5633 if (win
->ws
->focus_prev
)
5634 draw_frame(win
->ws
->focus_prev
);
5637 DNPRINTF(SWM_D_STACK
, "send_to_ws: focus_pending: %#x, focus: %#x, "
5638 "focus_prev: %#x, first: %#x, win: %#x\n",
5639 WINID(r
->ws
->focus_pending
), WINID(r
->ws
->focus
),
5640 WINID(r
->ws
->focus_prev
), WINID(TAILQ_FIRST(&r
->ws
->winlist
)),
5643 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5644 ewmh_update_wm_state(win
);
5646 /* Restack focused region. */
5649 /* If destination ws has a region, restack. */
5652 load_float_geom(win
);
5657 /* Set new focus on current ws. */
5658 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5659 if (r
->ws
->focus
!= NULL
) {
5660 focus_win(r
->ws
->focus
);
5662 DNPRINTF(SWM_D_FOCUS
, "send_to_ws: set_input_focus: "
5663 "%#x, revert-to: parent, time: 0\n", r
->id
);
5664 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
5665 r
->id
, XCB_CURRENT_TIME
);
5674 /* Transfer focused window to region-relative workspace and focus. */
5676 send_to_rg_relative(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5679 struct swm_region
*r_other
;
5681 if (args
->id
== 1) {
5682 r_other
= TAILQ_NEXT(r
, entry
);
5683 if (r_other
== NULL
)
5684 r_other
= TAILQ_FIRST(&r
->s
->rl
);
5686 r_other
= TAILQ_PREV(r
, swm_region_list
, entry
);
5687 if (r_other
== NULL
)
5688 r_other
= TAILQ_LAST(&r
->s
->rl
, swm_region_list
);
5691 /* Map relative to absolute */
5693 args_abs
.id
= r_other
->ws
->idx
;
5695 send_to_ws(bp
, r
, &args_abs
);
5699 win_to_ws(struct ws_win
*win
, int wsid
, bool unfocus
)
5701 struct ws_win
*parent
;
5702 struct workspace
*ws
, *nws
, *pws
;
5704 if (wsid
< 0 || wsid
>= workspace_limit
)
5707 if (win
->ws
->idx
== wsid
)
5711 nws
= &win
->s
->ws
[wsid
];
5713 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win %#x, ws %d -> %d\n", win
->id
,
5716 /* Cleanup focus on source ws. */
5717 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
5718 (ws
->focus
== win
|| ws
->focus_pending
== win
))
5719 ws
->focus_pending
= get_focus_prev(win
);
5721 /* Move the parent if this is a transient window. */
5723 parent
= find_window(win
->transient
);
5726 /* Set new focus in parent's ws if needed. */
5727 if (pws
->focus
== parent
) {
5728 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5729 pws
->focus_pending
=
5730 get_focus_prev(parent
);
5732 unfocus_win(parent
);
5734 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5735 pws
->focus
= pws
->focus_pending
;
5736 pws
->focus_pending
= NULL
;
5740 /* Don't unmap parent if new ws is visible */
5742 unmap_window(parent
);
5745 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
5746 TAILQ_REMOVE(&ws
->stack
, parent
, stack_entry
);
5747 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
5748 TAILQ_INSERT_TAIL(&nws
->stack
, parent
, stack_entry
);
5751 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5752 "_NET_WM_DESKTOP: %d\n", wsid
);
5753 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5754 parent
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
5755 XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5762 if (ws
->focus_prev
== win
)
5763 ws
->focus_prev
= NULL
;
5765 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& ws
->focus_pending
!= NULL
) {
5766 ws
->focus
= ws
->focus_pending
;
5767 ws
->focus_pending
= NULL
;
5770 /* Don't unmap if new ws is visible */
5775 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
5776 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
5777 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
5778 TAILQ_INSERT_TAIL(&nws
->stack
, win
, stack_entry
);
5781 /* Update the window's workspace property: _NET_WM_DESKTOP */
5782 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5783 "_NET_WM_DESKTOP: %d\n", wsid
);
5784 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
5785 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5787 ewmh_update_client_list();
5789 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
5793 pressbutton(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5795 /* suppress unused warning since var is needed */
5799 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
5800 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5801 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
5802 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5806 raise_focus(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5811 /* Suppress warning. */
5815 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
5819 r
->ws
->focus_raise
= win
;
5822 /* Temporarily override stacking order also in the stack */
5823 if (!FLOATING(win
)) {
5824 val
= XCB_STACK_MODE_ABOVE
;
5825 xcb_configure_window(conn
, win
->frame
,
5826 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
5831 raise_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5833 /* Suppress warning. */
5837 if (r
== NULL
|| r
->ws
== NULL
)
5840 if (r
->ws
->focus
&& MAXIMIZED(r
->ws
->focus
))
5843 r
->ws
->always_raise
= !r
->ws
->always_raise
;
5845 /* Update focused win stacking order based on new always_raise value. */
5846 raise_window(r
->ws
->focus
);
5852 iconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5856 /* Suppress warning. */
5860 if ((w
= r
->ws
->focus
) == NULL
)
5863 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_HIDDEN
);
5864 ewmh_update_wm_state(w
);
5872 get_win_name(xcb_window_t win
)
5875 xcb_get_property_cookie_t c
;
5876 xcb_get_property_reply_t
*r
;
5878 /* First try _NET_WM_NAME for UTF-8. */
5879 c
= xcb_get_property(conn
, 0, win
, ewmh
[_NET_WM_NAME
].atom
,
5880 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5881 r
= xcb_get_property_reply(conn
, c
, NULL
);
5882 if (r
&& r
->type
== XCB_NONE
) {
5884 /* Use WM_NAME instead; no UTF-8. */
5885 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
5886 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5887 r
= xcb_get_property_reply(conn
, c
, NULL
);
5890 if (r
&& r
->type
!= XCB_NONE
&& r
->length
> 0)
5891 name
= strndup(xcb_get_property_value(r
),
5892 xcb_get_property_value_length(r
));
5897 err(1, "get_win_name: failed to allocate memory.");
5905 uniconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5914 DNPRINTF(SWM_D_MISC
, "uniconify\n");
5916 if (r
== NULL
|| r
->ws
== NULL
)
5919 /* make sure we have anything to uniconify */
5920 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5921 if (win
->ws
== NULL
)
5922 continue; /* should never happen */
5928 DNPRINTF(SWM_D_MISC
, "uniconify: count: %d\n", count
);
5934 search_resp_action
= SWM_SEARCH_UNICONIFY
;
5936 spawn_select(r
, args
, "search", &searchpid
);
5938 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5941 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5942 if (win
->ws
== NULL
)
5943 continue; /* should never happen */
5947 name
= get_win_name(win
->id
);
5948 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
5956 name_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5962 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
5968 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
5970 spawn_select(r
, args
, "name_workspace", &searchpid
);
5972 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5975 fprintf(lfile
, "%s", "");
5980 search_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5983 struct workspace
*ws
;
5988 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
5994 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
5996 spawn_select(r
, args
, "search", &searchpid
);
5998 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
6001 for (i
= 0; i
< workspace_limit
; i
++) {
6005 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
6006 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
6013 search_win_cleanup(void)
6015 struct search_window
*sw
= NULL
;
6017 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
6018 xcb_destroy_window(conn
, sw
->indicator
);
6019 TAILQ_REMOVE(&search_wl
, sw
, entry
);
6025 search_win(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6027 struct ws_win
*win
= NULL
;
6028 struct search_window
*sw
= NULL
;
6031 xcb_screen_t
*screen
;
6032 int i
, width
, height
;
6040 XRectangle l_ibox
, l_lbox
;
6044 DNPRINTF(SWM_D_MISC
, "search_win\n");
6047 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
6049 spawn_select(r
, args
, "search", &searchpid
);
6051 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
6054 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
6055 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
6057 TAILQ_INIT(&search_wl
);
6060 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
6064 sw
= calloc(1, sizeof(struct search_window
));
6066 warn("search_win: calloc");
6068 search_win_cleanup();
6074 snprintf(s
, sizeof s
, "%d", i
);
6077 w
= xcb_generate_id(conn
);
6078 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6079 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6080 wa
[2] = screen
->default_colormap
;
6082 if (bar_font_legacy
) {
6083 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
6084 width
= l_lbox
.width
+ 4;
6085 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
6087 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
6089 width
= info
.width
+ 4;
6090 height
= bar_font
->height
+ 4;
6093 xcb_create_window(conn
, screen
->root_depth
, w
, win
->frame
, 0, 0,
6094 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
6095 screen
->root_visual
, XCB_CW_BACK_PIXEL
|
6096 XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
, wa
);
6098 xcb_map_window(conn
, w
);
6101 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
6103 if (bar_font_legacy
) {
6104 l_gcv
.graphics_exposures
= 0;
6105 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
6107 XSetForeground(display
, l_draw
,
6108 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
6110 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
6111 (bar_fs_extents
->max_logical_extent
.height
-
6112 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
6114 XFreeGC(display
, l_draw
);
6117 draw
= XftDrawCreate(display
, w
,
6118 DefaultVisual(display
, r
->s
->idx
),
6119 DefaultColormap(display
, r
->s
->idx
));
6121 XftDrawStringUtf8(draw
, &search_font_color
, bar_font
, 2,
6122 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
6123 bar_font
->descent
, (FcChar8
*)s
, len
);
6125 XftDrawDestroy(draw
);
6128 DNPRINTF(SWM_D_MISC
, "search_win: mapped win %#x\n", w
);
6130 fprintf(lfile
, "%d\n", i
);
6140 search_resp_uniconify(const char *resp
, size_t len
)
6146 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
6148 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
6151 name
= get_win_name(win
->id
);
6152 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
6157 if (strncmp(s
, resp
, len
) == 0) {
6158 /* XXX this should be a callback to generalize */
6159 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_HIDDEN
);
6160 ewmh_update_wm_state(win
);
6170 search_resp_name_workspace(const char *resp
, size_t len
)
6172 struct workspace
*ws
;
6174 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
6176 if (search_r
->ws
== NULL
)
6181 free(search_r
->ws
->name
);
6182 search_r
->ws
->name
= NULL
;
6186 ws
->name
= strdup(resp
);
6187 if (ws
->name
== NULL
) {
6188 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
6189 "strdup: %s", strerror(errno
));
6193 ewmh_update_desktop_names();
6194 ewmh_get_desktop_names();
6199 ewmh_update_desktop_names(void)
6201 char *name_list
= NULL
, *p
;
6202 int num_screens
, i
, j
, len
= 0, tot
= 0;
6204 num_screens
= get_screen_count();
6205 for (i
= 0; i
< num_screens
; ++i
) {
6206 for (j
= 0; j
< workspace_limit
; ++j
) {
6207 if (screens
[i
].ws
[j
].name
!= NULL
)
6208 len
+= strlen(screens
[i
].ws
[j
].name
);
6212 if ((name_list
= calloc(len
, sizeof(char))) == NULL
)
6213 err(1, "update_desktop_names: calloc: failed to "
6214 "allocate memory.");
6217 for (j
= 0; j
< workspace_limit
; ++j
) {
6218 if (screens
[i
].ws
[j
].name
!= NULL
) {
6219 len
= strlen(screens
[i
].ws
[j
].name
);
6220 memcpy(p
, screens
[i
].ws
[j
].name
, len
);
6229 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6230 screens
[i
].root
, ewmh
[_NET_DESKTOP_NAMES
].atom
,
6231 a_utf8_string
, 8, tot
, name_list
);
6241 ewmh_get_desktop_names(void)
6244 xcb_get_property_cookie_t c
;
6245 xcb_get_property_reply_t
*r
;
6246 int num_screens
, i
, j
, n
, k
;
6248 num_screens
= get_screen_count();
6249 for (i
= 0; i
< num_screens
; ++i
) {
6250 for (j
= 0; j
< workspace_limit
; ++j
) {
6251 free(screens
[i
].ws
[j
].name
);
6252 screens
[i
].ws
[j
].name
= NULL
;
6255 c
= xcb_get_property(conn
, 0, screens
[i
].root
,
6256 ewmh
[_NET_DESKTOP_NAMES
].atom
,
6257 a_utf8_string
, 0, UINT32_MAX
);
6258 r
= xcb_get_property_reply(conn
, c
, NULL
);
6262 names
= xcb_get_property_value(r
);
6263 n
= xcb_get_property_value_length(r
);
6265 for (j
= 0, k
= 0; j
< n
; ++j
) {
6266 if (*(names
+ j
) != '\0') {
6267 screens
[i
].ws
[k
].name
= strdup(names
+ j
);
6268 j
+= strlen(names
+ j
);
6277 ewmh_update_client_list(void)
6280 int num_screens
, i
, j
, k
= 0, count
= 0;
6283 num_screens
= get_screen_count();
6284 for (i
= 0; i
< num_screens
; ++i
) {
6285 for (j
= 0; j
< workspace_limit
; ++j
)
6286 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6289 DNPRINTF(SWM_D_PROP
, "ewmh_update_client_list: win count: %d\n",
6295 wins
= calloc(count
, sizeof(xcb_window_t
));
6297 err(1, "ewmh_update_client_list: calloc: failed to "
6298 "allocate memory.");
6300 for (j
= 0, k
= 0; j
< workspace_limit
; ++j
)
6301 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6302 wins
[k
++] = win
->id
;
6304 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6305 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
6306 XCB_ATOM_WINDOW
, 32, count
, wins
);
6313 ewmh_update_current_desktop(void)
6317 num_screens
= get_screen_count();
6318 for (i
= 0; i
< num_screens
; ++i
) {
6319 if (screens
[i
].r_focus
)
6320 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6321 screens
[i
].root
, ewmh
[_NET_CURRENT_DESKTOP
].atom
,
6322 XCB_ATOM_CARDINAL
, 32, 1,
6323 &screens
[i
].r_focus
->ws
->idx
);
6328 ewmh_update_desktops(void)
6330 int num_screens
, i
, j
;
6333 vals
= calloc(workspace_limit
* 2, sizeof(uint32_t));
6335 err(1, "ewmh_update_desktops: calloc: failed to allocate "
6338 num_screens
= get_screen_count();
6339 for (i
= 0; i
< num_screens
; i
++) {
6340 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6341 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
6342 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
6344 for (j
= 0; j
< workspace_limit
; ++j
) {
6345 if (screens
[i
].ws
[j
].r
!= NULL
) {
6346 vals
[j
* 2] = X(screens
[i
].ws
[j
].r
);
6347 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].r
);
6348 } else if (screens
[i
].ws
[j
].old_r
!= NULL
) {
6349 vals
[j
* 2] = X(screens
[i
].ws
[j
].old_r
);
6350 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].old_r
);
6352 vals
[j
* 2] = vals
[j
* 2 + 1] = 0;
6356 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6357 screens
[i
].root
, ewmh
[_NET_DESKTOP_VIEWPORT
].atom
,
6358 XCB_ATOM_CARDINAL
, 32, workspace_limit
* 2, vals
);
6365 search_resp_search_workspace(const char *resp
)
6372 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
6376 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
6383 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
6385 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
6392 switchws(NULL
, search_r
, &a
);
6396 search_resp_search_window(const char *resp
)
6401 struct search_window
*sw
;
6403 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
6407 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
6412 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
6414 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
6421 TAILQ_FOREACH(sw
, &search_wl
, entry
)
6422 if (idx
== sw
->idx
) {
6428 #define MAX_RESP_LEN 1024
6431 search_do_resp(void)
6437 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
6442 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
6443 warn("search: calloc");
6447 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
6449 warn("search: read error");
6452 resp
[rbytes
] = '\0';
6455 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
6456 * newline, so work around that by sanitizing the resp now.
6458 resp
[strcspn(resp
, "\n")] = '\0';
6461 switch (search_resp_action
) {
6462 case SWM_SEARCH_UNICONIFY
:
6463 search_resp_uniconify(resp
, len
);
6465 case SWM_SEARCH_NAME_WORKSPACE
:
6466 search_resp_name_workspace(resp
, len
);
6468 case SWM_SEARCH_SEARCH_WORKSPACE
:
6469 search_resp_search_workspace(resp
);
6471 case SWM_SEARCH_SEARCH_WINDOW
:
6472 search_resp_search_window(resp
);
6477 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
6478 search_win_cleanup();
6480 search_resp_action
= SWM_SEARCH_NONE
;
6481 close(select_resp_pipe
[0]);
6488 wkill(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6492 DNPRINTF(SWM_D_MISC
, "wkill: win %#x, id: %d\n", WINID(r
->ws
->focus
),
6495 if (r
->ws
->focus
== NULL
)
6498 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
6499 xcb_kill_client(conn
, r
->ws
->focus
->id
);
6501 if (r
->ws
->focus
->can_delete
)
6502 client_msg(r
->ws
->focus
, a_delete
, 0);
6508 clear_maximized(struct workspace
*ws
)
6513 /* Clear any maximized win(s) on ws, from bottom up. */
6514 TAILQ_FOREACH_REVERSE(w
, &ws
->stack
, ws_win_stack
, stack_entry
)
6516 ewmh_apply_flags(w
, w
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
6517 ewmh_update_wm_state(w
);
6525 maximize_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6527 struct ws_win
*w
= r
->ws
->focus
;
6529 /* suppress unused warning since var is needed */
6536 DNPRINTF(SWM_D_MISC
, "maximize_toggle: win %#x\n", w
->id
);
6541 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6544 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_MAXIMIZED
);
6545 ewmh_update_wm_state(w
);
6549 if (w
== w
->ws
->focus
)
6554 DNPRINTF(SWM_D_MISC
, "maximize_toggle: done\n");
6558 floating_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6560 struct ws_win
*w
= r
->ws
->focus
;
6562 /* suppress unused warning since var is needed */
6569 DNPRINTF(SWM_D_MISC
, "floating_toggle: win %#x\n", w
->id
);
6571 if (FULLSCREEN(w
) || TRANS(w
))
6574 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6577 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_ABOVE
);
6578 ewmh_update_wm_state(w
);
6582 if (w
== w
->ws
->focus
)
6587 DNPRINTF(SWM_D_MISC
, "floating_toggle: done\n");
6591 fullscreen_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6593 struct ws_win
*w
= r
->ws
->focus
;
6595 /* suppress unused warning since var is needed */
6602 DNPRINTF(SWM_D_MISC
, "fullscreen_toggle: win %#x\n", w
->id
);
6604 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_FULLSCREEN
);
6605 ewmh_update_wm_state(w
);
6609 if (w
== w
->ws
->focus
)
6614 DNPRINTF(SWM_D_MISC
, "fullscreen_toggle: done\n");
6617 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
6619 struct swm_geometry g
= r
->g
;
6620 int rt
, lt
, tp
, bm
, bw
;
6622 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
6625 * Perpendicular distance of each side of the window to the respective
6626 * side of the region boundary. Positive values indicate the side of
6627 * the window has passed beyond the region boundary.
6629 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) - MAX_X(r
) : bw
;
6630 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) : bw
;
6631 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) - MAX_Y(r
) : bw
;
6632 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) : bw
;
6634 DNPRINTF(SWM_D_MISC
, "region_containment: win %#x, rt: %d, lt: %d, "
6635 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
6636 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
6637 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
6640 * Disable containment if any of the flagged sides went beyond the
6641 * containment boundary, or if containment is disabled.
6643 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
6644 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
6645 /* Make sure window has at least 1 pixel in the region */
6646 g
.x
+= 1 - WIDTH(win
);
6647 g
.y
+= 1 - HEIGHT(win
);
6648 g
.w
+= 2 * WIDTH(win
) - 2;
6649 g
.h
+= 2 * HEIGHT(win
) - 2;
6652 constrain_window(win
, &g
, &opts
);
6655 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6657 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
6659 DNPRINTF(SWM_D_MISC
, "constrain_window: win %#x, (x,y) w x h: "
6660 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6661 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
6662 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
6663 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
6664 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
6665 YESNO(*opts
& SWM_CW_RESIZABLE
));
6667 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) > b
->x
+ b
->w
) {
6668 if (*opts
& SWM_CW_RESIZABLE
)
6669 WIDTH(win
) = b
->x
+ b
->w
- X(win
);
6671 X(win
) = b
->x
+ b
->w
- WIDTH(win
);
6674 if ((*opts
& SWM_CW_LEFT
) && X(win
) < b
->x
) {
6675 if (*opts
& SWM_CW_RESIZABLE
)
6676 WIDTH(win
) -= b
->x
- X(win
);
6681 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) > b
->y
+ b
->h
) {
6682 if (*opts
& SWM_CW_RESIZABLE
)
6683 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
);
6685 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
);
6688 if ((*opts
& SWM_CW_TOP
) && Y(win
) < b
->y
) {
6689 if (*opts
& SWM_CW_RESIZABLE
)
6690 HEIGHT(win
) -= b
->y
- Y(win
);
6695 if (*opts
& SWM_CW_RESIZABLE
) {
6698 if (HEIGHT(win
) < 1)
6704 draw_frame(struct ws_win
*win
)
6706 xcb_rectangle_t rect
[4];
6710 if (win
->frame
== XCB_WINDOW_NONE
) {
6711 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x not "
6712 "reparented.\n", win
->id
);
6716 if (!win
->bordered
) {
6717 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x frame "
6718 "disabled\n", win
->id
);
6721 if (WS_FOCUSED(win
->ws
) && win
->ws
->focus
== win
)
6722 pixel
= MAXIMIZED(win
) ?
6723 &win
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].pixel
:
6724 &win
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6726 pixel
= MAXIMIZED(win
) ?
6727 &win
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].pixel
:
6728 &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6730 /* Top (with corners) */
6733 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6734 rect
[n
].height
= border_width
;
6735 /* Left (without corners) */
6737 rect
[n
].y
= border_width
;
6738 rect
[n
].width
= border_width
;
6739 rect
[n
].height
= HEIGHT(win
);
6740 /* Right (without corners) */
6741 rect
[++n
].x
= border_width
+ WIDTH(win
);
6742 rect
[n
].y
= border_width
;
6743 rect
[n
].width
= border_width
;
6744 rect
[n
].height
= HEIGHT(win
);
6745 /* Bottom (with corners)*/
6747 rect
[n
].y
= border_width
+ HEIGHT(win
);
6748 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6749 rect
[n
].height
= border_width
;
6751 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, pixel
);
6752 xcb_poly_fill_rectangle(conn
, win
->frame
, win
->s
->bar_gc
, 4, rect
);
6756 update_window(struct ws_win
*win
)
6761 if (win
->frame
== XCB_WINDOW_NONE
) {
6762 DNPRINTF(SWM_D_EVENT
, "update_window: skip win %#x; "
6763 "not reparented.\n", win
->id
);
6767 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
6768 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
6769 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6771 /* Reconfigure frame. */
6772 if (win
->bordered
) {
6773 wc
[0] = X(win
) - border_width
;
6774 wc
[1] = Y(win
) - border_width
;
6775 wc
[2] = WIDTH(win
) + 2 * border_width
;
6776 wc
[3] = HEIGHT(win
) + 2 * border_width
;
6781 wc
[3] = HEIGHT(win
);
6786 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x frame %#x, (x,y) w x h: "
6787 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, win
->frame
, wc
[0],
6788 wc
[1], wc
[2], wc
[3], YESNO(win
->bordered
));
6790 xcb_configure_window(conn
, win
->frame
, mask
, wc
);
6792 /* Reconfigure client window. */
6793 wc
[0] = wc
[1] = win
->bordered
? border_width
: 0;
6795 wc
[3] = HEIGHT(win
);
6797 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x, (x,y) w x h: "
6798 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
6799 wc
[3], YESNO(win
->bordered
));
6800 xcb_configure_window(conn
, win
->id
, mask
, wc
);
6802 /* ICCCM 4.2.3 Synthetic ConfigureNotify when moved and not resized. */
6803 if ((X(win
) != win
->g_prev
.x
|| Y(win
) != win
->g_prev
.y
) &&
6804 (win
->java
|| (WIDTH(win
) == win
->g_prev
.w
&&
6805 HEIGHT(win
) == win
->g_prev
.h
))) {
6806 /* Java has special needs when moved together with a resize. */
6807 config_win(win
, NULL
);
6810 win
->g_prev
= win
->g
;
6814 SIMPLEQ_ENTRY(event
) entry
;
6815 xcb_generic_event_t
*ev
;
6817 SIMPLEQ_HEAD(event_queue
, event
) events
= SIMPLEQ_HEAD_INITIALIZER(events
);
6819 xcb_generic_event_t
*
6820 get_next_event(bool dowait
)
6823 xcb_generic_event_t
*evt
;
6825 /* Try queue first. */
6826 if ((ep
= SIMPLEQ_FIRST(&events
))) {
6828 SIMPLEQ_REMOVE_HEAD(&events
, entry
);
6831 evt
= xcb_wait_for_event(conn
);
6833 evt
= xcb_poll_for_event(conn
);
6839 put_back_event(xcb_generic_event_t
*evt
)
6842 if ((ep
= malloc(sizeof (struct event
))) == NULL
)
6843 err(1, "put_back_event: failed to allocate memory.");
6845 SIMPLEQ_INSERT_HEAD(&events
, ep
, entry
);
6848 /* Peeks at next event to detect auto-repeat. */
6850 keyrepeating(xcb_key_release_event_t
*kre
)
6852 xcb_generic_event_t
*evt
;
6854 /* Ensure repeating keypress is finished processing. */
6857 if ((evt
= get_next_event(false))) {
6858 put_back_event(evt
);
6860 if (XCB_EVENT_RESPONSE_TYPE(evt
) == XCB_KEY_PRESS
&&
6861 kre
->sequence
== evt
->sequence
&&
6862 kre
->detail
== ((xcb_key_press_event_t
*)evt
)->detail
)
6870 keybindreleased(struct binding
*bp
, xcb_key_release_event_t
*kre
)
6872 if (bp
->type
== KEYBIND
&& !keyrepeating(kre
) &&
6873 bp
->value
== xcb_key_press_lookup_keysym(syms
, kre
, 0))
6879 #define SWM_RESIZE_STEPS (50)
6882 resize_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
6884 xcb_timestamp_t timestamp
= 0;
6885 struct swm_region
*r
= NULL
;
6886 struct swm_geometry g
;
6887 int top
= 0, left
= 0;
6889 xcb_cursor_t cursor
;
6890 xcb_query_pointer_reply_t
*xpr
= NULL
;
6891 xcb_generic_event_t
*evt
;
6892 xcb_motion_notify_event_t
*mne
;
6893 bool resizing
, step
= false;
6899 if (FULLSCREEN(win
))
6902 /* In max_stack mode, should only resize transients. */
6903 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6906 DNPRINTF(SWM_D_EVENT
, "resize: win %#x, floating: %s, "
6907 "transient: %#x\n", win
->id
, YESNO(ABOVE(win
)),
6911 store_float_geom(win
);
6912 else if (!(TRANS(win
) || ABOVE(win
)))
6915 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6917 ewmh_update_wm_state(win
);
6923 /* It's possible for win to have been freed during focus_flush(). */
6924 if (validate_win(win
)) {
6925 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6930 case SWM_ARG_ID_WIDTHSHRINK
:
6931 WIDTH(win
) -= SWM_RESIZE_STEPS
;
6934 case SWM_ARG_ID_WIDTHGROW
:
6935 WIDTH(win
) += SWM_RESIZE_STEPS
;
6938 case SWM_ARG_ID_HEIGHTSHRINK
:
6939 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
6942 case SWM_ARG_ID_HEIGHTGROW
:
6943 HEIGHT(win
) += SWM_RESIZE_STEPS
;
6950 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6951 SWM_CW_HARDBOUNDARY
);
6953 store_float_geom(win
);
6957 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6958 SWM_CW_SOFTBOUNDARY
);
6961 /* get cursor offset from window root */
6962 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6969 if (xpr
->win_x
< WIDTH(win
) / 2)
6972 if (xpr
->win_y
< HEIGHT(win
) / 2)
6975 if (opt
== SWM_ARG_ID_CENTER
)
6976 cursor
= cursors
[XC_SIZING
].cid
;
6978 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
6979 XC_TOP_RIGHT_CORNER
].cid
;
6981 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
6982 XC_BOTTOM_RIGHT_CORNER
].cid
;
6984 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6985 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
6988 /* Release keyboard freeze if called via keybind. */
6989 if (bp
->type
== KEYBIND
)
6990 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6995 while (resizing
&& (evt
= get_next_event(true))) {
6996 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6997 case XCB_BUTTON_RELEASE
:
6998 if (bp
->type
== BTNBIND
&& bp
->value
==
6999 ((xcb_button_release_event_t
*)evt
)->detail
)
7002 case XCB_KEY_RELEASE
:
7003 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
7006 case XCB_MOTION_NOTIFY
:
7007 mne
= (xcb_motion_notify_event_t
*)evt
;
7008 DNPRINTF(SWM_D_EVENT
, "resize: MOTION_NOTIFY: "
7009 "root: %#x\n", mne
->root
);
7011 /* cursor offset/delta from start of the operation */
7012 dx
= mne
->root_x
- xpr
->root_x
;
7013 dy
= mne
->root_y
- xpr
->root_y
;
7019 if (opt
== SWM_ARG_ID_CENTER
) {
7020 if (g
.h
/ 2 + dy
< 1)
7024 HEIGHT(win
) = g
.h
+ 2 * dy
;
7032 HEIGHT(win
) = g
.h
+ dy
;
7039 if (opt
== SWM_ARG_ID_CENTER
) {
7040 if (g
.w
/ 2 + dx
< 1)
7044 WIDTH(win
) = g
.w
+ 2 * dx
;
7052 WIDTH(win
) = g
.w
+ dx
;
7055 /* not free, don't sync more than 120 times / second */
7056 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
7057 timestamp
= mne
->time
;
7058 regionize(win
, mne
->root_x
, mne
->root_y
);
7059 region_containment(win
, r
, SWM_CW_ALLSIDES
|
7060 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
7061 SWM_CW_SOFTBOUNDARY
);
7066 case XCB_BUTTON_PRESS
:
7068 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_PRESS ignored\n");
7069 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7070 ((xcb_button_press_event_t
*)evt
)->time
);
7075 DNPRINTF(SWM_D_EVENT
, "resize: KEY_PRESS ignored\n");
7076 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7077 ((xcb_key_press_event_t
*)evt
)->time
);
7083 /* It's possible for win to have been freed above. */
7084 if (validate_win(win
)) {
7085 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
7093 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
7094 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
7098 store_float_geom(win
);
7100 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7102 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
7106 resize(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
7108 struct ws_win
*win
= NULL
;
7109 xcb_query_pointer_reply_t
*qpr
= NULL
;
7114 if (args
->id
!= SWM_ARG_ID_DONTCENTER
&&
7115 args
->id
!= SWM_ARG_ID_CENTER
) {
7116 /* {height,width}_{grow,shrink} use the focus window. */
7120 /* resize uses window under pointer. */
7121 qpr
= xcb_query_pointer_reply(conn
,
7122 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7124 win
= find_window(qpr
->child
);
7130 resize_win(win
, bp
, args
->id
);
7132 if (args
->id
&& bp
->type
== KEYBIND
)
7138 /* Try to set window region based on supplied coordinates or window center. */
7140 regionize(struct ws_win
*win
, int x
, int y
)
7142 struct swm_region
*r
= NULL
;
7144 r
= region_under(win
->s
, x
, y
);
7146 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
7147 Y(win
) + HEIGHT(win
) / 2);
7149 if (r
&& r
!= win
->ws
->r
) {
7150 clear_maximized(r
->ws
);
7152 win_to_ws(win
, r
->ws
->idx
, false);
7154 /* Stack old and new region. */
7158 /* Set focus on new ws. */
7159 unfocus_win(r
->ws
->focus
);
7167 #define SWM_MOVE_STEPS (50)
7170 move_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
7172 struct swm_region
*r
;
7173 xcb_timestamp_t timestamp
= 0;
7174 xcb_query_pointer_reply_t
*qpr
= NULL
;
7175 xcb_generic_event_t
*evt
;
7176 xcb_motion_notify_event_t
*mne
;
7177 bool moving
, restack
= false, step
= false;
7182 if ((r
= win
->ws
->r
) == NULL
)
7185 if (FULLSCREEN(win
))
7188 DNPRINTF(SWM_D_EVENT
, "move: win %#x, floating: %s, transient: "
7189 "%#x\n", win
->id
, YESNO(ABOVE(win
)), win
->transient
);
7191 /* in max_stack mode should only move transients */
7192 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
7195 if (!(ABOVE(win
) || TRANS(win
)) || MAXIMIZED(win
)) {
7196 store_float_geom(win
);
7200 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
7202 ewmh_update_wm_state(win
);
7209 /* It's possible for win to have been freed during focus_flush(). */
7210 if (validate_win(win
)) {
7211 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7216 case SWM_ARG_ID_MOVELEFT
:
7217 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
7220 case SWM_ARG_ID_MOVERIGHT
:
7221 X(win
) += (SWM_MOVE_STEPS
- border_width
);
7224 case SWM_ARG_ID_MOVEUP
:
7225 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
7228 case SWM_ARG_ID_MOVEDOWN
:
7229 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
7236 regionize(win
, -1, -1);
7237 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
7239 store_float_geom(win
);
7243 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
7244 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
7245 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
7247 /* get cursor offset from window root */
7248 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
7251 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7255 /* Release keyboard freeze if called via keybind. */
7256 if (bp
->type
== KEYBIND
)
7257 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7260 regionize(win
, qpr
->root_x
, qpr
->root_y
);
7261 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7262 SWM_CW_SOFTBOUNDARY
);
7266 while (moving
&& (evt
= get_next_event(true))) {
7267 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
7268 case XCB_BUTTON_RELEASE
:
7269 if (bp
->type
== BTNBIND
&& bp
->value
==
7270 ((xcb_button_release_event_t
*)evt
)->detail
)
7273 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7274 ((xcb_button_release_event_t
*)evt
)->time
);
7277 case XCB_KEY_RELEASE
:
7278 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
7281 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7282 ((xcb_key_release_event_t
*)evt
)->time
);
7285 case XCB_MOTION_NOTIFY
:
7286 mne
= (xcb_motion_notify_event_t
*)evt
;
7287 DNPRINTF(SWM_D_EVENT
, "move: MOTION_NOTIFY: "
7288 "root: %#x\n", mne
->root
);
7289 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
7290 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
7292 /* not free, don't sync more than 120 times / second */
7293 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
7294 timestamp
= mne
->time
;
7295 regionize(win
, mne
->root_x
, mne
->root_y
);
7296 region_containment(win
, win
->ws
->r
,
7297 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
7302 case XCB_BUTTON_PRESS
:
7303 /* Thaw and ignore. */
7304 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7305 ((xcb_button_press_event_t
*)evt
)->time
);
7310 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7311 ((xcb_key_press_event_t
*)evt
)->time
);
7317 /* It's possible for win to have been freed. */
7318 if (validate_win(win
)) {
7319 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7327 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7328 SWM_CW_SOFTBOUNDARY
);
7332 store_float_geom(win
);
7334 /* New region set to fullscreen layout. */
7335 if (win
->ws
->r
&& win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
]) {
7342 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7343 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
7347 move(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
7349 struct ws_win
*win
= NULL
;
7350 xcb_query_pointer_reply_t
*qpr
= NULL
;
7356 /* move_* uses focus window. */
7360 /* Disallow move_ on tiled. */
7361 if (win
&& !(TRANS(win
) || ABOVE(win
)))
7364 /* move uses window under pointer. */
7365 qpr
= xcb_query_pointer_reply(conn
,
7366 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7368 win
= find_window(qpr
->child
);
7374 move_win(win
, bp
, args
->id
);
7376 if (args
->id
&& bp
->type
== KEYBIND
)
7382 /* action definitions */
7384 char name
[SWM_FUNCNAME_LEN
];
7385 void (*func
)(struct binding
*, struct swm_region
*,
7389 } actions
[FN_INVALID
+ 2] = {
7390 /* name function argument */
7391 { "bar_toggle", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
7392 { "bar_toggle_ws", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
7393 { "button2", pressbutton
, 0, {.id
= 2} },
7394 { "cycle_layout", cycle_layout
, 0, {0} },
7395 { "flip_layout", stack_config
, 0, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
7396 { "float_toggle", floating_toggle
,0, {0} },
7397 { "focus", focus_pointer
, 0, {0} },
7398 { "focus_main", focus
, 0, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
7399 { "focus_next", focus
, 0, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
7400 { "focus_prev", focus
, 0, {.id
= SWM_ARG_ID_FOCUSPREV
} },
7401 { "focus_urgent", focus
, 0, {.id
= SWM_ARG_ID_FOCUSURGENT
} },
7402 { "fullscreen_toggle", fullscreen_toggle
, 0, {0} },
7403 { "maximize_toggle", maximize_toggle
,0, {0} },
7404 { "height_grow", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
7405 { "height_shrink", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
7406 { "iconify", iconify
, 0, {0} },
7407 { "master_shrink", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
7408 { "master_grow", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERGROW
} },
7409 { "master_add", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERADD
} },
7410 { "master_del", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERDEL
} },
7411 { "move", move
, FN_F_NOREPLAY
, {0} },
7412 { "move_down", move
, 0, {.id
= SWM_ARG_ID_MOVEDOWN
} },
7413 { "move_left", move
, 0, {.id
= SWM_ARG_ID_MOVELEFT
} },
7414 { "move_right", move
, 0, {.id
= SWM_ARG_ID_MOVERIGHT
} },
7415 { "move_up", move
, 0, {.id
= SWM_ARG_ID_MOVEUP
} },
7416 { "mvrg_1", send_to_rg
, 0, {.id
= 0} },
7417 { "mvrg_2", send_to_rg
, 0, {.id
= 1} },
7418 { "mvrg_3", send_to_rg
, 0, {.id
= 2} },
7419 { "mvrg_4", send_to_rg
, 0, {.id
= 3} },
7420 { "mvrg_5", send_to_rg
, 0, {.id
= 4} },
7421 { "mvrg_6", send_to_rg
, 0, {.id
= 5} },
7422 { "mvrg_7", send_to_rg
, 0, {.id
= 6} },
7423 { "mvrg_8", send_to_rg
, 0, {.id
= 7} },
7424 { "mvrg_9", send_to_rg
, 0, {.id
= 8} },
7425 { "mvrg_next", send_to_rg_relative
, 0, {.id
= 1} },
7426 { "mvrg_prev", send_to_rg_relative
, 0, {.id
= -1} },
7427 { "mvws_1", send_to_ws
, 0, {.id
= 0} },
7428 { "mvws_2", send_to_ws
, 0, {.id
= 1} },
7429 { "mvws_3", send_to_ws
, 0, {.id
= 2} },
7430 { "mvws_4", send_to_ws
, 0, {.id
= 3} },
7431 { "mvws_5", send_to_ws
, 0, {.id
= 4} },
7432 { "mvws_6", send_to_ws
, 0, {.id
= 5} },
7433 { "mvws_7", send_to_ws
, 0, {.id
= 6} },
7434 { "mvws_8", send_to_ws
, 0, {.id
= 7} },
7435 { "mvws_9", send_to_ws
, 0, {.id
= 8} },
7436 { "mvws_10", send_to_ws
, 0, {.id
= 9} },
7437 { "mvws_11", send_to_ws
, 0, {.id
= 10} },
7438 { "mvws_12", send_to_ws
, 0, {.id
= 11} },
7439 { "mvws_13", send_to_ws
, 0, {.id
= 12} },
7440 { "mvws_14", send_to_ws
, 0, {.id
= 13} },
7441 { "mvws_15", send_to_ws
, 0, {.id
= 14} },
7442 { "mvws_16", send_to_ws
, 0, {.id
= 15} },
7443 { "mvws_17", send_to_ws
, 0, {.id
= 16} },
7444 { "mvws_18", send_to_ws
, 0, {.id
= 17} },
7445 { "mvws_19", send_to_ws
, 0, {.id
= 18} },
7446 { "mvws_20", send_to_ws
, 0, {.id
= 19} },
7447 { "mvws_21", send_to_ws
, 0, {.id
= 20} },
7448 { "mvws_22", send_to_ws
, 0, {.id
= 21} },
7449 { "name_workspace", name_workspace
, 0, {0} },
7450 { "quit", quit
, 0, {0} },
7451 { "raise", raise_focus
, 0, {0} },
7452 { "raise_toggle", raise_toggle
, 0, {0} },
7453 { "resize", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_DONTCENTER
} },
7454 { "resize_centered", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_CENTER
} },
7455 { "restart", restart
, 0, {0} },
7456 { "rg_1", focusrg
, 0, {.id
= 0} },
7457 { "rg_2", focusrg
, 0, {.id
= 1} },
7458 { "rg_3", focusrg
, 0, {.id
= 2} },
7459 { "rg_4", focusrg
, 0, {.id
= 3} },
7460 { "rg_5", focusrg
, 0, {.id
= 4} },
7461 { "rg_6", focusrg
, 0, {.id
= 5} },
7462 { "rg_7", focusrg
, 0, {.id
= 6} },
7463 { "rg_8", focusrg
, 0, {.id
= 7} },
7464 { "rg_9", focusrg
, 0, {.id
= 8} },
7465 { "rg_move_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_UP
} },
7466 { "rg_move_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_DOWN
} },
7467 { "rg_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7468 { "rg_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7469 { "screen_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7470 { "screen_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7471 { "search_win", search_win
, 0, {0} },
7472 { "search_workspace", search_workspace
, 0, {0} },
7473 { "spawn_custom", NULL
, 0, {0} },
7474 { "stack_balance", stack_config
, 0, {.id
= SWM_ARG_ID_STACKBALANCE
} },
7475 { "stack_inc", stack_config
, 0, {.id
= SWM_ARG_ID_STACKINC
} },
7476 { "stack_dec", stack_config
, 0, {.id
= SWM_ARG_ID_STACKDEC
} },
7477 { "stack_reset", stack_config
, 0, {.id
= SWM_ARG_ID_STACKRESET
} },
7478 { "swap_main", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPMAIN
} },
7479 { "swap_next", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPNEXT
} },
7480 { "swap_prev", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPPREV
} },
7481 { "uniconify", uniconify
, 0, {0} },
7482 { "version", version
, 0, {0} },
7483 { "width_grow", resize
, 0, {.id
= SWM_ARG_ID_WIDTHGROW
} },
7484 { "width_shrink", resize
, 0, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
7485 { "wind_del", wkill
, 0, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
7486 { "wind_kill", wkill
, 0, {.id
= SWM_ARG_ID_KILLWINDOW
} },
7487 { "ws_1", switchws
, 0, {.id
= 0} },
7488 { "ws_2", switchws
, 0, {.id
= 1} },
7489 { "ws_3", switchws
, 0, {.id
= 2} },
7490 { "ws_4", switchws
, 0, {.id
= 3} },
7491 { "ws_5", switchws
, 0, {.id
= 4} },
7492 { "ws_6", switchws
, 0, {.id
= 5} },
7493 { "ws_7", switchws
, 0, {.id
= 6} },
7494 { "ws_8", switchws
, 0, {.id
= 7} },
7495 { "ws_9", switchws
, 0, {.id
= 8} },
7496 { "ws_10", switchws
, 0, {.id
= 9} },
7497 { "ws_11", switchws
, 0, {.id
= 10} },
7498 { "ws_12", switchws
, 0, {.id
= 11} },
7499 { "ws_13", switchws
, 0, {.id
= 12} },
7500 { "ws_14", switchws
, 0, {.id
= 13} },
7501 { "ws_15", switchws
, 0, {.id
= 14} },
7502 { "ws_16", switchws
, 0, {.id
= 15} },
7503 { "ws_17", switchws
, 0, {.id
= 16} },
7504 { "ws_18", switchws
, 0, {.id
= 17} },
7505 { "ws_19", switchws
, 0, {.id
= 18} },
7506 { "ws_20", switchws
, 0, {.id
= 19} },
7507 { "ws_21", switchws
, 0, {.id
= 20} },
7508 { "ws_22", switchws
, 0, {.id
= 21} },
7509 { "ws_next", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
7510 { "ws_next_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
7511 { "ws_next_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
7512 { "ws_prev", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
7513 { "ws_prev_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
7514 { "ws_prev_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
7515 { "ws_prior", priorws
, 0, {0} },
7516 /* SWM_DEBUG actions MUST be here: */
7517 { "debug_toggle", debug_toggle
, 0, {0} },
7518 { "dumpwins", dumpwins
, 0, {0} },
7520 { "invalid action", NULL
, 0, {0} },
7524 update_modkey(uint16_t mod
)
7528 /* Replace all instances of the old mod key. */
7529 RB_FOREACH(bp
, binding_tree
, &bindings
)
7530 if (bp
->mod
& mod_key
)
7531 bp
->mod
= (bp
->mod
& ~mod_key
) | mod
;
7536 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7539 struct spawn_prog
*prog
= NULL
;
7541 char *ap
, **real_args
;
7543 /* suppress unused warning since var is needed */
7546 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
7549 TAILQ_FOREACH(prog
, &spawns
, entry
) {
7550 if (strcasecmp(spawn_name
, prog
->name
) == 0)
7554 warnx("spawn_custom: program %s not found", spawn_name
);
7558 /* make room for expanded args */
7559 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
7560 err(1, "spawn_custom: calloc real_args");
7562 /* expand spawn_args into real_args */
7563 for (i
= c
= 0; i
< prog
->argc
; i
++) {
7565 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
7566 if (strcasecmp(ap
, "$bar_border") == 0) {
7568 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
7570 err(1, "spawn_custom border color");
7571 } else if (strcasecmp(ap
, "$bar_color") == 0) {
7573 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
7575 err(1, "spawn_custom bar color");
7576 } else if (strcasecmp(ap
, "$bar_font") == 0) {
7577 if ((real_args
[c
] = strdup(bar_fonts
))
7579 err(1, "spawn_custom bar fonts");
7580 } else if (strcasecmp(ap
, "$bar_font_color") == 0) {
7582 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
7584 err(1, "spawn_custom color font");
7585 } else if (strcasecmp(ap
, "$color_focus") == 0) {
7587 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
7589 err(1, "spawn_custom color focus");
7590 } else if (strcasecmp(ap
, "$color_focus_maximized") == 0) {
7592 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].name
))
7594 err(1, "spawn_custom color focus maximized");
7595 } else if (strcasecmp(ap
, "$color_unfocus") == 0) {
7597 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
7599 err(1, "spawn_custom color unfocus");
7600 } else if (strcasecmp(ap
, "$color_unfocus_maximized") == 0) {
7602 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].name
))
7604 err(1, "spawn_custom color unfocus maximized");
7605 } else if (strcasecmp(ap
, "$region_index") == 0) {
7606 if (asprintf(&real_args
[c
], "%d",
7607 get_region_index(r
) + 1) < 1)
7608 err(1, "spawn_custom region index");
7609 } else if (strcasecmp(ap
, "$workspace_index") == 0) {
7610 if (asprintf(&real_args
[c
], "%d", r
->ws
->idx
+ 1) < 1)
7611 err(1, "spawn_custom workspace index");
7612 } else if (strcasecmp(ap
, "$dmenu_bottom") == 0) {
7615 if ((real_args
[c
] = strdup("-b")) == NULL
)
7616 err(1, "spawn_custom workspace index");
7618 /* no match --> copy as is */
7619 if ((real_args
[c
] = strdup(ap
)) == NULL
)
7620 err(1, "spawn_custom strdup(ap)");
7622 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
7628 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
7629 for (i
= 0; i
< c
; ++i
)
7630 DPRINTF("\"%s\" ", real_args
[i
]);
7633 *ret_args
= real_args
;
7638 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
7644 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7648 spawn(r
->ws
->idx
, &a
, true);
7650 for (i
= 0; i
< spawn_argc
; i
++)
7656 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7663 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7667 if (pipe(select_list_pipe
) == -1)
7668 err(1, "pipe error");
7669 if (pipe(select_resp_pipe
) == -1)
7670 err(1, "pipe error");
7672 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
7673 err(1, "could not disable SIGPIPE");
7674 switch (*pid
= fork()) {
7676 err(1, "cannot fork");
7679 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
7681 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
7683 close(select_list_pipe
[1]);
7684 close(select_resp_pipe
[0]);
7685 spawn(r
->ws
->idx
, &a
, false);
7687 default: /* parent */
7688 close(select_list_pipe
[0]);
7689 close(select_resp_pipe
[1]);
7693 for (i
= 0; i
< spawn_argc
; i
++)
7698 /* Argument tokenizer. */
7701 char *arg
, *cp
, *next
;
7702 bool single_quoted
= false, double_quoted
= false;
7707 /* Eat and move characters until end of argument is found. */
7708 for (arg
= next
= cp
= *sp
; *cp
!= '\0'; ++cp
) {
7709 if (!double_quoted
&& *cp
== '\'') {
7710 /* Eat single-quote. */
7711 single_quoted
= !single_quoted
;
7712 } else if (!single_quoted
&& *cp
== '"') {
7713 /* Eat double-quote. */
7714 double_quoted
= !double_quoted
;
7715 } else if (!single_quoted
&& *cp
== '\\' && *(cp
+ 1) == '"') {
7716 /* Eat backslash; copy escaped character to arg. */
7718 } else if (!single_quoted
&& !double_quoted
&& *cp
== '\\' &&
7719 (*(cp
+ 1) == '\'' || *(cp
+ 1) == ' ')) {
7720 /* Eat backslash; move escaped character. */
7722 } else if (!single_quoted
&& !double_quoted
&&
7723 (*cp
== ' ' || *cp
== '\t')) {
7724 /* Terminate argument. */
7726 /* Point sp to beginning of next argument. */
7730 /* Move regular character. */
7735 /* Terminate argument if end of string. */
7745 spawn_insert(const char *name
, const char *args
, int flags
)
7747 struct spawn_prog
*sp
;
7748 char *arg
, *cp
, *ptr
;
7750 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s[%s]\n", name
, args
);
7752 if (args
== NULL
|| *args
== '\0')
7755 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
7756 err(1, "spawn_insert: calloc");
7757 if ((sp
->name
= strdup(name
)) == NULL
)
7758 err(1, "spawn_insert: strdup");
7760 /* Convert the arguments to an argument list. */
7761 if ((ptr
= cp
= strdup(args
)) == NULL
)
7762 err(1, "spawn_insert: strdup");
7763 while ((arg
= argsep(&ptr
)) != NULL
) {
7764 /* Null argument; skip it. */
7769 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
7770 sizeof *sp
->argv
)) == NULL
)
7771 err(1, "spawn_insert: realloc");
7772 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
7773 err(1, "spawn_insert: strdup");
7779 DNPRINTF(SWM_D_SPAWN
, "arg %d: [%s]\n", sp
->argc
, sp
->argv
[sp
->argc
-1]);
7780 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
7781 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
7785 spawn_remove(struct spawn_prog
*sp
)
7789 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
7791 TAILQ_REMOVE(&spawns
, sp
, entry
);
7792 for (i
= 0; i
< sp
->argc
; i
++)
7798 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
7804 struct spawn_prog
*sp
;
7806 while ((sp
= TAILQ_FIRST(&spawns
)) != NULL
) {
7812 spawn_find(const char *name
)
7814 struct spawn_prog
*sp
;
7816 TAILQ_FOREACH(sp
, &spawns
, entry
)
7817 if (strcasecmp(sp
->name
, name
) == 0)
7824 setspawn(const char *name
, const char *args
, int flags
)
7826 struct spawn_prog
*sp
;
7828 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
7833 /* Remove any old spawn under the same name. */
7834 if ((sp
= spawn_find(name
)) != NULL
)
7838 spawn_insert(name
, args
, flags
);
7840 warnx("error: setspawn: cannot find program: %s", name
);
7842 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
7846 setconfspawn(const char *selector
, const char *value
, int flags
)
7850 if (selector
== NULL
|| strlen(selector
) == 0)
7853 args
= expand_tilde(value
);
7855 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
7857 setspawn(selector
, args
, flags
);
7860 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
7865 validate_spawns(void)
7868 struct spawn_prog
*sp
;
7869 char which
[PATH_MAX
];
7872 RB_FOREACH(bp
, binding_tree
, &bindings
) {
7873 if (bp
->action
!= FN_SPAWN_CUSTOM
)
7877 sp
= spawn_find(bp
->spawn_name
);
7878 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
7881 /* verify we have the goods */
7882 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
7883 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
7885 for (i
= strlen("which "); i
< strlen(which
); i
++)
7886 if (which
[i
] == ' ') {
7890 if (system(which
) != 0)
7891 add_startup_exception("could not find %s",
7892 &which
[strlen("which ")]);
7899 setconfspawn("lock", "xlock", 0);
7901 setconfspawn("term", "xterm", 0);
7902 setconfspawn("spawn_term", "xterm", 0);
7904 setconfspawn("menu", "dmenu_run"
7908 " -nf $bar_font_color"
7910 " -sf $bar_color", 0);
7912 setconfspawn("search", "dmenu"
7917 " -nf $bar_font_color"
7919 " -sf $bar_color", 0);
7921 setconfspawn("name_workspace", "dmenu"
7926 " -nf $bar_font_color"
7928 " -sf $bar_color", 0);
7930 /* These are not verified for existence, even with a binding set. */
7931 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
7932 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
7933 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
7937 #define SWM_MODNAME_SIZE 32
7938 #define SWM_KEY_WS "\n+ \t"
7940 parsebinding(const char *bindstr
, uint16_t *mod
, enum binding_type
*type
,
7941 uint32_t *val
, uint32_t *flags
)
7943 char *str
, *cp
, *name
;
7944 KeySym ks
, lks
, uks
;
7946 DNPRINTF(SWM_D_KEY
, "parsebinding: enter [%s]\n", bindstr
);
7947 if (mod
== NULL
|| val
== NULL
) {
7948 DNPRINTF(SWM_D_KEY
, "parsebinding: no mod or key vars\n");
7951 if (bindstr
== NULL
|| strlen(bindstr
) == 0) {
7952 DNPRINTF(SWM_D_KEY
, "parsebinding: no bindstr\n");
7956 if ((cp
= str
= strdup(bindstr
)) == NULL
)
7957 err(1, "parsebinding: strdup");
7959 *val
= XCB_NO_SYMBOL
;
7963 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
7964 DNPRINTF(SWM_D_KEY
, "parsebinding: entry [%s]\n", name
);
7966 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
7967 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
7969 else if (strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
) == 0)
7970 *mod
|= XCB_MOD_MASK_1
;
7971 else if (strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
) == 0)
7972 *mod
|= XCB_MOD_MASK_2
;
7973 else if (strncmp(name
, "Mod3", SWM_MODNAME_SIZE
) == 0)
7974 *mod
|= XCB_MOD_MASK_3
;
7975 else if (strncmp(name
, "Mod4", SWM_MODNAME_SIZE
) == 0)
7976 *mod
|= XCB_MOD_MASK_4
;
7977 else if (strncmp(name
, "Mod5", SWM_MODNAME_SIZE
) == 0)
7978 *mod
|= XCB_MOD_MASK_5
;
7979 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
7980 *mod
|= XCB_MOD_MASK_SHIFT
;
7981 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
7982 *mod
|= XCB_MOD_MASK_CONTROL
;
7983 else if (strncasecmp(name
, "ANYMOD", SWM_MODNAME_SIZE
) == 0)
7984 *mod
|= XCB_MOD_MASK_ANY
;
7985 else if (strncasecmp(name
, "REPLAY", SWM_MODNAME_SIZE
) == 0)
7986 *flags
|= BINDING_F_REPLAY
;
7987 else if (sscanf(name
, "Button%u", val
) == 1) {
7988 DNPRINTF(SWM_D_KEY
, "parsebinding: button %u\n", *val
);
7990 if (*val
> 255 || *val
== 0) {
7992 "parsebinding: invalid button %u\n", *val
);
7996 /* TODO: do this without Xlib. */
7997 ks
= XStringToKeysym(name
);
7998 if (ks
== NoSymbol
) {
8000 "parsebinding: invalid key %s\n", name
);
8005 XConvertCase(ks
, &lks
, &uks
);
8006 *val
= (uint32_t)lks
;
8010 /* If ANYMOD was specified, ignore the rest. */
8011 if (*mod
& XCB_MOD_MASK_ANY
)
8012 *mod
= XCB_MOD_MASK_ANY
;
8015 DNPRINTF(SWM_D_KEY
, "parsebinding: leave ok\n");
8020 strdupsafe(const char *str
)
8025 return (strdup(str
));
8029 binding_cmp(struct binding
*bp1
, struct binding
*bp2
)
8031 if (bp1
->type
< bp2
->type
)
8033 if (bp1
->type
> bp2
->type
)
8036 if (bp1
->value
< bp2
->value
)
8038 if (bp1
->value
> bp2
->value
)
8041 if (bp1
->mod
< bp2
->mod
)
8043 if (bp1
->mod
> bp2
->mod
)
8050 binding_insert(uint16_t mod
, enum binding_type type
, uint32_t val
,
8051 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
8055 DNPRINTF(SWM_D_KEY
, "binding_insert: mod: %u, type: %d, val: %u, "
8056 "action: %s(%d), spawn_name: %s\n", mod
, type
, val
,
8057 actions
[aid
].name
, aid
, spawn_name
);
8059 if ((bp
= malloc(sizeof *bp
)) == NULL
)
8060 err(1, "binding_insert: malloc");
8067 bp
->spawn_name
= strdupsafe(spawn_name
);
8068 RB_INSERT(binding_tree
, &bindings
, bp
);
8070 DNPRINTF(SWM_D_KEY
, "binding_insert: leave\n");
8074 binding_lookup(uint16_t mod
, enum binding_type type
, uint32_t val
)
8082 return (RB_FIND(binding_tree
, &bindings
, &bp
));
8086 binding_remove(struct binding
*bp
)
8088 DNPRINTF(SWM_D_KEY
, "binding_remove: mod: %u, type: %d, val: %u, "
8089 "action: %s(%d), spawn_name: %s\n", bp
->mod
, bp
->type
, bp
->value
,
8090 actions
[bp
->action
].name
, bp
->action
, bp
->spawn_name
);
8092 RB_REMOVE(binding_tree
, &bindings
, bp
);
8093 free(bp
->spawn_name
);
8096 DNPRINTF(SWM_D_KEY
, "binding_remove: leave\n");
8100 setbinding(uint16_t mod
, enum binding_type type
, uint32_t val
,
8101 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
8105 DNPRINTF(SWM_D_KEY
, "setbinding: enter %s [%s]\n",
8106 actions
[aid
].name
, spawn_name
);
8108 /* Unbind any existing. Loop is to handle MOD_MASK_ANY. */
8109 while ((bp
= binding_lookup(mod
, type
, val
)))
8112 if (aid
!= FN_INVALID
)
8113 binding_insert(mod
, type
, val
, aid
, flags
, spawn_name
);
8115 DNPRINTF(SWM_D_KEY
, "setbinding: leave\n");
8119 setconfbinding(const char *selector
, const char *value
, int flags
)
8121 struct spawn_prog
*sp
;
8122 uint32_t keybtn
, opts
;
8125 enum binding_type type
;
8127 /* suppress unused warning since var is needed */
8130 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter selector: [%s], "
8131 "value: [%s]\n", selector
, value
);
8132 if (selector
== NULL
|| strlen(selector
) == 0) {
8133 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
8134 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8135 setbinding(mod
, type
, keybtn
, FN_INVALID
, opts
, NULL
);
8140 /* search by key function name */
8141 for (aid
= 0; aid
< FN_INVALID
; aid
++) {
8142 if (strncasecmp(selector
, actions
[aid
].name
,
8143 SWM_FUNCNAME_LEN
) == 0) {
8144 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8145 "action\n", selector
);
8146 if (parsebinding(value
, &mod
, &type
, &keybtn
,
8148 setbinding(mod
, type
, keybtn
, aid
, opts
, NULL
);
8154 /* search by custom spawn name */
8155 if ((sp
= spawn_find(selector
)) != NULL
) {
8156 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8157 "spawn\n", selector
);
8158 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8159 setbinding(mod
, type
, keybtn
, FN_SPAWN_CUSTOM
, opts
,
8165 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
8169 #define MODSHIFT MODKEY | XCB_MOD_MASK_SHIFT
8171 setup_keybindings(void)
8173 #define BINDKEY(m, k, a) setbinding(m, KEYBIND, k, a, 0, NULL)
8174 #define BINDKEYSPAWN(m, k, s) setbinding(m, KEYBIND, k, FN_SPAWN_CUSTOM, 0, s)
8175 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8176 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8177 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8178 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8179 BINDKEY(MODKEY
, XK_v
, FN_BUTTON2
);
8180 BINDKEY(MODKEY
, XK_space
, FN_CYCLE_LAYOUT
);
8181 BINDKEY(MODSHIFT
, XK_backslash
, FN_FLIP_LAYOUT
);
8182 BINDKEY(MODKEY
, XK_t
, FN_FLOAT_TOGGLE
);
8183 BINDKEY(MODKEY
, XK_m
, FN_FOCUS_MAIN
);
8184 BINDKEY(MODKEY
, XK_j
, FN_FOCUS_NEXT
);
8185 BINDKEY(MODKEY
, XK_Tab
, FN_FOCUS_NEXT
);
8186 BINDKEY(MODKEY
, XK_k
, FN_FOCUS_PREV
);
8187 BINDKEY(MODSHIFT
, XK_Tab
, FN_FOCUS_PREV
);
8188 BINDKEY(MODKEY
, XK_u
, FN_FOCUS_URGENT
);
8189 BINDKEY(MODSHIFT
, XK_e
, FN_FULLSCREEN_TOGGLE
);
8190 BINDKEY(MODKEY
, XK_e
, FN_MAXIMIZE_TOGGLE
);
8191 BINDKEY(MODSHIFT
, XK_equal
, FN_HEIGHT_GROW
);
8192 BINDKEY(MODSHIFT
, XK_minus
, FN_HEIGHT_SHRINK
);
8193 BINDKEY(MODKEY
, XK_w
, FN_ICONIFY
);
8194 BINDKEY(MODKEY
, XK_h
, FN_MASTER_SHRINK
);
8195 BINDKEY(MODKEY
, XK_l
, FN_MASTER_GROW
);
8196 BINDKEY(MODKEY
, XK_comma
, FN_MASTER_ADD
);
8197 BINDKEY(MODKEY
, XK_period
, FN_MASTER_DEL
);
8198 BINDKEY(MODSHIFT
, XK_bracketright
, FN_MOVE_DOWN
);
8199 BINDKEY(MODKEY
, XK_bracketleft
, FN_MOVE_LEFT
);
8200 BINDKEY(MODKEY
, XK_bracketright
, FN_MOVE_RIGHT
);
8201 BINDKEY(MODSHIFT
, XK_bracketleft
, FN_MOVE_UP
);
8202 BINDKEY(MODSHIFT
, XK_KP_End
, FN_MVRG_1
);
8203 BINDKEY(MODSHIFT
, XK_KP_Down
, FN_MVRG_2
);
8204 BINDKEY(MODSHIFT
, XK_KP_Next
, FN_MVRG_3
);
8205 BINDKEY(MODSHIFT
, XK_KP_Left
, FN_MVRG_4
);
8206 BINDKEY(MODSHIFT
, XK_KP_Begin
, FN_MVRG_5
);
8207 BINDKEY(MODSHIFT
, XK_KP_Right
, FN_MVRG_6
);
8208 BINDKEY(MODSHIFT
, XK_KP_Home
, FN_MVRG_7
);
8209 BINDKEY(MODSHIFT
, XK_KP_Up
, FN_MVRG_8
);
8210 BINDKEY(MODSHIFT
, XK_KP_Prior
, FN_MVRG_9
);
8211 BINDKEY(MODSHIFT
, XK_1
, FN_MVWS_1
);
8212 BINDKEY(MODSHIFT
, XK_2
, FN_MVWS_2
);
8213 BINDKEY(MODSHIFT
, XK_3
, FN_MVWS_3
);
8214 BINDKEY(MODSHIFT
, XK_4
, FN_MVWS_4
);
8215 BINDKEY(MODSHIFT
, XK_5
, FN_MVWS_5
);
8216 BINDKEY(MODSHIFT
, XK_6
, FN_MVWS_6
);
8217 BINDKEY(MODSHIFT
, XK_7
, FN_MVWS_7
);
8218 BINDKEY(MODSHIFT
, XK_8
, FN_MVWS_8
);
8219 BINDKEY(MODSHIFT
, XK_9
, FN_MVWS_9
);
8220 BINDKEY(MODSHIFT
, XK_0
, FN_MVWS_10
);
8221 BINDKEY(MODSHIFT
, XK_F1
, FN_MVWS_11
);
8222 BINDKEY(MODSHIFT
, XK_F2
, FN_MVWS_12
);
8223 BINDKEY(MODSHIFT
, XK_F3
, FN_MVWS_13
);
8224 BINDKEY(MODSHIFT
, XK_F4
, FN_MVWS_14
);
8225 BINDKEY(MODSHIFT
, XK_F5
, FN_MVWS_15
);
8226 BINDKEY(MODSHIFT
, XK_F6
, FN_MVWS_16
);
8227 BINDKEY(MODSHIFT
, XK_F7
, FN_MVWS_17
);
8228 BINDKEY(MODSHIFT
, XK_F8
, FN_MVWS_18
);
8229 BINDKEY(MODSHIFT
, XK_F9
, FN_MVWS_19
);
8230 BINDKEY(MODSHIFT
, XK_F10
, FN_MVWS_20
);
8231 BINDKEY(MODSHIFT
, XK_F11
, FN_MVWS_21
);
8232 BINDKEY(MODSHIFT
, XK_F12
, FN_MVWS_22
);
8233 BINDKEY(MODSHIFT
, XK_slash
, FN_NAME_WORKSPACE
);
8234 BINDKEY(MODSHIFT
, XK_q
, FN_QUIT
);
8235 BINDKEY(MODKEY
, XK_r
, FN_RAISE
);
8236 BINDKEY(MODSHIFT
, XK_r
, FN_RAISE_TOGGLE
);
8237 BINDKEY(MODKEY
, XK_q
, FN_RESTART
);
8238 BINDKEY(MODKEY
, XK_KP_End
, FN_RG_1
);
8239 BINDKEY(MODKEY
, XK_KP_Down
, FN_RG_2
);
8240 BINDKEY(MODKEY
, XK_KP_Next
, FN_RG_3
);
8241 BINDKEY(MODKEY
, XK_KP_Left
, FN_RG_4
);
8242 BINDKEY(MODKEY
, XK_KP_Begin
, FN_RG_5
);
8243 BINDKEY(MODKEY
, XK_KP_Right
, FN_RG_6
);
8244 BINDKEY(MODKEY
, XK_KP_Home
, FN_RG_7
);
8245 BINDKEY(MODKEY
, XK_KP_Up
, FN_RG_8
);
8246 BINDKEY(MODKEY
, XK_KP_Prior
, FN_RG_9
);
8247 BINDKEY(MODSHIFT
, XK_Right
, FN_RG_NEXT
);
8248 BINDKEY(MODSHIFT
, XK_Left
, FN_RG_PREV
);
8249 BINDKEY(MODKEY
, XK_f
, FN_SEARCH_WIN
);
8250 BINDKEY(MODKEY
, XK_slash
, FN_SEARCH_WORKSPACE
);
8251 BINDKEYSPAWN(MODSHIFT
, XK_i
, "initscr");
8252 BINDKEYSPAWN(MODSHIFT
, XK_Delete
, "lock");
8253 BINDKEYSPAWN(MODKEY
, XK_p
, "menu");
8254 BINDKEYSPAWN(MODKEY
, XK_s
, "screenshot_all");
8255 BINDKEYSPAWN(MODSHIFT
, XK_s
, "screenshot_wind");
8256 BINDKEYSPAWN(MODSHIFT
, XK_Return
, "term");
8257 BINDKEY(MODSHIFT
, XK_comma
, FN_STACK_INC
);
8258 BINDKEY(MODSHIFT
, XK_period
, FN_STACK_DEC
);
8259 BINDKEY(MODSHIFT
, XK_space
, FN_STACK_RESET
);
8260 BINDKEY(MODKEY
, XK_Return
, FN_SWAP_MAIN
);
8261 BINDKEY(MODSHIFT
, XK_j
, FN_SWAP_NEXT
);
8262 BINDKEY(MODSHIFT
, XK_k
, FN_SWAP_PREV
);
8263 BINDKEY(MODSHIFT
, XK_w
, FN_UNICONIFY
);
8264 BINDKEY(MODSHIFT
, XK_v
, FN_VERSION
);
8265 BINDKEY(MODKEY
, XK_equal
, FN_WIDTH_GROW
);
8266 BINDKEY(MODKEY
, XK_minus
, FN_WIDTH_SHRINK
);
8267 BINDKEY(MODKEY
, XK_x
, FN_WIND_DEL
);
8268 BINDKEY(MODSHIFT
, XK_x
, FN_WIND_KILL
);
8269 BINDKEY(MODKEY
, XK_1
, FN_WS_1
);
8270 BINDKEY(MODKEY
, XK_2
, FN_WS_2
);
8271 BINDKEY(MODKEY
, XK_3
, FN_WS_3
);
8272 BINDKEY(MODKEY
, XK_4
, FN_WS_4
);
8273 BINDKEY(MODKEY
, XK_5
, FN_WS_5
);
8274 BINDKEY(MODKEY
, XK_6
, FN_WS_6
);
8275 BINDKEY(MODKEY
, XK_7
, FN_WS_7
);
8276 BINDKEY(MODKEY
, XK_8
, FN_WS_8
);
8277 BINDKEY(MODKEY
, XK_9
, FN_WS_9
);
8278 BINDKEY(MODKEY
, XK_0
, FN_WS_10
);
8279 BINDKEY(MODKEY
, XK_F1
, FN_WS_11
);
8280 BINDKEY(MODKEY
, XK_F2
, FN_WS_12
);
8281 BINDKEY(MODKEY
, XK_F3
, FN_WS_13
);
8282 BINDKEY(MODKEY
, XK_F4
, FN_WS_14
);
8283 BINDKEY(MODKEY
, XK_F5
, FN_WS_15
);
8284 BINDKEY(MODKEY
, XK_F6
, FN_WS_16
);
8285 BINDKEY(MODKEY
, XK_F7
, FN_WS_17
);
8286 BINDKEY(MODKEY
, XK_F8
, FN_WS_18
);
8287 BINDKEY(MODKEY
, XK_F9
, FN_WS_19
);
8288 BINDKEY(MODKEY
, XK_F10
, FN_WS_20
);
8289 BINDKEY(MODKEY
, XK_F11
, FN_WS_21
);
8290 BINDKEY(MODKEY
, XK_F12
, FN_WS_22
);
8291 BINDKEY(MODKEY
, XK_Right
, FN_WS_NEXT
);
8292 BINDKEY(MODKEY
, XK_Left
, FN_WS_PREV
);
8293 BINDKEY(MODKEY
, XK_Up
, FN_WS_NEXT_ALL
);
8294 BINDKEY(MODKEY
, XK_Down
, FN_WS_PREV_ALL
);
8295 BINDKEY(MODSHIFT
, XK_Up
, FN_WS_NEXT_MOVE
);
8296 BINDKEY(MODSHIFT
, XK_Down
, FN_WS_PREV_MOVE
);
8297 BINDKEY(MODKEY
, XK_a
, FN_WS_PRIOR
);
8299 BINDKEY(MODKEY
, XK_d
, FN_DEBUG_TOGGLE
);
8300 BINDKEY(MODSHIFT
, XK_d
, FN_DUMPWINS
);
8307 setup_btnbindings(void)
8309 setbinding(ANYMOD
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_FOCUS
,
8310 BINDING_F_REPLAY
, NULL
);
8311 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE
, 0, NULL
);
8312 setbinding(MODSHIFT
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE_CENTERED
, 0,
8314 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_MOVE
, 0, NULL
);
8319 clear_bindings(void)
8323 while ((bp
= RB_ROOT(&bindings
)))
8328 clear_keybindings(void)
8330 struct binding
*bp
, *bptmp
;
8332 RB_FOREACH_SAFE(bp
, binding_tree
, &bindings
, bptmp
) {
8333 if (bp
->type
!= KEYBIND
)
8340 setkeymapping(const char *selector
, const char *value
, int flags
)
8342 char *keymapping_file
;
8344 /* suppress unused warnings since vars are needed */
8348 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
8350 keymapping_file
= expand_tilde(value
);
8352 clear_keybindings();
8353 /* load new key bindings; if it fails, revert to default bindings */
8354 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
8355 clear_keybindings();
8356 setup_keybindings();
8359 free(keymapping_file
);
8361 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
8366 updatenumlockmask(void)
8369 xcb_get_modifier_mapping_reply_t
*modmap_r
;
8370 xcb_keycode_t
*modmap
, kc
, *keycode
;
8374 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
8375 xcb_get_modifier_mapping(conn
),
8378 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
8379 for (i
= 0; i
< 8; i
++) {
8380 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
8381 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
8383 keycode
= xcb_key_symbols_get_keycode(syms
,
8387 numlockmask
= (1 << i
);
8394 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
8401 int num_screens
, k
, j
;
8402 uint16_t modifiers
[4];
8403 xcb_keycode_t
*code
;
8405 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
8406 updatenumlockmask();
8409 modifiers
[1] = numlockmask
;
8410 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8411 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8413 num_screens
= get_screen_count();
8414 for (k
= 0; k
< num_screens
; k
++) {
8415 if (TAILQ_EMPTY(&screens
[k
].rl
))
8417 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
8419 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8420 if (bp
->type
!= KEYBIND
)
8423 /* If there is a catch-all, only bind that. */
8424 if ((binding_lookup(ANYMOD
, KEYBIND
, bp
->value
)) &&
8428 /* Skip unused ws binds. */
8429 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8430 bp
->action
<= FN_WS_22
)
8433 /* Skip unused mvws binds. */
8434 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8435 bp
->action
<= FN_MVWS_22
)
8438 if ((code
= xcb_key_symbols_get_keycode(syms
,
8439 bp
->value
)) == NULL
)
8442 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8443 /* All modifiers are grabbed in one pass. */
8444 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8445 "key: %u, modifiers: %d\n", bp
->value
,
8447 xcb_grab_key(conn
, 1, screens
[k
].root
,
8448 bp
->mod
, *code
, XCB_GRAB_MODE_ASYNC
,
8449 XCB_GRAB_MODE_SYNC
);
8451 /* Need to grab each modifier permutation. */
8452 for (j
= 0; j
< LENGTH(modifiers
); j
++) {
8453 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8454 "key: %u, modifiers: %d\n",
8455 bp
->value
, bp
->mod
| modifiers
[j
]);
8456 xcb_grab_key(conn
, 1,
8458 bp
->mod
| modifiers
[j
],
8459 *code
, XCB_GRAB_MODE_ASYNC
,
8460 XCB_GRAB_MODE_SYNC
);
8472 int num_screens
, i
, k
;
8473 uint16_t modifiers
[4];
8475 DNPRINTF(SWM_D_MOUSE
, "grabbuttons\n");
8476 updatenumlockmask();
8479 modifiers
[1] = numlockmask
;
8480 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8481 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8483 num_screens
= get_screen_count();
8484 for (k
= 0; k
< num_screens
; k
++) {
8485 if (TAILQ_EMPTY(&screens
[k
].rl
))
8487 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, screens
[k
].root
,
8489 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8490 if (bp
->type
!= BTNBIND
)
8493 /* If there is a catch-all, only bind that. */
8494 if ((binding_lookup(ANYMOD
, BTNBIND
, bp
->value
)) &&
8498 /* Skip unused ws binds. */
8499 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8500 bp
->action
<= FN_WS_22
)
8503 /* Skip unused mvws binds. */
8504 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8505 bp
->action
<= FN_MVWS_22
)
8508 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8509 /* All modifiers are grabbed in one pass. */
8510 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: grab, "
8511 "button: %u, modifiers: %d\n", bp
->value
,
8513 xcb_grab_button(conn
, 0, screens
[k
].root
,
8514 BUTTONMASK
, XCB_GRAB_MODE_SYNC
,
8515 XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
8516 XCB_CURSOR_NONE
, bp
->value
, bp
->mod
);
8518 /* Need to grab each modifier permutation. */
8519 for (i
= 0; i
< LENGTH(modifiers
); ++i
) {
8520 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: "
8521 "grab, button: %u, modifiers: %u\n",
8522 bp
->value
, bp
->mod
| modifiers
[i
]);
8523 xcb_grab_button(conn
, 0,
8524 screens
[k
].root
, BUTTONMASK
,
8526 XCB_GRAB_MODE_ASYNC
,
8528 XCB_CURSOR_NONE
, bp
->value
,
8529 bp
->mod
| modifiers
[i
]);
8536 const char *quirkname
[] = {
8537 "NONE", /* config string for "no value" */
8545 "FOCUSONMAP_SINGLE",
8553 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
8554 #define SWM_Q_DELIM "\n|+ \t"
8556 parsequirks(const char *qstr
, uint32_t *quirk
, int *ws
)
8558 char *str
, *cp
, *name
;
8561 if (quirk
== NULL
|| qstr
== NULL
)
8564 if ((cp
= str
= strdup(qstr
)) == NULL
)
8565 err(1, "parsequirks: strdup");
8568 while ((name
= strsep(&cp
, SWM_Q_DELIM
)) != NULL
) {
8570 cp
+= (long)strspn(cp
, SWM_Q_DELIM
);
8572 if (sscanf(name
, "WS[%d]", ws
) == 1) {
8578 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
8579 if (strncasecmp(name
, quirkname
[i
],
8580 SWM_QUIRK_LEN
) == 0) {
8581 DNPRINTF(SWM_D_QUIRK
,
8582 "parsequirks: %s\n", name
);
8588 *quirk
|= 1 << (i
-1);
8592 if (i
>= LENGTH(quirkname
)) {
8593 DNPRINTF(SWM_D_QUIRK
,
8594 "parsequirks: invalid quirk [%s]\n", name
);
8605 quirk_insert(const char *class, const char *instance
, const char *name
,
8606 uint32_t quirk
, int ws
)
8610 bool failed
= false;
8612 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: class: %s, instance: %s, name: %s,"
8613 " value: %u, ws: %d\n", class, instance
, name
, quirk
, ws
);
8615 if ((qp
= malloc(sizeof *qp
)) == NULL
)
8616 err(1, "quirk_insert: malloc");
8618 if ((qp
->class = strdup(class)) == NULL
)
8619 err(1, "quirk_insert: strdup");
8620 if ((qp
->instance
= strdup(instance
)) == NULL
)
8621 err(1, "quirk_insert: strdup");
8622 if ((qp
->name
= strdup(name
)) == NULL
)
8623 err(1, "quirk_insert: strdup");
8625 if (asprintf(&str
, "^%s$", class) == -1)
8626 err(1, "quirk_insert: asprintf");
8627 if (regcomp(&qp
->regex_class
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8628 add_startup_exception("regex failed to compile quirk 'class' "
8629 "field: %s", class);
8632 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8635 if (asprintf(&str
, "^%s$", instance
) == -1)
8636 err(1, "quirk_insert: asprintf");
8637 if (regcomp(&qp
->regex_instance
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8638 add_startup_exception("regex failed to compile quirk 'instance'"
8639 " field: %s", instance
);
8642 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8645 if (asprintf(&str
, "^%s$", name
) == -1)
8646 err(1, "quirk_insert: asprintf");
8647 if (regcomp(&qp
->regex_name
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8648 add_startup_exception("regex failed to compile quirk 'name' "
8652 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8656 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: regex error; skipping.\n");
8661 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
8663 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
8667 quirk_remove(struct quirk
*qp
)
8669 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%u]\n", qp
->class,
8670 qp
->name
, qp
->quirk
);
8672 TAILQ_REMOVE(&quirks
, qp
, entry
);
8675 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
8679 quirk_free(struct quirk
*qp
)
8681 regfree(&qp
->regex_class
);
8682 regfree(&qp
->regex_instance
);
8683 regfree(&qp
->regex_name
);
8695 while ((qp
= TAILQ_FIRST(&quirks
)) != NULL
) {
8701 quirk_replace(struct quirk
*qp
, const char *class, const char *instance
,
8702 const char *name
, uint32_t quirk
, int ws
)
8704 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp
->class,
8705 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
8708 quirk_insert(class, instance
, name
, quirk
, ws
);
8710 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
8714 setquirk(const char *class, const char *instance
, const char *name
,
8715 uint32_t quirk
, int ws
)
8719 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
8720 instance
, name
, quirk
, ws
);
8722 /* Remove/replace existing quirk. */
8723 TAILQ_FOREACH(qp
, &quirks
, entry
) {
8724 if (strcmp(qp
->class, class) == 0 &&
8725 strcmp(qp
->instance
, instance
) == 0 &&
8726 strcmp(qp
->name
, name
) == 0) {
8727 if (quirk
== 0 && ws
== -1)
8730 quirk_replace(qp
, class, instance
, name
, quirk
,
8732 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8737 /* Only insert if quirk is not NONE or forced ws is set. */
8738 if (quirk
|| ws
!= -1)
8739 quirk_insert(class, instance
, name
, quirk
, ws
);
8741 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8744 /* Eat '\' in str used to escape square brackets and colon. */
8746 unescape_selector(char *str
)
8750 for (cp
= str
; *str
!= '\0'; ++str
, ++cp
) {
8751 if (*str
== '\\' && (*(str
+ 1) == ':' || *(str
+ 1) == ']' ||
8761 setconfquirk(const char *selector
, const char *value
, int flags
)
8763 char *str
, *cp
, *class;
8764 char *instance
= NULL
, *name
= NULL
;
8765 int retval
, count
= 0, ws
= -1;
8768 /* suppress unused warning since var is needed */
8771 if (selector
== NULL
|| strlen(selector
) == 0)
8774 if ((str
= strdup(selector
)) == NULL
)
8775 err(1, "setconfquirk: strdup");
8777 /* Find non-escaped colon. */
8784 for (++cp
; *cp
!= '\0'; ++cp
) {
8785 if (*cp
== ':' && *(cp
- 1) != '\\') {
8791 unescape_selector(class);
8793 instance
= class + strlen(class) + 1;
8794 unescape_selector(instance
);
8800 name
= instance
+ strlen(instance
) + 1;
8801 unescape_selector(name
);
8806 DNPRINTF(SWM_D_CONF
, "setconfquirk: class: %s, instance: %s, "
8807 "name: %s\n", class, instance
, name
);
8809 if ((retval
= parsequirks(value
, &qrks
, &ws
)) == 0)
8810 setquirk(class, instance
, name
, qrks
, ws
);
8819 setquirk("MPlayer", "xv", ".*",
8820 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
, -1);
8821 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
8823 setquirk("Firefox-bin", "firefox-bin", ".*",
8825 setquirk("Firefox", "Dialog", ".*",
8827 setquirk("Gimp", "gimp", ".*",
8828 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8829 setquirk("XTerm", "xterm", ".*",
8830 SWM_Q_XTERM_FONTADJ
, -1);
8831 setquirk("xine", "Xine Window", ".*",
8832 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8833 setquirk("Xitk", "Xitk Combo", ".*",
8834 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8835 setquirk("xine", "xine Panel", ".*",
8836 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8837 setquirk("Xitk", "Xine Window", ".*",
8838 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8839 setquirk("xine", "xine Video Fullscreen Window", ".*",
8840 SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
, -1);
8841 setquirk("pcb", "pcb", ".*",
8843 setquirk("SDL_App", "SDL_App", ".*",
8844 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
, -1);
8847 /* conf file stuff */
8848 #define SWM_CONF_FILE "spectrwm.conf"
8849 #define SWM_CONF_FILE_OLD "scrotwm.conf"
8853 SWM_S_BAR_AT_BOTTOM
,
8854 SWM_S_BAR_BORDER_WIDTH
,
8857 SWM_S_BAR_ENABLED_WS
,
8862 SWM_S_BOUNDARY_WIDTH
,
8863 SWM_S_CLOCK_ENABLED
,
8866 SWM_S_CYCLE_VISIBLE
,
8868 SWM_S_DISABLE_BORDER
,
8870 SWM_S_FOCUS_CLOSE_WRAP
,
8871 SWM_S_FOCUS_DEFAULT
,
8873 SWM_S_ICONIC_ENABLED
,
8874 SWM_S_JAVA_WORKAROUND
,
8875 SWM_S_MAXIMIZE_HIDE_BAR
,
8876 SWM_S_REGION_PADDING
,
8881 SWM_S_STACK_ENABLED
,
8884 SWM_S_URGENT_COLLAPSE
,
8885 SWM_S_URGENT_ENABLED
,
8886 SWM_S_VERBOSE_LAYOUT
,
8889 SWM_S_WINDOW_CLASS_ENABLED
,
8890 SWM_S_WINDOW_INSTANCE_ENABLED
,
8891 SWM_S_WINDOW_NAME_ENABLED
,
8892 SWM_S_WORKSPACE_CLAMP
,
8893 SWM_S_WORKSPACE_LIMIT
,
8894 SWM_S_WORKSPACE_NAME
,
8898 setconfvalue(const char *selector
, const char *value
, int flags
)
8900 struct workspace
*ws
;
8901 int i
, ws_id
, num_screens
;
8902 char *b
, *str
, *sp
, s
[1024];
8905 case SWM_S_BAR_ACTION
:
8907 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
8908 err(1, "setconfvalue: bar_action");
8910 case SWM_S_BAR_AT_BOTTOM
:
8911 bar_at_bottom
= (atoi(value
) != 0);
8913 case SWM_S_BAR_BORDER_WIDTH
:
8914 bar_border_width
= atoi(value
);
8915 if (bar_border_width
< 0)
8916 bar_border_width
= 0;
8918 case SWM_S_BAR_DELAY
:
8919 /* No longer needed; leave to not break old conf files. */
8921 case SWM_S_BAR_ENABLED
:
8922 bar_enabled
= (atoi(value
) != 0);
8924 case SWM_S_BAR_ENABLED_WS
:
8925 ws_id
= atoi(selector
) - 1;
8926 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8927 errx(1, "setconfvalue: bar_enabled_ws: invalid "
8928 "workspace %d.", ws_id
+ 1);
8930 num_screens
= get_screen_count();
8931 for (i
= 0; i
< num_screens
; i
++) {
8932 ws
= (struct workspace
*)&screens
[i
].ws
;
8933 ws
[ws_id
].bar_enabled
= (atoi(value
) != 0);
8936 case SWM_S_BAR_FONT
:
8938 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
8939 err(1, "setconfvalue: asprintf: failed to allocate "
8940 "memory for bar_fonts.");
8943 /* If already in xft mode, then we are done. */
8944 if (!bar_font_legacy
)
8947 if ((sp
= str
= strdup(value
)) == NULL
)
8948 err(1, "setconfvalue: strdup");
8950 /* If there are any non-XLFD entries, switch to Xft mode. */
8951 while ((b
= strsep(&sp
, ",")) != NULL
) {
8955 bar_font_legacy
= false;
8962 case SWM_S_BAR_FORMAT
:
8964 if ((bar_format
= strdup(value
)) == NULL
)
8965 err(1, "setconfvalue: bar_format");
8967 case SWM_S_BAR_JUSTIFY
:
8968 if (strcmp(value
, "left") == 0)
8969 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
8970 else if (strcmp(value
, "center") == 0)
8971 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
8972 else if (strcmp(value
, "right") == 0)
8973 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
8975 errx(1, "invalid bar_justify");
8977 case SWM_S_BORDER_WIDTH
:
8978 border_width
= atoi(value
);
8979 if (border_width
< 0)
8982 case SWM_S_BOUNDARY_WIDTH
:
8983 boundary_width
= atoi(value
);
8984 if (boundary_width
< 0)
8987 case SWM_S_CLOCK_ENABLED
:
8988 clock_enabled
= (atoi(value
) != 0);
8990 case SWM_S_CLOCK_FORMAT
:
8991 #ifndef SWM_DENY_CLOCK_FORMAT
8993 if ((clock_format
= strdup(value
)) == NULL
)
8994 err(1, "setconfvalue: clock_format");
8997 case SWM_S_CYCLE_EMPTY
:
8998 cycle_empty
= (atoi(value
) != 0);
9000 case SWM_S_CYCLE_VISIBLE
:
9001 cycle_visible
= (atoi(value
) != 0);
9003 case SWM_S_DIALOG_RATIO
:
9004 dialog_ratio
= atof(value
);
9005 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
9008 case SWM_S_DISABLE_BORDER
:
9009 disable_border
= (atoi(value
) != 0);
9011 case SWM_S_FOCUS_CLOSE
:
9012 if (strcmp(value
, "first") == 0)
9013 focus_close
= SWM_STACK_BOTTOM
;
9014 else if (strcmp(value
, "last") == 0)
9015 focus_close
= SWM_STACK_TOP
;
9016 else if (strcmp(value
, "next") == 0)
9017 focus_close
= SWM_STACK_ABOVE
;
9018 else if (strcmp(value
, "previous") == 0)
9019 focus_close
= SWM_STACK_BELOW
;
9021 errx(1, "focus_close");
9023 case SWM_S_FOCUS_CLOSE_WRAP
:
9024 focus_close_wrap
= (atoi(value
) != 0);
9026 case SWM_S_FOCUS_DEFAULT
:
9027 if (strcmp(value
, "last") == 0)
9028 focus_default
= SWM_STACK_TOP
;
9029 else if (strcmp(value
, "first") == 0)
9030 focus_default
= SWM_STACK_BOTTOM
;
9032 errx(1, "focus_default");
9034 case SWM_S_FOCUS_MODE
:
9035 if (strcmp(value
, "default") == 0)
9036 focus_mode
= SWM_FOCUS_DEFAULT
;
9037 else if (strcmp(value
, "follow") == 0 ||
9038 strcmp(value
, "follow_cursor") == 0)
9039 focus_mode
= SWM_FOCUS_FOLLOW
;
9040 else if (strcmp(value
, "manual") == 0)
9041 focus_mode
= SWM_FOCUS_MANUAL
;
9043 errx(1, "focus_mode");
9045 case SWM_S_ICONIC_ENABLED
:
9046 iconic_enabled
= (atoi(value
) != 0);
9048 case SWM_S_JAVA_WORKAROUND
:
9049 java_workaround
= (atoi(value
) != 0);
9051 case SWM_S_MAXIMIZE_HIDE_BAR
:
9052 maximize_hide_bar
= atoi(value
);
9054 case SWM_S_REGION_PADDING
:
9055 region_padding
= atoi(value
);
9056 if (region_padding
< 0)
9059 case SWM_S_SPAWN_ORDER
:
9060 if (strcmp(value
, "first") == 0)
9061 spawn_position
= SWM_STACK_BOTTOM
;
9062 else if (strcmp(value
, "last") == 0)
9063 spawn_position
= SWM_STACK_TOP
;
9064 else if (strcmp(value
, "next") == 0)
9065 spawn_position
= SWM_STACK_ABOVE
;
9066 else if (strcmp(value
, "previous") == 0)
9067 spawn_position
= SWM_STACK_BELOW
;
9069 errx(1, "spawn_position");
9071 case SWM_S_SPAWN_TERM
:
9072 setconfspawn("term", value
, 0);
9073 setconfspawn("spawn_term", value
, 0);
9076 /* No longer needed; leave to not break old conf files. */
9078 case SWM_S_SS_ENABLED
:
9079 /* No longer needed; leave to not break old conf files. */
9081 case SWM_S_STACK_ENABLED
:
9082 stack_enabled
= (atoi(value
) != 0);
9084 case SWM_S_TERM_WIDTH
:
9085 term_width
= atoi(value
);
9089 case SWM_S_TILE_GAP
:
9090 tile_gap
= atoi(value
);
9092 case SWM_S_URGENT_COLLAPSE
:
9093 urgent_collapse
= (atoi(value
) != 0);
9095 case SWM_S_URGENT_ENABLED
:
9096 urgent_enabled
= (atoi(value
) != 0);
9098 case SWM_S_VERBOSE_LAYOUT
:
9099 verbose_layout
= (atoi(value
) != 0);
9100 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
9102 layouts
[i
].l_string
= fancy_stacker
;
9104 layouts
[i
].l_string
= plain_stacker
;
9107 case SWM_S_WARP_FOCUS
:
9108 warp_focus
= (atoi(value
) != 0);
9110 case SWM_S_WARP_POINTER
:
9111 warp_pointer
= (atoi(value
) != 0);
9113 case SWM_S_WINDOW_CLASS_ENABLED
:
9114 window_class_enabled
= (atoi(value
) != 0);
9116 case SWM_S_WINDOW_INSTANCE_ENABLED
:
9117 window_instance_enabled
= (atoi(value
) != 0);
9119 case SWM_S_WINDOW_NAME_ENABLED
:
9120 window_name_enabled
= (atoi(value
) != 0);
9122 case SWM_S_WORKSPACE_CLAMP
:
9123 workspace_clamp
= (atoi(value
) != 0);
9125 case SWM_S_WORKSPACE_LIMIT
:
9126 workspace_limit
= atoi(value
);
9127 if (workspace_limit
> SWM_WS_MAX
)
9128 workspace_limit
= SWM_WS_MAX
;
9129 else if (workspace_limit
< 1)
9130 workspace_limit
= 1;
9132 ewmh_update_desktops();
9134 case SWM_S_WORKSPACE_NAME
:
9135 if (getenv("SWM_STARTED") != NULL
)
9139 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9140 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
9142 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9143 errx(1, "setconfvalue: workspace_name: invalid "
9144 "workspace %d.", ws_id
+ 1);
9146 num_screens
= get_screen_count();
9147 for (i
= 0; i
< num_screens
; ++i
) {
9148 ws
= (struct workspace
*)&screens
[i
].ws
;
9150 if (strlen(s
) > 0) {
9151 free(ws
[ws_id
].name
);
9152 if ((ws
[ws_id
].name
= strdup(s
)) == NULL
)
9153 err(1, "setconfvalue: workspace_name.");
9155 ewmh_update_desktop_names();
9156 ewmh_get_desktop_names();
9167 setconfmodkey(const char *selector
, const char *value
, int flags
)
9169 /* suppress unused warnings since vars are needed */
9173 if (strncasecmp(value
, "Mod1", strlen("Mod1")) == 0)
9174 update_modkey(XCB_MOD_MASK_1
);
9175 else if (strncasecmp(value
, "Mod2", strlen("Mod2")) == 0)
9176 update_modkey(XCB_MOD_MASK_2
);
9177 else if (strncasecmp(value
, "Mod3", strlen("Mod3")) == 0)
9178 update_modkey(XCB_MOD_MASK_3
);
9179 else if (strncasecmp(value
, "Mod4", strlen("Mod4")) == 0)
9180 update_modkey(XCB_MOD_MASK_4
);
9181 else if (strncasecmp(value
, "Mod5", strlen("Mod5")) == 0)
9182 update_modkey(XCB_MOD_MASK_5
);
9189 setconfcolor(const char *selector
, const char *value
, int flags
)
9191 int first
, last
, i
= 0, num_screens
;
9193 num_screens
= get_screen_count();
9195 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
9196 if (selector
== NULL
|| strlen(selector
) == 0 ||
9197 (last
= atoi(selector
) - 1) < 0) {
9199 last
= num_screens
- 1;
9204 if (last
>= num_screens
) {
9205 add_startup_exception("invalid screen index: %d out of bounds "
9206 "(maximum %d)", last
+ 1, num_screens
);
9210 for (i
= first
; i
<= last
; ++i
) {
9211 setscreencolor(value
, i
, flags
);
9214 * When setting focus/unfocus colors, we need to also
9215 * set maximize colors to match if they haven't been customized.
9217 if (flags
== SWM_S_COLOR_FOCUS
&&
9218 !screens
[i
].c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].manual
)
9219 setscreencolor(value
, i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
9220 else if (flags
== SWM_S_COLOR_UNFOCUS
&&
9221 !screens
[i
].c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].manual
)
9222 setscreencolor(value
, i
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
9224 screens
[i
].c
[flags
].manual
= 1;
9231 setconfregion(const char *selector
, const char *value
, int flags
)
9233 /* suppress unused warnings since vars are needed */
9237 custom_region(value
);
9242 setautorun(const char *selector
, const char *value
, int flags
)
9246 char *ap
, *sp
, *str
;
9252 /* suppress unused warnings since vars are needed */
9256 if (getenv("SWM_STARTED"))
9260 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9261 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
9263 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9264 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
9266 sp
= str
= expand_tilde((char *)&s
);
9269 * This is a little intricate
9271 * If the pid already exists we simply reuse it because it means it was
9272 * used before AND not claimed by manage_window. We get away with
9273 * altering it in the parent after INSERT because this can not be a race
9276 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
9279 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
9281 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
9282 err(1, "setautorun: realloc");
9283 a
.argv
[argc
- 1] = ap
;
9286 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
9287 err(1, "setautorun: realloc");
9288 a
.argv
[argc
] = NULL
;
9290 if ((pid
= fork()) == 0) {
9291 spawn(ws_id
, &a
, true);
9301 p
= calloc(1, sizeof *p
);
9304 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
9314 setlayout(const char *selector
, const char *value
, int flags
)
9316 struct workspace
*ws
;
9317 int ws_id
, i
, x
, mg
, ma
, si
, ar
;
9318 int st
= SWM_V_STACK
, num_screens
;
9322 /* suppress unused warnings since vars are needed */
9326 if (getenv("SWM_STARTED"))
9330 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
9331 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
9332 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9333 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9336 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9337 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
9339 if (strcasecmp(s
, "vertical") == 0)
9341 else if (strcasecmp(s
, "vertical_flip") == 0) {
9344 } else if (strcasecmp(s
, "horizontal") == 0)
9346 else if (strcasecmp(s
, "horizontal_flip") == 0) {
9349 } else if (strcasecmp(s
, "fullscreen") == 0)
9352 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9353 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9356 num_screens
= get_screen_count();
9357 for (i
= 0; i
< num_screens
; i
++) {
9358 ws
= (struct workspace
*)&screens
[i
].ws
;
9359 ws
[ws_id
].cur_layout
= &layouts
[st
];
9361 ws
[ws_id
].always_raise
= (ar
!= 0);
9362 if (st
== SWM_MAX_STACK
)
9366 for (x
= 0; x
< abs(mg
); x
++) {
9367 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9368 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
9369 SWM_ARG_ID_MASTERSHRINK
);
9372 for (x
= 0; x
< abs(ma
); x
++) {
9373 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9374 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
9375 SWM_ARG_ID_MASTERDEL
);
9378 for (x
= 0; x
< abs(si
); x
++) {
9379 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9380 si
>= 0 ? SWM_ARG_ID_STACKINC
:
9381 SWM_ARG_ID_STACKDEC
);
9385 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9386 SWM_ARG_ID_FLIPLAYOUT
);
9393 /* config options */
9394 struct config_option
{
9396 int (*func
)(const char*, const char*, int);
9399 struct config_option configopt
[] = {
9400 { "autorun", setautorun
, 0 },
9401 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
9402 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
9403 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
9404 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
9405 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
9406 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
9407 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
9408 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
9409 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
9410 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
9411 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
9412 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
9413 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
9414 { "bind", setconfbinding
, 0 },
9415 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
9416 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
9417 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
9418 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
9419 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
9420 { "color_focus_maximized", setconfcolor
, SWM_S_COLOR_FOCUS_MAXIMIZED
},
9421 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
9422 { "color_unfocus_maximized", setconfcolor
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
},
9423 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
9424 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
9425 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
9426 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
9427 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
9428 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
9429 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
9430 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
9431 { "iconic_enabled", setconfvalue
, SWM_S_ICONIC_ENABLED
},
9432 { "java_workaround", setconfvalue
, SWM_S_JAVA_WORKAROUND
},
9433 { "keyboard_mapping", setkeymapping
, 0 },
9434 { "layout", setlayout
, 0 },
9435 { "maximize_hide_bar", setconfvalue
, SWM_S_MAXIMIZE_HIDE_BAR
},
9436 { "modkey", setconfmodkey
, 0 },
9437 { "program", setconfspawn
, 0 },
9438 { "quirk", setconfquirk
, 0 },
9439 { "region", setconfregion
, 0 },
9440 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
9441 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
9442 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
9443 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
9444 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
9445 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
9446 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
9447 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
9448 { "title_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
}, /* For backwards compat. */
9449 { "title_name_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
}, /* For backwards compat. */
9450 { "urgent_collapse", setconfvalue
, SWM_S_URGENT_COLLAPSE
},
9451 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
9452 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
9453 { "warp_focus", setconfvalue
, SWM_S_WARP_FOCUS
},
9454 { "warp_pointer", setconfvalue
, SWM_S_WARP_POINTER
},
9455 { "window_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
},
9456 { "window_instance_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
},
9457 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
9458 { "workspace_clamp", setconfvalue
, SWM_S_WORKSPACE_CLAMP
},
9459 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
9460 { "name", setconfvalue
, SWM_S_WORKSPACE_NAME
},
9464 _add_startup_exception(const char *fmt
, va_list ap
)
9466 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
9467 warn("%s: asprintf", __func__
);
9471 add_startup_exception(const char *fmt
, ...)
9477 if (startup_exception
)
9480 /* force bar to be enabled due to exception */
9484 _add_startup_exception(fmt
, ap
);
9489 conf_load(const char *filename
, int keymapping
)
9492 char *line
= NULL
, *cp
, *ce
, *optsub
, *optval
= NULL
;
9493 size_t linelen
, lineno
= 0;
9494 int wordlen
, i
, optidx
, count
;
9495 struct config_option
*opt
= NULL
;
9497 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
9499 if (filename
== NULL
) {
9500 warnx("conf_load: no filename");
9504 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
9506 if ((config
= fopen(filename
, "r")) == NULL
) {
9507 warn("conf_load: fopen: %s", filename
);
9511 while (!feof(config
)) {
9515 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
9516 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
9518 err(1, "%s", filename
);
9523 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9524 if (cp
[0] == '\0') {
9528 /* get config option */
9529 wordlen
= strcspn(cp
, "=[ \t\n");
9531 add_startup_exception("%s: line %zd: no option found",
9536 for (i
= 0; i
< LENGTH(configopt
); i
++) {
9537 opt
= &configopt
[i
];
9538 if (strncasecmp(cp
, opt
->optname
, wordlen
) == 0 &&
9539 (int)strlen(opt
->optname
) == wordlen
) {
9545 add_startup_exception("%s: line %zd: unknown option "
9546 "%.*s", filename
, lineno
, wordlen
, cp
);
9549 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
9550 add_startup_exception("%s: line %zd: invalid option "
9551 "%.*s", filename
, lineno
, wordlen
, cp
);
9555 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9557 /* get [selector] if any */
9562 /* Get length of selector. */
9563 for (ce
= ++cp
; *ce
!= '\0'; ++ce
) {
9564 /* Find matching (not escaped) bracket. */
9565 if (*ce
== ']' && *(ce
- 1) != '\\') {
9572 add_startup_exception("%s: line %zd: syntax "
9573 "error: unterminated selector", filename
,
9578 /* ce points at ']'; terminate optsub. */
9583 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
9586 if (strlen(optval
) == 0) {
9587 add_startup_exception("%s: line %zd: must supply value "
9588 "to %s", filename
, lineno
,
9589 configopt
[optidx
].optname
);
9592 /* call function to deal with it all */
9593 if (configopt
[optidx
].func(optsub
, optval
,
9594 configopt
[optidx
].funcflags
) != 0) {
9595 add_startup_exception("%s: line %zd: invalid data for "
9596 "%s", filename
, lineno
, configopt
[optidx
].optname
);
9605 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
9611 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
9613 struct ws_win
*parent
, *w
;
9614 struct swm_region
*r
;
9615 struct workspace
*ws
;
9616 xcb_icccm_wm_hints_t wmh
;
9618 parent
= find_window(win
->transient
);
9620 parent
->focus_child
= win
;
9622 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
9623 " for %#x trans %#x\n", win
->id
, win
->transient
);
9625 r
= root_to_region(win
->s
->root
, SWM_CK_ALL
);
9627 /* parent doen't exist in our window list */
9628 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
9629 if (xcb_icccm_get_wm_hints_reply(conn
,
9630 xcb_icccm_get_wm_hints(conn
, w
->id
),
9632 warnx("can't get hints for %#x", w
->id
);
9636 if (win
->hints
.window_group
!= wmh
.window_group
)
9639 w
->focus_child
= win
;
9640 win
->transient
= w
->id
;
9642 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
9643 "transient to %#x\n", win
->transient
);
9650 window_get_pid(xcb_window_t win
)
9655 xcb_get_property_cookie_t pc
;
9656 xcb_get_property_reply_t
*pr
;
9658 apid
= get_atom_from_string("_NET_WM_PID");
9659 if (apid
== XCB_ATOM_NONE
)
9662 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
9663 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9666 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
9671 if (pr
->type
== apid
&& pr
->format
== 32)
9672 ret
= *((pid_t
*)xcb_get_property_value(pr
));
9678 apid
= get_atom_from_string("_SWM_PID");
9679 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
9681 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9684 if (pr
->type
!= apid
) {
9689 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
9696 get_swm_ws(xcb_window_t id
)
9702 xcb_get_property_reply_t
*gpr
;
9704 gpr
= xcb_get_property_reply(conn
,
9705 xcb_get_property(conn
, 0, id
, a_swm_ws
,
9706 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
9711 proplen
= xcb_get_property_value_length(gpr
);
9713 prop
= malloc(proplen
+ 1);
9716 xcb_get_property_value(gpr
),
9718 prop
[proplen
] = '\0';
9725 DNPRINTF(SWM_D_PROP
, "get_swm_ws: _SWM_WS: %s\n", prop
);
9726 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
9728 DNPRINTF(SWM_D_PROP
, "get_swm_ws: win #%s: %s",
9738 get_ws_idx(struct ws_win
*win
)
9740 xcb_get_property_reply_t
*gpr
;
9746 gpr
= xcb_get_property_reply(conn
,
9747 xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
9748 XCB_ATOM_CARDINAL
, 0, 1),
9751 if (gpr
->type
== XCB_ATOM_CARDINAL
&& gpr
->format
== 32)
9752 ws_idx
= *((int *)xcb_get_property_value(gpr
));
9756 if (ws_idx
== -1 && !(win
->quirks
& SWM_Q_IGNORESPAWNWS
))
9757 ws_idx
= get_swm_ws(win
->id
);
9759 if (ws_idx
> workspace_limit
- 1 || ws_idx
< -1)
9762 DNPRINTF(SWM_D_PROP
, "get_ws_idx: win %#x, ws_idx: %d\n", win
->id
,
9769 reparent_window(struct ws_win
*win
)
9771 xcb_void_cookie_t c
;
9772 xcb_generic_error_t
*error
;
9775 win
->frame
= xcb_generate_id(conn
);
9777 DNPRINTF(SWM_D_MISC
, "reparent_window: win %#x, frame: %#x\n",
9778 win
->id
, win
->frame
);
9781 XCB_EVENT_MASK_ENTER_WINDOW
|
9782 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
9783 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
9784 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
|
9785 XCB_EVENT_MASK_EXPOSURE
;
9787 wa
[0] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
9790 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
->frame
, win
->s
->root
,
9791 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), 0,
9792 XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
9793 XCB_CW_EVENT_MASK
, wa
);
9795 win
->state
= SWM_WIN_STATE_REPARENTING
;
9796 c
= xcb_reparent_window_checked(conn
, win
->id
, win
->frame
, 0, 0);
9797 if ((error
= xcb_request_check(conn
, c
))) {
9798 DNPRINTF(SWM_D_MISC
, "reparent_window: error:\n");
9803 xcb_destroy_window(conn
, win
->frame
);
9804 win
->frame
= XCB_WINDOW_NONE
;
9806 xcb_change_save_set(conn
, XCB_SET_MODE_INSERT
, win
->id
);
9808 DNPRINTF(SWM_D_MISC
, "reparent_window: done.\n");
9812 unparent_window(struct ws_win
*win
)
9814 xcb_change_save_set(conn
, XCB_SET_MODE_DELETE
, win
->id
);
9815 xcb_reparent_window(conn
, win
->id
, win
->s
->root
, X(win
), Y(win
));
9816 xcb_destroy_window(conn
, win
->frame
);
9817 win
->frame
= XCB_WINDOW_NONE
;
9818 win
->state
= SWM_WIN_STATE_UNPARENTING
;
9822 manage_window(xcb_window_t id
, int spawn_pos
, bool mapping
)
9824 struct ws_win
*win
= NULL
, *ww
;
9825 struct swm_region
*r
;
9828 xcb_get_geometry_reply_t
*gr
;
9829 xcb_get_window_attributes_reply_t
*war
= NULL
;
9830 xcb_window_t trans
= XCB_WINDOW_NONE
;
9831 uint32_t i
, wa
[1], new_flags
;
9832 int ws_idx
, force_ws
= -1;
9833 char *class, *instance
, *name
;
9836 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region bar; "
9841 if (find_region(id
)) {
9842 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region window; "
9847 if ((win
= find_window(id
)) != NULL
) {
9848 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x already "
9849 "managed; skipping.)\n", id
);
9850 return (win
); /* Already managed. */
9853 /* See if window is on the unmanaged list. */
9854 if ((win
= find_unmanaged_window(id
)) != NULL
) {
9855 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x found on "
9856 "unmanaged list.\n", id
);
9857 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
9860 set_child_transient(win
, &trans
);
9864 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is new.\n", id
);
9867 war
= xcb_get_window_attributes_reply(conn
,
9868 xcb_get_window_attributes(conn
, id
), NULL
);
9870 DNPRINTF(SWM_D_EVENT
, "manage_window: window lost.\n");
9874 if (war
->override_redirect
) {
9875 DNPRINTF(SWM_D_EVENT
, "manage_window: override_redirect; "
9880 if (!mapping
&& war
->map_state
== XCB_MAP_STATE_UNMAPPED
&&
9881 get_win_state(id
) == XCB_ICCCM_WM_STATE_WITHDRAWN
) {
9882 DNPRINTF(SWM_D_EVENT
, "manage_window: window withdrawn; "
9887 /* Try to get initial window geometry. */
9888 gr
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, id
), NULL
);
9890 DNPRINTF(SWM_D_MISC
, "manage_window: get geometry failed.\n");
9894 /* Create and initialize ws_win object. */
9895 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
9896 err(1, "manage_window: calloc: failed to allocate memory for "
9901 /* Figureout which region the window belongs to. */
9902 r
= root_to_region(gr
->root
, SWM_CK_ALL
);
9904 /* Ignore window border if there is one. */
9905 WIDTH(win
) = gr
->width
;
9906 HEIGHT(win
) = gr
->height
;
9907 X(win
) = gr
->x
+ gr
->border_width
;
9908 Y(win
) = gr
->y
+ gr
->border_width
;
9909 win
->bordered
= false;
9910 win
->mapped
= (war
->map_state
!= XCB_MAP_STATE_UNMAPPED
);
9911 win
->s
= r
->s
; /* this never changes */
9915 /* Select which X events to monitor and set border pixel color. */
9916 wa
[0] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
9917 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
9919 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_EVENT_MASK
, wa
);
9921 /* Get WM_SIZE_HINTS. */
9922 xcb_icccm_get_wm_normal_hints_reply(conn
,
9923 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
9927 xcb_icccm_get_wm_hints_reply(conn
,
9928 xcb_icccm_get_wm_hints(conn
, win
->id
),
9931 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
9932 xcb_icccm_get_wm_transient_for_reply(conn
,
9933 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
9936 win
->transient
= trans
;
9937 set_child_transient(win
, &win
->transient
);
9940 /* Get WM_PROTOCOLS. */
9941 get_wm_protocols(win
);
9944 /* Must be after getting WM_HINTS and WM_PROTOCOLS. */
9945 DNPRINTF(SWM_D_FOCUS
, "manage_window: input_model: %s\n",
9946 get_win_input_model(win
));
9949 /* Set initial quirks based on EWMH. */
9950 ewmh_autoquirk(win
);
9952 /* Determine initial quirks. */
9953 xcb_icccm_get_wm_class_reply(conn
,
9954 xcb_icccm_get_wm_class(conn
, win
->id
),
9957 class = win
->ch
.class_name
? win
->ch
.class_name
: "";
9958 instance
= win
->ch
.instance_name
? win
->ch
.instance_name
: "";
9959 name
= get_win_name(win
->id
);
9961 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, instance: %s, "
9962 "name: %s\n", class, instance
, name
);
9964 /* java is retarded so treat it special */
9965 if (strstr(instance
, "sun-awt")) {
9966 DNPRINTF(SWM_D_CLASS
, "manage_window: java window detected.\n");
9970 TAILQ_FOREACH(qp
, &quirks
, entry
) {
9971 if (regexec(&qp
->regex_class
, class, 0, NULL
, 0) == 0 &&
9972 regexec(&qp
->regex_instance
, instance
, 0, NULL
, 0) == 0 &&
9973 regexec(&qp
->regex_name
, name
, 0, NULL
, 0) == 0) {
9974 DNPRINTF(SWM_D_CLASS
, "manage_window: matched "
9975 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp
->class,
9976 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
9977 win
->quirks
= qp
->quirk
;
9978 if (qp
->ws
>= 0 && qp
->ws
< workspace_limit
)
9985 /* Reset font sizes (the bruteforce way; no default keybinding). */
9986 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
9987 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9988 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
9989 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9990 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
9993 /* Figure out which workspace the window belongs to. */
9994 if (!(win
->quirks
& SWM_Q_IGNOREPID
) &&
9995 (p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
9996 win
->ws
= &r
->s
->ws
[p
->ws
];
9997 TAILQ_REMOVE(&pidlist
, p
, entry
);
10000 } else if ((ws_idx
= get_ws_idx(win
)) != -1 &&
10002 /* _SWM_WS is set; use that. */
10003 win
->ws
= &r
->s
->ws
[ws_idx
];
10004 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
10005 /* Launch transients in the same ws as parent. */
10011 if (force_ws
!= -1)
10012 win
->ws
= &r
->s
->ws
[force_ws
];
10014 /* Set the _NET_WM_DESKTOP atom. */
10015 DNPRINTF(SWM_D_PROP
, "manage_window: set _NET_WM_DESKTOP: %d\n",
10017 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
10018 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &win
->ws
->idx
);
10020 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
10021 xcb_delete_property(conn
, win
->id
, a_swm_ws
);
10023 /* WS must already be set for this to work. */
10024 store_float_geom(win
);
10026 /* Make sure window is positioned inside its region, if its active. */
10028 region_containment(win
, r
, SWM_CW_ALLSIDES
|
10029 SWM_CW_HARDBOUNDARY
);
10030 update_window(win
);
10034 /* Figure out where to insert the window in the workspace list. */
10035 if (trans
&& (ww
= find_window(trans
)))
10036 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
10037 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_ABOVE
)
10038 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
10040 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_BELOW
)
10041 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
10042 else switch (spawn_pos
) {
10044 case SWM_STACK_TOP
:
10045 case SWM_STACK_ABOVE
:
10046 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
10048 case SWM_STACK_BOTTOM
:
10049 case SWM_STACK_BELOW
:
10050 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
10053 ewmh_update_client_list();
10055 TAILQ_INSERT_HEAD(&win
->ws
->stack
, win
, stack_entry
);
10058 /* Get/apply initial _NET_WM_STATE */
10059 ewmh_get_wm_state(win
);
10061 /* Apply quirks. */
10062 new_flags
= win
->ewmh_flags
;
10064 if (win
->quirks
& SWM_Q_FLOAT
)
10065 new_flags
|= EWMH_F_ABOVE
;
10067 if (win
->quirks
& SWM_Q_ANYWHERE
)
10068 new_flags
|= SWM_F_MANUAL
;
10070 ewmh_apply_flags(win
, new_flags
);
10071 ewmh_update_wm_state(win
);
10073 /* Set initial _NET_WM_ALLOWED_ACTIONS */
10074 ewmh_update_actions(win
);
10076 reparent_window(win
);
10078 DNPRINTF(SWM_D_MISC
, "manage_window: done. win %#x, (x,y) w x h: "
10079 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win
->id
,
10080 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
10081 YESNO(ICONIC(win
)), win
->transient
);
10088 free_window(struct ws_win
*win
)
10090 DNPRINTF(SWM_D_MISC
, "free_window: win %#x\n", WINID(win
));
10095 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
10098 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
10101 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
10105 unmanage_window(struct ws_win
*win
)
10107 DNPRINTF(SWM_D_MISC
, "unmanage_window: win %#x\n", WINID(win
));
10113 unparent_window(win
);
10115 TAILQ_REMOVE(&win
->ws
->stack
, win
, stack_entry
);
10116 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
10117 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
10119 ewmh_update_client_list();
10123 expose(xcb_expose_event_t
*e
)
10128 struct workspace
*ws
;
10131 DNPRINTF(SWM_D_EVENT
, "expose: win %#x, count: %d\n", e
->window
,
10137 if ((b
= find_bar(e
->window
))) {
10140 } else if ((w
= find_window(e
->window
)) && w
->frame
== e
->window
) {
10144 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
10150 DNPRINTF(SWM_D_EVENT
, "expose: done\n");
10154 focusin(xcb_focus_in_event_t
*e
)
10156 struct ws_win
*win
;
10158 DNPRINTF(SWM_D_EVENT
, "focusin: win %#x, mode: %s(%u), "
10159 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10160 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10161 if ((win
= find_window(e
->event
)) && win
!= win
->ws
->focus
&&
10162 win
!= win
->ws
->focus_pending
&&
10163 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10164 win
->ws
->focus_prev
= win
->ws
->focus
;
10165 win
->ws
->focus
= win
;
10166 win
->ws
->focus_pending
= NULL
;
10168 if (win
->ws
->focus_prev
)
10169 draw_frame(win
->ws
->focus_prev
);
10177 focusout(xcb_focus_out_event_t
*e
)
10179 DNPRINTF(SWM_D_EVENT
, "focusout: win %#x, mode: %s(%u), "
10180 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10181 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10186 keypress(xcb_key_press_event_t
*e
)
10189 struct binding
*bp
;
10190 xcb_keysym_t keysym
;
10191 bool replay
= true;
10193 last_event_time
= e
->time
;
10195 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
10197 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
10198 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10199 "state: %u, cleaned: %u, same_screen: %s\n", keysym
, e
->event
,
10200 e
->event_x
, e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
,
10201 e
->root_y
, e
->child
, e
->state
, CLEANMASK(e
->state
),
10202 YESNO(e
->same_screen
));
10204 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10206 /* Look for catch-all. */
10207 if ((bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
)) == NULL
)
10211 replay
= bp
->flags
& BINDING_F_REPLAY
;
10213 if ((ap
= &actions
[bp
->action
]) == NULL
)
10216 if (bp
->action
== FN_SPAWN_CUSTOM
)
10217 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10220 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10222 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10226 DNPRINTF(SWM_D_EVENT
, "keypress: replaying.\n");
10227 /* Pass keypress to event window and unfreeze keyboard queue. */
10228 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10230 /* Release freeze. */
10231 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10235 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
10239 keyrelease(xcb_key_release_event_t
*e
)
10242 struct binding
*bp
;
10243 xcb_keysym_t keysym
;
10245 last_event_time
= e
->time
;
10247 keysym
= xcb_key_release_lookup_keysym(syms
, e
, 0);
10249 DNPRINTF(SWM_D_EVENT
, "keyrelease: keysym: %u, win (x,y): %#x (%d,%d), "
10250 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10251 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
10252 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
10253 e
->child
, e
->state
, YESNO(e
->same_screen
));
10255 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10257 /* Look for catch-all. */
10258 bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
);
10260 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10261 bp
->flags
& BINDING_F_REPLAY
) {
10262 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10263 DNPRINTF(SWM_D_EVENT
, "keyrelease: replaying.\n");
10265 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10266 DNPRINTF(SWM_D_EVENT
, "keyrelease: unfreezing.\n");
10271 DNPRINTF(SWM_D_EVENT
, "keyrelease: done.\n");
10275 buttonpress(xcb_button_press_event_t
*e
)
10277 struct ws_win
*win
= NULL
, *newf
;
10278 struct swm_region
*r
, *old_r
;
10280 struct binding
*bp
;
10281 bool replay
= true;
10283 last_event_time
= e
->time
;
10285 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): %#x (%d,%d), "
10286 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10287 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10288 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10289 e
->state
, YESNO(e
->same_screen
));
10291 if (e
->event
== e
->root
) {
10293 win
= find_window(e
->child
);
10295 /* Focus on empty region */
10296 /* If no windows on region if its empty. */
10297 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10298 if (r
&& TAILQ_EMPTY(&r
->ws
->winlist
)) {
10299 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
10300 if (old_r
&& old_r
!= r
)
10301 unfocus_win(old_r
->ws
->focus
);
10303 DNPRINTF(SWM_D_FOCUS
, "buttonpress: "
10304 "set_input_focus: %#x, revert-to: parent, "
10305 "time: %#x\n", e
->root
, e
->time
);
10306 xcb_set_input_focus(conn
,
10307 XCB_INPUT_FOCUS_POINTER_ROOT
,
10310 /* Clear bar since empty. */
10313 /* No need to replay event. */
10318 win
= find_window(e
->event
);
10322 newf
= get_focus_magic(win
);
10323 if (win
->ws
->focus
== newf
&& newf
!= win
) {
10324 if (win
->focus_child
== win
)
10325 win
->focus_child
= NULL
;
10331 /* Handle any bound action. */
10332 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10334 /* Look for catch-all. */
10335 if ((bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
)) == NULL
)
10340 replay
= bp
->flags
& BINDING_F_REPLAY
;
10342 if ((ap
= &actions
[bp
->action
]) == NULL
)
10345 if (bp
->action
== FN_SPAWN_CUSTOM
)
10346 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10349 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10351 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10355 DNPRINTF(SWM_D_EVENT
, "buttonpress: replaying.\n");
10356 /* Replay event to event window */
10357 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10359 /* Unfreeze grab events. */
10360 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10367 buttonrelease(xcb_button_release_event_t
*e
)
10370 struct binding
*bp
;
10372 last_event_time
= e
->time
;
10374 DNPRINTF(SWM_D_EVENT
, "buttonrelease: win (x,y): %#x (%d,%d), "
10375 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10376 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10377 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10378 e
->state
, YESNO(e
->same_screen
));
10380 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10382 /* Look for catch-all. */
10383 bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
);
10385 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10386 bp
->flags
& BINDING_F_REPLAY
) {
10387 DNPRINTF(SWM_D_EVENT
, "buttonrelease: replaying.\n");
10388 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10390 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10398 get_win_input_model(struct ws_win
*win
)
10402 * Input Model Input Field WM_TAKE_FOCUS
10403 * No Input False Absent
10404 * Passive True Absent
10405 * Locally Active True Present
10406 * Globally Active False Present
10409 if (ACCEPTS_FOCUS(win
))
10410 inputmodel
= (win
->take_focus
) ? "Locally Active" : "Passive";
10412 inputmodel
= (win
->take_focus
) ? "Globally Active" : "No Input";
10418 print_win_geom(xcb_window_t w
)
10420 xcb_get_geometry_reply_t
*wa
;
10422 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
10424 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: %#x\n",
10429 DNPRINTF(SWM_D_MISC
, "print_win_geom: win %#x, root: %#x, "
10430 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
10431 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
10440 get_stack_mode_name(uint8_t mode
)
10445 case XCB_STACK_MODE_ABOVE
:
10448 case XCB_STACK_MODE_BELOW
:
10451 case XCB_STACK_MODE_TOP_IF
:
10454 case XCB_STACK_MODE_BOTTOM_IF
:
10457 case XCB_STACK_MODE_OPPOSITE
:
10469 configurerequest(xcb_configure_request_event_t
*e
)
10471 struct ws_win
*win
;
10472 struct swm_region
*r
= NULL
;
10474 uint32_t wc
[7] = {0};
10478 if ((win
= find_window(e
->window
)) == NULL
)
10479 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10483 if (swm_debug
& SWM_D_EVENT
) {
10484 print_win_geom(e
->window
);
10486 DNPRINTF(SWM_D_EVENT
, "configurerequest: win %#x, "
10487 "parent: %#x, new: %s, value_mask: %u { ", e
->window
,
10488 e
->parent
, YESNO(new), e
->value_mask
);
10489 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
10490 DPRINTF("X: %d ", e
->x
);
10491 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
10492 DPRINTF("Y: %d ", e
->y
);
10493 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10494 DPRINTF("W: %u ", e
->width
);
10495 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10496 DPRINTF("H: %u ", e
->height
);
10497 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
10498 DPRINTF("Border: %u ", e
->border_width
);
10499 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
10500 DPRINTF("Sibling: %#x ", e
->sibling
);
10501 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
10502 DPRINTF("StackMode: %s(%u) ",
10503 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
10509 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10510 mask
|= XCB_CONFIG_WINDOW_X
;
10513 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10514 mask
|= XCB_CONFIG_WINDOW_Y
;
10517 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
10518 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
10519 wc
[i
++] = e
->width
;
10521 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
10522 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
10523 wc
[i
++] = e
->height
;
10525 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
10526 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
10527 wc
[i
++] = e
->border_width
;
10529 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
10530 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
10531 wc
[i
++] = e
->sibling
;
10533 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
10534 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
10535 wc
[i
++] = e
->stack_mode
;
10539 xcb_configure_window(conn
, e
->window
, mask
, wc
);
10542 } else if ((!MANUAL(win
) || win
->quirks
& SWM_Q_ANYWHERE
) &&
10543 !FULLSCREEN(win
) && !MAXIMIZED(win
)) {
10546 else if (win
->ws
->old_r
)
10547 r
= win
->ws
->old_r
;
10549 /* windows are centered unless ANYWHERE quirk is set. */
10550 if (win
->quirks
& SWM_Q_ANYWHERE
) {
10551 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10552 win
->g_float
.x
= e
->x
;
10554 win
->g_float
.x
-= X(r
);
10557 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10558 win
->g_float
.y
= e
->y
;
10560 win
->g_float
.y
-= Y(r
);
10564 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10565 win
->g_float
.w
= e
->width
;
10567 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10568 win
->g_float
.h
= e
->height
;
10570 win
->g_floatvalid
= true;
10572 if (!MAXIMIZED(win
) && !FULLSCREEN(win
) &&
10573 (TRANS(win
) || (ABOVE(win
) &&
10574 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]))) {
10575 WIDTH(win
) = win
->g_float
.w
;
10576 HEIGHT(win
) = win
->g_float
.h
;
10579 update_floater(win
);
10582 config_win(win
, e
);
10586 config_win(win
, e
);
10590 config_win(win
, e
);
10594 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
10598 configurenotify(xcb_configure_notify_event_t
*e
)
10600 struct ws_win
*win
;
10602 DNPRINTF(SWM_D_EVENT
, "configurenotify: win %#x, event win: %#x, "
10603 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
10604 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
10605 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
10606 YESNO(e
->override_redirect
));
10608 win
= find_window(e
->window
);
10611 if (font_adjusted
&& win
->ws
->r
) {
10619 destroynotify(xcb_destroy_notify_event_t
*e
)
10621 struct ws_win
*win
;
10622 struct workspace
*ws
;
10624 DNPRINTF(SWM_D_EVENT
, "destroynotify: win %#x\n", e
->window
);
10626 if ((win
= find_window(e
->window
)) == NULL
) {
10627 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10629 /* Window is on unmanaged list. */
10630 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10637 if (win
->frame
== e
->window
) {
10638 DNPRINTF(SWM_D_EVENT
, "destroynotify: frame for win %#x\n",
10640 win
->frame
= XCB_WINDOW_NONE
;
10644 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10645 /* If we were focused, make sure we focus on something else. */
10646 if (win
== ws
->focus
) {
10647 ws
->focus_pending
= get_focus_prev(win
);
10648 if (ws
->focus_pending
== win
)
10649 ws
->focus_pending
= NULL
;
10653 unmanage_window(win
);
10654 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10658 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
10659 if (ws
->focus_pending
) {
10660 focus_win(ws
->focus_pending
);
10661 ws
->focus_pending
= NULL
;
10662 } else if (ws
->focus
== NULL
) {
10663 DNPRINTF(SWM_D_FOCUS
, "destroynotify: set_input_focus: "
10664 "%#x, revert-to: parent, time: 0\n", ws
->r
->id
);
10665 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
10666 ws
->r
->id
, XCB_CURRENT_TIME
);
10673 DNPRINTF(SWM_D_EVENT
, "destroynotify: done.\n");
10678 get_notify_detail_label(uint8_t detail
)
10683 case XCB_NOTIFY_DETAIL_ANCESTOR
:
10684 label
= "Ancestor";
10686 case XCB_NOTIFY_DETAIL_VIRTUAL
:
10689 case XCB_NOTIFY_DETAIL_INFERIOR
:
10690 label
= "Inferior";
10692 case XCB_NOTIFY_DETAIL_NONLINEAR
:
10693 label
= "Nonlinear";
10695 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
10696 label
= "NonlinearVirtual";
10698 case XCB_NOTIFY_DETAIL_POINTER
:
10701 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
10702 label
= "PointerRoot";
10704 case XCB_NOTIFY_DETAIL_NONE
:
10715 get_notify_mode_label(uint8_t mode
)
10720 case XCB_NOTIFY_MODE_NORMAL
:
10723 case XCB_NOTIFY_MODE_GRAB
:
10726 case XCB_NOTIFY_MODE_UNGRAB
:
10729 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
10730 label
= "WhileGrabbed";
10740 get_state_mask_label(uint16_t state
)
10745 case XCB_KEY_BUT_MASK_SHIFT
:
10746 label
= "ShiftMask";
10748 case XCB_KEY_BUT_MASK_LOCK
:
10749 label
= "LockMask";
10751 case XCB_KEY_BUT_MASK_CONTROL
:
10752 label
= "ControlMask";
10754 case XCB_KEY_BUT_MASK_MOD_1
:
10755 label
= "Mod1Mask";
10757 case XCB_KEY_BUT_MASK_MOD_2
:
10758 label
= "Mod2Mask";
10760 case XCB_KEY_BUT_MASK_MOD_3
:
10761 label
= "Mod3Mask";
10763 case XCB_KEY_BUT_MASK_MOD_4
:
10764 label
= "Mod4Mask";
10766 case XCB_KEY_BUT_MASK_MOD_5
:
10767 label
= "Mod5Mask";
10769 case XCB_KEY_BUT_MASK_BUTTON_1
:
10770 label
= "Button1Mask";
10772 case XCB_KEY_BUT_MASK_BUTTON_2
:
10773 label
= "Button2Mask";
10775 case XCB_KEY_BUT_MASK_BUTTON_3
:
10776 label
= "Button3Mask";
10778 case XCB_KEY_BUT_MASK_BUTTON_4
:
10779 label
= "Button4Mask";
10781 case XCB_KEY_BUT_MASK_BUTTON_5
:
10782 label
= "Button5Mask";
10796 enternotify(xcb_enter_notify_event_t
*e
)
10798 struct ws_win
*win
;
10799 struct swm_region
*r
;
10801 last_event_time
= e
->time
;
10803 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): %#x "
10804 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10805 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10806 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10807 get_notify_mode_label(e
->mode
), e
->mode
,
10808 get_notify_detail_label(e
->detail
), e
->detail
,
10809 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10810 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10813 if (e
->event
== e
->root
&& e
->child
== XCB_WINDOW_NONE
&&
10814 e
->mode
== XCB_NOTIFY_MODE_GRAB
&&
10815 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10816 DNPRINTF(SWM_D_EVENT
, "enternotify: grab inferior; ignoring.\n");
10820 if (focus_mode
== SWM_FOCUS_MANUAL
&&
10821 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10822 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
10826 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10827 e
->mode
== XCB_NOTIFY_MODE_UNGRAB
&&
10828 e
->detail
!= XCB_NOTIFY_DETAIL_ANCESTOR
) {
10829 DNPRINTF(SWM_D_EVENT
, "enternotify: ungrab; ignoring.\n");
10833 if ((win
= find_window(e
->event
)) == NULL
) {
10834 if (e
->event
== e
->root
) {
10835 /* If no windows on pointer region, then focus root. */
10836 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10838 DNPRINTF(SWM_D_EVENT
, "enternotify: "
10839 "NULL region; ignoring.\n");
10845 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
10850 if (e
->mode
== XCB_NOTIFY_MODE_NORMAL
&&
10851 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10852 DNPRINTF(SWM_D_EVENT
, "enternotify: entering from "
10853 "inferior; ignoring\n");
10857 focus_win(get_focus_magic(win
));
10860 DNPRINTF(SWM_D_EVENT
, "enternotify: done\n");
10867 leavenotify(xcb_leave_notify_event_t
*e
)
10869 last_event_time
= e
->time
;
10871 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): %#x "
10872 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10873 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10874 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10875 get_notify_mode_label(e
->mode
), e
->mode
,
10876 get_notify_detail_label(e
->detail
), e
->detail
,
10877 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10878 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10884 mapnotify(xcb_map_notify_event_t
*e
)
10886 struct ws_win
*win
, *parent
= NULL
;
10887 struct workspace
*ws
;
10889 DNPRINTF(SWM_D_EVENT
, "mapnotify: win %#x\n", e
->window
);
10891 if ((win
= manage_window(e
->window
, spawn_position
, false)) == NULL
)
10895 if (win
->state
== SWM_WIN_STATE_REPARENTING
)
10898 if (ws
->r
== NULL
) {
10903 /* Need to know if win was mapped due to ws switch. */
10904 if (ws
->state
== SWM_WS_STATE_MAPPED
) {
10905 if (ws
->focus_pending
&& TRANS(ws
->focus_pending
))
10906 parent
= find_window(win
->transient
);
10908 /* If window's parent is maximized, don't clear it. */
10909 if ((parent
== NULL
) || !MAXIMIZED(parent
))
10910 if (clear_maximized(ws
) > 0)
10914 win
->mapped
= true;
10915 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
10917 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
10918 if (ws
->focus_pending
== win
) {
10920 ws
->focus_pending
= NULL
;
10921 center_pointer(ws
->r
);
10927 DNPRINTF(SWM_D_EVENT
, "mapnotify: done\n");
10933 mappingnotify(xcb_mapping_notify_event_t
*e
)
10935 if (e
->request
!= XCB_MAPPING_POINTER
) {
10936 xcb_refresh_keyboard_mapping(syms
, e
);
10944 maprequest(xcb_map_request_event_t
*e
)
10946 struct ws_win
*win
, *w
= NULL
;
10948 DNPRINTF(SWM_D_EVENT
, "maprequest: win %#x\n",
10951 win
= manage_window(e
->window
, spawn_position
, true);
10955 /* The new window should get focus; prepare. */
10956 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10957 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
) && ACCEPTS_FOCUS(win
)) {
10958 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
10959 /* See if other wins of same type are already mapped. */
10960 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
10961 if (w
== win
|| !w
->mapped
)
10964 if (w
->ch
.class_name
&&
10965 win
->ch
.class_name
&&
10966 strcmp(w
->ch
.class_name
,
10967 win
->ch
.class_name
) == 0 &&
10968 w
->ch
.instance_name
&&
10969 win
->ch
.instance_name
&&
10970 strcmp(w
->ch
.instance_name
,
10971 win
->ch
.instance_name
) == 0)
10977 win
->ws
->focus_pending
= get_focus_magic(win
);
10980 /* All windows need to be mapped if they are in the current workspace.*/
10983 /* Ignore EnterNotify to handle the mapnotify without interference. */
10984 if (focus_mode
== SWM_FOCUS_DEFAULT
)
10985 event_drain(XCB_ENTER_NOTIFY
);
10987 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
10991 motionnotify(xcb_motion_notify_event_t
*e
)
10993 struct swm_region
*r
= NULL
;
10994 int i
, num_screens
;
10996 last_event_time
= e
->time
;
10998 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): %#x "
10999 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
11000 "child: %#x, same_screen_focus: %s, state: %d\n",
11001 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
11002 get_notify_detail_label(e
->detail
), e
->detail
,
11003 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
11004 YESNO(e
->same_screen
), e
->state
);
11006 if (focus_mode
== SWM_FOCUS_MANUAL
)
11009 num_screens
= get_screen_count();
11010 for (i
= 0; i
< num_screens
; i
++)
11011 if (screens
[i
].root
== e
->root
)
11014 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11015 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
11016 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
11024 get_atom_name(xcb_atom_t atom
)
11027 #ifdef SWM_DEBUG_ATOM_NAMES
11029 * This should be disabled during most debugging since
11030 * xcb_get_* causes an xcb_flush.
11033 xcb_get_atom_name_reply_t
*r
;
11035 r
= xcb_get_atom_name_reply(conn
,
11036 xcb_get_atom_name(conn
, atom
),
11039 len
= xcb_get_atom_name_name_length(r
);
11041 name
= malloc(len
+ 1);
11043 memcpy(name
, xcb_get_atom_name_name(r
), len
);
11057 propertynotify(xcb_property_notify_event_t
*e
)
11059 struct ws_win
*win
;
11060 struct workspace
*ws
;
11064 name
= get_atom_name(e
->atom
);
11065 DNPRINTF(SWM_D_EVENT
, "propertynotify: win %#x, atom: %s(%u), "
11066 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
11070 last_event_time
= e
->time
;
11072 win
= find_window(e
->window
);
11078 if (e
->atom
== a_state
) {
11079 /* State just changed, make sure it gets focused if mapped. */
11080 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
11081 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
11083 win
->state
== SWM_WIN_STATE_REPARENTED
&&
11084 ws
->focus_pending
== win
) {
11085 focus_win(ws
->focus_pending
);
11086 ws
->focus_pending
= NULL
;
11090 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
11091 e
->atom
== XCB_ATOM_WM_NAME
) {
11093 bar_draw(ws
->r
->bar
);
11094 } else if (e
->atom
== a_prot
) {
11095 get_wm_protocols(win
);
11096 } else if (e
->atom
== XCB_ATOM_WM_NORMAL_HINTS
) {
11097 xcb_icccm_get_wm_normal_hints_reply(conn
,
11098 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
11106 reparentnotify(xcb_reparent_notify_event_t
*e
)
11108 struct ws_win
*win
;
11110 DNPRINTF(SWM_D_EVENT
, "reparentnotify: event: %#x, win %#x, "
11111 "parent: %#x, (x,y): (%u,%u), override_redirect: %u\n",
11112 e
->event
, e
->window
, e
->parent
, e
->x
, e
->y
, e
->override_redirect
);
11114 win
= find_window(e
->window
);
11116 if (win
->state
== SWM_WIN_STATE_REPARENTING
) {
11117 win
->state
= SWM_WIN_STATE_REPARENTED
;
11119 if (win
->ws
->r
&& !ICONIC(win
))
11124 update_window(win
);
11125 update_win_stacking(win
);
11126 } else if (win
->state
== SWM_WIN_STATE_UNPARENTING
) {
11127 win
->state
= SWM_WIN_STATE_UNPARENTED
;
11133 unmapnotify(xcb_unmap_notify_event_t
*e
)
11135 struct ws_win
*win
;
11136 struct workspace
*ws
;
11138 DNPRINTF(SWM_D_EVENT
, "unmapnotify: win %#x\n", e
->window
);
11140 /* If we aren't managing the window, then ignore. */
11141 win
= find_window(e
->window
);
11142 if (win
== NULL
|| win
->id
!= e
->window
) {
11143 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore unmanaged.\n");
11147 /* Do nothing if already withdrawn. */
11148 if (!win
->mapped
&& !ICONIC(win
)) {
11149 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore withdrawn.\n");
11154 win
->mapped
= false;
11156 /* Ignore if reparenting-related. */
11157 if (win
->state
!= SWM_WIN_STATE_REPARENTED
) {
11158 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore not reparented.\n");
11162 /* If win was focused, make sure to focus on something else. */
11163 if (win
== ws
->focus
) {
11164 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
11165 ws
->focus_pending
= get_focus_prev(win
);
11166 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
11167 "focus_pending: %#x\n",
11168 WINID(ws
->focus_pending
));
11176 DNPRINTF(SWM_D_EVENT
, "unmapnotify: iconify.\n");
11177 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
11180 DNPRINTF(SWM_D_EVENT
, "unmapnotify: withdraw.\n");
11181 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
11182 unmanage_window(win
);
11187 /* Update focus if ws is active. */
11188 if (WS_FOCUSED(ws
)) {
11189 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
11190 focus_win(get_pointer_win(ws
->r
->s
->root
));
11191 } else if (ws
->focus_pending
) {
11192 focus_win(ws
->focus_pending
);
11193 ws
->focus_pending
= NULL
;
11194 } else if (ws
->focus
== NULL
) {
11195 DNPRINTF(SWM_D_FOCUS
, "unmapnotify: set_input_focus: "
11196 "%#x, revert-to: parent, time: 0\n",
11198 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11199 ws
->r
->id
, XCB_CURRENT_TIME
);
11203 center_pointer(ws
->r
);
11205 DNPRINTF(SWM_D_EVENT
, "unmapnotify: done.\n");
11210 get_source_type_label(uint32_t type
)
11215 case EWMH_SOURCE_TYPE_NONE
:
11218 case EWMH_SOURCE_TYPE_NORMAL
:
11221 case EWMH_SOURCE_TYPE_OTHER
:
11233 clientmessage(xcb_client_message_event_t
*e
)
11235 struct ws_win
*win
;
11236 struct swm_region
*r
= NULL
;
11239 int num_screens
, i
;
11240 xcb_map_request_event_t mre
;
11244 name
= get_atom_name(e
->type
);
11245 DNPRINTF(SWM_D_EVENT
, "clientmessage: win %#x, atom: %s(%u)\n",
11246 e
->window
, name
, e
->type
);
11250 if (e
->type
== ewmh
[_NET_CURRENT_DESKTOP
].atom
) {
11251 num_screens
= get_screen_count();
11252 for (i
= 0; i
< num_screens
; i
++)
11253 if (screens
[i
].root
== e
->window
) {
11254 r
= screens
[i
].r_focus
;
11258 if (r
&& e
->data
.data32
[0] < (uint32_t)workspace_limit
) {
11259 a
.id
= e
->data
.data32
[0];
11260 switchws(NULL
, r
, &a
);
11265 } else if (e
->type
== ewmh
[_NET_REQUEST_FRAME_EXTENTS
].atom
) {
11266 DNPRINTF(SWM_D_EVENT
,
11267 "clientmessage: set _NET_FRAME_EXTENTS on window.\n");
11268 vals
[0] = vals
[1] = vals
[2] = vals
[3] = border_width
;
11269 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, e
->window
,
11270 a_net_frame_extents
, XCB_ATOM_CARDINAL
, 32, 4, vals
);
11275 win
= find_window(e
->window
);
11277 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11278 /* Manage the window with maprequest. */
11279 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
11280 "unmanaged window.\n");
11281 mre
.window
= e
->window
;
11287 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11288 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW, "
11289 "source_type: %s(%d)\n",
11290 get_source_type_label(e
->data
.data32
[0]),
11291 e
->data
.data32
[0]);
11294 * Allow focus changes that are a result of direct user
11295 * action and from applications that use the old EWMH spec.
11297 if (e
->data
.data32
[0] != EWMH_SOURCE_TYPE_NORMAL
||
11298 win
->quirks
& SWM_Q_OBEYAPPFOCUSREQ
) {
11299 if (WS_FOCUSED(win
->ws
))
11302 win
->ws
->focus_pending
= win
;
11304 } else if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
11305 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
11306 if (win
->can_delete
)
11307 client_msg(win
, a_delete
, 0);
11309 xcb_kill_client(conn
, win
->id
);
11310 } else if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
11311 DNPRINTF(SWM_D_EVENT
,
11312 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
11314 if (e
->data
.data32
[0] & (1<<8)) /* x */
11315 X(win
) = e
->data
.data32
[1];
11316 if (e
->data
.data32
[0] & (1<<9)) /* y */
11317 Y(win
) = e
->data
.data32
[2];
11318 if (e
->data
.data32
[0] & (1<<10)) /* width */
11319 WIDTH(win
) = e
->data
.data32
[3];
11320 if (e
->data
.data32
[0] & (1<<11)) /* height */
11321 HEIGHT(win
) = e
->data
.data32
[4];
11323 update_window(win
);
11325 /* Notify no change was made. */
11326 config_win(win
, NULL
);
11327 /* TODO: Change stack sizes */
11329 } else if (e
->type
== ewmh
[_NET_RESTACK_WINDOW
].atom
) {
11330 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_RESTACK_WINDOW\n");
11331 vals
[0] = e
->data
.data32
[1]; /* Sibling window. */
11332 vals
[1] = e
->data
.data32
[2]; /* Stack mode detail. */
11334 if (win
->frame
!= XCB_WINDOW_NONE
)
11335 xcb_configure_window(conn
, win
->frame
,
11336 XCB_CONFIG_WINDOW_SIBLING
|
11337 XCB_CONFIG_WINDOW_STACK_MODE
, vals
);
11338 } else if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
11339 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
11340 ewmh_change_wm_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
11341 if (e
->data
.data32
[2])
11342 ewmh_change_wm_state(win
, e
->data
.data32
[2],
11343 e
->data
.data32
[0]);
11345 ewmh_update_wm_state(win
);
11347 } else if (e
->type
== ewmh
[_NET_WM_DESKTOP
].atom
) {
11348 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_DESKTOP\n");
11351 win_to_ws(win
, e
->data
.data32
[0], true);
11353 /* Stack source and destination ws, if mapped. */
11354 if (r
!= win
->ws
->r
) {
11360 load_float_geom(win
);
11373 int errcode
= xcb_connection_has_error(conn
);
11374 #ifdef XCB_CONN_ERROR
11377 case XCB_CONN_ERROR
:
11378 s
= "Socket error, pipe error or other stream error.";
11380 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
11381 s
= "Extension not supported.";
11383 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
11384 s
= "Insufficient memory.";
11386 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
11387 s
= "Request length was exceeded.";
11389 case XCB_CONN_CLOSED_PARSE_ERR
:
11390 s
= "Error parsing display string.";
11393 s
= "Unknown error.";
11396 errx(errcode
, "X CONNECTION ERROR: %s", s
);
11399 errx(errcode
, "X CONNECTION ERROR");
11406 int num_screens
, i
;
11407 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
11408 XCB_EVENT_MASK_ENTER_WINDOW
|
11409 XCB_EVENT_MASK_OWNER_GRAB_BUTTON
|
11410 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
11411 XCB_EVENT_MASK_ENTER_WINDOW
|
11412 XCB_EVENT_MASK_LEAVE_WINDOW
|
11413 XCB_EVENT_MASK_BUTTON_PRESS
|
11414 XCB_EVENT_MASK_BUTTON_RELEASE
|
11415 XCB_EVENT_MASK_KEY_PRESS
|
11416 XCB_EVENT_MASK_KEY_RELEASE
|
11417 XCB_EVENT_MASK_PROPERTY_CHANGE
;
11419 xcb_void_cookie_t wac
;
11420 xcb_generic_error_t
*error
;
11422 /* this causes an error if some other window manager is running */
11423 num_screens
= get_screen_count();
11424 for (i
= 0; i
< num_screens
; i
++) {
11425 if ((sc
= get_screen(i
)) == NULL
)
11426 errx(1, "ERROR: can't get screen %d.", i
);
11427 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: %#x\n",
11429 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
11430 XCB_CW_EVENT_MASK
, &val
);
11431 if ((error
= xcb_request_check(conn
, wac
))) {
11432 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
11433 error
->error_code
);
11443 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
11445 struct swm_region
*r
= NULL
, *n
;
11446 struct workspace
*ws
= NULL
;
11450 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
11451 s
->idx
, w
, h
, x
, y
);
11453 /* remove any conflicting regions */
11454 n
= TAILQ_FIRST(&s
->rl
);
11457 n
= TAILQ_NEXT(r
, entry
);
11458 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
11459 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
11460 if (r
->ws
->r
!= NULL
)
11461 r
->ws
->old_r
= r
->ws
->r
;
11464 xcb_destroy_window(conn
, r
->id
);
11465 r
->id
= XCB_WINDOW_NONE
;
11466 TAILQ_REMOVE(&s
->rl
, r
, entry
);
11467 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
11471 /* search old regions for one to reuse */
11473 /* size + location match */
11474 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11475 if (X(r
) == x
&& Y(r
) == y
&&
11476 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11480 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11481 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11485 TAILQ_REMOVE(&s
->orl
, r
, entry
);
11486 /* try to use old region's workspace */
11487 if (r
->ws
->r
== NULL
)
11490 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
11491 err(1, "new_region: calloc: failed to allocate memory "
11494 /* if we don't have a workspace already, find one */
11496 for (i
= 0; i
< workspace_limit
; i
++)
11497 if (s
->ws
[i
].r
== NULL
) {
11504 errx(1, "new_region: no free workspaces");
11506 if (ws
->state
== SWM_WS_STATE_HIDDEN
)
11507 ws
->state
= SWM_WS_STATE_MAPPING
;
11516 r
->ws_prior
= NULL
;
11519 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
11521 /* Invisible region window to detect pointer events on empty regions. */
11522 r
->id
= xcb_generate_id(conn
);
11523 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
11524 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
11526 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
11527 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
11528 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
11530 /* Make sure region input is at the bottom. */
11531 wa
[0] = XCB_STACK_MODE_BELOW
;
11532 xcb_configure_window(conn
, r
->id
, XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
11534 xcb_map_window(conn
, r
->id
);
11538 scan_randr(int idx
)
11540 #ifdef SWM_XRR_HAS_CRTC
11543 #endif /* SWM_XRR_HAS_CRTC */
11544 struct swm_region
*r
;
11546 xcb_randr_get_screen_resources_current_cookie_t src
;
11547 xcb_randr_get_screen_resources_current_reply_t
*srr
;
11548 xcb_randr_get_crtc_info_cookie_t cic
;
11549 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
11550 xcb_randr_crtc_t
*crtc
;
11551 xcb_screen_t
*screen
;
11553 DNPRINTF(SWM_D_MISC
, "scan_randr: screen: %d\n", idx
);
11555 if ((screen
= get_screen(idx
)) == NULL
)
11556 errx(1, "ERROR: can't get screen %d.", idx
);
11558 num_screens
= get_screen_count();
11559 if (idx
>= num_screens
)
11560 errx(1, "scan_randr: invalid screen");
11562 /* remove any old regions */
11563 while ((r
= TAILQ_FIRST(&screens
[idx
].rl
)) != NULL
) {
11564 r
->ws
->old_r
= r
->ws
->r
= NULL
;
11566 xcb_destroy_window(conn
, r
->id
);
11567 r
->id
= XCB_WINDOW_NONE
;
11568 TAILQ_REMOVE(&screens
[idx
].rl
, r
, entry
);
11569 TAILQ_INSERT_TAIL(&screens
[idx
].orl
, r
, entry
);
11573 /* map virtual screens onto physical screens */
11574 #ifdef SWM_XRR_HAS_CRTC
11575 if (randr_support
) {
11576 src
= xcb_randr_get_screen_resources_current(conn
,
11577 screens
[idx
].root
);
11578 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
11581 new_region(&screens
[idx
], 0, 0,
11582 screen
->width_in_pixels
,
11583 screen
->height_in_pixels
);
11586 ncrtc
= srr
->num_crtcs
;
11588 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
11589 for (c
= 0; c
< ncrtc
; c
++) {
11590 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
11592 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
11595 if (cir
->num_outputs
== 0) {
11600 if (cir
->mode
== 0)
11601 new_region(&screens
[idx
], 0, 0,
11602 screen
->width_in_pixels
,
11603 screen
->height_in_pixels
);
11605 new_region(&screens
[idx
],
11606 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
11611 #endif /* SWM_XRR_HAS_CRTC */
11613 /* If detection failed, create a single region that spans the screen. */
11614 if (TAILQ_EMPTY(&screens
[idx
].rl
))
11615 new_region(&screens
[idx
], 0, 0, screen
->width_in_pixels
,
11616 screen
->height_in_pixels
);
11619 /* The screen shouldn't focus on unused regions. */
11620 TAILQ_FOREACH(r
, &screens
[idx
].orl
, entry
) {
11621 if (screens
[idx
].r_focus
== r
)
11622 screens
[idx
].r_focus
= NULL
;
11625 DNPRINTF(SWM_D_MISC
, "scan_randr: done.\n");
11629 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
11631 struct swm_region
*r
;
11632 struct workspace
*ws
;
11633 struct ws_win
*win
;
11634 int i
, j
, num_screens
;
11636 DNPRINTF(SWM_D_EVENT
, "screenchange: root: %#x\n", e
->root
);
11638 num_screens
= get_screen_count();
11639 /* silly event doesn't include the screen index */
11640 for (i
= 0; i
< num_screens
; i
++)
11641 if (screens
[i
].root
== e
->root
)
11643 if (i
>= num_screens
)
11644 errx(1, "screenchange: screen not found");
11646 /* brute force for now, just re-enumerate the regions */
11650 print_win_geom(e
->root
);
11652 /* add bars to all regions */
11653 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11656 /* Stack all regions. */
11657 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11660 /* Make sure a region has focus. */
11661 if (screens
[i
].r_focus
== NULL
) {
11662 r
= TAILQ_FIRST(&screens
[i
].rl
);
11667 /* Cleanup unused previously visible workspaces. */
11668 for (j
= 0; j
< workspace_limit
; j
++) {
11669 ws
= &screens
[i
].ws
[j
];
11670 if (ws
->r
== NULL
&& ws
->state
!= SWM_WS_STATE_HIDDEN
) {
11671 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
11673 ws
->state
= SWM_WS_STATE_HIDDEN
;
11679 /* Update workspace state and bar on all regions. */
11680 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11681 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
11689 struct swm_region
*r
= NULL
;
11690 xcb_query_tree_cookie_t qtc
;
11691 xcb_query_tree_reply_t
*qtr
;
11692 xcb_get_property_cookie_t pc
;
11693 xcb_get_property_reply_t
*pr
;
11694 xcb_window_t
*wins
= NULL
, trans
, *cwins
= NULL
;
11695 int i
, j
, k
, n
, no
, num_screens
;
11697 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
11698 num_screens
= get_screen_count();
11699 for (i
= 0; i
< num_screens
; i
++) {
11700 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
11701 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
11704 wins
= xcb_query_tree_children(qtr
);
11705 no
= xcb_query_tree_children_length(qtr
);
11707 /* Try to sort windows according to _NET_CLIENT_LIST. */
11708 pr
= xcb_get_property_reply(conn
, xcb_get_property(conn
, 0,
11709 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
11710 XCB_ATOM_WINDOW
, 0, UINT32_MAX
), NULL
);
11712 cwins
= xcb_get_property_value(pr
);
11713 n
= xcb_get_property_value_length(pr
) /
11714 sizeof(xcb_atom_t
);
11716 for (j
= 0; j
< n
; ++j
) {
11717 for (k
= j
; k
< no
; ++k
) {
11718 if (wins
[k
] == cwins
[j
]) {
11719 /* Swap wins j and k. */
11721 wins
[j
] = cwins
[j
];
11729 /* Manage top-level windows first, then transients. */
11730 /* TODO: allow transients to be managed before leader. */
11731 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top-level windows.\n");
11732 for (j
= 0; j
< no
; j
++) {
11733 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11734 if (r
->id
== wins
[j
]) {
11735 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11736 "skip %#x; region input window.\n",
11739 } else if (r
->bar
->id
== wins
[j
]) {
11740 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11741 "skip %#x; region bar.\n",
11750 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11751 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11753 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
11754 "is transient for %#x.\n", wins
[j
], trans
);
11758 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11761 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
11762 for (j
= 0; j
< no
; j
++) {
11763 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11764 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11766 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11770 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
11774 setup_screens(void)
11776 int i
, j
, k
, num_screens
;
11777 struct workspace
*ws
;
11778 uint32_t gcv
[1], wa
[1];
11779 const xcb_query_extension_reply_t
*qep
;
11780 xcb_screen_t
*screen
;
11781 xcb_randr_query_version_cookie_t c
;
11782 xcb_randr_query_version_reply_t
*r
;
11784 num_screens
= get_screen_count();
11785 if ((screens
= calloc(num_screens
, sizeof(struct swm_screen
))) == NULL
)
11786 err(1, "setup_screens: calloc: failed to allocate memory for "
11789 /* Initial RandR setup. */
11790 randr_support
= false;
11791 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
11792 if (qep
->present
) {
11793 c
= xcb_randr_query_version(conn
, 1, 1);
11794 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
11796 if (r
->major_version
>= 1) {
11797 randr_support
= true;
11798 randr_eventbase
= qep
->first_event
;
11804 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
11806 /* map physical screens */
11807 for (i
= 0; i
< num_screens
; i
++) {
11808 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
11809 screens
[i
].idx
= i
;
11810 screens
[i
].r_focus
= NULL
;
11812 TAILQ_INIT(&screens
[i
].rl
);
11813 TAILQ_INIT(&screens
[i
].orl
);
11814 if ((screen
= get_screen(i
)) == NULL
)
11815 errx(1, "ERROR: can't get screen %d.", i
);
11816 screens
[i
].root
= screen
->root
;
11818 /* set default colors */
11819 setscreencolor("red", i
, SWM_S_COLOR_FOCUS
);
11820 setscreencolor("rgb:88/88/88", i
, SWM_S_COLOR_UNFOCUS
);
11821 setscreencolor("rgb:00/80/80", i
, SWM_S_COLOR_BAR_BORDER
);
11822 setscreencolor("rgb:00/40/40", i
,
11823 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
11824 setscreencolor("black", i
, SWM_S_COLOR_BAR
);
11825 setscreencolor("rgb:a0/a0/a0", i
, SWM_S_COLOR_BAR_FONT
);
11826 setscreencolor("red", i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
11827 setscreencolor("rgb:88/88/88", i
,
11828 SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
11830 /* create graphics context on screen */
11831 screens
[i
].bar_gc
= xcb_generate_id(conn
);
11833 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
11834 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
11836 /* set default cursor */
11837 xcb_change_window_attributes(conn
, screens
[i
].root
,
11838 XCB_CW_CURSOR
, wa
);
11840 /* init all workspaces */
11841 /* XXX these should be dynamically allocated too */
11842 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
11843 ws
= &screens
[i
].ws
[j
];
11846 ws
->bar_enabled
= true;
11848 ws
->focus_prev
= NULL
;
11849 ws
->focus_pending
= NULL
;
11850 ws
->focus_raise
= NULL
;
11853 ws
->state
= SWM_WS_STATE_HIDDEN
;
11854 TAILQ_INIT(&ws
->stack
);
11855 TAILQ_INIT(&ws
->winlist
);
11856 TAILQ_INIT(&ws
->unmanagedlist
);
11858 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
11859 if (layouts
[k
].l_config
!= NULL
)
11860 layouts
[k
].l_config(ws
,
11861 SWM_ARG_ID_STACKINIT
);
11862 ws
->cur_layout
= &layouts
[0];
11863 ws
->cur_layout
->l_string(ws
);
11869 xcb_randr_select_input(conn
, screens
[i
].root
,
11870 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
11875 setup_globals(void)
11877 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
11878 err(1, "setup_globals: strdup: failed to allocate memory.");
11880 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
11881 err(1, "setup_globals: strdup: failed to allocate memory.");
11883 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
11884 errx(1, "unable to allocate key symbols");
11886 a_state
= get_atom_from_string("WM_STATE");
11887 a_prot
= get_atom_from_string("WM_PROTOCOLS");
11888 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
11889 a_net_frame_extents
= get_atom_from_string("_NET_FRAME_EXTENTS");
11890 a_net_supported
= get_atom_from_string("_NET_SUPPORTED");
11891 a_net_wm_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
11892 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
11893 a_utf8_string
= get_atom_from_string("UTF8_STRING");
11894 a_swm_ws
= get_atom_from_string("_SWM_WS");
11898 shutdown_cleanup(void)
11900 struct swm_region
*r
;
11902 struct workspace
*ws
;
11903 int i
, num_screens
;
11905 /* disable alarm because the following code may not be interrupted */
11907 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
11908 err(1, "can't disable alarm");
11921 num_screens
= get_screen_count();
11922 for (i
= 0; i
< num_screens
; ++i
) {
11925 DNPRINTF(SWM_D_FOCUS
, "shutdown_cleanup: set_input_focus: "
11926 "%#x, revert-to: root, time: 0\n", screens
[i
].root
);
11927 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11928 screens
[i
].root
, XCB_CURRENT_TIME
);
11930 if (screens
[i
].bar_gc
!= XCB_NONE
)
11931 xcb_free_gc(conn
, screens
[i
].bar_gc
);
11932 if (!bar_font_legacy
) {
11933 XftColorFree(display
, DefaultVisual(display
, i
),
11934 DefaultColormap(display
, i
), &bar_font_color
);
11935 XftColorFree(display
, DefaultVisual(display
, i
),
11936 DefaultColormap(display
, i
), &search_font_color
);
11939 for (j
= 0; j
< SWM_S_COLOR_MAX
; ++j
) {
11940 free(screens
[i
].c
[j
].name
);
11943 /* Free window memory. */
11944 for (j
= 0; j
< SWM_WS_MAX
; ++j
) {
11945 ws
= &screens
[i
].ws
[j
];
11948 while ((w
= TAILQ_FIRST(&ws
->winlist
)) != NULL
) {
11949 TAILQ_REMOVE(&ws
->winlist
, w
, entry
);
11953 while ((w
= TAILQ_FIRST(&ws
->unmanagedlist
)) != NULL
) {
11954 TAILQ_REMOVE(&ws
->unmanagedlist
, w
, entry
);
11959 /* Free region memory. */
11960 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
11961 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
11966 while ((r
= TAILQ_FIRST(&screens
[i
].orl
)) != NULL
) {
11967 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
11976 free(clock_format
);
11977 free(startup_exception
);
11980 XFreeFontSet(display
, bar_fs
);
11982 XftFontClose(display
, bar_font
);
11984 xcb_key_symbols_free(syms
);
11986 xcb_aux_sync(conn
);
11987 xcb_disconnect(conn
);
11991 event_error(xcb_generic_error_t
*e
)
11995 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
11996 "resource_id: %u, minor_code: %u\n",
11997 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
11998 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
11999 e
->sequence
, e
->resource_id
, e
->minor_code
);
12003 event_handle(xcb_generic_event_t
*evt
)
12005 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
12007 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
12008 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
12009 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
12012 #define EVENT(type, callback) case type: callback((void *)evt); return
12013 EVENT(0, event_error
);
12014 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
12015 EVENT(XCB_BUTTON_RELEASE
, buttonrelease
);
12016 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
12017 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
12018 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
12019 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
12020 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
12021 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
12022 /*EVENT(XCB_CREATE_NOTIFY, );*/
12023 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
12024 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
12025 EVENT(XCB_EXPOSE
, expose
);
12026 EVENT(XCB_FOCUS_IN
, focusin
);
12028 EVENT(XCB_FOCUS_OUT
, focusout
);
12030 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
12031 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
12032 EVENT(XCB_KEY_PRESS
, keypress
);
12033 EVENT(XCB_KEY_RELEASE
, keyrelease
);
12034 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
12036 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
12038 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
12039 EVENT(XCB_MAP_REQUEST
, maprequest
);
12040 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
12041 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
12042 /*EVENT(XCB_NO_EXPOSURE, );*/
12043 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
12044 EVENT(XCB_REPARENT_NOTIFY
, reparentnotify
);
12045 /*EVENT(XCB_RESIZE_REQUEST, );*/
12046 /*EVENT(XCB_SELECTION_CLEAR, );*/
12047 /*EVENT(XCB_SELECTION_NOTIFY, );*/
12048 /*EVENT(XCB_SELECTION_REQUEST, );*/
12049 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
12050 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
12053 if (type
- randr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
12054 screenchange((void *)evt
);
12058 main(int argc
, char *argv
[])
12060 struct pollfd pfd
[2];
12061 struct sigaction sact
;
12063 struct passwd
*pwd
;
12064 struct swm_region
*r
;
12065 xcb_generic_event_t
*evt
;
12066 int xfd
, i
, num_screens
, num_readable
;
12067 char conf
[PATH_MAX
], *cfile
= NULL
;
12068 bool stdin_ready
= false, startup
= true;
12070 /* suppress unused warning since var is needed */
12074 time_started
= time(NULL
);
12078 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
12079 if (setlocale(LC_CTYPE
, "") == NULL
|| setlocale(LC_TIME
, "") == NULL
)
12080 warnx("no locale support");
12082 /* handle some signals */
12083 bzero(&sact
, sizeof(sact
));
12084 sigemptyset(&sact
.sa_mask
);
12086 sact
.sa_handler
= sighdlr
;
12087 sigaction(SIGINT
, &sact
, NULL
);
12088 sigaction(SIGQUIT
, &sact
, NULL
);
12089 sigaction(SIGTERM
, &sact
, NULL
);
12090 sigaction(SIGHUP
, &sact
, NULL
);
12092 sact
.sa_handler
= sighdlr
;
12093 sact
.sa_flags
= SA_NOCLDSTOP
;
12094 sigaction(SIGCHLD
, &sact
, NULL
);
12096 if ((display
= XOpenDisplay(0)) == NULL
)
12097 errx(1, "can not open display");
12099 conn
= XGetXCBConnection(display
);
12100 if (xcb_connection_has_error(conn
))
12101 errx(1, "can not get XCB connection");
12103 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
12105 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
12106 xfd
= xcb_get_file_descriptor(conn
);
12108 /* look for local and global conf file */
12109 pwd
= getpwuid(getuid());
12111 errx(1, "invalid user: %d", getuid());
12113 xcb_grab_server(conn
);
12114 xcb_aux_sync(conn
);
12116 /* flush all events */
12117 while ((evt
= get_next_event(false))) {
12118 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
12124 errx(1, "another window manager is currently running");
12126 /* Load Xcursors and/or cursorfont glyph cursors. */
12129 xcb_aux_sync(conn
);
12134 setup_keybindings();
12135 setup_btnbindings();
12140 for (i
= 0; ; i
++) {
12145 snprintf(conf
, sizeof conf
, "%s/.%s",
12146 pwd
->pw_dir
, SWM_CONF_FILE
);
12150 snprintf(conf
, sizeof conf
, "/etc/%s",
12155 snprintf(conf
, sizeof conf
, "%s/.%s",
12156 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
12159 /* global compat */
12160 snprintf(conf
, sizeof conf
, "/etc/%s",
12161 SWM_CONF_FILE_OLD
);
12167 if (strlen(conf
) && stat(conf
, &sb
) != -1)
12168 if (S_ISREG(sb
.st_mode
)) {
12175 /* load conf (if any) */
12177 conf_load(cfile
, SWM_CONF_DEFAULT
);
12181 if (getenv("SWM_STARTED") == NULL
)
12182 setenv("SWM_STARTED", "YES", 1);
12184 /* setup all bars */
12185 num_screens
= get_screen_count();
12186 for (i
= 0; i
< num_screens
; i
++)
12187 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12190 /* Manage existing windows. */
12196 /* Stack all regions to trigger mapping. */
12197 for (i
= 0; i
< num_screens
; i
++)
12198 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12201 xcb_ungrab_server(conn
);
12204 /* Update state and bar of each newly mapped workspace. */
12205 for (i
= 0; i
< num_screens
; i
++)
12206 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
12207 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
12211 memset(&pfd
, 0, sizeof(pfd
));
12213 pfd
[0].events
= POLLIN
;
12214 pfd
[1].fd
= STDIN_FILENO
;
12215 pfd
[1].events
= POLLIN
;
12218 while ((evt
= get_next_event(false))) {
12225 /* If just (re)started, set default focus if needed. */
12229 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
12230 r
= TAILQ_FIRST(&screens
[0].rl
);
12242 num_readable
= poll(pfd
, bar_extra
? 2 : 1, 1000);
12243 if (num_readable
== -1) {
12244 DNPRINTF(SWM_D_MISC
, "poll failed: %s",
12246 } else if (num_readable
> 0 && bar_extra
&&
12247 pfd
[1].revents
& POLLIN
) {
12248 stdin_ready
= true;
12252 restart(NULL
, NULL
, NULL
);
12258 stdin_ready
= false;
12259 bar_extra_update();
12262 /* Need to ensure the bar(s) are always updated. */
12263 for (i
= 0; i
< num_screens
; i
++)
12264 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12270 shutdown_cleanup();