2 * Copyright (c) 2009-2015 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2009-2011 Ryan McBride <mcbride@countersiege.com>
4 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
5 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
6 * Copyright (c) 2010 Tuukka Kataja <stuge@xor.fi>
7 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 * Copyright (c) 2011-2016 Reginald Kennedy <rk@rejii.com>
9 * Copyright (c) 2011-2012 Lawrence Teo <lteo@lteo.net>
10 * Copyright (c) 2011-2012 Tiago Cunha <tcunha@gmx.com>
11 * Copyright (c) 2012-2015 David Hill <dhill@mindcry.org>
12 * Copyright (c) 2014-2015 Yuri D'Elia <yuri.delia@eurac.edu>
14 * Permission to use, copy, modify, and distribute this software for any
15 * purpose with or without fee is hereby granted, provided that the above
16 * copyright notice and this permission notice appear in all copies.
18 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
19 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
21 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
23 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
24 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 #include <sys/types.h>
35 #include <sys/queue.h>
37 #include <sys/param.h>
38 #include <sys/select.h>
39 #if defined(__linux__)
41 #elif defined(__OpenBSD__)
43 #elif defined(__FreeBSD__)
67 #include <X11/cursorfont.h>
68 #include <X11/extensions/Xrandr.h>
69 #include <X11/Xcursor/Xcursor.h>
70 #include <X11/Xft/Xft.h>
71 #include <X11/Xlib-xcb.h>
72 #include <xcb/xcb_atom.h>
73 #include <xcb/xcb_aux.h>
74 #include <xcb/xcb_event.h>
75 #include <xcb/xcb_icccm.h>
76 #include <xcb/xcb_keysyms.h>
77 #include <xcb/xtest.h>
78 #include <xcb/randr.h>
86 #ifdef SPECTRWM_BUILDSTR
87 static const char *buildstr
= SPECTRWM_BUILDSTR
;
89 static const char *buildstr
= SPECTRWM_VERSION
;
92 #if !defined(__CYGWIN__) /* cygwin chokes on randr stuff */
94 # error RandR versions less than 1.0 are not supported
99 # define SWM_XRR_HAS_CRTC
102 #endif /* __CYGWIN__ */
104 #ifndef XCB_ICCCM_NUM_WM_HINTS_ELEMENTS
105 #define XCB_ICCCM_SIZE_HINT_P_MIN_SIZE XCB_SIZE_HINT_P_MIN_SIZE
106 #define XCB_ICCCM_SIZE_HINT_P_MAX_SIZE XCB_SIZE_HINT_P_MAX_SIZE
107 #define XCB_ICCCM_SIZE_HINT_P_RESIZE_INC XCB_SIZE_HINT_P_RESIZE_INC
108 #define XCB_ICCCM_WM_HINT_INPUT XCB_WM_HINT_INPUT
109 #define XCB_ICCCM_WM_HINT_X_URGENCY XCB_WM_HINT_X_URGENCY
110 #define XCB_ICCCM_WM_STATE_ICONIC XCB_WM_STATE_ICONIC
111 #define XCB_ICCCM_WM_STATE_WITHDRAWN XCB_WM_STATE_WITHDRAWN
112 #define XCB_ICCCM_WM_STATE_NORMAL XCB_WM_STATE_NORMAL
113 #define xcb_icccm_get_text_property_reply_t xcb_get_text_property_reply_t
114 #define xcb_icccm_get_text_property_reply_wipe xcb_get_text_property_reply_wipe
115 #define xcb_icccm_get_wm_class xcb_get_wm_class
116 #define xcb_icccm_get_wm_class_reply xcb_get_wm_class_reply
117 #define xcb_icccm_get_wm_class_reply_t xcb_get_wm_class_reply_t
118 #define xcb_icccm_get_wm_class_reply_wipe xcb_get_wm_class_reply_wipe
119 #define xcb_icccm_get_wm_hints xcb_get_wm_hints
120 #define xcb_icccm_wm_hints_get_urgency xcb_wm_hints_get_urgency
121 #define xcb_icccm_get_wm_hints_reply xcb_get_wm_hints_reply
122 #define xcb_icccm_get_wm_name xcb_get_wm_name
123 #define xcb_icccm_get_wm_name_reply xcb_get_wm_name_reply
124 #define xcb_icccm_get_wm_normal_hints xcb_get_wm_normal_hints
125 #define xcb_icccm_get_wm_normal_hints_reply xcb_get_wm_normal_hints_reply
126 #define xcb_icccm_get_wm_protocols xcb_get_wm_protocols
127 #define xcb_icccm_get_wm_protocols_reply xcb_get_wm_protocols_reply
128 #define xcb_icccm_get_wm_protocols_reply_t xcb_get_wm_protocols_reply_t
129 #define xcb_icccm_get_wm_protocols_reply_wipe xcb_get_wm_protocols_reply_wipe
130 #define xcb_icccm_get_wm_transient_for xcb_get_wm_transient_for
131 #define xcb_icccm_get_wm_transient_for_reply xcb_get_wm_transient_for_reply
132 #define xcb_icccm_wm_hints_t xcb_wm_hints_t
135 /*#define SWM_DEBUG*/
137 #define DPRINTF(x...) do { \
139 fprintf(stderr, x); \
141 #define DNPRINTF(n,x...) do { \
142 if (swm_debug & n) { \
143 fprintf(stderr, "%ld ", (long)(time(NULL) - time_started)); \
144 fprintf(stderr, x); \
147 #define SWM_D_MISC 0x0001
148 #define SWM_D_EVENT 0x0002
149 #define SWM_D_WS 0x0004
150 #define SWM_D_FOCUS 0x0008
151 #define SWM_D_MOVE 0x0010
152 #define SWM_D_STACK 0x0020
153 #define SWM_D_MOUSE 0x0040
154 #define SWM_D_PROP 0x0080
155 #define SWM_D_CLASS 0x0100
156 #define SWM_D_KEY 0x0200
157 #define SWM_D_QUIRK 0x0400
158 #define SWM_D_SPAWN 0x0800
159 #define SWM_D_EVENTQ 0x1000
160 #define SWM_D_CONF 0x2000
161 #define SWM_D_BAR 0x4000
162 #define SWM_D_INIT 0x8000
164 uint32_t swm_debug
= 0
183 #define DPRINTF(x...)
184 #define DNPRINTF(n,x...)
187 #define SWM_EWMH_ACTION_COUNT_MAX (8)
188 #define EWMH_F_FULLSCREEN (0x001)
189 #define EWMH_F_ABOVE (0x002)
190 #define EWMH_F_HIDDEN (0x004)
191 #define EWMH_F_MAXIMIZED_VERT (0x008)
192 #define EWMH_F_MAXIMIZED_HORZ (0x010)
193 #define EWMH_F_SKIP_PAGER (0x020)
194 #define EWMH_F_SKIP_TASKBAR (0x040)
195 #define SWM_F_MANUAL (0x080)
197 #define EWMH_F_MAXIMIZED (EWMH_F_MAXIMIZED_VERT | EWMH_F_MAXIMIZED_HORZ)
199 /* convert 8-bit to 16-bit */
200 #define RGB_8_TO_16(col) (((col) << 8) + (col))
202 #define PIXEL_TO_XRENDERCOLOR(px, xrc) \
203 xrc.red = RGB_8_TO_16((px) >> 16 & 0xff); \
204 xrc.green = RGB_8_TO_16((px) >> 8 & 0xff); \
205 xrc.blue = RGB_8_TO_16((px) & 0xff); \
208 #define LENGTH(x) (int)(sizeof (x) / sizeof (x)[0])
209 #define MODKEY XCB_MOD_MASK_1
210 #define ANYMOD XCB_MOD_MASK_ANY
211 #define CLEANMASK(mask) ((mask) & (XCB_KEY_BUT_MASK_SHIFT | \
212 XCB_KEY_BUT_MASK_CONTROL | XCB_KEY_BUT_MASK_MOD_1 | \
213 XCB_KEY_BUT_MASK_MOD_2 | XCB_KEY_BUT_MASK_MOD_3 | \
214 XCB_KEY_BUT_MASK_MOD_4 | XCB_KEY_BUT_MASK_MOD_5) & ~(numlockmask))
215 #define BUTTONMASK (XCB_EVENT_MASK_BUTTON_PRESS | \
216 XCB_EVENT_MASK_BUTTON_RELEASE)
217 #define MOUSEMASK (BUTTONMASK|XCB_EVENT_MASK_POINTER_MOTION)
218 #define SWM_PROPLEN (16)
219 #define SWM_FUNCNAME_LEN (32)
220 #define SWM_KEYS_LEN (255)
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_NONE (0)
269 #define SWM_CK_ALL (0xf)
270 #define SWM_CK_FOCUS (0x1)
271 #define SWM_CK_POINTER (0x2)
272 #define SWM_CK_FALLBACK (0x4)
273 #define SWM_CK_REGION (0x8)
275 #define SWM_G_ALL (0xf)
276 #define SWM_G_SIZE (0x1)
277 #define SWM_G_POS (0x2)
279 #define SWM_CONF_DEFAULT (0)
280 #define SWM_CONF_KEYMAPPING (1)
283 #define SWM_LIB "/usr/local/lib/libswmhack.so"
290 xcb_atom_t a_net_frame_extents
;
291 xcb_atom_t a_net_wm_check
;
292 xcb_atom_t a_net_supported
;
293 xcb_atom_t a_takefocus
;
294 xcb_atom_t a_utf8_string
;
296 volatile sig_atomic_t running
= 1;
297 volatile sig_atomic_t restart_wm
= 0;
298 xcb_timestamp_t last_event_time
= 0;
302 unsigned int numlockmask
= 0;
305 xcb_connection_t
*conn
;
306 xcb_key_symbols_t
*syms
;
308 int boundary_width
= 50;
309 bool cycle_empty
= false;
310 bool cycle_visible
= false;
312 int font_adjusted
= 0;
313 uint16_t mod_key
= MODKEY
;
314 bool warp_focus
= false;
315 bool warp_pointer
= false;
316 bool workspace_clamp
= false;
319 struct swm_region
*search_r
;
320 int select_list_pipe
[2];
321 int select_resp_pipe
[2];
323 volatile sig_atomic_t search_resp
;
324 int search_resp_action
;
326 struct search_window
{
327 TAILQ_ENTRY(search_window
) entry
;
331 xcb_window_t indicator
;
333 TAILQ_HEAD(search_winlist
, search_window
);
334 struct search_winlist search_wl
;
339 SWM_SEARCH_UNICONIFY
,
340 SWM_SEARCH_NAME_WORKSPACE
,
341 SWM_SEARCH_SEARCH_WORKSPACE
,
342 SWM_SEARCH_SEARCH_WINDOW
345 #define SWM_STACK_TOP (0)
346 #define SWM_STACK_BOTTOM (1)
347 #define SWM_STACK_ABOVE (2)
348 #define SWM_STACK_BELOW (3)
351 double dialog_ratio
= 0.6;
353 #define SWM_BAR_MAX (256)
354 #define SWM_BAR_JUSTIFY_LEFT (0)
355 #define SWM_BAR_JUSTIFY_CENTER (1)
356 #define SWM_BAR_JUSTIFY_RIGHT (2)
357 #define SWM_BAR_OFFSET (4)
358 #define SWM_BAR_FONTS "-*-terminus-medium-*-*-*-12-*-*-*-*-*-*-*," \
359 "-*-profont-*-*-*-*-12-*-*-*-*-*-*-*," \
360 "-*-times-medium-r-*-*-12-*-*-*-*-*-*-*," \
361 "-misc-fixed-medium-r-*-*-12-*-*-*-*-*-*-*," \
362 "-*-*-*-r-*-*-*-*-*-*-*-*-*-*"
364 #ifdef X_HAVE_UTF8_STRING
365 #define DRAWSTRING(x...) Xutf8DrawString(x)
367 #define DRAWSTRING(x...) XmbDrawString(x)
370 char *bar_argv
[] = { NULL
, NULL
};
372 char bar_ext
[SWM_BAR_MAX
];
373 char bar_ext_buf
[SWM_BAR_MAX
];
374 char bar_vertext
[SWM_BAR_MAX
];
375 bool bar_version
= false;
376 bool bar_enabled
= true;
377 int bar_border_width
= 1;
378 bool bar_at_bottom
= false;
379 bool bar_extra
= false;
381 int bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
382 char *bar_format
= NULL
;
383 bool stack_enabled
= true;
384 bool clock_enabled
= true;
385 bool iconic_enabled
= false;
386 bool maximize_hide_bar
= false;
387 bool urgent_enabled
= false;
388 bool urgent_collapse
= false;
389 char *clock_format
= NULL
;
390 bool window_class_enabled
= false;
391 bool window_instance_enabled
= false;
392 bool window_name_enabled
= false;
393 int focus_mode
= SWM_FOCUS_DEFAULT
;
394 int focus_close
= SWM_STACK_BELOW
;
395 bool focus_close_wrap
= true;
396 int focus_default
= SWM_STACK_TOP
;
397 int spawn_position
= SWM_STACK_TOP
;
398 bool disable_border
= false;
399 int border_width
= 1;
400 int region_padding
= 0;
402 bool java_workaround
= true;
403 bool verbose_layout
= false;
409 XFontSet bar_fs
= NULL
;
410 XFontSetExtents
*bar_fs_extents
;
411 XftFont
*bar_font
= NULL
;
412 bool bar_font_legacy
= true;
413 char *bar_fonts
= NULL
;
414 XftColor bar_font_color
;
415 XftColor search_font_color
;
416 char *startup_exception
= NULL
;
417 unsigned int nr_exceptions
= 0;
419 /* layout manager data */
420 struct swm_geometry
{
433 struct swm_geometry g
;
434 struct swm_region
*r
; /* Associated region. */
437 /* virtual "screens" */
439 TAILQ_ENTRY(swm_region
) entry
;
441 struct swm_geometry g
;
442 struct workspace
*ws
; /* current workspace on this region */
443 struct workspace
*ws_prior
; /* prior workspace on this region */
444 struct swm_screen
*s
; /* screen idx */
447 TAILQ_HEAD(swm_region_list
, swm_region
);
450 SWM_WIN_STATE_REPARENTING
,
451 SWM_WIN_STATE_REPARENTED
,
452 SWM_WIN_STATE_UNPARENTING
,
453 SWM_WIN_STATE_UNPARENTED
,
457 TAILQ_ENTRY(ws_win
) entry
;
458 TAILQ_ENTRY(ws_win
) stack_entry
;
461 xcb_window_t transient
;
462 struct ws_win
*focus_child
; /* focus on child transient */
463 struct swm_geometry g
; /* current geometry */
464 struct swm_geometry g_prev
; /* prev configured geometry */
465 struct swm_geometry g_float
; /* region coordinates */
466 bool g_floatvalid
; /* g_float geometry validity */
471 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
478 struct workspace
*ws
; /* always valid */
479 struct swm_screen
*s
; /* always valid, never changes */
481 xcb_icccm_get_wm_class_reply_t ch
;
482 xcb_icccm_wm_hints_t hints
;
487 TAILQ_HEAD(ws_win_list
, ws_win
);
488 TAILQ_HEAD(ws_win_stack
, ws_win
);
492 TAILQ_ENTRY(pid_e
) entry
;
496 TAILQ_HEAD(pid_list
, pid_e
);
497 struct pid_list pidlist
= TAILQ_HEAD_INITIALIZER(pidlist
);
499 /* layout handlers */
500 void stack(struct swm_region
*);
501 void vertical_config(struct workspace
*, int);
502 void vertical_stack(struct workspace
*, struct swm_geometry
*);
503 void horizontal_config(struct workspace
*, int);
504 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
505 void max_stack(struct workspace
*, struct swm_geometry
*);
506 void plain_stacker(struct workspace
*);
507 void fancy_stacker(struct workspace
*);
510 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
511 void (*l_config
)(struct workspace
*, int);
513 #define SWM_L_FOCUSPREV (1<<0)
514 #define SWM_L_MAPONFOCUS (1<<1)
515 void (*l_string
)(struct workspace
*);
517 /* stack, configure */
518 { vertical_stack
, vertical_config
, 0, plain_stacker
},
519 { horizontal_stack
, horizontal_config
, 0, plain_stacker
},
521 SWM_L_MAPONFOCUS
| SWM_L_FOCUSPREV
, plain_stacker
},
522 { NULL
, NULL
, 0, NULL
},
525 /* position of max_stack mode in the layouts array, index into layouts! */
526 #define SWM_V_STACK (0)
527 #define SWM_H_STACK (1)
528 #define SWM_MAX_STACK (2)
530 #define SWM_H_SLICE (32)
531 #define SWM_V_SLICE (32)
533 /* define work spaces */
535 int idx
; /* workspace index */
536 char *name
; /* workspace name */
537 bool always_raise
; /* raise windows on focus */
538 bool bar_enabled
; /* bar visibility */
539 struct layout
*cur_layout
; /* current layout handlers */
540 struct ws_win
*focus
; /* may be NULL */
541 struct ws_win
*focus_prev
; /* may be NULL */
542 struct ws_win
*focus_pending
; /* may be NULL */
543 struct swm_region
*r
; /* may be NULL */
544 struct swm_region
*old_r
; /* may be NULL */
545 struct ws_win_list winlist
; /* list of windows in ws */
546 struct ws_win_list unmanagedlist
; /* list of dead windows in ws */
547 struct ws_win_stack stack
; /* stacking order */
548 int state
; /* mapping state */
549 char stacker
[10]; /* display stacker and layout */
553 int horizontal_msize
;
555 int horizontal_stacks
;
556 bool horizontal_flip
;
566 SWM_WS_STATE_MAPPING
,
572 SWM_S_COLOR_BAR_BORDER
,
573 SWM_S_COLOR_BAR_BORDER_UNFOCUS
,
574 SWM_S_COLOR_BAR_FONT
,
576 SWM_S_COLOR_FOCUS_MAXIMIZED
,
578 SWM_S_COLOR_UNFOCUS_MAXIMIZED
,
582 /* physical screen mapping */
583 #define SWM_WS_MAX (22) /* hard limit */
584 int workspace_limit
= 10; /* soft limit */
587 int idx
; /* screen index */
588 struct swm_region_list rl
; /* list of regions on this screen */
589 struct swm_region_list orl
; /* list of old regions */
591 struct workspace ws
[SWM_WS_MAX
];
592 struct swm_region
*r_focus
;
599 } c
[SWM_S_COLOR_MAX
];
601 xcb_gcontext_t bar_gc
;
604 struct swm_screen
*screens
;
606 /* args to functions */
609 #define SWM_ARG_ID_FOCUSNEXT (0)
610 #define SWM_ARG_ID_FOCUSPREV (1)
611 #define SWM_ARG_ID_FOCUSMAIN (2)
612 #define SWM_ARG_ID_FOCUSURGENT (3)
613 #define SWM_ARG_ID_SWAPNEXT (10)
614 #define SWM_ARG_ID_SWAPPREV (11)
615 #define SWM_ARG_ID_SWAPMAIN (12)
616 #define SWM_ARG_ID_MOVELAST (13)
617 #define SWM_ARG_ID_MASTERSHRINK (20)
618 #define SWM_ARG_ID_MASTERGROW (21)
619 #define SWM_ARG_ID_MASTERADD (22)
620 #define SWM_ARG_ID_MASTERDEL (23)
621 #define SWM_ARG_ID_FLIPLAYOUT (24)
622 #define SWM_ARG_ID_STACKRESET (30)
623 #define SWM_ARG_ID_STACKINIT (31)
624 #define SWM_ARG_ID_STACKBALANCE (32)
625 #define SWM_ARG_ID_CYCLEWS_UP (40)
626 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
627 #define SWM_ARG_ID_CYCLERG_UP (42)
628 #define SWM_ARG_ID_CYCLERG_DOWN (43)
629 #define SWM_ARG_ID_CYCLEWS_UP_ALL (44)
630 #define SWM_ARG_ID_CYCLEWS_DOWN_ALL (45)
631 #define SWM_ARG_ID_CYCLEWS_MOVE_UP (46)
632 #define SWM_ARG_ID_CYCLEWS_MOVE_DOWN (47)
633 #define SWM_ARG_ID_STACKINC (50)
634 #define SWM_ARG_ID_STACKDEC (51)
635 #define SWM_ARG_ID_SS_ALL (60)
636 #define SWM_ARG_ID_SS_WINDOW (61)
637 #define SWM_ARG_ID_DONTCENTER (70)
638 #define SWM_ARG_ID_CENTER (71)
639 #define SWM_ARG_ID_KILLWINDOW (80)
640 #define SWM_ARG_ID_DELETEWINDOW (81)
641 #define SWM_ARG_ID_WIDTHGROW (90)
642 #define SWM_ARG_ID_WIDTHSHRINK (91)
643 #define SWM_ARG_ID_HEIGHTGROW (92)
644 #define SWM_ARG_ID_HEIGHTSHRINK (93)
645 #define SWM_ARG_ID_MOVEUP (100)
646 #define SWM_ARG_ID_MOVEDOWN (101)
647 #define SWM_ARG_ID_MOVELEFT (102)
648 #define SWM_ARG_ID_MOVERIGHT (103)
649 #define SWM_ARG_ID_RAISE (105)
650 #define SWM_ARG_ID_LOWER (106)
651 #define SWM_ARG_ID_BAR_TOGGLE (110)
652 #define SWM_ARG_ID_BAR_TOGGLE_WS (111)
653 #define SWM_ARG_ID_CYCLERG_MOVE_UP (112)
654 #define SWM_ARG_ID_CYCLERG_MOVE_DOWN (113)
660 TAILQ_ENTRY(quirk
) entry
;
661 char *class; /* WM_CLASS:class */
662 char *instance
; /* WM_CLASS:instance */
663 char *name
; /* WM_NAME */
665 regex_t regex_instance
;
668 int ws
; /* Initial workspace. */
669 #define SWM_Q_FLOAT (1<<0) /* Float this window. */
670 #define SWM_Q_TRANSSZ (1<<1) /* Transient window size too small. */
671 #define SWM_Q_ANYWHERE (1<<2) /* Don't position this window */
672 #define SWM_Q_XTERM_FONTADJ (1<<3) /* Adjust xterm fonts when resizing. */
673 #define SWM_Q_FULLSCREEN (1<<4) /* Remove border when fullscreen. */
674 #define SWM_Q_FOCUSPREV (1<<5) /* Focus on caller. */
675 #define SWM_Q_NOFOCUSONMAP (1<<6) /* Don't focus on window when mapped. */
676 #define SWM_Q_FOCUSONMAP_SINGLE (1<<7) /* Only focus if single win of type. */
677 #define SWM_Q_OBEYAPPFOCUSREQ (1<<8) /* Focus when applications ask. */
678 #define SWM_Q_IGNOREPID (1<<9) /* Ignore PID when determining ws. */
679 #define SWM_Q_IGNORESPAWNWS (1<<10) /* Ignore _SWM_WS when managing win. */
680 #define SWM_Q_NOFOCUSCYCLE (1<<11) /* Remove from normal focus cycle. */
681 #define SWM_Q_MINIMALBORDER (1<<12) /* No border when floating/unfocused. */
683 TAILQ_HEAD(quirk_list
, quirk
);
684 struct quirk_list quirks
= TAILQ_HEAD_INITIALIZER(quirks
);
687 * Supported EWMH hints should be added to
688 * both the enum and the ewmh array
694 _NET_CURRENT_DESKTOP
,
695 _NET_DESKTOP_GEOMETRY
,
697 _NET_DESKTOP_VIEWPORT
,
698 _NET_MOVERESIZE_WINDOW
,
699 _NET_NUMBER_OF_DESKTOPS
,
700 _NET_REQUEST_FRAME_EXTENTS
,
702 _NET_WM_ACTION_ABOVE
,
703 _NET_WM_ACTION_CLOSE
,
704 _NET_WM_ACTION_FULLSCREEN
,
706 _NET_WM_ACTION_RESIZE
,
707 _NET_WM_ALLOWED_ACTIONS
,
709 _NET_WM_FULL_PLACEMENT
,
713 _NET_WM_STATE_FULLSCREEN
,
714 _NET_WM_STATE_HIDDEN
,
715 _NET_WM_STATE_MAXIMIZED_VERT
,
716 _NET_WM_STATE_MAXIMIZED_HORZ
,
717 _NET_WM_STATE_SKIP_PAGER
,
718 _NET_WM_STATE_SKIP_TASKBAR
,
720 _NET_WM_WINDOW_TYPE_DIALOG
,
721 _NET_WM_WINDOW_TYPE_DOCK
,
722 _NET_WM_WINDOW_TYPE_NORMAL
,
723 _NET_WM_WINDOW_TYPE_SPLASH
,
724 _NET_WM_WINDOW_TYPE_TOOLBAR
,
725 _NET_WM_WINDOW_TYPE_UTILITY
,
726 _SWM_WM_STATE_MANUAL
,
733 } ewmh
[SWM_EWMH_HINT_MAX
] = {
734 /* must be in same order as in the enum */
735 {"_NET_ACTIVE_WINDOW", XCB_ATOM_NONE
},
736 {"_NET_CLIENT_LIST", XCB_ATOM_NONE
},
737 {"_NET_CLOSE_WINDOW", XCB_ATOM_NONE
},
738 {"_NET_CURRENT_DESKTOP", XCB_ATOM_NONE
},
739 {"_NET_DESKTOP_GEOMETRY", XCB_ATOM_NONE
},
740 {"_NET_DESKTOP_NAMES", XCB_ATOM_NONE
},
741 {"_NET_DESKTOP_VIEWPORT", XCB_ATOM_NONE
},
742 {"_NET_MOVERESIZE_WINDOW", XCB_ATOM_NONE
},
743 {"_NET_NUMBER_OF_DESKTOPS", XCB_ATOM_NONE
},
744 {"_NET_REQUEST_FRAME_EXTENTS", XCB_ATOM_NONE
},
745 {"_NET_RESTACK_WINDOW", XCB_ATOM_NONE
},
746 {"_NET_WM_ACTION_ABOVE", XCB_ATOM_NONE
},
747 {"_NET_WM_ACTION_CLOSE", XCB_ATOM_NONE
},
748 {"_NET_WM_ACTION_FULLSCREEN", XCB_ATOM_NONE
},
749 {"_NET_WM_ACTION_MOVE", XCB_ATOM_NONE
},
750 {"_NET_WM_ACTION_RESIZE", XCB_ATOM_NONE
},
751 {"_NET_WM_ALLOWED_ACTIONS", XCB_ATOM_NONE
},
752 {"_NET_WM_DESKTOP", XCB_ATOM_NONE
},
753 {"_NET_WM_FULL_PLACEMENT", XCB_ATOM_NONE
},
754 {"_NET_WM_NAME", XCB_ATOM_NONE
},
755 {"_NET_WM_STATE", XCB_ATOM_NONE
},
756 {"_NET_WM_STATE_ABOVE", XCB_ATOM_NONE
},
757 {"_NET_WM_STATE_FULLSCREEN", XCB_ATOM_NONE
},
758 {"_NET_WM_STATE_HIDDEN", XCB_ATOM_NONE
},
759 {"_NET_WM_STATE_MAXIMIZED_VERT", XCB_ATOM_NONE
},
760 {"_NET_WM_STATE_MAXIMIZED_HORZ", XCB_ATOM_NONE
},
761 {"_NET_WM_STATE_SKIP_PAGER", XCB_ATOM_NONE
},
762 {"_NET_WM_STATE_SKIP_TASKBAR", XCB_ATOM_NONE
},
763 {"_NET_WM_WINDOW_TYPE", XCB_ATOM_NONE
},
764 {"_NET_WM_WINDOW_TYPE_DIALOG", XCB_ATOM_NONE
},
765 {"_NET_WM_WINDOW_TYPE_DOCK", XCB_ATOM_NONE
},
766 {"_NET_WM_WINDOW_TYPE_NORMAL", XCB_ATOM_NONE
},
767 {"_NET_WM_WINDOW_TYPE_SPLASH", XCB_ATOM_NONE
},
768 {"_NET_WM_WINDOW_TYPE_TOOLBAR", XCB_ATOM_NONE
},
769 {"_NET_WM_WINDOW_TYPE_UTILITY", XCB_ATOM_NONE
},
770 {"_SWM_WM_STATE_MANUAL", XCB_ATOM_NONE
},
773 /* EWMH source type */
775 EWMH_SOURCE_TYPE_NONE
= 0,
776 EWMH_SOURCE_TYPE_NORMAL
= 1,
777 EWMH_SOURCE_TYPE_OTHER
= 2,
784 XC_BOTTOM_LEFT_CORNER
,
785 XC_BOTTOM_RIGHT_CORNER
,
793 char *name
; /* Name used by Xcursor .*/
794 uint8_t cf_char
; /* cursorfont index. */
796 } cursors
[XC_MAX
] = {
797 {"fleur", XC_fleur
, XCB_CURSOR_NONE
},
798 {"left_ptr", XC_left_ptr
, XCB_CURSOR_NONE
},
799 {"bottom_left_corner", XC_bottom_left_corner
, XCB_CURSOR_NONE
},
800 {"bottom_right_corner", XC_bottom_right_corner
, XCB_CURSOR_NONE
},
801 {"sizing", XC_sizing
, XCB_CURSOR_NONE
},
802 {"top_left_corner", XC_top_left_corner
, XCB_CURSOR_NONE
},
803 {"top_right_corner", XC_top_right_corner
, XCB_CURSOR_NONE
},
806 #define SWM_SPAWN_OPTIONAL 0x1
810 TAILQ_ENTRY(spawn_prog
) entry
;
816 TAILQ_HEAD(spawn_list
, spawn_prog
);
817 struct spawn_list spawns
= TAILQ_HEAD_INITIALIZER(spawns
);
823 /* User callable function IDs. */
946 /* SWM_DEBUG actions MUST be here: */
959 BINDING_F_REPLAY
= 0x1,
963 RB_ENTRY(binding
) entry
;
964 uint16_t mod
; /* Modifier Mask. */
965 enum binding_type type
; /* Key or Button. */
966 uint32_t value
; /* KeySym or Button Index. */
967 enum actionid action
; /* Action Identifier. */
971 RB_HEAD(binding_tree
, binding
);
973 /* function prototypes */
974 void adjust_font(struct ws_win
*);
975 char *argsep(char **);
976 void bar_cleanup(struct swm_region
*);
977 void bar_extra_setup(void);
978 void bar_extra_stop(void);
979 int bar_extra_update(void);
980 void bar_fmt(const char *, char *, struct swm_region
*, size_t);
981 void bar_fmt_expand(char *, size_t);
982 void bar_draw(struct swm_bar
*);
983 void bar_print(struct swm_region
*, const char *);
984 void bar_print_legacy(struct swm_region
*, const char *);
985 void bar_replace(char *, char *, struct swm_region
*, size_t);
986 void bar_replace_pad(char *, int *, size_t);
987 char *bar_replace_seq(char *, char *, struct swm_region
*, size_t *, size_t);
988 void bar_setup(struct swm_region
*);
989 void bar_toggle(struct binding
*, struct swm_region
*, union arg
*);
990 void bar_urgent(char *, size_t);
991 void bar_window_class(char *, size_t, struct swm_region
*);
992 void bar_window_class_instance(char *, size_t, struct swm_region
*);
993 void bar_window_float(char *, size_t, struct swm_region
*);
994 void bar_window_instance(char *, size_t, struct swm_region
*);
995 void bar_window_name(char *, size_t, struct swm_region
*);
996 void bar_window_state(char *, size_t, struct swm_region
*);
997 void bar_workspace_name(char *, size_t, struct swm_region
*);
998 int binding_cmp(struct binding
*, struct binding
*);
999 void binding_insert(uint16_t, enum binding_type
, uint32_t, enum actionid
,
1000 uint32_t, const char *);
1001 struct binding
*binding_lookup(uint16_t, enum binding_type
, uint32_t);
1002 void binding_remove(struct binding
*);
1003 void buttonpress(xcb_button_press_event_t
*);
1004 void buttonrelease(xcb_button_release_event_t
*);
1005 void center_pointer(struct swm_region
*);
1006 void check_conn(void);
1007 void clear_bindings(void);
1008 void clear_keybindings(void);
1009 int clear_maximized(struct workspace
*);
1010 void clear_quirks(void);
1011 void clear_spawns(void);
1012 void clientmessage(xcb_client_message_event_t
*);
1013 void client_msg(struct ws_win
*, xcb_atom_t
, xcb_timestamp_t
);
1014 int conf_load(const char *, int);
1015 void configurenotify(xcb_configure_notify_event_t
*);
1016 void configurerequest(xcb_configure_request_event_t
*);
1017 void config_win(struct ws_win
*, xcb_configure_request_event_t
*);
1018 void constrain_window(struct ws_win
*, struct swm_geometry
*, int *);
1019 int count_win(struct workspace
*, bool);
1020 void cursors_cleanup(void);
1021 void cursors_load(void);
1022 void custom_region(const char *);
1023 void cycle_layout(struct binding
*, struct swm_region
*, union arg
*);
1024 void cyclerg(struct binding
*, struct swm_region
*, union arg
*);
1025 void cyclews(struct binding
*, struct swm_region
*, union arg
*);
1027 void debug_refresh(struct ws_win
*);
1029 void debug_toggle(struct binding
*, struct swm_region
*, union arg
*);
1030 void destroynotify(xcb_destroy_notify_event_t
*);
1031 void dumpwins(struct binding
*, struct swm_region
*, union arg
*);
1032 int enable_wm(void);
1033 void enternotify(xcb_enter_notify_event_t
*);
1034 void event_drain(uint8_t);
1035 void event_error(xcb_generic_error_t
*);
1036 void event_handle(xcb_generic_event_t
*);
1037 void ewmh_apply_flags(struct ws_win
*, uint32_t);
1038 void ewmh_autoquirk(struct ws_win
*);
1039 void ewmh_get_desktop_names(void);
1040 void ewmh_get_wm_state(struct ws_win
*);
1041 void ewmh_update_actions(struct ws_win
*);
1042 void ewmh_update_client_list(void);
1043 void ewmh_update_current_desktop(void);
1044 void ewmh_update_desktop_names(void);
1045 void ewmh_update_desktops(void);
1046 void ewmh_change_wm_state(struct ws_win
*, xcb_atom_t
, long);
1047 void ewmh_update_wm_state(struct ws_win
*);
1048 char *expand_tilde(const char *);
1049 void expose(xcb_expose_event_t
*);
1050 void fake_keypress(struct ws_win
*, xcb_keysym_t
, uint16_t);
1051 struct swm_bar
*find_bar(xcb_window_t
);
1052 struct ws_win
*find_frame_window(xcb_window_t
);
1053 struct pid_e
*find_pid(pid_t
);
1054 struct swm_region
*find_region(xcb_window_t
);
1055 struct ws_win
*find_unmanaged_window(xcb_window_t
);
1056 struct ws_win
*find_window(xcb_window_t
);
1057 void floating_toggle(struct binding
*, struct swm_region
*, union arg
*);
1058 void focus(struct binding
*, struct swm_region
*, union arg
*);
1059 void focus_flush(void);
1060 void focus_pointer(struct binding
*, struct swm_region
*, union arg
*);
1061 void focus_region(struct swm_region
*);
1062 void focus_win(struct ws_win
*);
1063 void focusin(xcb_focus_in_event_t
*);
1065 void focusout(xcb_focus_out_event_t
*);
1067 void focusrg(struct binding
*, struct swm_region
*, union arg
*);
1068 void fontset_init(void);
1069 void free_window(struct ws_win
*);
1070 xcb_atom_t
get_atom_from_string(const char *);
1072 char *get_atom_name(xcb_atom_t
);
1074 struct ws_win
*get_focus_magic(struct ws_win
*);
1075 struct ws_win
*get_focus_prev(struct ws_win
*);
1076 xcb_generic_event_t
*get_next_event(bool);
1078 char *get_notify_detail_label(uint8_t);
1079 char *get_notify_mode_label(uint8_t);
1081 struct ws_win
*get_pointer_win(xcb_window_t
);
1082 struct ws_win
*get_region_focus(struct swm_region
*);
1083 int get_region_index(struct swm_region
*);
1084 xcb_screen_t
*get_screen(int);
1085 int get_screen_count(void);
1087 char *get_source_type_label(uint32_t);
1088 char *get_stack_mode_name(uint8_t);
1089 char *get_state_mask_label(uint16_t);
1091 int32_t get_swm_ws(xcb_window_t
);
1092 bool get_urgent(struct ws_win
*);
1094 char *get_win_input_model(struct ws_win
*);
1096 char *get_win_name(xcb_window_t
);
1097 uint8_t get_win_state(xcb_window_t
);
1098 void get_wm_protocols(struct ws_win
*);
1099 int get_ws_idx(struct ws_win
*);
1100 void grab_windows(void);
1101 void grabbuttons(void);
1102 void grabkeys(void);
1103 void iconify(struct binding
*, struct swm_region
*, union arg
*);
1104 bool isxlfd(char *);
1105 bool keybindreleased(struct binding
*, xcb_key_release_event_t
*);
1106 void keypress(xcb_key_press_event_t
*);
1107 void keyrelease(xcb_key_release_event_t
*);
1108 bool keyrepeating(xcb_key_release_event_t
*);
1109 void kill_bar_extra_atexit(void);
1110 void kill_refs(struct ws_win
*);
1112 void leavenotify(xcb_leave_notify_event_t
*);
1114 void load_float_geom(struct ws_win
*);
1115 void lower_window(struct ws_win
*);
1116 struct ws_win
*manage_window(xcb_window_t
, int, bool);
1117 void map_window(struct ws_win
*);
1118 void mapnotify(xcb_map_notify_event_t
*);
1119 void mappingnotify(xcb_mapping_notify_event_t
*);
1120 void maprequest(xcb_map_request_event_t
*);
1121 void maximize_toggle(struct binding
*, struct swm_region
*, union arg
*);
1122 void motionnotify(xcb_motion_notify_event_t
*);
1123 void move(struct binding
*, struct swm_region
*, union arg
*);
1124 void move_win(struct ws_win
*, struct binding
*, int);
1125 uint32_t name_to_pixel(int, const char *);
1126 void name_workspace(struct binding
*, struct swm_region
*, union arg
*);
1127 void new_region(struct swm_screen
*, int, int, int, int);
1128 int parse_rgb(const char *, uint16_t *, uint16_t *, uint16_t *);
1129 int parsebinding(const char *, uint16_t *, enum binding_type
*, uint32_t *,
1131 int parsequirks(const char *, uint32_t *, int *);
1132 void pressbutton(struct binding
*, struct swm_region
*, union arg
*);
1133 void priorws(struct binding
*, struct swm_region
*, union arg
*);
1135 void print_win_geom(xcb_window_t
);
1137 void propertynotify(xcb_property_notify_event_t
*);
1138 void put_back_event(xcb_generic_event_t
*);
1139 void quirk_free(struct quirk
*);
1140 void quirk_insert(const char *, const char *, const char *, uint32_t, int);
1141 void quirk_remove(struct quirk
*);
1142 void quirk_replace(struct quirk
*, const char *, const char *, const char *,
1144 void quit(struct binding
*, struct swm_region
*, union arg
*);
1145 void raise_toggle(struct binding
*, struct swm_region
*, union arg
*);
1146 void raise_window(struct ws_win
*);
1147 void region_containment(struct ws_win
*, struct swm_region
*, int);
1148 struct swm_region
*region_under(struct swm_screen
*, int, int);
1149 void regionize(struct ws_win
*, int, int);
1150 void reparent_window(struct ws_win
*);
1151 void reparentnotify(xcb_reparent_notify_event_t
*);
1152 void resize(struct binding
*, struct swm_region
*, union arg
*);
1153 void resize_win(struct ws_win
*, struct binding
*, int);
1154 void restart(struct binding
*, struct swm_region
*, union arg
*);
1155 struct swm_region
*root_to_region(xcb_window_t
, int);
1156 void screenchange(xcb_randr_screen_change_notify_event_t
*);
1157 void scan_randr(int);
1158 void search_do_resp(void);
1159 void search_resp_name_workspace(const char *, size_t);
1160 void search_resp_search_window(const char *);
1161 void search_resp_search_workspace(const char *);
1162 void search_resp_uniconify(const char *, size_t);
1163 void search_win(struct binding
*, struct swm_region
*, union arg
*);
1164 void search_win_cleanup(void);
1165 void search_workspace(struct binding
*, struct swm_region
*, union arg
*);
1166 void send_to_rg(struct binding
*, struct swm_region
*, union arg
*);
1167 void send_to_ws(struct binding
*, struct swm_region
*, union arg
*);
1168 void set_region(struct swm_region
*);
1169 int setautorun(const char *, const char *, int);
1170 void setbinding(uint16_t, enum binding_type
, uint32_t, enum actionid
,
1171 uint32_t, const char *);
1172 int setconfbinding(const char *, const char *, int);
1173 int setconfcolor(const char *, const char *, int);
1174 int setconfmodkey(const char *, const char *, int);
1175 int setconfquirk(const char *, const char *, int);
1176 int setconfregion(const char *, const char *, int);
1177 int setconfspawn(const char *, const char *, int);
1178 int setconfvalue(const char *, const char *, int);
1179 int setkeymapping(const char *, const char *, int);
1180 int setlayout(const char *, const char *, int);
1181 void setquirk(const char *, const char *, const char *, uint32_t, int);
1182 void setscreencolor(const char *, int, int);
1183 void setspawn(const char *, const char *, int);
1184 void setup_btnbindings(void);
1185 void setup_ewmh(void);
1186 void setup_globals(void);
1187 void setup_keybindings(void);
1188 void setup_quirks(void);
1189 void setup_screens(void);
1190 void setup_spawn(void);
1191 void set_child_transient(struct ws_win
*, xcb_window_t
*);
1192 void set_win_state(struct ws_win
*, uint8_t);
1193 void shutdown_cleanup(void);
1195 void socket_setnonblock(int);
1196 void sort_windows(struct ws_win_list
*);
1197 void spawn(int, union arg
*, bool);
1198 void spawn_custom(struct swm_region
*, union arg
*, const char *);
1199 int spawn_expand(struct swm_region
*, union arg
*, const char *, char ***);
1200 void spawn_insert(const char *, const char *, int);
1201 struct spawn_prog
*spawn_find(const char *);
1202 void spawn_remove(struct spawn_prog
*);
1203 void spawn_replace(struct spawn_prog
*, const char *, const char *, int);
1204 void spawn_select(struct swm_region
*, union arg
*, const char *, int *);
1205 void stack_config(struct binding
*, struct swm_region
*, union arg
*);
1206 void stack_master(struct workspace
*, struct swm_geometry
*, int, bool);
1207 void store_float_geom(struct ws_win
*);
1208 char *strdupsafe(const char *);
1209 void swapwin(struct binding
*, struct swm_region
*, union arg
*);
1210 void switchws(struct binding
*, struct swm_region
*, union arg
*);
1211 void teardown_ewmh(void);
1212 void unescape_selector(char *);
1213 void unfocus_win(struct ws_win
*);
1214 void uniconify(struct binding
*, struct swm_region
*, union arg
*);
1215 void unmanage_window(struct ws_win
*);
1216 void unmap_all(void);
1217 void unmap_window(struct ws_win
*);
1218 void unmapnotify(xcb_unmap_notify_event_t
*);
1219 void unparent_window(struct ws_win
*);
1220 void update_floater(struct ws_win
*);
1221 void update_modkey(uint16_t);
1222 void update_win_stacking(struct ws_win
*);
1223 void update_window(struct ws_win
*);
1224 void draw_frame(struct ws_win
*);
1225 void update_wm_state(struct ws_win
*win
);
1226 void updatenumlockmask(void);
1227 void validate_spawns(void);
1228 int validate_win(struct ws_win
*);
1229 int validate_ws(struct workspace
*);
1230 void version(struct binding
*, struct swm_region
*, union arg
*);
1231 void win_to_ws(struct ws_win
*, int, bool);
1232 pid_t
window_get_pid(xcb_window_t
);
1233 void wkill(struct binding
*, struct swm_region
*, union arg
*);
1234 void update_ws_stack(struct workspace
*);
1235 void xft_init(struct swm_region
*);
1236 void _add_startup_exception(const char *, va_list);
1237 void add_startup_exception(const char *, ...);
1239 RB_PROTOTYPE(binding_tree
, binding
, entry
, binding_cmp
);
1240 RB_GENERATE(binding_tree
, binding
, entry
, binding_cmp
);
1241 struct binding_tree bindings
;
1246 xcb_font_t cf
= XCB_NONE
;
1249 for (i
= 0; i
< LENGTH(cursors
); ++i
) {
1250 /* try to load Xcursor first. */
1251 cursors
[i
].cid
= XcursorLibraryLoadCursor(display
,
1254 /* fallback to cursorfont. */
1255 if (cursors
[i
].cid
== XCB_CURSOR_NONE
) {
1256 if (cf
== XCB_NONE
) {
1257 cf
= xcb_generate_id(conn
);
1258 xcb_open_font(conn
, cf
, strlen("cursor"),
1262 cursors
[i
].cid
= xcb_generate_id(conn
);
1263 xcb_create_glyph_cursor(conn
, cursors
[i
].cid
, cf
, cf
,
1264 cursors
[i
].cf_char
, cursors
[i
].cf_char
+ 1, 0, 0, 0,
1265 0xffff, 0xffff, 0xffff);
1271 xcb_close_font(conn
, cf
);
1275 cursors_cleanup(void)
1278 for (i
= 0; i
< LENGTH(cursors
); ++i
)
1279 xcb_free_cursor(conn
, cursors
[i
].cid
);
1283 expand_tilde(const char *s
)
1285 struct passwd
*ppwd
;
1293 errx(1, "expand_tilde: NULL string.");
1296 result
= strdup(sc
);
1302 if ((max
= sysconf(_SC_LOGIN_NAME_MAX
)) == -1)
1303 errx(1, "expand_tilde: sysconf");
1305 if ((user
= calloc(1, max
+ 1)) == NULL
)
1306 errx(1, "expand_tilde: calloc");
1308 for (i
= 0; s
[i
] != '/' && s
[i
] != '\0'; ++i
)
1313 ppwd
= strlen(user
) == 0 ? getpwuid(getuid()) : getpwnam(user
);
1317 result
= strdup(sc
);
1319 if (asprintf(&result
, "%s%s", ppwd
->pw_dir
, s
) == -1)
1323 errx(1, "expand_tilde: failed to allocate memory.");
1329 parse_rgb(const char *rgb
, uint16_t *rr
, uint16_t *gg
, uint16_t *bb
)
1331 unsigned int tmpr
, tmpg
, tmpb
;
1333 if (sscanf(rgb
, "rgb:%x/%x/%x", &tmpr
, &tmpg
, &tmpb
) != 3)
1336 *rr
= RGB_8_TO_16(tmpr
);
1337 *gg
= RGB_8_TO_16(tmpg
);
1338 *bb
= RGB_8_TO_16(tmpb
);
1344 get_screen(int screen
)
1346 const xcb_setup_t
*r
;
1347 xcb_screen_iterator_t iter
;
1349 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1350 DNPRINTF(SWM_D_MISC
, "get_screen: xcb_get_setup\n");
1354 iter
= xcb_setup_roots_iterator(r
);
1355 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
1363 get_screen_count(void)
1365 const xcb_setup_t
*r
;
1367 if ((r
= xcb_get_setup(conn
)) == NULL
) {
1368 DNPRINTF(SWM_D_MISC
, "get_screen_count: xcb_get_setup\n");
1372 return xcb_setup_roots_length(r
);
1376 get_region_index(struct swm_region
*r
)
1378 struct swm_region
*rr
;
1384 TAILQ_FOREACH(rr
, &r
->s
->rl
, entry
) {
1399 if (focus_mode
== SWM_FOCUS_DEFAULT
)
1400 event_drain(XCB_ENTER_NOTIFY
);
1406 get_atom_from_string(const char *str
)
1408 xcb_intern_atom_cookie_t c
;
1409 xcb_intern_atom_reply_t
*r
;
1412 c
= xcb_intern_atom(conn
, 0, strlen(str
), str
);
1413 r
= xcb_intern_atom_reply(conn
, c
, NULL
);
1421 return (XCB_ATOM_NONE
);
1425 get_wm_protocols(struct ws_win
*win
) {
1427 xcb_icccm_get_wm_protocols_reply_t wpr
;
1429 if (xcb_icccm_get_wm_protocols_reply(conn
,
1430 xcb_icccm_get_wm_protocols(conn
, win
->id
, a_prot
),
1432 for (i
= 0; i
< (int)wpr
.atoms_len
; i
++) {
1433 if (wpr
.atoms
[i
] == a_takefocus
)
1434 win
->take_focus
= true;
1435 if (wpr
.atoms
[i
] == a_delete
)
1436 win
->can_delete
= true;
1438 xcb_icccm_get_wm_protocols_reply_wipe(&wpr
);
1445 xcb_window_t root
, win
;
1446 int i
, j
, num_screens
;
1448 for (i
= 0; i
< LENGTH(ewmh
); i
++)
1449 ewmh
[i
].atom
= get_atom_from_string(ewmh
[i
].name
);
1451 num_screens
= get_screen_count();
1452 for (i
= 0; i
< num_screens
; i
++) {
1453 root
= screens
[i
].root
;
1455 /* Set up _NET_SUPPORTING_WM_CHECK. */
1456 win
= xcb_generate_id(conn
);
1457 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
, root
,
1458 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
1459 XCB_COPY_FROM_PARENT
, 0, NULL
);
1461 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, root
,
1462 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1463 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1464 a_net_wm_check
, XCB_ATOM_WINDOW
, 32, 1, &win
);
1466 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
,
1467 ewmh
[_NET_WM_NAME
].atom
, a_utf8_string
,
1468 8, strlen("spectrwm"), "spectrwm");
1470 /* Report supported atoms */
1471 xcb_delete_property(conn
, root
, a_net_supported
);
1472 for (j
= 0; j
< LENGTH(ewmh
); j
++)
1473 xcb_change_property(conn
, XCB_PROP_MODE_APPEND
, root
,
1474 a_net_supported
, XCB_ATOM_ATOM
, 32, 1,
1479 ewmh_update_desktops();
1480 ewmh_get_desktop_names();
1488 xcb_get_property_cookie_t pc
;
1489 xcb_get_property_reply_t
*pr
;
1491 num_screens
= get_screen_count();
1493 for (i
= 0; i
< num_screens
; i
++) {
1494 /* Get the support check window and destroy it */
1495 pc
= xcb_get_property(conn
, 0, screens
[i
].root
, a_net_wm_check
,
1496 XCB_ATOM_WINDOW
, 0, 1);
1497 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
1500 if (pr
->format
== a_net_wm_check
) {
1501 id
= *((xcb_window_t
*)xcb_get_property_value(pr
));
1503 xcb_destroy_window(conn
, id
);
1504 xcb_delete_property(conn
, screens
[i
].root
,
1506 xcb_delete_property(conn
, screens
[i
].root
,
1514 ewmh_autoquirk(struct ws_win
*win
)
1516 xcb_get_property_reply_t
*r
;
1517 xcb_get_property_cookie_t c
;
1521 c
= xcb_get_property(conn
, 0, win
->id
,
1522 ewmh
[_NET_WM_WINDOW_TYPE
].atom
, XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1523 r
= xcb_get_property_reply(conn
, c
, NULL
);
1527 type
= xcb_get_property_value(r
);
1528 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1530 for (i
= 0; i
< n
; i
++) {
1531 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_NORMAL
].atom
)
1533 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DOCK
].atom
||
1534 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_TOOLBAR
].atom
||
1535 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_UTILITY
].atom
) {
1536 win
->quirks
= SWM_Q_FLOAT
| SWM_Q_ANYWHERE
;
1539 if (type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_SPLASH
].atom
||
1540 type
[i
] == ewmh
[_NET_WM_WINDOW_TYPE_DIALOG
].atom
) {
1541 win
->quirks
= SWM_Q_FLOAT
;
1549 ewmh_update_actions(struct ws_win
*win
)
1551 xcb_atom_t actions
[SWM_EWMH_ACTION_COUNT_MAX
];
1557 actions
[n
++] = ewmh
[_NET_WM_ACTION_CLOSE
].atom
;
1560 actions
[n
++] = ewmh
[_NET_WM_ACTION_MOVE
].atom
;
1561 actions
[n
++] = ewmh
[_NET_WM_ACTION_RESIZE
].atom
;
1562 actions
[n
++] = ewmh
[_NET_WM_ACTION_ABOVE
].atom
;
1565 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1566 ewmh
[_NET_WM_ALLOWED_ACTIONS
].atom
, XCB_ATOM_ATOM
, 32, 1, actions
);
1569 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1570 #define _NET_WM_STATE_ADD 1 /* add/set property */
1571 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1574 ewmh_change_wm_state(struct ws_win
*win
, xcb_atom_t state
, long action
)
1581 name
= get_atom_name(state
);
1582 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: win %#x, state: %s, "
1583 "action: %ld\n", WINID(win
), name
, action
);
1589 if (state
== ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
)
1590 flag
= EWMH_F_FULLSCREEN
;
1591 else if (state
== ewmh
[_NET_WM_STATE_ABOVE
].atom
)
1592 flag
= EWMH_F_ABOVE
;
1593 else if (state
== ewmh
[_NET_WM_STATE_HIDDEN
].atom
)
1594 flag
= EWMH_F_HIDDEN
;
1595 else if (state
== ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
||
1596 state
== ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
)
1597 flag
= EWMH_F_MAXIMIZED
;
1598 else if (state
== ewmh
[_SWM_WM_STATE_MANUAL
].atom
)
1599 flag
= SWM_F_MANUAL
;
1600 else if (state
== ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
)
1601 flag
= EWMH_F_SKIP_PAGER
;
1602 else if (state
== ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
)
1603 flag
= EWMH_F_SKIP_TASKBAR
;
1605 /* Disallow unfloating transients. */
1606 if (TRANS(win
) && flag
== EWMH_F_ABOVE
)
1609 new_flags
= win
->ewmh_flags
;
1612 case _NET_WM_STATE_REMOVE
:
1615 case _NET_WM_STATE_ADD
:
1618 case _NET_WM_STATE_TOGGLE
:
1623 ewmh_apply_flags(win
, new_flags
);
1626 DNPRINTF(SWM_D_PROP
, "ewmh_change_wm_state: done.\n");
1630 ewmh_apply_flags(struct ws_win
*win
, uint32_t pending
)
1632 struct workspace
*ws
;
1635 changed
= win
->ewmh_flags
^ pending
;
1639 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: pending: %d\n", pending
);
1641 win
->ewmh_flags
= pending
;
1644 if (changed
& EWMH_F_HIDDEN
) {
1646 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1647 ws
->focus_pending
= get_focus_prev(win
);
1652 /* Reload floating geometry in case region changed. */
1654 load_float_geom(win
);
1656 /* The window is no longer iconic, prepare focus. */
1657 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
1658 ws
->focus_pending
= get_focus_magic(win
);
1663 if (changed
& EWMH_F_ABOVE
) {
1664 if (ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1666 load_float_geom(win
);
1667 else if (!MAXIMIZED(win
))
1668 store_float_geom(win
);
1670 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1671 changed
&= ~EWMH_F_MAXIMIZED
;
1675 win
->ewmh_flags
^= EWMH_F_ABOVE
& pending
;
1679 if (changed
& EWMH_F_MAXIMIZED
) {
1680 /* VERT and/or HORZ changed. */
1682 if (!MAXIMIZED(win
))
1683 load_float_geom(win
);
1685 store_float_geom(win
);
1688 if (MAXIMIZED(win
)) {
1689 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
1690 ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]) {
1694 ws
->focus_pending
= win
;
1702 if (changed
& EWMH_F_FULLSCREEN
) {
1703 if (FULLSCREEN(win
)) {
1704 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
1708 ws
->focus_pending
= win
;
1711 load_float_geom(win
);
1714 win
->ewmh_flags
&= ~EWMH_F_MAXIMIZED
;
1718 DNPRINTF(SWM_D_PROP
, "ewmh_apply_flags: done.\n");
1722 ewmh_update_wm_state(struct ws_win
*win
) {
1723 xcb_atom_t vals
[SWM_EWMH_ACTION_COUNT_MAX
];
1727 vals
[n
++] = ewmh
[_NET_WM_STATE_HIDDEN
].atom
;
1728 if (FULLSCREEN(win
))
1729 vals
[n
++] = ewmh
[_NET_WM_STATE_FULLSCREEN
].atom
;
1730 if (MAXIMIZED_VERT(win
))
1731 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_VERT
].atom
;
1732 if (MAXIMIZED_HORZ(win
))
1733 vals
[n
++] = ewmh
[_NET_WM_STATE_MAXIMIZED_HORZ
].atom
;
1734 if (win
->ewmh_flags
& EWMH_F_SKIP_PAGER
)
1735 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_PAGER
].atom
;
1736 if (win
->ewmh_flags
& EWMH_F_SKIP_TASKBAR
)
1737 vals
[n
++] = ewmh
[_NET_WM_STATE_SKIP_TASKBAR
].atom
;
1738 if (win
->ewmh_flags
& EWMH_F_ABOVE
)
1739 vals
[n
++] = ewmh
[_NET_WM_STATE_ABOVE
].atom
;
1740 if (win
->ewmh_flags
& SWM_F_MANUAL
)
1741 vals
[n
++] = ewmh
[_SWM_WM_STATE_MANUAL
].atom
;
1744 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
1745 ewmh
[_NET_WM_STATE
].atom
, XCB_ATOM_ATOM
, 32, n
, vals
);
1747 xcb_delete_property(conn
, win
->id
, ewmh
[_NET_WM_STATE
].atom
);
1751 ewmh_get_wm_state(struct ws_win
*win
)
1754 xcb_get_property_cookie_t c
;
1755 xcb_get_property_reply_t
*r
;
1761 win
->ewmh_flags
= 0;
1763 c
= xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_STATE
].atom
,
1764 XCB_ATOM_ATOM
, 0, UINT32_MAX
);
1765 r
= xcb_get_property_reply(conn
, c
, NULL
);
1769 states
= xcb_get_property_value(r
);
1770 n
= xcb_get_property_value_length(r
) / sizeof(xcb_atom_t
);
1772 for (i
= 0; i
< n
; i
++)
1773 ewmh_change_wm_state(win
, states
[i
], _NET_WM_STATE_ADD
);
1781 dumpwins(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
1785 xcb_get_window_attributes_cookie_t c
;
1786 xcb_get_window_attributes_reply_t
*wa
;
1788 /* suppress unused warning since var is needed */
1792 if (r
->ws
== NULL
) {
1793 DPRINTF("dumpwins: invalid workspace\n");
1797 DPRINTF("=== managed window list ws %02d ===\n", r
->ws
->idx
);
1798 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
) {
1799 state
= get_win_state(w
->id
);
1800 c
= xcb_get_window_attributes(conn
, w
->id
);
1801 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1803 DPRINTF("win %#x (%#x), map_state: %d, state: %u, "
1804 "transient: %#x\n", w
->frame
, w
->id
, wa
->map_state
,
1805 state
, w
->transient
);
1808 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1812 DPRINTF("=== stacking order (top down) === \n");
1813 TAILQ_FOREACH(w
, &r
->ws
->stack
, stack_entry
) {
1814 DPRINTF("win %#x (%#x), fs: %s, maximized: %s, above: %s, "
1815 "iconic: %s\n", w
->frame
, w
->id
, YESNO(FULLSCREEN(w
)),
1816 YESNO(MAXIMIZED(w
)), YESNO(ABOVE(w
)), YESNO(ICONIC(w
)));
1819 DPRINTF("===== unmanaged window list =====\n");
1820 TAILQ_FOREACH(w
, &r
->ws
->unmanagedlist
, entry
) {
1821 state
= get_win_state(w
->id
);
1822 c
= xcb_get_window_attributes(conn
, w
->id
);
1823 wa
= xcb_get_window_attributes_reply(conn
, c
, NULL
);
1825 DPRINTF("win %#x, map_state: %d, state: %u, "
1826 "transient: %#x\n", w
->id
, wa
->map_state
,
1827 state
, w
->transient
);
1830 DPRINTF("win %#x, failed xcb_get_window_attributes\n",
1834 DPRINTF("=================================\n");
1838 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
1841 int num_screens
, i
, j
;
1843 /* Suppress warnings. */
1848 DNPRINTF(SWM_D_MISC
, "debug_toggle\n");
1850 debug_enabled
= !debug_enabled
;
1852 num_screens
= get_screen_count();
1853 for (i
= 0; i
< num_screens
; i
++)
1854 for (j
= 0; j
< workspace_limit
; j
++)
1855 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1862 debug_refresh(struct ws_win
*win
)
1869 XRectangle l_ibox
, l_lbox
;
1870 xcb_rectangle_t rect
;
1872 uint32_t wc
[4], mask
, width
, height
, gcv
[1];
1875 xcb_screen_t
*screen
;
1877 if (debug_enabled
) {
1878 /* Create debug window if it doesn't exist. */
1879 if (win
->debug
== XCB_WINDOW_NONE
) {
1880 if ((screen
= get_screen(win
->s
->idx
)) == NULL
)
1881 errx(1, "ERROR: can't get screen %d.",
1884 win
->debug
= xcb_generate_id(conn
);
1885 wc
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1886 wc
[1] = win
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
;
1887 wc
[2] = screen
->default_colormap
;
1889 xcb_create_window(conn
, screen
->root_depth
, win
->debug
,
1890 win
->frame
, 0, 0, 10, 10, 1,
1891 XCB_WINDOW_CLASS_INPUT_OUTPUT
, screen
->root_visual
,
1892 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
|
1893 XCB_CW_COLORMAP
, wc
);
1895 xcb_map_window(conn
, win
->debug
);
1898 /* Determine workspace window list index. */
1900 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
1906 /* Determine stacking index (top down). */
1908 TAILQ_FOREACH(w
, &win
->ws
->stack
, stack_entry
) {
1914 if (asprintf(&s
, "%#x f:%#x wl:%d s:%d im:%s", win
->id
,
1915 win
->frame
, widx
, sidx
, get_win_input_model(win
)) == -1)
1920 /* Update window to an appropriate dimension. */
1921 if (bar_font_legacy
) {
1922 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
1923 width
= l_lbox
.width
+ 4;
1924 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
1926 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
1928 width
= info
.width
+ 4;
1929 height
= bar_font
->height
+ 4;
1932 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
1933 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
;
1935 wc
[0] = wc
[1] = border_width
;
1942 xcb_configure_window(conn
, win
->debug
, mask
, wc
);
1944 /* Draw a filled rectangle to 'clear' window. */
1948 rect
.height
= height
;
1950 gcv
[0] = win
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
1951 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
1952 xcb_poly_fill_rectangle(conn
, win
->debug
, win
->s
->bar_gc
, 1,
1956 if (bar_font_legacy
) {
1957 l_gcv
.graphics_exposures
= 0;
1958 l_draw
= XCreateGC(display
, win
->debug
, 0, &l_gcv
);
1960 XSetForeground(display
, l_draw
,
1961 win
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
1963 DRAWSTRING(display
, win
->debug
, bar_fs
, l_draw
, 2,
1964 (bar_fs_extents
->max_logical_extent
.height
-
1965 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
1967 XFreeGC(display
, l_draw
);
1969 draw
= XftDrawCreate(display
, win
->debug
,
1970 DefaultVisual(display
, win
->s
->idx
),
1971 DefaultColormap(display
, win
->s
->idx
));
1973 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, 2,
1974 (bar_height
+ bar_font
->height
) / 2 -
1975 bar_font
->descent
, (FcChar8
*)s
, len
);
1977 XftDrawDestroy(draw
);
1981 } else if (win
->debug
!= XCB_WINDOW_NONE
) {
1982 xcb_destroy_window(conn
, win
->debug
);
1983 win
->debug
= XCB_WINDOW_NONE
;
1988 dumpwins(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
1996 debug_toggle(struct binding
*b
, struct swm_region
*r
, union arg
*s
)
2002 #endif /* SWM_DEBUG */
2007 int saved_errno
, status
;
2010 saved_errno
= errno
;
2014 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
2019 if (errno
!= ECHILD
)
2020 warn("sighdlr: waitpid");
2021 #endif /* SWM_DEBUG */
2024 if (pid
== searchpid
)
2028 if (WIFEXITED(status
)) {
2029 if (WEXITSTATUS(status
) != 0)
2030 warnx("sighdlr: child exit status: %d",
2031 WEXITSTATUS(status
));
2033 warnx("sighdlr: child is terminated "
2035 #endif /* SWM_DEBUG */
2049 errno
= saved_errno
;
2055 struct pid_e
*p
= NULL
;
2057 DNPRINTF(SWM_D_MISC
, "find_pid: %d\n", pid
);
2062 TAILQ_FOREACH(p
, &pidlist
, entry
) {
2071 name_to_pixel(int sidx
, const char *colorname
)
2073 uint32_t result
= 0;
2074 char cname
[32] = "#";
2075 xcb_screen_t
*screen
;
2076 xcb_colormap_t cmap
;
2077 xcb_alloc_color_reply_t
*cr
;
2078 xcb_alloc_named_color_reply_t
*nr
;
2079 uint16_t rr
, gg
, bb
;
2081 screen
= get_screen(sidx
);
2082 cmap
= screen
->default_colormap
;
2084 /* color is in format rgb://rr/gg/bb */
2085 if (strncmp(colorname
, "rgb:", 4) == 0) {
2086 if (parse_rgb(colorname
, &rr
, &gg
, &bb
) == -1)
2087 warnx("could not parse rgb %s", colorname
);
2089 cr
= xcb_alloc_color_reply(conn
,
2090 xcb_alloc_color(conn
, cmap
, rr
, gg
, bb
),
2096 warnx("color '%s' not found", colorname
);
2099 nr
= xcb_alloc_named_color_reply(conn
,
2100 xcb_alloc_named_color(conn
, cmap
, strlen(colorname
),
2103 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
2104 nr
= xcb_alloc_named_color_reply(conn
,
2105 xcb_alloc_named_color(conn
, cmap
, strlen(cname
),
2112 warnx("color '%s' not found", colorname
);
2119 setscreencolor(const char *val
, int i
, int c
)
2121 if (i
< 0 || i
>= get_screen_count())
2124 screens
[i
].c
[c
].pixel
= name_to_pixel(i
, val
);
2125 free(screens
[i
].c
[c
].name
);
2126 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
2131 fancy_stacker(struct workspace
*ws
)
2133 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2134 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2135 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2136 ws
->l_state
.vertical_flip
? "[%d>%d]" : "[%d|%d]",
2137 ws
->l_state
.vertical_mwin
, ws
->l_state
.vertical_stacks
);
2138 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2139 snprintf(ws
->stacker
, sizeof ws
->stacker
,
2140 ws
->l_state
.horizontal_flip
? "[%dv%d]" : "[%d-%d]",
2141 ws
->l_state
.horizontal_mwin
, ws
->l_state
.horizontal_stacks
);
2145 plain_stacker(struct workspace
*ws
)
2147 strlcpy(ws
->stacker
, "[ ]", sizeof ws
->stacker
);
2148 if (ws
->cur_layout
->l_stack
== vertical_stack
)
2149 strlcpy(ws
->stacker
, ws
->l_state
.vertical_flip
? "[>]" : "[|]",
2150 sizeof ws
->stacker
);
2151 else if (ws
->cur_layout
->l_stack
== horizontal_stack
)
2152 strlcpy(ws
->stacker
, ws
->l_state
.horizontal_flip
? "[v]" : "[-]",
2153 sizeof ws
->stacker
);
2157 custom_region(const char *val
)
2159 unsigned int x
, y
, w
, h
;
2160 int sidx
, num_screens
;
2161 xcb_screen_t
*screen
;
2163 DNPRINTF(SWM_D_CONF
, "custom_region: %s\n", val
);
2165 num_screens
= get_screen_count();
2166 if (sscanf(val
, "screen[%d]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
2167 errx(1, "invalid custom region, "
2168 "should be 'screen[<n>]:<n>x<n>+<n>+<n>");
2169 if (sidx
< 1 || sidx
> num_screens
)
2170 errx(1, "invalid screen index: %d out of bounds (maximum %d)",
2174 if ((screen
= get_screen(sidx
)) == NULL
)
2175 errx(1, "ERROR: can't get screen %d.", sidx
);
2178 errx(1, "region %ux%u+%u+%u too small", w
, h
, x
, y
);
2180 if (x
> screen
->width_in_pixels
||
2181 y
> screen
->height_in_pixels
||
2182 w
+ x
> screen
->width_in_pixels
||
2183 h
+ y
> screen
->height_in_pixels
) {
2184 warnx("ignoring region %ux%u+%u+%u - not within screen "
2185 "boundaries (%ux%u)", w
, h
, x
, y
,
2186 screen
->width_in_pixels
, screen
->height_in_pixels
);
2190 new_region(&screens
[sidx
], x
, y
, w
, h
);
2194 socket_setnonblock(int fd
)
2198 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
2199 err(1, "fcntl F_GETFL");
2200 flags
|= O_NONBLOCK
;
2201 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
2202 err(1, "fcntl F_SETFL");
2206 bar_print_legacy(struct swm_region
*r
, const char *s
)
2208 xcb_rectangle_t rect
;
2213 XRectangle ibox
, lbox
;
2217 XmbTextExtents(bar_fs
, s
, len
, &ibox
, &lbox
);
2219 switch (bar_justify
) {
2220 case SWM_BAR_JUSTIFY_LEFT
:
2223 case SWM_BAR_JUSTIFY_CENTER
:
2224 x
= (WIDTH(r
) - lbox
.width
) / 2;
2226 case SWM_BAR_JUSTIFY_RIGHT
:
2227 x
= WIDTH(r
) - lbox
.width
- SWM_BAR_OFFSET
;
2231 if (x
< SWM_BAR_OFFSET
)
2236 rect
.width
= WIDTH(r
->bar
);
2237 rect
.height
= HEIGHT(r
->bar
);
2239 /* clear back buffer */
2240 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2241 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2242 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2244 /* draw back buffer */
2245 gcvd
.graphics_exposures
= 0;
2246 draw
= XCreateGC(display
, r
->bar
->buffer
, GCGraphicsExposures
, &gcvd
);
2247 XSetForeground(display
, draw
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
);
2248 DRAWSTRING(display
, r
->bar
->buffer
, bar_fs
, draw
,
2249 x
, (bar_fs_extents
->max_logical_extent
.height
- lbox
.height
) / 2 -
2251 XFreeGC(display
, draw
);
2254 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2255 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2259 bar_print(struct swm_region
*r
, const char *s
)
2262 xcb_rectangle_t rect
;
2270 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
, &info
);
2272 switch (bar_justify
) {
2273 case SWM_BAR_JUSTIFY_LEFT
:
2276 case SWM_BAR_JUSTIFY_CENTER
:
2277 x
= (WIDTH(r
) - info
.width
) / 2;
2279 case SWM_BAR_JUSTIFY_RIGHT
:
2280 x
= WIDTH(r
) - info
.width
- SWM_BAR_OFFSET
;
2284 if (x
< SWM_BAR_OFFSET
)
2289 rect
.width
= WIDTH(r
->bar
);
2290 rect
.height
= HEIGHT(r
->bar
);
2292 /* clear back buffer */
2293 gcv
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2294 xcb_change_gc(conn
, r
->s
->bar_gc
, XCB_GC_FOREGROUND
, gcv
);
2295 xcb_poly_fill_rectangle(conn
, r
->bar
->buffer
, r
->s
->bar_gc
, 1, &rect
);
2297 /* draw back buffer */
2298 draw
= XftDrawCreate(display
, r
->bar
->buffer
,
2299 DefaultVisual(display
, r
->s
->idx
),
2300 DefaultColormap(display
, r
->s
->idx
));
2302 XftDrawStringUtf8(draw
, &bar_font_color
, bar_font
, x
,
2303 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 - bar_font
->descent
,
2306 XftDrawDestroy(draw
);
2309 xcb_copy_area(conn
, r
->bar
->buffer
, r
->bar
->id
, r
->s
->bar_gc
, 0, 0,
2310 0, 0, WIDTH(r
->bar
), HEIGHT(r
->bar
));
2314 bar_extra_stop(void)
2318 bzero(bar_pipe
, sizeof bar_pipe
);
2321 kill(bar_pid
, SIGTERM
);
2324 strlcpy(bar_ext
, "", sizeof bar_ext
);
2329 bar_window_class(char *s
, size_t sz
, struct swm_region
*r
)
2331 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2333 if (r
->ws
->focus
->ch
.class_name
!= NULL
)
2334 strlcat(s
, r
->ws
->focus
->ch
.class_name
, sz
);
2338 bar_window_instance(char *s
, size_t sz
, struct swm_region
*r
)
2340 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2342 if (r
->ws
->focus
->ch
.instance_name
!= NULL
)
2343 strlcat(s
, r
->ws
->focus
->ch
.instance_name
, sz
);
2347 bar_window_class_instance(char *s
, size_t sz
, struct swm_region
*r
)
2349 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2352 bar_window_class(s
, sz
, r
);
2353 strlcat(s
, ":", sz
);
2354 bar_window_instance(s
, sz
, r
);
2358 bar_window_state(char *s
, size_t sz
, struct swm_region
*r
)
2360 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2362 if (MAXIMIZED(r
->ws
->focus
))
2363 strlcat(s
, "(m)", sz
);
2364 else if (ABOVE(r
->ws
->focus
))
2365 strlcat(s
, "(f)", sz
);
2369 bar_window_name(char *s
, size_t sz
, struct swm_region
*r
)
2373 if (r
== NULL
|| r
->ws
== NULL
|| r
->ws
->focus
== NULL
)
2376 title
= get_win_name(r
->ws
->focus
->id
);
2377 strlcat(s
, title
, sz
);
2382 get_urgent(struct ws_win
*win
)
2384 xcb_icccm_wm_hints_t hints
;
2385 xcb_get_property_cookie_t c
;
2386 bool urgent
= false;
2389 c
= xcb_icccm_get_wm_hints(conn
, win
->id
);
2390 if (xcb_icccm_get_wm_hints_reply(conn
, c
, &hints
, NULL
))
2391 urgent
= xcb_icccm_wm_hints_get_urgency(&hints
);
2398 bar_urgent(char *s
, size_t sz
)
2401 int i
, j
, num_screens
;
2402 bool urgent
[SWM_WS_MAX
];
2405 for (i
= 0; i
< workspace_limit
; i
++)
2408 num_screens
= get_screen_count();
2409 for (i
= 0; i
< num_screens
; i
++)
2410 for (j
= 0; j
< workspace_limit
; j
++)
2411 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
2412 if (get_urgent(win
))
2415 for (i
= 0; i
< workspace_limit
; i
++) {
2417 snprintf(b
, sizeof b
, "%d ", i
+ 1);
2419 } else if (!urgent_collapse
) {
2420 strlcat(s
, "- ", sz
);
2423 if(urgent_collapse
&& s
[0])
2424 s
[strlen(s
) - 1] = 0;
2428 bar_workspace_name(char *s
, size_t sz
, struct swm_region
*r
)
2430 if (r
== NULL
|| r
->ws
== NULL
)
2432 if (r
->ws
->name
!= NULL
)
2433 strlcat(s
, r
->ws
->name
, sz
);
2436 /* build the default bar format according to the defined enabled options */
2438 bar_fmt(const char *fmtexp
, char *fmtnew
, struct swm_region
*r
, size_t sz
)
2442 /* if format provided, just copy the buffers */
2443 if (bar_format
!= NULL
) {
2444 strlcpy(fmtnew
, fmtexp
, sz
);
2448 /* reset the output buffer */
2451 strlcat(fmtnew
, "+N:+I ", sz
);
2453 strlcat(fmtnew
, "+S", sz
);
2454 strlcat(fmtnew
, " ", sz
);
2456 /* only show the workspace name if there's actually one */
2457 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->name
!= NULL
)
2458 strlcat(fmtnew
, "<+D>", sz
);
2460 /* If enabled, only show the iconic count if there are iconic wins. */
2461 if (iconic_enabled
&& r
!= NULL
&& r
->ws
!= NULL
)
2462 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2464 strlcat(fmtnew
, "{+M}", sz
);
2468 strlcat(fmtnew
, "+3<", sz
);
2470 if (clock_enabled
) {
2471 strlcat(fmtnew
, fmtexp
, sz
);
2472 strlcat(fmtnew
, "+4<", sz
);
2475 /* bar_urgent already adds the space before the last asterisk */
2477 strlcat(fmtnew
, (urgent_collapse
? "*+U*+4<" : "* +U*+4<"), sz
);
2479 if (window_class_enabled
) {
2480 strlcat(fmtnew
, "+C", sz
);
2481 if (!window_instance_enabled
)
2482 strlcat(fmtnew
, "+4<", sz
);
2485 /* checks needed by the colon and floating strlcat(3) calls below */
2486 if (r
!= NULL
&& r
->ws
!= NULL
&& r
->ws
->focus
!= NULL
) {
2487 if (window_instance_enabled
) {
2488 if (window_class_enabled
)
2489 strlcat(fmtnew
, ":", sz
);
2490 strlcat(fmtnew
, "+T+4<", sz
);
2492 if (window_name_enabled
) {
2493 if (ABOVE(r
->ws
->focus
) || MAXIMIZED(r
->ws
->focus
))
2494 strlcat(fmtnew
, "+F ", sz
);
2495 strlcat(fmtnew
, "+64W ", sz
);
2499 /* finally add the action script output and the version */
2500 strlcat(fmtnew
, "+4<+A+4<+V", sz
);
2504 bar_replace_pad(char *tmp
, int *limit
, size_t sz
)
2506 /* special case; no limit given, pad one space, instead */
2507 if (*limit
== (int)sz
- 1)
2509 snprintf(tmp
, sz
, "%*s", *limit
, " ");
2512 /* replaces the bar format character sequences (like in tmux(1)) */
2514 bar_replace_seq(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t *offrep
,
2519 char tmp
[SWM_BAR_MAX
];
2520 int limit
, size
, count
;
2523 /* reset strlcat(3) buffer */
2526 /* get number, if any */
2529 if (sscanf(fmt
, "%d%n", &limit
, &size
) != 1)
2530 limit
= sizeof tmp
- 1;
2531 if (limit
<= 0 || limit
>= (int)sizeof tmp
)
2532 limit
= sizeof tmp
- 1;
2534 /* there is nothing to replace (ie EOL) */
2541 bar_replace_pad(tmp
, &limit
, sizeof tmp
);
2544 snprintf(tmp
, sizeof tmp
, "%s", bar_ext
);
2547 bar_window_class(tmp
, sizeof tmp
, r
);
2550 bar_workspace_name(tmp
, sizeof tmp
, r
);
2553 bar_window_state(tmp
, sizeof tmp
, r
);
2556 snprintf(tmp
, sizeof tmp
, "%d", r
->ws
->idx
+ 1);
2560 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
2564 snprintf(tmp
, sizeof tmp
, "%d", count
);
2567 snprintf(tmp
, sizeof tmp
, "%d", r
->s
->idx
+ 1);
2570 bar_window_class_instance(tmp
, sizeof tmp
, r
);
2573 snprintf(tmp
, sizeof tmp
, "%s", r
->ws
->stacker
);
2576 bar_window_instance(tmp
, sizeof tmp
, r
);
2579 bar_urgent(tmp
, sizeof tmp
);
2582 snprintf(tmp
, sizeof tmp
, "%s", bar_vertext
);
2585 bar_window_name(tmp
, sizeof tmp
, r
);
2588 /* unknown character sequence; copy as-is */
2589 snprintf(tmp
, sizeof tmp
, "+%c", *fmt
);
2595 if ((int)len
< limit
)
2597 while (limit
-- > 0) {
2598 if (*offrep
>= sz
- 1)
2600 fmtrep
[(*offrep
)++] = *ptr
++;
2608 bar_replace(char *fmt
, char *fmtrep
, struct swm_region
*r
, size_t sz
)
2613 while (*fmt
!= '\0') {
2615 /* skip ordinary characters */
2618 fmtrep
[off
++] = *fmt
++;
2622 /* character sequence found; replace it */
2623 fmt
= bar_replace_seq(fmt
, fmtrep
, r
, &off
, sz
);
2632 bar_fmt_expand(char *fmtexp
, size_t sz
)
2639 /* start by grabbing the current time and date */
2641 localtime_r(&tmt
, &tm
);
2643 /* figure out what to expand */
2644 if (bar_format
!= NULL
)
2646 else if (bar_format
== NULL
&& clock_enabled
)
2648 /* if nothing to expand bail out */
2654 /* copy as-is, just in case the format shouldn't be expanded below */
2655 strlcpy(fmtexp
, fmt
, sz
);
2656 /* finally pass the string through strftime(3) */
2657 #ifndef SWM_DENY_CLOCK_FORMAT
2658 if ((len
= strftime(fmtexp
, sz
, fmt
, &tm
)) == 0)
2659 warnx("format too long");
2664 /* Redraws a region bar; need to follow with xcb_flush() or focus_flush(). */
2666 bar_draw(struct swm_bar
*bar
)
2668 struct swm_region
*r
;
2669 char fmtexp
[SWM_BAR_MAX
], fmtnew
[SWM_BAR_MAX
];
2670 char fmtrep
[SWM_BAR_MAX
];
2672 /* expand the format by first passing it through strftime(3) */
2673 bar_fmt_expand(fmtexp
, sizeof fmtexp
);
2680 if (bar_enabled
&& r
->ws
->bar_enabled
)
2681 xcb_map_window(conn
, bar
->id
);
2683 xcb_unmap_window(conn
, bar
->id
);
2687 if (startup_exception
)
2688 snprintf(fmtrep
, sizeof fmtrep
, "total "
2689 "exceptions: %d, first exception: %s",
2693 bar_fmt(fmtexp
, fmtnew
, r
, sizeof fmtnew
);
2694 bar_replace(fmtnew
, fmtrep
, r
, sizeof fmtrep
);
2697 if (bar_font_legacy
)
2698 bar_print_legacy(r
, fmtrep
);
2700 bar_print(r
, fmtrep
);
2704 * Reads external script output; call when stdin is readable.
2705 * Returns 1 if bar_ext was updated; otherwise 0.
2708 bar_extra_update(void)
2711 char b
[SWM_BAR_MAX
];
2712 bool changed
= false;
2717 while (fgets(b
, sizeof(b
), stdin
) != NULL
) {
2720 if (b
[len
- 1] == '\n') {
2721 /* Remove newline. */
2724 /* "Clear" bar_ext. */
2727 /* Flush buffered output. */
2728 strlcpy(bar_ext
, bar_ext_buf
, sizeof(bar_ext
));
2729 bar_ext_buf
[0] = '\0';
2731 /* Append new output to bar. */
2732 strlcat(bar_ext
, b
, sizeof(bar_ext
));
2736 /* Buffer output. */
2737 strlcat(bar_ext_buf
, b
, sizeof(bar_ext_buf
));
2742 if (errno
!= EAGAIN
) {
2743 warn("bar_action failed");
2752 bar_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
2754 struct swm_region
*tmpr
;
2757 /* suppress unused warnings since vars are needed */
2762 DNPRINTF(SWM_D_BAR
, "bar_toggle\n");
2765 case SWM_ARG_ID_BAR_TOGGLE_WS
:
2766 /* Only change if master switch is enabled. */
2768 r
->ws
->bar_enabled
= !r
->ws
->bar_enabled
;
2770 bar_enabled
= r
->ws
->bar_enabled
= true;
2772 case SWM_ARG_ID_BAR_TOGGLE
:
2773 bar_enabled
= !bar_enabled
;
2777 /* update bars as necessary */
2778 num_screens
= get_screen_count();
2779 for (i
= 0; i
< num_screens
; i
++)
2780 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
2782 if (bar_enabled
&& tmpr
->ws
->bar_enabled
)
2783 xcb_map_window(conn
, tmpr
->bar
->id
);
2785 xcb_unmap_window(conn
, tmpr
->bar
->id
);
2788 /* Restack all regions and redraw bar. */
2789 num_screens
= get_screen_count();
2790 for (i
= 0; i
< num_screens
; i
++)
2791 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
2793 bar_draw(tmpr
->bar
);
2800 bar_extra_setup(void)
2802 /* do this here because the conf file is in memory */
2803 if (!bar_extra
&& bar_argv
[0]) {
2804 /* launch external status app */
2806 if (pipe(bar_pipe
) == -1)
2807 err(1, "pipe error");
2808 socket_setnonblock(bar_pipe
[0]);
2809 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
2811 /* Set stdin to read from the pipe. */
2812 if (dup2(bar_pipe
[0], STDIN_FILENO
) == -1)
2815 /* Set stdout to write to the pipe. */
2816 if (dup2(bar_pipe
[1], STDOUT_FILENO
) == -1)
2819 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
2820 err(1, "could not disable SIGPIPE");
2821 switch (bar_pid
= fork()) {
2823 err(1, "cannot fork");
2827 execvp(bar_argv
[0], bar_argv
);
2828 err(1, "%s external app failed", bar_argv
[0]);
2830 default: /* parent */
2835 atexit(kill_bar_extra_atexit
);
2840 kill_bar_extra_atexit(void)
2843 kill(bar_pid
, SIGTERM
);
2851 while ((s
= index(s
, '-'))) {
2856 return (count
== 14);
2862 char *default_string
;
2863 char **missing_charsets
;
2864 int num_missing_charsets
= 0;
2868 XFreeFontSet(display
, bar_fs
);
2872 DNPRINTF(SWM_D_INIT
, "fontset_init: loading bar_fonts: %s\n",
2875 bar_fs
= XCreateFontSet(display
, bar_fonts
, &missing_charsets
,
2876 &num_missing_charsets
, &default_string
);
2878 if (num_missing_charsets
> 0) {
2879 warnx("Unable to load charset(s):");
2881 for (i
= 0; i
< num_missing_charsets
; ++i
)
2882 warnx("%s", missing_charsets
[i
]);
2884 XFreeStringList(missing_charsets
);
2886 if (strcmp(default_string
, ""))
2887 warnx("Glyphs from those sets will be replaced "
2888 "by '%s'.", default_string
);
2890 warnx("Glyphs from those sets won't be drawn.");
2894 errx(1, "Error creating font set structure.");
2896 bar_fs_extents
= XExtentsOfFontSet(bar_fs
);
2898 bar_height
= bar_fs_extents
->max_logical_extent
.height
+
2899 2 * bar_border_width
;
2906 xft_init(struct swm_region
*r
)
2908 char *font
, *str
, *search
;
2911 if (bar_font
== NULL
) {
2912 if ((search
= str
= strdup(bar_fonts
)) == NULL
)
2913 errx(1, "insufficient memory.");
2915 while ((font
= strsep(&search
, ",")) != NULL
) {
2919 DNPRINTF(SWM_D_INIT
, "xft_init: try font %s\n", font
);
2922 bar_font
= XftFontOpenXlfd(display
, r
->s
->idx
,
2925 bar_font
= XftFontOpenName(display
, r
->s
->idx
,
2929 if (bar_font
== NULL
) {
2930 warnx("unable to load font %s", font
);
2933 DNPRINTF(SWM_D_INIT
, "successfully opened "
2941 if (bar_font
== NULL
)
2942 errx(1, "unable to open a font");
2944 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].pixel
, color
);
2946 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2947 DefaultColormap(display
, r
->s
->idx
), &color
, &bar_font_color
))
2948 warn("Xft error: unable to allocate color.");
2950 PIXEL_TO_XRENDERCOLOR(r
->s
->c
[SWM_S_COLOR_BAR
].pixel
, color
);
2952 if (!XftColorAllocValue(display
, DefaultVisual(display
, r
->s
->idx
),
2953 DefaultColormap(display
, r
->s
->idx
), &color
, &search_font_color
))
2954 warn("Xft error: unable to allocate color.");
2956 bar_height
= bar_font
->height
+ 2 * bar_border_width
;
2963 bar_setup(struct swm_region
*r
)
2965 xcb_screen_t
*screen
;
2968 DNPRINTF(SWM_D_BAR
, "bar_setup: screen %d.\n",
2971 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
2972 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
2977 if ((r
->bar
= calloc(1, sizeof(struct swm_bar
))) == NULL
)
2978 err(1, "bar_setup: calloc: failed to allocate memory.");
2980 if (bar_font_legacy
)
2987 Y(r
->bar
) = bar_at_bottom
? (Y(r
) + HEIGHT(r
) - bar_height
) : Y(r
);
2988 WIDTH(r
->bar
) = WIDTH(r
) - 2 * bar_border_width
;
2989 HEIGHT(r
->bar
) = bar_height
- 2 * bar_border_width
;
2991 /* Assume region is unfocused when we create the bar. */
2992 r
->bar
->id
= xcb_generate_id(conn
);
2993 wa
[0] = r
->s
->c
[SWM_S_COLOR_BAR
].pixel
;
2994 wa
[1] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
;
2995 wa
[2] = XCB_EVENT_MASK_EXPOSURE
| XCB_EVENT_MASK_POINTER_MOTION
|
2996 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
2998 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->bar
->id
, r
->s
->root
,
2999 X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
), HEIGHT(r
->bar
),
3000 bar_border_width
, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
3001 XCB_COPY_FROM_PARENT
, XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
3002 | XCB_CW_EVENT_MASK
, wa
);
3004 /* Stack bar window above region window to start. */
3006 wa
[1] = XCB_STACK_MODE_ABOVE
;
3008 xcb_configure_window(conn
, r
->bar
->id
, XCB_CONFIG_WINDOW_SIBLING
|
3009 XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
3011 r
->bar
->buffer
= xcb_generate_id(conn
);
3012 xcb_create_pixmap(conn
, screen
->root_depth
, r
->bar
->buffer
, r
->bar
->id
,
3013 WIDTH(r
->bar
), HEIGHT(r
->bar
));
3016 xcb_randr_select_input(conn
, r
->bar
->id
,
3017 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE
);
3020 xcb_map_window(conn
, r
->bar
->id
);
3022 DNPRINTF(SWM_D_BAR
, "bar_setup: win %#x, (x,y) w x h: (%d,%d) "
3023 "%d x %d\n", WINID(r
->bar
), X(r
->bar
), Y(r
->bar
), WIDTH(r
->bar
),
3030 bar_cleanup(struct swm_region
*r
)
3034 xcb_destroy_window(conn
, r
->bar
->id
);
3035 xcb_free_pixmap(conn
, r
->bar
->buffer
);
3041 set_win_state(struct ws_win
*win
, uint8_t state
)
3043 uint16_t data
[2] = { state
, XCB_ATOM_NONE
};
3045 DNPRINTF(SWM_D_EVENT
, "set_win_state: win %#x, state: %u\n",
3051 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
, a_state
,
3052 a_state
, 32, 2, data
);
3056 get_win_state(xcb_window_t w
)
3058 xcb_get_property_reply_t
*r
;
3059 xcb_get_property_cookie_t c
;
3060 uint32_t result
= 0;
3062 c
= xcb_get_property(conn
, 0, w
, a_state
, a_state
, 0L, 2L);
3063 r
= xcb_get_property_reply(conn
, c
, NULL
);
3065 if (r
->type
== a_state
&& r
->format
== 32 && r
->length
== 2)
3066 result
= *((uint32_t *)xcb_get_property_value(r
));
3070 DNPRINTF(SWM_D_MISC
, "get_win_state property: win %#x state %u\n", w
,
3076 version(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3078 struct swm_region
*tmpr
;
3081 /* suppress unused warnings since vars are needed */
3086 bar_version
= !bar_version
;
3088 snprintf(bar_vertext
, sizeof bar_vertext
,
3089 "Version: %s Build: %s", SPECTRWM_VERSION
, buildstr
);
3091 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
3093 num_screens
= get_screen_count();
3094 for (i
= 0; i
< num_screens
; i
++) {
3095 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
) {
3096 bar_draw(tmpr
->bar
);
3103 client_msg(struct ws_win
*win
, xcb_atom_t a
, xcb_timestamp_t t
)
3105 xcb_client_message_event_t ev
;
3113 name
= get_atom_name(a
);
3114 DNPRINTF(SWM_D_EVENT
, "client_msg: win %#x, atom: %s(%u), "
3116 win
->id
, name
, a
, t
);
3120 bzero(&ev
, sizeof ev
);
3121 ev
.response_type
= XCB_CLIENT_MESSAGE
;
3122 ev
.window
= win
->id
;
3125 ev
.data
.data32
[0] = a
;
3126 ev
.data
.data32
[1] = t
;
3128 xcb_send_event(conn
, 0, win
->id
,
3129 XCB_EVENT_MASK_NO_EVENT
, (const char *)&ev
);
3132 /* synthetic response to a ConfigureRequest when not making a change */
3134 config_win(struct ws_win
*win
, xcb_configure_request_event_t
*ev
)
3136 xcb_configure_notify_event_t ce
;
3141 /* send notification of unchanged state. */
3142 bzero(&ce
, sizeof(ce
));
3143 ce
.response_type
= XCB_CONFIGURE_NOTIFY
;
3146 ce
.width
= WIDTH(win
);
3147 ce
.height
= HEIGHT(win
);
3148 ce
.border_width
= 0;
3149 ce
.override_redirect
= 0;
3154 ce
.window
= win
->id
;
3155 ce
.above_sibling
= XCB_WINDOW_NONE
;
3158 ce
.event
= ev
->window
;
3159 ce
.window
= ev
->window
;
3161 /* make response appear more WM_SIZE_HINTS-compliant */
3162 if (win
->sh
.flags
) {
3163 DNPRINTF(SWM_D_MISC
, "config_win: hints: win %#x,"
3164 " sh.flags: %u, min: %d x %d, max: %d x %d, inc: "
3165 "%d x %d\n", win
->id
, win
->sh
.flags
, SH_MIN_W(win
),
3166 SH_MIN_H(win
), SH_MAX_W(win
), SH_MAX_H(win
),
3167 SH_INC_W(win
), SH_INC_H(win
));
3172 /* the hint may be set... to 0! */
3173 if (SH_MIN_W(win
) > 0 && ce
.width
< SH_MIN_W(win
))
3174 ce
.width
= SH_MIN_W(win
);
3175 if (SH_MIN_H(win
) > 0 && ce
.height
< SH_MIN_H(win
))
3176 ce
.height
= SH_MIN_H(win
);
3181 /* may also be advertized as 0 */
3182 if (SH_MAX_W(win
) > 0 && ce
.width
> SH_MAX_W(win
))
3183 ce
.width
= SH_MAX_W(win
);
3184 if (SH_MAX_H(win
) > 0 && ce
.height
> SH_MAX_H(win
))
3185 ce
.height
= SH_MAX_H(win
);
3188 /* resize increment. */
3190 if (SH_INC_W(win
) > 1 && ce
.width
> SH_INC_W(win
))
3191 ce
.width
-= (ce
.width
- SH_MIN_W(win
)) %
3193 if (SH_INC_H(win
) > 1 && ce
.height
> SH_INC_H(win
))
3194 ce
.height
-= (ce
.height
- SH_MIN_H(win
)) %
3198 /* adjust x and y for requested border_width. */
3199 ce
.x
+= ev
->border_width
;
3200 ce
.y
+= ev
->border_width
;
3202 ce
.above_sibling
= ev
->sibling
;
3205 DNPRINTF(SWM_D_MISC
, "config_win: ewmh: %s, win %#x, (x,y) w x h: "
3206 "(%d,%d) %d x %d, border: %d\n", YESNO(ev
== NULL
), win
->id
, ce
.x
,
3207 ce
.y
, ce
.width
, ce
.height
, ce
.border_width
);
3209 xcb_send_event(conn
, 0, win
->id
, XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
3214 count_win(struct workspace
*ws
, bool count_transient
)
3219 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
3220 if (!count_transient
&& FLOATING(win
))
3226 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
3232 quit(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3234 /* suppress unused warnings since vars are needed */
3239 DNPRINTF(SWM_D_MISC
, "quit\n");
3244 lower_window(struct ws_win
*win
)
3246 struct ws_win
*target
= NULL
;
3247 struct workspace
*ws
;
3249 DNPRINTF(SWM_D_EVENT
, "lower_window: win %#x\n", WINID(win
));
3256 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3257 if (target
== win
|| ICONIC(target
))
3259 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3262 if (win
->transient
== target
->transient
)
3264 if (win
->transient
== target
->id
)
3267 if (FULLSCREEN(target
))
3269 if (FULLSCREEN(win
))
3271 if (MAXIMIZED(target
))
3275 if (ABOVE(target
) || TRANS(target
))
3277 if (ABOVE(win
) || TRANS(win
))
3281 /* Change stack position. */
3282 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3284 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3286 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3288 update_win_stacking(win
);
3291 if (swm_debug
& SWM_D_STACK
) {
3292 DPRINTF("=== stacking order (top down) === \n");
3293 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3294 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3295 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3296 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3297 YESNO(ICONIC(target
)));
3301 DNPRINTF(SWM_D_EVENT
, "lower_window: done\n");
3305 raise_window(struct ws_win
*win
)
3307 struct ws_win
*target
= NULL
;
3308 struct workspace
*ws
;
3310 DNPRINTF(SWM_D_EVENT
, "raise_window: win %#x\n", WINID(win
));
3317 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3318 if (target
== win
|| ICONIC(target
))
3320 if (ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
3322 if (TRANS(win
) && (win
->transient
== target
->transient
||
3323 win
->transient
== target
->id
))
3325 if (FULLSCREEN(win
))
3327 if (FULLSCREEN(target
))
3331 if (MAXIMIZED(target
))
3333 if (ABOVE(win
) || TRANS(win
) ||
3334 (win
->ws
->focus
== win
&& ws
->always_raise
))
3336 if (!ABOVE(target
) && !TRANS(target
))
3340 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
3342 TAILQ_INSERT_BEFORE(target
, win
, stack_entry
);
3344 TAILQ_INSERT_TAIL(&ws
->stack
, win
, stack_entry
);
3346 update_win_stacking(win
);
3349 if (swm_debug
& SWM_D_STACK
) {
3350 DPRINTF("=== stacking order (top down) === \n");
3351 TAILQ_FOREACH(target
, &ws
->stack
, stack_entry
) {
3352 DPRINTF("win %#x, fs: %s, maximized: %s, above: %s, "
3353 "iconic: %s\n", target
->id
, YESNO(FULLSCREEN(target
)),
3354 YESNO(MAXIMIZED(target
)), YESNO(ABOVE(target
)),
3355 YESNO(ICONIC(target
)));
3359 DNPRINTF(SWM_D_EVENT
, "raise_window: done\n");
3363 update_win_stacking(struct ws_win
*win
)
3365 struct ws_win
*sibling
;
3369 struct swm_region
*r
;
3372 if (win
== NULL
|| (r
= win
->ws
->r
) == NULL
)
3375 if (win
->frame
== XCB_WINDOW_NONE
) {
3376 DNPRINTF(SWM_D_EVENT
, "update_window_stacking: win %#x not "
3377 "reparented.\n", win
->id
);
3381 sibling
= TAILQ_NEXT(win
, stack_entry
);
3382 if (sibling
!= NULL
&& (FLOATING(win
) == FLOATING(sibling
) ||
3383 (win
->ws
->always_raise
&& win
->ws
->focus
== win
)))
3384 val
[0] = sibling
->frame
;
3385 else if (FLOATING(win
) || (win
->ws
->always_raise
&&
3386 win
->ws
->focus
== win
))
3387 val
[0] = r
->bar
->id
;
3391 DNPRINTF(SWM_D_EVENT
, "update_win_stacking: win %#x (%#x), "
3392 "sibling %#x\n", win
->frame
, win
->id
, val
[0]);
3394 val
[1] = XCB_STACK_MODE_ABOVE
;
3396 xcb_configure_window(conn
, win
->frame
, XCB_CONFIG_WINDOW_SIBLING
|
3397 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
3400 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
)
3406 map_window(struct ws_win
*win
)
3411 DNPRINTF(SWM_D_EVENT
, "map_window: win %#x, mapped: %s\n",
3412 win
->id
, YESNO(win
->mapped
));
3417 xcb_map_window(conn
, win
->frame
);
3418 xcb_map_window(conn
, win
->id
);
3419 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
3424 unmap_window(struct ws_win
*win
)
3429 DNPRINTF(SWM_D_EVENT
, "unmap_window: win %#x, mapped: %s\n", win
->id
,
3430 YESNO(win
->mapped
));
3435 xcb_unmap_window(conn
, win
->id
);
3436 xcb_unmap_window(conn
, win
->frame
);
3437 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
3438 win
->mapped
= false;
3445 int i
, j
, num_screens
;
3447 num_screens
= get_screen_count();
3448 for (i
= 0; i
< num_screens
; i
++)
3449 for (j
= 0; j
< workspace_limit
; j
++)
3450 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3455 fake_keypress(struct ws_win
*win
, xcb_keysym_t keysym
, uint16_t modifiers
)
3457 xcb_key_press_event_t event
;
3458 xcb_keycode_t
*keycode
;
3463 keycode
= xcb_key_symbols_get_keycode(syms
, keysym
);
3465 DNPRINTF(SWM_D_MISC
, "fake_keypress: win %#x, keycode %u\n",
3468 bzero(&event
, sizeof(event
));
3469 event
.event
= win
->id
;
3470 event
.root
= win
->s
->root
;
3471 event
.child
= XCB_WINDOW_NONE
;
3472 event
.time
= XCB_CURRENT_TIME
;
3473 event
.event_x
= X(win
);
3474 event
.event_y
= Y(win
);
3477 event
.same_screen
= 1;
3478 event
.detail
= *keycode
;
3479 event
.state
= modifiers
;
3481 event
.response_type
= XCB_KEY_PRESS
;
3482 xcb_send_event(conn
, 1, win
->id
,
3483 XCB_EVENT_MASK_KEY_PRESS
, (const char *)&event
);
3485 event
.response_type
= XCB_KEY_RELEASE
;
3486 xcb_send_event(conn
, 1, win
->id
,
3487 XCB_EVENT_MASK_KEY_RELEASE
, (const char *)&event
);
3493 restart(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
3495 /* suppress unused warning since var is needed */
3500 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
3504 execvp(start_argv
[0], start_argv
);
3505 warn("execvp failed");
3506 quit(NULL
, NULL
, NULL
);
3510 get_pointer_win(xcb_window_t root
)
3512 struct ws_win
*win
= NULL
;
3513 xcb_query_pointer_reply_t
*r
;
3515 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: root: %#x.\n", root
);
3517 r
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, root
), NULL
);
3519 win
= find_window(r
->child
);
3521 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: %#x.\n",
3524 DNPRINTF(SWM_D_EVENT
, "get_pointer_win: none.\n");
3533 center_pointer(struct swm_region
*r
)
3537 if (!warp_pointer
|| r
== NULL
)
3542 DNPRINTF(SWM_D_EVENT
, "center_pointer: win %#x.\n", WINID(win
));
3544 if (win
&& win
->mapped
)
3545 xcb_warp_pointer(conn
, XCB_NONE
, win
->frame
, 0, 0, 0, 0,
3546 WIDTH(win
) / 2, HEIGHT(win
) / 2);
3548 xcb_warp_pointer(conn
, XCB_NONE
, r
->id
, 0, 0, 0, 0,
3549 WIDTH(r
) / 2, HEIGHT(r
) / 2);
3553 root_to_region(xcb_window_t root
, int check
)
3556 struct swm_region
*r
= NULL
;
3558 xcb_query_pointer_reply_t
*qpr
;
3559 xcb_get_input_focus_reply_t
*gifr
;
3561 DNPRINTF(SWM_D_MISC
, "root_to_region: win %#x\n", root
);
3563 num_screens
= get_screen_count();
3564 for (i
= 0; i
< num_screens
; i
++)
3565 if (screens
[i
].root
== root
)
3568 if (check
& SWM_CK_REGION
)
3569 r
= screens
[i
].r_focus
;
3571 if (r
== NULL
&& check
& SWM_CK_FOCUS
) {
3572 /* Try to find an actively focused window */
3573 gifr
= xcb_get_input_focus_reply(conn
,
3574 xcb_get_input_focus(conn
), NULL
);
3576 cfw
= find_window(gifr
->focus
);
3577 if (cfw
&& cfw
->ws
->r
)
3584 if (r
== NULL
&& check
& SWM_CK_POINTER
) {
3585 /* No region with an active focus; try to use pointer. */
3586 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
,
3587 screens
[i
].root
), NULL
);
3589 DNPRINTF(SWM_D_MISC
, "root_to_region: pointer: "
3590 "(%d,%d)\n", qpr
->root_x
, qpr
->root_y
);
3591 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3592 if (X(r
) <= qpr
->root_x
&&
3593 qpr
->root_x
< MAX_X(r
) &&
3594 Y(r
) <= qpr
->root_y
&&
3595 qpr
->root_y
< MAX_Y(r
))
3602 if (r
== NULL
&& check
& SWM_CK_FALLBACK
)
3603 r
= TAILQ_FIRST(&screens
[i
].rl
);
3605 DNPRINTF(SWM_D_MISC
, "root_to_region: idx: %d\n", get_region_index(r
));
3611 find_region(xcb_window_t id
)
3613 struct swm_region
*r
;
3616 num_screens
= get_screen_count();
3617 for (i
= 0; i
< num_screens
; i
++)
3618 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3626 find_bar(xcb_window_t id
)
3628 struct swm_region
*r
;
3631 num_screens
= get_screen_count();
3632 for (i
= 0; i
< num_screens
; i
++)
3633 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3634 if (r
->bar
&& r
->bar
->id
== id
)
3641 find_frame_window(xcb_window_t id
) {
3642 struct swm_region
*r
;
3646 num_screens
= get_screen_count();
3647 for (i
= 0; i
< num_screens
; i
++)
3648 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3649 TAILQ_FOREACH(w
, &r
->ws
->winlist
, entry
)
3657 find_window(xcb_window_t id
)
3659 struct ws_win
*win
= NULL
;
3660 int i
, j
, num_screens
;
3661 xcb_query_tree_reply_t
*qtr
;
3663 DNPRINTF(SWM_D_MISC
, "find_window: id: %#x\n", id
);
3665 num_screens
= get_screen_count();
3666 for (i
= 0; i
< num_screens
; i
++)
3667 for (j
= 0; j
< workspace_limit
; j
++)
3668 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
3669 if (id
== win
->id
|| id
== win
->frame
)
3673 /* If window isn't top-level, try to find managed ancestor. */
3674 qtr
= xcb_query_tree_reply(conn
, xcb_query_tree(conn
, id
), NULL
);
3676 if (qtr
->parent
!= XCB_WINDOW_NONE
&& qtr
->parent
!= qtr
->root
)
3677 win
= find_window(qtr
->parent
);
3681 DNPRINTF(SWM_D_MISC
, "find_window: found child %#x "
3682 "of %#x.\n", win
->id
, qtr
->parent
);
3692 find_unmanaged_window(xcb_window_t id
)
3695 int i
, j
, num_screens
;
3697 num_screens
= get_screen_count();
3698 for (i
= 0; i
< num_screens
; i
++)
3699 for (j
= 0; j
< workspace_limit
; j
++)
3700 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].unmanagedlist
,
3708 spawn(int ws_idx
, union arg
*args
, bool close_fd
)
3713 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
3715 close(xcb_get_file_descriptor(conn
));
3717 setenv("LD_PRELOAD", SWM_LIB
, 1);
3719 if (asprintf(&ret
, "%d", ws_idx
) == -1) {
3720 warn("spawn: asprintf SWM_WS");
3723 setenv("_SWM_WS", ret
, 1);
3727 if (asprintf(&ret
, "%d", getpid()) == -1) {
3728 warn("spawn: asprintf _SWM_PID");
3731 setenv("_SWM_PID", ret
, 1);
3735 if (setsid() == -1) {
3736 warn("spawn: setsid");
3742 * close stdin and stdout to prevent interaction between apps
3743 * and the baraction script
3744 * leave stderr open to record errors
3746 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) == -1) {
3747 warn("spawn: open");
3750 dup2(fd
, STDIN_FILENO
);
3751 dup2(fd
, STDOUT_FILENO
);
3756 execvp(args
->argv
[0], args
->argv
);
3758 warn("spawn: execvp");
3763 kill_refs(struct ws_win
*win
)
3765 struct workspace
*ws
;
3767 int i
, j
, num_screens
;
3772 num_screens
= get_screen_count();
3773 for (i
= 0; i
< num_screens
; i
++) {
3774 for (j
= 0; j
< workspace_limit
; j
++) {
3775 ws
= &screens
[i
].ws
[j
];
3777 if (win
== ws
->focus
)
3779 if (win
== ws
->focus_prev
)
3780 ws
->focus_prev
= NULL
;
3781 if (win
== ws
->focus_pending
)
3782 ws
->focus_pending
= NULL
;
3785 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
3786 if (win
== w
->focus_child
)
3787 w
->focus_child
= NULL
;
3793 validate_win(struct ws_win
*testwin
)
3796 struct workspace
*ws
;
3797 struct swm_region
*r
;
3798 int i
, x
, num_screens
;
3800 if (testwin
== NULL
)
3803 num_screens
= get_screen_count();
3804 for (i
= 0; i
< num_screens
; i
++)
3805 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3806 for (x
= 0; x
< workspace_limit
; x
++) {
3808 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
3816 validate_ws(struct workspace
*testws
)
3818 struct swm_region
*r
;
3819 struct workspace
*ws
;
3820 int i
, x
, num_screens
;
3822 /* validate all ws */
3823 num_screens
= get_screen_count();
3824 for (i
= 0; i
< num_screens
; i
++)
3825 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3826 for (x
= 0; x
< workspace_limit
; x
++) {
3835 unfocus_win(struct ws_win
*win
)
3837 xcb_window_t none
= XCB_WINDOW_NONE
;
3839 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: win %#x\n", WINID(win
));
3844 if (win
->ws
== NULL
) {
3845 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL ws.\n");
3849 if (validate_ws(win
->ws
)) {
3850 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid ws.\n");
3854 if (win
->ws
->r
== NULL
) {
3855 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: NULL region.\n");
3859 if (validate_win(win
)) {
3860 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: invalid win.\n");
3865 if (win
->ws
->focus
== win
) {
3866 win
->ws
->focus
= NULL
;
3867 win
->ws
->focus_prev
= win
;
3870 if (validate_win(win
->ws
->focus
)) {
3871 kill_refs(win
->ws
->focus
);
3872 win
->ws
->focus
= NULL
;
3875 if (validate_win(win
->ws
->focus_prev
)) {
3876 kill_refs(win
->ws
->focus_prev
);
3877 win
->ws
->focus_prev
= NULL
;
3882 /* Raise window to "top unfocused position." */
3883 if (win
->ws
->always_raise
)
3886 /* Update border width */
3887 if (win
->bordered
&& (win
->quirks
& SWM_Q_MINIMALBORDER
) &&
3890 X(win
) += border_width
;
3891 Y(win
) += border_width
;
3895 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
3896 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1, &none
);
3898 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: done.\n");
3902 focus_win(struct ws_win
*win
)
3904 struct ws_win
*cfw
= NULL
, *parent
= NULL
, *w
, *tmpw
;
3905 struct workspace
*ws
;
3906 xcb_get_input_focus_reply_t
*gifr
= NULL
;
3907 xcb_get_window_attributes_reply_t
*war
= NULL
;
3909 DNPRINTF(SWM_D_FOCUS
, "focus_win: win %#x\n", WINID(win
));
3911 if (win
== NULL
|| win
->ws
== NULL
|| !win
->mapped
)
3916 if (validate_ws(ws
))
3919 if (validate_win(win
)) {
3924 gifr
= xcb_get_input_focus_reply(conn
, xcb_get_input_focus(conn
), NULL
);
3926 DNPRINTF(SWM_D_FOCUS
, "focus_win: cur focus: %#x\n",
3929 cfw
= find_window(gifr
->focus
);
3932 if (cfw
->ws
!= ws
&& cfw
->ws
->r
!= NULL
&&
3933 cfw
->frame
!= XCB_WINDOW_NONE
) {
3940 war
= xcb_get_window_attributes_reply(conn
,
3941 xcb_get_window_attributes(conn
, gifr
->focus
), NULL
);
3942 if (war
&& war
->override_redirect
&& ws
->focus
== win
) {
3943 DNPRINTF(SWM_D_FOCUS
, "focus_win: skip refocus "
3944 "from override_redirect.\n");
3950 if (ws
->focus
!= win
) {
3951 if (ws
->focus
&& ws
->focus
!= cfw
)
3952 unfocus_win(ws
->focus
);
3956 /* Clear focus child redirect. */
3957 win
->focus_child
= NULL
;
3959 /* If transient, adjust parent's focus child for focus_magic. */
3961 parent
= find_window(win
->transient
);
3962 if (parent
&& parent
->focus_child
!= win
)
3963 parent
->focus_child
= win
;
3966 /* Update window border even if workspace is hidden. */
3970 DNPRINTF(SWM_D_FOCUS
, "focus_win: already focused.\n");
3975 /* Set input focus if no input hint, or indicated by hint. */
3976 if (ACCEPTS_FOCUS(win
)) {
3977 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
3978 " revert-to: parent, time: %#x\n", win
->id
,
3980 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
3981 win
->id
, last_event_time
);
3982 } else if (!win
->take_focus
) {
3983 DNPRINTF(SWM_D_FOCUS
, "focus_win: set_input_focus: %#x,"
3984 " revert-to: parent, time: 0\n", ws
->r
->id
);
3985 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
3986 ws
->r
->id
, XCB_CURRENT_TIME
);
3989 /* Tell app it can adjust focus to a specific window. */
3990 if (win
->take_focus
) {
3991 /* java is special; always tell parent */
3992 if (TRANS(win
) && win
->java
)
3993 client_msg(parent
, a_takefocus
,
3996 client_msg(win
, a_takefocus
, last_event_time
);
3999 if (ws
->cur_layout
->flags
& SWM_L_MAPONFOCUS
||
4001 /* If a parent exists, map it first. */
4003 raise_window(parent
);
4006 /* Map siblings next. */
4007 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
,
4009 if (w
!= win
&& !ICONIC(w
) &&
4010 w
->transient
== parent
->id
) {
4016 /* Map focused window. */
4020 /* Stack any children of focus window. */
4021 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
4022 if (w
->transient
== win
->id
&& !ICONIC(w
)) {
4026 } else if (tile_gap
< 0 && !ABOVE(win
)) {
4028 * Windows overlap in the layout.
4029 * Raise focused win above all tiled wins.
4037 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4038 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4041 bar_draw(ws
->r
->bar
);
4047 DNPRINTF(SWM_D_FOCUS
, "focus_win: done.\n");
4050 /* If a child window should have focus instead, return it. */
4052 get_focus_magic(struct ws_win
*win
)
4054 struct ws_win
*parent
= NULL
;
4055 struct ws_win
*child
= NULL
;
4057 DNPRINTF(SWM_D_FOCUS
, "get_focus_magic: win %#x\n", WINID(win
));
4062 parent
= find_window(win
->transient
);
4064 /* If parent prefers focus elsewhere, then try to do so. */
4065 if (parent
&& (child
= parent
->focus_child
)) {
4066 if (validate_win(child
) == 0 && child
->mapped
)
4069 parent
->focus_child
= NULL
;
4073 /* If this window prefers focus elsewhere, then try to do so. */
4074 if ((child
= win
->focus_child
)) {
4075 if (validate_win(child
) == 0 && child
->mapped
)
4078 win
->focus_child
= NULL
;
4085 event_drain(uint8_t rt
)
4087 xcb_generic_event_t
*evt
;
4089 /* ensure all pending requests have been processed before filtering. */
4091 while ((evt
= get_next_event(false))) {
4092 if (XCB_EVENT_RESPONSE_TYPE(evt
) != rt
)
4100 set_region(struct swm_region
*r
)
4102 struct swm_region
*rf
;
4109 /* Unfocus old region bar. */
4114 xcb_change_window_attributes(conn
, rf
->bar
->id
,
4115 XCB_CW_BORDER_PIXEL
,
4116 &r
->s
->c
[SWM_S_COLOR_BAR_BORDER_UNFOCUS
].pixel
);
4119 if (rf
!= NULL
&& rf
!= r
&& (X(rf
) != X(r
) || Y(rf
) != Y(r
) ||
4120 WIDTH(rf
) != WIDTH(r
) || HEIGHT(rf
) != HEIGHT(r
))) {
4121 /* Set _NET_DESKTOP_GEOMETRY. */
4123 vals
[1] = HEIGHT(r
);
4124 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, r
->s
->root
,
4125 ewmh
[_NET_DESKTOP_GEOMETRY
].atom
, XCB_ATOM_CARDINAL
, 32, 2,
4129 /* Set region bar border to focus_color. */
4130 xcb_change_window_attributes(conn
, r
->bar
->id
,
4131 XCB_CW_BORDER_PIXEL
, &r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].pixel
);
4135 ewmh_update_current_desktop();
4139 focus_region(struct swm_region
*r
)
4142 struct swm_region
*old_r
;
4147 old_r
= r
->s
->r_focus
;
4150 nfw
= get_region_focus(r
);
4154 /* New region is empty; need to manually unfocus win. */
4156 unfocus_win(old_r
->ws
->focus
);
4157 /* Clear bar since empty. */
4158 bar_draw(old_r
->bar
);
4161 DNPRINTF(SWM_D_FOCUS
, "focus_region: set_input_focus: %#x, "
4162 "revert-to: parent, time: 0\n", r
->id
);
4163 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4170 switchws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4172 struct swm_region
*this_r
, *other_r
;
4174 struct workspace
*new_ws
, *old_ws
;
4175 xcb_window_t none
= XCB_WINDOW_NONE
;
4176 int wsid
= args
->id
;
4177 bool unmap_old
= false;
4184 if (wsid
>= workspace_limit
)
4188 old_ws
= this_r
->ws
;
4189 new_ws
= &this_r
->s
->ws
[wsid
];
4191 DNPRINTF(SWM_D_WS
, "switchws: screen[%d]:%dx%d+%d+%d: %d -> %d\n",
4192 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), old_ws
->idx
, wsid
);
4194 if (new_ws
== NULL
|| old_ws
== NULL
)
4196 if (new_ws
== old_ws
)
4199 other_r
= new_ws
->r
;
4200 if (other_r
&& workspace_clamp
) {
4201 DNPRINTF(SWM_D_WS
, "switchws: ws clamped.\n");
4204 DNPRINTF(SWM_D_WS
, "switchws: warping focus to region "
4205 "with ws %d.\n", wsid
);
4206 focus_region(other_r
);
4207 center_pointer(other_r
);
4213 if ((win
= old_ws
->focus
) != NULL
) {
4216 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->s
->root
,
4217 ewmh
[_NET_ACTIVE_WINDOW
].atom
, XCB_ATOM_WINDOW
, 32, 1,
4222 /* the other ws is visible in another region, exchange them */
4223 other_r
->ws_prior
= new_ws
;
4224 other_r
->ws
= old_ws
;
4225 old_ws
->r
= other_r
;
4227 /* the other workspace is hidden, hide this one */
4232 this_r
->ws_prior
= old_ws
;
4233 this_r
->ws
= new_ws
;
4236 /* Set focus_pending before stacking, if needed. */
4237 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& (!new_ws
->focus_pending
||
4238 validate_win(new_ws
->focus_pending
))) {
4239 new_ws
->focus_pending
= get_region_focus(new_ws
->r
);
4240 new_ws
->focus
= new_ws
->focus_prev
;
4241 new_ws
->focus_prev
= NULL
;
4244 new_ws
->state
= SWM_WS_STATE_MAPPING
;
4249 /* unmap old windows */
4251 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
4253 old_ws
->state
= SWM_WS_STATE_HIDDEN
;
4256 /* if workspaces were swapped, then don't wait to set focus */
4257 if (old_ws
->r
&& focus_mode
!= SWM_FOCUS_FOLLOW
) {
4258 if (new_ws
->focus_pending
) {
4259 focus_win(new_ws
->focus_pending
);
4260 new_ws
->focus_pending
= NULL
;
4264 /* Clear bar and set focus on region input win if new ws is empty. */
4265 if (new_ws
->focus_pending
== NULL
&& new_ws
->focus
== NULL
) {
4266 DNPRINTF(SWM_D_FOCUS
, "switchws: set_input_focus: %#x, "
4267 "revert-to: parent, time: 0\n", r
->id
);
4268 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
, r
->id
,
4273 ewmh_update_current_desktop();
4277 new_ws
->state
= SWM_WS_STATE_MAPPED
;
4279 DNPRINTF(SWM_D_WS
, "switchws: done.\n");
4283 cyclews(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4286 struct swm_screen
*s
= r
->s
;
4287 bool cycle_all
= false, mv
= false;
4289 DNPRINTF(SWM_D_WS
, "cyclews: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4290 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4296 case SWM_ARG_ID_CYCLEWS_MOVE_UP
:
4299 case SWM_ARG_ID_CYCLEWS_UP_ALL
:
4302 case SWM_ARG_ID_CYCLEWS_UP
:
4303 a
.id
= (a
.id
< workspace_limit
- 1) ? a
.id
+ 1 : 0;
4305 case SWM_ARG_ID_CYCLEWS_MOVE_DOWN
:
4308 case SWM_ARG_ID_CYCLEWS_DOWN_ALL
:
4311 case SWM_ARG_ID_CYCLEWS_DOWN
:
4312 a
.id
= (a
.id
> 0) ? a
.id
- 1 : workspace_limit
- 1;
4319 (!cycle_empty
&& TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
)))
4321 if (!cycle_visible
&& s
->ws
[a
.id
].r
!= NULL
)
4325 send_to_ws(bp
, r
, &a
);
4327 switchws(bp
, r
, &a
);
4328 } while (a
.id
!= r
->ws
->idx
);
4330 DNPRINTF(SWM_D_FOCUS
, "cyclews: done\n");
4334 priorws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4340 DNPRINTF(SWM_D_WS
, "priorws: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4341 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4343 if (r
->ws_prior
== NULL
)
4346 a
.id
= r
->ws_prior
->idx
;
4347 switchws(bp
, r
, &a
);
4348 DNPRINTF(SWM_D_FOCUS
, "priorws: done\n");
4352 focusrg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4354 int ridx
= args
->id
, i
, num_screens
;
4355 struct swm_region
*rr
= NULL
;
4359 num_screens
= get_screen_count();
4360 /* do nothing if we don't have more than one screen */
4361 if (!(num_screens
> 1 || outputs
> 1))
4364 DNPRINTF(SWM_D_FOCUS
, "focusrg: id: %d\n", ridx
);
4366 rr
= TAILQ_FIRST(&r
->s
->rl
);
4367 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
4368 rr
= TAILQ_NEXT(rr
, entry
);
4376 DNPRINTF(SWM_D_FOCUS
, "focusrg: done\n");
4380 cyclerg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4383 struct swm_region
*rr
= NULL
;
4386 num_screens
= get_screen_count();
4387 /* do nothing if we don't have more than one screen */
4388 if (!(num_screens
> 1 || outputs
> 1))
4392 DNPRINTF(SWM_D_FOCUS
, "cyclerg: id: %d, region: %d\n", args
->id
, i
);
4395 case SWM_ARG_ID_CYCLERG_UP
:
4396 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4397 rr
= TAILQ_NEXT(r
, entry
);
4399 rr
= TAILQ_FIRST(&screens
[i
].rl
);
4401 case SWM_ARG_ID_CYCLERG_DOWN
:
4402 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4403 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
4405 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
4414 case SWM_ARG_ID_CYCLERG_UP
:
4415 case SWM_ARG_ID_CYCLERG_DOWN
:
4420 case SWM_ARG_ID_CYCLERG_MOVE_UP
:
4421 case SWM_ARG_ID_CYCLERG_MOVE_DOWN
:
4423 switchws(bp
, r
, &a
);
4429 DNPRINTF(SWM_D_FOCUS
, "cyclerg: done\n");
4432 /* Sorts transients after parent. */
4434 sort_windows(struct ws_win_list
*wl
)
4436 struct ws_win
*win
, *parent
, *nxt
;
4441 for (win
= TAILQ_FIRST(wl
); win
!= TAILQ_END(wl
); win
= nxt
) {
4442 nxt
= TAILQ_NEXT(win
, entry
);
4444 parent
= find_window(win
->transient
);
4445 if (parent
== NULL
) {
4446 warnx("not possible bug");
4449 TAILQ_REMOVE(wl
, win
, entry
);
4450 TAILQ_INSERT_AFTER(wl
, parent
, win
, entry
);
4456 swapwin(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4458 struct ws_win
*target
, *source
;
4459 struct ws_win
*cur_focus
;
4460 struct ws_win_list
*wl
;
4464 DNPRINTF(SWM_D_WS
, "swapwin: id: %d, screen[%d]:%dx%d+%d+%d, ws: %d\n",
4465 args
->id
, r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
4467 cur_focus
= r
->ws
->focus
;
4468 if (cur_focus
== NULL
|| FULLSCREEN(cur_focus
))
4471 /* Adjust stacking in floating layer. */
4472 if (ABOVE(cur_focus
)) {
4474 case SWM_ARG_ID_SWAPPREV
:
4475 target
= TAILQ_PREV(cur_focus
, ws_win_stack
,
4477 if (target
!= NULL
&& FLOATING(target
)) {
4478 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4480 TAILQ_INSERT_BEFORE(target
, cur_focus
,
4482 update_win_stacking(cur_focus
);
4486 case SWM_ARG_ID_SWAPNEXT
:
4487 target
= TAILQ_NEXT(cur_focus
, stack_entry
);
4488 if (target
!= NULL
&& FLOATING(target
)) {
4489 TAILQ_REMOVE(&cur_focus
->ws
->stack
, cur_focus
,
4491 TAILQ_INSERT_AFTER(&cur_focus
->ws
->stack
,
4492 target
, cur_focus
, stack_entry
);
4493 update_win_stacking(cur_focus
);
4501 if (r
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
4504 clear_maximized(r
->ws
);
4507 wl
= &source
->ws
->winlist
;
4510 case SWM_ARG_ID_SWAPPREV
:
4512 source
= find_window(source
->transient
);
4513 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
4514 if (target
&& target
->transient
)
4515 target
= find_window(target
->transient
);
4516 TAILQ_REMOVE(wl
, source
, entry
);
4518 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4520 TAILQ_INSERT_BEFORE(target
, source
, entry
);
4522 case SWM_ARG_ID_SWAPNEXT
:
4523 target
= TAILQ_NEXT(source
, entry
);
4524 /* move the parent and let the sort handle the move */
4526 source
= find_window(source
->transient
);
4527 TAILQ_REMOVE(wl
, source
, entry
);
4529 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4531 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
4533 case SWM_ARG_ID_SWAPMAIN
:
4534 target
= TAILQ_FIRST(wl
);
4535 if (target
== source
) {
4536 if (source
->ws
->focus_prev
!= NULL
&&
4537 source
->ws
->focus_prev
!= target
)
4538 source
= source
->ws
->focus_prev
;
4542 if (target
== NULL
|| source
== NULL
)
4544 source
->ws
->focus_prev
= target
;
4545 TAILQ_REMOVE(wl
, target
, entry
);
4546 TAILQ_INSERT_BEFORE(source
, target
, entry
);
4547 TAILQ_REMOVE(wl
, source
, entry
);
4548 TAILQ_INSERT_HEAD(wl
, source
, entry
);
4550 case SWM_ARG_ID_MOVELAST
:
4551 TAILQ_REMOVE(wl
, source
, entry
);
4552 TAILQ_INSERT_TAIL(wl
, source
, entry
);
4555 DNPRINTF(SWM_D_MOVE
, "swapwin: invalid id: %d\n", args
->id
);
4560 ewmh_update_client_list();
4566 DNPRINTF(SWM_D_MOVE
, "swapwin: done\n");
4570 get_focus_prev(struct ws_win
*win
)
4572 struct ws_win
*winfocus
= NULL
;
4573 struct ws_win
*cur_focus
= NULL
;
4574 struct ws_win_list
*wl
= NULL
;
4575 struct workspace
*ws
= NULL
;
4577 if (!(win
&& win
->ws
))
4582 cur_focus
= ws
->focus
;
4584 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: win %#x, cur_focus: %#x, "
4585 "focus_prev: %#x\n", WINID(win
), WINID(cur_focus
),
4586 WINID(ws
->focus_prev
));
4588 /* pickle, just focus on whatever */
4589 if (cur_focus
== NULL
) {
4590 /* use prev_focus if valid */
4591 if (ws
->focus_prev
&& find_window(ws
->focus_prev
->id
))
4592 winfocus
= ws
->focus_prev
;
4596 /* if transient focus on parent */
4597 if (TRANS(cur_focus
)) {
4598 winfocus
= find_window(cur_focus
->transient
);
4602 /* if in max_stack try harder */
4603 if ((win
->quirks
& SWM_Q_FOCUSPREV
) ||
4604 (ws
->cur_layout
->flags
& SWM_L_FOCUSPREV
)) {
4605 if (cur_focus
!= ws
->focus_prev
)
4606 winfocus
= ws
->focus_prev
;
4608 winfocus
= TAILQ_PREV(win
, ws_win_list
, entry
);
4613 DNPRINTF(SWM_D_FOCUS
, "get_focus_prev: focus_close: %d\n", focus_close
);
4615 if (winfocus
== NULL
|| winfocus
== win
) {
4616 switch (focus_close
) {
4617 case SWM_STACK_BOTTOM
:
4618 TAILQ_FOREACH(winfocus
, wl
, entry
)
4619 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4623 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4624 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4627 case SWM_STACK_ABOVE
:
4628 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
4629 while (winfocus
&& ICONIC(winfocus
))
4630 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4632 if (winfocus
== NULL
) {
4633 if (focus_close_wrap
) {
4634 TAILQ_FOREACH(winfocus
, wl
, entry
)
4635 if (!ICONIC(winfocus
) &&
4636 winfocus
!= cur_focus
)
4639 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4641 if (!ICONIC(winfocus
) &&
4642 winfocus
!= cur_focus
)
4647 case SWM_STACK_BELOW
:
4648 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
4649 while (winfocus
&& ICONIC(winfocus
))
4650 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
,
4653 if (winfocus
== NULL
) {
4654 if (focus_close_wrap
) {
4655 TAILQ_FOREACH_REVERSE(winfocus
, wl
,
4657 if (!ICONIC(winfocus
) &&
4658 winfocus
!= cur_focus
)
4661 TAILQ_FOREACH(winfocus
, wl
, entry
)
4662 if (!ICONIC(winfocus
) &&
4663 winfocus
!= cur_focus
)
4671 if (winfocus
== NULL
||
4672 (winfocus
&& (ICONIC(winfocus
) || winfocus
== cur_focus
))) {
4673 if (focus_default
== SWM_STACK_TOP
) {
4674 TAILQ_FOREACH_REVERSE(winfocus
, wl
, ws_win_list
, entry
)
4675 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4678 TAILQ_FOREACH(winfocus
, wl
, entry
)
4679 if (!ICONIC(winfocus
) && winfocus
!= cur_focus
)
4686 return get_focus_magic(winfocus
);
4690 get_region_focus(struct swm_region
*r
)
4692 struct ws_win
*winfocus
= NULL
;
4697 if (r
->ws
->focus
&& !ICONIC(r
->ws
->focus
))
4698 winfocus
= r
->ws
->focus
;
4699 else if (r
->ws
->focus_prev
&& !ICONIC(r
->ws
->focus_prev
))
4700 winfocus
= r
->ws
->focus_prev
;
4702 TAILQ_FOREACH(winfocus
, &r
->ws
->winlist
, entry
)
4703 if (!ICONIC(winfocus
))
4706 return get_focus_magic(winfocus
);
4710 focus(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4712 struct ws_win
*head
, *cur_focus
= NULL
, *winfocus
= NULL
;
4713 struct ws_win_list
*wl
= NULL
;
4714 struct workspace
*ws
= NULL
;
4721 cur_focus
= r
->ws
->focus
;
4725 DNPRINTF(SWM_D_FOCUS
, "focus: id: %d, cur_focus: %#x\n", args
->id
,
4728 /* Make sure an uniconified window has focus, if one exists. */
4729 if (cur_focus
== NULL
) {
4730 cur_focus
= TAILQ_FIRST(wl
);
4731 while (cur_focus
!= NULL
&& ICONIC(cur_focus
))
4732 cur_focus
= TAILQ_NEXT(cur_focus
, entry
);
4734 DNPRINTF(SWM_D_FOCUS
, "focus: new cur_focus: %#x\n",
4739 case SWM_ARG_ID_FOCUSPREV
:
4740 if (cur_focus
== NULL
)
4743 winfocus
= cur_focus
;
4745 winfocus
= TAILQ_PREV(winfocus
, ws_win_list
, entry
);
4746 if (winfocus
== NULL
)
4747 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
4748 if (winfocus
== cur_focus
)
4750 } while (winfocus
&& (ICONIC(winfocus
) ||
4751 winfocus
->id
== cur_focus
->transient
||
4752 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4753 winfocus
->transient
== cur_focus
->transient
) ||
4754 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4756 case SWM_ARG_ID_FOCUSNEXT
:
4757 if (cur_focus
== NULL
)
4760 winfocus
= cur_focus
;
4762 winfocus
= TAILQ_NEXT(winfocus
, entry
);
4763 if (winfocus
== NULL
)
4764 winfocus
= TAILQ_FIRST(wl
);
4765 if (winfocus
== cur_focus
)
4767 } while (winfocus
&& (ICONIC(winfocus
) ||
4768 winfocus
->id
== cur_focus
->transient
||
4769 (cur_focus
->transient
!= XCB_WINDOW_NONE
&&
4770 winfocus
->transient
== cur_focus
->transient
) ||
4771 (winfocus
->quirks
& SWM_Q_NOFOCUSCYCLE
)));
4773 case SWM_ARG_ID_FOCUSMAIN
:
4774 if (cur_focus
== NULL
)
4777 winfocus
= TAILQ_FIRST(wl
);
4778 if (winfocus
== cur_focus
)
4779 winfocus
= cur_focus
->ws
->focus_prev
;
4781 case SWM_ARG_ID_FOCUSURGENT
:
4782 /* Search forward for the next urgent window. */
4786 for (i
= 0; i
<= workspace_limit
; ++i
) {
4788 head
= TAILQ_FIRST(&r
->s
->ws
[(ws
->idx
+ i
) %
4789 workspace_limit
].winlist
);
4792 if (head
== cur_focus
) {
4794 winfocus
= cur_focus
;
4797 } else if (get_urgent(head
)) {
4802 head
= TAILQ_NEXT(head
, entry
);
4809 /* Switch ws if new focus is on a different ws. */
4810 if (winfocus
&& winfocus
->ws
!= ws
) {
4811 a
.id
= winfocus
->ws
->idx
;
4812 switchws(bp
, r
, &a
);
4819 if (clear_maximized(ws
) > 0)
4822 focus_win(get_focus_magic(winfocus
));
4827 DNPRINTF(SWM_D_FOCUS
, "focus: done\n");
4831 focus_pointer(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4835 /* Not needed for buttons since this is already done in buttonpress. */
4836 if (bp
->type
== KEYBIND
) {
4837 focus_win(get_pointer_win(r
->s
->root
));
4843 cycle_layout(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4845 struct workspace
*ws
= r
->ws
;
4847 /* suppress unused warning since var is needed */
4851 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
4854 if (ws
->cur_layout
->l_stack
== NULL
)
4855 ws
->cur_layout
= &layouts
[0];
4857 clear_maximized(ws
);
4862 focus_win(get_region_focus(r
));
4869 stack_config(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
4871 struct workspace
*ws
= r
->ws
;
4875 DNPRINTF(SWM_D_STACK
, "stack_config: id: %d workspace: %d\n",
4878 if (clear_maximized(ws
) > 0)
4881 if (ws
->cur_layout
->l_config
!= NULL
)
4882 ws
->cur_layout
->l_config(ws
, args
->id
);
4884 if (args
->id
!= SWM_ARG_ID_STACKINIT
)
4893 stack(struct swm_region
*r
) {
4894 struct swm_geometry g
;
4895 struct swm_region
*r_prev
;
4901 DNPRINTF(SWM_D_STACK
, "stack: begin\n");
4903 /* Adjust stack area for region bar and padding. */
4905 g
.x
+= region_padding
;
4906 g
.y
+= region_padding
;
4907 g
.w
-= 2 * border_width
+ 2 * region_padding
;
4908 g
.h
-= 2 * border_width
+ 2 * region_padding
;
4909 if (bar_enabled
&& r
->ws
->bar_enabled
) {
4915 DNPRINTF(SWM_D_STACK
, "stack: workspace: %d (screen: "
4916 "%d, region: %d), (x,y) WxH: (%d,%d) %d x %d\n",
4917 r
->ws
->idx
, r
->s
->idx
, get_region_index(r
), g
.x
, g
.y
, g
.w
, g
.h
);
4919 r_prev
= TAILQ_PREV(r
, swm_region_list
, entry
);
4921 /* Stack bar/input relative to prev. region. */
4922 val
[1] = XCB_STACK_MODE_ABOVE
;
4924 val
[0] = r_prev
->id
;
4925 DNPRINTF(SWM_D_STACK
, "stack: region input %#x "
4926 "relative to %#x.\n", r
->id
, val
[0]);
4927 xcb_configure_window(conn
, r
->id
,
4928 XCB_CONFIG_WINDOW_SIBLING
|
4929 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4931 val
[0] = r_prev
->bar
->id
;
4932 DNPRINTF(SWM_D_STACK
, "stack: region bar %#x "
4933 "relative to %#x.\n", r
->bar
->id
, val
[0]);
4934 xcb_configure_window(conn
, r
->bar
->id
,
4935 XCB_CONFIG_WINDOW_SIBLING
|
4936 XCB_CONFIG_WINDOW_STACK_MODE
, val
);
4939 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
4940 r
->ws
->cur_layout
->l_string(r
->ws
);
4941 /* save r so we can track region changes */
4947 DNPRINTF(SWM_D_STACK
, "stack: end\n");
4951 store_float_geom(struct ws_win
*win
)
4953 if (win
== NULL
|| win
->ws
->r
== NULL
)
4956 /* retain window geom and region geom */
4957 win
->g_float
= win
->g
;
4958 win
->g_float
.x
-= X(win
->ws
->r
);
4959 win
->g_float
.y
-= Y(win
->ws
->r
);
4960 win
->g_floatvalid
= true;
4961 DNPRINTF(SWM_D_MISC
, "store_float_geom: win %#x, g: (%d,%d)"
4962 " %d x %d, g_float: (%d,%d) %d x %d\n", win
->id
, X(win
), Y(win
),
4963 WIDTH(win
), HEIGHT(win
), win
->g_float
.x
, win
->g_float
.y
,
4964 win
->g_float
.w
, win
->g_float
.h
);
4968 load_float_geom(struct ws_win
*win
)
4970 if (win
== NULL
|| win
->ws
->r
== NULL
)
4973 if (win
->g_floatvalid
) {
4974 win
->g
= win
->g_float
;
4975 X(win
) += X(win
->ws
->r
);
4976 Y(win
) += Y(win
->ws
->r
);
4977 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g: (%d,%d)"
4978 "%d x %d\n", win
->id
, X(win
), Y(win
), WIDTH(win
),
4981 DNPRINTF(SWM_D_MISC
, "load_float_geom: win %#x, g_float "
4982 "is not set.\n", win
->id
);
4987 update_floater(struct ws_win
*win
)
4989 struct workspace
*ws
;
4990 struct swm_region
*r
;
4992 DNPRINTF(SWM_D_MISC
, "update_floater: win %#x\n", WINID(win
));
4999 if ((r
= ws
->r
) == NULL
)
5002 win
->bordered
= true;
5004 if (FULLSCREEN(win
)) {
5005 /* _NET_WM_FULLSCREEN: fullscreen without border. */
5006 if (!win
->g_floatvalid
)
5007 store_float_geom(win
);
5010 win
->bordered
= false;
5011 } else if (MAXIMIZED(win
)) {
5012 /* Maximize: like a single stacked window. */
5013 if (!win
->g_floatvalid
)
5014 store_float_geom(win
);
5018 if (bar_enabled
&& ws
->bar_enabled
&& !maximize_hide_bar
) {
5020 Y(win
) += bar_height
;
5021 HEIGHT(win
) -= bar_height
;
5022 } else if (disable_border
) {
5023 win
->bordered
= false;
5026 if (win
->bordered
) {
5027 /* Window geometry excludes frame. */
5028 X(win
) += border_width
;
5029 Y(win
) += border_width
;
5030 HEIGHT(win
) -= 2 * border_width
;
5031 WIDTH(win
) -= 2 * border_width
;
5034 /* Normal floating window. */
5035 /* Update geometry if new region. */
5037 load_float_geom(win
);
5039 if (((win
->quirks
& SWM_Q_FULLSCREEN
) &&
5040 WIDTH(win
) >= WIDTH(r
) && HEIGHT(win
) >= HEIGHT(r
)) ||
5041 ((!WS_FOCUSED(win
->ws
) || win
->ws
->focus
!= win
) &&
5042 (win
->quirks
& SWM_Q_MINIMALBORDER
))) {
5044 win
->bordered
= false;
5045 } else if (!MANUAL(win
)) {
5046 if (TRANS(win
) && (win
->quirks
& SWM_Q_TRANSSZ
)) {
5047 /* Adjust size on TRANSSZ quirk. */
5048 WIDTH(win
) = (double)WIDTH(r
) * dialog_ratio
;
5049 HEIGHT(win
) = (double)HEIGHT(r
) * dialog_ratio
;
5052 if (!(win
->quirks
& SWM_Q_ANYWHERE
)) {
5054 * Floaters and transients are auto-centred
5055 * unless manually moved, resized or ANYWHERE
5058 X(win
) = X(r
) + (WIDTH(r
) - WIDTH(win
)) / 2;
5059 Y(win
) = Y(r
) + (HEIGHT(r
) - HEIGHT(win
)) / 2;
5060 store_float_geom(win
);
5065 /* Ensure at least 1 pixel of the window is in the region. */
5066 region_containment(win
, r
, SWM_CW_ALLSIDES
);
5072 * Send keystrokes to terminal to decrease/increase the font size as the
5073 * window size changes.
5076 adjust_font(struct ws_win
*win
)
5078 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
5079 ABOVE(win
) || TRANS(win
))
5082 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
5083 WIDTH(win
) / win
->sh
.width_inc
< term_width
&&
5084 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
5085 win
->font_size_boundary
[win
->font_steps
] =
5086 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
5089 win
->last_inc
= win
->sh
.width_inc
;
5090 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
5091 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
5092 WIDTH(win
) > win
->font_size_boundary
[win
->font_steps
- 1]) {
5095 win
->last_inc
= win
->sh
.width_inc
;
5096 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
5100 #define SWAPXY(g) do { \
5102 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
5103 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
5106 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, bool flip
)
5108 struct swm_geometry cell
, r_g
= *g
;
5110 int i
= 0, j
= 0, s
= 0, stacks
= 0;
5111 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
5112 int hrh
= 0, extra
= 0, h_slice
= 0, last_h
= 0;
5113 int split
= 0, colno
= 0;
5114 int winno
, mwin
= 0, msize
= 0;
5115 int remain
, missing
, v_slice
, mscale
;
5116 bool bordered
= true, reconfigure
= false;
5119 * cell: geometry for window, including frame.
5120 * mwin: # of windows in master area.
5121 * mscale: size increment of master area.
5122 * stacks: # of stack columns
5125 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d, rot: %s, "
5126 "flip: %s\n", ws
->idx
, YESNO(rot
), YESNO(flip
));
5128 memset(&cell
, 0, sizeof(cell
));
5130 /* Prepare tiling variables, if needed. */
5131 if ((winno
= count_win(ws
, false)) > 0) {
5132 /* Find first tiled window. */
5133 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
5134 if (!FLOATING(win
) && !ICONIC(win
))
5137 /* Take into account size hints of first tiled window. */
5139 w_inc
= win
->sh
.width_inc
;
5140 w_base
= win
->sh
.base_width
;
5141 mwin
= ws
->l_state
.horizontal_mwin
;
5142 mscale
= ws
->l_state
.horizontal_msize
;
5143 stacks
= ws
->l_state
.horizontal_stacks
;
5146 w_inc
= win
->sh
.height_inc
;
5147 w_base
= win
->sh
.base_height
;
5148 mwin
= ws
->l_state
.vertical_mwin
;
5149 mscale
= ws
->l_state
.vertical_msize
;
5150 stacks
= ws
->l_state
.vertical_stacks
;
5154 cell
.x
+= border_width
;
5155 cell
.y
+= border_width
;
5157 if (stacks
> winno
- mwin
)
5158 stacks
= winno
- mwin
;
5162 h_slice
= r_g
.h
/ SWM_H_SLICE
;
5163 if (mwin
&& winno
> mwin
) {
5164 v_slice
= r_g
.w
/ SWM_V_SLICE
;
5168 cell
.w
= v_slice
* mscale
;
5170 if (w_inc
> 1 && w_inc
< v_slice
) {
5171 /* Adjust for requested size increment. */
5172 remain
= (cell
.w
- w_base
) % w_inc
;
5178 cell
.x
+= r_g
.w
- msize
;
5181 colno
= split
= winno
/ stacks
;
5182 cell
.w
= ((r_g
.w
- (stacks
* 2 * border_width
) +
5183 2 * border_width
) / stacks
);
5186 hrh
= r_g
.h
/ colno
;
5187 extra
= r_g
.h
- (colno
* hrh
);
5188 cell
.h
= hrh
- 2 * border_width
;
5189 i
= j
= 0, s
= stacks
;
5192 /* Update window geometry. */
5193 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
5197 if (FLOATING(win
)) {
5198 update_floater(win
);
5203 if (split
&& i
== split
) {
5204 colno
= (winno
- mwin
) / stacks
;
5205 if (s
<= (winno
- mwin
) % stacks
)
5208 hrh
= r_g
.h
/ colno
;
5209 extra
= r_g
.h
- (colno
* hrh
);
5212 cell
.x
+= cell
.w
+ 2 * border_width
+
5215 cell
.w
= (r_g
.w
- msize
-
5216 (stacks
* (2 * border_width
+ tile_gap
))) / stacks
;
5218 cell
.w
+= (r_g
.w
- msize
-
5219 (stacks
* (2 * border_width
+ tile_gap
))) %
5223 cell
.x
-= cell
.w
+ 2 * border_width
+
5229 cell
.h
= hrh
- 2 * border_width
- tile_gap
;
5232 h_inc
= win
->sh
.width_inc
;
5233 h_base
= win
->sh
.base_width
;
5235 h_inc
= win
->sh
.height_inc
;
5236 h_base
= win
->sh
.base_height
;
5239 if (j
== colno
- 1) {
5240 cell
.h
= hrh
+ extra
;
5241 } else if (h_inc
> 1 && h_inc
< h_slice
) {
5242 /* adjust for window's requested size increment */
5243 remain
= (cell
.h
- h_base
) % h_inc
;
5244 missing
= h_inc
- remain
;
5246 if (missing
<= extra
|| j
== 0) {
5256 cell
.y
= r_g
.y
+ border_width
;
5258 cell
.y
+= last_h
+ 2 * border_width
+ tile_gap
;
5260 /* Window coordinates exclude frame. */
5262 if (winno
> 1 || !disable_border
||
5263 (bar_enabled
&& ws
->bar_enabled
)) {
5270 if (X(win
) != cell
.y
|| Y(win
) != cell
.x
||
5271 WIDTH(win
) != cell
.h
|| HEIGHT(win
) != cell
.w
) {
5275 WIDTH(win
) = cell
.h
;
5276 HEIGHT(win
) = cell
.w
;
5279 if (X(win
) != cell
.x
|| Y(win
) != cell
.y
||
5280 WIDTH(win
) != cell
.w
|| HEIGHT(win
) != cell
.h
) {
5284 WIDTH(win
) = cell
.w
;
5285 HEIGHT(win
) = cell
.h
;
5290 X(win
) -= border_width
;
5291 Y(win
) -= border_width
;
5292 WIDTH(win
) += 2 * border_width
;
5293 HEIGHT(win
) += 2 * border_width
;
5296 if (bordered
!= win
->bordered
) {
5298 win
->bordered
= bordered
;
5311 /* Stack all windows from bottom up. */
5312 TAILQ_FOREACH_REVERSE(win
, &ws
->stack
, ws_win_stack
, stack_entry
)
5314 update_win_stacking(win
);
5316 /* Map all windows from top down. */
5317 TAILQ_FOREACH(win
, &ws
->stack
, stack_entry
)
5321 DNPRINTF(SWM_D_STACK
, "stack_master: done\n");
5325 vertical_config(struct workspace
*ws
, int id
)
5327 DNPRINTF(SWM_D_STACK
, "vertical_config: id: %d, workspace: %d\n",
5331 case SWM_ARG_ID_STACKRESET
:
5332 case SWM_ARG_ID_STACKINIT
:
5333 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
5334 ws
->l_state
.vertical_mwin
= 1;
5335 ws
->l_state
.vertical_stacks
= 1;
5337 case SWM_ARG_ID_MASTERSHRINK
:
5338 if (ws
->l_state
.vertical_msize
> 1)
5339 ws
->l_state
.vertical_msize
--;
5341 case SWM_ARG_ID_MASTERGROW
:
5342 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
5343 ws
->l_state
.vertical_msize
++;
5345 case SWM_ARG_ID_MASTERADD
:
5346 ws
->l_state
.vertical_mwin
++;
5348 case SWM_ARG_ID_MASTERDEL
:
5349 if (ws
->l_state
.vertical_mwin
> 0)
5350 ws
->l_state
.vertical_mwin
--;
5352 case SWM_ARG_ID_STACKBALANCE
:
5353 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ (ws
->l_state
.vertical_stacks
+ 1);
5355 case SWM_ARG_ID_STACKINC
:
5356 ws
->l_state
.vertical_stacks
++;
5358 case SWM_ARG_ID_STACKDEC
:
5359 if (ws
->l_state
.vertical_stacks
> 1)
5360 ws
->l_state
.vertical_stacks
--;
5362 case SWM_ARG_ID_FLIPLAYOUT
:
5363 ws
->l_state
.vertical_flip
= !ws
->l_state
.vertical_flip
;
5371 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5373 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
5375 stack_master(ws
, g
, 0, ws
->l_state
.vertical_flip
);
5379 horizontal_config(struct workspace
*ws
, int id
)
5381 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
5384 case SWM_ARG_ID_STACKRESET
:
5385 case SWM_ARG_ID_STACKINIT
:
5386 ws
->l_state
.horizontal_mwin
= 1;
5387 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
5388 ws
->l_state
.horizontal_stacks
= 1;
5390 case SWM_ARG_ID_MASTERSHRINK
:
5391 if (ws
->l_state
.horizontal_msize
> 1)
5392 ws
->l_state
.horizontal_msize
--;
5394 case SWM_ARG_ID_MASTERGROW
:
5395 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
5396 ws
->l_state
.horizontal_msize
++;
5398 case SWM_ARG_ID_MASTERADD
:
5399 ws
->l_state
.horizontal_mwin
++;
5401 case SWM_ARG_ID_MASTERDEL
:
5402 if (ws
->l_state
.horizontal_mwin
> 0)
5403 ws
->l_state
.horizontal_mwin
--;
5405 case SWM_ARG_ID_STACKBALANCE
:
5406 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ (ws
->l_state
.horizontal_stacks
+ 1);
5408 case SWM_ARG_ID_STACKINC
:
5409 ws
->l_state
.horizontal_stacks
++;
5411 case SWM_ARG_ID_STACKDEC
:
5412 if (ws
->l_state
.horizontal_stacks
> 1)
5413 ws
->l_state
.horizontal_stacks
--;
5415 case SWM_ARG_ID_FLIPLAYOUT
:
5416 ws
->l_state
.horizontal_flip
= !ws
->l_state
.horizontal_flip
;
5424 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5426 DNPRINTF(SWM_D_STACK
, "horizontal_stack: workspace: %d\n", ws
->idx
);
5428 stack_master(ws
, g
, 1, ws
->l_state
.horizontal_flip
);
5431 /* fullscreen view */
5433 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
5435 struct swm_geometry gg
= *g
;
5436 struct ws_win
*w
, *win
= NULL
, *parent
= NULL
, *tmpw
;
5439 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
5444 winno
= count_win(ws
, false);
5445 if (winno
== 0 && count_win(ws
, true) == 0)
5448 /* Figure out which top level window should be visible. */
5449 if (ws
->focus_pending
)
5450 win
= ws
->focus_pending
;
5453 else if (ws
->focus_prev
)
5454 win
= ws
->focus_prev
;
5456 win
= TAILQ_FIRST(&ws
->winlist
);
5458 DNPRINTF(SWM_D_STACK
, "max_stack: focus_pending: %#x, focus: %#x, "
5459 "focus_prev: %#x, first: %#x, win: %#x\n", WINID(ws
->focus_pending
),
5460 WINID(ws
->focus
), WINID(ws
->focus_prev
),
5461 WINID(TAILQ_FIRST(&ws
->winlist
)), win
->id
);
5463 /* Update window geometry. */
5464 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
5473 /* Set maximized flag for all maxed windows. */
5474 if (!MAXIMIZED(w
)) {
5475 /* Preserve floating geometry. */
5477 store_float_geom(w
);
5479 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_MAXIMIZED
);
5480 ewmh_update_wm_state(w
);
5483 /* Only reconfigure if necessary. */
5484 if (X(w
) != gg
.x
|| Y(w
) != gg
.y
|| WIDTH(w
) != gg
.w
||
5485 HEIGHT(w
) != gg
.h
) {
5488 if (disable_border
&& !(bar_enabled
&& ws
->bar_enabled
)) {
5489 w
->bordered
= false;
5490 WIDTH(w
) += 2 * border_width
;
5491 HEIGHT(w
) += 2 * border_width
;
5494 X(w
) += border_width
;
5495 Y(w
) += border_width
;
5502 /* If transient, stack parent and its children. */
5503 if (TRANS(win
) && (parent
= find_window(win
->transient
))) {
5504 raise_window(parent
);
5506 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5507 if (w
->transient
== parent
->id
)
5511 /* Make sure focus window is on top. */
5514 /* Stack any children of focus window. */
5515 TAILQ_FOREACH_SAFE(w
, &ws
->stack
, stack_entry
, tmpw
)
5516 if (w
->transient
== win
->id
)
5519 /* Map all windows. */
5520 TAILQ_FOREACH(w
, &ws
->stack
, stack_entry
)
5526 send_to_rg(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5528 int ridx
= args
->id
, i
, num_screens
;
5529 struct swm_region
*rr
= NULL
;
5532 num_screens
= get_screen_count();
5533 /* do nothing if we don't have more than one screen */
5534 if (!(num_screens
> 1 || outputs
> 1))
5537 DNPRINTF(SWM_D_FOCUS
, "send_to_rg: id: %d\n", ridx
);
5539 rr
= TAILQ_FIRST(&r
->s
->rl
);
5540 for (i
= 0; i
< ridx
&& rr
!= NULL
; ++i
)
5541 rr
= TAILQ_NEXT(rr
, entry
);
5548 send_to_ws(bp
, r
, &a
);
5552 region_under(struct swm_screen
*s
, int x
, int y
)
5554 struct swm_region
*r
= NULL
;
5556 TAILQ_FOREACH(r
, &s
->rl
, entry
) {
5557 DNPRINTF(SWM_D_MISC
, "region_under: ws: %d, region g: (%d,%d) "
5558 "%d x %d, coords: (%d,%d)\n", r
->ws
->idx
, X(r
), Y(r
),
5559 WIDTH(r
), HEIGHT(r
), x
, y
);
5560 if (X(r
) <= x
&& x
< MAX_X(r
))
5561 if (Y(r
) <= y
&& y
< MAX_Y(r
))
5568 /* Transfer focused window to target workspace and focus. */
5570 send_to_ws(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5572 int wsid
= args
->id
;
5573 struct ws_win
*win
= NULL
;
5577 if (r
&& r
->ws
&& r
->ws
->focus
)
5582 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win %#x, ws %d\n", win
->id
, wsid
);
5584 if (wsid
< 0 || wsid
>= workspace_limit
)
5587 if (win
->ws
->idx
== wsid
)
5590 win_to_ws(win
, wsid
, true);
5592 /* Set new focus on target ws. */
5593 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5594 win
->ws
->focus_prev
= win
->ws
->focus
;
5595 win
->ws
->focus
= win
;
5596 win
->ws
->focus_pending
= NULL
;
5598 if (win
->ws
->focus_prev
)
5599 draw_frame(win
->ws
->focus_prev
);
5602 DNPRINTF(SWM_D_STACK
, "send_to_ws: focus_pending: %#x, focus: %#x, "
5603 "focus_prev: %#x, first: %#x, win: %#x\n",
5604 WINID(r
->ws
->focus_pending
), WINID(r
->ws
->focus
),
5605 WINID(r
->ws
->focus_prev
), WINID(TAILQ_FIRST(&r
->ws
->winlist
)),
5608 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
5609 ewmh_update_wm_state(win
);
5611 /* Restack focused region. */
5614 /* If destination ws has a region, restack. */
5617 load_float_geom(win
);
5622 /* Set new focus on current ws. */
5623 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5624 if (r
->ws
->focus
!= NULL
) {
5625 focus_win(r
->ws
->focus
);
5627 DNPRINTF(SWM_D_FOCUS
, "send_to_ws: set_input_focus: "
5628 "%#x, revert-to: parent, time: 0\n", r
->id
);
5629 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
5630 r
->id
, XCB_CURRENT_TIME
);
5640 win_to_ws(struct ws_win
*win
, int wsid
, bool unfocus
)
5642 struct ws_win
*parent
;
5643 struct workspace
*ws
, *nws
, *pws
;
5645 if (wsid
< 0 || wsid
>= workspace_limit
)
5648 if (win
->ws
->idx
== wsid
)
5652 nws
= &win
->s
->ws
[wsid
];
5654 DNPRINTF(SWM_D_MOVE
, "win_to_ws: win %#x, ws %d -> %d\n", win
->id
,
5657 /* Cleanup focus on source ws. */
5658 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
5659 (ws
->focus
== win
|| ws
->focus_pending
== win
))
5660 ws
->focus_pending
= get_focus_prev(win
);
5662 /* Move the parent if this is a transient window. */
5664 parent
= find_window(win
->transient
);
5667 /* Set new focus in parent's ws if needed. */
5668 if (pws
->focus
== parent
) {
5669 if (focus_mode
!= SWM_FOCUS_FOLLOW
)
5670 pws
->focus_pending
=
5671 get_focus_prev(parent
);
5673 unfocus_win(parent
);
5675 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
5676 pws
->focus
= pws
->focus_pending
;
5677 pws
->focus_pending
= NULL
;
5681 /* Don't unmap parent if new ws is visible */
5683 unmap_window(parent
);
5686 TAILQ_REMOVE(&ws
->winlist
, parent
, entry
);
5687 TAILQ_REMOVE(&ws
->stack
, parent
, stack_entry
);
5688 TAILQ_INSERT_TAIL(&nws
->winlist
, parent
, entry
);
5689 TAILQ_INSERT_TAIL(&nws
->stack
, parent
, stack_entry
);
5692 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5693 "_NET_WM_DESKTOP: %d\n", wsid
);
5694 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
5695 parent
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
5696 XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5703 if (ws
->focus_prev
== win
)
5704 ws
->focus_prev
= NULL
;
5706 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& ws
->focus_pending
!= NULL
) {
5707 ws
->focus
= ws
->focus_pending
;
5708 ws
->focus_pending
= NULL
;
5711 /* Don't unmap if new ws is visible */
5716 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
5717 TAILQ_REMOVE(&ws
->stack
, win
, stack_entry
);
5718 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
5719 TAILQ_INSERT_TAIL(&nws
->stack
, win
, stack_entry
);
5722 /* Update the window's workspace property: _NET_WM_DESKTOP */
5723 DNPRINTF(SWM_D_PROP
, "win_to_ws: set property: "
5724 "_NET_WM_DESKTOP: %d\n", wsid
);
5725 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
5726 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &wsid
);
5728 ewmh_update_client_list();
5730 DNPRINTF(SWM_D_MOVE
, "win_to_ws: done.\n");
5734 pressbutton(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5736 /* suppress unused warning since var is needed */
5740 xcb_test_fake_input(conn
, XCB_BUTTON_PRESS
, args
->id
,
5741 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5742 xcb_test_fake_input(conn
, XCB_BUTTON_RELEASE
, args
->id
,
5743 XCB_CURRENT_TIME
, XCB_WINDOW_NONE
, 0, 0, 0);
5747 raise_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5749 /* Suppress warning. */
5753 if (r
== NULL
|| r
->ws
== NULL
)
5756 if (r
->ws
->focus
&& MAXIMIZED(r
->ws
->focus
))
5759 r
->ws
->always_raise
= !r
->ws
->always_raise
;
5761 /* Update focused win stacking order based on new always_raise value. */
5762 raise_window(r
->ws
->focus
);
5768 iconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5772 /* Suppress warning. */
5776 if ((w
= r
->ws
->focus
) == NULL
)
5779 ewmh_apply_flags(w
, w
->ewmh_flags
| EWMH_F_HIDDEN
);
5780 ewmh_update_wm_state(w
);
5788 get_win_name(xcb_window_t win
)
5791 xcb_get_property_cookie_t c
;
5792 xcb_get_property_reply_t
*r
;
5794 /* First try _NET_WM_NAME for UTF-8. */
5795 c
= xcb_get_property(conn
, 0, win
, ewmh
[_NET_WM_NAME
].atom
,
5796 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5797 r
= xcb_get_property_reply(conn
, c
, NULL
);
5798 if (r
&& r
->type
== XCB_NONE
) {
5800 /* Use WM_NAME instead; no UTF-8. */
5801 c
= xcb_get_property(conn
, 0, win
, XCB_ATOM_WM_NAME
,
5802 XCB_GET_PROPERTY_TYPE_ANY
, 0, UINT_MAX
);
5803 r
= xcb_get_property_reply(conn
, c
, NULL
);
5806 if (r
&& r
->type
!= XCB_NONE
&& r
->length
> 0)
5807 name
= strndup(xcb_get_property_value(r
),
5808 xcb_get_property_value_length(r
));
5813 err(1, "get_win_name: failed to allocate memory.");
5821 uniconify(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5830 DNPRINTF(SWM_D_MISC
, "uniconify\n");
5832 if (r
== NULL
|| r
->ws
== NULL
)
5835 /* make sure we have anything to uniconify */
5836 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5837 if (win
->ws
== NULL
)
5838 continue; /* should never happen */
5844 DNPRINTF(SWM_D_MISC
, "uniconify: count: %d\n", count
);
5850 search_resp_action
= SWM_SEARCH_UNICONIFY
;
5852 spawn_select(r
, args
, "search", &searchpid
);
5854 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5857 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5858 if (win
->ws
== NULL
)
5859 continue; /* should never happen */
5863 name
= get_win_name(win
->id
);
5864 fprintf(lfile
, "%s.%u\n", name
, win
->id
);
5872 name_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5878 DNPRINTF(SWM_D_MISC
, "name_workspace\n");
5884 search_resp_action
= SWM_SEARCH_NAME_WORKSPACE
;
5886 spawn_select(r
, args
, "name_workspace", &searchpid
);
5888 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5891 fprintf(lfile
, "%s", "");
5896 search_workspace(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5899 struct workspace
*ws
;
5904 DNPRINTF(SWM_D_MISC
, "search_workspace\n");
5910 search_resp_action
= SWM_SEARCH_SEARCH_WORKSPACE
;
5912 spawn_select(r
, args
, "search", &searchpid
);
5914 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5917 for (i
= 0; i
< workspace_limit
; i
++) {
5921 fprintf(lfile
, "%d%s%s\n", ws
->idx
+ 1,
5922 (ws
->name
? ":" : ""), (ws
->name
? ws
->name
: ""));
5929 search_win_cleanup(void)
5931 struct search_window
*sw
= NULL
;
5933 while ((sw
= TAILQ_FIRST(&search_wl
)) != NULL
) {
5934 xcb_destroy_window(conn
, sw
->indicator
);
5935 TAILQ_REMOVE(&search_wl
, sw
, entry
);
5941 search_win(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
5943 struct ws_win
*win
= NULL
;
5944 struct search_window
*sw
= NULL
;
5947 xcb_screen_t
*screen
;
5948 int i
, width
, height
;
5956 XRectangle l_ibox
, l_lbox
;
5960 DNPRINTF(SWM_D_MISC
, "search_win\n");
5963 search_resp_action
= SWM_SEARCH_SEARCH_WINDOW
;
5965 spawn_select(r
, args
, "search", &searchpid
);
5967 if ((lfile
= fdopen(select_list_pipe
[1], "w")) == NULL
)
5970 if ((screen
= get_screen(r
->s
->idx
)) == NULL
)
5971 errx(1, "ERROR: can't get screen %d.", r
->s
->idx
);
5973 TAILQ_INIT(&search_wl
);
5976 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
) {
5980 sw
= calloc(1, sizeof(struct search_window
));
5982 warn("search_win: calloc");
5984 search_win_cleanup();
5990 snprintf(s
, sizeof s
, "%d", i
);
5993 w
= xcb_generate_id(conn
);
5994 wa
[0] = r
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
5995 wa
[1] = r
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
5996 wa
[2] = screen
->default_colormap
;
5998 if (bar_font_legacy
) {
5999 XmbTextExtents(bar_fs
, s
, len
, &l_ibox
, &l_lbox
);
6000 width
= l_lbox
.width
+ 4;
6001 height
= bar_fs_extents
->max_logical_extent
.height
+ 4;
6003 XftTextExtentsUtf8(display
, bar_font
, (FcChar8
*)s
, len
,
6005 width
= info
.width
+ 4;
6006 height
= bar_font
->height
+ 4;
6009 xcb_create_window(conn
, screen
->root_depth
, w
, win
->frame
, 0, 0,
6010 width
, height
, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT
,
6011 screen
->root_visual
, XCB_CW_BACK_PIXEL
|
6012 XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
, wa
);
6014 xcb_map_window(conn
, w
);
6017 TAILQ_INSERT_TAIL(&search_wl
, sw
, entry
);
6019 if (bar_font_legacy
) {
6020 l_gcv
.graphics_exposures
= 0;
6021 l_draw
= XCreateGC(display
, w
, 0, &l_gcv
);
6023 XSetForeground(display
, l_draw
,
6024 r
->s
->c
[SWM_S_COLOR_BAR
].pixel
);
6026 DRAWSTRING(display
, w
, bar_fs
, l_draw
, 2,
6027 (bar_fs_extents
->max_logical_extent
.height
-
6028 l_lbox
.height
) / 2 - l_lbox
.y
, s
, len
);
6030 XFreeGC(display
, l_draw
);
6033 draw
= XftDrawCreate(display
, w
,
6034 DefaultVisual(display
, r
->s
->idx
),
6035 DefaultColormap(display
, r
->s
->idx
));
6037 XftDrawStringUtf8(draw
, &search_font_color
, bar_font
, 2,
6038 (HEIGHT(r
->bar
) + bar_font
->height
) / 2 -
6039 bar_font
->descent
, (FcChar8
*)s
, len
);
6041 XftDrawDestroy(draw
);
6044 DNPRINTF(SWM_D_MISC
, "search_win: mapped win %#x\n", w
);
6046 fprintf(lfile
, "%d\n", i
);
6056 search_resp_uniconify(const char *resp
, size_t len
)
6062 DNPRINTF(SWM_D_MISC
, "search_resp_uniconify: resp: %s\n", resp
);
6064 TAILQ_FOREACH(win
, &search_r
->ws
->winlist
, entry
) {
6067 name
= get_win_name(win
->id
);
6068 if (asprintf(&s
, "%s.%u", name
, win
->id
) == -1) {
6073 if (strncmp(s
, resp
, len
) == 0) {
6074 /* XXX this should be a callback to generalize */
6075 ewmh_apply_flags(win
, win
->ewmh_flags
& ~EWMH_F_HIDDEN
);
6076 ewmh_update_wm_state(win
);
6086 search_resp_name_workspace(const char *resp
, size_t len
)
6088 struct workspace
*ws
;
6090 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: resp: %s\n", resp
);
6092 if (search_r
->ws
== NULL
)
6097 free(search_r
->ws
->name
);
6098 search_r
->ws
->name
= NULL
;
6102 ws
->name
= strdup(resp
);
6103 if (ws
->name
== NULL
) {
6104 DNPRINTF(SWM_D_MISC
, "search_resp_name_workspace: "
6105 "strdup: %s", strerror(errno
));
6109 ewmh_update_desktop_names();
6110 ewmh_get_desktop_names();
6115 ewmh_update_desktop_names(void)
6117 char *name_list
= NULL
, *p
;
6118 int num_screens
, i
, j
, len
= 0, tot
= 0;
6120 num_screens
= get_screen_count();
6121 for (i
= 0; i
< num_screens
; ++i
) {
6122 for (j
= 0; j
< workspace_limit
; ++j
) {
6123 if (screens
[i
].ws
[j
].name
!= NULL
)
6124 len
+= strlen(screens
[i
].ws
[j
].name
);
6128 if((name_list
= calloc(len
, sizeof(char))) == NULL
)
6129 err(1, "update_desktop_names: calloc: failed to "
6130 "allocate memory.");
6133 for (j
= 0; j
< workspace_limit
; ++j
) {
6134 if (screens
[i
].ws
[j
].name
!= NULL
) {
6135 len
= strlen(screens
[i
].ws
[j
].name
);
6136 memcpy(p
, screens
[i
].ws
[j
].name
, len
);
6145 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6146 screens
[i
].root
, ewmh
[_NET_DESKTOP_NAMES
].atom
,
6147 a_utf8_string
, 8, tot
, name_list
);
6157 ewmh_get_desktop_names(void)
6160 xcb_get_property_cookie_t c
;
6161 xcb_get_property_reply_t
*r
;
6162 int num_screens
, i
, j
, n
, k
;
6164 num_screens
= get_screen_count();
6165 for (i
= 0; i
< num_screens
; ++i
) {
6166 for (j
= 0; j
< workspace_limit
; ++j
) {
6167 free(screens
[i
].ws
[j
].name
);
6168 screens
[i
].ws
[j
].name
= NULL
;
6171 c
= xcb_get_property(conn
, 0, screens
[i
].root
,
6172 ewmh
[_NET_DESKTOP_NAMES
].atom
,
6173 a_utf8_string
, 0, UINT32_MAX
);
6174 r
= xcb_get_property_reply(conn
, c
, NULL
);
6178 names
= xcb_get_property_value(r
);
6179 n
= xcb_get_property_value_length(r
);
6181 for (j
= 0, k
= 0; j
< n
; ++j
) {
6182 if (*(names
+ j
) != '\0') {
6183 screens
[i
].ws
[k
].name
= strdup(names
+ j
);
6184 j
+= strlen(names
+ j
);
6193 ewmh_update_client_list(void)
6196 int num_screens
, i
, j
, k
= 0, count
= 0;
6199 num_screens
= get_screen_count();
6200 for (i
= 0; i
< num_screens
; ++i
) {
6201 for (j
= 0; j
< workspace_limit
; ++j
)
6202 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6205 DNPRINTF(SWM_D_PROP
, "ewmh_update_client_list: win count: %d\n",
6211 wins
= calloc(count
, sizeof(xcb_window_t
));
6213 err(1, "ewmh_update_client_list: calloc: failed to "
6214 "allocate memory.");
6216 for (j
= 0, k
= 0; j
< workspace_limit
; ++j
)
6217 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
6218 wins
[k
++] = win
->id
;
6220 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6221 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
6222 XCB_ATOM_WINDOW
, 32, count
, wins
);
6229 ewmh_update_current_desktop(void)
6233 num_screens
= get_screen_count();
6234 for (i
= 0; i
< num_screens
; ++i
) {
6235 if (screens
[i
].r_focus
)
6236 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6237 screens
[i
].root
, ewmh
[_NET_CURRENT_DESKTOP
].atom
,
6238 XCB_ATOM_CARDINAL
, 32, 1,
6239 &screens
[i
].r_focus
->ws
->idx
);
6244 ewmh_update_desktops(void)
6246 int num_screens
, i
, j
;
6249 vals
= calloc(workspace_limit
* 2, sizeof(uint32_t));
6251 err(1, "ewmh_update_desktops: calloc: failed to allocate "
6254 num_screens
= get_screen_count();
6255 for (i
= 0; i
< num_screens
; i
++) {
6256 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6257 screens
[i
].root
, ewmh
[_NET_NUMBER_OF_DESKTOPS
].atom
,
6258 XCB_ATOM_CARDINAL
, 32, 1, &workspace_limit
);
6260 for (j
= 0; j
< workspace_limit
; ++j
) {
6261 if (screens
[i
].ws
[j
].r
!= NULL
) {
6262 vals
[j
* 2] = X(screens
[i
].ws
[j
].r
);
6263 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].r
);
6264 } else if (screens
[i
].ws
[j
].old_r
!= NULL
) {
6265 vals
[j
* 2] = X(screens
[i
].ws
[j
].old_r
);
6266 vals
[j
* 2 + 1] = Y(screens
[i
].ws
[j
].old_r
);
6268 vals
[j
* 2] = vals
[j
* 2 + 1] = 0;
6272 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
,
6273 screens
[i
].root
, ewmh
[_NET_DESKTOP_VIEWPORT
].atom
,
6274 XCB_ATOM_CARDINAL
, 32, workspace_limit
* 2, vals
);
6281 search_resp_search_workspace(const char *resp
)
6288 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: resp: %s\n", resp
);
6292 DNPRINTF(SWM_D_MISC
, "search_resp_search_workspace: strdup: %s",
6299 ws_idx
= (int)strtonum(q
, 1, workspace_limit
, &errstr
);
6301 DNPRINTF(SWM_D_MISC
, "workspace idx is %s: %s",
6308 switchws(NULL
, search_r
, &a
);
6312 search_resp_search_window(const char *resp
)
6317 struct search_window
*sw
;
6319 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: resp: %s\n", resp
);
6323 DNPRINTF(SWM_D_MISC
, "search_resp_search_window: strdup: %s",
6328 idx
= (int)strtonum(s
, 1, INT_MAX
, &errstr
);
6330 DNPRINTF(SWM_D_MISC
, "window idx is %s: %s",
6337 TAILQ_FOREACH(sw
, &search_wl
, entry
)
6338 if (idx
== sw
->idx
) {
6344 #define MAX_RESP_LEN 1024
6347 search_do_resp(void)
6353 DNPRINTF(SWM_D_MISC
, "search_do_resp:\n");
6358 if ((resp
= calloc(1, MAX_RESP_LEN
+ 1)) == NULL
) {
6359 warn("search: calloc");
6363 rbytes
= read(select_resp_pipe
[0], resp
, MAX_RESP_LEN
);
6365 warn("search: read error");
6368 resp
[rbytes
] = '\0';
6371 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
6372 * newline, so work around that by sanitizing the resp now.
6374 resp
[strcspn(resp
, "\n")] = '\0';
6377 switch (search_resp_action
) {
6378 case SWM_SEARCH_UNICONIFY
:
6379 search_resp_uniconify(resp
, len
);
6381 case SWM_SEARCH_NAME_WORKSPACE
:
6382 search_resp_name_workspace(resp
, len
);
6384 case SWM_SEARCH_SEARCH_WORKSPACE
:
6385 search_resp_search_workspace(resp
);
6387 case SWM_SEARCH_SEARCH_WINDOW
:
6388 search_resp_search_window(resp
);
6393 if (search_resp_action
== SWM_SEARCH_SEARCH_WINDOW
)
6394 search_win_cleanup();
6396 search_resp_action
= SWM_SEARCH_NONE
;
6397 close(select_resp_pipe
[0]);
6404 wkill(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6408 DNPRINTF(SWM_D_MISC
, "wkill: win %#x, id: %d\n", WINID(r
->ws
->focus
),
6411 if (r
->ws
->focus
== NULL
)
6414 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
6415 xcb_kill_client(conn
, r
->ws
->focus
->id
);
6417 if (r
->ws
->focus
->can_delete
)
6418 client_msg(r
->ws
->focus
, a_delete
, 0);
6424 clear_maximized(struct workspace
*ws
)
6429 /* Clear any maximized win(s) on ws, from bottom up. */
6430 TAILQ_FOREACH_REVERSE(w
, &ws
->stack
, ws_win_stack
, stack_entry
)
6432 ewmh_apply_flags(w
, w
->ewmh_flags
& ~EWMH_F_MAXIMIZED
);
6433 ewmh_update_wm_state(w
);
6441 maximize_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6443 struct ws_win
*w
= r
->ws
->focus
;
6445 /* suppress unused warning since var is needed */
6452 DNPRINTF(SWM_D_MISC
, "maximize_toggle: win %#x\n", w
->id
);
6457 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6460 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_MAXIMIZED
);
6461 ewmh_update_wm_state(w
);
6465 if (w
== w
->ws
->focus
)
6470 DNPRINTF(SWM_D_MISC
, "maximize_toggle: done\n");
6474 floating_toggle(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6476 struct ws_win
*w
= r
->ws
->focus
;
6478 /* suppress unused warning since var is needed */
6485 DNPRINTF(SWM_D_MISC
, "floating_toggle: win %#x\n", w
->id
);
6487 if (FULLSCREEN(w
) || TRANS(w
))
6490 if (w
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
])
6493 ewmh_apply_flags(w
, w
->ewmh_flags
^ EWMH_F_ABOVE
);
6494 ewmh_update_wm_state(w
);
6498 if (w
== w
->ws
->focus
)
6503 DNPRINTF(SWM_D_MISC
, "floating_toggle: done\n");
6507 region_containment(struct ws_win
*win
, struct swm_region
*r
, int opts
)
6509 struct swm_geometry g
= r
->g
;
6510 int rt
, lt
, tp
, bm
, bw
;
6512 bw
= (opts
& SWM_CW_SOFTBOUNDARY
) ? boundary_width
: 0;
6515 * Perpendicular distance of each side of the window to the respective
6516 * side of the region boundary. Positive values indicate the side of
6517 * the window has passed beyond the region boundary.
6519 rt
= opts
& SWM_CW_RIGHT
? MAX_X(win
) - MAX_X(r
) : bw
;
6520 lt
= opts
& SWM_CW_LEFT
? X(r
) - X(win
) : bw
;
6521 bm
= opts
& SWM_CW_BOTTOM
? MAX_Y(win
) - MAX_Y(r
) : bw
;
6522 tp
= opts
& SWM_CW_TOP
? Y(r
) - Y(win
) : bw
;
6524 DNPRINTF(SWM_D_MISC
, "region_containment: win %#x, rt: %d, lt: %d, "
6525 "bm: %d, tp: %d, SOFTBOUNDARY: %s, HARDBOUNDARY: %s\n", win
->id
, rt
,
6526 lt
, bm
, tp
, YESNO(opts
& SWM_CW_SOFTBOUNDARY
),
6527 YESNO(opts
& SWM_CW_HARDBOUNDARY
));
6530 * Disable containment if any of the flagged sides went beyond the
6531 * containment boundary, or if containment is disabled.
6533 if (!(opts
& SWM_CW_HARDBOUNDARY
|| opts
& SWM_CW_SOFTBOUNDARY
) ||
6534 (bw
!= 0 && ((rt
> bw
) || (lt
> bw
) || (bm
> bw
) || (tp
> bw
)))) {
6535 /* Make sure window has at least 1 pixel in the region */
6536 g
.x
+= 1 - WIDTH(win
);
6537 g
.y
+= 1 - HEIGHT(win
);
6538 g
.w
+= 2 * WIDTH(win
) - 2;
6539 g
.h
+= 2 * HEIGHT(win
) - 2;
6542 constrain_window(win
, &g
, &opts
);
6545 /* Move or resize a window so that flagged side(s) fit into the supplied box. */
6547 constrain_window(struct ws_win
*win
, struct swm_geometry
*b
, int *opts
)
6549 DNPRINTF(SWM_D_MISC
, "constrain_window: win %#x, (x,y) w x h: "
6550 "(%d,%d) %d x %d, box: (x,y) w x h: (%d,%d) %d x %d, rt: %s, "
6551 "lt: %s, bt: %s, tp: %s, allow resize: %s\n", win
->id
, X(win
),
6552 Y(win
), WIDTH(win
), HEIGHT(win
), b
->x
, b
->y
, b
->w
, b
->h
,
6553 YESNO(*opts
& SWM_CW_RIGHT
), YESNO(*opts
& SWM_CW_LEFT
),
6554 YESNO(*opts
& SWM_CW_BOTTOM
), YESNO(*opts
& SWM_CW_TOP
),
6555 YESNO(*opts
& SWM_CW_RESIZABLE
));
6557 if ((*opts
& SWM_CW_RIGHT
) && MAX_X(win
) > b
->x
+ b
->w
) {
6558 if (*opts
& SWM_CW_RESIZABLE
)
6559 WIDTH(win
) = b
->x
+ b
->w
- X(win
);
6561 X(win
) = b
->x
+ b
->w
- WIDTH(win
);
6564 if ((*opts
& SWM_CW_LEFT
) && X(win
) < b
->x
) {
6565 if (*opts
& SWM_CW_RESIZABLE
)
6566 WIDTH(win
) -= b
->x
- X(win
);
6571 if ((*opts
& SWM_CW_BOTTOM
) && MAX_Y(win
) > b
->y
+ b
->h
) {
6572 if (*opts
& SWM_CW_RESIZABLE
)
6573 HEIGHT(win
) = b
->y
+ b
->h
- Y(win
);
6575 Y(win
) = b
->y
+ b
->h
- HEIGHT(win
);
6578 if ((*opts
& SWM_CW_TOP
) && Y(win
) < b
->y
) {
6579 if (*opts
& SWM_CW_RESIZABLE
)
6580 HEIGHT(win
) -= b
->y
- Y(win
);
6585 if (*opts
& SWM_CW_RESIZABLE
) {
6588 if (HEIGHT(win
) < 1)
6594 draw_frame(struct ws_win
*win
)
6596 xcb_rectangle_t rect
[4];
6600 if (win
->frame
== XCB_WINDOW_NONE
) {
6601 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x not "
6602 "reparented.\n", win
->id
);
6606 if (!win
->bordered
) {
6607 DNPRINTF(SWM_D_EVENT
, "draw_frame: win %#x frame "
6608 "disabled\n", win
->id
);
6611 if (WS_FOCUSED(win
->ws
) && win
->ws
->focus
== win
)
6612 pixel
= MAXIMIZED(win
) ?
6613 &win
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].pixel
:
6614 &win
->s
->c
[SWM_S_COLOR_FOCUS
].pixel
;
6616 pixel
= MAXIMIZED(win
) ?
6617 &win
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].pixel
:
6618 &win
->s
->c
[SWM_S_COLOR_UNFOCUS
].pixel
;
6620 /* Top (with corners) */
6623 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6624 rect
[n
].height
= border_width
;
6625 /* Left (without corners) */
6627 rect
[n
].y
= border_width
;
6628 rect
[n
].width
= border_width
;
6629 rect
[n
].height
= HEIGHT(win
);
6630 /* Right (without corners) */
6631 rect
[++n
].x
= border_width
+ WIDTH(win
);
6632 rect
[n
].y
= border_width
;
6633 rect
[n
].width
= border_width
;
6634 rect
[n
].height
= HEIGHT(win
);
6635 /* Bottom (with corners)*/
6637 rect
[n
].y
= border_width
+ HEIGHT(win
);
6638 rect
[n
].width
= WIDTH(win
) + 2 * border_width
;
6639 rect
[n
].height
= border_width
;
6641 xcb_change_gc(conn
, win
->s
->bar_gc
, XCB_GC_FOREGROUND
, pixel
);
6642 xcb_poly_fill_rectangle(conn
, win
->frame
, win
->s
->bar_gc
, 4, rect
);
6646 update_window(struct ws_win
*win
)
6651 if (win
->frame
== XCB_WINDOW_NONE
) {
6652 DNPRINTF(SWM_D_EVENT
, "update_window: skip win %#x; "
6653 "not reparented.\n", win
->id
);
6657 mask
= XCB_CONFIG_WINDOW_X
| XCB_CONFIG_WINDOW_Y
|
6658 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
|
6659 XCB_CONFIG_WINDOW_BORDER_WIDTH
;
6661 /* Reconfigure frame. */
6662 if (win
->bordered
) {
6663 wc
[0] = X(win
) - border_width
;
6664 wc
[1] = Y(win
) - border_width
;
6665 wc
[2] = WIDTH(win
) + 2 * border_width
;
6666 wc
[3] = HEIGHT(win
) + 2 * border_width
;
6671 wc
[3] = HEIGHT(win
);
6676 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x frame %#x, (x,y) w x h: "
6677 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, win
->frame
, wc
[0],
6678 wc
[1], wc
[2], wc
[3], YESNO(win
->bordered
));
6680 xcb_configure_window(conn
, win
->frame
, mask
, wc
);
6682 /* Reconfigure client window. */
6683 wc
[0] = wc
[1] = win
->bordered
? border_width
: 0;
6685 wc
[3] = HEIGHT(win
);
6687 DNPRINTF(SWM_D_EVENT
, "update_window: win %#x, (x,y) w x h: "
6688 "(%d,%d) %d x %d, bordered: %s\n", win
->id
, wc
[0], wc
[1], wc
[2],
6689 wc
[3], YESNO(win
->bordered
));
6690 xcb_configure_window(conn
, win
->id
, mask
, wc
);
6692 /* ICCCM 4.2.3 Synthetic ConfigureNotify when moved and not resized. */
6693 if ((X(win
) != win
->g_prev
.x
|| Y(win
) != win
->g_prev
.y
) &&
6694 (win
->java
|| (WIDTH(win
) == win
->g_prev
.w
&&
6695 HEIGHT(win
) == win
->g_prev
.h
))) {
6696 /* Java has special needs when moved together with a resize. */
6697 config_win(win
, NULL
);
6700 win
->g_prev
= win
->g
;
6704 SIMPLEQ_ENTRY(event
) entry
;
6705 xcb_generic_event_t
*ev
;
6707 SIMPLEQ_HEAD(event_queue
, event
) events
= SIMPLEQ_HEAD_INITIALIZER(events
);
6709 xcb_generic_event_t
*
6710 get_next_event(bool dowait
)
6713 xcb_generic_event_t
*evt
;
6715 /* Try queue first. */
6716 if ((ep
= SIMPLEQ_FIRST(&events
))) {
6718 SIMPLEQ_REMOVE_HEAD(&events
, entry
);
6721 evt
= xcb_wait_for_event(conn
);
6723 evt
= xcb_poll_for_event(conn
);
6729 put_back_event(xcb_generic_event_t
*evt
)
6732 if ((ep
= malloc(sizeof (struct event
))) == NULL
)
6733 err(1, "put_back_event: failed to allocate memory.");
6735 SIMPLEQ_INSERT_HEAD(&events
, ep
, entry
);
6738 /* Peeks at next event to detect auto-repeat. */
6740 keyrepeating(xcb_key_release_event_t
*kre
)
6742 xcb_generic_event_t
*evt
;
6744 /* Ensure repeating keypress is finished processing. */
6747 if ((evt
= get_next_event(false))) {
6748 put_back_event(evt
);
6750 if (XCB_EVENT_RESPONSE_TYPE(evt
) == XCB_KEY_PRESS
&&
6751 kre
->sequence
== evt
->sequence
&&
6752 kre
->detail
== ((xcb_key_press_event_t
*)evt
)->detail
)
6760 keybindreleased(struct binding
*bp
, xcb_key_release_event_t
*kre
)
6762 if (bp
->type
== KEYBIND
&& !keyrepeating(kre
) &&
6763 bp
->value
== xcb_key_press_lookup_keysym(syms
, kre
, 0))
6769 #define SWM_RESIZE_STEPS (50)
6772 resize_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
6774 xcb_timestamp_t timestamp
= 0;
6775 struct swm_region
*r
= NULL
;
6776 struct swm_geometry g
;
6777 int top
= 0, left
= 0;
6779 xcb_cursor_t cursor
;
6780 xcb_query_pointer_reply_t
*xpr
= NULL
;
6781 xcb_generic_event_t
*evt
;
6782 xcb_motion_notify_event_t
*mne
;
6783 bool resizing
, step
= false;
6789 if (FULLSCREEN(win
))
6792 /* In max_stack mode, should only resize transients. */
6793 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
6796 DNPRINTF(SWM_D_EVENT
, "resize: win %#x, floating: %s, "
6797 "transient: %#x\n", win
->id
, YESNO(ABOVE(win
)),
6801 store_float_geom(win
);
6802 else if (!(TRANS(win
) || ABOVE(win
)))
6805 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
6807 ewmh_update_wm_state(win
);
6813 /* It's possible for win to have been freed during focus_flush(). */
6814 if (validate_win(win
)) {
6815 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6820 case SWM_ARG_ID_WIDTHSHRINK
:
6821 WIDTH(win
) -= SWM_RESIZE_STEPS
;
6824 case SWM_ARG_ID_WIDTHGROW
:
6825 WIDTH(win
) += SWM_RESIZE_STEPS
;
6828 case SWM_ARG_ID_HEIGHTSHRINK
:
6829 HEIGHT(win
) -= SWM_RESIZE_STEPS
;
6832 case SWM_ARG_ID_HEIGHTGROW
:
6833 HEIGHT(win
) += SWM_RESIZE_STEPS
;
6840 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6841 SWM_CW_HARDBOUNDARY
);
6843 store_float_geom(win
);
6847 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6848 SWM_CW_SOFTBOUNDARY
);
6851 /* get cursor offset from window root */
6852 xpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
6859 if (xpr
->win_x
< WIDTH(win
) / 2)
6862 if (xpr
->win_y
< HEIGHT(win
) / 2)
6865 if (opt
== SWM_ARG_ID_CENTER
)
6866 cursor
= cursors
[XC_SIZING
].cid
;
6868 cursor
= cursors
[left
? XC_TOP_LEFT_CORNER
:
6869 XC_TOP_RIGHT_CORNER
].cid
;
6871 cursor
= cursors
[left
? XC_BOTTOM_LEFT_CORNER
:
6872 XC_BOTTOM_RIGHT_CORNER
].cid
;
6874 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
6875 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
, cursor
,
6878 /* Release keyboard freeze if called via keybind. */
6879 if (bp
->type
== KEYBIND
)
6880 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6885 while (resizing
&& (evt
= get_next_event(true))) {
6886 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
6887 case XCB_BUTTON_RELEASE
:
6888 if (bp
->type
== BTNBIND
&& bp
->value
==
6889 ((xcb_button_release_event_t
*)evt
)->detail
)
6892 case XCB_KEY_RELEASE
:
6893 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
6896 case XCB_MOTION_NOTIFY
:
6897 mne
= (xcb_motion_notify_event_t
*)evt
;
6898 DNPRINTF(SWM_D_EVENT
, "resize: MOTION_NOTIFY: "
6899 "root: %#x\n", mne
->root
);
6901 /* cursor offset/delta from start of the operation */
6902 dx
= mne
->root_x
- xpr
->root_x
;
6903 dy
= mne
->root_y
- xpr
->root_y
;
6909 if (opt
== SWM_ARG_ID_CENTER
) {
6910 if (g
.h
/ 2 + dy
< 1)
6914 HEIGHT(win
) = g
.h
+ 2 * dy
;
6922 HEIGHT(win
) = g
.h
+ dy
;
6929 if (opt
== SWM_ARG_ID_CENTER
) {
6930 if (g
.w
/ 2 + dx
< 1)
6934 WIDTH(win
) = g
.w
+ 2 * dx
;
6942 WIDTH(win
) = g
.w
+ dx
;
6945 /* not free, don't sync more than 120 times / second */
6946 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
6947 timestamp
= mne
->time
;
6948 regionize(win
, mne
->root_x
, mne
->root_y
);
6949 region_containment(win
, r
, SWM_CW_ALLSIDES
|
6950 SWM_CW_RESIZABLE
| SWM_CW_HARDBOUNDARY
|
6951 SWM_CW_SOFTBOUNDARY
);
6956 case XCB_BUTTON_PRESS
:
6958 DNPRINTF(SWM_D_EVENT
, "resize: BUTTON_PRESS ignored\n");
6959 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
6960 ((xcb_button_press_event_t
*)evt
)->time
);
6965 DNPRINTF(SWM_D_EVENT
, "resize: KEY_PRESS ignored\n");
6966 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
6967 ((xcb_key_press_event_t
*)evt
)->time
);
6973 /* It's possible for win to have been freed above. */
6974 if (validate_win(win
)) {
6975 DNPRINTF(SWM_D_EVENT
, "resize: invalid win.\n");
6983 region_containment(win
, r
, SWM_CW_ALLSIDES
| SWM_CW_RESIZABLE
|
6984 SWM_CW_HARDBOUNDARY
| SWM_CW_SOFTBOUNDARY
);
6988 store_float_geom(win
);
6990 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
6992 DNPRINTF(SWM_D_EVENT
, "resize: done.\n");
6996 resize(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
6998 struct ws_win
*win
= NULL
;
6999 xcb_query_pointer_reply_t
*qpr
= NULL
;
7004 if (args
->id
!= SWM_ARG_ID_DONTCENTER
&&
7005 args
->id
!= SWM_ARG_ID_CENTER
) {
7006 /* {height,width}_{grow,shrink} use the focus window. */
7010 /* resize uses window under pointer. */
7011 qpr
= xcb_query_pointer_reply(conn
,
7012 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7014 win
= find_window(qpr
->child
);
7020 resize_win(win
, bp
, args
->id
);
7022 if (args
->id
&& bp
->type
== KEYBIND
)
7028 /* Try to set window region based on supplied coordinates or window center. */
7030 regionize(struct ws_win
*win
, int x
, int y
)
7032 struct swm_region
*r
= NULL
;
7034 r
= region_under(win
->s
, x
, y
);
7036 r
= region_under(win
->s
, X(win
) + WIDTH(win
) / 2,
7037 Y(win
) + HEIGHT(win
) / 2);
7039 if (r
&& r
!= win
->ws
->r
) {
7040 clear_maximized(r
->ws
);
7042 win_to_ws(win
, r
->ws
->idx
, false);
7044 /* Stack old and new region. */
7048 /* Set focus on new ws. */
7049 unfocus_win(r
->ws
->focus
);
7057 #define SWM_MOVE_STEPS (50)
7060 move_win(struct ws_win
*win
, struct binding
*bp
, int opt
)
7062 struct swm_region
*r
;
7063 xcb_timestamp_t timestamp
= 0;
7064 xcb_query_pointer_reply_t
*qpr
= NULL
;
7065 xcb_generic_event_t
*evt
;
7066 xcb_motion_notify_event_t
*mne
;
7067 bool moving
, restack
= false, step
= false;
7072 if ((r
= win
->ws
->r
) == NULL
)
7075 if (FULLSCREEN(win
))
7078 DNPRINTF(SWM_D_EVENT
, "move: win %#x, floating: %s, transient: "
7079 "%#x\n", win
->id
, YESNO(ABOVE(win
)), win
->transient
);
7081 /* in max_stack mode should only move transients */
7082 if (win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
] && !TRANS(win
))
7085 if (!(ABOVE(win
) || TRANS(win
)) || MAXIMIZED(win
)) {
7086 store_float_geom(win
);
7090 ewmh_apply_flags(win
, (win
->ewmh_flags
| SWM_F_MANUAL
| EWMH_F_ABOVE
) &
7092 ewmh_update_wm_state(win
);
7099 /* It's possible for win to have been freed during focus_flush(). */
7100 if (validate_win(win
)) {
7101 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7106 case SWM_ARG_ID_MOVELEFT
:
7107 X(win
) -= (SWM_MOVE_STEPS
- border_width
);
7110 case SWM_ARG_ID_MOVERIGHT
:
7111 X(win
) += (SWM_MOVE_STEPS
- border_width
);
7114 case SWM_ARG_ID_MOVEUP
:
7115 Y(win
) -= (SWM_MOVE_STEPS
- border_width
);
7118 case SWM_ARG_ID_MOVEDOWN
:
7119 Y(win
) += (SWM_MOVE_STEPS
- border_width
);
7126 regionize(win
, -1, -1);
7127 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
);
7129 store_float_geom(win
);
7133 xcb_grab_pointer(conn
, 0, win
->id
, MOUSEMASK
,
7134 XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
7135 XCB_WINDOW_NONE
, cursors
[XC_FLEUR
].cid
, XCB_CURRENT_TIME
);
7137 /* get cursor offset from window root */
7138 qpr
= xcb_query_pointer_reply(conn
, xcb_query_pointer(conn
, win
->id
),
7141 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7145 /* Release keyboard freeze if called via keybind. */
7146 if (bp
->type
== KEYBIND
)
7147 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7150 regionize(win
, qpr
->root_x
, qpr
->root_y
);
7151 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7152 SWM_CW_SOFTBOUNDARY
);
7156 while (moving
&& (evt
= get_next_event(true))) {
7157 switch (XCB_EVENT_RESPONSE_TYPE(evt
)) {
7158 case XCB_BUTTON_RELEASE
:
7159 if (bp
->type
== BTNBIND
&& bp
->value
==
7160 ((xcb_button_release_event_t
*)evt
)->detail
)
7163 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7164 ((xcb_button_release_event_t
*)evt
)->time
);
7167 case XCB_KEY_RELEASE
:
7168 if (keybindreleased(bp
, (xcb_key_release_event_t
*)evt
))
7171 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7172 ((xcb_key_release_event_t
*)evt
)->time
);
7175 case XCB_MOTION_NOTIFY
:
7176 mne
= (xcb_motion_notify_event_t
*)evt
;
7177 DNPRINTF(SWM_D_EVENT
, "move: MOTION_NOTIFY: "
7178 "root: %#x\n", mne
->root
);
7179 X(win
) = mne
->root_x
- qpr
->win_x
- border_width
;
7180 Y(win
) = mne
->root_y
- qpr
->win_y
- border_width
;
7182 /* not free, don't sync more than 120 times / second */
7183 if ((mne
->time
- timestamp
) > (1000 / 120) ) {
7184 timestamp
= mne
->time
;
7185 regionize(win
, mne
->root_x
, mne
->root_y
);
7186 region_containment(win
, win
->ws
->r
,
7187 SWM_CW_ALLSIDES
| SWM_CW_SOFTBOUNDARY
);
7192 case XCB_BUTTON_PRESS
:
7193 /* Thaw and ignore. */
7194 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_POINTER
,
7195 ((xcb_button_press_event_t
*)evt
)->time
);
7200 xcb_allow_events(conn
, XCB_ALLOW_ASYNC_KEYBOARD
,
7201 ((xcb_key_press_event_t
*)evt
)->time
);
7207 /* It's possible for win to have been freed. */
7208 if (validate_win(win
)) {
7209 DNPRINTF(SWM_D_EVENT
, "move: invalid win.\n");
7217 region_containment(win
, win
->ws
->r
, SWM_CW_ALLSIDES
|
7218 SWM_CW_SOFTBOUNDARY
);
7222 store_float_geom(win
);
7224 /* New region set to fullscreen layout. */
7225 if (win
->ws
->r
&& win
->ws
->cur_layout
== &layouts
[SWM_MAX_STACK
]) {
7232 xcb_ungrab_pointer(conn
, XCB_CURRENT_TIME
);
7233 DNPRINTF(SWM_D_EVENT
, "move: done.\n");
7237 move(struct binding
*bp
, struct swm_region
*r
, union arg
*args
)
7239 struct ws_win
*win
= NULL
;
7240 xcb_query_pointer_reply_t
*qpr
= NULL
;
7246 /* move_* uses focus window. */
7250 /* Disallow move_ on tiled. */
7251 if (win
&& !(TRANS(win
) || ABOVE(win
)))
7254 /* move uses window under pointer. */
7255 qpr
= xcb_query_pointer_reply(conn
,
7256 xcb_query_pointer(conn
, r
->s
->root
), NULL
);
7258 win
= find_window(qpr
->child
);
7264 move_win(win
, bp
, args
->id
);
7266 if (args
->id
&& bp
->type
== KEYBIND
)
7272 /* action definitions */
7274 char name
[SWM_FUNCNAME_LEN
];
7275 void (*func
)(struct binding
*, struct swm_region
*,
7279 } actions
[FN_INVALID
+ 2] = {
7280 /* name function argument */
7281 { "bar_toggle", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE
} },
7282 { "bar_toggle_ws", bar_toggle
, 0, {.id
= SWM_ARG_ID_BAR_TOGGLE_WS
} },
7283 { "button2", pressbutton
, 0, {.id
= 2} },
7284 { "cycle_layout", cycle_layout
, 0, {0} },
7285 { "flip_layout", stack_config
, 0, {.id
= SWM_ARG_ID_FLIPLAYOUT
} },
7286 { "float_toggle", floating_toggle
,0, {0} },
7287 { "focus", focus_pointer
, 0, {0} },
7288 { "focus_main", focus
, 0, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
7289 { "focus_next", focus
, 0, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
7290 { "focus_prev", focus
, 0, {.id
= SWM_ARG_ID_FOCUSPREV
} },
7291 { "focus_urgent", focus
, 0, {.id
= SWM_ARG_ID_FOCUSURGENT
} },
7292 { "maximize_toggle", maximize_toggle
,0, {0} },
7293 { "height_grow", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTGROW
} },
7294 { "height_shrink", resize
, 0, {.id
= SWM_ARG_ID_HEIGHTSHRINK
} },
7295 { "iconify", iconify
, 0, {0} },
7296 { "master_shrink", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
7297 { "master_grow", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERGROW
} },
7298 { "master_add", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERADD
} },
7299 { "master_del", stack_config
, 0, {.id
= SWM_ARG_ID_MASTERDEL
} },
7300 { "move", move
, FN_F_NOREPLAY
, {0} },
7301 { "move_down", move
, 0, {.id
= SWM_ARG_ID_MOVEDOWN
} },
7302 { "move_left", move
, 0, {.id
= SWM_ARG_ID_MOVELEFT
} },
7303 { "move_right", move
, 0, {.id
= SWM_ARG_ID_MOVERIGHT
} },
7304 { "move_up", move
, 0, {.id
= SWM_ARG_ID_MOVEUP
} },
7305 { "mvrg_1", send_to_rg
, 0, {.id
= 0} },
7306 { "mvrg_2", send_to_rg
, 0, {.id
= 1} },
7307 { "mvrg_3", send_to_rg
, 0, {.id
= 2} },
7308 { "mvrg_4", send_to_rg
, 0, {.id
= 3} },
7309 { "mvrg_5", send_to_rg
, 0, {.id
= 4} },
7310 { "mvrg_6", send_to_rg
, 0, {.id
= 5} },
7311 { "mvrg_7", send_to_rg
, 0, {.id
= 6} },
7312 { "mvrg_8", send_to_rg
, 0, {.id
= 7} },
7313 { "mvrg_9", send_to_rg
, 0, {.id
= 8} },
7314 { "mvws_1", send_to_ws
, 0, {.id
= 0} },
7315 { "mvws_2", send_to_ws
, 0, {.id
= 1} },
7316 { "mvws_3", send_to_ws
, 0, {.id
= 2} },
7317 { "mvws_4", send_to_ws
, 0, {.id
= 3} },
7318 { "mvws_5", send_to_ws
, 0, {.id
= 4} },
7319 { "mvws_6", send_to_ws
, 0, {.id
= 5} },
7320 { "mvws_7", send_to_ws
, 0, {.id
= 6} },
7321 { "mvws_8", send_to_ws
, 0, {.id
= 7} },
7322 { "mvws_9", send_to_ws
, 0, {.id
= 8} },
7323 { "mvws_10", send_to_ws
, 0, {.id
= 9} },
7324 { "mvws_11", send_to_ws
, 0, {.id
= 10} },
7325 { "mvws_12", send_to_ws
, 0, {.id
= 11} },
7326 { "mvws_13", send_to_ws
, 0, {.id
= 12} },
7327 { "mvws_14", send_to_ws
, 0, {.id
= 13} },
7328 { "mvws_15", send_to_ws
, 0, {.id
= 14} },
7329 { "mvws_16", send_to_ws
, 0, {.id
= 15} },
7330 { "mvws_17", send_to_ws
, 0, {.id
= 16} },
7331 { "mvws_18", send_to_ws
, 0, {.id
= 17} },
7332 { "mvws_19", send_to_ws
, 0, {.id
= 18} },
7333 { "mvws_20", send_to_ws
, 0, {.id
= 19} },
7334 { "mvws_21", send_to_ws
, 0, {.id
= 20} },
7335 { "mvws_22", send_to_ws
, 0, {.id
= 21} },
7336 { "name_workspace", name_workspace
, 0, {0} },
7337 { "quit", quit
, 0, {0} },
7338 { "raise_toggle", raise_toggle
, 0, {0} },
7339 { "resize", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_DONTCENTER
} },
7340 { "resize_centered", resize
, FN_F_NOREPLAY
, {.id
= SWM_ARG_ID_CENTER
} },
7341 { "restart", restart
, 0, {0} },
7342 { "rg_1", focusrg
, 0, {.id
= 0} },
7343 { "rg_2", focusrg
, 0, {.id
= 1} },
7344 { "rg_3", focusrg
, 0, {.id
= 2} },
7345 { "rg_4", focusrg
, 0, {.id
= 3} },
7346 { "rg_5", focusrg
, 0, {.id
= 4} },
7347 { "rg_6", focusrg
, 0, {.id
= 5} },
7348 { "rg_7", focusrg
, 0, {.id
= 6} },
7349 { "rg_8", focusrg
, 0, {.id
= 7} },
7350 { "rg_9", focusrg
, 0, {.id
= 8} },
7351 { "rg_move_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_UP
} },
7352 { "rg_move_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_MOVE_DOWN
} },
7353 { "rg_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7354 { "rg_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7355 { "screen_next", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_UP
} },
7356 { "screen_prev", cyclerg
, 0, {.id
= SWM_ARG_ID_CYCLERG_DOWN
} },
7357 { "search_win", search_win
, 0, {0} },
7358 { "search_workspace", search_workspace
, 0, {0} },
7359 { "spawn_custom", NULL
, 0, {0} },
7360 { "stack_balance", stack_config
, 0, {.id
= SWM_ARG_ID_STACKBALANCE
} },
7361 { "stack_inc", stack_config
, 0, {.id
= SWM_ARG_ID_STACKINC
} },
7362 { "stack_dec", stack_config
, 0, {.id
= SWM_ARG_ID_STACKDEC
} },
7363 { "stack_reset", stack_config
, 0, {.id
= SWM_ARG_ID_STACKRESET
} },
7364 { "swap_main", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPMAIN
} },
7365 { "swap_next", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPNEXT
} },
7366 { "swap_prev", swapwin
, 0, {.id
= SWM_ARG_ID_SWAPPREV
} },
7367 { "uniconify", uniconify
, 0, {0} },
7368 { "version", version
, 0, {0} },
7369 { "width_grow", resize
, 0, {.id
= SWM_ARG_ID_WIDTHGROW
} },
7370 { "width_shrink", resize
, 0, {.id
= SWM_ARG_ID_WIDTHSHRINK
} },
7371 { "wind_del", wkill
, 0, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
7372 { "wind_kill", wkill
, 0, {.id
= SWM_ARG_ID_KILLWINDOW
} },
7373 { "ws_1", switchws
, 0, {.id
= 0} },
7374 { "ws_2", switchws
, 0, {.id
= 1} },
7375 { "ws_3", switchws
, 0, {.id
= 2} },
7376 { "ws_4", switchws
, 0, {.id
= 3} },
7377 { "ws_5", switchws
, 0, {.id
= 4} },
7378 { "ws_6", switchws
, 0, {.id
= 5} },
7379 { "ws_7", switchws
, 0, {.id
= 6} },
7380 { "ws_8", switchws
, 0, {.id
= 7} },
7381 { "ws_9", switchws
, 0, {.id
= 8} },
7382 { "ws_10", switchws
, 0, {.id
= 9} },
7383 { "ws_11", switchws
, 0, {.id
= 10} },
7384 { "ws_12", switchws
, 0, {.id
= 11} },
7385 { "ws_13", switchws
, 0, {.id
= 12} },
7386 { "ws_14", switchws
, 0, {.id
= 13} },
7387 { "ws_15", switchws
, 0, {.id
= 14} },
7388 { "ws_16", switchws
, 0, {.id
= 15} },
7389 { "ws_17", switchws
, 0, {.id
= 16} },
7390 { "ws_18", switchws
, 0, {.id
= 17} },
7391 { "ws_19", switchws
, 0, {.id
= 18} },
7392 { "ws_20", switchws
, 0, {.id
= 19} },
7393 { "ws_21", switchws
, 0, {.id
= 20} },
7394 { "ws_22", switchws
, 0, {.id
= 21} },
7395 { "ws_next", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
7396 { "ws_next_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_UP_ALL
} },
7397 { "ws_next_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_UP
} },
7398 { "ws_prev", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
7399 { "ws_prev_all", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_DOWN_ALL
} },
7400 { "ws_prev_move", cyclews
, 0, {.id
= SWM_ARG_ID_CYCLEWS_MOVE_DOWN
} },
7401 { "ws_prior", priorws
, 0, {0} },
7402 /* SWM_DEBUG actions MUST be here: */
7403 { "debug_toggle", debug_toggle
, 0, {0} },
7404 { "dumpwins", dumpwins
, 0, {0} },
7406 { "invalid action", NULL
, 0, {0} },
7410 update_modkey(uint16_t mod
)
7414 /* Replace all instances of the old mod key. */
7415 RB_FOREACH(bp
, binding_tree
, &bindings
)
7416 if (bp
->mod
& mod_key
)
7417 bp
->mod
= (bp
->mod
& ~mod_key
) | mod
;
7422 spawn_expand(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7425 struct spawn_prog
*prog
= NULL
;
7427 char *ap
, **real_args
;
7429 /* suppress unused warning since var is needed */
7432 DNPRINTF(SWM_D_SPAWN
, "spawn_expand: %s\n", spawn_name
);
7435 TAILQ_FOREACH(prog
, &spawns
, entry
) {
7436 if (strcasecmp(spawn_name
, prog
->name
) == 0)
7440 warnx("spawn_custom: program %s not found", spawn_name
);
7444 /* make room for expanded args */
7445 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
7446 err(1, "spawn_custom: calloc real_args");
7448 /* expand spawn_args into real_args */
7449 for (i
= c
= 0; i
< prog
->argc
; i
++) {
7451 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg: %s\n", ap
);
7452 if (strcasecmp(ap
, "$bar_border") == 0) {
7454 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
7456 err(1, "spawn_custom border color");
7457 } else if (strcasecmp(ap
, "$bar_color") == 0) {
7459 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
7461 err(1, "spawn_custom bar color");
7462 } else if (strcasecmp(ap
, "$bar_font") == 0) {
7463 if ((real_args
[c
] = strdup(bar_fonts
))
7465 err(1, "spawn_custom bar fonts");
7466 } else if (strcasecmp(ap
, "$bar_font_color") == 0) {
7468 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
7470 err(1, "spawn_custom color font");
7471 } else if (strcasecmp(ap
, "$color_focus") == 0) {
7473 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
7475 err(1, "spawn_custom color focus");
7476 } else if (strcasecmp(ap
, "$color_focus_maximized") == 0) {
7478 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].name
))
7480 err(1, "spawn_custom color focus maximized");
7481 } else if (strcasecmp(ap
, "$color_unfocus") == 0) {
7483 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
7485 err(1, "spawn_custom color unfocus");
7486 } else if (strcasecmp(ap
, "$color_unfocus_maximized") == 0) {
7488 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].name
))
7490 err(1, "spawn_custom color unfocus maximized");
7491 } else if (strcasecmp(ap
, "$region_index") == 0) {
7492 if (asprintf(&real_args
[c
], "%d",
7493 get_region_index(r
) + 1) < 1)
7494 err(1, "spawn_custom region index");
7495 } else if (strcasecmp(ap
, "$workspace_index") == 0) {
7496 if (asprintf(&real_args
[c
], "%d", r
->ws
->idx
+ 1) < 1)
7497 err(1, "spawn_custom workspace index");
7498 } else if (strcasecmp(ap
, "$dmenu_bottom") == 0) {
7501 if ((real_args
[c
] = strdup("-b")) == NULL
)
7502 err(1, "spawn_custom workspace index");
7504 /* no match --> copy as is */
7505 if ((real_args
[c
] = strdup(ap
)) == NULL
)
7506 err(1, "spawn_custom strdup(ap)");
7508 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg: %s\n",
7514 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: result: ");
7515 for (i
= 0; i
< c
; ++i
)
7516 DPRINTF("\"%s\" ", real_args
[i
]);
7519 *ret_args
= real_args
;
7524 spawn_custom(struct swm_region
*r
, union arg
*args
, const char *spawn_name
)
7530 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7534 spawn(r
->ws
->idx
, &a
, true);
7536 for (i
= 0; i
< spawn_argc
; i
++)
7542 spawn_select(struct swm_region
*r
, union arg
*args
, const char *spawn_name
,
7549 if ((spawn_argc
= spawn_expand(r
, args
, spawn_name
, &real_args
)) < 0)
7553 if (pipe(select_list_pipe
) == -1)
7554 err(1, "pipe error");
7555 if (pipe(select_resp_pipe
) == -1)
7556 err(1, "pipe error");
7558 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
7559 err(1, "could not disable SIGPIPE");
7560 switch (*pid
= fork()) {
7562 err(1, "cannot fork");
7565 if (dup2(select_list_pipe
[0], STDIN_FILENO
) == -1)
7567 if (dup2(select_resp_pipe
[1], STDOUT_FILENO
) == -1)
7569 close(select_list_pipe
[1]);
7570 close(select_resp_pipe
[0]);
7571 spawn(r
->ws
->idx
, &a
, false);
7573 default: /* parent */
7574 close(select_list_pipe
[0]);
7575 close(select_resp_pipe
[1]);
7579 for (i
= 0; i
< spawn_argc
; i
++)
7584 /* Argument tokenizer. */
7587 char *arg
, *cp
, *next
;
7588 bool single_quoted
= false, double_quoted
= false;
7593 /* Eat and move characters until end of argument is found. */
7594 for (arg
= next
= cp
= *sp
; *cp
!= '\0'; ++cp
) {
7595 if (!double_quoted
&& *cp
== '\'') {
7596 /* Eat single-quote. */
7597 single_quoted
= !single_quoted
;
7598 } else if (!single_quoted
&& *cp
== '"') {
7599 /* Eat double-quote. */
7600 double_quoted
= !double_quoted
;
7601 } else if (!single_quoted
&& *cp
== '\\' && *(cp
+ 1) == '"') {
7602 /* Eat backslash; copy escaped character to arg. */
7604 } else if (!single_quoted
&& !double_quoted
&& *cp
== '\\' &&
7605 (*(cp
+ 1) == '\'' || *(cp
+ 1) == ' ')) {
7606 /* Eat backslash; move escaped character. */
7608 } else if (!single_quoted
&& !double_quoted
&&
7609 (*cp
== ' ' || *cp
== '\t')) {
7610 /* Terminate argument. */
7612 /* Point sp to beginning of next argument. */
7616 /* Move regular character. */
7621 /* Terminate argument if end of string. */
7631 spawn_insert(const char *name
, const char *args
, int flags
)
7633 struct spawn_prog
*sp
;
7634 char *arg
, *cp
, *ptr
;
7636 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: %s[%s]\n", name
, args
);
7638 if (args
== NULL
|| *args
== '\0')
7641 if ((sp
= calloc(1, sizeof *sp
)) == NULL
)
7642 err(1, "spawn_insert: calloc");
7643 if ((sp
->name
= strdup(name
)) == NULL
)
7644 err(1, "spawn_insert: strdup");
7646 /* Convert the arguments to an argument list. */
7647 if ((ptr
= cp
= strdup(args
)) == NULL
)
7648 err(1, "spawn_insert: strdup");
7649 while ((arg
= argsep(&ptr
)) != NULL
) {
7650 /* Null argument; skip it. */
7655 if ((sp
->argv
= realloc(sp
->argv
, sp
->argc
*
7656 sizeof *sp
->argv
)) == NULL
)
7657 err(1, "spawn_insert: realloc");
7658 if ((sp
->argv
[sp
->argc
- 1] = strdup(arg
)) == NULL
)
7659 err(1, "spawn_insert: strdup");
7665 DNPRINTF(SWM_D_SPAWN
, "arg %d: [%s]\n", sp
->argc
, sp
->argv
[sp
->argc
-1]);
7666 TAILQ_INSERT_TAIL(&spawns
, sp
, entry
);
7667 DNPRINTF(SWM_D_SPAWN
, "spawn_insert: leave\n");
7671 spawn_remove(struct spawn_prog
*sp
)
7675 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: %s\n", sp
->name
);
7677 TAILQ_REMOVE(&spawns
, sp
, entry
);
7678 for (i
= 0; i
< sp
->argc
; i
++)
7684 DNPRINTF(SWM_D_SPAWN
, "spawn_remove: leave\n");
7690 struct spawn_prog
*sp
;
7692 while ((sp
= TAILQ_FIRST(&spawns
)) != NULL
) {
7698 spawn_find(const char *name
)
7700 struct spawn_prog
*sp
;
7702 TAILQ_FOREACH(sp
, &spawns
, entry
)
7703 if (strcasecmp(sp
->name
, name
) == 0)
7710 setspawn(const char *name
, const char *args
, int flags
)
7712 struct spawn_prog
*sp
;
7714 DNPRINTF(SWM_D_SPAWN
, "setspawn: %s\n", name
);
7719 /* Remove any old spawn under the same name. */
7720 if ((sp
= spawn_find(name
)) != NULL
)
7724 spawn_insert(name
, args
, flags
);
7726 warnx("error: setspawn: cannot find program: %s", name
);
7728 DNPRINTF(SWM_D_SPAWN
, "setspawn: leave\n");
7732 setconfspawn(const char *selector
, const char *value
, int flags
)
7736 if (selector
== NULL
|| strlen(selector
) == 0)
7739 args
= expand_tilde(value
);
7741 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, args
);
7743 setspawn(selector
, args
, flags
);
7746 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done.\n");
7751 validate_spawns(void)
7754 struct spawn_prog
*sp
;
7755 char which
[PATH_MAX
];
7758 RB_FOREACH(bp
, binding_tree
, &bindings
) {
7759 if (bp
->action
!= FN_SPAWN_CUSTOM
)
7763 sp
= spawn_find(bp
->spawn_name
);
7764 if (sp
== NULL
|| sp
->flags
& SWM_SPAWN_OPTIONAL
)
7767 /* verify we have the goods */
7768 snprintf(which
, sizeof which
, "which %s", sp
->argv
[0]);
7769 DNPRINTF(SWM_D_CONF
, "validate_spawns: which %s\n",
7771 for (i
= strlen("which "); i
< strlen(which
); i
++)
7772 if (which
[i
] == ' ') {
7776 if (system(which
) != 0)
7777 add_startup_exception("could not find %s",
7778 &which
[strlen("which ")]);
7785 setconfspawn("lock", "xlock", 0);
7787 setconfspawn("term", "xterm", 0);
7788 setconfspawn("spawn_term", "xterm", 0);
7790 setconfspawn("menu", "dmenu_run"
7794 " -nf $bar_font_color"
7796 " -sf $bar_color", 0);
7798 setconfspawn("search", "dmenu"
7803 " -nf $bar_font_color"
7805 " -sf $bar_color", 0);
7807 setconfspawn("name_workspace", "dmenu"
7812 " -nf $bar_font_color"
7814 " -sf $bar_color", 0);
7816 /* These are not verified for existence, even with a binding set. */
7817 setconfspawn("screenshot_all", "screenshot.sh full", SWM_SPAWN_OPTIONAL
);
7818 setconfspawn("screenshot_wind", "screenshot.sh window", SWM_SPAWN_OPTIONAL
);
7819 setconfspawn("initscr", "initscreen.sh", SWM_SPAWN_OPTIONAL
);
7823 #define SWM_MODNAME_SIZE 32
7824 #define SWM_KEY_WS "\n+ \t"
7826 parsebinding(const char *bindstr
, uint16_t *mod
, enum binding_type
*type
,
7827 uint32_t *val
, uint32_t *flags
)
7829 char *str
, *cp
, *name
;
7830 KeySym ks
, lks
, uks
;
7832 DNPRINTF(SWM_D_KEY
, "parsebinding: enter [%s]\n", bindstr
);
7833 if (mod
== NULL
|| val
== NULL
) {
7834 DNPRINTF(SWM_D_KEY
, "parsebinding: no mod or key vars\n");
7837 if (bindstr
== NULL
|| strlen(bindstr
) == 0) {
7838 DNPRINTF(SWM_D_KEY
, "parsebinding: no bindstr\n");
7842 if ((cp
= str
= strdup(bindstr
)) == NULL
)
7843 err(1, "parsebinding: strdup");
7845 *val
= XCB_NO_SYMBOL
;
7849 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
7850 DNPRINTF(SWM_D_KEY
, "parsebinding: entry [%s]\n", name
);
7852 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
7853 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
7855 else if (strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
) == 0)
7856 *mod
|= XCB_MOD_MASK_1
;
7857 else if (strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
) == 0)
7858 *mod
|= XCB_MOD_MASK_2
;
7859 else if (strncmp(name
, "Mod3", SWM_MODNAME_SIZE
) == 0)
7860 *mod
|= XCB_MOD_MASK_3
;
7861 else if (strncmp(name
, "Mod4", SWM_MODNAME_SIZE
) == 0)
7862 *mod
|= XCB_MOD_MASK_4
;
7863 else if (strncmp(name
, "Mod5", SWM_MODNAME_SIZE
) == 0)
7864 *mod
|= XCB_MOD_MASK_5
;
7865 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
7866 *mod
|= XCB_MOD_MASK_SHIFT
;
7867 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
7868 *mod
|= XCB_MOD_MASK_CONTROL
;
7869 else if (strncasecmp(name
, "ANYMOD", SWM_MODNAME_SIZE
) == 0)
7870 *mod
|= XCB_MOD_MASK_ANY
;
7871 else if (strncasecmp(name
, "REPLAY", SWM_MODNAME_SIZE
) == 0)
7872 *flags
|= BINDING_F_REPLAY
;
7873 else if (sscanf(name
, "Button%u", val
) == 1) {
7874 DNPRINTF(SWM_D_KEY
, "parsebinding: button %u\n", *val
);
7876 if (*val
> 255 || *val
== 0) {
7878 "parsebinding: invalid button %u\n", *val
);
7882 /* TODO: do this without Xlib. */
7883 ks
= XStringToKeysym(name
);
7884 if (ks
== NoSymbol
) {
7886 "parsebinding: invalid key %s\n", name
);
7891 XConvertCase(ks
, &lks
, &uks
);
7892 *val
= (uint32_t)lks
;
7896 /* If ANYMOD was specified, ignore the rest. */
7897 if (*mod
& XCB_MOD_MASK_ANY
)
7898 *mod
= XCB_MOD_MASK_ANY
;
7901 DNPRINTF(SWM_D_KEY
, "parsebinding: leave ok\n");
7906 strdupsafe(const char *str
)
7911 return (strdup(str
));
7915 binding_cmp(struct binding
*bp1
, struct binding
*bp2
)
7917 if (bp1
->type
< bp2
->type
)
7919 if (bp1
->type
> bp2
->type
)
7922 if (bp1
->value
< bp2
->value
)
7924 if (bp1
->value
> bp2
->value
)
7927 if (bp1
->mod
< bp2
->mod
)
7929 if (bp1
->mod
> bp2
->mod
)
7936 binding_insert(uint16_t mod
, enum binding_type type
, uint32_t val
,
7937 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
7941 DNPRINTF(SWM_D_KEY
, "binding_insert: mod: %u, type: %d, val: %u, "
7942 "action: %s(%d), spawn_name: %s\n", mod
, type
, val
,
7943 actions
[aid
].name
, aid
, spawn_name
);
7945 if ((bp
= malloc(sizeof *bp
)) == NULL
)
7946 err(1, "binding_insert: malloc");
7953 bp
->spawn_name
= strdupsafe(spawn_name
);
7954 RB_INSERT(binding_tree
, &bindings
, bp
);
7956 DNPRINTF(SWM_D_KEY
, "binding_insert: leave\n");
7960 binding_lookup(uint16_t mod
, enum binding_type type
, uint32_t val
)
7968 return (RB_FIND(binding_tree
, &bindings
, &bp
));
7972 binding_remove(struct binding
*bp
)
7974 DNPRINTF(SWM_D_KEY
, "binding_remove: mod: %u, type: %d, val: %u, "
7975 "action: %s(%d), spawn_name: %s\n", bp
->mod
, bp
->type
, bp
->value
,
7976 actions
[bp
->action
].name
, bp
->action
, bp
->spawn_name
);
7978 RB_REMOVE(binding_tree
, &bindings
, bp
);
7979 free(bp
->spawn_name
);
7982 DNPRINTF(SWM_D_KEY
, "binding_remove: leave\n");
7986 setbinding(uint16_t mod
, enum binding_type type
, uint32_t val
,
7987 enum actionid aid
, uint32_t flags
, const char *spawn_name
)
7991 DNPRINTF(SWM_D_KEY
, "setbinding: enter %s [%s]\n",
7992 actions
[aid
].name
, spawn_name
);
7994 /* Unbind any existing. Loop is to handle MOD_MASK_ANY. */
7995 while ((bp
= binding_lookup(mod
, type
, val
)))
7998 if (aid
!= FN_INVALID
)
7999 binding_insert(mod
, type
, val
, aid
, flags
, spawn_name
);
8001 DNPRINTF(SWM_D_KEY
, "setbinding: leave\n");
8005 setconfbinding(const char *selector
, const char *value
, int flags
)
8007 struct spawn_prog
*sp
;
8008 uint32_t keybtn
, opts
;
8011 enum binding_type type
;
8013 /* suppress unused warning since var is needed */
8016 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter selector: [%s], "
8017 "value: [%s]\n", selector
, value
);
8018 if (selector
== NULL
|| strlen(selector
) == 0) {
8019 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
8020 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8021 setbinding(mod
, type
, keybtn
, FN_INVALID
, opts
, NULL
);
8026 /* search by key function name */
8027 for (aid
= 0; aid
< FN_INVALID
; aid
++) {
8028 if (strncasecmp(selector
, actions
[aid
].name
,
8029 SWM_FUNCNAME_LEN
) == 0) {
8030 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8031 "action\n", selector
);
8032 if (parsebinding(value
, &mod
, &type
, &keybtn
,
8034 setbinding(mod
, type
, keybtn
, aid
, opts
, NULL
);
8040 /* search by custom spawn name */
8041 if ((sp
= spawn_find(selector
)) != NULL
) {
8042 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match "
8043 "spawn\n", selector
);
8044 if (parsebinding(value
, &mod
, &type
, &keybtn
, &opts
) == 0) {
8045 setbinding(mod
, type
, keybtn
, FN_SPAWN_CUSTOM
, opts
,
8051 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
8055 #define MODSHIFT MODKEY | XCB_MOD_MASK_SHIFT
8057 setup_keybindings(void)
8059 #define BINDKEY(m, k, a) setbinding(m, KEYBIND, k, a, 0, NULL)
8060 #define BINDKEYSPAWN(m, k, s) setbinding(m, KEYBIND, k, FN_SPAWN_CUSTOM, 0, s)
8061 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8062 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8063 BINDKEY(MODKEY
, XK_b
, FN_BAR_TOGGLE
);
8064 BINDKEY(MODSHIFT
, XK_b
, FN_BAR_TOGGLE_WS
);
8065 BINDKEY(MODKEY
, XK_v
, FN_BUTTON2
);
8066 BINDKEY(MODKEY
, XK_space
, FN_CYCLE_LAYOUT
);
8067 BINDKEY(MODSHIFT
, XK_backslash
, FN_FLIP_LAYOUT
);
8068 BINDKEY(MODKEY
, XK_t
, FN_FLOAT_TOGGLE
);
8069 BINDKEY(MODKEY
, XK_m
, FN_FOCUS_MAIN
);
8070 BINDKEY(MODKEY
, XK_j
, FN_FOCUS_NEXT
);
8071 BINDKEY(MODKEY
, XK_Tab
, FN_FOCUS_NEXT
);
8072 BINDKEY(MODKEY
, XK_k
, FN_FOCUS_PREV
);
8073 BINDKEY(MODSHIFT
, XK_Tab
, FN_FOCUS_PREV
);
8074 BINDKEY(MODKEY
, XK_u
, FN_FOCUS_URGENT
);
8075 BINDKEY(MODKEY
, XK_e
, FN_MAXIMIZE_TOGGLE
);
8076 BINDKEY(MODSHIFT
, XK_equal
, FN_HEIGHT_GROW
);
8077 BINDKEY(MODSHIFT
, XK_minus
, FN_HEIGHT_SHRINK
);
8078 BINDKEY(MODKEY
, XK_w
, FN_ICONIFY
);
8079 BINDKEY(MODKEY
, XK_h
, FN_MASTER_SHRINK
);
8080 BINDKEY(MODKEY
, XK_l
, FN_MASTER_GROW
);
8081 BINDKEY(MODKEY
, XK_comma
, FN_MASTER_ADD
);
8082 BINDKEY(MODKEY
, XK_period
, FN_MASTER_DEL
);
8083 BINDKEY(MODSHIFT
, XK_bracketright
, FN_MOVE_DOWN
);
8084 BINDKEY(MODKEY
, XK_bracketleft
, FN_MOVE_LEFT
);
8085 BINDKEY(MODKEY
, XK_bracketright
, FN_MOVE_RIGHT
);
8086 BINDKEY(MODSHIFT
, XK_bracketleft
, FN_MOVE_UP
);
8087 BINDKEY(MODSHIFT
, XK_KP_End
, FN_MVRG_1
);
8088 BINDKEY(MODSHIFT
, XK_KP_Down
, FN_MVRG_2
);
8089 BINDKEY(MODSHIFT
, XK_KP_Next
, FN_MVRG_3
);
8090 BINDKEY(MODSHIFT
, XK_KP_Left
, FN_MVRG_4
);
8091 BINDKEY(MODSHIFT
, XK_KP_Begin
, FN_MVRG_5
);
8092 BINDKEY(MODSHIFT
, XK_KP_Right
, FN_MVRG_6
);
8093 BINDKEY(MODSHIFT
, XK_KP_Home
, FN_MVRG_7
);
8094 BINDKEY(MODSHIFT
, XK_KP_Up
, FN_MVRG_8
);
8095 BINDKEY(MODSHIFT
, XK_KP_Prior
, FN_MVRG_9
);
8096 BINDKEY(MODSHIFT
, XK_1
, FN_MVWS_1
);
8097 BINDKEY(MODSHIFT
, XK_2
, FN_MVWS_2
);
8098 BINDKEY(MODSHIFT
, XK_3
, FN_MVWS_3
);
8099 BINDKEY(MODSHIFT
, XK_4
, FN_MVWS_4
);
8100 BINDKEY(MODSHIFT
, XK_5
, FN_MVWS_5
);
8101 BINDKEY(MODSHIFT
, XK_6
, FN_MVWS_6
);
8102 BINDKEY(MODSHIFT
, XK_7
, FN_MVWS_7
);
8103 BINDKEY(MODSHIFT
, XK_8
, FN_MVWS_8
);
8104 BINDKEY(MODSHIFT
, XK_9
, FN_MVWS_9
);
8105 BINDKEY(MODSHIFT
, XK_0
, FN_MVWS_10
);
8106 BINDKEY(MODSHIFT
, XK_F1
, FN_MVWS_11
);
8107 BINDKEY(MODSHIFT
, XK_F2
, FN_MVWS_12
);
8108 BINDKEY(MODSHIFT
, XK_F3
, FN_MVWS_13
);
8109 BINDKEY(MODSHIFT
, XK_F4
, FN_MVWS_14
);
8110 BINDKEY(MODSHIFT
, XK_F5
, FN_MVWS_15
);
8111 BINDKEY(MODSHIFT
, XK_F6
, FN_MVWS_16
);
8112 BINDKEY(MODSHIFT
, XK_F7
, FN_MVWS_17
);
8113 BINDKEY(MODSHIFT
, XK_F8
, FN_MVWS_18
);
8114 BINDKEY(MODSHIFT
, XK_F9
, FN_MVWS_19
);
8115 BINDKEY(MODSHIFT
, XK_F10
, FN_MVWS_20
);
8116 BINDKEY(MODSHIFT
, XK_F11
, FN_MVWS_21
);
8117 BINDKEY(MODSHIFT
, XK_F12
, FN_MVWS_22
);
8118 BINDKEY(MODSHIFT
, XK_slash
, FN_NAME_WORKSPACE
);
8119 BINDKEY(MODSHIFT
, XK_q
, FN_QUIT
);
8120 BINDKEY(MODSHIFT
, XK_r
, FN_RAISE_TOGGLE
);
8121 BINDKEY(MODKEY
, XK_q
, FN_RESTART
);
8122 BINDKEY(MODKEY
, XK_KP_End
, FN_RG_1
);
8123 BINDKEY(MODKEY
, XK_KP_Down
, FN_RG_2
);
8124 BINDKEY(MODKEY
, XK_KP_Next
, FN_RG_3
);
8125 BINDKEY(MODKEY
, XK_KP_Left
, FN_RG_4
);
8126 BINDKEY(MODKEY
, XK_KP_Begin
, FN_RG_5
);
8127 BINDKEY(MODKEY
, XK_KP_Right
, FN_RG_6
);
8128 BINDKEY(MODKEY
, XK_KP_Home
, FN_RG_7
);
8129 BINDKEY(MODKEY
, XK_KP_Up
, FN_RG_8
);
8130 BINDKEY(MODKEY
, XK_KP_Prior
, FN_RG_9
);
8131 BINDKEY(MODSHIFT
, XK_Right
, FN_RG_NEXT
);
8132 BINDKEY(MODSHIFT
, XK_Left
, FN_RG_PREV
);
8133 BINDKEY(MODKEY
, XK_f
, FN_SEARCH_WIN
);
8134 BINDKEY(MODKEY
, XK_slash
, FN_SEARCH_WORKSPACE
);
8135 BINDKEYSPAWN(MODSHIFT
, XK_i
, "initscr");
8136 BINDKEYSPAWN(MODSHIFT
, XK_Delete
, "lock");
8137 BINDKEYSPAWN(MODKEY
, XK_p
, "menu");
8138 BINDKEYSPAWN(MODKEY
, XK_s
, "screenshot_all");
8139 BINDKEYSPAWN(MODSHIFT
, XK_s
, "screenshot_wind");
8140 BINDKEYSPAWN(MODSHIFT
, XK_Return
, "term");
8141 BINDKEY(MODSHIFT
, XK_comma
, FN_STACK_INC
);
8142 BINDKEY(MODSHIFT
, XK_period
, FN_STACK_DEC
);
8143 BINDKEY(MODSHIFT
, XK_space
, FN_STACK_RESET
);
8144 BINDKEY(MODKEY
, XK_Return
, FN_SWAP_MAIN
);
8145 BINDKEY(MODSHIFT
, XK_j
, FN_SWAP_NEXT
);
8146 BINDKEY(MODSHIFT
, XK_k
, FN_SWAP_PREV
);
8147 BINDKEY(MODSHIFT
, XK_w
, FN_UNICONIFY
);
8148 BINDKEY(MODSHIFT
, XK_v
, FN_VERSION
);
8149 BINDKEY(MODKEY
, XK_equal
, FN_WIDTH_GROW
);
8150 BINDKEY(MODKEY
, XK_minus
, FN_WIDTH_SHRINK
);
8151 BINDKEY(MODKEY
, XK_x
, FN_WIND_DEL
);
8152 BINDKEY(MODSHIFT
, XK_x
, FN_WIND_KILL
);
8153 BINDKEY(MODKEY
, XK_1
, FN_WS_1
);
8154 BINDKEY(MODKEY
, XK_2
, FN_WS_2
);
8155 BINDKEY(MODKEY
, XK_3
, FN_WS_3
);
8156 BINDKEY(MODKEY
, XK_4
, FN_WS_4
);
8157 BINDKEY(MODKEY
, XK_5
, FN_WS_5
);
8158 BINDKEY(MODKEY
, XK_6
, FN_WS_6
);
8159 BINDKEY(MODKEY
, XK_7
, FN_WS_7
);
8160 BINDKEY(MODKEY
, XK_8
, FN_WS_8
);
8161 BINDKEY(MODKEY
, XK_9
, FN_WS_9
);
8162 BINDKEY(MODKEY
, XK_0
, FN_WS_10
);
8163 BINDKEY(MODKEY
, XK_F1
, FN_WS_11
);
8164 BINDKEY(MODKEY
, XK_F2
, FN_WS_12
);
8165 BINDKEY(MODKEY
, XK_F3
, FN_WS_13
);
8166 BINDKEY(MODKEY
, XK_F4
, FN_WS_14
);
8167 BINDKEY(MODKEY
, XK_F5
, FN_WS_15
);
8168 BINDKEY(MODKEY
, XK_F6
, FN_WS_16
);
8169 BINDKEY(MODKEY
, XK_F7
, FN_WS_17
);
8170 BINDKEY(MODKEY
, XK_F8
, FN_WS_18
);
8171 BINDKEY(MODKEY
, XK_F9
, FN_WS_19
);
8172 BINDKEY(MODKEY
, XK_F10
, FN_WS_20
);
8173 BINDKEY(MODKEY
, XK_F11
, FN_WS_21
);
8174 BINDKEY(MODKEY
, XK_F12
, FN_WS_22
);
8175 BINDKEY(MODKEY
, XK_Right
, FN_WS_NEXT
);
8176 BINDKEY(MODKEY
, XK_Left
, FN_WS_PREV
);
8177 BINDKEY(MODKEY
, XK_Up
, FN_WS_NEXT_ALL
);
8178 BINDKEY(MODKEY
, XK_Down
, FN_WS_PREV_ALL
);
8179 BINDKEY(MODSHIFT
, XK_Up
, FN_WS_NEXT_MOVE
);
8180 BINDKEY(MODSHIFT
, XK_Down
, FN_WS_PREV_MOVE
);
8181 BINDKEY(MODKEY
, XK_a
, FN_WS_PRIOR
);
8183 BINDKEY(MODKEY
, XK_d
, FN_DEBUG_TOGGLE
);
8184 BINDKEY(MODSHIFT
, XK_d
, FN_DUMPWINS
);
8191 setup_btnbindings(void)
8193 setbinding(ANYMOD
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_FOCUS
,
8194 BINDING_F_REPLAY
, NULL
);
8195 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE
, 0, NULL
);
8196 setbinding(MODSHIFT
, BTNBIND
, XCB_BUTTON_INDEX_3
, FN_RESIZE_CENTERED
, 0,
8198 setbinding(MODKEY
, BTNBIND
, XCB_BUTTON_INDEX_1
, FN_MOVE
, 0, NULL
);
8203 clear_bindings(void)
8207 while ((bp
= RB_ROOT(&bindings
)))
8212 clear_keybindings(void)
8214 struct binding
*bp
, *bptmp
;
8216 RB_FOREACH_SAFE(bp
, binding_tree
, &bindings
, bptmp
) {
8217 if (bp
->type
!= KEYBIND
)
8224 setkeymapping(const char *selector
, const char *value
, int flags
)
8226 char *keymapping_file
;
8228 /* suppress unused warnings since vars are needed */
8232 DNPRINTF(SWM_D_KEY
, "setkeymapping: enter\n");
8234 keymapping_file
= expand_tilde(value
);
8236 clear_keybindings();
8237 /* load new key bindings; if it fails, revert to default bindings */
8238 if (conf_load(keymapping_file
, SWM_CONF_KEYMAPPING
)) {
8239 clear_keybindings();
8240 setup_keybindings();
8243 free(keymapping_file
);
8245 DNPRINTF(SWM_D_KEY
, "setkeymapping: leave\n");
8250 updatenumlockmask(void)
8253 xcb_get_modifier_mapping_reply_t
*modmap_r
;
8254 xcb_keycode_t
*modmap
, kc
, *keycode
;
8258 modmap_r
= xcb_get_modifier_mapping_reply(conn
,
8259 xcb_get_modifier_mapping(conn
),
8262 modmap
= xcb_get_modifier_mapping_keycodes(modmap_r
);
8263 for (i
= 0; i
< 8; i
++) {
8264 for (j
= 0; j
< modmap_r
->keycodes_per_modifier
; j
++) {
8265 kc
= modmap
[i
* modmap_r
->keycodes_per_modifier
8267 keycode
= xcb_key_symbols_get_keycode(syms
,
8271 numlockmask
= (1 << i
);
8278 DNPRINTF(SWM_D_MISC
, "updatenumlockmask: %d\n", numlockmask
);
8285 int num_screens
, k
, j
;
8286 uint16_t modifiers
[4];
8287 xcb_keycode_t
*code
;
8289 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
8290 updatenumlockmask();
8293 modifiers
[1] = numlockmask
;
8294 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8295 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8297 num_screens
= get_screen_count();
8298 for (k
= 0; k
< num_screens
; k
++) {
8299 if (TAILQ_EMPTY(&screens
[k
].rl
))
8301 xcb_ungrab_key(conn
, XCB_GRAB_ANY
, screens
[k
].root
,
8303 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8304 if (bp
->type
!= KEYBIND
)
8307 /* If there is a catch-all, only bind that. */
8308 if ((binding_lookup(ANYMOD
, KEYBIND
, bp
->value
)) &&
8312 /* Skip unused ws binds. */
8313 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8314 bp
->action
<= FN_WS_22
)
8317 /* Skip unused mvws binds. */
8318 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8319 bp
->action
<= FN_MVWS_22
)
8322 if ((code
= xcb_key_symbols_get_keycode(syms
,
8323 bp
->value
)) == NULL
)
8326 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8327 /* All modifiers are grabbed in one pass. */
8328 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8329 "key: %u, modifiers: %d\n", bp
->value
,
8331 xcb_grab_key(conn
, 1, screens
[k
].root
,
8332 bp
->mod
, *code
, XCB_GRAB_MODE_ASYNC
,
8333 XCB_GRAB_MODE_SYNC
);
8335 /* Need to grab each modifier permutation. */
8336 for (j
= 0; j
< LENGTH(modifiers
); j
++) {
8337 DNPRINTF(SWM_D_MOUSE
, "grabkeys: grab, "
8338 "key: %u, modifiers: %d\n",
8339 bp
->value
, bp
->mod
| modifiers
[j
]);
8340 xcb_grab_key(conn
, 1,
8342 bp
->mod
| modifiers
[j
],
8343 *code
, XCB_GRAB_MODE_ASYNC
,
8344 XCB_GRAB_MODE_SYNC
);
8356 int num_screens
, i
, k
;
8357 uint16_t modifiers
[4];
8359 DNPRINTF(SWM_D_MOUSE
, "grabbuttons\n");
8360 updatenumlockmask();
8363 modifiers
[1] = numlockmask
;
8364 modifiers
[2] = XCB_MOD_MASK_LOCK
;
8365 modifiers
[3] = numlockmask
| XCB_MOD_MASK_LOCK
;
8367 num_screens
= get_screen_count();
8368 for (k
= 0; k
< num_screens
; k
++) {
8369 if (TAILQ_EMPTY(&screens
[k
].rl
))
8371 xcb_ungrab_button(conn
, XCB_BUTTON_INDEX_ANY
, screens
[k
].root
,
8373 RB_FOREACH(bp
, binding_tree
, &bindings
) {
8374 if (bp
->type
!= BTNBIND
)
8377 /* If there is a catch-all, only bind that. */
8378 if ((binding_lookup(ANYMOD
, BTNBIND
, bp
->value
)) &&
8382 /* Skip unused ws binds. */
8383 if ((int)bp
->action
> FN_WS_1
+ workspace_limit
- 1 &&
8384 bp
->action
<= FN_WS_22
)
8387 /* Skip unused mvws binds. */
8388 if ((int)bp
->action
> FN_MVWS_1
+ workspace_limit
- 1 &&
8389 bp
->action
<= FN_MVWS_22
)
8392 if (bp
->mod
== XCB_MOD_MASK_ANY
) {
8393 /* All modifiers are grabbed in one pass. */
8394 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: grab, "
8395 "button: %u, modifiers: %d\n", bp
->value
,
8397 xcb_grab_button(conn
, 0, screens
[k
].root
,
8398 BUTTONMASK
, XCB_GRAB_MODE_SYNC
,
8399 XCB_GRAB_MODE_ASYNC
, XCB_WINDOW_NONE
,
8400 XCB_CURSOR_NONE
, bp
->value
, bp
->mod
);
8402 /* Need to grab each modifier permutation. */
8403 for (i
= 0; i
< LENGTH(modifiers
); ++i
) {
8404 DNPRINTF(SWM_D_MOUSE
, "grabbuttons: "
8405 "grab, button: %u, modifiers: %u\n",
8406 bp
->value
, bp
->mod
| modifiers
[i
]);
8407 xcb_grab_button(conn
, 0,
8408 screens
[k
].root
, BUTTONMASK
,
8410 XCB_GRAB_MODE_ASYNC
,
8412 XCB_CURSOR_NONE
, bp
->value
,
8413 bp
->mod
| modifiers
[i
]);
8420 const char *quirkname
[] = {
8421 "NONE", /* config string for "no value" */
8429 "FOCUSONMAP_SINGLE",
8437 /* SWM_Q_DELIM: retain '|' for back compat for now (2009-08-11) */
8438 #define SWM_Q_DELIM "\n|+ \t"
8440 parsequirks(const char *qstr
, uint32_t *quirk
, int *ws
)
8442 char *str
, *cp
, *name
;
8445 if (quirk
== NULL
|| qstr
== NULL
)
8448 if ((cp
= str
= strdup(qstr
)) == NULL
)
8449 err(1, "parsequirks: strdup");
8452 while ((name
= strsep(&cp
, SWM_Q_DELIM
)) != NULL
) {
8454 cp
+= (long)strspn(cp
, SWM_Q_DELIM
);
8456 if (sscanf(name
, "WS[%d]", ws
) == 1) {
8462 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
8463 if (strncasecmp(name
, quirkname
[i
],
8464 SWM_QUIRK_LEN
) == 0) {
8465 DNPRINTF(SWM_D_QUIRK
,
8466 "parsequirks: %s\n", name
);
8472 *quirk
|= 1 << (i
-1);
8476 if (i
>= LENGTH(quirkname
)) {
8477 DNPRINTF(SWM_D_QUIRK
,
8478 "parsequirks: invalid quirk [%s]\n", name
);
8489 quirk_insert(const char *class, const char *instance
, const char *name
,
8490 uint32_t quirk
, int ws
)
8494 bool failed
= false;
8496 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: class: %s, instance: %s, name: %s,"
8497 " value: %u, ws: %d\n", class, instance
, name
, quirk
, ws
);
8499 if ((qp
= malloc(sizeof *qp
)) == NULL
)
8500 err(1, "quirk_insert: malloc");
8502 if ((qp
->class = strdup(class)) == NULL
)
8503 err(1, "quirk_insert: strdup");
8504 if ((qp
->instance
= strdup(instance
)) == NULL
)
8505 err(1, "quirk_insert: strdup");
8506 if ((qp
->name
= strdup(name
)) == NULL
)
8507 err(1, "quirk_insert: strdup");
8509 if (asprintf(&str
, "^%s$", class) == -1)
8510 err(1, "quirk_insert: asprintf");
8511 if (regcomp(&qp
->regex_class
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8512 add_startup_exception("regex failed to compile quirk 'class' "
8513 "field: %s", class);
8516 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8519 if (asprintf(&str
, "^%s$", instance
) == -1)
8520 err(1, "quirk_insert: asprintf");
8521 if (regcomp(&qp
->regex_instance
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8522 add_startup_exception("regex failed to compile quirk 'instance'"
8523 " field: %s", instance
);
8526 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8529 if (asprintf(&str
, "^%s$", name
) == -1)
8530 err(1, "quirk_insert: asprintf");
8531 if (regcomp(&qp
->regex_name
, str
, REG_EXTENDED
| REG_NOSUB
)) {
8532 add_startup_exception("regex failed to compile quirk 'name' "
8536 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: compiled: %s\n", str
);
8540 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: regex error; skipping.\n");
8545 TAILQ_INSERT_TAIL(&quirks
, qp
, entry
);
8547 DNPRINTF(SWM_D_QUIRK
, "quirk_insert: leave\n");
8551 quirk_remove(struct quirk
*qp
)
8553 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: %s:%s [%u]\n", qp
->class,
8554 qp
->name
, qp
->quirk
);
8556 TAILQ_REMOVE(&quirks
, qp
, entry
);
8559 DNPRINTF(SWM_D_QUIRK
, "quirk_remove: leave\n");
8563 quirk_free(struct quirk
*qp
)
8565 regfree(&qp
->regex_class
);
8566 regfree(&qp
->regex_instance
);
8567 regfree(&qp
->regex_name
);
8579 while ((qp
= TAILQ_FIRST(&quirks
)) != NULL
) {
8585 quirk_replace(struct quirk
*qp
, const char *class, const char *instance
,
8586 const char *name
, uint32_t quirk
, int ws
)
8588 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: %s:%s:%s [%u], ws: %d\n", qp
->class,
8589 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
8592 quirk_insert(class, instance
, name
, quirk
, ws
);
8594 DNPRINTF(SWM_D_QUIRK
, "quirk_replace: leave\n");
8598 setquirk(const char *class, const char *instance
, const char *name
,
8599 uint32_t quirk
, int ws
)
8603 DNPRINTF(SWM_D_QUIRK
, "setquirk: enter %s:%s:%s [%u], ws: %d\n", class,
8604 instance
, name
, quirk
, ws
);
8606 /* Remove/replace existing quirk. */
8607 TAILQ_FOREACH(qp
, &quirks
, entry
) {
8608 if (strcmp(qp
->class, class) == 0 &&
8609 strcmp(qp
->instance
, instance
) == 0 &&
8610 strcmp(qp
->name
, name
) == 0) {
8611 if (quirk
== 0 && ws
== -1)
8614 quirk_replace(qp
, class, instance
, name
, quirk
,
8616 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8621 /* Only insert if quirk is not NONE or forced ws is set. */
8622 if (quirk
|| ws
!= -1)
8623 quirk_insert(class, instance
, name
, quirk
, ws
);
8625 DNPRINTF(SWM_D_QUIRK
, "setquirk: leave\n");
8628 /* Eat '\' in str used to escape square brackets and colon. */
8630 unescape_selector(char *str
)
8634 for (cp
= str
; *str
!= '\0'; ++str
, ++cp
) {
8635 if (*str
== '\\' && (*(str
+ 1) == ':' || *(str
+ 1) == ']' ||
8645 setconfquirk(const char *selector
, const char *value
, int flags
)
8647 char *str
, *cp
, *class;
8648 char *instance
= NULL
, *name
= NULL
;
8649 int retval
, count
= 0, ws
= -1;
8652 /* suppress unused warning since var is needed */
8655 if (selector
== NULL
|| strlen(selector
) == 0)
8658 if ((str
= strdup(selector
)) == NULL
)
8659 err(1, "setconfquirk: strdup");
8661 /* Find non-escaped colon. */
8668 for (++cp
; *cp
!= '\0'; ++cp
) {
8669 if (*cp
== ':' && *(cp
- 1) != '\\') {
8675 unescape_selector(class);
8677 instance
= class + strlen(class) + 1;
8678 unescape_selector(instance
);
8684 name
= instance
+ strlen(instance
) + 1;
8685 unescape_selector(name
);
8690 DNPRINTF(SWM_D_CONF
, "setconfquirk: class: %s, instance: %s, "
8691 "name: %s\n", class, instance
, name
);
8693 if ((retval
= parsequirks(value
, &qrks
, &ws
)) == 0)
8694 setquirk(class, instance
, name
, qrks
, ws
);
8703 setquirk("MPlayer", "xv", ".*",
8704 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
| SWM_Q_FOCUSPREV
, -1);
8705 setquirk("OpenOffice.org 3.2", "VCLSalFrame", ".*",
8707 setquirk("Firefox-bin", "firefox-bin", ".*",
8709 setquirk("Firefox", "Dialog", ".*",
8711 setquirk("Gimp", "gimp", ".*",
8712 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8713 setquirk("XTerm", "xterm", ".*",
8714 SWM_Q_XTERM_FONTADJ
, -1);
8715 setquirk("xine", "Xine Window", ".*",
8716 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8717 setquirk("Xitk", "Xitk Combo", ".*",
8718 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8719 setquirk("xine", "xine Panel", ".*",
8720 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8721 setquirk("Xitk", "Xine Window", ".*",
8722 SWM_Q_FLOAT
| SWM_Q_ANYWHERE
, -1);
8723 setquirk("xine", "xine Video Fullscreen Window", ".*",
8724 SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
, -1);
8725 setquirk("pcb", "pcb", ".*",
8727 setquirk("SDL_App", "SDL_App", ".*",
8728 SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
, -1);
8731 /* conf file stuff */
8732 #define SWM_CONF_FILE "spectrwm.conf"
8733 #define SWM_CONF_FILE_OLD "scrotwm.conf"
8737 SWM_S_BAR_AT_BOTTOM
,
8738 SWM_S_BAR_BORDER_WIDTH
,
8741 SWM_S_BAR_ENABLED_WS
,
8746 SWM_S_BOUNDARY_WIDTH
,
8747 SWM_S_CLOCK_ENABLED
,
8750 SWM_S_CYCLE_VISIBLE
,
8752 SWM_S_DISABLE_BORDER
,
8754 SWM_S_FOCUS_CLOSE_WRAP
,
8755 SWM_S_FOCUS_DEFAULT
,
8757 SWM_S_ICONIC_ENABLED
,
8758 SWM_S_JAVA_WORKAROUND
,
8759 SWM_S_MAXIMIZE_HIDE_BAR
,
8760 SWM_S_REGION_PADDING
,
8765 SWM_S_STACK_ENABLED
,
8768 SWM_S_URGENT_COLLAPSE
,
8769 SWM_S_URGENT_ENABLED
,
8770 SWM_S_VERBOSE_LAYOUT
,
8773 SWM_S_WINDOW_CLASS_ENABLED
,
8774 SWM_S_WINDOW_INSTANCE_ENABLED
,
8775 SWM_S_WINDOW_NAME_ENABLED
,
8776 SWM_S_WORKSPACE_CLAMP
,
8777 SWM_S_WORKSPACE_LIMIT
,
8778 SWM_S_WORKSPACE_NAME
,
8782 setconfvalue(const char *selector
, const char *value
, int flags
)
8784 struct workspace
*ws
;
8785 int i
, ws_id
, num_screens
;
8786 char *b
, *str
, *sp
, s
[1024];
8789 case SWM_S_BAR_ACTION
:
8791 if ((bar_argv
[0] = expand_tilde(value
)) == NULL
)
8792 err(1, "setconfvalue: bar_action");
8794 case SWM_S_BAR_AT_BOTTOM
:
8795 bar_at_bottom
= (atoi(value
) != 0);
8797 case SWM_S_BAR_BORDER_WIDTH
:
8798 bar_border_width
= atoi(value
);
8799 if (bar_border_width
< 0)
8800 bar_border_width
= 0;
8802 case SWM_S_BAR_DELAY
:
8803 /* No longer needed; leave to not break old conf files. */
8805 case SWM_S_BAR_ENABLED
:
8806 bar_enabled
= (atoi(value
) != 0);
8808 case SWM_S_BAR_ENABLED_WS
:
8809 ws_id
= atoi(selector
) - 1;
8810 if (ws_id
< 0 || ws_id
>= workspace_limit
)
8811 errx(1, "setconfvalue: bar_enabled_ws: invalid "
8812 "workspace %d.", ws_id
+ 1);
8814 num_screens
= get_screen_count();
8815 for (i
= 0; i
< num_screens
; i
++) {
8816 ws
= (struct workspace
*)&screens
[i
].ws
;
8817 ws
[ws_id
].bar_enabled
= (atoi(value
) != 0);
8820 case SWM_S_BAR_FONT
:
8822 if (asprintf(&bar_fonts
, "%s,%s", value
, bar_fonts
) == -1)
8823 err(1, "setconfvalue: asprintf: failed to allocate "
8824 "memory for bar_fonts.");
8827 /* If already in xft mode, then we are done. */
8828 if (!bar_font_legacy
)
8831 if ((sp
= str
= strdup(value
)) == NULL
)
8832 err(1, "setconfvalue: strdup");
8834 /* If there are any non-XLFD entries, switch to Xft mode. */
8835 while ((b
= strsep(&sp
, ",")) != NULL
) {
8839 bar_font_legacy
= false;
8846 case SWM_S_BAR_FORMAT
:
8848 if ((bar_format
= strdup(value
)) == NULL
)
8849 err(1, "setconfvalue: bar_format");
8851 case SWM_S_BAR_JUSTIFY
:
8852 if (strcmp(value
, "left") == 0)
8853 bar_justify
= SWM_BAR_JUSTIFY_LEFT
;
8854 else if (strcmp(value
, "center") == 0)
8855 bar_justify
= SWM_BAR_JUSTIFY_CENTER
;
8856 else if (strcmp(value
, "right") == 0)
8857 bar_justify
= SWM_BAR_JUSTIFY_RIGHT
;
8859 errx(1, "invalid bar_justify");
8861 case SWM_S_BORDER_WIDTH
:
8862 border_width
= atoi(value
);
8863 if (border_width
< 0)
8866 case SWM_S_BOUNDARY_WIDTH
:
8867 boundary_width
= atoi(value
);
8868 if (boundary_width
< 0)
8871 case SWM_S_CLOCK_ENABLED
:
8872 clock_enabled
= (atoi(value
) != 0);
8874 case SWM_S_CLOCK_FORMAT
:
8875 #ifndef SWM_DENY_CLOCK_FORMAT
8877 if ((clock_format
= strdup(value
)) == NULL
)
8878 err(1, "setconfvalue: clock_format");
8881 case SWM_S_CYCLE_EMPTY
:
8882 cycle_empty
= (atoi(value
) != 0);
8884 case SWM_S_CYCLE_VISIBLE
:
8885 cycle_visible
= (atoi(value
) != 0);
8887 case SWM_S_DIALOG_RATIO
:
8888 dialog_ratio
= atof(value
);
8889 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
8892 case SWM_S_DISABLE_BORDER
:
8893 disable_border
= (atoi(value
) != 0);
8895 case SWM_S_FOCUS_CLOSE
:
8896 if (strcmp(value
, "first") == 0)
8897 focus_close
= SWM_STACK_BOTTOM
;
8898 else if (strcmp(value
, "last") == 0)
8899 focus_close
= SWM_STACK_TOP
;
8900 else if (strcmp(value
, "next") == 0)
8901 focus_close
= SWM_STACK_ABOVE
;
8902 else if (strcmp(value
, "previous") == 0)
8903 focus_close
= SWM_STACK_BELOW
;
8905 errx(1, "focus_close");
8907 case SWM_S_FOCUS_CLOSE_WRAP
:
8908 focus_close_wrap
= (atoi(value
) != 0);
8910 case SWM_S_FOCUS_DEFAULT
:
8911 if (strcmp(value
, "last") == 0)
8912 focus_default
= SWM_STACK_TOP
;
8913 else if (strcmp(value
, "first") == 0)
8914 focus_default
= SWM_STACK_BOTTOM
;
8916 errx(1, "focus_default");
8918 case SWM_S_FOCUS_MODE
:
8919 if (strcmp(value
, "default") == 0)
8920 focus_mode
= SWM_FOCUS_DEFAULT
;
8921 else if (strcmp(value
, "follow") == 0 ||
8922 strcmp(value
, "follow_cursor") == 0)
8923 focus_mode
= SWM_FOCUS_FOLLOW
;
8924 else if (strcmp(value
, "manual") == 0)
8925 focus_mode
= SWM_FOCUS_MANUAL
;
8927 errx(1, "focus_mode");
8929 case SWM_S_ICONIC_ENABLED
:
8930 iconic_enabled
= (atoi(value
) != 0);
8932 case SWM_S_JAVA_WORKAROUND
:
8933 java_workaround
= (atoi(value
) != 0);
8935 case SWM_S_MAXIMIZE_HIDE_BAR
:
8936 maximize_hide_bar
= atoi(value
);
8938 case SWM_S_REGION_PADDING
:
8939 region_padding
= atoi(value
);
8940 if (region_padding
< 0)
8943 case SWM_S_SPAWN_ORDER
:
8944 if (strcmp(value
, "first") == 0)
8945 spawn_position
= SWM_STACK_BOTTOM
;
8946 else if (strcmp(value
, "last") == 0)
8947 spawn_position
= SWM_STACK_TOP
;
8948 else if (strcmp(value
, "next") == 0)
8949 spawn_position
= SWM_STACK_ABOVE
;
8950 else if (strcmp(value
, "previous") == 0)
8951 spawn_position
= SWM_STACK_BELOW
;
8953 errx(1, "spawn_position");
8955 case SWM_S_SPAWN_TERM
:
8956 setconfspawn("term", value
, 0);
8957 setconfspawn("spawn_term", value
, 0);
8960 /* No longer needed; leave to not break old conf files. */
8962 case SWM_S_SS_ENABLED
:
8963 /* No longer needed; leave to not break old conf files. */
8965 case SWM_S_STACK_ENABLED
:
8966 stack_enabled
= (atoi(value
) != 0);
8968 case SWM_S_TERM_WIDTH
:
8969 term_width
= atoi(value
);
8973 case SWM_S_TILE_GAP
:
8974 tile_gap
= atoi(value
);
8976 case SWM_S_URGENT_COLLAPSE
:
8977 urgent_collapse
= (atoi(value
) != 0);
8979 case SWM_S_URGENT_ENABLED
:
8980 urgent_enabled
= (atoi(value
) != 0);
8982 case SWM_S_VERBOSE_LAYOUT
:
8983 verbose_layout
= (atoi(value
) != 0);
8984 for (i
= 0; layouts
[i
].l_stack
!= NULL
; i
++) {
8986 layouts
[i
].l_string
= fancy_stacker
;
8988 layouts
[i
].l_string
= plain_stacker
;
8991 case SWM_S_WARP_FOCUS
:
8992 warp_focus
= (atoi(value
) != 0);
8994 case SWM_S_WARP_POINTER
:
8995 warp_pointer
= (atoi(value
) != 0);
8997 case SWM_S_WINDOW_CLASS_ENABLED
:
8998 window_class_enabled
= (atoi(value
) != 0);
9000 case SWM_S_WINDOW_INSTANCE_ENABLED
:
9001 window_instance_enabled
= (atoi(value
) != 0);
9003 case SWM_S_WINDOW_NAME_ENABLED
:
9004 window_name_enabled
= (atoi(value
) != 0);
9006 case SWM_S_WORKSPACE_CLAMP
:
9007 workspace_clamp
= (atoi(value
) != 0);
9009 case SWM_S_WORKSPACE_LIMIT
:
9010 workspace_limit
= atoi(value
);
9011 if (workspace_limit
> SWM_WS_MAX
)
9012 workspace_limit
= SWM_WS_MAX
;
9013 else if (workspace_limit
< 1)
9014 workspace_limit
= 1;
9016 ewmh_update_desktops();
9018 case SWM_S_WORKSPACE_NAME
:
9019 if (getenv("SWM_STARTED") != NULL
)
9023 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9024 errx(1, "invalid entry, should be 'ws[<idx>]:name'");
9026 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9027 errx(1, "setconfvalue: workspace_name: invalid "
9028 "workspace %d.", ws_id
+ 1);
9030 num_screens
= get_screen_count();
9031 for (i
= 0; i
< num_screens
; ++i
) {
9032 ws
= (struct workspace
*)&screens
[i
].ws
;
9034 if (strlen(s
) > 0) {
9035 free(ws
[ws_id
].name
);
9036 if ((ws
[ws_id
].name
= strdup(s
)) == NULL
)
9037 err(1, "setconfvalue: workspace_name.");
9039 ewmh_update_desktop_names();
9040 ewmh_get_desktop_names();
9051 setconfmodkey(const char *selector
, const char *value
, int flags
)
9053 /* suppress unused warnings since vars are needed */
9057 if (strncasecmp(value
, "Mod1", strlen("Mod1")) == 0)
9058 update_modkey(XCB_MOD_MASK_1
);
9059 else if (strncasecmp(value
, "Mod2", strlen("Mod2")) == 0)
9060 update_modkey(XCB_MOD_MASK_2
);
9061 else if (strncasecmp(value
, "Mod3", strlen("Mod3")) == 0)
9062 update_modkey(XCB_MOD_MASK_3
);
9063 else if (strncasecmp(value
, "Mod4", strlen("Mod4")) == 0)
9064 update_modkey(XCB_MOD_MASK_4
);
9065 else if (strncasecmp(value
, "Mod5", strlen("Mod5")) == 0)
9066 update_modkey(XCB_MOD_MASK_5
);
9073 setconfcolor(const char *selector
, const char *value
, int flags
)
9075 int first
, last
, i
= 0, num_screens
;
9077 num_screens
= get_screen_count();
9079 /* conf screen indices begin at 1; treat vals <= 0 as 'all screens.' */
9080 if (selector
== NULL
|| strlen(selector
) == 0 ||
9081 (last
= atoi(selector
) - 1) < 0) {
9083 last
= num_screens
- 1;
9088 if (last
>= num_screens
) {
9089 add_startup_exception("invalid screen index: %d out of bounds "
9090 "(maximum %d)", last
+ 1, num_screens
);
9094 for (i
= first
; i
<= last
; ++i
) {
9095 setscreencolor(value
, i
, flags
);
9098 * When setting focus/unfocus colors, we need to also
9099 * set maximize colors to match if they haven't been customized.
9101 if (flags
== SWM_S_COLOR_FOCUS
&&
9102 !screens
[i
].c
[SWM_S_COLOR_FOCUS_MAXIMIZED
].manual
)
9103 setscreencolor(value
, i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
9104 else if (flags
== SWM_S_COLOR_UNFOCUS
&&
9105 !screens
[i
].c
[SWM_S_COLOR_UNFOCUS_MAXIMIZED
].manual
)
9106 setscreencolor(value
, i
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
9108 screens
[i
].c
[flags
].manual
= 1;
9115 setconfregion(const char *selector
, const char *value
, int flags
)
9117 /* suppress unused warnings since vars are needed */
9121 custom_region(value
);
9126 setautorun(const char *selector
, const char *value
, int flags
)
9130 char *ap
, *sp
, *str
;
9136 /* suppress unused warnings since vars are needed */
9140 if (getenv("SWM_STARTED"))
9144 if (sscanf(value
, "ws[%d]:%1023c", &ws_id
, s
) != 2)
9145 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
9147 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9148 errx(1, "autorun: invalid workspace %d", ws_id
+ 1);
9150 sp
= str
= expand_tilde((char *)&s
);
9153 * This is a little intricate
9155 * If the pid already exists we simply reuse it because it means it was
9156 * used before AND not claimed by manage_window. We get away with
9157 * altering it in the parent after INSERT because this can not be a race
9160 while ((ap
= strsep(&sp
, " \t")) != NULL
) {
9163 DNPRINTF(SWM_D_SPAWN
, "setautorun: arg [%s]\n", ap
);
9165 if ((a
.argv
= realloc(a
.argv
, argc
* sizeof(char *))) == NULL
)
9166 err(1, "setautorun: realloc");
9167 a
.argv
[argc
- 1] = ap
;
9170 if ((a
.argv
= realloc(a
.argv
, (argc
+ 1) * sizeof(char *))) == NULL
)
9171 err(1, "setautorun: realloc");
9172 a
.argv
[argc
] = NULL
;
9174 if ((pid
= fork()) == 0) {
9175 spawn(ws_id
, &a
, true);
9185 p
= calloc(1, sizeof *p
);
9188 TAILQ_INSERT_TAIL(&pidlist
, p
, entry
);
9198 setlayout(const char *selector
, const char *value
, int flags
)
9200 struct workspace
*ws
;
9201 int ws_id
, i
, x
, mg
, ma
, si
, ar
;
9202 int st
= SWM_V_STACK
, num_screens
;
9206 /* suppress unused warnings since vars are needed */
9210 if (getenv("SWM_STARTED"))
9214 if (sscanf(value
, "ws[%d]:%d:%d:%d:%d:%1023c",
9215 &ws_id
, &mg
, &ma
, &si
, &ar
, s
) != 6)
9216 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9217 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9220 if (ws_id
< 0 || ws_id
>= workspace_limit
)
9221 errx(1, "layout: invalid workspace %d", ws_id
+ 1);
9223 if (strcasecmp(s
, "vertical") == 0)
9225 else if (strcasecmp(s
, "vertical_flip") == 0) {
9228 } else if (strcasecmp(s
, "horizontal") == 0)
9230 else if (strcasecmp(s
, "horizontal_flip") == 0) {
9233 } else if (strcasecmp(s
, "fullscreen") == 0)
9236 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
9237 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
9240 num_screens
= get_screen_count();
9241 for (i
= 0; i
< num_screens
; i
++) {
9242 ws
= (struct workspace
*)&screens
[i
].ws
;
9243 ws
[ws_id
].cur_layout
= &layouts
[st
];
9245 ws
[ws_id
].always_raise
= (ar
!= 0);
9246 if (st
== SWM_MAX_STACK
)
9250 for (x
= 0; x
< abs(mg
); x
++) {
9251 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9252 mg
>= 0 ? SWM_ARG_ID_MASTERGROW
:
9253 SWM_ARG_ID_MASTERSHRINK
);
9256 for (x
= 0; x
< abs(ma
); x
++) {
9257 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9258 ma
>= 0 ? SWM_ARG_ID_MASTERADD
:
9259 SWM_ARG_ID_MASTERDEL
);
9262 for (x
= 0; x
< abs(si
); x
++) {
9263 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9264 si
>= 0 ? SWM_ARG_ID_STACKINC
:
9265 SWM_ARG_ID_STACKDEC
);
9269 ws
[ws_id
].cur_layout
->l_config(&ws
[ws_id
],
9270 SWM_ARG_ID_FLIPLAYOUT
);
9277 /* config options */
9278 struct config_option
{
9280 int (*func
)(const char*, const char*, int);
9283 struct config_option configopt
[] = {
9284 { "autorun", setautorun
, 0 },
9285 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
9286 { "bar_at_bottom", setconfvalue
, SWM_S_BAR_AT_BOTTOM
},
9287 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
9288 { "bar_border_unfocus", setconfcolor
, SWM_S_COLOR_BAR_BORDER_UNFOCUS
},
9289 { "bar_border_width", setconfvalue
, SWM_S_BAR_BORDER_WIDTH
},
9290 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
9291 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
9292 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
9293 { "bar_enabled_ws", setconfvalue
, SWM_S_BAR_ENABLED_WS
},
9294 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
9295 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
9296 { "bar_format", setconfvalue
, SWM_S_BAR_FORMAT
},
9297 { "bar_justify", setconfvalue
, SWM_S_BAR_JUSTIFY
},
9298 { "bind", setconfbinding
, 0 },
9299 { "border_width", setconfvalue
, SWM_S_BORDER_WIDTH
},
9300 { "boundary_width", setconfvalue
, SWM_S_BOUNDARY_WIDTH
},
9301 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
9302 { "clock_format", setconfvalue
, SWM_S_CLOCK_FORMAT
},
9303 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
9304 { "color_focus_maximized", setconfcolor
, SWM_S_COLOR_FOCUS_MAXIMIZED
},
9305 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
9306 { "color_unfocus_maximized", setconfcolor
, SWM_S_COLOR_UNFOCUS_MAXIMIZED
},
9307 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
9308 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
9309 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
9310 { "disable_border", setconfvalue
, SWM_S_DISABLE_BORDER
},
9311 { "focus_close", setconfvalue
, SWM_S_FOCUS_CLOSE
},
9312 { "focus_close_wrap", setconfvalue
, SWM_S_FOCUS_CLOSE_WRAP
},
9313 { "focus_default", setconfvalue
, SWM_S_FOCUS_DEFAULT
},
9314 { "focus_mode", setconfvalue
, SWM_S_FOCUS_MODE
},
9315 { "iconic_enabled", setconfvalue
, SWM_S_ICONIC_ENABLED
},
9316 { "java_workaround", setconfvalue
, SWM_S_JAVA_WORKAROUND
},
9317 { "keyboard_mapping", setkeymapping
, 0 },
9318 { "layout", setlayout
, 0 },
9319 { "maximize_hide_bar", setconfvalue
, SWM_S_MAXIMIZE_HIDE_BAR
},
9320 { "modkey", setconfmodkey
, 0 },
9321 { "program", setconfspawn
, 0 },
9322 { "quirk", setconfquirk
, 0 },
9323 { "region", setconfregion
, 0 },
9324 { "region_padding", setconfvalue
, SWM_S_REGION_PADDING
},
9325 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
9326 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
9327 { "spawn_position", setconfvalue
, SWM_S_SPAWN_ORDER
},
9328 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
9329 { "stack_enabled", setconfvalue
, SWM_S_STACK_ENABLED
},
9330 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
9331 { "tile_gap", setconfvalue
, SWM_S_TILE_GAP
},
9332 { "title_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
}, /* For backwards compat. */
9333 { "title_name_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
}, /* For backwards compat. */
9334 { "urgent_collapse", setconfvalue
, SWM_S_URGENT_COLLAPSE
},
9335 { "urgent_enabled", setconfvalue
, SWM_S_URGENT_ENABLED
},
9336 { "verbose_layout", setconfvalue
, SWM_S_VERBOSE_LAYOUT
},
9337 { "warp_focus", setconfvalue
, SWM_S_WARP_FOCUS
},
9338 { "warp_pointer", setconfvalue
, SWM_S_WARP_POINTER
},
9339 { "window_class_enabled", setconfvalue
, SWM_S_WINDOW_CLASS_ENABLED
},
9340 { "window_instance_enabled", setconfvalue
, SWM_S_WINDOW_INSTANCE_ENABLED
},
9341 { "window_name_enabled", setconfvalue
, SWM_S_WINDOW_NAME_ENABLED
},
9342 { "workspace_clamp", setconfvalue
, SWM_S_WORKSPACE_CLAMP
},
9343 { "workspace_limit", setconfvalue
, SWM_S_WORKSPACE_LIMIT
},
9344 { "name", setconfvalue
, SWM_S_WORKSPACE_NAME
},
9348 _add_startup_exception(const char *fmt
, va_list ap
)
9350 if (vasprintf(&startup_exception
, fmt
, ap
) == -1)
9351 warn("%s: asprintf", __func__
);
9355 add_startup_exception(const char *fmt
, ...)
9361 if (startup_exception
)
9364 /* force bar to be enabled due to exception */
9368 _add_startup_exception(fmt
, ap
);
9373 conf_load(const char *filename
, int keymapping
)
9376 char *line
= NULL
, *cp
, *ce
, *optsub
, *optval
= NULL
;
9377 size_t linelen
, lineno
= 0;
9378 int wordlen
, i
, optidx
, count
;
9379 struct config_option
*opt
= NULL
;
9381 DNPRINTF(SWM_D_CONF
, "conf_load: begin\n");
9383 if (filename
== NULL
) {
9384 warnx("conf_load: no filename");
9388 DNPRINTF(SWM_D_CONF
, "conf_load: open %s\n", filename
);
9390 if ((config
= fopen(filename
, "r")) == NULL
) {
9391 warn("conf_load: fopen: %s", filename
);
9395 while (!feof(config
)) {
9399 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
,
9400 FPARSELN_UNESCCOMM
| FPARSELN_UNESCCONT
)) == NULL
) {
9402 err(1, "%s", filename
);
9407 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9408 if (cp
[0] == '\0') {
9412 /* get config option */
9413 wordlen
= strcspn(cp
, "=[ \t\n");
9415 add_startup_exception("%s: line %zd: no option found",
9420 for (i
= 0; i
< LENGTH(configopt
); i
++) {
9421 opt
= &configopt
[i
];
9422 if (strncasecmp(cp
, opt
->optname
, wordlen
) == 0 &&
9423 (int)strlen(opt
->optname
) == wordlen
) {
9429 add_startup_exception("%s: line %zd: unknown option "
9430 "%.*s", filename
, lineno
, wordlen
, cp
);
9433 if (keymapping
&& opt
&& strcmp(opt
->optname
, "bind")) {
9434 add_startup_exception("%s: line %zd: invalid option "
9435 "%.*s", filename
, lineno
, wordlen
, cp
);
9439 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
9441 /* get [selector] if any */
9446 /* Get length of selector. */
9447 for (ce
= ++cp
; *ce
!= '\0'; ++ce
) {
9448 /* Find matching (not escaped) bracket. */
9449 if (*ce
== ']' && *(ce
- 1) != '\\') {
9456 add_startup_exception("%s: line %zd: syntax "
9457 "error: unterminated selector", filename
,
9462 /* ce points at ']'; terminate optsub. */
9467 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
9470 if (strlen(optval
) == 0) {
9471 add_startup_exception("%s: line %zd: must supply value "
9472 "to %s", filename
, lineno
,
9473 configopt
[optidx
].optname
);
9476 /* call function to deal with it all */
9477 if (configopt
[optidx
].func(optsub
, optval
,
9478 configopt
[optidx
].funcflags
) != 0) {
9479 add_startup_exception("%s: line %zd: invalid data for "
9480 "%s", filename
, lineno
, configopt
[optidx
].optname
);
9489 DNPRINTF(SWM_D_CONF
, "conf_load: end\n");
9495 set_child_transient(struct ws_win
*win
, xcb_window_t
*trans
)
9497 struct ws_win
*parent
, *w
;
9498 struct swm_region
*r
;
9499 struct workspace
*ws
;
9500 xcb_icccm_wm_hints_t wmh
;
9502 parent
= find_window(win
->transient
);
9504 parent
->focus_child
= win
;
9506 DNPRINTF(SWM_D_MISC
, "set_child_transient: parent doesn't exist"
9507 " for %#x trans %#x\n", win
->id
, win
->transient
);
9509 r
= root_to_region(win
->s
->root
, SWM_CK_ALL
);
9511 /* parent doen't exist in our window list */
9512 TAILQ_FOREACH(w
, &ws
->winlist
, entry
) {
9513 if (xcb_icccm_get_wm_hints_reply(conn
,
9514 xcb_icccm_get_wm_hints(conn
, w
->id
),
9516 warnx("can't get hints for %#x", w
->id
);
9520 if (win
->hints
.window_group
!= wmh
.window_group
)
9523 w
->focus_child
= win
;
9524 win
->transient
= w
->id
;
9526 DNPRINTF(SWM_D_MISC
, "set_child_transient: adjusting "
9527 "transient to %#x\n", win
->transient
);
9534 window_get_pid(xcb_window_t win
)
9539 xcb_get_property_cookie_t pc
;
9540 xcb_get_property_reply_t
*pr
;
9542 apid
= get_atom_from_string("_NET_WM_PID");
9543 if (apid
== XCB_ATOM_NONE
)
9546 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_CARDINAL
, 0, 1);
9547 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9550 if (pr
->type
!= XCB_ATOM_CARDINAL
) {
9555 if (pr
->type
== apid
&& pr
->format
== 32)
9556 ret
= *((pid_t
*)xcb_get_property_value(pr
));
9562 apid
= get_atom_from_string("_SWM_PID");
9563 pc
= xcb_get_property(conn
, 0, win
, apid
, XCB_ATOM_STRING
,
9565 pr
= xcb_get_property_reply(conn
, pc
, NULL
);
9568 if (pr
->type
!= apid
) {
9573 ret
= (pid_t
)strtonum(xcb_get_property_value(pr
), 0, INT_MAX
, &errstr
);
9580 get_swm_ws(xcb_window_t id
)
9586 xcb_get_property_reply_t
*gpr
;
9588 gpr
= xcb_get_property_reply(conn
,
9589 xcb_get_property(conn
, 0, id
, a_swm_ws
,
9590 XCB_ATOM_STRING
, 0, SWM_PROPLEN
),
9595 proplen
= xcb_get_property_value_length(gpr
);
9597 prop
= malloc(proplen
+ 1);
9600 xcb_get_property_value(gpr
),
9602 prop
[proplen
] = '\0';
9609 DNPRINTF(SWM_D_PROP
, "get_swm_ws: _SWM_WS: %s\n", prop
);
9610 ws_idx
= (int)strtonum(prop
, 0, workspace_limit
- 1, &errstr
);
9612 DNPRINTF(SWM_D_PROP
, "get_swm_ws: win #%s: %s",
9622 get_ws_idx(struct ws_win
*win
)
9624 xcb_get_property_reply_t
*gpr
;
9630 gpr
= xcb_get_property_reply(conn
,
9631 xcb_get_property(conn
, 0, win
->id
, ewmh
[_NET_WM_DESKTOP
].atom
,
9632 XCB_ATOM_CARDINAL
, 0, 1),
9635 if (gpr
->type
== XCB_ATOM_CARDINAL
&& gpr
->format
== 32)
9636 ws_idx
= *((int *)xcb_get_property_value(gpr
));
9640 if (ws_idx
== -1 && !(win
->quirks
& SWM_Q_IGNORESPAWNWS
))
9641 ws_idx
= get_swm_ws(win
->id
);
9643 if (ws_idx
> workspace_limit
- 1 || ws_idx
< -1)
9646 DNPRINTF(SWM_D_PROP
, "get_ws_idx: win %#x, ws_idx: %d\n", win
->id
,
9653 reparent_window(struct ws_win
*win
)
9655 xcb_void_cookie_t c
;
9656 xcb_generic_error_t
*error
;
9659 win
->frame
= xcb_generate_id(conn
);
9661 DNPRINTF(SWM_D_MISC
, "reparent_window: win %#x, frame: %#x\n",
9662 win
->id
, win
->frame
);
9665 XCB_EVENT_MASK_ENTER_WINDOW
|
9666 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
9667 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
9668 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
|
9669 XCB_EVENT_MASK_EXPOSURE
;
9671 wa
[0] |= XCB_EVENT_MASK_LEAVE_WINDOW
| XCB_EVENT_MASK_FOCUS_CHANGE
;
9674 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, win
->frame
, win
->s
->root
,
9675 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), 0,
9676 XCB_WINDOW_CLASS_INPUT_OUTPUT
, XCB_COPY_FROM_PARENT
,
9677 XCB_CW_EVENT_MASK
, wa
);
9679 win
->state
= SWM_WIN_STATE_REPARENTING
;
9680 c
= xcb_reparent_window_checked(conn
, win
->id
, win
->frame
, 0, 0);
9681 if ((error
= xcb_request_check(conn
, c
))) {
9682 DNPRINTF(SWM_D_MISC
, "reparent_window: error:\n");
9687 xcb_destroy_window(conn
, win
->frame
);
9688 win
->frame
= XCB_WINDOW_NONE
;
9690 xcb_change_save_set(conn
, XCB_SET_MODE_INSERT
, win
->id
);
9692 DNPRINTF(SWM_D_MISC
, "reparent_window: done.\n");
9696 unparent_window(struct ws_win
*win
)
9698 xcb_change_save_set(conn
, XCB_SET_MODE_DELETE
, win
->id
);
9699 xcb_reparent_window(conn
, win
->id
, win
->s
->root
, X(win
), Y(win
));
9700 xcb_destroy_window(conn
, win
->frame
);
9701 win
->frame
= XCB_WINDOW_NONE
;
9702 win
->state
= SWM_WIN_STATE_UNPARENTING
;
9706 manage_window(xcb_window_t id
, int spawn_pos
, bool mapping
)
9708 struct ws_win
*win
= NULL
, *ww
;
9709 struct swm_region
*r
;
9712 xcb_get_geometry_reply_t
*gr
;
9713 xcb_get_window_attributes_reply_t
*war
= NULL
;
9714 xcb_window_t trans
= XCB_WINDOW_NONE
;
9715 uint32_t i
, wa
[1], new_flags
;
9716 int ws_idx
, force_ws
= -1;
9717 char *class, *instance
, *name
;
9720 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region bar; "
9725 if (find_region(id
)) {
9726 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is region window; "
9731 if ((win
= find_window(id
)) != NULL
) {
9732 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x already "
9733 "managed; skipping.)\n", id
);
9734 return (win
); /* Already managed. */
9737 /* See if window is on the unmanaged list. */
9738 if ((win
= find_unmanaged_window(id
)) != NULL
) {
9739 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x found on "
9740 "unmanaged list.\n", id
);
9741 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
9744 set_child_transient(win
, &trans
);
9748 DNPRINTF(SWM_D_MISC
, "manage_window: win %#x is new.\n", id
);
9751 war
= xcb_get_window_attributes_reply(conn
,
9752 xcb_get_window_attributes(conn
, id
), NULL
);
9754 DNPRINTF(SWM_D_EVENT
, "manage_window: window lost.\n");
9758 if (war
->override_redirect
) {
9759 DNPRINTF(SWM_D_EVENT
, "manage_window: override_redirect; "
9764 if (!mapping
&& war
->map_state
== XCB_MAP_STATE_UNMAPPED
&&
9765 get_win_state(id
) == XCB_ICCCM_WM_STATE_WITHDRAWN
) {
9766 DNPRINTF(SWM_D_EVENT
, "manage_window: window withdrawn; "
9771 /* Try to get initial window geometry. */
9772 gr
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, id
), NULL
);
9774 DNPRINTF(SWM_D_MISC
, "manage_window: get geometry failed.\n");
9778 /* Create and initialize ws_win object. */
9779 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
9780 err(1, "manage_window: calloc: failed to allocate memory for "
9785 /* Figureout which region the window belongs to. */
9786 r
= root_to_region(gr
->root
, SWM_CK_ALL
);
9788 /* Ignore window border if there is one. */
9789 WIDTH(win
) = gr
->width
;
9790 HEIGHT(win
) = gr
->height
;
9791 X(win
) = gr
->x
+ gr
->border_width
;
9792 Y(win
) = gr
->y
+ gr
->border_width
;
9793 win
->bordered
= false;
9794 win
->mapped
= (war
->map_state
!= XCB_MAP_STATE_UNMAPPED
);
9795 win
->s
= r
->s
; /* this never changes */
9799 /* Select which X events to monitor and set border pixel color. */
9800 wa
[0] = XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_PROPERTY_CHANGE
|
9801 XCB_EVENT_MASK_STRUCTURE_NOTIFY
;
9803 xcb_change_window_attributes(conn
, win
->id
, XCB_CW_EVENT_MASK
, wa
);
9805 /* Get WM_SIZE_HINTS. */
9806 xcb_icccm_get_wm_normal_hints_reply(conn
,
9807 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
9811 xcb_icccm_get_wm_hints_reply(conn
,
9812 xcb_icccm_get_wm_hints(conn
, win
->id
),
9815 /* Get WM_TRANSIENT_FOR; see if window is a transient. */
9816 xcb_icccm_get_wm_transient_for_reply(conn
,
9817 xcb_icccm_get_wm_transient_for(conn
, win
->id
),
9820 win
->transient
= trans
;
9821 set_child_transient(win
, &win
->transient
);
9824 /* Get WM_PROTOCOLS. */
9825 get_wm_protocols(win
);
9828 /* Must be after getting WM_HINTS and WM_PROTOCOLS. */
9829 DNPRINTF(SWM_D_FOCUS
, "manage_window: input_model: %s\n",
9830 get_win_input_model(win
));
9833 /* Set initial quirks based on EWMH. */
9834 ewmh_autoquirk(win
);
9836 /* Determine initial quirks. */
9837 xcb_icccm_get_wm_class_reply(conn
,
9838 xcb_icccm_get_wm_class(conn
, win
->id
),
9841 class = win
->ch
.class_name
? win
->ch
.class_name
: "";
9842 instance
= win
->ch
.instance_name
? win
->ch
.instance_name
: "";
9843 name
= get_win_name(win
->id
);
9845 DNPRINTF(SWM_D_CLASS
, "manage_window: class: %s, instance: %s, "
9846 "name: %s\n", class, instance
, name
);
9848 /* java is retarded so treat it special */
9849 if (strstr(instance
, "sun-awt")) {
9850 DNPRINTF(SWM_D_CLASS
, "manage_window: java window detected.\n");
9854 TAILQ_FOREACH(qp
, &quirks
, entry
) {
9855 if (regexec(&qp
->regex_class
, class, 0, NULL
, 0) == 0 &&
9856 regexec(&qp
->regex_instance
, instance
, 0, NULL
, 0) == 0 &&
9857 regexec(&qp
->regex_name
, name
, 0, NULL
, 0) == 0) {
9858 DNPRINTF(SWM_D_CLASS
, "manage_window: matched "
9859 "quirk: %s:%s:%s mask: %#x, ws: %d\n", qp
->class,
9860 qp
->instance
, qp
->name
, qp
->quirk
, qp
->ws
);
9861 win
->quirks
= qp
->quirk
;
9862 if (qp
->ws
>= 0 && qp
->ws
< workspace_limit
)
9869 /* Reset font sizes (the bruteforce way; no default keybinding). */
9870 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
9871 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9872 fake_keypress(win
, XK_KP_Subtract
, XCB_MOD_MASK_SHIFT
);
9873 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
9874 fake_keypress(win
, XK_KP_Add
, XCB_MOD_MASK_SHIFT
);
9877 /* Figure out which workspace the window belongs to. */
9878 if (!(win
->quirks
& SWM_Q_IGNOREPID
) &&
9879 (p
= find_pid(window_get_pid(win
->id
))) != NULL
) {
9880 win
->ws
= &r
->s
->ws
[p
->ws
];
9881 TAILQ_REMOVE(&pidlist
, p
, entry
);
9884 } else if ((ws_idx
= get_ws_idx(win
)) != -1 &&
9886 /* _SWM_WS is set; use that. */
9887 win
->ws
= &r
->s
->ws
[ws_idx
];
9888 } else if (trans
&& (ww
= find_window(trans
)) != NULL
) {
9889 /* Launch transients in the same ws as parent. */
9896 win
->ws
= &r
->s
->ws
[force_ws
];
9898 /* Set the _NET_WM_DESKTOP atom. */
9899 DNPRINTF(SWM_D_PROP
, "manage_window: set _NET_WM_DESKTOP: %d\n",
9901 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, win
->id
,
9902 ewmh
[_NET_WM_DESKTOP
].atom
, XCB_ATOM_CARDINAL
, 32, 1, &win
->ws
->idx
);
9904 /* Remove any _SWM_WS now that we set _NET_WM_DESKTOP. */
9905 xcb_delete_property(conn
, win
->id
, a_swm_ws
);
9907 /* WS must already be set for this to work. */
9908 store_float_geom(win
);
9910 /* Make sure window is positioned inside its region, if its active. */
9912 region_containment(win
, r
, SWM_CW_ALLSIDES
|
9913 SWM_CW_HARDBOUNDARY
);
9918 /* Figure out where to insert the window in the workspace list. */
9919 if (trans
&& (ww
= find_window(trans
)))
9920 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, ww
, win
, entry
);
9921 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_ABOVE
)
9922 TAILQ_INSERT_AFTER(&win
->ws
->winlist
, win
->ws
->focus
, win
,
9924 else if (win
->ws
->focus
&& spawn_pos
== SWM_STACK_BELOW
)
9925 TAILQ_INSERT_BEFORE(win
->ws
->focus
, win
, entry
);
9926 else switch (spawn_pos
) {
9929 case SWM_STACK_ABOVE
:
9930 TAILQ_INSERT_TAIL(&win
->ws
->winlist
, win
, entry
);
9932 case SWM_STACK_BOTTOM
:
9933 case SWM_STACK_BELOW
:
9934 TAILQ_INSERT_HEAD(&win
->ws
->winlist
, win
, entry
);
9937 ewmh_update_client_list();
9939 TAILQ_INSERT_HEAD(&win
->ws
->stack
, win
, stack_entry
);
9942 /* Get/apply initial _NET_WM_STATE */
9943 ewmh_get_wm_state(win
);
9946 new_flags
= win
->ewmh_flags
;
9948 if (win
->quirks
& SWM_Q_FLOAT
)
9949 new_flags
|= EWMH_F_ABOVE
;
9951 if (win
->quirks
& SWM_Q_ANYWHERE
)
9952 new_flags
|= SWM_F_MANUAL
;
9954 ewmh_apply_flags(win
, new_flags
);
9955 ewmh_update_wm_state(win
);
9957 /* Set initial _NET_WM_ALLOWED_ACTIONS */
9958 ewmh_update_actions(win
);
9960 reparent_window(win
);
9962 DNPRINTF(SWM_D_MISC
, "manage_window: done. win %#x, (x,y) w x h: "
9963 "(%d,%d) %d x %d, ws: %d, iconic: %s, transient: %#x\n", win
->id
,
9964 X(win
), Y(win
), WIDTH(win
), HEIGHT(win
), win
->ws
->idx
,
9965 YESNO(ICONIC(win
)), win
->transient
);
9972 free_window(struct ws_win
*win
)
9974 DNPRINTF(SWM_D_MISC
, "free_window: win %#x\n", WINID(win
));
9979 xcb_icccm_get_wm_class_reply_wipe(&win
->ch
);
9982 memset(win
, 0xff, sizeof *win
); /* XXX kill later */
9985 DNPRINTF(SWM_D_MISC
, "free_window: done.\n");
9989 unmanage_window(struct ws_win
*win
)
9991 DNPRINTF(SWM_D_MISC
, "unmanage_window: win %#x\n", WINID(win
));
9997 unparent_window(win
);
9999 TAILQ_REMOVE(&win
->ws
->stack
, win
, stack_entry
);
10000 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
10001 TAILQ_INSERT_TAIL(&win
->ws
->unmanagedlist
, win
, entry
);
10003 ewmh_update_client_list();
10007 expose(xcb_expose_event_t
*e
)
10012 struct workspace
*ws
;
10015 DNPRINTF(SWM_D_EVENT
, "expose: win %#x, count: %d\n", e
->window
,
10021 if ((b
= find_bar(e
->window
))) {
10024 } else if ((w
= find_window(e
->window
)) && w
->frame
== e
->window
) {
10028 TAILQ_FOREACH(w
, &ws
->winlist
, entry
)
10034 DNPRINTF(SWM_D_EVENT
, "expose: done\n");
10038 focusin(xcb_focus_in_event_t
*e
)
10040 struct ws_win
*win
;
10042 DNPRINTF(SWM_D_EVENT
, "focusin: win %#x, mode: %s(%u), "
10043 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10044 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10045 if ((win
= find_window(e
->event
)) && win
!= win
->ws
->focus
&&
10046 win
!= win
->ws
->focus_pending
&&
10047 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10048 win
->ws
->focus_prev
= win
->ws
->focus
;
10049 win
->ws
->focus
= win
;
10050 win
->ws
->focus_pending
= NULL
;
10052 if (win
->ws
->focus_prev
)
10053 draw_frame(win
->ws
->focus_prev
);
10061 focusout(xcb_focus_out_event_t
*e
)
10063 DNPRINTF(SWM_D_EVENT
, "focusout: win %#x, mode: %s(%u), "
10064 "detail: %s(%u)\n", e
->event
, get_notify_mode_label(e
->mode
),
10065 e
->mode
, get_notify_detail_label(e
->detail
), e
->detail
);
10070 keypress(xcb_key_press_event_t
*e
)
10073 struct binding
*bp
;
10074 xcb_keysym_t keysym
;
10075 bool replay
= true;
10077 last_event_time
= e
->time
;
10079 keysym
= xcb_key_press_lookup_keysym(syms
, e
, 0);
10081 DNPRINTF(SWM_D_EVENT
, "keypress: keysym: %u, win (x,y): %#x (%d,%d), "
10082 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10083 "state: %u, cleaned: %u, same_screen: %s\n", keysym
, e
->event
,
10084 e
->event_x
, e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
,
10085 e
->root_y
, e
->child
, e
->state
, CLEANMASK(e
->state
),
10086 YESNO(e
->same_screen
));
10088 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10090 /* Look for catch-all. */
10091 if ((bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
)) == NULL
)
10095 replay
= bp
->flags
& BINDING_F_REPLAY
;
10097 if ((ap
= &actions
[bp
->action
]) == NULL
)
10100 if (bp
->action
== FN_SPAWN_CUSTOM
)
10101 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10104 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10106 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10110 DNPRINTF(SWM_D_EVENT
, "keypress: replaying.\n");
10111 /* Pass keypress to event window and unfreeze keyboard queue. */
10112 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10114 /* Release freeze. */
10115 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10119 DNPRINTF(SWM_D_EVENT
, "keypress: done.\n");
10123 keyrelease(xcb_key_release_event_t
*e
)
10126 struct binding
*bp
;
10127 xcb_keysym_t keysym
;
10129 last_event_time
= e
->time
;
10131 keysym
= xcb_key_release_lookup_keysym(syms
, e
, 0);
10133 DNPRINTF(SWM_D_EVENT
, "keyrelease: keysym: %u, win (x,y): %#x (%d,%d), "
10134 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10135 "state: %u, same_screen: %s\n", keysym
, e
->event
, e
->event_x
,
10136 e
->event_y
, e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
,
10137 e
->child
, e
->state
, YESNO(e
->same_screen
));
10139 bp
= binding_lookup(CLEANMASK(e
->state
), KEYBIND
, keysym
);
10141 /* Look for catch-all. */
10142 bp
= binding_lookup(ANYMOD
, KEYBIND
, keysym
);
10144 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10145 bp
->flags
& BINDING_F_REPLAY
) {
10146 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_KEYBOARD
, e
->time
);
10147 DNPRINTF(SWM_D_EVENT
, "keyrelease: replaying.\n");
10149 xcb_allow_events(conn
, XCB_ALLOW_SYNC_KEYBOARD
, e
->time
);
10150 DNPRINTF(SWM_D_EVENT
, "keyrelease: unfreezing.\n");
10155 DNPRINTF(SWM_D_EVENT
, "keyrelease: done.\n");
10159 buttonpress(xcb_button_press_event_t
*e
)
10161 struct ws_win
*win
= NULL
, *newf
;
10162 struct swm_region
*r
, *old_r
;
10164 struct binding
*bp
;
10165 bool replay
= true;
10167 last_event_time
= e
->time
;
10169 DNPRINTF(SWM_D_EVENT
, "buttonpress: win (x,y): %#x (%d,%d), "
10170 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10171 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10172 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10173 e
->state
, YESNO(e
->same_screen
));
10175 if (e
->event
== e
->root
) {
10177 win
= find_window(e
->child
);
10179 /* Focus on empty region */
10180 /* If no windows on region if its empty. */
10181 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10182 if (r
&& TAILQ_EMPTY(&r
->ws
->winlist
)) {
10183 old_r
= root_to_region(e
->root
, SWM_CK_FOCUS
);
10184 if (old_r
&& old_r
!= r
)
10185 unfocus_win(old_r
->ws
->focus
);
10187 DNPRINTF(SWM_D_FOCUS
, "buttonpress: "
10188 "set_input_focus: %#x, revert-to: parent, "
10189 "time: %#x\n", e
->root
, e
->time
);
10190 xcb_set_input_focus(conn
,
10191 XCB_INPUT_FOCUS_POINTER_ROOT
,
10194 /* Clear bar since empty. */
10197 /* No need to replay event. */
10202 win
= find_window(e
->event
);
10206 newf
= get_focus_magic(win
);
10207 if (win
->ws
->focus
== newf
&& newf
!= win
) {
10208 if (win
->focus_child
== win
)
10209 win
->focus_child
= NULL
;
10215 /* Handle any bound action. */
10216 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10218 /* Look for catch-all. */
10219 if ((bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
)) == NULL
)
10224 replay
= bp
->flags
& BINDING_F_REPLAY
;
10226 if ((ap
= &actions
[bp
->action
]) == NULL
)
10229 if (bp
->action
== FN_SPAWN_CUSTOM
)
10230 spawn_custom(root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
,
10233 ap
->func(bp
, root_to_region(e
->root
, SWM_CK_ALL
), &ap
->args
);
10235 replay
= replay
&& !(ap
->flags
& FN_F_NOREPLAY
);
10239 DNPRINTF(SWM_D_EVENT
, "buttonpress: replaying.\n");
10240 /* Replay event to event window */
10241 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10243 /* Unfreeze grab events. */
10244 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10251 buttonrelease(xcb_button_release_event_t
*e
)
10254 struct binding
*bp
;
10256 last_event_time
= e
->time
;
10258 DNPRINTF(SWM_D_EVENT
, "buttonrelease: win (x,y): %#x (%d,%d), "
10259 "detail: %u, time: %u, root (x,y): %#x (%d,%d), child: %#x, "
10260 "state: %u, same_screen: %s\n", e
->event
, e
->event_x
, e
->event_y
,
10261 e
->detail
, e
->time
, e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10262 e
->state
, YESNO(e
->same_screen
));
10264 bp
= binding_lookup(CLEANMASK(e
->state
), BTNBIND
, e
->detail
);
10266 /* Look for catch-all. */
10267 bp
= binding_lookup(ANYMOD
, BTNBIND
, e
->detail
);
10269 if (bp
&& (ap
= &actions
[bp
->action
]) && !(ap
->flags
& FN_F_NOREPLAY
) &&
10270 bp
->flags
& BINDING_F_REPLAY
) {
10271 DNPRINTF(SWM_D_EVENT
, "buttonrelease: replaying.\n");
10272 xcb_allow_events(conn
, XCB_ALLOW_REPLAY_POINTER
, e
->time
);
10274 xcb_allow_events(conn
, XCB_ALLOW_SYNC_POINTER
, e
->time
);
10282 get_win_input_model(struct ws_win
*win
)
10286 * Input Model Input Field WM_TAKE_FOCUS
10287 * No Input False Absent
10288 * Passive True Absent
10289 * Locally Active True Present
10290 * Globally Active False Present
10293 if (ACCEPTS_FOCUS(win
))
10294 inputmodel
= (win
->take_focus
) ? "Locally Active" : "Passive";
10296 inputmodel
= (win
->take_focus
) ? "Globally Active" : "No Input";
10302 print_win_geom(xcb_window_t w
)
10304 xcb_get_geometry_reply_t
*wa
;
10306 wa
= xcb_get_geometry_reply(conn
, xcb_get_geometry(conn
, w
), NULL
);
10308 DNPRINTF(SWM_D_MISC
, "print_win_geom: window not found: %#x\n",
10313 DNPRINTF(SWM_D_MISC
, "print_win_geom: win %#x, root: %#x, "
10314 "depth: %u, (x,y) w x h: (%d,%d) %d x %d, border: %d\n",
10315 w
, wa
->root
, wa
->depth
, wa
->x
, wa
->y
, wa
->width
, wa
->height
,
10324 get_stack_mode_name(uint8_t mode
)
10329 case XCB_STACK_MODE_ABOVE
:
10332 case XCB_STACK_MODE_BELOW
:
10335 case XCB_STACK_MODE_TOP_IF
:
10338 case XCB_STACK_MODE_BOTTOM_IF
:
10341 case XCB_STACK_MODE_OPPOSITE
:
10353 configurerequest(xcb_configure_request_event_t
*e
)
10355 struct ws_win
*win
;
10356 struct swm_region
*r
= NULL
;
10358 uint32_t wc
[7] = {0};
10362 if ((win
= find_window(e
->window
)) == NULL
)
10363 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10367 if (swm_debug
& SWM_D_EVENT
) {
10368 print_win_geom(e
->window
);
10370 DNPRINTF(SWM_D_EVENT
, "configurerequest: win %#x, "
10371 "parent: %#x, new: %s, value_mask: %u { ", e
->window
,
10372 e
->parent
, YESNO(new), e
->value_mask
);
10373 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
)
10374 DPRINTF("X: %d ", e
->x
);
10375 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
)
10376 DPRINTF("Y: %d ", e
->y
);
10377 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10378 DPRINTF("W: %u ", e
->width
);
10379 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10380 DPRINTF("H: %u ", e
->height
);
10381 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
)
10382 DPRINTF("Border: %u ", e
->border_width
);
10383 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
)
10384 DPRINTF("Sibling: %#x ", e
->sibling
);
10385 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
)
10386 DPRINTF("StackMode: %s(%u) ",
10387 get_stack_mode_name(e
->stack_mode
), e
->stack_mode
);
10393 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10394 mask
|= XCB_CONFIG_WINDOW_X
;
10397 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10398 mask
|= XCB_CONFIG_WINDOW_Y
;
10401 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
) {
10402 mask
|= XCB_CONFIG_WINDOW_WIDTH
;
10403 wc
[i
++] = e
->width
;
10405 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
) {
10406 mask
|= XCB_CONFIG_WINDOW_HEIGHT
;
10407 wc
[i
++] = e
->height
;
10409 if (e
->value_mask
& XCB_CONFIG_WINDOW_BORDER_WIDTH
) {
10410 mask
|= XCB_CONFIG_WINDOW_BORDER_WIDTH
;
10411 wc
[i
++] = e
->border_width
;
10413 if (e
->value_mask
& XCB_CONFIG_WINDOW_SIBLING
) {
10414 mask
|= XCB_CONFIG_WINDOW_SIBLING
;
10415 wc
[i
++] = e
->sibling
;
10417 if (e
->value_mask
& XCB_CONFIG_WINDOW_STACK_MODE
) {
10418 mask
|= XCB_CONFIG_WINDOW_STACK_MODE
;
10419 wc
[i
++] = e
->stack_mode
;
10423 xcb_configure_window(conn
, e
->window
, mask
, wc
);
10426 } else if ((!MANUAL(win
) || win
->quirks
& SWM_Q_ANYWHERE
) &&
10427 !FULLSCREEN(win
) && !MAXIMIZED(win
)) {
10430 else if (win
->ws
->old_r
)
10431 r
= win
->ws
->old_r
;
10433 /* windows are centered unless ANYWHERE quirk is set. */
10434 if (win
->quirks
& SWM_Q_ANYWHERE
) {
10435 if (e
->value_mask
& XCB_CONFIG_WINDOW_X
) {
10436 win
->g_float
.x
= e
->x
;
10438 win
->g_float
.x
-= X(r
);
10441 if (e
->value_mask
& XCB_CONFIG_WINDOW_Y
) {
10442 win
->g_float
.y
= e
->y
;
10444 win
->g_float
.y
-= Y(r
);
10448 if (e
->value_mask
& XCB_CONFIG_WINDOW_WIDTH
)
10449 win
->g_float
.w
= e
->width
;
10451 if (e
->value_mask
& XCB_CONFIG_WINDOW_HEIGHT
)
10452 win
->g_float
.h
= e
->height
;
10454 win
->g_floatvalid
= true;
10456 if (!MAXIMIZED(win
) && !FULLSCREEN(win
) &&
10457 (TRANS(win
) || (ABOVE(win
) &&
10458 win
->ws
->cur_layout
!= &layouts
[SWM_MAX_STACK
]))) {
10459 WIDTH(win
) = win
->g_float
.w
;
10460 HEIGHT(win
) = win
->g_float
.h
;
10463 update_floater(win
);
10466 config_win(win
, e
);
10470 config_win(win
, e
);
10474 config_win(win
, e
);
10478 DNPRINTF(SWM_D_EVENT
, "configurerequest: done.\n");
10482 configurenotify(xcb_configure_notify_event_t
*e
)
10484 struct ws_win
*win
;
10486 DNPRINTF(SWM_D_EVENT
, "configurenotify: win %#x, event win: %#x, "
10487 "(x,y) WxH: (%d,%d) %ux%u, border: %u, above_sibling: %#x, "
10488 "override_redirect: %s\n", e
->window
, e
->event
, e
->x
, e
->y
,
10489 e
->width
, e
->height
, e
->border_width
, e
->above_sibling
,
10490 YESNO(e
->override_redirect
));
10492 win
= find_window(e
->window
);
10495 if (font_adjusted
&& win
->ws
->r
) {
10503 destroynotify(xcb_destroy_notify_event_t
*e
)
10505 struct ws_win
*win
;
10506 struct workspace
*ws
;
10508 DNPRINTF(SWM_D_EVENT
, "destroynotify: win %#x\n", e
->window
);
10510 if ((win
= find_window(e
->window
)) == NULL
) {
10511 if ((win
= find_unmanaged_window(e
->window
)) == NULL
)
10513 /* Window is on unmanaged list. */
10514 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10521 if (win
->frame
== e
->window
) {
10522 DNPRINTF(SWM_D_EVENT
, "destroynotify: frame for win %#x\n",
10524 win
->frame
= XCB_WINDOW_NONE
;
10528 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
10529 /* If we were focused, make sure we focus on something else. */
10530 if (win
== ws
->focus
) {
10531 ws
->focus_pending
= get_focus_prev(win
);
10532 if (ws
->focus_pending
== win
)
10533 ws
->focus_pending
= NULL
;
10537 unmanage_window(win
);
10538 TAILQ_REMOVE(&win
->ws
->unmanagedlist
, win
, entry
);
10542 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
10543 if (ws
->focus_pending
) {
10544 focus_win(ws
->focus_pending
);
10545 ws
->focus_pending
= NULL
;
10546 } else if (ws
->focus
== NULL
) {
10547 DNPRINTF(SWM_D_FOCUS
, "destroynotify: set_input_focus: "
10548 "%#x, revert-to: parent, time: 0\n", ws
->r
->id
);
10549 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
10550 ws
->r
->id
, XCB_CURRENT_TIME
);
10557 DNPRINTF(SWM_D_EVENT
, "destroynotify: done.\n");
10562 get_notify_detail_label(uint8_t detail
)
10567 case XCB_NOTIFY_DETAIL_ANCESTOR
:
10568 label
= "Ancestor";
10570 case XCB_NOTIFY_DETAIL_VIRTUAL
:
10573 case XCB_NOTIFY_DETAIL_INFERIOR
:
10574 label
= "Inferior";
10576 case XCB_NOTIFY_DETAIL_NONLINEAR
:
10577 label
= "Nonlinear";
10579 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL
:
10580 label
= "NonlinearVirtual";
10582 case XCB_NOTIFY_DETAIL_POINTER
:
10585 case XCB_NOTIFY_DETAIL_POINTER_ROOT
:
10586 label
= "PointerRoot";
10588 case XCB_NOTIFY_DETAIL_NONE
:
10599 get_notify_mode_label(uint8_t mode
)
10604 case XCB_NOTIFY_MODE_NORMAL
:
10607 case XCB_NOTIFY_MODE_GRAB
:
10610 case XCB_NOTIFY_MODE_UNGRAB
:
10613 case XCB_NOTIFY_MODE_WHILE_GRABBED
:
10614 label
= "WhileGrabbed";
10624 get_state_mask_label(uint16_t state
)
10629 case XCB_KEY_BUT_MASK_SHIFT
:
10630 label
= "ShiftMask";
10632 case XCB_KEY_BUT_MASK_LOCK
:
10633 label
= "LockMask";
10635 case XCB_KEY_BUT_MASK_CONTROL
:
10636 label
= "ControlMask";
10638 case XCB_KEY_BUT_MASK_MOD_1
:
10639 label
= "Mod1Mask";
10641 case XCB_KEY_BUT_MASK_MOD_2
:
10642 label
= "Mod2Mask";
10644 case XCB_KEY_BUT_MASK_MOD_3
:
10645 label
= "Mod3Mask";
10647 case XCB_KEY_BUT_MASK_MOD_4
:
10648 label
= "Mod4Mask";
10650 case XCB_KEY_BUT_MASK_MOD_5
:
10651 label
= "Mod5Mask";
10653 case XCB_KEY_BUT_MASK_BUTTON_1
:
10654 label
= "Button1Mask";
10656 case XCB_KEY_BUT_MASK_BUTTON_2
:
10657 label
= "Button2Mask";
10659 case XCB_KEY_BUT_MASK_BUTTON_3
:
10660 label
= "Button3Mask";
10662 case XCB_KEY_BUT_MASK_BUTTON_4
:
10663 label
= "Button4Mask";
10665 case XCB_KEY_BUT_MASK_BUTTON_5
:
10666 label
= "Button5Mask";
10680 enternotify(xcb_enter_notify_event_t
*e
)
10682 struct ws_win
*win
;
10683 struct swm_region
*r
;
10685 last_event_time
= e
->time
;
10687 DNPRINTF(SWM_D_FOCUS
, "enternotify: time: %u, win (x,y): %#x "
10688 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10689 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10690 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10691 get_notify_mode_label(e
->mode
), e
->mode
,
10692 get_notify_detail_label(e
->detail
), e
->detail
,
10693 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10694 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10697 if (e
->event
== e
->root
&& e
->child
== XCB_WINDOW_NONE
&&
10698 e
->mode
== XCB_NOTIFY_MODE_GRAB
&&
10699 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10700 DNPRINTF(SWM_D_EVENT
, "enternotify: grab inferior; ignoring.\n");
10704 if (focus_mode
== SWM_FOCUS_MANUAL
&&
10705 e
->mode
== XCB_NOTIFY_MODE_NORMAL
) {
10706 DNPRINTF(SWM_D_EVENT
, "enternotify: manual focus; ignoring.\n");
10710 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10711 e
->mode
== XCB_NOTIFY_MODE_UNGRAB
&&
10712 e
->detail
!= XCB_NOTIFY_DETAIL_ANCESTOR
) {
10713 DNPRINTF(SWM_D_EVENT
, "enternotify: ungrab; ignoring.\n");
10717 if ((win
= find_window(e
->event
)) == NULL
) {
10718 if (e
->event
== e
->root
) {
10719 /* If no windows on pointer region, then focus root. */
10720 r
= root_to_region(e
->root
, SWM_CK_POINTER
);
10722 DNPRINTF(SWM_D_EVENT
, "enternotify: "
10723 "NULL region; ignoring.\n");
10729 DNPRINTF(SWM_D_EVENT
, "enternotify: window is NULL; "
10734 if (e
->mode
== XCB_NOTIFY_MODE_NORMAL
&&
10735 e
->detail
== XCB_NOTIFY_DETAIL_INFERIOR
) {
10736 DNPRINTF(SWM_D_EVENT
, "enternotify: entering from "
10737 "inferior; ignoring\n");
10741 focus_win(get_focus_magic(win
));
10744 DNPRINTF(SWM_D_EVENT
, "enternotify: done\n");
10751 leavenotify(xcb_leave_notify_event_t
*e
)
10753 last_event_time
= e
->time
;
10755 DNPRINTF(SWM_D_FOCUS
, "leavenotify: time: %u, win (x,y): %#x "
10756 "(%d,%d), mode: %s(%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10757 "child: %#x, same_screen_focus: %s, state: %s(%d)\n",
10758 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10759 get_notify_mode_label(e
->mode
), e
->mode
,
10760 get_notify_detail_label(e
->detail
), e
->detail
,
10761 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10762 YESNO(e
->same_screen_focus
), get_state_mask_label(e
->state
),
10768 mapnotify(xcb_map_notify_event_t
*e
)
10770 struct ws_win
*win
, *parent
= NULL
;
10771 struct workspace
*ws
;
10773 DNPRINTF(SWM_D_EVENT
, "mapnotify: win %#x\n", e
->window
);
10775 if ((win
= manage_window(e
->window
, spawn_position
, false)) == NULL
)
10779 if (win
->state
== SWM_WIN_STATE_REPARENTING
)
10782 if (ws
->r
== NULL
) {
10787 /* Need to know if win was mapped due to ws switch. */
10788 if (ws
->state
== SWM_WS_STATE_MAPPED
) {
10789 if (ws
->focus_pending
&& TRANS(ws
->focus_pending
))
10790 parent
= find_window(win
->transient
);
10792 /* If window's parent is maximized, don't clear it. */
10793 if ((parent
== NULL
) || !MAXIMIZED(parent
))
10794 if (clear_maximized(ws
) > 0)
10798 win
->mapped
= true;
10799 set_win_state(win
, XCB_ICCCM_WM_STATE_NORMAL
);
10801 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(win
->ws
)) {
10802 if (ws
->focus_pending
== win
) {
10804 ws
->focus_pending
= NULL
;
10805 center_pointer(ws
->r
);
10811 DNPRINTF(SWM_D_EVENT
, "mapnotify: done\n");
10817 mappingnotify(xcb_mapping_notify_event_t
*e
)
10819 if (e
->request
!= XCB_MAPPING_POINTER
) {
10820 xcb_refresh_keyboard_mapping(syms
, e
);
10828 maprequest(xcb_map_request_event_t
*e
)
10830 struct ws_win
*win
, *w
= NULL
;
10832 DNPRINTF(SWM_D_EVENT
, "maprequest: win %#x\n",
10835 win
= manage_window(e
->window
, spawn_position
, true);
10839 /* The new window should get focus; prepare. */
10840 if (focus_mode
!= SWM_FOCUS_FOLLOW
&&
10841 !(win
->quirks
& SWM_Q_NOFOCUSONMAP
) && ACCEPTS_FOCUS(win
)) {
10842 if (win
->quirks
& SWM_Q_FOCUSONMAP_SINGLE
) {
10843 /* See if other wins of same type are already mapped. */
10844 TAILQ_FOREACH(w
, &win
->ws
->winlist
, entry
) {
10845 if (w
== win
|| !w
->mapped
)
10848 if (w
->ch
.class_name
&&
10849 win
->ch
.class_name
&&
10850 strcmp(w
->ch
.class_name
,
10851 win
->ch
.class_name
) == 0 &&
10852 w
->ch
.instance_name
&&
10853 win
->ch
.instance_name
&&
10854 strcmp(w
->ch
.instance_name
,
10855 win
->ch
.instance_name
) == 0)
10861 win
->ws
->focus_pending
= get_focus_magic(win
);
10864 /* All windows need to be mapped if they are in the current workspace.*/
10867 /* Ignore EnterNotify to handle the mapnotify without interference. */
10868 if (focus_mode
== SWM_FOCUS_DEFAULT
)
10869 event_drain(XCB_ENTER_NOTIFY
);
10871 DNPRINTF(SWM_D_EVENT
, "maprequest: done.\n");
10875 motionnotify(xcb_motion_notify_event_t
*e
)
10877 struct swm_region
*r
= NULL
;
10878 int i
, num_screens
;
10880 last_event_time
= e
->time
;
10882 DNPRINTF(SWM_D_FOCUS
, "motionnotify: time: %u, win (x,y): %#x "
10883 "(%d,%d), detail: %s(%d), root (x,y): %#x (%d,%d), "
10884 "child: %#x, same_screen_focus: %s, state: %d\n",
10885 e
->time
, e
->event
, e
->event_x
, e
->event_y
,
10886 get_notify_detail_label(e
->detail
), e
->detail
,
10887 e
->root
, e
->root_x
, e
->root_y
, e
->child
,
10888 YESNO(e
->same_screen
), e
->state
);
10890 if (focus_mode
== SWM_FOCUS_MANUAL
)
10893 num_screens
= get_screen_count();
10894 for (i
= 0; i
< num_screens
; i
++)
10895 if (screens
[i
].root
== e
->root
)
10898 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
10899 if (X(r
) <= e
->root_x
&& e
->root_x
< MAX_X(r
) &&
10900 Y(r
) <= e
->root_y
&& e
->root_y
< MAX_Y(r
))
10908 get_atom_name(xcb_atom_t atom
)
10911 #ifdef SWM_DEBUG_ATOM_NAMES
10913 * This should be disabled during most debugging since
10914 * xcb_get_* causes an xcb_flush.
10917 xcb_get_atom_name_reply_t
*r
;
10919 r
= xcb_get_atom_name_reply(conn
,
10920 xcb_get_atom_name(conn
, atom
),
10923 len
= xcb_get_atom_name_name_length(r
);
10925 name
= malloc(len
+ 1);
10927 memcpy(name
, xcb_get_atom_name_name(r
), len
);
10941 propertynotify(xcb_property_notify_event_t
*e
)
10943 struct ws_win
*win
;
10944 struct workspace
*ws
;
10948 name
= get_atom_name(e
->atom
);
10949 DNPRINTF(SWM_D_EVENT
, "propertynotify: win %#x, atom: %s(%u), "
10950 "time: %#x, state: %u\n", e
->window
, name
, e
->atom
, e
->time
,
10954 last_event_time
= e
->time
;
10956 win
= find_window(e
->window
);
10962 if (e
->atom
== a_state
) {
10963 /* State just changed, make sure it gets focused if mapped. */
10964 if (e
->state
== XCB_PROPERTY_NEW_VALUE
) {
10965 if (focus_mode
!= SWM_FOCUS_FOLLOW
&& WS_FOCUSED(ws
)) {
10967 win
->state
== SWM_WIN_STATE_REPARENTED
&&
10968 ws
->focus_pending
== win
) {
10969 focus_win(ws
->focus_pending
);
10970 ws
->focus_pending
= NULL
;
10974 } else if (e
->atom
== XCB_ATOM_WM_CLASS
||
10975 e
->atom
== XCB_ATOM_WM_NAME
) {
10977 bar_draw(ws
->r
->bar
);
10978 } else if (e
->atom
== a_prot
) {
10979 get_wm_protocols(win
);
10980 } else if (e
->atom
== XCB_ATOM_WM_NORMAL_HINTS
) {
10981 xcb_icccm_get_wm_normal_hints_reply(conn
,
10982 xcb_icccm_get_wm_normal_hints(conn
, win
->id
),
10990 reparentnotify(xcb_reparent_notify_event_t
*e
)
10992 struct ws_win
*win
;
10994 DNPRINTF(SWM_D_EVENT
, "reparentnotify: event: %#x, win %#x, "
10995 "parent: %#x, (x,y): (%u,%u), override_redirect: %u\n",
10996 e
->event
, e
->window
, e
->parent
, e
->x
, e
->y
, e
->override_redirect
);
10998 win
= find_window(e
->window
);
11000 if (win
->state
== SWM_WIN_STATE_REPARENTING
) {
11001 win
->state
= SWM_WIN_STATE_REPARENTED
;
11008 update_window(win
);
11009 update_win_stacking(win
);
11010 } else if (win
->state
== SWM_WIN_STATE_UNPARENTING
) {
11011 win
->state
= SWM_WIN_STATE_UNPARENTED
;
11017 unmapnotify(xcb_unmap_notify_event_t
*e
)
11019 struct ws_win
*win
;
11020 struct workspace
*ws
;
11022 DNPRINTF(SWM_D_EVENT
, "unmapnotify: win %#x\n", e
->window
);
11024 /* If we aren't managing the window, then ignore. */
11025 win
= find_window(e
->window
);
11026 if (win
== NULL
|| win
->id
!= e
->window
) {
11027 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore unmanaged.\n");
11031 /* Do nothing if already withdrawn. */
11032 if (!win
->mapped
&& !ICONIC(win
)) {
11033 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore withdrawn.\n");
11038 win
->mapped
= false;
11040 /* Ignore if reparenting-related. */
11041 if (win
->state
!= SWM_WIN_STATE_REPARENTED
) {
11042 DNPRINTF(SWM_D_EVENT
, "unmapnotify: ignore not reparented.\n");
11046 /* If win was focused, make sure to focus on something else. */
11047 if (win
== ws
->focus
) {
11048 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
11049 ws
->focus_pending
= get_focus_prev(win
);
11050 DNPRINTF(SWM_D_EVENT
, "unmapnotify: "
11051 "focus_pending: %#x\n",
11052 WINID(ws
->focus_pending
));
11060 DNPRINTF(SWM_D_EVENT
, "unmapnotify: iconify.\n");
11061 set_win_state(win
, XCB_ICCCM_WM_STATE_ICONIC
);
11064 DNPRINTF(SWM_D_EVENT
, "unmapnotify: withdraw.\n");
11065 set_win_state(win
, XCB_ICCCM_WM_STATE_WITHDRAWN
);
11066 unmanage_window(win
);
11071 /* Update focus if ws is active. */
11072 if (WS_FOCUSED(ws
)) {
11073 if (focus_mode
== SWM_FOCUS_FOLLOW
) {
11074 focus_win(get_pointer_win(ws
->r
->s
->root
));
11075 } else if (ws
->focus_pending
) {
11076 focus_win(ws
->focus_pending
);
11077 ws
->focus_pending
= NULL
;
11078 } else if (ws
->focus
== NULL
) {
11079 DNPRINTF(SWM_D_FOCUS
, "unmapnotify: set_input_focus: "
11080 "%#x, revert-to: parent, time: 0\n",
11082 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11083 ws
->r
->id
, XCB_CURRENT_TIME
);
11087 center_pointer(ws
->r
);
11089 DNPRINTF(SWM_D_EVENT
, "unmapnotify: done.\n");
11094 get_source_type_label(uint32_t type
)
11099 case EWMH_SOURCE_TYPE_NONE
:
11102 case EWMH_SOURCE_TYPE_NORMAL
:
11105 case EWMH_SOURCE_TYPE_OTHER
:
11117 clientmessage(xcb_client_message_event_t
*e
)
11119 struct ws_win
*win
;
11120 struct swm_region
*r
= NULL
;
11123 int num_screens
, i
;
11124 xcb_map_request_event_t mre
;
11128 name
= get_atom_name(e
->type
);
11129 DNPRINTF(SWM_D_EVENT
, "clientmessage: win %#x, atom: %s(%u)\n",
11130 e
->window
, name
, e
->type
);
11134 if (e
->type
== ewmh
[_NET_CURRENT_DESKTOP
].atom
) {
11135 num_screens
= get_screen_count();
11136 for (i
= 0; i
< num_screens
; i
++)
11137 if (screens
[i
].root
== e
->window
) {
11138 r
= screens
[i
].r_focus
;
11142 if (r
&& e
->data
.data32
[0] < (uint32_t)workspace_limit
) {
11143 a
.id
= e
->data
.data32
[0];
11144 switchws(NULL
, r
, &a
);
11149 } else if (e
->type
== ewmh
[_NET_REQUEST_FRAME_EXTENTS
].atom
) {
11150 DNPRINTF(SWM_D_EVENT
,
11151 "clientmessage: set _NET_FRAME_EXTENTS on window.\n");
11152 vals
[0] = vals
[1] = vals
[2] = vals
[3] = border_width
;
11153 xcb_change_property(conn
, XCB_PROP_MODE_REPLACE
, e
->window
,
11154 a_net_frame_extents
, XCB_ATOM_CARDINAL
, 32, 4, vals
);
11159 win
= find_window(e
->window
);
11161 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11162 /* Manage the window with maprequest. */
11163 DNPRINTF(SWM_D_EVENT
, "clientmessage: request focus on "
11164 "unmanaged window.\n");
11165 mre
.window
= e
->window
;
11171 if (e
->type
== ewmh
[_NET_ACTIVE_WINDOW
].atom
) {
11172 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_ACTIVE_WINDOW, "
11173 "source_type: %s(%d)\n",
11174 get_source_type_label(e
->data
.data32
[0]),
11175 e
->data
.data32
[0]);
11178 * Allow focus changes that are a result of direct user
11179 * action and from applications that use the old EWMH spec.
11181 if (e
->data
.data32
[0] != EWMH_SOURCE_TYPE_NORMAL
||
11182 win
->quirks
& SWM_Q_OBEYAPPFOCUSREQ
) {
11183 if (WS_FOCUSED(win
->ws
))
11186 win
->ws
->focus_pending
= win
;
11188 } else if (e
->type
== ewmh
[_NET_CLOSE_WINDOW
].atom
) {
11189 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_CLOSE_WINDOW\n");
11190 if (win
->can_delete
)
11191 client_msg(win
, a_delete
, 0);
11193 xcb_kill_client(conn
, win
->id
);
11194 } else if (e
->type
== ewmh
[_NET_MOVERESIZE_WINDOW
].atom
) {
11195 DNPRINTF(SWM_D_EVENT
,
11196 "clientmessage: _NET_MOVERESIZE_WINDOW\n");
11198 if (e
->data
.data32
[0] & (1<<8)) /* x */
11199 X(win
) = e
->data
.data32
[1];
11200 if (e
->data
.data32
[0] & (1<<9)) /* y */
11201 Y(win
) = e
->data
.data32
[2];
11202 if (e
->data
.data32
[0] & (1<<10)) /* width */
11203 WIDTH(win
) = e
->data
.data32
[3];
11204 if (e
->data
.data32
[0] & (1<<11)) /* height */
11205 HEIGHT(win
) = e
->data
.data32
[4];
11207 update_window(win
);
11209 /* Notify no change was made. */
11210 config_win(win
, NULL
);
11211 /* TODO: Change stack sizes */
11213 } else if (e
->type
== ewmh
[_NET_RESTACK_WINDOW
].atom
) {
11214 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_RESTACK_WINDOW\n");
11215 vals
[0] = e
->data
.data32
[1]; /* Sibling window. */
11216 vals
[1] = e
->data
.data32
[2]; /* Stack mode detail. */
11218 if (win
->frame
!= XCB_WINDOW_NONE
)
11219 xcb_configure_window(conn
, win
->frame
,
11220 XCB_CONFIG_WINDOW_SIBLING
|
11221 XCB_CONFIG_WINDOW_STACK_MODE
, vals
);
11222 } else if (e
->type
== ewmh
[_NET_WM_STATE
].atom
) {
11223 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_STATE\n");
11224 ewmh_change_wm_state(win
, e
->data
.data32
[1], e
->data
.data32
[0]);
11225 if (e
->data
.data32
[2])
11226 ewmh_change_wm_state(win
, e
->data
.data32
[2],
11227 e
->data
.data32
[0]);
11229 ewmh_update_wm_state(win
);
11231 } else if (e
->type
== ewmh
[_NET_WM_DESKTOP
].atom
) {
11232 DNPRINTF(SWM_D_EVENT
, "clientmessage: _NET_WM_DESKTOP\n");
11235 win_to_ws(win
, e
->data
.data32
[0], true);
11237 /* Stack source and destination ws, if mapped. */
11238 if (r
!= win
->ws
->r
) {
11244 load_float_geom(win
);
11257 int errcode
= xcb_connection_has_error(conn
);
11258 #ifdef XCB_CONN_ERROR
11261 case XCB_CONN_ERROR
:
11262 s
= "Socket error, pipe error or other stream error.";
11264 case XCB_CONN_CLOSED_EXT_NOTSUPPORTED
:
11265 s
= "Extension not supported.";
11267 case XCB_CONN_CLOSED_MEM_INSUFFICIENT
:
11268 s
= "Insufficient memory.";
11270 case XCB_CONN_CLOSED_REQ_LEN_EXCEED
:
11271 s
= "Request length was exceeded.";
11273 case XCB_CONN_CLOSED_PARSE_ERR
:
11274 s
= "Error parsing display string.";
11277 s
= "Unknown error.";
11280 errx(errcode
, "X CONNECTION ERROR: %s", s
);
11283 errx(errcode
, "X CONNECTION ERROR");
11290 int num_screens
, i
;
11291 const uint32_t val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
|
11292 XCB_EVENT_MASK_ENTER_WINDOW
|
11293 XCB_EVENT_MASK_OWNER_GRAB_BUTTON
|
11294 XCB_EVENT_MASK_STRUCTURE_NOTIFY
|
11295 XCB_EVENT_MASK_ENTER_WINDOW
|
11296 XCB_EVENT_MASK_LEAVE_WINDOW
|
11297 XCB_EVENT_MASK_BUTTON_PRESS
|
11298 XCB_EVENT_MASK_BUTTON_RELEASE
|
11299 XCB_EVENT_MASK_KEY_PRESS
|
11300 XCB_EVENT_MASK_KEY_RELEASE
|
11301 XCB_EVENT_MASK_PROPERTY_CHANGE
;
11303 xcb_void_cookie_t wac
;
11304 xcb_generic_error_t
*error
;
11306 /* this causes an error if some other window manager is running */
11307 num_screens
= get_screen_count();
11308 for (i
= 0; i
< num_screens
; i
++) {
11309 if ((sc
= get_screen(i
)) == NULL
)
11310 errx(1, "ERROR: can't get screen %d.", i
);
11311 DNPRINTF(SWM_D_INIT
, "enable_wm: screen %d, root: %#x\n",
11313 wac
= xcb_change_window_attributes_checked(conn
, sc
->root
,
11314 XCB_CW_EVENT_MASK
, &val
);
11315 if ((error
= xcb_request_check(conn
, wac
))) {
11316 DNPRINTF(SWM_D_INIT
, "enable_wm: error_code: %u\n",
11317 error
->error_code
);
11327 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
11329 struct swm_region
*r
= NULL
, *n
;
11330 struct workspace
*ws
= NULL
;
11334 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
11335 s
->idx
, w
, h
, x
, y
);
11337 /* remove any conflicting regions */
11338 n
= TAILQ_FIRST(&s
->rl
);
11341 n
= TAILQ_NEXT(r
, entry
);
11342 if (X(r
) < (x
+ w
) && (X(r
) + WIDTH(r
)) > x
&&
11343 Y(r
) < (y
+ h
) && (Y(r
) + HEIGHT(r
)) > y
) {
11344 if (r
->ws
->r
!= NULL
)
11345 r
->ws
->old_r
= r
->ws
->r
;
11348 xcb_destroy_window(conn
, r
->id
);
11349 r
->id
= XCB_WINDOW_NONE
;
11350 TAILQ_REMOVE(&s
->rl
, r
, entry
);
11351 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
11355 /* search old regions for one to reuse */
11357 /* size + location match */
11358 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11359 if (X(r
) == x
&& Y(r
) == y
&&
11360 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11364 TAILQ_FOREACH(r
, &s
->orl
, entry
)
11365 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
11369 TAILQ_REMOVE(&s
->orl
, r
, entry
);
11370 /* try to use old region's workspace */
11371 if (r
->ws
->r
== NULL
)
11374 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
11375 err(1, "new_region: calloc: failed to allocate memory "
11378 /* if we don't have a workspace already, find one */
11380 for (i
= 0; i
< workspace_limit
; i
++)
11381 if (s
->ws
[i
].r
== NULL
) {
11388 errx(1, "new_region: no free workspaces");
11390 if (ws
->state
== SWM_WS_STATE_HIDDEN
)
11391 ws
->state
= SWM_WS_STATE_MAPPING
;
11400 r
->ws_prior
= NULL
;
11403 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
11405 /* Invisible region window to detect pointer events on empty regions. */
11406 r
->id
= xcb_generate_id(conn
);
11407 wa
[0] = XCB_EVENT_MASK_POINTER_MOTION
|
11408 XCB_EVENT_MASK_POINTER_MOTION_HINT
;
11410 xcb_create_window(conn
, XCB_COPY_FROM_PARENT
, r
->id
, r
->s
->root
,
11411 X(r
), Y(r
), WIDTH(r
), HEIGHT(r
), 0, XCB_WINDOW_CLASS_INPUT_ONLY
,
11412 XCB_COPY_FROM_PARENT
, XCB_CW_EVENT_MASK
, wa
);
11414 /* Make sure region input is at the bottom. */
11415 wa
[0] = XCB_STACK_MODE_BELOW
;
11416 xcb_configure_window(conn
, r
->id
, XCB_CONFIG_WINDOW_STACK_MODE
, wa
);
11418 xcb_map_window(conn
, r
->id
);
11422 scan_randr(int idx
)
11424 #ifdef SWM_XRR_HAS_CRTC
11427 #endif /* SWM_XRR_HAS_CRTC */
11428 struct swm_region
*r
;
11429 struct ws_win
*win
;
11431 xcb_randr_get_screen_resources_current_cookie_t src
;
11432 xcb_randr_get_screen_resources_current_reply_t
*srr
;
11433 xcb_randr_get_crtc_info_cookie_t cic
;
11434 xcb_randr_get_crtc_info_reply_t
*cir
= NULL
;
11435 xcb_randr_crtc_t
*crtc
;
11436 xcb_screen_t
*screen
;
11438 DNPRINTF(SWM_D_MISC
, "scan_randr: screen: %d\n", idx
);
11440 if ((screen
= get_screen(idx
)) == NULL
)
11441 errx(1, "ERROR: can't get screen %d.", idx
);
11443 num_screens
= get_screen_count();
11444 if (idx
>= num_screens
)
11445 errx(1, "scan_randr: invalid screen");
11447 /* remove any old regions */
11448 while ((r
= TAILQ_FIRST(&screens
[idx
].rl
)) != NULL
) {
11449 r
->ws
->old_r
= r
->ws
->r
= NULL
;
11451 xcb_destroy_window(conn
, r
->id
);
11452 r
->id
= XCB_WINDOW_NONE
;
11453 TAILQ_REMOVE(&screens
[idx
].rl
, r
, entry
);
11454 TAILQ_INSERT_TAIL(&screens
[idx
].orl
, r
, entry
);
11458 /* map virtual screens onto physical screens */
11459 #ifdef SWM_XRR_HAS_CRTC
11460 if (randr_support
) {
11461 src
= xcb_randr_get_screen_resources_current(conn
,
11462 screens
[idx
].root
);
11463 srr
= xcb_randr_get_screen_resources_current_reply(conn
, src
,
11466 new_region(&screens
[idx
], 0, 0,
11467 screen
->width_in_pixels
,
11468 screen
->height_in_pixels
);
11471 ncrtc
= srr
->num_crtcs
;
11473 crtc
= xcb_randr_get_screen_resources_current_crtcs(srr
);
11474 for (c
= 0; c
< ncrtc
; c
++) {
11475 cic
= xcb_randr_get_crtc_info(conn
, crtc
[c
],
11477 cir
= xcb_randr_get_crtc_info_reply(conn
, cic
, NULL
);
11480 if (cir
->num_outputs
== 0) {
11485 if (cir
->mode
== 0)
11486 new_region(&screens
[idx
], 0, 0,
11487 screen
->width_in_pixels
,
11488 screen
->height_in_pixels
);
11490 new_region(&screens
[idx
],
11491 cir
->x
, cir
->y
, cir
->width
, cir
->height
);
11496 #endif /* SWM_XRR_HAS_CRTC */
11498 /* If detection failed, create a single region that spans the screen. */
11499 if (TAILQ_EMPTY(&screens
[idx
].rl
))
11500 new_region(&screens
[idx
], 0, 0, screen
->width_in_pixels
,
11501 screen
->height_in_pixels
);
11504 /* Cleanup unused previously visible workspaces. */
11505 TAILQ_FOREACH(r
, &screens
[idx
].orl
, entry
) {
11506 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
11508 r
->ws
->state
= SWM_WS_STATE_HIDDEN
;
11510 /* The screen shouldn't focus on an unused region. */
11511 if (screens
[idx
].r_focus
== r
)
11512 screens
[idx
].r_focus
= NULL
;
11515 DNPRINTF(SWM_D_MISC
, "scan_randr: done.\n");
11519 screenchange(xcb_randr_screen_change_notify_event_t
*e
)
11521 struct swm_region
*r
;
11522 int i
, num_screens
;
11524 DNPRINTF(SWM_D_EVENT
, "screenchange: root: %#x\n", e
->root
);
11526 num_screens
= get_screen_count();
11527 /* silly event doesn't include the screen index */
11528 for (i
= 0; i
< num_screens
; i
++)
11529 if (screens
[i
].root
== e
->root
)
11531 if (i
>= num_screens
)
11532 errx(1, "screenchange: screen not found");
11534 /* brute force for now, just re-enumerate the regions */
11538 print_win_geom(e
->root
);
11540 /* add bars to all regions */
11541 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11544 /* Stack all regions. */
11545 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
11548 /* Make sure a region has focus. */
11549 if (screens
[i
].r_focus
== NULL
) {
11550 r
= TAILQ_FIRST(&screens
[i
].rl
);
11557 /* Update workspace state and bar on all regions. */
11558 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11559 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
11567 struct swm_region
*r
= NULL
;
11568 xcb_query_tree_cookie_t qtc
;
11569 xcb_query_tree_reply_t
*qtr
;
11570 xcb_get_property_cookie_t pc
;
11571 xcb_get_property_reply_t
*pr
;
11572 xcb_window_t
*wins
= NULL
, trans
, *cwins
= NULL
;
11573 int i
, j
, k
, n
, no
, num_screens
;
11575 DNPRINTF(SWM_D_INIT
, "grab_windows: begin\n");
11576 num_screens
= get_screen_count();
11577 for (i
= 0; i
< num_screens
; i
++) {
11578 qtc
= xcb_query_tree(conn
, screens
[i
].root
);
11579 qtr
= xcb_query_tree_reply(conn
, qtc
, NULL
);
11582 wins
= xcb_query_tree_children(qtr
);
11583 no
= xcb_query_tree_children_length(qtr
);
11585 /* Try to sort windows according to _NET_CLIENT_LIST. */
11586 pr
= xcb_get_property_reply(conn
, xcb_get_property(conn
, 0,
11587 screens
[i
].root
, ewmh
[_NET_CLIENT_LIST
].atom
,
11588 XCB_ATOM_WINDOW
, 0, UINT32_MAX
), NULL
);
11590 cwins
= xcb_get_property_value(pr
);
11591 n
= xcb_get_property_value_length(pr
) /
11592 sizeof(xcb_atom_t
);
11594 for (j
= 0; j
< n
; ++j
) {
11595 for (k
= j
; k
< no
; ++k
) {
11596 if (wins
[k
] == cwins
[j
]) {
11597 /* Swap wins j and k. */
11599 wins
[j
] = cwins
[j
];
11607 /* Manage top-level windows first, then transients. */
11608 /* TODO: allow transients to be managed before leader. */
11609 DNPRINTF(SWM_D_INIT
, "grab_windows: grab top-level windows.\n");
11610 for (j
= 0; j
< no
; j
++) {
11611 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
11612 if (r
->id
== wins
[j
]) {
11613 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11614 "skip %#x; region input window.\n",
11617 } else if (r
->bar
->id
== wins
[j
]) {
11618 DNPRINTF(SWM_D_INIT
, "grab_windows: "
11619 "skip %#x; region bar.\n",
11628 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11629 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11631 DNPRINTF(SWM_D_INIT
, "grab_windows: skip %#x; "
11632 "is transient for %#x.\n", wins
[j
], trans
);
11636 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11639 DNPRINTF(SWM_D_INIT
, "grab_windows: grab transient windows.\n");
11640 for (j
= 0; j
< no
; j
++) {
11641 pc
= xcb_icccm_get_wm_transient_for(conn
, wins
[j
]);
11642 if (xcb_icccm_get_wm_transient_for_reply(conn
, pc
,
11644 manage_window(wins
[j
], SWM_STACK_TOP
, false);
11648 DNPRINTF(SWM_D_INIT
, "grab_windows: done.\n");
11652 setup_screens(void)
11654 int i
, j
, k
, num_screens
;
11655 struct workspace
*ws
;
11656 uint32_t gcv
[1], wa
[1];
11657 const xcb_query_extension_reply_t
*qep
;
11658 xcb_screen_t
*screen
;
11659 xcb_randr_query_version_cookie_t c
;
11660 xcb_randr_query_version_reply_t
*r
;
11662 num_screens
= get_screen_count();
11663 if ((screens
= calloc(num_screens
, sizeof(struct swm_screen
))) == NULL
)
11664 err(1, "setup_screens: calloc: failed to allocate memory for "
11667 /* Initial RandR setup. */
11668 randr_support
= false;
11669 qep
= xcb_get_extension_data(conn
, &xcb_randr_id
);
11670 if (qep
->present
) {
11671 c
= xcb_randr_query_version(conn
, 1, 1);
11672 r
= xcb_randr_query_version_reply(conn
, c
, NULL
);
11674 if (r
->major_version
>= 1) {
11675 randr_support
= true;
11676 randr_eventbase
= qep
->first_event
;
11682 wa
[0] = cursors
[XC_LEFT_PTR
].cid
;
11684 /* map physical screens */
11685 for (i
= 0; i
< num_screens
; i
++) {
11686 DNPRINTF(SWM_D_WS
, "setup_screens: init screen: %d\n", i
);
11687 screens
[i
].idx
= i
;
11688 screens
[i
].r_focus
= NULL
;
11690 TAILQ_INIT(&screens
[i
].rl
);
11691 TAILQ_INIT(&screens
[i
].orl
);
11692 if ((screen
= get_screen(i
)) == NULL
)
11693 errx(1, "ERROR: can't get screen %d.", i
);
11694 screens
[i
].root
= screen
->root
;
11696 /* set default colors */
11697 setscreencolor("red", i
, SWM_S_COLOR_FOCUS
);
11698 setscreencolor("rgb:88/88/88", i
, SWM_S_COLOR_UNFOCUS
);
11699 setscreencolor("rgb:00/80/80", i
, SWM_S_COLOR_BAR_BORDER
);
11700 setscreencolor("rgb:00/40/40", i
,
11701 SWM_S_COLOR_BAR_BORDER_UNFOCUS
);
11702 setscreencolor("black", i
, SWM_S_COLOR_BAR
);
11703 setscreencolor("rgb:a0/a0/a0", i
, SWM_S_COLOR_BAR_FONT
);
11704 setscreencolor("red", i
, SWM_S_COLOR_FOCUS_MAXIMIZED
);
11705 setscreencolor("rgb:88/88/88", i
,
11706 SWM_S_COLOR_UNFOCUS_MAXIMIZED
);
11708 /* create graphics context on screen */
11709 screens
[i
].bar_gc
= xcb_generate_id(conn
);
11711 xcb_create_gc(conn
, screens
[i
].bar_gc
, screens
[i
].root
,
11712 XCB_GC_GRAPHICS_EXPOSURES
, gcv
);
11714 /* set default cursor */
11715 xcb_change_window_attributes(conn
, screens
[i
].root
,
11716 XCB_CW_CURSOR
, wa
);
11718 /* init all workspaces */
11719 /* XXX these should be dynamically allocated too */
11720 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
11721 ws
= &screens
[i
].ws
[j
];
11724 ws
->bar_enabled
= true;
11726 ws
->focus_prev
= NULL
;
11727 ws
->focus_pending
= NULL
;
11730 ws
->state
= SWM_WS_STATE_HIDDEN
;
11731 TAILQ_INIT(&ws
->stack
);
11732 TAILQ_INIT(&ws
->winlist
);
11733 TAILQ_INIT(&ws
->unmanagedlist
);
11735 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
11736 if (layouts
[k
].l_config
!= NULL
)
11737 layouts
[k
].l_config(ws
,
11738 SWM_ARG_ID_STACKINIT
);
11739 ws
->cur_layout
= &layouts
[0];
11740 ws
->cur_layout
->l_string(ws
);
11746 xcb_randr_select_input(conn
, screens
[i
].root
,
11747 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE
);
11752 setup_globals(void)
11754 if ((bar_fonts
= strdup(SWM_BAR_FONTS
)) == NULL
)
11755 err(1, "setup_globals: strdup: failed to allocate memory.");
11757 if ((clock_format
= strdup("%a %b %d %R %Z %Y")) == NULL
)
11758 err(1, "setup_globals: strdup: failed to allocate memory.");
11760 if ((syms
= xcb_key_symbols_alloc(conn
)) == NULL
)
11761 errx(1, "unable to allocate key symbols");
11763 a_state
= get_atom_from_string("WM_STATE");
11764 a_prot
= get_atom_from_string("WM_PROTOCOLS");
11765 a_delete
= get_atom_from_string("WM_DELETE_WINDOW");
11766 a_net_frame_extents
= get_atom_from_string("_NET_FRAME_EXTENTS");
11767 a_net_supported
= get_atom_from_string("_NET_SUPPORTED");
11768 a_net_wm_check
= get_atom_from_string("_NET_SUPPORTING_WM_CHECK");
11769 a_takefocus
= get_atom_from_string("WM_TAKE_FOCUS");
11770 a_utf8_string
= get_atom_from_string("UTF8_STRING");
11771 a_swm_ws
= get_atom_from_string("_SWM_WS");
11775 shutdown_cleanup(void)
11777 struct swm_region
*r
;
11779 struct workspace
*ws
;
11780 int i
, num_screens
;
11782 /* disable alarm because the following code may not be interrupted */
11784 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
11785 err(1, "can't disable alarm");
11798 num_screens
= get_screen_count();
11799 for (i
= 0; i
< num_screens
; ++i
) {
11802 DNPRINTF(SWM_D_FOCUS
, "shutdown_cleanup: set_input_focus: "
11803 "%#x, revert-to: root, time: 0\n", screens
[i
].root
);
11804 xcb_set_input_focus(conn
, XCB_INPUT_FOCUS_POINTER_ROOT
,
11805 screens
[i
].root
, XCB_CURRENT_TIME
);
11807 if (screens
[i
].bar_gc
!= XCB_NONE
)
11808 xcb_free_gc(conn
, screens
[i
].bar_gc
);
11809 if (!bar_font_legacy
) {
11810 XftColorFree(display
, DefaultVisual(display
, i
),
11811 DefaultColormap(display
, i
), &bar_font_color
);
11812 XftColorFree(display
, DefaultVisual(display
, i
),
11813 DefaultColormap(display
, i
), &search_font_color
);
11816 for (j
= 0; j
< SWM_S_COLOR_MAX
; ++j
) {
11817 free(screens
[i
].c
[j
].name
);
11820 /* Free window memory. */
11821 for (j
= 0; j
< SWM_WS_MAX
; ++j
) {
11822 ws
= &screens
[i
].ws
[j
];
11825 while ((w
= TAILQ_FIRST(&ws
->winlist
)) != NULL
) {
11826 TAILQ_REMOVE(&ws
->winlist
, w
, entry
);
11830 while ((w
= TAILQ_FIRST(&ws
->unmanagedlist
)) != NULL
) {
11831 TAILQ_REMOVE(&ws
->unmanagedlist
, w
, entry
);
11836 /* Free region memory. */
11837 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
11838 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
11843 while ((r
= TAILQ_FIRST(&screens
[i
].orl
)) != NULL
) {
11844 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
11853 free(clock_format
);
11854 free(startup_exception
);
11857 XFreeFontSet(display
, bar_fs
);
11859 XftFontClose(display
, bar_font
);
11861 xcb_key_symbols_free(syms
);
11863 xcb_aux_sync(conn
);
11864 xcb_disconnect(conn
);
11868 event_error(xcb_generic_error_t
*e
)
11872 DNPRINTF(SWM_D_EVENT
, "event_error: %s(%u) from %s(%u), sequence: %u, "
11873 "resource_id: %u, minor_code: %u\n",
11874 xcb_event_get_error_label(e
->error_code
), e
->error_code
,
11875 xcb_event_get_request_label(e
->major_code
), e
->major_code
,
11876 e
->sequence
, e
->resource_id
, e
->minor_code
);
11880 event_handle(xcb_generic_event_t
*evt
)
11882 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(evt
);
11884 DNPRINTF(SWM_D_EVENT
, "XCB Event: %s(%d), seq %u\n",
11885 xcb_event_get_label(XCB_EVENT_RESPONSE_TYPE(evt
)),
11886 XCB_EVENT_RESPONSE_TYPE(evt
), evt
->sequence
);
11889 #define EVENT(type, callback) case type: callback((void *)evt); return
11890 EVENT(0, event_error
);
11891 EVENT(XCB_BUTTON_PRESS
, buttonpress
);
11892 EVENT(XCB_BUTTON_RELEASE
, buttonrelease
);
11893 /*EVENT(XCB_CIRCULATE_NOTIFY, );*/
11894 /*EVENT(XCB_CIRCULATE_REQUEST, );*/
11895 EVENT(XCB_CLIENT_MESSAGE
, clientmessage
);
11896 /*EVENT(XCB_COLORMAP_NOTIFY, );*/
11897 EVENT(XCB_CONFIGURE_NOTIFY
, configurenotify
);
11898 EVENT(XCB_CONFIGURE_REQUEST
, configurerequest
);
11899 /*EVENT(XCB_CREATE_NOTIFY, );*/
11900 EVENT(XCB_DESTROY_NOTIFY
, destroynotify
);
11901 EVENT(XCB_ENTER_NOTIFY
, enternotify
);
11902 EVENT(XCB_EXPOSE
, expose
);
11903 EVENT(XCB_FOCUS_IN
, focusin
);
11905 EVENT(XCB_FOCUS_OUT
, focusout
);
11907 /*EVENT(XCB_GRAPHICS_EXPOSURE, );*/
11908 /*EVENT(XCB_GRAVITY_NOTIFY, );*/
11909 EVENT(XCB_KEY_PRESS
, keypress
);
11910 EVENT(XCB_KEY_RELEASE
, keyrelease
);
11911 /*EVENT(XCB_KEYMAP_NOTIFY, );*/
11913 EVENT(XCB_LEAVE_NOTIFY
, leavenotify
);
11915 EVENT(XCB_MAP_NOTIFY
, mapnotify
);
11916 EVENT(XCB_MAP_REQUEST
, maprequest
);
11917 EVENT(XCB_MAPPING_NOTIFY
, mappingnotify
);
11918 EVENT(XCB_MOTION_NOTIFY
, motionnotify
);
11919 /*EVENT(XCB_NO_EXPOSURE, );*/
11920 EVENT(XCB_PROPERTY_NOTIFY
, propertynotify
);
11921 EVENT(XCB_REPARENT_NOTIFY
, reparentnotify
);
11922 /*EVENT(XCB_RESIZE_REQUEST, );*/
11923 /*EVENT(XCB_SELECTION_CLEAR, );*/
11924 /*EVENT(XCB_SELECTION_NOTIFY, );*/
11925 /*EVENT(XCB_SELECTION_REQUEST, );*/
11926 EVENT(XCB_UNMAP_NOTIFY
, unmapnotify
);
11927 /*EVENT(XCB_VISIBILITY_NOTIFY, );*/
11930 if (type
- randr_eventbase
== XCB_RANDR_SCREEN_CHANGE_NOTIFY
)
11931 screenchange((void *)evt
);
11935 main(int argc
, char *argv
[])
11937 struct pollfd pfd
[2];
11938 struct sigaction sact
;
11940 struct passwd
*pwd
;
11941 struct swm_region
*r
;
11942 xcb_generic_event_t
*evt
;
11943 int xfd
, i
, num_screens
, num_readable
;
11944 char conf
[PATH_MAX
], *cfile
= NULL
;
11945 bool stdin_ready
= false, startup
= true;
11947 /* suppress unused warning since var is needed */
11951 time_started
= time(NULL
);
11955 warnx("Welcome to spectrwm V%s Build: %s", SPECTRWM_VERSION
, buildstr
);
11956 if (setlocale(LC_CTYPE
, "") == NULL
|| setlocale(LC_TIME
, "") == NULL
)
11957 warnx("no locale support");
11959 /* handle some signals */
11960 bzero(&sact
, sizeof(sact
));
11961 sigemptyset(&sact
.sa_mask
);
11963 sact
.sa_handler
= sighdlr
;
11964 sigaction(SIGINT
, &sact
, NULL
);
11965 sigaction(SIGQUIT
, &sact
, NULL
);
11966 sigaction(SIGTERM
, &sact
, NULL
);
11967 sigaction(SIGHUP
, &sact
, NULL
);
11969 sact
.sa_handler
= sighdlr
;
11970 sact
.sa_flags
= SA_NOCLDSTOP
;
11971 sigaction(SIGCHLD
, &sact
, NULL
);
11973 if ((display
= XOpenDisplay(0)) == NULL
)
11974 errx(1, "can not open display");
11976 conn
= XGetXCBConnection(display
);
11977 if (xcb_connection_has_error(conn
))
11978 errx(1, "can not get XCB connection");
11980 XSetEventQueueOwner(display
, XCBOwnsEventQueue
);
11982 xcb_prefetch_extension_data(conn
, &xcb_randr_id
);
11983 xfd
= xcb_get_file_descriptor(conn
);
11985 /* look for local and global conf file */
11986 pwd
= getpwuid(getuid());
11988 errx(1, "invalid user: %d", getuid());
11990 xcb_grab_server(conn
);
11991 xcb_aux_sync(conn
);
11993 /* flush all events */
11994 while ((evt
= get_next_event(false))) {
11995 if (XCB_EVENT_RESPONSE_TYPE(evt
) == 0)
12001 errx(1, "another window manager is currently running");
12003 /* Load Xcursors and/or cursorfont glyph cursors. */
12006 xcb_aux_sync(conn
);
12011 setup_keybindings();
12012 setup_btnbindings();
12017 for (i
= 0; ; i
++) {
12022 snprintf(conf
, sizeof conf
, "%s/.%s",
12023 pwd
->pw_dir
, SWM_CONF_FILE
);
12027 snprintf(conf
, sizeof conf
, "/etc/%s",
12032 snprintf(conf
, sizeof conf
, "%s/.%s",
12033 pwd
->pw_dir
, SWM_CONF_FILE_OLD
);
12036 /* global compat */
12037 snprintf(conf
, sizeof conf
, "/etc/%s",
12038 SWM_CONF_FILE_OLD
);
12044 if (strlen(conf
) && stat(conf
, &sb
) != -1)
12045 if (S_ISREG(sb
.st_mode
)) {
12052 /* load conf (if any) */
12054 conf_load(cfile
, SWM_CONF_DEFAULT
);
12058 if (getenv("SWM_STARTED") == NULL
)
12059 setenv("SWM_STARTED", "YES", 1);
12061 /* setup all bars */
12062 num_screens
= get_screen_count();
12063 for (i
= 0; i
< num_screens
; i
++)
12064 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12067 /* Manage existing windows. */
12073 /* Stack all regions to trigger mapping. */
12074 for (i
= 0; i
< num_screens
; i
++)
12075 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12078 xcb_ungrab_server(conn
);
12081 /* Update state and bar of each newly mapped workspace. */
12082 for (i
= 0; i
< num_screens
; i
++)
12083 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
12084 r
->ws
->state
= SWM_WS_STATE_MAPPED
;
12088 memset(&pfd
, 0, sizeof(pfd
));
12090 pfd
[0].events
= POLLIN
;
12091 pfd
[1].fd
= STDIN_FILENO
;
12092 pfd
[1].events
= POLLIN
;
12095 while ((evt
= get_next_event(false))) {
12102 /* If just (re)started, set default focus if needed. */
12106 if (focus_mode
!= SWM_FOCUS_FOLLOW
) {
12107 r
= TAILQ_FIRST(&screens
[0].rl
);
12119 num_readable
= poll(pfd
, bar_extra
? 2 : 1, 1000);
12120 if (num_readable
== -1) {
12121 DNPRINTF(SWM_D_MISC
, "poll failed: %s",
12123 } else if (num_readable
> 0 && bar_extra
&&
12124 pfd
[1].revents
& POLLIN
) {
12125 stdin_ready
= true;
12129 restart(NULL
, NULL
, NULL
);
12135 stdin_ready
= false;
12136 bar_extra_update();
12139 /* Need to ensure the bar(s) are always updated. */
12140 for (i
= 0; i
< num_screens
; i
++)
12141 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
12147 shutdown_cleanup();