2 * Copyright (c) 2009-2015 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2016 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 * Copyright (c) 2012-2015 David Hill <dhill@mindcry.org>
12 * Copyright (c) 2014-2015 Yuri D'Elia <yuri.delia@eurac.edu>
14 * Permission to use, copy, modify, and distribute this software for any
15 * purpose with or without fee is hereby granted, provided that the above
16 * copyright notice and this permission notice appear in all copies.
18 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
19 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
21 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
23 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
24 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 #include <sys/types.h>
35 #include <sys/queue.h>
37 #include <sys/param.h>
38 #include <sys/select.h>
39 #if defined(__linux__)
41 #elif defined(__OpenBSD__)
43 #elif defined(__FreeBSD__)
67 #include <X11/cursorfont.h>
68 #include <X11/extensions/Xrandr.h>
69 #include <X11/Xcursor/Xcursor.h>
70 #include <X11/Xft/Xft.h>
71 #include <X11/Xlib-xcb.h>
73 #include <xcb/xcb_atom.h>
74 #include <xcb/xcb_aux.h>
75 #include <xcb/xcb_event.h>
76 #include <xcb/xcb_icccm.h>
77 #include <xcb/xcb_keysyms.h>
78 #include <xcb/xtest.h>
79 #include <xcb/randr.h>
87 #ifdef SPECTRWM_BUILDSTR
88 static const char *buildstr
= SPECTRWM_BUILDSTR
;
90 static const char *buildstr
= SPECTRWM_VERSION
;
93 #if !defined(__CYGWIN__) /* cygwin chokes on randr stuff */
95 # error RandR versions less than 1.0 are not supported
100 # define SWM_XRR_HAS_CRTC
103 #endif /* __CYGWIN__ */
105 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
106 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
107 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
108 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
109 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
110 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
111 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
112 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
113 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
114 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
115 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
116 #define xcb_icccm_get_wm_class xcb_get_wm_class
117 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
118 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
119 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
120 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
121 #define xcb_icccm_wm_hints_get_urgency xcb_wm_hints_get_urgency
122 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
123 #define xcb_icccm_get_wm_name xcb_get_wm_name
124 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
125 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
126 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
127 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
128 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
129 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
130 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
131 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
132 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
133 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
136 /*#define SWM_DEBUG*/
138 #define DPRINTF(x...) do { \
140 fprintf(stderr, x); \
142 #define DNPRINTF(n,x...) do { \
143 if (swm_debug & n) { \
144 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
145 fprintf(stderr, x); \
148 #define SWM_D_MISC 0x0001
149 #define SWM_D_EVENT 0x0002
150 #define SWM_D_WS 0x0004
151 #define SWM_D_FOCUS 0x0008
152 #define SWM_D_MOVE 0x0010
153 #define SWM_D_STACK 0x0020
154 #define SWM_D_MOUSE 0x0040
155 #define SWM_D_PROP 0x0080
156 #define SWM_D_CLASS 0x0100
157 #define SWM_D_KEY 0x0200
158 #define SWM_D_QUIRK 0x0400
159 #define SWM_D_SPAWN 0x0800
160 #define SWM_D_EVENTQ 0x1000
161 #define SWM_D_CONF 0x2000
162 #define SWM_D_BAR 0x4000
163 #define SWM_D_INIT 0x8000
165 uint32_t swm_debug
= 0
184 #define DPRINTF(x...)
185 #define DNPRINTF(n,x...)
188 #define SWM_EWMH_ACTION_COUNT_MAX (8)
189 #define EWMH_F_FULLSCREEN (0x001)
190 #define EWMH_F_ABOVE (0x002)
191 #define EWMH_F_HIDDEN (0x004)
192 #define EWMH_F_MAXIMIZED_VERT (0x008)
193 #define EWMH_F_MAXIMIZED_HORZ (0x010)
194 #define EWMH_F_SKIP_PAGER (0x020)
195 #define EWMH_F_SKIP_TASKBAR (0x040)
196 #define SWM_F_MANUAL (0x080)
198 #define EWMH_F_MAXIMIZED (EWMH_F_MAXIMIZED_VERT | EWMH_F_MAXIMIZED_HORZ)
200 /* convert 8-bit to 16-bit */
201 #define RGB_8_TO_16(col) (((col) << 8) + (col))
203 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
204 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
205 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
206 xrc.blue = RGB_8_TO_16((px) & 0xff); \
209 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
210 #define MODKEY XCB_MOD_MASK_1
211 #define ANYMOD XCB_MOD_MASK_ANY
212 #define CLEANMASK(mask) ((mask) & (XCB_KEY_BUT_MASK_SHIFT | \
213 XCB_KEY_BUT_MASK_CONTROL | XCB_KEY_BUT_MASK_MOD_1 | \
214 XCB_KEY_BUT_MASK_MOD_2 | XCB_KEY_BUT_MASK_MOD_3 | \
215 XCB_KEY_BUT_MASK_MOD_4 | XCB_KEY_BUT_MASK_MOD_5) & ~(numlockmask))
216 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
217 XCB_EVENT_MASK_BUTTON_RELEASE)
218 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
219 #define SWM_PROPLEN (16)
220 #define SWM_FUNCNAME_LEN (32)
221 #define SWM_QUIRK_LEN (64)
222 #define X(r) ((r)->g.x)
223 #define Y(r) ((r)->g.y)
224 #define WIDTH(r) ((r)->g.w)
225 #define HEIGHT(r) ((r)->g.h)
226 #define MAX_X(r) ((r)->g.x + (r)->g.w)
227 #define MAX_Y(r) ((r)->g.y + (r)->g.h)
228 #define SH_MIN(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)
229 #define SH_MIN_W(w) ((w)->sh.min_width)
230 #define SH_MIN_H(w) ((w)->sh.min_height)
231 #define SH_MAX(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
232 #define SH_MAX_W(w) ((w)->sh.max_width)
233 #define SH_MAX_H(w) ((w)->sh.max_height)
234 #define SH_INC(w) ((w)->sh.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)
235 #define SH_INC_W(w) ((w)->sh.width_inc)
236 #define SH_INC_H(w) ((w)->sh.height_inc)
237 #define SWM_MAX_FONT_STEPS (3)
238 #define WINID(w) ((w) ? (w)->id : XCB_WINDOW_NONE)
239 #define ACCEPTS_FOCUS(w) (!((w)->hints.flags & XCB_ICCCM_WM_HINT_INPUT) \
240 || ((w)->hints.input))
241 #define WS_FOCUSED(ws) ((ws)->r && (ws)->r->s->r_focus == (ws)->r)
242 #define YESNO(x) ((x) ? "yes" : "no")
243 #define ICONIC(w) ((w)->ewmh_flags & EWMH_F_HIDDEN)
244 #define ABOVE(w) ((w)->ewmh_flags & EWMH_F_ABOVE)
245 #define FULLSCREEN(w) ((w)->ewmh_flags & EWMH_F_FULLSCREEN)
246 #define MAXIMIZED_VERT(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_VERT)
247 #define MAXIMIZED_HORZ(w) ((w)->ewmh_flags & EWMH_F_MAXIMIZED_HORZ)
248 #define MAXIMIZED(w) (MAXIMIZED_VERT(w) || MAXIMIZED_HORZ(w))
249 #define MANUAL(w) ((w)->ewmh_flags & SWM_F_MANUAL)
250 #define TRANS(w) ((w)->transient != XCB_WINDOW_NONE)
251 #define FLOATING(w) (ABOVE(w) || TRANS(w) || FULLSCREEN(w) || \
254 /* Constrain Window flags */
255 #define SWM_CW_RESIZABLE (0x01)
256 #define SWM_CW_SOFTBOUNDARY (0x02)
257 #define SWM_CW_HARDBOUNDARY (0x04)
258 #define SWM_CW_RIGHT (0x10)
259 #define SWM_CW_LEFT (0x20)
260 #define SWM_CW_BOTTOM (0x40)
261 #define SWM_CW_TOP (0x80)
262 #define SWM_CW_ALLSIDES (0xf0)
264 #define SWM_FOCUS_DEFAULT (0)
265 #define SWM_FOCUS_FOLLOW (1)
266 #define SWM_FOCUS_MANUAL (2)
268 #define SWM_CK_ALL (0xf)
269 #define SWM_CK_FOCUS (0x1)
270 #define SWM_CK_POINTER (0x2)
271 #define SWM_CK_FALLBACK (0x4)
272 #define SWM_CK_REGION (0x8)
274 #define SWM_CONF_DEFAULT (0)
275 #define SWM_CONF_KEYMAPPING (1)
278 #define SWM_LIB "/usr/local/lib/libswmhack.so"
285 xcb_atom_t a_net_frame_extents
;
286 xcb_atom_t a_net_wm_check
;
287 xcb_atom_t a_net_supported
;
288 xcb_atom_t a_takefocus
;
289 xcb_atom_t a_utf8_string
;
291 volatile sig_atomic_t running
= 1;
292 volatile sig_atomic_t restart_wm
= 0;
293 xcb_timestamp_t last_event_time
= 0;
297 unsigned int numlockmask
= 0;
300 xcb_connection_t
*conn
;
301 xcb_key_symbols_t
*syms
;
303 int boundary_width
= 50;
304 bool cycle_empty
= false;
305 bool cycle_visible
= false;
307 int font_adjusted
= 0;
308 uint16_t mod_key
= MODKEY
;
309 bool warp_focus
= false;
310 bool warp_pointer
= false;
311 bool workspace_clamp
= false;
314 struct swm_region
*search_r
;
315 int select_list_pipe
[2];
316 int select_resp_pipe
[2];
318 volatile sig_atomic_t search_resp
;
319 int search_resp_action
;
321 struct search_window
{
322 TAILQ_ENTRY(search_window
) entry
;
326 xcb_window_t indicator
;
328 TAILQ_HEAD(search_winlist
, search_window
);
329 struct search_winlist search_wl
;
334 SWM_SEARCH_UNICONIFY
,
335 SWM_SEARCH_NAME_WORKSPACE
,
336 SWM_SEARCH_SEARCH_WORKSPACE
,
337 SWM_SEARCH_SEARCH_WINDOW
340 #define SWM_STACK_TOP (0)
341 #define SWM_STACK_BOTTOM (1)
342 #define SWM_STACK_ABOVE (2)
343 #define SWM_STACK_BELOW (3)
346 double dialog_ratio
= 0.6;
348 #define SWM_BAR_MAX (256)
349 #define SWM_BAR_JUSTIFY_LEFT (0)
350 #define SWM_BAR_JUSTIFY_CENTER (1)
351 #define SWM_BAR_JUSTIFY_RIGHT (2)
352 #define SWM_BAR_OFFSET (4)
353 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
354 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
355 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
356 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
357 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
359 #ifdef X_HAVE_UTF8_STRING
360 #define DRAWSTRING(x...) Xutf8DrawString(x)
362 #define DRAWSTRING(x...) XmbDrawString(x)
365 char *bar_argv
[] = { NULL
, NULL
};
367 char bar_ext
[SWM_BAR_MAX
];
368 char bar_ext_buf
[SWM_BAR_MAX
];
369 char bar_vertext
[SWM_BAR_MAX
];
370 bool bar_version
= false;
371 bool bar_enabled
= true;
372 int bar_border_width
= 1;
373 bool bar_at_bottom
= false;
374 bool bar_extra
= false;
376 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
377 char *bar_format
= NULL
;
378 bool stack_enabled
= true;
379 bool clock_enabled
= true;
380 bool iconic_enabled
= false;
381 bool maximize_hide_bar
= false;
382 bool urgent_enabled
= false;
383 bool urgent_collapse
= false;
384 char *clock_format
= NULL
;
385 bool window_class_enabled
= false;
386 bool window_instance_enabled
= false;
387 bool window_name_enabled
= false;
388 int focus_mode
= SWM_FOCUS_DEFAULT
;
389 int focus_close
= SWM_STACK_BELOW
;
390 bool focus_close_wrap
= true;
391 int focus_default
= SWM_STACK_TOP
;
392 int spawn_position
= SWM_STACK_TOP
;
393 bool disable_border
= false;
394 int border_width
= 1;
395 int region_padding
= 0;
397 bool java_workaround
= true;
398 bool verbose_layout
= false;
404 XFontSet bar_fs
= NULL
;
405 XFontSetExtents
*bar_fs_extents
;
406 XftFont
*bar_font
= NULL
;
407 bool bar_font_legacy
= true;
408 char *bar_fonts
= NULL
;
409 XftColor bar_font_color
;
410 XftColor search_font_color
;
411 char *startup_exception
= NULL
;
412 unsigned int nr_exceptions
= 0;
414 /* layout manager data */
415 struct swm_geometry
{
428 struct swm_geometry g
;
429 struct swm_region
*r
; /* Associated region. */
432 /* virtual "screens" */
434 TAILQ_ENTRY(swm_region
) entry
;
436 struct swm_geometry g
;
437 struct workspace
*ws
; /* current workspace on this region */
438 struct workspace
*ws_prior
; /* prior workspace on this region */
439 struct swm_screen
*s
; /* screen idx */
442 TAILQ_HEAD(swm_region_list
, swm_region
);
445 SWM_WIN_STATE_REPARENTING
,
446 SWM_WIN_STATE_REPARENTED
,
447 SWM_WIN_STATE_UNPARENTING
,
448 SWM_WIN_STATE_UNPARENTED
,
452 TAILQ_ENTRY(ws_win
) entry
;
453 TAILQ_ENTRY(ws_win
) stack_entry
;
456 xcb_window_t transient
;
457 struct ws_win
*focus_child
; /* focus on child transient */
458 struct swm_geometry g
; /* current geometry */
459 struct swm_geometry g_prev
; /* prev configured geometry */
460 struct swm_geometry g_float
; /* region coordinates */
461 bool g_floatvalid
; /* g_float geometry validity */
466 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
473 struct workspace
*ws
; /* always valid */
474 struct swm_screen
*s
; /* always valid, never changes */
476 xcb_icccm_get_wm_class_reply_t ch
;
477 xcb_icccm_wm_hints_t hints
;
482 TAILQ_HEAD(ws_win_list
, ws_win
);
483 TAILQ_HEAD(ws_win_stack
, ws_win
);
487 TAILQ_ENTRY(pid_e
) entry
;
491 TAILQ_HEAD(pid_list
, pid_e
);
492 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
494 /* layout handlers */
495 void stack(struct swm_region
*);
496 void vertical_config(struct workspace
*, int);
497 void vertical_stack(struct workspace
*, struct swm_geometry
*);
498 void horizontal_config(struct workspace
*, int);
499 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
500 void max_stack(struct workspace
*, struct swm_geometry
*);
501 void plain_stacker(struct workspace
*);
502 void fancy_stacker(struct workspace
*);
505 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
506 void (*l_config
)(struct workspace
*, int);
508 #define SWM_L_FOCUSPREV (1<<0)
509 #define SWM_L_MAPONFOCUS (1<<1)
510 void (*l_string
)(struct workspace
*);
512 /* stack, configure */
513 { vertical_stack
, vertical_config
, 0, plain_stacker
},
514 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
516 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
517 { NULL
, NULL
, 0, NULL
},
520 /* position of max_stack mode in the layouts array, index into layouts! */
521 #define SWM_V_STACK (0)
522 #define SWM_H_STACK (1)
523 #define SWM_MAX_STACK (2)
525 #define SWM_H_SLICE (32)
526 #define SWM_V_SLICE (32)
528 /* define work spaces */
530 int idx
; /* workspace index */
531 char *name
; /* workspace name */
532 bool always_raise
; /* raise windows on focus */
533 bool bar_enabled
; /* bar visibility */
534 struct layout
*cur_layout
; /* current layout handlers */
535 struct ws_win
*focus
; /* may be NULL */
536 struct ws_win
*focus_prev
; /* may be NULL */
537 struct ws_win
*focus_pending
; /* may be NULL */
538 struct ws_win
*focus_raise
; /* may be NULL */
539 struct swm_region
*r
; /* may be NULL */
540 struct swm_region
*old_r
; /* may be NULL */
541 struct ws_win_list winlist
; /* list of windows in ws */
542 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
543 struct ws_win_stack stack
; /* stacking order */
544 int state
; /* mapping state */
545 char stacker
[10]; /* display stacker and layout */
549 int horizontal_msize
;
551 int horizontal_stacks
;
552 bool horizontal_flip
;
562 SWM_WS_STATE_MAPPING
,
568 SWM_S_COLOR_BAR_BORDER
,
569 SWM_S_COLOR_BAR_BORDER_UNFOCUS
,
570 SWM_S_COLOR_BAR_FONT
,
572 SWM_S_COLOR_FOCUS_MAXIMIZED
,
574 SWM_S_COLOR_UNFOCUS_MAXIMIZED
,
578 /* physical screen mapping */
579 #define SWM_WS_MAX (22) /* hard limit */
580 int workspace_limit
= 10; /* soft limit */
583 int idx
; /* screen index */
584 struct swm_region_list rl
; /* list of regions on this screen */
585 struct swm_region_list orl
; /* list of old regions */
587 struct workspace ws
[SWM_WS_MAX
];
588 struct swm_region
*r_focus
;
595 } c
[SWM_S_COLOR_MAX
];
597 xcb_gcontext_t bar_gc
;
600 struct swm_screen
*screens
;
602 /* args to functions */
605 #define SWM_ARG_ID_FOCUSNEXT (0)
606 #define SWM_ARG_ID_FOCUSPREV (1)
607 #define SWM_ARG_ID_FOCUSMAIN (2)
608 #define SWM_ARG_ID_FOCUSURGENT (3)
609 #define SWM_ARG_ID_SWAPNEXT (10)
610 #define SWM_ARG_ID_SWAPPREV (11)
611 #define SWM_ARG_ID_SWAPMAIN (12)
612 #define SWM_ARG_ID_MOVELAST (13)
613 #define SWM_ARG_ID_MASTERSHRINK (20)
614 #define SWM_ARG_ID_MASTERGROW (21)
615 #define SWM_ARG_ID_MASTERADD (22)
616 #define SWM_ARG_ID_MASTERDEL (23)
617 #define SWM_ARG_ID_FLIPLAYOUT (24)
618 #define SWM_ARG_ID_STACKRESET (30)
619 #define SWM_ARG_ID_STACKINIT (31)
620 #define SWM_ARG_ID_STACKBALANCE (32)
621 #define SWM_ARG_ID_CYCLEWS_UP (40)
622 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
623 #define SWM_ARG_ID_CYCLERG_UP (42)
624 #define SWM_ARG_ID_CYCLERG_DOWN (43)
625 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
626 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
627 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
628 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
629 #define SWM_ARG_ID_STACKINC (50)
630 #define SWM_ARG_ID_STACKDEC (51)
631 #define SWM_ARG_ID_DONTCENTER (70)
632 #define SWM_ARG_ID_CENTER (71)
633 #define SWM_ARG_ID_KILLWINDOW (80)
634 #define SWM_ARG_ID_DELETEWINDOW (81)
635 #define SWM_ARG_ID_WIDTHGROW (90)
636 #define SWM_ARG_ID_WIDTHSHRINK (91)
637 #define SWM_ARG_ID_HEIGHTGROW (92)
638 #define SWM_ARG_ID_HEIGHTSHRINK (93)
639 #define SWM_ARG_ID_MOVEUP (100)
640 #define SWM_ARG_ID_MOVEDOWN (101)
641 #define SWM_ARG_ID_MOVELEFT (102)
642 #define SWM_ARG_ID_MOVERIGHT (103)
643 #define SWM_ARG_ID_BAR_TOGGLE (110)
644 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
645 #define SWM_ARG_ID_CYCLERG_MOVE_UP (112)
646 #define SWM_ARG_ID_CYCLERG_MOVE_DOWN (113)
652 TAILQ_ENTRY(quirk
) entry
;
653 char *class; /* WM_CLASS:class */
654 char *instance
; /* WM_CLASS:instance */
655 char *name
; /* WM_NAME */
657 regex_t regex_instance
;
660 int ws
; /* Initial workspace. */
661 #define SWM_Q_FLOAT (1<<0) /* Float this window. */
662 #define SWM_Q_TRANSSZ (1<<1) /* Transient window size too small. */
663 #define SWM_Q_ANYWHERE (1<<2) /* Don't position this window */
664 #define SWM_Q_XTERM_FONTADJ (1<<3) /* Adjust xterm fonts when resizing. */
665 #define SWM_Q_FULLSCREEN (1<<4) /* Remove border when fullscreen. */
666 #define SWM_Q_FOCUSPREV (1<<5) /* Focus on caller. */
667 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
668 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
669 #define SWM_Q_OBEYAPPFOCUSREQ (1<<8) /* Focus when applications ask. */
670 #define SWM_Q_IGNOREPID (1<<9) /* Ignore PID when determining ws. */
671 #define SWM_Q_IGNORESPAWNWS (1<<10) /* Ignore _SWM_WS when managing win. */
672 #define SWM_Q_NOFOCUSCYCLE (1<<11) /* Remove from normal focus cycle. */
673 #define SWM_Q_MINIMALBORDER (1<<12) /* No border when floating/unfocused. */
675 TAILQ_HEAD(quirk_list
, quirk
);
676 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
679 * Supported EWMH hints should be added to
680 * both the enum and the ewmh array
686 _NET_CURRENT_DESKTOP
,
687 _NET_DESKTOP_GEOMETRY
,
689 _NET_DESKTOP_VIEWPORT
,
690 _NET_MOVERESIZE_WINDOW
,
691 _NET_NUMBER_OF_DESKTOPS
,
692 _NET_REQUEST_FRAME_EXTENTS
,
694 _NET_WM_ACTION_ABOVE
,
695 _NET_WM_ACTION_CLOSE
,
696 _NET_WM_ACTION_FULLSCREEN
,
698 _NET_WM_ACTION_RESIZE
,
699 _NET_WM_ALLOWED_ACTIONS
,
701 _NET_WM_FULL_PLACEMENT
,
705 _NET_WM_STATE_FULLSCREEN
,
706 _NET_WM_STATE_HIDDEN
,
707 _NET_WM_STATE_MAXIMIZED_VERT
,
708 _NET_WM_STATE_MAXIMIZED_HORZ
,
709 _NET_WM_STATE_SKIP_PAGER
,
710 _NET_WM_STATE_SKIP_TASKBAR
,
712 _NET_WM_WINDOW_TYPE_DIALOG
,
713 _NET_WM_WINDOW_TYPE_DOCK
,
714 _NET_WM_WINDOW_TYPE_NORMAL
,
715 _NET_WM_WINDOW_TYPE_SPLASH
,
716 _NET_WM_WINDOW_TYPE_TOOLBAR
,
717 _NET_WM_WINDOW_TYPE_UTILITY
,
718 _SWM_WM_STATE_MANUAL
,
725 } ewmh
[SWM_EWMH_HINT_MAX
] = {
726 /* must be in same order as in the enum */
727 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
728 {"_NET_CLIENT_LIST", XCB_ATOM_NONE
},
729 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
730 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE
},
731 {"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE
},
732 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE
},
733 {"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE
},
734 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
735 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE
},
736 {"_NET_REQUEST_FRAME_EXTENTS", XCB_ATOM_NONE
},
737 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE
},
738 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
739 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
740 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
741 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
742 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
743 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
744 {"_NET_WM_DESKTOP", XCB_ATOM_NONE
},
745 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE
},
746 {"_NET_WM_NAME", XCB_ATOM_NONE
},
747 {"_NET_WM_STATE", XCB_ATOM_NONE
},
748 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
749 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
750 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
751 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
752 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
753 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
754 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
755 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
756 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
757 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
758 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
759 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
760 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
761 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
762 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
765 /* EWMH source type */
767 EWMH_SOURCE_TYPE_NONE
= 0,
768 EWMH_SOURCE_TYPE_NORMAL
= 1,
769 EWMH_SOURCE_TYPE_OTHER
= 2,
776 XC_BOTTOM_LEFT_CORNER
,
777 XC_BOTTOM_RIGHT_CORNER
,
785 char *name
; /* Name used by Xcursor .*/
786 uint8_t cf_char
; /* cursorfont index. */
788 } cursors
[XC_MAX
] = {
789 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
790 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
791 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
792 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
793 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
794 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
795 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
798 #define SWM_SPAWN_OPTIONAL 0x1
802 TAILQ_ENTRY(spawn_prog
) entry
;
808 TAILQ_HEAD(spawn_list
, spawn_prog
);
809 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
815 /* User callable function IDs. */
828 FN_FULLSCREEN_TOGGLE
,
942 /* SWM_DEBUG actions MUST be here: */
955 BINDING_F_REPLAY
= 0x1,
959 RB_ENTRY(binding
) entry
;
960 uint16_t mod
; /* Modifier Mask. */
961 enum binding_type type
; /* Key or Button. */
962 uint32_t value
; /* KeySym or Button Index. */
963 enum actionid action
; /* Action Identifier. */
967 RB_HEAD(binding_tree
, binding
);
969 /* function prototypes */
970 void adjust_font(struct ws_win
*);
971 char *argsep(char **);
972 void bar_cleanup(struct swm_region
*);
973 void bar_extra_setup(void);
974 void bar_extra_stop(void);
975 int bar_extra_update(void);
976 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
977 void bar_fmt_expand(char *, size_t);
978 void bar_draw(struct swm_bar
*);
979 void bar_print(struct swm_region
*, const char *);
980 void bar_print_legacy(struct swm_region
*, const char *);
981 void bar_replace(char *, char *, struct swm_region
*, size_t);
982 void bar_replace_pad(char *, int *, size_t);
983 char *bar_replace_seq(char *, char *, struct swm_region
*, size_t *, size_t);
984 void bar_setup(struct swm_region
*);
985 void bar_toggle(struct binding
*, struct swm_region
*, union arg
*);
986 void bar_urgent(char *, size_t);
987 void bar_window_class(char *, size_t, struct swm_region
*);
988 void bar_window_class_instance(char *, size_t, struct swm_region
*);
989 void bar_window_float(char *, size_t, struct swm_region
*);
990 void bar_window_instance(char *, size_t, struct swm_region
*);
991 void bar_window_name(char *, size_t, struct swm_region
*);
992 void bar_window_state(char *, size_t, struct swm_region
*);
993 void bar_workspace_name(char *, size_t, struct swm_region
*);
994 int binding_cmp(struct binding
*, struct binding
*);
995 void binding_insert(uint16_t, enum binding_type
, uint32_t, enum actionid
,
996 uint32_t, const char *);
997 struct binding
*binding_lookup(uint16_t, enum binding_type
, uint32_t);
998 void binding_remove(struct binding
*);
999 void buttonpress(xcb_button_press_event_t
*);
1000 void buttonrelease(xcb_button_release_event_t
*);
1001 void center_pointer(struct swm_region
*);
1002 void check_conn(void);
1003 void clear_bindings(void);
1004 void clear_keybindings(void);
1005 int clear_maximized(struct workspace
*);
1006 void clear_quirks(void);
1007 void clear_spawns(void);
1008 void clientmessage(xcb_client_message_event_t
*);
1009 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
1010 int conf_load(const char *, int);
1011 void configurenotify(xcb_configure_notify_event_t
*);
1012 void configurerequest(xcb_configure_request_event_t
*);
1013 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
1014 void constrain_window(struct ws_win
*, struct swm_geometry
*, int *);
1015 int count_win(struct workspace
*, bool);
1016 void cursors_cleanup(void);
1017 void cursors_load(void);
1018 void custom_region(const char *);
1019 void cycle_layout(struct binding
*, struct swm_region
*, union arg
*);
1020 void cyclerg(struct binding
*, struct swm_region
*, union arg
*);
1021 void cyclews(struct binding
*, struct swm_region
*, union arg
*);
1023 void debug_refresh(struct ws_win
*);
1025 void debug_toggle(struct binding
*, struct swm_region
*, union arg
*);
1026 void destroynotify(xcb_destroy_notify_event_t
*);
1027 void dumpwins(struct binding
*, struct swm_region
*, union arg
*);
1028 int enable_wm(void);
1029 void enternotify(xcb_enter_notify_event_t
*);
1030 void event_drain(uint8_t);
1031 void event_error(xcb_generic_error_t
*);
1032 void event_handle(xcb_generic_event_t
*);
1033 void ewmh_apply_flags(struct ws_win
*, uint32_t);
1034 void ewmh_autoquirk(struct ws_win
*);
1035 void ewmh_get_desktop_names(void);
1036 void ewmh_get_wm_state(struct ws_win
*);
1037 void ewmh_update_actions(struct ws_win
*);
1038 void ewmh_update_client_list(void);
1039 void ewmh_update_current_desktop(void);
1040 void ewmh_update_desktop_names(void);
1041 void ewmh_update_desktops(void);
1042 void ewmh_change_wm_state(struct ws_win
*, xcb_atom_t
, long);
1043 void ewmh_update_wm_state(struct ws_win
*);
1044 char *expand_tilde(const char *);
1045 void expose(xcb_expose_event_t
*);
1046 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
1047 struct swm_bar
*find_bar(xcb_window_t
);
1048 struct ws_win
*find_frame_window(xcb_window_t
);
1049 struct pid_e
*find_pid(pid_t
);
1050 struct swm_region
*find_region(xcb_window_t
);
1051 struct ws_win
*find_unmanaged_window(xcb_window_t
);
1052 struct ws_win
*find_window(xcb_window_t
);
1053 void floating_toggle(struct binding
*, struct swm_region
*, union arg
*);
1054 void focus(struct binding
*, struct swm_region
*, union arg
*);
1055 void focus_flush(void);
1056 void focus_pointer(struct binding
*, struct swm_region
*, union arg
*);
1057 void focus_region(struct swm_region
*);
1058 void focus_win(struct ws_win
*);
1059 void focusin(xcb_focus_in_event_t
*);
1061 void focusout(xcb_focus_out_event_t
*);
1063 void focusrg(struct binding
*, struct swm_region
*, union arg
*);
1064 void fontset_init(void);
1065 void free_window(struct ws_win
*);
1066 void fullscreen_toggle(struct binding
*, struct swm_region
*, union arg
*);
1067 xcb_atom_t
get_atom_from_string(const char *);
1069 char *get_atom_name(xcb_atom_t
);
1071 struct ws_win
*get_focus_magic(struct ws_win
*);
1072 struct ws_win
*get_focus_prev(struct ws_win
*);
1073 xcb_generic_event_t
*get_next_event(bool);
1075 char *get_notify_detail_label(uint8_t);
1076 char *get_notify_mode_label(uint8_t);
1078 struct ws_win
*get_pointer_win(xcb_window_t
);
1079 struct ws_win
*get_region_focus(struct swm_region
*);
1080 int get_region_index(struct swm_region
*);
1081 xcb_screen_t
*get_screen(int);
1082 int get_screen_count(void);
1084 char *get_source_type_label(uint32_t);
1085 char *get_stack_mode_name(uint8_t);
1086 char *get_state_mask_label(uint16_t);
1088 int32_t get_swm_ws(xcb_window_t
);
1089 bool get_urgent(struct ws_win
*);
1091 char *get_win_input_model(struct ws_win
*);
1093 char *get_win_name(xcb_window_t
);
1094 uint8_t get_win_state(xcb_window_t
);
1095 void get_wm_protocols(struct ws_win
*);
1096 int get_ws_idx(struct ws_win
*);
1097 void grab_windows(void);
1098 void grabbuttons(void);
1099 void grabkeys(void);
1100 void iconify(struct binding
*, struct swm_region
*, union arg
*);
1101 bool isxlfd(char *);
1102 bool keybindreleased(struct binding
*, xcb_key_release_event_t
*);
1103 void keypress(xcb_key_press_event_t
*);
1104 void keyrelease(xcb_key_release_event_t
*);
1105 bool keyrepeating(xcb_key_release_event_t
*);
1106 void kill_bar_extra_atexit(void);
1107 void kill_refs(struct ws_win
*);
1109 void leavenotify(xcb_leave_notify_event_t
*);
1111 void load_float_geom(struct ws_win
*);
1112 void lower_window(struct ws_win
*);
1113 struct ws_win
*manage_window(xcb_window_t
, int, bool);
1114 void map_window(struct ws_win
*);
1115 void mapnotify(xcb_map_notify_event_t
*);
1116 void mappingnotify(xcb_mapping_notify_event_t
*);
1117 void maprequest(xcb_map_request_event_t
*);
1118 void maximize_toggle(struct binding
*, struct swm_region
*, union arg
*);
1119 void motionnotify(xcb_motion_notify_event_t
*);
1120 void move(struct binding
*, struct swm_region
*, union arg
*);
1121 void move_win(struct ws_win
*, struct binding
*, int);
1122 uint32_t name_to_pixel(int, const char *);
1123 void name_workspace(struct binding
*, struct swm_region
*, union arg
*);
1124 void new_region(struct swm_screen
*, int, int, int, int);
1125 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1126 int parsebinding(const char *, uint16_t *, enum binding_type
*, uint32_t *,
1128 int parsequirks(const char *, uint32_t *, int *);
1129 void pressbutton(struct binding
*, struct swm_region
*, union arg
*);
1130 void priorws(struct binding
*, struct swm_region
*, union arg
*);
1132 void print_win_geom(xcb_window_t
);
1134 void propertynotify(xcb_property_notify_event_t
*);
1135 void put_back_event(xcb_generic_event_t
*);
1136 void quirk_free(struct quirk
*);
1137 void quirk_insert(const char *, const char *, const char *, uint32_t, int);
1138 void quirk_remove(struct quirk
*);
1139 void quirk_replace(struct quirk
*, const char *, const char *, const char *,
1141 void quit(struct binding
*, struct swm_region
*, union arg
*);
1142 void raise_focus(struct binding
*, struct swm_region
*, union arg
*);
1143 void raise_toggle(struct binding
*, struct swm_region
*, union arg
*);
1144 void raise_window(struct ws_win
*);
1145 void region_containment(struct ws_win
*, struct swm_region
*, int);
1146 struct swm_region
*region_under(struct swm_screen
*, int, int);
1147 void regionize(struct ws_win
*, int, int);
1148 void reparent_window(struct ws_win
*);
1149 void reparentnotify(xcb_reparent_notify_event_t
*);
1150 void resize(struct binding
*, struct swm_region
*, union arg
*);
1151 void resize_win(struct ws_win
*, struct binding
*, int);
1152 void restart(struct binding
*, struct swm_region
*, union arg
*);
1153 struct swm_region
*root_to_region(xcb_window_t
, int);
1154 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1155 void scan_randr(int);
1156 void search_do_resp(void);
1157 void search_resp_name_workspace(const char *, size_t);
1158 void search_resp_search_window(const char *);
1159 void search_resp_search_workspace(const char *);
1160 void search_resp_uniconify(const char *, size_t);
1161 void search_win(struct binding
*, struct swm_region
*, union arg
*);
1162 void search_win_cleanup(void);
1163 void search_workspace(struct binding
*, struct swm_region
*, union arg
*);
1164 void send_to_rg(struct binding
*, struct swm_region
*, union arg
*);
1165 void send_to_rg_relative(struct binding
*, struct swm_region
*, union arg
*);
1166 void send_to_ws(struct binding
*, struct swm_region
*, union arg
*);
1167 void set_region(struct swm_region
*);
1168 int setautorun(const char *, const char *, int);
1169 void setbinding(uint16_t, enum binding_type
, uint32_t, enum actionid
,
1170 uint32_t, const char *);
1171 int setconfbinding(const char *, const char *, int);
1172 int setconfcolor(const char *, const char *, int);
1173 int setconfmodkey(const char *, const char *, int);
1174 int setconfquirk(const char *, const char *, int);
1175 int setconfregion(const char *, const char *, int);
1176 int setconfspawn(const char *, const char *, int);
1177 int setconfvalue(const char *, const char *, int);
1178 int setkeymapping(const char *, const char *, int);
1179 int setlayout(const char *, const char *, int);
1180 void setquirk(const char *, const char *, const char *, uint32_t, int);
1181 void setscreencolor(const char *, int, int);
1182 void setspawn(const char *, const char *, int);
1183 void setup_btnbindings(void);
1184 void setup_ewmh(void);
1185 void setup_globals(void);
1186 void setup_keybindings(void);
1187 void setup_quirks(void);
1188 void setup_screens(void);
1189 void setup_spawn(void);
1190 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1191 void set_win_state(struct ws_win
*, uint8_t);
1192 void shutdown_cleanup(void);
1194 void socket_setnonblock(int);
1195 void sort_windows(struct ws_win_list
*);
1196 void spawn(int, union arg
*, bool);
1197 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1198 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1199 void spawn_insert(const char *, const char *, int);
1200 struct spawn_prog
*spawn_find(const char *);
1201 void spawn_remove(struct spawn_prog
*);
1202 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1203 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1204 void stack_config(struct binding
*, struct swm_region
*, union arg
*);
1205 void stack_master(struct workspace
*, struct swm_geometry
*, int, bool);
1206 void store_float_geom(struct ws_win
*);
1207 char *strdupsafe(const char *);
1208 void swapwin(struct binding
*, struct swm_region
*, union arg
*);
1209 void switchws(struct binding
*, struct swm_region
*, union arg
*);
1210 void teardown_ewmh(void);
1211 void unescape_selector(char *);
1212 void unfocus_win(struct ws_win
*);
1213 void uniconify(struct binding
*, struct swm_region
*, union arg
*);
1214 void unmanage_window(struct ws_win
*);
1215 void unmap_all(void);
1216 void unmap_window(struct ws_win
*);
1217 void unmapnotify(xcb_unmap_notify_event_t
*);
1218 void unparent_window(struct ws_win
*);
1219 void update_floater(struct ws_win
*);
1220 void update_modkey(uint16_t);
1221 void update_win_stacking(struct ws_win
*);
1222 void update_window(struct ws_win
*);
1223 void draw_frame(struct ws_win
*);
1224 void update_wm_state(struct ws_win
*win
);
1225 void updatenumlockmask(void);
1226 void validate_spawns(void);
1227 int validate_win(struct ws_win
*);
1228 int validate_ws(struct workspace
*);
1229 void version(struct binding
*, struct swm_region
*, union arg
*);
1230 void win_to_ws(struct ws_win
*, int, bool);
1231 pid_t
window_get_pid(xcb_window_t
);
1232 void wkill(struct binding
*, struct swm_region
*, union arg
*);
1233 void update_ws_stack(struct workspace
*);
1234 void xft_init(struct swm_region
*);
1235 void _add_startup_exception(const char *, va_list);
1236 void add_startup_exception(const char *, ...);
1238 RB_PROTOTYPE(binding_tree
, binding
, entry
, binding_cmp
);
1239 RB_GENERATE(binding_tree
, binding
, entry
, binding_cmp
);
1240 struct binding_tree bindings
;
1245 xcb_font_t cf
= XCB_NONE
;
1248 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1249 /* try to load Xcursor first. */
1250 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1253 /* fallback to cursorfont. */
1254 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1255 if (cf
== XCB_NONE
) {
1256 cf
= xcb_generate_id(conn
);
1257 xcb_open_font(conn
, cf
, strlen("cursor"),
1261 cursors
[i
].cid
= xcb_generate_id(conn
);
1262 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1263 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1264 0xffff, 0xffff, 0xffff);
1270 xcb_close_font(conn
, cf
);
1274 cursors_cleanup(void)
1277 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1278 xcb_free_cursor(conn
, cursors
[i
].cid
);
1282 expand_tilde(const char *s
)
1284 struct passwd
*ppwd
;
1292 errx(1, "expand_tilde: NULL string.");
1295 result
= strdup(sc
);
1301 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1302 errx(1, "expand_tilde: sysconf");
1304 if ((user
= calloc(1, max
+ 1)) == NULL
)
1305 errx(1, "expand_tilde: calloc");
1307 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1312 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1316 result
= strdup(sc
);
1318 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1322 errx(1, "expand_tilde: failed to allocate memory.");
1328 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1330 unsigned int tmpr
, tmpg
, tmpb
;
1332 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1335 *rr
= RGB_8_TO_16(tmpr
);
1336 *gg
= RGB_8_TO_16(tmpg
);
1337 *bb
= RGB_8_TO_16(tmpb
);
1343 get_screen(int screen
)
1345 const xcb_setup_t
*r
;
1346 xcb_screen_iterator_t iter
;
1348 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1349 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1353 iter
= xcb_setup_roots_iterator(r
);
1354 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1362 get_screen_count(void)
1364 const xcb_setup_t
*r
;
1366 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1367 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1371 return xcb_setup_roots_length(r
);
1375 get_region_index(struct swm_region
*r
)
1377 struct swm_region
*rr
;
1383 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1398 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1399 event_drain(XCB_ENTER_NOTIFY
);
1405 get_atom_from_string(const char *str
)
1407 xcb_intern_atom_cookie_t c
;
1408 xcb_intern_atom_reply_t
*r
;
1411 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1412 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1420 return (XCB_ATOM_NONE
);
1424 get_wm_protocols(struct ws_win
*win
) {
1426 xcb_icccm_get_wm_protocols_reply_t wpr
;
1428 if (xcb_icccm_get_wm_protocols_reply(conn
,
1429 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
1431 for (i
= 0; i
< (int)wpr
.atoms_len
; i
++) {
1432 if (wpr
.atoms
[i
] == a_takefocus
)
1433 win
->take_focus
= true;
1434 if (wpr
.atoms
[i
] == a_delete
)
1435 win
->can_delete
= true;
1437 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
1444 xcb_window_t root
, win
;
1445 int i
, j
, num_screens
;
1447 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1448 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1450 num_screens
= get_screen_count();
1451 for (i
= 0; i
< num_screens
; i
++) {
1452 root
= screens
[i
].root
;
1454 /* Set up _NET_SUPPORTING_WM_CHECK. */
1455 win
= xcb_generate_id(conn
);
1456 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
1457 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1458 XCB_COPY_FROM_PARENT
, 0, NULL
);
1460 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
1461 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1462 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1463 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1465 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1466 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1467 8, strlen("spectrwm"), "spectrwm");
1469 /* Report supported atoms */
1470 xcb_delete_property(conn
, root
, a_net_supported
);
1471 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1472 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, root
,
1473 a_net_supported
, XCB_ATOM_ATOM
, 32, 1,
1478 ewmh_update_desktops();
1479 ewmh_get_desktop_names();
1487 xcb_get_property_cookie_t pc
;
1488 xcb_get_property_reply_t
*pr
;
1490 num_screens
= get_screen_count();
1492 for (i
= 0; i
< num_screens
; i
++) {
1493 /* Get the support check window and destroy it */
1494 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, a_net_wm_check
,
1495 XCB_ATOM_WINDOW
, 0, 1);
1496 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1499 if (pr
->format
== a_net_wm_check
) {
1500 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1502 xcb_destroy_window(conn
, id
);
1503 xcb_delete_property(conn
, screens
[i
].root
,
1505 xcb_delete_property(conn
, screens
[i
].root
,
1513 ewmh_autoquirk(struct ws_win
*win
)
1515 xcb_get_property_reply_t
*r
;
1516 xcb_get_property_cookie_t c
;
1520 c
= xcb_get_property(conn
, 0, win
->id
,
1521 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1522 r
= xcb_get_property_reply(conn
, c
, NULL
);
1526 type
= xcb_get_property_value(r
);
1527 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1529 for (i
= 0; i
< n
; i
++) {
1530 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1532 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1533 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1534 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1535 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1538 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1539 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1540 win
->quirks
= SWM_Q_FLOAT
;
1548 ewmh_update_actions(struct ws_win
*win
)
1550 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1556 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1559 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1560 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1561 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1564 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1565 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1568 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1569 #define _NET_WM_STATE_ADD 1 /* add/set property */
1570 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1573 ewmh_change_wm_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1580 name
= get_atom_name(state
);
1581 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: win %#x, state: %s, "
1582 "action: %ld\n", WINID(win
), name
, action
);
1588 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1589 flag
= EWMH_F_FULLSCREEN
;
1590 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1591 flag
= EWMH_F_ABOVE
;
1592 else if (state
== ewmh
[_NET_WM_STATE_HIDDEN
].atom
)
1593 flag
= EWMH_F_HIDDEN
;
1594 else if (state
== ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
||
1595 state
== ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
)
1596 flag
= EWMH_F_MAXIMIZED
;
1597 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1598 flag
= SWM_F_MANUAL
;
1599 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1600 flag
= EWMH_F_SKIP_PAGER
;
1601 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1602 flag
= EWMH_F_SKIP_TASKBAR
;
1604 /* Disallow unfloating transients. */
1605 if (TRANS(win
) && flag
== EWMH_F_ABOVE
)
1608 new_flags
= win
->ewmh_flags
;
1611 case _NET_WM_STATE_REMOVE
:
1614 case _NET_WM_STATE_ADD
:
1617 case _NET_WM_STATE_TOGGLE
:
1622 ewmh_apply_flags(win
, new_flags
);
1625 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: done.\n");
1629 ewmh_apply_flags(struct ws_win
*win
, uint32_t pending
)
1631 struct workspace
*ws
;
1634 changed
= win
->ewmh_flags
^ pending
;
1638 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: pending: %d\n", pending
);
1640 win
->ewmh_flags
= pending
;
1643 if (changed
& EWMH_F_HIDDEN
) {
1645 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1646 ws
->focus_pending
= get_focus_prev(win
);
1651 /* Reload floating geometry in case region changed. */
1653 load_float_geom(win
);
1655 /* The window is no longer iconic, prepare focus. */
1656 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1657 ws
->focus_pending
= get_focus_magic(win
);
1662 if (changed
& EWMH_F_ABOVE
) {
1663 if (ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1665 load_float_geom(win
);
1666 else if (!MAXIMIZED(win
))
1667 store_float_geom(win
);
1669 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1670 changed
&= ~EWMH_F_MAXIMIZED
;
1674 win
->ewmh_flags
^= EWMH_F_ABOVE
& pending
;
1678 if (changed
& EWMH_F_MAXIMIZED
) {
1679 /* VERT and/or HORZ changed. */
1681 if (!MAXIMIZED(win
))
1682 load_float_geom(win
);
1684 store_float_geom(win
);
1687 if (MAXIMIZED(win
)) {
1688 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
1689 ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1693 ws
->focus_pending
= win
;
1701 if (changed
& EWMH_F_FULLSCREEN
) {
1702 if (FULLSCREEN(win
)) {
1703 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
1707 ws
->focus_pending
= win
;
1710 load_float_geom(win
);
1713 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1717 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: done.\n");
1721 ewmh_update_wm_state(struct ws_win
*win
) {
1722 xcb_atom_t vals
[SWM_EWMH_ACTION_COUNT_MAX
];
1726 vals
[n
++] = ewmh
[_NET_WM_STATE_HIDDEN
].atom
;
1727 if (FULLSCREEN(win
))
1728 vals
[n
++] = ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
;
1729 if (MAXIMIZED_VERT(win
))
1730 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
;
1731 if (MAXIMIZED_HORZ(win
))
1732 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
;
1733 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1734 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
;
1735 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1736 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
;
1737 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1738 vals
[n
++] = ewmh
[_NET_WM_STATE_ABOVE
].atom
;
1739 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1740 vals
[n
++] = ewmh
[_SWM_WM_STATE_MANUAL
].atom
;
1743 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1744 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, n
, vals
);
1746 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1750 ewmh_get_wm_state(struct ws_win
*win
)
1753 xcb_get_property_cookie_t c
;
1754 xcb_get_property_reply_t
*r
;
1760 win
->ewmh_flags
= 0;
1762 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1763 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1764 r
= xcb_get_property_reply(conn
, c
, NULL
);
1768 states
= xcb_get_property_value(r
);
1769 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1771 for (i
= 0; i
< n
; i
++)
1772 ewmh_change_wm_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1780 dumpwins(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
1784 xcb_get_window_attributes_cookie_t c
;
1785 xcb_get_window_attributes_reply_t
*wa
;
1787 /* suppress unused warning since var is needed */
1791 if (r
->ws
== NULL
) {
1792 DPRINTF("dumpwins: invalid workspace\n");
1796 DPRINTF("=== managed window list ws %02d ===\n", r
->ws
->idx
);
1797 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
) {
1798 state
= get_win_state(w
->id
);
1799 c
= xcb_get_window_attributes(conn
, w
->id
);
1800 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1802 DPRINTF("win %#x (%#x), map_state: %d, state: %u, "
1803 "transient: %#x\n", w
->frame
, w
->id
, wa
->map_state
,
1804 state
, w
->transient
);
1807 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1811 DPRINTF("=== stacking order (top down) === \n");
1812 TAILQ_FOREACH(w
, &r
->ws
->stack
, stack_entry
) {
1813 DPRINTF("win %#x (%#x), fs: %s, maximized: %s, above: %s, "
1814 "iconic: %s\n", w
->frame
, w
->id
, YESNO(FULLSCREEN(w
)),
1815 YESNO(MAXIMIZED(w
)), YESNO(ABOVE(w
)), YESNO(ICONIC(w
)));
1818 DPRINTF("===== unmanaged window list =====\n");
1819 TAILQ_FOREACH(w
, &r
->ws
->unmanagedlist
, entry
) {
1820 state
= get_win_state(w
->id
);
1821 c
= xcb_get_window_attributes(conn
, w
->id
);
1822 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1824 DPRINTF("win %#x, map_state: %d, state: %u, "
1825 "transient: %#x\n", w
->id
, wa
->map_state
,
1826 state
, w
->transient
);
1829 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1833 DPRINTF("=================================\n");
1837 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
1840 int num_screens
, i
, j
;
1842 /* Suppress warnings. */
1847 DNPRINTF(SWM_D_MISC
, "debug_toggle\n");
1849 debug_enabled
= !debug_enabled
;
1851 num_screens
= get_screen_count();
1852 for (i
= 0; i
< num_screens
; i
++)
1853 for (j
= 0; j
< workspace_limit
; j
++)
1854 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1861 debug_refresh(struct ws_win
*win
)
1868 XRectangle l_ibox
, l_lbox
;
1869 xcb_rectangle_t rect
;
1871 uint32_t wc
[4], mask
, width
, height
, gcv
[1];
1874 xcb_screen_t
*screen
;
1876 if (debug_enabled
) {
1877 /* Create debug window if it doesn't exist. */
1878 if (win
->debug
== XCB_WINDOW_NONE
) {
1879 if ((screen
= get_screen(win
->s
->idx
)) == NULL
)
1880 errx(1, "ERROR: can't get screen %d.",
1883 win
->debug
= xcb_generate_id(conn
);
1884 wc
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1885 wc
[1] = win
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
1886 wc
[2] = screen
->default_colormap
;
1888 xcb_create_window(conn
, screen
->root_depth
, win
->debug
,
1889 win
->frame
, 0, 0, 10, 10, 1,
1890 XCB_WINDOW_CLASS_INPUT_OUTPUT
, screen
->root_visual
,
1891 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
|
1892 XCB_CW_COLORMAP
, wc
);
1894 xcb_map_window(conn
, win
->debug
);
1897 /* Determine workspace window list index. */
1899 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
1905 /* Determine stacking index (top down). */
1907 TAILQ_FOREACH(w
, &win
->ws
->stack
, stack_entry
) {
1913 if (asprintf(&s
, "%#x f:%#x wl:%d s:%d im:%s", win
->id
,
1914 win
->frame
, widx
, sidx
, get_win_input_model(win
)) == -1)
1919 /* Update window to an appropriate dimension. */
1920 if (bar_font_legacy
) {
1921 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
1922 width
= l_lbox
.width
+ 4;
1923 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
1925 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
1927 width
= info
.width
+ 4;
1928 height
= bar_font
->height
+ 4;
1931 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
1932 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
;
1934 wc
[0] = wc
[1] = border_width
;
1941 xcb_configure_window(conn
, win
->debug
, mask
, wc
);
1943 /* Draw a filled rectangle to 'clear' window. */
1947 rect
.height
= height
;
1949 gcv
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1950 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1951 xcb_poly_fill_rectangle(conn
, win
->debug
, win
->s
->bar_gc
, 1,
1955 if (bar_font_legacy
) {
1956 l_gcv
.graphics_exposures
= 0;
1957 l_draw
= XCreateGC(display
, win
->debug
, 0, &l_gcv
);
1959 XSetForeground(display
, l_draw
,
1960 win
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
1962 DRAWSTRING(display
, win
->debug
, bar_fs
, l_draw
, 2,
1963 (bar_fs_extents
->max_logical_extent
.height
-
1964 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
1966 XFreeGC(display
, l_draw
);
1968 draw
= XftDrawCreate(display
, win
->debug
,
1969 DefaultVisual(display
, win
->s
->idx
),
1970 DefaultColormap(display
, win
->s
->idx
));
1972 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
1973 (bar_height
+ bar_font
->height
) / 2 -
1974 bar_font
->descent
, (FcChar8
*)s
, len
);
1976 XftDrawDestroy(draw
);
1980 } else if (win
->debug
!= XCB_WINDOW_NONE
) {
1981 xcb_destroy_window(conn
, win
->debug
);
1982 win
->debug
= XCB_WINDOW_NONE
;
1987 dumpwins(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
1995 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
2001 #endif /* SWM_DEBUG */
2006 int saved_errno
, status
;
2009 saved_errno
= errno
;
2013 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
2018 if (errno
!= ECHILD
)
2019 warn("sighdlr: waitpid");
2020 #endif /* SWM_DEBUG */
2023 if (pid
== searchpid
)
2027 if (WIFEXITED(status
)) {
2028 if (WEXITSTATUS(status
) != 0)
2029 warnx("sighdlr: child exit status: %d",
2030 WEXITSTATUS(status
));
2032 warnx("sighdlr: child is terminated "
2034 #endif /* SWM_DEBUG */
2048 errno
= saved_errno
;
2054 struct pid_e
*p
= NULL
;
2056 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
2061 TAILQ_FOREACH(p
, &pidlist
, entry
) {
2070 name_to_pixel(int sidx
, const char *colorname
)
2072 uint32_t result
= 0;
2073 char cname
[32] = "#";
2074 xcb_screen_t
*screen
;
2075 xcb_colormap_t cmap
;
2076 xcb_alloc_color_reply_t
*cr
;
2077 xcb_alloc_named_color_reply_t
*nr
;
2078 uint16_t rr
, gg
, bb
;
2080 screen
= get_screen(sidx
);
2081 cmap
= screen
->default_colormap
;
2083 /* color is in format rgb://rr/gg/bb */
2084 if (strncmp(colorname
, "rgb:", 4) == 0) {
2085 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
2086 warnx("could not parse rgb %s", colorname
);
2088 cr
= xcb_alloc_color_reply(conn
,
2089 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
2095 warnx("color '%s' not found", colorname
);
2098 nr
= xcb_alloc_named_color_reply(conn
,
2099 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
2102 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
2103 nr
= xcb_alloc_named_color_reply(conn
,
2104 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
2111 warnx("color '%s' not found", colorname
);
2118 setscreencolor(const char *val
, int i
, int c
)
2120 if (i
< 0 || i
>= get_screen_count())
2123 screens
[i
].c
[c
].pixel
= name_to_pixel(i
, val
);
2124 free(screens
[i
].c
[c
].name
);
2125 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
2130 fancy_stacker(struct workspace
*ws
)
2132 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2133 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2134 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2135 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
2136 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
2137 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2138 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2139 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
2140 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
2144 plain_stacker(struct workspace
*ws
)
2146 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2147 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2148 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
2149 sizeof ws
->stacker
);
2150 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2151 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
2152 sizeof ws
->stacker
);
2156 custom_region(const char *val
)
2158 unsigned int x
, y
, w
, h
;
2159 int sidx
, num_screens
;
2160 xcb_screen_t
*screen
;
2162 DNPRINTF(SWM_D_CONF
, "custom_region: %s\n", val
);
2164 num_screens
= get_screen_count();
2165 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
2166 errx(1, "invalid custom region, "
2167 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
2168 if (sidx
< 1 || sidx
> num_screens
)
2169 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
2173 if ((screen
= get_screen(sidx
)) == NULL
)
2174 errx(1, "ERROR: can't get screen %d.", sidx
);
2177 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
2179 if (x
> screen
->width_in_pixels
||
2180 y
> screen
->height_in_pixels
||
2181 w
+ x
> screen
->width_in_pixels
||
2182 h
+ y
> screen
->height_in_pixels
) {
2183 warnx("ignoring region %ux%u+%u+%u - not within screen "
2184 "boundaries (%ux%u)", w
, h
, x
, y
,
2185 screen
->width_in_pixels
, screen
->height_in_pixels
);
2189 new_region(&screens
[sidx
], x
, y
, w
, h
);
2193 socket_setnonblock(int fd
)
2197 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
2198 err(1, "fcntl F_GETFL");
2199 flags
|= O_NONBLOCK
;
2200 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
2201 err(1, "fcntl F_SETFL");
2205 bar_print_legacy(struct swm_region
*r
, const char *s
)
2207 xcb_rectangle_t rect
;
2212 XRectangle ibox
, lbox
;
2216 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
2218 switch (bar_justify
) {
2219 case SWM_BAR_JUSTIFY_LEFT
:
2222 case SWM_BAR_JUSTIFY_CENTER
:
2223 x
= (WIDTH(r
) - lbox
.width
) / 2;
2225 case SWM_BAR_JUSTIFY_RIGHT
:
2226 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
2230 if (x
< SWM_BAR_OFFSET
)
2235 rect
.width
= WIDTH(r
->bar
);
2236 rect
.height
= HEIGHT(r
->bar
);
2238 /* clear back buffer */
2239 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2240 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2241 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2243 /* draw back buffer */
2244 gcvd
.graphics_exposures
= 0;
2245 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
2246 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2247 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
2248 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
2250 XFreeGC(display
, draw
);
2253 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2254 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2258 bar_print(struct swm_region
*r
, const char *s
)
2261 xcb_rectangle_t rect
;
2269 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
2271 switch (bar_justify
) {
2272 case SWM_BAR_JUSTIFY_LEFT
:
2275 case SWM_BAR_JUSTIFY_CENTER
:
2276 x
= (WIDTH(r
) - info
.width
) / 2;
2278 case SWM_BAR_JUSTIFY_RIGHT
:
2279 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
2283 if (x
< SWM_BAR_OFFSET
)
2288 rect
.width
= WIDTH(r
->bar
);
2289 rect
.height
= HEIGHT(r
->bar
);
2291 /* clear back buffer */
2292 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2293 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2294 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2296 /* draw back buffer */
2297 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
2298 DefaultVisual(display
, r
->s
->idx
),
2299 DefaultColormap(display
, r
->s
->idx
));
2301 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
2302 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
2305 XftDrawDestroy(draw
);
2308 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2309 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2313 bar_extra_stop(void)
2317 bzero(bar_pipe
, sizeof bar_pipe
);
2320 kill(bar_pid
, SIGTERM
);
2323 strlcpy(bar_ext
, "", sizeof bar_ext
);
2328 bar_window_class(char *s
, size_t sz
, struct swm_region
*r
)
2330 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2332 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
2333 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
2337 bar_window_instance(char *s
, size_t sz
, struct swm_region
*r
)
2339 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2341 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
2342 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
2346 bar_window_class_instance(char *s
, size_t sz
, struct swm_region
*r
)
2348 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2351 bar_window_class(s
, sz
, r
);
2352 strlcat(s
, ":", sz
);
2353 bar_window_instance(s
, sz
, r
);
2357 bar_window_state(char *s
, size_t sz
, struct swm_region
*r
)
2359 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2361 if (MAXIMIZED(r
->ws
->focus
))
2362 strlcat(s
, "(m)", sz
);
2363 else if (ABOVE(r
->ws
->focus
))
2364 strlcat(s
, "(f)", sz
);
2368 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
2372 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2375 title
= get_win_name(r
->ws
->focus
->id
);
2376 strlcat(s
, title
, sz
);
2381 get_urgent(struct ws_win
*win
)
2383 xcb_icccm_wm_hints_t hints
;
2384 xcb_get_property_cookie_t c
;
2385 bool urgent
= false;
2388 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2389 if (xcb_icccm_get_wm_hints_reply(conn
, c
, &hints
, NULL
))
2390 urgent
= xcb_icccm_wm_hints_get_urgency(&hints
);
2397 bar_urgent(char *s
, size_t sz
)
2400 int i
, j
, num_screens
;
2401 bool urgent
[SWM_WS_MAX
];
2404 for (i
= 0; i
< workspace_limit
; i
++)
2407 num_screens
= get_screen_count();
2408 for (i
= 0; i
< num_screens
; i
++)
2409 for (j
= 0; j
< workspace_limit
; j
++)
2410 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2411 if (get_urgent(win
))
2414 for (i
= 0; i
< workspace_limit
; i
++) {
2416 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2418 } else if (!urgent_collapse
) {
2419 strlcat(s
, "- ", sz
);
2422 if (urgent_collapse
&& s
[0])
2423 s
[strlen(s
) - 1] = 0;
2427 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2429 if (r
== NULL
|| r
->ws
== NULL
)
2431 if (r
->ws
->name
!= NULL
)
2432 strlcat(s
, r
->ws
->name
, sz
);
2435 /* build the default bar format according to the defined enabled options */
2437 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2441 /* if format provided, just copy the buffers */
2442 if (bar_format
!= NULL
) {
2443 strlcpy(fmtnew
, fmtexp
, sz
);
2447 /* reset the output buffer */
2450 strlcat(fmtnew
, "+N:+I ", sz
);
2452 strlcat(fmtnew
, "+S", sz
);
2453 strlcat(fmtnew
, " ", sz
);
2455 /* only show the workspace name if there's actually one */
2456 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2457 strlcat(fmtnew
, "<+D>", sz
);
2459 /* If enabled, only show the iconic count if there are iconic wins. */
2460 if (iconic_enabled
&& r
!= NULL
&& r
->ws
!= NULL
)
2461 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2463 strlcat(fmtnew
, "{+M}", sz
);
2467 strlcat(fmtnew
, "+3<", sz
);
2469 if (clock_enabled
) {
2470 strlcat(fmtnew
, fmtexp
, sz
);
2471 strlcat(fmtnew
, "+4<", sz
);
2474 /* bar_urgent already adds the space before the last asterisk */
2476 strlcat(fmtnew
, (urgent_collapse
? "*+U*+4<" : "* +U*+4<"), sz
);
2478 if (window_class_enabled
) {
2479 strlcat(fmtnew
, "+C", sz
);
2480 if (!window_instance_enabled
)
2481 strlcat(fmtnew
, "+4<", sz
);
2484 /* checks needed by the colon and floating strlcat(3) calls below */
2485 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2486 if (window_instance_enabled
) {
2487 if (window_class_enabled
)
2488 strlcat(fmtnew
, ":", sz
);
2489 strlcat(fmtnew
, "+T+4<", sz
);
2491 if (window_name_enabled
) {
2492 if (ABOVE(r
->ws
->focus
) || MAXIMIZED(r
->ws
->focus
))
2493 strlcat(fmtnew
, "+F ", sz
);
2494 strlcat(fmtnew
, "+64W ", sz
);
2498 /* finally add the action script output and the version */
2499 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2503 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2505 /* special case; no limit given, pad one space, instead */
2506 if (*limit
== (int)sz
- 1)
2508 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2511 /* replaces the bar format character sequences (like in tmux(1)) */
2513 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2518 char tmp
[SWM_BAR_MAX
];
2519 int limit
, size
, count
;
2522 /* reset strlcat(3) buffer */
2525 /* get number, if any */
2528 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2529 limit
= sizeof tmp
- 1;
2530 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2531 limit
= sizeof tmp
- 1;
2533 /* there is nothing to replace (ie EOL) */
2540 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2543 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2546 bar_window_class(tmp
, sizeof tmp
, r
);
2549 bar_workspace_name(tmp
, sizeof tmp
, r
);
2552 bar_window_state(tmp
, sizeof tmp
, r
);
2555 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2559 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2563 snprintf(tmp
, sizeof tmp
, "%d", count
);
2566 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2569 bar_window_class_instance(tmp
, sizeof tmp
, r
);
2572 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2575 bar_window_instance(tmp
, sizeof tmp
, r
);
2578 bar_urgent(tmp
, sizeof tmp
);
2581 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2584 bar_window_name(tmp
, sizeof tmp
, r
);
2587 /* unknown character sequence; copy as-is */
2588 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2594 if ((int)len
< limit
)
2596 while (limit
-- > 0) {
2597 if (*offrep
>= sz
- 1)
2599 fmtrep
[(*offrep
)++] = *ptr
++;
2607 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2612 while (*fmt
!= '\0') {
2614 /* skip ordinary characters */
2617 fmtrep
[off
++] = *fmt
++;
2621 /* character sequence found; replace it */
2622 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2631 bar_fmt_expand(char *fmtexp
, size_t sz
)
2638 /* start by grabbing the current time and date */
2640 localtime_r(&tmt
, &tm
);
2642 /* figure out what to expand */
2643 if (bar_format
!= NULL
)
2645 else if (bar_format
== NULL
&& clock_enabled
)
2647 /* if nothing to expand bail out */
2653 /* copy as-is, just in case the format shouldn't be expanded below */
2654 strlcpy(fmtexp
, fmt
, sz
);
2655 /* finally pass the string through strftime(3) */
2656 #ifndef SWM_DENY_CLOCK_FORMAT
2657 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2658 warnx("format too long");
2663 /* Redraws a region bar; need to follow with xcb_flush() or focus_flush(). */
2665 bar_draw(struct swm_bar
*bar
)
2667 struct swm_region
*r
;
2668 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2669 char fmtrep
[SWM_BAR_MAX
];
2671 /* expand the format by first passing it through strftime(3) */
2672 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2679 if (bar_enabled
&& r
->ws
->bar_enabled
)
2680 xcb_map_window(conn
, bar
->id
);
2682 xcb_unmap_window(conn
, bar
->id
);
2686 if (startup_exception
)
2687 snprintf(fmtrep
, sizeof fmtrep
, "total "
2688 "exceptions: %d, first exception: %s",
2692 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2693 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2696 if (bar_font_legacy
)
2697 bar_print_legacy(r
, fmtrep
);
2699 bar_print(r
, fmtrep
);
2703 * Reads external script output; call when stdin is readable.
2704 * Returns 1 if bar_ext was updated; otherwise 0.
2707 bar_extra_update(void)
2710 char b
[SWM_BAR_MAX
];
2711 bool changed
= false;
2716 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2719 if (b
[len
- 1] == '\n') {
2720 /* Remove newline. */
2723 /* "Clear" bar_ext. */
2726 /* Flush buffered output. */
2727 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2728 bar_ext_buf
[0] = '\0';
2730 /* Append new output to bar. */
2731 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2735 /* Buffer output. */
2736 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2741 if (errno
!= EAGAIN
) {
2742 warn("bar_action failed");
2751 bar_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
2753 struct swm_region
*tmpr
;
2756 /* suppress unused warnings since vars are needed */
2761 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2764 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2765 /* Only change if master switch is enabled. */
2767 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2769 bar_enabled
= r
->ws
->bar_enabled
= true;
2771 case SWM_ARG_ID_BAR_TOGGLE
:
2772 bar_enabled
= !bar_enabled
;
2776 /* update bars as necessary */
2777 num_screens
= get_screen_count();
2778 for (i
= 0; i
< num_screens
; i
++)
2779 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2781 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2782 xcb_map_window(conn
, tmpr
->bar
->id
);
2784 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2787 /* Restack all regions and redraw bar. */
2788 num_screens
= get_screen_count();
2789 for (i
= 0; i
< num_screens
; i
++)
2790 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
2792 bar_draw(tmpr
->bar
);
2799 bar_extra_setup(void)
2801 /* do this here because the conf file is in memory */
2802 if (!bar_extra
&& bar_argv
[0]) {
2803 /* launch external status app */
2805 if (pipe(bar_pipe
) == -1)
2806 err(1, "pipe error");
2807 socket_setnonblock(bar_pipe
[0]);
2808 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2810 /* Set stdin to read from the pipe. */
2811 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2814 /* Set stdout to write to the pipe. */
2815 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2818 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2819 err(1, "could not disable SIGPIPE");
2820 switch (bar_pid
= fork()) {
2822 err(1, "cannot fork");
2826 execvp(bar_argv
[0], bar_argv
);
2827 err(1, "%s external app failed", bar_argv
[0]);
2829 default: /* parent */
2834 atexit(kill_bar_extra_atexit
);
2839 kill_bar_extra_atexit(void)
2842 kill(bar_pid
, SIGTERM
);
2850 while ((s
= index(s
, '-'))) {
2855 return (count
== 14);
2861 char *default_string
;
2862 char **missing_charsets
;
2863 int num_missing_charsets
= 0;
2867 XFreeFontSet(display
, bar_fs
);
2871 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n",
2874 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2875 &num_missing_charsets
, &default_string
);
2877 if (num_missing_charsets
> 0) {
2878 warnx("Unable to load charset(s):");
2880 for (i
= 0; i
< num_missing_charsets
; ++i
)
2881 warnx("%s", missing_charsets
[i
]);
2883 XFreeStringList(missing_charsets
);
2885 if (strcmp(default_string
, ""))
2886 warnx("Glyphs from those sets will be replaced "
2887 "by '%s'.", default_string
);
2889 warnx("Glyphs from those sets won't be drawn.");
2893 errx(1, "Error creating font set structure.");
2895 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2897 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2898 2 * bar_border_width
;
2905 xft_init(struct swm_region
*r
)
2907 char *font
, *str
, *search
;
2910 if (bar_font
== NULL
) {
2911 if ((search
= str
= strdup(bar_fonts
)) == NULL
)
2912 errx(1, "insufficient memory.");
2914 while ((font
= strsep(&search
, ",")) != NULL
) {
2918 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2921 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2924 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2928 if (bar_font
== NULL
) {
2929 warnx("unable to load font %s", font
);
2932 DNPRINTF(SWM_D_INIT
, "successfully opened "
2940 if (bar_font
== NULL
)
2941 errx(1, "unable to open a font");
2943 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2945 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2946 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2947 warn("Xft error: unable to allocate color.");
2949 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR
].pixel
, color
);
2951 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2952 DefaultColormap(display
, r
->s
->idx
), &color
, &search_font_color
))
2953 warn("Xft error: unable to allocate color.");
2955 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2962 bar_setup(struct swm_region
*r
)
2964 xcb_screen_t
*screen
;
2967 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2970 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2971 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2976 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2977 err(1, "bar_setup: calloc: failed to allocate memory.");
2979 if (bar_font_legacy
)
2986 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2987 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2988 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2990 /* Assume region is unfocused when we create the bar. */
2991 r
->bar
->id
= xcb_generate_id(conn
);
2992 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2993 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
2994 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
2995 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
2997 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2998 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
2999 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
3000 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
3001 | XCB_CW_EVENT_MASK
, wa
);
3003 /* Stack bar window above region window to start. */
3005 wa
[1] = XCB_STACK_MODE_ABOVE
;
3007 xcb_configure_window(conn
, r
->bar
->id
, XCB_CONFIG_WINDOW_SIBLING
|
3008 XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
3010 r
->bar
->buffer
= xcb_generate_id(conn
);
3011 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
3012 WIDTH(r
->bar
), HEIGHT(r
->bar
));
3015 xcb_randr_select_input(conn
, r
->bar
->id
,
3016 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
3019 xcb_map_window(conn
, r
->bar
->id
);
3021 DNPRINTF(SWM_D_BAR
, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
3022 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
3029 bar_cleanup(struct swm_region
*r
)
3033 xcb_destroy_window(conn
, r
->bar
->id
);
3034 xcb_free_pixmap(conn
, r
->bar
->buffer
);
3040 set_win_state(struct ws_win
*win
, uint8_t state
)
3042 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
3044 DNPRINTF(SWM_D_EVENT
, "set_win_state: win %#x, state: %u\n",
3050 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
3051 a_state
, 32, 2, data
);
3055 get_win_state(xcb_window_t w
)
3057 xcb_get_property_reply_t
*r
;
3058 xcb_get_property_cookie_t c
;
3059 uint32_t result
= 0;
3061 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
3062 r
= xcb_get_property_reply(conn
, c
, NULL
);
3064 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
3065 result
= *((uint32_t *)xcb_get_property_value(r
));
3069 DNPRINTF(SWM_D_MISC
, "get_win_state property: win %#x state %u\n", w
,
3075 version(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3077 struct swm_region
*tmpr
;
3080 /* suppress unused warnings since vars are needed */
3085 bar_version
= !bar_version
;
3087 snprintf(bar_vertext
, sizeof bar_vertext
,
3088 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
3090 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
3092 num_screens
= get_screen_count();
3093 for (i
= 0; i
< num_screens
; i
++) {
3094 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
3095 bar_draw(tmpr
->bar
);
3102 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
3104 xcb_client_message_event_t ev
;
3112 name
= get_atom_name(a
);
3113 DNPRINTF(SWM_D_EVENT
, "client_msg: win %#x, atom: %s(%u), "
3115 win
->id
, name
, a
, t
);
3119 bzero(&ev
, sizeof ev
);
3120 ev
.response_type
= XCB_CLIENT_MESSAGE
;
3121 ev
.window
= win
->id
;
3124 ev
.data
.data32
[0] = a
;
3125 ev
.data
.data32
[1] = t
;
3127 xcb_send_event(conn
, 0, win
->id
,
3128 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
3131 /* synthetic response to a ConfigureRequest when not making a change */
3133 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
3135 xcb_configure_notify_event_t ce
;
3140 /* send notification of unchanged state. */
3141 bzero(&ce
, sizeof(ce
));
3142 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
3145 ce
.width
= WIDTH(win
);
3146 ce
.height
= HEIGHT(win
);
3147 ce
.border_width
= 0;
3148 ce
.override_redirect
= 0;
3153 ce
.window
= win
->id
;
3154 ce
.above_sibling
= XCB_WINDOW_NONE
;
3157 ce
.event
= ev
->window
;
3158 ce
.window
= ev
->window
;
3160 /* make response appear more WM_SIZE_HINTS-compliant */
3161 if (win
->sh
.flags
) {
3162 DNPRINTF(SWM_D_MISC
, "config_win: hints: win %#x,"
3163 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
3164 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
3165 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
3166 SH_INC_W(win
), SH_INC_H(win
));
3171 /* the hint may be set... to 0! */
3172 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
3173 ce
.width
= SH_MIN_W(win
);
3174 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
3175 ce
.height
= SH_MIN_H(win
);
3180 /* may also be advertized as 0 */
3181 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
3182 ce
.width
= SH_MAX_W(win
);
3183 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
3184 ce
.height
= SH_MAX_H(win
);
3187 /* resize increment. */
3189 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
3190 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
3192 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
3193 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
3197 /* adjust x and y for requested border_width. */
3198 ce
.x
+= ev
->border_width
;
3199 ce
.y
+= ev
->border_width
;
3201 ce
.above_sibling
= ev
->sibling
;
3204 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
3205 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
3206 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
3208 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
3213 count_win(struct workspace
*ws
, bool count_transient
)
3218 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3219 if (!count_transient
&& FLOATING(win
))
3225 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
3231 quit(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3233 /* suppress unused warnings since vars are needed */
3238 DNPRINTF(SWM_D_MISC
, "quit\n");
3243 lower_window(struct ws_win
*win
)
3245 struct ws_win
*target
= NULL
;
3246 struct workspace
*ws
;
3248 DNPRINTF(SWM_D_EVENT
, "lower_window: win %#x\n", WINID(win
));
3255 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3256 if (target
== win
|| ICONIC(target
))
3258 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3261 if (win
->transient
== target
->transient
)
3263 if (win
->transient
== target
->id
)
3266 if (FULLSCREEN(target
))
3268 if (FULLSCREEN(win
))
3270 if (MAXIMIZED(target
))
3274 if (ABOVE(target
) || TRANS(target
))
3276 if (ABOVE(win
) || TRANS(win
))
3280 /* Change stack position. */
3281 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3283 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3285 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3287 update_win_stacking(win
);
3290 if (swm_debug
& SWM_D_STACK
) {
3291 DPRINTF("=== stacking order (top down) === \n");
3292 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3293 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3294 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3295 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3296 YESNO(ICONIC(target
)));
3300 DNPRINTF(SWM_D_EVENT
, "lower_window: done\n");
3304 raise_window(struct ws_win
*win
)
3306 struct ws_win
*target
= NULL
;
3307 struct workspace
*ws
;
3309 DNPRINTF(SWM_D_EVENT
, "raise_window: win %#x\n", WINID(win
));
3316 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3317 if (target
== win
|| ICONIC(target
))
3319 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3321 if (TRANS(win
) && (win
->transient
== target
->transient
||
3322 win
->transient
== target
->id
))
3324 if (FULLSCREEN(win
))
3326 if (FULLSCREEN(target
))
3330 if (MAXIMIZED(target
))
3332 if (ABOVE(win
) || TRANS(win
) ||
3333 (win
->ws
->focus
== win
&& ws
->always_raise
))
3335 if (!ABOVE(target
) && !TRANS(target
))
3339 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3341 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3343 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3345 update_win_stacking(win
);
3348 if (swm_debug
& SWM_D_STACK
) {
3349 DPRINTF("=== stacking order (top down) === \n");
3350 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3351 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3352 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3353 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3354 YESNO(ICONIC(target
)));
3358 DNPRINTF(SWM_D_EVENT
, "raise_window: done\n");
3362 update_win_stacking(struct ws_win
*win
)
3364 struct ws_win
*sibling
;
3368 struct swm_region
*r
;
3371 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
3374 if (win
->frame
== XCB_WINDOW_NONE
) {
3375 DNPRINTF(SWM_D_EVENT
, "update_window_stacking: win %#x not "
3376 "reparented.\n", win
->id
);
3380 sibling
= TAILQ_NEXT(win
, stack_entry
);
3381 if (sibling
!= NULL
&& (FLOATING(win
) == FLOATING(sibling
) ||
3382 (win
->ws
->always_raise
&& win
->ws
->focus
== win
)))
3383 val
[0] = sibling
->frame
;
3384 else if (FLOATING(win
) || (win
->ws
->always_raise
&&
3385 win
->ws
->focus
== win
))
3386 val
[0] = r
->bar
->id
;
3390 DNPRINTF(SWM_D_EVENT
, "update_win_stacking: win %#x (%#x), "
3391 "sibling %#x\n", win
->frame
, win
->id
, val
[0]);
3393 val
[1] = XCB_STACK_MODE_ABOVE
;
3395 xcb_configure_window(conn
, win
->frame
, XCB_CONFIG_WINDOW_SIBLING
|
3396 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
3399 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
)
3405 map_window(struct ws_win
*win
)
3410 DNPRINTF(SWM_D_EVENT
, "map_window: win %#x, mapped: %s\n",
3411 win
->id
, YESNO(win
->mapped
));
3416 xcb_map_window(conn
, win
->frame
);
3417 xcb_map_window(conn
, win
->id
);
3418 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
3423 unmap_window(struct ws_win
*win
)
3428 DNPRINTF(SWM_D_EVENT
, "unmap_window: win %#x, mapped: %s\n", win
->id
,
3429 YESNO(win
->mapped
));
3434 xcb_unmap_window(conn
, win
->id
);
3435 xcb_unmap_window(conn
, win
->frame
);
3436 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
3437 win
->mapped
= false;
3444 int i
, j
, num_screens
;
3446 num_screens
= get_screen_count();
3447 for (i
= 0; i
< num_screens
; i
++)
3448 for (j
= 0; j
< workspace_limit
; j
++)
3449 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3454 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
3456 xcb_key_press_event_t event
;
3457 xcb_keycode_t
*keycode
;
3462 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
3464 DNPRINTF(SWM_D_MISC
, "fake_keypress: win %#x, keycode %u\n",
3467 bzero(&event
, sizeof(event
));
3468 event
.event
= win
->id
;
3469 event
.root
= win
->s
->root
;
3470 event
.child
= XCB_WINDOW_NONE
;
3471 event
.time
= XCB_CURRENT_TIME
;
3472 event
.event_x
= X(win
);
3473 event
.event_y
= Y(win
);
3476 event
.same_screen
= 1;
3477 event
.detail
= *keycode
;
3478 event
.state
= modifiers
;
3480 event
.response_type
= XCB_KEY_PRESS
;
3481 xcb_send_event(conn
, 1, win
->id
,
3482 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
3484 event
.response_type
= XCB_KEY_RELEASE
;
3485 xcb_send_event(conn
, 1, win
->id
,
3486 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
3492 restart(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3494 /* suppress unused warning since var is needed */
3499 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
3503 execvp(start_argv
[0], start_argv
);
3504 warn("execvp failed");
3505 quit(NULL
, NULL
, NULL
);
3509 get_pointer_win(xcb_window_t root
)
3511 struct ws_win
*win
= NULL
;
3512 xcb_query_pointer_reply_t
*r
;
3514 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: %#x.\n", root
);
3516 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
3518 win
= find_window(r
->child
);
3520 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: %#x.\n",
3523 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
3532 center_pointer(struct swm_region
*r
)
3536 if (!warp_pointer
|| r
== NULL
)
3541 DNPRINTF(SWM_D_EVENT
, "center_pointer: win %#x.\n", WINID(win
));
3543 if (win
&& win
->mapped
)
3544 xcb_warp_pointer(conn
, XCB_NONE
, win
->frame
, 0, 0, 0, 0,
3545 WIDTH(win
) / 2, HEIGHT(win
) / 2);
3547 xcb_warp_pointer(conn
, XCB_NONE
, r
->id
, 0, 0, 0, 0,
3548 WIDTH(r
) / 2, HEIGHT(r
) / 2);
3552 root_to_region(xcb_window_t root
, int check
)
3555 struct swm_region
*r
= NULL
;
3557 xcb_query_pointer_reply_t
*qpr
;
3558 xcb_get_input_focus_reply_t
*gifr
;
3560 DNPRINTF(SWM_D_MISC
, "root_to_region: win %#x\n", root
);
3562 num_screens
= get_screen_count();
3563 for (i
= 0; i
< num_screens
; i
++)
3564 if (screens
[i
].root
== root
)
3567 if (check
& SWM_CK_REGION
)
3568 r
= screens
[i
].r_focus
;
3570 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
3571 /* Try to find an actively focused window */
3572 gifr
= xcb_get_input_focus_reply(conn
,
3573 xcb_get_input_focus(conn
), NULL
);
3575 cfw
= find_window(gifr
->focus
);
3576 if (cfw
&& cfw
->ws
->r
)
3583 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
3584 /* No region with an active focus; try to use pointer. */
3585 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
3586 screens
[i
].root
), NULL
);
3588 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
3589 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
3590 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3591 if (X(r
) <= qpr
->root_x
&&
3592 qpr
->root_x
< MAX_X(r
) &&
3593 Y(r
) <= qpr
->root_y
&&
3594 qpr
->root_y
< MAX_Y(r
))
3601 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
3602 r
= TAILQ_FIRST(&screens
[i
].rl
);
3604 DNPRINTF(SWM_D_MISC
, "root_to_region: idx: %d\n", get_region_index(r
));
3610 find_region(xcb_window_t id
)
3612 struct swm_region
*r
;
3615 num_screens
= get_screen_count();
3616 for (i
= 0; i
< num_screens
; i
++)
3617 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3625 find_bar(xcb_window_t id
)
3627 struct swm_region
*r
;
3630 num_screens
= get_screen_count();
3631 for (i
= 0; i
< num_screens
; i
++)
3632 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3633 if (r
->bar
&& r
->bar
->id
== id
)
3640 find_frame_window(xcb_window_t id
) {
3641 struct swm_region
*r
;
3645 num_screens
= get_screen_count();
3646 for (i
= 0; i
< num_screens
; i
++)
3647 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3648 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
3656 find_window(xcb_window_t id
)
3658 struct ws_win
*win
= NULL
;
3659 int i
, j
, num_screens
;
3660 xcb_query_tree_reply_t
*qtr
;
3662 DNPRINTF(SWM_D_MISC
, "find_window: id: %#x\n", id
);
3664 num_screens
= get_screen_count();
3665 for (i
= 0; i
< num_screens
; i
++)
3666 for (j
= 0; j
< workspace_limit
; j
++)
3667 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3668 if (id
== win
->id
|| id
== win
->frame
)
3672 /* If window isn't top-level, try to find managed ancestor. */
3673 qtr
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3675 if (qtr
->parent
!= XCB_WINDOW_NONE
&& qtr
->parent
!= qtr
->root
)
3676 win
= find_window(qtr
->parent
);
3680 DNPRINTF(SWM_D_MISC
, "find_window: found child %#x "
3681 "of %#x.\n", win
->id
, qtr
->parent
);
3691 find_unmanaged_window(xcb_window_t id
)
3694 int i
, j
, num_screens
;
3696 num_screens
= get_screen_count();
3697 for (i
= 0; i
< num_screens
; i
++)
3698 for (j
= 0; j
< workspace_limit
; j
++)
3699 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
3707 spawn(int ws_idx
, union arg
*args
, bool close_fd
)
3712 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3714 close(xcb_get_file_descriptor(conn
));
3716 if ((ret
= getenv("LD_PRELOAD"))) {
3717 if (asprintf(&ret
, "%s:%s", SWM_LIB
, ret
) == -1) {
3718 warn("spawn: asprintf LD_PRELOAD");
3721 setenv("LD_PRELOAD", ret
, 1);
3724 setenv("LD_PRELOAD", SWM_LIB
, 1);
3727 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3728 warn("spawn: asprintf SWM_WS");
3731 setenv("_SWM_WS", ret
, 1);
3735 if (asprintf(&ret
, "%d", getpid()) == -1) {
3736 warn("spawn: asprintf _SWM_PID");
3739 setenv("_SWM_PID", ret
, 1);
3743 if (setsid() == -1) {
3744 warn("spawn: setsid");
3750 * close stdin and stdout to prevent interaction between apps
3751 * and the baraction script
3752 * leave stderr open to record errors
3754 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3755 warn("spawn: open");
3758 dup2(fd
, STDIN_FILENO
);
3759 dup2(fd
, STDOUT_FILENO
);
3764 execvp(args
->argv
[0], args
->argv
);
3766 warn("spawn: execvp");
3771 kill_refs(struct ws_win
*win
)
3773 struct workspace
*ws
;
3775 int i
, j
, num_screens
;
3780 num_screens
= get_screen_count();
3781 for (i
= 0; i
< num_screens
; i
++) {
3782 for (j
= 0; j
< workspace_limit
; j
++) {
3783 ws
= &screens
[i
].ws
[j
];
3785 if (win
== ws
->focus
)
3787 if (win
== ws
->focus_prev
)
3788 ws
->focus_prev
= NULL
;
3789 if (win
== ws
->focus_pending
)
3790 ws
->focus_pending
= NULL
;
3791 if (win
== ws
->focus_raise
)
3792 ws
->focus_raise
= NULL
;
3795 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3796 if (win
== w
->focus_child
)
3797 w
->focus_child
= NULL
;
3803 validate_win(struct ws_win
*testwin
)
3806 struct workspace
*ws
;
3807 struct swm_region
*r
;
3808 int i
, x
, num_screens
;
3810 if (testwin
== NULL
)
3813 num_screens
= get_screen_count();
3814 for (i
= 0; i
< num_screens
; i
++)
3815 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3816 for (x
= 0; x
< workspace_limit
; x
++) {
3818 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3826 validate_ws(struct workspace
*testws
)
3828 struct swm_region
*r
;
3829 struct workspace
*ws
;
3830 int i
, x
, num_screens
;
3832 /* validate all ws */
3833 num_screens
= get_screen_count();
3834 for (i
= 0; i
< num_screens
; i
++)
3835 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3836 for (x
= 0; x
< workspace_limit
; x
++) {
3845 unfocus_win(struct ws_win
*win
)
3847 xcb_window_t none
= XCB_WINDOW_NONE
;
3849 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: win %#x\n", WINID(win
));
3854 if (win
->ws
== NULL
) {
3855 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3859 if (validate_ws(win
->ws
)) {
3860 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3864 if (win
->ws
->r
== NULL
) {
3865 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3869 if (validate_win(win
)) {
3870 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3875 if (win
->ws
->focus
== win
) {
3876 win
->ws
->focus
= NULL
;
3877 win
->ws
->focus_prev
= win
;
3878 if (win
->ws
->focus_raise
== win
&& !FLOATING(win
)) {
3879 update_win_stacking(win
);
3883 if (validate_win(win
->ws
->focus
)) {
3884 kill_refs(win
->ws
->focus
);
3885 win
->ws
->focus
= NULL
;
3888 if (validate_win(win
->ws
->focus_prev
)) {
3889 kill_refs(win
->ws
->focus_prev
);
3890 win
->ws
->focus_prev
= NULL
;
3895 /* Raise window to "top unfocused position." */
3896 if (win
->ws
->always_raise
)
3899 /* Update border width */
3900 if (win
->bordered
&& (win
->quirks
& SWM_Q_MINIMALBORDER
) &&
3903 X(win
) += border_width
;
3904 Y(win
) += border_width
;
3908 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3909 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3911 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3915 focus_win(struct ws_win
*win
)
3917 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
, *tmpw
;
3918 struct workspace
*ws
;
3919 xcb_get_input_focus_reply_t
*gifr
= NULL
;
3920 xcb_get_window_attributes_reply_t
*war
= NULL
;
3922 DNPRINTF(SWM_D_FOCUS
, "focus_win: win %#x\n", WINID(win
));
3924 if (win
== NULL
|| win
->ws
== NULL
|| !win
->mapped
)
3929 if (validate_ws(ws
))
3932 if (validate_win(win
)) {
3937 gifr
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3939 DNPRINTF(SWM_D_FOCUS
, "focus_win: cur focus: %#x\n",
3942 cfw
= find_window(gifr
->focus
);
3945 if (cfw
->ws
!= ws
&& cfw
->ws
->r
!= NULL
&&
3946 cfw
->frame
!= XCB_WINDOW_NONE
) {
3953 war
= xcb_get_window_attributes_reply(conn
,
3954 xcb_get_window_attributes(conn
, gifr
->focus
), NULL
);
3955 if (war
&& war
->override_redirect
&& ws
->focus
== win
) {
3956 DNPRINTF(SWM_D_FOCUS
, "focus_win: skip refocus "
3957 "from override_redirect.\n");
3963 if (ws
->focus
!= win
) {
3964 if (ws
->focus
&& ws
->focus
!= cfw
)
3965 unfocus_win(ws
->focus
);
3969 /* Clear focus child redirect. */
3970 win
->focus_child
= NULL
;
3972 /* If transient, adjust parent's focus child for focus_magic. */
3974 parent
= find_window(win
->transient
);
3975 if (parent
&& parent
->focus_child
!= win
)
3976 parent
->focus_child
= win
;
3979 /* Update window border even if workspace is hidden. */
3983 DNPRINTF(SWM_D_FOCUS
, "focus_win: already focused.\n");
3988 /* Set input focus if no input hint, or indicated by hint. */
3989 if (ACCEPTS_FOCUS(win
)) {
3990 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
3991 " revert-to: parent, time: %#x\n", win
->id
,
3993 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
3994 win
->id
, last_event_time
);
3995 } else if (!win
->take_focus
) {
3996 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
3997 " revert-to: parent, time: 0\n", ws
->r
->id
);
3998 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
3999 ws
->r
->id
, XCB_CURRENT_TIME
);
4002 /* Tell app it can adjust focus to a specific window. */
4003 if (win
->take_focus
) {
4004 /* java is special; always tell parent */
4005 if (TRANS(win
) && win
->java
)
4006 client_msg(parent
, a_takefocus
,
4009 client_msg(win
, a_takefocus
, last_event_time
);
4012 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
4014 /* If a parent exists, map it first. */
4016 raise_window(parent
);
4019 /* Map siblings next. */
4020 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
,
4022 if (w
!= win
&& !ICONIC(w
) &&
4023 w
->transient
== parent
->id
) {
4029 /* Map focused window. */
4033 /* Stack any children of focus window. */
4034 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
4035 if (w
->transient
== win
->id
&& !ICONIC(w
)) {
4039 } else if (tile_gap
< 0 && !ABOVE(win
)) {
4041 * Windows overlap in the layout.
4042 * Raise focused win above all tiled wins.
4050 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4051 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4054 bar_draw(ws
->r
->bar
);
4060 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
4063 /* If a child window should have focus instead, return it. */
4065 get_focus_magic(struct ws_win
*win
)
4067 struct ws_win
*parent
= NULL
;
4068 struct ws_win
*child
= NULL
;
4070 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: win %#x\n", WINID(win
));
4075 parent
= find_window(win
->transient
);
4077 /* If parent prefers focus elsewhere, then try to do so. */
4078 if (parent
&& (child
= parent
->focus_child
)) {
4079 if (validate_win(child
) == 0 && child
->mapped
)
4082 parent
->focus_child
= NULL
;
4086 /* If this window prefers focus elsewhere, then try to do so. */
4087 if ((child
= win
->focus_child
)) {
4088 if (validate_win(child
) == 0 && child
->mapped
)
4091 win
->focus_child
= NULL
;
4098 event_drain(uint8_t rt
)
4100 xcb_generic_event_t
*evt
;
4102 /* ensure all pending requests have been processed before filtering. */
4104 while ((evt
= get_next_event(false))) {
4105 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
4113 set_region(struct swm_region
*r
)
4115 struct swm_region
*rf
;
4122 /* Unfocus old region bar. */
4127 xcb_change_window_attributes(conn
, rf
->bar
->id
,
4128 XCB_CW_BORDER_PIXEL
,
4129 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
4132 if (rf
!= NULL
&& rf
!= r
&& (X(rf
) != X(r
) || Y(rf
) != Y(r
) ||
4133 WIDTH(rf
) != WIDTH(r
) || HEIGHT(rf
) != HEIGHT(r
))) {
4134 /* Set _NET_DESKTOP_GEOMETRY. */
4136 vals
[1] = HEIGHT(r
);
4137 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, r
->s
->root
,
4138 ewmh
[_NET_DESKTOP_GEOMETRY
].atom
, XCB_ATOM_CARDINAL
, 32, 2,
4142 /* Set region bar border to focus_color. */
4143 xcb_change_window_attributes(conn
, r
->bar
->id
,
4144 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
4148 /* Update the focus window frame on the now unfocused region. */
4149 if (rf
&& rf
->ws
->focus
)
4150 draw_frame(rf
->ws
->focus
);
4152 ewmh_update_current_desktop();
4156 focus_region(struct swm_region
*r
)
4159 struct swm_region
*old_r
;
4164 old_r
= r
->s
->r_focus
;
4167 nfw
= get_region_focus(r
);
4171 /* New region is empty; need to manually unfocus win. */
4173 unfocus_win(old_r
->ws
->focus
);
4174 /* Clear bar since empty. */
4175 bar_draw(old_r
->bar
);
4178 DNPRINTF(SWM_D_FOCUS
, "focus_region: set_input_focus: %#x, "
4179 "revert-to: parent, time: 0\n", r
->id
);
4180 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4187 switchws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4189 struct swm_region
*this_r
, *other_r
;
4191 struct workspace
*new_ws
, *old_ws
;
4192 xcb_window_t none
= XCB_WINDOW_NONE
;
4193 int wsid
= args
->id
;
4194 bool unmap_old
= false;
4199 if (wsid
>= workspace_limit
)
4203 old_ws
= this_r
->ws
;
4204 new_ws
= &this_r
->s
->ws
[wsid
];
4206 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
4207 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
4209 if (new_ws
== NULL
|| old_ws
== NULL
)
4211 if (new_ws
== old_ws
)
4214 other_r
= new_ws
->r
;
4215 if (other_r
&& workspace_clamp
&&
4216 bp
->action
!= FN_RG_MOVE_NEXT
&& bp
->action
!= FN_RG_MOVE_PREV
) {
4217 DNPRINTF(SWM_D_WS
, "switchws: ws clamped.\n");
4220 DNPRINTF(SWM_D_WS
, "switchws: warping focus to region "
4221 "with ws %d.\n", wsid
);
4222 focus_region(other_r
);
4223 center_pointer(other_r
);
4229 if ((win
= old_ws
->focus
) != NULL
) {
4232 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4233 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4238 /* the other ws is visible in another region, exchange them */
4239 other_r
->ws_prior
= new_ws
;
4240 other_r
->ws
= old_ws
;
4241 old_ws
->r
= other_r
;
4243 /* the other workspace is hidden, hide this one */
4248 this_r
->ws_prior
= old_ws
;
4249 this_r
->ws
= new_ws
;
4252 /* Set focus_pending before stacking, if needed. */
4253 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
4254 validate_win(new_ws
->focus_pending
))) {
4255 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
4256 new_ws
->focus
= new_ws
->focus_prev
;
4257 new_ws
->focus_prev
= NULL
;
4260 new_ws
->state
= SWM_WS_STATE_MAPPING
;
4265 /* unmap old windows */
4267 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
4269 old_ws
->state
= SWM_WS_STATE_HIDDEN
;
4272 /* if workspaces were swapped, then don't wait to set focus */
4273 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
4274 if (new_ws
->focus_pending
) {
4275 focus_win(new_ws
->focus_pending
);
4276 new_ws
->focus_pending
= NULL
;
4280 /* Clear bar and set focus on region input win if new ws is empty. */
4281 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
4282 DNPRINTF(SWM_D_FOCUS
, "switchws: set_input_focus: %#x, "
4283 "revert-to: parent, time: 0\n", r
->id
);
4284 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4289 ewmh_update_current_desktop();
4293 new_ws
->state
= SWM_WS_STATE_MAPPED
;
4295 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
4299 cyclews(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4302 struct swm_screen
*s
= r
->s
;
4303 bool cycle_all
= false, mv
= false;
4305 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4306 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4312 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
4315 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
4318 case SWM_ARG_ID_CYCLEWS_UP
:
4319 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
4321 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
4324 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
4327 case SWM_ARG_ID_CYCLEWS_DOWN
:
4328 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
4335 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
4337 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
4341 send_to_ws(bp
, r
, &a
);
4343 switchws(bp
, r
, &a
);
4344 } while (a
.id
!= r
->ws
->idx
);
4346 DNPRINTF(SWM_D_FOCUS
, "cyclews: done\n");
4350 priorws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4356 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4357 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4359 if (r
->ws_prior
== NULL
)
4362 a
.id
= r
->ws_prior
->idx
;
4363 switchws(bp
, r
, &a
);
4364 DNPRINTF(SWM_D_FOCUS
, "priorws: done\n");
4368 focusrg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4370 int ridx
= args
->id
, i
, num_screens
;
4371 struct swm_region
*rr
= NULL
;
4375 num_screens
= get_screen_count();
4376 /* do nothing if we don't have more than one screen */
4377 if (!(num_screens
> 1 || outputs
> 1))
4380 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
4382 rr
= TAILQ_FIRST(&r
->s
->rl
);
4383 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
4384 rr
= TAILQ_NEXT(rr
, entry
);
4392 DNPRINTF(SWM_D_FOCUS
, "focusrg: done\n");
4396 cyclerg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4399 struct swm_region
*rr
= NULL
;
4402 num_screens
= get_screen_count();
4403 /* do nothing if we don't have more than one screen */
4404 if (!(num_screens
> 1 || outputs
> 1))
4408 DNPRINTF(SWM_D_FOCUS
, "cyclerg: id: %d, region: %d\n", args
->id
, i
);
4411 case SWM_ARG_ID_CYCLERG_UP
:
4412 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4413 rr
= TAILQ_NEXT(r
, entry
);
4415 rr
= TAILQ_FIRST(&screens
[i
].rl
);
4417 case SWM_ARG_ID_CYCLERG_DOWN
:
4418 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4419 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
4421 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
4430 case SWM_ARG_ID_CYCLERG_UP
:
4431 case SWM_ARG_ID_CYCLERG_DOWN
:
4436 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4437 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4439 switchws(bp
, r
, &a
);
4445 DNPRINTF(SWM_D_FOCUS
, "cyclerg: done\n");
4448 /* Sorts transients after parent. */
4450 sort_windows(struct ws_win_list
*wl
)
4452 struct ws_win
*win
, *parent
, *nxt
;
4457 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
4458 nxt
= TAILQ_NEXT(win
, entry
);
4460 parent
= find_window(win
->transient
);
4461 if (parent
== NULL
) {
4462 warnx("not possible bug");
4465 TAILQ_REMOVE(wl
, win
, entry
);
4466 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
4472 swapwin(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4474 struct ws_win
*target
, *source
;
4475 struct ws_win
*cur_focus
;
4476 struct ws_win_list
*wl
;
4480 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4481 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4483 cur_focus
= r
->ws
->focus
;
4484 if (cur_focus
== NULL
|| FULLSCREEN(cur_focus
))
4487 /* Adjust stacking in floating layer. */
4488 if (ABOVE(cur_focus
)) {
4490 case SWM_ARG_ID_SWAPPREV
:
4491 target
= TAILQ_PREV(cur_focus
, ws_win_stack
,
4493 if (target
!= NULL
&& FLOATING(target
)) {
4494 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4496 TAILQ_INSERT_BEFORE(target
, cur_focus
,
4498 update_win_stacking(cur_focus
);
4502 case SWM_ARG_ID_SWAPNEXT
:
4503 target
= TAILQ_NEXT(cur_focus
, stack_entry
);
4504 if (target
!= NULL
&& FLOATING(target
)) {
4505 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4507 TAILQ_INSERT_AFTER(&cur_focus
->ws
->stack
,
4508 target
, cur_focus
, stack_entry
);
4509 update_win_stacking(cur_focus
);
4517 if (r
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4520 clear_maximized(r
->ws
);
4523 wl
= &source
->ws
->winlist
;
4526 case SWM_ARG_ID_SWAPPREV
:
4528 source
= find_window(source
->transient
);
4529 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
4530 if (target
&& target
->transient
)
4531 target
= find_window(target
->transient
);
4532 TAILQ_REMOVE(wl
, source
, entry
);
4534 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4536 TAILQ_INSERT_BEFORE(target
, source
, entry
);
4538 case SWM_ARG_ID_SWAPNEXT
:
4539 target
= TAILQ_NEXT(source
, entry
);
4540 /* move the parent and let the sort handle the move */
4542 source
= find_window(source
->transient
);
4543 TAILQ_REMOVE(wl
, source
, entry
);
4545 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4547 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
4549 case SWM_ARG_ID_SWAPMAIN
:
4550 target
= TAILQ_FIRST(wl
);
4551 if (target
== source
) {
4552 if (source
->ws
->focus_prev
!= NULL
&&
4553 source
->ws
->focus_prev
!= target
)
4554 source
= source
->ws
->focus_prev
;
4558 if (target
== NULL
|| source
== NULL
)
4560 source
->ws
->focus_prev
= target
;
4561 TAILQ_REMOVE(wl
, target
, entry
);
4562 TAILQ_INSERT_BEFORE(source
, target
, entry
);
4563 TAILQ_REMOVE(wl
, source
, entry
);
4564 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4566 case SWM_ARG_ID_MOVELAST
:
4567 TAILQ_REMOVE(wl
, source
, entry
);
4568 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4571 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
4576 ewmh_update_client_list();
4582 DNPRINTF(SWM_D_MOVE
, "swapwin: done\n");
4586 get_focus_prev(struct ws_win
*win
)
4588 struct ws_win
*winfocus
= NULL
;
4589 struct ws_win
*cur_focus
= NULL
;
4590 struct ws_win_list
*wl
= NULL
;
4591 struct workspace
*ws
= NULL
;
4593 if (!(win
&& win
->ws
))
4598 cur_focus
= ws
->focus
;
4600 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: win %#x, cur_focus: %#x, "
4601 "focus_prev: %#x\n", WINID(win
), WINID(cur_focus
),
4602 WINID(ws
->focus_prev
));
4604 /* pickle, just focus on whatever */
4605 if (cur_focus
== NULL
) {
4606 /* use prev_focus if valid */
4607 if (ws
->focus_prev
&& find_window(ws
->focus_prev
->id
))
4608 winfocus
= ws
->focus_prev
;
4612 /* if transient focus on parent */
4613 if (TRANS(cur_focus
)) {
4614 winfocus
= find_window(cur_focus
->transient
);
4618 /* if in max_stack try harder */
4619 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
4620 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
4621 if (cur_focus
!= ws
->focus_prev
)
4622 winfocus
= ws
->focus_prev
;
4624 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
4629 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
4631 if (winfocus
== NULL
|| winfocus
== win
) {
4632 switch (focus_close
) {
4633 case SWM_STACK_BOTTOM
:
4634 TAILQ_FOREACH(winfocus
, wl
, entry
)
4635 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4639 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4640 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4643 case SWM_STACK_ABOVE
:
4644 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
4645 while (winfocus
&& ICONIC(winfocus
))
4646 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4648 if (winfocus
== NULL
) {
4649 if (focus_close_wrap
) {
4650 TAILQ_FOREACH(winfocus
, wl
, entry
)
4651 if (!ICONIC(winfocus
) &&
4652 winfocus
!= cur_focus
)
4655 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4657 if (!ICONIC(winfocus
) &&
4658 winfocus
!= cur_focus
)
4663 case SWM_STACK_BELOW
:
4664 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
4665 while (winfocus
&& ICONIC(winfocus
))
4666 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
4669 if (winfocus
== NULL
) {
4670 if (focus_close_wrap
) {
4671 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4673 if (!ICONIC(winfocus
) &&
4674 winfocus
!= cur_focus
)
4677 TAILQ_FOREACH(winfocus
, wl
, entry
)
4678 if (!ICONIC(winfocus
) &&
4679 winfocus
!= cur_focus
)
4687 if (winfocus
== NULL
||
4688 (winfocus
&& (ICONIC(winfocus
) || winfocus
== cur_focus
))) {
4689 if (focus_default
== SWM_STACK_TOP
) {
4690 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4691 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4694 TAILQ_FOREACH(winfocus
, wl
, entry
)
4695 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4702 return get_focus_magic(winfocus
);
4706 get_region_focus(struct swm_region
*r
)
4708 struct ws_win
*winfocus
= NULL
;
4713 if (r
->ws
->focus
&& !ICONIC(r
->ws
->focus
))
4714 winfocus
= r
->ws
->focus
;
4715 else if (r
->ws
->focus_prev
&& !ICONIC(r
->ws
->focus_prev
))
4716 winfocus
= r
->ws
->focus_prev
;
4718 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
4719 if (!ICONIC(winfocus
))
4722 return get_focus_magic(winfocus
);
4726 focus(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4728 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
4729 struct ws_win_list
*wl
= NULL
;
4730 struct workspace
*ws
= NULL
;
4737 cur_focus
= r
->ws
->focus
;
4741 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d, cur_focus: %#x\n", args
->id
,
4744 /* Make sure an uniconified window has focus, if one exists. */
4745 if (cur_focus
== NULL
) {
4746 cur_focus
= TAILQ_FIRST(wl
);
4747 while (cur_focus
!= NULL
&& ICONIC(cur_focus
))
4748 cur_focus
= TAILQ_NEXT(cur_focus
, entry
);
4750 DNPRINTF(SWM_D_FOCUS
, "focus: new cur_focus: %#x\n",
4755 case SWM_ARG_ID_FOCUSPREV
:
4756 if (cur_focus
== NULL
)
4759 winfocus
= cur_focus
;
4761 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
4762 if (winfocus
== NULL
)
4763 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
4764 if (winfocus
== cur_focus
)
4766 } while (winfocus
&& (ICONIC(winfocus
) ||
4767 winfocus
->id
== cur_focus
->transient
||
4768 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4769 winfocus
->transient
== cur_focus
->transient
) ||
4770 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4772 case SWM_ARG_ID_FOCUSNEXT
:
4773 if (cur_focus
== NULL
)
4776 winfocus
= cur_focus
;
4778 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4779 if (winfocus
== NULL
)
4780 winfocus
= TAILQ_FIRST(wl
);
4781 if (winfocus
== cur_focus
)
4783 } while (winfocus
&& (ICONIC(winfocus
) ||
4784 winfocus
->id
== cur_focus
->transient
||
4785 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4786 winfocus
->transient
== cur_focus
->transient
) ||
4787 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4789 case SWM_ARG_ID_FOCUSMAIN
:
4790 if (cur_focus
== NULL
)
4793 winfocus
= TAILQ_FIRST(wl
);
4794 if (winfocus
== cur_focus
)
4795 winfocus
= cur_focus
->ws
->focus_prev
;
4797 case SWM_ARG_ID_FOCUSURGENT
:
4798 /* Search forward for the next urgent window. */
4802 for (i
= 0; i
<= workspace_limit
; ++i
) {
4804 head
= TAILQ_FIRST(&r
->s
->ws
[(ws
->idx
+ i
) %
4805 workspace_limit
].winlist
);
4808 if (head
== cur_focus
) {
4810 winfocus
= cur_focus
;
4813 } else if (get_urgent(head
)) {
4818 head
= TAILQ_NEXT(head
, entry
);
4825 /* Switch ws if new focus is on a different ws. */
4826 if (winfocus
&& winfocus
->ws
!= ws
) {
4827 a
.id
= winfocus
->ws
->idx
;
4828 switchws(bp
, r
, &a
);
4835 if (clear_maximized(ws
) > 0)
4838 focus_win(get_focus_magic(winfocus
));
4843 DNPRINTF(SWM_D_FOCUS
, "focus: done\n");
4847 focus_pointer(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4851 /* Not needed for buttons since this is already done in buttonpress. */
4852 if (bp
->type
== KEYBIND
) {
4853 focus_win(get_pointer_win(r
->s
->root
));
4859 cycle_layout(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4861 struct workspace
*ws
= r
->ws
;
4863 /* suppress unused warning since var is needed */
4867 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
4870 if (ws
->cur_layout
->l_stack
== NULL
)
4871 ws
->cur_layout
= &layouts
[0];
4873 clear_maximized(ws
);
4878 focus_win(get_region_focus(r
));
4885 stack_config(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4887 struct workspace
*ws
= r
->ws
;
4891 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
4894 if (clear_maximized(ws
) > 0)
4897 if (ws
->cur_layout
->l_config
!= NULL
)
4898 ws
->cur_layout
->l_config(ws
, args
->id
);
4900 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
4909 stack(struct swm_region
*r
) {
4910 struct swm_geometry g
;
4911 struct swm_region
*r_prev
;
4917 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
4919 /* Adjust stack area for region bar and padding. */
4921 g
.x
+= region_padding
;
4922 g
.y
+= region_padding
;
4923 g
.w
-= 2 * border_width
+ 2 * region_padding
;
4924 g
.h
-= 2 * border_width
+ 2 * region_padding
;
4925 if (bar_enabled
&& r
->ws
->bar_enabled
) {
4931 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d (screen: "
4932 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4933 r
->ws
->idx
, r
->s
->idx
, get_region_index(r
), g
.x
, g
.y
, g
.w
, g
.h
);
4935 r_prev
= TAILQ_PREV(r
, swm_region_list
, entry
);
4937 /* Stack bar/input relative to prev. region. */
4938 val
[1] = XCB_STACK_MODE_ABOVE
;
4940 val
[0] = r_prev
->id
;
4941 DNPRINTF(SWM_D_STACK
, "stack: region input %#x "
4942 "relative to %#x.\n", r
->id
, val
[0]);
4943 xcb_configure_window(conn
, r
->id
,
4944 XCB_CONFIG_WINDOW_SIBLING
|
4945 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4947 val
[0] = r_prev
->bar
->id
;
4948 DNPRINTF(SWM_D_STACK
, "stack: region bar %#x "
4949 "relative to %#x.\n", r
->bar
->id
, val
[0]);
4950 xcb_configure_window(conn
, r
->bar
->id
,
4951 XCB_CONFIG_WINDOW_SIBLING
|
4952 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4955 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
4956 r
->ws
->cur_layout
->l_string(r
->ws
);
4957 /* save r so we can track region changes */
4963 DNPRINTF(SWM_D_STACK
, "stack: end\n");
4967 store_float_geom(struct ws_win
*win
)
4969 if (win
== NULL
|| win
->ws
->r
== NULL
)
4972 /* retain window geom and region geom */
4973 win
->g_float
= win
->g
;
4974 win
->g_float
.x
-= X(win
->ws
->r
);
4975 win
->g_float
.y
-= Y(win
->ws
->r
);
4976 win
->g_floatvalid
= true;
4977 DNPRINTF(SWM_D_MISC
, "store_float_geom: win %#x, g: (%d,%d)"
4978 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
4979 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
4980 win
->g_float
.w
, win
->g_float
.h
);
4984 load_float_geom(struct ws_win
*win
)
4986 if (win
== NULL
|| win
->ws
->r
== NULL
)
4989 if (win
->g_floatvalid
) {
4990 win
->g
= win
->g_float
;
4991 X(win
) += X(win
->ws
->r
);
4992 Y(win
) += Y(win
->ws
->r
);
4993 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g: (%d,%d)"
4994 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
4997 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g_float "
4998 "is not set.\n", win
->id
);
5003 update_floater(struct ws_win
*win
)
5005 struct workspace
*ws
;
5006 struct swm_region
*r
;
5008 DNPRINTF(SWM_D_MISC
, "update_floater: win %#x\n", WINID(win
));
5015 if ((r
= ws
->r
) == NULL
)
5018 win
->bordered
= true;
5020 if (FULLSCREEN(win
)) {
5021 /* _NET_WM_FULLSCREEN: fullscreen without border. */
5022 if (!win
->g_floatvalid
)
5023 store_float_geom(win
);
5026 win
->bordered
= false;
5027 } else if (MAXIMIZED(win
)) {
5028 /* Maximize: like a single stacked window. */
5029 if (!win
->g_floatvalid
)
5030 store_float_geom(win
);
5034 if (bar_enabled
&& ws
->bar_enabled
&& !maximize_hide_bar
) {
5036 Y(win
) += bar_height
;
5037 HEIGHT(win
) -= bar_height
;
5038 } else if (disable_border
) {
5039 win
->bordered
= false;
5042 if (win
->bordered
) {
5043 /* Window geometry excludes frame. */
5044 X(win
) += border_width
;
5045 Y(win
) += border_width
;
5046 HEIGHT(win
) -= 2 * border_width
;
5047 WIDTH(win
) -= 2 * border_width
;
5050 /* Normal floating window. */
5051 /* Update geometry if new region. */
5053 load_float_geom(win
);
5055 if (((win
->quirks
& SWM_Q_FULLSCREEN
) &&
5056 WIDTH(win
) >= WIDTH(r
) && HEIGHT(win
) >= HEIGHT(r
)) ||
5057 ((!WS_FOCUSED(win
->ws
) || win
->ws
->focus
!= win
) &&
5058 (win
->quirks
& SWM_Q_MINIMALBORDER
))) {
5060 win
->bordered
= false;
5061 } else if (!MANUAL(win
)) {
5062 if (TRANS(win
) && (win
->quirks
& SWM_Q_TRANSSZ
)) {
5063 /* Adjust size on TRANSSZ quirk. */
5064 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
5065 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
5068 if (!(win
->quirks
& SWM_Q_ANYWHERE
)) {
5070 * Floaters and transients are auto-centred
5071 * unless manually moved, resized or ANYWHERE
5074 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2;
5075 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2;
5076 store_float_geom(win
);
5081 /* Ensure at least 1 pixel of the window is in the region. */
5082 region_containment(win
, r
, SWM_CW_ALLSIDES
);
5088 * Send keystrokes to terminal to decrease/increase the font size as the
5089 * window size changes.
5092 adjust_font(struct ws_win
*win
)
5094 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
5095 ABOVE(win
) || TRANS(win
))
5098 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
5099 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
5100 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
5101 win
->font_size_boundary
[win
->font_steps
] =
5102 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
5105 win
->last_inc
= win
->sh
.width_inc
;
5106 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
5107 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
5108 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
5111 win
->last_inc
= win
->sh
.width_inc
;
5112 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
5116 #define SWAPXY(g) do { \
5118 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
5119 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
5122 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, bool flip
)
5124 struct swm_geometry cell
, r_g
= *g
;
5126 int i
= 0, j
= 0, s
= 0, stacks
= 0;
5127 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
5128 int hrh
= 0, extra
= 0, h_slice
= 0, last_h
= 0;
5129 int split
= 0, colno
= 0;
5130 int winno
, mwin
= 0, msize
= 0;
5131 int remain
, missing
, v_slice
, mscale
;
5132 bool bordered
= true, reconfigure
= false;
5135 * cell: geometry for window, including frame.
5136 * mwin: # of windows in master area.
5137 * mscale: size increment of master area.
5138 * stacks: # of stack columns
5141 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
5142 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
5144 memset(&cell
, 0, sizeof(cell
));
5146 /* Prepare tiling variables, if needed. */
5147 if ((winno
= count_win(ws
, false)) > 0) {
5148 /* Find first tiled window. */
5149 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
5150 if (!FLOATING(win
) && !ICONIC(win
))
5153 /* Take into account size hints of first tiled window. */
5155 w_inc
= win
->sh
.width_inc
;
5156 w_base
= win
->sh
.base_width
;
5157 mwin
= ws
->l_state
.horizontal_mwin
;
5158 mscale
= ws
->l_state
.horizontal_msize
;
5159 stacks
= ws
->l_state
.horizontal_stacks
;
5162 w_inc
= win
->sh
.height_inc
;
5163 w_base
= win
->sh
.base_height
;
5164 mwin
= ws
->l_state
.vertical_mwin
;
5165 mscale
= ws
->l_state
.vertical_msize
;
5166 stacks
= ws
->l_state
.vertical_stacks
;
5170 cell
.x
+= border_width
;
5171 cell
.y
+= border_width
;
5173 if (stacks
> winno
- mwin
)
5174 stacks
= winno
- mwin
;
5178 h_slice
= r_g
.h
/ SWM_H_SLICE
;
5179 if (mwin
&& winno
> mwin
) {
5180 v_slice
= r_g
.w
/ SWM_V_SLICE
;
5184 cell
.w
= v_slice
* mscale
;
5186 if (w_inc
> 1 && w_inc
< v_slice
) {
5187 /* Adjust for requested size increment. */
5188 remain
= (cell
.w
- w_base
) % w_inc
;
5194 cell
.x
+= r_g
.w
- msize
;
5197 colno
= split
= winno
/ stacks
;
5198 cell
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
5199 2 * border_width
) / stacks
);
5202 hrh
= r_g
.h
/ colno
;
5203 extra
= r_g
.h
- (colno
* hrh
);
5204 cell
.h
= hrh
- 2 * border_width
;
5205 i
= j
= 0, s
= stacks
;
5208 /* Update window geometry. */
5209 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
5213 if (FLOATING(win
)) {
5214 update_floater(win
);
5219 if (split
&& i
== split
) {
5220 colno
= (winno
- mwin
) / stacks
;
5221 if (s
<= (winno
- mwin
) % stacks
)
5224 hrh
= r_g
.h
/ colno
;
5225 extra
= r_g
.h
- (colno
* hrh
);
5228 cell
.x
+= cell
.w
+ 2 * border_width
+
5231 cell
.w
= (r_g
.w
- msize
-
5232 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
5234 cell
.w
+= (r_g
.w
- msize
-
5235 (stacks
* (2 * border_width
+ tile_gap
))) %
5239 cell
.x
-= cell
.w
+ 2 * border_width
+
5245 cell
.h
= hrh
- 2 * border_width
- tile_gap
;
5248 h_inc
= win
->sh
.width_inc
;
5249 h_base
= win
->sh
.base_width
;
5251 h_inc
= win
->sh
.height_inc
;
5252 h_base
= win
->sh
.base_height
;
5255 if (j
== colno
- 1) {
5256 cell
.h
= hrh
+ extra
;
5257 } else if (h_inc
> 1 && h_inc
< h_slice
) {
5258 /* adjust for window's requested size increment */
5259 remain
= (cell
.h
- h_base
) % h_inc
;
5260 missing
= h_inc
- remain
;
5262 if (missing
<= extra
|| j
== 0) {
5272 cell
.y
= r_g
.y
+ border_width
;
5274 cell
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
5276 /* Window coordinates exclude frame. */
5278 if (winno
> 1 || !disable_border
||
5279 (bar_enabled
&& ws
->bar_enabled
)) {
5286 if (X(win
) != cell
.y
|| Y(win
) != cell
.x
||
5287 WIDTH(win
) != cell
.h
|| HEIGHT(win
) != cell
.w
) {
5291 WIDTH(win
) = cell
.h
;
5292 HEIGHT(win
) = cell
.w
;
5295 if (X(win
) != cell
.x
|| Y(win
) != cell
.y
||
5296 WIDTH(win
) != cell
.w
|| HEIGHT(win
) != cell
.h
) {
5300 WIDTH(win
) = cell
.w
;
5301 HEIGHT(win
) = cell
.h
;
5306 X(win
) -= border_width
;
5307 Y(win
) -= border_width
;
5308 WIDTH(win
) += 2 * border_width
;
5309 HEIGHT(win
) += 2 * border_width
;
5312 if (bordered
!= win
->bordered
) {
5314 win
->bordered
= bordered
;
5327 /* Stack all windows from bottom up. */
5328 TAILQ_FOREACH_REVERSE(win
, &ws
->stack
, ws_win_stack
, stack_entry
)
5330 update_win_stacking(win
);
5332 /* Map all windows from top down. */
5333 TAILQ_FOREACH(win
, &ws
->stack
, stack_entry
)
5337 DNPRINTF(SWM_D_STACK
, "stack_master: done\n");
5341 vertical_config(struct workspace
*ws
, int id
)
5343 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
5347 case SWM_ARG_ID_STACKRESET
:
5348 case SWM_ARG_ID_STACKINIT
:
5349 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
5350 ws
->l_state
.vertical_mwin
= 1;
5351 ws
->l_state
.vertical_stacks
= 1;
5353 case SWM_ARG_ID_MASTERSHRINK
:
5354 if (ws
->l_state
.vertical_msize
> 1)
5355 ws
->l_state
.vertical_msize
--;
5357 case SWM_ARG_ID_MASTERGROW
:
5358 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
5359 ws
->l_state
.vertical_msize
++;
5361 case SWM_ARG_ID_MASTERADD
:
5362 ws
->l_state
.vertical_mwin
++;
5364 case SWM_ARG_ID_MASTERDEL
:
5365 if (ws
->l_state
.vertical_mwin
> 0)
5366 ws
->l_state
.vertical_mwin
--;
5368 case SWM_ARG_ID_STACKBALANCE
:
5369 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ (ws
->l_state
.vertical_stacks
+ 1);
5371 case SWM_ARG_ID_STACKINC
:
5372 ws
->l_state
.vertical_stacks
++;
5374 case SWM_ARG_ID_STACKDEC
:
5375 if (ws
->l_state
.vertical_stacks
> 1)
5376 ws
->l_state
.vertical_stacks
--;
5378 case SWM_ARG_ID_FLIPLAYOUT
:
5379 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
5387 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5389 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
5391 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
5395 horizontal_config(struct workspace
*ws
, int id
)
5397 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
5400 case SWM_ARG_ID_STACKRESET
:
5401 case SWM_ARG_ID_STACKINIT
:
5402 ws
->l_state
.horizontal_mwin
= 1;
5403 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
5404 ws
->l_state
.horizontal_stacks
= 1;
5406 case SWM_ARG_ID_MASTERSHRINK
:
5407 if (ws
->l_state
.horizontal_msize
> 1)
5408 ws
->l_state
.horizontal_msize
--;
5410 case SWM_ARG_ID_MASTERGROW
:
5411 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
5412 ws
->l_state
.horizontal_msize
++;
5414 case SWM_ARG_ID_MASTERADD
:
5415 ws
->l_state
.horizontal_mwin
++;
5417 case SWM_ARG_ID_MASTERDEL
:
5418 if (ws
->l_state
.horizontal_mwin
> 0)
5419 ws
->l_state
.horizontal_mwin
--;
5421 case SWM_ARG_ID_STACKBALANCE
:
5422 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ (ws
->l_state
.horizontal_stacks
+ 1);
5424 case SWM_ARG_ID_STACKINC
:
5425 ws
->l_state
.horizontal_stacks
++;
5427 case SWM_ARG_ID_STACKDEC
:
5428 if (ws
->l_state
.horizontal_stacks
> 1)
5429 ws
->l_state
.horizontal_stacks
--;
5431 case SWM_ARG_ID_FLIPLAYOUT
:
5432 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
5440 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5442 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
5444 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
5447 /* fullscreen view */
5449 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5451 struct swm_geometry gg
= *g
;
5452 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
, *tmpw
;
5455 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
5460 winno
= count_win(ws
, false);
5461 if (winno
== 0 && count_win(ws
, true) == 0)
5464 /* Figure out which top level window should be visible. */
5465 if (ws
->focus_pending
)
5466 win
= ws
->focus_pending
;
5469 else if (ws
->focus_prev
)
5470 win
= ws
->focus_prev
;
5472 win
= TAILQ_FIRST(&ws
->winlist
);
5474 DNPRINTF(SWM_D_STACK
, "max_stack: focus_pending: %#x, focus: %#x, "
5475 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws
->focus_pending
),
5476 WINID(ws
->focus
), WINID(ws
->focus_prev
),
5477 WINID(TAILQ_FIRST(&ws
->winlist
)), win
->id
);
5479 /* Update window geometry. */
5480 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5489 /* Set maximized flag for all maxed windows. */
5490 if (!MAXIMIZED(w
)) {
5491 /* Preserve floating geometry. */
5493 store_float_geom(w
);
5495 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_MAXIMIZED
);
5496 ewmh_update_wm_state(w
);
5499 /* Only reconfigure if necessary. */
5500 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
5501 HEIGHT(w
) != gg
.h
) {
5504 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
)) {
5505 w
->bordered
= false;
5506 WIDTH(w
) += 2 * border_width
;
5507 HEIGHT(w
) += 2 * border_width
;
5510 X(w
) += border_width
;
5511 Y(w
) += border_width
;
5518 /* If transient, stack parent and its children. */
5519 if (TRANS(win
) && (parent
= find_window(win
->transient
))) {
5520 raise_window(parent
);
5522 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5523 if (w
->transient
== parent
->id
)
5527 /* Make sure focus window is on top. */
5530 /* Stack any children of focus window. */
5531 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5532 if (w
->transient
== win
->id
)
5535 /* Map all windows. */
5536 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
5542 send_to_rg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5544 int ridx
= args
->id
, i
, num_screens
;
5545 struct swm_region
*rr
= NULL
;
5548 num_screens
= get_screen_count();
5549 /* do nothing if we don't have more than one screen */
5550 if (!(num_screens
> 1 || outputs
> 1))
5553 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
5555 rr
= TAILQ_FIRST(&r
->s
->rl
);
5556 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
5557 rr
= TAILQ_NEXT(rr
, entry
);
5564 send_to_ws(bp
, r
, &a
);
5568 region_under(struct swm_screen
*s
, int x
, int y
)
5570 struct swm_region
*r
= NULL
;
5572 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
5573 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
5574 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
5575 WIDTH(r
), HEIGHT(r
), x
, y
);
5576 if (X(r
) <= x
&& x
< MAX_X(r
))
5577 if (Y(r
) <= y
&& y
< MAX_Y(r
))
5584 /* Transfer focused window to target workspace and focus. */
5586 send_to_ws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5588 int wsid
= args
->id
;
5589 struct ws_win
*win
= NULL
;
5593 if (r
&& r
->ws
&& r
->ws
->focus
)
5598 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win %#x, ws %d\n", win
->id
, wsid
);
5600 if (wsid
< 0 || wsid
>= workspace_limit
)
5603 if (win
->ws
->idx
== wsid
)
5606 win_to_ws(win
, wsid
, true);
5608 /* Set new focus on target ws. */
5609 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5610 win
->ws
->focus_prev
= win
->ws
->focus
;
5611 win
->ws
->focus
= win
;
5612 win
->ws
->focus_pending
= NULL
;
5614 if (win
->ws
->focus_prev
)
5615 draw_frame(win
->ws
->focus_prev
);
5618 DNPRINTF(SWM_D_STACK
, "send_to_ws: focus_pending: %#x, focus: %#x, "
5619 "focus_prev: %#x, first: %#x, win: %#x\n",
5620 WINID(r
->ws
->focus_pending
), WINID(r
->ws
->focus
),
5621 WINID(r
->ws
->focus_prev
), WINID(TAILQ_FIRST(&r
->ws
->winlist
)),
5624 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5625 ewmh_update_wm_state(win
);
5627 /* Restack focused region. */
5630 /* If destination ws has a region, restack. */
5633 load_float_geom(win
);
5638 /* Set new focus on current ws. */
5639 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5640 if (r
->ws
->focus
!= NULL
) {
5641 focus_win(r
->ws
->focus
);
5643 DNPRINTF(SWM_D_FOCUS
, "send_to_ws: set_input_focus: "
5644 "%#x, revert-to: parent, time: 0\n", r
->id
);
5645 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
5646 r
->id
, XCB_CURRENT_TIME
);
5655 /* Transfer focused window to region-relative workspace and focus. */
5657 send_to_rg_relative(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5660 struct swm_region
*r_other
;
5662 if (args
->id
== 1) {
5663 r_other
= TAILQ_NEXT(r
, entry
);
5664 if (r_other
== NULL
)
5665 r_other
= TAILQ_FIRST(&r
->s
->rl
);
5667 r_other
= TAILQ_PREV(r
, swm_region_list
, entry
);
5668 if (r_other
== NULL
)
5669 r_other
= TAILQ_LAST(&r
->s
->rl
, swm_region_list
);
5672 /* Map relative to absolute */
5674 args_abs
.id
= r_other
->ws
->idx
;
5676 send_to_ws(bp
, r
, &args_abs
);
5680 win_to_ws(struct ws_win
*win
, int wsid
, bool unfocus
)
5682 struct ws_win
*parent
;
5683 struct workspace
*ws
, *nws
, *pws
;
5685 if (wsid
< 0 || wsid
>= workspace_limit
)
5688 if (win
->ws
->idx
== wsid
)
5692 nws
= &win
->s
->ws
[wsid
];
5694 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win %#x, ws %d -> %d\n", win
->id
,
5697 /* Cleanup focus on source ws. */
5698 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
5699 (ws
->focus
== win
|| ws
->focus_pending
== win
))
5700 ws
->focus_pending
= get_focus_prev(win
);
5702 /* Move the parent if this is a transient window. */
5704 parent
= find_window(win
->transient
);
5707 /* Set new focus in parent's ws if needed. */
5708 if (pws
->focus
== parent
) {
5709 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5710 pws
->focus_pending
=
5711 get_focus_prev(parent
);
5713 unfocus_win(parent
);
5715 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5716 pws
->focus
= pws
->focus_pending
;
5717 pws
->focus_pending
= NULL
;
5721 /* Don't unmap parent if new ws is visible */
5723 unmap_window(parent
);
5726 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
5727 TAILQ_REMOVE(&ws
->stack
, parent
, stack_entry
);
5728 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
5729 TAILQ_INSERT_TAIL(&nws
->stack
, parent
, stack_entry
);
5732 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5733 "_NET_WM_DESKTOP: %d\n", wsid
);
5734 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5735 parent
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
5736 XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5743 if (ws
->focus_prev
== win
)
5744 ws
->focus_prev
= NULL
;
5746 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& ws
->focus_pending
!= NULL
) {
5747 ws
->focus
= ws
->focus_pending
;
5748 ws
->focus_pending
= NULL
;
5751 /* Don't unmap if new ws is visible */
5756 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
5757 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
5758 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
5759 TAILQ_INSERT_TAIL(&nws
->stack
, win
, stack_entry
);
5762 /* Update the window's workspace property: _NET_WM_DESKTOP */
5763 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5764 "_NET_WM_DESKTOP: %d\n", wsid
);
5765 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
5766 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5768 ewmh_update_client_list();
5770 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
5774 pressbutton(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5776 /* suppress unused warning since var is needed */
5780 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
5781 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5782 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
5783 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5787 raise_focus(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5792 /* Suppress warning. */
5796 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
5800 r
->ws
->focus_raise
= win
;
5803 /* Temporarily override stacking order also in the stack */
5804 if (!FLOATING(win
)) {
5805 val
= XCB_STACK_MODE_ABOVE
;
5806 xcb_configure_window(conn
, win
->frame
,
5807 XCB_CONFIG_WINDOW_STACK_MODE
, &val
);
5812 raise_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5814 /* Suppress warning. */
5818 if (r
== NULL
|| r
->ws
== NULL
)
5821 if (r
->ws
->focus
&& MAXIMIZED(r
->ws
->focus
))
5824 r
->ws
->always_raise
= !r
->ws
->always_raise
;
5826 /* Update focused win stacking order based on new always_raise value. */
5827 raise_window(r
->ws
->focus
);
5833 iconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5837 /* Suppress warning. */
5841 if ((w
= r
->ws
->focus
) == NULL
)
5844 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_HIDDEN
);
5845 ewmh_update_wm_state(w
);
5853 get_win_name(xcb_window_t win
)
5856 xcb_get_property_cookie_t c
;
5857 xcb_get_property_reply_t
*r
;
5859 /* First try _NET_WM_NAME for UTF-8. */
5860 c
= xcb_get_property(conn
, 0, win
, ewmh
[_NET_WM_NAME
].atom
,
5861 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5862 r
= xcb_get_property_reply(conn
, c
, NULL
);
5863 if (r
&& r
->type
== XCB_NONE
) {
5865 /* Use WM_NAME instead; no UTF-8. */
5866 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
5867 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5868 r
= xcb_get_property_reply(conn
, c
, NULL
);
5871 if (r
&& r
->type
!= XCB_NONE
&& r
->length
> 0)
5872 name
= strndup(xcb_get_property_value(r
),
5873 xcb_get_property_value_length(r
));
5878 err(1, "get_win_name: failed to allocate memory.");
5886 uniconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5895 DNPRINTF(SWM_D_MISC
, "uniconify\n");
5897 if (r
== NULL
|| r
->ws
== NULL
)
5900 /* make sure we have anything to uniconify */
5901 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5902 if (win
->ws
== NULL
)
5903 continue; /* should never happen */
5909 DNPRINTF(SWM_D_MISC
, "uniconify: count: %d\n", count
);
5915 search_resp_action
= SWM_SEARCH_UNICONIFY
;
5917 spawn_select(r
, args
, "search", &searchpid
);
5919 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5922 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5923 if (win
->ws
== NULL
)
5924 continue; /* should never happen */
5928 name
= get_win_name(win
->id
);
5929 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
5937 name_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5943 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
5949 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
5951 spawn_select(r
, args
, "name_workspace", &searchpid
);
5953 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5956 fprintf(lfile
, "%s", "");
5961 search_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5964 struct workspace
*ws
;
5969 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
5975 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
5977 spawn_select(r
, args
, "search", &searchpid
);
5979 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5982 for (i
= 0; i
< workspace_limit
; i
++) {
5986 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
5987 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
5994 search_win_cleanup(void)
5996 struct search_window
*sw
= NULL
;
5998 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
5999 xcb_destroy_window(conn
, sw
->indicator
);
6000 TAILQ_REMOVE(&search_wl
, sw
, entry
);
6006 search_win(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6008 struct ws_win
*win
= NULL
;
6009 struct search_window
*sw
= NULL
;
6012 xcb_screen_t
*screen
;
6013 int i
, width
, height
;
6021 XRectangle l_ibox
, l_lbox
;
6025 DNPRINTF(SWM_D_MISC
, "search_win\n");
6028 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
6030 spawn_select(r
, args
, "search", &searchpid
);
6032 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
6035 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
6036 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
6038 TAILQ_INIT(&search_wl
);
6041 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
6045 sw
= calloc(1, sizeof(struct search_window
));
6047 warn("search_win: calloc");
6049 search_win_cleanup();
6055 snprintf(s
, sizeof s
, "%d", i
);
6058 w
= xcb_generate_id(conn
);
6059 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6060 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6061 wa
[2] = screen
->default_colormap
;
6063 if (bar_font_legacy
) {
6064 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
6065 width
= l_lbox
.width
+ 4;
6066 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
6068 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
6070 width
= info
.width
+ 4;
6071 height
= bar_font
->height
+ 4;
6074 xcb_create_window(conn
, screen
->root_depth
, w
, win
->frame
, 0, 0,
6075 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
6076 screen
->root_visual
, XCB_CW_BACK_PIXEL
|
6077 XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
, wa
);
6079 xcb_map_window(conn
, w
);
6082 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
6084 if (bar_font_legacy
) {
6085 l_gcv
.graphics_exposures
= 0;
6086 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
6088 XSetForeground(display
, l_draw
,
6089 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
6091 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
6092 (bar_fs_extents
->max_logical_extent
.height
-
6093 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
6095 XFreeGC(display
, l_draw
);
6098 draw
= XftDrawCreate(display
, w
,
6099 DefaultVisual(display
, r
->s
->idx
),
6100 DefaultColormap(display
, r
->s
->idx
));
6102 XftDrawStringUtf8(draw
, &search_font_color
, bar_font
, 2,
6103 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
6104 bar_font
->descent
, (FcChar8
*)s
, len
);
6106 XftDrawDestroy(draw
);
6109 DNPRINTF(SWM_D_MISC
, "search_win: mapped win %#x\n", w
);
6111 fprintf(lfile
, "%d\n", i
);
6121 search_resp_uniconify(const char *resp
, size_t len
)
6127 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
6129 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
6132 name
= get_win_name(win
->id
);
6133 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
6138 if (strncmp(s
, resp
, len
) == 0) {
6139 /* XXX this should be a callback to generalize */
6140 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_HIDDEN
);
6141 ewmh_update_wm_state(win
);
6151 search_resp_name_workspace(const char *resp
, size_t len
)
6153 struct workspace
*ws
;
6155 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
6157 if (search_r
->ws
== NULL
)
6162 free(search_r
->ws
->name
);
6163 search_r
->ws
->name
= NULL
;
6167 ws
->name
= strdup(resp
);
6168 if (ws
->name
== NULL
) {
6169 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
6170 "strdup: %s", strerror(errno
));
6174 ewmh_update_desktop_names();
6175 ewmh_get_desktop_names();
6180 ewmh_update_desktop_names(void)
6182 char *name_list
= NULL
, *p
;
6183 int num_screens
, i
, j
, len
= 0, tot
= 0;
6185 num_screens
= get_screen_count();
6186 for (i
= 0; i
< num_screens
; ++i
) {
6187 for (j
= 0; j
< workspace_limit
; ++j
) {
6188 if (screens
[i
].ws
[j
].name
!= NULL
)
6189 len
+= strlen(screens
[i
].ws
[j
].name
);
6193 if ((name_list
= calloc(len
, sizeof(char))) == NULL
)
6194 err(1, "update_desktop_names: calloc: failed to "
6195 "allocate memory.");
6198 for (j
= 0; j
< workspace_limit
; ++j
) {
6199 if (screens
[i
].ws
[j
].name
!= NULL
) {
6200 len
= strlen(screens
[i
].ws
[j
].name
);
6201 memcpy(p
, screens
[i
].ws
[j
].name
, len
);
6210 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6211 screens
[i
].root
, ewmh
[_NET_DESKTOP_NAMES
].atom
,
6212 a_utf8_string
, 8, tot
, name_list
);
6222 ewmh_get_desktop_names(void)
6225 xcb_get_property_cookie_t c
;
6226 xcb_get_property_reply_t
*r
;
6227 int num_screens
, i
, j
, n
, k
;
6229 num_screens
= get_screen_count();
6230 for (i
= 0; i
< num_screens
; ++i
) {
6231 for (j
= 0; j
< workspace_limit
; ++j
) {
6232 free(screens
[i
].ws
[j
].name
);
6233 screens
[i
].ws
[j
].name
= NULL
;
6236 c
= xcb_get_property(conn
, 0, screens
[i
].root
,
6237 ewmh
[_NET_DESKTOP_NAMES
].atom
,
6238 a_utf8_string
, 0, UINT32_MAX
);
6239 r
= xcb_get_property_reply(conn
, c
, NULL
);
6243 names
= xcb_get_property_value(r
);
6244 n
= xcb_get_property_value_length(r
);
6246 for (j
= 0, k
= 0; j
< n
; ++j
) {
6247 if (*(names
+ j
) != '\0') {
6248 screens
[i
].ws
[k
].name
= strdup(names
+ j
);
6249 j
+= strlen(names
+ j
);
6258 ewmh_update_client_list(void)
6261 int num_screens
, i
, j
, k
= 0, count
= 0;
6264 num_screens
= get_screen_count();
6265 for (i
= 0; i
< num_screens
; ++i
) {
6266 for (j
= 0; j
< workspace_limit
; ++j
)
6267 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6270 DNPRINTF(SWM_D_PROP
, "ewmh_update_client_list: win count: %d\n",
6276 wins
= calloc(count
, sizeof(xcb_window_t
));
6278 err(1, "ewmh_update_client_list: calloc: failed to "
6279 "allocate memory.");
6281 for (j
= 0, k
= 0; j
< workspace_limit
; ++j
)
6282 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6283 wins
[k
++] = win
->id
;
6285 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6286 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
6287 XCB_ATOM_WINDOW
, 32, count
, wins
);
6294 ewmh_update_current_desktop(void)
6298 num_screens
= get_screen_count();
6299 for (i
= 0; i
< num_screens
; ++i
) {
6300 if (screens
[i
].r_focus
)
6301 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6302 screens
[i
].root
, ewmh
[_NET_CURRENT_DESKTOP
].atom
,
6303 XCB_ATOM_CARDINAL
, 32, 1,
6304 &screens
[i
].r_focus
->ws
->idx
);
6309 ewmh_update_desktops(void)
6311 int num_screens
, i
, j
;
6314 vals
= calloc(workspace_limit
* 2, sizeof(uint32_t));
6316 err(1, "ewmh_update_desktops: calloc: failed to allocate "
6319 num_screens
= get_screen_count();
6320 for (i
= 0; i
< num_screens
; i
++) {
6321 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6322 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
6323 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
6325 for (j
= 0; j
< workspace_limit
; ++j
) {
6326 if (screens
[i
].ws
[j
].r
!= NULL
) {
6327 vals
[j
* 2] = X(screens
[i
].ws
[j
].r
);
6328 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].r
);
6329 } else if (screens
[i
].ws
[j
].old_r
!= NULL
) {
6330 vals
[j
* 2] = X(screens
[i
].ws
[j
].old_r
);
6331 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].old_r
);
6333 vals
[j
* 2] = vals
[j
* 2 + 1] = 0;
6337 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6338 screens
[i
].root
, ewmh
[_NET_DESKTOP_VIEWPORT
].atom
,
6339 XCB_ATOM_CARDINAL
, 32, workspace_limit
* 2, vals
);
6346 search_resp_search_workspace(const char *resp
)
6353 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
6357 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
6364 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
6366 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
6373 switchws(NULL
, search_r
, &a
);
6377 search_resp_search_window(const char *resp
)
6382 struct search_window
*sw
;
6384 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
6388 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
6393 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
6395 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
6402 TAILQ_FOREACH(sw
, &search_wl
, entry
)
6403 if (idx
== sw
->idx
) {
6409 #define MAX_RESP_LEN 1024
6412 search_do_resp(void)
6418 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
6423 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
6424 warn("search: calloc");
6428 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
6430 warn("search: read error");
6433 resp
[rbytes
] = '\0';
6436 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
6437 * newline, so work around that by sanitizing the resp now.
6439 resp
[strcspn(resp
, "\n")] = '\0';
6442 switch (search_resp_action
) {
6443 case SWM_SEARCH_UNICONIFY
:
6444 search_resp_uniconify(resp
, len
);
6446 case SWM_SEARCH_NAME_WORKSPACE
:
6447 search_resp_name_workspace(resp
, len
);
6449 case SWM_SEARCH_SEARCH_WORKSPACE
:
6450 search_resp_search_workspace(resp
);
6452 case SWM_SEARCH_SEARCH_WINDOW
:
6453 search_resp_search_window(resp
);
6458 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
6459 search_win_cleanup();
6461 search_resp_action
= SWM_SEARCH_NONE
;
6462 close(select_resp_pipe
[0]);
6469 wkill(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6473 DNPRINTF(SWM_D_MISC
, "wkill: win %#x, id: %d\n", WINID(r
->ws
->focus
),
6476 if (r
->ws
->focus
== NULL
)
6479 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
6480 xcb_kill_client(conn
, r
->ws
->focus
->id
);
6482 if (r
->ws
->focus
->can_delete
)
6483 client_msg(r
->ws
->focus
, a_delete
, 0);
6489 clear_maximized(struct workspace
*ws
)
6494 /* Clear any maximized win(s) on ws, from bottom up. */
6495 TAILQ_FOREACH_REVERSE(w
, &ws
->stack
, ws_win_stack
, stack_entry
)
6497 ewmh_apply_flags(w
, w
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
6498 ewmh_update_wm_state(w
);
6506 maximize_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6508 struct ws_win
*w
= r
->ws
->focus
;
6510 /* suppress unused warning since var is needed */
6517 DNPRINTF(SWM_D_MISC
, "maximize_toggle: win %#x\n", w
->id
);
6522 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6525 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_MAXIMIZED
);
6526 ewmh_update_wm_state(w
);
6530 if (w
== w
->ws
->focus
)
6535 DNPRINTF(SWM_D_MISC
, "maximize_toggle: done\n");
6539 floating_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6541 struct ws_win
*w
= r
->ws
->focus
;
6543 /* suppress unused warning since var is needed */
6550 DNPRINTF(SWM_D_MISC
, "floating_toggle: win %#x\n", w
->id
);
6552 if (FULLSCREEN(w
) || TRANS(w
))
6555 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6558 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_ABOVE
);
6559 ewmh_update_wm_state(w
);
6563 if (w
== w
->ws
->focus
)
6568 DNPRINTF(SWM_D_MISC
, "floating_toggle: done\n");
6572 fullscreen_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6574 struct ws_win
*w
= r
->ws
->focus
;
6576 /* suppress unused warning since var is needed */
6583 DNPRINTF(SWM_D_MISC
, "fullscreen_toggle: win %#x\n", w
->id
);
6585 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_FULLSCREEN
);
6586 ewmh_update_wm_state(w
);
6590 if (w
== w
->ws
->focus
)
6595 DNPRINTF(SWM_D_MISC
, "fullscreen_toggle: done\n");
6598 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
6600 struct swm_geometry g
= r
->g
;
6601 int rt
, lt
, tp
, bm
, bw
;
6603 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
6606 * Perpendicular distance of each side of the window to the respective
6607 * side of the region boundary. Positive values indicate the side of
6608 * the window has passed beyond the region boundary.
6610 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) - MAX_X(r
) : bw
;
6611 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) : bw
;
6612 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) - MAX_Y(r
) : bw
;
6613 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) : bw
;
6615 DNPRINTF(SWM_D_MISC
, "region_containment: win %#x, rt: %d, lt: %d, "
6616 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
6617 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
6618 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
6621 * Disable containment if any of the flagged sides went beyond the
6622 * containment boundary, or if containment is disabled.
6624 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
6625 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
6626 /* Make sure window has at least 1 pixel in the region */
6627 g
.x
+= 1 - WIDTH(win
);
6628 g
.y
+= 1 - HEIGHT(win
);
6629 g
.w
+= 2 * WIDTH(win
) - 2;
6630 g
.h
+= 2 * HEIGHT(win
) - 2;
6633 constrain_window(win
, &g
, &opts
);
6636 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6638 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
6640 DNPRINTF(SWM_D_MISC
, "constrain_window: win %#x, (x,y) w x h: "
6641 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6642 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
6643 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
6644 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
6645 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
6646 YESNO(*opts
& SWM_CW_RESIZABLE
));
6648 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) > b
->x
+ b
->w
) {
6649 if (*opts
& SWM_CW_RESIZABLE
)
6650 WIDTH(win
) = b
->x
+ b
->w
- X(win
);
6652 X(win
) = b
->x
+ b
->w
- WIDTH(win
);
6655 if ((*opts
& SWM_CW_LEFT
) && X(win
) < b
->x
) {
6656 if (*opts
& SWM_CW_RESIZABLE
)
6657 WIDTH(win
) -= b
->x
- X(win
);
6662 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) > b
->y
+ b
->h
) {
6663 if (*opts
& SWM_CW_RESIZABLE
)
6664 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
);
6666 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
);
6669 if ((*opts
& SWM_CW_TOP
) && Y(win
) < b
->y
) {
6670 if (*opts
& SWM_CW_RESIZABLE
)
6671 HEIGHT(win
) -= b
->y
- Y(win
);
6676 if (*opts
& SWM_CW_RESIZABLE
) {
6679 if (HEIGHT(win
) < 1)
6685 draw_frame(struct ws_win
*win
)
6687 xcb_rectangle_t rect
[4];
6691 if (win
->frame
== XCB_WINDOW_NONE
) {
6692 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x not "
6693 "reparented.\n", win
->id
);
6697 if (!win
->bordered
) {
6698 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x frame "
6699 "disabled\n", win
->id
);
6702 if (WS_FOCUSED(win
->ws
) && win
->ws
->focus
== win
)
6703 pixel
= MAXIMIZED(win
) ?
6704 &win
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].pixel
:
6705 &win
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6707 pixel
= MAXIMIZED(win
) ?
6708 &win
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].pixel
:
6709 &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6711 /* Top (with corners) */
6714 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6715 rect
[n
].height
= border_width
;
6716 /* Left (without corners) */
6718 rect
[n
].y
= border_width
;
6719 rect
[n
].width
= border_width
;
6720 rect
[n
].height
= HEIGHT(win
);
6721 /* Right (without corners) */
6722 rect
[++n
].x
= border_width
+ WIDTH(win
);
6723 rect
[n
].y
= border_width
;
6724 rect
[n
].width
= border_width
;
6725 rect
[n
].height
= HEIGHT(win
);
6726 /* Bottom (with corners)*/
6728 rect
[n
].y
= border_width
+ HEIGHT(win
);
6729 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6730 rect
[n
].height
= border_width
;
6732 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, pixel
);
6733 xcb_poly_fill_rectangle(conn
, win
->frame
, win
->s
->bar_gc
, 4, rect
);
6737 update_window(struct ws_win
*win
)
6742 if (win
->frame
== XCB_WINDOW_NONE
) {
6743 DNPRINTF(SWM_D_EVENT
, "update_window: skip win %#x; "
6744 "not reparented.\n", win
->id
);
6748 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
6749 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
6750 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6752 /* Reconfigure frame. */
6753 if (win
->bordered
) {
6754 wc
[0] = X(win
) - border_width
;
6755 wc
[1] = Y(win
) - border_width
;
6756 wc
[2] = WIDTH(win
) + 2 * border_width
;
6757 wc
[3] = HEIGHT(win
) + 2 * border_width
;
6762 wc
[3] = HEIGHT(win
);
6767 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x frame %#x, (x,y) w x h: "
6768 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, win
->frame
, wc
[0],
6769 wc
[1], wc
[2], wc
[3], YESNO(win
->bordered
));
6771 xcb_configure_window(conn
, win
->frame
, mask
, wc
);
6773 /* Reconfigure client window. */
6774 wc
[0] = wc
[1] = win
->bordered
? border_width
: 0;
6776 wc
[3] = HEIGHT(win
);
6778 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x, (x,y) w x h: "
6779 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
6780 wc
[3], YESNO(win
->bordered
));
6781 xcb_configure_window(conn
, win
->id
, mask
, wc
);
6783 /* ICCCM 4.2.3 Synthetic ConfigureNotify when moved and not resized. */
6784 if ((X(win
) != win
->g_prev
.x
|| Y(win
) != win
->g_prev
.y
) &&
6785 (win
->java
|| (WIDTH(win
) == win
->g_prev
.w
&&
6786 HEIGHT(win
) == win
->g_prev
.h
))) {
6787 /* Java has special needs when moved together with a resize. */
6788 config_win(win
, NULL
);
6791 win
->g_prev
= win
->g
;
6795 SIMPLEQ_ENTRY(event
) entry
;
6796 xcb_generic_event_t
*ev
;
6798 SIMPLEQ_HEAD(event_queue
, event
) events
= SIMPLEQ_HEAD_INITIALIZER(events
);
6800 xcb_generic_event_t
*
6801 get_next_event(bool dowait
)
6804 xcb_generic_event_t
*evt
;
6806 /* Try queue first. */
6807 if ((ep
= SIMPLEQ_FIRST(&events
))) {
6809 SIMPLEQ_REMOVE_HEAD(&events
, entry
);
6812 evt
= xcb_wait_for_event(conn
);
6814 evt
= xcb_poll_for_event(conn
);
6820 put_back_event(xcb_generic_event_t
*evt
)
6823 if ((ep
= malloc(sizeof (struct event
))) == NULL
)
6824 err(1, "put_back_event: failed to allocate memory.");
6826 SIMPLEQ_INSERT_HEAD(&events
, ep
, entry
);
6829 /* Peeks at next event to detect auto-repeat. */
6831 keyrepeating(xcb_key_release_event_t
*kre
)
6833 xcb_generic_event_t
*evt
;
6835 /* Ensure repeating keypress is finished processing. */
6838 if ((evt
= get_next_event(false))) {
6839 put_back_event(evt
);
6841 if (XCB_EVENT_RESPONSE_TYPE(evt
) == XCB_KEY_PRESS
&&
6842 kre
->sequence
== evt
->sequence
&&
6843 kre
->detail
== ((xcb_key_press_event_t
*)evt
)->detail
)
6851 keybindreleased(struct binding
*bp
, xcb_key_release_event_t
*kre
)
6853 if (bp
->type
== KEYBIND
&& !keyrepeating(kre
) &&
6854 bp
->value
== xcb_key_press_lookup_keysym(syms
, kre
, 0))
6860 #define SWM_RESIZE_STEPS (50)
6863 resize_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
6865 xcb_timestamp_t timestamp
= 0;
6866 struct swm_region
*r
= NULL
;
6867 struct swm_geometry g
;
6868 int top
= 0, left
= 0;
6870 xcb_cursor_t cursor
;
6871 xcb_query_pointer_reply_t
*xpr
= NULL
;
6872 xcb_generic_event_t
*evt
;
6873 xcb_motion_notify_event_t
*mne
;
6874 bool resizing
, step
= false;
6880 if (FULLSCREEN(win
))
6883 /* In max_stack mode, should only resize transients. */
6884 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6887 DNPRINTF(SWM_D_EVENT
, "resize: win %#x, floating: %s, "
6888 "transient: %#x\n", win
->id
, YESNO(ABOVE(win
)),
6892 store_float_geom(win
);
6893 else if (!(TRANS(win
) || ABOVE(win
)))
6896 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6898 ewmh_update_wm_state(win
);
6904 /* It's possible for win to have been freed during focus_flush(). */
6905 if (validate_win(win
)) {
6906 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6911 case SWM_ARG_ID_WIDTHSHRINK
:
6912 WIDTH(win
) -= SWM_RESIZE_STEPS
;
6915 case SWM_ARG_ID_WIDTHGROW
:
6916 WIDTH(win
) += SWM_RESIZE_STEPS
;
6919 case SWM_ARG_ID_HEIGHTSHRINK
:
6920 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
6923 case SWM_ARG_ID_HEIGHTGROW
:
6924 HEIGHT(win
) += SWM_RESIZE_STEPS
;
6931 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6932 SWM_CW_HARDBOUNDARY
);
6934 store_float_geom(win
);
6938 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6939 SWM_CW_SOFTBOUNDARY
);
6942 /* get cursor offset from window root */
6943 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6950 if (xpr
->win_x
< WIDTH(win
) / 2)
6953 if (xpr
->win_y
< HEIGHT(win
) / 2)
6956 if (opt
== SWM_ARG_ID_CENTER
)
6957 cursor
= cursors
[XC_SIZING
].cid
;
6959 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
6960 XC_TOP_RIGHT_CORNER
].cid
;
6962 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
6963 XC_BOTTOM_RIGHT_CORNER
].cid
;
6965 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6966 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
6969 /* Release keyboard freeze if called via keybind. */
6970 if (bp
->type
== KEYBIND
)
6971 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6976 while (resizing
&& (evt
= get_next_event(true))) {
6977 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6978 case XCB_BUTTON_RELEASE
:
6979 if (bp
->type
== BTNBIND
&& bp
->value
==
6980 ((xcb_button_release_event_t
*)evt
)->detail
)
6983 case XCB_KEY_RELEASE
:
6984 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
6987 case XCB_MOTION_NOTIFY
:
6988 mne
= (xcb_motion_notify_event_t
*)evt
;
6989 DNPRINTF(SWM_D_EVENT
, "resize: MOTION_NOTIFY: "
6990 "root: %#x\n", mne
->root
);
6992 /* cursor offset/delta from start of the operation */
6993 dx
= mne
->root_x
- xpr
->root_x
;
6994 dy
= mne
->root_y
- xpr
->root_y
;
7000 if (opt
== SWM_ARG_ID_CENTER
) {
7001 if (g
.h
/ 2 + dy
< 1)
7005 HEIGHT(win
) = g
.h
+ 2 * dy
;
7013 HEIGHT(win
) = g
.h
+ dy
;
7020 if (opt
== SWM_ARG_ID_CENTER
) {
7021 if (g
.w
/ 2 + dx
< 1)
7025 WIDTH(win
) = g
.w
+ 2 * dx
;
7033 WIDTH(win
) = g
.w
+ dx
;
7036 /* not free, don't sync more than 120 times / second */
7037 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
7038 timestamp
= mne
->time
;
7039 regionize(win
, mne
->root_x
, mne
->root_y
);
7040 region_containment(win
, r
, SWM_CW_ALLSIDES
|
7041 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
7042 SWM_CW_SOFTBOUNDARY
);
7047 case XCB_BUTTON_PRESS
:
7049 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_PRESS ignored\n");
7050 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7051 ((xcb_button_press_event_t
*)evt
)->time
);
7056 DNPRINTF(SWM_D_EVENT
, "resize: KEY_PRESS ignored\n");
7057 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7058 ((xcb_key_press_event_t
*)evt
)->time
);
7064 /* It's possible for win to have been freed above. */
7065 if (validate_win(win
)) {
7066 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
7074 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
7075 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
7079 store_float_geom(win
);
7081 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7083 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
7087 resize(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
7089 struct ws_win
*win
= NULL
;
7090 xcb_query_pointer_reply_t
*qpr
= NULL
;
7095 if (args
->id
!= SWM_ARG_ID_DONTCENTER
&&
7096 args
->id
!= SWM_ARG_ID_CENTER
) {
7097 /* {height,width}_{grow,shrink} use the focus window. */
7101 /* resize uses window under pointer. */
7102 qpr
= xcb_query_pointer_reply(conn
,
7103 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7105 win
= find_window(qpr
->child
);
7111 resize_win(win
, bp
, args
->id
);
7113 if (args
->id
&& bp
->type
== KEYBIND
)
7119 /* Try to set window region based on supplied coordinates or window center. */
7121 regionize(struct ws_win
*win
, int x
, int y
)
7123 struct swm_region
*r
= NULL
;
7125 r
= region_under(win
->s
, x
, y
);
7127 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
7128 Y(win
) + HEIGHT(win
) / 2);
7130 if (r
&& r
!= win
->ws
->r
) {
7131 clear_maximized(r
->ws
);
7133 win_to_ws(win
, r
->ws
->idx
, false);
7135 /* Stack old and new region. */
7139 /* Set focus on new ws. */
7140 unfocus_win(r
->ws
->focus
);
7148 #define SWM_MOVE_STEPS (50)
7151 move_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
7153 struct swm_region
*r
;
7154 xcb_timestamp_t timestamp
= 0;
7155 xcb_query_pointer_reply_t
*qpr
= NULL
;
7156 xcb_generic_event_t
*evt
;
7157 xcb_motion_notify_event_t
*mne
;
7158 bool moving
, restack
= false, step
= false;
7163 if ((r
= win
->ws
->r
) == NULL
)
7166 if (FULLSCREEN(win
))
7169 DNPRINTF(SWM_D_EVENT
, "move: win %#x, floating: %s, transient: "
7170 "%#x\n", win
->id
, YESNO(ABOVE(win
)), win
->transient
);
7172 /* in max_stack mode should only move transients */
7173 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
7176 if (!(ABOVE(win
) || TRANS(win
)) || MAXIMIZED(win
)) {
7177 store_float_geom(win
);
7181 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
7183 ewmh_update_wm_state(win
);
7190 /* It's possible for win to have been freed during focus_flush(). */
7191 if (validate_win(win
)) {
7192 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7197 case SWM_ARG_ID_MOVELEFT
:
7198 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
7201 case SWM_ARG_ID_MOVERIGHT
:
7202 X(win
) += (SWM_MOVE_STEPS
- border_width
);
7205 case SWM_ARG_ID_MOVEUP
:
7206 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
7209 case SWM_ARG_ID_MOVEDOWN
:
7210 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
7217 regionize(win
, -1, -1);
7218 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
7220 store_float_geom(win
);
7224 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
7225 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
7226 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
7228 /* get cursor offset from window root */
7229 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
7232 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7236 /* Release keyboard freeze if called via keybind. */
7237 if (bp
->type
== KEYBIND
)
7238 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7241 regionize(win
, qpr
->root_x
, qpr
->root_y
);
7242 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7243 SWM_CW_SOFTBOUNDARY
);
7247 while (moving
&& (evt
= get_next_event(true))) {
7248 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
7249 case XCB_BUTTON_RELEASE
:
7250 if (bp
->type
== BTNBIND
&& bp
->value
==
7251 ((xcb_button_release_event_t
*)evt
)->detail
)
7254 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7255 ((xcb_button_release_event_t
*)evt
)->time
);
7258 case XCB_KEY_RELEASE
:
7259 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
7262 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7263 ((xcb_key_release_event_t
*)evt
)->time
);
7266 case XCB_MOTION_NOTIFY
:
7267 mne
= (xcb_motion_notify_event_t
*)evt
;
7268 DNPRINTF(SWM_D_EVENT
, "move: MOTION_NOTIFY: "
7269 "root: %#x\n", mne
->root
);
7270 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
7271 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
7273 /* not free, don't sync more than 120 times / second */
7274 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
7275 timestamp
= mne
->time
;
7276 regionize(win
, mne
->root_x
, mne
->root_y
);
7277 region_containment(win
, win
->ws
->r
,
7278 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
7283 case XCB_BUTTON_PRESS
:
7284 /* Thaw and ignore. */
7285 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7286 ((xcb_button_press_event_t
*)evt
)->time
);
7291 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7292 ((xcb_key_press_event_t
*)evt
)->time
);
7298 /* It's possible for win to have been freed. */
7299 if (validate_win(win
)) {
7300 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7308 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7309 SWM_CW_SOFTBOUNDARY
);
7313 store_float_geom(win
);
7315 /* New region set to fullscreen layout. */
7316 if (win
->ws
->r
&& win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
]) {
7323 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7324 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
7328 move(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
7330 struct ws_win
*win
= NULL
;
7331 xcb_query_pointer_reply_t
*qpr
= NULL
;
7337 /* move_* uses focus window. */
7341 /* Disallow move_ on tiled. */
7342 if (win
&& !(TRANS(win
) || ABOVE(win
)))
7345 /* move uses window under pointer. */
7346 qpr
= xcb_query_pointer_reply(conn
,
7347 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7349 win
= find_window(qpr
->child
);
7355 move_win(win
, bp
, args
->id
);
7357 if (args
->id
&& bp
->type
== KEYBIND
)
7363 /* action definitions */
7365 char name
[SWM_FUNCNAME_LEN
];
7366 void (*func
)(struct binding
*, struct swm_region
*,
7370 } actions
[FN_INVALID
+ 2] = {
7371 /* name function argument */
7372 { "bar_toggle", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
7373 { "bar_toggle_ws", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
7374 { "button2", pressbutton
, 0, {.id
= 2} },
7375 { "cycle_layout", cycle_layout
, 0, {0} },
7376 { "flip_layout", stack_config
, 0, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
7377 { "float_toggle", floating_toggle
,0, {0} },
7378 { "focus", focus_pointer
, 0, {0} },
7379 { "focus_main", focus
, 0, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
7380 { "focus_next", focus
, 0, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
7381 { "focus_prev", focus
, 0, {.id
= SWM_ARG_ID_FOCUSPREV
} },
7382 { "focus_urgent", focus
, 0, {.id
= SWM_ARG_ID_FOCUSURGENT
} },
7383 { "fullscreen_toggle", fullscreen_toggle
, 0, {0} },
7384 { "maximize_toggle", maximize_toggle
,0, {0} },
7385 { "height_grow", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
7386 { "height_shrink", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
7387 { "iconify", iconify
, 0, {0} },
7388 { "master_shrink", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
7389 { "master_grow", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERGROW
} },
7390 { "master_add", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERADD
} },
7391 { "master_del", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERDEL
} },
7392 { "move", move
, FN_F_NOREPLAY
, {0} },
7393 { "move_down", move
, 0, {.id
= SWM_ARG_ID_MOVEDOWN
} },
7394 { "move_left", move
, 0, {.id
= SWM_ARG_ID_MOVELEFT
} },
7395 { "move_right", move
, 0, {.id
= SWM_ARG_ID_MOVERIGHT
} },
7396 { "move_up", move
, 0, {.id
= SWM_ARG_ID_MOVEUP
} },
7397 { "mvrg_1", send_to_rg
, 0, {.id
= 0} },
7398 { "mvrg_2", send_to_rg
, 0, {.id
= 1} },
7399 { "mvrg_3", send_to_rg
, 0, {.id
= 2} },
7400 { "mvrg_4", send_to_rg
, 0, {.id
= 3} },
7401 { "mvrg_5", send_to_rg
, 0, {.id
= 4} },
7402 { "mvrg_6", send_to_rg
, 0, {.id
= 5} },
7403 { "mvrg_7", send_to_rg
, 0, {.id
= 6} },
7404 { "mvrg_8", send_to_rg
, 0, {.id
= 7} },
7405 { "mvrg_9", send_to_rg
, 0, {.id
= 8} },
7406 { "mvrg_next", send_to_rg_relative
, 0, {.id
= 1} },
7407 { "mvrg_prev", send_to_rg_relative
, 0, {.id
= -1} },
7408 { "mvws_1", send_to_ws
, 0, {.id
= 0} },
7409 { "mvws_2", send_to_ws
, 0, {.id
= 1} },
7410 { "mvws_3", send_to_ws
, 0, {.id
= 2} },
7411 { "mvws_4", send_to_ws
, 0, {.id
= 3} },
7412 { "mvws_5", send_to_ws
, 0, {.id
= 4} },
7413 { "mvws_6", send_to_ws
, 0, {.id
= 5} },
7414 { "mvws_7", send_to_ws
, 0, {.id
= 6} },
7415 { "mvws_8", send_to_ws
, 0, {.id
= 7} },
7416 { "mvws_9", send_to_ws
, 0, {.id
= 8} },
7417 { "mvws_10", send_to_ws
, 0, {.id
= 9} },
7418 { "mvws_11", send_to_ws
, 0, {.id
= 10} },
7419 { "mvws_12", send_to_ws
, 0, {.id
= 11} },
7420 { "mvws_13", send_to_ws
, 0, {.id
= 12} },
7421 { "mvws_14", send_to_ws
, 0, {.id
= 13} },
7422 { "mvws_15", send_to_ws
, 0, {.id
= 14} },
7423 { "mvws_16", send_to_ws
, 0, {.id
= 15} },
7424 { "mvws_17", send_to_ws
, 0, {.id
= 16} },
7425 { "mvws_18", send_to_ws
, 0, {.id
= 17} },
7426 { "mvws_19", send_to_ws
, 0, {.id
= 18} },
7427 { "mvws_20", send_to_ws
, 0, {.id
= 19} },
7428 { "mvws_21", send_to_ws
, 0, {.id
= 20} },
7429 { "mvws_22", send_to_ws
, 0, {.id
= 21} },
7430 { "name_workspace", name_workspace
, 0, {0} },
7431 { "quit", quit
, 0, {0} },
7432 { "raise", raise_focus
, 0, {0} },
7433 { "raise_toggle", raise_toggle
, 0, {0} },
7434 { "resize", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_DONTCENTER
} },
7435 { "resize_centered", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_CENTER
} },
7436 { "restart", restart
, 0, {0} },
7437 { "rg_1", focusrg
, 0, {.id
= 0} },
7438 { "rg_2", focusrg
, 0, {.id
= 1} },
7439 { "rg_3", focusrg
, 0, {.id
= 2} },
7440 { "rg_4", focusrg
, 0, {.id
= 3} },
7441 { "rg_5", focusrg
, 0, {.id
= 4} },
7442 { "rg_6", focusrg
, 0, {.id
= 5} },
7443 { "rg_7", focusrg
, 0, {.id
= 6} },
7444 { "rg_8", focusrg
, 0, {.id
= 7} },
7445 { "rg_9", focusrg
, 0, {.id
= 8} },
7446 { "rg_move_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_UP
} },
7447 { "rg_move_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_DOWN
} },
7448 { "rg_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7449 { "rg_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7450 { "screen_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7451 { "screen_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7452 { "search_win", search_win
, 0, {0} },
7453 { "search_workspace", search_workspace
, 0, {0} },
7454 { "spawn_custom", NULL
, 0, {0} },
7455 { "stack_balance", stack_config
, 0, {.id
= SWM_ARG_ID_STACKBALANCE
} },
7456 { "stack_inc", stack_config
, 0, {.id
= SWM_ARG_ID_STACKINC
} },
7457 { "stack_dec", stack_config
, 0, {.id
= SWM_ARG_ID_STACKDEC
} },
7458 { "stack_reset", stack_config
, 0, {.id
= SWM_ARG_ID_STACKRESET
} },
7459 { "swap_main", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPMAIN
} },
7460 { "swap_next", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPNEXT
} },
7461 { "swap_prev", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPPREV
} },
7462 { "uniconify", uniconify
, 0, {0} },
7463 { "version", version
, 0, {0} },
7464 { "width_grow", resize
, 0, {.id
= SWM_ARG_ID_WIDTHGROW
} },
7465 { "width_shrink", resize
, 0, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
7466 { "wind_del", wkill
, 0, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
7467 { "wind_kill", wkill
, 0, {.id
= SWM_ARG_ID_KILLWINDOW
} },
7468 { "ws_1", switchws
, 0, {.id
= 0} },
7469 { "ws_2", switchws
, 0, {.id
= 1} },
7470 { "ws_3", switchws
, 0, {.id
= 2} },
7471 { "ws_4", switchws
, 0, {.id
= 3} },
7472 { "ws_5", switchws
, 0, {.id
= 4} },
7473 { "ws_6", switchws
, 0, {.id
= 5} },
7474 { "ws_7", switchws
, 0, {.id
= 6} },
7475 { "ws_8", switchws
, 0, {.id
= 7} },
7476 { "ws_9", switchws
, 0, {.id
= 8} },
7477 { "ws_10", switchws
, 0, {.id
= 9} },
7478 { "ws_11", switchws
, 0, {.id
= 10} },
7479 { "ws_12", switchws
, 0, {.id
= 11} },
7480 { "ws_13", switchws
, 0, {.id
= 12} },
7481 { "ws_14", switchws
, 0, {.id
= 13} },
7482 { "ws_15", switchws
, 0, {.id
= 14} },
7483 { "ws_16", switchws
, 0, {.id
= 15} },
7484 { "ws_17", switchws
, 0, {.id
= 16} },
7485 { "ws_18", switchws
, 0, {.id
= 17} },
7486 { "ws_19", switchws
, 0, {.id
= 18} },
7487 { "ws_20", switchws
, 0, {.id
= 19} },
7488 { "ws_21", switchws
, 0, {.id
= 20} },
7489 { "ws_22", switchws
, 0, {.id
= 21} },
7490 { "ws_next", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
7491 { "ws_next_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
7492 { "ws_next_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
7493 { "ws_prev", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
7494 { "ws_prev_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
7495 { "ws_prev_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
7496 { "ws_prior", priorws
, 0, {0} },
7497 /* SWM_DEBUG actions MUST be here: */
7498 { "debug_toggle", debug_toggle
, 0, {0} },
7499 { "dumpwins", dumpwins
, 0, {0} },
7501 { "invalid action", NULL
, 0, {0} },
7505 update_modkey(uint16_t mod
)
7509 /* Replace all instances of the old mod key. */
7510 RB_FOREACH(bp
, binding_tree
, &bindings
)
7511 if (bp
->mod
& mod_key
)
7512 bp
->mod
= (bp
->mod
& ~mod_key
) | mod
;
7517 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7520 struct spawn_prog
*prog
= NULL
;
7522 char *ap
, **real_args
;
7524 /* suppress unused warning since var is needed */
7527 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
7530 TAILQ_FOREACH(prog
, &spawns
, entry
) {
7531 if (strcasecmp(spawn_name
, prog
->name
) == 0)
7535 warnx("spawn_custom: program %s not found", spawn_name
);
7539 /* make room for expanded args */
7540 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
7541 err(1, "spawn_custom: calloc real_args");
7543 /* expand spawn_args into real_args */
7544 for (i
= c
= 0; i
< prog
->argc
; i
++) {
7546 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
7547 if (strcasecmp(ap
, "$bar_border") == 0) {
7549 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
7551 err(1, "spawn_custom border color");
7552 } else if (strcasecmp(ap
, "$bar_color") == 0) {
7554 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
7556 err(1, "spawn_custom bar color");
7557 } else if (strcasecmp(ap
, "$bar_font") == 0) {
7558 if ((real_args
[c
] = strdup(bar_fonts
))
7560 err(1, "spawn_custom bar fonts");
7561 } else if (strcasecmp(ap
, "$bar_font_color") == 0) {
7563 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
7565 err(1, "spawn_custom color font");
7566 } else if (strcasecmp(ap
, "$color_focus") == 0) {
7568 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
7570 err(1, "spawn_custom color focus");
7571 } else if (strcasecmp(ap
, "$color_focus_maximized") == 0) {
7573 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].name
))
7575 err(1, "spawn_custom color focus maximized");
7576 } else if (strcasecmp(ap
, "$color_unfocus") == 0) {
7578 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
7580 err(1, "spawn_custom color unfocus");
7581 } else if (strcasecmp(ap
, "$color_unfocus_maximized") == 0) {
7583 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].name
))
7585 err(1, "spawn_custom color unfocus maximized");
7586 } else if (strcasecmp(ap
, "$region_index") == 0) {
7587 if (asprintf(&real_args
[c
], "%d",
7588 get_region_index(r
) + 1) < 1)
7589 err(1, "spawn_custom region index");
7590 } else if (strcasecmp(ap
, "$workspace_index") == 0) {
7591 if (asprintf(&real_args
[c
], "%d", r
->ws
->idx
+ 1) < 1)
7592 err(1, "spawn_custom workspace index");
7593 } else if (strcasecmp(ap
, "$dmenu_bottom") == 0) {
7596 if ((real_args
[c
] = strdup("-b")) == NULL
)
7597 err(1, "spawn_custom workspace index");
7599 /* no match --> copy as is */
7600 if ((real_args
[c
] = strdup(ap
)) == NULL
)
7601 err(1, "spawn_custom strdup(ap)");
7603 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
7609 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
7610 for (i
= 0; i
< c
; ++i
)
7611 DPRINTF("\"%s\" ", real_args
[i
]);
7614 *ret_args
= real_args
;
7619 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
7625 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7629 spawn(r
->ws
->idx
, &a
, true);
7631 for (i
= 0; i
< spawn_argc
; i
++)
7637 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7644 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7648 if (pipe(select_list_pipe
) == -1)
7649 err(1, "pipe error");
7650 if (pipe(select_resp_pipe
) == -1)
7651 err(1, "pipe error");
7653 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
7654 err(1, "could not disable SIGPIPE");
7655 switch (*pid
= fork()) {
7657 err(1, "cannot fork");
7660 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
7662 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
7664 close(select_list_pipe
[1]);
7665 close(select_resp_pipe
[0]);
7666 spawn(r
->ws
->idx
, &a
, false);
7668 default: /* parent */
7669 close(select_list_pipe
[0]);
7670 close(select_resp_pipe
[1]);
7674 for (i
= 0; i
< spawn_argc
; i
++)
7679 /* Argument tokenizer. */
7682 char *arg
, *cp
, *next
;
7683 bool single_quoted
= false, double_quoted
= false;
7688 /* Eat and move characters until end of argument is found. */
7689 for (arg
= next
= cp
= *sp
; *cp
!= '\0'; ++cp
) {
7690 if (!double_quoted
&& *cp
== '\'') {
7691 /* Eat single-quote. */
7692 single_quoted
= !single_quoted
;
7693 } else if (!single_quoted
&& *cp
== '"') {
7694 /* Eat double-quote. */
7695 double_quoted
= !double_quoted
;
7696 } else if (!single_quoted
&& *cp
== '\\' && *(cp
+ 1) == '"') {
7697 /* Eat backslash; copy escaped character to arg. */
7699 } else if (!single_quoted
&& !double_quoted
&& *cp
== '\\' &&
7700 (*(cp
+ 1) == '\'' || *(cp
+ 1) == ' ')) {
7701 /* Eat backslash; move escaped character. */
7703 } else if (!single_quoted
&& !double_quoted
&&
7704 (*cp
== ' ' || *cp
== '\t')) {
7705 /* Terminate argument. */
7707 /* Point sp to beginning of next argument. */
7711 /* Move regular character. */
7716 /* Terminate argument if end of string. */
7726 spawn_insert(const char *name
, const char *args
, int flags
)
7728 struct spawn_prog
*sp
;
7729 char *arg
, *cp
, *ptr
;
7731 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s[%s]\n", name
, args
);
7733 if (args
== NULL
|| *args
== '\0')
7736 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
7737 err(1, "spawn_insert: calloc");
7738 if ((sp
->name
= strdup(name
)) == NULL
)
7739 err(1, "spawn_insert: strdup");
7741 /* Convert the arguments to an argument list. */
7742 if ((ptr
= cp
= strdup(args
)) == NULL
)
7743 err(1, "spawn_insert: strdup");
7744 while ((arg
= argsep(&ptr
)) != NULL
) {
7745 /* Null argument; skip it. */
7750 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
7751 sizeof *sp
->argv
)) == NULL
)
7752 err(1, "spawn_insert: realloc");
7753 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
7754 err(1, "spawn_insert: strdup");
7760 DNPRINTF(SWM_D_SPAWN
, "arg %d: [%s]\n", sp
->argc
, sp
->argv
[sp
->argc
-1]);
7761 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
7762 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
7766 spawn_remove(struct spawn_prog
*sp
)
7770 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
7772 TAILQ_REMOVE(&spawns
, sp
, entry
);
7773 for (i
= 0; i
< sp
->argc
; i
++)
7779 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
7785 struct spawn_prog
*sp
;
7787 while ((sp
= TAILQ_FIRST(&spawns
)) != NULL
) {
7793 spawn_find(const char *name
)
7795 struct spawn_prog
*sp
;
7797 TAILQ_FOREACH(sp
, &spawns
, entry
)
7798 if (strcasecmp(sp
->name
, name
) == 0)
7805 setspawn(const char *name
, const char *args
, int flags
)
7807 struct spawn_prog
*sp
;
7809 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
7814 /* Remove any old spawn under the same name. */
7815 if ((sp
= spawn_find(name
)) != NULL
)
7819 spawn_insert(name
, args
, flags
);
7821 warnx("error: setspawn: cannot find program: %s", name
);
7823 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
7827 setconfspawn(const char *selector
, const char *value
, int flags
)
7831 if (selector
== NULL
|| strlen(selector
) == 0)
7834 args
= expand_tilde(value
);
7836 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
7838 setspawn(selector
, args
, flags
);
7841 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
7846 validate_spawns(void)
7849 struct spawn_prog
*sp
;
7850 char which
[PATH_MAX
];
7853 RB_FOREACH(bp
, binding_tree
, &bindings
) {
7854 if (bp
->action
!= FN_SPAWN_CUSTOM
)
7858 sp
= spawn_find(bp
->spawn_name
);
7859 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
7862 /* verify we have the goods */
7863 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
7864 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
7866 for (i
= strlen("which "); i
< strlen(which
); i
++)
7867 if (which
[i
] == ' ') {
7871 if (system(which
) != 0)
7872 add_startup_exception("could not find %s",
7873 &which
[strlen("which ")]);
7880 setconfspawn("lock", "xlock", 0);
7882 setconfspawn("term", "xterm", 0);
7883 setconfspawn("spawn_term", "xterm", 0);
7885 setconfspawn("menu", "dmenu_run"
7889 " -nf $bar_font_color"
7891 " -sf $bar_color", 0);
7893 setconfspawn("search", "dmenu"
7898 " -nf $bar_font_color"
7900 " -sf $bar_color", 0);
7902 setconfspawn("name_workspace", "dmenu"
7907 " -nf $bar_font_color"
7909 " -sf $bar_color", 0);
7911 /* These are not verified for existence, even with a binding set. */
7912 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
7913 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
7914 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
7918 #define SWM_MODNAME_SIZE 32
7919 #define SWM_KEY_WS "\n+ \t"
7921 parsebinding(const char *bindstr
, uint16_t *mod
, enum binding_type
*type
,
7922 uint32_t *val
, uint32_t *flags
)
7924 char *str
, *cp
, *name
;
7925 KeySym ks
, lks
, uks
;
7927 DNPRINTF(SWM_D_KEY
, "parsebinding: enter [%s]\n", bindstr
);
7928 if (mod
== NULL
|| val
== NULL
) {
7929 DNPRINTF(SWM_D_KEY
, "parsebinding: no mod or key vars\n");
7932 if (bindstr
== NULL
|| strlen(bindstr
) == 0) {
7933 DNPRINTF(SWM_D_KEY
, "parsebinding: no bindstr\n");
7937 if ((cp
= str
= strdup(bindstr
)) == NULL
)
7938 err(1, "parsebinding: strdup");
7940 *val
= XCB_NO_SYMBOL
;
7944 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
7945 DNPRINTF(SWM_D_KEY
, "parsebinding: entry [%s]\n", name
);
7947 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
7948 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
7950 else if (strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
) == 0)
7951 *mod
|= XCB_MOD_MASK_1
;
7952 else if (strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
) == 0)
7953 *mod
|= XCB_MOD_MASK_2
;
7954 else if (strncmp(name
, "Mod3", SWM_MODNAME_SIZE
) == 0)
7955 *mod
|= XCB_MOD_MASK_3
;
7956 else if (strncmp(name
, "Mod4", SWM_MODNAME_SIZE
) == 0)
7957 *mod
|= XCB_MOD_MASK_4
;
7958 else if (strncmp(name
, "Mod5", SWM_MODNAME_SIZE
) == 0)
7959 *mod
|= XCB_MOD_MASK_5
;
7960 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
7961 *mod
|= XCB_MOD_MASK_SHIFT
;
7962 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
7963 *mod
|= XCB_MOD_MASK_CONTROL
;
7964 else if (strncasecmp(name
, "ANYMOD", SWM_MODNAME_SIZE
) == 0)
7965 *mod
|= XCB_MOD_MASK_ANY
;
7966 else if (strncasecmp(name
, "REPLAY", SWM_MODNAME_SIZE
) == 0)
7967 *flags
|= BINDING_F_REPLAY
;
7968 else if (sscanf(name
, "Button%u", val
) == 1) {
7969 DNPRINTF(SWM_D_KEY
, "parsebinding: button %u\n", *val
);
7971 if (*val
> 255 || *val
== 0) {
7973 "parsebinding: invalid button %u\n", *val
);
7977 /* TODO: do this without Xlib. */
7978 ks
= XStringToKeysym(name
);
7979 if (ks
== NoSymbol
) {
7981 "parsebinding: invalid key %s\n", name
);
7986 XConvertCase(ks
, &lks
, &uks
);
7987 *val
= (uint32_t)lks
;
7991 /* If ANYMOD was specified, ignore the rest. */
7992 if (*mod
& XCB_MOD_MASK_ANY
)
7993 *mod
= XCB_MOD_MASK_ANY
;
7996 DNPRINTF(SWM_D_KEY
, "parsebinding: leave ok\n");
8001 strdupsafe(const char *str
)
8006 return (strdup(str
));
8010 binding_cmp(struct binding
*bp1
, struct binding
*bp2
)
8012 if (bp1
->type
< bp2
->type
)
8014 if (bp1
->type
> bp2
->type
)
8017 if (bp1
->value
< bp2
->value
)
8019 if (bp1
->value
> bp2
->value
)
8022 if (bp1
->mod
< bp2
->mod
)
8024 if (bp1
->mod
> bp2
->mod
)
8031 binding_insert(uint16_t mod
, enum binding_type type
, uint32_t val
,
8032 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
8036 DNPRINTF(SWM_D_KEY
, "binding_insert: mod: %u, type: %d, val: %u, "
8037 "action: %s(%d), spawn_name: %s\n", mod
, type
, val
,
8038 actions
[aid
].name
, aid
, spawn_name
);
8040 if ((bp
= malloc(sizeof *bp
)) == NULL
)
8041 err(1, "binding_insert: malloc");
8048 bp
->spawn_name
= strdupsafe(spawn_name
);
8049 RB_INSERT(binding_tree
, &bindings
, bp
);
8051 DNPRINTF(SWM_D_KEY
, "binding_insert: leave\n");
8055 binding_lookup(uint16_t mod
, enum binding_type type
, uint32_t val
)
8063 return (RB_FIND(binding_tree
, &bindings
, &bp
));
8067 binding_remove(struct binding
*bp
)
8069 DNPRINTF(SWM_D_KEY
, "binding_remove: mod: %u, type: %d, val: %u, "
8070 "action: %s(%d), spawn_name: %s\n", bp
->mod
, bp
->type
, bp
->value
,
8071 actions
[bp
->action
].name
, bp
->action
, bp
->spawn_name
);
8073 RB_REMOVE(binding_tree
, &bindings
, bp
);
8074 free(bp
->spawn_name
);
8077 DNPRINTF(SWM_D_KEY
, "binding_remove: leave\n");
8081 setbinding(uint16_t mod
, enum binding_type type
, uint32_t val
,
8082 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
8086 DNPRINTF(SWM_D_KEY
, "setbinding: enter %s [%s]\n",
8087 actions
[aid
].name
, spawn_name
);
8089 /* Unbind any existing. Loop is to handle MOD_MASK_ANY. */
8090 while ((bp
= binding_lookup(mod
, type
, val
)))
8093 if (aid
!= FN_INVALID
)
8094 binding_insert(mod
, type
, val
, aid
, flags
, spawn_name
);
8096 DNPRINTF(SWM_D_KEY
, "setbinding: leave\n");
8100 setconfbinding(const char *selector
, const char *value
, int flags
)
8102 struct spawn_prog
*sp
;
8103 uint32_t keybtn
, opts
;
8106 enum binding_type type
;
8108 /* suppress unused warning since var is needed */
8111 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter selector: [%s], "
8112 "value: [%s]\n", selector
, value
);
8113 if (selector
== NULL
|| strlen(selector
) == 0) {
8114 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
8115 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8116 setbinding(mod
, type
, keybtn
, FN_INVALID
, opts
, NULL
);
8121 /* search by key function name */
8122 for (aid
= 0; aid
< FN_INVALID
; aid
++) {
8123 if (strncasecmp(selector
, actions
[aid
].name
,
8124 SWM_FUNCNAME_LEN
) == 0) {
8125 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8126 "action\n", selector
);
8127 if (parsebinding(value
, &mod
, &type
, &keybtn
,
8129 setbinding(mod
, type
, keybtn
, aid
, opts
, NULL
);
8135 /* search by custom spawn name */
8136 if ((sp
= spawn_find(selector
)) != NULL
) {
8137 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8138 "spawn\n", selector
);
8139 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8140 setbinding(mod
, type
, keybtn
, FN_SPAWN_CUSTOM
, opts
,
8146 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
8150 #define MODSHIFT MODKEY | XCB_MOD_MASK_SHIFT
8152 setup_keybindings(void)
8154 #define BINDKEY(m, k, a) setbinding(m, KEYBIND, k, a, 0, NULL)
8155 #define BINDKEYSPAWN(m, k, s) setbinding(m, KEYBIND, k, FN_SPAWN_CUSTOM, 0, s)
8156 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8157 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8158 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8159 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8160 BINDKEY(MODKEY
, XK_v
, FN_BUTTON2
);
8161 BINDKEY(MODKEY
, XK_space
, FN_CYCLE_LAYOUT
);
8162 BINDKEY(MODSHIFT
, XK_backslash
, FN_FLIP_LAYOUT
);
8163 BINDKEY(MODKEY
, XK_t
, FN_FLOAT_TOGGLE
);
8164 BINDKEY(MODKEY
, XK_m
, FN_FOCUS_MAIN
);
8165 BINDKEY(MODKEY
, XK_j
, FN_FOCUS_NEXT
);
8166 BINDKEY(MODKEY
, XK_Tab
, FN_FOCUS_NEXT
);
8167 BINDKEY(MODKEY
, XK_k
, FN_FOCUS_PREV
);
8168 BINDKEY(MODSHIFT
, XK_Tab
, FN_FOCUS_PREV
);
8169 BINDKEY(MODKEY
, XK_u
, FN_FOCUS_URGENT
);
8170 BINDKEY(MODSHIFT
, XK_e
, FN_FULLSCREEN_TOGGLE
);
8171 BINDKEY(MODKEY
, XK_e
, FN_MAXIMIZE_TOGGLE
);
8172 BINDKEY(MODSHIFT
, XK_equal
, FN_HEIGHT_GROW
);
8173 BINDKEY(MODSHIFT
, XK_minus
, FN_HEIGHT_SHRINK
);
8174 BINDKEY(MODKEY
, XK_w
, FN_ICONIFY
);
8175 BINDKEY(MODKEY
, XK_h
, FN_MASTER_SHRINK
);
8176 BINDKEY(MODKEY
, XK_l
, FN_MASTER_GROW
);
8177 BINDKEY(MODKEY
, XK_comma
, FN_MASTER_ADD
);
8178 BINDKEY(MODKEY
, XK_period
, FN_MASTER_DEL
);
8179 BINDKEY(MODSHIFT
, XK_bracketright
, FN_MOVE_DOWN
);
8180 BINDKEY(MODKEY
, XK_bracketleft
, FN_MOVE_LEFT
);
8181 BINDKEY(MODKEY
, XK_bracketright
, FN_MOVE_RIGHT
);
8182 BINDKEY(MODSHIFT
, XK_bracketleft
, FN_MOVE_UP
);
8183 BINDKEY(MODSHIFT
, XK_KP_End
, FN_MVRG_1
);
8184 BINDKEY(MODSHIFT
, XK_KP_Down
, FN_MVRG_2
);
8185 BINDKEY(MODSHIFT
, XK_KP_Next
, FN_MVRG_3
);
8186 BINDKEY(MODSHIFT
, XK_KP_Left
, FN_MVRG_4
);
8187 BINDKEY(MODSHIFT
, XK_KP_Begin
, FN_MVRG_5
);
8188 BINDKEY(MODSHIFT
, XK_KP_Right
, FN_MVRG_6
);
8189 BINDKEY(MODSHIFT
, XK_KP_Home
, FN_MVRG_7
);
8190 BINDKEY(MODSHIFT
, XK_KP_Up
, FN_MVRG_8
);
8191 BINDKEY(MODSHIFT
, XK_KP_Prior
, FN_MVRG_9
);
8192 BINDKEY(MODSHIFT
, XK_1
, FN_MVWS_1
);
8193 BINDKEY(MODSHIFT
, XK_2
, FN_MVWS_2
);
8194 BINDKEY(MODSHIFT
, XK_3
, FN_MVWS_3
);
8195 BINDKEY(MODSHIFT
, XK_4
, FN_MVWS_4
);
8196 BINDKEY(MODSHIFT
, XK_5
, FN_MVWS_5
);
8197 BINDKEY(MODSHIFT
, XK_6
, FN_MVWS_6
);
8198 BINDKEY(MODSHIFT
, XK_7
, FN_MVWS_7
);
8199 BINDKEY(MODSHIFT
, XK_8
, FN_MVWS_8
);
8200 BINDKEY(MODSHIFT
, XK_9
, FN_MVWS_9
);
8201 BINDKEY(MODSHIFT
, XK_0
, FN_MVWS_10
);
8202 BINDKEY(MODSHIFT
, XK_F1
, FN_MVWS_11
);
8203 BINDKEY(MODSHIFT
, XK_F2
, FN_MVWS_12
);
8204 BINDKEY(MODSHIFT
, XK_F3
, FN_MVWS_13
);
8205 BINDKEY(MODSHIFT
, XK_F4
, FN_MVWS_14
);
8206 BINDKEY(MODSHIFT
, XK_F5
, FN_MVWS_15
);
8207 BINDKEY(MODSHIFT
, XK_F6
, FN_MVWS_16
);
8208 BINDKEY(MODSHIFT
, XK_F7
, FN_MVWS_17
);
8209 BINDKEY(MODSHIFT
, XK_F8
, FN_MVWS_18
);
8210 BINDKEY(MODSHIFT
, XK_F9
, FN_MVWS_19
);
8211 BINDKEY(MODSHIFT
, XK_F10
, FN_MVWS_20
);
8212 BINDKEY(MODSHIFT
, XK_F11
, FN_MVWS_21
);
8213 BINDKEY(MODSHIFT
, XK_F12
, FN_MVWS_22
);
8214 BINDKEY(MODSHIFT
, XK_slash
, FN_NAME_WORKSPACE
);
8215 BINDKEY(MODSHIFT
, XK_q
, FN_QUIT
);
8216 BINDKEY(MODKEY
, XK_r
, FN_RAISE
);
8217 BINDKEY(MODSHIFT
, XK_r
, FN_RAISE_TOGGLE
);
8218 BINDKEY(MODKEY
, XK_q
, FN_RESTART
);
8219 BINDKEY(MODKEY
, XK_KP_End
, FN_RG_1
);
8220 BINDKEY(MODKEY
, XK_KP_Down
, FN_RG_2
);
8221 BINDKEY(MODKEY
, XK_KP_Next
, FN_RG_3
);
8222 BINDKEY(MODKEY
, XK_KP_Left
, FN_RG_4
);
8223 BINDKEY(MODKEY
, XK_KP_Begin
, FN_RG_5
);
8224 BINDKEY(MODKEY
, XK_KP_Right
, FN_RG_6
);
8225 BINDKEY(MODKEY
, XK_KP_Home
, FN_RG_7
);
8226 BINDKEY(MODKEY
, XK_KP_Up
, FN_RG_8
);
8227 BINDKEY(MODKEY
, XK_KP_Prior
, FN_RG_9
);
8228 BINDKEY(MODSHIFT
, XK_Right
, FN_RG_NEXT
);
8229 BINDKEY(MODSHIFT
, XK_Left
, FN_RG_PREV
);
8230 BINDKEY(MODKEY
, XK_f
, FN_SEARCH_WIN
);
8231 BINDKEY(MODKEY
, XK_slash
, FN_SEARCH_WORKSPACE
);
8232 BINDKEYSPAWN(MODSHIFT
, XK_i
, "initscr");
8233 BINDKEYSPAWN(MODSHIFT
, XK_Delete
, "lock");
8234 BINDKEYSPAWN(MODKEY
, XK_p
, "menu");
8235 BINDKEYSPAWN(MODKEY
, XK_s
, "screenshot_all");
8236 BINDKEYSPAWN(MODSHIFT
, XK_s
, "screenshot_wind");
8237 BINDKEYSPAWN(MODSHIFT
, XK_Return
, "term");
8238 BINDKEY(MODSHIFT
, XK_comma
, FN_STACK_INC
);
8239 BINDKEY(MODSHIFT
, XK_period
, FN_STACK_DEC
);
8240 BINDKEY(MODSHIFT
, XK_space
, FN_STACK_RESET
);
8241 BINDKEY(MODKEY
, XK_Return
, FN_SWAP_MAIN
);
8242 BINDKEY(MODSHIFT
, XK_j
, FN_SWAP_NEXT
);
8243 BINDKEY(MODSHIFT
, XK_k
, FN_SWAP_PREV
);
8244 BINDKEY(MODSHIFT
, XK_w
, FN_UNICONIFY
);
8245 BINDKEY(MODSHIFT
, XK_v
, FN_VERSION
);
8246 BINDKEY(MODKEY
, XK_equal
, FN_WIDTH_GROW
);
8247 BINDKEY(MODKEY
, XK_minus
, FN_WIDTH_SHRINK
);
8248 BINDKEY(MODKEY
, XK_x
, FN_WIND_DEL
);
8249 BINDKEY(MODSHIFT
, XK_x
, FN_WIND_KILL
);
8250 BINDKEY(MODKEY
, XK_1
, FN_WS_1
);
8251 BINDKEY(MODKEY
, XK_2
, FN_WS_2
);
8252 BINDKEY(MODKEY
, XK_3
, FN_WS_3
);
8253 BINDKEY(MODKEY
, XK_4
, FN_WS_4
);
8254 BINDKEY(MODKEY
, XK_5
, FN_WS_5
);
8255 BINDKEY(MODKEY
, XK_6
, FN_WS_6
);
8256 BINDKEY(MODKEY
, XK_7
, FN_WS_7
);
8257 BINDKEY(MODKEY
, XK_8
, FN_WS_8
);
8258 BINDKEY(MODKEY
, XK_9
, FN_WS_9
);
8259 BINDKEY(MODKEY
, XK_0
, FN_WS_10
);
8260 BINDKEY(MODKEY
, XK_F1
, FN_WS_11
);
8261 BINDKEY(MODKEY
, XK_F2
, FN_WS_12
);
8262 BINDKEY(MODKEY
, XK_F3
, FN_WS_13
);
8263 BINDKEY(MODKEY
, XK_F4
, FN_WS_14
);
8264 BINDKEY(MODKEY
, XK_F5
, FN_WS_15
);
8265 BINDKEY(MODKEY
, XK_F6
, FN_WS_16
);
8266 BINDKEY(MODKEY
, XK_F7
, FN_WS_17
);
8267 BINDKEY(MODKEY
, XK_F8
, FN_WS_18
);
8268 BINDKEY(MODKEY
, XK_F9
, FN_WS_19
);
8269 BINDKEY(MODKEY
, XK_F10
, FN_WS_20
);
8270 BINDKEY(MODKEY
, XK_F11
, FN_WS_21
);
8271 BINDKEY(MODKEY
, XK_F12
, FN_WS_22
);
8272 BINDKEY(MODKEY
, XK_Right
, FN_WS_NEXT
);
8273 BINDKEY(MODKEY
, XK_Left
, FN_WS_PREV
);
8274 BINDKEY(MODKEY
, XK_Up
, FN_WS_NEXT_ALL
);
8275 BINDKEY(MODKEY
, XK_Down
, FN_WS_PREV_ALL
);
8276 BINDKEY(MODSHIFT
, XK_Up
, FN_WS_NEXT_MOVE
);
8277 BINDKEY(MODSHIFT
, XK_Down
, FN_WS_PREV_MOVE
);
8278 BINDKEY(MODKEY
, XK_a
, FN_WS_PRIOR
);
8280 BINDKEY(MODKEY
, XK_d
, FN_DEBUG_TOGGLE
);
8281 BINDKEY(MODSHIFT
, XK_d
, FN_DUMPWINS
);
8288 setup_btnbindings(void)
8290 setbinding(ANYMOD
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_FOCUS
,
8291 BINDING_F_REPLAY
, NULL
);
8292 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE
, 0, NULL
);
8293 setbinding(MODSHIFT
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE_CENTERED
, 0,
8295 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_MOVE
, 0, NULL
);
8300 clear_bindings(void)
8304 while ((bp
= RB_ROOT(&bindings
)))
8309 clear_keybindings(void)
8311 struct binding
*bp
, *bptmp
;
8313 RB_FOREACH_SAFE(bp
, binding_tree
, &bindings
, bptmp
) {
8314 if (bp
->type
!= KEYBIND
)
8321 setkeymapping(const char *selector
, const char *value
, int flags
)
8323 char *keymapping_file
;
8325 /* suppress unused warnings since vars are needed */
8329 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
8331 keymapping_file
= expand_tilde(value
);
8333 clear_keybindings();
8334 /* load new key bindings; if it fails, revert to default bindings */
8335 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
8336 clear_keybindings();
8337 setup_keybindings();
8340 free(keymapping_file
);
8342 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
8347 updatenumlockmask(void)
8350 xcb_get_modifier_mapping_reply_t
*modmap_r
;
8351 xcb_keycode_t
*modmap
, kc
, *keycode
;
8355 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
8356 xcb_get_modifier_mapping(conn
),
8359 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
8360 for (i
= 0; i
< 8; i
++) {
8361 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
8362 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
8364 keycode
= xcb_key_symbols_get_keycode(syms
,
8368 numlockmask
= (1 << i
);
8375 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
8382 int num_screens
, k
, j
;
8383 uint16_t modifiers
[4];
8384 xcb_keycode_t
*code
;
8386 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
8387 updatenumlockmask();
8390 modifiers
[1] = numlockmask
;
8391 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8392 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8394 num_screens
= get_screen_count();
8395 for (k
= 0; k
< num_screens
; k
++) {
8396 if (TAILQ_EMPTY(&screens
[k
].rl
))
8398 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
8400 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8401 if (bp
->type
!= KEYBIND
)
8404 /* If there is a catch-all, only bind that. */
8405 if ((binding_lookup(ANYMOD
, KEYBIND
, bp
->value
)) &&
8409 /* Skip unused ws binds. */
8410 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8411 bp
->action
<= FN_WS_22
)
8414 /* Skip unused mvws binds. */
8415 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8416 bp
->action
<= FN_MVWS_22
)
8419 if ((code
= xcb_key_symbols_get_keycode(syms
,
8420 bp
->value
)) == NULL
)
8423 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8424 /* All modifiers are grabbed in one pass. */
8425 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8426 "key: %u, modifiers: %d\n", bp
->value
,
8428 xcb_grab_key(conn
, 1, screens
[k
].root
,
8429 bp
->mod
, *code
, XCB_GRAB_MODE_ASYNC
,
8430 XCB_GRAB_MODE_SYNC
);
8432 /* Need to grab each modifier permutation. */
8433 for (j
= 0; j
< LENGTH(modifiers
); j
++) {
8434 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8435 "key: %u, modifiers: %d\n",
8436 bp
->value
, bp
->mod
| modifiers
[j
]);
8437 xcb_grab_key(conn
, 1,
8439 bp
->mod
| modifiers
[j
],
8440 *code
, XCB_GRAB_MODE_ASYNC
,
8441 XCB_GRAB_MODE_SYNC
);
8453 int num_screens
, i
, k
;
8454 uint16_t modifiers
[4];
8456 DNPRINTF(SWM_D_MOUSE
, "grabbuttons\n");
8457 updatenumlockmask();
8460 modifiers
[1] = numlockmask
;
8461 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8462 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8464 num_screens
= get_screen_count();
8465 for (k
= 0; k
< num_screens
; k
++) {
8466 if (TAILQ_EMPTY(&screens
[k
].rl
))
8468 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, screens
[k
].root
,
8470 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8471 if (bp
->type
!= BTNBIND
)
8474 /* If there is a catch-all, only bind that. */
8475 if ((binding_lookup(ANYMOD
, BTNBIND
, bp
->value
)) &&
8479 /* Skip unused ws binds. */
8480 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8481 bp
->action
<= FN_WS_22
)
8484 /* Skip unused mvws binds. */
8485 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8486 bp
->action
<= FN_MVWS_22
)
8489 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8490 /* All modifiers are grabbed in one pass. */
8491 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: grab, "
8492 "button: %u, modifiers: %d\n", bp
->value
,
8494 xcb_grab_button(conn
, 0, screens
[k
].root
,
8495 BUTTONMASK
, XCB_GRAB_MODE_SYNC
,
8496 XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
8497 XCB_CURSOR_NONE
, bp
->value
, bp
->mod
);
8499 /* Need to grab each modifier permutation. */
8500 for (i
= 0; i
< LENGTH(modifiers
); ++i
) {
8501 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: "
8502 "grab, button: %u, modifiers: %u\n",
8503 bp
->value
, bp
->mod
| modifiers
[i
]);
8504 xcb_grab_button(conn
, 0,
8505 screens
[k
].root
, BUTTONMASK
,
8507 XCB_GRAB_MODE_ASYNC
,
8509 XCB_CURSOR_NONE
, bp
->value
,
8510 bp
->mod
| modifiers
[i
]);
8517 const char *quirkname
[] = {
8518 "NONE", /* config string for "no value" */
8526 "FOCUSONMAP_SINGLE",
8534 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
8535 #define SWM_Q_DELIM "\n|+ \t"
8537 parsequirks(const char *qstr
, uint32_t *quirk
, int *ws
)
8539 char *str
, *cp
, *name
;
8542 if (quirk
== NULL
|| qstr
== NULL
)
8545 if ((cp
= str
= strdup(qstr
)) == NULL
)
8546 err(1, "parsequirks: strdup");
8549 while ((name
= strsep(&cp
, SWM_Q_DELIM
)) != NULL
) {
8551 cp
+= (long)strspn(cp
, SWM_Q_DELIM
);
8553 if (sscanf(name
, "WS[%d]", ws
) == 1) {
8559 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
8560 if (strncasecmp(name
, quirkname
[i
],
8561 SWM_QUIRK_LEN
) == 0) {
8562 DNPRINTF(SWM_D_QUIRK
,
8563 "parsequirks: %s\n", name
);
8569 *quirk
|= 1 << (i
-1);
8573 if (i
>= LENGTH(quirkname
)) {
8574 DNPRINTF(SWM_D_QUIRK
,
8575 "parsequirks: invalid quirk [%s]\n", name
);
8586 quirk_insert(const char *class, const char *instance
, const char *name
,
8587 uint32_t quirk
, int ws
)
8591 bool failed
= false;
8593 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: class: %s, instance: %s, name: %s,"
8594 " value: %u, ws: %d\n", class, instance
, name
, quirk
, ws
);
8596 if ((qp
= malloc(sizeof *qp
)) == NULL
)
8597 err(1, "quirk_insert: malloc");
8599 if ((qp
->class = strdup(class)) == NULL
)
8600 err(1, "quirk_insert: strdup");
8601 if ((qp
->instance
= strdup(instance
)) == NULL
)
8602 err(1, "quirk_insert: strdup");
8603 if ((qp
->name
= strdup(name
)) == NULL
)
8604 err(1, "quirk_insert: strdup");
8606 if (asprintf(&str
, "^%s$", class) == -1)
8607 err(1, "quirk_insert: asprintf");
8608 if (regcomp(&qp
->regex_class
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8609 add_startup_exception("regex failed to compile quirk 'class' "
8610 "field: %s", class);
8613 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8616 if (asprintf(&str
, "^%s$", instance
) == -1)
8617 err(1, "quirk_insert: asprintf");
8618 if (regcomp(&qp
->regex_instance
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8619 add_startup_exception("regex failed to compile quirk 'instance'"
8620 " field: %s", instance
);
8623 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8626 if (asprintf(&str
, "^%s$", name
) == -1)
8627 err(1, "quirk_insert: asprintf");
8628 if (regcomp(&qp
->regex_name
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8629 add_startup_exception("regex failed to compile quirk 'name' "
8633 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8637 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: regex error; skipping.\n");
8642 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
8644 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
8648 quirk_remove(struct quirk
*qp
)
8650 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%u]\n", qp
->class,
8651 qp
->name
, qp
->quirk
);
8653 TAILQ_REMOVE(&quirks
, qp
, entry
);
8656 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
8660 quirk_free(struct quirk
*qp
)
8662 regfree(&qp
->regex_class
);
8663 regfree(&qp
->regex_instance
);
8664 regfree(&qp
->regex_name
);
8676 while ((qp
= TAILQ_FIRST(&quirks
)) != NULL
) {
8682 quirk_replace(struct quirk
*qp
, const char *class, const char *instance
,
8683 const char *name
, uint32_t quirk
, int ws
)
8685 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp
->class,
8686 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
8689 quirk_insert(class, instance
, name
, quirk
, ws
);
8691 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
8695 setquirk(const char *class, const char *instance
, const char *name
,
8696 uint32_t quirk
, int ws
)
8700 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
8701 instance
, name
, quirk
, ws
);
8703 /* Remove/replace existing quirk. */
8704 TAILQ_FOREACH(qp
, &quirks
, entry
) {
8705 if (strcmp(qp
->class, class) == 0 &&
8706 strcmp(qp
->instance
, instance
) == 0 &&
8707 strcmp(qp
->name
, name
) == 0) {
8708 if (quirk
== 0 && ws
== -1)
8711 quirk_replace(qp
, class, instance
, name
, quirk
,
8713 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8718 /* Only insert if quirk is not NONE or forced ws is set. */
8719 if (quirk
|| ws
!= -1)
8720 quirk_insert(class, instance
, name
, quirk
, ws
);
8722 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8725 /* Eat '\' in str used to escape square brackets and colon. */
8727 unescape_selector(char *str
)
8731 for (cp
= str
; *str
!= '\0'; ++str
, ++cp
) {
8732 if (*str
== '\\' && (*(str
+ 1) == ':' || *(str
+ 1) == ']' ||
8742 setconfquirk(const char *selector
, const char *value
, int flags
)
8744 char *str
, *cp
, *class;
8745 char *instance
= NULL
, *name
= NULL
;
8746 int retval
, count
= 0, ws
= -1;
8749 /* suppress unused warning since var is needed */
8752 if (selector
== NULL
|| strlen(selector
) == 0)
8755 if ((str
= strdup(selector
)) == NULL
)
8756 err(1, "setconfquirk: strdup");
8758 /* Find non-escaped colon. */
8765 for (++cp
; *cp
!= '\0'; ++cp
) {
8766 if (*cp
== ':' && *(cp
- 1) != '\\') {
8772 unescape_selector(class);
8774 instance
= class + strlen(class) + 1;
8775 unescape_selector(instance
);
8781 name
= instance
+ strlen(instance
) + 1;
8782 unescape_selector(name
);
8787 DNPRINTF(SWM_D_CONF
, "setconfquirk: class: %s, instance: %s, "
8788 "name: %s\n", class, instance
, name
);
8790 if ((retval
= parsequirks(value
, &qrks
, &ws
)) == 0)
8791 setquirk(class, instance
, name
, qrks
, ws
);
8800 setquirk("MPlayer", "xv", ".*",
8801 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
, -1);
8802 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
8804 setquirk("Firefox-bin", "firefox-bin", ".*",
8806 setquirk("Firefox", "Dialog", ".*",
8808 setquirk("Gimp", "gimp", ".*",
8809 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8810 setquirk("XTerm", "xterm", ".*",
8811 SWM_Q_XTERM_FONTADJ
, -1);
8812 setquirk("xine", "Xine Window", ".*",
8813 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8814 setquirk("Xitk", "Xitk Combo", ".*",
8815 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8816 setquirk("xine", "xine Panel", ".*",
8817 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8818 setquirk("Xitk", "Xine Window", ".*",
8819 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8820 setquirk("xine", "xine Video Fullscreen Window", ".*",
8821 SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
, -1);
8822 setquirk("pcb", "pcb", ".*",
8824 setquirk("SDL_App", "SDL_App", ".*",
8825 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
, -1);
8828 /* conf file stuff */
8829 #define SWM_CONF_FILE "spectrwm.conf"
8830 #define SWM_CONF_FILE_OLD "scrotwm.conf"
8834 SWM_S_BAR_AT_BOTTOM
,
8835 SWM_S_BAR_BORDER_WIDTH
,
8838 SWM_S_BAR_ENABLED_WS
,
8843 SWM_S_BOUNDARY_WIDTH
,
8844 SWM_S_CLOCK_ENABLED
,
8847 SWM_S_CYCLE_VISIBLE
,
8849 SWM_S_DISABLE_BORDER
,
8851 SWM_S_FOCUS_CLOSE_WRAP
,
8852 SWM_S_FOCUS_DEFAULT
,
8854 SWM_S_ICONIC_ENABLED
,
8855 SWM_S_JAVA_WORKAROUND
,
8856 SWM_S_MAXIMIZE_HIDE_BAR
,
8857 SWM_S_REGION_PADDING
,
8862 SWM_S_STACK_ENABLED
,
8865 SWM_S_URGENT_COLLAPSE
,
8866 SWM_S_URGENT_ENABLED
,
8867 SWM_S_VERBOSE_LAYOUT
,
8870 SWM_S_WINDOW_CLASS_ENABLED
,
8871 SWM_S_WINDOW_INSTANCE_ENABLED
,
8872 SWM_S_WINDOW_NAME_ENABLED
,
8873 SWM_S_WORKSPACE_CLAMP
,
8874 SWM_S_WORKSPACE_LIMIT
,
8875 SWM_S_WORKSPACE_NAME
,
8879 setconfvalue(const char *selector
, const char *value
, int flags
)
8881 struct workspace
*ws
;
8882 int i
, ws_id
, num_screens
;
8883 char *b
, *str
, *sp
, s
[1024];
8886 case SWM_S_BAR_ACTION
:
8888 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
8889 err(1, "setconfvalue: bar_action");
8891 case SWM_S_BAR_AT_BOTTOM
:
8892 bar_at_bottom
= (atoi(value
) != 0);
8894 case SWM_S_BAR_BORDER_WIDTH
:
8895 bar_border_width
= atoi(value
);
8896 if (bar_border_width
< 0)
8897 bar_border_width
= 0;
8899 case SWM_S_BAR_DELAY
:
8900 /* No longer needed; leave to not break old conf files. */
8902 case SWM_S_BAR_ENABLED
:
8903 bar_enabled
= (atoi(value
) != 0);
8905 case SWM_S_BAR_ENABLED_WS
:
8906 ws_id
= atoi(selector
) - 1;
8907 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8908 errx(1, "setconfvalue: bar_enabled_ws: invalid "
8909 "workspace %d.", ws_id
+ 1);
8911 num_screens
= get_screen_count();
8912 for (i
= 0; i
< num_screens
; i
++) {
8913 ws
= (struct workspace
*)&screens
[i
].ws
;
8914 ws
[ws_id
].bar_enabled
= (atoi(value
) != 0);
8917 case SWM_S_BAR_FONT
:
8919 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
8920 err(1, "setconfvalue: asprintf: failed to allocate "
8921 "memory for bar_fonts.");
8924 /* If already in xft mode, then we are done. */
8925 if (!bar_font_legacy
)
8928 if ((sp
= str
= strdup(value
)) == NULL
)
8929 err(1, "setconfvalue: strdup");
8931 /* If there are any non-XLFD entries, switch to Xft mode. */
8932 while ((b
= strsep(&sp
, ",")) != NULL
) {
8936 bar_font_legacy
= false;
8943 case SWM_S_BAR_FORMAT
:
8945 if ((bar_format
= strdup(value
)) == NULL
)
8946 err(1, "setconfvalue: bar_format");
8948 case SWM_S_BAR_JUSTIFY
:
8949 if (strcmp(value
, "left") == 0)
8950 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
8951 else if (strcmp(value
, "center") == 0)
8952 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
8953 else if (strcmp(value
, "right") == 0)
8954 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
8956 errx(1, "invalid bar_justify");
8958 case SWM_S_BORDER_WIDTH
:
8959 border_width
= atoi(value
);
8960 if (border_width
< 0)
8963 case SWM_S_BOUNDARY_WIDTH
:
8964 boundary_width
= atoi(value
);
8965 if (boundary_width
< 0)
8968 case SWM_S_CLOCK_ENABLED
:
8969 clock_enabled
= (atoi(value
) != 0);
8971 case SWM_S_CLOCK_FORMAT
:
8972 #ifndef SWM_DENY_CLOCK_FORMAT
8974 if ((clock_format
= strdup(value
)) == NULL
)
8975 err(1, "setconfvalue: clock_format");
8978 case SWM_S_CYCLE_EMPTY
:
8979 cycle_empty
= (atoi(value
) != 0);
8981 case SWM_S_CYCLE_VISIBLE
:
8982 cycle_visible
= (atoi(value
) != 0);
8984 case SWM_S_DIALOG_RATIO
:
8985 dialog_ratio
= atof(value
);
8986 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
8989 case SWM_S_DISABLE_BORDER
:
8990 disable_border
= (atoi(value
) != 0);
8992 case SWM_S_FOCUS_CLOSE
:
8993 if (strcmp(value
, "first") == 0)
8994 focus_close
= SWM_STACK_BOTTOM
;
8995 else if (strcmp(value
, "last") == 0)
8996 focus_close
= SWM_STACK_TOP
;
8997 else if (strcmp(value
, "next") == 0)
8998 focus_close
= SWM_STACK_ABOVE
;
8999 else if (strcmp(value
, "previous") == 0)
9000 focus_close
= SWM_STACK_BELOW
;
9002 errx(1, "focus_close");
9004 case SWM_S_FOCUS_CLOSE_WRAP
:
9005 focus_close_wrap
= (atoi(value
) != 0);
9007 case SWM_S_FOCUS_DEFAULT
:
9008 if (strcmp(value
, "last") == 0)
9009 focus_default
= SWM_STACK_TOP
;
9010 else if (strcmp(value
, "first") == 0)
9011 focus_default
= SWM_STACK_BOTTOM
;
9013 errx(1, "focus_default");
9015 case SWM_S_FOCUS_MODE
:
9016 if (strcmp(value
, "default") == 0)
9017 focus_mode
= SWM_FOCUS_DEFAULT
;
9018 else if (strcmp(value
, "follow") == 0 ||
9019 strcmp(value
, "follow_cursor") == 0)
9020 focus_mode
= SWM_FOCUS_FOLLOW
;
9021 else if (strcmp(value
, "manual") == 0)
9022 focus_mode
= SWM_FOCUS_MANUAL
;
9024 errx(1, "focus_mode");
9026 case SWM_S_ICONIC_ENABLED
:
9027 iconic_enabled
= (atoi(value
) != 0);
9029 case SWM_S_JAVA_WORKAROUND
:
9030 java_workaround
= (atoi(value
) != 0);
9032 case SWM_S_MAXIMIZE_HIDE_BAR
:
9033 maximize_hide_bar
= atoi(value
);
9035 case SWM_S_REGION_PADDING
:
9036 region_padding
= atoi(value
);
9037 if (region_padding
< 0)
9040 case SWM_S_SPAWN_ORDER
:
9041 if (strcmp(value
, "first") == 0)
9042 spawn_position
= SWM_STACK_BOTTOM
;
9043 else if (strcmp(value
, "last") == 0)
9044 spawn_position
= SWM_STACK_TOP
;
9045 else if (strcmp(value
, "next") == 0)
9046 spawn_position
= SWM_STACK_ABOVE
;
9047 else if (strcmp(value
, "previous") == 0)
9048 spawn_position
= SWM_STACK_BELOW
;
9050 errx(1, "spawn_position");
9052 case SWM_S_SPAWN_TERM
:
9053 setconfspawn("term", value
, 0);
9054 setconfspawn("spawn_term", value
, 0);
9057 /* No longer needed; leave to not break old conf files. */
9059 case SWM_S_SS_ENABLED
:
9060 /* No longer needed; leave to not break old conf files. */
9062 case SWM_S_STACK_ENABLED
:
9063 stack_enabled
= (atoi(value
) != 0);
9065 case SWM_S_TERM_WIDTH
:
9066 term_width
= atoi(value
);
9070 case SWM_S_TILE_GAP
:
9071 tile_gap
= atoi(value
);
9073 case SWM_S_URGENT_COLLAPSE
:
9074 urgent_collapse
= (atoi(value
) != 0);
9076 case SWM_S_URGENT_ENABLED
:
9077 urgent_enabled
= (atoi(value
) != 0);
9079 case SWM_S_VERBOSE_LAYOUT
:
9080 verbose_layout
= (atoi(value
) != 0);
9081 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
9083 layouts
[i
].l_string
= fancy_stacker
;
9085 layouts
[i
].l_string
= plain_stacker
;
9088 case SWM_S_WARP_FOCUS
:
9089 warp_focus
= (atoi(value
) != 0);
9091 case SWM_S_WARP_POINTER
:
9092 warp_pointer
= (atoi(value
) != 0);
9094 case SWM_S_WINDOW_CLASS_ENABLED
:
9095 window_class_enabled
= (atoi(value
) != 0);
9097 case SWM_S_WINDOW_INSTANCE_ENABLED
:
9098 window_instance_enabled
= (atoi(value
) != 0);
9100 case SWM_S_WINDOW_NAME_ENABLED
:
9101 window_name_enabled
= (atoi(value
) != 0);
9103 case SWM_S_WORKSPACE_CLAMP
:
9104 workspace_clamp
= (atoi(value
) != 0);
9106 case SWM_S_WORKSPACE_LIMIT
:
9107 workspace_limit
= atoi(value
);
9108 if (workspace_limit
> SWM_WS_MAX
)
9109 workspace_limit
= SWM_WS_MAX
;
9110 else if (workspace_limit
< 1)
9111 workspace_limit
= 1;
9113 ewmh_update_desktops();
9115 case SWM_S_WORKSPACE_NAME
:
9116 if (getenv("SWM_STARTED") != NULL
)
9120 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9121 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
9123 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9124 errx(1, "setconfvalue: workspace_name: invalid "
9125 "workspace %d.", ws_id
+ 1);
9127 num_screens
= get_screen_count();
9128 for (i
= 0; i
< num_screens
; ++i
) {
9129 ws
= (struct workspace
*)&screens
[i
].ws
;
9131 if (strlen(s
) > 0) {
9132 free(ws
[ws_id
].name
);
9133 if ((ws
[ws_id
].name
= strdup(s
)) == NULL
)
9134 err(1, "setconfvalue: workspace_name.");
9136 ewmh_update_desktop_names();
9137 ewmh_get_desktop_names();
9148 setconfmodkey(const char *selector
, const char *value
, int flags
)
9150 /* suppress unused warnings since vars are needed */
9154 if (strncasecmp(value
, "Mod1", strlen("Mod1")) == 0)
9155 update_modkey(XCB_MOD_MASK_1
);
9156 else if (strncasecmp(value
, "Mod2", strlen("Mod2")) == 0)
9157 update_modkey(XCB_MOD_MASK_2
);
9158 else if (strncasecmp(value
, "Mod3", strlen("Mod3")) == 0)
9159 update_modkey(XCB_MOD_MASK_3
);
9160 else if (strncasecmp(value
, "Mod4", strlen("Mod4")) == 0)
9161 update_modkey(XCB_MOD_MASK_4
);
9162 else if (strncasecmp(value
, "Mod5", strlen("Mod5")) == 0)
9163 update_modkey(XCB_MOD_MASK_5
);
9170 setconfcolor(const char *selector
, const char *value
, int flags
)
9172 int first
, last
, i
= 0, num_screens
;
9174 num_screens
= get_screen_count();
9176 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
9177 if (selector
== NULL
|| strlen(selector
) == 0 ||
9178 (last
= atoi(selector
) - 1) < 0) {
9180 last
= num_screens
- 1;
9185 if (last
>= num_screens
) {
9186 add_startup_exception("invalid screen index: %d out of bounds "
9187 "(maximum %d)", last
+ 1, num_screens
);
9191 for (i
= first
; i
<= last
; ++i
) {
9192 setscreencolor(value
, i
, flags
);
9195 * When setting focus/unfocus colors, we need to also
9196 * set maximize colors to match if they haven't been customized.
9198 if (flags
== SWM_S_COLOR_FOCUS
&&
9199 !screens
[i
].c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].manual
)
9200 setscreencolor(value
, i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
9201 else if (flags
== SWM_S_COLOR_UNFOCUS
&&
9202 !screens
[i
].c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].manual
)
9203 setscreencolor(value
, i
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
9205 screens
[i
].c
[flags
].manual
= 1;
9212 setconfregion(const char *selector
, const char *value
, int flags
)
9214 /* suppress unused warnings since vars are needed */
9218 custom_region(value
);
9223 setautorun(const char *selector
, const char *value
, int flags
)
9227 char *ap
, *sp
, *str
;
9233 /* suppress unused warnings since vars are needed */
9237 if (getenv("SWM_STARTED"))
9241 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9242 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
9244 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9245 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
9247 sp
= str
= expand_tilde((char *)&s
);
9250 * This is a little intricate
9252 * If the pid already exists we simply reuse it because it means it was
9253 * used before AND not claimed by manage_window. We get away with
9254 * altering it in the parent after INSERT because this can not be a race
9257 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
9260 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
9262 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
9263 err(1, "setautorun: realloc");
9264 a
.argv
[argc
- 1] = ap
;
9267 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
9268 err(1, "setautorun: realloc");
9269 a
.argv
[argc
] = NULL
;
9271 if ((pid
= fork()) == 0) {
9272 spawn(ws_id
, &a
, true);
9282 p
= calloc(1, sizeof *p
);
9285 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
9295 setlayout(const char *selector
, const char *value
, int flags
)
9297 struct workspace
*ws
;
9298 int ws_id
, i
, x
, mg
, ma
, si
, ar
;
9299 int st
= SWM_V_STACK
, num_screens
;
9303 /* suppress unused warnings since vars are needed */
9307 if (getenv("SWM_STARTED"))
9311 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
9312 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
9313 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9314 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9317 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9318 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
9320 if (strcasecmp(s
, "vertical") == 0)
9322 else if (strcasecmp(s
, "vertical_flip") == 0) {
9325 } else if (strcasecmp(s
, "horizontal") == 0)
9327 else if (strcasecmp(s
, "horizontal_flip") == 0) {
9330 } else if (strcasecmp(s
, "fullscreen") == 0)
9333 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9334 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9337 num_screens
= get_screen_count();
9338 for (i
= 0; i
< num_screens
; i
++) {
9339 ws
= (struct workspace
*)&screens
[i
].ws
;
9340 ws
[ws_id
].cur_layout
= &layouts
[st
];
9342 ws
[ws_id
].always_raise
= (ar
!= 0);
9343 if (st
== SWM_MAX_STACK
)
9347 for (x
= 0; x
< abs(mg
); x
++) {
9348 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9349 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
9350 SWM_ARG_ID_MASTERSHRINK
);
9353 for (x
= 0; x
< abs(ma
); x
++) {
9354 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9355 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
9356 SWM_ARG_ID_MASTERDEL
);
9359 for (x
= 0; x
< abs(si
); x
++) {
9360 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9361 si
>= 0 ? SWM_ARG_ID_STACKINC
:
9362 SWM_ARG_ID_STACKDEC
);
9366 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9367 SWM_ARG_ID_FLIPLAYOUT
);
9374 /* config options */
9375 struct config_option
{
9377 int (*func
)(const char*, const char*, int);
9380 struct config_option configopt
[] = {
9381 { "autorun", setautorun
, 0 },
9382 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
9383 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
9384 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
9385 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
9386 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
9387 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
9388 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
9389 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
9390 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
9391 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
9392 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
9393 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
9394 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
9395 { "bind", setconfbinding
, 0 },
9396 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
9397 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
9398 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
9399 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
9400 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
9401 { "color_focus_maximized", setconfcolor
, SWM_S_COLOR_FOCUS_MAXIMIZED
},
9402 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
9403 { "color_unfocus_maximized", setconfcolor
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
},
9404 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
9405 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
9406 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
9407 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
9408 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
9409 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
9410 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
9411 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
9412 { "iconic_enabled", setconfvalue
, SWM_S_ICONIC_ENABLED
},
9413 { "java_workaround", setconfvalue
, SWM_S_JAVA_WORKAROUND
},
9414 { "keyboard_mapping", setkeymapping
, 0 },
9415 { "layout", setlayout
, 0 },
9416 { "maximize_hide_bar", setconfvalue
, SWM_S_MAXIMIZE_HIDE_BAR
},
9417 { "modkey", setconfmodkey
, 0 },
9418 { "program", setconfspawn
, 0 },
9419 { "quirk", setconfquirk
, 0 },
9420 { "region", setconfregion
, 0 },
9421 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
9422 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
9423 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
9424 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
9425 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
9426 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
9427 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
9428 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
9429 { "title_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
}, /* For backwards compat. */
9430 { "title_name_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
}, /* For backwards compat. */
9431 { "urgent_collapse", setconfvalue
, SWM_S_URGENT_COLLAPSE
},
9432 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
9433 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
9434 { "warp_focus", setconfvalue
, SWM_S_WARP_FOCUS
},
9435 { "warp_pointer", setconfvalue
, SWM_S_WARP_POINTER
},
9436 { "window_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
},
9437 { "window_instance_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
},
9438 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
9439 { "workspace_clamp", setconfvalue
, SWM_S_WORKSPACE_CLAMP
},
9440 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
9441 { "name", setconfvalue
, SWM_S_WORKSPACE_NAME
},
9445 _add_startup_exception(const char *fmt
, va_list ap
)
9447 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
9448 warn("%s: asprintf", __func__
);
9452 add_startup_exception(const char *fmt
, ...)
9458 if (startup_exception
)
9461 /* force bar to be enabled due to exception */
9465 _add_startup_exception(fmt
, ap
);
9470 conf_load(const char *filename
, int keymapping
)
9473 char *line
= NULL
, *cp
, *ce
, *optsub
, *optval
= NULL
;
9474 size_t linelen
, lineno
= 0;
9475 int wordlen
, i
, optidx
, count
;
9476 struct config_option
*opt
= NULL
;
9478 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
9480 if (filename
== NULL
) {
9481 warnx("conf_load: no filename");
9485 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
9487 if ((config
= fopen(filename
, "r")) == NULL
) {
9488 warn("conf_load: fopen: %s", filename
);
9492 while (!feof(config
)) {
9496 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
9497 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
9499 err(1, "%s", filename
);
9504 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9505 if (cp
[0] == '\0') {
9509 /* get config option */
9510 wordlen
= strcspn(cp
, "=[ \t\n");
9512 add_startup_exception("%s: line %zd: no option found",
9517 for (i
= 0; i
< LENGTH(configopt
); i
++) {
9518 opt
= &configopt
[i
];
9519 if (strncasecmp(cp
, opt
->optname
, wordlen
) == 0 &&
9520 (int)strlen(opt
->optname
) == wordlen
) {
9526 add_startup_exception("%s: line %zd: unknown option "
9527 "%.*s", filename
, lineno
, wordlen
, cp
);
9530 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
9531 add_startup_exception("%s: line %zd: invalid option "
9532 "%.*s", filename
, lineno
, wordlen
, cp
);
9536 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9538 /* get [selector] if any */
9543 /* Get length of selector. */
9544 for (ce
= ++cp
; *ce
!= '\0'; ++ce
) {
9545 /* Find matching (not escaped) bracket. */
9546 if (*ce
== ']' && *(ce
- 1) != '\\') {
9553 add_startup_exception("%s: line %zd: syntax "
9554 "error: unterminated selector", filename
,
9559 /* ce points at ']'; terminate optsub. */
9564 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
9567 if (strlen(optval
) == 0) {
9568 add_startup_exception("%s: line %zd: must supply value "
9569 "to %s", filename
, lineno
,
9570 configopt
[optidx
].optname
);
9573 /* call function to deal with it all */
9574 if (configopt
[optidx
].func(optsub
, optval
,
9575 configopt
[optidx
].funcflags
) != 0) {
9576 add_startup_exception("%s: line %zd: invalid data for "
9577 "%s", filename
, lineno
, configopt
[optidx
].optname
);
9586 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
9592 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
9594 struct ws_win
*parent
, *w
;
9595 struct swm_region
*r
;
9596 struct workspace
*ws
;
9597 xcb_icccm_wm_hints_t wmh
;
9599 parent
= find_window(win
->transient
);
9601 parent
->focus_child
= win
;
9603 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
9604 " for %#x trans %#x\n", win
->id
, win
->transient
);
9606 r
= root_to_region(win
->s
->root
, SWM_CK_ALL
);
9608 /* parent doen't exist in our window list */
9609 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
9610 if (xcb_icccm_get_wm_hints_reply(conn
,
9611 xcb_icccm_get_wm_hints(conn
, w
->id
),
9613 warnx("can't get hints for %#x", w
->id
);
9617 if (win
->hints
.window_group
!= wmh
.window_group
)
9620 w
->focus_child
= win
;
9621 win
->transient
= w
->id
;
9623 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
9624 "transient to %#x\n", win
->transient
);
9631 window_get_pid(xcb_window_t win
)
9636 xcb_get_property_cookie_t pc
;
9637 xcb_get_property_reply_t
*pr
;
9639 apid
= get_atom_from_string("_NET_WM_PID");
9640 if (apid
== XCB_ATOM_NONE
)
9643 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
9644 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9647 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
9652 if (pr
->type
== apid
&& pr
->format
== 32)
9653 ret
= *((pid_t
*)xcb_get_property_value(pr
));
9659 apid
= get_atom_from_string("_SWM_PID");
9660 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
9662 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9665 if (pr
->type
!= apid
) {
9670 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
9677 get_swm_ws(xcb_window_t id
)
9683 xcb_get_property_reply_t
*gpr
;
9685 gpr
= xcb_get_property_reply(conn
,
9686 xcb_get_property(conn
, 0, id
, a_swm_ws
,
9687 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
9692 proplen
= xcb_get_property_value_length(gpr
);
9694 prop
= malloc(proplen
+ 1);
9697 xcb_get_property_value(gpr
),
9699 prop
[proplen
] = '\0';
9706 DNPRINTF(SWM_D_PROP
, "get_swm_ws: _SWM_WS: %s\n", prop
);
9707 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
9709 DNPRINTF(SWM_D_PROP
, "get_swm_ws: win #%s: %s",
9719 get_ws_idx(struct ws_win
*win
)
9721 xcb_get_property_reply_t
*gpr
;
9727 gpr
= xcb_get_property_reply(conn
,
9728 xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
9729 XCB_ATOM_CARDINAL
, 0, 1),
9732 if (gpr
->type
== XCB_ATOM_CARDINAL
&& gpr
->format
== 32)
9733 ws_idx
= *((int *)xcb_get_property_value(gpr
));
9737 if (ws_idx
== -1 && !(win
->quirks
& SWM_Q_IGNORESPAWNWS
))
9738 ws_idx
= get_swm_ws(win
->id
);
9740 if (ws_idx
> workspace_limit
- 1 || ws_idx
< -1)
9743 DNPRINTF(SWM_D_PROP
, "get_ws_idx: win %#x, ws_idx: %d\n", win
->id
,
9750 reparent_window(struct ws_win
*win
)
9752 xcb_void_cookie_t c
;
9753 xcb_generic_error_t
*error
;
9756 win
->frame
= xcb_generate_id(conn
);
9758 DNPRINTF(SWM_D_MISC
, "reparent_window: win %#x, frame: %#x\n",
9759 win
->id
, win
->frame
);
9762 XCB_EVENT_MASK_ENTER_WINDOW
|
9763 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
9764 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
9765 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
|
9766 XCB_EVENT_MASK_EXPOSURE
;
9768 wa
[0] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
9771 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
->frame
, win
->s
->root
,
9772 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), 0,
9773 XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
9774 XCB_CW_EVENT_MASK
, wa
);
9776 win
->state
= SWM_WIN_STATE_REPARENTING
;
9777 c
= xcb_reparent_window_checked(conn
, win
->id
, win
->frame
, 0, 0);
9778 if ((error
= xcb_request_check(conn
, c
))) {
9779 DNPRINTF(SWM_D_MISC
, "reparent_window: error:\n");
9784 xcb_destroy_window(conn
, win
->frame
);
9785 win
->frame
= XCB_WINDOW_NONE
;
9787 xcb_change_save_set(conn
, XCB_SET_MODE_INSERT
, win
->id
);
9789 DNPRINTF(SWM_D_MISC
, "reparent_window: done.\n");
9793 unparent_window(struct ws_win
*win
)
9795 xcb_change_save_set(conn
, XCB_SET_MODE_DELETE
, win
->id
);
9796 xcb_reparent_window(conn
, win
->id
, win
->s
->root
, X(win
), Y(win
));
9797 xcb_destroy_window(conn
, win
->frame
);
9798 win
->frame
= XCB_WINDOW_NONE
;
9799 win
->state
= SWM_WIN_STATE_UNPARENTING
;
9803 manage_window(xcb_window_t id
, int spawn_pos
, bool mapping
)
9805 struct ws_win
*win
= NULL
, *ww
;
9806 struct swm_region
*r
;
9809 xcb_get_geometry_reply_t
*gr
;
9810 xcb_get_window_attributes_reply_t
*war
= NULL
;
9811 xcb_window_t trans
= XCB_WINDOW_NONE
;
9812 uint32_t i
, wa
[1], new_flags
;
9813 int ws_idx
, force_ws
= -1;
9814 char *class, *instance
, *name
;
9817 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region bar; "
9822 if (find_region(id
)) {
9823 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region window; "
9828 if ((win
= find_window(id
)) != NULL
) {
9829 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x already "
9830 "managed; skipping.)\n", id
);
9831 return (win
); /* Already managed. */
9834 /* See if window is on the unmanaged list. */
9835 if ((win
= find_unmanaged_window(id
)) != NULL
) {
9836 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x found on "
9837 "unmanaged list.\n", id
);
9838 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
9841 set_child_transient(win
, &trans
);
9845 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is new.\n", id
);
9848 war
= xcb_get_window_attributes_reply(conn
,
9849 xcb_get_window_attributes(conn
, id
), NULL
);
9851 DNPRINTF(SWM_D_EVENT
, "manage_window: window lost.\n");
9855 if (war
->override_redirect
) {
9856 DNPRINTF(SWM_D_EVENT
, "manage_window: override_redirect; "
9861 if (!mapping
&& war
->map_state
== XCB_MAP_STATE_UNMAPPED
&&
9862 get_win_state(id
) == XCB_ICCCM_WM_STATE_WITHDRAWN
) {
9863 DNPRINTF(SWM_D_EVENT
, "manage_window: window withdrawn; "
9868 /* Try to get initial window geometry. */
9869 gr
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, id
), NULL
);
9871 DNPRINTF(SWM_D_MISC
, "manage_window: get geometry failed.\n");
9875 /* Create and initialize ws_win object. */
9876 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
9877 err(1, "manage_window: calloc: failed to allocate memory for "
9882 /* Figureout which region the window belongs to. */
9883 r
= root_to_region(gr
->root
, SWM_CK_ALL
);
9885 /* Ignore window border if there is one. */
9886 WIDTH(win
) = gr
->width
;
9887 HEIGHT(win
) = gr
->height
;
9888 X(win
) = gr
->x
+ gr
->border_width
;
9889 Y(win
) = gr
->y
+ gr
->border_width
;
9890 win
->bordered
= false;
9891 win
->mapped
= (war
->map_state
!= XCB_MAP_STATE_UNMAPPED
);
9892 win
->s
= r
->s
; /* this never changes */
9896 /* Select which X events to monitor and set border pixel color. */
9897 wa
[0] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
9898 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
9900 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_EVENT_MASK
, wa
);
9902 /* Get WM_SIZE_HINTS. */
9903 xcb_icccm_get_wm_normal_hints_reply(conn
,
9904 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
9908 xcb_icccm_get_wm_hints_reply(conn
,
9909 xcb_icccm_get_wm_hints(conn
, win
->id
),
9912 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
9913 xcb_icccm_get_wm_transient_for_reply(conn
,
9914 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
9917 win
->transient
= trans
;
9918 set_child_transient(win
, &win
->transient
);
9921 /* Get WM_PROTOCOLS. */
9922 get_wm_protocols(win
);
9925 /* Must be after getting WM_HINTS and WM_PROTOCOLS. */
9926 DNPRINTF(SWM_D_FOCUS
, "manage_window: input_model: %s\n",
9927 get_win_input_model(win
));
9930 /* Set initial quirks based on EWMH. */
9931 ewmh_autoquirk(win
);
9933 /* Determine initial quirks. */
9934 xcb_icccm_get_wm_class_reply(conn
,
9935 xcb_icccm_get_wm_class(conn
, win
->id
),
9938 class = win
->ch
.class_name
? win
->ch
.class_name
: "";
9939 instance
= win
->ch
.instance_name
? win
->ch
.instance_name
: "";
9940 name
= get_win_name(win
->id
);
9942 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, instance: %s, "
9943 "name: %s\n", class, instance
, name
);
9945 /* java is retarded so treat it special */
9946 if (strstr(instance
, "sun-awt")) {
9947 DNPRINTF(SWM_D_CLASS
, "manage_window: java window detected.\n");
9951 TAILQ_FOREACH(qp
, &quirks
, entry
) {
9952 if (regexec(&qp
->regex_class
, class, 0, NULL
, 0) == 0 &&
9953 regexec(&qp
->regex_instance
, instance
, 0, NULL
, 0) == 0 &&
9954 regexec(&qp
->regex_name
, name
, 0, NULL
, 0) == 0) {
9955 DNPRINTF(SWM_D_CLASS
, "manage_window: matched "
9956 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp
->class,
9957 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
9958 win
->quirks
= qp
->quirk
;
9959 if (qp
->ws
>= 0 && qp
->ws
< workspace_limit
)
9966 /* Reset font sizes (the bruteforce way; no default keybinding). */
9967 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
9968 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9969 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
9970 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9971 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
9974 /* Figure out which workspace the window belongs to. */
9975 if (!(win
->quirks
& SWM_Q_IGNOREPID
) &&
9976 (p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
9977 win
->ws
= &r
->s
->ws
[p
->ws
];
9978 TAILQ_REMOVE(&pidlist
, p
, entry
);
9981 } else if ((ws_idx
= get_ws_idx(win
)) != -1 &&
9983 /* _SWM_WS is set; use that. */
9984 win
->ws
= &r
->s
->ws
[ws_idx
];
9985 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
9986 /* Launch transients in the same ws as parent. */
9993 win
->ws
= &r
->s
->ws
[force_ws
];
9995 /* Set the _NET_WM_DESKTOP atom. */
9996 DNPRINTF(SWM_D_PROP
, "manage_window: set _NET_WM_DESKTOP: %d\n",
9998 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
9999 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &win
->ws
->idx
);
10001 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
10002 xcb_delete_property(conn
, win
->id
, a_swm_ws
);
10004 /* WS must already be set for this to work. */
10005 store_float_geom(win
);
10007 /* Make sure window is positioned inside its region, if its active. */
10009 region_containment(win
, r
, SWM_CW_ALLSIDES
|
10010 SWM_CW_HARDBOUNDARY
);
10011 update_window(win
);
10015 /* Figure out where to insert the window in the workspace list. */
10016 if (trans
&& (ww
= find_window(trans
)))
10017 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
10018 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_ABOVE
)
10019 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
10021 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_BELOW
)
10022 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
10023 else switch (spawn_pos
) {
10025 case SWM_STACK_TOP
:
10026 case SWM_STACK_ABOVE
:
10027 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
10029 case SWM_STACK_BOTTOM
:
10030 case SWM_STACK_BELOW
:
10031 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
10034 ewmh_update_client_list();
10036 TAILQ_INSERT_HEAD(&win
->ws
->stack
, win
, stack_entry
);
10039 /* Get/apply initial _NET_WM_STATE */
10040 ewmh_get_wm_state(win
);
10042 /* Apply quirks. */
10043 new_flags
= win
->ewmh_flags
;
10045 if (win
->quirks
& SWM_Q_FLOAT
)
10046 new_flags
|= EWMH_F_ABOVE
;
10048 if (win
->quirks
& SWM_Q_ANYWHERE
)
10049 new_flags
|= SWM_F_MANUAL
;
10051 ewmh_apply_flags(win
, new_flags
);
10052 ewmh_update_wm_state(win
);
10054 /* Set initial _NET_WM_ALLOWED_ACTIONS */
10055 ewmh_update_actions(win
);
10057 reparent_window(win
);
10059 DNPRINTF(SWM_D_MISC
, "manage_window: done. win %#x, (x,y) w x h: "
10060 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win
->id
,
10061 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
10062 YESNO(ICONIC(win
)), win
->transient
);
10069 free_window(struct ws_win
*win
)
10071 DNPRINTF(SWM_D_MISC
, "free_window: win %#x\n", WINID(win
));
10076 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
10079 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
10082 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
10086 unmanage_window(struct ws_win
*win
)
10088 DNPRINTF(SWM_D_MISC
, "unmanage_window: win %#x\n", WINID(win
));
10094 unparent_window(win
);
10096 TAILQ_REMOVE(&win
->ws
->stack
, win
, stack_entry
);
10097 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
10098 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
10100 ewmh_update_client_list();
10104 expose(xcb_expose_event_t
*e
)
10109 struct workspace
*ws
;
10112 DNPRINTF(SWM_D_EVENT
, "expose: win %#x, count: %d\n", e
->window
,
10118 if ((b
= find_bar(e
->window
))) {
10121 } else if ((w
= find_window(e
->window
)) && w
->frame
== e
->window
) {
10125 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
10131 DNPRINTF(SWM_D_EVENT
, "expose: done\n");
10135 focusin(xcb_focus_in_event_t
*e
)
10137 struct ws_win
*win
;
10139 DNPRINTF(SWM_D_EVENT
, "focusin: win %#x, mode: %s(%u), "
10140 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10141 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10142 if ((win
= find_window(e
->event
)) && win
!= win
->ws
->focus
&&
10143 win
!= win
->ws
->focus_pending
&&
10144 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10145 win
->ws
->focus_prev
= win
->ws
->focus
;
10146 win
->ws
->focus
= win
;
10147 win
->ws
->focus_pending
= NULL
;
10149 if (win
->ws
->focus_prev
)
10150 draw_frame(win
->ws
->focus_prev
);
10158 focusout(xcb_focus_out_event_t
*e
)
10160 DNPRINTF(SWM_D_EVENT
, "focusout: win %#x, mode: %s(%u), "
10161 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10162 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10167 keypress(xcb_key_press_event_t
*e
)
10170 struct binding
*bp
;
10171 xcb_keysym_t keysym
;
10172 bool replay
= true;
10174 last_event_time
= e
->time
;
10176 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
10178 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
10179 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10180 "state: %u, cleaned: %u, same_screen: %s\n", keysym
, e
->event
,
10181 e
->event_x
, e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
,
10182 e
->root_y
, e
->child
, e
->state
, CLEANMASK(e
->state
),
10183 YESNO(e
->same_screen
));
10185 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10187 /* Look for catch-all. */
10188 if ((bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
)) == NULL
)
10192 replay
= bp
->flags
& BINDING_F_REPLAY
;
10194 if ((ap
= &actions
[bp
->action
]) == NULL
)
10197 if (bp
->action
== FN_SPAWN_CUSTOM
)
10198 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10201 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10203 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10207 DNPRINTF(SWM_D_EVENT
, "keypress: replaying.\n");
10208 /* Pass keypress to event window and unfreeze keyboard queue. */
10209 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10211 /* Release freeze. */
10212 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10216 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
10220 keyrelease(xcb_key_release_event_t
*e
)
10223 struct binding
*bp
;
10224 xcb_keysym_t keysym
;
10226 last_event_time
= e
->time
;
10228 keysym
= xcb_key_release_lookup_keysym(syms
, e
, 0);
10230 DNPRINTF(SWM_D_EVENT
, "keyrelease: keysym: %u, win (x,y): %#x (%d,%d), "
10231 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10232 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
10233 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
10234 e
->child
, e
->state
, YESNO(e
->same_screen
));
10236 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10238 /* Look for catch-all. */
10239 bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
);
10241 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10242 bp
->flags
& BINDING_F_REPLAY
) {
10243 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10244 DNPRINTF(SWM_D_EVENT
, "keyrelease: replaying.\n");
10246 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10247 DNPRINTF(SWM_D_EVENT
, "keyrelease: unfreezing.\n");
10252 DNPRINTF(SWM_D_EVENT
, "keyrelease: done.\n");
10256 buttonpress(xcb_button_press_event_t
*e
)
10258 struct ws_win
*win
= NULL
, *newf
;
10259 struct swm_region
*r
, *old_r
;
10261 struct binding
*bp
;
10262 bool replay
= true;
10264 last_event_time
= e
->time
;
10266 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): %#x (%d,%d), "
10267 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10268 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10269 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10270 e
->state
, YESNO(e
->same_screen
));
10272 if (e
->event
== e
->root
) {
10274 win
= find_window(e
->child
);
10276 /* Focus on empty region */
10277 /* If no windows on region if its empty. */
10278 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10279 if (r
&& TAILQ_EMPTY(&r
->ws
->winlist
)) {
10280 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
10281 if (old_r
&& old_r
!= r
)
10282 unfocus_win(old_r
->ws
->focus
);
10284 DNPRINTF(SWM_D_FOCUS
, "buttonpress: "
10285 "set_input_focus: %#x, revert-to: parent, "
10286 "time: %#x\n", e
->root
, e
->time
);
10287 xcb_set_input_focus(conn
,
10288 XCB_INPUT_FOCUS_POINTER_ROOT
,
10291 /* Clear bar since empty. */
10294 /* No need to replay event. */
10299 win
= find_window(e
->event
);
10303 newf
= get_focus_magic(win
);
10304 if (win
->ws
->focus
== newf
&& newf
!= win
) {
10305 if (win
->focus_child
== win
)
10306 win
->focus_child
= NULL
;
10312 /* Handle any bound action. */
10313 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10315 /* Look for catch-all. */
10316 if ((bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
)) == NULL
)
10321 replay
= bp
->flags
& BINDING_F_REPLAY
;
10323 if ((ap
= &actions
[bp
->action
]) == NULL
)
10326 if (bp
->action
== FN_SPAWN_CUSTOM
)
10327 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10330 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10332 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10336 DNPRINTF(SWM_D_EVENT
, "buttonpress: replaying.\n");
10337 /* Replay event to event window */
10338 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10340 /* Unfreeze grab events. */
10341 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10348 buttonrelease(xcb_button_release_event_t
*e
)
10351 struct binding
*bp
;
10353 last_event_time
= e
->time
;
10355 DNPRINTF(SWM_D_EVENT
, "buttonrelease: win (x,y): %#x (%d,%d), "
10356 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10357 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10358 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10359 e
->state
, YESNO(e
->same_screen
));
10361 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10363 /* Look for catch-all. */
10364 bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
);
10366 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10367 bp
->flags
& BINDING_F_REPLAY
) {
10368 DNPRINTF(SWM_D_EVENT
, "buttonrelease: replaying.\n");
10369 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10371 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10379 get_win_input_model(struct ws_win
*win
)
10383 * Input Model Input Field WM_TAKE_FOCUS
10384 * No Input False Absent
10385 * Passive True Absent
10386 * Locally Active True Present
10387 * Globally Active False Present
10390 if (ACCEPTS_FOCUS(win
))
10391 inputmodel
= (win
->take_focus
) ? "Locally Active" : "Passive";
10393 inputmodel
= (win
->take_focus
) ? "Globally Active" : "No Input";
10399 print_win_geom(xcb_window_t w
)
10401 xcb_get_geometry_reply_t
*wa
;
10403 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
10405 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: %#x\n",
10410 DNPRINTF(SWM_D_MISC
, "print_win_geom: win %#x, root: %#x, "
10411 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
10412 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
10421 get_stack_mode_name(uint8_t mode
)
10426 case XCB_STACK_MODE_ABOVE
:
10429 case XCB_STACK_MODE_BELOW
:
10432 case XCB_STACK_MODE_TOP_IF
:
10435 case XCB_STACK_MODE_BOTTOM_IF
:
10438 case XCB_STACK_MODE_OPPOSITE
:
10450 configurerequest(xcb_configure_request_event_t
*e
)
10452 struct ws_win
*win
;
10453 struct swm_region
*r
= NULL
;
10455 uint32_t wc
[7] = {0};
10459 if ((win
= find_window(e
->window
)) == NULL
)
10460 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10464 if (swm_debug
& SWM_D_EVENT
) {
10465 print_win_geom(e
->window
);
10467 DNPRINTF(SWM_D_EVENT
, "configurerequest: win %#x, "
10468 "parent: %#x, new: %s, value_mask: %u { ", e
->window
,
10469 e
->parent
, YESNO(new), e
->value_mask
);
10470 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
10471 DPRINTF("X: %d ", e
->x
);
10472 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
10473 DPRINTF("Y: %d ", e
->y
);
10474 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10475 DPRINTF("W: %u ", e
->width
);
10476 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10477 DPRINTF("H: %u ", e
->height
);
10478 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
10479 DPRINTF("Border: %u ", e
->border_width
);
10480 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
10481 DPRINTF("Sibling: %#x ", e
->sibling
);
10482 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
10483 DPRINTF("StackMode: %s(%u) ",
10484 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
10490 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10491 mask
|= XCB_CONFIG_WINDOW_X
;
10494 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10495 mask
|= XCB_CONFIG_WINDOW_Y
;
10498 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
10499 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
10500 wc
[i
++] = e
->width
;
10502 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
10503 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
10504 wc
[i
++] = e
->height
;
10506 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
10507 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
10508 wc
[i
++] = e
->border_width
;
10510 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
10511 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
10512 wc
[i
++] = e
->sibling
;
10514 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
10515 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
10516 wc
[i
++] = e
->stack_mode
;
10520 xcb_configure_window(conn
, e
->window
, mask
, wc
);
10523 } else if ((!MANUAL(win
) || win
->quirks
& SWM_Q_ANYWHERE
) &&
10524 !FULLSCREEN(win
) && !MAXIMIZED(win
)) {
10527 else if (win
->ws
->old_r
)
10528 r
= win
->ws
->old_r
;
10530 /* windows are centered unless ANYWHERE quirk is set. */
10531 if (win
->quirks
& SWM_Q_ANYWHERE
) {
10532 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10533 win
->g_float
.x
= e
->x
;
10535 win
->g_float
.x
-= X(r
);
10538 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10539 win
->g_float
.y
= e
->y
;
10541 win
->g_float
.y
-= Y(r
);
10545 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10546 win
->g_float
.w
= e
->width
;
10548 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10549 win
->g_float
.h
= e
->height
;
10551 win
->g_floatvalid
= true;
10553 if (!MAXIMIZED(win
) && !FULLSCREEN(win
) &&
10554 (TRANS(win
) || (ABOVE(win
) &&
10555 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]))) {
10556 WIDTH(win
) = win
->g_float
.w
;
10557 HEIGHT(win
) = win
->g_float
.h
;
10560 update_floater(win
);
10563 config_win(win
, e
);
10567 config_win(win
, e
);
10571 config_win(win
, e
);
10575 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
10579 configurenotify(xcb_configure_notify_event_t
*e
)
10581 struct ws_win
*win
;
10583 DNPRINTF(SWM_D_EVENT
, "configurenotify: win %#x, event win: %#x, "
10584 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
10585 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
10586 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
10587 YESNO(e
->override_redirect
));
10589 win
= find_window(e
->window
);
10592 if (font_adjusted
&& win
->ws
->r
) {
10600 destroynotify(xcb_destroy_notify_event_t
*e
)
10602 struct ws_win
*win
;
10603 struct workspace
*ws
;
10605 DNPRINTF(SWM_D_EVENT
, "destroynotify: win %#x\n", e
->window
);
10607 if ((win
= find_window(e
->window
)) == NULL
) {
10608 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10610 /* Window is on unmanaged list. */
10611 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10618 if (win
->frame
== e
->window
) {
10619 DNPRINTF(SWM_D_EVENT
, "destroynotify: frame for win %#x\n",
10621 win
->frame
= XCB_WINDOW_NONE
;
10625 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10626 /* If we were focused, make sure we focus on something else. */
10627 if (win
== ws
->focus
) {
10628 ws
->focus_pending
= get_focus_prev(win
);
10629 if (ws
->focus_pending
== win
)
10630 ws
->focus_pending
= NULL
;
10634 unmanage_window(win
);
10635 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10639 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
10640 if (ws
->focus_pending
) {
10641 focus_win(ws
->focus_pending
);
10642 ws
->focus_pending
= NULL
;
10643 } else if (ws
->focus
== NULL
) {
10644 DNPRINTF(SWM_D_FOCUS
, "destroynotify: set_input_focus: "
10645 "%#x, revert-to: parent, time: 0\n", ws
->r
->id
);
10646 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
10647 ws
->r
->id
, XCB_CURRENT_TIME
);
10654 DNPRINTF(SWM_D_EVENT
, "destroynotify: done.\n");
10659 get_notify_detail_label(uint8_t detail
)
10664 case XCB_NOTIFY_DETAIL_ANCESTOR
:
10665 label
= "Ancestor";
10667 case XCB_NOTIFY_DETAIL_VIRTUAL
:
10670 case XCB_NOTIFY_DETAIL_INFERIOR
:
10671 label
= "Inferior";
10673 case XCB_NOTIFY_DETAIL_NONLINEAR
:
10674 label
= "Nonlinear";
10676 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
10677 label
= "NonlinearVirtual";
10679 case XCB_NOTIFY_DETAIL_POINTER
:
10682 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
10683 label
= "PointerRoot";
10685 case XCB_NOTIFY_DETAIL_NONE
:
10696 get_notify_mode_label(uint8_t mode
)
10701 case XCB_NOTIFY_MODE_NORMAL
:
10704 case XCB_NOTIFY_MODE_GRAB
:
10707 case XCB_NOTIFY_MODE_UNGRAB
:
10710 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
10711 label
= "WhileGrabbed";
10721 get_state_mask_label(uint16_t state
)
10726 case XCB_KEY_BUT_MASK_SHIFT
:
10727 label
= "ShiftMask";
10729 case XCB_KEY_BUT_MASK_LOCK
:
10730 label
= "LockMask";
10732 case XCB_KEY_BUT_MASK_CONTROL
:
10733 label
= "ControlMask";
10735 case XCB_KEY_BUT_MASK_MOD_1
:
10736 label
= "Mod1Mask";
10738 case XCB_KEY_BUT_MASK_MOD_2
:
10739 label
= "Mod2Mask";
10741 case XCB_KEY_BUT_MASK_MOD_3
:
10742 label
= "Mod3Mask";
10744 case XCB_KEY_BUT_MASK_MOD_4
:
10745 label
= "Mod4Mask";
10747 case XCB_KEY_BUT_MASK_MOD_5
:
10748 label
= "Mod5Mask";
10750 case XCB_KEY_BUT_MASK_BUTTON_1
:
10751 label
= "Button1Mask";
10753 case XCB_KEY_BUT_MASK_BUTTON_2
:
10754 label
= "Button2Mask";
10756 case XCB_KEY_BUT_MASK_BUTTON_3
:
10757 label
= "Button3Mask";
10759 case XCB_KEY_BUT_MASK_BUTTON_4
:
10760 label
= "Button4Mask";
10762 case XCB_KEY_BUT_MASK_BUTTON_5
:
10763 label
= "Button5Mask";
10777 enternotify(xcb_enter_notify_event_t
*e
)
10779 struct ws_win
*win
;
10780 struct swm_region
*r
;
10782 last_event_time
= e
->time
;
10784 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): %#x "
10785 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10786 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10787 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10788 get_notify_mode_label(e
->mode
), e
->mode
,
10789 get_notify_detail_label(e
->detail
), e
->detail
,
10790 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10791 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10794 if (e
->event
== e
->root
&& e
->child
== XCB_WINDOW_NONE
&&
10795 e
->mode
== XCB_NOTIFY_MODE_GRAB
&&
10796 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10797 DNPRINTF(SWM_D_EVENT
, "enternotify: grab inferior; ignoring.\n");
10801 if (focus_mode
== SWM_FOCUS_MANUAL
&&
10802 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10803 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
10807 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10808 e
->mode
== XCB_NOTIFY_MODE_UNGRAB
&&
10809 e
->detail
!= XCB_NOTIFY_DETAIL_ANCESTOR
) {
10810 DNPRINTF(SWM_D_EVENT
, "enternotify: ungrab; ignoring.\n");
10814 if ((win
= find_window(e
->event
)) == NULL
) {
10815 if (e
->event
== e
->root
) {
10816 /* If no windows on pointer region, then focus root. */
10817 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10819 DNPRINTF(SWM_D_EVENT
, "enternotify: "
10820 "NULL region; ignoring.\n");
10826 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
10831 if (e
->mode
== XCB_NOTIFY_MODE_NORMAL
&&
10832 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10833 DNPRINTF(SWM_D_EVENT
, "enternotify: entering from "
10834 "inferior; ignoring\n");
10838 focus_win(get_focus_magic(win
));
10841 DNPRINTF(SWM_D_EVENT
, "enternotify: done\n");
10848 leavenotify(xcb_leave_notify_event_t
*e
)
10850 last_event_time
= e
->time
;
10852 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): %#x "
10853 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10854 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10855 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10856 get_notify_mode_label(e
->mode
), e
->mode
,
10857 get_notify_detail_label(e
->detail
), e
->detail
,
10858 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10859 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10865 mapnotify(xcb_map_notify_event_t
*e
)
10867 struct ws_win
*win
, *parent
= NULL
;
10868 struct workspace
*ws
;
10870 DNPRINTF(SWM_D_EVENT
, "mapnotify: win %#x\n", e
->window
);
10872 if ((win
= manage_window(e
->window
, spawn_position
, false)) == NULL
)
10876 if (win
->state
== SWM_WIN_STATE_REPARENTING
)
10879 if (ws
->r
== NULL
) {
10884 /* Need to know if win was mapped due to ws switch. */
10885 if (ws
->state
== SWM_WS_STATE_MAPPED
) {
10886 if (ws
->focus_pending
&& TRANS(ws
->focus_pending
))
10887 parent
= find_window(win
->transient
);
10889 /* If window's parent is maximized, don't clear it. */
10890 if ((parent
== NULL
) || !MAXIMIZED(parent
))
10891 if (clear_maximized(ws
) > 0)
10895 win
->mapped
= true;
10896 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
10898 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
10899 if (ws
->focus_pending
== win
) {
10901 ws
->focus_pending
= NULL
;
10902 center_pointer(ws
->r
);
10908 DNPRINTF(SWM_D_EVENT
, "mapnotify: done\n");
10914 mappingnotify(xcb_mapping_notify_event_t
*e
)
10916 if (e
->request
!= XCB_MAPPING_POINTER
) {
10917 xcb_refresh_keyboard_mapping(syms
, e
);
10925 maprequest(xcb_map_request_event_t
*e
)
10927 struct ws_win
*win
, *w
= NULL
;
10929 DNPRINTF(SWM_D_EVENT
, "maprequest: win %#x\n",
10932 win
= manage_window(e
->window
, spawn_position
, true);
10936 /* The new window should get focus; prepare. */
10937 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10938 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
) && ACCEPTS_FOCUS(win
)) {
10939 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
10940 /* See if other wins of same type are already mapped. */
10941 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
10942 if (w
== win
|| !w
->mapped
)
10945 if (w
->ch
.class_name
&&
10946 win
->ch
.class_name
&&
10947 strcmp(w
->ch
.class_name
,
10948 win
->ch
.class_name
) == 0 &&
10949 w
->ch
.instance_name
&&
10950 win
->ch
.instance_name
&&
10951 strcmp(w
->ch
.instance_name
,
10952 win
->ch
.instance_name
) == 0)
10958 win
->ws
->focus_pending
= get_focus_magic(win
);
10961 /* All windows need to be mapped if they are in the current workspace.*/
10964 /* Ignore EnterNotify to handle the mapnotify without interference. */
10965 if (focus_mode
== SWM_FOCUS_DEFAULT
)
10966 event_drain(XCB_ENTER_NOTIFY
);
10968 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
10972 motionnotify(xcb_motion_notify_event_t
*e
)
10974 struct swm_region
*r
= NULL
;
10975 int i
, num_screens
;
10977 last_event_time
= e
->time
;
10979 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): %#x "
10980 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10981 "child: %#x, same_screen_focus: %s, state: %d\n",
10982 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10983 get_notify_detail_label(e
->detail
), e
->detail
,
10984 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10985 YESNO(e
->same_screen
), e
->state
);
10987 if (focus_mode
== SWM_FOCUS_MANUAL
)
10990 num_screens
= get_screen_count();
10991 for (i
= 0; i
< num_screens
; i
++)
10992 if (screens
[i
].root
== e
->root
)
10995 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10996 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
10997 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
11005 get_atom_name(xcb_atom_t atom
)
11008 #ifdef SWM_DEBUG_ATOM_NAMES
11010 * This should be disabled during most debugging since
11011 * xcb_get_* causes an xcb_flush.
11014 xcb_get_atom_name_reply_t
*r
;
11016 r
= xcb_get_atom_name_reply(conn
,
11017 xcb_get_atom_name(conn
, atom
),
11020 len
= xcb_get_atom_name_name_length(r
);
11022 name
= malloc(len
+ 1);
11024 memcpy(name
, xcb_get_atom_name_name(r
), len
);
11038 propertynotify(xcb_property_notify_event_t
*e
)
11040 struct ws_win
*win
;
11041 struct workspace
*ws
;
11045 name
= get_atom_name(e
->atom
);
11046 DNPRINTF(SWM_D_EVENT
, "propertynotify: win %#x, atom: %s(%u), "
11047 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
11051 last_event_time
= e
->time
;
11053 win
= find_window(e
->window
);
11059 if (e
->atom
== a_state
) {
11060 /* State just changed, make sure it gets focused if mapped. */
11061 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
11062 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
11064 win
->state
== SWM_WIN_STATE_REPARENTED
&&
11065 ws
->focus_pending
== win
) {
11066 focus_win(ws
->focus_pending
);
11067 ws
->focus_pending
= NULL
;
11071 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
11072 e
->atom
== XCB_ATOM_WM_NAME
) {
11074 bar_draw(ws
->r
->bar
);
11075 } else if (e
->atom
== a_prot
) {
11076 get_wm_protocols(win
);
11077 } else if (e
->atom
== XCB_ATOM_WM_NORMAL_HINTS
) {
11078 xcb_icccm_get_wm_normal_hints_reply(conn
,
11079 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
11087 reparentnotify(xcb_reparent_notify_event_t
*e
)
11089 struct ws_win
*win
;
11091 DNPRINTF(SWM_D_EVENT
, "reparentnotify: event: %#x, win %#x, "
11092 "parent: %#x, (x,y): (%u,%u), override_redirect: %u\n",
11093 e
->event
, e
->window
, e
->parent
, e
->x
, e
->y
, e
->override_redirect
);
11095 win
= find_window(e
->window
);
11097 if (win
->state
== SWM_WIN_STATE_REPARENTING
) {
11098 win
->state
= SWM_WIN_STATE_REPARENTED
;
11100 if (win
->ws
->r
&& !ICONIC(win
))
11105 update_window(win
);
11106 update_win_stacking(win
);
11107 } else if (win
->state
== SWM_WIN_STATE_UNPARENTING
) {
11108 win
->state
= SWM_WIN_STATE_UNPARENTED
;
11114 unmapnotify(xcb_unmap_notify_event_t
*e
)
11116 struct ws_win
*win
;
11117 struct workspace
*ws
;
11119 DNPRINTF(SWM_D_EVENT
, "unmapnotify: win %#x\n", e
->window
);
11121 /* If we aren't managing the window, then ignore. */
11122 win
= find_window(e
->window
);
11123 if (win
== NULL
|| win
->id
!= e
->window
) {
11124 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore unmanaged.\n");
11128 /* Do nothing if already withdrawn. */
11129 if (!win
->mapped
&& !ICONIC(win
)) {
11130 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore withdrawn.\n");
11135 win
->mapped
= false;
11137 /* Ignore if reparenting-related. */
11138 if (win
->state
!= SWM_WIN_STATE_REPARENTED
) {
11139 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore not reparented.\n");
11143 /* If win was focused, make sure to focus on something else. */
11144 if (win
== ws
->focus
) {
11145 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
11146 ws
->focus_pending
= get_focus_prev(win
);
11147 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
11148 "focus_pending: %#x\n",
11149 WINID(ws
->focus_pending
));
11157 DNPRINTF(SWM_D_EVENT
, "unmapnotify: iconify.\n");
11158 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
11161 DNPRINTF(SWM_D_EVENT
, "unmapnotify: withdraw.\n");
11162 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
11163 unmanage_window(win
);
11168 /* Update focus if ws is active. */
11169 if (WS_FOCUSED(ws
)) {
11170 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
11171 focus_win(get_pointer_win(ws
->r
->s
->root
));
11172 } else if (ws
->focus_pending
) {
11173 focus_win(ws
->focus_pending
);
11174 ws
->focus_pending
= NULL
;
11175 } else if (ws
->focus
== NULL
) {
11176 DNPRINTF(SWM_D_FOCUS
, "unmapnotify: set_input_focus: "
11177 "%#x, revert-to: parent, time: 0\n",
11179 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11180 ws
->r
->id
, XCB_CURRENT_TIME
);
11184 center_pointer(ws
->r
);
11186 DNPRINTF(SWM_D_EVENT
, "unmapnotify: done.\n");
11191 get_source_type_label(uint32_t type
)
11196 case EWMH_SOURCE_TYPE_NONE
:
11199 case EWMH_SOURCE_TYPE_NORMAL
:
11202 case EWMH_SOURCE_TYPE_OTHER
:
11214 clientmessage(xcb_client_message_event_t
*e
)
11216 struct ws_win
*win
;
11217 struct swm_region
*r
= NULL
;
11220 int num_screens
, i
;
11221 xcb_map_request_event_t mre
;
11225 name
= get_atom_name(e
->type
);
11226 DNPRINTF(SWM_D_EVENT
, "clientmessage: win %#x, atom: %s(%u)\n",
11227 e
->window
, name
, e
->type
);
11231 if (e
->type
== ewmh
[_NET_CURRENT_DESKTOP
].atom
) {
11232 num_screens
= get_screen_count();
11233 for (i
= 0; i
< num_screens
; i
++)
11234 if (screens
[i
].root
== e
->window
) {
11235 r
= screens
[i
].r_focus
;
11239 if (r
&& e
->data
.data32
[0] < (uint32_t)workspace_limit
) {
11240 a
.id
= e
->data
.data32
[0];
11241 switchws(NULL
, r
, &a
);
11246 } else if (e
->type
== ewmh
[_NET_REQUEST_FRAME_EXTENTS
].atom
) {
11247 DNPRINTF(SWM_D_EVENT
,
11248 "clientmessage: set _NET_FRAME_EXTENTS on window.\n");
11249 vals
[0] = vals
[1] = vals
[2] = vals
[3] = border_width
;
11250 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, e
->window
,
11251 a_net_frame_extents
, XCB_ATOM_CARDINAL
, 32, 4, vals
);
11256 win
= find_window(e
->window
);
11258 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11259 /* Manage the window with maprequest. */
11260 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
11261 "unmanaged window.\n");
11262 mre
.window
= e
->window
;
11268 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11269 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW, "
11270 "source_type: %s(%d)\n",
11271 get_source_type_label(e
->data
.data32
[0]),
11272 e
->data
.data32
[0]);
11275 * Allow focus changes that are a result of direct user
11276 * action and from applications that use the old EWMH spec.
11278 if (e
->data
.data32
[0] != EWMH_SOURCE_TYPE_NORMAL
||
11279 win
->quirks
& SWM_Q_OBEYAPPFOCUSREQ
) {
11280 if (WS_FOCUSED(win
->ws
))
11283 win
->ws
->focus_pending
= win
;
11285 } else if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
11286 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
11287 if (win
->can_delete
)
11288 client_msg(win
, a_delete
, 0);
11290 xcb_kill_client(conn
, win
->id
);
11291 } else if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
11292 DNPRINTF(SWM_D_EVENT
,
11293 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
11295 if (e
->data
.data32
[0] & (1<<8)) /* x */
11296 X(win
) = e
->data
.data32
[1];
11297 if (e
->data
.data32
[0] & (1<<9)) /* y */
11298 Y(win
) = e
->data
.data32
[2];
11299 if (e
->data
.data32
[0] & (1<<10)) /* width */
11300 WIDTH(win
) = e
->data
.data32
[3];
11301 if (e
->data
.data32
[0] & (1<<11)) /* height */
11302 HEIGHT(win
) = e
->data
.data32
[4];
11304 update_window(win
);
11306 /* Notify no change was made. */
11307 config_win(win
, NULL
);
11308 /* TODO: Change stack sizes */
11310 } else if (e
->type
== ewmh
[_NET_RESTACK_WINDOW
].atom
) {
11311 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_RESTACK_WINDOW\n");
11312 vals
[0] = e
->data
.data32
[1]; /* Sibling window. */
11313 vals
[1] = e
->data
.data32
[2]; /* Stack mode detail. */
11315 if (win
->frame
!= XCB_WINDOW_NONE
)
11316 xcb_configure_window(conn
, win
->frame
,
11317 XCB_CONFIG_WINDOW_SIBLING
|
11318 XCB_CONFIG_WINDOW_STACK_MODE
, vals
);
11319 } else if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
11320 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
11321 ewmh_change_wm_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
11322 if (e
->data
.data32
[2])
11323 ewmh_change_wm_state(win
, e
->data
.data32
[2],
11324 e
->data
.data32
[0]);
11326 ewmh_update_wm_state(win
);
11328 } else if (e
->type
== ewmh
[_NET_WM_DESKTOP
].atom
) {
11329 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_DESKTOP\n");
11332 win_to_ws(win
, e
->data
.data32
[0], true);
11334 /* Stack source and destination ws, if mapped. */
11335 if (r
!= win
->ws
->r
) {
11341 load_float_geom(win
);
11354 int errcode
= xcb_connection_has_error(conn
);
11355 #ifdef XCB_CONN_ERROR
11358 case XCB_CONN_ERROR
:
11359 s
= "Socket error, pipe error or other stream error.";
11361 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
11362 s
= "Extension not supported.";
11364 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
11365 s
= "Insufficient memory.";
11367 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
11368 s
= "Request length was exceeded.";
11370 case XCB_CONN_CLOSED_PARSE_ERR
:
11371 s
= "Error parsing display string.";
11374 s
= "Unknown error.";
11377 errx(errcode
, "X CONNECTION ERROR: %s", s
);
11380 errx(errcode
, "X CONNECTION ERROR");
11387 int num_screens
, i
;
11388 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
11389 XCB_EVENT_MASK_ENTER_WINDOW
|
11390 XCB_EVENT_MASK_OWNER_GRAB_BUTTON
|
11391 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
11392 XCB_EVENT_MASK_ENTER_WINDOW
|
11393 XCB_EVENT_MASK_LEAVE_WINDOW
|
11394 XCB_EVENT_MASK_BUTTON_PRESS
|
11395 XCB_EVENT_MASK_BUTTON_RELEASE
|
11396 XCB_EVENT_MASK_KEY_PRESS
|
11397 XCB_EVENT_MASK_KEY_RELEASE
|
11398 XCB_EVENT_MASK_PROPERTY_CHANGE
;
11400 xcb_void_cookie_t wac
;
11401 xcb_generic_error_t
*error
;
11403 /* this causes an error if some other window manager is running */
11404 num_screens
= get_screen_count();
11405 for (i
= 0; i
< num_screens
; i
++) {
11406 if ((sc
= get_screen(i
)) == NULL
)
11407 errx(1, "ERROR: can't get screen %d.", i
);
11408 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: %#x\n",
11410 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
11411 XCB_CW_EVENT_MASK
, &val
);
11412 if ((error
= xcb_request_check(conn
, wac
))) {
11413 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
11414 error
->error_code
);
11424 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
11426 struct swm_region
*r
= NULL
, *n
;
11427 struct workspace
*ws
= NULL
;
11431 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
11432 s
->idx
, w
, h
, x
, y
);
11434 /* remove any conflicting regions */
11435 n
= TAILQ_FIRST(&s
->rl
);
11438 n
= TAILQ_NEXT(r
, entry
);
11439 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
11440 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
11441 if (r
->ws
->r
!= NULL
)
11442 r
->ws
->old_r
= r
->ws
->r
;
11445 xcb_destroy_window(conn
, r
->id
);
11446 r
->id
= XCB_WINDOW_NONE
;
11447 TAILQ_REMOVE(&s
->rl
, r
, entry
);
11448 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
11452 /* search old regions for one to reuse */
11454 /* size + location match */
11455 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11456 if (X(r
) == x
&& Y(r
) == y
&&
11457 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11461 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11462 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11466 TAILQ_REMOVE(&s
->orl
, r
, entry
);
11467 /* try to use old region's workspace */
11468 if (r
->ws
->r
== NULL
)
11471 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
11472 err(1, "new_region: calloc: failed to allocate memory "
11475 /* if we don't have a workspace already, find one */
11477 for (i
= 0; i
< workspace_limit
; i
++)
11478 if (s
->ws
[i
].r
== NULL
) {
11485 errx(1, "new_region: no free workspaces");
11487 if (ws
->state
== SWM_WS_STATE_HIDDEN
)
11488 ws
->state
= SWM_WS_STATE_MAPPING
;
11497 r
->ws_prior
= NULL
;
11500 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
11502 /* Invisible region window to detect pointer events on empty regions. */
11503 r
->id
= xcb_generate_id(conn
);
11504 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
11505 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
11507 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
11508 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
11509 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
11511 /* Make sure region input is at the bottom. */
11512 wa
[0] = XCB_STACK_MODE_BELOW
;
11513 xcb_configure_window(conn
, r
->id
, XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
11515 xcb_map_window(conn
, r
->id
);
11519 scan_randr(int idx
)
11521 #ifdef SWM_XRR_HAS_CRTC
11524 #endif /* SWM_XRR_HAS_CRTC */
11525 struct swm_region
*r
;
11527 xcb_randr_get_screen_resources_current_cookie_t src
;
11528 xcb_randr_get_screen_resources_current_reply_t
*srr
;
11529 xcb_randr_get_crtc_info_cookie_t cic
;
11530 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
11531 xcb_randr_crtc_t
*crtc
;
11532 xcb_screen_t
*screen
;
11534 DNPRINTF(SWM_D_MISC
, "scan_randr: screen: %d\n", idx
);
11536 if ((screen
= get_screen(idx
)) == NULL
)
11537 errx(1, "ERROR: can't get screen %d.", idx
);
11539 num_screens
= get_screen_count();
11540 if (idx
>= num_screens
)
11541 errx(1, "scan_randr: invalid screen");
11543 /* remove any old regions */
11544 while ((r
= TAILQ_FIRST(&screens
[idx
].rl
)) != NULL
) {
11545 r
->ws
->old_r
= r
->ws
->r
= NULL
;
11547 xcb_destroy_window(conn
, r
->id
);
11548 r
->id
= XCB_WINDOW_NONE
;
11549 TAILQ_REMOVE(&screens
[idx
].rl
, r
, entry
);
11550 TAILQ_INSERT_TAIL(&screens
[idx
].orl
, r
, entry
);
11554 /* map virtual screens onto physical screens */
11555 #ifdef SWM_XRR_HAS_CRTC
11556 if (randr_support
) {
11557 src
= xcb_randr_get_screen_resources_current(conn
,
11558 screens
[idx
].root
);
11559 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
11562 new_region(&screens
[idx
], 0, 0,
11563 screen
->width_in_pixels
,
11564 screen
->height_in_pixels
);
11567 ncrtc
= srr
->num_crtcs
;
11569 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
11570 for (c
= 0; c
< ncrtc
; c
++) {
11571 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
11573 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
11576 if (cir
->num_outputs
== 0) {
11581 if (cir
->mode
== 0)
11582 new_region(&screens
[idx
], 0, 0,
11583 screen
->width_in_pixels
,
11584 screen
->height_in_pixels
);
11586 new_region(&screens
[idx
],
11587 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
11592 #endif /* SWM_XRR_HAS_CRTC */
11594 /* If detection failed, create a single region that spans the screen. */
11595 if (TAILQ_EMPTY(&screens
[idx
].rl
))
11596 new_region(&screens
[idx
], 0, 0, screen
->width_in_pixels
,
11597 screen
->height_in_pixels
);
11600 /* The screen shouldn't focus on unused regions. */
11601 TAILQ_FOREACH(r
, &screens
[idx
].orl
, entry
) {
11602 if (screens
[idx
].r_focus
== r
)
11603 screens
[idx
].r_focus
= NULL
;
11606 DNPRINTF(SWM_D_MISC
, "scan_randr: done.\n");
11610 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
11612 struct swm_region
*r
;
11613 struct workspace
*ws
;
11614 struct ws_win
*win
;
11615 int i
, j
, num_screens
;
11617 DNPRINTF(SWM_D_EVENT
, "screenchange: root: %#x\n", e
->root
);
11619 num_screens
= get_screen_count();
11620 /* silly event doesn't include the screen index */
11621 for (i
= 0; i
< num_screens
; i
++)
11622 if (screens
[i
].root
== e
->root
)
11624 if (i
>= num_screens
)
11625 errx(1, "screenchange: screen not found");
11627 /* brute force for now, just re-enumerate the regions */
11631 print_win_geom(e
->root
);
11633 /* add bars to all regions */
11634 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11637 /* Stack all regions. */
11638 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11641 /* Make sure a region has focus. */
11642 if (screens
[i
].r_focus
== NULL
) {
11643 r
= TAILQ_FIRST(&screens
[i
].rl
);
11648 /* Cleanup unused previously visible workspaces. */
11649 for (j
= 0; j
< workspace_limit
; j
++) {
11650 ws
= &screens
[i
].ws
[j
];
11651 if (ws
->r
== NULL
&& ws
->state
!= SWM_WS_STATE_HIDDEN
) {
11652 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
11654 ws
->state
= SWM_WS_STATE_HIDDEN
;
11660 /* Update workspace state and bar on all regions. */
11661 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11662 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
11670 struct swm_region
*r
= NULL
;
11671 xcb_query_tree_cookie_t qtc
;
11672 xcb_query_tree_reply_t
*qtr
;
11673 xcb_get_property_cookie_t pc
;
11674 xcb_get_property_reply_t
*pr
;
11675 xcb_window_t
*wins
= NULL
, trans
, *cwins
= NULL
;
11676 int i
, j
, k
, n
, no
, num_screens
;
11678 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
11679 num_screens
= get_screen_count();
11680 for (i
= 0; i
< num_screens
; i
++) {
11681 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
11682 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
11685 wins
= xcb_query_tree_children(qtr
);
11686 no
= xcb_query_tree_children_length(qtr
);
11688 /* Try to sort windows according to _NET_CLIENT_LIST. */
11689 pr
= xcb_get_property_reply(conn
, xcb_get_property(conn
, 0,
11690 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
11691 XCB_ATOM_WINDOW
, 0, UINT32_MAX
), NULL
);
11693 cwins
= xcb_get_property_value(pr
);
11694 n
= xcb_get_property_value_length(pr
) /
11695 sizeof(xcb_atom_t
);
11697 for (j
= 0; j
< n
; ++j
) {
11698 for (k
= j
; k
< no
; ++k
) {
11699 if (wins
[k
] == cwins
[j
]) {
11700 /* Swap wins j and k. */
11702 wins
[j
] = cwins
[j
];
11710 /* Manage top-level windows first, then transients. */
11711 /* TODO: allow transients to be managed before leader. */
11712 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top-level windows.\n");
11713 for (j
= 0; j
< no
; j
++) {
11714 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11715 if (r
->id
== wins
[j
]) {
11716 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11717 "skip %#x; region input window.\n",
11720 } else if (r
->bar
->id
== wins
[j
]) {
11721 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11722 "skip %#x; region bar.\n",
11731 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11732 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11734 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
11735 "is transient for %#x.\n", wins
[j
], trans
);
11739 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11742 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
11743 for (j
= 0; j
< no
; j
++) {
11744 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11745 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11747 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11751 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
11755 setup_screens(void)
11757 int i
, j
, k
, num_screens
;
11758 struct workspace
*ws
;
11759 uint32_t gcv
[1], wa
[1];
11760 const xcb_query_extension_reply_t
*qep
;
11761 xcb_screen_t
*screen
;
11762 xcb_randr_query_version_cookie_t c
;
11763 xcb_randr_query_version_reply_t
*r
;
11765 num_screens
= get_screen_count();
11766 if ((screens
= calloc(num_screens
, sizeof(struct swm_screen
))) == NULL
)
11767 err(1, "setup_screens: calloc: failed to allocate memory for "
11770 /* Initial RandR setup. */
11771 randr_support
= false;
11772 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
11773 if (qep
->present
) {
11774 c
= xcb_randr_query_version(conn
, 1, 1);
11775 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
11777 if (r
->major_version
>= 1) {
11778 randr_support
= true;
11779 randr_eventbase
= qep
->first_event
;
11785 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
11787 /* map physical screens */
11788 for (i
= 0; i
< num_screens
; i
++) {
11789 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
11790 screens
[i
].idx
= i
;
11791 screens
[i
].r_focus
= NULL
;
11793 TAILQ_INIT(&screens
[i
].rl
);
11794 TAILQ_INIT(&screens
[i
].orl
);
11795 if ((screen
= get_screen(i
)) == NULL
)
11796 errx(1, "ERROR: can't get screen %d.", i
);
11797 screens
[i
].root
= screen
->root
;
11799 /* set default colors */
11800 setscreencolor("red", i
, SWM_S_COLOR_FOCUS
);
11801 setscreencolor("rgb:88/88/88", i
, SWM_S_COLOR_UNFOCUS
);
11802 setscreencolor("rgb:00/80/80", i
, SWM_S_COLOR_BAR_BORDER
);
11803 setscreencolor("rgb:00/40/40", i
,
11804 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
11805 setscreencolor("black", i
, SWM_S_COLOR_BAR
);
11806 setscreencolor("rgb:a0/a0/a0", i
, SWM_S_COLOR_BAR_FONT
);
11807 setscreencolor("red", i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
11808 setscreencolor("rgb:88/88/88", i
,
11809 SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
11811 /* create graphics context on screen */
11812 screens
[i
].bar_gc
= xcb_generate_id(conn
);
11814 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
11815 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
11817 /* set default cursor */
11818 xcb_change_window_attributes(conn
, screens
[i
].root
,
11819 XCB_CW_CURSOR
, wa
);
11821 /* init all workspaces */
11822 /* XXX these should be dynamically allocated too */
11823 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
11824 ws
= &screens
[i
].ws
[j
];
11827 ws
->bar_enabled
= true;
11829 ws
->focus_prev
= NULL
;
11830 ws
->focus_pending
= NULL
;
11831 ws
->focus_raise
= NULL
;
11834 ws
->state
= SWM_WS_STATE_HIDDEN
;
11835 TAILQ_INIT(&ws
->stack
);
11836 TAILQ_INIT(&ws
->winlist
);
11837 TAILQ_INIT(&ws
->unmanagedlist
);
11839 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
11840 if (layouts
[k
].l_config
!= NULL
)
11841 layouts
[k
].l_config(ws
,
11842 SWM_ARG_ID_STACKINIT
);
11843 ws
->cur_layout
= &layouts
[0];
11844 ws
->cur_layout
->l_string(ws
);
11850 xcb_randr_select_input(conn
, screens
[i
].root
,
11851 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
11856 setup_globals(void)
11858 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
11859 err(1, "setup_globals: strdup: failed to allocate memory.");
11861 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
11862 err(1, "setup_globals: strdup: failed to allocate memory.");
11864 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
11865 errx(1, "unable to allocate key symbols");
11867 a_state
= get_atom_from_string("WM_STATE");
11868 a_prot
= get_atom_from_string("WM_PROTOCOLS");
11869 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
11870 a_net_frame_extents
= get_atom_from_string("_NET_FRAME_EXTENTS");
11871 a_net_supported
= get_atom_from_string("_NET_SUPPORTED");
11872 a_net_wm_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
11873 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
11874 a_utf8_string
= get_atom_from_string("UTF8_STRING");
11875 a_swm_ws
= get_atom_from_string("_SWM_WS");
11879 shutdown_cleanup(void)
11881 struct swm_region
*r
;
11883 struct workspace
*ws
;
11884 int i
, num_screens
;
11886 /* disable alarm because the following code may not be interrupted */
11888 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
11889 err(1, "can't disable alarm");
11902 num_screens
= get_screen_count();
11903 for (i
= 0; i
< num_screens
; ++i
) {
11906 DNPRINTF(SWM_D_FOCUS
, "shutdown_cleanup: set_input_focus: "
11907 "%#x, revert-to: root, time: 0\n", screens
[i
].root
);
11908 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11909 screens
[i
].root
, XCB_CURRENT_TIME
);
11911 if (screens
[i
].bar_gc
!= XCB_NONE
)
11912 xcb_free_gc(conn
, screens
[i
].bar_gc
);
11913 if (!bar_font_legacy
) {
11914 XftColorFree(display
, DefaultVisual(display
, i
),
11915 DefaultColormap(display
, i
), &bar_font_color
);
11916 XftColorFree(display
, DefaultVisual(display
, i
),
11917 DefaultColormap(display
, i
), &search_font_color
);
11920 for (j
= 0; j
< SWM_S_COLOR_MAX
; ++j
) {
11921 free(screens
[i
].c
[j
].name
);
11924 /* Free window memory. */
11925 for (j
= 0; j
< SWM_WS_MAX
; ++j
) {
11926 ws
= &screens
[i
].ws
[j
];
11929 while ((w
= TAILQ_FIRST(&ws
->winlist
)) != NULL
) {
11930 TAILQ_REMOVE(&ws
->winlist
, w
, entry
);
11934 while ((w
= TAILQ_FIRST(&ws
->unmanagedlist
)) != NULL
) {
11935 TAILQ_REMOVE(&ws
->unmanagedlist
, w
, entry
);
11940 /* Free region memory. */
11941 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
11942 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
11947 while ((r
= TAILQ_FIRST(&screens
[i
].orl
)) != NULL
) {
11948 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
11957 free(clock_format
);
11958 free(startup_exception
);
11961 XFreeFontSet(display
, bar_fs
);
11963 XftFontClose(display
, bar_font
);
11965 xcb_key_symbols_free(syms
);
11967 xcb_aux_sync(conn
);
11968 xcb_disconnect(conn
);
11972 event_error(xcb_generic_error_t
*e
)
11976 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
11977 "resource_id: %u, minor_code: %u\n",
11978 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
11979 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
11980 e
->sequence
, e
->resource_id
, e
->minor_code
);
11984 event_handle(xcb_generic_event_t
*evt
)
11986 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
11988 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
11989 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
11990 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
11993 #define EVENT(type, callback) case type: callback((void *)evt); return
11994 EVENT(0, event_error
);
11995 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
11996 EVENT(XCB_BUTTON_RELEASE
, buttonrelease
);
11997 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
11998 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
11999 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
12000 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
12001 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
12002 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
12003 /*EVENT(XCB_CREATE_NOTIFY, );*/
12004 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
12005 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
12006 EVENT(XCB_EXPOSE
, expose
);
12007 EVENT(XCB_FOCUS_IN
, focusin
);
12009 EVENT(XCB_FOCUS_OUT
, focusout
);
12011 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
12012 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
12013 EVENT(XCB_KEY_PRESS
, keypress
);
12014 EVENT(XCB_KEY_RELEASE
, keyrelease
);
12015 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
12017 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
12019 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
12020 EVENT(XCB_MAP_REQUEST
, maprequest
);
12021 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
12022 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
12023 /*EVENT(XCB_NO_EXPOSURE, );*/
12024 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
12025 EVENT(XCB_REPARENT_NOTIFY
, reparentnotify
);
12026 /*EVENT(XCB_RESIZE_REQUEST, );*/
12027 /*EVENT(XCB_SELECTION_CLEAR, );*/
12028 /*EVENT(XCB_SELECTION_NOTIFY, );*/
12029 /*EVENT(XCB_SELECTION_REQUEST, );*/
12030 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
12031 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
12034 if (type
- randr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
12035 screenchange((void *)evt
);
12039 main(int argc
, char *argv
[])
12041 struct pollfd pfd
[2];
12042 struct sigaction sact
;
12044 struct passwd
*pwd
;
12045 struct swm_region
*r
;
12046 xcb_generic_event_t
*evt
;
12047 int xfd
, i
, num_screens
, num_readable
;
12048 char conf
[PATH_MAX
], *cfile
= NULL
;
12049 bool stdin_ready
= false, startup
= true;
12051 /* suppress unused warning since var is needed */
12055 time_started
= time(NULL
);
12059 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
12060 if (setlocale(LC_CTYPE
, "") == NULL
|| setlocale(LC_TIME
, "") == NULL
)
12061 warnx("no locale support");
12063 /* handle some signals */
12064 bzero(&sact
, sizeof(sact
));
12065 sigemptyset(&sact
.sa_mask
);
12067 sact
.sa_handler
= sighdlr
;
12068 sigaction(SIGINT
, &sact
, NULL
);
12069 sigaction(SIGQUIT
, &sact
, NULL
);
12070 sigaction(SIGTERM
, &sact
, NULL
);
12071 sigaction(SIGHUP
, &sact
, NULL
);
12073 sact
.sa_handler
= sighdlr
;
12074 sact
.sa_flags
= SA_NOCLDSTOP
;
12075 sigaction(SIGCHLD
, &sact
, NULL
);
12077 if ((display
= XOpenDisplay(0)) == NULL
)
12078 errx(1, "can not open display");
12080 conn
= XGetXCBConnection(display
);
12081 if (xcb_connection_has_error(conn
))
12082 errx(1, "can not get XCB connection");
12084 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
12086 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
12087 xfd
= xcb_get_file_descriptor(conn
);
12089 /* look for local and global conf file */
12090 pwd
= getpwuid(getuid());
12092 errx(1, "invalid user: %d", getuid());
12094 xcb_grab_server(conn
);
12095 xcb_aux_sync(conn
);
12097 /* flush all events */
12098 while ((evt
= get_next_event(false))) {
12099 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
12105 errx(1, "another window manager is currently running");
12107 /* Load Xcursors and/or cursorfont glyph cursors. */
12110 xcb_aux_sync(conn
);
12115 setup_keybindings();
12116 setup_btnbindings();
12121 for (i
= 0; ; i
++) {
12126 snprintf(conf
, sizeof conf
, "%s/.%s",
12127 pwd
->pw_dir
, SWM_CONF_FILE
);
12131 snprintf(conf
, sizeof conf
, "/etc/%s",
12136 snprintf(conf
, sizeof conf
, "%s/.%s",
12137 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
12140 /* global compat */
12141 snprintf(conf
, sizeof conf
, "/etc/%s",
12142 SWM_CONF_FILE_OLD
);
12148 if (strlen(conf
) && stat(conf
, &sb
) != -1)
12149 if (S_ISREG(sb
.st_mode
)) {
12156 /* load conf (if any) */
12158 conf_load(cfile
, SWM_CONF_DEFAULT
);
12162 if (getenv("SWM_STARTED") == NULL
)
12163 setenv("SWM_STARTED", "YES", 1);
12165 /* setup all bars */
12166 num_screens
= get_screen_count();
12167 for (i
= 0; i
< num_screens
; i
++)
12168 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12171 /* Manage existing windows. */
12177 /* Stack all regions to trigger mapping. */
12178 for (i
= 0; i
< num_screens
; i
++)
12179 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12182 xcb_ungrab_server(conn
);
12185 /* Update state and bar of each newly mapped workspace. */
12186 for (i
= 0; i
< num_screens
; i
++)
12187 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
12188 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
12192 memset(&pfd
, 0, sizeof(pfd
));
12194 pfd
[0].events
= POLLIN
;
12195 pfd
[1].fd
= STDIN_FILENO
;
12196 pfd
[1].events
= POLLIN
;
12199 while ((evt
= get_next_event(false))) {
12206 /* If just (re)started, set default focus if needed. */
12210 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
12211 r
= TAILQ_FIRST(&screens
[0].rl
);
12223 num_readable
= poll(pfd
, bar_extra
? 2 : 1, 1000);
12224 if (num_readable
== -1) {
12225 DNPRINTF(SWM_D_MISC
, "poll failed: %s",
12227 } else if (num_readable
> 0 && bar_extra
&&
12228 pfd
[1].revents
& POLLIN
) {
12229 stdin_ready
= true;
12233 restart(NULL
, NULL
, NULL
);
12239 stdin_ready
= false;
12240 bar_extra_update();
12243 /* Need to ensure the bar(s) are always updated. */
12244 for (i
= 0; i
< num_screens
; i
++)
12245 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12251 shutdown_cleanup();