2 * Copyright (c) 2009-2015 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2016 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 * Copyright (c) 2012-2015 David Hill <dhill@mindcry.org>
12 * Copyright (c) 2014-2015 Yuri D'Elia <yuri.delia@eurac.edu>
14 * Permission to use, copy, modify, and distribute this software for any
15 * purpose with or without fee is hereby granted, provided that the above
16 * copyright notice and this permission notice appear in all copies.
18 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
19 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
21 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
23 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
24 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 #include <sys/types.h>
35 #include <sys/queue.h>
37 #include <sys/param.h>
38 #include <sys/select.h>
39 #if defined(__linux__)
41 #elif defined(__OpenBSD__)
43 #elif defined(__FreeBSD__)
67 #include <X11/cursorfont.h>
68 #include <X11/extensions/Xrandr.h>
69 #include <X11/Xcursor/Xcursor.h>
70 #include <X11/Xft/Xft.h>
71 #include <X11/Xlib-xcb.h>
73 #include <xcb/xcb_atom.h>
74 #include <xcb/xcb_aux.h>
75 #include <xcb/xcb_event.h>
76 #include <xcb/xcb_icccm.h>
77 #include <xcb/xcb_keysyms.h>
78 #include <xcb/xtest.h>
79 #include <xcb/randr.h>
87 #ifdef SPECTRWM_BUILDSTR
88 static const char *buildstr
= SPECTRWM_BUILDSTR
;
90 static const char *buildstr
= SPECTRWM_VERSION
;
93 #if !defined(__CYGWIN__) /* cygwin chokes on randr stuff */
95 # error RandR versions less than 1.0 are not supported
100 # define SWM_XRR_HAS_CRTC
103 #endif /* __CYGWIN__ */
105 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
106 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
107 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
108 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
109 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
110 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
111 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
112 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
113 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
114 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
115 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
116 #define xcb_icccm_get_wm_class xcb_get_wm_class
117 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
118 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
119 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
120 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
121 #define xcb_icccm_wm_hints_get_urgency xcb_wm_hints_get_urgency
122 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
123 #define xcb_icccm_get_wm_name xcb_get_wm_name
124 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
125 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
126 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
127 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
128 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
129 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
130 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
131 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
132 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
133 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
136 /*#define SWM_DEBUG*/
138 #define DPRINTF(x...) do { \
140 fprintf(stderr, x); \
142 #define DNPRINTF(n,x...) do { \
143 if (swm_debug & n) { \
144 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
145 fprintf(stderr, x); \
148 #define SWM_D_MISC 0x0001
149 #define SWM_D_EVENT 0x0002
150 #define SWM_D_WS 0x0004
151 #define SWM_D_FOCUS 0x0008
152 #define SWM_D_MOVE 0x0010
153 #define SWM_D_STACK 0x0020
154 #define SWM_D_MOUSE 0x0040
155 #define SWM_D_PROP 0x0080
156 #define SWM_D_CLASS 0x0100
157 #define SWM_D_KEY 0x0200
158 #define SWM_D_QUIRK 0x0400
159 #define SWM_D_SPAWN 0x0800
160 #define SWM_D_EVENTQ 0x1000
161 #define SWM_D_CONF 0x2000
162 #define SWM_D_BAR 0x4000
163 #define SWM_D_INIT 0x8000
165 uint32_t swm_debug
= 0
184 #define DPRINTF(x...)
185 #define DNPRINTF(n,x...)
188 #define SWM_EWMH_ACTION_COUNT_MAX (8)
189 #define EWMH_F_FULLSCREEN (0x001)
190 #define EWMH_F_ABOVE (0x002)
191 #define EWMH_F_HIDDEN (0x004)
192 #define EWMH_F_MAXIMIZED_VERT (0x008)
193 #define EWMH_F_MAXIMIZED_HORZ (0x010)
194 #define EWMH_F_SKIP_PAGER (0x020)
195 #define EWMH_F_SKIP_TASKBAR (0x040)
196 #define SWM_F_MANUAL (0x080)
198 #define EWMH_F_MAXIMIZED (EWMH_F_MAXIMIZED_VERT | EWMH_F_MAXIMIZED_HORZ)
200 /* convert 8-bit to 16-bit */
201 #define RGB_8_TO_16(col) (((col) << 8) + (col))
203 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
204 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
205 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
206 xrc.blue = RGB_8_TO_16((px) & 0xff); \
209 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
210 #define MODKEY XCB_MOD_MASK_1
211 #define ANYMOD XCB_MOD_MASK_ANY
212 #define CLEANMASK(mask) ((mask) & (XCB_KEY_BUT_MASK_SHIFT | \
213 XCB_KEY_BUT_MASK_CONTROL | XCB_KEY_BUT_MASK_MOD_1 | \
214 XCB_KEY_BUT_MASK_MOD_2 | XCB_KEY_BUT_MASK_MOD_3 | \
215 XCB_KEY_BUT_MASK_MOD_4 | XCB_KEY_BUT_MASK_MOD_5) & ~(numlockmask))
216 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
217 XCB_EVENT_MASK_BUTTON_RELEASE)
218 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
219 #define SWM_PROPLEN (16)
220 #define SWM_FUNCNAME_LEN (32)
221 #define SWM_QUIRK_LEN (64)
222 #define X(r) ((r)->g.x)
223 #define Y(r) ((r)->g.y)
224 #define WIDTH(r) ((r)->g.w)
225 #define HEIGHT(r) ((r)->g.h)
226 #define MAX_X(r) ((r)->g.x + (r)->g.w)
227 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
228 #define SH_MIN(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
229 #define SH_MIN_W(w) ((w)->sh.min_width)
230 #define SH_MIN_H(w) ((w)->sh.min_height)
231 #define SH_MAX(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
232 #define SH_MAX_W(w) ((w)->sh.max_width)
233 #define SH_MAX_H(w) ((w)->sh.max_height)
234 #define SH_INC(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)
235 #define SH_INC_W(w) ((w)->sh.width_inc)
236 #define SH_INC_H(w) ((w)->sh.height_inc)
237 #define SWM_MAX_FONT_STEPS (3)
238 #define WINID(w) ((w) ? (w)->id : XCB_WINDOW_NONE)
239 #define ACCEPTS_FOCUS(w) (!((w)->hints.flags & XCB_ICCCM_WM_HINT_INPUT) \
240 || ((w)->hints.input))
241 #define WS_FOCUSED(ws) ((ws)->r && (ws)->r->s->r_focus == (ws)->r)
242 #define YESNO(x) ((x) ? "yes" : "no")
243 #define ICONIC(w) ((w)->ewmh_flags & EWMH_F_HIDDEN)
244 #define ABOVE(w) ((w)->ewmh_flags & EWMH_F_ABOVE)
245 #define FULLSCREEN(w) ((w)->ewmh_flags & EWMH_F_FULLSCREEN)
246 #define MAXIMIZED_VERT(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_VERT)
247 #define MAXIMIZED_HORZ(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_HORZ)
248 #define MAXIMIZED(w) (MAXIMIZED_VERT(w) || MAXIMIZED_HORZ(w))
249 #define MANUAL(w) ((w)->ewmh_flags & SWM_F_MANUAL)
250 #define TRANS(w) ((w)->transient != XCB_WINDOW_NONE)
251 #define FLOATING(w) (ABOVE(w) || TRANS(w) || FULLSCREEN(w) || \
254 /* Constrain Window flags */
255 #define SWM_CW_RESIZABLE (0x01)
256 #define SWM_CW_SOFTBOUNDARY (0x02)
257 #define SWM_CW_HARDBOUNDARY (0x04)
258 #define SWM_CW_RIGHT (0x10)
259 #define SWM_CW_LEFT (0x20)
260 #define SWM_CW_BOTTOM (0x40)
261 #define SWM_CW_TOP (0x80)
262 #define SWM_CW_ALLSIDES (0xf0)
264 #define SWM_FOCUS_DEFAULT (0)
265 #define SWM_FOCUS_FOLLOW (1)
266 #define SWM_FOCUS_MANUAL (2)
268 #define SWM_CK_ALL (0xf)
269 #define SWM_CK_FOCUS (0x1)
270 #define SWM_CK_POINTER (0x2)
271 #define SWM_CK_FALLBACK (0x4)
272 #define SWM_CK_REGION (0x8)
274 #define SWM_CONF_DEFAULT (0)
275 #define SWM_CONF_KEYMAPPING (1)
278 #define SWM_LIB "/usr/local/lib/libswmhack.so"
285 xcb_atom_t a_net_frame_extents
;
286 xcb_atom_t a_net_wm_check
;
287 xcb_atom_t a_net_supported
;
288 xcb_atom_t a_takefocus
;
289 xcb_atom_t a_utf8_string
;
291 volatile sig_atomic_t running
= 1;
292 volatile sig_atomic_t restart_wm
= 0;
293 xcb_timestamp_t last_event_time
= 0;
297 unsigned int numlockmask
= 0;
300 xcb_connection_t
*conn
;
301 xcb_key_symbols_t
*syms
;
303 int boundary_width
= 50;
304 bool cycle_empty
= false;
305 bool cycle_visible
= false;
307 int font_adjusted
= 0;
308 uint16_t mod_key
= MODKEY
;
309 bool warp_focus
= false;
310 bool warp_pointer
= false;
311 bool workspace_clamp
= false;
314 struct swm_region
*search_r
;
315 int select_list_pipe
[2];
316 int select_resp_pipe
[2];
318 volatile sig_atomic_t search_resp
;
319 int search_resp_action
;
321 struct search_window
{
322 TAILQ_ENTRY(search_window
) entry
;
326 xcb_window_t indicator
;
328 TAILQ_HEAD(search_winlist
, search_window
);
329 struct search_winlist search_wl
;
334 SWM_SEARCH_UNICONIFY
,
335 SWM_SEARCH_NAME_WORKSPACE
,
336 SWM_SEARCH_SEARCH_WORKSPACE
,
337 SWM_SEARCH_SEARCH_WINDOW
340 #define SWM_STACK_TOP (0)
341 #define SWM_STACK_BOTTOM (1)
342 #define SWM_STACK_ABOVE (2)
343 #define SWM_STACK_BELOW (3)
346 double dialog_ratio
= 0.6;
348 #define SWM_BAR_MAX (256)
349 #define SWM_BAR_JUSTIFY_LEFT (0)
350 #define SWM_BAR_JUSTIFY_CENTER (1)
351 #define SWM_BAR_JUSTIFY_RIGHT (2)
352 #define SWM_BAR_OFFSET (4)
353 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
354 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
355 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
356 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
357 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
359 #ifdef X_HAVE_UTF8_STRING
360 #define DRAWSTRING(x...) Xutf8DrawString(x)
362 #define DRAWSTRING(x...) XmbDrawString(x)
365 char *bar_argv
[] = { NULL
, NULL
};
367 char bar_ext
[SWM_BAR_MAX
];
368 char bar_ext_buf
[SWM_BAR_MAX
];
369 char bar_vertext
[SWM_BAR_MAX
];
370 bool bar_version
= false;
371 bool bar_enabled
= true;
372 int bar_border_width
= 1;
373 bool bar_at_bottom
= false;
374 bool bar_extra
= false;
376 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
377 char *bar_format
= NULL
;
378 bool stack_enabled
= true;
379 bool clock_enabled
= true;
380 bool iconic_enabled
= false;
381 bool maximize_hide_bar
= false;
382 bool urgent_enabled
= false;
383 bool urgent_collapse
= false;
384 char *clock_format
= NULL
;
385 bool window_class_enabled
= false;
386 bool window_instance_enabled
= false;
387 bool window_name_enabled
= false;
388 int focus_mode
= SWM_FOCUS_DEFAULT
;
389 int focus_close
= SWM_STACK_BELOW
;
390 bool focus_close_wrap
= true;
391 int focus_default
= SWM_STACK_TOP
;
392 int spawn_position
= SWM_STACK_TOP
;
393 bool disable_border
= false;
394 int border_width
= 1;
395 int region_padding
= 0;
397 bool java_workaround
= true;
398 bool verbose_layout
= false;
404 XFontSet bar_fs
= NULL
;
405 XFontSetExtents
*bar_fs_extents
;
406 XftFont
*bar_font
= NULL
;
407 bool bar_font_legacy
= true;
408 char *bar_fonts
= NULL
;
409 XftColor bar_font_color
;
410 XftColor search_font_color
;
411 char *startup_exception
= NULL
;
412 unsigned int nr_exceptions
= 0;
414 /* layout manager data */
415 struct swm_geometry
{
428 struct swm_geometry g
;
429 struct swm_region
*r
; /* Associated region. */
432 /* virtual "screens" */
434 TAILQ_ENTRY(swm_region
) entry
;
436 struct swm_geometry g
;
437 struct workspace
*ws
; /* current workspace on this region */
438 struct workspace
*ws_prior
; /* prior workspace on this region */
439 struct swm_screen
*s
; /* screen idx */
442 TAILQ_HEAD(swm_region_list
, swm_region
);
445 SWM_WIN_STATE_REPARENTING
,
446 SWM_WIN_STATE_REPARENTED
,
447 SWM_WIN_STATE_UNPARENTING
,
448 SWM_WIN_STATE_UNPARENTED
,
452 TAILQ_ENTRY(ws_win
) entry
;
453 TAILQ_ENTRY(ws_win
) stack_entry
;
456 xcb_window_t transient
;
457 struct ws_win
*focus_child
; /* focus on child transient */
458 struct swm_geometry g
; /* current geometry */
459 struct swm_geometry g_prev
; /* prev configured geometry */
460 struct swm_geometry g_float
; /* region coordinates */
461 bool g_floatvalid
; /* g_float geometry validity */
466 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
473 struct workspace
*ws
; /* always valid */
474 struct swm_screen
*s
; /* always valid, never changes */
476 xcb_icccm_get_wm_class_reply_t ch
;
477 xcb_icccm_wm_hints_t hints
;
482 TAILQ_HEAD(ws_win_list
, ws_win
);
483 TAILQ_HEAD(ws_win_stack
, ws_win
);
487 TAILQ_ENTRY(pid_e
) entry
;
491 TAILQ_HEAD(pid_list
, pid_e
);
492 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
494 /* layout handlers */
495 void stack(struct swm_region
*);
496 void vertical_config(struct workspace
*, int);
497 void vertical_stack(struct workspace
*, struct swm_geometry
*);
498 void horizontal_config(struct workspace
*, int);
499 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
500 void max_stack(struct workspace
*, struct swm_geometry
*);
501 void plain_stacker(struct workspace
*);
502 void fancy_stacker(struct workspace
*);
505 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
506 void (*l_config
)(struct workspace
*, int);
508 #define SWM_L_FOCUSPREV (1<<0)
509 #define SWM_L_MAPONFOCUS (1<<1)
510 void (*l_string
)(struct workspace
*);
512 /* stack, configure */
513 { vertical_stack
, vertical_config
, 0, plain_stacker
},
514 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
516 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
517 { NULL
, NULL
, 0, NULL
},
520 /* position of max_stack mode in the layouts array, index into layouts! */
521 #define SWM_V_STACK (0)
522 #define SWM_H_STACK (1)
523 #define SWM_MAX_STACK (2)
525 #define SWM_H_SLICE (32)
526 #define SWM_V_SLICE (32)
528 /* define work spaces */
530 int idx
; /* workspace index */
531 char *name
; /* workspace name */
532 bool always_raise
; /* raise windows on focus */
533 bool bar_enabled
; /* bar visibility */
534 struct layout
*cur_layout
; /* current layout handlers */
535 struct ws_win
*focus
; /* may be NULL */
536 struct ws_win
*focus_prev
; /* may be NULL */
537 struct ws_win
*focus_pending
; /* may be NULL */
538 struct ws_win
*focus_raise
; /* may be NULL */
539 struct swm_region
*r
; /* may be NULL */
540 struct swm_region
*old_r
; /* may be NULL */
541 struct ws_win_list winlist
; /* list of windows in ws */
542 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
543 struct ws_win_stack stack
; /* stacking order */
544 int state
; /* mapping state */
545 char stacker
[10]; /* display stacker and layout */
549 int horizontal_msize
;
551 int horizontal_stacks
;
552 bool horizontal_flip
;
562 SWM_WS_STATE_MAPPING
,
568 SWM_S_COLOR_BAR_BORDER
,
569 SWM_S_COLOR_BAR_BORDER_UNFOCUS
,
570 SWM_S_COLOR_BAR_FONT
,
572 SWM_S_COLOR_FOCUS_MAXIMIZED
,
574 SWM_S_COLOR_UNFOCUS_MAXIMIZED
,
578 /* physical screen mapping */
579 #define SWM_WS_MAX (22) /* hard limit */
580 int workspace_limit
= 10; /* soft limit */
583 int idx
; /* screen index */
584 struct swm_region_list rl
; /* list of regions on this screen */
585 struct swm_region_list orl
; /* list of old regions */
587 struct workspace ws
[SWM_WS_MAX
];
588 struct swm_region
*r_focus
;
595 } c
[SWM_S_COLOR_MAX
];
597 xcb_gcontext_t bar_gc
;
600 struct swm_screen
*screens
;
602 /* args to functions */
605 #define SWM_ARG_ID_FOCUSNEXT (0)
606 #define SWM_ARG_ID_FOCUSPREV (1)
607 #define SWM_ARG_ID_FOCUSMAIN (2)
608 #define SWM_ARG_ID_FOCUSURGENT (3)
609 #define SWM_ARG_ID_SWAPNEXT (10)
610 #define SWM_ARG_ID_SWAPPREV (11)
611 #define SWM_ARG_ID_SWAPMAIN (12)
612 #define SWM_ARG_ID_MOVELAST (13)
613 #define SWM_ARG_ID_MASTERSHRINK (20)
614 #define SWM_ARG_ID_MASTERGROW (21)
615 #define SWM_ARG_ID_MASTERADD (22)
616 #define SWM_ARG_ID_MASTERDEL (23)
617 #define SWM_ARG_ID_FLIPLAYOUT (24)
618 #define SWM_ARG_ID_STACKRESET (30)
619 #define SWM_ARG_ID_STACKINIT (31)
620 #define SWM_ARG_ID_STACKBALANCE (32)
621 #define SWM_ARG_ID_CYCLEWS_UP (40)
622 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
623 #define SWM_ARG_ID_CYCLERG_UP (42)
624 #define SWM_ARG_ID_CYCLERG_DOWN (43)
625 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
626 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
627 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
628 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
629 #define SWM_ARG_ID_STACKINC (50)
630 #define SWM_ARG_ID_STACKDEC (51)
631 #define SWM_ARG_ID_DONTCENTER (70)
632 #define SWM_ARG_ID_CENTER (71)
633 #define SWM_ARG_ID_KILLWINDOW (80)
634 #define SWM_ARG_ID_DELETEWINDOW (81)
635 #define SWM_ARG_ID_WIDTHGROW (90)
636 #define SWM_ARG_ID_WIDTHSHRINK (91)
637 #define SWM_ARG_ID_HEIGHTGROW (92)
638 #define SWM_ARG_ID_HEIGHTSHRINK (93)
639 #define SWM_ARG_ID_MOVEUP (100)
640 #define SWM_ARG_ID_MOVEDOWN (101)
641 #define SWM_ARG_ID_MOVELEFT (102)
642 #define SWM_ARG_ID_MOVERIGHT (103)
643 #define SWM_ARG_ID_BAR_TOGGLE (110)
644 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
645 #define SWM_ARG_ID_CYCLERG_MOVE_UP (112)
646 #define SWM_ARG_ID_CYCLERG_MOVE_DOWN (113)
652 TAILQ_ENTRY(quirk
) entry
;
653 char *class; /* WM_CLASS:class */
654 char *instance
; /* WM_CLASS:instance */
655 char *name
; /* WM_NAME */
657 regex_t regex_instance
;
660 int ws
; /* Initial workspace. */
661 #define SWM_Q_FLOAT (1<<0) /* Float this window. */
662 #define SWM_Q_TRANSSZ (1<<1) /* Transient window size too small. */
663 #define SWM_Q_ANYWHERE (1<<2) /* Don't position this window */
664 #define SWM_Q_XTERM_FONTADJ (1<<3) /* Adjust xterm fonts when resizing. */
665 #define SWM_Q_FULLSCREEN (1<<4) /* Remove border when fullscreen. */
666 #define SWM_Q_FOCUSPREV (1<<5) /* Focus on caller. */
667 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
668 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
669 #define SWM_Q_OBEYAPPFOCUSREQ (1<<8) /* Focus when applications ask. */
670 #define SWM_Q_IGNOREPID (1<<9) /* Ignore PID when determining ws. */
671 #define SWM_Q_IGNORESPAWNWS (1<<10) /* Ignore _SWM_WS when managing win. */
672 #define SWM_Q_NOFOCUSCYCLE (1<<11) /* Remove from normal focus cycle. */
673 #define SWM_Q_MINIMALBORDER (1<<12) /* No border when floating/unfocused. */
675 TAILQ_HEAD(quirk_list
, quirk
);
676 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
679 * Supported EWMH hints should be added to
680 * both the enum and the ewmh array
686 _NET_CURRENT_DESKTOP
,
687 _NET_DESKTOP_GEOMETRY
,
689 _NET_DESKTOP_VIEWPORT
,
690 _NET_MOVERESIZE_WINDOW
,
691 _NET_NUMBER_OF_DESKTOPS
,
692 _NET_REQUEST_FRAME_EXTENTS
,
694 _NET_WM_ACTION_ABOVE
,
695 _NET_WM_ACTION_CLOSE
,
696 _NET_WM_ACTION_FULLSCREEN
,
698 _NET_WM_ACTION_RESIZE
,
699 _NET_WM_ALLOWED_ACTIONS
,
701 _NET_WM_FULL_PLACEMENT
,
705 _NET_WM_STATE_FULLSCREEN
,
706 _NET_WM_STATE_HIDDEN
,
707 _NET_WM_STATE_MAXIMIZED_VERT
,
708 _NET_WM_STATE_MAXIMIZED_HORZ
,
709 _NET_WM_STATE_SKIP_PAGER
,
710 _NET_WM_STATE_SKIP_TASKBAR
,
712 _NET_WM_WINDOW_TYPE_DESKTOP
,
713 _NET_WM_WINDOW_TYPE_DIALOG
,
714 _NET_WM_WINDOW_TYPE_DOCK
,
715 _NET_WM_WINDOW_TYPE_NORMAL
,
716 _NET_WM_WINDOW_TYPE_SPLASH
,
717 _NET_WM_WINDOW_TYPE_TOOLBAR
,
718 _NET_WM_WINDOW_TYPE_UTILITY
,
719 _SWM_WM_STATE_MANUAL
,
726 } ewmh
[SWM_EWMH_HINT_MAX
] = {
727 /* must be in same order as in the enum */
728 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
729 {"_NET_CLIENT_LIST", XCB_ATOM_NONE
},
730 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
731 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE
},
732 {"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE
},
733 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE
},
734 {"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE
},
735 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
736 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE
},
737 {"_NET_REQUEST_FRAME_EXTENTS", XCB_ATOM_NONE
},
738 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE
},
739 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
740 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
741 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
742 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
743 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
744 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
745 {"_NET_WM_DESKTOP", XCB_ATOM_NONE
},
746 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE
},
747 {"_NET_WM_NAME", XCB_ATOM_NONE
},
748 {"_NET_WM_STATE", XCB_ATOM_NONE
},
749 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
750 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
751 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
752 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
753 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
754 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
755 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
756 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
757 {"_NET_WM_WINDOW_TYPE_DESKTOP", XCB_ATOM_NONE
},
758 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
759 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
760 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
761 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
762 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
763 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
764 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
767 /* EWMH source type */
769 EWMH_SOURCE_TYPE_NONE
= 0,
770 EWMH_SOURCE_TYPE_NORMAL
= 1,
771 EWMH_SOURCE_TYPE_OTHER
= 2,
778 XC_BOTTOM_LEFT_CORNER
,
779 XC_BOTTOM_RIGHT_CORNER
,
787 char *name
; /* Name used by Xcursor .*/
788 uint8_t cf_char
; /* cursorfont index. */
790 } cursors
[XC_MAX
] = {
791 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
792 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
793 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
794 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
795 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
796 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
797 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
800 #define SWM_SPAWN_OPTIONAL 0x1
804 TAILQ_ENTRY(spawn_prog
) entry
;
810 TAILQ_HEAD(spawn_list
, spawn_prog
);
811 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
817 /* User callable function IDs. */
830 FN_FULLSCREEN_TOGGLE
,
944 /* SWM_DEBUG actions MUST be here: */
957 BINDING_F_REPLAY
= 0x1,
961 RB_ENTRY(binding
) entry
;
962 uint16_t mod
; /* Modifier Mask. */
963 enum binding_type type
; /* Key or Button. */
964 uint32_t value
; /* KeySym or Button Index. */
965 enum actionid action
; /* Action Identifier. */
969 RB_HEAD(binding_tree
, binding
);
971 /* function prototypes */
972 void adjust_font(struct ws_win
*);
973 char *argsep(char **);
974 void bar_cleanup(struct swm_region
*);
975 void bar_extra_setup(void);
976 void bar_extra_stop(void);
977 int bar_extra_update(void);
978 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
979 void bar_fmt_expand(char *, size_t);
980 void bar_draw(struct swm_bar
*);
981 void bar_print(struct swm_region
*, const char *);
982 void bar_print_legacy(struct swm_region
*, const char *);
983 void bar_replace(char *, char *, struct swm_region
*, size_t);
984 void bar_replace_pad(char *, int *, size_t);
985 char *bar_replace_seq(char *, char *, struct swm_region
*, size_t *, size_t);
986 void bar_setup(struct swm_region
*);
987 void bar_toggle(struct binding
*, struct swm_region
*, union arg
*);
988 void bar_urgent(char *, size_t);
989 void bar_window_class(char *, size_t, struct swm_region
*);
990 void bar_window_class_instance(char *, size_t, struct swm_region
*);
991 void bar_window_instance(char *, size_t, struct swm_region
*);
992 void bar_window_name(char *, size_t, struct swm_region
*);
993 void bar_window_state(char *, size_t, struct swm_region
*);
994 void bar_workspace_name(char *, size_t, struct swm_region
*);
995 int binding_cmp(struct binding
*, struct binding
*);
996 void binding_insert(uint16_t, enum binding_type
, uint32_t, enum actionid
,
997 uint32_t, const char *);
998 struct binding
*binding_lookup(uint16_t, enum binding_type
, uint32_t);
999 void binding_remove(struct binding
*);
1000 void buttonpress(xcb_button_press_event_t
*);
1001 void buttonrelease(xcb_button_release_event_t
*);
1002 void center_pointer(struct swm_region
*);
1003 void check_conn(void);
1004 void clear_bindings(void);
1005 void clear_keybindings(void);
1006 int clear_maximized(struct workspace
*);
1007 void clear_quirks(void);
1008 void clear_spawns(void);
1009 void clientmessage(xcb_client_message_event_t
*);
1010 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
1011 int conf_load(const char *, int);
1012 void configurenotify(xcb_configure_notify_event_t
*);
1013 void configurerequest(xcb_configure_request_event_t
*);
1014 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
1015 void constrain_window(struct ws_win
*, struct swm_geometry
*, int *);
1016 int count_win(struct workspace
*, bool);
1017 void cursors_cleanup(void);
1018 void cursors_load(void);
1019 void custom_region(const char *);
1020 void cycle_layout(struct binding
*, struct swm_region
*, union arg
*);
1021 void cyclerg(struct binding
*, struct swm_region
*, union arg
*);
1022 void cyclews(struct binding
*, struct swm_region
*, union arg
*);
1024 void debug_refresh(struct ws_win
*);
1026 void debug_toggle(struct binding
*, struct swm_region
*, union arg
*);
1027 void destroynotify(xcb_destroy_notify_event_t
*);
1028 void dumpwins(struct binding
*, struct swm_region
*, union arg
*);
1029 int enable_wm(void);
1030 void enternotify(xcb_enter_notify_event_t
*);
1031 void event_drain(uint8_t);
1032 void event_error(xcb_generic_error_t
*);
1033 void event_handle(xcb_generic_event_t
*);
1034 void ewmh_apply_flags(struct ws_win
*, uint32_t);
1035 void ewmh_autoquirk(struct ws_win
*);
1036 void ewmh_get_desktop_names(void);
1037 void ewmh_get_wm_state(struct ws_win
*);
1038 int ewmh_handle_special_types(xcb_window_t
, struct swm_region
*);
1039 void ewmh_update_actions(struct ws_win
*);
1040 void ewmh_update_client_list(void);
1041 void ewmh_update_current_desktop(void);
1042 void ewmh_update_desktop_names(void);
1043 void ewmh_update_desktops(void);
1044 void ewmh_change_wm_state(struct ws_win
*, xcb_atom_t
, long);
1045 void ewmh_update_wm_state(struct ws_win
*);
1046 char *expand_tilde(const char *);
1047 void expose(xcb_expose_event_t
*);
1048 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
1049 struct swm_bar
*find_bar(xcb_window_t
);
1050 struct ws_win
*find_frame_window(xcb_window_t
);
1051 struct pid_e
*find_pid(pid_t
);
1052 struct swm_region
*find_region(xcb_window_t
);
1053 struct ws_win
*find_unmanaged_window(xcb_window_t
);
1054 struct ws_win
*find_window(xcb_window_t
);
1055 void floating_toggle(struct binding
*, struct swm_region
*, union arg
*);
1056 void focus(struct binding
*, struct swm_region
*, union arg
*);
1057 void focus_flush(void);
1058 void focus_pointer(struct binding
*, struct swm_region
*, union arg
*);
1059 void focus_region(struct swm_region
*);
1060 void focus_win(struct ws_win
*);
1061 void focusin(xcb_focus_in_event_t
*);
1063 void focusout(xcb_focus_out_event_t
*);
1065 void focusrg(struct binding
*, struct swm_region
*, union arg
*);
1066 void fontset_init(void);
1067 void free_window(struct ws_win
*);
1068 void fullscreen_toggle(struct binding
*, struct swm_region
*, union arg
*);
1069 xcb_atom_t
get_atom_from_string(const char *);
1071 char *get_atom_name(xcb_atom_t
);
1073 struct ws_win
*get_focus_magic(struct ws_win
*);
1074 struct ws_win
*get_focus_prev(struct ws_win
*);
1075 xcb_generic_event_t
*get_next_event(bool);
1077 char *get_notify_detail_label(uint8_t);
1078 char *get_notify_mode_label(uint8_t);
1080 struct ws_win
*get_pointer_win(xcb_window_t
);
1081 struct ws_win
*get_region_focus(struct swm_region
*);
1082 int get_region_index(struct swm_region
*);
1083 xcb_screen_t
*get_screen(int);
1084 int get_screen_count(void);
1086 char *get_source_type_label(uint32_t);
1087 char *get_stack_mode_name(uint8_t);
1088 char *get_state_mask_label(uint16_t);
1090 int32_t get_swm_ws(xcb_window_t
);
1091 bool get_urgent(struct ws_win
*);
1093 char *get_win_input_model(struct ws_win
*);
1095 char *get_win_name(xcb_window_t
);
1096 uint8_t get_win_state(xcb_window_t
);
1097 void get_wm_protocols(struct ws_win
*);
1098 int get_ws_idx(struct ws_win
*);
1099 void grab_windows(void);
1100 void grabbuttons(void);
1101 void grabkeys(void);
1102 void iconify(struct binding
*, struct swm_region
*, union arg
*);
1103 bool isxlfd(char *);
1104 bool keybindreleased(struct binding
*, xcb_key_release_event_t
*);
1105 void keypress(xcb_key_press_event_t
*);
1106 void keyrelease(xcb_key_release_event_t
*);
1107 bool keyrepeating(xcb_key_release_event_t
*);
1108 void kill_bar_extra_atexit(void);
1109 void kill_refs(struct ws_win
*);
1111 void leavenotify(xcb_leave_notify_event_t
*);
1113 void load_float_geom(struct ws_win
*);
1114 void lower_window(struct ws_win
*);
1115 struct ws_win
*manage_window(xcb_window_t
, int, bool);
1116 void map_window(struct ws_win
*);
1117 void mapnotify(xcb_map_notify_event_t
*);
1118 void mappingnotify(xcb_mapping_notify_event_t
*);
1119 void maprequest(xcb_map_request_event_t
*);
1120 void maximize_toggle(struct binding
*, struct swm_region
*, union arg
*);
1121 void motionnotify(xcb_motion_notify_event_t
*);
1122 void move(struct binding
*, struct swm_region
*, union arg
*);
1123 void move_win(struct ws_win
*, struct binding
*, int);
1124 uint32_t name_to_pixel(int, const char *);
1125 void name_workspace(struct binding
*, struct swm_region
*, union arg
*);
1126 void new_region(struct swm_screen
*, int, int, int, int);
1127 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1128 int parsebinding(const char *, uint16_t *, enum binding_type
*, uint32_t *,
1130 int parsequirks(const char *, uint32_t *, int *);
1131 void pressbutton(struct binding
*, struct swm_region
*, union arg
*);
1132 void priorws(struct binding
*, struct swm_region
*, union arg
*);
1134 void print_win_geom(xcb_window_t
);
1136 void propertynotify(xcb_property_notify_event_t
*);
1137 void put_back_event(xcb_generic_event_t
*);
1138 void quirk_free(struct quirk
*);
1139 void quirk_insert(const char *, const char *, const char *, uint32_t, int);
1140 void quirk_remove(struct quirk
*);
1141 void quirk_replace(struct quirk
*, const char *, const char *, const char *,
1143 void quit(struct binding
*, struct swm_region
*, union arg
*);
1144 void raise_focus(struct binding
*, struct swm_region
*, union arg
*);
1145 void raise_toggle(struct binding
*, struct swm_region
*, union arg
*);
1146 void raise_window(struct ws_win
*);
1147 void region_containment(struct ws_win
*, struct swm_region
*, int);
1148 struct swm_region
*region_under(struct swm_screen
*, int, int);
1149 void regionize(struct ws_win
*, int, int);
1150 void reparent_window(struct ws_win
*);
1151 void reparentnotify(xcb_reparent_notify_event_t
*);
1152 void resize(struct binding
*, struct swm_region
*, union arg
*);
1153 void resize_win(struct ws_win
*, struct binding
*, int);
1154 void restart(struct binding
*, struct swm_region
*, union arg
*);
1155 struct swm_region
*root_to_region(xcb_window_t
, int);
1156 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1157 void scan_randr(int);
1158 void search_do_resp(void);
1159 void search_resp_name_workspace(const char *, size_t);
1160 void search_resp_search_window(const char *);
1161 void search_resp_search_workspace(const char *);
1162 void search_resp_uniconify(const char *, size_t);
1163 void search_win(struct binding
*, struct swm_region
*, union arg
*);
1164 void search_win_cleanup(void);
1165 void search_workspace(struct binding
*, struct swm_region
*, union arg
*);
1166 void send_to_rg(struct binding
*, struct swm_region
*, union arg
*);
1167 void send_to_rg_relative(struct binding
*, struct swm_region
*, union arg
*);
1168 void send_to_ws(struct binding
*, struct swm_region
*, union arg
*);
1169 void set_region(struct swm_region
*);
1170 int setautorun(const char *, const char *, int);
1171 void setbinding(uint16_t, enum binding_type
, uint32_t, enum actionid
,
1172 uint32_t, const char *);
1173 int setconfbinding(const char *, const char *, int);
1174 int setconfcolor(const char *, const char *, int);
1175 int setconfmodkey(const char *, const char *, int);
1176 int setconfquirk(const char *, const char *, int);
1177 int setconfregion(const char *, const char *, int);
1178 int setconfspawn(const char *, const char *, int);
1179 int setconfvalue(const char *, const char *, int);
1180 int setkeymapping(const char *, const char *, int);
1181 int setlayout(const char *, const char *, int);
1182 void setquirk(const char *, const char *, const char *, uint32_t, int);
1183 void setscreencolor(const char *, int, int);
1184 void setspawn(const char *, const char *, int);
1185 void setup_btnbindings(void);
1186 void setup_ewmh(void);
1187 void setup_globals(void);
1188 void setup_keybindings(void);
1189 void setup_quirks(void);
1190 void setup_screens(void);
1191 void setup_spawn(void);
1192 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1193 void set_win_state(struct ws_win
*, uint8_t);
1194 void shutdown_cleanup(void);
1196 void socket_setnonblock(int);
1197 void sort_windows(struct ws_win_list
*);
1198 void spawn(int, union arg
*, bool);
1199 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1200 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1201 void spawn_insert(const char *, const char *, int);
1202 struct spawn_prog
*spawn_find(const char *);
1203 void spawn_remove(struct spawn_prog
*);
1204 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1205 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1206 void stack_config(struct binding
*, struct swm_region
*, union arg
*);
1207 void stack_master(struct workspace
*, struct swm_geometry
*, int, bool);
1208 void store_float_geom(struct ws_win
*);
1209 char *strdupsafe(const char *);
1210 void swapwin(struct binding
*, struct swm_region
*, union arg
*);
1211 void switchws(struct binding
*, struct swm_region
*, union arg
*);
1212 void teardown_ewmh(void);
1213 void unescape_selector(char *);
1214 void unfocus_win(struct ws_win
*);
1215 void uniconify(struct binding
*, struct swm_region
*, union arg
*);
1216 void unmanage_window(struct ws_win
*);
1217 void unmap_all(void);
1218 void unmap_window(struct ws_win
*);
1219 void unmapnotify(xcb_unmap_notify_event_t
*);
1220 void unparent_window(struct ws_win
*);
1221 void update_floater(struct ws_win
*);
1222 void update_modkey(uint16_t);
1223 void update_win_stacking(struct ws_win
*);
1224 void update_window(struct ws_win
*);
1225 void draw_frame(struct ws_win
*);
1226 void update_wm_state(struct ws_win
*win
);
1227 void updatenumlockmask(void);
1228 void validate_spawns(void);
1229 int validate_win(struct ws_win
*);
1230 int validate_ws(struct workspace
*);
1231 void version(struct binding
*, struct swm_region
*, union arg
*);
1232 void win_to_ws(struct ws_win
*, int, bool);
1233 pid_t
window_get_pid(xcb_window_t
);
1234 void wkill(struct binding
*, struct swm_region
*, union arg
*);
1235 void update_ws_stack(struct workspace
*);
1236 void xft_init(struct swm_region
*);
1237 void _add_startup_exception(const char *, va_list);
1238 void add_startup_exception(const char *, ...);
1240 RB_PROTOTYPE(binding_tree
, binding
, entry
, binding_cmp
);
1241 RB_GENERATE(binding_tree
, binding
, entry
, binding_cmp
);
1242 struct binding_tree bindings
;
1247 xcb_font_t cf
= XCB_NONE
;
1250 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1251 /* try to load Xcursor first. */
1252 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1255 /* fallback to cursorfont. */
1256 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1257 if (cf
== XCB_NONE
) {
1258 cf
= xcb_generate_id(conn
);
1259 xcb_open_font(conn
, cf
, strlen("cursor"),
1263 cursors
[i
].cid
= xcb_generate_id(conn
);
1264 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1265 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1266 0xffff, 0xffff, 0xffff);
1272 xcb_close_font(conn
, cf
);
1276 cursors_cleanup(void)
1279 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1280 xcb_free_cursor(conn
, cursors
[i
].cid
);
1284 expand_tilde(const char *s
)
1286 struct passwd
*ppwd
;
1294 errx(1, "expand_tilde: NULL string.");
1297 result
= strdup(sc
);
1303 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1304 errx(1, "expand_tilde: sysconf");
1306 if ((user
= calloc(1, max
+ 1)) == NULL
)
1307 errx(1, "expand_tilde: calloc");
1309 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1314 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1318 result
= strdup(sc
);
1320 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1324 errx(1, "expand_tilde: failed to allocate memory.");
1330 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1332 unsigned int tmpr
, tmpg
, tmpb
;
1334 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1337 *rr
= RGB_8_TO_16(tmpr
);
1338 *gg
= RGB_8_TO_16(tmpg
);
1339 *bb
= RGB_8_TO_16(tmpb
);
1345 get_screen(int screen
)
1347 const xcb_setup_t
*r
;
1348 xcb_screen_iterator_t iter
;
1350 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1351 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1355 iter
= xcb_setup_roots_iterator(r
);
1356 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1364 get_screen_count(void)
1366 const xcb_setup_t
*r
;
1368 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1369 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1373 return xcb_setup_roots_length(r
);
1377 get_region_index(struct swm_region
*r
)
1379 struct swm_region
*rr
;
1385 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1400 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1401 event_drain(XCB_ENTER_NOTIFY
);
1407 get_atom_from_string(const char *str
)
1409 xcb_intern_atom_cookie_t c
;
1410 xcb_intern_atom_reply_t
*r
;
1413 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1414 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1422 return (XCB_ATOM_NONE
);
1426 get_wm_protocols(struct ws_win
*win
) {
1428 xcb_icccm_get_wm_protocols_reply_t wpr
;
1430 if (xcb_icccm_get_wm_protocols_reply(conn
,
1431 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
1433 for (i
= 0; i
< (int)wpr
.atoms_len
; i
++) {
1434 if (wpr
.atoms
[i
] == a_takefocus
)
1435 win
->take_focus
= true;
1436 if (wpr
.atoms
[i
] == a_delete
)
1437 win
->can_delete
= true;
1439 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
1446 xcb_window_t root
, win
;
1447 int i
, j
, num_screens
;
1449 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1450 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1452 num_screens
= get_screen_count();
1453 for (i
= 0; i
< num_screens
; i
++) {
1454 root
= screens
[i
].root
;
1456 /* Set up _NET_SUPPORTING_WM_CHECK. */
1457 win
= xcb_generate_id(conn
);
1458 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
1459 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1460 XCB_COPY_FROM_PARENT
, 0, NULL
);
1462 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
1463 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1464 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1465 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1467 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1468 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1469 8, strlen("spectrwm"), "spectrwm");
1471 /* Report supported atoms */
1472 xcb_delete_property(conn
, root
, a_net_supported
);
1473 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1474 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, root
,
1475 a_net_supported
, XCB_ATOM_ATOM
, 32, 1,
1480 ewmh_update_desktops();
1481 ewmh_get_desktop_names();
1489 xcb_get_property_cookie_t pc
;
1490 xcb_get_property_reply_t
*pr
;
1492 num_screens
= get_screen_count();
1494 for (i
= 0; i
< num_screens
; i
++) {
1495 /* Get the support check window and destroy it */
1496 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, a_net_wm_check
,
1497 XCB_ATOM_WINDOW
, 0, 1);
1498 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1501 if (pr
->format
== a_net_wm_check
) {
1502 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1504 xcb_destroy_window(conn
, id
);
1505 xcb_delete_property(conn
, screens
[i
].root
,
1507 xcb_delete_property(conn
, screens
[i
].root
,
1515 ewmh_handle_special_types(xcb_window_t id
, struct swm_region
*region
)
1517 xcb_get_property_reply_t
*r
;
1518 xcb_get_property_cookie_t c
;
1521 uint16_t configure_mask
= 0;
1524 c
= xcb_get_property(conn
, 0, id
,
1525 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1526 r
= xcb_get_property_reply(conn
, c
, NULL
);
1530 type
= xcb_get_property_value(r
);
1531 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1533 for (i
= 0; i
< n
; i
++) {
1534 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DESKTOP
].atom
) {
1535 configure_mask
= XCB_CONFIG_WINDOW_STACK_MODE
|
1536 XCB_CONFIG_WINDOW_SIBLING
;
1538 wa
[1] = XCB_STACK_MODE_ABOVE
;
1542 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
) {
1543 configure_mask
= XCB_CONFIG_WINDOW_STACK_MODE
;
1544 wa
[0] = XCB_STACK_MODE_ABOVE
;
1550 if (configure_mask
!= 0) {
1551 xcb_map_window(conn
, id
);
1552 xcb_configure_window (conn
, id
, configure_mask
, wa
);
1560 ewmh_autoquirk(struct ws_win
*win
)
1562 xcb_get_property_reply_t
*r
;
1563 xcb_get_property_cookie_t c
;
1567 c
= xcb_get_property(conn
, 0, win
->id
,
1568 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1569 r
= xcb_get_property_reply(conn
, c
, NULL
);
1573 type
= xcb_get_property_value(r
);
1574 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1576 for (i
= 0; i
< n
; i
++) {
1577 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1579 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1580 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1581 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1584 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1585 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1586 win
->quirks
= SWM_Q_FLOAT
;
1593 c
= xcb_get_property(conn
, 0, win
->id
,
1594 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1595 r
= xcb_get_property_reply(conn
, c
, NULL
);
1599 type
= xcb_get_property_value(r
);
1600 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1602 for (i
= 0; i
< n
; i
++) {
1603 if (type
[i
] == ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
||
1604 type
[i
] == ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
) {
1605 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1613 ewmh_update_actions(struct ws_win
*win
)
1615 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1621 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1624 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1625 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1626 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1629 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1630 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1633 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1634 #define _NET_WM_STATE_ADD 1 /* add/set property */
1635 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1638 ewmh_change_wm_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1645 name
= get_atom_name(state
);
1646 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: win %#x, state: %s, "
1647 "action: %ld\n", WINID(win
), name
, action
);
1653 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1654 flag
= EWMH_F_FULLSCREEN
;
1655 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1656 flag
= EWMH_F_ABOVE
;
1657 else if (state
== ewmh
[_NET_WM_STATE_HIDDEN
].atom
)
1658 flag
= EWMH_F_HIDDEN
;
1659 else if (state
== ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
||
1660 state
== ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
)
1661 flag
= EWMH_F_MAXIMIZED
;
1662 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1663 flag
= SWM_F_MANUAL
;
1664 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1665 flag
= EWMH_F_SKIP_PAGER
;
1666 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1667 flag
= EWMH_F_SKIP_TASKBAR
;
1669 /* Disallow unfloating transients. */
1670 if (TRANS(win
) && flag
== EWMH_F_ABOVE
)
1673 new_flags
= win
->ewmh_flags
;
1676 case _NET_WM_STATE_REMOVE
:
1679 case _NET_WM_STATE_ADD
:
1682 case _NET_WM_STATE_TOGGLE
:
1687 ewmh_apply_flags(win
, new_flags
);
1690 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: done.\n");
1694 ewmh_apply_flags(struct ws_win
*win
, uint32_t pending
)
1696 struct workspace
*ws
;
1699 changed
= win
->ewmh_flags
^ pending
;
1703 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: pending: %d\n", pending
);
1705 win
->ewmh_flags
= pending
;
1708 if (changed
& EWMH_F_HIDDEN
) {
1710 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1711 ws
->focus_pending
= get_focus_prev(win
);
1716 /* Reload floating geometry in case region changed. */
1718 load_float_geom(win
);
1720 /* The window is no longer iconic, prepare focus. */
1721 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1722 ws
->focus_pending
= get_focus_magic(win
);
1727 if (changed
& EWMH_F_ABOVE
) {
1728 if (ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1730 load_float_geom(win
);
1731 else if (!MAXIMIZED(win
))
1732 store_float_geom(win
);
1734 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1735 changed
&= ~EWMH_F_MAXIMIZED
;
1739 win
->ewmh_flags
^= EWMH_F_ABOVE
& pending
;
1743 if (changed
& EWMH_F_MAXIMIZED
) {
1744 /* VERT and/or HORZ changed. */
1746 if (!MAXIMIZED(win
))
1747 load_float_geom(win
);
1749 store_float_geom(win
);
1752 if (MAXIMIZED(win
)) {
1753 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
1754 ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1758 ws
->focus_pending
= win
;
1766 if (changed
& EWMH_F_FULLSCREEN
) {
1767 if (FULLSCREEN(win
)) {
1768 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
1772 ws
->focus_pending
= win
;
1775 load_float_geom(win
);
1778 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1782 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: done.\n");
1786 ewmh_update_wm_state(struct ws_win
*win
) {
1787 xcb_atom_t vals
[SWM_EWMH_ACTION_COUNT_MAX
];
1791 vals
[n
++] = ewmh
[_NET_WM_STATE_HIDDEN
].atom
;
1792 if (FULLSCREEN(win
))
1793 vals
[n
++] = ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
;
1794 if (MAXIMIZED_VERT(win
))
1795 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
;
1796 if (MAXIMIZED_HORZ(win
))
1797 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
;
1798 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1799 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
;
1800 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1801 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
;
1802 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1803 vals
[n
++] = ewmh
[_NET_WM_STATE_ABOVE
].atom
;
1804 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1805 vals
[n
++] = ewmh
[_SWM_WM_STATE_MANUAL
].atom
;
1808 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1809 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, n
, vals
);
1811 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1815 ewmh_get_wm_state(struct ws_win
*win
)
1818 xcb_get_property_cookie_t c
;
1819 xcb_get_property_reply_t
*r
;
1825 win
->ewmh_flags
= 0;
1827 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1828 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1829 r
= xcb_get_property_reply(conn
, c
, NULL
);
1833 states
= xcb_get_property_value(r
);
1834 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1836 for (i
= 0; i
< n
; i
++)
1837 ewmh_change_wm_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1845 dumpwins(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
1849 xcb_get_window_attributes_cookie_t c
;
1850 xcb_get_window_attributes_reply_t
*wa
;
1852 /* suppress unused warning since var is needed */
1856 if (r
->ws
== NULL
) {
1857 DPRINTF("dumpwins: invalid workspace\n");
1861 DPRINTF("=== managed window list ws %02d ===\n", r
->ws
->idx
);
1862 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
) {
1863 state
= get_win_state(w
->id
);
1864 c
= xcb_get_window_attributes(conn
, w
->id
);
1865 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1867 DPRINTF("win %#x (%#x), map_state: %d, state: %u, "
1868 "transient: %#x\n", w
->frame
, w
->id
, wa
->map_state
,
1869 state
, w
->transient
);
1872 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1876 DPRINTF("=== stacking order (top down) === \n");
1877 TAILQ_FOREACH(w
, &r
->ws
->stack
, stack_entry
) {
1878 DPRINTF("win %#x (%#x), fs: %s, maximized: %s, above: %s, "
1879 "iconic: %s\n", w
->frame
, w
->id
, YESNO(FULLSCREEN(w
)),
1880 YESNO(MAXIMIZED(w
)), YESNO(ABOVE(w
)), YESNO(ICONIC(w
)));
1883 DPRINTF("===== unmanaged window list =====\n");
1884 TAILQ_FOREACH(w
, &r
->ws
->unmanagedlist
, entry
) {
1885 state
= get_win_state(w
->id
);
1886 c
= xcb_get_window_attributes(conn
, w
->id
);
1887 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1889 DPRINTF("win %#x, map_state: %d, state: %u, "
1890 "transient: %#x\n", w
->id
, wa
->map_state
,
1891 state
, w
->transient
);
1894 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1898 DPRINTF("=================================\n");
1902 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
1905 int num_screens
, i
, j
;
1907 /* Suppress warnings. */
1912 DNPRINTF(SWM_D_MISC
, "debug_toggle\n");
1914 debug_enabled
= !debug_enabled
;
1916 num_screens
= get_screen_count();
1917 for (i
= 0; i
< num_screens
; i
++)
1918 for (j
= 0; j
< workspace_limit
; j
++)
1919 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1926 debug_refresh(struct ws_win
*win
)
1933 XRectangle l_ibox
, l_lbox
;
1934 xcb_rectangle_t rect
;
1936 uint32_t wc
[4], mask
, width
, height
, gcv
[1];
1939 xcb_screen_t
*screen
;
1941 if (debug_enabled
) {
1942 /* Create debug window if it doesn't exist. */
1943 if (win
->debug
== XCB_WINDOW_NONE
) {
1944 if ((screen
= get_screen(win
->s
->idx
)) == NULL
)
1945 errx(1, "ERROR: can't get screen %d.",
1948 win
->debug
= xcb_generate_id(conn
);
1949 wc
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1950 wc
[1] = win
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
1951 wc
[2] = screen
->default_colormap
;
1953 xcb_create_window(conn
, screen
->root_depth
, win
->debug
,
1954 win
->frame
, 0, 0, 10, 10, 1,
1955 XCB_WINDOW_CLASS_INPUT_OUTPUT
, screen
->root_visual
,
1956 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
|
1957 XCB_CW_COLORMAP
, wc
);
1959 xcb_map_window(conn
, win
->debug
);
1962 /* Determine workspace window list index. */
1964 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
1970 /* Determine stacking index (top down). */
1972 TAILQ_FOREACH(w
, &win
->ws
->stack
, stack_entry
) {
1978 if (asprintf(&s
, "%#x f:%#x wl:%d s:%d im:%s", win
->id
,
1979 win
->frame
, widx
, sidx
, get_win_input_model(win
)) == -1)
1984 /* Update window to an appropriate dimension. */
1985 if (bar_font_legacy
) {
1986 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
1987 width
= l_lbox
.width
+ 4;
1988 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
1990 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
1992 width
= info
.width
+ 4;
1993 height
= bar_font
->height
+ 4;
1996 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
1997 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
;
1999 wc
[0] = wc
[1] = border_width
;
2006 xcb_configure_window(conn
, win
->debug
, mask
, wc
);
2008 /* Draw a filled rectangle to 'clear' window. */
2012 rect
.height
= height
;
2014 gcv
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2015 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2016 xcb_poly_fill_rectangle(conn
, win
->debug
, win
->s
->bar_gc
, 1,
2020 if (bar_font_legacy
) {
2021 l_gcv
.graphics_exposures
= 0;
2022 l_draw
= XCreateGC(display
, win
->debug
, 0, &l_gcv
);
2024 XSetForeground(display
, l_draw
,
2025 win
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2027 DRAWSTRING(display
, win
->debug
, bar_fs
, l_draw
, 2,
2028 (bar_fs_extents
->max_logical_extent
.height
-
2029 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
2031 XFreeGC(display
, l_draw
);
2033 draw
= XftDrawCreate(display
, win
->debug
,
2034 DefaultVisual(display
, win
->s
->idx
),
2035 DefaultColormap(display
, win
->s
->idx
));
2037 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
2038 (bar_height
+ bar_font
->height
) / 2 -
2039 bar_font
->descent
, (FcChar8
*)s
, len
);
2041 XftDrawDestroy(draw
);
2045 } else if (win
->debug
!= XCB_WINDOW_NONE
) {
2046 xcb_destroy_window(conn
, win
->debug
);
2047 win
->debug
= XCB_WINDOW_NONE
;
2052 dumpwins(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
2060 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
2066 #endif /* SWM_DEBUG */
2071 int saved_errno
, status
;
2074 saved_errno
= errno
;
2078 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
2083 if (errno
!= ECHILD
)
2084 warn("sighdlr: waitpid");
2085 #endif /* SWM_DEBUG */
2088 if (pid
== searchpid
)
2092 if (WIFEXITED(status
)) {
2093 if (WEXITSTATUS(status
) != 0)
2094 warnx("sighdlr: child exit status: %d",
2095 WEXITSTATUS(status
));
2097 warnx("sighdlr: child is terminated "
2099 #endif /* SWM_DEBUG */
2113 errno
= saved_errno
;
2119 struct pid_e
*p
= NULL
;
2121 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
2126 TAILQ_FOREACH(p
, &pidlist
, entry
) {
2135 name_to_pixel(int sidx
, const char *colorname
)
2137 uint32_t result
= 0;
2138 char cname
[32] = "#";
2139 xcb_screen_t
*screen
;
2140 xcb_colormap_t cmap
;
2141 xcb_alloc_color_reply_t
*cr
;
2142 xcb_alloc_named_color_reply_t
*nr
;
2143 uint16_t rr
, gg
, bb
;
2145 screen
= get_screen(sidx
);
2146 cmap
= screen
->default_colormap
;
2148 /* color is in format rgb://rr/gg/bb */
2149 if (strncmp(colorname
, "rgb:", 4) == 0) {
2150 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
2151 warnx("could not parse rgb %s", colorname
);
2153 cr
= xcb_alloc_color_reply(conn
,
2154 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
2160 warnx("color '%s' not found", colorname
);
2163 nr
= xcb_alloc_named_color_reply(conn
,
2164 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
2167 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
2168 nr
= xcb_alloc_named_color_reply(conn
,
2169 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
2176 warnx("color '%s' not found", colorname
);
2183 setscreencolor(const char *val
, int i
, int c
)
2185 if (i
< 0 || i
>= get_screen_count())
2188 screens
[i
].c
[c
].pixel
= name_to_pixel(i
, val
);
2189 free(screens
[i
].c
[c
].name
);
2190 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
2195 fancy_stacker(struct workspace
*ws
)
2197 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2198 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2199 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2200 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
2201 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
2202 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2203 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2204 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
2205 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
2209 plain_stacker(struct workspace
*ws
)
2211 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2212 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2213 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
2214 sizeof ws
->stacker
);
2215 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2216 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
2217 sizeof ws
->stacker
);
2221 custom_region(const char *val
)
2223 unsigned int x
, y
, w
, h
;
2224 int sidx
, num_screens
;
2225 xcb_screen_t
*screen
;
2227 DNPRINTF(SWM_D_CONF
, "custom_region: %s\n", val
);
2229 num_screens
= get_screen_count();
2230 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
2231 errx(1, "invalid custom region, "
2232 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
2233 if (sidx
< 1 || sidx
> num_screens
)
2234 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
2238 if ((screen
= get_screen(sidx
)) == NULL
)
2239 errx(1, "ERROR: can't get screen %d.", sidx
);
2242 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
2244 if (x
> screen
->width_in_pixels
||
2245 y
> screen
->height_in_pixels
||
2246 w
+ x
> screen
->width_in_pixels
||
2247 h
+ y
> screen
->height_in_pixels
) {
2248 warnx("ignoring region %ux%u+%u+%u - not within screen "
2249 "boundaries (%ux%u)", w
, h
, x
, y
,
2250 screen
->width_in_pixels
, screen
->height_in_pixels
);
2254 new_region(&screens
[sidx
], x
, y
, w
, h
);
2258 socket_setnonblock(int fd
)
2262 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
2263 err(1, "fcntl F_GETFL");
2264 flags
|= O_NONBLOCK
;
2265 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
2266 err(1, "fcntl F_SETFL");
2270 bar_print_legacy(struct swm_region
*r
, const char *s
)
2272 xcb_rectangle_t rect
;
2277 XRectangle ibox
, lbox
;
2281 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
2283 switch (bar_justify
) {
2284 case SWM_BAR_JUSTIFY_LEFT
:
2287 case SWM_BAR_JUSTIFY_CENTER
:
2288 x
= (WIDTH(r
) - lbox
.width
) / 2;
2290 case SWM_BAR_JUSTIFY_RIGHT
:
2291 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
2295 if (x
< SWM_BAR_OFFSET
)
2300 rect
.width
= WIDTH(r
->bar
);
2301 rect
.height
= HEIGHT(r
->bar
);
2303 /* clear back buffer */
2304 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2305 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2306 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2308 /* draw back buffer */
2309 gcvd
.graphics_exposures
= 0;
2310 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
2311 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2312 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
2313 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
2315 XFreeGC(display
, draw
);
2318 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2319 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2323 bar_print(struct swm_region
*r
, const char *s
)
2326 xcb_rectangle_t rect
;
2334 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
2336 switch (bar_justify
) {
2337 case SWM_BAR_JUSTIFY_LEFT
:
2340 case SWM_BAR_JUSTIFY_CENTER
:
2341 x
= (WIDTH(r
) - info
.width
) / 2;
2343 case SWM_BAR_JUSTIFY_RIGHT
:
2344 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
2348 if (x
< SWM_BAR_OFFSET
)
2353 rect
.width
= WIDTH(r
->bar
);
2354 rect
.height
= HEIGHT(r
->bar
);
2356 /* clear back buffer */
2357 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2358 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2359 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2361 /* draw back buffer */
2362 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
2363 DefaultVisual(display
, r
->s
->idx
),
2364 DefaultColormap(display
, r
->s
->idx
));
2366 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
2367 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
2370 XftDrawDestroy(draw
);
2373 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2374 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2378 bar_extra_stop(void)
2382 bzero(bar_pipe
, sizeof bar_pipe
);
2385 kill(bar_pid
, SIGTERM
);
2388 strlcpy(bar_ext
, "", sizeof bar_ext
);
2393 bar_window_class(char *s
, size_t sz
, struct swm_region
*r
)
2395 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2397 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
2398 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
2402 bar_window_instance(char *s
, size_t sz
, struct swm_region
*r
)
2404 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2406 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
2407 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
2411 bar_window_class_instance(char *s
, size_t sz
, struct swm_region
*r
)
2413 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2416 bar_window_class(s
, sz
, r
);
2417 strlcat(s
, ":", sz
);
2418 bar_window_instance(s
, sz
, r
);
2422 bar_window_state(char *s
, size_t sz
, struct swm_region
*r
)
2424 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2426 if (MAXIMIZED(r
->ws
->focus
))
2427 strlcat(s
, "(m)", sz
);
2428 else if (ABOVE(r
->ws
->focus
))
2429 strlcat(s
, "(f)", sz
);
2433 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
2437 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2440 title
= get_win_name(r
->ws
->focus
->id
);
2441 strlcat(s
, title
, sz
);
2446 get_urgent(struct ws_win
*win
)
2448 xcb_icccm_wm_hints_t hints
;
2449 xcb_get_property_cookie_t c
;
2450 bool urgent
= false;
2453 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2454 if (xcb_icccm_get_wm_hints_reply(conn
, c
, &hints
, NULL
))
2455 urgent
= xcb_icccm_wm_hints_get_urgency(&hints
);
2462 bar_urgent(char *s
, size_t sz
)
2465 int i
, j
, num_screens
;
2466 bool urgent
[SWM_WS_MAX
];
2469 for (i
= 0; i
< workspace_limit
; i
++)
2472 num_screens
= get_screen_count();
2473 for (i
= 0; i
< num_screens
; i
++)
2474 for (j
= 0; j
< workspace_limit
; j
++)
2475 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2476 if (get_urgent(win
))
2479 for (i
= 0; i
< workspace_limit
; i
++) {
2481 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2483 } else if (!urgent_collapse
) {
2484 strlcat(s
, "- ", sz
);
2487 if (urgent_collapse
&& s
[0])
2488 s
[strlen(s
) - 1] = 0;
2492 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2494 if (r
== NULL
|| r
->ws
== NULL
)
2496 if (r
->ws
->name
!= NULL
)
2497 strlcat(s
, r
->ws
->name
, sz
);
2500 /* build the default bar format according to the defined enabled options */
2502 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2506 /* if format provided, just copy the buffers */
2507 if (bar_format
!= NULL
) {
2508 strlcpy(fmtnew
, fmtexp
, sz
);
2512 /* reset the output buffer */
2515 strlcat(fmtnew
, "+N:+I ", sz
);
2517 strlcat(fmtnew
, "+S", sz
);
2518 strlcat(fmtnew
, " ", sz
);
2520 /* only show the workspace name if there's actually one */
2521 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2522 strlcat(fmtnew
, "<+D>", sz
);
2524 /* If enabled, only show the iconic count if there are iconic wins. */
2525 if (iconic_enabled
&& r
!= NULL
&& r
->ws
!= NULL
)
2526 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2528 strlcat(fmtnew
, "{+M}", sz
);
2532 strlcat(fmtnew
, "+3<", sz
);
2534 if (clock_enabled
) {
2535 strlcat(fmtnew
, fmtexp
, sz
);
2536 strlcat(fmtnew
, "+4<", sz
);
2539 /* bar_urgent already adds the space before the last asterisk */
2541 strlcat(fmtnew
, (urgent_collapse
? "*+U*+4<" : "* +U*+4<"), sz
);
2543 if (window_class_enabled
) {
2544 strlcat(fmtnew
, "+C", sz
);
2545 if (!window_instance_enabled
)
2546 strlcat(fmtnew
, "+4<", sz
);
2549 /* checks needed by the colon and floating strlcat(3) calls below */
2550 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2551 if (window_instance_enabled
) {
2552 if (window_class_enabled
)
2553 strlcat(fmtnew
, ":", sz
);
2554 strlcat(fmtnew
, "+T+4<", sz
);
2556 if (window_name_enabled
) {
2557 if (ABOVE(r
->ws
->focus
) || MAXIMIZED(r
->ws
->focus
))
2558 strlcat(fmtnew
, "+F ", sz
);
2559 strlcat(fmtnew
, "+64W ", sz
);
2563 /* finally add the action script output and the version */
2564 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2568 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2570 /* special case; no limit given, pad one space, instead */
2571 if (*limit
== (int)sz
- 1)
2573 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2576 /* replaces the bar format character sequences (like in tmux(1)) */
2578 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2583 char tmp
[SWM_BAR_MAX
];
2584 int limit
, size
, count
;
2587 /* reset strlcat(3) buffer */
2590 /* get number, if any */
2593 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2594 limit
= sizeof tmp
- 1;
2595 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2596 limit
= sizeof tmp
- 1;
2598 /* there is nothing to replace (ie EOL) */
2605 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2608 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2611 bar_window_class(tmp
, sizeof tmp
, r
);
2614 bar_workspace_name(tmp
, sizeof tmp
, r
);
2617 bar_window_state(tmp
, sizeof tmp
, r
);
2620 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2624 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2628 snprintf(tmp
, sizeof tmp
, "%d", count
);
2631 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2634 bar_window_class_instance(tmp
, sizeof tmp
, r
);
2637 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2640 bar_window_instance(tmp
, sizeof tmp
, r
);
2643 bar_urgent(tmp
, sizeof tmp
);
2646 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2649 bar_window_name(tmp
, sizeof tmp
, r
);
2652 /* unknown character sequence; copy as-is */
2653 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2659 if ((int)len
< limit
)
2661 while (limit
-- > 0) {
2662 if (*offrep
>= sz
- 1)
2664 fmtrep
[(*offrep
)++] = *ptr
++;
2672 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2677 while (*fmt
!= '\0') {
2679 /* skip ordinary characters */
2682 fmtrep
[off
++] = *fmt
++;
2686 /* character sequence found; replace it */
2687 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2696 bar_fmt_expand(char *fmtexp
, size_t sz
)
2703 /* start by grabbing the current time and date */
2705 localtime_r(&tmt
, &tm
);
2707 /* figure out what to expand */
2708 if (bar_format
!= NULL
)
2710 else if (bar_format
== NULL
&& clock_enabled
)
2712 /* if nothing to expand bail out */
2718 /* copy as-is, just in case the format shouldn't be expanded below */
2719 strlcpy(fmtexp
, fmt
, sz
);
2720 /* finally pass the string through strftime(3) */
2721 #ifndef SWM_DENY_CLOCK_FORMAT
2722 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2723 warnx("format too long");
2728 /* Redraws a region bar; need to follow with xcb_flush() or focus_flush(). */
2730 bar_draw(struct swm_bar
*bar
)
2732 struct swm_region
*r
;
2733 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2734 char fmtrep
[SWM_BAR_MAX
];
2736 /* expand the format by first passing it through strftime(3) */
2737 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2744 if (bar_enabled
&& r
->ws
->bar_enabled
)
2745 xcb_map_window(conn
, bar
->id
);
2747 xcb_unmap_window(conn
, bar
->id
);
2751 if (startup_exception
)
2752 snprintf(fmtrep
, sizeof fmtrep
, "total "
2753 "exceptions: %d, first exception: %s",
2757 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2758 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2761 if (bar_font_legacy
)
2762 bar_print_legacy(r
, fmtrep
);
2764 bar_print(r
, fmtrep
);
2768 * Reads external script output; call when stdin is readable.
2769 * Returns 1 if bar_ext was updated; otherwise 0.
2772 bar_extra_update(void)
2775 char b
[SWM_BAR_MAX
];
2776 bool changed
= false;
2781 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2784 if (b
[len
- 1] == '\n') {
2785 /* Remove newline. */
2788 /* "Clear" bar_ext. */
2791 /* Flush buffered output. */
2792 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2793 bar_ext_buf
[0] = '\0';
2795 /* Append new output to bar. */
2796 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2800 /* Buffer output. */
2801 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2806 if (errno
!= EAGAIN
) {
2807 warn("bar_action failed");
2816 bar_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
2818 struct swm_region
*tmpr
;
2821 /* suppress unused warnings since vars are needed */
2826 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2829 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2830 /* Only change if master switch is enabled. */
2832 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2834 bar_enabled
= r
->ws
->bar_enabled
= true;
2836 case SWM_ARG_ID_BAR_TOGGLE
:
2837 bar_enabled
= !bar_enabled
;
2841 /* update bars as necessary */
2842 num_screens
= get_screen_count();
2843 for (i
= 0; i
< num_screens
; i
++)
2844 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2846 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2847 xcb_map_window(conn
, tmpr
->bar
->id
);
2849 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2852 /* Restack all regions and redraw bar. */
2853 num_screens
= get_screen_count();
2854 for (i
= 0; i
< num_screens
; i
++)
2855 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
2857 bar_draw(tmpr
->bar
);
2864 bar_extra_setup(void)
2866 /* do this here because the conf file is in memory */
2867 if (!bar_extra
&& bar_argv
[0]) {
2868 /* launch external status app */
2870 if (pipe(bar_pipe
) == -1)
2871 err(1, "pipe error");
2872 socket_setnonblock(bar_pipe
[0]);
2873 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2875 /* Set stdin to read from the pipe. */
2876 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2879 /* Set stdout to write to the pipe. */
2880 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2883 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2884 err(1, "could not disable SIGPIPE");
2885 switch (bar_pid
= fork()) {
2887 err(1, "cannot fork");
2891 execvp(bar_argv
[0], bar_argv
);
2892 err(1, "%s external app failed", bar_argv
[0]);
2894 default: /* parent */
2899 atexit(kill_bar_extra_atexit
);
2904 kill_bar_extra_atexit(void)
2907 kill(bar_pid
, SIGTERM
);
2915 while ((s
= index(s
, '-'))) {
2920 return (count
== 14);
2926 char *default_string
;
2927 char **missing_charsets
;
2928 int num_missing_charsets
= 0;
2932 XFreeFontSet(display
, bar_fs
);
2936 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n",
2939 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2940 &num_missing_charsets
, &default_string
);
2942 if (num_missing_charsets
> 0) {
2943 warnx("Unable to load charset(s):");
2945 for (i
= 0; i
< num_missing_charsets
; ++i
)
2946 warnx("%s", missing_charsets
[i
]);
2948 XFreeStringList(missing_charsets
);
2950 if (strcmp(default_string
, ""))
2951 warnx("Glyphs from those sets will be replaced "
2952 "by '%s'.", default_string
);
2954 warnx("Glyphs from those sets won't be drawn.");
2958 errx(1, "Error creating font set structure.");
2960 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2962 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2963 2 * bar_border_width
;
2970 xft_init(struct swm_region
*r
)
2972 char *font
, *str
, *search
;
2975 if (bar_font
== NULL
) {
2976 if ((search
= str
= strdup(bar_fonts
)) == NULL
)
2977 errx(1, "insufficient memory.");
2979 while ((font
= strsep(&search
, ",")) != NULL
) {
2983 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2986 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2989 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2993 if (bar_font
== NULL
) {
2994 warnx("unable to load font %s", font
);
2997 DNPRINTF(SWM_D_INIT
, "successfully opened "
3005 if (bar_font
== NULL
)
3006 errx(1, "unable to open a font");
3008 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
3010 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
3011 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
3012 warn("Xft error: unable to allocate color.");
3014 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR
].pixel
, color
);
3016 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
3017 DefaultColormap(display
, r
->s
->idx
), &color
, &search_font_color
))
3018 warn("Xft error: unable to allocate color.");
3020 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
3027 bar_setup(struct swm_region
*r
)
3029 xcb_screen_t
*screen
;
3032 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
3035 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
3036 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
3041 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
3042 err(1, "bar_setup: calloc: failed to allocate memory.");
3044 if (bar_font_legacy
)
3051 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
3052 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
3053 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
3055 /* Assume region is unfocused when we create the bar. */
3056 r
->bar
->id
= xcb_generate_id(conn
);
3057 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
3058 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
3059 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
3060 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
3062 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
3063 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
3064 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
3065 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
3066 | XCB_CW_EVENT_MASK
, wa
);
3068 /* Stack bar window above region window to start. */
3070 wa
[1] = XCB_STACK_MODE_ABOVE
;
3072 xcb_configure_window(conn
, r
->bar
->id
, XCB_CONFIG_WINDOW_SIBLING
|
3073 XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
3075 r
->bar
->buffer
= xcb_generate_id(conn
);
3076 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
3077 WIDTH(r
->bar
), HEIGHT(r
->bar
));
3080 xcb_randr_select_input(conn
, r
->bar
->id
,
3081 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
3084 xcb_map_window(conn
, r
->bar
->id
);
3086 DNPRINTF(SWM_D_BAR
, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
3087 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
3094 bar_cleanup(struct swm_region
*r
)
3098 xcb_destroy_window(conn
, r
->bar
->id
);
3099 xcb_free_pixmap(conn
, r
->bar
->buffer
);
3105 set_win_state(struct ws_win
*win
, uint8_t state
)
3107 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
3109 DNPRINTF(SWM_D_EVENT
, "set_win_state: win %#x, state: %u\n",
3115 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
3116 a_state
, 32, 2, data
);
3120 get_win_state(xcb_window_t w
)
3122 xcb_get_property_reply_t
*r
;
3123 xcb_get_property_cookie_t c
;
3124 uint32_t result
= 0;
3126 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
3127 r
= xcb_get_property_reply(conn
, c
, NULL
);
3129 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
3130 result
= *((uint32_t *)xcb_get_property_value(r
));
3134 DNPRINTF(SWM_D_MISC
, "get_win_state property: win %#x state %u\n", w
,
3140 version(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3142 struct swm_region
*tmpr
;
3145 /* suppress unused warnings since vars are needed */
3150 bar_version
= !bar_version
;
3152 snprintf(bar_vertext
, sizeof bar_vertext
,
3153 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
3155 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
3157 num_screens
= get_screen_count();
3158 for (i
= 0; i
< num_screens
; i
++) {
3159 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
3160 bar_draw(tmpr
->bar
);
3167 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
3169 xcb_client_message_event_t ev
;
3177 name
= get_atom_name(a
);
3178 DNPRINTF(SWM_D_EVENT
, "client_msg: win %#x, atom: %s(%u), "
3180 win
->id
, name
, a
, t
);
3184 bzero(&ev
, sizeof ev
);
3185 ev
.response_type
= XCB_CLIENT_MESSAGE
;
3186 ev
.window
= win
->id
;
3189 ev
.data
.data32
[0] = a
;
3190 ev
.data
.data32
[1] = t
;
3192 xcb_send_event(conn
, 0, win
->id
,
3193 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
3196 /* synthetic response to a ConfigureRequest when not making a change */
3198 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
3200 xcb_configure_notify_event_t ce
;
3205 /* send notification of unchanged state. */
3206 bzero(&ce
, sizeof(ce
));
3207 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
3210 ce
.width
= WIDTH(win
);
3211 ce
.height
= HEIGHT(win
);
3212 ce
.border_width
= 0;
3213 ce
.override_redirect
= 0;
3218 ce
.window
= win
->id
;
3219 ce
.above_sibling
= XCB_WINDOW_NONE
;
3222 ce
.event
= ev
->window
;
3223 ce
.window
= ev
->window
;
3225 /* make response appear more WM_SIZE_HINTS-compliant */
3226 if (win
->sh
.flags
) {
3227 DNPRINTF(SWM_D_MISC
, "config_win: hints: win %#x,"
3228 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
3229 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
3230 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
3231 SH_INC_W(win
), SH_INC_H(win
));
3236 /* the hint may be set... to 0! */
3237 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
3238 ce
.width
= SH_MIN_W(win
);
3239 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
3240 ce
.height
= SH_MIN_H(win
);
3245 /* may also be advertized as 0 */
3246 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
3247 ce
.width
= SH_MAX_W(win
);
3248 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
3249 ce
.height
= SH_MAX_H(win
);
3252 /* resize increment. */
3254 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
3255 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
3257 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
3258 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
3262 /* adjust x and y for requested border_width. */
3263 ce
.x
+= ev
->border_width
;
3264 ce
.y
+= ev
->border_width
;
3266 ce
.above_sibling
= ev
->sibling
;
3269 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
3270 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
3271 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
3273 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
3278 count_win(struct workspace
*ws
, bool count_transient
)
3283 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3284 if (!count_transient
&& FLOATING(win
))
3290 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
3296 quit(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3298 /* suppress unused warnings since vars are needed */
3303 DNPRINTF(SWM_D_MISC
, "quit\n");
3308 lower_window(struct ws_win
*win
)
3310 struct ws_win
*target
= NULL
;
3311 struct workspace
*ws
;
3313 DNPRINTF(SWM_D_EVENT
, "lower_window: win %#x\n", WINID(win
));
3320 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3321 if (target
== win
|| ICONIC(target
))
3323 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3326 if (win
->transient
== target
->transient
)
3328 if (win
->transient
== target
->id
)
3331 if (FULLSCREEN(target
))
3333 if (FULLSCREEN(win
))
3335 if (MAXIMIZED(target
))
3339 if (ABOVE(target
) || TRANS(target
))
3341 if (ABOVE(win
) || TRANS(win
))
3345 /* Change stack position. */
3346 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3348 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3350 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3352 update_win_stacking(win
);
3355 if (swm_debug
& SWM_D_STACK
) {
3356 DPRINTF("=== stacking order (top down) === \n");
3357 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3358 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3359 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3360 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3361 YESNO(ICONIC(target
)));
3365 DNPRINTF(SWM_D_EVENT
, "lower_window: done\n");
3369 raise_window(struct ws_win
*win
)
3371 struct ws_win
*target
= NULL
;
3372 struct workspace
*ws
;
3374 DNPRINTF(SWM_D_EVENT
, "raise_window: win %#x\n", WINID(win
));
3381 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3382 if (target
== win
|| ICONIC(target
))
3384 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3386 if (TRANS(win
) && (win
->transient
== target
->transient
||
3387 win
->transient
== target
->id
))
3389 if (FULLSCREEN(win
))
3391 if (FULLSCREEN(target
))
3395 if (MAXIMIZED(target
))
3397 if (ABOVE(win
) || TRANS(win
) ||
3398 (win
->ws
->focus
== win
&& ws
->always_raise
))
3400 if (!ABOVE(target
) && !TRANS(target
))
3404 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3406 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3408 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3410 update_win_stacking(win
);
3413 if (swm_debug
& SWM_D_STACK
) {
3414 DPRINTF("=== stacking order (top down) === \n");
3415 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3416 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3417 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3418 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3419 YESNO(ICONIC(target
)));
3423 DNPRINTF(SWM_D_EVENT
, "raise_window: done\n");
3427 update_win_stacking(struct ws_win
*win
)
3429 struct ws_win
*sibling
;
3433 struct swm_region
*r
;
3436 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
3439 if (win
->frame
== XCB_WINDOW_NONE
) {
3440 DNPRINTF(SWM_D_EVENT
, "update_window_stacking: win %#x not "
3441 "reparented.\n", win
->id
);
3445 sibling
= TAILQ_NEXT(win
, stack_entry
);
3446 if (sibling
!= NULL
&& (FLOATING(win
) == FLOATING(sibling
) ||
3447 (win
->ws
->always_raise
&& win
->ws
->focus
== win
))) {
3448 val
[0] = sibling
->frame
;
3449 val
[1] = XCB_STACK_MODE_ABOVE
;
3450 } else if (FLOATING(win
) || (win
->ws
->always_raise
&&
3451 win
->ws
->focus
== win
)) {
3452 val
[0] = r
->bar
->id
;
3453 val
[1] = XCB_STACK_MODE_ABOVE
;
3455 val
[0] = r
->bar
->id
;
3456 val
[1] = XCB_STACK_MODE_BELOW
;
3459 DNPRINTF(SWM_D_EVENT
, "update_win_stacking: win %#x (%#x), "
3460 "sibling %#x mode %#x\n", win
->frame
, win
->id
, val
[0], val
[1]);
3462 xcb_configure_window(conn
, win
->frame
, XCB_CONFIG_WINDOW_SIBLING
|
3463 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
3466 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
)
3472 map_window(struct ws_win
*win
)
3477 DNPRINTF(SWM_D_EVENT
, "map_window: win %#x, mapped: %s\n",
3478 win
->id
, YESNO(win
->mapped
));
3483 xcb_map_window(conn
, win
->frame
);
3484 xcb_map_window(conn
, win
->id
);
3485 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
3490 unmap_window(struct ws_win
*win
)
3495 DNPRINTF(SWM_D_EVENT
, "unmap_window: win %#x, mapped: %s\n", win
->id
,
3496 YESNO(win
->mapped
));
3501 xcb_unmap_window(conn
, win
->id
);
3502 xcb_unmap_window(conn
, win
->frame
);
3503 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
3504 win
->mapped
= false;
3511 int i
, j
, num_screens
;
3513 num_screens
= get_screen_count();
3514 for (i
= 0; i
< num_screens
; i
++)
3515 for (j
= 0; j
< workspace_limit
; j
++)
3516 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3521 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
3523 xcb_key_press_event_t event
;
3524 xcb_keycode_t
*keycode
;
3529 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
3531 DNPRINTF(SWM_D_MISC
, "fake_keypress: win %#x, keycode %u\n",
3534 bzero(&event
, sizeof(event
));
3535 event
.event
= win
->id
;
3536 event
.root
= win
->s
->root
;
3537 event
.child
= XCB_WINDOW_NONE
;
3538 event
.time
= XCB_CURRENT_TIME
;
3539 event
.event_x
= X(win
);
3540 event
.event_y
= Y(win
);
3543 event
.same_screen
= 1;
3544 event
.detail
= *keycode
;
3545 event
.state
= modifiers
;
3547 event
.response_type
= XCB_KEY_PRESS
;
3548 xcb_send_event(conn
, 1, win
->id
,
3549 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
3551 event
.response_type
= XCB_KEY_RELEASE
;
3552 xcb_send_event(conn
, 1, win
->id
,
3553 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
3559 restart(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3561 /* suppress unused warning since var is needed */
3566 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
3570 execvp(start_argv
[0], start_argv
);
3571 warn("execvp failed");
3572 quit(NULL
, NULL
, NULL
);
3576 get_pointer_win(xcb_window_t root
)
3578 struct ws_win
*win
= NULL
;
3579 xcb_query_pointer_reply_t
*r
;
3581 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: %#x.\n", root
);
3583 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
3585 win
= find_window(r
->child
);
3587 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: %#x.\n",
3590 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
3599 center_pointer(struct swm_region
*r
)
3603 if (!warp_pointer
|| r
== NULL
)
3608 DNPRINTF(SWM_D_EVENT
, "center_pointer: win %#x.\n", WINID(win
));
3610 if (win
&& win
->mapped
)
3611 xcb_warp_pointer(conn
, XCB_NONE
, win
->frame
, 0, 0, 0, 0,
3612 WIDTH(win
) / 2, HEIGHT(win
) / 2);
3614 xcb_warp_pointer(conn
, XCB_NONE
, r
->id
, 0, 0, 0, 0,
3615 WIDTH(r
) / 2, HEIGHT(r
) / 2);
3619 root_to_region(xcb_window_t root
, int check
)
3622 struct swm_region
*r
= NULL
;
3624 xcb_query_pointer_reply_t
*qpr
;
3625 xcb_get_input_focus_reply_t
*gifr
;
3627 DNPRINTF(SWM_D_MISC
, "root_to_region: win %#x\n", root
);
3629 num_screens
= get_screen_count();
3630 for (i
= 0; i
< num_screens
; i
++)
3631 if (screens
[i
].root
== root
)
3634 if (check
& SWM_CK_REGION
)
3635 r
= screens
[i
].r_focus
;
3637 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
3638 /* Try to find an actively focused window */
3639 gifr
= xcb_get_input_focus_reply(conn
,
3640 xcb_get_input_focus(conn
), NULL
);
3642 cfw
= find_window(gifr
->focus
);
3643 if (cfw
&& cfw
->ws
->r
)
3650 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
3651 /* No region with an active focus; try to use pointer. */
3652 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
3653 screens
[i
].root
), NULL
);
3655 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
3656 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
3657 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3658 if (X(r
) <= qpr
->root_x
&&
3659 qpr
->root_x
< MAX_X(r
) &&
3660 Y(r
) <= qpr
->root_y
&&
3661 qpr
->root_y
< MAX_Y(r
))
3668 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
3669 r
= TAILQ_FIRST(&screens
[i
].rl
);
3671 DNPRINTF(SWM_D_MISC
, "root_to_region: idx: %d\n", get_region_index(r
));
3677 find_region(xcb_window_t id
)
3679 struct swm_region
*r
;
3682 num_screens
= get_screen_count();
3683 for (i
= 0; i
< num_screens
; i
++)
3684 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3692 find_bar(xcb_window_t id
)
3694 struct swm_region
*r
;
3697 num_screens
= get_screen_count();
3698 for (i
= 0; i
< num_screens
; i
++)
3699 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3700 if (r
->bar
&& r
->bar
->id
== id
)
3707 find_frame_window(xcb_window_t id
) {
3708 struct swm_region
*r
;
3712 num_screens
= get_screen_count();
3713 for (i
= 0; i
< num_screens
; i
++)
3714 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3715 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
3723 find_window(xcb_window_t id
)
3725 struct ws_win
*win
= NULL
;
3726 int i
, j
, num_screens
;
3727 xcb_query_tree_reply_t
*qtr
;
3729 DNPRINTF(SWM_D_MISC
, "find_window: id: %#x\n", id
);
3731 num_screens
= get_screen_count();
3732 for (i
= 0; i
< num_screens
; i
++)
3733 for (j
= 0; j
< workspace_limit
; j
++)
3734 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3735 if (id
== win
->id
|| id
== win
->frame
)
3739 /* If window isn't top-level, try to find managed ancestor. */
3740 qtr
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3742 if (qtr
->parent
!= XCB_WINDOW_NONE
&& qtr
->parent
!= qtr
->root
)
3743 win
= find_window(qtr
->parent
);
3747 DNPRINTF(SWM_D_MISC
, "find_window: found child %#x "
3748 "of %#x.\n", win
->id
, qtr
->parent
);
3758 find_unmanaged_window(xcb_window_t id
)
3761 int i
, j
, num_screens
;
3763 num_screens
= get_screen_count();
3764 for (i
= 0; i
< num_screens
; i
++)
3765 for (j
= 0; j
< workspace_limit
; j
++)
3766 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
3774 spawn(int ws_idx
, union arg
*args
, bool close_fd
)
3779 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3781 close(xcb_get_file_descriptor(conn
));
3783 if ((ret
= getenv("LD_PRELOAD"))) {
3784 if (asprintf(&ret
, "%s:%s", SWM_LIB
, ret
) == -1) {
3785 warn("spawn: asprintf LD_PRELOAD");
3788 setenv("LD_PRELOAD", ret
, 1);
3791 setenv("LD_PRELOAD", SWM_LIB
, 1);
3794 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3795 warn("spawn: asprintf SWM_WS");
3798 setenv("_SWM_WS", ret
, 1);
3802 if (asprintf(&ret
, "%d", getpid()) == -1) {
3803 warn("spawn: asprintf _SWM_PID");
3806 setenv("_SWM_PID", ret
, 1);
3810 if (setsid() == -1) {
3811 warn("spawn: setsid");
3817 * close stdin and stdout to prevent interaction between apps
3818 * and the baraction script
3819 * leave stderr open to record errors
3821 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3822 warn("spawn: open");
3825 dup2(fd
, STDIN_FILENO
);
3826 dup2(fd
, STDOUT_FILENO
);
3831 execvp(args
->argv
[0], args
->argv
);
3833 warn("spawn: execvp");
3838 kill_refs(struct ws_win
*win
)
3840 struct workspace
*ws
;
3842 int i
, j
, num_screens
;
3847 num_screens
= get_screen_count();
3848 for (i
= 0; i
< num_screens
; i
++) {
3849 for (j
= 0; j
< workspace_limit
; j
++) {
3850 ws
= &screens
[i
].ws
[j
];
3852 if (win
== ws
->focus
)
3854 if (win
== ws
->focus_prev
)
3855 ws
->focus_prev
= NULL
;
3856 if (win
== ws
->focus_pending
)
3857 ws
->focus_pending
= NULL
;
3858 if (win
== ws
->focus_raise
)
3859 ws
->focus_raise
= NULL
;
3862 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3863 if (win
== w
->focus_child
)
3864 w
->focus_child
= NULL
;
3870 validate_win(struct ws_win
*testwin
)
3873 struct workspace
*ws
;
3874 struct swm_region
*r
;
3875 int i
, x
, num_screens
;
3877 if (testwin
== NULL
)
3880 num_screens
= get_screen_count();
3881 for (i
= 0; i
< num_screens
; i
++)
3882 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3883 for (x
= 0; x
< workspace_limit
; x
++) {
3885 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3893 validate_ws(struct workspace
*testws
)
3895 struct swm_region
*r
;
3896 struct workspace
*ws
;
3897 int i
, x
, num_screens
;
3899 /* validate all ws */
3900 num_screens
= get_screen_count();
3901 for (i
= 0; i
< num_screens
; i
++)
3902 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3903 for (x
= 0; x
< workspace_limit
; x
++) {
3912 unfocus_win(struct ws_win
*win
)
3914 xcb_window_t none
= XCB_WINDOW_NONE
;
3916 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: win %#x\n", WINID(win
));
3921 if (win
->ws
== NULL
) {
3922 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3926 if (validate_ws(win
->ws
)) {
3927 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3931 if (win
->ws
->r
== NULL
) {
3932 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3936 if (validate_win(win
)) {
3937 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3942 if (win
->ws
->focus
== win
) {
3943 win
->ws
->focus
= NULL
;
3944 win
->ws
->focus_prev
= win
;
3945 if (win
->ws
->focus_raise
== win
&& !FLOATING(win
)) {
3946 update_win_stacking(win
);
3950 if (validate_win(win
->ws
->focus
)) {
3951 kill_refs(win
->ws
->focus
);
3952 win
->ws
->focus
= NULL
;
3955 if (validate_win(win
->ws
->focus_prev
)) {
3956 kill_refs(win
->ws
->focus_prev
);
3957 win
->ws
->focus_prev
= NULL
;
3962 /* Raise window to "top unfocused position." */
3963 if (win
->ws
->always_raise
)
3966 /* Update border width */
3967 if (win
->bordered
&& (win
->quirks
& SWM_Q_MINIMALBORDER
) &&
3970 X(win
) += border_width
;
3971 Y(win
) += border_width
;
3975 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3976 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3978 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3982 focus_win(struct ws_win
*win
)
3984 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
, *tmpw
;
3985 struct workspace
*ws
;
3986 xcb_get_input_focus_reply_t
*gifr
= NULL
;
3987 xcb_get_window_attributes_reply_t
*war
= NULL
;
3989 DNPRINTF(SWM_D_FOCUS
, "focus_win: win %#x\n", WINID(win
));
3991 if (win
== NULL
|| win
->ws
== NULL
|| !win
->mapped
)
3996 if (validate_ws(ws
))
3999 if (validate_win(win
)) {
4004 gifr
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
4006 DNPRINTF(SWM_D_FOCUS
, "focus_win: cur focus: %#x\n",
4009 cfw
= find_window(gifr
->focus
);
4012 if (cfw
->ws
!= ws
&& cfw
->ws
->r
!= NULL
&&
4013 cfw
->frame
!= XCB_WINDOW_NONE
) {
4020 war
= xcb_get_window_attributes_reply(conn
,
4021 xcb_get_window_attributes(conn
, gifr
->focus
), NULL
);
4022 if (war
&& war
->override_redirect
&& ws
->focus
== win
) {
4023 DNPRINTF(SWM_D_FOCUS
, "focus_win: skip refocus "
4024 "from override_redirect.\n");
4030 if (ws
->focus
!= win
) {
4031 if (ws
->focus
&& ws
->focus
!= cfw
)
4032 unfocus_win(ws
->focus
);
4036 /* Clear focus child redirect. */
4037 win
->focus_child
= NULL
;
4039 /* If transient, adjust parent's focus child for focus_magic. */
4041 parent
= find_window(win
->transient
);
4042 if (parent
&& parent
->focus_child
!= win
)
4043 parent
->focus_child
= win
;
4046 /* Update window border even if workspace is hidden. */
4050 DNPRINTF(SWM_D_FOCUS
, "focus_win: already focused.\n");
4055 /* Set input focus if no input hint, or indicated by hint. */
4056 if (ACCEPTS_FOCUS(win
)) {
4057 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
4058 " revert-to: parent, time: %#x\n", win
->id
,
4060 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
4061 win
->id
, last_event_time
);
4062 } else if (!win
->take_focus
) {
4063 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
4064 " revert-to: parent, time: 0\n", ws
->r
->id
);
4065 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
4066 ws
->r
->id
, XCB_CURRENT_TIME
);
4069 /* Tell app it can adjust focus to a specific window. */
4070 if (win
->take_focus
) {
4071 /* java is special; always tell parent */
4072 if (TRANS(win
) && win
->java
)
4073 client_msg(parent
, a_takefocus
,
4076 client_msg(win
, a_takefocus
, last_event_time
);
4079 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
4081 /* If a parent exists, map it first. */
4083 raise_window(parent
);
4086 /* Map siblings next. */
4087 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
,
4089 if (w
!= win
&& !ICONIC(w
) &&
4090 w
->transient
== parent
->id
) {
4096 /* Map focused window. */
4100 /* Stack any children of focus window. */
4101 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
4102 if (w
->transient
== win
->id
&& !ICONIC(w
)) {
4106 } else if (tile_gap
< 0 && !ABOVE(win
)) {
4108 * Windows overlap in the layout.
4109 * Raise focused win above all tiled wins.
4117 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4118 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4121 bar_draw(ws
->r
->bar
);
4127 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
4130 /* If a child window should have focus instead, return it. */
4132 get_focus_magic(struct ws_win
*win
)
4134 struct ws_win
*parent
= NULL
;
4135 struct ws_win
*child
= NULL
;
4137 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: win %#x\n", WINID(win
));
4142 parent
= find_window(win
->transient
);
4144 /* If parent prefers focus elsewhere, then try to do so. */
4145 if (parent
&& (child
= parent
->focus_child
)) {
4146 if (validate_win(child
) == 0 && child
->mapped
)
4149 parent
->focus_child
= NULL
;
4153 /* If this window prefers focus elsewhere, then try to do so. */
4154 if ((child
= win
->focus_child
)) {
4155 if (validate_win(child
) == 0 && child
->mapped
)
4158 win
->focus_child
= NULL
;
4165 event_drain(uint8_t rt
)
4167 xcb_generic_event_t
*evt
;
4169 /* ensure all pending requests have been processed before filtering. */
4171 while ((evt
= get_next_event(false))) {
4172 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
4180 set_region(struct swm_region
*r
)
4182 struct swm_region
*rf
;
4189 /* Unfocus old region bar. */
4194 xcb_change_window_attributes(conn
, rf
->bar
->id
,
4195 XCB_CW_BORDER_PIXEL
,
4196 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
4199 if (rf
!= NULL
&& rf
!= r
&& (X(rf
) != X(r
) || Y(rf
) != Y(r
) ||
4200 WIDTH(rf
) != WIDTH(r
) || HEIGHT(rf
) != HEIGHT(r
))) {
4201 /* Set _NET_DESKTOP_GEOMETRY. */
4203 vals
[1] = HEIGHT(r
);
4204 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, r
->s
->root
,
4205 ewmh
[_NET_DESKTOP_GEOMETRY
].atom
, XCB_ATOM_CARDINAL
, 32, 2,
4209 /* Set region bar border to focus_color. */
4210 xcb_change_window_attributes(conn
, r
->bar
->id
,
4211 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
4215 /* Update the focus window frame on the now unfocused region. */
4216 if (rf
&& rf
->ws
->focus
)
4217 draw_frame(rf
->ws
->focus
);
4219 ewmh_update_current_desktop();
4223 focus_region(struct swm_region
*r
)
4226 struct swm_region
*old_r
;
4231 old_r
= r
->s
->r_focus
;
4234 nfw
= get_region_focus(r
);
4238 /* New region is empty; need to manually unfocus win. */
4240 unfocus_win(old_r
->ws
->focus
);
4241 /* Clear bar since empty. */
4242 bar_draw(old_r
->bar
);
4245 DNPRINTF(SWM_D_FOCUS
, "focus_region: set_input_focus: %#x, "
4246 "revert-to: parent, time: 0\n", r
->id
);
4247 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4254 switchws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4256 struct swm_region
*this_r
, *other_r
;
4258 struct workspace
*new_ws
, *old_ws
;
4259 xcb_window_t none
= XCB_WINDOW_NONE
;
4260 int wsid
= args
->id
;
4261 bool unmap_old
= false;
4266 if (wsid
>= workspace_limit
)
4270 old_ws
= this_r
->ws
;
4271 new_ws
= &this_r
->s
->ws
[wsid
];
4273 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
4274 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
4276 if (new_ws
== NULL
|| old_ws
== NULL
)
4278 if (new_ws
== old_ws
)
4281 other_r
= new_ws
->r
;
4282 if (other_r
&& workspace_clamp
&&
4283 bp
->action
!= FN_RG_MOVE_NEXT
&& bp
->action
!= FN_RG_MOVE_PREV
) {
4284 DNPRINTF(SWM_D_WS
, "switchws: ws clamped.\n");
4287 DNPRINTF(SWM_D_WS
, "switchws: warping focus to region "
4288 "with ws %d.\n", wsid
);
4289 focus_region(other_r
);
4290 center_pointer(other_r
);
4296 if ((win
= old_ws
->focus
) != NULL
) {
4299 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4300 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4305 /* the other ws is visible in another region, exchange them */
4306 other_r
->ws_prior
= new_ws
;
4307 other_r
->ws
= old_ws
;
4308 old_ws
->r
= other_r
;
4310 /* the other workspace is hidden, hide this one */
4315 this_r
->ws_prior
= old_ws
;
4316 this_r
->ws
= new_ws
;
4319 /* Set focus_pending before stacking, if needed. */
4320 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
4321 validate_win(new_ws
->focus_pending
))) {
4322 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
4323 new_ws
->focus
= new_ws
->focus_prev
;
4324 new_ws
->focus_prev
= NULL
;
4327 new_ws
->state
= SWM_WS_STATE_MAPPING
;
4332 /* unmap old windows */
4334 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
4336 old_ws
->state
= SWM_WS_STATE_HIDDEN
;
4339 /* if workspaces were swapped, then don't wait to set focus */
4340 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
4341 if (new_ws
->focus_pending
) {
4342 focus_win(new_ws
->focus_pending
);
4343 new_ws
->focus_pending
= NULL
;
4347 /* Clear bar and set focus on region input win if new ws is empty. */
4348 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
4349 DNPRINTF(SWM_D_FOCUS
, "switchws: set_input_focus: %#x, "
4350 "revert-to: parent, time: 0\n", r
->id
);
4351 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4356 ewmh_update_current_desktop();
4360 new_ws
->state
= SWM_WS_STATE_MAPPED
;
4362 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
4366 cyclews(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4369 struct swm_screen
*s
= r
->s
;
4370 bool cycle_all
= false, mv
= false;
4372 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4373 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4379 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
4382 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
4385 case SWM_ARG_ID_CYCLEWS_UP
:
4386 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
4388 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
4391 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
4394 case SWM_ARG_ID_CYCLEWS_DOWN
:
4395 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
4402 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
4404 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
4408 send_to_ws(bp
, r
, &a
);
4410 switchws(bp
, r
, &a
);
4411 } while (a
.id
!= r
->ws
->idx
);
4413 DNPRINTF(SWM_D_FOCUS
, "cyclews: done\n");
4417 priorws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4423 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4424 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4426 if (r
->ws_prior
== NULL
)
4429 a
.id
= r
->ws_prior
->idx
;
4430 switchws(bp
, r
, &a
);
4431 DNPRINTF(SWM_D_FOCUS
, "priorws: done\n");
4435 focusrg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4437 int ridx
= args
->id
, i
, num_screens
;
4438 struct swm_region
*rr
= NULL
;
4442 num_screens
= get_screen_count();
4443 /* do nothing if we don't have more than one screen */
4444 if (!(num_screens
> 1 || outputs
> 1))
4447 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
4449 rr
= TAILQ_FIRST(&r
->s
->rl
);
4450 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
4451 rr
= TAILQ_NEXT(rr
, entry
);
4459 DNPRINTF(SWM_D_FOCUS
, "focusrg: done\n");
4463 cyclerg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4466 struct swm_region
*rr
= NULL
;
4469 num_screens
= get_screen_count();
4470 /* do nothing if we don't have more than one screen */
4471 if (!(num_screens
> 1 || outputs
> 1))
4475 DNPRINTF(SWM_D_FOCUS
, "cyclerg: id: %d, region: %d\n", args
->id
, i
);
4478 case SWM_ARG_ID_CYCLERG_UP
:
4479 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4480 rr
= TAILQ_NEXT(r
, entry
);
4482 rr
= TAILQ_FIRST(&screens
[i
].rl
);
4484 case SWM_ARG_ID_CYCLERG_DOWN
:
4485 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4486 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
4488 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
4497 case SWM_ARG_ID_CYCLERG_UP
:
4498 case SWM_ARG_ID_CYCLERG_DOWN
:
4503 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4504 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4506 switchws(bp
, r
, &a
);
4512 DNPRINTF(SWM_D_FOCUS
, "cyclerg: done\n");
4515 /* Sorts transients after parent. */
4517 sort_windows(struct ws_win_list
*wl
)
4519 struct ws_win
*win
, *parent
, *nxt
;
4524 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
4525 nxt
= TAILQ_NEXT(win
, entry
);
4527 parent
= find_window(win
->transient
);
4528 if (parent
== NULL
) {
4529 warnx("not possible bug");
4532 TAILQ_REMOVE(wl
, win
, entry
);
4533 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
4539 swapwin(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4541 struct ws_win
*target
, *source
;
4542 struct ws_win
*cur_focus
;
4543 struct ws_win_list
*wl
;
4547 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4548 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4550 cur_focus
= r
->ws
->focus
;
4551 if (cur_focus
== NULL
|| FULLSCREEN(cur_focus
))
4554 /* Adjust stacking in floating layer. */
4555 if (ABOVE(cur_focus
)) {
4557 case SWM_ARG_ID_SWAPPREV
:
4558 target
= TAILQ_PREV(cur_focus
, ws_win_stack
,
4560 if (target
!= NULL
&& FLOATING(target
)) {
4561 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4563 TAILQ_INSERT_BEFORE(target
, cur_focus
,
4565 update_win_stacking(cur_focus
);
4569 case SWM_ARG_ID_SWAPNEXT
:
4570 target
= TAILQ_NEXT(cur_focus
, stack_entry
);
4571 if (target
!= NULL
&& FLOATING(target
)) {
4572 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4574 TAILQ_INSERT_AFTER(&cur_focus
->ws
->stack
,
4575 target
, cur_focus
, stack_entry
);
4576 update_win_stacking(cur_focus
);
4584 if (r
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4587 clear_maximized(r
->ws
);
4590 wl
= &source
->ws
->winlist
;
4593 case SWM_ARG_ID_SWAPPREV
:
4595 source
= find_window(source
->transient
);
4596 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
4597 if (target
&& target
->transient
)
4598 target
= find_window(target
->transient
);
4599 TAILQ_REMOVE(wl
, source
, entry
);
4601 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4603 TAILQ_INSERT_BEFORE(target
, source
, entry
);
4605 case SWM_ARG_ID_SWAPNEXT
:
4606 target
= TAILQ_NEXT(source
, entry
);
4607 /* move the parent and let the sort handle the move */
4609 source
= find_window(source
->transient
);
4610 TAILQ_REMOVE(wl
, source
, entry
);
4612 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4614 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
4616 case SWM_ARG_ID_SWAPMAIN
:
4617 target
= TAILQ_FIRST(wl
);
4618 if (target
== source
) {
4619 if (source
->ws
->focus_prev
!= NULL
&&
4620 source
->ws
->focus_prev
!= target
)
4621 source
= source
->ws
->focus_prev
;
4625 if (target
== NULL
|| source
== NULL
)
4627 source
->ws
->focus_prev
= target
;
4628 TAILQ_REMOVE(wl
, target
, entry
);
4629 TAILQ_INSERT_BEFORE(source
, target
, entry
);
4630 TAILQ_REMOVE(wl
, source
, entry
);
4631 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4633 case SWM_ARG_ID_MOVELAST
:
4634 TAILQ_REMOVE(wl
, source
, entry
);
4635 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4638 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
4643 ewmh_update_client_list();
4649 DNPRINTF(SWM_D_MOVE
, "swapwin: done\n");
4653 get_focus_prev(struct ws_win
*win
)
4655 struct ws_win
*winfocus
= NULL
;
4656 struct ws_win
*cur_focus
= NULL
;
4657 struct ws_win_list
*wl
= NULL
;
4658 struct workspace
*ws
= NULL
;
4660 if (!(win
&& win
->ws
))
4665 cur_focus
= ws
->focus
;
4667 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: win %#x, cur_focus: %#x, "
4668 "focus_prev: %#x\n", WINID(win
), WINID(cur_focus
),
4669 WINID(ws
->focus_prev
));
4671 /* pickle, just focus on whatever */
4672 if (cur_focus
== NULL
) {
4673 /* use prev_focus if valid */
4674 if (ws
->focus_prev
&& find_window(ws
->focus_prev
->id
))
4675 winfocus
= ws
->focus_prev
;
4679 /* if transient focus on parent */
4680 if (TRANS(cur_focus
)) {
4681 winfocus
= find_window(cur_focus
->transient
);
4685 /* if in max_stack try harder */
4686 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
4687 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
4688 if (cur_focus
!= ws
->focus_prev
)
4689 winfocus
= ws
->focus_prev
;
4691 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
4696 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
4698 if (winfocus
== NULL
|| winfocus
== win
) {
4699 switch (focus_close
) {
4700 case SWM_STACK_BOTTOM
:
4701 TAILQ_FOREACH(winfocus
, wl
, entry
)
4702 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4706 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4707 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4710 case SWM_STACK_ABOVE
:
4711 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
4712 while (winfocus
&& ICONIC(winfocus
))
4713 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4715 if (winfocus
== NULL
) {
4716 if (focus_close_wrap
) {
4717 TAILQ_FOREACH(winfocus
, wl
, entry
)
4718 if (!ICONIC(winfocus
) &&
4719 winfocus
!= cur_focus
)
4722 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4724 if (!ICONIC(winfocus
) &&
4725 winfocus
!= cur_focus
)
4730 case SWM_STACK_BELOW
:
4731 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
4732 while (winfocus
&& ICONIC(winfocus
))
4733 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
4736 if (winfocus
== NULL
) {
4737 if (focus_close_wrap
) {
4738 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4740 if (!ICONIC(winfocus
) &&
4741 winfocus
!= cur_focus
)
4744 TAILQ_FOREACH(winfocus
, wl
, entry
)
4745 if (!ICONIC(winfocus
) &&
4746 winfocus
!= cur_focus
)
4754 if (winfocus
== NULL
||
4755 (winfocus
&& (ICONIC(winfocus
) || winfocus
== cur_focus
))) {
4756 if (focus_default
== SWM_STACK_TOP
) {
4757 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4758 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4761 TAILQ_FOREACH(winfocus
, wl
, entry
)
4762 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4769 return get_focus_magic(winfocus
);
4773 get_region_focus(struct swm_region
*r
)
4775 struct ws_win
*winfocus
= NULL
;
4780 if (r
->ws
->focus
&& !ICONIC(r
->ws
->focus
))
4781 winfocus
= r
->ws
->focus
;
4782 else if (r
->ws
->focus_prev
&& !ICONIC(r
->ws
->focus_prev
))
4783 winfocus
= r
->ws
->focus_prev
;
4785 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
4786 if (!ICONIC(winfocus
))
4789 return get_focus_magic(winfocus
);
4793 focus(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4795 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
4796 struct ws_win_list
*wl
= NULL
;
4797 struct workspace
*ws
= NULL
;
4804 cur_focus
= r
->ws
->focus
;
4808 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d, cur_focus: %#x\n", args
->id
,
4811 /* Make sure an uniconified window has focus, if one exists. */
4812 if (cur_focus
== NULL
) {
4813 cur_focus
= TAILQ_FIRST(wl
);
4814 while (cur_focus
!= NULL
&& ICONIC(cur_focus
))
4815 cur_focus
= TAILQ_NEXT(cur_focus
, entry
);
4817 DNPRINTF(SWM_D_FOCUS
, "focus: new cur_focus: %#x\n",
4822 case SWM_ARG_ID_FOCUSPREV
:
4823 if (cur_focus
== NULL
)
4826 winfocus
= cur_focus
;
4828 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
4829 if (winfocus
== NULL
)
4830 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
4831 if (winfocus
== cur_focus
)
4833 } while (winfocus
&& (ICONIC(winfocus
) ||
4834 winfocus
->id
== cur_focus
->transient
||
4835 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4836 winfocus
->transient
== cur_focus
->transient
) ||
4837 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4839 case SWM_ARG_ID_FOCUSNEXT
:
4840 if (cur_focus
== NULL
)
4843 winfocus
= cur_focus
;
4845 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4846 if (winfocus
== NULL
)
4847 winfocus
= TAILQ_FIRST(wl
);
4848 if (winfocus
== cur_focus
)
4850 } while (winfocus
&& (ICONIC(winfocus
) ||
4851 winfocus
->id
== cur_focus
->transient
||
4852 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4853 winfocus
->transient
== cur_focus
->transient
) ||
4854 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4856 case SWM_ARG_ID_FOCUSMAIN
:
4857 if (cur_focus
== NULL
)
4860 winfocus
= TAILQ_FIRST(wl
);
4861 if (winfocus
== cur_focus
)
4862 winfocus
= cur_focus
->ws
->focus_prev
;
4864 case SWM_ARG_ID_FOCUSURGENT
:
4865 /* Search forward for the next urgent window. */
4869 for (i
= 0; i
<= workspace_limit
; ++i
) {
4871 head
= TAILQ_FIRST(&r
->s
->ws
[(ws
->idx
+ i
) %
4872 workspace_limit
].winlist
);
4875 if (head
== cur_focus
) {
4877 winfocus
= cur_focus
;
4880 } else if (get_urgent(head
)) {
4885 head
= TAILQ_NEXT(head
, entry
);
4892 /* Switch ws if new focus is on a different ws. */
4893 if (winfocus
&& winfocus
->ws
!= ws
) {
4894 a
.id
= winfocus
->ws
->idx
;
4895 switchws(bp
, r
, &a
);
4902 if (clear_maximized(ws
) > 0)
4905 focus_win(get_focus_magic(winfocus
));
4910 DNPRINTF(SWM_D_FOCUS
, "focus: done\n");
4914 focus_pointer(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4918 /* Not needed for buttons since this is already done in buttonpress. */
4919 if (bp
->type
== KEYBIND
) {
4920 focus_win(get_pointer_win(r
->s
->root
));
4926 cycle_layout(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4928 struct workspace
*ws
= r
->ws
;
4930 /* suppress unused warning since var is needed */
4934 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
4937 if (ws
->cur_layout
->l_stack
== NULL
)
4938 ws
->cur_layout
= &layouts
[0];
4940 clear_maximized(ws
);
4945 focus_win(get_region_focus(r
));
4952 stack_config(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4954 struct workspace
*ws
= r
->ws
;
4958 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
4961 if (clear_maximized(ws
) > 0)
4964 if (ws
->cur_layout
->l_config
!= NULL
)
4965 ws
->cur_layout
->l_config(ws
, args
->id
);
4967 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
4976 stack(struct swm_region
*r
) {
4977 struct swm_geometry g
;
4978 struct swm_region
*r_prev
;
4984 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
4986 /* Adjust stack area for region bar and padding. */
4988 g
.x
+= region_padding
;
4989 g
.y
+= region_padding
;
4990 g
.w
-= 2 * border_width
+ 2 * region_padding
;
4991 g
.h
-= 2 * border_width
+ 2 * region_padding
;
4992 if (bar_enabled
&& r
->ws
->bar_enabled
) {
4998 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d (screen: "
4999 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
5000 r
->ws
->idx
, r
->s
->idx
, get_region_index(r
), g
.x
, g
.y
, g
.w
, g
.h
);
5002 r_prev
= TAILQ_PREV(r
, swm_region_list
, entry
);
5004 /* Stack bar/input relative to prev. region. */
5005 val
[1] = XCB_STACK_MODE_ABOVE
;
5007 val
[0] = r_prev
->id
;
5008 DNPRINTF(SWM_D_STACK
, "stack: region input %#x "
5009 "relative to %#x.\n", r
->id
, val
[0]);
5010 xcb_configure_window(conn
, r
->id
,
5011 XCB_CONFIG_WINDOW_SIBLING
|
5012 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
5014 val
[0] = r_prev
->bar
->id
;
5015 DNPRINTF(SWM_D_STACK
, "stack: region bar %#x "
5016 "relative to %#x.\n", r
->bar
->id
, val
[0]);
5017 xcb_configure_window(conn
, r
->bar
->id
,
5018 XCB_CONFIG_WINDOW_SIBLING
|
5019 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
5022 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
5023 r
->ws
->cur_layout
->l_string(r
->ws
);
5024 /* save r so we can track region changes */
5030 DNPRINTF(SWM_D_STACK
, "stack: end\n");
5034 store_float_geom(struct ws_win
*win
)
5036 if (win
== NULL
|| win
->ws
->r
== NULL
)
5039 /* retain window geom and region geom */
5040 win
->g_float
= win
->g
;
5041 win
->g_float
.x
-= X(win
->ws
->r
);
5042 win
->g_float
.y
-= Y(win
->ws
->r
);
5043 win
->g_floatvalid
= true;
5044 DNPRINTF(SWM_D_MISC
, "store_float_geom: win %#x, g: (%d,%d)"
5045 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
5046 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
5047 win
->g_float
.w
, win
->g_float
.h
);
5051 load_float_geom(struct ws_win
*win
)
5053 if (win
== NULL
|| win
->ws
->r
== NULL
)
5056 if (win
->g_floatvalid
) {
5057 win
->g
= win
->g_float
;
5058 X(win
) += X(win
->ws
->r
);
5059 Y(win
) += Y(win
->ws
->r
);
5060 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g: (%d,%d)"
5061 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
5064 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g_float "
5065 "is not set.\n", win
->id
);
5070 update_floater(struct ws_win
*win
)
5072 struct workspace
*ws
;
5073 struct swm_region
*r
;
5075 DNPRINTF(SWM_D_MISC
, "update_floater: win %#x\n", WINID(win
));
5082 if ((r
= ws
->r
) == NULL
)
5085 win
->bordered
= true;
5087 if (FULLSCREEN(win
)) {
5088 /* _NET_WM_FULLSCREEN: fullscreen without border. */
5089 if (!win
->g_floatvalid
)
5090 store_float_geom(win
);
5093 win
->bordered
= false;
5094 } else if (MAXIMIZED(win
)) {
5095 /* Maximize: like a single stacked window. */
5096 if (!win
->g_floatvalid
)
5097 store_float_geom(win
);
5101 if (bar_enabled
&& ws
->bar_enabled
&& !maximize_hide_bar
) {
5103 Y(win
) += bar_height
;
5104 HEIGHT(win
) -= bar_height
;
5105 } else if (disable_border
) {
5106 win
->bordered
= false;
5109 if (win
->bordered
) {
5110 /* Window geometry excludes frame. */
5111 X(win
) += border_width
;
5112 Y(win
) += border_width
;
5113 HEIGHT(win
) -= 2 * border_width
;
5114 WIDTH(win
) -= 2 * border_width
;
5117 /* Normal floating window. */
5118 /* Update geometry if new region. */
5120 load_float_geom(win
);
5122 if (((win
->quirks
& SWM_Q_FULLSCREEN
) &&
5123 WIDTH(win
) >= WIDTH(r
) && HEIGHT(win
) >= HEIGHT(r
)) ||
5124 ((!WS_FOCUSED(win
->ws
) || win
->ws
->focus
!= win
) &&
5125 (win
->quirks
& SWM_Q_MINIMALBORDER
))) {
5127 win
->bordered
= false;
5128 } else if (!MANUAL(win
)) {
5129 if (TRANS(win
) && (win
->quirks
& SWM_Q_TRANSSZ
)) {
5130 /* Adjust size on TRANSSZ quirk. */
5131 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
5132 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
5135 if (!(win
->quirks
& SWM_Q_ANYWHERE
)) {
5137 * Floaters and transients are auto-centred
5138 * unless manually moved, resized or ANYWHERE
5141 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2;
5142 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2;
5143 store_float_geom(win
);
5148 /* Ensure at least 1 pixel of the window is in the region. */
5149 region_containment(win
, r
, SWM_CW_ALLSIDES
);
5155 * Send keystrokes to terminal to decrease/increase the font size as the
5156 * window size changes.
5159 adjust_font(struct ws_win
*win
)
5161 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
5162 ABOVE(win
) || TRANS(win
))
5165 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
5166 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
5167 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
5168 win
->font_size_boundary
[win
->font_steps
] =
5169 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
5172 win
->last_inc
= win
->sh
.width_inc
;
5173 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
5174 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
5175 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
5178 win
->last_inc
= win
->sh
.width_inc
;
5179 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
5183 #define SWAPXY(g) do { \
5185 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
5186 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
5189 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, bool flip
)
5191 struct swm_geometry cell
, r_g
= *g
;
5193 int i
= 0, j
= 0, s
= 0, stacks
= 0;
5194 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
5195 int hrh
= 0, extra
= 0, h_slice
= 0, last_h
= 0;
5196 int split
= 0, colno
= 0;
5197 int winno
, mwin
= 0, msize
= 0;
5198 int remain
, missing
, v_slice
, mscale
;
5199 bool bordered
= true, reconfigure
= false;
5202 * cell: geometry for window, including frame.
5203 * mwin: # of windows in master area.
5204 * mscale: size increment of master area.
5205 * stacks: # of stack columns
5208 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
5209 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
5211 memset(&cell
, 0, sizeof(cell
));
5213 /* Prepare tiling variables, if needed. */
5214 if ((winno
= count_win(ws
, false)) > 0) {
5215 /* Find first tiled window. */
5216 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
5217 if (!FLOATING(win
) && !ICONIC(win
))
5220 /* Take into account size hints of first tiled window. */
5222 w_inc
= win
->sh
.width_inc
;
5223 w_base
= win
->sh
.base_width
;
5224 mwin
= ws
->l_state
.horizontal_mwin
;
5225 mscale
= ws
->l_state
.horizontal_msize
;
5226 stacks
= ws
->l_state
.horizontal_stacks
;
5229 w_inc
= win
->sh
.height_inc
;
5230 w_base
= win
->sh
.base_height
;
5231 mwin
= ws
->l_state
.vertical_mwin
;
5232 mscale
= ws
->l_state
.vertical_msize
;
5233 stacks
= ws
->l_state
.vertical_stacks
;
5237 cell
.x
+= border_width
;
5238 cell
.y
+= border_width
;
5240 if (stacks
> winno
- mwin
)
5241 stacks
= winno
- mwin
;
5245 h_slice
= r_g
.h
/ SWM_H_SLICE
;
5246 if (mwin
&& winno
> mwin
) {
5247 v_slice
= r_g
.w
/ SWM_V_SLICE
;
5251 cell
.w
= v_slice
* mscale
;
5253 if (w_inc
> 1 && w_inc
< v_slice
) {
5254 /* Adjust for requested size increment. */
5255 remain
= (cell
.w
- w_base
) % w_inc
;
5261 cell
.x
+= r_g
.w
- msize
;
5264 colno
= split
= winno
/ stacks
;
5265 cell
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
5266 2 * border_width
) / stacks
);
5269 hrh
= r_g
.h
/ colno
;
5270 extra
= r_g
.h
- (colno
* hrh
);
5271 cell
.h
= hrh
- 2 * border_width
;
5272 i
= j
= 0, s
= stacks
;
5275 /* Update window geometry. */
5276 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
5280 if (FLOATING(win
)) {
5281 update_floater(win
);
5286 if (split
&& i
== split
) {
5287 colno
= (winno
- mwin
) / stacks
;
5288 if (s
<= (winno
- mwin
) % stacks
)
5291 hrh
= r_g
.h
/ colno
;
5292 extra
= r_g
.h
- (colno
* hrh
);
5295 cell
.x
+= cell
.w
+ 2 * border_width
+
5298 cell
.w
= (r_g
.w
- msize
-
5299 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
5301 cell
.w
+= (r_g
.w
- msize
-
5302 (stacks
* (2 * border_width
+ tile_gap
))) %
5306 cell
.x
-= cell
.w
+ 2 * border_width
+
5312 cell
.h
= hrh
- 2 * border_width
- tile_gap
;
5315 h_inc
= win
->sh
.width_inc
;
5316 h_base
= win
->sh
.base_width
;
5318 h_inc
= win
->sh
.height_inc
;
5319 h_base
= win
->sh
.base_height
;
5322 if (j
== colno
- 1) {
5323 cell
.h
= hrh
+ extra
;
5324 } else if (h_inc
> 1 && h_inc
< h_slice
) {
5325 /* adjust for window's requested size increment */
5326 remain
= (cell
.h
- h_base
) % h_inc
;
5327 missing
= h_inc
- remain
;
5329 if (missing
<= extra
|| j
== 0) {
5339 cell
.y
= r_g
.y
+ border_width
;
5341 cell
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
5343 /* Window coordinates exclude frame. */
5345 if (winno
> 1 || !disable_border
||
5346 (bar_enabled
&& ws
->bar_enabled
)) {
5353 if (X(win
) != cell
.y
|| Y(win
) != cell
.x
||
5354 WIDTH(win
) != cell
.h
|| HEIGHT(win
) != cell
.w
) {
5358 WIDTH(win
) = cell
.h
;
5359 HEIGHT(win
) = cell
.w
;
5362 if (X(win
) != cell
.x
|| Y(win
) != cell
.y
||
5363 WIDTH(win
) != cell
.w
|| HEIGHT(win
) != cell
.h
) {
5367 WIDTH(win
) = cell
.w
;
5368 HEIGHT(win
) = cell
.h
;
5373 X(win
) -= border_width
;
5374 Y(win
) -= border_width
;
5375 WIDTH(win
) += 2 * border_width
;
5376 HEIGHT(win
) += 2 * border_width
;
5379 if (bordered
!= win
->bordered
) {
5381 win
->bordered
= bordered
;
5394 /* Stack all windows from bottom up. */
5395 TAILQ_FOREACH_REVERSE(win
, &ws
->stack
, ws_win_stack
, stack_entry
)
5397 update_win_stacking(win
);
5399 /* Map all windows from top down. */
5400 TAILQ_FOREACH(win
, &ws
->stack
, stack_entry
)
5404 DNPRINTF(SWM_D_STACK
, "stack_master: done\n");
5408 vertical_config(struct workspace
*ws
, int id
)
5410 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
5414 case SWM_ARG_ID_STACKRESET
:
5415 case SWM_ARG_ID_STACKINIT
:
5416 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
5417 ws
->l_state
.vertical_mwin
= 1;
5418 ws
->l_state
.vertical_stacks
= 1;
5420 case SWM_ARG_ID_MASTERSHRINK
:
5421 if (ws
->l_state
.vertical_msize
> 1)
5422 ws
->l_state
.vertical_msize
--;
5424 case SWM_ARG_ID_MASTERGROW
:
5425 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
5426 ws
->l_state
.vertical_msize
++;
5428 case SWM_ARG_ID_MASTERADD
:
5429 ws
->l_state
.vertical_mwin
++;
5431 case SWM_ARG_ID_MASTERDEL
:
5432 if (ws
->l_state
.vertical_mwin
> 0)
5433 ws
->l_state
.vertical_mwin
--;
5435 case SWM_ARG_ID_STACKBALANCE
:
5436 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ (ws
->l_state
.vertical_stacks
+ 1);
5438 case SWM_ARG_ID_STACKINC
:
5439 ws
->l_state
.vertical_stacks
++;
5441 case SWM_ARG_ID_STACKDEC
:
5442 if (ws
->l_state
.vertical_stacks
> 1)
5443 ws
->l_state
.vertical_stacks
--;
5445 case SWM_ARG_ID_FLIPLAYOUT
:
5446 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
5454 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5456 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
5458 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
5462 horizontal_config(struct workspace
*ws
, int id
)
5464 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
5467 case SWM_ARG_ID_STACKRESET
:
5468 case SWM_ARG_ID_STACKINIT
:
5469 ws
->l_state
.horizontal_mwin
= 1;
5470 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
5471 ws
->l_state
.horizontal_stacks
= 1;
5473 case SWM_ARG_ID_MASTERSHRINK
:
5474 if (ws
->l_state
.horizontal_msize
> 1)
5475 ws
->l_state
.horizontal_msize
--;
5477 case SWM_ARG_ID_MASTERGROW
:
5478 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
5479 ws
->l_state
.horizontal_msize
++;
5481 case SWM_ARG_ID_MASTERADD
:
5482 ws
->l_state
.horizontal_mwin
++;
5484 case SWM_ARG_ID_MASTERDEL
:
5485 if (ws
->l_state
.horizontal_mwin
> 0)
5486 ws
->l_state
.horizontal_mwin
--;
5488 case SWM_ARG_ID_STACKBALANCE
:
5489 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ (ws
->l_state
.horizontal_stacks
+ 1);
5491 case SWM_ARG_ID_STACKINC
:
5492 ws
->l_state
.horizontal_stacks
++;
5494 case SWM_ARG_ID_STACKDEC
:
5495 if (ws
->l_state
.horizontal_stacks
> 1)
5496 ws
->l_state
.horizontal_stacks
--;
5498 case SWM_ARG_ID_FLIPLAYOUT
:
5499 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
5507 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5509 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
5511 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
5514 /* fullscreen view */
5516 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5518 struct swm_geometry gg
= *g
;
5519 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
, *tmpw
;
5522 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
5527 winno
= count_win(ws
, false);
5528 if (winno
== 0 && count_win(ws
, true) == 0)
5531 /* Figure out which top level window should be visible. */
5532 if (ws
->focus_pending
)
5533 win
= ws
->focus_pending
;
5536 else if (ws
->focus_prev
)
5537 win
= ws
->focus_prev
;
5539 win
= TAILQ_FIRST(&ws
->winlist
);
5541 DNPRINTF(SWM_D_STACK
, "max_stack: focus_pending: %#x, focus: %#x, "
5542 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws
->focus_pending
),
5543 WINID(ws
->focus
), WINID(ws
->focus_prev
),
5544 WINID(TAILQ_FIRST(&ws
->winlist
)), win
->id
);
5546 /* Update window geometry. */
5547 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5556 /* Set maximized flag for all maxed windows. */
5557 if (!MAXIMIZED(w
)) {
5558 /* Preserve floating geometry. */
5560 store_float_geom(w
);
5562 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_MAXIMIZED
);
5563 ewmh_update_wm_state(w
);
5566 /* Only reconfigure if necessary. */
5567 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
5568 HEIGHT(w
) != gg
.h
) {
5571 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
)) {
5572 w
->bordered
= false;
5573 WIDTH(w
) += 2 * border_width
;
5574 HEIGHT(w
) += 2 * border_width
;
5577 X(w
) += border_width
;
5578 Y(w
) += border_width
;
5585 /* If transient, stack parent and its children. */
5586 if (TRANS(win
) && (parent
= find_window(win
->transient
))) {
5587 raise_window(parent
);
5589 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5590 if (w
->transient
== parent
->id
)
5594 /* Make sure focus window is on top. */
5597 /* Stack any children of focus window. */
5598 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5599 if (w
->transient
== win
->id
)
5602 /* Map all windows. */
5603 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
5609 send_to_rg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5611 int ridx
= args
->id
, i
, num_screens
;
5612 struct swm_region
*rr
= NULL
;
5615 num_screens
= get_screen_count();
5616 /* do nothing if we don't have more than one screen */
5617 if (!(num_screens
> 1 || outputs
> 1))
5620 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
5622 rr
= TAILQ_FIRST(&r
->s
->rl
);
5623 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
5624 rr
= TAILQ_NEXT(rr
, entry
);
5631 send_to_ws(bp
, r
, &a
);
5635 region_under(struct swm_screen
*s
, int x
, int y
)
5637 struct swm_region
*r
= NULL
;
5639 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
5640 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
5641 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
5642 WIDTH(r
), HEIGHT(r
), x
, y
);
5643 if (X(r
) <= x
&& x
< MAX_X(r
))
5644 if (Y(r
) <= y
&& y
< MAX_Y(r
))
5651 /* Transfer focused window to target workspace and focus. */
5653 send_to_ws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5655 int wsid
= args
->id
;
5656 struct ws_win
*win
= NULL
;
5660 if (r
&& r
->ws
&& r
->ws
->focus
)
5665 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win %#x, ws %d\n", win
->id
, wsid
);
5667 if (wsid
< 0 || wsid
>= workspace_limit
)
5670 if (win
->ws
->idx
== wsid
)
5673 win_to_ws(win
, wsid
, true);
5675 /* Set new focus on target ws. */
5676 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5677 win
->ws
->focus_prev
= win
->ws
->focus
;
5678 win
->ws
->focus
= win
;
5679 win
->ws
->focus_pending
= NULL
;
5681 if (win
->ws
->focus_prev
)
5682 draw_frame(win
->ws
->focus_prev
);
5685 DNPRINTF(SWM_D_STACK
, "send_to_ws: focus_pending: %#x, focus: %#x, "
5686 "focus_prev: %#x, first: %#x, win: %#x\n",
5687 WINID(r
->ws
->focus_pending
), WINID(r
->ws
->focus
),
5688 WINID(r
->ws
->focus_prev
), WINID(TAILQ_FIRST(&r
->ws
->winlist
)),
5691 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5692 ewmh_update_wm_state(win
);
5694 /* Restack focused region. */
5697 /* If destination ws has a region, restack. */
5700 load_float_geom(win
);
5705 /* Set new focus on current ws. */
5706 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5707 if (r
->ws
->focus
!= NULL
) {
5708 focus_win(r
->ws
->focus
);
5710 DNPRINTF(SWM_D_FOCUS
, "send_to_ws: set_input_focus: "
5711 "%#x, revert-to: parent, time: 0\n", r
->id
);
5712 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
5713 r
->id
, XCB_CURRENT_TIME
);
5722 /* Transfer focused window to region-relative workspace and focus. */
5724 send_to_rg_relative(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5727 struct swm_region
*r_other
;
5729 if (args
->id
== 1) {
5730 r_other
= TAILQ_NEXT(r
, entry
);
5731 if (r_other
== NULL
)
5732 r_other
= TAILQ_FIRST(&r
->s
->rl
);
5734 r_other
= TAILQ_PREV(r
, swm_region_list
, entry
);
5735 if (r_other
== NULL
)
5736 r_other
= TAILQ_LAST(&r
->s
->rl
, swm_region_list
);
5739 /* Map relative to absolute */
5741 args_abs
.id
= r_other
->ws
->idx
;
5743 send_to_ws(bp
, r
, &args_abs
);
5747 win_to_ws(struct ws_win
*win
, int wsid
, bool unfocus
)
5749 struct ws_win
*parent
;
5750 struct workspace
*ws
, *nws
, *pws
;
5752 if (wsid
< 0 || wsid
>= workspace_limit
)
5755 if (win
->ws
->idx
== wsid
)
5759 nws
= &win
->s
->ws
[wsid
];
5761 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win %#x, ws %d -> %d\n", win
->id
,
5764 /* Cleanup focus on source ws. */
5765 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
5766 (ws
->focus
== win
|| ws
->focus_pending
== win
))
5767 ws
->focus_pending
= get_focus_prev(win
);
5769 /* Move the parent if this is a transient window. */
5771 parent
= find_window(win
->transient
);
5774 /* Set new focus in parent's ws if needed. */
5775 if (pws
->focus
== parent
) {
5776 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5777 pws
->focus_pending
=
5778 get_focus_prev(parent
);
5780 unfocus_win(parent
);
5782 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5783 pws
->focus
= pws
->focus_pending
;
5784 pws
->focus_pending
= NULL
;
5788 /* Don't unmap parent if new ws is visible */
5790 unmap_window(parent
);
5793 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
5794 TAILQ_REMOVE(&ws
->stack
, parent
, stack_entry
);
5795 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
5796 TAILQ_INSERT_TAIL(&nws
->stack
, parent
, stack_entry
);
5799 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5800 "_NET_WM_DESKTOP: %d\n", wsid
);
5801 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5802 parent
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
5803 XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5810 if (ws
->focus_prev
== win
)
5811 ws
->focus_prev
= NULL
;
5813 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& ws
->focus_pending
!= NULL
) {
5814 ws
->focus
= ws
->focus_pending
;
5815 ws
->focus_pending
= NULL
;
5818 /* Don't unmap if new ws is visible */
5823 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
5824 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
5825 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
5826 TAILQ_INSERT_TAIL(&nws
->stack
, win
, stack_entry
);
5829 /* Update the window's workspace property: _NET_WM_DESKTOP */
5830 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5831 "_NET_WM_DESKTOP: %d\n", wsid
);
5832 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
5833 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5835 ewmh_update_client_list();
5837 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
5841 pressbutton(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5843 /* suppress unused warning since var is needed */
5847 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
5848 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5849 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
5850 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5854 raise_focus(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5859 /* Suppress warning. */
5863 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
5867 r
->ws
->focus_raise
= win
;
5870 /* Temporarily override stacking order also in the stack */
5871 if (!FLOATING(win
)) {
5872 val
= XCB_STACK_MODE_ABOVE
;
5873 xcb_configure_window(conn
, win
->frame
,
5874 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
5879 raise_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5881 /* Suppress warning. */
5885 if (r
== NULL
|| r
->ws
== NULL
)
5888 if (r
->ws
->focus
&& MAXIMIZED(r
->ws
->focus
))
5891 r
->ws
->always_raise
= !r
->ws
->always_raise
;
5893 /* Update focused win stacking order based on new always_raise value. */
5894 raise_window(r
->ws
->focus
);
5900 iconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5904 /* Suppress warning. */
5908 if ((w
= r
->ws
->focus
) == NULL
)
5911 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_HIDDEN
);
5912 ewmh_update_wm_state(w
);
5920 get_win_name(xcb_window_t win
)
5923 xcb_get_property_cookie_t c
;
5924 xcb_get_property_reply_t
*r
;
5926 /* First try _NET_WM_NAME for UTF-8. */
5927 c
= xcb_get_property(conn
, 0, win
, ewmh
[_NET_WM_NAME
].atom
,
5928 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5929 r
= xcb_get_property_reply(conn
, c
, NULL
);
5930 if (r
&& r
->type
== XCB_NONE
) {
5932 /* Use WM_NAME instead; no UTF-8. */
5933 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
5934 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5935 r
= xcb_get_property_reply(conn
, c
, NULL
);
5938 if (r
&& r
->type
!= XCB_NONE
&& r
->length
> 0)
5939 name
= strndup(xcb_get_property_value(r
),
5940 xcb_get_property_value_length(r
));
5945 err(1, "get_win_name: failed to allocate memory.");
5953 uniconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5962 DNPRINTF(SWM_D_MISC
, "uniconify\n");
5964 if (r
== NULL
|| r
->ws
== NULL
)
5967 /* make sure we have anything to uniconify */
5968 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5969 if (win
->ws
== NULL
)
5970 continue; /* should never happen */
5976 DNPRINTF(SWM_D_MISC
, "uniconify: count: %d\n", count
);
5982 search_resp_action
= SWM_SEARCH_UNICONIFY
;
5984 spawn_select(r
, args
, "search", &searchpid
);
5986 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5989 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5990 if (win
->ws
== NULL
)
5991 continue; /* should never happen */
5995 name
= get_win_name(win
->id
);
5996 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
6004 name_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6010 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
6016 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
6018 spawn_select(r
, args
, "name_workspace", &searchpid
);
6020 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
6023 fprintf(lfile
, "%s", "");
6028 search_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6031 struct workspace
*ws
;
6036 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
6042 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
6044 spawn_select(r
, args
, "search", &searchpid
);
6046 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
6049 for (i
= 0; i
< workspace_limit
; i
++) {
6053 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
6054 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
6061 search_win_cleanup(void)
6063 struct search_window
*sw
= NULL
;
6065 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
6066 xcb_destroy_window(conn
, sw
->indicator
);
6067 TAILQ_REMOVE(&search_wl
, sw
, entry
);
6073 search_win(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6075 struct ws_win
*win
= NULL
;
6076 struct search_window
*sw
= NULL
;
6079 xcb_screen_t
*screen
;
6080 int i
, width
, height
;
6088 XRectangle l_ibox
, l_lbox
;
6092 DNPRINTF(SWM_D_MISC
, "search_win\n");
6095 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
6097 spawn_select(r
, args
, "search", &searchpid
);
6099 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
6102 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
6103 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
6105 TAILQ_INIT(&search_wl
);
6108 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
6112 sw
= calloc(1, sizeof(struct search_window
));
6114 warn("search_win: calloc");
6116 search_win_cleanup();
6122 snprintf(s
, sizeof s
, "%d", i
);
6125 w
= xcb_generate_id(conn
);
6126 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6127 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6128 wa
[2] = screen
->default_colormap
;
6130 if (bar_font_legacy
) {
6131 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
6132 width
= l_lbox
.width
+ 4;
6133 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
6135 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
6137 width
= info
.width
+ 4;
6138 height
= bar_font
->height
+ 4;
6141 xcb_create_window(conn
, screen
->root_depth
, w
, win
->frame
, 0, 0,
6142 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
6143 screen
->root_visual
, XCB_CW_BACK_PIXEL
|
6144 XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
, wa
);
6146 xcb_map_window(conn
, w
);
6149 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
6151 if (bar_font_legacy
) {
6152 l_gcv
.graphics_exposures
= 0;
6153 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
6155 XSetForeground(display
, l_draw
,
6156 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
6158 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
6159 (bar_fs_extents
->max_logical_extent
.height
-
6160 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
6162 XFreeGC(display
, l_draw
);
6165 draw
= XftDrawCreate(display
, w
,
6166 DefaultVisual(display
, r
->s
->idx
),
6167 DefaultColormap(display
, r
->s
->idx
));
6169 XftDrawStringUtf8(draw
, &search_font_color
, bar_font
, 2,
6170 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
6171 bar_font
->descent
, (FcChar8
*)s
, len
);
6173 XftDrawDestroy(draw
);
6176 DNPRINTF(SWM_D_MISC
, "search_win: mapped win %#x\n", w
);
6178 fprintf(lfile
, "%d\n", i
);
6188 search_resp_uniconify(const char *resp
, size_t len
)
6194 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
6196 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
6199 name
= get_win_name(win
->id
);
6200 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
6205 if (strncmp(s
, resp
, len
) == 0) {
6206 /* XXX this should be a callback to generalize */
6207 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_HIDDEN
);
6208 ewmh_update_wm_state(win
);
6218 search_resp_name_workspace(const char *resp
, size_t len
)
6220 struct workspace
*ws
;
6222 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
6224 if (search_r
->ws
== NULL
)
6229 free(search_r
->ws
->name
);
6230 search_r
->ws
->name
= NULL
;
6234 ws
->name
= strdup(resp
);
6235 if (ws
->name
== NULL
) {
6236 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
6237 "strdup: %s", strerror(errno
));
6241 ewmh_update_desktop_names();
6242 ewmh_get_desktop_names();
6247 ewmh_update_desktop_names(void)
6249 char *name_list
= NULL
, *p
;
6250 int num_screens
, i
, j
, len
= 0, tot
= 0;
6252 num_screens
= get_screen_count();
6253 for (i
= 0; i
< num_screens
; ++i
) {
6254 for (j
= 0; j
< workspace_limit
; ++j
) {
6255 if (screens
[i
].ws
[j
].name
!= NULL
)
6256 len
+= strlen(screens
[i
].ws
[j
].name
);
6260 if ((name_list
= calloc(len
, sizeof(char))) == NULL
)
6261 err(1, "update_desktop_names: calloc: failed to "
6262 "allocate memory.");
6265 for (j
= 0; j
< workspace_limit
; ++j
) {
6266 if (screens
[i
].ws
[j
].name
!= NULL
) {
6267 len
= strlen(screens
[i
].ws
[j
].name
);
6268 memcpy(p
, screens
[i
].ws
[j
].name
, len
);
6277 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6278 screens
[i
].root
, ewmh
[_NET_DESKTOP_NAMES
].atom
,
6279 a_utf8_string
, 8, tot
, name_list
);
6289 ewmh_get_desktop_names(void)
6292 xcb_get_property_cookie_t c
;
6293 xcb_get_property_reply_t
*r
;
6294 int num_screens
, i
, j
, n
, k
;
6296 num_screens
= get_screen_count();
6297 for (i
= 0; i
< num_screens
; ++i
) {
6298 for (j
= 0; j
< workspace_limit
; ++j
) {
6299 free(screens
[i
].ws
[j
].name
);
6300 screens
[i
].ws
[j
].name
= NULL
;
6303 c
= xcb_get_property(conn
, 0, screens
[i
].root
,
6304 ewmh
[_NET_DESKTOP_NAMES
].atom
,
6305 a_utf8_string
, 0, UINT32_MAX
);
6306 r
= xcb_get_property_reply(conn
, c
, NULL
);
6310 names
= xcb_get_property_value(r
);
6311 n
= xcb_get_property_value_length(r
);
6313 for (j
= 0, k
= 0; j
< n
; ++j
) {
6314 if (*(names
+ j
) != '\0') {
6315 screens
[i
].ws
[k
].name
= strdup(names
+ j
);
6316 j
+= strlen(names
+ j
);
6325 ewmh_update_client_list(void)
6328 int num_screens
, i
, j
, k
= 0, count
= 0;
6331 num_screens
= get_screen_count();
6332 for (i
= 0; i
< num_screens
; ++i
) {
6333 for (j
= 0; j
< workspace_limit
; ++j
)
6334 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6337 DNPRINTF(SWM_D_PROP
, "ewmh_update_client_list: win count: %d\n",
6343 wins
= calloc(count
, sizeof(xcb_window_t
));
6345 err(1, "ewmh_update_client_list: calloc: failed to "
6346 "allocate memory.");
6348 for (j
= 0, k
= 0; j
< workspace_limit
; ++j
)
6349 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6350 wins
[k
++] = win
->id
;
6352 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6353 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
6354 XCB_ATOM_WINDOW
, 32, count
, wins
);
6361 ewmh_update_current_desktop(void)
6365 num_screens
= get_screen_count();
6366 for (i
= 0; i
< num_screens
; ++i
) {
6367 if (screens
[i
].r_focus
)
6368 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6369 screens
[i
].root
, ewmh
[_NET_CURRENT_DESKTOP
].atom
,
6370 XCB_ATOM_CARDINAL
, 32, 1,
6371 &screens
[i
].r_focus
->ws
->idx
);
6376 ewmh_update_desktops(void)
6378 int num_screens
, i
, j
;
6381 vals
= calloc(workspace_limit
* 2, sizeof(uint32_t));
6383 err(1, "ewmh_update_desktops: calloc: failed to allocate "
6386 num_screens
= get_screen_count();
6387 for (i
= 0; i
< num_screens
; i
++) {
6388 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6389 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
6390 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
6392 for (j
= 0; j
< workspace_limit
; ++j
) {
6393 if (screens
[i
].ws
[j
].r
!= NULL
) {
6394 vals
[j
* 2] = X(screens
[i
].ws
[j
].r
);
6395 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].r
);
6396 } else if (screens
[i
].ws
[j
].old_r
!= NULL
) {
6397 vals
[j
* 2] = X(screens
[i
].ws
[j
].old_r
);
6398 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].old_r
);
6400 vals
[j
* 2] = vals
[j
* 2 + 1] = 0;
6404 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6405 screens
[i
].root
, ewmh
[_NET_DESKTOP_VIEWPORT
].atom
,
6406 XCB_ATOM_CARDINAL
, 32, workspace_limit
* 2, vals
);
6413 search_resp_search_workspace(const char *resp
)
6420 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
6424 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
6431 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
6433 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
6440 switchws(NULL
, search_r
, &a
);
6444 search_resp_search_window(const char *resp
)
6449 struct search_window
*sw
;
6451 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
6455 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
6460 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
6462 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
6469 TAILQ_FOREACH(sw
, &search_wl
, entry
)
6470 if (idx
== sw
->idx
) {
6476 #define MAX_RESP_LEN 1024
6479 search_do_resp(void)
6485 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
6490 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
6491 warn("search: calloc");
6495 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
6497 warn("search: read error");
6500 resp
[rbytes
] = '\0';
6503 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
6504 * newline, so work around that by sanitizing the resp now.
6506 resp
[strcspn(resp
, "\n")] = '\0';
6509 switch (search_resp_action
) {
6510 case SWM_SEARCH_UNICONIFY
:
6511 search_resp_uniconify(resp
, len
);
6513 case SWM_SEARCH_NAME_WORKSPACE
:
6514 search_resp_name_workspace(resp
, len
);
6516 case SWM_SEARCH_SEARCH_WORKSPACE
:
6517 search_resp_search_workspace(resp
);
6519 case SWM_SEARCH_SEARCH_WINDOW
:
6520 search_resp_search_window(resp
);
6525 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
6526 search_win_cleanup();
6528 search_resp_action
= SWM_SEARCH_NONE
;
6529 close(select_resp_pipe
[0]);
6536 wkill(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6540 DNPRINTF(SWM_D_MISC
, "wkill: win %#x, id: %d\n", WINID(r
->ws
->focus
),
6543 if (r
->ws
->focus
== NULL
)
6546 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
6547 xcb_kill_client(conn
, r
->ws
->focus
->id
);
6549 if (r
->ws
->focus
->can_delete
)
6550 client_msg(r
->ws
->focus
, a_delete
, 0);
6556 clear_maximized(struct workspace
*ws
)
6561 /* Clear any maximized win(s) on ws, from bottom up. */
6562 TAILQ_FOREACH_REVERSE(w
, &ws
->stack
, ws_win_stack
, stack_entry
)
6564 ewmh_apply_flags(w
, w
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
6565 ewmh_update_wm_state(w
);
6573 maximize_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6575 struct ws_win
*w
= r
->ws
->focus
;
6577 /* suppress unused warning since var is needed */
6584 DNPRINTF(SWM_D_MISC
, "maximize_toggle: win %#x\n", w
->id
);
6589 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6592 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_MAXIMIZED
);
6593 ewmh_update_wm_state(w
);
6597 if (w
== w
->ws
->focus
)
6602 DNPRINTF(SWM_D_MISC
, "maximize_toggle: done\n");
6606 floating_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6608 struct ws_win
*w
= r
->ws
->focus
;
6610 /* suppress unused warning since var is needed */
6617 DNPRINTF(SWM_D_MISC
, "floating_toggle: win %#x\n", w
->id
);
6619 if (FULLSCREEN(w
) || TRANS(w
))
6622 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6625 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_ABOVE
);
6626 ewmh_update_wm_state(w
);
6630 if (w
== w
->ws
->focus
)
6635 DNPRINTF(SWM_D_MISC
, "floating_toggle: done\n");
6639 fullscreen_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6641 struct ws_win
*w
= r
->ws
->focus
;
6643 /* suppress unused warning since var is needed */
6650 DNPRINTF(SWM_D_MISC
, "fullscreen_toggle: win %#x\n", w
->id
);
6652 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_FULLSCREEN
);
6653 ewmh_update_wm_state(w
);
6657 if (w
== w
->ws
->focus
)
6662 DNPRINTF(SWM_D_MISC
, "fullscreen_toggle: done\n");
6665 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
6667 struct swm_geometry g
= r
->g
;
6668 int rt
, lt
, tp
, bm
, bw
;
6670 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
6673 * Perpendicular distance of each side of the window to the respective
6674 * side of the region boundary. Positive values indicate the side of
6675 * the window has passed beyond the region boundary.
6677 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) - MAX_X(r
) : bw
;
6678 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) : bw
;
6679 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) - MAX_Y(r
) : bw
;
6680 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) : bw
;
6682 DNPRINTF(SWM_D_MISC
, "region_containment: win %#x, rt: %d, lt: %d, "
6683 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
6684 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
6685 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
6688 * Disable containment if any of the flagged sides went beyond the
6689 * containment boundary, or if containment is disabled.
6691 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
6692 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
6693 /* Make sure window has at least 1 pixel in the region */
6694 g
.x
+= 1 - WIDTH(win
);
6695 g
.y
+= 1 - HEIGHT(win
);
6696 g
.w
+= 2 * WIDTH(win
) - 2;
6697 g
.h
+= 2 * HEIGHT(win
) - 2;
6700 constrain_window(win
, &g
, &opts
);
6703 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6705 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
6707 DNPRINTF(SWM_D_MISC
, "constrain_window: win %#x, (x,y) w x h: "
6708 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6709 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
6710 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
6711 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
6712 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
6713 YESNO(*opts
& SWM_CW_RESIZABLE
));
6715 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) > b
->x
+ b
->w
) {
6716 if (*opts
& SWM_CW_RESIZABLE
)
6717 WIDTH(win
) = b
->x
+ b
->w
- X(win
);
6719 X(win
) = b
->x
+ b
->w
- WIDTH(win
);
6722 if ((*opts
& SWM_CW_LEFT
) && X(win
) < b
->x
) {
6723 if (*opts
& SWM_CW_RESIZABLE
)
6724 WIDTH(win
) -= b
->x
- X(win
);
6729 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) > b
->y
+ b
->h
) {
6730 if (*opts
& SWM_CW_RESIZABLE
)
6731 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
);
6733 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
);
6736 if ((*opts
& SWM_CW_TOP
) && Y(win
) < b
->y
) {
6737 if (*opts
& SWM_CW_RESIZABLE
)
6738 HEIGHT(win
) -= b
->y
- Y(win
);
6743 if (*opts
& SWM_CW_RESIZABLE
) {
6746 if (HEIGHT(win
) < 1)
6752 draw_frame(struct ws_win
*win
)
6754 xcb_rectangle_t rect
[4];
6758 if (win
->frame
== XCB_WINDOW_NONE
) {
6759 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x not "
6760 "reparented.\n", win
->id
);
6764 if (!win
->bordered
) {
6765 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x frame "
6766 "disabled\n", win
->id
);
6769 if (WS_FOCUSED(win
->ws
) && win
->ws
->focus
== win
)
6770 pixel
= MAXIMIZED(win
) ?
6771 &win
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].pixel
:
6772 &win
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6774 pixel
= MAXIMIZED(win
) ?
6775 &win
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].pixel
:
6776 &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6778 /* Top (with corners) */
6781 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6782 rect
[n
].height
= border_width
;
6783 /* Left (without corners) */
6785 rect
[n
].y
= border_width
;
6786 rect
[n
].width
= border_width
;
6787 rect
[n
].height
= HEIGHT(win
);
6788 /* Right (without corners) */
6789 rect
[++n
].x
= border_width
+ WIDTH(win
);
6790 rect
[n
].y
= border_width
;
6791 rect
[n
].width
= border_width
;
6792 rect
[n
].height
= HEIGHT(win
);
6793 /* Bottom (with corners)*/
6795 rect
[n
].y
= border_width
+ HEIGHT(win
);
6796 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6797 rect
[n
].height
= border_width
;
6799 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, pixel
);
6800 xcb_poly_fill_rectangle(conn
, win
->frame
, win
->s
->bar_gc
, 4, rect
);
6804 update_window(struct ws_win
*win
)
6809 if (win
->frame
== XCB_WINDOW_NONE
) {
6810 DNPRINTF(SWM_D_EVENT
, "update_window: skip win %#x; "
6811 "not reparented.\n", win
->id
);
6815 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
6816 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
6817 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6819 /* Reconfigure frame. */
6820 if (win
->bordered
) {
6821 wc
[0] = X(win
) - border_width
;
6822 wc
[1] = Y(win
) - border_width
;
6823 wc
[2] = WIDTH(win
) + 2 * border_width
;
6824 wc
[3] = HEIGHT(win
) + 2 * border_width
;
6829 wc
[3] = HEIGHT(win
);
6834 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x frame %#x, (x,y) w x h: "
6835 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, win
->frame
, wc
[0],
6836 wc
[1], wc
[2], wc
[3], YESNO(win
->bordered
));
6838 xcb_configure_window(conn
, win
->frame
, mask
, wc
);
6840 /* Reconfigure client window. */
6841 wc
[0] = wc
[1] = win
->bordered
? border_width
: 0;
6843 wc
[3] = HEIGHT(win
);
6845 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x, (x,y) w x h: "
6846 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
6847 wc
[3], YESNO(win
->bordered
));
6848 xcb_configure_window(conn
, win
->id
, mask
, wc
);
6850 /* ICCCM 4.2.3 Synthetic ConfigureNotify when moved and not resized. */
6851 if ((X(win
) != win
->g_prev
.x
|| Y(win
) != win
->g_prev
.y
) &&
6852 (win
->java
|| (WIDTH(win
) == win
->g_prev
.w
&&
6853 HEIGHT(win
) == win
->g_prev
.h
))) {
6854 /* Java has special needs when moved together with a resize. */
6855 config_win(win
, NULL
);
6858 win
->g_prev
= win
->g
;
6862 SIMPLEQ_ENTRY(event
) entry
;
6863 xcb_generic_event_t
*ev
;
6865 SIMPLEQ_HEAD(event_queue
, event
) events
= SIMPLEQ_HEAD_INITIALIZER(events
);
6867 xcb_generic_event_t
*
6868 get_next_event(bool dowait
)
6871 xcb_generic_event_t
*evt
;
6873 /* Try queue first. */
6874 if ((ep
= SIMPLEQ_FIRST(&events
))) {
6876 SIMPLEQ_REMOVE_HEAD(&events
, entry
);
6879 evt
= xcb_wait_for_event(conn
);
6881 evt
= xcb_poll_for_event(conn
);
6887 put_back_event(xcb_generic_event_t
*evt
)
6890 if ((ep
= malloc(sizeof (struct event
))) == NULL
)
6891 err(1, "put_back_event: failed to allocate memory.");
6893 SIMPLEQ_INSERT_HEAD(&events
, ep
, entry
);
6896 /* Peeks at next event to detect auto-repeat. */
6898 keyrepeating(xcb_key_release_event_t
*kre
)
6900 xcb_generic_event_t
*evt
;
6902 /* Ensure repeating keypress is finished processing. */
6905 if ((evt
= get_next_event(false))) {
6906 put_back_event(evt
);
6908 if (XCB_EVENT_RESPONSE_TYPE(evt
) == XCB_KEY_PRESS
&&
6909 kre
->sequence
== evt
->sequence
&&
6910 kre
->detail
== ((xcb_key_press_event_t
*)evt
)->detail
)
6918 keybindreleased(struct binding
*bp
, xcb_key_release_event_t
*kre
)
6920 if (bp
->type
== KEYBIND
&& !keyrepeating(kre
) &&
6921 bp
->value
== xcb_key_press_lookup_keysym(syms
, kre
, 0))
6927 #define SWM_RESIZE_STEPS (50)
6930 resize_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
6932 xcb_timestamp_t timestamp
= 0;
6933 struct swm_region
*r
= NULL
;
6934 struct swm_geometry g
;
6935 int top
= 0, left
= 0;
6937 xcb_cursor_t cursor
;
6938 xcb_query_pointer_reply_t
*xpr
= NULL
;
6939 xcb_generic_event_t
*evt
;
6940 xcb_motion_notify_event_t
*mne
;
6941 bool resizing
, step
= false;
6947 if (FULLSCREEN(win
))
6950 /* In max_stack mode, should only resize transients. */
6951 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6954 DNPRINTF(SWM_D_EVENT
, "resize: win %#x, floating: %s, "
6955 "transient: %#x\n", win
->id
, YESNO(ABOVE(win
)),
6959 store_float_geom(win
);
6960 else if (!(TRANS(win
) || ABOVE(win
)))
6963 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6965 ewmh_update_wm_state(win
);
6971 /* It's possible for win to have been freed during focus_flush(). */
6972 if (validate_win(win
)) {
6973 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6978 case SWM_ARG_ID_WIDTHSHRINK
:
6979 WIDTH(win
) -= SWM_RESIZE_STEPS
;
6982 case SWM_ARG_ID_WIDTHGROW
:
6983 WIDTH(win
) += SWM_RESIZE_STEPS
;
6986 case SWM_ARG_ID_HEIGHTSHRINK
:
6987 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
6990 case SWM_ARG_ID_HEIGHTGROW
:
6991 HEIGHT(win
) += SWM_RESIZE_STEPS
;
6998 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6999 SWM_CW_HARDBOUNDARY
);
7001 store_float_geom(win
);
7005 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
7006 SWM_CW_SOFTBOUNDARY
);
7009 /* get cursor offset from window root */
7010 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
7017 if (xpr
->win_x
< WIDTH(win
) / 2)
7020 if (xpr
->win_y
< HEIGHT(win
) / 2)
7023 if (opt
== SWM_ARG_ID_CENTER
)
7024 cursor
= cursors
[XC_SIZING
].cid
;
7026 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
7027 XC_TOP_RIGHT_CORNER
].cid
;
7029 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
7030 XC_BOTTOM_RIGHT_CORNER
].cid
;
7032 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
7033 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
7036 /* Release keyboard freeze if called via keybind. */
7037 if (bp
->type
== KEYBIND
)
7038 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7043 while (resizing
&& (evt
= get_next_event(true))) {
7044 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
7045 case XCB_BUTTON_RELEASE
:
7046 if (bp
->type
== BTNBIND
&& bp
->value
==
7047 ((xcb_button_release_event_t
*)evt
)->detail
)
7050 case XCB_KEY_RELEASE
:
7051 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
7054 case XCB_MOTION_NOTIFY
:
7055 mne
= (xcb_motion_notify_event_t
*)evt
;
7056 DNPRINTF(SWM_D_EVENT
, "resize: MOTION_NOTIFY: "
7057 "root: %#x\n", mne
->root
);
7059 /* cursor offset/delta from start of the operation */
7060 dx
= mne
->root_x
- xpr
->root_x
;
7061 dy
= mne
->root_y
- xpr
->root_y
;
7067 if (opt
== SWM_ARG_ID_CENTER
) {
7068 if (g
.h
/ 2 + dy
< 1)
7072 HEIGHT(win
) = g
.h
+ 2 * dy
;
7080 HEIGHT(win
) = g
.h
+ dy
;
7087 if (opt
== SWM_ARG_ID_CENTER
) {
7088 if (g
.w
/ 2 + dx
< 1)
7092 WIDTH(win
) = g
.w
+ 2 * dx
;
7100 WIDTH(win
) = g
.w
+ dx
;
7103 /* not free, don't sync more than 120 times / second */
7104 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
7105 timestamp
= mne
->time
;
7106 regionize(win
, mne
->root_x
, mne
->root_y
);
7107 region_containment(win
, r
, SWM_CW_ALLSIDES
|
7108 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
7109 SWM_CW_SOFTBOUNDARY
);
7114 case XCB_BUTTON_PRESS
:
7116 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_PRESS ignored\n");
7117 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7118 ((xcb_button_press_event_t
*)evt
)->time
);
7123 DNPRINTF(SWM_D_EVENT
, "resize: KEY_PRESS ignored\n");
7124 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7125 ((xcb_key_press_event_t
*)evt
)->time
);
7131 /* It's possible for win to have been freed above. */
7132 if (validate_win(win
)) {
7133 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
7141 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
7142 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
7146 store_float_geom(win
);
7148 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7150 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
7154 resize(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
7156 struct ws_win
*win
= NULL
;
7157 xcb_query_pointer_reply_t
*qpr
= NULL
;
7162 if (args
->id
!= SWM_ARG_ID_DONTCENTER
&&
7163 args
->id
!= SWM_ARG_ID_CENTER
) {
7164 /* {height,width}_{grow,shrink} use the focus window. */
7168 /* resize uses window under pointer. */
7169 qpr
= xcb_query_pointer_reply(conn
,
7170 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7172 win
= find_window(qpr
->child
);
7178 resize_win(win
, bp
, args
->id
);
7180 if (args
->id
&& bp
->type
== KEYBIND
)
7186 /* Try to set window region based on supplied coordinates or window center. */
7188 regionize(struct ws_win
*win
, int x
, int y
)
7190 struct swm_region
*r
= NULL
;
7192 r
= region_under(win
->s
, x
, y
);
7194 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
7195 Y(win
) + HEIGHT(win
) / 2);
7197 if (r
&& r
!= win
->ws
->r
) {
7198 clear_maximized(r
->ws
);
7200 win_to_ws(win
, r
->ws
->idx
, false);
7202 /* Stack old and new region. */
7206 /* Set focus on new ws. */
7207 unfocus_win(r
->ws
->focus
);
7215 #define SWM_MOVE_STEPS (50)
7218 move_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
7220 struct swm_region
*r
;
7221 xcb_timestamp_t timestamp
= 0;
7222 xcb_query_pointer_reply_t
*qpr
= NULL
;
7223 xcb_generic_event_t
*evt
;
7224 xcb_motion_notify_event_t
*mne
;
7225 bool moving
, restack
= false, step
= false;
7230 if ((r
= win
->ws
->r
) == NULL
)
7233 if (FULLSCREEN(win
))
7236 DNPRINTF(SWM_D_EVENT
, "move: win %#x, floating: %s, transient: "
7237 "%#x\n", win
->id
, YESNO(ABOVE(win
)), win
->transient
);
7239 /* in max_stack mode should only move transients */
7240 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
7243 if (!(ABOVE(win
) || TRANS(win
)) || MAXIMIZED(win
)) {
7244 store_float_geom(win
);
7248 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
7250 ewmh_update_wm_state(win
);
7257 /* It's possible for win to have been freed during focus_flush(). */
7258 if (validate_win(win
)) {
7259 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7264 case SWM_ARG_ID_MOVELEFT
:
7265 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
7268 case SWM_ARG_ID_MOVERIGHT
:
7269 X(win
) += (SWM_MOVE_STEPS
- border_width
);
7272 case SWM_ARG_ID_MOVEUP
:
7273 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
7276 case SWM_ARG_ID_MOVEDOWN
:
7277 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
7284 regionize(win
, -1, -1);
7285 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
7287 store_float_geom(win
);
7291 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
7292 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
7293 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
7295 /* get cursor offset from window root */
7296 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
7299 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7303 /* Release keyboard freeze if called via keybind. */
7304 if (bp
->type
== KEYBIND
)
7305 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7308 regionize(win
, qpr
->root_x
, qpr
->root_y
);
7309 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7310 SWM_CW_SOFTBOUNDARY
);
7314 while (moving
&& (evt
= get_next_event(true))) {
7315 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
7316 case XCB_BUTTON_RELEASE
:
7317 if (bp
->type
== BTNBIND
&& bp
->value
==
7318 ((xcb_button_release_event_t
*)evt
)->detail
)
7321 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7322 ((xcb_button_release_event_t
*)evt
)->time
);
7325 case XCB_KEY_RELEASE
:
7326 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
7329 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7330 ((xcb_key_release_event_t
*)evt
)->time
);
7333 case XCB_MOTION_NOTIFY
:
7334 mne
= (xcb_motion_notify_event_t
*)evt
;
7335 DNPRINTF(SWM_D_EVENT
, "move: MOTION_NOTIFY: "
7336 "root: %#x\n", mne
->root
);
7337 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
7338 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
7340 /* not free, don't sync more than 120 times / second */
7341 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
7342 timestamp
= mne
->time
;
7343 regionize(win
, mne
->root_x
, mne
->root_y
);
7344 region_containment(win
, win
->ws
->r
,
7345 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
7350 case XCB_BUTTON_PRESS
:
7351 /* Thaw and ignore. */
7352 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7353 ((xcb_button_press_event_t
*)evt
)->time
);
7358 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7359 ((xcb_key_press_event_t
*)evt
)->time
);
7365 /* It's possible for win to have been freed. */
7366 if (validate_win(win
)) {
7367 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7375 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7376 SWM_CW_SOFTBOUNDARY
);
7380 store_float_geom(win
);
7382 /* New region set to fullscreen layout. */
7383 if (win
->ws
->r
&& win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
]) {
7390 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7391 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
7395 move(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
7397 struct ws_win
*win
= NULL
;
7398 xcb_query_pointer_reply_t
*qpr
= NULL
;
7404 /* move_* uses focus window. */
7408 /* Disallow move_ on tiled. */
7409 if (win
&& !(TRANS(win
) || ABOVE(win
)))
7412 /* move uses window under pointer. */
7413 qpr
= xcb_query_pointer_reply(conn
,
7414 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7416 win
= find_window(qpr
->child
);
7422 move_win(win
, bp
, args
->id
);
7424 if (args
->id
&& bp
->type
== KEYBIND
)
7430 /* action definitions */
7432 char name
[SWM_FUNCNAME_LEN
];
7433 void (*func
)(struct binding
*, struct swm_region
*,
7437 } actions
[FN_INVALID
+ 2] = {
7438 /* name function argument */
7439 { "bar_toggle", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
7440 { "bar_toggle_ws", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
7441 { "button2", pressbutton
, 0, {.id
= 2} },
7442 { "cycle_layout", cycle_layout
, 0, {0} },
7443 { "flip_layout", stack_config
, 0, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
7444 { "float_toggle", floating_toggle
,0, {0} },
7445 { "focus", focus_pointer
, 0, {0} },
7446 { "focus_main", focus
, 0, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
7447 { "focus_next", focus
, 0, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
7448 { "focus_prev", focus
, 0, {.id
= SWM_ARG_ID_FOCUSPREV
} },
7449 { "focus_urgent", focus
, 0, {.id
= SWM_ARG_ID_FOCUSURGENT
} },
7450 { "fullscreen_toggle", fullscreen_toggle
, 0, {0} },
7451 { "maximize_toggle", maximize_toggle
,0, {0} },
7452 { "height_grow", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
7453 { "height_shrink", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
7454 { "iconify", iconify
, 0, {0} },
7455 { "master_shrink", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
7456 { "master_grow", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERGROW
} },
7457 { "master_add", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERADD
} },
7458 { "master_del", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERDEL
} },
7459 { "move", move
, FN_F_NOREPLAY
, {0} },
7460 { "move_down", move
, 0, {.id
= SWM_ARG_ID_MOVEDOWN
} },
7461 { "move_left", move
, 0, {.id
= SWM_ARG_ID_MOVELEFT
} },
7462 { "move_right", move
, 0, {.id
= SWM_ARG_ID_MOVERIGHT
} },
7463 { "move_up", move
, 0, {.id
= SWM_ARG_ID_MOVEUP
} },
7464 { "mvrg_1", send_to_rg
, 0, {.id
= 0} },
7465 { "mvrg_2", send_to_rg
, 0, {.id
= 1} },
7466 { "mvrg_3", send_to_rg
, 0, {.id
= 2} },
7467 { "mvrg_4", send_to_rg
, 0, {.id
= 3} },
7468 { "mvrg_5", send_to_rg
, 0, {.id
= 4} },
7469 { "mvrg_6", send_to_rg
, 0, {.id
= 5} },
7470 { "mvrg_7", send_to_rg
, 0, {.id
= 6} },
7471 { "mvrg_8", send_to_rg
, 0, {.id
= 7} },
7472 { "mvrg_9", send_to_rg
, 0, {.id
= 8} },
7473 { "mvrg_next", send_to_rg_relative
, 0, {.id
= 1} },
7474 { "mvrg_prev", send_to_rg_relative
, 0, {.id
= -1} },
7475 { "mvws_1", send_to_ws
, 0, {.id
= 0} },
7476 { "mvws_2", send_to_ws
, 0, {.id
= 1} },
7477 { "mvws_3", send_to_ws
, 0, {.id
= 2} },
7478 { "mvws_4", send_to_ws
, 0, {.id
= 3} },
7479 { "mvws_5", send_to_ws
, 0, {.id
= 4} },
7480 { "mvws_6", send_to_ws
, 0, {.id
= 5} },
7481 { "mvws_7", send_to_ws
, 0, {.id
= 6} },
7482 { "mvws_8", send_to_ws
, 0, {.id
= 7} },
7483 { "mvws_9", send_to_ws
, 0, {.id
= 8} },
7484 { "mvws_10", send_to_ws
, 0, {.id
= 9} },
7485 { "mvws_11", send_to_ws
, 0, {.id
= 10} },
7486 { "mvws_12", send_to_ws
, 0, {.id
= 11} },
7487 { "mvws_13", send_to_ws
, 0, {.id
= 12} },
7488 { "mvws_14", send_to_ws
, 0, {.id
= 13} },
7489 { "mvws_15", send_to_ws
, 0, {.id
= 14} },
7490 { "mvws_16", send_to_ws
, 0, {.id
= 15} },
7491 { "mvws_17", send_to_ws
, 0, {.id
= 16} },
7492 { "mvws_18", send_to_ws
, 0, {.id
= 17} },
7493 { "mvws_19", send_to_ws
, 0, {.id
= 18} },
7494 { "mvws_20", send_to_ws
, 0, {.id
= 19} },
7495 { "mvws_21", send_to_ws
, 0, {.id
= 20} },
7496 { "mvws_22", send_to_ws
, 0, {.id
= 21} },
7497 { "name_workspace", name_workspace
, 0, {0} },
7498 { "quit", quit
, 0, {0} },
7499 { "raise", raise_focus
, 0, {0} },
7500 { "raise_toggle", raise_toggle
, 0, {0} },
7501 { "resize", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_DONTCENTER
} },
7502 { "resize_centered", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_CENTER
} },
7503 { "restart", restart
, 0, {0} },
7504 { "rg_1", focusrg
, 0, {.id
= 0} },
7505 { "rg_2", focusrg
, 0, {.id
= 1} },
7506 { "rg_3", focusrg
, 0, {.id
= 2} },
7507 { "rg_4", focusrg
, 0, {.id
= 3} },
7508 { "rg_5", focusrg
, 0, {.id
= 4} },
7509 { "rg_6", focusrg
, 0, {.id
= 5} },
7510 { "rg_7", focusrg
, 0, {.id
= 6} },
7511 { "rg_8", focusrg
, 0, {.id
= 7} },
7512 { "rg_9", focusrg
, 0, {.id
= 8} },
7513 { "rg_move_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_UP
} },
7514 { "rg_move_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_DOWN
} },
7515 { "rg_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7516 { "rg_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7517 { "screen_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7518 { "screen_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7519 { "search_win", search_win
, 0, {0} },
7520 { "search_workspace", search_workspace
, 0, {0} },
7521 { "spawn_custom", NULL
, 0, {0} },
7522 { "stack_balance", stack_config
, 0, {.id
= SWM_ARG_ID_STACKBALANCE
} },
7523 { "stack_inc", stack_config
, 0, {.id
= SWM_ARG_ID_STACKINC
} },
7524 { "stack_dec", stack_config
, 0, {.id
= SWM_ARG_ID_STACKDEC
} },
7525 { "stack_reset", stack_config
, 0, {.id
= SWM_ARG_ID_STACKRESET
} },
7526 { "swap_main", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPMAIN
} },
7527 { "swap_next", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPNEXT
} },
7528 { "swap_prev", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPPREV
} },
7529 { "uniconify", uniconify
, 0, {0} },
7530 { "version", version
, 0, {0} },
7531 { "width_grow", resize
, 0, {.id
= SWM_ARG_ID_WIDTHGROW
} },
7532 { "width_shrink", resize
, 0, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
7533 { "wind_del", wkill
, 0, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
7534 { "wind_kill", wkill
, 0, {.id
= SWM_ARG_ID_KILLWINDOW
} },
7535 { "ws_1", switchws
, 0, {.id
= 0} },
7536 { "ws_2", switchws
, 0, {.id
= 1} },
7537 { "ws_3", switchws
, 0, {.id
= 2} },
7538 { "ws_4", switchws
, 0, {.id
= 3} },
7539 { "ws_5", switchws
, 0, {.id
= 4} },
7540 { "ws_6", switchws
, 0, {.id
= 5} },
7541 { "ws_7", switchws
, 0, {.id
= 6} },
7542 { "ws_8", switchws
, 0, {.id
= 7} },
7543 { "ws_9", switchws
, 0, {.id
= 8} },
7544 { "ws_10", switchws
, 0, {.id
= 9} },
7545 { "ws_11", switchws
, 0, {.id
= 10} },
7546 { "ws_12", switchws
, 0, {.id
= 11} },
7547 { "ws_13", switchws
, 0, {.id
= 12} },
7548 { "ws_14", switchws
, 0, {.id
= 13} },
7549 { "ws_15", switchws
, 0, {.id
= 14} },
7550 { "ws_16", switchws
, 0, {.id
= 15} },
7551 { "ws_17", switchws
, 0, {.id
= 16} },
7552 { "ws_18", switchws
, 0, {.id
= 17} },
7553 { "ws_19", switchws
, 0, {.id
= 18} },
7554 { "ws_20", switchws
, 0, {.id
= 19} },
7555 { "ws_21", switchws
, 0, {.id
= 20} },
7556 { "ws_22", switchws
, 0, {.id
= 21} },
7557 { "ws_next", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
7558 { "ws_next_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
7559 { "ws_next_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
7560 { "ws_prev", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
7561 { "ws_prev_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
7562 { "ws_prev_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
7563 { "ws_prior", priorws
, 0, {0} },
7564 /* SWM_DEBUG actions MUST be here: */
7565 { "debug_toggle", debug_toggle
, 0, {0} },
7566 { "dumpwins", dumpwins
, 0, {0} },
7568 { "invalid action", NULL
, 0, {0} },
7572 update_modkey(uint16_t mod
)
7576 /* Replace all instances of the old mod key. */
7577 RB_FOREACH(bp
, binding_tree
, &bindings
)
7578 if (bp
->mod
& mod_key
)
7579 bp
->mod
= (bp
->mod
& ~mod_key
) | mod
;
7584 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7587 struct spawn_prog
*prog
= NULL
;
7589 char *ap
, **real_args
;
7591 /* suppress unused warning since var is needed */
7594 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
7597 TAILQ_FOREACH(prog
, &spawns
, entry
) {
7598 if (strcasecmp(spawn_name
, prog
->name
) == 0)
7602 warnx("spawn_custom: program %s not found", spawn_name
);
7606 /* make room for expanded args */
7607 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
7608 err(1, "spawn_custom: calloc real_args");
7610 /* expand spawn_args into real_args */
7611 for (i
= c
= 0; i
< prog
->argc
; i
++) {
7613 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
7614 if (strcasecmp(ap
, "$bar_border") == 0) {
7616 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
7618 err(1, "spawn_custom border color");
7619 } else if (strcasecmp(ap
, "$bar_color") == 0) {
7621 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
7623 err(1, "spawn_custom bar color");
7624 } else if (strcasecmp(ap
, "$bar_font") == 0) {
7625 if ((real_args
[c
] = strdup(bar_fonts
))
7627 err(1, "spawn_custom bar fonts");
7628 } else if (strcasecmp(ap
, "$bar_font_color") == 0) {
7630 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
7632 err(1, "spawn_custom color font");
7633 } else if (strcasecmp(ap
, "$color_focus") == 0) {
7635 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
7637 err(1, "spawn_custom color focus");
7638 } else if (strcasecmp(ap
, "$color_focus_maximized") == 0) {
7640 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].name
))
7642 err(1, "spawn_custom color focus maximized");
7643 } else if (strcasecmp(ap
, "$color_unfocus") == 0) {
7645 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
7647 err(1, "spawn_custom color unfocus");
7648 } else if (strcasecmp(ap
, "$color_unfocus_maximized") == 0) {
7650 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].name
))
7652 err(1, "spawn_custom color unfocus maximized");
7653 } else if (strcasecmp(ap
, "$region_index") == 0) {
7654 if (asprintf(&real_args
[c
], "%d",
7655 get_region_index(r
) + 1) < 1)
7656 err(1, "spawn_custom region index");
7657 } else if (strcasecmp(ap
, "$workspace_index") == 0) {
7658 if (asprintf(&real_args
[c
], "%d", r
->ws
->idx
+ 1) < 1)
7659 err(1, "spawn_custom workspace index");
7660 } else if (strcasecmp(ap
, "$dmenu_bottom") == 0) {
7663 if ((real_args
[c
] = strdup("-b")) == NULL
)
7664 err(1, "spawn_custom workspace index");
7666 /* no match --> copy as is */
7667 if ((real_args
[c
] = strdup(ap
)) == NULL
)
7668 err(1, "spawn_custom strdup(ap)");
7670 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
7676 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
7677 for (i
= 0; i
< c
; ++i
)
7678 DPRINTF("\"%s\" ", real_args
[i
]);
7681 *ret_args
= real_args
;
7686 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
7692 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7696 spawn(r
->ws
->idx
, &a
, true);
7698 for (i
= 0; i
< spawn_argc
; i
++)
7704 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7711 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7715 if (pipe(select_list_pipe
) == -1)
7716 err(1, "pipe error");
7717 if (pipe(select_resp_pipe
) == -1)
7718 err(1, "pipe error");
7720 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
7721 err(1, "could not disable SIGPIPE");
7722 switch (*pid
= fork()) {
7724 err(1, "cannot fork");
7727 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
7729 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
7731 close(select_list_pipe
[1]);
7732 close(select_resp_pipe
[0]);
7733 spawn(r
->ws
->idx
, &a
, false);
7735 default: /* parent */
7736 close(select_list_pipe
[0]);
7737 close(select_resp_pipe
[1]);
7741 for (i
= 0; i
< spawn_argc
; i
++)
7746 /* Argument tokenizer. */
7749 char *arg
, *cp
, *next
;
7750 bool single_quoted
= false, double_quoted
= false;
7755 /* Eat and move characters until end of argument is found. */
7756 for (arg
= next
= cp
= *sp
; *cp
!= '\0'; ++cp
) {
7757 if (!double_quoted
&& *cp
== '\'') {
7758 /* Eat single-quote. */
7759 single_quoted
= !single_quoted
;
7760 } else if (!single_quoted
&& *cp
== '"') {
7761 /* Eat double-quote. */
7762 double_quoted
= !double_quoted
;
7763 } else if (!single_quoted
&& *cp
== '\\' && *(cp
+ 1) == '"') {
7764 /* Eat backslash; copy escaped character to arg. */
7766 } else if (!single_quoted
&& !double_quoted
&& *cp
== '\\' &&
7767 (*(cp
+ 1) == '\'' || *(cp
+ 1) == ' ')) {
7768 /* Eat backslash; move escaped character. */
7770 } else if (!single_quoted
&& !double_quoted
&&
7771 (*cp
== ' ' || *cp
== '\t')) {
7772 /* Terminate argument. */
7774 /* Point sp to beginning of next argument. */
7778 /* Move regular character. */
7783 /* Terminate argument if end of string. */
7793 spawn_insert(const char *name
, const char *args
, int flags
)
7795 struct spawn_prog
*sp
;
7796 char *arg
, *cp
, *ptr
;
7798 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s[%s]\n", name
, args
);
7800 if (args
== NULL
|| *args
== '\0')
7803 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
7804 err(1, "spawn_insert: calloc");
7805 if ((sp
->name
= strdup(name
)) == NULL
)
7806 err(1, "spawn_insert: strdup");
7808 /* Convert the arguments to an argument list. */
7809 if ((ptr
= cp
= strdup(args
)) == NULL
)
7810 err(1, "spawn_insert: strdup");
7811 while ((arg
= argsep(&ptr
)) != NULL
) {
7812 /* Null argument; skip it. */
7817 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
7818 sizeof *sp
->argv
)) == NULL
)
7819 err(1, "spawn_insert: realloc");
7820 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
7821 err(1, "spawn_insert: strdup");
7827 DNPRINTF(SWM_D_SPAWN
, "arg %d: [%s]\n", sp
->argc
, sp
->argv
[sp
->argc
-1]);
7828 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
7829 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
7833 spawn_remove(struct spawn_prog
*sp
)
7837 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
7839 TAILQ_REMOVE(&spawns
, sp
, entry
);
7840 for (i
= 0; i
< sp
->argc
; i
++)
7846 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
7852 struct spawn_prog
*sp
;
7854 while ((sp
= TAILQ_FIRST(&spawns
)) != NULL
) {
7860 spawn_find(const char *name
)
7862 struct spawn_prog
*sp
;
7864 TAILQ_FOREACH(sp
, &spawns
, entry
)
7865 if (strcasecmp(sp
->name
, name
) == 0)
7872 setspawn(const char *name
, const char *args
, int flags
)
7874 struct spawn_prog
*sp
;
7876 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
7881 /* Remove any old spawn under the same name. */
7882 if ((sp
= spawn_find(name
)) != NULL
)
7886 spawn_insert(name
, args
, flags
);
7888 warnx("error: setspawn: cannot find program: %s", name
);
7890 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
7894 setconfspawn(const char *selector
, const char *value
, int flags
)
7898 if (selector
== NULL
|| strlen(selector
) == 0)
7901 args
= expand_tilde(value
);
7903 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
7905 setspawn(selector
, args
, flags
);
7908 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
7913 validate_spawns(void)
7916 struct spawn_prog
*sp
;
7917 char which
[PATH_MAX
];
7920 RB_FOREACH(bp
, binding_tree
, &bindings
) {
7921 if (bp
->action
!= FN_SPAWN_CUSTOM
)
7925 sp
= spawn_find(bp
->spawn_name
);
7926 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
7929 /* verify we have the goods */
7930 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
7931 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
7933 for (i
= strlen("which "); i
< strlen(which
); i
++)
7934 if (which
[i
] == ' ') {
7938 if (system(which
) != 0)
7939 add_startup_exception("could not find %s",
7940 &which
[strlen("which ")]);
7947 setconfspawn("lock", "xlock", 0);
7949 setconfspawn("term", "xterm", 0);
7950 setconfspawn("spawn_term", "xterm", 0);
7952 setconfspawn("menu", "dmenu_run"
7956 " -nf $bar_font_color"
7958 " -sf $bar_color", 0);
7960 setconfspawn("search", "dmenu"
7965 " -nf $bar_font_color"
7967 " -sf $bar_color", 0);
7969 setconfspawn("name_workspace", "dmenu"
7974 " -nf $bar_font_color"
7976 " -sf $bar_color", 0);
7978 /* These are not verified for existence, even with a binding set. */
7979 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
7980 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
7981 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
7985 #define SWM_MODNAME_SIZE 32
7986 #define SWM_KEY_WS "\n+ \t"
7988 parsebinding(const char *bindstr
, uint16_t *mod
, enum binding_type
*type
,
7989 uint32_t *val
, uint32_t *flags
)
7991 char *str
, *cp
, *name
;
7992 KeySym ks
, lks
, uks
;
7994 DNPRINTF(SWM_D_KEY
, "parsebinding: enter [%s]\n", bindstr
);
7995 if (mod
== NULL
|| val
== NULL
) {
7996 DNPRINTF(SWM_D_KEY
, "parsebinding: no mod or key vars\n");
7999 if (bindstr
== NULL
|| strlen(bindstr
) == 0) {
8000 DNPRINTF(SWM_D_KEY
, "parsebinding: no bindstr\n");
8004 if ((cp
= str
= strdup(bindstr
)) == NULL
)
8005 err(1, "parsebinding: strdup");
8007 *val
= XCB_NO_SYMBOL
;
8011 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
8012 DNPRINTF(SWM_D_KEY
, "parsebinding: entry [%s]\n", name
);
8014 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
8015 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
8017 else if (strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
) == 0)
8018 *mod
|= XCB_MOD_MASK_1
;
8019 else if (strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
) == 0)
8020 *mod
|= XCB_MOD_MASK_2
;
8021 else if (strncmp(name
, "Mod3", SWM_MODNAME_SIZE
) == 0)
8022 *mod
|= XCB_MOD_MASK_3
;
8023 else if (strncmp(name
, "Mod4", SWM_MODNAME_SIZE
) == 0)
8024 *mod
|= XCB_MOD_MASK_4
;
8025 else if (strncmp(name
, "Mod5", SWM_MODNAME_SIZE
) == 0)
8026 *mod
|= XCB_MOD_MASK_5
;
8027 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
8028 *mod
|= XCB_MOD_MASK_SHIFT
;
8029 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
8030 *mod
|= XCB_MOD_MASK_CONTROL
;
8031 else if (strncasecmp(name
, "ANYMOD", SWM_MODNAME_SIZE
) == 0)
8032 *mod
|= XCB_MOD_MASK_ANY
;
8033 else if (strncasecmp(name
, "REPLAY", SWM_MODNAME_SIZE
) == 0)
8034 *flags
|= BINDING_F_REPLAY
;
8035 else if (sscanf(name
, "Button%u", val
) == 1) {
8036 DNPRINTF(SWM_D_KEY
, "parsebinding: button %u\n", *val
);
8038 if (*val
> 255 || *val
== 0) {
8040 "parsebinding: invalid button %u\n", *val
);
8044 /* TODO: do this without Xlib. */
8045 ks
= XStringToKeysym(name
);
8046 if (ks
== NoSymbol
) {
8048 "parsebinding: invalid key %s\n", name
);
8053 XConvertCase(ks
, &lks
, &uks
);
8054 *val
= (uint32_t)lks
;
8058 /* If ANYMOD was specified, ignore the rest. */
8059 if (*mod
& XCB_MOD_MASK_ANY
)
8060 *mod
= XCB_MOD_MASK_ANY
;
8063 DNPRINTF(SWM_D_KEY
, "parsebinding: leave ok\n");
8068 strdupsafe(const char *str
)
8073 return (strdup(str
));
8077 binding_cmp(struct binding
*bp1
, struct binding
*bp2
)
8079 if (bp1
->type
< bp2
->type
)
8081 if (bp1
->type
> bp2
->type
)
8084 if (bp1
->value
< bp2
->value
)
8086 if (bp1
->value
> bp2
->value
)
8089 if (bp1
->mod
< bp2
->mod
)
8091 if (bp1
->mod
> bp2
->mod
)
8098 binding_insert(uint16_t mod
, enum binding_type type
, uint32_t val
,
8099 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
8103 DNPRINTF(SWM_D_KEY
, "binding_insert: mod: %u, type: %d, val: %u, "
8104 "action: %s(%d), spawn_name: %s\n", mod
, type
, val
,
8105 actions
[aid
].name
, aid
, spawn_name
);
8107 if ((bp
= malloc(sizeof *bp
)) == NULL
)
8108 err(1, "binding_insert: malloc");
8115 bp
->spawn_name
= strdupsafe(spawn_name
);
8116 RB_INSERT(binding_tree
, &bindings
, bp
);
8118 DNPRINTF(SWM_D_KEY
, "binding_insert: leave\n");
8122 binding_lookup(uint16_t mod
, enum binding_type type
, uint32_t val
)
8130 return (RB_FIND(binding_tree
, &bindings
, &bp
));
8134 binding_remove(struct binding
*bp
)
8136 DNPRINTF(SWM_D_KEY
, "binding_remove: mod: %u, type: %d, val: %u, "
8137 "action: %s(%d), spawn_name: %s\n", bp
->mod
, bp
->type
, bp
->value
,
8138 actions
[bp
->action
].name
, bp
->action
, bp
->spawn_name
);
8140 RB_REMOVE(binding_tree
, &bindings
, bp
);
8141 free(bp
->spawn_name
);
8144 DNPRINTF(SWM_D_KEY
, "binding_remove: leave\n");
8148 setbinding(uint16_t mod
, enum binding_type type
, uint32_t val
,
8149 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
8153 DNPRINTF(SWM_D_KEY
, "setbinding: enter %s [%s]\n",
8154 actions
[aid
].name
, spawn_name
);
8156 /* Unbind any existing. Loop is to handle MOD_MASK_ANY. */
8157 while ((bp
= binding_lookup(mod
, type
, val
)))
8160 if (aid
!= FN_INVALID
)
8161 binding_insert(mod
, type
, val
, aid
, flags
, spawn_name
);
8163 DNPRINTF(SWM_D_KEY
, "setbinding: leave\n");
8167 setconfbinding(const char *selector
, const char *value
, int flags
)
8169 struct spawn_prog
*sp
;
8170 uint32_t keybtn
, opts
;
8173 enum binding_type type
;
8175 /* suppress unused warning since var is needed */
8178 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter selector: [%s], "
8179 "value: [%s]\n", selector
, value
);
8180 if (selector
== NULL
|| strlen(selector
) == 0) {
8181 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
8182 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8183 setbinding(mod
, type
, keybtn
, FN_INVALID
, opts
, NULL
);
8188 /* search by key function name */
8189 for (aid
= 0; aid
< FN_INVALID
; aid
++) {
8190 if (strncasecmp(selector
, actions
[aid
].name
,
8191 SWM_FUNCNAME_LEN
) == 0) {
8192 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8193 "action\n", selector
);
8194 if (parsebinding(value
, &mod
, &type
, &keybtn
,
8196 setbinding(mod
, type
, keybtn
, aid
, opts
, NULL
);
8202 /* search by custom spawn name */
8203 if ((sp
= spawn_find(selector
)) != NULL
) {
8204 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8205 "spawn\n", selector
);
8206 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8207 setbinding(mod
, type
, keybtn
, FN_SPAWN_CUSTOM
, opts
,
8213 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
8217 #define MODSHIFT MODKEY | XCB_MOD_MASK_SHIFT
8219 setup_keybindings(void)
8221 #define BINDKEY(m, k, a) setbinding(m, KEYBIND, k, a, 0, NULL)
8222 #define BINDKEYSPAWN(m, k, s) setbinding(m, KEYBIND, k, FN_SPAWN_CUSTOM, 0, s)
8223 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8224 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8225 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8226 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8227 BINDKEY(MODKEY
, XK_v
, FN_BUTTON2
);
8228 BINDKEY(MODKEY
, XK_space
, FN_CYCLE_LAYOUT
);
8229 BINDKEY(MODSHIFT
, XK_backslash
, FN_FLIP_LAYOUT
);
8230 BINDKEY(MODKEY
, XK_t
, FN_FLOAT_TOGGLE
);
8231 BINDKEY(MODKEY
, XK_m
, FN_FOCUS_MAIN
);
8232 BINDKEY(MODKEY
, XK_j
, FN_FOCUS_NEXT
);
8233 BINDKEY(MODKEY
, XK_Tab
, FN_FOCUS_NEXT
);
8234 BINDKEY(MODKEY
, XK_k
, FN_FOCUS_PREV
);
8235 BINDKEY(MODSHIFT
, XK_Tab
, FN_FOCUS_PREV
);
8236 BINDKEY(MODKEY
, XK_u
, FN_FOCUS_URGENT
);
8237 BINDKEY(MODSHIFT
, XK_e
, FN_FULLSCREEN_TOGGLE
);
8238 BINDKEY(MODKEY
, XK_e
, FN_MAXIMIZE_TOGGLE
);
8239 BINDKEY(MODSHIFT
, XK_equal
, FN_HEIGHT_GROW
);
8240 BINDKEY(MODSHIFT
, XK_minus
, FN_HEIGHT_SHRINK
);
8241 BINDKEY(MODKEY
, XK_w
, FN_ICONIFY
);
8242 BINDKEY(MODKEY
, XK_h
, FN_MASTER_SHRINK
);
8243 BINDKEY(MODKEY
, XK_l
, FN_MASTER_GROW
);
8244 BINDKEY(MODKEY
, XK_comma
, FN_MASTER_ADD
);
8245 BINDKEY(MODKEY
, XK_period
, FN_MASTER_DEL
);
8246 BINDKEY(MODSHIFT
, XK_bracketright
, FN_MOVE_DOWN
);
8247 BINDKEY(MODKEY
, XK_bracketleft
, FN_MOVE_LEFT
);
8248 BINDKEY(MODKEY
, XK_bracketright
, FN_MOVE_RIGHT
);
8249 BINDKEY(MODSHIFT
, XK_bracketleft
, FN_MOVE_UP
);
8250 BINDKEY(MODSHIFT
, XK_KP_End
, FN_MVRG_1
);
8251 BINDKEY(MODSHIFT
, XK_KP_Down
, FN_MVRG_2
);
8252 BINDKEY(MODSHIFT
, XK_KP_Next
, FN_MVRG_3
);
8253 BINDKEY(MODSHIFT
, XK_KP_Left
, FN_MVRG_4
);
8254 BINDKEY(MODSHIFT
, XK_KP_Begin
, FN_MVRG_5
);
8255 BINDKEY(MODSHIFT
, XK_KP_Right
, FN_MVRG_6
);
8256 BINDKEY(MODSHIFT
, XK_KP_Home
, FN_MVRG_7
);
8257 BINDKEY(MODSHIFT
, XK_KP_Up
, FN_MVRG_8
);
8258 BINDKEY(MODSHIFT
, XK_KP_Prior
, FN_MVRG_9
);
8259 BINDKEY(MODSHIFT
, XK_1
, FN_MVWS_1
);
8260 BINDKEY(MODSHIFT
, XK_2
, FN_MVWS_2
);
8261 BINDKEY(MODSHIFT
, XK_3
, FN_MVWS_3
);
8262 BINDKEY(MODSHIFT
, XK_4
, FN_MVWS_4
);
8263 BINDKEY(MODSHIFT
, XK_5
, FN_MVWS_5
);
8264 BINDKEY(MODSHIFT
, XK_6
, FN_MVWS_6
);
8265 BINDKEY(MODSHIFT
, XK_7
, FN_MVWS_7
);
8266 BINDKEY(MODSHIFT
, XK_8
, FN_MVWS_8
);
8267 BINDKEY(MODSHIFT
, XK_9
, FN_MVWS_9
);
8268 BINDKEY(MODSHIFT
, XK_0
, FN_MVWS_10
);
8269 BINDKEY(MODSHIFT
, XK_F1
, FN_MVWS_11
);
8270 BINDKEY(MODSHIFT
, XK_F2
, FN_MVWS_12
);
8271 BINDKEY(MODSHIFT
, XK_F3
, FN_MVWS_13
);
8272 BINDKEY(MODSHIFT
, XK_F4
, FN_MVWS_14
);
8273 BINDKEY(MODSHIFT
, XK_F5
, FN_MVWS_15
);
8274 BINDKEY(MODSHIFT
, XK_F6
, FN_MVWS_16
);
8275 BINDKEY(MODSHIFT
, XK_F7
, FN_MVWS_17
);
8276 BINDKEY(MODSHIFT
, XK_F8
, FN_MVWS_18
);
8277 BINDKEY(MODSHIFT
, XK_F9
, FN_MVWS_19
);
8278 BINDKEY(MODSHIFT
, XK_F10
, FN_MVWS_20
);
8279 BINDKEY(MODSHIFT
, XK_F11
, FN_MVWS_21
);
8280 BINDKEY(MODSHIFT
, XK_F12
, FN_MVWS_22
);
8281 BINDKEY(MODSHIFT
, XK_slash
, FN_NAME_WORKSPACE
);
8282 BINDKEY(MODSHIFT
, XK_q
, FN_QUIT
);
8283 BINDKEY(MODKEY
, XK_r
, FN_RAISE
);
8284 BINDKEY(MODSHIFT
, XK_r
, FN_RAISE_TOGGLE
);
8285 BINDKEY(MODKEY
, XK_q
, FN_RESTART
);
8286 BINDKEY(MODKEY
, XK_KP_End
, FN_RG_1
);
8287 BINDKEY(MODKEY
, XK_KP_Down
, FN_RG_2
);
8288 BINDKEY(MODKEY
, XK_KP_Next
, FN_RG_3
);
8289 BINDKEY(MODKEY
, XK_KP_Left
, FN_RG_4
);
8290 BINDKEY(MODKEY
, XK_KP_Begin
, FN_RG_5
);
8291 BINDKEY(MODKEY
, XK_KP_Right
, FN_RG_6
);
8292 BINDKEY(MODKEY
, XK_KP_Home
, FN_RG_7
);
8293 BINDKEY(MODKEY
, XK_KP_Up
, FN_RG_8
);
8294 BINDKEY(MODKEY
, XK_KP_Prior
, FN_RG_9
);
8295 BINDKEY(MODSHIFT
, XK_Right
, FN_RG_NEXT
);
8296 BINDKEY(MODSHIFT
, XK_Left
, FN_RG_PREV
);
8297 BINDKEY(MODKEY
, XK_f
, FN_SEARCH_WIN
);
8298 BINDKEY(MODKEY
, XK_slash
, FN_SEARCH_WORKSPACE
);
8299 BINDKEYSPAWN(MODSHIFT
, XK_i
, "initscr");
8300 BINDKEYSPAWN(MODSHIFT
, XK_Delete
, "lock");
8301 BINDKEYSPAWN(MODKEY
, XK_p
, "menu");
8302 BINDKEYSPAWN(MODKEY
, XK_s
, "screenshot_all");
8303 BINDKEYSPAWN(MODSHIFT
, XK_s
, "screenshot_wind");
8304 BINDKEYSPAWN(MODSHIFT
, XK_Return
, "term");
8305 BINDKEY(MODSHIFT
, XK_comma
, FN_STACK_INC
);
8306 BINDKEY(MODSHIFT
, XK_period
, FN_STACK_DEC
);
8307 BINDKEY(MODSHIFT
, XK_space
, FN_STACK_RESET
);
8308 BINDKEY(MODKEY
, XK_Return
, FN_SWAP_MAIN
);
8309 BINDKEY(MODSHIFT
, XK_j
, FN_SWAP_NEXT
);
8310 BINDKEY(MODSHIFT
, XK_k
, FN_SWAP_PREV
);
8311 BINDKEY(MODSHIFT
, XK_w
, FN_UNICONIFY
);
8312 BINDKEY(MODSHIFT
, XK_v
, FN_VERSION
);
8313 BINDKEY(MODKEY
, XK_equal
, FN_WIDTH_GROW
);
8314 BINDKEY(MODKEY
, XK_minus
, FN_WIDTH_SHRINK
);
8315 BINDKEY(MODKEY
, XK_x
, FN_WIND_DEL
);
8316 BINDKEY(MODSHIFT
, XK_x
, FN_WIND_KILL
);
8317 BINDKEY(MODKEY
, XK_1
, FN_WS_1
);
8318 BINDKEY(MODKEY
, XK_2
, FN_WS_2
);
8319 BINDKEY(MODKEY
, XK_3
, FN_WS_3
);
8320 BINDKEY(MODKEY
, XK_4
, FN_WS_4
);
8321 BINDKEY(MODKEY
, XK_5
, FN_WS_5
);
8322 BINDKEY(MODKEY
, XK_6
, FN_WS_6
);
8323 BINDKEY(MODKEY
, XK_7
, FN_WS_7
);
8324 BINDKEY(MODKEY
, XK_8
, FN_WS_8
);
8325 BINDKEY(MODKEY
, XK_9
, FN_WS_9
);
8326 BINDKEY(MODKEY
, XK_0
, FN_WS_10
);
8327 BINDKEY(MODKEY
, XK_F1
, FN_WS_11
);
8328 BINDKEY(MODKEY
, XK_F2
, FN_WS_12
);
8329 BINDKEY(MODKEY
, XK_F3
, FN_WS_13
);
8330 BINDKEY(MODKEY
, XK_F4
, FN_WS_14
);
8331 BINDKEY(MODKEY
, XK_F5
, FN_WS_15
);
8332 BINDKEY(MODKEY
, XK_F6
, FN_WS_16
);
8333 BINDKEY(MODKEY
, XK_F7
, FN_WS_17
);
8334 BINDKEY(MODKEY
, XK_F8
, FN_WS_18
);
8335 BINDKEY(MODKEY
, XK_F9
, FN_WS_19
);
8336 BINDKEY(MODKEY
, XK_F10
, FN_WS_20
);
8337 BINDKEY(MODKEY
, XK_F11
, FN_WS_21
);
8338 BINDKEY(MODKEY
, XK_F12
, FN_WS_22
);
8339 BINDKEY(MODKEY
, XK_Right
, FN_WS_NEXT
);
8340 BINDKEY(MODKEY
, XK_Left
, FN_WS_PREV
);
8341 BINDKEY(MODKEY
, XK_Up
, FN_WS_NEXT_ALL
);
8342 BINDKEY(MODKEY
, XK_Down
, FN_WS_PREV_ALL
);
8343 BINDKEY(MODSHIFT
, XK_Up
, FN_WS_NEXT_MOVE
);
8344 BINDKEY(MODSHIFT
, XK_Down
, FN_WS_PREV_MOVE
);
8345 BINDKEY(MODKEY
, XK_a
, FN_WS_PRIOR
);
8347 BINDKEY(MODKEY
, XK_d
, FN_DEBUG_TOGGLE
);
8348 BINDKEY(MODSHIFT
, XK_d
, FN_DUMPWINS
);
8355 setup_btnbindings(void)
8357 setbinding(ANYMOD
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_FOCUS
,
8358 BINDING_F_REPLAY
, NULL
);
8359 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE
, 0, NULL
);
8360 setbinding(MODSHIFT
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE_CENTERED
, 0,
8362 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_MOVE
, 0, NULL
);
8367 clear_bindings(void)
8371 while ((bp
= RB_ROOT(&bindings
)))
8376 clear_keybindings(void)
8378 struct binding
*bp
, *bptmp
;
8380 RB_FOREACH_SAFE(bp
, binding_tree
, &bindings
, bptmp
) {
8381 if (bp
->type
!= KEYBIND
)
8388 setkeymapping(const char *selector
, const char *value
, int flags
)
8390 char *keymapping_file
;
8392 /* suppress unused warnings since vars are needed */
8396 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
8398 keymapping_file
= expand_tilde(value
);
8400 clear_keybindings();
8401 /* load new key bindings; if it fails, revert to default bindings */
8402 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
8403 clear_keybindings();
8404 setup_keybindings();
8407 free(keymapping_file
);
8409 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
8414 updatenumlockmask(void)
8417 xcb_get_modifier_mapping_reply_t
*modmap_r
;
8418 xcb_keycode_t
*modmap
, kc
, *keycode
;
8422 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
8423 xcb_get_modifier_mapping(conn
),
8426 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
8427 for (i
= 0; i
< 8; i
++) {
8428 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
8429 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
8431 keycode
= xcb_key_symbols_get_keycode(syms
,
8435 numlockmask
= (1 << i
);
8442 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
8449 int num_screens
, k
, j
;
8450 uint16_t modifiers
[4];
8451 xcb_keycode_t
*code
;
8453 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
8454 updatenumlockmask();
8457 modifiers
[1] = numlockmask
;
8458 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8459 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8461 num_screens
= get_screen_count();
8462 for (k
= 0; k
< num_screens
; k
++) {
8463 if (TAILQ_EMPTY(&screens
[k
].rl
))
8465 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
8467 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8468 if (bp
->type
!= KEYBIND
)
8471 /* If there is a catch-all, only bind that. */
8472 if ((binding_lookup(ANYMOD
, KEYBIND
, bp
->value
)) &&
8476 /* Skip unused ws binds. */
8477 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8478 bp
->action
<= FN_WS_22
)
8481 /* Skip unused mvws binds. */
8482 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8483 bp
->action
<= FN_MVWS_22
)
8486 if ((code
= xcb_key_symbols_get_keycode(syms
,
8487 bp
->value
)) == NULL
)
8490 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8491 /* All modifiers are grabbed in one pass. */
8492 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8493 "key: %u, modifiers: %d\n", bp
->value
,
8495 xcb_grab_key(conn
, 1, screens
[k
].root
,
8496 bp
->mod
, *code
, XCB_GRAB_MODE_ASYNC
,
8497 XCB_GRAB_MODE_SYNC
);
8499 /* Need to grab each modifier permutation. */
8500 for (j
= 0; j
< LENGTH(modifiers
); j
++) {
8501 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8502 "key: %u, modifiers: %d\n",
8503 bp
->value
, bp
->mod
| modifiers
[j
]);
8504 xcb_grab_key(conn
, 1,
8506 bp
->mod
| modifiers
[j
],
8507 *code
, XCB_GRAB_MODE_ASYNC
,
8508 XCB_GRAB_MODE_SYNC
);
8520 int num_screens
, i
, k
;
8521 uint16_t modifiers
[4];
8523 DNPRINTF(SWM_D_MOUSE
, "grabbuttons\n");
8524 updatenumlockmask();
8527 modifiers
[1] = numlockmask
;
8528 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8529 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8531 num_screens
= get_screen_count();
8532 for (k
= 0; k
< num_screens
; k
++) {
8533 if (TAILQ_EMPTY(&screens
[k
].rl
))
8535 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, screens
[k
].root
,
8537 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8538 if (bp
->type
!= BTNBIND
)
8541 /* If there is a catch-all, only bind that. */
8542 if ((binding_lookup(ANYMOD
, BTNBIND
, bp
->value
)) &&
8546 /* Skip unused ws binds. */
8547 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8548 bp
->action
<= FN_WS_22
)
8551 /* Skip unused mvws binds. */
8552 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8553 bp
->action
<= FN_MVWS_22
)
8556 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8557 /* All modifiers are grabbed in one pass. */
8558 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: grab, "
8559 "button: %u, modifiers: %d\n", bp
->value
,
8561 xcb_grab_button(conn
, 0, screens
[k
].root
,
8562 BUTTONMASK
, XCB_GRAB_MODE_SYNC
,
8563 XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
8564 XCB_CURSOR_NONE
, bp
->value
, bp
->mod
);
8566 /* Need to grab each modifier permutation. */
8567 for (i
= 0; i
< LENGTH(modifiers
); ++i
) {
8568 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: "
8569 "grab, button: %u, modifiers: %u\n",
8570 bp
->value
, bp
->mod
| modifiers
[i
]);
8571 xcb_grab_button(conn
, 0,
8572 screens
[k
].root
, BUTTONMASK
,
8574 XCB_GRAB_MODE_ASYNC
,
8576 XCB_CURSOR_NONE
, bp
->value
,
8577 bp
->mod
| modifiers
[i
]);
8584 const char *quirkname
[] = {
8585 "NONE", /* config string for "no value" */
8593 "FOCUSONMAP_SINGLE",
8601 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
8602 #define SWM_Q_DELIM "\n|+ \t"
8604 parsequirks(const char *qstr
, uint32_t *quirk
, int *ws
)
8606 char *str
, *cp
, *name
;
8609 if (quirk
== NULL
|| qstr
== NULL
)
8612 if ((cp
= str
= strdup(qstr
)) == NULL
)
8613 err(1, "parsequirks: strdup");
8616 while ((name
= strsep(&cp
, SWM_Q_DELIM
)) != NULL
) {
8618 cp
+= (long)strspn(cp
, SWM_Q_DELIM
);
8620 if (sscanf(name
, "WS[%d]", ws
) == 1) {
8626 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
8627 if (strncasecmp(name
, quirkname
[i
],
8628 SWM_QUIRK_LEN
) == 0) {
8629 DNPRINTF(SWM_D_QUIRK
,
8630 "parsequirks: %s\n", name
);
8636 *quirk
|= 1 << (i
-1);
8640 if (i
>= LENGTH(quirkname
)) {
8641 DNPRINTF(SWM_D_QUIRK
,
8642 "parsequirks: invalid quirk [%s]\n", name
);
8653 quirk_insert(const char *class, const char *instance
, const char *name
,
8654 uint32_t quirk
, int ws
)
8658 bool failed
= false;
8660 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: class: %s, instance: %s, name: %s,"
8661 " value: %u, ws: %d\n", class, instance
, name
, quirk
, ws
);
8663 if ((qp
= malloc(sizeof *qp
)) == NULL
)
8664 err(1, "quirk_insert: malloc");
8666 if ((qp
->class = strdup(class)) == NULL
)
8667 err(1, "quirk_insert: strdup");
8668 if ((qp
->instance
= strdup(instance
)) == NULL
)
8669 err(1, "quirk_insert: strdup");
8670 if ((qp
->name
= strdup(name
)) == NULL
)
8671 err(1, "quirk_insert: strdup");
8673 if (asprintf(&str
, "^%s$", class) == -1)
8674 err(1, "quirk_insert: asprintf");
8675 if (regcomp(&qp
->regex_class
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8676 add_startup_exception("regex failed to compile quirk 'class' "
8677 "field: %s", class);
8680 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8683 if (asprintf(&str
, "^%s$", instance
) == -1)
8684 err(1, "quirk_insert: asprintf");
8685 if (regcomp(&qp
->regex_instance
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8686 add_startup_exception("regex failed to compile quirk 'instance'"
8687 " field: %s", instance
);
8690 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8693 if (asprintf(&str
, "^%s$", name
) == -1)
8694 err(1, "quirk_insert: asprintf");
8695 if (regcomp(&qp
->regex_name
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8696 add_startup_exception("regex failed to compile quirk 'name' "
8700 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8704 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: regex error; skipping.\n");
8709 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
8711 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
8715 quirk_remove(struct quirk
*qp
)
8717 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%u]\n", qp
->class,
8718 qp
->name
, qp
->quirk
);
8720 TAILQ_REMOVE(&quirks
, qp
, entry
);
8723 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
8727 quirk_free(struct quirk
*qp
)
8729 regfree(&qp
->regex_class
);
8730 regfree(&qp
->regex_instance
);
8731 regfree(&qp
->regex_name
);
8743 while ((qp
= TAILQ_FIRST(&quirks
)) != NULL
) {
8749 quirk_replace(struct quirk
*qp
, const char *class, const char *instance
,
8750 const char *name
, uint32_t quirk
, int ws
)
8752 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp
->class,
8753 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
8756 quirk_insert(class, instance
, name
, quirk
, ws
);
8758 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
8762 setquirk(const char *class, const char *instance
, const char *name
,
8763 uint32_t quirk
, int ws
)
8767 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
8768 instance
, name
, quirk
, ws
);
8770 /* Remove/replace existing quirk. */
8771 TAILQ_FOREACH(qp
, &quirks
, entry
) {
8772 if (strcmp(qp
->class, class) == 0 &&
8773 strcmp(qp
->instance
, instance
) == 0 &&
8774 strcmp(qp
->name
, name
) == 0) {
8775 if (quirk
== 0 && ws
== -1)
8778 quirk_replace(qp
, class, instance
, name
, quirk
,
8780 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8785 /* Only insert if quirk is not NONE or forced ws is set. */
8786 if (quirk
|| ws
!= -1)
8787 quirk_insert(class, instance
, name
, quirk
, ws
);
8789 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8792 /* Eat '\' in str used to escape square brackets and colon. */
8794 unescape_selector(char *str
)
8798 for (cp
= str
; *str
!= '\0'; ++str
, ++cp
) {
8799 if (*str
== '\\' && (*(str
+ 1) == ':' || *(str
+ 1) == ']' ||
8809 setconfquirk(const char *selector
, const char *value
, int flags
)
8811 char *str
, *cp
, *class;
8812 char *instance
= NULL
, *name
= NULL
;
8813 int retval
, count
= 0, ws
= -1;
8816 /* suppress unused warning since var is needed */
8819 if (selector
== NULL
|| strlen(selector
) == 0)
8822 if ((str
= strdup(selector
)) == NULL
)
8823 err(1, "setconfquirk: strdup");
8825 /* Find non-escaped colon. */
8832 for (++cp
; *cp
!= '\0'; ++cp
) {
8833 if (*cp
== ':' && *(cp
- 1) != '\\') {
8839 unescape_selector(class);
8841 instance
= class + strlen(class) + 1;
8842 unescape_selector(instance
);
8848 name
= instance
+ strlen(instance
) + 1;
8849 unescape_selector(name
);
8854 DNPRINTF(SWM_D_CONF
, "setconfquirk: class: %s, instance: %s, "
8855 "name: %s\n", class, instance
, name
);
8857 if ((retval
= parsequirks(value
, &qrks
, &ws
)) == 0)
8858 setquirk(class, instance
, name
, qrks
, ws
);
8867 setquirk("MPlayer", "xv", ".*",
8868 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
, -1);
8869 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
8871 setquirk("Firefox-bin", "firefox-bin", ".*",
8873 setquirk("Firefox", "Dialog", ".*",
8875 setquirk("Gimp", "gimp", ".*",
8876 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8877 setquirk("XTerm", "xterm", ".*",
8878 SWM_Q_XTERM_FONTADJ
, -1);
8879 setquirk("xine", "Xine Window", ".*",
8880 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8881 setquirk("Xitk", "Xitk Combo", ".*",
8882 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8883 setquirk("xine", "xine Panel", ".*",
8884 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8885 setquirk("Xitk", "Xine Window", ".*",
8886 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8887 setquirk("xine", "xine Video Fullscreen Window", ".*",
8888 SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
, -1);
8889 setquirk("pcb", "pcb", ".*",
8891 setquirk("SDL_App", "SDL_App", ".*",
8892 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
, -1);
8895 /* conf file stuff */
8896 #define SWM_CONF_FILE "spectrwm.conf"
8897 #define SWM_CONF_FILE_OLD "scrotwm.conf"
8901 SWM_S_BAR_AT_BOTTOM
,
8902 SWM_S_BAR_BORDER_WIDTH
,
8905 SWM_S_BAR_ENABLED_WS
,
8910 SWM_S_BOUNDARY_WIDTH
,
8911 SWM_S_CLOCK_ENABLED
,
8914 SWM_S_CYCLE_VISIBLE
,
8916 SWM_S_DISABLE_BORDER
,
8918 SWM_S_FOCUS_CLOSE_WRAP
,
8919 SWM_S_FOCUS_DEFAULT
,
8921 SWM_S_ICONIC_ENABLED
,
8922 SWM_S_JAVA_WORKAROUND
,
8923 SWM_S_MAXIMIZE_HIDE_BAR
,
8924 SWM_S_REGION_PADDING
,
8929 SWM_S_STACK_ENABLED
,
8932 SWM_S_URGENT_COLLAPSE
,
8933 SWM_S_URGENT_ENABLED
,
8934 SWM_S_VERBOSE_LAYOUT
,
8937 SWM_S_WINDOW_CLASS_ENABLED
,
8938 SWM_S_WINDOW_INSTANCE_ENABLED
,
8939 SWM_S_WINDOW_NAME_ENABLED
,
8940 SWM_S_WORKSPACE_CLAMP
,
8941 SWM_S_WORKSPACE_LIMIT
,
8942 SWM_S_WORKSPACE_NAME
,
8946 setconfvalue(const char *selector
, const char *value
, int flags
)
8948 struct workspace
*ws
;
8949 int i
, ws_id
, num_screens
;
8950 char *b
, *str
, *sp
, s
[1024];
8953 case SWM_S_BAR_ACTION
:
8955 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
8956 err(1, "setconfvalue: bar_action");
8958 case SWM_S_BAR_AT_BOTTOM
:
8959 bar_at_bottom
= (atoi(value
) != 0);
8961 case SWM_S_BAR_BORDER_WIDTH
:
8962 bar_border_width
= atoi(value
);
8963 if (bar_border_width
< 0)
8964 bar_border_width
= 0;
8966 case SWM_S_BAR_DELAY
:
8967 /* No longer needed; leave to not break old conf files. */
8969 case SWM_S_BAR_ENABLED
:
8970 bar_enabled
= (atoi(value
) != 0);
8972 case SWM_S_BAR_ENABLED_WS
:
8973 ws_id
= atoi(selector
) - 1;
8974 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8975 errx(1, "setconfvalue: bar_enabled_ws: invalid "
8976 "workspace %d.", ws_id
+ 1);
8978 num_screens
= get_screen_count();
8979 for (i
= 0; i
< num_screens
; i
++) {
8980 ws
= (struct workspace
*)&screens
[i
].ws
;
8981 ws
[ws_id
].bar_enabled
= (atoi(value
) != 0);
8984 case SWM_S_BAR_FONT
:
8986 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
8987 err(1, "setconfvalue: asprintf: failed to allocate "
8988 "memory for bar_fonts.");
8991 /* If already in xft mode, then we are done. */
8992 if (!bar_font_legacy
)
8995 if ((sp
= str
= strdup(value
)) == NULL
)
8996 err(1, "setconfvalue: strdup");
8998 /* If there are any non-XLFD entries, switch to Xft mode. */
8999 while ((b
= strsep(&sp
, ",")) != NULL
) {
9003 bar_font_legacy
= false;
9010 case SWM_S_BAR_FORMAT
:
9012 if ((bar_format
= strdup(value
)) == NULL
)
9013 err(1, "setconfvalue: bar_format");
9015 case SWM_S_BAR_JUSTIFY
:
9016 if (strcmp(value
, "left") == 0)
9017 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
9018 else if (strcmp(value
, "center") == 0)
9019 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
9020 else if (strcmp(value
, "right") == 0)
9021 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
9023 errx(1, "invalid bar_justify");
9025 case SWM_S_BORDER_WIDTH
:
9026 border_width
= atoi(value
);
9027 if (border_width
< 0)
9030 case SWM_S_BOUNDARY_WIDTH
:
9031 boundary_width
= atoi(value
);
9032 if (boundary_width
< 0)
9035 case SWM_S_CLOCK_ENABLED
:
9036 clock_enabled
= (atoi(value
) != 0);
9038 case SWM_S_CLOCK_FORMAT
:
9039 #ifndef SWM_DENY_CLOCK_FORMAT
9041 if ((clock_format
= strdup(value
)) == NULL
)
9042 err(1, "setconfvalue: clock_format");
9045 case SWM_S_CYCLE_EMPTY
:
9046 cycle_empty
= (atoi(value
) != 0);
9048 case SWM_S_CYCLE_VISIBLE
:
9049 cycle_visible
= (atoi(value
) != 0);
9051 case SWM_S_DIALOG_RATIO
:
9052 dialog_ratio
= atof(value
);
9053 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
9056 case SWM_S_DISABLE_BORDER
:
9057 disable_border
= (atoi(value
) != 0);
9059 case SWM_S_FOCUS_CLOSE
:
9060 if (strcmp(value
, "first") == 0)
9061 focus_close
= SWM_STACK_BOTTOM
;
9062 else if (strcmp(value
, "last") == 0)
9063 focus_close
= SWM_STACK_TOP
;
9064 else if (strcmp(value
, "next") == 0)
9065 focus_close
= SWM_STACK_ABOVE
;
9066 else if (strcmp(value
, "previous") == 0)
9067 focus_close
= SWM_STACK_BELOW
;
9069 errx(1, "focus_close");
9071 case SWM_S_FOCUS_CLOSE_WRAP
:
9072 focus_close_wrap
= (atoi(value
) != 0);
9074 case SWM_S_FOCUS_DEFAULT
:
9075 if (strcmp(value
, "last") == 0)
9076 focus_default
= SWM_STACK_TOP
;
9077 else if (strcmp(value
, "first") == 0)
9078 focus_default
= SWM_STACK_BOTTOM
;
9080 errx(1, "focus_default");
9082 case SWM_S_FOCUS_MODE
:
9083 if (strcmp(value
, "default") == 0)
9084 focus_mode
= SWM_FOCUS_DEFAULT
;
9085 else if (strcmp(value
, "follow") == 0 ||
9086 strcmp(value
, "follow_cursor") == 0)
9087 focus_mode
= SWM_FOCUS_FOLLOW
;
9088 else if (strcmp(value
, "manual") == 0)
9089 focus_mode
= SWM_FOCUS_MANUAL
;
9091 errx(1, "focus_mode");
9093 case SWM_S_ICONIC_ENABLED
:
9094 iconic_enabled
= (atoi(value
) != 0);
9096 case SWM_S_JAVA_WORKAROUND
:
9097 java_workaround
= (atoi(value
) != 0);
9099 case SWM_S_MAXIMIZE_HIDE_BAR
:
9100 maximize_hide_bar
= atoi(value
);
9102 case SWM_S_REGION_PADDING
:
9103 region_padding
= atoi(value
);
9104 if (region_padding
< 0)
9107 case SWM_S_SPAWN_ORDER
:
9108 if (strcmp(value
, "first") == 0)
9109 spawn_position
= SWM_STACK_BOTTOM
;
9110 else if (strcmp(value
, "last") == 0)
9111 spawn_position
= SWM_STACK_TOP
;
9112 else if (strcmp(value
, "next") == 0)
9113 spawn_position
= SWM_STACK_ABOVE
;
9114 else if (strcmp(value
, "previous") == 0)
9115 spawn_position
= SWM_STACK_BELOW
;
9117 errx(1, "spawn_position");
9119 case SWM_S_SPAWN_TERM
:
9120 setconfspawn("term", value
, 0);
9121 setconfspawn("spawn_term", value
, 0);
9124 /* No longer needed; leave to not break old conf files. */
9126 case SWM_S_SS_ENABLED
:
9127 /* No longer needed; leave to not break old conf files. */
9129 case SWM_S_STACK_ENABLED
:
9130 stack_enabled
= (atoi(value
) != 0);
9132 case SWM_S_TERM_WIDTH
:
9133 term_width
= atoi(value
);
9137 case SWM_S_TILE_GAP
:
9138 tile_gap
= atoi(value
);
9140 case SWM_S_URGENT_COLLAPSE
:
9141 urgent_collapse
= (atoi(value
) != 0);
9143 case SWM_S_URGENT_ENABLED
:
9144 urgent_enabled
= (atoi(value
) != 0);
9146 case SWM_S_VERBOSE_LAYOUT
:
9147 verbose_layout
= (atoi(value
) != 0);
9148 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
9150 layouts
[i
].l_string
= fancy_stacker
;
9152 layouts
[i
].l_string
= plain_stacker
;
9155 case SWM_S_WARP_FOCUS
:
9156 warp_focus
= (atoi(value
) != 0);
9158 case SWM_S_WARP_POINTER
:
9159 warp_pointer
= (atoi(value
) != 0);
9161 case SWM_S_WINDOW_CLASS_ENABLED
:
9162 window_class_enabled
= (atoi(value
) != 0);
9164 case SWM_S_WINDOW_INSTANCE_ENABLED
:
9165 window_instance_enabled
= (atoi(value
) != 0);
9167 case SWM_S_WINDOW_NAME_ENABLED
:
9168 window_name_enabled
= (atoi(value
) != 0);
9170 case SWM_S_WORKSPACE_CLAMP
:
9171 workspace_clamp
= (atoi(value
) != 0);
9173 case SWM_S_WORKSPACE_LIMIT
:
9174 workspace_limit
= atoi(value
);
9175 if (workspace_limit
> SWM_WS_MAX
)
9176 workspace_limit
= SWM_WS_MAX
;
9177 else if (workspace_limit
< 1)
9178 workspace_limit
= 1;
9180 ewmh_update_desktops();
9182 case SWM_S_WORKSPACE_NAME
:
9183 if (getenv("SWM_STARTED") != NULL
)
9187 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9188 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
9190 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9191 errx(1, "setconfvalue: workspace_name: invalid "
9192 "workspace %d.", ws_id
+ 1);
9194 num_screens
= get_screen_count();
9195 for (i
= 0; i
< num_screens
; ++i
) {
9196 ws
= (struct workspace
*)&screens
[i
].ws
;
9198 if (strlen(s
) > 0) {
9199 free(ws
[ws_id
].name
);
9200 if ((ws
[ws_id
].name
= strdup(s
)) == NULL
)
9201 err(1, "setconfvalue: workspace_name.");
9203 ewmh_update_desktop_names();
9204 ewmh_get_desktop_names();
9215 setconfmodkey(const char *selector
, const char *value
, int flags
)
9217 /* suppress unused warnings since vars are needed */
9221 if (strncasecmp(value
, "Mod1", strlen("Mod1")) == 0)
9222 update_modkey(XCB_MOD_MASK_1
);
9223 else if (strncasecmp(value
, "Mod2", strlen("Mod2")) == 0)
9224 update_modkey(XCB_MOD_MASK_2
);
9225 else if (strncasecmp(value
, "Mod3", strlen("Mod3")) == 0)
9226 update_modkey(XCB_MOD_MASK_3
);
9227 else if (strncasecmp(value
, "Mod4", strlen("Mod4")) == 0)
9228 update_modkey(XCB_MOD_MASK_4
);
9229 else if (strncasecmp(value
, "Mod5", strlen("Mod5")) == 0)
9230 update_modkey(XCB_MOD_MASK_5
);
9237 setconfcolor(const char *selector
, const char *value
, int flags
)
9239 int first
, last
, i
= 0, num_screens
;
9241 num_screens
= get_screen_count();
9243 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
9244 if (selector
== NULL
|| strlen(selector
) == 0 ||
9245 (last
= atoi(selector
) - 1) < 0) {
9247 last
= num_screens
- 1;
9252 if (last
>= num_screens
) {
9253 add_startup_exception("invalid screen index: %d out of bounds "
9254 "(maximum %d)", last
+ 1, num_screens
);
9258 for (i
= first
; i
<= last
; ++i
) {
9259 setscreencolor(value
, i
, flags
);
9262 * When setting focus/unfocus colors, we need to also
9263 * set maximize colors to match if they haven't been customized.
9265 if (flags
== SWM_S_COLOR_FOCUS
&&
9266 !screens
[i
].c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].manual
)
9267 setscreencolor(value
, i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
9268 else if (flags
== SWM_S_COLOR_UNFOCUS
&&
9269 !screens
[i
].c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].manual
)
9270 setscreencolor(value
, i
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
9272 screens
[i
].c
[flags
].manual
= 1;
9279 setconfregion(const char *selector
, const char *value
, int flags
)
9281 /* suppress unused warnings since vars are needed */
9285 custom_region(value
);
9290 setautorun(const char *selector
, const char *value
, int flags
)
9294 char *ap
, *sp
, *str
;
9300 /* suppress unused warnings since vars are needed */
9304 if (getenv("SWM_STARTED"))
9308 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9309 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
9311 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9312 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
9314 sp
= str
= expand_tilde((char *)&s
);
9317 * This is a little intricate
9319 * If the pid already exists we simply reuse it because it means it was
9320 * used before AND not claimed by manage_window. We get away with
9321 * altering it in the parent after INSERT because this can not be a race
9324 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
9327 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
9329 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
9330 err(1, "setautorun: realloc");
9331 a
.argv
[argc
- 1] = ap
;
9334 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
9335 err(1, "setautorun: realloc");
9336 a
.argv
[argc
] = NULL
;
9338 if ((pid
= fork()) == 0) {
9339 spawn(ws_id
, &a
, true);
9349 p
= calloc(1, sizeof *p
);
9352 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
9362 setlayout(const char *selector
, const char *value
, int flags
)
9364 struct workspace
*ws
;
9365 int ws_id
, i
, x
, mg
, ma
, si
, ar
;
9366 int st
= SWM_V_STACK
, num_screens
;
9370 /* suppress unused warnings since vars are needed */
9374 if (getenv("SWM_STARTED"))
9378 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
9379 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
9380 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9381 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9384 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9385 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
9387 if (strcasecmp(s
, "vertical") == 0)
9389 else if (strcasecmp(s
, "vertical_flip") == 0) {
9392 } else if (strcasecmp(s
, "horizontal") == 0)
9394 else if (strcasecmp(s
, "horizontal_flip") == 0) {
9397 } else if (strcasecmp(s
, "fullscreen") == 0)
9400 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9401 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9404 num_screens
= get_screen_count();
9405 for (i
= 0; i
< num_screens
; i
++) {
9406 ws
= (struct workspace
*)&screens
[i
].ws
;
9407 ws
[ws_id
].cur_layout
= &layouts
[st
];
9409 ws
[ws_id
].always_raise
= (ar
!= 0);
9410 if (st
== SWM_MAX_STACK
)
9414 for (x
= 0; x
< abs(mg
); x
++) {
9415 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9416 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
9417 SWM_ARG_ID_MASTERSHRINK
);
9420 for (x
= 0; x
< abs(ma
); x
++) {
9421 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9422 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
9423 SWM_ARG_ID_MASTERDEL
);
9426 for (x
= 0; x
< abs(si
); x
++) {
9427 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9428 si
>= 0 ? SWM_ARG_ID_STACKINC
:
9429 SWM_ARG_ID_STACKDEC
);
9433 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9434 SWM_ARG_ID_FLIPLAYOUT
);
9441 /* config options */
9442 struct config_option
{
9444 int (*func
)(const char*, const char*, int);
9447 struct config_option configopt
[] = {
9448 { "autorun", setautorun
, 0 },
9449 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
9450 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
9451 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
9452 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
9453 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
9454 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
9455 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
9456 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
9457 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
9458 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
9459 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
9460 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
9461 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
9462 { "bind", setconfbinding
, 0 },
9463 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
9464 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
9465 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
9466 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
9467 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
9468 { "color_focus_maximized", setconfcolor
, SWM_S_COLOR_FOCUS_MAXIMIZED
},
9469 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
9470 { "color_unfocus_maximized", setconfcolor
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
},
9471 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
9472 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
9473 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
9474 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
9475 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
9476 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
9477 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
9478 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
9479 { "iconic_enabled", setconfvalue
, SWM_S_ICONIC_ENABLED
},
9480 { "java_workaround", setconfvalue
, SWM_S_JAVA_WORKAROUND
},
9481 { "keyboard_mapping", setkeymapping
, 0 },
9482 { "layout", setlayout
, 0 },
9483 { "maximize_hide_bar", setconfvalue
, SWM_S_MAXIMIZE_HIDE_BAR
},
9484 { "modkey", setconfmodkey
, 0 },
9485 { "program", setconfspawn
, 0 },
9486 { "quirk", setconfquirk
, 0 },
9487 { "region", setconfregion
, 0 },
9488 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
9489 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
9490 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
9491 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
9492 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
9493 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
9494 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
9495 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
9496 { "title_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
}, /* For backwards compat. */
9497 { "title_name_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
}, /* For backwards compat. */
9498 { "urgent_collapse", setconfvalue
, SWM_S_URGENT_COLLAPSE
},
9499 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
9500 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
9501 { "warp_focus", setconfvalue
, SWM_S_WARP_FOCUS
},
9502 { "warp_pointer", setconfvalue
, SWM_S_WARP_POINTER
},
9503 { "window_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
},
9504 { "window_instance_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
},
9505 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
9506 { "workspace_clamp", setconfvalue
, SWM_S_WORKSPACE_CLAMP
},
9507 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
9508 { "name", setconfvalue
, SWM_S_WORKSPACE_NAME
},
9512 _add_startup_exception(const char *fmt
, va_list ap
)
9514 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
9515 warn("%s: asprintf", __func__
);
9519 add_startup_exception(const char *fmt
, ...)
9525 if (startup_exception
)
9528 /* force bar to be enabled due to exception */
9532 _add_startup_exception(fmt
, ap
);
9537 conf_load(const char *filename
, int keymapping
)
9540 char *line
= NULL
, *cp
, *ce
, *optsub
, *optval
= NULL
;
9541 size_t linelen
, lineno
= 0;
9542 int wordlen
, i
, optidx
, count
;
9543 struct config_option
*opt
= NULL
;
9545 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
9547 if (filename
== NULL
) {
9548 warnx("conf_load: no filename");
9552 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
9554 if ((config
= fopen(filename
, "r")) == NULL
) {
9555 warn("conf_load: fopen: %s", filename
);
9559 while (!feof(config
)) {
9563 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
9564 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
9566 err(1, "%s", filename
);
9571 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9572 if (cp
[0] == '\0') {
9576 /* get config option */
9577 wordlen
= strcspn(cp
, "=[ \t\n");
9579 add_startup_exception("%s: line %zd: no option found",
9584 for (i
= 0; i
< LENGTH(configopt
); i
++) {
9585 opt
= &configopt
[i
];
9586 if (strncasecmp(cp
, opt
->optname
, wordlen
) == 0 &&
9587 (int)strlen(opt
->optname
) == wordlen
) {
9593 add_startup_exception("%s: line %zd: unknown option "
9594 "%.*s", filename
, lineno
, wordlen
, cp
);
9597 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
9598 add_startup_exception("%s: line %zd: invalid option "
9599 "%.*s", filename
, lineno
, wordlen
, cp
);
9603 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9605 /* get [selector] if any */
9610 /* Get length of selector. */
9611 for (ce
= ++cp
; *ce
!= '\0'; ++ce
) {
9612 /* Find matching (not escaped) bracket. */
9613 if (*ce
== ']' && *(ce
- 1) != '\\') {
9620 add_startup_exception("%s: line %zd: syntax "
9621 "error: unterminated selector", filename
,
9626 /* ce points at ']'; terminate optsub. */
9631 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
9634 if (strlen(optval
) == 0) {
9635 add_startup_exception("%s: line %zd: must supply value "
9636 "to %s", filename
, lineno
,
9637 configopt
[optidx
].optname
);
9640 /* call function to deal with it all */
9641 if (configopt
[optidx
].func(optsub
, optval
,
9642 configopt
[optidx
].funcflags
) != 0) {
9643 add_startup_exception("%s: line %zd: invalid data for "
9644 "%s", filename
, lineno
, configopt
[optidx
].optname
);
9653 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
9659 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
9661 struct ws_win
*parent
, *w
;
9662 struct swm_region
*r
;
9663 struct workspace
*ws
;
9664 xcb_icccm_wm_hints_t wmh
;
9666 parent
= find_window(win
->transient
);
9668 parent
->focus_child
= win
;
9670 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
9671 " for %#x trans %#x\n", win
->id
, win
->transient
);
9673 r
= root_to_region(win
->s
->root
, SWM_CK_ALL
);
9675 /* parent doen't exist in our window list */
9676 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
9677 if (xcb_icccm_get_wm_hints_reply(conn
,
9678 xcb_icccm_get_wm_hints(conn
, w
->id
),
9680 warnx("can't get hints for %#x", w
->id
);
9684 if (win
->hints
.window_group
!= wmh
.window_group
)
9687 w
->focus_child
= win
;
9688 win
->transient
= w
->id
;
9690 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
9691 "transient to %#x\n", win
->transient
);
9698 window_get_pid(xcb_window_t win
)
9703 xcb_get_property_cookie_t pc
;
9704 xcb_get_property_reply_t
*pr
;
9706 apid
= get_atom_from_string("_NET_WM_PID");
9707 if (apid
== XCB_ATOM_NONE
)
9710 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
9711 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9714 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
9719 if (pr
->type
== apid
&& pr
->format
== 32)
9720 ret
= *((pid_t
*)xcb_get_property_value(pr
));
9726 apid
= get_atom_from_string("_SWM_PID");
9727 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
9729 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9732 if (pr
->type
!= apid
) {
9737 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
9744 get_swm_ws(xcb_window_t id
)
9750 xcb_get_property_reply_t
*gpr
;
9752 gpr
= xcb_get_property_reply(conn
,
9753 xcb_get_property(conn
, 0, id
, a_swm_ws
,
9754 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
9759 proplen
= xcb_get_property_value_length(gpr
);
9761 prop
= malloc(proplen
+ 1);
9764 xcb_get_property_value(gpr
),
9766 prop
[proplen
] = '\0';
9773 DNPRINTF(SWM_D_PROP
, "get_swm_ws: _SWM_WS: %s\n", prop
);
9774 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
9776 DNPRINTF(SWM_D_PROP
, "get_swm_ws: win #%s: %s",
9786 get_ws_idx(struct ws_win
*win
)
9788 xcb_get_property_reply_t
*gpr
;
9794 gpr
= xcb_get_property_reply(conn
,
9795 xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
9796 XCB_ATOM_CARDINAL
, 0, 1),
9799 if (gpr
->type
== XCB_ATOM_CARDINAL
&& gpr
->format
== 32)
9800 ws_idx
= *((int *)xcb_get_property_value(gpr
));
9804 if (ws_idx
== -1 && !(win
->quirks
& SWM_Q_IGNORESPAWNWS
))
9805 ws_idx
= get_swm_ws(win
->id
);
9807 if (ws_idx
> workspace_limit
- 1 || ws_idx
< -1)
9810 DNPRINTF(SWM_D_PROP
, "get_ws_idx: win %#x, ws_idx: %d\n", win
->id
,
9817 reparent_window(struct ws_win
*win
)
9819 xcb_void_cookie_t c
;
9820 xcb_generic_error_t
*error
;
9823 win
->frame
= xcb_generate_id(conn
);
9825 DNPRINTF(SWM_D_MISC
, "reparent_window: win %#x, frame: %#x\n",
9826 win
->id
, win
->frame
);
9829 XCB_EVENT_MASK_ENTER_WINDOW
|
9830 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
9831 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
9832 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
|
9833 XCB_EVENT_MASK_EXPOSURE
;
9835 wa
[0] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
9838 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
->frame
, win
->s
->root
,
9839 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), 0,
9840 XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
9841 XCB_CW_EVENT_MASK
, wa
);
9843 win
->state
= SWM_WIN_STATE_REPARENTING
;
9844 c
= xcb_reparent_window_checked(conn
, win
->id
, win
->frame
, 0, 0);
9845 if ((error
= xcb_request_check(conn
, c
))) {
9846 DNPRINTF(SWM_D_MISC
, "reparent_window: error:\n");
9851 xcb_destroy_window(conn
, win
->frame
);
9852 win
->frame
= XCB_WINDOW_NONE
;
9854 xcb_change_save_set(conn
, XCB_SET_MODE_INSERT
, win
->id
);
9856 DNPRINTF(SWM_D_MISC
, "reparent_window: done.\n");
9860 unparent_window(struct ws_win
*win
)
9862 xcb_change_save_set(conn
, XCB_SET_MODE_DELETE
, win
->id
);
9863 xcb_reparent_window(conn
, win
->id
, win
->s
->root
, X(win
), Y(win
));
9864 xcb_destroy_window(conn
, win
->frame
);
9865 win
->frame
= XCB_WINDOW_NONE
;
9866 win
->state
= SWM_WIN_STATE_UNPARENTING
;
9870 manage_window(xcb_window_t id
, int spawn_pos
, bool mapping
)
9872 struct ws_win
*win
= NULL
, *ww
;
9873 struct swm_region
*r
;
9876 xcb_get_geometry_reply_t
*gr
;
9877 xcb_get_window_attributes_reply_t
*war
= NULL
;
9878 xcb_window_t trans
= XCB_WINDOW_NONE
;
9879 uint32_t i
, wa
[1], new_flags
;
9880 int ws_idx
, force_ws
= -1;
9881 char *class, *instance
, *name
;
9884 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region bar; "
9889 if (find_region(id
)) {
9890 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region window; "
9895 if ((win
= find_window(id
)) != NULL
) {
9896 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x already "
9897 "managed; skipping.)\n", id
);
9898 return (win
); /* Already managed. */
9901 /* See if window is on the unmanaged list. */
9902 if ((win
= find_unmanaged_window(id
)) != NULL
) {
9903 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x found on "
9904 "unmanaged list.\n", id
);
9905 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
9907 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is new.\n", id
);
9910 war
= xcb_get_window_attributes_reply(conn
,
9911 xcb_get_window_attributes(conn
, id
), NULL
);
9913 DNPRINTF(SWM_D_EVENT
, "manage_window: window lost.\n");
9917 if (war
->override_redirect
) {
9918 DNPRINTF(SWM_D_EVENT
, "manage_window: override_redirect; "
9923 if (!mapping
&& war
->map_state
== XCB_MAP_STATE_UNMAPPED
&&
9924 get_win_state(id
) == XCB_ICCCM_WM_STATE_WITHDRAWN
) {
9925 DNPRINTF(SWM_D_EVENT
, "manage_window: window withdrawn; "
9930 /* Try to get initial window geometry. */
9931 gr
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, id
), NULL
);
9933 DNPRINTF(SWM_D_MISC
, "manage_window: get geometry failed.\n");
9937 /* Figure out which region the window belongs to. */
9938 r
= root_to_region(gr
->root
, SWM_CK_ALL
);
9940 /* Handle special windows with special _NET_WM_WINDOW_TYPE */
9941 if (ewmh_handle_special_types(id
, r
)) {
9942 DNPRINTF(SWM_D_EVENT
, "manage_window: "
9943 "unmanaged ewmh window type\n");
9948 /* Create and initialize ws_win object. */
9949 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
9950 err(1, "manage_window: calloc: failed to allocate "
9951 "memory for new window");
9956 /* Ignore window border if there is one. */
9957 WIDTH(win
) = gr
->width
;
9958 HEIGHT(win
) = gr
->height
;
9959 X(win
) = gr
->x
+ gr
->border_width
;
9960 Y(win
) = gr
->y
+ gr
->border_width
;
9961 win
->bordered
= false;
9962 win
->mapped
= (war
->map_state
!= XCB_MAP_STATE_UNMAPPED
);
9963 win
->s
= r
->s
; /* this never changes */
9967 /* Select which X events to monitor and set border pixel color. */
9968 wa
[0] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
9969 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
9971 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_EVENT_MASK
, wa
);
9973 /* Get WM_SIZE_HINTS. */
9974 xcb_icccm_get_wm_normal_hints_reply(conn
,
9975 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
9979 xcb_icccm_get_wm_hints_reply(conn
,
9980 xcb_icccm_get_wm_hints(conn
, win
->id
),
9983 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
9984 xcb_icccm_get_wm_transient_for_reply(conn
,
9985 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
9988 win
->transient
= trans
;
9989 set_child_transient(win
, &win
->transient
);
9992 /* Get WM_PROTOCOLS. */
9993 get_wm_protocols(win
);
9996 /* Must be after getting WM_HINTS and WM_PROTOCOLS. */
9997 DNPRINTF(SWM_D_FOCUS
, "manage_window: input_model: %s\n",
9998 get_win_input_model(win
));
10001 /* Set initial quirks based on EWMH. */
10002 ewmh_autoquirk(win
);
10004 /* Determine initial quirks. */
10005 xcb_icccm_get_wm_class_reply(conn
,
10006 xcb_icccm_get_wm_class(conn
, win
->id
),
10009 class = win
->ch
.class_name
? win
->ch
.class_name
: "";
10010 instance
= win
->ch
.instance_name
? win
->ch
.instance_name
: "";
10011 name
= get_win_name(win
->id
);
10013 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, instance: %s, "
10014 "name: %s\n", class, instance
, name
);
10016 /* java is retarded so treat it special */
10017 if (strstr(instance
, "sun-awt")) {
10018 DNPRINTF(SWM_D_CLASS
, "manage_window: java window detected.\n");
10022 TAILQ_FOREACH(qp
, &quirks
, entry
) {
10023 if (regexec(&qp
->regex_class
, class, 0, NULL
, 0) == 0 &&
10024 regexec(&qp
->regex_instance
, instance
, 0, NULL
, 0) == 0 &&
10025 regexec(&qp
->regex_name
, name
, 0, NULL
, 0) == 0) {
10026 DNPRINTF(SWM_D_CLASS
, "manage_window: matched "
10027 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp
->class,
10028 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
10029 win
->quirks
= qp
->quirk
;
10030 if (qp
->ws
>= 0 && qp
->ws
< workspace_limit
)
10037 /* Reset font sizes (the bruteforce way; no default keybinding). */
10038 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
10039 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
10040 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
10041 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
10042 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
10045 /* Figure out which workspace the window belongs to. */
10046 if (!(win
->quirks
& SWM_Q_IGNOREPID
) &&
10047 (p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
10048 win
->ws
= &r
->s
->ws
[p
->ws
];
10049 TAILQ_REMOVE(&pidlist
, p
, entry
);
10052 } else if ((ws_idx
= get_ws_idx(win
)) != -1 &&
10054 /* _SWM_WS is set; use that. */
10055 win
->ws
= &r
->s
->ws
[ws_idx
];
10056 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
10057 /* Launch transients in the same ws as parent. */
10063 if (force_ws
!= -1)
10064 win
->ws
= &r
->s
->ws
[force_ws
];
10066 /* Set the _NET_WM_DESKTOP atom. */
10067 DNPRINTF(SWM_D_PROP
, "manage_window: set _NET_WM_DESKTOP: %d\n",
10069 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
10070 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &win
->ws
->idx
);
10072 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
10073 xcb_delete_property(conn
, win
->id
, a_swm_ws
);
10075 /* WS must already be set for this to work. */
10076 store_float_geom(win
);
10078 /* Make sure window is positioned inside its region, if its active. */
10080 region_containment(win
, r
, SWM_CW_ALLSIDES
|
10081 SWM_CW_HARDBOUNDARY
);
10082 update_window(win
);
10085 /* Figure out where to insert the window in the workspace list. */
10086 if (trans
&& (ww
= find_window(trans
)))
10087 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
10088 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_ABOVE
)
10089 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
10091 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_BELOW
)
10092 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
10093 else switch (spawn_pos
) {
10095 case SWM_STACK_TOP
:
10096 case SWM_STACK_ABOVE
:
10097 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
10099 case SWM_STACK_BOTTOM
:
10100 case SWM_STACK_BELOW
:
10101 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
10104 ewmh_update_client_list();
10106 TAILQ_INSERT_HEAD(&win
->ws
->stack
, win
, stack_entry
);
10109 /* Get/apply initial _NET_WM_STATE */
10110 ewmh_get_wm_state(win
);
10112 /* Apply quirks. */
10113 new_flags
= win
->ewmh_flags
;
10115 if (win
->quirks
& SWM_Q_FLOAT
)
10116 new_flags
|= EWMH_F_ABOVE
;
10118 if (win
->quirks
& SWM_Q_ANYWHERE
)
10119 new_flags
|= SWM_F_MANUAL
;
10121 ewmh_apply_flags(win
, new_flags
);
10122 ewmh_update_wm_state(win
);
10124 /* Set initial _NET_WM_ALLOWED_ACTIONS */
10125 ewmh_update_actions(win
);
10127 reparent_window(win
);
10129 DNPRINTF(SWM_D_MISC
, "manage_window: done. win %#x, (x,y) w x h: "
10130 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win
->id
,
10131 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
10132 YESNO(ICONIC(win
)), win
->transient
);
10139 free_window(struct ws_win
*win
)
10141 DNPRINTF(SWM_D_MISC
, "free_window: win %#x\n", WINID(win
));
10146 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
10149 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
10152 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
10156 unmanage_window(struct ws_win
*win
)
10158 DNPRINTF(SWM_D_MISC
, "unmanage_window: win %#x\n", WINID(win
));
10164 unparent_window(win
);
10166 TAILQ_REMOVE(&win
->ws
->stack
, win
, stack_entry
);
10167 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
10168 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
10170 ewmh_update_client_list();
10174 expose(xcb_expose_event_t
*e
)
10179 struct workspace
*ws
;
10182 DNPRINTF(SWM_D_EVENT
, "expose: win %#x, count: %d\n", e
->window
,
10188 if ((b
= find_bar(e
->window
))) {
10191 } else if ((w
= find_window(e
->window
)) && w
->frame
== e
->window
) {
10195 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
10201 DNPRINTF(SWM_D_EVENT
, "expose: done\n");
10205 focusin(xcb_focus_in_event_t
*e
)
10207 struct ws_win
*win
;
10209 DNPRINTF(SWM_D_EVENT
, "focusin: win %#x, mode: %s(%u), "
10210 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10211 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10212 if ((win
= find_window(e
->event
)) && win
!= win
->ws
->focus
&&
10213 win
!= win
->ws
->focus_pending
&&
10214 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10215 win
->ws
->focus_prev
= win
->ws
->focus
;
10216 win
->ws
->focus
= win
;
10217 win
->ws
->focus_pending
= NULL
;
10219 if (win
->ws
->focus_prev
)
10220 draw_frame(win
->ws
->focus_prev
);
10228 focusout(xcb_focus_out_event_t
*e
)
10230 DNPRINTF(SWM_D_EVENT
, "focusout: win %#x, mode: %s(%u), "
10231 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10232 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10237 keypress(xcb_key_press_event_t
*e
)
10240 struct binding
*bp
;
10241 xcb_keysym_t keysym
;
10242 bool replay
= true;
10244 last_event_time
= e
->time
;
10246 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
10248 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
10249 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10250 "state: %u, cleaned: %u, same_screen: %s\n", keysym
, e
->event
,
10251 e
->event_x
, e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
,
10252 e
->root_y
, e
->child
, e
->state
, CLEANMASK(e
->state
),
10253 YESNO(e
->same_screen
));
10255 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10257 /* Look for catch-all. */
10258 if ((bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
)) == NULL
)
10262 replay
= bp
->flags
& BINDING_F_REPLAY
;
10264 if ((ap
= &actions
[bp
->action
]) == NULL
)
10267 if (bp
->action
== FN_SPAWN_CUSTOM
)
10268 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10271 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10273 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10277 DNPRINTF(SWM_D_EVENT
, "keypress: replaying.\n");
10278 /* Pass keypress to event window and unfreeze keyboard queue. */
10279 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10281 /* Release freeze. */
10282 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10286 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
10290 keyrelease(xcb_key_release_event_t
*e
)
10293 struct binding
*bp
;
10294 xcb_keysym_t keysym
;
10296 last_event_time
= e
->time
;
10298 keysym
= xcb_key_release_lookup_keysym(syms
, e
, 0);
10300 DNPRINTF(SWM_D_EVENT
, "keyrelease: keysym: %u, win (x,y): %#x (%d,%d), "
10301 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10302 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
10303 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
10304 e
->child
, e
->state
, YESNO(e
->same_screen
));
10306 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10308 /* Look for catch-all. */
10309 bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
);
10311 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10312 bp
->flags
& BINDING_F_REPLAY
) {
10313 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10314 DNPRINTF(SWM_D_EVENT
, "keyrelease: replaying.\n");
10316 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10317 DNPRINTF(SWM_D_EVENT
, "keyrelease: unfreezing.\n");
10322 DNPRINTF(SWM_D_EVENT
, "keyrelease: done.\n");
10326 buttonpress(xcb_button_press_event_t
*e
)
10328 struct ws_win
*win
= NULL
, *newf
;
10329 struct swm_region
*r
, *old_r
;
10331 struct binding
*bp
;
10332 bool replay
= true;
10334 last_event_time
= e
->time
;
10336 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): %#x (%d,%d), "
10337 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10338 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10339 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10340 e
->state
, YESNO(e
->same_screen
));
10342 if (e
->event
== e
->root
) {
10344 win
= find_window(e
->child
);
10346 /* Focus on empty region */
10347 /* If no windows on region if its empty. */
10348 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10349 if (r
&& TAILQ_EMPTY(&r
->ws
->winlist
)) {
10350 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
10351 if (old_r
&& old_r
!= r
)
10352 unfocus_win(old_r
->ws
->focus
);
10354 DNPRINTF(SWM_D_FOCUS
, "buttonpress: "
10355 "set_input_focus: %#x, revert-to: parent, "
10356 "time: %#x\n", e
->root
, e
->time
);
10357 xcb_set_input_focus(conn
,
10358 XCB_INPUT_FOCUS_POINTER_ROOT
,
10361 /* Clear bar since empty. */
10364 /* No need to replay event. */
10369 win
= find_window(e
->event
);
10373 newf
= get_focus_magic(win
);
10374 if (win
->ws
->focus
== newf
&& newf
!= win
) {
10375 if (win
->focus_child
== win
)
10376 win
->focus_child
= NULL
;
10382 /* Handle any bound action. */
10383 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10385 /* Look for catch-all. */
10386 if ((bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
)) == NULL
)
10391 replay
= bp
->flags
& BINDING_F_REPLAY
;
10393 if ((ap
= &actions
[bp
->action
]) == NULL
)
10396 if (bp
->action
== FN_SPAWN_CUSTOM
)
10397 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10400 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10402 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10406 DNPRINTF(SWM_D_EVENT
, "buttonpress: replaying.\n");
10407 /* Replay event to event window */
10408 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10410 /* Unfreeze grab events. */
10411 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10418 buttonrelease(xcb_button_release_event_t
*e
)
10421 struct binding
*bp
;
10423 last_event_time
= e
->time
;
10425 DNPRINTF(SWM_D_EVENT
, "buttonrelease: win (x,y): %#x (%d,%d), "
10426 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10427 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10428 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10429 e
->state
, YESNO(e
->same_screen
));
10431 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10433 /* Look for catch-all. */
10434 bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
);
10436 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10437 bp
->flags
& BINDING_F_REPLAY
) {
10438 DNPRINTF(SWM_D_EVENT
, "buttonrelease: replaying.\n");
10439 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10441 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10449 get_win_input_model(struct ws_win
*win
)
10453 * Input Model Input Field WM_TAKE_FOCUS
10454 * No Input False Absent
10455 * Passive True Absent
10456 * Locally Active True Present
10457 * Globally Active False Present
10460 if (ACCEPTS_FOCUS(win
))
10461 inputmodel
= (win
->take_focus
) ? "Locally Active" : "Passive";
10463 inputmodel
= (win
->take_focus
) ? "Globally Active" : "No Input";
10469 print_win_geom(xcb_window_t w
)
10471 xcb_get_geometry_reply_t
*wa
;
10473 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
10475 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: %#x\n",
10480 DNPRINTF(SWM_D_MISC
, "print_win_geom: win %#x, root: %#x, "
10481 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
10482 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
10491 get_stack_mode_name(uint8_t mode
)
10496 case XCB_STACK_MODE_ABOVE
:
10499 case XCB_STACK_MODE_BELOW
:
10502 case XCB_STACK_MODE_TOP_IF
:
10505 case XCB_STACK_MODE_BOTTOM_IF
:
10508 case XCB_STACK_MODE_OPPOSITE
:
10520 configurerequest(xcb_configure_request_event_t
*e
)
10522 struct ws_win
*win
;
10523 struct swm_region
*r
= NULL
;
10525 uint32_t wc
[7] = {0};
10529 if ((win
= find_window(e
->window
)) == NULL
)
10530 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10534 if (swm_debug
& SWM_D_EVENT
) {
10535 print_win_geom(e
->window
);
10537 DNPRINTF(SWM_D_EVENT
, "configurerequest: win %#x, "
10538 "parent: %#x, new: %s, value_mask: %u { ", e
->window
,
10539 e
->parent
, YESNO(new), e
->value_mask
);
10540 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
10541 DPRINTF("X: %d ", e
->x
);
10542 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
10543 DPRINTF("Y: %d ", e
->y
);
10544 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10545 DPRINTF("W: %u ", e
->width
);
10546 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10547 DPRINTF("H: %u ", e
->height
);
10548 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
10549 DPRINTF("Border: %u ", e
->border_width
);
10550 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
10551 DPRINTF("Sibling: %#x ", e
->sibling
);
10552 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
10553 DPRINTF("StackMode: %s(%u) ",
10554 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
10560 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10561 mask
|= XCB_CONFIG_WINDOW_X
;
10564 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10565 mask
|= XCB_CONFIG_WINDOW_Y
;
10568 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
10569 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
10570 wc
[i
++] = e
->width
;
10572 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
10573 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
10574 wc
[i
++] = e
->height
;
10576 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
10577 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
10578 wc
[i
++] = e
->border_width
;
10580 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
10581 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
10582 wc
[i
++] = e
->sibling
;
10584 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
10585 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
10586 wc
[i
++] = e
->stack_mode
;
10590 xcb_configure_window(conn
, e
->window
, mask
, wc
);
10593 } else if ((!MANUAL(win
) || win
->quirks
& SWM_Q_ANYWHERE
) &&
10594 !FULLSCREEN(win
) && !MAXIMIZED(win
)) {
10597 else if (win
->ws
->old_r
)
10598 r
= win
->ws
->old_r
;
10600 /* windows are centered unless ANYWHERE quirk is set. */
10601 if (win
->quirks
& SWM_Q_ANYWHERE
) {
10602 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10603 win
->g_float
.x
= e
->x
;
10605 win
->g_float
.x
-= X(r
);
10608 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10609 win
->g_float
.y
= e
->y
;
10611 win
->g_float
.y
-= Y(r
);
10615 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10616 win
->g_float
.w
= e
->width
;
10618 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10619 win
->g_float
.h
= e
->height
;
10621 win
->g_floatvalid
= true;
10623 if (!MAXIMIZED(win
) && !FULLSCREEN(win
) &&
10624 (TRANS(win
) || (ABOVE(win
) &&
10625 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]))) {
10626 WIDTH(win
) = win
->g_float
.w
;
10627 HEIGHT(win
) = win
->g_float
.h
;
10630 update_floater(win
);
10633 config_win(win
, e
);
10637 config_win(win
, e
);
10641 config_win(win
, e
);
10645 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
10649 configurenotify(xcb_configure_notify_event_t
*e
)
10651 struct ws_win
*win
;
10653 DNPRINTF(SWM_D_EVENT
, "configurenotify: win %#x, event win: %#x, "
10654 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
10655 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
10656 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
10657 YESNO(e
->override_redirect
));
10659 win
= find_window(e
->window
);
10662 if (font_adjusted
&& win
->ws
->r
) {
10670 destroynotify(xcb_destroy_notify_event_t
*e
)
10672 struct ws_win
*win
;
10673 struct workspace
*ws
;
10675 DNPRINTF(SWM_D_EVENT
, "destroynotify: win %#x\n", e
->window
);
10677 if ((win
= find_window(e
->window
)) == NULL
) {
10678 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10680 /* Window is on unmanaged list. */
10681 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10688 if (win
->frame
== e
->window
) {
10689 DNPRINTF(SWM_D_EVENT
, "destroynotify: frame for win %#x\n",
10691 win
->frame
= XCB_WINDOW_NONE
;
10695 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10696 /* If we were focused, make sure we focus on something else. */
10697 if (win
== ws
->focus
) {
10698 ws
->focus_pending
= get_focus_prev(win
);
10699 if (ws
->focus_pending
== win
)
10700 ws
->focus_pending
= NULL
;
10704 unmanage_window(win
);
10705 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10709 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
10710 if (ws
->focus_pending
) {
10711 focus_win(ws
->focus_pending
);
10712 ws
->focus_pending
= NULL
;
10713 } else if (ws
->focus
== NULL
) {
10714 DNPRINTF(SWM_D_FOCUS
, "destroynotify: set_input_focus: "
10715 "%#x, revert-to: parent, time: 0\n", ws
->r
->id
);
10716 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
10717 ws
->r
->id
, XCB_CURRENT_TIME
);
10724 DNPRINTF(SWM_D_EVENT
, "destroynotify: done.\n");
10729 get_notify_detail_label(uint8_t detail
)
10734 case XCB_NOTIFY_DETAIL_ANCESTOR
:
10735 label
= "Ancestor";
10737 case XCB_NOTIFY_DETAIL_VIRTUAL
:
10740 case XCB_NOTIFY_DETAIL_INFERIOR
:
10741 label
= "Inferior";
10743 case XCB_NOTIFY_DETAIL_NONLINEAR
:
10744 label
= "Nonlinear";
10746 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
10747 label
= "NonlinearVirtual";
10749 case XCB_NOTIFY_DETAIL_POINTER
:
10752 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
10753 label
= "PointerRoot";
10755 case XCB_NOTIFY_DETAIL_NONE
:
10766 get_notify_mode_label(uint8_t mode
)
10771 case XCB_NOTIFY_MODE_NORMAL
:
10774 case XCB_NOTIFY_MODE_GRAB
:
10777 case XCB_NOTIFY_MODE_UNGRAB
:
10780 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
10781 label
= "WhileGrabbed";
10791 get_state_mask_label(uint16_t state
)
10796 case XCB_KEY_BUT_MASK_SHIFT
:
10797 label
= "ShiftMask";
10799 case XCB_KEY_BUT_MASK_LOCK
:
10800 label
= "LockMask";
10802 case XCB_KEY_BUT_MASK_CONTROL
:
10803 label
= "ControlMask";
10805 case XCB_KEY_BUT_MASK_MOD_1
:
10806 label
= "Mod1Mask";
10808 case XCB_KEY_BUT_MASK_MOD_2
:
10809 label
= "Mod2Mask";
10811 case XCB_KEY_BUT_MASK_MOD_3
:
10812 label
= "Mod3Mask";
10814 case XCB_KEY_BUT_MASK_MOD_4
:
10815 label
= "Mod4Mask";
10817 case XCB_KEY_BUT_MASK_MOD_5
:
10818 label
= "Mod5Mask";
10820 case XCB_KEY_BUT_MASK_BUTTON_1
:
10821 label
= "Button1Mask";
10823 case XCB_KEY_BUT_MASK_BUTTON_2
:
10824 label
= "Button2Mask";
10826 case XCB_KEY_BUT_MASK_BUTTON_3
:
10827 label
= "Button3Mask";
10829 case XCB_KEY_BUT_MASK_BUTTON_4
:
10830 label
= "Button4Mask";
10832 case XCB_KEY_BUT_MASK_BUTTON_5
:
10833 label
= "Button5Mask";
10847 enternotify(xcb_enter_notify_event_t
*e
)
10849 struct ws_win
*win
;
10850 struct swm_region
*r
;
10852 last_event_time
= e
->time
;
10854 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): %#x "
10855 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10856 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10857 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10858 get_notify_mode_label(e
->mode
), e
->mode
,
10859 get_notify_detail_label(e
->detail
), e
->detail
,
10860 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10861 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10864 if (e
->event
== e
->root
&& e
->child
== XCB_WINDOW_NONE
&&
10865 e
->mode
== XCB_NOTIFY_MODE_GRAB
&&
10866 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10867 DNPRINTF(SWM_D_EVENT
, "enternotify: grab inferior; ignoring.\n");
10871 if (focus_mode
== SWM_FOCUS_MANUAL
&&
10872 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10873 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
10877 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10878 e
->mode
== XCB_NOTIFY_MODE_UNGRAB
&&
10879 e
->detail
!= XCB_NOTIFY_DETAIL_ANCESTOR
) {
10880 DNPRINTF(SWM_D_EVENT
, "enternotify: ungrab; ignoring.\n");
10884 if ((win
= find_window(e
->event
)) == NULL
) {
10885 if (e
->event
== e
->root
) {
10886 /* If no windows on pointer region, then focus root. */
10887 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10889 DNPRINTF(SWM_D_EVENT
, "enternotify: "
10890 "NULL region; ignoring.\n");
10896 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
10901 if (e
->mode
== XCB_NOTIFY_MODE_NORMAL
&&
10902 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10903 DNPRINTF(SWM_D_EVENT
, "enternotify: entering from "
10904 "inferior; ignoring\n");
10908 focus_win(get_focus_magic(win
));
10911 DNPRINTF(SWM_D_EVENT
, "enternotify: done\n");
10918 leavenotify(xcb_leave_notify_event_t
*e
)
10920 last_event_time
= e
->time
;
10922 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): %#x "
10923 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10924 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10925 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10926 get_notify_mode_label(e
->mode
), e
->mode
,
10927 get_notify_detail_label(e
->detail
), e
->detail
,
10928 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10929 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10935 mapnotify(xcb_map_notify_event_t
*e
)
10937 struct ws_win
*win
, *parent
= NULL
;
10938 struct workspace
*ws
;
10940 DNPRINTF(SWM_D_EVENT
, "mapnotify: win %#x\n", e
->window
);
10942 if ((win
= manage_window(e
->window
, spawn_position
, false)) == NULL
)
10946 if (win
->state
== SWM_WIN_STATE_REPARENTING
)
10949 if (ws
->r
== NULL
) {
10954 /* Need to know if win was mapped due to ws switch. */
10955 if (ws
->state
== SWM_WS_STATE_MAPPED
) {
10956 if (ws
->focus_pending
&& TRANS(ws
->focus_pending
))
10957 parent
= find_window(win
->transient
);
10959 /* If window's parent is maximized, don't clear it. */
10960 if ((parent
== NULL
) || !MAXIMIZED(parent
))
10961 if (clear_maximized(ws
) > 0)
10965 win
->mapped
= true;
10966 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
10968 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
10969 if (ws
->focus_pending
== win
) {
10971 ws
->focus_pending
= NULL
;
10972 center_pointer(ws
->r
);
10978 DNPRINTF(SWM_D_EVENT
, "mapnotify: done\n");
10984 mappingnotify(xcb_mapping_notify_event_t
*e
)
10986 if (e
->request
!= XCB_MAPPING_POINTER
) {
10987 xcb_refresh_keyboard_mapping(syms
, e
);
10995 maprequest(xcb_map_request_event_t
*e
)
10997 struct ws_win
*win
, *w
= NULL
;
10999 DNPRINTF(SWM_D_EVENT
, "maprequest: win %#x\n",
11002 win
= manage_window(e
->window
, spawn_position
, true);
11006 /* The new window should get focus; prepare. */
11007 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
11008 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
) && ACCEPTS_FOCUS(win
)) {
11009 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
11010 /* See if other wins of same type are already mapped. */
11011 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
11012 if (w
== win
|| !w
->mapped
)
11015 if (w
->ch
.class_name
&&
11016 win
->ch
.class_name
&&
11017 strcmp(w
->ch
.class_name
,
11018 win
->ch
.class_name
) == 0 &&
11019 w
->ch
.instance_name
&&
11020 win
->ch
.instance_name
&&
11021 strcmp(w
->ch
.instance_name
,
11022 win
->ch
.instance_name
) == 0)
11028 win
->ws
->focus_pending
= get_focus_magic(win
);
11031 /* All windows need to be mapped if they are in the current workspace.*/
11034 /* Ignore EnterNotify to handle the mapnotify without interference. */
11035 if (focus_mode
== SWM_FOCUS_DEFAULT
)
11036 event_drain(XCB_ENTER_NOTIFY
);
11038 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
11042 motionnotify(xcb_motion_notify_event_t
*e
)
11044 struct swm_region
*r
= NULL
;
11045 int i
, num_screens
;
11047 last_event_time
= e
->time
;
11049 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): %#x "
11050 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
11051 "child: %#x, same_screen_focus: %s, state: %d\n",
11052 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
11053 get_notify_detail_label(e
->detail
), e
->detail
,
11054 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
11055 YESNO(e
->same_screen
), e
->state
);
11057 if (focus_mode
== SWM_FOCUS_MANUAL
)
11060 num_screens
= get_screen_count();
11061 for (i
= 0; i
< num_screens
; i
++)
11062 if (screens
[i
].root
== e
->root
)
11065 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11066 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
11067 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
11075 get_atom_name(xcb_atom_t atom
)
11078 #ifdef SWM_DEBUG_ATOM_NAMES
11080 * This should be disabled during most debugging since
11081 * xcb_get_* causes an xcb_flush.
11084 xcb_get_atom_name_reply_t
*r
;
11086 r
= xcb_get_atom_name_reply(conn
,
11087 xcb_get_atom_name(conn
, atom
),
11090 len
= xcb_get_atom_name_name_length(r
);
11092 name
= malloc(len
+ 1);
11094 memcpy(name
, xcb_get_atom_name_name(r
), len
);
11108 propertynotify(xcb_property_notify_event_t
*e
)
11110 struct ws_win
*win
;
11111 struct workspace
*ws
;
11115 name
= get_atom_name(e
->atom
);
11116 DNPRINTF(SWM_D_EVENT
, "propertynotify: win %#x, atom: %s(%u), "
11117 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
11121 last_event_time
= e
->time
;
11123 win
= find_window(e
->window
);
11129 if (e
->atom
== a_state
) {
11130 /* State just changed, make sure it gets focused if mapped. */
11131 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
11132 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
11134 win
->state
== SWM_WIN_STATE_REPARENTED
&&
11135 ws
->focus_pending
== win
) {
11136 focus_win(ws
->focus_pending
);
11137 ws
->focus_pending
= NULL
;
11141 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
11142 e
->atom
== XCB_ATOM_WM_NAME
) {
11144 bar_draw(ws
->r
->bar
);
11145 } else if (e
->atom
== a_prot
) {
11146 get_wm_protocols(win
);
11147 } else if (e
->atom
== XCB_ATOM_WM_NORMAL_HINTS
) {
11148 xcb_icccm_get_wm_normal_hints_reply(conn
,
11149 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
11157 reparentnotify(xcb_reparent_notify_event_t
*e
)
11159 struct ws_win
*win
;
11161 DNPRINTF(SWM_D_EVENT
, "reparentnotify: event: %#x, win %#x, "
11162 "parent: %#x, (x,y): (%u,%u), override_redirect: %u\n",
11163 e
->event
, e
->window
, e
->parent
, e
->x
, e
->y
, e
->override_redirect
);
11165 win
= find_window(e
->window
);
11167 if (win
->state
== SWM_WIN_STATE_REPARENTING
) {
11168 win
->state
= SWM_WIN_STATE_REPARENTED
;
11170 if (win
->ws
->r
&& !ICONIC(win
))
11175 update_window(win
);
11176 update_win_stacking(win
);
11177 } else if (win
->state
== SWM_WIN_STATE_UNPARENTING
) {
11178 win
->state
= SWM_WIN_STATE_UNPARENTED
;
11184 unmapnotify(xcb_unmap_notify_event_t
*e
)
11186 struct ws_win
*win
;
11187 struct workspace
*ws
;
11189 DNPRINTF(SWM_D_EVENT
, "unmapnotify: win %#x\n", e
->window
);
11191 /* If we aren't managing the window, then ignore. */
11192 win
= find_window(e
->window
);
11193 if (win
== NULL
|| win
->id
!= e
->window
) {
11194 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore unmanaged.\n");
11198 /* Do nothing if already withdrawn. */
11199 if (!win
->mapped
&& !ICONIC(win
)) {
11200 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore withdrawn.\n");
11205 win
->mapped
= false;
11207 /* Ignore if reparenting-related. */
11208 if (win
->state
!= SWM_WIN_STATE_REPARENTED
) {
11209 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore not reparented.\n");
11213 /* If win was focused, make sure to focus on something else. */
11214 if (win
== ws
->focus
) {
11215 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
11216 ws
->focus_pending
= get_focus_prev(win
);
11217 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
11218 "focus_pending: %#x\n",
11219 WINID(ws
->focus_pending
));
11227 DNPRINTF(SWM_D_EVENT
, "unmapnotify: iconify.\n");
11228 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
11231 DNPRINTF(SWM_D_EVENT
, "unmapnotify: withdraw.\n");
11232 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
11233 unmanage_window(win
);
11238 /* Update focus if ws is active. */
11239 if (WS_FOCUSED(ws
)) {
11240 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
11241 focus_win(get_pointer_win(ws
->r
->s
->root
));
11242 } else if (ws
->focus_pending
) {
11243 focus_win(ws
->focus_pending
);
11244 ws
->focus_pending
= NULL
;
11245 } else if (ws
->focus
== NULL
) {
11246 DNPRINTF(SWM_D_FOCUS
, "unmapnotify: set_input_focus: "
11247 "%#x, revert-to: parent, time: 0\n",
11249 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11250 ws
->r
->id
, XCB_CURRENT_TIME
);
11254 center_pointer(ws
->r
);
11256 DNPRINTF(SWM_D_EVENT
, "unmapnotify: done.\n");
11261 get_source_type_label(uint32_t type
)
11266 case EWMH_SOURCE_TYPE_NONE
:
11269 case EWMH_SOURCE_TYPE_NORMAL
:
11272 case EWMH_SOURCE_TYPE_OTHER
:
11284 clientmessage(xcb_client_message_event_t
*e
)
11286 struct ws_win
*win
;
11287 struct swm_region
*r
= NULL
;
11290 int num_screens
, i
;
11291 xcb_map_request_event_t mre
;
11295 name
= get_atom_name(e
->type
);
11296 DNPRINTF(SWM_D_EVENT
, "clientmessage: win %#x, atom: %s(%u)\n",
11297 e
->window
, name
, e
->type
);
11301 if (e
->type
== ewmh
[_NET_CURRENT_DESKTOP
].atom
) {
11302 num_screens
= get_screen_count();
11303 for (i
= 0; i
< num_screens
; i
++)
11304 if (screens
[i
].root
== e
->window
) {
11305 r
= screens
[i
].r_focus
;
11309 if (r
&& e
->data
.data32
[0] < (uint32_t)workspace_limit
) {
11310 a
.id
= e
->data
.data32
[0];
11311 switchws(NULL
, r
, &a
);
11316 } else if (e
->type
== ewmh
[_NET_REQUEST_FRAME_EXTENTS
].atom
) {
11317 DNPRINTF(SWM_D_EVENT
,
11318 "clientmessage: set _NET_FRAME_EXTENTS on window.\n");
11319 vals
[0] = vals
[1] = vals
[2] = vals
[3] = border_width
;
11320 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, e
->window
,
11321 a_net_frame_extents
, XCB_ATOM_CARDINAL
, 32, 4, vals
);
11326 win
= find_window(e
->window
);
11328 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11329 /* Manage the window with maprequest. */
11330 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
11331 "unmanaged window.\n");
11332 mre
.window
= e
->window
;
11338 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11339 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW, "
11340 "source_type: %s(%d)\n",
11341 get_source_type_label(e
->data
.data32
[0]),
11342 e
->data
.data32
[0]);
11345 * Allow focus changes that are a result of direct user
11346 * action and from applications that use the old EWMH spec.
11348 if (e
->data
.data32
[0] != EWMH_SOURCE_TYPE_NORMAL
||
11349 win
->quirks
& SWM_Q_OBEYAPPFOCUSREQ
) {
11350 if (WS_FOCUSED(win
->ws
))
11353 win
->ws
->focus_pending
= win
;
11355 } else if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
11356 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
11357 if (win
->can_delete
)
11358 client_msg(win
, a_delete
, 0);
11360 xcb_kill_client(conn
, win
->id
);
11361 } else if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
11362 DNPRINTF(SWM_D_EVENT
,
11363 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
11365 if (e
->data
.data32
[0] & (1<<8)) /* x */
11366 X(win
) = e
->data
.data32
[1];
11367 if (e
->data
.data32
[0] & (1<<9)) /* y */
11368 Y(win
) = e
->data
.data32
[2];
11369 if (e
->data
.data32
[0] & (1<<10)) /* width */
11370 WIDTH(win
) = e
->data
.data32
[3];
11371 if (e
->data
.data32
[0] & (1<<11)) /* height */
11372 HEIGHT(win
) = e
->data
.data32
[4];
11374 update_window(win
);
11376 /* Notify no change was made. */
11377 config_win(win
, NULL
);
11378 /* TODO: Change stack sizes */
11380 } else if (e
->type
== ewmh
[_NET_RESTACK_WINDOW
].atom
) {
11381 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_RESTACK_WINDOW\n");
11382 vals
[0] = e
->data
.data32
[1]; /* Sibling window. */
11383 vals
[1] = e
->data
.data32
[2]; /* Stack mode detail. */
11385 if (win
->frame
!= XCB_WINDOW_NONE
)
11386 xcb_configure_window(conn
, win
->frame
,
11387 XCB_CONFIG_WINDOW_SIBLING
|
11388 XCB_CONFIG_WINDOW_STACK_MODE
, vals
);
11389 } else if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
11390 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
11391 ewmh_change_wm_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
11392 if (e
->data
.data32
[2])
11393 ewmh_change_wm_state(win
, e
->data
.data32
[2],
11394 e
->data
.data32
[0]);
11396 ewmh_update_wm_state(win
);
11398 } else if (e
->type
== ewmh
[_NET_WM_DESKTOP
].atom
) {
11399 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_DESKTOP\n");
11402 win_to_ws(win
, e
->data
.data32
[0], true);
11404 /* Stack source and destination ws, if mapped. */
11405 if (r
!= win
->ws
->r
) {
11411 load_float_geom(win
);
11424 int errcode
= xcb_connection_has_error(conn
);
11425 #ifdef XCB_CONN_ERROR
11428 case XCB_CONN_ERROR
:
11429 s
= "Socket error, pipe error or other stream error.";
11431 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
11432 s
= "Extension not supported.";
11434 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
11435 s
= "Insufficient memory.";
11437 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
11438 s
= "Request length was exceeded.";
11440 case XCB_CONN_CLOSED_PARSE_ERR
:
11441 s
= "Error parsing display string.";
11444 s
= "Unknown error.";
11447 errx(errcode
, "X CONNECTION ERROR: %s", s
);
11450 errx(errcode
, "X CONNECTION ERROR");
11457 int num_screens
, i
;
11458 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
11459 XCB_EVENT_MASK_ENTER_WINDOW
|
11460 XCB_EVENT_MASK_OWNER_GRAB_BUTTON
|
11461 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
11462 XCB_EVENT_MASK_ENTER_WINDOW
|
11463 XCB_EVENT_MASK_LEAVE_WINDOW
|
11464 XCB_EVENT_MASK_BUTTON_PRESS
|
11465 XCB_EVENT_MASK_BUTTON_RELEASE
|
11466 XCB_EVENT_MASK_KEY_PRESS
|
11467 XCB_EVENT_MASK_KEY_RELEASE
|
11468 XCB_EVENT_MASK_PROPERTY_CHANGE
;
11470 xcb_void_cookie_t wac
;
11471 xcb_generic_error_t
*error
;
11473 /* this causes an error if some other window manager is running */
11474 num_screens
= get_screen_count();
11475 for (i
= 0; i
< num_screens
; i
++) {
11476 if ((sc
= get_screen(i
)) == NULL
)
11477 errx(1, "ERROR: can't get screen %d.", i
);
11478 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: %#x\n",
11480 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
11481 XCB_CW_EVENT_MASK
, &val
);
11482 if ((error
= xcb_request_check(conn
, wac
))) {
11483 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
11484 error
->error_code
);
11494 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
11496 struct swm_region
*r
= NULL
, *n
;
11497 struct workspace
*ws
= NULL
;
11501 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
11502 s
->idx
, w
, h
, x
, y
);
11504 /* remove any conflicting regions */
11505 n
= TAILQ_FIRST(&s
->rl
);
11508 n
= TAILQ_NEXT(r
, entry
);
11509 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
11510 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
11511 if (r
->ws
->r
!= NULL
)
11512 r
->ws
->old_r
= r
->ws
->r
;
11515 xcb_destroy_window(conn
, r
->id
);
11516 r
->id
= XCB_WINDOW_NONE
;
11517 TAILQ_REMOVE(&s
->rl
, r
, entry
);
11518 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
11522 /* search old regions for one to reuse */
11524 /* size + location match */
11525 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11526 if (X(r
) == x
&& Y(r
) == y
&&
11527 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11531 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11532 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11536 TAILQ_REMOVE(&s
->orl
, r
, entry
);
11537 /* try to use old region's workspace */
11538 if (r
->ws
->r
== NULL
)
11541 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
11542 err(1, "new_region: calloc: failed to allocate memory "
11545 /* if we don't have a workspace already, find one */
11547 for (i
= 0; i
< workspace_limit
; i
++)
11548 if (s
->ws
[i
].r
== NULL
) {
11555 errx(1, "new_region: no free workspaces");
11557 if (ws
->state
== SWM_WS_STATE_HIDDEN
)
11558 ws
->state
= SWM_WS_STATE_MAPPING
;
11567 r
->ws_prior
= NULL
;
11570 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
11572 /* Invisible region window to detect pointer events on empty regions. */
11573 r
->id
= xcb_generate_id(conn
);
11574 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
11575 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
11577 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
11578 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
11579 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
11581 /* Make sure region input is at the bottom. */
11582 wa
[0] = XCB_STACK_MODE_BELOW
;
11583 xcb_configure_window(conn
, r
->id
, XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
11585 xcb_map_window(conn
, r
->id
);
11589 scan_randr(int idx
)
11591 #ifdef SWM_XRR_HAS_CRTC
11594 #endif /* SWM_XRR_HAS_CRTC */
11595 struct swm_region
*r
;
11597 xcb_randr_get_screen_resources_current_cookie_t src
;
11598 xcb_randr_get_screen_resources_current_reply_t
*srr
;
11599 xcb_randr_get_crtc_info_cookie_t cic
;
11600 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
11601 xcb_randr_crtc_t
*crtc
;
11602 xcb_screen_t
*screen
;
11604 DNPRINTF(SWM_D_MISC
, "scan_randr: screen: %d\n", idx
);
11606 if ((screen
= get_screen(idx
)) == NULL
)
11607 errx(1, "ERROR: can't get screen %d.", idx
);
11609 num_screens
= get_screen_count();
11610 if (idx
>= num_screens
)
11611 errx(1, "scan_randr: invalid screen");
11613 /* remove any old regions */
11614 while ((r
= TAILQ_FIRST(&screens
[idx
].rl
)) != NULL
) {
11615 r
->ws
->old_r
= r
->ws
->r
= NULL
;
11617 xcb_destroy_window(conn
, r
->id
);
11618 r
->id
= XCB_WINDOW_NONE
;
11619 TAILQ_REMOVE(&screens
[idx
].rl
, r
, entry
);
11620 TAILQ_INSERT_TAIL(&screens
[idx
].orl
, r
, entry
);
11624 /* map virtual screens onto physical screens */
11625 #ifdef SWM_XRR_HAS_CRTC
11626 if (randr_support
) {
11627 src
= xcb_randr_get_screen_resources_current(conn
,
11628 screens
[idx
].root
);
11629 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
11632 new_region(&screens
[idx
], 0, 0,
11633 screen
->width_in_pixels
,
11634 screen
->height_in_pixels
);
11637 ncrtc
= srr
->num_crtcs
;
11639 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
11640 for (c
= 0; c
< ncrtc
; c
++) {
11641 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
11643 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
11646 if (cir
->num_outputs
== 0) {
11651 if (cir
->mode
== 0)
11652 new_region(&screens
[idx
], 0, 0,
11653 screen
->width_in_pixels
,
11654 screen
->height_in_pixels
);
11656 new_region(&screens
[idx
],
11657 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
11662 #endif /* SWM_XRR_HAS_CRTC */
11664 /* If detection failed, create a single region that spans the screen. */
11665 if (TAILQ_EMPTY(&screens
[idx
].rl
))
11666 new_region(&screens
[idx
], 0, 0, screen
->width_in_pixels
,
11667 screen
->height_in_pixels
);
11670 /* The screen shouldn't focus on unused regions. */
11671 TAILQ_FOREACH(r
, &screens
[idx
].orl
, entry
) {
11672 if (screens
[idx
].r_focus
== r
)
11673 screens
[idx
].r_focus
= NULL
;
11676 DNPRINTF(SWM_D_MISC
, "scan_randr: done.\n");
11680 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
11682 struct swm_region
*r
;
11683 struct workspace
*ws
;
11684 struct ws_win
*win
;
11685 int i
, j
, num_screens
;
11687 DNPRINTF(SWM_D_EVENT
, "screenchange: root: %#x\n", e
->root
);
11689 num_screens
= get_screen_count();
11690 /* silly event doesn't include the screen index */
11691 for (i
= 0; i
< num_screens
; i
++)
11692 if (screens
[i
].root
== e
->root
)
11694 if (i
>= num_screens
)
11695 errx(1, "screenchange: screen not found");
11697 /* brute force for now, just re-enumerate the regions */
11701 print_win_geom(e
->root
);
11703 /* add bars to all regions */
11704 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11707 /* Stack all regions. */
11708 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11711 /* Make sure a region has focus. */
11712 if (screens
[i
].r_focus
== NULL
) {
11713 r
= TAILQ_FIRST(&screens
[i
].rl
);
11718 /* Cleanup unused previously visible workspaces. */
11719 for (j
= 0; j
< workspace_limit
; j
++) {
11720 ws
= &screens
[i
].ws
[j
];
11721 if (ws
->r
== NULL
&& ws
->state
!= SWM_WS_STATE_HIDDEN
) {
11722 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
11724 ws
->state
= SWM_WS_STATE_HIDDEN
;
11730 /* Update workspace state and bar on all regions. */
11731 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11732 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
11740 struct swm_region
*r
= NULL
;
11741 xcb_query_tree_cookie_t qtc
;
11742 xcb_query_tree_reply_t
*qtr
;
11743 xcb_get_property_cookie_t pc
;
11744 xcb_get_property_reply_t
*pr
;
11745 xcb_window_t
*wins
= NULL
, trans
, *cwins
= NULL
;
11746 int i
, j
, k
, n
, no
, num_screens
;
11748 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
11749 num_screens
= get_screen_count();
11750 for (i
= 0; i
< num_screens
; i
++) {
11751 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
11752 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
11755 wins
= xcb_query_tree_children(qtr
);
11756 no
= xcb_query_tree_children_length(qtr
);
11758 /* Try to sort windows according to _NET_CLIENT_LIST. */
11759 pr
= xcb_get_property_reply(conn
, xcb_get_property(conn
, 0,
11760 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
11761 XCB_ATOM_WINDOW
, 0, UINT32_MAX
), NULL
);
11763 cwins
= xcb_get_property_value(pr
);
11764 n
= xcb_get_property_value_length(pr
) /
11765 sizeof(xcb_atom_t
);
11767 for (j
= 0; j
< n
; ++j
) {
11768 for (k
= j
; k
< no
; ++k
) {
11769 if (wins
[k
] == cwins
[j
]) {
11770 /* Swap wins j and k. */
11772 wins
[j
] = cwins
[j
];
11780 /* Manage top-level windows first, then transients. */
11781 /* TODO: allow transients to be managed before leader. */
11782 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top-level windows.\n");
11783 for (j
= 0; j
< no
; j
++) {
11784 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11785 if (r
->id
== wins
[j
]) {
11786 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11787 "skip %#x; region input window.\n",
11790 } else if (r
->bar
->id
== wins
[j
]) {
11791 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11792 "skip %#x; region bar.\n",
11801 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11802 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11804 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
11805 "is transient for %#x.\n", wins
[j
], trans
);
11809 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11812 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
11813 for (j
= 0; j
< no
; j
++) {
11814 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11815 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11817 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11821 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
11825 setup_screens(void)
11827 int i
, j
, k
, num_screens
;
11828 struct workspace
*ws
;
11829 uint32_t gcv
[1], wa
[1];
11830 const xcb_query_extension_reply_t
*qep
;
11831 xcb_screen_t
*screen
;
11832 xcb_randr_query_version_cookie_t c
;
11833 xcb_randr_query_version_reply_t
*r
;
11835 num_screens
= get_screen_count();
11836 if ((screens
= calloc(num_screens
, sizeof(struct swm_screen
))) == NULL
)
11837 err(1, "setup_screens: calloc: failed to allocate memory for "
11840 /* Initial RandR setup. */
11841 randr_support
= false;
11842 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
11843 if (qep
->present
) {
11844 c
= xcb_randr_query_version(conn
, 1, 1);
11845 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
11847 if (r
->major_version
>= 1) {
11848 randr_support
= true;
11849 randr_eventbase
= qep
->first_event
;
11855 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
11857 /* map physical screens */
11858 for (i
= 0; i
< num_screens
; i
++) {
11859 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
11860 screens
[i
].idx
= i
;
11861 screens
[i
].r_focus
= NULL
;
11863 TAILQ_INIT(&screens
[i
].rl
);
11864 TAILQ_INIT(&screens
[i
].orl
);
11865 if ((screen
= get_screen(i
)) == NULL
)
11866 errx(1, "ERROR: can't get screen %d.", i
);
11867 screens
[i
].root
= screen
->root
;
11869 /* set default colors */
11870 setscreencolor("red", i
, SWM_S_COLOR_FOCUS
);
11871 setscreencolor("rgb:88/88/88", i
, SWM_S_COLOR_UNFOCUS
);
11872 setscreencolor("rgb:00/80/80", i
, SWM_S_COLOR_BAR_BORDER
);
11873 setscreencolor("rgb:00/40/40", i
,
11874 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
11875 setscreencolor("black", i
, SWM_S_COLOR_BAR
);
11876 setscreencolor("rgb:a0/a0/a0", i
, SWM_S_COLOR_BAR_FONT
);
11877 setscreencolor("red", i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
11878 setscreencolor("rgb:88/88/88", i
,
11879 SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
11881 /* create graphics context on screen */
11882 screens
[i
].bar_gc
= xcb_generate_id(conn
);
11884 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
11885 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
11887 /* set default cursor */
11888 xcb_change_window_attributes(conn
, screens
[i
].root
,
11889 XCB_CW_CURSOR
, wa
);
11891 /* init all workspaces */
11892 /* XXX these should be dynamically allocated too */
11893 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
11894 ws
= &screens
[i
].ws
[j
];
11897 ws
->bar_enabled
= true;
11899 ws
->focus_prev
= NULL
;
11900 ws
->focus_pending
= NULL
;
11901 ws
->focus_raise
= NULL
;
11904 ws
->state
= SWM_WS_STATE_HIDDEN
;
11905 TAILQ_INIT(&ws
->stack
);
11906 TAILQ_INIT(&ws
->winlist
);
11907 TAILQ_INIT(&ws
->unmanagedlist
);
11909 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
11910 if (layouts
[k
].l_config
!= NULL
)
11911 layouts
[k
].l_config(ws
,
11912 SWM_ARG_ID_STACKINIT
);
11913 ws
->cur_layout
= &layouts
[0];
11914 ws
->cur_layout
->l_string(ws
);
11920 xcb_randr_select_input(conn
, screens
[i
].root
,
11921 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
11926 setup_globals(void)
11928 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
11929 err(1, "setup_globals: strdup: failed to allocate memory.");
11931 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
11932 err(1, "setup_globals: strdup: failed to allocate memory.");
11934 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
11935 errx(1, "unable to allocate key symbols");
11937 a_state
= get_atom_from_string("WM_STATE");
11938 a_prot
= get_atom_from_string("WM_PROTOCOLS");
11939 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
11940 a_net_frame_extents
= get_atom_from_string("_NET_FRAME_EXTENTS");
11941 a_net_supported
= get_atom_from_string("_NET_SUPPORTED");
11942 a_net_wm_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
11943 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
11944 a_utf8_string
= get_atom_from_string("UTF8_STRING");
11945 a_swm_ws
= get_atom_from_string("_SWM_WS");
11949 shutdown_cleanup(void)
11951 struct swm_region
*r
;
11953 struct workspace
*ws
;
11954 int i
, num_screens
;
11956 /* disable alarm because the following code may not be interrupted */
11958 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
11959 err(1, "can't disable alarm");
11972 num_screens
= get_screen_count();
11973 for (i
= 0; i
< num_screens
; ++i
) {
11976 DNPRINTF(SWM_D_FOCUS
, "shutdown_cleanup: set_input_focus: "
11977 "%#x, revert-to: root, time: 0\n", screens
[i
].root
);
11978 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11979 screens
[i
].root
, XCB_CURRENT_TIME
);
11981 if (screens
[i
].bar_gc
!= XCB_NONE
)
11982 xcb_free_gc(conn
, screens
[i
].bar_gc
);
11983 if (!bar_font_legacy
) {
11984 XftColorFree(display
, DefaultVisual(display
, i
),
11985 DefaultColormap(display
, i
), &bar_font_color
);
11986 XftColorFree(display
, DefaultVisual(display
, i
),
11987 DefaultColormap(display
, i
), &search_font_color
);
11990 for (j
= 0; j
< SWM_S_COLOR_MAX
; ++j
) {
11991 free(screens
[i
].c
[j
].name
);
11994 /* Free window memory. */
11995 for (j
= 0; j
< SWM_WS_MAX
; ++j
) {
11996 ws
= &screens
[i
].ws
[j
];
11999 while ((w
= TAILQ_FIRST(&ws
->winlist
)) != NULL
) {
12000 TAILQ_REMOVE(&ws
->winlist
, w
, entry
);
12004 while ((w
= TAILQ_FIRST(&ws
->unmanagedlist
)) != NULL
) {
12005 TAILQ_REMOVE(&ws
->unmanagedlist
, w
, entry
);
12010 /* Free region memory. */
12011 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
12012 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
12017 while ((r
= TAILQ_FIRST(&screens
[i
].orl
)) != NULL
) {
12018 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
12027 free(clock_format
);
12028 free(startup_exception
);
12031 XFreeFontSet(display
, bar_fs
);
12033 XftFontClose(display
, bar_font
);
12035 xcb_key_symbols_free(syms
);
12037 xcb_aux_sync(conn
);
12038 xcb_disconnect(conn
);
12042 event_error(xcb_generic_error_t
*e
)
12046 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
12047 "resource_id: %u, minor_code: %u\n",
12048 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
12049 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
12050 e
->sequence
, e
->resource_id
, e
->minor_code
);
12054 event_handle(xcb_generic_event_t
*evt
)
12056 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
12058 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
12059 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
12060 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
12063 #define EVENT(type, callback) case type: callback((void *)evt); return
12064 EVENT(0, event_error
);
12065 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
12066 EVENT(XCB_BUTTON_RELEASE
, buttonrelease
);
12067 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
12068 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
12069 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
12070 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
12071 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
12072 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
12073 /*EVENT(XCB_CREATE_NOTIFY, );*/
12074 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
12075 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
12076 EVENT(XCB_EXPOSE
, expose
);
12077 EVENT(XCB_FOCUS_IN
, focusin
);
12079 EVENT(XCB_FOCUS_OUT
, focusout
);
12081 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
12082 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
12083 EVENT(XCB_KEY_PRESS
, keypress
);
12084 EVENT(XCB_KEY_RELEASE
, keyrelease
);
12085 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
12087 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
12089 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
12090 EVENT(XCB_MAP_REQUEST
, maprequest
);
12091 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
12092 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
12093 /*EVENT(XCB_NO_EXPOSURE, );*/
12094 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
12095 EVENT(XCB_REPARENT_NOTIFY
, reparentnotify
);
12096 /*EVENT(XCB_RESIZE_REQUEST, );*/
12097 /*EVENT(XCB_SELECTION_CLEAR, );*/
12098 /*EVENT(XCB_SELECTION_NOTIFY, );*/
12099 /*EVENT(XCB_SELECTION_REQUEST, );*/
12100 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
12101 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
12104 if (type
- randr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
12105 screenchange((void *)evt
);
12109 main(int argc
, char *argv
[])
12111 struct pollfd pfd
[2];
12112 struct sigaction sact
;
12114 struct passwd
*pwd
;
12115 struct swm_region
*r
;
12116 xcb_generic_event_t
*evt
;
12117 int xfd
, i
, num_screens
, num_readable
;
12118 char conf
[PATH_MAX
], *cfile
= NULL
;
12119 bool stdin_ready
= false, startup
= true;
12121 /* suppress unused warning since var is needed */
12125 time_started
= time(NULL
);
12129 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
12130 if (setlocale(LC_CTYPE
, "") == NULL
|| setlocale(LC_TIME
, "") == NULL
)
12131 warnx("no locale support");
12133 /* handle some signals */
12134 bzero(&sact
, sizeof(sact
));
12135 sigemptyset(&sact
.sa_mask
);
12137 sact
.sa_handler
= sighdlr
;
12138 sigaction(SIGINT
, &sact
, NULL
);
12139 sigaction(SIGQUIT
, &sact
, NULL
);
12140 sigaction(SIGTERM
, &sact
, NULL
);
12141 sigaction(SIGHUP
, &sact
, NULL
);
12143 sact
.sa_handler
= sighdlr
;
12144 sact
.sa_flags
= SA_NOCLDSTOP
;
12145 sigaction(SIGCHLD
, &sact
, NULL
);
12147 if ((display
= XOpenDisplay(0)) == NULL
)
12148 errx(1, "can not open display");
12150 conn
= XGetXCBConnection(display
);
12151 if (xcb_connection_has_error(conn
))
12152 errx(1, "can not get XCB connection");
12154 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
12156 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
12157 xfd
= xcb_get_file_descriptor(conn
);
12159 /* look for local and global conf file */
12160 pwd
= getpwuid(getuid());
12162 errx(1, "invalid user: %d", getuid());
12164 xcb_grab_server(conn
);
12165 xcb_aux_sync(conn
);
12167 /* flush all events */
12168 while ((evt
= get_next_event(false))) {
12169 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
12175 errx(1, "another window manager is currently running");
12177 /* Load Xcursors and/or cursorfont glyph cursors. */
12180 xcb_aux_sync(conn
);
12185 setup_keybindings();
12186 setup_btnbindings();
12191 for (i
= 0; ; i
++) {
12196 snprintf(conf
, sizeof conf
, "%s/.%s",
12197 pwd
->pw_dir
, SWM_CONF_FILE
);
12201 snprintf(conf
, sizeof conf
, "/etc/%s",
12206 snprintf(conf
, sizeof conf
, "%s/.%s",
12207 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
12210 /* global compat */
12211 snprintf(conf
, sizeof conf
, "/etc/%s",
12212 SWM_CONF_FILE_OLD
);
12218 if (strlen(conf
) && stat(conf
, &sb
) != -1)
12219 if (S_ISREG(sb
.st_mode
)) {
12226 /* load conf (if any) */
12228 conf_load(cfile
, SWM_CONF_DEFAULT
);
12232 if (getenv("SWM_STARTED") == NULL
)
12233 setenv("SWM_STARTED", "YES", 1);
12235 /* setup all bars */
12236 num_screens
= get_screen_count();
12237 for (i
= 0; i
< num_screens
; i
++)
12238 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12241 /* Manage existing windows. */
12247 /* Stack all regions to trigger mapping. */
12248 for (i
= 0; i
< num_screens
; i
++)
12249 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12252 xcb_ungrab_server(conn
);
12255 /* Update state and bar of each newly mapped workspace. */
12256 for (i
= 0; i
< num_screens
; i
++)
12257 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
12258 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
12262 memset(&pfd
, 0, sizeof(pfd
));
12264 pfd
[0].events
= POLLIN
;
12265 pfd
[1].fd
= STDIN_FILENO
;
12266 pfd
[1].events
= POLLIN
;
12269 while ((evt
= get_next_event(false))) {
12276 /* If just (re)started, set default focus if needed. */
12280 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
12281 r
= TAILQ_FIRST(&screens
[0].rl
);
12293 num_readable
= poll(pfd
, bar_extra
? 2 : 1, 1000);
12294 if (num_readable
== -1) {
12295 DNPRINTF(SWM_D_MISC
, "poll failed: %s",
12297 } else if (num_readable
> 0 && bar_extra
&&
12298 pfd
[1].revents
& POLLIN
) {
12299 stdin_ready
= true;
12303 restart(NULL
, NULL
, NULL
);
12309 stdin_ready
= false;
12310 bar_extra_update();
12313 /* Need to ensure the bar(s) are always updated. */
12314 for (i
= 0; i
< num_screens
; i
++)
12315 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12321 shutdown_cleanup();